]>
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> | |
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 |
39 | DEFINE_STATIC_KEY_FALSE(rdt_enable_key); |
40 | DEFINE_STATIC_KEY_FALSE(rdt_mon_enable_key); | |
1b5c0b75 | 41 | DEFINE_STATIC_KEY_FALSE(rdt_alloc_enable_key); |
cb2200e9 | 42 | static struct kernfs_root *rdt_root; |
5ff193fb FY |
43 | struct rdtgroup rdtgroup_default; |
44 | LIST_HEAD(rdt_all_groups); | |
45 | ||
4e978d06 FY |
46 | /* Kernel fs node for "info" directory under root */ |
47 | static struct kernfs_node *kn_info; | |
48 | ||
4af4a88e VS |
49 | /* Kernel fs node for "mon_groups" directory under root */ |
50 | static struct kernfs_node *kn_mongrp; | |
51 | ||
52 | /* Kernel fs node for "mon_data" directory under root */ | |
53 | static struct kernfs_node *kn_mondata; | |
54 | ||
9b3a7fd0 TL |
55 | static struct seq_buf last_cmd_status; |
56 | static char last_cmd_status_buf[512]; | |
57 | ||
58 | void rdt_last_cmd_clear(void) | |
59 | { | |
60 | lockdep_assert_held(&rdtgroup_mutex); | |
61 | seq_buf_clear(&last_cmd_status); | |
62 | } | |
63 | ||
64 | void rdt_last_cmd_puts(const char *s) | |
65 | { | |
66 | lockdep_assert_held(&rdtgroup_mutex); | |
67 | seq_buf_puts(&last_cmd_status, s); | |
68 | } | |
69 | ||
70 | void 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 | */ | |
95 | static int closid_free_map; | |
96 | ||
97 | static 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 | 112 | static 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 | ||
124 | static 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 */ |
130 | static 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 | ||
143 | static 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 |
163 | static 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 | ||
173 | static 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 | ||
184 | static 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 |
190 | static struct kernfs_ops kf_mondata_ops = { |
191 | .atomic_write_len = PAGE_SIZE, | |
192 | .seq_show = rdtgroup_mondata_show, | |
193 | }; | |
194 | ||
4ffa3c97 JO |
195 | static 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 |
202 | static 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 | 227 | static 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 | */ |
249 | static void | |
a9fcf862 | 250 | update_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 |
260 | static 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 | ||
303 | static 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 | 313 | static 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 |
368 | static 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 |
423 | unlock: |
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 |
432 | struct task_move_callback { |
433 | struct callback_head work; | |
434 | struct rdtgroup *rdtgrp; | |
435 | }; | |
436 | ||
437 | static 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 | ||
465 | static 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 | ||
513 | static 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 | ||
535 | static 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 | ||
564 | static 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 | ||
586 | static 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 | ||
599 | static 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 |
615 | static 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 |
630 | static 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 | 639 | static 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 |
648 | static 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 |
657 | static 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 |
666 | static 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 |
675 | static 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 | ||
685 | static 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 |
697 | static 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 | ||
706 | static 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 |
715 | static 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 | ||
725 | static 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 | 745 | static 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 | 859 | static 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; | |
878 | error: | |
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 |
887 | static 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 |
910 | static 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 | ||
956 | out_destroy: | |
957 | kernfs_remove(kn_info); | |
958 | return ret; | |
959 | } | |
960 | ||
c7d9aac6 VS |
961 | static int |
962 | mongroup_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 | ||
990 | out_destroy: | |
991 | kernfs_remove(kn); | |
992 | return ret; | |
993 | } | |
5ff193fb FY |
994 | static 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 | ||
1001 | static 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 | ||
1027 | static 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 | ||
1047 | static 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 | ||
1060 | static 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 | */ | |
1084 | static 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 | ||
1102 | struct 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 | ||
1121 | void 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 |
1140 | static int mkdir_mondata_all(struct kernfs_node *parent_kn, |
1141 | struct rdtgroup *prgrp, | |
1142 | struct kernfs_node **mon_data_kn); | |
1143 | ||
5ff193fb FY |
1144 | static 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, | |
1179 | NULL, "mon_groups", | |
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 |
1218 | out_mondata: |
1219 | if (rdt_mon_capable) | |
1220 | kernfs_remove(kn_mondata); | |
1221 | out_mongrp: | |
1222 | if (rdt_mon_capable) | |
1223 | kernfs_remove(kn_mongrp); | |
1224 | out_info: | |
79298acc | 1225 | kernfs_remove(kn_info); |
5ff193fb FY |
1226 | out_cdp: |
1227 | cdp_disable(); | |
1228 | out: | |
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 | 1236 | static 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 |
1274 | static 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 | ||
1280 | static 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 |
1294 | static 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 |
1324 | static 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); | |
1333 | kfree(sentry); | |
1334 | } | |
1335 | } | |
1336 | ||
0efc89be FY |
1337 | /* |
1338 | * Forcibly remove all of subdirectories under root. | |
1339 | */ | |
1340 | static void rmdir_all_sub(void) | |
1341 | { | |
1342 | struct rdtgroup *rdtgrp, *tmp; | |
1343 | ||
1344 | /* Move all tasks to the default resource group */ | |
1345 | rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); | |
60cf5e10 | 1346 | |
60cf5e10 | 1347 | list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { |
4af4a88e VS |
1348 | /* Free any child rmids */ |
1349 | free_all_child_rdtgrp(rdtgrp); | |
1350 | ||
60cf5e10 FY |
1351 | /* Remove each rdtgroup other than root */ |
1352 | if (rdtgrp == &rdtgroup_default) | |
1353 | continue; | |
c7cc0cc1 FY |
1354 | |
1355 | /* | |
1356 | * Give any CPUs back to the default group. We cannot copy | |
1357 | * cpu_online_mask because a CPU might have executed the | |
1358 | * offline callback already, but is still marked online. | |
1359 | */ | |
1360 | cpumask_or(&rdtgroup_default.cpu_mask, | |
1361 | &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); | |
1362 | ||
4af4a88e VS |
1363 | free_rmid(rdtgrp->mon.rmid); |
1364 | ||
60cf5e10 FY |
1365 | kernfs_remove(rdtgrp->kn); |
1366 | list_del(&rdtgrp->rdtgroup_list); | |
1367 | kfree(rdtgrp); | |
1368 | } | |
0efc89be | 1369 | /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ |
a9fcf862 | 1370 | update_closid_rmid(cpu_online_mask, &rdtgroup_default); |
0efc89be | 1371 | |
4e978d06 | 1372 | kernfs_remove(kn_info); |
4af4a88e VS |
1373 | kernfs_remove(kn_mongrp); |
1374 | kernfs_remove(kn_mondata); | |
4e978d06 FY |
1375 | } |
1376 | ||
5ff193fb FY |
1377 | static void rdt_kill_sb(struct super_block *sb) |
1378 | { | |
1379 | struct rdt_resource *r; | |
1380 | ||
36b6f9fc | 1381 | cpus_read_lock(); |
5ff193fb FY |
1382 | mutex_lock(&rdtgroup_mutex); |
1383 | ||
1384 | /*Put everything back to default values. */ | |
1b5c0b75 | 1385 | for_each_alloc_enabled_rdt_resource(r) |
2545e9f5 | 1386 | reset_all_ctrls(r); |
5ff193fb | 1387 | cdp_disable(); |
4e978d06 | 1388 | rmdir_all_sub(); |
36b6f9fc RC |
1389 | static_branch_disable_cpuslocked(&rdt_alloc_enable_key); |
1390 | static_branch_disable_cpuslocked(&rdt_mon_enable_key); | |
1391 | static_branch_disable_cpuslocked(&rdt_enable_key); | |
5ff193fb FY |
1392 | kernfs_kill_sb(sb); |
1393 | mutex_unlock(&rdtgroup_mutex); | |
36b6f9fc | 1394 | cpus_read_unlock(); |
5ff193fb FY |
1395 | } |
1396 | ||
1397 | static struct file_system_type rdt_fs_type = { | |
1398 | .name = "resctrl", | |
1399 | .mount = rdt_mount, | |
1400 | .kill_sb = rdt_kill_sb, | |
1401 | }; | |
1402 | ||
d89b7379 VS |
1403 | static int mon_addfile(struct kernfs_node *parent_kn, const char *name, |
1404 | void *priv) | |
1405 | { | |
1406 | struct kernfs_node *kn; | |
1407 | int ret = 0; | |
1408 | ||
630d0eb6 DT |
1409 | kn = __kernfs_create_file(parent_kn, name, 0444, |
1410 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 0, | |
d89b7379 VS |
1411 | &kf_mondata_ops, priv, NULL, NULL); |
1412 | if (IS_ERR(kn)) | |
1413 | return PTR_ERR(kn); | |
1414 | ||
1415 | ret = rdtgroup_kn_set_ugid(kn); | |
1416 | if (ret) { | |
1417 | kernfs_remove(kn); | |
1418 | return ret; | |
1419 | } | |
1420 | ||
1421 | return ret; | |
1422 | } | |
1423 | ||
895c663e VS |
1424 | /* |
1425 | * Remove all subdirectories of mon_data of ctrl_mon groups | |
1426 | * and monitor groups with given domain id. | |
1427 | */ | |
1428 | void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id) | |
1429 | { | |
1430 | struct rdtgroup *prgrp, *crgrp; | |
1431 | char name[32]; | |
1432 | ||
1433 | if (!r->mon_enabled) | |
1434 | return; | |
1435 | ||
1436 | list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { | |
1437 | sprintf(name, "mon_%s_%02d", r->name, dom_id); | |
1438 | kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); | |
1439 | ||
1440 | list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) | |
1441 | kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); | |
1442 | } | |
1443 | } | |
1444 | ||
d89b7379 VS |
1445 | static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, |
1446 | struct rdt_domain *d, | |
1447 | struct rdt_resource *r, struct rdtgroup *prgrp) | |
1448 | { | |
1449 | union mon_data_bits priv; | |
1450 | struct kernfs_node *kn; | |
1451 | struct mon_evt *mevt; | |
a4de1dfd | 1452 | struct rmid_read rr; |
d89b7379 VS |
1453 | char name[32]; |
1454 | int ret; | |
1455 | ||
1456 | sprintf(name, "mon_%s_%02d", r->name, d->id); | |
1457 | /* create the directory */ | |
1458 | kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); | |
1459 | if (IS_ERR(kn)) | |
1460 | return PTR_ERR(kn); | |
1461 | ||
1462 | /* | |
1463 | * This extra ref will be put in kernfs_remove() and guarantees | |
1464 | * that kn is always accessible. | |
1465 | */ | |
1466 | kernfs_get(kn); | |
1467 | ret = rdtgroup_kn_set_ugid(kn); | |
1468 | if (ret) | |
1469 | goto out_destroy; | |
1470 | ||
1471 | if (WARN_ON(list_empty(&r->evt_list))) { | |
1472 | ret = -EPERM; | |
1473 | goto out_destroy; | |
1474 | } | |
1475 | ||
1476 | priv.u.rid = r->rid; | |
1477 | priv.u.domid = d->id; | |
1478 | list_for_each_entry(mevt, &r->evt_list, list) { | |
1479 | priv.u.evtid = mevt->evtid; | |
1480 | ret = mon_addfile(kn, mevt->name, priv.priv); | |
1481 | if (ret) | |
1482 | goto out_destroy; | |
a4de1dfd VS |
1483 | |
1484 | if (is_mbm_event(mevt->evtid)) | |
1485 | mon_event_read(&rr, d, prgrp, mevt->evtid, true); | |
d89b7379 VS |
1486 | } |
1487 | kernfs_activate(kn); | |
1488 | return 0; | |
1489 | ||
1490 | out_destroy: | |
1491 | kernfs_remove(kn); | |
1492 | return ret; | |
1493 | } | |
1494 | ||
895c663e VS |
1495 | /* |
1496 | * Add all subdirectories of mon_data for "ctrl_mon" groups | |
1497 | * and "monitor" groups with given domain id. | |
1498 | */ | |
1499 | void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, | |
1500 | struct rdt_domain *d) | |
1501 | { | |
1502 | struct kernfs_node *parent_kn; | |
1503 | struct rdtgroup *prgrp, *crgrp; | |
1504 | struct list_head *head; | |
1505 | ||
1506 | if (!r->mon_enabled) | |
1507 | return; | |
1508 | ||
1509 | list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { | |
1510 | parent_kn = prgrp->mon.mon_data_kn; | |
1511 | mkdir_mondata_subdir(parent_kn, d, r, prgrp); | |
1512 | ||
1513 | head = &prgrp->mon.crdtgrp_list; | |
1514 | list_for_each_entry(crgrp, head, mon.crdtgrp_list) { | |
1515 | parent_kn = crgrp->mon.mon_data_kn; | |
1516 | mkdir_mondata_subdir(parent_kn, d, r, crgrp); | |
1517 | } | |
1518 | } | |
1519 | } | |
1520 | ||
d89b7379 VS |
1521 | static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, |
1522 | struct rdt_resource *r, | |
1523 | struct rdtgroup *prgrp) | |
1524 | { | |
1525 | struct rdt_domain *dom; | |
1526 | int ret; | |
1527 | ||
1528 | list_for_each_entry(dom, &r->domains, list) { | |
1529 | ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp); | |
1530 | if (ret) | |
1531 | return ret; | |
1532 | } | |
1533 | ||
1534 | return 0; | |
1535 | } | |
1536 | ||
1537 | /* | |
1538 | * This creates a directory mon_data which contains the monitored data. | |
1539 | * | |
1540 | * mon_data has one directory for each domain whic are named | |
1541 | * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data | |
1542 | * with L3 domain looks as below: | |
1543 | * ./mon_data: | |
1544 | * mon_L3_00 | |
1545 | * mon_L3_01 | |
1546 | * mon_L3_02 | |
1547 | * ... | |
1548 | * | |
1549 | * Each domain directory has one file per event: | |
1550 | * ./mon_L3_00/: | |
1551 | * llc_occupancy | |
1552 | * | |
1553 | */ | |
1554 | static int mkdir_mondata_all(struct kernfs_node *parent_kn, | |
1555 | struct rdtgroup *prgrp, | |
1556 | struct kernfs_node **dest_kn) | |
1557 | { | |
1558 | struct rdt_resource *r; | |
1559 | struct kernfs_node *kn; | |
1560 | int ret; | |
1561 | ||
1562 | /* | |
1563 | * Create the mon_data directory first. | |
1564 | */ | |
1565 | ret = mongroup_create_dir(parent_kn, NULL, "mon_data", &kn); | |
1566 | if (ret) | |
1567 | return ret; | |
1568 | ||
1569 | if (dest_kn) | |
1570 | *dest_kn = kn; | |
1571 | ||
1572 | /* | |
1573 | * Create the subdirectories for each domain. Note that all events | |
1574 | * in a domain like L3 are grouped into a resource whose domain is L3 | |
1575 | */ | |
1576 | for_each_mon_enabled_rdt_resource(r) { | |
1577 | ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); | |
1578 | if (ret) | |
1579 | goto out_destroy; | |
1580 | } | |
1581 | ||
1582 | return 0; | |
1583 | ||
1584 | out_destroy: | |
1585 | kernfs_remove(kn); | |
1586 | return ret; | |
1587 | } | |
1588 | ||
65b4f403 VS |
1589 | static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, |
1590 | struct kernfs_node *prgrp_kn, | |
1591 | const char *name, umode_t mode, | |
c7d9aac6 | 1592 | enum rdt_group_type rtype, struct rdtgroup **r) |
60cf5e10 | 1593 | { |
65b4f403 | 1594 | struct rdtgroup *prdtgrp, *rdtgrp; |
60cf5e10 | 1595 | struct kernfs_node *kn; |
65b4f403 VS |
1596 | uint files = 0; |
1597 | int ret; | |
60cf5e10 | 1598 | |
65b4f403 | 1599 | prdtgrp = rdtgroup_kn_lock_live(prgrp_kn); |
cfd0f34e | 1600 | rdt_last_cmd_clear(); |
65b4f403 | 1601 | if (!prdtgrp) { |
60cf5e10 | 1602 | ret = -ENODEV; |
cfd0f34e | 1603 | rdt_last_cmd_puts("directory was removed\n"); |
60cf5e10 FY |
1604 | goto out_unlock; |
1605 | } | |
1606 | ||
60cf5e10 FY |
1607 | /* allocate the rdtgroup. */ |
1608 | rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); | |
1609 | if (!rdtgrp) { | |
1610 | ret = -ENOSPC; | |
cfd0f34e | 1611 | rdt_last_cmd_puts("kernel out of memory\n"); |
65b4f403 | 1612 | goto out_unlock; |
60cf5e10 | 1613 | } |
65b4f403 | 1614 | *r = rdtgrp; |
c7d9aac6 VS |
1615 | rdtgrp->mon.parent = prdtgrp; |
1616 | rdtgrp->type = rtype; | |
1617 | INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); | |
60cf5e10 FY |
1618 | |
1619 | /* kernfs creates the directory for rdtgrp */ | |
65b4f403 | 1620 | kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); |
60cf5e10 FY |
1621 | if (IS_ERR(kn)) { |
1622 | ret = PTR_ERR(kn); | |
cfd0f34e | 1623 | rdt_last_cmd_puts("kernfs create error\n"); |
65b4f403 | 1624 | goto out_free_rgrp; |
60cf5e10 FY |
1625 | } |
1626 | rdtgrp->kn = kn; | |
1627 | ||
1628 | /* | |
1629 | * kernfs_remove() will drop the reference count on "kn" which | |
1630 | * will free it. But we still need it to stick around for the | |
1631 | * rdtgroup_kn_unlock(kn} call below. Take one extra reference | |
1632 | * here, which will be dropped inside rdtgroup_kn_unlock(). | |
1633 | */ | |
1634 | kernfs_get(kn); | |
1635 | ||
1636 | ret = rdtgroup_kn_set_ugid(kn); | |
cfd0f34e TL |
1637 | if (ret) { |
1638 | rdt_last_cmd_puts("kernfs perm error\n"); | |
60cf5e10 | 1639 | goto out_destroy; |
cfd0f34e | 1640 | } |
60cf5e10 | 1641 | |
c7d9aac6 | 1642 | files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype); |
65b4f403 | 1643 | ret = rdtgroup_add_files(kn, files); |
cfd0f34e TL |
1644 | if (ret) { |
1645 | rdt_last_cmd_puts("kernfs fill error\n"); | |
12e0110c | 1646 | goto out_destroy; |
cfd0f34e | 1647 | } |
12e0110c | 1648 | |
c7d9aac6 VS |
1649 | if (rdt_mon_capable) { |
1650 | ret = alloc_rmid(); | |
cfd0f34e TL |
1651 | if (ret < 0) { |
1652 | rdt_last_cmd_puts("out of RMIDs\n"); | |
c7d9aac6 | 1653 | goto out_destroy; |
cfd0f34e | 1654 | } |
c7d9aac6 | 1655 | rdtgrp->mon.rmid = ret; |
d89b7379 VS |
1656 | |
1657 | ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn); | |
cfd0f34e TL |
1658 | if (ret) { |
1659 | rdt_last_cmd_puts("kernfs subdir error\n"); | |
d89b7379 | 1660 | goto out_idfree; |
cfd0f34e | 1661 | } |
c7d9aac6 | 1662 | } |
60cf5e10 FY |
1663 | kernfs_activate(kn); |
1664 | ||
65b4f403 VS |
1665 | /* |
1666 | * The caller unlocks the prgrp_kn upon success. | |
1667 | */ | |
1668 | return 0; | |
60cf5e10 | 1669 | |
d89b7379 VS |
1670 | out_idfree: |
1671 | free_rmid(rdtgrp->mon.rmid); | |
60cf5e10 FY |
1672 | out_destroy: |
1673 | kernfs_remove(rdtgrp->kn); | |
65b4f403 | 1674 | out_free_rgrp: |
60cf5e10 | 1675 | kfree(rdtgrp); |
60cf5e10 | 1676 | out_unlock: |
65b4f403 VS |
1677 | rdtgroup_kn_unlock(prgrp_kn); |
1678 | return ret; | |
1679 | } | |
1680 | ||
1681 | static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) | |
1682 | { | |
1683 | kernfs_remove(rgrp->kn); | |
c7d9aac6 | 1684 | free_rmid(rgrp->mon.rmid); |
65b4f403 VS |
1685 | kfree(rgrp); |
1686 | } | |
1687 | ||
c7d9aac6 VS |
1688 | /* |
1689 | * Create a monitor group under "mon_groups" directory of a control | |
1690 | * and monitor group(ctrl_mon). This is a resource group | |
1691 | * to monitor a subset of tasks and cpus in its parent ctrl_mon group. | |
1692 | */ | |
1693 | static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, | |
1694 | struct kernfs_node *prgrp_kn, | |
1695 | const char *name, | |
1696 | umode_t mode) | |
1697 | { | |
1698 | struct rdtgroup *rdtgrp, *prgrp; | |
1699 | int ret; | |
1700 | ||
1701 | ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP, | |
1702 | &rdtgrp); | |
1703 | if (ret) | |
1704 | return ret; | |
1705 | ||
1706 | prgrp = rdtgrp->mon.parent; | |
1707 | rdtgrp->closid = prgrp->closid; | |
1708 | ||
1709 | /* | |
1710 | * Add the rdtgrp to the list of rdtgrps the parent | |
1711 | * ctrl_mon group has to track. | |
1712 | */ | |
1713 | list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); | |
1714 | ||
1715 | rdtgroup_kn_unlock(prgrp_kn); | |
1716 | return ret; | |
1717 | } | |
1718 | ||
65b4f403 VS |
1719 | /* |
1720 | * These are rdtgroups created under the root directory. Can be used | |
c7d9aac6 | 1721 | * to allocate and monitor resources. |
65b4f403 | 1722 | */ |
c7d9aac6 VS |
1723 | static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, |
1724 | struct kernfs_node *prgrp_kn, | |
1725 | const char *name, umode_t mode) | |
65b4f403 VS |
1726 | { |
1727 | struct rdtgroup *rdtgrp; | |
1728 | struct kernfs_node *kn; | |
1729 | u32 closid; | |
1730 | int ret; | |
1731 | ||
c7d9aac6 VS |
1732 | ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP, |
1733 | &rdtgrp); | |
65b4f403 VS |
1734 | if (ret) |
1735 | return ret; | |
1736 | ||
1737 | kn = rdtgrp->kn; | |
1738 | ret = closid_alloc(); | |
cfd0f34e TL |
1739 | if (ret < 0) { |
1740 | rdt_last_cmd_puts("out of CLOSIDs\n"); | |
65b4f403 | 1741 | goto out_common_fail; |
cfd0f34e | 1742 | } |
65b4f403 | 1743 | closid = ret; |
3d74ade8 | 1744 | ret = 0; |
65b4f403 VS |
1745 | |
1746 | rdtgrp->closid = closid; | |
1747 | list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); | |
1748 | ||
c7d9aac6 VS |
1749 | if (rdt_mon_capable) { |
1750 | /* | |
1751 | * Create an empty mon_groups directory to hold the subset | |
1752 | * of tasks and cpus to monitor. | |
1753 | */ | |
1754 | ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL); | |
cfd0f34e TL |
1755 | if (ret) { |
1756 | rdt_last_cmd_puts("kernfs subdir error\n"); | |
c7d9aac6 | 1757 | goto out_id_free; |
cfd0f34e | 1758 | } |
c7d9aac6 VS |
1759 | } |
1760 | ||
65b4f403 VS |
1761 | goto out_unlock; |
1762 | ||
c7d9aac6 VS |
1763 | out_id_free: |
1764 | closid_free(closid); | |
1765 | list_del(&rdtgrp->rdtgroup_list); | |
65b4f403 VS |
1766 | out_common_fail: |
1767 | mkdir_rdt_prepare_clean(rdtgrp); | |
1768 | out_unlock: | |
1769 | rdtgroup_kn_unlock(prgrp_kn); | |
60cf5e10 FY |
1770 | return ret; |
1771 | } | |
1772 | ||
c7d9aac6 VS |
1773 | /* |
1774 | * We allow creating mon groups only with in a directory called "mon_groups" | |
1775 | * which is present in every ctrl_mon group. Check if this is a valid | |
1776 | * "mon_groups" directory. | |
1777 | * | |
1778 | * 1. The directory should be named "mon_groups". | |
1779 | * 2. The mon group itself should "not" be named "mon_groups". | |
1780 | * This makes sure "mon_groups" directory always has a ctrl_mon group | |
1781 | * as parent. | |
1782 | */ | |
1783 | static bool is_mon_groups(struct kernfs_node *kn, const char *name) | |
1784 | { | |
1785 | return (!strcmp(kn->name, "mon_groups") && | |
1786 | strcmp(name, "mon_groups")); | |
1787 | } | |
1788 | ||
65b4f403 VS |
1789 | static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
1790 | umode_t mode) | |
1791 | { | |
1792 | /* Do not accept '\n' to avoid unparsable situation. */ | |
1793 | if (strchr(name, '\n')) | |
1794 | return -EINVAL; | |
1795 | ||
1796 | /* | |
1797 | * If the parent directory is the root directory and RDT | |
c7d9aac6 VS |
1798 | * allocation is supported, add a control and monitoring |
1799 | * subdirectory | |
65b4f403 VS |
1800 | */ |
1801 | if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn) | |
c7d9aac6 VS |
1802 | return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode); |
1803 | ||
1804 | /* | |
1805 | * If RDT monitoring is supported and the parent directory is a valid | |
1806 | * "mon_groups" directory, add a monitoring subdirectory. | |
1807 | */ | |
1808 | if (rdt_mon_capable && is_mon_groups(parent_kn, name)) | |
1809 | return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode); | |
65b4f403 VS |
1810 | |
1811 | return -EPERM; | |
1812 | } | |
1813 | ||
f3cbeaca VS |
1814 | static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp, |
1815 | cpumask_var_t tmpmask) | |
1816 | { | |
1817 | struct rdtgroup *prdtgrp = rdtgrp->mon.parent; | |
1818 | int cpu; | |
1819 | ||
1820 | /* Give any tasks back to the parent group */ | |
1821 | rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); | |
1822 | ||
1823 | /* Update per cpu rmid of the moved CPUs first */ | |
1824 | for_each_cpu(cpu, &rdtgrp->cpu_mask) | |
a9110b55 | 1825 | per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid; |
f3cbeaca VS |
1826 | /* |
1827 | * Update the MSR on moved CPUs and CPUs which have moved | |
1828 | * task running on them. | |
1829 | */ | |
1830 | cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); | |
1831 | update_closid_rmid(tmpmask, NULL); | |
1832 | ||
1833 | rdtgrp->flags = RDT_DELETED; | |
1834 | free_rmid(rdtgrp->mon.rmid); | |
1835 | ||
1836 | /* | |
1837 | * Remove the rdtgrp from the parent ctrl_mon group's list | |
1838 | */ | |
1839 | WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); | |
1840 | list_del(&rdtgrp->mon.crdtgrp_list); | |
1841 | ||
1842 | /* | |
1843 | * one extra hold on this, will drop when we kfree(rdtgrp) | |
1844 | * in rdtgroup_kn_unlock() | |
1845 | */ | |
1846 | kernfs_get(kn); | |
1847 | kernfs_remove(rdtgrp->kn); | |
1848 | ||
1849 | return 0; | |
1850 | } | |
1851 | ||
f9049547 VS |
1852 | static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp, |
1853 | cpumask_var_t tmpmask) | |
60cf5e10 | 1854 | { |
f9049547 | 1855 | int cpu; |
60cf5e10 | 1856 | |
e02737d5 | 1857 | /* Give any tasks back to the default group */ |
0efc89be | 1858 | rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); |
e02737d5 | 1859 | |
12e0110c TL |
1860 | /* Give any CPUs back to the default group */ |
1861 | cpumask_or(&rdtgroup_default.cpu_mask, | |
1862 | &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); | |
0efc89be | 1863 | |
f3cbeaca VS |
1864 | /* Update per cpu closid and rmid of the moved CPUs first */ |
1865 | for_each_cpu(cpu, &rdtgrp->cpu_mask) { | |
a9110b55 VS |
1866 | per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid; |
1867 | per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid; | |
f3cbeaca VS |
1868 | } |
1869 | ||
0efc89be FY |
1870 | /* |
1871 | * Update the MSR on moved CPUs and CPUs which have moved | |
1872 | * task running on them. | |
1873 | */ | |
1874 | cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); | |
a9fcf862 | 1875 | update_closid_rmid(tmpmask, NULL); |
12e0110c | 1876 | |
60cf5e10 FY |
1877 | rdtgrp->flags = RDT_DELETED; |
1878 | closid_free(rdtgrp->closid); | |
f3cbeaca VS |
1879 | free_rmid(rdtgrp->mon.rmid); |
1880 | ||
1881 | /* | |
1882 | * Free all the child monitor group rmids. | |
1883 | */ | |
1884 | free_all_child_rdtgrp(rdtgrp); | |
1885 | ||
60cf5e10 FY |
1886 | list_del(&rdtgrp->rdtgroup_list); |
1887 | ||
1888 | /* | |
1889 | * one extra hold on this, will drop when we kfree(rdtgrp) | |
1890 | * in rdtgroup_kn_unlock() | |
1891 | */ | |
1892 | kernfs_get(kn); | |
1893 | kernfs_remove(rdtgrp->kn); | |
f9049547 VS |
1894 | |
1895 | return 0; | |
1896 | } | |
1897 | ||
1898 | static int rdtgroup_rmdir(struct kernfs_node *kn) | |
1899 | { | |
1900 | struct kernfs_node *parent_kn = kn->parent; | |
1901 | struct rdtgroup *rdtgrp; | |
1902 | cpumask_var_t tmpmask; | |
1903 | int ret = 0; | |
1904 | ||
1905 | if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) | |
1906 | return -ENOMEM; | |
1907 | ||
1908 | rdtgrp = rdtgroup_kn_lock_live(kn); | |
1909 | if (!rdtgrp) { | |
1910 | ret = -EPERM; | |
1911 | goto out; | |
1912 | } | |
1913 | ||
1914 | /* | |
1915 | * If the rdtgroup is a ctrl_mon group and parent directory | |
f3cbeaca VS |
1916 | * is the root directory, remove the ctrl_mon group. |
1917 | * | |
1918 | * If the rdtgroup is a mon group and parent directory | |
1919 | * is a valid "mon_groups" directory, remove the mon group. | |
f9049547 VS |
1920 | */ |
1921 | if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn) | |
1922 | ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask); | |
f3cbeaca VS |
1923 | else if (rdtgrp->type == RDTMON_GROUP && |
1924 | is_mon_groups(parent_kn, kn->name)) | |
1925 | ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask); | |
f9049547 VS |
1926 | else |
1927 | ret = -EPERM; | |
1928 | ||
0efc89be | 1929 | out: |
60cf5e10 | 1930 | rdtgroup_kn_unlock(kn); |
0efc89be FY |
1931 | free_cpumask_var(tmpmask); |
1932 | return ret; | |
60cf5e10 FY |
1933 | } |
1934 | ||
76ae054c SL |
1935 | static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) |
1936 | { | |
1b5c0b75 | 1937 | if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) |
76ae054c SL |
1938 | seq_puts(seq, ",cdp"); |
1939 | return 0; | |
1940 | } | |
1941 | ||
5ff193fb | 1942 | static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { |
76ae054c SL |
1943 | .mkdir = rdtgroup_mkdir, |
1944 | .rmdir = rdtgroup_rmdir, | |
1945 | .show_options = rdtgroup_show_options, | |
5ff193fb FY |
1946 | }; |
1947 | ||
1948 | static int __init rdtgroup_setup_root(void) | |
1949 | { | |
12e0110c TL |
1950 | int ret; |
1951 | ||
5ff193fb FY |
1952 | rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, |
1953 | KERNFS_ROOT_CREATE_DEACTIVATED, | |
1954 | &rdtgroup_default); | |
1955 | if (IS_ERR(rdt_root)) | |
1956 | return PTR_ERR(rdt_root); | |
1957 | ||
1958 | mutex_lock(&rdtgroup_mutex); | |
1959 | ||
1960 | rdtgroup_default.closid = 0; | |
c7d9aac6 VS |
1961 | rdtgroup_default.mon.rmid = 0; |
1962 | rdtgroup_default.type = RDTCTRL_GROUP; | |
1963 | INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); | |
1964 | ||
5ff193fb FY |
1965 | list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); |
1966 | ||
5dc1d5c6 | 1967 | ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE); |
12e0110c TL |
1968 | if (ret) { |
1969 | kernfs_destroy_root(rdt_root); | |
1970 | goto out; | |
1971 | } | |
1972 | ||
5ff193fb FY |
1973 | rdtgroup_default.kn = rdt_root->kn; |
1974 | kernfs_activate(rdtgroup_default.kn); | |
1975 | ||
12e0110c | 1976 | out: |
5ff193fb FY |
1977 | mutex_unlock(&rdtgroup_mutex); |
1978 | ||
12e0110c | 1979 | return ret; |
5ff193fb FY |
1980 | } |
1981 | ||
1982 | /* | |
1983 | * rdtgroup_init - rdtgroup initialization | |
1984 | * | |
1985 | * Setup resctrl file system including set up root, create mount point, | |
1986 | * register rdtgroup filesystem, and initialize files under root directory. | |
1987 | * | |
1988 | * Return: 0 on success or -errno | |
1989 | */ | |
1990 | int __init rdtgroup_init(void) | |
1991 | { | |
1992 | int ret = 0; | |
1993 | ||
9b3a7fd0 TL |
1994 | seq_buf_init(&last_cmd_status, last_cmd_status_buf, |
1995 | sizeof(last_cmd_status_buf)); | |
1996 | ||
5ff193fb FY |
1997 | ret = rdtgroup_setup_root(); |
1998 | if (ret) | |
1999 | return ret; | |
2000 | ||
2001 | ret = sysfs_create_mount_point(fs_kobj, "resctrl"); | |
2002 | if (ret) | |
2003 | goto cleanup_root; | |
2004 | ||
2005 | ret = register_filesystem(&rdt_fs_type); | |
2006 | if (ret) | |
2007 | goto cleanup_mountpoint; | |
2008 | ||
2009 | return 0; | |
2010 | ||
2011 | cleanup_mountpoint: | |
2012 | sysfs_remove_mount_point(fs_kobj, "resctrl"); | |
2013 | cleanup_root: | |
2014 | kernfs_destroy_root(rdt_root); | |
2015 | ||
2016 | return ret; | |
2017 | } |