From nobody Thu Sep 19 01:41:19 2024 Received: from out-187.mta0.migadu.com (out-187.mta0.migadu.com [91.218.175.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34C4814BF97 for ; Tue, 23 Jul 2024 08:33:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721723588; cv=none; b=hWBN3HafcPOjTlCm/J96PkOs8OMyV14y7KU7plBVWE8yWpbx9e9BDwif7UbN517Gcp2WeGue5bWfa2RlTumxrtPBIhfXj2NlJZG8v4bkE1RmHwxSjv2tiNxSd7IESBiGCGxZD9ReqxATq0uBg82AvE0QK7Yx+OUPLcQWZsvRmkg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721723588; c=relaxed/simple; bh=16C3qOghK8TaOTax2+QAysgUWXWtYbusUjh2y2kiDPA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oRyTkQsemyJ+b1VbDhsWYabVIQNG2EJkQ9bKyM+rvLq6vYai62ULyrIe4bpXvU+LFgV3IcWiPO9IZH3sDy1I1WT/at/rYdp6LE2gEJr1wvaooah14VF3a1KrTEewduFKeoddtelwIdC4B6LUD0Dp1tUT/m+eU65RSMVyWsSuMa4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=cQx+gVTP; arc=none smtp.client-ip=91.218.175.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="cQx+gVTP" X-Envelope-To: arnd@arndb.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1721723584; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3VCZOZWRmGd8xL5SN+J0EY1zI5GAsbkGIgLsBKI32rY=; b=cQx+gVTPFpYaKvaHYXeReUXRhKDPTWx/sb5DeYOwR8gRsNRrwJFY99FHH1UzFuoA6giSAU /S7ZkQOGJ63xrCh96+rIE9S1wWbRPni1uyscrvGEAXhmVxohVvd7OpcuqyO8XTXotjZH2w UFvUkVqKYobrZYhSNeojetonj1chNhg= X-Envelope-To: mcgrof@kernel.org X-Envelope-To: clm@fb.com X-Envelope-To: josef@toxicpanda.com X-Envelope-To: dsterba@suse.com X-Envelope-To: tytso@mit.edu X-Envelope-To: adilger.kernel@dilger.ca X-Envelope-To: jaegeuk@kernel.org X-Envelope-To: chao@kernel.org X-Envelope-To: hch@infradead.org X-Envelope-To: linux-arch@vger.kernel.org X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: linux-modules@vger.kernel.org X-Envelope-To: linux-btrfs@vger.kernel.org X-Envelope-To: linux-ext4@vger.kernel.org X-Envelope-To: linux-f2fs-devel@lists.sourceforge.net X-Envelope-To: youling.tang@linux.dev X-Envelope-To: tangyouling@kylinos.cn X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Youling Tang To: Arnd Bergmann , Luis Chamberlain , Chris Mason , Josef Bacik , David Sterba , tytso@mit.edu, Andreas Dilger , Jaegeuk Kim , Chao Yu , Christoph Hellwig Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, youling.tang@linux.dev, Youling Tang Subject: [PATCH 1/4] module: Add module_subinit{_noexit} and module_subeixt helper macros Date: Tue, 23 Jul 2024 16:32:36 +0800 Message-Id: <20240723083239.41533-2-youling.tang@linux.dev> In-Reply-To: <20240723083239.41533-1-youling.tang@linux.dev> References: <20240723083239.41533-1-youling.tang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Youling Tang In theory init/exit should match their sequence, thus normally they should look like this: -------------------------+------------------------ init_A(); | init_B(); | init_C(); | | exit_C(); | exit_B(); | exit_A(); Providing module_subinit{_noexit} and module_subeixt helps macros ensure that modules init/exit match their order, while also simplifying the code. The three macros are defined as follows: - module_subinit(initfn, exitfn,rollback) - module_subinit_noexit(initfn, rollback) - module_subexit(rollback) `initfn` is the initialization function and `exitfn` is the corresponding exit function. Signed-off-by: Youling Tang --- include/asm-generic/vmlinux.lds.h | 5 +++ include/linux/init.h | 62 ++++++++++++++++++++++++++++++- include/linux/module.h | 22 +++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index 677315e51e54..48ccac7c6448 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -927,6 +927,10 @@ INIT_CALLS_LEVEL(7) \ __initcall_end =3D .; =20 +#define SUBINIT_CALL \ + *(.subinitcall.init) \ + *(.subexitcall.exit) + #define CON_INITCALL \ BOUNDED_SECTION_POST_LABEL(.con_initcall.init, __con_initcall, _start, _e= nd) =20 @@ -1155,6 +1159,7 @@ INIT_DATA \ INIT_SETUP(initsetup_align) \ INIT_CALLS \ + SUBINIT_CALL \ CON_INITCALL \ INIT_RAM_FS \ } diff --git a/include/linux/init.h b/include/linux/init.h index ee1309473bc6..e8689ff2cb6c 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -55,6 +55,9 @@ #define __exitdata __section(".exit.data") #define __exit_call __used __section(".exitcall.exit") =20 +#define __subinit_call __used __section(".subinitcall.init") +#define __subexit_call __used __section(".subexitcall.exit") + /* * modpost check for section mismatches during the kernel build. * A section mismatch happens when there are references from a @@ -115,6 +118,9 @@ typedef int (*initcall_t)(void); typedef void (*exitcall_t)(void); =20 +typedef int (*subinitcall_t)(void); +typedef void (*subexitcall_t)(void); + #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS typedef int initcall_entry_t; =20 @@ -183,7 +189,61 @@ extern struct module __this_module; #endif =20 #endif - =20 + +#ifndef __ASSEMBLY__ +struct subexitcall_rollback { + /* + * Records the address of the first sub-initialization function in the + * ".subexitcall.exit" section + */ + unsigned long first_addr; + int ncalls; +}; + +static inline void __subexitcall_rollback(struct subexitcall_rollback *r) +{ + unsigned long addr =3D r->first_addr - sizeof(r->first_addr) * (r->ncalls= - 1); + + for (; r->ncalls--; addr +=3D sizeof(r->first_addr)) { + unsigned long *tmp =3D (void *)addr; + subexitcall_t fn =3D (subexitcall_t)*tmp; + fn(); + } +} + +static inline void set_rollback_ncalls(struct subexitcall_rollback *r) +{ + r->ncalls++; +} + +static inline void set_rollback_first_addr(struct subexitcall_rollback *ro= llback, + unsigned long addr) +{ + if (!rollback->first_addr) + rollback->first_addr =3D addr; +} + +#define __subinitcall_noexit(initfn, rollback) \ +do { \ + static subinitcall_t __subinitcall_##initfn __subinit_call =3D initfn; \ + int _ret; \ + _ret =3D initfn(); \ + if (_ret < 0) { \ + __subexitcall_rollback(rollback); \ + return _ret; \ + } \ +} while (0) + +#define __subinitcall(initfn, exitfn, rollback) \ +do { \ + static subexitcall_t __subexitcall_##exitfn __subexit_call =3D exitfn; \ + set_rollback_first_addr(rollback, (unsigned long)&__subexitcall_##exitfn)= ; \ + __subinitcall_noexit(initfn, rollback); \ + set_rollback_ncalls(rollback); \ +} while (0) + +#endif /* !__ASSEMBLY__ */ + #ifndef MODULE =20 #ifndef __ASSEMBLY__ diff --git a/include/linux/module.h b/include/linux/module.h index 4213d8993cd8..95f7c60dede9 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -76,6 +76,28 @@ extern struct module_attribute module_uevent; extern int init_module(void); extern void cleanup_module(void); =20 +/* + * module_subinit() - Called when the driver is subinitialized + * @initfn: The subinitialization function that is called + * @exitfn: Corresponding exit function + * @rollback: Record information when the subinitialization failed + * or the driver was removed + * + * Use module_subinit_noexit() when there is only an subinitialization + * function but no corresponding exit function. + */ +#define module_subinit(initfn, exitfn, rollback) \ + __subinitcall(initfn, exitfn, rollback); + +#define module_subinit_noexit(initfn, rollback) \ + __subinitcall_noexit(initfn, rollback); + +/* + * module_subexit() - Called when the driver exits + */ +#define module_subexit(rollback) \ + __subexitcall_rollback(rollback); + #ifndef MODULE /** * module_init() - driver initialization entry point --=20 2.34.1