1 // SPDX-License-Identifier: GPL-2.0-only
3 * User interface for Resource Alloction in Resource Director Technology(RDT)
5 * Copyright (C) 2016 Intel Corporation
7 * Author: Fenghua Yu <fenghua.yu@intel.com>
9 * More information about RDT be found in the Intel (R) x86 Architecture
10 * Software Developer Manual.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/cacheinfo.h>
16 #include <linux/cpu.h>
17 #include <linux/debugfs.h>
19 #include <linux/fs_parser.h>
20 #include <linux/sysfs.h>
21 #include <linux/kernfs.h>
22 #include <linux/seq_buf.h>
23 #include <linux/seq_file.h>
24 #include <linux/sched/signal.h>
25 #include <linux/sched/task.h>
26 #include <linux/slab.h>
27 #include <linux/task_work.h>
28 #include <linux/user_namespace.h>
30 #include <uapi/linux/magic.h>
32 #include <asm/resctrl.h>
35 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
36 DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
37 DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
38 static struct kernfs_root *rdt_root;
39 struct rdtgroup rdtgroup_default;
40 LIST_HEAD(rdt_all_groups);
42 /* Kernel fs node for "info" directory under root */
43 static struct kernfs_node *kn_info;
45 /* Kernel fs node for "mon_groups" directory under root */
46 static struct kernfs_node *kn_mongrp;
48 /* Kernel fs node for "mon_data" directory under root */
49 static struct kernfs_node *kn_mondata;
51 static struct seq_buf last_cmd_status;
52 static char last_cmd_status_buf[512];
54 struct dentry *debugfs_resctrl;
56 void rdt_last_cmd_clear(void)
58 lockdep_assert_held(&rdtgroup_mutex);
59 seq_buf_clear(&last_cmd_status);
62 void rdt_last_cmd_puts(const char *s)
64 lockdep_assert_held(&rdtgroup_mutex);
65 seq_buf_puts(&last_cmd_status, s);
68 void rdt_last_cmd_printf(const char *fmt, ...)
73 lockdep_assert_held(&rdtgroup_mutex);
74 seq_buf_vprintf(&last_cmd_status, fmt, ap);
79 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
80 * we can keep a bitmap of free CLOSIDs in a single integer.
82 * Using a global CLOSID across all resources has some advantages and
84 * + We can simply set "current->closid" to assign a task to a resource
86 * + Context switch code can avoid extra memory references deciding which
87 * CLOSID to load into the PQR_ASSOC MSR
88 * - We give up some options in configuring resource groups across multi-socket
90 * - Our choices on how to configure each resource become progressively more
91 * limited as the number of resources grows.
93 static int closid_free_map;
94 static int closid_free_map_len;
96 int closids_supported(void)
98 return closid_free_map_len;
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;
114 closid_free_map_len = rdt_min_closid;
117 static int closid_alloc(void)
119 u32 closid = ffs(closid_free_map);
124 closid_free_map &= ~(1 << closid);
129 void closid_free(int closid)
131 closid_free_map |= 1 << closid;
135 * closid_allocated - test if provided closid is in use
136 * @closid: closid to be tested
138 * Return: true if @closid is currently associated with a resource group,
139 * false if @closid is free
141 static bool closid_allocated(unsigned int closid)
143 return (closid_free_map & (1 << closid)) == 0;
147 * rdtgroup_mode_by_closid - Return mode of resource group with closid
148 * @closid: closid if the resource group
150 * Each resource group is associated with a @closid. Here the mode
151 * of a resource group can be queried by searching for it using its closid.
153 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
155 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
157 struct rdtgroup *rdtgrp;
159 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
160 if (rdtgrp->closid == closid)
164 return RDT_NUM_MODES;
167 static const char * const rdt_mode_str[] = {
168 [RDT_MODE_SHAREABLE] = "shareable",
169 [RDT_MODE_EXCLUSIVE] = "exclusive",
170 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
171 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
175 * rdtgroup_mode_str - Return the string representation of mode
176 * @mode: the resource group mode as &enum rdtgroup_mode
178 * Return: string representation of valid mode, "unknown" otherwise
180 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
182 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
185 return rdt_mode_str[mode];
188 /* set uid and gid of rdtgroup dirs and files to that of the creator */
189 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
191 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
192 .ia_uid = current_fsuid(),
193 .ia_gid = current_fsgid(), };
195 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
196 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
199 return kernfs_setattr(kn, &iattr);
202 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
204 struct kernfs_node *kn;
207 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
208 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
209 0, rft->kf_ops, rft, NULL, NULL);
213 ret = rdtgroup_kn_set_ugid(kn);
222 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
224 struct kernfs_open_file *of = m->private;
225 struct rftype *rft = of->kn->priv;
228 return rft->seq_show(of, m, arg);
232 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
233 size_t nbytes, loff_t off)
235 struct rftype *rft = of->kn->priv;
238 return rft->write(of, buf, nbytes, off);
243 static struct kernfs_ops rdtgroup_kf_single_ops = {
244 .atomic_write_len = PAGE_SIZE,
245 .write = rdtgroup_file_write,
246 .seq_show = rdtgroup_seqfile_show,
249 static struct kernfs_ops kf_mondata_ops = {
250 .atomic_write_len = PAGE_SIZE,
251 .seq_show = rdtgroup_mondata_show,
254 static bool is_cpu_list(struct kernfs_open_file *of)
256 struct rftype *rft = of->kn->priv;
258 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
261 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
262 struct seq_file *s, void *v)
264 struct rdtgroup *rdtgrp;
265 struct cpumask *mask;
268 rdtgrp = rdtgroup_kn_lock_live(of->kn);
271 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
272 if (!rdtgrp->plr->d) {
273 rdt_last_cmd_clear();
274 rdt_last_cmd_puts("Cache domain offline\n");
277 mask = &rdtgrp->plr->d->cpu_mask;
278 seq_printf(s, is_cpu_list(of) ?
279 "%*pbl\n" : "%*pb\n",
280 cpumask_pr_args(mask));
283 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
284 cpumask_pr_args(&rdtgrp->cpu_mask));
289 rdtgroup_kn_unlock(of->kn);
295 * This is safe against resctrl_sched_in() called from __switch_to()
296 * because __switch_to() is executed with interrupts disabled. A local call
297 * from update_closid_rmid() is proteced against __switch_to() because
298 * preemption is disabled.
300 static void update_cpu_closid_rmid(void *info)
302 struct rdtgroup *r = info;
305 this_cpu_write(pqr_state.default_closid, r->closid);
306 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
310 * We cannot unconditionally write the MSR because the current
311 * executing task might have its own closid selected. Just reuse
312 * the context switch code.
318 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
320 * Per task closids/rmids must have been set up before calling this function.
323 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
327 if (cpumask_test_cpu(cpu, cpu_mask))
328 update_cpu_closid_rmid(r);
329 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
333 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
334 cpumask_var_t tmpmask)
336 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
337 struct list_head *head;
339 /* Check whether cpus belong to parent ctrl group */
340 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
341 if (cpumask_weight(tmpmask)) {
342 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
346 /* Check whether cpus are dropped from this group */
347 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
348 if (cpumask_weight(tmpmask)) {
349 /* Give any dropped cpus to parent rdtgroup */
350 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
351 update_closid_rmid(tmpmask, prgrp);
355 * If we added cpus, remove them from previous group that owned them
356 * and update per-cpu rmid
358 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
359 if (cpumask_weight(tmpmask)) {
360 head = &prgrp->mon.crdtgrp_list;
361 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
364 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
367 update_closid_rmid(tmpmask, rdtgrp);
370 /* Done pushing/pulling - update this group with new mask */
371 cpumask_copy(&rdtgrp->cpu_mask, newmask);
376 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
378 struct rdtgroup *crgrp;
380 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
381 /* update the child mon group masks as well*/
382 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
383 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
386 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
387 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
389 struct rdtgroup *r, *crgrp;
390 struct list_head *head;
392 /* Check whether cpus are dropped from this group */
393 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
394 if (cpumask_weight(tmpmask)) {
395 /* Can't drop from default group */
396 if (rdtgrp == &rdtgroup_default) {
397 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
401 /* Give any dropped cpus to rdtgroup_default */
402 cpumask_or(&rdtgroup_default.cpu_mask,
403 &rdtgroup_default.cpu_mask, tmpmask);
404 update_closid_rmid(tmpmask, &rdtgroup_default);
408 * If we added cpus, remove them from previous group and
409 * the prev group's child groups that owned them
410 * and update per-cpu closid/rmid.
412 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
413 if (cpumask_weight(tmpmask)) {
414 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
417 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
418 if (cpumask_weight(tmpmask1))
419 cpumask_rdtgrp_clear(r, tmpmask1);
421 update_closid_rmid(tmpmask, rdtgrp);
424 /* Done pushing/pulling - update this group with new mask */
425 cpumask_copy(&rdtgrp->cpu_mask, newmask);
428 * Clear child mon group masks since there is a new parent mask
429 * now and update the rmid for the cpus the child lost.
431 head = &rdtgrp->mon.crdtgrp_list;
432 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
433 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
434 update_closid_rmid(tmpmask, rdtgrp);
435 cpumask_clear(&crgrp->cpu_mask);
441 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
442 char *buf, size_t nbytes, loff_t off)
444 cpumask_var_t tmpmask, newmask, tmpmask1;
445 struct rdtgroup *rdtgrp;
451 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
453 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
454 free_cpumask_var(tmpmask);
457 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
458 free_cpumask_var(tmpmask);
459 free_cpumask_var(newmask);
463 rdtgrp = rdtgroup_kn_lock_live(of->kn);
469 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
470 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
472 rdt_last_cmd_puts("Pseudo-locking in progress\n");
477 ret = cpulist_parse(buf, newmask);
479 ret = cpumask_parse(buf, newmask);
482 rdt_last_cmd_puts("Bad CPU list/mask\n");
486 /* check that user didn't specify any offline cpus */
487 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
488 if (cpumask_weight(tmpmask)) {
490 rdt_last_cmd_puts("Can only assign online CPUs\n");
494 if (rdtgrp->type == RDTCTRL_GROUP)
495 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
496 else if (rdtgrp->type == RDTMON_GROUP)
497 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
502 rdtgroup_kn_unlock(of->kn);
503 free_cpumask_var(tmpmask);
504 free_cpumask_var(newmask);
505 free_cpumask_var(tmpmask1);
507 return ret ?: nbytes;
510 struct task_move_callback {
511 struct callback_head work;
512 struct rdtgroup *rdtgrp;
515 static void move_myself(struct callback_head *head)
517 struct task_move_callback *callback;
518 struct rdtgroup *rdtgrp;
520 callback = container_of(head, struct task_move_callback, work);
521 rdtgrp = callback->rdtgrp;
524 * If resource group was deleted before this task work callback
525 * was invoked, then assign the task to root group and free the
528 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
529 (rdtgrp->flags & RDT_DELETED)) {
535 if (unlikely(current->flags & PF_EXITING))
539 /* update PQR_ASSOC MSR to make resource group go into effect */
547 static int __rdtgroup_move_task(struct task_struct *tsk,
548 struct rdtgroup *rdtgrp)
550 struct task_move_callback *callback;
553 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
556 callback->work.func = move_myself;
557 callback->rdtgrp = rdtgrp;
560 * Take a refcount, so rdtgrp cannot be freed before the
561 * callback has been invoked.
563 atomic_inc(&rdtgrp->waitcount);
564 ret = task_work_add(tsk, &callback->work, true);
567 * Task is exiting. Drop the refcount and free the callback.
568 * No need to check the refcount as the group cannot be
569 * deleted before the write function unlocks rdtgroup_mutex.
571 atomic_dec(&rdtgrp->waitcount);
573 rdt_last_cmd_puts("Task exited\n");
576 * For ctrl_mon groups move both closid and rmid.
577 * For monitor groups, can move the tasks only from
578 * their parent CTRL group.
580 if (rdtgrp->type == RDTCTRL_GROUP) {
581 tsk->closid = rdtgrp->closid;
582 tsk->rmid = rdtgrp->mon.rmid;
583 } else if (rdtgrp->type == RDTMON_GROUP) {
584 if (rdtgrp->mon.parent->closid == tsk->closid) {
585 tsk->rmid = rdtgrp->mon.rmid;
587 rdt_last_cmd_puts("Can't move task to different control group\n");
596 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
599 * Return: 1 if tasks have been assigned to @r, 0 otherwise
601 int rdtgroup_tasks_assigned(struct rdtgroup *r)
603 struct task_struct *p, *t;
606 lockdep_assert_held(&rdtgroup_mutex);
609 for_each_process_thread(p, t) {
610 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
611 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) {
621 static int rdtgroup_task_write_permission(struct task_struct *task,
622 struct kernfs_open_file *of)
624 const struct cred *tcred = get_task_cred(task);
625 const struct cred *cred = current_cred();
629 * Even if we're attaching all tasks in the thread group, we only
630 * need to check permissions on one of them.
632 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
633 !uid_eq(cred->euid, tcred->uid) &&
634 !uid_eq(cred->euid, tcred->suid)) {
635 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
643 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
644 struct kernfs_open_file *of)
646 struct task_struct *tsk;
651 tsk = find_task_by_vpid(pid);
654 rdt_last_cmd_printf("No task %d\n", pid);
661 get_task_struct(tsk);
664 ret = rdtgroup_task_write_permission(tsk, of);
666 ret = __rdtgroup_move_task(tsk, rdtgrp);
668 put_task_struct(tsk);
672 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
673 char *buf, size_t nbytes, loff_t off)
675 struct rdtgroup *rdtgrp;
679 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
681 rdtgrp = rdtgroup_kn_lock_live(of->kn);
683 rdtgroup_kn_unlock(of->kn);
686 rdt_last_cmd_clear();
688 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
689 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
691 rdt_last_cmd_puts("Pseudo-locking in progress\n");
695 ret = rdtgroup_move_task(pid, rdtgrp, of);
698 rdtgroup_kn_unlock(of->kn);
700 return ret ?: nbytes;
703 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
705 struct task_struct *p, *t;
708 for_each_process_thread(p, t) {
709 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
710 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid))
711 seq_printf(s, "%d\n", t->pid);
716 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
717 struct seq_file *s, void *v)
719 struct rdtgroup *rdtgrp;
722 rdtgrp = rdtgroup_kn_lock_live(of->kn);
724 show_rdt_tasks(rdtgrp, s);
727 rdtgroup_kn_unlock(of->kn);
732 #ifdef CONFIG_PROC_CPU_RESCTRL
735 * A task can only be part of one resctrl control group and of one monitor
736 * group which is associated to that control group.
741 * resctrl is not available.
746 * Task is part of the root resctrl control group, and it is not associated
747 * to any monitor group.
752 * Task is part of the root resctrl control group and monitor group mon0.
757 * Task is part of resctrl control group group0, and it is not associated
758 * to any monitor group.
763 * Task is part of resctrl control group group0 and monitor group mon1.
765 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
766 struct pid *pid, struct task_struct *tsk)
768 struct rdtgroup *rdtg;
771 mutex_lock(&rdtgroup_mutex);
773 /* Return empty if resctrl has not been mounted. */
774 if (!static_branch_unlikely(&rdt_enable_key)) {
775 seq_puts(s, "res:\nmon:\n");
779 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
780 struct rdtgroup *crg;
783 * Task information is only relevant for shareable
784 * and exclusive groups.
786 if (rdtg->mode != RDT_MODE_SHAREABLE &&
787 rdtg->mode != RDT_MODE_EXCLUSIVE)
790 if (rdtg->closid != tsk->closid)
793 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
796 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
798 if (tsk->rmid != crg->mon.rmid)
800 seq_printf(s, "%s", crg->kn->name);
807 * The above search should succeed. Otherwise return
812 mutex_unlock(&rdtgroup_mutex);
818 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
819 struct seq_file *seq, void *v)
823 mutex_lock(&rdtgroup_mutex);
824 len = seq_buf_used(&last_cmd_status);
826 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
828 seq_puts(seq, "ok\n");
829 mutex_unlock(&rdtgroup_mutex);
833 static int rdt_num_closids_show(struct kernfs_open_file *of,
834 struct seq_file *seq, void *v)
836 struct rdt_resource *r = of->kn->parent->priv;
838 seq_printf(seq, "%d\n", r->num_closid);
842 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
843 struct seq_file *seq, void *v)
845 struct rdt_resource *r = of->kn->parent->priv;
847 seq_printf(seq, "%x\n", r->default_ctrl);
851 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
852 struct seq_file *seq, void *v)
854 struct rdt_resource *r = of->kn->parent->priv;
856 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
860 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
861 struct seq_file *seq, void *v)
863 struct rdt_resource *r = of->kn->parent->priv;
865 seq_printf(seq, "%x\n", r->cache.shareable_bits);
870 * rdt_bit_usage_show - Display current usage of resources
872 * A domain is a shared resource that can now be allocated differently. Here
873 * we display the current regions of the domain as an annotated bitmask.
874 * For each domain of this resource its allocation bitmask
875 * is annotated as below to indicate the current usage of the corresponding bit:
876 * 0 - currently unused
877 * X - currently available for sharing and used by software and hardware
878 * H - currently used by hardware only but available for software use
879 * S - currently used and shareable by software only
880 * E - currently used exclusively by one resource group
881 * P - currently pseudo-locked by one resource group
883 static int rdt_bit_usage_show(struct kernfs_open_file *of,
884 struct seq_file *seq, void *v)
886 struct rdt_resource *r = of->kn->parent->priv;
888 * Use unsigned long even though only 32 bits are used to ensure
889 * test_bit() is used safely.
891 unsigned long sw_shareable = 0, hw_shareable = 0;
892 unsigned long exclusive = 0, pseudo_locked = 0;
893 struct rdt_domain *dom;
894 int i, hwb, swb, excl, psl;
895 enum rdtgrp_mode mode;
899 mutex_lock(&rdtgroup_mutex);
900 hw_shareable = r->cache.shareable_bits;
901 list_for_each_entry(dom, &r->domains, list) {
904 ctrl = dom->ctrl_val;
907 seq_printf(seq, "%d=", dom->id);
908 for (i = 0; i < closids_supported(); i++, ctrl++) {
909 if (!closid_allocated(i))
911 mode = rdtgroup_mode_by_closid(i);
913 case RDT_MODE_SHAREABLE:
914 sw_shareable |= *ctrl;
916 case RDT_MODE_EXCLUSIVE:
919 case RDT_MODE_PSEUDO_LOCKSETUP:
921 * RDT_MODE_PSEUDO_LOCKSETUP is possible
922 * here but not included since the CBM
923 * associated with this CLOSID in this mode
924 * is not initialized and no task or cpu can be
925 * assigned this CLOSID.
928 case RDT_MODE_PSEUDO_LOCKED:
931 "invalid mode for closid %d\n", i);
935 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
936 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
937 hwb = test_bit(i, &hw_shareable);
938 swb = test_bit(i, &sw_shareable);
939 excl = test_bit(i, &exclusive);
940 psl = test_bit(i, &pseudo_locked);
943 else if (hwb && !swb)
945 else if (!hwb && swb)
951 else /* Unused bits remain */
957 mutex_unlock(&rdtgroup_mutex);
961 static int rdt_min_bw_show(struct kernfs_open_file *of,
962 struct seq_file *seq, void *v)
964 struct rdt_resource *r = of->kn->parent->priv;
966 seq_printf(seq, "%u\n", r->membw.min_bw);
970 static int rdt_num_rmids_show(struct kernfs_open_file *of,
971 struct seq_file *seq, void *v)
973 struct rdt_resource *r = of->kn->parent->priv;
975 seq_printf(seq, "%d\n", r->num_rmid);
980 static int rdt_mon_features_show(struct kernfs_open_file *of,
981 struct seq_file *seq, void *v)
983 struct rdt_resource *r = of->kn->parent->priv;
984 struct mon_evt *mevt;
986 list_for_each_entry(mevt, &r->evt_list, list)
987 seq_printf(seq, "%s\n", mevt->name);
992 static int rdt_bw_gran_show(struct kernfs_open_file *of,
993 struct seq_file *seq, void *v)
995 struct rdt_resource *r = of->kn->parent->priv;
997 seq_printf(seq, "%u\n", r->membw.bw_gran);
1001 static int rdt_delay_linear_show(struct kernfs_open_file *of,
1002 struct seq_file *seq, void *v)
1004 struct rdt_resource *r = of->kn->parent->priv;
1006 seq_printf(seq, "%u\n", r->membw.delay_linear);
1010 static int max_threshold_occ_show(struct kernfs_open_file *of,
1011 struct seq_file *seq, void *v)
1013 struct rdt_resource *r = of->kn->parent->priv;
1015 seq_printf(seq, "%u\n", resctrl_cqm_threshold * r->mon_scale);
1020 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1021 char *buf, size_t nbytes, loff_t off)
1023 struct rdt_resource *r = of->kn->parent->priv;
1027 ret = kstrtouint(buf, 0, &bytes);
1031 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
1034 resctrl_cqm_threshold = bytes / r->mon_scale;
1040 * rdtgroup_mode_show - Display mode of this resource group
1042 static int rdtgroup_mode_show(struct kernfs_open_file *of,
1043 struct seq_file *s, void *v)
1045 struct rdtgroup *rdtgrp;
1047 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1049 rdtgroup_kn_unlock(of->kn);
1053 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1055 rdtgroup_kn_unlock(of->kn);
1060 * rdt_cdp_peer_get - Retrieve CDP peer if it exists
1061 * @r: RDT resource to which RDT domain @d belongs
1062 * @d: Cache instance for which a CDP peer is requested
1063 * @r_cdp: RDT resource that shares hardware with @r (RDT resource peer)
1064 * Used to return the result.
1065 * @d_cdp: RDT domain that shares hardware with @d (RDT domain peer)
1066 * Used to return the result.
1068 * RDT resources are managed independently and by extension the RDT domains
1069 * (RDT resource instances) are managed independently also. The Code and
1070 * Data Prioritization (CDP) RDT resources, while managed independently,
1071 * could refer to the same underlying hardware. For example,
1072 * RDT_RESOURCE_L2CODE and RDT_RESOURCE_L2DATA both refer to the L2 cache.
1074 * When provided with an RDT resource @r and an instance of that RDT
1075 * resource @d rdt_cdp_peer_get() will return if there is a peer RDT
1076 * resource and the exact instance that shares the same hardware.
1078 * Return: 0 if a CDP peer was found, <0 on error or if no CDP peer exists.
1079 * If a CDP peer was found, @r_cdp will point to the peer RDT resource
1080 * and @d_cdp will point to the peer RDT domain.
1082 static int rdt_cdp_peer_get(struct rdt_resource *r, struct rdt_domain *d,
1083 struct rdt_resource **r_cdp,
1084 struct rdt_domain **d_cdp)
1086 struct rdt_resource *_r_cdp = NULL;
1087 struct rdt_domain *_d_cdp = NULL;
1091 case RDT_RESOURCE_L3DATA:
1092 _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3CODE];
1094 case RDT_RESOURCE_L3CODE:
1095 _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3DATA];
1097 case RDT_RESOURCE_L2DATA:
1098 _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2CODE];
1100 case RDT_RESOURCE_L2CODE:
1101 _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2DATA];
1109 * When a new CPU comes online and CDP is enabled then the new
1110 * RDT domains (if any) associated with both CDP RDT resources
1111 * are added in the same CPU online routine while the
1112 * rdtgroup_mutex is held. It should thus not happen for one
1113 * RDT domain to exist and be associated with its RDT CDP
1114 * resource but there is no RDT domain associated with the
1115 * peer RDT CDP resource. Hence the WARN.
1117 _d_cdp = rdt_find_domain(_r_cdp, d->id, NULL);
1118 if (WARN_ON(IS_ERR_OR_NULL(_d_cdp))) {
1131 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
1132 * @r: Resource to which domain instance @d belongs.
1133 * @d: The domain instance for which @closid is being tested.
1134 * @cbm: Capacity bitmask being tested.
1135 * @closid: Intended closid for @cbm.
1136 * @exclusive: Only check if overlaps with exclusive resource groups
1138 * Checks if provided @cbm intended to be used for @closid on domain
1139 * @d overlaps with any other closids or other hardware usage associated
1140 * with this domain. If @exclusive is true then only overlaps with
1141 * resource groups in exclusive mode will be considered. If @exclusive
1142 * is false then overlaps with any resource group or hardware entities
1143 * will be considered.
1145 * @cbm is unsigned long, even if only 32 bits are used, to make the
1146 * bitmap functions work correctly.
1148 * Return: false if CBM does not overlap, true if it does.
1150 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1151 unsigned long cbm, int closid, bool exclusive)
1153 enum rdtgrp_mode mode;
1154 unsigned long ctrl_b;
1158 /* Check for any overlap with regions used by hardware directly */
1160 ctrl_b = r->cache.shareable_bits;
1161 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
1165 /* Check for overlap with other resource groups */
1167 for (i = 0; i < closids_supported(); i++, ctrl++) {
1169 mode = rdtgroup_mode_by_closid(i);
1170 if (closid_allocated(i) && i != closid &&
1171 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1172 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1174 if (mode == RDT_MODE_EXCLUSIVE)
1187 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
1188 * @r: Resource to which domain instance @d belongs.
1189 * @d: The domain instance for which @closid is being tested.
1190 * @cbm: Capacity bitmask being tested.
1191 * @closid: Intended closid for @cbm.
1192 * @exclusive: Only check if overlaps with exclusive resource groups
1194 * Resources that can be allocated using a CBM can use the CBM to control
1195 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1196 * for overlap. Overlap test is not limited to the specific resource for
1197 * which the CBM is intended though - when dealing with CDP resources that
1198 * share the underlying hardware the overlap check should be performed on
1199 * the CDP resource sharing the hardware also.
1201 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1204 * Return: true if CBM overlap detected, false if there is no overlap
1206 bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1207 unsigned long cbm, int closid, bool exclusive)
1209 struct rdt_resource *r_cdp;
1210 struct rdt_domain *d_cdp;
1212 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, exclusive))
1215 if (rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp) < 0)
1218 return __rdtgroup_cbm_overlaps(r_cdp, d_cdp, cbm, closid, exclusive);
1222 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1224 * An exclusive resource group implies that there should be no sharing of
1225 * its allocated resources. At the time this group is considered to be
1226 * exclusive this test can determine if its current schemata supports this
1227 * setting by testing for overlap with all other resource groups.
1229 * Return: true if resource group can be exclusive, false if there is overlap
1230 * with allocations of other resource groups and thus this resource group
1231 * cannot be exclusive.
1233 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1235 int closid = rdtgrp->closid;
1236 struct rdt_resource *r;
1237 bool has_cache = false;
1238 struct rdt_domain *d;
1240 for_each_alloc_enabled_rdt_resource(r) {
1241 if (r->rid == RDT_RESOURCE_MBA)
1244 list_for_each_entry(d, &r->domains, list) {
1245 if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid],
1246 rdtgrp->closid, false)) {
1247 rdt_last_cmd_puts("Schemata overlaps\n");
1254 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
1262 * rdtgroup_mode_write - Modify the resource group's mode
1265 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1266 char *buf, size_t nbytes, loff_t off)
1268 struct rdtgroup *rdtgrp;
1269 enum rdtgrp_mode mode;
1272 /* Valid input requires a trailing newline */
1273 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1275 buf[nbytes - 1] = '\0';
1277 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1279 rdtgroup_kn_unlock(of->kn);
1283 rdt_last_cmd_clear();
1285 mode = rdtgrp->mode;
1287 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1288 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1289 (!strcmp(buf, "pseudo-locksetup") &&
1290 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1291 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1294 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1295 rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
1300 if (!strcmp(buf, "shareable")) {
1301 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1302 ret = rdtgroup_locksetup_exit(rdtgrp);
1306 rdtgrp->mode = RDT_MODE_SHAREABLE;
1307 } else if (!strcmp(buf, "exclusive")) {
1308 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1312 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1313 ret = rdtgroup_locksetup_exit(rdtgrp);
1317 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1318 } else if (!strcmp(buf, "pseudo-locksetup")) {
1319 ret = rdtgroup_locksetup_enter(rdtgrp);
1322 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1324 rdt_last_cmd_puts("Unknown or unsupported mode\n");
1329 rdtgroup_kn_unlock(of->kn);
1330 return ret ?: nbytes;
1334 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1335 * @r: RDT resource to which @d belongs.
1336 * @d: RDT domain instance.
1337 * @cbm: bitmask for which the size should be computed.
1339 * The bitmask provided associated with the RDT domain instance @d will be
1340 * translated into how many bytes it represents. The size in bytes is
1341 * computed by first dividing the total cache size by the CBM length to
1342 * determine how many bytes each bit in the bitmask represents. The result
1343 * is multiplied with the number of bits set in the bitmask.
1345 * @cbm is unsigned long, even if only 32 bits are used to make the
1346 * bitmap functions work correctly.
1348 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1349 struct rdt_domain *d, unsigned long cbm)
1351 struct cpu_cacheinfo *ci;
1352 unsigned int size = 0;
1355 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1356 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1357 for (i = 0; i < ci->num_leaves; i++) {
1358 if (ci->info_list[i].level == r->cache_level) {
1359 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1368 * rdtgroup_size_show - Display size in bytes of allocated regions
1370 * The "size" file mirrors the layout of the "schemata" file, printing the
1371 * size in bytes of each region instead of the capacity bitmask.
1374 static int rdtgroup_size_show(struct kernfs_open_file *of,
1375 struct seq_file *s, void *v)
1377 struct rdtgroup *rdtgrp;
1378 struct rdt_resource *r;
1379 struct rdt_domain *d;
1385 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1387 rdtgroup_kn_unlock(of->kn);
1391 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1392 if (!rdtgrp->plr->d) {
1393 rdt_last_cmd_clear();
1394 rdt_last_cmd_puts("Cache domain offline\n");
1397 seq_printf(s, "%*s:", max_name_width,
1398 rdtgrp->plr->r->name);
1399 size = rdtgroup_cbm_to_size(rdtgrp->plr->r,
1402 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1407 for_each_alloc_enabled_rdt_resource(r) {
1409 seq_printf(s, "%*s:", max_name_width, r->name);
1410 list_for_each_entry(d, &r->domains, list) {
1413 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1416 ctrl = (!is_mba_sc(r) ?
1417 d->ctrl_val[rdtgrp->closid] :
1418 d->mbps_val[rdtgrp->closid]);
1419 if (r->rid == RDT_RESOURCE_MBA)
1422 size = rdtgroup_cbm_to_size(r, d, ctrl);
1424 seq_printf(s, "%d=%u", d->id, size);
1431 rdtgroup_kn_unlock(of->kn);
1436 /* rdtgroup information files for one cache resource. */
1437 static struct rftype res_common_files[] = {
1439 .name = "last_cmd_status",
1441 .kf_ops = &rdtgroup_kf_single_ops,
1442 .seq_show = rdt_last_cmd_status_show,
1443 .fflags = RF_TOP_INFO,
1446 .name = "num_closids",
1448 .kf_ops = &rdtgroup_kf_single_ops,
1449 .seq_show = rdt_num_closids_show,
1450 .fflags = RF_CTRL_INFO,
1453 .name = "mon_features",
1455 .kf_ops = &rdtgroup_kf_single_ops,
1456 .seq_show = rdt_mon_features_show,
1457 .fflags = RF_MON_INFO,
1460 .name = "num_rmids",
1462 .kf_ops = &rdtgroup_kf_single_ops,
1463 .seq_show = rdt_num_rmids_show,
1464 .fflags = RF_MON_INFO,
1469 .kf_ops = &rdtgroup_kf_single_ops,
1470 .seq_show = rdt_default_ctrl_show,
1471 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1474 .name = "min_cbm_bits",
1476 .kf_ops = &rdtgroup_kf_single_ops,
1477 .seq_show = rdt_min_cbm_bits_show,
1478 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1481 .name = "shareable_bits",
1483 .kf_ops = &rdtgroup_kf_single_ops,
1484 .seq_show = rdt_shareable_bits_show,
1485 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1488 .name = "bit_usage",
1490 .kf_ops = &rdtgroup_kf_single_ops,
1491 .seq_show = rdt_bit_usage_show,
1492 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1495 .name = "min_bandwidth",
1497 .kf_ops = &rdtgroup_kf_single_ops,
1498 .seq_show = rdt_min_bw_show,
1499 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1502 .name = "bandwidth_gran",
1504 .kf_ops = &rdtgroup_kf_single_ops,
1505 .seq_show = rdt_bw_gran_show,
1506 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1509 .name = "delay_linear",
1511 .kf_ops = &rdtgroup_kf_single_ops,
1512 .seq_show = rdt_delay_linear_show,
1513 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1516 .name = "max_threshold_occupancy",
1518 .kf_ops = &rdtgroup_kf_single_ops,
1519 .write = max_threshold_occ_write,
1520 .seq_show = max_threshold_occ_show,
1521 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
1526 .kf_ops = &rdtgroup_kf_single_ops,
1527 .write = rdtgroup_cpus_write,
1528 .seq_show = rdtgroup_cpus_show,
1529 .fflags = RFTYPE_BASE,
1532 .name = "cpus_list",
1534 .kf_ops = &rdtgroup_kf_single_ops,
1535 .write = rdtgroup_cpus_write,
1536 .seq_show = rdtgroup_cpus_show,
1537 .flags = RFTYPE_FLAGS_CPUS_LIST,
1538 .fflags = RFTYPE_BASE,
1543 .kf_ops = &rdtgroup_kf_single_ops,
1544 .write = rdtgroup_tasks_write,
1545 .seq_show = rdtgroup_tasks_show,
1546 .fflags = RFTYPE_BASE,
1551 .kf_ops = &rdtgroup_kf_single_ops,
1552 .write = rdtgroup_schemata_write,
1553 .seq_show = rdtgroup_schemata_show,
1554 .fflags = RF_CTRL_BASE,
1559 .kf_ops = &rdtgroup_kf_single_ops,
1560 .write = rdtgroup_mode_write,
1561 .seq_show = rdtgroup_mode_show,
1562 .fflags = RF_CTRL_BASE,
1567 .kf_ops = &rdtgroup_kf_single_ops,
1568 .seq_show = rdtgroup_size_show,
1569 .fflags = RF_CTRL_BASE,
1574 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1576 struct rftype *rfts, *rft;
1579 rfts = res_common_files;
1580 len = ARRAY_SIZE(res_common_files);
1582 lockdep_assert_held(&rdtgroup_mutex);
1584 for (rft = rfts; rft < rfts + len; rft++) {
1585 if ((fflags & rft->fflags) == rft->fflags) {
1586 ret = rdtgroup_add_file(kn, rft);
1594 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1595 while (--rft >= rfts) {
1596 if ((fflags & rft->fflags) == rft->fflags)
1597 kernfs_remove_by_name(kn, rft->name);
1603 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1604 * @r: The resource group with which the file is associated.
1605 * @name: Name of the file
1607 * The permissions of named resctrl file, directory, or link are modified
1608 * to not allow read, write, or execute by any user.
1610 * WARNING: This function is intended to communicate to the user that the
1611 * resctrl file has been locked down - that it is not relevant to the
1612 * particular state the system finds itself in. It should not be relied
1613 * on to protect from user access because after the file's permissions
1614 * are restricted the user can still change the permissions using chmod
1615 * from the command line.
1617 * Return: 0 on success, <0 on failure.
1619 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1621 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1622 struct kernfs_node *kn;
1625 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1629 switch (kernfs_type(kn)) {
1631 iattr.ia_mode = S_IFDIR;
1634 iattr.ia_mode = S_IFREG;
1637 iattr.ia_mode = S_IFLNK;
1641 ret = kernfs_setattr(kn, &iattr);
1647 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1648 * @r: The resource group with which the file is associated.
1649 * @name: Name of the file
1650 * @mask: Mask of permissions that should be restored
1652 * Restore the permissions of the named file. If @name is a directory the
1653 * permissions of its parent will be used.
1655 * Return: 0 on success, <0 on failure.
1657 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1660 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1661 struct kernfs_node *kn, *parent;
1662 struct rftype *rfts, *rft;
1665 rfts = res_common_files;
1666 len = ARRAY_SIZE(res_common_files);
1668 for (rft = rfts; rft < rfts + len; rft++) {
1669 if (!strcmp(rft->name, name))
1670 iattr.ia_mode = rft->mode & mask;
1673 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1677 switch (kernfs_type(kn)) {
1679 parent = kernfs_get_parent(kn);
1681 iattr.ia_mode |= parent->mode;
1684 iattr.ia_mode |= S_IFDIR;
1687 iattr.ia_mode |= S_IFREG;
1690 iattr.ia_mode |= S_IFLNK;
1694 ret = kernfs_setattr(kn, &iattr);
1699 static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
1700 unsigned long fflags)
1702 struct kernfs_node *kn_subdir;
1705 kn_subdir = kernfs_create_dir(kn_info, name,
1707 if (IS_ERR(kn_subdir))
1708 return PTR_ERR(kn_subdir);
1710 kernfs_get(kn_subdir);
1711 ret = rdtgroup_kn_set_ugid(kn_subdir);
1715 ret = rdtgroup_add_files(kn_subdir, fflags);
1717 kernfs_activate(kn_subdir);
1722 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
1724 struct rdt_resource *r;
1725 unsigned long fflags;
1729 /* create the directory */
1730 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
1731 if (IS_ERR(kn_info))
1732 return PTR_ERR(kn_info);
1733 kernfs_get(kn_info);
1735 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
1739 for_each_alloc_enabled_rdt_resource(r) {
1740 fflags = r->fflags | RF_CTRL_INFO;
1741 ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
1746 for_each_mon_enabled_rdt_resource(r) {
1747 fflags = r->fflags | RF_MON_INFO;
1748 sprintf(name, "%s_MON", r->name);
1749 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
1755 * This extra ref will be put in kernfs_remove() and guarantees
1756 * that @rdtgrp->kn is always accessible.
1758 kernfs_get(kn_info);
1760 ret = rdtgroup_kn_set_ugid(kn_info);
1764 kernfs_activate(kn_info);
1769 kernfs_remove(kn_info);
1774 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
1775 char *name, struct kernfs_node **dest_kn)
1777 struct kernfs_node *kn;
1780 /* create the directory */
1781 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1789 * This extra ref will be put in kernfs_remove() and guarantees
1790 * that @rdtgrp->kn is always accessible.
1794 ret = rdtgroup_kn_set_ugid(kn);
1798 kernfs_activate(kn);
1807 static void l3_qos_cfg_update(void *arg)
1811 wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
1814 static void l2_qos_cfg_update(void *arg)
1818 wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
1821 static inline bool is_mba_linear(void)
1823 return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear;
1826 static int set_cache_qos_cfg(int level, bool enable)
1828 void (*update)(void *arg);
1829 struct rdt_resource *r_l;
1830 cpumask_var_t cpu_mask;
1831 struct rdt_domain *d;
1834 if (level == RDT_RESOURCE_L3)
1835 update = l3_qos_cfg_update;
1836 else if (level == RDT_RESOURCE_L2)
1837 update = l2_qos_cfg_update;
1841 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1844 r_l = &rdt_resources_all[level];
1845 list_for_each_entry(d, &r_l->domains, list) {
1846 /* Pick one CPU from each domain instance to update MSR */
1847 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1850 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1851 if (cpumask_test_cpu(cpu, cpu_mask))
1853 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1854 smp_call_function_many(cpu_mask, update, &enable, 1);
1857 free_cpumask_var(cpu_mask);
1862 /* Restore the qos cfg state when a domain comes online */
1863 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
1865 if (!r->alloc_capable)
1868 if (r == &rdt_resources_all[RDT_RESOURCE_L2DATA])
1869 l2_qos_cfg_update(&r->alloc_enabled);
1871 if (r == &rdt_resources_all[RDT_RESOURCE_L3DATA])
1872 l3_qos_cfg_update(&r->alloc_enabled);
1876 * Enable or disable the MBA software controller
1877 * which helps user specify bandwidth in MBps.
1878 * MBA software controller is supported only if
1879 * MBM is supported and MBA is in linear scale.
1881 static int set_mba_sc(bool mba_sc)
1883 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA];
1884 struct rdt_domain *d;
1886 if (!is_mbm_enabled() || !is_mba_linear() ||
1887 mba_sc == is_mba_sc(r))
1890 r->membw.mba_sc = mba_sc;
1891 list_for_each_entry(d, &r->domains, list)
1892 setup_default_ctrlval(r, d->ctrl_val, d->mbps_val);
1897 static int cdp_enable(int level, int data_type, int code_type)
1899 struct rdt_resource *r_ldata = &rdt_resources_all[data_type];
1900 struct rdt_resource *r_lcode = &rdt_resources_all[code_type];
1901 struct rdt_resource *r_l = &rdt_resources_all[level];
1904 if (!r_l->alloc_capable || !r_ldata->alloc_capable ||
1905 !r_lcode->alloc_capable)
1908 ret = set_cache_qos_cfg(level, true);
1910 r_l->alloc_enabled = false;
1911 r_ldata->alloc_enabled = true;
1912 r_lcode->alloc_enabled = true;
1917 static int cdpl3_enable(void)
1919 return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA,
1920 RDT_RESOURCE_L3CODE);
1923 static int cdpl2_enable(void)
1925 return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA,
1926 RDT_RESOURCE_L2CODE);
1929 static void cdp_disable(int level, int data_type, int code_type)
1931 struct rdt_resource *r = &rdt_resources_all[level];
1933 r->alloc_enabled = r->alloc_capable;
1935 if (rdt_resources_all[data_type].alloc_enabled) {
1936 rdt_resources_all[data_type].alloc_enabled = false;
1937 rdt_resources_all[code_type].alloc_enabled = false;
1938 set_cache_qos_cfg(level, false);
1942 static void cdpl3_disable(void)
1944 cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE);
1947 static void cdpl2_disable(void)
1949 cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE);
1952 static void cdp_disable_all(void)
1954 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
1956 if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
1961 * We don't allow rdtgroup directories to be created anywhere
1962 * except the root directory. Thus when looking for the rdtgroup
1963 * structure for a kernfs node we are either looking at a directory,
1964 * in which case the rdtgroup structure is pointed at by the "priv"
1965 * field, otherwise we have a file, and need only look to the parent
1966 * to find the rdtgroup.
1968 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1970 if (kernfs_type(kn) == KERNFS_DIR) {
1972 * All the resource directories use "kn->priv"
1973 * to point to the "struct rdtgroup" for the
1974 * resource. "info" and its subdirectories don't
1975 * have rdtgroup structures, so return NULL here.
1977 if (kn == kn_info || kn->parent == kn_info)
1982 return kn->parent->priv;
1986 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1988 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1993 atomic_inc(&rdtgrp->waitcount);
1994 kernfs_break_active_protection(kn);
1996 mutex_lock(&rdtgroup_mutex);
1998 /* Was this group deleted while we waited? */
1999 if (rdtgrp->flags & RDT_DELETED)
2005 void rdtgroup_kn_unlock(struct kernfs_node *kn)
2007 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2012 mutex_unlock(&rdtgroup_mutex);
2014 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2015 (rdtgrp->flags & RDT_DELETED)) {
2016 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2017 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2018 rdtgroup_pseudo_lock_remove(rdtgrp);
2019 kernfs_unbreak_active_protection(kn);
2020 kernfs_put(rdtgrp->kn);
2023 kernfs_unbreak_active_protection(kn);
2027 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2028 struct rdtgroup *prgrp,
2029 struct kernfs_node **mon_data_kn);
2031 static int rdt_enable_ctx(struct rdt_fs_context *ctx)
2035 if (ctx->enable_cdpl2)
2036 ret = cdpl2_enable();
2038 if (!ret && ctx->enable_cdpl3)
2039 ret = cdpl3_enable();
2041 if (!ret && ctx->enable_mba_mbps)
2042 ret = set_mba_sc(true);
2047 static int rdt_get_tree(struct fs_context *fc)
2049 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2050 struct rdt_domain *dom;
2051 struct rdt_resource *r;
2055 mutex_lock(&rdtgroup_mutex);
2057 * resctrl file system can only be mounted once.
2059 if (static_branch_unlikely(&rdt_enable_key)) {
2064 ret = rdt_enable_ctx(ctx);
2070 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
2074 if (rdt_mon_capable) {
2075 ret = mongroup_create_dir(rdtgroup_default.kn,
2076 &rdtgroup_default, "mon_groups",
2080 kernfs_get(kn_mongrp);
2082 ret = mkdir_mondata_all(rdtgroup_default.kn,
2083 &rdtgroup_default, &kn_mondata);
2086 kernfs_get(kn_mondata);
2087 rdtgroup_default.mon.mon_data_kn = kn_mondata;
2090 ret = rdt_pseudo_lock_init();
2094 ret = kernfs_get_tree(fc);
2098 if (rdt_alloc_capable)
2099 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
2100 if (rdt_mon_capable)
2101 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
2103 if (rdt_alloc_capable || rdt_mon_capable)
2104 static_branch_enable_cpuslocked(&rdt_enable_key);
2106 if (is_mbm_enabled()) {
2107 r = &rdt_resources_all[RDT_RESOURCE_L3];
2108 list_for_each_entry(dom, &r->domains, list)
2109 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
2115 rdt_pseudo_lock_release();
2117 if (rdt_mon_capable)
2118 kernfs_remove(kn_mondata);
2120 if (rdt_mon_capable)
2121 kernfs_remove(kn_mongrp);
2123 kernfs_remove(kn_info);
2125 if (ctx->enable_mba_mbps)
2130 rdt_last_cmd_clear();
2131 mutex_unlock(&rdtgroup_mutex);
2143 static const struct fs_parameter_spec rdt_fs_parameters[] = {
2144 fsparam_flag("cdp", Opt_cdp),
2145 fsparam_flag("cdpl2", Opt_cdpl2),
2146 fsparam_flag("mba_MBps", Opt_mba_mbps),
2150 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2152 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2153 struct fs_parse_result result;
2156 opt = fs_parse(fc, rdt_fs_parameters, param, &result);
2162 ctx->enable_cdpl3 = true;
2165 ctx->enable_cdpl2 = true;
2168 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2170 ctx->enable_mba_mbps = true;
2177 static void rdt_fs_context_free(struct fs_context *fc)
2179 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2181 kernfs_free_fs_context(fc);
2185 static const struct fs_context_operations rdt_fs_context_ops = {
2186 .free = rdt_fs_context_free,
2187 .parse_param = rdt_parse_param,
2188 .get_tree = rdt_get_tree,
2191 static int rdt_init_fs_context(struct fs_context *fc)
2193 struct rdt_fs_context *ctx;
2195 ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2199 ctx->kfc.root = rdt_root;
2200 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2201 fc->fs_private = &ctx->kfc;
2202 fc->ops = &rdt_fs_context_ops;
2203 put_user_ns(fc->user_ns);
2204 fc->user_ns = get_user_ns(&init_user_ns);
2209 static int reset_all_ctrls(struct rdt_resource *r)
2211 struct msr_param msr_param;
2212 cpumask_var_t cpu_mask;
2213 struct rdt_domain *d;
2216 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2221 msr_param.high = r->num_closid;
2224 * Disable resource control for this resource by setting all
2225 * CBMs in all domains to the maximum mask value. Pick one CPU
2226 * from each domain to update the MSRs below.
2228 list_for_each_entry(d, &r->domains, list) {
2229 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2231 for (i = 0; i < r->num_closid; i++)
2232 d->ctrl_val[i] = r->default_ctrl;
2235 /* Update CBM on this cpu if it's in cpu_mask. */
2236 if (cpumask_test_cpu(cpu, cpu_mask))
2237 rdt_ctrl_update(&msr_param);
2238 /* Update CBM on all other cpus in cpu_mask. */
2239 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
2242 free_cpumask_var(cpu_mask);
2247 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
2249 return (rdt_alloc_capable &&
2250 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
2253 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
2255 return (rdt_mon_capable &&
2256 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
2260 * Move tasks from one to the other group. If @from is NULL, then all tasks
2261 * in the systems are moved unconditionally (used for teardown).
2263 * If @mask is not NULL the cpus on which moved tasks are running are set
2264 * in that mask so the update smp function call is restricted to affected
2267 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2268 struct cpumask *mask)
2270 struct task_struct *p, *t;
2272 read_lock(&tasklist_lock);
2273 for_each_process_thread(p, t) {
2274 if (!from || is_closid_match(t, from) ||
2275 is_rmid_match(t, from)) {
2276 t->closid = to->closid;
2277 t->rmid = to->mon.rmid;
2281 * This is safe on x86 w/o barriers as the ordering
2282 * of writing to task_cpu() and t->on_cpu is
2283 * reverse to the reading here. The detection is
2284 * inaccurate as tasks might move or schedule
2285 * before the smp function call takes place. In
2286 * such a case the function call is pointless, but
2287 * there is no other side effect.
2289 if (mask && t->on_cpu)
2290 cpumask_set_cpu(task_cpu(t), mask);
2294 read_unlock(&tasklist_lock);
2297 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2299 struct rdtgroup *sentry, *stmp;
2300 struct list_head *head;
2302 head = &rdtgrp->mon.crdtgrp_list;
2303 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2304 free_rmid(sentry->mon.rmid);
2305 list_del(&sentry->mon.crdtgrp_list);
2307 if (atomic_read(&sentry->waitcount) != 0)
2308 sentry->flags = RDT_DELETED;
2315 * Forcibly remove all of subdirectories under root.
2317 static void rmdir_all_sub(void)
2319 struct rdtgroup *rdtgrp, *tmp;
2321 /* Move all tasks to the default resource group */
2322 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2324 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2325 /* Free any child rmids */
2326 free_all_child_rdtgrp(rdtgrp);
2328 /* Remove each rdtgroup other than root */
2329 if (rdtgrp == &rdtgroup_default)
2332 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2333 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2334 rdtgroup_pseudo_lock_remove(rdtgrp);
2337 * Give any CPUs back to the default group. We cannot copy
2338 * cpu_online_mask because a CPU might have executed the
2339 * offline callback already, but is still marked online.
2341 cpumask_or(&rdtgroup_default.cpu_mask,
2342 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2344 free_rmid(rdtgrp->mon.rmid);
2346 kernfs_remove(rdtgrp->kn);
2347 list_del(&rdtgrp->rdtgroup_list);
2349 if (atomic_read(&rdtgrp->waitcount) != 0)
2350 rdtgrp->flags = RDT_DELETED;
2354 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2355 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2357 kernfs_remove(kn_info);
2358 kernfs_remove(kn_mongrp);
2359 kernfs_remove(kn_mondata);
2362 static void rdt_kill_sb(struct super_block *sb)
2364 struct rdt_resource *r;
2367 mutex_lock(&rdtgroup_mutex);
2371 /*Put everything back to default values. */
2372 for_each_alloc_enabled_rdt_resource(r)
2376 rdt_pseudo_lock_release();
2377 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2378 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2379 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2380 static_branch_disable_cpuslocked(&rdt_enable_key);
2382 mutex_unlock(&rdtgroup_mutex);
2386 static struct file_system_type rdt_fs_type = {
2388 .init_fs_context = rdt_init_fs_context,
2389 .parameters = rdt_fs_parameters,
2390 .kill_sb = rdt_kill_sb,
2393 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2396 struct kernfs_node *kn;
2399 kn = __kernfs_create_file(parent_kn, name, 0444,
2400 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
2401 &kf_mondata_ops, priv, NULL, NULL);
2405 ret = rdtgroup_kn_set_ugid(kn);
2415 * Remove all subdirectories of mon_data of ctrl_mon groups
2416 * and monitor groups with given domain id.
2418 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
2420 struct rdtgroup *prgrp, *crgrp;
2423 if (!r->mon_enabled)
2426 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2427 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2428 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2430 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2431 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2435 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2436 struct rdt_domain *d,
2437 struct rdt_resource *r, struct rdtgroup *prgrp)
2439 union mon_data_bits priv;
2440 struct kernfs_node *kn;
2441 struct mon_evt *mevt;
2442 struct rmid_read rr;
2446 sprintf(name, "mon_%s_%02d", r->name, d->id);
2447 /* create the directory */
2448 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2453 * This extra ref will be put in kernfs_remove() and guarantees
2454 * that kn is always accessible.
2457 ret = rdtgroup_kn_set_ugid(kn);
2461 if (WARN_ON(list_empty(&r->evt_list))) {
2466 priv.u.rid = r->rid;
2467 priv.u.domid = d->id;
2468 list_for_each_entry(mevt, &r->evt_list, list) {
2469 priv.u.evtid = mevt->evtid;
2470 ret = mon_addfile(kn, mevt->name, priv.priv);
2474 if (is_mbm_event(mevt->evtid))
2475 mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
2477 kernfs_activate(kn);
2486 * Add all subdirectories of mon_data for "ctrl_mon" groups
2487 * and "monitor" groups with given domain id.
2489 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2490 struct rdt_domain *d)
2492 struct kernfs_node *parent_kn;
2493 struct rdtgroup *prgrp, *crgrp;
2494 struct list_head *head;
2496 if (!r->mon_enabled)
2499 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2500 parent_kn = prgrp->mon.mon_data_kn;
2501 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2503 head = &prgrp->mon.crdtgrp_list;
2504 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2505 parent_kn = crgrp->mon.mon_data_kn;
2506 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2511 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2512 struct rdt_resource *r,
2513 struct rdtgroup *prgrp)
2515 struct rdt_domain *dom;
2518 list_for_each_entry(dom, &r->domains, list) {
2519 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2528 * This creates a directory mon_data which contains the monitored data.
2530 * mon_data has one directory for each domain whic are named
2531 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2532 * with L3 domain looks as below:
2539 * Each domain directory has one file per event:
2544 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2545 struct rdtgroup *prgrp,
2546 struct kernfs_node **dest_kn)
2548 struct rdt_resource *r;
2549 struct kernfs_node *kn;
2553 * Create the mon_data directory first.
2555 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
2563 * Create the subdirectories for each domain. Note that all events
2564 * in a domain like L3 are grouped into a resource whose domain is L3
2566 for_each_mon_enabled_rdt_resource(r) {
2567 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2580 * cbm_ensure_valid - Enforce validity on provided CBM
2581 * @_val: Candidate CBM
2582 * @r: RDT resource to which the CBM belongs
2584 * The provided CBM represents all cache portions available for use. This
2585 * may be represented by a bitmap that does not consist of contiguous ones
2586 * and thus be an invalid CBM.
2587 * Here the provided CBM is forced to be a valid CBM by only considering
2588 * the first set of contiguous bits as valid and clearing all bits.
2589 * The intention here is to provide a valid default CBM with which a new
2590 * resource group is initialized. The user can follow this with a
2591 * modification to the CBM if the default does not satisfy the
2594 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
2596 unsigned int cbm_len = r->cache.cbm_len;
2597 unsigned long first_bit, zero_bit;
2598 unsigned long val = _val;
2603 first_bit = find_first_bit(&val, cbm_len);
2604 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
2606 /* Clear any remaining bits to ensure contiguous region */
2607 bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
2612 * Initialize cache resources per RDT domain
2614 * Set the RDT domain up to start off with all usable allocations. That is,
2615 * all shareable and unused bits. All-zero CBM is invalid.
2617 static int __init_one_rdt_domain(struct rdt_domain *d, struct rdt_resource *r,
2620 struct rdt_resource *r_cdp = NULL;
2621 struct rdt_domain *d_cdp = NULL;
2622 u32 used_b = 0, unused_b = 0;
2623 unsigned long tmp_cbm;
2624 enum rdtgrp_mode mode;
2625 u32 peer_ctl, *ctrl;
2628 rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp);
2629 d->have_new_ctrl = false;
2630 d->new_ctrl = r->cache.shareable_bits;
2631 used_b = r->cache.shareable_bits;
2633 for (i = 0; i < closids_supported(); i++, ctrl++) {
2634 if (closid_allocated(i) && i != closid) {
2635 mode = rdtgroup_mode_by_closid(i);
2636 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
2638 * ctrl values for locksetup aren't relevant
2639 * until the schemata is written, and the mode
2640 * becomes RDT_MODE_PSEUDO_LOCKED.
2644 * If CDP is active include peer domain's
2645 * usage to ensure there is no overlap
2646 * with an exclusive group.
2649 peer_ctl = d_cdp->ctrl_val[i];
2652 used_b |= *ctrl | peer_ctl;
2653 if (mode == RDT_MODE_SHAREABLE)
2654 d->new_ctrl |= *ctrl | peer_ctl;
2657 if (d->plr && d->plr->cbm > 0)
2658 used_b |= d->plr->cbm;
2659 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
2660 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
2661 d->new_ctrl |= unused_b;
2663 * Force the initial CBM to be valid, user can
2664 * modify the CBM based on system availability.
2666 d->new_ctrl = cbm_ensure_valid(d->new_ctrl, r);
2668 * Assign the u32 CBM to an unsigned long to ensure that
2669 * bitmap_weight() does not access out-of-bound memory.
2671 tmp_cbm = d->new_ctrl;
2672 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
2673 rdt_last_cmd_printf("No space on %s:%d\n", r->name, d->id);
2676 d->have_new_ctrl = true;
2682 * Initialize cache resources with default values.
2684 * A new RDT group is being created on an allocation capable (CAT)
2685 * supporting system. Set this group up to start off with all usable
2688 * If there are no more shareable bits available on any domain then
2689 * the entire allocation will fail.
2691 static int rdtgroup_init_cat(struct rdt_resource *r, u32 closid)
2693 struct rdt_domain *d;
2696 list_for_each_entry(d, &r->domains, list) {
2697 ret = __init_one_rdt_domain(d, r, closid);
2705 /* Initialize MBA resource with default values. */
2706 static void rdtgroup_init_mba(struct rdt_resource *r)
2708 struct rdt_domain *d;
2710 list_for_each_entry(d, &r->domains, list) {
2711 d->new_ctrl = is_mba_sc(r) ? MBA_MAX_MBPS : r->default_ctrl;
2712 d->have_new_ctrl = true;
2716 /* Initialize the RDT group's allocations. */
2717 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
2719 struct rdt_resource *r;
2722 for_each_alloc_enabled_rdt_resource(r) {
2723 if (r->rid == RDT_RESOURCE_MBA) {
2724 rdtgroup_init_mba(r);
2726 ret = rdtgroup_init_cat(r, rdtgrp->closid);
2731 ret = update_domains(r, rdtgrp->closid);
2733 rdt_last_cmd_puts("Failed to initialize allocations\n");
2739 rdtgrp->mode = RDT_MODE_SHAREABLE;
2744 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
2745 const char *name, umode_t mode,
2746 enum rdt_group_type rtype, struct rdtgroup **r)
2748 struct rdtgroup *prdtgrp, *rdtgrp;
2749 struct kernfs_node *kn;
2753 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
2759 if (rtype == RDTMON_GROUP &&
2760 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2761 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
2763 rdt_last_cmd_puts("Pseudo-locking in progress\n");
2767 /* allocate the rdtgroup. */
2768 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
2771 rdt_last_cmd_puts("Kernel out of memory\n");
2775 rdtgrp->mon.parent = prdtgrp;
2776 rdtgrp->type = rtype;
2777 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
2779 /* kernfs creates the directory for rdtgrp */
2780 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
2783 rdt_last_cmd_puts("kernfs create error\n");
2789 * kernfs_remove() will drop the reference count on "kn" which
2790 * will free it. But we still need it to stick around for the
2791 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
2792 * here, which will be dropped inside rdtgroup_kn_unlock().
2796 ret = rdtgroup_kn_set_ugid(kn);
2798 rdt_last_cmd_puts("kernfs perm error\n");
2802 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
2803 ret = rdtgroup_add_files(kn, files);
2805 rdt_last_cmd_puts("kernfs fill error\n");
2809 if (rdt_mon_capable) {
2812 rdt_last_cmd_puts("Out of RMIDs\n");
2815 rdtgrp->mon.rmid = ret;
2817 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
2819 rdt_last_cmd_puts("kernfs subdir error\n");
2823 kernfs_activate(kn);
2826 * The caller unlocks the parent_kn upon success.
2831 free_rmid(rdtgrp->mon.rmid);
2833 kernfs_remove(rdtgrp->kn);
2837 rdtgroup_kn_unlock(parent_kn);
2841 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
2843 kernfs_remove(rgrp->kn);
2844 free_rmid(rgrp->mon.rmid);
2849 * Create a monitor group under "mon_groups" directory of a control
2850 * and monitor group(ctrl_mon). This is a resource group
2851 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
2853 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
2854 const char *name, umode_t mode)
2856 struct rdtgroup *rdtgrp, *prgrp;
2859 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
2863 prgrp = rdtgrp->mon.parent;
2864 rdtgrp->closid = prgrp->closid;
2867 * Add the rdtgrp to the list of rdtgrps the parent
2868 * ctrl_mon group has to track.
2870 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
2872 rdtgroup_kn_unlock(parent_kn);
2877 * These are rdtgroups created under the root directory. Can be used
2878 * to allocate and monitor resources.
2880 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
2881 const char *name, umode_t mode)
2883 struct rdtgroup *rdtgrp;
2884 struct kernfs_node *kn;
2888 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
2893 ret = closid_alloc();
2895 rdt_last_cmd_puts("Out of CLOSIDs\n");
2896 goto out_common_fail;
2901 rdtgrp->closid = closid;
2902 ret = rdtgroup_init_alloc(rdtgrp);
2906 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
2908 if (rdt_mon_capable) {
2910 * Create an empty mon_groups directory to hold the subset
2911 * of tasks and cpus to monitor.
2913 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
2915 rdt_last_cmd_puts("kernfs subdir error\n");
2923 list_del(&rdtgrp->rdtgroup_list);
2925 closid_free(closid);
2927 mkdir_rdt_prepare_clean(rdtgrp);
2929 rdtgroup_kn_unlock(parent_kn);
2934 * We allow creating mon groups only with in a directory called "mon_groups"
2935 * which is present in every ctrl_mon group. Check if this is a valid
2936 * "mon_groups" directory.
2938 * 1. The directory should be named "mon_groups".
2939 * 2. The mon group itself should "not" be named "mon_groups".
2940 * This makes sure "mon_groups" directory always has a ctrl_mon group
2943 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
2945 return (!strcmp(kn->name, "mon_groups") &&
2946 strcmp(name, "mon_groups"));
2949 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
2952 /* Do not accept '\n' to avoid unparsable situation. */
2953 if (strchr(name, '\n'))
2957 * If the parent directory is the root directory and RDT
2958 * allocation is supported, add a control and monitoring
2961 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
2962 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
2965 * If RDT monitoring is supported and the parent directory is a valid
2966 * "mon_groups" directory, add a monitoring subdirectory.
2968 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
2969 return rdtgroup_mkdir_mon(parent_kn, name, mode);
2974 static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
2975 cpumask_var_t tmpmask)
2977 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
2980 /* Give any tasks back to the parent group */
2981 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
2983 /* Update per cpu rmid of the moved CPUs first */
2984 for_each_cpu(cpu, &rdtgrp->cpu_mask)
2985 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
2987 * Update the MSR on moved CPUs and CPUs which have moved
2988 * task running on them.
2990 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
2991 update_closid_rmid(tmpmask, NULL);
2993 rdtgrp->flags = RDT_DELETED;
2994 free_rmid(rdtgrp->mon.rmid);
2997 * Remove the rdtgrp from the parent ctrl_mon group's list
2999 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3000 list_del(&rdtgrp->mon.crdtgrp_list);
3003 * one extra hold on this, will drop when we kfree(rdtgrp)
3004 * in rdtgroup_kn_unlock()
3007 kernfs_remove(rdtgrp->kn);
3012 static int rdtgroup_ctrl_remove(struct kernfs_node *kn,
3013 struct rdtgroup *rdtgrp)
3015 rdtgrp->flags = RDT_DELETED;
3016 list_del(&rdtgrp->rdtgroup_list);
3019 * one extra hold on this, will drop when we kfree(rdtgrp)
3020 * in rdtgroup_kn_unlock()
3023 kernfs_remove(rdtgrp->kn);
3027 static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
3028 cpumask_var_t tmpmask)
3032 /* Give any tasks back to the default group */
3033 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
3035 /* Give any CPUs back to the default group */
3036 cpumask_or(&rdtgroup_default.cpu_mask,
3037 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
3039 /* Update per cpu closid and rmid of the moved CPUs first */
3040 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
3041 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3042 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
3046 * Update the MSR on moved CPUs and CPUs which have moved
3047 * task running on them.
3049 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3050 update_closid_rmid(tmpmask, NULL);
3052 closid_free(rdtgrp->closid);
3053 free_rmid(rdtgrp->mon.rmid);
3055 rdtgroup_ctrl_remove(kn, rdtgrp);
3058 * Free all the child monitor group rmids.
3060 free_all_child_rdtgrp(rdtgrp);
3065 static int rdtgroup_rmdir(struct kernfs_node *kn)
3067 struct kernfs_node *parent_kn = kn->parent;
3068 struct rdtgroup *rdtgrp;
3069 cpumask_var_t tmpmask;
3072 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3075 rdtgrp = rdtgroup_kn_lock_live(kn);
3082 * If the rdtgroup is a ctrl_mon group and parent directory
3083 * is the root directory, remove the ctrl_mon group.
3085 * If the rdtgroup is a mon group and parent directory
3086 * is a valid "mon_groups" directory, remove the mon group.
3088 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3089 rdtgrp != &rdtgroup_default) {
3090 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3091 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
3092 ret = rdtgroup_ctrl_remove(kn, rdtgrp);
3094 ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask);
3096 } else if (rdtgrp->type == RDTMON_GROUP &&
3097 is_mon_groups(parent_kn, kn->name)) {
3098 ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask);
3104 rdtgroup_kn_unlock(kn);
3105 free_cpumask_var(tmpmask);
3109 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3111 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
3112 seq_puts(seq, ",cdp");
3114 if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
3115 seq_puts(seq, ",cdpl2");
3117 if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA]))
3118 seq_puts(seq, ",mba_MBps");
3123 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
3124 .mkdir = rdtgroup_mkdir,
3125 .rmdir = rdtgroup_rmdir,
3126 .show_options = rdtgroup_show_options,
3129 static int __init rdtgroup_setup_root(void)
3133 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
3134 KERNFS_ROOT_CREATE_DEACTIVATED |
3135 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
3137 if (IS_ERR(rdt_root))
3138 return PTR_ERR(rdt_root);
3140 mutex_lock(&rdtgroup_mutex);
3142 rdtgroup_default.closid = 0;
3143 rdtgroup_default.mon.rmid = 0;
3144 rdtgroup_default.type = RDTCTRL_GROUP;
3145 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3147 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3149 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
3151 kernfs_destroy_root(rdt_root);
3155 rdtgroup_default.kn = rdt_root->kn;
3156 kernfs_activate(rdtgroup_default.kn);
3159 mutex_unlock(&rdtgroup_mutex);
3165 * rdtgroup_init - rdtgroup initialization
3167 * Setup resctrl file system including set up root, create mount point,
3168 * register rdtgroup filesystem, and initialize files under root directory.
3170 * Return: 0 on success or -errno
3172 int __init rdtgroup_init(void)
3176 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
3177 sizeof(last_cmd_status_buf));
3179 ret = rdtgroup_setup_root();
3183 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
3187 ret = register_filesystem(&rdt_fs_type);
3189 goto cleanup_mountpoint;
3192 * Adding the resctrl debugfs directory here may not be ideal since
3193 * it would let the resctrl debugfs directory appear on the debugfs
3194 * filesystem before the resctrl filesystem is mounted.
3195 * It may also be ok since that would enable debugging of RDT before
3196 * resctrl is mounted.
3197 * The reason why the debugfs directory is created here and not in
3198 * rdt_mount() is because rdt_mount() takes rdtgroup_mutex and
3199 * during the debugfs directory creation also &sb->s_type->i_mutex_key
3200 * (the lockdep class of inode->i_rwsem). Other filesystem
3201 * interactions (eg. SyS_getdents) have the lock ordering:
3202 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
3203 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
3204 * is taken, thus creating dependency:
3205 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
3206 * issues considering the other two lock dependencies.
3207 * By creating the debugfs directory here we avoid a dependency
3208 * that may cause deadlock (even though file operations cannot
3209 * occur until the filesystem is mounted, but I do not know how to
3210 * tell lockdep that).
3212 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
3217 sysfs_remove_mount_point(fs_kobj, "resctrl");
3219 kernfs_destroy_root(rdt_root);
3224 void __exit rdtgroup_exit(void)
3226 debugfs_remove_recursive(debugfs_resctrl);
3227 unregister_filesystem(&rdt_fs_type);
3228 sysfs_remove_mount_point(fs_kobj, "resctrl");
3229 kernfs_destroy_root(rdt_root);