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