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