2 * User interface for Resource Alloction in Resource Director Technology(RDT)
4 * Copyright (C) 2016 Intel Corporation
6 * Author: Fenghua Yu <fenghua.yu@intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * More information about RDT be found in the Intel (R) x86 Architecture
18 * Software Developer Manual.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/cacheinfo.h>
24 #include <linux/cpu.h>
25 #include <linux/debugfs.h>
27 #include <linux/sysfs.h>
28 #include <linux/kernfs.h>
29 #include <linux/seq_buf.h>
30 #include <linux/seq_file.h>
31 #include <linux/sched/signal.h>
32 #include <linux/sched/task.h>
33 #include <linux/slab.h>
34 #include <linux/task_work.h>
36 #include <uapi/linux/magic.h>
38 #include <asm/intel_rdt_sched.h>
39 #include "intel_rdt.h"
41 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
42 DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
43 DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
44 static struct kernfs_root *rdt_root;
45 struct rdtgroup rdtgroup_default;
46 LIST_HEAD(rdt_all_groups);
48 /* Kernel fs node for "info" directory under root */
49 static struct kernfs_node *kn_info;
51 /* Kernel fs node for "mon_groups" directory under root */
52 static struct kernfs_node *kn_mongrp;
54 /* Kernel fs node for "mon_data" directory under root */
55 static struct kernfs_node *kn_mondata;
57 static struct seq_buf last_cmd_status;
58 static char last_cmd_status_buf[512];
60 struct dentry *debugfs_resctrl;
62 void rdt_last_cmd_clear(void)
64 lockdep_assert_held(&rdtgroup_mutex);
65 seq_buf_clear(&last_cmd_status);
68 void rdt_last_cmd_puts(const char *s)
70 lockdep_assert_held(&rdtgroup_mutex);
71 seq_buf_puts(&last_cmd_status, s);
74 void rdt_last_cmd_printf(const char *fmt, ...)
79 lockdep_assert_held(&rdtgroup_mutex);
80 seq_buf_vprintf(&last_cmd_status, fmt, ap);
85 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
86 * we can keep a bitmap of free CLOSIDs in a single integer.
88 * Using a global CLOSID across all resources has some advantages and
90 * + We can simply set "current->closid" to assign a task to a resource
92 * + Context switch code can avoid extra memory references deciding which
93 * CLOSID to load into the PQR_ASSOC MSR
94 * - We give up some options in configuring resource groups across multi-socket
96 * - Our choices on how to configure each resource become progressively more
97 * limited as the number of resources grows.
99 static int closid_free_map;
100 static int closid_free_map_len;
102 int closids_supported(void)
104 return closid_free_map_len;
107 static void closid_init(void)
109 struct rdt_resource *r;
110 int rdt_min_closid = 32;
112 /* Compute rdt_min_closid across all resources */
113 for_each_alloc_enabled_rdt_resource(r)
114 rdt_min_closid = min(rdt_min_closid, r->num_closid);
116 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
118 /* CLOSID 0 is always reserved for the default group */
119 closid_free_map &= ~1;
120 closid_free_map_len = rdt_min_closid;
123 static int closid_alloc(void)
125 u32 closid = ffs(closid_free_map);
130 closid_free_map &= ~(1 << closid);
135 void closid_free(int closid)
137 closid_free_map |= 1 << closid;
141 * closid_allocated - test if provided closid is in use
142 * @closid: closid to be tested
144 * Return: true if @closid is currently associated with a resource group,
145 * false if @closid is free
147 static bool closid_allocated(unsigned int closid)
149 return (closid_free_map & (1 << closid)) == 0;
153 * rdtgroup_mode_by_closid - Return mode of resource group with closid
154 * @closid: closid if the resource group
156 * Each resource group is associated with a @closid. Here the mode
157 * of a resource group can be queried by searching for it using its closid.
159 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
161 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
163 struct rdtgroup *rdtgrp;
165 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
166 if (rdtgrp->closid == closid)
170 return RDT_NUM_MODES;
173 static const char * const rdt_mode_str[] = {
174 [RDT_MODE_SHAREABLE] = "shareable",
175 [RDT_MODE_EXCLUSIVE] = "exclusive",
176 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
177 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
181 * rdtgroup_mode_str - Return the string representation of mode
182 * @mode: the resource group mode as &enum rdtgroup_mode
184 * Return: string representation of valid mode, "unknown" otherwise
186 static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
188 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
191 return rdt_mode_str[mode];
194 /* set uid and gid of rdtgroup dirs and files to that of the creator */
195 static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
197 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
198 .ia_uid = current_fsuid(),
199 .ia_gid = current_fsgid(), };
201 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
202 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
205 return kernfs_setattr(kn, &iattr);
208 static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
210 struct kernfs_node *kn;
213 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
214 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
215 0, rft->kf_ops, rft, NULL, NULL);
219 ret = rdtgroup_kn_set_ugid(kn);
228 static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
230 struct kernfs_open_file *of = m->private;
231 struct rftype *rft = of->kn->priv;
234 return rft->seq_show(of, m, arg);
238 static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
239 size_t nbytes, loff_t off)
241 struct rftype *rft = of->kn->priv;
244 return rft->write(of, buf, nbytes, off);
249 static struct kernfs_ops rdtgroup_kf_single_ops = {
250 .atomic_write_len = PAGE_SIZE,
251 .write = rdtgroup_file_write,
252 .seq_show = rdtgroup_seqfile_show,
255 static struct kernfs_ops kf_mondata_ops = {
256 .atomic_write_len = PAGE_SIZE,
257 .seq_show = rdtgroup_mondata_show,
260 static bool is_cpu_list(struct kernfs_open_file *of)
262 struct rftype *rft = of->kn->priv;
264 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
267 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
268 struct seq_file *s, void *v)
270 struct rdtgroup *rdtgrp;
273 rdtgrp = rdtgroup_kn_lock_live(of->kn);
276 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
277 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
278 cpumask_pr_args(&rdtgrp->plr->d->cpu_mask));
280 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
281 cpumask_pr_args(&rdtgrp->cpu_mask));
285 rdtgroup_kn_unlock(of->kn);
291 * This is safe against intel_rdt_sched_in() called from __switch_to()
292 * because __switch_to() is executed with interrupts disabled. A local call
293 * from update_closid_rmid() is proteced against __switch_to() because
294 * preemption is disabled.
296 static void update_cpu_closid_rmid(void *info)
298 struct rdtgroup *r = info;
301 this_cpu_write(pqr_state.default_closid, r->closid);
302 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
306 * We cannot unconditionally write the MSR because the current
307 * executing task might have its own closid selected. Just reuse
308 * the context switch code.
310 intel_rdt_sched_in();
314 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
316 * Per task closids/rmids must have been set up before calling this function.
319 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
323 if (cpumask_test_cpu(cpu, cpu_mask))
324 update_cpu_closid_rmid(r);
325 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
329 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
330 cpumask_var_t tmpmask)
332 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
333 struct list_head *head;
335 /* Check whether cpus belong to parent ctrl group */
336 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
337 if (cpumask_weight(tmpmask)) {
338 rdt_last_cmd_puts("can only add CPUs to mongroup that belong to parent\n");
342 /* Check whether cpus are dropped from this group */
343 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
344 if (cpumask_weight(tmpmask)) {
345 /* Give any dropped cpus to parent rdtgroup */
346 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
347 update_closid_rmid(tmpmask, prgrp);
351 * If we added cpus, remove them from previous group that owned them
352 * and update per-cpu rmid
354 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
355 if (cpumask_weight(tmpmask)) {
356 head = &prgrp->mon.crdtgrp_list;
357 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
360 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
363 update_closid_rmid(tmpmask, rdtgrp);
366 /* Done pushing/pulling - update this group with new mask */
367 cpumask_copy(&rdtgrp->cpu_mask, newmask);
372 static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
374 struct rdtgroup *crgrp;
376 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
377 /* update the child mon group masks as well*/
378 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
379 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
382 static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
383 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
385 struct rdtgroup *r, *crgrp;
386 struct list_head *head;
388 /* Check whether cpus are dropped from this group */
389 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
390 if (cpumask_weight(tmpmask)) {
391 /* Can't drop from default group */
392 if (rdtgrp == &rdtgroup_default) {
393 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
397 /* Give any dropped cpus to rdtgroup_default */
398 cpumask_or(&rdtgroup_default.cpu_mask,
399 &rdtgroup_default.cpu_mask, tmpmask);
400 update_closid_rmid(tmpmask, &rdtgroup_default);
404 * If we added cpus, remove them from previous group and
405 * the prev group's child groups that owned them
406 * and update per-cpu closid/rmid.
408 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
409 if (cpumask_weight(tmpmask)) {
410 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
413 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
414 if (cpumask_weight(tmpmask1))
415 cpumask_rdtgrp_clear(r, tmpmask1);
417 update_closid_rmid(tmpmask, rdtgrp);
420 /* Done pushing/pulling - update this group with new mask */
421 cpumask_copy(&rdtgrp->cpu_mask, newmask);
424 * Clear child mon group masks since there is a new parent mask
425 * now and update the rmid for the cpus the child lost.
427 head = &rdtgrp->mon.crdtgrp_list;
428 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
429 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
430 update_closid_rmid(tmpmask, rdtgrp);
431 cpumask_clear(&crgrp->cpu_mask);
437 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
438 char *buf, size_t nbytes, loff_t off)
440 cpumask_var_t tmpmask, newmask, tmpmask1;
441 struct rdtgroup *rdtgrp;
447 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
449 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
450 free_cpumask_var(tmpmask);
453 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
454 free_cpumask_var(tmpmask);
455 free_cpumask_var(newmask);
459 rdtgrp = rdtgroup_kn_lock_live(of->kn);
460 rdt_last_cmd_clear();
463 rdt_last_cmd_puts("directory was removed\n");
467 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
468 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
470 rdt_last_cmd_puts("pseudo-locking in progress\n");
475 ret = cpulist_parse(buf, newmask);
477 ret = cpumask_parse(buf, newmask);
480 rdt_last_cmd_puts("bad cpu list/mask\n");
484 /* check that user didn't specify any offline cpus */
485 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
486 if (cpumask_weight(tmpmask)) {
488 rdt_last_cmd_puts("can only assign online cpus\n");
492 if (rdtgrp->type == RDTCTRL_GROUP)
493 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
494 else if (rdtgrp->type == RDTMON_GROUP)
495 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
500 rdtgroup_kn_unlock(of->kn);
501 free_cpumask_var(tmpmask);
502 free_cpumask_var(newmask);
503 free_cpumask_var(tmpmask1);
505 return ret ?: nbytes;
508 struct task_move_callback {
509 struct callback_head work;
510 struct rdtgroup *rdtgrp;
513 static void move_myself(struct callback_head *head)
515 struct task_move_callback *callback;
516 struct rdtgroup *rdtgrp;
518 callback = container_of(head, struct task_move_callback, work);
519 rdtgrp = callback->rdtgrp;
522 * If resource group was deleted before this task work callback
523 * was invoked, then assign the task to root group and free the
526 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
527 (rdtgrp->flags & RDT_DELETED)) {
534 /* update PQR_ASSOC MSR to make resource group go into effect */
535 intel_rdt_sched_in();
541 static int __rdtgroup_move_task(struct task_struct *tsk,
542 struct rdtgroup *rdtgrp)
544 struct task_move_callback *callback;
547 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
550 callback->work.func = move_myself;
551 callback->rdtgrp = rdtgrp;
554 * Take a refcount, so rdtgrp cannot be freed before the
555 * callback has been invoked.
557 atomic_inc(&rdtgrp->waitcount);
558 ret = task_work_add(tsk, &callback->work, true);
561 * Task is exiting. Drop the refcount and free the callback.
562 * No need to check the refcount as the group cannot be
563 * deleted before the write function unlocks rdtgroup_mutex.
565 atomic_dec(&rdtgrp->waitcount);
567 rdt_last_cmd_puts("task exited\n");
570 * For ctrl_mon groups move both closid and rmid.
571 * For monitor groups, can move the tasks only from
572 * their parent CTRL group.
574 if (rdtgrp->type == RDTCTRL_GROUP) {
575 tsk->closid = rdtgrp->closid;
576 tsk->rmid = rdtgrp->mon.rmid;
577 } else if (rdtgrp->type == RDTMON_GROUP) {
578 if (rdtgrp->mon.parent->closid == tsk->closid) {
579 tsk->rmid = rdtgrp->mon.rmid;
581 rdt_last_cmd_puts("Can't move task to different control group\n");
590 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
593 * Return: 1 if tasks have been assigned to @r, 0 otherwise
595 int rdtgroup_tasks_assigned(struct rdtgroup *r)
597 struct task_struct *p, *t;
600 lockdep_assert_held(&rdtgroup_mutex);
603 for_each_process_thread(p, t) {
604 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
605 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) {
615 static int rdtgroup_task_write_permission(struct task_struct *task,
616 struct kernfs_open_file *of)
618 const struct cred *tcred = get_task_cred(task);
619 const struct cred *cred = current_cred();
623 * Even if we're attaching all tasks in the thread group, we only
624 * need to check permissions on one of them.
626 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
627 !uid_eq(cred->euid, tcred->uid) &&
628 !uid_eq(cred->euid, tcred->suid)) {
629 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
637 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
638 struct kernfs_open_file *of)
640 struct task_struct *tsk;
645 tsk = find_task_by_vpid(pid);
648 rdt_last_cmd_printf("No task %d\n", pid);
655 get_task_struct(tsk);
658 ret = rdtgroup_task_write_permission(tsk, of);
660 ret = __rdtgroup_move_task(tsk, rdtgrp);
662 put_task_struct(tsk);
666 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
667 char *buf, size_t nbytes, loff_t off)
669 struct rdtgroup *rdtgrp;
673 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
675 rdtgrp = rdtgroup_kn_lock_live(of->kn);
677 rdtgroup_kn_unlock(of->kn);
680 rdt_last_cmd_clear();
682 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
683 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
685 rdt_last_cmd_puts("pseudo-locking in progress\n");
689 ret = rdtgroup_move_task(pid, rdtgrp, of);
692 rdtgroup_kn_unlock(of->kn);
694 return ret ?: nbytes;
697 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
699 struct task_struct *p, *t;
702 for_each_process_thread(p, t) {
703 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
704 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid))
705 seq_printf(s, "%d\n", t->pid);
710 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
711 struct seq_file *s, void *v)
713 struct rdtgroup *rdtgrp;
716 rdtgrp = rdtgroup_kn_lock_live(of->kn);
718 show_rdt_tasks(rdtgrp, s);
721 rdtgroup_kn_unlock(of->kn);
726 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
727 struct seq_file *seq, void *v)
731 mutex_lock(&rdtgroup_mutex);
732 len = seq_buf_used(&last_cmd_status);
734 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
736 seq_puts(seq, "ok\n");
737 mutex_unlock(&rdtgroup_mutex);
741 static int rdt_num_closids_show(struct kernfs_open_file *of,
742 struct seq_file *seq, void *v)
744 struct rdt_resource *r = of->kn->parent->priv;
746 seq_printf(seq, "%d\n", r->num_closid);
750 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
751 struct seq_file *seq, void *v)
753 struct rdt_resource *r = of->kn->parent->priv;
755 seq_printf(seq, "%x\n", r->default_ctrl);
759 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
760 struct seq_file *seq, void *v)
762 struct rdt_resource *r = of->kn->parent->priv;
764 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
768 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
769 struct seq_file *seq, void *v)
771 struct rdt_resource *r = of->kn->parent->priv;
773 seq_printf(seq, "%x\n", r->cache.shareable_bits);
778 * rdt_bit_usage_show - Display current usage of resources
780 * A domain is a shared resource that can now be allocated differently. Here
781 * we display the current regions of the domain as an annotated bitmask.
782 * For each domain of this resource its allocation bitmask
783 * is annotated as below to indicate the current usage of the corresponding bit:
784 * 0 - currently unused
785 * X - currently available for sharing and used by software and hardware
786 * H - currently used by hardware only but available for software use
787 * S - currently used and shareable by software only
788 * E - currently used exclusively by one resource group
789 * P - currently pseudo-locked by one resource group
791 static int rdt_bit_usage_show(struct kernfs_open_file *of,
792 struct seq_file *seq, void *v)
794 struct rdt_resource *r = of->kn->parent->priv;
795 u32 sw_shareable = 0, hw_shareable = 0;
796 u32 exclusive = 0, pseudo_locked = 0;
797 struct rdt_domain *dom;
798 int i, hwb, swb, excl, psl;
799 enum rdtgrp_mode mode;
803 mutex_lock(&rdtgroup_mutex);
804 hw_shareable = r->cache.shareable_bits;
805 list_for_each_entry(dom, &r->domains, list) {
808 ctrl = dom->ctrl_val;
811 seq_printf(seq, "%d=", dom->id);
812 for (i = 0; i < closids_supported(); i++, ctrl++) {
813 if (!closid_allocated(i))
815 mode = rdtgroup_mode_by_closid(i);
817 case RDT_MODE_SHAREABLE:
818 sw_shareable |= *ctrl;
820 case RDT_MODE_EXCLUSIVE:
823 case RDT_MODE_PSEUDO_LOCKSETUP:
825 * RDT_MODE_PSEUDO_LOCKSETUP is possible
826 * here but not included since the CBM
827 * associated with this CLOSID in this mode
828 * is not initialized and no task or cpu can be
829 * assigned this CLOSID.
832 case RDT_MODE_PSEUDO_LOCKED:
835 "invalid mode for closid %d\n", i);
839 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
840 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
841 hwb = test_bit(i, (unsigned long *)&hw_shareable);
842 swb = test_bit(i, (unsigned long *)&sw_shareable);
843 excl = test_bit(i, (unsigned long *)&exclusive);
844 psl = test_bit(i, (unsigned long *)&pseudo_locked);
847 else if (hwb && !swb)
849 else if (!hwb && swb)
855 else /* Unused bits remain */
861 mutex_unlock(&rdtgroup_mutex);
865 static int rdt_min_bw_show(struct kernfs_open_file *of,
866 struct seq_file *seq, void *v)
868 struct rdt_resource *r = of->kn->parent->priv;
870 seq_printf(seq, "%u\n", r->membw.min_bw);
874 static int rdt_num_rmids_show(struct kernfs_open_file *of,
875 struct seq_file *seq, void *v)
877 struct rdt_resource *r = of->kn->parent->priv;
879 seq_printf(seq, "%d\n", r->num_rmid);
884 static int rdt_mon_features_show(struct kernfs_open_file *of,
885 struct seq_file *seq, void *v)
887 struct rdt_resource *r = of->kn->parent->priv;
888 struct mon_evt *mevt;
890 list_for_each_entry(mevt, &r->evt_list, list)
891 seq_printf(seq, "%s\n", mevt->name);
896 static int rdt_bw_gran_show(struct kernfs_open_file *of,
897 struct seq_file *seq, void *v)
899 struct rdt_resource *r = of->kn->parent->priv;
901 seq_printf(seq, "%u\n", r->membw.bw_gran);
905 static int rdt_delay_linear_show(struct kernfs_open_file *of,
906 struct seq_file *seq, void *v)
908 struct rdt_resource *r = of->kn->parent->priv;
910 seq_printf(seq, "%u\n", r->membw.delay_linear);
914 static int max_threshold_occ_show(struct kernfs_open_file *of,
915 struct seq_file *seq, void *v)
917 struct rdt_resource *r = of->kn->parent->priv;
919 seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale);
924 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
925 char *buf, size_t nbytes, loff_t off)
927 struct rdt_resource *r = of->kn->parent->priv;
931 ret = kstrtouint(buf, 0, &bytes);
935 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
938 intel_cqm_threshold = bytes / r->mon_scale;
944 * rdtgroup_mode_show - Display mode of this resource group
946 static int rdtgroup_mode_show(struct kernfs_open_file *of,
947 struct seq_file *s, void *v)
949 struct rdtgroup *rdtgrp;
951 rdtgrp = rdtgroup_kn_lock_live(of->kn);
953 rdtgroup_kn_unlock(of->kn);
957 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
959 rdtgroup_kn_unlock(of->kn);
964 * rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
965 * @r: Resource to which domain instance @d belongs.
966 * @d: The domain instance for which @closid is being tested.
967 * @cbm: Capacity bitmask being tested.
968 * @closid: Intended closid for @cbm.
969 * @exclusive: Only check if overlaps with exclusive resource groups
971 * Checks if provided @cbm intended to be used for @closid on domain
972 * @d overlaps with any other closids or other hardware usage associated
973 * with this domain. If @exclusive is true then only overlaps with
974 * resource groups in exclusive mode will be considered. If @exclusive
975 * is false then overlaps with any resource group or hardware entities
976 * will be considered.
978 * @cbm is unsigned long, even if only 32 bits are used, to make the
979 * bitmap functions work correctly.
981 * Return: false if CBM does not overlap, true if it does.
983 bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
984 unsigned long cbm, int closid, bool exclusive)
986 enum rdtgrp_mode mode;
987 unsigned long ctrl_b;
991 /* Check for any overlap with regions used by hardware directly */
993 ctrl_b = r->cache.shareable_bits;
994 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
998 /* Check for overlap with other resource groups */
1000 for (i = 0; i < closids_supported(); i++, ctrl++) {
1002 mode = rdtgroup_mode_by_closid(i);
1003 if (closid_allocated(i) && i != closid &&
1004 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1005 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1007 if (mode == RDT_MODE_EXCLUSIVE)
1020 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1022 * An exclusive resource group implies that there should be no sharing of
1023 * its allocated resources. At the time this group is considered to be
1024 * exclusive this test can determine if its current schemata supports this
1025 * setting by testing for overlap with all other resource groups.
1027 * Return: true if resource group can be exclusive, false if there is overlap
1028 * with allocations of other resource groups and thus this resource group
1029 * cannot be exclusive.
1031 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1033 int closid = rdtgrp->closid;
1034 struct rdt_resource *r;
1035 bool has_cache = false;
1036 struct rdt_domain *d;
1038 for_each_alloc_enabled_rdt_resource(r) {
1039 if (r->rid == RDT_RESOURCE_MBA)
1042 list_for_each_entry(d, &r->domains, list) {
1043 if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid],
1044 rdtgrp->closid, false)) {
1045 rdt_last_cmd_puts("schemata overlaps\n");
1052 rdt_last_cmd_puts("cannot be exclusive without CAT/CDP\n");
1060 * rdtgroup_mode_write - Modify the resource group's mode
1063 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1064 char *buf, size_t nbytes, loff_t off)
1066 struct rdtgroup *rdtgrp;
1067 enum rdtgrp_mode mode;
1070 /* Valid input requires a trailing newline */
1071 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1073 buf[nbytes - 1] = '\0';
1075 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1077 rdtgroup_kn_unlock(of->kn);
1081 rdt_last_cmd_clear();
1083 mode = rdtgrp->mode;
1085 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1086 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1087 (!strcmp(buf, "pseudo-locksetup") &&
1088 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1089 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1092 if (mode == RDT_MODE_PSEUDO_LOCKED) {
1093 rdt_last_cmd_printf("cannot change pseudo-locked group\n");
1098 if (!strcmp(buf, "shareable")) {
1099 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1100 ret = rdtgroup_locksetup_exit(rdtgrp);
1104 rdtgrp->mode = RDT_MODE_SHAREABLE;
1105 } else if (!strcmp(buf, "exclusive")) {
1106 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1110 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1111 ret = rdtgroup_locksetup_exit(rdtgrp);
1115 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1116 } else if (!strcmp(buf, "pseudo-locksetup")) {
1117 ret = rdtgroup_locksetup_enter(rdtgrp);
1120 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1122 rdt_last_cmd_printf("unknown/unsupported mode\n");
1127 rdtgroup_kn_unlock(of->kn);
1128 return ret ?: nbytes;
1132 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1133 * @r: RDT resource to which @d belongs.
1134 * @d: RDT domain instance.
1135 * @cbm: bitmask for which the size should be computed.
1137 * The bitmask provided associated with the RDT domain instance @d will be
1138 * translated into how many bytes it represents. The size in bytes is
1139 * computed by first dividing the total cache size by the CBM length to
1140 * determine how many bytes each bit in the bitmask represents. The result
1141 * is multiplied with the number of bits set in the bitmask.
1143 * @cbm is unsigned long, even if only 32 bits are used to make the
1144 * bitmap functions work correctly.
1146 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1147 struct rdt_domain *d, unsigned long cbm)
1149 struct cpu_cacheinfo *ci;
1150 unsigned int size = 0;
1153 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1154 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1155 for (i = 0; i < ci->num_leaves; i++) {
1156 if (ci->info_list[i].level == r->cache_level) {
1157 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1166 * rdtgroup_size_show - Display size in bytes of allocated regions
1168 * The "size" file mirrors the layout of the "schemata" file, printing the
1169 * size in bytes of each region instead of the capacity bitmask.
1172 static int rdtgroup_size_show(struct kernfs_open_file *of,
1173 struct seq_file *s, void *v)
1175 struct rdtgroup *rdtgrp;
1176 struct rdt_resource *r;
1177 struct rdt_domain *d;
1182 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1184 rdtgroup_kn_unlock(of->kn);
1188 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1189 seq_printf(s, "%*s:", max_name_width, rdtgrp->plr->r->name);
1190 size = rdtgroup_cbm_to_size(rdtgrp->plr->r,
1193 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1197 for_each_alloc_enabled_rdt_resource(r) {
1199 seq_printf(s, "%*s:", max_name_width, r->name);
1200 list_for_each_entry(d, &r->domains, list) {
1203 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1206 ctrl = (!is_mba_sc(r) ?
1207 d->ctrl_val[rdtgrp->closid] :
1208 d->mbps_val[rdtgrp->closid]);
1209 if (r->rid == RDT_RESOURCE_MBA)
1212 size = rdtgroup_cbm_to_size(r, d, ctrl);
1214 seq_printf(s, "%d=%u", d->id, size);
1221 rdtgroup_kn_unlock(of->kn);
1226 /* rdtgroup information files for one cache resource. */
1227 static struct rftype res_common_files[] = {
1229 .name = "last_cmd_status",
1231 .kf_ops = &rdtgroup_kf_single_ops,
1232 .seq_show = rdt_last_cmd_status_show,
1233 .fflags = RF_TOP_INFO,
1236 .name = "num_closids",
1238 .kf_ops = &rdtgroup_kf_single_ops,
1239 .seq_show = rdt_num_closids_show,
1240 .fflags = RF_CTRL_INFO,
1243 .name = "mon_features",
1245 .kf_ops = &rdtgroup_kf_single_ops,
1246 .seq_show = rdt_mon_features_show,
1247 .fflags = RF_MON_INFO,
1250 .name = "num_rmids",
1252 .kf_ops = &rdtgroup_kf_single_ops,
1253 .seq_show = rdt_num_rmids_show,
1254 .fflags = RF_MON_INFO,
1259 .kf_ops = &rdtgroup_kf_single_ops,
1260 .seq_show = rdt_default_ctrl_show,
1261 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1264 .name = "min_cbm_bits",
1266 .kf_ops = &rdtgroup_kf_single_ops,
1267 .seq_show = rdt_min_cbm_bits_show,
1268 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1271 .name = "shareable_bits",
1273 .kf_ops = &rdtgroup_kf_single_ops,
1274 .seq_show = rdt_shareable_bits_show,
1275 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1278 .name = "bit_usage",
1280 .kf_ops = &rdtgroup_kf_single_ops,
1281 .seq_show = rdt_bit_usage_show,
1282 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1285 .name = "min_bandwidth",
1287 .kf_ops = &rdtgroup_kf_single_ops,
1288 .seq_show = rdt_min_bw_show,
1289 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1292 .name = "bandwidth_gran",
1294 .kf_ops = &rdtgroup_kf_single_ops,
1295 .seq_show = rdt_bw_gran_show,
1296 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1299 .name = "delay_linear",
1301 .kf_ops = &rdtgroup_kf_single_ops,
1302 .seq_show = rdt_delay_linear_show,
1303 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1306 .name = "max_threshold_occupancy",
1308 .kf_ops = &rdtgroup_kf_single_ops,
1309 .write = max_threshold_occ_write,
1310 .seq_show = max_threshold_occ_show,
1311 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
1316 .kf_ops = &rdtgroup_kf_single_ops,
1317 .write = rdtgroup_cpus_write,
1318 .seq_show = rdtgroup_cpus_show,
1319 .fflags = RFTYPE_BASE,
1322 .name = "cpus_list",
1324 .kf_ops = &rdtgroup_kf_single_ops,
1325 .write = rdtgroup_cpus_write,
1326 .seq_show = rdtgroup_cpus_show,
1327 .flags = RFTYPE_FLAGS_CPUS_LIST,
1328 .fflags = RFTYPE_BASE,
1333 .kf_ops = &rdtgroup_kf_single_ops,
1334 .write = rdtgroup_tasks_write,
1335 .seq_show = rdtgroup_tasks_show,
1336 .fflags = RFTYPE_BASE,
1341 .kf_ops = &rdtgroup_kf_single_ops,
1342 .write = rdtgroup_schemata_write,
1343 .seq_show = rdtgroup_schemata_show,
1344 .fflags = RF_CTRL_BASE,
1349 .kf_ops = &rdtgroup_kf_single_ops,
1350 .write = rdtgroup_mode_write,
1351 .seq_show = rdtgroup_mode_show,
1352 .fflags = RF_CTRL_BASE,
1357 .kf_ops = &rdtgroup_kf_single_ops,
1358 .seq_show = rdtgroup_size_show,
1359 .fflags = RF_CTRL_BASE,
1364 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1366 struct rftype *rfts, *rft;
1369 rfts = res_common_files;
1370 len = ARRAY_SIZE(res_common_files);
1372 lockdep_assert_held(&rdtgroup_mutex);
1374 for (rft = rfts; rft < rfts + len; rft++) {
1375 if ((fflags & rft->fflags) == rft->fflags) {
1376 ret = rdtgroup_add_file(kn, rft);
1384 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1385 while (--rft >= rfts) {
1386 if ((fflags & rft->fflags) == rft->fflags)
1387 kernfs_remove_by_name(kn, rft->name);
1393 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1394 * @r: The resource group with which the file is associated.
1395 * @name: Name of the file
1397 * The permissions of named resctrl file, directory, or link are modified
1398 * to not allow read, write, or execute by any user.
1400 * WARNING: This function is intended to communicate to the user that the
1401 * resctrl file has been locked down - that it is not relevant to the
1402 * particular state the system finds itself in. It should not be relied
1403 * on to protect from user access because after the file's permissions
1404 * are restricted the user can still change the permissions using chmod
1405 * from the command line.
1407 * Return: 0 on success, <0 on failure.
1409 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1411 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1412 struct kernfs_node *kn;
1415 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1419 switch (kernfs_type(kn)) {
1421 iattr.ia_mode = S_IFDIR;
1424 iattr.ia_mode = S_IFREG;
1427 iattr.ia_mode = S_IFLNK;
1431 ret = kernfs_setattr(kn, &iattr);
1437 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1438 * @r: The resource group with which the file is associated.
1439 * @name: Name of the file
1440 * @mask: Mask of permissions that should be restored
1442 * Restore the permissions of the named file. If @name is a directory the
1443 * permissions of its parent will be used.
1445 * Return: 0 on success, <0 on failure.
1447 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1450 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1451 struct kernfs_node *kn, *parent;
1452 struct rftype *rfts, *rft;
1455 rfts = res_common_files;
1456 len = ARRAY_SIZE(res_common_files);
1458 for (rft = rfts; rft < rfts + len; rft++) {
1459 if (!strcmp(rft->name, name))
1460 iattr.ia_mode = rft->mode & mask;
1463 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1467 switch (kernfs_type(kn)) {
1469 parent = kernfs_get_parent(kn);
1471 iattr.ia_mode |= parent->mode;
1474 iattr.ia_mode |= S_IFDIR;
1477 iattr.ia_mode |= S_IFREG;
1480 iattr.ia_mode |= S_IFLNK;
1484 ret = kernfs_setattr(kn, &iattr);
1489 static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
1490 unsigned long fflags)
1492 struct kernfs_node *kn_subdir;
1495 kn_subdir = kernfs_create_dir(kn_info, name,
1497 if (IS_ERR(kn_subdir))
1498 return PTR_ERR(kn_subdir);
1500 kernfs_get(kn_subdir);
1501 ret = rdtgroup_kn_set_ugid(kn_subdir);
1505 ret = rdtgroup_add_files(kn_subdir, fflags);
1507 kernfs_activate(kn_subdir);
1512 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
1514 struct rdt_resource *r;
1515 unsigned long fflags;
1519 /* create the directory */
1520 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
1521 if (IS_ERR(kn_info))
1522 return PTR_ERR(kn_info);
1523 kernfs_get(kn_info);
1525 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
1529 for_each_alloc_enabled_rdt_resource(r) {
1530 fflags = r->fflags | RF_CTRL_INFO;
1531 ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
1536 for_each_mon_enabled_rdt_resource(r) {
1537 fflags = r->fflags | RF_MON_INFO;
1538 sprintf(name, "%s_MON", r->name);
1539 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
1545 * This extra ref will be put in kernfs_remove() and guarantees
1546 * that @rdtgrp->kn is always accessible.
1548 kernfs_get(kn_info);
1550 ret = rdtgroup_kn_set_ugid(kn_info);
1554 kernfs_activate(kn_info);
1559 kernfs_remove(kn_info);
1564 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
1565 char *name, struct kernfs_node **dest_kn)
1567 struct kernfs_node *kn;
1570 /* create the directory */
1571 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1579 * This extra ref will be put in kernfs_remove() and guarantees
1580 * that @rdtgrp->kn is always accessible.
1584 ret = rdtgroup_kn_set_ugid(kn);
1588 kernfs_activate(kn);
1597 static void l3_qos_cfg_update(void *arg)
1601 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
1604 static void l2_qos_cfg_update(void *arg)
1608 wrmsrl(IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
1611 static inline bool is_mba_linear(void)
1613 return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear;
1616 static int set_cache_qos_cfg(int level, bool enable)
1618 void (*update)(void *arg);
1619 struct rdt_resource *r_l;
1620 cpumask_var_t cpu_mask;
1621 struct rdt_domain *d;
1624 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1627 if (level == RDT_RESOURCE_L3)
1628 update = l3_qos_cfg_update;
1629 else if (level == RDT_RESOURCE_L2)
1630 update = l2_qos_cfg_update;
1634 r_l = &rdt_resources_all[level];
1635 list_for_each_entry(d, &r_l->domains, list) {
1636 /* Pick one CPU from each domain instance to update MSR */
1637 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1640 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1641 if (cpumask_test_cpu(cpu, cpu_mask))
1643 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1644 smp_call_function_many(cpu_mask, update, &enable, 1);
1647 free_cpumask_var(cpu_mask);
1653 * Enable or disable the MBA software controller
1654 * which helps user specify bandwidth in MBps.
1655 * MBA software controller is supported only if
1656 * MBM is supported and MBA is in linear scale.
1658 static int set_mba_sc(bool mba_sc)
1660 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA];
1661 struct rdt_domain *d;
1663 if (!is_mbm_enabled() || !is_mba_linear() ||
1664 mba_sc == is_mba_sc(r))
1667 r->membw.mba_sc = mba_sc;
1668 list_for_each_entry(d, &r->domains, list)
1669 setup_default_ctrlval(r, d->ctrl_val, d->mbps_val);
1674 static int cdp_enable(int level, int data_type, int code_type)
1676 struct rdt_resource *r_ldata = &rdt_resources_all[data_type];
1677 struct rdt_resource *r_lcode = &rdt_resources_all[code_type];
1678 struct rdt_resource *r_l = &rdt_resources_all[level];
1681 if (!r_l->alloc_capable || !r_ldata->alloc_capable ||
1682 !r_lcode->alloc_capable)
1685 ret = set_cache_qos_cfg(level, true);
1687 r_l->alloc_enabled = false;
1688 r_ldata->alloc_enabled = true;
1689 r_lcode->alloc_enabled = true;
1694 static int cdpl3_enable(void)
1696 return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA,
1697 RDT_RESOURCE_L3CODE);
1700 static int cdpl2_enable(void)
1702 return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA,
1703 RDT_RESOURCE_L2CODE);
1706 static void cdp_disable(int level, int data_type, int code_type)
1708 struct rdt_resource *r = &rdt_resources_all[level];
1710 r->alloc_enabled = r->alloc_capable;
1712 if (rdt_resources_all[data_type].alloc_enabled) {
1713 rdt_resources_all[data_type].alloc_enabled = false;
1714 rdt_resources_all[code_type].alloc_enabled = false;
1715 set_cache_qos_cfg(level, false);
1719 static void cdpl3_disable(void)
1721 cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE);
1724 static void cdpl2_disable(void)
1726 cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE);
1729 static void cdp_disable_all(void)
1731 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
1733 if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled)
1737 static int parse_rdtgroupfs_options(char *data)
1739 char *token, *o = data;
1742 while ((token = strsep(&o, ",")) != NULL) {
1748 if (!strcmp(token, "cdp")) {
1749 ret = cdpl3_enable();
1752 } else if (!strcmp(token, "cdpl2")) {
1753 ret = cdpl2_enable();
1756 } else if (!strcmp(token, "mba_MBps")) {
1757 ret = set_mba_sc(true);
1769 pr_err("Invalid mount option \"%s\"\n", token);
1775 * We don't allow rdtgroup directories to be created anywhere
1776 * except the root directory. Thus when looking for the rdtgroup
1777 * structure for a kernfs node we are either looking at a directory,
1778 * in which case the rdtgroup structure is pointed at by the "priv"
1779 * field, otherwise we have a file, and need only look to the parent
1780 * to find the rdtgroup.
1782 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1784 if (kernfs_type(kn) == KERNFS_DIR) {
1786 * All the resource directories use "kn->priv"
1787 * to point to the "struct rdtgroup" for the
1788 * resource. "info" and its subdirectories don't
1789 * have rdtgroup structures, so return NULL here.
1791 if (kn == kn_info || kn->parent == kn_info)
1796 return kn->parent->priv;
1800 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1802 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1807 atomic_inc(&rdtgrp->waitcount);
1808 kernfs_break_active_protection(kn);
1810 mutex_lock(&rdtgroup_mutex);
1812 /* Was this group deleted while we waited? */
1813 if (rdtgrp->flags & RDT_DELETED)
1819 void rdtgroup_kn_unlock(struct kernfs_node *kn)
1821 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1826 mutex_unlock(&rdtgroup_mutex);
1828 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
1829 (rdtgrp->flags & RDT_DELETED)) {
1830 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
1831 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
1832 rdtgroup_pseudo_lock_remove(rdtgrp);
1833 kernfs_unbreak_active_protection(kn);
1834 kernfs_put(rdtgrp->kn);
1837 kernfs_unbreak_active_protection(kn);
1841 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1842 struct rdtgroup *prgrp,
1843 struct kernfs_node **mon_data_kn);
1845 static struct dentry *rdt_mount(struct file_system_type *fs_type,
1846 int flags, const char *unused_dev_name,
1849 struct rdt_domain *dom;
1850 struct rdt_resource *r;
1851 struct dentry *dentry;
1855 mutex_lock(&rdtgroup_mutex);
1857 * resctrl file system can only be mounted once.
1859 if (static_branch_unlikely(&rdt_enable_key)) {
1860 dentry = ERR_PTR(-EBUSY);
1864 ret = parse_rdtgroupfs_options(data);
1866 dentry = ERR_PTR(ret);
1872 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
1874 dentry = ERR_PTR(ret);
1878 if (rdt_mon_capable) {
1879 ret = mongroup_create_dir(rdtgroup_default.kn,
1883 dentry = ERR_PTR(ret);
1886 kernfs_get(kn_mongrp);
1888 ret = mkdir_mondata_all(rdtgroup_default.kn,
1889 &rdtgroup_default, &kn_mondata);
1891 dentry = ERR_PTR(ret);
1894 kernfs_get(kn_mondata);
1895 rdtgroup_default.mon.mon_data_kn = kn_mondata;
1898 ret = rdt_pseudo_lock_init();
1900 dentry = ERR_PTR(ret);
1904 dentry = kernfs_mount(fs_type, flags, rdt_root,
1905 RDTGROUP_SUPER_MAGIC, NULL);
1909 if (rdt_alloc_capable)
1910 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
1911 if (rdt_mon_capable)
1912 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
1914 if (rdt_alloc_capable || rdt_mon_capable)
1915 static_branch_enable_cpuslocked(&rdt_enable_key);
1917 if (is_mbm_enabled()) {
1918 r = &rdt_resources_all[RDT_RESOURCE_L3];
1919 list_for_each_entry(dom, &r->domains, list)
1920 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
1926 rdt_pseudo_lock_release();
1928 if (rdt_mon_capable)
1929 kernfs_remove(kn_mondata);
1931 if (rdt_mon_capable)
1932 kernfs_remove(kn_mongrp);
1934 kernfs_remove(kn_info);
1938 rdt_last_cmd_clear();
1939 mutex_unlock(&rdtgroup_mutex);
1945 static int reset_all_ctrls(struct rdt_resource *r)
1947 struct msr_param msr_param;
1948 cpumask_var_t cpu_mask;
1949 struct rdt_domain *d;
1952 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1957 msr_param.high = r->num_closid;
1960 * Disable resource control for this resource by setting all
1961 * CBMs in all domains to the maximum mask value. Pick one CPU
1962 * from each domain to update the MSRs below.
1964 list_for_each_entry(d, &r->domains, list) {
1965 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1967 for (i = 0; i < r->num_closid; i++)
1968 d->ctrl_val[i] = r->default_ctrl;
1971 /* Update CBM on this cpu if it's in cpu_mask. */
1972 if (cpumask_test_cpu(cpu, cpu_mask))
1973 rdt_ctrl_update(&msr_param);
1974 /* Update CBM on all other cpus in cpu_mask. */
1975 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
1978 free_cpumask_var(cpu_mask);
1983 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
1985 return (rdt_alloc_capable &&
1986 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
1989 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
1991 return (rdt_mon_capable &&
1992 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
1996 * Move tasks from one to the other group. If @from is NULL, then all tasks
1997 * in the systems are moved unconditionally (used for teardown).
1999 * If @mask is not NULL the cpus on which moved tasks are running are set
2000 * in that mask so the update smp function call is restricted to affected
2003 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2004 struct cpumask *mask)
2006 struct task_struct *p, *t;
2008 read_lock(&tasklist_lock);
2009 for_each_process_thread(p, t) {
2010 if (!from || is_closid_match(t, from) ||
2011 is_rmid_match(t, from)) {
2012 t->closid = to->closid;
2013 t->rmid = to->mon.rmid;
2017 * This is safe on x86 w/o barriers as the ordering
2018 * of writing to task_cpu() and t->on_cpu is
2019 * reverse to the reading here. The detection is
2020 * inaccurate as tasks might move or schedule
2021 * before the smp function call takes place. In
2022 * such a case the function call is pointless, but
2023 * there is no other side effect.
2025 if (mask && t->on_cpu)
2026 cpumask_set_cpu(task_cpu(t), mask);
2030 read_unlock(&tasklist_lock);
2033 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2035 struct rdtgroup *sentry, *stmp;
2036 struct list_head *head;
2038 head = &rdtgrp->mon.crdtgrp_list;
2039 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2040 free_rmid(sentry->mon.rmid);
2041 list_del(&sentry->mon.crdtgrp_list);
2047 * Forcibly remove all of subdirectories under root.
2049 static void rmdir_all_sub(void)
2051 struct rdtgroup *rdtgrp, *tmp;
2053 /* Move all tasks to the default resource group */
2054 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2056 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2057 /* Free any child rmids */
2058 free_all_child_rdtgrp(rdtgrp);
2060 /* Remove each rdtgroup other than root */
2061 if (rdtgrp == &rdtgroup_default)
2064 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2065 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2066 rdtgroup_pseudo_lock_remove(rdtgrp);
2069 * Give any CPUs back to the default group. We cannot copy
2070 * cpu_online_mask because a CPU might have executed the
2071 * offline callback already, but is still marked online.
2073 cpumask_or(&rdtgroup_default.cpu_mask,
2074 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2076 free_rmid(rdtgrp->mon.rmid);
2078 kernfs_remove(rdtgrp->kn);
2079 list_del(&rdtgrp->rdtgroup_list);
2082 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2083 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2085 kernfs_remove(kn_info);
2086 kernfs_remove(kn_mongrp);
2087 kernfs_remove(kn_mondata);
2090 static void rdt_kill_sb(struct super_block *sb)
2092 struct rdt_resource *r;
2095 mutex_lock(&rdtgroup_mutex);
2099 /*Put everything back to default values. */
2100 for_each_alloc_enabled_rdt_resource(r)
2104 rdt_pseudo_lock_release();
2105 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2106 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2107 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2108 static_branch_disable_cpuslocked(&rdt_enable_key);
2110 mutex_unlock(&rdtgroup_mutex);
2114 static struct file_system_type rdt_fs_type = {
2117 .kill_sb = rdt_kill_sb,
2120 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2123 struct kernfs_node *kn;
2126 kn = __kernfs_create_file(parent_kn, name, 0444,
2127 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
2128 &kf_mondata_ops, priv, NULL, NULL);
2132 ret = rdtgroup_kn_set_ugid(kn);
2142 * Remove all subdirectories of mon_data of ctrl_mon groups
2143 * and monitor groups with given domain id.
2145 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
2147 struct rdtgroup *prgrp, *crgrp;
2150 if (!r->mon_enabled)
2153 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2154 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2155 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2157 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2158 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2162 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2163 struct rdt_domain *d,
2164 struct rdt_resource *r, struct rdtgroup *prgrp)
2166 union mon_data_bits priv;
2167 struct kernfs_node *kn;
2168 struct mon_evt *mevt;
2169 struct rmid_read rr;
2173 sprintf(name, "mon_%s_%02d", r->name, d->id);
2174 /* create the directory */
2175 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2180 * This extra ref will be put in kernfs_remove() and guarantees
2181 * that kn is always accessible.
2184 ret = rdtgroup_kn_set_ugid(kn);
2188 if (WARN_ON(list_empty(&r->evt_list))) {
2193 priv.u.rid = r->rid;
2194 priv.u.domid = d->id;
2195 list_for_each_entry(mevt, &r->evt_list, list) {
2196 priv.u.evtid = mevt->evtid;
2197 ret = mon_addfile(kn, mevt->name, priv.priv);
2201 if (is_mbm_event(mevt->evtid))
2202 mon_event_read(&rr, d, prgrp, mevt->evtid, true);
2204 kernfs_activate(kn);
2213 * Add all subdirectories of mon_data for "ctrl_mon" groups
2214 * and "monitor" groups with given domain id.
2216 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2217 struct rdt_domain *d)
2219 struct kernfs_node *parent_kn;
2220 struct rdtgroup *prgrp, *crgrp;
2221 struct list_head *head;
2223 if (!r->mon_enabled)
2226 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2227 parent_kn = prgrp->mon.mon_data_kn;
2228 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2230 head = &prgrp->mon.crdtgrp_list;
2231 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2232 parent_kn = crgrp->mon.mon_data_kn;
2233 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2238 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2239 struct rdt_resource *r,
2240 struct rdtgroup *prgrp)
2242 struct rdt_domain *dom;
2245 list_for_each_entry(dom, &r->domains, list) {
2246 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2255 * This creates a directory mon_data which contains the monitored data.
2257 * mon_data has one directory for each domain whic are named
2258 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2259 * with L3 domain looks as below:
2266 * Each domain directory has one file per event:
2271 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2272 struct rdtgroup *prgrp,
2273 struct kernfs_node **dest_kn)
2275 struct rdt_resource *r;
2276 struct kernfs_node *kn;
2280 * Create the mon_data directory first.
2282 ret = mongroup_create_dir(parent_kn, NULL, "mon_data", &kn);
2290 * Create the subdirectories for each domain. Note that all events
2291 * in a domain like L3 are grouped into a resource whose domain is L3
2293 for_each_mon_enabled_rdt_resource(r) {
2294 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2307 * cbm_ensure_valid - Enforce validity on provided CBM
2308 * @_val: Candidate CBM
2309 * @r: RDT resource to which the CBM belongs
2311 * The provided CBM represents all cache portions available for use. This
2312 * may be represented by a bitmap that does not consist of contiguous ones
2313 * and thus be an invalid CBM.
2314 * Here the provided CBM is forced to be a valid CBM by only considering
2315 * the first set of contiguous bits as valid and clearing all bits.
2316 * The intention here is to provide a valid default CBM with which a new
2317 * resource group is initialized. The user can follow this with a
2318 * modification to the CBM if the default does not satisfy the
2321 static void cbm_ensure_valid(u32 *_val, struct rdt_resource *r)
2324 * Convert the u32 _val to an unsigned long required by all the bit
2325 * operations within this function. No more than 32 bits of this
2326 * converted value can be accessed because all bit operations are
2327 * additionally provided with cbm_len that is initialized during
2328 * hardware enumeration using five bits from the EAX register and
2329 * thus never can exceed 32 bits.
2331 unsigned long *val = (unsigned long *)_val;
2332 unsigned int cbm_len = r->cache.cbm_len;
2333 unsigned long first_bit, zero_bit;
2338 first_bit = find_first_bit(val, cbm_len);
2339 zero_bit = find_next_zero_bit(val, cbm_len, first_bit);
2341 /* Clear any remaining bits to ensure contiguous region */
2342 bitmap_clear(val, zero_bit, cbm_len - zero_bit);
2346 * rdtgroup_init_alloc - Initialize the new RDT group's allocations
2348 * A new RDT group is being created on an allocation capable (CAT)
2349 * supporting system. Set this group up to start off with all usable
2350 * allocations. That is, all shareable and unused bits.
2352 * All-zero CBM is invalid. If there are no more shareable bits available
2353 * on any domain then the entire allocation will fail.
2355 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
2357 u32 used_b = 0, unused_b = 0;
2358 u32 closid = rdtgrp->closid;
2359 struct rdt_resource *r;
2360 unsigned long tmp_cbm;
2361 enum rdtgrp_mode mode;
2362 struct rdt_domain *d;
2366 for_each_alloc_enabled_rdt_resource(r) {
2368 * Only initialize default allocations for CBM cache
2371 if (r->rid == RDT_RESOURCE_MBA)
2373 list_for_each_entry(d, &r->domains, list) {
2374 d->have_new_ctrl = false;
2375 d->new_ctrl = r->cache.shareable_bits;
2376 used_b = r->cache.shareable_bits;
2378 for (i = 0; i < closids_supported(); i++, ctrl++) {
2379 if (closid_allocated(i) && i != closid) {
2380 mode = rdtgroup_mode_by_closid(i);
2381 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
2384 if (mode == RDT_MODE_SHAREABLE)
2385 d->new_ctrl |= *ctrl;
2388 if (d->plr && d->plr->cbm > 0)
2389 used_b |= d->plr->cbm;
2390 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
2391 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
2392 d->new_ctrl |= unused_b;
2394 * Force the initial CBM to be valid, user can
2395 * modify the CBM based on system availability.
2397 cbm_ensure_valid(&d->new_ctrl, r);
2399 * Assign the u32 CBM to an unsigned long to ensure
2400 * that bitmap_weight() does not access out-of-bound
2403 tmp_cbm = d->new_ctrl;
2404 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) <
2405 r->cache.min_cbm_bits) {
2406 rdt_last_cmd_printf("no space on %s:%d\n",
2410 d->have_new_ctrl = true;
2414 for_each_alloc_enabled_rdt_resource(r) {
2416 * Only initialize default allocations for CBM cache
2419 if (r->rid == RDT_RESOURCE_MBA)
2421 ret = update_domains(r, rdtgrp->closid);
2423 rdt_last_cmd_puts("failed to initialize allocations\n");
2426 rdtgrp->mode = RDT_MODE_SHAREABLE;
2432 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
2433 struct kernfs_node *prgrp_kn,
2434 const char *name, umode_t mode,
2435 enum rdt_group_type rtype, struct rdtgroup **r)
2437 struct rdtgroup *prdtgrp, *rdtgrp;
2438 struct kernfs_node *kn;
2442 prdtgrp = rdtgroup_kn_lock_live(prgrp_kn);
2443 rdt_last_cmd_clear();
2446 rdt_last_cmd_puts("directory was removed\n");
2450 if (rtype == RDTMON_GROUP &&
2451 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2452 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
2454 rdt_last_cmd_puts("pseudo-locking in progress\n");
2458 /* allocate the rdtgroup. */
2459 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
2462 rdt_last_cmd_puts("kernel out of memory\n");
2466 rdtgrp->mon.parent = prdtgrp;
2467 rdtgrp->type = rtype;
2468 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
2470 /* kernfs creates the directory for rdtgrp */
2471 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
2474 rdt_last_cmd_puts("kernfs create error\n");
2480 * kernfs_remove() will drop the reference count on "kn" which
2481 * will free it. But we still need it to stick around for the
2482 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
2483 * here, which will be dropped inside rdtgroup_kn_unlock().
2487 ret = rdtgroup_kn_set_ugid(kn);
2489 rdt_last_cmd_puts("kernfs perm error\n");
2493 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
2494 ret = rdtgroup_add_files(kn, files);
2496 rdt_last_cmd_puts("kernfs fill error\n");
2500 if (rdt_mon_capable) {
2503 rdt_last_cmd_puts("out of RMIDs\n");
2506 rdtgrp->mon.rmid = ret;
2508 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
2510 rdt_last_cmd_puts("kernfs subdir error\n");
2514 kernfs_activate(kn);
2517 * The caller unlocks the prgrp_kn upon success.
2522 free_rmid(rdtgrp->mon.rmid);
2524 kernfs_remove(rdtgrp->kn);
2528 rdtgroup_kn_unlock(prgrp_kn);
2532 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
2534 kernfs_remove(rgrp->kn);
2535 free_rmid(rgrp->mon.rmid);
2540 * Create a monitor group under "mon_groups" directory of a control
2541 * and monitor group(ctrl_mon). This is a resource group
2542 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
2544 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
2545 struct kernfs_node *prgrp_kn,
2549 struct rdtgroup *rdtgrp, *prgrp;
2552 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP,
2557 prgrp = rdtgrp->mon.parent;
2558 rdtgrp->closid = prgrp->closid;
2561 * Add the rdtgrp to the list of rdtgrps the parent
2562 * ctrl_mon group has to track.
2564 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
2566 rdtgroup_kn_unlock(prgrp_kn);
2571 * These are rdtgroups created under the root directory. Can be used
2572 * to allocate and monitor resources.
2574 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
2575 struct kernfs_node *prgrp_kn,
2576 const char *name, umode_t mode)
2578 struct rdtgroup *rdtgrp;
2579 struct kernfs_node *kn;
2583 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP,
2589 ret = closid_alloc();
2591 rdt_last_cmd_puts("out of CLOSIDs\n");
2592 goto out_common_fail;
2597 rdtgrp->closid = closid;
2598 ret = rdtgroup_init_alloc(rdtgrp);
2602 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
2604 if (rdt_mon_capable) {
2606 * Create an empty mon_groups directory to hold the subset
2607 * of tasks and cpus to monitor.
2609 ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL);
2611 rdt_last_cmd_puts("kernfs subdir error\n");
2619 list_del(&rdtgrp->rdtgroup_list);
2621 closid_free(closid);
2623 mkdir_rdt_prepare_clean(rdtgrp);
2625 rdtgroup_kn_unlock(prgrp_kn);
2630 * We allow creating mon groups only with in a directory called "mon_groups"
2631 * which is present in every ctrl_mon group. Check if this is a valid
2632 * "mon_groups" directory.
2634 * 1. The directory should be named "mon_groups".
2635 * 2. The mon group itself should "not" be named "mon_groups".
2636 * This makes sure "mon_groups" directory always has a ctrl_mon group
2639 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
2641 return (!strcmp(kn->name, "mon_groups") &&
2642 strcmp(name, "mon_groups"));
2645 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
2648 /* Do not accept '\n' to avoid unparsable situation. */
2649 if (strchr(name, '\n'))
2653 * If the parent directory is the root directory and RDT
2654 * allocation is supported, add a control and monitoring
2657 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
2658 return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode);
2661 * If RDT monitoring is supported and the parent directory is a valid
2662 * "mon_groups" directory, add a monitoring subdirectory.
2664 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
2665 return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode);
2670 static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
2671 cpumask_var_t tmpmask)
2673 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
2676 /* Give any tasks back to the parent group */
2677 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
2679 /* Update per cpu rmid of the moved CPUs first */
2680 for_each_cpu(cpu, &rdtgrp->cpu_mask)
2681 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
2683 * Update the MSR on moved CPUs and CPUs which have moved
2684 * task running on them.
2686 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
2687 update_closid_rmid(tmpmask, NULL);
2689 rdtgrp->flags = RDT_DELETED;
2690 free_rmid(rdtgrp->mon.rmid);
2693 * Remove the rdtgrp from the parent ctrl_mon group's list
2695 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
2696 list_del(&rdtgrp->mon.crdtgrp_list);
2699 * one extra hold on this, will drop when we kfree(rdtgrp)
2700 * in rdtgroup_kn_unlock()
2703 kernfs_remove(rdtgrp->kn);
2708 static int rdtgroup_ctrl_remove(struct kernfs_node *kn,
2709 struct rdtgroup *rdtgrp)
2711 rdtgrp->flags = RDT_DELETED;
2712 list_del(&rdtgrp->rdtgroup_list);
2715 * one extra hold on this, will drop when we kfree(rdtgrp)
2716 * in rdtgroup_kn_unlock()
2719 kernfs_remove(rdtgrp->kn);
2723 static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
2724 cpumask_var_t tmpmask)
2728 /* Give any tasks back to the default group */
2729 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
2731 /* Give any CPUs back to the default group */
2732 cpumask_or(&rdtgroup_default.cpu_mask,
2733 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2735 /* Update per cpu closid and rmid of the moved CPUs first */
2736 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
2737 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
2738 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
2742 * Update the MSR on moved CPUs and CPUs which have moved
2743 * task running on them.
2745 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
2746 update_closid_rmid(tmpmask, NULL);
2748 closid_free(rdtgrp->closid);
2749 free_rmid(rdtgrp->mon.rmid);
2752 * Free all the child monitor group rmids.
2754 free_all_child_rdtgrp(rdtgrp);
2756 rdtgroup_ctrl_remove(kn, rdtgrp);
2761 static int rdtgroup_rmdir(struct kernfs_node *kn)
2763 struct kernfs_node *parent_kn = kn->parent;
2764 struct rdtgroup *rdtgrp;
2765 cpumask_var_t tmpmask;
2768 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
2771 rdtgrp = rdtgroup_kn_lock_live(kn);
2778 * If the rdtgroup is a ctrl_mon group and parent directory
2779 * is the root directory, remove the ctrl_mon group.
2781 * If the rdtgroup is a mon group and parent directory
2782 * is a valid "mon_groups" directory, remove the mon group.
2784 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn) {
2785 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2786 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
2787 ret = rdtgroup_ctrl_remove(kn, rdtgrp);
2789 ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask);
2791 } else if (rdtgrp->type == RDTMON_GROUP &&
2792 is_mon_groups(parent_kn, kn->name)) {
2793 ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask);
2799 rdtgroup_kn_unlock(kn);
2800 free_cpumask_var(tmpmask);
2804 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
2806 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
2807 seq_puts(seq, ",cdp");
2811 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
2812 .mkdir = rdtgroup_mkdir,
2813 .rmdir = rdtgroup_rmdir,
2814 .show_options = rdtgroup_show_options,
2817 static int __init rdtgroup_setup_root(void)
2821 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
2822 KERNFS_ROOT_CREATE_DEACTIVATED |
2823 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
2825 if (IS_ERR(rdt_root))
2826 return PTR_ERR(rdt_root);
2828 mutex_lock(&rdtgroup_mutex);
2830 rdtgroup_default.closid = 0;
2831 rdtgroup_default.mon.rmid = 0;
2832 rdtgroup_default.type = RDTCTRL_GROUP;
2833 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
2835 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
2837 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
2839 kernfs_destroy_root(rdt_root);
2843 rdtgroup_default.kn = rdt_root->kn;
2844 kernfs_activate(rdtgroup_default.kn);
2847 mutex_unlock(&rdtgroup_mutex);
2853 * rdtgroup_init - rdtgroup initialization
2855 * Setup resctrl file system including set up root, create mount point,
2856 * register rdtgroup filesystem, and initialize files under root directory.
2858 * Return: 0 on success or -errno
2860 int __init rdtgroup_init(void)
2864 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
2865 sizeof(last_cmd_status_buf));
2867 ret = rdtgroup_setup_root();
2871 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
2875 ret = register_filesystem(&rdt_fs_type);
2877 goto cleanup_mountpoint;
2880 * Adding the resctrl debugfs directory here may not be ideal since
2881 * it would let the resctrl debugfs directory appear on the debugfs
2882 * filesystem before the resctrl filesystem is mounted.
2883 * It may also be ok since that would enable debugging of RDT before
2884 * resctrl is mounted.
2885 * The reason why the debugfs directory is created here and not in
2886 * rdt_mount() is because rdt_mount() takes rdtgroup_mutex and
2887 * during the debugfs directory creation also &sb->s_type->i_mutex_key
2888 * (the lockdep class of inode->i_rwsem). Other filesystem
2889 * interactions (eg. SyS_getdents) have the lock ordering:
2890 * &sb->s_type->i_mutex_key --> &mm->mmap_sem
2891 * During mmap(), called with &mm->mmap_sem, the rdtgroup_mutex
2892 * is taken, thus creating dependency:
2893 * &mm->mmap_sem --> rdtgroup_mutex for the latter that can cause
2894 * issues considering the other two lock dependencies.
2895 * By creating the debugfs directory here we avoid a dependency
2896 * that may cause deadlock (even though file operations cannot
2897 * occur until the filesystem is mounted, but I do not know how to
2898 * tell lockdep that).
2900 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
2905 sysfs_remove_mount_point(fs_kobj, "resctrl");
2907 kernfs_destroy_root(rdt_root);
2912 void __exit rdtgroup_exit(void)
2914 debugfs_remove_recursive(debugfs_resctrl);
2915 unregister_filesystem(&rdt_fs_type);
2916 sysfs_remove_mount_point(fs_kobj, "resctrl");
2917 kernfs_destroy_root(rdt_root);