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/cacheinfo.h>
24 #include <linux/cpu.h>
25 #include <linux/debugfs.h>
27 #include <linux/sysfs.h>
28 #include <linux/kernfs.h>
29 #include <linux/seq_buf.h>
30 #include <linux/seq_file.h>
31 #include <linux/sched/signal.h>
32 #include <linux/sched/task.h>
33 #include <linux/slab.h>
34 #include <linux/task_work.h>
36 #include <uapi/linux/magic.h>
38 #include <asm/intel_rdt_sched.h>
39 #include "intel_rdt.h"
41 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
42 DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
43 DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
44 static struct kernfs_root *rdt_root;
45 struct rdtgroup rdtgroup_default;
46 LIST_HEAD(rdt_all_groups);
48 /* Kernel fs node for "info" directory under root */
49 static struct kernfs_node *kn_info;
51 /* Kernel fs node for "mon_groups" directory under root */
52 static struct kernfs_node *kn_mongrp;
54 /* Kernel fs node for "mon_data" directory under root */
55 static struct kernfs_node *kn_mondata;
57 static struct seq_buf last_cmd_status;
58 static char last_cmd_status_buf[512];
60 struct dentry *debugfs_resctrl;
62 void rdt_last_cmd_clear(void)
64 lockdep_assert_held(&rdtgroup_mutex);
65 seq_buf_clear(&last_cmd_status);
68 void rdt_last_cmd_puts(const char *s)
70 lockdep_assert_held(&rdtgroup_mutex);
71 seq_buf_puts(&last_cmd_status, s);
74 void rdt_last_cmd_printf(const char *fmt, ...)
79 lockdep_assert_held(&rdtgroup_mutex);
80 seq_buf_vprintf(&last_cmd_status, fmt, ap);
85 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
86 * we can keep a bitmap of free CLOSIDs in a single integer.
88 * Using a global CLOSID across all resources has some advantages and
90 * + We can simply set "current->closid" to assign a task to a resource
92 * + Context switch code can avoid extra memory references deciding which
93 * CLOSID to load into the PQR_ASSOC MSR
94 * - We give up some options in configuring resource groups across multi-socket
96 * - Our choices on how to configure each resource become progressively more
97 * limited as the number of resources grows.
99 static int closid_free_map;
101 static void closid_init(void)
103 struct rdt_resource *r;
104 int rdt_min_closid = 32;
106 /* Compute rdt_min_closid across all resources */
107 for_each_alloc_enabled_rdt_resource(r)
108 rdt_min_closid = min(rdt_min_closid, r->num_closid);
110 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
112 /* CLOSID 0 is always reserved for the default group */
113 closid_free_map &= ~1;
116 static int closid_alloc(void)
118 u32 closid = ffs(closid_free_map);
123 closid_free_map &= ~(1 << closid);
128 void closid_free(int closid)
130 closid_free_map |= 1 << closid;
134 * closid_allocated - test if provided closid is in use
135 * @closid: closid to be tested
137 * Return: true if @closid is currently associated with a resource group,
138 * false if @closid is free
140 static bool closid_allocated(unsigned int closid)
142 return (closid_free_map & (1 << closid)) == 0;
146 * rdtgroup_mode_by_closid - Return mode of resource group with closid
147 * @closid: closid if the resource group
149 * Each resource group is associated with a @closid. Here the mode
150 * of a resource group can be queried by searching for it using its closid.
152 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
154 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
156 struct rdtgroup *rdtgrp;
158 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
159 if (rdtgrp->closid == closid)
163 return RDT_NUM_MODES;
166 static const char * const rdt_mode_str[] = {
167 [RDT_MODE_SHAREABLE] = "shareable",
168 [RDT_MODE_EXCLUSIVE] = "exclusive",
169 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
170 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
174 * rdtgroup_mode_str - Return the string representation of mode
175 * @mode: the resource group mode as &enum rdtgroup_mode
177 * Return: string representation of valid mode, "unknown" otherwise
179 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
181 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
184 return rdt_mode_str[mode];
187 /* set uid and gid of rdtgroup dirs and files to that of the creator */
188 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
190 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
191 .ia_uid = current_fsuid(),
192 .ia_gid = current_fsgid(), };
194 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
195 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
198 return kernfs_setattr(kn, &iattr);
201 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
203 struct kernfs_node *kn;
206 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
207 0, rft->kf_ops, rft, NULL, NULL);
211 ret = rdtgroup_kn_set_ugid(kn);
220 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
222 struct kernfs_open_file *of = m->private;
223 struct rftype *rft = of->kn->priv;
226 return rft->seq_show(of, m, arg);
230 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
231 size_t nbytes, loff_t off)
233 struct rftype *rft = of->kn->priv;
236 return rft->write(of, buf, nbytes, off);
241 static struct kernfs_ops rdtgroup_kf_single_ops = {
242 .atomic_write_len = PAGE_SIZE,
243 .write = rdtgroup_file_write,
244 .seq_show = rdtgroup_seqfile_show,
247 static struct kernfs_ops kf_mondata_ops = {
248 .atomic_write_len = PAGE_SIZE,
249 .seq_show = rdtgroup_mondata_show,
252 static bool is_cpu_list(struct kernfs_open_file *of)
254 struct rftype *rft = of->kn->priv;
256 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
259 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
260 struct seq_file *s, void *v)
262 struct rdtgroup *rdtgrp;
265 rdtgrp = rdtgroup_kn_lock_live(of->kn);
268 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
269 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
270 cpumask_pr_args(&rdtgrp->plr->d->cpu_mask));
272 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
273 cpumask_pr_args(&rdtgrp->cpu_mask));
277 rdtgroup_kn_unlock(of->kn);
283 * This is safe against intel_rdt_sched_in() called from __switch_to()
284 * because __switch_to() is executed with interrupts disabled. A local call
285 * from update_closid_rmid() is proteced against __switch_to() because
286 * preemption is disabled.
288 static void update_cpu_closid_rmid(void *info)
290 struct rdtgroup *r = info;
293 this_cpu_write(pqr_state.default_closid, r->closid);
294 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
298 * We cannot unconditionally write the MSR because the current
299 * executing task might have its own closid selected. Just reuse
300 * the context switch code.
302 intel_rdt_sched_in();
306 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
308 * Per task closids/rmids must have been set up before calling this function.
311 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
315 if (cpumask_test_cpu(cpu, cpu_mask))
316 update_cpu_closid_rmid(r);
317 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
321 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
322 cpumask_var_t tmpmask)
324 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
325 struct list_head *head;
327 /* Check whether cpus belong to parent ctrl group */
328 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
329 if (cpumask_weight(tmpmask)) {
330 rdt_last_cmd_puts("can only add CPUs to mongroup that belong to parent\n");
334 /* Check whether cpus are dropped from this group */
335 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
336 if (cpumask_weight(tmpmask)) {
337 /* Give any dropped cpus to parent rdtgroup */
338 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
339 update_closid_rmid(tmpmask, prgrp);
343 * If we added cpus, remove them from previous group that owned them
344 * and update per-cpu rmid
346 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
347 if (cpumask_weight(tmpmask)) {
348 head = &prgrp->mon.crdtgrp_list;
349 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
352 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
355 update_closid_rmid(tmpmask, rdtgrp);
358 /* Done pushing/pulling - update this group with new mask */
359 cpumask_copy(&rdtgrp->cpu_mask, newmask);
364 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
366 struct rdtgroup *crgrp;
368 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
369 /* update the child mon group masks as well*/
370 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
371 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
374 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
375 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
377 struct rdtgroup *r, *crgrp;
378 struct list_head *head;
380 /* Check whether cpus are dropped from this group */
381 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
382 if (cpumask_weight(tmpmask)) {
383 /* Can't drop from default group */
384 if (rdtgrp == &rdtgroup_default) {
385 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
389 /* Give any dropped cpus to rdtgroup_default */
390 cpumask_or(&rdtgroup_default.cpu_mask,
391 &rdtgroup_default.cpu_mask, tmpmask);
392 update_closid_rmid(tmpmask, &rdtgroup_default);
396 * If we added cpus, remove them from previous group and
397 * the prev group's child groups that owned them
398 * and update per-cpu closid/rmid.
400 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
401 if (cpumask_weight(tmpmask)) {
402 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
405 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
406 if (cpumask_weight(tmpmask1))
407 cpumask_rdtgrp_clear(r, tmpmask1);
409 update_closid_rmid(tmpmask, rdtgrp);
412 /* Done pushing/pulling - update this group with new mask */
413 cpumask_copy(&rdtgrp->cpu_mask, newmask);
416 * Clear child mon group masks since there is a new parent mask
417 * now and update the rmid for the cpus the child lost.
419 head = &rdtgrp->mon.crdtgrp_list;
420 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
421 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
422 update_closid_rmid(tmpmask, rdtgrp);
423 cpumask_clear(&crgrp->cpu_mask);
429 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
430 char *buf, size_t nbytes, loff_t off)
432 cpumask_var_t tmpmask, newmask, tmpmask1;
433 struct rdtgroup *rdtgrp;
439 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
441 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
442 free_cpumask_var(tmpmask);
445 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
446 free_cpumask_var(tmpmask);
447 free_cpumask_var(newmask);
451 rdtgrp = rdtgroup_kn_lock_live(of->kn);
452 rdt_last_cmd_clear();
455 rdt_last_cmd_puts("directory was removed\n");
459 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
460 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
462 rdt_last_cmd_puts("pseudo-locking in progress\n");
467 ret = cpulist_parse(buf, newmask);
469 ret = cpumask_parse(buf, newmask);
472 rdt_last_cmd_puts("bad cpu list/mask\n");
476 /* check that user didn't specify any offline cpus */
477 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
478 if (cpumask_weight(tmpmask)) {
480 rdt_last_cmd_puts("can only assign online cpus\n");
484 if (rdtgrp->type == RDTCTRL_GROUP)
485 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
486 else if (rdtgrp->type == RDTMON_GROUP)
487 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
492 rdtgroup_kn_unlock(of->kn);
493 free_cpumask_var(tmpmask);
494 free_cpumask_var(newmask);
495 free_cpumask_var(tmpmask1);
497 return ret ?: nbytes;
500 struct task_move_callback {
501 struct callback_head work;
502 struct rdtgroup *rdtgrp;
505 static void move_myself(struct callback_head *head)
507 struct task_move_callback *callback;
508 struct rdtgroup *rdtgrp;
510 callback = container_of(head, struct task_move_callback, work);
511 rdtgrp = callback->rdtgrp;
514 * If resource group was deleted before this task work callback
515 * was invoked, then assign the task to root group and free the
518 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
519 (rdtgrp->flags & RDT_DELETED)) {
526 /* update PQR_ASSOC MSR to make resource group go into effect */
527 intel_rdt_sched_in();
533 static int __rdtgroup_move_task(struct task_struct *tsk,
534 struct rdtgroup *rdtgrp)
536 struct task_move_callback *callback;
539 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
542 callback->work.func = move_myself;
543 callback->rdtgrp = rdtgrp;
546 * Take a refcount, so rdtgrp cannot be freed before the
547 * callback has been invoked.
549 atomic_inc(&rdtgrp->waitcount);
550 ret = task_work_add(tsk, &callback->work, true);
553 * Task is exiting. Drop the refcount and free the callback.
554 * No need to check the refcount as the group cannot be
555 * deleted before the write function unlocks rdtgroup_mutex.
557 atomic_dec(&rdtgrp->waitcount);
559 rdt_last_cmd_puts("task exited\n");
562 * For ctrl_mon groups move both closid and rmid.
563 * For monitor groups, can move the tasks only from
564 * their parent CTRL group.
566 if (rdtgrp->type == RDTCTRL_GROUP) {
567 tsk->closid = rdtgrp->closid;
568 tsk->rmid = rdtgrp->mon.rmid;
569 } else if (rdtgrp->type == RDTMON_GROUP) {
570 if (rdtgrp->mon.parent->closid == tsk->closid) {
571 tsk->rmid = rdtgrp->mon.rmid;
573 rdt_last_cmd_puts("Can't move task to different control group\n");
582 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
585 * Return: 1 if tasks have been assigned to @r, 0 otherwise
587 int rdtgroup_tasks_assigned(struct rdtgroup *r)
589 struct task_struct *p, *t;
592 lockdep_assert_held(&rdtgroup_mutex);
595 for_each_process_thread(p, t) {
596 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
597 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) {
607 static int rdtgroup_task_write_permission(struct task_struct *task,
608 struct kernfs_open_file *of)
610 const struct cred *tcred = get_task_cred(task);
611 const struct cred *cred = current_cred();
615 * Even if we're attaching all tasks in the thread group, we only
616 * need to check permissions on one of them.
618 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
619 !uid_eq(cred->euid, tcred->uid) &&
620 !uid_eq(cred->euid, tcred->suid)) {
621 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
629 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
630 struct kernfs_open_file *of)
632 struct task_struct *tsk;
637 tsk = find_task_by_vpid(pid);
640 rdt_last_cmd_printf("No task %d\n", pid);
647 get_task_struct(tsk);
650 ret = rdtgroup_task_write_permission(tsk, of);
652 ret = __rdtgroup_move_task(tsk, rdtgrp);
654 put_task_struct(tsk);
658 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
659 char *buf, size_t nbytes, loff_t off)
661 struct rdtgroup *rdtgrp;
665 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
667 rdtgrp = rdtgroup_kn_lock_live(of->kn);
669 rdtgroup_kn_unlock(of->kn);
672 rdt_last_cmd_clear();
674 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
675 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
677 rdt_last_cmd_puts("pseudo-locking in progress\n");
681 ret = rdtgroup_move_task(pid, rdtgrp, of);
684 rdtgroup_kn_unlock(of->kn);
686 return ret ?: nbytes;
689 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
691 struct task_struct *p, *t;
694 for_each_process_thread(p, t) {
695 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
696 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid))
697 seq_printf(s, "%d\n", t->pid);
702 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
703 struct seq_file *s, void *v)
705 struct rdtgroup *rdtgrp;
708 rdtgrp = rdtgroup_kn_lock_live(of->kn);
710 show_rdt_tasks(rdtgrp, s);
713 rdtgroup_kn_unlock(of->kn);
718 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
719 struct seq_file *seq, void *v)
723 mutex_lock(&rdtgroup_mutex);
724 len = seq_buf_used(&last_cmd_status);
726 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
728 seq_puts(seq, "ok\n");
729 mutex_unlock(&rdtgroup_mutex);
733 static int rdt_num_closids_show(struct kernfs_open_file *of,
734 struct seq_file *seq, void *v)
736 struct rdt_resource *r = of->kn->parent->priv;
738 seq_printf(seq, "%d\n", r->num_closid);
742 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
743 struct seq_file *seq, void *v)
745 struct rdt_resource *r = of->kn->parent->priv;
747 seq_printf(seq, "%x\n", r->default_ctrl);
751 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
752 struct seq_file *seq, void *v)
754 struct rdt_resource *r = of->kn->parent->priv;
756 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
760 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
761 struct seq_file *seq, void *v)
763 struct rdt_resource *r = of->kn->parent->priv;
765 seq_printf(seq, "%x\n", r->cache.shareable_bits);
770 * rdt_bit_usage_show - Display current usage of resources
772 * A domain is a shared resource that can now be allocated differently. Here
773 * we display the current regions of the domain as an annotated bitmask.
774 * For each domain of this resource its allocation bitmask
775 * is annotated as below to indicate the current usage of the corresponding bit:
776 * 0 - currently unused
777 * X - currently available for sharing and used by software and hardware
778 * H - currently used by hardware only but available for software use
779 * S - currently used and shareable by software only
780 * E - currently used exclusively by one resource group
781 * P - currently pseudo-locked by one resource group
783 static int rdt_bit_usage_show(struct kernfs_open_file *of,
784 struct seq_file *seq, void *v)
786 struct rdt_resource *r = of->kn->parent->priv;
787 u32 sw_shareable = 0, hw_shareable = 0;
788 u32 exclusive = 0, pseudo_locked = 0;
789 struct rdt_domain *dom;
790 int i, hwb, swb, excl, psl;
791 enum rdtgrp_mode mode;
795 mutex_lock(&rdtgroup_mutex);
796 hw_shareable = r->cache.shareable_bits;
797 list_for_each_entry(dom, &r->domains, list) {
800 ctrl = dom->ctrl_val;
803 seq_printf(seq, "%d=", dom->id);
804 for (i = 0; i < r->num_closid; i++, ctrl++) {
805 if (!closid_allocated(i))
807 mode = rdtgroup_mode_by_closid(i);
809 case RDT_MODE_SHAREABLE:
810 sw_shareable |= *ctrl;
812 case RDT_MODE_EXCLUSIVE:
815 case RDT_MODE_PSEUDO_LOCKSETUP:
817 * RDT_MODE_PSEUDO_LOCKSETUP is possible
818 * here but not included since the CBM
819 * associated with this CLOSID in this mode
820 * is not initialized and no task or cpu can be
821 * assigned this CLOSID.
824 case RDT_MODE_PSEUDO_LOCKED:
827 "invalid mode for closid %d\n", i);
831 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
832 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
833 hwb = test_bit(i, (unsigned long *)&hw_shareable);
834 swb = test_bit(i, (unsigned long *)&sw_shareable);
835 excl = test_bit(i, (unsigned long *)&exclusive);
836 psl = test_bit(i, (unsigned long *)&pseudo_locked);
839 else if (hwb && !swb)
841 else if (!hwb && swb)
847 else /* Unused bits remain */
853 mutex_unlock(&rdtgroup_mutex);
857 static int rdt_min_bw_show(struct kernfs_open_file *of,
858 struct seq_file *seq, void *v)
860 struct rdt_resource *r = of->kn->parent->priv;
862 seq_printf(seq, "%u\n", r->membw.min_bw);
866 static int rdt_num_rmids_show(struct kernfs_open_file *of,
867 struct seq_file *seq, void *v)
869 struct rdt_resource *r = of->kn->parent->priv;
871 seq_printf(seq, "%d\n", r->num_rmid);
876 static int rdt_mon_features_show(struct kernfs_open_file *of,
877 struct seq_file *seq, void *v)
879 struct rdt_resource *r = of->kn->parent->priv;
880 struct mon_evt *mevt;
882 list_for_each_entry(mevt, &r->evt_list, list)
883 seq_printf(seq, "%s\n", mevt->name);
888 static int rdt_bw_gran_show(struct kernfs_open_file *of,
889 struct seq_file *seq, void *v)
891 struct rdt_resource *r = of->kn->parent->priv;
893 seq_printf(seq, "%u\n", r->membw.bw_gran);
897 static int rdt_delay_linear_show(struct kernfs_open_file *of,
898 struct seq_file *seq, void *v)
900 struct rdt_resource *r = of->kn->parent->priv;
902 seq_printf(seq, "%u\n", r->membw.delay_linear);
906 static int max_threshold_occ_show(struct kernfs_open_file *of,
907 struct seq_file *seq, void *v)
909 struct rdt_resource *r = of->kn->parent->priv;
911 seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale);
916 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
917 char *buf, size_t nbytes, loff_t off)
919 struct rdt_resource *r = of->kn->parent->priv;
923 ret = kstrtouint(buf, 0, &bytes);
927 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
930 intel_cqm_threshold = bytes / r->mon_scale;
936 * rdtgroup_mode_show - Display mode of this resource group
938 static int rdtgroup_mode_show(struct kernfs_open_file *of,
939 struct seq_file *s, void *v)
941 struct rdtgroup *rdtgrp;
943 rdtgrp = rdtgroup_kn_lock_live(of->kn);
945 rdtgroup_kn_unlock(of->kn);
949 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
951 rdtgroup_kn_unlock(of->kn);
956 * rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
957 * @r: Resource to which domain instance @d belongs.
958 * @d: The domain instance for which @closid is being tested.
959 * @cbm: Capacity bitmask being tested.
960 * @closid: Intended closid for @cbm.
961 * @exclusive: Only check if overlaps with exclusive resource groups
963 * Checks if provided @cbm intended to be used for @closid on domain
964 * @d overlaps with any other closids or other hardware usage associated
965 * with this domain. If @exclusive is true then only overlaps with
966 * resource groups in exclusive mode will be considered. If @exclusive
967 * is false then overlaps with any resource group or hardware entities
968 * will be considered.
970 * Return: false if CBM does not overlap, true if it does.
972 bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
973 u32 _cbm, int closid, bool exclusive)
975 unsigned long *cbm = (unsigned long *)&_cbm;
976 unsigned long *ctrl_b;
977 enum rdtgrp_mode mode;
981 /* Check for any overlap with regions used by hardware directly */
983 if (bitmap_intersects(cbm,
984 (unsigned long *)&r->cache.shareable_bits,
989 /* Check for overlap with other resource groups */
991 for (i = 0; i < r->num_closid; i++, ctrl++) {
992 ctrl_b = (unsigned long *)ctrl;
993 mode = rdtgroup_mode_by_closid(i);
994 if (closid_allocated(i) && i != closid &&
995 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
996 if (bitmap_intersects(cbm, ctrl_b, r->cache.cbm_len)) {
998 if (mode == RDT_MODE_EXCLUSIVE)
1011 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1013 * An exclusive resource group implies that there should be no sharing of
1014 * its allocated resources. At the time this group is considered to be
1015 * exclusive this test can determine if its current schemata supports this
1016 * setting by testing for overlap with all other resource groups.
1018 * Return: true if resource group can be exclusive, false if there is overlap
1019 * with allocations of other resource groups and thus this resource group
1020 * cannot be exclusive.
1022 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1024 int closid = rdtgrp->closid;
1025 struct rdt_resource *r;
1026 struct rdt_domain *d;
1028 for_each_alloc_enabled_rdt_resource(r) {
1029 list_for_each_entry(d, &r->domains, list) {
1030 if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid],
1031 rdtgrp->closid, false))
1040 * rdtgroup_mode_write - Modify the resource group's mode
1043 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1044 char *buf, size_t nbytes, loff_t off)
1046 struct rdtgroup *rdtgrp;
1047 enum rdtgrp_mode mode;
1050 /* Valid input requires a trailing newline */
1051 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1053 buf[nbytes - 1] = '\0';
1055 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1057 rdtgroup_kn_unlock(of->kn);
1061 rdt_last_cmd_clear();
1063 mode = rdtgrp->mode;
1065 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1066 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1067 (!strcmp(buf, "pseudo-locksetup") &&
1068 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1069 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1072 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1073 rdt_last_cmd_printf("cannot change pseudo-locked group\n");
1078 if (!strcmp(buf, "shareable")) {
1079 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1080 ret = rdtgroup_locksetup_exit(rdtgrp);
1084 rdtgrp->mode = RDT_MODE_SHAREABLE;
1085 } else if (!strcmp(buf, "exclusive")) {
1086 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1087 rdt_last_cmd_printf("schemata overlaps\n");
1091 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1092 ret = rdtgroup_locksetup_exit(rdtgrp);
1096 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1097 } else if (!strcmp(buf, "pseudo-locksetup")) {
1098 ret = rdtgroup_locksetup_enter(rdtgrp);
1101 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1103 rdt_last_cmd_printf("unknown/unsupported mode\n");
1108 rdtgroup_kn_unlock(of->kn);
1109 return ret ?: nbytes;
1113 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1114 * @r: RDT resource to which @d belongs.
1115 * @d: RDT domain instance.
1116 * @cbm: bitmask for which the size should be computed.
1118 * The bitmask provided associated with the RDT domain instance @d will be
1119 * translated into how many bytes it represents. The size in bytes is
1120 * computed by first dividing the total cache size by the CBM length to
1121 * determine how many bytes each bit in the bitmask represents. The result
1122 * is multiplied with the number of bits set in the bitmask.
1124 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1125 struct rdt_domain *d, u32 cbm)
1127 struct cpu_cacheinfo *ci;
1128 unsigned int size = 0;
1131 num_b = bitmap_weight((unsigned long *)&cbm, r->cache.cbm_len);
1132 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1133 for (i = 0; i < ci->num_leaves; i++) {
1134 if (ci->info_list[i].level == r->cache_level) {
1135 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1144 * rdtgroup_size_show - Display size in bytes of allocated regions
1146 * The "size" file mirrors the layout of the "schemata" file, printing the
1147 * size in bytes of each region instead of the capacity bitmask.
1150 static int rdtgroup_size_show(struct kernfs_open_file *of,
1151 struct seq_file *s, void *v)
1153 struct rdtgroup *rdtgrp;
1154 struct rdt_resource *r;
1155 struct rdt_domain *d;
1160 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1162 rdtgroup_kn_unlock(of->kn);
1166 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1167 seq_printf(s, "%*s:", max_name_width, rdtgrp->plr->r->name);
1168 size = rdtgroup_cbm_to_size(rdtgrp->plr->r,
1171 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1175 for_each_alloc_enabled_rdt_resource(r) {
1176 seq_printf(s, "%*s:", max_name_width, r->name);
1177 list_for_each_entry(d, &r->domains, list) {
1180 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1183 cbm = d->ctrl_val[rdtgrp->closid];
1184 size = rdtgroup_cbm_to_size(r, d, cbm);
1186 seq_printf(s, "%d=%u", d->id, size);
1193 rdtgroup_kn_unlock(of->kn);
1198 /* rdtgroup information files for one cache resource. */
1199 static struct rftype res_common_files[] = {
1201 .name = "last_cmd_status",
1203 .kf_ops = &rdtgroup_kf_single_ops,
1204 .seq_show = rdt_last_cmd_status_show,
1205 .fflags = RF_TOP_INFO,
1208 .name = "num_closids",
1210 .kf_ops = &rdtgroup_kf_single_ops,
1211 .seq_show = rdt_num_closids_show,
1212 .fflags = RF_CTRL_INFO,
1215 .name = "mon_features",
1217 .kf_ops = &rdtgroup_kf_single_ops,
1218 .seq_show = rdt_mon_features_show,
1219 .fflags = RF_MON_INFO,
1222 .name = "num_rmids",
1224 .kf_ops = &rdtgroup_kf_single_ops,
1225 .seq_show = rdt_num_rmids_show,
1226 .fflags = RF_MON_INFO,
1231 .kf_ops = &rdtgroup_kf_single_ops,
1232 .seq_show = rdt_default_ctrl_show,
1233 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1236 .name = "min_cbm_bits",
1238 .kf_ops = &rdtgroup_kf_single_ops,
1239 .seq_show = rdt_min_cbm_bits_show,
1240 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1243 .name = "shareable_bits",
1245 .kf_ops = &rdtgroup_kf_single_ops,
1246 .seq_show = rdt_shareable_bits_show,
1247 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1250 .name = "bit_usage",
1252 .kf_ops = &rdtgroup_kf_single_ops,
1253 .seq_show = rdt_bit_usage_show,
1254 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1257 .name = "min_bandwidth",
1259 .kf_ops = &rdtgroup_kf_single_ops,
1260 .seq_show = rdt_min_bw_show,
1261 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1264 .name = "bandwidth_gran",
1266 .kf_ops = &rdtgroup_kf_single_ops,
1267 .seq_show = rdt_bw_gran_show,
1268 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1271 .name = "delay_linear",
1273 .kf_ops = &rdtgroup_kf_single_ops,
1274 .seq_show = rdt_delay_linear_show,
1275 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1278 .name = "max_threshold_occupancy",
1280 .kf_ops = &rdtgroup_kf_single_ops,
1281 .write = max_threshold_occ_write,
1282 .seq_show = max_threshold_occ_show,
1283 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
1288 .kf_ops = &rdtgroup_kf_single_ops,
1289 .write = rdtgroup_cpus_write,
1290 .seq_show = rdtgroup_cpus_show,
1291 .fflags = RFTYPE_BASE,
1294 .name = "cpus_list",
1296 .kf_ops = &rdtgroup_kf_single_ops,
1297 .write = rdtgroup_cpus_write,
1298 .seq_show = rdtgroup_cpus_show,
1299 .flags = RFTYPE_FLAGS_CPUS_LIST,
1300 .fflags = RFTYPE_BASE,
1305 .kf_ops = &rdtgroup_kf_single_ops,
1306 .write = rdtgroup_tasks_write,
1307 .seq_show = rdtgroup_tasks_show,
1308 .fflags = RFTYPE_BASE,
1313 .kf_ops = &rdtgroup_kf_single_ops,
1314 .write = rdtgroup_schemata_write,
1315 .seq_show = rdtgroup_schemata_show,
1316 .fflags = RF_CTRL_BASE,
1321 .kf_ops = &rdtgroup_kf_single_ops,
1322 .write = rdtgroup_mode_write,
1323 .seq_show = rdtgroup_mode_show,
1324 .fflags = RF_CTRL_BASE,
1329 .kf_ops = &rdtgroup_kf_single_ops,
1330 .seq_show = rdtgroup_size_show,
1331 .fflags = RF_CTRL_BASE,
1336 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1338 struct rftype *rfts, *rft;
1341 rfts = res_common_files;
1342 len = ARRAY_SIZE(res_common_files);
1344 lockdep_assert_held(&rdtgroup_mutex);
1346 for (rft = rfts; rft < rfts + len; rft++) {
1347 if ((fflags & rft->fflags) == rft->fflags) {
1348 ret = rdtgroup_add_file(kn, rft);
1356 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1357 while (--rft >= rfts) {
1358 if ((fflags & rft->fflags) == rft->fflags)
1359 kernfs_remove_by_name(kn, rft->name);
1365 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1366 * @r: The resource group with which the file is associated.
1367 * @name: Name of the file
1369 * The permissions of named resctrl file, directory, or link are modified
1370 * to not allow read, write, or execute by any user.
1372 * WARNING: This function is intended to communicate to the user that the
1373 * resctrl file has been locked down - that it is not relevant to the
1374 * particular state the system finds itself in. It should not be relied
1375 * on to protect from user access because after the file's permissions
1376 * are restricted the user can still change the permissions using chmod
1377 * from the command line.
1379 * Return: 0 on success, <0 on failure.
1381 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1383 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1384 struct kernfs_node *kn;
1387 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1391 switch (kernfs_type(kn)) {
1393 iattr.ia_mode = S_IFDIR;
1396 iattr.ia_mode = S_IFREG;
1399 iattr.ia_mode = S_IFLNK;
1403 ret = kernfs_setattr(kn, &iattr);
1409 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1410 * @r: The resource group with which the file is associated.
1411 * @name: Name of the file
1412 * @mask: Mask of permissions that should be restored
1414 * Restore the permissions of the named file. If @name is a directory the
1415 * permissions of its parent will be used.
1417 * Return: 0 on success, <0 on failure.
1419 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1422 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1423 struct kernfs_node *kn, *parent;
1424 struct rftype *rfts, *rft;
1427 rfts = res_common_files;
1428 len = ARRAY_SIZE(res_common_files);
1430 for (rft = rfts; rft < rfts + len; rft++) {
1431 if (!strcmp(rft->name, name))
1432 iattr.ia_mode = rft->mode & mask;
1435 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1439 switch (kernfs_type(kn)) {
1441 parent = kernfs_get_parent(kn);
1443 iattr.ia_mode |= parent->mode;
1446 iattr.ia_mode |= S_IFDIR;
1449 iattr.ia_mode |= S_IFREG;
1452 iattr.ia_mode |= S_IFLNK;
1456 ret = kernfs_setattr(kn, &iattr);
1461 static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
1462 unsigned long fflags)
1464 struct kernfs_node *kn_subdir;
1467 kn_subdir = kernfs_create_dir(kn_info, name,
1469 if (IS_ERR(kn_subdir))
1470 return PTR_ERR(kn_subdir);
1472 kernfs_get(kn_subdir);
1473 ret = rdtgroup_kn_set_ugid(kn_subdir);
1477 ret = rdtgroup_add_files(kn_subdir, fflags);
1479 kernfs_activate(kn_subdir);
1484 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
1486 struct rdt_resource *r;
1487 unsigned long fflags;
1491 /* create the directory */
1492 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
1493 if (IS_ERR(kn_info))
1494 return PTR_ERR(kn_info);
1495 kernfs_get(kn_info);
1497 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
1501 for_each_alloc_enabled_rdt_resource(r) {
1502 fflags = r->fflags | RF_CTRL_INFO;
1503 ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
1508 for_each_mon_enabled_rdt_resource(r) {
1509 fflags = r->fflags | RF_MON_INFO;
1510 sprintf(name, "%s_MON", r->name);
1511 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
1517 * This extra ref will be put in kernfs_remove() and guarantees
1518 * that @rdtgrp->kn is always accessible.
1520 kernfs_get(kn_info);
1522 ret = rdtgroup_kn_set_ugid(kn_info);
1526 kernfs_activate(kn_info);
1531 kernfs_remove(kn_info);
1536 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
1537 char *name, struct kernfs_node **dest_kn)
1539 struct kernfs_node *kn;
1542 /* create the directory */
1543 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1551 * This extra ref will be put in kernfs_remove() and guarantees
1552 * that @rdtgrp->kn is always accessible.
1556 ret = rdtgroup_kn_set_ugid(kn);
1560 kernfs_activate(kn);
1569 static void l3_qos_cfg_update(void *arg)
1573 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
1576 static void l2_qos_cfg_update(void *arg)
1580 wrmsrl(IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
1583 static inline bool is_mba_linear(void)
1585 return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear;
1588 static int set_cache_qos_cfg(int level, bool enable)
1590 void (*update)(void *arg);
1591 struct rdt_resource *r_l;
1592 cpumask_var_t cpu_mask;
1593 struct rdt_domain *d;
1596 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1599 if (level == RDT_RESOURCE_L3)
1600 update = l3_qos_cfg_update;
1601 else if (level == RDT_RESOURCE_L2)
1602 update = l2_qos_cfg_update;
1606 r_l = &rdt_resources_all[level];
1607 list_for_each_entry(d, &r_l->domains, list) {
1608 /* Pick one CPU from each domain instance to update MSR */
1609 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1612 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1613 if (cpumask_test_cpu(cpu, cpu_mask))
1615 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1616 smp_call_function_many(cpu_mask, update, &enable, 1);
1619 free_cpumask_var(cpu_mask);
1625 * Enable or disable the MBA software controller
1626 * which helps user specify bandwidth in MBps.
1627 * MBA software controller is supported only if
1628 * MBM is supported and MBA is in linear scale.
1630 static int set_mba_sc(bool mba_sc)
1632 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA];
1633 struct rdt_domain *d;
1635 if (!is_mbm_enabled() || !is_mba_linear() ||
1636 mba_sc == is_mba_sc(r))
1639 r->membw.mba_sc = mba_sc;
1640 list_for_each_entry(d, &r->domains, list)
1641 setup_default_ctrlval(r, d->ctrl_val, d->mbps_val);
1646 static int cdp_enable(int level, int data_type, int code_type)
1648 struct rdt_resource *r_ldata = &rdt_resources_all[data_type];
1649 struct rdt_resource *r_lcode = &rdt_resources_all[code_type];
1650 struct rdt_resource *r_l = &rdt_resources_all[level];
1653 if (!r_l->alloc_capable || !r_ldata->alloc_capable ||
1654 !r_lcode->alloc_capable)
1657 ret = set_cache_qos_cfg(level, true);
1659 r_l->alloc_enabled = false;
1660 r_ldata->alloc_enabled = true;
1661 r_lcode->alloc_enabled = true;
1666 static int cdpl3_enable(void)
1668 return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA,
1669 RDT_RESOURCE_L3CODE);
1672 static int cdpl2_enable(void)
1674 return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA,
1675 RDT_RESOURCE_L2CODE);
1678 static void cdp_disable(int level, int data_type, int code_type)
1680 struct rdt_resource *r = &rdt_resources_all[level];
1682 r->alloc_enabled = r->alloc_capable;
1684 if (rdt_resources_all[data_type].alloc_enabled) {
1685 rdt_resources_all[data_type].alloc_enabled = false;
1686 rdt_resources_all[code_type].alloc_enabled = false;
1687 set_cache_qos_cfg(level, false);
1691 static void cdpl3_disable(void)
1693 cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE);
1696 static void cdpl2_disable(void)
1698 cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE);
1701 static void cdp_disable_all(void)
1703 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
1705 if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
1709 static int parse_rdtgroupfs_options(char *data)
1711 char *token, *o = data;
1714 while ((token = strsep(&o, ",")) != NULL) {
1720 if (!strcmp(token, "cdp")) {
1721 ret = cdpl3_enable();
1724 } else if (!strcmp(token, "cdpl2")) {
1725 ret = cdpl2_enable();
1728 } else if (!strcmp(token, "mba_MBps")) {
1729 ret = set_mba_sc(true);
1741 pr_err("Invalid mount option \"%s\"\n", token);
1747 * We don't allow rdtgroup directories to be created anywhere
1748 * except the root directory. Thus when looking for the rdtgroup
1749 * structure for a kernfs node we are either looking at a directory,
1750 * in which case the rdtgroup structure is pointed at by the "priv"
1751 * field, otherwise we have a file, and need only look to the parent
1752 * to find the rdtgroup.
1754 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1756 if (kernfs_type(kn) == KERNFS_DIR) {
1758 * All the resource directories use "kn->priv"
1759 * to point to the "struct rdtgroup" for the
1760 * resource. "info" and its subdirectories don't
1761 * have rdtgroup structures, so return NULL here.
1763 if (kn == kn_info || kn->parent == kn_info)
1768 return kn->parent->priv;
1772 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1774 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1779 atomic_inc(&rdtgrp->waitcount);
1780 kernfs_break_active_protection(kn);
1782 mutex_lock(&rdtgroup_mutex);
1784 /* Was this group deleted while we waited? */
1785 if (rdtgrp->flags & RDT_DELETED)
1791 void rdtgroup_kn_unlock(struct kernfs_node *kn)
1793 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1798 mutex_unlock(&rdtgroup_mutex);
1800 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
1801 (rdtgrp->flags & RDT_DELETED)) {
1802 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
1803 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
1804 rdtgroup_pseudo_lock_remove(rdtgrp);
1805 kernfs_unbreak_active_protection(kn);
1806 kernfs_put(rdtgrp->kn);
1809 kernfs_unbreak_active_protection(kn);
1813 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1814 struct rdtgroup *prgrp,
1815 struct kernfs_node **mon_data_kn);
1817 static struct dentry *rdt_mount(struct file_system_type *fs_type,
1818 int flags, const char *unused_dev_name,
1821 struct rdt_domain *dom;
1822 struct rdt_resource *r;
1823 struct dentry *dentry;
1827 mutex_lock(&rdtgroup_mutex);
1829 * resctrl file system can only be mounted once.
1831 if (static_branch_unlikely(&rdt_enable_key)) {
1832 dentry = ERR_PTR(-EBUSY);
1836 ret = parse_rdtgroupfs_options(data);
1838 dentry = ERR_PTR(ret);
1844 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
1846 dentry = ERR_PTR(ret);
1850 if (rdt_mon_capable) {
1851 ret = mongroup_create_dir(rdtgroup_default.kn,
1855 dentry = ERR_PTR(ret);
1858 kernfs_get(kn_mongrp);
1860 ret = mkdir_mondata_all(rdtgroup_default.kn,
1861 &rdtgroup_default, &kn_mondata);
1863 dentry = ERR_PTR(ret);
1866 kernfs_get(kn_mondata);
1867 rdtgroup_default.mon.mon_data_kn = kn_mondata;
1870 ret = rdt_pseudo_lock_init();
1872 dentry = ERR_PTR(ret);
1876 dentry = kernfs_mount(fs_type, flags, rdt_root,
1877 RDTGROUP_SUPER_MAGIC, NULL);
1881 if (rdt_alloc_capable)
1882 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
1883 if (rdt_mon_capable)
1884 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
1886 if (rdt_alloc_capable || rdt_mon_capable)
1887 static_branch_enable_cpuslocked(&rdt_enable_key);
1889 if (is_mbm_enabled()) {
1890 r = &rdt_resources_all[RDT_RESOURCE_L3];
1891 list_for_each_entry(dom, &r->domains, list)
1892 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
1898 rdt_pseudo_lock_release();
1900 if (rdt_mon_capable)
1901 kernfs_remove(kn_mondata);
1903 if (rdt_mon_capable)
1904 kernfs_remove(kn_mongrp);
1906 kernfs_remove(kn_info);
1910 rdt_last_cmd_clear();
1911 mutex_unlock(&rdtgroup_mutex);
1917 static int reset_all_ctrls(struct rdt_resource *r)
1919 struct msr_param msr_param;
1920 cpumask_var_t cpu_mask;
1921 struct rdt_domain *d;
1924 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1929 msr_param.high = r->num_closid;
1932 * Disable resource control for this resource by setting all
1933 * CBMs in all domains to the maximum mask value. Pick one CPU
1934 * from each domain to update the MSRs below.
1936 list_for_each_entry(d, &r->domains, list) {
1937 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1939 for (i = 0; i < r->num_closid; i++)
1940 d->ctrl_val[i] = r->default_ctrl;
1943 /* Update CBM on this cpu if it's in cpu_mask. */
1944 if (cpumask_test_cpu(cpu, cpu_mask))
1945 rdt_ctrl_update(&msr_param);
1946 /* Update CBM on all other cpus in cpu_mask. */
1947 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
1950 free_cpumask_var(cpu_mask);
1955 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
1957 return (rdt_alloc_capable &&
1958 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
1961 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
1963 return (rdt_mon_capable &&
1964 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
1968 * Move tasks from one to the other group. If @from is NULL, then all tasks
1969 * in the systems are moved unconditionally (used for teardown).
1971 * If @mask is not NULL the cpus on which moved tasks are running are set
1972 * in that mask so the update smp function call is restricted to affected
1975 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
1976 struct cpumask *mask)
1978 struct task_struct *p, *t;
1980 read_lock(&tasklist_lock);
1981 for_each_process_thread(p, t) {
1982 if (!from || is_closid_match(t, from) ||
1983 is_rmid_match(t, from)) {
1984 t->closid = to->closid;
1985 t->rmid = to->mon.rmid;
1989 * This is safe on x86 w/o barriers as the ordering
1990 * of writing to task_cpu() and t->on_cpu is
1991 * reverse to the reading here. The detection is
1992 * inaccurate as tasks might move or schedule
1993 * before the smp function call takes place. In
1994 * such a case the function call is pointless, but
1995 * there is no other side effect.
1997 if (mask && t->on_cpu)
1998 cpumask_set_cpu(task_cpu(t), mask);
2002 read_unlock(&tasklist_lock);
2005 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2007 struct rdtgroup *sentry, *stmp;
2008 struct list_head *head;
2010 head = &rdtgrp->mon.crdtgrp_list;
2011 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2012 free_rmid(sentry->mon.rmid);
2013 list_del(&sentry->mon.crdtgrp_list);
2019 * Forcibly remove all of subdirectories under root.
2021 static void rmdir_all_sub(void)
2023 struct rdtgroup *rdtgrp, *tmp;
2025 /* Move all tasks to the default resource group */
2026 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2028 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2029 /* Free any child rmids */
2030 free_all_child_rdtgrp(rdtgrp);
2032 /* Remove each rdtgroup other than root */
2033 if (rdtgrp == &rdtgroup_default)
2036 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2037 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2038 rdtgroup_pseudo_lock_remove(rdtgrp);
2041 * Give any CPUs back to the default group. We cannot copy
2042 * cpu_online_mask because a CPU might have executed the
2043 * offline callback already, but is still marked online.
2045 cpumask_or(&rdtgroup_default.cpu_mask,
2046 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2048 free_rmid(rdtgrp->mon.rmid);
2050 kernfs_remove(rdtgrp->kn);
2051 list_del(&rdtgrp->rdtgroup_list);
2054 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2055 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2057 kernfs_remove(kn_info);
2058 kernfs_remove(kn_mongrp);
2059 kernfs_remove(kn_mondata);
2062 static void rdt_kill_sb(struct super_block *sb)
2064 struct rdt_resource *r;
2067 mutex_lock(&rdtgroup_mutex);
2071 /*Put everything back to default values. */
2072 for_each_alloc_enabled_rdt_resource(r)
2076 rdt_pseudo_lock_release();
2077 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2078 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2079 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2080 static_branch_disable_cpuslocked(&rdt_enable_key);
2082 mutex_unlock(&rdtgroup_mutex);
2086 static struct file_system_type rdt_fs_type = {
2089 .kill_sb = rdt_kill_sb,
2092 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2095 struct kernfs_node *kn;
2098 kn = __kernfs_create_file(parent_kn, name, 0444, 0,
2099 &kf_mondata_ops, priv, NULL, NULL);
2103 ret = rdtgroup_kn_set_ugid(kn);
2113 * Remove all subdirectories of mon_data of ctrl_mon groups
2114 * and monitor groups with given domain id.
2116 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
2118 struct rdtgroup *prgrp, *crgrp;
2121 if (!r->mon_enabled)
2124 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2125 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2126 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2128 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2129 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2133 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2134 struct rdt_domain *d,
2135 struct rdt_resource *r, struct rdtgroup *prgrp)
2137 union mon_data_bits priv;
2138 struct kernfs_node *kn;
2139 struct mon_evt *mevt;
2140 struct rmid_read rr;
2144 sprintf(name, "mon_%s_%02d", r->name, d->id);
2145 /* create the directory */
2146 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2151 * This extra ref will be put in kernfs_remove() and guarantees
2152 * that kn is always accessible.
2155 ret = rdtgroup_kn_set_ugid(kn);
2159 if (WARN_ON(list_empty(&r->evt_list))) {
2164 priv.u.rid = r->rid;
2165 priv.u.domid = d->id;
2166 list_for_each_entry(mevt, &r->evt_list, list) {
2167 priv.u.evtid = mevt->evtid;
2168 ret = mon_addfile(kn, mevt->name, priv.priv);
2172 if (is_mbm_event(mevt->evtid))
2173 mon_event_read(&rr, d, prgrp, mevt->evtid, true);
2175 kernfs_activate(kn);
2184 * Add all subdirectories of mon_data for "ctrl_mon" groups
2185 * and "monitor" groups with given domain id.
2187 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2188 struct rdt_domain *d)
2190 struct kernfs_node *parent_kn;
2191 struct rdtgroup *prgrp, *crgrp;
2192 struct list_head *head;
2194 if (!r->mon_enabled)
2197 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2198 parent_kn = prgrp->mon.mon_data_kn;
2199 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2201 head = &prgrp->mon.crdtgrp_list;
2202 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2203 parent_kn = crgrp->mon.mon_data_kn;
2204 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2209 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2210 struct rdt_resource *r,
2211 struct rdtgroup *prgrp)
2213 struct rdt_domain *dom;
2216 list_for_each_entry(dom, &r->domains, list) {
2217 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2226 * This creates a directory mon_data which contains the monitored data.
2228 * mon_data has one directory for each domain whic are named
2229 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2230 * with L3 domain looks as below:
2237 * Each domain directory has one file per event:
2242 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2243 struct rdtgroup *prgrp,
2244 struct kernfs_node **dest_kn)
2246 struct rdt_resource *r;
2247 struct kernfs_node *kn;
2251 * Create the mon_data directory first.
2253 ret = mongroup_create_dir(parent_kn, NULL, "mon_data", &kn);
2261 * Create the subdirectories for each domain. Note that all events
2262 * in a domain like L3 are grouped into a resource whose domain is L3
2264 for_each_mon_enabled_rdt_resource(r) {
2265 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2278 * cbm_ensure_valid - Enforce validity on provided CBM
2279 * @_val: Candidate CBM
2280 * @r: RDT resource to which the CBM belongs
2282 * The provided CBM represents all cache portions available for use. This
2283 * may be represented by a bitmap that does not consist of contiguous ones
2284 * and thus be an invalid CBM.
2285 * Here the provided CBM is forced to be a valid CBM by only considering
2286 * the first set of contiguous bits as valid and clearing all bits.
2287 * The intention here is to provide a valid default CBM with which a new
2288 * resource group is initialized. The user can follow this with a
2289 * modification to the CBM if the default does not satisfy the
2292 static void cbm_ensure_valid(u32 *_val, struct rdt_resource *r)
2295 * Convert the u32 _val to an unsigned long required by all the bit
2296 * operations within this function. No more than 32 bits of this
2297 * converted value can be accessed because all bit operations are
2298 * additionally provided with cbm_len that is initialized during
2299 * hardware enumeration using five bits from the EAX register and
2300 * thus never can exceed 32 bits.
2302 unsigned long *val = (unsigned long *)_val;
2303 unsigned int cbm_len = r->cache.cbm_len;
2304 unsigned long first_bit, zero_bit;
2309 first_bit = find_first_bit(val, cbm_len);
2310 zero_bit = find_next_zero_bit(val, cbm_len, first_bit);
2312 /* Clear any remaining bits to ensure contiguous region */
2313 bitmap_clear(val, zero_bit, cbm_len - zero_bit);
2317 * rdtgroup_init_alloc - Initialize the new RDT group's allocations
2319 * A new RDT group is being created on an allocation capable (CAT)
2320 * supporting system. Set this group up to start off with all usable
2321 * allocations. That is, all shareable and unused bits.
2323 * All-zero CBM is invalid. If there are no more shareable bits available
2324 * on any domain then the entire allocation will fail.
2326 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
2328 u32 used_b = 0, unused_b = 0;
2329 u32 closid = rdtgrp->closid;
2330 struct rdt_resource *r;
2331 enum rdtgrp_mode mode;
2332 struct rdt_domain *d;
2336 for_each_alloc_enabled_rdt_resource(r) {
2337 list_for_each_entry(d, &r->domains, list) {
2338 d->have_new_ctrl = false;
2339 d->new_ctrl = r->cache.shareable_bits;
2340 used_b = r->cache.shareable_bits;
2342 for (i = 0; i < r->num_closid; i++, ctrl++) {
2343 if (closid_allocated(i) && i != closid) {
2344 mode = rdtgroup_mode_by_closid(i);
2345 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
2348 if (mode == RDT_MODE_SHAREABLE)
2349 d->new_ctrl |= *ctrl;
2352 if (d->plr && d->plr->cbm > 0)
2353 used_b |= d->plr->cbm;
2354 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
2355 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
2356 d->new_ctrl |= unused_b;
2358 * Force the initial CBM to be valid, user can
2359 * modify the CBM based on system availability.
2361 cbm_ensure_valid(&d->new_ctrl, r);
2362 if (bitmap_weight((unsigned long *) &d->new_ctrl,
2364 r->cache.min_cbm_bits) {
2365 rdt_last_cmd_printf("no space on %s:%d\n",
2369 d->have_new_ctrl = true;
2373 for_each_alloc_enabled_rdt_resource(r) {
2374 ret = update_domains(r, rdtgrp->closid);
2376 rdt_last_cmd_puts("failed to initialize allocations\n");
2379 rdtgrp->mode = RDT_MODE_SHAREABLE;
2385 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
2386 struct kernfs_node *prgrp_kn,
2387 const char *name, umode_t mode,
2388 enum rdt_group_type rtype, struct rdtgroup **r)
2390 struct rdtgroup *prdtgrp, *rdtgrp;
2391 struct kernfs_node *kn;
2395 prdtgrp = rdtgroup_kn_lock_live(prgrp_kn);
2396 rdt_last_cmd_clear();
2399 rdt_last_cmd_puts("directory was removed\n");
2403 if (rtype == RDTMON_GROUP &&
2404 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2405 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
2407 rdt_last_cmd_puts("pseudo-locking in progress\n");
2411 /* allocate the rdtgroup. */
2412 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
2415 rdt_last_cmd_puts("kernel out of memory\n");
2419 rdtgrp->mon.parent = prdtgrp;
2420 rdtgrp->type = rtype;
2421 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
2423 /* kernfs creates the directory for rdtgrp */
2424 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
2427 rdt_last_cmd_puts("kernfs create error\n");
2433 * kernfs_remove() will drop the reference count on "kn" which
2434 * will free it. But we still need it to stick around for the
2435 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
2436 * here, which will be dropped inside rdtgroup_kn_unlock().
2440 ret = rdtgroup_kn_set_ugid(kn);
2442 rdt_last_cmd_puts("kernfs perm error\n");
2446 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
2447 ret = rdtgroup_add_files(kn, files);
2449 rdt_last_cmd_puts("kernfs fill error\n");
2453 if (rdt_mon_capable) {
2456 rdt_last_cmd_puts("out of RMIDs\n");
2459 rdtgrp->mon.rmid = ret;
2461 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
2463 rdt_last_cmd_puts("kernfs subdir error\n");
2467 kernfs_activate(kn);
2470 * The caller unlocks the prgrp_kn upon success.
2475 free_rmid(rdtgrp->mon.rmid);
2477 kernfs_remove(rdtgrp->kn);
2481 rdtgroup_kn_unlock(prgrp_kn);
2485 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
2487 kernfs_remove(rgrp->kn);
2488 free_rmid(rgrp->mon.rmid);
2493 * Create a monitor group under "mon_groups" directory of a control
2494 * and monitor group(ctrl_mon). This is a resource group
2495 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
2497 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
2498 struct kernfs_node *prgrp_kn,
2502 struct rdtgroup *rdtgrp, *prgrp;
2505 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP,
2510 prgrp = rdtgrp->mon.parent;
2511 rdtgrp->closid = prgrp->closid;
2514 * Add the rdtgrp to the list of rdtgrps the parent
2515 * ctrl_mon group has to track.
2517 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
2519 rdtgroup_kn_unlock(prgrp_kn);
2524 * These are rdtgroups created under the root directory. Can be used
2525 * to allocate and monitor resources.
2527 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
2528 struct kernfs_node *prgrp_kn,
2529 const char *name, umode_t mode)
2531 struct rdtgroup *rdtgrp;
2532 struct kernfs_node *kn;
2536 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP,
2542 ret = closid_alloc();
2544 rdt_last_cmd_puts("out of CLOSIDs\n");
2545 goto out_common_fail;
2550 rdtgrp->closid = closid;
2551 ret = rdtgroup_init_alloc(rdtgrp);
2555 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
2557 if (rdt_mon_capable) {
2559 * Create an empty mon_groups directory to hold the subset
2560 * of tasks and cpus to monitor.
2562 ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL);
2564 rdt_last_cmd_puts("kernfs subdir error\n");
2572 list_del(&rdtgrp->rdtgroup_list);
2574 closid_free(closid);
2576 mkdir_rdt_prepare_clean(rdtgrp);
2578 rdtgroup_kn_unlock(prgrp_kn);
2583 * We allow creating mon groups only with in a directory called "mon_groups"
2584 * which is present in every ctrl_mon group. Check if this is a valid
2585 * "mon_groups" directory.
2587 * 1. The directory should be named "mon_groups".
2588 * 2. The mon group itself should "not" be named "mon_groups".
2589 * This makes sure "mon_groups" directory always has a ctrl_mon group
2592 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
2594 return (!strcmp(kn->name, "mon_groups") &&
2595 strcmp(name, "mon_groups"));
2598 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
2601 /* Do not accept '\n' to avoid unparsable situation. */
2602 if (strchr(name, '\n'))
2606 * If the parent directory is the root directory and RDT
2607 * allocation is supported, add a control and monitoring
2610 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
2611 return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode);
2614 * If RDT monitoring is supported and the parent directory is a valid
2615 * "mon_groups" directory, add a monitoring subdirectory.
2617 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
2618 return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode);
2623 static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
2624 cpumask_var_t tmpmask)
2626 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
2629 /* Give any tasks back to the parent group */
2630 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
2632 /* Update per cpu rmid of the moved CPUs first */
2633 for_each_cpu(cpu, &rdtgrp->cpu_mask)
2634 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
2636 * Update the MSR on moved CPUs and CPUs which have moved
2637 * task running on them.
2639 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
2640 update_closid_rmid(tmpmask, NULL);
2642 rdtgrp->flags = RDT_DELETED;
2643 free_rmid(rdtgrp->mon.rmid);
2646 * Remove the rdtgrp from the parent ctrl_mon group's list
2648 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
2649 list_del(&rdtgrp->mon.crdtgrp_list);
2652 * one extra hold on this, will drop when we kfree(rdtgrp)
2653 * in rdtgroup_kn_unlock()
2656 kernfs_remove(rdtgrp->kn);
2661 static int rdtgroup_ctrl_remove(struct kernfs_node *kn,
2662 struct rdtgroup *rdtgrp)
2664 rdtgrp->flags = RDT_DELETED;
2665 list_del(&rdtgrp->rdtgroup_list);
2668 * one extra hold on this, will drop when we kfree(rdtgrp)
2669 * in rdtgroup_kn_unlock()
2672 kernfs_remove(rdtgrp->kn);
2676 static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
2677 cpumask_var_t tmpmask)
2681 /* Give any tasks back to the default group */
2682 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
2684 /* Give any CPUs back to the default group */
2685 cpumask_or(&rdtgroup_default.cpu_mask,
2686 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2688 /* Update per cpu closid and rmid of the moved CPUs first */
2689 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
2690 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
2691 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
2695 * Update the MSR on moved CPUs and CPUs which have moved
2696 * task running on them.
2698 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
2699 update_closid_rmid(tmpmask, NULL);
2701 closid_free(rdtgrp->closid);
2702 free_rmid(rdtgrp->mon.rmid);
2705 * Free all the child monitor group rmids.
2707 free_all_child_rdtgrp(rdtgrp);
2709 rdtgroup_ctrl_remove(kn, rdtgrp);
2714 static int rdtgroup_rmdir(struct kernfs_node *kn)
2716 struct kernfs_node *parent_kn = kn->parent;
2717 struct rdtgroup *rdtgrp;
2718 cpumask_var_t tmpmask;
2721 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
2724 rdtgrp = rdtgroup_kn_lock_live(kn);
2731 * If the rdtgroup is a ctrl_mon group and parent directory
2732 * is the root directory, remove the ctrl_mon group.
2734 * If the rdtgroup is a mon group and parent directory
2735 * is a valid "mon_groups" directory, remove the mon group.
2737 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn) {
2738 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2739 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
2740 ret = rdtgroup_ctrl_remove(kn, rdtgrp);
2742 ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask);
2744 } else if (rdtgrp->type == RDTMON_GROUP &&
2745 is_mon_groups(parent_kn, kn->name)) {
2746 ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask);
2752 rdtgroup_kn_unlock(kn);
2753 free_cpumask_var(tmpmask);
2757 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
2759 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
2760 seq_puts(seq, ",cdp");
2764 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
2765 .mkdir = rdtgroup_mkdir,
2766 .rmdir = rdtgroup_rmdir,
2767 .show_options = rdtgroup_show_options,
2770 static int __init rdtgroup_setup_root(void)
2774 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
2775 KERNFS_ROOT_CREATE_DEACTIVATED |
2776 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
2778 if (IS_ERR(rdt_root))
2779 return PTR_ERR(rdt_root);
2781 mutex_lock(&rdtgroup_mutex);
2783 rdtgroup_default.closid = 0;
2784 rdtgroup_default.mon.rmid = 0;
2785 rdtgroup_default.type = RDTCTRL_GROUP;
2786 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
2788 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
2790 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
2792 kernfs_destroy_root(rdt_root);
2796 rdtgroup_default.kn = rdt_root->kn;
2797 kernfs_activate(rdtgroup_default.kn);
2800 mutex_unlock(&rdtgroup_mutex);
2806 * rdtgroup_init - rdtgroup initialization
2808 * Setup resctrl file system including set up root, create mount point,
2809 * register rdtgroup filesystem, and initialize files under root directory.
2811 * Return: 0 on success or -errno
2813 int __init rdtgroup_init(void)
2817 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
2818 sizeof(last_cmd_status_buf));
2820 ret = rdtgroup_setup_root();
2824 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
2828 ret = register_filesystem(&rdt_fs_type);
2830 goto cleanup_mountpoint;
2833 * Adding the resctrl debugfs directory here may not be ideal since
2834 * it would let the resctrl debugfs directory appear on the debugfs
2835 * filesystem before the resctrl filesystem is mounted.
2836 * It may also be ok since that would enable debugging of RDT before
2837 * resctrl is mounted.
2838 * The reason why the debugfs directory is created here and not in
2839 * rdt_mount() is because rdt_mount() takes rdtgroup_mutex and
2840 * during the debugfs directory creation also &sb->s_type->i_mutex_key
2841 * (the lockdep class of inode->i_rwsem). Other filesystem
2842 * interactions (eg. SyS_getdents) have the lock ordering:
2843 * &sb->s_type->i_mutex_key --> &mm->mmap_sem
2844 * During mmap(), called with &mm->mmap_sem, the rdtgroup_mutex
2845 * is taken, thus creating dependency:
2846 * &mm->mmap_sem --> rdtgroup_mutex for the latter that can cause
2847 * issues considering the other two lock dependencies.
2848 * By creating the debugfs directory here we avoid a dependency
2849 * that may cause deadlock (even though file operations cannot
2850 * occur until the filesystem is mounted, but I do not know how to
2851 * tell lockdep that).
2853 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
2858 sysfs_remove_mount_point(fs_kobj, "resctrl");
2860 kernfs_destroy_root(rdt_root);
2865 void __exit rdtgroup_exit(void)
2867 debugfs_remove_recursive(debugfs_resctrl);
2868 unregister_filesystem(&rdt_fs_type);
2869 sysfs_remove_mount_point(fs_kobj, "resctrl");
2870 kernfs_destroy_root(rdt_root);