2 * User interface for Resource Alloction in Resource Director Technology(RDT)
4 * Copyright (C) 2016 Intel Corporation
6 * Author: Fenghua Yu <fenghua.yu@intel.com>
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.
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
17 * More information about RDT be found in the Intel (R) x86 Architecture
18 * Software Developer Manual.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/cpu.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>
33 #include <uapi/linux/magic.h>
35 #include <asm/intel_rdt_sched.h>
36 #include "intel_rdt.h"
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);
45 /* Kernel fs node for "info" directory under root */
46 static struct kernfs_node *kn_info;
48 /* Kernel fs node for "mon_groups" directory under root */
49 static struct kernfs_node *kn_mongrp;
51 /* Kernel fs node for "mon_data" directory under root */
52 static struct kernfs_node *kn_mondata;
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.
58 * Using a global CLOSID across all resources has some advantages and
60 * + We can simply set "current->closid" to assign a task to a resource
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
66 * - Our choices on how to configure each resource become progressively more
67 * limited as the number of resources grows.
69 static int closid_free_map;
71 static void closid_init(void)
73 struct rdt_resource *r;
74 int rdt_min_closid = 32;
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);
80 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
82 /* CLOSID 0 is always reserved for the default group */
83 closid_free_map &= ~1;
86 static int closid_alloc(void)
88 u32 closid = ffs(closid_free_map);
93 closid_free_map &= ~(1 << closid);
98 static void closid_free(int closid)
100 closid_free_map |= 1 << closid;
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)
106 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
107 .ia_uid = current_fsuid(),
108 .ia_gid = current_fsgid(), };
110 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
111 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
114 return kernfs_setattr(kn, &iattr);
117 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
119 struct kernfs_node *kn;
122 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
123 0, rft->kf_ops, rft, NULL, NULL);
127 ret = rdtgroup_kn_set_ugid(kn);
136 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
138 struct kernfs_open_file *of = m->private;
139 struct rftype *rft = of->kn->priv;
142 return rft->seq_show(of, m, arg);
146 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
147 size_t nbytes, loff_t off)
149 struct rftype *rft = of->kn->priv;
152 return rft->write(of, buf, nbytes, off);
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,
163 static struct kernfs_ops kf_mondata_ops = {
164 .atomic_write_len = PAGE_SIZE,
165 .seq_show = rdtgroup_mondata_show,
168 static bool is_cpu_list(struct kernfs_open_file *of)
170 struct rftype *rft = of->kn->priv;
172 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
175 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
176 struct seq_file *s, void *v)
178 struct rdtgroup *rdtgrp;
181 rdtgrp = rdtgroup_kn_lock_live(of->kn);
184 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
185 cpumask_pr_args(&rdtgrp->cpu_mask));
189 rdtgroup_kn_unlock(of->kn);
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.
200 static void update_cpu_closid_rmid(void *info)
202 struct rdtgroup *r = info;
205 this_cpu_write(pqr_state.default_closid, r->closid);
206 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
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.
214 intel_rdt_sched_in();
218 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
220 * Per task closids/rmids must have been set up before calling this function.
223 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
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);
233 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
234 cpumask_var_t tmpmask)
236 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
237 struct list_head *head;
239 /* Check whether cpus belong to parent ctrl group */
240 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
241 if (cpumask_weight(tmpmask))
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);
253 * If we added cpus, remove them from previous group that owned them
254 * and update per-cpu rmid
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) {
262 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
265 update_closid_rmid(tmpmask, rdtgrp);
268 /* Done pushing/pulling - update this group with new mask */
269 cpumask_copy(&rdtgrp->cpu_mask, newmask);
274 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
276 struct rdtgroup *crgrp;
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);
284 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
285 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
287 struct rdtgroup *r, *crgrp;
288 struct list_head *head;
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)
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);
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.
308 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
309 if (cpumask_weight(tmpmask)) {
310 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
313 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
314 if (cpumask_weight(tmpmask1))
315 cpumask_rdtgrp_clear(r, tmpmask1);
317 update_closid_rmid(tmpmask, rdtgrp);
320 /* Done pushing/pulling - update this group with new mask */
321 cpumask_copy(&rdtgrp->cpu_mask, newmask);
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.
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);
337 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
338 char *buf, size_t nbytes, loff_t off)
340 cpumask_var_t tmpmask, newmask, tmpmask1;
341 struct rdtgroup *rdtgrp;
347 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
349 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
350 free_cpumask_var(tmpmask);
353 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
354 free_cpumask_var(tmpmask);
355 free_cpumask_var(newmask);
359 rdtgrp = rdtgroup_kn_lock_live(of->kn);
366 ret = cpulist_parse(buf, newmask);
368 ret = cpumask_parse(buf, newmask);
373 /* check that user didn't specify any offline cpus */
374 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
375 if (cpumask_weight(tmpmask)) {
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);
388 rdtgroup_kn_unlock(of->kn);
389 free_cpumask_var(tmpmask);
390 free_cpumask_var(newmask);
391 free_cpumask_var(tmpmask1);
393 return ret ?: nbytes;
397 * rdtgroup_remove - the helper to remove resource group safely
398 * @rdtgrp: resource group to remove
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.
404 * Drop the extra reference here, then free the rdtgroup structure.
408 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
410 kernfs_put(rdtgrp->kn);
414 static void _update_task_closid_rmid(void *task)
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.
421 intel_rdt_sched_in();
424 static void update_task_closid_rmid(struct task_struct *t)
426 if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
427 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
429 _update_task_closid_rmid(t);
432 static int __rdtgroup_move_task(struct task_struct *tsk,
433 struct rdtgroup *rdtgrp)
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))
443 * Set the task's closid/rmid before the PQR_ASSOC MSR can be
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.
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;
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.
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.
473 update_task_closid_rmid(tsk);
478 static int rdtgroup_task_write_permission(struct task_struct *task,
479 struct kernfs_open_file *of)
481 const struct cred *tcred = get_task_cred(task);
482 const struct cred *cred = current_cred();
486 * Even if we're attaching all tasks in the thread group, we only
487 * need to check permissions on one of them.
489 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
490 !uid_eq(cred->euid, tcred->uid) &&
491 !uid_eq(cred->euid, tcred->suid))
498 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
499 struct kernfs_open_file *of)
501 struct task_struct *tsk;
506 tsk = find_task_by_vpid(pid);
515 get_task_struct(tsk);
518 ret = rdtgroup_task_write_permission(tsk, of);
520 ret = __rdtgroup_move_task(tsk, rdtgrp);
522 put_task_struct(tsk);
526 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
527 char *buf, size_t nbytes, loff_t off)
529 struct rdtgroup *rdtgrp;
533 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
535 rdtgrp = rdtgroup_kn_lock_live(of->kn);
538 ret = rdtgroup_move_task(pid, rdtgrp, of);
542 rdtgroup_kn_unlock(of->kn);
544 return ret ?: nbytes;
547 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
549 struct task_struct *p, *t;
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);
560 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
561 struct seq_file *s, void *v)
563 struct rdtgroup *rdtgrp;
566 rdtgrp = rdtgroup_kn_lock_live(of->kn);
568 show_rdt_tasks(rdtgrp, s);
571 rdtgroup_kn_unlock(of->kn);
576 static int rdt_num_closids_show(struct kernfs_open_file *of,
577 struct seq_file *seq, void *v)
579 struct rdt_resource *r = of->kn->parent->priv;
581 seq_printf(seq, "%d\n", r->num_closid);
585 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
586 struct seq_file *seq, void *v)
588 struct rdt_resource *r = of->kn->parent->priv;
590 seq_printf(seq, "%x\n", r->default_ctrl);
594 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
595 struct seq_file *seq, void *v)
597 struct rdt_resource *r = of->kn->parent->priv;
599 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
603 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
604 struct seq_file *seq, void *v)
606 struct rdt_resource *r = of->kn->parent->priv;
608 seq_printf(seq, "%x\n", r->cache.shareable_bits);
612 static int rdt_min_bw_show(struct kernfs_open_file *of,
613 struct seq_file *seq, void *v)
615 struct rdt_resource *r = of->kn->parent->priv;
617 seq_printf(seq, "%u\n", r->membw.min_bw);
621 static int rdt_num_rmids_show(struct kernfs_open_file *of,
622 struct seq_file *seq, void *v)
624 struct rdt_resource *r = of->kn->parent->priv;
626 seq_printf(seq, "%d\n", r->num_rmid);
631 static int rdt_mon_features_show(struct kernfs_open_file *of,
632 struct seq_file *seq, void *v)
634 struct rdt_resource *r = of->kn->parent->priv;
635 struct mon_evt *mevt;
637 list_for_each_entry(mevt, &r->evt_list, list)
638 seq_printf(seq, "%s\n", mevt->name);
643 static int rdt_bw_gran_show(struct kernfs_open_file *of,
644 struct seq_file *seq, void *v)
646 struct rdt_resource *r = of->kn->parent->priv;
648 seq_printf(seq, "%u\n", r->membw.bw_gran);
652 static int rdt_delay_linear_show(struct kernfs_open_file *of,
653 struct seq_file *seq, void *v)
655 struct rdt_resource *r = of->kn->parent->priv;
657 seq_printf(seq, "%u\n", r->membw.delay_linear);
661 static int max_threshold_occ_show(struct kernfs_open_file *of,
662 struct seq_file *seq, void *v)
664 struct rdt_resource *r = of->kn->parent->priv;
666 seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale);
671 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
672 char *buf, size_t nbytes, loff_t off)
674 struct rdt_resource *r = of->kn->parent->priv;
678 ret = kstrtouint(buf, 0, &bytes);
682 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
685 intel_cqm_threshold = bytes / r->mon_scale;
690 /* rdtgroup information files for one cache resource. */
691 static struct rftype res_common_files[] = {
693 .name = "num_closids",
695 .kf_ops = &rdtgroup_kf_single_ops,
696 .seq_show = rdt_num_closids_show,
697 .fflags = RF_CTRL_INFO,
700 .name = "mon_features",
702 .kf_ops = &rdtgroup_kf_single_ops,
703 .seq_show = rdt_mon_features_show,
704 .fflags = RF_MON_INFO,
709 .kf_ops = &rdtgroup_kf_single_ops,
710 .seq_show = rdt_num_rmids_show,
711 .fflags = RF_MON_INFO,
716 .kf_ops = &rdtgroup_kf_single_ops,
717 .seq_show = rdt_default_ctrl_show,
718 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
721 .name = "min_cbm_bits",
723 .kf_ops = &rdtgroup_kf_single_ops,
724 .seq_show = rdt_min_cbm_bits_show,
725 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
728 .name = "shareable_bits",
730 .kf_ops = &rdtgroup_kf_single_ops,
731 .seq_show = rdt_shareable_bits_show,
732 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
735 .name = "min_bandwidth",
737 .kf_ops = &rdtgroup_kf_single_ops,
738 .seq_show = rdt_min_bw_show,
739 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
742 .name = "bandwidth_gran",
744 .kf_ops = &rdtgroup_kf_single_ops,
745 .seq_show = rdt_bw_gran_show,
746 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
749 .name = "delay_linear",
751 .kf_ops = &rdtgroup_kf_single_ops,
752 .seq_show = rdt_delay_linear_show,
753 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
756 .name = "max_threshold_occupancy",
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,
766 .kf_ops = &rdtgroup_kf_single_ops,
767 .write = rdtgroup_cpus_write,
768 .seq_show = rdtgroup_cpus_show,
769 .fflags = RFTYPE_BASE,
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,
783 .kf_ops = &rdtgroup_kf_single_ops,
784 .write = rdtgroup_tasks_write,
785 .seq_show = rdtgroup_tasks_show,
786 .fflags = RFTYPE_BASE,
791 .kf_ops = &rdtgroup_kf_single_ops,
792 .write = rdtgroup_schemata_write,
793 .seq_show = rdtgroup_schemata_show,
794 .fflags = RF_CTRL_BASE,
798 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
800 struct rftype *rfts, *rft;
803 rfts = res_common_files;
804 len = ARRAY_SIZE(res_common_files);
806 lockdep_assert_held(&rdtgroup_mutex);
808 for (rft = rfts; rft < rfts + len; rft++) {
809 if ((fflags & rft->fflags) == rft->fflags) {
810 ret = rdtgroup_add_file(kn, rft);
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);
826 static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
827 unsigned long fflags)
829 struct kernfs_node *kn_subdir;
832 kn_subdir = kernfs_create_dir(kn_info, name,
834 if (IS_ERR(kn_subdir))
835 return PTR_ERR(kn_subdir);
837 ret = rdtgroup_kn_set_ugid(kn_subdir);
841 ret = rdtgroup_add_files(kn_subdir, fflags);
843 kernfs_activate(kn_subdir);
848 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
850 struct rdt_resource *r;
851 unsigned long fflags;
855 /* create the directory */
856 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
858 return PTR_ERR(kn_info);
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);
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);
875 ret = rdtgroup_kn_set_ugid(kn_info);
879 kernfs_activate(kn_info);
884 kernfs_remove(kn_info);
889 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
890 char *name, struct kernfs_node **dest_kn)
892 struct kernfs_node *kn;
895 /* create the directory */
896 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
903 ret = rdtgroup_kn_set_ugid(kn);
916 static void l3_qos_cfg_update(void *arg)
920 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
923 static void l2_qos_cfg_update(void *arg)
927 wrmsrl(IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
930 static int set_cache_qos_cfg(int level, bool enable)
932 void (*update)(void *arg);
933 struct rdt_resource *r_l;
934 cpumask_var_t cpu_mask;
935 struct rdt_domain *d;
938 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
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;
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);
954 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
955 if (cpumask_test_cpu(cpu, cpu_mask))
957 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
958 smp_call_function_many(cpu_mask, update, &enable, 1);
961 free_cpumask_var(cpu_mask);
966 static int cdp_enable(int level, int data_type, int code_type)
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];
973 if (!r_l->alloc_capable || !r_ldata->alloc_capable ||
974 !r_lcode->alloc_capable)
977 ret = set_cache_qos_cfg(level, true);
979 r_l->alloc_enabled = false;
980 r_ldata->alloc_enabled = true;
981 r_lcode->alloc_enabled = true;
986 static int cdpl3_enable(void)
988 return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA,
989 RDT_RESOURCE_L3CODE);
992 static int cdpl2_enable(void)
994 return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA,
995 RDT_RESOURCE_L2CODE);
998 static void cdp_disable(int level, int data_type, int code_type)
1000 struct rdt_resource *r = &rdt_resources_all[level];
1002 r->alloc_enabled = r->alloc_capable;
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);
1011 static void cdpl3_disable(void)
1013 cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE);
1016 static void cdpl2_disable(void)
1018 cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE);
1021 static void cdp_disable_all(void)
1023 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
1025 if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
1029 static int parse_rdtgroupfs_options(char *data)
1031 char *token, *o = data;
1034 while ((token = strsep(&o, ",")) != NULL) {
1040 if (!strcmp(token, "cdp")) {
1041 ret = cdpl3_enable();
1044 } else if (!strcmp(token, "cdpl2")) {
1045 ret = cdpl2_enable();
1057 pr_err("Invalid mount option \"%s\"\n", token);
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.
1070 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1072 if (kernfs_type(kn) == KERNFS_DIR) {
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.
1079 if (kn == kn_info || kn->parent == kn_info)
1084 return kn->parent->priv;
1088 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1090 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1095 atomic_inc(&rdtgrp->waitcount);
1096 kernfs_break_active_protection(kn);
1098 mutex_lock(&rdtgroup_mutex);
1100 /* Was this group deleted while we waited? */
1101 if (rdtgrp->flags & RDT_DELETED)
1107 void rdtgroup_kn_unlock(struct kernfs_node *kn)
1109 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1114 mutex_unlock(&rdtgroup_mutex);
1116 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
1117 (rdtgrp->flags & RDT_DELETED)) {
1118 kernfs_unbreak_active_protection(kn);
1119 rdtgroup_remove(rdtgrp);
1121 kernfs_unbreak_active_protection(kn);
1125 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1126 struct rdtgroup *prgrp,
1127 struct kernfs_node **mon_data_kn);
1129 static struct dentry *rdt_mount(struct file_system_type *fs_type,
1130 int flags, const char *unused_dev_name,
1133 struct rdt_domain *dom;
1134 struct rdt_resource *r;
1135 struct dentry *dentry;
1139 mutex_lock(&rdtgroup_mutex);
1141 * resctrl file system can only be mounted once.
1143 if (static_branch_unlikely(&rdt_enable_key)) {
1144 dentry = ERR_PTR(-EBUSY);
1148 ret = parse_rdtgroupfs_options(data);
1150 dentry = ERR_PTR(ret);
1156 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
1158 dentry = ERR_PTR(ret);
1162 if (rdt_mon_capable) {
1163 ret = mongroup_create_dir(rdtgroup_default.kn,
1164 &rdtgroup_default, "mon_groups",
1167 dentry = ERR_PTR(ret);
1171 ret = mkdir_mondata_all(rdtgroup_default.kn,
1172 &rdtgroup_default, &kn_mondata);
1174 dentry = ERR_PTR(ret);
1177 rdtgroup_default.mon.mon_data_kn = kn_mondata;
1180 dentry = kernfs_mount(fs_type, flags, rdt_root,
1181 RDTGROUP_SUPER_MAGIC, NULL);
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);
1190 if (rdt_alloc_capable || rdt_mon_capable)
1191 static_branch_enable_cpuslocked(&rdt_enable_key);
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);
1202 if (rdt_mon_capable)
1203 kernfs_remove(kn_mondata);
1205 if (rdt_mon_capable)
1206 kernfs_remove(kn_mongrp);
1208 kernfs_remove(kn_info);
1212 mutex_unlock(&rdtgroup_mutex);
1218 static int reset_all_ctrls(struct rdt_resource *r)
1220 struct msr_param msr_param;
1221 cpumask_var_t cpu_mask;
1222 struct rdt_domain *d;
1225 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1230 msr_param.high = r->num_closid;
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.
1237 list_for_each_entry(d, &r->domains, list) {
1238 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1240 for (i = 0; i < r->num_closid; i++)
1241 d->ctrl_val[i] = r->default_ctrl;
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);
1251 free_cpumask_var(cpu_mask);
1256 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
1258 return (rdt_alloc_capable &&
1259 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
1262 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
1264 return (rdt_mon_capable &&
1265 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
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).
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
1276 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
1277 struct cpumask *mask)
1279 struct task_struct *p, *t;
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;
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.
1298 if (mask && t->on_cpu)
1299 cpumask_set_cpu(task_cpu(t), mask);
1303 read_unlock(&tasklist_lock);
1306 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
1308 struct rdtgroup *sentry, *stmp;
1309 struct list_head *head;
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);
1316 if (atomic_read(&sentry->waitcount) != 0)
1317 sentry->flags = RDT_DELETED;
1319 rdtgroup_remove(sentry);
1324 * Forcibly remove all of subdirectories under root.
1326 static void rmdir_all_sub(void)
1328 struct rdtgroup *rdtgrp, *tmp;
1330 /* Move all tasks to the default resource group */
1331 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
1333 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
1334 /* Free any child rmids */
1335 free_all_child_rdtgrp(rdtgrp);
1337 /* Remove each rdtgroup other than root */
1338 if (rdtgrp == &rdtgroup_default)
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.
1346 cpumask_or(&rdtgroup_default.cpu_mask,
1347 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1349 free_rmid(rdtgrp->mon.rmid);
1351 kernfs_remove(rdtgrp->kn);
1352 list_del(&rdtgrp->rdtgroup_list);
1354 if (atomic_read(&rdtgrp->waitcount) != 0)
1355 rdtgrp->flags = RDT_DELETED;
1357 rdtgroup_remove(rdtgrp);
1359 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
1360 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
1362 kernfs_remove(kn_info);
1363 kernfs_remove(kn_mongrp);
1364 kernfs_remove(kn_mondata);
1367 static void rdt_kill_sb(struct super_block *sb)
1369 struct rdt_resource *r;
1372 mutex_lock(&rdtgroup_mutex);
1374 /*Put everything back to default values. */
1375 for_each_alloc_enabled_rdt_resource(r)
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);
1383 mutex_unlock(&rdtgroup_mutex);
1387 static struct file_system_type rdt_fs_type = {
1390 .kill_sb = rdt_kill_sb,
1393 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
1396 struct kernfs_node *kn;
1399 kn = __kernfs_create_file(parent_kn, name, 0444, 0,
1400 &kf_mondata_ops, priv, NULL, NULL);
1404 ret = rdtgroup_kn_set_ugid(kn);
1414 * Remove all subdirectories of mon_data of ctrl_mon groups
1415 * and monitor groups with given domain id.
1417 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
1419 struct rdtgroup *prgrp, *crgrp;
1422 if (!r->mon_enabled)
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);
1429 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
1430 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
1434 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
1435 struct rdt_domain *d,
1436 struct rdt_resource *r, struct rdtgroup *prgrp)
1438 union mon_data_bits priv;
1439 struct kernfs_node *kn;
1440 struct mon_evt *mevt;
1441 struct rmid_read rr;
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);
1451 ret = rdtgroup_kn_set_ugid(kn);
1455 if (WARN_ON(list_empty(&r->evt_list))) {
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);
1468 if (is_mbm_event(mevt->evtid))
1469 mon_event_read(&rr, d, prgrp, mevt->evtid, true);
1471 kernfs_activate(kn);
1480 * Add all subdirectories of mon_data for "ctrl_mon" groups
1481 * and "monitor" groups with given domain id.
1483 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
1484 struct rdt_domain *d)
1486 struct kernfs_node *parent_kn;
1487 struct rdtgroup *prgrp, *crgrp;
1488 struct list_head *head;
1490 if (!r->mon_enabled)
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);
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);
1505 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
1506 struct rdt_resource *r,
1507 struct rdtgroup *prgrp)
1509 struct rdt_domain *dom;
1512 list_for_each_entry(dom, &r->domains, list) {
1513 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
1522 * This creates a directory mon_data which contains the monitored data.
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:
1533 * Each domain directory has one file per event:
1538 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1539 struct rdtgroup *prgrp,
1540 struct kernfs_node **dest_kn)
1542 struct rdt_resource *r;
1543 struct kernfs_node *kn;
1547 * Create the mon_data directory first.
1549 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
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
1560 for_each_mon_enabled_rdt_resource(r) {
1561 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
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)
1578 struct rdtgroup *prdtgrp, *rdtgrp;
1579 struct kernfs_node *kn;
1583 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
1589 /* allocate the rdtgroup. */
1590 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
1596 rdtgrp->mon.parent = prdtgrp;
1597 rdtgrp->type = rtype;
1598 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
1600 /* kernfs creates the directory for rdtgrp */
1601 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
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().
1616 ret = rdtgroup_kn_set_ugid(kn);
1620 files = RFTYPE_BASE | RFTYPE_CTRL;
1621 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
1622 ret = rdtgroup_add_files(kn, files);
1626 if (rdt_mon_capable) {
1630 rdtgrp->mon.rmid = ret;
1632 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
1636 kernfs_activate(kn);
1639 * The caller unlocks the parent_kn upon success.
1644 free_rmid(rdtgrp->mon.rmid);
1646 kernfs_put(rdtgrp->kn);
1647 kernfs_remove(rdtgrp->kn);
1651 rdtgroup_kn_unlock(parent_kn);
1655 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
1657 kernfs_remove(rgrp->kn);
1658 free_rmid(rgrp->mon.rmid);
1659 rdtgroup_remove(rgrp);
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.
1667 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
1668 struct kernfs_node *prgrp_kn,
1672 struct rdtgroup *rdtgrp, *prgrp;
1675 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP,
1680 prgrp = rdtgrp->mon.parent;
1681 rdtgrp->closid = prgrp->closid;
1684 * Add the rdtgrp to the list of rdtgrps the parent
1685 * ctrl_mon group has to track.
1687 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
1689 rdtgroup_kn_unlock(parent_kn);
1694 * These are rdtgroups created under the root directory. Can be used
1695 * to allocate and monitor resources.
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)
1701 struct rdtgroup *rdtgrp;
1702 struct kernfs_node *kn;
1706 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP,
1712 ret = closid_alloc();
1714 goto out_common_fail;
1718 rdtgrp->closid = closid;
1719 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
1721 if (rdt_mon_capable) {
1723 * Create an empty mon_groups directory to hold the subset
1724 * of tasks and cpus to monitor.
1726 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
1734 closid_free(closid);
1735 list_del(&rdtgrp->rdtgroup_list);
1737 mkdir_rdt_prepare_clean(rdtgrp);
1739 rdtgroup_kn_unlock(parent_kn);
1743 /* Restore the qos cfg state when a domain comes online */
1744 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
1746 if (!r->alloc_capable)
1749 if (r == &rdt_resources_all[RDT_RESOURCE_L2DATA])
1750 l2_qos_cfg_update(&r->alloc_enabled);
1752 if (r == &rdt_resources_all[RDT_RESOURCE_L3DATA])
1753 l3_qos_cfg_update(&r->alloc_enabled);
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.
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
1766 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
1768 return (!strcmp(kn->name, "mon_groups") &&
1769 strcmp(name, "mon_groups"));
1772 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
1775 /* Do not accept '\n' to avoid unparsable situation. */
1776 if (strchr(name, '\n'))
1780 * If the parent directory is the root directory and RDT
1781 * allocation is supported, add a control and monitoring
1784 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
1785 return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode);
1788 * If RDT monitoring is supported and the parent directory is a valid
1789 * "mon_groups" directory, add a monitoring subdirectory.
1791 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
1792 return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode);
1797 static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
1798 cpumask_var_t tmpmask)
1800 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
1803 /* Give any tasks back to the parent group */
1804 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
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;
1810 * Update the MSR on moved CPUs and CPUs which have moved
1811 * task running on them.
1813 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1814 update_closid_rmid(tmpmask, NULL);
1816 rdtgrp->flags = RDT_DELETED;
1817 free_rmid(rdtgrp->mon.rmid);
1820 * Remove the rdtgrp from the parent ctrl_mon group's list
1822 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
1823 list_del(&rdtgrp->mon.crdtgrp_list);
1825 kernfs_remove(rdtgrp->kn);
1830 static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
1831 cpumask_var_t tmpmask)
1835 /* Give any tasks back to the default group */
1836 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
1838 /* Give any CPUs back to the default group */
1839 cpumask_or(&rdtgroup_default.cpu_mask,
1840 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
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;
1849 * Update the MSR on moved CPUs and CPUs which have moved
1850 * task running on them.
1852 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1853 update_closid_rmid(tmpmask, NULL);
1855 rdtgrp->flags = RDT_DELETED;
1856 closid_free(rdtgrp->closid);
1857 free_rmid(rdtgrp->mon.rmid);
1859 list_del(&rdtgrp->rdtgroup_list);
1861 kernfs_remove(rdtgrp->kn);
1864 * Free all the child monitor group rmids.
1866 free_all_child_rdtgrp(rdtgrp);
1871 static int rdtgroup_rmdir(struct kernfs_node *kn)
1873 struct kernfs_node *parent_kn = kn->parent;
1874 struct rdtgroup *rdtgrp;
1875 cpumask_var_t tmpmask;
1878 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1881 rdtgrp = rdtgroup_kn_lock_live(kn);
1888 * If the rdtgroup is a ctrl_mon group and parent directory
1889 * is the root directory, remove the ctrl_mon group.
1891 * If the rdtgroup is a mon group and parent directory
1892 * is a valid "mon_groups" directory, remove the mon group.
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);
1904 rdtgroup_kn_unlock(kn);
1905 free_cpumask_var(tmpmask);
1909 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1911 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
1912 seq_puts(seq, ",cdp");
1916 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
1917 .mkdir = rdtgroup_mkdir,
1918 .rmdir = rdtgroup_rmdir,
1919 .show_options = rdtgroup_show_options,
1922 static int __init rdtgroup_setup_root(void)
1926 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1927 KERNFS_ROOT_CREATE_DEACTIVATED,
1929 if (IS_ERR(rdt_root))
1930 return PTR_ERR(rdt_root);
1932 mutex_lock(&rdtgroup_mutex);
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);
1939 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1941 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
1943 kernfs_destroy_root(rdt_root);
1947 rdtgroup_default.kn = rdt_root->kn;
1948 kernfs_activate(rdtgroup_default.kn);
1951 mutex_unlock(&rdtgroup_mutex);
1957 * rdtgroup_init - rdtgroup initialization
1959 * Setup resctrl file system including set up root, create mount point,
1960 * register rdtgroup filesystem, and initialize files under root directory.
1962 * Return: 0 on success or -errno
1964 int __init rdtgroup_init(void)
1968 ret = rdtgroup_setup_root();
1972 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1976 ret = register_filesystem(&rdt_fs_type);
1978 goto cleanup_mountpoint;
1983 sysfs_remove_mount_point(fs_kobj, "resctrl");
1985 kernfs_destroy_root(rdt_root);