12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2fa7d9493SBabu Moger /* 3fa7d9493SBabu Moger * User interface for Resource Alloction 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 42fa7d9493SBabu Moger /* Kernel fs node for "info" directory under root */ 43fa7d9493SBabu Moger static struct kernfs_node *kn_info; 44fa7d9493SBabu Moger 45fa7d9493SBabu Moger /* Kernel fs node for "mon_groups" directory under root */ 46fa7d9493SBabu Moger static struct kernfs_node *kn_mongrp; 47fa7d9493SBabu Moger 48fa7d9493SBabu Moger /* Kernel fs node for "mon_data" directory under root */ 49fa7d9493SBabu Moger static struct kernfs_node *kn_mondata; 50fa7d9493SBabu Moger 51fa7d9493SBabu Moger static struct seq_buf last_cmd_status; 52fa7d9493SBabu Moger static char last_cmd_status_buf[512]; 53fa7d9493SBabu Moger 54fa7d9493SBabu Moger struct dentry *debugfs_resctrl; 55fa7d9493SBabu Moger 56fa7d9493SBabu Moger void rdt_last_cmd_clear(void) 57fa7d9493SBabu Moger { 58fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 59fa7d9493SBabu Moger seq_buf_clear(&last_cmd_status); 60fa7d9493SBabu Moger } 61fa7d9493SBabu Moger 62fa7d9493SBabu Moger void rdt_last_cmd_puts(const char *s) 63fa7d9493SBabu Moger { 64fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 65fa7d9493SBabu Moger seq_buf_puts(&last_cmd_status, s); 66fa7d9493SBabu Moger } 67fa7d9493SBabu Moger 68fa7d9493SBabu Moger void rdt_last_cmd_printf(const char *fmt, ...) 69fa7d9493SBabu Moger { 70fa7d9493SBabu Moger va_list ap; 71fa7d9493SBabu Moger 72fa7d9493SBabu Moger va_start(ap, fmt); 73fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 74fa7d9493SBabu Moger seq_buf_vprintf(&last_cmd_status, fmt, ap); 75fa7d9493SBabu Moger va_end(ap); 76fa7d9493SBabu Moger } 77fa7d9493SBabu Moger 78fa7d9493SBabu Moger /* 79fa7d9493SBabu Moger * Trivial allocator for CLOSIDs. Since h/w only supports a small number, 80fa7d9493SBabu Moger * we can keep a bitmap of free CLOSIDs in a single integer. 81fa7d9493SBabu Moger * 82fa7d9493SBabu Moger * Using a global CLOSID across all resources has some advantages and 83fa7d9493SBabu Moger * some drawbacks: 84fa7d9493SBabu Moger * + We can simply set "current->closid" to assign a task to a resource 85fa7d9493SBabu Moger * group. 86fa7d9493SBabu Moger * + Context switch code can avoid extra memory references deciding which 87fa7d9493SBabu Moger * CLOSID to load into the PQR_ASSOC MSR 88fa7d9493SBabu Moger * - We give up some options in configuring resource groups across multi-socket 89fa7d9493SBabu Moger * systems. 90fa7d9493SBabu Moger * - Our choices on how to configure each resource become progressively more 91fa7d9493SBabu Moger * limited as the number of resources grows. 92fa7d9493SBabu Moger */ 93fa7d9493SBabu Moger static int closid_free_map; 94fa7d9493SBabu Moger static int closid_free_map_len; 95fa7d9493SBabu Moger 96fa7d9493SBabu Moger int closids_supported(void) 97fa7d9493SBabu Moger { 98fa7d9493SBabu Moger return closid_free_map_len; 99fa7d9493SBabu Moger } 100fa7d9493SBabu Moger 101fa7d9493SBabu Moger static void closid_init(void) 102fa7d9493SBabu Moger { 103fa7d9493SBabu Moger struct rdt_resource *r; 104fa7d9493SBabu Moger int rdt_min_closid = 32; 105fa7d9493SBabu Moger 106fa7d9493SBabu Moger /* Compute rdt_min_closid across all resources */ 107fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) 108fa7d9493SBabu Moger rdt_min_closid = min(rdt_min_closid, r->num_closid); 109fa7d9493SBabu Moger 110fa7d9493SBabu Moger closid_free_map = BIT_MASK(rdt_min_closid) - 1; 111fa7d9493SBabu Moger 112fa7d9493SBabu Moger /* CLOSID 0 is always reserved for the default group */ 113fa7d9493SBabu Moger closid_free_map &= ~1; 114fa7d9493SBabu Moger closid_free_map_len = rdt_min_closid; 115fa7d9493SBabu Moger } 116fa7d9493SBabu Moger 117fa7d9493SBabu Moger static int closid_alloc(void) 118fa7d9493SBabu Moger { 119fa7d9493SBabu Moger u32 closid = ffs(closid_free_map); 120fa7d9493SBabu Moger 121fa7d9493SBabu Moger if (closid == 0) 122fa7d9493SBabu Moger return -ENOSPC; 123fa7d9493SBabu Moger closid--; 124fa7d9493SBabu Moger closid_free_map &= ~(1 << closid); 125fa7d9493SBabu Moger 126fa7d9493SBabu Moger return closid; 127fa7d9493SBabu Moger } 128fa7d9493SBabu Moger 129fa7d9493SBabu Moger void closid_free(int closid) 130fa7d9493SBabu Moger { 131fa7d9493SBabu Moger closid_free_map |= 1 << closid; 132fa7d9493SBabu Moger } 133fa7d9493SBabu Moger 134fa7d9493SBabu Moger /** 135fa7d9493SBabu Moger * closid_allocated - test if provided closid is in use 136fa7d9493SBabu Moger * @closid: closid to be tested 137fa7d9493SBabu Moger * 138fa7d9493SBabu Moger * Return: true if @closid is currently associated with a resource group, 139fa7d9493SBabu Moger * false if @closid is free 140fa7d9493SBabu Moger */ 141fa7d9493SBabu Moger static bool closid_allocated(unsigned int closid) 142fa7d9493SBabu Moger { 143fa7d9493SBabu Moger return (closid_free_map & (1 << closid)) == 0; 144fa7d9493SBabu Moger } 145fa7d9493SBabu Moger 146fa7d9493SBabu Moger /** 147fa7d9493SBabu Moger * rdtgroup_mode_by_closid - Return mode of resource group with closid 148fa7d9493SBabu Moger * @closid: closid if the resource group 149fa7d9493SBabu Moger * 150fa7d9493SBabu Moger * Each resource group is associated with a @closid. Here the mode 151fa7d9493SBabu Moger * of a resource group can be queried by searching for it using its closid. 152fa7d9493SBabu Moger * 153fa7d9493SBabu Moger * Return: mode as &enum rdtgrp_mode of resource group with closid @closid 154fa7d9493SBabu Moger */ 155fa7d9493SBabu Moger enum rdtgrp_mode rdtgroup_mode_by_closid(int closid) 156fa7d9493SBabu Moger { 157fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 158fa7d9493SBabu Moger 159fa7d9493SBabu Moger list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { 160fa7d9493SBabu Moger if (rdtgrp->closid == closid) 161fa7d9493SBabu Moger return rdtgrp->mode; 162fa7d9493SBabu Moger } 163fa7d9493SBabu Moger 164fa7d9493SBabu Moger return RDT_NUM_MODES; 165fa7d9493SBabu Moger } 166fa7d9493SBabu Moger 167fa7d9493SBabu Moger static const char * const rdt_mode_str[] = { 168fa7d9493SBabu Moger [RDT_MODE_SHAREABLE] = "shareable", 169fa7d9493SBabu Moger [RDT_MODE_EXCLUSIVE] = "exclusive", 170fa7d9493SBabu Moger [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup", 171fa7d9493SBabu Moger [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked", 172fa7d9493SBabu Moger }; 173fa7d9493SBabu Moger 174fa7d9493SBabu Moger /** 175fa7d9493SBabu Moger * rdtgroup_mode_str - Return the string representation of mode 176fa7d9493SBabu Moger * @mode: the resource group mode as &enum rdtgroup_mode 177fa7d9493SBabu Moger * 178fa7d9493SBabu Moger * Return: string representation of valid mode, "unknown" otherwise 179fa7d9493SBabu Moger */ 180fa7d9493SBabu Moger static const char *rdtgroup_mode_str(enum rdtgrp_mode mode) 181fa7d9493SBabu Moger { 182fa7d9493SBabu Moger if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES) 183fa7d9493SBabu Moger return "unknown"; 184fa7d9493SBabu Moger 185fa7d9493SBabu Moger return rdt_mode_str[mode]; 186fa7d9493SBabu Moger } 187fa7d9493SBabu Moger 188fa7d9493SBabu Moger /* set uid and gid of rdtgroup dirs and files to that of the creator */ 189fa7d9493SBabu Moger static int rdtgroup_kn_set_ugid(struct kernfs_node *kn) 190fa7d9493SBabu Moger { 191fa7d9493SBabu Moger struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, 192fa7d9493SBabu Moger .ia_uid = current_fsuid(), 193fa7d9493SBabu Moger .ia_gid = current_fsgid(), }; 194fa7d9493SBabu Moger 195fa7d9493SBabu Moger if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && 196fa7d9493SBabu Moger gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) 197fa7d9493SBabu Moger return 0; 198fa7d9493SBabu Moger 199fa7d9493SBabu Moger return kernfs_setattr(kn, &iattr); 200fa7d9493SBabu Moger } 201fa7d9493SBabu Moger 202fa7d9493SBabu Moger static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft) 203fa7d9493SBabu Moger { 204fa7d9493SBabu Moger struct kernfs_node *kn; 205fa7d9493SBabu Moger int ret; 206fa7d9493SBabu Moger 207fa7d9493SBabu Moger kn = __kernfs_create_file(parent_kn, rft->name, rft->mode, 208fa7d9493SBabu Moger GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 209fa7d9493SBabu Moger 0, rft->kf_ops, rft, NULL, NULL); 210fa7d9493SBabu Moger if (IS_ERR(kn)) 211fa7d9493SBabu Moger return PTR_ERR(kn); 212fa7d9493SBabu Moger 213fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 214fa7d9493SBabu Moger if (ret) { 215fa7d9493SBabu Moger kernfs_remove(kn); 216fa7d9493SBabu Moger return ret; 217fa7d9493SBabu Moger } 218fa7d9493SBabu Moger 219fa7d9493SBabu Moger return 0; 220fa7d9493SBabu Moger } 221fa7d9493SBabu Moger 222fa7d9493SBabu Moger static int rdtgroup_seqfile_show(struct seq_file *m, void *arg) 223fa7d9493SBabu Moger { 224fa7d9493SBabu Moger struct kernfs_open_file *of = m->private; 225fa7d9493SBabu Moger struct rftype *rft = of->kn->priv; 226fa7d9493SBabu Moger 227fa7d9493SBabu Moger if (rft->seq_show) 228fa7d9493SBabu Moger return rft->seq_show(of, m, arg); 229fa7d9493SBabu Moger return 0; 230fa7d9493SBabu Moger } 231fa7d9493SBabu Moger 232fa7d9493SBabu Moger static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf, 233fa7d9493SBabu Moger size_t nbytes, loff_t off) 234fa7d9493SBabu Moger { 235fa7d9493SBabu Moger struct rftype *rft = of->kn->priv; 236fa7d9493SBabu Moger 237fa7d9493SBabu Moger if (rft->write) 238fa7d9493SBabu Moger return rft->write(of, buf, nbytes, off); 239fa7d9493SBabu Moger 240fa7d9493SBabu Moger return -EINVAL; 241fa7d9493SBabu Moger } 242fa7d9493SBabu Moger 243fa7d9493SBabu Moger static struct kernfs_ops rdtgroup_kf_single_ops = { 244fa7d9493SBabu Moger .atomic_write_len = PAGE_SIZE, 245fa7d9493SBabu Moger .write = rdtgroup_file_write, 246fa7d9493SBabu Moger .seq_show = rdtgroup_seqfile_show, 247fa7d9493SBabu Moger }; 248fa7d9493SBabu Moger 249fa7d9493SBabu Moger static struct kernfs_ops kf_mondata_ops = { 250fa7d9493SBabu Moger .atomic_write_len = PAGE_SIZE, 251fa7d9493SBabu Moger .seq_show = rdtgroup_mondata_show, 252fa7d9493SBabu Moger }; 253fa7d9493SBabu Moger 254fa7d9493SBabu Moger static bool is_cpu_list(struct kernfs_open_file *of) 255fa7d9493SBabu Moger { 256fa7d9493SBabu Moger struct rftype *rft = of->kn->priv; 257fa7d9493SBabu Moger 258fa7d9493SBabu Moger return rft->flags & RFTYPE_FLAGS_CPUS_LIST; 259fa7d9493SBabu Moger } 260fa7d9493SBabu Moger 261fa7d9493SBabu Moger static int rdtgroup_cpus_show(struct kernfs_open_file *of, 262fa7d9493SBabu Moger struct seq_file *s, void *v) 263fa7d9493SBabu Moger { 264fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 265fa7d9493SBabu Moger struct cpumask *mask; 266fa7d9493SBabu Moger int ret = 0; 267fa7d9493SBabu Moger 268fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 269fa7d9493SBabu Moger 270fa7d9493SBabu Moger if (rdtgrp) { 271fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 272fa7d9493SBabu Moger if (!rdtgrp->plr->d) { 273fa7d9493SBabu Moger rdt_last_cmd_clear(); 274fa7d9493SBabu Moger rdt_last_cmd_puts("Cache domain offline\n"); 275fa7d9493SBabu Moger ret = -ENODEV; 276fa7d9493SBabu Moger } else { 277fa7d9493SBabu Moger mask = &rdtgrp->plr->d->cpu_mask; 278fa7d9493SBabu Moger seq_printf(s, is_cpu_list(of) ? 279fa7d9493SBabu Moger "%*pbl\n" : "%*pb\n", 280fa7d9493SBabu Moger cpumask_pr_args(mask)); 281fa7d9493SBabu Moger } 282fa7d9493SBabu Moger } else { 283fa7d9493SBabu Moger seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n", 284fa7d9493SBabu Moger cpumask_pr_args(&rdtgrp->cpu_mask)); 285fa7d9493SBabu Moger } 286fa7d9493SBabu Moger } else { 287fa7d9493SBabu Moger ret = -ENOENT; 288fa7d9493SBabu Moger } 289fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 290fa7d9493SBabu Moger 291fa7d9493SBabu Moger return ret; 292fa7d9493SBabu Moger } 293fa7d9493SBabu Moger 294fa7d9493SBabu Moger /* 295352940ecSBabu Moger * This is safe against resctrl_sched_in() called from __switch_to() 296fa7d9493SBabu Moger * because __switch_to() is executed with interrupts disabled. A local call 297fa7d9493SBabu Moger * from update_closid_rmid() is proteced against __switch_to() because 298fa7d9493SBabu Moger * preemption is disabled. 299fa7d9493SBabu Moger */ 300fa7d9493SBabu Moger static void update_cpu_closid_rmid(void *info) 301fa7d9493SBabu Moger { 302fa7d9493SBabu Moger struct rdtgroup *r = info; 303fa7d9493SBabu Moger 304fa7d9493SBabu Moger if (r) { 305fa7d9493SBabu Moger this_cpu_write(pqr_state.default_closid, r->closid); 306fa7d9493SBabu Moger this_cpu_write(pqr_state.default_rmid, r->mon.rmid); 307fa7d9493SBabu Moger } 308fa7d9493SBabu Moger 309fa7d9493SBabu Moger /* 310fa7d9493SBabu Moger * We cannot unconditionally write the MSR because the current 311fa7d9493SBabu Moger * executing task might have its own closid selected. Just reuse 312fa7d9493SBabu Moger * the context switch code. 313fa7d9493SBabu Moger */ 314352940ecSBabu Moger resctrl_sched_in(); 315fa7d9493SBabu Moger } 316fa7d9493SBabu Moger 317fa7d9493SBabu Moger /* 318fa7d9493SBabu Moger * Update the PGR_ASSOC MSR on all cpus in @cpu_mask, 319fa7d9493SBabu Moger * 320fa7d9493SBabu Moger * Per task closids/rmids must have been set up before calling this function. 321fa7d9493SBabu Moger */ 322fa7d9493SBabu Moger static void 323fa7d9493SBabu Moger update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r) 324fa7d9493SBabu Moger { 325fa7d9493SBabu Moger int cpu = get_cpu(); 326fa7d9493SBabu Moger 327fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 328fa7d9493SBabu Moger update_cpu_closid_rmid(r); 329fa7d9493SBabu Moger smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1); 330fa7d9493SBabu Moger put_cpu(); 331fa7d9493SBabu Moger } 332fa7d9493SBabu Moger 333fa7d9493SBabu Moger static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, 334fa7d9493SBabu Moger cpumask_var_t tmpmask) 335fa7d9493SBabu Moger { 336fa7d9493SBabu Moger struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp; 337fa7d9493SBabu Moger struct list_head *head; 338fa7d9493SBabu Moger 339fa7d9493SBabu Moger /* Check whether cpus belong to parent ctrl group */ 340fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask); 341fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 342723f1a0dSBabu Moger rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n"); 343fa7d9493SBabu Moger return -EINVAL; 344fa7d9493SBabu Moger } 345fa7d9493SBabu Moger 346fa7d9493SBabu Moger /* Check whether cpus are dropped from this group */ 347fa7d9493SBabu Moger cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); 348fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 349fa7d9493SBabu Moger /* Give any dropped cpus to parent rdtgroup */ 350fa7d9493SBabu Moger cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask); 351fa7d9493SBabu Moger update_closid_rmid(tmpmask, prgrp); 352fa7d9493SBabu Moger } 353fa7d9493SBabu Moger 354fa7d9493SBabu Moger /* 355fa7d9493SBabu Moger * If we added cpus, remove them from previous group that owned them 356fa7d9493SBabu Moger * and update per-cpu rmid 357fa7d9493SBabu Moger */ 358fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); 359fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 360fa7d9493SBabu Moger head = &prgrp->mon.crdtgrp_list; 361fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 362fa7d9493SBabu Moger if (crgrp == rdtgrp) 363fa7d9493SBabu Moger continue; 364fa7d9493SBabu Moger cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask, 365fa7d9493SBabu Moger tmpmask); 366fa7d9493SBabu Moger } 367fa7d9493SBabu Moger update_closid_rmid(tmpmask, rdtgrp); 368fa7d9493SBabu Moger } 369fa7d9493SBabu Moger 370fa7d9493SBabu Moger /* Done pushing/pulling - update this group with new mask */ 371fa7d9493SBabu Moger cpumask_copy(&rdtgrp->cpu_mask, newmask); 372fa7d9493SBabu Moger 373fa7d9493SBabu Moger return 0; 374fa7d9493SBabu Moger } 375fa7d9493SBabu Moger 376fa7d9493SBabu Moger static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m) 377fa7d9493SBabu Moger { 378fa7d9493SBabu Moger struct rdtgroup *crgrp; 379fa7d9493SBabu Moger 380fa7d9493SBabu Moger cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m); 381fa7d9493SBabu Moger /* update the child mon group masks as well*/ 382fa7d9493SBabu Moger list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list) 383fa7d9493SBabu Moger cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask); 384fa7d9493SBabu Moger } 385fa7d9493SBabu Moger 386fa7d9493SBabu Moger static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, 387fa7d9493SBabu Moger cpumask_var_t tmpmask, cpumask_var_t tmpmask1) 388fa7d9493SBabu Moger { 389fa7d9493SBabu Moger struct rdtgroup *r, *crgrp; 390fa7d9493SBabu Moger struct list_head *head; 391fa7d9493SBabu Moger 392fa7d9493SBabu Moger /* Check whether cpus are dropped from this group */ 393fa7d9493SBabu Moger cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); 394fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 395fa7d9493SBabu Moger /* Can't drop from default group */ 396fa7d9493SBabu Moger if (rdtgrp == &rdtgroup_default) { 397fa7d9493SBabu Moger rdt_last_cmd_puts("Can't drop CPUs from default group\n"); 398fa7d9493SBabu Moger return -EINVAL; 399fa7d9493SBabu Moger } 400fa7d9493SBabu Moger 401fa7d9493SBabu Moger /* Give any dropped cpus to rdtgroup_default */ 402fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 403fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, tmpmask); 404fa7d9493SBabu Moger update_closid_rmid(tmpmask, &rdtgroup_default); 405fa7d9493SBabu Moger } 406fa7d9493SBabu Moger 407fa7d9493SBabu Moger /* 408fa7d9493SBabu Moger * If we added cpus, remove them from previous group and 409fa7d9493SBabu Moger * the prev group's child groups that owned them 410fa7d9493SBabu Moger * and update per-cpu closid/rmid. 411fa7d9493SBabu Moger */ 412fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); 413fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 414fa7d9493SBabu Moger list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) { 415fa7d9493SBabu Moger if (r == rdtgrp) 416fa7d9493SBabu Moger continue; 417fa7d9493SBabu Moger cpumask_and(tmpmask1, &r->cpu_mask, tmpmask); 418fa7d9493SBabu Moger if (cpumask_weight(tmpmask1)) 419fa7d9493SBabu Moger cpumask_rdtgrp_clear(r, tmpmask1); 420fa7d9493SBabu Moger } 421fa7d9493SBabu Moger update_closid_rmid(tmpmask, rdtgrp); 422fa7d9493SBabu Moger } 423fa7d9493SBabu Moger 424fa7d9493SBabu Moger /* Done pushing/pulling - update this group with new mask */ 425fa7d9493SBabu Moger cpumask_copy(&rdtgrp->cpu_mask, newmask); 426fa7d9493SBabu Moger 427fa7d9493SBabu Moger /* 428fa7d9493SBabu Moger * Clear child mon group masks since there is a new parent mask 429fa7d9493SBabu Moger * now and update the rmid for the cpus the child lost. 430fa7d9493SBabu Moger */ 431fa7d9493SBabu Moger head = &rdtgrp->mon.crdtgrp_list; 432fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 433fa7d9493SBabu Moger cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask); 434fa7d9493SBabu Moger update_closid_rmid(tmpmask, rdtgrp); 435fa7d9493SBabu Moger cpumask_clear(&crgrp->cpu_mask); 436fa7d9493SBabu Moger } 437fa7d9493SBabu Moger 438fa7d9493SBabu Moger return 0; 439fa7d9493SBabu Moger } 440fa7d9493SBabu Moger 441fa7d9493SBabu Moger static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of, 442fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 443fa7d9493SBabu Moger { 444fa7d9493SBabu Moger cpumask_var_t tmpmask, newmask, tmpmask1; 445fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 446fa7d9493SBabu Moger int ret; 447fa7d9493SBabu Moger 448fa7d9493SBabu Moger if (!buf) 449fa7d9493SBabu Moger return -EINVAL; 450fa7d9493SBabu Moger 451fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 452fa7d9493SBabu Moger return -ENOMEM; 453fa7d9493SBabu Moger if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) { 454fa7d9493SBabu Moger free_cpumask_var(tmpmask); 455fa7d9493SBabu Moger return -ENOMEM; 456fa7d9493SBabu Moger } 457fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) { 458fa7d9493SBabu Moger free_cpumask_var(tmpmask); 459fa7d9493SBabu Moger free_cpumask_var(newmask); 460fa7d9493SBabu Moger return -ENOMEM; 461fa7d9493SBabu Moger } 462fa7d9493SBabu Moger 463fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 464fa7d9493SBabu Moger if (!rdtgrp) { 465fa7d9493SBabu Moger ret = -ENOENT; 466fa7d9493SBabu Moger goto unlock; 467fa7d9493SBabu Moger } 468fa7d9493SBabu Moger 469fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 470fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 471fa7d9493SBabu Moger ret = -EINVAL; 472723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 473fa7d9493SBabu Moger goto unlock; 474fa7d9493SBabu Moger } 475fa7d9493SBabu Moger 476fa7d9493SBabu Moger if (is_cpu_list(of)) 477fa7d9493SBabu Moger ret = cpulist_parse(buf, newmask); 478fa7d9493SBabu Moger else 479fa7d9493SBabu Moger ret = cpumask_parse(buf, newmask); 480fa7d9493SBabu Moger 481fa7d9493SBabu Moger if (ret) { 482723f1a0dSBabu Moger rdt_last_cmd_puts("Bad CPU list/mask\n"); 483fa7d9493SBabu Moger goto unlock; 484fa7d9493SBabu Moger } 485fa7d9493SBabu Moger 486fa7d9493SBabu Moger /* check that user didn't specify any offline cpus */ 487fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, cpu_online_mask); 488fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 489fa7d9493SBabu Moger ret = -EINVAL; 490723f1a0dSBabu Moger rdt_last_cmd_puts("Can only assign online CPUs\n"); 491fa7d9493SBabu Moger goto unlock; 492fa7d9493SBabu Moger } 493fa7d9493SBabu Moger 494fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP) 495fa7d9493SBabu Moger ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1); 496fa7d9493SBabu Moger else if (rdtgrp->type == RDTMON_GROUP) 497fa7d9493SBabu Moger ret = cpus_mon_write(rdtgrp, newmask, tmpmask); 498fa7d9493SBabu Moger else 499fa7d9493SBabu Moger ret = -EINVAL; 500fa7d9493SBabu Moger 501fa7d9493SBabu Moger unlock: 502fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 503fa7d9493SBabu Moger free_cpumask_var(tmpmask); 504fa7d9493SBabu Moger free_cpumask_var(newmask); 505fa7d9493SBabu Moger free_cpumask_var(tmpmask1); 506fa7d9493SBabu Moger 507fa7d9493SBabu Moger return ret ?: nbytes; 508fa7d9493SBabu Moger } 509fa7d9493SBabu Moger 510fa7d9493SBabu Moger struct task_move_callback { 511fa7d9493SBabu Moger struct callback_head work; 512fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 513fa7d9493SBabu Moger }; 514fa7d9493SBabu Moger 515fa7d9493SBabu Moger static void move_myself(struct callback_head *head) 516fa7d9493SBabu Moger { 517fa7d9493SBabu Moger struct task_move_callback *callback; 518fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 519fa7d9493SBabu Moger 520fa7d9493SBabu Moger callback = container_of(head, struct task_move_callback, work); 521fa7d9493SBabu Moger rdtgrp = callback->rdtgrp; 522fa7d9493SBabu Moger 523fa7d9493SBabu Moger /* 524fa7d9493SBabu Moger * If resource group was deleted before this task work callback 525fa7d9493SBabu Moger * was invoked, then assign the task to root group and free the 526fa7d9493SBabu Moger * resource group. 527fa7d9493SBabu Moger */ 528fa7d9493SBabu Moger if (atomic_dec_and_test(&rdtgrp->waitcount) && 529fa7d9493SBabu Moger (rdtgrp->flags & RDT_DELETED)) { 530fa7d9493SBabu Moger current->closid = 0; 531fa7d9493SBabu Moger current->rmid = 0; 532fa7d9493SBabu Moger kfree(rdtgrp); 533fa7d9493SBabu Moger } 534fa7d9493SBabu Moger 535dc433797SXiaochen Shen if (unlikely(current->flags & PF_EXITING)) 536dc433797SXiaochen Shen goto out; 537dc433797SXiaochen Shen 538fa7d9493SBabu Moger preempt_disable(); 539fa7d9493SBabu Moger /* update PQR_ASSOC MSR to make resource group go into effect */ 540352940ecSBabu Moger resctrl_sched_in(); 541fa7d9493SBabu Moger preempt_enable(); 542fa7d9493SBabu Moger 543dc433797SXiaochen Shen out: 544fa7d9493SBabu Moger kfree(callback); 545fa7d9493SBabu Moger } 546fa7d9493SBabu Moger 547fa7d9493SBabu Moger static int __rdtgroup_move_task(struct task_struct *tsk, 548fa7d9493SBabu Moger struct rdtgroup *rdtgrp) 549fa7d9493SBabu Moger { 550fa7d9493SBabu Moger struct task_move_callback *callback; 551fa7d9493SBabu Moger int ret; 552fa7d9493SBabu Moger 553fa7d9493SBabu Moger callback = kzalloc(sizeof(*callback), GFP_KERNEL); 554fa7d9493SBabu Moger if (!callback) 555fa7d9493SBabu Moger return -ENOMEM; 556fa7d9493SBabu Moger callback->work.func = move_myself; 557fa7d9493SBabu Moger callback->rdtgrp = rdtgrp; 558fa7d9493SBabu Moger 559fa7d9493SBabu Moger /* 560fa7d9493SBabu Moger * Take a refcount, so rdtgrp cannot be freed before the 561fa7d9493SBabu Moger * callback has been invoked. 562fa7d9493SBabu Moger */ 563fa7d9493SBabu Moger atomic_inc(&rdtgrp->waitcount); 564fa7d9493SBabu Moger ret = task_work_add(tsk, &callback->work, true); 565fa7d9493SBabu Moger if (ret) { 566fa7d9493SBabu Moger /* 567fa7d9493SBabu Moger * Task is exiting. Drop the refcount and free the callback. 568fa7d9493SBabu Moger * No need to check the refcount as the group cannot be 569fa7d9493SBabu Moger * deleted before the write function unlocks rdtgroup_mutex. 570fa7d9493SBabu Moger */ 571fa7d9493SBabu Moger atomic_dec(&rdtgrp->waitcount); 572fa7d9493SBabu Moger kfree(callback); 573723f1a0dSBabu Moger rdt_last_cmd_puts("Task exited\n"); 574fa7d9493SBabu Moger } else { 575fa7d9493SBabu Moger /* 576fa7d9493SBabu Moger * For ctrl_mon groups move both closid and rmid. 577fa7d9493SBabu Moger * For monitor groups, can move the tasks only from 578fa7d9493SBabu Moger * their parent CTRL group. 579fa7d9493SBabu Moger */ 580fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP) { 581fa7d9493SBabu Moger tsk->closid = rdtgrp->closid; 582fa7d9493SBabu Moger tsk->rmid = rdtgrp->mon.rmid; 583fa7d9493SBabu Moger } else if (rdtgrp->type == RDTMON_GROUP) { 584fa7d9493SBabu Moger if (rdtgrp->mon.parent->closid == tsk->closid) { 585fa7d9493SBabu Moger tsk->rmid = rdtgrp->mon.rmid; 586fa7d9493SBabu Moger } else { 587fa7d9493SBabu Moger rdt_last_cmd_puts("Can't move task to different control group\n"); 588fa7d9493SBabu Moger ret = -EINVAL; 589fa7d9493SBabu Moger } 590fa7d9493SBabu Moger } 591fa7d9493SBabu Moger } 592fa7d9493SBabu Moger return ret; 593fa7d9493SBabu Moger } 594fa7d9493SBabu Moger 595e6b2fac3SJames Morse static bool is_closid_match(struct task_struct *t, struct rdtgroup *r) 596e6b2fac3SJames Morse { 597e6b2fac3SJames Morse return (rdt_alloc_capable && 598e6b2fac3SJames Morse (r->type == RDTCTRL_GROUP) && (t->closid == r->closid)); 599e6b2fac3SJames Morse } 600e6b2fac3SJames Morse 601e6b2fac3SJames Morse static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r) 602e6b2fac3SJames Morse { 603e6b2fac3SJames Morse return (rdt_mon_capable && 604e6b2fac3SJames Morse (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid)); 605e6b2fac3SJames Morse } 606e6b2fac3SJames Morse 607fa7d9493SBabu Moger /** 608fa7d9493SBabu Moger * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group 609fa7d9493SBabu Moger * @r: Resource group 610fa7d9493SBabu Moger * 611fa7d9493SBabu Moger * Return: 1 if tasks have been assigned to @r, 0 otherwise 612fa7d9493SBabu Moger */ 613fa7d9493SBabu Moger int rdtgroup_tasks_assigned(struct rdtgroup *r) 614fa7d9493SBabu Moger { 615fa7d9493SBabu Moger struct task_struct *p, *t; 616fa7d9493SBabu Moger int ret = 0; 617fa7d9493SBabu Moger 618fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 619fa7d9493SBabu Moger 620fa7d9493SBabu Moger rcu_read_lock(); 621fa7d9493SBabu Moger for_each_process_thread(p, t) { 622e6b2fac3SJames Morse if (is_closid_match(t, r) || is_rmid_match(t, r)) { 623fa7d9493SBabu Moger ret = 1; 624fa7d9493SBabu Moger break; 625fa7d9493SBabu Moger } 626fa7d9493SBabu Moger } 627fa7d9493SBabu Moger rcu_read_unlock(); 628fa7d9493SBabu Moger 629fa7d9493SBabu Moger return ret; 630fa7d9493SBabu Moger } 631fa7d9493SBabu Moger 632fa7d9493SBabu Moger static int rdtgroup_task_write_permission(struct task_struct *task, 633fa7d9493SBabu Moger struct kernfs_open_file *of) 634fa7d9493SBabu Moger { 635fa7d9493SBabu Moger const struct cred *tcred = get_task_cred(task); 636fa7d9493SBabu Moger const struct cred *cred = current_cred(); 637fa7d9493SBabu Moger int ret = 0; 638fa7d9493SBabu Moger 639fa7d9493SBabu Moger /* 640fa7d9493SBabu Moger * Even if we're attaching all tasks in the thread group, we only 641fa7d9493SBabu Moger * need to check permissions on one of them. 642fa7d9493SBabu Moger */ 643fa7d9493SBabu Moger if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && 644fa7d9493SBabu Moger !uid_eq(cred->euid, tcred->uid) && 645fa7d9493SBabu Moger !uid_eq(cred->euid, tcred->suid)) { 646fa7d9493SBabu Moger rdt_last_cmd_printf("No permission to move task %d\n", task->pid); 647fa7d9493SBabu Moger ret = -EPERM; 648fa7d9493SBabu Moger } 649fa7d9493SBabu Moger 650fa7d9493SBabu Moger put_cred(tcred); 651fa7d9493SBabu Moger return ret; 652fa7d9493SBabu Moger } 653fa7d9493SBabu Moger 654fa7d9493SBabu Moger static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp, 655fa7d9493SBabu Moger struct kernfs_open_file *of) 656fa7d9493SBabu Moger { 657fa7d9493SBabu Moger struct task_struct *tsk; 658fa7d9493SBabu Moger int ret; 659fa7d9493SBabu Moger 660fa7d9493SBabu Moger rcu_read_lock(); 661fa7d9493SBabu Moger if (pid) { 662fa7d9493SBabu Moger tsk = find_task_by_vpid(pid); 663fa7d9493SBabu Moger if (!tsk) { 664fa7d9493SBabu Moger rcu_read_unlock(); 665fa7d9493SBabu Moger rdt_last_cmd_printf("No task %d\n", pid); 666fa7d9493SBabu Moger return -ESRCH; 667fa7d9493SBabu Moger } 668fa7d9493SBabu Moger } else { 669fa7d9493SBabu Moger tsk = current; 670fa7d9493SBabu Moger } 671fa7d9493SBabu Moger 672fa7d9493SBabu Moger get_task_struct(tsk); 673fa7d9493SBabu Moger rcu_read_unlock(); 674fa7d9493SBabu Moger 675fa7d9493SBabu Moger ret = rdtgroup_task_write_permission(tsk, of); 676fa7d9493SBabu Moger if (!ret) 677fa7d9493SBabu Moger ret = __rdtgroup_move_task(tsk, rdtgrp); 678fa7d9493SBabu Moger 679fa7d9493SBabu Moger put_task_struct(tsk); 680fa7d9493SBabu Moger return ret; 681fa7d9493SBabu Moger } 682fa7d9493SBabu Moger 683fa7d9493SBabu Moger static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of, 684fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 685fa7d9493SBabu Moger { 686fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 687fa7d9493SBabu Moger int ret = 0; 688fa7d9493SBabu Moger pid_t pid; 689fa7d9493SBabu Moger 690fa7d9493SBabu Moger if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) 691fa7d9493SBabu Moger return -EINVAL; 692fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 693fa7d9493SBabu Moger if (!rdtgrp) { 694fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 695fa7d9493SBabu Moger return -ENOENT; 696fa7d9493SBabu Moger } 697fa7d9493SBabu Moger rdt_last_cmd_clear(); 698fa7d9493SBabu Moger 699fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 700fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 701fa7d9493SBabu Moger ret = -EINVAL; 702723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 703fa7d9493SBabu Moger goto unlock; 704fa7d9493SBabu Moger } 705fa7d9493SBabu Moger 706fa7d9493SBabu Moger ret = rdtgroup_move_task(pid, rdtgrp, of); 707fa7d9493SBabu Moger 708fa7d9493SBabu Moger unlock: 709fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 710fa7d9493SBabu Moger 711fa7d9493SBabu Moger return ret ?: nbytes; 712fa7d9493SBabu Moger } 713fa7d9493SBabu Moger 714fa7d9493SBabu Moger static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s) 715fa7d9493SBabu Moger { 716fa7d9493SBabu Moger struct task_struct *p, *t; 717fa7d9493SBabu Moger 718fa7d9493SBabu Moger rcu_read_lock(); 719fa7d9493SBabu Moger for_each_process_thread(p, t) { 720e6b2fac3SJames Morse if (is_closid_match(t, r) || is_rmid_match(t, r)) 721fa7d9493SBabu Moger seq_printf(s, "%d\n", t->pid); 722fa7d9493SBabu Moger } 723fa7d9493SBabu Moger rcu_read_unlock(); 724fa7d9493SBabu Moger } 725fa7d9493SBabu Moger 726fa7d9493SBabu Moger static int rdtgroup_tasks_show(struct kernfs_open_file *of, 727fa7d9493SBabu Moger struct seq_file *s, void *v) 728fa7d9493SBabu Moger { 729fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 730fa7d9493SBabu Moger int ret = 0; 731fa7d9493SBabu Moger 732fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 733fa7d9493SBabu Moger if (rdtgrp) 734fa7d9493SBabu Moger show_rdt_tasks(rdtgrp, s); 735fa7d9493SBabu Moger else 736fa7d9493SBabu Moger ret = -ENOENT; 737fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 738fa7d9493SBabu Moger 739fa7d9493SBabu Moger return ret; 740fa7d9493SBabu Moger } 741fa7d9493SBabu Moger 742e79f15a4SChen Yu #ifdef CONFIG_PROC_CPU_RESCTRL 743e79f15a4SChen Yu 744e79f15a4SChen Yu /* 745e79f15a4SChen Yu * A task can only be part of one resctrl control group and of one monitor 746e79f15a4SChen Yu * group which is associated to that control group. 747e79f15a4SChen Yu * 748e79f15a4SChen Yu * 1) res: 749e79f15a4SChen Yu * mon: 750e79f15a4SChen Yu * 751e79f15a4SChen Yu * resctrl is not available. 752e79f15a4SChen Yu * 753e79f15a4SChen Yu * 2) res:/ 754e79f15a4SChen Yu * mon: 755e79f15a4SChen Yu * 756e79f15a4SChen Yu * Task is part of the root resctrl control group, and it is not associated 757e79f15a4SChen Yu * to any monitor group. 758e79f15a4SChen Yu * 759e79f15a4SChen Yu * 3) res:/ 760e79f15a4SChen Yu * mon:mon0 761e79f15a4SChen Yu * 762e79f15a4SChen Yu * Task is part of the root resctrl control group and monitor group mon0. 763e79f15a4SChen Yu * 764e79f15a4SChen Yu * 4) res:group0 765e79f15a4SChen Yu * mon: 766e79f15a4SChen Yu * 767e79f15a4SChen Yu * Task is part of resctrl control group group0, and it is not associated 768e79f15a4SChen Yu * to any monitor group. 769e79f15a4SChen Yu * 770e79f15a4SChen Yu * 5) res:group0 771e79f15a4SChen Yu * mon:mon1 772e79f15a4SChen Yu * 773e79f15a4SChen Yu * Task is part of resctrl control group group0 and monitor group mon1. 774e79f15a4SChen Yu */ 775e79f15a4SChen Yu int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns, 776e79f15a4SChen Yu struct pid *pid, struct task_struct *tsk) 777e79f15a4SChen Yu { 778e79f15a4SChen Yu struct rdtgroup *rdtg; 779e79f15a4SChen Yu int ret = 0; 780e79f15a4SChen Yu 781e79f15a4SChen Yu mutex_lock(&rdtgroup_mutex); 782e79f15a4SChen Yu 783e79f15a4SChen Yu /* Return empty if resctrl has not been mounted. */ 784e79f15a4SChen Yu if (!static_branch_unlikely(&rdt_enable_key)) { 785e79f15a4SChen Yu seq_puts(s, "res:\nmon:\n"); 786e79f15a4SChen Yu goto unlock; 787e79f15a4SChen Yu } 788e79f15a4SChen Yu 789e79f15a4SChen Yu list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) { 790e79f15a4SChen Yu struct rdtgroup *crg; 791e79f15a4SChen Yu 792e79f15a4SChen Yu /* 793e79f15a4SChen Yu * Task information is only relevant for shareable 794e79f15a4SChen Yu * and exclusive groups. 795e79f15a4SChen Yu */ 796e79f15a4SChen Yu if (rdtg->mode != RDT_MODE_SHAREABLE && 797e79f15a4SChen Yu rdtg->mode != RDT_MODE_EXCLUSIVE) 798e79f15a4SChen Yu continue; 799e79f15a4SChen Yu 800e79f15a4SChen Yu if (rdtg->closid != tsk->closid) 801e79f15a4SChen Yu continue; 802e79f15a4SChen Yu 803e79f15a4SChen Yu seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "", 804e79f15a4SChen Yu rdtg->kn->name); 805e79f15a4SChen Yu seq_puts(s, "mon:"); 806e79f15a4SChen Yu list_for_each_entry(crg, &rdtg->mon.crdtgrp_list, 807e79f15a4SChen Yu mon.crdtgrp_list) { 808e79f15a4SChen Yu if (tsk->rmid != crg->mon.rmid) 809e79f15a4SChen Yu continue; 810e79f15a4SChen Yu seq_printf(s, "%s", crg->kn->name); 811e79f15a4SChen Yu break; 812e79f15a4SChen Yu } 813e79f15a4SChen Yu seq_putc(s, '\n'); 814e79f15a4SChen Yu goto unlock; 815e79f15a4SChen Yu } 816e79f15a4SChen Yu /* 817e79f15a4SChen Yu * The above search should succeed. Otherwise return 818e79f15a4SChen Yu * with an error. 819e79f15a4SChen Yu */ 820e79f15a4SChen Yu ret = -ENOENT; 821e79f15a4SChen Yu unlock: 822e79f15a4SChen Yu mutex_unlock(&rdtgroup_mutex); 823e79f15a4SChen Yu 824e79f15a4SChen Yu return ret; 825e79f15a4SChen Yu } 826e79f15a4SChen Yu #endif 827e79f15a4SChen Yu 828fa7d9493SBabu Moger static int rdt_last_cmd_status_show(struct kernfs_open_file *of, 829fa7d9493SBabu Moger struct seq_file *seq, void *v) 830fa7d9493SBabu Moger { 831fa7d9493SBabu Moger int len; 832fa7d9493SBabu Moger 833fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 834fa7d9493SBabu Moger len = seq_buf_used(&last_cmd_status); 835fa7d9493SBabu Moger if (len) 836fa7d9493SBabu Moger seq_printf(seq, "%.*s", len, last_cmd_status_buf); 837fa7d9493SBabu Moger else 838fa7d9493SBabu Moger seq_puts(seq, "ok\n"); 839fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 840fa7d9493SBabu Moger return 0; 841fa7d9493SBabu Moger } 842fa7d9493SBabu Moger 843fa7d9493SBabu Moger static int rdt_num_closids_show(struct kernfs_open_file *of, 844fa7d9493SBabu Moger struct seq_file *seq, void *v) 845fa7d9493SBabu Moger { 846fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 847fa7d9493SBabu Moger 848fa7d9493SBabu Moger seq_printf(seq, "%d\n", r->num_closid); 849fa7d9493SBabu Moger return 0; 850fa7d9493SBabu Moger } 851fa7d9493SBabu Moger 852fa7d9493SBabu Moger static int rdt_default_ctrl_show(struct kernfs_open_file *of, 853fa7d9493SBabu Moger struct seq_file *seq, void *v) 854fa7d9493SBabu Moger { 855fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 856fa7d9493SBabu Moger 857fa7d9493SBabu Moger seq_printf(seq, "%x\n", r->default_ctrl); 858fa7d9493SBabu Moger return 0; 859fa7d9493SBabu Moger } 860fa7d9493SBabu Moger 861fa7d9493SBabu Moger static int rdt_min_cbm_bits_show(struct kernfs_open_file *of, 862fa7d9493SBabu Moger struct seq_file *seq, void *v) 863fa7d9493SBabu Moger { 864fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 865fa7d9493SBabu Moger 866fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->cache.min_cbm_bits); 867fa7d9493SBabu Moger return 0; 868fa7d9493SBabu Moger } 869fa7d9493SBabu Moger 870fa7d9493SBabu Moger static int rdt_shareable_bits_show(struct kernfs_open_file *of, 871fa7d9493SBabu Moger struct seq_file *seq, void *v) 872fa7d9493SBabu Moger { 873fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 874fa7d9493SBabu Moger 875fa7d9493SBabu Moger seq_printf(seq, "%x\n", r->cache.shareable_bits); 876fa7d9493SBabu Moger return 0; 877fa7d9493SBabu Moger } 878fa7d9493SBabu Moger 879fa7d9493SBabu Moger /** 880fa7d9493SBabu Moger * rdt_bit_usage_show - Display current usage of resources 881fa7d9493SBabu Moger * 882fa7d9493SBabu Moger * A domain is a shared resource that can now be allocated differently. Here 883fa7d9493SBabu Moger * we display the current regions of the domain as an annotated bitmask. 884fa7d9493SBabu Moger * For each domain of this resource its allocation bitmask 885fa7d9493SBabu Moger * is annotated as below to indicate the current usage of the corresponding bit: 886fa7d9493SBabu Moger * 0 - currently unused 887fa7d9493SBabu Moger * X - currently available for sharing and used by software and hardware 888fa7d9493SBabu Moger * H - currently used by hardware only but available for software use 889fa7d9493SBabu Moger * S - currently used and shareable by software only 890fa7d9493SBabu Moger * E - currently used exclusively by one resource group 891fa7d9493SBabu Moger * P - currently pseudo-locked by one resource group 892fa7d9493SBabu Moger */ 893fa7d9493SBabu Moger static int rdt_bit_usage_show(struct kernfs_open_file *of, 894fa7d9493SBabu Moger struct seq_file *seq, void *v) 895fa7d9493SBabu Moger { 896fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 89732f010deSReinette Chatre /* 89832f010deSReinette Chatre * Use unsigned long even though only 32 bits are used to ensure 89932f010deSReinette Chatre * test_bit() is used safely. 90032f010deSReinette Chatre */ 90132f010deSReinette Chatre unsigned long sw_shareable = 0, hw_shareable = 0; 90232f010deSReinette Chatre unsigned long exclusive = 0, pseudo_locked = 0; 903fa7d9493SBabu Moger struct rdt_domain *dom; 904fa7d9493SBabu Moger int i, hwb, swb, excl, psl; 905fa7d9493SBabu Moger enum rdtgrp_mode mode; 906fa7d9493SBabu Moger bool sep = false; 907fa7d9493SBabu Moger u32 *ctrl; 908fa7d9493SBabu Moger 909fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 910fa7d9493SBabu Moger hw_shareable = r->cache.shareable_bits; 911fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) { 912fa7d9493SBabu Moger if (sep) 913fa7d9493SBabu Moger seq_putc(seq, ';'); 914fa7d9493SBabu Moger ctrl = dom->ctrl_val; 915fa7d9493SBabu Moger sw_shareable = 0; 916fa7d9493SBabu Moger exclusive = 0; 917fa7d9493SBabu Moger seq_printf(seq, "%d=", dom->id); 918fa7d9493SBabu Moger for (i = 0; i < closids_supported(); i++, ctrl++) { 919fa7d9493SBabu Moger if (!closid_allocated(i)) 920fa7d9493SBabu Moger continue; 921fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 922fa7d9493SBabu Moger switch (mode) { 923fa7d9493SBabu Moger case RDT_MODE_SHAREABLE: 924fa7d9493SBabu Moger sw_shareable |= *ctrl; 925fa7d9493SBabu Moger break; 926fa7d9493SBabu Moger case RDT_MODE_EXCLUSIVE: 927fa7d9493SBabu Moger exclusive |= *ctrl; 928fa7d9493SBabu Moger break; 929fa7d9493SBabu Moger case RDT_MODE_PSEUDO_LOCKSETUP: 930fa7d9493SBabu Moger /* 931fa7d9493SBabu Moger * RDT_MODE_PSEUDO_LOCKSETUP is possible 932fa7d9493SBabu Moger * here but not included since the CBM 933fa7d9493SBabu Moger * associated with this CLOSID in this mode 934fa7d9493SBabu Moger * is not initialized and no task or cpu can be 935fa7d9493SBabu Moger * assigned this CLOSID. 936fa7d9493SBabu Moger */ 937fa7d9493SBabu Moger break; 938fa7d9493SBabu Moger case RDT_MODE_PSEUDO_LOCKED: 939fa7d9493SBabu Moger case RDT_NUM_MODES: 940fa7d9493SBabu Moger WARN(1, 941fa7d9493SBabu Moger "invalid mode for closid %d\n", i); 942fa7d9493SBabu Moger break; 943fa7d9493SBabu Moger } 944fa7d9493SBabu Moger } 945fa7d9493SBabu Moger for (i = r->cache.cbm_len - 1; i >= 0; i--) { 946fa7d9493SBabu Moger pseudo_locked = dom->plr ? dom->plr->cbm : 0; 94732f010deSReinette Chatre hwb = test_bit(i, &hw_shareable); 94832f010deSReinette Chatre swb = test_bit(i, &sw_shareable); 94932f010deSReinette Chatre excl = test_bit(i, &exclusive); 95032f010deSReinette Chatre psl = test_bit(i, &pseudo_locked); 951fa7d9493SBabu Moger if (hwb && swb) 952fa7d9493SBabu Moger seq_putc(seq, 'X'); 953fa7d9493SBabu Moger else if (hwb && !swb) 954fa7d9493SBabu Moger seq_putc(seq, 'H'); 955fa7d9493SBabu Moger else if (!hwb && swb) 956fa7d9493SBabu Moger seq_putc(seq, 'S'); 957fa7d9493SBabu Moger else if (excl) 958fa7d9493SBabu Moger seq_putc(seq, 'E'); 959fa7d9493SBabu Moger else if (psl) 960fa7d9493SBabu Moger seq_putc(seq, 'P'); 961fa7d9493SBabu Moger else /* Unused bits remain */ 962fa7d9493SBabu Moger seq_putc(seq, '0'); 963fa7d9493SBabu Moger } 964fa7d9493SBabu Moger sep = true; 965fa7d9493SBabu Moger } 966fa7d9493SBabu Moger seq_putc(seq, '\n'); 967fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 968fa7d9493SBabu Moger return 0; 969fa7d9493SBabu Moger } 970fa7d9493SBabu Moger 971fa7d9493SBabu Moger static int rdt_min_bw_show(struct kernfs_open_file *of, 972fa7d9493SBabu Moger struct seq_file *seq, void *v) 973fa7d9493SBabu Moger { 974fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 975fa7d9493SBabu Moger 976fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.min_bw); 977fa7d9493SBabu Moger return 0; 978fa7d9493SBabu Moger } 979fa7d9493SBabu Moger 980fa7d9493SBabu Moger static int rdt_num_rmids_show(struct kernfs_open_file *of, 981fa7d9493SBabu Moger struct seq_file *seq, void *v) 982fa7d9493SBabu Moger { 983fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 984fa7d9493SBabu Moger 985fa7d9493SBabu Moger seq_printf(seq, "%d\n", r->num_rmid); 986fa7d9493SBabu Moger 987fa7d9493SBabu Moger return 0; 988fa7d9493SBabu Moger } 989fa7d9493SBabu Moger 990fa7d9493SBabu Moger static int rdt_mon_features_show(struct kernfs_open_file *of, 991fa7d9493SBabu Moger struct seq_file *seq, void *v) 992fa7d9493SBabu Moger { 993fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 994fa7d9493SBabu Moger struct mon_evt *mevt; 995fa7d9493SBabu Moger 996fa7d9493SBabu Moger list_for_each_entry(mevt, &r->evt_list, list) 997fa7d9493SBabu Moger seq_printf(seq, "%s\n", mevt->name); 998fa7d9493SBabu Moger 999fa7d9493SBabu Moger return 0; 1000fa7d9493SBabu Moger } 1001fa7d9493SBabu Moger 1002fa7d9493SBabu Moger static int rdt_bw_gran_show(struct kernfs_open_file *of, 1003fa7d9493SBabu Moger struct seq_file *seq, void *v) 1004fa7d9493SBabu Moger { 1005fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 1006fa7d9493SBabu Moger 1007fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.bw_gran); 1008fa7d9493SBabu Moger return 0; 1009fa7d9493SBabu Moger } 1010fa7d9493SBabu Moger 1011fa7d9493SBabu Moger static int rdt_delay_linear_show(struct kernfs_open_file *of, 1012fa7d9493SBabu Moger struct seq_file *seq, void *v) 1013fa7d9493SBabu Moger { 1014fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 1015fa7d9493SBabu Moger 1016fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.delay_linear); 1017fa7d9493SBabu Moger return 0; 1018fa7d9493SBabu Moger } 1019fa7d9493SBabu Moger 1020fa7d9493SBabu Moger static int max_threshold_occ_show(struct kernfs_open_file *of, 1021fa7d9493SBabu Moger struct seq_file *seq, void *v) 1022fa7d9493SBabu Moger { 1023fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 1024fa7d9493SBabu Moger 1025352940ecSBabu Moger seq_printf(seq, "%u\n", resctrl_cqm_threshold * r->mon_scale); 1026fa7d9493SBabu Moger 1027fa7d9493SBabu Moger return 0; 1028fa7d9493SBabu Moger } 1029fa7d9493SBabu Moger 1030*29b6bd41SFenghua Yu static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of, 1031*29b6bd41SFenghua Yu struct seq_file *seq, void *v) 1032*29b6bd41SFenghua Yu { 1033*29b6bd41SFenghua Yu struct rdt_resource *r = of->kn->parent->priv; 1034*29b6bd41SFenghua Yu 1035*29b6bd41SFenghua Yu if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD) 1036*29b6bd41SFenghua Yu seq_puts(seq, "per-thread\n"); 1037*29b6bd41SFenghua Yu else 1038*29b6bd41SFenghua Yu seq_puts(seq, "max\n"); 1039*29b6bd41SFenghua Yu 1040*29b6bd41SFenghua Yu return 0; 1041*29b6bd41SFenghua Yu } 1042*29b6bd41SFenghua Yu 1043fa7d9493SBabu Moger static ssize_t max_threshold_occ_write(struct kernfs_open_file *of, 1044fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 1045fa7d9493SBabu Moger { 1046fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 1047fa7d9493SBabu Moger unsigned int bytes; 1048fa7d9493SBabu Moger int ret; 1049fa7d9493SBabu Moger 1050fa7d9493SBabu Moger ret = kstrtouint(buf, 0, &bytes); 1051fa7d9493SBabu Moger if (ret) 1052fa7d9493SBabu Moger return ret; 1053fa7d9493SBabu Moger 1054fa7d9493SBabu Moger if (bytes > (boot_cpu_data.x86_cache_size * 1024)) 1055fa7d9493SBabu Moger return -EINVAL; 1056fa7d9493SBabu Moger 1057352940ecSBabu Moger resctrl_cqm_threshold = bytes / r->mon_scale; 1058fa7d9493SBabu Moger 1059fa7d9493SBabu Moger return nbytes; 1060fa7d9493SBabu Moger } 1061fa7d9493SBabu Moger 1062fa7d9493SBabu Moger /* 1063fa7d9493SBabu Moger * rdtgroup_mode_show - Display mode of this resource group 1064fa7d9493SBabu Moger */ 1065fa7d9493SBabu Moger static int rdtgroup_mode_show(struct kernfs_open_file *of, 1066fa7d9493SBabu Moger struct seq_file *s, void *v) 1067fa7d9493SBabu Moger { 1068fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1069fa7d9493SBabu Moger 1070fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1071fa7d9493SBabu Moger if (!rdtgrp) { 1072fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1073fa7d9493SBabu Moger return -ENOENT; 1074fa7d9493SBabu Moger } 1075fa7d9493SBabu Moger 1076fa7d9493SBabu Moger seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode)); 1077fa7d9493SBabu Moger 1078fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1079fa7d9493SBabu Moger return 0; 1080fa7d9493SBabu Moger } 1081fa7d9493SBabu Moger 1082fa7d9493SBabu Moger /** 1083fa7d9493SBabu Moger * rdt_cdp_peer_get - Retrieve CDP peer if it exists 1084fa7d9493SBabu Moger * @r: RDT resource to which RDT domain @d belongs 1085fa7d9493SBabu Moger * @d: Cache instance for which a CDP peer is requested 1086fa7d9493SBabu Moger * @r_cdp: RDT resource that shares hardware with @r (RDT resource peer) 1087fa7d9493SBabu Moger * Used to return the result. 1088fa7d9493SBabu Moger * @d_cdp: RDT domain that shares hardware with @d (RDT domain peer) 1089fa7d9493SBabu Moger * Used to return the result. 1090fa7d9493SBabu Moger * 1091fa7d9493SBabu Moger * RDT resources are managed independently and by extension the RDT domains 1092fa7d9493SBabu Moger * (RDT resource instances) are managed independently also. The Code and 1093fa7d9493SBabu Moger * Data Prioritization (CDP) RDT resources, while managed independently, 1094fa7d9493SBabu Moger * could refer to the same underlying hardware. For example, 1095fa7d9493SBabu Moger * RDT_RESOURCE_L2CODE and RDT_RESOURCE_L2DATA both refer to the L2 cache. 1096fa7d9493SBabu Moger * 1097fa7d9493SBabu Moger * When provided with an RDT resource @r and an instance of that RDT 1098fa7d9493SBabu Moger * resource @d rdt_cdp_peer_get() will return if there is a peer RDT 1099fa7d9493SBabu Moger * resource and the exact instance that shares the same hardware. 1100fa7d9493SBabu Moger * 1101fa7d9493SBabu Moger * Return: 0 if a CDP peer was found, <0 on error or if no CDP peer exists. 1102fa7d9493SBabu Moger * If a CDP peer was found, @r_cdp will point to the peer RDT resource 1103fa7d9493SBabu Moger * and @d_cdp will point to the peer RDT domain. 1104fa7d9493SBabu Moger */ 1105fa7d9493SBabu Moger static int rdt_cdp_peer_get(struct rdt_resource *r, struct rdt_domain *d, 1106fa7d9493SBabu Moger struct rdt_resource **r_cdp, 1107fa7d9493SBabu Moger struct rdt_domain **d_cdp) 1108fa7d9493SBabu Moger { 1109fa7d9493SBabu Moger struct rdt_resource *_r_cdp = NULL; 1110fa7d9493SBabu Moger struct rdt_domain *_d_cdp = NULL; 1111fa7d9493SBabu Moger int ret = 0; 1112fa7d9493SBabu Moger 1113fa7d9493SBabu Moger switch (r->rid) { 1114fa7d9493SBabu Moger case RDT_RESOURCE_L3DATA: 1115fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3CODE]; 1116fa7d9493SBabu Moger break; 1117fa7d9493SBabu Moger case RDT_RESOURCE_L3CODE: 1118fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3DATA]; 1119fa7d9493SBabu Moger break; 1120fa7d9493SBabu Moger case RDT_RESOURCE_L2DATA: 1121fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2CODE]; 1122fa7d9493SBabu Moger break; 1123fa7d9493SBabu Moger case RDT_RESOURCE_L2CODE: 1124fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2DATA]; 1125fa7d9493SBabu Moger break; 1126fa7d9493SBabu Moger default: 1127fa7d9493SBabu Moger ret = -ENOENT; 1128fa7d9493SBabu Moger goto out; 1129fa7d9493SBabu Moger } 1130fa7d9493SBabu Moger 1131fa7d9493SBabu Moger /* 1132fa7d9493SBabu Moger * When a new CPU comes online and CDP is enabled then the new 1133fa7d9493SBabu Moger * RDT domains (if any) associated with both CDP RDT resources 1134fa7d9493SBabu Moger * are added in the same CPU online routine while the 1135fa7d9493SBabu Moger * rdtgroup_mutex is held. It should thus not happen for one 1136fa7d9493SBabu Moger * RDT domain to exist and be associated with its RDT CDP 1137fa7d9493SBabu Moger * resource but there is no RDT domain associated with the 1138fa7d9493SBabu Moger * peer RDT CDP resource. Hence the WARN. 1139fa7d9493SBabu Moger */ 1140fa7d9493SBabu Moger _d_cdp = rdt_find_domain(_r_cdp, d->id, NULL); 114152eb7433SReinette Chatre if (WARN_ON(IS_ERR_OR_NULL(_d_cdp))) { 1142fa7d9493SBabu Moger _r_cdp = NULL; 1143cc5277feSDan Carpenter _d_cdp = NULL; 1144fa7d9493SBabu Moger ret = -EINVAL; 1145fa7d9493SBabu Moger } 1146fa7d9493SBabu Moger 1147fa7d9493SBabu Moger out: 1148fa7d9493SBabu Moger *r_cdp = _r_cdp; 1149fa7d9493SBabu Moger *d_cdp = _d_cdp; 1150fa7d9493SBabu Moger 1151fa7d9493SBabu Moger return ret; 1152fa7d9493SBabu Moger } 1153fa7d9493SBabu Moger 1154fa7d9493SBabu Moger /** 1155fa7d9493SBabu Moger * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other 1156fa7d9493SBabu Moger * @r: Resource to which domain instance @d belongs. 1157fa7d9493SBabu Moger * @d: The domain instance for which @closid is being tested. 1158fa7d9493SBabu Moger * @cbm: Capacity bitmask being tested. 1159fa7d9493SBabu Moger * @closid: Intended closid for @cbm. 1160fa7d9493SBabu Moger * @exclusive: Only check if overlaps with exclusive resource groups 1161fa7d9493SBabu Moger * 1162fa7d9493SBabu Moger * Checks if provided @cbm intended to be used for @closid on domain 1163fa7d9493SBabu Moger * @d overlaps with any other closids or other hardware usage associated 1164fa7d9493SBabu Moger * with this domain. If @exclusive is true then only overlaps with 1165fa7d9493SBabu Moger * resource groups in exclusive mode will be considered. If @exclusive 1166fa7d9493SBabu Moger * is false then overlaps with any resource group or hardware entities 1167fa7d9493SBabu Moger * will be considered. 1168fa7d9493SBabu Moger * 1169fa7d9493SBabu Moger * @cbm is unsigned long, even if only 32 bits are used, to make the 1170fa7d9493SBabu Moger * bitmap functions work correctly. 1171fa7d9493SBabu Moger * 1172fa7d9493SBabu Moger * Return: false if CBM does not overlap, true if it does. 1173fa7d9493SBabu Moger */ 1174fa7d9493SBabu Moger static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, 1175fa7d9493SBabu Moger unsigned long cbm, int closid, bool exclusive) 1176fa7d9493SBabu Moger { 1177fa7d9493SBabu Moger enum rdtgrp_mode mode; 1178fa7d9493SBabu Moger unsigned long ctrl_b; 1179fa7d9493SBabu Moger u32 *ctrl; 1180fa7d9493SBabu Moger int i; 1181fa7d9493SBabu Moger 1182fa7d9493SBabu Moger /* Check for any overlap with regions used by hardware directly */ 1183fa7d9493SBabu Moger if (!exclusive) { 1184fa7d9493SBabu Moger ctrl_b = r->cache.shareable_bits; 1185fa7d9493SBabu Moger if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) 1186fa7d9493SBabu Moger return true; 1187fa7d9493SBabu Moger } 1188fa7d9493SBabu Moger 1189fa7d9493SBabu Moger /* Check for overlap with other resource groups */ 1190fa7d9493SBabu Moger ctrl = d->ctrl_val; 1191fa7d9493SBabu Moger for (i = 0; i < closids_supported(); i++, ctrl++) { 1192fa7d9493SBabu Moger ctrl_b = *ctrl; 1193fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 1194fa7d9493SBabu Moger if (closid_allocated(i) && i != closid && 1195fa7d9493SBabu Moger mode != RDT_MODE_PSEUDO_LOCKSETUP) { 1196fa7d9493SBabu Moger if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) { 1197fa7d9493SBabu Moger if (exclusive) { 1198fa7d9493SBabu Moger if (mode == RDT_MODE_EXCLUSIVE) 1199fa7d9493SBabu Moger return true; 1200fa7d9493SBabu Moger continue; 1201fa7d9493SBabu Moger } 1202fa7d9493SBabu Moger return true; 1203fa7d9493SBabu Moger } 1204fa7d9493SBabu Moger } 1205fa7d9493SBabu Moger } 1206fa7d9493SBabu Moger 1207fa7d9493SBabu Moger return false; 1208fa7d9493SBabu Moger } 1209fa7d9493SBabu Moger 1210fa7d9493SBabu Moger /** 1211fa7d9493SBabu Moger * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware 1212fa7d9493SBabu Moger * @r: Resource to which domain instance @d belongs. 1213fa7d9493SBabu Moger * @d: The domain instance for which @closid is being tested. 1214fa7d9493SBabu Moger * @cbm: Capacity bitmask being tested. 1215fa7d9493SBabu Moger * @closid: Intended closid for @cbm. 1216fa7d9493SBabu Moger * @exclusive: Only check if overlaps with exclusive resource groups 1217fa7d9493SBabu Moger * 1218fa7d9493SBabu Moger * Resources that can be allocated using a CBM can use the CBM to control 1219fa7d9493SBabu Moger * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test 1220fa7d9493SBabu Moger * for overlap. Overlap test is not limited to the specific resource for 1221fa7d9493SBabu Moger * which the CBM is intended though - when dealing with CDP resources that 1222fa7d9493SBabu Moger * share the underlying hardware the overlap check should be performed on 1223fa7d9493SBabu Moger * the CDP resource sharing the hardware also. 1224fa7d9493SBabu Moger * 1225fa7d9493SBabu Moger * Refer to description of __rdtgroup_cbm_overlaps() for the details of the 1226fa7d9493SBabu Moger * overlap test. 1227fa7d9493SBabu Moger * 1228fa7d9493SBabu Moger * Return: true if CBM overlap detected, false if there is no overlap 1229fa7d9493SBabu Moger */ 1230fa7d9493SBabu Moger bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, 1231fa7d9493SBabu Moger unsigned long cbm, int closid, bool exclusive) 1232fa7d9493SBabu Moger { 1233fa7d9493SBabu Moger struct rdt_resource *r_cdp; 1234fa7d9493SBabu Moger struct rdt_domain *d_cdp; 1235fa7d9493SBabu Moger 1236fa7d9493SBabu Moger if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, exclusive)) 1237fa7d9493SBabu Moger return true; 1238fa7d9493SBabu Moger 1239fa7d9493SBabu Moger if (rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp) < 0) 1240fa7d9493SBabu Moger return false; 1241fa7d9493SBabu Moger 1242fa7d9493SBabu Moger return __rdtgroup_cbm_overlaps(r_cdp, d_cdp, cbm, closid, exclusive); 1243fa7d9493SBabu Moger } 1244fa7d9493SBabu Moger 1245fa7d9493SBabu Moger /** 1246fa7d9493SBabu Moger * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive 1247fa7d9493SBabu Moger * 1248fa7d9493SBabu Moger * An exclusive resource group implies that there should be no sharing of 1249fa7d9493SBabu Moger * its allocated resources. At the time this group is considered to be 1250fa7d9493SBabu Moger * exclusive this test can determine if its current schemata supports this 1251fa7d9493SBabu Moger * setting by testing for overlap with all other resource groups. 1252fa7d9493SBabu Moger * 1253fa7d9493SBabu Moger * Return: true if resource group can be exclusive, false if there is overlap 1254fa7d9493SBabu Moger * with allocations of other resource groups and thus this resource group 1255fa7d9493SBabu Moger * cannot be exclusive. 1256fa7d9493SBabu Moger */ 1257fa7d9493SBabu Moger static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp) 1258fa7d9493SBabu Moger { 1259fa7d9493SBabu Moger int closid = rdtgrp->closid; 1260fa7d9493SBabu Moger struct rdt_resource *r; 1261fa7d9493SBabu Moger bool has_cache = false; 1262fa7d9493SBabu Moger struct rdt_domain *d; 1263fa7d9493SBabu Moger 1264fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 1265fa7d9493SBabu Moger if (r->rid == RDT_RESOURCE_MBA) 1266fa7d9493SBabu Moger continue; 1267fa7d9493SBabu Moger has_cache = true; 1268fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 1269fa7d9493SBabu Moger if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid], 1270fa7d9493SBabu Moger rdtgrp->closid, false)) { 1271723f1a0dSBabu Moger rdt_last_cmd_puts("Schemata overlaps\n"); 1272fa7d9493SBabu Moger return false; 1273fa7d9493SBabu Moger } 1274fa7d9493SBabu Moger } 1275fa7d9493SBabu Moger } 1276fa7d9493SBabu Moger 1277fa7d9493SBabu Moger if (!has_cache) { 1278723f1a0dSBabu Moger rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n"); 1279fa7d9493SBabu Moger return false; 1280fa7d9493SBabu Moger } 1281fa7d9493SBabu Moger 1282fa7d9493SBabu Moger return true; 1283fa7d9493SBabu Moger } 1284fa7d9493SBabu Moger 1285fa7d9493SBabu Moger /** 1286fa7d9493SBabu Moger * rdtgroup_mode_write - Modify the resource group's mode 1287fa7d9493SBabu Moger * 1288fa7d9493SBabu Moger */ 1289fa7d9493SBabu Moger static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, 1290fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 1291fa7d9493SBabu Moger { 1292fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1293fa7d9493SBabu Moger enum rdtgrp_mode mode; 1294fa7d9493SBabu Moger int ret = 0; 1295fa7d9493SBabu Moger 1296fa7d9493SBabu Moger /* Valid input requires a trailing newline */ 1297fa7d9493SBabu Moger if (nbytes == 0 || buf[nbytes - 1] != '\n') 1298fa7d9493SBabu Moger return -EINVAL; 1299fa7d9493SBabu Moger buf[nbytes - 1] = '\0'; 1300fa7d9493SBabu Moger 1301fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1302fa7d9493SBabu Moger if (!rdtgrp) { 1303fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1304fa7d9493SBabu Moger return -ENOENT; 1305fa7d9493SBabu Moger } 1306fa7d9493SBabu Moger 1307fa7d9493SBabu Moger rdt_last_cmd_clear(); 1308fa7d9493SBabu Moger 1309fa7d9493SBabu Moger mode = rdtgrp->mode; 1310fa7d9493SBabu Moger 1311fa7d9493SBabu Moger if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) || 1312fa7d9493SBabu Moger (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) || 1313fa7d9493SBabu Moger (!strcmp(buf, "pseudo-locksetup") && 1314fa7d9493SBabu Moger mode == RDT_MODE_PSEUDO_LOCKSETUP) || 1315fa7d9493SBabu Moger (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED)) 1316fa7d9493SBabu Moger goto out; 1317fa7d9493SBabu Moger 1318fa7d9493SBabu Moger if (mode == RDT_MODE_PSEUDO_LOCKED) { 131945682489SReinette Chatre rdt_last_cmd_puts("Cannot change pseudo-locked group\n"); 1320fa7d9493SBabu Moger ret = -EINVAL; 1321fa7d9493SBabu Moger goto out; 1322fa7d9493SBabu Moger } 1323fa7d9493SBabu Moger 1324fa7d9493SBabu Moger if (!strcmp(buf, "shareable")) { 1325fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1326fa7d9493SBabu Moger ret = rdtgroup_locksetup_exit(rdtgrp); 1327fa7d9493SBabu Moger if (ret) 1328fa7d9493SBabu Moger goto out; 1329fa7d9493SBabu Moger } 1330fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_SHAREABLE; 1331fa7d9493SBabu Moger } else if (!strcmp(buf, "exclusive")) { 1332fa7d9493SBabu Moger if (!rdtgroup_mode_test_exclusive(rdtgrp)) { 1333fa7d9493SBabu Moger ret = -EINVAL; 1334fa7d9493SBabu Moger goto out; 1335fa7d9493SBabu Moger } 1336fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1337fa7d9493SBabu Moger ret = rdtgroup_locksetup_exit(rdtgrp); 1338fa7d9493SBabu Moger if (ret) 1339fa7d9493SBabu Moger goto out; 1340fa7d9493SBabu Moger } 1341fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_EXCLUSIVE; 1342fa7d9493SBabu Moger } else if (!strcmp(buf, "pseudo-locksetup")) { 1343fa7d9493SBabu Moger ret = rdtgroup_locksetup_enter(rdtgrp); 1344fa7d9493SBabu Moger if (ret) 1345fa7d9493SBabu Moger goto out; 1346fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP; 1347fa7d9493SBabu Moger } else { 134845682489SReinette Chatre rdt_last_cmd_puts("Unknown or unsupported mode\n"); 1349fa7d9493SBabu Moger ret = -EINVAL; 1350fa7d9493SBabu Moger } 1351fa7d9493SBabu Moger 1352fa7d9493SBabu Moger out: 1353fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1354fa7d9493SBabu Moger return ret ?: nbytes; 1355fa7d9493SBabu Moger } 1356fa7d9493SBabu Moger 1357fa7d9493SBabu Moger /** 1358fa7d9493SBabu Moger * rdtgroup_cbm_to_size - Translate CBM to size in bytes 1359fa7d9493SBabu Moger * @r: RDT resource to which @d belongs. 1360fa7d9493SBabu Moger * @d: RDT domain instance. 1361fa7d9493SBabu Moger * @cbm: bitmask for which the size should be computed. 1362fa7d9493SBabu Moger * 1363fa7d9493SBabu Moger * The bitmask provided associated with the RDT domain instance @d will be 1364fa7d9493SBabu Moger * translated into how many bytes it represents. The size in bytes is 1365fa7d9493SBabu Moger * computed by first dividing the total cache size by the CBM length to 1366fa7d9493SBabu Moger * determine how many bytes each bit in the bitmask represents. The result 1367fa7d9493SBabu Moger * is multiplied with the number of bits set in the bitmask. 1368fa7d9493SBabu Moger * 1369fa7d9493SBabu Moger * @cbm is unsigned long, even if only 32 bits are used to make the 1370fa7d9493SBabu Moger * bitmap functions work correctly. 1371fa7d9493SBabu Moger */ 1372fa7d9493SBabu Moger unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, 1373fa7d9493SBabu Moger struct rdt_domain *d, unsigned long cbm) 1374fa7d9493SBabu Moger { 1375fa7d9493SBabu Moger struct cpu_cacheinfo *ci; 1376fa7d9493SBabu Moger unsigned int size = 0; 1377fa7d9493SBabu Moger int num_b, i; 1378fa7d9493SBabu Moger 1379fa7d9493SBabu Moger num_b = bitmap_weight(&cbm, r->cache.cbm_len); 1380fa7d9493SBabu Moger ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask)); 1381fa7d9493SBabu Moger for (i = 0; i < ci->num_leaves; i++) { 1382fa7d9493SBabu Moger if (ci->info_list[i].level == r->cache_level) { 1383fa7d9493SBabu Moger size = ci->info_list[i].size / r->cache.cbm_len * num_b; 1384fa7d9493SBabu Moger break; 1385fa7d9493SBabu Moger } 1386fa7d9493SBabu Moger } 1387fa7d9493SBabu Moger 1388fa7d9493SBabu Moger return size; 1389fa7d9493SBabu Moger } 1390fa7d9493SBabu Moger 1391fa7d9493SBabu Moger /** 1392fa7d9493SBabu Moger * rdtgroup_size_show - Display size in bytes of allocated regions 1393fa7d9493SBabu Moger * 1394fa7d9493SBabu Moger * The "size" file mirrors the layout of the "schemata" file, printing the 1395fa7d9493SBabu Moger * size in bytes of each region instead of the capacity bitmask. 1396fa7d9493SBabu Moger * 1397fa7d9493SBabu Moger */ 1398fa7d9493SBabu Moger static int rdtgroup_size_show(struct kernfs_open_file *of, 1399fa7d9493SBabu Moger struct seq_file *s, void *v) 1400fa7d9493SBabu Moger { 1401fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1402fa7d9493SBabu Moger struct rdt_resource *r; 1403fa7d9493SBabu Moger struct rdt_domain *d; 1404fa7d9493SBabu Moger unsigned int size; 1405fa7d9493SBabu Moger int ret = 0; 1406fa7d9493SBabu Moger bool sep; 1407fa7d9493SBabu Moger u32 ctrl; 1408fa7d9493SBabu Moger 1409fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1410fa7d9493SBabu Moger if (!rdtgrp) { 1411fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1412fa7d9493SBabu Moger return -ENOENT; 1413fa7d9493SBabu Moger } 1414fa7d9493SBabu Moger 1415fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 1416fa7d9493SBabu Moger if (!rdtgrp->plr->d) { 1417fa7d9493SBabu Moger rdt_last_cmd_clear(); 1418fa7d9493SBabu Moger rdt_last_cmd_puts("Cache domain offline\n"); 1419fa7d9493SBabu Moger ret = -ENODEV; 1420fa7d9493SBabu Moger } else { 1421fa7d9493SBabu Moger seq_printf(s, "%*s:", max_name_width, 1422fa7d9493SBabu Moger rdtgrp->plr->r->name); 1423fa7d9493SBabu Moger size = rdtgroup_cbm_to_size(rdtgrp->plr->r, 1424fa7d9493SBabu Moger rdtgrp->plr->d, 1425fa7d9493SBabu Moger rdtgrp->plr->cbm); 1426fa7d9493SBabu Moger seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size); 1427fa7d9493SBabu Moger } 1428fa7d9493SBabu Moger goto out; 1429fa7d9493SBabu Moger } 1430fa7d9493SBabu Moger 1431fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 1432fa7d9493SBabu Moger sep = false; 1433fa7d9493SBabu Moger seq_printf(s, "%*s:", max_name_width, r->name); 1434fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 1435fa7d9493SBabu Moger if (sep) 1436fa7d9493SBabu Moger seq_putc(s, ';'); 1437fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1438fa7d9493SBabu Moger size = 0; 1439fa7d9493SBabu Moger } else { 1440fa7d9493SBabu Moger ctrl = (!is_mba_sc(r) ? 1441fa7d9493SBabu Moger d->ctrl_val[rdtgrp->closid] : 1442fa7d9493SBabu Moger d->mbps_val[rdtgrp->closid]); 1443fa7d9493SBabu Moger if (r->rid == RDT_RESOURCE_MBA) 1444fa7d9493SBabu Moger size = ctrl; 1445fa7d9493SBabu Moger else 1446fa7d9493SBabu Moger size = rdtgroup_cbm_to_size(r, d, ctrl); 1447fa7d9493SBabu Moger } 1448fa7d9493SBabu Moger seq_printf(s, "%d=%u", d->id, size); 1449fa7d9493SBabu Moger sep = true; 1450fa7d9493SBabu Moger } 1451fa7d9493SBabu Moger seq_putc(s, '\n'); 1452fa7d9493SBabu Moger } 1453fa7d9493SBabu Moger 1454fa7d9493SBabu Moger out: 1455fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1456fa7d9493SBabu Moger 1457fa7d9493SBabu Moger return ret; 1458fa7d9493SBabu Moger } 1459fa7d9493SBabu Moger 1460fa7d9493SBabu Moger /* rdtgroup information files for one cache resource. */ 1461fa7d9493SBabu Moger static struct rftype res_common_files[] = { 1462fa7d9493SBabu Moger { 1463fa7d9493SBabu Moger .name = "last_cmd_status", 1464fa7d9493SBabu Moger .mode = 0444, 1465fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1466fa7d9493SBabu Moger .seq_show = rdt_last_cmd_status_show, 1467fa7d9493SBabu Moger .fflags = RF_TOP_INFO, 1468fa7d9493SBabu Moger }, 1469fa7d9493SBabu Moger { 1470fa7d9493SBabu Moger .name = "num_closids", 1471fa7d9493SBabu Moger .mode = 0444, 1472fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1473fa7d9493SBabu Moger .seq_show = rdt_num_closids_show, 1474fa7d9493SBabu Moger .fflags = RF_CTRL_INFO, 1475fa7d9493SBabu Moger }, 1476fa7d9493SBabu Moger { 1477fa7d9493SBabu Moger .name = "mon_features", 1478fa7d9493SBabu Moger .mode = 0444, 1479fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1480fa7d9493SBabu Moger .seq_show = rdt_mon_features_show, 1481fa7d9493SBabu Moger .fflags = RF_MON_INFO, 1482fa7d9493SBabu Moger }, 1483fa7d9493SBabu Moger { 1484fa7d9493SBabu Moger .name = "num_rmids", 1485fa7d9493SBabu Moger .mode = 0444, 1486fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1487fa7d9493SBabu Moger .seq_show = rdt_num_rmids_show, 1488fa7d9493SBabu Moger .fflags = RF_MON_INFO, 1489fa7d9493SBabu Moger }, 1490fa7d9493SBabu Moger { 1491fa7d9493SBabu Moger .name = "cbm_mask", 1492fa7d9493SBabu Moger .mode = 0444, 1493fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1494fa7d9493SBabu Moger .seq_show = rdt_default_ctrl_show, 1495fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1496fa7d9493SBabu Moger }, 1497fa7d9493SBabu Moger { 1498fa7d9493SBabu Moger .name = "min_cbm_bits", 1499fa7d9493SBabu Moger .mode = 0444, 1500fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1501fa7d9493SBabu Moger .seq_show = rdt_min_cbm_bits_show, 1502fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1503fa7d9493SBabu Moger }, 1504fa7d9493SBabu Moger { 1505fa7d9493SBabu Moger .name = "shareable_bits", 1506fa7d9493SBabu Moger .mode = 0444, 1507fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1508fa7d9493SBabu Moger .seq_show = rdt_shareable_bits_show, 1509fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1510fa7d9493SBabu Moger }, 1511fa7d9493SBabu Moger { 1512fa7d9493SBabu Moger .name = "bit_usage", 1513fa7d9493SBabu Moger .mode = 0444, 1514fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1515fa7d9493SBabu Moger .seq_show = rdt_bit_usage_show, 1516fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1517fa7d9493SBabu Moger }, 1518fa7d9493SBabu Moger { 1519fa7d9493SBabu Moger .name = "min_bandwidth", 1520fa7d9493SBabu Moger .mode = 0444, 1521fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1522fa7d9493SBabu Moger .seq_show = rdt_min_bw_show, 1523fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1524fa7d9493SBabu Moger }, 1525fa7d9493SBabu Moger { 1526fa7d9493SBabu Moger .name = "bandwidth_gran", 1527fa7d9493SBabu Moger .mode = 0444, 1528fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1529fa7d9493SBabu Moger .seq_show = rdt_bw_gran_show, 1530fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1531fa7d9493SBabu Moger }, 1532fa7d9493SBabu Moger { 1533fa7d9493SBabu Moger .name = "delay_linear", 1534fa7d9493SBabu Moger .mode = 0444, 1535fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1536fa7d9493SBabu Moger .seq_show = rdt_delay_linear_show, 1537fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1538fa7d9493SBabu Moger }, 1539*29b6bd41SFenghua Yu /* 1540*29b6bd41SFenghua Yu * Platform specific which (if any) capabilities are provided by 1541*29b6bd41SFenghua Yu * thread_throttle_mode. Defer "fflags" initialization to platform 1542*29b6bd41SFenghua Yu * discovery. 1543*29b6bd41SFenghua Yu */ 1544*29b6bd41SFenghua Yu { 1545*29b6bd41SFenghua Yu .name = "thread_throttle_mode", 1546*29b6bd41SFenghua Yu .mode = 0444, 1547*29b6bd41SFenghua Yu .kf_ops = &rdtgroup_kf_single_ops, 1548*29b6bd41SFenghua Yu .seq_show = rdt_thread_throttle_mode_show, 1549*29b6bd41SFenghua Yu }, 1550fa7d9493SBabu Moger { 1551fa7d9493SBabu Moger .name = "max_threshold_occupancy", 1552fa7d9493SBabu Moger .mode = 0644, 1553fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1554fa7d9493SBabu Moger .write = max_threshold_occ_write, 1555fa7d9493SBabu Moger .seq_show = max_threshold_occ_show, 1556fa7d9493SBabu Moger .fflags = RF_MON_INFO | RFTYPE_RES_CACHE, 1557fa7d9493SBabu Moger }, 1558fa7d9493SBabu Moger { 1559fa7d9493SBabu Moger .name = "cpus", 1560fa7d9493SBabu Moger .mode = 0644, 1561fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1562fa7d9493SBabu Moger .write = rdtgroup_cpus_write, 1563fa7d9493SBabu Moger .seq_show = rdtgroup_cpus_show, 1564fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1565fa7d9493SBabu Moger }, 1566fa7d9493SBabu Moger { 1567fa7d9493SBabu Moger .name = "cpus_list", 1568fa7d9493SBabu Moger .mode = 0644, 1569fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1570fa7d9493SBabu Moger .write = rdtgroup_cpus_write, 1571fa7d9493SBabu Moger .seq_show = rdtgroup_cpus_show, 1572fa7d9493SBabu Moger .flags = RFTYPE_FLAGS_CPUS_LIST, 1573fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1574fa7d9493SBabu Moger }, 1575fa7d9493SBabu Moger { 1576fa7d9493SBabu Moger .name = "tasks", 1577fa7d9493SBabu Moger .mode = 0644, 1578fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1579fa7d9493SBabu Moger .write = rdtgroup_tasks_write, 1580fa7d9493SBabu Moger .seq_show = rdtgroup_tasks_show, 1581fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1582fa7d9493SBabu Moger }, 1583fa7d9493SBabu Moger { 1584fa7d9493SBabu Moger .name = "schemata", 1585fa7d9493SBabu Moger .mode = 0644, 1586fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1587fa7d9493SBabu Moger .write = rdtgroup_schemata_write, 1588fa7d9493SBabu Moger .seq_show = rdtgroup_schemata_show, 1589fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1590fa7d9493SBabu Moger }, 1591fa7d9493SBabu Moger { 1592fa7d9493SBabu Moger .name = "mode", 1593fa7d9493SBabu Moger .mode = 0644, 1594fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1595fa7d9493SBabu Moger .write = rdtgroup_mode_write, 1596fa7d9493SBabu Moger .seq_show = rdtgroup_mode_show, 1597fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1598fa7d9493SBabu Moger }, 1599fa7d9493SBabu Moger { 1600fa7d9493SBabu Moger .name = "size", 1601fa7d9493SBabu Moger .mode = 0444, 1602fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1603fa7d9493SBabu Moger .seq_show = rdtgroup_size_show, 1604fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1605fa7d9493SBabu Moger }, 1606fa7d9493SBabu Moger 1607fa7d9493SBabu Moger }; 1608fa7d9493SBabu Moger 1609fa7d9493SBabu Moger static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags) 1610fa7d9493SBabu Moger { 1611fa7d9493SBabu Moger struct rftype *rfts, *rft; 1612fa7d9493SBabu Moger int ret, len; 1613fa7d9493SBabu Moger 1614fa7d9493SBabu Moger rfts = res_common_files; 1615fa7d9493SBabu Moger len = ARRAY_SIZE(res_common_files); 1616fa7d9493SBabu Moger 1617fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 1618fa7d9493SBabu Moger 1619fa7d9493SBabu Moger for (rft = rfts; rft < rfts + len; rft++) { 1620*29b6bd41SFenghua Yu if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) { 1621fa7d9493SBabu Moger ret = rdtgroup_add_file(kn, rft); 1622fa7d9493SBabu Moger if (ret) 1623fa7d9493SBabu Moger goto error; 1624fa7d9493SBabu Moger } 1625fa7d9493SBabu Moger } 1626fa7d9493SBabu Moger 1627fa7d9493SBabu Moger return 0; 1628fa7d9493SBabu Moger error: 1629fa7d9493SBabu Moger pr_warn("Failed to add %s, err=%d\n", rft->name, ret); 1630fa7d9493SBabu Moger while (--rft >= rfts) { 1631fa7d9493SBabu Moger if ((fflags & rft->fflags) == rft->fflags) 1632fa7d9493SBabu Moger kernfs_remove_by_name(kn, rft->name); 1633fa7d9493SBabu Moger } 1634fa7d9493SBabu Moger return ret; 1635fa7d9493SBabu Moger } 1636fa7d9493SBabu Moger 1637*29b6bd41SFenghua Yu static struct rftype *rdtgroup_get_rftype_by_name(const char *name) 1638*29b6bd41SFenghua Yu { 1639*29b6bd41SFenghua Yu struct rftype *rfts, *rft; 1640*29b6bd41SFenghua Yu int len; 1641*29b6bd41SFenghua Yu 1642*29b6bd41SFenghua Yu rfts = res_common_files; 1643*29b6bd41SFenghua Yu len = ARRAY_SIZE(res_common_files); 1644*29b6bd41SFenghua Yu 1645*29b6bd41SFenghua Yu for (rft = rfts; rft < rfts + len; rft++) { 1646*29b6bd41SFenghua Yu if (!strcmp(rft->name, name)) 1647*29b6bd41SFenghua Yu return rft; 1648*29b6bd41SFenghua Yu } 1649*29b6bd41SFenghua Yu 1650*29b6bd41SFenghua Yu return NULL; 1651*29b6bd41SFenghua Yu } 1652*29b6bd41SFenghua Yu 1653*29b6bd41SFenghua Yu void __init thread_throttle_mode_init(void) 1654*29b6bd41SFenghua Yu { 1655*29b6bd41SFenghua Yu struct rftype *rft; 1656*29b6bd41SFenghua Yu 1657*29b6bd41SFenghua Yu rft = rdtgroup_get_rftype_by_name("thread_throttle_mode"); 1658*29b6bd41SFenghua Yu if (!rft) 1659*29b6bd41SFenghua Yu return; 1660*29b6bd41SFenghua Yu 1661*29b6bd41SFenghua Yu rft->fflags = RF_CTRL_INFO | RFTYPE_RES_MB; 1662*29b6bd41SFenghua Yu } 1663*29b6bd41SFenghua Yu 1664fa7d9493SBabu Moger /** 1665fa7d9493SBabu Moger * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file 1666fa7d9493SBabu Moger * @r: The resource group with which the file is associated. 1667fa7d9493SBabu Moger * @name: Name of the file 1668fa7d9493SBabu Moger * 1669fa7d9493SBabu Moger * The permissions of named resctrl file, directory, or link are modified 1670fa7d9493SBabu Moger * to not allow read, write, or execute by any user. 1671fa7d9493SBabu Moger * 1672fa7d9493SBabu Moger * WARNING: This function is intended to communicate to the user that the 1673fa7d9493SBabu Moger * resctrl file has been locked down - that it is not relevant to the 1674fa7d9493SBabu Moger * particular state the system finds itself in. It should not be relied 1675fa7d9493SBabu Moger * on to protect from user access because after the file's permissions 1676fa7d9493SBabu Moger * are restricted the user can still change the permissions using chmod 1677fa7d9493SBabu Moger * from the command line. 1678fa7d9493SBabu Moger * 1679fa7d9493SBabu Moger * Return: 0 on success, <0 on failure. 1680fa7d9493SBabu Moger */ 1681fa7d9493SBabu Moger int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name) 1682fa7d9493SBabu Moger { 1683fa7d9493SBabu Moger struct iattr iattr = {.ia_valid = ATTR_MODE,}; 1684fa7d9493SBabu Moger struct kernfs_node *kn; 1685fa7d9493SBabu Moger int ret = 0; 1686fa7d9493SBabu Moger 1687fa7d9493SBabu Moger kn = kernfs_find_and_get_ns(r->kn, name, NULL); 1688fa7d9493SBabu Moger if (!kn) 1689fa7d9493SBabu Moger return -ENOENT; 1690fa7d9493SBabu Moger 1691fa7d9493SBabu Moger switch (kernfs_type(kn)) { 1692fa7d9493SBabu Moger case KERNFS_DIR: 1693fa7d9493SBabu Moger iattr.ia_mode = S_IFDIR; 1694fa7d9493SBabu Moger break; 1695fa7d9493SBabu Moger case KERNFS_FILE: 1696fa7d9493SBabu Moger iattr.ia_mode = S_IFREG; 1697fa7d9493SBabu Moger break; 1698fa7d9493SBabu Moger case KERNFS_LINK: 1699fa7d9493SBabu Moger iattr.ia_mode = S_IFLNK; 1700fa7d9493SBabu Moger break; 1701fa7d9493SBabu Moger } 1702fa7d9493SBabu Moger 1703fa7d9493SBabu Moger ret = kernfs_setattr(kn, &iattr); 1704fa7d9493SBabu Moger kernfs_put(kn); 1705fa7d9493SBabu Moger return ret; 1706fa7d9493SBabu Moger } 1707fa7d9493SBabu Moger 1708fa7d9493SBabu Moger /** 1709fa7d9493SBabu Moger * rdtgroup_kn_mode_restore - Restore user access to named resctrl file 1710fa7d9493SBabu Moger * @r: The resource group with which the file is associated. 1711fa7d9493SBabu Moger * @name: Name of the file 1712fa7d9493SBabu Moger * @mask: Mask of permissions that should be restored 1713fa7d9493SBabu Moger * 1714fa7d9493SBabu Moger * Restore the permissions of the named file. If @name is a directory the 1715fa7d9493SBabu Moger * permissions of its parent will be used. 1716fa7d9493SBabu Moger * 1717fa7d9493SBabu Moger * Return: 0 on success, <0 on failure. 1718fa7d9493SBabu Moger */ 1719fa7d9493SBabu Moger int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name, 1720fa7d9493SBabu Moger umode_t mask) 1721fa7d9493SBabu Moger { 1722fa7d9493SBabu Moger struct iattr iattr = {.ia_valid = ATTR_MODE,}; 1723fa7d9493SBabu Moger struct kernfs_node *kn, *parent; 1724fa7d9493SBabu Moger struct rftype *rfts, *rft; 1725fa7d9493SBabu Moger int ret, len; 1726fa7d9493SBabu Moger 1727fa7d9493SBabu Moger rfts = res_common_files; 1728fa7d9493SBabu Moger len = ARRAY_SIZE(res_common_files); 1729fa7d9493SBabu Moger 1730fa7d9493SBabu Moger for (rft = rfts; rft < rfts + len; rft++) { 1731fa7d9493SBabu Moger if (!strcmp(rft->name, name)) 1732fa7d9493SBabu Moger iattr.ia_mode = rft->mode & mask; 1733fa7d9493SBabu Moger } 1734fa7d9493SBabu Moger 1735fa7d9493SBabu Moger kn = kernfs_find_and_get_ns(r->kn, name, NULL); 1736fa7d9493SBabu Moger if (!kn) 1737fa7d9493SBabu Moger return -ENOENT; 1738fa7d9493SBabu Moger 1739fa7d9493SBabu Moger switch (kernfs_type(kn)) { 1740fa7d9493SBabu Moger case KERNFS_DIR: 1741fa7d9493SBabu Moger parent = kernfs_get_parent(kn); 1742fa7d9493SBabu Moger if (parent) { 1743fa7d9493SBabu Moger iattr.ia_mode |= parent->mode; 1744fa7d9493SBabu Moger kernfs_put(parent); 1745fa7d9493SBabu Moger } 1746fa7d9493SBabu Moger iattr.ia_mode |= S_IFDIR; 1747fa7d9493SBabu Moger break; 1748fa7d9493SBabu Moger case KERNFS_FILE: 1749fa7d9493SBabu Moger iattr.ia_mode |= S_IFREG; 1750fa7d9493SBabu Moger break; 1751fa7d9493SBabu Moger case KERNFS_LINK: 1752fa7d9493SBabu Moger iattr.ia_mode |= S_IFLNK; 1753fa7d9493SBabu Moger break; 1754fa7d9493SBabu Moger } 1755fa7d9493SBabu Moger 1756fa7d9493SBabu Moger ret = kernfs_setattr(kn, &iattr); 1757fa7d9493SBabu Moger kernfs_put(kn); 1758fa7d9493SBabu Moger return ret; 1759fa7d9493SBabu Moger } 1760fa7d9493SBabu Moger 1761fa7d9493SBabu Moger static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name, 1762fa7d9493SBabu Moger unsigned long fflags) 1763fa7d9493SBabu Moger { 1764fa7d9493SBabu Moger struct kernfs_node *kn_subdir; 1765fa7d9493SBabu Moger int ret; 1766fa7d9493SBabu Moger 1767fa7d9493SBabu Moger kn_subdir = kernfs_create_dir(kn_info, name, 1768fa7d9493SBabu Moger kn_info->mode, r); 1769fa7d9493SBabu Moger if (IS_ERR(kn_subdir)) 1770fa7d9493SBabu Moger return PTR_ERR(kn_subdir); 1771fa7d9493SBabu Moger 1772fa7d9493SBabu Moger kernfs_get(kn_subdir); 1773fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn_subdir); 1774fa7d9493SBabu Moger if (ret) 1775fa7d9493SBabu Moger return ret; 1776fa7d9493SBabu Moger 1777fa7d9493SBabu Moger ret = rdtgroup_add_files(kn_subdir, fflags); 1778fa7d9493SBabu Moger if (!ret) 1779fa7d9493SBabu Moger kernfs_activate(kn_subdir); 1780fa7d9493SBabu Moger 1781fa7d9493SBabu Moger return ret; 1782fa7d9493SBabu Moger } 1783fa7d9493SBabu Moger 1784fa7d9493SBabu Moger static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn) 1785fa7d9493SBabu Moger { 1786fa7d9493SBabu Moger struct rdt_resource *r; 1787fa7d9493SBabu Moger unsigned long fflags; 1788fa7d9493SBabu Moger char name[32]; 1789fa7d9493SBabu Moger int ret; 1790fa7d9493SBabu Moger 1791fa7d9493SBabu Moger /* create the directory */ 1792fa7d9493SBabu Moger kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL); 1793fa7d9493SBabu Moger if (IS_ERR(kn_info)) 1794fa7d9493SBabu Moger return PTR_ERR(kn_info); 1795fa7d9493SBabu Moger kernfs_get(kn_info); 1796fa7d9493SBabu Moger 1797fa7d9493SBabu Moger ret = rdtgroup_add_files(kn_info, RF_TOP_INFO); 1798fa7d9493SBabu Moger if (ret) 1799fa7d9493SBabu Moger goto out_destroy; 1800fa7d9493SBabu Moger 1801fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 1802fa7d9493SBabu Moger fflags = r->fflags | RF_CTRL_INFO; 1803fa7d9493SBabu Moger ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags); 1804fa7d9493SBabu Moger if (ret) 1805fa7d9493SBabu Moger goto out_destroy; 1806fa7d9493SBabu Moger } 1807fa7d9493SBabu Moger 1808fa7d9493SBabu Moger for_each_mon_enabled_rdt_resource(r) { 1809fa7d9493SBabu Moger fflags = r->fflags | RF_MON_INFO; 1810fa7d9493SBabu Moger sprintf(name, "%s_MON", r->name); 1811fa7d9493SBabu Moger ret = rdtgroup_mkdir_info_resdir(r, name, fflags); 1812fa7d9493SBabu Moger if (ret) 1813fa7d9493SBabu Moger goto out_destroy; 1814fa7d9493SBabu Moger } 1815fa7d9493SBabu Moger 1816fa7d9493SBabu Moger /* 1817fa7d9493SBabu Moger * This extra ref will be put in kernfs_remove() and guarantees 1818fa7d9493SBabu Moger * that @rdtgrp->kn is always accessible. 1819fa7d9493SBabu Moger */ 1820fa7d9493SBabu Moger kernfs_get(kn_info); 1821fa7d9493SBabu Moger 1822fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn_info); 1823fa7d9493SBabu Moger if (ret) 1824fa7d9493SBabu Moger goto out_destroy; 1825fa7d9493SBabu Moger 1826fa7d9493SBabu Moger kernfs_activate(kn_info); 1827fa7d9493SBabu Moger 1828fa7d9493SBabu Moger return 0; 1829fa7d9493SBabu Moger 1830fa7d9493SBabu Moger out_destroy: 1831fa7d9493SBabu Moger kernfs_remove(kn_info); 1832fa7d9493SBabu Moger return ret; 1833fa7d9493SBabu Moger } 1834fa7d9493SBabu Moger 1835fa7d9493SBabu Moger static int 1836fa7d9493SBabu Moger mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp, 1837fa7d9493SBabu Moger char *name, struct kernfs_node **dest_kn) 1838fa7d9493SBabu Moger { 1839fa7d9493SBabu Moger struct kernfs_node *kn; 1840fa7d9493SBabu Moger int ret; 1841fa7d9493SBabu Moger 1842fa7d9493SBabu Moger /* create the directory */ 1843fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 1844fa7d9493SBabu Moger if (IS_ERR(kn)) 1845fa7d9493SBabu Moger return PTR_ERR(kn); 1846fa7d9493SBabu Moger 1847fa7d9493SBabu Moger if (dest_kn) 1848fa7d9493SBabu Moger *dest_kn = kn; 1849fa7d9493SBabu Moger 1850fa7d9493SBabu Moger /* 1851fa7d9493SBabu Moger * This extra ref will be put in kernfs_remove() and guarantees 1852fa7d9493SBabu Moger * that @rdtgrp->kn is always accessible. 1853fa7d9493SBabu Moger */ 1854fa7d9493SBabu Moger kernfs_get(kn); 1855fa7d9493SBabu Moger 1856fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 1857fa7d9493SBabu Moger if (ret) 1858fa7d9493SBabu Moger goto out_destroy; 1859fa7d9493SBabu Moger 1860fa7d9493SBabu Moger kernfs_activate(kn); 1861fa7d9493SBabu Moger 1862fa7d9493SBabu Moger return 0; 1863fa7d9493SBabu Moger 1864fa7d9493SBabu Moger out_destroy: 1865fa7d9493SBabu Moger kernfs_remove(kn); 1866fa7d9493SBabu Moger return ret; 1867fa7d9493SBabu Moger } 1868fa7d9493SBabu Moger 1869fa7d9493SBabu Moger static void l3_qos_cfg_update(void *arg) 1870fa7d9493SBabu Moger { 1871fa7d9493SBabu Moger bool *enable = arg; 1872fa7d9493SBabu Moger 1873aa50453aSBabu Moger wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL); 1874fa7d9493SBabu Moger } 1875fa7d9493SBabu Moger 1876fa7d9493SBabu Moger static void l2_qos_cfg_update(void *arg) 1877fa7d9493SBabu Moger { 1878fa7d9493SBabu Moger bool *enable = arg; 1879fa7d9493SBabu Moger 1880aa50453aSBabu Moger wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL); 1881fa7d9493SBabu Moger } 1882fa7d9493SBabu Moger 1883fa7d9493SBabu Moger static inline bool is_mba_linear(void) 1884fa7d9493SBabu Moger { 1885fa7d9493SBabu Moger return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear; 1886fa7d9493SBabu Moger } 1887fa7d9493SBabu Moger 1888fa7d9493SBabu Moger static int set_cache_qos_cfg(int level, bool enable) 1889fa7d9493SBabu Moger { 1890fa7d9493SBabu Moger void (*update)(void *arg); 1891fa7d9493SBabu Moger struct rdt_resource *r_l; 1892fa7d9493SBabu Moger cpumask_var_t cpu_mask; 1893fa7d9493SBabu Moger struct rdt_domain *d; 1894fa7d9493SBabu Moger int cpu; 1895fa7d9493SBabu Moger 1896fa7d9493SBabu Moger if (level == RDT_RESOURCE_L3) 1897fa7d9493SBabu Moger update = l3_qos_cfg_update; 1898fa7d9493SBabu Moger else if (level == RDT_RESOURCE_L2) 1899fa7d9493SBabu Moger update = l2_qos_cfg_update; 1900fa7d9493SBabu Moger else 1901fa7d9493SBabu Moger return -EINVAL; 1902fa7d9493SBabu Moger 1903ab6a2114SShakeel Butt if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) 1904ab6a2114SShakeel Butt return -ENOMEM; 1905ab6a2114SShakeel Butt 1906fa7d9493SBabu Moger r_l = &rdt_resources_all[level]; 1907fa7d9493SBabu Moger list_for_each_entry(d, &r_l->domains, list) { 1908fa7d9493SBabu Moger /* Pick one CPU from each domain instance to update MSR */ 1909fa7d9493SBabu Moger cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); 1910fa7d9493SBabu Moger } 1911fa7d9493SBabu Moger cpu = get_cpu(); 1912fa7d9493SBabu Moger /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */ 1913fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 1914fa7d9493SBabu Moger update(&enable); 1915fa7d9493SBabu Moger /* Update QOS_CFG MSR on all other cpus in cpu_mask. */ 1916fa7d9493SBabu Moger smp_call_function_many(cpu_mask, update, &enable, 1); 1917fa7d9493SBabu Moger put_cpu(); 1918fa7d9493SBabu Moger 1919fa7d9493SBabu Moger free_cpumask_var(cpu_mask); 1920fa7d9493SBabu Moger 1921fa7d9493SBabu Moger return 0; 1922fa7d9493SBabu Moger } 1923fa7d9493SBabu Moger 19249fe04507SJames Morse /* Restore the qos cfg state when a domain comes online */ 19259fe04507SJames Morse void rdt_domain_reconfigure_cdp(struct rdt_resource *r) 19269fe04507SJames Morse { 19279fe04507SJames Morse if (!r->alloc_capable) 19289fe04507SJames Morse return; 19299fe04507SJames Morse 19309fe04507SJames Morse if (r == &rdt_resources_all[RDT_RESOURCE_L2DATA]) 19319fe04507SJames Morse l2_qos_cfg_update(&r->alloc_enabled); 19329fe04507SJames Morse 19339fe04507SJames Morse if (r == &rdt_resources_all[RDT_RESOURCE_L3DATA]) 19349fe04507SJames Morse l3_qos_cfg_update(&r->alloc_enabled); 19359fe04507SJames Morse } 19369fe04507SJames Morse 1937fa7d9493SBabu Moger /* 1938fa7d9493SBabu Moger * Enable or disable the MBA software controller 1939fa7d9493SBabu Moger * which helps user specify bandwidth in MBps. 1940fa7d9493SBabu Moger * MBA software controller is supported only if 1941fa7d9493SBabu Moger * MBM is supported and MBA is in linear scale. 1942fa7d9493SBabu Moger */ 1943fa7d9493SBabu Moger static int set_mba_sc(bool mba_sc) 1944fa7d9493SBabu Moger { 1945fa7d9493SBabu Moger struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA]; 1946fa7d9493SBabu Moger struct rdt_domain *d; 1947fa7d9493SBabu Moger 1948fa7d9493SBabu Moger if (!is_mbm_enabled() || !is_mba_linear() || 1949fa7d9493SBabu Moger mba_sc == is_mba_sc(r)) 1950fa7d9493SBabu Moger return -EINVAL; 1951fa7d9493SBabu Moger 1952fa7d9493SBabu Moger r->membw.mba_sc = mba_sc; 1953fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) 1954fa7d9493SBabu Moger setup_default_ctrlval(r, d->ctrl_val, d->mbps_val); 1955fa7d9493SBabu Moger 1956fa7d9493SBabu Moger return 0; 1957fa7d9493SBabu Moger } 1958fa7d9493SBabu Moger 1959fa7d9493SBabu Moger static int cdp_enable(int level, int data_type, int code_type) 1960fa7d9493SBabu Moger { 1961fa7d9493SBabu Moger struct rdt_resource *r_ldata = &rdt_resources_all[data_type]; 1962fa7d9493SBabu Moger struct rdt_resource *r_lcode = &rdt_resources_all[code_type]; 1963fa7d9493SBabu Moger struct rdt_resource *r_l = &rdt_resources_all[level]; 1964fa7d9493SBabu Moger int ret; 1965fa7d9493SBabu Moger 1966fa7d9493SBabu Moger if (!r_l->alloc_capable || !r_ldata->alloc_capable || 1967fa7d9493SBabu Moger !r_lcode->alloc_capable) 1968fa7d9493SBabu Moger return -EINVAL; 1969fa7d9493SBabu Moger 1970fa7d9493SBabu Moger ret = set_cache_qos_cfg(level, true); 1971fa7d9493SBabu Moger if (!ret) { 1972fa7d9493SBabu Moger r_l->alloc_enabled = false; 1973fa7d9493SBabu Moger r_ldata->alloc_enabled = true; 1974fa7d9493SBabu Moger r_lcode->alloc_enabled = true; 1975fa7d9493SBabu Moger } 1976fa7d9493SBabu Moger return ret; 1977fa7d9493SBabu Moger } 1978fa7d9493SBabu Moger 1979fa7d9493SBabu Moger static int cdpl3_enable(void) 1980fa7d9493SBabu Moger { 1981fa7d9493SBabu Moger return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, 1982fa7d9493SBabu Moger RDT_RESOURCE_L3CODE); 1983fa7d9493SBabu Moger } 1984fa7d9493SBabu Moger 1985fa7d9493SBabu Moger static int cdpl2_enable(void) 1986fa7d9493SBabu Moger { 1987fa7d9493SBabu Moger return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, 1988fa7d9493SBabu Moger RDT_RESOURCE_L2CODE); 1989fa7d9493SBabu Moger } 1990fa7d9493SBabu Moger 1991fa7d9493SBabu Moger static void cdp_disable(int level, int data_type, int code_type) 1992fa7d9493SBabu Moger { 1993fa7d9493SBabu Moger struct rdt_resource *r = &rdt_resources_all[level]; 1994fa7d9493SBabu Moger 1995fa7d9493SBabu Moger r->alloc_enabled = r->alloc_capable; 1996fa7d9493SBabu Moger 1997fa7d9493SBabu Moger if (rdt_resources_all[data_type].alloc_enabled) { 1998fa7d9493SBabu Moger rdt_resources_all[data_type].alloc_enabled = false; 1999fa7d9493SBabu Moger rdt_resources_all[code_type].alloc_enabled = false; 2000fa7d9493SBabu Moger set_cache_qos_cfg(level, false); 2001fa7d9493SBabu Moger } 2002fa7d9493SBabu Moger } 2003fa7d9493SBabu Moger 2004fa7d9493SBabu Moger static void cdpl3_disable(void) 2005fa7d9493SBabu Moger { 2006fa7d9493SBabu Moger cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE); 2007fa7d9493SBabu Moger } 2008fa7d9493SBabu Moger 2009fa7d9493SBabu Moger static void cdpl2_disable(void) 2010fa7d9493SBabu Moger { 2011fa7d9493SBabu Moger cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE); 2012fa7d9493SBabu Moger } 2013fa7d9493SBabu Moger 2014fa7d9493SBabu Moger static void cdp_disable_all(void) 2015fa7d9493SBabu Moger { 2016fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) 2017fa7d9493SBabu Moger cdpl3_disable(); 2018fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) 2019fa7d9493SBabu Moger cdpl2_disable(); 2020fa7d9493SBabu Moger } 2021fa7d9493SBabu Moger 2022fa7d9493SBabu Moger /* 2023fa7d9493SBabu Moger * We don't allow rdtgroup directories to be created anywhere 2024fa7d9493SBabu Moger * except the root directory. Thus when looking for the rdtgroup 2025fa7d9493SBabu Moger * structure for a kernfs node we are either looking at a directory, 2026fa7d9493SBabu Moger * in which case the rdtgroup structure is pointed at by the "priv" 2027fa7d9493SBabu Moger * field, otherwise we have a file, and need only look to the parent 2028fa7d9493SBabu Moger * to find the rdtgroup. 2029fa7d9493SBabu Moger */ 2030fa7d9493SBabu Moger static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn) 2031fa7d9493SBabu Moger { 2032fa7d9493SBabu Moger if (kernfs_type(kn) == KERNFS_DIR) { 2033fa7d9493SBabu Moger /* 2034fa7d9493SBabu Moger * All the resource directories use "kn->priv" 2035fa7d9493SBabu Moger * to point to the "struct rdtgroup" for the 2036fa7d9493SBabu Moger * resource. "info" and its subdirectories don't 2037fa7d9493SBabu Moger * have rdtgroup structures, so return NULL here. 2038fa7d9493SBabu Moger */ 2039fa7d9493SBabu Moger if (kn == kn_info || kn->parent == kn_info) 2040fa7d9493SBabu Moger return NULL; 2041fa7d9493SBabu Moger else 2042fa7d9493SBabu Moger return kn->priv; 2043fa7d9493SBabu Moger } else { 2044fa7d9493SBabu Moger return kn->parent->priv; 2045fa7d9493SBabu Moger } 2046fa7d9493SBabu Moger } 2047fa7d9493SBabu Moger 2048fa7d9493SBabu Moger struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn) 2049fa7d9493SBabu Moger { 2050fa7d9493SBabu Moger struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 2051fa7d9493SBabu Moger 2052fa7d9493SBabu Moger if (!rdtgrp) 2053fa7d9493SBabu Moger return NULL; 2054fa7d9493SBabu Moger 2055fa7d9493SBabu Moger atomic_inc(&rdtgrp->waitcount); 2056fa7d9493SBabu Moger kernfs_break_active_protection(kn); 2057fa7d9493SBabu Moger 2058fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2059fa7d9493SBabu Moger 2060fa7d9493SBabu Moger /* Was this group deleted while we waited? */ 2061fa7d9493SBabu Moger if (rdtgrp->flags & RDT_DELETED) 2062fa7d9493SBabu Moger return NULL; 2063fa7d9493SBabu Moger 2064fa7d9493SBabu Moger return rdtgrp; 2065fa7d9493SBabu Moger } 2066fa7d9493SBabu Moger 2067fa7d9493SBabu Moger void rdtgroup_kn_unlock(struct kernfs_node *kn) 2068fa7d9493SBabu Moger { 2069fa7d9493SBabu Moger struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 2070fa7d9493SBabu Moger 2071fa7d9493SBabu Moger if (!rdtgrp) 2072fa7d9493SBabu Moger return; 2073fa7d9493SBabu Moger 2074fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2075fa7d9493SBabu Moger 2076fa7d9493SBabu Moger if (atomic_dec_and_test(&rdtgrp->waitcount) && 2077fa7d9493SBabu Moger (rdtgrp->flags & RDT_DELETED)) { 2078fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2079fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 2080fa7d9493SBabu Moger rdtgroup_pseudo_lock_remove(rdtgrp); 2081fa7d9493SBabu Moger kernfs_unbreak_active_protection(kn); 2082fa7d9493SBabu Moger kernfs_put(rdtgrp->kn); 2083fa7d9493SBabu Moger kfree(rdtgrp); 2084fa7d9493SBabu Moger } else { 2085fa7d9493SBabu Moger kernfs_unbreak_active_protection(kn); 2086fa7d9493SBabu Moger } 2087fa7d9493SBabu Moger } 2088fa7d9493SBabu Moger 2089fa7d9493SBabu Moger static int mkdir_mondata_all(struct kernfs_node *parent_kn, 2090fa7d9493SBabu Moger struct rdtgroup *prgrp, 2091fa7d9493SBabu Moger struct kernfs_node **mon_data_kn); 2092fa7d9493SBabu Moger 209323bf1b6bSDavid Howells static int rdt_enable_ctx(struct rdt_fs_context *ctx) 2094fa7d9493SBabu Moger { 209523bf1b6bSDavid Howells int ret = 0; 209623bf1b6bSDavid Howells 209723bf1b6bSDavid Howells if (ctx->enable_cdpl2) 209823bf1b6bSDavid Howells ret = cdpl2_enable(); 209923bf1b6bSDavid Howells 210023bf1b6bSDavid Howells if (!ret && ctx->enable_cdpl3) 210123bf1b6bSDavid Howells ret = cdpl3_enable(); 210223bf1b6bSDavid Howells 210323bf1b6bSDavid Howells if (!ret && ctx->enable_mba_mbps) 210423bf1b6bSDavid Howells ret = set_mba_sc(true); 210523bf1b6bSDavid Howells 210623bf1b6bSDavid Howells return ret; 210723bf1b6bSDavid Howells } 210823bf1b6bSDavid Howells 210923bf1b6bSDavid Howells static int rdt_get_tree(struct fs_context *fc) 211023bf1b6bSDavid Howells { 211123bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 2112fa7d9493SBabu Moger struct rdt_domain *dom; 2113fa7d9493SBabu Moger struct rdt_resource *r; 2114fa7d9493SBabu Moger int ret; 2115fa7d9493SBabu Moger 2116fa7d9493SBabu Moger cpus_read_lock(); 2117fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2118fa7d9493SBabu Moger /* 2119fa7d9493SBabu Moger * resctrl file system can only be mounted once. 2120fa7d9493SBabu Moger */ 2121fa7d9493SBabu Moger if (static_branch_unlikely(&rdt_enable_key)) { 212223bf1b6bSDavid Howells ret = -EBUSY; 2123fa7d9493SBabu Moger goto out; 2124fa7d9493SBabu Moger } 2125fa7d9493SBabu Moger 212623bf1b6bSDavid Howells ret = rdt_enable_ctx(ctx); 212723bf1b6bSDavid Howells if (ret < 0) 2128fa7d9493SBabu Moger goto out_cdp; 2129fa7d9493SBabu Moger 2130fa7d9493SBabu Moger closid_init(); 2131fa7d9493SBabu Moger 2132fa7d9493SBabu Moger ret = rdtgroup_create_info_dir(rdtgroup_default.kn); 213323bf1b6bSDavid Howells if (ret < 0) 213423bf1b6bSDavid Howells goto out_mba; 2135fa7d9493SBabu Moger 2136fa7d9493SBabu Moger if (rdt_mon_capable) { 2137fa7d9493SBabu Moger ret = mongroup_create_dir(rdtgroup_default.kn, 2138334b0f4eSXiaochen Shen &rdtgroup_default, "mon_groups", 2139fa7d9493SBabu Moger &kn_mongrp); 214023bf1b6bSDavid Howells if (ret < 0) 2141fa7d9493SBabu Moger goto out_info; 2142fa7d9493SBabu Moger kernfs_get(kn_mongrp); 2143fa7d9493SBabu Moger 2144fa7d9493SBabu Moger ret = mkdir_mondata_all(rdtgroup_default.kn, 2145fa7d9493SBabu Moger &rdtgroup_default, &kn_mondata); 214623bf1b6bSDavid Howells if (ret < 0) 2147fa7d9493SBabu Moger goto out_mongrp; 2148fa7d9493SBabu Moger kernfs_get(kn_mondata); 2149fa7d9493SBabu Moger rdtgroup_default.mon.mon_data_kn = kn_mondata; 2150fa7d9493SBabu Moger } 2151fa7d9493SBabu Moger 2152fa7d9493SBabu Moger ret = rdt_pseudo_lock_init(); 215323bf1b6bSDavid Howells if (ret) 2154fa7d9493SBabu Moger goto out_mondata; 2155fa7d9493SBabu Moger 215623bf1b6bSDavid Howells ret = kernfs_get_tree(fc); 215723bf1b6bSDavid Howells if (ret < 0) 2158fa7d9493SBabu Moger goto out_psl; 2159fa7d9493SBabu Moger 2160fa7d9493SBabu Moger if (rdt_alloc_capable) 2161fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_alloc_enable_key); 2162fa7d9493SBabu Moger if (rdt_mon_capable) 2163fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_mon_enable_key); 2164fa7d9493SBabu Moger 2165fa7d9493SBabu Moger if (rdt_alloc_capable || rdt_mon_capable) 2166fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_enable_key); 2167fa7d9493SBabu Moger 2168fa7d9493SBabu Moger if (is_mbm_enabled()) { 2169fa7d9493SBabu Moger r = &rdt_resources_all[RDT_RESOURCE_L3]; 2170fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) 2171fa7d9493SBabu Moger mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL); 2172fa7d9493SBabu Moger } 2173fa7d9493SBabu Moger 2174fa7d9493SBabu Moger goto out; 2175fa7d9493SBabu Moger 2176fa7d9493SBabu Moger out_psl: 2177fa7d9493SBabu Moger rdt_pseudo_lock_release(); 2178fa7d9493SBabu Moger out_mondata: 2179fa7d9493SBabu Moger if (rdt_mon_capable) 2180fa7d9493SBabu Moger kernfs_remove(kn_mondata); 2181fa7d9493SBabu Moger out_mongrp: 2182fa7d9493SBabu Moger if (rdt_mon_capable) 2183fa7d9493SBabu Moger kernfs_remove(kn_mongrp); 2184fa7d9493SBabu Moger out_info: 2185fa7d9493SBabu Moger kernfs_remove(kn_info); 218623bf1b6bSDavid Howells out_mba: 218723bf1b6bSDavid Howells if (ctx->enable_mba_mbps) 218823bf1b6bSDavid Howells set_mba_sc(false); 2189fa7d9493SBabu Moger out_cdp: 2190fa7d9493SBabu Moger cdp_disable_all(); 2191fa7d9493SBabu Moger out: 2192fa7d9493SBabu Moger rdt_last_cmd_clear(); 2193fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2194fa7d9493SBabu Moger cpus_read_unlock(); 219523bf1b6bSDavid Howells return ret; 219623bf1b6bSDavid Howells } 2197fa7d9493SBabu Moger 219823bf1b6bSDavid Howells enum rdt_param { 219923bf1b6bSDavid Howells Opt_cdp, 220023bf1b6bSDavid Howells Opt_cdpl2, 2201faa3604eSXiaochen Shen Opt_mba_mbps, 220223bf1b6bSDavid Howells nr__rdt_params 220323bf1b6bSDavid Howells }; 220423bf1b6bSDavid Howells 2205d7167b14SAl Viro static const struct fs_parameter_spec rdt_fs_parameters[] = { 220623bf1b6bSDavid Howells fsparam_flag("cdp", Opt_cdp), 220723bf1b6bSDavid Howells fsparam_flag("cdpl2", Opt_cdpl2), 2208faa3604eSXiaochen Shen fsparam_flag("mba_MBps", Opt_mba_mbps), 220923bf1b6bSDavid Howells {} 221023bf1b6bSDavid Howells }; 221123bf1b6bSDavid Howells 221223bf1b6bSDavid Howells static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param) 221323bf1b6bSDavid Howells { 221423bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 221523bf1b6bSDavid Howells struct fs_parse_result result; 221623bf1b6bSDavid Howells int opt; 221723bf1b6bSDavid Howells 2218d7167b14SAl Viro opt = fs_parse(fc, rdt_fs_parameters, param, &result); 221923bf1b6bSDavid Howells if (opt < 0) 222023bf1b6bSDavid Howells return opt; 222123bf1b6bSDavid Howells 222223bf1b6bSDavid Howells switch (opt) { 222323bf1b6bSDavid Howells case Opt_cdp: 222423bf1b6bSDavid Howells ctx->enable_cdpl3 = true; 222523bf1b6bSDavid Howells return 0; 222623bf1b6bSDavid Howells case Opt_cdpl2: 222723bf1b6bSDavid Howells ctx->enable_cdpl2 = true; 222823bf1b6bSDavid Howells return 0; 2229faa3604eSXiaochen Shen case Opt_mba_mbps: 223023bf1b6bSDavid Howells if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 223123bf1b6bSDavid Howells return -EINVAL; 223223bf1b6bSDavid Howells ctx->enable_mba_mbps = true; 223323bf1b6bSDavid Howells return 0; 223423bf1b6bSDavid Howells } 223523bf1b6bSDavid Howells 223623bf1b6bSDavid Howells return -EINVAL; 223723bf1b6bSDavid Howells } 223823bf1b6bSDavid Howells 223923bf1b6bSDavid Howells static void rdt_fs_context_free(struct fs_context *fc) 224023bf1b6bSDavid Howells { 224123bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 224223bf1b6bSDavid Howells 224323bf1b6bSDavid Howells kernfs_free_fs_context(fc); 224423bf1b6bSDavid Howells kfree(ctx); 224523bf1b6bSDavid Howells } 224623bf1b6bSDavid Howells 224723bf1b6bSDavid Howells static const struct fs_context_operations rdt_fs_context_ops = { 224823bf1b6bSDavid Howells .free = rdt_fs_context_free, 224923bf1b6bSDavid Howells .parse_param = rdt_parse_param, 225023bf1b6bSDavid Howells .get_tree = rdt_get_tree, 225123bf1b6bSDavid Howells }; 225223bf1b6bSDavid Howells 225323bf1b6bSDavid Howells static int rdt_init_fs_context(struct fs_context *fc) 225423bf1b6bSDavid Howells { 225523bf1b6bSDavid Howells struct rdt_fs_context *ctx; 225623bf1b6bSDavid Howells 225723bf1b6bSDavid Howells ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL); 225823bf1b6bSDavid Howells if (!ctx) 225923bf1b6bSDavid Howells return -ENOMEM; 226023bf1b6bSDavid Howells 226123bf1b6bSDavid Howells ctx->kfc.root = rdt_root; 226223bf1b6bSDavid Howells ctx->kfc.magic = RDTGROUP_SUPER_MAGIC; 226323bf1b6bSDavid Howells fc->fs_private = &ctx->kfc; 226423bf1b6bSDavid Howells fc->ops = &rdt_fs_context_ops; 226523bf1b6bSDavid Howells put_user_ns(fc->user_ns); 226623bf1b6bSDavid Howells fc->user_ns = get_user_ns(&init_user_ns); 226723bf1b6bSDavid Howells fc->global = true; 226823bf1b6bSDavid Howells return 0; 2269fa7d9493SBabu Moger } 2270fa7d9493SBabu Moger 2271fa7d9493SBabu Moger static int reset_all_ctrls(struct rdt_resource *r) 2272fa7d9493SBabu Moger { 2273fa7d9493SBabu Moger struct msr_param msr_param; 2274fa7d9493SBabu Moger cpumask_var_t cpu_mask; 2275fa7d9493SBabu Moger struct rdt_domain *d; 2276fa7d9493SBabu Moger int i, cpu; 2277fa7d9493SBabu Moger 2278fa7d9493SBabu Moger if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) 2279fa7d9493SBabu Moger return -ENOMEM; 2280fa7d9493SBabu Moger 2281fa7d9493SBabu Moger msr_param.res = r; 2282fa7d9493SBabu Moger msr_param.low = 0; 2283fa7d9493SBabu Moger msr_param.high = r->num_closid; 2284fa7d9493SBabu Moger 2285fa7d9493SBabu Moger /* 2286fa7d9493SBabu Moger * Disable resource control for this resource by setting all 2287fa7d9493SBabu Moger * CBMs in all domains to the maximum mask value. Pick one CPU 2288fa7d9493SBabu Moger * from each domain to update the MSRs below. 2289fa7d9493SBabu Moger */ 2290fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 2291fa7d9493SBabu Moger cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); 2292fa7d9493SBabu Moger 2293fa7d9493SBabu Moger for (i = 0; i < r->num_closid; i++) 2294fa7d9493SBabu Moger d->ctrl_val[i] = r->default_ctrl; 2295fa7d9493SBabu Moger } 2296fa7d9493SBabu Moger cpu = get_cpu(); 2297fa7d9493SBabu Moger /* Update CBM on this cpu if it's in cpu_mask. */ 2298fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 2299fa7d9493SBabu Moger rdt_ctrl_update(&msr_param); 2300fa7d9493SBabu Moger /* Update CBM on all other cpus in cpu_mask. */ 2301fa7d9493SBabu Moger smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1); 2302fa7d9493SBabu Moger put_cpu(); 2303fa7d9493SBabu Moger 2304fa7d9493SBabu Moger free_cpumask_var(cpu_mask); 2305fa7d9493SBabu Moger 2306fa7d9493SBabu Moger return 0; 2307fa7d9493SBabu Moger } 2308fa7d9493SBabu Moger 2309fa7d9493SBabu Moger /* 2310fa7d9493SBabu Moger * Move tasks from one to the other group. If @from is NULL, then all tasks 2311fa7d9493SBabu Moger * in the systems are moved unconditionally (used for teardown). 2312fa7d9493SBabu Moger * 2313fa7d9493SBabu Moger * If @mask is not NULL the cpus on which moved tasks are running are set 2314fa7d9493SBabu Moger * in that mask so the update smp function call is restricted to affected 2315fa7d9493SBabu Moger * cpus. 2316fa7d9493SBabu Moger */ 2317fa7d9493SBabu Moger static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, 2318fa7d9493SBabu Moger struct cpumask *mask) 2319fa7d9493SBabu Moger { 2320fa7d9493SBabu Moger struct task_struct *p, *t; 2321fa7d9493SBabu Moger 2322fa7d9493SBabu Moger read_lock(&tasklist_lock); 2323fa7d9493SBabu Moger for_each_process_thread(p, t) { 2324fa7d9493SBabu Moger if (!from || is_closid_match(t, from) || 2325fa7d9493SBabu Moger is_rmid_match(t, from)) { 2326fa7d9493SBabu Moger t->closid = to->closid; 2327fa7d9493SBabu Moger t->rmid = to->mon.rmid; 2328fa7d9493SBabu Moger 2329fa7d9493SBabu Moger #ifdef CONFIG_SMP 2330fa7d9493SBabu Moger /* 2331fa7d9493SBabu Moger * This is safe on x86 w/o barriers as the ordering 2332fa7d9493SBabu Moger * of writing to task_cpu() and t->on_cpu is 2333fa7d9493SBabu Moger * reverse to the reading here. The detection is 2334fa7d9493SBabu Moger * inaccurate as tasks might move or schedule 2335fa7d9493SBabu Moger * before the smp function call takes place. In 2336fa7d9493SBabu Moger * such a case the function call is pointless, but 2337fa7d9493SBabu Moger * there is no other side effect. 2338fa7d9493SBabu Moger */ 2339fa7d9493SBabu Moger if (mask && t->on_cpu) 2340fa7d9493SBabu Moger cpumask_set_cpu(task_cpu(t), mask); 2341fa7d9493SBabu Moger #endif 2342fa7d9493SBabu Moger } 2343fa7d9493SBabu Moger } 2344fa7d9493SBabu Moger read_unlock(&tasklist_lock); 2345fa7d9493SBabu Moger } 2346fa7d9493SBabu Moger 2347fa7d9493SBabu Moger static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp) 2348fa7d9493SBabu Moger { 2349fa7d9493SBabu Moger struct rdtgroup *sentry, *stmp; 2350fa7d9493SBabu Moger struct list_head *head; 2351fa7d9493SBabu Moger 2352fa7d9493SBabu Moger head = &rdtgrp->mon.crdtgrp_list; 2353fa7d9493SBabu Moger list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) { 2354fa7d9493SBabu Moger free_rmid(sentry->mon.rmid); 2355fa7d9493SBabu Moger list_del(&sentry->mon.crdtgrp_list); 2356b8511cccSXiaochen Shen 2357b8511cccSXiaochen Shen if (atomic_read(&sentry->waitcount) != 0) 2358b8511cccSXiaochen Shen sentry->flags = RDT_DELETED; 2359b8511cccSXiaochen Shen else 2360fa7d9493SBabu Moger kfree(sentry); 2361fa7d9493SBabu Moger } 2362fa7d9493SBabu Moger } 2363fa7d9493SBabu Moger 2364fa7d9493SBabu Moger /* 2365fa7d9493SBabu Moger * Forcibly remove all of subdirectories under root. 2366fa7d9493SBabu Moger */ 2367fa7d9493SBabu Moger static void rmdir_all_sub(void) 2368fa7d9493SBabu Moger { 2369fa7d9493SBabu Moger struct rdtgroup *rdtgrp, *tmp; 2370fa7d9493SBabu Moger 2371fa7d9493SBabu Moger /* Move all tasks to the default resource group */ 2372fa7d9493SBabu Moger rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); 2373fa7d9493SBabu Moger 2374fa7d9493SBabu Moger list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { 2375fa7d9493SBabu Moger /* Free any child rmids */ 2376fa7d9493SBabu Moger free_all_child_rdtgrp(rdtgrp); 2377fa7d9493SBabu Moger 2378fa7d9493SBabu Moger /* Remove each rdtgroup other than root */ 2379fa7d9493SBabu Moger if (rdtgrp == &rdtgroup_default) 2380fa7d9493SBabu Moger continue; 2381fa7d9493SBabu Moger 2382fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2383fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 2384fa7d9493SBabu Moger rdtgroup_pseudo_lock_remove(rdtgrp); 2385fa7d9493SBabu Moger 2386fa7d9493SBabu Moger /* 2387fa7d9493SBabu Moger * Give any CPUs back to the default group. We cannot copy 2388fa7d9493SBabu Moger * cpu_online_mask because a CPU might have executed the 2389fa7d9493SBabu Moger * offline callback already, but is still marked online. 2390fa7d9493SBabu Moger */ 2391fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 2392fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 2393fa7d9493SBabu Moger 2394fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2395fa7d9493SBabu Moger 2396fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2397fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 2398b8511cccSXiaochen Shen 2399b8511cccSXiaochen Shen if (atomic_read(&rdtgrp->waitcount) != 0) 2400b8511cccSXiaochen Shen rdtgrp->flags = RDT_DELETED; 2401b8511cccSXiaochen Shen else 2402fa7d9493SBabu Moger kfree(rdtgrp); 2403fa7d9493SBabu Moger } 2404fa7d9493SBabu Moger /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ 2405fa7d9493SBabu Moger update_closid_rmid(cpu_online_mask, &rdtgroup_default); 2406fa7d9493SBabu Moger 2407fa7d9493SBabu Moger kernfs_remove(kn_info); 2408fa7d9493SBabu Moger kernfs_remove(kn_mongrp); 2409fa7d9493SBabu Moger kernfs_remove(kn_mondata); 2410fa7d9493SBabu Moger } 2411fa7d9493SBabu Moger 2412fa7d9493SBabu Moger static void rdt_kill_sb(struct super_block *sb) 2413fa7d9493SBabu Moger { 2414fa7d9493SBabu Moger struct rdt_resource *r; 2415fa7d9493SBabu Moger 2416fa7d9493SBabu Moger cpus_read_lock(); 2417fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2418fa7d9493SBabu Moger 2419fa7d9493SBabu Moger set_mba_sc(false); 2420fa7d9493SBabu Moger 2421fa7d9493SBabu Moger /*Put everything back to default values. */ 2422fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) 2423fa7d9493SBabu Moger reset_all_ctrls(r); 2424fa7d9493SBabu Moger cdp_disable_all(); 2425fa7d9493SBabu Moger rmdir_all_sub(); 2426fa7d9493SBabu Moger rdt_pseudo_lock_release(); 2427fa7d9493SBabu Moger rdtgroup_default.mode = RDT_MODE_SHAREABLE; 2428fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_alloc_enable_key); 2429fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_mon_enable_key); 2430fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_enable_key); 2431fa7d9493SBabu Moger kernfs_kill_sb(sb); 2432fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2433fa7d9493SBabu Moger cpus_read_unlock(); 2434fa7d9493SBabu Moger } 2435fa7d9493SBabu Moger 2436fa7d9493SBabu Moger static struct file_system_type rdt_fs_type = { 2437fa7d9493SBabu Moger .name = "resctrl", 243823bf1b6bSDavid Howells .init_fs_context = rdt_init_fs_context, 2439d7167b14SAl Viro .parameters = rdt_fs_parameters, 2440fa7d9493SBabu Moger .kill_sb = rdt_kill_sb, 2441fa7d9493SBabu Moger }; 2442fa7d9493SBabu Moger 2443fa7d9493SBabu Moger static int mon_addfile(struct kernfs_node *parent_kn, const char *name, 2444fa7d9493SBabu Moger void *priv) 2445fa7d9493SBabu Moger { 2446fa7d9493SBabu Moger struct kernfs_node *kn; 2447fa7d9493SBabu Moger int ret = 0; 2448fa7d9493SBabu Moger 2449fa7d9493SBabu Moger kn = __kernfs_create_file(parent_kn, name, 0444, 2450fa7d9493SBabu Moger GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0, 2451fa7d9493SBabu Moger &kf_mondata_ops, priv, NULL, NULL); 2452fa7d9493SBabu Moger if (IS_ERR(kn)) 2453fa7d9493SBabu Moger return PTR_ERR(kn); 2454fa7d9493SBabu Moger 2455fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2456fa7d9493SBabu Moger if (ret) { 2457fa7d9493SBabu Moger kernfs_remove(kn); 2458fa7d9493SBabu Moger return ret; 2459fa7d9493SBabu Moger } 2460fa7d9493SBabu Moger 2461fa7d9493SBabu Moger return ret; 2462fa7d9493SBabu Moger } 2463fa7d9493SBabu Moger 2464fa7d9493SBabu Moger /* 2465fa7d9493SBabu Moger * Remove all subdirectories of mon_data of ctrl_mon groups 2466fa7d9493SBabu Moger * and monitor groups with given domain id. 2467fa7d9493SBabu Moger */ 2468fa7d9493SBabu Moger void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id) 2469fa7d9493SBabu Moger { 2470fa7d9493SBabu Moger struct rdtgroup *prgrp, *crgrp; 2471fa7d9493SBabu Moger char name[32]; 2472fa7d9493SBabu Moger 2473fa7d9493SBabu Moger if (!r->mon_enabled) 2474fa7d9493SBabu Moger return; 2475fa7d9493SBabu Moger 2476fa7d9493SBabu Moger list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 2477fa7d9493SBabu Moger sprintf(name, "mon_%s_%02d", r->name, dom_id); 2478fa7d9493SBabu Moger kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); 2479fa7d9493SBabu Moger 2480fa7d9493SBabu Moger list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 2481fa7d9493SBabu Moger kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); 2482fa7d9493SBabu Moger } 2483fa7d9493SBabu Moger } 2484fa7d9493SBabu Moger 2485fa7d9493SBabu Moger static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, 2486fa7d9493SBabu Moger struct rdt_domain *d, 2487fa7d9493SBabu Moger struct rdt_resource *r, struct rdtgroup *prgrp) 2488fa7d9493SBabu Moger { 2489fa7d9493SBabu Moger union mon_data_bits priv; 2490fa7d9493SBabu Moger struct kernfs_node *kn; 2491fa7d9493SBabu Moger struct mon_evt *mevt; 2492fa7d9493SBabu Moger struct rmid_read rr; 2493fa7d9493SBabu Moger char name[32]; 2494fa7d9493SBabu Moger int ret; 2495fa7d9493SBabu Moger 2496fa7d9493SBabu Moger sprintf(name, "mon_%s_%02d", r->name, d->id); 2497fa7d9493SBabu Moger /* create the directory */ 2498fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 2499fa7d9493SBabu Moger if (IS_ERR(kn)) 2500fa7d9493SBabu Moger return PTR_ERR(kn); 2501fa7d9493SBabu Moger 2502fa7d9493SBabu Moger /* 2503fa7d9493SBabu Moger * This extra ref will be put in kernfs_remove() and guarantees 2504fa7d9493SBabu Moger * that kn is always accessible. 2505fa7d9493SBabu Moger */ 2506fa7d9493SBabu Moger kernfs_get(kn); 2507fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2508fa7d9493SBabu Moger if (ret) 2509fa7d9493SBabu Moger goto out_destroy; 2510fa7d9493SBabu Moger 2511fa7d9493SBabu Moger if (WARN_ON(list_empty(&r->evt_list))) { 2512fa7d9493SBabu Moger ret = -EPERM; 2513fa7d9493SBabu Moger goto out_destroy; 2514fa7d9493SBabu Moger } 2515fa7d9493SBabu Moger 2516fa7d9493SBabu Moger priv.u.rid = r->rid; 2517fa7d9493SBabu Moger priv.u.domid = d->id; 2518fa7d9493SBabu Moger list_for_each_entry(mevt, &r->evt_list, list) { 2519fa7d9493SBabu Moger priv.u.evtid = mevt->evtid; 2520fa7d9493SBabu Moger ret = mon_addfile(kn, mevt->name, priv.priv); 2521fa7d9493SBabu Moger if (ret) 2522fa7d9493SBabu Moger goto out_destroy; 2523fa7d9493SBabu Moger 2524fa7d9493SBabu Moger if (is_mbm_event(mevt->evtid)) 252546637d45SReinette Chatre mon_event_read(&rr, r, d, prgrp, mevt->evtid, true); 2526fa7d9493SBabu Moger } 2527fa7d9493SBabu Moger kernfs_activate(kn); 2528fa7d9493SBabu Moger return 0; 2529fa7d9493SBabu Moger 2530fa7d9493SBabu Moger out_destroy: 2531fa7d9493SBabu Moger kernfs_remove(kn); 2532fa7d9493SBabu Moger return ret; 2533fa7d9493SBabu Moger } 2534fa7d9493SBabu Moger 2535fa7d9493SBabu Moger /* 2536fa7d9493SBabu Moger * Add all subdirectories of mon_data for "ctrl_mon" groups 2537fa7d9493SBabu Moger * and "monitor" groups with given domain id. 2538fa7d9493SBabu Moger */ 2539fa7d9493SBabu Moger void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 2540fa7d9493SBabu Moger struct rdt_domain *d) 2541fa7d9493SBabu Moger { 2542fa7d9493SBabu Moger struct kernfs_node *parent_kn; 2543fa7d9493SBabu Moger struct rdtgroup *prgrp, *crgrp; 2544fa7d9493SBabu Moger struct list_head *head; 2545fa7d9493SBabu Moger 2546fa7d9493SBabu Moger if (!r->mon_enabled) 2547fa7d9493SBabu Moger return; 2548fa7d9493SBabu Moger 2549fa7d9493SBabu Moger list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 2550fa7d9493SBabu Moger parent_kn = prgrp->mon.mon_data_kn; 2551fa7d9493SBabu Moger mkdir_mondata_subdir(parent_kn, d, r, prgrp); 2552fa7d9493SBabu Moger 2553fa7d9493SBabu Moger head = &prgrp->mon.crdtgrp_list; 2554fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 2555fa7d9493SBabu Moger parent_kn = crgrp->mon.mon_data_kn; 2556fa7d9493SBabu Moger mkdir_mondata_subdir(parent_kn, d, r, crgrp); 2557fa7d9493SBabu Moger } 2558fa7d9493SBabu Moger } 2559fa7d9493SBabu Moger } 2560fa7d9493SBabu Moger 2561fa7d9493SBabu Moger static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, 2562fa7d9493SBabu Moger struct rdt_resource *r, 2563fa7d9493SBabu Moger struct rdtgroup *prgrp) 2564fa7d9493SBabu Moger { 2565fa7d9493SBabu Moger struct rdt_domain *dom; 2566fa7d9493SBabu Moger int ret; 2567fa7d9493SBabu Moger 2568fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) { 2569fa7d9493SBabu Moger ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp); 2570fa7d9493SBabu Moger if (ret) 2571fa7d9493SBabu Moger return ret; 2572fa7d9493SBabu Moger } 2573fa7d9493SBabu Moger 2574fa7d9493SBabu Moger return 0; 2575fa7d9493SBabu Moger } 2576fa7d9493SBabu Moger 2577fa7d9493SBabu Moger /* 2578fa7d9493SBabu Moger * This creates a directory mon_data which contains the monitored data. 2579fa7d9493SBabu Moger * 2580fa7d9493SBabu Moger * mon_data has one directory for each domain whic are named 2581fa7d9493SBabu Moger * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data 2582fa7d9493SBabu Moger * with L3 domain looks as below: 2583fa7d9493SBabu Moger * ./mon_data: 2584fa7d9493SBabu Moger * mon_L3_00 2585fa7d9493SBabu Moger * mon_L3_01 2586fa7d9493SBabu Moger * mon_L3_02 2587fa7d9493SBabu Moger * ... 2588fa7d9493SBabu Moger * 2589fa7d9493SBabu Moger * Each domain directory has one file per event: 2590fa7d9493SBabu Moger * ./mon_L3_00/: 2591fa7d9493SBabu Moger * llc_occupancy 2592fa7d9493SBabu Moger * 2593fa7d9493SBabu Moger */ 2594fa7d9493SBabu Moger static int mkdir_mondata_all(struct kernfs_node *parent_kn, 2595fa7d9493SBabu Moger struct rdtgroup *prgrp, 2596fa7d9493SBabu Moger struct kernfs_node **dest_kn) 2597fa7d9493SBabu Moger { 2598fa7d9493SBabu Moger struct rdt_resource *r; 2599fa7d9493SBabu Moger struct kernfs_node *kn; 2600fa7d9493SBabu Moger int ret; 2601fa7d9493SBabu Moger 2602fa7d9493SBabu Moger /* 2603fa7d9493SBabu Moger * Create the mon_data directory first. 2604fa7d9493SBabu Moger */ 2605334b0f4eSXiaochen Shen ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn); 2606fa7d9493SBabu Moger if (ret) 2607fa7d9493SBabu Moger return ret; 2608fa7d9493SBabu Moger 2609fa7d9493SBabu Moger if (dest_kn) 2610fa7d9493SBabu Moger *dest_kn = kn; 2611fa7d9493SBabu Moger 2612fa7d9493SBabu Moger /* 2613fa7d9493SBabu Moger * Create the subdirectories for each domain. Note that all events 2614fa7d9493SBabu Moger * in a domain like L3 are grouped into a resource whose domain is L3 2615fa7d9493SBabu Moger */ 2616fa7d9493SBabu Moger for_each_mon_enabled_rdt_resource(r) { 2617fa7d9493SBabu Moger ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); 2618fa7d9493SBabu Moger if (ret) 2619fa7d9493SBabu Moger goto out_destroy; 2620fa7d9493SBabu Moger } 2621fa7d9493SBabu Moger 2622fa7d9493SBabu Moger return 0; 2623fa7d9493SBabu Moger 2624fa7d9493SBabu Moger out_destroy: 2625fa7d9493SBabu Moger kernfs_remove(kn); 2626fa7d9493SBabu Moger return ret; 2627fa7d9493SBabu Moger } 2628fa7d9493SBabu Moger 2629fa7d9493SBabu Moger /** 2630fa7d9493SBabu Moger * cbm_ensure_valid - Enforce validity on provided CBM 2631fa7d9493SBabu Moger * @_val: Candidate CBM 2632fa7d9493SBabu Moger * @r: RDT resource to which the CBM belongs 2633fa7d9493SBabu Moger * 2634fa7d9493SBabu Moger * The provided CBM represents all cache portions available for use. This 2635fa7d9493SBabu Moger * may be represented by a bitmap that does not consist of contiguous ones 2636fa7d9493SBabu Moger * and thus be an invalid CBM. 2637fa7d9493SBabu Moger * Here the provided CBM is forced to be a valid CBM by only considering 2638fa7d9493SBabu Moger * the first set of contiguous bits as valid and clearing all bits. 2639fa7d9493SBabu Moger * The intention here is to provide a valid default CBM with which a new 2640fa7d9493SBabu Moger * resource group is initialized. The user can follow this with a 2641fa7d9493SBabu Moger * modification to the CBM if the default does not satisfy the 2642fa7d9493SBabu Moger * requirements. 2643fa7d9493SBabu Moger */ 26442ef085bdSReinette Chatre static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r) 2645fa7d9493SBabu Moger { 2646fa7d9493SBabu Moger unsigned int cbm_len = r->cache.cbm_len; 2647fa7d9493SBabu Moger unsigned long first_bit, zero_bit; 26482ef085bdSReinette Chatre unsigned long val = _val; 2649fa7d9493SBabu Moger 26502ef085bdSReinette Chatre if (!val) 26512ef085bdSReinette Chatre return 0; 2652fa7d9493SBabu Moger 265332f010deSReinette Chatre first_bit = find_first_bit(&val, cbm_len); 265432f010deSReinette Chatre zero_bit = find_next_zero_bit(&val, cbm_len, first_bit); 2655fa7d9493SBabu Moger 2656fa7d9493SBabu Moger /* Clear any remaining bits to ensure contiguous region */ 265732f010deSReinette Chatre bitmap_clear(&val, zero_bit, cbm_len - zero_bit); 26582ef085bdSReinette Chatre return (u32)val; 2659fa7d9493SBabu Moger } 2660fa7d9493SBabu Moger 26617390619aSXiaochen Shen /* 26627390619aSXiaochen Shen * Initialize cache resources per RDT domain 2663fa7d9493SBabu Moger * 26647390619aSXiaochen Shen * Set the RDT domain up to start off with all usable allocations. That is, 26657390619aSXiaochen Shen * all shareable and unused bits. All-zero CBM is invalid. 2666fa7d9493SBabu Moger */ 26677390619aSXiaochen Shen static int __init_one_rdt_domain(struct rdt_domain *d, struct rdt_resource *r, 26687390619aSXiaochen Shen u32 closid) 2669fa7d9493SBabu Moger { 2670fa7d9493SBabu Moger struct rdt_resource *r_cdp = NULL; 2671fa7d9493SBabu Moger struct rdt_domain *d_cdp = NULL; 2672fa7d9493SBabu Moger u32 used_b = 0, unused_b = 0; 2673fa7d9493SBabu Moger unsigned long tmp_cbm; 2674fa7d9493SBabu Moger enum rdtgrp_mode mode; 2675fa7d9493SBabu Moger u32 peer_ctl, *ctrl; 26767390619aSXiaochen Shen int i; 2677fa7d9493SBabu Moger 2678fa7d9493SBabu Moger rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp); 2679fa7d9493SBabu Moger d->have_new_ctrl = false; 2680fa7d9493SBabu Moger d->new_ctrl = r->cache.shareable_bits; 2681fa7d9493SBabu Moger used_b = r->cache.shareable_bits; 2682fa7d9493SBabu Moger ctrl = d->ctrl_val; 2683fa7d9493SBabu Moger for (i = 0; i < closids_supported(); i++, ctrl++) { 2684fa7d9493SBabu Moger if (closid_allocated(i) && i != closid) { 2685fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 2686fa7d9493SBabu Moger if (mode == RDT_MODE_PSEUDO_LOCKSETUP) 268787d3aa28SJames Morse /* 268887d3aa28SJames Morse * ctrl values for locksetup aren't relevant 268987d3aa28SJames Morse * until the schemata is written, and the mode 269087d3aa28SJames Morse * becomes RDT_MODE_PSEUDO_LOCKED. 269187d3aa28SJames Morse */ 269287d3aa28SJames Morse continue; 2693fa7d9493SBabu Moger /* 26947390619aSXiaochen Shen * If CDP is active include peer domain's 26957390619aSXiaochen Shen * usage to ensure there is no overlap 26967390619aSXiaochen Shen * with an exclusive group. 2697fa7d9493SBabu Moger */ 2698fa7d9493SBabu Moger if (d_cdp) 2699fa7d9493SBabu Moger peer_ctl = d_cdp->ctrl_val[i]; 2700fa7d9493SBabu Moger else 2701fa7d9493SBabu Moger peer_ctl = 0; 2702fa7d9493SBabu Moger used_b |= *ctrl | peer_ctl; 2703fa7d9493SBabu Moger if (mode == RDT_MODE_SHAREABLE) 2704fa7d9493SBabu Moger d->new_ctrl |= *ctrl | peer_ctl; 2705fa7d9493SBabu Moger } 2706fa7d9493SBabu Moger } 2707fa7d9493SBabu Moger if (d->plr && d->plr->cbm > 0) 2708fa7d9493SBabu Moger used_b |= d->plr->cbm; 2709fa7d9493SBabu Moger unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1); 2710fa7d9493SBabu Moger unused_b &= BIT_MASK(r->cache.cbm_len) - 1; 2711fa7d9493SBabu Moger d->new_ctrl |= unused_b; 2712fa7d9493SBabu Moger /* 2713fa7d9493SBabu Moger * Force the initial CBM to be valid, user can 2714fa7d9493SBabu Moger * modify the CBM based on system availability. 2715fa7d9493SBabu Moger */ 27162ef085bdSReinette Chatre d->new_ctrl = cbm_ensure_valid(d->new_ctrl, r); 2717fa7d9493SBabu Moger /* 27187390619aSXiaochen Shen * Assign the u32 CBM to an unsigned long to ensure that 27197390619aSXiaochen Shen * bitmap_weight() does not access out-of-bound memory. 2720fa7d9493SBabu Moger */ 2721fa7d9493SBabu Moger tmp_cbm = d->new_ctrl; 27227390619aSXiaochen Shen if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) { 27237390619aSXiaochen Shen rdt_last_cmd_printf("No space on %s:%d\n", r->name, d->id); 2724fa7d9493SBabu Moger return -ENOSPC; 2725fa7d9493SBabu Moger } 2726fa7d9493SBabu Moger d->have_new_ctrl = true; 27277390619aSXiaochen Shen 27287390619aSXiaochen Shen return 0; 27297390619aSXiaochen Shen } 27307390619aSXiaochen Shen 273147820e73SXiaochen Shen /* 273247820e73SXiaochen Shen * Initialize cache resources with default values. 27337390619aSXiaochen Shen * 27347390619aSXiaochen Shen * A new RDT group is being created on an allocation capable (CAT) 27357390619aSXiaochen Shen * supporting system. Set this group up to start off with all usable 27367390619aSXiaochen Shen * allocations. 27377390619aSXiaochen Shen * 27387390619aSXiaochen Shen * If there are no more shareable bits available on any domain then 27397390619aSXiaochen Shen * the entire allocation will fail. 27407390619aSXiaochen Shen */ 274147820e73SXiaochen Shen static int rdtgroup_init_cat(struct rdt_resource *r, u32 closid) 27427390619aSXiaochen Shen { 27437390619aSXiaochen Shen struct rdt_domain *d; 27447390619aSXiaochen Shen int ret; 27457390619aSXiaochen Shen 27467390619aSXiaochen Shen list_for_each_entry(d, &r->domains, list) { 274747820e73SXiaochen Shen ret = __init_one_rdt_domain(d, r, closid); 27487390619aSXiaochen Shen if (ret < 0) 27497390619aSXiaochen Shen return ret; 2750fa7d9493SBabu Moger } 275147820e73SXiaochen Shen 275247820e73SXiaochen Shen return 0; 2753fa7d9493SBabu Moger } 2754fa7d9493SBabu Moger 275547820e73SXiaochen Shen /* Initialize MBA resource with default values. */ 275647820e73SXiaochen Shen static void rdtgroup_init_mba(struct rdt_resource *r) 275747820e73SXiaochen Shen { 275847820e73SXiaochen Shen struct rdt_domain *d; 275947820e73SXiaochen Shen 276047820e73SXiaochen Shen list_for_each_entry(d, &r->domains, list) { 276147820e73SXiaochen Shen d->new_ctrl = is_mba_sc(r) ? MBA_MAX_MBPS : r->default_ctrl; 276247820e73SXiaochen Shen d->have_new_ctrl = true; 276347820e73SXiaochen Shen } 276447820e73SXiaochen Shen } 276547820e73SXiaochen Shen 276647820e73SXiaochen Shen /* Initialize the RDT group's allocations. */ 276747820e73SXiaochen Shen static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp) 276847820e73SXiaochen Shen { 276947820e73SXiaochen Shen struct rdt_resource *r; 277047820e73SXiaochen Shen int ret; 277147820e73SXiaochen Shen 2772fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 277347820e73SXiaochen Shen if (r->rid == RDT_RESOURCE_MBA) { 277447820e73SXiaochen Shen rdtgroup_init_mba(r); 277547820e73SXiaochen Shen } else { 277647820e73SXiaochen Shen ret = rdtgroup_init_cat(r, rdtgrp->closid); 277747820e73SXiaochen Shen if (ret < 0) 277847820e73SXiaochen Shen return ret; 277947820e73SXiaochen Shen } 278047820e73SXiaochen Shen 2781fa7d9493SBabu Moger ret = update_domains(r, rdtgrp->closid); 2782fa7d9493SBabu Moger if (ret < 0) { 2783723f1a0dSBabu Moger rdt_last_cmd_puts("Failed to initialize allocations\n"); 2784fa7d9493SBabu Moger return ret; 2785fa7d9493SBabu Moger } 278647820e73SXiaochen Shen 2787fa7d9493SBabu Moger } 2788fa7d9493SBabu Moger 278940fba00fSXiaochen Shen rdtgrp->mode = RDT_MODE_SHAREABLE; 279040fba00fSXiaochen Shen 2791fa7d9493SBabu Moger return 0; 2792fa7d9493SBabu Moger } 2793fa7d9493SBabu Moger 2794fa7d9493SBabu Moger static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, 2795fa7d9493SBabu Moger const char *name, umode_t mode, 2796fa7d9493SBabu Moger enum rdt_group_type rtype, struct rdtgroup **r) 2797fa7d9493SBabu Moger { 2798fa7d9493SBabu Moger struct rdtgroup *prdtgrp, *rdtgrp; 2799fa7d9493SBabu Moger struct kernfs_node *kn; 2800fa7d9493SBabu Moger uint files = 0; 2801fa7d9493SBabu Moger int ret; 2802fa7d9493SBabu Moger 2803334b0f4eSXiaochen Shen prdtgrp = rdtgroup_kn_lock_live(parent_kn); 2804fa7d9493SBabu Moger if (!prdtgrp) { 2805fa7d9493SBabu Moger ret = -ENODEV; 2806fa7d9493SBabu Moger goto out_unlock; 2807fa7d9493SBabu Moger } 2808fa7d9493SBabu Moger 2809fa7d9493SBabu Moger if (rtype == RDTMON_GROUP && 2810fa7d9493SBabu Moger (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2811fa7d9493SBabu Moger prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) { 2812fa7d9493SBabu Moger ret = -EINVAL; 2813723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 2814fa7d9493SBabu Moger goto out_unlock; 2815fa7d9493SBabu Moger } 2816fa7d9493SBabu Moger 2817fa7d9493SBabu Moger /* allocate the rdtgroup. */ 2818fa7d9493SBabu Moger rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); 2819fa7d9493SBabu Moger if (!rdtgrp) { 2820fa7d9493SBabu Moger ret = -ENOSPC; 2821723f1a0dSBabu Moger rdt_last_cmd_puts("Kernel out of memory\n"); 2822fa7d9493SBabu Moger goto out_unlock; 2823fa7d9493SBabu Moger } 2824fa7d9493SBabu Moger *r = rdtgrp; 2825fa7d9493SBabu Moger rdtgrp->mon.parent = prdtgrp; 2826fa7d9493SBabu Moger rdtgrp->type = rtype; 2827fa7d9493SBabu Moger INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); 2828fa7d9493SBabu Moger 2829fa7d9493SBabu Moger /* kernfs creates the directory for rdtgrp */ 2830fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); 2831fa7d9493SBabu Moger if (IS_ERR(kn)) { 2832fa7d9493SBabu Moger ret = PTR_ERR(kn); 2833fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs create error\n"); 2834fa7d9493SBabu Moger goto out_free_rgrp; 2835fa7d9493SBabu Moger } 2836fa7d9493SBabu Moger rdtgrp->kn = kn; 2837fa7d9493SBabu Moger 2838fa7d9493SBabu Moger /* 2839fa7d9493SBabu Moger * kernfs_remove() will drop the reference count on "kn" which 2840fa7d9493SBabu Moger * will free it. But we still need it to stick around for the 2841fa7d9493SBabu Moger * rdtgroup_kn_unlock(kn} call below. Take one extra reference 2842fa7d9493SBabu Moger * here, which will be dropped inside rdtgroup_kn_unlock(). 2843fa7d9493SBabu Moger */ 2844fa7d9493SBabu Moger kernfs_get(kn); 2845fa7d9493SBabu Moger 2846fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2847fa7d9493SBabu Moger if (ret) { 2848fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs perm error\n"); 2849fa7d9493SBabu Moger goto out_destroy; 2850fa7d9493SBabu Moger } 2851fa7d9493SBabu Moger 2852fa7d9493SBabu Moger files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype); 2853fa7d9493SBabu Moger ret = rdtgroup_add_files(kn, files); 2854fa7d9493SBabu Moger if (ret) { 2855fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs fill error\n"); 2856fa7d9493SBabu Moger goto out_destroy; 2857fa7d9493SBabu Moger } 2858fa7d9493SBabu Moger 2859fa7d9493SBabu Moger if (rdt_mon_capable) { 2860fa7d9493SBabu Moger ret = alloc_rmid(); 2861fa7d9493SBabu Moger if (ret < 0) { 2862723f1a0dSBabu Moger rdt_last_cmd_puts("Out of RMIDs\n"); 2863fa7d9493SBabu Moger goto out_destroy; 2864fa7d9493SBabu Moger } 2865fa7d9493SBabu Moger rdtgrp->mon.rmid = ret; 2866fa7d9493SBabu Moger 2867fa7d9493SBabu Moger ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn); 2868fa7d9493SBabu Moger if (ret) { 2869fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs subdir error\n"); 2870fa7d9493SBabu Moger goto out_idfree; 2871fa7d9493SBabu Moger } 2872fa7d9493SBabu Moger } 2873fa7d9493SBabu Moger kernfs_activate(kn); 2874fa7d9493SBabu Moger 2875fa7d9493SBabu Moger /* 2876334b0f4eSXiaochen Shen * The caller unlocks the parent_kn upon success. 2877fa7d9493SBabu Moger */ 2878fa7d9493SBabu Moger return 0; 2879fa7d9493SBabu Moger 2880fa7d9493SBabu Moger out_idfree: 2881fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2882fa7d9493SBabu Moger out_destroy: 2883fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2884fa7d9493SBabu Moger out_free_rgrp: 2885fa7d9493SBabu Moger kfree(rdtgrp); 2886fa7d9493SBabu Moger out_unlock: 2887334b0f4eSXiaochen Shen rdtgroup_kn_unlock(parent_kn); 2888fa7d9493SBabu Moger return ret; 2889fa7d9493SBabu Moger } 2890fa7d9493SBabu Moger 2891fa7d9493SBabu Moger static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) 2892fa7d9493SBabu Moger { 2893fa7d9493SBabu Moger kernfs_remove(rgrp->kn); 2894fa7d9493SBabu Moger free_rmid(rgrp->mon.rmid); 2895fa7d9493SBabu Moger kfree(rgrp); 2896fa7d9493SBabu Moger } 2897fa7d9493SBabu Moger 2898fa7d9493SBabu Moger /* 2899fa7d9493SBabu Moger * Create a monitor group under "mon_groups" directory of a control 2900fa7d9493SBabu Moger * and monitor group(ctrl_mon). This is a resource group 2901fa7d9493SBabu Moger * to monitor a subset of tasks and cpus in its parent ctrl_mon group. 2902fa7d9493SBabu Moger */ 2903fa7d9493SBabu Moger static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, 290432ada3b9SXiaochen Shen const char *name, umode_t mode) 2905fa7d9493SBabu Moger { 2906fa7d9493SBabu Moger struct rdtgroup *rdtgrp, *prgrp; 2907fa7d9493SBabu Moger int ret; 2908fa7d9493SBabu Moger 290932ada3b9SXiaochen Shen ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp); 2910fa7d9493SBabu Moger if (ret) 2911fa7d9493SBabu Moger return ret; 2912fa7d9493SBabu Moger 2913fa7d9493SBabu Moger prgrp = rdtgrp->mon.parent; 2914fa7d9493SBabu Moger rdtgrp->closid = prgrp->closid; 2915fa7d9493SBabu Moger 2916fa7d9493SBabu Moger /* 2917fa7d9493SBabu Moger * Add the rdtgrp to the list of rdtgrps the parent 2918fa7d9493SBabu Moger * ctrl_mon group has to track. 2919fa7d9493SBabu Moger */ 2920fa7d9493SBabu Moger list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); 2921fa7d9493SBabu Moger 2922334b0f4eSXiaochen Shen rdtgroup_kn_unlock(parent_kn); 2923fa7d9493SBabu Moger return ret; 2924fa7d9493SBabu Moger } 2925fa7d9493SBabu Moger 2926fa7d9493SBabu Moger /* 2927fa7d9493SBabu Moger * These are rdtgroups created under the root directory. Can be used 2928fa7d9493SBabu Moger * to allocate and monitor resources. 2929fa7d9493SBabu Moger */ 2930fa7d9493SBabu Moger static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, 2931fa7d9493SBabu Moger const char *name, umode_t mode) 2932fa7d9493SBabu Moger { 2933fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 2934fa7d9493SBabu Moger struct kernfs_node *kn; 2935fa7d9493SBabu Moger u32 closid; 2936fa7d9493SBabu Moger int ret; 2937fa7d9493SBabu Moger 293832ada3b9SXiaochen Shen ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp); 2939fa7d9493SBabu Moger if (ret) 2940fa7d9493SBabu Moger return ret; 2941fa7d9493SBabu Moger 2942fa7d9493SBabu Moger kn = rdtgrp->kn; 2943fa7d9493SBabu Moger ret = closid_alloc(); 2944fa7d9493SBabu Moger if (ret < 0) { 2945723f1a0dSBabu Moger rdt_last_cmd_puts("Out of CLOSIDs\n"); 2946fa7d9493SBabu Moger goto out_common_fail; 2947fa7d9493SBabu Moger } 2948fa7d9493SBabu Moger closid = ret; 2949fa7d9493SBabu Moger ret = 0; 2950fa7d9493SBabu Moger 2951fa7d9493SBabu Moger rdtgrp->closid = closid; 2952fa7d9493SBabu Moger ret = rdtgroup_init_alloc(rdtgrp); 2953fa7d9493SBabu Moger if (ret < 0) 2954fa7d9493SBabu Moger goto out_id_free; 2955fa7d9493SBabu Moger 2956fa7d9493SBabu Moger list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); 2957fa7d9493SBabu Moger 2958fa7d9493SBabu Moger if (rdt_mon_capable) { 2959fa7d9493SBabu Moger /* 2960fa7d9493SBabu Moger * Create an empty mon_groups directory to hold the subset 2961fa7d9493SBabu Moger * of tasks and cpus to monitor. 2962fa7d9493SBabu Moger */ 2963334b0f4eSXiaochen Shen ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL); 2964fa7d9493SBabu Moger if (ret) { 2965fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs subdir error\n"); 2966fa7d9493SBabu Moger goto out_del_list; 2967fa7d9493SBabu Moger } 2968fa7d9493SBabu Moger } 2969fa7d9493SBabu Moger 2970fa7d9493SBabu Moger goto out_unlock; 2971fa7d9493SBabu Moger 2972fa7d9493SBabu Moger out_del_list: 2973fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 2974fa7d9493SBabu Moger out_id_free: 2975fa7d9493SBabu Moger closid_free(closid); 2976fa7d9493SBabu Moger out_common_fail: 2977fa7d9493SBabu Moger mkdir_rdt_prepare_clean(rdtgrp); 2978fa7d9493SBabu Moger out_unlock: 2979334b0f4eSXiaochen Shen rdtgroup_kn_unlock(parent_kn); 2980fa7d9493SBabu Moger return ret; 2981fa7d9493SBabu Moger } 2982fa7d9493SBabu Moger 2983fa7d9493SBabu Moger /* 2984fa7d9493SBabu Moger * We allow creating mon groups only with in a directory called "mon_groups" 2985fa7d9493SBabu Moger * which is present in every ctrl_mon group. Check if this is a valid 2986fa7d9493SBabu Moger * "mon_groups" directory. 2987fa7d9493SBabu Moger * 2988fa7d9493SBabu Moger * 1. The directory should be named "mon_groups". 2989fa7d9493SBabu Moger * 2. The mon group itself should "not" be named "mon_groups". 2990fa7d9493SBabu Moger * This makes sure "mon_groups" directory always has a ctrl_mon group 2991fa7d9493SBabu Moger * as parent. 2992fa7d9493SBabu Moger */ 2993fa7d9493SBabu Moger static bool is_mon_groups(struct kernfs_node *kn, const char *name) 2994fa7d9493SBabu Moger { 2995fa7d9493SBabu Moger return (!strcmp(kn->name, "mon_groups") && 2996fa7d9493SBabu Moger strcmp(name, "mon_groups")); 2997fa7d9493SBabu Moger } 2998fa7d9493SBabu Moger 2999fa7d9493SBabu Moger static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, 3000fa7d9493SBabu Moger umode_t mode) 3001fa7d9493SBabu Moger { 3002fa7d9493SBabu Moger /* Do not accept '\n' to avoid unparsable situation. */ 3003fa7d9493SBabu Moger if (strchr(name, '\n')) 3004fa7d9493SBabu Moger return -EINVAL; 3005fa7d9493SBabu Moger 3006fa7d9493SBabu Moger /* 3007fa7d9493SBabu Moger * If the parent directory is the root directory and RDT 3008fa7d9493SBabu Moger * allocation is supported, add a control and monitoring 3009fa7d9493SBabu Moger * subdirectory 3010fa7d9493SBabu Moger */ 3011fa7d9493SBabu Moger if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn) 301232ada3b9SXiaochen Shen return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode); 3013fa7d9493SBabu Moger 3014fa7d9493SBabu Moger /* 3015fa7d9493SBabu Moger * If RDT monitoring is supported and the parent directory is a valid 3016fa7d9493SBabu Moger * "mon_groups" directory, add a monitoring subdirectory. 3017fa7d9493SBabu Moger */ 3018fa7d9493SBabu Moger if (rdt_mon_capable && is_mon_groups(parent_kn, name)) 301932ada3b9SXiaochen Shen return rdtgroup_mkdir_mon(parent_kn, name, mode); 3020fa7d9493SBabu Moger 3021fa7d9493SBabu Moger return -EPERM; 3022fa7d9493SBabu Moger } 3023fa7d9493SBabu Moger 3024fa7d9493SBabu Moger static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp, 3025fa7d9493SBabu Moger cpumask_var_t tmpmask) 3026fa7d9493SBabu Moger { 3027fa7d9493SBabu Moger struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 3028fa7d9493SBabu Moger int cpu; 3029fa7d9493SBabu Moger 3030fa7d9493SBabu Moger /* Give any tasks back to the parent group */ 3031fa7d9493SBabu Moger rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); 3032fa7d9493SBabu Moger 3033fa7d9493SBabu Moger /* Update per cpu rmid of the moved CPUs first */ 3034fa7d9493SBabu Moger for_each_cpu(cpu, &rdtgrp->cpu_mask) 3035fa7d9493SBabu Moger per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid; 3036fa7d9493SBabu Moger /* 3037fa7d9493SBabu Moger * Update the MSR on moved CPUs and CPUs which have moved 3038fa7d9493SBabu Moger * task running on them. 3039fa7d9493SBabu Moger */ 3040fa7d9493SBabu Moger cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 3041fa7d9493SBabu Moger update_closid_rmid(tmpmask, NULL); 3042fa7d9493SBabu Moger 3043fa7d9493SBabu Moger rdtgrp->flags = RDT_DELETED; 3044fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 3045fa7d9493SBabu Moger 3046fa7d9493SBabu Moger /* 3047fa7d9493SBabu Moger * Remove the rdtgrp from the parent ctrl_mon group's list 3048fa7d9493SBabu Moger */ 3049fa7d9493SBabu Moger WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 3050fa7d9493SBabu Moger list_del(&rdtgrp->mon.crdtgrp_list); 3051fa7d9493SBabu Moger 3052fa7d9493SBabu Moger /* 3053fa7d9493SBabu Moger * one extra hold on this, will drop when we kfree(rdtgrp) 3054fa7d9493SBabu Moger * in rdtgroup_kn_unlock() 3055fa7d9493SBabu Moger */ 3056fa7d9493SBabu Moger kernfs_get(kn); 3057fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 3058fa7d9493SBabu Moger 3059fa7d9493SBabu Moger return 0; 3060fa7d9493SBabu Moger } 3061fa7d9493SBabu Moger 3062fa7d9493SBabu Moger static int rdtgroup_ctrl_remove(struct kernfs_node *kn, 3063fa7d9493SBabu Moger struct rdtgroup *rdtgrp) 3064fa7d9493SBabu Moger { 3065fa7d9493SBabu Moger rdtgrp->flags = RDT_DELETED; 3066fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 3067fa7d9493SBabu Moger 3068fa7d9493SBabu Moger /* 3069fa7d9493SBabu Moger * one extra hold on this, will drop when we kfree(rdtgrp) 3070fa7d9493SBabu Moger * in rdtgroup_kn_unlock() 3071fa7d9493SBabu Moger */ 3072fa7d9493SBabu Moger kernfs_get(kn); 3073fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 3074fa7d9493SBabu Moger return 0; 3075fa7d9493SBabu Moger } 3076fa7d9493SBabu Moger 3077fa7d9493SBabu Moger static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp, 3078fa7d9493SBabu Moger cpumask_var_t tmpmask) 3079fa7d9493SBabu Moger { 3080fa7d9493SBabu Moger int cpu; 3081fa7d9493SBabu Moger 3082fa7d9493SBabu Moger /* Give any tasks back to the default group */ 3083fa7d9493SBabu Moger rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); 3084fa7d9493SBabu Moger 3085fa7d9493SBabu Moger /* Give any CPUs back to the default group */ 3086fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 3087fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 3088fa7d9493SBabu Moger 3089fa7d9493SBabu Moger /* Update per cpu closid and rmid of the moved CPUs first */ 3090fa7d9493SBabu Moger for_each_cpu(cpu, &rdtgrp->cpu_mask) { 3091fa7d9493SBabu Moger per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid; 3092fa7d9493SBabu Moger per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid; 3093fa7d9493SBabu Moger } 3094fa7d9493SBabu Moger 3095fa7d9493SBabu Moger /* 3096fa7d9493SBabu Moger * Update the MSR on moved CPUs and CPUs which have moved 3097fa7d9493SBabu Moger * task running on them. 3098fa7d9493SBabu Moger */ 3099fa7d9493SBabu Moger cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 3100fa7d9493SBabu Moger update_closid_rmid(tmpmask, NULL); 3101fa7d9493SBabu Moger 3102fa7d9493SBabu Moger closid_free(rdtgrp->closid); 3103fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 3104fa7d9493SBabu Moger 3105074fadeeSXiaochen Shen rdtgroup_ctrl_remove(kn, rdtgrp); 3106074fadeeSXiaochen Shen 3107fa7d9493SBabu Moger /* 3108fa7d9493SBabu Moger * Free all the child monitor group rmids. 3109fa7d9493SBabu Moger */ 3110fa7d9493SBabu Moger free_all_child_rdtgrp(rdtgrp); 3111fa7d9493SBabu Moger 3112fa7d9493SBabu Moger return 0; 3113fa7d9493SBabu Moger } 3114fa7d9493SBabu Moger 3115fa7d9493SBabu Moger static int rdtgroup_rmdir(struct kernfs_node *kn) 3116fa7d9493SBabu Moger { 3117fa7d9493SBabu Moger struct kernfs_node *parent_kn = kn->parent; 3118fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 3119fa7d9493SBabu Moger cpumask_var_t tmpmask; 3120fa7d9493SBabu Moger int ret = 0; 3121fa7d9493SBabu Moger 3122fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 3123fa7d9493SBabu Moger return -ENOMEM; 3124fa7d9493SBabu Moger 3125fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(kn); 3126fa7d9493SBabu Moger if (!rdtgrp) { 3127fa7d9493SBabu Moger ret = -EPERM; 3128fa7d9493SBabu Moger goto out; 3129fa7d9493SBabu Moger } 3130fa7d9493SBabu Moger 3131fa7d9493SBabu Moger /* 3132fa7d9493SBabu Moger * If the rdtgroup is a ctrl_mon group and parent directory 3133fa7d9493SBabu Moger * is the root directory, remove the ctrl_mon group. 3134fa7d9493SBabu Moger * 3135fa7d9493SBabu Moger * If the rdtgroup is a mon group and parent directory 3136fa7d9493SBabu Moger * is a valid "mon_groups" directory, remove the mon group. 3137fa7d9493SBabu Moger */ 3138b0151da5SReinette Chatre if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn && 3139b0151da5SReinette Chatre rdtgrp != &rdtgroup_default) { 3140fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 3141fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 3142fa7d9493SBabu Moger ret = rdtgroup_ctrl_remove(kn, rdtgrp); 3143fa7d9493SBabu Moger } else { 3144fa7d9493SBabu Moger ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask); 3145fa7d9493SBabu Moger } 3146fa7d9493SBabu Moger } else if (rdtgrp->type == RDTMON_GROUP && 3147fa7d9493SBabu Moger is_mon_groups(parent_kn, kn->name)) { 3148fa7d9493SBabu Moger ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask); 3149fa7d9493SBabu Moger } else { 3150fa7d9493SBabu Moger ret = -EPERM; 3151fa7d9493SBabu Moger } 3152fa7d9493SBabu Moger 3153fa7d9493SBabu Moger out: 3154fa7d9493SBabu Moger rdtgroup_kn_unlock(kn); 3155fa7d9493SBabu Moger free_cpumask_var(tmpmask); 3156fa7d9493SBabu Moger return ret; 3157fa7d9493SBabu Moger } 3158fa7d9493SBabu Moger 3159fa7d9493SBabu Moger static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) 3160fa7d9493SBabu Moger { 3161fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) 3162fa7d9493SBabu Moger seq_puts(seq, ",cdp"); 3163fa7d9493SBabu Moger 3164fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) 3165fa7d9493SBabu Moger seq_puts(seq, ",cdpl2"); 3166fa7d9493SBabu Moger 3167fa7d9493SBabu Moger if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA])) 3168fa7d9493SBabu Moger seq_puts(seq, ",mba_MBps"); 3169fa7d9493SBabu Moger 3170fa7d9493SBabu Moger return 0; 3171fa7d9493SBabu Moger } 3172fa7d9493SBabu Moger 3173fa7d9493SBabu Moger static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { 3174fa7d9493SBabu Moger .mkdir = rdtgroup_mkdir, 3175fa7d9493SBabu Moger .rmdir = rdtgroup_rmdir, 3176fa7d9493SBabu Moger .show_options = rdtgroup_show_options, 3177fa7d9493SBabu Moger }; 3178fa7d9493SBabu Moger 3179fa7d9493SBabu Moger static int __init rdtgroup_setup_root(void) 3180fa7d9493SBabu Moger { 3181fa7d9493SBabu Moger int ret; 3182fa7d9493SBabu Moger 3183fa7d9493SBabu Moger rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, 3184fa7d9493SBabu Moger KERNFS_ROOT_CREATE_DEACTIVATED | 3185fa7d9493SBabu Moger KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, 3186fa7d9493SBabu Moger &rdtgroup_default); 3187fa7d9493SBabu Moger if (IS_ERR(rdt_root)) 3188fa7d9493SBabu Moger return PTR_ERR(rdt_root); 3189fa7d9493SBabu Moger 3190fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 3191fa7d9493SBabu Moger 3192fa7d9493SBabu Moger rdtgroup_default.closid = 0; 3193fa7d9493SBabu Moger rdtgroup_default.mon.rmid = 0; 3194fa7d9493SBabu Moger rdtgroup_default.type = RDTCTRL_GROUP; 3195fa7d9493SBabu Moger INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); 3196fa7d9493SBabu Moger 3197fa7d9493SBabu Moger list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); 3198fa7d9493SBabu Moger 3199fa7d9493SBabu Moger ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE); 3200fa7d9493SBabu Moger if (ret) { 3201fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3202fa7d9493SBabu Moger goto out; 3203fa7d9493SBabu Moger } 3204fa7d9493SBabu Moger 3205fa7d9493SBabu Moger rdtgroup_default.kn = rdt_root->kn; 3206fa7d9493SBabu Moger kernfs_activate(rdtgroup_default.kn); 3207fa7d9493SBabu Moger 3208fa7d9493SBabu Moger out: 3209fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 3210fa7d9493SBabu Moger 3211fa7d9493SBabu Moger return ret; 3212fa7d9493SBabu Moger } 3213fa7d9493SBabu Moger 3214fa7d9493SBabu Moger /* 3215fa7d9493SBabu Moger * rdtgroup_init - rdtgroup initialization 3216fa7d9493SBabu Moger * 3217fa7d9493SBabu Moger * Setup resctrl file system including set up root, create mount point, 3218fa7d9493SBabu Moger * register rdtgroup filesystem, and initialize files under root directory. 3219fa7d9493SBabu Moger * 3220fa7d9493SBabu Moger * Return: 0 on success or -errno 3221fa7d9493SBabu Moger */ 3222fa7d9493SBabu Moger int __init rdtgroup_init(void) 3223fa7d9493SBabu Moger { 3224fa7d9493SBabu Moger int ret = 0; 3225fa7d9493SBabu Moger 3226fa7d9493SBabu Moger seq_buf_init(&last_cmd_status, last_cmd_status_buf, 3227fa7d9493SBabu Moger sizeof(last_cmd_status_buf)); 3228fa7d9493SBabu Moger 3229fa7d9493SBabu Moger ret = rdtgroup_setup_root(); 3230fa7d9493SBabu Moger if (ret) 3231fa7d9493SBabu Moger return ret; 3232fa7d9493SBabu Moger 3233fa7d9493SBabu Moger ret = sysfs_create_mount_point(fs_kobj, "resctrl"); 3234fa7d9493SBabu Moger if (ret) 3235fa7d9493SBabu Moger goto cleanup_root; 3236fa7d9493SBabu Moger 3237fa7d9493SBabu Moger ret = register_filesystem(&rdt_fs_type); 3238fa7d9493SBabu Moger if (ret) 3239fa7d9493SBabu Moger goto cleanup_mountpoint; 3240fa7d9493SBabu Moger 3241fa7d9493SBabu Moger /* 3242fa7d9493SBabu Moger * Adding the resctrl debugfs directory here may not be ideal since 3243fa7d9493SBabu Moger * it would let the resctrl debugfs directory appear on the debugfs 3244fa7d9493SBabu Moger * filesystem before the resctrl filesystem is mounted. 3245fa7d9493SBabu Moger * It may also be ok since that would enable debugging of RDT before 3246fa7d9493SBabu Moger * resctrl is mounted. 3247fa7d9493SBabu Moger * The reason why the debugfs directory is created here and not in 3248ae0fbeddSJames Morse * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and 3249fa7d9493SBabu Moger * during the debugfs directory creation also &sb->s_type->i_mutex_key 3250fa7d9493SBabu Moger * (the lockdep class of inode->i_rwsem). Other filesystem 3251fa7d9493SBabu Moger * interactions (eg. SyS_getdents) have the lock ordering: 3252c1e8d7c6SMichel Lespinasse * &sb->s_type->i_mutex_key --> &mm->mmap_lock 3253c1e8d7c6SMichel Lespinasse * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex 3254fa7d9493SBabu Moger * is taken, thus creating dependency: 3255c1e8d7c6SMichel Lespinasse * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause 3256fa7d9493SBabu Moger * issues considering the other two lock dependencies. 3257fa7d9493SBabu Moger * By creating the debugfs directory here we avoid a dependency 3258fa7d9493SBabu Moger * that may cause deadlock (even though file operations cannot 3259fa7d9493SBabu Moger * occur until the filesystem is mounted, but I do not know how to 3260fa7d9493SBabu Moger * tell lockdep that). 3261fa7d9493SBabu Moger */ 3262fa7d9493SBabu Moger debugfs_resctrl = debugfs_create_dir("resctrl", NULL); 3263fa7d9493SBabu Moger 3264fa7d9493SBabu Moger return 0; 3265fa7d9493SBabu Moger 3266fa7d9493SBabu Moger cleanup_mountpoint: 3267fa7d9493SBabu Moger sysfs_remove_mount_point(fs_kobj, "resctrl"); 3268fa7d9493SBabu Moger cleanup_root: 3269fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3270fa7d9493SBabu Moger 3271fa7d9493SBabu Moger return ret; 3272fa7d9493SBabu Moger } 3273fa7d9493SBabu Moger 3274fa7d9493SBabu Moger void __exit rdtgroup_exit(void) 3275fa7d9493SBabu Moger { 3276fa7d9493SBabu Moger debugfs_remove_recursive(debugfs_resctrl); 3277fa7d9493SBabu Moger unregister_filesystem(&rdt_fs_type); 3278fa7d9493SBabu Moger sysfs_remove_mount_point(fs_kobj, "resctrl"); 3279fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3280fa7d9493SBabu Moger } 3281