Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Resource Director Technology (RDT)
0004  *
0005  * Pseudo-locking support built on top of Cache Allocation Technology (CAT)
0006  *
0007  * Copyright (C) 2018 Intel Corporation
0008  *
0009  * Author: Reinette Chatre <reinette.chatre@intel.com>
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/cacheinfo.h>
0015 #include <linux/cpu.h>
0016 #include <linux/cpumask.h>
0017 #include <linux/debugfs.h>
0018 #include <linux/kthread.h>
0019 #include <linux/mman.h>
0020 #include <linux/perf_event.h>
0021 #include <linux/pm_qos.h>
0022 #include <linux/slab.h>
0023 #include <linux/uaccess.h>
0024 
0025 #include <asm/cacheflush.h>
0026 #include <asm/intel-family.h>
0027 #include <asm/resctrl.h>
0028 #include <asm/perf_event.h>
0029 
0030 #include "../../events/perf_event.h" /* For X86_CONFIG() */
0031 #include "internal.h"
0032 
0033 #define CREATE_TRACE_POINTS
0034 #include "pseudo_lock_event.h"
0035 
0036 /*
0037  * The bits needed to disable hardware prefetching varies based on the
0038  * platform. During initialization we will discover which bits to use.
0039  */
0040 static u64 prefetch_disable_bits;
0041 
0042 /*
0043  * Major number assigned to and shared by all devices exposing
0044  * pseudo-locked regions.
0045  */
0046 static unsigned int pseudo_lock_major;
0047 static unsigned long pseudo_lock_minor_avail = GENMASK(MINORBITS, 0);
0048 static struct class *pseudo_lock_class;
0049 
0050 /**
0051  * get_prefetch_disable_bits - prefetch disable bits of supported platforms
0052  * @void: It takes no parameters.
0053  *
0054  * Capture the list of platforms that have been validated to support
0055  * pseudo-locking. This includes testing to ensure pseudo-locked regions
0056  * with low cache miss rates can be created under variety of load conditions
0057  * as well as that these pseudo-locked regions can maintain their low cache
0058  * miss rates under variety of load conditions for significant lengths of time.
0059  *
0060  * After a platform has been validated to support pseudo-locking its
0061  * hardware prefetch disable bits are included here as they are documented
0062  * in the SDM.
0063  *
0064  * When adding a platform here also add support for its cache events to
0065  * measure_cycles_perf_fn()
0066  *
0067  * Return:
0068  * If platform is supported, the bits to disable hardware prefetchers, 0
0069  * if platform is not supported.
0070  */
0071 static u64 get_prefetch_disable_bits(void)
0072 {
0073     if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
0074         boot_cpu_data.x86 != 6)
0075         return 0;
0076 
0077     switch (boot_cpu_data.x86_model) {
0078     case INTEL_FAM6_BROADWELL_X:
0079         /*
0080          * SDM defines bits of MSR_MISC_FEATURE_CONTROL register
0081          * as:
0082          * 0    L2 Hardware Prefetcher Disable (R/W)
0083          * 1    L2 Adjacent Cache Line Prefetcher Disable (R/W)
0084          * 2    DCU Hardware Prefetcher Disable (R/W)
0085          * 3    DCU IP Prefetcher Disable (R/W)
0086          * 63:4 Reserved
0087          */
0088         return 0xF;
0089     case INTEL_FAM6_ATOM_GOLDMONT:
0090     case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
0091         /*
0092          * SDM defines bits of MSR_MISC_FEATURE_CONTROL register
0093          * as:
0094          * 0     L2 Hardware Prefetcher Disable (R/W)
0095          * 1     Reserved
0096          * 2     DCU Hardware Prefetcher Disable (R/W)
0097          * 63:3  Reserved
0098          */
0099         return 0x5;
0100     }
0101 
0102     return 0;
0103 }
0104 
0105 /**
0106  * pseudo_lock_minor_get - Obtain available minor number
0107  * @minor: Pointer to where new minor number will be stored
0108  *
0109  * A bitmask is used to track available minor numbers. Here the next free
0110  * minor number is marked as unavailable and returned.
0111  *
0112  * Return: 0 on success, <0 on failure.
0113  */
0114 static int pseudo_lock_minor_get(unsigned int *minor)
0115 {
0116     unsigned long first_bit;
0117 
0118     first_bit = find_first_bit(&pseudo_lock_minor_avail, MINORBITS);
0119 
0120     if (first_bit == MINORBITS)
0121         return -ENOSPC;
0122 
0123     __clear_bit(first_bit, &pseudo_lock_minor_avail);
0124     *minor = first_bit;
0125 
0126     return 0;
0127 }
0128 
0129 /**
0130  * pseudo_lock_minor_release - Return minor number to available
0131  * @minor: The minor number made available
0132  */
0133 static void pseudo_lock_minor_release(unsigned int minor)
0134 {
0135     __set_bit(minor, &pseudo_lock_minor_avail);
0136 }
0137 
0138 /**
0139  * region_find_by_minor - Locate a pseudo-lock region by inode minor number
0140  * @minor: The minor number of the device representing pseudo-locked region
0141  *
0142  * When the character device is accessed we need to determine which
0143  * pseudo-locked region it belongs to. This is done by matching the minor
0144  * number of the device to the pseudo-locked region it belongs.
0145  *
0146  * Minor numbers are assigned at the time a pseudo-locked region is associated
0147  * with a cache instance.
0148  *
0149  * Return: On success return pointer to resource group owning the pseudo-locked
0150  *         region, NULL on failure.
0151  */
0152 static struct rdtgroup *region_find_by_minor(unsigned int minor)
0153 {
0154     struct rdtgroup *rdtgrp, *rdtgrp_match = NULL;
0155 
0156     list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
0157         if (rdtgrp->plr && rdtgrp->plr->minor == minor) {
0158             rdtgrp_match = rdtgrp;
0159             break;
0160         }
0161     }
0162     return rdtgrp_match;
0163 }
0164 
0165 /**
0166  * struct pseudo_lock_pm_req - A power management QoS request list entry
0167  * @list:   Entry within the @pm_reqs list for a pseudo-locked region
0168  * @req:    PM QoS request
0169  */
0170 struct pseudo_lock_pm_req {
0171     struct list_head list;
0172     struct dev_pm_qos_request req;
0173 };
0174 
0175 static void pseudo_lock_cstates_relax(struct pseudo_lock_region *plr)
0176 {
0177     struct pseudo_lock_pm_req *pm_req, *next;
0178 
0179     list_for_each_entry_safe(pm_req, next, &plr->pm_reqs, list) {
0180         dev_pm_qos_remove_request(&pm_req->req);
0181         list_del(&pm_req->list);
0182         kfree(pm_req);
0183     }
0184 }
0185 
0186 /**
0187  * pseudo_lock_cstates_constrain - Restrict cores from entering C6
0188  * @plr: Pseudo-locked region
0189  *
0190  * To prevent the cache from being affected by power management entering
0191  * C6 has to be avoided. This is accomplished by requesting a latency
0192  * requirement lower than lowest C6 exit latency of all supported
0193  * platforms as found in the cpuidle state tables in the intel_idle driver.
0194  * At this time it is possible to do so with a single latency requirement
0195  * for all supported platforms.
0196  *
0197  * Since Goldmont is supported, which is affected by X86_BUG_MONITOR,
0198  * the ACPI latencies need to be considered while keeping in mind that C2
0199  * may be set to map to deeper sleep states. In this case the latency
0200  * requirement needs to prevent entering C2 also.
0201  *
0202  * Return: 0 on success, <0 on failure
0203  */
0204 static int pseudo_lock_cstates_constrain(struct pseudo_lock_region *plr)
0205 {
0206     struct pseudo_lock_pm_req *pm_req;
0207     int cpu;
0208     int ret;
0209 
0210     for_each_cpu(cpu, &plr->d->cpu_mask) {
0211         pm_req = kzalloc(sizeof(*pm_req), GFP_KERNEL);
0212         if (!pm_req) {
0213             rdt_last_cmd_puts("Failure to allocate memory for PM QoS\n");
0214             ret = -ENOMEM;
0215             goto out_err;
0216         }
0217         ret = dev_pm_qos_add_request(get_cpu_device(cpu),
0218                          &pm_req->req,
0219                          DEV_PM_QOS_RESUME_LATENCY,
0220                          30);
0221         if (ret < 0) {
0222             rdt_last_cmd_printf("Failed to add latency req CPU%d\n",
0223                         cpu);
0224             kfree(pm_req);
0225             ret = -1;
0226             goto out_err;
0227         }
0228         list_add(&pm_req->list, &plr->pm_reqs);
0229     }
0230 
0231     return 0;
0232 
0233 out_err:
0234     pseudo_lock_cstates_relax(plr);
0235     return ret;
0236 }
0237 
0238 /**
0239  * pseudo_lock_region_clear - Reset pseudo-lock region data
0240  * @plr: pseudo-lock region
0241  *
0242  * All content of the pseudo-locked region is reset - any memory allocated
0243  * freed.
0244  *
0245  * Return: void
0246  */
0247 static void pseudo_lock_region_clear(struct pseudo_lock_region *plr)
0248 {
0249     plr->size = 0;
0250     plr->line_size = 0;
0251     kfree(plr->kmem);
0252     plr->kmem = NULL;
0253     plr->s = NULL;
0254     if (plr->d)
0255         plr->d->plr = NULL;
0256     plr->d = NULL;
0257     plr->cbm = 0;
0258     plr->debugfs_dir = NULL;
0259 }
0260 
0261 /**
0262  * pseudo_lock_region_init - Initialize pseudo-lock region information
0263  * @plr: pseudo-lock region
0264  *
0265  * Called after user provided a schemata to be pseudo-locked. From the
0266  * schemata the &struct pseudo_lock_region is on entry already initialized
0267  * with the resource, domain, and capacity bitmask. Here the information
0268  * required for pseudo-locking is deduced from this data and &struct
0269  * pseudo_lock_region initialized further. This information includes:
0270  * - size in bytes of the region to be pseudo-locked
0271  * - cache line size to know the stride with which data needs to be accessed
0272  *   to be pseudo-locked
0273  * - a cpu associated with the cache instance on which the pseudo-locking
0274  *   flow can be executed
0275  *
0276  * Return: 0 on success, <0 on failure. Descriptive error will be written
0277  * to last_cmd_status buffer.
0278  */
0279 static int pseudo_lock_region_init(struct pseudo_lock_region *plr)
0280 {
0281     struct cpu_cacheinfo *ci;
0282     int ret;
0283     int i;
0284 
0285     /* Pick the first cpu we find that is associated with the cache. */
0286     plr->cpu = cpumask_first(&plr->d->cpu_mask);
0287 
0288     if (!cpu_online(plr->cpu)) {
0289         rdt_last_cmd_printf("CPU %u associated with cache not online\n",
0290                     plr->cpu);
0291         ret = -ENODEV;
0292         goto out_region;
0293     }
0294 
0295     ci = get_cpu_cacheinfo(plr->cpu);
0296 
0297     plr->size = rdtgroup_cbm_to_size(plr->s->res, plr->d, plr->cbm);
0298 
0299     for (i = 0; i < ci->num_leaves; i++) {
0300         if (ci->info_list[i].level == plr->s->res->cache_level) {
0301             plr->line_size = ci->info_list[i].coherency_line_size;
0302             return 0;
0303         }
0304     }
0305 
0306     ret = -1;
0307     rdt_last_cmd_puts("Unable to determine cache line size\n");
0308 out_region:
0309     pseudo_lock_region_clear(plr);
0310     return ret;
0311 }
0312 
0313 /**
0314  * pseudo_lock_init - Initialize a pseudo-lock region
0315  * @rdtgrp: resource group to which new pseudo-locked region will belong
0316  *
0317  * A pseudo-locked region is associated with a resource group. When this
0318  * association is created the pseudo-locked region is initialized. The
0319  * details of the pseudo-locked region are not known at this time so only
0320  * allocation is done and association established.
0321  *
0322  * Return: 0 on success, <0 on failure
0323  */
0324 static int pseudo_lock_init(struct rdtgroup *rdtgrp)
0325 {
0326     struct pseudo_lock_region *plr;
0327 
0328     plr = kzalloc(sizeof(*plr), GFP_KERNEL);
0329     if (!plr)
0330         return -ENOMEM;
0331 
0332     init_waitqueue_head(&plr->lock_thread_wq);
0333     INIT_LIST_HEAD(&plr->pm_reqs);
0334     rdtgrp->plr = plr;
0335     return 0;
0336 }
0337 
0338 /**
0339  * pseudo_lock_region_alloc - Allocate kernel memory that will be pseudo-locked
0340  * @plr: pseudo-lock region
0341  *
0342  * Initialize the details required to set up the pseudo-locked region and
0343  * allocate the contiguous memory that will be pseudo-locked to the cache.
0344  *
0345  * Return: 0 on success, <0 on failure.  Descriptive error will be written
0346  * to last_cmd_status buffer.
0347  */
0348 static int pseudo_lock_region_alloc(struct pseudo_lock_region *plr)
0349 {
0350     int ret;
0351 
0352     ret = pseudo_lock_region_init(plr);
0353     if (ret < 0)
0354         return ret;
0355 
0356     /*
0357      * We do not yet support contiguous regions larger than
0358      * KMALLOC_MAX_SIZE.
0359      */
0360     if (plr->size > KMALLOC_MAX_SIZE) {
0361         rdt_last_cmd_puts("Requested region exceeds maximum size\n");
0362         ret = -E2BIG;
0363         goto out_region;
0364     }
0365 
0366     plr->kmem = kzalloc(plr->size, GFP_KERNEL);
0367     if (!plr->kmem) {
0368         rdt_last_cmd_puts("Unable to allocate memory\n");
0369         ret = -ENOMEM;
0370         goto out_region;
0371     }
0372 
0373     ret = 0;
0374     goto out;
0375 out_region:
0376     pseudo_lock_region_clear(plr);
0377 out:
0378     return ret;
0379 }
0380 
0381 /**
0382  * pseudo_lock_free - Free a pseudo-locked region
0383  * @rdtgrp: resource group to which pseudo-locked region belonged
0384  *
0385  * The pseudo-locked region's resources have already been released, or not
0386  * yet created at this point. Now it can be freed and disassociated from the
0387  * resource group.
0388  *
0389  * Return: void
0390  */
0391 static void pseudo_lock_free(struct rdtgroup *rdtgrp)
0392 {
0393     pseudo_lock_region_clear(rdtgrp->plr);
0394     kfree(rdtgrp->plr);
0395     rdtgrp->plr = NULL;
0396 }
0397 
0398 /**
0399  * pseudo_lock_fn - Load kernel memory into cache
0400  * @_rdtgrp: resource group to which pseudo-lock region belongs
0401  *
0402  * This is the core pseudo-locking flow.
0403  *
0404  * First we ensure that the kernel memory cannot be found in the cache.
0405  * Then, while taking care that there will be as little interference as
0406  * possible, the memory to be loaded is accessed while core is running
0407  * with class of service set to the bitmask of the pseudo-locked region.
0408  * After this is complete no future CAT allocations will be allowed to
0409  * overlap with this bitmask.
0410  *
0411  * Local register variables are utilized to ensure that the memory region
0412  * to be locked is the only memory access made during the critical locking
0413  * loop.
0414  *
0415  * Return: 0. Waiter on waitqueue will be woken on completion.
0416  */
0417 static int pseudo_lock_fn(void *_rdtgrp)
0418 {
0419     struct rdtgroup *rdtgrp = _rdtgrp;
0420     struct pseudo_lock_region *plr = rdtgrp->plr;
0421     u32 rmid_p, closid_p;
0422     unsigned long i;
0423 #ifdef CONFIG_KASAN
0424     /*
0425      * The registers used for local register variables are also used
0426      * when KASAN is active. When KASAN is active we use a regular
0427      * variable to ensure we always use a valid pointer, but the cost
0428      * is that this variable will enter the cache through evicting the
0429      * memory we are trying to lock into the cache. Thus expect lower
0430      * pseudo-locking success rate when KASAN is active.
0431      */
0432     unsigned int line_size;
0433     unsigned int size;
0434     void *mem_r;
0435 #else
0436     register unsigned int line_size asm("esi");
0437     register unsigned int size asm("edi");
0438     register void *mem_r asm(_ASM_BX);
0439 #endif /* CONFIG_KASAN */
0440 
0441     /*
0442      * Make sure none of the allocated memory is cached. If it is we
0443      * will get a cache hit in below loop from outside of pseudo-locked
0444      * region.
0445      * wbinvd (as opposed to clflush/clflushopt) is required to
0446      * increase likelihood that allocated cache portion will be filled
0447      * with associated memory.
0448      */
0449     native_wbinvd();
0450 
0451     /*
0452      * Always called with interrupts enabled. By disabling interrupts
0453      * ensure that we will not be preempted during this critical section.
0454      */
0455     local_irq_disable();
0456 
0457     /*
0458      * Call wrmsr and rdmsr as directly as possible to avoid tracing
0459      * clobbering local register variables or affecting cache accesses.
0460      *
0461      * Disable the hardware prefetcher so that when the end of the memory
0462      * being pseudo-locked is reached the hardware will not read beyond
0463      * the buffer and evict pseudo-locked memory read earlier from the
0464      * cache.
0465      */
0466     __wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
0467     closid_p = this_cpu_read(pqr_state.cur_closid);
0468     rmid_p = this_cpu_read(pqr_state.cur_rmid);
0469     mem_r = plr->kmem;
0470     size = plr->size;
0471     line_size = plr->line_size;
0472     /*
0473      * Critical section begin: start by writing the closid associated
0474      * with the capacity bitmask of the cache region being
0475      * pseudo-locked followed by reading of kernel memory to load it
0476      * into the cache.
0477      */
0478     __wrmsr(IA32_PQR_ASSOC, rmid_p, rdtgrp->closid);
0479     /*
0480      * Cache was flushed earlier. Now access kernel memory to read it
0481      * into cache region associated with just activated plr->closid.
0482      * Loop over data twice:
0483      * - In first loop the cache region is shared with the page walker
0484      *   as it populates the paging structure caches (including TLB).
0485      * - In the second loop the paging structure caches are used and
0486      *   cache region is populated with the memory being referenced.
0487      */
0488     for (i = 0; i < size; i += PAGE_SIZE) {
0489         /*
0490          * Add a barrier to prevent speculative execution of this
0491          * loop reading beyond the end of the buffer.
0492          */
0493         rmb();
0494         asm volatile("mov (%0,%1,1), %%eax\n\t"
0495             :
0496             : "r" (mem_r), "r" (i)
0497             : "%eax", "memory");
0498     }
0499     for (i = 0; i < size; i += line_size) {
0500         /*
0501          * Add a barrier to prevent speculative execution of this
0502          * loop reading beyond the end of the buffer.
0503          */
0504         rmb();
0505         asm volatile("mov (%0,%1,1), %%eax\n\t"
0506             :
0507             : "r" (mem_r), "r" (i)
0508             : "%eax", "memory");
0509     }
0510     /*
0511      * Critical section end: restore closid with capacity bitmask that
0512      * does not overlap with pseudo-locked region.
0513      */
0514     __wrmsr(IA32_PQR_ASSOC, rmid_p, closid_p);
0515 
0516     /* Re-enable the hardware prefetcher(s) */
0517     wrmsr(MSR_MISC_FEATURE_CONTROL, 0x0, 0x0);
0518     local_irq_enable();
0519 
0520     plr->thread_done = 1;
0521     wake_up_interruptible(&plr->lock_thread_wq);
0522     return 0;
0523 }
0524 
0525 /**
0526  * rdtgroup_monitor_in_progress - Test if monitoring in progress
0527  * @rdtgrp: resource group being queried
0528  *
0529  * Return: 1 if monitor groups have been created for this resource
0530  * group, 0 otherwise.
0531  */
0532 static int rdtgroup_monitor_in_progress(struct rdtgroup *rdtgrp)
0533 {
0534     return !list_empty(&rdtgrp->mon.crdtgrp_list);
0535 }
0536 
0537 /**
0538  * rdtgroup_locksetup_user_restrict - Restrict user access to group
0539  * @rdtgrp: resource group needing access restricted
0540  *
0541  * A resource group used for cache pseudo-locking cannot have cpus or tasks
0542  * assigned to it. This is communicated to the user by restricting access
0543  * to all the files that can be used to make such changes.
0544  *
0545  * Permissions restored with rdtgroup_locksetup_user_restore()
0546  *
0547  * Return: 0 on success, <0 on failure. If a failure occurs during the
0548  * restriction of access an attempt will be made to restore permissions but
0549  * the state of the mode of these files will be uncertain when a failure
0550  * occurs.
0551  */
0552 static int rdtgroup_locksetup_user_restrict(struct rdtgroup *rdtgrp)
0553 {
0554     int ret;
0555 
0556     ret = rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
0557     if (ret)
0558         return ret;
0559 
0560     ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
0561     if (ret)
0562         goto err_tasks;
0563 
0564     ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
0565     if (ret)
0566         goto err_cpus;
0567 
0568     if (rdt_mon_capable) {
0569         ret = rdtgroup_kn_mode_restrict(rdtgrp, "mon_groups");
0570         if (ret)
0571             goto err_cpus_list;
0572     }
0573 
0574     ret = 0;
0575     goto out;
0576 
0577 err_cpus_list:
0578     rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
0579 err_cpus:
0580     rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
0581 err_tasks:
0582     rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
0583 out:
0584     return ret;
0585 }
0586 
0587 /**
0588  * rdtgroup_locksetup_user_restore - Restore user access to group
0589  * @rdtgrp: resource group needing access restored
0590  *
0591  * Restore all file access previously removed using
0592  * rdtgroup_locksetup_user_restrict()
0593  *
0594  * Return: 0 on success, <0 on failure.  If a failure occurs during the
0595  * restoration of access an attempt will be made to restrict permissions
0596  * again but the state of the mode of these files will be uncertain when
0597  * a failure occurs.
0598  */
0599 static int rdtgroup_locksetup_user_restore(struct rdtgroup *rdtgrp)
0600 {
0601     int ret;
0602 
0603     ret = rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);
0604     if (ret)
0605         return ret;
0606 
0607     ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);
0608     if (ret)
0609         goto err_tasks;
0610 
0611     ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);
0612     if (ret)
0613         goto err_cpus;
0614 
0615     if (rdt_mon_capable) {
0616         ret = rdtgroup_kn_mode_restore(rdtgrp, "mon_groups", 0777);
0617         if (ret)
0618             goto err_cpus_list;
0619     }
0620 
0621     ret = 0;
0622     goto out;
0623 
0624 err_cpus_list:
0625     rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");
0626 err_cpus:
0627     rdtgroup_kn_mode_restrict(rdtgrp, "cpus");
0628 err_tasks:
0629     rdtgroup_kn_mode_restrict(rdtgrp, "tasks");
0630 out:
0631     return ret;
0632 }
0633 
0634 /**
0635  * rdtgroup_locksetup_enter - Resource group enters locksetup mode
0636  * @rdtgrp: resource group requested to enter locksetup mode
0637  *
0638  * A resource group enters locksetup mode to reflect that it would be used
0639  * to represent a pseudo-locked region and is in the process of being set
0640  * up to do so. A resource group used for a pseudo-locked region would
0641  * lose the closid associated with it so we cannot allow it to have any
0642  * tasks or cpus assigned nor permit tasks or cpus to be assigned in the
0643  * future. Monitoring of a pseudo-locked region is not allowed either.
0644  *
0645  * The above and more restrictions on a pseudo-locked region are checked
0646  * for and enforced before the resource group enters the locksetup mode.
0647  *
0648  * Returns: 0 if the resource group successfully entered locksetup mode, <0
0649  * on failure. On failure the last_cmd_status buffer is updated with text to
0650  * communicate details of failure to the user.
0651  */
0652 int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
0653 {
0654     int ret;
0655 
0656     /*
0657      * The default resource group can neither be removed nor lose the
0658      * default closid associated with it.
0659      */
0660     if (rdtgrp == &rdtgroup_default) {
0661         rdt_last_cmd_puts("Cannot pseudo-lock default group\n");
0662         return -EINVAL;
0663     }
0664 
0665     /*
0666      * Cache Pseudo-locking not supported when CDP is enabled.
0667      *
0668      * Some things to consider if you would like to enable this
0669      * support (using L3 CDP as example):
0670      * - When CDP is enabled two separate resources are exposed,
0671      *   L3DATA and L3CODE, but they are actually on the same cache.
0672      *   The implication for pseudo-locking is that if a
0673      *   pseudo-locked region is created on a domain of one
0674      *   resource (eg. L3CODE), then a pseudo-locked region cannot
0675      *   be created on that same domain of the other resource
0676      *   (eg. L3DATA). This is because the creation of a
0677      *   pseudo-locked region involves a call to wbinvd that will
0678      *   affect all cache allocations on particular domain.
0679      * - Considering the previous, it may be possible to only
0680      *   expose one of the CDP resources to pseudo-locking and
0681      *   hide the other. For example, we could consider to only
0682      *   expose L3DATA and since the L3 cache is unified it is
0683      *   still possible to place instructions there are execute it.
0684      * - If only one region is exposed to pseudo-locking we should
0685      *   still keep in mind that availability of a portion of cache
0686      *   for pseudo-locking should take into account both resources.
0687      *   Similarly, if a pseudo-locked region is created in one
0688      *   resource, the portion of cache used by it should be made
0689      *   unavailable to all future allocations from both resources.
0690      */
0691     if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L3) ||
0692         resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2)) {
0693         rdt_last_cmd_puts("CDP enabled\n");
0694         return -EINVAL;
0695     }
0696 
0697     /*
0698      * Not knowing the bits to disable prefetching implies that this
0699      * platform does not support Cache Pseudo-Locking.
0700      */
0701     prefetch_disable_bits = get_prefetch_disable_bits();
0702     if (prefetch_disable_bits == 0) {
0703         rdt_last_cmd_puts("Pseudo-locking not supported\n");
0704         return -EINVAL;
0705     }
0706 
0707     if (rdtgroup_monitor_in_progress(rdtgrp)) {
0708         rdt_last_cmd_puts("Monitoring in progress\n");
0709         return -EINVAL;
0710     }
0711 
0712     if (rdtgroup_tasks_assigned(rdtgrp)) {
0713         rdt_last_cmd_puts("Tasks assigned to resource group\n");
0714         return -EINVAL;
0715     }
0716 
0717     if (!cpumask_empty(&rdtgrp->cpu_mask)) {
0718         rdt_last_cmd_puts("CPUs assigned to resource group\n");
0719         return -EINVAL;
0720     }
0721 
0722     if (rdtgroup_locksetup_user_restrict(rdtgrp)) {
0723         rdt_last_cmd_puts("Unable to modify resctrl permissions\n");
0724         return -EIO;
0725     }
0726 
0727     ret = pseudo_lock_init(rdtgrp);
0728     if (ret) {
0729         rdt_last_cmd_puts("Unable to init pseudo-lock region\n");
0730         goto out_release;
0731     }
0732 
0733     /*
0734      * If this system is capable of monitoring a rmid would have been
0735      * allocated when the control group was created. This is not needed
0736      * anymore when this group would be used for pseudo-locking. This
0737      * is safe to call on platforms not capable of monitoring.
0738      */
0739     free_rmid(rdtgrp->mon.rmid);
0740 
0741     ret = 0;
0742     goto out;
0743 
0744 out_release:
0745     rdtgroup_locksetup_user_restore(rdtgrp);
0746 out:
0747     return ret;
0748 }
0749 
0750 /**
0751  * rdtgroup_locksetup_exit - resource group exist locksetup mode
0752  * @rdtgrp: resource group
0753  *
0754  * When a resource group exits locksetup mode the earlier restrictions are
0755  * lifted.
0756  *
0757  * Return: 0 on success, <0 on failure
0758  */
0759 int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp)
0760 {
0761     int ret;
0762 
0763     if (rdt_mon_capable) {
0764         ret = alloc_rmid();
0765         if (ret < 0) {
0766             rdt_last_cmd_puts("Out of RMIDs\n");
0767             return ret;
0768         }
0769         rdtgrp->mon.rmid = ret;
0770     }
0771 
0772     ret = rdtgroup_locksetup_user_restore(rdtgrp);
0773     if (ret) {
0774         free_rmid(rdtgrp->mon.rmid);
0775         return ret;
0776     }
0777 
0778     pseudo_lock_free(rdtgrp);
0779     return 0;
0780 }
0781 
0782 /**
0783  * rdtgroup_cbm_overlaps_pseudo_locked - Test if CBM or portion is pseudo-locked
0784  * @d: RDT domain
0785  * @cbm: CBM to test
0786  *
0787  * @d represents a cache instance and @cbm a capacity bitmask that is
0788  * considered for it. Determine if @cbm overlaps with any existing
0789  * pseudo-locked region on @d.
0790  *
0791  * @cbm is unsigned long, even if only 32 bits are used, to make the
0792  * bitmap functions work correctly.
0793  *
0794  * Return: true if @cbm overlaps with pseudo-locked region on @d, false
0795  * otherwise.
0796  */
0797 bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm)
0798 {
0799     unsigned int cbm_len;
0800     unsigned long cbm_b;
0801 
0802     if (d->plr) {
0803         cbm_len = d->plr->s->res->cache.cbm_len;
0804         cbm_b = d->plr->cbm;
0805         if (bitmap_intersects(&cbm, &cbm_b, cbm_len))
0806             return true;
0807     }
0808     return false;
0809 }
0810 
0811 /**
0812  * rdtgroup_pseudo_locked_in_hierarchy - Pseudo-locked region in cache hierarchy
0813  * @d: RDT domain under test
0814  *
0815  * The setup of a pseudo-locked region affects all cache instances within
0816  * the hierarchy of the region. It is thus essential to know if any
0817  * pseudo-locked regions exist within a cache hierarchy to prevent any
0818  * attempts to create new pseudo-locked regions in the same hierarchy.
0819  *
0820  * Return: true if a pseudo-locked region exists in the hierarchy of @d or
0821  *         if it is not possible to test due to memory allocation issue,
0822  *         false otherwise.
0823  */
0824 bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d)
0825 {
0826     cpumask_var_t cpu_with_psl;
0827     struct rdt_resource *r;
0828     struct rdt_domain *d_i;
0829     bool ret = false;
0830 
0831     if (!zalloc_cpumask_var(&cpu_with_psl, GFP_KERNEL))
0832         return true;
0833 
0834     /*
0835      * First determine which cpus have pseudo-locked regions
0836      * associated with them.
0837      */
0838     for_each_alloc_enabled_rdt_resource(r) {
0839         list_for_each_entry(d_i, &r->domains, list) {
0840             if (d_i->plr)
0841                 cpumask_or(cpu_with_psl, cpu_with_psl,
0842                        &d_i->cpu_mask);
0843         }
0844     }
0845 
0846     /*
0847      * Next test if new pseudo-locked region would intersect with
0848      * existing region.
0849      */
0850     if (cpumask_intersects(&d->cpu_mask, cpu_with_psl))
0851         ret = true;
0852 
0853     free_cpumask_var(cpu_with_psl);
0854     return ret;
0855 }
0856 
0857 /**
0858  * measure_cycles_lat_fn - Measure cycle latency to read pseudo-locked memory
0859  * @_plr: pseudo-lock region to measure
0860  *
0861  * There is no deterministic way to test if a memory region is cached. One
0862  * way is to measure how long it takes to read the memory, the speed of
0863  * access is a good way to learn how close to the cpu the data was. Even
0864  * more, if the prefetcher is disabled and the memory is read at a stride
0865  * of half the cache line, then a cache miss will be easy to spot since the
0866  * read of the first half would be significantly slower than the read of
0867  * the second half.
0868  *
0869  * Return: 0. Waiter on waitqueue will be woken on completion.
0870  */
0871 static int measure_cycles_lat_fn(void *_plr)
0872 {
0873     struct pseudo_lock_region *plr = _plr;
0874     unsigned long i;
0875     u64 start, end;
0876     void *mem_r;
0877 
0878     local_irq_disable();
0879     /*
0880      * Disable hardware prefetchers.
0881      */
0882     wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
0883     mem_r = READ_ONCE(plr->kmem);
0884     /*
0885      * Dummy execute of the time measurement to load the needed
0886      * instructions into the L1 instruction cache.
0887      */
0888     start = rdtsc_ordered();
0889     for (i = 0; i < plr->size; i += 32) {
0890         start = rdtsc_ordered();
0891         asm volatile("mov (%0,%1,1), %%eax\n\t"
0892                  :
0893                  : "r" (mem_r), "r" (i)
0894                  : "%eax", "memory");
0895         end = rdtsc_ordered();
0896         trace_pseudo_lock_mem_latency((u32)(end - start));
0897     }
0898     wrmsr(MSR_MISC_FEATURE_CONTROL, 0x0, 0x0);
0899     local_irq_enable();
0900     plr->thread_done = 1;
0901     wake_up_interruptible(&plr->lock_thread_wq);
0902     return 0;
0903 }
0904 
0905 /*
0906  * Create a perf_event_attr for the hit and miss perf events that will
0907  * be used during the performance measurement. A perf_event maintains
0908  * a pointer to its perf_event_attr so a unique attribute structure is
0909  * created for each perf_event.
0910  *
0911  * The actual configuration of the event is set right before use in order
0912  * to use the X86_CONFIG macro.
0913  */
0914 static struct perf_event_attr perf_miss_attr = {
0915     .type       = PERF_TYPE_RAW,
0916     .size       = sizeof(struct perf_event_attr),
0917     .pinned     = 1,
0918     .disabled   = 0,
0919     .exclude_user   = 1,
0920 };
0921 
0922 static struct perf_event_attr perf_hit_attr = {
0923     .type       = PERF_TYPE_RAW,
0924     .size       = sizeof(struct perf_event_attr),
0925     .pinned     = 1,
0926     .disabled   = 0,
0927     .exclude_user   = 1,
0928 };
0929 
0930 struct residency_counts {
0931     u64 miss_before, hits_before;
0932     u64 miss_after,  hits_after;
0933 };
0934 
0935 static int measure_residency_fn(struct perf_event_attr *miss_attr,
0936                 struct perf_event_attr *hit_attr,
0937                 struct pseudo_lock_region *plr,
0938                 struct residency_counts *counts)
0939 {
0940     u64 hits_before = 0, hits_after = 0, miss_before = 0, miss_after = 0;
0941     struct perf_event *miss_event, *hit_event;
0942     int hit_pmcnum, miss_pmcnum;
0943     unsigned int line_size;
0944     unsigned int size;
0945     unsigned long i;
0946     void *mem_r;
0947     u64 tmp;
0948 
0949     miss_event = perf_event_create_kernel_counter(miss_attr, plr->cpu,
0950                               NULL, NULL, NULL);
0951     if (IS_ERR(miss_event))
0952         goto out;
0953 
0954     hit_event = perf_event_create_kernel_counter(hit_attr, plr->cpu,
0955                              NULL, NULL, NULL);
0956     if (IS_ERR(hit_event))
0957         goto out_miss;
0958 
0959     local_irq_disable();
0960     /*
0961      * Check any possible error state of events used by performing
0962      * one local read.
0963      */
0964     if (perf_event_read_local(miss_event, &tmp, NULL, NULL)) {
0965         local_irq_enable();
0966         goto out_hit;
0967     }
0968     if (perf_event_read_local(hit_event, &tmp, NULL, NULL)) {
0969         local_irq_enable();
0970         goto out_hit;
0971     }
0972 
0973     /*
0974      * Disable hardware prefetchers.
0975      */
0976     wrmsr(MSR_MISC_FEATURE_CONTROL, prefetch_disable_bits, 0x0);
0977 
0978     /* Initialize rest of local variables */
0979     /*
0980      * Performance event has been validated right before this with
0981      * interrupts disabled - it is thus safe to read the counter index.
0982      */
0983     miss_pmcnum = x86_perf_rdpmc_index(miss_event);
0984     hit_pmcnum = x86_perf_rdpmc_index(hit_event);
0985     line_size = READ_ONCE(plr->line_size);
0986     mem_r = READ_ONCE(plr->kmem);
0987     size = READ_ONCE(plr->size);
0988 
0989     /*
0990      * Read counter variables twice - first to load the instructions
0991      * used in L1 cache, second to capture accurate value that does not
0992      * include cache misses incurred because of instruction loads.
0993      */
0994     rdpmcl(hit_pmcnum, hits_before);
0995     rdpmcl(miss_pmcnum, miss_before);
0996     /*
0997      * From SDM: Performing back-to-back fast reads are not guaranteed
0998      * to be monotonic.
0999      * Use LFENCE to ensure all previous instructions are retired
1000      * before proceeding.
1001      */
1002     rmb();
1003     rdpmcl(hit_pmcnum, hits_before);
1004     rdpmcl(miss_pmcnum, miss_before);
1005     /*
1006      * Use LFENCE to ensure all previous instructions are retired
1007      * before proceeding.
1008      */
1009     rmb();
1010     for (i = 0; i < size; i += line_size) {
1011         /*
1012          * Add a barrier to prevent speculative execution of this
1013          * loop reading beyond the end of the buffer.
1014          */
1015         rmb();
1016         asm volatile("mov (%0,%1,1), %%eax\n\t"
1017                  :
1018                  : "r" (mem_r), "r" (i)
1019                  : "%eax", "memory");
1020     }
1021     /*
1022      * Use LFENCE to ensure all previous instructions are retired
1023      * before proceeding.
1024      */
1025     rmb();
1026     rdpmcl(hit_pmcnum, hits_after);
1027     rdpmcl(miss_pmcnum, miss_after);
1028     /*
1029      * Use LFENCE to ensure all previous instructions are retired
1030      * before proceeding.
1031      */
1032     rmb();
1033     /* Re-enable hardware prefetchers */
1034     wrmsr(MSR_MISC_FEATURE_CONTROL, 0x0, 0x0);
1035     local_irq_enable();
1036 out_hit:
1037     perf_event_release_kernel(hit_event);
1038 out_miss:
1039     perf_event_release_kernel(miss_event);
1040 out:
1041     /*
1042      * All counts will be zero on failure.
1043      */
1044     counts->miss_before = miss_before;
1045     counts->hits_before = hits_before;
1046     counts->miss_after  = miss_after;
1047     counts->hits_after  = hits_after;
1048     return 0;
1049 }
1050 
1051 static int measure_l2_residency(void *_plr)
1052 {
1053     struct pseudo_lock_region *plr = _plr;
1054     struct residency_counts counts = {0};
1055 
1056     /*
1057      * Non-architectural event for the Goldmont Microarchitecture
1058      * from Intel x86 Architecture Software Developer Manual (SDM):
1059      * MEM_LOAD_UOPS_RETIRED D1H (event number)
1060      * Umask values:
1061      *     L2_HIT   02H
1062      *     L2_MISS  10H
1063      */
1064     switch (boot_cpu_data.x86_model) {
1065     case INTEL_FAM6_ATOM_GOLDMONT:
1066     case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
1067         perf_miss_attr.config = X86_CONFIG(.event = 0xd1,
1068                            .umask = 0x10);
1069         perf_hit_attr.config = X86_CONFIG(.event = 0xd1,
1070                           .umask = 0x2);
1071         break;
1072     default:
1073         goto out;
1074     }
1075 
1076     measure_residency_fn(&perf_miss_attr, &perf_hit_attr, plr, &counts);
1077     /*
1078      * If a failure prevented the measurements from succeeding
1079      * tracepoints will still be written and all counts will be zero.
1080      */
1081     trace_pseudo_lock_l2(counts.hits_after - counts.hits_before,
1082                  counts.miss_after - counts.miss_before);
1083 out:
1084     plr->thread_done = 1;
1085     wake_up_interruptible(&plr->lock_thread_wq);
1086     return 0;
1087 }
1088 
1089 static int measure_l3_residency(void *_plr)
1090 {
1091     struct pseudo_lock_region *plr = _plr;
1092     struct residency_counts counts = {0};
1093 
1094     /*
1095      * On Broadwell Microarchitecture the MEM_LOAD_UOPS_RETIRED event
1096      * has two "no fix" errata associated with it: BDM35 and BDM100. On
1097      * this platform the following events are used instead:
1098      * LONGEST_LAT_CACHE 2EH (Documented in SDM)
1099      *       REFERENCE 4FH
1100      *       MISS      41H
1101      */
1102 
1103     switch (boot_cpu_data.x86_model) {
1104     case INTEL_FAM6_BROADWELL_X:
1105         /* On BDW the hit event counts references, not hits */
1106         perf_hit_attr.config = X86_CONFIG(.event = 0x2e,
1107                           .umask = 0x4f);
1108         perf_miss_attr.config = X86_CONFIG(.event = 0x2e,
1109                            .umask = 0x41);
1110         break;
1111     default:
1112         goto out;
1113     }
1114 
1115     measure_residency_fn(&perf_miss_attr, &perf_hit_attr, plr, &counts);
1116     /*
1117      * If a failure prevented the measurements from succeeding
1118      * tracepoints will still be written and all counts will be zero.
1119      */
1120 
1121     counts.miss_after -= counts.miss_before;
1122     if (boot_cpu_data.x86_model == INTEL_FAM6_BROADWELL_X) {
1123         /*
1124          * On BDW references and misses are counted, need to adjust.
1125          * Sometimes the "hits" counter is a bit more than the
1126          * references, for example, x references but x + 1 hits.
1127          * To not report invalid hit values in this case we treat
1128          * that as misses equal to references.
1129          */
1130         /* First compute the number of cache references measured */
1131         counts.hits_after -= counts.hits_before;
1132         /* Next convert references to cache hits */
1133         counts.hits_after -= min(counts.miss_after, counts.hits_after);
1134     } else {
1135         counts.hits_after -= counts.hits_before;
1136     }
1137 
1138     trace_pseudo_lock_l3(counts.hits_after, counts.miss_after);
1139 out:
1140     plr->thread_done = 1;
1141     wake_up_interruptible(&plr->lock_thread_wq);
1142     return 0;
1143 }
1144 
1145 /**
1146  * pseudo_lock_measure_cycles - Trigger latency measure to pseudo-locked region
1147  * @rdtgrp: Resource group to which the pseudo-locked region belongs.
1148  * @sel: Selector of which measurement to perform on a 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  */
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 
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  */
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 dereference
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_lock which is obtained in the
1336      * device_create() and debugfs_create_dir() callpath below as well as
1337      * 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 asymmetry 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  */
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 
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 
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 
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 
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 
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 
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 
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 }