From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=52.35.192.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795977220; b=mdZ08de7xZVCbFhf8SFMmIPPZyeiv/Xy3ixT275jCGTG4IlcWW2Z9Fwh71RpEMoMC5DD ao14iO6Yd5SPvN0/ThdyPMP5VrkapiI74sqdhlMm1kqRRt4JcJ5RJp+3j3WUKblnknS0D Sry9coWpqaQtNyBpJM2zECyFNWYvIQOc33xGjhZUwOsclf1mIFvm75X5Irb9iiyKX0q9S HOu4nkXjeyU2Gm2bhMxi91IpFEL48Jx21IYliUi9WWSPg/psuS3xYzWnko5BPjmuAOZ1t aSNjCmA8tMZzNbSw0JZEMJUFaqwTUDEUKAYq2z9Qa+U9n+B5eJEGdNId0idy5MA3FxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795977220; 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=q/Un5OmS6aGksv4+r/rman+S8m4hdjQEFvVvVvSQMj0=; b=Zj7eFhNxcpEXzH56TYgZdrsF2kP9ey4IWuOt8hZIqwtvCpJnZp6JWCww5cn1izg52fjf qK5RaXe1jIpPqgaSUqo0iIFqvATlCVSS8w7lUldYDshLZZnjHfTzr6NMWmrlhp5kbe32y lDPJvevTtRMpjv457NRCtX+sFkeUWHlxQXhfzk7FoOtlmMkoM45QTT9ye/47Gw1DIwmAK oqbRVXUCWH17zYyQbddFRiMSY3ha/ppRtpaXOCJbQZWoonoClfi7vU4hiBbfqtkh/wRad bgKBsz23JvIWlikRoXsIbPmmaA4D2BTtc5Z4YAHkDVCDdROvGuUynOif1oi5lFey9kw== 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=U8V2ixTn; arc=none smtp.remote-ip=52.35.192.45 Received: from pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com [52.35.192.45]) (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 E67C4606A31C; Sun, 29 Nov 2026 18:33:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E67C4606A31C 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=U8V2ixTn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795977219; x=1827513219; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=q/Un5OmS6aGksv4+r/rman+S8m4hdjQEFvVvVvSQMj0=; b=U8V2ixTnfYDFhEYlL5KUDKqoHTxS2J7YwiX+ur43uPauIKt/BLDMlNs+ PQjcc7CPnx1fSO3DRp5evrZ5giIwOYRzYn4hIMMqJmyxgNV3Ce7Uk/pHH B/3RJlQOXgI1PHCZ/WMjzr7uRJ3WrXQowrzkTSS9A4ajAFuZ3X3HGMPKY ZiwMEnxhM0b0U7lfnai0/dKCM+LzCPt5jjf5CdpnEuoPxKKO/WA7U9RqG gW8n9eiJqbC2WtztK26T0Ymvwwn8aMAAc10+f8TVTkdnSC4SaBVYifMdf QfZeWKm2LSrGy3mbpFLXYYspyNeKRhYvE7M2FrZQgkSnxAoHvZ9Ljy92J g==; X-CSE-ConnectionGUID: RZyKFvIjTH28goFml/Elvw== X-CSE-MsgGUID: Mg7bgYi4T56Mzpbh2K/zow== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35140373" Received: from ip-10-5-9-48.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.9.48]) by internal-pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:33:39 +0000 Received: from EX19MTAUWA001.ant.amazon.com [205.251.233.236:21055] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.230:2525] with esmtp (Farcaster) id 59f93640-e2e9-42c2-8ef2-229cad06dcd9; Sun, 29 Nov 2026 18:33:39 +0000 (UTC) X-Farcaster-Flow-ID: 59f93640-e2e9-42c2-8ef2-229cad06dcd9 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA001.ant.amazon.com (10.250.64.218) 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:33:38 +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:33:35 +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 20/23] nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock Date: Sun, 29 Nov 2026 18:21:49 +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: EX19D031UWC004.ant.amazon.com (10.13.139.246) To EX19D001UWA001.ant.amazon.com (10.13.138.214) An admin who writes a CPU list to ne_cpus while a CPU goes offline can get a pool built from the topology before and after that CPU went. ne_setup_cpu_pool() reads cpu_is_offline(), cpu_to_node() and topology_sibling_cpumask() for the list without the CPU hotplug lock. Take cpus_read_lock() over those reads, and drop it before the CPUs are offlined, because remove_cpu() takes the write side. The failure paths before the drop share one cpus_read_unlock(). Fixes: ff8a4d3e3a99 ("nitro_enclaves: Add logic for setting an enclave vCPU") Signed-off-by: Alexander Graf --- v1 -> v2: - Adapt to the pool setup that offlines the CPUs with no lock held - Unwind failures before the lock drop with one cpus_read_unlock() drivers/virt/nitro_enclaves/ne_misc_dev.c | 31 +++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index ed7af30f69afa..7fc6b0bb5dbc6 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -226,6 +226,11 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n * pool after the checks passed. * @cpu_pool: The CPU mask used for setting NE CPU pool. * + * The topology of the pool - online state, sibling masks, core ids - is read + * under the CPU hotplug read lock. The lock is dropped before the pool CPUs are + * offlined, because remove_cpu() takes the write side of the same percpu-rwsem. + * Nothing reads topology after the drop. + * * 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. @@ -245,13 +250,15 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) int rc = -EINVAL; cpumask_var_t *threads_per_core = NULL; + cpus_read_lock(); + cpu = cpumask_any(cpu_pool); if (cpu >= nr_cpu_ids) { pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev.name); rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } /* @@ -265,7 +272,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } /* @@ -280,7 +287,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } } else { if (numa_node != cpu_to_node(cpu)) { @@ -289,7 +296,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } } @@ -302,7 +309,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } for_each_cpu(cpu_sibling, topology_sibling_cpumask(0)) { @@ -312,7 +319,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } } @@ -329,7 +336,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) rc = -EINVAL; - goto free_cores_cpumask; + goto unlock_hotplug; } } } @@ -345,7 +352,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) if (!threads_per_core) { rc = -ENOMEM; - goto free_cores_cpumask; + goto unlock_hotplug; } for (i = 0; i < nr_parent_vm_cores; i++) @@ -373,6 +380,8 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) cpumask_set_cpu(cpu, threads_per_core[core_id]); } + cpus_read_unlock(); + /* * CPUs that are given to enclave(s) should not be considered online * by Linux anymore, as the hypervisor will degrade them to floating. @@ -410,8 +419,14 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool) online_cpus: for_each_cpu(cpu, cpu_pool) add_cpu(cpu); + ne_free_core_slots(threads_per_core, nr_parent_vm_cores); + + return rc; + free_cores_cpumask: ne_free_core_slots(threads_per_core, nr_parent_vm_cores); +unlock_hotplug: + cpus_read_unlock(); return rc; } -- 2.47.1