remove_cpu() and add_cpu() take device_hotplug_lock, and the driver calls both with the CPU pool mutex held. ACPI hotplug holds device_hotplug_lock around a device's probe and remove callbacks, and enclave creation takes the pool mutex with the enclave list mutex held. In preparation to make remove wait for the driver's file operations, rework pool setup and teardown so that no CPU changes state under the pool mutex: a remove that waits on a lock an ioctl holds, while the ioctl waits for the pool mutex and a pool change waits for device_hotplug_lock, would deadlock. Build a new pool in local memory, offline its CPUs with no locks held, and publish the finished pool as the last step. Tear a pool down by taking it out of the driver's view first, and online its CPUs once no reader can see them. The pool mutex then only guards the switch of the pool fields, so a reader sees the whole pool or none of it. Two pool changes cannot interleave: the kernel holds the parameter lock across the ne_cpus setter. Signed-off-by: Alexander Graf --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 148 ++++++++++++++-------- 1 file changed, 98 insertions(+), 50 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 45ed6c3d89fdb..049ea089d0cb4 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -163,6 +163,75 @@ static bool ne_check_enclaves_created(void) return ret; } +/** + * ne_free_core_slots() - Free an array of cpumasks that holds the threads of + * full CPU cores. + * @threads_per_core: The array with one cpumask per full CPU core. + * @nr_cores: The size of the array. + * + * Context: Process context. + */ +static void ne_free_core_slots(cpumask_var_t *threads_per_core, unsigned int nr_cores) +{ + unsigned int i = 0; + + if (!threads_per_core) + return; + + for (i = 0; i < nr_cores; i++) + free_cpumask_var(threads_per_core[i]); + + kfree(threads_per_core); +} + +/** + * ne_online_core_slots() - Online the CPUs of an array of full CPU cores. + * @threads_per_core: The array with one cpumask per full CPU core. + * @nr_cores: The size of the array. + * + * Context: Process context. No driver lock may be held: add_cpu() takes + * device_hotplug_lock. + */ +static void ne_online_core_slots(cpumask_var_t *threads_per_core, unsigned int nr_cores) +{ + unsigned int cpu = 0; + unsigned int i = 0; + int rc = -EINVAL; + + for (i = 0; i < nr_cores; i++) + for_each_cpu(cpu, threads_per_core[i]) { + rc = add_cpu(cpu); + if (rc != 0) + pr_err("%s: CPU %d is not onlined [rc=%d]\n", + ne_misc_dev.name, cpu, rc); + } +} + +/** + * ne_detach_cpu_pool() - Take the CPU pool out of the driver's view. + * @threads_per_core: Returns the array with one cpumask per core of the pool. + * @nr_cores: Returns the size of the array. + * + * The caller owns the returned array: it onlines the CPUs and frees the array + * once no reader can see the pool anymore. + * + * Context: Process context. + */ +static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *nr_cores) +{ + mutex_lock(&ne_cpu_pool.mutex); + + *threads_per_core = ne_cpu_pool.avail_threads_per_core; + *nr_cores = ne_cpu_pool.nr_parent_vm_cores; + + ne_cpu_pool.avail_threads_per_core = NULL; + ne_cpu_pool.nr_parent_vm_cores = 0; + ne_cpu_pool.nr_threads_per_core = 0; + ne_cpu_pool.numa_node = -1; + + mutex_unlock(&ne_cpu_pool.mutex); +} + /** * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such * as not sharing CPU cores with the primary / parent VM @@ -171,7 +240,9 @@ static bool ne_check_enclaves_created(void) * pool after the checks passed. * @ne_cpu_list: The CPU list used for setting NE CPU pool. * - * Context: Process context. + * Context: Process context. No driver lock may be held: remove_cpu() and + * add_cpu() take device_hotplug_lock. The pool mutex only guards the + * publish of the finished pool. * Return: * * 0 on success. * * Negative return value on failure. @@ -183,14 +254,15 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) cpumask_var_t cpu_pool; unsigned int cpu_sibling = 0; unsigned int i = 0; + unsigned int nr_parent_vm_cores = 0; + unsigned int nr_threads_per_core = 0; int numa_node = -1; int rc = -EINVAL; + cpumask_var_t *threads_per_core = NULL; if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL)) return -ENOMEM; - mutex_lock(&ne_cpu_pool.mutex); - rc = cpulist_parse(ne_cpu_list, cpu_pool); if (rc < 0) { pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev.name, rc); @@ -290,20 +362,19 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) /* Calculate the number of threads from a full CPU core. */ cpu = cpumask_any(cpu_pool); for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu)) - ne_cpu_pool.nr_threads_per_core++; + nr_threads_per_core++; - ne_cpu_pool.nr_parent_vm_cores = nr_cpu_ids / ne_cpu_pool.nr_threads_per_core; + nr_parent_vm_cores = nr_cpu_ids / nr_threads_per_core; - ne_cpu_pool.avail_threads_per_core = kzalloc_objs(*ne_cpu_pool.avail_threads_per_core, - ne_cpu_pool.nr_parent_vm_cores); - if (!ne_cpu_pool.avail_threads_per_core) { + threads_per_core = kzalloc_objs(*threads_per_core, nr_parent_vm_cores); + if (!threads_per_core) { rc = -ENOMEM; goto free_pool_cpumask; } - for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) - if (!zalloc_cpumask_var(&ne_cpu_pool.avail_threads_per_core[i], GFP_KERNEL)) { + for (i = 0; i < nr_parent_vm_cores; i++) + if (!zalloc_cpumask_var(&threads_per_core[i], GFP_KERNEL)) { rc = -ENOMEM; goto free_cores_cpumask; @@ -315,16 +386,16 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) */ for_each_cpu(cpu, cpu_pool) { core_id = topology_core_id(cpu); - if (core_id < 0 || core_id >= ne_cpu_pool.nr_parent_vm_cores) { + if (core_id < 0 || core_id >= nr_parent_vm_cores) { pr_err("%s: Invalid core id %d for CPU %d\n", ne_misc_dev.name, core_id, cpu); rc = -EINVAL; - goto clear_cpumask; + goto free_cores_cpumask; } - cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id]); + cpumask_set_cpu(cpu, threads_per_core[core_id]); } /* @@ -349,6 +420,11 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) free_cpumask_var(cpu_pool); + mutex_lock(&ne_cpu_pool.mutex); + + ne_cpu_pool.avail_threads_per_core = threads_per_core; + ne_cpu_pool.nr_parent_vm_cores = nr_parent_vm_cores; + ne_cpu_pool.nr_threads_per_core = nr_threads_per_core; ne_cpu_pool.numa_node = numa_node; mutex_unlock(&ne_cpu_pool.mutex); @@ -358,19 +434,10 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) online_cpus: for_each_cpu(cpu, cpu_pool) add_cpu(cpu); -clear_cpumask: - for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) - cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]); free_cores_cpumask: - for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) - free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]); - kfree(ne_cpu_pool.avail_threads_per_core); + ne_free_core_slots(threads_per_core, nr_parent_vm_cores); free_pool_cpumask: free_cpumask_var(cpu_pool); - ne_cpu_pool.nr_parent_vm_cores = 0; - ne_cpu_pool.nr_threads_per_core = 0; - ne_cpu_pool.numa_node = -1; - mutex_unlock(&ne_cpu_pool.mutex); return rc; } @@ -380,41 +447,22 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list) * CPU pool. * @void: No parameters provided. * - * Context: Process context. + * Context: Process context. No driver lock may be held: add_cpu() takes + * device_hotplug_lock. */ static void ne_teardown_cpu_pool(void) { - unsigned int cpu = 0; - unsigned int i = 0; - int rc = -EINVAL; + cpumask_var_t *threads_per_core = NULL; + unsigned int nr_cores = 0; - mutex_lock(&ne_cpu_pool.mutex); - - if (!ne_cpu_pool.nr_parent_vm_cores) { - mutex_unlock(&ne_cpu_pool.mutex); + ne_detach_cpu_pool(&threads_per_core, &nr_cores); + if (!threads_per_core) return; - } - for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) { - for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]) { - rc = add_cpu(cpu); - if (rc != 0) - pr_err("%s: CPU %d is not onlined [rc=%d]\n", - ne_misc_dev.name, cpu, rc); - } - - cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]); + ne_online_core_slots(threads_per_core, nr_cores); - free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]); - } - - kfree(ne_cpu_pool.avail_threads_per_core); - ne_cpu_pool.nr_parent_vm_cores = 0; - ne_cpu_pool.nr_threads_per_core = 0; - ne_cpu_pool.numa_node = -1; - - mutex_unlock(&ne_cpu_pool.mutex); + ne_free_core_slots(threads_per_core, nr_cores); } /** -- 2.47.1