1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * User interface for Resource Allocation in Resource Director Technology(RDT)
4 *
5 * Copyright (C) 2016 Intel Corporation
6 *
7 * Author: Fenghua Yu <fenghua.yu@intel.com>
8 *
9 * More information about RDT be found in the Intel (R) x86 Architecture
10 * Software Developer Manual.
11 */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/cacheinfo.h>
16 #include <linux/cpu.h>
17 #include <linux/debugfs.h>
18 #include <linux/fs.h>
19 #include <linux/fs_parser.h>
20 #include <linux/sysfs.h>
21 #include <linux/kernfs.h>
22 #include <linux/seq_buf.h>
23 #include <linux/seq_file.h>
24 #include <linux/sched/signal.h>
25 #include <linux/sched/task.h>
26 #include <linux/slab.h>
27 #include <linux/task_work.h>
28 #include <linux/user_namespace.h>
29
30 #include <uapi/linux/magic.h>
31
32 #include <asm/resctrl.h>
33 #include "internal.h"
34
35 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
36 DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
37 DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
38 static struct kernfs_root *rdt_root;
39 struct rdtgroup rdtgroup_default;
40 LIST_HEAD(rdt_all_groups);
41
42 /* list of entries for the schemata file */
43 LIST_HEAD(resctrl_schema_all);
44
45 /* Kernel fs node for "info" directory under root */
46 static struct kernfs_node *kn_info;
47
48 /* Kernel fs node for "mon_groups" directory under root */
49 static struct kernfs_node *kn_mongrp;
50
51 /* Kernel fs node for "mon_data" directory under root */
52 static struct kernfs_node *kn_mondata;
53
54 static struct seq_buf last_cmd_status;
55 static char last_cmd_status_buf[512];
56
57 struct dentry *debugfs_resctrl;
58
rdt_last_cmd_clear(void)59 void rdt_last_cmd_clear(void)
60 {
61 lockdep_assert_held(&rdtgroup_mutex);
62 seq_buf_clear(&last_cmd_status);
63 }
64
rdt_last_cmd_puts(const char * s)65 void rdt_last_cmd_puts(const char *s)
66 {
67 lockdep_assert_held(&rdtgroup_mutex);
68 seq_buf_puts(&last_cmd_status, s);
69 }
70
rdt_last_cmd_printf(const char * fmt,...)71 void rdt_last_cmd_printf(const char *fmt, ...)
72 {
73 va_list ap;
74
75 va_start(ap, fmt);
76 lockdep_assert_held(&rdtgroup_mutex);
77 seq_buf_vprintf(&last_cmd_status, fmt, ap);
78 va_end(ap);
79 }
80
rdt_staged_configs_clear(void)81 void rdt_staged_configs_clear(void)
82 {
83 struct rdt_resource *r;
84 struct rdt_domain *dom;
85
86 lockdep_assert_held(&rdtgroup_mutex);
87
88 for_each_alloc_capable_rdt_resource(r) {
89 list_for_each_entry(dom, &r->domains, list)
90 memset(dom->staged_config, 0, sizeof(dom->staged_config));
91 }
92 }
93
94 /*
95 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
96 * we can keep a bitmap of free CLOSIDs in a single integer.
97 *
98 * Using a global CLOSID across all resources has some advantages and
99 * some drawbacks:
100 * + We can simply set "current->closid" to assign a task to a resource
101 * group.
102 * + Context switch code can avoid extra memory references deciding which
103 * CLOSID to load into the PQR_ASSOC MSR
104 * - We give up some options in configuring resource groups across multi-socket
105 * systems.
106 * - Our choices on how to configure each resource become progressively more
107 * limited as the number of resources grows.
108 */
109 static int closid_free_map;
110 static int closid_free_map_len;
111
closids_supported(void)112 int closids_supported(void)
113 {
114 return closid_free_map_len;
115 }
116
closid_init(void)117 static void closid_init(void)
118 {
119 struct resctrl_schema *s;
120 u32 rdt_min_closid = 32;
121
122 /* Compute rdt_min_closid across all resources */
123 list_for_each_entry(s, &resctrl_schema_all, list)
124 rdt_min_closid = min(rdt_min_closid, s->num_closid);
125
126 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
127
128 /* CLOSID 0 is always reserved for the default group */
129 closid_free_map &= ~1;
130 closid_free_map_len = rdt_min_closid;
131 }
132
closid_alloc(void)133 static int closid_alloc(void)
134 {
135 u32 closid = ffs(closid_free_map);
136
137 if (closid == 0)
138 return -ENOSPC;
139 closid--;
140 closid_free_map &= ~(1 << closid);
141
142 return closid;
143 }
144
closid_free(int closid)145 void closid_free(int closid)
146 {
147 closid_free_map |= 1 << closid;
148 }
149
150 /**
151 * closid_allocated - test if provided closid is in use
152 * @closid: closid to be tested
153 *
154 * Return: true if @closid is currently associated with a resource group,
155 * false if @closid is free
156 */
closid_allocated(unsigned int closid)157 static bool closid_allocated(unsigned int closid)
158 {
159 return (closid_free_map & (1 << closid)) == 0;
160 }
161
162 /**
163 * rdtgroup_mode_by_closid - Return mode of resource group with closid
164 * @closid: closid if the resource group
165 *
166 * Each resource group is associated with a @closid. Here the mode
167 * of a resource group can be queried by searching for it using its closid.
168 *
169 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
170 */
rdtgroup_mode_by_closid(int closid)171 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
172 {
173 struct rdtgroup *rdtgrp;
174
175 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
176 if (rdtgrp->closid == closid)
177 return rdtgrp->mode;
178 }
179
180 return RDT_NUM_MODES;
181 }
182
183 static const char * const rdt_mode_str[] = {
184 [RDT_MODE_SHAREABLE] = "shareable",
185 [RDT_MODE_EXCLUSIVE] = "exclusive",
186 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
187 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
188 };
189
190 /**
191 * rdtgroup_mode_str - Return the string representation of mode
192 * @mode: the resource group mode as &enum rdtgroup_mode
193 *
194 * Return: string representation of valid mode, "unknown" otherwise
195 */
rdtgroup_mode_str(enum rdtgrp_mode mode)196 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
197 {
198 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
199 return "unknown";
200
201 return rdt_mode_str[mode];
202 }
203
204 /* set uid and gid of rdtgroup dirs and files to that of the creator */
rdtgroup_kn_set_ugid(struct kernfs_node * kn)205 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
206 {
207 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
208 .ia_uid = current_fsuid(),
209 .ia_gid = current_fsgid(), };
210
211 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
212 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
213 return 0;
214
215 return kernfs_setattr(kn, &iattr);
216 }
217
rdtgroup_add_file(struct kernfs_node * parent_kn,struct rftype * rft)218 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
219 {
220 struct kernfs_node *kn;
221 int ret;
222
223 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
224 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
225 0, rft->kf_ops, rft, NULL, NULL);
226 if (IS_ERR(kn))
227 return PTR_ERR(kn);
228
229 ret = rdtgroup_kn_set_ugid(kn);
230 if (ret) {
231 kernfs_remove(kn);
232 return ret;
233 }
234
235 return 0;
236 }
237
rdtgroup_seqfile_show(struct seq_file * m,void * arg)238 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
239 {
240 struct kernfs_open_file *of = m->private;
241 struct rftype *rft = of->kn->priv;
242
243 if (rft->seq_show)
244 return rft->seq_show(of, m, arg);
245 return 0;
246 }
247
rdtgroup_file_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)248 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
249 size_t nbytes, loff_t off)
250 {
251 struct rftype *rft = of->kn->priv;
252
253 if (rft->write)
254 return rft->write(of, buf, nbytes, off);
255
256 return -EINVAL;
257 }
258
259 static const struct kernfs_ops rdtgroup_kf_single_ops = {
260 .atomic_write_len = PAGE_SIZE,
261 .write = rdtgroup_file_write,
262 .seq_show = rdtgroup_seqfile_show,
263 };
264
265 static const struct kernfs_ops kf_mondata_ops = {
266 .atomic_write_len = PAGE_SIZE,
267 .seq_show = rdtgroup_mondata_show,
268 };
269
is_cpu_list(struct kernfs_open_file * of)270 static bool is_cpu_list(struct kernfs_open_file *of)
271 {
272 struct rftype *rft = of->kn->priv;
273
274 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
275 }
276
rdtgroup_cpus_show(struct kernfs_open_file * of,struct seq_file * s,void * v)277 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
278 struct seq_file *s, void *v)
279 {
280 struct rdtgroup *rdtgrp;
281 struct cpumask *mask;
282 int ret = 0;
283
284 rdtgrp = rdtgroup_kn_lock_live(of->kn);
285
286 if (rdtgrp) {
287 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
288 if (!rdtgrp->plr->d) {
289 rdt_last_cmd_clear();
290 rdt_last_cmd_puts("Cache domain offline\n");
291 ret = -ENODEV;
292 } else {
293 mask = &rdtgrp->plr->d->cpu_mask;
294 seq_printf(s, is_cpu_list(of) ?
295 "%*pbl\n" : "%*pb\n",
296 cpumask_pr_args(mask));
297 }
298 } else {
299 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
300 cpumask_pr_args(&rdtgrp->cpu_mask));
301 }
302 } else {
303 ret = -ENOENT;
304 }
305 rdtgroup_kn_unlock(of->kn);
306
307 return ret;
308 }
309
310 /*
311 * This is safe against resctrl_sched_in() called from __switch_to()
312 * because __switch_to() is executed with interrupts disabled. A local call
313 * from update_closid_rmid() is protected against __switch_to() because
314 * preemption is disabled.
315 */
update_cpu_closid_rmid(void * info)316 static void update_cpu_closid_rmid(void *info)
317 {
318 struct rdtgroup *r = info;
319
320 if (r) {
321 this_cpu_write(pqr_state.default_closid, r->closid);
322 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
323 }
324
325 /*
326 * We cannot unconditionally write the MSR because the current
327 * executing task might have its own closid selected. Just reuse
328 * the context switch code.
329 */
330 resctrl_sched_in(current);
331 }
332
333 /*
334 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
335 *
336 * Per task closids/rmids must have been set up before calling this function.
337 */
338 static void
update_closid_rmid(const struct cpumask * cpu_mask,struct rdtgroup * r)339 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
340 {
341 int cpu = get_cpu();
342
343 if (cpumask_test_cpu(cpu, cpu_mask))
344 update_cpu_closid_rmid(r);
345 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
346 put_cpu();
347 }
348
cpus_mon_write(struct rdtgroup * rdtgrp,cpumask_var_t newmask,cpumask_var_t tmpmask)349 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
350 cpumask_var_t tmpmask)
351 {
352 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
353 struct list_head *head;
354
355 /* Check whether cpus belong to parent ctrl group */
356 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
357 if (cpumask_weight(tmpmask)) {
358 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
359 return -EINVAL;
360 }
361
362 /* Check whether cpus are dropped from this group */
363 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
364 if (cpumask_weight(tmpmask)) {
365 /* Give any dropped cpus to parent rdtgroup */
366 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
367 update_closid_rmid(tmpmask, prgrp);
368 }
369
370 /*
371 * If we added cpus, remove them from previous group that owned them
372 * and update per-cpu rmid
373 */
374 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
375 if (cpumask_weight(tmpmask)) {
376 head = &prgrp->mon.crdtgrp_list;
377 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
378 if (crgrp == rdtgrp)
379 continue;
380 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
381 tmpmask);
382 }
383 update_closid_rmid(tmpmask, rdtgrp);
384 }
385
386 /* Done pushing/pulling - update this group with new mask */
387 cpumask_copy(&rdtgrp->cpu_mask, newmask);
388
389 return 0;
390 }
391
cpumask_rdtgrp_clear(struct rdtgroup * r,struct cpumask * m)392 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
393 {
394 struct rdtgroup *crgrp;
395
396 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
397 /* update the child mon group masks as well*/
398 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
399 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
400 }
401
cpus_ctrl_write(struct rdtgroup * rdtgrp,cpumask_var_t newmask,cpumask_var_t tmpmask,cpumask_var_t tmpmask1)402 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
403 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
404 {
405 struct rdtgroup *r, *crgrp;
406 struct list_head *head;
407
408 /* Check whether cpus are dropped from this group */
409 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
410 if (cpumask_weight(tmpmask)) {
411 /* Can't drop from default group */
412 if (rdtgrp == &rdtgroup_default) {
413 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
414 return -EINVAL;
415 }
416
417 /* Give any dropped cpus to rdtgroup_default */
418 cpumask_or(&rdtgroup_default.cpu_mask,
419 &rdtgroup_default.cpu_mask, tmpmask);
420 update_closid_rmid(tmpmask, &rdtgroup_default);
421 }
422
423 /*
424 * If we added cpus, remove them from previous group and
425 * the prev group's child groups that owned them
426 * and update per-cpu closid/rmid.
427 */
428 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
429 if (cpumask_weight(tmpmask)) {
430 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
431 if (r == rdtgrp)
432 continue;
433 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
434 if (cpumask_weight(tmpmask1))
435 cpumask_rdtgrp_clear(r, tmpmask1);
436 }
437 update_closid_rmid(tmpmask, rdtgrp);
438 }
439
440 /* Done pushing/pulling - update this group with new mask */
441 cpumask_copy(&rdtgrp->cpu_mask, newmask);
442
443 /*
444 * Clear child mon group masks since there is a new parent mask
445 * now and update the rmid for the cpus the child lost.
446 */
447 head = &rdtgrp->mon.crdtgrp_list;
448 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
449 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
450 update_closid_rmid(tmpmask, rdtgrp);
451 cpumask_clear(&crgrp->cpu_mask);
452 }
453
454 return 0;
455 }
456
rdtgroup_cpus_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)457 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
458 char *buf, size_t nbytes, loff_t off)
459 {
460 cpumask_var_t tmpmask, newmask, tmpmask1;
461 struct rdtgroup *rdtgrp;
462 int ret;
463
464 if (!buf)
465 return -EINVAL;
466
467 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
468 return -ENOMEM;
469 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
470 free_cpumask_var(tmpmask);
471 return -ENOMEM;
472 }
473 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
474 free_cpumask_var(tmpmask);
475 free_cpumask_var(newmask);
476 return -ENOMEM;
477 }
478
479 rdtgrp = rdtgroup_kn_lock_live(of->kn);
480 if (!rdtgrp) {
481 ret = -ENOENT;
482 goto unlock;
483 }
484
485 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
486 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
487 ret = -EINVAL;
488 rdt_last_cmd_puts("Pseudo-locking in progress\n");
489 goto unlock;
490 }
491
492 if (is_cpu_list(of))
493 ret = cpulist_parse(buf, newmask);
494 else
495 ret = cpumask_parse(buf, newmask);
496
497 if (ret) {
498 rdt_last_cmd_puts("Bad CPU list/mask\n");
499 goto unlock;
500 }
501
502 /* check that user didn't specify any offline cpus */
503 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
504 if (cpumask_weight(tmpmask)) {
505 ret = -EINVAL;
506 rdt_last_cmd_puts("Can only assign online CPUs\n");
507 goto unlock;
508 }
509
510 if (rdtgrp->type == RDTCTRL_GROUP)
511 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
512 else if (rdtgrp->type == RDTMON_GROUP)
513 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
514 else
515 ret = -EINVAL;
516
517 unlock:
518 rdtgroup_kn_unlock(of->kn);
519 free_cpumask_var(tmpmask);
520 free_cpumask_var(newmask);
521 free_cpumask_var(tmpmask1);
522
523 return ret ?: nbytes;
524 }
525
526 /**
527 * rdtgroup_remove - the helper to remove resource group safely
528 * @rdtgrp: resource group to remove
529 *
530 * On resource group creation via a mkdir, an extra kernfs_node reference is
531 * taken to ensure that the rdtgroup structure remains accessible for the
532 * rdtgroup_kn_unlock() calls where it is removed.
533 *
534 * Drop the extra reference here, then free the rdtgroup structure.
535 *
536 * Return: void
537 */
rdtgroup_remove(struct rdtgroup * rdtgrp)538 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
539 {
540 kernfs_put(rdtgrp->kn);
541 kfree(rdtgrp);
542 }
543
_update_task_closid_rmid(void * task)544 static void _update_task_closid_rmid(void *task)
545 {
546 /*
547 * If the task is still current on this CPU, update PQR_ASSOC MSR.
548 * Otherwise, the MSR is updated when the task is scheduled in.
549 */
550 if (task == current)
551 resctrl_sched_in(task);
552 }
553
update_task_closid_rmid(struct task_struct * t)554 static void update_task_closid_rmid(struct task_struct *t)
555 {
556 if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
557 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
558 else
559 _update_task_closid_rmid(t);
560 }
561
__rdtgroup_move_task(struct task_struct * tsk,struct rdtgroup * rdtgrp)562 static int __rdtgroup_move_task(struct task_struct *tsk,
563 struct rdtgroup *rdtgrp)
564 {
565 /* If the task is already in rdtgrp, no need to move the task. */
566 if ((rdtgrp->type == RDTCTRL_GROUP && tsk->closid == rdtgrp->closid &&
567 tsk->rmid == rdtgrp->mon.rmid) ||
568 (rdtgrp->type == RDTMON_GROUP && tsk->rmid == rdtgrp->mon.rmid &&
569 tsk->closid == rdtgrp->mon.parent->closid))
570 return 0;
571
572 /*
573 * Set the task's closid/rmid before the PQR_ASSOC MSR can be
574 * updated by them.
575 *
576 * For ctrl_mon groups, move both closid and rmid.
577 * For monitor groups, can move the tasks only from
578 * their parent CTRL group.
579 */
580
581 if (rdtgrp->type == RDTCTRL_GROUP) {
582 WRITE_ONCE(tsk->closid, rdtgrp->closid);
583 WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
584 } else if (rdtgrp->type == RDTMON_GROUP) {
585 if (rdtgrp->mon.parent->closid == tsk->closid) {
586 WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
587 } else {
588 rdt_last_cmd_puts("Can't move task to different control group\n");
589 return -EINVAL;
590 }
591 }
592
593 /*
594 * Ensure the task's closid and rmid are written before determining if
595 * the task is current that will decide if it will be interrupted.
596 * This pairs with the full barrier between the rq->curr update and
597 * resctrl_sched_in() during context switch.
598 */
599 smp_mb();
600
601 /*
602 * By now, the task's closid and rmid are set. If the task is current
603 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
604 * group go into effect. If the task is not current, the MSR will be
605 * updated when the task is scheduled in.
606 */
607 update_task_closid_rmid(tsk);
608
609 return 0;
610 }
611
is_closid_match(struct task_struct * t,struct rdtgroup * r)612 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
613 {
614 return (rdt_alloc_capable &&
615 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
616 }
617
is_rmid_match(struct task_struct * t,struct rdtgroup * r)618 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
619 {
620 return (rdt_mon_capable &&
621 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
622 }
623
624 /**
625 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
626 * @r: Resource group
627 *
628 * Return: 1 if tasks have been assigned to @r, 0 otherwise
629 */
rdtgroup_tasks_assigned(struct rdtgroup * r)630 int rdtgroup_tasks_assigned(struct rdtgroup *r)
631 {
632 struct task_struct *p, *t;
633 int ret = 0;
634
635 lockdep_assert_held(&rdtgroup_mutex);
636
637 rcu_read_lock();
638 for_each_process_thread(p, t) {
639 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
640 ret = 1;
641 break;
642 }
643 }
644 rcu_read_unlock();
645
646 return ret;
647 }
648
rdtgroup_task_write_permission(struct task_struct * task,struct kernfs_open_file * of)649 static int rdtgroup_task_write_permission(struct task_struct *task,
650 struct kernfs_open_file *of)
651 {
652 const struct cred *tcred = get_task_cred(task);
653 const struct cred *cred = current_cred();
654 int ret = 0;
655
656 /*
657 * Even if we're attaching all tasks in the thread group, we only
658 * need to check permissions on one of them.
659 */
660 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
661 !uid_eq(cred->euid, tcred->uid) &&
662 !uid_eq(cred->euid, tcred->suid)) {
663 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
664 ret = -EPERM;
665 }
666
667 put_cred(tcred);
668 return ret;
669 }
670
rdtgroup_move_task(pid_t pid,struct rdtgroup * rdtgrp,struct kernfs_open_file * of)671 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
672 struct kernfs_open_file *of)
673 {
674 struct task_struct *tsk;
675 int ret;
676
677 rcu_read_lock();
678 if (pid) {
679 tsk = find_task_by_vpid(pid);
680 if (!tsk) {
681 rcu_read_unlock();
682 rdt_last_cmd_printf("No task %d\n", pid);
683 return -ESRCH;
684 }
685 } else {
686 tsk = current;
687 }
688
689 get_task_struct(tsk);
690 rcu_read_unlock();
691
692 ret = rdtgroup_task_write_permission(tsk, of);
693 if (!ret)
694 ret = __rdtgroup_move_task(tsk, rdtgrp);
695
696 put_task_struct(tsk);
697 return ret;
698 }
699
rdtgroup_tasks_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)700 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
701 char *buf, size_t nbytes, loff_t off)
702 {
703 struct rdtgroup *rdtgrp;
704 int ret = 0;
705 pid_t pid;
706
707 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
708 return -EINVAL;
709 rdtgrp = rdtgroup_kn_lock_live(of->kn);
710 if (!rdtgrp) {
711 rdtgroup_kn_unlock(of->kn);
712 return -ENOENT;
713 }
714 rdt_last_cmd_clear();
715
716 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
717 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
718 ret = -EINVAL;
719 rdt_last_cmd_puts("Pseudo-locking in progress\n");
720 goto unlock;
721 }
722
723 ret = rdtgroup_move_task(pid, rdtgrp, of);
724
725 unlock:
726 rdtgroup_kn_unlock(of->kn);
727
728 return ret ?: nbytes;
729 }
730
show_rdt_tasks(struct rdtgroup * r,struct seq_file * s)731 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
732 {
733 struct task_struct *p, *t;
734 pid_t pid;
735
736 rcu_read_lock();
737 for_each_process_thread(p, t) {
738 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
739 pid = task_pid_vnr(t);
740 if (pid)
741 seq_printf(s, "%d\n", pid);
742 }
743 }
744 rcu_read_unlock();
745 }
746
rdtgroup_tasks_show(struct kernfs_open_file * of,struct seq_file * s,void * v)747 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
748 struct seq_file *s, void *v)
749 {
750 struct rdtgroup *rdtgrp;
751 int ret = 0;
752
753 rdtgrp = rdtgroup_kn_lock_live(of->kn);
754 if (rdtgrp)
755 show_rdt_tasks(rdtgrp, s);
756 else
757 ret = -ENOENT;
758 rdtgroup_kn_unlock(of->kn);
759
760 return ret;
761 }
762
763 #ifdef CONFIG_PROC_CPU_RESCTRL
764
765 /*
766 * A task can only be part of one resctrl control group and of one monitor
767 * group which is associated to that control group.
768 *
769 * 1) res:
770 * mon:
771 *
772 * resctrl is not available.
773 *
774 * 2) res:/
775 * mon:
776 *
777 * Task is part of the root resctrl control group, and it is not associated
778 * to any monitor group.
779 *
780 * 3) res:/
781 * mon:mon0
782 *
783 * Task is part of the root resctrl control group and monitor group mon0.
784 *
785 * 4) res:group0
786 * mon:
787 *
788 * Task is part of resctrl control group group0, and it is not associated
789 * to any monitor group.
790 *
791 * 5) res:group0
792 * mon:mon1
793 *
794 * Task is part of resctrl control group group0 and monitor group mon1.
795 */
proc_resctrl_show(struct seq_file * s,struct pid_namespace * ns,struct pid * pid,struct task_struct * tsk)796 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
797 struct pid *pid, struct task_struct *tsk)
798 {
799 struct rdtgroup *rdtg;
800 int ret = 0;
801
802 mutex_lock(&rdtgroup_mutex);
803
804 /* Return empty if resctrl has not been mounted. */
805 if (!static_branch_unlikely(&rdt_enable_key)) {
806 seq_puts(s, "res:\nmon:\n");
807 goto unlock;
808 }
809
810 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
811 struct rdtgroup *crg;
812
813 /*
814 * Task information is only relevant for shareable
815 * and exclusive groups.
816 */
817 if (rdtg->mode != RDT_MODE_SHAREABLE &&
818 rdtg->mode != RDT_MODE_EXCLUSIVE)
819 continue;
820
821 if (rdtg->closid != tsk->closid)
822 continue;
823
824 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
825 rdtg->kn->name);
826 seq_puts(s, "mon:");
827 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
828 mon.crdtgrp_list) {
829 if (tsk->rmid != crg->mon.rmid)
830 continue;
831 seq_printf(s, "%s", crg->kn->name);
832 break;
833 }
834 seq_putc(s, '\n');
835 goto unlock;
836 }
837 /*
838 * The above search should succeed. Otherwise return
839 * with an error.
840 */
841 ret = -ENOENT;
842 unlock:
843 mutex_unlock(&rdtgroup_mutex);
844
845 return ret;
846 }
847 #endif
848
rdt_last_cmd_status_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)849 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
850 struct seq_file *seq, void *v)
851 {
852 int len;
853
854 mutex_lock(&rdtgroup_mutex);
855 len = seq_buf_used(&last_cmd_status);
856 if (len)
857 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
858 else
859 seq_puts(seq, "ok\n");
860 mutex_unlock(&rdtgroup_mutex);
861 return 0;
862 }
863
rdt_num_closids_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)864 static int rdt_num_closids_show(struct kernfs_open_file *of,
865 struct seq_file *seq, void *v)
866 {
867 struct resctrl_schema *s = of->kn->parent->priv;
868
869 seq_printf(seq, "%u\n", s->num_closid);
870 return 0;
871 }
872
rdt_default_ctrl_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)873 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
874 struct seq_file *seq, void *v)
875 {
876 struct resctrl_schema *s = of->kn->parent->priv;
877 struct rdt_resource *r = s->res;
878
879 seq_printf(seq, "%x\n", r->default_ctrl);
880 return 0;
881 }
882
rdt_min_cbm_bits_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)883 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
884 struct seq_file *seq, void *v)
885 {
886 struct resctrl_schema *s = of->kn->parent->priv;
887 struct rdt_resource *r = s->res;
888
889 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
890 return 0;
891 }
892
rdt_shareable_bits_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)893 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
894 struct seq_file *seq, void *v)
895 {
896 struct resctrl_schema *s = of->kn->parent->priv;
897 struct rdt_resource *r = s->res;
898
899 seq_printf(seq, "%x\n", r->cache.shareable_bits);
900 return 0;
901 }
902
903 /**
904 * rdt_bit_usage_show - Display current usage of resources
905 *
906 * A domain is a shared resource that can now be allocated differently. Here
907 * we display the current regions of the domain as an annotated bitmask.
908 * For each domain of this resource its allocation bitmask
909 * is annotated as below to indicate the current usage of the corresponding bit:
910 * 0 - currently unused
911 * X - currently available for sharing and used by software and hardware
912 * H - currently used by hardware only but available for software use
913 * S - currently used and shareable by software only
914 * E - currently used exclusively by one resource group
915 * P - currently pseudo-locked by one resource group
916 */
rdt_bit_usage_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)917 static int rdt_bit_usage_show(struct kernfs_open_file *of,
918 struct seq_file *seq, void *v)
919 {
920 struct resctrl_schema *s = of->kn->parent->priv;
921 /*
922 * Use unsigned long even though only 32 bits are used to ensure
923 * test_bit() is used safely.
924 */
925 unsigned long sw_shareable = 0, hw_shareable = 0;
926 unsigned long exclusive = 0, pseudo_locked = 0;
927 struct rdt_resource *r = s->res;
928 struct rdt_domain *dom;
929 int i, hwb, swb, excl, psl;
930 enum rdtgrp_mode mode;
931 bool sep = false;
932 u32 ctrl_val;
933
934 mutex_lock(&rdtgroup_mutex);
935 hw_shareable = r->cache.shareable_bits;
936 list_for_each_entry(dom, &r->domains, list) {
937 if (sep)
938 seq_putc(seq, ';');
939 sw_shareable = 0;
940 exclusive = 0;
941 seq_printf(seq, "%d=", dom->id);
942 for (i = 0; i < closids_supported(); i++) {
943 if (!closid_allocated(i))
944 continue;
945 ctrl_val = resctrl_arch_get_config(r, dom, i,
946 s->conf_type);
947 mode = rdtgroup_mode_by_closid(i);
948 switch (mode) {
949 case RDT_MODE_SHAREABLE:
950 sw_shareable |= ctrl_val;
951 break;
952 case RDT_MODE_EXCLUSIVE:
953 exclusive |= ctrl_val;
954 break;
955 case RDT_MODE_PSEUDO_LOCKSETUP:
956 /*
957 * RDT_MODE_PSEUDO_LOCKSETUP is possible
958 * here but not included since the CBM
959 * associated with this CLOSID in this mode
960 * is not initialized and no task or cpu can be
961 * assigned this CLOSID.
962 */
963 break;
964 case RDT_MODE_PSEUDO_LOCKED:
965 case RDT_NUM_MODES:
966 WARN(1,
967 "invalid mode for closid %d\n", i);
968 break;
969 }
970 }
971 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
972 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
973 hwb = test_bit(i, &hw_shareable);
974 swb = test_bit(i, &sw_shareable);
975 excl = test_bit(i, &exclusive);
976 psl = test_bit(i, &pseudo_locked);
977 if (hwb && swb)
978 seq_putc(seq, 'X');
979 else if (hwb && !swb)
980 seq_putc(seq, 'H');
981 else if (!hwb && swb)
982 seq_putc(seq, 'S');
983 else if (excl)
984 seq_putc(seq, 'E');
985 else if (psl)
986 seq_putc(seq, 'P');
987 else /* Unused bits remain */
988 seq_putc(seq, '0');
989 }
990 sep = true;
991 }
992 seq_putc(seq, '\n');
993 mutex_unlock(&rdtgroup_mutex);
994 return 0;
995 }
996
rdt_min_bw_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)997 static int rdt_min_bw_show(struct kernfs_open_file *of,
998 struct seq_file *seq, void *v)
999 {
1000 struct resctrl_schema *s = of->kn->parent->priv;
1001 struct rdt_resource *r = s->res;
1002
1003 seq_printf(seq, "%u\n", r->membw.min_bw);
1004 return 0;
1005 }
1006
rdt_num_rmids_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1007 static int rdt_num_rmids_show(struct kernfs_open_file *of,
1008 struct seq_file *seq, void *v)
1009 {
1010 struct rdt_resource *r = of->kn->parent->priv;
1011
1012 seq_printf(seq, "%d\n", r->num_rmid);
1013
1014 return 0;
1015 }
1016
rdt_mon_features_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1017 static int rdt_mon_features_show(struct kernfs_open_file *of,
1018 struct seq_file *seq, void *v)
1019 {
1020 struct rdt_resource *r = of->kn->parent->priv;
1021 struct mon_evt *mevt;
1022
1023 list_for_each_entry(mevt, &r->evt_list, list)
1024 seq_printf(seq, "%s\n", mevt->name);
1025
1026 return 0;
1027 }
1028
rdt_bw_gran_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1029 static int rdt_bw_gran_show(struct kernfs_open_file *of,
1030 struct seq_file *seq, void *v)
1031 {
1032 struct resctrl_schema *s = of->kn->parent->priv;
1033 struct rdt_resource *r = s->res;
1034
1035 seq_printf(seq, "%u\n", r->membw.bw_gran);
1036 return 0;
1037 }
1038
rdt_delay_linear_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1039 static int rdt_delay_linear_show(struct kernfs_open_file *of,
1040 struct seq_file *seq, void *v)
1041 {
1042 struct resctrl_schema *s = of->kn->parent->priv;
1043 struct rdt_resource *r = s->res;
1044
1045 seq_printf(seq, "%u\n", r->membw.delay_linear);
1046 return 0;
1047 }
1048
max_threshold_occ_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1049 static int max_threshold_occ_show(struct kernfs_open_file *of,
1050 struct seq_file *seq, void *v)
1051 {
1052 struct rdt_resource *r = of->kn->parent->priv;
1053 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
1054
1055 seq_printf(seq, "%u\n", resctrl_cqm_threshold * hw_res->mon_scale);
1056
1057 return 0;
1058 }
1059
rdt_thread_throttle_mode_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1060 static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
1061 struct seq_file *seq, void *v)
1062 {
1063 struct resctrl_schema *s = of->kn->parent->priv;
1064 struct rdt_resource *r = s->res;
1065
1066 if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
1067 seq_puts(seq, "per-thread\n");
1068 else
1069 seq_puts(seq, "max\n");
1070
1071 return 0;
1072 }
1073
max_threshold_occ_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1074 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1075 char *buf, size_t nbytes, loff_t off)
1076 {
1077 struct rdt_hw_resource *hw_res;
1078 unsigned int bytes;
1079 int ret;
1080
1081 ret = kstrtouint(buf, 0, &bytes);
1082 if (ret)
1083 return ret;
1084
1085 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
1086 return -EINVAL;
1087
1088 hw_res = resctrl_to_arch_res(of->kn->parent->priv);
1089 resctrl_cqm_threshold = bytes / hw_res->mon_scale;
1090
1091 return nbytes;
1092 }
1093
1094 /*
1095 * rdtgroup_mode_show - Display mode of this resource group
1096 */
rdtgroup_mode_show(struct kernfs_open_file * of,struct seq_file * s,void * v)1097 static int rdtgroup_mode_show(struct kernfs_open_file *of,
1098 struct seq_file *s, void *v)
1099 {
1100 struct rdtgroup *rdtgrp;
1101
1102 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1103 if (!rdtgrp) {
1104 rdtgroup_kn_unlock(of->kn);
1105 return -ENOENT;
1106 }
1107
1108 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1109
1110 rdtgroup_kn_unlock(of->kn);
1111 return 0;
1112 }
1113
resctrl_peer_type(enum resctrl_conf_type my_type)1114 static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
1115 {
1116 switch (my_type) {
1117 case CDP_CODE:
1118 return CDP_DATA;
1119 case CDP_DATA:
1120 return CDP_CODE;
1121 default:
1122 case CDP_NONE:
1123 return CDP_NONE;
1124 }
1125 }
1126
1127 /**
1128 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
1129 * @r: Resource to which domain instance @d belongs.
1130 * @d: The domain instance for which @closid is being tested.
1131 * @cbm: Capacity bitmask being tested.
1132 * @closid: Intended closid for @cbm.
1133 * @exclusive: Only check if overlaps with exclusive resource groups
1134 *
1135 * Checks if provided @cbm intended to be used for @closid on domain
1136 * @d overlaps with any other closids or other hardware usage associated
1137 * with this domain. If @exclusive is true then only overlaps with
1138 * resource groups in exclusive mode will be considered. If @exclusive
1139 * is false then overlaps with any resource group or hardware entities
1140 * will be considered.
1141 *
1142 * @cbm is unsigned long, even if only 32 bits are used, to make the
1143 * bitmap functions work correctly.
1144 *
1145 * Return: false if CBM does not overlap, true if it does.
1146 */
__rdtgroup_cbm_overlaps(struct rdt_resource * r,struct rdt_domain * d,unsigned long cbm,int closid,enum resctrl_conf_type type,bool exclusive)1147 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1148 unsigned long cbm, int closid,
1149 enum resctrl_conf_type type, bool exclusive)
1150 {
1151 enum rdtgrp_mode mode;
1152 unsigned long ctrl_b;
1153 int i;
1154
1155 /* Check for any overlap with regions used by hardware directly */
1156 if (!exclusive) {
1157 ctrl_b = r->cache.shareable_bits;
1158 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
1159 return true;
1160 }
1161
1162 /* Check for overlap with other resource groups */
1163 for (i = 0; i < closids_supported(); i++) {
1164 ctrl_b = resctrl_arch_get_config(r, d, i, type);
1165 mode = rdtgroup_mode_by_closid(i);
1166 if (closid_allocated(i) && i != closid &&
1167 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1168 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1169 if (exclusive) {
1170 if (mode == RDT_MODE_EXCLUSIVE)
1171 return true;
1172 continue;
1173 }
1174 return true;
1175 }
1176 }
1177 }
1178
1179 return false;
1180 }
1181
1182 /**
1183 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
1184 * @s: Schema for the resource to which domain instance @d belongs.
1185 * @d: The domain instance for which @closid is being tested.
1186 * @cbm: Capacity bitmask being tested.
1187 * @closid: Intended closid for @cbm.
1188 * @exclusive: Only check if overlaps with exclusive resource groups
1189 *
1190 * Resources that can be allocated using a CBM can use the CBM to control
1191 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1192 * for overlap. Overlap test is not limited to the specific resource for
1193 * which the CBM is intended though - when dealing with CDP resources that
1194 * share the underlying hardware the overlap check should be performed on
1195 * the CDP resource sharing the hardware also.
1196 *
1197 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1198 * overlap test.
1199 *
1200 * Return: true if CBM overlap detected, false if there is no overlap
1201 */
rdtgroup_cbm_overlaps(struct resctrl_schema * s,struct rdt_domain * d,unsigned long cbm,int closid,bool exclusive)1202 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
1203 unsigned long cbm, int closid, bool exclusive)
1204 {
1205 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
1206 struct rdt_resource *r = s->res;
1207
1208 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
1209 exclusive))
1210 return true;
1211
1212 if (!resctrl_arch_get_cdp_enabled(r->rid))
1213 return false;
1214 return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
1215 }
1216
1217 /**
1218 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1219 *
1220 * An exclusive resource group implies that there should be no sharing of
1221 * its allocated resources. At the time this group is considered to be
1222 * exclusive this test can determine if its current schemata supports this
1223 * setting by testing for overlap with all other resource groups.
1224 *
1225 * Return: true if resource group can be exclusive, false if there is overlap
1226 * with allocations of other resource groups and thus this resource group
1227 * cannot be exclusive.
1228 */
rdtgroup_mode_test_exclusive(struct rdtgroup * rdtgrp)1229 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1230 {
1231 int closid = rdtgrp->closid;
1232 struct resctrl_schema *s;
1233 struct rdt_resource *r;
1234 bool has_cache = false;
1235 struct rdt_domain *d;
1236 u32 ctrl;
1237
1238 list_for_each_entry(s, &resctrl_schema_all, list) {
1239 r = s->res;
1240 if (r->rid == RDT_RESOURCE_MBA)
1241 continue;
1242 has_cache = true;
1243 list_for_each_entry(d, &r->domains, list) {
1244 ctrl = resctrl_arch_get_config(r, d, closid,
1245 s->conf_type);
1246 if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
1247 rdt_last_cmd_puts("Schemata overlaps\n");
1248 return false;
1249 }
1250 }
1251 }
1252
1253 if (!has_cache) {
1254 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
1255 return false;
1256 }
1257
1258 return true;
1259 }
1260
1261 /**
1262 * rdtgroup_mode_write - Modify the resource group's mode
1263 *
1264 */
rdtgroup_mode_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1265 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1266 char *buf, size_t nbytes, loff_t off)
1267 {
1268 struct rdtgroup *rdtgrp;
1269 enum rdtgrp_mode mode;
1270 int ret = 0;
1271
1272 /* Valid input requires a trailing newline */
1273 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1274 return -EINVAL;
1275 buf[nbytes - 1] = '\0';
1276
1277 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1278 if (!rdtgrp) {
1279 rdtgroup_kn_unlock(of->kn);
1280 return -ENOENT;
1281 }
1282
1283 rdt_last_cmd_clear();
1284
1285 mode = rdtgrp->mode;
1286
1287 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1288 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1289 (!strcmp(buf, "pseudo-locksetup") &&
1290 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1291 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1292 goto out;
1293
1294 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1295 rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
1296 ret = -EINVAL;
1297 goto out;
1298 }
1299
1300 if (!strcmp(buf, "shareable")) {
1301 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1302 ret = rdtgroup_locksetup_exit(rdtgrp);
1303 if (ret)
1304 goto out;
1305 }
1306 rdtgrp->mode = RDT_MODE_SHAREABLE;
1307 } else if (!strcmp(buf, "exclusive")) {
1308 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1309 ret = -EINVAL;
1310 goto out;
1311 }
1312 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1313 ret = rdtgroup_locksetup_exit(rdtgrp);
1314 if (ret)
1315 goto out;
1316 }
1317 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1318 } else if (!strcmp(buf, "pseudo-locksetup")) {
1319 ret = rdtgroup_locksetup_enter(rdtgrp);
1320 if (ret)
1321 goto out;
1322 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1323 } else {
1324 rdt_last_cmd_puts("Unknown or unsupported mode\n");
1325 ret = -EINVAL;
1326 }
1327
1328 out:
1329 rdtgroup_kn_unlock(of->kn);
1330 return ret ?: nbytes;
1331 }
1332
1333 /**
1334 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1335 * @r: RDT resource to which @d belongs.
1336 * @d: RDT domain instance.
1337 * @cbm: bitmask for which the size should be computed.
1338 *
1339 * The bitmask provided associated with the RDT domain instance @d will be
1340 * translated into how many bytes it represents. The size in bytes is
1341 * computed by first dividing the total cache size by the CBM length to
1342 * determine how many bytes each bit in the bitmask represents. The result
1343 * is multiplied with the number of bits set in the bitmask.
1344 *
1345 * @cbm is unsigned long, even if only 32 bits are used to make the
1346 * bitmap functions work correctly.
1347 */
rdtgroup_cbm_to_size(struct rdt_resource * r,struct rdt_domain * d,unsigned long cbm)1348 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1349 struct rdt_domain *d, unsigned long cbm)
1350 {
1351 struct cpu_cacheinfo *ci;
1352 unsigned int size = 0;
1353 int num_b, i;
1354
1355 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1356 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1357 for (i = 0; i < ci->num_leaves; i++) {
1358 if (ci->info_list[i].level == r->cache_level) {
1359 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1360 break;
1361 }
1362 }
1363
1364 return size;
1365 }
1366
1367 /**
1368 * rdtgroup_size_show - Display size in bytes of allocated regions
1369 *
1370 * The "size" file mirrors the layout of the "schemata" file, printing the
1371 * size in bytes of each region instead of the capacity bitmask.
1372 *
1373 */
rdtgroup_size_show(struct kernfs_open_file * of,struct seq_file * s,void * v)1374 static int rdtgroup_size_show(struct kernfs_open_file *of,
1375 struct seq_file *s, void *v)
1376 {
1377 struct resctrl_schema *schema;
1378 struct rdtgroup *rdtgrp;
1379 struct rdt_resource *r;
1380 struct rdt_domain *d;
1381 unsigned int size;
1382 int ret = 0;
1383 bool sep;
1384 u32 ctrl;
1385
1386 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1387 if (!rdtgrp) {
1388 rdtgroup_kn_unlock(of->kn);
1389 return -ENOENT;
1390 }
1391
1392 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1393 if (!rdtgrp->plr->d) {
1394 rdt_last_cmd_clear();
1395 rdt_last_cmd_puts("Cache domain offline\n");
1396 ret = -ENODEV;
1397 } else {
1398 seq_printf(s, "%*s:", max_name_width,
1399 rdtgrp->plr->s->name);
1400 size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
1401 rdtgrp->plr->d,
1402 rdtgrp->plr->cbm);
1403 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1404 }
1405 goto out;
1406 }
1407
1408 list_for_each_entry(schema, &resctrl_schema_all, list) {
1409 r = schema->res;
1410 sep = false;
1411 seq_printf(s, "%*s:", max_name_width, schema->name);
1412 list_for_each_entry(d, &r->domains, list) {
1413 if (sep)
1414 seq_putc(s, ';');
1415 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1416 size = 0;
1417 } else {
1418 ctrl = resctrl_arch_get_config(r, d,
1419 rdtgrp->closid,
1420 schema->conf_type);
1421 if (r->rid == RDT_RESOURCE_MBA)
1422 size = ctrl;
1423 else
1424 size = rdtgroup_cbm_to_size(r, d, ctrl);
1425 }
1426 seq_printf(s, "%d=%u", d->id, size);
1427 sep = true;
1428 }
1429 seq_putc(s, '\n');
1430 }
1431
1432 out:
1433 rdtgroup_kn_unlock(of->kn);
1434
1435 return ret;
1436 }
1437
1438 /* rdtgroup information files for one cache resource. */
1439 static struct rftype res_common_files[] = {
1440 {
1441 .name = "last_cmd_status",
1442 .mode = 0444,
1443 .kf_ops = &rdtgroup_kf_single_ops,
1444 .seq_show = rdt_last_cmd_status_show,
1445 .fflags = RF_TOP_INFO,
1446 },
1447 {
1448 .name = "num_closids",
1449 .mode = 0444,
1450 .kf_ops = &rdtgroup_kf_single_ops,
1451 .seq_show = rdt_num_closids_show,
1452 .fflags = RF_CTRL_INFO,
1453 },
1454 {
1455 .name = "mon_features",
1456 .mode = 0444,
1457 .kf_ops = &rdtgroup_kf_single_ops,
1458 .seq_show = rdt_mon_features_show,
1459 .fflags = RF_MON_INFO,
1460 },
1461 {
1462 .name = "num_rmids",
1463 .mode = 0444,
1464 .kf_ops = &rdtgroup_kf_single_ops,
1465 .seq_show = rdt_num_rmids_show,
1466 .fflags = RF_MON_INFO,
1467 },
1468 {
1469 .name = "cbm_mask",
1470 .mode = 0444,
1471 .kf_ops = &rdtgroup_kf_single_ops,
1472 .seq_show = rdt_default_ctrl_show,
1473 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1474 },
1475 {
1476 .name = "min_cbm_bits",
1477 .mode = 0444,
1478 .kf_ops = &rdtgroup_kf_single_ops,
1479 .seq_show = rdt_min_cbm_bits_show,
1480 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1481 },
1482 {
1483 .name = "shareable_bits",
1484 .mode = 0444,
1485 .kf_ops = &rdtgroup_kf_single_ops,
1486 .seq_show = rdt_shareable_bits_show,
1487 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1488 },
1489 {
1490 .name = "bit_usage",
1491 .mode = 0444,
1492 .kf_ops = &rdtgroup_kf_single_ops,
1493 .seq_show = rdt_bit_usage_show,
1494 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1495 },
1496 {
1497 .name = "min_bandwidth",
1498 .mode = 0444,
1499 .kf_ops = &rdtgroup_kf_single_ops,
1500 .seq_show = rdt_min_bw_show,
1501 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1502 },
1503 {
1504 .name = "bandwidth_gran",
1505 .mode = 0444,
1506 .kf_ops = &rdtgroup_kf_single_ops,
1507 .seq_show = rdt_bw_gran_show,
1508 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1509 },
1510 {
1511 .name = "delay_linear",
1512 .mode = 0444,
1513 .kf_ops = &rdtgroup_kf_single_ops,
1514 .seq_show = rdt_delay_linear_show,
1515 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1516 },
1517 /*
1518 * Platform specific which (if any) capabilities are provided by
1519 * thread_throttle_mode. Defer "fflags" initialization to platform
1520 * discovery.
1521 */
1522 {
1523 .name = "thread_throttle_mode",
1524 .mode = 0444,
1525 .kf_ops = &rdtgroup_kf_single_ops,
1526 .seq_show = rdt_thread_throttle_mode_show,
1527 },
1528 {
1529 .name = "max_threshold_occupancy",
1530 .mode = 0644,
1531 .kf_ops = &rdtgroup_kf_single_ops,
1532 .write = max_threshold_occ_write,
1533 .seq_show = max_threshold_occ_show,
1534 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
1535 },
1536 {
1537 .name = "cpus",
1538 .mode = 0644,
1539 .kf_ops = &rdtgroup_kf_single_ops,
1540 .write = rdtgroup_cpus_write,
1541 .seq_show = rdtgroup_cpus_show,
1542 .fflags = RFTYPE_BASE,
1543 },
1544 {
1545 .name = "cpus_list",
1546 .mode = 0644,
1547 .kf_ops = &rdtgroup_kf_single_ops,
1548 .write = rdtgroup_cpus_write,
1549 .seq_show = rdtgroup_cpus_show,
1550 .flags = RFTYPE_FLAGS_CPUS_LIST,
1551 .fflags = RFTYPE_BASE,
1552 },
1553 {
1554 .name = "tasks",
1555 .mode = 0644,
1556 .kf_ops = &rdtgroup_kf_single_ops,
1557 .write = rdtgroup_tasks_write,
1558 .seq_show = rdtgroup_tasks_show,
1559 .fflags = RFTYPE_BASE,
1560 },
1561 {
1562 .name = "schemata",
1563 .mode = 0644,
1564 .kf_ops = &rdtgroup_kf_single_ops,
1565 .write = rdtgroup_schemata_write,
1566 .seq_show = rdtgroup_schemata_show,
1567 .fflags = RF_CTRL_BASE,
1568 },
1569 {
1570 .name = "mode",
1571 .mode = 0644,
1572 .kf_ops = &rdtgroup_kf_single_ops,
1573 .write = rdtgroup_mode_write,
1574 .seq_show = rdtgroup_mode_show,
1575 .fflags = RF_CTRL_BASE,
1576 },
1577 {
1578 .name = "size",
1579 .mode = 0444,
1580 .kf_ops = &rdtgroup_kf_single_ops,
1581 .seq_show = rdtgroup_size_show,
1582 .fflags = RF_CTRL_BASE,
1583 },
1584
1585 };
1586
rdtgroup_add_files(struct kernfs_node * kn,unsigned long fflags)1587 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1588 {
1589 struct rftype *rfts, *rft;
1590 int ret, len;
1591
1592 rfts = res_common_files;
1593 len = ARRAY_SIZE(res_common_files);
1594
1595 lockdep_assert_held(&rdtgroup_mutex);
1596
1597 for (rft = rfts; rft < rfts + len; rft++) {
1598 if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
1599 ret = rdtgroup_add_file(kn, rft);
1600 if (ret)
1601 goto error;
1602 }
1603 }
1604
1605 return 0;
1606 error:
1607 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1608 while (--rft >= rfts) {
1609 if ((fflags & rft->fflags) == rft->fflags)
1610 kernfs_remove_by_name(kn, rft->name);
1611 }
1612 return ret;
1613 }
1614
rdtgroup_get_rftype_by_name(const char * name)1615 static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
1616 {
1617 struct rftype *rfts, *rft;
1618 int len;
1619
1620 rfts = res_common_files;
1621 len = ARRAY_SIZE(res_common_files);
1622
1623 for (rft = rfts; rft < rfts + len; rft++) {
1624 if (!strcmp(rft->name, name))
1625 return rft;
1626 }
1627
1628 return NULL;
1629 }
1630
thread_throttle_mode_init(void)1631 void __init thread_throttle_mode_init(void)
1632 {
1633 struct rftype *rft;
1634
1635 rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
1636 if (!rft)
1637 return;
1638
1639 rft->fflags = RF_CTRL_INFO | RFTYPE_RES_MB;
1640 }
1641
1642 /**
1643 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1644 * @r: The resource group with which the file is associated.
1645 * @name: Name of the file
1646 *
1647 * The permissions of named resctrl file, directory, or link are modified
1648 * to not allow read, write, or execute by any user.
1649 *
1650 * WARNING: This function is intended to communicate to the user that the
1651 * resctrl file has been locked down - that it is not relevant to the
1652 * particular state the system finds itself in. It should not be relied
1653 * on to protect from user access because after the file's permissions
1654 * are restricted the user can still change the permissions using chmod
1655 * from the command line.
1656 *
1657 * Return: 0 on success, <0 on failure.
1658 */
rdtgroup_kn_mode_restrict(struct rdtgroup * r,const char * name)1659 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1660 {
1661 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1662 struct kernfs_node *kn;
1663 int ret = 0;
1664
1665 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1666 if (!kn)
1667 return -ENOENT;
1668
1669 switch (kernfs_type(kn)) {
1670 case KERNFS_DIR:
1671 iattr.ia_mode = S_IFDIR;
1672 break;
1673 case KERNFS_FILE:
1674 iattr.ia_mode = S_IFREG;
1675 break;
1676 case KERNFS_LINK:
1677 iattr.ia_mode = S_IFLNK;
1678 break;
1679 }
1680
1681 ret = kernfs_setattr(kn, &iattr);
1682 kernfs_put(kn);
1683 return ret;
1684 }
1685
1686 /**
1687 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1688 * @r: The resource group with which the file is associated.
1689 * @name: Name of the file
1690 * @mask: Mask of permissions that should be restored
1691 *
1692 * Restore the permissions of the named file. If @name is a directory the
1693 * permissions of its parent will be used.
1694 *
1695 * Return: 0 on success, <0 on failure.
1696 */
rdtgroup_kn_mode_restore(struct rdtgroup * r,const char * name,umode_t mask)1697 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1698 umode_t mask)
1699 {
1700 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1701 struct kernfs_node *kn, *parent;
1702 struct rftype *rfts, *rft;
1703 int ret, len;
1704
1705 rfts = res_common_files;
1706 len = ARRAY_SIZE(res_common_files);
1707
1708 for (rft = rfts; rft < rfts + len; rft++) {
1709 if (!strcmp(rft->name, name))
1710 iattr.ia_mode = rft->mode & mask;
1711 }
1712
1713 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1714 if (!kn)
1715 return -ENOENT;
1716
1717 switch (kernfs_type(kn)) {
1718 case KERNFS_DIR:
1719 parent = kernfs_get_parent(kn);
1720 if (parent) {
1721 iattr.ia_mode |= parent->mode;
1722 kernfs_put(parent);
1723 }
1724 iattr.ia_mode |= S_IFDIR;
1725 break;
1726 case KERNFS_FILE:
1727 iattr.ia_mode |= S_IFREG;
1728 break;
1729 case KERNFS_LINK:
1730 iattr.ia_mode |= S_IFLNK;
1731 break;
1732 }
1733
1734 ret = kernfs_setattr(kn, &iattr);
1735 kernfs_put(kn);
1736 return ret;
1737 }
1738
rdtgroup_mkdir_info_resdir(void * priv,char * name,unsigned long fflags)1739 static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
1740 unsigned long fflags)
1741 {
1742 struct kernfs_node *kn_subdir;
1743 int ret;
1744
1745 kn_subdir = kernfs_create_dir(kn_info, name,
1746 kn_info->mode, priv);
1747 if (IS_ERR(kn_subdir))
1748 return PTR_ERR(kn_subdir);
1749
1750 ret = rdtgroup_kn_set_ugid(kn_subdir);
1751 if (ret)
1752 return ret;
1753
1754 ret = rdtgroup_add_files(kn_subdir, fflags);
1755 if (!ret)
1756 kernfs_activate(kn_subdir);
1757
1758 return ret;
1759 }
1760
rdtgroup_create_info_dir(struct kernfs_node * parent_kn)1761 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
1762 {
1763 struct resctrl_schema *s;
1764 struct rdt_resource *r;
1765 unsigned long fflags;
1766 char name[32];
1767 int ret;
1768
1769 /* create the directory */
1770 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
1771 if (IS_ERR(kn_info))
1772 return PTR_ERR(kn_info);
1773
1774 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
1775 if (ret)
1776 goto out_destroy;
1777
1778 /* loop over enabled controls, these are all alloc_enabled */
1779 list_for_each_entry(s, &resctrl_schema_all, list) {
1780 r = s->res;
1781 fflags = r->fflags | RF_CTRL_INFO;
1782 ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
1783 if (ret)
1784 goto out_destroy;
1785 }
1786
1787 for_each_mon_enabled_rdt_resource(r) {
1788 fflags = r->fflags | RF_MON_INFO;
1789 sprintf(name, "%s_MON", r->name);
1790 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
1791 if (ret)
1792 goto out_destroy;
1793 }
1794
1795 ret = rdtgroup_kn_set_ugid(kn_info);
1796 if (ret)
1797 goto out_destroy;
1798
1799 kernfs_activate(kn_info);
1800
1801 return 0;
1802
1803 out_destroy:
1804 kernfs_remove(kn_info);
1805 return ret;
1806 }
1807
1808 static int
mongroup_create_dir(struct kernfs_node * parent_kn,struct rdtgroup * prgrp,char * name,struct kernfs_node ** dest_kn)1809 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
1810 char *name, struct kernfs_node **dest_kn)
1811 {
1812 struct kernfs_node *kn;
1813 int ret;
1814
1815 /* create the directory */
1816 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1817 if (IS_ERR(kn))
1818 return PTR_ERR(kn);
1819
1820 if (dest_kn)
1821 *dest_kn = kn;
1822
1823 ret = rdtgroup_kn_set_ugid(kn);
1824 if (ret)
1825 goto out_destroy;
1826
1827 kernfs_activate(kn);
1828
1829 return 0;
1830
1831 out_destroy:
1832 kernfs_remove(kn);
1833 return ret;
1834 }
1835
l3_qos_cfg_update(void * arg)1836 static void l3_qos_cfg_update(void *arg)
1837 {
1838 bool *enable = arg;
1839
1840 wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
1841 }
1842
l2_qos_cfg_update(void * arg)1843 static void l2_qos_cfg_update(void *arg)
1844 {
1845 bool *enable = arg;
1846
1847 wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
1848 }
1849
is_mba_linear(void)1850 static inline bool is_mba_linear(void)
1851 {
1852 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
1853 }
1854
set_cache_qos_cfg(int level,bool enable)1855 static int set_cache_qos_cfg(int level, bool enable)
1856 {
1857 void (*update)(void *arg);
1858 struct rdt_resource *r_l;
1859 cpumask_var_t cpu_mask;
1860 struct rdt_domain *d;
1861 int cpu;
1862
1863 if (level == RDT_RESOURCE_L3)
1864 update = l3_qos_cfg_update;
1865 else if (level == RDT_RESOURCE_L2)
1866 update = l2_qos_cfg_update;
1867 else
1868 return -EINVAL;
1869
1870 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1871 return -ENOMEM;
1872
1873 r_l = &rdt_resources_all[level].r_resctrl;
1874 list_for_each_entry(d, &r_l->domains, list) {
1875 if (r_l->cache.arch_has_per_cpu_cfg)
1876 /* Pick all the CPUs in the domain instance */
1877 for_each_cpu(cpu, &d->cpu_mask)
1878 cpumask_set_cpu(cpu, cpu_mask);
1879 else
1880 /* Pick one CPU from each domain instance to update MSR */
1881 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1882 }
1883 cpu = get_cpu();
1884 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1885 if (cpumask_test_cpu(cpu, cpu_mask))
1886 update(&enable);
1887 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1888 smp_call_function_many(cpu_mask, update, &enable, 1);
1889 put_cpu();
1890
1891 free_cpumask_var(cpu_mask);
1892
1893 return 0;
1894 }
1895
1896 /* Restore the qos cfg state when a domain comes online */
rdt_domain_reconfigure_cdp(struct rdt_resource * r)1897 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
1898 {
1899 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
1900
1901 if (!r->cdp_capable)
1902 return;
1903
1904 if (r->rid == RDT_RESOURCE_L2)
1905 l2_qos_cfg_update(&hw_res->cdp_enabled);
1906
1907 if (r->rid == RDT_RESOURCE_L3)
1908 l3_qos_cfg_update(&hw_res->cdp_enabled);
1909 }
1910
1911 /*
1912 * Enable or disable the MBA software controller
1913 * which helps user specify bandwidth in MBps.
1914 * MBA software controller is supported only if
1915 * MBM is supported and MBA is in linear scale.
1916 */
set_mba_sc(bool mba_sc)1917 static int set_mba_sc(bool mba_sc)
1918 {
1919 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
1920 struct rdt_hw_domain *hw_dom;
1921 struct rdt_domain *d;
1922
1923 if (!is_mbm_enabled() || !is_mba_linear() ||
1924 mba_sc == is_mba_sc(r))
1925 return -EINVAL;
1926
1927 r->membw.mba_sc = mba_sc;
1928 list_for_each_entry(d, &r->domains, list) {
1929 hw_dom = resctrl_to_arch_dom(d);
1930 setup_default_ctrlval(r, hw_dom->ctrl_val, hw_dom->mbps_val);
1931 }
1932
1933 return 0;
1934 }
1935
cdp_enable(int level)1936 static int cdp_enable(int level)
1937 {
1938 struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
1939 int ret;
1940
1941 if (!r_l->alloc_capable)
1942 return -EINVAL;
1943
1944 ret = set_cache_qos_cfg(level, true);
1945 if (!ret)
1946 rdt_resources_all[level].cdp_enabled = true;
1947
1948 return ret;
1949 }
1950
cdp_disable(int level)1951 static void cdp_disable(int level)
1952 {
1953 struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
1954
1955 if (r_hw->cdp_enabled) {
1956 set_cache_qos_cfg(level, false);
1957 r_hw->cdp_enabled = false;
1958 }
1959 }
1960
resctrl_arch_set_cdp_enabled(enum resctrl_res_level l,bool enable)1961 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
1962 {
1963 struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
1964
1965 if (!hw_res->r_resctrl.cdp_capable)
1966 return -EINVAL;
1967
1968 if (enable)
1969 return cdp_enable(l);
1970
1971 cdp_disable(l);
1972
1973 return 0;
1974 }
1975
cdp_disable_all(void)1976 static void cdp_disable_all(void)
1977 {
1978 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
1979 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
1980 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
1981 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
1982 }
1983
1984 /*
1985 * We don't allow rdtgroup directories to be created anywhere
1986 * except the root directory. Thus when looking for the rdtgroup
1987 * structure for a kernfs node we are either looking at a directory,
1988 * in which case the rdtgroup structure is pointed at by the "priv"
1989 * field, otherwise we have a file, and need only look to the parent
1990 * to find the rdtgroup.
1991 */
kernfs_to_rdtgroup(struct kernfs_node * kn)1992 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1993 {
1994 if (kernfs_type(kn) == KERNFS_DIR) {
1995 /*
1996 * All the resource directories use "kn->priv"
1997 * to point to the "struct rdtgroup" for the
1998 * resource. "info" and its subdirectories don't
1999 * have rdtgroup structures, so return NULL here.
2000 */
2001 if (kn == kn_info || kn->parent == kn_info)
2002 return NULL;
2003 else
2004 return kn->priv;
2005 } else {
2006 return kn->parent->priv;
2007 }
2008 }
2009
rdtgroup_kn_lock_live(struct kernfs_node * kn)2010 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
2011 {
2012 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2013
2014 if (!rdtgrp)
2015 return NULL;
2016
2017 atomic_inc(&rdtgrp->waitcount);
2018 kernfs_break_active_protection(kn);
2019
2020 mutex_lock(&rdtgroup_mutex);
2021
2022 /* Was this group deleted while we waited? */
2023 if (rdtgrp->flags & RDT_DELETED)
2024 return NULL;
2025
2026 return rdtgrp;
2027 }
2028
rdtgroup_kn_unlock(struct kernfs_node * kn)2029 void rdtgroup_kn_unlock(struct kernfs_node *kn)
2030 {
2031 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2032
2033 if (!rdtgrp)
2034 return;
2035
2036 mutex_unlock(&rdtgroup_mutex);
2037
2038 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2039 (rdtgrp->flags & RDT_DELETED)) {
2040 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2041 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2042 rdtgroup_pseudo_lock_remove(rdtgrp);
2043 kernfs_unbreak_active_protection(kn);
2044 rdtgroup_remove(rdtgrp);
2045 } else {
2046 kernfs_unbreak_active_protection(kn);
2047 }
2048 }
2049
2050 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2051 struct rdtgroup *prgrp,
2052 struct kernfs_node **mon_data_kn);
2053
rdt_enable_ctx(struct rdt_fs_context * ctx)2054 static int rdt_enable_ctx(struct rdt_fs_context *ctx)
2055 {
2056 int ret = 0;
2057
2058 if (ctx->enable_cdpl2)
2059 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
2060
2061 if (!ret && ctx->enable_cdpl3)
2062 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
2063
2064 if (!ret && ctx->enable_mba_mbps)
2065 ret = set_mba_sc(true);
2066
2067 return ret;
2068 }
2069
schemata_list_add(struct rdt_resource * r,enum resctrl_conf_type type)2070 static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
2071 {
2072 struct resctrl_schema *s;
2073 const char *suffix = "";
2074 int ret, cl;
2075
2076 s = kzalloc(sizeof(*s), GFP_KERNEL);
2077 if (!s)
2078 return -ENOMEM;
2079
2080 s->res = r;
2081 s->num_closid = resctrl_arch_get_num_closid(r);
2082 if (resctrl_arch_get_cdp_enabled(r->rid))
2083 s->num_closid /= 2;
2084
2085 s->conf_type = type;
2086 switch (type) {
2087 case CDP_CODE:
2088 suffix = "CODE";
2089 break;
2090 case CDP_DATA:
2091 suffix = "DATA";
2092 break;
2093 case CDP_NONE:
2094 suffix = "";
2095 break;
2096 }
2097
2098 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
2099 if (ret >= sizeof(s->name)) {
2100 kfree(s);
2101 return -EINVAL;
2102 }
2103
2104 cl = strlen(s->name);
2105
2106 /*
2107 * If CDP is supported by this resource, but not enabled,
2108 * include the suffix. This ensures the tabular format of the
2109 * schemata file does not change between mounts of the filesystem.
2110 */
2111 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
2112 cl += 4;
2113
2114 if (cl > max_name_width)
2115 max_name_width = cl;
2116
2117 INIT_LIST_HEAD(&s->list);
2118 list_add(&s->list, &resctrl_schema_all);
2119
2120 return 0;
2121 }
2122
schemata_list_create(void)2123 static int schemata_list_create(void)
2124 {
2125 struct rdt_resource *r;
2126 int ret = 0;
2127
2128 for_each_alloc_enabled_rdt_resource(r) {
2129 if (resctrl_arch_get_cdp_enabled(r->rid)) {
2130 ret = schemata_list_add(r, CDP_CODE);
2131 if (ret)
2132 break;
2133
2134 ret = schemata_list_add(r, CDP_DATA);
2135 } else {
2136 ret = schemata_list_add(r, CDP_NONE);
2137 }
2138
2139 if (ret)
2140 break;
2141 }
2142
2143 return ret;
2144 }
2145
schemata_list_destroy(void)2146 static void schemata_list_destroy(void)
2147 {
2148 struct resctrl_schema *s, *tmp;
2149
2150 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
2151 list_del(&s->list);
2152 kfree(s);
2153 }
2154 }
2155
rdt_get_tree(struct fs_context * fc)2156 static int rdt_get_tree(struct fs_context *fc)
2157 {
2158 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2159 struct rdt_domain *dom;
2160 struct rdt_resource *r;
2161 int ret;
2162
2163 cpus_read_lock();
2164 mutex_lock(&rdtgroup_mutex);
2165 /*
2166 * resctrl file system can only be mounted once.
2167 */
2168 if (static_branch_unlikely(&rdt_enable_key)) {
2169 ret = -EBUSY;
2170 goto out;
2171 }
2172
2173 ret = rdt_enable_ctx(ctx);
2174 if (ret < 0)
2175 goto out_cdp;
2176
2177 ret = schemata_list_create();
2178 if (ret) {
2179 schemata_list_destroy();
2180 goto out_mba;
2181 }
2182
2183 closid_init();
2184
2185 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
2186 if (ret < 0)
2187 goto out_schemata_free;
2188
2189 if (rdt_mon_capable) {
2190 ret = mongroup_create_dir(rdtgroup_default.kn,
2191 &rdtgroup_default, "mon_groups",
2192 &kn_mongrp);
2193 if (ret < 0)
2194 goto out_info;
2195
2196 ret = mkdir_mondata_all(rdtgroup_default.kn,
2197 &rdtgroup_default, &kn_mondata);
2198 if (ret < 0)
2199 goto out_mongrp;
2200 rdtgroup_default.mon.mon_data_kn = kn_mondata;
2201 }
2202
2203 ret = rdt_pseudo_lock_init();
2204 if (ret)
2205 goto out_mondata;
2206
2207 ret = kernfs_get_tree(fc);
2208 if (ret < 0)
2209 goto out_psl;
2210
2211 if (rdt_alloc_capable)
2212 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
2213 if (rdt_mon_capable)
2214 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
2215
2216 if (rdt_alloc_capable || rdt_mon_capable)
2217 static_branch_enable_cpuslocked(&rdt_enable_key);
2218
2219 if (is_mbm_enabled()) {
2220 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
2221 list_for_each_entry(dom, &r->domains, list)
2222 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
2223 }
2224
2225 goto out;
2226
2227 out_psl:
2228 rdt_pseudo_lock_release();
2229 out_mondata:
2230 if (rdt_mon_capable)
2231 kernfs_remove(kn_mondata);
2232 out_mongrp:
2233 if (rdt_mon_capable)
2234 kernfs_remove(kn_mongrp);
2235 out_info:
2236 kernfs_remove(kn_info);
2237 out_schemata_free:
2238 schemata_list_destroy();
2239 out_mba:
2240 if (ctx->enable_mba_mbps)
2241 set_mba_sc(false);
2242 out_cdp:
2243 cdp_disable_all();
2244 out:
2245 rdt_last_cmd_clear();
2246 mutex_unlock(&rdtgroup_mutex);
2247 cpus_read_unlock();
2248 return ret;
2249 }
2250
2251 enum rdt_param {
2252 Opt_cdp,
2253 Opt_cdpl2,
2254 Opt_mba_mbps,
2255 nr__rdt_params
2256 };
2257
2258 static const struct fs_parameter_spec rdt_fs_parameters[] = {
2259 fsparam_flag("cdp", Opt_cdp),
2260 fsparam_flag("cdpl2", Opt_cdpl2),
2261 fsparam_flag("mba_MBps", Opt_mba_mbps),
2262 {}
2263 };
2264
rdt_parse_param(struct fs_context * fc,struct fs_parameter * param)2265 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2266 {
2267 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2268 struct fs_parse_result result;
2269 int opt;
2270
2271 opt = fs_parse(fc, rdt_fs_parameters, param, &result);
2272 if (opt < 0)
2273 return opt;
2274
2275 switch (opt) {
2276 case Opt_cdp:
2277 ctx->enable_cdpl3 = true;
2278 return 0;
2279 case Opt_cdpl2:
2280 ctx->enable_cdpl2 = true;
2281 return 0;
2282 case Opt_mba_mbps:
2283 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2284 return -EINVAL;
2285 ctx->enable_mba_mbps = true;
2286 return 0;
2287 }
2288
2289 return -EINVAL;
2290 }
2291
rdt_fs_context_free(struct fs_context * fc)2292 static void rdt_fs_context_free(struct fs_context *fc)
2293 {
2294 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2295
2296 kernfs_free_fs_context(fc);
2297 kfree(ctx);
2298 }
2299
2300 static const struct fs_context_operations rdt_fs_context_ops = {
2301 .free = rdt_fs_context_free,
2302 .parse_param = rdt_parse_param,
2303 .get_tree = rdt_get_tree,
2304 };
2305
rdt_init_fs_context(struct fs_context * fc)2306 static int rdt_init_fs_context(struct fs_context *fc)
2307 {
2308 struct rdt_fs_context *ctx;
2309
2310 ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2311 if (!ctx)
2312 return -ENOMEM;
2313
2314 ctx->kfc.root = rdt_root;
2315 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2316 fc->fs_private = &ctx->kfc;
2317 fc->ops = &rdt_fs_context_ops;
2318 put_user_ns(fc->user_ns);
2319 fc->user_ns = get_user_ns(&init_user_ns);
2320 fc->global = true;
2321 return 0;
2322 }
2323
reset_all_ctrls(struct rdt_resource * r)2324 static int reset_all_ctrls(struct rdt_resource *r)
2325 {
2326 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2327 struct rdt_hw_domain *hw_dom;
2328 struct msr_param msr_param;
2329 cpumask_var_t cpu_mask;
2330 struct rdt_domain *d;
2331 int i, cpu;
2332
2333 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2334 return -ENOMEM;
2335
2336 msr_param.res = r;
2337 msr_param.low = 0;
2338 msr_param.high = hw_res->num_closid;
2339
2340 /*
2341 * Disable resource control for this resource by setting all
2342 * CBMs in all domains to the maximum mask value. Pick one CPU
2343 * from each domain to update the MSRs below.
2344 */
2345 list_for_each_entry(d, &r->domains, list) {
2346 hw_dom = resctrl_to_arch_dom(d);
2347 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2348
2349 for (i = 0; i < hw_res->num_closid; i++)
2350 hw_dom->ctrl_val[i] = r->default_ctrl;
2351 }
2352 cpu = get_cpu();
2353 /* Update CBM on this cpu if it's in cpu_mask. */
2354 if (cpumask_test_cpu(cpu, cpu_mask))
2355 rdt_ctrl_update(&msr_param);
2356 /* Update CBM on all other cpus in cpu_mask. */
2357 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
2358 put_cpu();
2359
2360 free_cpumask_var(cpu_mask);
2361
2362 return 0;
2363 }
2364
2365 /*
2366 * Move tasks from one to the other group. If @from is NULL, then all tasks
2367 * in the systems are moved unconditionally (used for teardown).
2368 *
2369 * If @mask is not NULL the cpus on which moved tasks are running are set
2370 * in that mask so the update smp function call is restricted to affected
2371 * cpus.
2372 */
rdt_move_group_tasks(struct rdtgroup * from,struct rdtgroup * to,struct cpumask * mask)2373 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2374 struct cpumask *mask)
2375 {
2376 struct task_struct *p, *t;
2377
2378 read_lock(&tasklist_lock);
2379 for_each_process_thread(p, t) {
2380 if (!from || is_closid_match(t, from) ||
2381 is_rmid_match(t, from)) {
2382 WRITE_ONCE(t->closid, to->closid);
2383 WRITE_ONCE(t->rmid, to->mon.rmid);
2384
2385 /*
2386 * Order the closid/rmid stores above before the loads
2387 * in task_curr(). This pairs with the full barrier
2388 * between the rq->curr update and resctrl_sched_in()
2389 * during context switch.
2390 */
2391 smp_mb();
2392
2393 /*
2394 * If the task is on a CPU, set the CPU in the mask.
2395 * The detection is inaccurate as tasks might move or
2396 * schedule before the smp function call takes place.
2397 * In such a case the function call is pointless, but
2398 * there is no other side effect.
2399 */
2400 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
2401 cpumask_set_cpu(task_cpu(t), mask);
2402 }
2403 }
2404 read_unlock(&tasklist_lock);
2405 }
2406
free_all_child_rdtgrp(struct rdtgroup * rdtgrp)2407 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2408 {
2409 struct rdtgroup *sentry, *stmp;
2410 struct list_head *head;
2411
2412 head = &rdtgrp->mon.crdtgrp_list;
2413 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2414 free_rmid(sentry->mon.rmid);
2415 list_del(&sentry->mon.crdtgrp_list);
2416
2417 if (atomic_read(&sentry->waitcount) != 0)
2418 sentry->flags = RDT_DELETED;
2419 else
2420 rdtgroup_remove(sentry);
2421 }
2422 }
2423
2424 /*
2425 * Forcibly remove all of subdirectories under root.
2426 */
rmdir_all_sub(void)2427 static void rmdir_all_sub(void)
2428 {
2429 struct rdtgroup *rdtgrp, *tmp;
2430
2431 /* Move all tasks to the default resource group */
2432 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2433
2434 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2435 /* Free any child rmids */
2436 free_all_child_rdtgrp(rdtgrp);
2437
2438 /* Remove each rdtgroup other than root */
2439 if (rdtgrp == &rdtgroup_default)
2440 continue;
2441
2442 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2443 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2444 rdtgroup_pseudo_lock_remove(rdtgrp);
2445
2446 /*
2447 * Give any CPUs back to the default group. We cannot copy
2448 * cpu_online_mask because a CPU might have executed the
2449 * offline callback already, but is still marked online.
2450 */
2451 cpumask_or(&rdtgroup_default.cpu_mask,
2452 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2453
2454 free_rmid(rdtgrp->mon.rmid);
2455
2456 kernfs_remove(rdtgrp->kn);
2457 list_del(&rdtgrp->rdtgroup_list);
2458
2459 if (atomic_read(&rdtgrp->waitcount) != 0)
2460 rdtgrp->flags = RDT_DELETED;
2461 else
2462 rdtgroup_remove(rdtgrp);
2463 }
2464 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2465 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2466
2467 kernfs_remove(kn_info);
2468 kernfs_remove(kn_mongrp);
2469 kernfs_remove(kn_mondata);
2470 }
2471
rdt_kill_sb(struct super_block * sb)2472 static void rdt_kill_sb(struct super_block *sb)
2473 {
2474 struct rdt_resource *r;
2475
2476 cpus_read_lock();
2477 mutex_lock(&rdtgroup_mutex);
2478
2479 set_mba_sc(false);
2480
2481 /*Put everything back to default values. */
2482 for_each_alloc_enabled_rdt_resource(r)
2483 reset_all_ctrls(r);
2484 cdp_disable_all();
2485 rmdir_all_sub();
2486 rdt_pseudo_lock_release();
2487 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2488 schemata_list_destroy();
2489 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2490 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2491 static_branch_disable_cpuslocked(&rdt_enable_key);
2492 kernfs_kill_sb(sb);
2493 mutex_unlock(&rdtgroup_mutex);
2494 cpus_read_unlock();
2495 }
2496
2497 static struct file_system_type rdt_fs_type = {
2498 .name = "resctrl",
2499 .init_fs_context = rdt_init_fs_context,
2500 .parameters = rdt_fs_parameters,
2501 .kill_sb = rdt_kill_sb,
2502 };
2503
mon_addfile(struct kernfs_node * parent_kn,const char * name,void * priv)2504 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2505 void *priv)
2506 {
2507 struct kernfs_node *kn;
2508 int ret = 0;
2509
2510 kn = __kernfs_create_file(parent_kn, name, 0444,
2511 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
2512 &kf_mondata_ops, priv, NULL, NULL);
2513 if (IS_ERR(kn))
2514 return PTR_ERR(kn);
2515
2516 ret = rdtgroup_kn_set_ugid(kn);
2517 if (ret) {
2518 kernfs_remove(kn);
2519 return ret;
2520 }
2521
2522 return ret;
2523 }
2524
2525 /*
2526 * Remove all subdirectories of mon_data of ctrl_mon groups
2527 * and monitor groups with given domain id.
2528 */
rmdir_mondata_subdir_allrdtgrp(struct rdt_resource * r,unsigned int dom_id)2529 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
2530 {
2531 struct rdtgroup *prgrp, *crgrp;
2532 char name[32];
2533
2534 if (!r->mon_enabled)
2535 return;
2536
2537 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2538 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2539 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2540
2541 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2542 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2543 }
2544 }
2545
mkdir_mondata_subdir(struct kernfs_node * parent_kn,struct rdt_domain * d,struct rdt_resource * r,struct rdtgroup * prgrp)2546 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2547 struct rdt_domain *d,
2548 struct rdt_resource *r, struct rdtgroup *prgrp)
2549 {
2550 union mon_data_bits priv;
2551 struct kernfs_node *kn;
2552 struct mon_evt *mevt;
2553 struct rmid_read rr;
2554 char name[32];
2555 int ret;
2556
2557 sprintf(name, "mon_%s_%02d", r->name, d->id);
2558 /* create the directory */
2559 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2560 if (IS_ERR(kn))
2561 return PTR_ERR(kn);
2562
2563 ret = rdtgroup_kn_set_ugid(kn);
2564 if (ret)
2565 goto out_destroy;
2566
2567 if (WARN_ON(list_empty(&r->evt_list))) {
2568 ret = -EPERM;
2569 goto out_destroy;
2570 }
2571
2572 priv.u.rid = r->rid;
2573 priv.u.domid = d->id;
2574 list_for_each_entry(mevt, &r->evt_list, list) {
2575 priv.u.evtid = mevt->evtid;
2576 ret = mon_addfile(kn, mevt->name, priv.priv);
2577 if (ret)
2578 goto out_destroy;
2579
2580 if (is_mbm_event(mevt->evtid))
2581 mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
2582 }
2583 kernfs_activate(kn);
2584 return 0;
2585
2586 out_destroy:
2587 kernfs_remove(kn);
2588 return ret;
2589 }
2590
2591 /*
2592 * Add all subdirectories of mon_data for "ctrl_mon" groups
2593 * and "monitor" groups with given domain id.
2594 */
mkdir_mondata_subdir_allrdtgrp(struct rdt_resource * r,struct rdt_domain * d)2595 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2596 struct rdt_domain *d)
2597 {
2598 struct kernfs_node *parent_kn;
2599 struct rdtgroup *prgrp, *crgrp;
2600 struct list_head *head;
2601
2602 if (!r->mon_enabled)
2603 return;
2604
2605 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2606 parent_kn = prgrp->mon.mon_data_kn;
2607 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2608
2609 head = &prgrp->mon.crdtgrp_list;
2610 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2611 parent_kn = crgrp->mon.mon_data_kn;
2612 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2613 }
2614 }
2615 }
2616
mkdir_mondata_subdir_alldom(struct kernfs_node * parent_kn,struct rdt_resource * r,struct rdtgroup * prgrp)2617 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2618 struct rdt_resource *r,
2619 struct rdtgroup *prgrp)
2620 {
2621 struct rdt_domain *dom;
2622 int ret;
2623
2624 list_for_each_entry(dom, &r->domains, list) {
2625 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2626 if (ret)
2627 return ret;
2628 }
2629
2630 return 0;
2631 }
2632
2633 /*
2634 * This creates a directory mon_data which contains the monitored data.
2635 *
2636 * mon_data has one directory for each domain which are named
2637 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2638 * with L3 domain looks as below:
2639 * ./mon_data:
2640 * mon_L3_00
2641 * mon_L3_01
2642 * mon_L3_02
2643 * ...
2644 *
2645 * Each domain directory has one file per event:
2646 * ./mon_L3_00/:
2647 * llc_occupancy
2648 *
2649 */
mkdir_mondata_all(struct kernfs_node * parent_kn,struct rdtgroup * prgrp,struct kernfs_node ** dest_kn)2650 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2651 struct rdtgroup *prgrp,
2652 struct kernfs_node **dest_kn)
2653 {
2654 struct rdt_resource *r;
2655 struct kernfs_node *kn;
2656 int ret;
2657
2658 /*
2659 * Create the mon_data directory first.
2660 */
2661 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
2662 if (ret)
2663 return ret;
2664
2665 if (dest_kn)
2666 *dest_kn = kn;
2667
2668 /*
2669 * Create the subdirectories for each domain. Note that all events
2670 * in a domain like L3 are grouped into a resource whose domain is L3
2671 */
2672 for_each_mon_enabled_rdt_resource(r) {
2673 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2674 if (ret)
2675 goto out_destroy;
2676 }
2677
2678 return 0;
2679
2680 out_destroy:
2681 kernfs_remove(kn);
2682 return ret;
2683 }
2684
2685 /**
2686 * cbm_ensure_valid - Enforce validity on provided CBM
2687 * @_val: Candidate CBM
2688 * @r: RDT resource to which the CBM belongs
2689 *
2690 * The provided CBM represents all cache portions available for use. This
2691 * may be represented by a bitmap that does not consist of contiguous ones
2692 * and thus be an invalid CBM.
2693 * Here the provided CBM is forced to be a valid CBM by only considering
2694 * the first set of contiguous bits as valid and clearing all bits.
2695 * The intention here is to provide a valid default CBM with which a new
2696 * resource group is initialized. The user can follow this with a
2697 * modification to the CBM if the default does not satisfy the
2698 * requirements.
2699 */
cbm_ensure_valid(u32 _val,struct rdt_resource * r)2700 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
2701 {
2702 unsigned int cbm_len = r->cache.cbm_len;
2703 unsigned long first_bit, zero_bit;
2704 unsigned long val = _val;
2705
2706 if (!val)
2707 return 0;
2708
2709 first_bit = find_first_bit(&val, cbm_len);
2710 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
2711
2712 /* Clear any remaining bits to ensure contiguous region */
2713 bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
2714 return (u32)val;
2715 }
2716
2717 /*
2718 * Initialize cache resources per RDT domain
2719 *
2720 * Set the RDT domain up to start off with all usable allocations. That is,
2721 * all shareable and unused bits. All-zero CBM is invalid.
2722 */
__init_one_rdt_domain(struct rdt_domain * d,struct resctrl_schema * s,u32 closid)2723 static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
2724 u32 closid)
2725 {
2726 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
2727 enum resctrl_conf_type t = s->conf_type;
2728 struct resctrl_staged_config *cfg;
2729 struct rdt_resource *r = s->res;
2730 u32 used_b = 0, unused_b = 0;
2731 unsigned long tmp_cbm;
2732 enum rdtgrp_mode mode;
2733 u32 peer_ctl, ctrl_val;
2734 int i;
2735
2736 cfg = &d->staged_config[t];
2737 cfg->have_new_ctrl = false;
2738 cfg->new_ctrl = r->cache.shareable_bits;
2739 used_b = r->cache.shareable_bits;
2740 for (i = 0; i < closids_supported(); i++) {
2741 if (closid_allocated(i) && i != closid) {
2742 mode = rdtgroup_mode_by_closid(i);
2743 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
2744 /*
2745 * ctrl values for locksetup aren't relevant
2746 * until the schemata is written, and the mode
2747 * becomes RDT_MODE_PSEUDO_LOCKED.
2748 */
2749 continue;
2750 /*
2751 * If CDP is active include peer domain's
2752 * usage to ensure there is no overlap
2753 * with an exclusive group.
2754 */
2755 if (resctrl_arch_get_cdp_enabled(r->rid))
2756 peer_ctl = resctrl_arch_get_config(r, d, i,
2757 peer_type);
2758 else
2759 peer_ctl = 0;
2760 ctrl_val = resctrl_arch_get_config(r, d, i,
2761 s->conf_type);
2762 used_b |= ctrl_val | peer_ctl;
2763 if (mode == RDT_MODE_SHAREABLE)
2764 cfg->new_ctrl |= ctrl_val | peer_ctl;
2765 }
2766 }
2767 if (d->plr && d->plr->cbm > 0)
2768 used_b |= d->plr->cbm;
2769 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
2770 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
2771 cfg->new_ctrl |= unused_b;
2772 /*
2773 * Force the initial CBM to be valid, user can
2774 * modify the CBM based on system availability.
2775 */
2776 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
2777 /*
2778 * Assign the u32 CBM to an unsigned long to ensure that
2779 * bitmap_weight() does not access out-of-bound memory.
2780 */
2781 tmp_cbm = cfg->new_ctrl;
2782 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
2783 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id);
2784 return -ENOSPC;
2785 }
2786 cfg->have_new_ctrl = true;
2787
2788 return 0;
2789 }
2790
2791 /*
2792 * Initialize cache resources with default values.
2793 *
2794 * A new RDT group is being created on an allocation capable (CAT)
2795 * supporting system. Set this group up to start off with all usable
2796 * allocations.
2797 *
2798 * If there are no more shareable bits available on any domain then
2799 * the entire allocation will fail.
2800 */
rdtgroup_init_cat(struct resctrl_schema * s,u32 closid)2801 static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
2802 {
2803 struct rdt_domain *d;
2804 int ret;
2805
2806 list_for_each_entry(d, &s->res->domains, list) {
2807 ret = __init_one_rdt_domain(d, s, closid);
2808 if (ret < 0)
2809 return ret;
2810 }
2811
2812 return 0;
2813 }
2814
2815 /* Initialize MBA resource with default values. */
rdtgroup_init_mba(struct rdt_resource * r)2816 static void rdtgroup_init_mba(struct rdt_resource *r)
2817 {
2818 struct resctrl_staged_config *cfg;
2819 struct rdt_domain *d;
2820
2821 list_for_each_entry(d, &r->domains, list) {
2822 cfg = &d->staged_config[CDP_NONE];
2823 cfg->new_ctrl = is_mba_sc(r) ? MBA_MAX_MBPS : r->default_ctrl;
2824 cfg->have_new_ctrl = true;
2825 }
2826 }
2827
2828 /* Initialize the RDT group's allocations. */
rdtgroup_init_alloc(struct rdtgroup * rdtgrp)2829 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
2830 {
2831 struct resctrl_schema *s;
2832 struct rdt_resource *r;
2833 int ret = 0;
2834
2835 rdt_staged_configs_clear();
2836
2837 list_for_each_entry(s, &resctrl_schema_all, list) {
2838 r = s->res;
2839 if (r->rid == RDT_RESOURCE_MBA) {
2840 rdtgroup_init_mba(r);
2841 } else {
2842 ret = rdtgroup_init_cat(s, rdtgrp->closid);
2843 if (ret < 0)
2844 goto out;
2845 }
2846
2847 ret = resctrl_arch_update_domains(r, rdtgrp->closid);
2848 if (ret < 0) {
2849 rdt_last_cmd_puts("Failed to initialize allocations\n");
2850 goto out;
2851 }
2852
2853 }
2854
2855 rdtgrp->mode = RDT_MODE_SHAREABLE;
2856
2857 out:
2858 rdt_staged_configs_clear();
2859 return ret;
2860 }
2861
mkdir_rdt_prepare(struct kernfs_node * parent_kn,const char * name,umode_t mode,enum rdt_group_type rtype,struct rdtgroup ** r)2862 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
2863 const char *name, umode_t mode,
2864 enum rdt_group_type rtype, struct rdtgroup **r)
2865 {
2866 struct rdtgroup *prdtgrp, *rdtgrp;
2867 struct kernfs_node *kn;
2868 uint files = 0;
2869 int ret;
2870
2871 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
2872 if (!prdtgrp) {
2873 ret = -ENODEV;
2874 goto out_unlock;
2875 }
2876
2877 if (rtype == RDTMON_GROUP &&
2878 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2879 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
2880 ret = -EINVAL;
2881 rdt_last_cmd_puts("Pseudo-locking in progress\n");
2882 goto out_unlock;
2883 }
2884
2885 /* allocate the rdtgroup. */
2886 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
2887 if (!rdtgrp) {
2888 ret = -ENOSPC;
2889 rdt_last_cmd_puts("Kernel out of memory\n");
2890 goto out_unlock;
2891 }
2892 *r = rdtgrp;
2893 rdtgrp->mon.parent = prdtgrp;
2894 rdtgrp->type = rtype;
2895 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
2896
2897 /* kernfs creates the directory for rdtgrp */
2898 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
2899 if (IS_ERR(kn)) {
2900 ret = PTR_ERR(kn);
2901 rdt_last_cmd_puts("kernfs create error\n");
2902 goto out_free_rgrp;
2903 }
2904 rdtgrp->kn = kn;
2905
2906 /*
2907 * kernfs_remove() will drop the reference count on "kn" which
2908 * will free it. But we still need it to stick around for the
2909 * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
2910 * which will be dropped by kernfs_put() in rdtgroup_remove().
2911 */
2912 kernfs_get(kn);
2913
2914 ret = rdtgroup_kn_set_ugid(kn);
2915 if (ret) {
2916 rdt_last_cmd_puts("kernfs perm error\n");
2917 goto out_destroy;
2918 }
2919
2920 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
2921 ret = rdtgroup_add_files(kn, files);
2922 if (ret) {
2923 rdt_last_cmd_puts("kernfs fill error\n");
2924 goto out_destroy;
2925 }
2926
2927 if (rdt_mon_capable) {
2928 ret = alloc_rmid();
2929 if (ret < 0) {
2930 rdt_last_cmd_puts("Out of RMIDs\n");
2931 goto out_destroy;
2932 }
2933 rdtgrp->mon.rmid = ret;
2934
2935 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
2936 if (ret) {
2937 rdt_last_cmd_puts("kernfs subdir error\n");
2938 goto out_idfree;
2939 }
2940 }
2941 kernfs_activate(kn);
2942
2943 /*
2944 * The caller unlocks the parent_kn upon success.
2945 */
2946 return 0;
2947
2948 out_idfree:
2949 free_rmid(rdtgrp->mon.rmid);
2950 out_destroy:
2951 kernfs_put(rdtgrp->kn);
2952 kernfs_remove(rdtgrp->kn);
2953 out_free_rgrp:
2954 kfree(rdtgrp);
2955 out_unlock:
2956 rdtgroup_kn_unlock(parent_kn);
2957 return ret;
2958 }
2959
mkdir_rdt_prepare_clean(struct rdtgroup * rgrp)2960 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
2961 {
2962 kernfs_remove(rgrp->kn);
2963 free_rmid(rgrp->mon.rmid);
2964 rdtgroup_remove(rgrp);
2965 }
2966
2967 /*
2968 * Create a monitor group under "mon_groups" directory of a control
2969 * and monitor group(ctrl_mon). This is a resource group
2970 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
2971 */
rdtgroup_mkdir_mon(struct kernfs_node * parent_kn,const char * name,umode_t mode)2972 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
2973 const char *name, umode_t mode)
2974 {
2975 struct rdtgroup *rdtgrp, *prgrp;
2976 int ret;
2977
2978 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
2979 if (ret)
2980 return ret;
2981
2982 prgrp = rdtgrp->mon.parent;
2983 rdtgrp->closid = prgrp->closid;
2984
2985 /*
2986 * Add the rdtgrp to the list of rdtgrps the parent
2987 * ctrl_mon group has to track.
2988 */
2989 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
2990
2991 rdtgroup_kn_unlock(parent_kn);
2992 return ret;
2993 }
2994
2995 /*
2996 * These are rdtgroups created under the root directory. Can be used
2997 * to allocate and monitor resources.
2998 */
rdtgroup_mkdir_ctrl_mon(struct kernfs_node * parent_kn,const char * name,umode_t mode)2999 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
3000 const char *name, umode_t mode)
3001 {
3002 struct rdtgroup *rdtgrp;
3003 struct kernfs_node *kn;
3004 u32 closid;
3005 int ret;
3006
3007 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
3008 if (ret)
3009 return ret;
3010
3011 kn = rdtgrp->kn;
3012 ret = closid_alloc();
3013 if (ret < 0) {
3014 rdt_last_cmd_puts("Out of CLOSIDs\n");
3015 goto out_common_fail;
3016 }
3017 closid = ret;
3018 ret = 0;
3019
3020 rdtgrp->closid = closid;
3021 ret = rdtgroup_init_alloc(rdtgrp);
3022 if (ret < 0)
3023 goto out_id_free;
3024
3025 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
3026
3027 if (rdt_mon_capable) {
3028 /*
3029 * Create an empty mon_groups directory to hold the subset
3030 * of tasks and cpus to monitor.
3031 */
3032 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
3033 if (ret) {
3034 rdt_last_cmd_puts("kernfs subdir error\n");
3035 goto out_del_list;
3036 }
3037 }
3038
3039 goto out_unlock;
3040
3041 out_del_list:
3042 list_del(&rdtgrp->rdtgroup_list);
3043 out_id_free:
3044 closid_free(closid);
3045 out_common_fail:
3046 mkdir_rdt_prepare_clean(rdtgrp);
3047 out_unlock:
3048 rdtgroup_kn_unlock(parent_kn);
3049 return ret;
3050 }
3051
3052 /*
3053 * We allow creating mon groups only with in a directory called "mon_groups"
3054 * which is present in every ctrl_mon group. Check if this is a valid
3055 * "mon_groups" directory.
3056 *
3057 * 1. The directory should be named "mon_groups".
3058 * 2. The mon group itself should "not" be named "mon_groups".
3059 * This makes sure "mon_groups" directory always has a ctrl_mon group
3060 * as parent.
3061 */
is_mon_groups(struct kernfs_node * kn,const char * name)3062 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
3063 {
3064 return (!strcmp(kn->name, "mon_groups") &&
3065 strcmp(name, "mon_groups"));
3066 }
3067
rdtgroup_mkdir(struct kernfs_node * parent_kn,const char * name,umode_t mode)3068 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3069 umode_t mode)
3070 {
3071 /* Do not accept '\n' to avoid unparsable situation. */
3072 if (strchr(name, '\n'))
3073 return -EINVAL;
3074
3075 /*
3076 * If the parent directory is the root directory and RDT
3077 * allocation is supported, add a control and monitoring
3078 * subdirectory
3079 */
3080 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
3081 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
3082
3083 /*
3084 * If RDT monitoring is supported and the parent directory is a valid
3085 * "mon_groups" directory, add a monitoring subdirectory.
3086 */
3087 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
3088 return rdtgroup_mkdir_mon(parent_kn, name, mode);
3089
3090 return -EPERM;
3091 }
3092
rdtgroup_rmdir_mon(struct rdtgroup * rdtgrp,cpumask_var_t tmpmask)3093 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3094 {
3095 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3096 int cpu;
3097
3098 /* Give any tasks back to the parent group */
3099 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
3100
3101 /* Update per cpu rmid of the moved CPUs first */
3102 for_each_cpu(cpu, &rdtgrp->cpu_mask)
3103 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
3104 /*
3105 * Update the MSR on moved CPUs and CPUs which have moved
3106 * task running on them.
3107 */
3108 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3109 update_closid_rmid(tmpmask, NULL);
3110
3111 rdtgrp->flags = RDT_DELETED;
3112 free_rmid(rdtgrp->mon.rmid);
3113
3114 /*
3115 * Remove the rdtgrp from the parent ctrl_mon group's list
3116 */
3117 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3118 list_del(&rdtgrp->mon.crdtgrp_list);
3119
3120 kernfs_remove(rdtgrp->kn);
3121
3122 return 0;
3123 }
3124
rdtgroup_ctrl_remove(struct rdtgroup * rdtgrp)3125 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
3126 {
3127 rdtgrp->flags = RDT_DELETED;
3128 list_del(&rdtgrp->rdtgroup_list);
3129
3130 kernfs_remove(rdtgrp->kn);
3131 return 0;
3132 }
3133
rdtgroup_rmdir_ctrl(struct rdtgroup * rdtgrp,cpumask_var_t tmpmask)3134 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3135 {
3136 int cpu;
3137
3138 /* Give any tasks back to the default group */
3139 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
3140
3141 /* Give any CPUs back to the default group */
3142 cpumask_or(&rdtgroup_default.cpu_mask,
3143 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
3144
3145 /* Update per cpu closid and rmid of the moved CPUs first */
3146 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
3147 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3148 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
3149 }
3150
3151 /*
3152 * Update the MSR on moved CPUs and CPUs which have moved
3153 * task running on them.
3154 */
3155 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3156 update_closid_rmid(tmpmask, NULL);
3157
3158 closid_free(rdtgrp->closid);
3159 free_rmid(rdtgrp->mon.rmid);
3160
3161 rdtgroup_ctrl_remove(rdtgrp);
3162
3163 /*
3164 * Free all the child monitor group rmids.
3165 */
3166 free_all_child_rdtgrp(rdtgrp);
3167
3168 return 0;
3169 }
3170
rdtgroup_rmdir(struct kernfs_node * kn)3171 static int rdtgroup_rmdir(struct kernfs_node *kn)
3172 {
3173 struct kernfs_node *parent_kn = kn->parent;
3174 struct rdtgroup *rdtgrp;
3175 cpumask_var_t tmpmask;
3176 int ret = 0;
3177
3178 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3179 return -ENOMEM;
3180
3181 rdtgrp = rdtgroup_kn_lock_live(kn);
3182 if (!rdtgrp) {
3183 ret = -EPERM;
3184 goto out;
3185 }
3186
3187 /*
3188 * If the rdtgroup is a ctrl_mon group and parent directory
3189 * is the root directory, remove the ctrl_mon group.
3190 *
3191 * If the rdtgroup is a mon group and parent directory
3192 * is a valid "mon_groups" directory, remove the mon group.
3193 */
3194 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3195 rdtgrp != &rdtgroup_default) {
3196 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3197 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
3198 ret = rdtgroup_ctrl_remove(rdtgrp);
3199 } else {
3200 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
3201 }
3202 } else if (rdtgrp->type == RDTMON_GROUP &&
3203 is_mon_groups(parent_kn, kn->name)) {
3204 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
3205 } else {
3206 ret = -EPERM;
3207 }
3208
3209 out:
3210 rdtgroup_kn_unlock(kn);
3211 free_cpumask_var(tmpmask);
3212 return ret;
3213 }
3214
rdtgroup_show_options(struct seq_file * seq,struct kernfs_root * kf)3215 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3216 {
3217 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
3218 seq_puts(seq, ",cdp");
3219
3220 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
3221 seq_puts(seq, ",cdpl2");
3222
3223 if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
3224 seq_puts(seq, ",mba_MBps");
3225
3226 return 0;
3227 }
3228
3229 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
3230 .mkdir = rdtgroup_mkdir,
3231 .rmdir = rdtgroup_rmdir,
3232 .show_options = rdtgroup_show_options,
3233 };
3234
rdtgroup_setup_root(void)3235 static int __init rdtgroup_setup_root(void)
3236 {
3237 int ret;
3238
3239 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
3240 KERNFS_ROOT_CREATE_DEACTIVATED |
3241 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
3242 &rdtgroup_default);
3243 if (IS_ERR(rdt_root))
3244 return PTR_ERR(rdt_root);
3245
3246 mutex_lock(&rdtgroup_mutex);
3247
3248 rdtgroup_default.closid = 0;
3249 rdtgroup_default.mon.rmid = 0;
3250 rdtgroup_default.type = RDTCTRL_GROUP;
3251 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3252
3253 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3254
3255 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
3256 if (ret) {
3257 kernfs_destroy_root(rdt_root);
3258 goto out;
3259 }
3260
3261 rdtgroup_default.kn = rdt_root->kn;
3262 kernfs_activate(rdtgroup_default.kn);
3263
3264 out:
3265 mutex_unlock(&rdtgroup_mutex);
3266
3267 return ret;
3268 }
3269
3270 /*
3271 * rdtgroup_init - rdtgroup initialization
3272 *
3273 * Setup resctrl file system including set up root, create mount point,
3274 * register rdtgroup filesystem, and initialize files under root directory.
3275 *
3276 * Return: 0 on success or -errno
3277 */
rdtgroup_init(void)3278 int __init rdtgroup_init(void)
3279 {
3280 int ret = 0;
3281
3282 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
3283 sizeof(last_cmd_status_buf));
3284
3285 ret = rdtgroup_setup_root();
3286 if (ret)
3287 return ret;
3288
3289 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
3290 if (ret)
3291 goto cleanup_root;
3292
3293 ret = register_filesystem(&rdt_fs_type);
3294 if (ret)
3295 goto cleanup_mountpoint;
3296
3297 /*
3298 * Adding the resctrl debugfs directory here may not be ideal since
3299 * it would let the resctrl debugfs directory appear on the debugfs
3300 * filesystem before the resctrl filesystem is mounted.
3301 * It may also be ok since that would enable debugging of RDT before
3302 * resctrl is mounted.
3303 * The reason why the debugfs directory is created here and not in
3304 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
3305 * during the debugfs directory creation also &sb->s_type->i_mutex_key
3306 * (the lockdep class of inode->i_rwsem). Other filesystem
3307 * interactions (eg. SyS_getdents) have the lock ordering:
3308 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
3309 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
3310 * is taken, thus creating dependency:
3311 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
3312 * issues considering the other two lock dependencies.
3313 * By creating the debugfs directory here we avoid a dependency
3314 * that may cause deadlock (even though file operations cannot
3315 * occur until the filesystem is mounted, but I do not know how to
3316 * tell lockdep that).
3317 */
3318 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
3319
3320 return 0;
3321
3322 cleanup_mountpoint:
3323 sysfs_remove_mount_point(fs_kobj, "resctrl");
3324 cleanup_root:
3325 kernfs_destroy_root(rdt_root);
3326
3327 return ret;
3328 }
3329
rdtgroup_exit(void)3330 void __exit rdtgroup_exit(void)
3331 {
3332 debugfs_remove_recursive(debugfs_resctrl);
3333 unregister_filesystem(&rdt_fs_type);
3334 sysfs_remove_mount_point(fs_kobj, "resctrl");
3335 kernfs_destroy_root(rdt_root);
3336 }
3337