Netdev List
 help / color / mirror / Atom feed
* [PATCH net] xfrm: do not move state_cache_input across CPUs without RCU grace period
@ 2026-11-30  8:52 Bilal Hashim
  2026-11-30  8:59 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Bilal Hashim @ 2026-11-30  8:52 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel

In xfrm_input_state_lookup(), each CPU traverses its per-CPU
net->xfrm.state_cache_input hlist locklessly under RCU via
hlist_for_each_entry_rcu().

When a lookup misses on one CPU and finds an xfrm_state that is already
cached on another CPU's state_cache_input list, xfrm_input_state_lookup()
acquires xfrm_state_lock and immediately executes:

    hlist_del_rcu(&x->state_cache_input);
    hlist_add_head_rcu(&x->state_cache_input, state_cache_input);

Re-inserting an hlist_node right after hlist_del_rcu() without waiting
for an RCU grace period overwrites x->state_cache_input.next while
concurrent softirq RCU readers on the previous CPU may still be
traversing that list. This can cause readers to jump onto another CPU's
list, miss valid states, or loop indefinitely in NET_RX_SOFTIRQ (RCU CPU
stall / soft lockup) when two states bounce between per-CPU lists.

Since xfrm_input_state_lookup() runs in softirq context and cannot wait
for an RCU grace period, match the outbound state_cache behavior and only
insert the state into state_cache_input if it is currently unhashed.
Also use hlist_unhashed_lockless() as a fast pre-check to avoid taking
the global xfrm_state_lock on cross-CPU cache misses.

Found by manual code inspection.

Fixes: 81a331a0e72d ("xfrm: Add an inbound percpu state cache.")
Cc: stable@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
 net/xfrm/xfrm_state.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -1253,21 +1253,11 @@ struct xfrm_state *xfrm_input_state_look
 	xfrm_hash_ptrs_get(net, &state_ptrs);
 
 	x = __xfrm_state_lookup(&state_ptrs, mark, 0, false, daddr, spi, proto, family);
-	if (x) {
+	if (x && hlist_unhashed_lockless(&x->state_cache_input)) {
 		spin_lock(&net->xfrm.xfrm_state_lock);
-		if (x->km.state != XFRM_STATE_VALID) {
-			/*
-			 * The state is about to be destroyed.
-			 *
-			 * Don't add it to the cache but still
-			 * return it to the caller.
-			 */
-		} else if (hlist_unhashed(&x->state_cache_input)) {
+		if (x->km.state == XFRM_STATE_VALID &&
+		    hlist_unhashed(&x->state_cache_input))
 			hlist_add_head_rcu(&x->state_cache_input, state_cache_input);
-		} else {
-			hlist_del_rcu(&x->state_cache_input);
-			hlist_add_head_rcu(&x->state_cache_input, state_cache_input);
-		}
 		spin_unlock(&net->xfrm.xfrm_state_lock);
 	}
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  8:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  8:52 [PATCH net] xfrm: do not move state_cache_input across CPUs without RCU grace period Bilal Hashim
2026-11-30  8:59 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox