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.h>
36 #include <asm/intel_rdt_common.h>
38 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
39 struct kernfs_root *rdt_root;
40 struct rdtgroup rdtgroup_default;
41 LIST_HEAD(rdt_all_groups);
43 /* Kernel fs node for "info" directory under root */
44 static struct kernfs_node *kn_info;
47 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
48 * we can keep a bitmap of free CLOSIDs in a single integer.
50 * Using a global CLOSID across all resources has some advantages and
52 * + We can simply set "current->closid" to assign a task to a resource
54 * + Context switch code can avoid extra memory references deciding which
55 * CLOSID to load into the PQR_ASSOC MSR
56 * - We give up some options in configuring resource groups across multi-socket
58 * - Our choices on how to configure each resource become progressively more
59 * limited as the number of resources grows.
61 static int closid_free_map;
63 static void closid_init(void)
65 struct rdt_resource *r;
66 int rdt_min_closid = 32;
68 /* Compute rdt_min_closid across all resources */
69 for_each_enabled_rdt_resource(r)
70 rdt_min_closid = min(rdt_min_closid, r->num_closid);
72 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
74 /* CLOSID 0 is always reserved for the default group */
75 closid_free_map &= ~1;
78 int closid_alloc(void)
80 int closid = ffs(closid_free_map);
85 closid_free_map &= ~(1 << closid);
90 static void closid_free(int closid)
92 closid_free_map |= 1 << closid;
95 /* set uid and gid of rdtgroup dirs and files to that of the creator */
96 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
98 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
99 .ia_uid = current_fsuid(),
100 .ia_gid = current_fsgid(), };
102 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
103 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
106 return kernfs_setattr(kn, &iattr);
109 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
111 struct kernfs_node *kn;
114 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
115 0, rft->kf_ops, rft, NULL, NULL);
119 ret = rdtgroup_kn_set_ugid(kn);
128 static int rdtgroup_add_files(struct kernfs_node *kn, struct rftype *rfts,
134 lockdep_assert_held(&rdtgroup_mutex);
136 for (rft = rfts; rft < rfts + len; rft++) {
137 ret = rdtgroup_add_file(kn, rft);
144 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
145 while (--rft >= rfts)
146 kernfs_remove_by_name(kn, rft->name);
150 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
152 struct kernfs_open_file *of = m->private;
153 struct rftype *rft = of->kn->priv;
156 return rft->seq_show(of, m, arg);
160 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
161 size_t nbytes, loff_t off)
163 struct rftype *rft = of->kn->priv;
166 return rft->write(of, buf, nbytes, off);
171 static struct kernfs_ops rdtgroup_kf_single_ops = {
172 .atomic_write_len = PAGE_SIZE,
173 .write = rdtgroup_file_write,
174 .seq_show = rdtgroup_seqfile_show,
177 static bool is_cpu_list(struct kernfs_open_file *of)
179 struct rftype *rft = of->kn->priv;
181 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
184 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
185 struct seq_file *s, void *v)
187 struct rdtgroup *rdtgrp;
190 rdtgrp = rdtgroup_kn_lock_live(of->kn);
193 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
194 cpumask_pr_args(&rdtgrp->cpu_mask));
198 rdtgroup_kn_unlock(of->kn);
204 * This is safe against intel_rdt_sched_in() called from __switch_to()
205 * because __switch_to() is executed with interrupts disabled. A local call
206 * from rdt_update_closid() is proteced against __switch_to() because
207 * preemption is disabled.
209 static void rdt_update_cpu_closid(void *closid)
212 this_cpu_write(cpu_closid, *(int *)closid);
214 * We cannot unconditionally write the MSR because the current
215 * executing task might have its own closid selected. Just reuse
216 * the context switch code.
218 intel_rdt_sched_in();
222 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
224 * Per task closids must have been set up before calling this function.
226 * The per cpu closids are updated with the smp function call, when @closid
227 * is not NULL. If @closid is NULL then all affected percpu closids must
228 * have been set up before calling this function.
231 rdt_update_closid(const struct cpumask *cpu_mask, int *closid)
235 if (cpumask_test_cpu(cpu, cpu_mask))
236 rdt_update_cpu_closid(closid);
237 smp_call_function_many(cpu_mask, rdt_update_cpu_closid, closid, 1);
241 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
242 char *buf, size_t nbytes, loff_t off)
244 cpumask_var_t tmpmask, newmask;
245 struct rdtgroup *rdtgrp, *r;
251 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
253 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
254 free_cpumask_var(tmpmask);
258 rdtgrp = rdtgroup_kn_lock_live(of->kn);
265 ret = cpulist_parse(buf, newmask);
267 ret = cpumask_parse(buf, newmask);
272 /* check that user didn't specify any offline cpus */
273 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
274 if (cpumask_weight(tmpmask)) {
279 /* Check whether cpus are dropped from this group */
280 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
281 if (cpumask_weight(tmpmask)) {
282 /* Can't drop from default group */
283 if (rdtgrp == &rdtgroup_default) {
287 /* Give any dropped cpus to rdtgroup_default */
288 cpumask_or(&rdtgroup_default.cpu_mask,
289 &rdtgroup_default.cpu_mask, tmpmask);
290 rdt_update_closid(tmpmask, &rdtgroup_default.closid);
294 * If we added cpus, remove them from previous group that owned them
295 * and update per-cpu closid
297 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
298 if (cpumask_weight(tmpmask)) {
299 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
302 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, tmpmask);
304 rdt_update_closid(tmpmask, &rdtgrp->closid);
307 /* Done pushing/pulling - update this group with new mask */
308 cpumask_copy(&rdtgrp->cpu_mask, newmask);
311 rdtgroup_kn_unlock(of->kn);
312 free_cpumask_var(tmpmask);
313 free_cpumask_var(newmask);
315 return ret ?: nbytes;
318 struct task_move_callback {
319 struct callback_head work;
320 struct rdtgroup *rdtgrp;
323 static void move_myself(struct callback_head *head)
325 struct task_move_callback *callback;
326 struct rdtgroup *rdtgrp;
328 callback = container_of(head, struct task_move_callback, work);
329 rdtgrp = callback->rdtgrp;
332 * If resource group was deleted before this task work callback
333 * was invoked, then assign the task to root group and free the
336 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
337 (rdtgrp->flags & RDT_DELETED)) {
343 /* update PQR_ASSOC MSR to make resource group go into effect */
344 intel_rdt_sched_in();
350 static int __rdtgroup_move_task(struct task_struct *tsk,
351 struct rdtgroup *rdtgrp)
353 struct task_move_callback *callback;
356 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
359 callback->work.func = move_myself;
360 callback->rdtgrp = rdtgrp;
363 * Take a refcount, so rdtgrp cannot be freed before the
364 * callback has been invoked.
366 atomic_inc(&rdtgrp->waitcount);
367 ret = task_work_add(tsk, &callback->work, true);
370 * Task is exiting. Drop the refcount and free the callback.
371 * No need to check the refcount as the group cannot be
372 * deleted before the write function unlocks rdtgroup_mutex.
374 atomic_dec(&rdtgrp->waitcount);
377 tsk->closid = rdtgrp->closid;
382 static int rdtgroup_task_write_permission(struct task_struct *task,
383 struct kernfs_open_file *of)
385 const struct cred *tcred = get_task_cred(task);
386 const struct cred *cred = current_cred();
390 * Even if we're attaching all tasks in the thread group, we only
391 * need to check permissions on one of them.
393 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
394 !uid_eq(cred->euid, tcred->uid) &&
395 !uid_eq(cred->euid, tcred->suid))
402 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
403 struct kernfs_open_file *of)
405 struct task_struct *tsk;
410 tsk = find_task_by_vpid(pid);
419 get_task_struct(tsk);
422 ret = rdtgroup_task_write_permission(tsk, of);
424 ret = __rdtgroup_move_task(tsk, rdtgrp);
426 put_task_struct(tsk);
430 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
431 char *buf, size_t nbytes, loff_t off)
433 struct rdtgroup *rdtgrp;
437 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
439 rdtgrp = rdtgroup_kn_lock_live(of->kn);
442 ret = rdtgroup_move_task(pid, rdtgrp, of);
446 rdtgroup_kn_unlock(of->kn);
448 return ret ?: nbytes;
451 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
453 struct task_struct *p, *t;
456 for_each_process_thread(p, t) {
457 if (t->closid == r->closid)
458 seq_printf(s, "%d\n", t->pid);
463 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
464 struct seq_file *s, void *v)
466 struct rdtgroup *rdtgrp;
469 rdtgrp = rdtgroup_kn_lock_live(of->kn);
471 show_rdt_tasks(rdtgrp, s);
474 rdtgroup_kn_unlock(of->kn);
479 /* Files in each rdtgroup */
480 static struct rftype rdtgroup_base_files[] = {
484 .kf_ops = &rdtgroup_kf_single_ops,
485 .write = rdtgroup_cpus_write,
486 .seq_show = rdtgroup_cpus_show,
491 .kf_ops = &rdtgroup_kf_single_ops,
492 .write = rdtgroup_cpus_write,
493 .seq_show = rdtgroup_cpus_show,
494 .flags = RFTYPE_FLAGS_CPUS_LIST,
499 .kf_ops = &rdtgroup_kf_single_ops,
500 .write = rdtgroup_tasks_write,
501 .seq_show = rdtgroup_tasks_show,
506 .kf_ops = &rdtgroup_kf_single_ops,
507 .write = rdtgroup_schemata_write,
508 .seq_show = rdtgroup_schemata_show,
512 static int rdt_num_closids_show(struct kernfs_open_file *of,
513 struct seq_file *seq, void *v)
515 struct rdt_resource *r = of->kn->parent->priv;
517 seq_printf(seq, "%d\n", r->num_closid);
522 static int rdt_cbm_mask_show(struct kernfs_open_file *of,
523 struct seq_file *seq, void *v)
525 struct rdt_resource *r = of->kn->parent->priv;
527 seq_printf(seq, "%x\n", r->max_cbm);
532 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
533 struct seq_file *seq, void *v)
535 struct rdt_resource *r = of->kn->parent->priv;
537 seq_printf(seq, "%d\n", r->min_cbm_bits);
542 /* rdtgroup information files for one cache resource. */
543 static struct rftype res_info_files[] = {
545 .name = "num_closids",
547 .kf_ops = &rdtgroup_kf_single_ops,
548 .seq_show = rdt_num_closids_show,
553 .kf_ops = &rdtgroup_kf_single_ops,
554 .seq_show = rdt_cbm_mask_show,
557 .name = "min_cbm_bits",
559 .kf_ops = &rdtgroup_kf_single_ops,
560 .seq_show = rdt_min_cbm_bits_show,
564 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
566 struct kernfs_node *kn_subdir;
567 struct rdt_resource *r;
570 /* create the directory */
571 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
573 return PTR_ERR(kn_info);
576 for_each_enabled_rdt_resource(r) {
577 kn_subdir = kernfs_create_dir(kn_info, r->name,
579 if (IS_ERR(kn_subdir)) {
580 ret = PTR_ERR(kn_subdir);
583 kernfs_get(kn_subdir);
584 ret = rdtgroup_kn_set_ugid(kn_subdir);
587 ret = rdtgroup_add_files(kn_subdir, res_info_files,
588 ARRAY_SIZE(res_info_files));
591 kernfs_activate(kn_subdir);
595 * This extra ref will be put in kernfs_remove() and guarantees
596 * that @rdtgrp->kn is always accessible.
600 ret = rdtgroup_kn_set_ugid(kn_info);
604 kernfs_activate(kn_info);
609 kernfs_remove(kn_info);
613 static void l3_qos_cfg_update(void *arg)
617 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
620 static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
622 cpumask_var_t cpu_mask;
623 struct rdt_domain *d;
626 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
629 list_for_each_entry(d, &r->domains, list) {
630 /* Pick one CPU from each domain instance to update MSR */
631 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
634 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
635 if (cpumask_test_cpu(cpu, cpu_mask))
636 l3_qos_cfg_update(&enable);
637 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
638 smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
641 free_cpumask_var(cpu_mask);
646 static int cdp_enable(void)
648 struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
649 struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
650 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
653 if (!r_l3->capable || !r_l3data->capable || !r_l3code->capable)
656 ret = set_l3_qos_cfg(r_l3, true);
658 r_l3->enabled = false;
659 r_l3data->enabled = true;
660 r_l3code->enabled = true;
665 static void cdp_disable(void)
667 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
669 r->enabled = r->capable;
671 if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled) {
672 rdt_resources_all[RDT_RESOURCE_L3DATA].enabled = false;
673 rdt_resources_all[RDT_RESOURCE_L3CODE].enabled = false;
674 set_l3_qos_cfg(r, false);
678 static int parse_rdtgroupfs_options(char *data)
680 char *token, *o = data;
683 while ((token = strsep(&o, ",")) != NULL) {
687 if (!strcmp(token, "cdp"))
695 * We don't allow rdtgroup directories to be created anywhere
696 * except the root directory. Thus when looking for the rdtgroup
697 * structure for a kernfs node we are either looking at a directory,
698 * in which case the rdtgroup structure is pointed at by the "priv"
699 * field, otherwise we have a file, and need only look to the parent
700 * to find the rdtgroup.
702 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
704 if (kernfs_type(kn) == KERNFS_DIR) {
706 * All the resource directories use "kn->priv"
707 * to point to the "struct rdtgroup" for the
708 * resource. "info" and its subdirectories don't
709 * have rdtgroup structures, so return NULL here.
711 if (kn == kn_info || kn->parent == kn_info)
716 return kn->parent->priv;
720 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
722 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
727 atomic_inc(&rdtgrp->waitcount);
728 kernfs_break_active_protection(kn);
730 mutex_lock(&rdtgroup_mutex);
732 /* Was this group deleted while we waited? */
733 if (rdtgrp->flags & RDT_DELETED)
739 void rdtgroup_kn_unlock(struct kernfs_node *kn)
741 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
746 mutex_unlock(&rdtgroup_mutex);
748 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
749 (rdtgrp->flags & RDT_DELETED)) {
750 kernfs_unbreak_active_protection(kn);
751 kernfs_put(rdtgrp->kn);
754 kernfs_unbreak_active_protection(kn);
758 static struct dentry *rdt_mount(struct file_system_type *fs_type,
759 int flags, const char *unused_dev_name,
762 struct dentry *dentry;
765 mutex_lock(&rdtgroup_mutex);
767 * resctrl file system can only be mounted once.
769 if (static_branch_unlikely(&rdt_enable_key)) {
770 dentry = ERR_PTR(-EBUSY);
774 ret = parse_rdtgroupfs_options(data);
776 dentry = ERR_PTR(ret);
782 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
784 dentry = ERR_PTR(ret);
788 dentry = kernfs_mount(fs_type, flags, rdt_root,
789 RDTGROUP_SUPER_MAGIC, NULL);
793 static_branch_enable(&rdt_enable_key);
799 mutex_unlock(&rdtgroup_mutex);
804 static int reset_all_cbms(struct rdt_resource *r)
806 struct msr_param msr_param;
807 cpumask_var_t cpu_mask;
808 struct rdt_domain *d;
811 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
816 msr_param.high = r->num_closid;
819 * Disable resource control for this resource by setting all
820 * CBMs in all domains to the maximum mask value. Pick one CPU
821 * from each domain to update the MSRs below.
823 list_for_each_entry(d, &r->domains, list) {
824 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
826 for (i = 0; i < r->num_closid; i++)
827 d->cbm[i] = r->max_cbm;
830 /* Update CBM on this cpu if it's in cpu_mask. */
831 if (cpumask_test_cpu(cpu, cpu_mask))
832 rdt_cbm_update(&msr_param);
833 /* Update CBM on all other cpus in cpu_mask. */
834 smp_call_function_many(cpu_mask, rdt_cbm_update, &msr_param, 1);
837 free_cpumask_var(cpu_mask);
843 * Move tasks from one to the other group. If @from is NULL, then all tasks
844 * in the systems are moved unconditionally (used for teardown).
846 * If @mask is not NULL the cpus on which moved tasks are running are set
847 * in that mask so the update smp function call is restricted to affected
850 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
851 struct cpumask *mask)
853 struct task_struct *p, *t;
855 read_lock(&tasklist_lock);
856 for_each_process_thread(p, t) {
857 if (!from || t->closid == from->closid) {
858 t->closid = to->closid;
861 * This is safe on x86 w/o barriers as the ordering
862 * of writing to task_cpu() and t->on_cpu is
863 * reverse to the reading here. The detection is
864 * inaccurate as tasks might move or schedule
865 * before the smp function call takes place. In
866 * such a case the function call is pointless, but
867 * there is no other side effect.
869 if (mask && t->on_cpu)
870 cpumask_set_cpu(task_cpu(t), mask);
874 read_unlock(&tasklist_lock);
878 * Forcibly remove all of subdirectories under root.
880 static void rmdir_all_sub(void)
882 struct rdtgroup *rdtgrp, *tmp;
884 /* Move all tasks to the default resource group */
885 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
887 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
888 /* Remove each rdtgroup other than root */
889 if (rdtgrp == &rdtgroup_default)
893 * Give any CPUs back to the default group. We cannot copy
894 * cpu_online_mask because a CPU might have executed the
895 * offline callback already, but is still marked online.
897 cpumask_or(&rdtgroup_default.cpu_mask,
898 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
900 kernfs_remove(rdtgrp->kn);
901 list_del(&rdtgrp->rdtgroup_list);
904 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
906 rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
909 kernfs_remove(kn_info);
912 static void rdt_kill_sb(struct super_block *sb)
914 struct rdt_resource *r;
916 mutex_lock(&rdtgroup_mutex);
918 /*Put everything back to default values. */
919 for_each_enabled_rdt_resource(r)
923 static_branch_disable(&rdt_enable_key);
925 mutex_unlock(&rdtgroup_mutex);
928 static struct file_system_type rdt_fs_type = {
931 .kill_sb = rdt_kill_sb,
934 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
937 struct rdtgroup *parent, *rdtgrp;
938 struct kernfs_node *kn;
941 /* Only allow mkdir in the root directory */
942 if (parent_kn != rdtgroup_default.kn)
945 /* Do not accept '\n' to avoid unparsable situation. */
946 if (strchr(name, '\n'))
949 parent = rdtgroup_kn_lock_live(parent_kn);
955 ret = closid_alloc();
960 /* allocate the rdtgroup. */
961 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
964 goto out_closid_free;
966 rdtgrp->closid = closid;
967 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
969 /* kernfs creates the directory for rdtgrp */
970 kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
978 * kernfs_remove() will drop the reference count on "kn" which
979 * will free it. But we still need it to stick around for the
980 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
981 * here, which will be dropped inside rdtgroup_kn_unlock().
985 ret = rdtgroup_kn_set_ugid(kn);
989 ret = rdtgroup_add_files(kn, rdtgroup_base_files,
990 ARRAY_SIZE(rdtgroup_base_files));
1000 kernfs_remove(rdtgrp->kn);
1002 list_del(&rdtgrp->rdtgroup_list);
1005 closid_free(closid);
1007 rdtgroup_kn_unlock(parent_kn);
1011 static int rdtgroup_rmdir(struct kernfs_node *kn)
1013 int ret, cpu, closid = rdtgroup_default.closid;
1014 struct rdtgroup *rdtgrp;
1015 cpumask_var_t tmpmask;
1017 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1020 rdtgrp = rdtgroup_kn_lock_live(kn);
1026 /* Give any tasks back to the default group */
1027 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
1029 /* Give any CPUs back to the default group */
1030 cpumask_or(&rdtgroup_default.cpu_mask,
1031 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1033 /* Update per cpu closid of the moved CPUs first */
1034 for_each_cpu(cpu, &rdtgrp->cpu_mask)
1035 per_cpu(cpu_closid, cpu) = closid;
1037 * Update the MSR on moved CPUs and CPUs which have moved
1038 * task running on them.
1040 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1041 rdt_update_closid(tmpmask, NULL);
1043 rdtgrp->flags = RDT_DELETED;
1044 closid_free(rdtgrp->closid);
1045 list_del(&rdtgrp->rdtgroup_list);
1048 * one extra hold on this, will drop when we kfree(rdtgrp)
1049 * in rdtgroup_kn_unlock()
1052 kernfs_remove(rdtgrp->kn);
1055 rdtgroup_kn_unlock(kn);
1056 free_cpumask_var(tmpmask);
1060 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1062 if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled)
1063 seq_puts(seq, ",cdp");
1067 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
1068 .mkdir = rdtgroup_mkdir,
1069 .rmdir = rdtgroup_rmdir,
1070 .show_options = rdtgroup_show_options,
1073 static int __init rdtgroup_setup_root(void)
1077 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1078 KERNFS_ROOT_CREATE_DEACTIVATED,
1080 if (IS_ERR(rdt_root))
1081 return PTR_ERR(rdt_root);
1083 mutex_lock(&rdtgroup_mutex);
1085 rdtgroup_default.closid = 0;
1086 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1088 ret = rdtgroup_add_files(rdt_root->kn, rdtgroup_base_files,
1089 ARRAY_SIZE(rdtgroup_base_files));
1091 kernfs_destroy_root(rdt_root);
1095 rdtgroup_default.kn = rdt_root->kn;
1096 kernfs_activate(rdtgroup_default.kn);
1099 mutex_unlock(&rdtgroup_mutex);
1105 * rdtgroup_init - rdtgroup initialization
1107 * Setup resctrl file system including set up root, create mount point,
1108 * register rdtgroup filesystem, and initialize files under root directory.
1110 * Return: 0 on success or -errno
1112 int __init rdtgroup_init(void)
1116 ret = rdtgroup_setup_root();
1120 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1124 ret = register_filesystem(&rdt_fs_type);
1126 goto cleanup_mountpoint;
1131 sysfs_remove_mount_point(fs_kobj, "resctrl");
1133 kernfs_destroy_root(rdt_root);