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 32fa7d9493SBabu Moger #include <asm/resctrl_sched.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 rdt_last_cmd_clear(); 465fa7d9493SBabu Moger if (!rdtgrp) { 466fa7d9493SBabu Moger ret = -ENOENT; 467723f1a0dSBabu Moger rdt_last_cmd_puts("Directory was removed\n"); 468fa7d9493SBabu Moger goto unlock; 469fa7d9493SBabu Moger } 470fa7d9493SBabu Moger 471fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 472fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 473fa7d9493SBabu Moger ret = -EINVAL; 474723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 475fa7d9493SBabu Moger goto unlock; 476fa7d9493SBabu Moger } 477fa7d9493SBabu Moger 478fa7d9493SBabu Moger if (is_cpu_list(of)) 479fa7d9493SBabu Moger ret = cpulist_parse(buf, newmask); 480fa7d9493SBabu Moger else 481fa7d9493SBabu Moger ret = cpumask_parse(buf, newmask); 482fa7d9493SBabu Moger 483fa7d9493SBabu Moger if (ret) { 484723f1a0dSBabu Moger rdt_last_cmd_puts("Bad CPU list/mask\n"); 485fa7d9493SBabu Moger goto unlock; 486fa7d9493SBabu Moger } 487fa7d9493SBabu Moger 488fa7d9493SBabu Moger /* check that user didn't specify any offline cpus */ 489fa7d9493SBabu Moger cpumask_andnot(tmpmask, newmask, cpu_online_mask); 490fa7d9493SBabu Moger if (cpumask_weight(tmpmask)) { 491fa7d9493SBabu Moger ret = -EINVAL; 492723f1a0dSBabu Moger rdt_last_cmd_puts("Can only assign online CPUs\n"); 493fa7d9493SBabu Moger goto unlock; 494fa7d9493SBabu Moger } 495fa7d9493SBabu Moger 496fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP) 497fa7d9493SBabu Moger ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1); 498fa7d9493SBabu Moger else if (rdtgrp->type == RDTMON_GROUP) 499fa7d9493SBabu Moger ret = cpus_mon_write(rdtgrp, newmask, tmpmask); 500fa7d9493SBabu Moger else 501fa7d9493SBabu Moger ret = -EINVAL; 502fa7d9493SBabu Moger 503fa7d9493SBabu Moger unlock: 504fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 505fa7d9493SBabu Moger free_cpumask_var(tmpmask); 506fa7d9493SBabu Moger free_cpumask_var(newmask); 507fa7d9493SBabu Moger free_cpumask_var(tmpmask1); 508fa7d9493SBabu Moger 509fa7d9493SBabu Moger return ret ?: nbytes; 510fa7d9493SBabu Moger } 511fa7d9493SBabu Moger 512fa7d9493SBabu Moger struct task_move_callback { 513fa7d9493SBabu Moger struct callback_head work; 514fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 515fa7d9493SBabu Moger }; 516fa7d9493SBabu Moger 517fa7d9493SBabu Moger static void move_myself(struct callback_head *head) 518fa7d9493SBabu Moger { 519fa7d9493SBabu Moger struct task_move_callback *callback; 520fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 521fa7d9493SBabu Moger 522fa7d9493SBabu Moger callback = container_of(head, struct task_move_callback, work); 523fa7d9493SBabu Moger rdtgrp = callback->rdtgrp; 524fa7d9493SBabu Moger 525fa7d9493SBabu Moger /* 526fa7d9493SBabu Moger * If resource group was deleted before this task work callback 527fa7d9493SBabu Moger * was invoked, then assign the task to root group and free the 528fa7d9493SBabu Moger * resource group. 529fa7d9493SBabu Moger */ 530fa7d9493SBabu Moger if (atomic_dec_and_test(&rdtgrp->waitcount) && 531fa7d9493SBabu Moger (rdtgrp->flags & RDT_DELETED)) { 532fa7d9493SBabu Moger current->closid = 0; 533fa7d9493SBabu Moger current->rmid = 0; 534fa7d9493SBabu Moger kfree(rdtgrp); 535fa7d9493SBabu Moger } 536fa7d9493SBabu Moger 537fa7d9493SBabu Moger preempt_disable(); 538fa7d9493SBabu Moger /* update PQR_ASSOC MSR to make resource group go into effect */ 539352940ecSBabu Moger resctrl_sched_in(); 540fa7d9493SBabu Moger preempt_enable(); 541fa7d9493SBabu Moger 542fa7d9493SBabu Moger kfree(callback); 543fa7d9493SBabu Moger } 544fa7d9493SBabu Moger 545fa7d9493SBabu Moger static int __rdtgroup_move_task(struct task_struct *tsk, 546fa7d9493SBabu Moger struct rdtgroup *rdtgrp) 547fa7d9493SBabu Moger { 548fa7d9493SBabu Moger struct task_move_callback *callback; 549fa7d9493SBabu Moger int ret; 550fa7d9493SBabu Moger 551fa7d9493SBabu Moger callback = kzalloc(sizeof(*callback), GFP_KERNEL); 552fa7d9493SBabu Moger if (!callback) 553fa7d9493SBabu Moger return -ENOMEM; 554fa7d9493SBabu Moger callback->work.func = move_myself; 555fa7d9493SBabu Moger callback->rdtgrp = rdtgrp; 556fa7d9493SBabu Moger 557fa7d9493SBabu Moger /* 558fa7d9493SBabu Moger * Take a refcount, so rdtgrp cannot be freed before the 559fa7d9493SBabu Moger * callback has been invoked. 560fa7d9493SBabu Moger */ 561fa7d9493SBabu Moger atomic_inc(&rdtgrp->waitcount); 562fa7d9493SBabu Moger ret = task_work_add(tsk, &callback->work, true); 563fa7d9493SBabu Moger if (ret) { 564fa7d9493SBabu Moger /* 565fa7d9493SBabu Moger * Task is exiting. Drop the refcount and free the callback. 566fa7d9493SBabu Moger * No need to check the refcount as the group cannot be 567fa7d9493SBabu Moger * deleted before the write function unlocks rdtgroup_mutex. 568fa7d9493SBabu Moger */ 569fa7d9493SBabu Moger atomic_dec(&rdtgrp->waitcount); 570fa7d9493SBabu Moger kfree(callback); 571723f1a0dSBabu Moger rdt_last_cmd_puts("Task exited\n"); 572fa7d9493SBabu Moger } else { 573fa7d9493SBabu Moger /* 574fa7d9493SBabu Moger * For ctrl_mon groups move both closid and rmid. 575fa7d9493SBabu Moger * For monitor groups, can move the tasks only from 576fa7d9493SBabu Moger * their parent CTRL group. 577fa7d9493SBabu Moger */ 578fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP) { 579fa7d9493SBabu Moger tsk->closid = rdtgrp->closid; 580fa7d9493SBabu Moger tsk->rmid = rdtgrp->mon.rmid; 581fa7d9493SBabu Moger } else if (rdtgrp->type == RDTMON_GROUP) { 582fa7d9493SBabu Moger if (rdtgrp->mon.parent->closid == tsk->closid) { 583fa7d9493SBabu Moger tsk->rmid = rdtgrp->mon.rmid; 584fa7d9493SBabu Moger } else { 585fa7d9493SBabu Moger rdt_last_cmd_puts("Can't move task to different control group\n"); 586fa7d9493SBabu Moger ret = -EINVAL; 587fa7d9493SBabu Moger } 588fa7d9493SBabu Moger } 589fa7d9493SBabu Moger } 590fa7d9493SBabu Moger return ret; 591fa7d9493SBabu Moger } 592fa7d9493SBabu Moger 593fa7d9493SBabu Moger /** 594fa7d9493SBabu Moger * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group 595fa7d9493SBabu Moger * @r: Resource group 596fa7d9493SBabu Moger * 597fa7d9493SBabu Moger * Return: 1 if tasks have been assigned to @r, 0 otherwise 598fa7d9493SBabu Moger */ 599fa7d9493SBabu Moger int rdtgroup_tasks_assigned(struct rdtgroup *r) 600fa7d9493SBabu Moger { 601fa7d9493SBabu Moger struct task_struct *p, *t; 602fa7d9493SBabu Moger int ret = 0; 603fa7d9493SBabu Moger 604fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 605fa7d9493SBabu Moger 606fa7d9493SBabu Moger rcu_read_lock(); 607fa7d9493SBabu Moger for_each_process_thread(p, t) { 608fa7d9493SBabu Moger if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) || 609fa7d9493SBabu Moger (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) { 610fa7d9493SBabu Moger ret = 1; 611fa7d9493SBabu Moger break; 612fa7d9493SBabu Moger } 613fa7d9493SBabu Moger } 614fa7d9493SBabu Moger rcu_read_unlock(); 615fa7d9493SBabu Moger 616fa7d9493SBabu Moger return ret; 617fa7d9493SBabu Moger } 618fa7d9493SBabu Moger 619fa7d9493SBabu Moger static int rdtgroup_task_write_permission(struct task_struct *task, 620fa7d9493SBabu Moger struct kernfs_open_file *of) 621fa7d9493SBabu Moger { 622fa7d9493SBabu Moger const struct cred *tcred = get_task_cred(task); 623fa7d9493SBabu Moger const struct cred *cred = current_cred(); 624fa7d9493SBabu Moger int ret = 0; 625fa7d9493SBabu Moger 626fa7d9493SBabu Moger /* 627fa7d9493SBabu Moger * Even if we're attaching all tasks in the thread group, we only 628fa7d9493SBabu Moger * need to check permissions on one of them. 629fa7d9493SBabu Moger */ 630fa7d9493SBabu Moger if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && 631fa7d9493SBabu Moger !uid_eq(cred->euid, tcred->uid) && 632fa7d9493SBabu Moger !uid_eq(cred->euid, tcred->suid)) { 633fa7d9493SBabu Moger rdt_last_cmd_printf("No permission to move task %d\n", task->pid); 634fa7d9493SBabu Moger ret = -EPERM; 635fa7d9493SBabu Moger } 636fa7d9493SBabu Moger 637fa7d9493SBabu Moger put_cred(tcred); 638fa7d9493SBabu Moger return ret; 639fa7d9493SBabu Moger } 640fa7d9493SBabu Moger 641fa7d9493SBabu Moger static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp, 642fa7d9493SBabu Moger struct kernfs_open_file *of) 643fa7d9493SBabu Moger { 644fa7d9493SBabu Moger struct task_struct *tsk; 645fa7d9493SBabu Moger int ret; 646fa7d9493SBabu Moger 647fa7d9493SBabu Moger rcu_read_lock(); 648fa7d9493SBabu Moger if (pid) { 649fa7d9493SBabu Moger tsk = find_task_by_vpid(pid); 650fa7d9493SBabu Moger if (!tsk) { 651fa7d9493SBabu Moger rcu_read_unlock(); 652fa7d9493SBabu Moger rdt_last_cmd_printf("No task %d\n", pid); 653fa7d9493SBabu Moger return -ESRCH; 654fa7d9493SBabu Moger } 655fa7d9493SBabu Moger } else { 656fa7d9493SBabu Moger tsk = current; 657fa7d9493SBabu Moger } 658fa7d9493SBabu Moger 659fa7d9493SBabu Moger get_task_struct(tsk); 660fa7d9493SBabu Moger rcu_read_unlock(); 661fa7d9493SBabu Moger 662fa7d9493SBabu Moger ret = rdtgroup_task_write_permission(tsk, of); 663fa7d9493SBabu Moger if (!ret) 664fa7d9493SBabu Moger ret = __rdtgroup_move_task(tsk, rdtgrp); 665fa7d9493SBabu Moger 666fa7d9493SBabu Moger put_task_struct(tsk); 667fa7d9493SBabu Moger return ret; 668fa7d9493SBabu Moger } 669fa7d9493SBabu Moger 670fa7d9493SBabu Moger static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of, 671fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 672fa7d9493SBabu Moger { 673fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 674fa7d9493SBabu Moger int ret = 0; 675fa7d9493SBabu Moger pid_t pid; 676fa7d9493SBabu Moger 677fa7d9493SBabu Moger if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) 678fa7d9493SBabu Moger return -EINVAL; 679fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 680fa7d9493SBabu Moger if (!rdtgrp) { 681fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 682fa7d9493SBabu Moger return -ENOENT; 683fa7d9493SBabu Moger } 684fa7d9493SBabu Moger rdt_last_cmd_clear(); 685fa7d9493SBabu Moger 686fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED || 687fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 688fa7d9493SBabu Moger ret = -EINVAL; 689723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 690fa7d9493SBabu Moger goto unlock; 691fa7d9493SBabu Moger } 692fa7d9493SBabu Moger 693fa7d9493SBabu Moger ret = rdtgroup_move_task(pid, rdtgrp, of); 694fa7d9493SBabu Moger 695fa7d9493SBabu Moger unlock: 696fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 697fa7d9493SBabu Moger 698fa7d9493SBabu Moger return ret ?: nbytes; 699fa7d9493SBabu Moger } 700fa7d9493SBabu Moger 701fa7d9493SBabu Moger static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s) 702fa7d9493SBabu Moger { 703fa7d9493SBabu Moger struct task_struct *p, *t; 704fa7d9493SBabu Moger 705fa7d9493SBabu Moger rcu_read_lock(); 706fa7d9493SBabu Moger for_each_process_thread(p, t) { 707fa7d9493SBabu Moger if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) || 708fa7d9493SBabu Moger (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) 709fa7d9493SBabu Moger seq_printf(s, "%d\n", t->pid); 710fa7d9493SBabu Moger } 711fa7d9493SBabu Moger rcu_read_unlock(); 712fa7d9493SBabu Moger } 713fa7d9493SBabu Moger 714fa7d9493SBabu Moger static int rdtgroup_tasks_show(struct kernfs_open_file *of, 715fa7d9493SBabu Moger struct seq_file *s, void *v) 716fa7d9493SBabu Moger { 717fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 718fa7d9493SBabu Moger int ret = 0; 719fa7d9493SBabu Moger 720fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 721fa7d9493SBabu Moger if (rdtgrp) 722fa7d9493SBabu Moger show_rdt_tasks(rdtgrp, s); 723fa7d9493SBabu Moger else 724fa7d9493SBabu Moger ret = -ENOENT; 725fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 726fa7d9493SBabu Moger 727fa7d9493SBabu Moger return ret; 728fa7d9493SBabu Moger } 729fa7d9493SBabu Moger 730fa7d9493SBabu Moger static int rdt_last_cmd_status_show(struct kernfs_open_file *of, 731fa7d9493SBabu Moger struct seq_file *seq, void *v) 732fa7d9493SBabu Moger { 733fa7d9493SBabu Moger int len; 734fa7d9493SBabu Moger 735fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 736fa7d9493SBabu Moger len = seq_buf_used(&last_cmd_status); 737fa7d9493SBabu Moger if (len) 738fa7d9493SBabu Moger seq_printf(seq, "%.*s", len, last_cmd_status_buf); 739fa7d9493SBabu Moger else 740fa7d9493SBabu Moger seq_puts(seq, "ok\n"); 741fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 742fa7d9493SBabu Moger return 0; 743fa7d9493SBabu Moger } 744fa7d9493SBabu Moger 745fa7d9493SBabu Moger static int rdt_num_closids_show(struct kernfs_open_file *of, 746fa7d9493SBabu Moger struct seq_file *seq, void *v) 747fa7d9493SBabu Moger { 748fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 749fa7d9493SBabu Moger 750fa7d9493SBabu Moger seq_printf(seq, "%d\n", r->num_closid); 751fa7d9493SBabu Moger return 0; 752fa7d9493SBabu Moger } 753fa7d9493SBabu Moger 754fa7d9493SBabu Moger static int rdt_default_ctrl_show(struct kernfs_open_file *of, 755fa7d9493SBabu Moger struct seq_file *seq, void *v) 756fa7d9493SBabu Moger { 757fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 758fa7d9493SBabu Moger 759fa7d9493SBabu Moger seq_printf(seq, "%x\n", r->default_ctrl); 760fa7d9493SBabu Moger return 0; 761fa7d9493SBabu Moger } 762fa7d9493SBabu Moger 763fa7d9493SBabu Moger static int rdt_min_cbm_bits_show(struct kernfs_open_file *of, 764fa7d9493SBabu Moger struct seq_file *seq, void *v) 765fa7d9493SBabu Moger { 766fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 767fa7d9493SBabu Moger 768fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->cache.min_cbm_bits); 769fa7d9493SBabu Moger return 0; 770fa7d9493SBabu Moger } 771fa7d9493SBabu Moger 772fa7d9493SBabu Moger static int rdt_shareable_bits_show(struct kernfs_open_file *of, 773fa7d9493SBabu Moger struct seq_file *seq, void *v) 774fa7d9493SBabu Moger { 775fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 776fa7d9493SBabu Moger 777fa7d9493SBabu Moger seq_printf(seq, "%x\n", r->cache.shareable_bits); 778fa7d9493SBabu Moger return 0; 779fa7d9493SBabu Moger } 780fa7d9493SBabu Moger 781fa7d9493SBabu Moger /** 782fa7d9493SBabu Moger * rdt_bit_usage_show - Display current usage of resources 783fa7d9493SBabu Moger * 784fa7d9493SBabu Moger * A domain is a shared resource that can now be allocated differently. Here 785fa7d9493SBabu Moger * we display the current regions of the domain as an annotated bitmask. 786fa7d9493SBabu Moger * For each domain of this resource its allocation bitmask 787fa7d9493SBabu Moger * is annotated as below to indicate the current usage of the corresponding bit: 788fa7d9493SBabu Moger * 0 - currently unused 789fa7d9493SBabu Moger * X - currently available for sharing and used by software and hardware 790fa7d9493SBabu Moger * H - currently used by hardware only but available for software use 791fa7d9493SBabu Moger * S - currently used and shareable by software only 792fa7d9493SBabu Moger * E - currently used exclusively by one resource group 793fa7d9493SBabu Moger * P - currently pseudo-locked by one resource group 794fa7d9493SBabu Moger */ 795fa7d9493SBabu Moger static int rdt_bit_usage_show(struct kernfs_open_file *of, 796fa7d9493SBabu Moger struct seq_file *seq, void *v) 797fa7d9493SBabu Moger { 798fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 79932f010deSReinette Chatre /* 80032f010deSReinette Chatre * Use unsigned long even though only 32 bits are used to ensure 80132f010deSReinette Chatre * test_bit() is used safely. 80232f010deSReinette Chatre */ 80332f010deSReinette Chatre unsigned long sw_shareable = 0, hw_shareable = 0; 80432f010deSReinette Chatre unsigned long exclusive = 0, pseudo_locked = 0; 805fa7d9493SBabu Moger struct rdt_domain *dom; 806fa7d9493SBabu Moger int i, hwb, swb, excl, psl; 807fa7d9493SBabu Moger enum rdtgrp_mode mode; 808fa7d9493SBabu Moger bool sep = false; 809fa7d9493SBabu Moger u32 *ctrl; 810fa7d9493SBabu Moger 811fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 812fa7d9493SBabu Moger hw_shareable = r->cache.shareable_bits; 813fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) { 814fa7d9493SBabu Moger if (sep) 815fa7d9493SBabu Moger seq_putc(seq, ';'); 816fa7d9493SBabu Moger ctrl = dom->ctrl_val; 817fa7d9493SBabu Moger sw_shareable = 0; 818fa7d9493SBabu Moger exclusive = 0; 819fa7d9493SBabu Moger seq_printf(seq, "%d=", dom->id); 820fa7d9493SBabu Moger for (i = 0; i < closids_supported(); i++, ctrl++) { 821fa7d9493SBabu Moger if (!closid_allocated(i)) 822fa7d9493SBabu Moger continue; 823fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 824fa7d9493SBabu Moger switch (mode) { 825fa7d9493SBabu Moger case RDT_MODE_SHAREABLE: 826fa7d9493SBabu Moger sw_shareable |= *ctrl; 827fa7d9493SBabu Moger break; 828fa7d9493SBabu Moger case RDT_MODE_EXCLUSIVE: 829fa7d9493SBabu Moger exclusive |= *ctrl; 830fa7d9493SBabu Moger break; 831fa7d9493SBabu Moger case RDT_MODE_PSEUDO_LOCKSETUP: 832fa7d9493SBabu Moger /* 833fa7d9493SBabu Moger * RDT_MODE_PSEUDO_LOCKSETUP is possible 834fa7d9493SBabu Moger * here but not included since the CBM 835fa7d9493SBabu Moger * associated with this CLOSID in this mode 836fa7d9493SBabu Moger * is not initialized and no task or cpu can be 837fa7d9493SBabu Moger * assigned this CLOSID. 838fa7d9493SBabu Moger */ 839fa7d9493SBabu Moger break; 840fa7d9493SBabu Moger case RDT_MODE_PSEUDO_LOCKED: 841fa7d9493SBabu Moger case RDT_NUM_MODES: 842fa7d9493SBabu Moger WARN(1, 843fa7d9493SBabu Moger "invalid mode for closid %d\n", i); 844fa7d9493SBabu Moger break; 845fa7d9493SBabu Moger } 846fa7d9493SBabu Moger } 847fa7d9493SBabu Moger for (i = r->cache.cbm_len - 1; i >= 0; i--) { 848fa7d9493SBabu Moger pseudo_locked = dom->plr ? dom->plr->cbm : 0; 84932f010deSReinette Chatre hwb = test_bit(i, &hw_shareable); 85032f010deSReinette Chatre swb = test_bit(i, &sw_shareable); 85132f010deSReinette Chatre excl = test_bit(i, &exclusive); 85232f010deSReinette Chatre psl = test_bit(i, &pseudo_locked); 853fa7d9493SBabu Moger if (hwb && swb) 854fa7d9493SBabu Moger seq_putc(seq, 'X'); 855fa7d9493SBabu Moger else if (hwb && !swb) 856fa7d9493SBabu Moger seq_putc(seq, 'H'); 857fa7d9493SBabu Moger else if (!hwb && swb) 858fa7d9493SBabu Moger seq_putc(seq, 'S'); 859fa7d9493SBabu Moger else if (excl) 860fa7d9493SBabu Moger seq_putc(seq, 'E'); 861fa7d9493SBabu Moger else if (psl) 862fa7d9493SBabu Moger seq_putc(seq, 'P'); 863fa7d9493SBabu Moger else /* Unused bits remain */ 864fa7d9493SBabu Moger seq_putc(seq, '0'); 865fa7d9493SBabu Moger } 866fa7d9493SBabu Moger sep = true; 867fa7d9493SBabu Moger } 868fa7d9493SBabu Moger seq_putc(seq, '\n'); 869fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 870fa7d9493SBabu Moger return 0; 871fa7d9493SBabu Moger } 872fa7d9493SBabu Moger 873fa7d9493SBabu Moger static int rdt_min_bw_show(struct kernfs_open_file *of, 874fa7d9493SBabu Moger struct seq_file *seq, void *v) 875fa7d9493SBabu Moger { 876fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 877fa7d9493SBabu Moger 878fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.min_bw); 879fa7d9493SBabu Moger return 0; 880fa7d9493SBabu Moger } 881fa7d9493SBabu Moger 882fa7d9493SBabu Moger static int rdt_num_rmids_show(struct kernfs_open_file *of, 883fa7d9493SBabu Moger struct seq_file *seq, void *v) 884fa7d9493SBabu Moger { 885fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 886fa7d9493SBabu Moger 887fa7d9493SBabu Moger seq_printf(seq, "%d\n", r->num_rmid); 888fa7d9493SBabu Moger 889fa7d9493SBabu Moger return 0; 890fa7d9493SBabu Moger } 891fa7d9493SBabu Moger 892fa7d9493SBabu Moger static int rdt_mon_features_show(struct kernfs_open_file *of, 893fa7d9493SBabu Moger struct seq_file *seq, void *v) 894fa7d9493SBabu Moger { 895fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 896fa7d9493SBabu Moger struct mon_evt *mevt; 897fa7d9493SBabu Moger 898fa7d9493SBabu Moger list_for_each_entry(mevt, &r->evt_list, list) 899fa7d9493SBabu Moger seq_printf(seq, "%s\n", mevt->name); 900fa7d9493SBabu Moger 901fa7d9493SBabu Moger return 0; 902fa7d9493SBabu Moger } 903fa7d9493SBabu Moger 904fa7d9493SBabu Moger static int rdt_bw_gran_show(struct kernfs_open_file *of, 905fa7d9493SBabu Moger struct seq_file *seq, void *v) 906fa7d9493SBabu Moger { 907fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 908fa7d9493SBabu Moger 909fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.bw_gran); 910fa7d9493SBabu Moger return 0; 911fa7d9493SBabu Moger } 912fa7d9493SBabu Moger 913fa7d9493SBabu Moger static int rdt_delay_linear_show(struct kernfs_open_file *of, 914fa7d9493SBabu Moger struct seq_file *seq, void *v) 915fa7d9493SBabu Moger { 916fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 917fa7d9493SBabu Moger 918fa7d9493SBabu Moger seq_printf(seq, "%u\n", r->membw.delay_linear); 919fa7d9493SBabu Moger return 0; 920fa7d9493SBabu Moger } 921fa7d9493SBabu Moger 922fa7d9493SBabu Moger static int max_threshold_occ_show(struct kernfs_open_file *of, 923fa7d9493SBabu Moger struct seq_file *seq, void *v) 924fa7d9493SBabu Moger { 925fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 926fa7d9493SBabu Moger 927352940ecSBabu Moger seq_printf(seq, "%u\n", resctrl_cqm_threshold * r->mon_scale); 928fa7d9493SBabu Moger 929fa7d9493SBabu Moger return 0; 930fa7d9493SBabu Moger } 931fa7d9493SBabu Moger 932fa7d9493SBabu Moger static ssize_t max_threshold_occ_write(struct kernfs_open_file *of, 933fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 934fa7d9493SBabu Moger { 935fa7d9493SBabu Moger struct rdt_resource *r = of->kn->parent->priv; 936fa7d9493SBabu Moger unsigned int bytes; 937fa7d9493SBabu Moger int ret; 938fa7d9493SBabu Moger 939fa7d9493SBabu Moger ret = kstrtouint(buf, 0, &bytes); 940fa7d9493SBabu Moger if (ret) 941fa7d9493SBabu Moger return ret; 942fa7d9493SBabu Moger 943fa7d9493SBabu Moger if (bytes > (boot_cpu_data.x86_cache_size * 1024)) 944fa7d9493SBabu Moger return -EINVAL; 945fa7d9493SBabu Moger 946352940ecSBabu Moger resctrl_cqm_threshold = bytes / r->mon_scale; 947fa7d9493SBabu Moger 948fa7d9493SBabu Moger return nbytes; 949fa7d9493SBabu Moger } 950fa7d9493SBabu Moger 951fa7d9493SBabu Moger /* 952fa7d9493SBabu Moger * rdtgroup_mode_show - Display mode of this resource group 953fa7d9493SBabu Moger */ 954fa7d9493SBabu Moger static int rdtgroup_mode_show(struct kernfs_open_file *of, 955fa7d9493SBabu Moger struct seq_file *s, void *v) 956fa7d9493SBabu Moger { 957fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 958fa7d9493SBabu Moger 959fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 960fa7d9493SBabu Moger if (!rdtgrp) { 961fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 962fa7d9493SBabu Moger return -ENOENT; 963fa7d9493SBabu Moger } 964fa7d9493SBabu Moger 965fa7d9493SBabu Moger seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode)); 966fa7d9493SBabu Moger 967fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 968fa7d9493SBabu Moger return 0; 969fa7d9493SBabu Moger } 970fa7d9493SBabu Moger 971fa7d9493SBabu Moger /** 972fa7d9493SBabu Moger * rdt_cdp_peer_get - Retrieve CDP peer if it exists 973fa7d9493SBabu Moger * @r: RDT resource to which RDT domain @d belongs 974fa7d9493SBabu Moger * @d: Cache instance for which a CDP peer is requested 975fa7d9493SBabu Moger * @r_cdp: RDT resource that shares hardware with @r (RDT resource peer) 976fa7d9493SBabu Moger * Used to return the result. 977fa7d9493SBabu Moger * @d_cdp: RDT domain that shares hardware with @d (RDT domain peer) 978fa7d9493SBabu Moger * Used to return the result. 979fa7d9493SBabu Moger * 980fa7d9493SBabu Moger * RDT resources are managed independently and by extension the RDT domains 981fa7d9493SBabu Moger * (RDT resource instances) are managed independently also. The Code and 982fa7d9493SBabu Moger * Data Prioritization (CDP) RDT resources, while managed independently, 983fa7d9493SBabu Moger * could refer to the same underlying hardware. For example, 984fa7d9493SBabu Moger * RDT_RESOURCE_L2CODE and RDT_RESOURCE_L2DATA both refer to the L2 cache. 985fa7d9493SBabu Moger * 986fa7d9493SBabu Moger * When provided with an RDT resource @r and an instance of that RDT 987fa7d9493SBabu Moger * resource @d rdt_cdp_peer_get() will return if there is a peer RDT 988fa7d9493SBabu Moger * resource and the exact instance that shares the same hardware. 989fa7d9493SBabu Moger * 990fa7d9493SBabu Moger * Return: 0 if a CDP peer was found, <0 on error or if no CDP peer exists. 991fa7d9493SBabu Moger * If a CDP peer was found, @r_cdp will point to the peer RDT resource 992fa7d9493SBabu Moger * and @d_cdp will point to the peer RDT domain. 993fa7d9493SBabu Moger */ 994fa7d9493SBabu Moger static int rdt_cdp_peer_get(struct rdt_resource *r, struct rdt_domain *d, 995fa7d9493SBabu Moger struct rdt_resource **r_cdp, 996fa7d9493SBabu Moger struct rdt_domain **d_cdp) 997fa7d9493SBabu Moger { 998fa7d9493SBabu Moger struct rdt_resource *_r_cdp = NULL; 999fa7d9493SBabu Moger struct rdt_domain *_d_cdp = NULL; 1000fa7d9493SBabu Moger int ret = 0; 1001fa7d9493SBabu Moger 1002fa7d9493SBabu Moger switch (r->rid) { 1003fa7d9493SBabu Moger case RDT_RESOURCE_L3DATA: 1004fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3CODE]; 1005fa7d9493SBabu Moger break; 1006fa7d9493SBabu Moger case RDT_RESOURCE_L3CODE: 1007fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L3DATA]; 1008fa7d9493SBabu Moger break; 1009fa7d9493SBabu Moger case RDT_RESOURCE_L2DATA: 1010fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2CODE]; 1011fa7d9493SBabu Moger break; 1012fa7d9493SBabu Moger case RDT_RESOURCE_L2CODE: 1013fa7d9493SBabu Moger _r_cdp = &rdt_resources_all[RDT_RESOURCE_L2DATA]; 1014fa7d9493SBabu Moger break; 1015fa7d9493SBabu Moger default: 1016fa7d9493SBabu Moger ret = -ENOENT; 1017fa7d9493SBabu Moger goto out; 1018fa7d9493SBabu Moger } 1019fa7d9493SBabu Moger 1020fa7d9493SBabu Moger /* 1021fa7d9493SBabu Moger * When a new CPU comes online and CDP is enabled then the new 1022fa7d9493SBabu Moger * RDT domains (if any) associated with both CDP RDT resources 1023fa7d9493SBabu Moger * are added in the same CPU online routine while the 1024fa7d9493SBabu Moger * rdtgroup_mutex is held. It should thus not happen for one 1025fa7d9493SBabu Moger * RDT domain to exist and be associated with its RDT CDP 1026fa7d9493SBabu Moger * resource but there is no RDT domain associated with the 1027fa7d9493SBabu Moger * peer RDT CDP resource. Hence the WARN. 1028fa7d9493SBabu Moger */ 1029fa7d9493SBabu Moger _d_cdp = rdt_find_domain(_r_cdp, d->id, NULL); 103052eb7433SReinette Chatre if (WARN_ON(IS_ERR_OR_NULL(_d_cdp))) { 1031fa7d9493SBabu Moger _r_cdp = NULL; 1032fa7d9493SBabu Moger ret = -EINVAL; 1033fa7d9493SBabu Moger } 1034fa7d9493SBabu Moger 1035fa7d9493SBabu Moger out: 1036fa7d9493SBabu Moger *r_cdp = _r_cdp; 1037fa7d9493SBabu Moger *d_cdp = _d_cdp; 1038fa7d9493SBabu Moger 1039fa7d9493SBabu Moger return ret; 1040fa7d9493SBabu Moger } 1041fa7d9493SBabu Moger 1042fa7d9493SBabu Moger /** 1043fa7d9493SBabu Moger * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other 1044fa7d9493SBabu Moger * @r: Resource to which domain instance @d belongs. 1045fa7d9493SBabu Moger * @d: The domain instance for which @closid is being tested. 1046fa7d9493SBabu Moger * @cbm: Capacity bitmask being tested. 1047fa7d9493SBabu Moger * @closid: Intended closid for @cbm. 1048fa7d9493SBabu Moger * @exclusive: Only check if overlaps with exclusive resource groups 1049fa7d9493SBabu Moger * 1050fa7d9493SBabu Moger * Checks if provided @cbm intended to be used for @closid on domain 1051fa7d9493SBabu Moger * @d overlaps with any other closids or other hardware usage associated 1052fa7d9493SBabu Moger * with this domain. If @exclusive is true then only overlaps with 1053fa7d9493SBabu Moger * resource groups in exclusive mode will be considered. If @exclusive 1054fa7d9493SBabu Moger * is false then overlaps with any resource group or hardware entities 1055fa7d9493SBabu Moger * will be considered. 1056fa7d9493SBabu Moger * 1057fa7d9493SBabu Moger * @cbm is unsigned long, even if only 32 bits are used, to make the 1058fa7d9493SBabu Moger * bitmap functions work correctly. 1059fa7d9493SBabu Moger * 1060fa7d9493SBabu Moger * Return: false if CBM does not overlap, true if it does. 1061fa7d9493SBabu Moger */ 1062fa7d9493SBabu Moger static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, 1063fa7d9493SBabu Moger unsigned long cbm, int closid, bool exclusive) 1064fa7d9493SBabu Moger { 1065fa7d9493SBabu Moger enum rdtgrp_mode mode; 1066fa7d9493SBabu Moger unsigned long ctrl_b; 1067fa7d9493SBabu Moger u32 *ctrl; 1068fa7d9493SBabu Moger int i; 1069fa7d9493SBabu Moger 1070fa7d9493SBabu Moger /* Check for any overlap with regions used by hardware directly */ 1071fa7d9493SBabu Moger if (!exclusive) { 1072fa7d9493SBabu Moger ctrl_b = r->cache.shareable_bits; 1073fa7d9493SBabu Moger if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) 1074fa7d9493SBabu Moger return true; 1075fa7d9493SBabu Moger } 1076fa7d9493SBabu Moger 1077fa7d9493SBabu Moger /* Check for overlap with other resource groups */ 1078fa7d9493SBabu Moger ctrl = d->ctrl_val; 1079fa7d9493SBabu Moger for (i = 0; i < closids_supported(); i++, ctrl++) { 1080fa7d9493SBabu Moger ctrl_b = *ctrl; 1081fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 1082fa7d9493SBabu Moger if (closid_allocated(i) && i != closid && 1083fa7d9493SBabu Moger mode != RDT_MODE_PSEUDO_LOCKSETUP) { 1084fa7d9493SBabu Moger if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) { 1085fa7d9493SBabu Moger if (exclusive) { 1086fa7d9493SBabu Moger if (mode == RDT_MODE_EXCLUSIVE) 1087fa7d9493SBabu Moger return true; 1088fa7d9493SBabu Moger continue; 1089fa7d9493SBabu Moger } 1090fa7d9493SBabu Moger return true; 1091fa7d9493SBabu Moger } 1092fa7d9493SBabu Moger } 1093fa7d9493SBabu Moger } 1094fa7d9493SBabu Moger 1095fa7d9493SBabu Moger return false; 1096fa7d9493SBabu Moger } 1097fa7d9493SBabu Moger 1098fa7d9493SBabu Moger /** 1099fa7d9493SBabu Moger * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware 1100fa7d9493SBabu Moger * @r: Resource to which domain instance @d belongs. 1101fa7d9493SBabu Moger * @d: The domain instance for which @closid is being tested. 1102fa7d9493SBabu Moger * @cbm: Capacity bitmask being tested. 1103fa7d9493SBabu Moger * @closid: Intended closid for @cbm. 1104fa7d9493SBabu Moger * @exclusive: Only check if overlaps with exclusive resource groups 1105fa7d9493SBabu Moger * 1106fa7d9493SBabu Moger * Resources that can be allocated using a CBM can use the CBM to control 1107fa7d9493SBabu Moger * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test 1108fa7d9493SBabu Moger * for overlap. Overlap test is not limited to the specific resource for 1109fa7d9493SBabu Moger * which the CBM is intended though - when dealing with CDP resources that 1110fa7d9493SBabu Moger * share the underlying hardware the overlap check should be performed on 1111fa7d9493SBabu Moger * the CDP resource sharing the hardware also. 1112fa7d9493SBabu Moger * 1113fa7d9493SBabu Moger * Refer to description of __rdtgroup_cbm_overlaps() for the details of the 1114fa7d9493SBabu Moger * overlap test. 1115fa7d9493SBabu Moger * 1116fa7d9493SBabu Moger * Return: true if CBM overlap detected, false if there is no overlap 1117fa7d9493SBabu Moger */ 1118fa7d9493SBabu Moger bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, 1119fa7d9493SBabu Moger unsigned long cbm, int closid, bool exclusive) 1120fa7d9493SBabu Moger { 1121fa7d9493SBabu Moger struct rdt_resource *r_cdp; 1122fa7d9493SBabu Moger struct rdt_domain *d_cdp; 1123fa7d9493SBabu Moger 1124fa7d9493SBabu Moger if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, exclusive)) 1125fa7d9493SBabu Moger return true; 1126fa7d9493SBabu Moger 1127fa7d9493SBabu Moger if (rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp) < 0) 1128fa7d9493SBabu Moger return false; 1129fa7d9493SBabu Moger 1130fa7d9493SBabu Moger return __rdtgroup_cbm_overlaps(r_cdp, d_cdp, cbm, closid, exclusive); 1131fa7d9493SBabu Moger } 1132fa7d9493SBabu Moger 1133fa7d9493SBabu Moger /** 1134fa7d9493SBabu Moger * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive 1135fa7d9493SBabu Moger * 1136fa7d9493SBabu Moger * An exclusive resource group implies that there should be no sharing of 1137fa7d9493SBabu Moger * its allocated resources. At the time this group is considered to be 1138fa7d9493SBabu Moger * exclusive this test can determine if its current schemata supports this 1139fa7d9493SBabu Moger * setting by testing for overlap with all other resource groups. 1140fa7d9493SBabu Moger * 1141fa7d9493SBabu Moger * Return: true if resource group can be exclusive, false if there is overlap 1142fa7d9493SBabu Moger * with allocations of other resource groups and thus this resource group 1143fa7d9493SBabu Moger * cannot be exclusive. 1144fa7d9493SBabu Moger */ 1145fa7d9493SBabu Moger static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp) 1146fa7d9493SBabu Moger { 1147fa7d9493SBabu Moger int closid = rdtgrp->closid; 1148fa7d9493SBabu Moger struct rdt_resource *r; 1149fa7d9493SBabu Moger bool has_cache = false; 1150fa7d9493SBabu Moger struct rdt_domain *d; 1151fa7d9493SBabu Moger 1152fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 1153fa7d9493SBabu Moger if (r->rid == RDT_RESOURCE_MBA) 1154fa7d9493SBabu Moger continue; 1155fa7d9493SBabu Moger has_cache = true; 1156fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 1157fa7d9493SBabu Moger if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid], 1158fa7d9493SBabu Moger rdtgrp->closid, false)) { 1159723f1a0dSBabu Moger rdt_last_cmd_puts("Schemata overlaps\n"); 1160fa7d9493SBabu Moger return false; 1161fa7d9493SBabu Moger } 1162fa7d9493SBabu Moger } 1163fa7d9493SBabu Moger } 1164fa7d9493SBabu Moger 1165fa7d9493SBabu Moger if (!has_cache) { 1166723f1a0dSBabu Moger rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n"); 1167fa7d9493SBabu Moger return false; 1168fa7d9493SBabu Moger } 1169fa7d9493SBabu Moger 1170fa7d9493SBabu Moger return true; 1171fa7d9493SBabu Moger } 1172fa7d9493SBabu Moger 1173fa7d9493SBabu Moger /** 1174fa7d9493SBabu Moger * rdtgroup_mode_write - Modify the resource group's mode 1175fa7d9493SBabu Moger * 1176fa7d9493SBabu Moger */ 1177fa7d9493SBabu Moger static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, 1178fa7d9493SBabu Moger char *buf, size_t nbytes, loff_t off) 1179fa7d9493SBabu Moger { 1180fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1181fa7d9493SBabu Moger enum rdtgrp_mode mode; 1182fa7d9493SBabu Moger int ret = 0; 1183fa7d9493SBabu Moger 1184fa7d9493SBabu Moger /* Valid input requires a trailing newline */ 1185fa7d9493SBabu Moger if (nbytes == 0 || buf[nbytes - 1] != '\n') 1186fa7d9493SBabu Moger return -EINVAL; 1187fa7d9493SBabu Moger buf[nbytes - 1] = '\0'; 1188fa7d9493SBabu Moger 1189fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1190fa7d9493SBabu Moger if (!rdtgrp) { 1191fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1192fa7d9493SBabu Moger return -ENOENT; 1193fa7d9493SBabu Moger } 1194fa7d9493SBabu Moger 1195fa7d9493SBabu Moger rdt_last_cmd_clear(); 1196fa7d9493SBabu Moger 1197fa7d9493SBabu Moger mode = rdtgrp->mode; 1198fa7d9493SBabu Moger 1199fa7d9493SBabu Moger if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) || 1200fa7d9493SBabu Moger (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) || 1201fa7d9493SBabu Moger (!strcmp(buf, "pseudo-locksetup") && 1202fa7d9493SBabu Moger mode == RDT_MODE_PSEUDO_LOCKSETUP) || 1203fa7d9493SBabu Moger (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED)) 1204fa7d9493SBabu Moger goto out; 1205fa7d9493SBabu Moger 1206fa7d9493SBabu Moger if (mode == RDT_MODE_PSEUDO_LOCKED) { 120745682489SReinette Chatre rdt_last_cmd_puts("Cannot change pseudo-locked group\n"); 1208fa7d9493SBabu Moger ret = -EINVAL; 1209fa7d9493SBabu Moger goto out; 1210fa7d9493SBabu Moger } 1211fa7d9493SBabu Moger 1212fa7d9493SBabu Moger if (!strcmp(buf, "shareable")) { 1213fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1214fa7d9493SBabu Moger ret = rdtgroup_locksetup_exit(rdtgrp); 1215fa7d9493SBabu Moger if (ret) 1216fa7d9493SBabu Moger goto out; 1217fa7d9493SBabu Moger } 1218fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_SHAREABLE; 1219fa7d9493SBabu Moger } else if (!strcmp(buf, "exclusive")) { 1220fa7d9493SBabu Moger if (!rdtgroup_mode_test_exclusive(rdtgrp)) { 1221fa7d9493SBabu Moger ret = -EINVAL; 1222fa7d9493SBabu Moger goto out; 1223fa7d9493SBabu Moger } 1224fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1225fa7d9493SBabu Moger ret = rdtgroup_locksetup_exit(rdtgrp); 1226fa7d9493SBabu Moger if (ret) 1227fa7d9493SBabu Moger goto out; 1228fa7d9493SBabu Moger } 1229fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_EXCLUSIVE; 1230fa7d9493SBabu Moger } else if (!strcmp(buf, "pseudo-locksetup")) { 1231fa7d9493SBabu Moger ret = rdtgroup_locksetup_enter(rdtgrp); 1232fa7d9493SBabu Moger if (ret) 1233fa7d9493SBabu Moger goto out; 1234fa7d9493SBabu Moger rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP; 1235fa7d9493SBabu Moger } else { 123645682489SReinette Chatre rdt_last_cmd_puts("Unknown or unsupported mode\n"); 1237fa7d9493SBabu Moger ret = -EINVAL; 1238fa7d9493SBabu Moger } 1239fa7d9493SBabu Moger 1240fa7d9493SBabu Moger out: 1241fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1242fa7d9493SBabu Moger return ret ?: nbytes; 1243fa7d9493SBabu Moger } 1244fa7d9493SBabu Moger 1245fa7d9493SBabu Moger /** 1246fa7d9493SBabu Moger * rdtgroup_cbm_to_size - Translate CBM to size in bytes 1247fa7d9493SBabu Moger * @r: RDT resource to which @d belongs. 1248fa7d9493SBabu Moger * @d: RDT domain instance. 1249fa7d9493SBabu Moger * @cbm: bitmask for which the size should be computed. 1250fa7d9493SBabu Moger * 1251fa7d9493SBabu Moger * The bitmask provided associated with the RDT domain instance @d will be 1252fa7d9493SBabu Moger * translated into how many bytes it represents. The size in bytes is 1253fa7d9493SBabu Moger * computed by first dividing the total cache size by the CBM length to 1254fa7d9493SBabu Moger * determine how many bytes each bit in the bitmask represents. The result 1255fa7d9493SBabu Moger * is multiplied with the number of bits set in the bitmask. 1256fa7d9493SBabu Moger * 1257fa7d9493SBabu Moger * @cbm is unsigned long, even if only 32 bits are used to make the 1258fa7d9493SBabu Moger * bitmap functions work correctly. 1259fa7d9493SBabu Moger */ 1260fa7d9493SBabu Moger unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, 1261fa7d9493SBabu Moger struct rdt_domain *d, unsigned long cbm) 1262fa7d9493SBabu Moger { 1263fa7d9493SBabu Moger struct cpu_cacheinfo *ci; 1264fa7d9493SBabu Moger unsigned int size = 0; 1265fa7d9493SBabu Moger int num_b, i; 1266fa7d9493SBabu Moger 1267fa7d9493SBabu Moger num_b = bitmap_weight(&cbm, r->cache.cbm_len); 1268fa7d9493SBabu Moger ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask)); 1269fa7d9493SBabu Moger for (i = 0; i < ci->num_leaves; i++) { 1270fa7d9493SBabu Moger if (ci->info_list[i].level == r->cache_level) { 1271fa7d9493SBabu Moger size = ci->info_list[i].size / r->cache.cbm_len * num_b; 1272fa7d9493SBabu Moger break; 1273fa7d9493SBabu Moger } 1274fa7d9493SBabu Moger } 1275fa7d9493SBabu Moger 1276fa7d9493SBabu Moger return size; 1277fa7d9493SBabu Moger } 1278fa7d9493SBabu Moger 1279fa7d9493SBabu Moger /** 1280fa7d9493SBabu Moger * rdtgroup_size_show - Display size in bytes of allocated regions 1281fa7d9493SBabu Moger * 1282fa7d9493SBabu Moger * The "size" file mirrors the layout of the "schemata" file, printing the 1283fa7d9493SBabu Moger * size in bytes of each region instead of the capacity bitmask. 1284fa7d9493SBabu Moger * 1285fa7d9493SBabu Moger */ 1286fa7d9493SBabu Moger static int rdtgroup_size_show(struct kernfs_open_file *of, 1287fa7d9493SBabu Moger struct seq_file *s, void *v) 1288fa7d9493SBabu Moger { 1289fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 1290fa7d9493SBabu Moger struct rdt_resource *r; 1291fa7d9493SBabu Moger struct rdt_domain *d; 1292fa7d9493SBabu Moger unsigned int size; 1293fa7d9493SBabu Moger int ret = 0; 1294fa7d9493SBabu Moger bool sep; 1295fa7d9493SBabu Moger u32 ctrl; 1296fa7d9493SBabu Moger 1297fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(of->kn); 1298fa7d9493SBabu Moger if (!rdtgrp) { 1299fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1300fa7d9493SBabu Moger return -ENOENT; 1301fa7d9493SBabu Moger } 1302fa7d9493SBabu Moger 1303fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 1304fa7d9493SBabu Moger if (!rdtgrp->plr->d) { 1305fa7d9493SBabu Moger rdt_last_cmd_clear(); 1306fa7d9493SBabu Moger rdt_last_cmd_puts("Cache domain offline\n"); 1307fa7d9493SBabu Moger ret = -ENODEV; 1308fa7d9493SBabu Moger } else { 1309fa7d9493SBabu Moger seq_printf(s, "%*s:", max_name_width, 1310fa7d9493SBabu Moger rdtgrp->plr->r->name); 1311fa7d9493SBabu Moger size = rdtgroup_cbm_to_size(rdtgrp->plr->r, 1312fa7d9493SBabu Moger rdtgrp->plr->d, 1313fa7d9493SBabu Moger rdtgrp->plr->cbm); 1314fa7d9493SBabu Moger seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size); 1315fa7d9493SBabu Moger } 1316fa7d9493SBabu Moger goto out; 1317fa7d9493SBabu Moger } 1318fa7d9493SBabu Moger 1319fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 1320fa7d9493SBabu Moger sep = false; 1321fa7d9493SBabu Moger seq_printf(s, "%*s:", max_name_width, r->name); 1322fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 1323fa7d9493SBabu Moger if (sep) 1324fa7d9493SBabu Moger seq_putc(s, ';'); 1325fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { 1326fa7d9493SBabu Moger size = 0; 1327fa7d9493SBabu Moger } else { 1328fa7d9493SBabu Moger ctrl = (!is_mba_sc(r) ? 1329fa7d9493SBabu Moger d->ctrl_val[rdtgrp->closid] : 1330fa7d9493SBabu Moger d->mbps_val[rdtgrp->closid]); 1331fa7d9493SBabu Moger if (r->rid == RDT_RESOURCE_MBA) 1332fa7d9493SBabu Moger size = ctrl; 1333fa7d9493SBabu Moger else 1334fa7d9493SBabu Moger size = rdtgroup_cbm_to_size(r, d, ctrl); 1335fa7d9493SBabu Moger } 1336fa7d9493SBabu Moger seq_printf(s, "%d=%u", d->id, size); 1337fa7d9493SBabu Moger sep = true; 1338fa7d9493SBabu Moger } 1339fa7d9493SBabu Moger seq_putc(s, '\n'); 1340fa7d9493SBabu Moger } 1341fa7d9493SBabu Moger 1342fa7d9493SBabu Moger out: 1343fa7d9493SBabu Moger rdtgroup_kn_unlock(of->kn); 1344fa7d9493SBabu Moger 1345fa7d9493SBabu Moger return ret; 1346fa7d9493SBabu Moger } 1347fa7d9493SBabu Moger 1348fa7d9493SBabu Moger /* rdtgroup information files for one cache resource. */ 1349fa7d9493SBabu Moger static struct rftype res_common_files[] = { 1350fa7d9493SBabu Moger { 1351fa7d9493SBabu Moger .name = "last_cmd_status", 1352fa7d9493SBabu Moger .mode = 0444, 1353fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1354fa7d9493SBabu Moger .seq_show = rdt_last_cmd_status_show, 1355fa7d9493SBabu Moger .fflags = RF_TOP_INFO, 1356fa7d9493SBabu Moger }, 1357fa7d9493SBabu Moger { 1358fa7d9493SBabu Moger .name = "num_closids", 1359fa7d9493SBabu Moger .mode = 0444, 1360fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1361fa7d9493SBabu Moger .seq_show = rdt_num_closids_show, 1362fa7d9493SBabu Moger .fflags = RF_CTRL_INFO, 1363fa7d9493SBabu Moger }, 1364fa7d9493SBabu Moger { 1365fa7d9493SBabu Moger .name = "mon_features", 1366fa7d9493SBabu Moger .mode = 0444, 1367fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1368fa7d9493SBabu Moger .seq_show = rdt_mon_features_show, 1369fa7d9493SBabu Moger .fflags = RF_MON_INFO, 1370fa7d9493SBabu Moger }, 1371fa7d9493SBabu Moger { 1372fa7d9493SBabu Moger .name = "num_rmids", 1373fa7d9493SBabu Moger .mode = 0444, 1374fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1375fa7d9493SBabu Moger .seq_show = rdt_num_rmids_show, 1376fa7d9493SBabu Moger .fflags = RF_MON_INFO, 1377fa7d9493SBabu Moger }, 1378fa7d9493SBabu Moger { 1379fa7d9493SBabu Moger .name = "cbm_mask", 1380fa7d9493SBabu Moger .mode = 0444, 1381fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1382fa7d9493SBabu Moger .seq_show = rdt_default_ctrl_show, 1383fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1384fa7d9493SBabu Moger }, 1385fa7d9493SBabu Moger { 1386fa7d9493SBabu Moger .name = "min_cbm_bits", 1387fa7d9493SBabu Moger .mode = 0444, 1388fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1389fa7d9493SBabu Moger .seq_show = rdt_min_cbm_bits_show, 1390fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1391fa7d9493SBabu Moger }, 1392fa7d9493SBabu Moger { 1393fa7d9493SBabu Moger .name = "shareable_bits", 1394fa7d9493SBabu Moger .mode = 0444, 1395fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1396fa7d9493SBabu Moger .seq_show = rdt_shareable_bits_show, 1397fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1398fa7d9493SBabu Moger }, 1399fa7d9493SBabu Moger { 1400fa7d9493SBabu Moger .name = "bit_usage", 1401fa7d9493SBabu Moger .mode = 0444, 1402fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1403fa7d9493SBabu Moger .seq_show = rdt_bit_usage_show, 1404fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, 1405fa7d9493SBabu Moger }, 1406fa7d9493SBabu Moger { 1407fa7d9493SBabu Moger .name = "min_bandwidth", 1408fa7d9493SBabu Moger .mode = 0444, 1409fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1410fa7d9493SBabu Moger .seq_show = rdt_min_bw_show, 1411fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1412fa7d9493SBabu Moger }, 1413fa7d9493SBabu Moger { 1414fa7d9493SBabu Moger .name = "bandwidth_gran", 1415fa7d9493SBabu Moger .mode = 0444, 1416fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1417fa7d9493SBabu Moger .seq_show = rdt_bw_gran_show, 1418fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1419fa7d9493SBabu Moger }, 1420fa7d9493SBabu Moger { 1421fa7d9493SBabu Moger .name = "delay_linear", 1422fa7d9493SBabu Moger .mode = 0444, 1423fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1424fa7d9493SBabu Moger .seq_show = rdt_delay_linear_show, 1425fa7d9493SBabu Moger .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, 1426fa7d9493SBabu Moger }, 1427fa7d9493SBabu Moger { 1428fa7d9493SBabu Moger .name = "max_threshold_occupancy", 1429fa7d9493SBabu Moger .mode = 0644, 1430fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1431fa7d9493SBabu Moger .write = max_threshold_occ_write, 1432fa7d9493SBabu Moger .seq_show = max_threshold_occ_show, 1433fa7d9493SBabu Moger .fflags = RF_MON_INFO | RFTYPE_RES_CACHE, 1434fa7d9493SBabu Moger }, 1435fa7d9493SBabu Moger { 1436fa7d9493SBabu Moger .name = "cpus", 1437fa7d9493SBabu Moger .mode = 0644, 1438fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1439fa7d9493SBabu Moger .write = rdtgroup_cpus_write, 1440fa7d9493SBabu Moger .seq_show = rdtgroup_cpus_show, 1441fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1442fa7d9493SBabu Moger }, 1443fa7d9493SBabu Moger { 1444fa7d9493SBabu Moger .name = "cpus_list", 1445fa7d9493SBabu Moger .mode = 0644, 1446fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1447fa7d9493SBabu Moger .write = rdtgroup_cpus_write, 1448fa7d9493SBabu Moger .seq_show = rdtgroup_cpus_show, 1449fa7d9493SBabu Moger .flags = RFTYPE_FLAGS_CPUS_LIST, 1450fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1451fa7d9493SBabu Moger }, 1452fa7d9493SBabu Moger { 1453fa7d9493SBabu Moger .name = "tasks", 1454fa7d9493SBabu Moger .mode = 0644, 1455fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1456fa7d9493SBabu Moger .write = rdtgroup_tasks_write, 1457fa7d9493SBabu Moger .seq_show = rdtgroup_tasks_show, 1458fa7d9493SBabu Moger .fflags = RFTYPE_BASE, 1459fa7d9493SBabu Moger }, 1460fa7d9493SBabu Moger { 1461fa7d9493SBabu Moger .name = "schemata", 1462fa7d9493SBabu Moger .mode = 0644, 1463fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1464fa7d9493SBabu Moger .write = rdtgroup_schemata_write, 1465fa7d9493SBabu Moger .seq_show = rdtgroup_schemata_show, 1466fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1467fa7d9493SBabu Moger }, 1468fa7d9493SBabu Moger { 1469fa7d9493SBabu Moger .name = "mode", 1470fa7d9493SBabu Moger .mode = 0644, 1471fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1472fa7d9493SBabu Moger .write = rdtgroup_mode_write, 1473fa7d9493SBabu Moger .seq_show = rdtgroup_mode_show, 1474fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1475fa7d9493SBabu Moger }, 1476fa7d9493SBabu Moger { 1477fa7d9493SBabu Moger .name = "size", 1478fa7d9493SBabu Moger .mode = 0444, 1479fa7d9493SBabu Moger .kf_ops = &rdtgroup_kf_single_ops, 1480fa7d9493SBabu Moger .seq_show = rdtgroup_size_show, 1481fa7d9493SBabu Moger .fflags = RF_CTRL_BASE, 1482fa7d9493SBabu Moger }, 1483fa7d9493SBabu Moger 1484fa7d9493SBabu Moger }; 1485fa7d9493SBabu Moger 1486fa7d9493SBabu Moger static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags) 1487fa7d9493SBabu Moger { 1488fa7d9493SBabu Moger struct rftype *rfts, *rft; 1489fa7d9493SBabu Moger int ret, len; 1490fa7d9493SBabu Moger 1491fa7d9493SBabu Moger rfts = res_common_files; 1492fa7d9493SBabu Moger len = ARRAY_SIZE(res_common_files); 1493fa7d9493SBabu Moger 1494fa7d9493SBabu Moger lockdep_assert_held(&rdtgroup_mutex); 1495fa7d9493SBabu Moger 1496fa7d9493SBabu Moger for (rft = rfts; rft < rfts + len; rft++) { 1497fa7d9493SBabu Moger if ((fflags & rft->fflags) == rft->fflags) { 1498fa7d9493SBabu Moger ret = rdtgroup_add_file(kn, rft); 1499fa7d9493SBabu Moger if (ret) 1500fa7d9493SBabu Moger goto error; 1501fa7d9493SBabu Moger } 1502fa7d9493SBabu Moger } 1503fa7d9493SBabu Moger 1504fa7d9493SBabu Moger return 0; 1505fa7d9493SBabu Moger error: 1506fa7d9493SBabu Moger pr_warn("Failed to add %s, err=%d\n", rft->name, ret); 1507fa7d9493SBabu Moger while (--rft >= rfts) { 1508fa7d9493SBabu Moger if ((fflags & rft->fflags) == rft->fflags) 1509fa7d9493SBabu Moger kernfs_remove_by_name(kn, rft->name); 1510fa7d9493SBabu Moger } 1511fa7d9493SBabu Moger return ret; 1512fa7d9493SBabu Moger } 1513fa7d9493SBabu Moger 1514fa7d9493SBabu Moger /** 1515fa7d9493SBabu Moger * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file 1516fa7d9493SBabu Moger * @r: The resource group with which the file is associated. 1517fa7d9493SBabu Moger * @name: Name of the file 1518fa7d9493SBabu Moger * 1519fa7d9493SBabu Moger * The permissions of named resctrl file, directory, or link are modified 1520fa7d9493SBabu Moger * to not allow read, write, or execute by any user. 1521fa7d9493SBabu Moger * 1522fa7d9493SBabu Moger * WARNING: This function is intended to communicate to the user that the 1523fa7d9493SBabu Moger * resctrl file has been locked down - that it is not relevant to the 1524fa7d9493SBabu Moger * particular state the system finds itself in. It should not be relied 1525fa7d9493SBabu Moger * on to protect from user access because after the file's permissions 1526fa7d9493SBabu Moger * are restricted the user can still change the permissions using chmod 1527fa7d9493SBabu Moger * from the command line. 1528fa7d9493SBabu Moger * 1529fa7d9493SBabu Moger * Return: 0 on success, <0 on failure. 1530fa7d9493SBabu Moger */ 1531fa7d9493SBabu Moger int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name) 1532fa7d9493SBabu Moger { 1533fa7d9493SBabu Moger struct iattr iattr = {.ia_valid = ATTR_MODE,}; 1534fa7d9493SBabu Moger struct kernfs_node *kn; 1535fa7d9493SBabu Moger int ret = 0; 1536fa7d9493SBabu Moger 1537fa7d9493SBabu Moger kn = kernfs_find_and_get_ns(r->kn, name, NULL); 1538fa7d9493SBabu Moger if (!kn) 1539fa7d9493SBabu Moger return -ENOENT; 1540fa7d9493SBabu Moger 1541fa7d9493SBabu Moger switch (kernfs_type(kn)) { 1542fa7d9493SBabu Moger case KERNFS_DIR: 1543fa7d9493SBabu Moger iattr.ia_mode = S_IFDIR; 1544fa7d9493SBabu Moger break; 1545fa7d9493SBabu Moger case KERNFS_FILE: 1546fa7d9493SBabu Moger iattr.ia_mode = S_IFREG; 1547fa7d9493SBabu Moger break; 1548fa7d9493SBabu Moger case KERNFS_LINK: 1549fa7d9493SBabu Moger iattr.ia_mode = S_IFLNK; 1550fa7d9493SBabu Moger break; 1551fa7d9493SBabu Moger } 1552fa7d9493SBabu Moger 1553fa7d9493SBabu Moger ret = kernfs_setattr(kn, &iattr); 1554fa7d9493SBabu Moger kernfs_put(kn); 1555fa7d9493SBabu Moger return ret; 1556fa7d9493SBabu Moger } 1557fa7d9493SBabu Moger 1558fa7d9493SBabu Moger /** 1559fa7d9493SBabu Moger * rdtgroup_kn_mode_restore - Restore user access to named resctrl file 1560fa7d9493SBabu Moger * @r: The resource group with which the file is associated. 1561fa7d9493SBabu Moger * @name: Name of the file 1562fa7d9493SBabu Moger * @mask: Mask of permissions that should be restored 1563fa7d9493SBabu Moger * 1564fa7d9493SBabu Moger * Restore the permissions of the named file. If @name is a directory the 1565fa7d9493SBabu Moger * permissions of its parent will be used. 1566fa7d9493SBabu Moger * 1567fa7d9493SBabu Moger * Return: 0 on success, <0 on failure. 1568fa7d9493SBabu Moger */ 1569fa7d9493SBabu Moger int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name, 1570fa7d9493SBabu Moger umode_t mask) 1571fa7d9493SBabu Moger { 1572fa7d9493SBabu Moger struct iattr iattr = {.ia_valid = ATTR_MODE,}; 1573fa7d9493SBabu Moger struct kernfs_node *kn, *parent; 1574fa7d9493SBabu Moger struct rftype *rfts, *rft; 1575fa7d9493SBabu Moger int ret, len; 1576fa7d9493SBabu Moger 1577fa7d9493SBabu Moger rfts = res_common_files; 1578fa7d9493SBabu Moger len = ARRAY_SIZE(res_common_files); 1579fa7d9493SBabu Moger 1580fa7d9493SBabu Moger for (rft = rfts; rft < rfts + len; rft++) { 1581fa7d9493SBabu Moger if (!strcmp(rft->name, name)) 1582fa7d9493SBabu Moger iattr.ia_mode = rft->mode & mask; 1583fa7d9493SBabu Moger } 1584fa7d9493SBabu Moger 1585fa7d9493SBabu Moger kn = kernfs_find_and_get_ns(r->kn, name, NULL); 1586fa7d9493SBabu Moger if (!kn) 1587fa7d9493SBabu Moger return -ENOENT; 1588fa7d9493SBabu Moger 1589fa7d9493SBabu Moger switch (kernfs_type(kn)) { 1590fa7d9493SBabu Moger case KERNFS_DIR: 1591fa7d9493SBabu Moger parent = kernfs_get_parent(kn); 1592fa7d9493SBabu Moger if (parent) { 1593fa7d9493SBabu Moger iattr.ia_mode |= parent->mode; 1594fa7d9493SBabu Moger kernfs_put(parent); 1595fa7d9493SBabu Moger } 1596fa7d9493SBabu Moger iattr.ia_mode |= S_IFDIR; 1597fa7d9493SBabu Moger break; 1598fa7d9493SBabu Moger case KERNFS_FILE: 1599fa7d9493SBabu Moger iattr.ia_mode |= S_IFREG; 1600fa7d9493SBabu Moger break; 1601fa7d9493SBabu Moger case KERNFS_LINK: 1602fa7d9493SBabu Moger iattr.ia_mode |= S_IFLNK; 1603fa7d9493SBabu Moger break; 1604fa7d9493SBabu Moger } 1605fa7d9493SBabu Moger 1606fa7d9493SBabu Moger ret = kernfs_setattr(kn, &iattr); 1607fa7d9493SBabu Moger kernfs_put(kn); 1608fa7d9493SBabu Moger return ret; 1609fa7d9493SBabu Moger } 1610fa7d9493SBabu Moger 1611fa7d9493SBabu Moger static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name, 1612fa7d9493SBabu Moger unsigned long fflags) 1613fa7d9493SBabu Moger { 1614fa7d9493SBabu Moger struct kernfs_node *kn_subdir; 1615fa7d9493SBabu Moger int ret; 1616fa7d9493SBabu Moger 1617fa7d9493SBabu Moger kn_subdir = kernfs_create_dir(kn_info, name, 1618fa7d9493SBabu Moger kn_info->mode, r); 1619fa7d9493SBabu Moger if (IS_ERR(kn_subdir)) 1620fa7d9493SBabu Moger return PTR_ERR(kn_subdir); 1621fa7d9493SBabu Moger 1622fa7d9493SBabu Moger kernfs_get(kn_subdir); 1623fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn_subdir); 1624fa7d9493SBabu Moger if (ret) 1625fa7d9493SBabu Moger return ret; 1626fa7d9493SBabu Moger 1627fa7d9493SBabu Moger ret = rdtgroup_add_files(kn_subdir, fflags); 1628fa7d9493SBabu Moger if (!ret) 1629fa7d9493SBabu Moger kernfs_activate(kn_subdir); 1630fa7d9493SBabu Moger 1631fa7d9493SBabu Moger return ret; 1632fa7d9493SBabu Moger } 1633fa7d9493SBabu Moger 1634fa7d9493SBabu Moger static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn) 1635fa7d9493SBabu Moger { 1636fa7d9493SBabu Moger struct rdt_resource *r; 1637fa7d9493SBabu Moger unsigned long fflags; 1638fa7d9493SBabu Moger char name[32]; 1639fa7d9493SBabu Moger int ret; 1640fa7d9493SBabu Moger 1641fa7d9493SBabu Moger /* create the directory */ 1642fa7d9493SBabu Moger kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL); 1643fa7d9493SBabu Moger if (IS_ERR(kn_info)) 1644fa7d9493SBabu Moger return PTR_ERR(kn_info); 1645fa7d9493SBabu Moger kernfs_get(kn_info); 1646fa7d9493SBabu Moger 1647fa7d9493SBabu Moger ret = rdtgroup_add_files(kn_info, RF_TOP_INFO); 1648fa7d9493SBabu Moger if (ret) 1649fa7d9493SBabu Moger goto out_destroy; 1650fa7d9493SBabu Moger 1651fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 1652fa7d9493SBabu Moger fflags = r->fflags | RF_CTRL_INFO; 1653fa7d9493SBabu Moger ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags); 1654fa7d9493SBabu Moger if (ret) 1655fa7d9493SBabu Moger goto out_destroy; 1656fa7d9493SBabu Moger } 1657fa7d9493SBabu Moger 1658fa7d9493SBabu Moger for_each_mon_enabled_rdt_resource(r) { 1659fa7d9493SBabu Moger fflags = r->fflags | RF_MON_INFO; 1660fa7d9493SBabu Moger sprintf(name, "%s_MON", r->name); 1661fa7d9493SBabu Moger ret = rdtgroup_mkdir_info_resdir(r, name, fflags); 1662fa7d9493SBabu Moger if (ret) 1663fa7d9493SBabu Moger goto out_destroy; 1664fa7d9493SBabu Moger } 1665fa7d9493SBabu Moger 1666fa7d9493SBabu Moger /* 1667fa7d9493SBabu Moger * This extra ref will be put in kernfs_remove() and guarantees 1668fa7d9493SBabu Moger * that @rdtgrp->kn is always accessible. 1669fa7d9493SBabu Moger */ 1670fa7d9493SBabu Moger kernfs_get(kn_info); 1671fa7d9493SBabu Moger 1672fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn_info); 1673fa7d9493SBabu Moger if (ret) 1674fa7d9493SBabu Moger goto out_destroy; 1675fa7d9493SBabu Moger 1676fa7d9493SBabu Moger kernfs_activate(kn_info); 1677fa7d9493SBabu Moger 1678fa7d9493SBabu Moger return 0; 1679fa7d9493SBabu Moger 1680fa7d9493SBabu Moger out_destroy: 1681fa7d9493SBabu Moger kernfs_remove(kn_info); 1682fa7d9493SBabu Moger return ret; 1683fa7d9493SBabu Moger } 1684fa7d9493SBabu Moger 1685fa7d9493SBabu Moger static int 1686fa7d9493SBabu Moger mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp, 1687fa7d9493SBabu Moger char *name, struct kernfs_node **dest_kn) 1688fa7d9493SBabu Moger { 1689fa7d9493SBabu Moger struct kernfs_node *kn; 1690fa7d9493SBabu Moger int ret; 1691fa7d9493SBabu Moger 1692fa7d9493SBabu Moger /* create the directory */ 1693fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 1694fa7d9493SBabu Moger if (IS_ERR(kn)) 1695fa7d9493SBabu Moger return PTR_ERR(kn); 1696fa7d9493SBabu Moger 1697fa7d9493SBabu Moger if (dest_kn) 1698fa7d9493SBabu Moger *dest_kn = kn; 1699fa7d9493SBabu Moger 1700fa7d9493SBabu Moger /* 1701fa7d9493SBabu Moger * This extra ref will be put in kernfs_remove() and guarantees 1702fa7d9493SBabu Moger * that @rdtgrp->kn is always accessible. 1703fa7d9493SBabu Moger */ 1704fa7d9493SBabu Moger kernfs_get(kn); 1705fa7d9493SBabu Moger 1706fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 1707fa7d9493SBabu Moger if (ret) 1708fa7d9493SBabu Moger goto out_destroy; 1709fa7d9493SBabu Moger 1710fa7d9493SBabu Moger kernfs_activate(kn); 1711fa7d9493SBabu Moger 1712fa7d9493SBabu Moger return 0; 1713fa7d9493SBabu Moger 1714fa7d9493SBabu Moger out_destroy: 1715fa7d9493SBabu Moger kernfs_remove(kn); 1716fa7d9493SBabu Moger return ret; 1717fa7d9493SBabu Moger } 1718fa7d9493SBabu Moger 1719fa7d9493SBabu Moger static void l3_qos_cfg_update(void *arg) 1720fa7d9493SBabu Moger { 1721fa7d9493SBabu Moger bool *enable = arg; 1722fa7d9493SBabu Moger 1723aa50453aSBabu Moger wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL); 1724fa7d9493SBabu Moger } 1725fa7d9493SBabu Moger 1726fa7d9493SBabu Moger static void l2_qos_cfg_update(void *arg) 1727fa7d9493SBabu Moger { 1728fa7d9493SBabu Moger bool *enable = arg; 1729fa7d9493SBabu Moger 1730aa50453aSBabu Moger wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL); 1731fa7d9493SBabu Moger } 1732fa7d9493SBabu Moger 1733fa7d9493SBabu Moger static inline bool is_mba_linear(void) 1734fa7d9493SBabu Moger { 1735fa7d9493SBabu Moger return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear; 1736fa7d9493SBabu Moger } 1737fa7d9493SBabu Moger 1738fa7d9493SBabu Moger static int set_cache_qos_cfg(int level, bool enable) 1739fa7d9493SBabu Moger { 1740fa7d9493SBabu Moger void (*update)(void *arg); 1741fa7d9493SBabu Moger struct rdt_resource *r_l; 1742fa7d9493SBabu Moger cpumask_var_t cpu_mask; 1743fa7d9493SBabu Moger struct rdt_domain *d; 1744fa7d9493SBabu Moger int cpu; 1745fa7d9493SBabu Moger 1746fa7d9493SBabu Moger if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) 1747fa7d9493SBabu Moger return -ENOMEM; 1748fa7d9493SBabu Moger 1749fa7d9493SBabu Moger if (level == RDT_RESOURCE_L3) 1750fa7d9493SBabu Moger update = l3_qos_cfg_update; 1751fa7d9493SBabu Moger else if (level == RDT_RESOURCE_L2) 1752fa7d9493SBabu Moger update = l2_qos_cfg_update; 1753fa7d9493SBabu Moger else 1754fa7d9493SBabu Moger return -EINVAL; 1755fa7d9493SBabu Moger 1756fa7d9493SBabu Moger r_l = &rdt_resources_all[level]; 1757fa7d9493SBabu Moger list_for_each_entry(d, &r_l->domains, list) { 1758fa7d9493SBabu Moger /* Pick one CPU from each domain instance to update MSR */ 1759fa7d9493SBabu Moger cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); 1760fa7d9493SBabu Moger } 1761fa7d9493SBabu Moger cpu = get_cpu(); 1762fa7d9493SBabu Moger /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */ 1763fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 1764fa7d9493SBabu Moger update(&enable); 1765fa7d9493SBabu Moger /* Update QOS_CFG MSR on all other cpus in cpu_mask. */ 1766fa7d9493SBabu Moger smp_call_function_many(cpu_mask, update, &enable, 1); 1767fa7d9493SBabu Moger put_cpu(); 1768fa7d9493SBabu Moger 1769fa7d9493SBabu Moger free_cpumask_var(cpu_mask); 1770fa7d9493SBabu Moger 1771fa7d9493SBabu Moger return 0; 1772fa7d9493SBabu Moger } 1773fa7d9493SBabu Moger 1774fa7d9493SBabu Moger /* 1775fa7d9493SBabu Moger * Enable or disable the MBA software controller 1776fa7d9493SBabu Moger * which helps user specify bandwidth in MBps. 1777fa7d9493SBabu Moger * MBA software controller is supported only if 1778fa7d9493SBabu Moger * MBM is supported and MBA is in linear scale. 1779fa7d9493SBabu Moger */ 1780fa7d9493SBabu Moger static int set_mba_sc(bool mba_sc) 1781fa7d9493SBabu Moger { 1782fa7d9493SBabu Moger struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA]; 1783fa7d9493SBabu Moger struct rdt_domain *d; 1784fa7d9493SBabu Moger 1785fa7d9493SBabu Moger if (!is_mbm_enabled() || !is_mba_linear() || 1786fa7d9493SBabu Moger mba_sc == is_mba_sc(r)) 1787fa7d9493SBabu Moger return -EINVAL; 1788fa7d9493SBabu Moger 1789fa7d9493SBabu Moger r->membw.mba_sc = mba_sc; 1790fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) 1791fa7d9493SBabu Moger setup_default_ctrlval(r, d->ctrl_val, d->mbps_val); 1792fa7d9493SBabu Moger 1793fa7d9493SBabu Moger return 0; 1794fa7d9493SBabu Moger } 1795fa7d9493SBabu Moger 1796fa7d9493SBabu Moger static int cdp_enable(int level, int data_type, int code_type) 1797fa7d9493SBabu Moger { 1798fa7d9493SBabu Moger struct rdt_resource *r_ldata = &rdt_resources_all[data_type]; 1799fa7d9493SBabu Moger struct rdt_resource *r_lcode = &rdt_resources_all[code_type]; 1800fa7d9493SBabu Moger struct rdt_resource *r_l = &rdt_resources_all[level]; 1801fa7d9493SBabu Moger int ret; 1802fa7d9493SBabu Moger 1803fa7d9493SBabu Moger if (!r_l->alloc_capable || !r_ldata->alloc_capable || 1804fa7d9493SBabu Moger !r_lcode->alloc_capable) 1805fa7d9493SBabu Moger return -EINVAL; 1806fa7d9493SBabu Moger 1807fa7d9493SBabu Moger ret = set_cache_qos_cfg(level, true); 1808fa7d9493SBabu Moger if (!ret) { 1809fa7d9493SBabu Moger r_l->alloc_enabled = false; 1810fa7d9493SBabu Moger r_ldata->alloc_enabled = true; 1811fa7d9493SBabu Moger r_lcode->alloc_enabled = true; 1812fa7d9493SBabu Moger } 1813fa7d9493SBabu Moger return ret; 1814fa7d9493SBabu Moger } 1815fa7d9493SBabu Moger 1816fa7d9493SBabu Moger static int cdpl3_enable(void) 1817fa7d9493SBabu Moger { 1818fa7d9493SBabu Moger return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, 1819fa7d9493SBabu Moger RDT_RESOURCE_L3CODE); 1820fa7d9493SBabu Moger } 1821fa7d9493SBabu Moger 1822fa7d9493SBabu Moger static int cdpl2_enable(void) 1823fa7d9493SBabu Moger { 1824fa7d9493SBabu Moger return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, 1825fa7d9493SBabu Moger RDT_RESOURCE_L2CODE); 1826fa7d9493SBabu Moger } 1827fa7d9493SBabu Moger 1828fa7d9493SBabu Moger static void cdp_disable(int level, int data_type, int code_type) 1829fa7d9493SBabu Moger { 1830fa7d9493SBabu Moger struct rdt_resource *r = &rdt_resources_all[level]; 1831fa7d9493SBabu Moger 1832fa7d9493SBabu Moger r->alloc_enabled = r->alloc_capable; 1833fa7d9493SBabu Moger 1834fa7d9493SBabu Moger if (rdt_resources_all[data_type].alloc_enabled) { 1835fa7d9493SBabu Moger rdt_resources_all[data_type].alloc_enabled = false; 1836fa7d9493SBabu Moger rdt_resources_all[code_type].alloc_enabled = false; 1837fa7d9493SBabu Moger set_cache_qos_cfg(level, false); 1838fa7d9493SBabu Moger } 1839fa7d9493SBabu Moger } 1840fa7d9493SBabu Moger 1841fa7d9493SBabu Moger static void cdpl3_disable(void) 1842fa7d9493SBabu Moger { 1843fa7d9493SBabu Moger cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE); 1844fa7d9493SBabu Moger } 1845fa7d9493SBabu Moger 1846fa7d9493SBabu Moger static void cdpl2_disable(void) 1847fa7d9493SBabu Moger { 1848fa7d9493SBabu Moger cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE); 1849fa7d9493SBabu Moger } 1850fa7d9493SBabu Moger 1851fa7d9493SBabu Moger static void cdp_disable_all(void) 1852fa7d9493SBabu Moger { 1853fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) 1854fa7d9493SBabu Moger cdpl3_disable(); 1855fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) 1856fa7d9493SBabu Moger cdpl2_disable(); 1857fa7d9493SBabu Moger } 1858fa7d9493SBabu Moger 1859fa7d9493SBabu Moger /* 1860fa7d9493SBabu Moger * We don't allow rdtgroup directories to be created anywhere 1861fa7d9493SBabu Moger * except the root directory. Thus when looking for the rdtgroup 1862fa7d9493SBabu Moger * structure for a kernfs node we are either looking at a directory, 1863fa7d9493SBabu Moger * in which case the rdtgroup structure is pointed at by the "priv" 1864fa7d9493SBabu Moger * field, otherwise we have a file, and need only look to the parent 1865fa7d9493SBabu Moger * to find the rdtgroup. 1866fa7d9493SBabu Moger */ 1867fa7d9493SBabu Moger static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn) 1868fa7d9493SBabu Moger { 1869fa7d9493SBabu Moger if (kernfs_type(kn) == KERNFS_DIR) { 1870fa7d9493SBabu Moger /* 1871fa7d9493SBabu Moger * All the resource directories use "kn->priv" 1872fa7d9493SBabu Moger * to point to the "struct rdtgroup" for the 1873fa7d9493SBabu Moger * resource. "info" and its subdirectories don't 1874fa7d9493SBabu Moger * have rdtgroup structures, so return NULL here. 1875fa7d9493SBabu Moger */ 1876fa7d9493SBabu Moger if (kn == kn_info || kn->parent == kn_info) 1877fa7d9493SBabu Moger return NULL; 1878fa7d9493SBabu Moger else 1879fa7d9493SBabu Moger return kn->priv; 1880fa7d9493SBabu Moger } else { 1881fa7d9493SBabu Moger return kn->parent->priv; 1882fa7d9493SBabu Moger } 1883fa7d9493SBabu Moger } 1884fa7d9493SBabu Moger 1885fa7d9493SBabu Moger struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn) 1886fa7d9493SBabu Moger { 1887fa7d9493SBabu Moger struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 1888fa7d9493SBabu Moger 1889fa7d9493SBabu Moger if (!rdtgrp) 1890fa7d9493SBabu Moger return NULL; 1891fa7d9493SBabu Moger 1892fa7d9493SBabu Moger atomic_inc(&rdtgrp->waitcount); 1893fa7d9493SBabu Moger kernfs_break_active_protection(kn); 1894fa7d9493SBabu Moger 1895fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 1896fa7d9493SBabu Moger 1897fa7d9493SBabu Moger /* Was this group deleted while we waited? */ 1898fa7d9493SBabu Moger if (rdtgrp->flags & RDT_DELETED) 1899fa7d9493SBabu Moger return NULL; 1900fa7d9493SBabu Moger 1901fa7d9493SBabu Moger return rdtgrp; 1902fa7d9493SBabu Moger } 1903fa7d9493SBabu Moger 1904fa7d9493SBabu Moger void rdtgroup_kn_unlock(struct kernfs_node *kn) 1905fa7d9493SBabu Moger { 1906fa7d9493SBabu Moger struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); 1907fa7d9493SBabu Moger 1908fa7d9493SBabu Moger if (!rdtgrp) 1909fa7d9493SBabu Moger return; 1910fa7d9493SBabu Moger 1911fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 1912fa7d9493SBabu Moger 1913fa7d9493SBabu Moger if (atomic_dec_and_test(&rdtgrp->waitcount) && 1914fa7d9493SBabu Moger (rdtgrp->flags & RDT_DELETED)) { 1915fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 1916fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 1917fa7d9493SBabu Moger rdtgroup_pseudo_lock_remove(rdtgrp); 1918fa7d9493SBabu Moger kernfs_unbreak_active_protection(kn); 1919fa7d9493SBabu Moger kernfs_put(rdtgrp->kn); 1920fa7d9493SBabu Moger kfree(rdtgrp); 1921fa7d9493SBabu Moger } else { 1922fa7d9493SBabu Moger kernfs_unbreak_active_protection(kn); 1923fa7d9493SBabu Moger } 1924fa7d9493SBabu Moger } 1925fa7d9493SBabu Moger 1926fa7d9493SBabu Moger static int mkdir_mondata_all(struct kernfs_node *parent_kn, 1927fa7d9493SBabu Moger struct rdtgroup *prgrp, 1928fa7d9493SBabu Moger struct kernfs_node **mon_data_kn); 1929fa7d9493SBabu Moger 193023bf1b6bSDavid Howells static int rdt_enable_ctx(struct rdt_fs_context *ctx) 1931fa7d9493SBabu Moger { 193223bf1b6bSDavid Howells int ret = 0; 193323bf1b6bSDavid Howells 193423bf1b6bSDavid Howells if (ctx->enable_cdpl2) 193523bf1b6bSDavid Howells ret = cdpl2_enable(); 193623bf1b6bSDavid Howells 193723bf1b6bSDavid Howells if (!ret && ctx->enable_cdpl3) 193823bf1b6bSDavid Howells ret = cdpl3_enable(); 193923bf1b6bSDavid Howells 194023bf1b6bSDavid Howells if (!ret && ctx->enable_mba_mbps) 194123bf1b6bSDavid Howells ret = set_mba_sc(true); 194223bf1b6bSDavid Howells 194323bf1b6bSDavid Howells return ret; 194423bf1b6bSDavid Howells } 194523bf1b6bSDavid Howells 194623bf1b6bSDavid Howells static int rdt_get_tree(struct fs_context *fc) 194723bf1b6bSDavid Howells { 194823bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 1949fa7d9493SBabu Moger struct rdt_domain *dom; 1950fa7d9493SBabu Moger struct rdt_resource *r; 1951fa7d9493SBabu Moger int ret; 1952fa7d9493SBabu Moger 1953fa7d9493SBabu Moger cpus_read_lock(); 1954fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 1955fa7d9493SBabu Moger /* 1956fa7d9493SBabu Moger * resctrl file system can only be mounted once. 1957fa7d9493SBabu Moger */ 1958fa7d9493SBabu Moger if (static_branch_unlikely(&rdt_enable_key)) { 195923bf1b6bSDavid Howells ret = -EBUSY; 1960fa7d9493SBabu Moger goto out; 1961fa7d9493SBabu Moger } 1962fa7d9493SBabu Moger 196323bf1b6bSDavid Howells ret = rdt_enable_ctx(ctx); 196423bf1b6bSDavid Howells if (ret < 0) 1965fa7d9493SBabu Moger goto out_cdp; 1966fa7d9493SBabu Moger 1967fa7d9493SBabu Moger closid_init(); 1968fa7d9493SBabu Moger 1969fa7d9493SBabu Moger ret = rdtgroup_create_info_dir(rdtgroup_default.kn); 197023bf1b6bSDavid Howells if (ret < 0) 197123bf1b6bSDavid Howells goto out_mba; 1972fa7d9493SBabu Moger 1973fa7d9493SBabu Moger if (rdt_mon_capable) { 1974fa7d9493SBabu Moger ret = mongroup_create_dir(rdtgroup_default.kn, 1975fa7d9493SBabu Moger NULL, "mon_groups", 1976fa7d9493SBabu Moger &kn_mongrp); 197723bf1b6bSDavid Howells if (ret < 0) 1978fa7d9493SBabu Moger goto out_info; 1979fa7d9493SBabu Moger kernfs_get(kn_mongrp); 1980fa7d9493SBabu Moger 1981fa7d9493SBabu Moger ret = mkdir_mondata_all(rdtgroup_default.kn, 1982fa7d9493SBabu Moger &rdtgroup_default, &kn_mondata); 198323bf1b6bSDavid Howells if (ret < 0) 1984fa7d9493SBabu Moger goto out_mongrp; 1985fa7d9493SBabu Moger kernfs_get(kn_mondata); 1986fa7d9493SBabu Moger rdtgroup_default.mon.mon_data_kn = kn_mondata; 1987fa7d9493SBabu Moger } 1988fa7d9493SBabu Moger 1989fa7d9493SBabu Moger ret = rdt_pseudo_lock_init(); 199023bf1b6bSDavid Howells if (ret) 1991fa7d9493SBabu Moger goto out_mondata; 1992fa7d9493SBabu Moger 199323bf1b6bSDavid Howells ret = kernfs_get_tree(fc); 199423bf1b6bSDavid Howells if (ret < 0) 1995fa7d9493SBabu Moger goto out_psl; 1996fa7d9493SBabu Moger 1997fa7d9493SBabu Moger if (rdt_alloc_capable) 1998fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_alloc_enable_key); 1999fa7d9493SBabu Moger if (rdt_mon_capable) 2000fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_mon_enable_key); 2001fa7d9493SBabu Moger 2002fa7d9493SBabu Moger if (rdt_alloc_capable || rdt_mon_capable) 2003fa7d9493SBabu Moger static_branch_enable_cpuslocked(&rdt_enable_key); 2004fa7d9493SBabu Moger 2005fa7d9493SBabu Moger if (is_mbm_enabled()) { 2006fa7d9493SBabu Moger r = &rdt_resources_all[RDT_RESOURCE_L3]; 2007fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) 2008fa7d9493SBabu Moger mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL); 2009fa7d9493SBabu Moger } 2010fa7d9493SBabu Moger 2011fa7d9493SBabu Moger goto out; 2012fa7d9493SBabu Moger 2013fa7d9493SBabu Moger out_psl: 2014fa7d9493SBabu Moger rdt_pseudo_lock_release(); 2015fa7d9493SBabu Moger out_mondata: 2016fa7d9493SBabu Moger if (rdt_mon_capable) 2017fa7d9493SBabu Moger kernfs_remove(kn_mondata); 2018fa7d9493SBabu Moger out_mongrp: 2019fa7d9493SBabu Moger if (rdt_mon_capable) 2020fa7d9493SBabu Moger kernfs_remove(kn_mongrp); 2021fa7d9493SBabu Moger out_info: 2022fa7d9493SBabu Moger kernfs_remove(kn_info); 202323bf1b6bSDavid Howells out_mba: 202423bf1b6bSDavid Howells if (ctx->enable_mba_mbps) 202523bf1b6bSDavid Howells set_mba_sc(false); 2026fa7d9493SBabu Moger out_cdp: 2027fa7d9493SBabu Moger cdp_disable_all(); 2028fa7d9493SBabu Moger out: 2029fa7d9493SBabu Moger rdt_last_cmd_clear(); 2030fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2031fa7d9493SBabu Moger cpus_read_unlock(); 203223bf1b6bSDavid Howells return ret; 203323bf1b6bSDavid Howells } 2034fa7d9493SBabu Moger 203523bf1b6bSDavid Howells enum rdt_param { 203623bf1b6bSDavid Howells Opt_cdp, 203723bf1b6bSDavid Howells Opt_cdpl2, 2038faa3604eSXiaochen Shen Opt_mba_mbps, 203923bf1b6bSDavid Howells nr__rdt_params 204023bf1b6bSDavid Howells }; 204123bf1b6bSDavid Howells 204223bf1b6bSDavid Howells static const struct fs_parameter_spec rdt_param_specs[] = { 204323bf1b6bSDavid Howells fsparam_flag("cdp", Opt_cdp), 204423bf1b6bSDavid Howells fsparam_flag("cdpl2", Opt_cdpl2), 2045faa3604eSXiaochen Shen fsparam_flag("mba_MBps", Opt_mba_mbps), 204623bf1b6bSDavid Howells {} 204723bf1b6bSDavid Howells }; 204823bf1b6bSDavid Howells 204923bf1b6bSDavid Howells static const struct fs_parameter_description rdt_fs_parameters = { 205023bf1b6bSDavid Howells .name = "rdt", 205123bf1b6bSDavid Howells .specs = rdt_param_specs, 205223bf1b6bSDavid Howells }; 205323bf1b6bSDavid Howells 205423bf1b6bSDavid Howells static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param) 205523bf1b6bSDavid Howells { 205623bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 205723bf1b6bSDavid Howells struct fs_parse_result result; 205823bf1b6bSDavid Howells int opt; 205923bf1b6bSDavid Howells 206023bf1b6bSDavid Howells opt = fs_parse(fc, &rdt_fs_parameters, param, &result); 206123bf1b6bSDavid Howells if (opt < 0) 206223bf1b6bSDavid Howells return opt; 206323bf1b6bSDavid Howells 206423bf1b6bSDavid Howells switch (opt) { 206523bf1b6bSDavid Howells case Opt_cdp: 206623bf1b6bSDavid Howells ctx->enable_cdpl3 = true; 206723bf1b6bSDavid Howells return 0; 206823bf1b6bSDavid Howells case Opt_cdpl2: 206923bf1b6bSDavid Howells ctx->enable_cdpl2 = true; 207023bf1b6bSDavid Howells return 0; 2071faa3604eSXiaochen Shen case Opt_mba_mbps: 207223bf1b6bSDavid Howells if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 207323bf1b6bSDavid Howells return -EINVAL; 207423bf1b6bSDavid Howells ctx->enable_mba_mbps = true; 207523bf1b6bSDavid Howells return 0; 207623bf1b6bSDavid Howells } 207723bf1b6bSDavid Howells 207823bf1b6bSDavid Howells return -EINVAL; 207923bf1b6bSDavid Howells } 208023bf1b6bSDavid Howells 208123bf1b6bSDavid Howells static void rdt_fs_context_free(struct fs_context *fc) 208223bf1b6bSDavid Howells { 208323bf1b6bSDavid Howells struct rdt_fs_context *ctx = rdt_fc2context(fc); 208423bf1b6bSDavid Howells 208523bf1b6bSDavid Howells kernfs_free_fs_context(fc); 208623bf1b6bSDavid Howells kfree(ctx); 208723bf1b6bSDavid Howells } 208823bf1b6bSDavid Howells 208923bf1b6bSDavid Howells static const struct fs_context_operations rdt_fs_context_ops = { 209023bf1b6bSDavid Howells .free = rdt_fs_context_free, 209123bf1b6bSDavid Howells .parse_param = rdt_parse_param, 209223bf1b6bSDavid Howells .get_tree = rdt_get_tree, 209323bf1b6bSDavid Howells }; 209423bf1b6bSDavid Howells 209523bf1b6bSDavid Howells static int rdt_init_fs_context(struct fs_context *fc) 209623bf1b6bSDavid Howells { 209723bf1b6bSDavid Howells struct rdt_fs_context *ctx; 209823bf1b6bSDavid Howells 209923bf1b6bSDavid Howells ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL); 210023bf1b6bSDavid Howells if (!ctx) 210123bf1b6bSDavid Howells return -ENOMEM; 210223bf1b6bSDavid Howells 210323bf1b6bSDavid Howells ctx->kfc.root = rdt_root; 210423bf1b6bSDavid Howells ctx->kfc.magic = RDTGROUP_SUPER_MAGIC; 210523bf1b6bSDavid Howells fc->fs_private = &ctx->kfc; 210623bf1b6bSDavid Howells fc->ops = &rdt_fs_context_ops; 210723bf1b6bSDavid Howells if (fc->user_ns) 210823bf1b6bSDavid Howells put_user_ns(fc->user_ns); 210923bf1b6bSDavid Howells fc->user_ns = get_user_ns(&init_user_ns); 211023bf1b6bSDavid Howells fc->global = true; 211123bf1b6bSDavid Howells return 0; 2112fa7d9493SBabu Moger } 2113fa7d9493SBabu Moger 2114fa7d9493SBabu Moger static int reset_all_ctrls(struct rdt_resource *r) 2115fa7d9493SBabu Moger { 2116fa7d9493SBabu Moger struct msr_param msr_param; 2117fa7d9493SBabu Moger cpumask_var_t cpu_mask; 2118fa7d9493SBabu Moger struct rdt_domain *d; 2119fa7d9493SBabu Moger int i, cpu; 2120fa7d9493SBabu Moger 2121fa7d9493SBabu Moger if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) 2122fa7d9493SBabu Moger return -ENOMEM; 2123fa7d9493SBabu Moger 2124fa7d9493SBabu Moger msr_param.res = r; 2125fa7d9493SBabu Moger msr_param.low = 0; 2126fa7d9493SBabu Moger msr_param.high = r->num_closid; 2127fa7d9493SBabu Moger 2128fa7d9493SBabu Moger /* 2129fa7d9493SBabu Moger * Disable resource control for this resource by setting all 2130fa7d9493SBabu Moger * CBMs in all domains to the maximum mask value. Pick one CPU 2131fa7d9493SBabu Moger * from each domain to update the MSRs below. 2132fa7d9493SBabu Moger */ 2133fa7d9493SBabu Moger list_for_each_entry(d, &r->domains, list) { 2134fa7d9493SBabu Moger cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); 2135fa7d9493SBabu Moger 2136fa7d9493SBabu Moger for (i = 0; i < r->num_closid; i++) 2137fa7d9493SBabu Moger d->ctrl_val[i] = r->default_ctrl; 2138fa7d9493SBabu Moger } 2139fa7d9493SBabu Moger cpu = get_cpu(); 2140fa7d9493SBabu Moger /* Update CBM on this cpu if it's in cpu_mask. */ 2141fa7d9493SBabu Moger if (cpumask_test_cpu(cpu, cpu_mask)) 2142fa7d9493SBabu Moger rdt_ctrl_update(&msr_param); 2143fa7d9493SBabu Moger /* Update CBM on all other cpus in cpu_mask. */ 2144fa7d9493SBabu Moger smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1); 2145fa7d9493SBabu Moger put_cpu(); 2146fa7d9493SBabu Moger 2147fa7d9493SBabu Moger free_cpumask_var(cpu_mask); 2148fa7d9493SBabu Moger 2149fa7d9493SBabu Moger return 0; 2150fa7d9493SBabu Moger } 2151fa7d9493SBabu Moger 2152fa7d9493SBabu Moger static bool is_closid_match(struct task_struct *t, struct rdtgroup *r) 2153fa7d9493SBabu Moger { 2154fa7d9493SBabu Moger return (rdt_alloc_capable && 2155fa7d9493SBabu Moger (r->type == RDTCTRL_GROUP) && (t->closid == r->closid)); 2156fa7d9493SBabu Moger } 2157fa7d9493SBabu Moger 2158fa7d9493SBabu Moger static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r) 2159fa7d9493SBabu Moger { 2160fa7d9493SBabu Moger return (rdt_mon_capable && 2161fa7d9493SBabu Moger (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid)); 2162fa7d9493SBabu Moger } 2163fa7d9493SBabu Moger 2164fa7d9493SBabu Moger /* 2165fa7d9493SBabu Moger * Move tasks from one to the other group. If @from is NULL, then all tasks 2166fa7d9493SBabu Moger * in the systems are moved unconditionally (used for teardown). 2167fa7d9493SBabu Moger * 2168fa7d9493SBabu Moger * If @mask is not NULL the cpus on which moved tasks are running are set 2169fa7d9493SBabu Moger * in that mask so the update smp function call is restricted to affected 2170fa7d9493SBabu Moger * cpus. 2171fa7d9493SBabu Moger */ 2172fa7d9493SBabu Moger static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, 2173fa7d9493SBabu Moger struct cpumask *mask) 2174fa7d9493SBabu Moger { 2175fa7d9493SBabu Moger struct task_struct *p, *t; 2176fa7d9493SBabu Moger 2177fa7d9493SBabu Moger read_lock(&tasklist_lock); 2178fa7d9493SBabu Moger for_each_process_thread(p, t) { 2179fa7d9493SBabu Moger if (!from || is_closid_match(t, from) || 2180fa7d9493SBabu Moger is_rmid_match(t, from)) { 2181fa7d9493SBabu Moger t->closid = to->closid; 2182fa7d9493SBabu Moger t->rmid = to->mon.rmid; 2183fa7d9493SBabu Moger 2184fa7d9493SBabu Moger #ifdef CONFIG_SMP 2185fa7d9493SBabu Moger /* 2186fa7d9493SBabu Moger * This is safe on x86 w/o barriers as the ordering 2187fa7d9493SBabu Moger * of writing to task_cpu() and t->on_cpu is 2188fa7d9493SBabu Moger * reverse to the reading here. The detection is 2189fa7d9493SBabu Moger * inaccurate as tasks might move or schedule 2190fa7d9493SBabu Moger * before the smp function call takes place. In 2191fa7d9493SBabu Moger * such a case the function call is pointless, but 2192fa7d9493SBabu Moger * there is no other side effect. 2193fa7d9493SBabu Moger */ 2194fa7d9493SBabu Moger if (mask && t->on_cpu) 2195fa7d9493SBabu Moger cpumask_set_cpu(task_cpu(t), mask); 2196fa7d9493SBabu Moger #endif 2197fa7d9493SBabu Moger } 2198fa7d9493SBabu Moger } 2199fa7d9493SBabu Moger read_unlock(&tasklist_lock); 2200fa7d9493SBabu Moger } 2201fa7d9493SBabu Moger 2202fa7d9493SBabu Moger static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp) 2203fa7d9493SBabu Moger { 2204fa7d9493SBabu Moger struct rdtgroup *sentry, *stmp; 2205fa7d9493SBabu Moger struct list_head *head; 2206fa7d9493SBabu Moger 2207fa7d9493SBabu Moger head = &rdtgrp->mon.crdtgrp_list; 2208fa7d9493SBabu Moger list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) { 2209fa7d9493SBabu Moger free_rmid(sentry->mon.rmid); 2210fa7d9493SBabu Moger list_del(&sentry->mon.crdtgrp_list); 2211fa7d9493SBabu Moger kfree(sentry); 2212fa7d9493SBabu Moger } 2213fa7d9493SBabu Moger } 2214fa7d9493SBabu Moger 2215fa7d9493SBabu Moger /* 2216fa7d9493SBabu Moger * Forcibly remove all of subdirectories under root. 2217fa7d9493SBabu Moger */ 2218fa7d9493SBabu Moger static void rmdir_all_sub(void) 2219fa7d9493SBabu Moger { 2220fa7d9493SBabu Moger struct rdtgroup *rdtgrp, *tmp; 2221fa7d9493SBabu Moger 2222fa7d9493SBabu Moger /* Move all tasks to the default resource group */ 2223fa7d9493SBabu Moger rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); 2224fa7d9493SBabu Moger 2225fa7d9493SBabu Moger list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { 2226fa7d9493SBabu Moger /* Free any child rmids */ 2227fa7d9493SBabu Moger free_all_child_rdtgrp(rdtgrp); 2228fa7d9493SBabu Moger 2229fa7d9493SBabu Moger /* Remove each rdtgroup other than root */ 2230fa7d9493SBabu Moger if (rdtgrp == &rdtgroup_default) 2231fa7d9493SBabu Moger continue; 2232fa7d9493SBabu Moger 2233fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2234fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) 2235fa7d9493SBabu Moger rdtgroup_pseudo_lock_remove(rdtgrp); 2236fa7d9493SBabu Moger 2237fa7d9493SBabu Moger /* 2238fa7d9493SBabu Moger * Give any CPUs back to the default group. We cannot copy 2239fa7d9493SBabu Moger * cpu_online_mask because a CPU might have executed the 2240fa7d9493SBabu Moger * offline callback already, but is still marked online. 2241fa7d9493SBabu Moger */ 2242fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 2243fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 2244fa7d9493SBabu Moger 2245fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2246fa7d9493SBabu Moger 2247fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2248fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 2249fa7d9493SBabu Moger kfree(rdtgrp); 2250fa7d9493SBabu Moger } 2251fa7d9493SBabu Moger /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ 2252fa7d9493SBabu Moger update_closid_rmid(cpu_online_mask, &rdtgroup_default); 2253fa7d9493SBabu Moger 2254fa7d9493SBabu Moger kernfs_remove(kn_info); 2255fa7d9493SBabu Moger kernfs_remove(kn_mongrp); 2256fa7d9493SBabu Moger kernfs_remove(kn_mondata); 2257fa7d9493SBabu Moger } 2258fa7d9493SBabu Moger 2259fa7d9493SBabu Moger static void rdt_kill_sb(struct super_block *sb) 2260fa7d9493SBabu Moger { 2261fa7d9493SBabu Moger struct rdt_resource *r; 2262fa7d9493SBabu Moger 2263fa7d9493SBabu Moger cpus_read_lock(); 2264fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 2265fa7d9493SBabu Moger 2266fa7d9493SBabu Moger set_mba_sc(false); 2267fa7d9493SBabu Moger 2268fa7d9493SBabu Moger /*Put everything back to default values. */ 2269fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) 2270fa7d9493SBabu Moger reset_all_ctrls(r); 2271fa7d9493SBabu Moger cdp_disable_all(); 2272fa7d9493SBabu Moger rmdir_all_sub(); 2273fa7d9493SBabu Moger rdt_pseudo_lock_release(); 2274fa7d9493SBabu Moger rdtgroup_default.mode = RDT_MODE_SHAREABLE; 2275fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_alloc_enable_key); 2276fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_mon_enable_key); 2277fa7d9493SBabu Moger static_branch_disable_cpuslocked(&rdt_enable_key); 2278fa7d9493SBabu Moger kernfs_kill_sb(sb); 2279fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 2280fa7d9493SBabu Moger cpus_read_unlock(); 2281fa7d9493SBabu Moger } 2282fa7d9493SBabu Moger 2283fa7d9493SBabu Moger static struct file_system_type rdt_fs_type = { 2284fa7d9493SBabu Moger .name = "resctrl", 228523bf1b6bSDavid Howells .init_fs_context = rdt_init_fs_context, 228623bf1b6bSDavid Howells .parameters = &rdt_fs_parameters, 2287fa7d9493SBabu Moger .kill_sb = rdt_kill_sb, 2288fa7d9493SBabu Moger }; 2289fa7d9493SBabu Moger 2290fa7d9493SBabu Moger static int mon_addfile(struct kernfs_node *parent_kn, const char *name, 2291fa7d9493SBabu Moger void *priv) 2292fa7d9493SBabu Moger { 2293fa7d9493SBabu Moger struct kernfs_node *kn; 2294fa7d9493SBabu Moger int ret = 0; 2295fa7d9493SBabu Moger 2296fa7d9493SBabu Moger kn = __kernfs_create_file(parent_kn, name, 0444, 2297fa7d9493SBabu Moger GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0, 2298fa7d9493SBabu Moger &kf_mondata_ops, priv, NULL, NULL); 2299fa7d9493SBabu Moger if (IS_ERR(kn)) 2300fa7d9493SBabu Moger return PTR_ERR(kn); 2301fa7d9493SBabu Moger 2302fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2303fa7d9493SBabu Moger if (ret) { 2304fa7d9493SBabu Moger kernfs_remove(kn); 2305fa7d9493SBabu Moger return ret; 2306fa7d9493SBabu Moger } 2307fa7d9493SBabu Moger 2308fa7d9493SBabu Moger return ret; 2309fa7d9493SBabu Moger } 2310fa7d9493SBabu Moger 2311fa7d9493SBabu Moger /* 2312fa7d9493SBabu Moger * Remove all subdirectories of mon_data of ctrl_mon groups 2313fa7d9493SBabu Moger * and monitor groups with given domain id. 2314fa7d9493SBabu Moger */ 2315fa7d9493SBabu Moger void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id) 2316fa7d9493SBabu Moger { 2317fa7d9493SBabu Moger struct rdtgroup *prgrp, *crgrp; 2318fa7d9493SBabu Moger char name[32]; 2319fa7d9493SBabu Moger 2320fa7d9493SBabu Moger if (!r->mon_enabled) 2321fa7d9493SBabu Moger return; 2322fa7d9493SBabu Moger 2323fa7d9493SBabu Moger list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 2324fa7d9493SBabu Moger sprintf(name, "mon_%s_%02d", r->name, dom_id); 2325fa7d9493SBabu Moger kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); 2326fa7d9493SBabu Moger 2327fa7d9493SBabu Moger list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) 2328fa7d9493SBabu Moger kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); 2329fa7d9493SBabu Moger } 2330fa7d9493SBabu Moger } 2331fa7d9493SBabu Moger 2332fa7d9493SBabu Moger static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, 2333fa7d9493SBabu Moger struct rdt_domain *d, 2334fa7d9493SBabu Moger struct rdt_resource *r, struct rdtgroup *prgrp) 2335fa7d9493SBabu Moger { 2336fa7d9493SBabu Moger union mon_data_bits priv; 2337fa7d9493SBabu Moger struct kernfs_node *kn; 2338fa7d9493SBabu Moger struct mon_evt *mevt; 2339fa7d9493SBabu Moger struct rmid_read rr; 2340fa7d9493SBabu Moger char name[32]; 2341fa7d9493SBabu Moger int ret; 2342fa7d9493SBabu Moger 2343fa7d9493SBabu Moger sprintf(name, "mon_%s_%02d", r->name, d->id); 2344fa7d9493SBabu Moger /* create the directory */ 2345fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); 2346fa7d9493SBabu Moger if (IS_ERR(kn)) 2347fa7d9493SBabu Moger return PTR_ERR(kn); 2348fa7d9493SBabu Moger 2349fa7d9493SBabu Moger /* 2350fa7d9493SBabu Moger * This extra ref will be put in kernfs_remove() and guarantees 2351fa7d9493SBabu Moger * that kn is always accessible. 2352fa7d9493SBabu Moger */ 2353fa7d9493SBabu Moger kernfs_get(kn); 2354fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2355fa7d9493SBabu Moger if (ret) 2356fa7d9493SBabu Moger goto out_destroy; 2357fa7d9493SBabu Moger 2358fa7d9493SBabu Moger if (WARN_ON(list_empty(&r->evt_list))) { 2359fa7d9493SBabu Moger ret = -EPERM; 2360fa7d9493SBabu Moger goto out_destroy; 2361fa7d9493SBabu Moger } 2362fa7d9493SBabu Moger 2363fa7d9493SBabu Moger priv.u.rid = r->rid; 2364fa7d9493SBabu Moger priv.u.domid = d->id; 2365fa7d9493SBabu Moger list_for_each_entry(mevt, &r->evt_list, list) { 2366fa7d9493SBabu Moger priv.u.evtid = mevt->evtid; 2367fa7d9493SBabu Moger ret = mon_addfile(kn, mevt->name, priv.priv); 2368fa7d9493SBabu Moger if (ret) 2369fa7d9493SBabu Moger goto out_destroy; 2370fa7d9493SBabu Moger 2371fa7d9493SBabu Moger if (is_mbm_event(mevt->evtid)) 2372fa7d9493SBabu Moger mon_event_read(&rr, d, prgrp, mevt->evtid, true); 2373fa7d9493SBabu Moger } 2374fa7d9493SBabu Moger kernfs_activate(kn); 2375fa7d9493SBabu Moger return 0; 2376fa7d9493SBabu Moger 2377fa7d9493SBabu Moger out_destroy: 2378fa7d9493SBabu Moger kernfs_remove(kn); 2379fa7d9493SBabu Moger return ret; 2380fa7d9493SBabu Moger } 2381fa7d9493SBabu Moger 2382fa7d9493SBabu Moger /* 2383fa7d9493SBabu Moger * Add all subdirectories of mon_data for "ctrl_mon" groups 2384fa7d9493SBabu Moger * and "monitor" groups with given domain id. 2385fa7d9493SBabu Moger */ 2386fa7d9493SBabu Moger void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 2387fa7d9493SBabu Moger struct rdt_domain *d) 2388fa7d9493SBabu Moger { 2389fa7d9493SBabu Moger struct kernfs_node *parent_kn; 2390fa7d9493SBabu Moger struct rdtgroup *prgrp, *crgrp; 2391fa7d9493SBabu Moger struct list_head *head; 2392fa7d9493SBabu Moger 2393fa7d9493SBabu Moger if (!r->mon_enabled) 2394fa7d9493SBabu Moger return; 2395fa7d9493SBabu Moger 2396fa7d9493SBabu Moger list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { 2397fa7d9493SBabu Moger parent_kn = prgrp->mon.mon_data_kn; 2398fa7d9493SBabu Moger mkdir_mondata_subdir(parent_kn, d, r, prgrp); 2399fa7d9493SBabu Moger 2400fa7d9493SBabu Moger head = &prgrp->mon.crdtgrp_list; 2401fa7d9493SBabu Moger list_for_each_entry(crgrp, head, mon.crdtgrp_list) { 2402fa7d9493SBabu Moger parent_kn = crgrp->mon.mon_data_kn; 2403fa7d9493SBabu Moger mkdir_mondata_subdir(parent_kn, d, r, crgrp); 2404fa7d9493SBabu Moger } 2405fa7d9493SBabu Moger } 2406fa7d9493SBabu Moger } 2407fa7d9493SBabu Moger 2408fa7d9493SBabu Moger static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, 2409fa7d9493SBabu Moger struct rdt_resource *r, 2410fa7d9493SBabu Moger struct rdtgroup *prgrp) 2411fa7d9493SBabu Moger { 2412fa7d9493SBabu Moger struct rdt_domain *dom; 2413fa7d9493SBabu Moger int ret; 2414fa7d9493SBabu Moger 2415fa7d9493SBabu Moger list_for_each_entry(dom, &r->domains, list) { 2416fa7d9493SBabu Moger ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp); 2417fa7d9493SBabu Moger if (ret) 2418fa7d9493SBabu Moger return ret; 2419fa7d9493SBabu Moger } 2420fa7d9493SBabu Moger 2421fa7d9493SBabu Moger return 0; 2422fa7d9493SBabu Moger } 2423fa7d9493SBabu Moger 2424fa7d9493SBabu Moger /* 2425fa7d9493SBabu Moger * This creates a directory mon_data which contains the monitored data. 2426fa7d9493SBabu Moger * 2427fa7d9493SBabu Moger * mon_data has one directory for each domain whic are named 2428fa7d9493SBabu Moger * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data 2429fa7d9493SBabu Moger * with L3 domain looks as below: 2430fa7d9493SBabu Moger * ./mon_data: 2431fa7d9493SBabu Moger * mon_L3_00 2432fa7d9493SBabu Moger * mon_L3_01 2433fa7d9493SBabu Moger * mon_L3_02 2434fa7d9493SBabu Moger * ... 2435fa7d9493SBabu Moger * 2436fa7d9493SBabu Moger * Each domain directory has one file per event: 2437fa7d9493SBabu Moger * ./mon_L3_00/: 2438fa7d9493SBabu Moger * llc_occupancy 2439fa7d9493SBabu Moger * 2440fa7d9493SBabu Moger */ 2441fa7d9493SBabu Moger static int mkdir_mondata_all(struct kernfs_node *parent_kn, 2442fa7d9493SBabu Moger struct rdtgroup *prgrp, 2443fa7d9493SBabu Moger struct kernfs_node **dest_kn) 2444fa7d9493SBabu Moger { 2445fa7d9493SBabu Moger struct rdt_resource *r; 2446fa7d9493SBabu Moger struct kernfs_node *kn; 2447fa7d9493SBabu Moger int ret; 2448fa7d9493SBabu Moger 2449fa7d9493SBabu Moger /* 2450fa7d9493SBabu Moger * Create the mon_data directory first. 2451fa7d9493SBabu Moger */ 2452fa7d9493SBabu Moger ret = mongroup_create_dir(parent_kn, NULL, "mon_data", &kn); 2453fa7d9493SBabu Moger if (ret) 2454fa7d9493SBabu Moger return ret; 2455fa7d9493SBabu Moger 2456fa7d9493SBabu Moger if (dest_kn) 2457fa7d9493SBabu Moger *dest_kn = kn; 2458fa7d9493SBabu Moger 2459fa7d9493SBabu Moger /* 2460fa7d9493SBabu Moger * Create the subdirectories for each domain. Note that all events 2461fa7d9493SBabu Moger * in a domain like L3 are grouped into a resource whose domain is L3 2462fa7d9493SBabu Moger */ 2463fa7d9493SBabu Moger for_each_mon_enabled_rdt_resource(r) { 2464fa7d9493SBabu Moger ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); 2465fa7d9493SBabu Moger if (ret) 2466fa7d9493SBabu Moger goto out_destroy; 2467fa7d9493SBabu Moger } 2468fa7d9493SBabu Moger 2469fa7d9493SBabu Moger return 0; 2470fa7d9493SBabu Moger 2471fa7d9493SBabu Moger out_destroy: 2472fa7d9493SBabu Moger kernfs_remove(kn); 2473fa7d9493SBabu Moger return ret; 2474fa7d9493SBabu Moger } 2475fa7d9493SBabu Moger 2476fa7d9493SBabu Moger /** 2477fa7d9493SBabu Moger * cbm_ensure_valid - Enforce validity on provided CBM 2478fa7d9493SBabu Moger * @_val: Candidate CBM 2479fa7d9493SBabu Moger * @r: RDT resource to which the CBM belongs 2480fa7d9493SBabu Moger * 2481fa7d9493SBabu Moger * The provided CBM represents all cache portions available for use. This 2482fa7d9493SBabu Moger * may be represented by a bitmap that does not consist of contiguous ones 2483fa7d9493SBabu Moger * and thus be an invalid CBM. 2484fa7d9493SBabu Moger * Here the provided CBM is forced to be a valid CBM by only considering 2485fa7d9493SBabu Moger * the first set of contiguous bits as valid and clearing all bits. 2486fa7d9493SBabu Moger * The intention here is to provide a valid default CBM with which a new 2487fa7d9493SBabu Moger * resource group is initialized. The user can follow this with a 2488fa7d9493SBabu Moger * modification to the CBM if the default does not satisfy the 2489fa7d9493SBabu Moger * requirements. 2490fa7d9493SBabu Moger */ 2491*2ef085bdSReinette Chatre static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r) 2492fa7d9493SBabu Moger { 2493fa7d9493SBabu Moger unsigned int cbm_len = r->cache.cbm_len; 2494fa7d9493SBabu Moger unsigned long first_bit, zero_bit; 2495*2ef085bdSReinette Chatre unsigned long val = _val; 2496fa7d9493SBabu Moger 2497*2ef085bdSReinette Chatre if (!val) 2498*2ef085bdSReinette Chatre return 0; 2499fa7d9493SBabu Moger 250032f010deSReinette Chatre first_bit = find_first_bit(&val, cbm_len); 250132f010deSReinette Chatre zero_bit = find_next_zero_bit(&val, cbm_len, first_bit); 2502fa7d9493SBabu Moger 2503fa7d9493SBabu Moger /* Clear any remaining bits to ensure contiguous region */ 250432f010deSReinette Chatre bitmap_clear(&val, zero_bit, cbm_len - zero_bit); 2505*2ef085bdSReinette Chatre return (u32)val; 2506fa7d9493SBabu Moger } 2507fa7d9493SBabu Moger 25087390619aSXiaochen Shen /* 25097390619aSXiaochen Shen * Initialize cache resources per RDT domain 2510fa7d9493SBabu Moger * 25117390619aSXiaochen Shen * Set the RDT domain up to start off with all usable allocations. That is, 25127390619aSXiaochen Shen * all shareable and unused bits. All-zero CBM is invalid. 2513fa7d9493SBabu Moger */ 25147390619aSXiaochen Shen static int __init_one_rdt_domain(struct rdt_domain *d, struct rdt_resource *r, 25157390619aSXiaochen Shen u32 closid) 2516fa7d9493SBabu Moger { 2517fa7d9493SBabu Moger struct rdt_resource *r_cdp = NULL; 2518fa7d9493SBabu Moger struct rdt_domain *d_cdp = NULL; 2519fa7d9493SBabu Moger u32 used_b = 0, unused_b = 0; 2520fa7d9493SBabu Moger unsigned long tmp_cbm; 2521fa7d9493SBabu Moger enum rdtgrp_mode mode; 2522fa7d9493SBabu Moger u32 peer_ctl, *ctrl; 25237390619aSXiaochen Shen int i; 2524fa7d9493SBabu Moger 2525fa7d9493SBabu Moger rdt_cdp_peer_get(r, d, &r_cdp, &d_cdp); 2526fa7d9493SBabu Moger d->have_new_ctrl = false; 2527fa7d9493SBabu Moger d->new_ctrl = r->cache.shareable_bits; 2528fa7d9493SBabu Moger used_b = r->cache.shareable_bits; 2529fa7d9493SBabu Moger ctrl = d->ctrl_val; 2530fa7d9493SBabu Moger for (i = 0; i < closids_supported(); i++, ctrl++) { 2531fa7d9493SBabu Moger if (closid_allocated(i) && i != closid) { 2532fa7d9493SBabu Moger mode = rdtgroup_mode_by_closid(i); 2533fa7d9493SBabu Moger if (mode == RDT_MODE_PSEUDO_LOCKSETUP) 253487d3aa28SJames Morse /* 253587d3aa28SJames Morse * ctrl values for locksetup aren't relevant 253687d3aa28SJames Morse * until the schemata is written, and the mode 253787d3aa28SJames Morse * becomes RDT_MODE_PSEUDO_LOCKED. 253887d3aa28SJames Morse */ 253987d3aa28SJames Morse continue; 2540fa7d9493SBabu Moger /* 25417390619aSXiaochen Shen * If CDP is active include peer domain's 25427390619aSXiaochen Shen * usage to ensure there is no overlap 25437390619aSXiaochen Shen * with an exclusive group. 2544fa7d9493SBabu Moger */ 2545fa7d9493SBabu Moger if (d_cdp) 2546fa7d9493SBabu Moger peer_ctl = d_cdp->ctrl_val[i]; 2547fa7d9493SBabu Moger else 2548fa7d9493SBabu Moger peer_ctl = 0; 2549fa7d9493SBabu Moger used_b |= *ctrl | peer_ctl; 2550fa7d9493SBabu Moger if (mode == RDT_MODE_SHAREABLE) 2551fa7d9493SBabu Moger d->new_ctrl |= *ctrl | peer_ctl; 2552fa7d9493SBabu Moger } 2553fa7d9493SBabu Moger } 2554fa7d9493SBabu Moger if (d->plr && d->plr->cbm > 0) 2555fa7d9493SBabu Moger used_b |= d->plr->cbm; 2556fa7d9493SBabu Moger unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1); 2557fa7d9493SBabu Moger unused_b &= BIT_MASK(r->cache.cbm_len) - 1; 2558fa7d9493SBabu Moger d->new_ctrl |= unused_b; 2559fa7d9493SBabu Moger /* 2560fa7d9493SBabu Moger * Force the initial CBM to be valid, user can 2561fa7d9493SBabu Moger * modify the CBM based on system availability. 2562fa7d9493SBabu Moger */ 2563*2ef085bdSReinette Chatre d->new_ctrl = cbm_ensure_valid(d->new_ctrl, r); 2564fa7d9493SBabu Moger /* 25657390619aSXiaochen Shen * Assign the u32 CBM to an unsigned long to ensure that 25667390619aSXiaochen Shen * bitmap_weight() does not access out-of-bound memory. 2567fa7d9493SBabu Moger */ 2568fa7d9493SBabu Moger tmp_cbm = d->new_ctrl; 25697390619aSXiaochen Shen if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) { 25707390619aSXiaochen Shen rdt_last_cmd_printf("No space on %s:%d\n", r->name, d->id); 2571fa7d9493SBabu Moger return -ENOSPC; 2572fa7d9493SBabu Moger } 2573fa7d9493SBabu Moger d->have_new_ctrl = true; 25747390619aSXiaochen Shen 25757390619aSXiaochen Shen return 0; 25767390619aSXiaochen Shen } 25777390619aSXiaochen Shen 257847820e73SXiaochen Shen /* 257947820e73SXiaochen Shen * Initialize cache resources with default values. 25807390619aSXiaochen Shen * 25817390619aSXiaochen Shen * A new RDT group is being created on an allocation capable (CAT) 25827390619aSXiaochen Shen * supporting system. Set this group up to start off with all usable 25837390619aSXiaochen Shen * allocations. 25847390619aSXiaochen Shen * 25857390619aSXiaochen Shen * If there are no more shareable bits available on any domain then 25867390619aSXiaochen Shen * the entire allocation will fail. 25877390619aSXiaochen Shen */ 258847820e73SXiaochen Shen static int rdtgroup_init_cat(struct rdt_resource *r, u32 closid) 25897390619aSXiaochen Shen { 25907390619aSXiaochen Shen struct rdt_domain *d; 25917390619aSXiaochen Shen int ret; 25927390619aSXiaochen Shen 25937390619aSXiaochen Shen list_for_each_entry(d, &r->domains, list) { 259447820e73SXiaochen Shen ret = __init_one_rdt_domain(d, r, closid); 25957390619aSXiaochen Shen if (ret < 0) 25967390619aSXiaochen Shen return ret; 2597fa7d9493SBabu Moger } 259847820e73SXiaochen Shen 259947820e73SXiaochen Shen return 0; 2600fa7d9493SBabu Moger } 2601fa7d9493SBabu Moger 260247820e73SXiaochen Shen /* Initialize MBA resource with default values. */ 260347820e73SXiaochen Shen static void rdtgroup_init_mba(struct rdt_resource *r) 260447820e73SXiaochen Shen { 260547820e73SXiaochen Shen struct rdt_domain *d; 260647820e73SXiaochen Shen 260747820e73SXiaochen Shen list_for_each_entry(d, &r->domains, list) { 260847820e73SXiaochen Shen d->new_ctrl = is_mba_sc(r) ? MBA_MAX_MBPS : r->default_ctrl; 260947820e73SXiaochen Shen d->have_new_ctrl = true; 261047820e73SXiaochen Shen } 261147820e73SXiaochen Shen } 261247820e73SXiaochen Shen 261347820e73SXiaochen Shen /* Initialize the RDT group's allocations. */ 261447820e73SXiaochen Shen static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp) 261547820e73SXiaochen Shen { 261647820e73SXiaochen Shen struct rdt_resource *r; 261747820e73SXiaochen Shen int ret; 261847820e73SXiaochen Shen 2619fa7d9493SBabu Moger for_each_alloc_enabled_rdt_resource(r) { 262047820e73SXiaochen Shen if (r->rid == RDT_RESOURCE_MBA) { 262147820e73SXiaochen Shen rdtgroup_init_mba(r); 262247820e73SXiaochen Shen } else { 262347820e73SXiaochen Shen ret = rdtgroup_init_cat(r, rdtgrp->closid); 262447820e73SXiaochen Shen if (ret < 0) 262547820e73SXiaochen Shen return ret; 262647820e73SXiaochen Shen } 262747820e73SXiaochen Shen 2628fa7d9493SBabu Moger ret = update_domains(r, rdtgrp->closid); 2629fa7d9493SBabu Moger if (ret < 0) { 2630723f1a0dSBabu Moger rdt_last_cmd_puts("Failed to initialize allocations\n"); 2631fa7d9493SBabu Moger return ret; 2632fa7d9493SBabu Moger } 263347820e73SXiaochen Shen 2634fa7d9493SBabu Moger } 2635fa7d9493SBabu Moger 263640fba00fSXiaochen Shen rdtgrp->mode = RDT_MODE_SHAREABLE; 263740fba00fSXiaochen Shen 2638fa7d9493SBabu Moger return 0; 2639fa7d9493SBabu Moger } 2640fa7d9493SBabu Moger 2641fa7d9493SBabu Moger static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, 2642fa7d9493SBabu Moger struct kernfs_node *prgrp_kn, 2643fa7d9493SBabu Moger const char *name, umode_t mode, 2644fa7d9493SBabu Moger enum rdt_group_type rtype, struct rdtgroup **r) 2645fa7d9493SBabu Moger { 2646fa7d9493SBabu Moger struct rdtgroup *prdtgrp, *rdtgrp; 2647fa7d9493SBabu Moger struct kernfs_node *kn; 2648fa7d9493SBabu Moger uint files = 0; 2649fa7d9493SBabu Moger int ret; 2650fa7d9493SBabu Moger 2651fa7d9493SBabu Moger prdtgrp = rdtgroup_kn_lock_live(prgrp_kn); 2652fa7d9493SBabu Moger rdt_last_cmd_clear(); 2653fa7d9493SBabu Moger if (!prdtgrp) { 2654fa7d9493SBabu Moger ret = -ENODEV; 2655723f1a0dSBabu Moger rdt_last_cmd_puts("Directory was removed\n"); 2656fa7d9493SBabu Moger goto out_unlock; 2657fa7d9493SBabu Moger } 2658fa7d9493SBabu Moger 2659fa7d9493SBabu Moger if (rtype == RDTMON_GROUP && 2660fa7d9493SBabu Moger (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2661fa7d9493SBabu Moger prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) { 2662fa7d9493SBabu Moger ret = -EINVAL; 2663723f1a0dSBabu Moger rdt_last_cmd_puts("Pseudo-locking in progress\n"); 2664fa7d9493SBabu Moger goto out_unlock; 2665fa7d9493SBabu Moger } 2666fa7d9493SBabu Moger 2667fa7d9493SBabu Moger /* allocate the rdtgroup. */ 2668fa7d9493SBabu Moger rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); 2669fa7d9493SBabu Moger if (!rdtgrp) { 2670fa7d9493SBabu Moger ret = -ENOSPC; 2671723f1a0dSBabu Moger rdt_last_cmd_puts("Kernel out of memory\n"); 2672fa7d9493SBabu Moger goto out_unlock; 2673fa7d9493SBabu Moger } 2674fa7d9493SBabu Moger *r = rdtgrp; 2675fa7d9493SBabu Moger rdtgrp->mon.parent = prdtgrp; 2676fa7d9493SBabu Moger rdtgrp->type = rtype; 2677fa7d9493SBabu Moger INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); 2678fa7d9493SBabu Moger 2679fa7d9493SBabu Moger /* kernfs creates the directory for rdtgrp */ 2680fa7d9493SBabu Moger kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); 2681fa7d9493SBabu Moger if (IS_ERR(kn)) { 2682fa7d9493SBabu Moger ret = PTR_ERR(kn); 2683fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs create error\n"); 2684fa7d9493SBabu Moger goto out_free_rgrp; 2685fa7d9493SBabu Moger } 2686fa7d9493SBabu Moger rdtgrp->kn = kn; 2687fa7d9493SBabu Moger 2688fa7d9493SBabu Moger /* 2689fa7d9493SBabu Moger * kernfs_remove() will drop the reference count on "kn" which 2690fa7d9493SBabu Moger * will free it. But we still need it to stick around for the 2691fa7d9493SBabu Moger * rdtgroup_kn_unlock(kn} call below. Take one extra reference 2692fa7d9493SBabu Moger * here, which will be dropped inside rdtgroup_kn_unlock(). 2693fa7d9493SBabu Moger */ 2694fa7d9493SBabu Moger kernfs_get(kn); 2695fa7d9493SBabu Moger 2696fa7d9493SBabu Moger ret = rdtgroup_kn_set_ugid(kn); 2697fa7d9493SBabu Moger if (ret) { 2698fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs perm error\n"); 2699fa7d9493SBabu Moger goto out_destroy; 2700fa7d9493SBabu Moger } 2701fa7d9493SBabu Moger 2702fa7d9493SBabu Moger files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype); 2703fa7d9493SBabu Moger ret = rdtgroup_add_files(kn, files); 2704fa7d9493SBabu Moger if (ret) { 2705fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs fill error\n"); 2706fa7d9493SBabu Moger goto out_destroy; 2707fa7d9493SBabu Moger } 2708fa7d9493SBabu Moger 2709fa7d9493SBabu Moger if (rdt_mon_capable) { 2710fa7d9493SBabu Moger ret = alloc_rmid(); 2711fa7d9493SBabu Moger if (ret < 0) { 2712723f1a0dSBabu Moger rdt_last_cmd_puts("Out of RMIDs\n"); 2713fa7d9493SBabu Moger goto out_destroy; 2714fa7d9493SBabu Moger } 2715fa7d9493SBabu Moger rdtgrp->mon.rmid = ret; 2716fa7d9493SBabu Moger 2717fa7d9493SBabu Moger ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn); 2718fa7d9493SBabu Moger if (ret) { 2719fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs subdir error\n"); 2720fa7d9493SBabu Moger goto out_idfree; 2721fa7d9493SBabu Moger } 2722fa7d9493SBabu Moger } 2723fa7d9493SBabu Moger kernfs_activate(kn); 2724fa7d9493SBabu Moger 2725fa7d9493SBabu Moger /* 2726fa7d9493SBabu Moger * The caller unlocks the prgrp_kn upon success. 2727fa7d9493SBabu Moger */ 2728fa7d9493SBabu Moger return 0; 2729fa7d9493SBabu Moger 2730fa7d9493SBabu Moger out_idfree: 2731fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2732fa7d9493SBabu Moger out_destroy: 2733fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2734fa7d9493SBabu Moger out_free_rgrp: 2735fa7d9493SBabu Moger kfree(rdtgrp); 2736fa7d9493SBabu Moger out_unlock: 2737fa7d9493SBabu Moger rdtgroup_kn_unlock(prgrp_kn); 2738fa7d9493SBabu Moger return ret; 2739fa7d9493SBabu Moger } 2740fa7d9493SBabu Moger 2741fa7d9493SBabu Moger static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) 2742fa7d9493SBabu Moger { 2743fa7d9493SBabu Moger kernfs_remove(rgrp->kn); 2744fa7d9493SBabu Moger free_rmid(rgrp->mon.rmid); 2745fa7d9493SBabu Moger kfree(rgrp); 2746fa7d9493SBabu Moger } 2747fa7d9493SBabu Moger 2748fa7d9493SBabu Moger /* 2749fa7d9493SBabu Moger * Create a monitor group under "mon_groups" directory of a control 2750fa7d9493SBabu Moger * and monitor group(ctrl_mon). This is a resource group 2751fa7d9493SBabu Moger * to monitor a subset of tasks and cpus in its parent ctrl_mon group. 2752fa7d9493SBabu Moger */ 2753fa7d9493SBabu Moger static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, 2754fa7d9493SBabu Moger struct kernfs_node *prgrp_kn, 2755fa7d9493SBabu Moger const char *name, 2756fa7d9493SBabu Moger umode_t mode) 2757fa7d9493SBabu Moger { 2758fa7d9493SBabu Moger struct rdtgroup *rdtgrp, *prgrp; 2759fa7d9493SBabu Moger int ret; 2760fa7d9493SBabu Moger 2761fa7d9493SBabu Moger ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP, 2762fa7d9493SBabu Moger &rdtgrp); 2763fa7d9493SBabu Moger if (ret) 2764fa7d9493SBabu Moger return ret; 2765fa7d9493SBabu Moger 2766fa7d9493SBabu Moger prgrp = rdtgrp->mon.parent; 2767fa7d9493SBabu Moger rdtgrp->closid = prgrp->closid; 2768fa7d9493SBabu Moger 2769fa7d9493SBabu Moger /* 2770fa7d9493SBabu Moger * Add the rdtgrp to the list of rdtgrps the parent 2771fa7d9493SBabu Moger * ctrl_mon group has to track. 2772fa7d9493SBabu Moger */ 2773fa7d9493SBabu Moger list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); 2774fa7d9493SBabu Moger 2775fa7d9493SBabu Moger rdtgroup_kn_unlock(prgrp_kn); 2776fa7d9493SBabu Moger return ret; 2777fa7d9493SBabu Moger } 2778fa7d9493SBabu Moger 2779fa7d9493SBabu Moger /* 2780fa7d9493SBabu Moger * These are rdtgroups created under the root directory. Can be used 2781fa7d9493SBabu Moger * to allocate and monitor resources. 2782fa7d9493SBabu Moger */ 2783fa7d9493SBabu Moger static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, 2784fa7d9493SBabu Moger struct kernfs_node *prgrp_kn, 2785fa7d9493SBabu Moger const char *name, umode_t mode) 2786fa7d9493SBabu Moger { 2787fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 2788fa7d9493SBabu Moger struct kernfs_node *kn; 2789fa7d9493SBabu Moger u32 closid; 2790fa7d9493SBabu Moger int ret; 2791fa7d9493SBabu Moger 2792fa7d9493SBabu Moger ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP, 2793fa7d9493SBabu Moger &rdtgrp); 2794fa7d9493SBabu Moger if (ret) 2795fa7d9493SBabu Moger return ret; 2796fa7d9493SBabu Moger 2797fa7d9493SBabu Moger kn = rdtgrp->kn; 2798fa7d9493SBabu Moger ret = closid_alloc(); 2799fa7d9493SBabu Moger if (ret < 0) { 2800723f1a0dSBabu Moger rdt_last_cmd_puts("Out of CLOSIDs\n"); 2801fa7d9493SBabu Moger goto out_common_fail; 2802fa7d9493SBabu Moger } 2803fa7d9493SBabu Moger closid = ret; 2804fa7d9493SBabu Moger ret = 0; 2805fa7d9493SBabu Moger 2806fa7d9493SBabu Moger rdtgrp->closid = closid; 2807fa7d9493SBabu Moger ret = rdtgroup_init_alloc(rdtgrp); 2808fa7d9493SBabu Moger if (ret < 0) 2809fa7d9493SBabu Moger goto out_id_free; 2810fa7d9493SBabu Moger 2811fa7d9493SBabu Moger list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); 2812fa7d9493SBabu Moger 2813fa7d9493SBabu Moger if (rdt_mon_capable) { 2814fa7d9493SBabu Moger /* 2815fa7d9493SBabu Moger * Create an empty mon_groups directory to hold the subset 2816fa7d9493SBabu Moger * of tasks and cpus to monitor. 2817fa7d9493SBabu Moger */ 2818fa7d9493SBabu Moger ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL); 2819fa7d9493SBabu Moger if (ret) { 2820fa7d9493SBabu Moger rdt_last_cmd_puts("kernfs subdir error\n"); 2821fa7d9493SBabu Moger goto out_del_list; 2822fa7d9493SBabu Moger } 2823fa7d9493SBabu Moger } 2824fa7d9493SBabu Moger 2825fa7d9493SBabu Moger goto out_unlock; 2826fa7d9493SBabu Moger 2827fa7d9493SBabu Moger out_del_list: 2828fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 2829fa7d9493SBabu Moger out_id_free: 2830fa7d9493SBabu Moger closid_free(closid); 2831fa7d9493SBabu Moger out_common_fail: 2832fa7d9493SBabu Moger mkdir_rdt_prepare_clean(rdtgrp); 2833fa7d9493SBabu Moger out_unlock: 2834fa7d9493SBabu Moger rdtgroup_kn_unlock(prgrp_kn); 2835fa7d9493SBabu Moger return ret; 2836fa7d9493SBabu Moger } 2837fa7d9493SBabu Moger 2838fa7d9493SBabu Moger /* 2839fa7d9493SBabu Moger * We allow creating mon groups only with in a directory called "mon_groups" 2840fa7d9493SBabu Moger * which is present in every ctrl_mon group. Check if this is a valid 2841fa7d9493SBabu Moger * "mon_groups" directory. 2842fa7d9493SBabu Moger * 2843fa7d9493SBabu Moger * 1. The directory should be named "mon_groups". 2844fa7d9493SBabu Moger * 2. The mon group itself should "not" be named "mon_groups". 2845fa7d9493SBabu Moger * This makes sure "mon_groups" directory always has a ctrl_mon group 2846fa7d9493SBabu Moger * as parent. 2847fa7d9493SBabu Moger */ 2848fa7d9493SBabu Moger static bool is_mon_groups(struct kernfs_node *kn, const char *name) 2849fa7d9493SBabu Moger { 2850fa7d9493SBabu Moger return (!strcmp(kn->name, "mon_groups") && 2851fa7d9493SBabu Moger strcmp(name, "mon_groups")); 2852fa7d9493SBabu Moger } 2853fa7d9493SBabu Moger 2854fa7d9493SBabu Moger static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, 2855fa7d9493SBabu Moger umode_t mode) 2856fa7d9493SBabu Moger { 2857fa7d9493SBabu Moger /* Do not accept '\n' to avoid unparsable situation. */ 2858fa7d9493SBabu Moger if (strchr(name, '\n')) 2859fa7d9493SBabu Moger return -EINVAL; 2860fa7d9493SBabu Moger 2861fa7d9493SBabu Moger /* 2862fa7d9493SBabu Moger * If the parent directory is the root directory and RDT 2863fa7d9493SBabu Moger * allocation is supported, add a control and monitoring 2864fa7d9493SBabu Moger * subdirectory 2865fa7d9493SBabu Moger */ 2866fa7d9493SBabu Moger if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn) 2867fa7d9493SBabu Moger return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode); 2868fa7d9493SBabu Moger 2869fa7d9493SBabu Moger /* 2870fa7d9493SBabu Moger * If RDT monitoring is supported and the parent directory is a valid 2871fa7d9493SBabu Moger * "mon_groups" directory, add a monitoring subdirectory. 2872fa7d9493SBabu Moger */ 2873fa7d9493SBabu Moger if (rdt_mon_capable && is_mon_groups(parent_kn, name)) 2874fa7d9493SBabu Moger return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode); 2875fa7d9493SBabu Moger 2876fa7d9493SBabu Moger return -EPERM; 2877fa7d9493SBabu Moger } 2878fa7d9493SBabu Moger 2879fa7d9493SBabu Moger static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp, 2880fa7d9493SBabu Moger cpumask_var_t tmpmask) 2881fa7d9493SBabu Moger { 2882fa7d9493SBabu Moger struct rdtgroup *prdtgrp = rdtgrp->mon.parent; 2883fa7d9493SBabu Moger int cpu; 2884fa7d9493SBabu Moger 2885fa7d9493SBabu Moger /* Give any tasks back to the parent group */ 2886fa7d9493SBabu Moger rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); 2887fa7d9493SBabu Moger 2888fa7d9493SBabu Moger /* Update per cpu rmid of the moved CPUs first */ 2889fa7d9493SBabu Moger for_each_cpu(cpu, &rdtgrp->cpu_mask) 2890fa7d9493SBabu Moger per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid; 2891fa7d9493SBabu Moger /* 2892fa7d9493SBabu Moger * Update the MSR on moved CPUs and CPUs which have moved 2893fa7d9493SBabu Moger * task running on them. 2894fa7d9493SBabu Moger */ 2895fa7d9493SBabu Moger cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 2896fa7d9493SBabu Moger update_closid_rmid(tmpmask, NULL); 2897fa7d9493SBabu Moger 2898fa7d9493SBabu Moger rdtgrp->flags = RDT_DELETED; 2899fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2900fa7d9493SBabu Moger 2901fa7d9493SBabu Moger /* 2902fa7d9493SBabu Moger * Remove the rdtgrp from the parent ctrl_mon group's list 2903fa7d9493SBabu Moger */ 2904fa7d9493SBabu Moger WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); 2905fa7d9493SBabu Moger list_del(&rdtgrp->mon.crdtgrp_list); 2906fa7d9493SBabu Moger 2907fa7d9493SBabu Moger /* 2908fa7d9493SBabu Moger * one extra hold on this, will drop when we kfree(rdtgrp) 2909fa7d9493SBabu Moger * in rdtgroup_kn_unlock() 2910fa7d9493SBabu Moger */ 2911fa7d9493SBabu Moger kernfs_get(kn); 2912fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2913fa7d9493SBabu Moger 2914fa7d9493SBabu Moger return 0; 2915fa7d9493SBabu Moger } 2916fa7d9493SBabu Moger 2917fa7d9493SBabu Moger static int rdtgroup_ctrl_remove(struct kernfs_node *kn, 2918fa7d9493SBabu Moger struct rdtgroup *rdtgrp) 2919fa7d9493SBabu Moger { 2920fa7d9493SBabu Moger rdtgrp->flags = RDT_DELETED; 2921fa7d9493SBabu Moger list_del(&rdtgrp->rdtgroup_list); 2922fa7d9493SBabu Moger 2923fa7d9493SBabu Moger /* 2924fa7d9493SBabu Moger * one extra hold on this, will drop when we kfree(rdtgrp) 2925fa7d9493SBabu Moger * in rdtgroup_kn_unlock() 2926fa7d9493SBabu Moger */ 2927fa7d9493SBabu Moger kernfs_get(kn); 2928fa7d9493SBabu Moger kernfs_remove(rdtgrp->kn); 2929fa7d9493SBabu Moger return 0; 2930fa7d9493SBabu Moger } 2931fa7d9493SBabu Moger 2932fa7d9493SBabu Moger static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp, 2933fa7d9493SBabu Moger cpumask_var_t tmpmask) 2934fa7d9493SBabu Moger { 2935fa7d9493SBabu Moger int cpu; 2936fa7d9493SBabu Moger 2937fa7d9493SBabu Moger /* Give any tasks back to the default group */ 2938fa7d9493SBabu Moger rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); 2939fa7d9493SBabu Moger 2940fa7d9493SBabu Moger /* Give any CPUs back to the default group */ 2941fa7d9493SBabu Moger cpumask_or(&rdtgroup_default.cpu_mask, 2942fa7d9493SBabu Moger &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); 2943fa7d9493SBabu Moger 2944fa7d9493SBabu Moger /* Update per cpu closid and rmid of the moved CPUs first */ 2945fa7d9493SBabu Moger for_each_cpu(cpu, &rdtgrp->cpu_mask) { 2946fa7d9493SBabu Moger per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid; 2947fa7d9493SBabu Moger per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid; 2948fa7d9493SBabu Moger } 2949fa7d9493SBabu Moger 2950fa7d9493SBabu Moger /* 2951fa7d9493SBabu Moger * Update the MSR on moved CPUs and CPUs which have moved 2952fa7d9493SBabu Moger * task running on them. 2953fa7d9493SBabu Moger */ 2954fa7d9493SBabu Moger cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); 2955fa7d9493SBabu Moger update_closid_rmid(tmpmask, NULL); 2956fa7d9493SBabu Moger 2957fa7d9493SBabu Moger closid_free(rdtgrp->closid); 2958fa7d9493SBabu Moger free_rmid(rdtgrp->mon.rmid); 2959fa7d9493SBabu Moger 2960fa7d9493SBabu Moger /* 2961fa7d9493SBabu Moger * Free all the child monitor group rmids. 2962fa7d9493SBabu Moger */ 2963fa7d9493SBabu Moger free_all_child_rdtgrp(rdtgrp); 2964fa7d9493SBabu Moger 2965fa7d9493SBabu Moger rdtgroup_ctrl_remove(kn, rdtgrp); 2966fa7d9493SBabu Moger 2967fa7d9493SBabu Moger return 0; 2968fa7d9493SBabu Moger } 2969fa7d9493SBabu Moger 2970fa7d9493SBabu Moger static int rdtgroup_rmdir(struct kernfs_node *kn) 2971fa7d9493SBabu Moger { 2972fa7d9493SBabu Moger struct kernfs_node *parent_kn = kn->parent; 2973fa7d9493SBabu Moger struct rdtgroup *rdtgrp; 2974fa7d9493SBabu Moger cpumask_var_t tmpmask; 2975fa7d9493SBabu Moger int ret = 0; 2976fa7d9493SBabu Moger 2977fa7d9493SBabu Moger if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) 2978fa7d9493SBabu Moger return -ENOMEM; 2979fa7d9493SBabu Moger 2980fa7d9493SBabu Moger rdtgrp = rdtgroup_kn_lock_live(kn); 2981fa7d9493SBabu Moger if (!rdtgrp) { 2982fa7d9493SBabu Moger ret = -EPERM; 2983fa7d9493SBabu Moger goto out; 2984fa7d9493SBabu Moger } 2985fa7d9493SBabu Moger 2986fa7d9493SBabu Moger /* 2987fa7d9493SBabu Moger * If the rdtgroup is a ctrl_mon group and parent directory 2988fa7d9493SBabu Moger * is the root directory, remove the ctrl_mon group. 2989fa7d9493SBabu Moger * 2990fa7d9493SBabu Moger * If the rdtgroup is a mon group and parent directory 2991fa7d9493SBabu Moger * is a valid "mon_groups" directory, remove the mon group. 2992fa7d9493SBabu Moger */ 2993fa7d9493SBabu Moger if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn) { 2994fa7d9493SBabu Moger if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP || 2995fa7d9493SBabu Moger rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { 2996fa7d9493SBabu Moger ret = rdtgroup_ctrl_remove(kn, rdtgrp); 2997fa7d9493SBabu Moger } else { 2998fa7d9493SBabu Moger ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask); 2999fa7d9493SBabu Moger } 3000fa7d9493SBabu Moger } else if (rdtgrp->type == RDTMON_GROUP && 3001fa7d9493SBabu Moger is_mon_groups(parent_kn, kn->name)) { 3002fa7d9493SBabu Moger ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask); 3003fa7d9493SBabu Moger } else { 3004fa7d9493SBabu Moger ret = -EPERM; 3005fa7d9493SBabu Moger } 3006fa7d9493SBabu Moger 3007fa7d9493SBabu Moger out: 3008fa7d9493SBabu Moger rdtgroup_kn_unlock(kn); 3009fa7d9493SBabu Moger free_cpumask_var(tmpmask); 3010fa7d9493SBabu Moger return ret; 3011fa7d9493SBabu Moger } 3012fa7d9493SBabu Moger 3013fa7d9493SBabu Moger static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) 3014fa7d9493SBabu Moger { 3015fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) 3016fa7d9493SBabu Moger seq_puts(seq, ",cdp"); 3017fa7d9493SBabu Moger 3018fa7d9493SBabu Moger if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) 3019fa7d9493SBabu Moger seq_puts(seq, ",cdpl2"); 3020fa7d9493SBabu Moger 3021fa7d9493SBabu Moger if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA])) 3022fa7d9493SBabu Moger seq_puts(seq, ",mba_MBps"); 3023fa7d9493SBabu Moger 3024fa7d9493SBabu Moger return 0; 3025fa7d9493SBabu Moger } 3026fa7d9493SBabu Moger 3027fa7d9493SBabu Moger static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { 3028fa7d9493SBabu Moger .mkdir = rdtgroup_mkdir, 3029fa7d9493SBabu Moger .rmdir = rdtgroup_rmdir, 3030fa7d9493SBabu Moger .show_options = rdtgroup_show_options, 3031fa7d9493SBabu Moger }; 3032fa7d9493SBabu Moger 3033fa7d9493SBabu Moger static int __init rdtgroup_setup_root(void) 3034fa7d9493SBabu Moger { 3035fa7d9493SBabu Moger int ret; 3036fa7d9493SBabu Moger 3037fa7d9493SBabu Moger rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, 3038fa7d9493SBabu Moger KERNFS_ROOT_CREATE_DEACTIVATED | 3039fa7d9493SBabu Moger KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, 3040fa7d9493SBabu Moger &rdtgroup_default); 3041fa7d9493SBabu Moger if (IS_ERR(rdt_root)) 3042fa7d9493SBabu Moger return PTR_ERR(rdt_root); 3043fa7d9493SBabu Moger 3044fa7d9493SBabu Moger mutex_lock(&rdtgroup_mutex); 3045fa7d9493SBabu Moger 3046fa7d9493SBabu Moger rdtgroup_default.closid = 0; 3047fa7d9493SBabu Moger rdtgroup_default.mon.rmid = 0; 3048fa7d9493SBabu Moger rdtgroup_default.type = RDTCTRL_GROUP; 3049fa7d9493SBabu Moger INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); 3050fa7d9493SBabu Moger 3051fa7d9493SBabu Moger list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); 3052fa7d9493SBabu Moger 3053fa7d9493SBabu Moger ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE); 3054fa7d9493SBabu Moger if (ret) { 3055fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3056fa7d9493SBabu Moger goto out; 3057fa7d9493SBabu Moger } 3058fa7d9493SBabu Moger 3059fa7d9493SBabu Moger rdtgroup_default.kn = rdt_root->kn; 3060fa7d9493SBabu Moger kernfs_activate(rdtgroup_default.kn); 3061fa7d9493SBabu Moger 3062fa7d9493SBabu Moger out: 3063fa7d9493SBabu Moger mutex_unlock(&rdtgroup_mutex); 3064fa7d9493SBabu Moger 3065fa7d9493SBabu Moger return ret; 3066fa7d9493SBabu Moger } 3067fa7d9493SBabu Moger 3068fa7d9493SBabu Moger /* 3069fa7d9493SBabu Moger * rdtgroup_init - rdtgroup initialization 3070fa7d9493SBabu Moger * 3071fa7d9493SBabu Moger * Setup resctrl file system including set up root, create mount point, 3072fa7d9493SBabu Moger * register rdtgroup filesystem, and initialize files under root directory. 3073fa7d9493SBabu Moger * 3074fa7d9493SBabu Moger * Return: 0 on success or -errno 3075fa7d9493SBabu Moger */ 3076fa7d9493SBabu Moger int __init rdtgroup_init(void) 3077fa7d9493SBabu Moger { 3078fa7d9493SBabu Moger int ret = 0; 3079fa7d9493SBabu Moger 3080fa7d9493SBabu Moger seq_buf_init(&last_cmd_status, last_cmd_status_buf, 3081fa7d9493SBabu Moger sizeof(last_cmd_status_buf)); 3082fa7d9493SBabu Moger 3083fa7d9493SBabu Moger ret = rdtgroup_setup_root(); 3084fa7d9493SBabu Moger if (ret) 3085fa7d9493SBabu Moger return ret; 3086fa7d9493SBabu Moger 3087fa7d9493SBabu Moger ret = sysfs_create_mount_point(fs_kobj, "resctrl"); 3088fa7d9493SBabu Moger if (ret) 3089fa7d9493SBabu Moger goto cleanup_root; 3090fa7d9493SBabu Moger 3091fa7d9493SBabu Moger ret = register_filesystem(&rdt_fs_type); 3092fa7d9493SBabu Moger if (ret) 3093fa7d9493SBabu Moger goto cleanup_mountpoint; 3094fa7d9493SBabu Moger 3095fa7d9493SBabu Moger /* 3096fa7d9493SBabu Moger * Adding the resctrl debugfs directory here may not be ideal since 3097fa7d9493SBabu Moger * it would let the resctrl debugfs directory appear on the debugfs 3098fa7d9493SBabu Moger * filesystem before the resctrl filesystem is mounted. 3099fa7d9493SBabu Moger * It may also be ok since that would enable debugging of RDT before 3100fa7d9493SBabu Moger * resctrl is mounted. 3101fa7d9493SBabu Moger * The reason why the debugfs directory is created here and not in 3102fa7d9493SBabu Moger * rdt_mount() is because rdt_mount() takes rdtgroup_mutex and 3103fa7d9493SBabu Moger * during the debugfs directory creation also &sb->s_type->i_mutex_key 3104fa7d9493SBabu Moger * (the lockdep class of inode->i_rwsem). Other filesystem 3105fa7d9493SBabu Moger * interactions (eg. SyS_getdents) have the lock ordering: 3106fa7d9493SBabu Moger * &sb->s_type->i_mutex_key --> &mm->mmap_sem 3107fa7d9493SBabu Moger * During mmap(), called with &mm->mmap_sem, the rdtgroup_mutex 3108fa7d9493SBabu Moger * is taken, thus creating dependency: 3109fa7d9493SBabu Moger * &mm->mmap_sem --> rdtgroup_mutex for the latter that can cause 3110fa7d9493SBabu Moger * issues considering the other two lock dependencies. 3111fa7d9493SBabu Moger * By creating the debugfs directory here we avoid a dependency 3112fa7d9493SBabu Moger * that may cause deadlock (even though file operations cannot 3113fa7d9493SBabu Moger * occur until the filesystem is mounted, but I do not know how to 3114fa7d9493SBabu Moger * tell lockdep that). 3115fa7d9493SBabu Moger */ 3116fa7d9493SBabu Moger debugfs_resctrl = debugfs_create_dir("resctrl", NULL); 3117fa7d9493SBabu Moger 3118fa7d9493SBabu Moger return 0; 3119fa7d9493SBabu Moger 3120fa7d9493SBabu Moger cleanup_mountpoint: 3121fa7d9493SBabu Moger sysfs_remove_mount_point(fs_kobj, "resctrl"); 3122fa7d9493SBabu Moger cleanup_root: 3123fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3124fa7d9493SBabu Moger 3125fa7d9493SBabu Moger return ret; 3126fa7d9493SBabu Moger } 3127fa7d9493SBabu Moger 3128fa7d9493SBabu Moger void __exit rdtgroup_exit(void) 3129fa7d9493SBabu Moger { 3130fa7d9493SBabu Moger debugfs_remove_recursive(debugfs_resctrl); 3131fa7d9493SBabu Moger unregister_filesystem(&rdt_fs_type); 3132fa7d9493SBabu Moger sysfs_remove_mount_point(fs_kobj, "resctrl"); 3133fa7d9493SBabu Moger kernfs_destroy_root(rdt_root); 3134fa7d9493SBabu Moger } 3135