blob: f276aff521e8b851b0cf2641aaec1adef59a4c15 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002/*
Ingo Molnard9f6e122021-03-18 15:28:01 +01003 * User interface for Resource Allocation in Resource Director Technology(RDT)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07004 *
5 * Copyright (C) 2016 Intel Corporation
6 *
7 * Author: Fenghua Yu <fenghua.yu@intel.com>
8 *
Fenghua Yu5ff193f2016-10-28 15:04:42 -07009 * More information about RDT be found in the Intel (R) x86 Architecture
10 * Software Developer Manual.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Reinette Chatred9b48c82018-06-22 15:42:06 -070015#include <linux/cacheinfo.h>
Tony Luck12e01102016-10-28 15:04:45 -070016#include <linux/cpu.h>
Reinette Chatre37707ec2018-06-22 15:42:25 -070017#include <linux/debugfs.h>
Fenghua Yu5ff193f2016-10-28 15:04:42 -070018#include <linux/fs.h>
David Howells23bf1b62018-11-01 23:07:26 +000019#include <linux/fs_parser.h>
Fenghua Yu5ff193f2016-10-28 15:04:42 -070020#include <linux/sysfs.h>
21#include <linux/kernfs.h>
Tony Luck9b3a7fd2017-09-25 16:39:33 -070022#include <linux/seq_buf.h>
Fenghua Yu4e978d02016-10-28 15:04:43 -070023#include <linux/seq_file.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010024#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010025#include <linux/sched/task.h>
Fenghua Yu5ff193f2016-10-28 15:04:42 -070026#include <linux/slab.h>
Fenghua Yue02737d2016-10-28 15:04:46 -070027#include <linux/task_work.h>
David Howells23bf1b62018-11-01 23:07:26 +000028#include <linux/user_namespace.h>
Fenghua Yu5ff193f2016-10-28 15:04:42 -070029
30#include <uapi/linux/magic.h>
31
Reinette Chatre8dd97c62020-05-05 15:36:12 -070032#include <asm/resctrl.h>
Babu Mogerfa7d9492018-11-21 20:28:25 +000033#include "internal.h"
Fenghua Yu5ff193f2016-10-28 15:04:42 -070034
Vikas Shivappa4af4a882017-07-25 14:14:41 -070035DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
36DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -070037DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
Reinette Chatrecb2200e2017-07-25 14:14:24 -070038static struct kernfs_root *rdt_root;
Fenghua Yu5ff193f2016-10-28 15:04:42 -070039struct rdtgroup rdtgroup_default;
40LIST_HEAD(rdt_all_groups);
41
James Morsecdb9ebc2021-07-28 17:06:16 +000042/* list of entries for the schemata file */
43LIST_HEAD(resctrl_schema_all);
44
Fenghua Yu4e978d02016-10-28 15:04:43 -070045/* Kernel fs node for "info" directory under root */
46static struct kernfs_node *kn_info;
47
Vikas Shivappa4af4a882017-07-25 14:14:41 -070048/* Kernel fs node for "mon_groups" directory under root */
49static struct kernfs_node *kn_mongrp;
50
51/* Kernel fs node for "mon_data" directory under root */
52static struct kernfs_node *kn_mondata;
53
Tony Luck9b3a7fd2017-09-25 16:39:33 -070054static struct seq_buf last_cmd_status;
55static char last_cmd_status_buf[512];
56
Reinette Chatre37707ec2018-06-22 15:42:25 -070057struct dentry *debugfs_resctrl;
58
Tony Luck9b3a7fd2017-09-25 16:39:33 -070059void rdt_last_cmd_clear(void)
60{
61 lockdep_assert_held(&rdtgroup_mutex);
62 seq_buf_clear(&last_cmd_status);
63}
64
65void rdt_last_cmd_puts(const char *s)
66{
67 lockdep_assert_held(&rdtgroup_mutex);
68 seq_buf_puts(&last_cmd_status, s);
69}
70
71void rdt_last_cmd_printf(const char *fmt, ...)
72{
73 va_list ap;
74
75 va_start(ap, fmt);
76 lockdep_assert_held(&rdtgroup_mutex);
77 seq_buf_vprintf(&last_cmd_status, fmt, ap);
78 va_end(ap);
79}
80
Fenghua Yu60cf5e12016-10-28 15:04:44 -070081/*
82 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
83 * we can keep a bitmap of free CLOSIDs in a single integer.
84 *
85 * Using a global CLOSID across all resources has some advantages and
86 * some drawbacks:
87 * + We can simply set "current->closid" to assign a task to a resource
88 * group.
89 * + Context switch code can avoid extra memory references deciding which
90 * CLOSID to load into the PQR_ASSOC MSR
91 * - We give up some options in configuring resource groups across multi-socket
92 * systems.
93 * - Our choices on how to configure each resource become progressively more
94 * limited as the number of resources grows.
95 */
96static int closid_free_map;
Reinette Chatrec793da82018-09-15 14:58:21 -070097static int closid_free_map_len;
98
99int closids_supported(void)
100{
101 return closid_free_map_len;
102}
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700103
104static void closid_init(void)
105{
James Morse331ebe42021-07-28 17:06:19 +0000106 struct resctrl_schema *s;
James Morseeb6f3182021-07-28 17:06:21 +0000107 u32 rdt_min_closid = 32;
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700108
109 /* Compute rdt_min_closid across all resources */
James Morse3183e872021-07-28 17:06:20 +0000110 list_for_each_entry(s, &resctrl_schema_all, list)
111 rdt_min_closid = min(rdt_min_closid, s->num_closid);
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700112
113 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
114
115 /* CLOSID 0 is always reserved for the default group */
116 closid_free_map &= ~1;
Reinette Chatrec793da82018-09-15 14:58:21 -0700117 closid_free_map_len = rdt_min_closid;
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700118}
119
Reinette Chatrecb2200e2017-07-25 14:14:24 -0700120static int closid_alloc(void)
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700121{
Vikas Shivappa0734ded2017-07-25 14:14:33 -0700122 u32 closid = ffs(closid_free_map);
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700123
124 if (closid == 0)
125 return -ENOSPC;
126 closid--;
127 closid_free_map &= ~(1 << closid);
128
129 return closid;
130}
131
Reinette Chatre024d15b2018-06-22 15:41:58 -0700132void closid_free(int closid)
Fenghua Yu60cf5e12016-10-28 15:04:44 -0700133{
134 closid_free_map |= 1 << closid;
135}
136
Reinette Chatre472ef09b2018-06-22 15:41:55 -0700137/**
Reinette Chatre0b9aa652018-06-22 15:41:57 -0700138 * closid_allocated - test if provided closid is in use
139 * @closid: closid to be tested
140 *
141 * Return: true if @closid is currently associated with a resource group,
142 * false if @closid is free
143 */
Reinette Chatre95f0b772018-06-22 15:41:59 -0700144static bool closid_allocated(unsigned int closid)
Reinette Chatre0b9aa652018-06-22 15:41:57 -0700145{
146 return (closid_free_map & (1 << closid)) == 0;
147}
148
149/**
Reinette Chatre472ef09b2018-06-22 15:41:55 -0700150 * rdtgroup_mode_by_closid - Return mode of resource group with closid
151 * @closid: closid if the resource group
152 *
153 * Each resource group is associated with a @closid. Here the mode
154 * of a resource group can be queried by searching for it using its closid.
155 *
156 * Return: mode as &enum rdtgrp_mode of resource group with closid @closid
157 */
158enum rdtgrp_mode rdtgroup_mode_by_closid(int closid)
159{
160 struct rdtgroup *rdtgrp;
161
162 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
163 if (rdtgrp->closid == closid)
164 return rdtgrp->mode;
165 }
166
167 return RDT_NUM_MODES;
168}
169
Reinette Chatred48d7a52018-06-22 15:41:56 -0700170static const char * const rdt_mode_str[] = {
Reinette Chatrebb9fec62018-06-22 15:42:08 -0700171 [RDT_MODE_SHAREABLE] = "shareable",
172 [RDT_MODE_EXCLUSIVE] = "exclusive",
173 [RDT_MODE_PSEUDO_LOCKSETUP] = "pseudo-locksetup",
174 [RDT_MODE_PSEUDO_LOCKED] = "pseudo-locked",
Reinette Chatred48d7a52018-06-22 15:41:56 -0700175};
176
177/**
178 * rdtgroup_mode_str - Return the string representation of mode
179 * @mode: the resource group mode as &enum rdtgroup_mode
180 *
181 * Return: string representation of valid mode, "unknown" otherwise
182 */
183static const char *rdtgroup_mode_str(enum rdtgrp_mode mode)
184{
185 if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES)
186 return "unknown";
187
188 return rdt_mode_str[mode];
189}
190
Fenghua Yu4e978d02016-10-28 15:04:43 -0700191/* set uid and gid of rdtgroup dirs and files to that of the creator */
192static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
193{
194 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
195 .ia_uid = current_fsuid(),
196 .ia_gid = current_fsgid(), };
197
198 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
199 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
200 return 0;
201
202 return kernfs_setattr(kn, &iattr);
203}
204
205static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
206{
207 struct kernfs_node *kn;
208 int ret;
209
210 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000211 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
Fenghua Yu4e978d02016-10-28 15:04:43 -0700212 0, rft->kf_ops, rft, NULL, NULL);
213 if (IS_ERR(kn))
214 return PTR_ERR(kn);
215
216 ret = rdtgroup_kn_set_ugid(kn);
217 if (ret) {
218 kernfs_remove(kn);
219 return ret;
220 }
221
222 return 0;
223}
224
Fenghua Yu4e978d02016-10-28 15:04:43 -0700225static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
226{
227 struct kernfs_open_file *of = m->private;
228 struct rftype *rft = of->kn->priv;
229
230 if (rft->seq_show)
231 return rft->seq_show(of, m, arg);
232 return 0;
233}
234
235static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
236 size_t nbytes, loff_t off)
237{
238 struct rftype *rft = of->kn->priv;
239
240 if (rft->write)
241 return rft->write(of, buf, nbytes, off);
242
243 return -EINVAL;
244}
245
Rikard Falkeborn2002d292020-11-11 00:02:28 +0100246static const struct kernfs_ops rdtgroup_kf_single_ops = {
Fenghua Yu4e978d02016-10-28 15:04:43 -0700247 .atomic_write_len = PAGE_SIZE,
248 .write = rdtgroup_file_write,
249 .seq_show = rdtgroup_seqfile_show,
250};
251
Rikard Falkeborn2002d292020-11-11 00:02:28 +0100252static const struct kernfs_ops kf_mondata_ops = {
Vikas Shivappad89b7372017-07-25 14:14:38 -0700253 .atomic_write_len = PAGE_SIZE,
254 .seq_show = rdtgroup_mondata_show,
255};
256
Jiri Olsa4ffa3c92017-04-10 16:52:32 +0200257static bool is_cpu_list(struct kernfs_open_file *of)
258{
259 struct rftype *rft = of->kn->priv;
260
261 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
262}
263
Tony Luck12e01102016-10-28 15:04:45 -0700264static int rdtgroup_cpus_show(struct kernfs_open_file *of,
265 struct seq_file *s, void *v)
266{
267 struct rdtgroup *rdtgrp;
Jithu Josephb61b8bb2018-10-12 15:51:01 -0700268 struct cpumask *mask;
Tony Luck12e01102016-10-28 15:04:45 -0700269 int ret = 0;
270
271 rdtgrp = rdtgroup_kn_lock_live(of->kn);
272
Jiri Olsa4ffa3c92017-04-10 16:52:32 +0200273 if (rdtgrp) {
Jithu Josephb61b8bb2018-10-12 15:51:01 -0700274 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
275 if (!rdtgrp->plr->d) {
276 rdt_last_cmd_clear();
277 rdt_last_cmd_puts("Cache domain offline\n");
278 ret = -ENODEV;
279 } else {
280 mask = &rdtgrp->plr->d->cpu_mask;
281 seq_printf(s, is_cpu_list(of) ?
282 "%*pbl\n" : "%*pb\n",
283 cpumask_pr_args(mask));
284 }
285 } else {
Reinette Chatre33dc3e42018-06-30 22:17:33 -0700286 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
287 cpumask_pr_args(&rdtgrp->cpu_mask));
Jithu Josephb61b8bb2018-10-12 15:51:01 -0700288 }
Jiri Olsa4ffa3c92017-04-10 16:52:32 +0200289 } else {
Tony Luck12e01102016-10-28 15:04:45 -0700290 ret = -ENOENT;
Jiri Olsa4ffa3c92017-04-10 16:52:32 +0200291 }
Tony Luck12e01102016-10-28 15:04:45 -0700292 rdtgroup_kn_unlock(of->kn);
293
294 return ret;
295}
296
Fenghua Yuf4107702016-11-11 17:02:38 -0800297/*
Babu Moger352940e2018-11-21 20:28:27 +0000298 * This is safe against resctrl_sched_in() called from __switch_to()
Fenghua Yuf4107702016-11-11 17:02:38 -0800299 * because __switch_to() is executed with interrupts disabled. A local call
Ingo Molnard9f6e122021-03-18 15:28:01 +0100300 * from update_closid_rmid() is protected against __switch_to() because
Fenghua Yuf4107702016-11-11 17:02:38 -0800301 * preemption is disabled.
302 */
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700303static void update_cpu_closid_rmid(void *info)
Fenghua Yuf4107702016-11-11 17:02:38 -0800304{
Vikas Shivappab09d9812017-07-25 14:14:35 -0700305 struct rdtgroup *r = info;
306
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700307 if (r) {
Vikas Shivappaa9110b52017-08-09 11:46:34 -0700308 this_cpu_write(pqr_state.default_closid, r->closid);
309 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700310 }
Vikas Shivappab09d9812017-07-25 14:14:35 -0700311
Fenghua Yuf4107702016-11-11 17:02:38 -0800312 /*
313 * We cannot unconditionally write the MSR because the current
314 * executing task might have its own closid selected. Just reuse
315 * the context switch code.
316 */
Babu Moger352940e2018-11-21 20:28:27 +0000317 resctrl_sched_in();
Fenghua Yuf4107702016-11-11 17:02:38 -0800318}
319
Fenghua Yu0efc89b2016-11-18 15:18:04 -0800320/*
321 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
322 *
Vikas Shivappab09d9812017-07-25 14:14:35 -0700323 * Per task closids/rmids must have been set up before calling this function.
Fenghua Yu0efc89b2016-11-18 15:18:04 -0800324 */
325static void
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700326update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
Fenghua Yuf4107702016-11-11 17:02:38 -0800327{
328 int cpu = get_cpu();
329
330 if (cpumask_test_cpu(cpu, cpu_mask))
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700331 update_cpu_closid_rmid(r);
332 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
Fenghua Yuf4107702016-11-11 17:02:38 -0800333 put_cpu();
334}
335
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700336static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
337 cpumask_var_t tmpmask)
Vikas Shivappab09d9812017-07-25 14:14:35 -0700338{
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700339 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
340 struct list_head *head;
341
342 /* Check whether cpus belong to parent ctrl group */
343 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800344 if (!cpumask_empty(tmpmask)) {
Babu Moger723f1a0d2018-11-21 20:28:43 +0000345 rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700346 return -EINVAL;
Tony Luck94457b32017-09-25 16:39:36 -0700347 }
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700348
349 /* Check whether cpus are dropped from this group */
350 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800351 if (!cpumask_empty(tmpmask)) {
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700352 /* Give any dropped cpus to parent rdtgroup */
353 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
354 update_closid_rmid(tmpmask, prgrp);
355 }
356
357 /*
358 * If we added cpus, remove them from previous group that owned them
359 * and update per-cpu rmid
360 */
361 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800362 if (!cpumask_empty(tmpmask)) {
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700363 head = &prgrp->mon.crdtgrp_list;
364 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
365 if (crgrp == rdtgrp)
366 continue;
367 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
368 tmpmask);
369 }
370 update_closid_rmid(tmpmask, rdtgrp);
371 }
372
373 /* Done pushing/pulling - update this group with new mask */
374 cpumask_copy(&rdtgrp->cpu_mask, newmask);
375
376 return 0;
377}
378
379static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
380{
381 struct rdtgroup *crgrp;
382
383 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
384 /* update the child mon group masks as well*/
385 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
386 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
387}
388
389static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
390 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
391{
392 struct rdtgroup *r, *crgrp;
393 struct list_head *head;
Vikas Shivappab09d9812017-07-25 14:14:35 -0700394
395 /* Check whether cpus are dropped from this group */
396 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800397 if (!cpumask_empty(tmpmask)) {
Vikas Shivappab09d9812017-07-25 14:14:35 -0700398 /* Can't drop from default group */
Tony Luck94457b32017-09-25 16:39:36 -0700399 if (rdtgrp == &rdtgroup_default) {
400 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
Vikas Shivappab09d9812017-07-25 14:14:35 -0700401 return -EINVAL;
Tony Luck94457b32017-09-25 16:39:36 -0700402 }
Vikas Shivappab09d9812017-07-25 14:14:35 -0700403
404 /* Give any dropped cpus to rdtgroup_default */
405 cpumask_or(&rdtgroup_default.cpu_mask,
406 &rdtgroup_default.cpu_mask, tmpmask);
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700407 update_closid_rmid(tmpmask, &rdtgroup_default);
Vikas Shivappab09d9812017-07-25 14:14:35 -0700408 }
409
410 /*
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700411 * If we added cpus, remove them from previous group and
412 * the prev group's child groups that owned them
413 * and update per-cpu closid/rmid.
Vikas Shivappab09d9812017-07-25 14:14:35 -0700414 */
415 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800416 if (!cpumask_empty(tmpmask)) {
Vikas Shivappab09d9812017-07-25 14:14:35 -0700417 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
418 if (r == rdtgrp)
419 continue;
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700420 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800421 if (!cpumask_empty(tmpmask1))
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700422 cpumask_rdtgrp_clear(r, tmpmask1);
Vikas Shivappab09d9812017-07-25 14:14:35 -0700423 }
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700424 update_closid_rmid(tmpmask, rdtgrp);
Vikas Shivappab09d9812017-07-25 14:14:35 -0700425 }
426
427 /* Done pushing/pulling - update this group with new mask */
428 cpumask_copy(&rdtgrp->cpu_mask, newmask);
429
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700430 /*
431 * Clear child mon group masks since there is a new parent mask
432 * now and update the rmid for the cpus the child lost.
433 */
434 head = &rdtgrp->mon.crdtgrp_list;
435 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
436 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
437 update_closid_rmid(tmpmask, rdtgrp);
438 cpumask_clear(&crgrp->cpu_mask);
439 }
440
Vikas Shivappab09d9812017-07-25 14:14:35 -0700441 return 0;
442}
443
Tony Luck12e01102016-10-28 15:04:45 -0700444static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
445 char *buf, size_t nbytes, loff_t off)
446{
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700447 cpumask_var_t tmpmask, newmask, tmpmask1;
Vikas Shivappab09d9812017-07-25 14:14:35 -0700448 struct rdtgroup *rdtgrp;
Fenghua Yuf4107702016-11-11 17:02:38 -0800449 int ret;
Tony Luck12e01102016-10-28 15:04:45 -0700450
451 if (!buf)
452 return -EINVAL;
453
454 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
455 return -ENOMEM;
456 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
457 free_cpumask_var(tmpmask);
458 return -ENOMEM;
459 }
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700460 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
461 free_cpumask_var(tmpmask);
462 free_cpumask_var(newmask);
463 return -ENOMEM;
464 }
Thomas Gleixnera2584e12016-11-15 15:12:13 +0100465
Tony Luck12e01102016-10-28 15:04:45 -0700466 rdtgrp = rdtgroup_kn_lock_live(of->kn);
467 if (!rdtgrp) {
468 ret = -ENOENT;
469 goto unlock;
470 }
471
Reinette Chatrec966dac2018-06-22 15:42:12 -0700472 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
473 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
474 ret = -EINVAL;
Babu Moger723f1a0d2018-11-21 20:28:43 +0000475 rdt_last_cmd_puts("Pseudo-locking in progress\n");
Reinette Chatrec966dac2018-06-22 15:42:12 -0700476 goto unlock;
477 }
478
Jiri Olsa4ffa3c92017-04-10 16:52:32 +0200479 if (is_cpu_list(of))
480 ret = cpulist_parse(buf, newmask);
481 else
482 ret = cpumask_parse(buf, newmask);
483
Tony Luck94457b32017-09-25 16:39:36 -0700484 if (ret) {
Babu Moger723f1a0d2018-11-21 20:28:43 +0000485 rdt_last_cmd_puts("Bad CPU list/mask\n");
Tony Luck12e01102016-10-28 15:04:45 -0700486 goto unlock;
Tony Luck94457b32017-09-25 16:39:36 -0700487 }
Tony Luck12e01102016-10-28 15:04:45 -0700488
Tony Luck12e01102016-10-28 15:04:45 -0700489 /* check that user didn't specify any offline cpus */
490 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
Yury Norov3a5ff1f2022-02-10 14:49:00 -0800491 if (!cpumask_empty(tmpmask)) {
Tony Luck12e01102016-10-28 15:04:45 -0700492 ret = -EINVAL;
Babu Moger723f1a0d2018-11-21 20:28:43 +0000493 rdt_last_cmd_puts("Can only assign online CPUs\n");
Thomas Gleixnera2584e12016-11-15 15:12:13 +0100494 goto unlock;
Tony Luck12e01102016-10-28 15:04:45 -0700495 }
496
Vikas Shivappab09d9812017-07-25 14:14:35 -0700497 if (rdtgrp->type == RDTCTRL_GROUP)
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700498 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
499 else if (rdtgrp->type == RDTMON_GROUP)
500 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
Vikas Shivappab09d9812017-07-25 14:14:35 -0700501 else
502 ret = -EINVAL;
Tony Luck12e01102016-10-28 15:04:45 -0700503
Tony Luck12e01102016-10-28 15:04:45 -0700504unlock:
505 rdtgroup_kn_unlock(of->kn);
506 free_cpumask_var(tmpmask);
507 free_cpumask_var(newmask);
Vikas Shivappaa9fcf862017-07-25 14:14:36 -0700508 free_cpumask_var(tmpmask1);
Tony Luck12e01102016-10-28 15:04:45 -0700509
510 return ret ?: nbytes;
511}
512
Xiaochen Shen75899922020-10-31 03:11:28 +0800513/**
514 * rdtgroup_remove - the helper to remove resource group safely
515 * @rdtgrp: resource group to remove
516 *
517 * On resource group creation via a mkdir, an extra kernfs_node reference is
518 * taken to ensure that the rdtgroup structure remains accessible for the
519 * rdtgroup_kn_unlock() calls where it is removed.
520 *
521 * Drop the extra reference here, then free the rdtgroup structure.
522 *
523 * Return: void
524 */
525static void rdtgroup_remove(struct rdtgroup *rdtgrp)
526{
527 kernfs_put(rdtgrp->kn);
528 kfree(rdtgrp);
529}
530
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800531static void _update_task_closid_rmid(void *task)
Fenghua Yue02737d2016-10-28 15:04:46 -0700532{
Fenghua Yue02737d2016-10-28 15:04:46 -0700533 /*
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800534 * If the task is still current on this CPU, update PQR_ASSOC MSR.
535 * Otherwise, the MSR is updated when the task is scheduled in.
Fenghua Yue02737d2016-10-28 15:04:46 -0700536 */
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800537 if (task == current)
538 resctrl_sched_in();
539}
Fenghua Yue02737d2016-10-28 15:04:46 -0700540
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800541static void update_task_closid_rmid(struct task_struct *t)
542{
543 if (IS_ENABLED(CONFIG_SMP) && task_curr(t))
544 smp_call_function_single(task_cpu(t), _update_task_closid_rmid, t, 1);
545 else
546 _update_task_closid_rmid(t);
Fenghua Yue02737d2016-10-28 15:04:46 -0700547}
548
549static int __rdtgroup_move_task(struct task_struct *tsk,
550 struct rdtgroup *rdtgrp)
551{
Fenghua Yua0195f32020-12-17 14:31:19 -0800552 /* If the task is already in rdtgrp, no need to move the task. */
553 if ((rdtgrp->type == RDTCTRL_GROUP && tsk->closid == rdtgrp->closid &&
554 tsk->rmid == rdtgrp->mon.rmid) ||
555 (rdtgrp->type == RDTMON_GROUP && tsk->rmid == rdtgrp->mon.rmid &&
556 tsk->closid == rdtgrp->mon.parent->closid))
557 return 0;
558
Fenghua Yue02737d2016-10-28 15:04:46 -0700559 /*
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800560 * Set the task's closid/rmid before the PQR_ASSOC MSR can be
561 * updated by them.
562 *
563 * For ctrl_mon groups, move both closid and rmid.
564 * For monitor groups, can move the tasks only from
565 * their parent CTRL group.
Fenghua Yue02737d2016-10-28 15:04:46 -0700566 */
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800567
568 if (rdtgrp->type == RDTCTRL_GROUP) {
Valentin Schneider6d3b47d2020-12-17 14:31:21 -0800569 WRITE_ONCE(tsk->closid, rdtgrp->closid);
570 WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800571 } else if (rdtgrp->type == RDTMON_GROUP) {
572 if (rdtgrp->mon.parent->closid == tsk->closid) {
Valentin Schneider6d3b47d2020-12-17 14:31:21 -0800573 WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800574 } else {
575 rdt_last_cmd_puts("Can't move task to different control group\n");
576 return -EINVAL;
Vikas Shivappad6aaba62017-07-25 14:14:34 -0700577 }
Fenghua Yue02737d2016-10-28 15:04:46 -0700578 }
Fenghua Yuae28d1a2020-12-17 14:31:18 -0800579
580 /*
581 * Ensure the task's closid and rmid are written before determining if
582 * the task is current that will decide if it will be interrupted.
583 */
584 barrier();
585
586 /*
587 * By now, the task's closid and rmid are set. If the task is current
588 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
589 * group go into effect. If the task is not current, the MSR will be
590 * updated when the task is scheduled in.
591 */
592 update_task_closid_rmid(tsk);
593
594 return 0;
Fenghua Yue02737d2016-10-28 15:04:46 -0700595}
596
James Morsee6b2fac2020-07-08 16:39:25 +0000597static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
598{
599 return (rdt_alloc_capable &&
600 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
601}
602
603static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
604{
605 return (rdt_mon_capable &&
606 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
607}
608
Reinette Chatref7a6e3f2018-06-22 15:42:10 -0700609/**
610 * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
611 * @r: Resource group
612 *
613 * Return: 1 if tasks have been assigned to @r, 0 otherwise
614 */
615int rdtgroup_tasks_assigned(struct rdtgroup *r)
616{
617 struct task_struct *p, *t;
618 int ret = 0;
619
620 lockdep_assert_held(&rdtgroup_mutex);
621
622 rcu_read_lock();
623 for_each_process_thread(p, t) {
James Morsee6b2fac2020-07-08 16:39:25 +0000624 if (is_closid_match(t, r) || is_rmid_match(t, r)) {
Reinette Chatref7a6e3f2018-06-22 15:42:10 -0700625 ret = 1;
626 break;
627 }
628 }
629 rcu_read_unlock();
630
631 return ret;
632}
633
Fenghua Yue02737d2016-10-28 15:04:46 -0700634static int rdtgroup_task_write_permission(struct task_struct *task,
635 struct kernfs_open_file *of)
636{
637 const struct cred *tcred = get_task_cred(task);
638 const struct cred *cred = current_cred();
639 int ret = 0;
640
641 /*
642 * Even if we're attaching all tasks in the thread group, we only
643 * need to check permissions on one of them.
644 */
645 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
646 !uid_eq(cred->euid, tcred->uid) &&
Tony Luck29e74f32017-09-25 16:39:35 -0700647 !uid_eq(cred->euid, tcred->suid)) {
648 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
Fenghua Yue02737d2016-10-28 15:04:46 -0700649 ret = -EPERM;
Tony Luck29e74f32017-09-25 16:39:35 -0700650 }
Fenghua Yue02737d2016-10-28 15:04:46 -0700651
652 put_cred(tcred);
653 return ret;
654}
655
656static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
657 struct kernfs_open_file *of)
658{
659 struct task_struct *tsk;
660 int ret;
661
662 rcu_read_lock();
663 if (pid) {
664 tsk = find_task_by_vpid(pid);
665 if (!tsk) {
666 rcu_read_unlock();
Tony Luck29e74f32017-09-25 16:39:35 -0700667 rdt_last_cmd_printf("No task %d\n", pid);
Fenghua Yue02737d2016-10-28 15:04:46 -0700668 return -ESRCH;
669 }
670 } else {
671 tsk = current;
672 }
673
674 get_task_struct(tsk);
675 rcu_read_unlock();
676
677 ret = rdtgroup_task_write_permission(tsk, of);
678 if (!ret)
679 ret = __rdtgroup_move_task(tsk, rdtgrp);
680
681 put_task_struct(tsk);
682 return ret;
683}
684
685static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
686 char *buf, size_t nbytes, loff_t off)
687{
688 struct rdtgroup *rdtgrp;
689 int ret = 0;
690 pid_t pid;
691
692 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
693 return -EINVAL;
694 rdtgrp = rdtgroup_kn_lock_live(of->kn);
Reinette Chatrec966dac2018-06-22 15:42:12 -0700695 if (!rdtgrp) {
696 rdtgroup_kn_unlock(of->kn);
697 return -ENOENT;
698 }
Tony Luck29e74f32017-09-25 16:39:35 -0700699 rdt_last_cmd_clear();
Fenghua Yue02737d2016-10-28 15:04:46 -0700700
Reinette Chatrec966dac2018-06-22 15:42:12 -0700701 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
702 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
703 ret = -EINVAL;
Babu Moger723f1a0d2018-11-21 20:28:43 +0000704 rdt_last_cmd_puts("Pseudo-locking in progress\n");
Reinette Chatrec966dac2018-06-22 15:42:12 -0700705 goto unlock;
706 }
Fenghua Yue02737d2016-10-28 15:04:46 -0700707
Reinette Chatrec966dac2018-06-22 15:42:12 -0700708 ret = rdtgroup_move_task(pid, rdtgrp, of);
709
710unlock:
Fenghua Yue02737d2016-10-28 15:04:46 -0700711 rdtgroup_kn_unlock(of->kn);
712
713 return ret ?: nbytes;
714}
715
716static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
717{
718 struct task_struct *p, *t;
719
720 rcu_read_lock();
721 for_each_process_thread(p, t) {
James Morsee6b2fac2020-07-08 16:39:25 +0000722 if (is_closid_match(t, r) || is_rmid_match(t, r))
Fenghua Yue02737d2016-10-28 15:04:46 -0700723 seq_printf(s, "%d\n", t->pid);
724 }
725 rcu_read_unlock();
726}
727
728static int rdtgroup_tasks_show(struct kernfs_open_file *of,
729 struct seq_file *s, void *v)
730{
731 struct rdtgroup *rdtgrp;
732 int ret = 0;
733
734 rdtgrp = rdtgroup_kn_lock_live(of->kn);
735 if (rdtgrp)
736 show_rdt_tasks(rdtgrp, s);
737 else
738 ret = -ENOENT;
739 rdtgroup_kn_unlock(of->kn);
740
741 return ret;
742}
743
Chen Yue79f15a2020-01-15 17:28:51 +0800744#ifdef CONFIG_PROC_CPU_RESCTRL
745
746/*
747 * A task can only be part of one resctrl control group and of one monitor
748 * group which is associated to that control group.
749 *
750 * 1) res:
751 * mon:
752 *
753 * resctrl is not available.
754 *
755 * 2) res:/
756 * mon:
757 *
758 * Task is part of the root resctrl control group, and it is not associated
759 * to any monitor group.
760 *
761 * 3) res:/
762 * mon:mon0
763 *
764 * Task is part of the root resctrl control group and monitor group mon0.
765 *
766 * 4) res:group0
767 * mon:
768 *
769 * Task is part of resctrl control group group0, and it is not associated
770 * to any monitor group.
771 *
772 * 5) res:group0
773 * mon:mon1
774 *
775 * Task is part of resctrl control group group0 and monitor group mon1.
776 */
777int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
778 struct pid *pid, struct task_struct *tsk)
779{
780 struct rdtgroup *rdtg;
781 int ret = 0;
782
783 mutex_lock(&rdtgroup_mutex);
784
785 /* Return empty if resctrl has not been mounted. */
786 if (!static_branch_unlikely(&rdt_enable_key)) {
787 seq_puts(s, "res:\nmon:\n");
788 goto unlock;
789 }
790
791 list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
792 struct rdtgroup *crg;
793
794 /*
795 * Task information is only relevant for shareable
796 * and exclusive groups.
797 */
798 if (rdtg->mode != RDT_MODE_SHAREABLE &&
799 rdtg->mode != RDT_MODE_EXCLUSIVE)
800 continue;
801
802 if (rdtg->closid != tsk->closid)
803 continue;
804
805 seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
806 rdtg->kn->name);
807 seq_puts(s, "mon:");
808 list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
809 mon.crdtgrp_list) {
810 if (tsk->rmid != crg->mon.rmid)
811 continue;
812 seq_printf(s, "%s", crg->kn->name);
813 break;
814 }
815 seq_putc(s, '\n');
816 goto unlock;
817 }
818 /*
819 * The above search should succeed. Otherwise return
820 * with an error.
821 */
822 ret = -ENOENT;
823unlock:
824 mutex_unlock(&rdtgroup_mutex);
825
826 return ret;
827}
828#endif
829
Tony Luck9b3a7fd2017-09-25 16:39:33 -0700830static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
831 struct seq_file *seq, void *v)
832{
833 int len;
834
835 mutex_lock(&rdtgroup_mutex);
836 len = seq_buf_used(&last_cmd_status);
837 if (len)
838 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
839 else
840 seq_puts(seq, "ok\n");
841 mutex_unlock(&rdtgroup_mutex);
842 return 0;
843}
844
Fenghua Yu4e978d02016-10-28 15:04:43 -0700845static int rdt_num_closids_show(struct kernfs_open_file *of,
846 struct seq_file *seq, void *v)
847{
James Morsef2594492021-07-28 17:06:17 +0000848 struct resctrl_schema *s = of->kn->parent->priv;
Fenghua Yu4e978d02016-10-28 15:04:43 -0700849
James Morse3183e872021-07-28 17:06:20 +0000850 seq_printf(seq, "%u\n", s->num_closid);
Fenghua Yu4e978d02016-10-28 15:04:43 -0700851 return 0;
852}
853
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700854static int rdt_default_ctrl_show(struct kernfs_open_file *of,
Fenghua Yu4e978d02016-10-28 15:04:43 -0700855 struct seq_file *seq, void *v)
856{
James Morsef2594492021-07-28 17:06:17 +0000857 struct resctrl_schema *s = of->kn->parent->priv;
858 struct rdt_resource *r = s->res;
Fenghua Yu4e978d02016-10-28 15:04:43 -0700859
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700860 seq_printf(seq, "%x\n", r->default_ctrl);
Fenghua Yu4e978d02016-10-28 15:04:43 -0700861 return 0;
862}
863
Shaohua Li53a114a2016-11-03 14:09:06 -0700864static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
865 struct seq_file *seq, void *v)
866{
James Morsef2594492021-07-28 17:06:17 +0000867 struct resctrl_schema *s = of->kn->parent->priv;
868 struct rdt_resource *r = s->res;
Shaohua Li53a114a2016-11-03 14:09:06 -0700869
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200870 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
Vikas Shivappadb69ef62017-04-07 17:33:55 -0700871 return 0;
872}
Shaohua Li53a114a2016-11-03 14:09:06 -0700873
Fenghua Yu0dd2d742017-07-25 15:39:04 -0700874static int rdt_shareable_bits_show(struct kernfs_open_file *of,
875 struct seq_file *seq, void *v)
876{
James Morsef2594492021-07-28 17:06:17 +0000877 struct resctrl_schema *s = of->kn->parent->priv;
878 struct rdt_resource *r = s->res;
Fenghua Yu0dd2d742017-07-25 15:39:04 -0700879
880 seq_printf(seq, "%x\n", r->cache.shareable_bits);
881 return 0;
882}
883
Reinette Chatree6519012018-06-22 15:42:05 -0700884/**
885 * rdt_bit_usage_show - Display current usage of resources
886 *
887 * A domain is a shared resource that can now be allocated differently. Here
888 * we display the current regions of the domain as an annotated bitmask.
889 * For each domain of this resource its allocation bitmask
890 * is annotated as below to indicate the current usage of the corresponding bit:
891 * 0 - currently unused
892 * X - currently available for sharing and used by software and hardware
893 * H - currently used by hardware only but available for software use
894 * S - currently used and shareable by software only
895 * E - currently used exclusively by one resource group
Reinette Chatref4e80d62018-06-22 15:42:23 -0700896 * P - currently pseudo-locked by one resource group
Reinette Chatree6519012018-06-22 15:42:05 -0700897 */
898static int rdt_bit_usage_show(struct kernfs_open_file *of,
899 struct seq_file *seq, void *v)
900{
James Morsef2594492021-07-28 17:06:17 +0000901 struct resctrl_schema *s = of->kn->parent->priv;
Reinette Chatre32f010d2019-06-19 13:27:16 -0700902 /*
903 * Use unsigned long even though only 32 bits are used to ensure
904 * test_bit() is used safely.
905 */
906 unsigned long sw_shareable = 0, hw_shareable = 0;
907 unsigned long exclusive = 0, pseudo_locked = 0;
James Morsef2594492021-07-28 17:06:17 +0000908 struct rdt_resource *r = s->res;
Reinette Chatree6519012018-06-22 15:42:05 -0700909 struct rdt_domain *dom;
Reinette Chatref4e80d62018-06-22 15:42:23 -0700910 int i, hwb, swb, excl, psl;
Reinette Chatree6519012018-06-22 15:42:05 -0700911 enum rdtgrp_mode mode;
912 bool sep = false;
James Morsef07e9d02021-07-28 17:06:29 +0000913 u32 ctrl_val;
Reinette Chatree6519012018-06-22 15:42:05 -0700914
915 mutex_lock(&rdtgroup_mutex);
916 hw_shareable = r->cache.shareable_bits;
917 list_for_each_entry(dom, &r->domains, list) {
918 if (sep)
919 seq_putc(seq, ';');
Reinette Chatree6519012018-06-22 15:42:05 -0700920 sw_shareable = 0;
921 exclusive = 0;
922 seq_printf(seq, "%d=", dom->id);
James Morsef07e9d02021-07-28 17:06:29 +0000923 for (i = 0; i < closids_supported(); i++) {
Reinette Chatree6519012018-06-22 15:42:05 -0700924 if (!closid_allocated(i))
925 continue;
James Morse111136e2021-08-11 16:38:31 +0000926 ctrl_val = resctrl_arch_get_config(r, dom, i,
927 s->conf_type);
Reinette Chatree6519012018-06-22 15:42:05 -0700928 mode = rdtgroup_mode_by_closid(i);
929 switch (mode) {
930 case RDT_MODE_SHAREABLE:
James Morsef07e9d02021-07-28 17:06:29 +0000931 sw_shareable |= ctrl_val;
Reinette Chatree6519012018-06-22 15:42:05 -0700932 break;
933 case RDT_MODE_EXCLUSIVE:
James Morsef07e9d02021-07-28 17:06:29 +0000934 exclusive |= ctrl_val;
Reinette Chatree6519012018-06-22 15:42:05 -0700935 break;
Reinette Chatrebb9fec62018-06-22 15:42:08 -0700936 case RDT_MODE_PSEUDO_LOCKSETUP:
Reinette Chatref4e80d62018-06-22 15:42:23 -0700937 /*
938 * RDT_MODE_PSEUDO_LOCKSETUP is possible
939 * here but not included since the CBM
940 * associated with this CLOSID in this mode
941 * is not initialized and no task or cpu can be
942 * assigned this CLOSID.
943 */
944 break;
Reinette Chatrebb9fec62018-06-22 15:42:08 -0700945 case RDT_MODE_PSEUDO_LOCKED:
Reinette Chatree6519012018-06-22 15:42:05 -0700946 case RDT_NUM_MODES:
947 WARN(1,
948 "invalid mode for closid %d\n", i);
949 break;
950 }
951 }
952 for (i = r->cache.cbm_len - 1; i >= 0; i--) {
Reinette Chatref4e80d62018-06-22 15:42:23 -0700953 pseudo_locked = dom->plr ? dom->plr->cbm : 0;
Reinette Chatre32f010d2019-06-19 13:27:16 -0700954 hwb = test_bit(i, &hw_shareable);
955 swb = test_bit(i, &sw_shareable);
956 excl = test_bit(i, &exclusive);
957 psl = test_bit(i, &pseudo_locked);
Reinette Chatree6519012018-06-22 15:42:05 -0700958 if (hwb && swb)
959 seq_putc(seq, 'X');
960 else if (hwb && !swb)
961 seq_putc(seq, 'H');
962 else if (!hwb && swb)
963 seq_putc(seq, 'S');
964 else if (excl)
965 seq_putc(seq, 'E');
Reinette Chatref4e80d62018-06-22 15:42:23 -0700966 else if (psl)
967 seq_putc(seq, 'P');
Reinette Chatree6519012018-06-22 15:42:05 -0700968 else /* Unused bits remain */
969 seq_putc(seq, '0');
970 }
971 sep = true;
972 }
973 seq_putc(seq, '\n');
974 mutex_unlock(&rdtgroup_mutex);
975 return 0;
976}
977
Vikas Shivappadb69ef62017-04-07 17:33:55 -0700978static int rdt_min_bw_show(struct kernfs_open_file *of,
979 struct seq_file *seq, void *v)
980{
James Morsef2594492021-07-28 17:06:17 +0000981 struct resctrl_schema *s = of->kn->parent->priv;
982 struct rdt_resource *r = s->res;
Vikas Shivappadb69ef62017-04-07 17:33:55 -0700983
984 seq_printf(seq, "%u\n", r->membw.min_bw);
985 return 0;
986}
987
Vikas Shivappad4ab3322017-07-25 14:14:30 -0700988static int rdt_num_rmids_show(struct kernfs_open_file *of,
989 struct seq_file *seq, void *v)
990{
991 struct rdt_resource *r = of->kn->parent->priv;
992
993 seq_printf(seq, "%d\n", r->num_rmid);
994
995 return 0;
996}
997
998static int rdt_mon_features_show(struct kernfs_open_file *of,
999 struct seq_file *seq, void *v)
1000{
1001 struct rdt_resource *r = of->kn->parent->priv;
1002 struct mon_evt *mevt;
1003
1004 list_for_each_entry(mevt, &r->evt_list, list)
1005 seq_printf(seq, "%s\n", mevt->name);
1006
1007 return 0;
1008}
1009
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001010static int rdt_bw_gran_show(struct kernfs_open_file *of,
1011 struct seq_file *seq, void *v)
1012{
James Morsef2594492021-07-28 17:06:17 +00001013 struct resctrl_schema *s = of->kn->parent->priv;
1014 struct rdt_resource *r = s->res;
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001015
1016 seq_printf(seq, "%u\n", r->membw.bw_gran);
1017 return 0;
1018}
1019
1020static int rdt_delay_linear_show(struct kernfs_open_file *of,
1021 struct seq_file *seq, void *v)
1022{
James Morsef2594492021-07-28 17:06:17 +00001023 struct resctrl_schema *s = of->kn->parent->priv;
1024 struct rdt_resource *r = s->res;
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001025
1026 seq_printf(seq, "%u\n", r->membw.delay_linear);
Shaohua Li53a114a2016-11-03 14:09:06 -07001027 return 0;
1028}
1029
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001030static int max_threshold_occ_show(struct kernfs_open_file *of,
1031 struct seq_file *seq, void *v)
1032{
1033 struct rdt_resource *r = of->kn->parent->priv;
James Morse63c8b122021-07-28 17:06:14 +00001034 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001035
James Morse63c8b122021-07-28 17:06:14 +00001036 seq_printf(seq, "%u\n", resctrl_cqm_threshold * hw_res->mon_scale);
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001037
1038 return 0;
1039}
1040
Fenghua Yu29b6bd42020-08-24 12:11:21 -07001041static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
1042 struct seq_file *seq, void *v)
1043{
James Morsef2594492021-07-28 17:06:17 +00001044 struct resctrl_schema *s = of->kn->parent->priv;
1045 struct rdt_resource *r = s->res;
Fenghua Yu29b6bd42020-08-24 12:11:21 -07001046
1047 if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
1048 seq_puts(seq, "per-thread\n");
1049 else
1050 seq_puts(seq, "max\n");
1051
1052 return 0;
1053}
1054
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001055static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1056 char *buf, size_t nbytes, loff_t off)
1057{
James Morse63c8b122021-07-28 17:06:14 +00001058 struct rdt_hw_resource *hw_res;
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001059 unsigned int bytes;
1060 int ret;
1061
1062 ret = kstrtouint(buf, 0, &bytes);
1063 if (ret)
1064 return ret;
1065
1066 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
1067 return -EINVAL;
1068
James Morse63c8b122021-07-28 17:06:14 +00001069 hw_res = resctrl_to_arch_res(of->kn->parent->priv);
1070 resctrl_cqm_threshold = bytes / hw_res->mon_scale;
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001071
Colin Ian King5707b462017-08-08 10:28:59 +01001072 return nbytes;
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001073}
1074
Reinette Chatred48d7a52018-06-22 15:41:56 -07001075/*
1076 * rdtgroup_mode_show - Display mode of this resource group
1077 */
1078static int rdtgroup_mode_show(struct kernfs_open_file *of,
1079 struct seq_file *s, void *v)
1080{
1081 struct rdtgroup *rdtgrp;
1082
1083 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1084 if (!rdtgrp) {
1085 rdtgroup_kn_unlock(of->kn);
1086 return -ENOENT;
1087 }
1088
1089 seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1090
1091 rdtgroup_kn_unlock(of->kn);
1092 return 0;
1093}
1094
James Morsefbc06c62021-07-28 17:06:35 +00001095static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
Reinette Chatre521348b2018-10-03 15:17:01 -07001096{
James Morsefbc06c62021-07-28 17:06:35 +00001097 switch (my_type) {
1098 case CDP_CODE:
1099 return CDP_DATA;
1100 case CDP_DATA:
1101 return CDP_CODE;
Reinette Chatre521348b2018-10-03 15:17:01 -07001102 default:
James Morsefbc06c62021-07-28 17:06:35 +00001103 case CDP_NONE:
1104 return CDP_NONE;
Reinette Chatre521348b2018-10-03 15:17:01 -07001105 }
Reinette Chatre521348b2018-10-03 15:17:01 -07001106}
1107
1108/**
Reinette Chatree5f35302018-10-03 15:17:02 -07001109 * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001110 * @r: Resource to which domain instance @d belongs.
1111 * @d: The domain instance for which @closid is being tested.
1112 * @cbm: Capacity bitmask being tested.
1113 * @closid: Intended closid for @cbm.
1114 * @exclusive: Only check if overlaps with exclusive resource groups
1115 *
1116 * Checks if provided @cbm intended to be used for @closid on domain
1117 * @d overlaps with any other closids or other hardware usage associated
1118 * with this domain. If @exclusive is true then only overlaps with
1119 * resource groups in exclusive mode will be considered. If @exclusive
1120 * is false then overlaps with any resource group or hardware entities
1121 * will be considered.
1122 *
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001123 * @cbm is unsigned long, even if only 32 bits are used, to make the
1124 * bitmap functions work correctly.
1125 *
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001126 * Return: false if CBM does not overlap, true if it does.
1127 */
Reinette Chatree5f35302018-10-03 15:17:02 -07001128static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
James Morsefa8f7112021-07-28 17:06:30 +00001129 unsigned long cbm, int closid,
1130 enum resctrl_conf_type type, bool exclusive)
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001131{
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001132 enum rdtgrp_mode mode;
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001133 unsigned long ctrl_b;
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001134 int i;
1135
1136 /* Check for any overlap with regions used by hardware directly */
1137 if (!exclusive) {
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001138 ctrl_b = r->cache.shareable_bits;
1139 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001140 return true;
1141 }
1142
1143 /* Check for overlap with other resource groups */
James Morsef07e9d02021-07-28 17:06:29 +00001144 for (i = 0; i < closids_supported(); i++) {
James Morse111136e2021-08-11 16:38:31 +00001145 ctrl_b = resctrl_arch_get_config(r, d, i, type);
Reinette Chatredfe96742018-06-22 15:42:17 -07001146 mode = rdtgroup_mode_by_closid(i);
1147 if (closid_allocated(i) && i != closid &&
1148 mode != RDT_MODE_PSEUDO_LOCKSETUP) {
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001149 if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001150 if (exclusive) {
1151 if (mode == RDT_MODE_EXCLUSIVE)
1152 return true;
1153 continue;
1154 }
1155 return true;
1156 }
1157 }
1158 }
1159
1160 return false;
1161}
1162
1163/**
Reinette Chatree5f35302018-10-03 15:17:02 -07001164 * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
James Morse1c2906822021-07-28 17:06:22 +00001165 * @s: Schema for the resource to which domain instance @d belongs.
Reinette Chatree5f35302018-10-03 15:17:02 -07001166 * @d: The domain instance for which @closid is being tested.
1167 * @cbm: Capacity bitmask being tested.
1168 * @closid: Intended closid for @cbm.
1169 * @exclusive: Only check if overlaps with exclusive resource groups
1170 *
1171 * Resources that can be allocated using a CBM can use the CBM to control
1172 * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1173 * for overlap. Overlap test is not limited to the specific resource for
1174 * which the CBM is intended though - when dealing with CDP resources that
1175 * share the underlying hardware the overlap check should be performed on
1176 * the CDP resource sharing the hardware also.
1177 *
1178 * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1179 * overlap test.
1180 *
1181 * Return: true if CBM overlap detected, false if there is no overlap
1182 */
James Morse1c2906822021-07-28 17:06:22 +00001183bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
Reinette Chatree5f35302018-10-03 15:17:02 -07001184 unsigned long cbm, int closid, bool exclusive)
1185{
James Morsefbc06c62021-07-28 17:06:35 +00001186 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
James Morse1c2906822021-07-28 17:06:22 +00001187 struct rdt_resource *r = s->res;
Reinette Chatree5f35302018-10-03 15:17:02 -07001188
James Morsefa8f7112021-07-28 17:06:30 +00001189 if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
1190 exclusive))
Reinette Chatree5f35302018-10-03 15:17:02 -07001191 return true;
1192
James Morsefbc06c62021-07-28 17:06:35 +00001193 if (!resctrl_arch_get_cdp_enabled(r->rid))
Reinette Chatree5f35302018-10-03 15:17:02 -07001194 return false;
James Morsefbc06c62021-07-28 17:06:35 +00001195 return __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
Reinette Chatree5f35302018-10-03 15:17:02 -07001196}
1197
1198/**
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001199 * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1200 *
1201 * An exclusive resource group implies that there should be no sharing of
1202 * its allocated resources. At the time this group is considered to be
1203 * exclusive this test can determine if its current schemata supports this
1204 * setting by testing for overlap with all other resource groups.
1205 *
1206 * Return: true if resource group can be exclusive, false if there is overlap
1207 * with allocations of other resource groups and thus this resource group
1208 * cannot be exclusive.
1209 */
1210static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1211{
1212 int closid = rdtgrp->closid;
James Morse331ebe42021-07-28 17:06:19 +00001213 struct resctrl_schema *s;
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001214 struct rdt_resource *r;
Reinette Chatre939b90b2018-09-15 14:58:26 -07001215 bool has_cache = false;
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001216 struct rdt_domain *d;
James Morsef07e9d02021-07-28 17:06:29 +00001217 u32 ctrl;
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001218
James Morse331ebe42021-07-28 17:06:19 +00001219 list_for_each_entry(s, &resctrl_schema_all, list) {
1220 r = s->res;
Reinette Chatre939b90b2018-09-15 14:58:26 -07001221 if (r->rid == RDT_RESOURCE_MBA)
1222 continue;
1223 has_cache = true;
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001224 list_for_each_entry(d, &r->domains, list) {
James Morse111136e2021-08-11 16:38:31 +00001225 ctrl = resctrl_arch_get_config(r, d, closid,
1226 s->conf_type);
James Morsef07e9d02021-07-28 17:06:29 +00001227 if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
Babu Moger723f1a0d2018-11-21 20:28:43 +00001228 rdt_last_cmd_puts("Schemata overlaps\n");
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001229 return false;
Reinette Chatre939b90b2018-09-15 14:58:26 -07001230 }
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001231 }
1232 }
1233
Reinette Chatre939b90b2018-09-15 14:58:26 -07001234 if (!has_cache) {
Babu Moger723f1a0d2018-11-21 20:28:43 +00001235 rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
Reinette Chatre939b90b2018-09-15 14:58:26 -07001236 return false;
1237 }
1238
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001239 return true;
1240}
1241
1242/**
1243 * rdtgroup_mode_write - Modify the resource group's mode
1244 *
1245 */
Reinette Chatred48d7a52018-06-22 15:41:56 -07001246static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1247 char *buf, size_t nbytes, loff_t off)
1248{
1249 struct rdtgroup *rdtgrp;
1250 enum rdtgrp_mode mode;
1251 int ret = 0;
1252
1253 /* Valid input requires a trailing newline */
1254 if (nbytes == 0 || buf[nbytes - 1] != '\n')
1255 return -EINVAL;
1256 buf[nbytes - 1] = '\0';
1257
1258 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1259 if (!rdtgrp) {
1260 rdtgroup_kn_unlock(of->kn);
1261 return -ENOENT;
1262 }
1263
1264 rdt_last_cmd_clear();
1265
1266 mode = rdtgrp->mode;
1267
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001268 if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
Reinette Chatredfe96742018-06-22 15:42:17 -07001269 (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1270 (!strcmp(buf, "pseudo-locksetup") &&
1271 mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1272 (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
Reinette Chatred48d7a52018-06-22 15:41:56 -07001273 goto out;
1274
Reinette Chatredfe96742018-06-22 15:42:17 -07001275 if (mode == RDT_MODE_PSEUDO_LOCKED) {
Reinette Chatre456824892018-11-27 11:19:36 -08001276 rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
Reinette Chatredfe96742018-06-22 15:42:17 -07001277 ret = -EINVAL;
1278 goto out;
1279 }
1280
Reinette Chatred48d7a52018-06-22 15:41:56 -07001281 if (!strcmp(buf, "shareable")) {
Reinette Chatredfe96742018-06-22 15:42:17 -07001282 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1283 ret = rdtgroup_locksetup_exit(rdtgrp);
1284 if (ret)
1285 goto out;
1286 }
Reinette Chatred48d7a52018-06-22 15:41:56 -07001287 rdtgrp->mode = RDT_MODE_SHAREABLE;
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001288 } else if (!strcmp(buf, "exclusive")) {
1289 if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001290 ret = -EINVAL;
1291 goto out;
1292 }
Reinette Chatredfe96742018-06-22 15:42:17 -07001293 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1294 ret = rdtgroup_locksetup_exit(rdtgrp);
1295 if (ret)
1296 goto out;
1297 }
Reinette Chatre49f7b4e2018-06-22 15:42:01 -07001298 rdtgrp->mode = RDT_MODE_EXCLUSIVE;
Reinette Chatredfe96742018-06-22 15:42:17 -07001299 } else if (!strcmp(buf, "pseudo-locksetup")) {
1300 ret = rdtgroup_locksetup_enter(rdtgrp);
1301 if (ret)
1302 goto out;
1303 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
Reinette Chatred48d7a52018-06-22 15:41:56 -07001304 } else {
Reinette Chatre456824892018-11-27 11:19:36 -08001305 rdt_last_cmd_puts("Unknown or unsupported mode\n");
Reinette Chatred48d7a52018-06-22 15:41:56 -07001306 ret = -EINVAL;
1307 }
1308
1309out:
1310 rdtgroup_kn_unlock(of->kn);
1311 return ret ?: nbytes;
1312}
1313
Reinette Chatred9b48c82018-06-22 15:42:06 -07001314/**
1315 * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1316 * @r: RDT resource to which @d belongs.
1317 * @d: RDT domain instance.
1318 * @cbm: bitmask for which the size should be computed.
1319 *
1320 * The bitmask provided associated with the RDT domain instance @d will be
1321 * translated into how many bytes it represents. The size in bytes is
1322 * computed by first dividing the total cache size by the CBM length to
1323 * determine how many bytes each bit in the bitmask represents. The result
1324 * is multiplied with the number of bits set in the bitmask.
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001325 *
1326 * @cbm is unsigned long, even if only 32 bits are used to make the
1327 * bitmap functions work correctly.
Reinette Chatred9b48c82018-06-22 15:42:06 -07001328 */
1329unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001330 struct rdt_domain *d, unsigned long cbm)
Reinette Chatred9b48c82018-06-22 15:42:06 -07001331{
1332 struct cpu_cacheinfo *ci;
1333 unsigned int size = 0;
1334 int num_b, i;
1335
Reinette Chatre49e00eee2018-10-04 14:05:23 -07001336 num_b = bitmap_weight(&cbm, r->cache.cbm_len);
Reinette Chatred9b48c82018-06-22 15:42:06 -07001337 ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1338 for (i = 0; i < ci->num_leaves; i++) {
1339 if (ci->info_list[i].level == r->cache_level) {
1340 size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1341 break;
1342 }
1343 }
1344
1345 return size;
1346}
1347
1348/**
1349 * rdtgroup_size_show - Display size in bytes of allocated regions
1350 *
1351 * The "size" file mirrors the layout of the "schemata" file, printing the
1352 * size in bytes of each region instead of the capacity bitmask.
1353 *
1354 */
1355static int rdtgroup_size_show(struct kernfs_open_file *of,
1356 struct seq_file *s, void *v)
1357{
James Morse331ebe42021-07-28 17:06:19 +00001358 struct resctrl_schema *schema;
Reinette Chatred9b48c82018-06-22 15:42:06 -07001359 struct rdtgroup *rdtgrp;
1360 struct rdt_resource *r;
1361 struct rdt_domain *d;
1362 unsigned int size;
Jithu Josephb61b8bb2018-10-12 15:51:01 -07001363 int ret = 0;
Reinette Chatref968dc12018-09-15 14:58:20 -07001364 bool sep;
1365 u32 ctrl;
Reinette Chatred9b48c82018-06-22 15:42:06 -07001366
1367 rdtgrp = rdtgroup_kn_lock_live(of->kn);
1368 if (!rdtgrp) {
1369 rdtgroup_kn_unlock(of->kn);
1370 return -ENOENT;
1371 }
1372
Reinette Chatref4e80d62018-06-22 15:42:23 -07001373 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
Jithu Josephb61b8bb2018-10-12 15:51:01 -07001374 if (!rdtgrp->plr->d) {
1375 rdt_last_cmd_clear();
1376 rdt_last_cmd_puts("Cache domain offline\n");
1377 ret = -ENODEV;
1378 } else {
1379 seq_printf(s, "%*s:", max_name_width,
James Morsee198fde2021-07-28 17:06:25 +00001380 rdtgrp->plr->s->name);
James Morse32150ed2021-07-28 17:06:23 +00001381 size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
Jithu Josephb61b8bb2018-10-12 15:51:01 -07001382 rdtgrp->plr->d,
1383 rdtgrp->plr->cbm);
1384 seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1385 }
Reinette Chatref4e80d62018-06-22 15:42:23 -07001386 goto out;
1387 }
1388
James Morse331ebe42021-07-28 17:06:19 +00001389 list_for_each_entry(schema, &resctrl_schema_all, list) {
1390 r = schema->res;
Reinette Chatref968dc12018-09-15 14:58:20 -07001391 sep = false;
James Morsee198fde2021-07-28 17:06:25 +00001392 seq_printf(s, "%*s:", max_name_width, schema->name);
Reinette Chatred9b48c82018-06-22 15:42:06 -07001393 list_for_each_entry(d, &r->domains, list) {
1394 if (sep)
1395 seq_putc(s, ';');
Reinette Chatredfe96742018-06-22 15:42:17 -07001396 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1397 size = 0;
1398 } else {
James Morse111136e2021-08-11 16:38:31 +00001399 ctrl = resctrl_arch_get_config(r, d,
1400 rdtgrp->closid,
1401 schema->conf_type);
Reinette Chatref968dc12018-09-15 14:58:20 -07001402 if (r->rid == RDT_RESOURCE_MBA)
1403 size = ctrl;
1404 else
1405 size = rdtgroup_cbm_to_size(r, d, ctrl);
Reinette Chatredfe96742018-06-22 15:42:17 -07001406 }
Reinette Chatred9b48c82018-06-22 15:42:06 -07001407 seq_printf(s, "%d=%u", d->id, size);
1408 sep = true;
1409 }
1410 seq_putc(s, '\n');
1411 }
1412
Reinette Chatref4e80d62018-06-22 15:42:23 -07001413out:
Reinette Chatred9b48c82018-06-22 15:42:06 -07001414 rdtgroup_kn_unlock(of->kn);
1415
Jithu Josephb61b8bb2018-10-12 15:51:01 -07001416 return ret;
Reinette Chatred9b48c82018-06-22 15:42:06 -07001417}
1418
Fenghua Yu4e978d02016-10-28 15:04:43 -07001419/* rdtgroup information files for one cache resource. */
Tony luck5dc1d5c2017-07-25 14:14:29 -07001420static struct rftype res_common_files[] = {
Fenghua Yu4e978d02016-10-28 15:04:43 -07001421 {
Tony Luck9b3a7fd2017-09-25 16:39:33 -07001422 .name = "last_cmd_status",
1423 .mode = 0444,
1424 .kf_ops = &rdtgroup_kf_single_ops,
1425 .seq_show = rdt_last_cmd_status_show,
1426 .fflags = RF_TOP_INFO,
1427 },
1428 {
Fenghua Yu4e978d02016-10-28 15:04:43 -07001429 .name = "num_closids",
1430 .mode = 0444,
1431 .kf_ops = &rdtgroup_kf_single_ops,
1432 .seq_show = rdt_num_closids_show,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001433 .fflags = RF_CTRL_INFO,
Fenghua Yu4e978d02016-10-28 15:04:43 -07001434 },
1435 {
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001436 .name = "mon_features",
1437 .mode = 0444,
1438 .kf_ops = &rdtgroup_kf_single_ops,
1439 .seq_show = rdt_mon_features_show,
1440 .fflags = RF_MON_INFO,
1441 },
1442 {
1443 .name = "num_rmids",
1444 .mode = 0444,
1445 .kf_ops = &rdtgroup_kf_single_ops,
1446 .seq_show = rdt_num_rmids_show,
1447 .fflags = RF_MON_INFO,
1448 },
1449 {
Fenghua Yu4e978d02016-10-28 15:04:43 -07001450 .name = "cbm_mask",
1451 .mode = 0444,
1452 .kf_ops = &rdtgroup_kf_single_ops,
Vikas Shivappa2545e9f2017-04-07 17:33:51 -07001453 .seq_show = rdt_default_ctrl_show,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001454 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
Fenghua Yu4e978d02016-10-28 15:04:43 -07001455 },
Shaohua Li53a114a2016-11-03 14:09:06 -07001456 {
1457 .name = "min_cbm_bits",
1458 .mode = 0444,
1459 .kf_ops = &rdtgroup_kf_single_ops,
1460 .seq_show = rdt_min_cbm_bits_show,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001461 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001462 },
1463 {
Fenghua Yu0dd2d742017-07-25 15:39:04 -07001464 .name = "shareable_bits",
1465 .mode = 0444,
1466 .kf_ops = &rdtgroup_kf_single_ops,
1467 .seq_show = rdt_shareable_bits_show,
1468 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1469 },
1470 {
Reinette Chatree6519012018-06-22 15:42:05 -07001471 .name = "bit_usage",
1472 .mode = 0444,
1473 .kf_ops = &rdtgroup_kf_single_ops,
1474 .seq_show = rdt_bit_usage_show,
1475 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
1476 },
1477 {
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001478 .name = "min_bandwidth",
1479 .mode = 0444,
1480 .kf_ops = &rdtgroup_kf_single_ops,
1481 .seq_show = rdt_min_bw_show,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001482 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001483 },
1484 {
1485 .name = "bandwidth_gran",
1486 .mode = 0444,
1487 .kf_ops = &rdtgroup_kf_single_ops,
1488 .seq_show = rdt_bw_gran_show,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001489 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001490 },
1491 {
1492 .name = "delay_linear",
1493 .mode = 0444,
1494 .kf_ops = &rdtgroup_kf_single_ops,
1495 .seq_show = rdt_delay_linear_show,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001496 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
1497 },
Fenghua Yu29b6bd42020-08-24 12:11:21 -07001498 /*
1499 * Platform specific which (if any) capabilities are provided by
1500 * thread_throttle_mode. Defer "fflags" initialization to platform
1501 * discovery.
1502 */
1503 {
1504 .name = "thread_throttle_mode",
1505 .mode = 0444,
1506 .kf_ops = &rdtgroup_kf_single_ops,
1507 .seq_show = rdt_thread_throttle_mode_show,
1508 },
Tony luck5dc1d5c2017-07-25 14:14:29 -07001509 {
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001510 .name = "max_threshold_occupancy",
1511 .mode = 0644,
1512 .kf_ops = &rdtgroup_kf_single_ops,
1513 .write = max_threshold_occ_write,
1514 .seq_show = max_threshold_occ_show,
1515 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
1516 },
1517 {
Tony luck5dc1d5c2017-07-25 14:14:29 -07001518 .name = "cpus",
1519 .mode = 0644,
1520 .kf_ops = &rdtgroup_kf_single_ops,
1521 .write = rdtgroup_cpus_write,
1522 .seq_show = rdtgroup_cpus_show,
1523 .fflags = RFTYPE_BASE,
1524 },
1525 {
1526 .name = "cpus_list",
1527 .mode = 0644,
1528 .kf_ops = &rdtgroup_kf_single_ops,
1529 .write = rdtgroup_cpus_write,
1530 .seq_show = rdtgroup_cpus_show,
1531 .flags = RFTYPE_FLAGS_CPUS_LIST,
1532 .fflags = RFTYPE_BASE,
1533 },
1534 {
1535 .name = "tasks",
1536 .mode = 0644,
1537 .kf_ops = &rdtgroup_kf_single_ops,
1538 .write = rdtgroup_tasks_write,
1539 .seq_show = rdtgroup_tasks_show,
1540 .fflags = RFTYPE_BASE,
1541 },
1542 {
1543 .name = "schemata",
1544 .mode = 0644,
1545 .kf_ops = &rdtgroup_kf_single_ops,
1546 .write = rdtgroup_schemata_write,
1547 .seq_show = rdtgroup_schemata_show,
1548 .fflags = RF_CTRL_BASE,
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001549 },
Reinette Chatred48d7a52018-06-22 15:41:56 -07001550 {
1551 .name = "mode",
1552 .mode = 0644,
1553 .kf_ops = &rdtgroup_kf_single_ops,
1554 .write = rdtgroup_mode_write,
1555 .seq_show = rdtgroup_mode_show,
1556 .fflags = RF_CTRL_BASE,
1557 },
Reinette Chatred9b48c82018-06-22 15:42:06 -07001558 {
1559 .name = "size",
1560 .mode = 0444,
1561 .kf_ops = &rdtgroup_kf_single_ops,
1562 .seq_show = rdtgroup_size_show,
1563 .fflags = RF_CTRL_BASE,
1564 },
1565
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001566};
1567
Tony luck5dc1d5c2017-07-25 14:14:29 -07001568static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001569{
Tony luck5dc1d5c2017-07-25 14:14:29 -07001570 struct rftype *rfts, *rft;
1571 int ret, len;
1572
1573 rfts = res_common_files;
1574 len = ARRAY_SIZE(res_common_files);
1575
1576 lockdep_assert_held(&rdtgroup_mutex);
1577
1578 for (rft = rfts; rft < rfts + len; rft++) {
Fenghua Yu29b6bd42020-08-24 12:11:21 -07001579 if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
Tony luck5dc1d5c2017-07-25 14:14:29 -07001580 ret = rdtgroup_add_file(kn, rft);
1581 if (ret)
1582 goto error;
1583 }
1584 }
1585
1586 return 0;
1587error:
1588 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1589 while (--rft >= rfts) {
1590 if ((fflags & rft->fflags) == rft->fflags)
1591 kernfs_remove_by_name(kn, rft->name);
1592 }
1593 return ret;
Vikas Shivappadb69ef62017-04-07 17:33:55 -07001594}
1595
Fenghua Yu29b6bd42020-08-24 12:11:21 -07001596static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
1597{
1598 struct rftype *rfts, *rft;
1599 int len;
1600
1601 rfts = res_common_files;
1602 len = ARRAY_SIZE(res_common_files);
1603
1604 for (rft = rfts; rft < rfts + len; rft++) {
1605 if (!strcmp(rft->name, name))
1606 return rft;
1607 }
1608
1609 return NULL;
1610}
1611
1612void __init thread_throttle_mode_init(void)
1613{
1614 struct rftype *rft;
1615
1616 rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
1617 if (!rft)
1618 return;
1619
1620 rft->fflags = RF_CTRL_INFO | RFTYPE_RES_MB;
1621}
1622
Reinette Chatre125db712018-06-22 15:42:11 -07001623/**
1624 * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1625 * @r: The resource group with which the file is associated.
1626 * @name: Name of the file
1627 *
1628 * The permissions of named resctrl file, directory, or link are modified
1629 * to not allow read, write, or execute by any user.
1630 *
1631 * WARNING: This function is intended to communicate to the user that the
1632 * resctrl file has been locked down - that it is not relevant to the
1633 * particular state the system finds itself in. It should not be relied
1634 * on to protect from user access because after the file's permissions
1635 * are restricted the user can still change the permissions using chmod
1636 * from the command line.
1637 *
1638 * Return: 0 on success, <0 on failure.
1639 */
1640int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1641{
1642 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1643 struct kernfs_node *kn;
1644 int ret = 0;
1645
1646 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1647 if (!kn)
1648 return -ENOENT;
1649
1650 switch (kernfs_type(kn)) {
1651 case KERNFS_DIR:
1652 iattr.ia_mode = S_IFDIR;
1653 break;
1654 case KERNFS_FILE:
1655 iattr.ia_mode = S_IFREG;
1656 break;
1657 case KERNFS_LINK:
1658 iattr.ia_mode = S_IFLNK;
1659 break;
1660 }
1661
1662 ret = kernfs_setattr(kn, &iattr);
1663 kernfs_put(kn);
1664 return ret;
1665}
1666
1667/**
1668 * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1669 * @r: The resource group with which the file is associated.
1670 * @name: Name of the file
Reinette Chatre392487de2018-06-30 22:17:32 -07001671 * @mask: Mask of permissions that should be restored
Reinette Chatre125db712018-06-22 15:42:11 -07001672 *
1673 * Restore the permissions of the named file. If @name is a directory the
1674 * permissions of its parent will be used.
1675 *
1676 * Return: 0 on success, <0 on failure.
1677 */
Reinette Chatre392487de2018-06-30 22:17:32 -07001678int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1679 umode_t mask)
Reinette Chatre125db712018-06-22 15:42:11 -07001680{
1681 struct iattr iattr = {.ia_valid = ATTR_MODE,};
1682 struct kernfs_node *kn, *parent;
1683 struct rftype *rfts, *rft;
1684 int ret, len;
1685
1686 rfts = res_common_files;
1687 len = ARRAY_SIZE(res_common_files);
1688
1689 for (rft = rfts; rft < rfts + len; rft++) {
1690 if (!strcmp(rft->name, name))
Reinette Chatre392487de2018-06-30 22:17:32 -07001691 iattr.ia_mode = rft->mode & mask;
Reinette Chatre125db712018-06-22 15:42:11 -07001692 }
1693
1694 kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1695 if (!kn)
1696 return -ENOENT;
1697
1698 switch (kernfs_type(kn)) {
1699 case KERNFS_DIR:
1700 parent = kernfs_get_parent(kn);
1701 if (parent) {
1702 iattr.ia_mode |= parent->mode;
1703 kernfs_put(parent);
1704 }
1705 iattr.ia_mode |= S_IFDIR;
1706 break;
1707 case KERNFS_FILE:
1708 iattr.ia_mode |= S_IFREG;
1709 break;
1710 case KERNFS_LINK:
1711 iattr.ia_mode |= S_IFLNK;
1712 break;
1713 }
1714
1715 ret = kernfs_setattr(kn, &iattr);
1716 kernfs_put(kn);
1717 return ret;
1718}
1719
James Morsef2594492021-07-28 17:06:17 +00001720static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
Tony luck5dc1d5c2017-07-25 14:14:29 -07001721 unsigned long fflags)
Vikas Shivappa6a507a62017-04-07 17:33:54 -07001722{
Tony luck5dc1d5c2017-07-25 14:14:29 -07001723 struct kernfs_node *kn_subdir;
1724 int ret;
1725
1726 kn_subdir = kernfs_create_dir(kn_info, name,
James Morsef2594492021-07-28 17:06:17 +00001727 kn_info->mode, priv);
Tony luck5dc1d5c2017-07-25 14:14:29 -07001728 if (IS_ERR(kn_subdir))
1729 return PTR_ERR(kn_subdir);
1730
Tony luck5dc1d5c2017-07-25 14:14:29 -07001731 ret = rdtgroup_kn_set_ugid(kn_subdir);
1732 if (ret)
1733 return ret;
1734
1735 ret = rdtgroup_add_files(kn_subdir, fflags);
1736 if (!ret)
1737 kernfs_activate(kn_subdir);
1738
1739 return ret;
Vikas Shivappa6a507a62017-04-07 17:33:54 -07001740}
1741
Fenghua Yu4e978d02016-10-28 15:04:43 -07001742static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
1743{
James Morsef2594492021-07-28 17:06:17 +00001744 struct resctrl_schema *s;
Fenghua Yu4e978d02016-10-28 15:04:43 -07001745 struct rdt_resource *r;
Tony luck5dc1d5c2017-07-25 14:14:29 -07001746 unsigned long fflags;
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001747 char name[32];
Tony luck5dc1d5c2017-07-25 14:14:29 -07001748 int ret;
Fenghua Yu4e978d02016-10-28 15:04:43 -07001749
1750 /* create the directory */
1751 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
1752 if (IS_ERR(kn_info))
1753 return PTR_ERR(kn_info);
Fenghua Yu4e978d02016-10-28 15:04:43 -07001754
Tony Luck9b3a7fd2017-09-25 16:39:33 -07001755 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
1756 if (ret)
1757 goto out_destroy;
1758
James Morsef2594492021-07-28 17:06:17 +00001759 /* loop over enabled controls, these are all alloc_enabled */
1760 list_for_each_entry(s, &resctrl_schema_all, list) {
1761 r = s->res;
Tony luck5dc1d5c2017-07-25 14:14:29 -07001762 fflags = r->fflags | RF_CTRL_INFO;
James Morsee198fde2021-07-28 17:06:25 +00001763 ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
Fenghua Yu4e978d02016-10-28 15:04:43 -07001764 if (ret)
1765 goto out_destroy;
Fenghua Yu4e978d02016-10-28 15:04:43 -07001766 }
Vikas Shivappad4ab3322017-07-25 14:14:30 -07001767
1768 for_each_mon_enabled_rdt_resource(r) {
1769 fflags = r->fflags | RF_MON_INFO;
1770 sprintf(name, "%s_MON", r->name);
1771 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
1772 if (ret)
1773 goto out_destroy;
1774 }
1775
Fenghua Yu4e978d02016-10-28 15:04:43 -07001776 ret = rdtgroup_kn_set_ugid(kn_info);
1777 if (ret)
1778 goto out_destroy;
1779
1780 kernfs_activate(kn_info);
1781
1782 return 0;
1783
1784out_destroy:
1785 kernfs_remove(kn_info);
1786 return ret;
1787}
1788
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07001789static int
1790mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
1791 char *name, struct kernfs_node **dest_kn)
1792{
1793 struct kernfs_node *kn;
1794 int ret;
1795
1796 /* create the directory */
1797 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1798 if (IS_ERR(kn))
1799 return PTR_ERR(kn);
1800
1801 if (dest_kn)
1802 *dest_kn = kn;
1803
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07001804 ret = rdtgroup_kn_set_ugid(kn);
1805 if (ret)
1806 goto out_destroy;
1807
1808 kernfs_activate(kn);
1809
1810 return 0;
1811
1812out_destroy:
1813 kernfs_remove(kn);
1814 return ret;
1815}
Fenghua Yu99adde92017-12-20 14:57:23 -08001816
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001817static void l3_qos_cfg_update(void *arg)
1818{
1819 bool *enable = arg;
1820
Babu Mogeraa504532018-11-21 20:28:31 +00001821 wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001822}
1823
Fenghua Yu99adde92017-12-20 14:57:23 -08001824static void l2_qos_cfg_update(void *arg)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001825{
Fenghua Yu99adde92017-12-20 14:57:23 -08001826 bool *enable = arg;
1827
Babu Mogeraa504532018-11-21 20:28:31 +00001828 wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
Fenghua Yu99adde92017-12-20 14:57:23 -08001829}
1830
Vikas Shivappa19c635a2018-04-20 15:36:17 -07001831static inline bool is_mba_linear(void)
1832{
James Morse63c8b122021-07-28 17:06:14 +00001833 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
Vikas Shivappa19c635a2018-04-20 15:36:17 -07001834}
1835
Fenghua Yu99adde92017-12-20 14:57:23 -08001836static int set_cache_qos_cfg(int level, bool enable)
1837{
1838 void (*update)(void *arg);
1839 struct rdt_resource *r_l;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001840 cpumask_var_t cpu_mask;
1841 struct rdt_domain *d;
1842 int cpu;
1843
Fenghua Yu99adde92017-12-20 14:57:23 -08001844 if (level == RDT_RESOURCE_L3)
1845 update = l3_qos_cfg_update;
1846 else if (level == RDT_RESOURCE_L2)
1847 update = l2_qos_cfg_update;
1848 else
1849 return -EINVAL;
1850
Shakeel Buttab6a2112020-01-02 08:58:44 -08001851 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1852 return -ENOMEM;
1853
James Morse63c8b122021-07-28 17:06:14 +00001854 r_l = &rdt_resources_all[level].r_resctrl;
Fenghua Yu99adde92017-12-20 14:57:23 -08001855 list_for_each_entry(d, &r_l->domains, list) {
Babu Mogerfae3a132020-11-30 09:57:20 -06001856 if (r_l->cache.arch_has_per_cpu_cfg)
1857 /* Pick all the CPUs in the domain instance */
1858 for_each_cpu(cpu, &d->cpu_mask)
1859 cpumask_set_cpu(cpu, cpu_mask);
1860 else
1861 /* Pick one CPU from each domain instance to update MSR */
1862 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001863 }
1864 cpu = get_cpu();
1865 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1866 if (cpumask_test_cpu(cpu, cpu_mask))
Fenghua Yu99adde92017-12-20 14:57:23 -08001867 update(&enable);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001868 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
Fenghua Yu99adde92017-12-20 14:57:23 -08001869 smp_call_function_many(cpu_mask, update, &enable, 1);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001870 put_cpu();
1871
1872 free_cpumask_var(cpu_mask);
1873
1874 return 0;
1875}
1876
James Morse9fe045072020-02-21 16:21:05 +00001877/* Restore the qos cfg state when a domain comes online */
1878void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
1879{
James Morsec091e902021-07-28 17:06:24 +00001880 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
1881
1882 if (!r->cdp_capable)
James Morse9fe045072020-02-21 16:21:05 +00001883 return;
1884
James Morse5c3b63c2021-07-28 17:06:37 +00001885 if (r->rid == RDT_RESOURCE_L2)
James Morsec091e902021-07-28 17:06:24 +00001886 l2_qos_cfg_update(&hw_res->cdp_enabled);
James Morse9fe045072020-02-21 16:21:05 +00001887
James Morse5c3b63c2021-07-28 17:06:37 +00001888 if (r->rid == RDT_RESOURCE_L3)
James Morsec091e902021-07-28 17:06:24 +00001889 l3_qos_cfg_update(&hw_res->cdp_enabled);
James Morse9fe045072020-02-21 16:21:05 +00001890}
1891
Vikas Shivappa19c635a2018-04-20 15:36:17 -07001892/*
1893 * Enable or disable the MBA software controller
1894 * which helps user specify bandwidth in MBps.
1895 * MBA software controller is supported only if
1896 * MBM is supported and MBA is in linear scale.
1897 */
1898static int set_mba_sc(bool mba_sc)
1899{
James Morse63c8b122021-07-28 17:06:14 +00001900 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
James Morse792e0f62021-07-28 17:06:15 +00001901 struct rdt_hw_domain *hw_dom;
Vikas Shivappa1bd2a632018-04-20 15:36:18 -07001902 struct rdt_domain *d;
Vikas Shivappa19c635a2018-04-20 15:36:17 -07001903
1904 if (!is_mbm_enabled() || !is_mba_linear() ||
1905 mba_sc == is_mba_sc(r))
1906 return -EINVAL;
1907
1908 r->membw.mba_sc = mba_sc;
James Morse792e0f62021-07-28 17:06:15 +00001909 list_for_each_entry(d, &r->domains, list) {
1910 hw_dom = resctrl_to_arch_dom(d);
1911 setup_default_ctrlval(r, hw_dom->ctrl_val, hw_dom->mbps_val);
1912 }
Vikas Shivappa19c635a2018-04-20 15:36:17 -07001913
1914 return 0;
1915}
1916
James Morse5c3b63c2021-07-28 17:06:37 +00001917static int cdp_enable(int level)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001918{
James Morse63c8b122021-07-28 17:06:14 +00001919 struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001920 int ret;
1921
James Morse5c3b63c2021-07-28 17:06:37 +00001922 if (!r_l->alloc_capable)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001923 return -EINVAL;
1924
Fenghua Yu99adde92017-12-20 14:57:23 -08001925 ret = set_cache_qos_cfg(level, true);
James Morse5c3b63c2021-07-28 17:06:37 +00001926 if (!ret)
James Morsec091e902021-07-28 17:06:24 +00001927 rdt_resources_all[level].cdp_enabled = true;
James Morse5c3b63c2021-07-28 17:06:37 +00001928
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001929 return ret;
1930}
1931
James Morse5c3b63c2021-07-28 17:06:37 +00001932static void cdp_disable(int level)
Fenghua Yu99adde92017-12-20 14:57:23 -08001933{
James Morsec091e902021-07-28 17:06:24 +00001934 struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001935
James Morsec091e902021-07-28 17:06:24 +00001936 if (r_hw->cdp_enabled) {
Fenghua Yu99adde92017-12-20 14:57:23 -08001937 set_cache_qos_cfg(level, false);
James Morsec091e902021-07-28 17:06:24 +00001938 r_hw->cdp_enabled = false;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07001939 }
1940}
1941
James Morsec091e902021-07-28 17:06:24 +00001942int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
Fenghua Yu99adde92017-12-20 14:57:23 -08001943{
James Morsec091e902021-07-28 17:06:24 +00001944 struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
Fenghua Yu99adde92017-12-20 14:57:23 -08001945
James Morsec091e902021-07-28 17:06:24 +00001946 if (!hw_res->r_resctrl.cdp_capable)
1947 return -EINVAL;
1948
James Morsec091e902021-07-28 17:06:24 +00001949 if (enable)
James Morse5c3b63c2021-07-28 17:06:37 +00001950 return cdp_enable(l);
James Morsec091e902021-07-28 17:06:24 +00001951
James Morse5c3b63c2021-07-28 17:06:37 +00001952 cdp_disable(l);
James Morsec091e902021-07-28 17:06:24 +00001953
1954 return 0;
Fenghua Yu99adde92017-12-20 14:57:23 -08001955}
1956
1957static void cdp_disable_all(void)
1958{
James Morsec091e902021-07-28 17:06:24 +00001959 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
1960 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
1961 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
1962 resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
Fenghua Yu99adde92017-12-20 14:57:23 -08001963}
1964
Fenghua Yu60cf5e12016-10-28 15:04:44 -07001965/*
1966 * We don't allow rdtgroup directories to be created anywhere
1967 * except the root directory. Thus when looking for the rdtgroup
1968 * structure for a kernfs node we are either looking at a directory,
1969 * in which case the rdtgroup structure is pointed at by the "priv"
1970 * field, otherwise we have a file, and need only look to the parent
1971 * to find the rdtgroup.
1972 */
1973static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1974{
Fenghua Yuf57b3082016-11-11 17:02:36 -08001975 if (kernfs_type(kn) == KERNFS_DIR) {
1976 /*
1977 * All the resource directories use "kn->priv"
1978 * to point to the "struct rdtgroup" for the
1979 * resource. "info" and its subdirectories don't
1980 * have rdtgroup structures, so return NULL here.
1981 */
1982 if (kn == kn_info || kn->parent == kn_info)
1983 return NULL;
1984 else
1985 return kn->priv;
1986 } else {
Fenghua Yu60cf5e12016-10-28 15:04:44 -07001987 return kn->parent->priv;
Fenghua Yuf57b3082016-11-11 17:02:36 -08001988 }
Fenghua Yu60cf5e12016-10-28 15:04:44 -07001989}
1990
1991struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1992{
1993 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1994
Fenghua Yuf57b3082016-11-11 17:02:36 -08001995 if (!rdtgrp)
1996 return NULL;
1997
Fenghua Yu60cf5e12016-10-28 15:04:44 -07001998 atomic_inc(&rdtgrp->waitcount);
1999 kernfs_break_active_protection(kn);
2000
2001 mutex_lock(&rdtgroup_mutex);
2002
2003 /* Was this group deleted while we waited? */
2004 if (rdtgrp->flags & RDT_DELETED)
2005 return NULL;
2006
2007 return rdtgrp;
2008}
2009
2010void rdtgroup_kn_unlock(struct kernfs_node *kn)
2011{
2012 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2013
Fenghua Yuf57b3082016-11-11 17:02:36 -08002014 if (!rdtgrp)
2015 return;
2016
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002017 mutex_unlock(&rdtgroup_mutex);
2018
2019 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2020 (rdtgrp->flags & RDT_DELETED)) {
Reinette Chatree0bdfe82018-06-22 15:42:22 -07002021 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2022 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2023 rdtgroup_pseudo_lock_remove(rdtgrp);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002024 kernfs_unbreak_active_protection(kn);
Xiaochen Shen75899922020-10-31 03:11:28 +08002025 rdtgroup_remove(rdtgrp);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002026 } else {
2027 kernfs_unbreak_active_protection(kn);
2028 }
2029}
2030
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002031static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2032 struct rdtgroup *prgrp,
2033 struct kernfs_node **mon_data_kn);
2034
David Howells23bf1b62018-11-01 23:07:26 +00002035static int rdt_enable_ctx(struct rdt_fs_context *ctx)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002036{
David Howells23bf1b62018-11-01 23:07:26 +00002037 int ret = 0;
2038
2039 if (ctx->enable_cdpl2)
James Morsec091e902021-07-28 17:06:24 +00002040 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
David Howells23bf1b62018-11-01 23:07:26 +00002041
2042 if (!ret && ctx->enable_cdpl3)
James Morsec091e902021-07-28 17:06:24 +00002043 ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
David Howells23bf1b62018-11-01 23:07:26 +00002044
2045 if (!ret && ctx->enable_mba_mbps)
2046 ret = set_mba_sc(true);
2047
2048 return ret;
2049}
2050
James Morse5c3b63c2021-07-28 17:06:37 +00002051static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
James Morsecdb9ebc2021-07-28 17:06:16 +00002052{
2053 struct resctrl_schema *s;
James Morse5c3b63c2021-07-28 17:06:37 +00002054 const char *suffix = "";
James Morsee198fde2021-07-28 17:06:25 +00002055 int ret, cl;
James Morsecdb9ebc2021-07-28 17:06:16 +00002056
James Morse5c3b63c2021-07-28 17:06:37 +00002057 s = kzalloc(sizeof(*s), GFP_KERNEL);
2058 if (!s)
2059 return -ENOMEM;
James Morsecdb9ebc2021-07-28 17:06:16 +00002060
James Morse5c3b63c2021-07-28 17:06:37 +00002061 s->res = r;
2062 s->num_closid = resctrl_arch_get_num_closid(r);
2063 if (resctrl_arch_get_cdp_enabled(r->rid))
2064 s->num_closid /= 2;
James Morsecdb9ebc2021-07-28 17:06:16 +00002065
James Morse5c3b63c2021-07-28 17:06:37 +00002066 s->conf_type = type;
2067 switch (type) {
2068 case CDP_CODE:
2069 suffix = "CODE";
2070 break;
2071 case CDP_DATA:
2072 suffix = "DATA";
2073 break;
2074 case CDP_NONE:
2075 suffix = "";
2076 break;
James Morsecdb9ebc2021-07-28 17:06:16 +00002077 }
2078
James Morse5c3b63c2021-07-28 17:06:37 +00002079 ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
2080 if (ret >= sizeof(s->name)) {
2081 kfree(s);
2082 return -EINVAL;
2083 }
2084
2085 cl = strlen(s->name);
2086
2087 /*
2088 * If CDP is supported by this resource, but not enabled,
2089 * include the suffix. This ensures the tabular format of the
2090 * schemata file does not change between mounts of the filesystem.
2091 */
2092 if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
2093 cl += 4;
2094
2095 if (cl > max_name_width)
2096 max_name_width = cl;
2097
2098 INIT_LIST_HEAD(&s->list);
2099 list_add(&s->list, &resctrl_schema_all);
2100
James Morsecdb9ebc2021-07-28 17:06:16 +00002101 return 0;
2102}
2103
James Morse5c3b63c2021-07-28 17:06:37 +00002104static int schemata_list_create(void)
2105{
2106 struct rdt_resource *r;
2107 int ret = 0;
2108
2109 for_each_alloc_enabled_rdt_resource(r) {
2110 if (resctrl_arch_get_cdp_enabled(r->rid)) {
2111 ret = schemata_list_add(r, CDP_CODE);
2112 if (ret)
2113 break;
2114
2115 ret = schemata_list_add(r, CDP_DATA);
2116 } else {
2117 ret = schemata_list_add(r, CDP_NONE);
2118 }
2119
2120 if (ret)
2121 break;
2122 }
2123
2124 return ret;
2125}
2126
James Morsecdb9ebc2021-07-28 17:06:16 +00002127static void schemata_list_destroy(void)
2128{
2129 struct resctrl_schema *s, *tmp;
2130
2131 list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
2132 list_del(&s->list);
2133 kfree(s);
2134 }
2135}
2136
David Howells23bf1b62018-11-01 23:07:26 +00002137static int rdt_get_tree(struct fs_context *fc)
2138{
2139 struct rdt_fs_context *ctx = rdt_fc2context(fc);
Vikas Shivappae3302682017-07-25 14:14:47 -07002140 struct rdt_domain *dom;
2141 struct rdt_resource *r;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002142 int ret;
2143
Reinette Chatre87943db2017-10-20 02:16:59 -07002144 cpus_read_lock();
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002145 mutex_lock(&rdtgroup_mutex);
2146 /*
2147 * resctrl file system can only be mounted once.
2148 */
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002149 if (static_branch_unlikely(&rdt_enable_key)) {
David Howells23bf1b62018-11-01 23:07:26 +00002150 ret = -EBUSY;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002151 goto out;
2152 }
2153
David Howells23bf1b62018-11-01 23:07:26 +00002154 ret = rdt_enable_ctx(ctx);
2155 if (ret < 0)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002156 goto out_cdp;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002157
James Morsecdb9ebc2021-07-28 17:06:16 +00002158 ret = schemata_list_create();
2159 if (ret) {
2160 schemata_list_destroy();
2161 goto out_mba;
2162 }
2163
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002164 closid_init();
2165
Fenghua Yu4e978d02016-10-28 15:04:43 -07002166 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
David Howells23bf1b62018-11-01 23:07:26 +00002167 if (ret < 0)
James Morsecdb9ebc2021-07-28 17:06:16 +00002168 goto out_schemata_free;
Fenghua Yu4e978d02016-10-28 15:04:43 -07002169
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002170 if (rdt_mon_capable) {
2171 ret = mongroup_create_dir(rdtgroup_default.kn,
Xiaochen Shen334b0f42020-01-09 00:28:05 +08002172 &rdtgroup_default, "mon_groups",
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002173 &kn_mongrp);
David Howells23bf1b62018-11-01 23:07:26 +00002174 if (ret < 0)
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002175 goto out_info;
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002176
2177 ret = mkdir_mondata_all(rdtgroup_default.kn,
2178 &rdtgroup_default, &kn_mondata);
David Howells23bf1b62018-11-01 23:07:26 +00002179 if (ret < 0)
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002180 goto out_mongrp;
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002181 rdtgroup_default.mon.mon_data_kn = kn_mondata;
2182 }
2183
Reinette Chatre32206ab2018-06-22 15:41:52 -07002184 ret = rdt_pseudo_lock_init();
David Howells23bf1b62018-11-01 23:07:26 +00002185 if (ret)
Reinette Chatre32206ab2018-06-22 15:41:52 -07002186 goto out_mondata;
Reinette Chatre32206ab2018-06-22 15:41:52 -07002187
David Howells23bf1b62018-11-01 23:07:26 +00002188 ret = kernfs_get_tree(fc);
2189 if (ret < 0)
Reinette Chatre32206ab2018-06-22 15:41:52 -07002190 goto out_psl;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002191
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002192 if (rdt_alloc_capable)
Reinette Chatre87943db2017-10-20 02:16:59 -07002193 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002194 if (rdt_mon_capable)
Reinette Chatre87943db2017-10-20 02:16:59 -07002195 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002196
2197 if (rdt_alloc_capable || rdt_mon_capable)
Reinette Chatre87943db2017-10-20 02:16:59 -07002198 static_branch_enable_cpuslocked(&rdt_enable_key);
Vikas Shivappae3302682017-07-25 14:14:47 -07002199
2200 if (is_mbm_enabled()) {
James Morse63c8b122021-07-28 17:06:14 +00002201 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
Vikas Shivappae3302682017-07-25 14:14:47 -07002202 list_for_each_entry(dom, &r->domains, list)
Vikas Shivappabbc46152017-08-15 18:00:42 -07002203 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
Vikas Shivappae3302682017-07-25 14:14:47 -07002204 }
2205
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002206 goto out;
2207
Reinette Chatre32206ab2018-06-22 15:41:52 -07002208out_psl:
2209 rdt_pseudo_lock_release();
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002210out_mondata:
2211 if (rdt_mon_capable)
2212 kernfs_remove(kn_mondata);
2213out_mongrp:
2214 if (rdt_mon_capable)
2215 kernfs_remove(kn_mongrp);
2216out_info:
Vikas Shivappa79298ac2017-06-26 11:55:49 -07002217 kernfs_remove(kn_info);
James Morsecdb9ebc2021-07-28 17:06:16 +00002218out_schemata_free:
2219 schemata_list_destroy();
David Howells23bf1b62018-11-01 23:07:26 +00002220out_mba:
2221 if (ctx->enable_mba_mbps)
2222 set_mba_sc(false);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002223out_cdp:
Fenghua Yu99adde92017-12-20 14:57:23 -08002224 cdp_disable_all();
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002225out:
Tony Luck9b3a7fd2017-09-25 16:39:33 -07002226 rdt_last_cmd_clear();
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002227 mutex_unlock(&rdtgroup_mutex);
Reinette Chatre87943db2017-10-20 02:16:59 -07002228 cpus_read_unlock();
David Howells23bf1b62018-11-01 23:07:26 +00002229 return ret;
2230}
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002231
David Howells23bf1b62018-11-01 23:07:26 +00002232enum rdt_param {
2233 Opt_cdp,
2234 Opt_cdpl2,
Xiaochen Shenfaa36042019-03-30 05:50:38 +08002235 Opt_mba_mbps,
David Howells23bf1b62018-11-01 23:07:26 +00002236 nr__rdt_params
2237};
2238
Al Virod7167b12019-09-07 07:23:15 -04002239static const struct fs_parameter_spec rdt_fs_parameters[] = {
David Howells23bf1b62018-11-01 23:07:26 +00002240 fsparam_flag("cdp", Opt_cdp),
2241 fsparam_flag("cdpl2", Opt_cdpl2),
Xiaochen Shenfaa36042019-03-30 05:50:38 +08002242 fsparam_flag("mba_MBps", Opt_mba_mbps),
David Howells23bf1b62018-11-01 23:07:26 +00002243 {}
2244};
2245
David Howells23bf1b62018-11-01 23:07:26 +00002246static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2247{
2248 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2249 struct fs_parse_result result;
2250 int opt;
2251
Al Virod7167b12019-09-07 07:23:15 -04002252 opt = fs_parse(fc, rdt_fs_parameters, param, &result);
David Howells23bf1b62018-11-01 23:07:26 +00002253 if (opt < 0)
2254 return opt;
2255
2256 switch (opt) {
2257 case Opt_cdp:
2258 ctx->enable_cdpl3 = true;
2259 return 0;
2260 case Opt_cdpl2:
2261 ctx->enable_cdpl2 = true;
2262 return 0;
Xiaochen Shenfaa36042019-03-30 05:50:38 +08002263 case Opt_mba_mbps:
David Howells23bf1b62018-11-01 23:07:26 +00002264 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2265 return -EINVAL;
2266 ctx->enable_mba_mbps = true;
2267 return 0;
2268 }
2269
2270 return -EINVAL;
2271}
2272
2273static void rdt_fs_context_free(struct fs_context *fc)
2274{
2275 struct rdt_fs_context *ctx = rdt_fc2context(fc);
2276
2277 kernfs_free_fs_context(fc);
2278 kfree(ctx);
2279}
2280
2281static const struct fs_context_operations rdt_fs_context_ops = {
2282 .free = rdt_fs_context_free,
2283 .parse_param = rdt_parse_param,
2284 .get_tree = rdt_get_tree,
2285};
2286
2287static int rdt_init_fs_context(struct fs_context *fc)
2288{
2289 struct rdt_fs_context *ctx;
2290
2291 ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2292 if (!ctx)
2293 return -ENOMEM;
2294
2295 ctx->kfc.root = rdt_root;
2296 ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2297 fc->fs_private = &ctx->kfc;
2298 fc->ops = &rdt_fs_context_ops;
Al Virof7a99452019-05-12 12:42:58 -04002299 put_user_ns(fc->user_ns);
David Howells23bf1b62018-11-01 23:07:26 +00002300 fc->user_ns = get_user_ns(&init_user_ns);
2301 fc->global = true;
2302 return 0;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002303}
2304
Vikas Shivappa2545e9f2017-04-07 17:33:51 -07002305static int reset_all_ctrls(struct rdt_resource *r)
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002306{
James Morse63c8b122021-07-28 17:06:14 +00002307 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
James Morse792e0f62021-07-28 17:06:15 +00002308 struct rdt_hw_domain *hw_dom;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002309 struct msr_param msr_param;
2310 cpumask_var_t cpu_mask;
2311 struct rdt_domain *d;
2312 int i, cpu;
2313
2314 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2315 return -ENOMEM;
2316
2317 msr_param.res = r;
2318 msr_param.low = 0;
James Morse63c8b122021-07-28 17:06:14 +00002319 msr_param.high = hw_res->num_closid;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002320
2321 /*
2322 * Disable resource control for this resource by setting all
2323 * CBMs in all domains to the maximum mask value. Pick one CPU
2324 * from each domain to update the MSRs below.
2325 */
2326 list_for_each_entry(d, &r->domains, list) {
James Morse792e0f62021-07-28 17:06:15 +00002327 hw_dom = resctrl_to_arch_dom(d);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002328 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2329
James Morse63c8b122021-07-28 17:06:14 +00002330 for (i = 0; i < hw_res->num_closid; i++)
James Morse792e0f62021-07-28 17:06:15 +00002331 hw_dom->ctrl_val[i] = r->default_ctrl;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002332 }
2333 cpu = get_cpu();
2334 /* Update CBM on this cpu if it's in cpu_mask. */
2335 if (cpumask_test_cpu(cpu, cpu_mask))
Vikas Shivappa2545e9f2017-04-07 17:33:51 -07002336 rdt_ctrl_update(&msr_param);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002337 /* Update CBM on all other cpus in cpu_mask. */
Vikas Shivappa2545e9f2017-04-07 17:33:51 -07002338 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002339 put_cpu();
2340
2341 free_cpumask_var(cpu_mask);
2342
2343 return 0;
2344}
2345
Fenghua Yu4e978d02016-10-28 15:04:43 -07002346/*
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002347 * Move tasks from one to the other group. If @from is NULL, then all tasks
2348 * in the systems are moved unconditionally (used for teardown).
2349 *
2350 * If @mask is not NULL the cpus on which moved tasks are running are set
2351 * in that mask so the update smp function call is restricted to affected
2352 * cpus.
2353 */
2354static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2355 struct cpumask *mask)
2356{
2357 struct task_struct *p, *t;
2358
2359 read_lock(&tasklist_lock);
2360 for_each_process_thread(p, t) {
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07002361 if (!from || is_closid_match(t, from) ||
2362 is_rmid_match(t, from)) {
Valentin Schneider6d3b47d2020-12-17 14:31:21 -08002363 WRITE_ONCE(t->closid, to->closid);
2364 WRITE_ONCE(t->rmid, to->mon.rmid);
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07002365
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002366 /*
Reinette Chatree0ad6dc2020-12-17 14:31:20 -08002367 * If the task is on a CPU, set the CPU in the mask.
2368 * The detection is inaccurate as tasks might move or
2369 * schedule before the smp function call takes place.
2370 * In such a case the function call is pointless, but
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002371 * there is no other side effect.
2372 */
Reinette Chatree0ad6dc2020-12-17 14:31:20 -08002373 if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002374 cpumask_set_cpu(task_cpu(t), mask);
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002375 }
2376 }
2377 read_unlock(&tasklist_lock);
2378}
2379
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07002380static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2381{
2382 struct rdtgroup *sentry, *stmp;
2383 struct list_head *head;
2384
2385 head = &rdtgrp->mon.crdtgrp_list;
2386 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2387 free_rmid(sentry->mon.rmid);
2388 list_del(&sentry->mon.crdtgrp_list);
Xiaochen Shenb8511cc2020-01-09 00:28:03 +08002389
2390 if (atomic_read(&sentry->waitcount) != 0)
2391 sentry->flags = RDT_DELETED;
2392 else
Xiaochen Shen75899922020-10-31 03:11:28 +08002393 rdtgroup_remove(sentry);
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07002394 }
2395}
2396
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002397/*
Fenghua Yu4e978d02016-10-28 15:04:43 -07002398 * Forcibly remove all of subdirectories under root.
2399 */
2400static void rmdir_all_sub(void)
2401{
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002402 struct rdtgroup *rdtgrp, *tmp;
Fenghua Yue02737d2016-10-28 15:04:46 -07002403
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002404 /* Move all tasks to the default resource group */
2405 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002406
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002407 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002408 /* Free any child rmids */
2409 free_all_child_rdtgrp(rdtgrp);
2410
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002411 /* Remove each rdtgroup other than root */
2412 if (rdtgrp == &rdtgroup_default)
2413 continue;
Fenghua Yuc7cc0cc2016-11-11 17:02:37 -08002414
Reinette Chatree0bdfe82018-06-22 15:42:22 -07002415 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2416 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2417 rdtgroup_pseudo_lock_remove(rdtgrp);
2418
Fenghua Yuc7cc0cc2016-11-11 17:02:37 -08002419 /*
2420 * Give any CPUs back to the default group. We cannot copy
2421 * cpu_online_mask because a CPU might have executed the
2422 * offline callback already, but is still marked online.
2423 */
2424 cpumask_or(&rdtgroup_default.cpu_mask,
2425 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2426
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002427 free_rmid(rdtgrp->mon.rmid);
2428
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002429 kernfs_remove(rdtgrp->kn);
2430 list_del(&rdtgrp->rdtgroup_list);
Xiaochen Shenb8511cc2020-01-09 00:28:03 +08002431
2432 if (atomic_read(&rdtgrp->waitcount) != 0)
2433 rdtgrp->flags = RDT_DELETED;
2434 else
Xiaochen Shen75899922020-10-31 03:11:28 +08002435 rdtgroup_remove(rdtgrp);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002436 }
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002437 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
Vikas Shivappaa9fcf862017-07-25 14:14:36 -07002438 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
Fenghua Yu0efc89b2016-11-18 15:18:04 -08002439
Fenghua Yu4e978d02016-10-28 15:04:43 -07002440 kernfs_remove(kn_info);
Vikas Shivappa4af4a882017-07-25 14:14:41 -07002441 kernfs_remove(kn_mongrp);
2442 kernfs_remove(kn_mondata);
Fenghua Yu4e978d02016-10-28 15:04:43 -07002443}
2444
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002445static void rdt_kill_sb(struct super_block *sb)
2446{
2447 struct rdt_resource *r;
2448
Reinette Chatre36b6f9f2017-10-20 02:16:58 -07002449 cpus_read_lock();
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002450 mutex_lock(&rdtgroup_mutex);
2451
Vikas Shivappa19c635a2018-04-20 15:36:17 -07002452 set_mba_sc(false);
2453
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002454 /*Put everything back to default values. */
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -07002455 for_each_alloc_enabled_rdt_resource(r)
Vikas Shivappa2545e9f2017-04-07 17:33:51 -07002456 reset_all_ctrls(r);
Fenghua Yu99adde92017-12-20 14:57:23 -08002457 cdp_disable_all();
Fenghua Yu4e978d02016-10-28 15:04:43 -07002458 rmdir_all_sub();
Reinette Chatre746e0852018-06-22 15:42:27 -07002459 rdt_pseudo_lock_release();
Reinette Chatre472ef09b2018-06-22 15:41:55 -07002460 rdtgroup_default.mode = RDT_MODE_SHAREABLE;
James Morsecdb9ebc2021-07-28 17:06:16 +00002461 schemata_list_destroy();
Reinette Chatre36b6f9f2017-10-20 02:16:58 -07002462 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2463 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2464 static_branch_disable_cpuslocked(&rdt_enable_key);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002465 kernfs_kill_sb(sb);
2466 mutex_unlock(&rdtgroup_mutex);
Reinette Chatre36b6f9f2017-10-20 02:16:58 -07002467 cpus_read_unlock();
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002468}
2469
2470static struct file_system_type rdt_fs_type = {
David Howells23bf1b62018-11-01 23:07:26 +00002471 .name = "resctrl",
2472 .init_fs_context = rdt_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04002473 .parameters = rdt_fs_parameters,
David Howells23bf1b62018-11-01 23:07:26 +00002474 .kill_sb = rdt_kill_sb,
Fenghua Yu5ff193f2016-10-28 15:04:42 -07002475};
2476
Vikas Shivappad89b7372017-07-25 14:14:38 -07002477static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2478 void *priv)
2479{
2480 struct kernfs_node *kn;
2481 int ret = 0;
2482
Dmitry Torokhov488dee92018-07-20 21:56:47 +00002483 kn = __kernfs_create_file(parent_kn, name, 0444,
2484 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
Vikas Shivappad89b7372017-07-25 14:14:38 -07002485 &kf_mondata_ops, priv, NULL, NULL);
2486 if (IS_ERR(kn))
2487 return PTR_ERR(kn);
2488
2489 ret = rdtgroup_kn_set_ugid(kn);
2490 if (ret) {
2491 kernfs_remove(kn);
2492 return ret;
2493 }
2494
2495 return ret;
2496}
2497
Vikas Shivappa895c6632017-07-25 14:14:44 -07002498/*
2499 * Remove all subdirectories of mon_data of ctrl_mon groups
2500 * and monitor groups with given domain id.
2501 */
2502void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
2503{
2504 struct rdtgroup *prgrp, *crgrp;
2505 char name[32];
2506
2507 if (!r->mon_enabled)
2508 return;
2509
2510 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2511 sprintf(name, "mon_%s_%02d", r->name, dom_id);
2512 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2513
2514 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2515 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2516 }
2517}
2518
Vikas Shivappad89b7372017-07-25 14:14:38 -07002519static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2520 struct rdt_domain *d,
2521 struct rdt_resource *r, struct rdtgroup *prgrp)
2522{
2523 union mon_data_bits priv;
2524 struct kernfs_node *kn;
2525 struct mon_evt *mevt;
Vikas Shivappaa4de1df2017-07-25 14:14:46 -07002526 struct rmid_read rr;
Vikas Shivappad89b7372017-07-25 14:14:38 -07002527 char name[32];
2528 int ret;
2529
2530 sprintf(name, "mon_%s_%02d", r->name, d->id);
2531 /* create the directory */
2532 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2533 if (IS_ERR(kn))
2534 return PTR_ERR(kn);
2535
Vikas Shivappad89b7372017-07-25 14:14:38 -07002536 ret = rdtgroup_kn_set_ugid(kn);
2537 if (ret)
2538 goto out_destroy;
2539
2540 if (WARN_ON(list_empty(&r->evt_list))) {
2541 ret = -EPERM;
2542 goto out_destroy;
2543 }
2544
2545 priv.u.rid = r->rid;
2546 priv.u.domid = d->id;
2547 list_for_each_entry(mevt, &r->evt_list, list) {
2548 priv.u.evtid = mevt->evtid;
2549 ret = mon_addfile(kn, mevt->name, priv.priv);
2550 if (ret)
2551 goto out_destroy;
Vikas Shivappaa4de1df2017-07-25 14:14:46 -07002552
2553 if (is_mbm_event(mevt->evtid))
Reinette Chatre46637d42020-05-05 15:36:16 -07002554 mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
Vikas Shivappad89b7372017-07-25 14:14:38 -07002555 }
2556 kernfs_activate(kn);
2557 return 0;
2558
2559out_destroy:
2560 kernfs_remove(kn);
2561 return ret;
2562}
2563
Vikas Shivappa895c6632017-07-25 14:14:44 -07002564/*
2565 * Add all subdirectories of mon_data for "ctrl_mon" groups
2566 * and "monitor" groups with given domain id.
2567 */
2568void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2569 struct rdt_domain *d)
2570{
2571 struct kernfs_node *parent_kn;
2572 struct rdtgroup *prgrp, *crgrp;
2573 struct list_head *head;
2574
2575 if (!r->mon_enabled)
2576 return;
2577
2578 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2579 parent_kn = prgrp->mon.mon_data_kn;
2580 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2581
2582 head = &prgrp->mon.crdtgrp_list;
2583 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2584 parent_kn = crgrp->mon.mon_data_kn;
2585 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2586 }
2587 }
2588}
2589
Vikas Shivappad89b7372017-07-25 14:14:38 -07002590static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2591 struct rdt_resource *r,
2592 struct rdtgroup *prgrp)
2593{
2594 struct rdt_domain *dom;
2595 int ret;
2596
2597 list_for_each_entry(dom, &r->domains, list) {
2598 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2599 if (ret)
2600 return ret;
2601 }
2602
2603 return 0;
2604}
2605
2606/*
2607 * This creates a directory mon_data which contains the monitored data.
2608 *
Ingo Molnar163b0992021-03-21 22:28:53 +01002609 * mon_data has one directory for each domain which are named
Vikas Shivappad89b7372017-07-25 14:14:38 -07002610 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2611 * with L3 domain looks as below:
2612 * ./mon_data:
2613 * mon_L3_00
2614 * mon_L3_01
2615 * mon_L3_02
2616 * ...
2617 *
2618 * Each domain directory has one file per event:
2619 * ./mon_L3_00/:
2620 * llc_occupancy
2621 *
2622 */
2623static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2624 struct rdtgroup *prgrp,
2625 struct kernfs_node **dest_kn)
2626{
2627 struct rdt_resource *r;
2628 struct kernfs_node *kn;
2629 int ret;
2630
2631 /*
2632 * Create the mon_data directory first.
2633 */
Xiaochen Shen334b0f42020-01-09 00:28:05 +08002634 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
Vikas Shivappad89b7372017-07-25 14:14:38 -07002635 if (ret)
2636 return ret;
2637
2638 if (dest_kn)
2639 *dest_kn = kn;
2640
2641 /*
2642 * Create the subdirectories for each domain. Note that all events
2643 * in a domain like L3 are grouped into a resource whose domain is L3
2644 */
2645 for_each_mon_enabled_rdt_resource(r) {
2646 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2647 if (ret)
2648 goto out_destroy;
2649 }
2650
2651 return 0;
2652
2653out_destroy:
2654 kernfs_remove(kn);
2655 return ret;
2656}
2657
Reinette Chatre95f0b772018-06-22 15:41:59 -07002658/**
2659 * cbm_ensure_valid - Enforce validity on provided CBM
2660 * @_val: Candidate CBM
2661 * @r: RDT resource to which the CBM belongs
2662 *
2663 * The provided CBM represents all cache portions available for use. This
2664 * may be represented by a bitmap that does not consist of contiguous ones
2665 * and thus be an invalid CBM.
2666 * Here the provided CBM is forced to be a valid CBM by only considering
2667 * the first set of contiguous bits as valid and clearing all bits.
2668 * The intention here is to provide a valid default CBM with which a new
2669 * resource group is initialized. The user can follow this with a
2670 * modification to the CBM if the default does not satisfy the
2671 * requirements.
2672 */
Reinette Chatre2ef085b2019-06-24 13:34:27 -07002673static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
Reinette Chatre95f0b772018-06-22 15:41:59 -07002674{
Reinette Chatre95f0b772018-06-22 15:41:59 -07002675 unsigned int cbm_len = r->cache.cbm_len;
2676 unsigned long first_bit, zero_bit;
Reinette Chatre2ef085b2019-06-24 13:34:27 -07002677 unsigned long val = _val;
Reinette Chatre95f0b772018-06-22 15:41:59 -07002678
Reinette Chatre2ef085b2019-06-24 13:34:27 -07002679 if (!val)
2680 return 0;
Reinette Chatre95f0b772018-06-22 15:41:59 -07002681
Reinette Chatre32f010d2019-06-19 13:27:16 -07002682 first_bit = find_first_bit(&val, cbm_len);
2683 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
Reinette Chatre95f0b772018-06-22 15:41:59 -07002684
2685 /* Clear any remaining bits to ensure contiguous region */
Reinette Chatre32f010d2019-06-19 13:27:16 -07002686 bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
Reinette Chatre2ef085b2019-06-24 13:34:27 -07002687 return (u32)val;
Reinette Chatre95f0b772018-06-22 15:41:59 -07002688}
2689
Xiaochen Shen73906192019-04-17 19:08:48 +08002690/*
2691 * Initialize cache resources per RDT domain
2692 *
2693 * Set the RDT domain up to start off with all usable allocations. That is,
2694 * all shareable and unused bits. All-zero CBM is invalid.
2695 */
James Morse1c2906822021-07-28 17:06:22 +00002696static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
Xiaochen Shen73906192019-04-17 19:08:48 +08002697 u32 closid)
2698{
James Morsefbc06c62021-07-28 17:06:35 +00002699 enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
James Morse75408e42021-07-28 17:06:27 +00002700 enum resctrl_conf_type t = s->conf_type;
James Morsee8f72822021-07-28 17:06:26 +00002701 struct resctrl_staged_config *cfg;
James Morse1c2906822021-07-28 17:06:22 +00002702 struct rdt_resource *r = s->res;
Xiaochen Shen73906192019-04-17 19:08:48 +08002703 u32 used_b = 0, unused_b = 0;
2704 unsigned long tmp_cbm;
2705 enum rdtgrp_mode mode;
James Morsef07e9d02021-07-28 17:06:29 +00002706 u32 peer_ctl, ctrl_val;
Xiaochen Shen73906192019-04-17 19:08:48 +08002707 int i;
2708
James Morse75408e42021-07-28 17:06:27 +00002709 cfg = &d->staged_config[t];
James Morsee8f72822021-07-28 17:06:26 +00002710 cfg->have_new_ctrl = false;
2711 cfg->new_ctrl = r->cache.shareable_bits;
Xiaochen Shen73906192019-04-17 19:08:48 +08002712 used_b = r->cache.shareable_bits;
James Morsef07e9d02021-07-28 17:06:29 +00002713 for (i = 0; i < closids_supported(); i++) {
Xiaochen Shen73906192019-04-17 19:08:48 +08002714 if (closid_allocated(i) && i != closid) {
2715 mode = rdtgroup_mode_by_closid(i);
2716 if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
James Morse87d3aa22019-06-03 18:25:31 +01002717 /*
2718 * ctrl values for locksetup aren't relevant
2719 * until the schemata is written, and the mode
2720 * becomes RDT_MODE_PSEUDO_LOCKED.
2721 */
2722 continue;
Xiaochen Shen73906192019-04-17 19:08:48 +08002723 /*
2724 * If CDP is active include peer domain's
2725 * usage to ensure there is no overlap
2726 * with an exclusive group.
2727 */
James Morsefbc06c62021-07-28 17:06:35 +00002728 if (resctrl_arch_get_cdp_enabled(r->rid))
James Morse111136e2021-08-11 16:38:31 +00002729 peer_ctl = resctrl_arch_get_config(r, d, i,
2730 peer_type);
Xiaochen Shen73906192019-04-17 19:08:48 +08002731 else
2732 peer_ctl = 0;
James Morse111136e2021-08-11 16:38:31 +00002733 ctrl_val = resctrl_arch_get_config(r, d, i,
2734 s->conf_type);
James Morsef07e9d02021-07-28 17:06:29 +00002735 used_b |= ctrl_val | peer_ctl;
Xiaochen Shen73906192019-04-17 19:08:48 +08002736 if (mode == RDT_MODE_SHAREABLE)
James Morsef07e9d02021-07-28 17:06:29 +00002737 cfg->new_ctrl |= ctrl_val | peer_ctl;
Xiaochen Shen73906192019-04-17 19:08:48 +08002738 }
2739 }
2740 if (d->plr && d->plr->cbm > 0)
2741 used_b |= d->plr->cbm;
2742 unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
2743 unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
James Morsee8f72822021-07-28 17:06:26 +00002744 cfg->new_ctrl |= unused_b;
Xiaochen Shen73906192019-04-17 19:08:48 +08002745 /*
2746 * Force the initial CBM to be valid, user can
2747 * modify the CBM based on system availability.
2748 */
James Morsee8f72822021-07-28 17:06:26 +00002749 cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
Xiaochen Shen73906192019-04-17 19:08:48 +08002750 /*
2751 * Assign the u32 CBM to an unsigned long to ensure that
2752 * bitmap_weight() does not access out-of-bound memory.
2753 */
James Morsee8f72822021-07-28 17:06:26 +00002754 tmp_cbm = cfg->new_ctrl;
Xiaochen Shen73906192019-04-17 19:08:48 +08002755 if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
James Morsee198fde2021-07-28 17:06:25 +00002756 rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id);
Xiaochen Shen73906192019-04-17 19:08:48 +08002757 return -ENOSPC;
2758 }
James Morsee8f72822021-07-28 17:06:26 +00002759 cfg->have_new_ctrl = true;
Xiaochen Shen73906192019-04-17 19:08:48 +08002760
2761 return 0;
2762}
2763
Xiaochen Shen47820e72019-04-17 19:08:49 +08002764/*
2765 * Initialize cache resources with default values.
Reinette Chatre95f0b772018-06-22 15:41:59 -07002766 *
2767 * A new RDT group is being created on an allocation capable (CAT)
2768 * supporting system. Set this group up to start off with all usable
Xiaochen Shen73906192019-04-17 19:08:48 +08002769 * allocations.
Reinette Chatre95f0b772018-06-22 15:41:59 -07002770 *
Xiaochen Shen73906192019-04-17 19:08:48 +08002771 * If there are no more shareable bits available on any domain then
2772 * the entire allocation will fail.
Reinette Chatre95f0b772018-06-22 15:41:59 -07002773 */
James Morse1c2906822021-07-28 17:06:22 +00002774static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
Reinette Chatre95f0b772018-06-22 15:41:59 -07002775{
Reinette Chatre95f0b772018-06-22 15:41:59 -07002776 struct rdt_domain *d;
Xiaochen Shen73906192019-04-17 19:08:48 +08002777 int ret;
Reinette Chatre95f0b772018-06-22 15:41:59 -07002778
James Morse1c2906822021-07-28 17:06:22 +00002779 list_for_each_entry(d, &s->res->domains, list) {
2780 ret = __init_one_rdt_domain(d, s, closid);
Xiaochen Shen47820e72019-04-17 19:08:49 +08002781 if (ret < 0)
2782 return ret;
2783 }
2784
2785 return 0;
2786}
2787
2788/* Initialize MBA resource with default values. */
2789static void rdtgroup_init_mba(struct rdt_resource *r)
2790{
James Morsee8f72822021-07-28 17:06:26 +00002791 struct resctrl_staged_config *cfg;
Xiaochen Shen47820e72019-04-17 19:08:49 +08002792 struct rdt_domain *d;
2793
2794 list_for_each_entry(d, &r->domains, list) {
James Morse75408e42021-07-28 17:06:27 +00002795 cfg = &d->staged_config[CDP_NONE];
James Morsee8f72822021-07-28 17:06:26 +00002796 cfg->new_ctrl = is_mba_sc(r) ? MBA_MAX_MBPS : r->default_ctrl;
2797 cfg->have_new_ctrl = true;
Xiaochen Shen47820e72019-04-17 19:08:49 +08002798 }
2799}
2800
2801/* Initialize the RDT group's allocations. */
2802static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
2803{
James Morse331ebe42021-07-28 17:06:19 +00002804 struct resctrl_schema *s;
Xiaochen Shen47820e72019-04-17 19:08:49 +08002805 struct rdt_resource *r;
2806 int ret;
2807
James Morse331ebe42021-07-28 17:06:19 +00002808 list_for_each_entry(s, &resctrl_schema_all, list) {
2809 r = s->res;
Xiaochen Shen47820e72019-04-17 19:08:49 +08002810 if (r->rid == RDT_RESOURCE_MBA) {
2811 rdtgroup_init_mba(r);
2812 } else {
James Morse1c2906822021-07-28 17:06:22 +00002813 ret = rdtgroup_init_cat(s, rdtgrp->closid);
Xiaochen Shen73906192019-04-17 19:08:48 +08002814 if (ret < 0)
2815 return ret;
Reinette Chatre95f0b772018-06-22 15:41:59 -07002816 }
Reinette Chatre95f0b772018-06-22 15:41:59 -07002817
James Morse2e667812021-07-28 17:06:28 +00002818 ret = resctrl_arch_update_domains(r, rdtgrp->closid);
Reinette Chatre95f0b772018-06-22 15:41:59 -07002819 if (ret < 0) {
Babu Moger723f1a0d2018-11-21 20:28:43 +00002820 rdt_last_cmd_puts("Failed to initialize allocations\n");
Reinette Chatre95f0b772018-06-22 15:41:59 -07002821 return ret;
2822 }
Xiaochen Shen47820e72019-04-17 19:08:49 +08002823
Reinette Chatre95f0b772018-06-22 15:41:59 -07002824 }
2825
Xiaochen Shen40fba002019-04-10 03:53:49 +08002826 rdtgrp->mode = RDT_MODE_SHAREABLE;
2827
Reinette Chatre95f0b772018-06-22 15:41:59 -07002828 return 0;
2829}
2830
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002831static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002832 const char *name, umode_t mode,
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002833 enum rdt_group_type rtype, struct rdtgroup **r)
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002834{
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002835 struct rdtgroup *prdtgrp, *rdtgrp;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002836 struct kernfs_node *kn;
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002837 uint files = 0;
2838 int ret;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002839
Xiaochen Shen334b0f42020-01-09 00:28:05 +08002840 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002841 if (!prdtgrp) {
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002842 ret = -ENODEV;
2843 goto out_unlock;
2844 }
2845
Reinette Chatrec966dac2018-06-22 15:42:12 -07002846 if (rtype == RDTMON_GROUP &&
2847 (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2848 prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
2849 ret = -EINVAL;
Babu Moger723f1a0d2018-11-21 20:28:43 +00002850 rdt_last_cmd_puts("Pseudo-locking in progress\n");
Reinette Chatrec966dac2018-06-22 15:42:12 -07002851 goto out_unlock;
2852 }
2853
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002854 /* allocate the rdtgroup. */
2855 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
2856 if (!rdtgrp) {
2857 ret = -ENOSPC;
Babu Moger723f1a0d2018-11-21 20:28:43 +00002858 rdt_last_cmd_puts("Kernel out of memory\n");
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002859 goto out_unlock;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002860 }
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002861 *r = rdtgrp;
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002862 rdtgrp->mon.parent = prdtgrp;
2863 rdtgrp->type = rtype;
2864 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002865
2866 /* kernfs creates the directory for rdtgrp */
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002867 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002868 if (IS_ERR(kn)) {
2869 ret = PTR_ERR(kn);
Tony Luckcfd0f342017-09-25 16:39:37 -07002870 rdt_last_cmd_puts("kernfs create error\n");
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002871 goto out_free_rgrp;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002872 }
2873 rdtgrp->kn = kn;
2874
2875 /*
2876 * kernfs_remove() will drop the reference count on "kn" which
2877 * will free it. But we still need it to stick around for the
Xiaochen Shenfd8d9db2020-10-31 03:10:53 +08002878 * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
Xiaochen Shen75899922020-10-31 03:11:28 +08002879 * which will be dropped by kernfs_put() in rdtgroup_remove().
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002880 */
2881 kernfs_get(kn);
2882
2883 ret = rdtgroup_kn_set_ugid(kn);
Tony Luckcfd0f342017-09-25 16:39:37 -07002884 if (ret) {
2885 rdt_last_cmd_puts("kernfs perm error\n");
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002886 goto out_destroy;
Tony Luckcfd0f342017-09-25 16:39:37 -07002887 }
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002888
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002889 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002890 ret = rdtgroup_add_files(kn, files);
Tony Luckcfd0f342017-09-25 16:39:37 -07002891 if (ret) {
2892 rdt_last_cmd_puts("kernfs fill error\n");
Tony Luck12e01102016-10-28 15:04:45 -07002893 goto out_destroy;
Tony Luckcfd0f342017-09-25 16:39:37 -07002894 }
Tony Luck12e01102016-10-28 15:04:45 -07002895
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002896 if (rdt_mon_capable) {
2897 ret = alloc_rmid();
Tony Luckcfd0f342017-09-25 16:39:37 -07002898 if (ret < 0) {
Babu Moger723f1a0d2018-11-21 20:28:43 +00002899 rdt_last_cmd_puts("Out of RMIDs\n");
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002900 goto out_destroy;
Tony Luckcfd0f342017-09-25 16:39:37 -07002901 }
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002902 rdtgrp->mon.rmid = ret;
Vikas Shivappad89b7372017-07-25 14:14:38 -07002903
2904 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
Tony Luckcfd0f342017-09-25 16:39:37 -07002905 if (ret) {
2906 rdt_last_cmd_puts("kernfs subdir error\n");
Vikas Shivappad89b7372017-07-25 14:14:38 -07002907 goto out_idfree;
Tony Luckcfd0f342017-09-25 16:39:37 -07002908 }
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002909 }
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002910 kernfs_activate(kn);
2911
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002912 /*
Xiaochen Shen334b0f42020-01-09 00:28:05 +08002913 * The caller unlocks the parent_kn upon success.
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002914 */
2915 return 0;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002916
Vikas Shivappad89b7372017-07-25 14:14:38 -07002917out_idfree:
2918 free_rmid(rdtgrp->mon.rmid);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002919out_destroy:
Xiaochen Shen75899922020-10-31 03:11:28 +08002920 kernfs_put(rdtgrp->kn);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002921 kernfs_remove(rdtgrp->kn);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002922out_free_rgrp:
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002923 kfree(rdtgrp);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002924out_unlock:
Xiaochen Shen334b0f42020-01-09 00:28:05 +08002925 rdtgroup_kn_unlock(parent_kn);
Fenghua Yu60cf5e12016-10-28 15:04:44 -07002926 return ret;
2927}
2928
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002929static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
2930{
2931 kernfs_remove(rgrp->kn);
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002932 free_rmid(rgrp->mon.rmid);
Xiaochen Shen75899922020-10-31 03:11:28 +08002933 rdtgroup_remove(rgrp);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002934}
2935
2936/*
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002937 * Create a monitor group under "mon_groups" directory of a control
2938 * and monitor group(ctrl_mon). This is a resource group
2939 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002940 */
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002941static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
Xiaochen Shen32ada3b2020-01-09 00:28:06 +08002942 const char *name, umode_t mode)
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002943{
2944 struct rdtgroup *rdtgrp, *prgrp;
2945 int ret;
2946
Xiaochen Shen32ada3b2020-01-09 00:28:06 +08002947 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002948 if (ret)
2949 return ret;
2950
2951 prgrp = rdtgrp->mon.parent;
2952 rdtgrp->closid = prgrp->closid;
2953
2954 /*
2955 * Add the rdtgrp to the list of rdtgrps the parent
2956 * ctrl_mon group has to track.
2957 */
2958 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
2959
Xiaochen Shen334b0f42020-01-09 00:28:05 +08002960 rdtgroup_kn_unlock(parent_kn);
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002961 return ret;
2962}
2963
2964/*
2965 * These are rdtgroups created under the root directory. Can be used
2966 * to allocate and monitor resources.
2967 */
2968static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002969 const char *name, umode_t mode)
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002970{
2971 struct rdtgroup *rdtgrp;
2972 struct kernfs_node *kn;
2973 u32 closid;
2974 int ret;
2975
Xiaochen Shen32ada3b2020-01-09 00:28:06 +08002976 ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002977 if (ret)
2978 return ret;
2979
2980 kn = rdtgrp->kn;
2981 ret = closid_alloc();
Tony Luckcfd0f342017-09-25 16:39:37 -07002982 if (ret < 0) {
Babu Moger723f1a0d2018-11-21 20:28:43 +00002983 rdt_last_cmd_puts("Out of CLOSIDs\n");
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002984 goto out_common_fail;
Tony Luckcfd0f342017-09-25 16:39:37 -07002985 }
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002986 closid = ret;
Wang Hui36e74d32018-02-22 19:26:03 -08002987 ret = 0;
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002988
2989 rdtgrp->closid = closid;
Reinette Chatre95f0b772018-06-22 15:41:59 -07002990 ret = rdtgroup_init_alloc(rdtgrp);
2991 if (ret < 0)
2992 goto out_id_free;
2993
Vikas Shivappa65b4f402017-07-25 14:14:31 -07002994 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
2995
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07002996 if (rdt_mon_capable) {
2997 /*
2998 * Create an empty mon_groups directory to hold the subset
2999 * of tasks and cpus to monitor.
3000 */
Xiaochen Shen334b0f42020-01-09 00:28:05 +08003001 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
Tony Luckcfd0f342017-09-25 16:39:37 -07003002 if (ret) {
3003 rdt_last_cmd_puts("kernfs subdir error\n");
Reinette Chatre95f0b772018-06-22 15:41:59 -07003004 goto out_del_list;
Tony Luckcfd0f342017-09-25 16:39:37 -07003005 }
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07003006 }
3007
Vikas Shivappa65b4f402017-07-25 14:14:31 -07003008 goto out_unlock;
3009
Reinette Chatre95f0b772018-06-22 15:41:59 -07003010out_del_list:
3011 list_del(&rdtgrp->rdtgroup_list);
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07003012out_id_free:
3013 closid_free(closid);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07003014out_common_fail:
3015 mkdir_rdt_prepare_clean(rdtgrp);
3016out_unlock:
Xiaochen Shen334b0f42020-01-09 00:28:05 +08003017 rdtgroup_kn_unlock(parent_kn);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07003018 return ret;
3019}
3020
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07003021/*
3022 * We allow creating mon groups only with in a directory called "mon_groups"
3023 * which is present in every ctrl_mon group. Check if this is a valid
3024 * "mon_groups" directory.
3025 *
3026 * 1. The directory should be named "mon_groups".
3027 * 2. The mon group itself should "not" be named "mon_groups".
3028 * This makes sure "mon_groups" directory always has a ctrl_mon group
3029 * as parent.
3030 */
3031static bool is_mon_groups(struct kernfs_node *kn, const char *name)
3032{
3033 return (!strcmp(kn->name, "mon_groups") &&
3034 strcmp(name, "mon_groups"));
3035}
3036
Vikas Shivappa65b4f402017-07-25 14:14:31 -07003037static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3038 umode_t mode)
3039{
3040 /* Do not accept '\n' to avoid unparsable situation. */
3041 if (strchr(name, '\n'))
3042 return -EINVAL;
3043
3044 /*
3045 * If the parent directory is the root directory and RDT
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07003046 * allocation is supported, add a control and monitoring
3047 * subdirectory
Vikas Shivappa65b4f402017-07-25 14:14:31 -07003048 */
3049 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
Xiaochen Shen32ada3b2020-01-09 00:28:06 +08003050 return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07003051
3052 /*
3053 * If RDT monitoring is supported and the parent directory is a valid
3054 * "mon_groups" directory, add a monitoring subdirectory.
3055 */
3056 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
Xiaochen Shen32ada3b2020-01-09 00:28:06 +08003057 return rdtgroup_mkdir_mon(parent_kn, name, mode);
Vikas Shivappa65b4f402017-07-25 14:14:31 -07003058
3059 return -EPERM;
3060}
3061
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003062static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003063{
3064 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3065 int cpu;
3066
3067 /* Give any tasks back to the parent group */
3068 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
3069
3070 /* Update per cpu rmid of the moved CPUs first */
3071 for_each_cpu(cpu, &rdtgrp->cpu_mask)
Vikas Shivappaa9110b52017-08-09 11:46:34 -07003072 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003073 /*
3074 * Update the MSR on moved CPUs and CPUs which have moved
3075 * task running on them.
3076 */
3077 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3078 update_closid_rmid(tmpmask, NULL);
3079
3080 rdtgrp->flags = RDT_DELETED;
3081 free_rmid(rdtgrp->mon.rmid);
3082
3083 /*
3084 * Remove the rdtgrp from the parent ctrl_mon group's list
3085 */
3086 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3087 list_del(&rdtgrp->mon.crdtgrp_list);
3088
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003089 kernfs_remove(rdtgrp->kn);
3090
3091 return 0;
3092}
3093
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003094static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
Reinette Chatre17eafd02018-06-22 15:42:18 -07003095{
3096 rdtgrp->flags = RDT_DELETED;
3097 list_del(&rdtgrp->rdtgroup_list);
3098
Reinette Chatre17eafd02018-06-22 15:42:18 -07003099 kernfs_remove(rdtgrp->kn);
3100 return 0;
3101}
3102
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003103static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
Fenghua Yu60cf5e12016-10-28 15:04:44 -07003104{
Vikas Shivappaf9049542017-07-25 14:14:39 -07003105 int cpu;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07003106
Fenghua Yue02737d2016-10-28 15:04:46 -07003107 /* Give any tasks back to the default group */
Fenghua Yu0efc89b2016-11-18 15:18:04 -08003108 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
Fenghua Yue02737d2016-10-28 15:04:46 -07003109
Tony Luck12e01102016-10-28 15:04:45 -07003110 /* Give any CPUs back to the default group */
3111 cpumask_or(&rdtgroup_default.cpu_mask,
3112 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
Fenghua Yu0efc89b2016-11-18 15:18:04 -08003113
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003114 /* Update per cpu closid and rmid of the moved CPUs first */
3115 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
Vikas Shivappaa9110b52017-08-09 11:46:34 -07003116 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3117 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003118 }
3119
Fenghua Yu0efc89b2016-11-18 15:18:04 -08003120 /*
3121 * Update the MSR on moved CPUs and CPUs which have moved
3122 * task running on them.
3123 */
3124 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
Vikas Shivappaa9fcf862017-07-25 14:14:36 -07003125 update_closid_rmid(tmpmask, NULL);
Tony Luck12e01102016-10-28 15:04:45 -07003126
Fenghua Yu60cf5e12016-10-28 15:04:44 -07003127 closid_free(rdtgrp->closid);
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003128 free_rmid(rdtgrp->mon.rmid);
3129
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003130 rdtgroup_ctrl_remove(rdtgrp);
Xiaochen Shen074fadee2020-01-09 00:28:04 +08003131
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003132 /*
3133 * Free all the child monitor group rmids.
3134 */
3135 free_all_child_rdtgrp(rdtgrp);
3136
Vikas Shivappaf9049542017-07-25 14:14:39 -07003137 return 0;
3138}
3139
3140static int rdtgroup_rmdir(struct kernfs_node *kn)
3141{
3142 struct kernfs_node *parent_kn = kn->parent;
3143 struct rdtgroup *rdtgrp;
3144 cpumask_var_t tmpmask;
3145 int ret = 0;
3146
3147 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3148 return -ENOMEM;
3149
3150 rdtgrp = rdtgroup_kn_lock_live(kn);
3151 if (!rdtgrp) {
3152 ret = -EPERM;
3153 goto out;
3154 }
3155
3156 /*
3157 * If the rdtgroup is a ctrl_mon group and parent directory
Vikas Shivappaf3cbeac2017-07-25 14:14:40 -07003158 * is the root directory, remove the ctrl_mon group.
3159 *
3160 * If the rdtgroup is a mon group and parent directory
3161 * is a valid "mon_groups" directory, remove the mon group.
Vikas Shivappaf9049542017-07-25 14:14:39 -07003162 */
Reinette Chatreb0151da2020-03-17 09:26:45 -07003163 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3164 rdtgrp != &rdtgroup_default) {
Reinette Chatree0bdfe82018-06-22 15:42:22 -07003165 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3166 rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003167 ret = rdtgroup_ctrl_remove(rdtgrp);
Reinette Chatree0bdfe82018-06-22 15:42:22 -07003168 } else {
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003169 ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
Reinette Chatree0bdfe82018-06-22 15:42:22 -07003170 }
3171 } else if (rdtgrp->type == RDTMON_GROUP &&
3172 is_mon_groups(parent_kn, kn->name)) {
Xiaochen Shen19eb86a2020-12-01 02:06:58 +08003173 ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
Reinette Chatree0bdfe82018-06-22 15:42:22 -07003174 } else {
Vikas Shivappaf9049542017-07-25 14:14:39 -07003175 ret = -EPERM;
Reinette Chatree0bdfe82018-06-22 15:42:22 -07003176 }
Vikas Shivappaf9049542017-07-25 14:14:39 -07003177
Fenghua Yu0efc89b2016-11-18 15:18:04 -08003178out:
Fenghua Yu60cf5e12016-10-28 15:04:44 -07003179 rdtgroup_kn_unlock(kn);
Fenghua Yu0efc89b2016-11-18 15:18:04 -08003180 free_cpumask_var(tmpmask);
3181 return ret;
Fenghua Yu60cf5e12016-10-28 15:04:44 -07003182}
3183
Shaohua Li76ae054c2016-12-02 14:21:06 -08003184static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3185{
James Morsec091e902021-07-28 17:06:24 +00003186 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
Shaohua Li76ae054c2016-12-02 14:21:06 -08003187 seq_puts(seq, ",cdp");
Xiaochen Shen2cc81c692018-09-12 16:48:38 -07003188
James Morsec091e902021-07-28 17:06:24 +00003189 if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
Xiaochen Shen2cc81c692018-09-12 16:48:38 -07003190 seq_puts(seq, ",cdpl2");
3191
James Morse63c8b122021-07-28 17:06:14 +00003192 if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
Xiaochen Shen2cc81c692018-09-12 16:48:38 -07003193 seq_puts(seq, ",mba_MBps");
3194
Shaohua Li76ae054c2016-12-02 14:21:06 -08003195 return 0;
3196}
3197
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003198static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
Shaohua Li76ae054c2016-12-02 14:21:06 -08003199 .mkdir = rdtgroup_mkdir,
3200 .rmdir = rdtgroup_rmdir,
3201 .show_options = rdtgroup_show_options,
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003202};
3203
3204static int __init rdtgroup_setup_root(void)
3205{
Tony Luck12e01102016-10-28 15:04:45 -07003206 int ret;
3207
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003208 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
Reinette Chatre21220bb2018-06-22 15:42:09 -07003209 KERNFS_ROOT_CREATE_DEACTIVATED |
3210 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003211 &rdtgroup_default);
3212 if (IS_ERR(rdt_root))
3213 return PTR_ERR(rdt_root);
3214
3215 mutex_lock(&rdtgroup_mutex);
3216
3217 rdtgroup_default.closid = 0;
Vikas Shivappac7d9aac2017-07-25 14:14:32 -07003218 rdtgroup_default.mon.rmid = 0;
3219 rdtgroup_default.type = RDTCTRL_GROUP;
3220 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3221
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003222 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3223
Greg Kroah-Hartmanf2eb4782022-02-22 08:07:13 +01003224 ret = rdtgroup_add_files(kernfs_root_to_node(rdt_root), RF_CTRL_BASE);
Tony Luck12e01102016-10-28 15:04:45 -07003225 if (ret) {
3226 kernfs_destroy_root(rdt_root);
3227 goto out;
3228 }
3229
Greg Kroah-Hartmanf2eb4782022-02-22 08:07:13 +01003230 rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003231 kernfs_activate(rdtgroup_default.kn);
3232
Tony Luck12e01102016-10-28 15:04:45 -07003233out:
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003234 mutex_unlock(&rdtgroup_mutex);
3235
Tony Luck12e01102016-10-28 15:04:45 -07003236 return ret;
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003237}
3238
3239/*
3240 * rdtgroup_init - rdtgroup initialization
3241 *
3242 * Setup resctrl file system including set up root, create mount point,
3243 * register rdtgroup filesystem, and initialize files under root directory.
3244 *
3245 * Return: 0 on success or -errno
3246 */
3247int __init rdtgroup_init(void)
3248{
3249 int ret = 0;
3250
Tony Luck9b3a7fd2017-09-25 16:39:33 -07003251 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
3252 sizeof(last_cmd_status_buf));
3253
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003254 ret = rdtgroup_setup_root();
3255 if (ret)
3256 return ret;
3257
3258 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
3259 if (ret)
3260 goto cleanup_root;
3261
3262 ret = register_filesystem(&rdt_fs_type);
3263 if (ret)
3264 goto cleanup_mountpoint;
3265
Reinette Chatre37707ec2018-06-22 15:42:25 -07003266 /*
3267 * Adding the resctrl debugfs directory here may not be ideal since
3268 * it would let the resctrl debugfs directory appear on the debugfs
3269 * filesystem before the resctrl filesystem is mounted.
3270 * It may also be ok since that would enable debugging of RDT before
3271 * resctrl is mounted.
3272 * The reason why the debugfs directory is created here and not in
James Morseae0fbed2020-07-08 16:39:22 +00003273 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
Reinette Chatre37707ec2018-06-22 15:42:25 -07003274 * during the debugfs directory creation also &sb->s_type->i_mutex_key
3275 * (the lockdep class of inode->i_rwsem). Other filesystem
3276 * interactions (eg. SyS_getdents) have the lock ordering:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003277 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
3278 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
Reinette Chatre37707ec2018-06-22 15:42:25 -07003279 * is taken, thus creating dependency:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003280 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
Reinette Chatre37707ec2018-06-22 15:42:25 -07003281 * issues considering the other two lock dependencies.
3282 * By creating the debugfs directory here we avoid a dependency
3283 * that may cause deadlock (even though file operations cannot
3284 * occur until the filesystem is mounted, but I do not know how to
3285 * tell lockdep that).
3286 */
3287 debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
3288
Fenghua Yu5ff193f2016-10-28 15:04:42 -07003289 return 0;
3290
3291cleanup_mountpoint:
3292 sysfs_remove_mount_point(fs_kobj, "resctrl");
3293cleanup_root:
3294 kernfs_destroy_root(rdt_root);
3295
3296 return ret;
3297}
Reinette Chatre0af6a482018-06-22 15:42:24 -07003298
3299void __exit rdtgroup_exit(void)
3300{
Reinette Chatre37707ec2018-06-22 15:42:25 -07003301 debugfs_remove_recursive(debugfs_resctrl);
Reinette Chatre0af6a482018-06-22 15:42:24 -07003302 unregister_filesystem(&rdt_fs_type);
3303 sysfs_remove_mount_point(fs_kobj, "resctrl");
3304 kernfs_destroy_root(rdt_root);
3305}