]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/cpu/intel_rdt_rdtgroup.c
x86/intel_rdt: Simplify info and base file lists
[mirror_ubuntu-artful-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>
4e978d06 27#include <linux/seq_file.h>
3f07c014 28#include <linux/sched/signal.h>
29930025 29#include <linux/sched/task.h>
5ff193fb 30#include <linux/slab.h>
e02737d5 31#include <linux/task_work.h>
5ff193fb
FY
32
33#include <uapi/linux/magic.h>
34
7db9d979
VS
35#include <asm/intel_rdt_sched.h>
36#include "intel_rdt.h"
5ff193fb 37
26017611 38DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
0fc5d206 39static struct kernfs_root *rdt_root;
5ff193fb
FY
40struct rdtgroup rdtgroup_default;
41LIST_HEAD(rdt_all_groups);
42
4e978d06
FY
43/* Kernel fs node for "info" directory under root */
44static struct kernfs_node *kn_info;
45
60cf5e10
FY
46/*
47 * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
48 * we can keep a bitmap of free CLOSIDs in a single integer.
49 *
50 * Using a global CLOSID across all resources has some advantages and
51 * some drawbacks:
52 * + We can simply set "current->closid" to assign a task to a resource
53 * group.
54 * + Context switch code can avoid extra memory references deciding which
55 * CLOSID to load into the PQR_ASSOC MSR
56 * - We give up some options in configuring resource groups across multi-socket
57 * systems.
58 * - Our choices on how to configure each resource become progressively more
59 * limited as the number of resources grows.
60 */
61static int closid_free_map;
62
63static void closid_init(void)
64{
65 struct rdt_resource *r;
66 int rdt_min_closid = 32;
67
68 /* Compute rdt_min_closid across all resources */
26017611 69 for_each_alloc_enabled_rdt_resource(r)
60cf5e10
FY
70 rdt_min_closid = min(rdt_min_closid, r->num_closid);
71
72 closid_free_map = BIT_MASK(rdt_min_closid) - 1;
73
74 /* CLOSID 0 is always reserved for the default group */
75 closid_free_map &= ~1;
76}
77
0fc5d206 78static int closid_alloc(void)
60cf5e10
FY
79{
80 int closid = ffs(closid_free_map);
81
82 if (closid == 0)
83 return -ENOSPC;
84 closid--;
85 closid_free_map &= ~(1 << closid);
86
87 return closid;
88}
89
90static void closid_free(int closid)
91{
92 closid_free_map |= 1 << closid;
93}
94
4e978d06
FY
95/* set uid and gid of rdtgroup dirs and files to that of the creator */
96static int rdtgroup_kn_set_ugid(struct kernfs_node *kn)
97{
98 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
99 .ia_uid = current_fsuid(),
100 .ia_gid = current_fsgid(), };
101
102 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
103 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
104 return 0;
105
106 return kernfs_setattr(kn, &iattr);
107}
108
109static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft)
110{
111 struct kernfs_node *kn;
112 int ret;
113
114 kn = __kernfs_create_file(parent_kn, rft->name, rft->mode,
115 0, rft->kf_ops, rft, NULL, NULL);
116 if (IS_ERR(kn))
117 return PTR_ERR(kn);
118
119 ret = rdtgroup_kn_set_ugid(kn);
120 if (ret) {
121 kernfs_remove(kn);
122 return ret;
123 }
124
125 return 0;
126}
127
4e978d06
FY
128static int rdtgroup_seqfile_show(struct seq_file *m, void *arg)
129{
130 struct kernfs_open_file *of = m->private;
131 struct rftype *rft = of->kn->priv;
132
133 if (rft->seq_show)
134 return rft->seq_show(of, m, arg);
135 return 0;
136}
137
138static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf,
139 size_t nbytes, loff_t off)
140{
141 struct rftype *rft = of->kn->priv;
142
143 if (rft->write)
144 return rft->write(of, buf, nbytes, off);
145
146 return -EINVAL;
147}
148
149static struct kernfs_ops rdtgroup_kf_single_ops = {
150 .atomic_write_len = PAGE_SIZE,
151 .write = rdtgroup_file_write,
152 .seq_show = rdtgroup_seqfile_show,
153};
154
4ffa3c97
JO
155static bool is_cpu_list(struct kernfs_open_file *of)
156{
157 struct rftype *rft = of->kn->priv;
158
159 return rft->flags & RFTYPE_FLAGS_CPUS_LIST;
160}
161
12e0110c
TL
162static int rdtgroup_cpus_show(struct kernfs_open_file *of,
163 struct seq_file *s, void *v)
164{
165 struct rdtgroup *rdtgrp;
166 int ret = 0;
167
168 rdtgrp = rdtgroup_kn_lock_live(of->kn);
169
4ffa3c97
JO
170 if (rdtgrp) {
171 seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n",
172 cpumask_pr_args(&rdtgrp->cpu_mask));
173 } else {
12e0110c 174 ret = -ENOENT;
4ffa3c97 175 }
12e0110c
TL
176 rdtgroup_kn_unlock(of->kn);
177
178 return ret;
179}
180
f4107702
FY
181/*
182 * This is safe against intel_rdt_sched_in() called from __switch_to()
183 * because __switch_to() is executed with interrupts disabled. A local call
0efc89be 184 * from rdt_update_closid() is proteced against __switch_to() because
f4107702
FY
185 * preemption is disabled.
186 */
0efc89be 187static void rdt_update_cpu_closid(void *closid)
f4107702 188{
0efc89be
FY
189 if (closid)
190 this_cpu_write(cpu_closid, *(int *)closid);
f4107702
FY
191 /*
192 * We cannot unconditionally write the MSR because the current
193 * executing task might have its own closid selected. Just reuse
194 * the context switch code.
195 */
196 intel_rdt_sched_in();
197}
198
0efc89be
FY
199/*
200 * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
201 *
202 * Per task closids must have been set up before calling this function.
203 *
204 * The per cpu closids are updated with the smp function call, when @closid
205 * is not NULL. If @closid is NULL then all affected percpu closids must
206 * have been set up before calling this function.
207 */
208static void
209rdt_update_closid(const struct cpumask *cpu_mask, int *closid)
f4107702
FY
210{
211 int cpu = get_cpu();
212
213 if (cpumask_test_cpu(cpu, cpu_mask))
0efc89be
FY
214 rdt_update_cpu_closid(closid);
215 smp_call_function_many(cpu_mask, rdt_update_cpu_closid, closid, 1);
f4107702
FY
216 put_cpu();
217}
218
12e0110c
TL
219static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
220 char *buf, size_t nbytes, loff_t off)
221{
222 cpumask_var_t tmpmask, newmask;
223 struct rdtgroup *rdtgrp, *r;
f4107702 224 int ret;
12e0110c
TL
225
226 if (!buf)
227 return -EINVAL;
228
229 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
230 return -ENOMEM;
231 if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) {
232 free_cpumask_var(tmpmask);
233 return -ENOMEM;
234 }
a2584e1d 235
12e0110c
TL
236 rdtgrp = rdtgroup_kn_lock_live(of->kn);
237 if (!rdtgrp) {
238 ret = -ENOENT;
239 goto unlock;
240 }
241
4ffa3c97
JO
242 if (is_cpu_list(of))
243 ret = cpulist_parse(buf, newmask);
244 else
245 ret = cpumask_parse(buf, newmask);
246
12e0110c
TL
247 if (ret)
248 goto unlock;
249
12e0110c
TL
250 /* check that user didn't specify any offline cpus */
251 cpumask_andnot(tmpmask, newmask, cpu_online_mask);
252 if (cpumask_weight(tmpmask)) {
253 ret = -EINVAL;
a2584e1d 254 goto unlock;
12e0110c
TL
255 }
256
257 /* Check whether cpus are dropped from this group */
258 cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask);
259 if (cpumask_weight(tmpmask)) {
260 /* Can't drop from default group */
261 if (rdtgrp == &rdtgroup_default) {
262 ret = -EINVAL;
a2584e1d 263 goto unlock;
12e0110c
TL
264 }
265 /* Give any dropped cpus to rdtgroup_default */
266 cpumask_or(&rdtgroup_default.cpu_mask,
267 &rdtgroup_default.cpu_mask, tmpmask);
0efc89be 268 rdt_update_closid(tmpmask, &rdtgroup_default.closid);
12e0110c
TL
269 }
270
271 /*
272 * If we added cpus, remove them from previous group that owned them
273 * and update per-cpu closid
274 */
275 cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask);
276 if (cpumask_weight(tmpmask)) {
277 list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) {
278 if (r == rdtgrp)
279 continue;
280 cpumask_andnot(&r->cpu_mask, &r->cpu_mask, tmpmask);
281 }
0efc89be 282 rdt_update_closid(tmpmask, &rdtgrp->closid);
12e0110c
TL
283 }
284
285 /* Done pushing/pulling - update this group with new mask */
286 cpumask_copy(&rdtgrp->cpu_mask, newmask);
287
12e0110c
TL
288unlock:
289 rdtgroup_kn_unlock(of->kn);
290 free_cpumask_var(tmpmask);
291 free_cpumask_var(newmask);
292
293 return ret ?: nbytes;
294}
295
e02737d5
FY
296struct task_move_callback {
297 struct callback_head work;
298 struct rdtgroup *rdtgrp;
299};
300
301static void move_myself(struct callback_head *head)
302{
303 struct task_move_callback *callback;
304 struct rdtgroup *rdtgrp;
305
306 callback = container_of(head, struct task_move_callback, work);
307 rdtgrp = callback->rdtgrp;
308
309 /*
310 * If resource group was deleted before this task work callback
311 * was invoked, then assign the task to root group and free the
312 * resource group.
313 */
314 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
315 (rdtgrp->flags & RDT_DELETED)) {
316 current->closid = 0;
317 kfree(rdtgrp);
318 }
319
74fcdae1 320 preempt_disable();
4f341a5e
FY
321 /* update PQR_ASSOC MSR to make resource group go into effect */
322 intel_rdt_sched_in();
74fcdae1 323 preempt_enable();
4f341a5e 324
e02737d5
FY
325 kfree(callback);
326}
327
328static int __rdtgroup_move_task(struct task_struct *tsk,
329 struct rdtgroup *rdtgrp)
330{
331 struct task_move_callback *callback;
332 int ret;
333
334 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
335 if (!callback)
336 return -ENOMEM;
337 callback->work.func = move_myself;
338 callback->rdtgrp = rdtgrp;
339
340 /*
341 * Take a refcount, so rdtgrp cannot be freed before the
342 * callback has been invoked.
343 */
344 atomic_inc(&rdtgrp->waitcount);
345 ret = task_work_add(tsk, &callback->work, true);
346 if (ret) {
347 /*
348 * Task is exiting. Drop the refcount and free the callback.
349 * No need to check the refcount as the group cannot be
350 * deleted before the write function unlocks rdtgroup_mutex.
351 */
352 atomic_dec(&rdtgrp->waitcount);
353 kfree(callback);
354 } else {
355 tsk->closid = rdtgrp->closid;
356 }
357 return ret;
358}
359
360static int rdtgroup_task_write_permission(struct task_struct *task,
361 struct kernfs_open_file *of)
362{
363 const struct cred *tcred = get_task_cred(task);
364 const struct cred *cred = current_cred();
365 int ret = 0;
366
367 /*
368 * Even if we're attaching all tasks in the thread group, we only
369 * need to check permissions on one of them.
370 */
371 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
372 !uid_eq(cred->euid, tcred->uid) &&
373 !uid_eq(cred->euid, tcred->suid))
374 ret = -EPERM;
375
376 put_cred(tcred);
377 return ret;
378}
379
380static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp,
381 struct kernfs_open_file *of)
382{
383 struct task_struct *tsk;
384 int ret;
385
386 rcu_read_lock();
387 if (pid) {
388 tsk = find_task_by_vpid(pid);
389 if (!tsk) {
390 rcu_read_unlock();
391 return -ESRCH;
392 }
393 } else {
394 tsk = current;
395 }
396
397 get_task_struct(tsk);
398 rcu_read_unlock();
399
400 ret = rdtgroup_task_write_permission(tsk, of);
401 if (!ret)
402 ret = __rdtgroup_move_task(tsk, rdtgrp);
403
404 put_task_struct(tsk);
405 return ret;
406}
407
408static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of,
409 char *buf, size_t nbytes, loff_t off)
410{
411 struct rdtgroup *rdtgrp;
412 int ret = 0;
413 pid_t pid;
414
415 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
416 return -EINVAL;
417 rdtgrp = rdtgroup_kn_lock_live(of->kn);
418
419 if (rdtgrp)
420 ret = rdtgroup_move_task(pid, rdtgrp, of);
421 else
422 ret = -ENOENT;
423
424 rdtgroup_kn_unlock(of->kn);
425
426 return ret ?: nbytes;
427}
428
429static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
430{
431 struct task_struct *p, *t;
432
433 rcu_read_lock();
434 for_each_process_thread(p, t) {
435 if (t->closid == r->closid)
436 seq_printf(s, "%d\n", t->pid);
437 }
438 rcu_read_unlock();
439}
440
441static int rdtgroup_tasks_show(struct kernfs_open_file *of,
442 struct seq_file *s, void *v)
443{
444 struct rdtgroup *rdtgrp;
445 int ret = 0;
446
447 rdtgrp = rdtgroup_kn_lock_live(of->kn);
448 if (rdtgrp)
449 show_rdt_tasks(rdtgrp, s);
450 else
451 ret = -ENOENT;
452 rdtgroup_kn_unlock(of->kn);
453
454 return ret;
455}
456
4e978d06
FY
457static int rdt_num_closids_show(struct kernfs_open_file *of,
458 struct seq_file *seq, void *v)
459{
460 struct rdt_resource *r = of->kn->parent->priv;
461
462 seq_printf(seq, "%d\n", r->num_closid);
4e978d06
FY
463 return 0;
464}
465
2545e9f5 466static int rdt_default_ctrl_show(struct kernfs_open_file *of,
4e978d06
FY
467 struct seq_file *seq, void *v)
468{
469 struct rdt_resource *r = of->kn->parent->priv;
470
2545e9f5 471 seq_printf(seq, "%x\n", r->default_ctrl);
4e978d06
FY
472 return 0;
473}
474
53a114a6
SL
475static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
476 struct seq_file *seq, void *v)
477{
478 struct rdt_resource *r = of->kn->parent->priv;
479
d3e11b4d 480 seq_printf(seq, "%u\n", r->cache.min_cbm_bits);
db69ef65
VS
481 return 0;
482}
483
484static int rdt_min_bw_show(struct kernfs_open_file *of,
485 struct seq_file *seq, void *v)
486{
487 struct rdt_resource *r = of->kn->parent->priv;
53a114a6 488
db69ef65
VS
489 seq_printf(seq, "%u\n", r->membw.min_bw);
490 return 0;
491}
492
493static int rdt_bw_gran_show(struct kernfs_open_file *of,
494 struct seq_file *seq, void *v)
495{
496 struct rdt_resource *r = of->kn->parent->priv;
497
498 seq_printf(seq, "%u\n", r->membw.bw_gran);
499 return 0;
500}
501
502static int rdt_delay_linear_show(struct kernfs_open_file *of,
503 struct seq_file *seq, void *v)
504{
505 struct rdt_resource *r = of->kn->parent->priv;
506
507 seq_printf(seq, "%u\n", r->membw.delay_linear);
53a114a6
SL
508 return 0;
509}
510
4e978d06 511/* rdtgroup information files for one cache resource. */
5ae32bbc 512static struct rftype res_common_files[] = {
4e978d06
FY
513 {
514 .name = "num_closids",
515 .mode = 0444,
516 .kf_ops = &rdtgroup_kf_single_ops,
517 .seq_show = rdt_num_closids_show,
5ae32bbc 518 .fflags = RF_CTRL_INFO,
4e978d06
FY
519 },
520 {
521 .name = "cbm_mask",
522 .mode = 0444,
523 .kf_ops = &rdtgroup_kf_single_ops,
2545e9f5 524 .seq_show = rdt_default_ctrl_show,
5ae32bbc 525 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
4e978d06 526 },
53a114a6
SL
527 {
528 .name = "min_cbm_bits",
529 .mode = 0444,
530 .kf_ops = &rdtgroup_kf_single_ops,
531 .seq_show = rdt_min_cbm_bits_show,
5ae32bbc 532 .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE,
db69ef65
VS
533 },
534 {
535 .name = "min_bandwidth",
536 .mode = 0444,
537 .kf_ops = &rdtgroup_kf_single_ops,
538 .seq_show = rdt_min_bw_show,
5ae32bbc 539 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
db69ef65
VS
540 },
541 {
542 .name = "bandwidth_gran",
543 .mode = 0444,
544 .kf_ops = &rdtgroup_kf_single_ops,
545 .seq_show = rdt_bw_gran_show,
5ae32bbc 546 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
db69ef65
VS
547 },
548 {
549 .name = "delay_linear",
550 .mode = 0444,
551 .kf_ops = &rdtgroup_kf_single_ops,
552 .seq_show = rdt_delay_linear_show,
5ae32bbc
TL
553 .fflags = RF_CTRL_INFO | RFTYPE_RES_MB,
554 },
555 {
556 .name = "cpus",
557 .mode = 0644,
558 .kf_ops = &rdtgroup_kf_single_ops,
559 .write = rdtgroup_cpus_write,
560 .seq_show = rdtgroup_cpus_show,
561 .fflags = RFTYPE_BASE,
562 },
563 {
564 .name = "cpus_list",
565 .mode = 0644,
566 .kf_ops = &rdtgroup_kf_single_ops,
567 .write = rdtgroup_cpus_write,
568 .seq_show = rdtgroup_cpus_show,
569 .flags = RFTYPE_FLAGS_CPUS_LIST,
570 .fflags = RFTYPE_BASE,
571 },
572 {
573 .name = "tasks",
574 .mode = 0644,
575 .kf_ops = &rdtgroup_kf_single_ops,
576 .write = rdtgroup_tasks_write,
577 .seq_show = rdtgroup_tasks_show,
578 .fflags = RFTYPE_BASE,
579 },
580 {
581 .name = "schemata",
582 .mode = 0644,
583 .kf_ops = &rdtgroup_kf_single_ops,
584 .write = rdtgroup_schemata_write,
585 .seq_show = rdtgroup_schemata_show,
586 .fflags = RF_CTRL_BASE,
db69ef65
VS
587 },
588};
589
5ae32bbc 590static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
db69ef65 591{
5ae32bbc
TL
592 struct rftype *rfts, *rft;
593 int ret, len;
594
595 rfts = res_common_files;
596 len = ARRAY_SIZE(res_common_files);
597
598 lockdep_assert_held(&rdtgroup_mutex);
599
600 for (rft = rfts; rft < rfts + len; rft++) {
601 if ((fflags & rft->fflags) == rft->fflags) {
602 ret = rdtgroup_add_file(kn, rft);
603 if (ret)
604 goto error;
605 }
606 }
607
608 return 0;
609error:
610 pr_warn("Failed to add %s, err=%d\n", rft->name, ret);
611 while (--rft >= rfts) {
612 if ((fflags & rft->fflags) == rft->fflags)
613 kernfs_remove_by_name(kn, rft->name);
614 }
615 return ret;
db69ef65
VS
616}
617
5ae32bbc
TL
618static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name,
619 unsigned long fflags)
6a507a6a 620{
5ae32bbc
TL
621 struct kernfs_node *kn_subdir;
622 int ret;
623
624 kn_subdir = kernfs_create_dir(kn_info, name,
625 kn_info->mode, r);
626 if (IS_ERR(kn_subdir))
627 return PTR_ERR(kn_subdir);
628
629 kernfs_get(kn_subdir);
630 ret = rdtgroup_kn_set_ugid(kn_subdir);
631 if (ret)
632 return ret;
633
634 ret = rdtgroup_add_files(kn_subdir, fflags);
635 if (!ret)
636 kernfs_activate(kn_subdir);
637
638 return ret;
6a507a6a
VS
639}
640
4e978d06
FY
641static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
642{
4e978d06 643 struct rdt_resource *r;
5ae32bbc
TL
644 unsigned long fflags;
645 int ret;
4e978d06
FY
646
647 /* create the directory */
648 kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL);
649 if (IS_ERR(kn_info))
650 return PTR_ERR(kn_info);
651 kernfs_get(kn_info);
652
26017611 653 for_each_alloc_enabled_rdt_resource(r) {
5ae32bbc
TL
654 fflags = r->fflags | RF_CTRL_INFO;
655 ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags);
4e978d06
FY
656 if (ret)
657 goto out_destroy;
4e978d06 658 }
4e978d06
FY
659 /*
660 * This extra ref will be put in kernfs_remove() and guarantees
661 * that @rdtgrp->kn is always accessible.
662 */
663 kernfs_get(kn_info);
664
665 ret = rdtgroup_kn_set_ugid(kn_info);
666 if (ret)
667 goto out_destroy;
668
669 kernfs_activate(kn_info);
670
671 return 0;
672
673out_destroy:
674 kernfs_remove(kn_info);
675 return ret;
676}
677
5ff193fb
FY
678static void l3_qos_cfg_update(void *arg)
679{
680 bool *enable = arg;
681
682 wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL);
683}
684
685static int set_l3_qos_cfg(struct rdt_resource *r, bool enable)
686{
687 cpumask_var_t cpu_mask;
688 struct rdt_domain *d;
689 int cpu;
690
691 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
692 return -ENOMEM;
693
694 list_for_each_entry(d, &r->domains, list) {
695 /* Pick one CPU from each domain instance to update MSR */
696 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
697 }
698 cpu = get_cpu();
699 /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */
700 if (cpumask_test_cpu(cpu, cpu_mask))
701 l3_qos_cfg_update(&enable);
702 /* Update QOS_CFG MSR on all other cpus in cpu_mask. */
703 smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1);
704 put_cpu();
705
706 free_cpumask_var(cpu_mask);
707
708 return 0;
709}
710
711static int cdp_enable(void)
712{
713 struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA];
714 struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE];
715 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
716 int ret;
717
26017611
VS
718 if (!r_l3->alloc_capable || !r_l3data->alloc_capable ||
719 !r_l3code->alloc_capable)
5ff193fb
FY
720 return -EINVAL;
721
722 ret = set_l3_qos_cfg(r_l3, true);
723 if (!ret) {
26017611
VS
724 r_l3->alloc_enabled = false;
725 r_l3data->alloc_enabled = true;
726 r_l3code->alloc_enabled = true;
5ff193fb
FY
727 }
728 return ret;
729}
730
731static void cdp_disable(void)
732{
733 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
734
26017611 735 r->alloc_enabled = r->alloc_capable;
5ff193fb 736
26017611
VS
737 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) {
738 rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled = false;
739 rdt_resources_all[RDT_RESOURCE_L3CODE].alloc_enabled = false;
5ff193fb
FY
740 set_l3_qos_cfg(r, false);
741 }
742}
743
744static int parse_rdtgroupfs_options(char *data)
745{
746 char *token, *o = data;
747 int ret = 0;
748
749 while ((token = strsep(&o, ",")) != NULL) {
750 if (!*token)
751 return -EINVAL;
752
753 if (!strcmp(token, "cdp"))
754 ret = cdp_enable();
755 }
756
757 return ret;
758}
759
60cf5e10
FY
760/*
761 * We don't allow rdtgroup directories to be created anywhere
762 * except the root directory. Thus when looking for the rdtgroup
763 * structure for a kernfs node we are either looking at a directory,
764 * in which case the rdtgroup structure is pointed at by the "priv"
765 * field, otherwise we have a file, and need only look to the parent
766 * to find the rdtgroup.
767 */
768static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)
769{
f57b3087
FY
770 if (kernfs_type(kn) == KERNFS_DIR) {
771 /*
772 * All the resource directories use "kn->priv"
773 * to point to the "struct rdtgroup" for the
774 * resource. "info" and its subdirectories don't
775 * have rdtgroup structures, so return NULL here.
776 */
777 if (kn == kn_info || kn->parent == kn_info)
778 return NULL;
779 else
780 return kn->priv;
781 } else {
60cf5e10 782 return kn->parent->priv;
f57b3087 783 }
60cf5e10
FY
784}
785
786struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn)
787{
788 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
789
f57b3087
FY
790 if (!rdtgrp)
791 return NULL;
792
60cf5e10
FY
793 atomic_inc(&rdtgrp->waitcount);
794 kernfs_break_active_protection(kn);
795
796 mutex_lock(&rdtgroup_mutex);
797
798 /* Was this group deleted while we waited? */
799 if (rdtgrp->flags & RDT_DELETED)
800 return NULL;
801
802 return rdtgrp;
803}
804
805void rdtgroup_kn_unlock(struct kernfs_node *kn)
806{
807 struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn);
808
f57b3087
FY
809 if (!rdtgrp)
810 return;
811
60cf5e10
FY
812 mutex_unlock(&rdtgroup_mutex);
813
814 if (atomic_dec_and_test(&rdtgrp->waitcount) &&
815 (rdtgrp->flags & RDT_DELETED)) {
816 kernfs_unbreak_active_protection(kn);
49ec8f5b 817 kernfs_put(rdtgrp->kn);
60cf5e10
FY
818 kfree(rdtgrp);
819 } else {
820 kernfs_unbreak_active_protection(kn);
821 }
822}
823
5ff193fb
FY
824static struct dentry *rdt_mount(struct file_system_type *fs_type,
825 int flags, const char *unused_dev_name,
826 void *data)
827{
828 struct dentry *dentry;
829 int ret;
830
831 mutex_lock(&rdtgroup_mutex);
832 /*
833 * resctrl file system can only be mounted once.
834 */
26017611 835 if (static_branch_unlikely(&rdt_alloc_enable_key)) {
5ff193fb
FY
836 dentry = ERR_PTR(-EBUSY);
837 goto out;
838 }
839
840 ret = parse_rdtgroupfs_options(data);
841 if (ret) {
842 dentry = ERR_PTR(ret);
843 goto out_cdp;
844 }
845
60cf5e10
FY
846 closid_init();
847
4e978d06 848 ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
7bff0af5
SL
849 if (ret) {
850 dentry = ERR_PTR(ret);
4e978d06 851 goto out_cdp;
7bff0af5 852 }
4e978d06 853
5ff193fb
FY
854 dentry = kernfs_mount(fs_type, flags, rdt_root,
855 RDTGROUP_SUPER_MAGIC, NULL);
856 if (IS_ERR(dentry))
79298acc 857 goto out_destroy;
5ff193fb 858
26017611 859 static_branch_enable(&rdt_alloc_enable_key);
5ff193fb
FY
860 goto out;
861
79298acc
VS
862out_destroy:
863 kernfs_remove(kn_info);
5ff193fb
FY
864out_cdp:
865 cdp_disable();
866out:
867 mutex_unlock(&rdtgroup_mutex);
868
869 return dentry;
870}
871
2545e9f5 872static int reset_all_ctrls(struct rdt_resource *r)
5ff193fb
FY
873{
874 struct msr_param msr_param;
875 cpumask_var_t cpu_mask;
876 struct rdt_domain *d;
877 int i, cpu;
878
879 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
880 return -ENOMEM;
881
882 msr_param.res = r;
883 msr_param.low = 0;
884 msr_param.high = r->num_closid;
885
886 /*
887 * Disable resource control for this resource by setting all
888 * CBMs in all domains to the maximum mask value. Pick one CPU
889 * from each domain to update the MSRs below.
890 */
891 list_for_each_entry(d, &r->domains, list) {
892 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
893
894 for (i = 0; i < r->num_closid; i++)
2545e9f5 895 d->ctrl_val[i] = r->default_ctrl;
5ff193fb
FY
896 }
897 cpu = get_cpu();
898 /* Update CBM on this cpu if it's in cpu_mask. */
899 if (cpumask_test_cpu(cpu, cpu_mask))
2545e9f5 900 rdt_ctrl_update(&msr_param);
5ff193fb 901 /* Update CBM on all other cpus in cpu_mask. */
2545e9f5 902 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
5ff193fb
FY
903 put_cpu();
904
905 free_cpumask_var(cpu_mask);
906
907 return 0;
908}
909
4e978d06 910/*
0efc89be
FY
911 * Move tasks from one to the other group. If @from is NULL, then all tasks
912 * in the systems are moved unconditionally (used for teardown).
913 *
914 * If @mask is not NULL the cpus on which moved tasks are running are set
915 * in that mask so the update smp function call is restricted to affected
916 * cpus.
4e978d06 917 */
0efc89be
FY
918static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
919 struct cpumask *mask)
4e978d06 920{
e02737d5
FY
921 struct task_struct *p, *t;
922
e02737d5 923 read_lock(&tasklist_lock);
0efc89be
FY
924 for_each_process_thread(p, t) {
925 if (!from || t->closid == from->closid) {
926 t->closid = to->closid;
927#ifdef CONFIG_SMP
928 /*
929 * This is safe on x86 w/o barriers as the ordering
930 * of writing to task_cpu() and t->on_cpu is
931 * reverse to the reading here. The detection is
932 * inaccurate as tasks might move or schedule
933 * before the smp function call takes place. In
934 * such a case the function call is pointless, but
935 * there is no other side effect.
936 */
937 if (mask && t->on_cpu)
938 cpumask_set_cpu(task_cpu(t), mask);
939#endif
940 }
941 }
e02737d5 942 read_unlock(&tasklist_lock);
0efc89be
FY
943}
944
945/*
946 * Forcibly remove all of subdirectories under root.
947 */
948static void rmdir_all_sub(void)
949{
950 struct rdtgroup *rdtgrp, *tmp;
951
952 /* Move all tasks to the default resource group */
953 rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
60cf5e10 954
60cf5e10
FY
955 list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
956 /* Remove each rdtgroup other than root */
957 if (rdtgrp == &rdtgroup_default)
958 continue;
c7cc0cc1
FY
959
960 /*
961 * Give any CPUs back to the default group. We cannot copy
962 * cpu_online_mask because a CPU might have executed the
963 * offline callback already, but is still marked online.
964 */
965 cpumask_or(&rdtgroup_default.cpu_mask,
966 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
967
60cf5e10
FY
968 kernfs_remove(rdtgrp->kn);
969 list_del(&rdtgrp->rdtgroup_list);
970 kfree(rdtgrp);
971 }
0efc89be
FY
972 /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
973 get_online_cpus();
974 rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid);
975 put_online_cpus();
976
4e978d06
FY
977 kernfs_remove(kn_info);
978}
979
5ff193fb
FY
980static void rdt_kill_sb(struct super_block *sb)
981{
982 struct rdt_resource *r;
983
984 mutex_lock(&rdtgroup_mutex);
985
986 /*Put everything back to default values. */
26017611 987 for_each_alloc_enabled_rdt_resource(r)
2545e9f5 988 reset_all_ctrls(r);
5ff193fb 989 cdp_disable();
4e978d06 990 rmdir_all_sub();
26017611 991 static_branch_disable(&rdt_alloc_enable_key);
5ff193fb
FY
992 kernfs_kill_sb(sb);
993 mutex_unlock(&rdtgroup_mutex);
994}
995
996static struct file_system_type rdt_fs_type = {
997 .name = "resctrl",
998 .mount = rdt_mount,
999 .kill_sb = rdt_kill_sb,
1000};
1001
60cf5e10
FY
1002static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
1003 umode_t mode)
1004{
1005 struct rdtgroup *parent, *rdtgrp;
1006 struct kernfs_node *kn;
1007 int ret, closid;
1008
1009 /* Only allow mkdir in the root directory */
1010 if (parent_kn != rdtgroup_default.kn)
1011 return -EPERM;
1012
1013 /* Do not accept '\n' to avoid unparsable situation. */
1014 if (strchr(name, '\n'))
1015 return -EINVAL;
1016
1017 parent = rdtgroup_kn_lock_live(parent_kn);
1018 if (!parent) {
1019 ret = -ENODEV;
1020 goto out_unlock;
1021 }
1022
1023 ret = closid_alloc();
1024 if (ret < 0)
1025 goto out_unlock;
1026 closid = ret;
1027
1028 /* allocate the rdtgroup. */
1029 rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL);
1030 if (!rdtgrp) {
1031 ret = -ENOSPC;
1032 goto out_closid_free;
1033 }
1034 rdtgrp->closid = closid;
1035 list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups);
1036
1037 /* kernfs creates the directory for rdtgrp */
1038 kn = kernfs_create_dir(parent->kn, name, mode, rdtgrp);
1039 if (IS_ERR(kn)) {
1040 ret = PTR_ERR(kn);
1041 goto out_cancel_ref;
1042 }
1043 rdtgrp->kn = kn;
1044
1045 /*
1046 * kernfs_remove() will drop the reference count on "kn" which
1047 * will free it. But we still need it to stick around for the
1048 * rdtgroup_kn_unlock(kn} call below. Take one extra reference
1049 * here, which will be dropped inside rdtgroup_kn_unlock().
1050 */
1051 kernfs_get(kn);
1052
1053 ret = rdtgroup_kn_set_ugid(kn);
1054 if (ret)
1055 goto out_destroy;
1056
5ae32bbc 1057 ret = rdtgroup_add_files(kn, RF_CTRL_BASE);
12e0110c
TL
1058 if (ret)
1059 goto out_destroy;
1060
60cf5e10
FY
1061 kernfs_activate(kn);
1062
1063 ret = 0;
1064 goto out_unlock;
1065
1066out_destroy:
1067 kernfs_remove(rdtgrp->kn);
1068out_cancel_ref:
1069 list_del(&rdtgrp->rdtgroup_list);
1070 kfree(rdtgrp);
1071out_closid_free:
1072 closid_free(closid);
1073out_unlock:
1074 rdtgroup_kn_unlock(parent_kn);
1075 return ret;
1076}
1077
1078static int rdtgroup_rmdir(struct kernfs_node *kn)
1079{
0efc89be 1080 int ret, cpu, closid = rdtgroup_default.closid;
60cf5e10 1081 struct rdtgroup *rdtgrp;
0efc89be
FY
1082 cpumask_var_t tmpmask;
1083
1084 if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
1085 return -ENOMEM;
60cf5e10
FY
1086
1087 rdtgrp = rdtgroup_kn_lock_live(kn);
1088 if (!rdtgrp) {
0efc89be
FY
1089 ret = -EPERM;
1090 goto out;
60cf5e10
FY
1091 }
1092
e02737d5 1093 /* Give any tasks back to the default group */
0efc89be 1094 rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask);
e02737d5 1095
12e0110c
TL
1096 /* Give any CPUs back to the default group */
1097 cpumask_or(&rdtgroup_default.cpu_mask,
1098 &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
0efc89be
FY
1099
1100 /* Update per cpu closid of the moved CPUs first */
1101 for_each_cpu(cpu, &rdtgrp->cpu_mask)
1102 per_cpu(cpu_closid, cpu) = closid;
1103 /*
1104 * Update the MSR on moved CPUs and CPUs which have moved
1105 * task running on them.
1106 */
1107 cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
1108 rdt_update_closid(tmpmask, NULL);
12e0110c 1109
60cf5e10
FY
1110 rdtgrp->flags = RDT_DELETED;
1111 closid_free(rdtgrp->closid);
1112 list_del(&rdtgrp->rdtgroup_list);
1113
1114 /*
1115 * one extra hold on this, will drop when we kfree(rdtgrp)
1116 * in rdtgroup_kn_unlock()
1117 */
1118 kernfs_get(kn);
1119 kernfs_remove(rdtgrp->kn);
0efc89be
FY
1120 ret = 0;
1121out:
60cf5e10 1122 rdtgroup_kn_unlock(kn);
0efc89be
FY
1123 free_cpumask_var(tmpmask);
1124 return ret;
60cf5e10
FY
1125}
1126
76ae054c
SL
1127static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
1128{
26017611 1129 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled)
76ae054c
SL
1130 seq_puts(seq, ",cdp");
1131 return 0;
1132}
1133
5ff193fb 1134static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = {
76ae054c
SL
1135 .mkdir = rdtgroup_mkdir,
1136 .rmdir = rdtgroup_rmdir,
1137 .show_options = rdtgroup_show_options,
5ff193fb
FY
1138};
1139
1140static int __init rdtgroup_setup_root(void)
1141{
12e0110c
TL
1142 int ret;
1143
5ff193fb
FY
1144 rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops,
1145 KERNFS_ROOT_CREATE_DEACTIVATED,
1146 &rdtgroup_default);
1147 if (IS_ERR(rdt_root))
1148 return PTR_ERR(rdt_root);
1149
1150 mutex_lock(&rdtgroup_mutex);
1151
1152 rdtgroup_default.closid = 0;
1153 list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups);
1154
5ae32bbc 1155 ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE);
12e0110c
TL
1156 if (ret) {
1157 kernfs_destroy_root(rdt_root);
1158 goto out;
1159 }
1160
5ff193fb
FY
1161 rdtgroup_default.kn = rdt_root->kn;
1162 kernfs_activate(rdtgroup_default.kn);
1163
12e0110c 1164out:
5ff193fb
FY
1165 mutex_unlock(&rdtgroup_mutex);
1166
12e0110c 1167 return ret;
5ff193fb
FY
1168}
1169
1170/*
1171 * rdtgroup_init - rdtgroup initialization
1172 *
1173 * Setup resctrl file system including set up root, create mount point,
1174 * register rdtgroup filesystem, and initialize files under root directory.
1175 *
1176 * Return: 0 on success or -errno
1177 */
1178int __init rdtgroup_init(void)
1179{
1180 int ret = 0;
1181
1182 ret = rdtgroup_setup_root();
1183 if (ret)
1184 return ret;
1185
1186 ret = sysfs_create_mount_point(fs_kobj, "resctrl");
1187 if (ret)
1188 goto cleanup_root;
1189
1190 ret = register_filesystem(&rdt_fs_type);
1191 if (ret)
1192 goto cleanup_mountpoint;
1193
1194 return 0;
1195
1196cleanup_mountpoint:
1197 sysfs_remove_mount_point(fs_kobj, "resctrl");
1198cleanup_root:
1199 kernfs_destroy_root(rdt_root);
1200
1201 return ret;
1202}