🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #8476

Re: [PATCH ipsec] xfrm: keep the mode module pinned until destroy_state returns

#8476
From: Leon Romanovsky
Date: Thu, 29 Oct 2026 06:46
145 lines
14215 bytes
On Thu, Oct 29, 2026 at 11:19:11AM +0800, Cen Zhang wrote:
> An IPTFS state keeps its mode module referenced while mode_data exists.
> However, iptfs_destroy_state() drops that reference before returning to
> xfrm_state_gc_destroy(), which has no reference covering the callback's
> return path.

This is a good example of why module dependencies should be handled through
direct calls to functions exported with EXPORT_SYMBOL(), rather than through
callbacks for inter-module communication.

Thanks

> 
> With CONFIG_XFRM_IPTFS=m and module unloading enabled, deleting the last
> IPTFS SA can overlap with ordinary module removal in this order:
> 
>   XFRM GC worker                      rmmod xfrm_iptfs
>   iptfs_destroy_state()
>     free mode_data
>     module_put(owner)
>     <preempted>
>                                       delete_module()
>                                         xfrm_iptfs_fini()
>                                           unregister mode callbacks
>                                         free_module()
>     resume in freed module text
> 
> An unload started after the put can finish before the callback resumes.
> GC invokes the callback outside an RCU read-side critical section, after
> its grace period. The RCU waits in mode unregistration and module
> freeing therefore do not protect the callback. The GC mutex only
> serializes GC with device state flushes. Resuming the callback can cause
> an instruction-fetch fault in the freed module text.
> 
> The Oops below was observed on an earlier kernel with scheduling probes
> holding the worker in module_put() after the decrement until ordinary
> rmmod completed. The fixture created and deleted the SA over
> NETLINK_XFRM.
> 
> Take an additional reference to the already pinned owner before calling
> destroy_state(), and release it in XFRM core after the callback returns.
> Cache the owner before the call so releasing the extra reference does
> not need to access the callback table after the callback has finished.
> 
> Oops report as below:
>     BUG: unable to handle page fault for address: ffffffffc0407455
>     #PF: supervisor instruction fetch in kernel mode
>     #PF: error_code(0x0010) - not-present page
>     PGD 80a9067 P4D 80a9067 PUD 80ab067 PMD 0 
>     Oops: Oops: 0010 [#1] SMP KASAN NOPTI
>     CPU: 3 UID: 0 PID: 53 Comm: kworker/3:1 Not tainted 7.2.0-rc5-pmb-xfrm-functional-v1+ #11 PREEMPT(lazy) 
>     Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
>     Workqueue: events xfrm_state_gc_task
>     RIP: 0010:0xffffffffc0407455
>     Code: Unable to access opcode bytes at 0xffffffffc040742b.
>     RSP: 0018:ffff8881013e7aa0 EFLAGS: 00010282
>     RAX: 0000000000000000 RBX: ffffffffc02199c0 RCX: ffffffff816fc84e
>     RDX: ffff8881013c0000 RSI: 0000000000000000 RDI: ffffffff86338344
>     RBP: ffff8881013e7ba8 R08: 0000000000000001 R09: 0000000000000001
>     R10: ffffffff893e0257 R11: ffff8881013c0000 R12: ffff888107e9ec00
>     R13: ffff888102b07980 R14: ffff888107412ec0 R15: ffff8881013e7ae0
>     FS:  0000000000000000(0000) GS:ffff8881fd929000(0000) knlGS:0000000000000000
>     CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>     CR2: ffffffffc040742b CR3: 00000000080a6006 CR4: 0000000000770ef0
>     PKRU: 55555554
>     Call Trace:
>      <TASK>
>      ? pmbd_gate_maybe+0x47/0x60
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? lock_is_held_type+0x8f/0x100
>      xfrm_state_gc_task+0x17e/0x790
>      ? lock_acquire+0x17b/0x2f0
>      ? __pfx_xfrm_state_gc_task+0x10/0x10
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? lock_release+0xc8/0x280
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      process_one_work+0x908/0x19c0
>      ? __pfx_process_one_work+0x10/0x10
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? lock_is_held_type+0x8f/0x100
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      worker_thread+0x65c/0xe40
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? __kthread_parkme+0x175/0x220
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? __pfx_worker_thread+0x10/0x10
>      kthread+0x34f/0x460
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? __pfx_kthread+0x10/0x10
>      ret_from_fork+0x659/0x940
>      ? __pfx_ret_from_fork+0x10/0x10
>      ? srso_alias_return_thunk+0x5/0xfbef5
>      ? __switch_to+0x74f/0xf80
>      ? __pfx_kthread+0x10/0x10
>      ret_from_fork_asm+0x1a/0x30
>      </TASK>
>     Modules linked in: [last unloaded: xfrm_iptfs]
>     CR2: ffffffffc0407455
>     ---[ end trace 0000000000000000 ]---
>     RIP: 0010:0xffffffffc0407455
>     Code: Unable to access opcode bytes at 0xffffffffc040742b.
>     RSP: 0018:ffff8881013e7aa0 EFLAGS: 00010282
>     RAX: 0000000000000000 RBX: ffffffffc02199c0 RCX: ffffffff816fc84e
>     RDX: ffff8881013c0000 RSI: 0000000000000000 RDI: ffffffff86338344
>     RBP: ffff8881013e7ba8 R08: 0000000000000001 R09: 0000000000000001
>     R10: ffffffff893e0257 R11: ffff8881013c0000 R12: ffff888107e9ec00
>     R13: ffff888102b07980 R14: ffff888107412ec0 R15: ffff8881013e7ae0
>     FS:  0000000000000000(0000) GS:ffff8881fd929000(0000) knlGS:0000000000000000
>     CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>     CR2: ffffffffc040742b CR3: 00000000080a6006 CR4: 0000000000770ef0
>     PKRU: 55555554
>     Kernel panic - not syncing: Fatal exception
>     Kernel Offset: disabled
> 
> Fixes: 4b3faf610cc6 ("xfrm: iptfs: add new iptfs xfrm mode impl")
> Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
> Assisted-by: LLM
> 
> ---
>  net/xfrm/xfrm_state.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> index 0555e5d796ed..45a52771796b 100644
> --- a/net/xfrm/xfrm_state.c
> +++ b/net/xfrm/xfrm_state.c
> @@ -596,8 +596,14 @@ EXPORT_SYMBOL(xfrm_state_free);
>  static void xfrm_state_delete_tunnel(struct xfrm_state *x);
>  static void xfrm_state_gc_destroy(struct xfrm_state *x)
>  {
> -	if (x->mode_cbs && x->mode_cbs->destroy_state)
> +	if (x->mode_cbs && x->mode_cbs->destroy_state) {
> +		struct module *owner = x->mode_cbs->owner;
> +
> +		/* The callback may drop the last module reference. */
> +		__module_get(owner);
>  		x->mode_cbs->destroy_state(x);
> +		module_put(owner);
> +	}
>  	hrtimer_cancel(&x->mtimer);
>  	timer_delete_sync(&x->rtimer);
>  	kfree_sensitive(x->aead);
> 
Technical Details
Message-ID: <20261029064628.GC28851@unreal>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-353181-linux-kernel-netdev=dewy.ru
References: <pm-xfrm-core-objects-candidate-0003-v3-3f3e13b81234df0bb10f@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-353181-linux-kernel-netdev=dewy.ru
From: leon@kernel.org (Leon Romanovsky)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH ipsec] xfrm: keep the mode module pinned until
 destroy_state returns
Date: Thu, 29 Oct 2026 08:46:28 +0200
Organization: Usenet Network
Message-ID: <20261029064628.GC28851@unreal>
References: <pm-xfrm-core-objects-candidate-0003-v3-3f3e13b81234df0bb10f@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: usenet.network;
	logging-data="1952047"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:6Sbfr/9/yeFxjkRw8DmDb4I+GN8= sha256:JBvHwChdEKDCr7FQITxOprkGbwnTjBOFE2Ng3r6xrq4=
	sha1:yevizH9cCu0UoQvBZyvmqg5kQjo= sha256:8en3sFP92tbD9v+WRdnCBpQQTL5MO9rHwi4qcpz6J7c=
Content-Disposition: inline
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 411A6125C59; Thu, 29 Oct 2026 09:46:53 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 5PviFEPf33YU;
	Thu, 29 Oct 2026 09:46:46 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-353181-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id E796C125C58; Thu, 29 Oct 2026 09:46:37 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 2BFBD28A4B; Thu, 29 Oct 2026 06:46:35 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793256394;
	b=E1K5o7yCF8QYUuV/U54TRinhZsGv9mKt94e/8mniHLouIFfoZt4PxqB9EC66v6eZhmwU
	 firzpfWVgHGBgmOFGPNrVuc4NMw0XOjQPzh9AkIHFM1/WoCVoiO0SaZxmR2jdHHZEfFtZ
	 uLea5q1gqUd00nARJ6R8/WbFkyqcyN1mGoQaXDVOUXjGvgz6qdIbzLISNo1vVfvC/cd25
	 kVXysT0uNmIT3MS/54GLUYqw8EX+tCVlAyQyIJDJu8vcJOIZpaPe7ftJ7h3hA6agUuYSQ
	 3MXYtGxFsObu1JU/UqubjT7YnAW8RQBciR3mLgLlSWQOg3RDobe1+jV4nhex7MvD32Q==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793256394;
	h=X-Original-To:Received:Received:DKIM-Signature:Date:From:To:Cc:
	 Subject:Message-ID:References:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Type:Content-Disposition:In-Reply-To;
	bh=EjVXytVtPdyf8nmMgK3CDP18MdmAEUR+jL88CL1mcLQ=;
	b=l3vziNMZHaZDJgLa/HjChZKI6G08/le+ysMOwAoK4mM3LxoCl4x77p2RIvzhMwb2Gvk8
	 Gg/98Q9V7WfKKfoyYtr+pLWmm1m6VUZnFqmRFVHIrs/Y0eQxwbGD7sBAozXU6aHE7CXCq
	 GpXYyMPAf3EjnCEkV+bkth9c1K3nqh0I75jSr1O/Hybdn6crDNf68NPbg7uPrg2p5C4IC
	 wvPGgW2yBP0hot9zKyoSimoE1tIJvsSCtKJAFZhnNItGdyhDrJ339TS4bkitzHkV4Aowr
	 dYlNuKc/PS949/bJRuHrehRLCapKF9GxJvSmCsYlvLStaiWvHQYNZKF3rR3xaMOMpvQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=j+KHWZ+d;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id D5F02606A321;
	Thu, 29 Oct 2026 06:46:34 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=j+KHWZ+d
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793256394;
	b=b4SrlDke1VjAjOlg1sc6rCYBwQtbns+s1yi4O2JSHip44ZgFwWzouE5fsk55n2MYBlmZ
	 2v4Grbt/jcIajGCJtGQ+P3ugcIaFohWWr1s1c1DDfyTur5ly6effM3RS66R5xS2c8pbM7
	 YTHJx2XVVIQpoMxC5xLSvaMFDdbgQpcgC7cpHWNxLaOPMF+kDWjgXvJ44/tG/PSCmvVxr
	 kF9CH+0SC5WBMtp8o7b541X0xCHvjmHaANFQHzpAvWO1cWfMns+tddlC1dCmpIT1URiLy
	 wnnOaOaqn5DFxaGvoRUhgRiG3ztQaMFvuXPWa8k7QnV33JMqj5vR5QcHKJm119quxqg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793256394;
	h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID:
	 References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To;
	bh=EjVXytVtPdyf8nmMgK3CDP18MdmAEUR+jL88CL1mcLQ=;
	b=FxFFV5XfZnT7A0iOodN+sKKFxyGFpfUUbXs8esRw/kRFimWscuDVX06w2dGDKp1/ZU6z
	 rRJRpEGnGJHNeK0tq218Z7vATCb4vmO5DIoP15nwq1wIdJs32lpbFRYYS9bCxEuq9jdRH
	 dQO4atlIDxBHIcQ2UDscmlZu96dV2ooS7RElTartfW+NKm/DvdGypdnQPuIf+nuA1LK6r
	 F3YOTT5Vpo9n4pUuzY1XvKF6y7261qRHA7/Ggz/+h0/6m9c7gLOfyuwTTCLcI7ZT7REr6
	 zXr8eRBPHX+jmtWJ5LW9DrD1tH2NUffG672/xpCpH/ZR95PAzRc6eDw4b1a6Ti4qz3w==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=j+KHWZ+d;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFFFC606A31A
	for <netdev@vger.kernel.org>; Thu, 29 Oct 2026 06:46:33 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 610451F01825;
	Thu, 29 Oct 2026 06:46:32 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1793256393;
	bh=EjVXytVtPdyf8nmMgK3CDP18MdmAEUR+jL88CL1mcLQ=;
	h=Date:From:To:Cc:Subject:References:In-Reply-To;
	b=j+KHWZ+dYHKbzmQLQoOsnam6P/45DXkAIfUmahGbrZ7lh+NQHEWMqBIBmt/VJSYXo
	 0XI4JfUWyuqBTHT6XPBn9i8kTpkvCbN2lIU8R/mMGKnvWroboRKXOADjIzE3npafy9
	 KPxsAbNd/8lHHyXdk2mTRp3N3XlqP6z/mePdUVQ6iI0qrMbm7NJxUKXWJWUpygTjzC
	 fQBZ/j55lgT94Jv7KwXTMNeaPxF0G2huqzbhTazcW4Ksbj/Byr5rX4kTtl6Uga5ygN
	 Mb0FbDsUzd+6bV4nn6yfE8G4mkTSmxjewesGILCsbcRrU146jDetSeH6J0I3LmhMoW
	 GeW+OYTc3jprw==
X-Cc: steffen.klassert@secunet.com, herbert@gondor.apana.org.au,
	davem@davemloft.net, netdev@vger.kernel.org, baijiaju1990@gmail.com,
	jjzuming@gmail.com
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <pm-xfrm-core-objects-candidate-0003-v3-3f3e13b81234df0bb10f@gmail.com>