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