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 on_each_cpu_mask(cpu_mask, update_cpu_closid_rmid, r, 1);
342 }
343
cpus_mon_write(struct rdtgroup * rdtgrp,cpumask_var_t newmask,cpumask_var_t tmpmask)344 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
345 cpumask_var_t tmpmask)
346 {
347 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
348 struct list_head *head;
349
350 /* Check whether cpus belong to parent ctrl group */
351 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
352 if (!cpumask_empty(tmpmask)) {
353 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
354 return -EINVAL;
355 }
356
357 /* Check whether cpus are dropped from this group */
358 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
359 if (!cpumask_empty(tmpmask)) {
360 /* Give any dropped cpus to parent rdtgroup */
361 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
362 update_closid_rmid(tmpmask, prgrp);
363 }
364
365 /*
366 * If we added cpus, remove them from previous group that owned them
367 * and update per-cpu rmid
368 */
369 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
370 if (!cpumask_empty(tmpmask)) {
371 head = &prgrp->mon.crdtgrp_list;
372 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
373 if (crgrp == rdtgrp)
374 continue;
375 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
376 tmpmask);
377 }
378 update_closid_rmid(tmpmask, rdtgrp);
379 }
380
381 /* Done pushing/pulling - update this group with new mask */
382 cpumask_copy(&rdtgrp->cpu_mask, newmask);
383
384 return 0;
385 }
386
cpumask_rdtgrp_clear(struct rdtgroup * r,struct cpumask * m)387 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
388 {
389 struct rdtgroup *crgrp;
390
391 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
392 /* update the child mon group masks as well*/
393 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
394 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
395 }
396
cpus_ctrl_write(struct rdtgroup * rdtgrp,cpumask_var_t newmask,cpumask_var_t tmpmask,cpumask_var_t tmpmask1)397 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
398 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
399 {
400 struct rdtgroup *r, *crgrp;
401 struct list_head *head;
402
403 /* Check whether cpus are dropped from this group */
404 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
405 if (!cpumask_empty(tmpmask)) {
406 /* Can't drop from default group */
407 if (rdtgrp == &rdtgroup_default) {
408 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
409 return -EINVAL;
410 }
411
412 /* Give any dropped cpus to rdtgroup_default */
413 cpumask_or(&rdtgroup_default.cpu_mask,
414 &rdtgroup_default.cpu_mask, tmpmask);
415 update_closid_rmid(tmpmask, &rdtgroup_default);
416 }
417
418 /*
419 * If we added cpus, remove them from previous group and
420 * the prev group's child groups that owned them
421 * and update per-cpu closid/rmid.
422 */
423 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
424 if (!cpumask_empty(tmpmask)) {
425 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
426 if (r == rdtgrp)
427 continue;
428 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
429 if (!cpumask_empty(tmpmask1))
430 cpumask_rdtgrp_clear(r, tmpmask1);
431 }
432 update_closid_rmid(tmpmask, rdtgrp);
433 }
434
435 /* Done pushing/pulling - update this group with new mask */
436 cpumask_copy(&rdtgrp->cpu_mask, newmask);
437
438 /*
439 * Clear child mon group masks since there is a new parent mask
440 * now and update the rmid for the cpus the child lost.
441 */
442 head = &rdtgrp->mon.crdtgrp_list;
443 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
444 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
445 update_closid_rmid(tmpmask, rdtgrp);
446 cpumask_clear(&crgrp->cpu_mask);
447 }
448
449 return 0;
450 }
451
rdtgroup_cpus_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)452 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
453 char *buf, size_t nbytes, loff_t off)
454 {
455 cpumask_var_t tmpmask, newmask, tmpmask1;
456 struct rdtgroup *rdtgrp;
457 int ret;
458
459 if (!buf)
460 return -EINVAL;
461
462 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
463 return -ENOMEM;
464 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
465 free_cpumask_var(tmpmask);
466 return -ENOMEM;
467 }
468 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
469 free_cpumask_var(tmpmask);
470 free_cpumask_var(newmask);
471 return -ENOMEM;
472 }
473
474 rdtgrp = rdtgroup_kn_lock_live(of->kn);
475 if (!rdtgrp) {
476 ret = -ENOENT;
477 goto unlock;
478 }
479
480 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
481 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
482 ret = -EINVAL;
483 rdt_last_cmd_puts("Pseudo-locking in progress\n");
484 goto unlock;
485 }
486
487 if (is_cpu_list(of))
488 ret = cpulist_parse(buf, newmask);
489 else
490 ret = cpumask_parse(buf, newmask);
491
492 if (ret) {
493 rdt_last_cmd_puts("Bad CPU list/mask\n");
494 goto unlock;
495 }
496
497 /* check that user didn't specify any offline cpus */
498 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
499 if (!cpumask_empty(tmpmask)) {
500 ret = -EINVAL;
501 rdt_last_cmd_puts("Can only assign online CPUs\n");
502 goto unlock;
503 }
504
505 if (rdtgrp->type == RDTCTRL_GROUP)
506 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
507 else if (rdtgrp->type == RDTMON_GROUP)
508 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
509 else
510 ret = -EINVAL;
511
512 unlock:
513 rdtgroup_kn_unlock(of->kn);
514 free_cpumask_var(tmpmask);
515 free_cpumask_var(newmask);
516 free_cpumask_var(tmpmask1);
517
518 return ret ?: nbytes;
519 }
520
521 /**
522 * rdtgroup_remove - the helper to remove resource group safely
523 * @rdtgrp: resource group to remove
524 *
525 * On resource group creation via a mkdir, an extra kernfs_node reference is
526 * taken to ensure that the rdtgroup structure remains accessible for the
527 * rdtgroup_kn_unlock() calls where it is removed.
528 *
529 * Drop the extra reference here, then free the rdtgroup structure.
530 *
531 * Return: void
532 */
rdtgroup_remove(struct rdtgroup * rdtgrp)533 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
534 {
535 kernfs_put(rdtgrp->kn);
536 kfree(rdtgrp);
537 }
538
_update_task_closid_rmid(void * task)539 static void _update_task_closid_rmid(void *task)
540 {
541 /*
542 * If the task is still current on this CPU, update PQR_ASSOC MSR.
543 * Otherwise, the MSR is updated when the task is scheduled in.
544 */
545 if (task == current)
546 resctrl_sched_in(task);
547 }
548
update_task_closid_rmid(struct task_struct * t)549 static void update_task_closid_rmid(struct task_struct *t)
550 {
551 if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
552 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
553 else
554 _update_task_closid_rmid(t);
555 }
556
__rdtgroup_move_task(struct task_struct * tsk,struct rdtgroup * rdtgrp)557 static int __rdtgroup_move_task(struct task_struct *tsk,
558 struct rdtgroup *rdtgrp)
559 {
560 /* If the task is already in rdtgrp, no need to move the task. */
561 if ((rdtgrp->type == RDTCTRL_GROUP && tsk->closid == rdtgrp->closid &&
562 tsk->rmid == rdtgrp->mon.rmid) ||
563 (rdtgrp->type == RDTMON_GROUP && tsk->rmid == rdtgrp->mon.rmid &&
564 tsk->closid == rdtgrp->mon.parent->closid))
565 return 0;
566
567 /*
568 * Set the task's closid/rmid before the PQR_ASSOC MSR can be
569 * updated by them.
570 *
571 * For ctrl_mon groups, move both closid and rmid.
572 * For monitor groups, can move the tasks only from
573 * their parent CTRL group.
574 */
575
576 if (rdtgrp->type == RDTCTRL_GROUP) {
577 WRITE_ONCE(tsk->closid, rdtgrp->closid);
578 WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
579 } else if (rdtgrp->type == RDTMON_GROUP) {
580 if (rdtgrp->mon.parent->closid == tsk->closid) {
581 WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
582 } else {
583 rdt_last_cmd_puts("Can't move task to different control group\n");
584 return -EINVAL;
585 }
586 }
587
588 /*
589 * Ensure the task's closid and rmid are written before determining if
590 * the task is current that will decide if it will be interrupted.
591 * This pairs with the full barrier between the rq->curr update and
592 * resctrl_sched_in() during context switch.
593 */
594 smp_mb();
595
596 /*
597 * By now, the task's closid and rmid are set. If the task is current
598 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
599 * group go into effect. If the task is not current, the MSR will be
600 * updated when the task is scheduled in.
601 */
602 update_task_closid_rmid(tsk);
603
604 return 0;
605 }
606
is_closid_match(struct task_struct * t,struct rdtgroup * r)607 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
608 {
609 return (rdt_alloc_capable &&
610 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
611 }
612
is_rmid_match(struct task_struct * t,struct rdtgroup * r)613 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
614 {
615 return (rdt_mon_capable &&
616 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
617 }
618
619 /**
620 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
621 * @r: Resource group
622 *
623 * Return: 1 if tasks have been assigned to @r, 0 otherwise
624 */
rdtgroup_tasks_assigned(struct rdtgroup * r)625 int rdtgroup_tasks_assigned(struct rdtgroup *r)
626 {
627 struct task_struct *p, *t;
628 int ret = 0;
629
630 lockdep_assert_held(&rdtgroup_mutex);
631
632 rcu_read_lock();
633 for_each_process_thread(p, t) {
634 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
635 ret = 1;
636 break;
637 }
638 }
639 rcu_read_unlock();
640
641 return ret;
642 }
643
rdtgroup_task_write_permission(struct task_struct * task,struct kernfs_open_file * of)644 static int rdtgroup_task_write_permission(struct task_struct *task,
645 struct kernfs_open_file *of)
646 {
647 const struct cred *tcred = get_task_cred(task);
648 const struct cred *cred = current_cred();
649 int ret = 0;
650
651 /*
652 * Even if we're attaching all tasks in the thread group, we only
653 * need to check permissions on one of them.
654 */
655 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
656 !uid_eq(cred->euid, tcred->uid) &&
657 !uid_eq(cred->euid, tcred->suid)) {
658 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
659 ret = -EPERM;
660 }
661
662 put_cred(tcred);
663 return ret;
664 }
665
rdtgroup_move_task(pid_t pid,struct rdtgroup * rdtgrp,struct kernfs_open_file * of)666 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
667 struct kernfs_open_file *of)
668 {
669 struct task_struct *tsk;
670 int ret;
671
672 rcu_read_lock();
673 if (pid) {
674 tsk = find_task_by_vpid(pid);
675 if (!tsk) {
676 rcu_read_unlock();
677 rdt_last_cmd_printf("No task %d\n", pid);
678 return -ESRCH;
679 }
680 } else {
681 tsk = current;
682 }
683
684 get_task_struct(tsk);
685 rcu_read_unlock();
686
687 ret = rdtgroup_task_write_permission(tsk, of);
688 if (!ret)
689 ret = __rdtgroup_move_task(tsk, rdtgrp);
690
691 put_task_struct(tsk);
692 return ret;
693 }
694
rdtgroup_tasks_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)695 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
696 char *buf, size_t nbytes, loff_t off)
697 {
698 struct rdtgroup *rdtgrp;
699 int ret = 0;
700 pid_t pid;
701
702 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
703 return -EINVAL;
704 rdtgrp = rdtgroup_kn_lock_live(of->kn);
705 if (!rdtgrp) {
706 rdtgroup_kn_unlock(of->kn);
707 return -ENOENT;
708 }
709 rdt_last_cmd_clear();
710
711 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
712 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
713 ret = -EINVAL;
714 rdt_last_cmd_puts("Pseudo-locking in progress\n");
715 goto unlock;
716 }
717
718 ret = rdtgroup_move_task(pid, rdtgrp, of);
719
720 unlock:
721 rdtgroup_kn_unlock(of->kn);
722
723 return ret ?: nbytes;
724 }
725
show_rdt_tasks(struct rdtgroup * r,struct seq_file * s)726 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
727 {
728 struct task_struct *p, *t;
729 pid_t pid;
730
731 rcu_read_lock();
732 for_each_process_thread(p, t) {
733 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
734 pid = task_pid_vnr(t);
735 if (pid)
736 seq_printf(s, "%d\n", pid);
737 }
738 }
739 rcu_read_unlock();
740 }
741
rdtgroup_tasks_show(struct kernfs_open_file * of,struct seq_file * s,void * v)742 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
743 struct seq_file *s, void *v)
744 {
745 struct rdtgroup *rdtgrp;
746 int ret = 0;
747
748 rdtgrp = rdtgroup_kn_lock_live(of->kn);
749 if (rdtgrp)
750 show_rdt_tasks(rdtgrp, s);
751 else
752 ret = -ENOENT;
753 rdtgroup_kn_unlock(of->kn);
754
755 return ret;
756 }
757
758 #ifdef CONFIG_PROC_CPU_RESCTRL
759
760 /*
761 * A task can only be part of one resctrl control group and of one monitor
762 * group which is associated to that control group.
763 *
764 * 1) res:
765 * mon:
766 *
767 * resctrl is not available.
768 *
769 * 2) res:/
770 * mon:
771 *
772 * Task is part of the root resctrl control group, and it is not associated
773 * to any monitor group.
774 *
775 * 3) res:/
776 * mon:mon0
777 *
778 * Task is part of the root resctrl control group and monitor group mon0.
779 *
780 * 4) res:group0
781 * mon:
782 *
783 * Task is part of resctrl control group group0, and it is not associated
784 * to any monitor group.
785 *
786 * 5) res:group0
787 * mon:mon1
788 *
789 * Task is part of resctrl control group group0 and monitor group mon1.
790 */
proc_resctrl_show(struct seq_file * s,struct pid_namespace * ns,struct pid * pid,struct task_struct * tsk)791 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
792 struct pid *pid, struct task_struct *tsk)
793 {
794 struct rdtgroup *rdtg;
795 int ret = 0;
796
797 mutex_lock(&rdtgroup_mutex);
798
799 /* Return empty if resctrl has not been mounted. */
800 if (!static_branch_unlikely(&rdt_enable_key)) {
801 seq_puts(s, "res:\nmon:\n");
802 goto unlock;
803 }
804
805 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
806 struct rdtgroup *crg;
807
808 /*
809 * Task information is only relevant for shareable
810 * and exclusive groups.
811 */
812 if (rdtg->mode != RDT_MODE_SHAREABLE &&
813 rdtg->mode != RDT_MODE_EXCLUSIVE)
814 continue;
815
816 if (rdtg->closid != tsk->closid)
817 continue;
818
819 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
820 rdtg->kn->name);
821 seq_puts(s, "mon:");
822 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
823 mon.crdtgrp_list) {
824 if (tsk->rmid != crg->mon.rmid)
825 continue;
826 seq_printf(s, "%s", crg->kn->name);
827 break;
828 }
829 seq_putc(s, '\n');
830 goto unlock;
831 }
832 /*
833 * The above search should succeed. Otherwise return
834 * with an error.
835 */
836 ret = -ENOENT;
837 unlock:
838 mutex_unlock(&rdtgroup_mutex);
839
840 return ret;
841 }
842 #endif
843
rdt_last_cmd_status_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)844 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
845 struct seq_file *seq, void *v)
846 {
847 int len;
848
849 mutex_lock(&rdtgroup_mutex);
850 len = seq_buf_used(&last_cmd_status);
851 if (len)
852 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
853 else
854 seq_puts(seq, "ok\n");
855 mutex_unlock(&rdtgroup_mutex);
856 return 0;
857 }
858
rdt_num_closids_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)859 static int rdt_num_closids_show(struct kernfs_open_file *of,
860 struct seq_file *seq, void *v)
861 {
862 struct resctrl_schema *s = of->kn->parent->priv;
863
864 seq_printf(seq, "%u\n", s->num_closid);
865 return 0;
866 }
867
rdt_default_ctrl_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)868 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
869 struct seq_file *seq, void *v)
870 {
871 struct resctrl_schema *s = of->kn->parent->priv;
872 struct rdt_resource *r = s->res;
873
874 seq_printf(seq, "%x\n", r->default_ctrl);
875 return 0;
876 }
877
rdt_min_cbm_bits_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)878 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
879 struct seq_file *seq, void *v)
880 {
881 struct resctrl_schema *s = of->kn->parent->priv;
882 struct rdt_resource *r = s->res;
883
884 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
885 return 0;
886 }
887
rdt_shareable_bits_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)888 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
889 struct seq_file *seq, void *v)
890 {
891 struct resctrl_schema *s = of->kn->parent->priv;
892 struct rdt_resource *r = s->res;
893
894 seq_printf(seq, "%x\n", r->cache.shareable_bits);
895 return 0;
896 }
897
898 /**
899 * rdt_bit_usage_show - Display current usage of resources
900 *
901 * A domain is a shared resource that can now be allocated differently. Here
902 * we display the current regions of the domain as an annotated bitmask.
903 * For each domain of this resource its allocation bitmask
904 * is annotated as below to indicate the current usage of the corresponding bit:
905 * 0 - currently unused
906 * X - currently available for sharing and used by software and hardware
907 * H - currently used by hardware only but available for software use
908 * S - currently used and shareable by software only
909 * E - currently used exclusively by one resource group
910 * P - currently pseudo-locked by one resource group
911 */
rdt_bit_usage_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)912 static int rdt_bit_usage_show(struct kernfs_open_file *of,
913 struct seq_file *seq, void *v)
914 {
915 struct resctrl_schema *s = of->kn->parent->priv;
916 /*
917 * Use unsigned long even though only 32 bits are used to ensure
918 * test_bit() is used safely.
919 */
920 unsigned long sw_shareable = 0, hw_shareable = 0;
921 unsigned long exclusive = 0, pseudo_locked = 0;
922 struct rdt_resource *r = s->res;
923 struct rdt_domain *dom;
924 int i, hwb, swb, excl, psl;
925 enum rdtgrp_mode mode;
926 bool sep = false;
927 u32 ctrl_val;
928
929 mutex_lock(&rdtgroup_mutex);
930 hw_shareable = r->cache.shareable_bits;
931 list_for_each_entry(dom, &r->domains, list) {
932 if (sep)
933 seq_putc(seq, ';');
934 sw_shareable = 0;
935 exclusive = 0;
936 seq_printf(seq, "%d=", dom->id);
937 for (i = 0; i < closids_supported(); i++) {
938 if (!closid_allocated(i))
939 continue;
940 ctrl_val = resctrl_arch_get_config(r, dom, i,
941 s->conf_type);
942 mode = rdtgroup_mode_by_closid(i);
943 switch (mode) {
944 case RDT_MODE_SHAREABLE:
945 sw_shareable |= ctrl_val;
946 break;
947 case RDT_MODE_EXCLUSIVE:
948 exclusive |= ctrl_val;
949 break;
950 case RDT_MODE_PSEUDO_LOCKSETUP:
951 /*
952 * RDT_MODE_PSEUDO_LOCKSETUP is possible
953 * here but not included since the CBM
954 * associated with this CLOSID in this mode
955 * is not initialized and no task or cpu can be
956 * assigned this CLOSID.
957 */
958 break;
959 case RDT_MODE_PSEUDO_LOCKED:
960 case RDT_NUM_MODES:
961 WARN(1,
962 "invalid mode for closid %d\n", i);
963 break;
964 }
965 }
966 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
967 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
968 hwb = test_bit(i, &hw_shareable);
969 swb = test_bit(i, &sw_shareable);
970 excl = test_bit(i, &exclusive);
971 psl = test_bit(i, &pseudo_locked);
972 if (hwb && swb)
973 seq_putc(seq, 'X');
974 else if (hwb && !swb)
975 seq_putc(seq, 'H');
976 else if (!hwb && swb)
977 seq_putc(seq, 'S');
978 else if (excl)
979 seq_putc(seq, 'E');
980 else if (psl)
981 seq_putc(seq, 'P');
982 else /* Unused bits remain */
983 seq_putc(seq, '0');
984 }
985 sep = true;
986 }
987 seq_putc(seq, '\n');
988 mutex_unlock(&rdtgroup_mutex);
989 return 0;
990 }
991
rdt_min_bw_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)992 static int rdt_min_bw_show(struct kernfs_open_file *of,
993 struct seq_file *seq, void *v)
994 {
995 struct resctrl_schema *s = of->kn->parent->priv;
996 struct rdt_resource *r = s->res;
997
998 seq_printf(seq, "%u\n", r->membw.min_bw);
999 return 0;
1000 }
1001
rdt_num_rmids_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1002 static int rdt_num_rmids_show(struct kernfs_open_file *of,
1003 struct seq_file *seq, void *v)
1004 {
1005 struct rdt_resource *r = of->kn->parent->priv;
1006
1007 seq_printf(seq, "%d\n", r->num_rmid);
1008
1009 return 0;
1010 }
1011
rdt_mon_features_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1012 static int rdt_mon_features_show(struct kernfs_open_file *of,
1013 struct seq_file *seq, void *v)
1014 {
1015 struct rdt_resource *r = of->kn->parent->priv;
1016 struct mon_evt *mevt;
1017
1018 list_for_each_entry(mevt, &r->evt_list, list) {
1019 seq_printf(seq, "%s\n", mevt->name);
1020 if (mevt->configurable)
1021 seq_printf(seq, "%s_config\n", mevt->name);
1022 }
1023
1024 return 0;
1025 }
1026
rdt_bw_gran_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1027 static int rdt_bw_gran_show(struct kernfs_open_file *of,
1028 struct seq_file *seq, void *v)
1029 {
1030 struct resctrl_schema *s = of->kn->parent->priv;
1031 struct rdt_resource *r = s->res;
1032
1033 seq_printf(seq, "%u\n", r->membw.bw_gran);
1034 return 0;
1035 }
1036
rdt_delay_linear_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1037 static int rdt_delay_linear_show(struct kernfs_open_file *of,
1038 struct seq_file *seq, void *v)
1039 {
1040 struct resctrl_schema *s = of->kn->parent->priv;
1041 struct rdt_resource *r = s->res;
1042
1043 seq_printf(seq, "%u\n", r->membw.delay_linear);
1044 return 0;
1045 }
1046
max_threshold_occ_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1047 static int max_threshold_occ_show(struct kernfs_open_file *of,
1048 struct seq_file *seq, void *v)
1049 {
1050 seq_printf(seq, "%u\n", resctrl_rmid_realloc_threshold);
1051
1052 return 0;
1053 }
1054
rdt_thread_throttle_mode_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1055 static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
1056 struct seq_file *seq, void *v)
1057 {
1058 struct resctrl_schema *s = of->kn->parent->priv;
1059 struct rdt_resource *r = s->res;
1060
1061 if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
1062 seq_puts(seq, "per-thread\n");
1063 else
1064 seq_puts(seq, "max\n");
1065
1066 return 0;
1067 }
1068
max_threshold_occ_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1069 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1070 char *buf, size_t nbytes, loff_t off)
1071 {
1072 unsigned int bytes;
1073 int ret;
1074
1075 ret = kstrtouint(buf, 0, &bytes);
1076 if (ret)
1077 return ret;
1078
1079 if (bytes > resctrl_rmid_realloc_limit)
1080 return -EINVAL;
1081
1082 resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(bytes);
1083
1084 return nbytes;
1085 }
1086
1087 /*
1088 * rdtgroup_mode_show - Display mode of this resource group
1089 */
rdtgroup_mode_show(struct kernfs_open_file * of,struct seq_file * s,void * v)1090 static int rdtgroup_mode_show(struct kernfs_open_file *of,
1091 struct seq_file *s, void *v)
1092 {
1093 struct rdtgroup *rdtgrp;
1094
1095 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1096 if (!rdtgrp) {
1097 rdtgroup_kn_unlock(of->kn);
1098 return -ENOENT;
1099 }
1100
1101 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1102
1103 rdtgroup_kn_unlock(of->kn);
1104 return 0;
1105 }
1106
resctrl_peer_type(enum resctrl_conf_type my_type)1107 static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
1108 {
1109 switch (my_type) {
1110 case CDP_CODE:
1111 return CDP_DATA;
1112 case CDP_DATA:
1113 return CDP_CODE;
1114 default:
1115 case CDP_NONE:
1116 return CDP_NONE;
1117 }
1118 }
1119
1120 /**
1121 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
1122 * @r: Resource to which domain instance @d belongs.
1123 * @d: The domain instance for which @closid is being tested.
1124 * @cbm: Capacity bitmask being tested.
1125 * @closid: Intended closid for @cbm.
1126 * @exclusive: Only check if overlaps with exclusive resource groups
1127 *
1128 * Checks if provided @cbm intended to be used for @closid on domain
1129 * @d overlaps with any other closids or other hardware usage associated
1130 * with this domain. If @exclusive is true then only overlaps with
1131 * resource groups in exclusive mode will be considered. If @exclusive
1132 * is false then overlaps with any resource group or hardware entities
1133 * will be considered.
1134 *
1135 * @cbm is unsigned long, even if only 32 bits are used, to make the
1136 * bitmap functions work correctly.
1137 *
1138 * Return: false if CBM does not overlap, true if it does.
1139 */
__rdtgroup_cbm_overlaps(struct rdt_resource * r,struct rdt_domain * d,unsigned long cbm,int closid,enum resctrl_conf_type type,bool exclusive)1140 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1141 unsigned long cbm, int closid,
1142 enum resctrl_conf_type type, bool exclusive)
1143 {
1144 enum rdtgrp_mode mode;
1145 unsigned long ctrl_b;
1146 int i;
1147
1148 /* Check for any overlap with regions used by hardware directly */
1149 if (!exclusive) {
1150 ctrl_b = r->cache.shareable_bits;
1151 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
1152 return true;
1153 }
1154
1155 /* Check for overlap with other resource groups */
1156 for (i = 0; i < closids_supported(); i++) {
1157 ctrl_b = resctrl_arch_get_config(r, d, i, type);
1158 mode = rdtgroup_mode_by_closid(i);
1159 if (closid_allocated(i) && i != closid &&
1160 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1161 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1162 if (exclusive) {
1163 if (mode == RDT_MODE_EXCLUSIVE)
1164 return true;
1165 continue;
1166 }
1167 return true;
1168 }
1169 }
1170 }
1171
1172 return false;
1173 }
1174
1175 /**
1176 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
1177 * @s: Schema for the resource to which domain instance @d belongs.
1178 * @d: The domain instance for which @closid is being tested.
1179 * @cbm: Capacity bitmask being tested.
1180 * @closid: Intended closid for @cbm.
1181 * @exclusive: Only check if overlaps with exclusive resource groups
1182 *
1183 * Resources that can be allocated using a CBM can use the CBM to control
1184 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1185 * for overlap. Overlap test is not limited to the specific resource for
1186 * which the CBM is intended though - when dealing with CDP resources that
1187 * share the underlying hardware the overlap check should be performed on
1188 * the CDP resource sharing the hardware also.
1189 *
1190 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1191 * overlap test.
1192 *
1193 * Return: true if CBM overlap detected, false if there is no overlap
1194 */
rdtgroup_cbm_overlaps(struct resctrl_schema * s,struct rdt_domain * d,unsigned long cbm,int closid,bool exclusive)1195 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
1196 unsigned long cbm, int closid, bool exclusive)
1197 {
1198 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
1199 struct rdt_resource *r = s->res;
1200
1201 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
1202 exclusive))
1203 return true;
1204
1205 if (!resctrl_arch_get_cdp_enabled(r->rid))
1206 return false;
1207 return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
1208 }
1209
1210 /**
1211 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1212 *
1213 * An exclusive resource group implies that there should be no sharing of
1214 * its allocated resources. At the time this group is considered to be
1215 * exclusive this test can determine if its current schemata supports this
1216 * setting by testing for overlap with all other resource groups.
1217 *
1218 * Return: true if resource group can be exclusive, false if there is overlap
1219 * with allocations of other resource groups and thus this resource group
1220 * cannot be exclusive.
1221 */
rdtgroup_mode_test_exclusive(struct rdtgroup * rdtgrp)1222 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1223 {
1224 int closid = rdtgrp->closid;
1225 struct resctrl_schema *s;
1226 struct rdt_resource *r;
1227 bool has_cache = false;
1228 struct rdt_domain *d;
1229 u32 ctrl;
1230
1231 list_for_each_entry(s, &resctrl_schema_all, list) {
1232 r = s->res;
1233 if (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)
1234 continue;
1235 has_cache = true;
1236 list_for_each_entry(d, &r->domains, list) {
1237 ctrl = resctrl_arch_get_config(r, d, closid,
1238 s->conf_type);
1239 if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
1240 rdt_last_cmd_puts("Schemata overlaps\n");
1241 return false;
1242 }
1243 }
1244 }
1245
1246 if (!has_cache) {
1247 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
1248 return false;
1249 }
1250
1251 return true;
1252 }
1253
1254 /**
1255 * rdtgroup_mode_write - Modify the resource group's mode
1256 *
1257 */
rdtgroup_mode_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1258 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1259 char *buf, size_t nbytes, loff_t off)
1260 {
1261 struct rdtgroup *rdtgrp;
1262 enum rdtgrp_mode mode;
1263 int ret = 0;
1264
1265 /* Valid input requires a trailing newline */
1266 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1267 return -EINVAL;
1268 buf[nbytes - 1] = '\0';
1269
1270 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1271 if (!rdtgrp) {
1272 rdtgroup_kn_unlock(of->kn);
1273 return -ENOENT;
1274 }
1275
1276 rdt_last_cmd_clear();
1277
1278 mode = rdtgrp->mode;
1279
1280 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1281 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1282 (!strcmp(buf, "pseudo-locksetup") &&
1283 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1284 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1285 goto out;
1286
1287 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1288 rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
1289 ret = -EINVAL;
1290 goto out;
1291 }
1292
1293 if (!strcmp(buf, "shareable")) {
1294 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1295 ret = rdtgroup_locksetup_exit(rdtgrp);
1296 if (ret)
1297 goto out;
1298 }
1299 rdtgrp->mode = RDT_MODE_SHAREABLE;
1300 } else if (!strcmp(buf, "exclusive")) {
1301 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1302 ret = -EINVAL;
1303 goto out;
1304 }
1305 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1306 ret = rdtgroup_locksetup_exit(rdtgrp);
1307 if (ret)
1308 goto out;
1309 }
1310 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1311 } else if (!strcmp(buf, "pseudo-locksetup")) {
1312 ret = rdtgroup_locksetup_enter(rdtgrp);
1313 if (ret)
1314 goto out;
1315 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1316 } else {
1317 rdt_last_cmd_puts("Unknown or unsupported mode\n");
1318 ret = -EINVAL;
1319 }
1320
1321 out:
1322 rdtgroup_kn_unlock(of->kn);
1323 return ret ?: nbytes;
1324 }
1325
1326 /**
1327 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1328 * @r: RDT resource to which @d belongs.
1329 * @d: RDT domain instance.
1330 * @cbm: bitmask for which the size should be computed.
1331 *
1332 * The bitmask provided associated with the RDT domain instance @d will be
1333 * translated into how many bytes it represents. The size in bytes is
1334 * computed by first dividing the total cache size by the CBM length to
1335 * determine how many bytes each bit in the bitmask represents. The result
1336 * is multiplied with the number of bits set in the bitmask.
1337 *
1338 * @cbm is unsigned long, even if only 32 bits are used to make the
1339 * bitmap functions work correctly.
1340 */
rdtgroup_cbm_to_size(struct rdt_resource * r,struct rdt_domain * d,unsigned long cbm)1341 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1342 struct rdt_domain *d, unsigned long cbm)
1343 {
1344 struct cpu_cacheinfo *ci;
1345 unsigned int size = 0;
1346 int num_b, i;
1347
1348 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1349 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1350 for (i = 0; i < ci->num_leaves; i++) {
1351 if (ci->info_list[i].level == r->cache_level) {
1352 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1353 break;
1354 }
1355 }
1356
1357 return size;
1358 }
1359
1360 /**
1361 * rdtgroup_size_show - Display size in bytes of allocated regions
1362 *
1363 * The "size" file mirrors the layout of the "schemata" file, printing the
1364 * size in bytes of each region instead of the capacity bitmask.
1365 *
1366 */
rdtgroup_size_show(struct kernfs_open_file * of,struct seq_file * s,void * v)1367 static int rdtgroup_size_show(struct kernfs_open_file *of,
1368 struct seq_file *s, void *v)
1369 {
1370 struct resctrl_schema *schema;
1371 enum resctrl_conf_type type;
1372 struct rdtgroup *rdtgrp;
1373 struct rdt_resource *r;
1374 struct rdt_domain *d;
1375 unsigned int size;
1376 int ret = 0;
1377 u32 closid;
1378 bool sep;
1379 u32 ctrl;
1380
1381 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1382 if (!rdtgrp) {
1383 rdtgroup_kn_unlock(of->kn);
1384 return -ENOENT;
1385 }
1386
1387 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1388 if (!rdtgrp->plr->d) {
1389 rdt_last_cmd_clear();
1390 rdt_last_cmd_puts("Cache domain offline\n");
1391 ret = -ENODEV;
1392 } else {
1393 seq_printf(s, "%*s:", max_name_width,
1394 rdtgrp->plr->s->name);
1395 size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
1396 rdtgrp->plr->d,
1397 rdtgrp->plr->cbm);
1398 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1399 }
1400 goto out;
1401 }
1402
1403 closid = rdtgrp->closid;
1404
1405 list_for_each_entry(schema, &resctrl_schema_all, list) {
1406 r = schema->res;
1407 type = schema->conf_type;
1408 sep = false;
1409 seq_printf(s, "%*s:", max_name_width, schema->name);
1410 list_for_each_entry(d, &r->domains, list) {
1411 if (sep)
1412 seq_putc(s, ';');
1413 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1414 size = 0;
1415 } else {
1416 if (is_mba_sc(r))
1417 ctrl = d->mbps_val[closid];
1418 else
1419 ctrl = resctrl_arch_get_config(r, d,
1420 closid,
1421 type);
1422 if (r->rid == RDT_RESOURCE_MBA ||
1423 r->rid == RDT_RESOURCE_SMBA)
1424 size = ctrl;
1425 else
1426 size = rdtgroup_cbm_to_size(r, d, ctrl);
1427 }
1428 seq_printf(s, "%d=%u", d->id, size);
1429 sep = true;
1430 }
1431 seq_putc(s, '\n');
1432 }
1433
1434 out:
1435 rdtgroup_kn_unlock(of->kn);
1436
1437 return ret;
1438 }
1439
1440 struct mon_config_info {
1441 u32 evtid;
1442 u32 mon_config;
1443 };
1444
1445 #define INVALID_CONFIG_INDEX UINT_MAX
1446
1447 /**
1448 * mon_event_config_index_get - get the hardware index for the
1449 * configurable event
1450 * @evtid: event id.
1451 *
1452 * Return: 0 for evtid == QOS_L3_MBM_TOTAL_EVENT_ID
1453 * 1 for evtid == QOS_L3_MBM_LOCAL_EVENT_ID
1454 * INVALID_CONFIG_INDEX for invalid evtid
1455 */
mon_event_config_index_get(u32 evtid)1456 static inline unsigned int mon_event_config_index_get(u32 evtid)
1457 {
1458 switch (evtid) {
1459 case QOS_L3_MBM_TOTAL_EVENT_ID:
1460 return 0;
1461 case QOS_L3_MBM_LOCAL_EVENT_ID:
1462 return 1;
1463 default:
1464 /* Should never reach here */
1465 return INVALID_CONFIG_INDEX;
1466 }
1467 }
1468
mon_event_config_read(void * info)1469 static void mon_event_config_read(void *info)
1470 {
1471 struct mon_config_info *mon_info = info;
1472 unsigned int index;
1473 u64 msrval;
1474
1475 index = mon_event_config_index_get(mon_info->evtid);
1476 if (index == INVALID_CONFIG_INDEX) {
1477 pr_warn_once("Invalid event id %d\n", mon_info->evtid);
1478 return;
1479 }
1480 rdmsrl(MSR_IA32_EVT_CFG_BASE + index, msrval);
1481
1482 /* Report only the valid event configuration bits */
1483 mon_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
1484 }
1485
mondata_config_read(struct rdt_domain * d,struct mon_config_info * mon_info)1486 static void mondata_config_read(struct rdt_domain *d, struct mon_config_info *mon_info)
1487 {
1488 smp_call_function_any(&d->cpu_mask, mon_event_config_read, mon_info, 1);
1489 }
1490
mbm_config_show(struct seq_file * s,struct rdt_resource * r,u32 evtid)1491 static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid)
1492 {
1493 struct mon_config_info mon_info = {0};
1494 struct rdt_domain *dom;
1495 bool sep = false;
1496
1497 mutex_lock(&rdtgroup_mutex);
1498
1499 list_for_each_entry(dom, &r->domains, list) {
1500 if (sep)
1501 seq_puts(s, ";");
1502
1503 memset(&mon_info, 0, sizeof(struct mon_config_info));
1504 mon_info.evtid = evtid;
1505 mondata_config_read(dom, &mon_info);
1506
1507 seq_printf(s, "%d=0x%02x", dom->id, mon_info.mon_config);
1508 sep = true;
1509 }
1510 seq_puts(s, "\n");
1511
1512 mutex_unlock(&rdtgroup_mutex);
1513
1514 return 0;
1515 }
1516
mbm_total_bytes_config_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1517 static int mbm_total_bytes_config_show(struct kernfs_open_file *of,
1518 struct seq_file *seq, void *v)
1519 {
1520 struct rdt_resource *r = of->kn->parent->priv;
1521
1522 mbm_config_show(seq, r, QOS_L3_MBM_TOTAL_EVENT_ID);
1523
1524 return 0;
1525 }
1526
mbm_local_bytes_config_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1527 static int mbm_local_bytes_config_show(struct kernfs_open_file *of,
1528 struct seq_file *seq, void *v)
1529 {
1530 struct rdt_resource *r = of->kn->parent->priv;
1531
1532 mbm_config_show(seq, r, QOS_L3_MBM_LOCAL_EVENT_ID);
1533
1534 return 0;
1535 }
1536
mon_event_config_write(void * info)1537 static void mon_event_config_write(void *info)
1538 {
1539 struct mon_config_info *mon_info = info;
1540 unsigned int index;
1541
1542 index = mon_event_config_index_get(mon_info->evtid);
1543 if (index == INVALID_CONFIG_INDEX) {
1544 pr_warn_once("Invalid event id %d\n", mon_info->evtid);
1545 return;
1546 }
1547 wrmsr(MSR_IA32_EVT_CFG_BASE + index, mon_info->mon_config, 0);
1548 }
1549
mbm_config_write_domain(struct rdt_resource * r,struct rdt_domain * d,u32 evtid,u32 val)1550 static int mbm_config_write_domain(struct rdt_resource *r,
1551 struct rdt_domain *d, u32 evtid, u32 val)
1552 {
1553 struct mon_config_info mon_info = {0};
1554 int ret = 0;
1555
1556 /* mon_config cannot be more than the supported set of events */
1557 if (val > MAX_EVT_CONFIG_BITS) {
1558 rdt_last_cmd_puts("Invalid event configuration\n");
1559 return -EINVAL;
1560 }
1561
1562 /*
1563 * Read the current config value first. If both are the same then
1564 * no need to write it again.
1565 */
1566 mon_info.evtid = evtid;
1567 mondata_config_read(d, &mon_info);
1568 if (mon_info.mon_config == val)
1569 goto out;
1570
1571 mon_info.mon_config = val;
1572
1573 /*
1574 * Update MSR_IA32_EVT_CFG_BASE MSR on one of the CPUs in the
1575 * domain. The MSRs offset from MSR MSR_IA32_EVT_CFG_BASE
1576 * are scoped at the domain level. Writing any of these MSRs
1577 * on one CPU is observed by all the CPUs in the domain.
1578 */
1579 smp_call_function_any(&d->cpu_mask, mon_event_config_write,
1580 &mon_info, 1);
1581
1582 /*
1583 * When an Event Configuration is changed, the bandwidth counters
1584 * for all RMIDs and Events will be cleared by the hardware. The
1585 * hardware also sets MSR_IA32_QM_CTR.Unavailable (bit 62) for
1586 * every RMID on the next read to any event for every RMID.
1587 * Subsequent reads will have MSR_IA32_QM_CTR.Unavailable (bit 62)
1588 * cleared while it is tracked by the hardware. Clear the
1589 * mbm_local and mbm_total counts for all the RMIDs.
1590 */
1591 resctrl_arch_reset_rmid_all(r, d);
1592
1593 out:
1594 return ret;
1595 }
1596
mon_config_write(struct rdt_resource * r,char * tok,u32 evtid)1597 static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)
1598 {
1599 char *dom_str = NULL, *id_str;
1600 unsigned long dom_id, val;
1601 struct rdt_domain *d;
1602 int ret = 0;
1603
1604 next:
1605 if (!tok || tok[0] == '\0')
1606 return 0;
1607
1608 /* Start processing the strings for each domain */
1609 dom_str = strim(strsep(&tok, ";"));
1610 id_str = strsep(&dom_str, "=");
1611
1612 if (!id_str || kstrtoul(id_str, 10, &dom_id)) {
1613 rdt_last_cmd_puts("Missing '=' or non-numeric domain id\n");
1614 return -EINVAL;
1615 }
1616
1617 if (!dom_str || kstrtoul(dom_str, 16, &val)) {
1618 rdt_last_cmd_puts("Non-numeric event configuration value\n");
1619 return -EINVAL;
1620 }
1621
1622 list_for_each_entry(d, &r->domains, list) {
1623 if (d->id == dom_id) {
1624 ret = mbm_config_write_domain(r, d, evtid, val);
1625 if (ret)
1626 return -EINVAL;
1627 goto next;
1628 }
1629 }
1630
1631 return -EINVAL;
1632 }
1633
mbm_total_bytes_config_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1634 static ssize_t mbm_total_bytes_config_write(struct kernfs_open_file *of,
1635 char *buf, size_t nbytes,
1636 loff_t off)
1637 {
1638 struct rdt_resource *r = of->kn->parent->priv;
1639 int ret;
1640
1641 /* Valid input requires a trailing newline */
1642 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1643 return -EINVAL;
1644
1645 mutex_lock(&rdtgroup_mutex);
1646
1647 rdt_last_cmd_clear();
1648
1649 buf[nbytes - 1] = '\0';
1650
1651 ret = mon_config_write(r, buf, QOS_L3_MBM_TOTAL_EVENT_ID);
1652
1653 mutex_unlock(&rdtgroup_mutex);
1654
1655 return ret ?: nbytes;
1656 }
1657
mbm_local_bytes_config_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1658 static ssize_t mbm_local_bytes_config_write(struct kernfs_open_file *of,
1659 char *buf, size_t nbytes,
1660 loff_t off)
1661 {
1662 struct rdt_resource *r = of->kn->parent->priv;
1663 int ret;
1664
1665 /* Valid input requires a trailing newline */
1666 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1667 return -EINVAL;
1668
1669 mutex_lock(&rdtgroup_mutex);
1670
1671 rdt_last_cmd_clear();
1672
1673 buf[nbytes - 1] = '\0';
1674
1675 ret = mon_config_write(r, buf, QOS_L3_MBM_LOCAL_EVENT_ID);
1676
1677 mutex_unlock(&rdtgroup_mutex);
1678
1679 return ret ?: nbytes;
1680 }
1681
1682 /* rdtgroup information files for one cache resource. */
1683 static struct rftype res_common_files[] = {
1684 {
1685 .name = "last_cmd_status",
1686 .mode = 0444,
1687 .kf_ops = &rdtgroup_kf_single_ops,
1688 .seq_show = rdt_last_cmd_status_show,
1689 .fflags = RF_TOP_INFO,
1690 },
1691 {
1692 .name = "num_closids",
1693 .mode = 0444,
1694 .kf_ops = &rdtgroup_kf_single_ops,
1695 .seq_show = rdt_num_closids_show,
1696 .fflags = RF_CTRL_INFO,
1697 },
1698 {
1699 .name = "mon_features",
1700 .mode = 0444,
1701 .kf_ops = &rdtgroup_kf_single_ops,
1702 .seq_show = rdt_mon_features_show,
1703 .fflags = RF_MON_INFO,
1704 },
1705 {
1706 .name = "num_rmids",
1707 .mode = 0444,
1708 .kf_ops = &rdtgroup_kf_single_ops,
1709 .seq_show = rdt_num_rmids_show,
1710 .fflags = RF_MON_INFO,
1711 },
1712 {
1713 .name = "cbm_mask",
1714 .mode = 0444,
1715 .kf_ops = &rdtgroup_kf_single_ops,
1716 .seq_show = rdt_default_ctrl_show,
1717 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1718 },
1719 {
1720 .name = "min_cbm_bits",
1721 .mode = 0444,
1722 .kf_ops = &rdtgroup_kf_single_ops,
1723 .seq_show = rdt_min_cbm_bits_show,
1724 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1725 },
1726 {
1727 .name = "shareable_bits",
1728 .mode = 0444,
1729 .kf_ops = &rdtgroup_kf_single_ops,
1730 .seq_show = rdt_shareable_bits_show,
1731 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1732 },
1733 {
1734 .name = "bit_usage",
1735 .mode = 0444,
1736 .kf_ops = &rdtgroup_kf_single_ops,
1737 .seq_show = rdt_bit_usage_show,
1738 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1739 },
1740 {
1741 .name = "min_bandwidth",
1742 .mode = 0444,
1743 .kf_ops = &rdtgroup_kf_single_ops,
1744 .seq_show = rdt_min_bw_show,
1745 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1746 },
1747 {
1748 .name = "bandwidth_gran",
1749 .mode = 0444,
1750 .kf_ops = &rdtgroup_kf_single_ops,
1751 .seq_show = rdt_bw_gran_show,
1752 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1753 },
1754 {
1755 .name = "delay_linear",
1756 .mode = 0444,
1757 .kf_ops = &rdtgroup_kf_single_ops,
1758 .seq_show = rdt_delay_linear_show,
1759 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1760 },
1761 /*
1762 * Platform specific which (if any) capabilities are provided by
1763 * thread_throttle_mode. Defer "fflags" initialization to platform
1764 * discovery.
1765 */
1766 {
1767 .name = "thread_throttle_mode",
1768 .mode = 0444,
1769 .kf_ops = &rdtgroup_kf_single_ops,
1770 .seq_show = rdt_thread_throttle_mode_show,
1771 },
1772 {
1773 .name = "max_threshold_occupancy",
1774 .mode = 0644,
1775 .kf_ops = &rdtgroup_kf_single_ops,
1776 .write = max_threshold_occ_write,
1777 .seq_show = max_threshold_occ_show,
1778 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
1779 },
1780 {
1781 .name = "mbm_total_bytes_config",
1782 .mode = 0644,
1783 .kf_ops = &rdtgroup_kf_single_ops,
1784 .seq_show = mbm_total_bytes_config_show,
1785 .write = mbm_total_bytes_config_write,
1786 },
1787 {
1788 .name = "mbm_local_bytes_config",
1789 .mode = 0644,
1790 .kf_ops = &rdtgroup_kf_single_ops,
1791 .seq_show = mbm_local_bytes_config_show,
1792 .write = mbm_local_bytes_config_write,
1793 },
1794 {
1795 .name = "cpus",
1796 .mode = 0644,
1797 .kf_ops = &rdtgroup_kf_single_ops,
1798 .write = rdtgroup_cpus_write,
1799 .seq_show = rdtgroup_cpus_show,
1800 .fflags = RFTYPE_BASE,
1801 },
1802 {
1803 .name = "cpus_list",
1804 .mode = 0644,
1805 .kf_ops = &rdtgroup_kf_single_ops,
1806 .write = rdtgroup_cpus_write,
1807 .seq_show = rdtgroup_cpus_show,
1808 .flags = RFTYPE_FLAGS_CPUS_LIST,
1809 .fflags = RFTYPE_BASE,
1810 },
1811 {
1812 .name = "tasks",
1813 .mode = 0644,
1814 .kf_ops = &rdtgroup_kf_single_ops,
1815 .write = rdtgroup_tasks_write,
1816 .seq_show = rdtgroup_tasks_show,
1817 .fflags = RFTYPE_BASE,
1818 },
1819 {
1820 .name = "schemata",
1821 .mode = 0644,
1822 .kf_ops = &rdtgroup_kf_single_ops,
1823 .write = rdtgroup_schemata_write,
1824 .seq_show = rdtgroup_schemata_show,
1825 .fflags = RF_CTRL_BASE,
1826 },
1827 {
1828 .name = "mode",
1829 .mode = 0644,
1830 .kf_ops = &rdtgroup_kf_single_ops,
1831 .write = rdtgroup_mode_write,
1832 .seq_show = rdtgroup_mode_show,
1833 .fflags = RF_CTRL_BASE,
1834 },
1835 {
1836 .name = "size",
1837 .mode = 0444,
1838 .kf_ops = &rdtgroup_kf_single_ops,
1839 .seq_show = rdtgroup_size_show,
1840 .fflags = RF_CTRL_BASE,
1841 },
1842
1843 };
1844
rdtgroup_add_files(struct kernfs_node * kn,unsigned long fflags)1845 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1846 {
1847 struct rftype *rfts, *rft;
1848 int ret, len;
1849
1850 rfts = res_common_files;
1851 len = ARRAY_SIZE(res_common_files);
1852
1853 lockdep_assert_held(&rdtgroup_mutex);
1854
1855 for (rft = rfts; rft < rfts + len; rft++) {
1856 if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
1857 ret = rdtgroup_add_file(kn, rft);
1858 if (ret)
1859 goto error;
1860 }
1861 }
1862
1863 return 0;
1864 error:
1865 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1866 while (--rft >= rfts) {
1867 if ((fflags & rft->fflags) == rft->fflags)
1868 kernfs_remove_by_name(kn, rft->name);
1869 }
1870 return ret;
1871 }
1872
rdtgroup_get_rftype_by_name(const char * name)1873 static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
1874 {
1875 struct rftype *rfts, *rft;
1876 int len;
1877
1878 rfts = res_common_files;
1879 len = ARRAY_SIZE(res_common_files);
1880
1881 for (rft = rfts; rft < rfts + len; rft++) {
1882 if (!strcmp(rft->name, name))
1883 return rft;
1884 }
1885
1886 return NULL;
1887 }
1888
thread_throttle_mode_init(void)1889 void __init thread_throttle_mode_init(void)
1890 {
1891 struct rftype *rft;
1892
1893 rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
1894 if (!rft)
1895 return;
1896
1897 rft->fflags = RF_CTRL_INFO | RFTYPE_RES_MB;
1898 }
1899
mbm_config_rftype_init(const char * config)1900 void __init mbm_config_rftype_init(const char *config)
1901 {
1902 struct rftype *rft;
1903
1904 rft = rdtgroup_get_rftype_by_name(config);
1905 if (rft)
1906 rft->fflags = RF_MON_INFO | RFTYPE_RES_CACHE;
1907 }
1908
1909 /**
1910 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1911 * @r: The resource group with which the file is associated.
1912 * @name: Name of the file
1913 *
1914 * The permissions of named resctrl file, directory, or link are modified
1915 * to not allow read, write, or execute by any user.
1916 *
1917 * WARNING: This function is intended to communicate to the user that the
1918 * resctrl file has been locked down - that it is not relevant to the
1919 * particular state the system finds itself in. It should not be relied
1920 * on to protect from user access because after the file's permissions
1921 * are restricted the user can still change the permissions using chmod
1922 * from the command line.
1923 *
1924 * Return: 0 on success, <0 on failure.
1925 */
rdtgroup_kn_mode_restrict(struct rdtgroup * r,const char * name)1926 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1927 {
1928 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1929 struct kernfs_node *kn;
1930 int ret = 0;
1931
1932 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1933 if (!kn)
1934 return -ENOENT;
1935
1936 switch (kernfs_type(kn)) {
1937 case KERNFS_DIR:
1938 iattr.ia_mode = S_IFDIR;
1939 break;
1940 case KERNFS_FILE:
1941 iattr.ia_mode = S_IFREG;
1942 break;
1943 case KERNFS_LINK:
1944 iattr.ia_mode = S_IFLNK;
1945 break;
1946 }
1947
1948 ret = kernfs_setattr(kn, &iattr);
1949 kernfs_put(kn);
1950 return ret;
1951 }
1952
1953 /**
1954 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1955 * @r: The resource group with which the file is associated.
1956 * @name: Name of the file
1957 * @mask: Mask of permissions that should be restored
1958 *
1959 * Restore the permissions of the named file. If @name is a directory the
1960 * permissions of its parent will be used.
1961 *
1962 * Return: 0 on success, <0 on failure.
1963 */
rdtgroup_kn_mode_restore(struct rdtgroup * r,const char * name,umode_t mask)1964 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1965 umode_t mask)
1966 {
1967 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1968 struct kernfs_node *kn, *parent;
1969 struct rftype *rfts, *rft;
1970 int ret, len;
1971
1972 rfts = res_common_files;
1973 len = ARRAY_SIZE(res_common_files);
1974
1975 for (rft = rfts; rft < rfts + len; rft++) {
1976 if (!strcmp(rft->name, name))
1977 iattr.ia_mode = rft->mode & mask;
1978 }
1979
1980 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1981 if (!kn)
1982 return -ENOENT;
1983
1984 switch (kernfs_type(kn)) {
1985 case KERNFS_DIR:
1986 parent = kernfs_get_parent(kn);
1987 if (parent) {
1988 iattr.ia_mode |= parent->mode;
1989 kernfs_put(parent);
1990 }
1991 iattr.ia_mode |= S_IFDIR;
1992 break;
1993 case KERNFS_FILE:
1994 iattr.ia_mode |= S_IFREG;
1995 break;
1996 case KERNFS_LINK:
1997 iattr.ia_mode |= S_IFLNK;
1998 break;
1999 }
2000
2001 ret = kernfs_setattr(kn, &iattr);
2002 kernfs_put(kn);
2003 return ret;
2004 }
2005
rdtgroup_mkdir_info_resdir(void * priv,char * name,unsigned long fflags)2006 static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
2007 unsigned long fflags)
2008 {
2009 struct kernfs_node *kn_subdir;
2010 int ret;
2011
2012 kn_subdir = kernfs_create_dir(kn_info, name,
2013 kn_info->mode, priv);
2014 if (IS_ERR(kn_subdir))
2015 return PTR_ERR(kn_subdir);
2016
2017 ret = rdtgroup_kn_set_ugid(kn_subdir);
2018 if (ret)
2019 return ret;
2020
2021 ret = rdtgroup_add_files(kn_subdir, fflags);
2022 if (!ret)
2023 kernfs_activate(kn_subdir);
2024
2025 return ret;
2026 }
2027
rdtgroup_create_info_dir(struct kernfs_node * parent_kn)2028 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
2029 {
2030 struct resctrl_schema *s;
2031 struct rdt_resource *r;
2032 unsigned long fflags;
2033 char name[32];
2034 int ret;
2035
2036 /* create the directory */
2037 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
2038 if (IS_ERR(kn_info))
2039 return PTR_ERR(kn_info);
2040
2041 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
2042 if (ret)
2043 goto out_destroy;
2044
2045 /* loop over enabled controls, these are all alloc_capable */
2046 list_for_each_entry(s, &resctrl_schema_all, list) {
2047 r = s->res;
2048 fflags = r->fflags | RF_CTRL_INFO;
2049 ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
2050 if (ret)
2051 goto out_destroy;
2052 }
2053
2054 for_each_mon_capable_rdt_resource(r) {
2055 fflags = r->fflags | RF_MON_INFO;
2056 sprintf(name, "%s_MON", r->name);
2057 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
2058 if (ret)
2059 goto out_destroy;
2060 }
2061
2062 ret = rdtgroup_kn_set_ugid(kn_info);
2063 if (ret)
2064 goto out_destroy;
2065
2066 kernfs_activate(kn_info);
2067
2068 return 0;
2069
2070 out_destroy:
2071 kernfs_remove(kn_info);
2072 return ret;
2073 }
2074
2075 static int
mongroup_create_dir(struct kernfs_node * parent_kn,struct rdtgroup * prgrp,char * name,struct kernfs_node ** dest_kn)2076 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
2077 char *name, struct kernfs_node **dest_kn)
2078 {
2079 struct kernfs_node *kn;
2080 int ret;
2081
2082 /* create the directory */
2083 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2084 if (IS_ERR(kn))
2085 return PTR_ERR(kn);
2086
2087 if (dest_kn)
2088 *dest_kn = kn;
2089
2090 ret = rdtgroup_kn_set_ugid(kn);
2091 if (ret)
2092 goto out_destroy;
2093
2094 kernfs_activate(kn);
2095
2096 return 0;
2097
2098 out_destroy:
2099 kernfs_remove(kn);
2100 return ret;
2101 }
2102
l3_qos_cfg_update(void * arg)2103 static void l3_qos_cfg_update(void *arg)
2104 {
2105 bool *enable = arg;
2106
2107 wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
2108 }
2109
l2_qos_cfg_update(void * arg)2110 static void l2_qos_cfg_update(void *arg)
2111 {
2112 bool *enable = arg;
2113
2114 wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
2115 }
2116
is_mba_linear(void)2117 static inline bool is_mba_linear(void)
2118 {
2119 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
2120 }
2121
set_cache_qos_cfg(int level,bool enable)2122 static int set_cache_qos_cfg(int level, bool enable)
2123 {
2124 void (*update)(void *arg);
2125 struct rdt_resource *r_l;
2126 cpumask_var_t cpu_mask;
2127 struct rdt_domain *d;
2128 int cpu;
2129
2130 if (level == RDT_RESOURCE_L3)
2131 update = l3_qos_cfg_update;
2132 else if (level == RDT_RESOURCE_L2)
2133 update = l2_qos_cfg_update;
2134 else
2135 return -EINVAL;
2136
2137 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2138 return -ENOMEM;
2139
2140 r_l = &rdt_resources_all[level].r_resctrl;
2141 list_for_each_entry(d, &r_l->domains, list) {
2142 if (r_l->cache.arch_has_per_cpu_cfg)
2143 /* Pick all the CPUs in the domain instance */
2144 for_each_cpu(cpu, &d->cpu_mask)
2145 cpumask_set_cpu(cpu, cpu_mask);
2146 else
2147 /* Pick one CPU from each domain instance to update MSR */
2148 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2149 }
2150
2151 /* Update QOS_CFG MSR on all the CPUs in cpu_mask */
2152 on_each_cpu_mask(cpu_mask, update, &enable, 1);
2153
2154 free_cpumask_var(cpu_mask);
2155
2156 return 0;
2157 }
2158
2159 /* Restore the qos cfg state when a domain comes online */
rdt_domain_reconfigure_cdp(struct rdt_resource * r)2160 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
2161 {
2162 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2163
2164 if (!r->cdp_capable)
2165 return;
2166
2167 if (r->rid == RDT_RESOURCE_L2)
2168 l2_qos_cfg_update(&hw_res->cdp_enabled);
2169
2170 if (r->rid == RDT_RESOURCE_L3)
2171 l3_qos_cfg_update(&hw_res->cdp_enabled);
2172 }
2173
mba_sc_domain_allocate(struct rdt_resource * r,struct rdt_domain * d)2174 static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_domain *d)
2175 {
2176 u32 num_closid = resctrl_arch_get_num_closid(r);
2177 int cpu = cpumask_any(&d->cpu_mask);
2178 int i;
2179
2180 d->mbps_val = kcalloc_node(num_closid, sizeof(*d->mbps_val),
2181 GFP_KERNEL, cpu_to_node(cpu));
2182 if (!d->mbps_val)
2183 return -ENOMEM;
2184
2185 for (i = 0; i < num_closid; i++)
2186 d->mbps_val[i] = MBA_MAX_MBPS;
2187
2188 return 0;
2189 }
2190
mba_sc_domain_destroy(struct rdt_resource * r,struct rdt_domain * d)2191 static void mba_sc_domain_destroy(struct rdt_resource *r,
2192 struct rdt_domain *d)
2193 {
2194 kfree(d->mbps_val);
2195 d->mbps_val = NULL;
2196 }
2197
2198 /*
2199 * MBA software controller is supported only if
2200 * MBM is supported and MBA is in linear scale.
2201 */
supports_mba_mbps(void)2202 static bool supports_mba_mbps(void)
2203 {
2204 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
2205
2206 return (is_mbm_local_enabled() &&
2207 r->alloc_capable && is_mba_linear());
2208 }
2209
2210 /*
2211 * Enable or disable the MBA software controller
2212 * which helps user specify bandwidth in MBps.
2213 */
set_mba_sc(bool mba_sc)2214 static int set_mba_sc(bool mba_sc)
2215 {
2216 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
2217 u32 num_closid = resctrl_arch_get_num_closid(r);
2218 struct rdt_domain *d;
2219 int i;
2220
2221 if (!supports_mba_mbps() || mba_sc == is_mba_sc(r))
2222 return -EINVAL;
2223
2224 r->membw.mba_sc = mba_sc;
2225
2226 list_for_each_entry(d, &r->domains, list) {
2227 for (i = 0; i < num_closid; i++)
2228 d->mbps_val[i] = MBA_MAX_MBPS;
2229 }
2230
2231 return 0;
2232 }
2233
cdp_enable(int level)2234 static int cdp_enable(int level)
2235 {
2236 struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
2237 int ret;
2238
2239 if (!r_l->alloc_capable)
2240 return -EINVAL;
2241
2242 ret = set_cache_qos_cfg(level, true);
2243 if (!ret)
2244 rdt_resources_all[level].cdp_enabled = true;
2245
2246 return ret;
2247 }
2248
cdp_disable(int level)2249 static void cdp_disable(int level)
2250 {
2251 struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
2252
2253 if (r_hw->cdp_enabled) {
2254 set_cache_qos_cfg(level, false);
2255 r_hw->cdp_enabled = false;
2256 }
2257 }
2258
resctrl_arch_set_cdp_enabled(enum resctrl_res_level l,bool enable)2259 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
2260 {
2261 struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
2262
2263 if (!hw_res->r_resctrl.cdp_capable)
2264 return -EINVAL;
2265
2266 if (enable)
2267 return cdp_enable(l);
2268
2269 cdp_disable(l);
2270
2271 return 0;
2272 }
2273
cdp_disable_all(void)2274 static void cdp_disable_all(void)
2275 {
2276 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
2277 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
2278 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
2279 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
2280 }
2281
2282 /*
2283 * We don't allow rdtgroup directories to be created anywhere
2284 * except the root directory. Thus when looking for the rdtgroup
2285 * structure for a kernfs node we are either looking at a directory,
2286 * in which case the rdtgroup structure is pointed at by the "priv"
2287 * field, otherwise we have a file, and need only look to the parent
2288 * to find the rdtgroup.
2289 */
kernfs_to_rdtgroup(struct kernfs_node * kn)2290 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
2291 {
2292 if (kernfs_type(kn) == KERNFS_DIR) {
2293 /*
2294 * All the resource directories use "kn->priv"
2295 * to point to the "struct rdtgroup" for the
2296 * resource. "info" and its subdirectories don't
2297 * have rdtgroup structures, so return NULL here.
2298 */
2299 if (kn == kn_info || kn->parent == kn_info)
2300 return NULL;
2301 else
2302 return kn->priv;
2303 } else {
2304 return kn->parent->priv;
2305 }
2306 }
2307
rdtgroup_kn_get(struct rdtgroup * rdtgrp,struct kernfs_node * kn)2308 static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
2309 {
2310 atomic_inc(&rdtgrp->waitcount);
2311 kernfs_break_active_protection(kn);
2312 }
2313
rdtgroup_kn_put(struct rdtgroup * rdtgrp,struct kernfs_node * kn)2314 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
2315 {
2316 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2317 (rdtgrp->flags & RDT_DELETED)) {
2318 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2319 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2320 rdtgroup_pseudo_lock_remove(rdtgrp);
2321 kernfs_unbreak_active_protection(kn);
2322 rdtgroup_remove(rdtgrp);
2323 } else {
2324 kernfs_unbreak_active_protection(kn);
2325 }
2326 }
2327
rdtgroup_kn_lock_live(struct kernfs_node * kn)2328 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
2329 {
2330 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2331
2332 if (!rdtgrp)
2333 return NULL;
2334
2335 rdtgroup_kn_get(rdtgrp, kn);
2336
2337 mutex_lock(&rdtgroup_mutex);
2338
2339 /* Was this group deleted while we waited? */
2340 if (rdtgrp->flags & RDT_DELETED)
2341 return NULL;
2342
2343 return rdtgrp;
2344 }
2345
rdtgroup_kn_unlock(struct kernfs_node * kn)2346 void rdtgroup_kn_unlock(struct kernfs_node *kn)
2347 {
2348 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2349
2350 if (!rdtgrp)
2351 return;
2352
2353 mutex_unlock(&rdtgroup_mutex);
2354 rdtgroup_kn_put(rdtgrp, kn);
2355 }
2356
2357 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2358 struct rdtgroup *prgrp,
2359 struct kernfs_node **mon_data_kn);
2360
rdt_enable_ctx(struct rdt_fs_context * ctx)2361 static int rdt_enable_ctx(struct rdt_fs_context *ctx)
2362 {
2363 int ret = 0;
2364
2365 if (ctx->enable_cdpl2)
2366 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
2367
2368 if (!ret && ctx->enable_cdpl3)
2369 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
2370
2371 if (!ret && ctx->enable_mba_mbps)
2372 ret = set_mba_sc(true);
2373
2374 return ret;
2375 }
2376
schemata_list_add(struct rdt_resource * r,enum resctrl_conf_type type)2377 static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
2378 {
2379 struct resctrl_schema *s;
2380 const char *suffix = "";
2381 int ret, cl;
2382
2383 s = kzalloc(sizeof(*s), GFP_KERNEL);
2384 if (!s)
2385 return -ENOMEM;
2386
2387 s->res = r;
2388 s->num_closid = resctrl_arch_get_num_closid(r);
2389 if (resctrl_arch_get_cdp_enabled(r->rid))
2390 s->num_closid /= 2;
2391
2392 s->conf_type = type;
2393 switch (type) {
2394 case CDP_CODE:
2395 suffix = "CODE";
2396 break;
2397 case CDP_DATA:
2398 suffix = "DATA";
2399 break;
2400 case CDP_NONE:
2401 suffix = "";
2402 break;
2403 }
2404
2405 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
2406 if (ret >= sizeof(s->name)) {
2407 kfree(s);
2408 return -EINVAL;
2409 }
2410
2411 cl = strlen(s->name);
2412
2413 /*
2414 * If CDP is supported by this resource, but not enabled,
2415 * include the suffix. This ensures the tabular format of the
2416 * schemata file does not change between mounts of the filesystem.
2417 */
2418 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
2419 cl += 4;
2420
2421 if (cl > max_name_width)
2422 max_name_width = cl;
2423
2424 INIT_LIST_HEAD(&s->list);
2425 list_add(&s->list, &resctrl_schema_all);
2426
2427 return 0;
2428 }
2429
schemata_list_create(void)2430 static int schemata_list_create(void)
2431 {
2432 struct rdt_resource *r;
2433 int ret = 0;
2434
2435 for_each_alloc_capable_rdt_resource(r) {
2436 if (resctrl_arch_get_cdp_enabled(r->rid)) {
2437 ret = schemata_list_add(r, CDP_CODE);
2438 if (ret)
2439 break;
2440
2441 ret = schemata_list_add(r, CDP_DATA);
2442 } else {
2443 ret = schemata_list_add(r, CDP_NONE);
2444 }
2445
2446 if (ret)
2447 break;
2448 }
2449
2450 return ret;
2451 }
2452
schemata_list_destroy(void)2453 static void schemata_list_destroy(void)
2454 {
2455 struct resctrl_schema *s, *tmp;
2456
2457 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
2458 list_del(&s->list);
2459 kfree(s);
2460 }
2461 }
2462
rdt_get_tree(struct fs_context * fc)2463 static int rdt_get_tree(struct fs_context *fc)
2464 {
2465 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2466 struct rdt_domain *dom;
2467 struct rdt_resource *r;
2468 int ret;
2469
2470 cpus_read_lock();
2471 mutex_lock(&rdtgroup_mutex);
2472 /*
2473 * resctrl file system can only be mounted once.
2474 */
2475 if (static_branch_unlikely(&rdt_enable_key)) {
2476 ret = -EBUSY;
2477 goto out;
2478 }
2479
2480 ret = rdt_enable_ctx(ctx);
2481 if (ret < 0)
2482 goto out_cdp;
2483
2484 ret = schemata_list_create();
2485 if (ret) {
2486 schemata_list_destroy();
2487 goto out_mba;
2488 }
2489
2490 closid_init();
2491
2492 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
2493 if (ret < 0)
2494 goto out_schemata_free;
2495
2496 if (rdt_mon_capable) {
2497 ret = mongroup_create_dir(rdtgroup_default.kn,
2498 &rdtgroup_default, "mon_groups",
2499 &kn_mongrp);
2500 if (ret < 0)
2501 goto out_info;
2502
2503 ret = mkdir_mondata_all(rdtgroup_default.kn,
2504 &rdtgroup_default, &kn_mondata);
2505 if (ret < 0)
2506 goto out_mongrp;
2507 rdtgroup_default.mon.mon_data_kn = kn_mondata;
2508 }
2509
2510 ret = rdt_pseudo_lock_init();
2511 if (ret)
2512 goto out_mondata;
2513
2514 ret = kernfs_get_tree(fc);
2515 if (ret < 0)
2516 goto out_psl;
2517
2518 if (rdt_alloc_capable)
2519 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
2520 if (rdt_mon_capable)
2521 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
2522
2523 if (rdt_alloc_capable || rdt_mon_capable)
2524 static_branch_enable_cpuslocked(&rdt_enable_key);
2525
2526 if (is_mbm_enabled()) {
2527 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
2528 list_for_each_entry(dom, &r->domains, list)
2529 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
2530 }
2531
2532 goto out;
2533
2534 out_psl:
2535 rdt_pseudo_lock_release();
2536 out_mondata:
2537 if (rdt_mon_capable)
2538 kernfs_remove(kn_mondata);
2539 out_mongrp:
2540 if (rdt_mon_capable)
2541 kernfs_remove(kn_mongrp);
2542 out_info:
2543 kernfs_remove(kn_info);
2544 out_schemata_free:
2545 schemata_list_destroy();
2546 out_mba:
2547 if (ctx->enable_mba_mbps)
2548 set_mba_sc(false);
2549 out_cdp:
2550 cdp_disable_all();
2551 out:
2552 rdt_last_cmd_clear();
2553 mutex_unlock(&rdtgroup_mutex);
2554 cpus_read_unlock();
2555 return ret;
2556 }
2557
2558 enum rdt_param {
2559 Opt_cdp,
2560 Opt_cdpl2,
2561 Opt_mba_mbps,
2562 nr__rdt_params
2563 };
2564
2565 static const struct fs_parameter_spec rdt_fs_parameters[] = {
2566 fsparam_flag("cdp", Opt_cdp),
2567 fsparam_flag("cdpl2", Opt_cdpl2),
2568 fsparam_flag("mba_MBps", Opt_mba_mbps),
2569 {}
2570 };
2571
rdt_parse_param(struct fs_context * fc,struct fs_parameter * param)2572 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2573 {
2574 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2575 struct fs_parse_result result;
2576 int opt;
2577
2578 opt = fs_parse(fc, rdt_fs_parameters, param, &result);
2579 if (opt < 0)
2580 return opt;
2581
2582 switch (opt) {
2583 case Opt_cdp:
2584 ctx->enable_cdpl3 = true;
2585 return 0;
2586 case Opt_cdpl2:
2587 ctx->enable_cdpl2 = true;
2588 return 0;
2589 case Opt_mba_mbps:
2590 if (!supports_mba_mbps())
2591 return -EINVAL;
2592 ctx->enable_mba_mbps = true;
2593 return 0;
2594 }
2595
2596 return -EINVAL;
2597 }
2598
rdt_fs_context_free(struct fs_context * fc)2599 static void rdt_fs_context_free(struct fs_context *fc)
2600 {
2601 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2602
2603 kernfs_free_fs_context(fc);
2604 kfree(ctx);
2605 }
2606
2607 static const struct fs_context_operations rdt_fs_context_ops = {
2608 .free = rdt_fs_context_free,
2609 .parse_param = rdt_parse_param,
2610 .get_tree = rdt_get_tree,
2611 };
2612
rdt_init_fs_context(struct fs_context * fc)2613 static int rdt_init_fs_context(struct fs_context *fc)
2614 {
2615 struct rdt_fs_context *ctx;
2616
2617 ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2618 if (!ctx)
2619 return -ENOMEM;
2620
2621 ctx->kfc.root = rdt_root;
2622 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2623 fc->fs_private = &ctx->kfc;
2624 fc->ops = &rdt_fs_context_ops;
2625 put_user_ns(fc->user_ns);
2626 fc->user_ns = get_user_ns(&init_user_ns);
2627 fc->global = true;
2628 return 0;
2629 }
2630
reset_all_ctrls(struct rdt_resource * r)2631 static int reset_all_ctrls(struct rdt_resource *r)
2632 {
2633 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2634 struct rdt_hw_domain *hw_dom;
2635 struct msr_param msr_param;
2636 cpumask_var_t cpu_mask;
2637 struct rdt_domain *d;
2638 int i;
2639
2640 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2641 return -ENOMEM;
2642
2643 msr_param.res = r;
2644 msr_param.low = 0;
2645 msr_param.high = hw_res->num_closid;
2646
2647 /*
2648 * Disable resource control for this resource by setting all
2649 * CBMs in all domains to the maximum mask value. Pick one CPU
2650 * from each domain to update the MSRs below.
2651 */
2652 list_for_each_entry(d, &r->domains, list) {
2653 hw_dom = resctrl_to_arch_dom(d);
2654 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2655
2656 for (i = 0; i < hw_res->num_closid; i++)
2657 hw_dom->ctrl_val[i] = r->default_ctrl;
2658 }
2659
2660 /* Update CBM on all the CPUs in cpu_mask */
2661 on_each_cpu_mask(cpu_mask, rdt_ctrl_update, &msr_param, 1);
2662
2663 free_cpumask_var(cpu_mask);
2664
2665 return 0;
2666 }
2667
2668 /*
2669 * Move tasks from one to the other group. If @from is NULL, then all tasks
2670 * in the systems are moved unconditionally (used for teardown).
2671 *
2672 * If @mask is not NULL the cpus on which moved tasks are running are set
2673 * in that mask so the update smp function call is restricted to affected
2674 * cpus.
2675 */
rdt_move_group_tasks(struct rdtgroup * from,struct rdtgroup * to,struct cpumask * mask)2676 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2677 struct cpumask *mask)
2678 {
2679 struct task_struct *p, *t;
2680
2681 read_lock(&tasklist_lock);
2682 for_each_process_thread(p, t) {
2683 if (!from || is_closid_match(t, from) ||
2684 is_rmid_match(t, from)) {
2685 WRITE_ONCE(t->closid, to->closid);
2686 WRITE_ONCE(t->rmid, to->mon.rmid);
2687
2688 /*
2689 * Order the closid/rmid stores above before the loads
2690 * in task_curr(). This pairs with the full barrier
2691 * between the rq->curr update and resctrl_sched_in()
2692 * during context switch.
2693 */
2694 smp_mb();
2695
2696 /*
2697 * If the task is on a CPU, set the CPU in the mask.
2698 * The detection is inaccurate as tasks might move or
2699 * schedule before the smp function call takes place.
2700 * In such a case the function call is pointless, but
2701 * there is no other side effect.
2702 */
2703 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
2704 cpumask_set_cpu(task_cpu(t), mask);
2705 }
2706 }
2707 read_unlock(&tasklist_lock);
2708 }
2709
free_all_child_rdtgrp(struct rdtgroup * rdtgrp)2710 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2711 {
2712 struct rdtgroup *sentry, *stmp;
2713 struct list_head *head;
2714
2715 head = &rdtgrp->mon.crdtgrp_list;
2716 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2717 free_rmid(sentry->mon.rmid);
2718 list_del(&sentry->mon.crdtgrp_list);
2719
2720 if (atomic_read(&sentry->waitcount) != 0)
2721 sentry->flags = RDT_DELETED;
2722 else
2723 rdtgroup_remove(sentry);
2724 }
2725 }
2726
2727 /*
2728 * Forcibly remove all of subdirectories under root.
2729 */
rmdir_all_sub(void)2730 static void rmdir_all_sub(void)
2731 {
2732 struct rdtgroup *rdtgrp, *tmp;
2733
2734 /* Move all tasks to the default resource group */
2735 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2736
2737 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2738 /* Free any child rmids */
2739 free_all_child_rdtgrp(rdtgrp);
2740
2741 /* Remove each rdtgroup other than root */
2742 if (rdtgrp == &rdtgroup_default)
2743 continue;
2744
2745 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2746 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2747 rdtgroup_pseudo_lock_remove(rdtgrp);
2748
2749 /*
2750 * Give any CPUs back to the default group. We cannot copy
2751 * cpu_online_mask because a CPU might have executed the
2752 * offline callback already, but is still marked online.
2753 */
2754 cpumask_or(&rdtgroup_default.cpu_mask,
2755 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2756
2757 free_rmid(rdtgrp->mon.rmid);
2758
2759 kernfs_remove(rdtgrp->kn);
2760 list_del(&rdtgrp->rdtgroup_list);
2761
2762 if (atomic_read(&rdtgrp->waitcount) != 0)
2763 rdtgrp->flags = RDT_DELETED;
2764 else
2765 rdtgroup_remove(rdtgrp);
2766 }
2767 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2768 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2769
2770 kernfs_remove(kn_info);
2771 kernfs_remove(kn_mongrp);
2772 kernfs_remove(kn_mondata);
2773 }
2774
rdt_kill_sb(struct super_block * sb)2775 static void rdt_kill_sb(struct super_block *sb)
2776 {
2777 struct rdt_resource *r;
2778
2779 cpus_read_lock();
2780 mutex_lock(&rdtgroup_mutex);
2781
2782 set_mba_sc(false);
2783
2784 /*Put everything back to default values. */
2785 for_each_alloc_capable_rdt_resource(r)
2786 reset_all_ctrls(r);
2787 cdp_disable_all();
2788 rmdir_all_sub();
2789 rdt_pseudo_lock_release();
2790 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2791 schemata_list_destroy();
2792 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2793 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2794 static_branch_disable_cpuslocked(&rdt_enable_key);
2795 kernfs_kill_sb(sb);
2796 mutex_unlock(&rdtgroup_mutex);
2797 cpus_read_unlock();
2798 }
2799
2800 static struct file_system_type rdt_fs_type = {
2801 .name = "resctrl",
2802 .init_fs_context = rdt_init_fs_context,
2803 .parameters = rdt_fs_parameters,
2804 .kill_sb = rdt_kill_sb,
2805 };
2806
mon_addfile(struct kernfs_node * parent_kn,const char * name,void * priv)2807 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2808 void *priv)
2809 {
2810 struct kernfs_node *kn;
2811 int ret = 0;
2812
2813 kn = __kernfs_create_file(parent_kn, name, 0444,
2814 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
2815 &kf_mondata_ops, priv, NULL, NULL);
2816 if (IS_ERR(kn))
2817 return PTR_ERR(kn);
2818
2819 ret = rdtgroup_kn_set_ugid(kn);
2820 if (ret) {
2821 kernfs_remove(kn);
2822 return ret;
2823 }
2824
2825 return ret;
2826 }
2827
2828 /*
2829 * Remove all subdirectories of mon_data of ctrl_mon groups
2830 * and monitor groups with given domain id.
2831 */
rmdir_mondata_subdir_allrdtgrp(struct rdt_resource * r,unsigned int dom_id)2832 static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2833 unsigned int dom_id)
2834 {
2835 struct rdtgroup *prgrp, *crgrp;
2836 char name[32];
2837
2838 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2839 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2840 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2841
2842 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2843 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2844 }
2845 }
2846
mkdir_mondata_subdir(struct kernfs_node * parent_kn,struct rdt_domain * d,struct rdt_resource * r,struct rdtgroup * prgrp)2847 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2848 struct rdt_domain *d,
2849 struct rdt_resource *r, struct rdtgroup *prgrp)
2850 {
2851 union mon_data_bits priv;
2852 struct kernfs_node *kn;
2853 struct mon_evt *mevt;
2854 struct rmid_read rr;
2855 char name[32];
2856 int ret;
2857
2858 sprintf(name, "mon_%s_%02d", r->name, d->id);
2859 /* create the directory */
2860 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2861 if (IS_ERR(kn))
2862 return PTR_ERR(kn);
2863
2864 ret = rdtgroup_kn_set_ugid(kn);
2865 if (ret)
2866 goto out_destroy;
2867
2868 if (WARN_ON(list_empty(&r->evt_list))) {
2869 ret = -EPERM;
2870 goto out_destroy;
2871 }
2872
2873 priv.u.rid = r->rid;
2874 priv.u.domid = d->id;
2875 list_for_each_entry(mevt, &r->evt_list, list) {
2876 priv.u.evtid = mevt->evtid;
2877 ret = mon_addfile(kn, mevt->name, priv.priv);
2878 if (ret)
2879 goto out_destroy;
2880
2881 if (is_mbm_event(mevt->evtid))
2882 mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
2883 }
2884 kernfs_activate(kn);
2885 return 0;
2886
2887 out_destroy:
2888 kernfs_remove(kn);
2889 return ret;
2890 }
2891
2892 /*
2893 * Add all subdirectories of mon_data for "ctrl_mon" groups
2894 * and "monitor" groups with given domain id.
2895 */
mkdir_mondata_subdir_allrdtgrp(struct rdt_resource * r,struct rdt_domain * d)2896 static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2897 struct rdt_domain *d)
2898 {
2899 struct kernfs_node *parent_kn;
2900 struct rdtgroup *prgrp, *crgrp;
2901 struct list_head *head;
2902
2903 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2904 parent_kn = prgrp->mon.mon_data_kn;
2905 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2906
2907 head = &prgrp->mon.crdtgrp_list;
2908 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2909 parent_kn = crgrp->mon.mon_data_kn;
2910 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2911 }
2912 }
2913 }
2914
mkdir_mondata_subdir_alldom(struct kernfs_node * parent_kn,struct rdt_resource * r,struct rdtgroup * prgrp)2915 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2916 struct rdt_resource *r,
2917 struct rdtgroup *prgrp)
2918 {
2919 struct rdt_domain *dom;
2920 int ret;
2921
2922 list_for_each_entry(dom, &r->domains, list) {
2923 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2924 if (ret)
2925 return ret;
2926 }
2927
2928 return 0;
2929 }
2930
2931 /*
2932 * This creates a directory mon_data which contains the monitored data.
2933 *
2934 * mon_data has one directory for each domain which are named
2935 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2936 * with L3 domain looks as below:
2937 * ./mon_data:
2938 * mon_L3_00
2939 * mon_L3_01
2940 * mon_L3_02
2941 * ...
2942 *
2943 * Each domain directory has one file per event:
2944 * ./mon_L3_00/:
2945 * llc_occupancy
2946 *
2947 */
mkdir_mondata_all(struct kernfs_node * parent_kn,struct rdtgroup * prgrp,struct kernfs_node ** dest_kn)2948 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2949 struct rdtgroup *prgrp,
2950 struct kernfs_node **dest_kn)
2951 {
2952 struct rdt_resource *r;
2953 struct kernfs_node *kn;
2954 int ret;
2955
2956 /*
2957 * Create the mon_data directory first.
2958 */
2959 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
2960 if (ret)
2961 return ret;
2962
2963 if (dest_kn)
2964 *dest_kn = kn;
2965
2966 /*
2967 * Create the subdirectories for each domain. Note that all events
2968 * in a domain like L3 are grouped into a resource whose domain is L3
2969 */
2970 for_each_mon_capable_rdt_resource(r) {
2971 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2972 if (ret)
2973 goto out_destroy;
2974 }
2975
2976 return 0;
2977
2978 out_destroy:
2979 kernfs_remove(kn);
2980 return ret;
2981 }
2982
2983 /**
2984 * cbm_ensure_valid - Enforce validity on provided CBM
2985 * @_val: Candidate CBM
2986 * @r: RDT resource to which the CBM belongs
2987 *
2988 * The provided CBM represents all cache portions available for use. This
2989 * may be represented by a bitmap that does not consist of contiguous ones
2990 * and thus be an invalid CBM.
2991 * Here the provided CBM is forced to be a valid CBM by only considering
2992 * the first set of contiguous bits as valid and clearing all bits.
2993 * The intention here is to provide a valid default CBM with which a new
2994 * resource group is initialized. The user can follow this with a
2995 * modification to the CBM if the default does not satisfy the
2996 * requirements.
2997 */
cbm_ensure_valid(u32 _val,struct rdt_resource * r)2998 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
2999 {
3000 unsigned int cbm_len = r->cache.cbm_len;
3001 unsigned long first_bit, zero_bit;
3002 unsigned long val = _val;
3003
3004 if (!val)
3005 return 0;
3006
3007 first_bit = find_first_bit(&val, cbm_len);
3008 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
3009
3010 /* Clear any remaining bits to ensure contiguous region */
3011 bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
3012 return (u32)val;
3013 }
3014
3015 /*
3016 * Initialize cache resources per RDT domain
3017 *
3018 * Set the RDT domain up to start off with all usable allocations. That is,
3019 * all shareable and unused bits. All-zero CBM is invalid.
3020 */
__init_one_rdt_domain(struct rdt_domain * d,struct resctrl_schema * s,u32 closid)3021 static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
3022 u32 closid)
3023 {
3024 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
3025 enum resctrl_conf_type t = s->conf_type;
3026 struct resctrl_staged_config *cfg;
3027 struct rdt_resource *r = s->res;
3028 u32 used_b = 0, unused_b = 0;
3029 unsigned long tmp_cbm;
3030 enum rdtgrp_mode mode;
3031 u32 peer_ctl, ctrl_val;
3032 int i;
3033
3034 cfg = &d->staged_config[t];
3035 cfg->have_new_ctrl = false;
3036 cfg->new_ctrl = r->cache.shareable_bits;
3037 used_b = r->cache.shareable_bits;
3038 for (i = 0; i < closids_supported(); i++) {
3039 if (closid_allocated(i) && i != closid) {
3040 mode = rdtgroup_mode_by_closid(i);
3041 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
3042 /*
3043 * ctrl values for locksetup aren't relevant
3044 * until the schemata is written, and the mode
3045 * becomes RDT_MODE_PSEUDO_LOCKED.
3046 */
3047 continue;
3048 /*
3049 * If CDP is active include peer domain's
3050 * usage to ensure there is no overlap
3051 * with an exclusive group.
3052 */
3053 if (resctrl_arch_get_cdp_enabled(r->rid))
3054 peer_ctl = resctrl_arch_get_config(r, d, i,
3055 peer_type);
3056 else
3057 peer_ctl = 0;
3058 ctrl_val = resctrl_arch_get_config(r, d, i,
3059 s->conf_type);
3060 used_b |= ctrl_val | peer_ctl;
3061 if (mode == RDT_MODE_SHAREABLE)
3062 cfg->new_ctrl |= ctrl_val | peer_ctl;
3063 }
3064 }
3065 if (d->plr && d->plr->cbm > 0)
3066 used_b |= d->plr->cbm;
3067 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
3068 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
3069 cfg->new_ctrl |= unused_b;
3070 /*
3071 * Force the initial CBM to be valid, user can
3072 * modify the CBM based on system availability.
3073 */
3074 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
3075 /*
3076 * Assign the u32 CBM to an unsigned long to ensure that
3077 * bitmap_weight() does not access out-of-bound memory.
3078 */
3079 tmp_cbm = cfg->new_ctrl;
3080 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
3081 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id);
3082 return -ENOSPC;
3083 }
3084 cfg->have_new_ctrl = true;
3085
3086 return 0;
3087 }
3088
3089 /*
3090 * Initialize cache resources with default values.
3091 *
3092 * A new RDT group is being created on an allocation capable (CAT)
3093 * supporting system. Set this group up to start off with all usable
3094 * allocations.
3095 *
3096 * If there are no more shareable bits available on any domain then
3097 * the entire allocation will fail.
3098 */
rdtgroup_init_cat(struct resctrl_schema * s,u32 closid)3099 static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
3100 {
3101 struct rdt_domain *d;
3102 int ret;
3103
3104 list_for_each_entry(d, &s->res->domains, list) {
3105 ret = __init_one_rdt_domain(d, s, closid);
3106 if (ret < 0)
3107 return ret;
3108 }
3109
3110 return 0;
3111 }
3112
3113 /* Initialize MBA resource with default values. */
rdtgroup_init_mba(struct rdt_resource * r,u32 closid)3114 static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
3115 {
3116 struct resctrl_staged_config *cfg;
3117 struct rdt_domain *d;
3118
3119 list_for_each_entry(d, &r->domains, list) {
3120 if (is_mba_sc(r)) {
3121 d->mbps_val[closid] = MBA_MAX_MBPS;
3122 continue;
3123 }
3124
3125 cfg = &d->staged_config[CDP_NONE];
3126 cfg->new_ctrl = r->default_ctrl;
3127 cfg->have_new_ctrl = true;
3128 }
3129 }
3130
3131 /* Initialize the RDT group's allocations. */
rdtgroup_init_alloc(struct rdtgroup * rdtgrp)3132 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
3133 {
3134 struct resctrl_schema *s;
3135 struct rdt_resource *r;
3136 int ret = 0;
3137
3138 rdt_staged_configs_clear();
3139
3140 list_for_each_entry(s, &resctrl_schema_all, list) {
3141 r = s->res;
3142 if (r->rid == RDT_RESOURCE_MBA ||
3143 r->rid == RDT_RESOURCE_SMBA) {
3144 rdtgroup_init_mba(r, rdtgrp->closid);
3145 if (is_mba_sc(r))
3146 continue;
3147 } else {
3148 ret = rdtgroup_init_cat(s, rdtgrp->closid);
3149 if (ret < 0)
3150 goto out;
3151 }
3152
3153 ret = resctrl_arch_update_domains(r, rdtgrp->closid);
3154 if (ret < 0) {
3155 rdt_last_cmd_puts("Failed to initialize allocations\n");
3156 goto out;
3157 }
3158
3159 }
3160
3161 rdtgrp->mode = RDT_MODE_SHAREABLE;
3162
3163 out:
3164 rdt_staged_configs_clear();
3165 return ret;
3166 }
3167
mkdir_rdt_prepare(struct kernfs_node * parent_kn,const char * name,umode_t mode,enum rdt_group_type rtype,struct rdtgroup ** r)3168 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
3169 const char *name, umode_t mode,
3170 enum rdt_group_type rtype, struct rdtgroup **r)
3171 {
3172 struct rdtgroup *prdtgrp, *rdtgrp;
3173 struct kernfs_node *kn;
3174 uint files = 0;
3175 int ret;
3176
3177 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
3178 if (!prdtgrp) {
3179 ret = -ENODEV;
3180 goto out_unlock;
3181 }
3182
3183 if (rtype == RDTMON_GROUP &&
3184 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3185 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
3186 ret = -EINVAL;
3187 rdt_last_cmd_puts("Pseudo-locking in progress\n");
3188 goto out_unlock;
3189 }
3190
3191 /* allocate the rdtgroup. */
3192 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
3193 if (!rdtgrp) {
3194 ret = -ENOSPC;
3195 rdt_last_cmd_puts("Kernel out of memory\n");
3196 goto out_unlock;
3197 }
3198 *r = rdtgrp;
3199 rdtgrp->mon.parent = prdtgrp;
3200 rdtgrp->type = rtype;
3201 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
3202
3203 /* kernfs creates the directory for rdtgrp */
3204 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
3205 if (IS_ERR(kn)) {
3206 ret = PTR_ERR(kn);
3207 rdt_last_cmd_puts("kernfs create error\n");
3208 goto out_free_rgrp;
3209 }
3210 rdtgrp->kn = kn;
3211
3212 /*
3213 * kernfs_remove() will drop the reference count on "kn" which
3214 * will free it. But we still need it to stick around for the
3215 * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
3216 * which will be dropped by kernfs_put() in rdtgroup_remove().
3217 */
3218 kernfs_get(kn);
3219
3220 ret = rdtgroup_kn_set_ugid(kn);
3221 if (ret) {
3222 rdt_last_cmd_puts("kernfs perm error\n");
3223 goto out_destroy;
3224 }
3225
3226 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
3227 ret = rdtgroup_add_files(kn, files);
3228 if (ret) {
3229 rdt_last_cmd_puts("kernfs fill error\n");
3230 goto out_destroy;
3231 }
3232
3233 if (rdt_mon_capable) {
3234 ret = alloc_rmid();
3235 if (ret < 0) {
3236 rdt_last_cmd_puts("Out of RMIDs\n");
3237 goto out_destroy;
3238 }
3239 rdtgrp->mon.rmid = ret;
3240
3241 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
3242 if (ret) {
3243 rdt_last_cmd_puts("kernfs subdir error\n");
3244 goto out_idfree;
3245 }
3246 }
3247 kernfs_activate(kn);
3248
3249 /*
3250 * The caller unlocks the parent_kn upon success.
3251 */
3252 return 0;
3253
3254 out_idfree:
3255 free_rmid(rdtgrp->mon.rmid);
3256 out_destroy:
3257 kernfs_put(rdtgrp->kn);
3258 kernfs_remove(rdtgrp->kn);
3259 out_free_rgrp:
3260 kfree(rdtgrp);
3261 out_unlock:
3262 rdtgroup_kn_unlock(parent_kn);
3263 return ret;
3264 }
3265
mkdir_rdt_prepare_clean(struct rdtgroup * rgrp)3266 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
3267 {
3268 kernfs_remove(rgrp->kn);
3269 free_rmid(rgrp->mon.rmid);
3270 rdtgroup_remove(rgrp);
3271 }
3272
3273 /*
3274 * Create a monitor group under "mon_groups" directory of a control
3275 * and monitor group(ctrl_mon). This is a resource group
3276 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
3277 */
rdtgroup_mkdir_mon(struct kernfs_node * parent_kn,const char * name,umode_t mode)3278 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
3279 const char *name, umode_t mode)
3280 {
3281 struct rdtgroup *rdtgrp, *prgrp;
3282 int ret;
3283
3284 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
3285 if (ret)
3286 return ret;
3287
3288 prgrp = rdtgrp->mon.parent;
3289 rdtgrp->closid = prgrp->closid;
3290
3291 /*
3292 * Add the rdtgrp to the list of rdtgrps the parent
3293 * ctrl_mon group has to track.
3294 */
3295 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
3296
3297 rdtgroup_kn_unlock(parent_kn);
3298 return ret;
3299 }
3300
3301 /*
3302 * These are rdtgroups created under the root directory. Can be used
3303 * to allocate and monitor resources.
3304 */
rdtgroup_mkdir_ctrl_mon(struct kernfs_node * parent_kn,const char * name,umode_t mode)3305 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
3306 const char *name, umode_t mode)
3307 {
3308 struct rdtgroup *rdtgrp;
3309 struct kernfs_node *kn;
3310 u32 closid;
3311 int ret;
3312
3313 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
3314 if (ret)
3315 return ret;
3316
3317 kn = rdtgrp->kn;
3318 ret = closid_alloc();
3319 if (ret < 0) {
3320 rdt_last_cmd_puts("Out of CLOSIDs\n");
3321 goto out_common_fail;
3322 }
3323 closid = ret;
3324 ret = 0;
3325
3326 rdtgrp->closid = closid;
3327 ret = rdtgroup_init_alloc(rdtgrp);
3328 if (ret < 0)
3329 goto out_id_free;
3330
3331 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
3332
3333 if (rdt_mon_capable) {
3334 /*
3335 * Create an empty mon_groups directory to hold the subset
3336 * of tasks and cpus to monitor.
3337 */
3338 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
3339 if (ret) {
3340 rdt_last_cmd_puts("kernfs subdir error\n");
3341 goto out_del_list;
3342 }
3343 }
3344
3345 goto out_unlock;
3346
3347 out_del_list:
3348 list_del(&rdtgrp->rdtgroup_list);
3349 out_id_free:
3350 closid_free(closid);
3351 out_common_fail:
3352 mkdir_rdt_prepare_clean(rdtgrp);
3353 out_unlock:
3354 rdtgroup_kn_unlock(parent_kn);
3355 return ret;
3356 }
3357
3358 /*
3359 * We allow creating mon groups only with in a directory called "mon_groups"
3360 * which is present in every ctrl_mon group. Check if this is a valid
3361 * "mon_groups" directory.
3362 *
3363 * 1. The directory should be named "mon_groups".
3364 * 2. The mon group itself should "not" be named "mon_groups".
3365 * This makes sure "mon_groups" directory always has a ctrl_mon group
3366 * as parent.
3367 */
is_mon_groups(struct kernfs_node * kn,const char * name)3368 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
3369 {
3370 return (!strcmp(kn->name, "mon_groups") &&
3371 strcmp(name, "mon_groups"));
3372 }
3373
rdtgroup_mkdir(struct kernfs_node * parent_kn,const char * name,umode_t mode)3374 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3375 umode_t mode)
3376 {
3377 /* Do not accept '\n' to avoid unparsable situation. */
3378 if (strchr(name, '\n'))
3379 return -EINVAL;
3380
3381 /*
3382 * If the parent directory is the root directory and RDT
3383 * allocation is supported, add a control and monitoring
3384 * subdirectory
3385 */
3386 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
3387 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
3388
3389 /*
3390 * If RDT monitoring is supported and the parent directory is a valid
3391 * "mon_groups" directory, add a monitoring subdirectory.
3392 */
3393 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
3394 return rdtgroup_mkdir_mon(parent_kn, name, mode);
3395
3396 return -EPERM;
3397 }
3398
rdtgroup_rmdir_mon(struct rdtgroup * rdtgrp,cpumask_var_t tmpmask)3399 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3400 {
3401 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3402 int cpu;
3403
3404 /* Give any tasks back to the parent group */
3405 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
3406
3407 /* Update per cpu rmid of the moved CPUs first */
3408 for_each_cpu(cpu, &rdtgrp->cpu_mask)
3409 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
3410 /*
3411 * Update the MSR on moved CPUs and CPUs which have moved
3412 * task running on them.
3413 */
3414 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3415 update_closid_rmid(tmpmask, NULL);
3416
3417 rdtgrp->flags = RDT_DELETED;
3418 free_rmid(rdtgrp->mon.rmid);
3419
3420 /*
3421 * Remove the rdtgrp from the parent ctrl_mon group's list
3422 */
3423 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3424 list_del(&rdtgrp->mon.crdtgrp_list);
3425
3426 kernfs_remove(rdtgrp->kn);
3427
3428 return 0;
3429 }
3430
rdtgroup_ctrl_remove(struct rdtgroup * rdtgrp)3431 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
3432 {
3433 rdtgrp->flags = RDT_DELETED;
3434 list_del(&rdtgrp->rdtgroup_list);
3435
3436 kernfs_remove(rdtgrp->kn);
3437 return 0;
3438 }
3439
rdtgroup_rmdir_ctrl(struct rdtgroup * rdtgrp,cpumask_var_t tmpmask)3440 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3441 {
3442 int cpu;
3443
3444 /* Give any tasks back to the default group */
3445 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
3446
3447 /* Give any CPUs back to the default group */
3448 cpumask_or(&rdtgroup_default.cpu_mask,
3449 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
3450
3451 /* Update per cpu closid and rmid of the moved CPUs first */
3452 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
3453 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3454 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
3455 }
3456
3457 /*
3458 * Update the MSR on moved CPUs and CPUs which have moved
3459 * task running on them.
3460 */
3461 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3462 update_closid_rmid(tmpmask, NULL);
3463
3464 closid_free(rdtgrp->closid);
3465 free_rmid(rdtgrp->mon.rmid);
3466
3467 rdtgroup_ctrl_remove(rdtgrp);
3468
3469 /*
3470 * Free all the child monitor group rmids.
3471 */
3472 free_all_child_rdtgrp(rdtgrp);
3473
3474 return 0;
3475 }
3476
rdtgroup_rmdir(struct kernfs_node * kn)3477 static int rdtgroup_rmdir(struct kernfs_node *kn)
3478 {
3479 struct kernfs_node *parent_kn = kn->parent;
3480 struct rdtgroup *rdtgrp;
3481 cpumask_var_t tmpmask;
3482 int ret = 0;
3483
3484 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3485 return -ENOMEM;
3486
3487 rdtgrp = rdtgroup_kn_lock_live(kn);
3488 if (!rdtgrp) {
3489 ret = -EPERM;
3490 goto out;
3491 }
3492
3493 /*
3494 * If the rdtgroup is a ctrl_mon group and parent directory
3495 * is the root directory, remove the ctrl_mon group.
3496 *
3497 * If the rdtgroup is a mon group and parent directory
3498 * is a valid "mon_groups" directory, remove the mon group.
3499 */
3500 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3501 rdtgrp != &rdtgroup_default) {
3502 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3503 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
3504 ret = rdtgroup_ctrl_remove(rdtgrp);
3505 } else {
3506 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
3507 }
3508 } else if (rdtgrp->type == RDTMON_GROUP &&
3509 is_mon_groups(parent_kn, kn->name)) {
3510 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
3511 } else {
3512 ret = -EPERM;
3513 }
3514
3515 out:
3516 rdtgroup_kn_unlock(kn);
3517 free_cpumask_var(tmpmask);
3518 return ret;
3519 }
3520
3521 /**
3522 * mongrp_reparent() - replace parent CTRL_MON group of a MON group
3523 * @rdtgrp: the MON group whose parent should be replaced
3524 * @new_prdtgrp: replacement parent CTRL_MON group for @rdtgrp
3525 * @cpus: cpumask provided by the caller for use during this call
3526 *
3527 * Replaces the parent CTRL_MON group for a MON group, resulting in all member
3528 * tasks' CLOSID immediately changing to that of the new parent group.
3529 * Monitoring data for the group is unaffected by this operation.
3530 */
mongrp_reparent(struct rdtgroup * rdtgrp,struct rdtgroup * new_prdtgrp,cpumask_var_t cpus)3531 static void mongrp_reparent(struct rdtgroup *rdtgrp,
3532 struct rdtgroup *new_prdtgrp,
3533 cpumask_var_t cpus)
3534 {
3535 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3536
3537 WARN_ON(rdtgrp->type != RDTMON_GROUP);
3538 WARN_ON(new_prdtgrp->type != RDTCTRL_GROUP);
3539
3540 /* Nothing to do when simply renaming a MON group. */
3541 if (prdtgrp == new_prdtgrp)
3542 return;
3543
3544 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3545 list_move_tail(&rdtgrp->mon.crdtgrp_list,
3546 &new_prdtgrp->mon.crdtgrp_list);
3547
3548 rdtgrp->mon.parent = new_prdtgrp;
3549 rdtgrp->closid = new_prdtgrp->closid;
3550
3551 /* Propagate updated closid to all tasks in this group. */
3552 rdt_move_group_tasks(rdtgrp, rdtgrp, cpus);
3553
3554 update_closid_rmid(cpus, NULL);
3555 }
3556
rdtgroup_rename(struct kernfs_node * kn,struct kernfs_node * new_parent,const char * new_name)3557 static int rdtgroup_rename(struct kernfs_node *kn,
3558 struct kernfs_node *new_parent, const char *new_name)
3559 {
3560 struct rdtgroup *new_prdtgrp;
3561 struct rdtgroup *rdtgrp;
3562 cpumask_var_t tmpmask;
3563 int ret;
3564
3565 rdtgrp = kernfs_to_rdtgroup(kn);
3566 new_prdtgrp = kernfs_to_rdtgroup(new_parent);
3567 if (!rdtgrp || !new_prdtgrp)
3568 return -ENOENT;
3569
3570 /* Release both kernfs active_refs before obtaining rdtgroup mutex. */
3571 rdtgroup_kn_get(rdtgrp, kn);
3572 rdtgroup_kn_get(new_prdtgrp, new_parent);
3573
3574 mutex_lock(&rdtgroup_mutex);
3575
3576 rdt_last_cmd_clear();
3577
3578 /*
3579 * Don't allow kernfs_to_rdtgroup() to return a parent rdtgroup if
3580 * either kernfs_node is a file.
3581 */
3582 if (kernfs_type(kn) != KERNFS_DIR ||
3583 kernfs_type(new_parent) != KERNFS_DIR) {
3584 rdt_last_cmd_puts("Source and destination must be directories");
3585 ret = -EPERM;
3586 goto out;
3587 }
3588
3589 if ((rdtgrp->flags & RDT_DELETED) || (new_prdtgrp->flags & RDT_DELETED)) {
3590 ret = -ENOENT;
3591 goto out;
3592 }
3593
3594 if (rdtgrp->type != RDTMON_GROUP || !kn->parent ||
3595 !is_mon_groups(kn->parent, kn->name)) {
3596 rdt_last_cmd_puts("Source must be a MON group\n");
3597 ret = -EPERM;
3598 goto out;
3599 }
3600
3601 if (!is_mon_groups(new_parent, new_name)) {
3602 rdt_last_cmd_puts("Destination must be a mon_groups subdirectory\n");
3603 ret = -EPERM;
3604 goto out;
3605 }
3606
3607 /*
3608 * If the MON group is monitoring CPUs, the CPUs must be assigned to the
3609 * current parent CTRL_MON group and therefore cannot be assigned to
3610 * the new parent, making the move illegal.
3611 */
3612 if (!cpumask_empty(&rdtgrp->cpu_mask) &&
3613 rdtgrp->mon.parent != new_prdtgrp) {
3614 rdt_last_cmd_puts("Cannot move a MON group that monitors CPUs\n");
3615 ret = -EPERM;
3616 goto out;
3617 }
3618
3619 /*
3620 * Allocate the cpumask for use in mongrp_reparent() to avoid the
3621 * possibility of failing to allocate it after kernfs_rename() has
3622 * succeeded.
3623 */
3624 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) {
3625 ret = -ENOMEM;
3626 goto out;
3627 }
3628
3629 /*
3630 * Perform all input validation and allocations needed to ensure
3631 * mongrp_reparent() will succeed before calling kernfs_rename(),
3632 * otherwise it would be necessary to revert this call if
3633 * mongrp_reparent() failed.
3634 */
3635 ret = kernfs_rename(kn, new_parent, new_name);
3636 if (!ret)
3637 mongrp_reparent(rdtgrp, new_prdtgrp, tmpmask);
3638
3639 free_cpumask_var(tmpmask);
3640
3641 out:
3642 mutex_unlock(&rdtgroup_mutex);
3643 rdtgroup_kn_put(rdtgrp, kn);
3644 rdtgroup_kn_put(new_prdtgrp, new_parent);
3645 return ret;
3646 }
3647
rdtgroup_show_options(struct seq_file * seq,struct kernfs_root * kf)3648 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3649 {
3650 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
3651 seq_puts(seq, ",cdp");
3652
3653 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
3654 seq_puts(seq, ",cdpl2");
3655
3656 if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
3657 seq_puts(seq, ",mba_MBps");
3658
3659 return 0;
3660 }
3661
3662 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
3663 .mkdir = rdtgroup_mkdir,
3664 .rmdir = rdtgroup_rmdir,
3665 .rename = rdtgroup_rename,
3666 .show_options = rdtgroup_show_options,
3667 };
3668
rdtgroup_setup_root(void)3669 static int __init rdtgroup_setup_root(void)
3670 {
3671 int ret;
3672
3673 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
3674 KERNFS_ROOT_CREATE_DEACTIVATED |
3675 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
3676 &rdtgroup_default);
3677 if (IS_ERR(rdt_root))
3678 return PTR_ERR(rdt_root);
3679
3680 mutex_lock(&rdtgroup_mutex);
3681
3682 rdtgroup_default.closid = 0;
3683 rdtgroup_default.mon.rmid = 0;
3684 rdtgroup_default.type = RDTCTRL_GROUP;
3685 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3686
3687 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3688
3689 ret = rdtgroup_add_files(kernfs_root_to_node(rdt_root), RF_CTRL_BASE);
3690 if (ret) {
3691 kernfs_destroy_root(rdt_root);
3692 goto out;
3693 }
3694
3695 rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
3696 kernfs_activate(rdtgroup_default.kn);
3697
3698 out:
3699 mutex_unlock(&rdtgroup_mutex);
3700
3701 return ret;
3702 }
3703
domain_destroy_mon_state(struct rdt_domain * d)3704 static void domain_destroy_mon_state(struct rdt_domain *d)
3705 {
3706 bitmap_free(d->rmid_busy_llc);
3707 kfree(d->mbm_total);
3708 kfree(d->mbm_local);
3709 }
3710
resctrl_offline_domain(struct rdt_resource * r,struct rdt_domain * d)3711 void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
3712 {
3713 lockdep_assert_held(&rdtgroup_mutex);
3714
3715 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3716 mba_sc_domain_destroy(r, d);
3717
3718 if (!r->mon_capable)
3719 return;
3720
3721 /*
3722 * If resctrl is mounted, remove all the
3723 * per domain monitor data directories.
3724 */
3725 if (static_branch_unlikely(&rdt_mon_enable_key))
3726 rmdir_mondata_subdir_allrdtgrp(r, d->id);
3727
3728 if (is_mbm_enabled())
3729 cancel_delayed_work(&d->mbm_over);
3730 if (is_llc_occupancy_enabled() && has_busy_rmid(r, d)) {
3731 /*
3732 * When a package is going down, forcefully
3733 * decrement rmid->ebusy. There is no way to know
3734 * that the L3 was flushed and hence may lead to
3735 * incorrect counts in rare scenarios, but leaving
3736 * the RMID as busy creates RMID leaks if the
3737 * package never comes back.
3738 */
3739 __check_limbo(d, true);
3740 cancel_delayed_work(&d->cqm_limbo);
3741 }
3742
3743 domain_destroy_mon_state(d);
3744 }
3745
domain_setup_mon_state(struct rdt_resource * r,struct rdt_domain * d)3746 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
3747 {
3748 size_t tsize;
3749
3750 if (is_llc_occupancy_enabled()) {
3751 d->rmid_busy_llc = bitmap_zalloc(r->num_rmid, GFP_KERNEL);
3752 if (!d->rmid_busy_llc)
3753 return -ENOMEM;
3754 }
3755 if (is_mbm_total_enabled()) {
3756 tsize = sizeof(*d->mbm_total);
3757 d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
3758 if (!d->mbm_total) {
3759 bitmap_free(d->rmid_busy_llc);
3760 return -ENOMEM;
3761 }
3762 }
3763 if (is_mbm_local_enabled()) {
3764 tsize = sizeof(*d->mbm_local);
3765 d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
3766 if (!d->mbm_local) {
3767 bitmap_free(d->rmid_busy_llc);
3768 kfree(d->mbm_total);
3769 return -ENOMEM;
3770 }
3771 }
3772
3773 return 0;
3774 }
3775
resctrl_online_domain(struct rdt_resource * r,struct rdt_domain * d)3776 int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
3777 {
3778 int err;
3779
3780 lockdep_assert_held(&rdtgroup_mutex);
3781
3782 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3783 /* RDT_RESOURCE_MBA is never mon_capable */
3784 return mba_sc_domain_allocate(r, d);
3785
3786 if (!r->mon_capable)
3787 return 0;
3788
3789 err = domain_setup_mon_state(r, d);
3790 if (err)
3791 return err;
3792
3793 if (is_mbm_enabled()) {
3794 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
3795 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
3796 }
3797
3798 if (is_llc_occupancy_enabled())
3799 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
3800
3801 /* If resctrl is mounted, add per domain monitor data directories. */
3802 if (static_branch_unlikely(&rdt_mon_enable_key))
3803 mkdir_mondata_subdir_allrdtgrp(r, d);
3804
3805 return 0;
3806 }
3807
3808 /*
3809 * rdtgroup_init - rdtgroup initialization
3810 *
3811 * Setup resctrl file system including set up root, create mount point,
3812 * register rdtgroup filesystem, and initialize files under root directory.
3813 *
3814 * Return: 0 on success or -errno
3815 */
rdtgroup_init(void)3816 int __init rdtgroup_init(void)
3817 {
3818 int ret = 0;
3819
3820 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
3821 sizeof(last_cmd_status_buf));
3822
3823 ret = rdtgroup_setup_root();
3824 if (ret)
3825 return ret;
3826
3827 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
3828 if (ret)
3829 goto cleanup_root;
3830
3831 ret = register_filesystem(&rdt_fs_type);
3832 if (ret)
3833 goto cleanup_mountpoint;
3834
3835 /*
3836 * Adding the resctrl debugfs directory here may not be ideal since
3837 * it would let the resctrl debugfs directory appear on the debugfs
3838 * filesystem before the resctrl filesystem is mounted.
3839 * It may also be ok since that would enable debugging of RDT before
3840 * resctrl is mounted.
3841 * The reason why the debugfs directory is created here and not in
3842 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
3843 * during the debugfs directory creation also &sb->s_type->i_mutex_key
3844 * (the lockdep class of inode->i_rwsem). Other filesystem
3845 * interactions (eg. SyS_getdents) have the lock ordering:
3846 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
3847 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
3848 * is taken, thus creating dependency:
3849 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
3850 * issues considering the other two lock dependencies.
3851 * By creating the debugfs directory here we avoid a dependency
3852 * that may cause deadlock (even though file operations cannot
3853 * occur until the filesystem is mounted, but I do not know how to
3854 * tell lockdep that).
3855 */
3856 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
3857
3858 return 0;
3859
3860 cleanup_mountpoint:
3861 sysfs_remove_mount_point(fs_kobj, "resctrl");
3862 cleanup_root:
3863 kernfs_destroy_root(rdt_root);
3864
3865 return ret;
3866 }
3867
rdtgroup_exit(void)3868 void __exit rdtgroup_exit(void)
3869 {
3870 debugfs_remove_recursive(debugfs_resctrl);
3871 unregister_filesystem(&rdt_fs_type);
3872 sysfs_remove_mount_point(fs_kobj, "resctrl");
3873 kernfs_destroy_root(rdt_root);
3874 }
3875