]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kernel/cpu/intel_rdt_rdtgroup.c
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / cpu / intel_rdt_rdtgroup.c
1 /*
2 * User interface for Resource Alloction in Resource Director Technology(RDT)
3 *
4 * Copyright (C) 2016 Intel Corporation
5 *
6 * Author: Fenghua Yu <fenghua.yu@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * More information about RDT be found in the Intel (R) x86 Architecture
18 * Software Developer Manual.
19 */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/cpu.h>
24 #include <linux/fs.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernfs.h>
27 #include <linux/seq_buf.h>
28 #include <linux/seq_file.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sched/task.h>
31 #include <linux/slab.h>
32 #include <linux/task_work.h>
33
34 #include <uapi/linux/magic.h>
35
36 #include <asm/intel_rdt_sched.h>
37 #include "intel_rdt.h"
38
39 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
40 DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
41 DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
42 static struct kernfs_root *rdt_root;
43 struct rdtgroup rdtgroup_default;
44 LIST_HEAD(rdt_all_groups);
45
46 /* Kernel fs node for "info" directory under root */
47 static struct kernfs_node *kn_info;
48
49 /* Kernel fs node for "mon_groups" directory under root */
50 static struct kernfs_node *kn_mongrp;
51
52 /* Kernel fs node for "mon_data" directory under root */
53 static struct kernfs_node *kn_mondata;
54
55 static struct seq_buf last_cmd_status;
56 static char last_cmd_status_buf[512];
57
58 void rdt_last_cmd_clear(void)
59 {
60 lockdep_assert_held(&rdtgroup_mutex);
61 seq_buf_clear(&last_cmd_status);
62 }
63
64 void rdt_last_cmd_puts(const char *s)
65 {
66 lockdep_assert_held(&rdtgroup_mutex);
67 seq_buf_puts(&last_cmd_status, s);
68 }
69
70 void rdt_last_cmd_printf(const char *fmt, ...)
71 {
72 va_list ap;
73
74 va_start(ap, fmt);
75 lockdep_assert_held(&rdtgroup_mutex);
76 seq_buf_vprintf(&last_cmd_status, fmt, ap);
77 va_end(ap);
78 }
79
80 /*
81 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
82 * we can keep a bitmap of free CLOSIDs in a single integer.
83 *
84 * Using a global CLOSID across all resources has some advantages and
85 * some drawbacks:
86 * + We can simply set "current->closid" to assign a task to a resource
87 * group.
88 * + Context switch code can avoid extra memory references deciding which
89 * CLOSID to load into the PQR_ASSOC MSR
90 * - We give up some options in configuring resource groups across multi-socket
91 * systems.
92 * - Our choices on how to configure each resource become progressively more
93 * limited as the number of resources grows.
94 */
95 static int closid_free_map;
96
97 static void closid_init(void)
98 {
99 struct rdt_resource *r;
100 int rdt_min_closid = 32;
101
102 /* Compute rdt_min_closid across all resources */
103 for_each_alloc_enabled_rdt_resource(r)
104 rdt_min_closid = min(rdt_min_closid, r->num_closid);
105
106 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
107
108 /* CLOSID 0 is always reserved for the default group */
109 closid_free_map &= ~1;
110 }
111
112 static int closid_alloc(void)
113 {
114 u32 closid = ffs(closid_free_map);
115
116 if (closid == 0)
117 return -ENOSPC;
118 closid--;
119 closid_free_map &= ~(1 << closid);
120
121 return closid;
122 }
123
124 static void closid_free(int closid)
125 {
126 closid_free_map |= 1 << closid;
127 }
128
129 /* set uid and gid of rdtgroup dirs and files to that of the creator */
130 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
131 {
132 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
133 .ia_uid = current_fsuid(),
134 .ia_gid = current_fsgid(), };
135
136 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
137 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
138 return 0;
139
140 return kernfs_setattr(kn, &iattr);
141 }
142
143 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
144 {
145 struct kernfs_node *kn;
146 int ret;
147
148 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
149 0, rft->kf_ops, rft, NULL, NULL);
150 if (IS_ERR(kn))
151 return PTR_ERR(kn);
152
153 ret = rdtgroup_kn_set_ugid(kn);
154 if (ret) {
155 kernfs_remove(kn);
156 return ret;
157 }
158
159 return 0;
160 }
161
162 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
163 {
164 struct kernfs_open_file *of = m->private;
165 struct rftype *rft = of->kn->priv;
166
167 if (rft->seq_show)
168 return rft->seq_show(of, m, arg);
169 return 0;
170 }
171
172 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
173 size_t nbytes, loff_t off)
174 {
175 struct rftype *rft = of->kn->priv;
176
177 if (rft->write)
178 return rft->write(of, buf, nbytes, off);
179
180 return -EINVAL;
181 }
182
183 static struct kernfs_ops rdtgroup_kf_single_ops = {
184 .atomic_write_len = PAGE_SIZE,
185 .write = rdtgroup_file_write,
186 .seq_show = rdtgroup_seqfile_show,
187 };
188
189 static struct kernfs_ops kf_mondata_ops = {
190 .atomic_write_len = PAGE_SIZE,
191 .seq_show = rdtgroup_mondata_show,
192 };
193
194 static bool is_cpu_list(struct kernfs_open_file *of)
195 {
196 struct rftype *rft = of->kn->priv;
197
198 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
199 }
200
201 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
202 struct seq_file *s, void *v)
203 {
204 struct rdtgroup *rdtgrp;
205 int ret = 0;
206
207 rdtgrp = rdtgroup_kn_lock_live(of->kn);
208
209 if (rdtgrp) {
210 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
211 cpumask_pr_args(&rdtgrp->cpu_mask));
212 } else {
213 ret = -ENOENT;
214 }
215 rdtgroup_kn_unlock(of->kn);
216
217 return ret;
218 }
219
220 /*
221 * This is safe against intel_rdt_sched_in() called from __switch_to()
222 * because __switch_to() is executed with interrupts disabled. A local call
223 * from update_closid_rmid() is proteced against __switch_to() because
224 * preemption is disabled.
225 */
226 static void update_cpu_closid_rmid(void *info)
227 {
228 struct rdtgroup *r = info;
229
230 if (r) {
231 this_cpu_write(pqr_state.default_closid, r->closid);
232 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
233 }
234
235 /*
236 * We cannot unconditionally write the MSR because the current
237 * executing task might have its own closid selected. Just reuse
238 * the context switch code.
239 */
240 intel_rdt_sched_in();
241 }
242
243 /*
244 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
245 *
246 * Per task closids/rmids must have been set up before calling this function.
247 */
248 static void
249 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
250 {
251 int cpu = get_cpu();
252
253 if (cpumask_test_cpu(cpu, cpu_mask))
254 update_cpu_closid_rmid(r);
255 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
256 put_cpu();
257 }
258
259 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
260 cpumask_var_t tmpmask)
261 {
262 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
263 struct list_head *head;
264
265 /* Check whether cpus belong to parent ctrl group */
266 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
267 if (cpumask_weight(tmpmask)) {
268 rdt_last_cmd_puts("can only add CPUs to mongroup that belong to parent\n");
269 return -EINVAL;
270 }
271
272 /* Check whether cpus are dropped from this group */
273 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
274 if (cpumask_weight(tmpmask)) {
275 /* Give any dropped cpus to parent rdtgroup */
276 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
277 update_closid_rmid(tmpmask, prgrp);
278 }
279
280 /*
281 * If we added cpus, remove them from previous group that owned them
282 * and update per-cpu rmid
283 */
284 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
285 if (cpumask_weight(tmpmask)) {
286 head = &prgrp->mon.crdtgrp_list;
287 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
288 if (crgrp == rdtgrp)
289 continue;
290 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
291 tmpmask);
292 }
293 update_closid_rmid(tmpmask, rdtgrp);
294 }
295
296 /* Done pushing/pulling - update this group with new mask */
297 cpumask_copy(&rdtgrp->cpu_mask, newmask);
298
299 return 0;
300 }
301
302 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
303 {
304 struct rdtgroup *crgrp;
305
306 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
307 /* update the child mon group masks as well*/
308 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
309 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
310 }
311
312 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
313 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
314 {
315 struct rdtgroup *r, *crgrp;
316 struct list_head *head;
317
318 /* Check whether cpus are dropped from this group */
319 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
320 if (cpumask_weight(tmpmask)) {
321 /* Can't drop from default group */
322 if (rdtgrp == &rdtgroup_default) {
323 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
324 return -EINVAL;
325 }
326
327 /* Give any dropped cpus to rdtgroup_default */
328 cpumask_or(&rdtgroup_default.cpu_mask,
329 &rdtgroup_default.cpu_mask, tmpmask);
330 update_closid_rmid(tmpmask, &rdtgroup_default);
331 }
332
333 /*
334 * If we added cpus, remove them from previous group and
335 * the prev group's child groups that owned them
336 * and update per-cpu closid/rmid.
337 */
338 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
339 if (cpumask_weight(tmpmask)) {
340 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
341 if (r == rdtgrp)
342 continue;
343 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
344 if (cpumask_weight(tmpmask1))
345 cpumask_rdtgrp_clear(r, tmpmask1);
346 }
347 update_closid_rmid(tmpmask, rdtgrp);
348 }
349
350 /* Done pushing/pulling - update this group with new mask */
351 cpumask_copy(&rdtgrp->cpu_mask, newmask);
352
353 /*
354 * Clear child mon group masks since there is a new parent mask
355 * now and update the rmid for the cpus the child lost.
356 */
357 head = &rdtgrp->mon.crdtgrp_list;
358 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
359 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
360 update_closid_rmid(tmpmask, rdtgrp);
361 cpumask_clear(&crgrp->cpu_mask);
362 }
363
364 return 0;
365 }
366
367 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
368 char *buf, size_t nbytes, loff_t off)
369 {
370 cpumask_var_t tmpmask, newmask, tmpmask1;
371 struct rdtgroup *rdtgrp;
372 int ret;
373
374 if (!buf)
375 return -EINVAL;
376
377 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
378 return -ENOMEM;
379 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
380 free_cpumask_var(tmpmask);
381 return -ENOMEM;
382 }
383 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
384 free_cpumask_var(tmpmask);
385 free_cpumask_var(newmask);
386 return -ENOMEM;
387 }
388
389 rdtgrp = rdtgroup_kn_lock_live(of->kn);
390 rdt_last_cmd_clear();
391 if (!rdtgrp) {
392 ret = -ENOENT;
393 rdt_last_cmd_puts("directory was removed\n");
394 goto unlock;
395 }
396
397 if (is_cpu_list(of))
398 ret = cpulist_parse(buf, newmask);
399 else
400 ret = cpumask_parse(buf, newmask);
401
402 if (ret) {
403 rdt_last_cmd_puts("bad cpu list/mask\n");
404 goto unlock;
405 }
406
407 /* check that user didn't specify any offline cpus */
408 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
409 if (cpumask_weight(tmpmask)) {
410 ret = -EINVAL;
411 rdt_last_cmd_puts("can only assign online cpus\n");
412 goto unlock;
413 }
414
415 if (rdtgrp->type == RDTCTRL_GROUP)
416 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
417 else if (rdtgrp->type == RDTMON_GROUP)
418 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
419 else
420 ret = -EINVAL;
421
422 unlock:
423 rdtgroup_kn_unlock(of->kn);
424 free_cpumask_var(tmpmask);
425 free_cpumask_var(newmask);
426 free_cpumask_var(tmpmask1);
427
428 return ret ?: nbytes;
429 }
430
431 struct task_move_callback {
432 struct callback_head work;
433 struct rdtgroup *rdtgrp;
434 };
435
436 static void move_myself(struct callback_head *head)
437 {
438 struct task_move_callback *callback;
439 struct rdtgroup *rdtgrp;
440
441 callback = container_of(head, struct task_move_callback, work);
442 rdtgrp = callback->rdtgrp;
443
444 /*
445 * If resource group was deleted before this task work callback
446 * was invoked, then assign the task to root group and free the
447 * resource group.
448 */
449 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
450 (rdtgrp->flags & RDT_DELETED)) {
451 current->closid = 0;
452 current->rmid = 0;
453 kfree(rdtgrp);
454 }
455
456 preempt_disable();
457 /* update PQR_ASSOC MSR to make resource group go into effect */
458 intel_rdt_sched_in();
459 preempt_enable();
460
461 kfree(callback);
462 }
463
464 static int __rdtgroup_move_task(struct task_struct *tsk,
465 struct rdtgroup *rdtgrp)
466 {
467 struct task_move_callback *callback;
468 int ret;
469
470 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
471 if (!callback)
472 return -ENOMEM;
473 callback->work.func = move_myself;
474 callback->rdtgrp = rdtgrp;
475
476 /*
477 * Take a refcount, so rdtgrp cannot be freed before the
478 * callback has been invoked.
479 */
480 atomic_inc(&rdtgrp->waitcount);
481 ret = task_work_add(tsk, &callback->work, true);
482 if (ret) {
483 /*
484 * Task is exiting. Drop the refcount and free the callback.
485 * No need to check the refcount as the group cannot be
486 * deleted before the write function unlocks rdtgroup_mutex.
487 */
488 atomic_dec(&rdtgrp->waitcount);
489 kfree(callback);
490 rdt_last_cmd_puts("task exited\n");
491 } else {
492 /*
493 * For ctrl_mon groups move both closid and rmid.
494 * For monitor groups, can move the tasks only from
495 * their parent CTRL group.
496 */
497 if (rdtgrp->type == RDTCTRL_GROUP) {
498 tsk->closid = rdtgrp->closid;
499 tsk->rmid = rdtgrp->mon.rmid;
500 } else if (rdtgrp->type == RDTMON_GROUP) {
501 if (rdtgrp->mon.parent->closid == tsk->closid) {
502 tsk->rmid = rdtgrp->mon.rmid;
503 } else {
504 rdt_last_cmd_puts("Can't move task to different control group\n");
505 ret = -EINVAL;
506 }
507 }
508 }
509 return ret;
510 }
511
512 static int rdtgroup_task_write_permission(struct task_struct *task,
513 struct kernfs_open_file *of)
514 {
515 const struct cred *tcred = get_task_cred(task);
516 const struct cred *cred = current_cred();
517 int ret = 0;
518
519 /*
520 * Even if we're attaching all tasks in the thread group, we only
521 * need to check permissions on one of them.
522 */
523 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
524 !uid_eq(cred->euid, tcred->uid) &&
525 !uid_eq(cred->euid, tcred->suid)) {
526 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
527 ret = -EPERM;
528 }
529
530 put_cred(tcred);
531 return ret;
532 }
533
534 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
535 struct kernfs_open_file *of)
536 {
537 struct task_struct *tsk;
538 int ret;
539
540 rcu_read_lock();
541 if (pid) {
542 tsk = find_task_by_vpid(pid);
543 if (!tsk) {
544 rcu_read_unlock();
545 rdt_last_cmd_printf("No task %d\n", pid);
546 return -ESRCH;
547 }
548 } else {
549 tsk = current;
550 }
551
552 get_task_struct(tsk);
553 rcu_read_unlock();
554
555 ret = rdtgroup_task_write_permission(tsk, of);
556 if (!ret)
557 ret = __rdtgroup_move_task(tsk, rdtgrp);
558
559 put_task_struct(tsk);
560 return ret;
561 }
562
563 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
564 char *buf, size_t nbytes, loff_t off)
565 {
566 struct rdtgroup *rdtgrp;
567 int ret = 0;
568 pid_t pid;
569
570 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
571 return -EINVAL;
572 rdtgrp = rdtgroup_kn_lock_live(of->kn);
573 rdt_last_cmd_clear();
574
575 if (rdtgrp)
576 ret = rdtgroup_move_task(pid, rdtgrp, of);
577 else
578 ret = -ENOENT;
579
580 rdtgroup_kn_unlock(of->kn);
581
582 return ret ?: nbytes;
583 }
584
585 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
586 {
587 struct task_struct *p, *t;
588
589 rcu_read_lock();
590 for_each_process_thread(p, t) {
591 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
592 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid))
593 seq_printf(s, "%d\n", t->pid);
594 }
595 rcu_read_unlock();
596 }
597
598 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
599 struct seq_file *s, void *v)
600 {
601 struct rdtgroup *rdtgrp;
602 int ret = 0;
603
604 rdtgrp = rdtgroup_kn_lock_live(of->kn);
605 if (rdtgrp)
606 show_rdt_tasks(rdtgrp, s);
607 else
608 ret = -ENOENT;
609 rdtgroup_kn_unlock(of->kn);
610
611 return ret;
612 }
613
614 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
615 struct seq_file *seq, void *v)
616 {
617 int len;
618
619 mutex_lock(&rdtgroup_mutex);
620 len = seq_buf_used(&last_cmd_status);
621 if (len)
622 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
623 else
624 seq_puts(seq, "ok\n");
625 mutex_unlock(&rdtgroup_mutex);
626 return 0;
627 }
628
629 static int rdt_num_closids_show(struct kernfs_open_file *of,
630 struct seq_file *seq, void *v)
631 {
632 struct rdt_resource *r = of->kn->parent->priv;
633
634 seq_printf(seq, "%d\n", r->num_closid);
635 return 0;
636 }
637
638 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
639 struct seq_file *seq, void *v)
640 {
641 struct rdt_resource *r = of->kn->parent->priv;
642
643 seq_printf(seq, "%x\n", r->default_ctrl);
644 return 0;
645 }
646
647 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
648 struct seq_file *seq, void *v)
649 {
650 struct rdt_resource *r = of->kn->parent->priv;
651
652 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
653 return 0;
654 }
655
656 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
657 struct seq_file *seq, void *v)
658 {
659 struct rdt_resource *r = of->kn->parent->priv;
660
661 seq_printf(seq, "%x\n", r->cache.shareable_bits);
662 return 0;
663 }
664
665 static int rdt_min_bw_show(struct kernfs_open_file *of,
666 struct seq_file *seq, void *v)
667 {
668 struct rdt_resource *r = of->kn->parent->priv;
669
670 seq_printf(seq, "%u\n", r->membw.min_bw);
671 return 0;
672 }
673
674 static int rdt_num_rmids_show(struct kernfs_open_file *of,
675 struct seq_file *seq, void *v)
676 {
677 struct rdt_resource *r = of->kn->parent->priv;
678
679 seq_printf(seq, "%d\n", r->num_rmid);
680
681 return 0;
682 }
683
684 static int rdt_mon_features_show(struct kernfs_open_file *of,
685 struct seq_file *seq, void *v)
686 {
687 struct rdt_resource *r = of->kn->parent->priv;
688 struct mon_evt *mevt;
689
690 list_for_each_entry(mevt, &r->evt_list, list)
691 seq_printf(seq, "%s\n", mevt->name);
692
693 return 0;
694 }
695
696 static int rdt_bw_gran_show(struct kernfs_open_file *of,
697 struct seq_file *seq, void *v)
698 {
699 struct rdt_resource *r = of->kn->parent->priv;
700
701 seq_printf(seq, "%u\n", r->membw.bw_gran);
702 return 0;
703 }
704
705 static int rdt_delay_linear_show(struct kernfs_open_file *of,
706 struct seq_file *seq, void *v)
707 {
708 struct rdt_resource *r = of->kn->parent->priv;
709
710 seq_printf(seq, "%u\n", r->membw.delay_linear);
711 return 0;
712 }
713
714 static int max_threshold_occ_show(struct kernfs_open_file *of,
715 struct seq_file *seq, void *v)
716 {
717 struct rdt_resource *r = of->kn->parent->priv;
718
719 seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale);
720
721 return 0;
722 }
723
724 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
725 char *buf, size_t nbytes, loff_t off)
726 {
727 struct rdt_resource *r = of->kn->parent->priv;
728 unsigned int bytes;
729 int ret;
730
731 ret = kstrtouint(buf, 0, &bytes);
732 if (ret)
733 return ret;
734
735 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
736 return -EINVAL;
737
738 intel_cqm_threshold = bytes / r->mon_scale;
739
740 return nbytes;
741 }
742
743 /* rdtgroup information files for one cache resource. */
744 static struct rftype res_common_files[] = {
745 {
746 .name = "last_cmd_status",
747 .mode = 0444,
748 .kf_ops = &rdtgroup_kf_single_ops,
749 .seq_show = rdt_last_cmd_status_show,
750 .fflags = RF_TOP_INFO,
751 },
752 {
753 .name = "num_closids",
754 .mode = 0444,
755 .kf_ops = &rdtgroup_kf_single_ops,
756 .seq_show = rdt_num_closids_show,
757 .fflags = RF_CTRL_INFO,
758 },
759 {
760 .name = "mon_features",
761 .mode = 0444,
762 .kf_ops = &rdtgroup_kf_single_ops,
763 .seq_show = rdt_mon_features_show,
764 .fflags = RF_MON_INFO,
765 },
766 {
767 .name = "num_rmids",
768 .mode = 0444,
769 .kf_ops = &rdtgroup_kf_single_ops,
770 .seq_show = rdt_num_rmids_show,
771 .fflags = RF_MON_INFO,
772 },
773 {
774 .name = "cbm_mask",
775 .mode = 0444,
776 .kf_ops = &rdtgroup_kf_single_ops,
777 .seq_show = rdt_default_ctrl_show,
778 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
779 },
780 {
781 .name = "min_cbm_bits",
782 .mode = 0444,
783 .kf_ops = &rdtgroup_kf_single_ops,
784 .seq_show = rdt_min_cbm_bits_show,
785 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
786 },
787 {
788 .name = "shareable_bits",
789 .mode = 0444,
790 .kf_ops = &rdtgroup_kf_single_ops,
791 .seq_show = rdt_shareable_bits_show,
792 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
793 },
794 {
795 .name = "min_bandwidth",
796 .mode = 0444,
797 .kf_ops = &rdtgroup_kf_single_ops,
798 .seq_show = rdt_min_bw_show,
799 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
800 },
801 {
802 .name = "bandwidth_gran",
803 .mode = 0444,
804 .kf_ops = &rdtgroup_kf_single_ops,
805 .seq_show = rdt_bw_gran_show,
806 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
807 },
808 {
809 .name = "delay_linear",
810 .mode = 0444,
811 .kf_ops = &rdtgroup_kf_single_ops,
812 .seq_show = rdt_delay_linear_show,
813 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
814 },
815 {
816 .name = "max_threshold_occupancy",
817 .mode = 0644,
818 .kf_ops = &rdtgroup_kf_single_ops,
819 .write = max_threshold_occ_write,
820 .seq_show = max_threshold_occ_show,
821 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
822 },
823 {
824 .name = "cpus",
825 .mode = 0644,
826 .kf_ops = &rdtgroup_kf_single_ops,
827 .write = rdtgroup_cpus_write,
828 .seq_show = rdtgroup_cpus_show,
829 .fflags = RFTYPE_BASE,
830 },
831 {
832 .name = "cpus_list",
833 .mode = 0644,
834 .kf_ops = &rdtgroup_kf_single_ops,
835 .write = rdtgroup_cpus_write,
836 .seq_show = rdtgroup_cpus_show,
837 .flags = RFTYPE_FLAGS_CPUS_LIST,
838 .fflags = RFTYPE_BASE,
839 },
840 {
841 .name = "tasks",
842 .mode = 0644,
843 .kf_ops = &rdtgroup_kf_single_ops,
844 .write = rdtgroup_tasks_write,
845 .seq_show = rdtgroup_tasks_show,
846 .fflags = RFTYPE_BASE,
847 },
848 {
849 .name = "schemata",
850 .mode = 0644,
851 .kf_ops = &rdtgroup_kf_single_ops,
852 .write = rdtgroup_schemata_write,
853 .seq_show = rdtgroup_schemata_show,
854 .fflags = RF_CTRL_BASE,
855 },
856 };
857
858 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
859 {
860 struct rftype *rfts, *rft;
861 int ret, len;
862
863 rfts = res_common_files;
864 len = ARRAY_SIZE(res_common_files);
865
866 lockdep_assert_held(&rdtgroup_mutex);
867
868 for (rft = rfts; rft < rfts + len; rft++) {
869 if ((fflags & rft->fflags) == rft->fflags) {
870 ret = rdtgroup_add_file(kn, rft);
871 if (ret)
872 goto error;
873 }
874 }
875
876 return 0;
877 error:
878 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
879 while (--rft >= rfts) {
880 if ((fflags & rft->fflags) == rft->fflags)
881 kernfs_remove_by_name(kn, rft->name);
882 }
883 return ret;
884 }
885
886 static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
887 unsigned long fflags)
888 {
889 struct kernfs_node *kn_subdir;
890 int ret;
891
892 kn_subdir = kernfs_create_dir(kn_info, name,
893 kn_info->mode, r);
894 if (IS_ERR(kn_subdir))
895 return PTR_ERR(kn_subdir);
896
897 kernfs_get(kn_subdir);
898 ret = rdtgroup_kn_set_ugid(kn_subdir);
899 if (ret)
900 return ret;
901
902 ret = rdtgroup_add_files(kn_subdir, fflags);
903 if (!ret)
904 kernfs_activate(kn_subdir);
905
906 return ret;
907 }
908
909 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
910 {
911 struct rdt_resource *r;
912 unsigned long fflags;
913 char name[32];
914 int ret;
915
916 /* create the directory */
917 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
918 if (IS_ERR(kn_info))
919 return PTR_ERR(kn_info);
920 kernfs_get(kn_info);
921
922 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
923 if (ret)
924 goto out_destroy;
925
926 for_each_alloc_enabled_rdt_resource(r) {
927 fflags = r->fflags | RF_CTRL_INFO;
928 ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
929 if (ret)
930 goto out_destroy;
931 }
932
933 for_each_mon_enabled_rdt_resource(r) {
934 fflags = r->fflags | RF_MON_INFO;
935 sprintf(name, "%s_MON", r->name);
936 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
937 if (ret)
938 goto out_destroy;
939 }
940
941 /*
942 * This extra ref will be put in kernfs_remove() and guarantees
943 * that @rdtgrp->kn is always accessible.
944 */
945 kernfs_get(kn_info);
946
947 ret = rdtgroup_kn_set_ugid(kn_info);
948 if (ret)
949 goto out_destroy;
950
951 kernfs_activate(kn_info);
952
953 return 0;
954
955 out_destroy:
956 kernfs_remove(kn_info);
957 return ret;
958 }
959
960 static int
961 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
962 char *name, struct kernfs_node **dest_kn)
963 {
964 struct kernfs_node *kn;
965 int ret;
966
967 /* create the directory */
968 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
969 if (IS_ERR(kn))
970 return PTR_ERR(kn);
971
972 if (dest_kn)
973 *dest_kn = kn;
974
975 /*
976 * This extra ref will be put in kernfs_remove() and guarantees
977 * that @rdtgrp->kn is always accessible.
978 */
979 kernfs_get(kn);
980
981 ret = rdtgroup_kn_set_ugid(kn);
982 if (ret)
983 goto out_destroy;
984
985 kernfs_activate(kn);
986
987 return 0;
988
989 out_destroy:
990 kernfs_remove(kn);
991 return ret;
992 }
993 static void l3_qos_cfg_update(void *arg)
994 {
995 bool *enable = arg;
996
997 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
998 }
999
1000 static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
1001 {
1002 cpumask_var_t cpu_mask;
1003 struct rdt_domain *d;
1004 int cpu;
1005
1006 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1007 return -ENOMEM;
1008
1009 list_for_each_entry(d, &r->domains, list) {
1010 /* Pick one CPU from each domain instance to update MSR */
1011 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1012 }
1013 cpu = get_cpu();
1014 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1015 if (cpumask_test_cpu(cpu, cpu_mask))
1016 l3_qos_cfg_update(&enable);
1017 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1018 smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
1019 put_cpu();
1020
1021 free_cpumask_var(cpu_mask);
1022
1023 return 0;
1024 }
1025
1026 static int cdp_enable(void)
1027 {
1028 struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
1029 struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
1030 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
1031 int ret;
1032
1033 if (!r_l3->alloc_capable || !r_l3data->alloc_capable ||
1034 !r_l3code->alloc_capable)
1035 return -EINVAL;
1036
1037 ret = set_l3_qos_cfg(r_l3, true);
1038 if (!ret) {
1039 r_l3->alloc_enabled = false;
1040 r_l3data->alloc_enabled = true;
1041 r_l3code->alloc_enabled = true;
1042 }
1043 return ret;
1044 }
1045
1046 static void cdp_disable(void)
1047 {
1048 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
1049
1050 r->alloc_enabled = r->alloc_capable;
1051
1052 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) {
1053 rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled = false;
1054 rdt_resources_all[RDT_RESOURCE_L3CODE].alloc_enabled = false;
1055 set_l3_qos_cfg(r, false);
1056 }
1057 }
1058
1059 static int parse_rdtgroupfs_options(char *data)
1060 {
1061 char *token, *o = data;
1062 int ret = 0;
1063
1064 while ((token = strsep(&o, ",")) != NULL) {
1065 if (!*token)
1066 return -EINVAL;
1067
1068 if (!strcmp(token, "cdp"))
1069 ret = cdp_enable();
1070 }
1071
1072 return ret;
1073 }
1074
1075 /*
1076 * We don't allow rdtgroup directories to be created anywhere
1077 * except the root directory. Thus when looking for the rdtgroup
1078 * structure for a kernfs node we are either looking at a directory,
1079 * in which case the rdtgroup structure is pointed at by the "priv"
1080 * field, otherwise we have a file, and need only look to the parent
1081 * to find the rdtgroup.
1082 */
1083 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1084 {
1085 if (kernfs_type(kn) == KERNFS_DIR) {
1086 /*
1087 * All the resource directories use "kn->priv"
1088 * to point to the "struct rdtgroup" for the
1089 * resource. "info" and its subdirectories don't
1090 * have rdtgroup structures, so return NULL here.
1091 */
1092 if (kn == kn_info || kn->parent == kn_info)
1093 return NULL;
1094 else
1095 return kn->priv;
1096 } else {
1097 return kn->parent->priv;
1098 }
1099 }
1100
1101 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1102 {
1103 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1104
1105 if (!rdtgrp)
1106 return NULL;
1107
1108 atomic_inc(&rdtgrp->waitcount);
1109 kernfs_break_active_protection(kn);
1110
1111 mutex_lock(&rdtgroup_mutex);
1112
1113 /* Was this group deleted while we waited? */
1114 if (rdtgrp->flags & RDT_DELETED)
1115 return NULL;
1116
1117 return rdtgrp;
1118 }
1119
1120 void rdtgroup_kn_unlock(struct kernfs_node *kn)
1121 {
1122 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1123
1124 if (!rdtgrp)
1125 return;
1126
1127 mutex_unlock(&rdtgroup_mutex);
1128
1129 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
1130 (rdtgrp->flags & RDT_DELETED)) {
1131 kernfs_unbreak_active_protection(kn);
1132 kernfs_put(rdtgrp->kn);
1133 kfree(rdtgrp);
1134 } else {
1135 kernfs_unbreak_active_protection(kn);
1136 }
1137 }
1138
1139 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1140 struct rdtgroup *prgrp,
1141 struct kernfs_node **mon_data_kn);
1142
1143 static struct dentry *rdt_mount(struct file_system_type *fs_type,
1144 int flags, const char *unused_dev_name,
1145 void *data)
1146 {
1147 struct rdt_domain *dom;
1148 struct rdt_resource *r;
1149 struct dentry *dentry;
1150 int ret;
1151
1152 cpus_read_lock();
1153 mutex_lock(&rdtgroup_mutex);
1154 /*
1155 * resctrl file system can only be mounted once.
1156 */
1157 if (static_branch_unlikely(&rdt_enable_key)) {
1158 dentry = ERR_PTR(-EBUSY);
1159 goto out;
1160 }
1161
1162 ret = parse_rdtgroupfs_options(data);
1163 if (ret) {
1164 dentry = ERR_PTR(ret);
1165 goto out_cdp;
1166 }
1167
1168 closid_init();
1169
1170 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
1171 if (ret) {
1172 dentry = ERR_PTR(ret);
1173 goto out_cdp;
1174 }
1175
1176 if (rdt_mon_capable) {
1177 ret = mongroup_create_dir(rdtgroup_default.kn,
1178 NULL, "mon_groups",
1179 &kn_mongrp);
1180 if (ret) {
1181 dentry = ERR_PTR(ret);
1182 goto out_info;
1183 }
1184 kernfs_get(kn_mongrp);
1185
1186 ret = mkdir_mondata_all(rdtgroup_default.kn,
1187 &rdtgroup_default, &kn_mondata);
1188 if (ret) {
1189 dentry = ERR_PTR(ret);
1190 goto out_mongrp;
1191 }
1192 kernfs_get(kn_mondata);
1193 rdtgroup_default.mon.mon_data_kn = kn_mondata;
1194 }
1195
1196 dentry = kernfs_mount(fs_type, flags, rdt_root,
1197 RDTGROUP_SUPER_MAGIC, NULL);
1198 if (IS_ERR(dentry))
1199 goto out_mondata;
1200
1201 if (rdt_alloc_capable)
1202 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
1203 if (rdt_mon_capable)
1204 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
1205
1206 if (rdt_alloc_capable || rdt_mon_capable)
1207 static_branch_enable_cpuslocked(&rdt_enable_key);
1208
1209 if (is_mbm_enabled()) {
1210 r = &rdt_resources_all[RDT_RESOURCE_L3];
1211 list_for_each_entry(dom, &r->domains, list)
1212 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
1213 }
1214
1215 goto out;
1216
1217 out_mondata:
1218 if (rdt_mon_capable)
1219 kernfs_remove(kn_mondata);
1220 out_mongrp:
1221 if (rdt_mon_capable)
1222 kernfs_remove(kn_mongrp);
1223 out_info:
1224 kernfs_remove(kn_info);
1225 out_cdp:
1226 cdp_disable();
1227 out:
1228 rdt_last_cmd_clear();
1229 mutex_unlock(&rdtgroup_mutex);
1230 cpus_read_unlock();
1231
1232 return dentry;
1233 }
1234
1235 static int reset_all_ctrls(struct rdt_resource *r)
1236 {
1237 struct msr_param msr_param;
1238 cpumask_var_t cpu_mask;
1239 struct rdt_domain *d;
1240 int i, cpu;
1241
1242 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1243 return -ENOMEM;
1244
1245 msr_param.res = r;
1246 msr_param.low = 0;
1247 msr_param.high = r->num_closid;
1248
1249 /*
1250 * Disable resource control for this resource by setting all
1251 * CBMs in all domains to the maximum mask value. Pick one CPU
1252 * from each domain to update the MSRs below.
1253 */
1254 list_for_each_entry(d, &r->domains, list) {
1255 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1256
1257 for (i = 0; i < r->num_closid; i++)
1258 d->ctrl_val[i] = r->default_ctrl;
1259 }
1260 cpu = get_cpu();
1261 /* Update CBM on this cpu if it's in cpu_mask. */
1262 if (cpumask_test_cpu(cpu, cpu_mask))
1263 rdt_ctrl_update(&msr_param);
1264 /* Update CBM on all other cpus in cpu_mask. */
1265 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
1266 put_cpu();
1267
1268 free_cpumask_var(cpu_mask);
1269
1270 return 0;
1271 }
1272
1273 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
1274 {
1275 return (rdt_alloc_capable &&
1276 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
1277 }
1278
1279 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
1280 {
1281 return (rdt_mon_capable &&
1282 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
1283 }
1284
1285 /*
1286 * Move tasks from one to the other group. If @from is NULL, then all tasks
1287 * in the systems are moved unconditionally (used for teardown).
1288 *
1289 * If @mask is not NULL the cpus on which moved tasks are running are set
1290 * in that mask so the update smp function call is restricted to affected
1291 * cpus.
1292 */
1293 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
1294 struct cpumask *mask)
1295 {
1296 struct task_struct *p, *t;
1297
1298 read_lock(&tasklist_lock);
1299 for_each_process_thread(p, t) {
1300 if (!from || is_closid_match(t, from) ||
1301 is_rmid_match(t, from)) {
1302 t->closid = to->closid;
1303 t->rmid = to->mon.rmid;
1304
1305 #ifdef CONFIG_SMP
1306 /*
1307 * This is safe on x86 w/o barriers as the ordering
1308 * of writing to task_cpu() and t->on_cpu is
1309 * reverse to the reading here. The detection is
1310 * inaccurate as tasks might move or schedule
1311 * before the smp function call takes place. In
1312 * such a case the function call is pointless, but
1313 * there is no other side effect.
1314 */
1315 if (mask && t->on_cpu)
1316 cpumask_set_cpu(task_cpu(t), mask);
1317 #endif
1318 }
1319 }
1320 read_unlock(&tasklist_lock);
1321 }
1322
1323 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
1324 {
1325 struct rdtgroup *sentry, *stmp;
1326 struct list_head *head;
1327
1328 head = &rdtgrp->mon.crdtgrp_list;
1329 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
1330 free_rmid(sentry->mon.rmid);
1331 list_del(&sentry->mon.crdtgrp_list);
1332 kfree(sentry);
1333 }
1334 }
1335
1336 /*
1337 * Forcibly remove all of subdirectories under root.
1338 */
1339 static void rmdir_all_sub(void)
1340 {
1341 struct rdtgroup *rdtgrp, *tmp;
1342
1343 /* Move all tasks to the default resource group */
1344 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
1345
1346 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
1347 /* Free any child rmids */
1348 free_all_child_rdtgrp(rdtgrp);
1349
1350 /* Remove each rdtgroup other than root */
1351 if (rdtgrp == &rdtgroup_default)
1352 continue;
1353
1354 /*
1355 * Give any CPUs back to the default group. We cannot copy
1356 * cpu_online_mask because a CPU might have executed the
1357 * offline callback already, but is still marked online.
1358 */
1359 cpumask_or(&rdtgroup_default.cpu_mask,
1360 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1361
1362 free_rmid(rdtgrp->mon.rmid);
1363
1364 kernfs_remove(rdtgrp->kn);
1365 list_del(&rdtgrp->rdtgroup_list);
1366 kfree(rdtgrp);
1367 }
1368 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
1369 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
1370
1371 kernfs_remove(kn_info);
1372 kernfs_remove(kn_mongrp);
1373 kernfs_remove(kn_mondata);
1374 }
1375
1376 static void rdt_kill_sb(struct super_block *sb)
1377 {
1378 struct rdt_resource *r;
1379
1380 cpus_read_lock();
1381 mutex_lock(&rdtgroup_mutex);
1382
1383 /*Put everything back to default values. */
1384 for_each_alloc_enabled_rdt_resource(r)
1385 reset_all_ctrls(r);
1386 cdp_disable();
1387 rmdir_all_sub();
1388 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
1389 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
1390 static_branch_disable_cpuslocked(&rdt_enable_key);
1391 kernfs_kill_sb(sb);
1392 mutex_unlock(&rdtgroup_mutex);
1393 cpus_read_unlock();
1394 }
1395
1396 static struct file_system_type rdt_fs_type = {
1397 .name = "resctrl",
1398 .mount = rdt_mount,
1399 .kill_sb = rdt_kill_sb,
1400 };
1401
1402 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
1403 void *priv)
1404 {
1405 struct kernfs_node *kn;
1406 int ret = 0;
1407
1408 kn = __kernfs_create_file(parent_kn, name, 0444, 0,
1409 &kf_mondata_ops, priv, NULL, NULL);
1410 if (IS_ERR(kn))
1411 return PTR_ERR(kn);
1412
1413 ret = rdtgroup_kn_set_ugid(kn);
1414 if (ret) {
1415 kernfs_remove(kn);
1416 return ret;
1417 }
1418
1419 return ret;
1420 }
1421
1422 /*
1423 * Remove all subdirectories of mon_data of ctrl_mon groups
1424 * and monitor groups with given domain id.
1425 */
1426 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
1427 {
1428 struct rdtgroup *prgrp, *crgrp;
1429 char name[32];
1430
1431 if (!r->mon_enabled)
1432 return;
1433
1434 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
1435 sprintf(name, "mon_%s_%02d", r->name, dom_id);
1436 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
1437
1438 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
1439 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
1440 }
1441 }
1442
1443 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
1444 struct rdt_domain *d,
1445 struct rdt_resource *r, struct rdtgroup *prgrp)
1446 {
1447 union mon_data_bits priv;
1448 struct kernfs_node *kn;
1449 struct mon_evt *mevt;
1450 struct rmid_read rr;
1451 char name[32];
1452 int ret;
1453
1454 sprintf(name, "mon_%s_%02d", r->name, d->id);
1455 /* create the directory */
1456 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1457 if (IS_ERR(kn))
1458 return PTR_ERR(kn);
1459
1460 /*
1461 * This extra ref will be put in kernfs_remove() and guarantees
1462 * that kn is always accessible.
1463 */
1464 kernfs_get(kn);
1465 ret = rdtgroup_kn_set_ugid(kn);
1466 if (ret)
1467 goto out_destroy;
1468
1469 if (WARN_ON(list_empty(&r->evt_list))) {
1470 ret = -EPERM;
1471 goto out_destroy;
1472 }
1473
1474 priv.u.rid = r->rid;
1475 priv.u.domid = d->id;
1476 list_for_each_entry(mevt, &r->evt_list, list) {
1477 priv.u.evtid = mevt->evtid;
1478 ret = mon_addfile(kn, mevt->name, priv.priv);
1479 if (ret)
1480 goto out_destroy;
1481
1482 if (is_mbm_event(mevt->evtid))
1483 mon_event_read(&rr, d, prgrp, mevt->evtid, true);
1484 }
1485 kernfs_activate(kn);
1486 return 0;
1487
1488 out_destroy:
1489 kernfs_remove(kn);
1490 return ret;
1491 }
1492
1493 /*
1494 * Add all subdirectories of mon_data for "ctrl_mon" groups
1495 * and "monitor" groups with given domain id.
1496 */
1497 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
1498 struct rdt_domain *d)
1499 {
1500 struct kernfs_node *parent_kn;
1501 struct rdtgroup *prgrp, *crgrp;
1502 struct list_head *head;
1503
1504 if (!r->mon_enabled)
1505 return;
1506
1507 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
1508 parent_kn = prgrp->mon.mon_data_kn;
1509 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
1510
1511 head = &prgrp->mon.crdtgrp_list;
1512 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
1513 parent_kn = crgrp->mon.mon_data_kn;
1514 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
1515 }
1516 }
1517 }
1518
1519 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
1520 struct rdt_resource *r,
1521 struct rdtgroup *prgrp)
1522 {
1523 struct rdt_domain *dom;
1524 int ret;
1525
1526 list_for_each_entry(dom, &r->domains, list) {
1527 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
1528 if (ret)
1529 return ret;
1530 }
1531
1532 return 0;
1533 }
1534
1535 /*
1536 * This creates a directory mon_data which contains the monitored data.
1537 *
1538 * mon_data has one directory for each domain whic are named
1539 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
1540 * with L3 domain looks as below:
1541 * ./mon_data:
1542 * mon_L3_00
1543 * mon_L3_01
1544 * mon_L3_02
1545 * ...
1546 *
1547 * Each domain directory has one file per event:
1548 * ./mon_L3_00/:
1549 * llc_occupancy
1550 *
1551 */
1552 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1553 struct rdtgroup *prgrp,
1554 struct kernfs_node **dest_kn)
1555 {
1556 struct rdt_resource *r;
1557 struct kernfs_node *kn;
1558 int ret;
1559
1560 /*
1561 * Create the mon_data directory first.
1562 */
1563 ret = mongroup_create_dir(parent_kn, NULL, "mon_data", &kn);
1564 if (ret)
1565 return ret;
1566
1567 if (dest_kn)
1568 *dest_kn = kn;
1569
1570 /*
1571 * Create the subdirectories for each domain. Note that all events
1572 * in a domain like L3 are grouped into a resource whose domain is L3
1573 */
1574 for_each_mon_enabled_rdt_resource(r) {
1575 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
1576 if (ret)
1577 goto out_destroy;
1578 }
1579
1580 return 0;
1581
1582 out_destroy:
1583 kernfs_remove(kn);
1584 return ret;
1585 }
1586
1587 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
1588 struct kernfs_node *prgrp_kn,
1589 const char *name, umode_t mode,
1590 enum rdt_group_type rtype, struct rdtgroup **r)
1591 {
1592 struct rdtgroup *prdtgrp, *rdtgrp;
1593 struct kernfs_node *kn;
1594 uint files = 0;
1595 int ret;
1596
1597 prdtgrp = rdtgroup_kn_lock_live(prgrp_kn);
1598 rdt_last_cmd_clear();
1599 if (!prdtgrp) {
1600 ret = -ENODEV;
1601 rdt_last_cmd_puts("directory was removed\n");
1602 goto out_unlock;
1603 }
1604
1605 /* allocate the rdtgroup. */
1606 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
1607 if (!rdtgrp) {
1608 ret = -ENOSPC;
1609 rdt_last_cmd_puts("kernel out of memory\n");
1610 goto out_unlock;
1611 }
1612 *r = rdtgrp;
1613 rdtgrp->mon.parent = prdtgrp;
1614 rdtgrp->type = rtype;
1615 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
1616
1617 /* kernfs creates the directory for rdtgrp */
1618 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
1619 if (IS_ERR(kn)) {
1620 ret = PTR_ERR(kn);
1621 rdt_last_cmd_puts("kernfs create error\n");
1622 goto out_free_rgrp;
1623 }
1624 rdtgrp->kn = kn;
1625
1626 /*
1627 * kernfs_remove() will drop the reference count on "kn" which
1628 * will free it. But we still need it to stick around for the
1629 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
1630 * here, which will be dropped inside rdtgroup_kn_unlock().
1631 */
1632 kernfs_get(kn);
1633
1634 ret = rdtgroup_kn_set_ugid(kn);
1635 if (ret) {
1636 rdt_last_cmd_puts("kernfs perm error\n");
1637 goto out_destroy;
1638 }
1639
1640 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
1641 ret = rdtgroup_add_files(kn, files);
1642 if (ret) {
1643 rdt_last_cmd_puts("kernfs fill error\n");
1644 goto out_destroy;
1645 }
1646
1647 if (rdt_mon_capable) {
1648 ret = alloc_rmid();
1649 if (ret < 0) {
1650 rdt_last_cmd_puts("out of RMIDs\n");
1651 goto out_destroy;
1652 }
1653 rdtgrp->mon.rmid = ret;
1654
1655 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
1656 if (ret) {
1657 rdt_last_cmd_puts("kernfs subdir error\n");
1658 goto out_idfree;
1659 }
1660 }
1661 kernfs_activate(kn);
1662
1663 /*
1664 * The caller unlocks the prgrp_kn upon success.
1665 */
1666 return 0;
1667
1668 out_idfree:
1669 free_rmid(rdtgrp->mon.rmid);
1670 out_destroy:
1671 kernfs_remove(rdtgrp->kn);
1672 out_free_rgrp:
1673 kfree(rdtgrp);
1674 out_unlock:
1675 rdtgroup_kn_unlock(prgrp_kn);
1676 return ret;
1677 }
1678
1679 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
1680 {
1681 kernfs_remove(rgrp->kn);
1682 free_rmid(rgrp->mon.rmid);
1683 kfree(rgrp);
1684 }
1685
1686 /*
1687 * Create a monitor group under "mon_groups" directory of a control
1688 * and monitor group(ctrl_mon). This is a resource group
1689 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
1690 */
1691 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
1692 struct kernfs_node *prgrp_kn,
1693 const char *name,
1694 umode_t mode)
1695 {
1696 struct rdtgroup *rdtgrp, *prgrp;
1697 int ret;
1698
1699 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP,
1700 &rdtgrp);
1701 if (ret)
1702 return ret;
1703
1704 prgrp = rdtgrp->mon.parent;
1705 rdtgrp->closid = prgrp->closid;
1706
1707 /*
1708 * Add the rdtgrp to the list of rdtgrps the parent
1709 * ctrl_mon group has to track.
1710 */
1711 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
1712
1713 rdtgroup_kn_unlock(prgrp_kn);
1714 return ret;
1715 }
1716
1717 /*
1718 * These are rdtgroups created under the root directory. Can be used
1719 * to allocate and monitor resources.
1720 */
1721 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
1722 struct kernfs_node *prgrp_kn,
1723 const char *name, umode_t mode)
1724 {
1725 struct rdtgroup *rdtgrp;
1726 struct kernfs_node *kn;
1727 u32 closid;
1728 int ret;
1729
1730 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP,
1731 &rdtgrp);
1732 if (ret)
1733 return ret;
1734
1735 kn = rdtgrp->kn;
1736 ret = closid_alloc();
1737 if (ret < 0) {
1738 rdt_last_cmd_puts("out of CLOSIDs\n");
1739 goto out_common_fail;
1740 }
1741 closid = ret;
1742
1743 rdtgrp->closid = closid;
1744 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
1745
1746 if (rdt_mon_capable) {
1747 /*
1748 * Create an empty mon_groups directory to hold the subset
1749 * of tasks and cpus to monitor.
1750 */
1751 ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL);
1752 if (ret) {
1753 rdt_last_cmd_puts("kernfs subdir error\n");
1754 goto out_id_free;
1755 }
1756 }
1757
1758 goto out_unlock;
1759
1760 out_id_free:
1761 closid_free(closid);
1762 list_del(&rdtgrp->rdtgroup_list);
1763 out_common_fail:
1764 mkdir_rdt_prepare_clean(rdtgrp);
1765 out_unlock:
1766 rdtgroup_kn_unlock(prgrp_kn);
1767 return ret;
1768 }
1769
1770 /*
1771 * We allow creating mon groups only with in a directory called "mon_groups"
1772 * which is present in every ctrl_mon group. Check if this is a valid
1773 * "mon_groups" directory.
1774 *
1775 * 1. The directory should be named "mon_groups".
1776 * 2. The mon group itself should "not" be named "mon_groups".
1777 * This makes sure "mon_groups" directory always has a ctrl_mon group
1778 * as parent.
1779 */
1780 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
1781 {
1782 return (!strcmp(kn->name, "mon_groups") &&
1783 strcmp(name, "mon_groups"));
1784 }
1785
1786 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
1787 umode_t mode)
1788 {
1789 /* Do not accept '\n' to avoid unparsable situation. */
1790 if (strchr(name, '\n'))
1791 return -EINVAL;
1792
1793 /*
1794 * If the parent directory is the root directory and RDT
1795 * allocation is supported, add a control and monitoring
1796 * subdirectory
1797 */
1798 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
1799 return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode);
1800
1801 /*
1802 * If RDT monitoring is supported and the parent directory is a valid
1803 * "mon_groups" directory, add a monitoring subdirectory.
1804 */
1805 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
1806 return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode);
1807
1808 return -EPERM;
1809 }
1810
1811 static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
1812 cpumask_var_t tmpmask)
1813 {
1814 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
1815 int cpu;
1816
1817 /* Give any tasks back to the parent group */
1818 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
1819
1820 /* Update per cpu rmid of the moved CPUs first */
1821 for_each_cpu(cpu, &rdtgrp->cpu_mask)
1822 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
1823 /*
1824 * Update the MSR on moved CPUs and CPUs which have moved
1825 * task running on them.
1826 */
1827 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1828 update_closid_rmid(tmpmask, NULL);
1829
1830 rdtgrp->flags = RDT_DELETED;
1831 free_rmid(rdtgrp->mon.rmid);
1832
1833 /*
1834 * Remove the rdtgrp from the parent ctrl_mon group's list
1835 */
1836 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
1837 list_del(&rdtgrp->mon.crdtgrp_list);
1838
1839 /*
1840 * one extra hold on this, will drop when we kfree(rdtgrp)
1841 * in rdtgroup_kn_unlock()
1842 */
1843 kernfs_get(kn);
1844 kernfs_remove(rdtgrp->kn);
1845
1846 return 0;
1847 }
1848
1849 static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
1850 cpumask_var_t tmpmask)
1851 {
1852 int cpu;
1853
1854 /* Give any tasks back to the default group */
1855 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
1856
1857 /* Give any CPUs back to the default group */
1858 cpumask_or(&rdtgroup_default.cpu_mask,
1859 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1860
1861 /* Update per cpu closid and rmid of the moved CPUs first */
1862 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
1863 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
1864 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
1865 }
1866
1867 /*
1868 * Update the MSR on moved CPUs and CPUs which have moved
1869 * task running on them.
1870 */
1871 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1872 update_closid_rmid(tmpmask, NULL);
1873
1874 rdtgrp->flags = RDT_DELETED;
1875 closid_free(rdtgrp->closid);
1876 free_rmid(rdtgrp->mon.rmid);
1877
1878 /*
1879 * Free all the child monitor group rmids.
1880 */
1881 free_all_child_rdtgrp(rdtgrp);
1882
1883 list_del(&rdtgrp->rdtgroup_list);
1884
1885 /*
1886 * one extra hold on this, will drop when we kfree(rdtgrp)
1887 * in rdtgroup_kn_unlock()
1888 */
1889 kernfs_get(kn);
1890 kernfs_remove(rdtgrp->kn);
1891
1892 return 0;
1893 }
1894
1895 static int rdtgroup_rmdir(struct kernfs_node *kn)
1896 {
1897 struct kernfs_node *parent_kn = kn->parent;
1898 struct rdtgroup *rdtgrp;
1899 cpumask_var_t tmpmask;
1900 int ret = 0;
1901
1902 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1903 return -ENOMEM;
1904
1905 rdtgrp = rdtgroup_kn_lock_live(kn);
1906 if (!rdtgrp) {
1907 ret = -EPERM;
1908 goto out;
1909 }
1910
1911 /*
1912 * If the rdtgroup is a ctrl_mon group and parent directory
1913 * is the root directory, remove the ctrl_mon group.
1914 *
1915 * If the rdtgroup is a mon group and parent directory
1916 * is a valid "mon_groups" directory, remove the mon group.
1917 */
1918 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn)
1919 ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask);
1920 else if (rdtgrp->type == RDTMON_GROUP &&
1921 is_mon_groups(parent_kn, kn->name))
1922 ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask);
1923 else
1924 ret = -EPERM;
1925
1926 out:
1927 rdtgroup_kn_unlock(kn);
1928 free_cpumask_var(tmpmask);
1929 return ret;
1930 }
1931
1932 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1933 {
1934 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
1935 seq_puts(seq, ",cdp");
1936 return 0;
1937 }
1938
1939 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
1940 .mkdir = rdtgroup_mkdir,
1941 .rmdir = rdtgroup_rmdir,
1942 .show_options = rdtgroup_show_options,
1943 };
1944
1945 static int __init rdtgroup_setup_root(void)
1946 {
1947 int ret;
1948
1949 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1950 KERNFS_ROOT_CREATE_DEACTIVATED,
1951 &rdtgroup_default);
1952 if (IS_ERR(rdt_root))
1953 return PTR_ERR(rdt_root);
1954
1955 mutex_lock(&rdtgroup_mutex);
1956
1957 rdtgroup_default.closid = 0;
1958 rdtgroup_default.mon.rmid = 0;
1959 rdtgroup_default.type = RDTCTRL_GROUP;
1960 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
1961
1962 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1963
1964 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
1965 if (ret) {
1966 kernfs_destroy_root(rdt_root);
1967 goto out;
1968 }
1969
1970 rdtgroup_default.kn = rdt_root->kn;
1971 kernfs_activate(rdtgroup_default.kn);
1972
1973 out:
1974 mutex_unlock(&rdtgroup_mutex);
1975
1976 return ret;
1977 }
1978
1979 /*
1980 * rdtgroup_init - rdtgroup initialization
1981 *
1982 * Setup resctrl file system including set up root, create mount point,
1983 * register rdtgroup filesystem, and initialize files under root directory.
1984 *
1985 * Return: 0 on success or -errno
1986 */
1987 int __init rdtgroup_init(void)
1988 {
1989 int ret = 0;
1990
1991 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
1992 sizeof(last_cmd_status_buf));
1993
1994 ret = rdtgroup_setup_root();
1995 if (ret)
1996 return ret;
1997
1998 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1999 if (ret)
2000 goto cleanup_root;
2001
2002 ret = register_filesystem(&rdt_fs_type);
2003 if (ret)
2004 goto cleanup_mountpoint;
2005
2006 return 0;
2007
2008 cleanup_mountpoint:
2009 sysfs_remove_mount_point(fs_kobj, "resctrl");
2010 cleanup_root:
2011 kernfs_destroy_root(rdt_root);
2012
2013 return ret;
2014 }