1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * User interface for Resource Allocation in Resource Director Technology(RDT)
4  *
5  * Copyright (C) 2016 Intel Corporation
6  *
7  * Author: Fenghua Yu <fenghua.yu@intel.com>
8  *
9  * 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 
15 #include <linux/cacheinfo.h>
16 #include <linux/cpu.h>
17 #include <linux/debugfs.h>
18 #include <linux/fs.h>
19 #include <linux/fs_parser.h>
20 #include <linux/sysfs.h>
21 #include <linux/kernfs.h>
22 #include <linux/seq_buf.h>
23 #include <linux/seq_file.h>
24 #include <linux/sched/signal.h>
25 #include <linux/sched/task.h>
26 #include <linux/slab.h>
27 #include <linux/task_work.h>
28 #include <linux/user_namespace.h>
29 
30 #include <uapi/linux/magic.h>
31 
32 #include <asm/resctrl.h>
33 #include "internal.h"
34 
35 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
36 DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
37 DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
38 static struct kernfs_root *rdt_root;
39 struct rdtgroup rdtgroup_default;
40 LIST_HEAD(rdt_all_groups);
41 
42 /* list of entries for the schemata file */
43 LIST_HEAD(resctrl_schema_all);
44 
45 /* Kernel fs node for "info" directory under root */
46 static struct kernfs_node *kn_info;
47 
48 /* Kernel fs node for "mon_groups" directory under root */
49 static struct kernfs_node *kn_mongrp;
50 
51 /* Kernel fs node for "mon_data" directory under root */
52 static struct kernfs_node *kn_mondata;
53 
54 static struct seq_buf last_cmd_status;
55 static char last_cmd_status_buf[512];
56 
57 struct dentry *debugfs_resctrl;
58 
rdt_last_cmd_clear(void)59 void rdt_last_cmd_clear(void)
60 {
61 	lockdep_assert_held(&rdtgroup_mutex);
62 	seq_buf_clear(&last_cmd_status);
63 }
64 
rdt_last_cmd_puts(const char * s)65 void rdt_last_cmd_puts(const char *s)
66 {
67 	lockdep_assert_held(&rdtgroup_mutex);
68 	seq_buf_puts(&last_cmd_status, s);
69 }
70 
rdt_last_cmd_printf(const char * fmt,...)71 void 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 
81 /*
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  */
96 static int closid_free_map;
97 static int closid_free_map_len;
98 
closids_supported(void)99 int closids_supported(void)
100 {
101 	return closid_free_map_len;
102 }
103 
closid_init(void)104 static void closid_init(void)
105 {
106 	struct resctrl_schema *s;
107 	u32 rdt_min_closid = 32;
108 
109 	/* Compute rdt_min_closid across all resources */
110 	list_for_each_entry(s, &resctrl_schema_all, list)
111 		rdt_min_closid = min(rdt_min_closid, s->num_closid);
112 
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;
117 	closid_free_map_len = rdt_min_closid;
118 }
119 
closid_alloc(void)120 static int closid_alloc(void)
121 {
122 	u32 closid = ffs(closid_free_map);
123 
124 	if (closid == 0)
125 		return -ENOSPC;
126 	closid--;
127 	closid_free_map &= ~(1 << closid);
128 
129 	return closid;
130 }
131 
closid_free(int closid)132 void closid_free(int closid)
133 {
134 	closid_free_map |= 1 << closid;
135 }
136 
137 /**
138  * 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  */
closid_allocated(unsigned int closid)144 static bool closid_allocated(unsigned int closid)
145 {
146 	return (closid_free_map & (1 << closid)) == 0;
147 }
148 
149 /**
150  * 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  */
rdtgroup_mode_by_closid(int closid)158 enum 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 
170 static const char * const rdt_mode_str[] = {
171 	[RDT_MODE_SHAREABLE]		= "shareable",
172 	[RDT_MODE_EXCLUSIVE]		= "exclusive",
173 	[RDT_MODE_PSEUDO_LOCKSETUP]	= "pseudo-locksetup",
174 	[RDT_MODE_PSEUDO_LOCKED]	= "pseudo-locked",
175 };
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  */
rdtgroup_mode_str(enum rdtgrp_mode mode)183 static 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 
191 /* set uid and gid of rdtgroup dirs and files to that of the creator */
rdtgroup_kn_set_ugid(struct kernfs_node * kn)192 static 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 
rdtgroup_add_file(struct kernfs_node * parent_kn,struct rftype * rft)205 static 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,
211 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
212 				  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 
rdtgroup_seqfile_show(struct seq_file * m,void * arg)225 static 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 
rdtgroup_file_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)235 static 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 
246 static const struct kernfs_ops rdtgroup_kf_single_ops = {
247 	.atomic_write_len	= PAGE_SIZE,
248 	.write			= rdtgroup_file_write,
249 	.seq_show		= rdtgroup_seqfile_show,
250 };
251 
252 static const struct kernfs_ops kf_mondata_ops = {
253 	.atomic_write_len	= PAGE_SIZE,
254 	.seq_show		= rdtgroup_mondata_show,
255 };
256 
is_cpu_list(struct kernfs_open_file * of)257 static 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 
rdtgroup_cpus_show(struct kernfs_open_file * of,struct seq_file * s,void * v)264 static int rdtgroup_cpus_show(struct kernfs_open_file *of,
265 			      struct seq_file *s, void *v)
266 {
267 	struct rdtgroup *rdtgrp;
268 	struct cpumask *mask;
269 	int ret = 0;
270 
271 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
272 
273 	if (rdtgrp) {
274 		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 {
286 			seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
287 				   cpumask_pr_args(&rdtgrp->cpu_mask));
288 		}
289 	} else {
290 		ret = -ENOENT;
291 	}
292 	rdtgroup_kn_unlock(of->kn);
293 
294 	return ret;
295 }
296 
297 /*
298  * This is safe against resctrl_sched_in() called from __switch_to()
299  * because __switch_to() is executed with interrupts disabled. A local call
300  * from update_closid_rmid() is protected against __switch_to() because
301  * preemption is disabled.
302  */
update_cpu_closid_rmid(void * info)303 static void update_cpu_closid_rmid(void *info)
304 {
305 	struct rdtgroup *r = info;
306 
307 	if (r) {
308 		this_cpu_write(pqr_state.default_closid, r->closid);
309 		this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
310 	}
311 
312 	/*
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 	 */
317 	resctrl_sched_in();
318 }
319 
320 /*
321  * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
322  *
323  * Per task closids/rmids must have been set up before calling this function.
324  */
325 static void
update_closid_rmid(const struct cpumask * cpu_mask,struct rdtgroup * r)326 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
327 {
328 	int cpu = get_cpu();
329 
330 	if (cpumask_test_cpu(cpu, cpu_mask))
331 		update_cpu_closid_rmid(r);
332 	smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
333 	put_cpu();
334 }
335 
cpus_mon_write(struct rdtgroup * rdtgrp,cpumask_var_t newmask,cpumask_var_t tmpmask)336 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
337 			  cpumask_var_t tmpmask)
338 {
339 	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);
344 	if (!cpumask_empty(tmpmask)) {
345 		rdt_last_cmd_puts("Can only add CPUs to mongroup that belong to parent\n");
346 		return -EINVAL;
347 	}
348 
349 	/* Check whether cpus are dropped from this group */
350 	cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
351 	if (!cpumask_empty(tmpmask)) {
352 		/* 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);
362 	if (!cpumask_empty(tmpmask)) {
363 		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 
cpumask_rdtgrp_clear(struct rdtgroup * r,struct cpumask * m)379 static 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 
cpus_ctrl_write(struct rdtgroup * rdtgrp,cpumask_var_t newmask,cpumask_var_t tmpmask,cpumask_var_t tmpmask1)389 static 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;
394 
395 	/* Check whether cpus are dropped from this group */
396 	cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
397 	if (!cpumask_empty(tmpmask)) {
398 		/* Can't drop from default group */
399 		if (rdtgrp == &rdtgroup_default) {
400 			rdt_last_cmd_puts("Can't drop CPUs from default group\n");
401 			return -EINVAL;
402 		}
403 
404 		/* Give any dropped cpus to rdtgroup_default */
405 		cpumask_or(&rdtgroup_default.cpu_mask,
406 			   &rdtgroup_default.cpu_mask, tmpmask);
407 		update_closid_rmid(tmpmask, &rdtgroup_default);
408 	}
409 
410 	/*
411 	 * 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.
414 	 */
415 	cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
416 	if (!cpumask_empty(tmpmask)) {
417 		list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
418 			if (r == rdtgrp)
419 				continue;
420 			cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
421 			if (!cpumask_empty(tmpmask1))
422 				cpumask_rdtgrp_clear(r, tmpmask1);
423 		}
424 		update_closid_rmid(tmpmask, rdtgrp);
425 	}
426 
427 	/* Done pushing/pulling - update this group with new mask */
428 	cpumask_copy(&rdtgrp->cpu_mask, newmask);
429 
430 	/*
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 
441 	return 0;
442 }
443 
rdtgroup_cpus_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)444 static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
445 				   char *buf, size_t nbytes, loff_t off)
446 {
447 	cpumask_var_t tmpmask, newmask, tmpmask1;
448 	struct rdtgroup *rdtgrp;
449 	int ret;
450 
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 	}
460 	if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
461 		free_cpumask_var(tmpmask);
462 		free_cpumask_var(newmask);
463 		return -ENOMEM;
464 	}
465 
466 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
467 	if (!rdtgrp) {
468 		ret = -ENOENT;
469 		goto unlock;
470 	}
471 
472 	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
473 	    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
474 		ret = -EINVAL;
475 		rdt_last_cmd_puts("Pseudo-locking in progress\n");
476 		goto unlock;
477 	}
478 
479 	if (is_cpu_list(of))
480 		ret = cpulist_parse(buf, newmask);
481 	else
482 		ret = cpumask_parse(buf, newmask);
483 
484 	if (ret) {
485 		rdt_last_cmd_puts("Bad CPU list/mask\n");
486 		goto unlock;
487 	}
488 
489 	/* check that user didn't specify any offline cpus */
490 	cpumask_andnot(tmpmask, newmask, cpu_online_mask);
491 	if (!cpumask_empty(tmpmask)) {
492 		ret = -EINVAL;
493 		rdt_last_cmd_puts("Can only assign online CPUs\n");
494 		goto unlock;
495 	}
496 
497 	if (rdtgrp->type == RDTCTRL_GROUP)
498 		ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
499 	else if (rdtgrp->type == RDTMON_GROUP)
500 		ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
501 	else
502 		ret = -EINVAL;
503 
504 unlock:
505 	rdtgroup_kn_unlock(of->kn);
506 	free_cpumask_var(tmpmask);
507 	free_cpumask_var(newmask);
508 	free_cpumask_var(tmpmask1);
509 
510 	return ret ?: nbytes;
511 }
512 
513 /**
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  */
rdtgroup_remove(struct rdtgroup * rdtgrp)525 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
526 {
527 	kernfs_put(rdtgrp->kn);
528 	kfree(rdtgrp);
529 }
530 
_update_task_closid_rmid(void * task)531 static void _update_task_closid_rmid(void *task)
532 {
533 	/*
534 	 * 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.
536 	 */
537 	if (task == current)
538 		resctrl_sched_in();
539 }
540 
update_task_closid_rmid(struct task_struct * t)541 static 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);
547 }
548 
__rdtgroup_move_task(struct task_struct * tsk,struct rdtgroup * rdtgrp)549 static int __rdtgroup_move_task(struct task_struct *tsk,
550 				struct rdtgroup *rdtgrp)
551 {
552 	/* 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 
559 	/*
560 	 * 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.
566 	 */
567 
568 	if (rdtgrp->type == RDTCTRL_GROUP) {
569 		WRITE_ONCE(tsk->closid, rdtgrp->closid);
570 		WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
571 	} else if (rdtgrp->type == RDTMON_GROUP) {
572 		if (rdtgrp->mon.parent->closid == tsk->closid) {
573 			WRITE_ONCE(tsk->rmid, rdtgrp->mon.rmid);
574 		} else {
575 			rdt_last_cmd_puts("Can't move task to different control group\n");
576 			return -EINVAL;
577 		}
578 	}
579 
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 	 * This pairs with the full barrier between the rq->curr update and
584 	 * resctrl_sched_in() during context switch.
585 	 */
586 	smp_mb();
587 
588 	/*
589 	 * By now, the task's closid and rmid are set. If the task is current
590 	 * on a CPU, the PQR_ASSOC MSR needs to be updated to make the resource
591 	 * group go into effect. If the task is not current, the MSR will be
592 	 * updated when the task is scheduled in.
593 	 */
594 	update_task_closid_rmid(tsk);
595 
596 	return 0;
597 }
598 
is_closid_match(struct task_struct * t,struct rdtgroup * r)599 static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
600 {
601 	return (rdt_alloc_capable &&
602 	       (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
603 }
604 
is_rmid_match(struct task_struct * t,struct rdtgroup * r)605 static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
606 {
607 	return (rdt_mon_capable &&
608 	       (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
609 }
610 
611 /**
612  * rdtgroup_tasks_assigned - Test if tasks have been assigned to resource group
613  * @r: Resource group
614  *
615  * Return: 1 if tasks have been assigned to @r, 0 otherwise
616  */
rdtgroup_tasks_assigned(struct rdtgroup * r)617 int rdtgroup_tasks_assigned(struct rdtgroup *r)
618 {
619 	struct task_struct *p, *t;
620 	int ret = 0;
621 
622 	lockdep_assert_held(&rdtgroup_mutex);
623 
624 	rcu_read_lock();
625 	for_each_process_thread(p, t) {
626 		if (is_closid_match(t, r) || is_rmid_match(t, r)) {
627 			ret = 1;
628 			break;
629 		}
630 	}
631 	rcu_read_unlock();
632 
633 	return ret;
634 }
635 
rdtgroup_task_write_permission(struct task_struct * task,struct kernfs_open_file * of)636 static int rdtgroup_task_write_permission(struct task_struct *task,
637 					  struct kernfs_open_file *of)
638 {
639 	const struct cred *tcred = get_task_cred(task);
640 	const struct cred *cred = current_cred();
641 	int ret = 0;
642 
643 	/*
644 	 * Even if we're attaching all tasks in the thread group, we only
645 	 * need to check permissions on one of them.
646 	 */
647 	if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
648 	    !uid_eq(cred->euid, tcred->uid) &&
649 	    !uid_eq(cred->euid, tcred->suid)) {
650 		rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
651 		ret = -EPERM;
652 	}
653 
654 	put_cred(tcred);
655 	return ret;
656 }
657 
rdtgroup_move_task(pid_t pid,struct rdtgroup * rdtgrp,struct kernfs_open_file * of)658 static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
659 			      struct kernfs_open_file *of)
660 {
661 	struct task_struct *tsk;
662 	int ret;
663 
664 	rcu_read_lock();
665 	if (pid) {
666 		tsk = find_task_by_vpid(pid);
667 		if (!tsk) {
668 			rcu_read_unlock();
669 			rdt_last_cmd_printf("No task %d\n", pid);
670 			return -ESRCH;
671 		}
672 	} else {
673 		tsk = current;
674 	}
675 
676 	get_task_struct(tsk);
677 	rcu_read_unlock();
678 
679 	ret = rdtgroup_task_write_permission(tsk, of);
680 	if (!ret)
681 		ret = __rdtgroup_move_task(tsk, rdtgrp);
682 
683 	put_task_struct(tsk);
684 	return ret;
685 }
686 
rdtgroup_tasks_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)687 static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
688 				    char *buf, size_t nbytes, loff_t off)
689 {
690 	struct rdtgroup *rdtgrp;
691 	int ret = 0;
692 	pid_t pid;
693 
694 	if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
695 		return -EINVAL;
696 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
697 	if (!rdtgrp) {
698 		rdtgroup_kn_unlock(of->kn);
699 		return -ENOENT;
700 	}
701 	rdt_last_cmd_clear();
702 
703 	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED ||
704 	    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
705 		ret = -EINVAL;
706 		rdt_last_cmd_puts("Pseudo-locking in progress\n");
707 		goto unlock;
708 	}
709 
710 	ret = rdtgroup_move_task(pid, rdtgrp, of);
711 
712 unlock:
713 	rdtgroup_kn_unlock(of->kn);
714 
715 	return ret ?: nbytes;
716 }
717 
show_rdt_tasks(struct rdtgroup * r,struct seq_file * s)718 static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
719 {
720 	struct task_struct *p, *t;
721 
722 	rcu_read_lock();
723 	for_each_process_thread(p, t) {
724 		if (is_closid_match(t, r) || is_rmid_match(t, r))
725 			seq_printf(s, "%d\n", t->pid);
726 	}
727 	rcu_read_unlock();
728 }
729 
rdtgroup_tasks_show(struct kernfs_open_file * of,struct seq_file * s,void * v)730 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
731 			       struct seq_file *s, void *v)
732 {
733 	struct rdtgroup *rdtgrp;
734 	int ret = 0;
735 
736 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
737 	if (rdtgrp)
738 		show_rdt_tasks(rdtgrp, s);
739 	else
740 		ret = -ENOENT;
741 	rdtgroup_kn_unlock(of->kn);
742 
743 	return ret;
744 }
745 
746 #ifdef CONFIG_PROC_CPU_RESCTRL
747 
748 /*
749  * A task can only be part of one resctrl control group and of one monitor
750  * group which is associated to that control group.
751  *
752  * 1)   res:
753  *      mon:
754  *
755  *    resctrl is not available.
756  *
757  * 2)   res:/
758  *      mon:
759  *
760  *    Task is part of the root resctrl control group, and it is not associated
761  *    to any monitor group.
762  *
763  * 3)  res:/
764  *     mon:mon0
765  *
766  *    Task is part of the root resctrl control group and monitor group mon0.
767  *
768  * 4)  res:group0
769  *     mon:
770  *
771  *    Task is part of resctrl control group group0, and it is not associated
772  *    to any monitor group.
773  *
774  * 5) res:group0
775  *    mon:mon1
776  *
777  *    Task is part of resctrl control group group0 and monitor group mon1.
778  */
proc_resctrl_show(struct seq_file * s,struct pid_namespace * ns,struct pid * pid,struct task_struct * tsk)779 int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,
780 		      struct pid *pid, struct task_struct *tsk)
781 {
782 	struct rdtgroup *rdtg;
783 	int ret = 0;
784 
785 	mutex_lock(&rdtgroup_mutex);
786 
787 	/* Return empty if resctrl has not been mounted. */
788 	if (!static_branch_unlikely(&rdt_enable_key)) {
789 		seq_puts(s, "res:\nmon:\n");
790 		goto unlock;
791 	}
792 
793 	list_for_each_entry(rdtg, &rdt_all_groups, rdtgroup_list) {
794 		struct rdtgroup *crg;
795 
796 		/*
797 		 * Task information is only relevant for shareable
798 		 * and exclusive groups.
799 		 */
800 		if (rdtg->mode != RDT_MODE_SHAREABLE &&
801 		    rdtg->mode != RDT_MODE_EXCLUSIVE)
802 			continue;
803 
804 		if (rdtg->closid != tsk->closid)
805 			continue;
806 
807 		seq_printf(s, "res:%s%s\n", (rdtg == &rdtgroup_default) ? "/" : "",
808 			   rdtg->kn->name);
809 		seq_puts(s, "mon:");
810 		list_for_each_entry(crg, &rdtg->mon.crdtgrp_list,
811 				    mon.crdtgrp_list) {
812 			if (tsk->rmid != crg->mon.rmid)
813 				continue;
814 			seq_printf(s, "%s", crg->kn->name);
815 			break;
816 		}
817 		seq_putc(s, '\n');
818 		goto unlock;
819 	}
820 	/*
821 	 * The above search should succeed. Otherwise return
822 	 * with an error.
823 	 */
824 	ret = -ENOENT;
825 unlock:
826 	mutex_unlock(&rdtgroup_mutex);
827 
828 	return ret;
829 }
830 #endif
831 
rdt_last_cmd_status_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)832 static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
833 				    struct seq_file *seq, void *v)
834 {
835 	int len;
836 
837 	mutex_lock(&rdtgroup_mutex);
838 	len = seq_buf_used(&last_cmd_status);
839 	if (len)
840 		seq_printf(seq, "%.*s", len, last_cmd_status_buf);
841 	else
842 		seq_puts(seq, "ok\n");
843 	mutex_unlock(&rdtgroup_mutex);
844 	return 0;
845 }
846 
rdt_num_closids_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)847 static int rdt_num_closids_show(struct kernfs_open_file *of,
848 				struct seq_file *seq, void *v)
849 {
850 	struct resctrl_schema *s = of->kn->parent->priv;
851 
852 	seq_printf(seq, "%u\n", s->num_closid);
853 	return 0;
854 }
855 
rdt_default_ctrl_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)856 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
857 			     struct seq_file *seq, void *v)
858 {
859 	struct resctrl_schema *s = of->kn->parent->priv;
860 	struct rdt_resource *r = s->res;
861 
862 	seq_printf(seq, "%x\n", r->default_ctrl);
863 	return 0;
864 }
865 
rdt_min_cbm_bits_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)866 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
867 			     struct seq_file *seq, void *v)
868 {
869 	struct resctrl_schema *s = of->kn->parent->priv;
870 	struct rdt_resource *r = s->res;
871 
872 	seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
873 	return 0;
874 }
875 
rdt_shareable_bits_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)876 static int rdt_shareable_bits_show(struct kernfs_open_file *of,
877 				   struct seq_file *seq, void *v)
878 {
879 	struct resctrl_schema *s = of->kn->parent->priv;
880 	struct rdt_resource *r = s->res;
881 
882 	seq_printf(seq, "%x\n", r->cache.shareable_bits);
883 	return 0;
884 }
885 
886 /**
887  * rdt_bit_usage_show - Display current usage of resources
888  *
889  * A domain is a shared resource that can now be allocated differently. Here
890  * we display the current regions of the domain as an annotated bitmask.
891  * For each domain of this resource its allocation bitmask
892  * is annotated as below to indicate the current usage of the corresponding bit:
893  *   0 - currently unused
894  *   X - currently available for sharing and used by software and hardware
895  *   H - currently used by hardware only but available for software use
896  *   S - currently used and shareable by software only
897  *   E - currently used exclusively by one resource group
898  *   P - currently pseudo-locked by one resource group
899  */
rdt_bit_usage_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)900 static int rdt_bit_usage_show(struct kernfs_open_file *of,
901 			      struct seq_file *seq, void *v)
902 {
903 	struct resctrl_schema *s = of->kn->parent->priv;
904 	/*
905 	 * Use unsigned long even though only 32 bits are used to ensure
906 	 * test_bit() is used safely.
907 	 */
908 	unsigned long sw_shareable = 0, hw_shareable = 0;
909 	unsigned long exclusive = 0, pseudo_locked = 0;
910 	struct rdt_resource *r = s->res;
911 	struct rdt_domain *dom;
912 	int i, hwb, swb, excl, psl;
913 	enum rdtgrp_mode mode;
914 	bool sep = false;
915 	u32 ctrl_val;
916 
917 	mutex_lock(&rdtgroup_mutex);
918 	hw_shareable = r->cache.shareable_bits;
919 	list_for_each_entry(dom, &r->domains, list) {
920 		if (sep)
921 			seq_putc(seq, ';');
922 		sw_shareable = 0;
923 		exclusive = 0;
924 		seq_printf(seq, "%d=", dom->id);
925 		for (i = 0; i < closids_supported(); i++) {
926 			if (!closid_allocated(i))
927 				continue;
928 			ctrl_val = resctrl_arch_get_config(r, dom, i,
929 							   s->conf_type);
930 			mode = rdtgroup_mode_by_closid(i);
931 			switch (mode) {
932 			case RDT_MODE_SHAREABLE:
933 				sw_shareable |= ctrl_val;
934 				break;
935 			case RDT_MODE_EXCLUSIVE:
936 				exclusive |= ctrl_val;
937 				break;
938 			case RDT_MODE_PSEUDO_LOCKSETUP:
939 			/*
940 			 * RDT_MODE_PSEUDO_LOCKSETUP is possible
941 			 * here but not included since the CBM
942 			 * associated with this CLOSID in this mode
943 			 * is not initialized and no task or cpu can be
944 			 * assigned this CLOSID.
945 			 */
946 				break;
947 			case RDT_MODE_PSEUDO_LOCKED:
948 			case RDT_NUM_MODES:
949 				WARN(1,
950 				     "invalid mode for closid %d\n", i);
951 				break;
952 			}
953 		}
954 		for (i = r->cache.cbm_len - 1; i >= 0; i--) {
955 			pseudo_locked = dom->plr ? dom->plr->cbm : 0;
956 			hwb = test_bit(i, &hw_shareable);
957 			swb = test_bit(i, &sw_shareable);
958 			excl = test_bit(i, &exclusive);
959 			psl = test_bit(i, &pseudo_locked);
960 			if (hwb && swb)
961 				seq_putc(seq, 'X');
962 			else if (hwb && !swb)
963 				seq_putc(seq, 'H');
964 			else if (!hwb && swb)
965 				seq_putc(seq, 'S');
966 			else if (excl)
967 				seq_putc(seq, 'E');
968 			else if (psl)
969 				seq_putc(seq, 'P');
970 			else /* Unused bits remain */
971 				seq_putc(seq, '0');
972 		}
973 		sep = true;
974 	}
975 	seq_putc(seq, '\n');
976 	mutex_unlock(&rdtgroup_mutex);
977 	return 0;
978 }
979 
rdt_min_bw_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)980 static int rdt_min_bw_show(struct kernfs_open_file *of,
981 			     struct seq_file *seq, void *v)
982 {
983 	struct resctrl_schema *s = of->kn->parent->priv;
984 	struct rdt_resource *r = s->res;
985 
986 	seq_printf(seq, "%u\n", r->membw.min_bw);
987 	return 0;
988 }
989 
rdt_num_rmids_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)990 static int rdt_num_rmids_show(struct kernfs_open_file *of,
991 			      struct seq_file *seq, void *v)
992 {
993 	struct rdt_resource *r = of->kn->parent->priv;
994 
995 	seq_printf(seq, "%d\n", r->num_rmid);
996 
997 	return 0;
998 }
999 
rdt_mon_features_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1000 static int rdt_mon_features_show(struct kernfs_open_file *of,
1001 				 struct seq_file *seq, void *v)
1002 {
1003 	struct rdt_resource *r = of->kn->parent->priv;
1004 	struct mon_evt *mevt;
1005 
1006 	list_for_each_entry(mevt, &r->evt_list, list)
1007 		seq_printf(seq, "%s\n", mevt->name);
1008 
1009 	return 0;
1010 }
1011 
rdt_bw_gran_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1012 static int rdt_bw_gran_show(struct kernfs_open_file *of,
1013 			     struct seq_file *seq, void *v)
1014 {
1015 	struct resctrl_schema *s = of->kn->parent->priv;
1016 	struct rdt_resource *r = s->res;
1017 
1018 	seq_printf(seq, "%u\n", r->membw.bw_gran);
1019 	return 0;
1020 }
1021 
rdt_delay_linear_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1022 static int rdt_delay_linear_show(struct kernfs_open_file *of,
1023 			     struct seq_file *seq, void *v)
1024 {
1025 	struct resctrl_schema *s = of->kn->parent->priv;
1026 	struct rdt_resource *r = s->res;
1027 
1028 	seq_printf(seq, "%u\n", r->membw.delay_linear);
1029 	return 0;
1030 }
1031 
max_threshold_occ_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1032 static int max_threshold_occ_show(struct kernfs_open_file *of,
1033 				  struct seq_file *seq, void *v)
1034 {
1035 	seq_printf(seq, "%u\n", resctrl_rmid_realloc_threshold);
1036 
1037 	return 0;
1038 }
1039 
rdt_thread_throttle_mode_show(struct kernfs_open_file * of,struct seq_file * seq,void * v)1040 static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
1041 					 struct seq_file *seq, void *v)
1042 {
1043 	struct resctrl_schema *s = of->kn->parent->priv;
1044 	struct rdt_resource *r = s->res;
1045 
1046 	if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
1047 		seq_puts(seq, "per-thread\n");
1048 	else
1049 		seq_puts(seq, "max\n");
1050 
1051 	return 0;
1052 }
1053 
max_threshold_occ_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1054 static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
1055 				       char *buf, size_t nbytes, loff_t off)
1056 {
1057 	unsigned int bytes;
1058 	int ret;
1059 
1060 	ret = kstrtouint(buf, 0, &bytes);
1061 	if (ret)
1062 		return ret;
1063 
1064 	if (bytes > resctrl_rmid_realloc_limit)
1065 		return -EINVAL;
1066 
1067 	resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(bytes);
1068 
1069 	return nbytes;
1070 }
1071 
1072 /*
1073  * rdtgroup_mode_show - Display mode of this resource group
1074  */
rdtgroup_mode_show(struct kernfs_open_file * of,struct seq_file * s,void * v)1075 static int rdtgroup_mode_show(struct kernfs_open_file *of,
1076 			      struct seq_file *s, void *v)
1077 {
1078 	struct rdtgroup *rdtgrp;
1079 
1080 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
1081 	if (!rdtgrp) {
1082 		rdtgroup_kn_unlock(of->kn);
1083 		return -ENOENT;
1084 	}
1085 
1086 	seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode));
1087 
1088 	rdtgroup_kn_unlock(of->kn);
1089 	return 0;
1090 }
1091 
resctrl_peer_type(enum resctrl_conf_type my_type)1092 static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)
1093 {
1094 	switch (my_type) {
1095 	case CDP_CODE:
1096 		return CDP_DATA;
1097 	case CDP_DATA:
1098 		return CDP_CODE;
1099 	default:
1100 	case CDP_NONE:
1101 		return CDP_NONE;
1102 	}
1103 }
1104 
1105 /**
1106  * __rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other
1107  * @r: Resource to which domain instance @d belongs.
1108  * @d: The domain instance for which @closid is being tested.
1109  * @cbm: Capacity bitmask being tested.
1110  * @closid: Intended closid for @cbm.
1111  * @exclusive: Only check if overlaps with exclusive resource groups
1112  *
1113  * Checks if provided @cbm intended to be used for @closid on domain
1114  * @d overlaps with any other closids or other hardware usage associated
1115  * with this domain. If @exclusive is true then only overlaps with
1116  * resource groups in exclusive mode will be considered. If @exclusive
1117  * is false then overlaps with any resource group or hardware entities
1118  * will be considered.
1119  *
1120  * @cbm is unsigned long, even if only 32 bits are used, to make the
1121  * bitmap functions work correctly.
1122  *
1123  * Return: false if CBM does not overlap, true if it does.
1124  */
__rdtgroup_cbm_overlaps(struct rdt_resource * r,struct rdt_domain * d,unsigned long cbm,int closid,enum resctrl_conf_type type,bool exclusive)1125 static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
1126 				    unsigned long cbm, int closid,
1127 				    enum resctrl_conf_type type, bool exclusive)
1128 {
1129 	enum rdtgrp_mode mode;
1130 	unsigned long ctrl_b;
1131 	int i;
1132 
1133 	/* Check for any overlap with regions used by hardware directly */
1134 	if (!exclusive) {
1135 		ctrl_b = r->cache.shareable_bits;
1136 		if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len))
1137 			return true;
1138 	}
1139 
1140 	/* Check for overlap with other resource groups */
1141 	for (i = 0; i < closids_supported(); i++) {
1142 		ctrl_b = resctrl_arch_get_config(r, d, i, type);
1143 		mode = rdtgroup_mode_by_closid(i);
1144 		if (closid_allocated(i) && i != closid &&
1145 		    mode != RDT_MODE_PSEUDO_LOCKSETUP) {
1146 			if (bitmap_intersects(&cbm, &ctrl_b, r->cache.cbm_len)) {
1147 				if (exclusive) {
1148 					if (mode == RDT_MODE_EXCLUSIVE)
1149 						return true;
1150 					continue;
1151 				}
1152 				return true;
1153 			}
1154 		}
1155 	}
1156 
1157 	return false;
1158 }
1159 
1160 /**
1161  * rdtgroup_cbm_overlaps - Does CBM overlap with other use of hardware
1162  * @s: Schema for the resource to which domain instance @d belongs.
1163  * @d: The domain instance for which @closid is being tested.
1164  * @cbm: Capacity bitmask being tested.
1165  * @closid: Intended closid for @cbm.
1166  * @exclusive: Only check if overlaps with exclusive resource groups
1167  *
1168  * Resources that can be allocated using a CBM can use the CBM to control
1169  * the overlap of these allocations. rdtgroup_cmb_overlaps() is the test
1170  * for overlap. Overlap test is not limited to the specific resource for
1171  * which the CBM is intended though - when dealing with CDP resources that
1172  * share the underlying hardware the overlap check should be performed on
1173  * the CDP resource sharing the hardware also.
1174  *
1175  * Refer to description of __rdtgroup_cbm_overlaps() for the details of the
1176  * overlap test.
1177  *
1178  * Return: true if CBM overlap detected, false if there is no overlap
1179  */
rdtgroup_cbm_overlaps(struct resctrl_schema * s,struct rdt_domain * d,unsigned long cbm,int closid,bool exclusive)1180 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
1181 			   unsigned long cbm, int closid, bool exclusive)
1182 {
1183 	enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
1184 	struct rdt_resource *r = s->res;
1185 
1186 	if (__rdtgroup_cbm_overlaps(r, d, cbm, closid, s->conf_type,
1187 				    exclusive))
1188 		return true;
1189 
1190 	if (!resctrl_arch_get_cdp_enabled(r->rid))
1191 		return false;
1192 	return  __rdtgroup_cbm_overlaps(r, d, cbm, closid, peer_type, exclusive);
1193 }
1194 
1195 /**
1196  * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive
1197  *
1198  * An exclusive resource group implies that there should be no sharing of
1199  * its allocated resources. At the time this group is considered to be
1200  * exclusive this test can determine if its current schemata supports this
1201  * setting by testing for overlap with all other resource groups.
1202  *
1203  * Return: true if resource group can be exclusive, false if there is overlap
1204  * with allocations of other resource groups and thus this resource group
1205  * cannot be exclusive.
1206  */
rdtgroup_mode_test_exclusive(struct rdtgroup * rdtgrp)1207 static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
1208 {
1209 	int closid = rdtgrp->closid;
1210 	struct resctrl_schema *s;
1211 	struct rdt_resource *r;
1212 	bool has_cache = false;
1213 	struct rdt_domain *d;
1214 	u32 ctrl;
1215 
1216 	list_for_each_entry(s, &resctrl_schema_all, list) {
1217 		r = s->res;
1218 		if (r->rid == RDT_RESOURCE_MBA)
1219 			continue;
1220 		has_cache = true;
1221 		list_for_each_entry(d, &r->domains, list) {
1222 			ctrl = resctrl_arch_get_config(r, d, closid,
1223 						       s->conf_type);
1224 			if (rdtgroup_cbm_overlaps(s, d, ctrl, closid, false)) {
1225 				rdt_last_cmd_puts("Schemata overlaps\n");
1226 				return false;
1227 			}
1228 		}
1229 	}
1230 
1231 	if (!has_cache) {
1232 		rdt_last_cmd_puts("Cannot be exclusive without CAT/CDP\n");
1233 		return false;
1234 	}
1235 
1236 	return true;
1237 }
1238 
1239 /**
1240  * rdtgroup_mode_write - Modify the resource group's mode
1241  *
1242  */
rdtgroup_mode_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1243 static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
1244 				   char *buf, size_t nbytes, loff_t off)
1245 {
1246 	struct rdtgroup *rdtgrp;
1247 	enum rdtgrp_mode mode;
1248 	int ret = 0;
1249 
1250 	/* Valid input requires a trailing newline */
1251 	if (nbytes == 0 || buf[nbytes - 1] != '\n')
1252 		return -EINVAL;
1253 	buf[nbytes - 1] = '\0';
1254 
1255 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
1256 	if (!rdtgrp) {
1257 		rdtgroup_kn_unlock(of->kn);
1258 		return -ENOENT;
1259 	}
1260 
1261 	rdt_last_cmd_clear();
1262 
1263 	mode = rdtgrp->mode;
1264 
1265 	if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) ||
1266 	    (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) ||
1267 	    (!strcmp(buf, "pseudo-locksetup") &&
1268 	     mode == RDT_MODE_PSEUDO_LOCKSETUP) ||
1269 	    (!strcmp(buf, "pseudo-locked") && mode == RDT_MODE_PSEUDO_LOCKED))
1270 		goto out;
1271 
1272 	if (mode == RDT_MODE_PSEUDO_LOCKED) {
1273 		rdt_last_cmd_puts("Cannot change pseudo-locked group\n");
1274 		ret = -EINVAL;
1275 		goto out;
1276 	}
1277 
1278 	if (!strcmp(buf, "shareable")) {
1279 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1280 			ret = rdtgroup_locksetup_exit(rdtgrp);
1281 			if (ret)
1282 				goto out;
1283 		}
1284 		rdtgrp->mode = RDT_MODE_SHAREABLE;
1285 	} else if (!strcmp(buf, "exclusive")) {
1286 		if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
1287 			ret = -EINVAL;
1288 			goto out;
1289 		}
1290 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1291 			ret = rdtgroup_locksetup_exit(rdtgrp);
1292 			if (ret)
1293 				goto out;
1294 		}
1295 		rdtgrp->mode = RDT_MODE_EXCLUSIVE;
1296 	} else if (!strcmp(buf, "pseudo-locksetup")) {
1297 		ret = rdtgroup_locksetup_enter(rdtgrp);
1298 		if (ret)
1299 			goto out;
1300 		rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP;
1301 	} else {
1302 		rdt_last_cmd_puts("Unknown or unsupported mode\n");
1303 		ret = -EINVAL;
1304 	}
1305 
1306 out:
1307 	rdtgroup_kn_unlock(of->kn);
1308 	return ret ?: nbytes;
1309 }
1310 
1311 /**
1312  * rdtgroup_cbm_to_size - Translate CBM to size in bytes
1313  * @r: RDT resource to which @d belongs.
1314  * @d: RDT domain instance.
1315  * @cbm: bitmask for which the size should be computed.
1316  *
1317  * The bitmask provided associated with the RDT domain instance @d will be
1318  * translated into how many bytes it represents. The size in bytes is
1319  * computed by first dividing the total cache size by the CBM length to
1320  * determine how many bytes each bit in the bitmask represents. The result
1321  * is multiplied with the number of bits set in the bitmask.
1322  *
1323  * @cbm is unsigned long, even if only 32 bits are used to make the
1324  * bitmap functions work correctly.
1325  */
rdtgroup_cbm_to_size(struct rdt_resource * r,struct rdt_domain * d,unsigned long cbm)1326 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
1327 				  struct rdt_domain *d, unsigned long cbm)
1328 {
1329 	struct cpu_cacheinfo *ci;
1330 	unsigned int size = 0;
1331 	int num_b, i;
1332 
1333 	num_b = bitmap_weight(&cbm, r->cache.cbm_len);
1334 	ci = get_cpu_cacheinfo(cpumask_any(&d->cpu_mask));
1335 	for (i = 0; i < ci->num_leaves; i++) {
1336 		if (ci->info_list[i].level == r->cache_level) {
1337 			size = ci->info_list[i].size / r->cache.cbm_len * num_b;
1338 			break;
1339 		}
1340 	}
1341 
1342 	return size;
1343 }
1344 
1345 /**
1346  * rdtgroup_size_show - Display size in bytes of allocated regions
1347  *
1348  * The "size" file mirrors the layout of the "schemata" file, printing the
1349  * size in bytes of each region instead of the capacity bitmask.
1350  *
1351  */
rdtgroup_size_show(struct kernfs_open_file * of,struct seq_file * s,void * v)1352 static int rdtgroup_size_show(struct kernfs_open_file *of,
1353 			      struct seq_file *s, void *v)
1354 {
1355 	struct resctrl_schema *schema;
1356 	enum resctrl_conf_type type;
1357 	struct rdtgroup *rdtgrp;
1358 	struct rdt_resource *r;
1359 	struct rdt_domain *d;
1360 	unsigned int size;
1361 	int ret = 0;
1362 	u32 closid;
1363 	bool sep;
1364 	u32 ctrl;
1365 
1366 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
1367 	if (!rdtgrp) {
1368 		rdtgroup_kn_unlock(of->kn);
1369 		return -ENOENT;
1370 	}
1371 
1372 	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
1373 		if (!rdtgrp->plr->d) {
1374 			rdt_last_cmd_clear();
1375 			rdt_last_cmd_puts("Cache domain offline\n");
1376 			ret = -ENODEV;
1377 		} else {
1378 			seq_printf(s, "%*s:", max_name_width,
1379 				   rdtgrp->plr->s->name);
1380 			size = rdtgroup_cbm_to_size(rdtgrp->plr->s->res,
1381 						    rdtgrp->plr->d,
1382 						    rdtgrp->plr->cbm);
1383 			seq_printf(s, "%d=%u\n", rdtgrp->plr->d->id, size);
1384 		}
1385 		goto out;
1386 	}
1387 
1388 	closid = rdtgrp->closid;
1389 
1390 	list_for_each_entry(schema, &resctrl_schema_all, list) {
1391 		r = schema->res;
1392 		type = schema->conf_type;
1393 		sep = false;
1394 		seq_printf(s, "%*s:", max_name_width, schema->name);
1395 		list_for_each_entry(d, &r->domains, list) {
1396 			if (sep)
1397 				seq_putc(s, ';');
1398 			if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1399 				size = 0;
1400 			} else {
1401 				if (is_mba_sc(r))
1402 					ctrl = d->mbps_val[closid];
1403 				else
1404 					ctrl = resctrl_arch_get_config(r, d,
1405 								       closid,
1406 								       type);
1407 				if (r->rid == RDT_RESOURCE_MBA)
1408 					size = ctrl;
1409 				else
1410 					size = rdtgroup_cbm_to_size(r, d, ctrl);
1411 			}
1412 			seq_printf(s, "%d=%u", d->id, size);
1413 			sep = true;
1414 		}
1415 		seq_putc(s, '\n');
1416 	}
1417 
1418 out:
1419 	rdtgroup_kn_unlock(of->kn);
1420 
1421 	return ret;
1422 }
1423 
1424 /* rdtgroup information files for one cache resource. */
1425 static struct rftype res_common_files[] = {
1426 	{
1427 		.name		= "last_cmd_status",
1428 		.mode		= 0444,
1429 		.kf_ops		= &rdtgroup_kf_single_ops,
1430 		.seq_show	= rdt_last_cmd_status_show,
1431 		.fflags		= RF_TOP_INFO,
1432 	},
1433 	{
1434 		.name		= "num_closids",
1435 		.mode		= 0444,
1436 		.kf_ops		= &rdtgroup_kf_single_ops,
1437 		.seq_show	= rdt_num_closids_show,
1438 		.fflags		= RF_CTRL_INFO,
1439 	},
1440 	{
1441 		.name		= "mon_features",
1442 		.mode		= 0444,
1443 		.kf_ops		= &rdtgroup_kf_single_ops,
1444 		.seq_show	= rdt_mon_features_show,
1445 		.fflags		= RF_MON_INFO,
1446 	},
1447 	{
1448 		.name		= "num_rmids",
1449 		.mode		= 0444,
1450 		.kf_ops		= &rdtgroup_kf_single_ops,
1451 		.seq_show	= rdt_num_rmids_show,
1452 		.fflags		= RF_MON_INFO,
1453 	},
1454 	{
1455 		.name		= "cbm_mask",
1456 		.mode		= 0444,
1457 		.kf_ops		= &rdtgroup_kf_single_ops,
1458 		.seq_show	= rdt_default_ctrl_show,
1459 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_CACHE,
1460 	},
1461 	{
1462 		.name		= "min_cbm_bits",
1463 		.mode		= 0444,
1464 		.kf_ops		= &rdtgroup_kf_single_ops,
1465 		.seq_show	= rdt_min_cbm_bits_show,
1466 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_CACHE,
1467 	},
1468 	{
1469 		.name		= "shareable_bits",
1470 		.mode		= 0444,
1471 		.kf_ops		= &rdtgroup_kf_single_ops,
1472 		.seq_show	= rdt_shareable_bits_show,
1473 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_CACHE,
1474 	},
1475 	{
1476 		.name		= "bit_usage",
1477 		.mode		= 0444,
1478 		.kf_ops		= &rdtgroup_kf_single_ops,
1479 		.seq_show	= rdt_bit_usage_show,
1480 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_CACHE,
1481 	},
1482 	{
1483 		.name		= "min_bandwidth",
1484 		.mode		= 0444,
1485 		.kf_ops		= &rdtgroup_kf_single_ops,
1486 		.seq_show	= rdt_min_bw_show,
1487 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_MB,
1488 	},
1489 	{
1490 		.name		= "bandwidth_gran",
1491 		.mode		= 0444,
1492 		.kf_ops		= &rdtgroup_kf_single_ops,
1493 		.seq_show	= rdt_bw_gran_show,
1494 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_MB,
1495 	},
1496 	{
1497 		.name		= "delay_linear",
1498 		.mode		= 0444,
1499 		.kf_ops		= &rdtgroup_kf_single_ops,
1500 		.seq_show	= rdt_delay_linear_show,
1501 		.fflags		= RF_CTRL_INFO | RFTYPE_RES_MB,
1502 	},
1503 	/*
1504 	 * Platform specific which (if any) capabilities are provided by
1505 	 * thread_throttle_mode. Defer "fflags" initialization to platform
1506 	 * discovery.
1507 	 */
1508 	{
1509 		.name		= "thread_throttle_mode",
1510 		.mode		= 0444,
1511 		.kf_ops		= &rdtgroup_kf_single_ops,
1512 		.seq_show	= rdt_thread_throttle_mode_show,
1513 	},
1514 	{
1515 		.name		= "max_threshold_occupancy",
1516 		.mode		= 0644,
1517 		.kf_ops		= &rdtgroup_kf_single_ops,
1518 		.write		= max_threshold_occ_write,
1519 		.seq_show	= max_threshold_occ_show,
1520 		.fflags		= RF_MON_INFO | RFTYPE_RES_CACHE,
1521 	},
1522 	{
1523 		.name		= "cpus",
1524 		.mode		= 0644,
1525 		.kf_ops		= &rdtgroup_kf_single_ops,
1526 		.write		= rdtgroup_cpus_write,
1527 		.seq_show	= rdtgroup_cpus_show,
1528 		.fflags		= RFTYPE_BASE,
1529 	},
1530 	{
1531 		.name		= "cpus_list",
1532 		.mode		= 0644,
1533 		.kf_ops		= &rdtgroup_kf_single_ops,
1534 		.write		= rdtgroup_cpus_write,
1535 		.seq_show	= rdtgroup_cpus_show,
1536 		.flags		= RFTYPE_FLAGS_CPUS_LIST,
1537 		.fflags		= RFTYPE_BASE,
1538 	},
1539 	{
1540 		.name		= "tasks",
1541 		.mode		= 0644,
1542 		.kf_ops		= &rdtgroup_kf_single_ops,
1543 		.write		= rdtgroup_tasks_write,
1544 		.seq_show	= rdtgroup_tasks_show,
1545 		.fflags		= RFTYPE_BASE,
1546 	},
1547 	{
1548 		.name		= "schemata",
1549 		.mode		= 0644,
1550 		.kf_ops		= &rdtgroup_kf_single_ops,
1551 		.write		= rdtgroup_schemata_write,
1552 		.seq_show	= rdtgroup_schemata_show,
1553 		.fflags		= RF_CTRL_BASE,
1554 	},
1555 	{
1556 		.name		= "mode",
1557 		.mode		= 0644,
1558 		.kf_ops		= &rdtgroup_kf_single_ops,
1559 		.write		= rdtgroup_mode_write,
1560 		.seq_show	= rdtgroup_mode_show,
1561 		.fflags		= RF_CTRL_BASE,
1562 	},
1563 	{
1564 		.name		= "size",
1565 		.mode		= 0444,
1566 		.kf_ops		= &rdtgroup_kf_single_ops,
1567 		.seq_show	= rdtgroup_size_show,
1568 		.fflags		= RF_CTRL_BASE,
1569 	},
1570 
1571 };
1572 
rdtgroup_add_files(struct kernfs_node * kn,unsigned long fflags)1573 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
1574 {
1575 	struct rftype *rfts, *rft;
1576 	int ret, len;
1577 
1578 	rfts = res_common_files;
1579 	len = ARRAY_SIZE(res_common_files);
1580 
1581 	lockdep_assert_held(&rdtgroup_mutex);
1582 
1583 	for (rft = rfts; rft < rfts + len; rft++) {
1584 		if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
1585 			ret = rdtgroup_add_file(kn, rft);
1586 			if (ret)
1587 				goto error;
1588 		}
1589 	}
1590 
1591 	return 0;
1592 error:
1593 	pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
1594 	while (--rft >= rfts) {
1595 		if ((fflags & rft->fflags) == rft->fflags)
1596 			kernfs_remove_by_name(kn, rft->name);
1597 	}
1598 	return ret;
1599 }
1600 
rdtgroup_get_rftype_by_name(const char * name)1601 static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
1602 {
1603 	struct rftype *rfts, *rft;
1604 	int len;
1605 
1606 	rfts = res_common_files;
1607 	len = ARRAY_SIZE(res_common_files);
1608 
1609 	for (rft = rfts; rft < rfts + len; rft++) {
1610 		if (!strcmp(rft->name, name))
1611 			return rft;
1612 	}
1613 
1614 	return NULL;
1615 }
1616 
thread_throttle_mode_init(void)1617 void __init thread_throttle_mode_init(void)
1618 {
1619 	struct rftype *rft;
1620 
1621 	rft = rdtgroup_get_rftype_by_name("thread_throttle_mode");
1622 	if (!rft)
1623 		return;
1624 
1625 	rft->fflags = RF_CTRL_INFO | RFTYPE_RES_MB;
1626 }
1627 
1628 /**
1629  * rdtgroup_kn_mode_restrict - Restrict user access to named resctrl file
1630  * @r: The resource group with which the file is associated.
1631  * @name: Name of the file
1632  *
1633  * The permissions of named resctrl file, directory, or link are modified
1634  * to not allow read, write, or execute by any user.
1635  *
1636  * WARNING: This function is intended to communicate to the user that the
1637  * resctrl file has been locked down - that it is not relevant to the
1638  * particular state the system finds itself in. It should not be relied
1639  * on to protect from user access because after the file's permissions
1640  * are restricted the user can still change the permissions using chmod
1641  * from the command line.
1642  *
1643  * Return: 0 on success, <0 on failure.
1644  */
rdtgroup_kn_mode_restrict(struct rdtgroup * r,const char * name)1645 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name)
1646 {
1647 	struct iattr iattr = {.ia_valid = ATTR_MODE,};
1648 	struct kernfs_node *kn;
1649 	int ret = 0;
1650 
1651 	kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1652 	if (!kn)
1653 		return -ENOENT;
1654 
1655 	switch (kernfs_type(kn)) {
1656 	case KERNFS_DIR:
1657 		iattr.ia_mode = S_IFDIR;
1658 		break;
1659 	case KERNFS_FILE:
1660 		iattr.ia_mode = S_IFREG;
1661 		break;
1662 	case KERNFS_LINK:
1663 		iattr.ia_mode = S_IFLNK;
1664 		break;
1665 	}
1666 
1667 	ret = kernfs_setattr(kn, &iattr);
1668 	kernfs_put(kn);
1669 	return ret;
1670 }
1671 
1672 /**
1673  * rdtgroup_kn_mode_restore - Restore user access to named resctrl file
1674  * @r: The resource group with which the file is associated.
1675  * @name: Name of the file
1676  * @mask: Mask of permissions that should be restored
1677  *
1678  * Restore the permissions of the named file. If @name is a directory the
1679  * permissions of its parent will be used.
1680  *
1681  * Return: 0 on success, <0 on failure.
1682  */
rdtgroup_kn_mode_restore(struct rdtgroup * r,const char * name,umode_t mask)1683 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
1684 			     umode_t mask)
1685 {
1686 	struct iattr iattr = {.ia_valid = ATTR_MODE,};
1687 	struct kernfs_node *kn, *parent;
1688 	struct rftype *rfts, *rft;
1689 	int ret, len;
1690 
1691 	rfts = res_common_files;
1692 	len = ARRAY_SIZE(res_common_files);
1693 
1694 	for (rft = rfts; rft < rfts + len; rft++) {
1695 		if (!strcmp(rft->name, name))
1696 			iattr.ia_mode = rft->mode & mask;
1697 	}
1698 
1699 	kn = kernfs_find_and_get_ns(r->kn, name, NULL);
1700 	if (!kn)
1701 		return -ENOENT;
1702 
1703 	switch (kernfs_type(kn)) {
1704 	case KERNFS_DIR:
1705 		parent = kernfs_get_parent(kn);
1706 		if (parent) {
1707 			iattr.ia_mode |= parent->mode;
1708 			kernfs_put(parent);
1709 		}
1710 		iattr.ia_mode |= S_IFDIR;
1711 		break;
1712 	case KERNFS_FILE:
1713 		iattr.ia_mode |= S_IFREG;
1714 		break;
1715 	case KERNFS_LINK:
1716 		iattr.ia_mode |= S_IFLNK;
1717 		break;
1718 	}
1719 
1720 	ret = kernfs_setattr(kn, &iattr);
1721 	kernfs_put(kn);
1722 	return ret;
1723 }
1724 
rdtgroup_mkdir_info_resdir(void * priv,char * name,unsigned long fflags)1725 static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
1726 				      unsigned long fflags)
1727 {
1728 	struct kernfs_node *kn_subdir;
1729 	int ret;
1730 
1731 	kn_subdir = kernfs_create_dir(kn_info, name,
1732 				      kn_info->mode, priv);
1733 	if (IS_ERR(kn_subdir))
1734 		return PTR_ERR(kn_subdir);
1735 
1736 	ret = rdtgroup_kn_set_ugid(kn_subdir);
1737 	if (ret)
1738 		return ret;
1739 
1740 	ret = rdtgroup_add_files(kn_subdir, fflags);
1741 	if (!ret)
1742 		kernfs_activate(kn_subdir);
1743 
1744 	return ret;
1745 }
1746 
rdtgroup_create_info_dir(struct kernfs_node * parent_kn)1747 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
1748 {
1749 	struct resctrl_schema *s;
1750 	struct rdt_resource *r;
1751 	unsigned long fflags;
1752 	char name[32];
1753 	int ret;
1754 
1755 	/* create the directory */
1756 	kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
1757 	if (IS_ERR(kn_info))
1758 		return PTR_ERR(kn_info);
1759 
1760 	ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
1761 	if (ret)
1762 		goto out_destroy;
1763 
1764 	/* loop over enabled controls, these are all alloc_capable */
1765 	list_for_each_entry(s, &resctrl_schema_all, list) {
1766 		r = s->res;
1767 		fflags =  r->fflags | RF_CTRL_INFO;
1768 		ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
1769 		if (ret)
1770 			goto out_destroy;
1771 	}
1772 
1773 	for_each_mon_capable_rdt_resource(r) {
1774 		fflags =  r->fflags | RF_MON_INFO;
1775 		sprintf(name, "%s_MON", r->name);
1776 		ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
1777 		if (ret)
1778 			goto out_destroy;
1779 	}
1780 
1781 	ret = rdtgroup_kn_set_ugid(kn_info);
1782 	if (ret)
1783 		goto out_destroy;
1784 
1785 	kernfs_activate(kn_info);
1786 
1787 	return 0;
1788 
1789 out_destroy:
1790 	kernfs_remove(kn_info);
1791 	return ret;
1792 }
1793 
1794 static int
mongroup_create_dir(struct kernfs_node * parent_kn,struct rdtgroup * prgrp,char * name,struct kernfs_node ** dest_kn)1795 mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
1796 		    char *name, struct kernfs_node **dest_kn)
1797 {
1798 	struct kernfs_node *kn;
1799 	int ret;
1800 
1801 	/* create the directory */
1802 	kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1803 	if (IS_ERR(kn))
1804 		return PTR_ERR(kn);
1805 
1806 	if (dest_kn)
1807 		*dest_kn = kn;
1808 
1809 	ret = rdtgroup_kn_set_ugid(kn);
1810 	if (ret)
1811 		goto out_destroy;
1812 
1813 	kernfs_activate(kn);
1814 
1815 	return 0;
1816 
1817 out_destroy:
1818 	kernfs_remove(kn);
1819 	return ret;
1820 }
1821 
l3_qos_cfg_update(void * arg)1822 static void l3_qos_cfg_update(void *arg)
1823 {
1824 	bool *enable = arg;
1825 
1826 	wrmsrl(MSR_IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
1827 }
1828 
l2_qos_cfg_update(void * arg)1829 static void l2_qos_cfg_update(void *arg)
1830 {
1831 	bool *enable = arg;
1832 
1833 	wrmsrl(MSR_IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL);
1834 }
1835 
is_mba_linear(void)1836 static inline bool is_mba_linear(void)
1837 {
1838 	return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
1839 }
1840 
set_cache_qos_cfg(int level,bool enable)1841 static int set_cache_qos_cfg(int level, bool enable)
1842 {
1843 	void (*update)(void *arg);
1844 	struct rdt_resource *r_l;
1845 	cpumask_var_t cpu_mask;
1846 	struct rdt_domain *d;
1847 	int cpu;
1848 
1849 	if (level == RDT_RESOURCE_L3)
1850 		update = l3_qos_cfg_update;
1851 	else if (level == RDT_RESOURCE_L2)
1852 		update = l2_qos_cfg_update;
1853 	else
1854 		return -EINVAL;
1855 
1856 	if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1857 		return -ENOMEM;
1858 
1859 	r_l = &rdt_resources_all[level].r_resctrl;
1860 	list_for_each_entry(d, &r_l->domains, list) {
1861 		if (r_l->cache.arch_has_per_cpu_cfg)
1862 			/* Pick all the CPUs in the domain instance */
1863 			for_each_cpu(cpu, &d->cpu_mask)
1864 				cpumask_set_cpu(cpu, cpu_mask);
1865 		else
1866 			/* Pick one CPU from each domain instance to update MSR */
1867 			cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1868 	}
1869 	cpu = get_cpu();
1870 	/* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1871 	if (cpumask_test_cpu(cpu, cpu_mask))
1872 		update(&enable);
1873 	/* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1874 	smp_call_function_many(cpu_mask, update, &enable, 1);
1875 	put_cpu();
1876 
1877 	free_cpumask_var(cpu_mask);
1878 
1879 	return 0;
1880 }
1881 
1882 /* Restore the qos cfg state when a domain comes online */
rdt_domain_reconfigure_cdp(struct rdt_resource * r)1883 void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
1884 {
1885 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
1886 
1887 	if (!r->cdp_capable)
1888 		return;
1889 
1890 	if (r->rid == RDT_RESOURCE_L2)
1891 		l2_qos_cfg_update(&hw_res->cdp_enabled);
1892 
1893 	if (r->rid == RDT_RESOURCE_L3)
1894 		l3_qos_cfg_update(&hw_res->cdp_enabled);
1895 }
1896 
mba_sc_domain_allocate(struct rdt_resource * r,struct rdt_domain * d)1897 static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_domain *d)
1898 {
1899 	u32 num_closid = resctrl_arch_get_num_closid(r);
1900 	int cpu = cpumask_any(&d->cpu_mask);
1901 	int i;
1902 
1903 	d->mbps_val = kcalloc_node(num_closid, sizeof(*d->mbps_val),
1904 				   GFP_KERNEL, cpu_to_node(cpu));
1905 	if (!d->mbps_val)
1906 		return -ENOMEM;
1907 
1908 	for (i = 0; i < num_closid; i++)
1909 		d->mbps_val[i] = MBA_MAX_MBPS;
1910 
1911 	return 0;
1912 }
1913 
mba_sc_domain_destroy(struct rdt_resource * r,struct rdt_domain * d)1914 static void mba_sc_domain_destroy(struct rdt_resource *r,
1915 				  struct rdt_domain *d)
1916 {
1917 	kfree(d->mbps_val);
1918 	d->mbps_val = NULL;
1919 }
1920 
1921 /*
1922  * MBA software controller is supported only if
1923  * MBM is supported and MBA is in linear scale.
1924  */
supports_mba_mbps(void)1925 static bool supports_mba_mbps(void)
1926 {
1927 	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
1928 
1929 	return (is_mbm_local_enabled() &&
1930 		r->alloc_capable && is_mba_linear());
1931 }
1932 
1933 /*
1934  * Enable or disable the MBA software controller
1935  * which helps user specify bandwidth in MBps.
1936  */
set_mba_sc(bool mba_sc)1937 static int set_mba_sc(bool mba_sc)
1938 {
1939 	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
1940 	u32 num_closid = resctrl_arch_get_num_closid(r);
1941 	struct rdt_domain *d;
1942 	int i;
1943 
1944 	if (!supports_mba_mbps() || mba_sc == is_mba_sc(r))
1945 		return -EINVAL;
1946 
1947 	r->membw.mba_sc = mba_sc;
1948 
1949 	list_for_each_entry(d, &r->domains, list) {
1950 		for (i = 0; i < num_closid; i++)
1951 			d->mbps_val[i] = MBA_MAX_MBPS;
1952 	}
1953 
1954 	return 0;
1955 }
1956 
cdp_enable(int level)1957 static int cdp_enable(int level)
1958 {
1959 	struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
1960 	int ret;
1961 
1962 	if (!r_l->alloc_capable)
1963 		return -EINVAL;
1964 
1965 	ret = set_cache_qos_cfg(level, true);
1966 	if (!ret)
1967 		rdt_resources_all[level].cdp_enabled = true;
1968 
1969 	return ret;
1970 }
1971 
cdp_disable(int level)1972 static void cdp_disable(int level)
1973 {
1974 	struct rdt_hw_resource *r_hw = &rdt_resources_all[level];
1975 
1976 	if (r_hw->cdp_enabled) {
1977 		set_cache_qos_cfg(level, false);
1978 		r_hw->cdp_enabled = false;
1979 	}
1980 }
1981 
resctrl_arch_set_cdp_enabled(enum resctrl_res_level l,bool enable)1982 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
1983 {
1984 	struct rdt_hw_resource *hw_res = &rdt_resources_all[l];
1985 
1986 	if (!hw_res->r_resctrl.cdp_capable)
1987 		return -EINVAL;
1988 
1989 	if (enable)
1990 		return cdp_enable(l);
1991 
1992 	cdp_disable(l);
1993 
1994 	return 0;
1995 }
1996 
cdp_disable_all(void)1997 static void cdp_disable_all(void)
1998 {
1999 	if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
2000 		resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, false);
2001 	if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
2002 		resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, false);
2003 }
2004 
2005 /*
2006  * We don't allow rdtgroup directories to be created anywhere
2007  * except the root directory. Thus when looking for the rdtgroup
2008  * structure for a kernfs node we are either looking at a directory,
2009  * in which case the rdtgroup structure is pointed at by the "priv"
2010  * field, otherwise we have a file, and need only look to the parent
2011  * to find the rdtgroup.
2012  */
kernfs_to_rdtgroup(struct kernfs_node * kn)2013 static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
2014 {
2015 	if (kernfs_type(kn) == KERNFS_DIR) {
2016 		/*
2017 		 * All the resource directories use "kn->priv"
2018 		 * to point to the "struct rdtgroup" for the
2019 		 * resource. "info" and its subdirectories don't
2020 		 * have rdtgroup structures, so return NULL here.
2021 		 */
2022 		if (kn == kn_info || kn->parent == kn_info)
2023 			return NULL;
2024 		else
2025 			return kn->priv;
2026 	} else {
2027 		return kn->parent->priv;
2028 	}
2029 }
2030 
rdtgroup_kn_lock_live(struct kernfs_node * kn)2031 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
2032 {
2033 	struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2034 
2035 	if (!rdtgrp)
2036 		return NULL;
2037 
2038 	atomic_inc(&rdtgrp->waitcount);
2039 	kernfs_break_active_protection(kn);
2040 
2041 	mutex_lock(&rdtgroup_mutex);
2042 
2043 	/* Was this group deleted while we waited? */
2044 	if (rdtgrp->flags & RDT_DELETED)
2045 		return NULL;
2046 
2047 	return rdtgrp;
2048 }
2049 
rdtgroup_kn_unlock(struct kernfs_node * kn)2050 void rdtgroup_kn_unlock(struct kernfs_node *kn)
2051 {
2052 	struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
2053 
2054 	if (!rdtgrp)
2055 		return;
2056 
2057 	mutex_unlock(&rdtgroup_mutex);
2058 
2059 	if (atomic_dec_and_test(&rdtgrp->waitcount) &&
2060 	    (rdtgrp->flags & RDT_DELETED)) {
2061 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2062 		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2063 			rdtgroup_pseudo_lock_remove(rdtgrp);
2064 		kernfs_unbreak_active_protection(kn);
2065 		rdtgroup_remove(rdtgrp);
2066 	} else {
2067 		kernfs_unbreak_active_protection(kn);
2068 	}
2069 }
2070 
2071 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2072 			     struct rdtgroup *prgrp,
2073 			     struct kernfs_node **mon_data_kn);
2074 
rdt_enable_ctx(struct rdt_fs_context * ctx)2075 static int rdt_enable_ctx(struct rdt_fs_context *ctx)
2076 {
2077 	int ret = 0;
2078 
2079 	if (ctx->enable_cdpl2)
2080 		ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L2, true);
2081 
2082 	if (!ret && ctx->enable_cdpl3)
2083 		ret = resctrl_arch_set_cdp_enabled(RDT_RESOURCE_L3, true);
2084 
2085 	if (!ret && ctx->enable_mba_mbps)
2086 		ret = set_mba_sc(true);
2087 
2088 	return ret;
2089 }
2090 
schemata_list_add(struct rdt_resource * r,enum resctrl_conf_type type)2091 static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type)
2092 {
2093 	struct resctrl_schema *s;
2094 	const char *suffix = "";
2095 	int ret, cl;
2096 
2097 	s = kzalloc(sizeof(*s), GFP_KERNEL);
2098 	if (!s)
2099 		return -ENOMEM;
2100 
2101 	s->res = r;
2102 	s->num_closid = resctrl_arch_get_num_closid(r);
2103 	if (resctrl_arch_get_cdp_enabled(r->rid))
2104 		s->num_closid /= 2;
2105 
2106 	s->conf_type = type;
2107 	switch (type) {
2108 	case CDP_CODE:
2109 		suffix = "CODE";
2110 		break;
2111 	case CDP_DATA:
2112 		suffix = "DATA";
2113 		break;
2114 	case CDP_NONE:
2115 		suffix = "";
2116 		break;
2117 	}
2118 
2119 	ret = snprintf(s->name, sizeof(s->name), "%s%s", r->name, suffix);
2120 	if (ret >= sizeof(s->name)) {
2121 		kfree(s);
2122 		return -EINVAL;
2123 	}
2124 
2125 	cl = strlen(s->name);
2126 
2127 	/*
2128 	 * If CDP is supported by this resource, but not enabled,
2129 	 * include the suffix. This ensures the tabular format of the
2130 	 * schemata file does not change between mounts of the filesystem.
2131 	 */
2132 	if (r->cdp_capable && !resctrl_arch_get_cdp_enabled(r->rid))
2133 		cl += 4;
2134 
2135 	if (cl > max_name_width)
2136 		max_name_width = cl;
2137 
2138 	INIT_LIST_HEAD(&s->list);
2139 	list_add(&s->list, &resctrl_schema_all);
2140 
2141 	return 0;
2142 }
2143 
schemata_list_create(void)2144 static int schemata_list_create(void)
2145 {
2146 	struct rdt_resource *r;
2147 	int ret = 0;
2148 
2149 	for_each_alloc_capable_rdt_resource(r) {
2150 		if (resctrl_arch_get_cdp_enabled(r->rid)) {
2151 			ret = schemata_list_add(r, CDP_CODE);
2152 			if (ret)
2153 				break;
2154 
2155 			ret = schemata_list_add(r, CDP_DATA);
2156 		} else {
2157 			ret = schemata_list_add(r, CDP_NONE);
2158 		}
2159 
2160 		if (ret)
2161 			break;
2162 	}
2163 
2164 	return ret;
2165 }
2166 
schemata_list_destroy(void)2167 static void schemata_list_destroy(void)
2168 {
2169 	struct resctrl_schema *s, *tmp;
2170 
2171 	list_for_each_entry_safe(s, tmp, &resctrl_schema_all, list) {
2172 		list_del(&s->list);
2173 		kfree(s);
2174 	}
2175 }
2176 
rdt_get_tree(struct fs_context * fc)2177 static int rdt_get_tree(struct fs_context *fc)
2178 {
2179 	struct rdt_fs_context *ctx = rdt_fc2context(fc);
2180 	struct rdt_domain *dom;
2181 	struct rdt_resource *r;
2182 	int ret;
2183 
2184 	cpus_read_lock();
2185 	mutex_lock(&rdtgroup_mutex);
2186 	/*
2187 	 * resctrl file system can only be mounted once.
2188 	 */
2189 	if (static_branch_unlikely(&rdt_enable_key)) {
2190 		ret = -EBUSY;
2191 		goto out;
2192 	}
2193 
2194 	ret = rdt_enable_ctx(ctx);
2195 	if (ret < 0)
2196 		goto out_cdp;
2197 
2198 	ret = schemata_list_create();
2199 	if (ret) {
2200 		schemata_list_destroy();
2201 		goto out_mba;
2202 	}
2203 
2204 	closid_init();
2205 
2206 	ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
2207 	if (ret < 0)
2208 		goto out_schemata_free;
2209 
2210 	if (rdt_mon_capable) {
2211 		ret = mongroup_create_dir(rdtgroup_default.kn,
2212 					  &rdtgroup_default, "mon_groups",
2213 					  &kn_mongrp);
2214 		if (ret < 0)
2215 			goto out_info;
2216 
2217 		ret = mkdir_mondata_all(rdtgroup_default.kn,
2218 					&rdtgroup_default, &kn_mondata);
2219 		if (ret < 0)
2220 			goto out_mongrp;
2221 		rdtgroup_default.mon.mon_data_kn = kn_mondata;
2222 	}
2223 
2224 	ret = rdt_pseudo_lock_init();
2225 	if (ret)
2226 		goto out_mondata;
2227 
2228 	ret = kernfs_get_tree(fc);
2229 	if (ret < 0)
2230 		goto out_psl;
2231 
2232 	if (rdt_alloc_capable)
2233 		static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
2234 	if (rdt_mon_capable)
2235 		static_branch_enable_cpuslocked(&rdt_mon_enable_key);
2236 
2237 	if (rdt_alloc_capable || rdt_mon_capable)
2238 		static_branch_enable_cpuslocked(&rdt_enable_key);
2239 
2240 	if (is_mbm_enabled()) {
2241 		r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
2242 		list_for_each_entry(dom, &r->domains, list)
2243 			mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
2244 	}
2245 
2246 	goto out;
2247 
2248 out_psl:
2249 	rdt_pseudo_lock_release();
2250 out_mondata:
2251 	if (rdt_mon_capable)
2252 		kernfs_remove(kn_mondata);
2253 out_mongrp:
2254 	if (rdt_mon_capable)
2255 		kernfs_remove(kn_mongrp);
2256 out_info:
2257 	kernfs_remove(kn_info);
2258 out_schemata_free:
2259 	schemata_list_destroy();
2260 out_mba:
2261 	if (ctx->enable_mba_mbps)
2262 		set_mba_sc(false);
2263 out_cdp:
2264 	cdp_disable_all();
2265 out:
2266 	rdt_last_cmd_clear();
2267 	mutex_unlock(&rdtgroup_mutex);
2268 	cpus_read_unlock();
2269 	return ret;
2270 }
2271 
2272 enum rdt_param {
2273 	Opt_cdp,
2274 	Opt_cdpl2,
2275 	Opt_mba_mbps,
2276 	nr__rdt_params
2277 };
2278 
2279 static const struct fs_parameter_spec rdt_fs_parameters[] = {
2280 	fsparam_flag("cdp",		Opt_cdp),
2281 	fsparam_flag("cdpl2",		Opt_cdpl2),
2282 	fsparam_flag("mba_MBps",	Opt_mba_mbps),
2283 	{}
2284 };
2285 
rdt_parse_param(struct fs_context * fc,struct fs_parameter * param)2286 static int rdt_parse_param(struct fs_context *fc, struct fs_parameter *param)
2287 {
2288 	struct rdt_fs_context *ctx = rdt_fc2context(fc);
2289 	struct fs_parse_result result;
2290 	int opt;
2291 
2292 	opt = fs_parse(fc, rdt_fs_parameters, param, &result);
2293 	if (opt < 0)
2294 		return opt;
2295 
2296 	switch (opt) {
2297 	case Opt_cdp:
2298 		ctx->enable_cdpl3 = true;
2299 		return 0;
2300 	case Opt_cdpl2:
2301 		ctx->enable_cdpl2 = true;
2302 		return 0;
2303 	case Opt_mba_mbps:
2304 		if (!supports_mba_mbps())
2305 			return -EINVAL;
2306 		ctx->enable_mba_mbps = true;
2307 		return 0;
2308 	}
2309 
2310 	return -EINVAL;
2311 }
2312 
rdt_fs_context_free(struct fs_context * fc)2313 static void rdt_fs_context_free(struct fs_context *fc)
2314 {
2315 	struct rdt_fs_context *ctx = rdt_fc2context(fc);
2316 
2317 	kernfs_free_fs_context(fc);
2318 	kfree(ctx);
2319 }
2320 
2321 static const struct fs_context_operations rdt_fs_context_ops = {
2322 	.free		= rdt_fs_context_free,
2323 	.parse_param	= rdt_parse_param,
2324 	.get_tree	= rdt_get_tree,
2325 };
2326 
rdt_init_fs_context(struct fs_context * fc)2327 static int rdt_init_fs_context(struct fs_context *fc)
2328 {
2329 	struct rdt_fs_context *ctx;
2330 
2331 	ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
2332 	if (!ctx)
2333 		return -ENOMEM;
2334 
2335 	ctx->kfc.root = rdt_root;
2336 	ctx->kfc.magic = RDTGROUP_SUPER_MAGIC;
2337 	fc->fs_private = &ctx->kfc;
2338 	fc->ops = &rdt_fs_context_ops;
2339 	put_user_ns(fc->user_ns);
2340 	fc->user_ns = get_user_ns(&init_user_ns);
2341 	fc->global = true;
2342 	return 0;
2343 }
2344 
reset_all_ctrls(struct rdt_resource * r)2345 static int reset_all_ctrls(struct rdt_resource *r)
2346 {
2347 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
2348 	struct rdt_hw_domain *hw_dom;
2349 	struct msr_param msr_param;
2350 	cpumask_var_t cpu_mask;
2351 	struct rdt_domain *d;
2352 	int i, cpu;
2353 
2354 	if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
2355 		return -ENOMEM;
2356 
2357 	msr_param.res = r;
2358 	msr_param.low = 0;
2359 	msr_param.high = hw_res->num_closid;
2360 
2361 	/*
2362 	 * Disable resource control for this resource by setting all
2363 	 * CBMs in all domains to the maximum mask value. Pick one CPU
2364 	 * from each domain to update the MSRs below.
2365 	 */
2366 	list_for_each_entry(d, &r->domains, list) {
2367 		hw_dom = resctrl_to_arch_dom(d);
2368 		cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
2369 
2370 		for (i = 0; i < hw_res->num_closid; i++)
2371 			hw_dom->ctrl_val[i] = r->default_ctrl;
2372 	}
2373 	cpu = get_cpu();
2374 	/* Update CBM on this cpu if it's in cpu_mask. */
2375 	if (cpumask_test_cpu(cpu, cpu_mask))
2376 		rdt_ctrl_update(&msr_param);
2377 	/* Update CBM on all other cpus in cpu_mask. */
2378 	smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
2379 	put_cpu();
2380 
2381 	free_cpumask_var(cpu_mask);
2382 
2383 	return 0;
2384 }
2385 
2386 /*
2387  * Move tasks from one to the other group. If @from is NULL, then all tasks
2388  * in the systems are moved unconditionally (used for teardown).
2389  *
2390  * If @mask is not NULL the cpus on which moved tasks are running are set
2391  * in that mask so the update smp function call is restricted to affected
2392  * cpus.
2393  */
rdt_move_group_tasks(struct rdtgroup * from,struct rdtgroup * to,struct cpumask * mask)2394 static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
2395 				 struct cpumask *mask)
2396 {
2397 	struct task_struct *p, *t;
2398 
2399 	read_lock(&tasklist_lock);
2400 	for_each_process_thread(p, t) {
2401 		if (!from || is_closid_match(t, from) ||
2402 		    is_rmid_match(t, from)) {
2403 			WRITE_ONCE(t->closid, to->closid);
2404 			WRITE_ONCE(t->rmid, to->mon.rmid);
2405 
2406 			/*
2407 			 * Order the closid/rmid stores above before the loads
2408 			 * in task_curr(). This pairs with the full barrier
2409 			 * between the rq->curr update and resctrl_sched_in()
2410 			 * during context switch.
2411 			 */
2412 			smp_mb();
2413 
2414 			/*
2415 			 * If the task is on a CPU, set the CPU in the mask.
2416 			 * The detection is inaccurate as tasks might move or
2417 			 * schedule before the smp function call takes place.
2418 			 * In such a case the function call is pointless, but
2419 			 * there is no other side effect.
2420 			 */
2421 			if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
2422 				cpumask_set_cpu(task_cpu(t), mask);
2423 		}
2424 	}
2425 	read_unlock(&tasklist_lock);
2426 }
2427 
free_all_child_rdtgrp(struct rdtgroup * rdtgrp)2428 static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
2429 {
2430 	struct rdtgroup *sentry, *stmp;
2431 	struct list_head *head;
2432 
2433 	head = &rdtgrp->mon.crdtgrp_list;
2434 	list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
2435 		free_rmid(sentry->mon.rmid);
2436 		list_del(&sentry->mon.crdtgrp_list);
2437 
2438 		if (atomic_read(&sentry->waitcount) != 0)
2439 			sentry->flags = RDT_DELETED;
2440 		else
2441 			rdtgroup_remove(sentry);
2442 	}
2443 }
2444 
2445 /*
2446  * Forcibly remove all of subdirectories under root.
2447  */
rmdir_all_sub(void)2448 static void rmdir_all_sub(void)
2449 {
2450 	struct rdtgroup *rdtgrp, *tmp;
2451 
2452 	/* Move all tasks to the default resource group */
2453 	rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
2454 
2455 	list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
2456 		/* Free any child rmids */
2457 		free_all_child_rdtgrp(rdtgrp);
2458 
2459 		/* Remove each rdtgroup other than root */
2460 		if (rdtgrp == &rdtgroup_default)
2461 			continue;
2462 
2463 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2464 		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
2465 			rdtgroup_pseudo_lock_remove(rdtgrp);
2466 
2467 		/*
2468 		 * Give any CPUs back to the default group. We cannot copy
2469 		 * cpu_online_mask because a CPU might have executed the
2470 		 * offline callback already, but is still marked online.
2471 		 */
2472 		cpumask_or(&rdtgroup_default.cpu_mask,
2473 			   &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
2474 
2475 		free_rmid(rdtgrp->mon.rmid);
2476 
2477 		kernfs_remove(rdtgrp->kn);
2478 		list_del(&rdtgrp->rdtgroup_list);
2479 
2480 		if (atomic_read(&rdtgrp->waitcount) != 0)
2481 			rdtgrp->flags = RDT_DELETED;
2482 		else
2483 			rdtgroup_remove(rdtgrp);
2484 	}
2485 	/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
2486 	update_closid_rmid(cpu_online_mask, &rdtgroup_default);
2487 
2488 	kernfs_remove(kn_info);
2489 	kernfs_remove(kn_mongrp);
2490 	kernfs_remove(kn_mondata);
2491 }
2492 
rdt_kill_sb(struct super_block * sb)2493 static void rdt_kill_sb(struct super_block *sb)
2494 {
2495 	struct rdt_resource *r;
2496 
2497 	cpus_read_lock();
2498 	mutex_lock(&rdtgroup_mutex);
2499 
2500 	set_mba_sc(false);
2501 
2502 	/*Put everything back to default values. */
2503 	for_each_alloc_capable_rdt_resource(r)
2504 		reset_all_ctrls(r);
2505 	cdp_disable_all();
2506 	rmdir_all_sub();
2507 	rdt_pseudo_lock_release();
2508 	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
2509 	schemata_list_destroy();
2510 	static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
2511 	static_branch_disable_cpuslocked(&rdt_mon_enable_key);
2512 	static_branch_disable_cpuslocked(&rdt_enable_key);
2513 	kernfs_kill_sb(sb);
2514 	mutex_unlock(&rdtgroup_mutex);
2515 	cpus_read_unlock();
2516 }
2517 
2518 static struct file_system_type rdt_fs_type = {
2519 	.name			= "resctrl",
2520 	.init_fs_context	= rdt_init_fs_context,
2521 	.parameters		= rdt_fs_parameters,
2522 	.kill_sb		= rdt_kill_sb,
2523 };
2524 
mon_addfile(struct kernfs_node * parent_kn,const char * name,void * priv)2525 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
2526 		       void *priv)
2527 {
2528 	struct kernfs_node *kn;
2529 	int ret = 0;
2530 
2531 	kn = __kernfs_create_file(parent_kn, name, 0444,
2532 				  GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
2533 				  &kf_mondata_ops, priv, NULL, NULL);
2534 	if (IS_ERR(kn))
2535 		return PTR_ERR(kn);
2536 
2537 	ret = rdtgroup_kn_set_ugid(kn);
2538 	if (ret) {
2539 		kernfs_remove(kn);
2540 		return ret;
2541 	}
2542 
2543 	return ret;
2544 }
2545 
2546 /*
2547  * Remove all subdirectories of mon_data of ctrl_mon groups
2548  * and monitor groups with given domain id.
2549  */
rmdir_mondata_subdir_allrdtgrp(struct rdt_resource * r,unsigned int dom_id)2550 static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2551 					   unsigned int dom_id)
2552 {
2553 	struct rdtgroup *prgrp, *crgrp;
2554 	char name[32];
2555 
2556 	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2557 		sprintf(name, "mon_%s_%02d", r->name, dom_id);
2558 		kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
2559 
2560 		list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
2561 			kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
2562 	}
2563 }
2564 
mkdir_mondata_subdir(struct kernfs_node * parent_kn,struct rdt_domain * d,struct rdt_resource * r,struct rdtgroup * prgrp)2565 static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
2566 				struct rdt_domain *d,
2567 				struct rdt_resource *r, struct rdtgroup *prgrp)
2568 {
2569 	union mon_data_bits priv;
2570 	struct kernfs_node *kn;
2571 	struct mon_evt *mevt;
2572 	struct rmid_read rr;
2573 	char name[32];
2574 	int ret;
2575 
2576 	sprintf(name, "mon_%s_%02d", r->name, d->id);
2577 	/* create the directory */
2578 	kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
2579 	if (IS_ERR(kn))
2580 		return PTR_ERR(kn);
2581 
2582 	ret = rdtgroup_kn_set_ugid(kn);
2583 	if (ret)
2584 		goto out_destroy;
2585 
2586 	if (WARN_ON(list_empty(&r->evt_list))) {
2587 		ret = -EPERM;
2588 		goto out_destroy;
2589 	}
2590 
2591 	priv.u.rid = r->rid;
2592 	priv.u.domid = d->id;
2593 	list_for_each_entry(mevt, &r->evt_list, list) {
2594 		priv.u.evtid = mevt->evtid;
2595 		ret = mon_addfile(kn, mevt->name, priv.priv);
2596 		if (ret)
2597 			goto out_destroy;
2598 
2599 		if (is_mbm_event(mevt->evtid))
2600 			mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
2601 	}
2602 	kernfs_activate(kn);
2603 	return 0;
2604 
2605 out_destroy:
2606 	kernfs_remove(kn);
2607 	return ret;
2608 }
2609 
2610 /*
2611  * Add all subdirectories of mon_data for "ctrl_mon" groups
2612  * and "monitor" groups with given domain id.
2613  */
mkdir_mondata_subdir_allrdtgrp(struct rdt_resource * r,struct rdt_domain * d)2614 static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
2615 					   struct rdt_domain *d)
2616 {
2617 	struct kernfs_node *parent_kn;
2618 	struct rdtgroup *prgrp, *crgrp;
2619 	struct list_head *head;
2620 
2621 	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
2622 		parent_kn = prgrp->mon.mon_data_kn;
2623 		mkdir_mondata_subdir(parent_kn, d, r, prgrp);
2624 
2625 		head = &prgrp->mon.crdtgrp_list;
2626 		list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
2627 			parent_kn = crgrp->mon.mon_data_kn;
2628 			mkdir_mondata_subdir(parent_kn, d, r, crgrp);
2629 		}
2630 	}
2631 }
2632 
mkdir_mondata_subdir_alldom(struct kernfs_node * parent_kn,struct rdt_resource * r,struct rdtgroup * prgrp)2633 static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
2634 				       struct rdt_resource *r,
2635 				       struct rdtgroup *prgrp)
2636 {
2637 	struct rdt_domain *dom;
2638 	int ret;
2639 
2640 	list_for_each_entry(dom, &r->domains, list) {
2641 		ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
2642 		if (ret)
2643 			return ret;
2644 	}
2645 
2646 	return 0;
2647 }
2648 
2649 /*
2650  * This creates a directory mon_data which contains the monitored data.
2651  *
2652  * mon_data has one directory for each domain which are named
2653  * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
2654  * with L3 domain looks as below:
2655  * ./mon_data:
2656  * mon_L3_00
2657  * mon_L3_01
2658  * mon_L3_02
2659  * ...
2660  *
2661  * Each domain directory has one file per event:
2662  * ./mon_L3_00/:
2663  * llc_occupancy
2664  *
2665  */
mkdir_mondata_all(struct kernfs_node * parent_kn,struct rdtgroup * prgrp,struct kernfs_node ** dest_kn)2666 static int mkdir_mondata_all(struct kernfs_node *parent_kn,
2667 			     struct rdtgroup *prgrp,
2668 			     struct kernfs_node **dest_kn)
2669 {
2670 	struct rdt_resource *r;
2671 	struct kernfs_node *kn;
2672 	int ret;
2673 
2674 	/*
2675 	 * Create the mon_data directory first.
2676 	 */
2677 	ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
2678 	if (ret)
2679 		return ret;
2680 
2681 	if (dest_kn)
2682 		*dest_kn = kn;
2683 
2684 	/*
2685 	 * Create the subdirectories for each domain. Note that all events
2686 	 * in a domain like L3 are grouped into a resource whose domain is L3
2687 	 */
2688 	for_each_mon_capable_rdt_resource(r) {
2689 		ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
2690 		if (ret)
2691 			goto out_destroy;
2692 	}
2693 
2694 	return 0;
2695 
2696 out_destroy:
2697 	kernfs_remove(kn);
2698 	return ret;
2699 }
2700 
2701 /**
2702  * cbm_ensure_valid - Enforce validity on provided CBM
2703  * @_val:	Candidate CBM
2704  * @r:		RDT resource to which the CBM belongs
2705  *
2706  * The provided CBM represents all cache portions available for use. This
2707  * may be represented by a bitmap that does not consist of contiguous ones
2708  * and thus be an invalid CBM.
2709  * Here the provided CBM is forced to be a valid CBM by only considering
2710  * the first set of contiguous bits as valid and clearing all bits.
2711  * The intention here is to provide a valid default CBM with which a new
2712  * resource group is initialized. The user can follow this with a
2713  * modification to the CBM if the default does not satisfy the
2714  * requirements.
2715  */
cbm_ensure_valid(u32 _val,struct rdt_resource * r)2716 static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
2717 {
2718 	unsigned int cbm_len = r->cache.cbm_len;
2719 	unsigned long first_bit, zero_bit;
2720 	unsigned long val = _val;
2721 
2722 	if (!val)
2723 		return 0;
2724 
2725 	first_bit = find_first_bit(&val, cbm_len);
2726 	zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
2727 
2728 	/* Clear any remaining bits to ensure contiguous region */
2729 	bitmap_clear(&val, zero_bit, cbm_len - zero_bit);
2730 	return (u32)val;
2731 }
2732 
2733 /*
2734  * Initialize cache resources per RDT domain
2735  *
2736  * Set the RDT domain up to start off with all usable allocations. That is,
2737  * all shareable and unused bits. All-zero CBM is invalid.
2738  */
__init_one_rdt_domain(struct rdt_domain * d,struct resctrl_schema * s,u32 closid)2739 static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
2740 				 u32 closid)
2741 {
2742 	enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
2743 	enum resctrl_conf_type t = s->conf_type;
2744 	struct resctrl_staged_config *cfg;
2745 	struct rdt_resource *r = s->res;
2746 	u32 used_b = 0, unused_b = 0;
2747 	unsigned long tmp_cbm;
2748 	enum rdtgrp_mode mode;
2749 	u32 peer_ctl, ctrl_val;
2750 	int i;
2751 
2752 	cfg = &d->staged_config[t];
2753 	cfg->have_new_ctrl = false;
2754 	cfg->new_ctrl = r->cache.shareable_bits;
2755 	used_b = r->cache.shareable_bits;
2756 	for (i = 0; i < closids_supported(); i++) {
2757 		if (closid_allocated(i) && i != closid) {
2758 			mode = rdtgroup_mode_by_closid(i);
2759 			if (mode == RDT_MODE_PSEUDO_LOCKSETUP)
2760 				/*
2761 				 * ctrl values for locksetup aren't relevant
2762 				 * until the schemata is written, and the mode
2763 				 * becomes RDT_MODE_PSEUDO_LOCKED.
2764 				 */
2765 				continue;
2766 			/*
2767 			 * If CDP is active include peer domain's
2768 			 * usage to ensure there is no overlap
2769 			 * with an exclusive group.
2770 			 */
2771 			if (resctrl_arch_get_cdp_enabled(r->rid))
2772 				peer_ctl = resctrl_arch_get_config(r, d, i,
2773 								   peer_type);
2774 			else
2775 				peer_ctl = 0;
2776 			ctrl_val = resctrl_arch_get_config(r, d, i,
2777 							   s->conf_type);
2778 			used_b |= ctrl_val | peer_ctl;
2779 			if (mode == RDT_MODE_SHAREABLE)
2780 				cfg->new_ctrl |= ctrl_val | peer_ctl;
2781 		}
2782 	}
2783 	if (d->plr && d->plr->cbm > 0)
2784 		used_b |= d->plr->cbm;
2785 	unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1);
2786 	unused_b &= BIT_MASK(r->cache.cbm_len) - 1;
2787 	cfg->new_ctrl |= unused_b;
2788 	/*
2789 	 * Force the initial CBM to be valid, user can
2790 	 * modify the CBM based on system availability.
2791 	 */
2792 	cfg->new_ctrl = cbm_ensure_valid(cfg->new_ctrl, r);
2793 	/*
2794 	 * Assign the u32 CBM to an unsigned long to ensure that
2795 	 * bitmap_weight() does not access out-of-bound memory.
2796 	 */
2797 	tmp_cbm = cfg->new_ctrl;
2798 	if (bitmap_weight(&tmp_cbm, r->cache.cbm_len) < r->cache.min_cbm_bits) {
2799 		rdt_last_cmd_printf("No space on %s:%d\n", s->name, d->id);
2800 		return -ENOSPC;
2801 	}
2802 	cfg->have_new_ctrl = true;
2803 
2804 	return 0;
2805 }
2806 
2807 /*
2808  * Initialize cache resources with default values.
2809  *
2810  * A new RDT group is being created on an allocation capable (CAT)
2811  * supporting system. Set this group up to start off with all usable
2812  * allocations.
2813  *
2814  * If there are no more shareable bits available on any domain then
2815  * the entire allocation will fail.
2816  */
rdtgroup_init_cat(struct resctrl_schema * s,u32 closid)2817 static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
2818 {
2819 	struct rdt_domain *d;
2820 	int ret;
2821 
2822 	list_for_each_entry(d, &s->res->domains, list) {
2823 		ret = __init_one_rdt_domain(d, s, closid);
2824 		if (ret < 0)
2825 			return ret;
2826 	}
2827 
2828 	return 0;
2829 }
2830 
2831 /* Initialize MBA resource with default values. */
rdtgroup_init_mba(struct rdt_resource * r,u32 closid)2832 static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
2833 {
2834 	struct resctrl_staged_config *cfg;
2835 	struct rdt_domain *d;
2836 
2837 	list_for_each_entry(d, &r->domains, list) {
2838 		if (is_mba_sc(r)) {
2839 			d->mbps_val[closid] = MBA_MAX_MBPS;
2840 			continue;
2841 		}
2842 
2843 		cfg = &d->staged_config[CDP_NONE];
2844 		cfg->new_ctrl = r->default_ctrl;
2845 		cfg->have_new_ctrl = true;
2846 	}
2847 }
2848 
2849 /* Initialize the RDT group's allocations. */
rdtgroup_init_alloc(struct rdtgroup * rdtgrp)2850 static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
2851 {
2852 	struct resctrl_schema *s;
2853 	struct rdt_resource *r;
2854 	int ret;
2855 
2856 	list_for_each_entry(s, &resctrl_schema_all, list) {
2857 		r = s->res;
2858 		if (r->rid == RDT_RESOURCE_MBA) {
2859 			rdtgroup_init_mba(r, rdtgrp->closid);
2860 			if (is_mba_sc(r))
2861 				continue;
2862 		} else {
2863 			ret = rdtgroup_init_cat(s, rdtgrp->closid);
2864 			if (ret < 0)
2865 				return ret;
2866 		}
2867 
2868 		ret = resctrl_arch_update_domains(r, rdtgrp->closid);
2869 		if (ret < 0) {
2870 			rdt_last_cmd_puts("Failed to initialize allocations\n");
2871 			return ret;
2872 		}
2873 
2874 	}
2875 
2876 	rdtgrp->mode = RDT_MODE_SHAREABLE;
2877 
2878 	return 0;
2879 }
2880 
mkdir_rdt_prepare(struct kernfs_node * parent_kn,const char * name,umode_t mode,enum rdt_group_type rtype,struct rdtgroup ** r)2881 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
2882 			     const char *name, umode_t mode,
2883 			     enum rdt_group_type rtype, struct rdtgroup **r)
2884 {
2885 	struct rdtgroup *prdtgrp, *rdtgrp;
2886 	struct kernfs_node *kn;
2887 	uint files = 0;
2888 	int ret;
2889 
2890 	prdtgrp = rdtgroup_kn_lock_live(parent_kn);
2891 	if (!prdtgrp) {
2892 		ret = -ENODEV;
2893 		goto out_unlock;
2894 	}
2895 
2896 	if (rtype == RDTMON_GROUP &&
2897 	    (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
2898 	     prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
2899 		ret = -EINVAL;
2900 		rdt_last_cmd_puts("Pseudo-locking in progress\n");
2901 		goto out_unlock;
2902 	}
2903 
2904 	/* allocate the rdtgroup. */
2905 	rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
2906 	if (!rdtgrp) {
2907 		ret = -ENOSPC;
2908 		rdt_last_cmd_puts("Kernel out of memory\n");
2909 		goto out_unlock;
2910 	}
2911 	*r = rdtgrp;
2912 	rdtgrp->mon.parent = prdtgrp;
2913 	rdtgrp->type = rtype;
2914 	INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
2915 
2916 	/* kernfs creates the directory for rdtgrp */
2917 	kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
2918 	if (IS_ERR(kn)) {
2919 		ret = PTR_ERR(kn);
2920 		rdt_last_cmd_puts("kernfs create error\n");
2921 		goto out_free_rgrp;
2922 	}
2923 	rdtgrp->kn = kn;
2924 
2925 	/*
2926 	 * kernfs_remove() will drop the reference count on "kn" which
2927 	 * will free it. But we still need it to stick around for the
2928 	 * rdtgroup_kn_unlock(kn) call. Take one extra reference here,
2929 	 * which will be dropped by kernfs_put() in rdtgroup_remove().
2930 	 */
2931 	kernfs_get(kn);
2932 
2933 	ret = rdtgroup_kn_set_ugid(kn);
2934 	if (ret) {
2935 		rdt_last_cmd_puts("kernfs perm error\n");
2936 		goto out_destroy;
2937 	}
2938 
2939 	files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
2940 	ret = rdtgroup_add_files(kn, files);
2941 	if (ret) {
2942 		rdt_last_cmd_puts("kernfs fill error\n");
2943 		goto out_destroy;
2944 	}
2945 
2946 	if (rdt_mon_capable) {
2947 		ret = alloc_rmid();
2948 		if (ret < 0) {
2949 			rdt_last_cmd_puts("Out of RMIDs\n");
2950 			goto out_destroy;
2951 		}
2952 		rdtgrp->mon.rmid = ret;
2953 
2954 		ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
2955 		if (ret) {
2956 			rdt_last_cmd_puts("kernfs subdir error\n");
2957 			goto out_idfree;
2958 		}
2959 	}
2960 	kernfs_activate(kn);
2961 
2962 	/*
2963 	 * The caller unlocks the parent_kn upon success.
2964 	 */
2965 	return 0;
2966 
2967 out_idfree:
2968 	free_rmid(rdtgrp->mon.rmid);
2969 out_destroy:
2970 	kernfs_put(rdtgrp->kn);
2971 	kernfs_remove(rdtgrp->kn);
2972 out_free_rgrp:
2973 	kfree(rdtgrp);
2974 out_unlock:
2975 	rdtgroup_kn_unlock(parent_kn);
2976 	return ret;
2977 }
2978 
mkdir_rdt_prepare_clean(struct rdtgroup * rgrp)2979 static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
2980 {
2981 	kernfs_remove(rgrp->kn);
2982 	free_rmid(rgrp->mon.rmid);
2983 	rdtgroup_remove(rgrp);
2984 }
2985 
2986 /*
2987  * Create a monitor group under "mon_groups" directory of a control
2988  * and monitor group(ctrl_mon). This is a resource group
2989  * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
2990  */
rdtgroup_mkdir_mon(struct kernfs_node * parent_kn,const char * name,umode_t mode)2991 static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
2992 			      const char *name, umode_t mode)
2993 {
2994 	struct rdtgroup *rdtgrp, *prgrp;
2995 	int ret;
2996 
2997 	ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTMON_GROUP, &rdtgrp);
2998 	if (ret)
2999 		return ret;
3000 
3001 	prgrp = rdtgrp->mon.parent;
3002 	rdtgrp->closid = prgrp->closid;
3003 
3004 	/*
3005 	 * Add the rdtgrp to the list of rdtgrps the parent
3006 	 * ctrl_mon group has to track.
3007 	 */
3008 	list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
3009 
3010 	rdtgroup_kn_unlock(parent_kn);
3011 	return ret;
3012 }
3013 
3014 /*
3015  * These are rdtgroups created under the root directory. Can be used
3016  * to allocate and monitor resources.
3017  */
rdtgroup_mkdir_ctrl_mon(struct kernfs_node * parent_kn,const char * name,umode_t mode)3018 static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
3019 				   const char *name, umode_t mode)
3020 {
3021 	struct rdtgroup *rdtgrp;
3022 	struct kernfs_node *kn;
3023 	u32 closid;
3024 	int ret;
3025 
3026 	ret = mkdir_rdt_prepare(parent_kn, name, mode, RDTCTRL_GROUP, &rdtgrp);
3027 	if (ret)
3028 		return ret;
3029 
3030 	kn = rdtgrp->kn;
3031 	ret = closid_alloc();
3032 	if (ret < 0) {
3033 		rdt_last_cmd_puts("Out of CLOSIDs\n");
3034 		goto out_common_fail;
3035 	}
3036 	closid = ret;
3037 	ret = 0;
3038 
3039 	rdtgrp->closid = closid;
3040 	ret = rdtgroup_init_alloc(rdtgrp);
3041 	if (ret < 0)
3042 		goto out_id_free;
3043 
3044 	list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
3045 
3046 	if (rdt_mon_capable) {
3047 		/*
3048 		 * Create an empty mon_groups directory to hold the subset
3049 		 * of tasks and cpus to monitor.
3050 		 */
3051 		ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
3052 		if (ret) {
3053 			rdt_last_cmd_puts("kernfs subdir error\n");
3054 			goto out_del_list;
3055 		}
3056 	}
3057 
3058 	goto out_unlock;
3059 
3060 out_del_list:
3061 	list_del(&rdtgrp->rdtgroup_list);
3062 out_id_free:
3063 	closid_free(closid);
3064 out_common_fail:
3065 	mkdir_rdt_prepare_clean(rdtgrp);
3066 out_unlock:
3067 	rdtgroup_kn_unlock(parent_kn);
3068 	return ret;
3069 }
3070 
3071 /*
3072  * We allow creating mon groups only with in a directory called "mon_groups"
3073  * which is present in every ctrl_mon group. Check if this is a valid
3074  * "mon_groups" directory.
3075  *
3076  * 1. The directory should be named "mon_groups".
3077  * 2. The mon group itself should "not" be named "mon_groups".
3078  *   This makes sure "mon_groups" directory always has a ctrl_mon group
3079  *   as parent.
3080  */
is_mon_groups(struct kernfs_node * kn,const char * name)3081 static bool is_mon_groups(struct kernfs_node *kn, const char *name)
3082 {
3083 	return (!strcmp(kn->name, "mon_groups") &&
3084 		strcmp(name, "mon_groups"));
3085 }
3086 
rdtgroup_mkdir(struct kernfs_node * parent_kn,const char * name,umode_t mode)3087 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
3088 			  umode_t mode)
3089 {
3090 	/* Do not accept '\n' to avoid unparsable situation. */
3091 	if (strchr(name, '\n'))
3092 		return -EINVAL;
3093 
3094 	/*
3095 	 * If the parent directory is the root directory and RDT
3096 	 * allocation is supported, add a control and monitoring
3097 	 * subdirectory
3098 	 */
3099 	if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
3100 		return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
3101 
3102 	/*
3103 	 * If RDT monitoring is supported and the parent directory is a valid
3104 	 * "mon_groups" directory, add a monitoring subdirectory.
3105 	 */
3106 	if (rdt_mon_capable && is_mon_groups(parent_kn, name))
3107 		return rdtgroup_mkdir_mon(parent_kn, name, mode);
3108 
3109 	return -EPERM;
3110 }
3111 
rdtgroup_rmdir_mon(struct rdtgroup * rdtgrp,cpumask_var_t tmpmask)3112 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3113 {
3114 	struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
3115 	int cpu;
3116 
3117 	/* Give any tasks back to the parent group */
3118 	rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
3119 
3120 	/* Update per cpu rmid of the moved CPUs first */
3121 	for_each_cpu(cpu, &rdtgrp->cpu_mask)
3122 		per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
3123 	/*
3124 	 * Update the MSR on moved CPUs and CPUs which have moved
3125 	 * task running on them.
3126 	 */
3127 	cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3128 	update_closid_rmid(tmpmask, NULL);
3129 
3130 	rdtgrp->flags = RDT_DELETED;
3131 	free_rmid(rdtgrp->mon.rmid);
3132 
3133 	/*
3134 	 * Remove the rdtgrp from the parent ctrl_mon group's list
3135 	 */
3136 	WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
3137 	list_del(&rdtgrp->mon.crdtgrp_list);
3138 
3139 	kernfs_remove(rdtgrp->kn);
3140 
3141 	return 0;
3142 }
3143 
rdtgroup_ctrl_remove(struct rdtgroup * rdtgrp)3144 static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
3145 {
3146 	rdtgrp->flags = RDT_DELETED;
3147 	list_del(&rdtgrp->rdtgroup_list);
3148 
3149 	kernfs_remove(rdtgrp->kn);
3150 	return 0;
3151 }
3152 
rdtgroup_rmdir_ctrl(struct rdtgroup * rdtgrp,cpumask_var_t tmpmask)3153 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
3154 {
3155 	int cpu;
3156 
3157 	/* Give any tasks back to the default group */
3158 	rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
3159 
3160 	/* Give any CPUs back to the default group */
3161 	cpumask_or(&rdtgroup_default.cpu_mask,
3162 		   &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
3163 
3164 	/* Update per cpu closid and rmid of the moved CPUs first */
3165 	for_each_cpu(cpu, &rdtgrp->cpu_mask) {
3166 		per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
3167 		per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
3168 	}
3169 
3170 	/*
3171 	 * Update the MSR on moved CPUs and CPUs which have moved
3172 	 * task running on them.
3173 	 */
3174 	cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
3175 	update_closid_rmid(tmpmask, NULL);
3176 
3177 	closid_free(rdtgrp->closid);
3178 	free_rmid(rdtgrp->mon.rmid);
3179 
3180 	rdtgroup_ctrl_remove(rdtgrp);
3181 
3182 	/*
3183 	 * Free all the child monitor group rmids.
3184 	 */
3185 	free_all_child_rdtgrp(rdtgrp);
3186 
3187 	return 0;
3188 }
3189 
rdtgroup_rmdir(struct kernfs_node * kn)3190 static int rdtgroup_rmdir(struct kernfs_node *kn)
3191 {
3192 	struct kernfs_node *parent_kn = kn->parent;
3193 	struct rdtgroup *rdtgrp;
3194 	cpumask_var_t tmpmask;
3195 	int ret = 0;
3196 
3197 	if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
3198 		return -ENOMEM;
3199 
3200 	rdtgrp = rdtgroup_kn_lock_live(kn);
3201 	if (!rdtgrp) {
3202 		ret = -EPERM;
3203 		goto out;
3204 	}
3205 
3206 	/*
3207 	 * If the rdtgroup is a ctrl_mon group and parent directory
3208 	 * is the root directory, remove the ctrl_mon group.
3209 	 *
3210 	 * If the rdtgroup is a mon group and parent directory
3211 	 * is a valid "mon_groups" directory, remove the mon group.
3212 	 */
3213 	if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn &&
3214 	    rdtgrp != &rdtgroup_default) {
3215 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
3216 		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
3217 			ret = rdtgroup_ctrl_remove(rdtgrp);
3218 		} else {
3219 			ret = rdtgroup_rmdir_ctrl(rdtgrp, tmpmask);
3220 		}
3221 	} else if (rdtgrp->type == RDTMON_GROUP &&
3222 		 is_mon_groups(parent_kn, kn->name)) {
3223 		ret = rdtgroup_rmdir_mon(rdtgrp, tmpmask);
3224 	} else {
3225 		ret = -EPERM;
3226 	}
3227 
3228 out:
3229 	rdtgroup_kn_unlock(kn);
3230 	free_cpumask_var(tmpmask);
3231 	return ret;
3232 }
3233 
rdtgroup_show_options(struct seq_file * seq,struct kernfs_root * kf)3234 static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
3235 {
3236 	if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3))
3237 		seq_puts(seq, ",cdp");
3238 
3239 	if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
3240 		seq_puts(seq, ",cdpl2");
3241 
3242 	if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
3243 		seq_puts(seq, ",mba_MBps");
3244 
3245 	return 0;
3246 }
3247 
3248 static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
3249 	.mkdir		= rdtgroup_mkdir,
3250 	.rmdir		= rdtgroup_rmdir,
3251 	.show_options	= rdtgroup_show_options,
3252 };
3253 
rdtgroup_setup_root(void)3254 static int __init rdtgroup_setup_root(void)
3255 {
3256 	int ret;
3257 
3258 	rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
3259 				      KERNFS_ROOT_CREATE_DEACTIVATED |
3260 				      KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
3261 				      &rdtgroup_default);
3262 	if (IS_ERR(rdt_root))
3263 		return PTR_ERR(rdt_root);
3264 
3265 	mutex_lock(&rdtgroup_mutex);
3266 
3267 	rdtgroup_default.closid = 0;
3268 	rdtgroup_default.mon.rmid = 0;
3269 	rdtgroup_default.type = RDTCTRL_GROUP;
3270 	INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
3271 
3272 	list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
3273 
3274 	ret = rdtgroup_add_files(kernfs_root_to_node(rdt_root), RF_CTRL_BASE);
3275 	if (ret) {
3276 		kernfs_destroy_root(rdt_root);
3277 		goto out;
3278 	}
3279 
3280 	rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
3281 	kernfs_activate(rdtgroup_default.kn);
3282 
3283 out:
3284 	mutex_unlock(&rdtgroup_mutex);
3285 
3286 	return ret;
3287 }
3288 
domain_destroy_mon_state(struct rdt_domain * d)3289 static void domain_destroy_mon_state(struct rdt_domain *d)
3290 {
3291 	bitmap_free(d->rmid_busy_llc);
3292 	kfree(d->mbm_total);
3293 	kfree(d->mbm_local);
3294 }
3295 
resctrl_offline_domain(struct rdt_resource * r,struct rdt_domain * d)3296 void resctrl_offline_domain(struct rdt_resource *r, struct rdt_domain *d)
3297 {
3298 	lockdep_assert_held(&rdtgroup_mutex);
3299 
3300 	if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3301 		mba_sc_domain_destroy(r, d);
3302 
3303 	if (!r->mon_capable)
3304 		return;
3305 
3306 	/*
3307 	 * If resctrl is mounted, remove all the
3308 	 * per domain monitor data directories.
3309 	 */
3310 	if (static_branch_unlikely(&rdt_mon_enable_key))
3311 		rmdir_mondata_subdir_allrdtgrp(r, d->id);
3312 
3313 	if (is_mbm_enabled())
3314 		cancel_delayed_work(&d->mbm_over);
3315 	if (is_llc_occupancy_enabled() && has_busy_rmid(r, d)) {
3316 		/*
3317 		 * When a package is going down, forcefully
3318 		 * decrement rmid->ebusy. There is no way to know
3319 		 * that the L3 was flushed and hence may lead to
3320 		 * incorrect counts in rare scenarios, but leaving
3321 		 * the RMID as busy creates RMID leaks if the
3322 		 * package never comes back.
3323 		 */
3324 		__check_limbo(d, true);
3325 		cancel_delayed_work(&d->cqm_limbo);
3326 	}
3327 
3328 	domain_destroy_mon_state(d);
3329 }
3330 
domain_setup_mon_state(struct rdt_resource * r,struct rdt_domain * d)3331 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
3332 {
3333 	size_t tsize;
3334 
3335 	if (is_llc_occupancy_enabled()) {
3336 		d->rmid_busy_llc = bitmap_zalloc(r->num_rmid, GFP_KERNEL);
3337 		if (!d->rmid_busy_llc)
3338 			return -ENOMEM;
3339 	}
3340 	if (is_mbm_total_enabled()) {
3341 		tsize = sizeof(*d->mbm_total);
3342 		d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
3343 		if (!d->mbm_total) {
3344 			bitmap_free(d->rmid_busy_llc);
3345 			return -ENOMEM;
3346 		}
3347 	}
3348 	if (is_mbm_local_enabled()) {
3349 		tsize = sizeof(*d->mbm_local);
3350 		d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
3351 		if (!d->mbm_local) {
3352 			bitmap_free(d->rmid_busy_llc);
3353 			kfree(d->mbm_total);
3354 			return -ENOMEM;
3355 		}
3356 	}
3357 
3358 	return 0;
3359 }
3360 
resctrl_online_domain(struct rdt_resource * r,struct rdt_domain * d)3361 int resctrl_online_domain(struct rdt_resource *r, struct rdt_domain *d)
3362 {
3363 	int err;
3364 
3365 	lockdep_assert_held(&rdtgroup_mutex);
3366 
3367 	if (supports_mba_mbps() && r->rid == RDT_RESOURCE_MBA)
3368 		/* RDT_RESOURCE_MBA is never mon_capable */
3369 		return mba_sc_domain_allocate(r, d);
3370 
3371 	if (!r->mon_capable)
3372 		return 0;
3373 
3374 	err = domain_setup_mon_state(r, d);
3375 	if (err)
3376 		return err;
3377 
3378 	if (is_mbm_enabled()) {
3379 		INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
3380 		mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
3381 	}
3382 
3383 	if (is_llc_occupancy_enabled())
3384 		INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
3385 
3386 	/* If resctrl is mounted, add per domain monitor data directories. */
3387 	if (static_branch_unlikely(&rdt_mon_enable_key))
3388 		mkdir_mondata_subdir_allrdtgrp(r, d);
3389 
3390 	return 0;
3391 }
3392 
3393 /*
3394  * rdtgroup_init - rdtgroup initialization
3395  *
3396  * Setup resctrl file system including set up root, create mount point,
3397  * register rdtgroup filesystem, and initialize files under root directory.
3398  *
3399  * Return: 0 on success or -errno
3400  */
rdtgroup_init(void)3401 int __init rdtgroup_init(void)
3402 {
3403 	int ret = 0;
3404 
3405 	seq_buf_init(&last_cmd_status, last_cmd_status_buf,
3406 		     sizeof(last_cmd_status_buf));
3407 
3408 	ret = rdtgroup_setup_root();
3409 	if (ret)
3410 		return ret;
3411 
3412 	ret = sysfs_create_mount_point(fs_kobj, "resctrl");
3413 	if (ret)
3414 		goto cleanup_root;
3415 
3416 	ret = register_filesystem(&rdt_fs_type);
3417 	if (ret)
3418 		goto cleanup_mountpoint;
3419 
3420 	/*
3421 	 * Adding the resctrl debugfs directory here may not be ideal since
3422 	 * it would let the resctrl debugfs directory appear on the debugfs
3423 	 * filesystem before the resctrl filesystem is mounted.
3424 	 * It may also be ok since that would enable debugging of RDT before
3425 	 * resctrl is mounted.
3426 	 * The reason why the debugfs directory is created here and not in
3427 	 * rdt_get_tree() is because rdt_get_tree() takes rdtgroup_mutex and
3428 	 * during the debugfs directory creation also &sb->s_type->i_mutex_key
3429 	 * (the lockdep class of inode->i_rwsem). Other filesystem
3430 	 * interactions (eg. SyS_getdents) have the lock ordering:
3431 	 * &sb->s_type->i_mutex_key --> &mm->mmap_lock
3432 	 * During mmap(), called with &mm->mmap_lock, the rdtgroup_mutex
3433 	 * is taken, thus creating dependency:
3434 	 * &mm->mmap_lock --> rdtgroup_mutex for the latter that can cause
3435 	 * issues considering the other two lock dependencies.
3436 	 * By creating the debugfs directory here we avoid a dependency
3437 	 * that may cause deadlock (even though file operations cannot
3438 	 * occur until the filesystem is mounted, but I do not know how to
3439 	 * tell lockdep that).
3440 	 */
3441 	debugfs_resctrl = debugfs_create_dir("resctrl", NULL);
3442 
3443 	return 0;
3444 
3445 cleanup_mountpoint:
3446 	sysfs_remove_mount_point(fs_kobj, "resctrl");
3447 cleanup_root:
3448 	kernfs_destroy_root(rdt_root);
3449 
3450 	return ret;
3451 }
3452 
rdtgroup_exit(void)3453 void __exit rdtgroup_exit(void)
3454 {
3455 	debugfs_remove_recursive(debugfs_resctrl);
3456 	unregister_filesystem(&rdt_fs_type);
3457 	sysfs_remove_mount_point(fs_kobj, "resctrl");
3458 	kernfs_destroy_root(rdt_root);
3459 }
3460