]>
Commit | Line | Data |
---|---|---|
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 | 38 | DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key); |
0fc5d206 | 39 | static struct kernfs_root *rdt_root; |
5ff193fb FY |
40 | struct rdtgroup rdtgroup_default; |
41 | LIST_HEAD(rdt_all_groups); | |
42 | ||
4e978d06 FY |
43 | /* Kernel fs node for "info" directory under root */ |
44 | static 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 | */ | |
61 | static int closid_free_map; | |
62 | ||
63 | static 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 | 78 | static int closid_alloc(void) |
60cf5e10 | 79 | { |
703c3837 | 80 | u32 closid = ffs(closid_free_map); |
60cf5e10 FY |
81 | |
82 | if (closid == 0) | |
83 | return -ENOSPC; | |
84 | closid--; | |
85 | closid_free_map &= ~(1 << closid); | |
86 | ||
87 | return closid; | |
88 | } | |
89 | ||
90 | static 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 */ |
96 | static 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 | ||
109 | static 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 |
128 | static 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 | ||
138 | static 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 | ||
149 | static 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 |
155 | static 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 |
162 | static 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 | 187 | static 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 | */ | |
208 | static void | |
209 | rdt_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 |
219 | static 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 |
288 | unlock: |
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 |
296 | struct task_move_callback { |
297 | struct callback_head work; | |
298 | struct rdtgroup *rdtgrp; | |
299 | }; | |
300 | ||
301 | static 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 | ||
328 | static 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 | ||
360 | static 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 | ||
380 | static 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 | ||
408 | static 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 | ||
429 | static 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 | ||
441 | static 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 |
457 | static 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 | 466 | static 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 |
475 | static 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 | ||
484 | static 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 | ||
a9a0c771 VS |
493 | static int rdt_num_rmids_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, "%d\n", r->num_rmid); | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
503 | static int rdt_mon_features_show(struct kernfs_open_file *of, | |
504 | struct seq_file *seq, void *v) | |
505 | { | |
506 | struct rdt_resource *r = of->kn->parent->priv; | |
507 | struct mon_evt *mevt; | |
508 | ||
509 | list_for_each_entry(mevt, &r->evt_list, list) | |
510 | seq_printf(seq, "%s\n", mevt->name); | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
db69ef65 VS |
515 | static int rdt_bw_gran_show(struct kernfs_open_file *of, |
516 | struct seq_file *seq, void *v) | |
517 | { | |
518 | struct rdt_resource *r = of->kn->parent->priv; | |
519 | ||
520 | seq_printf(seq, "%u\n", r->membw.bw_gran); | |
521 | return 0; | |
522 | } | |
523 | ||
524 | static int rdt_delay_linear_show(struct kernfs_open_file *of, | |
525 | struct seq_file *seq, void *v) | |
526 | { | |
527 | struct rdt_resource *r = of->kn->parent->priv; | |
528 | ||
529 | seq_printf(seq, "%u\n", r->membw.delay_linear); | |
53a114a6 SL |
530 | return 0; |
531 | } | |
532 | ||
a9a0c771 VS |
533 | static int max_threshold_occ_show(struct kernfs_open_file *of, |
534 | struct seq_file *seq, void *v) | |
535 | { | |
536 | struct rdt_resource *r = of->kn->parent->priv; | |
537 | ||
538 | seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale); | |
539 | ||
540 | return 0; | |
541 | } | |
542 | ||
543 | static ssize_t max_threshold_occ_write(struct kernfs_open_file *of, | |
544 | char *buf, size_t nbytes, loff_t off) | |
545 | { | |
546 | struct rdt_resource *r = of->kn->parent->priv; | |
547 | unsigned int bytes; | |
548 | int ret; | |
549 | ||
550 | ret = kstrtouint(buf, 0, &bytes); | |
551 | if (ret) | |
552 | return ret; | |
553 | ||
554 | if (bytes > (boot_cpu_data.x86_cache_size * 1024)) | |
555 | return -EINVAL; | |
556 | ||
557 | intel_cqm_threshold = bytes / r->mon_scale; | |
558 | ||
559 | return ret ?: nbytes; | |
560 | } | |
561 | ||
4e978d06 | 562 | /* rdtgroup information files for one cache resource. */ |
5ae32bbc | 563 | static struct rftype res_common_files[] = { |
4e978d06 FY |
564 | { |
565 | .name = "num_closids", | |
566 | .mode = 0444, | |
567 | .kf_ops = &rdtgroup_kf_single_ops, | |
568 | .seq_show = rdt_num_closids_show, | |
5ae32bbc | 569 | .fflags = RF_CTRL_INFO, |
4e978d06 | 570 | }, |
a9a0c771 VS |
571 | { |
572 | .name = "mon_features", | |
573 | .mode = 0444, | |
574 | .kf_ops = &rdtgroup_kf_single_ops, | |
575 | .seq_show = rdt_mon_features_show, | |
576 | .fflags = RF_MON_INFO, | |
577 | }, | |
578 | { | |
579 | .name = "num_rmids", | |
580 | .mode = 0444, | |
581 | .kf_ops = &rdtgroup_kf_single_ops, | |
582 | .seq_show = rdt_num_rmids_show, | |
583 | .fflags = RF_MON_INFO, | |
584 | }, | |
4e978d06 FY |
585 | { |
586 | .name = "cbm_mask", | |
587 | .mode = 0444, | |
588 | .kf_ops = &rdtgroup_kf_single_ops, | |
2545e9f5 | 589 | .seq_show = rdt_default_ctrl_show, |
5ae32bbc | 590 | .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, |
4e978d06 | 591 | }, |
53a114a6 SL |
592 | { |
593 | .name = "min_cbm_bits", | |
594 | .mode = 0444, | |
595 | .kf_ops = &rdtgroup_kf_single_ops, | |
596 | .seq_show = rdt_min_cbm_bits_show, | |
5ae32bbc | 597 | .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, |
db69ef65 VS |
598 | }, |
599 | { | |
600 | .name = "min_bandwidth", | |
601 | .mode = 0444, | |
602 | .kf_ops = &rdtgroup_kf_single_ops, | |
603 | .seq_show = rdt_min_bw_show, | |
5ae32bbc | 604 | .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, |
db69ef65 VS |
605 | }, |
606 | { | |
607 | .name = "bandwidth_gran", | |
608 | .mode = 0444, | |
609 | .kf_ops = &rdtgroup_kf_single_ops, | |
610 | .seq_show = rdt_bw_gran_show, | |
5ae32bbc | 611 | .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, |
db69ef65 VS |
612 | }, |
613 | { | |
614 | .name = "delay_linear", | |
615 | .mode = 0444, | |
616 | .kf_ops = &rdtgroup_kf_single_ops, | |
617 | .seq_show = rdt_delay_linear_show, | |
5ae32bbc TL |
618 | .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, |
619 | }, | |
a9a0c771 VS |
620 | { |
621 | .name = "max_threshold_occupancy", | |
622 | .mode = 0644, | |
623 | .kf_ops = &rdtgroup_kf_single_ops, | |
624 | .write = max_threshold_occ_write, | |
625 | .seq_show = max_threshold_occ_show, | |
626 | .fflags = RF_MON_INFO | RFTYPE_RES_CACHE, | |
627 | }, | |
5ae32bbc TL |
628 | { |
629 | .name = "cpus", | |
630 | .mode = 0644, | |
631 | .kf_ops = &rdtgroup_kf_single_ops, | |
632 | .write = rdtgroup_cpus_write, | |
633 | .seq_show = rdtgroup_cpus_show, | |
634 | .fflags = RFTYPE_BASE, | |
635 | }, | |
636 | { | |
637 | .name = "cpus_list", | |
638 | .mode = 0644, | |
639 | .kf_ops = &rdtgroup_kf_single_ops, | |
640 | .write = rdtgroup_cpus_write, | |
641 | .seq_show = rdtgroup_cpus_show, | |
642 | .flags = RFTYPE_FLAGS_CPUS_LIST, | |
643 | .fflags = RFTYPE_BASE, | |
644 | }, | |
645 | { | |
646 | .name = "tasks", | |
647 | .mode = 0644, | |
648 | .kf_ops = &rdtgroup_kf_single_ops, | |
649 | .write = rdtgroup_tasks_write, | |
650 | .seq_show = rdtgroup_tasks_show, | |
651 | .fflags = RFTYPE_BASE, | |
652 | }, | |
653 | { | |
654 | .name = "schemata", | |
655 | .mode = 0644, | |
656 | .kf_ops = &rdtgroup_kf_single_ops, | |
657 | .write = rdtgroup_schemata_write, | |
658 | .seq_show = rdtgroup_schemata_show, | |
659 | .fflags = RF_CTRL_BASE, | |
db69ef65 VS |
660 | }, |
661 | }; | |
662 | ||
5ae32bbc | 663 | static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags) |
db69ef65 | 664 | { |
5ae32bbc TL |
665 | struct rftype *rfts, *rft; |
666 | int ret, len; | |
667 | ||
668 | rfts = res_common_files; | |
669 | len = ARRAY_SIZE(res_common_files); | |
670 | ||
671 | lockdep_assert_held(&rdtgroup_mutex); | |
672 | ||
673 | for (rft = rfts; rft < rfts + len; rft++) { | |
674 | if ((fflags & rft->fflags) == rft->fflags) { | |
675 | ret = rdtgroup_add_file(kn, rft); | |
676 | if (ret) | |
677 | goto error; | |
678 | } | |
679 | } | |
680 | ||
681 | return 0; | |
682 | error: | |
683 | pr_warn("Failed to add %s, err=%d\n", rft->name, ret); | |
684 | while (--rft >= rfts) { | |
685 | if ((fflags & rft->fflags) == rft->fflags) | |
686 | kernfs_remove_by_name(kn, rft->name); | |
687 | } | |
688 | return ret; | |
db69ef65 VS |
689 | } |
690 | ||
5ae32bbc TL |
691 | static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name, |
692 | unsigned long fflags) | |
6a507a6a | 693 | { |
5ae32bbc TL |
694 | struct kernfs_node *kn_subdir; |
695 | int ret; | |
696 | ||
697 | kn_subdir = kernfs_create_dir(kn_info, name, | |
698 | kn_info->mode, r); | |
699 | if (IS_ERR(kn_subdir)) | |
700 | return PTR_ERR(kn_subdir); | |
701 | ||
702 | kernfs_get(kn_subdir); | |
703 | ret = rdtgroup_kn_set_ugid(kn_subdir); | |
704 | if (ret) | |
705 | return ret; | |
706 | ||
707 | ret = rdtgroup_add_files(kn_subdir, fflags); | |
708 | if (!ret) | |
709 | kernfs_activate(kn_subdir); | |
710 | ||
711 | return ret; | |
6a507a6a VS |
712 | } |
713 | ||
4e978d06 FY |
714 | static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn) |
715 | { | |
4e978d06 | 716 | struct rdt_resource *r; |
5ae32bbc | 717 | unsigned long fflags; |
a9a0c771 | 718 | char name[32]; |
5ae32bbc | 719 | int ret; |
4e978d06 FY |
720 | |
721 | /* create the directory */ | |
722 | kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL); | |
723 | if (IS_ERR(kn_info)) | |
724 | return PTR_ERR(kn_info); | |
725 | kernfs_get(kn_info); | |
726 | ||
26017611 | 727 | for_each_alloc_enabled_rdt_resource(r) { |
5ae32bbc TL |
728 | fflags = r->fflags | RF_CTRL_INFO; |
729 | ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags); | |
4e978d06 FY |
730 | if (ret) |
731 | goto out_destroy; | |
4e978d06 | 732 | } |
a9a0c771 VS |
733 | |
734 | for_each_mon_enabled_rdt_resource(r) { | |
735 | fflags = r->fflags | RF_MON_INFO; | |
736 | sprintf(name, "%s_MON", r->name); | |
737 | ret = rdtgroup_mkdir_info_resdir(r, name, fflags); | |
738 | if (ret) | |
739 | goto out_destroy; | |
740 | } | |
741 | ||
4e978d06 FY |
742 | /* |
743 | * This extra ref will be put in kernfs_remove() and guarantees | |
744 | * that @rdtgrp->kn is always accessible. | |
745 | */ | |
746 | kernfs_get(kn_info); | |
747 | ||
748 | ret = rdtgroup_kn_set_ugid(kn_info); | |
749 | if (ret) | |
750 | goto out_destroy; | |
751 | ||
752 | kernfs_activate(kn_info); | |
753 | ||
754 | return 0; | |
755 | ||
756 | out_destroy: | |
757 | kernfs_remove(kn_info); | |
758 | return ret; | |
759 | } | |
760 | ||
ccac7180 VS |
761 | static int |
762 | mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp, | |
763 | char *name, struct kernfs_node **dest_kn) | |
764 | { | |
765 | struct kernfs_node *kn; | |
766 | int ret; | |
767 | ||
768 | /* create the directory */ | |
769 | kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); | |
770 | if (IS_ERR(kn)) | |
771 | return PTR_ERR(kn); | |
772 | ||
773 | if (dest_kn) | |
774 | *dest_kn = kn; | |
775 | ||
776 | /* | |
777 | * This extra ref will be put in kernfs_remove() and guarantees | |
778 | * that @rdtgrp->kn is always accessible. | |
779 | */ | |
780 | kernfs_get(kn); | |
781 | ||
782 | ret = rdtgroup_kn_set_ugid(kn); | |
783 | if (ret) | |
784 | goto out_destroy; | |
785 | ||
786 | kernfs_activate(kn); | |
787 | ||
788 | return 0; | |
789 | ||
790 | out_destroy: | |
791 | kernfs_remove(kn); | |
792 | return ret; | |
793 | } | |
5ff193fb FY |
794 | static void l3_qos_cfg_update(void *arg) |
795 | { | |
796 | bool *enable = arg; | |
797 | ||
798 | wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL); | |
799 | } | |
800 | ||
801 | static int set_l3_qos_cfg(struct rdt_resource *r, bool enable) | |
802 | { | |
803 | cpumask_var_t cpu_mask; | |
804 | struct rdt_domain *d; | |
805 | int cpu; | |
806 | ||
807 | if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) | |
808 | return -ENOMEM; | |
809 | ||
810 | list_for_each_entry(d, &r->domains, list) { | |
811 | /* Pick one CPU from each domain instance to update MSR */ | |
812 | cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); | |
813 | } | |
814 | cpu = get_cpu(); | |
815 | /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */ | |
816 | if (cpumask_test_cpu(cpu, cpu_mask)) | |
817 | l3_qos_cfg_update(&enable); | |
818 | /* Update QOS_CFG MSR on all other cpus in cpu_mask. */ | |
819 | smp_call_function_many(cpu_mask, l3_qos_cfg_update, &enable, 1); | |
820 | put_cpu(); | |
821 | ||
822 | free_cpumask_var(cpu_mask); | |
823 | ||
824 | return 0; | |
825 | } | |
826 | ||
827 | static int cdp_enable(void) | |
828 | { | |
829 | struct rdt_resource *r_l3data = &rdt_resources_all[RDT_RESOURCE_L3DATA]; | |
830 | struct rdt_resource *r_l3code = &rdt_resources_all[RDT_RESOURCE_L3CODE]; | |
831 | struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3]; | |
832 | int ret; | |
833 | ||
26017611 VS |
834 | if (!r_l3->alloc_capable || !r_l3data->alloc_capable || |
835 | !r_l3code->alloc_capable) | |
5ff193fb FY |
836 | return -EINVAL; |
837 | ||
838 | ret = set_l3_qos_cfg(r_l3, true); | |
839 | if (!ret) { | |
26017611 VS |
840 | r_l3->alloc_enabled = false; |
841 | r_l3data->alloc_enabled = true; | |
842 | r_l3code->alloc_enabled = true; | |
5ff193fb FY |
843 | } |
844 | return ret; | |
845 | } | |
846 | ||
847 | static void cdp_disable(void) | |
848 | { | |
849 | struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3]; | |
850 | ||
26017611 | 851 | r->alloc_enabled = r->alloc_capable; |
5ff193fb | 852 | |
26017611 VS |
853 | if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) { |
854 | rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled = false; | |
855 | rdt_resources_all[RDT_RESOURCE_L3CODE].alloc_enabled = false; | |
5ff193fb FY |
856 | set_l3_qos_cfg(r, false); |
857 | } | |
858 | } | |
859 | ||
860 | static int parse_rdtgroupfs_options(char *data) | |
861 | { | |
862 | char *token, *o = data; | |
863 | int ret = 0; | |
864 | ||
865 | while ((token = strsep(&o, ",")) != NULL) { | |
866 | if (!*token) | |
867 | return -EINVAL; | |
868 | ||
869 | if (!strcmp(token, "cdp")) | |
870 | ret = cdp_enable(); | |
871 | } | |
872 | ||
873 | return ret; | |
874 | } | |
875 | ||
60cf5e10 FY |
876 | /* |
877 | * We don't allow rdtgroup directories to be created anywhere | |
878 | * except the root directory. Thus when looking for the rdtgroup | |
879 | * structure for a kernfs node we are either looking at a directory, | |
880 | * in which case the rdtgroup structure is pointed at by the "priv" | |
881 | * field, otherwise we have a file, and need only look to the parent | |
882 | * to find the rdtgroup. | |
883 | */ | |
884 | static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn) | |
885 | { | |
f57b3087 FY |
886 | if (kernfs_type(kn) == KERNFS_DIR) { |
887 | /* | |
888 | * All the resource directories use "kn->priv" | |
889 | * to point to the "struct rdtgroup" for the | |
890 | * resource. "info" and its subdirectories don't | |
891 | * have rdtgroup structures, so return NULL here. | |
892 | */ | |
893 | if (kn == kn_info || kn->parent == kn_info) | |
894 | return NULL; | |
895 | else | |
896 | return kn->priv; | |
897 | } else { | |
60cf5e10 | 898 | return kn->parent->priv; |
f57b3087 | 899 | } |
60cf5e10 FY |
900 | } |
901 | ||
902 | struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn) | |
903 | { | |
904 | struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); | |
905 | ||
f57b3087 FY |
906 | if (!rdtgrp) |
907 | return NULL; | |
908 | ||
60cf5e10 FY |
909 | atomic_inc(&rdtgrp->waitcount); |
910 | kernfs_break_active_protection(kn); | |
911 | ||
912 | mutex_lock(&rdtgroup_mutex); | |
913 | ||
914 | /* Was this group deleted while we waited? */ | |
915 | if (rdtgrp->flags & RDT_DELETED) | |
916 | return NULL; | |
917 | ||
918 | return rdtgrp; | |
919 | } | |
920 | ||
921 | void rdtgroup_kn_unlock(struct kernfs_node *kn) | |
922 | { | |
923 | struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); | |
924 | ||
f57b3087 FY |
925 | if (!rdtgrp) |
926 | return; | |
927 | ||
60cf5e10 FY |
928 | mutex_unlock(&rdtgroup_mutex); |
929 | ||
930 | if (atomic_dec_and_test(&rdtgrp->waitcount) && | |
931 | (rdtgrp->flags & RDT_DELETED)) { | |
932 | kernfs_unbreak_active_protection(kn); | |
49ec8f5b | 933 | kernfs_put(rdtgrp->kn); |
60cf5e10 FY |
934 | kfree(rdtgrp); |
935 | } else { | |
936 | kernfs_unbreak_active_protection(kn); | |
937 | } | |
938 | } | |
939 | ||
5ff193fb FY |
940 | static struct dentry *rdt_mount(struct file_system_type *fs_type, |
941 | int flags, const char *unused_dev_name, | |
942 | void *data) | |
943 | { | |
944 | struct dentry *dentry; | |
945 | int ret; | |
946 | ||
947 | mutex_lock(&rdtgroup_mutex); | |
948 | /* | |
949 | * resctrl file system can only be mounted once. | |
950 | */ | |
26017611 | 951 | if (static_branch_unlikely(&rdt_alloc_enable_key)) { |
5ff193fb FY |
952 | dentry = ERR_PTR(-EBUSY); |
953 | goto out; | |
954 | } | |
955 | ||
956 | ret = parse_rdtgroupfs_options(data); | |
957 | if (ret) { | |
958 | dentry = ERR_PTR(ret); | |
959 | goto out_cdp; | |
960 | } | |
961 | ||
60cf5e10 FY |
962 | closid_init(); |
963 | ||
4e978d06 | 964 | ret = rdtgroup_create_info_dir(rdtgroup_default.kn); |
7bff0af5 SL |
965 | if (ret) { |
966 | dentry = ERR_PTR(ret); | |
4e978d06 | 967 | goto out_cdp; |
7bff0af5 | 968 | } |
4e978d06 | 969 | |
5ff193fb FY |
970 | dentry = kernfs_mount(fs_type, flags, rdt_root, |
971 | RDTGROUP_SUPER_MAGIC, NULL); | |
972 | if (IS_ERR(dentry)) | |
79298acc | 973 | goto out_destroy; |
5ff193fb | 974 | |
26017611 | 975 | static_branch_enable(&rdt_alloc_enable_key); |
5ff193fb FY |
976 | goto out; |
977 | ||
79298acc VS |
978 | out_destroy: |
979 | kernfs_remove(kn_info); | |
5ff193fb FY |
980 | out_cdp: |
981 | cdp_disable(); | |
982 | out: | |
983 | mutex_unlock(&rdtgroup_mutex); | |
984 | ||
985 | return dentry; | |
986 | } | |
987 | ||
2545e9f5 | 988 | static int reset_all_ctrls(struct rdt_resource *r) |
5ff193fb FY |
989 | { |
990 | struct msr_param msr_param; | |
991 | cpumask_var_t cpu_mask; | |
992 | struct rdt_domain *d; | |
993 | int i, cpu; | |
994 | ||
995 | if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) | |
996 | return -ENOMEM; | |
997 | ||
998 | msr_param.res = r; | |
999 | msr_param.low = 0; | |
1000 | msr_param.high = r->num_closid; | |
1001 | ||
1002 | /* | |
1003 | * Disable resource control for this resource by setting all | |
1004 | * CBMs in all domains to the maximum mask value. Pick one CPU | |
1005 | * from each domain to update the MSRs below. | |
1006 | */ | |
1007 | list_for_each_entry(d, &r->domains, list) { | |
1008 | cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); | |
1009 | ||
1010 | for (i = 0; i < r->num_closid; i++) | |
2545e9f5 | 1011 | d->ctrl_val[i] = r->default_ctrl; |
5ff193fb FY |
1012 | } |
1013 | cpu = get_cpu(); | |
1014 | /* Update CBM on this cpu if it's in cpu_mask. */ | |
1015 | if (cpumask_test_cpu(cpu, cpu_mask)) | |
2545e9f5 | 1016 | rdt_ctrl_update(&msr_param); |
5ff193fb | 1017 | /* Update CBM on all other cpus in cpu_mask. */ |
2545e9f5 | 1018 | smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1); |
5ff193fb FY |
1019 | put_cpu(); |
1020 | ||
1021 | free_cpumask_var(cpu_mask); | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
4e978d06 | 1026 | /* |
0efc89be FY |
1027 | * Move tasks from one to the other group. If @from is NULL, then all tasks |
1028 | * in the systems are moved unconditionally (used for teardown). | |
1029 | * | |
1030 | * If @mask is not NULL the cpus on which moved tasks are running are set | |
1031 | * in that mask so the update smp function call is restricted to affected | |
1032 | * cpus. | |
4e978d06 | 1033 | */ |
0efc89be FY |
1034 | static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, |
1035 | struct cpumask *mask) | |
4e978d06 | 1036 | { |
e02737d5 FY |
1037 | struct task_struct *p, *t; |
1038 | ||
e02737d5 | 1039 | read_lock(&tasklist_lock); |
0efc89be FY |
1040 | for_each_process_thread(p, t) { |
1041 | if (!from || t->closid == from->closid) { | |
1042 | t->closid = to->closid; | |
1043 | #ifdef CONFIG_SMP | |
1044 | /* | |
1045 | * This is safe on x86 w/o barriers as the ordering | |
1046 | * of writing to task_cpu() and t->on_cpu is | |
1047 | * reverse to the reading here. The detection is | |
1048 | * inaccurate as tasks might move or schedule | |
1049 | * before the smp function call takes place. In | |
1050 | * such a case the function call is pointless, but | |
1051 | * there is no other side effect. | |
1052 | */ | |
1053 | if (mask && t->on_cpu) | |
1054 | cpumask_set_cpu(task_cpu(t), mask); | |
1055 | #endif | |
1056 | } | |
1057 | } | |
e02737d5 | 1058 | read_unlock(&tasklist_lock); |
0efc89be FY |
1059 | } |
1060 | ||
1061 | /* | |
1062 | * Forcibly remove all of subdirectories under root. | |
1063 | */ | |
1064 | static void rmdir_all_sub(void) | |
1065 | { | |
1066 | struct rdtgroup *rdtgrp, *tmp; | |
1067 | ||
1068 | /* Move all tasks to the default resource group */ | |
1069 | rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); | |
60cf5e10 | 1070 | |
60cf5e10 FY |
1071 | list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { |
1072 | /* Remove each rdtgroup other than root */ | |
1073 | if (rdtgrp == &rdtgroup_default) | |
1074 | continue; | |
c7cc0cc1 FY |
1075 | |
1076 | /* | |
1077 | * Give any CPUs back to the default group. We cannot copy | |
1078 | * cpu_online_mask because a CPU might have executed the | |
1079 | * offline callback already, but is still marked online. | |
1080 | */ | |
1081 | cpumask_or(&rdtgroup_default.cpu_mask, | |
1082 | &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); | |
1083 | ||
60cf5e10 FY |
1084 | kernfs_remove(rdtgrp->kn); |
1085 | list_del(&rdtgrp->rdtgroup_list); | |
1086 | kfree(rdtgrp); | |
1087 | } | |
0efc89be FY |
1088 | /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ |
1089 | get_online_cpus(); | |
1090 | rdt_update_closid(cpu_online_mask, &rdtgroup_default.closid); | |
1091 | put_online_cpus(); | |
1092 | ||
4e978d06 FY |
1093 | kernfs_remove(kn_info); |
1094 | } | |
1095 | ||
5ff193fb FY |
1096 | static void rdt_kill_sb(struct super_block *sb) |
1097 | { | |
1098 | struct rdt_resource *r; | |
1099 | ||
1100 | mutex_lock(&rdtgroup_mutex); | |
1101 | ||
1102 | /*Put everything back to default values. */ | |
26017611 | 1103 | for_each_alloc_enabled_rdt_resource(r) |
2545e9f5 | 1104 | reset_all_ctrls(r); |
5ff193fb | 1105 | cdp_disable(); |
4e978d06 | 1106 | rmdir_all_sub(); |
26017611 | 1107 | static_branch_disable(&rdt_alloc_enable_key); |
5ff193fb FY |
1108 | kernfs_kill_sb(sb); |
1109 | mutex_unlock(&rdtgroup_mutex); | |
1110 | } | |
1111 | ||
1112 | static struct file_system_type rdt_fs_type = { | |
1113 | .name = "resctrl", | |
1114 | .mount = rdt_mount, | |
1115 | .kill_sb = rdt_kill_sb, | |
1116 | }; | |
1117 | ||
da68325a VS |
1118 | static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, |
1119 | struct kernfs_node *prgrp_kn, | |
1120 | const char *name, umode_t mode, | |
ccac7180 | 1121 | enum rdt_group_type rtype, struct rdtgroup **r) |
60cf5e10 | 1122 | { |
da68325a | 1123 | struct rdtgroup *prdtgrp, *rdtgrp; |
60cf5e10 | 1124 | struct kernfs_node *kn; |
da68325a VS |
1125 | uint files = 0; |
1126 | int ret; | |
60cf5e10 | 1127 | |
da68325a VS |
1128 | prdtgrp = rdtgroup_kn_lock_live(prgrp_kn); |
1129 | if (!prdtgrp) { | |
60cf5e10 FY |
1130 | ret = -ENODEV; |
1131 | goto out_unlock; | |
1132 | } | |
1133 | ||
60cf5e10 FY |
1134 | /* allocate the rdtgroup. */ |
1135 | rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); | |
1136 | if (!rdtgrp) { | |
1137 | ret = -ENOSPC; | |
da68325a | 1138 | goto out_unlock; |
60cf5e10 | 1139 | } |
da68325a | 1140 | *r = rdtgrp; |
ccac7180 VS |
1141 | rdtgrp->mon.parent = prdtgrp; |
1142 | rdtgrp->type = rtype; | |
1143 | INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); | |
60cf5e10 FY |
1144 | |
1145 | /* kernfs creates the directory for rdtgrp */ | |
da68325a | 1146 | kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); |
60cf5e10 FY |
1147 | if (IS_ERR(kn)) { |
1148 | ret = PTR_ERR(kn); | |
da68325a | 1149 | goto out_free_rgrp; |
60cf5e10 FY |
1150 | } |
1151 | rdtgrp->kn = kn; | |
1152 | ||
1153 | /* | |
1154 | * kernfs_remove() will drop the reference count on "kn" which | |
1155 | * will free it. But we still need it to stick around for the | |
1156 | * rdtgroup_kn_unlock(kn} call below. Take one extra reference | |
1157 | * here, which will be dropped inside rdtgroup_kn_unlock(). | |
1158 | */ | |
1159 | kernfs_get(kn); | |
1160 | ||
1161 | ret = rdtgroup_kn_set_ugid(kn); | |
1162 | if (ret) | |
1163 | goto out_destroy; | |
1164 | ||
da68325a | 1165 | files = RFTYPE_BASE | RFTYPE_CTRL; |
ccac7180 | 1166 | files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype); |
da68325a | 1167 | ret = rdtgroup_add_files(kn, files); |
12e0110c TL |
1168 | if (ret) |
1169 | goto out_destroy; | |
1170 | ||
ccac7180 VS |
1171 | if (rdt_mon_capable) { |
1172 | ret = alloc_rmid(); | |
1173 | if (ret < 0) | |
1174 | goto out_destroy; | |
1175 | rdtgrp->mon.rmid = ret; | |
1176 | } | |
60cf5e10 FY |
1177 | kernfs_activate(kn); |
1178 | ||
da68325a VS |
1179 | /* |
1180 | * The caller unlocks the prgrp_kn upon success. | |
1181 | */ | |
1182 | return 0; | |
60cf5e10 FY |
1183 | |
1184 | out_destroy: | |
1185 | kernfs_remove(rdtgrp->kn); | |
da68325a | 1186 | out_free_rgrp: |
60cf5e10 | 1187 | kfree(rdtgrp); |
60cf5e10 | 1188 | out_unlock: |
da68325a VS |
1189 | rdtgroup_kn_unlock(prgrp_kn); |
1190 | return ret; | |
1191 | } | |
1192 | ||
1193 | static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) | |
1194 | { | |
1195 | kernfs_remove(rgrp->kn); | |
ccac7180 | 1196 | free_rmid(rgrp->mon.rmid); |
da68325a VS |
1197 | kfree(rgrp); |
1198 | } | |
1199 | ||
ccac7180 VS |
1200 | /* |
1201 | * Create a monitor group under "mon_groups" directory of a control | |
1202 | * and monitor group(ctrl_mon). This is a resource group | |
1203 | * to monitor a subset of tasks and cpus in its parent ctrl_mon group. | |
1204 | */ | |
1205 | static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, | |
1206 | struct kernfs_node *prgrp_kn, | |
1207 | const char *name, | |
1208 | umode_t mode) | |
1209 | { | |
1210 | struct rdtgroup *rdtgrp, *prgrp; | |
1211 | int ret; | |
1212 | ||
1213 | ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP, | |
1214 | &rdtgrp); | |
1215 | if (ret) | |
1216 | return ret; | |
1217 | ||
1218 | prgrp = rdtgrp->mon.parent; | |
1219 | rdtgrp->closid = prgrp->closid; | |
1220 | ||
1221 | /* | |
1222 | * Add the rdtgrp to the list of rdtgrps the parent | |
1223 | * ctrl_mon group has to track. | |
1224 | */ | |
1225 | list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); | |
1226 | ||
1227 | rdtgroup_kn_unlock(prgrp_kn); | |
1228 | return ret; | |
1229 | } | |
1230 | ||
da68325a VS |
1231 | /* |
1232 | * These are rdtgroups created under the root directory. Can be used | |
ccac7180 | 1233 | * to allocate and monitor resources. |
da68325a | 1234 | */ |
ccac7180 VS |
1235 | static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, |
1236 | struct kernfs_node *prgrp_kn, | |
1237 | const char *name, umode_t mode) | |
da68325a VS |
1238 | { |
1239 | struct rdtgroup *rdtgrp; | |
1240 | struct kernfs_node *kn; | |
1241 | u32 closid; | |
1242 | int ret; | |
1243 | ||
ccac7180 VS |
1244 | ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP, |
1245 | &rdtgrp); | |
da68325a VS |
1246 | if (ret) |
1247 | return ret; | |
1248 | ||
1249 | kn = rdtgrp->kn; | |
1250 | ret = closid_alloc(); | |
1251 | if (ret < 0) | |
1252 | goto out_common_fail; | |
1253 | closid = ret; | |
1254 | ||
1255 | rdtgrp->closid = closid; | |
1256 | list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); | |
1257 | ||
ccac7180 VS |
1258 | if (rdt_mon_capable) { |
1259 | /* | |
1260 | * Create an empty mon_groups directory to hold the subset | |
1261 | * of tasks and cpus to monitor. | |
1262 | */ | |
1263 | ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL); | |
1264 | if (ret) | |
1265 | goto out_id_free; | |
1266 | } | |
1267 | ||
da68325a VS |
1268 | goto out_unlock; |
1269 | ||
ccac7180 VS |
1270 | out_id_free: |
1271 | closid_free(closid); | |
1272 | list_del(&rdtgrp->rdtgroup_list); | |
da68325a VS |
1273 | out_common_fail: |
1274 | mkdir_rdt_prepare_clean(rdtgrp); | |
1275 | out_unlock: | |
1276 | rdtgroup_kn_unlock(prgrp_kn); | |
60cf5e10 FY |
1277 | return ret; |
1278 | } | |
1279 | ||
ccac7180 VS |
1280 | /* |
1281 | * We allow creating mon groups only with in a directory called "mon_groups" | |
1282 | * which is present in every ctrl_mon group. Check if this is a valid | |
1283 | * "mon_groups" directory. | |
1284 | * | |
1285 | * 1. The directory should be named "mon_groups". | |
1286 | * 2. The mon group itself should "not" be named "mon_groups". | |
1287 | * This makes sure "mon_groups" directory always has a ctrl_mon group | |
1288 | * as parent. | |
1289 | */ | |
1290 | static bool is_mon_groups(struct kernfs_node *kn, const char *name) | |
1291 | { | |
1292 | return (!strcmp(kn->name, "mon_groups") && | |
1293 | strcmp(name, "mon_groups")); | |
1294 | } | |
1295 | ||
da68325a VS |
1296 | static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
1297 | umode_t mode) | |
1298 | { | |
1299 | /* Do not accept '\n' to avoid unparsable situation. */ | |
1300 | if (strchr(name, '\n')) | |
1301 | return -EINVAL; | |
1302 | ||
1303 | /* | |
1304 | * If the parent directory is the root directory and RDT | |
ccac7180 VS |
1305 | * allocation is supported, add a control and monitoring |
1306 | * subdirectory | |
da68325a VS |
1307 | */ |
1308 | if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn) | |
ccac7180 VS |
1309 | return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode); |
1310 | ||
1311 | /* | |
1312 | * If RDT monitoring is supported and the parent directory is a valid | |
1313 | * "mon_groups" directory, add a monitoring subdirectory. | |
1314 | */ | |
1315 | if (rdt_mon_capable && is_mon_groups(parent_kn, name)) | |
1316 | return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode); | |
da68325a VS |
1317 | |
1318 | return -EPERM; | |
1319 | } | |
1320 | ||
60cf5e10 FY |
1321 | static int rdtgroup_rmdir(struct kernfs_node *kn) |
1322 | { | |
0efc89be | 1323 | int ret, cpu, closid = rdtgroup_default.closid; |
60cf5e10 | 1324 | struct rdtgroup *rdtgrp; |
0efc89be FY |
1325 | cpumask_var_t tmpmask; |
1326 | ||
1327 | if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) | |
1328 | return -ENOMEM; | |
60cf5e10 FY |
1329 | |
1330 | rdtgrp = rdtgroup_kn_lock_live(kn); | |
1331 | if (!rdtgrp) { | |
0efc89be FY |
1332 | ret = -EPERM; |
1333 | goto out; | |
60cf5e10 FY |
1334 | } |
1335 | ||
e02737d5 | 1336 | /* Give any tasks back to the default group */ |
0efc89be | 1337 | rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); |
e02737d5 | 1338 | |
12e0110c TL |
1339 | /* Give any CPUs back to the default group */ |
1340 | cpumask_or(&rdtgroup_default.cpu_mask, | |
1341 | &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); | |
0efc89be FY |
1342 | |
1343 | /* Update per cpu closid of the moved CPUs first */ | |
1344 | for_each_cpu(cpu, &rdtgrp->cpu_mask) | |
1345 | per_cpu(cpu_closid, cpu) = closid; | |
1346 | /* | |
1347 | * Update the MSR on moved CPUs and CPUs which have moved | |
1348 | * task running on them. | |
1349 | */ | |
1350 | cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); | |
1351 | rdt_update_closid(tmpmask, NULL); | |
12e0110c | 1352 | |
60cf5e10 FY |
1353 | rdtgrp->flags = RDT_DELETED; |
1354 | closid_free(rdtgrp->closid); | |
1355 | list_del(&rdtgrp->rdtgroup_list); | |
1356 | ||
1357 | /* | |
1358 | * one extra hold on this, will drop when we kfree(rdtgrp) | |
1359 | * in rdtgroup_kn_unlock() | |
1360 | */ | |
1361 | kernfs_get(kn); | |
1362 | kernfs_remove(rdtgrp->kn); | |
0efc89be FY |
1363 | ret = 0; |
1364 | out: | |
60cf5e10 | 1365 | rdtgroup_kn_unlock(kn); |
0efc89be FY |
1366 | free_cpumask_var(tmpmask); |
1367 | return ret; | |
60cf5e10 FY |
1368 | } |
1369 | ||
76ae054c SL |
1370 | static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) |
1371 | { | |
26017611 | 1372 | if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) |
76ae054c SL |
1373 | seq_puts(seq, ",cdp"); |
1374 | return 0; | |
1375 | } | |
1376 | ||
5ff193fb | 1377 | static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { |
76ae054c SL |
1378 | .mkdir = rdtgroup_mkdir, |
1379 | .rmdir = rdtgroup_rmdir, | |
1380 | .show_options = rdtgroup_show_options, | |
5ff193fb FY |
1381 | }; |
1382 | ||
1383 | static int __init rdtgroup_setup_root(void) | |
1384 | { | |
12e0110c TL |
1385 | int ret; |
1386 | ||
5ff193fb FY |
1387 | rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, |
1388 | KERNFS_ROOT_CREATE_DEACTIVATED, | |
1389 | &rdtgroup_default); | |
1390 | if (IS_ERR(rdt_root)) | |
1391 | return PTR_ERR(rdt_root); | |
1392 | ||
1393 | mutex_lock(&rdtgroup_mutex); | |
1394 | ||
1395 | rdtgroup_default.closid = 0; | |
ccac7180 VS |
1396 | rdtgroup_default.mon.rmid = 0; |
1397 | rdtgroup_default.type = RDTCTRL_GROUP; | |
1398 | INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); | |
1399 | ||
5ff193fb FY |
1400 | list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); |
1401 | ||
5ae32bbc | 1402 | ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE); |
12e0110c TL |
1403 | if (ret) { |
1404 | kernfs_destroy_root(rdt_root); | |
1405 | goto out; | |
1406 | } | |
1407 | ||
5ff193fb FY |
1408 | rdtgroup_default.kn = rdt_root->kn; |
1409 | kernfs_activate(rdtgroup_default.kn); | |
1410 | ||
12e0110c | 1411 | out: |
5ff193fb FY |
1412 | mutex_unlock(&rdtgroup_mutex); |
1413 | ||
12e0110c | 1414 | return ret; |
5ff193fb FY |
1415 | } |
1416 | ||
1417 | /* | |
1418 | * rdtgroup_init - rdtgroup initialization | |
1419 | * | |
1420 | * Setup resctrl file system including set up root, create mount point, | |
1421 | * register rdtgroup filesystem, and initialize files under root directory. | |
1422 | * | |
1423 | * Return: 0 on success or -errno | |
1424 | */ | |
1425 | int __init rdtgroup_init(void) | |
1426 | { | |
1427 | int ret = 0; | |
1428 | ||
1429 | ret = rdtgroup_setup_root(); | |
1430 | if (ret) | |
1431 | return ret; | |
1432 | ||
1433 | ret = sysfs_create_mount_point(fs_kobj, "resctrl"); | |
1434 | if (ret) | |
1435 | goto cleanup_root; | |
1436 | ||
1437 | ret = register_filesystem(&rdt_fs_type); | |
1438 | if (ret) | |
1439 | goto cleanup_mountpoint; | |
1440 | ||
1441 | return 0; | |
1442 | ||
1443 | cleanup_mountpoint: | |
1444 | sysfs_remove_mount_point(fs_kobj, "resctrl"); | |
1445 | cleanup_root: | |
1446 | kernfs_destroy_root(rdt_root); | |
1447 | ||
1448 | return ret; | |
1449 | } |