From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FD2E29AAFA for ; Tue, 16 Sep 2025 06:47:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758005240; cv=none; b=Ev8olboUvGAKPCvqnhqK4wsElIjFPdklYmLl6+bWS6VDvo7V6SKm3akHQ7FpDXj8IAu5IJP7AIRw6qwyLRXDS22HumXgDv3TMZoK770l/8Zq05Y/XtxEj4v2wcLmPb5lUcrPIxh9OJJJXFTax6+jAcsAqTa2qd7cdaY3u+IUmho= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758005240; c=relaxed/simple; bh=ps8U25sao5CXG0tpqB93fhXdpxfVN3zjkKs2BTxHOsA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ebTGv5ZYhVxdwK8yq/VS2iZo4/T/t5vLEx7dav9oxMGwZTVqvw/gYOcjSN90fqtzP2udK7UvXeiCXOdmcMIda7nAbJ3IWTauVVwHNBrEF540ZcLu9hXpwr5Gft83xZzqbS8iI+NNhBOwfjjQK6sl8xycUYskfyZyh01mogzMx5g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=uSs5fQAM; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="uSs5fQAM" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1758005235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dD7fKg3IFj16bfftiC1LbdPu+LDhhXsLvJU8rsm5XSk=; b=uSs5fQAMD6jRLf7AwPF/F6acW98nWEE6en9yFE0GiL0+htaXHMd0eEQrJs5f+pcEDVluN3 nY1AXNQZkPIR34NxeBdRkv/T35V+Y7QFlNB9KW/X9g/XKw1S4dEYYGwMqJNyIvNaLbA7P9 JsP/w4Sb6OKrTcN2GiHkWFG6C2DOS9E= From: xuanqiang.luo@linux.dev To: edumazet@google.com, kuniyu@google.com Cc: kerneljasonxing@gmail.com, davem@davemloft.net, kuba@kernel.org, netdev@vger.kernel.org, Xuanqiang Luo Subject: [PATCH net-next v2 3/3] inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule() Date: Tue, 16 Sep 2025 14:46:14 +0800 Message-Id: <20250916064614.605075-4-xuanqiang.luo@linux.dev> In-Reply-To: <20250916064614.605075-1-xuanqiang.luo@linux.dev> References: <20250916064614.605075-1-xuanqiang.luo@linux.dev> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: Xuanqiang Luo Since ehash lookups are lockless, if another CPU is converting sk to tw concurrently, fetching the newly inserted tw with tw->tw_refcnt == 0 cause lookup failure. The call trace map is drawn as follows: CPU 0 CPU 1 ----- ----- inet_twsk_hashdance_schedule() spin_lock() inet_twsk_add_node_rcu(tw, ...) __inet_lookup_established() (find tw, failure due to tw_refcnt = 0) __sk_nulls_del_node_init_rcu(sk) refcount_set(&tw->tw_refcnt, 3) spin_unlock() By replacing sk with tw atomically via hlist_nulls_replace_init_rcu() after setting tw_refcnt, we ensure that tw is either fully initialized or not visible to other CPUs, eliminating the race. Fixes: 3ab5aee7fe84 ("net: Convert TCP & DCCP hash tables to use RCU / hlist_nulls") Signed-off-by: Xuanqiang Luo --- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c index 5b5426b8ee92..1ba20c4cb73b 100644 --- a/net/ipv4/inet_timewait_sock.c +++ b/net/ipv4/inet_timewait_sock.c @@ -116,7 +116,7 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); struct inet_bind_hashbucket *bhead, *bhead2; - /* Step 1: Put TW into bind hash. Original socket stays there too. + /* Put TW into bind hash. Original socket stays there too. Note, that any socket with inet->num != 0 MUST be bound in binding cache, even if it is closed. */ @@ -140,14 +140,6 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, spin_lock(lock); - /* Step 2: Hash TW into tcp ehash chain */ - inet_twsk_add_node_rcu(tw, &ehead->chain); - - /* Step 3: Remove SK from hash chain */ - if (__sk_nulls_del_node_init_rcu(sk)) - sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); - - /* Ensure above writes are committed into memory before updating the * refcount. * Provides ordering vs later refcount_inc(). @@ -162,6 +154,11 @@ void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, */ refcount_set(&tw->tw_refcnt, 3); + if (hlist_nulls_replace_init_rcu(&sk->sk_nulls_node, &tw->tw_node)) + sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); + else + inet_twsk_add_node_rcu(tw, &ehead->chain); + inet_twsk_schedule(tw, timeo); spin_unlock(lock); -- 2.25.1