[PATCH 2/8] nitro_enclaves: Initialise the CPU pool mutex statically

Alexander Graf <[email protected]>
Newsgroups org.kernel.vger.linux-doc,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
ne_cpus is registered with module_param_cb(), so its setter runs while
the parameter section is parsed: from the kernel command line as
nitro_enclaves.ne_cpus= for a built-in driver, or from modprobe
nitro_enclaves ne_cpus=<cpu-list>. parse_args() precedes do_initcalls()
in start_kernel() and do_init_module() in load_module(), so the setter
runs before the driver's own code. It reaches ne_teardown_cpu_pool() and
then ne_setup_cpu_pool(), both of which lock ne_cpu_pool.mutex, while
the mutex_init() for that lock sits in ne_init(). An admin who
configures the pool at boot or at module load therefore takes a mutex
that nothing has initialised. Commit ff8a4d3e3a99 ("nitro_enclaves: Add
logic for setting an enclave vCPU") added those two locks; the parameter
and its runtime mutex_init() were already in place.

Under CONFIG_DEBUG_MUTEXES the first acquire trips a
DEBUG_LOCKS_WARN_ON() in kernel/locking/mutex.c, because ->magic is
still NULL rather than the lock's own address, and the splat carries
parse_args() and ne_set_kernel_param() in its backtrace. It is not a
crash: with lock debugging off nothing is reported and the pool comes
up, which is how this survived. A write to
/sys/module/nitro_enclaves/parameters/ne_cpus is not affected: the
initcall has run before user space exists.

So initialise the mutex where the pool is defined and drop the
mutex_init() call, leaving it valid from the moment the parameter
section is parsed.

Fixes: ff8a4d3e3a99 ("nitro_enclaves: Add logic for setting an enclave vCPU")
Assisted-by: Kiro:claude-opus-5
Signed-off-by: Alexander Graf <[email protected]>
---
 drivers/virt/nitro_enclaves/ne_misc_dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index 8222fbe75800..59e1794587e6 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -125,7 +125,9 @@ struct ne_cpu_pool {
 	int		numa_node;
 };
 
-static struct ne_cpu_pool ne_cpu_pool;
+static struct ne_cpu_pool ne_cpu_pool = {
+	.mutex = __MUTEX_INITIALIZER(ne_cpu_pool.mutex),
+};
 
 /**
  * struct ne_phys_contig_mem_regions - Contiguous physical memory regions.
@@ -1761,8 +1763,6 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 static int __init ne_init(void)
 {
-	mutex_init(&ne_cpu_pool.mutex);
-
 	return pci_register_driver(&ne_pci_driver);
 }
 
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.