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