On Tue, Jun 03, 2025 at 07:44:08AM +0800, Huang, Kai wrote: > >> static int init_tdx_module(void) >> { >> int ret; >> @@ -1136,6 +1209,8 @@ static int init_tdx_module(void) >> >> pr_info("%lu KB allocated for PAMT\n", tdmrs_count_pamt_kb(&tdx_tdmr_list)); >> >> + tdx_subsys_init(); >> + >> out_put_tdxmem: >> /* >> * @tdx_memlist is written here and read at memory hotplug time. > >The error handling of init_module_module() is already very heavy. Although >tdx_subsys_init() doesn't return any error, I would prefer to putting >tdx_subsys_init() to __tdx_enable() (the caller of init_tdx_module()) so that >init_tdx_module() can just focus on initializing the TDX module. Sounds good. Will do. btw, I think we can use guard() to simplify the error-handling a bit, e.g., diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index a755cdef69d2..0b93064b9e0f 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1218,11 +1218,11 @@ static int init_tdx_module(void) * holding mem_hotplug_lock read-lock as the memory hotplug code * path reads the @tdx_memlist to reject any new memory. */ - get_online_mems(); + guard(online_mems)(); ret = build_tdx_memlist(&tdx_memlist); if (ret) - goto out_put_tdxmem; + return ret; /* Allocate enough space for constructing TDMRs */ ret = alloc_tdmr_list(&tdx_tdmr_list, &tdx_sysinfo.tdmr); @@ -1253,13 +1253,7 @@ static int init_tdx_module(void) tdx_subsys_init(); -out_put_tdxmem: - /* - * @tdx_memlist is written here and read at memory hotplug time. - * Lock out memory hotplug code while building it. - */ - put_online_mems(); - return ret; + return 0; err_reset_pamts: /* @@ -1283,7 +1277,7 @@ static int init_tdx_module(void) free_tdmr_list(&tdx_tdmr_list); err_free_tdxmem: free_tdx_memlist(&tdx_memlist); - goto out_put_tdxmem; + return ret; } static int __tdx_enable(void) diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index eaac5ae8c05c..a0c0535a9122 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -2,6 +2,7 @@ #ifndef __LINUX_MEMORY_HOTPLUG_H #define __LINUX_MEMORY_HOTPLUG_H +#include <linux/cleanup.h> #include <linux/mmzone.h> #include <linux/spinlock.h> #include <linux/notifier.h> @@ -172,6 +173,7 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages, void get_online_mems(void); void put_online_mems(void); +DEFINE_LOCK_GUARD_0(online_mems, get_online_mems(), put_online_mems()) void mem_hotplug_begin(void); void mem_hotplug_done(void);