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