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);
521 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
522 struct seq_file *seq, void *v)
524 struct rdt_resource *r = of->kn->parent->priv;
526 seq_printf(seq, "%x\n", r->default_ctrl);
530 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
531 struct seq_file *seq, void *v)
533 struct rdt_resource *r = of->kn->parent->priv;
535 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
539 static int rdt_min_bw_show(struct kernfs_open_file *of,
540 struct seq_file *seq, void *v)
542 struct rdt_resource *r = of->kn->parent->priv;
544 seq_printf(seq, "%u\n", r->membw.min_bw);
548 static int rdt_bw_gran_show(struct kernfs_open_file *of,
549 struct seq_file *seq, void *v)
551 struct rdt_resource *r = of->kn->parent->priv;
553 seq_printf(seq, "%u\n", r->membw.bw_gran);
557 static int rdt_delay_linear_show(struct kernfs_open_file *of,
558 struct seq_file *seq, void *v)
560 struct rdt_resource *r = of->kn->parent->priv;
562 seq_printf(seq, "%u\n", r->membw.delay_linear);
566 /* rdtgroup information files for one cache resource. */
567 static struct rftype res_cache_info_files[] = {
569 .name = "num_closids",
571 .kf_ops = &rdtgroup_kf_single_ops,
572 .seq_show = rdt_num_closids_show,
577 .kf_ops = &rdtgroup_kf_single_ops,
578 .seq_show = rdt_default_ctrl_show,
581 .name = "min_cbm_bits",
583 .kf_ops = &rdtgroup_kf_single_ops,
584 .seq_show = rdt_min_cbm_bits_show,
588 /* rdtgroup information files for memory bandwidth. */
589 static struct rftype res_mba_info_files[] = {
591 .name = "num_closids",
593 .kf_ops = &rdtgroup_kf_single_ops,
594 .seq_show = rdt_num_closids_show,
597 .name = "min_bandwidth",
599 .kf_ops = &rdtgroup_kf_single_ops,
600 .seq_show = rdt_min_bw_show,
603 .name = "bandwidth_gran",
605 .kf_ops = &rdtgroup_kf_single_ops,
606 .seq_show = rdt_bw_gran_show,
609 .name = "delay_linear",
611 .kf_ops = &rdtgroup_kf_single_ops,
612 .seq_show = rdt_delay_linear_show,
616 void rdt_get_mba_infofile(struct rdt_resource *r)
618 r->info_files = res_mba_info_files;
619 r->nr_info_files = ARRAY_SIZE(res_mba_info_files);
622 void rdt_get_cache_infofile(struct rdt_resource *r)
624 r->info_files = res_cache_info_files;
625 r->nr_info_files = ARRAY_SIZE(res_cache_info_files);
628 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
630 struct kernfs_node *kn_subdir;
631 struct rftype *res_info_files;
632 struct rdt_resource *r;
635 /* create the directory */
636 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
638 return PTR_ERR(kn_info);
641 for_each_enabled_rdt_resource(r) {
642 kn_subdir = kernfs_create_dir(kn_info, r->name,
644 if (IS_ERR(kn_subdir)) {
645 ret = PTR_ERR(kn_subdir);
648 kernfs_get(kn_subdir);
649 ret = rdtgroup_kn_set_ugid(kn_subdir);
653 res_info_files = r->info_files;
654 len = r->nr_info_files;
656 ret = rdtgroup_add_files(kn_subdir, res_info_files, len);
659 kernfs_activate(kn_subdir);
663 * This extra ref will be put in kernfs_remove() and guarantees
664 * that @rdtgrp->kn is always accessible.
668 ret = rdtgroup_kn_set_ugid(kn_info);
672 kernfs_activate(kn_info);
677 kernfs_remove(kn_info);
681 static void l3_qos_cfg_update(void *arg)
685 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
688 static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
690 cpumask_var_t cpu_mask;
691 struct rdt_domain *d;
694 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
697 list_for_each_entry(d, &r->domains, list) {
698 /* Pick one CPU from each domain instance to update MSR */
699 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
702 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
703 if (cpumask_test_cpu(cpu, cpu_mask))
704 l3_qos_cfg_update(&enable);
705 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
706 smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
709 free_cpumask_var(cpu_mask);
714 static int cdp_enable(void)
716 struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
717 struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
718 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
721 if (!r_l3->capable || !r_l3data->capable || !r_l3code->capable)
724 ret = set_l3_qos_cfg(r_l3, true);
726 r_l3->enabled = false;
727 r_l3data->enabled = true;
728 r_l3code->enabled = true;
733 static void cdp_disable(void)
735 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
737 r->enabled = r->capable;
739 if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled) {
740 rdt_resources_all[RDT_RESOURCE_L3DATA].enabled = false;
741 rdt_resources_all[RDT_RESOURCE_L3CODE].enabled = false;
742 set_l3_qos_cfg(r, false);
746 static int parse_rdtgroupfs_options(char *data)
748 char *token, *o = data;
751 while ((token = strsep(&o, ",")) != NULL) {
755 if (!strcmp(token, "cdp"))
763 * We don't allow rdtgroup directories to be created anywhere
764 * except the root directory. Thus when looking for the rdtgroup
765 * structure for a kernfs node we are either looking at a directory,
766 * in which case the rdtgroup structure is pointed at by the "priv"
767 * field, otherwise we have a file, and need only look to the parent
768 * to find the rdtgroup.
770 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
772 if (kernfs_type(kn) == KERNFS_DIR) {
774 * All the resource directories use "kn->priv"
775 * to point to the "struct rdtgroup" for the
776 * resource. "info" and its subdirectories don't
777 * have rdtgroup structures, so return NULL here.
779 if (kn == kn_info || kn->parent == kn_info)
784 return kn->parent->priv;
788 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
790 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
795 atomic_inc(&rdtgrp->waitcount);
796 kernfs_break_active_protection(kn);
798 mutex_lock(&rdtgroup_mutex);
800 /* Was this group deleted while we waited? */
801 if (rdtgrp->flags & RDT_DELETED)
807 void rdtgroup_kn_unlock(struct kernfs_node *kn)
809 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
814 mutex_unlock(&rdtgroup_mutex);
816 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
817 (rdtgrp->flags & RDT_DELETED)) {
818 kernfs_unbreak_active_protection(kn);
819 kernfs_put(rdtgrp->kn);
822 kernfs_unbreak_active_protection(kn);
826 static struct dentry *rdt_mount(struct file_system_type *fs_type,
827 int flags, const char *unused_dev_name,
830 struct dentry *dentry;
833 mutex_lock(&rdtgroup_mutex);
835 * resctrl file system can only be mounted once.
837 if (static_branch_unlikely(&rdt_enable_key)) {
838 dentry = ERR_PTR(-EBUSY);
842 ret = parse_rdtgroupfs_options(data);
844 dentry = ERR_PTR(ret);
850 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
852 dentry = ERR_PTR(ret);
856 dentry = kernfs_mount(fs_type, flags, rdt_root,
857 RDTGROUP_SUPER_MAGIC, NULL);
861 static_branch_enable(&rdt_enable_key);
867 mutex_unlock(&rdtgroup_mutex);
872 static int reset_all_ctrls(struct rdt_resource *r)
874 struct msr_param msr_param;
875 cpumask_var_t cpu_mask;
876 struct rdt_domain *d;
879 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
884 msr_param.high = r->num_closid;
887 * Disable resource control for this resource by setting all
888 * CBMs in all domains to the maximum mask value. Pick one CPU
889 * from each domain to update the MSRs below.
891 list_for_each_entry(d, &r->domains, list) {
892 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
894 for (i = 0; i < r->num_closid; i++)
895 d->ctrl_val[i] = r->default_ctrl;
898 /* Update CBM on this cpu if it's in cpu_mask. */
899 if (cpumask_test_cpu(cpu, cpu_mask))
900 rdt_ctrl_update(&msr_param);
901 /* Update CBM on all other cpus in cpu_mask. */
902 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
905 free_cpumask_var(cpu_mask);
911 * Move tasks from one to the other group. If @from is NULL, then all tasks
912 * in the systems are moved unconditionally (used for teardown).
914 * If @mask is not NULL the cpus on which moved tasks are running are set
915 * in that mask so the update smp function call is restricted to affected
918 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
919 struct cpumask *mask)
921 struct task_struct *p, *t;
923 read_lock(&tasklist_lock);
924 for_each_process_thread(p, t) {
925 if (!from || t->closid == from->closid) {
926 t->closid = to->closid;
929 * This is safe on x86 w/o barriers as the ordering
930 * of writing to task_cpu() and t->on_cpu is
931 * reverse to the reading here. The detection is
932 * inaccurate as tasks might move or schedule
933 * before the smp function call takes place. In
934 * such a case the function call is pointless, but
935 * there is no other side effect.
937 if (mask && t->on_cpu)
938 cpumask_set_cpu(task_cpu(t), mask);
942 read_unlock(&tasklist_lock);
946 * Forcibly remove all of subdirectories under root.
948 static void rmdir_all_sub(void)
950 struct rdtgroup *rdtgrp, *tmp;
952 /* Move all tasks to the default resource group */
953 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
955 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
956 /* Remove each rdtgroup other than root */
957 if (rdtgrp == &rdtgroup_default)
961 * Give any CPUs back to the default group. We cannot copy
962 * cpu_online_mask because a CPU might have executed the
963 * offline callback already, but is still marked online.
965 cpumask_or(&rdtgroup_default.cpu_mask,
966 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
968 kernfs_remove(rdtgrp->kn);
969 list_del(&rdtgrp->rdtgroup_list);
972 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
974 rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
977 kernfs_remove(kn_info);
980 static void rdt_kill_sb(struct super_block *sb)
982 struct rdt_resource *r;
984 mutex_lock(&rdtgroup_mutex);
986 /*Put everything back to default values. */
987 for_each_enabled_rdt_resource(r)
991 static_branch_disable(&rdt_enable_key);
993 mutex_unlock(&rdtgroup_mutex);
996 static struct file_system_type rdt_fs_type = {
999 .kill_sb = rdt_kill_sb,
1002 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
1005 struct rdtgroup *parent, *rdtgrp;
1006 struct kernfs_node *kn;
1009 /* Only allow mkdir in the root directory */
1010 if (parent_kn != rdtgroup_default.kn)
1013 /* Do not accept '\n' to avoid unparsable situation. */
1014 if (strchr(name, '\n'))
1017 parent = rdtgroup_kn_lock_live(parent_kn);
1023 ret = closid_alloc();
1028 /* allocate the rdtgroup. */
1029 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
1032 goto out_closid_free;
1034 rdtgrp->closid = closid;
1035 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
1037 /* kernfs creates the directory for rdtgrp */
1038 kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
1041 goto out_cancel_ref;
1046 * kernfs_remove() will drop the reference count on "kn" which
1047 * will free it. But we still need it to stick around for the
1048 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
1049 * here, which will be dropped inside rdtgroup_kn_unlock().
1053 ret = rdtgroup_kn_set_ugid(kn);
1057 ret = rdtgroup_add_files(kn, rdtgroup_base_files,
1058 ARRAY_SIZE(rdtgroup_base_files));
1062 kernfs_activate(kn);
1068 kernfs_remove(rdtgrp->kn);
1070 list_del(&rdtgrp->rdtgroup_list);
1073 closid_free(closid);
1075 rdtgroup_kn_unlock(parent_kn);
1079 static int rdtgroup_rmdir(struct kernfs_node *kn)
1081 int ret, cpu, closid = rdtgroup_default.closid;
1082 struct rdtgroup *rdtgrp;
1083 cpumask_var_t tmpmask;
1085 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1088 rdtgrp = rdtgroup_kn_lock_live(kn);
1094 /* Give any tasks back to the default group */
1095 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
1097 /* Give any CPUs back to the default group */
1098 cpumask_or(&rdtgroup_default.cpu_mask,
1099 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1101 /* Update per cpu closid of the moved CPUs first */
1102 for_each_cpu(cpu, &rdtgrp->cpu_mask)
1103 per_cpu(cpu_closid, cpu) = closid;
1105 * Update the MSR on moved CPUs and CPUs which have moved
1106 * task running on them.
1108 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1109 rdt_update_closid(tmpmask, NULL);
1111 rdtgrp->flags = RDT_DELETED;
1112 closid_free(rdtgrp->closid);
1113 list_del(&rdtgrp->rdtgroup_list);
1116 * one extra hold on this, will drop when we kfree(rdtgrp)
1117 * in rdtgroup_kn_unlock()
1120 kernfs_remove(rdtgrp->kn);
1123 rdtgroup_kn_unlock(kn);
1124 free_cpumask_var(tmpmask);
1128 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1130 if (rdt_resources_all[RDT_RESOURCE_L3DATA].enabled)
1131 seq_puts(seq, ",cdp");
1135 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
1136 .mkdir = rdtgroup_mkdir,
1137 .rmdir = rdtgroup_rmdir,
1138 .show_options = rdtgroup_show_options,
1141 static int __init rdtgroup_setup_root(void)
1145 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1146 KERNFS_ROOT_CREATE_DEACTIVATED,
1148 if (IS_ERR(rdt_root))
1149 return PTR_ERR(rdt_root);
1151 mutex_lock(&rdtgroup_mutex);
1153 rdtgroup_default.closid = 0;
1154 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1156 ret = rdtgroup_add_files(rdt_root->kn, rdtgroup_base_files,
1157 ARRAY_SIZE(rdtgroup_base_files));
1159 kernfs_destroy_root(rdt_root);
1163 rdtgroup_default.kn = rdt_root->kn;
1164 kernfs_activate(rdtgroup_default.kn);
1167 mutex_unlock(&rdtgroup_mutex);
1173 * rdtgroup_init - rdtgroup initialization
1175 * Setup resctrl file system including set up root, create mount point,
1176 * register rdtgroup filesystem, and initialize files under root directory.
1178 * Return: 0 on success or -errno
1180 int __init rdtgroup_init(void)
1184 ret = rdtgroup_setup_root();
1188 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1192 ret = register_filesystem(&rdt_fs_type);
1194 goto cleanup_mountpoint;
1199 sysfs_remove_mount_point(fs_kobj, "resctrl");
1201 kernfs_destroy_root(rdt_root);