xfrm: Serialize device state teardown
Commit 89fefad9f971 ("xfrm: serialize state GC with device state
flush") stopped the state GC and xfrm_dev_state_flush() from freeing
the same device state at once, but a third caller is not covered.
xfrm_state_find() frees a packet offload acquire state when km_query()
fails. It runs under RCU and can't take the mutex.
xfrm_dev_state_delete() first puts the state on xfrm_state_dev_gc_list
with xso->dev still set, so a device flush can find it there and call
xfrm_dev_state_free() too. Both callers see xso->dev set and both
unlink the state, so the second hlist_del() hits a poisoned node.
Claim the device in xfrm_dev_state_free() by clearing xso->dev under
x->lock and xfrm_state_dev_gc_lock, so only one caller unlinks the
state, invokes xdo_dev_state_free() and drops the netdevice reference.
x->lock also keeps xso->dev stable for the stats updates that hold it.
In the flush, take a state reference before removing a state from the
device GC list, so it stays alive while the flush owns it. Skip a
state with no reference left, the state GC frees it.
Keep xso->type unchanged after detaching the device, so lockless
readers of xso->type do not see a packet offload state as a software
state. Reject offloaded states with no device in
xfrm_dev_offload_ok(): a packet offload state would otherwise pass
the !dev check there, and xfrm_output() would send the packet with no
ESP or use the NULL device in xfrm_dev_direct_output().
Found by the AI review of the commit above (see Link) and reproduced
on ConnectX-7 with a packet offload policy that has no SA, an acquire
listener that never reads, UDP senders hitting the policy and the
device taken down in a loop:
[ 167.689163] Oops: general protection fault, probably for non-canonical address 0xdead000000000122: 0000 [#1] SMP
[ 167.690938] CPU: 11 UID: 0 PID: 2093 Comm: python3 Not tainted 7.4.0-rc1+ #10 PREEMPTLAZY
[ 167.694337] RIP: 0010:xfrm_dev_state_free+0x41/0xe0
[ 167.699068] RAX: dead000000000122 RBX: ff1100014f543800 RCX: ff11000103bbf800
[ 167.700247] RDX: dead000000000100 RSI: 0000000000000001 RDI: ffffffff82da8c68
[ 167.716897] Call Trace:
[ 167.717405] <TASK>
[ 167.717863] xfrm_state_find+0x19b0/0x1ab0
[ 167.718629] xfrm_resolve_and_create_bundle+0x17d/0x11e0
[ 167.722070] xfrm_lookup_with_ifid+0x1e7/0xb50
[ 167.722872] xfrm_lookup_route+0x1d/0xa0
[ 167.723593] udp_sendmsg+0x8da/0xc90
[ 167.724273] __sock_sendmsg+0x38/0x70
[ 167.724988] __sys_sendto+0x119/0x180
[ 167.727954] __x64_sys_sendto+0x20/0x30
[ 167.728666] do_syscall_64+0x72/0x3b0
[ 167.729357] entry_SYSCALL_64_after_hwframe+0x4b/0x53
[ 167.741064] </TASK>
[ 167.752388] Kernel panic - not syncing: Fatal exception in interrupt
With this series the same test ran 400 cycles with no crash, and 150
cycles on a kernel with KASAN and lockdep.
Fixes: 07b87f9eea0c ("xfrm: Fix unregister netdevice hang on hardware offload.")
Link: https://lore.kernel.org/netdev/20260908224854.1591115-1-kuba@kernel.org/
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
2 files changed