1099 lines · 29.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * Resource Director Technology (RDT) * * Pseudo-locking support built on top of Cache Allocation Technology (CAT) * * Copyright (C) 2018 Intel Corporation * * Author: Reinette Chatre <reinette.chatre@intel.com> */ #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt #include <linux/cacheinfo.h>#include <linux/cpu.h>#include <linux/cpumask.h>#include <linux/debugfs.h>#include <linux/kthread.h>#include <linux/mman.h>#include <linux/pm_qos.h>#include <linux/resctrl.h>#include <linux/slab.h>#include <linux/uaccess.h> #include "internal.h" /* * Major number assigned to and shared by all devices exposing * pseudo-locked regions. */static unsigned int pseudo_lock_major; static unsigned long pseudo_lock_minor_avail = GENMASK(MINORBITS, 0); static char *pseudo_lock_devnode(const struct device *dev, umode_t *mode){	const struct rdtgroup *rdtgrp; 	rdtgrp = dev_get_drvdata(dev);	if (mode)		*mode = 0600;	guard(mutex)(&rdtgroup_mutex);	return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdt_kn_name(rdtgrp->kn));} static const struct class pseudo_lock_class = {	.name = "pseudo_lock",	.devnode = pseudo_lock_devnode,}; /** * pseudo_lock_minor_get - Obtain available minor number * @minor: Pointer to where new minor number will be stored * * A bitmask is used to track available minor numbers. Here the next free * minor number is marked as unavailable and returned. * * Return: 0 on success, <0 on failure. */static int pseudo_lock_minor_get(unsigned int *minor){	unsigned long first_bit; 	first_bit = find_first_bit(&pseudo_lock_minor_avail, MINORBITS); 	if (first_bit == MINORBITS)		return -ENOSPC; 	__clear_bit(first_bit, &pseudo_lock_minor_avail);	*minor = first_bit; 	return 0;} /** * pseudo_lock_minor_release - Return minor number to available * @minor: The minor number made available */static void pseudo_lock_minor_release(unsigned int minor){	__set_bit(minor, &pseudo_lock_minor_avail);} /** * region_find_by_minor - Locate a pseudo-lock region by inode minor number * @minor: The minor number of the device representing pseudo-locked region * * When the character device is accessed we need to determine which * pseudo-locked region it belongs to. This is done by matching the minor * number of the device to the pseudo-locked region it belongs. * * Minor numbers are assigned at the time a pseudo-locked region is associated * with a cache instance. * * Return: On success return pointer to resource group owning the pseudo-locked *         region, NULL on failure. */static struct rdtgroup *region_find_by_minor(unsigned int minor){	struct rdtgroup *rdtgrp, *rdtgrp_match = NULL; 	list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {		if (rdtgrp->plr && rdtgrp->plr->minor == minor) {			rdtgrp_match = rdtgrp;			break;		}	}	return rdtgrp_match;} /** * struct pseudo_lock_pm_req - A power management QoS request list entry * @list:	Entry within the @pm_reqs list for a pseudo-locked region * @req:	PM QoS request */struct pseudo_lock_pm_req {	struct list_head list;	struct dev_pm_qos_request req;}; static void pseudo_lock_cstates_relax(struct pseudo_lock_region *plr){	struct pseudo_lock_pm_req *pm_req, *next; 	list_for_each_entry_safe(pm_req, next, &plr->pm_reqs, list) {		dev_pm_qos_remove_request(&pm_req->req);		list_del(&pm_req->list);		kfree(pm_req);	}} /** * pseudo_lock_cstates_constrain - Restrict cores from entering C6 * @plr: Pseudo-locked region * * To prevent the cache from being affected by power management entering * C6 has to be avoided. This is accomplished by requesting a latency * requirement lower than lowest C6 exit latency of all supported * platforms as found in the cpuidle state tables in the intel_idle driver. * At this time it is possible to do so with a single latency requirement * for all supported platforms. * * Since Goldmont is supported, which is affected by X86_BUG_MONITOR, * the ACPI latencies need to be considered while keeping in mind that C2 * may be set to map to deeper sleep states. In this case the latency * requirement needs to prevent entering C2 also. * * Return: 0 on success, <0 on failure */static int pseudo_lock_cstates_constrain(struct pseudo_lock_region *plr){	struct pseudo_lock_pm_req *pm_req;	int cpu;	int ret; 	for_each_cpu(cpu, &plr->d->hdr.cpu_mask) {		pm_req = kzalloc_obj(*pm_req);		if (!pm_req) {			rdt_last_cmd_puts("Failure to allocate memory for PM QoS\n");			ret = -ENOMEM;			goto out_err;		}		ret = dev_pm_qos_add_request(get_cpu_device(cpu),					     &pm_req->req,					     DEV_PM_QOS_RESUME_LATENCY,					     30);		if (ret < 0) {			rdt_last_cmd_printf("Failed to add latency req CPU%d\n",					    cpu);			kfree(pm_req);			ret = -1;			goto out_err;		}		list_add(&pm_req->list, &plr->pm_reqs);	} 	return 0; out_err:	pseudo_lock_cstates_relax(plr);	return ret;} /** * pseudo_lock_region_clear - Reset pseudo-lock region data * @plr: pseudo-lock region * * All content of the pseudo-locked region is reset - any memory allocated * freed. * * Return: void */static void pseudo_lock_region_clear(struct pseudo_lock_region *plr){	plr->size = 0;	plr->line_size = 0;	kfree(plr->kmem);	plr->kmem = NULL;	plr->s = NULL;	if (plr->d)		plr->d->plr = NULL;	plr->d = NULL;	plr->cbm = 0;	plr->debugfs_dir = NULL;} /** * pseudo_lock_region_init - Initialize pseudo-lock region information * @plr: pseudo-lock region * * Called after user provided a schemata to be pseudo-locked. From the * schemata the &struct pseudo_lock_region is on entry already initialized * with the resource, domain, and capacity bitmask. Here the information * required for pseudo-locking is deduced from this data and &struct * pseudo_lock_region initialized further. This information includes: * - size in bytes of the region to be pseudo-locked * - cache line size to know the stride with which data needs to be accessed *   to be pseudo-locked * - a cpu associated with the cache instance on which the pseudo-locking *   flow can be executed * * Return: 0 on success, <0 on failure. Descriptive error will be written * to last_cmd_status buffer. */static int pseudo_lock_region_init(struct pseudo_lock_region *plr){	enum resctrl_scope scope = plr->s->res->ctrl_scope;	struct cacheinfo *ci;	int ret; 	if (WARN_ON_ONCE(scope != RESCTRL_L2_CACHE && scope != RESCTRL_L3_CACHE))		return -ENODEV; 	/* Pick the first cpu we find that is associated with the cache. */	plr->cpu = cpumask_first(&plr->d->hdr.cpu_mask); 	if (!cpu_online(plr->cpu)) {		rdt_last_cmd_printf("CPU %u associated with cache not online\n",				    plr->cpu);		ret = -ENODEV;		goto out_region;	} 	ci = get_cpu_cacheinfo_level(plr->cpu, scope);	if (ci) {		plr->line_size = ci->coherency_line_size;		plr->size = rdtgroup_cbm_to_size(plr->s->res, plr->d, plr->cbm);		return 0;	} 	ret = -1;	rdt_last_cmd_puts("Unable to determine cache line size\n");out_region:	pseudo_lock_region_clear(plr);	return ret;} /** * pseudo_lock_init - Initialize a pseudo-lock region * @rdtgrp: resource group to which new pseudo-locked region will belong * * A pseudo-locked region is associated with a resource group. When this * association is created the pseudo-locked region is initialized. The * details of the pseudo-locked region are not known at this time so only * allocation is done and association established. * * Return: 0 on success, <0 on failure */static int pseudo_lock_init(struct rdtgroup *rdtgrp){	struct pseudo_lock_region *plr; 	plr = kzalloc_obj(*plr);	if (!plr)		return -ENOMEM; 	init_waitqueue_head(&plr->lock_thread_wq);	INIT_LIST_HEAD(&plr->pm_reqs);	rdtgrp->plr = plr;	return 0;} /** * pseudo_lock_region_alloc - Allocate kernel memory that will be pseudo-locked * @plr: pseudo-lock region * * Initialize the details required to set up the pseudo-locked region and * allocate the contiguous memory that will be pseudo-locked to the cache. * * Return: 0 on success, <0 on failure.  Descriptive error will be written * to last_cmd_status buffer. */static int pseudo_lock_region_alloc(struct pseudo_lock_region *plr){	int ret; 	ret = pseudo_lock_region_init(plr);	if (ret < 0)		return ret; 	/*	 * We do not yet support contiguous regions larger than	 * KMALLOC_MAX_SIZE.	 */	if (plr->size > KMALLOC_MAX_SIZE) {		rdt_last_cmd_puts("Requested region exceeds maximum size\n");		ret = -E2BIG;		goto out_region;	} 	plr->kmem = kzalloc(plr->size, GFP_KERNEL);	if (!plr->kmem) {		rdt_last_cmd_puts("Unable to allocate memory\n");		ret = -ENOMEM;		goto out_region;	} 	ret = 0;	goto out;out_region:	pseudo_lock_region_clear(plr);out:	return ret;} /** * pseudo_lock_free - Free a pseudo-locked region * @rdtgrp: resource group to which pseudo-locked region belonged * * The pseudo-locked region's resources have already been released, or not * yet created at this point. Now it can be freed and disassociated from the * resource group. * * Return: void */static void pseudo_lock_free(struct rdtgroup *rdtgrp){	pseudo_lock_region_clear(rdtgrp->plr);	kfree(rdtgrp->plr);	rdtgrp->plr = NULL;} /** * rdtgroup_monitor_in_progress - Test if monitoring in progress * @rdtgrp: resource group being queried * * Return: 1 if monitor groups have been created for this resource * group, 0 otherwise. */static int rdtgroup_monitor_in_progress(struct rdtgroup *rdtgrp){	return !list_empty(&rdtgrp->mon.crdtgrp_list);} /** * rdtgroup_locksetup_user_restrict - Restrict user access to group * @rdtgrp: resource group needing access restricted * * A resource group used for cache pseudo-locking cannot have cpus or tasks * assigned to it. This is communicated to the user by restricting access * to all the files that can be used to make such changes. * * Permissions restored with rdtgroup_locksetup_user_restore() * * Return: 0 on success, <0 on failure. If a failure occurs during the * restriction of access an attempt will be made to restore permissions but * the state of the mode of these files will be uncertain when a failure * occurs. */static int rdtgroup_locksetup_user_restrict(struct rdtgroup *rdtgrp){	int ret; 	ret = rdtgroup_kn_mode_restrict(rdtgrp, "tasks");	if (ret)		return ret; 	ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus");	if (ret)		goto err_tasks; 	ret = rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");	if (ret)		goto err_cpus; 	if (resctrl_arch_mon_capable()) {		ret = rdtgroup_kn_mode_restrict(rdtgrp, "mon_groups");		if (ret)			goto err_cpus_list;	} 	ret = 0;	goto out; err_cpus_list:	rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);err_cpus:	rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);err_tasks:	rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);out:	return ret;} /** * rdtgroup_locksetup_user_restore - Restore user access to group * @rdtgrp: resource group needing access restored * * Restore all file access previously removed using * rdtgroup_locksetup_user_restrict() * * Return: 0 on success, <0 on failure.  If a failure occurs during the * restoration of access an attempt will be made to restrict permissions * again but the state of the mode of these files will be uncertain when * a failure occurs. */static int rdtgroup_locksetup_user_restore(struct rdtgroup *rdtgrp){	int ret; 	ret = rdtgroup_kn_mode_restore(rdtgrp, "tasks", 0777);	if (ret)		return ret; 	ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus", 0777);	if (ret)		goto err_tasks; 	ret = rdtgroup_kn_mode_restore(rdtgrp, "cpus_list", 0777);	if (ret)		goto err_cpus; 	if (resctrl_arch_mon_capable()) {		ret = rdtgroup_kn_mode_restore(rdtgrp, "mon_groups", 0777);		if (ret)			goto err_cpus_list;	} 	ret = 0;	goto out; err_cpus_list:	rdtgroup_kn_mode_restrict(rdtgrp, "cpus_list");err_cpus:	rdtgroup_kn_mode_restrict(rdtgrp, "cpus");err_tasks:	rdtgroup_kn_mode_restrict(rdtgrp, "tasks");out:	return ret;} /** * rdtgroup_locksetup_enter - Resource group enters locksetup mode * @rdtgrp: resource group requested to enter locksetup mode * * A resource group enters locksetup mode to reflect that it would be used * to represent a pseudo-locked region and is in the process of being set * up to do so. A resource group used for a pseudo-locked region would * lose the closid associated with it so we cannot allow it to have any * tasks or cpus assigned nor permit tasks or cpus to be assigned in the * future. Monitoring of a pseudo-locked region is not allowed either. * * The above and more restrictions on a pseudo-locked region are checked * for and enforced before the resource group enters the locksetup mode. * * Returns: 0 if the resource group successfully entered locksetup mode, <0 * on failure. On failure the last_cmd_status buffer is updated with text to * communicate details of failure to the user. */int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp){	int ret; 	/*	 * The default resource group can neither be removed nor lose the	 * default closid associated with it.	 */	if (rdtgrp == &rdtgroup_default) {		rdt_last_cmd_puts("Cannot pseudo-lock default group\n");		return -EINVAL;	} 	/*	 * Cache Pseudo-locking not supported when CDP is enabled.	 *	 * Some things to consider if you would like to enable this	 * support (using L3 CDP as example):	 * - When CDP is enabled two separate resources are exposed,	 *   L3DATA and L3CODE, but they are actually on the same cache.	 *   The implication for pseudo-locking is that if a	 *   pseudo-locked region is created on a domain of one	 *   resource (eg. L3CODE), then a pseudo-locked region cannot	 *   be created on that same domain of the other resource	 *   (eg. L3DATA). This is because the creation of a	 *   pseudo-locked region involves a call to wbinvd that will	 *   affect all cache allocations on particular domain.	 * - Considering the previous, it may be possible to only	 *   expose one of the CDP resources to pseudo-locking and	 *   hide the other. For example, we could consider to only	 *   expose L3DATA and since the L3 cache is unified it is

Lines 1–500 of 1099.