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 | ||
024d15be | 124 | void closid_free(int closid) |
60cf5e10 FY |
125 | { |
126 | closid_free_map |= 1 << closid; | |
127 | } | |
128 | ||
0b9aa656 RC |
129 | /** |
130 | * closid_allocated - test if provided closid is in use | |
131 | * @closid: closid to be tested | |
132 | * | |
133 | * Return: true if @closid is currently associated with a resource group, | |
134 | * false if @closid is free | |
135 | */ | |
95f0b77e | 136 | static bool closid_allocated(unsigned int closid) |
0b9aa656 RC |
137 | { |
138 | return (closid_free_map & (1 << closid)) == 0; | |
139 | } | |
140 | ||
472ef09b RC |
141 | /** |
142 | * rdtgroup_mode_by_closid - Return mode of resource group with closid | |
143 | * @closid: closid if the resource group | |
144 | * | |
145 | * Each resource group is associated with a @closid. Here the mode | |
146 | * of a resource group can be queried by searching for it using its closid. | |
147 | * | |
148 | * Return: mode as &enum rdtgrp_mode of resource group with closid @closid | |
149 | */ | |
150 | enum rdtgrp_mode rdtgroup_mode_by_closid(int closid) | |
151 | { | |
152 | struct rdtgroup *rdtgrp; | |
153 | ||
154 | list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { | |
155 | if (rdtgrp->closid == closid) | |
156 | return rdtgrp->mode; | |
157 | } | |
158 | ||
159 | return RDT_NUM_MODES; | |
160 | } | |
161 | ||
d48d7a57 RC |
162 | static const char * const rdt_mode_str[] = { |
163 | [RDT_MODE_SHAREABLE] = "shareable", | |
414dd2b4 | 164 | [RDT_MODE_EXCLUSIVE] = "exclusive", |
d48d7a57 RC |
165 | }; |
166 | ||
167 | /** | |
168 | * rdtgroup_mode_str - Return the string representation of mode | |
169 | * @mode: the resource group mode as &enum rdtgroup_mode | |
170 | * | |
171 | * Return: string representation of valid mode, "unknown" otherwise | |
172 | */ | |
173 | static const char *rdtgroup_mode_str(enum rdtgrp_mode mode) | |
174 | { | |
175 | if (mode < RDT_MODE_SHAREABLE || mode >= RDT_NUM_MODES) | |
176 | return "unknown"; | |
177 | ||
178 | return rdt_mode_str[mode]; | |
179 | } | |
180 | ||
4e978d06 FY |
181 | /* set uid and gid of rdtgroup dirs and files to that of the creator */ |
182 | static int rdtgroup_kn_set_ugid(struct kernfs_node *kn) | |
183 | { | |
184 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, | |
185 | .ia_uid = current_fsuid(), | |
186 | .ia_gid = current_fsgid(), }; | |
187 | ||
188 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && | |
189 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) | |
190 | return 0; | |
191 | ||
192 | return kernfs_setattr(kn, &iattr); | |
193 | } | |
194 | ||
195 | static int rdtgroup_add_file(struct kernfs_node *parent_kn, struct rftype *rft) | |
196 | { | |
197 | struct kernfs_node *kn; | |
198 | int ret; | |
199 | ||
200 | kn = __kernfs_create_file(parent_kn, rft->name, rft->mode, | |
201 | 0, rft->kf_ops, rft, NULL, NULL); | |
202 | if (IS_ERR(kn)) | |
203 | return PTR_ERR(kn); | |
204 | ||
205 | ret = rdtgroup_kn_set_ugid(kn); | |
206 | if (ret) { | |
207 | kernfs_remove(kn); | |
208 | return ret; | |
209 | } | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
4e978d06 FY |
214 | static int rdtgroup_seqfile_show(struct seq_file *m, void *arg) |
215 | { | |
216 | struct kernfs_open_file *of = m->private; | |
217 | struct rftype *rft = of->kn->priv; | |
218 | ||
219 | if (rft->seq_show) | |
220 | return rft->seq_show(of, m, arg); | |
221 | return 0; | |
222 | } | |
223 | ||
224 | static ssize_t rdtgroup_file_write(struct kernfs_open_file *of, char *buf, | |
225 | size_t nbytes, loff_t off) | |
226 | { | |
227 | struct rftype *rft = of->kn->priv; | |
228 | ||
229 | if (rft->write) | |
230 | return rft->write(of, buf, nbytes, off); | |
231 | ||
232 | return -EINVAL; | |
233 | } | |
234 | ||
235 | static struct kernfs_ops rdtgroup_kf_single_ops = { | |
236 | .atomic_write_len = PAGE_SIZE, | |
237 | .write = rdtgroup_file_write, | |
238 | .seq_show = rdtgroup_seqfile_show, | |
239 | }; | |
240 | ||
d89b7379 VS |
241 | static struct kernfs_ops kf_mondata_ops = { |
242 | .atomic_write_len = PAGE_SIZE, | |
243 | .seq_show = rdtgroup_mondata_show, | |
244 | }; | |
245 | ||
4ffa3c97 JO |
246 | static bool is_cpu_list(struct kernfs_open_file *of) |
247 | { | |
248 | struct rftype *rft = of->kn->priv; | |
249 | ||
250 | return rft->flags & RFTYPE_FLAGS_CPUS_LIST; | |
251 | } | |
252 | ||
12e0110c TL |
253 | static int rdtgroup_cpus_show(struct kernfs_open_file *of, |
254 | struct seq_file *s, void *v) | |
255 | { | |
256 | struct rdtgroup *rdtgrp; | |
257 | int ret = 0; | |
258 | ||
259 | rdtgrp = rdtgroup_kn_lock_live(of->kn); | |
260 | ||
4ffa3c97 JO |
261 | if (rdtgrp) { |
262 | seq_printf(s, is_cpu_list(of) ? "%*pbl\n" : "%*pb\n", | |
263 | cpumask_pr_args(&rdtgrp->cpu_mask)); | |
264 | } else { | |
12e0110c | 265 | ret = -ENOENT; |
4ffa3c97 | 266 | } |
12e0110c TL |
267 | rdtgroup_kn_unlock(of->kn); |
268 | ||
269 | return ret; | |
270 | } | |
271 | ||
f4107702 FY |
272 | /* |
273 | * This is safe against intel_rdt_sched_in() called from __switch_to() | |
274 | * because __switch_to() is executed with interrupts disabled. A local call | |
a9fcf862 | 275 | * from update_closid_rmid() is proteced against __switch_to() because |
f4107702 FY |
276 | * preemption is disabled. |
277 | */ | |
a9fcf862 | 278 | static void update_cpu_closid_rmid(void *info) |
f4107702 | 279 | { |
b09d981b VS |
280 | struct rdtgroup *r = info; |
281 | ||
a9fcf862 | 282 | if (r) { |
a9110b55 VS |
283 | this_cpu_write(pqr_state.default_closid, r->closid); |
284 | this_cpu_write(pqr_state.default_rmid, r->mon.rmid); | |
a9fcf862 | 285 | } |
b09d981b | 286 | |
f4107702 FY |
287 | /* |
288 | * We cannot unconditionally write the MSR because the current | |
289 | * executing task might have its own closid selected. Just reuse | |
290 | * the context switch code. | |
291 | */ | |
292 | intel_rdt_sched_in(); | |
293 | } | |
294 | ||
0efc89be FY |
295 | /* |
296 | * Update the PGR_ASSOC MSR on all cpus in @cpu_mask, | |
297 | * | |
b09d981b | 298 | * Per task closids/rmids must have been set up before calling this function. |
0efc89be FY |
299 | */ |
300 | static void | |
a9fcf862 | 301 | update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r) |
f4107702 FY |
302 | { |
303 | int cpu = get_cpu(); | |
304 | ||
305 | if (cpumask_test_cpu(cpu, cpu_mask)) | |
a9fcf862 VS |
306 | update_cpu_closid_rmid(r); |
307 | smp_call_function_many(cpu_mask, update_cpu_closid_rmid, r, 1); | |
f4107702 FY |
308 | put_cpu(); |
309 | } | |
310 | ||
a9fcf862 VS |
311 | static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, |
312 | cpumask_var_t tmpmask) | |
313 | { | |
314 | struct rdtgroup *prgrp = rdtgrp->mon.parent, *crgrp; | |
315 | struct list_head *head; | |
316 | ||
317 | /* Check whether cpus belong to parent ctrl group */ | |
318 | cpumask_andnot(tmpmask, newmask, &prgrp->cpu_mask); | |
94457b36 TL |
319 | if (cpumask_weight(tmpmask)) { |
320 | rdt_last_cmd_puts("can only add CPUs to mongroup that belong to parent\n"); | |
a9fcf862 | 321 | return -EINVAL; |
94457b36 | 322 | } |
a9fcf862 VS |
323 | |
324 | /* Check whether cpus are dropped from this group */ | |
325 | cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); | |
326 | if (cpumask_weight(tmpmask)) { | |
327 | /* Give any dropped cpus to parent rdtgroup */ | |
328 | cpumask_or(&prgrp->cpu_mask, &prgrp->cpu_mask, tmpmask); | |
329 | update_closid_rmid(tmpmask, prgrp); | |
330 | } | |
331 | ||
332 | /* | |
333 | * If we added cpus, remove them from previous group that owned them | |
334 | * and update per-cpu rmid | |
335 | */ | |
336 | cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); | |
337 | if (cpumask_weight(tmpmask)) { | |
338 | head = &prgrp->mon.crdtgrp_list; | |
339 | list_for_each_entry(crgrp, head, mon.crdtgrp_list) { | |
340 | if (crgrp == rdtgrp) | |
341 | continue; | |
342 | cpumask_andnot(&crgrp->cpu_mask, &crgrp->cpu_mask, | |
343 | tmpmask); | |
344 | } | |
345 | update_closid_rmid(tmpmask, rdtgrp); | |
346 | } | |
347 | ||
348 | /* Done pushing/pulling - update this group with new mask */ | |
349 | cpumask_copy(&rdtgrp->cpu_mask, newmask); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
354 | static void cpumask_rdtgrp_clear(struct rdtgroup *r, struct cpumask *m) | |
355 | { | |
356 | struct rdtgroup *crgrp; | |
357 | ||
358 | cpumask_andnot(&r->cpu_mask, &r->cpu_mask, m); | |
359 | /* update the child mon group masks as well*/ | |
360 | list_for_each_entry(crgrp, &r->mon.crdtgrp_list, mon.crdtgrp_list) | |
361 | cpumask_and(&crgrp->cpu_mask, &r->cpu_mask, &crgrp->cpu_mask); | |
362 | } | |
363 | ||
b09d981b | 364 | static int cpus_ctrl_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask, |
a9fcf862 | 365 | cpumask_var_t tmpmask, cpumask_var_t tmpmask1) |
b09d981b | 366 | { |
a9fcf862 VS |
367 | struct rdtgroup *r, *crgrp; |
368 | struct list_head *head; | |
b09d981b VS |
369 | |
370 | /* Check whether cpus are dropped from this group */ | |
371 | cpumask_andnot(tmpmask, &rdtgrp->cpu_mask, newmask); | |
372 | if (cpumask_weight(tmpmask)) { | |
373 | /* Can't drop from default group */ | |
94457b36 TL |
374 | if (rdtgrp == &rdtgroup_default) { |
375 | rdt_last_cmd_puts("Can't drop CPUs from default group\n"); | |
b09d981b | 376 | return -EINVAL; |
94457b36 | 377 | } |
b09d981b VS |
378 | |
379 | /* Give any dropped cpus to rdtgroup_default */ | |
380 | cpumask_or(&rdtgroup_default.cpu_mask, | |
381 | &rdtgroup_default.cpu_mask, tmpmask); | |
a9fcf862 | 382 | update_closid_rmid(tmpmask, &rdtgroup_default); |
b09d981b VS |
383 | } |
384 | ||
385 | /* | |
a9fcf862 VS |
386 | * If we added cpus, remove them from previous group and |
387 | * the prev group's child groups that owned them | |
388 | * and update per-cpu closid/rmid. | |
b09d981b VS |
389 | */ |
390 | cpumask_andnot(tmpmask, newmask, &rdtgrp->cpu_mask); | |
391 | if (cpumask_weight(tmpmask)) { | |
392 | list_for_each_entry(r, &rdt_all_groups, rdtgroup_list) { | |
393 | if (r == rdtgrp) | |
394 | continue; | |
a9fcf862 VS |
395 | cpumask_and(tmpmask1, &r->cpu_mask, tmpmask); |
396 | if (cpumask_weight(tmpmask1)) | |
397 | cpumask_rdtgrp_clear(r, tmpmask1); | |
b09d981b | 398 | } |
a9fcf862 | 399 | update_closid_rmid(tmpmask, rdtgrp); |
b09d981b VS |
400 | } |
401 | ||
402 | /* Done pushing/pulling - update this group with new mask */ | |
403 | cpumask_copy(&rdtgrp->cpu_mask, newmask); | |
404 | ||
a9fcf862 VS |
405 | /* |
406 | * Clear child mon group masks since there is a new parent mask | |
407 | * now and update the rmid for the cpus the child lost. | |
408 | */ | |
409 | head = &rdtgrp->mon.crdtgrp_list; | |
410 | list_for_each_entry(crgrp, head, mon.crdtgrp_list) { | |
411 | cpumask_and(tmpmask, &rdtgrp->cpu_mask, &crgrp->cpu_mask); | |
412 | update_closid_rmid(tmpmask, rdtgrp); | |
413 | cpumask_clear(&crgrp->cpu_mask); | |
414 | } | |
415 | ||
b09d981b VS |
416 | return 0; |
417 | } | |
418 | ||
12e0110c TL |
419 | static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of, |
420 | char *buf, size_t nbytes, loff_t off) | |
421 | { | |
a9fcf862 | 422 | cpumask_var_t tmpmask, newmask, tmpmask1; |
b09d981b | 423 | struct rdtgroup *rdtgrp; |
f4107702 | 424 | int ret; |
12e0110c TL |
425 | |
426 | if (!buf) | |
427 | return -EINVAL; | |
428 | ||
429 | if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) | |
430 | return -ENOMEM; | |
431 | if (!zalloc_cpumask_var(&newmask, GFP_KERNEL)) { | |
432 | free_cpumask_var(tmpmask); | |
433 | return -ENOMEM; | |
434 | } | |
a9fcf862 VS |
435 | if (!zalloc_cpumask_var(&tmpmask1, GFP_KERNEL)) { |
436 | free_cpumask_var(tmpmask); | |
437 | free_cpumask_var(newmask); | |
438 | return -ENOMEM; | |
439 | } | |
a2584e1d | 440 | |
12e0110c | 441 | rdtgrp = rdtgroup_kn_lock_live(of->kn); |
94457b36 | 442 | rdt_last_cmd_clear(); |
12e0110c TL |
443 | if (!rdtgrp) { |
444 | ret = -ENOENT; | |
94457b36 | 445 | rdt_last_cmd_puts("directory was removed\n"); |
12e0110c TL |
446 | goto unlock; |
447 | } | |
448 | ||
4ffa3c97 JO |
449 | if (is_cpu_list(of)) |
450 | ret = cpulist_parse(buf, newmask); | |
451 | else | |
452 | ret = cpumask_parse(buf, newmask); | |
453 | ||
94457b36 TL |
454 | if (ret) { |
455 | rdt_last_cmd_puts("bad cpu list/mask\n"); | |
12e0110c | 456 | goto unlock; |
94457b36 | 457 | } |
12e0110c | 458 | |
12e0110c TL |
459 | /* check that user didn't specify any offline cpus */ |
460 | cpumask_andnot(tmpmask, newmask, cpu_online_mask); | |
461 | if (cpumask_weight(tmpmask)) { | |
462 | ret = -EINVAL; | |
94457b36 | 463 | rdt_last_cmd_puts("can only assign online cpus\n"); |
a2584e1d | 464 | goto unlock; |
12e0110c TL |
465 | } |
466 | ||
b09d981b | 467 | if (rdtgrp->type == RDTCTRL_GROUP) |
a9fcf862 VS |
468 | ret = cpus_ctrl_write(rdtgrp, newmask, tmpmask, tmpmask1); |
469 | else if (rdtgrp->type == RDTMON_GROUP) | |
470 | ret = cpus_mon_write(rdtgrp, newmask, tmpmask); | |
b09d981b VS |
471 | else |
472 | ret = -EINVAL; | |
12e0110c | 473 | |
12e0110c TL |
474 | unlock: |
475 | rdtgroup_kn_unlock(of->kn); | |
476 | free_cpumask_var(tmpmask); | |
477 | free_cpumask_var(newmask); | |
a9fcf862 | 478 | free_cpumask_var(tmpmask1); |
12e0110c TL |
479 | |
480 | return ret ?: nbytes; | |
481 | } | |
482 | ||
e02737d5 FY |
483 | struct task_move_callback { |
484 | struct callback_head work; | |
485 | struct rdtgroup *rdtgrp; | |
486 | }; | |
487 | ||
488 | static void move_myself(struct callback_head *head) | |
489 | { | |
490 | struct task_move_callback *callback; | |
491 | struct rdtgroup *rdtgrp; | |
492 | ||
493 | callback = container_of(head, struct task_move_callback, work); | |
494 | rdtgrp = callback->rdtgrp; | |
495 | ||
496 | /* | |
497 | * If resource group was deleted before this task work callback | |
498 | * was invoked, then assign the task to root group and free the | |
499 | * resource group. | |
500 | */ | |
501 | if (atomic_dec_and_test(&rdtgrp->waitcount) && | |
502 | (rdtgrp->flags & RDT_DELETED)) { | |
503 | current->closid = 0; | |
d6aaba61 | 504 | current->rmid = 0; |
e02737d5 FY |
505 | kfree(rdtgrp); |
506 | } | |
507 | ||
74fcdae1 | 508 | preempt_disable(); |
4f341a5e FY |
509 | /* update PQR_ASSOC MSR to make resource group go into effect */ |
510 | intel_rdt_sched_in(); | |
74fcdae1 | 511 | preempt_enable(); |
4f341a5e | 512 | |
e02737d5 FY |
513 | kfree(callback); |
514 | } | |
515 | ||
516 | static int __rdtgroup_move_task(struct task_struct *tsk, | |
517 | struct rdtgroup *rdtgrp) | |
518 | { | |
519 | struct task_move_callback *callback; | |
520 | int ret; | |
521 | ||
522 | callback = kzalloc(sizeof(*callback), GFP_KERNEL); | |
523 | if (!callback) | |
524 | return -ENOMEM; | |
525 | callback->work.func = move_myself; | |
526 | callback->rdtgrp = rdtgrp; | |
527 | ||
528 | /* | |
529 | * Take a refcount, so rdtgrp cannot be freed before the | |
530 | * callback has been invoked. | |
531 | */ | |
532 | atomic_inc(&rdtgrp->waitcount); | |
533 | ret = task_work_add(tsk, &callback->work, true); | |
534 | if (ret) { | |
535 | /* | |
536 | * Task is exiting. Drop the refcount and free the callback. | |
537 | * No need to check the refcount as the group cannot be | |
538 | * deleted before the write function unlocks rdtgroup_mutex. | |
539 | */ | |
540 | atomic_dec(&rdtgrp->waitcount); | |
541 | kfree(callback); | |
29e74f35 | 542 | rdt_last_cmd_puts("task exited\n"); |
e02737d5 | 543 | } else { |
d6aaba61 VS |
544 | /* |
545 | * For ctrl_mon groups move both closid and rmid. | |
546 | * For monitor groups, can move the tasks only from | |
547 | * their parent CTRL group. | |
548 | */ | |
549 | if (rdtgrp->type == RDTCTRL_GROUP) { | |
550 | tsk->closid = rdtgrp->closid; | |
551 | tsk->rmid = rdtgrp->mon.rmid; | |
552 | } else if (rdtgrp->type == RDTMON_GROUP) { | |
29e74f35 | 553 | if (rdtgrp->mon.parent->closid == tsk->closid) { |
d6aaba61 | 554 | tsk->rmid = rdtgrp->mon.rmid; |
29e74f35 TL |
555 | } else { |
556 | rdt_last_cmd_puts("Can't move task to different control group\n"); | |
d6aaba61 | 557 | ret = -EINVAL; |
29e74f35 | 558 | } |
d6aaba61 | 559 | } |
e02737d5 FY |
560 | } |
561 | return ret; | |
562 | } | |
563 | ||
564 | static int rdtgroup_task_write_permission(struct task_struct *task, | |
565 | struct kernfs_open_file *of) | |
566 | { | |
567 | const struct cred *tcred = get_task_cred(task); | |
568 | const struct cred *cred = current_cred(); | |
569 | int ret = 0; | |
570 | ||
571 | /* | |
572 | * Even if we're attaching all tasks in the thread group, we only | |
573 | * need to check permissions on one of them. | |
574 | */ | |
575 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && | |
576 | !uid_eq(cred->euid, tcred->uid) && | |
29e74f35 TL |
577 | !uid_eq(cred->euid, tcred->suid)) { |
578 | rdt_last_cmd_printf("No permission to move task %d\n", task->pid); | |
e02737d5 | 579 | ret = -EPERM; |
29e74f35 | 580 | } |
e02737d5 FY |
581 | |
582 | put_cred(tcred); | |
583 | return ret; | |
584 | } | |
585 | ||
586 | static int rdtgroup_move_task(pid_t pid, struct rdtgroup *rdtgrp, | |
587 | struct kernfs_open_file *of) | |
588 | { | |
589 | struct task_struct *tsk; | |
590 | int ret; | |
591 | ||
592 | rcu_read_lock(); | |
593 | if (pid) { | |
594 | tsk = find_task_by_vpid(pid); | |
595 | if (!tsk) { | |
596 | rcu_read_unlock(); | |
29e74f35 | 597 | rdt_last_cmd_printf("No task %d\n", pid); |
e02737d5 FY |
598 | return -ESRCH; |
599 | } | |
600 | } else { | |
601 | tsk = current; | |
602 | } | |
603 | ||
604 | get_task_struct(tsk); | |
605 | rcu_read_unlock(); | |
606 | ||
607 | ret = rdtgroup_task_write_permission(tsk, of); | |
608 | if (!ret) | |
609 | ret = __rdtgroup_move_task(tsk, rdtgrp); | |
610 | ||
611 | put_task_struct(tsk); | |
612 | return ret; | |
613 | } | |
614 | ||
615 | static ssize_t rdtgroup_tasks_write(struct kernfs_open_file *of, | |
616 | char *buf, size_t nbytes, loff_t off) | |
617 | { | |
618 | struct rdtgroup *rdtgrp; | |
619 | int ret = 0; | |
620 | pid_t pid; | |
621 | ||
622 | if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) | |
623 | return -EINVAL; | |
624 | rdtgrp = rdtgroup_kn_lock_live(of->kn); | |
29e74f35 | 625 | rdt_last_cmd_clear(); |
e02737d5 FY |
626 | |
627 | if (rdtgrp) | |
628 | ret = rdtgroup_move_task(pid, rdtgrp, of); | |
629 | else | |
630 | ret = -ENOENT; | |
631 | ||
632 | rdtgroup_kn_unlock(of->kn); | |
633 | ||
634 | return ret ?: nbytes; | |
635 | } | |
636 | ||
637 | static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s) | |
638 | { | |
639 | struct task_struct *p, *t; | |
640 | ||
641 | rcu_read_lock(); | |
642 | for_each_process_thread(p, t) { | |
d6aaba61 VS |
643 | if ((r->type == RDTCTRL_GROUP && t->closid == r->closid) || |
644 | (r->type == RDTMON_GROUP && t->rmid == r->mon.rmid)) | |
e02737d5 FY |
645 | seq_printf(s, "%d\n", t->pid); |
646 | } | |
647 | rcu_read_unlock(); | |
648 | } | |
649 | ||
650 | static int rdtgroup_tasks_show(struct kernfs_open_file *of, | |
651 | struct seq_file *s, void *v) | |
652 | { | |
653 | struct rdtgroup *rdtgrp; | |
654 | int ret = 0; | |
655 | ||
656 | rdtgrp = rdtgroup_kn_lock_live(of->kn); | |
657 | if (rdtgrp) | |
658 | show_rdt_tasks(rdtgrp, s); | |
659 | else | |
660 | ret = -ENOENT; | |
661 | rdtgroup_kn_unlock(of->kn); | |
662 | ||
663 | return ret; | |
664 | } | |
665 | ||
9b3a7fd0 TL |
666 | static int rdt_last_cmd_status_show(struct kernfs_open_file *of, |
667 | struct seq_file *seq, void *v) | |
668 | { | |
669 | int len; | |
670 | ||
671 | mutex_lock(&rdtgroup_mutex); | |
672 | len = seq_buf_used(&last_cmd_status); | |
673 | if (len) | |
674 | seq_printf(seq, "%.*s", len, last_cmd_status_buf); | |
675 | else | |
676 | seq_puts(seq, "ok\n"); | |
677 | mutex_unlock(&rdtgroup_mutex); | |
678 | return 0; | |
679 | } | |
680 | ||
4e978d06 FY |
681 | static int rdt_num_closids_show(struct kernfs_open_file *of, |
682 | struct seq_file *seq, void *v) | |
683 | { | |
684 | struct rdt_resource *r = of->kn->parent->priv; | |
685 | ||
686 | seq_printf(seq, "%d\n", r->num_closid); | |
4e978d06 FY |
687 | return 0; |
688 | } | |
689 | ||
2545e9f5 | 690 | static int rdt_default_ctrl_show(struct kernfs_open_file *of, |
4e978d06 FY |
691 | struct seq_file *seq, void *v) |
692 | { | |
693 | struct rdt_resource *r = of->kn->parent->priv; | |
694 | ||
2545e9f5 | 695 | seq_printf(seq, "%x\n", r->default_ctrl); |
4e978d06 FY |
696 | return 0; |
697 | } | |
698 | ||
53a114a6 SL |
699 | static int rdt_min_cbm_bits_show(struct kernfs_open_file *of, |
700 | struct seq_file *seq, void *v) | |
701 | { | |
702 | struct rdt_resource *r = of->kn->parent->priv; | |
703 | ||
d3e11b4d | 704 | seq_printf(seq, "%u\n", r->cache.min_cbm_bits); |
db69ef65 VS |
705 | return 0; |
706 | } | |
707 | ||
0dd2d749 FY |
708 | static int rdt_shareable_bits_show(struct kernfs_open_file *of, |
709 | struct seq_file *seq, void *v) | |
710 | { | |
711 | struct rdt_resource *r = of->kn->parent->priv; | |
712 | ||
713 | seq_printf(seq, "%x\n", r->cache.shareable_bits); | |
714 | return 0; | |
715 | } | |
716 | ||
db69ef65 VS |
717 | static int rdt_min_bw_show(struct kernfs_open_file *of, |
718 | struct seq_file *seq, void *v) | |
719 | { | |
720 | struct rdt_resource *r = of->kn->parent->priv; | |
53a114a6 | 721 | |
db69ef65 VS |
722 | seq_printf(seq, "%u\n", r->membw.min_bw); |
723 | return 0; | |
724 | } | |
725 | ||
d4ab3320 VS |
726 | static int rdt_num_rmids_show(struct kernfs_open_file *of, |
727 | struct seq_file *seq, void *v) | |
728 | { | |
729 | struct rdt_resource *r = of->kn->parent->priv; | |
730 | ||
731 | seq_printf(seq, "%d\n", r->num_rmid); | |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
736 | static int rdt_mon_features_show(struct kernfs_open_file *of, | |
737 | struct seq_file *seq, void *v) | |
738 | { | |
739 | struct rdt_resource *r = of->kn->parent->priv; | |
740 | struct mon_evt *mevt; | |
741 | ||
742 | list_for_each_entry(mevt, &r->evt_list, list) | |
743 | seq_printf(seq, "%s\n", mevt->name); | |
744 | ||
745 | return 0; | |
746 | } | |
747 | ||
db69ef65 VS |
748 | static int rdt_bw_gran_show(struct kernfs_open_file *of, |
749 | struct seq_file *seq, void *v) | |
750 | { | |
751 | struct rdt_resource *r = of->kn->parent->priv; | |
752 | ||
753 | seq_printf(seq, "%u\n", r->membw.bw_gran); | |
754 | return 0; | |
755 | } | |
756 | ||
757 | static int rdt_delay_linear_show(struct kernfs_open_file *of, | |
758 | struct seq_file *seq, void *v) | |
759 | { | |
760 | struct rdt_resource *r = of->kn->parent->priv; | |
761 | ||
762 | seq_printf(seq, "%u\n", r->membw.delay_linear); | |
53a114a6 SL |
763 | return 0; |
764 | } | |
765 | ||
d4ab3320 VS |
766 | static int max_threshold_occ_show(struct kernfs_open_file *of, |
767 | struct seq_file *seq, void *v) | |
768 | { | |
769 | struct rdt_resource *r = of->kn->parent->priv; | |
770 | ||
771 | seq_printf(seq, "%u\n", intel_cqm_threshold * r->mon_scale); | |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | static ssize_t max_threshold_occ_write(struct kernfs_open_file *of, | |
777 | char *buf, size_t nbytes, loff_t off) | |
778 | { | |
779 | struct rdt_resource *r = of->kn->parent->priv; | |
780 | unsigned int bytes; | |
781 | int ret; | |
782 | ||
783 | ret = kstrtouint(buf, 0, &bytes); | |
784 | if (ret) | |
785 | return ret; | |
786 | ||
787 | if (bytes > (boot_cpu_data.x86_cache_size * 1024)) | |
788 | return -EINVAL; | |
789 | ||
790 | intel_cqm_threshold = bytes / r->mon_scale; | |
791 | ||
5707b46a | 792 | return nbytes; |
d4ab3320 VS |
793 | } |
794 | ||
d48d7a57 RC |
795 | /* |
796 | * rdtgroup_mode_show - Display mode of this resource group | |
797 | */ | |
798 | static int rdtgroup_mode_show(struct kernfs_open_file *of, | |
799 | struct seq_file *s, void *v) | |
800 | { | |
801 | struct rdtgroup *rdtgrp; | |
802 | ||
803 | rdtgrp = rdtgroup_kn_lock_live(of->kn); | |
804 | if (!rdtgrp) { | |
805 | rdtgroup_kn_unlock(of->kn); | |
806 | return -ENOENT; | |
807 | } | |
808 | ||
809 | seq_printf(s, "%s\n", rdtgroup_mode_str(rdtgrp->mode)); | |
810 | ||
811 | rdtgroup_kn_unlock(of->kn); | |
812 | return 0; | |
813 | } | |
814 | ||
49f7b4ef RC |
815 | /** |
816 | * rdtgroup_cbm_overlaps - Does CBM for intended closid overlap with other | |
817 | * @r: Resource to which domain instance @d belongs. | |
818 | * @d: The domain instance for which @closid is being tested. | |
819 | * @cbm: Capacity bitmask being tested. | |
820 | * @closid: Intended closid for @cbm. | |
821 | * @exclusive: Only check if overlaps with exclusive resource groups | |
822 | * | |
823 | * Checks if provided @cbm intended to be used for @closid on domain | |
824 | * @d overlaps with any other closids or other hardware usage associated | |
825 | * with this domain. If @exclusive is true then only overlaps with | |
826 | * resource groups in exclusive mode will be considered. If @exclusive | |
827 | * is false then overlaps with any resource group or hardware entities | |
828 | * will be considered. | |
829 | * | |
830 | * Return: false if CBM does not overlap, true if it does. | |
831 | */ | |
9ab9aa15 RC |
832 | bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, |
833 | u32 _cbm, int closid, bool exclusive) | |
49f7b4ef RC |
834 | { |
835 | unsigned long *cbm = (unsigned long *)&_cbm; | |
836 | unsigned long *ctrl_b; | |
837 | enum rdtgrp_mode mode; | |
838 | u32 *ctrl; | |
839 | int i; | |
840 | ||
841 | /* Check for any overlap with regions used by hardware directly */ | |
842 | if (!exclusive) { | |
843 | if (bitmap_intersects(cbm, | |
844 | (unsigned long *)&r->cache.shareable_bits, | |
845 | r->cache.cbm_len)) | |
846 | return true; | |
847 | } | |
848 | ||
849 | /* Check for overlap with other resource groups */ | |
850 | ctrl = d->ctrl_val; | |
851 | for (i = 0; i < r->num_closid; i++, ctrl++) { | |
852 | ctrl_b = (unsigned long *)ctrl; | |
853 | if (closid_allocated(i) && i != closid) { | |
854 | if (bitmap_intersects(cbm, ctrl_b, r->cache.cbm_len)) { | |
855 | mode = rdtgroup_mode_by_closid(i); | |
856 | if (exclusive) { | |
857 | if (mode == RDT_MODE_EXCLUSIVE) | |
858 | return true; | |
859 | continue; | |
860 | } | |
861 | return true; | |
862 | } | |
863 | } | |
864 | } | |
865 | ||
866 | return false; | |
867 | } | |
868 | ||
869 | /** | |
870 | * rdtgroup_mode_test_exclusive - Test if this resource group can be exclusive | |
871 | * | |
872 | * An exclusive resource group implies that there should be no sharing of | |
873 | * its allocated resources. At the time this group is considered to be | |
874 | * exclusive this test can determine if its current schemata supports this | |
875 | * setting by testing for overlap with all other resource groups. | |
876 | * | |
877 | * Return: true if resource group can be exclusive, false if there is overlap | |
878 | * with allocations of other resource groups and thus this resource group | |
879 | * cannot be exclusive. | |
880 | */ | |
881 | static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp) | |
882 | { | |
883 | int closid = rdtgrp->closid; | |
884 | struct rdt_resource *r; | |
885 | struct rdt_domain *d; | |
886 | ||
887 | for_each_alloc_enabled_rdt_resource(r) { | |
888 | list_for_each_entry(d, &r->domains, list) { | |
889 | if (rdtgroup_cbm_overlaps(r, d, d->ctrl_val[closid], | |
890 | rdtgrp->closid, false)) | |
891 | return false; | |
892 | } | |
893 | } | |
894 | ||
895 | return true; | |
896 | } | |
897 | ||
898 | /** | |
899 | * rdtgroup_mode_write - Modify the resource group's mode | |
900 | * | |
901 | */ | |
d48d7a57 RC |
902 | static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, |
903 | char *buf, size_t nbytes, loff_t off) | |
904 | { | |
905 | struct rdtgroup *rdtgrp; | |
906 | enum rdtgrp_mode mode; | |
907 | int ret = 0; | |
908 | ||
909 | /* Valid input requires a trailing newline */ | |
910 | if (nbytes == 0 || buf[nbytes - 1] != '\n') | |
911 | return -EINVAL; | |
912 | buf[nbytes - 1] = '\0'; | |
913 | ||
914 | rdtgrp = rdtgroup_kn_lock_live(of->kn); | |
915 | if (!rdtgrp) { | |
916 | rdtgroup_kn_unlock(of->kn); | |
917 | return -ENOENT; | |
918 | } | |
919 | ||
920 | rdt_last_cmd_clear(); | |
921 | ||
922 | mode = rdtgrp->mode; | |
923 | ||
49f7b4ef RC |
924 | if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) || |
925 | (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE)) | |
d48d7a57 RC |
926 | goto out; |
927 | ||
928 | if (!strcmp(buf, "shareable")) { | |
929 | rdtgrp->mode = RDT_MODE_SHAREABLE; | |
49f7b4ef RC |
930 | } else if (!strcmp(buf, "exclusive")) { |
931 | if (!rdtgroup_mode_test_exclusive(rdtgrp)) { | |
932 | rdt_last_cmd_printf("schemata overlaps\n"); | |
933 | ret = -EINVAL; | |
934 | goto out; | |
935 | } | |
936 | rdtgrp->mode = RDT_MODE_EXCLUSIVE; | |
d48d7a57 RC |
937 | } else { |
938 | rdt_last_cmd_printf("unknown/unsupported mode\n"); | |
939 | ret = -EINVAL; | |
940 | } | |
941 | ||
942 | out: | |
943 | rdtgroup_kn_unlock(of->kn); | |
944 | return ret ?: nbytes; | |
945 | } | |
946 | ||
4e978d06 | 947 | /* rdtgroup information files for one cache resource. */ |
5dc1d5c6 | 948 | static struct rftype res_common_files[] = { |
9b3a7fd0 TL |
949 | { |
950 | .name = "last_cmd_status", | |
951 | .mode = 0444, | |
952 | .kf_ops = &rdtgroup_kf_single_ops, | |
953 | .seq_show = rdt_last_cmd_status_show, | |
954 | .fflags = RF_TOP_INFO, | |
955 | }, | |
4e978d06 FY |
956 | { |
957 | .name = "num_closids", | |
958 | .mode = 0444, | |
959 | .kf_ops = &rdtgroup_kf_single_ops, | |
960 | .seq_show = rdt_num_closids_show, | |
5dc1d5c6 | 961 | .fflags = RF_CTRL_INFO, |
4e978d06 | 962 | }, |
d4ab3320 VS |
963 | { |
964 | .name = "mon_features", | |
965 | .mode = 0444, | |
966 | .kf_ops = &rdtgroup_kf_single_ops, | |
967 | .seq_show = rdt_mon_features_show, | |
968 | .fflags = RF_MON_INFO, | |
969 | }, | |
970 | { | |
971 | .name = "num_rmids", | |
972 | .mode = 0444, | |
973 | .kf_ops = &rdtgroup_kf_single_ops, | |
974 | .seq_show = rdt_num_rmids_show, | |
975 | .fflags = RF_MON_INFO, | |
976 | }, | |
4e978d06 FY |
977 | { |
978 | .name = "cbm_mask", | |
979 | .mode = 0444, | |
980 | .kf_ops = &rdtgroup_kf_single_ops, | |
2545e9f5 | 981 | .seq_show = rdt_default_ctrl_show, |
5dc1d5c6 | 982 | .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, |
4e978d06 | 983 | }, |
53a114a6 SL |
984 | { |
985 | .name = "min_cbm_bits", | |
986 | .mode = 0444, | |
987 | .kf_ops = &rdtgroup_kf_single_ops, | |
988 | .seq_show = rdt_min_cbm_bits_show, | |
5dc1d5c6 | 989 | .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, |
db69ef65 | 990 | }, |
0dd2d749 FY |
991 | { |
992 | .name = "shareable_bits", | |
993 | .mode = 0444, | |
994 | .kf_ops = &rdtgroup_kf_single_ops, | |
995 | .seq_show = rdt_shareable_bits_show, | |
996 | .fflags = RF_CTRL_INFO | RFTYPE_RES_CACHE, | |
997 | }, | |
db69ef65 VS |
998 | { |
999 | .name = "min_bandwidth", | |
1000 | .mode = 0444, | |
1001 | .kf_ops = &rdtgroup_kf_single_ops, | |
1002 | .seq_show = rdt_min_bw_show, | |
5dc1d5c6 | 1003 | .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, |
db69ef65 VS |
1004 | }, |
1005 | { | |
1006 | .name = "bandwidth_gran", | |
1007 | .mode = 0444, | |
1008 | .kf_ops = &rdtgroup_kf_single_ops, | |
1009 | .seq_show = rdt_bw_gran_show, | |
5dc1d5c6 | 1010 | .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, |
db69ef65 VS |
1011 | }, |
1012 | { | |
1013 | .name = "delay_linear", | |
1014 | .mode = 0444, | |
1015 | .kf_ops = &rdtgroup_kf_single_ops, | |
1016 | .seq_show = rdt_delay_linear_show, | |
5dc1d5c6 T |
1017 | .fflags = RF_CTRL_INFO | RFTYPE_RES_MB, |
1018 | }, | |
d4ab3320 VS |
1019 | { |
1020 | .name = "max_threshold_occupancy", | |
1021 | .mode = 0644, | |
1022 | .kf_ops = &rdtgroup_kf_single_ops, | |
1023 | .write = max_threshold_occ_write, | |
1024 | .seq_show = max_threshold_occ_show, | |
1025 | .fflags = RF_MON_INFO | RFTYPE_RES_CACHE, | |
1026 | }, | |
5dc1d5c6 T |
1027 | { |
1028 | .name = "cpus", | |
1029 | .mode = 0644, | |
1030 | .kf_ops = &rdtgroup_kf_single_ops, | |
1031 | .write = rdtgroup_cpus_write, | |
1032 | .seq_show = rdtgroup_cpus_show, | |
1033 | .fflags = RFTYPE_BASE, | |
1034 | }, | |
1035 | { | |
1036 | .name = "cpus_list", | |
1037 | .mode = 0644, | |
1038 | .kf_ops = &rdtgroup_kf_single_ops, | |
1039 | .write = rdtgroup_cpus_write, | |
1040 | .seq_show = rdtgroup_cpus_show, | |
1041 | .flags = RFTYPE_FLAGS_CPUS_LIST, | |
1042 | .fflags = RFTYPE_BASE, | |
1043 | }, | |
1044 | { | |
1045 | .name = "tasks", | |
1046 | .mode = 0644, | |
1047 | .kf_ops = &rdtgroup_kf_single_ops, | |
1048 | .write = rdtgroup_tasks_write, | |
1049 | .seq_show = rdtgroup_tasks_show, | |
1050 | .fflags = RFTYPE_BASE, | |
1051 | }, | |
1052 | { | |
1053 | .name = "schemata", | |
1054 | .mode = 0644, | |
1055 | .kf_ops = &rdtgroup_kf_single_ops, | |
1056 | .write = rdtgroup_schemata_write, | |
1057 | .seq_show = rdtgroup_schemata_show, | |
1058 | .fflags = RF_CTRL_BASE, | |
db69ef65 | 1059 | }, |
d48d7a57 RC |
1060 | { |
1061 | .name = "mode", | |
1062 | .mode = 0644, | |
1063 | .kf_ops = &rdtgroup_kf_single_ops, | |
1064 | .write = rdtgroup_mode_write, | |
1065 | .seq_show = rdtgroup_mode_show, | |
1066 | .fflags = RF_CTRL_BASE, | |
1067 | }, | |
db69ef65 VS |
1068 | }; |
1069 | ||
5dc1d5c6 | 1070 | static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags) |
db69ef65 | 1071 | { |
5dc1d5c6 T |
1072 | struct rftype *rfts, *rft; |
1073 | int ret, len; | |
1074 | ||
1075 | rfts = res_common_files; | |
1076 | len = ARRAY_SIZE(res_common_files); | |
1077 | ||
1078 | lockdep_assert_held(&rdtgroup_mutex); | |
1079 | ||
1080 | for (rft = rfts; rft < rfts + len; rft++) { | |
1081 | if ((fflags & rft->fflags) == rft->fflags) { | |
1082 | ret = rdtgroup_add_file(kn, rft); | |
1083 | if (ret) | |
1084 | goto error; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | return 0; | |
1089 | error: | |
1090 | pr_warn("Failed to add %s, err=%d\n", rft->name, ret); | |
1091 | while (--rft >= rfts) { | |
1092 | if ((fflags & rft->fflags) == rft->fflags) | |
1093 | kernfs_remove_by_name(kn, rft->name); | |
1094 | } | |
1095 | return ret; | |
db69ef65 VS |
1096 | } |
1097 | ||
5dc1d5c6 T |
1098 | static int rdtgroup_mkdir_info_resdir(struct rdt_resource *r, char *name, |
1099 | unsigned long fflags) | |
6a507a6a | 1100 | { |
5dc1d5c6 T |
1101 | struct kernfs_node *kn_subdir; |
1102 | int ret; | |
1103 | ||
1104 | kn_subdir = kernfs_create_dir(kn_info, name, | |
1105 | kn_info->mode, r); | |
1106 | if (IS_ERR(kn_subdir)) | |
1107 | return PTR_ERR(kn_subdir); | |
1108 | ||
1109 | kernfs_get(kn_subdir); | |
1110 | ret = rdtgroup_kn_set_ugid(kn_subdir); | |
1111 | if (ret) | |
1112 | return ret; | |
1113 | ||
1114 | ret = rdtgroup_add_files(kn_subdir, fflags); | |
1115 | if (!ret) | |
1116 | kernfs_activate(kn_subdir); | |
1117 | ||
1118 | return ret; | |
6a507a6a VS |
1119 | } |
1120 | ||
4e978d06 FY |
1121 | static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn) |
1122 | { | |
4e978d06 | 1123 | struct rdt_resource *r; |
5dc1d5c6 | 1124 | unsigned long fflags; |
d4ab3320 | 1125 | char name[32]; |
5dc1d5c6 | 1126 | int ret; |
4e978d06 FY |
1127 | |
1128 | /* create the directory */ | |
1129 | kn_info = kernfs_create_dir(parent_kn, "info", parent_kn->mode, NULL); | |
1130 | if (IS_ERR(kn_info)) | |
1131 | return PTR_ERR(kn_info); | |
1132 | kernfs_get(kn_info); | |
1133 | ||
9b3a7fd0 TL |
1134 | ret = rdtgroup_add_files(kn_info, RF_TOP_INFO); |
1135 | if (ret) | |
1136 | goto out_destroy; | |
1137 | ||
1b5c0b75 | 1138 | for_each_alloc_enabled_rdt_resource(r) { |
5dc1d5c6 T |
1139 | fflags = r->fflags | RF_CTRL_INFO; |
1140 | ret = rdtgroup_mkdir_info_resdir(r, r->name, fflags); | |
4e978d06 FY |
1141 | if (ret) |
1142 | goto out_destroy; | |
4e978d06 | 1143 | } |
d4ab3320 VS |
1144 | |
1145 | for_each_mon_enabled_rdt_resource(r) { | |
1146 | fflags = r->fflags | RF_MON_INFO; | |
1147 | sprintf(name, "%s_MON", r->name); | |
1148 | ret = rdtgroup_mkdir_info_resdir(r, name, fflags); | |
1149 | if (ret) | |
1150 | goto out_destroy; | |
1151 | } | |
1152 | ||
4e978d06 FY |
1153 | /* |
1154 | * This extra ref will be put in kernfs_remove() and guarantees | |
1155 | * that @rdtgrp->kn is always accessible. | |
1156 | */ | |
1157 | kernfs_get(kn_info); | |
1158 | ||
1159 | ret = rdtgroup_kn_set_ugid(kn_info); | |
1160 | if (ret) | |
1161 | goto out_destroy; | |
1162 | ||
1163 | kernfs_activate(kn_info); | |
1164 | ||
1165 | return 0; | |
1166 | ||
1167 | out_destroy: | |
1168 | kernfs_remove(kn_info); | |
1169 | return ret; | |
1170 | } | |
1171 | ||
c7d9aac6 VS |
1172 | static int |
1173 | mongroup_create_dir(struct kernfs_node *parent_kn, struct rdtgroup *prgrp, | |
1174 | char *name, struct kernfs_node **dest_kn) | |
1175 | { | |
1176 | struct kernfs_node *kn; | |
1177 | int ret; | |
1178 | ||
1179 | /* create the directory */ | |
1180 | kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); | |
1181 | if (IS_ERR(kn)) | |
1182 | return PTR_ERR(kn); | |
1183 | ||
1184 | if (dest_kn) | |
1185 | *dest_kn = kn; | |
1186 | ||
1187 | /* | |
1188 | * This extra ref will be put in kernfs_remove() and guarantees | |
1189 | * that @rdtgrp->kn is always accessible. | |
1190 | */ | |
1191 | kernfs_get(kn); | |
1192 | ||
1193 | ret = rdtgroup_kn_set_ugid(kn); | |
1194 | if (ret) | |
1195 | goto out_destroy; | |
1196 | ||
1197 | kernfs_activate(kn); | |
1198 | ||
1199 | return 0; | |
1200 | ||
1201 | out_destroy: | |
1202 | kernfs_remove(kn); | |
1203 | return ret; | |
1204 | } | |
99adde9b | 1205 | |
5ff193fb FY |
1206 | static void l3_qos_cfg_update(void *arg) |
1207 | { | |
1208 | bool *enable = arg; | |
1209 | ||
1210 | wrmsrl(IA32_L3_QOS_CFG, *enable ? L3_QOS_CDP_ENABLE : 0ULL); | |
1211 | } | |
1212 | ||
99adde9b | 1213 | static void l2_qos_cfg_update(void *arg) |
5ff193fb | 1214 | { |
99adde9b FY |
1215 | bool *enable = arg; |
1216 | ||
1217 | wrmsrl(IA32_L2_QOS_CFG, *enable ? L2_QOS_CDP_ENABLE : 0ULL); | |
1218 | } | |
1219 | ||
19c635ab VS |
1220 | static inline bool is_mba_linear(void) |
1221 | { | |
1222 | return rdt_resources_all[RDT_RESOURCE_MBA].membw.delay_linear; | |
1223 | } | |
1224 | ||
99adde9b FY |
1225 | static int set_cache_qos_cfg(int level, bool enable) |
1226 | { | |
1227 | void (*update)(void *arg); | |
1228 | struct rdt_resource *r_l; | |
5ff193fb FY |
1229 | cpumask_var_t cpu_mask; |
1230 | struct rdt_domain *d; | |
1231 | int cpu; | |
1232 | ||
1233 | if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) | |
1234 | return -ENOMEM; | |
1235 | ||
99adde9b FY |
1236 | if (level == RDT_RESOURCE_L3) |
1237 | update = l3_qos_cfg_update; | |
1238 | else if (level == RDT_RESOURCE_L2) | |
1239 | update = l2_qos_cfg_update; | |
1240 | else | |
1241 | return -EINVAL; | |
1242 | ||
1243 | r_l = &rdt_resources_all[level]; | |
1244 | list_for_each_entry(d, &r_l->domains, list) { | |
5ff193fb FY |
1245 | /* Pick one CPU from each domain instance to update MSR */ |
1246 | cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); | |
1247 | } | |
1248 | cpu = get_cpu(); | |
1249 | /* Update QOS_CFG MSR on this cpu if it's in cpu_mask. */ | |
1250 | if (cpumask_test_cpu(cpu, cpu_mask)) | |
99adde9b | 1251 | update(&enable); |
5ff193fb | 1252 | /* Update QOS_CFG MSR on all other cpus in cpu_mask. */ |
99adde9b | 1253 | smp_call_function_many(cpu_mask, update, &enable, 1); |
5ff193fb FY |
1254 | put_cpu(); |
1255 | ||
1256 | free_cpumask_var(cpu_mask); | |
1257 | ||
1258 | return 0; | |
1259 | } | |
1260 | ||
19c635ab VS |
1261 | /* |
1262 | * Enable or disable the MBA software controller | |
1263 | * which helps user specify bandwidth in MBps. | |
1264 | * MBA software controller is supported only if | |
1265 | * MBM is supported and MBA is in linear scale. | |
1266 | */ | |
1267 | static int set_mba_sc(bool mba_sc) | |
1268 | { | |
1269 | struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA]; | |
1bd2a63b | 1270 | struct rdt_domain *d; |
19c635ab VS |
1271 | |
1272 | if (!is_mbm_enabled() || !is_mba_linear() || | |
1273 | mba_sc == is_mba_sc(r)) | |
1274 | return -EINVAL; | |
1275 | ||
1276 | r->membw.mba_sc = mba_sc; | |
1bd2a63b VS |
1277 | list_for_each_entry(d, &r->domains, list) |
1278 | setup_default_ctrlval(r, d->ctrl_val, d->mbps_val); | |
19c635ab VS |
1279 | |
1280 | return 0; | |
1281 | } | |
1282 | ||
99adde9b | 1283 | static int cdp_enable(int level, int data_type, int code_type) |
5ff193fb | 1284 | { |
99adde9b FY |
1285 | struct rdt_resource *r_ldata = &rdt_resources_all[data_type]; |
1286 | struct rdt_resource *r_lcode = &rdt_resources_all[code_type]; | |
1287 | struct rdt_resource *r_l = &rdt_resources_all[level]; | |
5ff193fb FY |
1288 | int ret; |
1289 | ||
99adde9b FY |
1290 | if (!r_l->alloc_capable || !r_ldata->alloc_capable || |
1291 | !r_lcode->alloc_capable) | |
5ff193fb FY |
1292 | return -EINVAL; |
1293 | ||
99adde9b | 1294 | ret = set_cache_qos_cfg(level, true); |
5ff193fb | 1295 | if (!ret) { |
99adde9b FY |
1296 | r_l->alloc_enabled = false; |
1297 | r_ldata->alloc_enabled = true; | |
1298 | r_lcode->alloc_enabled = true; | |
5ff193fb FY |
1299 | } |
1300 | return ret; | |
1301 | } | |
1302 | ||
99adde9b FY |
1303 | static int cdpl3_enable(void) |
1304 | { | |
1305 | return cdp_enable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, | |
1306 | RDT_RESOURCE_L3CODE); | |
1307 | } | |
1308 | ||
1309 | static int cdpl2_enable(void) | |
5ff193fb | 1310 | { |
99adde9b FY |
1311 | return cdp_enable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, |
1312 | RDT_RESOURCE_L2CODE); | |
1313 | } | |
1314 | ||
1315 | static void cdp_disable(int level, int data_type, int code_type) | |
1316 | { | |
1317 | struct rdt_resource *r = &rdt_resources_all[level]; | |
5ff193fb | 1318 | |
1b5c0b75 | 1319 | r->alloc_enabled = r->alloc_capable; |
5ff193fb | 1320 | |
99adde9b FY |
1321 | if (rdt_resources_all[data_type].alloc_enabled) { |
1322 | rdt_resources_all[data_type].alloc_enabled = false; | |
1323 | rdt_resources_all[code_type].alloc_enabled = false; | |
1324 | set_cache_qos_cfg(level, false); | |
5ff193fb FY |
1325 | } |
1326 | } | |
1327 | ||
99adde9b FY |
1328 | static void cdpl3_disable(void) |
1329 | { | |
1330 | cdp_disable(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA, RDT_RESOURCE_L3CODE); | |
1331 | } | |
1332 | ||
1333 | static void cdpl2_disable(void) | |
1334 | { | |
1335 | cdp_disable(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA, RDT_RESOURCE_L2CODE); | |
1336 | } | |
1337 | ||
1338 | static void cdp_disable_all(void) | |
1339 | { | |
1340 | if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) | |
1341 | cdpl3_disable(); | |
1342 | if (rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) | |
1343 | cdpl2_disable(); | |
1344 | } | |
1345 | ||
5ff193fb FY |
1346 | static int parse_rdtgroupfs_options(char *data) |
1347 | { | |
1348 | char *token, *o = data; | |
1349 | int ret = 0; | |
1350 | ||
1351 | while ((token = strsep(&o, ",")) != NULL) { | |
99adde9b FY |
1352 | if (!*token) { |
1353 | ret = -EINVAL; | |
1354 | goto out; | |
1355 | } | |
5ff193fb | 1356 | |
99adde9b FY |
1357 | if (!strcmp(token, "cdp")) { |
1358 | ret = cdpl3_enable(); | |
1359 | if (ret) | |
1360 | goto out; | |
1361 | } else if (!strcmp(token, "cdpl2")) { | |
1362 | ret = cdpl2_enable(); | |
1363 | if (ret) | |
1364 | goto out; | |
19c635ab VS |
1365 | } else if (!strcmp(token, "mba_MBps")) { |
1366 | ret = set_mba_sc(true); | |
1367 | if (ret) | |
1368 | goto out; | |
99adde9b FY |
1369 | } else { |
1370 | ret = -EINVAL; | |
1371 | goto out; | |
1372 | } | |
5ff193fb FY |
1373 | } |
1374 | ||
99adde9b FY |
1375 | return 0; |
1376 | ||
1377 | out: | |
1378 | pr_err("Invalid mount option \"%s\"\n", token); | |
1379 | ||
5ff193fb FY |
1380 | return ret; |
1381 | } | |
1382 | ||
60cf5e10 FY |
1383 | /* |
1384 | * We don't allow rdtgroup directories to be created anywhere | |
1385 | * except the root directory. Thus when looking for the rdtgroup | |
1386 | * structure for a kernfs node we are either looking at a directory, | |
1387 | * in which case the rdtgroup structure is pointed at by the "priv" | |
1388 | * field, otherwise we have a file, and need only look to the parent | |
1389 | * to find the rdtgroup. | |
1390 | */ | |
1391 | static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn) | |
1392 | { | |
f57b3087 FY |
1393 | if (kernfs_type(kn) == KERNFS_DIR) { |
1394 | /* | |
1395 | * All the resource directories use "kn->priv" | |
1396 | * to point to the "struct rdtgroup" for the | |
1397 | * resource. "info" and its subdirectories don't | |
1398 | * have rdtgroup structures, so return NULL here. | |
1399 | */ | |
1400 | if (kn == kn_info || kn->parent == kn_info) | |
1401 | return NULL; | |
1402 | else | |
1403 | return kn->priv; | |
1404 | } else { | |
60cf5e10 | 1405 | return kn->parent->priv; |
f57b3087 | 1406 | } |
60cf5e10 FY |
1407 | } |
1408 | ||
1409 | struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn) | |
1410 | { | |
1411 | struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); | |
1412 | ||
f57b3087 FY |
1413 | if (!rdtgrp) |
1414 | return NULL; | |
1415 | ||
60cf5e10 FY |
1416 | atomic_inc(&rdtgrp->waitcount); |
1417 | kernfs_break_active_protection(kn); | |
1418 | ||
1419 | mutex_lock(&rdtgroup_mutex); | |
1420 | ||
1421 | /* Was this group deleted while we waited? */ | |
1422 | if (rdtgrp->flags & RDT_DELETED) | |
1423 | return NULL; | |
1424 | ||
1425 | return rdtgrp; | |
1426 | } | |
1427 | ||
1428 | void rdtgroup_kn_unlock(struct kernfs_node *kn) | |
1429 | { | |
1430 | struct rdtgroup *rdtgrp = kernfs_to_rdtgroup(kn); | |
1431 | ||
f57b3087 FY |
1432 | if (!rdtgrp) |
1433 | return; | |
1434 | ||
60cf5e10 FY |
1435 | mutex_unlock(&rdtgroup_mutex); |
1436 | ||
1437 | if (atomic_dec_and_test(&rdtgrp->waitcount) && | |
1438 | (rdtgrp->flags & RDT_DELETED)) { | |
1439 | kernfs_unbreak_active_protection(kn); | |
49ec8f5b | 1440 | kernfs_put(rdtgrp->kn); |
60cf5e10 FY |
1441 | kfree(rdtgrp); |
1442 | } else { | |
1443 | kernfs_unbreak_active_protection(kn); | |
1444 | } | |
1445 | } | |
1446 | ||
4af4a88e VS |
1447 | static int mkdir_mondata_all(struct kernfs_node *parent_kn, |
1448 | struct rdtgroup *prgrp, | |
1449 | struct kernfs_node **mon_data_kn); | |
1450 | ||
5ff193fb FY |
1451 | static struct dentry *rdt_mount(struct file_system_type *fs_type, |
1452 | int flags, const char *unused_dev_name, | |
1453 | void *data) | |
1454 | { | |
e3302683 VS |
1455 | struct rdt_domain *dom; |
1456 | struct rdt_resource *r; | |
5ff193fb FY |
1457 | struct dentry *dentry; |
1458 | int ret; | |
1459 | ||
87943db7 | 1460 | cpus_read_lock(); |
5ff193fb FY |
1461 | mutex_lock(&rdtgroup_mutex); |
1462 | /* | |
1463 | * resctrl file system can only be mounted once. | |
1464 | */ | |
4af4a88e | 1465 | if (static_branch_unlikely(&rdt_enable_key)) { |
5ff193fb FY |
1466 | dentry = ERR_PTR(-EBUSY); |
1467 | goto out; | |
1468 | } | |
1469 | ||
1470 | ret = parse_rdtgroupfs_options(data); | |
1471 | if (ret) { | |
1472 | dentry = ERR_PTR(ret); | |
1473 | goto out_cdp; | |
1474 | } | |
1475 | ||
60cf5e10 FY |
1476 | closid_init(); |
1477 | ||
4e978d06 | 1478 | ret = rdtgroup_create_info_dir(rdtgroup_default.kn); |
7bff0af5 SL |
1479 | if (ret) { |
1480 | dentry = ERR_PTR(ret); | |
4e978d06 | 1481 | goto out_cdp; |
7bff0af5 | 1482 | } |
4e978d06 | 1483 | |
4af4a88e VS |
1484 | if (rdt_mon_capable) { |
1485 | ret = mongroup_create_dir(rdtgroup_default.kn, | |
1486 | NULL, "mon_groups", | |
1487 | &kn_mongrp); | |
1488 | if (ret) { | |
1489 | dentry = ERR_PTR(ret); | |
1490 | goto out_info; | |
1491 | } | |
1492 | kernfs_get(kn_mongrp); | |
1493 | ||
1494 | ret = mkdir_mondata_all(rdtgroup_default.kn, | |
1495 | &rdtgroup_default, &kn_mondata); | |
1496 | if (ret) { | |
1497 | dentry = ERR_PTR(ret); | |
1498 | goto out_mongrp; | |
1499 | } | |
1500 | kernfs_get(kn_mondata); | |
1501 | rdtgroup_default.mon.mon_data_kn = kn_mondata; | |
1502 | } | |
1503 | ||
32206ab3 RC |
1504 | ret = rdt_pseudo_lock_init(); |
1505 | if (ret) { | |
1506 | dentry = ERR_PTR(ret); | |
1507 | goto out_mondata; | |
1508 | } | |
1509 | ||
5ff193fb FY |
1510 | dentry = kernfs_mount(fs_type, flags, rdt_root, |
1511 | RDTGROUP_SUPER_MAGIC, NULL); | |
1512 | if (IS_ERR(dentry)) | |
32206ab3 | 1513 | goto out_psl; |
4af4a88e VS |
1514 | |
1515 | if (rdt_alloc_capable) | |
87943db7 | 1516 | static_branch_enable_cpuslocked(&rdt_alloc_enable_key); |
4af4a88e | 1517 | if (rdt_mon_capable) |
87943db7 | 1518 | static_branch_enable_cpuslocked(&rdt_mon_enable_key); |
5ff193fb | 1519 | |
4af4a88e | 1520 | if (rdt_alloc_capable || rdt_mon_capable) |
87943db7 | 1521 | static_branch_enable_cpuslocked(&rdt_enable_key); |
e3302683 VS |
1522 | |
1523 | if (is_mbm_enabled()) { | |
1524 | r = &rdt_resources_all[RDT_RESOURCE_L3]; | |
1525 | list_for_each_entry(dom, &r->domains, list) | |
bbc4615e | 1526 | mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL); |
e3302683 VS |
1527 | } |
1528 | ||
5ff193fb FY |
1529 | goto out; |
1530 | ||
32206ab3 RC |
1531 | out_psl: |
1532 | rdt_pseudo_lock_release(); | |
4af4a88e VS |
1533 | out_mondata: |
1534 | if (rdt_mon_capable) | |
1535 | kernfs_remove(kn_mondata); | |
1536 | out_mongrp: | |
1537 | if (rdt_mon_capable) | |
1538 | kernfs_remove(kn_mongrp); | |
1539 | out_info: | |
79298acc | 1540 | kernfs_remove(kn_info); |
5ff193fb | 1541 | out_cdp: |
99adde9b | 1542 | cdp_disable_all(); |
5ff193fb | 1543 | out: |
9b3a7fd0 | 1544 | rdt_last_cmd_clear(); |
5ff193fb | 1545 | mutex_unlock(&rdtgroup_mutex); |
87943db7 | 1546 | cpus_read_unlock(); |
5ff193fb FY |
1547 | |
1548 | return dentry; | |
1549 | } | |
1550 | ||
2545e9f5 | 1551 | static int reset_all_ctrls(struct rdt_resource *r) |
5ff193fb FY |
1552 | { |
1553 | struct msr_param msr_param; | |
1554 | cpumask_var_t cpu_mask; | |
1555 | struct rdt_domain *d; | |
1556 | int i, cpu; | |
1557 | ||
1558 | if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL)) | |
1559 | return -ENOMEM; | |
1560 | ||
1561 | msr_param.res = r; | |
1562 | msr_param.low = 0; | |
1563 | msr_param.high = r->num_closid; | |
1564 | ||
1565 | /* | |
1566 | * Disable resource control for this resource by setting all | |
1567 | * CBMs in all domains to the maximum mask value. Pick one CPU | |
1568 | * from each domain to update the MSRs below. | |
1569 | */ | |
1570 | list_for_each_entry(d, &r->domains, list) { | |
1571 | cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask); | |
1572 | ||
1573 | for (i = 0; i < r->num_closid; i++) | |
2545e9f5 | 1574 | d->ctrl_val[i] = r->default_ctrl; |
5ff193fb FY |
1575 | } |
1576 | cpu = get_cpu(); | |
1577 | /* Update CBM on this cpu if it's in cpu_mask. */ | |
1578 | if (cpumask_test_cpu(cpu, cpu_mask)) | |
2545e9f5 | 1579 | rdt_ctrl_update(&msr_param); |
5ff193fb | 1580 | /* Update CBM on all other cpus in cpu_mask. */ |
2545e9f5 | 1581 | smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1); |
5ff193fb FY |
1582 | put_cpu(); |
1583 | ||
1584 | free_cpumask_var(cpu_mask); | |
1585 | ||
1586 | return 0; | |
1587 | } | |
1588 | ||
f3cbeaca VS |
1589 | static bool is_closid_match(struct task_struct *t, struct rdtgroup *r) |
1590 | { | |
1591 | return (rdt_alloc_capable && | |
1592 | (r->type == RDTCTRL_GROUP) && (t->closid == r->closid)); | |
1593 | } | |
1594 | ||
1595 | static bool is_rmid_match(struct task_struct *t, struct rdtgroup *r) | |
1596 | { | |
1597 | return (rdt_mon_capable && | |
1598 | (r->type == RDTMON_GROUP) && (t->rmid == r->mon.rmid)); | |
1599 | } | |
1600 | ||
4e978d06 | 1601 | /* |
0efc89be FY |
1602 | * Move tasks from one to the other group. If @from is NULL, then all tasks |
1603 | * in the systems are moved unconditionally (used for teardown). | |
1604 | * | |
1605 | * If @mask is not NULL the cpus on which moved tasks are running are set | |
1606 | * in that mask so the update smp function call is restricted to affected | |
1607 | * cpus. | |
4e978d06 | 1608 | */ |
0efc89be FY |
1609 | static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to, |
1610 | struct cpumask *mask) | |
4e978d06 | 1611 | { |
e02737d5 FY |
1612 | struct task_struct *p, *t; |
1613 | ||
e02737d5 | 1614 | read_lock(&tasklist_lock); |
0efc89be | 1615 | for_each_process_thread(p, t) { |
f3cbeaca VS |
1616 | if (!from || is_closid_match(t, from) || |
1617 | is_rmid_match(t, from)) { | |
0efc89be | 1618 | t->closid = to->closid; |
f3cbeaca VS |
1619 | t->rmid = to->mon.rmid; |
1620 | ||
0efc89be FY |
1621 | #ifdef CONFIG_SMP |
1622 | /* | |
1623 | * This is safe on x86 w/o barriers as the ordering | |
1624 | * of writing to task_cpu() and t->on_cpu is | |
1625 | * reverse to the reading here. The detection is | |
1626 | * inaccurate as tasks might move or schedule | |
1627 | * before the smp function call takes place. In | |
1628 | * such a case the function call is pointless, but | |
1629 | * there is no other side effect. | |
1630 | */ | |
1631 | if (mask && t->on_cpu) | |
1632 | cpumask_set_cpu(task_cpu(t), mask); | |
1633 | #endif | |
1634 | } | |
1635 | } | |
e02737d5 | 1636 | read_unlock(&tasklist_lock); |
0efc89be FY |
1637 | } |
1638 | ||
f3cbeaca VS |
1639 | static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp) |
1640 | { | |
1641 | struct rdtgroup *sentry, *stmp; | |
1642 | struct list_head *head; | |
1643 | ||
1644 | head = &rdtgrp->mon.crdtgrp_list; | |
1645 | list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) { | |
1646 | free_rmid(sentry->mon.rmid); | |
1647 | list_del(&sentry->mon.crdtgrp_list); | |
1648 | kfree(sentry); | |
1649 | } | |
1650 | } | |
1651 | ||
0efc89be FY |
1652 | /* |
1653 | * Forcibly remove all of subdirectories under root. | |
1654 | */ | |
1655 | static void rmdir_all_sub(void) | |
1656 | { | |
1657 | struct rdtgroup *rdtgrp, *tmp; | |
1658 | ||
1659 | /* Move all tasks to the default resource group */ | |
1660 | rdt_move_group_tasks(NULL, &rdtgroup_default, NULL); | |
60cf5e10 | 1661 | |
60cf5e10 | 1662 | list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { |
4af4a88e VS |
1663 | /* Free any child rmids */ |
1664 | free_all_child_rdtgrp(rdtgrp); | |
1665 | ||
60cf5e10 FY |
1666 | /* Remove each rdtgroup other than root */ |
1667 | if (rdtgrp == &rdtgroup_default) | |
1668 | continue; | |
c7cc0cc1 FY |
1669 | |
1670 | /* | |
1671 | * Give any CPUs back to the default group. We cannot copy | |
1672 | * cpu_online_mask because a CPU might have executed the | |
1673 | * offline callback already, but is still marked online. | |
1674 | */ | |
1675 | cpumask_or(&rdtgroup_default.cpu_mask, | |
1676 | &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); | |
1677 | ||
4af4a88e VS |
1678 | free_rmid(rdtgrp->mon.rmid); |
1679 | ||
60cf5e10 FY |
1680 | kernfs_remove(rdtgrp->kn); |
1681 | list_del(&rdtgrp->rdtgroup_list); | |
1682 | kfree(rdtgrp); | |
1683 | } | |
0efc89be | 1684 | /* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ |
a9fcf862 | 1685 | update_closid_rmid(cpu_online_mask, &rdtgroup_default); |
0efc89be | 1686 | |
4e978d06 | 1687 | kernfs_remove(kn_info); |
4af4a88e VS |
1688 | kernfs_remove(kn_mongrp); |
1689 | kernfs_remove(kn_mondata); | |
4e978d06 FY |
1690 | } |
1691 | ||
5ff193fb FY |
1692 | static void rdt_kill_sb(struct super_block *sb) |
1693 | { | |
1694 | struct rdt_resource *r; | |
1695 | ||
36b6f9fc | 1696 | cpus_read_lock(); |
5ff193fb FY |
1697 | mutex_lock(&rdtgroup_mutex); |
1698 | ||
19c635ab VS |
1699 | set_mba_sc(false); |
1700 | ||
5ff193fb | 1701 | /*Put everything back to default values. */ |
1b5c0b75 | 1702 | for_each_alloc_enabled_rdt_resource(r) |
2545e9f5 | 1703 | reset_all_ctrls(r); |
99adde9b | 1704 | cdp_disable_all(); |
4e978d06 | 1705 | rmdir_all_sub(); |
472ef09b | 1706 | rdtgroup_default.mode = RDT_MODE_SHAREABLE; |
36b6f9fc RC |
1707 | static_branch_disable_cpuslocked(&rdt_alloc_enable_key); |
1708 | static_branch_disable_cpuslocked(&rdt_mon_enable_key); | |
1709 | static_branch_disable_cpuslocked(&rdt_enable_key); | |
5ff193fb FY |
1710 | kernfs_kill_sb(sb); |
1711 | mutex_unlock(&rdtgroup_mutex); | |
36b6f9fc | 1712 | cpus_read_unlock(); |
5ff193fb FY |
1713 | } |
1714 | ||
1715 | static struct file_system_type rdt_fs_type = { | |
1716 | .name = "resctrl", | |
1717 | .mount = rdt_mount, | |
1718 | .kill_sb = rdt_kill_sb, | |
1719 | }; | |
1720 | ||
d89b7379 VS |
1721 | static int mon_addfile(struct kernfs_node *parent_kn, const char *name, |
1722 | void *priv) | |
1723 | { | |
1724 | struct kernfs_node *kn; | |
1725 | int ret = 0; | |
1726 | ||
1727 | kn = __kernfs_create_file(parent_kn, name, 0444, 0, | |
1728 | &kf_mondata_ops, priv, NULL, NULL); | |
1729 | if (IS_ERR(kn)) | |
1730 | return PTR_ERR(kn); | |
1731 | ||
1732 | ret = rdtgroup_kn_set_ugid(kn); | |
1733 | if (ret) { | |
1734 | kernfs_remove(kn); | |
1735 | return ret; | |
1736 | } | |
1737 | ||
1738 | return ret; | |
1739 | } | |
1740 | ||
895c663e VS |
1741 | /* |
1742 | * Remove all subdirectories of mon_data of ctrl_mon groups | |
1743 | * and monitor groups with given domain id. | |
1744 | */ | |
1745 | void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, unsigned int dom_id) | |
1746 | { | |
1747 | struct rdtgroup *prgrp, *crgrp; | |
1748 | char name[32]; | |
1749 | ||
1750 | if (!r->mon_enabled) | |
1751 | return; | |
1752 | ||
1753 | list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { | |
1754 | sprintf(name, "mon_%s_%02d", r->name, dom_id); | |
1755 | kernfs_remove_by_name(prgrp->mon.mon_data_kn, name); | |
1756 | ||
1757 | list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) | |
1758 | kernfs_remove_by_name(crgrp->mon.mon_data_kn, name); | |
1759 | } | |
1760 | } | |
1761 | ||
d89b7379 VS |
1762 | static int mkdir_mondata_subdir(struct kernfs_node *parent_kn, |
1763 | struct rdt_domain *d, | |
1764 | struct rdt_resource *r, struct rdtgroup *prgrp) | |
1765 | { | |
1766 | union mon_data_bits priv; | |
1767 | struct kernfs_node *kn; | |
1768 | struct mon_evt *mevt; | |
a4de1dfd | 1769 | struct rmid_read rr; |
d89b7379 VS |
1770 | char name[32]; |
1771 | int ret; | |
1772 | ||
1773 | sprintf(name, "mon_%s_%02d", r->name, d->id); | |
1774 | /* create the directory */ | |
1775 | kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp); | |
1776 | if (IS_ERR(kn)) | |
1777 | return PTR_ERR(kn); | |
1778 | ||
1779 | /* | |
1780 | * This extra ref will be put in kernfs_remove() and guarantees | |
1781 | * that kn is always accessible. | |
1782 | */ | |
1783 | kernfs_get(kn); | |
1784 | ret = rdtgroup_kn_set_ugid(kn); | |
1785 | if (ret) | |
1786 | goto out_destroy; | |
1787 | ||
1788 | if (WARN_ON(list_empty(&r->evt_list))) { | |
1789 | ret = -EPERM; | |
1790 | goto out_destroy; | |
1791 | } | |
1792 | ||
1793 | priv.u.rid = r->rid; | |
1794 | priv.u.domid = d->id; | |
1795 | list_for_each_entry(mevt, &r->evt_list, list) { | |
1796 | priv.u.evtid = mevt->evtid; | |
1797 | ret = mon_addfile(kn, mevt->name, priv.priv); | |
1798 | if (ret) | |
1799 | goto out_destroy; | |
a4de1dfd VS |
1800 | |
1801 | if (is_mbm_event(mevt->evtid)) | |
1802 | mon_event_read(&rr, d, prgrp, mevt->evtid, true); | |
d89b7379 VS |
1803 | } |
1804 | kernfs_activate(kn); | |
1805 | return 0; | |
1806 | ||
1807 | out_destroy: | |
1808 | kernfs_remove(kn); | |
1809 | return ret; | |
1810 | } | |
1811 | ||
895c663e VS |
1812 | /* |
1813 | * Add all subdirectories of mon_data for "ctrl_mon" groups | |
1814 | * and "monitor" groups with given domain id. | |
1815 | */ | |
1816 | void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, | |
1817 | struct rdt_domain *d) | |
1818 | { | |
1819 | struct kernfs_node *parent_kn; | |
1820 | struct rdtgroup *prgrp, *crgrp; | |
1821 | struct list_head *head; | |
1822 | ||
1823 | if (!r->mon_enabled) | |
1824 | return; | |
1825 | ||
1826 | list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) { | |
1827 | parent_kn = prgrp->mon.mon_data_kn; | |
1828 | mkdir_mondata_subdir(parent_kn, d, r, prgrp); | |
1829 | ||
1830 | head = &prgrp->mon.crdtgrp_list; | |
1831 | list_for_each_entry(crgrp, head, mon.crdtgrp_list) { | |
1832 | parent_kn = crgrp->mon.mon_data_kn; | |
1833 | mkdir_mondata_subdir(parent_kn, d, r, crgrp); | |
1834 | } | |
1835 | } | |
1836 | } | |
1837 | ||
d89b7379 VS |
1838 | static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn, |
1839 | struct rdt_resource *r, | |
1840 | struct rdtgroup *prgrp) | |
1841 | { | |
1842 | struct rdt_domain *dom; | |
1843 | int ret; | |
1844 | ||
1845 | list_for_each_entry(dom, &r->domains, list) { | |
1846 | ret = mkdir_mondata_subdir(parent_kn, dom, r, prgrp); | |
1847 | if (ret) | |
1848 | return ret; | |
1849 | } | |
1850 | ||
1851 | return 0; | |
1852 | } | |
1853 | ||
1854 | /* | |
1855 | * This creates a directory mon_data which contains the monitored data. | |
1856 | * | |
1857 | * mon_data has one directory for each domain whic are named | |
1858 | * in the format mon_<domain_name>_<domain_id>. For ex: A mon_data | |
1859 | * with L3 domain looks as below: | |
1860 | * ./mon_data: | |
1861 | * mon_L3_00 | |
1862 | * mon_L3_01 | |
1863 | * mon_L3_02 | |
1864 | * ... | |
1865 | * | |
1866 | * Each domain directory has one file per event: | |
1867 | * ./mon_L3_00/: | |
1868 | * llc_occupancy | |
1869 | * | |
1870 | */ | |
1871 | static int mkdir_mondata_all(struct kernfs_node *parent_kn, | |
1872 | struct rdtgroup *prgrp, | |
1873 | struct kernfs_node **dest_kn) | |
1874 | { | |
1875 | struct rdt_resource *r; | |
1876 | struct kernfs_node *kn; | |
1877 | int ret; | |
1878 | ||
1879 | /* | |
1880 | * Create the mon_data directory first. | |
1881 | */ | |
1882 | ret = mongroup_create_dir(parent_kn, NULL, "mon_data", &kn); | |
1883 | if (ret) | |
1884 | return ret; | |
1885 | ||
1886 | if (dest_kn) | |
1887 | *dest_kn = kn; | |
1888 | ||
1889 | /* | |
1890 | * Create the subdirectories for each domain. Note that all events | |
1891 | * in a domain like L3 are grouped into a resource whose domain is L3 | |
1892 | */ | |
1893 | for_each_mon_enabled_rdt_resource(r) { | |
1894 | ret = mkdir_mondata_subdir_alldom(kn, r, prgrp); | |
1895 | if (ret) | |
1896 | goto out_destroy; | |
1897 | } | |
1898 | ||
1899 | return 0; | |
1900 | ||
1901 | out_destroy: | |
1902 | kernfs_remove(kn); | |
1903 | return ret; | |
1904 | } | |
1905 | ||
95f0b77e RC |
1906 | /** |
1907 | * cbm_ensure_valid - Enforce validity on provided CBM | |
1908 | * @_val: Candidate CBM | |
1909 | * @r: RDT resource to which the CBM belongs | |
1910 | * | |
1911 | * The provided CBM represents all cache portions available for use. This | |
1912 | * may be represented by a bitmap that does not consist of contiguous ones | |
1913 | * and thus be an invalid CBM. | |
1914 | * Here the provided CBM is forced to be a valid CBM by only considering | |
1915 | * the first set of contiguous bits as valid and clearing all bits. | |
1916 | * The intention here is to provide a valid default CBM with which a new | |
1917 | * resource group is initialized. The user can follow this with a | |
1918 | * modification to the CBM if the default does not satisfy the | |
1919 | * requirements. | |
1920 | */ | |
1921 | static void cbm_ensure_valid(u32 *_val, struct rdt_resource *r) | |
1922 | { | |
1923 | /* | |
1924 | * Convert the u32 _val to an unsigned long required by all the bit | |
1925 | * operations within this function. No more than 32 bits of this | |
1926 | * converted value can be accessed because all bit operations are | |
1927 | * additionally provided with cbm_len that is initialized during | |
1928 | * hardware enumeration using five bits from the EAX register and | |
1929 | * thus never can exceed 32 bits. | |
1930 | */ | |
1931 | unsigned long *val = (unsigned long *)_val; | |
1932 | unsigned int cbm_len = r->cache.cbm_len; | |
1933 | unsigned long first_bit, zero_bit; | |
1934 | ||
1935 | if (*val == 0) | |
1936 | return; | |
1937 | ||
1938 | first_bit = find_first_bit(val, cbm_len); | |
1939 | zero_bit = find_next_zero_bit(val, cbm_len, first_bit); | |
1940 | ||
1941 | /* Clear any remaining bits to ensure contiguous region */ | |
1942 | bitmap_clear(val, zero_bit, cbm_len - zero_bit); | |
1943 | } | |
1944 | ||
1945 | /** | |
1946 | * rdtgroup_init_alloc - Initialize the new RDT group's allocations | |
1947 | * | |
1948 | * A new RDT group is being created on an allocation capable (CAT) | |
1949 | * supporting system. Set this group up to start off with all usable | |
1950 | * allocations. That is, all shareable and unused bits. | |
1951 | * | |
1952 | * All-zero CBM is invalid. If there are no more shareable bits available | |
1953 | * on any domain then the entire allocation will fail. | |
1954 | */ | |
1955 | static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp) | |
1956 | { | |
1957 | u32 used_b = 0, unused_b = 0; | |
1958 | u32 closid = rdtgrp->closid; | |
1959 | struct rdt_resource *r; | |
1960 | enum rdtgrp_mode mode; | |
1961 | struct rdt_domain *d; | |
1962 | int i, ret; | |
1963 | u32 *ctrl; | |
1964 | ||
1965 | for_each_alloc_enabled_rdt_resource(r) { | |
1966 | list_for_each_entry(d, &r->domains, list) { | |
1967 | d->have_new_ctrl = false; | |
1968 | d->new_ctrl = r->cache.shareable_bits; | |
1969 | used_b = r->cache.shareable_bits; | |
1970 | ctrl = d->ctrl_val; | |
1971 | for (i = 0; i < r->num_closid; i++, ctrl++) { | |
1972 | if (closid_allocated(i) && i != closid) { | |
1973 | mode = rdtgroup_mode_by_closid(i); | |
1974 | used_b |= *ctrl; | |
1975 | if (mode == RDT_MODE_SHAREABLE) | |
1976 | d->new_ctrl |= *ctrl; | |
1977 | } | |
1978 | } | |
1979 | unused_b = used_b ^ (BIT_MASK(r->cache.cbm_len) - 1); | |
1980 | unused_b &= BIT_MASK(r->cache.cbm_len) - 1; | |
1981 | d->new_ctrl |= unused_b; | |
1982 | /* | |
1983 | * Force the initial CBM to be valid, user can | |
1984 | * modify the CBM based on system availability. | |
1985 | */ | |
1986 | cbm_ensure_valid(&d->new_ctrl, r); | |
1987 | if (bitmap_weight((unsigned long *) &d->new_ctrl, | |
1988 | r->cache.cbm_len) < | |
1989 | r->cache.min_cbm_bits) { | |
1990 | rdt_last_cmd_printf("no space on %s:%d\n", | |
1991 | r->name, d->id); | |
1992 | return -ENOSPC; | |
1993 | } | |
1994 | d->have_new_ctrl = true; | |
1995 | } | |
1996 | } | |
1997 | ||
1998 | for_each_alloc_enabled_rdt_resource(r) { | |
1999 | ret = update_domains(r, rdtgrp->closid); | |
2000 | if (ret < 0) { | |
2001 | rdt_last_cmd_puts("failed to initialize allocations\n"); | |
2002 | return ret; | |
2003 | } | |
2004 | rdtgrp->mode = RDT_MODE_SHAREABLE; | |
2005 | } | |
2006 | ||
2007 | return 0; | |
2008 | } | |
2009 | ||
65b4f403 VS |
2010 | static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, |
2011 | struct kernfs_node *prgrp_kn, | |
2012 | const char *name, umode_t mode, | |
c7d9aac6 | 2013 | enum rdt_group_type rtype, struct rdtgroup **r) |
60cf5e10 | 2014 | { |
65b4f403 | 2015 | struct rdtgroup *prdtgrp, *rdtgrp; |
60cf5e10 | 2016 | struct kernfs_node *kn; |
65b4f403 VS |
2017 | uint files = 0; |
2018 | int ret; | |
60cf5e10 | 2019 | |
65b4f403 | 2020 | prdtgrp = rdtgroup_kn_lock_live(prgrp_kn); |
cfd0f34e | 2021 | rdt_last_cmd_clear(); |
65b4f403 | 2022 | if (!prdtgrp) { |
60cf5e10 | 2023 | ret = -ENODEV; |
cfd0f34e | 2024 | rdt_last_cmd_puts("directory was removed\n"); |
60cf5e10 FY |
2025 | goto out_unlock; |
2026 | } | |
2027 | ||
60cf5e10 FY |
2028 | /* allocate the rdtgroup. */ |
2029 | rdtgrp = kzalloc(sizeof(*rdtgrp), GFP_KERNEL); | |
2030 | if (!rdtgrp) { | |
2031 | ret = -ENOSPC; | |
cfd0f34e | 2032 | rdt_last_cmd_puts("kernel out of memory\n"); |
65b4f403 | 2033 | goto out_unlock; |
60cf5e10 | 2034 | } |
65b4f403 | 2035 | *r = rdtgrp; |
c7d9aac6 VS |
2036 | rdtgrp->mon.parent = prdtgrp; |
2037 | rdtgrp->type = rtype; | |
2038 | INIT_LIST_HEAD(&rdtgrp->mon.crdtgrp_list); | |
60cf5e10 FY |
2039 | |
2040 | /* kernfs creates the directory for rdtgrp */ | |
65b4f403 | 2041 | kn = kernfs_create_dir(parent_kn, name, mode, rdtgrp); |
60cf5e10 FY |
2042 | if (IS_ERR(kn)) { |
2043 | ret = PTR_ERR(kn); | |
cfd0f34e | 2044 | rdt_last_cmd_puts("kernfs create error\n"); |
65b4f403 | 2045 | goto out_free_rgrp; |
60cf5e10 FY |
2046 | } |
2047 | rdtgrp->kn = kn; | |
2048 | ||
2049 | /* | |
2050 | * kernfs_remove() will drop the reference count on "kn" which | |
2051 | * will free it. But we still need it to stick around for the | |
2052 | * rdtgroup_kn_unlock(kn} call below. Take one extra reference | |
2053 | * here, which will be dropped inside rdtgroup_kn_unlock(). | |
2054 | */ | |
2055 | kernfs_get(kn); | |
2056 | ||
2057 | ret = rdtgroup_kn_set_ugid(kn); | |
cfd0f34e TL |
2058 | if (ret) { |
2059 | rdt_last_cmd_puts("kernfs perm error\n"); | |
60cf5e10 | 2060 | goto out_destroy; |
cfd0f34e | 2061 | } |
60cf5e10 | 2062 | |
c7d9aac6 | 2063 | files = RFTYPE_BASE | BIT(RF_CTRLSHIFT + rtype); |
65b4f403 | 2064 | ret = rdtgroup_add_files(kn, files); |
cfd0f34e TL |
2065 | if (ret) { |
2066 | rdt_last_cmd_puts("kernfs fill error\n"); | |
12e0110c | 2067 | goto out_destroy; |
cfd0f34e | 2068 | } |
12e0110c | 2069 | |
c7d9aac6 VS |
2070 | if (rdt_mon_capable) { |
2071 | ret = alloc_rmid(); | |
cfd0f34e TL |
2072 | if (ret < 0) { |
2073 | rdt_last_cmd_puts("out of RMIDs\n"); | |
c7d9aac6 | 2074 | goto out_destroy; |
cfd0f34e | 2075 | } |
c7d9aac6 | 2076 | rdtgrp->mon.rmid = ret; |
d89b7379 VS |
2077 | |
2078 | ret = mkdir_mondata_all(kn, rdtgrp, &rdtgrp->mon.mon_data_kn); | |
cfd0f34e TL |
2079 | if (ret) { |
2080 | rdt_last_cmd_puts("kernfs subdir error\n"); | |
d89b7379 | 2081 | goto out_idfree; |
cfd0f34e | 2082 | } |
c7d9aac6 | 2083 | } |
60cf5e10 FY |
2084 | kernfs_activate(kn); |
2085 | ||
65b4f403 VS |
2086 | /* |
2087 | * The caller unlocks the prgrp_kn upon success. | |
2088 | */ | |
2089 | return 0; | |
60cf5e10 | 2090 | |
d89b7379 VS |
2091 | out_idfree: |
2092 | free_rmid(rdtgrp->mon.rmid); | |
60cf5e10 FY |
2093 | out_destroy: |
2094 | kernfs_remove(rdtgrp->kn); | |
65b4f403 | 2095 | out_free_rgrp: |
60cf5e10 | 2096 | kfree(rdtgrp); |
60cf5e10 | 2097 | out_unlock: |
65b4f403 VS |
2098 | rdtgroup_kn_unlock(prgrp_kn); |
2099 | return ret; | |
2100 | } | |
2101 | ||
2102 | static void mkdir_rdt_prepare_clean(struct rdtgroup *rgrp) | |
2103 | { | |
2104 | kernfs_remove(rgrp->kn); | |
c7d9aac6 | 2105 | free_rmid(rgrp->mon.rmid); |
65b4f403 VS |
2106 | kfree(rgrp); |
2107 | } | |
2108 | ||
c7d9aac6 VS |
2109 | /* |
2110 | * Create a monitor group under "mon_groups" directory of a control | |
2111 | * and monitor group(ctrl_mon). This is a resource group | |
2112 | * to monitor a subset of tasks and cpus in its parent ctrl_mon group. | |
2113 | */ | |
2114 | static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn, | |
2115 | struct kernfs_node *prgrp_kn, | |
2116 | const char *name, | |
2117 | umode_t mode) | |
2118 | { | |
2119 | struct rdtgroup *rdtgrp, *prgrp; | |
2120 | int ret; | |
2121 | ||
2122 | ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTMON_GROUP, | |
2123 | &rdtgrp); | |
2124 | if (ret) | |
2125 | return ret; | |
2126 | ||
2127 | prgrp = rdtgrp->mon.parent; | |
2128 | rdtgrp->closid = prgrp->closid; | |
2129 | ||
2130 | /* | |
2131 | * Add the rdtgrp to the list of rdtgrps the parent | |
2132 | * ctrl_mon group has to track. | |
2133 | */ | |
2134 | list_add_tail(&rdtgrp->mon.crdtgrp_list, &prgrp->mon.crdtgrp_list); | |
2135 | ||
2136 | rdtgroup_kn_unlock(prgrp_kn); | |
2137 | return ret; | |
2138 | } | |
2139 | ||
65b4f403 VS |
2140 | /* |
2141 | * These are rdtgroups created under the root directory. Can be used | |
c7d9aac6 | 2142 | * to allocate and monitor resources. |
65b4f403 | 2143 | */ |
c7d9aac6 VS |
2144 | static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn, |
2145 | struct kernfs_node *prgrp_kn, | |
2146 | const char *name, umode_t mode) | |
65b4f403 VS |
2147 | { |
2148 | struct rdtgroup *rdtgrp; | |
2149 | struct kernfs_node *kn; | |
2150 | u32 closid; | |
2151 | int ret; | |
2152 | ||
c7d9aac6 VS |
2153 | ret = mkdir_rdt_prepare(parent_kn, prgrp_kn, name, mode, RDTCTRL_GROUP, |
2154 | &rdtgrp); | |
65b4f403 VS |
2155 | if (ret) |
2156 | return ret; | |
2157 | ||
2158 | kn = rdtgrp->kn; | |
2159 | ret = closid_alloc(); | |
cfd0f34e TL |
2160 | if (ret < 0) { |
2161 | rdt_last_cmd_puts("out of CLOSIDs\n"); | |
65b4f403 | 2162 | goto out_common_fail; |
cfd0f34e | 2163 | } |
65b4f403 | 2164 | closid = ret; |
36e74d35 | 2165 | ret = 0; |
65b4f403 VS |
2166 | |
2167 | rdtgrp->closid = closid; | |
95f0b77e RC |
2168 | ret = rdtgroup_init_alloc(rdtgrp); |
2169 | if (ret < 0) | |
2170 | goto out_id_free; | |
2171 | ||
65b4f403 VS |
2172 | list_add(&rdtgrp->rdtgroup_list, &rdt_all_groups); |
2173 | ||
c7d9aac6 VS |
2174 | if (rdt_mon_capable) { |
2175 | /* | |
2176 | * Create an empty mon_groups directory to hold the subset | |
2177 | * of tasks and cpus to monitor. | |
2178 | */ | |
2179 | ret = mongroup_create_dir(kn, NULL, "mon_groups", NULL); | |
cfd0f34e TL |
2180 | if (ret) { |
2181 | rdt_last_cmd_puts("kernfs subdir error\n"); | |
95f0b77e | 2182 | goto out_del_list; |
cfd0f34e | 2183 | } |
c7d9aac6 VS |
2184 | } |
2185 | ||
65b4f403 VS |
2186 | goto out_unlock; |
2187 | ||
95f0b77e RC |
2188 | out_del_list: |
2189 | list_del(&rdtgrp->rdtgroup_list); | |
c7d9aac6 VS |
2190 | out_id_free: |
2191 | closid_free(closid); | |
65b4f403 VS |
2192 | out_common_fail: |
2193 | mkdir_rdt_prepare_clean(rdtgrp); | |
2194 | out_unlock: | |
2195 | rdtgroup_kn_unlock(prgrp_kn); | |
60cf5e10 FY |
2196 | return ret; |
2197 | } | |
2198 | ||
c7d9aac6 VS |
2199 | /* |
2200 | * We allow creating mon groups only with in a directory called "mon_groups" | |
2201 | * which is present in every ctrl_mon group. Check if this is a valid | |
2202 | * "mon_groups" directory. | |
2203 | * | |
2204 | * 1. The directory should be named "mon_groups". | |
2205 | * 2. The mon group itself should "not" be named "mon_groups". | |
2206 | * This makes sure "mon_groups" directory always has a ctrl_mon group | |
2207 | * as parent. | |
2208 | */ | |
2209 | static bool is_mon_groups(struct kernfs_node *kn, const char *name) | |
2210 | { | |
2211 | return (!strcmp(kn->name, "mon_groups") && | |
2212 | strcmp(name, "mon_groups")); | |
2213 | } | |
2214 | ||
65b4f403 VS |
2215 | static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
2216 | umode_t mode) | |
2217 | { | |
2218 | /* Do not accept '\n' to avoid unparsable situation. */ | |
2219 | if (strchr(name, '\n')) | |
2220 | return -EINVAL; | |
2221 | ||
2222 | /* | |
2223 | * If the parent directory is the root directory and RDT | |
c7d9aac6 VS |
2224 | * allocation is supported, add a control and monitoring |
2225 | * subdirectory | |
65b4f403 VS |
2226 | */ |
2227 | if (rdt_alloc_capable && parent_kn == rdtgroup_default.kn) | |
c7d9aac6 VS |
2228 | return rdtgroup_mkdir_ctrl_mon(parent_kn, parent_kn, name, mode); |
2229 | ||
2230 | /* | |
2231 | * If RDT monitoring is supported and the parent directory is a valid | |
2232 | * "mon_groups" directory, add a monitoring subdirectory. | |
2233 | */ | |
2234 | if (rdt_mon_capable && is_mon_groups(parent_kn, name)) | |
2235 | return rdtgroup_mkdir_mon(parent_kn, parent_kn->parent, name, mode); | |
65b4f403 VS |
2236 | |
2237 | return -EPERM; | |
2238 | } | |
2239 | ||
f3cbeaca VS |
2240 | static int rdtgroup_rmdir_mon(struct kernfs_node *kn, struct rdtgroup *rdtgrp, |
2241 | cpumask_var_t tmpmask) | |
2242 | { | |
2243 | struct rdtgroup *prdtgrp = rdtgrp->mon.parent; | |
2244 | int cpu; | |
2245 | ||
2246 | /* Give any tasks back to the parent group */ | |
2247 | rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask); | |
2248 | ||
2249 | /* Update per cpu rmid of the moved CPUs first */ | |
2250 | for_each_cpu(cpu, &rdtgrp->cpu_mask) | |
a9110b55 | 2251 | per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid; |
f3cbeaca VS |
2252 | /* |
2253 | * Update the MSR on moved CPUs and CPUs which have moved | |
2254 | * task running on them. | |
2255 | */ | |
2256 | cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); | |
2257 | update_closid_rmid(tmpmask, NULL); | |
2258 | ||
2259 | rdtgrp->flags = RDT_DELETED; | |
2260 | free_rmid(rdtgrp->mon.rmid); | |
2261 | ||
2262 | /* | |
2263 | * Remove the rdtgrp from the parent ctrl_mon group's list | |
2264 | */ | |
2265 | WARN_ON(list_empty(&prdtgrp->mon.crdtgrp_list)); | |
2266 | list_del(&rdtgrp->mon.crdtgrp_list); | |
2267 | ||
2268 | /* | |
2269 | * one extra hold on this, will drop when we kfree(rdtgrp) | |
2270 | * in rdtgroup_kn_unlock() | |
2271 | */ | |
2272 | kernfs_get(kn); | |
2273 | kernfs_remove(rdtgrp->kn); | |
2274 | ||
2275 | return 0; | |
2276 | } | |
2277 | ||
f9049547 VS |
2278 | static int rdtgroup_rmdir_ctrl(struct kernfs_node *kn, struct rdtgroup *rdtgrp, |
2279 | cpumask_var_t tmpmask) | |
60cf5e10 | 2280 | { |
f9049547 | 2281 | int cpu; |
60cf5e10 | 2282 | |
e02737d5 | 2283 | /* Give any tasks back to the default group */ |
0efc89be | 2284 | rdt_move_group_tasks(rdtgrp, &rdtgroup_default, tmpmask); |
e02737d5 | 2285 | |
12e0110c TL |
2286 | /* Give any CPUs back to the default group */ |
2287 | cpumask_or(&rdtgroup_default.cpu_mask, | |
2288 | &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); | |
0efc89be | 2289 | |
f3cbeaca VS |
2290 | /* Update per cpu closid and rmid of the moved CPUs first */ |
2291 | for_each_cpu(cpu, &rdtgrp->cpu_mask) { | |
a9110b55 VS |
2292 | per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid; |
2293 | per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid; | |
f3cbeaca VS |
2294 | } |
2295 | ||
0efc89be FY |
2296 | /* |
2297 | * Update the MSR on moved CPUs and CPUs which have moved | |
2298 | * task running on them. | |
2299 | */ | |
2300 | cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask); | |
a9fcf862 | 2301 | update_closid_rmid(tmpmask, NULL); |
12e0110c | 2302 | |
60cf5e10 FY |
2303 | rdtgrp->flags = RDT_DELETED; |
2304 | closid_free(rdtgrp->closid); | |
f3cbeaca VS |
2305 | free_rmid(rdtgrp->mon.rmid); |
2306 | ||
2307 | /* | |
2308 | * Free all the child monitor group rmids. | |
2309 | */ | |
2310 | free_all_child_rdtgrp(rdtgrp); | |
2311 | ||
60cf5e10 FY |
2312 | list_del(&rdtgrp->rdtgroup_list); |
2313 | ||
2314 | /* | |
2315 | * one extra hold on this, will drop when we kfree(rdtgrp) | |
2316 | * in rdtgroup_kn_unlock() | |
2317 | */ | |
2318 | kernfs_get(kn); | |
2319 | kernfs_remove(rdtgrp->kn); | |
f9049547 VS |
2320 | |
2321 | return 0; | |
2322 | } | |
2323 | ||
2324 | static int rdtgroup_rmdir(struct kernfs_node *kn) | |
2325 | { | |
2326 | struct kernfs_node *parent_kn = kn->parent; | |
2327 | struct rdtgroup *rdtgrp; | |
2328 | cpumask_var_t tmpmask; | |
2329 | int ret = 0; | |
2330 | ||
2331 | if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL)) | |
2332 | return -ENOMEM; | |
2333 | ||
2334 | rdtgrp = rdtgroup_kn_lock_live(kn); | |
2335 | if (!rdtgrp) { | |
2336 | ret = -EPERM; | |
2337 | goto out; | |
2338 | } | |
2339 | ||
2340 | /* | |
2341 | * If the rdtgroup is a ctrl_mon group and parent directory | |
f3cbeaca VS |
2342 | * is the root directory, remove the ctrl_mon group. |
2343 | * | |
2344 | * If the rdtgroup is a mon group and parent directory | |
2345 | * is a valid "mon_groups" directory, remove the mon group. | |
f9049547 VS |
2346 | */ |
2347 | if (rdtgrp->type == RDTCTRL_GROUP && parent_kn == rdtgroup_default.kn) | |
2348 | ret = rdtgroup_rmdir_ctrl(kn, rdtgrp, tmpmask); | |
f3cbeaca VS |
2349 | else if (rdtgrp->type == RDTMON_GROUP && |
2350 | is_mon_groups(parent_kn, kn->name)) | |
2351 | ret = rdtgroup_rmdir_mon(kn, rdtgrp, tmpmask); | |
f9049547 VS |
2352 | else |
2353 | ret = -EPERM; | |
2354 | ||
0efc89be | 2355 | out: |
60cf5e10 | 2356 | rdtgroup_kn_unlock(kn); |
0efc89be FY |
2357 | free_cpumask_var(tmpmask); |
2358 | return ret; | |
60cf5e10 FY |
2359 | } |
2360 | ||
76ae054c SL |
2361 | static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf) |
2362 | { | |
1b5c0b75 | 2363 | if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled) |
76ae054c SL |
2364 | seq_puts(seq, ",cdp"); |
2365 | return 0; | |
2366 | } | |
2367 | ||
5ff193fb | 2368 | static struct kernfs_syscall_ops rdtgroup_kf_syscall_ops = { |
76ae054c SL |
2369 | .mkdir = rdtgroup_mkdir, |
2370 | .rmdir = rdtgroup_rmdir, | |
2371 | .show_options = rdtgroup_show_options, | |
5ff193fb FY |
2372 | }; |
2373 | ||
2374 | static int __init rdtgroup_setup_root(void) | |
2375 | { | |
12e0110c TL |
2376 | int ret; |
2377 | ||
5ff193fb FY |
2378 | rdt_root = kernfs_create_root(&rdtgroup_kf_syscall_ops, |
2379 | KERNFS_ROOT_CREATE_DEACTIVATED, | |
2380 | &rdtgroup_default); | |
2381 | if (IS_ERR(rdt_root)) | |
2382 | return PTR_ERR(rdt_root); | |
2383 | ||
2384 | mutex_lock(&rdtgroup_mutex); | |
2385 | ||
2386 | rdtgroup_default.closid = 0; | |
c7d9aac6 VS |
2387 | rdtgroup_default.mon.rmid = 0; |
2388 | rdtgroup_default.type = RDTCTRL_GROUP; | |
2389 | INIT_LIST_HEAD(&rdtgroup_default.mon.crdtgrp_list); | |
2390 | ||
5ff193fb FY |
2391 | list_add(&rdtgroup_default.rdtgroup_list, &rdt_all_groups); |
2392 | ||
5dc1d5c6 | 2393 | ret = rdtgroup_add_files(rdt_root->kn, RF_CTRL_BASE); |
12e0110c TL |
2394 | if (ret) { |
2395 | kernfs_destroy_root(rdt_root); | |
2396 | goto out; | |
2397 | } | |
2398 | ||
5ff193fb FY |
2399 | rdtgroup_default.kn = rdt_root->kn; |
2400 | kernfs_activate(rdtgroup_default.kn); | |
2401 | ||
12e0110c | 2402 | out: |
5ff193fb FY |
2403 | mutex_unlock(&rdtgroup_mutex); |
2404 | ||
12e0110c | 2405 | return ret; |
5ff193fb FY |
2406 | } |
2407 | ||
2408 | /* | |
2409 | * rdtgroup_init - rdtgroup initialization | |
2410 | * | |
2411 | * Setup resctrl file system including set up root, create mount point, | |
2412 | * register rdtgroup filesystem, and initialize files under root directory. | |
2413 | * | |
2414 | * Return: 0 on success or -errno | |
2415 | */ | |
2416 | int __init rdtgroup_init(void) | |
2417 | { | |
2418 | int ret = 0; | |
2419 | ||
9b3a7fd0 TL |
2420 | seq_buf_init(&last_cmd_status, last_cmd_status_buf, |
2421 | sizeof(last_cmd_status_buf)); | |
2422 | ||
5ff193fb FY |
2423 | ret = rdtgroup_setup_root(); |
2424 | if (ret) | |
2425 | return ret; | |
2426 | ||
2427 | ret = sysfs_create_mount_point(fs_kobj, "resctrl"); | |
2428 | if (ret) | |
2429 | goto cleanup_root; | |
2430 | ||
2431 | ret = register_filesystem(&rdt_fs_type); | |
2432 | if (ret) | |
2433 | goto cleanup_mountpoint; | |
2434 | ||
2435 | return 0; | |
2436 | ||
2437 | cleanup_mountpoint: | |
2438 | sysfs_remove_mount_point(fs_kobj, "resctrl"); | |
2439 | cleanup_root: | |
2440 | kernfs_destroy_root(rdt_root); | |
2441 | ||
2442 | return ret; | |
2443 | } |