[PATCH net] xfrm: do not move state_cache_input across CPUs without RCU grace period
From: Bilal Hashim
Date: Mon Nov 30 2026 - 03:53:43 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Bilal Hashim <AeeSec@xxxxxxx>
---
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);
}