Netdev List
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Steffen Klassert <steffen.klassert@secunet.com>,
	Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net] xfrm: do not move state_cache_input across CPUs without RCU grace period
Date: Mon, 30 Nov 2026 16:52:10 +0800	[thread overview]
Message-ID: <179602873084.13688.6020955771339996047@126.com> (raw)

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);
 	}
 


             reply	other threads:[~2026-11-30  8:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:52 Bilal Hashim [this message]
2026-11-30  8:59 ` [PATCH net] xfrm: do not move state_cache_input across CPUs without RCU grace period netdev-bot+sinfo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179602873084.13688.6020955771339996047@126.com \
    --to=aeesec@126.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=steffen.klassert@secunet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox