1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Resource Director Technology (RDT)
4 *
5 * Pseudo-locking support built on top of Cache Allocation Technology (CAT)
6 *
7 * Copyright (C) 2018 Intel Corporation
8 *
9 * Author: Reinette Chatre <reinette.chatre@intel.com>
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/cacheinfo.h>
15 #include <linux/cpu.h>
16 #include <linux/cpumask.h>
17 #include <linux/debugfs.h>
18 #include <linux/kthread.h>
19 #include <linux/mman.h>
20 #include <linux/perf_event.h>
21 #include <linux/pm_qos.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24
25 #include <asm/cacheflush.h>
26 #include <asm/intel-family.h>
27 #include <asm/resctrl_sched.h>
28 #include <asm/perf_event.h>
29
30 #include "../../events/perf_event.h" /* For X86_CONFIG() */
31 #include "internal.h"
32
33 #define CREATE_TRACE_POINTS
34 #include "pseudo_lock_event.h"
35
36 /*
37 * The bits needed to disable hardware prefetching varies based on the
38 * platform. During initialization we will discover which bits to use.
39 */
40 static u64 prefetch_disable_bits;
41
42 /*
43 * Major number assigned to and shared by all devices exposing
44 * pseudo-locked regions.
45 */
46 static unsigned int pseudo_lock_major;
47 static unsigned long pseudo_lock_minor_avail = GENMASK(MINORBITS, 0);
48 static struct class *pseudo_lock_class;
49
50 /**
51 * get_prefetch_disable_bits - prefetch disable bits of supported platforms
52 *
53 * Capture the list of platforms that have been validated to support
54 * pseudo-locking. This includes testing to ensure pseudo-locked regions
55 * with low cache miss rates can be created under variety of load conditions
56 * as well as that these pseudo-locked regions can maintain their low cache
57 * miss rates under variety of load conditions for significant lengths of time.
58 *
59 * After a platform has been validated to support pseudo-locking its
60 * hardware prefetch disable bits are included here as they are documented
61 * in the SDM.
62 *
63 * When adding a platform here also add support for its cache events to
64 * measure_cycles_perf_fn()
65 *
66 * Return:
67 * If platform is supported, the bits to disable hardware prefetchers, 0
68 * if platform is not supported.
69 */
get_prefetch_disable_bits(void)70 static u64 get_prefetch_disable_bits(void)
71 {
72 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
73 boot_cpu_data.x86 != 6)
74 return 0;
75
76 switch (boot_cpu_data.x86_model) {
77 case INTEL_FAM6_BROADWELL_X:
78 /*
79 * SDM defines bits of MSR_MISC_FEATURE_CONTROL register
80 * as:
81 * 0 L2 Hardware Prefetcher Disable (R/W)
82 * 1 L2 Adjacent Cache Line Prefetcher Disable (R/W)
83 * 2 DCU Hardware Prefetcher Disable (R/W)
84 * 3 DCU IP Prefetcher Disable (R/W)
85 * 63:4 Reserved
86 */
87 return 0xF;
88 case INTEL_FAM6_ATOM_GOLDMONT:
89 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
90 /*
91 * SDM defines bits of MSR_MISC_FEATURE_CONTROL register
92 * as:
93 * 0 L2 Hardware Prefetcher Disable (R/W)
94 * 1 Reserved
95 * 2 DCU Hardware Prefetcher Disable (R/W)
96 * 63:3 Reserved
97 */
98 return 0x5;
99 }
100
101 return 0;
102 }
103
104 /**
105 * pseudo_lock_minor_get - Obtain available minor number
106 * @minor: Pointer to where new minor number will be stored
107 *
108 * A bitmask is used to track available minor numbers. Here the next free
109 * minor number is marked as unavailable and returned.
110 *
111 * Return: 0 on success, <0 on failure.
112 */
pseudo_lock_minor_get(unsigned int * minor)113 static int pseudo_lock_minor_get(unsigned int *minor)
114 {
115 unsigned long first_bit;
116
117 first_bit = find_first_bit(&pseudo_lock_minor_avail, MINORBITS);
118
119 if (first_bit == MINORBITS)
120 return -ENOSPC;
121
122 __clear_bit(first_bit, &pseudo_lock_minor_avail);
123 *minor = first_bit;
124
125 return 0;
126 }
127
128 /**
129 * pseudo_lock_minor_release - Return minor number to available
130 * @minor: The minor number made available
131 */
pseudo_lock_minor_release(unsigned int minor)132 static void pseudo_lock_minor_release(unsigned int minor)
133 {
134 __set_bit(minor, &pseudo_lock_minor_avail);
135 }
136
137 /**
138 * region_find_by_minor - Locate a pseudo-lock region by inode minor number
139 * @minor: The minor number of the device representing pseudo-locked region
140 *
141 * When the character device is accessed we need to determine which
142 * pseudo-locked region it belongs to. This is done by matching the minor
143 * number of the device to the pseudo-locked region it belongs.
144 *
145 * Minor numbers are assigned at the time a pseudo-locked region is associated
146 * with a cache instance.
147 *
148 * Return: On success return pointer to resource group owning the pseudo-locked
149 * region, NULL on failure.
150 */
region_find_by_minor(unsigned int minor)151 static struct rdtgroup *region_find_by_minor(unsigned int minor)
152 {
153 struct rdtgroup *rdtgrp, *rdtgrp_match = NULL;
154
155 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
156 if (rdtgrp->plr && rdtgrp->plr->minor == minor) {
157 rdtgrp_match = rdtgrp;
158 break;
159 }
160 }
161 return rdtgrp_match;
162 }
163
164 /**
165 * pseudo_lock_pm_req - A power management QoS request list entry
166 * @list: Entry within the @pm_reqs list for a pseudo-locked region
167 * @req: PM QoS request
168 */
169 struct pseudo_lock_pm_req {
170 struct list_head list;
171 struct dev_pm_qos_request req;
172 };
173
pseudo_lock_cstates_relax(struct pseudo_lock_region * plr)174 static void pseudo_lock_cstates_relax(struct pseudo_lock_region *plr)
175 {
176 struct pseudo_lock_pm_req *pm_req, *next;
177
178 list_for_each_entry_safe(pm_req, next, &plr->pm_reqs, list) {
179 dev_pm_qos_remove_request(&pm_req->req);
180 list_del(&pm_req->list);
181 kfree(pm_req);
182 }
183 }
184
185 /**
186 * pseudo_lock_cstates_constrain - Restrict cores from entering C6
187 *
188 * To prevent the cache from being affected by power management entering
189 * C6 has to be avoided. This is accomplished by requesting a latency
190 * requirement lower than lowest C6 exit latency of all supported
191 * platforms as found in the cpuidle state tables in the intel_idle driver.
192 * At this time it is possible to do so with a single latency requirement
193 * for all supported platforms.
194 *
195 * Since Goldmont is supported, which is affected by X86_BUG_MONITOR,
196 * the ACPI latencies need to be considered while keeping in mind that C2
197 * may be set to map to deeper sleep states. In this case the latency
198 * requirement needs to prevent entering C2 also.
199 */
pseudo_lock_cstates_constrain(struct pseudo_lock_region * plr)200 static int pseudo_lock_cstates_constrain(struct pseudo_lock_region *plr)
201 {
202 struct pseudo_lock_pm_req *pm_req;
203 int cpu;
204 int ret;
205
206 for_each_cpu(cpu, &plr->d->cpu_mask) {
207 pm_req = kzalloc(sizeof(*pm_req), GFP_KERNEL);
208 if (!pm_req) {
209 rdt_last_cmd_puts("Failure to allocate memory for PM QoS\n");
210 ret = -ENOMEM;
211 goto out_err;
212 }
213 ret = dev_pm_qos_add_request(get_cpu_device(cpu),
214 &pm_req->req,
215 DEV_PM_QOS_RESUME_LATENCY,
216 30);
217 if (ret < 0) {
218 rdt_last_cmd_printf("Failed to add latency req CPU%d\n",
219 cpu);
220 kfree(pm_req);
221 ret = -1;
222 goto out_err;
223 }
224 list_add(&pm_req->list, &plr->pm_reqs);
225 }
226
227 return 0;
228
229 out_err:
230 pseudo_lock_cstates_relax(plr);
231 return ret;
232 }
233
234 /**
235 * pseudo_lock_region_clear - Reset pseudo-lock region data
236 * @plr: pseudo-lock region
237 *
238 * All content of the pseudo-locked region is reset - any memory allocated
239 * freed.
240 *
241 * Return: void
242 */
pseudo_lock_region_clear(struct pseudo_lock_region * plr)243 static void pseudo_lock_region_clear(struct pseudo_lock_region *plr)
244 {
245 plr->size = 0;
246 plr->line_size = 0;
247 kfree(plr->kmem);
248 plr->kmem = NULL;
249 plr->r = NULL;
250 if (plr->d)
251 plr->d->plr = NULL;
252 plr->d = NULL;
253 plr->cbm = 0;
254 plr->debugfs_dir = NULL;
255 }
256
257 /**
258 * pseudo_lock_region_init - Initialize pseudo-lock region information
259 * @plr: pseudo-lock region
260 *
261 * Called after user provided a schemata to be pseudo-locked. From the
262 * schemata the &struct pseudo_lock_region is on entry already initialized
263 * with the resource, domain, and capacity bitmask. Here the information
264 * required for pseudo-locking is deduced from this data and &struct
265 * pseudo_lock_region initialized further. This information includes:
266 * - size in bytes of the region to be pseudo-locked
267 * - cache line size to know the stride with which data needs to be accessed
268 * to be pseudo-locked
269 * - a cpu associated with the cache instance on which the pseudo-locking
270 * flow can be executed
271 *
272 * Return: 0 on success, <0 on failure. Descriptive error will be written
273 * to last_cmd_status buffer.
274 */
pseudo_lock_region_init(struct pseudo_lock_region * plr)275 static int pseudo_lock_region_init(struct pseudo_lock_region *plr)
276 {
277 struct cpu_cacheinfo *ci;
278 int ret;
279 int i;
280
281 /* Pick the first cpu we find that is associated with the cache. */
282 plr->cpu = cpumask_first(&plr->d->cpu_mask);
283
284 if (!cpu_online(plr->cpu)) {
285 rdt_last_cmd_printf("CPU %u associated with cache not online\n",
286 plr->cpu);
287 ret = -ENODEV;
288 goto out_region;
289 }
290
291 ci = get_cpu_cacheinfo(plr->cpu);
292
293 plr->size = rdtgroup_cbm_to_size(plr->r, plr->d, plr->cbm);
294
295 for (i = 0; i < ci->num_leaves; i++) {
296 if (ci->info_list[i].level == plr->r->cache_level) {
297 plr->line_size = ci->info_list[i].coherency_line_size;
298 return 0;
299 }
300 }
301
302 ret = -1;
303 rdt_last_cmd_puts("Unable to determine cache line size\n");
304 out_region:
305 pseudo_lock_region_clear(plr);
306 return ret;
307 }
308
309 /**
310 * pseudo_lock_init - Initialize a pseudo-lock region
311 * @rdtgrp: resource group to which new pseudo-locked region will belong
312 *
313 * A pseudo-locked region is associated with a resource group. When this
314 * association is created the pseudo-locked region is initialized. The
315 * details of the pseudo-locked region are not known at this time so only
316 * allocation is done and association established.
317 *
318 * Return: 0 on success, <0 on failure
319 */
pseudo_lock_init(struct rdtgroup * rdtgrp)320 static int pseudo_lock_init(struct rdtgroup *rdtgrp)
321 {
322 struct pseudo_lock_region *plr;
323
324 plr = kzalloc(sizeof(*plr), GFP_KERNEL);
325 if (!plr)
326 return -ENOMEM;
327
328 init_waitqueue_head(&plr->lock_thread_wq);
329 INIT_LIST_HEAD(&plr->pm_reqs);
330 rdtgrp->plr = plr;
331 return 0;
332 }
333
334 /**
335 * pseudo_lock_region_alloc - Allocate kernel memory that will be pseudo-locked
336 * @plr: pseudo-lock region
337 *
338 * Initialize the details required to set up the pseudo-locked region and
339 * allocate the contiguous memory that will be pseudo-locked to the cache.
340 *
341 * Return: 0 on success, <0 on failure. Descriptive error will be written
342 * to last_cmd_status buffer.
343 */
pseudo_lock_region_alloc(struct pseudo_lock_region * plr)344 static int pseudo_lock_region_alloc(struct pseudo_lock_region *plr)
345 {
346 int ret;
347
348 ret = pseudo_lock_region_init(plr);
349 if (ret < 0)
350 return ret;
351
352 /*
353 * We do not yet support contiguous regions larger than
354 * KMALLOC_MAX_SIZE.
355 */
356 if (plr->size > KMALLOC_MAX_SIZE) {
357 rdt_last_cmd_puts("Requested region exceeds maximum size\n");
358 ret = -E2BIG;
359 goto out_region;
360 }
361
362 plr->kmem = kzalloc(plr->size, GFP_KERNEL);
363 if (!plr->kmem) {
364 rdt_last_cmd_puts("Unable to allocate memory\n");
365 ret = -ENOMEM;
366 goto out_region;
367 }
368
369 ret = 0;
370 goto out;
371 out_region:
372 pseudo_lock_region_clear(plr);
373 out:
374 return ret;
375 }
376
377 /**
378 * pseudo_lock_free - Free a pseudo-locked region
379 * @rdtgrp: resource group to which pseudo-locked region belonged
380 *
381 * The pseudo-locked region's resources have already been released, or not
382 * yet created at this point. Now it can be freed and disassociated from the
383 * resource group.
384 *
385 * Return: void
386 */
pseudo_lock_free(struct rdtgroup * rdtgrp)387 static void pseudo_lock_free(struct rdtgroup *rdtgrp)
388 {
389 pseudo_lock_region_clear(rdtgrp->plr);
390 kfree(rdtgrp->plr);
391 rdtgrp->plr = NULL;
392 }
393
394 /**
395 * pseudo_lock_fn - Load kernel memory into cache
396 * @_rdtgrp: resource group to which pseudo-lock region belongs
397 *
398 * This is the core pseudo-locking flow.
399 *
400 * First we ensure that the kernel memory cannot be found in the cache.
401 * Then, while taking care that there will be as little interference as
402 * possible, the memory to be loaded is accessed while core is running
403 * with class of service set to the bitmask of the pseudo-locked region.
404 * After this is complete no future CAT allocations will be allowed to
405 * overlap with this bitmask.
406 *
407 * Local register variables are utilized to ensure that the memory region
408 * to be locked is the only memory access made during the critical locking
409 * loop.
410 *
411 * Return: 0. Waiter on waitqueue will be woken on completion.
412 */
pseudo_lock_fn(void * _rdtgrp)413 static int pseudo_lock_fn(void *_rdtgrp)
414 {
415 struct rdtgroup *rdtgrp = _rdtgrp;
416 struct pseudo_lock_region *plr = rdtgrp->plr;
417 u32 rmid_p, closid_p;
418 unsigned long i;
419 u64 saved_msr;
420 #ifdef CONFIG_KASAN
421 /*
422 * The registers used for local register variables are also used
423 * when KASAN is active. When KASAN is active we use a regular
424 * variable to ensure we always use a valid pointer, but the cost
425 * is that this variable will enter the cache through evicting the
426 * memory we are trying to lock into the cache. Thus expect lower
427 * pseudo-locking success rate when KASAN is active.
428 */
429 unsigned int line_size;
430 unsigned int size;
431 void *mem_r;
432 #else
433 register unsigned int line_size asm("esi");
434 register unsigned int size asm("edi");
435 register void *mem_r asm(_ASM_BX);
436 #endif /* CONFIG_KASAN */
437
438 /*
439 * Make sure none of the allocated memory is cached. If it is we
440 * will get a cache hit in below loop from outside of pseudo-locked
441 * region.
442 * wbinvd (as opposed to clflush/clflushopt) is required to
443 * increase likelihood that allocated cache portion will be filled
444 * with associated memory.
445 */
446 native_wbinvd();
447
448 /*
449 * Always called with interrupts enabled. By disabling interrupts
450 * ensure that we will not be preempted during this critical section.
451 */
452 local_irq_disable();
453
454 /*
455 * Call wrmsr and rdmsr as directly as possible to avoid tracing
456 * clobbering local register variables or affecting cache accesses.
457 *
458 * Disable the hardware prefetcher so that when the end of the memory
459 * being pseudo-locked is reached the hardware will not read beyond
460 * the buffer and evict pseudo-locked memory read earlier from the
461 * cache.
462 */
463 saved_msr = __rdmsr(MSR_MISC_FEATURE_CONTROL);
464 __wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
465 closid_p = this_cpu_read(pqr_state.cur_closid);
466 rmid_p = this_cpu_read(pqr_state.cur_rmid);
467 mem_r = plr->kmem;
468 size = plr->size;
469 line_size = plr->line_size;
470 /*
471 * Critical section begin: start by writing the closid associated
472 * with the capacity bitmask of the cache region being
473 * pseudo-locked followed by reading of kernel memory to load it
474 * into the cache.
475 */
476 __wrmsr(IA32_PQR_ASSOC, rmid_p, rdtgrp->closid);
477 /*
478 * Cache was flushed earlier. Now access kernel memory to read it
479 * into cache region associated with just activated plr->closid.
480 * Loop over data twice:
481 * - In first loop the cache region is shared with the page walker
482 * as it populates the paging structure caches (including TLB).
483 * - In the second loop the paging structure caches are used and
484 * cache region is populated with the memory being referenced.
485 */
486 for (i = 0; i < size; i += PAGE_SIZE) {
487 /*
488 * Add a barrier to prevent speculative execution of this
489 * loop reading beyond the end of the buffer.
490 */
491 rmb();
492 asm volatile("mov (%0,%1,1), %%eax\n\t"
493 :
494 : "r" (mem_r), "r" (i)
495 : "%eax", "memory");
496 }
497 for (i = 0; i < size; i += line_size) {
498 /*
499 * Add a barrier to prevent speculative execution of this
500 * loop reading beyond the end of the buffer.
501 */
502 rmb();
503 asm volatile("mov (%0,%1,1), %%eax\n\t"
504 :
505 : "r" (mem_r), "r" (i)
506 : "%eax", "memory");
507 }
508 /*
509 * Critical section end: restore closid with capacity bitmask that
510 * does not overlap with pseudo-locked region.
511 */
512 __wrmsr(IA32_PQR_ASSOC, rmid_p, closid_p);
513
514 /* Re-enable the hardware prefetcher(s) */
515 wrmsrl(MSR_MISC_FEATURE_CONTROL, saved_msr);
516 local_irq_enable();
517
518 plr->thread_done = 1;
519 wake_up_interruptible(&plr->lock_thread_wq);
520 return 0;
521 }
522
523 /**
524 * rdtgroup_monitor_in_progress - Test if monitoring in progress
525 * @r: resource group being queried
526 *
527 * Return: 1 if monitor groups have been created for this resource
528 * group, 0 otherwise.
529 */
rdtgroup_monitor_in_progress(struct rdtgroup * rdtgrp)530 static int rdtgroup_monitor_in_progress(struct rdtgroup *rdtgrp)
531 {
532 return !list_empty(&rdtgrp->mon.crdtgrp_list);
533 }
534
535 /**
536 * rdtgroup_locksetup_user_restrict - Restrict user access to group
537 * @rdtgrp: resource group needing access restricted
538 *
539 * A resource group used for cache pseudo-locking cannot have cpus or tasks
540 * assigned to it. This is communicated to the user by restricting access
541 * to all the files that can be used to make such changes.
542 *
543 * Permissions restored with rdtgroup_locksetup_user_restore()
544 *
545 * Return: 0 on success, <0 on failure. If a failure occurs during the
546 * restriction of access an attempt will be made to restore permissions but
547 * the state of the mode of these files will be uncertain when a failure
548 * occurs.
549 */
rdtgroup_locksetup_user_restrict(struct rdtgroup * rdtgrp)550 static int rdtgroup_locksetup_user_restrict(struct rdtgroup *rdtgrp)
551 {
552 int ret;
553
554 ret = rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
555 if (ret)
556 return ret;
557
558 ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
559 if (ret)
560 goto err_tasks;
561
562 ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
563 if (ret)
564 goto err_cpus;
565
566 if (rdt_mon_capable) {
567 ret = rdtgroup_kn_mode_restrict(rdtgrp, "mon_groups");
568 if (ret)
569 goto err_cpus_list;
570 }
571
572 ret = 0;
573 goto out;
574
575 err_cpus_list:
576 rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
577 err_cpus:
578 rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
579 err_tasks:
580 rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
581 out:
582 return ret;
583 }
584
585 /**
586 * rdtgroup_locksetup_user_restore - Restore user access to group
587 * @rdtgrp: resource group needing access restored
588 *
589 * Restore all file access previously removed using
590 * rdtgroup_locksetup_user_restrict()
591 *
592 * Return: 0 on success, <0 on failure. If a failure occurs during the
593 * restoration of access an attempt will be made to restrict permissions
594 * again but the state of the mode of these files will be uncertain when
595 * a failure occurs.
596 */
rdtgroup_locksetup_user_restore(struct rdtgroup * rdtgrp)597 static int rdtgroup_locksetup_user_restore(struct rdtgroup *rdtgrp)
598 {
599 int ret;
600
601 ret = rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
602 if (ret)
603 return ret;
604
605 ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
606 if (ret)
607 goto err_tasks;
608
609 ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
610 if (ret)
611 goto err_cpus;
612
613 if (rdt_mon_capable) {
614 ret = rdtgroup_kn_mode_restore(rdtgrp, "mon_groups", 0777);
615 if (ret)
616 goto err_cpus_list;
617 }
618
619 ret = 0;
620 goto out;
621
622 err_cpus_list:
623 rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
624 err_cpus:
625 rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
626 err_tasks:
627 rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
628 out:
629 return ret;
630 }
631
632 /**
633 * rdtgroup_locksetup_enter - Resource group enters locksetup mode
634 * @rdtgrp: resource group requested to enter locksetup mode
635 *
636 * A resource group enters locksetup mode to reflect that it would be used
637 * to represent a pseudo-locked region and is in the process of being set
638 * up to do so. A resource group used for a pseudo-locked region would
639 * lose the closid associated with it so we cannot allow it to have any
640 * tasks or cpus assigned nor permit tasks or cpus to be assigned in the
641 * future. Monitoring of a pseudo-locked region is not allowed either.
642 *
643 * The above and more restrictions on a pseudo-locked region are checked
644 * for and enforced before the resource group enters the locksetup mode.
645 *
646 * Returns: 0 if the resource group successfully entered locksetup mode, <0
647 * on failure. On failure the last_cmd_status buffer is updated with text to
648 * communicate details of failure to the user.
649 */
rdtgroup_locksetup_enter(struct rdtgroup * rdtgrp)650 int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
651 {
652 int ret;
653
654 /*
655 * The default resource group can neither be removed nor lose the
656 * default closid associated with it.
657 */
658 if (rdtgrp == &rdtgroup_default) {
659 rdt_last_cmd_puts("Cannot pseudo-lock default group\n");
660 return -EINVAL;
661 }
662
663 /*
664 * Cache Pseudo-locking not supported when CDP is enabled.
665 *
666 * Some things to consider if you would like to enable this
667 * support (using L3 CDP as example):
668 * - When CDP is enabled two separate resources are exposed,
669 * L3DATA and L3CODE, but they are actually on the same cache.
670 * The implication for pseudo-locking is that if a
671 * pseudo-locked region is created on a domain of one
672 * resource (eg. L3CODE), then a pseudo-locked region cannot
673 * be created on that same domain of the other resource
674 * (eg. L3DATA). This is because the creation of a
675 * pseudo-locked region involves a call to wbinvd that will
676 * affect all cache allocations on particular domain.
677 * - Considering the previous, it may be possible to only
678 * expose one of the CDP resources to pseudo-locking and
679 * hide the other. For example, we could consider to only
680 * expose L3DATA and since the L3 cache is unified it is
681 * still possible to place instructions there are execute it.
682 * - If only one region is exposed to pseudo-locking we should
683 * still keep in mind that availability of a portion of cache
684 * for pseudo-locking should take into account both resources.
685 * Similarly, if a pseudo-locked region is created in one
686 * resource, the portion of cache used by it should be made
687 * unavailable to all future allocations from both resources.
688 */
689 if (rdt_resources_all[RDT_RESOURCE_L3DATA].alloc_enabled ||
690 rdt_resources_all[RDT_RESOURCE_L2DATA].alloc_enabled) {
691 rdt_last_cmd_puts("CDP enabled\n");
692 return -EINVAL;
693 }
694
695 /*
696 * Not knowing the bits to disable prefetching implies that this
697 * platform does not support Cache Pseudo-Locking.
698 */
699 prefetch_disable_bits = get_prefetch_disable_bits();
700 if (prefetch_disable_bits == 0) {
701 rdt_last_cmd_puts("Pseudo-locking not supported\n");
702 return -EINVAL;
703 }
704
705 if (rdtgroup_monitor_in_progress(rdtgrp)) {
706 rdt_last_cmd_puts("Monitoring in progress\n");
707 return -EINVAL;
708 }
709
710 if (rdtgroup_tasks_assigned(rdtgrp)) {
711 rdt_last_cmd_puts("Tasks assigned to resource group\n");
712 return -EINVAL;
713 }
714
715 if (!cpumask_empty(&rdtgrp->cpu_mask)) {
716 rdt_last_cmd_puts("CPUs assigned to resource group\n");
717 return -EINVAL;
718 }
719
720 if (rdtgroup_locksetup_user_restrict(rdtgrp)) {
721 rdt_last_cmd_puts("Unable to modify resctrl permissions\n");
722 return -EIO;
723 }
724
725 ret = pseudo_lock_init(rdtgrp);
726 if (ret) {
727 rdt_last_cmd_puts("Unable to init pseudo-lock region\n");
728 goto out_release;
729 }
730
731 /*
732 * If this system is capable of monitoring a rmid would have been
733 * allocated when the control group was created. This is not needed
734 * anymore when this group would be used for pseudo-locking. This
735 * is safe to call on platforms not capable of monitoring.
736 */
737 free_rmid(rdtgrp->mon.rmid);
738
739 ret = 0;
740 goto out;
741
742 out_release:
743 rdtgroup_locksetup_user_restore(rdtgrp);
744 out:
745 return ret;
746 }
747
748 /**
749 * rdtgroup_locksetup_exit - resource group exist locksetup mode
750 * @rdtgrp: resource group
751 *
752 * When a resource group exits locksetup mode the earlier restrictions are
753 * lifted.
754 *
755 * Return: 0 on success, <0 on failure
756 */
rdtgroup_locksetup_exit(struct rdtgroup * rdtgrp)757 int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp)
758 {
759 int ret;
760
761 if (rdt_mon_capable) {
762 ret = alloc_rmid();
763 if (ret < 0) {
764 rdt_last_cmd_puts("Out of RMIDs\n");
765 return ret;
766 }
767 rdtgrp->mon.rmid = ret;
768 }
769
770 ret = rdtgroup_locksetup_user_restore(rdtgrp);
771 if (ret) {
772 free_rmid(rdtgrp->mon.rmid);
773 return ret;
774 }
775
776 pseudo_lock_free(rdtgrp);
777 return 0;
778 }
779
780 /**
781 * rdtgroup_cbm_overlaps_pseudo_locked - Test if CBM or portion is pseudo-locked
782 * @d: RDT domain
783 * @cbm: CBM to test
784 *
785 * @d represents a cache instance and @cbm a capacity bitmask that is
786 * considered for it. Determine if @cbm overlaps with any existing
787 * pseudo-locked region on @d.
788 *
789 * @cbm is unsigned long, even if only 32 bits are used, to make the
790 * bitmap functions work correctly.
791 *
792 * Return: true if @cbm overlaps with pseudo-locked region on @d, false
793 * otherwise.
794 */
rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain * d,unsigned long cbm)795 bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm)
796 {
797 unsigned int cbm_len;
798 unsigned long cbm_b;
799
800 if (d->plr) {
801 cbm_len = d->plr->r->cache.cbm_len;
802 cbm_b = d->plr->cbm;
803 if (bitmap_intersects(&cbm, &cbm_b, cbm_len))
804 return true;
805 }
806 return false;
807 }
808
809 /**
810 * rdtgroup_pseudo_locked_in_hierarchy - Pseudo-locked region in cache hierarchy
811 * @d: RDT domain under test
812 *
813 * The setup of a pseudo-locked region affects all cache instances within
814 * the hierarchy of the region. It is thus essential to know if any
815 * pseudo-locked regions exist within a cache hierarchy to prevent any
816 * attempts to create new pseudo-locked regions in the same hierarchy.
817 *
818 * Return: true if a pseudo-locked region exists in the hierarchy of @d or
819 * if it is not possible to test due to memory allocation issue,
820 * false otherwise.
821 */
rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain * d)822 bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d)
823 {
824 cpumask_var_t cpu_with_psl;
825 struct rdt_resource *r;
826 struct rdt_domain *d_i;
827 bool ret = false;
828
829 if (!zalloc_cpumask_var(&cpu_with_psl, GFP_KERNEL))
830 return true;
831
832 /*
833 * First determine which cpus have pseudo-locked regions
834 * associated with them.
835 */
836 for_each_alloc_enabled_rdt_resource(r) {
837 list_for_each_entry(d_i, &r->domains, list) {
838 if (d_i->plr)
839 cpumask_or(cpu_with_psl, cpu_with_psl,
840 &d_i->cpu_mask);
841 }
842 }
843
844 /*
845 * Next test if new pseudo-locked region would intersect with
846 * existing region.
847 */
848 if (cpumask_intersects(&d->cpu_mask, cpu_with_psl))
849 ret = true;
850
851 free_cpumask_var(cpu_with_psl);
852 return ret;
853 }
854
855 /**
856 * measure_cycles_lat_fn - Measure cycle latency to read pseudo-locked memory
857 * @_plr: pseudo-lock region to measure
858 *
859 * There is no deterministic way to test if a memory region is cached. One
860 * way is to measure how long it takes to read the memory, the speed of
861 * access is a good way to learn how close to the cpu the data was. Even
862 * more, if the prefetcher is disabled and the memory is read at a stride
863 * of half the cache line, then a cache miss will be easy to spot since the
864 * read of the first half would be significantly slower than the read of
865 * the second half.
866 *
867 * Return: 0. Waiter on waitqueue will be woken on completion.
868 */
measure_cycles_lat_fn(void * _plr)869 static int measure_cycles_lat_fn(void *_plr)
870 {
871 struct pseudo_lock_region *plr = _plr;
872 u32 saved_low, saved_high;
873 unsigned long i;
874 u64 start, end;
875 void *mem_r;
876
877 local_irq_disable();
878 /*
879 * Disable hardware prefetchers.
880 */
881 rdmsr(MSR_MISC_FEATURE_CONTROL, saved_low, saved_high);
882 wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
883 mem_r = READ_ONCE(plr->kmem);
884 /*
885 * Dummy execute of the time measurement to load the needed
886 * instructions into the L1 instruction cache.
887 */
888 start = rdtsc_ordered();
889 for (i = 0; i < plr->size; i += 32) {
890 start = rdtsc_ordered();
891 asm volatile("mov (%0,%1,1), %%eax\n\t"
892 :
893 : "r" (mem_r), "r" (i)
894 : "%eax", "memory");
895 end = rdtsc_ordered();
896 trace_pseudo_lock_mem_latency((u32)(end - start));
897 }
898 wrmsr(MSR_MISC_FEATURE_CONTROL, saved_low, saved_high);
899 local_irq_enable();
900 plr->thread_done = 1;
901 wake_up_interruptible(&plr->lock_thread_wq);
902 return 0;
903 }
904
905 /*
906 * Create a perf_event_attr for the hit and miss perf events that will
907 * be used during the performance measurement. A perf_event maintains
908 * a pointer to its perf_event_attr so a unique attribute structure is
909 * created for each perf_event.
910 *
911 * The actual configuration of the event is set right before use in order
912 * to use the X86_CONFIG macro.
913 */
914 static struct perf_event_attr perf_miss_attr = {
915 .type = PERF_TYPE_RAW,
916 .size = sizeof(struct perf_event_attr),
917 .pinned = 1,
918 .disabled = 0,
919 .exclude_user = 1,
920 };
921
922 static struct perf_event_attr perf_hit_attr = {
923 .type = PERF_TYPE_RAW,
924 .size = sizeof(struct perf_event_attr),
925 .pinned = 1,
926 .disabled = 0,
927 .exclude_user = 1,
928 };
929
930 struct residency_counts {
931 u64 miss_before, hits_before;
932 u64 miss_after, hits_after;
933 };
934
measure_residency_fn(struct perf_event_attr * miss_attr,struct perf_event_attr * hit_attr,struct pseudo_lock_region * plr,struct residency_counts * counts)935 static int measure_residency_fn(struct perf_event_attr *miss_attr,
936 struct perf_event_attr *hit_attr,
937 struct pseudo_lock_region *plr,
938 struct residency_counts *counts)
939 {
940 u64 hits_before = 0, hits_after = 0, miss_before = 0, miss_after = 0;
941 struct perf_event *miss_event, *hit_event;
942 int hit_pmcnum, miss_pmcnum;
943 u32 saved_low, saved_high;
944 unsigned int line_size;
945 unsigned int size;
946 unsigned long i;
947 void *mem_r;
948 u64 tmp;
949
950 miss_event = perf_event_create_kernel_counter(miss_attr, plr->cpu,
951 NULL, NULL, NULL);
952 if (IS_ERR(miss_event))
953 goto out;
954
955 hit_event = perf_event_create_kernel_counter(hit_attr, plr->cpu,
956 NULL, NULL, NULL);
957 if (IS_ERR(hit_event))
958 goto out_miss;
959
960 local_irq_disable();
961 /*
962 * Check any possible error state of events used by performing
963 * one local read.
964 */
965 if (perf_event_read_local(miss_event, &tmp, NULL, NULL)) {
966 local_irq_enable();
967 goto out_hit;
968 }
969 if (perf_event_read_local(hit_event, &tmp, NULL, NULL)) {
970 local_irq_enable();
971 goto out_hit;
972 }
973
974 /*
975 * Disable hardware prefetchers.
976 */
977 rdmsr(MSR_MISC_FEATURE_CONTROL, saved_low, saved_high);
978 wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
979
980 /* Initialize rest of local variables */
981 /*
982 * Performance event has been validated right before this with
983 * interrupts disabled - it is thus safe to read the counter index.
984 */
985 miss_pmcnum = x86_perf_rdpmc_index(miss_event);
986 hit_pmcnum = x86_perf_rdpmc_index(hit_event);
987 line_size = READ_ONCE(plr->line_size);
988 mem_r = READ_ONCE(plr->kmem);
989 size = READ_ONCE(plr->size);
990
991 /*
992 * Read counter variables twice - first to load the instructions
993 * used in L1 cache, second to capture accurate value that does not
994 * include cache misses incurred because of instruction loads.
995 */
996 rdpmcl(hit_pmcnum, hits_before);
997 rdpmcl(miss_pmcnum, miss_before);
998 /*
999 * From SDM: Performing back-to-back fast reads are not guaranteed
1000 * to be monotonic.
1001 * Use LFENCE to ensure all previous instructions are retired
1002 * before proceeding.
1003 */
1004 rmb();
1005 rdpmcl(hit_pmcnum, hits_before);
1006 rdpmcl(miss_pmcnum, miss_before);
1007 /*
1008 * Use LFENCE to ensure all previous instructions are retired
1009 * before proceeding.
1010 */
1011 rmb();
1012 for (i = 0; i < size; i += line_size) {
1013 /*
1014 * Add a barrier to prevent speculative execution of this
1015 * loop reading beyond the end of the buffer.
1016 */
1017 rmb();
1018 asm volatile("mov (%0,%1,1), %%eax\n\t"
1019 :
1020 : "r" (mem_r), "r" (i)
1021 : "%eax", "memory");
1022 }
1023 /*
1024 * Use LFENCE to ensure all previous instructions are retired
1025 * before proceeding.
1026 */
1027 rmb();
1028 rdpmcl(hit_pmcnum, hits_after);
1029 rdpmcl(miss_pmcnum, miss_after);
1030 /*
1031 * Use LFENCE to ensure all previous instructions are retired
1032 * before proceeding.
1033 */
1034 rmb();
1035 /* Re-enable hardware prefetchers */
1036 wrmsr(MSR_MISC_FEATURE_CONTROL, saved_low, saved_high);
1037 local_irq_enable();
1038 out_hit:
1039 perf_event_release_kernel(hit_event);
1040 out_miss:
1041 perf_event_release_kernel(miss_event);
1042 out:
1043 /*
1044 * All counts will be zero on failure.
1045 */
1046 counts->miss_before = miss_before;
1047 counts->hits_before = hits_before;
1048 counts->miss_after = miss_after;
1049 counts->hits_after = hits_after;
1050 return 0;
1051 }
1052
measure_l2_residency(void * _plr)1053 static int measure_l2_residency(void *_plr)
1054 {
1055 struct pseudo_lock_region *plr = _plr;
1056 struct residency_counts counts = {0};
1057
1058 /*
1059 * Non-architectural event for the Goldmont Microarchitecture
1060 * from Intel x86 Architecture Software Developer Manual (SDM):
1061 * MEM_LOAD_UOPS_RETIRED D1H (event number)
1062 * Umask values:
1063 * L2_HIT 02H
1064 * L2_MISS 10H
1065 */
1066 switch (boot_cpu_data.x86_model) {
1067 case INTEL_FAM6_ATOM_GOLDMONT:
1068 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
1069 perf_miss_attr.config = X86_CONFIG(.event = 0xd1,
1070 .umask = 0x10);
1071 perf_hit_attr.config = X86_CONFIG(.event = 0xd1,
1072 .umask = 0x2);
1073 break;
1074 default:
1075 goto out;
1076 }
1077
1078 measure_residency_fn(&perf_miss_attr, &perf_hit_attr, plr, &counts);
1079 /*
1080 * If a failure prevented the measurements from succeeding
1081 * tracepoints will still be written and all counts will be zero.
1082 */
1083 trace_pseudo_lock_l2(counts.hits_after - counts.hits_before,
1084 counts.miss_after - counts.miss_before);
1085 out:
1086 plr->thread_done = 1;
1087 wake_up_interruptible(&plr->lock_thread_wq);
1088 return 0;
1089 }
1090
measure_l3_residency(void * _plr)1091 static int measure_l3_residency(void *_plr)
1092 {
1093 struct pseudo_lock_region *plr = _plr;
1094 struct residency_counts counts = {0};
1095
1096 /*
1097 * On Broadwell Microarchitecture the MEM_LOAD_UOPS_RETIRED event
1098 * has two "no fix" errata associated with it: BDM35 and BDM100. On
1099 * this platform the following events are used instead:
1100 * LONGEST_LAT_CACHE 2EH (Documented in SDM)
1101 * REFERENCE 4FH
1102 * MISS 41H
1103 */
1104
1105 switch (boot_cpu_data.x86_model) {
1106 case INTEL_FAM6_BROADWELL_X:
1107 /* On BDW the hit event counts references, not hits */
1108 perf_hit_attr.config = X86_CONFIG(.event = 0x2e,
1109 .umask = 0x4f);
1110 perf_miss_attr.config = X86_CONFIG(.event = 0x2e,
1111 .umask = 0x41);
1112 break;
1113 default:
1114 goto out;
1115 }
1116
1117 measure_residency_fn(&perf_miss_attr, &perf_hit_attr, plr, &counts);
1118 /*
1119 * If a failure prevented the measurements from succeeding
1120 * tracepoints will still be written and all counts will be zero.
1121 */
1122
1123 counts.miss_after -= counts.miss_before;
1124 if (boot_cpu_data.x86_model == INTEL_FAM6_BROADWELL_X) {
1125 /*
1126 * On BDW references and misses are counted, need to adjust.
1127 * Sometimes the "hits" counter is a bit more than the
1128 * references, for example, x references but x + 1 hits.
1129 * To not report invalid hit values in this case we treat
1130 * that as misses equal to references.
1131 */
1132 /* First compute the number of cache references measured */
1133 counts.hits_after -= counts.hits_before;
1134 /* Next convert references to cache hits */
1135 counts.hits_after -= min(counts.miss_after, counts.hits_after);
1136 } else {
1137 counts.hits_after -= counts.hits_before;
1138 }
1139
1140 trace_pseudo_lock_l3(counts.hits_after, counts.miss_after);
1141 out:
1142 plr->thread_done = 1;
1143 wake_up_interruptible(&plr->lock_thread_wq);
1144 return 0;
1145 }
1146
1147 /**
1148 * pseudo_lock_measure_cycles - Trigger latency measure to pseudo-locked region
1149 *
1150 * The measurement of latency to access a pseudo-locked region should be
1151 * done from a cpu that is associated with that pseudo-locked region.
1152 * Determine which cpu is associated with this region and start a thread on
1153 * that cpu to perform the measurement, wait for that thread to complete.
1154 *
1155 * Return: 0 on success, <0 on failure
1156 */
pseudo_lock_measure_cycles(struct rdtgroup * rdtgrp,int sel)1157 static int pseudo_lock_measure_cycles(struct rdtgroup *rdtgrp, int sel)
1158 {
1159 struct pseudo_lock_region *plr = rdtgrp->plr;
1160 struct task_struct *thread;
1161 unsigned int cpu;
1162 int ret = -1;
1163
1164 cpus_read_lock();
1165 mutex_lock(&rdtgroup_mutex);
1166
1167 if (rdtgrp->flags & RDT_DELETED) {
1168 ret = -ENODEV;
1169 goto out;
1170 }
1171
1172 if (!plr->d) {
1173 ret = -ENODEV;
1174 goto out;
1175 }
1176
1177 plr->thread_done = 0;
1178 cpu = cpumask_first(&plr->d->cpu_mask);
1179 if (!cpu_online(cpu)) {
1180 ret = -ENODEV;
1181 goto out;
1182 }
1183
1184 plr->cpu = cpu;
1185
1186 if (sel == 1)
1187 thread = kthread_create_on_node(measure_cycles_lat_fn, plr,
1188 cpu_to_node(cpu),
1189 "pseudo_lock_measure/%u",
1190 cpu);
1191 else if (sel == 2)
1192 thread = kthread_create_on_node(measure_l2_residency, plr,
1193 cpu_to_node(cpu),
1194 "pseudo_lock_measure/%u",
1195 cpu);
1196 else if (sel == 3)
1197 thread = kthread_create_on_node(measure_l3_residency, plr,
1198 cpu_to_node(cpu),
1199 "pseudo_lock_measure/%u",
1200 cpu);
1201 else
1202 goto out;
1203
1204 if (IS_ERR(thread)) {
1205 ret = PTR_ERR(thread);
1206 goto out;
1207 }
1208 kthread_bind(thread, cpu);
1209 wake_up_process(thread);
1210
1211 ret = wait_event_interruptible(plr->lock_thread_wq,
1212 plr->thread_done == 1);
1213 if (ret < 0)
1214 goto out;
1215
1216 ret = 0;
1217
1218 out:
1219 mutex_unlock(&rdtgroup_mutex);
1220 cpus_read_unlock();
1221 return ret;
1222 }
1223
pseudo_lock_measure_trigger(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1224 static ssize_t pseudo_lock_measure_trigger(struct file *file,
1225 const char __user *user_buf,
1226 size_t count, loff_t *ppos)
1227 {
1228 struct rdtgroup *rdtgrp = file->private_data;
1229 size_t buf_size;
1230 char buf[32];
1231 int ret;
1232 int sel;
1233
1234 buf_size = min(count, (sizeof(buf) - 1));
1235 if (copy_from_user(buf, user_buf, buf_size))
1236 return -EFAULT;
1237
1238 buf[buf_size] = '\0';
1239 ret = kstrtoint(buf, 10, &sel);
1240 if (ret == 0) {
1241 if (sel != 1 && sel != 2 && sel != 3)
1242 return -EINVAL;
1243 ret = debugfs_file_get(file->f_path.dentry);
1244 if (ret)
1245 return ret;
1246 ret = pseudo_lock_measure_cycles(rdtgrp, sel);
1247 if (ret == 0)
1248 ret = count;
1249 debugfs_file_put(file->f_path.dentry);
1250 }
1251
1252 return ret;
1253 }
1254
1255 static const struct file_operations pseudo_measure_fops = {
1256 .write = pseudo_lock_measure_trigger,
1257 .open = simple_open,
1258 .llseek = default_llseek,
1259 };
1260
1261 /**
1262 * rdtgroup_pseudo_lock_create - Create a pseudo-locked region
1263 * @rdtgrp: resource group to which pseudo-lock region belongs
1264 *
1265 * Called when a resource group in the pseudo-locksetup mode receives a
1266 * valid schemata that should be pseudo-locked. Since the resource group is
1267 * in pseudo-locksetup mode the &struct pseudo_lock_region has already been
1268 * allocated and initialized with the essential information. If a failure
1269 * occurs the resource group remains in the pseudo-locksetup mode with the
1270 * &struct pseudo_lock_region associated with it, but cleared from all
1271 * information and ready for the user to re-attempt pseudo-locking by
1272 * writing the schemata again.
1273 *
1274 * Return: 0 if the pseudo-locked region was successfully pseudo-locked, <0
1275 * on failure. Descriptive error will be written to last_cmd_status buffer.
1276 */
rdtgroup_pseudo_lock_create(struct rdtgroup * rdtgrp)1277 int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
1278 {
1279 struct pseudo_lock_region *plr = rdtgrp->plr;
1280 struct task_struct *thread;
1281 unsigned int new_minor;
1282 struct device *dev;
1283 int ret;
1284
1285 ret = pseudo_lock_region_alloc(plr);
1286 if (ret < 0)
1287 return ret;
1288
1289 ret = pseudo_lock_cstates_constrain(plr);
1290 if (ret < 0) {
1291 ret = -EINVAL;
1292 goto out_region;
1293 }
1294
1295 plr->thread_done = 0;
1296
1297 thread = kthread_create_on_node(pseudo_lock_fn, rdtgrp,
1298 cpu_to_node(plr->cpu),
1299 "pseudo_lock/%u", plr->cpu);
1300 if (IS_ERR(thread)) {
1301 ret = PTR_ERR(thread);
1302 rdt_last_cmd_printf("Locking thread returned error %d\n", ret);
1303 goto out_cstates;
1304 }
1305
1306 kthread_bind(thread, plr->cpu);
1307 wake_up_process(thread);
1308
1309 ret = wait_event_interruptible(plr->lock_thread_wq,
1310 plr->thread_done == 1);
1311 if (ret < 0) {
1312 /*
1313 * If the thread does not get on the CPU for whatever
1314 * reason and the process which sets up the region is
1315 * interrupted then this will leave the thread in runnable
1316 * state and once it gets on the CPU it will derefence
1317 * the cleared, but not freed, plr struct resulting in an
1318 * empty pseudo-locking loop.
1319 */
1320 rdt_last_cmd_puts("Locking thread interrupted\n");
1321 goto out_cstates;
1322 }
1323
1324 ret = pseudo_lock_minor_get(&new_minor);
1325 if (ret < 0) {
1326 rdt_last_cmd_puts("Unable to obtain a new minor number\n");
1327 goto out_cstates;
1328 }
1329
1330 /*
1331 * Unlock access but do not release the reference. The
1332 * pseudo-locked region will still be here on return.
1333 *
1334 * The mutex has to be released temporarily to avoid a potential
1335 * deadlock with the mm->mmap_sem semaphore which is obtained in
1336 * the device_create() and debugfs_create_dir() callpath below
1337 * as well as before the mmap() callback is called.
1338 */
1339 mutex_unlock(&rdtgroup_mutex);
1340
1341 if (!IS_ERR_OR_NULL(debugfs_resctrl)) {
1342 plr->debugfs_dir = debugfs_create_dir(rdtgrp->kn->name,
1343 debugfs_resctrl);
1344 if (!IS_ERR_OR_NULL(plr->debugfs_dir))
1345 debugfs_create_file("pseudo_lock_measure", 0200,
1346 plr->debugfs_dir, rdtgrp,
1347 &pseudo_measure_fops);
1348 }
1349
1350 dev = device_create(pseudo_lock_class, NULL,
1351 MKDEV(pseudo_lock_major, new_minor),
1352 rdtgrp, "%s", rdtgrp->kn->name);
1353
1354 mutex_lock(&rdtgroup_mutex);
1355
1356 if (IS_ERR(dev)) {
1357 ret = PTR_ERR(dev);
1358 rdt_last_cmd_printf("Failed to create character device: %d\n",
1359 ret);
1360 goto out_debugfs;
1361 }
1362
1363 /* We released the mutex - check if group was removed while we did so */
1364 if (rdtgrp->flags & RDT_DELETED) {
1365 ret = -ENODEV;
1366 goto out_device;
1367 }
1368
1369 plr->minor = new_minor;
1370
1371 rdtgrp->mode = RDT_MODE_PSEUDO_LOCKED;
1372 closid_free(rdtgrp->closid);
1373 rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0444);
1374 rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0444);
1375
1376 ret = 0;
1377 goto out;
1378
1379 out_device:
1380 device_destroy(pseudo_lock_class, MKDEV(pseudo_lock_major, new_minor));
1381 out_debugfs:
1382 debugfs_remove_recursive(plr->debugfs_dir);
1383 pseudo_lock_minor_release(new_minor);
1384 out_cstates:
1385 pseudo_lock_cstates_relax(plr);
1386 out_region:
1387 pseudo_lock_region_clear(plr);
1388 out:
1389 return ret;
1390 }
1391
1392 /**
1393 * rdtgroup_pseudo_lock_remove - Remove a pseudo-locked region
1394 * @rdtgrp: resource group to which the pseudo-locked region belongs
1395 *
1396 * The removal of a pseudo-locked region can be initiated when the resource
1397 * group is removed from user space via a "rmdir" from userspace or the
1398 * unmount of the resctrl filesystem. On removal the resource group does
1399 * not go back to pseudo-locksetup mode before it is removed, instead it is
1400 * removed directly. There is thus assymmetry with the creation where the
1401 * &struct pseudo_lock_region is removed here while it was not created in
1402 * rdtgroup_pseudo_lock_create().
1403 *
1404 * Return: void
1405 */
rdtgroup_pseudo_lock_remove(struct rdtgroup * rdtgrp)1406 void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp)
1407 {
1408 struct pseudo_lock_region *plr = rdtgrp->plr;
1409
1410 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
1411 /*
1412 * Default group cannot be a pseudo-locked region so we can
1413 * free closid here.
1414 */
1415 closid_free(rdtgrp->closid);
1416 goto free;
1417 }
1418
1419 pseudo_lock_cstates_relax(plr);
1420 debugfs_remove_recursive(rdtgrp->plr->debugfs_dir);
1421 device_destroy(pseudo_lock_class, MKDEV(pseudo_lock_major, plr->minor));
1422 pseudo_lock_minor_release(plr->minor);
1423
1424 free:
1425 pseudo_lock_free(rdtgrp);
1426 }
1427
pseudo_lock_dev_open(struct inode * inode,struct file * filp)1428 static int pseudo_lock_dev_open(struct inode *inode, struct file *filp)
1429 {
1430 struct rdtgroup *rdtgrp;
1431
1432 mutex_lock(&rdtgroup_mutex);
1433
1434 rdtgrp = region_find_by_minor(iminor(inode));
1435 if (!rdtgrp) {
1436 mutex_unlock(&rdtgroup_mutex);
1437 return -ENODEV;
1438 }
1439
1440 filp->private_data = rdtgrp;
1441 atomic_inc(&rdtgrp->waitcount);
1442 /* Perform a non-seekable open - llseek is not supported */
1443 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1444
1445 mutex_unlock(&rdtgroup_mutex);
1446
1447 return 0;
1448 }
1449
pseudo_lock_dev_release(struct inode * inode,struct file * filp)1450 static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
1451 {
1452 struct rdtgroup *rdtgrp;
1453
1454 mutex_lock(&rdtgroup_mutex);
1455 rdtgrp = filp->private_data;
1456 WARN_ON(!rdtgrp);
1457 if (!rdtgrp) {
1458 mutex_unlock(&rdtgroup_mutex);
1459 return -ENODEV;
1460 }
1461 filp->private_data = NULL;
1462 atomic_dec(&rdtgrp->waitcount);
1463 mutex_unlock(&rdtgroup_mutex);
1464 return 0;
1465 }
1466
pseudo_lock_dev_mremap(struct vm_area_struct * area)1467 static int pseudo_lock_dev_mremap(struct vm_area_struct *area)
1468 {
1469 /* Not supported */
1470 return -EINVAL;
1471 }
1472
1473 static const struct vm_operations_struct pseudo_mmap_ops = {
1474 .mremap = pseudo_lock_dev_mremap,
1475 };
1476
pseudo_lock_dev_mmap(struct file * filp,struct vm_area_struct * vma)1477 static int pseudo_lock_dev_mmap(struct file *filp, struct vm_area_struct *vma)
1478 {
1479 unsigned long vsize = vma->vm_end - vma->vm_start;
1480 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1481 struct pseudo_lock_region *plr;
1482 struct rdtgroup *rdtgrp;
1483 unsigned long physical;
1484 unsigned long psize;
1485
1486 mutex_lock(&rdtgroup_mutex);
1487
1488 rdtgrp = filp->private_data;
1489 WARN_ON(!rdtgrp);
1490 if (!rdtgrp) {
1491 mutex_unlock(&rdtgroup_mutex);
1492 return -ENODEV;
1493 }
1494
1495 plr = rdtgrp->plr;
1496
1497 if (!plr->d) {
1498 mutex_unlock(&rdtgroup_mutex);
1499 return -ENODEV;
1500 }
1501
1502 /*
1503 * Task is required to run with affinity to the cpus associated
1504 * with the pseudo-locked region. If this is not the case the task
1505 * may be scheduled elsewhere and invalidate entries in the
1506 * pseudo-locked region.
1507 */
1508 if (!cpumask_subset(current->cpus_ptr, &plr->d->cpu_mask)) {
1509 mutex_unlock(&rdtgroup_mutex);
1510 return -EINVAL;
1511 }
1512
1513 physical = __pa(plr->kmem) >> PAGE_SHIFT;
1514 psize = plr->size - off;
1515
1516 if (off > plr->size) {
1517 mutex_unlock(&rdtgroup_mutex);
1518 return -ENOSPC;
1519 }
1520
1521 /*
1522 * Ensure changes are carried directly to the memory being mapped,
1523 * do not allow copy-on-write mapping.
1524 */
1525 if (!(vma->vm_flags & VM_SHARED)) {
1526 mutex_unlock(&rdtgroup_mutex);
1527 return -EINVAL;
1528 }
1529
1530 if (vsize > psize) {
1531 mutex_unlock(&rdtgroup_mutex);
1532 return -ENOSPC;
1533 }
1534
1535 memset(plr->kmem + off, 0, vsize);
1536
1537 if (remap_pfn_range(vma, vma->vm_start, physical + vma->vm_pgoff,
1538 vsize, vma->vm_page_prot)) {
1539 mutex_unlock(&rdtgroup_mutex);
1540 return -EAGAIN;
1541 }
1542 vma->vm_ops = &pseudo_mmap_ops;
1543 mutex_unlock(&rdtgroup_mutex);
1544 return 0;
1545 }
1546
1547 static const struct file_operations pseudo_lock_dev_fops = {
1548 .owner = THIS_MODULE,
1549 .llseek = no_llseek,
1550 .read = NULL,
1551 .write = NULL,
1552 .open = pseudo_lock_dev_open,
1553 .release = pseudo_lock_dev_release,
1554 .mmap = pseudo_lock_dev_mmap,
1555 };
1556
pseudo_lock_devnode(struct device * dev,umode_t * mode)1557 static char *pseudo_lock_devnode(struct device *dev, umode_t *mode)
1558 {
1559 struct rdtgroup *rdtgrp;
1560
1561 rdtgrp = dev_get_drvdata(dev);
1562 if (mode)
1563 *mode = 0600;
1564 return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdtgrp->kn->name);
1565 }
1566
rdt_pseudo_lock_init(void)1567 int rdt_pseudo_lock_init(void)
1568 {
1569 int ret;
1570
1571 ret = register_chrdev(0, "pseudo_lock", &pseudo_lock_dev_fops);
1572 if (ret < 0)
1573 return ret;
1574
1575 pseudo_lock_major = ret;
1576
1577 pseudo_lock_class = class_create(THIS_MODULE, "pseudo_lock");
1578 if (IS_ERR(pseudo_lock_class)) {
1579 ret = PTR_ERR(pseudo_lock_class);
1580 unregister_chrdev(pseudo_lock_major, "pseudo_lock");
1581 return ret;
1582 }
1583
1584 pseudo_lock_class->devnode = pseudo_lock_devnode;
1585 return 0;
1586 }
1587
rdt_pseudo_lock_release(void)1588 void rdt_pseudo_lock_release(void)
1589 {
1590 class_destroy(pseudo_lock_class);
1591 pseudo_lock_class = NULL;
1592 unregister_chrdev(pseudo_lock_major, "pseudo_lock");
1593 pseudo_lock_major = 0;
1594 }
1595