1 // SPDX-License-Identifier: GPL-2.0-only
3 * User interface for Resource Allocation 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 /* list of entries for the schemata file */
43 LIST_HEAD(resctrl_schema_all);
45 /* The filesystem can only be mounted once. */
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 static int rdtgroup_setup_root(struct rdt_fs_context *ctx);
61 static void rdtgroup_destroy_root(void);
63 struct dentry *debugfs_resctrl;
65 static bool resctrl_debug;
67 void rdt_last_cmd_clear(void)
69 lockdep_assert_held(&rdtgroup_mutex);
70 seq_buf_clear(&last_cmd_status);
73 void rdt_last_cmd_puts(const char *s)
75 lockdep_assert_held(&rdtgroup_mutex);
76 seq_buf_puts(&last_cmd_status, s);
79 void rdt_last_cmd_printf(const char *fmt, ...)
84 lockdep_assert_held(&rdtgroup_mutex);
85 seq_buf_vprintf(&last_cmd_status, fmt, ap);
89 void rdt_staged_configs_clear(void)
91 struct rdt_resource *r;
92 struct rdt_domain *dom;
94 lockdep_assert_held(&rdtgroup_mutex);
96 for_each_alloc_capable_rdt_resource(r) {
97 list_for_each_entry(dom, &r->domains, list)
98 memset(dom->staged_config, 0, sizeof(dom->staged_config));
103 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
104 * we can keep a bitmap of free CLOSIDs in a single integer.
106 * Using a global CLOSID across all resources has some advantages and
108 * + We can simply set current's closid to assign a task to a resource
110 * + Context switch code can avoid extra memory references deciding which
111 * CLOSID to load into the PQR_ASSOC MSR
112 * - We give up some options in configuring resource groups across multi-socket
114 * - Our choices on how to configure each resource become progressively more
115 * limited as the number of resources grows.
117 static unsigned long closid_free_map;
118 static int closid_free_map_len;
120 int closids_supported(void)
122 return closid_free_map_len;
125 static void closid_init(void)
127 struct resctrl_schema *s;
128 u32 rdt_min_closid = 32;
130 /* Compute rdt_min_closid across all resources */
131 list_for_each_entry(s, &resctrl_schema_all, list)
132 rdt_min_closid = min(rdt_min_closid, s->num_closid);
134 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
136 /* RESCTRL_RESERVED_CLOSID is always reserved for the default group */
137 __clear_bit(RESCTRL_RESERVED_CLOSID, &closid_free_map);
138 closid_free_map_len = rdt_min_closid;
141 static int closid_alloc(void)
146 lockdep_assert_held(&rdtgroup_mutex);
148 if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
149 cleanest_closid = resctrl_find_cleanest_closid();
150 if (cleanest_closid < 0)
151 return cleanest_closid;
152 closid = cleanest_closid;
154 closid = ffs(closid_free_map);
159 __clear_bit(closid, &closid_free_map);
164 void closid_free(int closid)
166 lockdep_assert_held(&rdtgroup_mutex);
168 __set_bit(closid, &closid_free_map);
172 * closid_allocated - test if provided closid is in use
173 * @closid: closid to be tested
175 * Return: true if @closid is currently associated with a resource group,
176 * false if @closid is free
178 bool closid_allocated(unsigned int closid)
180 lockdep_assert_held(&rdtgroup_mutex);
182 return !test_bit(closid, &closid_free_map);
186 * rdtgroup_mode_by_closid - Return mode of resource group with closid
187 * @closid: closid if the resource group
189 * Each resource group is associated with a @closid. Here the mode
190 * of a resource group can be queried by searching for it using its closid.
192 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
194 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
196 struct rdtgroup *rdtgrp;
198 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
199 if (rdtgrp->closid == closid)
203 return RDT_NUM_MODES;
206 static const char * const rdt_mode_str[] = {
207 [RDT_MODE_SHAREABLE] = "shareable",
208 [RDT_MODE_EXCLUSIVE] = "exclusive",
209 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
210 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
214 * rdtgroup_mode_str - Return the string representation of mode
215 * @mode: the resource group mode as &enum rdtgroup_mode
217 * Return: string representation of valid mode, "unknown" otherwise
219 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
221 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
224 return rdt_mode_str[mode];
227 /* set uid and gid of rdtgroup dirs and files to that of the creator */
228 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
230 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
231 .ia_uid = current_fsuid(),
232 .ia_gid = current_fsgid(), };
234 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
235 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
238 return kernfs_setattr(kn, &iattr);
241 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
243 struct kernfs_node *kn;
246 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
247 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
248 0, rft->kf_ops, rft, NULL, NULL);
252 ret = rdtgroup_kn_set_ugid(kn);
261 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
263 struct kernfs_open_file *of = m->private;
264 struct rftype *rft = of->kn->priv;
267 return rft->seq_show(of, m, arg);
271 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
272 size_t nbytes, loff_t off)
274 struct rftype *rft = of->kn->priv;
277 return rft->write(of, buf, nbytes, off);
282 static const struct kernfs_ops rdtgroup_kf_single_ops = {
283 .atomic_write_len = PAGE_SIZE,
284 .write = rdtgroup_file_write,
285 .seq_show = rdtgroup_seqfile_show,
288 static const struct kernfs_ops kf_mondata_ops = {
289 .atomic_write_len = PAGE_SIZE,
290 .seq_show = rdtgroup_mondata_show,
293 static bool is_cpu_list(struct kernfs_open_file *of)
295 struct rftype *rft = of->kn->priv;
297 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
300 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
301 struct seq_file *s, void *v)
303 struct rdtgroup *rdtgrp;
304 struct cpumask *mask;
307 rdtgrp = rdtgroup_kn_lock_live(of->kn);
310 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
311 if (!rdtgrp->plr->d) {
312 rdt_last_cmd_clear();
313 rdt_last_cmd_puts("Cache domain offline\n");
316 mask = &rdtgrp->plr->d->cpu_mask;
317 seq_printf(s, is_cpu_list(of) ?
318 "%*pbl\n" : "%*pb\n",
319 cpumask_pr_args(mask));
322 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
323 cpumask_pr_args(&rdtgrp->cpu_mask));
328 rdtgroup_kn_unlock(of->kn);
334 * This is safe against resctrl_sched_in() called from __switch_to()
335 * because __switch_to() is executed with interrupts disabled. A local call
336 * from update_closid_rmid() is protected against __switch_to() because
337 * preemption is disabled.
339 static void update_cpu_closid_rmid(void *info)
341 struct rdtgroup *r = info;
344 this_cpu_write(pqr_state.default_closid, r->closid);
345 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
349 * We cannot unconditionally write the MSR because the current
350 * executing task might have its own closid selected. Just reuse
351 * the context switch code.
353 resctrl_sched_in(current);
357 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
359 * Per task closids/rmids must have been set up before calling this function.
362 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
364 on_each_cpu_mask(cpu_mask, update_cpu_closid_rmid, r, 1);
367 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
368 cpumask_var_t tmpmask)
370 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
371 struct list_head *head;
373 /* Check whether cpus belong to parent ctrl group */
374 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
375 if (!cpumask_empty(tmpmask)) {
376 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
380 /* Check whether cpus are dropped from this group */
381 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
382 if (!cpumask_empty(tmpmask)) {
383 /* Give any dropped cpus to parent rdtgroup */
384 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
385 update_closid_rmid(tmpmask, prgrp);
389 * If we added cpus, remove them from previous group that owned them
390 * and update per-cpu rmid
392 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
393 if (!cpumask_empty(tmpmask)) {
394 head = &prgrp->mon.crdtgrp_list;
395 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
398 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
401 update_closid_rmid(tmpmask, rdtgrp);
404 /* Done pushing/pulling - update this group with new mask */
405 cpumask_copy(&rdtgrp->cpu_mask, newmask);
410 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
412 struct rdtgroup *crgrp;
414 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
415 /* update the child mon group masks as well*/
416 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
417 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
420 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
421 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
423 struct rdtgroup *r, *crgrp;
424 struct list_head *head;
426 /* Check whether cpus are dropped from this group */
427 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
428 if (!cpumask_empty(tmpmask)) {
429 /* Can't drop from default group */
430 if (rdtgrp == &rdtgroup_default) {
431 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
435 /* Give any dropped cpus to rdtgroup_default */
436 cpumask_or(&rdtgroup_default.cpu_mask,
437 &rdtgroup_default.cpu_mask, tmpmask);
438 update_closid_rmid(tmpmask, &rdtgroup_default);
442 * If we added cpus, remove them from previous group and
443 * the prev group's child groups that owned them
444 * and update per-cpu closid/rmid.
446 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
447 if (!cpumask_empty(tmpmask)) {
448 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
451 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
452 if (!cpumask_empty(tmpmask1))
453 cpumask_rdtgrp_clear(r, tmpmask1);
455 update_closid_rmid(tmpmask, rdtgrp);
458 /* Done pushing/pulling - update this group with new mask */
459 cpumask_copy(&rdtgrp->cpu_mask, newmask);
462 * Clear child mon group masks since there is a new parent mask
463 * now and update the rmid for the cpus the child lost.
465 head = &rdtgrp->mon.crdtgrp_list;
466 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
467 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
468 update_closid_rmid(tmpmask, rdtgrp);
469 cpumask_clear(&crgrp->cpu_mask);
475 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
476 char *buf, size_t nbytes, loff_t off)
478 cpumask_var_t tmpmask, newmask, tmpmask1;
479 struct rdtgroup *rdtgrp;
485 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
487 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
488 free_cpumask_var(tmpmask);
491 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
492 free_cpumask_var(tmpmask);
493 free_cpumask_var(newmask);
497 rdtgrp = rdtgroup_kn_lock_live(of->kn);
503 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
504 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
506 rdt_last_cmd_puts("Pseudo-locking in progress\n");
511 ret = cpulist_parse(buf, newmask);
513 ret = cpumask_parse(buf, newmask);
516 rdt_last_cmd_puts("Bad CPU list/mask\n");
520 /* check that user didn't specify any offline cpus */
521 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
522 if (!cpumask_empty(tmpmask)) {
524 rdt_last_cmd_puts("Can only assign online CPUs\n");
528 if (rdtgrp->type == RDTCTRL_GROUP)
529 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
530 else if (rdtgrp->type == RDTMON_GROUP)
531 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
536 rdtgroup_kn_unlock(of->kn);
537 free_cpumask_var(tmpmask);
538 free_cpumask_var(newmask);
539 free_cpumask_var(tmpmask1);
541 return ret ?: nbytes;
545 * rdtgroup_remove - the helper to remove resource group safely
546 * @rdtgrp: resource group to remove
548 * On resource group creation via a mkdir, an extra kernfs_node reference is
549 * taken to ensure that the rdtgroup structure remains accessible for the
550 * rdtgroup_kn_unlock() calls where it is removed.
552 * Drop the extra reference here, then free the rdtgroup structure.
556 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
558 kernfs_put(rdtgrp->kn);
562 static void _update_task_closid_rmid(void *task)
565 * If the task is still current on this CPU, update PQR_ASSOC MSR.
566 * Otherwise, the MSR is updated when the task is scheduled in.
569 resctrl_sched_in(task);
572 static void update_task_closid_rmid(struct task_struct *t)
574 if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
575 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
577 _update_task_closid_rmid(t);
580 static bool task_in_rdtgroup(struct task_struct *tsk, struct rdtgroup *rdtgrp)
582 u32 closid, rmid = rdtgrp->mon.rmid;
584 if (rdtgrp->type == RDTCTRL_GROUP)
585 closid = rdtgrp->closid;
586 else if (rdtgrp->type == RDTMON_GROUP)
587 closid = rdtgrp->mon.parent->closid;
591 return resctrl_arch_match_closid(tsk, closid) &&
592 resctrl_arch_match_rmid(tsk, closid, rmid);
595 static int __rdtgroup_move_task(struct task_struct *tsk,
596 struct rdtgroup *rdtgrp)
598 /* If the task is already in rdtgrp, no need to move the task. */
599 if (task_in_rdtgroup(tsk, rdtgrp))
603 * Set the task's closid/rmid before the PQR_ASSOC MSR can be
606 * For ctrl_mon groups, move both closid and rmid.
607 * For monitor groups, can move the tasks only from
608 * their parent CTRL group.
610 if (rdtgrp->type == RDTMON_GROUP &&
611 !resctrl_arch_match_closid(tsk, rdtgrp->mon.parent->closid)) {
612 rdt_last_cmd_puts("Can't move task to different control group\n");
616 if (rdtgrp->type == RDTMON_GROUP)
617 resctrl_arch_set_closid_rmid(tsk, rdtgrp->mon.parent->closid,
620 resctrl_arch_set_closid_rmid(tsk, rdtgrp->closid,
624 * Ensure the task's closid and rmid are written before determining if
625 * the task is current that will decide if it will be interrupted.
626 * This pairs with the full barrier between the rq->curr update and
627 * resctrl_sched_in() during context switch.
632 * By now, the task's closid and rmid are set. If the task is current
633 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
634 * group go into effect. If the task is not current, the MSR will be
635 * updated when the task is scheduled in.
637 update_task_closid_rmid(tsk);
642 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
644 return (resctrl_arch_alloc_capable() && (r->type == RDTCTRL_GROUP) &&
645 resctrl_arch_match_closid(t, r->closid));
648 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
650 return (resctrl_arch_mon_capable() && (r->type == RDTMON_GROUP) &&
651 resctrl_arch_match_rmid(t, r->mon.parent->closid,
656 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
659 * Return: 1 if tasks have been assigned to @r, 0 otherwise
661 int rdtgroup_tasks_assigned(struct rdtgroup *r)
663 struct task_struct *p, *t;
666 lockdep_assert_held(&rdtgroup_mutex);
669 for_each_process_thread(p, t) {
670 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
680 static int rdtgroup_task_write_permission(struct task_struct *task,
681 struct kernfs_open_file *of)
683 const struct cred *tcred = get_task_cred(task);
684 const struct cred *cred = current_cred();
688 * Even if we're attaching all tasks in the thread group, we only
689 * need to check permissions on one of them.
691 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
692 !uid_eq(cred->euid, tcred->uid) &&
693 !uid_eq(cred->euid, tcred->suid)) {
694 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
702 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
703 struct kernfs_open_file *of)
705 struct task_struct *tsk;
710 tsk = find_task_by_vpid(pid);
713 rdt_last_cmd_printf("No task %d\n", pid);
720 get_task_struct(tsk);
723 ret = rdtgroup_task_write_permission(tsk, of);
725 ret = __rdtgroup_move_task(tsk, rdtgrp);
727 put_task_struct(tsk);
731 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
732 char *buf, size_t nbytes, loff_t off)
734 struct rdtgroup *rdtgrp;
739 rdtgrp = rdtgroup_kn_lock_live(of->kn);
741 rdtgroup_kn_unlock(of->kn);
744 rdt_last_cmd_clear();
746 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
747 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
749 rdt_last_cmd_puts("Pseudo-locking in progress\n");
753 while (buf && buf[0] != '\0' && buf[0] != '\n') {
754 pid_str = strim(strsep(&buf, ","));
756 if (kstrtoint(pid_str, 0, &pid)) {
757 rdt_last_cmd_printf("Task list parsing error pid %s\n", pid_str);
763 rdt_last_cmd_printf("Invalid pid %d\n", pid);
768 ret = rdtgroup_move_task(pid, rdtgrp, of);
770 rdt_last_cmd_printf("Error while processing task %d\n", pid);
776 rdtgroup_kn_unlock(of->kn);
778 return ret ?: nbytes;
781 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
783 struct task_struct *p, *t;
787 for_each_process_thread(p, t) {
788 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
789 pid = task_pid_vnr(t);
791 seq_printf(s, "%d\n", pid);
797 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
798 struct seq_file *s, void *v)
800 struct rdtgroup *rdtgrp;
803 rdtgrp = rdtgroup_kn_lock_live(of->kn);
805 show_rdt_tasks(rdtgrp, s);
808 rdtgroup_kn_unlock(of->kn);
813 static int rdtgroup_closid_show(struct kernfs_open_file *of,
814 struct seq_file *s, void *v)
816 struct rdtgroup *rdtgrp;
819 rdtgrp = rdtgroup_kn_lock_live(of->kn);
821 seq_printf(s, "%u\n", rdtgrp->closid);
824 rdtgroup_kn_unlock(of->kn);
829 static int rdtgroup_rmid_show(struct kernfs_open_file *of,
830 struct seq_file *s, void *v)
832 struct rdtgroup *rdtgrp;
835 rdtgrp = rdtgroup_kn_lock_live(of->kn);
837 seq_printf(s, "%u\n", rdtgrp->mon.rmid);
840 rdtgroup_kn_unlock(of->kn);
845 #ifdef CONFIG_PROC_CPU_RESCTRL
848 * A task can only be part of one resctrl control group and of one monitor
849 * group which is associated to that control group.
854 * resctrl is not available.
859 * Task is part of the root resctrl control group, and it is not associated
860 * to any monitor group.
865 * Task is part of the root resctrl control group and monitor group mon0.
870 * Task is part of resctrl control group group0, and it is not associated
871 * to any monitor group.
876 * Task is part of resctrl control group group0 and monitor group mon1.
878 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
879 struct pid *pid, struct task_struct *tsk)
881 struct rdtgroup *rdtg;
884 mutex_lock(&rdtgroup_mutex);
886 /* Return empty if resctrl has not been mounted. */
887 if (!resctrl_mounted) {
888 seq_puts(s, "res:\nmon:\n");
892 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
893 struct rdtgroup *crg;
896 * Task information is only relevant for shareable
897 * and exclusive groups.
899 if (rdtg->mode != RDT_MODE_SHAREABLE &&
900 rdtg->mode != RDT_MODE_EXCLUSIVE)
903 if (!resctrl_arch_match_closid(tsk, rdtg->closid))
906 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
909 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
911 if (!resctrl_arch_match_rmid(tsk, crg->mon.parent->closid,
914 seq_printf(s, "%s", crg->kn->name);
921 * The above search should succeed. Otherwise return
926 mutex_unlock(&rdtgroup_mutex);
932 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
933 struct seq_file *seq, void *v)
937 mutex_lock(&rdtgroup_mutex);
938 len = seq_buf_used(&last_cmd_status);
940 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
942 seq_puts(seq, "ok\n");
943 mutex_unlock(&rdtgroup_mutex);
947 static int rdt_num_closids_show(struct kernfs_open_file *of,
948 struct seq_file *seq, void *v)
950 struct resctrl_schema *s = of->kn->parent->priv;
952 seq_printf(seq, "%u\n", s->num_closid);
956 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
957 struct seq_file *seq, void *v)
959 struct resctrl_schema *s = of->kn->parent->priv;
960 struct rdt_resource *r = s->res;
962 seq_printf(seq, "%x\n", r->default_ctrl);
966 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
967 struct seq_file *seq, void *v)
969 struct resctrl_schema *s = of->kn->parent->priv;
970 struct rdt_resource *r = s->res;
972 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
976 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
977 struct seq_file *seq, void *v)
979 struct resctrl_schema *s = of->kn->parent->priv;
980 struct rdt_resource *r = s->res;
982 seq_printf(seq, "%x\n", r->cache.shareable_bits);
987 * rdt_bit_usage_show - Display current usage of resources
989 * A domain is a shared resource that can now be allocated differently. Here
990 * we display the current regions of the domain as an annotated bitmask.
991 * For each domain of this resource its allocation bitmask
992 * is annotated as below to indicate the current usage of the corresponding bit:
993 * 0 - currently unused
994 * X - currently available for sharing and used by software and hardware
995 * H - currently used by hardware only but available for software use
996 * S - currently used and shareable by software only
997 * E - currently used exclusively by one resource group
998 * P - currently pseudo-locked by one resource group
1000 static int rdt_bit_usage_show(struct kernfs_open_file *of,
1001 struct seq_file *seq, void *v)
1003 struct resctrl_schema *s = of->kn->parent->priv;
1005 * Use unsigned long even though only 32 bits are used to ensure
1006 * test_bit() is used safely.
1008 unsigned long sw_shareable = 0, hw_shareable = 0;
1009 unsigned long exclusive = 0, pseudo_locked = 0;
1010 struct rdt_resource *r = s->res;
1011 struct rdt_domain *dom;
1012 int i, hwb, swb, excl, psl;
1013 enum rdtgrp_mode mode;
1017 mutex_lock(&rdtgroup_mutex);
1018 hw_shareable = r->cache.shareable_bits;
1019 list_for_each_entry(dom, &r->domains, list) {
1024 seq_printf(seq, "%d=", dom->id);
1025 for (i = 0; i < closids_supported(); i++) {
1026 if (!closid_allocated(i))
1028 ctrl_val = resctrl_arch_get_config(r, dom, i,
1030 mode = rdtgroup_mode_by_closid(i);
1032 case RDT_MODE_SHAREABLE:
1033 sw_shareable |= ctrl_val;
1035 case RDT_MODE_EXCLUSIVE:
1036 exclusive |= ctrl_val;
1038 case RDT_MODE_PSEUDO_LOCKSETUP:
1040 * RDT_MODE_PSEUDO_LOCKSETUP is possible
1041 * here but not included since the CBM
1042 * associated with this CLOSID in this mode
1043 * is not initialized and no task or cpu can be
1044 * assigned this CLOSID.
1047 case RDT_MODE_PSEUDO_LOCKED:
1050 "invalid mode for closid %d\n", i);
1054 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
1055 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
1056 hwb = test_bit(i, &hw_shareable);
1057 swb = test_bit(i, &sw_shareable);
1058 excl = test_bit(i, &exclusive);
1059 psl = test_bit(i, &pseudo_locked);
1062 else if (hwb && !swb)
1064 else if (!hwb && swb)
1070 else /* Unused bits remain */
1075 seq_putc(seq, '\n');
1076 mutex_unlock(&rdtgroup_mutex);
1080 static int rdt_min_bw_show(struct kernfs_open_file *of,
1081 struct seq_file *seq, void *v)
1083 struct resctrl_schema *s = of->kn->parent->priv;
1084 struct rdt_resource *r = s->res;
1086 seq_printf(seq, "%u\n", r->membw.min_bw);
1090 static int rdt_num_rmids_show(struct kernfs_open_file *of,
1091 struct seq_file *seq, void *v)
1093 struct rdt_resource *r = of->kn->parent->priv;
1095 seq_printf(seq, "%d\n", r->num_rmid);
1100 static int rdt_mon_features_show(struct kernfs_open_file *of,
1101 struct seq_file *seq, void *v)
1103 struct rdt_resource *r = of->kn->parent->priv;
1104 struct mon_evt *mevt;
1106 list_for_each_entry(mevt, &r->evt_list, list) {
1107 seq_printf(seq, "%s\n", mevt->name);
1108 if (mevt->configurable)
1109 seq_printf(seq, "%s_config\n", mevt->name);
1115 static int rdt_bw_gran_show(struct kernfs_open_file *of,
1116 struct seq_file *seq, void *v)
1118 struct resctrl_schema *s = of->kn->parent->priv;
1119 struct rdt_resource *r = s->res;
1121 seq_printf(seq, "%u\n", r->membw.bw_gran);
1125 static int rdt_delay_linear_show(struct kernfs_open_file *of,
1126 struct seq_file *seq, void *v)
1128 struct resctrl_schema *s = of->kn->parent->priv;
1129 struct rdt_resource *r = s->res;
1131 seq_printf(seq, "%u\n", r->membw.delay_linear);
1135 static int max_threshold_occ_show(struct kernfs_open_file *of,
1136 struct seq_file *seq, void *v)
1138 seq_printf(seq, "%u\n", resctrl_rmid_realloc_threshold);
1143 static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
1144 struct seq_file *seq, void *v)
1146 struct resctrl_schema *s = of->kn->parent->priv;
1147 struct rdt_resource *r = s->res;
1149 if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
1150 seq_puts(seq, "per-thread\n");
1152 seq_puts(seq, "max\n");
1157 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1158 char *buf, size_t nbytes, loff_t off)
1163 ret = kstrtouint(buf, 0, &bytes);
1167 if (bytes > resctrl_rmid_realloc_limit)
1170 resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(bytes);
1176 * rdtgroup_mode_show - Display mode of this resource group
1178 static int rdtgroup_mode_show(struct kernfs_open_file *of,
1179 struct seq_file *s, void *v)
1181 struct rdtgroup *rdtgrp;
1183 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1185 rdtgroup_kn_unlock(of->kn);
1189 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1191 rdtgroup_kn_unlock(of->kn);
1195 static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
1208 static int rdt_has_sparse_bitmasks_show(struct kernfs_open_file *of,
1209 struct seq_file *seq, void *v)
1211 struct resctrl_schema *s = of->kn->parent->priv;
1212 struct rdt_resource *r = s->res;
1214 seq_printf(seq, "%u\n", r->cache.arch_has_sparse_bitmasks);
1220 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
1221 * @r: Resource to which domain instance @d belongs.
1222 * @d: The domain instance for which @closid is being tested.
1223 * @cbm: Capacity bitmask being tested.
1224 * @closid: Intended closid for @cbm.
1225 * @type: CDP type of @r.
1226 * @exclusive: Only check if overlaps with exclusive resource groups
1228 * Checks if provided @cbm intended to be used for @closid on domain
1229 * @d overlaps with any other closids or other hardware usage associated
1230 * with this domain. If @exclusive is true then only overlaps with
1231 * resource groups in exclusive mode will be considered. If @exclusive
1232 * is false then overlaps with any resource group or hardware entities
1233 * will be considered.
1235 * @cbm is unsigned long, even if only 32 bits are used, to make the
1236 * bitmap functions work correctly.
1238 * Return: false if CBM does not overlap, true if it does.
1240 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1241 unsigned long cbm, int closid,
1242 enum resctrl_conf_type type, bool exclusive)
1244 enum rdtgrp_mode mode;
1245 unsigned long ctrl_b;
1248 /* Check for any overlap with regions used by hardware directly */
1250 ctrl_b = r->cache.shareable_bits;
1251 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
1255 /* Check for overlap with other resource groups */
1256 for (i = 0; i < closids_supported(); i++) {
1257 ctrl_b = resctrl_arch_get_config(r, d, i, type);
1258 mode = rdtgroup_mode_by_closid(i);
1259 if (closid_allocated(i) && i != closid &&
1260 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1261 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1263 if (mode == RDT_MODE_EXCLUSIVE)
1276 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
1277 * @s: Schema for the resource to which domain instance @d belongs.
1278 * @d: The domain instance for which @closid is being tested.
1279 * @cbm: Capacity bitmask being tested.
1280 * @closid: Intended closid for @cbm.
1281 * @exclusive: Only check if overlaps with exclusive resource groups
1283 * Resources that can be allocated using a CBM can use the CBM to control
1284 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1285 * for overlap. Overlap test is not limited to the specific resource for
1286 * which the CBM is intended though - when dealing with CDP resources that
1287 * share the underlying hardware the overlap check should be performed on
1288 * the CDP resource sharing the hardware also.
1290 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1293 * Return: true if CBM overlap detected, false if there is no overlap
1295 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
1296 unsigned long cbm, int closid, bool exclusive)
1298 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
1299 struct rdt_resource *r = s->res;
1301 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
1305 if (!resctrl_arch_get_cdp_enabled(r->rid))
1307 return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
1311 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1312 * @rdtgrp: Resource group identified through its closid.
1314 * An exclusive resource group implies that there should be no sharing of
1315 * its allocated resources. At the time this group is considered to be
1316 * exclusive this test can determine if its current schemata supports this
1317 * setting by testing for overlap with all other resource groups.
1319 * Return: true if resource group can be exclusive, false if there is overlap
1320 * with allocations of other resource groups and thus this resource group
1321 * cannot be exclusive.
1323 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1325 int closid = rdtgrp->closid;
1326 struct resctrl_schema *s;
1327 struct rdt_resource *r;
1328 bool has_cache = false;
1329 struct rdt_domain *d;
1332 list_for_each_entry(s, &resctrl_schema_all, list) {
1334 if (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)
1337 list_for_each_entry(d, &r->domains, list) {
1338 ctrl = resctrl_arch_get_config(r, d, closid,
1340 if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
1341 rdt_last_cmd_puts("Schemata overlaps\n");
1348 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
1356 * rdtgroup_mode_write - Modify the resource group's mode
1358 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1359 char *buf, size_t nbytes, loff_t off)
1361 struct rdtgroup *rdtgrp;
1362 enum rdtgrp_mode mode;
1365 /* Valid input requires a trailing newline */
1366 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1368 buf[nbytes - 1] = '\0';
1370 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1372 rdtgroup_kn_unlock(of->kn);
1376 rdt_last_cmd_clear();
1378 mode = rdtgrp->mode;
1380 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1381 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1382 (!strcmp(buf, "pseudo-locksetup") &&
1383 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1384 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1387 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1388 rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
1393 if (!strcmp(buf, "shareable")) {
1394 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1395 ret = rdtgroup_locksetup_exit(rdtgrp);
1399 rdtgrp->mode = RDT_MODE_SHAREABLE;
1400 } else if (!strcmp(buf, "exclusive")) {
1401 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1405 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1406 ret = rdtgroup_locksetup_exit(rdtgrp);
1410 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1411 } else if (!strcmp(buf, "pseudo-locksetup")) {
1412 ret = rdtgroup_locksetup_enter(rdtgrp);
1415 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1417 rdt_last_cmd_puts("Unknown or unsupported mode\n");
1422 rdtgroup_kn_unlock(of->kn);
1423 return ret ?: nbytes;
1427 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1428 * @r: RDT resource to which @d belongs.
1429 * @d: RDT domain instance.
1430 * @cbm: bitmask for which the size should be computed.
1432 * The bitmask provided associated with the RDT domain instance @d will be
1433 * translated into how many bytes it represents. The size in bytes is
1434 * computed by first dividing the total cache size by the CBM length to
1435 * determine how many bytes each bit in the bitmask represents. The result
1436 * is multiplied with the number of bits set in the bitmask.
1438 * @cbm is unsigned long, even if only 32 bits are used to make the
1439 * bitmap functions work correctly.
1441 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1442 struct rdt_domain *d, unsigned long cbm)
1444 struct cpu_cacheinfo *ci;
1445 unsigned int size = 0;
1448 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1449 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1450 for (i = 0; i < ci->num_leaves; i++) {
1451 if (ci->info_list[i].level == r->cache_level) {
1452 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1461 * rdtgroup_size_show - Display size in bytes of allocated regions
1463 * The "size" file mirrors the layout of the "schemata" file, printing the
1464 * size in bytes of each region instead of the capacity bitmask.
1466 static int rdtgroup_size_show(struct kernfs_open_file *of,
1467 struct seq_file *s, void *v)
1469 struct resctrl_schema *schema;
1470 enum resctrl_conf_type type;
1471 struct rdtgroup *rdtgrp;
1472 struct rdt_resource *r;
1473 struct rdt_domain *d;
1480 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1482 rdtgroup_kn_unlock(of->kn);
1486 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1487 if (!rdtgrp->plr->d) {
1488 rdt_last_cmd_clear();
1489 rdt_last_cmd_puts("Cache domain offline\n");
1492 seq_printf(s, "%*s:", max_name_width,
1493 rdtgrp->plr->s->name);
1494 size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
1497 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1502 closid = rdtgrp->closid;
1504 list_for_each_entry(schema, &resctrl_schema_all, list) {
1506 type = schema->conf_type;
1508 seq_printf(s, "%*s:", max_name_width, schema->name);
1509 list_for_each_entry(d, &r->domains, list) {
1512 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1516 ctrl = d->mbps_val[closid];
1518 ctrl = resctrl_arch_get_config(r, d,
1521 if (r->rid == RDT_RESOURCE_MBA ||
1522 r->rid == RDT_RESOURCE_SMBA)
1525 size = rdtgroup_cbm_to_size(r, d, ctrl);
1527 seq_printf(s, "%d=%u", d->id, size);
1534 rdtgroup_kn_unlock(of->kn);
1539 struct mon_config_info {
1544 #define INVALID_CONFIG_INDEX UINT_MAX
1547 * mon_event_config_index_get - get the hardware index for the
1548 * configurable event
1551 * Return: 0 for evtid == QOS_L3_MBM_TOTAL_EVENT_ID
1552 * 1 for evtid == QOS_L3_MBM_LOCAL_EVENT_ID
1553 * INVALID_CONFIG_INDEX for invalid evtid
1555 static inline unsigned int mon_event_config_index_get(u32 evtid)
1558 case QOS_L3_MBM_TOTAL_EVENT_ID:
1560 case QOS_L3_MBM_LOCAL_EVENT_ID:
1563 /* Should never reach here */
1564 return INVALID_CONFIG_INDEX;
1568 static void mon_event_config_read(void *info)
1570 struct mon_config_info *mon_info = info;
1574 index = mon_event_config_index_get(mon_info->evtid);
1575 if (index == INVALID_CONFIG_INDEX) {
1576 pr_warn_once("Invalid event id %d\n", mon_info->evtid);
1579 rdmsrl(MSR_IA32_EVT_CFG_BASE + index, msrval);
1581 /* Report only the valid event configuration bits */
1582 mon_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
1585 static void mondata_config_read(struct rdt_domain *d, struct mon_config_info *mon_info)
1587 smp_call_function_any(&d->cpu_mask, mon_event_config_read, mon_info, 1);
1590 static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid)
1592 struct mon_config_info mon_info = {0};
1593 struct rdt_domain *dom;
1596 mutex_lock(&rdtgroup_mutex);
1598 list_for_each_entry(dom, &r->domains, list) {
1602 memset(&mon_info, 0, sizeof(struct mon_config_info));
1603 mon_info.evtid = evtid;
1604 mondata_config_read(dom, &mon_info);
1606 seq_printf(s, "%d=0x%02x", dom->id, mon_info.mon_config);
1611 mutex_unlock(&rdtgroup_mutex);
1616 static int mbm_total_bytes_config_show(struct kernfs_open_file *of,
1617 struct seq_file *seq, void *v)
1619 struct rdt_resource *r = of->kn->parent->priv;
1621 mbm_config_show(seq, r, QOS_L3_MBM_TOTAL_EVENT_ID);
1626 static int mbm_local_bytes_config_show(struct kernfs_open_file *of,
1627 struct seq_file *seq, void *v)
1629 struct rdt_resource *r = of->kn->parent->priv;
1631 mbm_config_show(seq, r, QOS_L3_MBM_LOCAL_EVENT_ID);
1636 static void mon_event_config_write(void *info)
1638 struct mon_config_info *mon_info = info;
1641 index = mon_event_config_index_get(mon_info->evtid);
1642 if (index == INVALID_CONFIG_INDEX) {
1643 pr_warn_once("Invalid event id %d\n", mon_info->evtid);
1646 wrmsr(MSR_IA32_EVT_CFG_BASE + index, mon_info->mon_config, 0);
1649 static void mbm_config_write_domain(struct rdt_resource *r,
1650 struct rdt_domain *d, u32 evtid, u32 val)
1652 struct mon_config_info mon_info = {0};
1655 * Read the current config value first. If both are the same then
1656 * no need to write it again.
1658 mon_info.evtid = evtid;
1659 mondata_config_read(d, &mon_info);
1660 if (mon_info.mon_config == val)
1663 mon_info.mon_config = val;
1666 * Update MSR_IA32_EVT_CFG_BASE MSR on one of the CPUs in the
1667 * domain. The MSRs offset from MSR MSR_IA32_EVT_CFG_BASE
1668 * are scoped at the domain level. Writing any of these MSRs
1669 * on one CPU is observed by all the CPUs in the domain.
1671 smp_call_function_any(&d->cpu_mask, mon_event_config_write,
1675 * When an Event Configuration is changed, the bandwidth counters
1676 * for all RMIDs and Events will be cleared by the hardware. The
1677 * hardware also sets MSR_IA32_QM_CTR.Unavailable (bit 62) for
1678 * every RMID on the next read to any event for every RMID.
1679 * Subsequent reads will have MSR_IA32_QM_CTR.Unavailable (bit 62)
1680 * cleared while it is tracked by the hardware. Clear the
1681 * mbm_local and mbm_total counts for all the RMIDs.
1683 resctrl_arch_reset_rmid_all(r, d);
1686 static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)
1688 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
1689 char *dom_str = NULL, *id_str;
1690 unsigned long dom_id, val;
1691 struct rdt_domain *d;
1694 if (!tok || tok[0] == '\0')
1697 /* Start processing the strings for each domain */
1698 dom_str = strim(strsep(&tok, ";"));
1699 id_str = strsep(&dom_str, "=");
1701 if (!id_str || kstrtoul(id_str, 10, &dom_id)) {
1702 rdt_last_cmd_puts("Missing '=' or non-numeric domain id\n");
1706 if (!dom_str || kstrtoul(dom_str, 16, &val)) {
1707 rdt_last_cmd_puts("Non-numeric event configuration value\n");
1711 /* Value from user cannot be more than the supported set of events */
1712 if ((val & hw_res->mbm_cfg_mask) != val) {
1713 rdt_last_cmd_printf("Invalid event configuration: max valid mask is 0x%02x\n",
1714 hw_res->mbm_cfg_mask);
1718 list_for_each_entry(d, &r->domains, list) {
1719 if (d->id == dom_id) {
1720 mbm_config_write_domain(r, d, evtid, val);
1728 static ssize_t mbm_total_bytes_config_write(struct kernfs_open_file *of,
1729 char *buf, size_t nbytes,
1732 struct rdt_resource *r = of->kn->parent->priv;
1735 /* Valid input requires a trailing newline */
1736 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1739 mutex_lock(&rdtgroup_mutex);
1741 rdt_last_cmd_clear();
1743 buf[nbytes - 1] = '\0';
1745 ret = mon_config_write(r, buf, QOS_L3_MBM_TOTAL_EVENT_ID);
1747 mutex_unlock(&rdtgroup_mutex);
1749 return ret ?: nbytes;
1752 static ssize_t mbm_local_bytes_config_write(struct kernfs_open_file *of,
1753 char *buf, size_t nbytes,
1756 struct rdt_resource *r = of->kn->parent->priv;
1759 /* Valid input requires a trailing newline */
1760 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1763 mutex_lock(&rdtgroup_mutex);
1765 rdt_last_cmd_clear();
1767 buf[nbytes - 1] = '\0';
1769 ret = mon_config_write(r, buf, QOS_L3_MBM_LOCAL_EVENT_ID);
1771 mutex_unlock(&rdtgroup_mutex);
1773 return ret ?: nbytes;
1776 /* rdtgroup information files for one cache resource. */
1777 static struct rftype res_common_files[] = {
1779 .name = "last_cmd_status",
1781 .kf_ops = &rdtgroup_kf_single_ops,
1782 .seq_show = rdt_last_cmd_status_show,
1783 .fflags = RFTYPE_TOP_INFO,
1786 .name = "num_closids",
1788 .kf_ops = &rdtgroup_kf_single_ops,
1789 .seq_show = rdt_num_closids_show,
1790 .fflags = RFTYPE_CTRL_INFO,
1793 .name = "mon_features",
1795 .kf_ops = &rdtgroup_kf_single_ops,
1796 .seq_show = rdt_mon_features_show,
1797 .fflags = RFTYPE_MON_INFO,
1800 .name = "num_rmids",
1802 .kf_ops = &rdtgroup_kf_single_ops,
1803 .seq_show = rdt_num_rmids_show,
1804 .fflags = RFTYPE_MON_INFO,
1809 .kf_ops = &rdtgroup_kf_single_ops,
1810 .seq_show = rdt_default_ctrl_show,
1811 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1814 .name = "min_cbm_bits",
1816 .kf_ops = &rdtgroup_kf_single_ops,
1817 .seq_show = rdt_min_cbm_bits_show,
1818 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1821 .name = "shareable_bits",
1823 .kf_ops = &rdtgroup_kf_single_ops,
1824 .seq_show = rdt_shareable_bits_show,
1825 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1828 .name = "bit_usage",
1830 .kf_ops = &rdtgroup_kf_single_ops,
1831 .seq_show = rdt_bit_usage_show,
1832 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1835 .name = "min_bandwidth",
1837 .kf_ops = &rdtgroup_kf_single_ops,
1838 .seq_show = rdt_min_bw_show,
1839 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
1842 .name = "bandwidth_gran",
1844 .kf_ops = &rdtgroup_kf_single_ops,
1845 .seq_show = rdt_bw_gran_show,
1846 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
1849 .name = "delay_linear",
1851 .kf_ops = &rdtgroup_kf_single_ops,
1852 .seq_show = rdt_delay_linear_show,
1853 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
1856 * Platform specific which (if any) capabilities are provided by
1857 * thread_throttle_mode. Defer "fflags" initialization to platform
1861 .name = "thread_throttle_mode",
1863 .kf_ops = &rdtgroup_kf_single_ops,
1864 .seq_show = rdt_thread_throttle_mode_show,
1867 .name = "max_threshold_occupancy",
1869 .kf_ops = &rdtgroup_kf_single_ops,
1870 .write = max_threshold_occ_write,
1871 .seq_show = max_threshold_occ_show,
1872 .fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE,
1875 .name = "mbm_total_bytes_config",
1877 .kf_ops = &rdtgroup_kf_single_ops,
1878 .seq_show = mbm_total_bytes_config_show,
1879 .write = mbm_total_bytes_config_write,
1882 .name = "mbm_local_bytes_config",
1884 .kf_ops = &rdtgroup_kf_single_ops,
1885 .seq_show = mbm_local_bytes_config_show,
1886 .write = mbm_local_bytes_config_write,
1891 .kf_ops = &rdtgroup_kf_single_ops,
1892 .write = rdtgroup_cpus_write,
1893 .seq_show = rdtgroup_cpus_show,
1894 .fflags = RFTYPE_BASE,
1897 .name = "cpus_list",
1899 .kf_ops = &rdtgroup_kf_single_ops,
1900 .write = rdtgroup_cpus_write,
1901 .seq_show = rdtgroup_cpus_show,
1902 .flags = RFTYPE_FLAGS_CPUS_LIST,
1903 .fflags = RFTYPE_BASE,
1908 .kf_ops = &rdtgroup_kf_single_ops,
1909 .write = rdtgroup_tasks_write,
1910 .seq_show = rdtgroup_tasks_show,
1911 .fflags = RFTYPE_BASE,
1914 .name = "mon_hw_id",
1916 .kf_ops = &rdtgroup_kf_single_ops,
1917 .seq_show = rdtgroup_rmid_show,
1918 .fflags = RFTYPE_MON_BASE | RFTYPE_DEBUG,
1923 .kf_ops = &rdtgroup_kf_single_ops,
1924 .write = rdtgroup_schemata_write,
1925 .seq_show = rdtgroup_schemata_show,
1926 .fflags = RFTYPE_CTRL_BASE,
1931 .kf_ops = &rdtgroup_kf_single_ops,
1932 .write = rdtgroup_mode_write,
1933 .seq_show = rdtgroup_mode_show,
1934 .fflags = RFTYPE_CTRL_BASE,
1939 .kf_ops = &rdtgroup_kf_single_ops,
1940 .seq_show = rdtgroup_size_show,
1941 .fflags = RFTYPE_CTRL_BASE,
1944 .name = "sparse_masks",
1946 .kf_ops = &rdtgroup_kf_single_ops,
1947 .seq_show = rdt_has_sparse_bitmasks_show,
1948 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1951 .name = "ctrl_hw_id",
1953 .kf_ops = &rdtgroup_kf_single_ops,
1954 .seq_show = rdtgroup_closid_show,
1955 .fflags = RFTYPE_CTRL_BASE | RFTYPE_DEBUG,
1960 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1962 struct rftype *rfts, *rft;
1965 rfts = res_common_files;
1966 len = ARRAY_SIZE(res_common_files);
1968 lockdep_assert_held(&rdtgroup_mutex);
1971 fflags |= RFTYPE_DEBUG;
1973 for (rft = rfts; rft < rfts + len; rft++) {
1974 if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
1975 ret = rdtgroup_add_file(kn, rft);
1983 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1984 while (--rft >= rfts) {
1985 if ((fflags & rft->fflags) == rft->fflags)
1986 kernfs_remove_by_name(kn, rft->name);
1991 static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
1993 struct rftype *rfts, *rft;
1996 rfts = res_common_files;
1997 len = ARRAY_SIZE(res_common_files);
1999 for (rft = rfts; rft < rfts + len; rft++) {
2000 if (!strcmp(rft->name, name))
2007 void __init thread_throttle_mode_init(void)
2011 rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
2015 rft->fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB;
2018 void __init mbm_config_rftype_init(const char *config)
2022 rft = rdtgroup_get_rftype_by_name(config);
2024 rft->fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE;
2028 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
2029 * @r: The resource group with which the file is associated.
2030 * @name: Name of the file
2032 * The permissions of named resctrl file, directory, or link are modified
2033 * to not allow read, write, or execute by any user.
2035 * WARNING: This function is intended to communicate to the user that the
2036 * resctrl file has been locked down - that it is not relevant to the
2037 * particular state the system finds itself in. It should not be relied
2038 * on to protect from user access because after the file's permissions
2039 * are restricted the user can still change the permissions using chmod
2040 * from the command line.
2042 * Return: 0 on success, <0 on failure.
2044 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
2046 struct iattr iattr = {.ia_valid = ATTR_MODE,};
2047 struct kernfs_node *kn;
2050 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
2054 switch (kernfs_type(kn)) {
2056 iattr.ia_mode = S_IFDIR;
2059 iattr.ia_mode = S_IFREG;
2062 iattr.ia_mode = S_IFLNK;
2066 ret = kernfs_setattr(kn, &iattr);
2072 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
2073 * @r: The resource group with which the file is associated.
2074 * @name: Name of the file
2075 * @mask: Mask of permissions that should be restored
2077 * Restore the permissions of the named file. If @name is a directory the
2078 * permissions of its parent will be used.
2080 * Return: 0 on success, <0 on failure.
2082 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
2085 struct iattr iattr = {.ia_valid = ATTR_MODE,};
2086 struct kernfs_node *kn, *parent;
2087 struct rftype *rfts, *rft;
2090 rfts = res_common_files;
2091 len = ARRAY_SIZE(res_common_files);
2093 for (rft = rfts; rft < rfts + len; rft++) {
2094 if (!strcmp(rft->name, name))
2095 iattr.ia_mode = rft->mode & mask;
2098 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
2102 switch (kernfs_type(kn)) {
2104 parent = kernfs_get_parent(kn);
2106 iattr.ia_mode |= parent->mode;
2109 iattr.ia_mode |= S_IFDIR;
2112 iattr.ia_mode |= S_IFREG;
2115 iattr.ia_mode |= S_IFLNK;
2119 ret = kernfs_setattr(kn, &iattr);
2124 static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
2125 unsigned long fflags)
2127 struct kernfs_node *kn_subdir;
2130 kn_subdir = kernfs_create_dir(kn_info, name,
2131 kn_info->mode, priv);
2132 if (IS_ERR(kn_subdir))
2133 return PTR_ERR(kn_subdir);
2135 ret = rdtgroup_kn_set_ugid(kn_subdir);
2139 ret = rdtgroup_add_files(kn_subdir, fflags);
2141 kernfs_activate(kn_subdir);
2146 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
2148 struct resctrl_schema *s;
2149 struct rdt_resource *r;
2150 unsigned long fflags;
2154 /* create the directory */
2155 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
2156 if (IS_ERR(kn_info))
2157 return PTR_ERR(kn_info);
2159 ret = rdtgroup_add_files(kn_info, RFTYPE_TOP_INFO);
2163 /* loop over enabled controls, these are all alloc_capable */
2164 list_for_each_entry(s, &resctrl_schema_all, list) {
2166 fflags = r->fflags | RFTYPE_CTRL_INFO;
2167 ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
2172 for_each_mon_capable_rdt_resource(r) {
2173 fflags = r->fflags | RFTYPE_MON_INFO;
2174 sprintf(name, "%s_MON", r->name);
2175 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
2180 ret = rdtgroup_kn_set_ugid(kn_info);
2184 kernfs_activate(kn_info);
2189 kernfs_remove(kn_info);
2194 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
2195 char *name, struct kernfs_node **dest_kn)
2197 struct kernfs_node *kn;
2200 /* create the directory */
2201 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2208 ret = rdtgroup_kn_set_ugid(kn);
2212 kernfs_activate(kn);
2221 static void l3_qos_cfg_update(void *arg)
2225 wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
2228 static void l2_qos_cfg_update(void *arg)
2232 wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
2235 static inline bool is_mba_linear(void)
2237 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
2240 static int set_cache_qos_cfg(int level, bool enable)
2242 void (*update)(void *arg);
2243 struct rdt_resource *r_l;
2244 cpumask_var_t cpu_mask;
2245 struct rdt_domain *d;
2248 if (level == RDT_RESOURCE_L3)
2249 update = l3_qos_cfg_update;
2250 else if (level == RDT_RESOURCE_L2)
2251 update = l2_qos_cfg_update;
2255 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2258 r_l = &rdt_resources_all[level].r_resctrl;
2259 list_for_each_entry(d, &r_l->domains, list) {
2260 if (r_l->cache.arch_has_per_cpu_cfg)
2261 /* Pick all the CPUs in the domain instance */
2262 for_each_cpu(cpu, &d->cpu_mask)
2263 cpumask_set_cpu(cpu, cpu_mask);
2265 /* Pick one CPU from each domain instance to update MSR */
2266 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2269 /* Update QOS_CFG MSR on all the CPUs in cpu_mask */
2270 on_each_cpu_mask(cpu_mask, update, &enable, 1);
2272 free_cpumask_var(cpu_mask);
2277 /* Restore the qos cfg state when a domain comes online */
2278 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
2280 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2282 if (!r->cdp_capable)
2285 if (r->rid == RDT_RESOURCE_L2)
2286 l2_qos_cfg_update(&hw_res->cdp_enabled);
2288 if (r->rid == RDT_RESOURCE_L3)
2289 l3_qos_cfg_update(&hw_res->cdp_enabled);
2292 static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_domain *d)
2294 u32 num_closid = resctrl_arch_get_num_closid(r);
2295 int cpu = cpumask_any(&d->cpu_mask);
2298 d->mbps_val = kcalloc_node(num_closid, sizeof(*d->mbps_val),
2299 GFP_KERNEL, cpu_to_node(cpu));
2303 for (i = 0; i < num_closid; i++)
2304 d->mbps_val[i] = MBA_MAX_MBPS;
2309 static void mba_sc_domain_destroy(struct rdt_resource *r,
2310 struct rdt_domain *d)
2317 * MBA software controller is supported only if
2318 * MBM is supported and MBA is in linear scale.
2320 static bool supports_mba_mbps(void)
2322 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
2324 return (is_mbm_local_enabled() &&
2325 r->alloc_capable && is_mba_linear());
2329 * Enable or disable the MBA software controller
2330 * which helps user specify bandwidth in MBps.
2332 static int set_mba_sc(bool mba_sc)
2334 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
2335 u32 num_closid = resctrl_arch_get_num_closid(r);
2336 struct rdt_domain *d;
2339 if (!supports_mba_mbps() || mba_sc == is_mba_sc(r))
2342 r->membw.mba_sc = mba_sc;
2344 list_for_each_entry(d, &r->domains, list) {
2345 for (i = 0; i < num_closid; i++)
2346 d->mbps_val[i] = MBA_MAX_MBPS;
2352 static int cdp_enable(int level)
2354 struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
2357 if (!r_l->alloc_capable)
2360 ret = set_cache_qos_cfg(level, true);
2362 rdt_resources_all[level].cdp_enabled = true;
2367 static void cdp_disable(int level)
2369 struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
2371 if (r_hw->cdp_enabled) {
2372 set_cache_qos_cfg(level, false);
2373 r_hw->cdp_enabled = false;
2377 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
2379 struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
2381 if (!hw_res->r_resctrl.cdp_capable)
2385 return cdp_enable(l);
2393 * We don't allow rdtgroup directories to be created anywhere
2394 * except the root directory. Thus when looking for the rdtgroup
2395 * structure for a kernfs node we are either looking at a directory,
2396 * in which case the rdtgroup structure is pointed at by the "priv"
2397 * field, otherwise we have a file, and need only look to the parent
2398 * to find the rdtgroup.
2400 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
2402 if (kernfs_type(kn) == KERNFS_DIR) {
2404 * All the resource directories use "kn->priv"
2405 * to point to the "struct rdtgroup" for the
2406 * resource. "info" and its subdirectories don't
2407 * have rdtgroup structures, so return NULL here.
2409 if (kn == kn_info || kn->parent == kn_info)
2414 return kn->parent->priv;
2418 static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
2420 atomic_inc(&rdtgrp->waitcount);
2421 kernfs_break_active_protection(kn);
2424 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
2426 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2427 (rdtgrp->flags & RDT_DELETED)) {
2428 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2429 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2430 rdtgroup_pseudo_lock_remove(rdtgrp);
2431 kernfs_unbreak_active_protection(kn);
2432 rdtgroup_remove(rdtgrp);
2434 kernfs_unbreak_active_protection(kn);
2438 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
2440 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2445 rdtgroup_kn_get(rdtgrp, kn);
2447 mutex_lock(&rdtgroup_mutex);
2449 /* Was this group deleted while we waited? */
2450 if (rdtgrp->flags & RDT_DELETED)
2456 void rdtgroup_kn_unlock(struct kernfs_node *kn)
2458 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2463 mutex_unlock(&rdtgroup_mutex);
2464 rdtgroup_kn_put(rdtgrp, kn);
2467 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2468 struct rdtgroup *prgrp,
2469 struct kernfs_node **mon_data_kn);
2471 static void rdt_disable_ctx(void)
2473 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
2474 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
2477 resctrl_debug = false;
2480 static int rdt_enable_ctx(struct rdt_fs_context *ctx)
2484 if (ctx->enable_cdpl2) {
2485 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
2490 if (ctx->enable_cdpl3) {
2491 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
2496 if (ctx->enable_mba_mbps) {
2497 ret = set_mba_sc(true);
2502 if (ctx->enable_debug)
2503 resctrl_debug = true;
2508 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
2510 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
2515 static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
2517 struct resctrl_schema *s;
2518 const char *suffix = "";
2521 s = kzalloc(sizeof(*s), GFP_KERNEL);
2526 s->num_closid = resctrl_arch_get_num_closid(r);
2527 if (resctrl_arch_get_cdp_enabled(r->rid))
2530 s->conf_type = type;
2543 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
2544 if (ret >= sizeof(s->name)) {
2549 cl = strlen(s->name);
2552 * If CDP is supported by this resource, but not enabled,
2553 * include the suffix. This ensures the tabular format of the
2554 * schemata file does not change between mounts of the filesystem.
2556 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
2559 if (cl > max_name_width)
2560 max_name_width = cl;
2562 INIT_LIST_HEAD(&s->list);
2563 list_add(&s->list, &resctrl_schema_all);
2568 static int schemata_list_create(void)
2570 struct rdt_resource *r;
2573 for_each_alloc_capable_rdt_resource(r) {
2574 if (resctrl_arch_get_cdp_enabled(r->rid)) {
2575 ret = schemata_list_add(r, CDP_CODE);
2579 ret = schemata_list_add(r, CDP_DATA);
2581 ret = schemata_list_add(r, CDP_NONE);
2591 static void schemata_list_destroy(void)
2593 struct resctrl_schema *s, *tmp;
2595 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
2601 static int rdt_get_tree(struct fs_context *fc)
2603 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2604 unsigned long flags = RFTYPE_CTRL_BASE;
2605 struct rdt_domain *dom;
2606 struct rdt_resource *r;
2610 mutex_lock(&rdtgroup_mutex);
2612 * resctrl file system can only be mounted once.
2614 if (resctrl_mounted) {
2619 ret = rdtgroup_setup_root(ctx);
2623 ret = rdt_enable_ctx(ctx);
2627 ret = schemata_list_create();
2629 schemata_list_destroy();
2635 if (resctrl_arch_mon_capable())
2636 flags |= RFTYPE_MON;
2638 ret = rdtgroup_add_files(rdtgroup_default.kn, flags);
2640 goto out_schemata_free;
2642 kernfs_activate(rdtgroup_default.kn);
2644 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
2646 goto out_schemata_free;
2648 if (resctrl_arch_mon_capable()) {
2649 ret = mongroup_create_dir(rdtgroup_default.kn,
2650 &rdtgroup_default, "mon_groups",
2655 ret = mkdir_mondata_all(rdtgroup_default.kn,
2656 &rdtgroup_default, &kn_mondata);
2659 rdtgroup_default.mon.mon_data_kn = kn_mondata;
2662 ret = rdt_pseudo_lock_init();
2666 ret = kernfs_get_tree(fc);
2670 if (resctrl_arch_alloc_capable())
2671 resctrl_arch_enable_alloc();
2672 if (resctrl_arch_mon_capable())
2673 resctrl_arch_enable_mon();
2675 if (resctrl_arch_alloc_capable() || resctrl_arch_mon_capable())
2676 resctrl_mounted = true;
2678 if (is_mbm_enabled()) {
2679 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
2680 list_for_each_entry(dom, &r->domains, list)
2681 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL,
2682 RESCTRL_PICK_ANY_CPU);
2688 rdt_pseudo_lock_release();
2690 if (resctrl_arch_mon_capable())
2691 kernfs_remove(kn_mondata);
2693 if (resctrl_arch_mon_capable())
2694 kernfs_remove(kn_mongrp);
2696 kernfs_remove(kn_info);
2698 schemata_list_destroy();
2702 rdtgroup_destroy_root();
2704 rdt_last_cmd_clear();
2705 mutex_unlock(&rdtgroup_mutex);
2718 static const struct fs_parameter_spec rdt_fs_parameters[] = {
2719 fsparam_flag("cdp", Opt_cdp),
2720 fsparam_flag("cdpl2", Opt_cdpl2),
2721 fsparam_flag("mba_MBps", Opt_mba_mbps),
2722 fsparam_flag("debug", Opt_debug),
2726 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2728 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2729 struct fs_parse_result result;
2732 opt = fs_parse(fc, rdt_fs_parameters, param, &result);
2738 ctx->enable_cdpl3 = true;
2741 ctx->enable_cdpl2 = true;
2744 if (!supports_mba_mbps())
2746 ctx->enable_mba_mbps = true;
2749 ctx->enable_debug = true;
2756 static void rdt_fs_context_free(struct fs_context *fc)
2758 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2760 kernfs_free_fs_context(fc);
2764 static const struct fs_context_operations rdt_fs_context_ops = {
2765 .free = rdt_fs_context_free,
2766 .parse_param = rdt_parse_param,
2767 .get_tree = rdt_get_tree,
2770 static int rdt_init_fs_context(struct fs_context *fc)
2772 struct rdt_fs_context *ctx;
2774 ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2778 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2779 fc->fs_private = &ctx->kfc;
2780 fc->ops = &rdt_fs_context_ops;
2781 put_user_ns(fc->user_ns);
2782 fc->user_ns = get_user_ns(&init_user_ns);
2787 static int reset_all_ctrls(struct rdt_resource *r)
2789 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2790 struct rdt_hw_domain *hw_dom;
2791 struct msr_param msr_param;
2792 cpumask_var_t cpu_mask;
2793 struct rdt_domain *d;
2796 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2801 msr_param.high = hw_res->num_closid;
2804 * Disable resource control for this resource by setting all
2805 * CBMs in all domains to the maximum mask value. Pick one CPU
2806 * from each domain to update the MSRs below.
2808 list_for_each_entry(d, &r->domains, list) {
2809 hw_dom = resctrl_to_arch_dom(d);
2810 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2812 for (i = 0; i < hw_res->num_closid; i++)
2813 hw_dom->ctrl_val[i] = r->default_ctrl;
2816 /* Update CBM on all the CPUs in cpu_mask */
2817 on_each_cpu_mask(cpu_mask, rdt_ctrl_update, &msr_param, 1);
2819 free_cpumask_var(cpu_mask);
2825 * Move tasks from one to the other group. If @from is NULL, then all tasks
2826 * in the systems are moved unconditionally (used for teardown).
2828 * If @mask is not NULL the cpus on which moved tasks are running are set
2829 * in that mask so the update smp function call is restricted to affected
2832 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2833 struct cpumask *mask)
2835 struct task_struct *p, *t;
2837 read_lock(&tasklist_lock);
2838 for_each_process_thread(p, t) {
2839 if (!from || is_closid_match(t, from) ||
2840 is_rmid_match(t, from)) {
2841 resctrl_arch_set_closid_rmid(t, to->closid,
2845 * Order the closid/rmid stores above before the loads
2846 * in task_curr(). This pairs with the full barrier
2847 * between the rq->curr update and resctrl_sched_in()
2848 * during context switch.
2853 * If the task is on a CPU, set the CPU in the mask.
2854 * The detection is inaccurate as tasks might move or
2855 * schedule before the smp function call takes place.
2856 * In such a case the function call is pointless, but
2857 * there is no other side effect.
2859 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
2860 cpumask_set_cpu(task_cpu(t), mask);
2863 read_unlock(&tasklist_lock);
2866 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2868 struct rdtgroup *sentry, *stmp;
2869 struct list_head *head;
2871 head = &rdtgrp->mon.crdtgrp_list;
2872 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2873 free_rmid(sentry->closid, sentry->mon.rmid);
2874 list_del(&sentry->mon.crdtgrp_list);
2876 if (atomic_read(&sentry->waitcount) != 0)
2877 sentry->flags = RDT_DELETED;
2879 rdtgroup_remove(sentry);
2884 * Forcibly remove all of subdirectories under root.
2886 static void rmdir_all_sub(void)
2888 struct rdtgroup *rdtgrp, *tmp;
2890 /* Move all tasks to the default resource group */
2891 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2893 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2894 /* Free any child rmids */
2895 free_all_child_rdtgrp(rdtgrp);
2897 /* Remove each rdtgroup other than root */
2898 if (rdtgrp == &rdtgroup_default)
2901 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2902 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2903 rdtgroup_pseudo_lock_remove(rdtgrp);
2906 * Give any CPUs back to the default group. We cannot copy
2907 * cpu_online_mask because a CPU might have executed the
2908 * offline callback already, but is still marked online.
2910 cpumask_or(&rdtgroup_default.cpu_mask,
2911 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2913 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
2915 kernfs_remove(rdtgrp->kn);
2916 list_del(&rdtgrp->rdtgroup_list);
2918 if (atomic_read(&rdtgrp->waitcount) != 0)
2919 rdtgrp->flags = RDT_DELETED;
2921 rdtgroup_remove(rdtgrp);
2923 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2924 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2926 kernfs_remove(kn_info);
2927 kernfs_remove(kn_mongrp);
2928 kernfs_remove(kn_mondata);
2931 static void rdt_kill_sb(struct super_block *sb)
2933 struct rdt_resource *r;
2936 mutex_lock(&rdtgroup_mutex);
2940 /*Put everything back to default values. */
2941 for_each_alloc_capable_rdt_resource(r)
2944 rdt_pseudo_lock_release();
2945 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2946 schemata_list_destroy();
2947 rdtgroup_destroy_root();
2948 if (resctrl_arch_alloc_capable())
2949 resctrl_arch_disable_alloc();
2950 if (resctrl_arch_mon_capable())
2951 resctrl_arch_disable_mon();
2952 resctrl_mounted = false;
2954 mutex_unlock(&rdtgroup_mutex);
2958 static struct file_system_type rdt_fs_type = {
2960 .init_fs_context = rdt_init_fs_context,
2961 .parameters = rdt_fs_parameters,
2962 .kill_sb = rdt_kill_sb,
2965 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2968 struct kernfs_node *kn;
2971 kn = __kernfs_create_file(parent_kn, name, 0444,
2972 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
2973 &kf_mondata_ops, priv, NULL, NULL);
2977 ret = rdtgroup_kn_set_ugid(kn);
2987 * Remove all subdirectories of mon_data of ctrl_mon groups
2988 * and monitor groups with given domain id.
2990 static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2991 unsigned int dom_id)
2993 struct rdtgroup *prgrp, *crgrp;
2996 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2997 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2998 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
3000 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
3001 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
3005 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
3006 struct rdt_domain *d,
3007 struct rdt_resource *r, struct rdtgroup *prgrp)
3009 union mon_data_bits priv;
3010 struct kernfs_node *kn;
3011 struct mon_evt *mevt;
3012 struct rmid_read rr;
3016 sprintf(name, "mon_%s_%02d", r->name, d->id);
3017 /* create the directory */
3018 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
3022 ret = rdtgroup_kn_set_ugid(kn);
3026 if (WARN_ON(list_empty(&r->evt_list))) {
3031 priv.u.rid = r->rid;
3032 priv.u.domid = d->id;
3033 list_for_each_entry(mevt, &r->evt_list, list) {
3034 priv.u.evtid = mevt->evtid;
3035 ret = mon_addfile(kn, mevt->name, priv.priv);
3039 if (is_mbm_event(mevt->evtid))
3040 mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
3042 kernfs_activate(kn);
3051 * Add all subdirectories of mon_data for "ctrl_mon" groups
3052 * and "monitor" groups with given domain id.
3054 static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
3055 struct rdt_domain *d)
3057 struct kernfs_node *parent_kn;
3058 struct rdtgroup *prgrp, *crgrp;
3059 struct list_head *head;
3061 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
3062 parent_kn = prgrp->mon.mon_data_kn;
3063 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
3065 head = &prgrp->mon.crdtgrp_list;
3066 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
3067 parent_kn = crgrp->mon.mon_data_kn;
3068 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
3073 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
3074 struct rdt_resource *r,
3075 struct rdtgroup *prgrp)
3077 struct rdt_domain *dom;
3080 list_for_each_entry(dom, &r->domains, list) {
3081 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
3090 * This creates a directory mon_data which contains the monitored data.
3092 * mon_data has one directory for each domain which are named
3093 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
3094 * with L3 domain looks as below:
3101 * Each domain directory has one file per event:
3106 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
3107 struct rdtgroup *prgrp,
3108 struct kernfs_node **dest_kn)
3110 struct rdt_resource *r;
3111 struct kernfs_node *kn;
3115 * Create the mon_data directory first.
3117 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
3125 * Create the subdirectories for each domain. Note that all events
3126 * in a domain like L3 are grouped into a resource whose domain is L3
3128 for_each_mon_capable_rdt_resource(r) {
3129 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
3142 * cbm_ensure_valid - Enforce validity on provided CBM
3143 * @_val: Candidate CBM
3144 * @r: RDT resource to which the CBM belongs
3146 * The provided CBM represents all cache portions available for use. This
3147 * may be represented by a bitmap that does not consist of contiguous ones
3148 * and thus be an invalid CBM.
3149 * Here the provided CBM is forced to be a valid CBM by only considering
3150 * the first set of contiguous bits as valid and clearing all bits.
3151 * The intention here is to provide a valid default CBM with which a new
3152 * resource group is initialized. The user can follow this with a
3153 * modification to the CBM if the default does not satisfy the
3156 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
3158 unsigned int cbm_len = r->cache.cbm_len;
3159 unsigned long first_bit, zero_bit;
3160 unsigned long val = _val;
3165 first_bit = find_first_bit(&val, cbm_len);
3166 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
3168 /* Clear any remaining bits to ensure contiguous region */
3169 bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
3174 * Initialize cache resources per RDT domain
3176 * Set the RDT domain up to start off with all usable allocations. That is,
3177 * all shareable and unused bits. All-zero CBM is invalid.
3179 static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
3182 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
3183 enum resctrl_conf_type t = s->conf_type;
3184 struct resctrl_staged_config *cfg;
3185 struct rdt_resource *r = s->res;
3186 u32 used_b = 0, unused_b = 0;
3187 unsigned long tmp_cbm;
3188 enum rdtgrp_mode mode;
3189 u32 peer_ctl, ctrl_val;
3192 cfg = &d->staged_config[t];
3193 cfg->have_new_ctrl = false;
3194 cfg->new_ctrl = r->cache.shareable_bits;
3195 used_b = r->cache.shareable_bits;
3196 for (i = 0; i < closids_supported(); i++) {
3197 if (closid_allocated(i) && i != closid) {
3198 mode = rdtgroup_mode_by_closid(i);
3199 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
3201 * ctrl values for locksetup aren't relevant
3202 * until the schemata is written, and the mode
3203 * becomes RDT_MODE_PSEUDO_LOCKED.
3207 * If CDP is active include peer domain's
3208 * usage to ensure there is no overlap
3209 * with an exclusive group.
3211 if (resctrl_arch_get_cdp_enabled(r->rid))
3212 peer_ctl = resctrl_arch_get_config(r, d, i,
3216 ctrl_val = resctrl_arch_get_config(r, d, i,
3218 used_b |= ctrl_val | peer_ctl;
3219 if (mode == RDT_MODE_SHAREABLE)
3220 cfg->new_ctrl |= ctrl_val | peer_ctl;
3223 if (d->plr && d->plr->cbm > 0)
3224 used_b |= d->plr->cbm;
3225 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
3226 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
3227 cfg->new_ctrl |= unused_b;
3229 * Force the initial CBM to be valid, user can
3230 * modify the CBM based on system availability.
3232 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
3234 * Assign the u32 CBM to an unsigned long to ensure that
3235 * bitmap_weight() does not access out-of-bound memory.
3237 tmp_cbm = cfg->new_ctrl;
3238 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
3239 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id);
3242 cfg->have_new_ctrl = true;
3248 * Initialize cache resources with default values.
3250 * A new RDT group is being created on an allocation capable (CAT)
3251 * supporting system. Set this group up to start off with all usable
3254 * If there are no more shareable bits available on any domain then
3255 * the entire allocation will fail.
3257 static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
3259 struct rdt_domain *d;
3262 list_for_each_entry(d, &s->res->domains, list) {
3263 ret = __init_one_rdt_domain(d, s, closid);
3271 /* Initialize MBA resource with default values. */
3272 static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
3274 struct resctrl_staged_config *cfg;
3275 struct rdt_domain *d;
3277 list_for_each_entry(d, &r->domains, list) {
3279 d->mbps_val[closid] = MBA_MAX_MBPS;
3283 cfg = &d->staged_config[CDP_NONE];
3284 cfg->new_ctrl = r->default_ctrl;
3285 cfg->have_new_ctrl = true;
3289 /* Initialize the RDT group's allocations. */
3290 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
3292 struct resctrl_schema *s;
3293 struct rdt_resource *r;
3296 rdt_staged_configs_clear();
3298 list_for_each_entry(s, &resctrl_schema_all, list) {
3300 if (r->rid == RDT_RESOURCE_MBA ||
3301 r->rid == RDT_RESOURCE_SMBA) {
3302 rdtgroup_init_mba(r, rdtgrp->closid);
3306 ret = rdtgroup_init_cat(s, rdtgrp->closid);
3311 ret = resctrl_arch_update_domains(r, rdtgrp->closid);
3313 rdt_last_cmd_puts("Failed to initialize allocations\n");
3319 rdtgrp->mode = RDT_MODE_SHAREABLE;
3322 rdt_staged_configs_clear();
3326 static int mkdir_rdt_prepare_rmid_alloc(struct rdtgroup *rdtgrp)
3330 if (!resctrl_arch_mon_capable())
3333 ret = alloc_rmid(rdtgrp->closid);
3335 rdt_last_cmd_puts("Out of RMIDs\n");
3338 rdtgrp->mon.rmid = ret;
3340 ret = mkdir_mondata_all(rdtgrp->kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
3342 rdt_last_cmd_puts("kernfs subdir error\n");
3343 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
3350 static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)
3352 if (resctrl_arch_mon_capable())
3353 free_rmid(rgrp->closid, rgrp->mon.rmid);
3356 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
3357 const char *name, umode_t mode,
3358 enum rdt_group_type rtype, struct rdtgroup **r)
3360 struct rdtgroup *prdtgrp, *rdtgrp;
3361 unsigned long files = 0;
3362 struct kernfs_node *kn;
3365 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
3371 if (rtype == RDTMON_GROUP &&
3372 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3373 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
3375 rdt_last_cmd_puts("Pseudo-locking in progress\n");
3379 /* allocate the rdtgroup. */
3380 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
3383 rdt_last_cmd_puts("Kernel out of memory\n");
3387 rdtgrp->mon.parent = prdtgrp;
3388 rdtgrp->type = rtype;
3389 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
3391 /* kernfs creates the directory for rdtgrp */
3392 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
3395 rdt_last_cmd_puts("kernfs create error\n");
3401 * kernfs_remove() will drop the reference count on "kn" which
3402 * will free it. But we still need it to stick around for the
3403 * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
3404 * which will be dropped by kernfs_put() in rdtgroup_remove().
3408 ret = rdtgroup_kn_set_ugid(kn);
3410 rdt_last_cmd_puts("kernfs perm error\n");
3414 if (rtype == RDTCTRL_GROUP) {
3415 files = RFTYPE_BASE | RFTYPE_CTRL;
3416 if (resctrl_arch_mon_capable())
3417 files |= RFTYPE_MON;
3419 files = RFTYPE_BASE | RFTYPE_MON;
3422 ret = rdtgroup_add_files(kn, files);
3424 rdt_last_cmd_puts("kernfs fill error\n");
3429 * The caller unlocks the parent_kn upon success.
3434 kernfs_put(rdtgrp->kn);
3435 kernfs_remove(rdtgrp->kn);
3439 rdtgroup_kn_unlock(parent_kn);
3443 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
3445 kernfs_remove(rgrp->kn);
3446 rdtgroup_remove(rgrp);
3450 * Create a monitor group under "mon_groups" directory of a control
3451 * and monitor group(ctrl_mon). This is a resource group
3452 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
3454 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
3455 const char *name, umode_t mode)
3457 struct rdtgroup *rdtgrp, *prgrp;
3460 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
3464 prgrp = rdtgrp->mon.parent;
3465 rdtgrp->closid = prgrp->closid;
3467 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp);
3469 mkdir_rdt_prepare_clean(rdtgrp);
3473 kernfs_activate(rdtgrp->kn);
3476 * Add the rdtgrp to the list of rdtgrps the parent
3477 * ctrl_mon group has to track.
3479 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
3482 rdtgroup_kn_unlock(parent_kn);
3487 * These are rdtgroups created under the root directory. Can be used
3488 * to allocate and monitor resources.
3490 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
3491 const char *name, umode_t mode)
3493 struct rdtgroup *rdtgrp;
3494 struct kernfs_node *kn;
3498 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
3503 ret = closid_alloc();
3505 rdt_last_cmd_puts("Out of CLOSIDs\n");
3506 goto out_common_fail;
3511 rdtgrp->closid = closid;
3513 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp);
3515 goto out_closid_free;
3517 kernfs_activate(rdtgrp->kn);
3519 ret = rdtgroup_init_alloc(rdtgrp);
3523 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
3525 if (resctrl_arch_mon_capable()) {
3527 * Create an empty mon_groups directory to hold the subset
3528 * of tasks and cpus to monitor.
3530 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
3532 rdt_last_cmd_puts("kernfs subdir error\n");
3540 list_del(&rdtgrp->rdtgroup_list);
3542 mkdir_rdt_prepare_rmid_free(rdtgrp);
3544 closid_free(closid);
3546 mkdir_rdt_prepare_clean(rdtgrp);
3548 rdtgroup_kn_unlock(parent_kn);
3553 * We allow creating mon groups only with in a directory called "mon_groups"
3554 * which is present in every ctrl_mon group. Check if this is a valid
3555 * "mon_groups" directory.
3557 * 1. The directory should be named "mon_groups".
3558 * 2. The mon group itself should "not" be named "mon_groups".
3559 * This makes sure "mon_groups" directory always has a ctrl_mon group
3562 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
3564 return (!strcmp(kn->name, "mon_groups") &&
3565 strcmp(name, "mon_groups"));
3568 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3571 /* Do not accept '\n' to avoid unparsable situation. */
3572 if (strchr(name, '\n'))
3576 * If the parent directory is the root directory and RDT
3577 * allocation is supported, add a control and monitoring
3580 if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn)
3581 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
3584 * If RDT monitoring is supported and the parent directory is a valid
3585 * "mon_groups" directory, add a monitoring subdirectory.
3587 if (resctrl_arch_mon_capable() && is_mon_groups(parent_kn, name))
3588 return rdtgroup_mkdir_mon(parent_kn, name, mode);
3593 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3595 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3598 /* Give any tasks back to the parent group */
3599 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
3601 /* Update per cpu rmid of the moved CPUs first */
3602 for_each_cpu(cpu, &rdtgrp->cpu_mask)
3603 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
3605 * Update the MSR on moved CPUs and CPUs which have moved
3606 * task running on them.
3608 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3609 update_closid_rmid(tmpmask, NULL);
3611 rdtgrp->flags = RDT_DELETED;
3612 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
3615 * Remove the rdtgrp from the parent ctrl_mon group's list
3617 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3618 list_del(&rdtgrp->mon.crdtgrp_list);
3620 kernfs_remove(rdtgrp->kn);
3625 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
3627 rdtgrp->flags = RDT_DELETED;
3628 list_del(&rdtgrp->rdtgroup_list);
3630 kernfs_remove(rdtgrp->kn);
3634 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3638 /* Give any tasks back to the default group */
3639 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
3641 /* Give any CPUs back to the default group */
3642 cpumask_or(&rdtgroup_default.cpu_mask,
3643 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
3645 /* Update per cpu closid and rmid of the moved CPUs first */
3646 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
3647 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3648 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
3652 * Update the MSR on moved CPUs and CPUs which have moved
3653 * task running on them.
3655 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3656 update_closid_rmid(tmpmask, NULL);
3658 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
3659 closid_free(rdtgrp->closid);
3661 rdtgroup_ctrl_remove(rdtgrp);
3664 * Free all the child monitor group rmids.
3666 free_all_child_rdtgrp(rdtgrp);
3671 static int rdtgroup_rmdir(struct kernfs_node *kn)
3673 struct kernfs_node *parent_kn = kn->parent;
3674 struct rdtgroup *rdtgrp;
3675 cpumask_var_t tmpmask;
3678 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3681 rdtgrp = rdtgroup_kn_lock_live(kn);
3688 * If the rdtgroup is a ctrl_mon group and parent directory
3689 * is the root directory, remove the ctrl_mon group.
3691 * If the rdtgroup is a mon group and parent directory
3692 * is a valid "mon_groups" directory, remove the mon group.
3694 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3695 rdtgrp != &rdtgroup_default) {
3696 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3697 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
3698 ret = rdtgroup_ctrl_remove(rdtgrp);
3700 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
3702 } else if (rdtgrp->type == RDTMON_GROUP &&
3703 is_mon_groups(parent_kn, kn->name)) {
3704 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
3710 rdtgroup_kn_unlock(kn);
3711 free_cpumask_var(tmpmask);
3716 * mongrp_reparent() - replace parent CTRL_MON group of a MON group
3717 * @rdtgrp: the MON group whose parent should be replaced
3718 * @new_prdtgrp: replacement parent CTRL_MON group for @rdtgrp
3719 * @cpus: cpumask provided by the caller for use during this call
3721 * Replaces the parent CTRL_MON group for a MON group, resulting in all member
3722 * tasks' CLOSID immediately changing to that of the new parent group.
3723 * Monitoring data for the group is unaffected by this operation.
3725 static void mongrp_reparent(struct rdtgroup *rdtgrp,
3726 struct rdtgroup *new_prdtgrp,
3729 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3731 WARN_ON(rdtgrp->type != RDTMON_GROUP);
3732 WARN_ON(new_prdtgrp->type != RDTCTRL_GROUP);
3734 /* Nothing to do when simply renaming a MON group. */
3735 if (prdtgrp == new_prdtgrp)
3738 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3739 list_move_tail(&rdtgrp->mon.crdtgrp_list,
3740 &new_prdtgrp->mon.crdtgrp_list);
3742 rdtgrp->mon.parent = new_prdtgrp;
3743 rdtgrp->closid = new_prdtgrp->closid;
3745 /* Propagate updated closid to all tasks in this group. */
3746 rdt_move_group_tasks(rdtgrp, rdtgrp, cpus);
3748 update_closid_rmid(cpus, NULL);
3751 static int rdtgroup_rename(struct kernfs_node *kn,
3752 struct kernfs_node *new_parent, const char *new_name)
3754 struct rdtgroup *new_prdtgrp;
3755 struct rdtgroup *rdtgrp;
3756 cpumask_var_t tmpmask;
3759 rdtgrp = kernfs_to_rdtgroup(kn);
3760 new_prdtgrp = kernfs_to_rdtgroup(new_parent);
3761 if (!rdtgrp || !new_prdtgrp)
3764 /* Release both kernfs active_refs before obtaining rdtgroup mutex. */
3765 rdtgroup_kn_get(rdtgrp, kn);
3766 rdtgroup_kn_get(new_prdtgrp, new_parent);
3768 mutex_lock(&rdtgroup_mutex);
3770 rdt_last_cmd_clear();
3773 * Don't allow kernfs_to_rdtgroup() to return a parent rdtgroup if
3774 * either kernfs_node is a file.
3776 if (kernfs_type(kn) != KERNFS_DIR ||
3777 kernfs_type(new_parent) != KERNFS_DIR) {
3778 rdt_last_cmd_puts("Source and destination must be directories");
3783 if ((rdtgrp->flags & RDT_DELETED) || (new_prdtgrp->flags & RDT_DELETED)) {
3788 if (rdtgrp->type != RDTMON_GROUP || !kn->parent ||
3789 !is_mon_groups(kn->parent, kn->name)) {
3790 rdt_last_cmd_puts("Source must be a MON group\n");
3795 if (!is_mon_groups(new_parent, new_name)) {
3796 rdt_last_cmd_puts("Destination must be a mon_groups subdirectory\n");
3802 * If the MON group is monitoring CPUs, the CPUs must be assigned to the
3803 * current parent CTRL_MON group and therefore cannot be assigned to
3804 * the new parent, making the move illegal.
3806 if (!cpumask_empty(&rdtgrp->cpu_mask) &&
3807 rdtgrp->mon.parent != new_prdtgrp) {
3808 rdt_last_cmd_puts("Cannot move a MON group that monitors CPUs\n");
3814 * Allocate the cpumask for use in mongrp_reparent() to avoid the
3815 * possibility of failing to allocate it after kernfs_rename() has
3818 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) {
3824 * Perform all input validation and allocations needed to ensure
3825 * mongrp_reparent() will succeed before calling kernfs_rename(),
3826 * otherwise it would be necessary to revert this call if
3827 * mongrp_reparent() failed.
3829 ret = kernfs_rename(kn, new_parent, new_name);
3831 mongrp_reparent(rdtgrp, new_prdtgrp, tmpmask);
3833 free_cpumask_var(tmpmask);
3836 mutex_unlock(&rdtgroup_mutex);
3837 rdtgroup_kn_put(rdtgrp, kn);
3838 rdtgroup_kn_put(new_prdtgrp, new_parent);
3842 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3844 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
3845 seq_puts(seq, ",cdp");
3847 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
3848 seq_puts(seq, ",cdpl2");
3850 if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
3851 seq_puts(seq, ",mba_MBps");
3854 seq_puts(seq, ",debug");
3859 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
3860 .mkdir = rdtgroup_mkdir,
3861 .rmdir = rdtgroup_rmdir,
3862 .rename = rdtgroup_rename,
3863 .show_options = rdtgroup_show_options,
3866 static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
3868 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
3869 KERNFS_ROOT_CREATE_DEACTIVATED |
3870 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
3872 if (IS_ERR(rdt_root))
3873 return PTR_ERR(rdt_root);
3875 ctx->kfc.root = rdt_root;
3876 rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
3881 static void rdtgroup_destroy_root(void)
3883 kernfs_destroy_root(rdt_root);
3884 rdtgroup_default.kn = NULL;
3887 static void __init rdtgroup_setup_default(void)
3889 mutex_lock(&rdtgroup_mutex);
3891 rdtgroup_default.closid = RESCTRL_RESERVED_CLOSID;
3892 rdtgroup_default.mon.rmid = RESCTRL_RESERVED_RMID;
3893 rdtgroup_default.type = RDTCTRL_GROUP;
3894 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3896 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3898 mutex_unlock(&rdtgroup_mutex);
3901 static void domain_destroy_mon_state(struct rdt_domain *d)
3903 bitmap_free(d->rmid_busy_llc);
3904 kfree(d->mbm_total);
3905 kfree(d->mbm_local);
3908 void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
3910 lockdep_assert_held(&rdtgroup_mutex);
3912 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3913 mba_sc_domain_destroy(r, d);
3915 if (!r->mon_capable)
3919 * If resctrl is mounted, remove all the
3920 * per domain monitor data directories.
3922 if (resctrl_mounted && resctrl_arch_mon_capable())
3923 rmdir_mondata_subdir_allrdtgrp(r, d->id);
3925 if (is_mbm_enabled())
3926 cancel_delayed_work(&d->mbm_over);
3927 if (is_llc_occupancy_enabled() && has_busy_rmid(d)) {
3929 * When a package is going down, forcefully
3930 * decrement rmid->ebusy. There is no way to know
3931 * that the L3 was flushed and hence may lead to
3932 * incorrect counts in rare scenarios, but leaving
3933 * the RMID as busy creates RMID leaks if the
3934 * package never comes back.
3936 __check_limbo(d, true);
3937 cancel_delayed_work(&d->cqm_limbo);
3940 domain_destroy_mon_state(d);
3943 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
3945 u32 idx_limit = resctrl_arch_system_num_rmid_idx();
3948 if (is_llc_occupancy_enabled()) {
3949 d->rmid_busy_llc = bitmap_zalloc(idx_limit, GFP_KERNEL);
3950 if (!d->rmid_busy_llc)
3953 if (is_mbm_total_enabled()) {
3954 tsize = sizeof(*d->mbm_total);
3955 d->mbm_total = kcalloc(idx_limit, tsize, GFP_KERNEL);
3956 if (!d->mbm_total) {
3957 bitmap_free(d->rmid_busy_llc);
3961 if (is_mbm_local_enabled()) {
3962 tsize = sizeof(*d->mbm_local);
3963 d->mbm_local = kcalloc(idx_limit, tsize, GFP_KERNEL);
3964 if (!d->mbm_local) {
3965 bitmap_free(d->rmid_busy_llc);
3966 kfree(d->mbm_total);
3974 int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
3978 lockdep_assert_held(&rdtgroup_mutex);
3980 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3981 /* RDT_RESOURCE_MBA is never mon_capable */
3982 return mba_sc_domain_allocate(r, d);
3984 if (!r->mon_capable)
3987 err = domain_setup_mon_state(r, d);
3991 if (is_mbm_enabled()) {
3992 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
3993 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL,
3994 RESCTRL_PICK_ANY_CPU);
3997 if (is_llc_occupancy_enabled())
3998 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
4001 * If the filesystem is not mounted then only the default resource group
4002 * exists. Creation of its directories is deferred until mount time
4003 * by rdt_get_tree() calling mkdir_mondata_all().
4004 * If resctrl is mounted, add per domain monitor data directories.
4006 if (resctrl_mounted && resctrl_arch_mon_capable())
4007 mkdir_mondata_subdir_allrdtgrp(r, d);
4012 void resctrl_online_cpu(unsigned int cpu)
4014 lockdep_assert_held(&rdtgroup_mutex);
4016 /* The CPU is set in default rdtgroup after online. */
4017 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
4021 * rdtgroup_init - rdtgroup initialization
4023 * Setup resctrl file system including set up root, create mount point,
4024 * register rdtgroup filesystem, and initialize files under root directory.
4026 * Return: 0 on success or -errno
4028 int __init rdtgroup_init(void)
4032 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
4033 sizeof(last_cmd_status_buf));
4035 rdtgroup_setup_default();
4037 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
4041 ret = register_filesystem(&rdt_fs_type);
4043 goto cleanup_mountpoint;
4046 * Adding the resctrl debugfs directory here may not be ideal since
4047 * it would let the resctrl debugfs directory appear on the debugfs
4048 * filesystem before the resctrl filesystem is mounted.
4049 * It may also be ok since that would enable debugging of RDT before
4050 * resctrl is mounted.
4051 * The reason why the debugfs directory is created here and not in
4052 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
4053 * during the debugfs directory creation also &sb->s_type->i_mutex_key
4054 * (the lockdep class of inode->i_rwsem). Other filesystem
4055 * interactions (eg. SyS_getdents) have the lock ordering:
4056 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
4057 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
4058 * is taken, thus creating dependency:
4059 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
4060 * issues considering the other two lock dependencies.
4061 * By creating the debugfs directory here we avoid a dependency
4062 * that may cause deadlock (even though file operations cannot
4063 * occur until the filesystem is mounted, but I do not know how to
4064 * tell lockdep that).
4066 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
4071 sysfs_remove_mount_point(fs_kobj, "resctrl");
4076 void __exit rdtgroup_exit(void)
4078 debugfs_remove_recursive(debugfs_resctrl);
4079 unregister_filesystem(&rdt_fs_type);
4080 sysfs_remove_mount_point(fs_kobj, "resctrl");