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);
39 /* Mutex to protect rdtgroup access. */
40 DEFINE_MUTEX(rdtgroup_mutex);
42 static struct kernfs_root *rdt_root;
43 struct rdtgroup rdtgroup_default;
44 LIST_HEAD(rdt_all_groups);
46 /* list of entries for the schemata file */
47 LIST_HEAD(resctrl_schema_all);
49 /* The filesystem can only be mounted once. */
52 /* Kernel fs node for "info" directory under root */
53 static struct kernfs_node *kn_info;
55 /* Kernel fs node for "mon_groups" directory under root */
56 static struct kernfs_node *kn_mongrp;
58 /* Kernel fs node for "mon_data" directory under root */
59 static struct kernfs_node *kn_mondata;
61 static struct seq_buf last_cmd_status;
62 static char last_cmd_status_buf[512];
64 static int rdtgroup_setup_root(struct rdt_fs_context *ctx);
65 static void rdtgroup_destroy_root(void);
67 struct dentry *debugfs_resctrl;
69 static bool resctrl_debug;
71 void rdt_last_cmd_clear(void)
73 lockdep_assert_held(&rdtgroup_mutex);
74 seq_buf_clear(&last_cmd_status);
77 void rdt_last_cmd_puts(const char *s)
79 lockdep_assert_held(&rdtgroup_mutex);
80 seq_buf_puts(&last_cmd_status, s);
83 void rdt_last_cmd_printf(const char *fmt, ...)
88 lockdep_assert_held(&rdtgroup_mutex);
89 seq_buf_vprintf(&last_cmd_status, fmt, ap);
93 void rdt_staged_configs_clear(void)
95 struct rdt_resource *r;
96 struct rdt_domain *dom;
98 lockdep_assert_held(&rdtgroup_mutex);
100 for_each_alloc_capable_rdt_resource(r) {
101 list_for_each_entry(dom, &r->domains, list)
102 memset(dom->staged_config, 0, sizeof(dom->staged_config));
107 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
108 * we can keep a bitmap of free CLOSIDs in a single integer.
110 * Using a global CLOSID across all resources has some advantages and
112 * + We can simply set current's closid to assign a task to a resource
114 * + Context switch code can avoid extra memory references deciding which
115 * CLOSID to load into the PQR_ASSOC MSR
116 * - We give up some options in configuring resource groups across multi-socket
118 * - Our choices on how to configure each resource become progressively more
119 * limited as the number of resources grows.
121 static unsigned long closid_free_map;
122 static int closid_free_map_len;
124 int closids_supported(void)
126 return closid_free_map_len;
129 static void closid_init(void)
131 struct resctrl_schema *s;
132 u32 rdt_min_closid = 32;
134 /* Compute rdt_min_closid across all resources */
135 list_for_each_entry(s, &resctrl_schema_all, list)
136 rdt_min_closid = min(rdt_min_closid, s->num_closid);
138 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
140 /* RESCTRL_RESERVED_CLOSID is always reserved for the default group */
141 __clear_bit(RESCTRL_RESERVED_CLOSID, &closid_free_map);
142 closid_free_map_len = rdt_min_closid;
145 static int closid_alloc(void)
150 lockdep_assert_held(&rdtgroup_mutex);
152 if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
153 cleanest_closid = resctrl_find_cleanest_closid();
154 if (cleanest_closid < 0)
155 return cleanest_closid;
156 closid = cleanest_closid;
158 closid = ffs(closid_free_map);
163 __clear_bit(closid, &closid_free_map);
168 void closid_free(int closid)
170 lockdep_assert_held(&rdtgroup_mutex);
172 __set_bit(closid, &closid_free_map);
176 * closid_allocated - test if provided closid is in use
177 * @closid: closid to be tested
179 * Return: true if @closid is currently associated with a resource group,
180 * false if @closid is free
182 bool closid_allocated(unsigned int closid)
184 lockdep_assert_held(&rdtgroup_mutex);
186 return !test_bit(closid, &closid_free_map);
190 * rdtgroup_mode_by_closid - Return mode of resource group with closid
191 * @closid: closid if the resource group
193 * Each resource group is associated with a @closid. Here the mode
194 * of a resource group can be queried by searching for it using its closid.
196 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
198 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
200 struct rdtgroup *rdtgrp;
202 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
203 if (rdtgrp->closid == closid)
207 return RDT_NUM_MODES;
210 static const char * const rdt_mode_str[] = {
211 [RDT_MODE_SHAREABLE] = "shareable",
212 [RDT_MODE_EXCLUSIVE] = "exclusive",
213 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
214 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
218 * rdtgroup_mode_str - Return the string representation of mode
219 * @mode: the resource group mode as &enum rdtgroup_mode
221 * Return: string representation of valid mode, "unknown" otherwise
223 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
225 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
228 return rdt_mode_str[mode];
231 /* set uid and gid of rdtgroup dirs and files to that of the creator */
232 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
234 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
235 .ia_uid = current_fsuid(),
236 .ia_gid = current_fsgid(), };
238 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
239 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
242 return kernfs_setattr(kn, &iattr);
245 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
247 struct kernfs_node *kn;
250 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
251 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
252 0, rft->kf_ops, rft, NULL, NULL);
256 ret = rdtgroup_kn_set_ugid(kn);
265 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
267 struct kernfs_open_file *of = m->private;
268 struct rftype *rft = of->kn->priv;
271 return rft->seq_show(of, m, arg);
275 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
276 size_t nbytes, loff_t off)
278 struct rftype *rft = of->kn->priv;
281 return rft->write(of, buf, nbytes, off);
286 static const struct kernfs_ops rdtgroup_kf_single_ops = {
287 .atomic_write_len = PAGE_SIZE,
288 .write = rdtgroup_file_write,
289 .seq_show = rdtgroup_seqfile_show,
292 static const struct kernfs_ops kf_mondata_ops = {
293 .atomic_write_len = PAGE_SIZE,
294 .seq_show = rdtgroup_mondata_show,
297 static bool is_cpu_list(struct kernfs_open_file *of)
299 struct rftype *rft = of->kn->priv;
301 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
304 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
305 struct seq_file *s, void *v)
307 struct rdtgroup *rdtgrp;
308 struct cpumask *mask;
311 rdtgrp = rdtgroup_kn_lock_live(of->kn);
314 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
315 if (!rdtgrp->plr->d) {
316 rdt_last_cmd_clear();
317 rdt_last_cmd_puts("Cache domain offline\n");
320 mask = &rdtgrp->plr->d->cpu_mask;
321 seq_printf(s, is_cpu_list(of) ?
322 "%*pbl\n" : "%*pb\n",
323 cpumask_pr_args(mask));
326 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
327 cpumask_pr_args(&rdtgrp->cpu_mask));
332 rdtgroup_kn_unlock(of->kn);
338 * This is safe against resctrl_sched_in() called from __switch_to()
339 * because __switch_to() is executed with interrupts disabled. A local call
340 * from update_closid_rmid() is protected against __switch_to() because
341 * preemption is disabled.
343 static void update_cpu_closid_rmid(void *info)
345 struct rdtgroup *r = info;
348 this_cpu_write(pqr_state.default_closid, r->closid);
349 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
353 * We cannot unconditionally write the MSR because the current
354 * executing task might have its own closid selected. Just reuse
355 * the context switch code.
357 resctrl_sched_in(current);
361 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
363 * Per task closids/rmids must have been set up before calling this function.
366 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
368 on_each_cpu_mask(cpu_mask, update_cpu_closid_rmid, r, 1);
371 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
372 cpumask_var_t tmpmask)
374 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
375 struct list_head *head;
377 /* Check whether cpus belong to parent ctrl group */
378 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
379 if (!cpumask_empty(tmpmask)) {
380 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
384 /* Check whether cpus are dropped from this group */
385 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
386 if (!cpumask_empty(tmpmask)) {
387 /* Give any dropped cpus to parent rdtgroup */
388 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
389 update_closid_rmid(tmpmask, prgrp);
393 * If we added cpus, remove them from previous group that owned them
394 * and update per-cpu rmid
396 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
397 if (!cpumask_empty(tmpmask)) {
398 head = &prgrp->mon.crdtgrp_list;
399 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
402 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
405 update_closid_rmid(tmpmask, rdtgrp);
408 /* Done pushing/pulling - update this group with new mask */
409 cpumask_copy(&rdtgrp->cpu_mask, newmask);
414 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
416 struct rdtgroup *crgrp;
418 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
419 /* update the child mon group masks as well*/
420 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
421 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
424 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
425 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
427 struct rdtgroup *r, *crgrp;
428 struct list_head *head;
430 /* Check whether cpus are dropped from this group */
431 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
432 if (!cpumask_empty(tmpmask)) {
433 /* Can't drop from default group */
434 if (rdtgrp == &rdtgroup_default) {
435 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
439 /* Give any dropped cpus to rdtgroup_default */
440 cpumask_or(&rdtgroup_default.cpu_mask,
441 &rdtgroup_default.cpu_mask, tmpmask);
442 update_closid_rmid(tmpmask, &rdtgroup_default);
446 * If we added cpus, remove them from previous group and
447 * the prev group's child groups that owned them
448 * and update per-cpu closid/rmid.
450 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
451 if (!cpumask_empty(tmpmask)) {
452 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
455 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
456 if (!cpumask_empty(tmpmask1))
457 cpumask_rdtgrp_clear(r, tmpmask1);
459 update_closid_rmid(tmpmask, rdtgrp);
462 /* Done pushing/pulling - update this group with new mask */
463 cpumask_copy(&rdtgrp->cpu_mask, newmask);
466 * Clear child mon group masks since there is a new parent mask
467 * now and update the rmid for the cpus the child lost.
469 head = &rdtgrp->mon.crdtgrp_list;
470 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
471 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
472 update_closid_rmid(tmpmask, rdtgrp);
473 cpumask_clear(&crgrp->cpu_mask);
479 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
480 char *buf, size_t nbytes, loff_t off)
482 cpumask_var_t tmpmask, newmask, tmpmask1;
483 struct rdtgroup *rdtgrp;
489 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
491 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
492 free_cpumask_var(tmpmask);
495 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
496 free_cpumask_var(tmpmask);
497 free_cpumask_var(newmask);
501 rdtgrp = rdtgroup_kn_lock_live(of->kn);
507 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
508 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
510 rdt_last_cmd_puts("Pseudo-locking in progress\n");
515 ret = cpulist_parse(buf, newmask);
517 ret = cpumask_parse(buf, newmask);
520 rdt_last_cmd_puts("Bad CPU list/mask\n");
524 /* check that user didn't specify any offline cpus */
525 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
526 if (!cpumask_empty(tmpmask)) {
528 rdt_last_cmd_puts("Can only assign online CPUs\n");
532 if (rdtgrp->type == RDTCTRL_GROUP)
533 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
534 else if (rdtgrp->type == RDTMON_GROUP)
535 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
540 rdtgroup_kn_unlock(of->kn);
541 free_cpumask_var(tmpmask);
542 free_cpumask_var(newmask);
543 free_cpumask_var(tmpmask1);
545 return ret ?: nbytes;
549 * rdtgroup_remove - the helper to remove resource group safely
550 * @rdtgrp: resource group to remove
552 * On resource group creation via a mkdir, an extra kernfs_node reference is
553 * taken to ensure that the rdtgroup structure remains accessible for the
554 * rdtgroup_kn_unlock() calls where it is removed.
556 * Drop the extra reference here, then free the rdtgroup structure.
560 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
562 kernfs_put(rdtgrp->kn);
566 static void _update_task_closid_rmid(void *task)
569 * If the task is still current on this CPU, update PQR_ASSOC MSR.
570 * Otherwise, the MSR is updated when the task is scheduled in.
573 resctrl_sched_in(task);
576 static void update_task_closid_rmid(struct task_struct *t)
578 if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
579 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
581 _update_task_closid_rmid(t);
584 static bool task_in_rdtgroup(struct task_struct *tsk, struct rdtgroup *rdtgrp)
586 u32 closid, rmid = rdtgrp->mon.rmid;
588 if (rdtgrp->type == RDTCTRL_GROUP)
589 closid = rdtgrp->closid;
590 else if (rdtgrp->type == RDTMON_GROUP)
591 closid = rdtgrp->mon.parent->closid;
595 return resctrl_arch_match_closid(tsk, closid) &&
596 resctrl_arch_match_rmid(tsk, closid, rmid);
599 static int __rdtgroup_move_task(struct task_struct *tsk,
600 struct rdtgroup *rdtgrp)
602 /* If the task is already in rdtgrp, no need to move the task. */
603 if (task_in_rdtgroup(tsk, rdtgrp))
607 * Set the task's closid/rmid before the PQR_ASSOC MSR can be
610 * For ctrl_mon groups, move both closid and rmid.
611 * For monitor groups, can move the tasks only from
612 * their parent CTRL group.
614 if (rdtgrp->type == RDTMON_GROUP &&
615 !resctrl_arch_match_closid(tsk, rdtgrp->mon.parent->closid)) {
616 rdt_last_cmd_puts("Can't move task to different control group\n");
620 if (rdtgrp->type == RDTMON_GROUP)
621 resctrl_arch_set_closid_rmid(tsk, rdtgrp->mon.parent->closid,
624 resctrl_arch_set_closid_rmid(tsk, rdtgrp->closid,
628 * Ensure the task's closid and rmid are written before determining if
629 * the task is current that will decide if it will be interrupted.
630 * This pairs with the full barrier between the rq->curr update and
631 * resctrl_sched_in() during context switch.
636 * By now, the task's closid and rmid are set. If the task is current
637 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
638 * group go into effect. If the task is not current, the MSR will be
639 * updated when the task is scheduled in.
641 update_task_closid_rmid(tsk);
646 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
648 return (resctrl_arch_alloc_capable() && (r->type == RDTCTRL_GROUP) &&
649 resctrl_arch_match_closid(t, r->closid));
652 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
654 return (resctrl_arch_mon_capable() && (r->type == RDTMON_GROUP) &&
655 resctrl_arch_match_rmid(t, r->mon.parent->closid,
660 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
663 * Return: 1 if tasks have been assigned to @r, 0 otherwise
665 int rdtgroup_tasks_assigned(struct rdtgroup *r)
667 struct task_struct *p, *t;
670 lockdep_assert_held(&rdtgroup_mutex);
673 for_each_process_thread(p, t) {
674 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
684 static int rdtgroup_task_write_permission(struct task_struct *task,
685 struct kernfs_open_file *of)
687 const struct cred *tcred = get_task_cred(task);
688 const struct cred *cred = current_cred();
692 * Even if we're attaching all tasks in the thread group, we only
693 * need to check permissions on one of them.
695 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
696 !uid_eq(cred->euid, tcred->uid) &&
697 !uid_eq(cred->euid, tcred->suid)) {
698 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
706 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
707 struct kernfs_open_file *of)
709 struct task_struct *tsk;
714 tsk = find_task_by_vpid(pid);
717 rdt_last_cmd_printf("No task %d\n", pid);
724 get_task_struct(tsk);
727 ret = rdtgroup_task_write_permission(tsk, of);
729 ret = __rdtgroup_move_task(tsk, rdtgrp);
731 put_task_struct(tsk);
735 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
736 char *buf, size_t nbytes, loff_t off)
738 struct rdtgroup *rdtgrp;
743 rdtgrp = rdtgroup_kn_lock_live(of->kn);
745 rdtgroup_kn_unlock(of->kn);
748 rdt_last_cmd_clear();
750 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
751 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
753 rdt_last_cmd_puts("Pseudo-locking in progress\n");
757 while (buf && buf[0] != '\0' && buf[0] != '\n') {
758 pid_str = strim(strsep(&buf, ","));
760 if (kstrtoint(pid_str, 0, &pid)) {
761 rdt_last_cmd_printf("Task list parsing error pid %s\n", pid_str);
767 rdt_last_cmd_printf("Invalid pid %d\n", pid);
772 ret = rdtgroup_move_task(pid, rdtgrp, of);
774 rdt_last_cmd_printf("Error while processing task %d\n", pid);
780 rdtgroup_kn_unlock(of->kn);
782 return ret ?: nbytes;
785 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
787 struct task_struct *p, *t;
791 for_each_process_thread(p, t) {
792 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
793 pid = task_pid_vnr(t);
795 seq_printf(s, "%d\n", pid);
801 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
802 struct seq_file *s, void *v)
804 struct rdtgroup *rdtgrp;
807 rdtgrp = rdtgroup_kn_lock_live(of->kn);
809 show_rdt_tasks(rdtgrp, s);
812 rdtgroup_kn_unlock(of->kn);
817 static int rdtgroup_closid_show(struct kernfs_open_file *of,
818 struct seq_file *s, void *v)
820 struct rdtgroup *rdtgrp;
823 rdtgrp = rdtgroup_kn_lock_live(of->kn);
825 seq_printf(s, "%u\n", rdtgrp->closid);
828 rdtgroup_kn_unlock(of->kn);
833 static int rdtgroup_rmid_show(struct kernfs_open_file *of,
834 struct seq_file *s, void *v)
836 struct rdtgroup *rdtgrp;
839 rdtgrp = rdtgroup_kn_lock_live(of->kn);
841 seq_printf(s, "%u\n", rdtgrp->mon.rmid);
844 rdtgroup_kn_unlock(of->kn);
849 #ifdef CONFIG_PROC_CPU_RESCTRL
852 * A task can only be part of one resctrl control group and of one monitor
853 * group which is associated to that control group.
858 * resctrl is not available.
863 * Task is part of the root resctrl control group, and it is not associated
864 * to any monitor group.
869 * Task is part of the root resctrl control group and monitor group mon0.
874 * Task is part of resctrl control group group0, and it is not associated
875 * to any monitor group.
880 * Task is part of resctrl control group group0 and monitor group mon1.
882 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
883 struct pid *pid, struct task_struct *tsk)
885 struct rdtgroup *rdtg;
888 mutex_lock(&rdtgroup_mutex);
890 /* Return empty if resctrl has not been mounted. */
891 if (!resctrl_mounted) {
892 seq_puts(s, "res:\nmon:\n");
896 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
897 struct rdtgroup *crg;
900 * Task information is only relevant for shareable
901 * and exclusive groups.
903 if (rdtg->mode != RDT_MODE_SHAREABLE &&
904 rdtg->mode != RDT_MODE_EXCLUSIVE)
907 if (!resctrl_arch_match_closid(tsk, rdtg->closid))
910 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
913 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
915 if (!resctrl_arch_match_rmid(tsk, crg->mon.parent->closid,
918 seq_printf(s, "%s", crg->kn->name);
925 * The above search should succeed. Otherwise return
930 mutex_unlock(&rdtgroup_mutex);
936 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
937 struct seq_file *seq, void *v)
941 mutex_lock(&rdtgroup_mutex);
942 len = seq_buf_used(&last_cmd_status);
944 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
946 seq_puts(seq, "ok\n");
947 mutex_unlock(&rdtgroup_mutex);
951 static int rdt_num_closids_show(struct kernfs_open_file *of,
952 struct seq_file *seq, void *v)
954 struct resctrl_schema *s = of->kn->parent->priv;
956 seq_printf(seq, "%u\n", s->num_closid);
960 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
961 struct seq_file *seq, void *v)
963 struct resctrl_schema *s = of->kn->parent->priv;
964 struct rdt_resource *r = s->res;
966 seq_printf(seq, "%x\n", r->default_ctrl);
970 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
971 struct seq_file *seq, void *v)
973 struct resctrl_schema *s = of->kn->parent->priv;
974 struct rdt_resource *r = s->res;
976 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
980 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
981 struct seq_file *seq, void *v)
983 struct resctrl_schema *s = of->kn->parent->priv;
984 struct rdt_resource *r = s->res;
986 seq_printf(seq, "%x\n", r->cache.shareable_bits);
991 * rdt_bit_usage_show - Display current usage of resources
993 * A domain is a shared resource that can now be allocated differently. Here
994 * we display the current regions of the domain as an annotated bitmask.
995 * For each domain of this resource its allocation bitmask
996 * is annotated as below to indicate the current usage of the corresponding bit:
997 * 0 - currently unused
998 * X - currently available for sharing and used by software and hardware
999 * H - currently used by hardware only but available for software use
1000 * S - currently used and shareable by software only
1001 * E - currently used exclusively by one resource group
1002 * P - currently pseudo-locked by one resource group
1004 static int rdt_bit_usage_show(struct kernfs_open_file *of,
1005 struct seq_file *seq, void *v)
1007 struct resctrl_schema *s = of->kn->parent->priv;
1009 * Use unsigned long even though only 32 bits are used to ensure
1010 * test_bit() is used safely.
1012 unsigned long sw_shareable = 0, hw_shareable = 0;
1013 unsigned long exclusive = 0, pseudo_locked = 0;
1014 struct rdt_resource *r = s->res;
1015 struct rdt_domain *dom;
1016 int i, hwb, swb, excl, psl;
1017 enum rdtgrp_mode mode;
1022 mutex_lock(&rdtgroup_mutex);
1023 hw_shareable = r->cache.shareable_bits;
1024 list_for_each_entry(dom, &r->domains, list) {
1029 seq_printf(seq, "%d=", dom->id);
1030 for (i = 0; i < closids_supported(); i++) {
1031 if (!closid_allocated(i))
1033 ctrl_val = resctrl_arch_get_config(r, dom, i,
1035 mode = rdtgroup_mode_by_closid(i);
1037 case RDT_MODE_SHAREABLE:
1038 sw_shareable |= ctrl_val;
1040 case RDT_MODE_EXCLUSIVE:
1041 exclusive |= ctrl_val;
1043 case RDT_MODE_PSEUDO_LOCKSETUP:
1045 * RDT_MODE_PSEUDO_LOCKSETUP is possible
1046 * here but not included since the CBM
1047 * associated with this CLOSID in this mode
1048 * is not initialized and no task or cpu can be
1049 * assigned this CLOSID.
1052 case RDT_MODE_PSEUDO_LOCKED:
1055 "invalid mode for closid %d\n", i);
1059 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
1060 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
1061 hwb = test_bit(i, &hw_shareable);
1062 swb = test_bit(i, &sw_shareable);
1063 excl = test_bit(i, &exclusive);
1064 psl = test_bit(i, &pseudo_locked);
1067 else if (hwb && !swb)
1069 else if (!hwb && swb)
1075 else /* Unused bits remain */
1080 seq_putc(seq, '\n');
1081 mutex_unlock(&rdtgroup_mutex);
1086 static int rdt_min_bw_show(struct kernfs_open_file *of,
1087 struct seq_file *seq, void *v)
1089 struct resctrl_schema *s = of->kn->parent->priv;
1090 struct rdt_resource *r = s->res;
1092 seq_printf(seq, "%u\n", r->membw.min_bw);
1096 static int rdt_num_rmids_show(struct kernfs_open_file *of,
1097 struct seq_file *seq, void *v)
1099 struct rdt_resource *r = of->kn->parent->priv;
1101 seq_printf(seq, "%d\n", r->num_rmid);
1106 static int rdt_mon_features_show(struct kernfs_open_file *of,
1107 struct seq_file *seq, void *v)
1109 struct rdt_resource *r = of->kn->parent->priv;
1110 struct mon_evt *mevt;
1112 list_for_each_entry(mevt, &r->evt_list, list) {
1113 seq_printf(seq, "%s\n", mevt->name);
1114 if (mevt->configurable)
1115 seq_printf(seq, "%s_config\n", mevt->name);
1121 static int rdt_bw_gran_show(struct kernfs_open_file *of,
1122 struct seq_file *seq, void *v)
1124 struct resctrl_schema *s = of->kn->parent->priv;
1125 struct rdt_resource *r = s->res;
1127 seq_printf(seq, "%u\n", r->membw.bw_gran);
1131 static int rdt_delay_linear_show(struct kernfs_open_file *of,
1132 struct seq_file *seq, void *v)
1134 struct resctrl_schema *s = of->kn->parent->priv;
1135 struct rdt_resource *r = s->res;
1137 seq_printf(seq, "%u\n", r->membw.delay_linear);
1141 static int max_threshold_occ_show(struct kernfs_open_file *of,
1142 struct seq_file *seq, void *v)
1144 seq_printf(seq, "%u\n", resctrl_rmid_realloc_threshold);
1149 static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
1150 struct seq_file *seq, void *v)
1152 struct resctrl_schema *s = of->kn->parent->priv;
1153 struct rdt_resource *r = s->res;
1155 if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
1156 seq_puts(seq, "per-thread\n");
1158 seq_puts(seq, "max\n");
1163 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1164 char *buf, size_t nbytes, loff_t off)
1169 ret = kstrtouint(buf, 0, &bytes);
1173 if (bytes > resctrl_rmid_realloc_limit)
1176 resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(bytes);
1182 * rdtgroup_mode_show - Display mode of this resource group
1184 static int rdtgroup_mode_show(struct kernfs_open_file *of,
1185 struct seq_file *s, void *v)
1187 struct rdtgroup *rdtgrp;
1189 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1191 rdtgroup_kn_unlock(of->kn);
1195 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1197 rdtgroup_kn_unlock(of->kn);
1201 static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
1214 static int rdt_has_sparse_bitmasks_show(struct kernfs_open_file *of,
1215 struct seq_file *seq, void *v)
1217 struct resctrl_schema *s = of->kn->parent->priv;
1218 struct rdt_resource *r = s->res;
1220 seq_printf(seq, "%u\n", r->cache.arch_has_sparse_bitmasks);
1226 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
1227 * @r: Resource to which domain instance @d belongs.
1228 * @d: The domain instance for which @closid is being tested.
1229 * @cbm: Capacity bitmask being tested.
1230 * @closid: Intended closid for @cbm.
1231 * @type: CDP type of @r.
1232 * @exclusive: Only check if overlaps with exclusive resource groups
1234 * Checks if provided @cbm intended to be used for @closid on domain
1235 * @d overlaps with any other closids or other hardware usage associated
1236 * with this domain. If @exclusive is true then only overlaps with
1237 * resource groups in exclusive mode will be considered. If @exclusive
1238 * is false then overlaps with any resource group or hardware entities
1239 * will be considered.
1241 * @cbm is unsigned long, even if only 32 bits are used, to make the
1242 * bitmap functions work correctly.
1244 * Return: false if CBM does not overlap, true if it does.
1246 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1247 unsigned long cbm, int closid,
1248 enum resctrl_conf_type type, bool exclusive)
1250 enum rdtgrp_mode mode;
1251 unsigned long ctrl_b;
1254 /* Check for any overlap with regions used by hardware directly */
1256 ctrl_b = r->cache.shareable_bits;
1257 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
1261 /* Check for overlap with other resource groups */
1262 for (i = 0; i < closids_supported(); i++) {
1263 ctrl_b = resctrl_arch_get_config(r, d, i, type);
1264 mode = rdtgroup_mode_by_closid(i);
1265 if (closid_allocated(i) && i != closid &&
1266 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1267 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1269 if (mode == RDT_MODE_EXCLUSIVE)
1282 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
1283 * @s: Schema for the resource to which domain instance @d belongs.
1284 * @d: The domain instance for which @closid is being tested.
1285 * @cbm: Capacity bitmask being tested.
1286 * @closid: Intended closid for @cbm.
1287 * @exclusive: Only check if overlaps with exclusive resource groups
1289 * Resources that can be allocated using a CBM can use the CBM to control
1290 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1291 * for overlap. Overlap test is not limited to the specific resource for
1292 * which the CBM is intended though - when dealing with CDP resources that
1293 * share the underlying hardware the overlap check should be performed on
1294 * the CDP resource sharing the hardware also.
1296 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1299 * Return: true if CBM overlap detected, false if there is no overlap
1301 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
1302 unsigned long cbm, int closid, bool exclusive)
1304 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
1305 struct rdt_resource *r = s->res;
1307 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
1311 if (!resctrl_arch_get_cdp_enabled(r->rid))
1313 return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
1317 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1318 * @rdtgrp: Resource group identified through its closid.
1320 * An exclusive resource group implies that there should be no sharing of
1321 * its allocated resources. At the time this group is considered to be
1322 * exclusive this test can determine if its current schemata supports this
1323 * setting by testing for overlap with all other resource groups.
1325 * Return: true if resource group can be exclusive, false if there is overlap
1326 * with allocations of other resource groups and thus this resource group
1327 * cannot be exclusive.
1329 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1331 int closid = rdtgrp->closid;
1332 struct resctrl_schema *s;
1333 struct rdt_resource *r;
1334 bool has_cache = false;
1335 struct rdt_domain *d;
1338 /* Walking r->domains, ensure it can't race with cpuhp */
1339 lockdep_assert_cpus_held();
1341 list_for_each_entry(s, &resctrl_schema_all, list) {
1343 if (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)
1346 list_for_each_entry(d, &r->domains, list) {
1347 ctrl = resctrl_arch_get_config(r, d, closid,
1349 if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
1350 rdt_last_cmd_puts("Schemata overlaps\n");
1357 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
1365 * rdtgroup_mode_write - Modify the resource group's mode
1367 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1368 char *buf, size_t nbytes, loff_t off)
1370 struct rdtgroup *rdtgrp;
1371 enum rdtgrp_mode mode;
1374 /* Valid input requires a trailing newline */
1375 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1377 buf[nbytes - 1] = '\0';
1379 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1381 rdtgroup_kn_unlock(of->kn);
1385 rdt_last_cmd_clear();
1387 mode = rdtgrp->mode;
1389 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1390 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1391 (!strcmp(buf, "pseudo-locksetup") &&
1392 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1393 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1396 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1397 rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
1402 if (!strcmp(buf, "shareable")) {
1403 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1404 ret = rdtgroup_locksetup_exit(rdtgrp);
1408 rdtgrp->mode = RDT_MODE_SHAREABLE;
1409 } else if (!strcmp(buf, "exclusive")) {
1410 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1414 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1415 ret = rdtgroup_locksetup_exit(rdtgrp);
1419 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1420 } else if (!strcmp(buf, "pseudo-locksetup")) {
1421 ret = rdtgroup_locksetup_enter(rdtgrp);
1424 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1426 rdt_last_cmd_puts("Unknown or unsupported mode\n");
1431 rdtgroup_kn_unlock(of->kn);
1432 return ret ?: nbytes;
1436 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1437 * @r: RDT resource to which @d belongs.
1438 * @d: RDT domain instance.
1439 * @cbm: bitmask for which the size should be computed.
1441 * The bitmask provided associated with the RDT domain instance @d will be
1442 * translated into how many bytes it represents. The size in bytes is
1443 * computed by first dividing the total cache size by the CBM length to
1444 * determine how many bytes each bit in the bitmask represents. The result
1445 * is multiplied with the number of bits set in the bitmask.
1447 * @cbm is unsigned long, even if only 32 bits are used to make the
1448 * bitmap functions work correctly.
1450 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1451 struct rdt_domain *d, unsigned long cbm)
1453 struct cpu_cacheinfo *ci;
1454 unsigned int size = 0;
1457 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1458 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1459 for (i = 0; i < ci->num_leaves; i++) {
1460 if (ci->info_list[i].level == r->cache_level) {
1461 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1470 * rdtgroup_size_show - Display size in bytes of allocated regions
1472 * The "size" file mirrors the layout of the "schemata" file, printing the
1473 * size in bytes of each region instead of the capacity bitmask.
1475 static int rdtgroup_size_show(struct kernfs_open_file *of,
1476 struct seq_file *s, void *v)
1478 struct resctrl_schema *schema;
1479 enum resctrl_conf_type type;
1480 struct rdtgroup *rdtgrp;
1481 struct rdt_resource *r;
1482 struct rdt_domain *d;
1489 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1491 rdtgroup_kn_unlock(of->kn);
1495 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1496 if (!rdtgrp->plr->d) {
1497 rdt_last_cmd_clear();
1498 rdt_last_cmd_puts("Cache domain offline\n");
1501 seq_printf(s, "%*s:", max_name_width,
1502 rdtgrp->plr->s->name);
1503 size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
1506 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1511 closid = rdtgrp->closid;
1513 list_for_each_entry(schema, &resctrl_schema_all, list) {
1515 type = schema->conf_type;
1517 seq_printf(s, "%*s:", max_name_width, schema->name);
1518 list_for_each_entry(d, &r->domains, list) {
1521 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1525 ctrl = d->mbps_val[closid];
1527 ctrl = resctrl_arch_get_config(r, d,
1530 if (r->rid == RDT_RESOURCE_MBA ||
1531 r->rid == RDT_RESOURCE_SMBA)
1534 size = rdtgroup_cbm_to_size(r, d, ctrl);
1536 seq_printf(s, "%d=%u", d->id, size);
1543 rdtgroup_kn_unlock(of->kn);
1548 struct mon_config_info {
1553 #define INVALID_CONFIG_INDEX UINT_MAX
1556 * mon_event_config_index_get - get the hardware index for the
1557 * configurable event
1560 * Return: 0 for evtid == QOS_L3_MBM_TOTAL_EVENT_ID
1561 * 1 for evtid == QOS_L3_MBM_LOCAL_EVENT_ID
1562 * INVALID_CONFIG_INDEX for invalid evtid
1564 static inline unsigned int mon_event_config_index_get(u32 evtid)
1567 case QOS_L3_MBM_TOTAL_EVENT_ID:
1569 case QOS_L3_MBM_LOCAL_EVENT_ID:
1572 /* Should never reach here */
1573 return INVALID_CONFIG_INDEX;
1577 static void mon_event_config_read(void *info)
1579 struct mon_config_info *mon_info = info;
1583 index = mon_event_config_index_get(mon_info->evtid);
1584 if (index == INVALID_CONFIG_INDEX) {
1585 pr_warn_once("Invalid event id %d\n", mon_info->evtid);
1588 rdmsrl(MSR_IA32_EVT_CFG_BASE + index, msrval);
1590 /* Report only the valid event configuration bits */
1591 mon_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
1594 static void mondata_config_read(struct rdt_domain *d, struct mon_config_info *mon_info)
1596 smp_call_function_any(&d->cpu_mask, mon_event_config_read, mon_info, 1);
1599 static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid)
1601 struct mon_config_info mon_info = {0};
1602 struct rdt_domain *dom;
1606 mutex_lock(&rdtgroup_mutex);
1608 list_for_each_entry(dom, &r->domains, list) {
1612 memset(&mon_info, 0, sizeof(struct mon_config_info));
1613 mon_info.evtid = evtid;
1614 mondata_config_read(dom, &mon_info);
1616 seq_printf(s, "%d=0x%02x", dom->id, mon_info.mon_config);
1621 mutex_unlock(&rdtgroup_mutex);
1627 static int mbm_total_bytes_config_show(struct kernfs_open_file *of,
1628 struct seq_file *seq, void *v)
1630 struct rdt_resource *r = of->kn->parent->priv;
1632 mbm_config_show(seq, r, QOS_L3_MBM_TOTAL_EVENT_ID);
1637 static int mbm_local_bytes_config_show(struct kernfs_open_file *of,
1638 struct seq_file *seq, void *v)
1640 struct rdt_resource *r = of->kn->parent->priv;
1642 mbm_config_show(seq, r, QOS_L3_MBM_LOCAL_EVENT_ID);
1647 static void mon_event_config_write(void *info)
1649 struct mon_config_info *mon_info = info;
1652 index = mon_event_config_index_get(mon_info->evtid);
1653 if (index == INVALID_CONFIG_INDEX) {
1654 pr_warn_once("Invalid event id %d\n", mon_info->evtid);
1657 wrmsr(MSR_IA32_EVT_CFG_BASE + index, mon_info->mon_config, 0);
1660 static void mbm_config_write_domain(struct rdt_resource *r,
1661 struct rdt_domain *d, u32 evtid, u32 val)
1663 struct mon_config_info mon_info = {0};
1666 * Read the current config value first. If both are the same then
1667 * no need to write it again.
1669 mon_info.evtid = evtid;
1670 mondata_config_read(d, &mon_info);
1671 if (mon_info.mon_config == val)
1674 mon_info.mon_config = val;
1677 * Update MSR_IA32_EVT_CFG_BASE MSR on one of the CPUs in the
1678 * domain. The MSRs offset from MSR MSR_IA32_EVT_CFG_BASE
1679 * are scoped at the domain level. Writing any of these MSRs
1680 * on one CPU is observed by all the CPUs in the domain.
1682 smp_call_function_any(&d->cpu_mask, mon_event_config_write,
1686 * When an Event Configuration is changed, the bandwidth counters
1687 * for all RMIDs and Events will be cleared by the hardware. The
1688 * hardware also sets MSR_IA32_QM_CTR.Unavailable (bit 62) for
1689 * every RMID on the next read to any event for every RMID.
1690 * Subsequent reads will have MSR_IA32_QM_CTR.Unavailable (bit 62)
1691 * cleared while it is tracked by the hardware. Clear the
1692 * mbm_local and mbm_total counts for all the RMIDs.
1694 resctrl_arch_reset_rmid_all(r, d);
1697 static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)
1699 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
1700 char *dom_str = NULL, *id_str;
1701 unsigned long dom_id, val;
1702 struct rdt_domain *d;
1704 /* Walking r->domains, ensure it can't race with cpuhp */
1705 lockdep_assert_cpus_held();
1708 if (!tok || tok[0] == '\0')
1711 /* Start processing the strings for each domain */
1712 dom_str = strim(strsep(&tok, ";"));
1713 id_str = strsep(&dom_str, "=");
1715 if (!id_str || kstrtoul(id_str, 10, &dom_id)) {
1716 rdt_last_cmd_puts("Missing '=' or non-numeric domain id\n");
1720 if (!dom_str || kstrtoul(dom_str, 16, &val)) {
1721 rdt_last_cmd_puts("Non-numeric event configuration value\n");
1725 /* Value from user cannot be more than the supported set of events */
1726 if ((val & hw_res->mbm_cfg_mask) != val) {
1727 rdt_last_cmd_printf("Invalid event configuration: max valid mask is 0x%02x\n",
1728 hw_res->mbm_cfg_mask);
1732 list_for_each_entry(d, &r->domains, list) {
1733 if (d->id == dom_id) {
1734 mbm_config_write_domain(r, d, evtid, val);
1742 static ssize_t mbm_total_bytes_config_write(struct kernfs_open_file *of,
1743 char *buf, size_t nbytes,
1746 struct rdt_resource *r = of->kn->parent->priv;
1749 /* Valid input requires a trailing newline */
1750 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1754 mutex_lock(&rdtgroup_mutex);
1756 rdt_last_cmd_clear();
1758 buf[nbytes - 1] = '\0';
1760 ret = mon_config_write(r, buf, QOS_L3_MBM_TOTAL_EVENT_ID);
1762 mutex_unlock(&rdtgroup_mutex);
1765 return ret ?: nbytes;
1768 static ssize_t mbm_local_bytes_config_write(struct kernfs_open_file *of,
1769 char *buf, size_t nbytes,
1772 struct rdt_resource *r = of->kn->parent->priv;
1775 /* Valid input requires a trailing newline */
1776 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1780 mutex_lock(&rdtgroup_mutex);
1782 rdt_last_cmd_clear();
1784 buf[nbytes - 1] = '\0';
1786 ret = mon_config_write(r, buf, QOS_L3_MBM_LOCAL_EVENT_ID);
1788 mutex_unlock(&rdtgroup_mutex);
1791 return ret ?: nbytes;
1794 /* rdtgroup information files for one cache resource. */
1795 static struct rftype res_common_files[] = {
1797 .name = "last_cmd_status",
1799 .kf_ops = &rdtgroup_kf_single_ops,
1800 .seq_show = rdt_last_cmd_status_show,
1801 .fflags = RFTYPE_TOP_INFO,
1804 .name = "num_closids",
1806 .kf_ops = &rdtgroup_kf_single_ops,
1807 .seq_show = rdt_num_closids_show,
1808 .fflags = RFTYPE_CTRL_INFO,
1811 .name = "mon_features",
1813 .kf_ops = &rdtgroup_kf_single_ops,
1814 .seq_show = rdt_mon_features_show,
1815 .fflags = RFTYPE_MON_INFO,
1818 .name = "num_rmids",
1820 .kf_ops = &rdtgroup_kf_single_ops,
1821 .seq_show = rdt_num_rmids_show,
1822 .fflags = RFTYPE_MON_INFO,
1827 .kf_ops = &rdtgroup_kf_single_ops,
1828 .seq_show = rdt_default_ctrl_show,
1829 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1832 .name = "min_cbm_bits",
1834 .kf_ops = &rdtgroup_kf_single_ops,
1835 .seq_show = rdt_min_cbm_bits_show,
1836 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1839 .name = "shareable_bits",
1841 .kf_ops = &rdtgroup_kf_single_ops,
1842 .seq_show = rdt_shareable_bits_show,
1843 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1846 .name = "bit_usage",
1848 .kf_ops = &rdtgroup_kf_single_ops,
1849 .seq_show = rdt_bit_usage_show,
1850 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1853 .name = "min_bandwidth",
1855 .kf_ops = &rdtgroup_kf_single_ops,
1856 .seq_show = rdt_min_bw_show,
1857 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
1860 .name = "bandwidth_gran",
1862 .kf_ops = &rdtgroup_kf_single_ops,
1863 .seq_show = rdt_bw_gran_show,
1864 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
1867 .name = "delay_linear",
1869 .kf_ops = &rdtgroup_kf_single_ops,
1870 .seq_show = rdt_delay_linear_show,
1871 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB,
1874 * Platform specific which (if any) capabilities are provided by
1875 * thread_throttle_mode. Defer "fflags" initialization to platform
1879 .name = "thread_throttle_mode",
1881 .kf_ops = &rdtgroup_kf_single_ops,
1882 .seq_show = rdt_thread_throttle_mode_show,
1885 .name = "max_threshold_occupancy",
1887 .kf_ops = &rdtgroup_kf_single_ops,
1888 .write = max_threshold_occ_write,
1889 .seq_show = max_threshold_occ_show,
1890 .fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE,
1893 .name = "mbm_total_bytes_config",
1895 .kf_ops = &rdtgroup_kf_single_ops,
1896 .seq_show = mbm_total_bytes_config_show,
1897 .write = mbm_total_bytes_config_write,
1900 .name = "mbm_local_bytes_config",
1902 .kf_ops = &rdtgroup_kf_single_ops,
1903 .seq_show = mbm_local_bytes_config_show,
1904 .write = mbm_local_bytes_config_write,
1909 .kf_ops = &rdtgroup_kf_single_ops,
1910 .write = rdtgroup_cpus_write,
1911 .seq_show = rdtgroup_cpus_show,
1912 .fflags = RFTYPE_BASE,
1915 .name = "cpus_list",
1917 .kf_ops = &rdtgroup_kf_single_ops,
1918 .write = rdtgroup_cpus_write,
1919 .seq_show = rdtgroup_cpus_show,
1920 .flags = RFTYPE_FLAGS_CPUS_LIST,
1921 .fflags = RFTYPE_BASE,
1926 .kf_ops = &rdtgroup_kf_single_ops,
1927 .write = rdtgroup_tasks_write,
1928 .seq_show = rdtgroup_tasks_show,
1929 .fflags = RFTYPE_BASE,
1932 .name = "mon_hw_id",
1934 .kf_ops = &rdtgroup_kf_single_ops,
1935 .seq_show = rdtgroup_rmid_show,
1936 .fflags = RFTYPE_MON_BASE | RFTYPE_DEBUG,
1941 .kf_ops = &rdtgroup_kf_single_ops,
1942 .write = rdtgroup_schemata_write,
1943 .seq_show = rdtgroup_schemata_show,
1944 .fflags = RFTYPE_CTRL_BASE,
1949 .kf_ops = &rdtgroup_kf_single_ops,
1950 .write = rdtgroup_mode_write,
1951 .seq_show = rdtgroup_mode_show,
1952 .fflags = RFTYPE_CTRL_BASE,
1957 .kf_ops = &rdtgroup_kf_single_ops,
1958 .seq_show = rdtgroup_size_show,
1959 .fflags = RFTYPE_CTRL_BASE,
1962 .name = "sparse_masks",
1964 .kf_ops = &rdtgroup_kf_single_ops,
1965 .seq_show = rdt_has_sparse_bitmasks_show,
1966 .fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_CACHE,
1969 .name = "ctrl_hw_id",
1971 .kf_ops = &rdtgroup_kf_single_ops,
1972 .seq_show = rdtgroup_closid_show,
1973 .fflags = RFTYPE_CTRL_BASE | RFTYPE_DEBUG,
1978 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1980 struct rftype *rfts, *rft;
1983 rfts = res_common_files;
1984 len = ARRAY_SIZE(res_common_files);
1986 lockdep_assert_held(&rdtgroup_mutex);
1989 fflags |= RFTYPE_DEBUG;
1991 for (rft = rfts; rft < rfts + len; rft++) {
1992 if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
1993 ret = rdtgroup_add_file(kn, rft);
2001 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
2002 while (--rft >= rfts) {
2003 if ((fflags & rft->fflags) == rft->fflags)
2004 kernfs_remove_by_name(kn, rft->name);
2009 static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
2011 struct rftype *rfts, *rft;
2014 rfts = res_common_files;
2015 len = ARRAY_SIZE(res_common_files);
2017 for (rft = rfts; rft < rfts + len; rft++) {
2018 if (!strcmp(rft->name, name))
2025 void __init thread_throttle_mode_init(void)
2029 rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
2033 rft->fflags = RFTYPE_CTRL_INFO | RFTYPE_RES_MB;
2036 void __init mbm_config_rftype_init(const char *config)
2040 rft = rdtgroup_get_rftype_by_name(config);
2042 rft->fflags = RFTYPE_MON_INFO | RFTYPE_RES_CACHE;
2046 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
2047 * @r: The resource group with which the file is associated.
2048 * @name: Name of the file
2050 * The permissions of named resctrl file, directory, or link are modified
2051 * to not allow read, write, or execute by any user.
2053 * WARNING: This function is intended to communicate to the user that the
2054 * resctrl file has been locked down - that it is not relevant to the
2055 * particular state the system finds itself in. It should not be relied
2056 * on to protect from user access because after the file's permissions
2057 * are restricted the user can still change the permissions using chmod
2058 * from the command line.
2060 * Return: 0 on success, <0 on failure.
2062 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
2064 struct iattr iattr = {.ia_valid = ATTR_MODE,};
2065 struct kernfs_node *kn;
2068 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
2072 switch (kernfs_type(kn)) {
2074 iattr.ia_mode = S_IFDIR;
2077 iattr.ia_mode = S_IFREG;
2080 iattr.ia_mode = S_IFLNK;
2084 ret = kernfs_setattr(kn, &iattr);
2090 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
2091 * @r: The resource group with which the file is associated.
2092 * @name: Name of the file
2093 * @mask: Mask of permissions that should be restored
2095 * Restore the permissions of the named file. If @name is a directory the
2096 * permissions of its parent will be used.
2098 * Return: 0 on success, <0 on failure.
2100 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
2103 struct iattr iattr = {.ia_valid = ATTR_MODE,};
2104 struct kernfs_node *kn, *parent;
2105 struct rftype *rfts, *rft;
2108 rfts = res_common_files;
2109 len = ARRAY_SIZE(res_common_files);
2111 for (rft = rfts; rft < rfts + len; rft++) {
2112 if (!strcmp(rft->name, name))
2113 iattr.ia_mode = rft->mode & mask;
2116 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
2120 switch (kernfs_type(kn)) {
2122 parent = kernfs_get_parent(kn);
2124 iattr.ia_mode |= parent->mode;
2127 iattr.ia_mode |= S_IFDIR;
2130 iattr.ia_mode |= S_IFREG;
2133 iattr.ia_mode |= S_IFLNK;
2137 ret = kernfs_setattr(kn, &iattr);
2142 static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
2143 unsigned long fflags)
2145 struct kernfs_node *kn_subdir;
2148 kn_subdir = kernfs_create_dir(kn_info, name,
2149 kn_info->mode, priv);
2150 if (IS_ERR(kn_subdir))
2151 return PTR_ERR(kn_subdir);
2153 ret = rdtgroup_kn_set_ugid(kn_subdir);
2157 ret = rdtgroup_add_files(kn_subdir, fflags);
2159 kernfs_activate(kn_subdir);
2164 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
2166 struct resctrl_schema *s;
2167 struct rdt_resource *r;
2168 unsigned long fflags;
2172 /* create the directory */
2173 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
2174 if (IS_ERR(kn_info))
2175 return PTR_ERR(kn_info);
2177 ret = rdtgroup_add_files(kn_info, RFTYPE_TOP_INFO);
2181 /* loop over enabled controls, these are all alloc_capable */
2182 list_for_each_entry(s, &resctrl_schema_all, list) {
2184 fflags = r->fflags | RFTYPE_CTRL_INFO;
2185 ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
2190 for_each_mon_capable_rdt_resource(r) {
2191 fflags = r->fflags | RFTYPE_MON_INFO;
2192 sprintf(name, "%s_MON", r->name);
2193 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
2198 ret = rdtgroup_kn_set_ugid(kn_info);
2202 kernfs_activate(kn_info);
2207 kernfs_remove(kn_info);
2212 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
2213 char *name, struct kernfs_node **dest_kn)
2215 struct kernfs_node *kn;
2218 /* create the directory */
2219 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2226 ret = rdtgroup_kn_set_ugid(kn);
2230 kernfs_activate(kn);
2239 static void l3_qos_cfg_update(void *arg)
2243 wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
2246 static void l2_qos_cfg_update(void *arg)
2250 wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
2253 static inline bool is_mba_linear(void)
2255 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
2258 static int set_cache_qos_cfg(int level, bool enable)
2260 void (*update)(void *arg);
2261 struct rdt_resource *r_l;
2262 cpumask_var_t cpu_mask;
2263 struct rdt_domain *d;
2266 /* Walking r->domains, ensure it can't race with cpuhp */
2267 lockdep_assert_cpus_held();
2269 if (level == RDT_RESOURCE_L3)
2270 update = l3_qos_cfg_update;
2271 else if (level == RDT_RESOURCE_L2)
2272 update = l2_qos_cfg_update;
2276 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2279 r_l = &rdt_resources_all[level].r_resctrl;
2280 list_for_each_entry(d, &r_l->domains, list) {
2281 if (r_l->cache.arch_has_per_cpu_cfg)
2282 /* Pick all the CPUs in the domain instance */
2283 for_each_cpu(cpu, &d->cpu_mask)
2284 cpumask_set_cpu(cpu, cpu_mask);
2286 /* Pick one CPU from each domain instance to update MSR */
2287 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2290 /* Update QOS_CFG MSR on all the CPUs in cpu_mask */
2291 on_each_cpu_mask(cpu_mask, update, &enable, 1);
2293 free_cpumask_var(cpu_mask);
2298 /* Restore the qos cfg state when a domain comes online */
2299 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
2301 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2303 if (!r->cdp_capable)
2306 if (r->rid == RDT_RESOURCE_L2)
2307 l2_qos_cfg_update(&hw_res->cdp_enabled);
2309 if (r->rid == RDT_RESOURCE_L3)
2310 l3_qos_cfg_update(&hw_res->cdp_enabled);
2313 static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_domain *d)
2315 u32 num_closid = resctrl_arch_get_num_closid(r);
2316 int cpu = cpumask_any(&d->cpu_mask);
2319 d->mbps_val = kcalloc_node(num_closid, sizeof(*d->mbps_val),
2320 GFP_KERNEL, cpu_to_node(cpu));
2324 for (i = 0; i < num_closid; i++)
2325 d->mbps_val[i] = MBA_MAX_MBPS;
2330 static void mba_sc_domain_destroy(struct rdt_resource *r,
2331 struct rdt_domain *d)
2338 * MBA software controller is supported only if
2339 * MBM is supported and MBA is in linear scale.
2341 static bool supports_mba_mbps(void)
2343 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
2345 return (is_mbm_local_enabled() &&
2346 r->alloc_capable && is_mba_linear());
2350 * Enable or disable the MBA software controller
2351 * which helps user specify bandwidth in MBps.
2353 static int set_mba_sc(bool mba_sc)
2355 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
2356 u32 num_closid = resctrl_arch_get_num_closid(r);
2357 struct rdt_domain *d;
2360 if (!supports_mba_mbps() || mba_sc == is_mba_sc(r))
2363 r->membw.mba_sc = mba_sc;
2365 list_for_each_entry(d, &r->domains, list) {
2366 for (i = 0; i < num_closid; i++)
2367 d->mbps_val[i] = MBA_MAX_MBPS;
2373 static int cdp_enable(int level)
2375 struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
2378 if (!r_l->alloc_capable)
2381 ret = set_cache_qos_cfg(level, true);
2383 rdt_resources_all[level].cdp_enabled = true;
2388 static void cdp_disable(int level)
2390 struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
2392 if (r_hw->cdp_enabled) {
2393 set_cache_qos_cfg(level, false);
2394 r_hw->cdp_enabled = false;
2398 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
2400 struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
2402 if (!hw_res->r_resctrl.cdp_capable)
2406 return cdp_enable(l);
2414 * We don't allow rdtgroup directories to be created anywhere
2415 * except the root directory. Thus when looking for the rdtgroup
2416 * structure for a kernfs node we are either looking at a directory,
2417 * in which case the rdtgroup structure is pointed at by the "priv"
2418 * field, otherwise we have a file, and need only look to the parent
2419 * to find the rdtgroup.
2421 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
2423 if (kernfs_type(kn) == KERNFS_DIR) {
2425 * All the resource directories use "kn->priv"
2426 * to point to the "struct rdtgroup" for the
2427 * resource. "info" and its subdirectories don't
2428 * have rdtgroup structures, so return NULL here.
2430 if (kn == kn_info || kn->parent == kn_info)
2435 return kn->parent->priv;
2439 static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
2441 atomic_inc(&rdtgrp->waitcount);
2442 kernfs_break_active_protection(kn);
2445 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
2447 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2448 (rdtgrp->flags & RDT_DELETED)) {
2449 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2450 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2451 rdtgroup_pseudo_lock_remove(rdtgrp);
2452 kernfs_unbreak_active_protection(kn);
2453 rdtgroup_remove(rdtgrp);
2455 kernfs_unbreak_active_protection(kn);
2459 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
2461 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2466 rdtgroup_kn_get(rdtgrp, kn);
2469 mutex_lock(&rdtgroup_mutex);
2471 /* Was this group deleted while we waited? */
2472 if (rdtgrp->flags & RDT_DELETED)
2478 void rdtgroup_kn_unlock(struct kernfs_node *kn)
2480 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2485 mutex_unlock(&rdtgroup_mutex);
2488 rdtgroup_kn_put(rdtgrp, kn);
2491 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2492 struct rdtgroup *prgrp,
2493 struct kernfs_node **mon_data_kn);
2495 static void rdt_disable_ctx(void)
2497 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
2498 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
2501 resctrl_debug = false;
2504 static int rdt_enable_ctx(struct rdt_fs_context *ctx)
2508 if (ctx->enable_cdpl2) {
2509 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
2514 if (ctx->enable_cdpl3) {
2515 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
2520 if (ctx->enable_mba_mbps) {
2521 ret = set_mba_sc(true);
2526 if (ctx->enable_debug)
2527 resctrl_debug = true;
2532 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
2534 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
2539 static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
2541 struct resctrl_schema *s;
2542 const char *suffix = "";
2545 s = kzalloc(sizeof(*s), GFP_KERNEL);
2550 s->num_closid = resctrl_arch_get_num_closid(r);
2551 if (resctrl_arch_get_cdp_enabled(r->rid))
2554 s->conf_type = type;
2567 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
2568 if (ret >= sizeof(s->name)) {
2573 cl = strlen(s->name);
2576 * If CDP is supported by this resource, but not enabled,
2577 * include the suffix. This ensures the tabular format of the
2578 * schemata file does not change between mounts of the filesystem.
2580 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
2583 if (cl > max_name_width)
2584 max_name_width = cl;
2586 INIT_LIST_HEAD(&s->list);
2587 list_add(&s->list, &resctrl_schema_all);
2592 static int schemata_list_create(void)
2594 struct rdt_resource *r;
2597 for_each_alloc_capable_rdt_resource(r) {
2598 if (resctrl_arch_get_cdp_enabled(r->rid)) {
2599 ret = schemata_list_add(r, CDP_CODE);
2603 ret = schemata_list_add(r, CDP_DATA);
2605 ret = schemata_list_add(r, CDP_NONE);
2615 static void schemata_list_destroy(void)
2617 struct resctrl_schema *s, *tmp;
2619 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
2625 static int rdt_get_tree(struct fs_context *fc)
2627 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2628 unsigned long flags = RFTYPE_CTRL_BASE;
2629 struct rdt_domain *dom;
2630 struct rdt_resource *r;
2634 mutex_lock(&rdtgroup_mutex);
2636 * resctrl file system can only be mounted once.
2638 if (resctrl_mounted) {
2643 ret = rdtgroup_setup_root(ctx);
2647 ret = rdt_enable_ctx(ctx);
2651 ret = schemata_list_create();
2653 schemata_list_destroy();
2659 if (resctrl_arch_mon_capable())
2660 flags |= RFTYPE_MON;
2662 ret = rdtgroup_add_files(rdtgroup_default.kn, flags);
2664 goto out_schemata_free;
2666 kernfs_activate(rdtgroup_default.kn);
2668 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
2670 goto out_schemata_free;
2672 if (resctrl_arch_mon_capable()) {
2673 ret = mongroup_create_dir(rdtgroup_default.kn,
2674 &rdtgroup_default, "mon_groups",
2679 ret = mkdir_mondata_all(rdtgroup_default.kn,
2680 &rdtgroup_default, &kn_mondata);
2683 rdtgroup_default.mon.mon_data_kn = kn_mondata;
2686 ret = rdt_pseudo_lock_init();
2690 ret = kernfs_get_tree(fc);
2694 if (resctrl_arch_alloc_capable())
2695 resctrl_arch_enable_alloc();
2696 if (resctrl_arch_mon_capable())
2697 resctrl_arch_enable_mon();
2699 if (resctrl_arch_alloc_capable() || resctrl_arch_mon_capable())
2700 resctrl_mounted = true;
2702 if (is_mbm_enabled()) {
2703 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
2704 list_for_each_entry(dom, &r->domains, list)
2705 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL,
2706 RESCTRL_PICK_ANY_CPU);
2712 rdt_pseudo_lock_release();
2714 if (resctrl_arch_mon_capable())
2715 kernfs_remove(kn_mondata);
2717 if (resctrl_arch_mon_capable())
2718 kernfs_remove(kn_mongrp);
2720 kernfs_remove(kn_info);
2722 schemata_list_destroy();
2726 rdtgroup_destroy_root();
2728 rdt_last_cmd_clear();
2729 mutex_unlock(&rdtgroup_mutex);
2742 static const struct fs_parameter_spec rdt_fs_parameters[] = {
2743 fsparam_flag("cdp", Opt_cdp),
2744 fsparam_flag("cdpl2", Opt_cdpl2),
2745 fsparam_flag("mba_MBps", Opt_mba_mbps),
2746 fsparam_flag("debug", Opt_debug),
2750 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2752 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2753 struct fs_parse_result result;
2756 opt = fs_parse(fc, rdt_fs_parameters, param, &result);
2762 ctx->enable_cdpl3 = true;
2765 ctx->enable_cdpl2 = true;
2768 if (!supports_mba_mbps())
2770 ctx->enable_mba_mbps = true;
2773 ctx->enable_debug = true;
2780 static void rdt_fs_context_free(struct fs_context *fc)
2782 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2784 kernfs_free_fs_context(fc);
2788 static const struct fs_context_operations rdt_fs_context_ops = {
2789 .free = rdt_fs_context_free,
2790 .parse_param = rdt_parse_param,
2791 .get_tree = rdt_get_tree,
2794 static int rdt_init_fs_context(struct fs_context *fc)
2796 struct rdt_fs_context *ctx;
2798 ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2802 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2803 fc->fs_private = &ctx->kfc;
2804 fc->ops = &rdt_fs_context_ops;
2805 put_user_ns(fc->user_ns);
2806 fc->user_ns = get_user_ns(&init_user_ns);
2811 static int reset_all_ctrls(struct rdt_resource *r)
2813 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2814 struct rdt_hw_domain *hw_dom;
2815 struct msr_param msr_param;
2816 cpumask_var_t cpu_mask;
2817 struct rdt_domain *d;
2820 /* Walking r->domains, ensure it can't race with cpuhp */
2821 lockdep_assert_cpus_held();
2823 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2828 msr_param.high = hw_res->num_closid;
2831 * Disable resource control for this resource by setting all
2832 * CBMs in all domains to the maximum mask value. Pick one CPU
2833 * from each domain to update the MSRs below.
2835 list_for_each_entry(d, &r->domains, list) {
2836 hw_dom = resctrl_to_arch_dom(d);
2837 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2839 for (i = 0; i < hw_res->num_closid; i++)
2840 hw_dom->ctrl_val[i] = r->default_ctrl;
2843 /* Update CBM on all the CPUs in cpu_mask */
2844 on_each_cpu_mask(cpu_mask, rdt_ctrl_update, &msr_param, 1);
2846 free_cpumask_var(cpu_mask);
2852 * Move tasks from one to the other group. If @from is NULL, then all tasks
2853 * in the systems are moved unconditionally (used for teardown).
2855 * If @mask is not NULL the cpus on which moved tasks are running are set
2856 * in that mask so the update smp function call is restricted to affected
2859 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2860 struct cpumask *mask)
2862 struct task_struct *p, *t;
2864 read_lock(&tasklist_lock);
2865 for_each_process_thread(p, t) {
2866 if (!from || is_closid_match(t, from) ||
2867 is_rmid_match(t, from)) {
2868 resctrl_arch_set_closid_rmid(t, to->closid,
2872 * Order the closid/rmid stores above before the loads
2873 * in task_curr(). This pairs with the full barrier
2874 * between the rq->curr update and resctrl_sched_in()
2875 * during context switch.
2880 * If the task is on a CPU, set the CPU in the mask.
2881 * The detection is inaccurate as tasks might move or
2882 * schedule before the smp function call takes place.
2883 * In such a case the function call is pointless, but
2884 * there is no other side effect.
2886 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
2887 cpumask_set_cpu(task_cpu(t), mask);
2890 read_unlock(&tasklist_lock);
2893 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2895 struct rdtgroup *sentry, *stmp;
2896 struct list_head *head;
2898 head = &rdtgrp->mon.crdtgrp_list;
2899 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2900 free_rmid(sentry->closid, sentry->mon.rmid);
2901 list_del(&sentry->mon.crdtgrp_list);
2903 if (atomic_read(&sentry->waitcount) != 0)
2904 sentry->flags = RDT_DELETED;
2906 rdtgroup_remove(sentry);
2911 * Forcibly remove all of subdirectories under root.
2913 static void rmdir_all_sub(void)
2915 struct rdtgroup *rdtgrp, *tmp;
2917 /* Move all tasks to the default resource group */
2918 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2920 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2921 /* Free any child rmids */
2922 free_all_child_rdtgrp(rdtgrp);
2924 /* Remove each rdtgroup other than root */
2925 if (rdtgrp == &rdtgroup_default)
2928 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2929 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2930 rdtgroup_pseudo_lock_remove(rdtgrp);
2933 * Give any CPUs back to the default group. We cannot copy
2934 * cpu_online_mask because a CPU might have executed the
2935 * offline callback already, but is still marked online.
2937 cpumask_or(&rdtgroup_default.cpu_mask,
2938 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2940 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
2942 kernfs_remove(rdtgrp->kn);
2943 list_del(&rdtgrp->rdtgroup_list);
2945 if (atomic_read(&rdtgrp->waitcount) != 0)
2946 rdtgrp->flags = RDT_DELETED;
2948 rdtgroup_remove(rdtgrp);
2950 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2951 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2953 kernfs_remove(kn_info);
2954 kernfs_remove(kn_mongrp);
2955 kernfs_remove(kn_mondata);
2958 static void rdt_kill_sb(struct super_block *sb)
2960 struct rdt_resource *r;
2963 mutex_lock(&rdtgroup_mutex);
2967 /*Put everything back to default values. */
2968 for_each_alloc_capable_rdt_resource(r)
2971 rdt_pseudo_lock_release();
2972 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2973 schemata_list_destroy();
2974 rdtgroup_destroy_root();
2975 if (resctrl_arch_alloc_capable())
2976 resctrl_arch_disable_alloc();
2977 if (resctrl_arch_mon_capable())
2978 resctrl_arch_disable_mon();
2979 resctrl_mounted = false;
2981 mutex_unlock(&rdtgroup_mutex);
2985 static struct file_system_type rdt_fs_type = {
2987 .init_fs_context = rdt_init_fs_context,
2988 .parameters = rdt_fs_parameters,
2989 .kill_sb = rdt_kill_sb,
2992 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2995 struct kernfs_node *kn;
2998 kn = __kernfs_create_file(parent_kn, name, 0444,
2999 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
3000 &kf_mondata_ops, priv, NULL, NULL);
3004 ret = rdtgroup_kn_set_ugid(kn);
3014 * Remove all subdirectories of mon_data of ctrl_mon groups
3015 * and monitor groups with given domain id.
3017 static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
3018 unsigned int dom_id)
3020 struct rdtgroup *prgrp, *crgrp;
3023 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
3024 sprintf(name, "mon_%s_%02d", r->name, dom_id);
3025 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
3027 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
3028 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
3032 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
3033 struct rdt_domain *d,
3034 struct rdt_resource *r, struct rdtgroup *prgrp)
3036 union mon_data_bits priv;
3037 struct kernfs_node *kn;
3038 struct mon_evt *mevt;
3039 struct rmid_read rr;
3043 sprintf(name, "mon_%s_%02d", r->name, d->id);
3044 /* create the directory */
3045 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
3049 ret = rdtgroup_kn_set_ugid(kn);
3053 if (WARN_ON(list_empty(&r->evt_list))) {
3058 priv.u.rid = r->rid;
3059 priv.u.domid = d->id;
3060 list_for_each_entry(mevt, &r->evt_list, list) {
3061 priv.u.evtid = mevt->evtid;
3062 ret = mon_addfile(kn, mevt->name, priv.priv);
3066 if (is_mbm_event(mevt->evtid))
3067 mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
3069 kernfs_activate(kn);
3078 * Add all subdirectories of mon_data for "ctrl_mon" groups
3079 * and "monitor" groups with given domain id.
3081 static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
3082 struct rdt_domain *d)
3084 struct kernfs_node *parent_kn;
3085 struct rdtgroup *prgrp, *crgrp;
3086 struct list_head *head;
3088 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
3089 parent_kn = prgrp->mon.mon_data_kn;
3090 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
3092 head = &prgrp->mon.crdtgrp_list;
3093 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
3094 parent_kn = crgrp->mon.mon_data_kn;
3095 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
3100 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
3101 struct rdt_resource *r,
3102 struct rdtgroup *prgrp)
3104 struct rdt_domain *dom;
3107 /* Walking r->domains, ensure it can't race with cpuhp */
3108 lockdep_assert_cpus_held();
3110 list_for_each_entry(dom, &r->domains, list) {
3111 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
3120 * This creates a directory mon_data which contains the monitored data.
3122 * mon_data has one directory for each domain which are named
3123 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
3124 * with L3 domain looks as below:
3131 * Each domain directory has one file per event:
3136 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
3137 struct rdtgroup *prgrp,
3138 struct kernfs_node **dest_kn)
3140 struct rdt_resource *r;
3141 struct kernfs_node *kn;
3145 * Create the mon_data directory first.
3147 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
3155 * Create the subdirectories for each domain. Note that all events
3156 * in a domain like L3 are grouped into a resource whose domain is L3
3158 for_each_mon_capable_rdt_resource(r) {
3159 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
3172 * cbm_ensure_valid - Enforce validity on provided CBM
3173 * @_val: Candidate CBM
3174 * @r: RDT resource to which the CBM belongs
3176 * The provided CBM represents all cache portions available for use. This
3177 * may be represented by a bitmap that does not consist of contiguous ones
3178 * and thus be an invalid CBM.
3179 * Here the provided CBM is forced to be a valid CBM by only considering
3180 * the first set of contiguous bits as valid and clearing all bits.
3181 * The intention here is to provide a valid default CBM with which a new
3182 * resource group is initialized. The user can follow this with a
3183 * modification to the CBM if the default does not satisfy the
3186 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
3188 unsigned int cbm_len = r->cache.cbm_len;
3189 unsigned long first_bit, zero_bit;
3190 unsigned long val = _val;
3195 first_bit = find_first_bit(&val, cbm_len);
3196 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
3198 /* Clear any remaining bits to ensure contiguous region */
3199 bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
3204 * Initialize cache resources per RDT domain
3206 * Set the RDT domain up to start off with all usable allocations. That is,
3207 * all shareable and unused bits. All-zero CBM is invalid.
3209 static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
3212 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
3213 enum resctrl_conf_type t = s->conf_type;
3214 struct resctrl_staged_config *cfg;
3215 struct rdt_resource *r = s->res;
3216 u32 used_b = 0, unused_b = 0;
3217 unsigned long tmp_cbm;
3218 enum rdtgrp_mode mode;
3219 u32 peer_ctl, ctrl_val;
3222 cfg = &d->staged_config[t];
3223 cfg->have_new_ctrl = false;
3224 cfg->new_ctrl = r->cache.shareable_bits;
3225 used_b = r->cache.shareable_bits;
3226 for (i = 0; i < closids_supported(); i++) {
3227 if (closid_allocated(i) && i != closid) {
3228 mode = rdtgroup_mode_by_closid(i);
3229 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
3231 * ctrl values for locksetup aren't relevant
3232 * until the schemata is written, and the mode
3233 * becomes RDT_MODE_PSEUDO_LOCKED.
3237 * If CDP is active include peer domain's
3238 * usage to ensure there is no overlap
3239 * with an exclusive group.
3241 if (resctrl_arch_get_cdp_enabled(r->rid))
3242 peer_ctl = resctrl_arch_get_config(r, d, i,
3246 ctrl_val = resctrl_arch_get_config(r, d, i,
3248 used_b |= ctrl_val | peer_ctl;
3249 if (mode == RDT_MODE_SHAREABLE)
3250 cfg->new_ctrl |= ctrl_val | peer_ctl;
3253 if (d->plr && d->plr->cbm > 0)
3254 used_b |= d->plr->cbm;
3255 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
3256 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
3257 cfg->new_ctrl |= unused_b;
3259 * Force the initial CBM to be valid, user can
3260 * modify the CBM based on system availability.
3262 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
3264 * Assign the u32 CBM to an unsigned long to ensure that
3265 * bitmap_weight() does not access out-of-bound memory.
3267 tmp_cbm = cfg->new_ctrl;
3268 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
3269 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id);
3272 cfg->have_new_ctrl = true;
3278 * Initialize cache resources with default values.
3280 * A new RDT group is being created on an allocation capable (CAT)
3281 * supporting system. Set this group up to start off with all usable
3284 * If there are no more shareable bits available on any domain then
3285 * the entire allocation will fail.
3287 static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
3289 struct rdt_domain *d;
3292 list_for_each_entry(d, &s->res->domains, list) {
3293 ret = __init_one_rdt_domain(d, s, closid);
3301 /* Initialize MBA resource with default values. */
3302 static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
3304 struct resctrl_staged_config *cfg;
3305 struct rdt_domain *d;
3307 list_for_each_entry(d, &r->domains, list) {
3309 d->mbps_val[closid] = MBA_MAX_MBPS;
3313 cfg = &d->staged_config[CDP_NONE];
3314 cfg->new_ctrl = r->default_ctrl;
3315 cfg->have_new_ctrl = true;
3319 /* Initialize the RDT group's allocations. */
3320 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
3322 struct resctrl_schema *s;
3323 struct rdt_resource *r;
3326 rdt_staged_configs_clear();
3328 list_for_each_entry(s, &resctrl_schema_all, list) {
3330 if (r->rid == RDT_RESOURCE_MBA ||
3331 r->rid == RDT_RESOURCE_SMBA) {
3332 rdtgroup_init_mba(r, rdtgrp->closid);
3336 ret = rdtgroup_init_cat(s, rdtgrp->closid);
3341 ret = resctrl_arch_update_domains(r, rdtgrp->closid);
3343 rdt_last_cmd_puts("Failed to initialize allocations\n");
3349 rdtgrp->mode = RDT_MODE_SHAREABLE;
3352 rdt_staged_configs_clear();
3356 static int mkdir_rdt_prepare_rmid_alloc(struct rdtgroup *rdtgrp)
3360 if (!resctrl_arch_mon_capable())
3363 ret = alloc_rmid(rdtgrp->closid);
3365 rdt_last_cmd_puts("Out of RMIDs\n");
3368 rdtgrp->mon.rmid = ret;
3370 ret = mkdir_mondata_all(rdtgrp->kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
3372 rdt_last_cmd_puts("kernfs subdir error\n");
3373 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
3380 static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)
3382 if (resctrl_arch_mon_capable())
3383 free_rmid(rgrp->closid, rgrp->mon.rmid);
3386 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
3387 const char *name, umode_t mode,
3388 enum rdt_group_type rtype, struct rdtgroup **r)
3390 struct rdtgroup *prdtgrp, *rdtgrp;
3391 unsigned long files = 0;
3392 struct kernfs_node *kn;
3395 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
3401 if (rtype == RDTMON_GROUP &&
3402 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3403 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
3405 rdt_last_cmd_puts("Pseudo-locking in progress\n");
3409 /* allocate the rdtgroup. */
3410 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
3413 rdt_last_cmd_puts("Kernel out of memory\n");
3417 rdtgrp->mon.parent = prdtgrp;
3418 rdtgrp->type = rtype;
3419 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
3421 /* kernfs creates the directory for rdtgrp */
3422 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
3425 rdt_last_cmd_puts("kernfs create error\n");
3431 * kernfs_remove() will drop the reference count on "kn" which
3432 * will free it. But we still need it to stick around for the
3433 * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
3434 * which will be dropped by kernfs_put() in rdtgroup_remove().
3438 ret = rdtgroup_kn_set_ugid(kn);
3440 rdt_last_cmd_puts("kernfs perm error\n");
3444 if (rtype == RDTCTRL_GROUP) {
3445 files = RFTYPE_BASE | RFTYPE_CTRL;
3446 if (resctrl_arch_mon_capable())
3447 files |= RFTYPE_MON;
3449 files = RFTYPE_BASE | RFTYPE_MON;
3452 ret = rdtgroup_add_files(kn, files);
3454 rdt_last_cmd_puts("kernfs fill error\n");
3459 * The caller unlocks the parent_kn upon success.
3464 kernfs_put(rdtgrp->kn);
3465 kernfs_remove(rdtgrp->kn);
3469 rdtgroup_kn_unlock(parent_kn);
3473 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
3475 kernfs_remove(rgrp->kn);
3476 rdtgroup_remove(rgrp);
3480 * Create a monitor group under "mon_groups" directory of a control
3481 * and monitor group(ctrl_mon). This is a resource group
3482 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
3484 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
3485 const char *name, umode_t mode)
3487 struct rdtgroup *rdtgrp, *prgrp;
3490 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
3494 prgrp = rdtgrp->mon.parent;
3495 rdtgrp->closid = prgrp->closid;
3497 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp);
3499 mkdir_rdt_prepare_clean(rdtgrp);
3503 kernfs_activate(rdtgrp->kn);
3506 * Add the rdtgrp to the list of rdtgrps the parent
3507 * ctrl_mon group has to track.
3509 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
3512 rdtgroup_kn_unlock(parent_kn);
3517 * These are rdtgroups created under the root directory. Can be used
3518 * to allocate and monitor resources.
3520 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
3521 const char *name, umode_t mode)
3523 struct rdtgroup *rdtgrp;
3524 struct kernfs_node *kn;
3528 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
3533 ret = closid_alloc();
3535 rdt_last_cmd_puts("Out of CLOSIDs\n");
3536 goto out_common_fail;
3541 rdtgrp->closid = closid;
3543 ret = mkdir_rdt_prepare_rmid_alloc(rdtgrp);
3545 goto out_closid_free;
3547 kernfs_activate(rdtgrp->kn);
3549 ret = rdtgroup_init_alloc(rdtgrp);
3553 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
3555 if (resctrl_arch_mon_capable()) {
3557 * Create an empty mon_groups directory to hold the subset
3558 * of tasks and cpus to monitor.
3560 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
3562 rdt_last_cmd_puts("kernfs subdir error\n");
3570 list_del(&rdtgrp->rdtgroup_list);
3572 mkdir_rdt_prepare_rmid_free(rdtgrp);
3574 closid_free(closid);
3576 mkdir_rdt_prepare_clean(rdtgrp);
3578 rdtgroup_kn_unlock(parent_kn);
3583 * We allow creating mon groups only with in a directory called "mon_groups"
3584 * which is present in every ctrl_mon group. Check if this is a valid
3585 * "mon_groups" directory.
3587 * 1. The directory should be named "mon_groups".
3588 * 2. The mon group itself should "not" be named "mon_groups".
3589 * This makes sure "mon_groups" directory always has a ctrl_mon group
3592 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
3594 return (!strcmp(kn->name, "mon_groups") &&
3595 strcmp(name, "mon_groups"));
3598 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3601 /* Do not accept '\n' to avoid unparsable situation. */
3602 if (strchr(name, '\n'))
3606 * If the parent directory is the root directory and RDT
3607 * allocation is supported, add a control and monitoring
3610 if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn)
3611 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
3614 * If RDT monitoring is supported and the parent directory is a valid
3615 * "mon_groups" directory, add a monitoring subdirectory.
3617 if (resctrl_arch_mon_capable() && is_mon_groups(parent_kn, name))
3618 return rdtgroup_mkdir_mon(parent_kn, name, mode);
3623 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3625 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3628 /* Give any tasks back to the parent group */
3629 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
3631 /* Update per cpu rmid of the moved CPUs first */
3632 for_each_cpu(cpu, &rdtgrp->cpu_mask)
3633 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
3635 * Update the MSR on moved CPUs and CPUs which have moved
3636 * task running on them.
3638 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3639 update_closid_rmid(tmpmask, NULL);
3641 rdtgrp->flags = RDT_DELETED;
3642 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
3645 * Remove the rdtgrp from the parent ctrl_mon group's list
3647 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3648 list_del(&rdtgrp->mon.crdtgrp_list);
3650 kernfs_remove(rdtgrp->kn);
3655 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
3657 rdtgrp->flags = RDT_DELETED;
3658 list_del(&rdtgrp->rdtgroup_list);
3660 kernfs_remove(rdtgrp->kn);
3664 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3668 /* Give any tasks back to the default group */
3669 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
3671 /* Give any CPUs back to the default group */
3672 cpumask_or(&rdtgroup_default.cpu_mask,
3673 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
3675 /* Update per cpu closid and rmid of the moved CPUs first */
3676 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
3677 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3678 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
3682 * Update the MSR on moved CPUs and CPUs which have moved
3683 * task running on them.
3685 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3686 update_closid_rmid(tmpmask, NULL);
3688 free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
3689 closid_free(rdtgrp->closid);
3691 rdtgroup_ctrl_remove(rdtgrp);
3694 * Free all the child monitor group rmids.
3696 free_all_child_rdtgrp(rdtgrp);
3701 static int rdtgroup_rmdir(struct kernfs_node *kn)
3703 struct kernfs_node *parent_kn = kn->parent;
3704 struct rdtgroup *rdtgrp;
3705 cpumask_var_t tmpmask;
3708 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3711 rdtgrp = rdtgroup_kn_lock_live(kn);
3718 * If the rdtgroup is a ctrl_mon group and parent directory
3719 * is the root directory, remove the ctrl_mon group.
3721 * If the rdtgroup is a mon group and parent directory
3722 * is a valid "mon_groups" directory, remove the mon group.
3724 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3725 rdtgrp != &rdtgroup_default) {
3726 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3727 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
3728 ret = rdtgroup_ctrl_remove(rdtgrp);
3730 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
3732 } else if (rdtgrp->type == RDTMON_GROUP &&
3733 is_mon_groups(parent_kn, kn->name)) {
3734 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
3740 rdtgroup_kn_unlock(kn);
3741 free_cpumask_var(tmpmask);
3746 * mongrp_reparent() - replace parent CTRL_MON group of a MON group
3747 * @rdtgrp: the MON group whose parent should be replaced
3748 * @new_prdtgrp: replacement parent CTRL_MON group for @rdtgrp
3749 * @cpus: cpumask provided by the caller for use during this call
3751 * Replaces the parent CTRL_MON group for a MON group, resulting in all member
3752 * tasks' CLOSID immediately changing to that of the new parent group.
3753 * Monitoring data for the group is unaffected by this operation.
3755 static void mongrp_reparent(struct rdtgroup *rdtgrp,
3756 struct rdtgroup *new_prdtgrp,
3759 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3761 WARN_ON(rdtgrp->type != RDTMON_GROUP);
3762 WARN_ON(new_prdtgrp->type != RDTCTRL_GROUP);
3764 /* Nothing to do when simply renaming a MON group. */
3765 if (prdtgrp == new_prdtgrp)
3768 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3769 list_move_tail(&rdtgrp->mon.crdtgrp_list,
3770 &new_prdtgrp->mon.crdtgrp_list);
3772 rdtgrp->mon.parent = new_prdtgrp;
3773 rdtgrp->closid = new_prdtgrp->closid;
3775 /* Propagate updated closid to all tasks in this group. */
3776 rdt_move_group_tasks(rdtgrp, rdtgrp, cpus);
3778 update_closid_rmid(cpus, NULL);
3781 static int rdtgroup_rename(struct kernfs_node *kn,
3782 struct kernfs_node *new_parent, const char *new_name)
3784 struct rdtgroup *new_prdtgrp;
3785 struct rdtgroup *rdtgrp;
3786 cpumask_var_t tmpmask;
3789 rdtgrp = kernfs_to_rdtgroup(kn);
3790 new_prdtgrp = kernfs_to_rdtgroup(new_parent);
3791 if (!rdtgrp || !new_prdtgrp)
3794 /* Release both kernfs active_refs before obtaining rdtgroup mutex. */
3795 rdtgroup_kn_get(rdtgrp, kn);
3796 rdtgroup_kn_get(new_prdtgrp, new_parent);
3798 mutex_lock(&rdtgroup_mutex);
3800 rdt_last_cmd_clear();
3803 * Don't allow kernfs_to_rdtgroup() to return a parent rdtgroup if
3804 * either kernfs_node is a file.
3806 if (kernfs_type(kn) != KERNFS_DIR ||
3807 kernfs_type(new_parent) != KERNFS_DIR) {
3808 rdt_last_cmd_puts("Source and destination must be directories");
3813 if ((rdtgrp->flags & RDT_DELETED) || (new_prdtgrp->flags & RDT_DELETED)) {
3818 if (rdtgrp->type != RDTMON_GROUP || !kn->parent ||
3819 !is_mon_groups(kn->parent, kn->name)) {
3820 rdt_last_cmd_puts("Source must be a MON group\n");
3825 if (!is_mon_groups(new_parent, new_name)) {
3826 rdt_last_cmd_puts("Destination must be a mon_groups subdirectory\n");
3832 * If the MON group is monitoring CPUs, the CPUs must be assigned to the
3833 * current parent CTRL_MON group and therefore cannot be assigned to
3834 * the new parent, making the move illegal.
3836 if (!cpumask_empty(&rdtgrp->cpu_mask) &&
3837 rdtgrp->mon.parent != new_prdtgrp) {
3838 rdt_last_cmd_puts("Cannot move a MON group that monitors CPUs\n");
3844 * Allocate the cpumask for use in mongrp_reparent() to avoid the
3845 * possibility of failing to allocate it after kernfs_rename() has
3848 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) {
3854 * Perform all input validation and allocations needed to ensure
3855 * mongrp_reparent() will succeed before calling kernfs_rename(),
3856 * otherwise it would be necessary to revert this call if
3857 * mongrp_reparent() failed.
3859 ret = kernfs_rename(kn, new_parent, new_name);
3861 mongrp_reparent(rdtgrp, new_prdtgrp, tmpmask);
3863 free_cpumask_var(tmpmask);
3866 mutex_unlock(&rdtgroup_mutex);
3867 rdtgroup_kn_put(rdtgrp, kn);
3868 rdtgroup_kn_put(new_prdtgrp, new_parent);
3872 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3874 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
3875 seq_puts(seq, ",cdp");
3877 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
3878 seq_puts(seq, ",cdpl2");
3880 if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
3881 seq_puts(seq, ",mba_MBps");
3884 seq_puts(seq, ",debug");
3889 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
3890 .mkdir = rdtgroup_mkdir,
3891 .rmdir = rdtgroup_rmdir,
3892 .rename = rdtgroup_rename,
3893 .show_options = rdtgroup_show_options,
3896 static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
3898 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
3899 KERNFS_ROOT_CREATE_DEACTIVATED |
3900 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
3902 if (IS_ERR(rdt_root))
3903 return PTR_ERR(rdt_root);
3905 ctx->kfc.root = rdt_root;
3906 rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
3911 static void rdtgroup_destroy_root(void)
3913 kernfs_destroy_root(rdt_root);
3914 rdtgroup_default.kn = NULL;
3917 static void __init rdtgroup_setup_default(void)
3919 mutex_lock(&rdtgroup_mutex);
3921 rdtgroup_default.closid = RESCTRL_RESERVED_CLOSID;
3922 rdtgroup_default.mon.rmid = RESCTRL_RESERVED_RMID;
3923 rdtgroup_default.type = RDTCTRL_GROUP;
3924 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3926 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3928 mutex_unlock(&rdtgroup_mutex);
3931 static void domain_destroy_mon_state(struct rdt_domain *d)
3933 bitmap_free(d->rmid_busy_llc);
3934 kfree(d->mbm_total);
3935 kfree(d->mbm_local);
3938 void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
3940 mutex_lock(&rdtgroup_mutex);
3942 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3943 mba_sc_domain_destroy(r, d);
3945 if (!r->mon_capable)
3949 * If resctrl is mounted, remove all the
3950 * per domain monitor data directories.
3952 if (resctrl_mounted && resctrl_arch_mon_capable())
3953 rmdir_mondata_subdir_allrdtgrp(r, d->id);
3955 if (is_mbm_enabled())
3956 cancel_delayed_work(&d->mbm_over);
3957 if (is_llc_occupancy_enabled() && has_busy_rmid(d)) {
3959 * When a package is going down, forcefully
3960 * decrement rmid->ebusy. There is no way to know
3961 * that the L3 was flushed and hence may lead to
3962 * incorrect counts in rare scenarios, but leaving
3963 * the RMID as busy creates RMID leaks if the
3964 * package never comes back.
3966 __check_limbo(d, true);
3967 cancel_delayed_work(&d->cqm_limbo);
3970 domain_destroy_mon_state(d);
3973 mutex_unlock(&rdtgroup_mutex);
3976 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
3978 u32 idx_limit = resctrl_arch_system_num_rmid_idx();
3981 if (is_llc_occupancy_enabled()) {
3982 d->rmid_busy_llc = bitmap_zalloc(idx_limit, GFP_KERNEL);
3983 if (!d->rmid_busy_llc)
3986 if (is_mbm_total_enabled()) {
3987 tsize = sizeof(*d->mbm_total);
3988 d->mbm_total = kcalloc(idx_limit, tsize, GFP_KERNEL);
3989 if (!d->mbm_total) {
3990 bitmap_free(d->rmid_busy_llc);
3994 if (is_mbm_local_enabled()) {
3995 tsize = sizeof(*d->mbm_local);
3996 d->mbm_local = kcalloc(idx_limit, tsize, GFP_KERNEL);
3997 if (!d->mbm_local) {
3998 bitmap_free(d->rmid_busy_llc);
3999 kfree(d->mbm_total);
4007 int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
4011 mutex_lock(&rdtgroup_mutex);
4013 if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA) {
4014 /* RDT_RESOURCE_MBA is never mon_capable */
4015 err = mba_sc_domain_allocate(r, d);
4019 if (!r->mon_capable)
4022 err = domain_setup_mon_state(r, d);
4026 if (is_mbm_enabled()) {
4027 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
4028 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL,
4029 RESCTRL_PICK_ANY_CPU);
4032 if (is_llc_occupancy_enabled())
4033 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
4036 * If the filesystem is not mounted then only the default resource group
4037 * exists. Creation of its directories is deferred until mount time
4038 * by rdt_get_tree() calling mkdir_mondata_all().
4039 * If resctrl is mounted, add per domain monitor data directories.
4041 if (resctrl_mounted && resctrl_arch_mon_capable())
4042 mkdir_mondata_subdir_allrdtgrp(r, d);
4045 mutex_unlock(&rdtgroup_mutex);
4050 void resctrl_online_cpu(unsigned int cpu)
4052 mutex_lock(&rdtgroup_mutex);
4053 /* The CPU is set in default rdtgroup after online. */
4054 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
4055 mutex_unlock(&rdtgroup_mutex);
4058 static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
4060 struct rdtgroup *cr;
4062 list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
4063 if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask))
4068 void resctrl_offline_cpu(unsigned int cpu)
4070 struct rdt_resource *l3 = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
4071 struct rdtgroup *rdtgrp;
4072 struct rdt_domain *d;
4074 mutex_lock(&rdtgroup_mutex);
4075 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
4076 if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
4077 clear_childcpus(rdtgrp, cpu);
4082 if (!l3->mon_capable)
4085 d = get_domain_from_cpu(cpu, l3);
4087 if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
4088 cancel_delayed_work(&d->mbm_over);
4089 mbm_setup_overflow_handler(d, 0, cpu);
4091 if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
4093 cancel_delayed_work(&d->cqm_limbo);
4094 cqm_setup_limbo_handler(d, 0, cpu);
4099 mutex_unlock(&rdtgroup_mutex);
4103 * rdtgroup_init - rdtgroup initialization
4105 * Setup resctrl file system including set up root, create mount point,
4106 * register rdtgroup filesystem, and initialize files under root directory.
4108 * Return: 0 on success or -errno
4110 int __init rdtgroup_init(void)
4114 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
4115 sizeof(last_cmd_status_buf));
4117 rdtgroup_setup_default();
4119 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
4123 ret = register_filesystem(&rdt_fs_type);
4125 goto cleanup_mountpoint;
4128 * Adding the resctrl debugfs directory here may not be ideal since
4129 * it would let the resctrl debugfs directory appear on the debugfs
4130 * filesystem before the resctrl filesystem is mounted.
4131 * It may also be ok since that would enable debugging of RDT before
4132 * resctrl is mounted.
4133 * The reason why the debugfs directory is created here and not in
4134 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
4135 * during the debugfs directory creation also &sb->s_type->i_mutex_key
4136 * (the lockdep class of inode->i_rwsem). Other filesystem
4137 * interactions (eg. SyS_getdents) have the lock ordering:
4138 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
4139 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
4140 * is taken, thus creating dependency:
4141 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
4142 * issues considering the other two lock dependencies.
4143 * By creating the debugfs directory here we avoid a dependency
4144 * that may cause deadlock (even though file operations cannot
4145 * occur until the filesystem is mounted, but I do not know how to
4146 * tell lockdep that).
4148 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
4153 sysfs_remove_mount_point(fs_kobj, "resctrl");
4158 void __exit rdtgroup_exit(void)
4160 debugfs_remove_recursive(debugfs_resctrl);
4161 unregister_filesystem(&rdt_fs_type);
4162 sysfs_remove_mount_point(fs_kobj, "resctrl");