From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=44.245.243.92 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976835; b=ExFRFAPEp5Av/fjfertVw+NQWcrt691vTFw4cw9oQjy1kSNsuxMMQqqSnk+g5ehj0WuY ZDbmZ4SM3DobopmLmcwJIKGmqhJDgIM5ihulJcyNKPgYdbOklduhWxcgDG7D0uF+t2A03 ciYg5tm8G+b9zui0pOsf0lzTg2S5a8Z4HC0OH2j59oM/i69jtfzh4yoUpE4M+BkKznJva s0rRvmcYKWgxBJdDFNZHUa5gvVwz8FmKZWjI21kGoxgrCUp6Q7/iuhUNhWXAi4JqOI8XR QtC69fgGW7Nt82axHMId7HB2Qnye5ON9sEFZmhC1H4mVyetzxVt9Z4y0itDGDgVIwWw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976835; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=s2DPRmtXz7Z6doNg+oU8iXohKznu+QZr9saXZem8E+g=; b=rUw8uSkumlzTniKR45/Bltf94gwq6QAhCYe108z1aC1GAOv4GYBFCWJlSa91WPb7l3rO DUAfhNiIdSOVe9Q9Kuvaln6L+niQepAq7pAN5QHM4zkCIM8oIyLM9O1SHbtmjXxCkstq8 iY/CJDEEKm7JkSRVyq73hO3oWuhnD87JOxDnxt89hz/4lWHALW5JKgTbnlW4CwCQaPIjH H1EE8bLr/PeDUkpX1qkgKam+HduKi1luyT154w+ydCQ3xW0fYcaEDzaR/PeMdJhdd7+Fx Z84MduF0evRZC/tiPqPJzTjtmDYxZBefIcVb6R3mlFr2pOQuGlz4ibfrfRHb7nZq+tQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.de; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=bznaxNDK; arc=none smtp.remote-ip=44.245.243.92 Received: from pdx-out-001.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-001.esa.us-west-2.outbound.mail-perimeter.amazon.com [44.245.243.92]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8048606A31C; Sun, 29 Nov 2026 18:27:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8048606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=bznaxNDK DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976834; x=1827512834; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s2DPRmtXz7Z6doNg+oU8iXohKznu+QZr9saXZem8E+g=; b=bznaxNDK3osbozMQosUq2cyjqXbfP5OnUZFcXDBlSJ+omqydIzCczYln EvZI/s0aNqSemaU9wkK4Gv07sSsNV4ggtaVHuxrjAP//K96oA1G68WAX/ x9K/08TgHF/CvTY/l80zuGP8UnDY5ID4qp2NWyim8Cy7Q74UMpdAg0395 CuhSvGXcRhWdoqLUnHfHpyZbXidZ3IrCPAUkZkssZJYsiA0vCQ5rbKYZv eB0BbBMlrnA7HWTKJhPSfipfc3zJC6rCWCBIeDwCaLmr+lhOD2JG46kRY l7wGixpNsv8AwV8k02z02CZLeGEN3LShchw7GGaN0WGmUmu8Rkq2LekJA w==; X-CSE-ConnectionGUID: a+uOwK20RYufJ7Qty/s6Bw== X-CSE-MsgGUID: +BKjhJKISGGxN3NXRxWSWw== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="34939926" Received: from ip-10-5-6-203.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.6.203]) by internal-pdx-out-001.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:27:14 +0000 Received: from EX19MTAUWA002.ant.amazon.com [205.251.233.178:29322] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.27.231:2525] with esmtp (Farcaster) id b19bdbd8-5dc0-43c2-aac1-9d8ce6f38c08; Sun, 29 Nov 2026 18:27:14 +0000 (UTC) X-Farcaster-Flow-ID: b19bdbd8-5dc0-43c2-aac1-9d8ce6f38c08 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:27:13 +0000 Received: from ip-10-253-83-51.amazon.com (172.19.99.218) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:27:10 +0000 From: Alexander Graf To: Greg Kroah-Hartman , Arnd Bergmann CC: The AWS Nitro Enclaves Team , , Paolo Bonzini , "Stefano Garzarella" , Stefan Hajnoczi , Marek Szyprowski , Robin Murphy , Andrew Morton , "David Hildenbrand" , Mike Rapoport , "Jonathan Corbet" , Shuah Khan , "Randy Dunlap" , , , , , , , , , Subject: [PATCH v2 09/23] nitro_enclaves: Online and offline pool CPUs with no pool lock held Date: Sun, 29 Nov 2026 18:21:38 +0000 Message-ID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWA003.ant.amazon.com (10.13.139.47) To EX19D001UWA001.ant.amazon.com (10.13.138.214) 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