]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/cpu/intel_rdt_rdtgroup.c
x86/resctrl: Fix a deadlock due to inaccurate reference
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / cpu / intel_rdt_rdtgroup.c
CommitLineData
5ff193fb
FY
1/*
2 * User interface for Resource Alloction in Resource Director Technology(RDT)
3 *
4 * Copyright (C) 2016 Intel Corporation
5 *
6 * Author: Fenghua Yu <fenghua.yu@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * More information about RDT be found in the Intel (R) x86 Architecture
18 * Software Developer Manual.
19 */
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
12e0110c 23#include <linux/cpu.h>
5ff193fb
FY
24#include <linux/fs.h>
25#include <linux/sysfs.h>
26#include <linux/kernfs.h>
9b3a7fd0 27#include <linux/seq_buf.h>
4e978d06 28#include <linux/seq_file.h>
3f07c014 29#include <linux/sched/signal.h>
29930025 30#include <linux/sched/task.h>
5ff193fb 31#include <linux/slab.h>
e02737d5 32#include <linux/task_work.h>
5ff193fb
FY
33
34#include <uapi/linux/magic.h>
35
05830204
VS
36#include <asm/intel_rdt_sched.h>
37#include "intel_rdt.h"
5ff193fb 38
4af4a88e
VS
39DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
40DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key);
1b5c0b75 41DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
cb2200e9 42static struct kernfs_root *rdt_root;
5ff193fb
FY
43struct rdtgroup rdtgroup_default;
44LIST_HEAD(rdt_all_groups);
45
4e978d06
FY
46/* Kernel fs node for "info" directory under root */
47static struct kernfs_node *kn_info;
48
4af4a88e
VS
49/* Kernel fs node for "mon_groups" directory under root */
50static struct kernfs_node *kn_mongrp;
51
52/* Kernel fs node for "mon_data" directory under root */
53static struct kernfs_node *kn_mondata;
54
9b3a7fd0
TL
55static struct seq_buf last_cmd_status;
56static char last_cmd_status_buf[512];
57
58void rdt_last_cmd_clear(void)
59{
60 lockdep_assert_held(&rdtgroup_mutex);
61 seq_buf_clear(&last_cmd_status);
62}
63
64void rdt_last_cmd_puts(const char *s)
65{
66 lockdep_assert_held(&rdtgroup_mutex);
67 seq_buf_puts(&last_cmd_status, s);
68}
69
70void rdt_last_cmd_printf(const char *fmt, ...)
71{
72 va_list ap;
73
74 va_start(ap, fmt);
75 lockdep_assert_held(&rdtgroup_mutex);
76 seq_buf_vprintf(&last_cmd_status, fmt, ap);
77 va_end(ap);
78}
79
60cf5e10
FY
80/*
81 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
82 * we can keep a bitmap of free CLOSIDs in a single integer.
83 *
84 * Using a global CLOSID across all resources has some advantages and
85 * some drawbacks:
86 * + We can simply set "current->closid" to assign a task to a resource
87 * group.
88 * + Context switch code can avoid extra memory references deciding which
89 * CLOSID to load into the PQR_ASSOC MSR
90 * - We give up some options in configuring resource groups across multi-socket
91 * systems.
92 * - Our choices on how to configure each resource become progressively more
93 * limited as the number of resources grows.
94 */
95static int closid_free_map;
96
97static void closid_init(void)
98{
99 struct rdt_resource *r;
100 int rdt_min_closid = 32;
101
102 /* Compute rdt_min_closid across all resources */
1b5c0b75 103 for_each_alloc_enabled_rdt_resource(r)
60cf5e10
FY
104 rdt_min_closid = min(rdt_min_closid, r->num_closid);
105
106 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
107
108 /* CLOSID 0 is always reserved for the default group */
109 closid_free_map &= ~1;
110}
111
cb2200e9 112static int closid_alloc(void)
60cf5e10 113{
0734ded1 114 u32 closid = ffs(closid_free_map);
60cf5e10
FY
115
116 if (closid == 0)
117 return -ENOSPC;
118 closid--;
119 closid_free_map &= ~(1 << closid);
120
121 return closid;
122}
123
124static void closid_free(int closid)
125{
126 closid_free_map |= 1 << closid;
127}
128
4e978d06
FY
129/* set uid and gid of rdtgroup dirs and files to that of the creator */
130static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
131{
132 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
133 .ia_uid = current_fsuid(),
134 .ia_gid = current_fsgid(), };
135
136 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
137 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
138 return 0;
139
140 return kernfs_setattr(kn, &iattr);
141}
142
143static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
144{
145 struct kernfs_node *kn;
146 int ret;
147
148 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
630d0eb6 149 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
4e978d06
FY
150 0, rft->kf_ops, rft, NULL, NULL);
151 if (IS_ERR(kn))
152 return PTR_ERR(kn);
153
154 ret = rdtgroup_kn_set_ugid(kn);
155 if (ret) {
156 kernfs_remove(kn);
157 return ret;
158 }
159
160 return 0;
161}
162
4e978d06
FY
163static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
164{
165 struct kernfs_open_file *of = m->private;
166 struct rftype *rft = of->kn->priv;
167
168 if (rft->seq_show)
169 return rft->seq_show(of, m, arg);
170 return 0;
171}
172
173static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
174 size_t nbytes, loff_t off)
175{
176 struct rftype *rft = of->kn->priv;
177
178 if (rft->write)
179 return rft->write(of, buf, nbytes, off);
180
181 return -EINVAL;
182}
183
184static struct kernfs_ops rdtgroup_kf_single_ops = {
185 .atomic_write_len = PAGE_SIZE,
186 .write = rdtgroup_file_write,
187 .seq_show = rdtgroup_seqfile_show,
188};
189
d89b7379
VS
190static struct kernfs_ops kf_mondata_ops = {
191 .atomic_write_len = PAGE_SIZE,
192 .seq_show = rdtgroup_mondata_show,
193};
194
4ffa3c97
JO
195static bool is_cpu_list(struct kernfs_open_file *of)
196{
197 struct rftype *rft = of->kn->priv;
198
199 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
200}
201
12e0110c
TL
202static int rdtgroup_cpus_show(struct kernfs_open_file *of,
203 struct seq_file *s, void *v)
204{
205 struct rdtgroup *rdtgrp;
206 int ret = 0;
207
208 rdtgrp = rdtgroup_kn_lock_live(of->kn);
209
4ffa3c97
JO
210 if (rdtgrp) {
211 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
212 cpumask_pr_args(&rdtgrp->cpu_mask));
213 } else {
12e0110c 214 ret = -ENOENT;
4ffa3c97 215 }
12e0110c
TL
216 rdtgroup_kn_unlock(of->kn);
217
218 return ret;
219}
220
f4107702
FY
221/*
222 * This is safe against intel_rdt_sched_in() called from __switch_to()
223 * because __switch_to() is executed with interrupts disabled. A local call
a9fcf862 224 * from update_closid_rmid() is proteced against __switch_to() because
f4107702
FY
225 * preemption is disabled.
226 */
a9fcf862 227static void update_cpu_closid_rmid(void *info)
f4107702 228{
b09d981b
VS
229 struct rdtgroup *r = info;
230
a9fcf862 231 if (r) {
a9110b55
VS
232 this_cpu_write(pqr_state.default_closid, r->closid);
233 this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
a9fcf862 234 }
b09d981b 235
f4107702
FY
236 /*
237 * We cannot unconditionally write the MSR because the current
238 * executing task might have its own closid selected. Just reuse
239 * the context switch code.
240 */
241 intel_rdt_sched_in();
242}
243
0efc89be
FY
244/*
245 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
246 *
b09d981b 247 * Per task closids/rmids must have been set up before calling this function.
0efc89be
FY
248 */
249static void
a9fcf862 250update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
f4107702
FY
251{
252 int cpu = get_cpu();
253
254 if (cpumask_test_cpu(cpu, cpu_mask))
a9fcf862
VS
255 update_cpu_closid_rmid(r);
256 smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1);
f4107702
FY
257 put_cpu();
258}
259
a9fcf862
VS
260static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
261 cpumask_var_t tmpmask)
262{
263 struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp;
264 struct list_head *head;
265
266 /* Check whether cpus belong to parent ctrl group */
267 cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask);
94457b36
TL
268 if (cpumask_weight(tmpmask)) {
269 rdt_last_cmd_puts("can only add CPUs to mongroup that belong to parent\n");
a9fcf862 270 return -EINVAL;
94457b36 271 }
a9fcf862
VS
272
273 /* Check whether cpus are dropped from this group */
274 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
275 if (cpumask_weight(tmpmask)) {
276 /* Give any dropped cpus to parent rdtgroup */
277 cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask);
278 update_closid_rmid(tmpmask, prgrp);
279 }
280
281 /*
282 * If we added cpus, remove them from previous group that owned them
283 * and update per-cpu rmid
284 */
285 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
286 if (cpumask_weight(tmpmask)) {
287 head = &prgrp->mon.crdtgrp_list;
288 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
289 if (crgrp == rdtgrp)
290 continue;
291 cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask,
292 tmpmask);
293 }
294 update_closid_rmid(tmpmask, rdtgrp);
295 }
296
297 /* Done pushing/pulling - update this group with new mask */
298 cpumask_copy(&rdtgrp->cpu_mask, newmask);
299
300 return 0;
301}
302
303static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m)
304{
305 struct rdtgroup *crgrp;
306
307 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m);
308 /* update the child mon group masks as well*/
309 list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list)
310 cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask);
311}
312
b09d981b 313static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
a9fcf862 314 cpumask_var_t tmpmask, cpumask_var_t tmpmask1)
b09d981b 315{
a9fcf862
VS
316 struct rdtgroup *r, *crgrp;
317 struct list_head *head;
b09d981b
VS
318
319 /* Check whether cpus are dropped from this group */
320 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
321 if (cpumask_weight(tmpmask)) {
322 /* Can't drop from default group */
94457b36
TL
323 if (rdtgrp == &rdtgroup_default) {
324 rdt_last_cmd_puts("Can't drop CPUs from default group\n");
b09d981b 325 return -EINVAL;
94457b36 326 }
b09d981b
VS
327
328 /* Give any dropped cpus to rdtgroup_default */
329 cpumask_or(&rdtgroup_default.cpu_mask,
330 &rdtgroup_default.cpu_mask, tmpmask);
a9fcf862 331 update_closid_rmid(tmpmask, &rdtgroup_default);
b09d981b
VS
332 }
333
334 /*
a9fcf862
VS
335 * If we added cpus, remove them from previous group and
336 * the prev group's child groups that owned them
337 * and update per-cpu closid/rmid.
b09d981b
VS
338 */
339 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
340 if (cpumask_weight(tmpmask)) {
341 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
342 if (r == rdtgrp)
343 continue;
a9fcf862
VS
344 cpumask_and(tmpmask1, &r->cpu_mask, tmpmask);
345 if (cpumask_weight(tmpmask1))
346 cpumask_rdtgrp_clear(r, tmpmask1);
b09d981b 347 }
a9fcf862 348 update_closid_rmid(tmpmask, rdtgrp);
b09d981b
VS
349 }
350
351 /* Done pushing/pulling - update this group with new mask */
352 cpumask_copy(&rdtgrp->cpu_mask, newmask);
353
a9fcf862
VS
354 /*
355 * Clear child mon group masks since there is a new parent mask
356 * now and update the rmid for the cpus the child lost.
357 */
358 head = &rdtgrp->mon.crdtgrp_list;
359 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
360 cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask);
361 update_closid_rmid(tmpmask, rdtgrp);
362 cpumask_clear(&crgrp->cpu_mask);
363 }
364
b09d981b
VS
365 return 0;
366}
367
12e0110c
TL
368static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
369 char *buf, size_t nbytes, loff_t off)
370{
a9fcf862 371 cpumask_var_t tmpmask, newmask, tmpmask1;
b09d981b 372 struct rdtgroup *rdtgrp;
f4107702 373 int ret;
12e0110c
TL
374
375 if (!buf)
376 return -EINVAL;
377
378 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
379 return -ENOMEM;
380 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
381 free_cpumask_var(tmpmask);
382 return -ENOMEM;
383 }
a9fcf862
VS
384 if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) {
385 free_cpumask_var(tmpmask);
386 free_cpumask_var(newmask);
387 return -ENOMEM;
388 }
a2584e1d 389
12e0110c 390 rdtgrp = rdtgroup_kn_lock_live(of->kn);
94457b36 391 rdt_last_cmd_clear();
12e0110c
TL
392 if (!rdtgrp) {
393 ret = -ENOENT;
94457b36 394 rdt_last_cmd_puts("directory was removed\n");
12e0110c
TL
395 goto unlock;
396 }
397
4ffa3c97
JO
398 if (is_cpu_list(of))
399 ret = cpulist_parse(buf, newmask);
400 else
401 ret = cpumask_parse(buf, newmask);
402
94457b36
TL
403 if (ret) {
404 rdt_last_cmd_puts("bad cpu list/mask\n");
12e0110c 405 goto unlock;
94457b36 406 }
12e0110c 407
12e0110c
TL
408 /* check that user didn't specify any offline cpus */
409 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
410 if (cpumask_weight(tmpmask)) {
411 ret = -EINVAL;
94457b36 412 rdt_last_cmd_puts("can only assign online cpus\n");
a2584e1d 413 goto unlock;
12e0110c
TL
414 }
415
b09d981b 416 if (rdtgrp->type == RDTCTRL_GROUP)
a9fcf862
VS
417 ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1);
418 else if (rdtgrp->type == RDTMON_GROUP)
419 ret = cpus_mon_write(rdtgrp, newmask, tmpmask);
b09d981b
VS
420 else
421 ret = -EINVAL;
12e0110c 422
12e0110c
TL
423unlock:
424 rdtgroup_kn_unlock(of->kn);
425 free_cpumask_var(tmpmask);
426 free_cpumask_var(newmask);
a9fcf862 427 free_cpumask_var(tmpmask1);
12e0110c
TL
428
429 return ret ?: nbytes;
430}
431
e02737d5
FY
432struct task_move_callback {
433 struct callback_head work;
434 struct rdtgroup *rdtgrp;
435};
436
437static void move_myself(struct callback_head *head)
438{
439 struct task_move_callback *callback;
440 struct rdtgroup *rdtgrp;
441
442 callback = container_of(head, struct task_move_callback, work);
443 rdtgrp = callback->rdtgrp;
444
445 /*
446 * If resource group was deleted before this task work callback
447 * was invoked, then assign the task to root group and free the
448 * resource group.
449 */
450 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
451 (rdtgrp->flags & RDT_DELETED)) {
452 current->closid = 0;
d6aaba61 453 current->rmid = 0;
e02737d5
FY
454 kfree(rdtgrp);
455 }
456
74fcdae1 457 preempt_disable();
4f341a5e
FY
458 /* update PQR_ASSOC MSR to make resource group go into effect */
459 intel_rdt_sched_in();
74fcdae1 460 preempt_enable();
4f341a5e 461
e02737d5
FY
462 kfree(callback);
463}
464
465static int __rdtgroup_move_task(struct task_struct *tsk,
466 struct rdtgroup *rdtgrp)
467{
468 struct task_move_callback *callback;
469 int ret;
470
471 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
472 if (!callback)
473 return -ENOMEM;
474 callback->work.func = move_myself;
475 callback->rdtgrp = rdtgrp;
476
477 /*
478 * Take a refcount, so rdtgrp cannot be freed before the
479 * callback has been invoked.
480 */
481 atomic_inc(&rdtgrp->waitcount);
482 ret = task_work_add(tsk, &callback->work, true);
483 if (ret) {
484 /*
485 * Task is exiting. Drop the refcount and free the callback.
486 * No need to check the refcount as the group cannot be
487 * deleted before the write function unlocks rdtgroup_mutex.
488 */
489 atomic_dec(&rdtgrp->waitcount);
490 kfree(callback);
29e74f35 491 rdt_last_cmd_puts("task exited\n");
e02737d5 492 } else {
d6aaba61
VS
493 /*
494 * For ctrl_mon groups move both closid and rmid.
495 * For monitor groups, can move the tasks only from
496 * their parent CTRL group.
497 */
498 if (rdtgrp->type == RDTCTRL_GROUP) {
499 tsk->closid = rdtgrp->closid;
500 tsk->rmid = rdtgrp->mon.rmid;
501 } else if (rdtgrp->type == RDTMON_GROUP) {
29e74f35 502 if (rdtgrp->mon.parent->closid == tsk->closid) {
d6aaba61 503 tsk->rmid = rdtgrp->mon.rmid;
29e74f35
TL
504 } else {
505 rdt_last_cmd_puts("Can't move task to different control group\n");
d6aaba61 506 ret = -EINVAL;
29e74f35 507 }
d6aaba61 508 }
e02737d5
FY
509 }
510 return ret;
511}
512
513static int rdtgroup_task_write_permission(struct task_struct *task,
514 struct kernfs_open_file *of)
515{
516 const struct cred *tcred = get_task_cred(task);
517 const struct cred *cred = current_cred();
518 int ret = 0;
519
520 /*
521 * Even if we're attaching all tasks in the thread group, we only
522 * need to check permissions on one of them.
523 */
524 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
525 !uid_eq(cred->euid, tcred->uid) &&
29e74f35
TL
526 !uid_eq(cred->euid, tcred->suid)) {
527 rdt_last_cmd_printf("No permission to move task %d\n", task->pid);
e02737d5 528 ret = -EPERM;
29e74f35 529 }
e02737d5
FY
530
531 put_cred(tcred);
532 return ret;
533}
534
535static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
536 struct kernfs_open_file *of)
537{
538 struct task_struct *tsk;
539 int ret;
540
541 rcu_read_lock();
542 if (pid) {
543 tsk = find_task_by_vpid(pid);
544 if (!tsk) {
545 rcu_read_unlock();
29e74f35 546 rdt_last_cmd_printf("No task %d\n", pid);
e02737d5
FY
547 return -ESRCH;
548 }
549 } else {
550 tsk = current;
551 }
552
553 get_task_struct(tsk);
554 rcu_read_unlock();
555
556 ret = rdtgroup_task_write_permission(tsk, of);
557 if (!ret)
558 ret = __rdtgroup_move_task(tsk, rdtgrp);
559
560 put_task_struct(tsk);
561 return ret;
562}
563
564static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
565 char *buf, size_t nbytes, loff_t off)
566{
567 struct rdtgroup *rdtgrp;
568 int ret = 0;
569 pid_t pid;
570
571 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
572 return -EINVAL;
573 rdtgrp = rdtgroup_kn_lock_live(of->kn);
29e74f35 574 rdt_last_cmd_clear();
e02737d5
FY
575
576 if (rdtgrp)
577 ret = rdtgroup_move_task(pid, rdtgrp, of);
578 else
579 ret = -ENOENT;
580
581 rdtgroup_kn_unlock(of->kn);
582
583 return ret ?: nbytes;
584}
585
586static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
587{
588 struct task_struct *p, *t;
589
590 rcu_read_lock();
591 for_each_process_thread(p, t) {
d6aaba61
VS
592 if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) ||
593 (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid))
e02737d5
FY
594 seq_printf(s, "%d\n", t->pid);
595 }
596 rcu_read_unlock();
597}
598
599static int rdtgroup_tasks_show(struct kernfs_open_file *of,
600 struct seq_file *s, void *v)
601{
602 struct rdtgroup *rdtgrp;
603 int ret = 0;
604
605 rdtgrp = rdtgroup_kn_lock_live(of->kn);
606 if (rdtgrp)
607 show_rdt_tasks(rdtgrp, s);
608 else
609 ret = -ENOENT;
610 rdtgroup_kn_unlock(of->kn);
611
612 return ret;
613}
614
9b3a7fd0
TL
615static int rdt_last_cmd_status_show(struct kernfs_open_file *of,
616 struct seq_file *seq, void *v)
617{
618 int len;
619
620 mutex_lock(&rdtgroup_mutex);
621 len = seq_buf_used(&last_cmd_status);
622 if (len)
623 seq_printf(seq, "%.*s", len, last_cmd_status_buf);
624 else
625 seq_puts(seq, "ok\n");
626 mutex_unlock(&rdtgroup_mutex);
627 return 0;
628}
629
4e978d06
FY
630static int rdt_num_closids_show(struct kernfs_open_file *of,
631 struct seq_file *seq, void *v)
632{
633 struct rdt_resource *r = of->kn->parent->priv;
634
635 seq_printf(seq, "%d\n", r->num_closid);
4e978d06
FY
636 return 0;
637}
638
2545e9f5 639static int rdt_default_ctrl_show(struct kernfs_open_file *of,
4e978d06
FY
640 struct seq_file *seq, void *v)
641{
642 struct rdt_resource *r = of->kn->parent->priv;
643
2545e9f5 644 seq_printf(seq, "%x\n", r->default_ctrl);
4e978d06
FY
645 return 0;
646}
647
53a114a6
SL
648static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
649 struct seq_file *seq, void *v)
650{
651 struct rdt_resource *r = of->kn->parent->priv;
652
d3e11b4d 653 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
db69ef65
VS
654 return 0;
655}
656
0dd2d749
FY
657static int rdt_shareable_bits_show(struct kernfs_open_file *of,
658 struct seq_file *seq, void *v)
659{
660 struct rdt_resource *r = of->kn->parent->priv;
661
662 seq_printf(seq, "%x\n", r->cache.shareable_bits);
663 return 0;
664}
665
db69ef65
VS
666static int rdt_min_bw_show(struct kernfs_open_file *of,
667 struct seq_file *seq, void *v)
668{
669 struct rdt_resource *r = of->kn->parent->priv;
53a114a6 670
db69ef65
VS
671 seq_printf(seq, "%u\n", r->membw.min_bw);
672 return 0;
673}
674
d4ab3320
VS
675static int rdt_num_rmids_show(struct kernfs_open_file *of,
676 struct seq_file *seq, void *v)
677{
678 struct rdt_resource *r = of->kn->parent->priv;
679
680 seq_printf(seq, "%d\n", r->num_rmid);
681
682 return 0;
683}
684
685static int rdt_mon_features_show(struct kernfs_open_file *of,
686 struct seq_file *seq, void *v)
687{
688 struct rdt_resource *r = of->kn->parent->priv;
689 struct mon_evt *mevt;
690
691 list_for_each_entry(mevt, &r->evt_list, list)
692 seq_printf(seq, "%s\n", mevt->name);
693
694 return 0;
695}
696
db69ef65
VS
697static int rdt_bw_gran_show(struct kernfs_open_file *of,
698 struct seq_file *seq, void *v)
699{
700 struct rdt_resource *r = of->kn->parent->priv;
701
702 seq_printf(seq, "%u\n", r->membw.bw_gran);
703 return 0;
704}
705
706static int rdt_delay_linear_show(struct kernfs_open_file *of,
707 struct seq_file *seq, void *v)
708{
709 struct rdt_resource *r = of->kn->parent->priv;
710
711 seq_printf(seq, "%u\n", r->membw.delay_linear);
53a114a6
SL
712 return 0;
713}
714
d4ab3320
VS
715static int max_threshold_occ_show(struct kernfs_open_file *of,
716 struct seq_file *seq, void *v)
717{
718 struct rdt_resource *r = of->kn->parent->priv;
719
720 seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale);
721
722 return 0;
723}
724
725static ssize_t max_threshold_occ_write(struct kernfs_open_file *of,
726 char *buf, size_t nbytes, loff_t off)
727{
728 struct rdt_resource *r = of->kn->parent->priv;
729 unsigned int bytes;
730 int ret;
731
732 ret = kstrtouint(buf, 0, &bytes);
733 if (ret)
734 return ret;
735
736 if (bytes > (boot_cpu_data.x86_cache_size * 1024))
737 return -EINVAL;
738
739 intel_cqm_threshold = bytes / r->mon_scale;
740
5707b46a 741 return nbytes;
d4ab3320
VS
742}
743
4e978d06 744/* rdtgroup information files for one cache resource. */
5dc1d5c6 745static struct rftype res_common_files[] = {
9b3a7fd0
TL
746 {
747 .name = "last_cmd_status",
748 .mode = 0444,
749 .kf_ops = &rdtgroup_kf_single_ops,
750 .seq_show = rdt_last_cmd_status_show,
751 .fflags = RF_TOP_INFO,
752 },
4e978d06
FY
753 {
754 .name = "num_closids",
755 .mode = 0444,
756 .kf_ops = &rdtgroup_kf_single_ops,
757 .seq_show = rdt_num_closids_show,
5dc1d5c6 758 .fflags = RF_CTRL_INFO,
4e978d06 759 },
d4ab3320
VS
760 {
761 .name = "mon_features",
762 .mode = 0444,
763 .kf_ops = &rdtgroup_kf_single_ops,
764 .seq_show = rdt_mon_features_show,
765 .fflags = RF_MON_INFO,
766 },
767 {
768 .name = "num_rmids",
769 .mode = 0444,
770 .kf_ops = &rdtgroup_kf_single_ops,
771 .seq_show = rdt_num_rmids_show,
772 .fflags = RF_MON_INFO,
773 },
4e978d06
FY
774 {
775 .name = "cbm_mask",
776 .mode = 0444,
777 .kf_ops = &rdtgroup_kf_single_ops,
2545e9f5 778 .seq_show = rdt_default_ctrl_show,
5dc1d5c6 779 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
4e978d06 780 },
53a114a6
SL
781 {
782 .name = "min_cbm_bits",
783 .mode = 0444,
784 .kf_ops = &rdtgroup_kf_single_ops,
785 .seq_show = rdt_min_cbm_bits_show,
5dc1d5c6 786 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
db69ef65 787 },
0dd2d749
FY
788 {
789 .name = "shareable_bits",
790 .mode = 0444,
791 .kf_ops = &rdtgroup_kf_single_ops,
792 .seq_show = rdt_shareable_bits_show,
793 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
794 },
db69ef65
VS
795 {
796 .name = "min_bandwidth",
797 .mode = 0444,
798 .kf_ops = &rdtgroup_kf_single_ops,
799 .seq_show = rdt_min_bw_show,
5dc1d5c6 800 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
db69ef65
VS
801 },
802 {
803 .name = "bandwidth_gran",
804 .mode = 0444,
805 .kf_ops = &rdtgroup_kf_single_ops,
806 .seq_show = rdt_bw_gran_show,
5dc1d5c6 807 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
db69ef65
VS
808 },
809 {
810 .name = "delay_linear",
811 .mode = 0444,
812 .kf_ops = &rdtgroup_kf_single_ops,
813 .seq_show = rdt_delay_linear_show,
5dc1d5c6
TL
814 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
815 },
d4ab3320
VS
816 {
817 .name = "max_threshold_occupancy",
818 .mode = 0644,
819 .kf_ops = &rdtgroup_kf_single_ops,
820 .write = max_threshold_occ_write,
821 .seq_show = max_threshold_occ_show,
822 .fflags = RF_MON_INFO | RFTYPE_RES_CACHE,
823 },
5dc1d5c6
TL
824 {
825 .name = "cpus",
826 .mode = 0644,
827 .kf_ops = &rdtgroup_kf_single_ops,
828 .write = rdtgroup_cpus_write,
829 .seq_show = rdtgroup_cpus_show,
830 .fflags = RFTYPE_BASE,
831 },
832 {
833 .name = "cpus_list",
834 .mode = 0644,
835 .kf_ops = &rdtgroup_kf_single_ops,
836 .write = rdtgroup_cpus_write,
837 .seq_show = rdtgroup_cpus_show,
838 .flags = RFTYPE_FLAGS_CPUS_LIST,
839 .fflags = RFTYPE_BASE,
840 },
841 {
842 .name = "tasks",
843 .mode = 0644,
844 .kf_ops = &rdtgroup_kf_single_ops,
845 .write = rdtgroup_tasks_write,
846 .seq_show = rdtgroup_tasks_show,
847 .fflags = RFTYPE_BASE,
848 },
849 {
850 .name = "schemata",
851 .mode = 0644,
852 .kf_ops = &rdtgroup_kf_single_ops,
853 .write = rdtgroup_schemata_write,
854 .seq_show = rdtgroup_schemata_show,
855 .fflags = RF_CTRL_BASE,
db69ef65
VS
856 },
857};
858
5dc1d5c6 859static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
db69ef65 860{
5dc1d5c6
TL
861 struct rftype *rfts, *rft;
862 int ret, len;
863
864 rfts = res_common_files;
865 len = ARRAY_SIZE(res_common_files);
866
867 lockdep_assert_held(&rdtgroup_mutex);
868
869 for (rft = rfts; rft < rfts + len; rft++) {
870 if ((fflags & rft->fflags) == rft->fflags) {
871 ret = rdtgroup_add_file(kn, rft);
872 if (ret)
873 goto error;
874 }
875 }
876
877 return 0;
878error:
879 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
880 while (--rft >= rfts) {
881 if ((fflags & rft->fflags) == rft->fflags)
882 kernfs_remove_by_name(kn, rft->name);
883 }
884 return ret;
db69ef65
VS
885}
886
5dc1d5c6
TL
887static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
888 unsigned long fflags)
6a507a6a 889{
5dc1d5c6
TL
890 struct kernfs_node *kn_subdir;
891 int ret;
892
893 kn_subdir = kernfs_create_dir(kn_info, name,
894 kn_info->mode, r);
895 if (IS_ERR(kn_subdir))
896 return PTR_ERR(kn_subdir);
897
898 kernfs_get(kn_subdir);
899 ret = rdtgroup_kn_set_ugid(kn_subdir);
900 if (ret)
901 return ret;
902
903 ret = rdtgroup_add_files(kn_subdir, fflags);
904 if (!ret)
905 kernfs_activate(kn_subdir);
906
907 return ret;
6a507a6a
VS
908}
909
4e978d06
FY
910static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
911{
4e978d06 912 struct rdt_resource *r;
5dc1d5c6 913 unsigned long fflags;
d4ab3320 914 char name[32];
5dc1d5c6 915 int ret;
4e978d06
FY
916
917 /* create the directory */
918 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
919 if (IS_ERR(kn_info))
920 return PTR_ERR(kn_info);
921 kernfs_get(kn_info);
922
9b3a7fd0
TL
923 ret = rdtgroup_add_files(kn_info, RF_TOP_INFO);
924 if (ret)
925 goto out_destroy;
926
1b5c0b75 927 for_each_alloc_enabled_rdt_resource(r) {
5dc1d5c6
TL
928 fflags = r->fflags | RF_CTRL_INFO;
929 ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
4e978d06
FY
930 if (ret)
931 goto out_destroy;
4e978d06 932 }
d4ab3320
VS
933
934 for_each_mon_enabled_rdt_resource(r) {
935 fflags = r->fflags | RF_MON_INFO;
936 sprintf(name, "%s_MON", r->name);
937 ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
938 if (ret)
939 goto out_destroy;
940 }
941
4e978d06
FY
942 /*
943 * This extra ref will be put in kernfs_remove() and guarantees
944 * that @rdtgrp->kn is always accessible.
945 */
946 kernfs_get(kn_info);
947
948 ret = rdtgroup_kn_set_ugid(kn_info);
949 if (ret)
950 goto out_destroy;
951
952 kernfs_activate(kn_info);
953
954 return 0;
955
956out_destroy:
957 kernfs_remove(kn_info);
958 return ret;
959}
960
c7d9aac6
VS
961static int
962mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp,
963 char *name, struct kernfs_node **dest_kn)
964{
965 struct kernfs_node *kn;
966 int ret;
967
968 /* create the directory */
969 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
970 if (IS_ERR(kn))
971 return PTR_ERR(kn);
972
973 if (dest_kn)
974 *dest_kn = kn;
975
976 /*
977 * This extra ref will be put in kernfs_remove() and guarantees
978 * that @rdtgrp->kn is always accessible.
979 */
980 kernfs_get(kn);
981
982 ret = rdtgroup_kn_set_ugid(kn);
983 if (ret)
984 goto out_destroy;
985
986 kernfs_activate(kn);
987
988 return 0;
989
990out_destroy:
991 kernfs_remove(kn);
992 return ret;
993}
5ff193fb
FY
994static void l3_qos_cfg_update(void *arg)
995{
996 bool *enable = arg;
997
998 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
999}
1000
1001static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
1002{
1003 cpumask_var_t cpu_mask;
1004 struct rdt_domain *d;
1005 int cpu;
1006
1007 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1008 return -ENOMEM;
1009
1010 list_for_each_entry(d, &r->domains, list) {
1011 /* Pick one CPU from each domain instance to update MSR */
1012 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1013 }
1014 cpu = get_cpu();
1015 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
1016 if (cpumask_test_cpu(cpu, cpu_mask))
1017 l3_qos_cfg_update(&enable);
1018 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
1019 smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
1020 put_cpu();
1021
1022 free_cpumask_var(cpu_mask);
1023
1024 return 0;
1025}
1026
1027static int cdp_enable(void)
1028{
1029 struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
1030 struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
1031 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
1032 int ret;
1033
1b5c0b75
VS
1034 if (!r_l3->alloc_capable || !r_l3data->alloc_capable ||
1035 !r_l3code->alloc_capable)
5ff193fb
FY
1036 return -EINVAL;
1037
1038 ret = set_l3_qos_cfg(r_l3, true);
1039 if (!ret) {
1b5c0b75
VS
1040 r_l3->alloc_enabled = false;
1041 r_l3data->alloc_enabled = true;
1042 r_l3code->alloc_enabled = true;
5ff193fb
FY
1043 }
1044 return ret;
1045}
1046
1047static void cdp_disable(void)
1048{
1049 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
1050
1b5c0b75 1051 r->alloc_enabled = r->alloc_capable;
5ff193fb 1052
1b5c0b75
VS
1053 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) {
1054 rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled = false;
1055 rdt_resources_all[RDT_RESOURCE_L3CODE].alloc_enabled = false;
5ff193fb
FY
1056 set_l3_qos_cfg(r, false);
1057 }
1058}
1059
1060static int parse_rdtgroupfs_options(char *data)
1061{
1062 char *token, *o = data;
1063 int ret = 0;
1064
1065 while ((token = strsep(&o, ",")) != NULL) {
1066 if (!*token)
1067 return -EINVAL;
1068
1069 if (!strcmp(token, "cdp"))
1070 ret = cdp_enable();
1071 }
1072
1073 return ret;
1074}
1075
60cf5e10
FY
1076/*
1077 * We don't allow rdtgroup directories to be created anywhere
1078 * except the root directory. Thus when looking for the rdtgroup
1079 * structure for a kernfs node we are either looking at a directory,
1080 * in which case the rdtgroup structure is pointed at by the "priv"
1081 * field, otherwise we have a file, and need only look to the parent
1082 * to find the rdtgroup.
1083 */
1084static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
1085{
f57b3087
FY
1086 if (kernfs_type(kn) == KERNFS_DIR) {
1087 /*
1088 * All the resource directories use "kn->priv"
1089 * to point to the "struct rdtgroup" for the
1090 * resource. "info" and its subdirectories don't
1091 * have rdtgroup structures, so return NULL here.
1092 */
1093 if (kn == kn_info || kn->parent == kn_info)
1094 return NULL;
1095 else
1096 return kn->priv;
1097 } else {
60cf5e10 1098 return kn->parent->priv;
f57b3087 1099 }
60cf5e10
FY
1100}
1101
1102struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
1103{
1104 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1105
f57b3087
FY
1106 if (!rdtgrp)
1107 return NULL;
1108
60cf5e10
FY
1109 atomic_inc(&rdtgrp->waitcount);
1110 kernfs_break_active_protection(kn);
1111
1112 mutex_lock(&rdtgroup_mutex);
1113
1114 /* Was this group deleted while we waited? */
1115 if (rdtgrp->flags & RDT_DELETED)
1116 return NULL;
1117
1118 return rdtgrp;
1119}
1120
1121void rdtgroup_kn_unlock(struct kernfs_node *kn)
1122{
1123 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
1124
f57b3087
FY
1125 if (!rdtgrp)
1126 return;
1127
60cf5e10
FY
1128 mutex_unlock(&rdtgroup_mutex);
1129
1130 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
1131 (rdtgrp->flags & RDT_DELETED)) {
1132 kernfs_unbreak_active_protection(kn);
49ec8f5b 1133 kernfs_put(rdtgrp->kn);
60cf5e10
FY
1134 kfree(rdtgrp);
1135 } else {
1136 kernfs_unbreak_active_protection(kn);
1137 }
1138}
1139
4af4a88e
VS
1140static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1141 struct rdtgroup *prgrp,
1142 struct kernfs_node **mon_data_kn);
1143
5ff193fb
FY
1144static struct dentry *rdt_mount(struct file_system_type *fs_type,
1145 int flags, const char *unused_dev_name,
1146 void *data)
1147{
e3302683
VS
1148 struct rdt_domain *dom;
1149 struct rdt_resource *r;
5ff193fb
FY
1150 struct dentry *dentry;
1151 int ret;
1152
87943db7 1153 cpus_read_lock();
5ff193fb
FY
1154 mutex_lock(&rdtgroup_mutex);
1155 /*
1156 * resctrl file system can only be mounted once.
1157 */
4af4a88e 1158 if (static_branch_unlikely(&rdt_enable_key)) {
5ff193fb
FY
1159 dentry = ERR_PTR(-EBUSY);
1160 goto out;
1161 }
1162
1163 ret = parse_rdtgroupfs_options(data);
1164 if (ret) {
1165 dentry = ERR_PTR(ret);
1166 goto out_cdp;
1167 }
1168
60cf5e10
FY
1169 closid_init();
1170
4e978d06 1171 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
7bff0af5
SL
1172 if (ret) {
1173 dentry = ERR_PTR(ret);
4e978d06 1174 goto out_cdp;
7bff0af5 1175 }
4e978d06 1176
4af4a88e
VS
1177 if (rdt_mon_capable) {
1178 ret = mongroup_create_dir(rdtgroup_default.kn,
2c6d033d 1179 &rdtgroup_default, "mon_groups",
4af4a88e
VS
1180 &kn_mongrp);
1181 if (ret) {
1182 dentry = ERR_PTR(ret);
1183 goto out_info;
1184 }
1185 kernfs_get(kn_mongrp);
1186
1187 ret = mkdir_mondata_all(rdtgroup_default.kn,
1188 &rdtgroup_default, &kn_mondata);
1189 if (ret) {
1190 dentry = ERR_PTR(ret);
1191 goto out_mongrp;
1192 }
1193 kernfs_get(kn_mondata);
1194 rdtgroup_default.mon.mon_data_kn = kn_mondata;
1195 }
1196
5ff193fb
FY
1197 dentry = kernfs_mount(fs_type, flags, rdt_root,
1198 RDTGROUP_SUPER_MAGIC, NULL);
1199 if (IS_ERR(dentry))
4af4a88e
VS
1200 goto out_mondata;
1201
1202 if (rdt_alloc_capable)
87943db7 1203 static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
4af4a88e 1204 if (rdt_mon_capable)
87943db7 1205 static_branch_enable_cpuslocked(&rdt_mon_enable_key);
5ff193fb 1206
4af4a88e 1207 if (rdt_alloc_capable || rdt_mon_capable)
87943db7 1208 static_branch_enable_cpuslocked(&rdt_enable_key);
e3302683
VS
1209
1210 if (is_mbm_enabled()) {
1211 r = &rdt_resources_all[RDT_RESOURCE_L3];
1212 list_for_each_entry(dom, &r->domains, list)
bbc4615e 1213 mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL);
e3302683
VS
1214 }
1215
5ff193fb
FY
1216 goto out;
1217
4af4a88e
VS
1218out_mondata:
1219 if (rdt_mon_capable)
1220 kernfs_remove(kn_mondata);
1221out_mongrp:
1222 if (rdt_mon_capable)
1223 kernfs_remove(kn_mongrp);
1224out_info:
79298acc 1225 kernfs_remove(kn_info);
5ff193fb
FY
1226out_cdp:
1227 cdp_disable();
1228out:
9b3a7fd0 1229 rdt_last_cmd_clear();
5ff193fb 1230 mutex_unlock(&rdtgroup_mutex);
87943db7 1231 cpus_read_unlock();
5ff193fb
FY
1232
1233 return dentry;
1234}
1235
2545e9f5 1236static int reset_all_ctrls(struct rdt_resource *r)
5ff193fb
FY
1237{
1238 struct msr_param msr_param;
1239 cpumask_var_t cpu_mask;
1240 struct rdt_domain *d;
1241 int i, cpu;
1242
1243 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
1244 return -ENOMEM;
1245
1246 msr_param.res = r;
1247 msr_param.low = 0;
1248 msr_param.high = r->num_closid;
1249
1250 /*
1251 * Disable resource control for this resource by setting all
1252 * CBMs in all domains to the maximum mask value. Pick one CPU
1253 * from each domain to update the MSRs below.
1254 */
1255 list_for_each_entry(d, &r->domains, list) {
1256 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
1257
1258 for (i = 0; i < r->num_closid; i++)
2545e9f5 1259 d->ctrl_val[i] = r->default_ctrl;
5ff193fb
FY
1260 }
1261 cpu = get_cpu();
1262 /* Update CBM on this cpu if it's in cpu_mask. */
1263 if (cpumask_test_cpu(cpu, cpu_mask))
2545e9f5 1264 rdt_ctrl_update(&msr_param);
5ff193fb 1265 /* Update CBM on all other cpus in cpu_mask. */
2545e9f5 1266 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
5ff193fb
FY
1267 put_cpu();
1268
1269 free_cpumask_var(cpu_mask);
1270
1271 return 0;
1272}
1273
f3cbeaca
VS
1274static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)
1275{
1276 return (rdt_alloc_capable &&
1277 (r->type == RDTCTRL_GROUP) && (t->closid == r->closid));
1278}
1279
1280static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r)
1281{
1282 return (rdt_mon_capable &&
1283 (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid));
1284}
1285
4e978d06 1286/*
0efc89be
FY
1287 * Move tasks from one to the other group. If @from is NULL, then all tasks
1288 * in the systems are moved unconditionally (used for teardown).
1289 *
1290 * If @mask is not NULL the cpus on which moved tasks are running are set
1291 * in that mask so the update smp function call is restricted to affected
1292 * cpus.
4e978d06 1293 */
0efc89be
FY
1294static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
1295 struct cpumask *mask)
4e978d06 1296{
e02737d5
FY
1297 struct task_struct *p, *t;
1298
e02737d5 1299 read_lock(&tasklist_lock);
0efc89be 1300 for_each_process_thread(p, t) {
f3cbeaca
VS
1301 if (!from || is_closid_match(t, from) ||
1302 is_rmid_match(t, from)) {
0efc89be 1303 t->closid = to->closid;
f3cbeaca
VS
1304 t->rmid = to->mon.rmid;
1305
0efc89be
FY
1306#ifdef CONFIG_SMP
1307 /*
1308 * This is safe on x86 w/o barriers as the ordering
1309 * of writing to task_cpu() and t->on_cpu is
1310 * reverse to the reading here. The detection is
1311 * inaccurate as tasks might move or schedule
1312 * before the smp function call takes place. In
1313 * such a case the function call is pointless, but
1314 * there is no other side effect.
1315 */
1316 if (mask && t->on_cpu)
1317 cpumask_set_cpu(task_cpu(t), mask);
1318#endif
1319 }
1320 }
e02737d5 1321 read_unlock(&tasklist_lock);
0efc89be
FY
1322}
1323
f3cbeaca
VS
1324static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
1325{
1326 struct rdtgroup *sentry, *stmp;
1327 struct list_head *head;
1328
1329 head = &rdtgrp->mon.crdtgrp_list;
1330 list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
1331 free_rmid(sentry->mon.rmid);
1332 list_del(&sentry->mon.crdtgrp_list);
39af14f8
XS
1333
1334 if (atomic_read(&sentry->waitcount) != 0)
1335 sentry->flags = RDT_DELETED;
1336 else
1337 kfree(sentry);
f3cbeaca
VS
1338 }
1339}
1340
0efc89be
FY
1341/*
1342 * Forcibly remove all of subdirectories under root.
1343 */
1344static void rmdir_all_sub(void)
1345{
1346 struct rdtgroup *rdtgrp, *tmp;
1347
1348 /* Move all tasks to the default resource group */
1349 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
60cf5e10 1350
60cf5e10 1351 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
4af4a88e
VS
1352 /* Free any child rmids */
1353 free_all_child_rdtgrp(rdtgrp);
1354
60cf5e10
FY
1355 /* Remove each rdtgroup other than root */
1356 if (rdtgrp == &rdtgroup_default)
1357 continue;
c7cc0cc1
FY
1358
1359 /*
1360 * Give any CPUs back to the default group. We cannot copy
1361 * cpu_online_mask because a CPU might have executed the
1362 * offline callback already, but is still marked online.
1363 */
1364 cpumask_or(&rdtgroup_default.cpu_mask,
1365 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
1366
4af4a88e
VS
1367 free_rmid(rdtgrp->mon.rmid);
1368
60cf5e10
FY
1369 kernfs_remove(rdtgrp->kn);
1370 list_del(&rdtgrp->rdtgroup_list);
39af14f8
XS
1371
1372 if (atomic_read(&rdtgrp->waitcount) != 0)
1373 rdtgrp->flags = RDT_DELETED;
1374 else
1375 kfree(rdtgrp);
60cf5e10 1376 }
0efc89be 1377 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
a9fcf862 1378 update_closid_rmid(cpu_online_mask, &rdtgroup_default);
0efc89be 1379
4e978d06 1380 kernfs_remove(kn_info);
4af4a88e
VS
1381 kernfs_remove(kn_mongrp);
1382 kernfs_remove(kn_mondata);
4e978d06
FY
1383}
1384
5ff193fb
FY
1385static void rdt_kill_sb(struct super_block *sb)
1386{
1387 struct rdt_resource *r;
1388
36b6f9fc 1389 cpus_read_lock();
5ff193fb
FY
1390 mutex_lock(&rdtgroup_mutex);
1391
1392 /*Put everything back to default values. */
1b5c0b75 1393 for_each_alloc_enabled_rdt_resource(r)
2545e9f5 1394 reset_all_ctrls(r);
5ff193fb 1395 cdp_disable();
4e978d06 1396 rmdir_all_sub();
36b6f9fc
RC
1397 static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
1398 static_branch_disable_cpuslocked(&rdt_mon_enable_key);
1399 static_branch_disable_cpuslocked(&rdt_enable_key);
5ff193fb
FY
1400 kernfs_kill_sb(sb);
1401 mutex_unlock(&rdtgroup_mutex);
36b6f9fc 1402 cpus_read_unlock();
5ff193fb
FY
1403}
1404
1405static struct file_system_type rdt_fs_type = {
1406 .name = "resctrl",
1407 .mount = rdt_mount,
1408 .kill_sb = rdt_kill_sb,
1409};
1410
d89b7379
VS
1411static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
1412 void *priv)
1413{
1414 struct kernfs_node *kn;
1415 int ret = 0;
1416
630d0eb6
DT
1417 kn = __kernfs_create_file(parent_kn, name, 0444,
1418 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0,
d89b7379
VS
1419 &kf_mondata_ops, priv, NULL, NULL);
1420 if (IS_ERR(kn))
1421 return PTR_ERR(kn);
1422
1423 ret = rdtgroup_kn_set_ugid(kn);
1424 if (ret) {
1425 kernfs_remove(kn);
1426 return ret;
1427 }
1428
1429 return ret;
1430}
1431
895c663e
VS
1432/*
1433 * Remove all subdirectories of mon_data of ctrl_mon groups
1434 * and monitor groups with given domain id.
1435 */
1436void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id)
1437{
1438 struct rdtgroup *prgrp, *crgrp;
1439 char name[32];
1440
1441 if (!r->mon_enabled)
1442 return;
1443
1444 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
1445 sprintf(name, "mon_%s_%02d", r->name, dom_id);
1446 kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
1447
1448 list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
1449 kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
1450 }
1451}
1452
d89b7379
VS
1453static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
1454 struct rdt_domain *d,
1455 struct rdt_resource *r, struct rdtgroup *prgrp)
1456{
1457 union mon_data_bits priv;
1458 struct kernfs_node *kn;
1459 struct mon_evt *mevt;
a4de1dfd 1460 struct rmid_read rr;
d89b7379
VS
1461 char name[32];
1462 int ret;
1463
1464 sprintf(name, "mon_%s_%02d", r->name, d->id);
1465 /* create the directory */
1466 kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
1467 if (IS_ERR(kn))
1468 return PTR_ERR(kn);
1469
1470 /*
1471 * This extra ref will be put in kernfs_remove() and guarantees
1472 * that kn is always accessible.
1473 */
1474 kernfs_get(kn);
1475 ret = rdtgroup_kn_set_ugid(kn);
1476 if (ret)
1477 goto out_destroy;
1478
1479 if (WARN_ON(list_empty(&r->evt_list))) {
1480 ret = -EPERM;
1481 goto out_destroy;
1482 }
1483
1484 priv.u.rid = r->rid;
1485 priv.u.domid = d->id;
1486 list_for_each_entry(mevt, &r->evt_list, list) {
1487 priv.u.evtid = mevt->evtid;
1488 ret = mon_addfile(kn, mevt->name, priv.priv);
1489 if (ret)
1490 goto out_destroy;
a4de1dfd
VS
1491
1492 if (is_mbm_event(mevt->evtid))
1493 mon_event_read(&rr, d, prgrp, mevt->evtid, true);
d89b7379
VS
1494 }
1495 kernfs_activate(kn);
1496 return 0;
1497
1498out_destroy:
1499 kernfs_remove(kn);
1500 return ret;
1501}
1502
895c663e
VS
1503/*
1504 * Add all subdirectories of mon_data for "ctrl_mon" groups
1505 * and "monitor" groups with given domain id.
1506 */
1507void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
1508 struct rdt_domain *d)
1509{
1510 struct kernfs_node *parent_kn;
1511 struct rdtgroup *prgrp, *crgrp;
1512 struct list_head *head;
1513
1514 if (!r->mon_enabled)
1515 return;
1516
1517 list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
1518 parent_kn = prgrp->mon.mon_data_kn;
1519 mkdir_mondata_subdir(parent_kn, d, r, prgrp);
1520
1521 head = &prgrp->mon.crdtgrp_list;
1522 list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
1523 parent_kn = crgrp->mon.mon_data_kn;
1524 mkdir_mondata_subdir(parent_kn, d, r, crgrp);
1525 }
1526 }
1527}
1528
d89b7379
VS
1529static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
1530 struct rdt_resource *r,
1531 struct rdtgroup *prgrp)
1532{
1533 struct rdt_domain *dom;
1534 int ret;
1535
1536 list_for_each_entry(dom, &r->domains, list) {
1537 ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp);
1538 if (ret)
1539 return ret;
1540 }
1541
1542 return 0;
1543}
1544
1545/*
1546 * This creates a directory mon_data which contains the monitored data.
1547 *
1548 * mon_data has one directory for each domain whic are named
1549 * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data
1550 * with L3 domain looks as below:
1551 * ./mon_data:
1552 * mon_L3_00
1553 * mon_L3_01
1554 * mon_L3_02
1555 * ...
1556 *
1557 * Each domain directory has one file per event:
1558 * ./mon_L3_00/:
1559 * llc_occupancy
1560 *
1561 */
1562static int mkdir_mondata_all(struct kernfs_node *parent_kn,
1563 struct rdtgroup *prgrp,
1564 struct kernfs_node **dest_kn)
1565{
1566 struct rdt_resource *r;
1567 struct kernfs_node *kn;
1568 int ret;
1569
1570 /*
1571 * Create the mon_data directory first.
1572 */
2c6d033d 1573 ret = mongroup_create_dir(parent_kn, prgrp, "mon_data", &kn);
d89b7379
VS
1574 if (ret)
1575 return ret;
1576
1577 if (dest_kn)
1578 *dest_kn = kn;
1579
1580 /*
1581 * Create the subdirectories for each domain. Note that all events
1582 * in a domain like L3 are grouped into a resource whose domain is L3
1583 */
1584 for_each_mon_enabled_rdt_resource(r) {
1585 ret = mkdir_mondata_subdir_alldom(kn, r, prgrp);
1586 if (ret)
1587 goto out_destroy;
1588 }
1589
1590 return 0;
1591
1592out_destroy:
1593 kernfs_remove(kn);
1594 return ret;
1595}
1596
65b4f403
VS
1597static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
1598 struct kernfs_node *prgrp_kn,
1599 const char *name, umode_t mode,
c7d9aac6 1600 enum rdt_group_type rtype, struct rdtgroup **r)
60cf5e10 1601{
65b4f403 1602 struct rdtgroup *prdtgrp, *rdtgrp;
60cf5e10 1603 struct kernfs_node *kn;
65b4f403
VS
1604 uint files = 0;
1605 int ret;
60cf5e10 1606
2c6d033d 1607 prdtgrp = rdtgroup_kn_lock_live(parent_kn);
cfd0f34e 1608 rdt_last_cmd_clear();
65b4f403 1609 if (!prdtgrp) {
60cf5e10 1610 ret = -ENODEV;
cfd0f34e 1611 rdt_last_cmd_puts("directory was removed\n");
60cf5e10
FY
1612 goto out_unlock;
1613 }
1614
60cf5e10
FY
1615 /* allocate the rdtgroup. */
1616 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
1617 if (!rdtgrp) {
1618 ret = -ENOSPC;
cfd0f34e 1619 rdt_last_cmd_puts("kernel out of memory\n");
65b4f403 1620 goto out_unlock;
60cf5e10 1621 }
65b4f403 1622 *r = rdtgrp;
c7d9aac6
VS
1623 rdtgrp->mon.parent = prdtgrp;
1624 rdtgrp->type = rtype;
1625 INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list);
60cf5e10
FY
1626
1627 /* kernfs creates the directory for rdtgrp */
65b4f403 1628 kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp);
60cf5e10
FY
1629 if (IS_ERR(kn)) {
1630 ret = PTR_ERR(kn);
cfd0f34e 1631 rdt_last_cmd_puts("kernfs create error\n");
65b4f403 1632 goto out_free_rgrp;
60cf5e10
FY
1633 }
1634 rdtgrp->kn = kn;
1635
1636 /*
1637 * kernfs_remove() will drop the reference count on "kn" which
1638 * will free it. But we still need it to stick around for the
1639 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
1640 * here, which will be dropped inside rdtgroup_kn_unlock().
1641 */
1642 kernfs_get(kn);
1643
1644 ret = rdtgroup_kn_set_ugid(kn);
cfd0f34e
TL
1645 if (ret) {
1646 rdt_last_cmd_puts("kernfs perm error\n");
60cf5e10 1647 goto out_destroy;
cfd0f34e 1648 }
60cf5e10 1649
c7d9aac6 1650 files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype);
65b4f403 1651 ret = rdtgroup_add_files(kn, files);
cfd0f34e
TL
1652 if (ret) {
1653 rdt_last_cmd_puts("kernfs fill error\n");
12e0110c 1654 goto out_destroy;
cfd0f34e 1655 }
12e0110c 1656
c7d9aac6
VS
1657 if (rdt_mon_capable) {
1658 ret = alloc_rmid();
cfd0f34e
TL
1659 if (ret < 0) {
1660 rdt_last_cmd_puts("out of RMIDs\n");
c7d9aac6 1661 goto out_destroy;
cfd0f34e 1662 }
c7d9aac6 1663 rdtgrp->mon.rmid = ret;
d89b7379
VS
1664
1665 ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn);
cfd0f34e
TL
1666 if (ret) {
1667 rdt_last_cmd_puts("kernfs subdir error\n");
d89b7379 1668 goto out_idfree;
cfd0f34e 1669 }
c7d9aac6 1670 }
60cf5e10
FY
1671 kernfs_activate(kn);
1672
65b4f403 1673 /*
2c6d033d 1674 * The caller unlocks the parent_kn upon success.
65b4f403
VS
1675 */
1676 return 0;
60cf5e10 1677
d89b7379
VS
1678out_idfree:
1679 free_rmid(rdtgrp->mon.rmid);
60cf5e10
FY
1680out_destroy:
1681 kernfs_remove(rdtgrp->kn);
65b4f403 1682out_free_rgrp:
60cf5e10 1683 kfree(rdtgrp);
60cf5e10 1684out_unlock:
2c6d033d 1685 rdtgroup_kn_unlock(parent_kn);
65b4f403
VS
1686 return ret;
1687}
1688
1689static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp)
1690{
1691 kernfs_remove(rgrp->kn);
c7d9aac6 1692 free_rmid(rgrp->mon.rmid);
65b4f403
VS
1693 kfree(rgrp);
1694}
1695
c7d9aac6
VS
1696/*
1697 * Create a monitor group under "mon_groups" directory of a control
1698 * and monitor group(ctrl_mon). This is a resource group
1699 * to monitor a subset of tasks and cpus in its parent ctrl_mon group.
1700 */
1701static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
1702 struct kernfs_node *prgrp_kn,
1703 const char *name,
1704 umode_t mode)
1705{
1706 struct rdtgroup *rdtgrp, *prgrp;
1707 int ret;
1708
1709 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP,
1710 &rdtgrp);
1711 if (ret)
1712 return ret;
1713
1714 prgrp = rdtgrp->mon.parent;
1715 rdtgrp->closid = prgrp->closid;
1716
1717 /*
1718 * Add the rdtgrp to the list of rdtgrps the parent
1719 * ctrl_mon group has to track.
1720 */
1721 list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list);
1722
2c6d033d 1723 rdtgroup_kn_unlock(parent_kn);
c7d9aac6
VS
1724 return ret;
1725}
1726
65b4f403
VS
1727/*
1728 * These are rdtgroups created under the root directory. Can be used
c7d9aac6 1729 * to allocate and monitor resources.
65b4f403 1730 */
c7d9aac6
VS
1731static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
1732 struct kernfs_node *prgrp_kn,
1733 const char *name, umode_t mode)
65b4f403
VS
1734{
1735 struct rdtgroup *rdtgrp;
1736 struct kernfs_node *kn;
1737 u32 closid;
1738 int ret;
1739
c7d9aac6
VS
1740 ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP,
1741 &rdtgrp);
65b4f403
VS
1742 if (ret)
1743 return ret;
1744
1745 kn = rdtgrp->kn;
1746 ret = closid_alloc();
cfd0f34e
TL
1747 if (ret < 0) {
1748 rdt_last_cmd_puts("out of CLOSIDs\n");
65b4f403 1749 goto out_common_fail;
cfd0f34e 1750 }
65b4f403 1751 closid = ret;
3d74ade8 1752 ret = 0;
65b4f403
VS
1753
1754 rdtgrp->closid = closid;
1755 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
1756
c7d9aac6
VS
1757 if (rdt_mon_capable) {
1758 /*
1759 * Create an empty mon_groups directory to hold the subset
1760 * of tasks and cpus to monitor.
1761 */
2c6d033d 1762 ret = mongroup_create_dir(kn, rdtgrp, "mon_groups", NULL);
cfd0f34e
TL
1763 if (ret) {
1764 rdt_last_cmd_puts("kernfs subdir error\n");
c7d9aac6 1765 goto out_id_free;
cfd0f34e 1766 }
c7d9aac6
VS
1767 }
1768
65b4f403
VS
1769 goto out_unlock;
1770
c7d9aac6
VS
1771out_id_free:
1772 closid_free(closid);
1773 list_del(&rdtgrp->rdtgroup_list);
65b4f403
VS
1774out_common_fail:
1775 mkdir_rdt_prepare_clean(rdtgrp);
1776out_unlock:
2c6d033d 1777 rdtgroup_kn_unlock(parent_kn);
60cf5e10
FY
1778 return ret;
1779}
1780
c7d9aac6
VS
1781/*
1782 * We allow creating mon groups only with in a directory called "mon_groups"
1783 * which is present in every ctrl_mon group. Check if this is a valid
1784 * "mon_groups" directory.
1785 *
1786 * 1. The directory should be named "mon_groups".
1787 * 2. The mon group itself should "not" be named "mon_groups".
1788 * This makes sure "mon_groups" directory always has a ctrl_mon group
1789 * as parent.
1790 */
1791static bool is_mon_groups(struct kernfs_node *kn, const char *name)
1792{
1793 return (!strcmp(kn->name, "mon_groups") &&
1794 strcmp(name, "mon_groups"));
1795}
1796
65b4f403
VS
1797static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
1798 umode_t mode)
1799{
1800 /* Do not accept '\n' to avoid unparsable situation. */
1801 if (strchr(name, '\n'))
1802 return -EINVAL;
1803
1804 /*
1805 * If the parent directory is the root directory and RDT
c7d9aac6
VS
1806 * allocation is supported, add a control and monitoring
1807 * subdirectory
65b4f403
VS
1808 */
1809 if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn)
c7d9aac6
VS
1810 return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode);
1811
1812 /*
1813 * If RDT monitoring is supported and the parent directory is a valid
1814 * "mon_groups" directory, add a monitoring subdirectory.
1815 */
1816 if (rdt_mon_capable && is_mon_groups(parent_kn, name))
1817 return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode);
65b4f403
VS
1818
1819 return -EPERM;
1820}
1821
f3cbeaca
VS
1822static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
1823 cpumask_var_t tmpmask)
1824{
1825 struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
1826 int cpu;
1827
1828 /* Give any tasks back to the parent group */
1829 rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
1830
1831 /* Update per cpu rmid of the moved CPUs first */
1832 for_each_cpu(cpu, &rdtgrp->cpu_mask)
a9110b55 1833 per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
f3cbeaca
VS
1834 /*
1835 * Update the MSR on moved CPUs and CPUs which have moved
1836 * task running on them.
1837 */
1838 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1839 update_closid_rmid(tmpmask, NULL);
1840
1841 rdtgrp->flags = RDT_DELETED;
1842 free_rmid(rdtgrp->mon.rmid);
1843
1844 /*
1845 * Remove the rdtgrp from the parent ctrl_mon group's list
1846 */
1847 WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list));
1848 list_del(&rdtgrp->mon.crdtgrp_list);
1849
1850 /*
1851 * one extra hold on this, will drop when we kfree(rdtgrp)
1852 * in rdtgroup_kn_unlock()
1853 */
1854 kernfs_get(kn);
1855 kernfs_remove(rdtgrp->kn);
1856
c9c5f0ce
XS
1857 /*
1858 * Free all the child monitor group rmids.
1859 */
1860 free_all_child_rdtgrp(rdtgrp);
1861
f3cbeaca
VS
1862 return 0;
1863}
1864
f9049547
VS
1865static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp,
1866 cpumask_var_t tmpmask)
60cf5e10 1867{
f9049547 1868 int cpu;
60cf5e10 1869
e02737d5 1870 /* Give any tasks back to the default group */
0efc89be 1871 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
e02737d5 1872
12e0110c
TL
1873 /* Give any CPUs back to the default group */
1874 cpumask_or(&rdtgroup_default.cpu_mask,
1875 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
0efc89be 1876
f3cbeaca
VS
1877 /* Update per cpu closid and rmid of the moved CPUs first */
1878 for_each_cpu(cpu, &rdtgrp->cpu_mask) {
a9110b55
VS
1879 per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
1880 per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
f3cbeaca
VS
1881 }
1882
0efc89be
FY
1883 /*
1884 * Update the MSR on moved CPUs and CPUs which have moved
1885 * task running on them.
1886 */
1887 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
a9fcf862 1888 update_closid_rmid(tmpmask, NULL);
12e0110c 1889
60cf5e10
FY
1890 rdtgrp->flags = RDT_DELETED;
1891 closid_free(rdtgrp->closid);
f3cbeaca
VS
1892 free_rmid(rdtgrp->mon.rmid);
1893
60cf5e10
FY
1894 list_del(&rdtgrp->rdtgroup_list);
1895
1896 /*
1897 * one extra hold on this, will drop when we kfree(rdtgrp)
1898 * in rdtgroup_kn_unlock()
1899 */
1900 kernfs_get(kn);
1901 kernfs_remove(rdtgrp->kn);
f9049547
VS
1902
1903 return 0;
1904}
1905
1906static int rdtgroup_rmdir(struct kernfs_node *kn)
1907{
1908 struct kernfs_node *parent_kn = kn->parent;
1909 struct rdtgroup *rdtgrp;
1910 cpumask_var_t tmpmask;
1911 int ret = 0;
1912
1913 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1914 return -ENOMEM;
1915
1916 rdtgrp = rdtgroup_kn_lock_live(kn);
1917 if (!rdtgrp) {
1918 ret = -EPERM;
1919 goto out;
1920 }
1921
1922 /*
1923 * If the rdtgroup is a ctrl_mon group and parent directory
f3cbeaca
VS
1924 * is the root directory, remove the ctrl_mon group.
1925 *
1926 * If the rdtgroup is a mon group and parent directory
1927 * is a valid "mon_groups" directory, remove the mon group.
f9049547
VS
1928 */
1929 if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn)
1930 ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask);
f3cbeaca
VS
1931 else if (rdtgrp->type == RDTMON_GROUP &&
1932 is_mon_groups(parent_kn, kn->name))
1933 ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask);
f9049547
VS
1934 else
1935 ret = -EPERM;
1936
0efc89be 1937out:
60cf5e10 1938 rdtgroup_kn_unlock(kn);
0efc89be
FY
1939 free_cpumask_var(tmpmask);
1940 return ret;
60cf5e10
FY
1941}
1942
76ae054c
SL
1943static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1944{
1b5c0b75 1945 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
76ae054c
SL
1946 seq_puts(seq, ",cdp");
1947 return 0;
1948}
1949
5ff193fb 1950static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
76ae054c
SL
1951 .mkdir = rdtgroup_mkdir,
1952 .rmdir = rdtgroup_rmdir,
1953 .show_options = rdtgroup_show_options,
5ff193fb
FY
1954};
1955
1956static int __init rdtgroup_setup_root(void)
1957{
12e0110c
TL
1958 int ret;
1959
5ff193fb
FY
1960 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1961 KERNFS_ROOT_CREATE_DEACTIVATED,
1962 &rdtgroup_default);
1963 if (IS_ERR(rdt_root))
1964 return PTR_ERR(rdt_root);
1965
1966 mutex_lock(&rdtgroup_mutex);
1967
1968 rdtgroup_default.closid = 0;
c7d9aac6
VS
1969 rdtgroup_default.mon.rmid = 0;
1970 rdtgroup_default.type = RDTCTRL_GROUP;
1971 INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list);
1972
5ff193fb
FY
1973 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1974
5dc1d5c6 1975 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
12e0110c
TL
1976 if (ret) {
1977 kernfs_destroy_root(rdt_root);
1978 goto out;
1979 }
1980
5ff193fb
FY
1981 rdtgroup_default.kn = rdt_root->kn;
1982 kernfs_activate(rdtgroup_default.kn);
1983
12e0110c 1984out:
5ff193fb
FY
1985 mutex_unlock(&rdtgroup_mutex);
1986
12e0110c 1987 return ret;
5ff193fb
FY
1988}
1989
1990/*
1991 * rdtgroup_init - rdtgroup initialization
1992 *
1993 * Setup resctrl file system including set up root, create mount point,
1994 * register rdtgroup filesystem, and initialize files under root directory.
1995 *
1996 * Return: 0 on success or -errno
1997 */
1998int __init rdtgroup_init(void)
1999{
2000 int ret = 0;
2001
9b3a7fd0
TL
2002 seq_buf_init(&last_cmd_status, last_cmd_status_buf,
2003 sizeof(last_cmd_status_buf));
2004
5ff193fb
FY
2005 ret = rdtgroup_setup_root();
2006 if (ret)
2007 return ret;
2008
2009 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
2010 if (ret)
2011 goto cleanup_root;
2012
2013 ret = register_filesystem(&rdt_fs_type);
2014 if (ret)
2015 goto cleanup_mountpoint;
2016
2017 return 0;
2018
2019cleanup_mountpoint:
2020 sysfs_remove_mount_point(fs_kobj, "resctrl");
2021cleanup_root:
2022 kernfs_destroy_root(rdt_root);
2023
2024 return ret;
2025}