12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2fa7d9493SBabu Moger /* 3d9f6e12fSIngo Molnar * User interface for Resource Allocation in Resource Director Technology(RDT) 4fa7d9493SBabu Moger * 5fa7d9493SBabu Moger * Copyright (C) 2016 Intel Corporation 6fa7d9493SBabu Moger * 7fa7d9493SBabu Moger * Author: Fenghua Yu <fenghua.yu@intel.com> 8fa7d9493SBabu Moger * 9fa7d9493SBabu Moger * More information about RDT be found in the Intel (R) x86 Architecture 10fa7d9493SBabu Moger * Software Developer Manual. 11fa7d9493SBabu Moger */ 12fa7d9493SBabu Moger 13fa7d9493SBabu Moger #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14fa7d9493SBabu Moger 15fa7d9493SBabu Moger #include <linux/cacheinfo.h> 16fa7d9493SBabu Moger #include <linux/cpu.h> 17fa7d9493SBabu Moger #include <linux/debugfs.h> 18fa7d9493SBabu Moger #include <linux/fs.h> 1923bf1b6bSDavid Howells #include <linux/fs_parser.h> 20fa7d9493SBabu Moger #include <linux/sysfs.h> 21fa7d9493SBabu Moger #include <linux/kernfs.h> 22fa7d9493SBabu Moger #include <linux/seq_buf.h> 23fa7d9493SBabu Moger #include <linux/seq_file.h> 24fa7d9493SBabu Moger #include <linux/sched/signal.h> 25fa7d9493SBabu Moger #include <linux/sched/task.h> 26fa7d9493SBabu Moger #include <linux/slab.h> 27fa7d9493SBabu Moger #include <linux/task_work.h> 2823bf1b6bSDavid Howells #include <linux/user_namespace.h> 29fa7d9493SBabu Moger 30fa7d9493SBabu Moger #include <uapi/linux/magic.h> 31fa7d9493SBabu Moger 328dd97c65SReinette Chatre #include <asm/resctrl.h> 33fa7d9493SBabu Moger #include "internal.h" 34fa7d9493SBabu Moger 35fa7d9493SBabu Moger DEFINE_STATIC_KEY_FALSE(rdt_enable_key); 36fa7d9493SBabu Moger DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key); 37fa7d9493SBabu Moger DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key); 38fa7d9493SBabu Moger static struct kernfs_root *rdt_root; 39fa7d9493SBabu Moger struct rdtgroup rdtgroup_default; 40fa7d9493SBabu Moger LIST_HEAD(rdt_all_groups); 41fa7d9493SBabu Moger 42cdb9ebc9SJames Morse /* list of entries for the schemata file */ 43cdb9ebc9SJames Morse LIST_HEAD(resctrl_schema_all); 44cdb9ebc9SJames Morse 45fa7d9493SBabu Moger /* Kernel fs node for "info" directory under root */ 46fa7d9493SBabu Moger static struct kernfs_node *kn_info; 47fa7d9493SBabu Moger 48fa7d9493SBabu Moger /* Kernel fs node for "mon_groups" directory under root */ 49fa7d9493SBabu Moger static struct kernfs_node *kn_mongrp; 50fa7d9493SBabu Moger 51fa7d9493SBabu Moger /* Kernel fs node for "mon_data" directory under root */ 52fa7d9493SBabu Moger static struct kernfs_node *kn_mondata; 53fa7d9493SBabu Moger 54fa7d9493SBabu Moger static struct seq_buf last_cmd_status; 55fa7d9493SBabu Moger static char last_cmd_status_buf[512]; 56fa7d9493SBabu Moger 57fa7d9493SBabu Moger struct dentry *debugfs_resctrl; 58fa7d9493SBabu Moger 59fa7d9493SBabu Moger void rdt_last_cmd_clear(void) 60fa7d9493SBabu Moger { 61fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 62fa7d9493SBabu Moger seq_buf_clear(&last_cmd_status); 63fa7d9493SBabu Moger } 64fa7d9493SBabu Moger 65fa7d9493SBabu Moger void rdt_last_cmd_puts(const char *s) 66fa7d9493SBabu Moger { 67fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 68fa7d9493SBabu Moger seq_buf_puts(&last_cmd_status, s); 69fa7d9493SBabu Moger } 70fa7d9493SBabu Moger 71fa7d9493SBabu Moger void rdt_last_cmd_printf(const char *fmt, ...) 72fa7d9493SBabu Moger { 73fa7d9493SBabu Moger va_list ap; 74fa7d9493SBabu Moger 75fa7d9493SBabu Moger va_start(ap, fmt); 76fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 77fa7d9493SBabu Moger seq_buf_vprintf(&last_cmd_status, fmt, ap); 78fa7d9493SBabu Moger va_end(ap); 79fa7d9493SBabu Moger } 80fa7d9493SBabu Moger 81fa7d9493SBabu Moger /* 82fa7d9493SBabu Moger * Trivial allocator for CLOSIDs. Since h/w only supports a small number, 83fa7d9493SBabu Moger * we can keep a bitmap of free CLOSIDs in a single integer. 84fa7d9493SBabu Moger * 85fa7d9493SBabu Moger * Using a global CLOSID across all resources has some advantages and 86fa7d9493SBabu Moger * some drawbacks: 87fa7d9493SBabu Moger * + We can simply set "current->closid" to assign a task to a resource 88fa7d9493SBabu Moger * group. 89fa7d9493SBabu Moger * + Context switch code can avoid extra memory references deciding which 90fa7d9493SBabu Moger * CLOSID to load into the PQR_ASSOC MSR 91fa7d9493SBabu Moger * - We give up some options in configuring resource groups across multi-socket 92fa7d9493SBabu Moger * systems. 93fa7d9493SBabu Moger * - Our choices on how to configure each resource become progressively more 94fa7d9493SBabu Moger * limited as the number of resources grows. 95fa7d9493SBabu Moger */ 96fa7d9493SBabu Moger static int closid_free_map; 97fa7d9493SBabu Moger static int closid_free_map_len; 98fa7d9493SBabu Moger 99fa7d9493SBabu Moger int closids_supported(void) 100fa7d9493SBabu Moger { 101fa7d9493SBabu Moger return closid_free_map_len; 102fa7d9493SBabu Moger } 103fa7d9493SBabu Moger 104fa7d9493SBabu Moger static void closid_init(void) 105fa7d9493SBabu Moger { 106331ebe4cSJames Morse struct resctrl_schema *s; 107eb6f3187SJames Morse u32 rdt_min_closid = 32; 108fa7d9493SBabu Moger 109fa7d9493SBabu Moger /* Compute rdt_min_closid across all resources */ 1103183e87cSJames Morse list_for_each_entry(s, &resctrl_schema_all, list) 1113183e87cSJames Morse rdt_min_closid = min(rdt_min_closid, s->num_closid); 112fa7d9493SBabu Moger 113fa7d9493SBabu Moger closid_free_map = BIT_MASK(rdt_min_closid) - 1; 114fa7d9493SBabu Moger 115fa7d9493SBabu Moger /* CLOSID 0 is always reserved for the default group */ 116fa7d9493SBabu Moger closid_free_map &= ~1; 117fa7d9493SBabu Moger closid_free_map_len = rdt_min_closid; 118fa7d9493SBabu Moger } 119fa7d9493SBabu Moger 120fa7d9493SBabu Moger static int closid_alloc(void) 121fa7d9493SBabu Moger { 122fa7d9493SBabu Moger u32 closid = ffs(closid_free_map); 123fa7d9493SBabu Moger 124fa7d9493SBabu Moger if (closid == 0) 125fa7d9493SBabu Moger return -ENOSPC; 126fa7d9493SBabu Moger closid--; 127fa7d9493SBabu Moger closid_free_map &= ~(1 << closid); 128fa7d9493SBabu Moger 129fa7d9493SBabu Moger return closid; 130fa7d9493SBabu Moger } 131fa7d9493SBabu Moger 132fa7d9493SBabu Moger void closid_free(int closid) 133fa7d9493SBabu Moger { 134fa7d9493SBabu Moger closid_free_map |= 1 << closid; 135fa7d9493SBabu Moger } 136fa7d9493SBabu Moger 137fa7d9493SBabu Moger /** 138fa7d9493SBabu Moger * closid_allocated - test if provided closid is in use 139fa7d9493SBabu Moger * @closid: closid to be tested 140fa7d9493SBabu Moger * 141fa7d9493SBabu Moger * Return: true if @closid is currently associated with a resource group, 142fa7d9493SBabu Moger * false if @closid is free 143fa7d9493SBabu Moger */ 144fa7d9493SBabu Moger static bool closid_allocated(unsigned int closid) 145fa7d9493SBabu Moger { 146fa7d9493SBabu Moger return (closid_free_map & (1 << closid)) == 0; 147fa7d9493SBabu Moger } 148fa7d9493SBabu Moger 149fa7d9493SBabu Moger /** 150fa7d9493SBabu Moger * rdtgroup_mode_by_closid - Return mode of resource group with closid 151fa7d9493SBabu Moger * @closid: closid if the resource group 152fa7d9493SBabu Moger * 153fa7d9493SBabu Moger * Each resource group is associated with a @closid. Here the mode 154fa7d9493SBabu Moger * of a resource group can be queried by searching for it using its closid. 155fa7d9493SBabu Moger * 156fa7d9493SBabu Moger * Return: mode as &enum rdtgrp_mode of resource group with closid @closid 157fa7d9493SBabu Moger */ 158fa7d9493SBabu Moger enum rdtgrp_mode rdtgroup_mode_by_closid(int closid) 159fa7d9493SBabu Moger { 160fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 161fa7d9493SBabu Moger 162fa7d9493SBabu Moger list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { 163fa7d9493SBabu Moger if (rdtgrp->closid == closid) 164fa7d9493SBabu Moger return rdtgrp->mode; 165fa7d9493SBabu Moger } 166fa7d9493SBabu Moger 167fa7d9493SBabu Moger return RDT_NUM_MODES; 168fa7d9493SBabu Moger } 169fa7d9493SBabu Moger 170fa7d9493SBabu Moger static const char * const rdt_mode_str[] = { 171fa7d9493SBabu Moger [RDT_MODE_SHAREABLE] = "shareable", 172fa7d9493SBabu Moger [RDT_MODE_EXCLUSIVE] = "exclusive", 173fa7d9493SBabu Moger [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup", 174fa7d9493SBabu Moger [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked", 175fa7d9493SBabu Moger }; 176fa7d9493SBabu Moger 177fa7d9493SBabu Moger /** 178fa7d9493SBabu Moger * rdtgroup_mode_str - Return the string representation of mode 179fa7d9493SBabu Moger * @mode: the resource group mode as &enum rdtgroup_mode 180fa7d9493SBabu Moger * 181fa7d9493SBabu Moger * Return: string representation of valid mode, "unknown" otherwise 182fa7d9493SBabu Moger */ 183fa7d9493SBabu Moger static const char *rdtgroup_mode_str(enum rdtgrp_mode mode) 184fa7d9493SBabu Moger { 185fa7d9493SBabu Moger if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES) 186fa7d9493SBabu Moger return "unknown"; 187fa7d9493SBabu Moger 188fa7d9493SBabu Moger return rdt_mode_str[mode]; 189fa7d9493SBabu Moger } 190fa7d9493SBabu Moger 191fa7d9493SBabu Moger /* set uid and gid of rdtgroup dirs and files to that of the creator */ 192fa7d9493SBabu Moger static int rdtgroup_kn_set_ugid(struct kernfs_node *kn) 193fa7d9493SBabu Moger { 194fa7d9493SBabu Moger struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, 195fa7d9493SBabu Moger .ia_uid = current_fsuid(), 196fa7d9493SBabu Moger .ia_gid = current_fsgid(), }; 197fa7d9493SBabu Moger 198fa7d9493SBabu Moger if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && 199fa7d9493SBabu Moger gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) 200fa7d9493SBabu Moger return 0; 201fa7d9493SBabu Moger 202fa7d9493SBabu Moger return kernfs_setattr(kn, &iattr); 203fa7d9493SBabu Moger } 204fa7d9493SBabu Moger 205fa7d9493SBabu Moger static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft) 206fa7d9493SBabu Moger { 207fa7d9493SBabu Moger struct kernfs_node *kn; 208fa7d9493SBabu Moger int ret; 209fa7d9493SBabu Moger 210fa7d9493SBabu Moger kn = __kernfs_create_file(parent_kn, rft->name, rft->mode, 211fa7d9493SBabu Moger GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 212fa7d9493SBabu Moger 0, rft->kf_ops, rft, NULL, NULL); 213fa7d9493SBabu Moger if (IS_ERR(kn)) 214fa7d9493SBabu Moger return PTR_ERR(kn); 215fa7d9493SBabu Moger 216fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 217fa7d9493SBabu Moger if (ret) { 218fa7d9493SBabu Moger kernfs_remove(kn); 219fa7d9493SBabu Moger return ret; 220fa7d9493SBabu Moger } 221fa7d9493SBabu Moger 222fa7d9493SBabu Moger return 0; 223fa7d9493SBabu Moger } 224fa7d9493SBabu Moger 225fa7d9493SBabu Moger static int rdtgroup_seqfile_show(struct seq_file *m, void *arg) 226fa7d9493SBabu Moger { 227fa7d9493SBabu Moger struct kernfs_open_file *of = m->private; 228fa7d9493SBabu Moger struct rftype *rft = of->kn->priv; 229fa7d9493SBabu Moger 230fa7d9493SBabu Moger if (rft->seq_show) 231fa7d9493SBabu Moger return rft->seq_show(of, m, arg); 232fa7d9493SBabu Moger return 0; 233fa7d9493SBabu Moger } 234fa7d9493SBabu Moger 235fa7d9493SBabu Moger static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf, 236fa7d9493SBabu Moger size_t nbytes, loff_t off) 237fa7d9493SBabu Moger { 238fa7d9493SBabu Moger struct rftype *rft = of->kn->priv; 239fa7d9493SBabu Moger 240fa7d9493SBabu Moger if (rft->write) 241fa7d9493SBabu Moger return rft->write(of, buf, nbytes, off); 242fa7d9493SBabu Moger 243fa7d9493SBabu Moger return -EINVAL; 244fa7d9493SBabu Moger } 245fa7d9493SBabu Moger 2462002d295SRikard Falkeborn static const struct kernfs_ops rdtgroup_kf_single_ops = { 247fa7d9493SBabu Moger .atomic_write_len = PAGE_SIZE, 248fa7d9493SBabu Moger .write = rdtgroup_file_write, 249fa7d9493SBabu Moger .seq_show = rdtgroup_seqfile_show, 250fa7d9493SBabu Moger }; 251fa7d9493SBabu Moger 2522002d295SRikard Falkeborn static const struct kernfs_ops kf_mondata_ops = { 253fa7d9493SBabu Moger .atomic_write_len = PAGE_SIZE, 254fa7d9493SBabu Moger .seq_show = rdtgroup_mondata_show, 255fa7d9493SBabu Moger }; 256fa7d9493SBabu Moger 257fa7d9493SBabu Moger static bool is_cpu_list(struct kernfs_open_file *of) 258fa7d9493SBabu Moger { 259fa7d9493SBabu Moger struct rftype *rft = of->kn->priv; 260fa7d9493SBabu Moger 261fa7d9493SBabu Moger return rft->flags & RFTYPE_FLAGS_CPUS_LIST; 262fa7d9493SBabu Moger } 263fa7d9493SBabu Moger 264fa7d9493SBabu Moger static int rdtgroup_cpus_show(struct kernfs_open_file *of, 265fa7d9493SBabu Moger struct seq_file *s, void *v) 266fa7d9493SBabu Moger { 267fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 268fa7d9493SBabu Moger struct cpumask *mask; 269fa7d9493SBabu Moger int ret = 0; 270fa7d9493SBabu Moger 271fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 272fa7d9493SBabu Moger 273fa7d9493SBabu Moger if (rdtgrp) { 274fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 275fa7d9493SBabu Moger if (!rdtgrp->plr->d) { 276fa7d9493SBabu Moger rdt_last_cmd_clear(); 277fa7d9493SBabu Moger rdt_last_cmd_puts("Cache domain offline\n"); 278fa7d9493SBabu Moger ret = -ENODEV; 279fa7d9493SBabu Moger } else { 280fa7d9493SBabu Moger mask = &rdtgrp->plr->d->cpu_mask; 281fa7d9493SBabu Moger seq_printf(s, is_cpu_list(of) ? 282fa7d9493SBabu Moger "%*pbl\n" : "%*pb\n", 283fa7d9493SBabu Moger cpumask_pr_args(mask)); 284fa7d9493SBabu Moger } 285fa7d9493SBabu Moger } else { 286fa7d9493SBabu Moger seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n", 287fa7d9493SBabu Moger cpumask_pr_args(&rdtgrp->cpu_mask)); 288fa7d9493SBabu Moger } 289fa7d9493SBabu Moger } else { 290fa7d9493SBabu Moger ret = -ENOENT; 291fa7d9493SBabu Moger } 292fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 293fa7d9493SBabu Moger 294fa7d9493SBabu Moger return ret; 295fa7d9493SBabu Moger } 296fa7d9493SBabu Moger 297fa7d9493SBabu Moger /* 298352940ecSBabu Moger * This is safe against resctrl_sched_in() called from __switch_to() 299fa7d9493SBabu Moger * because __switch_to() is executed with interrupts disabled. A local call 300d9f6e12fSIngo Molnar * from update_closid_rmid() is protected against __switch_to() because 301fa7d9493SBabu Moger * preemption is disabled. 302fa7d9493SBabu Moger */ 303fa7d9493SBabu Moger static void update_cpu_closid_rmid(void *info) 304fa7d9493SBabu Moger { 305fa7d9493SBabu Moger struct rdtgroup *r = info; 306fa7d9493SBabu Moger 307fa7d9493SBabu Moger if (r) { 308fa7d9493SBabu Moger this_cpu_write(pqr_state.default_closid, r->closid); 309fa7d9493SBabu Moger this_cpu_write(pqr_state.default_rmid, r->mon.rmid); 310fa7d9493SBabu Moger } 311fa7d9493SBabu Moger 312fa7d9493SBabu Moger /* 313fa7d9493SBabu Moger * We cannot unconditionally write the MSR because the current 314fa7d9493SBabu Moger * executing task might have its own closid selected. Just reuse 315fa7d9493SBabu Moger * the context switch code. 316fa7d9493SBabu Moger */ 317352940ecSBabu Moger resctrl_sched_in(); 318fa7d9493SBabu Moger } 319fa7d9493SBabu Moger 320fa7d9493SBabu Moger /* 321fa7d9493SBabu Moger * Update the PGR_ASSOC MSR on all cpus in @cpu_mask, 322fa7d9493SBabu Moger * 323fa7d9493SBabu Moger * Per task closids/rmids must have been set up before calling this function. 324fa7d9493SBabu Moger */ 325fa7d9493SBabu Moger static void 326fa7d9493SBabu Moger update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r) 327fa7d9493SBabu Moger { 328fa7d9493SBabu Moger int cpu = get_cpu(); 329fa7d9493SBabu Moger 330fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 331fa7d9493SBabu Moger update_cpu_closid_rmid(r); 332fa7d9493SBabu Moger smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1); 333fa7d9493SBabu Moger put_cpu(); 334fa7d9493SBabu Moger } 335fa7d9493SBabu Moger 336fa7d9493SBabu Moger static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, 337fa7d9493SBabu Moger cpumask_var_t tmpmask) 338fa7d9493SBabu Moger { 339fa7d9493SBabu Moger struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp; 340fa7d9493SBabu Moger struct list_head *head; 341fa7d9493SBabu Moger 342fa7d9493SBabu Moger /* Check whether cpus belong to parent ctrl group */ 343fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask); 344*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask)) { 345723f1a0dSBabu Moger rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n"); 346fa7d9493SBabu Moger return -EINVAL; 347fa7d9493SBabu Moger } 348fa7d9493SBabu Moger 349fa7d9493SBabu Moger /* Check whether cpus are dropped from this group */ 350fa7d9493SBabu Moger cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); 351*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask)) { 352fa7d9493SBabu Moger /* Give any dropped cpus to parent rdtgroup */ 353fa7d9493SBabu Moger cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask); 354fa7d9493SBabu Moger update_closid_rmid(tmpmask, prgrp); 355fa7d9493SBabu Moger } 356fa7d9493SBabu Moger 357fa7d9493SBabu Moger /* 358fa7d9493SBabu Moger * If we added cpus, remove them from previous group that owned them 359fa7d9493SBabu Moger * and update per-cpu rmid 360fa7d9493SBabu Moger */ 361fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); 362*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask)) { 363fa7d9493SBabu Moger head = &prgrp->mon.crdtgrp_list; 364fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 365fa7d9493SBabu Moger if (crgrp == rdtgrp) 366fa7d9493SBabu Moger continue; 367fa7d9493SBabu Moger cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask, 368fa7d9493SBabu Moger tmpmask); 369fa7d9493SBabu Moger } 370fa7d9493SBabu Moger update_closid_rmid(tmpmask, rdtgrp); 371fa7d9493SBabu Moger } 372fa7d9493SBabu Moger 373fa7d9493SBabu Moger /* Done pushing/pulling - update this group with new mask */ 374fa7d9493SBabu Moger cpumask_copy(&rdtgrp->cpu_mask, newmask); 375fa7d9493SBabu Moger 376fa7d9493SBabu Moger return 0; 377fa7d9493SBabu Moger } 378fa7d9493SBabu Moger 379fa7d9493SBabu Moger static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m) 380fa7d9493SBabu Moger { 381fa7d9493SBabu Moger struct rdtgroup *crgrp; 382fa7d9493SBabu Moger 383fa7d9493SBabu Moger cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m); 384fa7d9493SBabu Moger /* update the child mon group masks as well*/ 385fa7d9493SBabu Moger list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list) 386fa7d9493SBabu Moger cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask); 387fa7d9493SBabu Moger } 388fa7d9493SBabu Moger 389fa7d9493SBabu Moger static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, 390fa7d9493SBabu Moger cpumask_var_t tmpmask, cpumask_var_t tmpmask1) 391fa7d9493SBabu Moger { 392fa7d9493SBabu Moger struct rdtgroup *r, *crgrp; 393fa7d9493SBabu Moger struct list_head *head; 394fa7d9493SBabu Moger 395fa7d9493SBabu Moger /* Check whether cpus are dropped from this group */ 396fa7d9493SBabu Moger cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); 397*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask)) { 398fa7d9493SBabu Moger /* Can't drop from default group */ 399fa7d9493SBabu Moger if (rdtgrp == &rdtgroup_default) { 400fa7d9493SBabu Moger rdt_last_cmd_puts("Can't drop CPUs from default group\n"); 401fa7d9493SBabu Moger return -EINVAL; 402fa7d9493SBabu Moger } 403fa7d9493SBabu Moger 404fa7d9493SBabu Moger /* Give any dropped cpus to rdtgroup_default */ 405fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 406fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, tmpmask); 407fa7d9493SBabu Moger update_closid_rmid(tmpmask, &rdtgroup_default); 408fa7d9493SBabu Moger } 409fa7d9493SBabu Moger 410fa7d9493SBabu Moger /* 411fa7d9493SBabu Moger * If we added cpus, remove them from previous group and 412fa7d9493SBabu Moger * the prev group's child groups that owned them 413fa7d9493SBabu Moger * and update per-cpu closid/rmid. 414fa7d9493SBabu Moger */ 415fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); 416*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask)) { 417fa7d9493SBabu Moger list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) { 418fa7d9493SBabu Moger if (r == rdtgrp) 419fa7d9493SBabu Moger continue; 420fa7d9493SBabu Moger cpumask_and(tmpmask1, &r->cpu_mask, tmpmask); 421*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask1)) 422fa7d9493SBabu Moger cpumask_rdtgrp_clear(r, tmpmask1); 423fa7d9493SBabu Moger } 424fa7d9493SBabu Moger update_closid_rmid(tmpmask, rdtgrp); 425fa7d9493SBabu Moger } 426fa7d9493SBabu Moger 427fa7d9493SBabu Moger /* Done pushing/pulling - update this group with new mask */ 428fa7d9493SBabu Moger cpumask_copy(&rdtgrp->cpu_mask, newmask); 429fa7d9493SBabu Moger 430fa7d9493SBabu Moger /* 431fa7d9493SBabu Moger * Clear child mon group masks since there is a new parent mask 432fa7d9493SBabu Moger * now and update the rmid for the cpus the child lost. 433fa7d9493SBabu Moger */ 434fa7d9493SBabu Moger head = &rdtgrp->mon.crdtgrp_list; 435fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 436fa7d9493SBabu Moger cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask); 437fa7d9493SBabu Moger update_closid_rmid(tmpmask, rdtgrp); 438fa7d9493SBabu Moger cpumask_clear(&crgrp->cpu_mask); 439fa7d9493SBabu Moger } 440fa7d9493SBabu Moger 441fa7d9493SBabu Moger return 0; 442fa7d9493SBabu Moger } 443fa7d9493SBabu Moger 444fa7d9493SBabu Moger static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of, 445fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 446fa7d9493SBabu Moger { 447fa7d9493SBabu Moger cpumask_var_t tmpmask, newmask, tmpmask1; 448fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 449fa7d9493SBabu Moger int ret; 450fa7d9493SBabu Moger 451fa7d9493SBabu Moger if (!buf) 452fa7d9493SBabu Moger return -EINVAL; 453fa7d9493SBabu Moger 454fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 455fa7d9493SBabu Moger return -ENOMEM; 456fa7d9493SBabu Moger if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) { 457fa7d9493SBabu Moger free_cpumask_var(tmpmask); 458fa7d9493SBabu Moger return -ENOMEM; 459fa7d9493SBabu Moger } 460fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) { 461fa7d9493SBabu Moger free_cpumask_var(tmpmask); 462fa7d9493SBabu Moger free_cpumask_var(newmask); 463fa7d9493SBabu Moger return -ENOMEM; 464fa7d9493SBabu Moger } 465fa7d9493SBabu Moger 466fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 467fa7d9493SBabu Moger if (!rdtgrp) { 468fa7d9493SBabu Moger ret = -ENOENT; 469fa7d9493SBabu Moger goto unlock; 470fa7d9493SBabu Moger } 471fa7d9493SBabu Moger 472fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 473fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 474fa7d9493SBabu Moger ret = -EINVAL; 475723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 476fa7d9493SBabu Moger goto unlock; 477fa7d9493SBabu Moger } 478fa7d9493SBabu Moger 479fa7d9493SBabu Moger if (is_cpu_list(of)) 480fa7d9493SBabu Moger ret = cpulist_parse(buf, newmask); 481fa7d9493SBabu Moger else 482fa7d9493SBabu Moger ret = cpumask_parse(buf, newmask); 483fa7d9493SBabu Moger 484fa7d9493SBabu Moger if (ret) { 485723f1a0dSBabu Moger rdt_last_cmd_puts("Bad CPU list/mask\n"); 486fa7d9493SBabu Moger goto unlock; 487fa7d9493SBabu Moger } 488fa7d9493SBabu Moger 489fa7d9493SBabu Moger /* check that user didn't specify any offline cpus */ 490fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, cpu_online_mask); 491*3a5ff1f6SYury Norov if (!cpumask_empty(tmpmask)) { 492fa7d9493SBabu Moger ret = -EINVAL; 493723f1a0dSBabu Moger rdt_last_cmd_puts("Can only assign online CPUs\n"); 494fa7d9493SBabu Moger goto unlock; 495fa7d9493SBabu Moger } 496fa7d9493SBabu Moger 497fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP) 498fa7d9493SBabu Moger ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1); 499fa7d9493SBabu Moger else if (rdtgrp->type == RDTMON_GROUP) 500fa7d9493SBabu Moger ret = cpus_mon_write(rdtgrp, newmask, tmpmask); 501fa7d9493SBabu Moger else 502fa7d9493SBabu Moger ret = -EINVAL; 503fa7d9493SBabu Moger 504fa7d9493SBabu Moger unlock: 505fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 506fa7d9493SBabu Moger free_cpumask_var(tmpmask); 507fa7d9493SBabu Moger free_cpumask_var(newmask); 508fa7d9493SBabu Moger free_cpumask_var(tmpmask1); 509fa7d9493SBabu Moger 510fa7d9493SBabu Moger return ret ?: nbytes; 511fa7d9493SBabu Moger } 512fa7d9493SBabu Moger 51375899924SXiaochen Shen /** 51475899924SXiaochen Shen * rdtgroup_remove - the helper to remove resource group safely 51575899924SXiaochen Shen * @rdtgrp: resource group to remove 51675899924SXiaochen Shen * 51775899924SXiaochen Shen * On resource group creation via a mkdir, an extra kernfs_node reference is 51875899924SXiaochen Shen * taken to ensure that the rdtgroup structure remains accessible for the 51975899924SXiaochen Shen * rdtgroup_kn_unlock() calls where it is removed. 52075899924SXiaochen Shen * 52175899924SXiaochen Shen * Drop the extra reference here, then free the rdtgroup structure. 52275899924SXiaochen Shen * 52375899924SXiaochen Shen * Return: void 52475899924SXiaochen Shen */ 52575899924SXiaochen Shen static void rdtgroup_remove(struct rdtgroup *rdtgrp) 52675899924SXiaochen Shen { 52775899924SXiaochen Shen kernfs_put(rdtgrp->kn); 52875899924SXiaochen Shen kfree(rdtgrp); 52975899924SXiaochen Shen } 53075899924SXiaochen Shen 531ae28d1aaSFenghua Yu static void _update_task_closid_rmid(void *task) 532fa7d9493SBabu Moger { 533fa7d9493SBabu Moger /* 534ae28d1aaSFenghua Yu * If the task is still current on this CPU, update PQR_ASSOC MSR. 535ae28d1aaSFenghua Yu * Otherwise, the MSR is updated when the task is scheduled in. 536fa7d9493SBabu Moger */ 537ae28d1aaSFenghua Yu if (task == current) 538ae28d1aaSFenghua Yu resctrl_sched_in(); 539fa7d9493SBabu Moger } 540fa7d9493SBabu Moger 541ae28d1aaSFenghua Yu static void update_task_closid_rmid(struct task_struct *t) 542ae28d1aaSFenghua Yu { 543ae28d1aaSFenghua Yu if (IS_ENABLED(CONFIG_SMP) && task_curr(t)) 544ae28d1aaSFenghua Yu smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1); 545ae28d1aaSFenghua Yu else 546ae28d1aaSFenghua Yu _update_task_closid_rmid(t); 547fa7d9493SBabu Moger } 548fa7d9493SBabu Moger 549fa7d9493SBabu Moger static int __rdtgroup_move_task(struct task_struct *tsk, 550fa7d9493SBabu Moger struct rdtgroup *rdtgrp) 551fa7d9493SBabu Moger { 552a0195f31SFenghua Yu /* If the task is already in rdtgrp, no need to move the task. */ 553a0195f31SFenghua Yu if ((rdtgrp->type == RDTCTRL_GROUP && tsk->closid == rdtgrp->closid && 554a0195f31SFenghua Yu tsk->rmid == rdtgrp->mon.rmid) || 555a0195f31SFenghua Yu (rdtgrp->type == RDTMON_GROUP && tsk->rmid == rdtgrp->mon.rmid && 556a0195f31SFenghua Yu tsk->closid == rdtgrp->mon.parent->closid)) 557a0195f31SFenghua Yu return 0; 558a0195f31SFenghua Yu 559fa7d9493SBabu Moger /* 560ae28d1aaSFenghua Yu * Set the task's closid/rmid before the PQR_ASSOC MSR can be 561ae28d1aaSFenghua Yu * updated by them. 562ae28d1aaSFenghua Yu * 563ae28d1aaSFenghua Yu * For ctrl_mon groups, move both closid and rmid. 564fa7d9493SBabu Moger * For monitor groups, can move the tasks only from 565fa7d9493SBabu Moger * their parent CTRL group. 566fa7d9493SBabu Moger */ 567ae28d1aaSFenghua Yu 568fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP) { 5696d3b47ddSValentin Schneider WRITE_ONCE(tsk->closid, rdtgrp->closid); 5706d3b47ddSValentin Schneider WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid); 571fa7d9493SBabu Moger } else if (rdtgrp->type == RDTMON_GROUP) { 572fa7d9493SBabu Moger if (rdtgrp->mon.parent->closid == tsk->closid) { 5736d3b47ddSValentin Schneider WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid); 574fa7d9493SBabu Moger } else { 575fa7d9493SBabu Moger rdt_last_cmd_puts("Can't move task to different control group\n"); 576ae28d1aaSFenghua Yu return -EINVAL; 577fa7d9493SBabu Moger } 578fa7d9493SBabu Moger } 579ae28d1aaSFenghua Yu 580ae28d1aaSFenghua Yu /* 581ae28d1aaSFenghua Yu * Ensure the task's closid and rmid are written before determining if 582ae28d1aaSFenghua Yu * the task is current that will decide if it will be interrupted. 583ae28d1aaSFenghua Yu */ 584ae28d1aaSFenghua Yu barrier(); 585ae28d1aaSFenghua Yu 586ae28d1aaSFenghua Yu /* 587ae28d1aaSFenghua Yu * By now, the task's closid and rmid are set. If the task is current 588ae28d1aaSFenghua Yu * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource 589ae28d1aaSFenghua Yu * group go into effect. If the task is not current, the MSR will be 590ae28d1aaSFenghua Yu * updated when the task is scheduled in. 591ae28d1aaSFenghua Yu */ 592ae28d1aaSFenghua Yu update_task_closid_rmid(tsk); 593ae28d1aaSFenghua Yu 594ae28d1aaSFenghua Yu return 0; 595fa7d9493SBabu Moger } 596fa7d9493SBabu Moger 597e6b2fac3SJames Morse static bool is_closid_match(struct task_struct *t, struct rdtgroup *r) 598e6b2fac3SJames Morse { 599e6b2fac3SJames Morse return (rdt_alloc_capable && 600e6b2fac3SJames Morse (r->type == RDTCTRL_GROUP) && (t->closid == r->closid)); 601e6b2fac3SJames Morse } 602e6b2fac3SJames Morse 603e6b2fac3SJames Morse static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r) 604e6b2fac3SJames Morse { 605e6b2fac3SJames Morse return (rdt_mon_capable && 606e6b2fac3SJames Morse (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid)); 607e6b2fac3SJames Morse } 608e6b2fac3SJames Morse 609fa7d9493SBabu Moger /** 610fa7d9493SBabu Moger * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group 611fa7d9493SBabu Moger * @r: Resource group 612fa7d9493SBabu Moger * 613fa7d9493SBabu Moger * Return: 1 if tasks have been assigned to @r, 0 otherwise 614fa7d9493SBabu Moger */ 615fa7d9493SBabu Moger int rdtgroup_tasks_assigned(struct rdtgroup *r) 616fa7d9493SBabu Moger { 617fa7d9493SBabu Moger struct task_struct *p, *t; 618fa7d9493SBabu Moger int ret = 0; 619fa7d9493SBabu Moger 620fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 621fa7d9493SBabu Moger 622fa7d9493SBabu Moger rcu_read_lock(); 623fa7d9493SBabu Moger for_each_process_thread(p, t) { 624e6b2fac3SJames Morse if (is_closid_match(t, r) || is_rmid_match(t, r)) { 625fa7d9493SBabu Moger ret = 1; 626fa7d9493SBabu Moger break; 627fa7d9493SBabu Moger } 628fa7d9493SBabu Moger } 629fa7d9493SBabu Moger rcu_read_unlock(); 630fa7d9493SBabu Moger 631fa7d9493SBabu Moger return ret; 632fa7d9493SBabu Moger } 633fa7d9493SBabu Moger 634fa7d9493SBabu Moger static int rdtgroup_task_write_permission(struct task_struct *task, 635fa7d9493SBabu Moger struct kernfs_open_file *of) 636fa7d9493SBabu Moger { 637fa7d9493SBabu Moger const struct cred *tcred = get_task_cred(task); 638fa7d9493SBabu Moger const struct cred *cred = current_cred(); 639fa7d9493SBabu Moger int ret = 0; 640fa7d9493SBabu Moger 641fa7d9493SBabu Moger /* 642fa7d9493SBabu Moger * Even if we're attaching all tasks in the thread group, we only 643fa7d9493SBabu Moger * need to check permissions on one of them. 644fa7d9493SBabu Moger */ 645fa7d9493SBabu Moger if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && 646fa7d9493SBabu Moger !uid_eq(cred->euid, tcred->uid) && 647fa7d9493SBabu Moger !uid_eq(cred->euid, tcred->suid)) { 648fa7d9493SBabu Moger rdt_last_cmd_printf("No permission to move task %d\n", task->pid); 649fa7d9493SBabu Moger ret = -EPERM; 650fa7d9493SBabu Moger } 651fa7d9493SBabu Moger 652fa7d9493SBabu Moger put_cred(tcred); 653fa7d9493SBabu Moger return ret; 654fa7d9493SBabu Moger } 655fa7d9493SBabu Moger 656fa7d9493SBabu Moger static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp, 657fa7d9493SBabu Moger struct kernfs_open_file *of) 658fa7d9493SBabu Moger { 659fa7d9493SBabu Moger struct task_struct *tsk; 660fa7d9493SBabu Moger int ret; 661fa7d9493SBabu Moger 662fa7d9493SBabu Moger rcu_read_lock(); 663fa7d9493SBabu Moger if (pid) { 664fa7d9493SBabu Moger tsk = find_task_by_vpid(pid); 665fa7d9493SBabu Moger if (!tsk) { 666fa7d9493SBabu Moger rcu_read_unlock(); 667fa7d9493SBabu Moger rdt_last_cmd_printf("No task %d\n", pid); 668fa7d9493SBabu Moger return -ESRCH; 669fa7d9493SBabu Moger } 670fa7d9493SBabu Moger } else { 671fa7d9493SBabu Moger tsk = current; 672fa7d9493SBabu Moger } 673fa7d9493SBabu Moger 674fa7d9493SBabu Moger get_task_struct(tsk); 675fa7d9493SBabu Moger rcu_read_unlock(); 676fa7d9493SBabu Moger 677fa7d9493SBabu Moger ret = rdtgroup_task_write_permission(tsk, of); 678fa7d9493SBabu Moger if (!ret) 679fa7d9493SBabu Moger ret = __rdtgroup_move_task(tsk, rdtgrp); 680fa7d9493SBabu Moger 681fa7d9493SBabu Moger put_task_struct(tsk); 682fa7d9493SBabu Moger return ret; 683fa7d9493SBabu Moger } 684fa7d9493SBabu Moger 685fa7d9493SBabu Moger static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of, 686fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 687fa7d9493SBabu Moger { 688fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 689fa7d9493SBabu Moger int ret = 0; 690fa7d9493SBabu Moger pid_t pid; 691fa7d9493SBabu Moger 692fa7d9493SBabu Moger if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) 693fa7d9493SBabu Moger return -EINVAL; 694fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 695fa7d9493SBabu Moger if (!rdtgrp) { 696fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 697fa7d9493SBabu Moger return -ENOENT; 698fa7d9493SBabu Moger } 699fa7d9493SBabu Moger rdt_last_cmd_clear(); 700fa7d9493SBabu Moger 701fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 702fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 703fa7d9493SBabu Moger ret = -EINVAL; 704723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 705fa7d9493SBabu Moger goto unlock; 706fa7d9493SBabu Moger } 707fa7d9493SBabu Moger 708fa7d9493SBabu Moger ret = rdtgroup_move_task(pid, rdtgrp, of); 709fa7d9493SBabu Moger 710fa7d9493SBabu Moger unlock: 711fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 712fa7d9493SBabu Moger 713fa7d9493SBabu Moger return ret ?: nbytes; 714fa7d9493SBabu Moger } 715fa7d9493SBabu Moger 716fa7d9493SBabu Moger static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s) 717fa7d9493SBabu Moger { 718fa7d9493SBabu Moger struct task_struct *p, *t; 719fa7d9493SBabu Moger 720fa7d9493SBabu Moger rcu_read_lock(); 721fa7d9493SBabu Moger for_each_process_thread(p, t) { 722e6b2fac3SJames Morse if (is_closid_match(t, r) || is_rmid_match(t, r)) 723fa7d9493SBabu Moger seq_printf(s, "%d\n", t->pid); 724fa7d9493SBabu Moger } 725fa7d9493SBabu Moger rcu_read_unlock(); 726fa7d9493SBabu Moger } 727fa7d9493SBabu Moger 728fa7d9493SBabu Moger static int rdtgroup_tasks_show(struct kernfs_open_file *of, 729fa7d9493SBabu Moger struct seq_file *s, void *v) 730fa7d9493SBabu Moger { 731fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 732fa7d9493SBabu Moger int ret = 0; 733fa7d9493SBabu Moger 734fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 735fa7d9493SBabu Moger if (rdtgrp) 736fa7d9493SBabu Moger show_rdt_tasks(rdtgrp, s); 737fa7d9493SBabu Moger else 738fa7d9493SBabu Moger ret = -ENOENT; 739fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 740fa7d9493SBabu Moger 741fa7d9493SBabu Moger return ret; 742fa7d9493SBabu Moger } 743fa7d9493SBabu Moger 744e79f15a4SChen Yu #ifdef CONFIG_PROC_CPU_RESCTRL 745e79f15a4SChen Yu 746e79f15a4SChen Yu /* 747e79f15a4SChen Yu * A task can only be part of one resctrl control group and of one monitor 748e79f15a4SChen Yu * group which is associated to that control group. 749e79f15a4SChen Yu * 750e79f15a4SChen Yu * 1) res: 751e79f15a4SChen Yu * mon: 752e79f15a4SChen Yu * 753e79f15a4SChen Yu * resctrl is not available. 754e79f15a4SChen Yu * 755e79f15a4SChen Yu * 2) res:/ 756e79f15a4SChen Yu * mon: 757e79f15a4SChen Yu * 758e79f15a4SChen Yu * Task is part of the root resctrl control group, and it is not associated 759e79f15a4SChen Yu * to any monitor group. 760e79f15a4SChen Yu * 761e79f15a4SChen Yu * 3) res:/ 762e79f15a4SChen Yu * mon:mon0 763e79f15a4SChen Yu * 764e79f15a4SChen Yu * Task is part of the root resctrl control group and monitor group mon0. 765e79f15a4SChen Yu * 766e79f15a4SChen Yu * 4) res:group0 767e79f15a4SChen Yu * mon: 768e79f15a4SChen Yu * 769e79f15a4SChen Yu * Task is part of resctrl control group group0, and it is not associated 770e79f15a4SChen Yu * to any monitor group. 771e79f15a4SChen Yu * 772e79f15a4SChen Yu * 5) res:group0 773e79f15a4SChen Yu * mon:mon1 774e79f15a4SChen Yu * 775e79f15a4SChen Yu * Task is part of resctrl control group group0 and monitor group mon1. 776e79f15a4SChen Yu */ 777e79f15a4SChen Yu int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns, 778e79f15a4SChen Yu struct pid *pid, struct task_struct *tsk) 779e79f15a4SChen Yu { 780e79f15a4SChen Yu struct rdtgroup *rdtg; 781e79f15a4SChen Yu int ret = 0; 782e79f15a4SChen Yu 783e79f15a4SChen Yu mutex_lock(&rdtgroup_mutex); 784e79f15a4SChen Yu 785e79f15a4SChen Yu /* Return empty if resctrl has not been mounted. */ 786e79f15a4SChen Yu if (!static_branch_unlikely(&rdt_enable_key)) { 787e79f15a4SChen Yu seq_puts(s, "res:\nmon:\n"); 788e79f15a4SChen Yu goto unlock; 789e79f15a4SChen Yu } 790e79f15a4SChen Yu 791e79f15a4SChen Yu list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) { 792e79f15a4SChen Yu struct rdtgroup *crg; 793e79f15a4SChen Yu 794e79f15a4SChen Yu /* 795e79f15a4SChen Yu * Task information is only relevant for shareable 796e79f15a4SChen Yu * and exclusive groups. 797e79f15a4SChen Yu */ 798e79f15a4SChen Yu if (rdtg->mode != RDT_MODE_SHAREABLE && 799e79f15a4SChen Yu rdtg->mode != RDT_MODE_EXCLUSIVE) 800e79f15a4SChen Yu continue; 801e79f15a4SChen Yu 802e79f15a4SChen Yu if (rdtg->closid != tsk->closid) 803e79f15a4SChen Yu continue; 804e79f15a4SChen Yu 805e79f15a4SChen Yu seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "", 806e79f15a4SChen Yu rdtg->kn->name); 807e79f15a4SChen Yu seq_puts(s, "mon:"); 808e79f15a4SChen Yu list_for_each_entry(crg, &rdtg->mon.crdtgrp_list, 809e79f15a4SChen Yu mon.crdtgrp_list) { 810e79f15a4SChen Yu if (tsk->rmid != crg->mon.rmid) 811e79f15a4SChen Yu continue; 812e79f15a4SChen Yu seq_printf(s, "%s", crg->kn->name); 813e79f15a4SChen Yu break; 814e79f15a4SChen Yu } 815e79f15a4SChen Yu seq_putc(s, '\n'); 816e79f15a4SChen Yu goto unlock; 817e79f15a4SChen Yu } 818e79f15a4SChen Yu /* 819e79f15a4SChen Yu * The above search should succeed. Otherwise return 820e79f15a4SChen Yu * with an error. 821e79f15a4SChen Yu */ 822e79f15a4SChen Yu ret = -ENOENT; 823e79f15a4SChen Yu unlock: 824e79f15a4SChen Yu mutex_unlock(&rdtgroup_mutex); 825e79f15a4SChen Yu 826e79f15a4SChen Yu return ret; 827e79f15a4SChen Yu } 828e79f15a4SChen Yu #endif 829e79f15a4SChen Yu 830fa7d9493SBabu Moger static int rdt_last_cmd_status_show(struct kernfs_open_file *of, 831fa7d9493SBabu Moger struct seq_file *seq, void *v) 832fa7d9493SBabu Moger { 833fa7d9493SBabu Moger int len; 834fa7d9493SBabu Moger 835fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 836fa7d9493SBabu Moger len = seq_buf_used(&last_cmd_status); 837fa7d9493SBabu Moger if (len) 838fa7d9493SBabu Moger seq_printf(seq, "%.*s", len, last_cmd_status_buf); 839fa7d9493SBabu Moger else 840fa7d9493SBabu Moger seq_puts(seq, "ok\n"); 841fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 842fa7d9493SBabu Moger return 0; 843fa7d9493SBabu Moger } 844fa7d9493SBabu Moger 845fa7d9493SBabu Moger static int rdt_num_closids_show(struct kernfs_open_file *of, 846fa7d9493SBabu Moger struct seq_file *seq, void *v) 847fa7d9493SBabu Moger { 848f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 849fa7d9493SBabu Moger 8503183e87cSJames Morse seq_printf(seq, "%u\n", s->num_closid); 851fa7d9493SBabu Moger return 0; 852fa7d9493SBabu Moger } 853fa7d9493SBabu Moger 854fa7d9493SBabu Moger static int rdt_default_ctrl_show(struct kernfs_open_file *of, 855fa7d9493SBabu Moger struct seq_file *seq, void *v) 856fa7d9493SBabu Moger { 857f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 858f2594492SJames Morse struct rdt_resource *r = s->res; 859fa7d9493SBabu Moger 860fa7d9493SBabu Moger seq_printf(seq, "%x\n", r->default_ctrl); 861fa7d9493SBabu Moger return 0; 862fa7d9493SBabu Moger } 863fa7d9493SBabu Moger 864fa7d9493SBabu Moger static int rdt_min_cbm_bits_show(struct kernfs_open_file *of, 865fa7d9493SBabu Moger struct seq_file *seq, void *v) 866fa7d9493SBabu Moger { 867f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 868f2594492SJames Morse struct rdt_resource *r = s->res; 869fa7d9493SBabu Moger 870fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->cache.min_cbm_bits); 871fa7d9493SBabu Moger return 0; 872fa7d9493SBabu Moger } 873fa7d9493SBabu Moger 874fa7d9493SBabu Moger static int rdt_shareable_bits_show(struct kernfs_open_file *of, 875fa7d9493SBabu Moger struct seq_file *seq, void *v) 876fa7d9493SBabu Moger { 877f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 878f2594492SJames Morse struct rdt_resource *r = s->res; 879fa7d9493SBabu Moger 880fa7d9493SBabu Moger seq_printf(seq, "%x\n", r->cache.shareable_bits); 881fa7d9493SBabu Moger return 0; 882fa7d9493SBabu Moger } 883fa7d9493SBabu Moger 884fa7d9493SBabu Moger /** 885fa7d9493SBabu Moger * rdt_bit_usage_show - Display current usage of resources 886fa7d9493SBabu Moger * 887fa7d9493SBabu Moger * A domain is a shared resource that can now be allocated differently. Here 888fa7d9493SBabu Moger * we display the current regions of the domain as an annotated bitmask. 889fa7d9493SBabu Moger * For each domain of this resource its allocation bitmask 890fa7d9493SBabu Moger * is annotated as below to indicate the current usage of the corresponding bit: 891fa7d9493SBabu Moger * 0 - currently unused 892fa7d9493SBabu Moger * X - currently available for sharing and used by software and hardware 893fa7d9493SBabu Moger * H - currently used by hardware only but available for software use 894fa7d9493SBabu Moger * S - currently used and shareable by software only 895fa7d9493SBabu Moger * E - currently used exclusively by one resource group 896fa7d9493SBabu Moger * P - currently pseudo-locked by one resource group 897fa7d9493SBabu Moger */ 898fa7d9493SBabu Moger static int rdt_bit_usage_show(struct kernfs_open_file *of, 899fa7d9493SBabu Moger struct seq_file *seq, void *v) 900fa7d9493SBabu Moger { 901f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 90232f010deSReinette Chatre /* 90332f010deSReinette Chatre * Use unsigned long even though only 32 bits are used to ensure 90432f010deSReinette Chatre * test_bit() is used safely. 90532f010deSReinette Chatre */ 90632f010deSReinette Chatre unsigned long sw_shareable = 0, hw_shareable = 0; 90732f010deSReinette Chatre unsigned long exclusive = 0, pseudo_locked = 0; 908f2594492SJames Morse struct rdt_resource *r = s->res; 909fa7d9493SBabu Moger struct rdt_domain *dom; 910fa7d9493SBabu Moger int i, hwb, swb, excl, psl; 911fa7d9493SBabu Moger enum rdtgrp_mode mode; 912fa7d9493SBabu Moger bool sep = false; 913f07e9d02SJames Morse u32 ctrl_val; 914fa7d9493SBabu Moger 915fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 916fa7d9493SBabu Moger hw_shareable = r->cache.shareable_bits; 917fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) { 918fa7d9493SBabu Moger if (sep) 919fa7d9493SBabu Moger seq_putc(seq, ';'); 920fa7d9493SBabu Moger sw_shareable = 0; 921fa7d9493SBabu Moger exclusive = 0; 922fa7d9493SBabu Moger seq_printf(seq, "%d=", dom->id); 923f07e9d02SJames Morse for (i = 0; i < closids_supported(); i++) { 924fa7d9493SBabu Moger if (!closid_allocated(i)) 925fa7d9493SBabu Moger continue; 926111136e6SJames Morse ctrl_val = resctrl_arch_get_config(r, dom, i, 927111136e6SJames Morse s->conf_type); 928fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 929fa7d9493SBabu Moger switch (mode) { 930fa7d9493SBabu Moger case RDT_MODE_SHAREABLE: 931f07e9d02SJames Morse sw_shareable |= ctrl_val; 932fa7d9493SBabu Moger break; 933fa7d9493SBabu Moger case RDT_MODE_EXCLUSIVE: 934f07e9d02SJames Morse exclusive |= ctrl_val; 935fa7d9493SBabu Moger break; 936fa7d9493SBabu Moger case RDT_MODE_PSEUDO_LOCKSETUP: 937fa7d9493SBabu Moger /* 938fa7d9493SBabu Moger * RDT_MODE_PSEUDO_LOCKSETUP is possible 939fa7d9493SBabu Moger * here but not included since the CBM 940fa7d9493SBabu Moger * associated with this CLOSID in this mode 941fa7d9493SBabu Moger * is not initialized and no task or cpu can be 942fa7d9493SBabu Moger * assigned this CLOSID. 943fa7d9493SBabu Moger */ 944fa7d9493SBabu Moger break; 945fa7d9493SBabu Moger case RDT_MODE_PSEUDO_LOCKED: 946fa7d9493SBabu Moger case RDT_NUM_MODES: 947fa7d9493SBabu Moger WARN(1, 948fa7d9493SBabu Moger "invalid mode for closid %d\n", i); 949fa7d9493SBabu Moger break; 950fa7d9493SBabu Moger } 951fa7d9493SBabu Moger } 952fa7d9493SBabu Moger for (i = r->cache.cbm_len - 1; i >= 0; i--) { 953fa7d9493SBabu Moger pseudo_locked = dom->plr ? dom->plr->cbm : 0; 95432f010deSReinette Chatre hwb = test_bit(i, &hw_shareable); 95532f010deSReinette Chatre swb = test_bit(i, &sw_shareable); 95632f010deSReinette Chatre excl = test_bit(i, &exclusive); 95732f010deSReinette Chatre psl = test_bit(i, &pseudo_locked); 958fa7d9493SBabu Moger if (hwb && swb) 959fa7d9493SBabu Moger seq_putc(seq, 'X'); 960fa7d9493SBabu Moger else if (hwb && !swb) 961fa7d9493SBabu Moger seq_putc(seq, 'H'); 962fa7d9493SBabu Moger else if (!hwb && swb) 963fa7d9493SBabu Moger seq_putc(seq, 'S'); 964fa7d9493SBabu Moger else if (excl) 965fa7d9493SBabu Moger seq_putc(seq, 'E'); 966fa7d9493SBabu Moger else if (psl) 967fa7d9493SBabu Moger seq_putc(seq, 'P'); 968fa7d9493SBabu Moger else /* Unused bits remain */ 969fa7d9493SBabu Moger seq_putc(seq, '0'); 970fa7d9493SBabu Moger } 971fa7d9493SBabu Moger sep = true; 972fa7d9493SBabu Moger } 973fa7d9493SBabu Moger seq_putc(seq, '\n'); 974fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 975fa7d9493SBabu Moger return 0; 976fa7d9493SBabu Moger } 977fa7d9493SBabu Moger 978fa7d9493SBabu Moger static int rdt_min_bw_show(struct kernfs_open_file *of, 979fa7d9493SBabu Moger struct seq_file *seq, void *v) 980fa7d9493SBabu Moger { 981f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 982f2594492SJames Morse struct rdt_resource *r = s->res; 983fa7d9493SBabu Moger 984fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.min_bw); 985fa7d9493SBabu Moger return 0; 986fa7d9493SBabu Moger } 987fa7d9493SBabu Moger 988fa7d9493SBabu Moger static int rdt_num_rmids_show(struct kernfs_open_file *of, 989fa7d9493SBabu Moger struct seq_file *seq, void *v) 990fa7d9493SBabu Moger { 991fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 992fa7d9493SBabu Moger 993fa7d9493SBabu Moger seq_printf(seq, "%d\n", r->num_rmid); 994fa7d9493SBabu Moger 995fa7d9493SBabu Moger return 0; 996fa7d9493SBabu Moger } 997fa7d9493SBabu Moger 998fa7d9493SBabu Moger static int rdt_mon_features_show(struct kernfs_open_file *of, 999fa7d9493SBabu Moger struct seq_file *seq, void *v) 1000fa7d9493SBabu Moger { 1001fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 1002fa7d9493SBabu Moger struct mon_evt *mevt; 1003fa7d9493SBabu Moger 1004fa7d9493SBabu Moger list_for_each_entry(mevt, &r->evt_list, list) 1005fa7d9493SBabu Moger seq_printf(seq, "%s\n", mevt->name); 1006fa7d9493SBabu Moger 1007fa7d9493SBabu Moger return 0; 1008fa7d9493SBabu Moger } 1009fa7d9493SBabu Moger 1010fa7d9493SBabu Moger static int rdt_bw_gran_show(struct kernfs_open_file *of, 1011fa7d9493SBabu Moger struct seq_file *seq, void *v) 1012fa7d9493SBabu Moger { 1013f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 1014f2594492SJames Morse struct rdt_resource *r = s->res; 1015fa7d9493SBabu Moger 1016fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.bw_gran); 1017fa7d9493SBabu Moger return 0; 1018fa7d9493SBabu Moger } 1019fa7d9493SBabu Moger 1020fa7d9493SBabu Moger static int rdt_delay_linear_show(struct kernfs_open_file *of, 1021fa7d9493SBabu Moger struct seq_file *seq, void *v) 1022fa7d9493SBabu Moger { 1023f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 1024f2594492SJames Morse struct rdt_resource *r = s->res; 1025fa7d9493SBabu Moger 1026fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.delay_linear); 1027fa7d9493SBabu Moger return 0; 1028fa7d9493SBabu Moger } 1029fa7d9493SBabu Moger 1030fa7d9493SBabu Moger static int max_threshold_occ_show(struct kernfs_open_file *of, 1031fa7d9493SBabu Moger struct seq_file *seq, void *v) 1032fa7d9493SBabu Moger { 1033fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 103463c8b123SJames Morse struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r); 1035fa7d9493SBabu Moger 103663c8b123SJames Morse seq_printf(seq, "%u\n", resctrl_cqm_threshold * hw_res->mon_scale); 1037fa7d9493SBabu Moger 1038fa7d9493SBabu Moger return 0; 1039fa7d9493SBabu Moger } 1040fa7d9493SBabu Moger 104129b6bd41SFenghua Yu static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of, 104229b6bd41SFenghua Yu struct seq_file *seq, void *v) 104329b6bd41SFenghua Yu { 1044f2594492SJames Morse struct resctrl_schema *s = of->kn->parent->priv; 1045f2594492SJames Morse struct rdt_resource *r = s->res; 104629b6bd41SFenghua Yu 104729b6bd41SFenghua Yu if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD) 104829b6bd41SFenghua Yu seq_puts(seq, "per-thread\n"); 104929b6bd41SFenghua Yu else 105029b6bd41SFenghua Yu seq_puts(seq, "max\n"); 105129b6bd41SFenghua Yu 105229b6bd41SFenghua Yu return 0; 105329b6bd41SFenghua Yu } 105429b6bd41SFenghua Yu 1055fa7d9493SBabu Moger static ssize_t max_threshold_occ_write(struct kernfs_open_file *of, 1056fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 1057fa7d9493SBabu Moger { 105863c8b123SJames Morse struct rdt_hw_resource *hw_res; 1059fa7d9493SBabu Moger unsigned int bytes; 1060fa7d9493SBabu Moger int ret; 1061fa7d9493SBabu Moger 1062fa7d9493SBabu Moger ret = kstrtouint(buf, 0, &bytes); 1063fa7d9493SBabu Moger if (ret) 1064fa7d9493SBabu Moger return ret; 1065fa7d9493SBabu Moger 1066fa7d9493SBabu Moger if (bytes > (boot_cpu_data.x86_cache_size * 1024)) 1067fa7d9493SBabu Moger return -EINVAL; 1068fa7d9493SBabu Moger 106963c8b123SJames Morse hw_res = resctrl_to_arch_res(of->kn->parent->priv); 107063c8b123SJames Morse resctrl_cqm_threshold = bytes / hw_res->mon_scale; 1071fa7d9493SBabu Moger 1072fa7d9493SBabu Moger return nbytes; 1073fa7d9493SBabu Moger } 1074fa7d9493SBabu Moger 1075fa7d9493SBabu Moger /* 1076fa7d9493SBabu Moger * rdtgroup_mode_show - Display mode of this resource group 1077fa7d9493SBabu Moger */ 1078fa7d9493SBabu Moger static int rdtgroup_mode_show(struct kernfs_open_file *of, 1079fa7d9493SBabu Moger struct seq_file *s, void *v) 1080fa7d9493SBabu Moger { 1081fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1082fa7d9493SBabu Moger 1083fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1084fa7d9493SBabu Moger if (!rdtgrp) { 1085fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1086fa7d9493SBabu Moger return -ENOENT; 1087fa7d9493SBabu Moger } 1088fa7d9493SBabu Moger 1089fa7d9493SBabu Moger seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode)); 1090fa7d9493SBabu Moger 1091fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1092fa7d9493SBabu Moger return 0; 1093fa7d9493SBabu Moger } 1094fa7d9493SBabu Moger 1095fbc06c69SJames Morse static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type) 1096fa7d9493SBabu Moger { 1097fbc06c69SJames Morse switch (my_type) { 1098fbc06c69SJames Morse case CDP_CODE: 1099fbc06c69SJames Morse return CDP_DATA; 1100fbc06c69SJames Morse case CDP_DATA: 1101fbc06c69SJames Morse return CDP_CODE; 1102fa7d9493SBabu Moger default: 1103fbc06c69SJames Morse case CDP_NONE: 1104fbc06c69SJames Morse return CDP_NONE; 1105fa7d9493SBabu Moger } 1106fa7d9493SBabu Moger } 1107fa7d9493SBabu Moger 1108fa7d9493SBabu Moger /** 1109fa7d9493SBabu Moger * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other 1110fa7d9493SBabu Moger * @r: Resource to which domain instance @d belongs. 1111fa7d9493SBabu Moger * @d: The domain instance for which @closid is being tested. 1112fa7d9493SBabu Moger * @cbm: Capacity bitmask being tested. 1113fa7d9493SBabu Moger * @closid: Intended closid for @cbm. 1114fa7d9493SBabu Moger * @exclusive: Only check if overlaps with exclusive resource groups 1115fa7d9493SBabu Moger * 1116fa7d9493SBabu Moger * Checks if provided @cbm intended to be used for @closid on domain 1117fa7d9493SBabu Moger * @d overlaps with any other closids or other hardware usage associated 1118fa7d9493SBabu Moger * with this domain. If @exclusive is true then only overlaps with 1119fa7d9493SBabu Moger * resource groups in exclusive mode will be considered. If @exclusive 1120fa7d9493SBabu Moger * is false then overlaps with any resource group or hardware entities 1121fa7d9493SBabu Moger * will be considered. 1122fa7d9493SBabu Moger * 1123fa7d9493SBabu Moger * @cbm is unsigned long, even if only 32 bits are used, to make the 1124fa7d9493SBabu Moger * bitmap functions work correctly. 1125fa7d9493SBabu Moger * 1126fa7d9493SBabu Moger * Return: false if CBM does not overlap, true if it does. 1127fa7d9493SBabu Moger */ 1128fa7d9493SBabu Moger static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, 1129fa8f711dSJames Morse unsigned long cbm, int closid, 1130fa8f711dSJames Morse enum resctrl_conf_type type, bool exclusive) 1131fa7d9493SBabu Moger { 1132fa7d9493SBabu Moger enum rdtgrp_mode mode; 1133fa7d9493SBabu Moger unsigned long ctrl_b; 1134fa7d9493SBabu Moger int i; 1135fa7d9493SBabu Moger 1136fa7d9493SBabu Moger /* Check for any overlap with regions used by hardware directly */ 1137fa7d9493SBabu Moger if (!exclusive) { 1138fa7d9493SBabu Moger ctrl_b = r->cache.shareable_bits; 1139fa7d9493SBabu Moger if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) 1140fa7d9493SBabu Moger return true; 1141fa7d9493SBabu Moger } 1142fa7d9493SBabu Moger 1143fa7d9493SBabu Moger /* Check for overlap with other resource groups */ 1144f07e9d02SJames Morse for (i = 0; i < closids_supported(); i++) { 1145111136e6SJames Morse ctrl_b = resctrl_arch_get_config(r, d, i, type); 1146fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 1147fa7d9493SBabu Moger if (closid_allocated(i) && i != closid && 1148fa7d9493SBabu Moger mode != RDT_MODE_PSEUDO_LOCKSETUP) { 1149fa7d9493SBabu Moger if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) { 1150fa7d9493SBabu Moger if (exclusive) { 1151fa7d9493SBabu Moger if (mode == RDT_MODE_EXCLUSIVE) 1152fa7d9493SBabu Moger return true; 1153fa7d9493SBabu Moger continue; 1154fa7d9493SBabu Moger } 1155fa7d9493SBabu Moger return true; 1156fa7d9493SBabu Moger } 1157fa7d9493SBabu Moger } 1158fa7d9493SBabu Moger } 1159fa7d9493SBabu Moger 1160fa7d9493SBabu Moger return false; 1161fa7d9493SBabu Moger } 1162fa7d9493SBabu Moger 1163fa7d9493SBabu Moger /** 1164fa7d9493SBabu Moger * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware 11651c290682SJames Morse * @s: Schema for the resource to which domain instance @d belongs. 1166fa7d9493SBabu Moger * @d: The domain instance for which @closid is being tested. 1167fa7d9493SBabu Moger * @cbm: Capacity bitmask being tested. 1168fa7d9493SBabu Moger * @closid: Intended closid for @cbm. 1169fa7d9493SBabu Moger * @exclusive: Only check if overlaps with exclusive resource groups 1170fa7d9493SBabu Moger * 1171fa7d9493SBabu Moger * Resources that can be allocated using a CBM can use the CBM to control 1172fa7d9493SBabu Moger * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test 1173fa7d9493SBabu Moger * for overlap. Overlap test is not limited to the specific resource for 1174fa7d9493SBabu Moger * which the CBM is intended though - when dealing with CDP resources that 1175fa7d9493SBabu Moger * share the underlying hardware the overlap check should be performed on 1176fa7d9493SBabu Moger * the CDP resource sharing the hardware also. 1177fa7d9493SBabu Moger * 1178fa7d9493SBabu Moger * Refer to description of __rdtgroup_cbm_overlaps() for the details of the 1179fa7d9493SBabu Moger * overlap test. 1180fa7d9493SBabu Moger * 1181fa7d9493SBabu Moger * Return: true if CBM overlap detected, false if there is no overlap 1182fa7d9493SBabu Moger */ 11831c290682SJames Morse bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d, 1184fa7d9493SBabu Moger unsigned long cbm, int closid, bool exclusive) 1185fa7d9493SBabu Moger { 1186fbc06c69SJames Morse enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type); 11871c290682SJames Morse struct rdt_resource *r = s->res; 1188fa7d9493SBabu Moger 1189fa8f711dSJames Morse if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type, 1190fa8f711dSJames Morse exclusive)) 1191fa7d9493SBabu Moger return true; 1192fa7d9493SBabu Moger 1193fbc06c69SJames Morse if (!resctrl_arch_get_cdp_enabled(r->rid)) 1194fa7d9493SBabu Moger return false; 1195fbc06c69SJames Morse return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive); 1196fa7d9493SBabu Moger } 1197fa7d9493SBabu Moger 1198fa7d9493SBabu Moger /** 1199fa7d9493SBabu Moger * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive 1200fa7d9493SBabu Moger * 1201fa7d9493SBabu Moger * An exclusive resource group implies that there should be no sharing of 1202fa7d9493SBabu Moger * its allocated resources. At the time this group is considered to be 1203fa7d9493SBabu Moger * exclusive this test can determine if its current schemata supports this 1204fa7d9493SBabu Moger * setting by testing for overlap with all other resource groups. 1205fa7d9493SBabu Moger * 1206fa7d9493SBabu Moger * Return: true if resource group can be exclusive, false if there is overlap 1207fa7d9493SBabu Moger * with allocations of other resource groups and thus this resource group 1208fa7d9493SBabu Moger * cannot be exclusive. 1209fa7d9493SBabu Moger */ 1210fa7d9493SBabu Moger static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp) 1211fa7d9493SBabu Moger { 1212fa7d9493SBabu Moger int closid = rdtgrp->closid; 1213331ebe4cSJames Morse struct resctrl_schema *s; 1214fa7d9493SBabu Moger struct rdt_resource *r; 1215fa7d9493SBabu Moger bool has_cache = false; 1216fa7d9493SBabu Moger struct rdt_domain *d; 1217f07e9d02SJames Morse u32 ctrl; 1218fa7d9493SBabu Moger 1219331ebe4cSJames Morse list_for_each_entry(s, &resctrl_schema_all, list) { 1220331ebe4cSJames Morse r = s->res; 1221fa7d9493SBabu Moger if (r->rid == RDT_RESOURCE_MBA) 1222fa7d9493SBabu Moger continue; 1223fa7d9493SBabu Moger has_cache = true; 1224fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 1225111136e6SJames Morse ctrl = resctrl_arch_get_config(r, d, closid, 1226111136e6SJames Morse s->conf_type); 1227f07e9d02SJames Morse if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) { 1228723f1a0dSBabu Moger rdt_last_cmd_puts("Schemata overlaps\n"); 1229fa7d9493SBabu Moger return false; 1230fa7d9493SBabu Moger } 1231fa7d9493SBabu Moger } 1232fa7d9493SBabu Moger } 1233fa7d9493SBabu Moger 1234fa7d9493SBabu Moger if (!has_cache) { 1235723f1a0dSBabu Moger rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n"); 1236fa7d9493SBabu Moger return false; 1237fa7d9493SBabu Moger } 1238fa7d9493SBabu Moger 1239fa7d9493SBabu Moger return true; 1240fa7d9493SBabu Moger } 1241fa7d9493SBabu Moger 1242fa7d9493SBabu Moger /** 1243fa7d9493SBabu Moger * rdtgroup_mode_write - Modify the resource group's mode 1244fa7d9493SBabu Moger * 1245fa7d9493SBabu Moger */ 1246fa7d9493SBabu Moger static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, 1247fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 1248fa7d9493SBabu Moger { 1249fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1250fa7d9493SBabu Moger enum rdtgrp_mode mode; 1251fa7d9493SBabu Moger int ret = 0; 1252fa7d9493SBabu Moger 1253fa7d9493SBabu Moger /* Valid input requires a trailing newline */ 1254fa7d9493SBabu Moger if (nbytes == 0 || buf[nbytes - 1] != '\n') 1255fa7d9493SBabu Moger return -EINVAL; 1256fa7d9493SBabu Moger buf[nbytes - 1] = '\0'; 1257fa7d9493SBabu Moger 1258fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1259fa7d9493SBabu Moger if (!rdtgrp) { 1260fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1261fa7d9493SBabu Moger return -ENOENT; 1262fa7d9493SBabu Moger } 1263fa7d9493SBabu Moger 1264fa7d9493SBabu Moger rdt_last_cmd_clear(); 1265fa7d9493SBabu Moger 1266fa7d9493SBabu Moger mode = rdtgrp->mode; 1267fa7d9493SBabu Moger 1268fa7d9493SBabu Moger if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) || 1269fa7d9493SBabu Moger (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) || 1270fa7d9493SBabu Moger (!strcmp(buf, "pseudo-locksetup") && 1271fa7d9493SBabu Moger mode == RDT_MODE_PSEUDO_LOCKSETUP) || 1272fa7d9493SBabu Moger (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED)) 1273fa7d9493SBabu Moger goto out; 1274fa7d9493SBabu Moger 1275fa7d9493SBabu Moger if (mode == RDT_MODE_PSEUDO_LOCKED) { 127645682489SReinette Chatre rdt_last_cmd_puts("Cannot change pseudo-locked group\n"); 1277fa7d9493SBabu Moger ret = -EINVAL; 1278fa7d9493SBabu Moger goto out; 1279fa7d9493SBabu Moger } 1280fa7d9493SBabu Moger 1281fa7d9493SBabu Moger if (!strcmp(buf, "shareable")) { 1282fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1283fa7d9493SBabu Moger ret = rdtgroup_locksetup_exit(rdtgrp); 1284fa7d9493SBabu Moger if (ret) 1285fa7d9493SBabu Moger goto out; 1286fa7d9493SBabu Moger } 1287fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_SHAREABLE; 1288fa7d9493SBabu Moger } else if (!strcmp(buf, "exclusive")) { 1289fa7d9493SBabu Moger if (!rdtgroup_mode_test_exclusive(rdtgrp)) { 1290fa7d9493SBabu Moger ret = -EINVAL; 1291fa7d9493SBabu Moger goto out; 1292fa7d9493SBabu Moger } 1293fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1294fa7d9493SBabu Moger ret = rdtgroup_locksetup_exit(rdtgrp); 1295fa7d9493SBabu Moger if (ret) 1296fa7d9493SBabu Moger goto out; 1297fa7d9493SBabu Moger } 1298fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_EXCLUSIVE; 1299fa7d9493SBabu Moger } else if (!strcmp(buf, "pseudo-locksetup")) { 1300fa7d9493SBabu Moger ret = rdtgroup_locksetup_enter(rdtgrp); 1301fa7d9493SBabu Moger if (ret) 1302fa7d9493SBabu Moger goto out; 1303fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP; 1304fa7d9493SBabu Moger } else { 130545682489SReinette Chatre rdt_last_cmd_puts("Unknown or unsupported mode\n"); 1306fa7d9493SBabu Moger ret = -EINVAL; 1307fa7d9493SBabu Moger } 1308fa7d9493SBabu Moger 1309fa7d9493SBabu Moger out: 1310fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1311fa7d9493SBabu Moger return ret ?: nbytes; 1312fa7d9493SBabu Moger } 1313fa7d9493SBabu Moger 1314fa7d9493SBabu Moger /** 1315fa7d9493SBabu Moger * rdtgroup_cbm_to_size - Translate CBM to size in bytes 1316fa7d9493SBabu Moger * @r: RDT resource to which @d belongs. 1317fa7d9493SBabu Moger * @d: RDT domain instance. 1318fa7d9493SBabu Moger * @cbm: bitmask for which the size should be computed. 1319fa7d9493SBabu Moger * 1320fa7d9493SBabu Moger * The bitmask provided associated with the RDT domain instance @d will be 1321fa7d9493SBabu Moger * translated into how many bytes it represents. The size in bytes is 1322fa7d9493SBabu Moger * computed by first dividing the total cache size by the CBM length to 1323fa7d9493SBabu Moger * determine how many bytes each bit in the bitmask represents. The result 1324fa7d9493SBabu Moger * is multiplied with the number of bits set in the bitmask. 1325fa7d9493SBabu Moger * 1326fa7d9493SBabu Moger * @cbm is unsigned long, even if only 32 bits are used to make the 1327fa7d9493SBabu Moger * bitmap functions work correctly. 1328fa7d9493SBabu Moger */ 1329fa7d9493SBabu Moger unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, 1330fa7d9493SBabu Moger struct rdt_domain *d, unsigned long cbm) 1331fa7d9493SBabu Moger { 1332fa7d9493SBabu Moger struct cpu_cacheinfo *ci; 1333fa7d9493SBabu Moger unsigned int size = 0; 1334fa7d9493SBabu Moger int num_b, i; 1335fa7d9493SBabu Moger 1336fa7d9493SBabu Moger num_b = bitmap_weight(&cbm, r->cache.cbm_len); 1337fa7d9493SBabu Moger ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask)); 1338fa7d9493SBabu Moger for (i = 0; i < ci->num_leaves; i++) { 1339fa7d9493SBabu Moger if (ci->info_list[i].level == r->cache_level) { 1340fa7d9493SBabu Moger size = ci->info_list[i].size / r->cache.cbm_len * num_b; 1341fa7d9493SBabu Moger break; 1342fa7d9493SBabu Moger } 1343fa7d9493SBabu Moger } 1344fa7d9493SBabu Moger 1345fa7d9493SBabu Moger return size; 1346fa7d9493SBabu Moger } 1347fa7d9493SBabu Moger 1348fa7d9493SBabu Moger /** 1349fa7d9493SBabu Moger * rdtgroup_size_show - Display size in bytes of allocated regions 1350fa7d9493SBabu Moger * 1351fa7d9493SBabu Moger * The "size" file mirrors the layout of the "schemata" file, printing the 1352fa7d9493SBabu Moger * size in bytes of each region instead of the capacity bitmask. 1353fa7d9493SBabu Moger * 1354fa7d9493SBabu Moger */ 1355fa7d9493SBabu Moger static int rdtgroup_size_show(struct kernfs_open_file *of, 1356fa7d9493SBabu Moger struct seq_file *s, void *v) 1357fa7d9493SBabu Moger { 1358331ebe4cSJames Morse struct resctrl_schema *schema; 1359fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1360fa7d9493SBabu Moger struct rdt_resource *r; 1361fa7d9493SBabu Moger struct rdt_domain *d; 1362fa7d9493SBabu Moger unsigned int size; 1363fa7d9493SBabu Moger int ret = 0; 1364fa7d9493SBabu Moger bool sep; 1365fa7d9493SBabu Moger u32 ctrl; 1366fa7d9493SBabu Moger 1367fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1368fa7d9493SBabu Moger if (!rdtgrp) { 1369fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1370fa7d9493SBabu Moger return -ENOENT; 1371fa7d9493SBabu Moger } 1372fa7d9493SBabu Moger 1373fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 1374fa7d9493SBabu Moger if (!rdtgrp->plr->d) { 1375fa7d9493SBabu Moger rdt_last_cmd_clear(); 1376fa7d9493SBabu Moger rdt_last_cmd_puts("Cache domain offline\n"); 1377fa7d9493SBabu Moger ret = -ENODEV; 1378fa7d9493SBabu Moger } else { 1379fa7d9493SBabu Moger seq_printf(s, "%*s:", max_name_width, 1380e198fde3SJames Morse rdtgrp->plr->s->name); 138132150eddSJames Morse size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res, 1382fa7d9493SBabu Moger rdtgrp->plr->d, 1383fa7d9493SBabu Moger rdtgrp->plr->cbm); 1384fa7d9493SBabu Moger seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size); 1385fa7d9493SBabu Moger } 1386fa7d9493SBabu Moger goto out; 1387fa7d9493SBabu Moger } 1388fa7d9493SBabu Moger 1389331ebe4cSJames Morse list_for_each_entry(schema, &resctrl_schema_all, list) { 1390331ebe4cSJames Morse r = schema->res; 1391fa7d9493SBabu Moger sep = false; 1392e198fde3SJames Morse seq_printf(s, "%*s:", max_name_width, schema->name); 1393fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 1394fa7d9493SBabu Moger if (sep) 1395fa7d9493SBabu Moger seq_putc(s, ';'); 1396fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1397fa7d9493SBabu Moger size = 0; 1398fa7d9493SBabu Moger } else { 1399111136e6SJames Morse ctrl = resctrl_arch_get_config(r, d, 1400111136e6SJames Morse rdtgrp->closid, 1401111136e6SJames Morse schema->conf_type); 1402fa7d9493SBabu Moger if (r->rid == RDT_RESOURCE_MBA) 1403fa7d9493SBabu Moger size = ctrl; 1404fa7d9493SBabu Moger else 1405fa7d9493SBabu Moger size = rdtgroup_cbm_to_size(r, d, ctrl); 1406fa7d9493SBabu Moger } 1407fa7d9493SBabu Moger seq_printf(s, "%d=%u", d->id, size); 1408fa7d9493SBabu Moger sep = true; 1409fa7d9493SBabu Moger } 1410fa7d9493SBabu Moger seq_putc(s, '\n'); 1411fa7d9493SBabu Moger } 1412fa7d9493SBabu Moger 1413fa7d9493SBabu Moger out: 1414fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1415fa7d9493SBabu Moger 1416fa7d9493SBabu Moger return ret; 1417fa7d9493SBabu Moger } 1418fa7d9493SBabu Moger 1419fa7d9493SBabu Moger /* rdtgroup information files for one cache resource. */ 1420fa7d9493SBabu Moger static struct rftype res_common_files[] = { 1421fa7d9493SBabu Moger { 1422fa7d9493SBabu Moger .name = "last_cmd_status", 1423fa7d9493SBabu Moger .mode = 0444, 1424fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1425fa7d9493SBabu Moger .seq_show = rdt_last_cmd_status_show, 1426fa7d9493SBabu Moger .fflags = RF_TOP_INFO, 1427fa7d9493SBabu Moger }, 1428fa7d9493SBabu Moger { 1429fa7d9493SBabu Moger .name = "num_closids", 1430fa7d9493SBabu Moger .mode = 0444, 1431fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1432fa7d9493SBabu Moger .seq_show = rdt_num_closids_show, 1433fa7d9493SBabu Moger .fflags = RF_CTRL_INFO, 1434fa7d9493SBabu Moger }, 1435fa7d9493SBabu Moger { 1436fa7d9493SBabu Moger .name = "mon_features", 1437fa7d9493SBabu Moger .mode = 0444, 1438fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1439fa7d9493SBabu Moger .seq_show = rdt_mon_features_show, 1440fa7d9493SBabu Moger .fflags = RF_MON_INFO, 1441fa7d9493SBabu Moger }, 1442fa7d9493SBabu Moger { 1443fa7d9493SBabu Moger .name = "num_rmids", 1444fa7d9493SBabu Moger .mode = 0444, 1445fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1446fa7d9493SBabu Moger .seq_show = rdt_num_rmids_show, 1447fa7d9493SBabu Moger .fflags = RF_MON_INFO, 1448fa7d9493SBabu Moger }, 1449fa7d9493SBabu Moger { 1450fa7d9493SBabu Moger .name = "cbm_mask", 1451fa7d9493SBabu Moger .mode = 0444, 1452fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1453fa7d9493SBabu Moger .seq_show = rdt_default_ctrl_show, 1454fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1455fa7d9493SBabu Moger }, 1456fa7d9493SBabu Moger { 1457fa7d9493SBabu Moger .name = "min_cbm_bits", 1458fa7d9493SBabu Moger .mode = 0444, 1459fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1460fa7d9493SBabu Moger .seq_show = rdt_min_cbm_bits_show, 1461fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1462fa7d9493SBabu Moger }, 1463fa7d9493SBabu Moger { 1464fa7d9493SBabu Moger .name = "shareable_bits", 1465fa7d9493SBabu Moger .mode = 0444, 1466fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1467fa7d9493SBabu Moger .seq_show = rdt_shareable_bits_show, 1468fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1469fa7d9493SBabu Moger }, 1470fa7d9493SBabu Moger { 1471fa7d9493SBabu Moger .name = "bit_usage", 1472fa7d9493SBabu Moger .mode = 0444, 1473fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1474fa7d9493SBabu Moger .seq_show = rdt_bit_usage_show, 1475fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1476fa7d9493SBabu Moger }, 1477fa7d9493SBabu Moger { 1478fa7d9493SBabu Moger .name = "min_bandwidth", 1479fa7d9493SBabu Moger .mode = 0444, 1480fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1481fa7d9493SBabu Moger .seq_show = rdt_min_bw_show, 1482fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1483fa7d9493SBabu Moger }, 1484fa7d9493SBabu Moger { 1485fa7d9493SBabu Moger .name = "bandwidth_gran", 1486fa7d9493SBabu Moger .mode = 0444, 1487fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1488fa7d9493SBabu Moger .seq_show = rdt_bw_gran_show, 1489fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1490fa7d9493SBabu Moger }, 1491fa7d9493SBabu Moger { 1492fa7d9493SBabu Moger .name = "delay_linear", 1493fa7d9493SBabu Moger .mode = 0444, 1494fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1495fa7d9493SBabu Moger .seq_show = rdt_delay_linear_show, 1496fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1497fa7d9493SBabu Moger }, 149829b6bd41SFenghua Yu /* 149929b6bd41SFenghua Yu * Platform specific which (if any) capabilities are provided by 150029b6bd41SFenghua Yu * thread_throttle_mode. Defer "fflags" initialization to platform 150129b6bd41SFenghua Yu * discovery. 150229b6bd41SFenghua Yu */ 150329b6bd41SFenghua Yu { 150429b6bd41SFenghua Yu .name = "thread_throttle_mode", 150529b6bd41SFenghua Yu .mode = 0444, 150629b6bd41SFenghua Yu .kf_ops = &rdtgroup_kf_single_ops, 150729b6bd41SFenghua Yu .seq_show = rdt_thread_throttle_mode_show, 150829b6bd41SFenghua Yu }, 1509fa7d9493SBabu Moger { 1510fa7d9493SBabu Moger .name = "max_threshold_occupancy", 1511fa7d9493SBabu Moger .mode = 0644, 1512fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1513fa7d9493SBabu Moger .write = max_threshold_occ_write, 1514fa7d9493SBabu Moger .seq_show = max_threshold_occ_show, 1515fa7d9493SBabu Moger .fflags = RF_MON_INFO | RFTYPE_RES_CACHE, 1516fa7d9493SBabu Moger }, 1517fa7d9493SBabu Moger { 1518fa7d9493SBabu Moger .name = "cpus", 1519fa7d9493SBabu Moger .mode = 0644, 1520fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1521fa7d9493SBabu Moger .write = rdtgroup_cpus_write, 1522fa7d9493SBabu Moger .seq_show = rdtgroup_cpus_show, 1523fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1524fa7d9493SBabu Moger }, 1525fa7d9493SBabu Moger { 1526fa7d9493SBabu Moger .name = "cpus_list", 1527fa7d9493SBabu Moger .mode = 0644, 1528fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1529fa7d9493SBabu Moger .write = rdtgroup_cpus_write, 1530fa7d9493SBabu Moger .seq_show = rdtgroup_cpus_show, 1531fa7d9493SBabu Moger .flags = RFTYPE_FLAGS_CPUS_LIST, 1532fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1533fa7d9493SBabu Moger }, 1534fa7d9493SBabu Moger { 1535fa7d9493SBabu Moger .name = "tasks", 1536fa7d9493SBabu Moger .mode = 0644, 1537fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1538fa7d9493SBabu Moger .write = rdtgroup_tasks_write, 1539fa7d9493SBabu Moger .seq_show = rdtgroup_tasks_show, 1540fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1541fa7d9493SBabu Moger }, 1542fa7d9493SBabu Moger { 1543fa7d9493SBabu Moger .name = "schemata", 1544fa7d9493SBabu Moger .mode = 0644, 1545fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1546fa7d9493SBabu Moger .write = rdtgroup_schemata_write, 1547fa7d9493SBabu Moger .seq_show = rdtgroup_schemata_show, 1548fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1549fa7d9493SBabu Moger }, 1550fa7d9493SBabu Moger { 1551fa7d9493SBabu Moger .name = "mode", 1552fa7d9493SBabu Moger .mode = 0644, 1553fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1554fa7d9493SBabu Moger .write = rdtgroup_mode_write, 1555fa7d9493SBabu Moger .seq_show = rdtgroup_mode_show, 1556fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1557fa7d9493SBabu Moger }, 1558fa7d9493SBabu Moger { 1559fa7d9493SBabu Moger .name = "size", 1560fa7d9493SBabu Moger .mode = 0444, 1561fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1562fa7d9493SBabu Moger .seq_show = rdtgroup_size_show, 1563fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1564fa7d9493SBabu Moger }, 1565fa7d9493SBabu Moger 1566fa7d9493SBabu Moger }; 1567fa7d9493SBabu Moger 1568fa7d9493SBabu Moger static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags) 1569fa7d9493SBabu Moger { 1570fa7d9493SBabu Moger struct rftype *rfts, *rft; 1571fa7d9493SBabu Moger int ret, len; 1572fa7d9493SBabu Moger 1573fa7d9493SBabu Moger rfts = res_common_files; 1574fa7d9493SBabu Moger len = ARRAY_SIZE(res_common_files); 1575fa7d9493SBabu Moger 1576fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 1577fa7d9493SBabu Moger 1578fa7d9493SBabu Moger for (rft = rfts; rft < rfts + len; rft++) { 157929b6bd41SFenghua Yu if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) { 1580fa7d9493SBabu Moger ret = rdtgroup_add_file(kn, rft); 1581fa7d9493SBabu Moger if (ret) 1582fa7d9493SBabu Moger goto error; 1583fa7d9493SBabu Moger } 1584fa7d9493SBabu Moger } 1585fa7d9493SBabu Moger 1586fa7d9493SBabu Moger return 0; 1587fa7d9493SBabu Moger error: 1588fa7d9493SBabu Moger pr_warn("Failed to add %s, err=%d\n", rft->name, ret); 1589fa7d9493SBabu Moger while (--rft >= rfts) { 1590fa7d9493SBabu Moger if ((fflags & rft->fflags) == rft->fflags) 1591fa7d9493SBabu Moger kernfs_remove_by_name(kn, rft->name); 1592fa7d9493SBabu Moger } 1593fa7d9493SBabu Moger return ret; 1594fa7d9493SBabu Moger } 1595fa7d9493SBabu Moger 159629b6bd41SFenghua Yu static struct rftype *rdtgroup_get_rftype_by_name(const char *name) 159729b6bd41SFenghua Yu { 159829b6bd41SFenghua Yu struct rftype *rfts, *rft; 159929b6bd41SFenghua Yu int len; 160029b6bd41SFenghua Yu 160129b6bd41SFenghua Yu rfts = res_common_files; 160229b6bd41SFenghua Yu len = ARRAY_SIZE(res_common_files); 160329b6bd41SFenghua Yu 160429b6bd41SFenghua Yu for (rft = rfts; rft < rfts + len; rft++) { 160529b6bd41SFenghua Yu if (!strcmp(rft->name, name)) 160629b6bd41SFenghua Yu return rft; 160729b6bd41SFenghua Yu } 160829b6bd41SFenghua Yu 160929b6bd41SFenghua Yu return NULL; 161029b6bd41SFenghua Yu } 161129b6bd41SFenghua Yu 161229b6bd41SFenghua Yu void __init thread_throttle_mode_init(void) 161329b6bd41SFenghua Yu { 161429b6bd41SFenghua Yu struct rftype *rft; 161529b6bd41SFenghua Yu 161629b6bd41SFenghua Yu rft = rdtgroup_get_rftype_by_name("thread_throttle_mode"); 161729b6bd41SFenghua Yu if (!rft) 161829b6bd41SFenghua Yu return; 161929b6bd41SFenghua Yu 162029b6bd41SFenghua Yu rft->fflags = RF_CTRL_INFO | RFTYPE_RES_MB; 162129b6bd41SFenghua Yu } 162229b6bd41SFenghua Yu 1623fa7d9493SBabu Moger /** 1624fa7d9493SBabu Moger * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file 1625fa7d9493SBabu Moger * @r: The resource group with which the file is associated. 1626fa7d9493SBabu Moger * @name: Name of the file 1627fa7d9493SBabu Moger * 1628fa7d9493SBabu Moger * The permissions of named resctrl file, directory, or link are modified 1629fa7d9493SBabu Moger * to not allow read, write, or execute by any user. 1630fa7d9493SBabu Moger * 1631fa7d9493SBabu Moger * WARNING: This function is intended to communicate to the user that the 1632fa7d9493SBabu Moger * resctrl file has been locked down - that it is not relevant to the 1633fa7d9493SBabu Moger * particular state the system finds itself in. It should not be relied 1634fa7d9493SBabu Moger * on to protect from user access because after the file's permissions 1635fa7d9493SBabu Moger * are restricted the user can still change the permissions using chmod 1636fa7d9493SBabu Moger * from the command line. 1637fa7d9493SBabu Moger * 1638fa7d9493SBabu Moger * Return: 0 on success, <0 on failure. 1639fa7d9493SBabu Moger */ 1640fa7d9493SBabu Moger int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name) 1641fa7d9493SBabu Moger { 1642fa7d9493SBabu Moger struct iattr iattr = {.ia_valid = ATTR_MODE,}; 1643fa7d9493SBabu Moger struct kernfs_node *kn; 1644fa7d9493SBabu Moger int ret = 0; 1645fa7d9493SBabu Moger 1646fa7d9493SBabu Moger kn = kernfs_find_and_get_ns(r->kn, name, NULL); 1647fa7d9493SBabu Moger if (!kn) 1648fa7d9493SBabu Moger return -ENOENT; 1649fa7d9493SBabu Moger 1650fa7d9493SBabu Moger switch (kernfs_type(kn)) { 1651fa7d9493SBabu Moger case KERNFS_DIR: 1652fa7d9493SBabu Moger iattr.ia_mode = S_IFDIR; 1653fa7d9493SBabu Moger break; 1654fa7d9493SBabu Moger case KERNFS_FILE: 1655fa7d9493SBabu Moger iattr.ia_mode = S_IFREG; 1656fa7d9493SBabu Moger break; 1657fa7d9493SBabu Moger case KERNFS_LINK: 1658fa7d9493SBabu Moger iattr.ia_mode = S_IFLNK; 1659fa7d9493SBabu Moger break; 1660fa7d9493SBabu Moger } 1661fa7d9493SBabu Moger 1662fa7d9493SBabu Moger ret = kernfs_setattr(kn, &iattr); 1663fa7d9493SBabu Moger kernfs_put(kn); 1664fa7d9493SBabu Moger return ret; 1665fa7d9493SBabu Moger } 1666fa7d9493SBabu Moger 1667fa7d9493SBabu Moger /** 1668fa7d9493SBabu Moger * rdtgroup_kn_mode_restore - Restore user access to named resctrl file 1669fa7d9493SBabu Moger * @r: The resource group with which the file is associated. 1670fa7d9493SBabu Moger * @name: Name of the file 1671fa7d9493SBabu Moger * @mask: Mask of permissions that should be restored 1672fa7d9493SBabu Moger * 1673fa7d9493SBabu Moger * Restore the permissions of the named file. If @name is a directory the 1674fa7d9493SBabu Moger * permissions of its parent will be used. 1675fa7d9493SBabu Moger * 1676fa7d9493SBabu Moger * Return: 0 on success, <0 on failure. 1677fa7d9493SBabu Moger */ 1678fa7d9493SBabu Moger int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name, 1679fa7d9493SBabu Moger umode_t mask) 1680fa7d9493SBabu Moger { 1681fa7d9493SBabu Moger struct iattr iattr = {.ia_valid = ATTR_MODE,}; 1682fa7d9493SBabu Moger struct kernfs_node *kn, *parent; 1683fa7d9493SBabu Moger struct rftype *rfts, *rft; 1684fa7d9493SBabu Moger int ret, len; 1685fa7d9493SBabu Moger 1686fa7d9493SBabu Moger rfts = res_common_files; 1687fa7d9493SBabu Moger len = ARRAY_SIZE(res_common_files); 1688fa7d9493SBabu Moger 1689fa7d9493SBabu Moger for (rft = rfts; rft < rfts + len; rft++) { 1690fa7d9493SBabu Moger if (!strcmp(rft->name, name)) 1691fa7d9493SBabu Moger iattr.ia_mode = rft->mode & mask; 1692fa7d9493SBabu Moger } 1693fa7d9493SBabu Moger 1694fa7d9493SBabu Moger kn = kernfs_find_and_get_ns(r->kn, name, NULL); 1695fa7d9493SBabu Moger if (!kn) 1696fa7d9493SBabu Moger return -ENOENT; 1697fa7d9493SBabu Moger 1698fa7d9493SBabu Moger switch (kernfs_type(kn)) { 1699fa7d9493SBabu Moger case KERNFS_DIR: 1700fa7d9493SBabu Moger parent = kernfs_get_parent(kn); 1701fa7d9493SBabu Moger if (parent) { 1702fa7d9493SBabu Moger iattr.ia_mode |= parent->mode; 1703fa7d9493SBabu Moger kernfs_put(parent); 1704fa7d9493SBabu Moger } 1705fa7d9493SBabu Moger iattr.ia_mode |= S_IFDIR; 1706fa7d9493SBabu Moger break; 1707fa7d9493SBabu Moger case KERNFS_FILE: 1708fa7d9493SBabu Moger iattr.ia_mode |= S_IFREG; 1709fa7d9493SBabu Moger break; 1710fa7d9493SBabu Moger case KERNFS_LINK: 1711fa7d9493SBabu Moger iattr.ia_mode |= S_IFLNK; 1712fa7d9493SBabu Moger break; 1713fa7d9493SBabu Moger } 1714fa7d9493SBabu Moger 1715fa7d9493SBabu Moger ret = kernfs_setattr(kn, &iattr); 1716fa7d9493SBabu Moger kernfs_put(kn); 1717fa7d9493SBabu Moger return ret; 1718fa7d9493SBabu Moger } 1719fa7d9493SBabu Moger 1720f2594492SJames Morse static int rdtgroup_mkdir_info_resdir(void *priv, char *name, 1721fa7d9493SBabu Moger unsigned long fflags) 1722fa7d9493SBabu Moger { 1723fa7d9493SBabu Moger struct kernfs_node *kn_subdir; 1724fa7d9493SBabu Moger int ret; 1725fa7d9493SBabu Moger 1726fa7d9493SBabu Moger kn_subdir = kernfs_create_dir(kn_info, name, 1727f2594492SJames Morse kn_info->mode, priv); 1728fa7d9493SBabu Moger if (IS_ERR(kn_subdir)) 1729fa7d9493SBabu Moger return PTR_ERR(kn_subdir); 1730fa7d9493SBabu Moger 1731fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn_subdir); 1732fa7d9493SBabu Moger if (ret) 1733fa7d9493SBabu Moger return ret; 1734fa7d9493SBabu Moger 1735fa7d9493SBabu Moger ret = rdtgroup_add_files(kn_subdir, fflags); 1736fa7d9493SBabu Moger if (!ret) 1737fa7d9493SBabu Moger kernfs_activate(kn_subdir); 1738fa7d9493SBabu Moger 1739fa7d9493SBabu Moger return ret; 1740fa7d9493SBabu Moger } 1741fa7d9493SBabu Moger 1742fa7d9493SBabu Moger static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn) 1743fa7d9493SBabu Moger { 1744f2594492SJames Morse struct resctrl_schema *s; 1745fa7d9493SBabu Moger struct rdt_resource *r; 1746fa7d9493SBabu Moger unsigned long fflags; 1747fa7d9493SBabu Moger char name[32]; 1748fa7d9493SBabu Moger int ret; 1749fa7d9493SBabu Moger 1750fa7d9493SBabu Moger /* create the directory */ 1751fa7d9493SBabu Moger kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL); 1752fa7d9493SBabu Moger if (IS_ERR(kn_info)) 1753fa7d9493SBabu Moger return PTR_ERR(kn_info); 1754fa7d9493SBabu Moger 1755fa7d9493SBabu Moger ret = rdtgroup_add_files(kn_info, RF_TOP_INFO); 1756fa7d9493SBabu Moger if (ret) 1757fa7d9493SBabu Moger goto out_destroy; 1758fa7d9493SBabu Moger 1759f2594492SJames Morse /* loop over enabled controls, these are all alloc_enabled */ 1760f2594492SJames Morse list_for_each_entry(s, &resctrl_schema_all, list) { 1761f2594492SJames Morse r = s->res; 1762fa7d9493SBabu Moger fflags = r->fflags | RF_CTRL_INFO; 1763e198fde3SJames Morse ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags); 1764fa7d9493SBabu Moger if (ret) 1765fa7d9493SBabu Moger goto out_destroy; 1766fa7d9493SBabu Moger } 1767fa7d9493SBabu Moger 1768fa7d9493SBabu Moger for_each_mon_enabled_rdt_resource(r) { 1769fa7d9493SBabu Moger fflags = r->fflags | RF_MON_INFO; 1770fa7d9493SBabu Moger sprintf(name, "%s_MON", r->name); 1771fa7d9493SBabu Moger ret = rdtgroup_mkdir_info_resdir(r, name, fflags); 1772fa7d9493SBabu Moger if (ret) 1773fa7d9493SBabu Moger goto out_destroy; 1774fa7d9493SBabu Moger } 1775fa7d9493SBabu Moger 1776fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn_info); 1777fa7d9493SBabu Moger if (ret) 1778fa7d9493SBabu Moger goto out_destroy; 1779fa7d9493SBabu Moger 1780fa7d9493SBabu Moger kernfs_activate(kn_info); 1781fa7d9493SBabu Moger 1782fa7d9493SBabu Moger return 0; 1783fa7d9493SBabu Moger 1784fa7d9493SBabu Moger out_destroy: 1785fa7d9493SBabu Moger kernfs_remove(kn_info); 1786fa7d9493SBabu Moger return ret; 1787fa7d9493SBabu Moger } 1788fa7d9493SBabu Moger 1789fa7d9493SBabu Moger static int 1790fa7d9493SBabu Moger mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp, 1791fa7d9493SBabu Moger char *name, struct kernfs_node **dest_kn) 1792fa7d9493SBabu Moger { 1793fa7d9493SBabu Moger struct kernfs_node *kn; 1794fa7d9493SBabu Moger int ret; 1795fa7d9493SBabu Moger 1796fa7d9493SBabu Moger /* create the directory */ 1797fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 1798fa7d9493SBabu Moger if (IS_ERR(kn)) 1799fa7d9493SBabu Moger return PTR_ERR(kn); 1800fa7d9493SBabu Moger 1801fa7d9493SBabu Moger if (dest_kn) 1802fa7d9493SBabu Moger *dest_kn = kn; 1803fa7d9493SBabu Moger 1804fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 1805fa7d9493SBabu Moger if (ret) 1806fa7d9493SBabu Moger goto out_destroy; 1807fa7d9493SBabu Moger 1808fa7d9493SBabu Moger kernfs_activate(kn); 1809fa7d9493SBabu Moger 1810fa7d9493SBabu Moger return 0; 1811fa7d9493SBabu Moger 1812fa7d9493SBabu Moger out_destroy: 1813fa7d9493SBabu Moger kernfs_remove(kn); 1814fa7d9493SBabu Moger return ret; 1815fa7d9493SBabu Moger } 1816fa7d9493SBabu Moger 1817fa7d9493SBabu Moger static void l3_qos_cfg_update(void *arg) 1818fa7d9493SBabu Moger { 1819fa7d9493SBabu Moger bool *enable = arg; 1820fa7d9493SBabu Moger 1821aa50453aSBabu Moger wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL); 1822fa7d9493SBabu Moger } 1823fa7d9493SBabu Moger 1824fa7d9493SBabu Moger static void l2_qos_cfg_update(void *arg) 1825fa7d9493SBabu Moger { 1826fa7d9493SBabu Moger bool *enable = arg; 1827fa7d9493SBabu Moger 1828aa50453aSBabu Moger wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL); 1829fa7d9493SBabu Moger } 1830fa7d9493SBabu Moger 1831fa7d9493SBabu Moger static inline bool is_mba_linear(void) 1832fa7d9493SBabu Moger { 183363c8b123SJames Morse return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear; 1834fa7d9493SBabu Moger } 1835fa7d9493SBabu Moger 1836fa7d9493SBabu Moger static int set_cache_qos_cfg(int level, bool enable) 1837fa7d9493SBabu Moger { 1838fa7d9493SBabu Moger void (*update)(void *arg); 1839fa7d9493SBabu Moger struct rdt_resource *r_l; 1840fa7d9493SBabu Moger cpumask_var_t cpu_mask; 1841fa7d9493SBabu Moger struct rdt_domain *d; 1842fa7d9493SBabu Moger int cpu; 1843fa7d9493SBabu Moger 1844fa7d9493SBabu Moger if (level == RDT_RESOURCE_L3) 1845fa7d9493SBabu Moger update = l3_qos_cfg_update; 1846fa7d9493SBabu Moger else if (level == RDT_RESOURCE_L2) 1847fa7d9493SBabu Moger update = l2_qos_cfg_update; 1848fa7d9493SBabu Moger else 1849fa7d9493SBabu Moger return -EINVAL; 1850fa7d9493SBabu Moger 1851ab6a2114SShakeel Butt if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) 1852ab6a2114SShakeel Butt return -ENOMEM; 1853ab6a2114SShakeel Butt 185463c8b123SJames Morse r_l = &rdt_resources_all[level].r_resctrl; 1855fa7d9493SBabu Moger list_for_each_entry(d, &r_l->domains, list) { 1856fae3a13dSBabu Moger if (r_l->cache.arch_has_per_cpu_cfg) 1857fae3a13dSBabu Moger /* Pick all the CPUs in the domain instance */ 1858fae3a13dSBabu Moger for_each_cpu(cpu, &d->cpu_mask) 1859fae3a13dSBabu Moger cpumask_set_cpu(cpu, cpu_mask); 1860fae3a13dSBabu Moger else 1861fa7d9493SBabu Moger /* Pick one CPU from each domain instance to update MSR */ 1862fa7d9493SBabu Moger cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); 1863fa7d9493SBabu Moger } 1864fa7d9493SBabu Moger cpu = get_cpu(); 1865fa7d9493SBabu Moger /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */ 1866fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 1867fa7d9493SBabu Moger update(&enable); 1868fa7d9493SBabu Moger /* Update QOS_CFG MSR on all other cpus in cpu_mask. */ 1869fa7d9493SBabu Moger smp_call_function_many(cpu_mask, update, &enable, 1); 1870fa7d9493SBabu Moger put_cpu(); 1871fa7d9493SBabu Moger 1872fa7d9493SBabu Moger free_cpumask_var(cpu_mask); 1873fa7d9493SBabu Moger 1874fa7d9493SBabu Moger return 0; 1875fa7d9493SBabu Moger } 1876fa7d9493SBabu Moger 18779fe04507SJames Morse /* Restore the qos cfg state when a domain comes online */ 18789fe04507SJames Morse void rdt_domain_reconfigure_cdp(struct rdt_resource *r) 18799fe04507SJames Morse { 1880c091e907SJames Morse struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r); 1881c091e907SJames Morse 1882c091e907SJames Morse if (!r->cdp_capable) 18839fe04507SJames Morse return; 18849fe04507SJames Morse 18855c3b63cdSJames Morse if (r->rid == RDT_RESOURCE_L2) 1886c091e907SJames Morse l2_qos_cfg_update(&hw_res->cdp_enabled); 18879fe04507SJames Morse 18885c3b63cdSJames Morse if (r->rid == RDT_RESOURCE_L3) 1889c091e907SJames Morse l3_qos_cfg_update(&hw_res->cdp_enabled); 18909fe04507SJames Morse } 18919fe04507SJames Morse 1892fa7d9493SBabu Moger /* 1893fa7d9493SBabu Moger * Enable or disable the MBA software controller 1894fa7d9493SBabu Moger * which helps user specify bandwidth in MBps. 1895fa7d9493SBabu Moger * MBA software controller is supported only if 1896fa7d9493SBabu Moger * MBM is supported and MBA is in linear scale. 1897fa7d9493SBabu Moger */ 1898fa7d9493SBabu Moger static int set_mba_sc(bool mba_sc) 1899fa7d9493SBabu Moger { 190063c8b123SJames Morse struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl; 1901792e0f6fSJames Morse struct rdt_hw_domain *hw_dom; 1902fa7d9493SBabu Moger struct rdt_domain *d; 1903fa7d9493SBabu Moger 1904fa7d9493SBabu Moger if (!is_mbm_enabled() || !is_mba_linear() || 1905fa7d9493SBabu Moger mba_sc == is_mba_sc(r)) 1906fa7d9493SBabu Moger return -EINVAL; 1907fa7d9493SBabu Moger 1908fa7d9493SBabu Moger r->membw.mba_sc = mba_sc; 1909792e0f6fSJames Morse list_for_each_entry(d, &r->domains, list) { 1910792e0f6fSJames Morse hw_dom = resctrl_to_arch_dom(d); 1911792e0f6fSJames Morse setup_default_ctrlval(r, hw_dom->ctrl_val, hw_dom->mbps_val); 1912792e0f6fSJames Morse } 1913fa7d9493SBabu Moger 1914fa7d9493SBabu Moger return 0; 1915fa7d9493SBabu Moger } 1916fa7d9493SBabu Moger 19175c3b63cdSJames Morse static int cdp_enable(int level) 1918fa7d9493SBabu Moger { 191963c8b123SJames Morse struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl; 1920fa7d9493SBabu Moger int ret; 1921fa7d9493SBabu Moger 19225c3b63cdSJames Morse if (!r_l->alloc_capable) 1923fa7d9493SBabu Moger return -EINVAL; 1924fa7d9493SBabu Moger 1925fa7d9493SBabu Moger ret = set_cache_qos_cfg(level, true); 19265c3b63cdSJames Morse if (!ret) 1927c091e907SJames Morse rdt_resources_all[level].cdp_enabled = true; 19285c3b63cdSJames Morse 1929fa7d9493SBabu Moger return ret; 1930fa7d9493SBabu Moger } 1931fa7d9493SBabu Moger 19325c3b63cdSJames Morse static void cdp_disable(int level) 1933fa7d9493SBabu Moger { 1934c091e907SJames Morse struct rdt_hw_resource *r_hw = &rdt_resources_all[level]; 1935fa7d9493SBabu Moger 1936c091e907SJames Morse if (r_hw->cdp_enabled) { 1937fa7d9493SBabu Moger set_cache_qos_cfg(level, false); 1938c091e907SJames Morse r_hw->cdp_enabled = false; 1939fa7d9493SBabu Moger } 1940fa7d9493SBabu Moger } 1941fa7d9493SBabu Moger 1942c091e907SJames Morse int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable) 1943fa7d9493SBabu Moger { 1944c091e907SJames Morse struct rdt_hw_resource *hw_res = &rdt_resources_all[l]; 1945c091e907SJames Morse 1946c091e907SJames Morse if (!hw_res->r_resctrl.cdp_capable) 1947c091e907SJames Morse return -EINVAL; 1948c091e907SJames Morse 1949c091e907SJames Morse if (enable) 19505c3b63cdSJames Morse return cdp_enable(l); 1951c091e907SJames Morse 19525c3b63cdSJames Morse cdp_disable(l); 1953c091e907SJames Morse 1954c091e907SJames Morse return 0; 1955fa7d9493SBabu Moger } 1956fa7d9493SBabu Moger 1957fa7d9493SBabu Moger static void cdp_disable_all(void) 1958fa7d9493SBabu Moger { 1959c091e907SJames Morse if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3)) 1960c091e907SJames Morse resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false); 1961c091e907SJames Morse if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2)) 1962c091e907SJames Morse resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false); 1963fa7d9493SBabu Moger } 1964fa7d9493SBabu Moger 1965fa7d9493SBabu Moger /* 1966fa7d9493SBabu Moger * We don't allow rdtgroup directories to be created anywhere 1967fa7d9493SBabu Moger * except the root directory. Thus when looking for the rdtgroup 1968fa7d9493SBabu Moger * structure for a kernfs node we are either looking at a directory, 1969fa7d9493SBabu Moger * in which case the rdtgroup structure is pointed at by the "priv" 1970fa7d9493SBabu Moger * field, otherwise we have a file, and need only look to the parent 1971fa7d9493SBabu Moger * to find the rdtgroup. 1972fa7d9493SBabu Moger */ 1973fa7d9493SBabu Moger static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn) 1974fa7d9493SBabu Moger { 1975fa7d9493SBabu Moger if (kernfs_type(kn) == KERNFS_DIR) { 1976fa7d9493SBabu Moger /* 1977fa7d9493SBabu Moger * All the resource directories use "kn->priv" 1978fa7d9493SBabu Moger * to point to the "struct rdtgroup" for the 1979fa7d9493SBabu Moger * resource. "info" and its subdirectories don't 1980fa7d9493SBabu Moger * have rdtgroup structures, so return NULL here. 1981fa7d9493SBabu Moger */ 1982fa7d9493SBabu Moger if (kn == kn_info || kn->parent == kn_info) 1983fa7d9493SBabu Moger return NULL; 1984fa7d9493SBabu Moger else 1985fa7d9493SBabu Moger return kn->priv; 1986fa7d9493SBabu Moger } else { 1987fa7d9493SBabu Moger return kn->parent->priv; 1988fa7d9493SBabu Moger } 1989fa7d9493SBabu Moger } 1990fa7d9493SBabu Moger 1991fa7d9493SBabu Moger struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn) 1992fa7d9493SBabu Moger { 1993fa7d9493SBabu Moger struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 1994fa7d9493SBabu Moger 1995fa7d9493SBabu Moger if (!rdtgrp) 1996fa7d9493SBabu Moger return NULL; 1997fa7d9493SBabu Moger 1998fa7d9493SBabu Moger atomic_inc(&rdtgrp->waitcount); 1999fa7d9493SBabu Moger kernfs_break_active_protection(kn); 2000fa7d9493SBabu Moger 2001fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2002fa7d9493SBabu Moger 2003fa7d9493SBabu Moger /* Was this group deleted while we waited? */ 2004fa7d9493SBabu Moger if (rdtgrp->flags & RDT_DELETED) 2005fa7d9493SBabu Moger return NULL; 2006fa7d9493SBabu Moger 2007fa7d9493SBabu Moger return rdtgrp; 2008fa7d9493SBabu Moger } 2009fa7d9493SBabu Moger 2010fa7d9493SBabu Moger void rdtgroup_kn_unlock(struct kernfs_node *kn) 2011fa7d9493SBabu Moger { 2012fa7d9493SBabu Moger struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 2013fa7d9493SBabu Moger 2014fa7d9493SBabu Moger if (!rdtgrp) 2015fa7d9493SBabu Moger return; 2016fa7d9493SBabu Moger 2017fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2018fa7d9493SBabu Moger 2019fa7d9493SBabu Moger if (atomic_dec_and_test(&rdtgrp->waitcount) && 2020fa7d9493SBabu Moger (rdtgrp->flags & RDT_DELETED)) { 2021fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2022fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 2023fa7d9493SBabu Moger rdtgroup_pseudo_lock_remove(rdtgrp); 2024fa7d9493SBabu Moger kernfs_unbreak_active_protection(kn); 202575899924SXiaochen Shen rdtgroup_remove(rdtgrp); 2026fa7d9493SBabu Moger } else { 2027fa7d9493SBabu Moger kernfs_unbreak_active_protection(kn); 2028fa7d9493SBabu Moger } 2029fa7d9493SBabu Moger } 2030fa7d9493SBabu Moger 2031fa7d9493SBabu Moger static int mkdir_mondata_all(struct kernfs_node *parent_kn, 2032fa7d9493SBabu Moger struct rdtgroup *prgrp, 2033fa7d9493SBabu Moger struct kernfs_node **mon_data_kn); 2034fa7d9493SBabu Moger 203523bf1b6bSDavid Howells static int rdt_enable_ctx(struct rdt_fs_context *ctx) 2036fa7d9493SBabu Moger { 203723bf1b6bSDavid Howells int ret = 0; 203823bf1b6bSDavid Howells 203923bf1b6bSDavid Howells if (ctx->enable_cdpl2) 2040c091e907SJames Morse ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true); 204123bf1b6bSDavid Howells 204223bf1b6bSDavid Howells if (!ret && ctx->enable_cdpl3) 2043c091e907SJames Morse ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true); 204423bf1b6bSDavid Howells 204523bf1b6bSDavid Howells if (!ret && ctx->enable_mba_mbps) 204623bf1b6bSDavid Howells ret = set_mba_sc(true); 204723bf1b6bSDavid Howells 204823bf1b6bSDavid Howells return ret; 204923bf1b6bSDavid Howells } 205023bf1b6bSDavid Howells 20515c3b63cdSJames Morse static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type) 2052cdb9ebc9SJames Morse { 2053cdb9ebc9SJames Morse struct resctrl_schema *s; 20545c3b63cdSJames Morse const char *suffix = ""; 2055e198fde3SJames Morse int ret, cl; 2056cdb9ebc9SJames Morse 2057cdb9ebc9SJames Morse s = kzalloc(sizeof(*s), GFP_KERNEL); 2058cdb9ebc9SJames Morse if (!s) 2059cdb9ebc9SJames Morse return -ENOMEM; 2060cdb9ebc9SJames Morse 2061cdb9ebc9SJames Morse s->res = r; 2062eb6f3187SJames Morse s->num_closid = resctrl_arch_get_num_closid(r); 2063141739aaSJames Morse if (resctrl_arch_get_cdp_enabled(r->rid)) 2064141739aaSJames Morse s->num_closid /= 2; 2065cdb9ebc9SJames Morse 20665c3b63cdSJames Morse s->conf_type = type; 20675c3b63cdSJames Morse switch (type) { 20685c3b63cdSJames Morse case CDP_CODE: 20695c3b63cdSJames Morse suffix = "CODE"; 20705c3b63cdSJames Morse break; 20715c3b63cdSJames Morse case CDP_DATA: 20725c3b63cdSJames Morse suffix = "DATA"; 20735c3b63cdSJames Morse break; 20745c3b63cdSJames Morse case CDP_NONE: 20755c3b63cdSJames Morse suffix = ""; 20765c3b63cdSJames Morse break; 20775c3b63cdSJames Morse } 20785c3b63cdSJames Morse 20795c3b63cdSJames Morse ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix); 2080e198fde3SJames Morse if (ret >= sizeof(s->name)) { 2081e198fde3SJames Morse kfree(s); 2082e198fde3SJames Morse return -EINVAL; 2083e198fde3SJames Morse } 2084e198fde3SJames Morse 2085e198fde3SJames Morse cl = strlen(s->name); 2086e198fde3SJames Morse 2087e198fde3SJames Morse /* 2088e198fde3SJames Morse * If CDP is supported by this resource, but not enabled, 2089e198fde3SJames Morse * include the suffix. This ensures the tabular format of the 20905c3b63cdSJames Morse * schemata file does not change between mounts of the filesystem. 2091e198fde3SJames Morse */ 2092e198fde3SJames Morse if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid)) 2093e198fde3SJames Morse cl += 4; 2094e198fde3SJames Morse 2095e198fde3SJames Morse if (cl > max_name_width) 2096e198fde3SJames Morse max_name_width = cl; 2097e198fde3SJames Morse 2098cdb9ebc9SJames Morse INIT_LIST_HEAD(&s->list); 2099cdb9ebc9SJames Morse list_add(&s->list, &resctrl_schema_all); 2100cdb9ebc9SJames Morse 2101cdb9ebc9SJames Morse return 0; 2102cdb9ebc9SJames Morse } 2103cdb9ebc9SJames Morse 21045c3b63cdSJames Morse static int schemata_list_create(void) 21055c3b63cdSJames Morse { 21065c3b63cdSJames Morse struct rdt_resource *r; 21075c3b63cdSJames Morse int ret = 0; 21085c3b63cdSJames Morse 21095c3b63cdSJames Morse for_each_alloc_enabled_rdt_resource(r) { 21105c3b63cdSJames Morse if (resctrl_arch_get_cdp_enabled(r->rid)) { 21115c3b63cdSJames Morse ret = schemata_list_add(r, CDP_CODE); 21125c3b63cdSJames Morse if (ret) 21135c3b63cdSJames Morse break; 21145c3b63cdSJames Morse 21155c3b63cdSJames Morse ret = schemata_list_add(r, CDP_DATA); 21165c3b63cdSJames Morse } else { 21175c3b63cdSJames Morse ret = schemata_list_add(r, CDP_NONE); 21185c3b63cdSJames Morse } 21195c3b63cdSJames Morse 21205c3b63cdSJames Morse if (ret) 21215c3b63cdSJames Morse break; 21225c3b63cdSJames Morse } 21235c3b63cdSJames Morse 21245c3b63cdSJames Morse return ret; 21255c3b63cdSJames Morse } 21265c3b63cdSJames Morse 2127cdb9ebc9SJames Morse static void schemata_list_destroy(void) 2128cdb9ebc9SJames Morse { 2129cdb9ebc9SJames Morse struct resctrl_schema *s, *tmp; 2130cdb9ebc9SJames Morse 2131cdb9ebc9SJames Morse list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) { 2132cdb9ebc9SJames Morse list_del(&s->list); 2133cdb9ebc9SJames Morse kfree(s); 2134cdb9ebc9SJames Morse } 2135cdb9ebc9SJames Morse } 2136cdb9ebc9SJames Morse 213723bf1b6bSDavid Howells static int rdt_get_tree(struct fs_context *fc) 213823bf1b6bSDavid Howells { 213923bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 2140fa7d9493SBabu Moger struct rdt_domain *dom; 2141fa7d9493SBabu Moger struct rdt_resource *r; 2142fa7d9493SBabu Moger int ret; 2143fa7d9493SBabu Moger 2144fa7d9493SBabu Moger cpus_read_lock(); 2145fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2146fa7d9493SBabu Moger /* 2147fa7d9493SBabu Moger * resctrl file system can only be mounted once. 2148fa7d9493SBabu Moger */ 2149fa7d9493SBabu Moger if (static_branch_unlikely(&rdt_enable_key)) { 215023bf1b6bSDavid Howells ret = -EBUSY; 2151fa7d9493SBabu Moger goto out; 2152fa7d9493SBabu Moger } 2153fa7d9493SBabu Moger 215423bf1b6bSDavid Howells ret = rdt_enable_ctx(ctx); 215523bf1b6bSDavid Howells if (ret < 0) 2156fa7d9493SBabu Moger goto out_cdp; 2157fa7d9493SBabu Moger 2158cdb9ebc9SJames Morse ret = schemata_list_create(); 2159cdb9ebc9SJames Morse if (ret) { 2160cdb9ebc9SJames Morse schemata_list_destroy(); 2161cdb9ebc9SJames Morse goto out_mba; 2162cdb9ebc9SJames Morse } 2163cdb9ebc9SJames Morse 2164fa7d9493SBabu Moger closid_init(); 2165fa7d9493SBabu Moger 2166fa7d9493SBabu Moger ret = rdtgroup_create_info_dir(rdtgroup_default.kn); 216723bf1b6bSDavid Howells if (ret < 0) 2168cdb9ebc9SJames Morse goto out_schemata_free; 2169fa7d9493SBabu Moger 2170fa7d9493SBabu Moger if (rdt_mon_capable) { 2171fa7d9493SBabu Moger ret = mongroup_create_dir(rdtgroup_default.kn, 2172334b0f4eSXiaochen Shen &rdtgroup_default, "mon_groups", 2173fa7d9493SBabu Moger &kn_mongrp); 217423bf1b6bSDavid Howells if (ret < 0) 2175fa7d9493SBabu Moger goto out_info; 2176fa7d9493SBabu Moger 2177fa7d9493SBabu Moger ret = mkdir_mondata_all(rdtgroup_default.kn, 2178fa7d9493SBabu Moger &rdtgroup_default, &kn_mondata); 217923bf1b6bSDavid Howells if (ret < 0) 2180fa7d9493SBabu Moger goto out_mongrp; 2181fa7d9493SBabu Moger rdtgroup_default.mon.mon_data_kn = kn_mondata; 2182fa7d9493SBabu Moger } 2183fa7d9493SBabu Moger 2184fa7d9493SBabu Moger ret = rdt_pseudo_lock_init(); 218523bf1b6bSDavid Howells if (ret) 2186fa7d9493SBabu Moger goto out_mondata; 2187fa7d9493SBabu Moger 218823bf1b6bSDavid Howells ret = kernfs_get_tree(fc); 218923bf1b6bSDavid Howells if (ret < 0) 2190fa7d9493SBabu Moger goto out_psl; 2191fa7d9493SBabu Moger 2192fa7d9493SBabu Moger if (rdt_alloc_capable) 2193fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_alloc_enable_key); 2194fa7d9493SBabu Moger if (rdt_mon_capable) 2195fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_mon_enable_key); 2196fa7d9493SBabu Moger 2197fa7d9493SBabu Moger if (rdt_alloc_capable || rdt_mon_capable) 2198fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_enable_key); 2199fa7d9493SBabu Moger 2200fa7d9493SBabu Moger if (is_mbm_enabled()) { 220163c8b123SJames Morse r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl; 2202fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) 2203fa7d9493SBabu Moger mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL); 2204fa7d9493SBabu Moger } 2205fa7d9493SBabu Moger 2206fa7d9493SBabu Moger goto out; 2207fa7d9493SBabu Moger 2208fa7d9493SBabu Moger out_psl: 2209fa7d9493SBabu Moger rdt_pseudo_lock_release(); 2210fa7d9493SBabu Moger out_mondata: 2211fa7d9493SBabu Moger if (rdt_mon_capable) 2212fa7d9493SBabu Moger kernfs_remove(kn_mondata); 2213fa7d9493SBabu Moger out_mongrp: 2214fa7d9493SBabu Moger if (rdt_mon_capable) 2215fa7d9493SBabu Moger kernfs_remove(kn_mongrp); 2216fa7d9493SBabu Moger out_info: 2217fa7d9493SBabu Moger kernfs_remove(kn_info); 2218cdb9ebc9SJames Morse out_schemata_free: 2219cdb9ebc9SJames Morse schemata_list_destroy(); 222023bf1b6bSDavid Howells out_mba: 222123bf1b6bSDavid Howells if (ctx->enable_mba_mbps) 222223bf1b6bSDavid Howells set_mba_sc(false); 2223fa7d9493SBabu Moger out_cdp: 2224fa7d9493SBabu Moger cdp_disable_all(); 2225fa7d9493SBabu Moger out: 2226fa7d9493SBabu Moger rdt_last_cmd_clear(); 2227fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2228fa7d9493SBabu Moger cpus_read_unlock(); 222923bf1b6bSDavid Howells return ret; 223023bf1b6bSDavid Howells } 2231fa7d9493SBabu Moger 223223bf1b6bSDavid Howells enum rdt_param { 223323bf1b6bSDavid Howells Opt_cdp, 223423bf1b6bSDavid Howells Opt_cdpl2, 2235faa3604eSXiaochen Shen Opt_mba_mbps, 223623bf1b6bSDavid Howells nr__rdt_params 223723bf1b6bSDavid Howells }; 223823bf1b6bSDavid Howells 2239d7167b14SAl Viro static const struct fs_parameter_spec rdt_fs_parameters[] = { 224023bf1b6bSDavid Howells fsparam_flag("cdp", Opt_cdp), 224123bf1b6bSDavid Howells fsparam_flag("cdpl2", Opt_cdpl2), 2242faa3604eSXiaochen Shen fsparam_flag("mba_MBps", Opt_mba_mbps), 224323bf1b6bSDavid Howells {} 224423bf1b6bSDavid Howells }; 224523bf1b6bSDavid Howells 224623bf1b6bSDavid Howells static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param) 224723bf1b6bSDavid Howells { 224823bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 224923bf1b6bSDavid Howells struct fs_parse_result result; 225023bf1b6bSDavid Howells int opt; 225123bf1b6bSDavid Howells 2252d7167b14SAl Viro opt = fs_parse(fc, rdt_fs_parameters, param, &result); 225323bf1b6bSDavid Howells if (opt < 0) 225423bf1b6bSDavid Howells return opt; 225523bf1b6bSDavid Howells 225623bf1b6bSDavid Howells switch (opt) { 225723bf1b6bSDavid Howells case Opt_cdp: 225823bf1b6bSDavid Howells ctx->enable_cdpl3 = true; 225923bf1b6bSDavid Howells return 0; 226023bf1b6bSDavid Howells case Opt_cdpl2: 226123bf1b6bSDavid Howells ctx->enable_cdpl2 = true; 226223bf1b6bSDavid Howells return 0; 2263faa3604eSXiaochen Shen case Opt_mba_mbps: 226423bf1b6bSDavid Howells if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 226523bf1b6bSDavid Howells return -EINVAL; 226623bf1b6bSDavid Howells ctx->enable_mba_mbps = true; 226723bf1b6bSDavid Howells return 0; 226823bf1b6bSDavid Howells } 226923bf1b6bSDavid Howells 227023bf1b6bSDavid Howells return -EINVAL; 227123bf1b6bSDavid Howells } 227223bf1b6bSDavid Howells 227323bf1b6bSDavid Howells static void rdt_fs_context_free(struct fs_context *fc) 227423bf1b6bSDavid Howells { 227523bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 227623bf1b6bSDavid Howells 227723bf1b6bSDavid Howells kernfs_free_fs_context(fc); 227823bf1b6bSDavid Howells kfree(ctx); 227923bf1b6bSDavid Howells } 228023bf1b6bSDavid Howells 228123bf1b6bSDavid Howells static const struct fs_context_operations rdt_fs_context_ops = { 228223bf1b6bSDavid Howells .free = rdt_fs_context_free, 228323bf1b6bSDavid Howells .parse_param = rdt_parse_param, 228423bf1b6bSDavid Howells .get_tree = rdt_get_tree, 228523bf1b6bSDavid Howells }; 228623bf1b6bSDavid Howells 228723bf1b6bSDavid Howells static int rdt_init_fs_context(struct fs_context *fc) 228823bf1b6bSDavid Howells { 228923bf1b6bSDavid Howells struct rdt_fs_context *ctx; 229023bf1b6bSDavid Howells 229123bf1b6bSDavid Howells ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL); 229223bf1b6bSDavid Howells if (!ctx) 229323bf1b6bSDavid Howells return -ENOMEM; 229423bf1b6bSDavid Howells 229523bf1b6bSDavid Howells ctx->kfc.root = rdt_root; 229623bf1b6bSDavid Howells ctx->kfc.magic = RDTGROUP_SUPER_MAGIC; 229723bf1b6bSDavid Howells fc->fs_private = &ctx->kfc; 229823bf1b6bSDavid Howells fc->ops = &rdt_fs_context_ops; 229923bf1b6bSDavid Howells put_user_ns(fc->user_ns); 230023bf1b6bSDavid Howells fc->user_ns = get_user_ns(&init_user_ns); 230123bf1b6bSDavid Howells fc->global = true; 230223bf1b6bSDavid Howells return 0; 2303fa7d9493SBabu Moger } 2304fa7d9493SBabu Moger 2305fa7d9493SBabu Moger static int reset_all_ctrls(struct rdt_resource *r) 2306fa7d9493SBabu Moger { 230763c8b123SJames Morse struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r); 2308792e0f6fSJames Morse struct rdt_hw_domain *hw_dom; 2309fa7d9493SBabu Moger struct msr_param msr_param; 2310fa7d9493SBabu Moger cpumask_var_t cpu_mask; 2311fa7d9493SBabu Moger struct rdt_domain *d; 2312fa7d9493SBabu Moger int i, cpu; 2313fa7d9493SBabu Moger 2314fa7d9493SBabu Moger if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) 2315fa7d9493SBabu Moger return -ENOMEM; 2316fa7d9493SBabu Moger 2317fa7d9493SBabu Moger msr_param.res = r; 2318fa7d9493SBabu Moger msr_param.low = 0; 231963c8b123SJames Morse msr_param.high = hw_res->num_closid; 2320fa7d9493SBabu Moger 2321fa7d9493SBabu Moger /* 2322fa7d9493SBabu Moger * Disable resource control for this resource by setting all 2323fa7d9493SBabu Moger * CBMs in all domains to the maximum mask value. Pick one CPU 2324fa7d9493SBabu Moger * from each domain to update the MSRs below. 2325fa7d9493SBabu Moger */ 2326fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 2327792e0f6fSJames Morse hw_dom = resctrl_to_arch_dom(d); 2328fa7d9493SBabu Moger cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); 2329fa7d9493SBabu Moger 233063c8b123SJames Morse for (i = 0; i < hw_res->num_closid; i++) 2331792e0f6fSJames Morse hw_dom->ctrl_val[i] = r->default_ctrl; 2332fa7d9493SBabu Moger } 2333fa7d9493SBabu Moger cpu = get_cpu(); 2334fa7d9493SBabu Moger /* Update CBM on this cpu if it's in cpu_mask. */ 2335fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 2336fa7d9493SBabu Moger rdt_ctrl_update(&msr_param); 2337fa7d9493SBabu Moger /* Update CBM on all other cpus in cpu_mask. */ 2338fa7d9493SBabu Moger smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1); 2339fa7d9493SBabu Moger put_cpu(); 2340fa7d9493SBabu Moger 2341fa7d9493SBabu Moger free_cpumask_var(cpu_mask); 2342fa7d9493SBabu Moger 2343fa7d9493SBabu Moger return 0; 2344fa7d9493SBabu Moger } 2345fa7d9493SBabu Moger 2346fa7d9493SBabu Moger /* 2347fa7d9493SBabu Moger * Move tasks from one to the other group. If @from is NULL, then all tasks 2348fa7d9493SBabu Moger * in the systems are moved unconditionally (used for teardown). 2349fa7d9493SBabu Moger * 2350fa7d9493SBabu Moger * If @mask is not NULL the cpus on which moved tasks are running are set 2351fa7d9493SBabu Moger * in that mask so the update smp function call is restricted to affected 2352fa7d9493SBabu Moger * cpus. 2353fa7d9493SBabu Moger */ 2354fa7d9493SBabu Moger static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, 2355fa7d9493SBabu Moger struct cpumask *mask) 2356fa7d9493SBabu Moger { 2357fa7d9493SBabu Moger struct task_struct *p, *t; 2358fa7d9493SBabu Moger 2359fa7d9493SBabu Moger read_lock(&tasklist_lock); 2360fa7d9493SBabu Moger for_each_process_thread(p, t) { 2361fa7d9493SBabu Moger if (!from || is_closid_match(t, from) || 2362fa7d9493SBabu Moger is_rmid_match(t, from)) { 23636d3b47ddSValentin Schneider WRITE_ONCE(t->closid, to->closid); 23646d3b47ddSValentin Schneider WRITE_ONCE(t->rmid, to->mon.rmid); 2365fa7d9493SBabu Moger 2366fa7d9493SBabu Moger /* 2367e0ad6dc8SReinette Chatre * If the task is on a CPU, set the CPU in the mask. 2368e0ad6dc8SReinette Chatre * The detection is inaccurate as tasks might move or 2369e0ad6dc8SReinette Chatre * schedule before the smp function call takes place. 2370e0ad6dc8SReinette Chatre * In such a case the function call is pointless, but 2371fa7d9493SBabu Moger * there is no other side effect. 2372fa7d9493SBabu Moger */ 2373e0ad6dc8SReinette Chatre if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t)) 2374fa7d9493SBabu Moger cpumask_set_cpu(task_cpu(t), mask); 2375fa7d9493SBabu Moger } 2376fa7d9493SBabu Moger } 2377fa7d9493SBabu Moger read_unlock(&tasklist_lock); 2378fa7d9493SBabu Moger } 2379fa7d9493SBabu Moger 2380fa7d9493SBabu Moger static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp) 2381fa7d9493SBabu Moger { 2382fa7d9493SBabu Moger struct rdtgroup *sentry, *stmp; 2383fa7d9493SBabu Moger struct list_head *head; 2384fa7d9493SBabu Moger 2385fa7d9493SBabu Moger head = &rdtgrp->mon.crdtgrp_list; 2386fa7d9493SBabu Moger list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) { 2387fa7d9493SBabu Moger free_rmid(sentry->mon.rmid); 2388fa7d9493SBabu Moger list_del(&sentry->mon.crdtgrp_list); 2389b8511cccSXiaochen Shen 2390b8511cccSXiaochen Shen if (atomic_read(&sentry->waitcount) != 0) 2391b8511cccSXiaochen Shen sentry->flags = RDT_DELETED; 2392b8511cccSXiaochen Shen else 239375899924SXiaochen Shen rdtgroup_remove(sentry); 2394fa7d9493SBabu Moger } 2395fa7d9493SBabu Moger } 2396fa7d9493SBabu Moger 2397fa7d9493SBabu Moger /* 2398fa7d9493SBabu Moger * Forcibly remove all of subdirectories under root. 2399fa7d9493SBabu Moger */ 2400fa7d9493SBabu Moger static void rmdir_all_sub(void) 2401fa7d9493SBabu Moger { 2402fa7d9493SBabu Moger struct rdtgroup *rdtgrp, *tmp; 2403fa7d9493SBabu Moger 2404fa7d9493SBabu Moger /* Move all tasks to the default resource group */ 2405fa7d9493SBabu Moger rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); 2406fa7d9493SBabu Moger 2407fa7d9493SBabu Moger list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { 2408fa7d9493SBabu Moger /* Free any child rmids */ 2409fa7d9493SBabu Moger free_all_child_rdtgrp(rdtgrp); 2410fa7d9493SBabu Moger 2411fa7d9493SBabu Moger /* Remove each rdtgroup other than root */ 2412fa7d9493SBabu Moger if (rdtgrp == &rdtgroup_default) 2413fa7d9493SBabu Moger continue; 2414fa7d9493SBabu Moger 2415fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2416fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 2417fa7d9493SBabu Moger rdtgroup_pseudo_lock_remove(rdtgrp); 2418fa7d9493SBabu Moger 2419fa7d9493SBabu Moger /* 2420fa7d9493SBabu Moger * Give any CPUs back to the default group. We cannot copy 2421fa7d9493SBabu Moger * cpu_online_mask because a CPU might have executed the 2422fa7d9493SBabu Moger * offline callback already, but is still marked online. 2423fa7d9493SBabu Moger */ 2424fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 2425fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 2426fa7d9493SBabu Moger 2427fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2428fa7d9493SBabu Moger 2429fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2430fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 2431b8511cccSXiaochen Shen 2432b8511cccSXiaochen Shen if (atomic_read(&rdtgrp->waitcount) != 0) 2433b8511cccSXiaochen Shen rdtgrp->flags = RDT_DELETED; 2434b8511cccSXiaochen Shen else 243575899924SXiaochen Shen rdtgroup_remove(rdtgrp); 2436fa7d9493SBabu Moger } 2437fa7d9493SBabu Moger /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ 2438fa7d9493SBabu Moger update_closid_rmid(cpu_online_mask, &rdtgroup_default); 2439fa7d9493SBabu Moger 2440fa7d9493SBabu Moger kernfs_remove(kn_info); 2441fa7d9493SBabu Moger kernfs_remove(kn_mongrp); 2442fa7d9493SBabu Moger kernfs_remove(kn_mondata); 2443fa7d9493SBabu Moger } 2444fa7d9493SBabu Moger 2445fa7d9493SBabu Moger static void rdt_kill_sb(struct super_block *sb) 2446fa7d9493SBabu Moger { 2447fa7d9493SBabu Moger struct rdt_resource *r; 2448fa7d9493SBabu Moger 2449fa7d9493SBabu Moger cpus_read_lock(); 2450fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2451fa7d9493SBabu Moger 2452fa7d9493SBabu Moger set_mba_sc(false); 2453fa7d9493SBabu Moger 2454fa7d9493SBabu Moger /*Put everything back to default values. */ 2455fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) 2456fa7d9493SBabu Moger reset_all_ctrls(r); 2457fa7d9493SBabu Moger cdp_disable_all(); 2458fa7d9493SBabu Moger rmdir_all_sub(); 2459fa7d9493SBabu Moger rdt_pseudo_lock_release(); 2460fa7d9493SBabu Moger rdtgroup_default.mode = RDT_MODE_SHAREABLE; 2461cdb9ebc9SJames Morse schemata_list_destroy(); 2462fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_alloc_enable_key); 2463fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_mon_enable_key); 2464fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_enable_key); 2465fa7d9493SBabu Moger kernfs_kill_sb(sb); 2466fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2467fa7d9493SBabu Moger cpus_read_unlock(); 2468fa7d9493SBabu Moger } 2469fa7d9493SBabu Moger 2470fa7d9493SBabu Moger static struct file_system_type rdt_fs_type = { 2471fa7d9493SBabu Moger .name = "resctrl", 247223bf1b6bSDavid Howells .init_fs_context = rdt_init_fs_context, 2473d7167b14SAl Viro .parameters = rdt_fs_parameters, 2474fa7d9493SBabu Moger .kill_sb = rdt_kill_sb, 2475fa7d9493SBabu Moger }; 2476fa7d9493SBabu Moger 2477fa7d9493SBabu Moger static int mon_addfile(struct kernfs_node *parent_kn, const char *name, 2478fa7d9493SBabu Moger void *priv) 2479fa7d9493SBabu Moger { 2480fa7d9493SBabu Moger struct kernfs_node *kn; 2481fa7d9493SBabu Moger int ret = 0; 2482fa7d9493SBabu Moger 2483fa7d9493SBabu Moger kn = __kernfs_create_file(parent_kn, name, 0444, 2484fa7d9493SBabu Moger GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0, 2485fa7d9493SBabu Moger &kf_mondata_ops, priv, NULL, NULL); 2486fa7d9493SBabu Moger if (IS_ERR(kn)) 2487fa7d9493SBabu Moger return PTR_ERR(kn); 2488fa7d9493SBabu Moger 2489fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2490fa7d9493SBabu Moger if (ret) { 2491fa7d9493SBabu Moger kernfs_remove(kn); 2492fa7d9493SBabu Moger return ret; 2493fa7d9493SBabu Moger } 2494fa7d9493SBabu Moger 2495fa7d9493SBabu Moger return ret; 2496fa7d9493SBabu Moger } 2497fa7d9493SBabu Moger 2498fa7d9493SBabu Moger /* 2499fa7d9493SBabu Moger * Remove all subdirectories of mon_data of ctrl_mon groups 2500fa7d9493SBabu Moger * and monitor groups with given domain id. 2501fa7d9493SBabu Moger */ 2502fa7d9493SBabu Moger void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id) 2503fa7d9493SBabu Moger { 2504fa7d9493SBabu Moger struct rdtgroup *prgrp, *crgrp; 2505fa7d9493SBabu Moger char name[32]; 2506fa7d9493SBabu Moger 2507fa7d9493SBabu Moger if (!r->mon_enabled) 2508fa7d9493SBabu Moger return; 2509fa7d9493SBabu Moger 2510fa7d9493SBabu Moger list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 2511fa7d9493SBabu Moger sprintf(name, "mon_%s_%02d", r->name, dom_id); 2512fa7d9493SBabu Moger kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); 2513fa7d9493SBabu Moger 2514fa7d9493SBabu Moger list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 2515fa7d9493SBabu Moger kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); 2516fa7d9493SBabu Moger } 2517fa7d9493SBabu Moger } 2518fa7d9493SBabu Moger 2519fa7d9493SBabu Moger static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, 2520fa7d9493SBabu Moger struct rdt_domain *d, 2521fa7d9493SBabu Moger struct rdt_resource *r, struct rdtgroup *prgrp) 2522fa7d9493SBabu Moger { 2523fa7d9493SBabu Moger union mon_data_bits priv; 2524fa7d9493SBabu Moger struct kernfs_node *kn; 2525fa7d9493SBabu Moger struct mon_evt *mevt; 2526fa7d9493SBabu Moger struct rmid_read rr; 2527fa7d9493SBabu Moger char name[32]; 2528fa7d9493SBabu Moger int ret; 2529fa7d9493SBabu Moger 2530fa7d9493SBabu Moger sprintf(name, "mon_%s_%02d", r->name, d->id); 2531fa7d9493SBabu Moger /* create the directory */ 2532fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 2533fa7d9493SBabu Moger if (IS_ERR(kn)) 2534fa7d9493SBabu Moger return PTR_ERR(kn); 2535fa7d9493SBabu Moger 2536fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2537fa7d9493SBabu Moger if (ret) 2538fa7d9493SBabu Moger goto out_destroy; 2539fa7d9493SBabu Moger 2540fa7d9493SBabu Moger if (WARN_ON(list_empty(&r->evt_list))) { 2541fa7d9493SBabu Moger ret = -EPERM; 2542fa7d9493SBabu Moger goto out_destroy; 2543fa7d9493SBabu Moger } 2544fa7d9493SBabu Moger 2545fa7d9493SBabu Moger priv.u.rid = r->rid; 2546fa7d9493SBabu Moger priv.u.domid = d->id; 2547fa7d9493SBabu Moger list_for_each_entry(mevt, &r->evt_list, list) { 2548fa7d9493SBabu Moger priv.u.evtid = mevt->evtid; 2549fa7d9493SBabu Moger ret = mon_addfile(kn, mevt->name, priv.priv); 2550fa7d9493SBabu Moger if (ret) 2551fa7d9493SBabu Moger goto out_destroy; 2552fa7d9493SBabu Moger 2553fa7d9493SBabu Moger if (is_mbm_event(mevt->evtid)) 255446637d45SReinette Chatre mon_event_read(&rr, r, d, prgrp, mevt->evtid, true); 2555fa7d9493SBabu Moger } 2556fa7d9493SBabu Moger kernfs_activate(kn); 2557fa7d9493SBabu Moger return 0; 2558fa7d9493SBabu Moger 2559fa7d9493SBabu Moger out_destroy: 2560fa7d9493SBabu Moger kernfs_remove(kn); 2561fa7d9493SBabu Moger return ret; 2562fa7d9493SBabu Moger } 2563fa7d9493SBabu Moger 2564fa7d9493SBabu Moger /* 2565fa7d9493SBabu Moger * Add all subdirectories of mon_data for "ctrl_mon" groups 2566fa7d9493SBabu Moger * and "monitor" groups with given domain id. 2567fa7d9493SBabu Moger */ 2568fa7d9493SBabu Moger void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 2569fa7d9493SBabu Moger struct rdt_domain *d) 2570fa7d9493SBabu Moger { 2571fa7d9493SBabu Moger struct kernfs_node *parent_kn; 2572fa7d9493SBabu Moger struct rdtgroup *prgrp, *crgrp; 2573fa7d9493SBabu Moger struct list_head *head; 2574fa7d9493SBabu Moger 2575fa7d9493SBabu Moger if (!r->mon_enabled) 2576fa7d9493SBabu Moger return; 2577fa7d9493SBabu Moger 2578fa7d9493SBabu Moger list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 2579fa7d9493SBabu Moger parent_kn = prgrp->mon.mon_data_kn; 2580fa7d9493SBabu Moger mkdir_mondata_subdir(parent_kn, d, r, prgrp); 2581fa7d9493SBabu Moger 2582fa7d9493SBabu Moger head = &prgrp->mon.crdtgrp_list; 2583fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 2584fa7d9493SBabu Moger parent_kn = crgrp->mon.mon_data_kn; 2585fa7d9493SBabu Moger mkdir_mondata_subdir(parent_kn, d, r, crgrp); 2586fa7d9493SBabu Moger } 2587fa7d9493SBabu Moger } 2588fa7d9493SBabu Moger } 2589fa7d9493SBabu Moger 2590fa7d9493SBabu Moger static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, 2591fa7d9493SBabu Moger struct rdt_resource *r, 2592fa7d9493SBabu Moger struct rdtgroup *prgrp) 2593fa7d9493SBabu Moger { 2594fa7d9493SBabu Moger struct rdt_domain *dom; 2595fa7d9493SBabu Moger int ret; 2596fa7d9493SBabu Moger 2597fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) { 2598fa7d9493SBabu Moger ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp); 2599fa7d9493SBabu Moger if (ret) 2600fa7d9493SBabu Moger return ret; 2601fa7d9493SBabu Moger } 2602fa7d9493SBabu Moger 2603fa7d9493SBabu Moger return 0; 2604fa7d9493SBabu Moger } 2605fa7d9493SBabu Moger 2606fa7d9493SBabu Moger /* 2607fa7d9493SBabu Moger * This creates a directory mon_data which contains the monitored data. 2608fa7d9493SBabu Moger * 2609163b0991SIngo Molnar * mon_data has one directory for each domain which are named 2610fa7d9493SBabu Moger * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data 2611fa7d9493SBabu Moger * with L3 domain looks as below: 2612fa7d9493SBabu Moger * ./mon_data: 2613fa7d9493SBabu Moger * mon_L3_00 2614fa7d9493SBabu Moger * mon_L3_01 2615fa7d9493SBabu Moger * mon_L3_02 2616fa7d9493SBabu Moger * ... 2617fa7d9493SBabu Moger * 2618fa7d9493SBabu Moger * Each domain directory has one file per event: 2619fa7d9493SBabu Moger * ./mon_L3_00/: 2620fa7d9493SBabu Moger * llc_occupancy 2621fa7d9493SBabu Moger * 2622fa7d9493SBabu Moger */ 2623fa7d9493SBabu Moger static int mkdir_mondata_all(struct kernfs_node *parent_kn, 2624fa7d9493SBabu Moger struct rdtgroup *prgrp, 2625fa7d9493SBabu Moger struct kernfs_node **dest_kn) 2626fa7d9493SBabu Moger { 2627fa7d9493SBabu Moger struct rdt_resource *r; 2628fa7d9493SBabu Moger struct kernfs_node *kn; 2629fa7d9493SBabu Moger int ret; 2630fa7d9493SBabu Moger 2631fa7d9493SBabu Moger /* 2632fa7d9493SBabu Moger * Create the mon_data directory first. 2633fa7d9493SBabu Moger */ 2634334b0f4eSXiaochen Shen ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn); 2635fa7d9493SBabu Moger if (ret) 2636fa7d9493SBabu Moger return ret; 2637fa7d9493SBabu Moger 2638fa7d9493SBabu Moger if (dest_kn) 2639fa7d9493SBabu Moger *dest_kn = kn; 2640fa7d9493SBabu Moger 2641fa7d9493SBabu Moger /* 2642fa7d9493SBabu Moger * Create the subdirectories for each domain. Note that all events 2643fa7d9493SBabu Moger * in a domain like L3 are grouped into a resource whose domain is L3 2644fa7d9493SBabu Moger */ 2645fa7d9493SBabu Moger for_each_mon_enabled_rdt_resource(r) { 2646fa7d9493SBabu Moger ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); 2647fa7d9493SBabu Moger if (ret) 2648fa7d9493SBabu Moger goto out_destroy; 2649fa7d9493SBabu Moger } 2650fa7d9493SBabu Moger 2651fa7d9493SBabu Moger return 0; 2652fa7d9493SBabu Moger 2653fa7d9493SBabu Moger out_destroy: 2654fa7d9493SBabu Moger kernfs_remove(kn); 2655fa7d9493SBabu Moger return ret; 2656fa7d9493SBabu Moger } 2657fa7d9493SBabu Moger 2658fa7d9493SBabu Moger /** 2659fa7d9493SBabu Moger * cbm_ensure_valid - Enforce validity on provided CBM 2660fa7d9493SBabu Moger * @_val: Candidate CBM 2661fa7d9493SBabu Moger * @r: RDT resource to which the CBM belongs 2662fa7d9493SBabu Moger * 2663fa7d9493SBabu Moger * The provided CBM represents all cache portions available for use. This 2664fa7d9493SBabu Moger * may be represented by a bitmap that does not consist of contiguous ones 2665fa7d9493SBabu Moger * and thus be an invalid CBM. 2666fa7d9493SBabu Moger * Here the provided CBM is forced to be a valid CBM by only considering 2667fa7d9493SBabu Moger * the first set of contiguous bits as valid and clearing all bits. 2668fa7d9493SBabu Moger * The intention here is to provide a valid default CBM with which a new 2669fa7d9493SBabu Moger * resource group is initialized. The user can follow this with a 2670fa7d9493SBabu Moger * modification to the CBM if the default does not satisfy the 2671fa7d9493SBabu Moger * requirements. 2672fa7d9493SBabu Moger */ 26732ef085bdSReinette Chatre static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r) 2674fa7d9493SBabu Moger { 2675fa7d9493SBabu Moger unsigned int cbm_len = r->cache.cbm_len; 2676fa7d9493SBabu Moger unsigned long first_bit, zero_bit; 26772ef085bdSReinette Chatre unsigned long val = _val; 2678fa7d9493SBabu Moger 26792ef085bdSReinette Chatre if (!val) 26802ef085bdSReinette Chatre return 0; 2681fa7d9493SBabu Moger 268232f010deSReinette Chatre first_bit = find_first_bit(&val, cbm_len); 268332f010deSReinette Chatre zero_bit = find_next_zero_bit(&val, cbm_len, first_bit); 2684fa7d9493SBabu Moger 2685fa7d9493SBabu Moger /* Clear any remaining bits to ensure contiguous region */ 268632f010deSReinette Chatre bitmap_clear(&val, zero_bit, cbm_len - zero_bit); 26872ef085bdSReinette Chatre return (u32)val; 2688fa7d9493SBabu Moger } 2689fa7d9493SBabu Moger 26907390619aSXiaochen Shen /* 26917390619aSXiaochen Shen * Initialize cache resources per RDT domain 2692fa7d9493SBabu Moger * 26937390619aSXiaochen Shen * Set the RDT domain up to start off with all usable allocations. That is, 26947390619aSXiaochen Shen * all shareable and unused bits. All-zero CBM is invalid. 2695fa7d9493SBabu Moger */ 26961c290682SJames Morse static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s, 26977390619aSXiaochen Shen u32 closid) 2698fa7d9493SBabu Moger { 2699fbc06c69SJames Morse enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type); 270075408e43SJames Morse enum resctrl_conf_type t = s->conf_type; 2701e8f72825SJames Morse struct resctrl_staged_config *cfg; 27021c290682SJames Morse struct rdt_resource *r = s->res; 2703fa7d9493SBabu Moger u32 used_b = 0, unused_b = 0; 2704fa7d9493SBabu Moger unsigned long tmp_cbm; 2705fa7d9493SBabu Moger enum rdtgrp_mode mode; 2706f07e9d02SJames Morse u32 peer_ctl, ctrl_val; 27077390619aSXiaochen Shen int i; 2708fa7d9493SBabu Moger 270975408e43SJames Morse cfg = &d->staged_config[t]; 2710e8f72825SJames Morse cfg->have_new_ctrl = false; 2711e8f72825SJames Morse cfg->new_ctrl = r->cache.shareable_bits; 2712fa7d9493SBabu Moger used_b = r->cache.shareable_bits; 2713f07e9d02SJames Morse for (i = 0; i < closids_supported(); i++) { 2714fa7d9493SBabu Moger if (closid_allocated(i) && i != closid) { 2715fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 2716fa7d9493SBabu Moger if (mode == RDT_MODE_PSEUDO_LOCKSETUP) 271787d3aa28SJames Morse /* 271887d3aa28SJames Morse * ctrl values for locksetup aren't relevant 271987d3aa28SJames Morse * until the schemata is written, and the mode 272087d3aa28SJames Morse * becomes RDT_MODE_PSEUDO_LOCKED. 272187d3aa28SJames Morse */ 272287d3aa28SJames Morse continue; 2723fa7d9493SBabu Moger /* 27247390619aSXiaochen Shen * If CDP is active include peer domain's 27257390619aSXiaochen Shen * usage to ensure there is no overlap 27267390619aSXiaochen Shen * with an exclusive group. 2727fa7d9493SBabu Moger */ 2728fbc06c69SJames Morse if (resctrl_arch_get_cdp_enabled(r->rid)) 2729111136e6SJames Morse peer_ctl = resctrl_arch_get_config(r, d, i, 2730111136e6SJames Morse peer_type); 2731fa7d9493SBabu Moger else 2732fa7d9493SBabu Moger peer_ctl = 0; 2733111136e6SJames Morse ctrl_val = resctrl_arch_get_config(r, d, i, 2734111136e6SJames Morse s->conf_type); 2735f07e9d02SJames Morse used_b |= ctrl_val | peer_ctl; 2736fa7d9493SBabu Moger if (mode == RDT_MODE_SHAREABLE) 2737f07e9d02SJames Morse cfg->new_ctrl |= ctrl_val | peer_ctl; 2738fa7d9493SBabu Moger } 2739fa7d9493SBabu Moger } 2740fa7d9493SBabu Moger if (d->plr && d->plr->cbm > 0) 2741fa7d9493SBabu Moger used_b |= d->plr->cbm; 2742fa7d9493SBabu Moger unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1); 2743fa7d9493SBabu Moger unused_b &= BIT_MASK(r->cache.cbm_len) - 1; 2744e8f72825SJames Morse cfg->new_ctrl |= unused_b; 2745fa7d9493SBabu Moger /* 2746fa7d9493SBabu Moger * Force the initial CBM to be valid, user can 2747fa7d9493SBabu Moger * modify the CBM based on system availability. 2748fa7d9493SBabu Moger */ 2749e8f72825SJames Morse cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r); 2750fa7d9493SBabu Moger /* 27517390619aSXiaochen Shen * Assign the u32 CBM to an unsigned long to ensure that 27527390619aSXiaochen Shen * bitmap_weight() does not access out-of-bound memory. 2753fa7d9493SBabu Moger */ 2754e8f72825SJames Morse tmp_cbm = cfg->new_ctrl; 27557390619aSXiaochen Shen if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) { 2756e198fde3SJames Morse rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id); 2757fa7d9493SBabu Moger return -ENOSPC; 2758fa7d9493SBabu Moger } 2759e8f72825SJames Morse cfg->have_new_ctrl = true; 27607390619aSXiaochen Shen 27617390619aSXiaochen Shen return 0; 27627390619aSXiaochen Shen } 27637390619aSXiaochen Shen 276447820e73SXiaochen Shen /* 276547820e73SXiaochen Shen * Initialize cache resources with default values. 27667390619aSXiaochen Shen * 27677390619aSXiaochen Shen * A new RDT group is being created on an allocation capable (CAT) 27687390619aSXiaochen Shen * supporting system. Set this group up to start off with all usable 27697390619aSXiaochen Shen * allocations. 27707390619aSXiaochen Shen * 27717390619aSXiaochen Shen * If there are no more shareable bits available on any domain then 27727390619aSXiaochen Shen * the entire allocation will fail. 27737390619aSXiaochen Shen */ 27741c290682SJames Morse static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid) 27757390619aSXiaochen Shen { 27767390619aSXiaochen Shen struct rdt_domain *d; 27777390619aSXiaochen Shen int ret; 27787390619aSXiaochen Shen 27791c290682SJames Morse list_for_each_entry(d, &s->res->domains, list) { 27801c290682SJames Morse ret = __init_one_rdt_domain(d, s, closid); 27817390619aSXiaochen Shen if (ret < 0) 27827390619aSXiaochen Shen return ret; 2783fa7d9493SBabu Moger } 278447820e73SXiaochen Shen 278547820e73SXiaochen Shen return 0; 2786fa7d9493SBabu Moger } 2787fa7d9493SBabu Moger 278847820e73SXiaochen Shen /* Initialize MBA resource with default values. */ 278947820e73SXiaochen Shen static void rdtgroup_init_mba(struct rdt_resource *r) 279047820e73SXiaochen Shen { 2791e8f72825SJames Morse struct resctrl_staged_config *cfg; 279247820e73SXiaochen Shen struct rdt_domain *d; 279347820e73SXiaochen Shen 279447820e73SXiaochen Shen list_for_each_entry(d, &r->domains, list) { 279575408e43SJames Morse cfg = &d->staged_config[CDP_NONE]; 2796e8f72825SJames Morse cfg->new_ctrl = is_mba_sc(r) ? MBA_MAX_MBPS : r->default_ctrl; 2797e8f72825SJames Morse cfg->have_new_ctrl = true; 279847820e73SXiaochen Shen } 279947820e73SXiaochen Shen } 280047820e73SXiaochen Shen 280147820e73SXiaochen Shen /* Initialize the RDT group's allocations. */ 280247820e73SXiaochen Shen static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp) 280347820e73SXiaochen Shen { 2804331ebe4cSJames Morse struct resctrl_schema *s; 280547820e73SXiaochen Shen struct rdt_resource *r; 280647820e73SXiaochen Shen int ret; 280747820e73SXiaochen Shen 2808331ebe4cSJames Morse list_for_each_entry(s, &resctrl_schema_all, list) { 2809331ebe4cSJames Morse r = s->res; 281047820e73SXiaochen Shen if (r->rid == RDT_RESOURCE_MBA) { 281147820e73SXiaochen Shen rdtgroup_init_mba(r); 281247820e73SXiaochen Shen } else { 28131c290682SJames Morse ret = rdtgroup_init_cat(s, rdtgrp->closid); 281447820e73SXiaochen Shen if (ret < 0) 281547820e73SXiaochen Shen return ret; 281647820e73SXiaochen Shen } 281747820e73SXiaochen Shen 28182e667819SJames Morse ret = resctrl_arch_update_domains(r, rdtgrp->closid); 2819fa7d9493SBabu Moger if (ret < 0) { 2820723f1a0dSBabu Moger rdt_last_cmd_puts("Failed to initialize allocations\n"); 2821fa7d9493SBabu Moger return ret; 2822fa7d9493SBabu Moger } 282347820e73SXiaochen Shen 2824fa7d9493SBabu Moger } 2825fa7d9493SBabu Moger 282640fba00fSXiaochen Shen rdtgrp->mode = RDT_MODE_SHAREABLE; 282740fba00fSXiaochen Shen 2828fa7d9493SBabu Moger return 0; 2829fa7d9493SBabu Moger } 2830fa7d9493SBabu Moger 2831fa7d9493SBabu Moger static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, 2832fa7d9493SBabu Moger const char *name, umode_t mode, 2833fa7d9493SBabu Moger enum rdt_group_type rtype, struct rdtgroup **r) 2834fa7d9493SBabu Moger { 2835fa7d9493SBabu Moger struct rdtgroup *prdtgrp, *rdtgrp; 2836fa7d9493SBabu Moger struct kernfs_node *kn; 2837fa7d9493SBabu Moger uint files = 0; 2838fa7d9493SBabu Moger int ret; 2839fa7d9493SBabu Moger 2840334b0f4eSXiaochen Shen prdtgrp = rdtgroup_kn_lock_live(parent_kn); 2841fa7d9493SBabu Moger if (!prdtgrp) { 2842fa7d9493SBabu Moger ret = -ENODEV; 2843fa7d9493SBabu Moger goto out_unlock; 2844fa7d9493SBabu Moger } 2845fa7d9493SBabu Moger 2846fa7d9493SBabu Moger if (rtype == RDTMON_GROUP && 2847fa7d9493SBabu Moger (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2848fa7d9493SBabu Moger prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) { 2849fa7d9493SBabu Moger ret = -EINVAL; 2850723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 2851fa7d9493SBabu Moger goto out_unlock; 2852fa7d9493SBabu Moger } 2853fa7d9493SBabu Moger 2854fa7d9493SBabu Moger /* allocate the rdtgroup. */ 2855fa7d9493SBabu Moger rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); 2856fa7d9493SBabu Moger if (!rdtgrp) { 2857fa7d9493SBabu Moger ret = -ENOSPC; 2858723f1a0dSBabu Moger rdt_last_cmd_puts("Kernel out of memory\n"); 2859fa7d9493SBabu Moger goto out_unlock; 2860fa7d9493SBabu Moger } 2861fa7d9493SBabu Moger *r = rdtgrp; 2862fa7d9493SBabu Moger rdtgrp->mon.parent = prdtgrp; 2863fa7d9493SBabu Moger rdtgrp->type = rtype; 2864fa7d9493SBabu Moger INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); 2865fa7d9493SBabu Moger 2866fa7d9493SBabu Moger /* kernfs creates the directory for rdtgrp */ 2867fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); 2868fa7d9493SBabu Moger if (IS_ERR(kn)) { 2869fa7d9493SBabu Moger ret = PTR_ERR(kn); 2870fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs create error\n"); 2871fa7d9493SBabu Moger goto out_free_rgrp; 2872fa7d9493SBabu Moger } 2873fa7d9493SBabu Moger rdtgrp->kn = kn; 2874fa7d9493SBabu Moger 2875fa7d9493SBabu Moger /* 2876fa7d9493SBabu Moger * kernfs_remove() will drop the reference count on "kn" which 2877fa7d9493SBabu Moger * will free it. But we still need it to stick around for the 2878fd8d9db3SXiaochen Shen * rdtgroup_kn_unlock(kn) call. Take one extra reference here, 287975899924SXiaochen Shen * which will be dropped by kernfs_put() in rdtgroup_remove(). 2880fa7d9493SBabu Moger */ 2881fa7d9493SBabu Moger kernfs_get(kn); 2882fa7d9493SBabu Moger 2883fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2884fa7d9493SBabu Moger if (ret) { 2885fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs perm error\n"); 2886fa7d9493SBabu Moger goto out_destroy; 2887fa7d9493SBabu Moger } 2888fa7d9493SBabu Moger 2889fa7d9493SBabu Moger files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype); 2890fa7d9493SBabu Moger ret = rdtgroup_add_files(kn, files); 2891fa7d9493SBabu Moger if (ret) { 2892fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs fill error\n"); 2893fa7d9493SBabu Moger goto out_destroy; 2894fa7d9493SBabu Moger } 2895fa7d9493SBabu Moger 2896fa7d9493SBabu Moger if (rdt_mon_capable) { 2897fa7d9493SBabu Moger ret = alloc_rmid(); 2898fa7d9493SBabu Moger if (ret < 0) { 2899723f1a0dSBabu Moger rdt_last_cmd_puts("Out of RMIDs\n"); 2900fa7d9493SBabu Moger goto out_destroy; 2901fa7d9493SBabu Moger } 2902fa7d9493SBabu Moger rdtgrp->mon.rmid = ret; 2903fa7d9493SBabu Moger 2904fa7d9493SBabu Moger ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn); 2905fa7d9493SBabu Moger if (ret) { 2906fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs subdir error\n"); 2907fa7d9493SBabu Moger goto out_idfree; 2908fa7d9493SBabu Moger } 2909fa7d9493SBabu Moger } 2910fa7d9493SBabu Moger kernfs_activate(kn); 2911fa7d9493SBabu Moger 2912fa7d9493SBabu Moger /* 2913334b0f4eSXiaochen Shen * The caller unlocks the parent_kn upon success. 2914fa7d9493SBabu Moger */ 2915fa7d9493SBabu Moger return 0; 2916fa7d9493SBabu Moger 2917fa7d9493SBabu Moger out_idfree: 2918fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2919fa7d9493SBabu Moger out_destroy: 292075899924SXiaochen Shen kernfs_put(rdtgrp->kn); 2921fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2922fa7d9493SBabu Moger out_free_rgrp: 2923fa7d9493SBabu Moger kfree(rdtgrp); 2924fa7d9493SBabu Moger out_unlock: 2925334b0f4eSXiaochen Shen rdtgroup_kn_unlock(parent_kn); 2926fa7d9493SBabu Moger return ret; 2927fa7d9493SBabu Moger } 2928fa7d9493SBabu Moger 2929fa7d9493SBabu Moger static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) 2930fa7d9493SBabu Moger { 2931fa7d9493SBabu Moger kernfs_remove(rgrp->kn); 2932fa7d9493SBabu Moger free_rmid(rgrp->mon.rmid); 293375899924SXiaochen Shen rdtgroup_remove(rgrp); 2934fa7d9493SBabu Moger } 2935fa7d9493SBabu Moger 2936fa7d9493SBabu Moger /* 2937fa7d9493SBabu Moger * Create a monitor group under "mon_groups" directory of a control 2938fa7d9493SBabu Moger * and monitor group(ctrl_mon). This is a resource group 2939fa7d9493SBabu Moger * to monitor a subset of tasks and cpus in its parent ctrl_mon group. 2940fa7d9493SBabu Moger */ 2941fa7d9493SBabu Moger static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, 294232ada3b9SXiaochen Shen const char *name, umode_t mode) 2943fa7d9493SBabu Moger { 2944fa7d9493SBabu Moger struct rdtgroup *rdtgrp, *prgrp; 2945fa7d9493SBabu Moger int ret; 2946fa7d9493SBabu Moger 294732ada3b9SXiaochen Shen ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp); 2948fa7d9493SBabu Moger if (ret) 2949fa7d9493SBabu Moger return ret; 2950fa7d9493SBabu Moger 2951fa7d9493SBabu Moger prgrp = rdtgrp->mon.parent; 2952fa7d9493SBabu Moger rdtgrp->closid = prgrp->closid; 2953fa7d9493SBabu Moger 2954fa7d9493SBabu Moger /* 2955fa7d9493SBabu Moger * Add the rdtgrp to the list of rdtgrps the parent 2956fa7d9493SBabu Moger * ctrl_mon group has to track. 2957fa7d9493SBabu Moger */ 2958fa7d9493SBabu Moger list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); 2959fa7d9493SBabu Moger 2960334b0f4eSXiaochen Shen rdtgroup_kn_unlock(parent_kn); 2961fa7d9493SBabu Moger return ret; 2962fa7d9493SBabu Moger } 2963fa7d9493SBabu Moger 2964fa7d9493SBabu Moger /* 2965fa7d9493SBabu Moger * These are rdtgroups created under the root directory. Can be used 2966fa7d9493SBabu Moger * to allocate and monitor resources. 2967fa7d9493SBabu Moger */ 2968fa7d9493SBabu Moger static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, 2969fa7d9493SBabu Moger const char *name, umode_t mode) 2970fa7d9493SBabu Moger { 2971fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 2972fa7d9493SBabu Moger struct kernfs_node *kn; 2973fa7d9493SBabu Moger u32 closid; 2974fa7d9493SBabu Moger int ret; 2975fa7d9493SBabu Moger 297632ada3b9SXiaochen Shen ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp); 2977fa7d9493SBabu Moger if (ret) 2978fa7d9493SBabu Moger return ret; 2979fa7d9493SBabu Moger 2980fa7d9493SBabu Moger kn = rdtgrp->kn; 2981fa7d9493SBabu Moger ret = closid_alloc(); 2982fa7d9493SBabu Moger if (ret < 0) { 2983723f1a0dSBabu Moger rdt_last_cmd_puts("Out of CLOSIDs\n"); 2984fa7d9493SBabu Moger goto out_common_fail; 2985fa7d9493SBabu Moger } 2986fa7d9493SBabu Moger closid = ret; 2987fa7d9493SBabu Moger ret = 0; 2988fa7d9493SBabu Moger 2989fa7d9493SBabu Moger rdtgrp->closid = closid; 2990fa7d9493SBabu Moger ret = rdtgroup_init_alloc(rdtgrp); 2991fa7d9493SBabu Moger if (ret < 0) 2992fa7d9493SBabu Moger goto out_id_free; 2993fa7d9493SBabu Moger 2994fa7d9493SBabu Moger list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); 2995fa7d9493SBabu Moger 2996fa7d9493SBabu Moger if (rdt_mon_capable) { 2997fa7d9493SBabu Moger /* 2998fa7d9493SBabu Moger * Create an empty mon_groups directory to hold the subset 2999fa7d9493SBabu Moger * of tasks and cpus to monitor. 3000fa7d9493SBabu Moger */ 3001334b0f4eSXiaochen Shen ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL); 3002fa7d9493SBabu Moger if (ret) { 3003fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs subdir error\n"); 3004fa7d9493SBabu Moger goto out_del_list; 3005fa7d9493SBabu Moger } 3006fa7d9493SBabu Moger } 3007fa7d9493SBabu Moger 3008fa7d9493SBabu Moger goto out_unlock; 3009fa7d9493SBabu Moger 3010fa7d9493SBabu Moger out_del_list: 3011fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 3012fa7d9493SBabu Moger out_id_free: 3013fa7d9493SBabu Moger closid_free(closid); 3014fa7d9493SBabu Moger out_common_fail: 3015fa7d9493SBabu Moger mkdir_rdt_prepare_clean(rdtgrp); 3016fa7d9493SBabu Moger out_unlock: 3017334b0f4eSXiaochen Shen rdtgroup_kn_unlock(parent_kn); 3018fa7d9493SBabu Moger return ret; 3019fa7d9493SBabu Moger } 3020fa7d9493SBabu Moger 3021fa7d9493SBabu Moger /* 3022fa7d9493SBabu Moger * We allow creating mon groups only with in a directory called "mon_groups" 3023fa7d9493SBabu Moger * which is present in every ctrl_mon group. Check if this is a valid 3024fa7d9493SBabu Moger * "mon_groups" directory. 3025fa7d9493SBabu Moger * 3026fa7d9493SBabu Moger * 1. The directory should be named "mon_groups". 3027fa7d9493SBabu Moger * 2. The mon group itself should "not" be named "mon_groups". 3028fa7d9493SBabu Moger * This makes sure "mon_groups" directory always has a ctrl_mon group 3029fa7d9493SBabu Moger * as parent. 3030fa7d9493SBabu Moger */ 3031fa7d9493SBabu Moger static bool is_mon_groups(struct kernfs_node *kn, const char *name) 3032fa7d9493SBabu Moger { 3033fa7d9493SBabu Moger return (!strcmp(kn->name, "mon_groups") && 3034fa7d9493SBabu Moger strcmp(name, "mon_groups")); 3035fa7d9493SBabu Moger } 3036fa7d9493SBabu Moger 3037fa7d9493SBabu Moger static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, 3038fa7d9493SBabu Moger umode_t mode) 3039fa7d9493SBabu Moger { 3040fa7d9493SBabu Moger /* Do not accept '\n' to avoid unparsable situation. */ 3041fa7d9493SBabu Moger if (strchr(name, '\n')) 3042fa7d9493SBabu Moger return -EINVAL; 3043fa7d9493SBabu Moger 3044fa7d9493SBabu Moger /* 3045fa7d9493SBabu Moger * If the parent directory is the root directory and RDT 3046fa7d9493SBabu Moger * allocation is supported, add a control and monitoring 3047fa7d9493SBabu Moger * subdirectory 3048fa7d9493SBabu Moger */ 3049fa7d9493SBabu Moger if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn) 305032ada3b9SXiaochen Shen return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode); 3051fa7d9493SBabu Moger 3052fa7d9493SBabu Moger /* 3053fa7d9493SBabu Moger * If RDT monitoring is supported and the parent directory is a valid 3054fa7d9493SBabu Moger * "mon_groups" directory, add a monitoring subdirectory. 3055fa7d9493SBabu Moger */ 3056fa7d9493SBabu Moger if (rdt_mon_capable && is_mon_groups(parent_kn, name)) 305732ada3b9SXiaochen Shen return rdtgroup_mkdir_mon(parent_kn, name, mode); 3058fa7d9493SBabu Moger 3059fa7d9493SBabu Moger return -EPERM; 3060fa7d9493SBabu Moger } 3061fa7d9493SBabu Moger 306219eb86a7SXiaochen Shen static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) 3063fa7d9493SBabu Moger { 3064fa7d9493SBabu Moger struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 3065fa7d9493SBabu Moger int cpu; 3066fa7d9493SBabu Moger 3067fa7d9493SBabu Moger /* Give any tasks back to the parent group */ 3068fa7d9493SBabu Moger rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); 3069fa7d9493SBabu Moger 3070fa7d9493SBabu Moger /* Update per cpu rmid of the moved CPUs first */ 3071fa7d9493SBabu Moger for_each_cpu(cpu, &rdtgrp->cpu_mask) 3072fa7d9493SBabu Moger per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid; 3073fa7d9493SBabu Moger /* 3074fa7d9493SBabu Moger * Update the MSR on moved CPUs and CPUs which have moved 3075fa7d9493SBabu Moger * task running on them. 3076fa7d9493SBabu Moger */ 3077fa7d9493SBabu Moger cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 3078fa7d9493SBabu Moger update_closid_rmid(tmpmask, NULL); 3079fa7d9493SBabu Moger 3080fa7d9493SBabu Moger rdtgrp->flags = RDT_DELETED; 3081fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 3082fa7d9493SBabu Moger 3083fa7d9493SBabu Moger /* 3084fa7d9493SBabu Moger * Remove the rdtgrp from the parent ctrl_mon group's list 3085fa7d9493SBabu Moger */ 3086fa7d9493SBabu Moger WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 3087fa7d9493SBabu Moger list_del(&rdtgrp->mon.crdtgrp_list); 3088fa7d9493SBabu Moger 3089fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 3090fa7d9493SBabu Moger 3091fa7d9493SBabu Moger return 0; 3092fa7d9493SBabu Moger } 3093fa7d9493SBabu Moger 309419eb86a7SXiaochen Shen static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp) 3095fa7d9493SBabu Moger { 3096fa7d9493SBabu Moger rdtgrp->flags = RDT_DELETED; 3097fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 3098fa7d9493SBabu Moger 3099fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 3100fa7d9493SBabu Moger return 0; 3101fa7d9493SBabu Moger } 3102fa7d9493SBabu Moger 310319eb86a7SXiaochen Shen static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) 3104fa7d9493SBabu Moger { 3105fa7d9493SBabu Moger int cpu; 3106fa7d9493SBabu Moger 3107fa7d9493SBabu Moger /* Give any tasks back to the default group */ 3108fa7d9493SBabu Moger rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); 3109fa7d9493SBabu Moger 3110fa7d9493SBabu Moger /* Give any CPUs back to the default group */ 3111fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 3112fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 3113fa7d9493SBabu Moger 3114fa7d9493SBabu Moger /* Update per cpu closid and rmid of the moved CPUs first */ 3115fa7d9493SBabu Moger for_each_cpu(cpu, &rdtgrp->cpu_mask) { 3116fa7d9493SBabu Moger per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid; 3117fa7d9493SBabu Moger per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid; 3118fa7d9493SBabu Moger } 3119fa7d9493SBabu Moger 3120fa7d9493SBabu Moger /* 3121fa7d9493SBabu Moger * Update the MSR on moved CPUs and CPUs which have moved 3122fa7d9493SBabu Moger * task running on them. 3123fa7d9493SBabu Moger */ 3124fa7d9493SBabu Moger cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 3125fa7d9493SBabu Moger update_closid_rmid(tmpmask, NULL); 3126fa7d9493SBabu Moger 3127fa7d9493SBabu Moger closid_free(rdtgrp->closid); 3128fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 3129fa7d9493SBabu Moger 313019eb86a7SXiaochen Shen rdtgroup_ctrl_remove(rdtgrp); 3131074fadeeSXiaochen Shen 3132fa7d9493SBabu Moger /* 3133fa7d9493SBabu Moger * Free all the child monitor group rmids. 3134fa7d9493SBabu Moger */ 3135fa7d9493SBabu Moger free_all_child_rdtgrp(rdtgrp); 3136fa7d9493SBabu Moger 3137fa7d9493SBabu Moger return 0; 3138fa7d9493SBabu Moger } 3139fa7d9493SBabu Moger 3140fa7d9493SBabu Moger static int rdtgroup_rmdir(struct kernfs_node *kn) 3141fa7d9493SBabu Moger { 3142fa7d9493SBabu Moger struct kernfs_node *parent_kn = kn->parent; 3143fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 3144fa7d9493SBabu Moger cpumask_var_t tmpmask; 3145fa7d9493SBabu Moger int ret = 0; 3146fa7d9493SBabu Moger 3147fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 3148fa7d9493SBabu Moger return -ENOMEM; 3149fa7d9493SBabu Moger 3150fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(kn); 3151fa7d9493SBabu Moger if (!rdtgrp) { 3152fa7d9493SBabu Moger ret = -EPERM; 3153fa7d9493SBabu Moger goto out; 3154fa7d9493SBabu Moger } 3155fa7d9493SBabu Moger 3156fa7d9493SBabu Moger /* 3157fa7d9493SBabu Moger * If the rdtgroup is a ctrl_mon group and parent directory 3158fa7d9493SBabu Moger * is the root directory, remove the ctrl_mon group. 3159fa7d9493SBabu Moger * 3160fa7d9493SBabu Moger * If the rdtgroup is a mon group and parent directory 3161fa7d9493SBabu Moger * is a valid "mon_groups" directory, remove the mon group. 3162fa7d9493SBabu Moger */ 3163b0151da5SReinette Chatre if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn && 3164b0151da5SReinette Chatre rdtgrp != &rdtgroup_default) { 3165fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 3166fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 316719eb86a7SXiaochen Shen ret = rdtgroup_ctrl_remove(rdtgrp); 3168fa7d9493SBabu Moger } else { 316919eb86a7SXiaochen Shen ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask); 3170fa7d9493SBabu Moger } 3171fa7d9493SBabu Moger } else if (rdtgrp->type == RDTMON_GROUP && 3172fa7d9493SBabu Moger is_mon_groups(parent_kn, kn->name)) { 317319eb86a7SXiaochen Shen ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask); 3174fa7d9493SBabu Moger } else { 3175fa7d9493SBabu Moger ret = -EPERM; 3176fa7d9493SBabu Moger } 3177fa7d9493SBabu Moger 3178fa7d9493SBabu Moger out: 3179fa7d9493SBabu Moger rdtgroup_kn_unlock(kn); 3180fa7d9493SBabu Moger free_cpumask_var(tmpmask); 3181fa7d9493SBabu Moger return ret; 3182fa7d9493SBabu Moger } 3183fa7d9493SBabu Moger 3184fa7d9493SBabu Moger static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) 3185fa7d9493SBabu Moger { 3186c091e907SJames Morse if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3)) 3187fa7d9493SBabu Moger seq_puts(seq, ",cdp"); 3188fa7d9493SBabu Moger 3189c091e907SJames Morse if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2)) 3190fa7d9493SBabu Moger seq_puts(seq, ",cdpl2"); 3191fa7d9493SBabu Moger 319263c8b123SJames Morse if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl)) 3193fa7d9493SBabu Moger seq_puts(seq, ",mba_MBps"); 3194fa7d9493SBabu Moger 3195fa7d9493SBabu Moger return 0; 3196fa7d9493SBabu Moger } 3197fa7d9493SBabu Moger 3198fa7d9493SBabu Moger static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { 3199fa7d9493SBabu Moger .mkdir = rdtgroup_mkdir, 3200fa7d9493SBabu Moger .rmdir = rdtgroup_rmdir, 3201fa7d9493SBabu Moger .show_options = rdtgroup_show_options, 3202fa7d9493SBabu Moger }; 3203fa7d9493SBabu Moger 3204fa7d9493SBabu Moger static int __init rdtgroup_setup_root(void) 3205fa7d9493SBabu Moger { 3206fa7d9493SBabu Moger int ret; 3207fa7d9493SBabu Moger 3208fa7d9493SBabu Moger rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, 3209fa7d9493SBabu Moger KERNFS_ROOT_CREATE_DEACTIVATED | 3210fa7d9493SBabu Moger KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, 3211fa7d9493SBabu Moger &rdtgroup_default); 3212fa7d9493SBabu Moger if (IS_ERR(rdt_root)) 3213fa7d9493SBabu Moger return PTR_ERR(rdt_root); 3214fa7d9493SBabu Moger 3215fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 3216fa7d9493SBabu Moger 3217fa7d9493SBabu Moger rdtgroup_default.closid = 0; 3218fa7d9493SBabu Moger rdtgroup_default.mon.rmid = 0; 3219fa7d9493SBabu Moger rdtgroup_default.type = RDTCTRL_GROUP; 3220fa7d9493SBabu Moger INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); 3221fa7d9493SBabu Moger 3222fa7d9493SBabu Moger list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); 3223fa7d9493SBabu Moger 3224f2eb478fSGreg Kroah-Hartman ret = rdtgroup_add_files(kernfs_root_to_node(rdt_root), RF_CTRL_BASE); 3225fa7d9493SBabu Moger if (ret) { 3226fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3227fa7d9493SBabu Moger goto out; 3228fa7d9493SBabu Moger } 3229fa7d9493SBabu Moger 3230f2eb478fSGreg Kroah-Hartman rdtgroup_default.kn = kernfs_root_to_node(rdt_root); 3231fa7d9493SBabu Moger kernfs_activate(rdtgroup_default.kn); 3232fa7d9493SBabu Moger 3233fa7d9493SBabu Moger out: 3234fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 3235fa7d9493SBabu Moger 3236fa7d9493SBabu Moger return ret; 3237fa7d9493SBabu Moger } 3238fa7d9493SBabu Moger 3239fa7d9493SBabu Moger /* 3240fa7d9493SBabu Moger * rdtgroup_init - rdtgroup initialization 3241fa7d9493SBabu Moger * 3242fa7d9493SBabu Moger * Setup resctrl file system including set up root, create mount point, 3243fa7d9493SBabu Moger * register rdtgroup filesystem, and initialize files under root directory. 3244fa7d9493SBabu Moger * 3245fa7d9493SBabu Moger * Return: 0 on success or -errno 3246fa7d9493SBabu Moger */ 3247fa7d9493SBabu Moger int __init rdtgroup_init(void) 3248fa7d9493SBabu Moger { 3249fa7d9493SBabu Moger int ret = 0; 3250fa7d9493SBabu Moger 3251fa7d9493SBabu Moger seq_buf_init(&last_cmd_status, last_cmd_status_buf, 3252fa7d9493SBabu Moger sizeof(last_cmd_status_buf)); 3253fa7d9493SBabu Moger 3254fa7d9493SBabu Moger ret = rdtgroup_setup_root(); 3255fa7d9493SBabu Moger if (ret) 3256fa7d9493SBabu Moger return ret; 3257fa7d9493SBabu Moger 3258fa7d9493SBabu Moger ret = sysfs_create_mount_point(fs_kobj, "resctrl"); 3259fa7d9493SBabu Moger if (ret) 3260fa7d9493SBabu Moger goto cleanup_root; 3261fa7d9493SBabu Moger 3262fa7d9493SBabu Moger ret = register_filesystem(&rdt_fs_type); 3263fa7d9493SBabu Moger if (ret) 3264fa7d9493SBabu Moger goto cleanup_mountpoint; 3265fa7d9493SBabu Moger 3266fa7d9493SBabu Moger /* 3267fa7d9493SBabu Moger * Adding the resctrl debugfs directory here may not be ideal since 3268fa7d9493SBabu Moger * it would let the resctrl debugfs directory appear on the debugfs 3269fa7d9493SBabu Moger * filesystem before the resctrl filesystem is mounted. 3270fa7d9493SBabu Moger * It may also be ok since that would enable debugging of RDT before 3271fa7d9493SBabu Moger * resctrl is mounted. 3272fa7d9493SBabu Moger * The reason why the debugfs directory is created here and not in 3273ae0fbeddSJames Morse * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and 3274fa7d9493SBabu Moger * during the debugfs directory creation also &sb->s_type->i_mutex_key 3275fa7d9493SBabu Moger * (the lockdep class of inode->i_rwsem). Other filesystem 3276fa7d9493SBabu Moger * interactions (eg. SyS_getdents) have the lock ordering: 3277c1e8d7c6SMichel Lespinasse * &sb->s_type->i_mutex_key --> &mm->mmap_lock 3278c1e8d7c6SMichel Lespinasse * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex 3279fa7d9493SBabu Moger * is taken, thus creating dependency: 3280c1e8d7c6SMichel Lespinasse * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause 3281fa7d9493SBabu Moger * issues considering the other two lock dependencies. 3282fa7d9493SBabu Moger * By creating the debugfs directory here we avoid a dependency 3283fa7d9493SBabu Moger * that may cause deadlock (even though file operations cannot 3284fa7d9493SBabu Moger * occur until the filesystem is mounted, but I do not know how to 3285fa7d9493SBabu Moger * tell lockdep that). 3286fa7d9493SBabu Moger */ 3287fa7d9493SBabu Moger debugfs_resctrl = debugfs_create_dir("resctrl", NULL); 3288fa7d9493SBabu Moger 3289fa7d9493SBabu Moger return 0; 3290fa7d9493SBabu Moger 3291fa7d9493SBabu Moger cleanup_mountpoint: 3292fa7d9493SBabu Moger sysfs_remove_mount_point(fs_kobj, "resctrl"); 3293fa7d9493SBabu Moger cleanup_root: 3294fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3295fa7d9493SBabu Moger 3296fa7d9493SBabu Moger return ret; 3297fa7d9493SBabu Moger } 3298fa7d9493SBabu Moger 3299fa7d9493SBabu Moger void __exit rdtgroup_exit(void) 3300fa7d9493SBabu Moger { 3301fa7d9493SBabu Moger debugfs_remove_recursive(debugfs_resctrl); 3302fa7d9493SBabu Moger unregister_filesystem(&rdt_fs_type); 3303fa7d9493SBabu Moger sysfs_remove_mount_point(fs_kobj, "resctrl"); 3304fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3305fa7d9493SBabu Moger } 3306