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