net: Avoid ehash lookup races
Subsystems (5)
v3 cover net: Avoid ehash lookup races
From: Xuanqiang Luo <[email protected]>
After replacing R/W locks with RCU in commit 3ab5aee7fe84 ("net: Convert TCP & DCCP hash tables to use RCU / hlist_nulls"), a race window emerged during the switch from reqsk/sk to sk/tw.
Now that both timewait sock (tw) and full sock (sk) reside on the same ehash chain, it is appropriate to introduce hlist_nulls replace operations, to eliminate the race conditions caused by this window.
Before this series of patches, I previously sent another version of the patch, attempting to avoid the issue using a lock mechanism. However, it seems there are some problems with that approach now, so I've switched to the "replace" method in the current patches to resolve the issue. For details, refer to:
https://lore.kernel.org/netdev/[email protected]/
Before I encountered this type of issue recently, I found there had been several historical discussions about it. Therefore, I'm adding this background information for those interested to reference:
1. https://lore.kernel.org/lkml/[email protected]/
2. https://lore.kernel.org/netdev/[email protected]/
---
Changes:
v3:
* Add more background information on this type of issue to the letter cover.
v2: https://lore.kernel.org/all/[email protected]/
* Patch 1
* Use WRITE_ONCE() to initialize old->pprev.
* Patch 2&3
* Optimize sk hashed check. Thanks Kuni for pointing it out!
v1: https://lore.kernel.org/all/[email protected]/
Xuanqiang Luo (3):
rculist: Add __hlist_nulls_replace_rcu() and
hlist_nulls_replace_init_rcu()
inet: Avoid ehash lookup race in inet_ehash_insert()
inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ include/net/sock.h | 23 +++++++++++++ net/ipv4/inet_hashtables.c | 4 ++- net/ipv4/inet_timewait_sock.c | 15 ++++----- 4 files changed, 93 insertions(+), 10 deletions(-)
Signature · 1 line
2.25.1
PATCH v3 1/3 rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected hlist_nulls entries.
--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
include/linux/rculist_nulls.h +61 -0
@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +struct hlist_nulls_node *next = old->next; + + new->next = next; +WRITE_ONCE (new->pprev, old->pprev); +rcu_assign_pointer (*(struct hlist_nulls_node __rcu **)new->pprev, new); +if (!is_a_nulls (next)) +WRITE_ONCE (new->next->pprev, &new->next); +} + +/** +* hlist_nulls_replace_init_rcu - replace an old entry by a new one and +* initialize the old +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals, and reinitialize the old entry. +* +* Return: true if the old entry was hashed and was replaced successfully, false +* otherwise. +* +* Note: hlist_nulls_unhashed() on the old node returns true after this. +* It is useful for RCU based read lockfree traversal if the writer side must +* know if the list entry is still hashed or already unhashed. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline bool hlist_nulls_replace_init_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +if (!hlist_nulls_unhashed (old)) { +__hlist_nulls_replace_rcu (old, new); +WRITE_ONCE (old->pprev,NULL ); +return true; + } +return false; +} +/** * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type * @tpos: the type * to use as a loop cursor.
Signature · 1 line
2.25.1
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v3 1/3] rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
Quoted 30 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected
hlist_nullsentries.Signed-off-by Xuanqiang Luo <[email protected]>--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)include/linux/rculist_nulls.h +12
-0@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 176
Quoted 10 lines
include/linux/rculist_nulls.h +10
-0+* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +struct hlist_nulls_node *next = old->next; + + new->next = next; +WRITE_ONCE (new->pprev, old->pprev);
As you don't use WRITE_ONCE() for ->next, the new node must not be published yet, so WRITE_ONCE() is unnecessary for ->pprev too.
Quoted 22 lines
include/linux/rculist_nulls.h +22
-0+rcu_assign_pointer (*(struct hlist_nulls_node __rcu **)new->pprev, new); +if (!is_a_nulls (next)) +WRITE_ONCE (new->next->pprev, &new->next); +} + +/** +* hlist_nulls_replace_init_rcu - replace an old entry by a new one and +* initialize the old +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals, and reinitialize the old entry. +* +* Return: true if the old entry was hashed and was replaced successfully, false +* otherwise. +* +* Note: hlist_nulls_unhashed() on the old node returns true after this. +* It is useful for RCU based read lockfree traversal if the writer side must +* know if the list entry is still hashed or already unhashed. +*
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 208
Quoted 10 lines
include/linux/rculist_nulls.h +10
-0+* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline bool hlist_nulls_replace_init_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +if (!hlist_nulls_unhashed (old)) {
As mentioned in v1, this check is redundant.
Quoted 13 lines
include/linux/rculist_nulls.h +7
-1+__hlist_nulls_replace_rcu (old, new); +WRITE_ONCE (old->pprev,NULL ); +return true; + } +return false; +} +/** * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type * @tpos: the type * to use as a loop cursor. -- 2.25.1
Re: [PATCH net-next v3 1/3] rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
Quoted 31 lines · Kuniyuki Iwashima
Quoted 30 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected
hlist_nullsentries.Signed-off-by Xuanqiang Luo <[email protected]>--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)include/linux/rculist_nulls.h +13
-0@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu
Quoted 8 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 175
Quoted 8 lines
include/linux/rculist_nulls.h +8
-0+* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +struct hlist_nulls_node *next = old->next; + + new->next = next;
Do we need to use WRITE_ONCE() here, as mentioned in efd04f8a8b45 ("rcu: Use WRITE_ONCE() for assignments to ->next for rculist_nulls")? I am more inclined to think that it is necessary.
Quoted 4 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 176
Quoted 1 line
include/linux/rculist_nulls.h +1
-0+WRITE_ONCE (new->pprev, old->pprev);As you don't use
WRITE_ONCE()for ->next, the new node must not be published yet, soWRITE_ONCE()is unnecessary for ->pprev too.
I noticed that point. My understanding is that using WRITE_ONCE() for new->pprev follows the approach in hlist_replace_rcu() to match the READ_ONCE() in hlist_nulls_unhashed_lockless() and hlist_unhashed_lockless().
Quoted 26 lines
Quoted 26 lines
include/linux/rculist_nulls.h +25
-0+rcu_assign_pointer (*(struct hlist_nulls_node __rcu **)new->pprev, new); +if (!is_a_nulls (next)) +WRITE_ONCE (new->next->pprev, &new->next); +} + +/** +* hlist_nulls_replace_init_rcu - replace an old entry by a new one and +* initialize the old +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals, and reinitialize the old entry. +* +* Return: true if the old entry was hashed and was replaced successfully, false +* otherwise. +* +* Note: hlist_nulls_unhashed() on the old node returns true after this. +* It is useful for RCU based read lockfree traversal if the writer side must +* know if the list entry is still hashed or already unhashed. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same
Quoted 8 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 208
Quoted 7 lines
include/linux/rculist_nulls.h +7
-0+* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline bool hlist_nulls_replace_init_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +if (!hlist_nulls_unhashed (old)) {As mentioned in v1, this check is redundant.
Apologies for bringing this up again. My understanding is that replacing a node requires checking if the old node is unhashed.
If so, we need a return value to inform the caller that the replace operation would fail.
Quoted 14 lines
Quoted 14 lines
include/linux/rculist_nulls.h +7
-1+__hlist_nulls_replace_rcu (old, new); +WRITE_ONCE (old->pprev,NULL ); +return true; + } +return false; +} +/** * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type * @tpos: the type * to use as a loop cursor. -- 2.25.1
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v3 1/3] rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
Quoted 38 lines · luoxuanqiang
Quoted 35 lines · Kuniyuki Iwashima
Quoted 34 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected
hlist_nullsentries.Signed-off-by Xuanqiang Luo <[email protected]>--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)include/linux/rculist_nulls.h +17
-0@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new)
Quoted 8 lines
Quoted 4 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 175
Quoted 4 lines
include/linux/rculist_nulls.h +4
-0+{ +struct hlist_nulls_node *next = old->next; + + new->next = next;Do we need to use
WRITE_ONCE()here, as mentioned inefd04f8a8b45("rcu: UseWRITE_ONCE()for assignments to ->next forrculist_nulls")? I am more inclined to think that it is necessary.
Good point, then WRITE_ONCE() makes sense.
Quoted 10 lines
Quoted 5 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 176
Quoted 2 lines
include/linux/rculist_nulls.h +1
-0+WRITE_ONCE (new->pprev, old->pprev);As you don't use
WRITE_ONCE()for ->next, the new node must not be published yet, soWRITE_ONCE()is unnecessary for ->pprev too.I noticed that point. My understanding is that using
WRITE_ONCE()fornew->pprevfollows the approach inhlist_replace_rcu()to match theREAD_ONCE()inhlist_nulls_unhashed_lockless()andhlist_unhashed_lockless().
Using WRITE_ONCE() or READ_ONCE() implies lockless readers or writers elsewhere.
sk_hashed() does not use the lockless version, and I think it's always called under lock_sock() or bh_. Perhaps run kernel w/ KCSAN and see if it complains.
[ It seems hlist_nulls_unhashed_lockless is not used at all and
hlist_unhashed_lockless() is only used by bpf and timer code. ]
That said, it might be fair to use WRITE_ONCE() here to make future users less error-prone.
Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
include/linux/rculist_nulls.h +25
-0+rcu_assign_pointer (*(struct hlist_nulls_node __rcu **)new->pprev, new); +if (!is_a_nulls (next)) +WRITE_ONCE (new->next->pprev, &new->next); +} + +/** +* hlist_nulls_replace_init_rcu - replace an old entry by a new one and +* initialize the old +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals, and reinitialize the old entry. +* +* Return: true if the old entry was hashed and was replaced successfully, false +* otherwise. +* +* Note: hlist_nulls_unhashed() on the old node returns true after this. +* It is useful for RCU based read lockfree traversal if the writer side must +* know if the list entry is still hashed or already unhashed. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same Quoted 8 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 208
Quoted 7 lines
include/linux/rculist_nulls.h +7
-0+* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline bool hlist_nulls_replace_init_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +if (!hlist_nulls_unhashed (old)) {As mentioned in v1, this check is redundant.
Quoted 2 lines
Apologies for bringing this up again. My understanding is that replacing a node requires checking if the old node is unhashed.
Only if the caller does not check it.
__sk_nulls_replace_node_init_rcu() has already checked sk_hashed(old), which is !hlist_nulls_unhashed(old), no ?
__sk_nulls_replace_node_init_rcu(struct sock *old, ...)
if (sk_hashed(old))
hlist_nulls_replace_init_rcu(&old->sk_nulls_node, ...)
if (!hlist_nulls_unhashed(old))Quoted 18 lines
If so, we need a return value to inform the caller that the replace operation would fail.
Quoted 14 lines
Quoted 14 lines
include/linux/rculist_nulls.h +7
-1+__hlist_nulls_replace_rcu (old, new); +WRITE_ONCE (old->pprev,NULL ); +return true; + } +return false; +} +/** * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type * @tpos: the type * to use as a loop cursor. -- 2.25.1
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v3 1/3] rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
Quoted 49 lines · Kuniyuki Iwashima
Quoted 38 lines · luoxuanqiang
Quoted 35 lines · Kuniyuki Iwashima
Quoted 34 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected
hlist_nullsentries.Signed-off-by Xuanqiang Luo <[email protected]>--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)include/linux/rculist_nulls.h +17
-0@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new)Quoted 8 lines
Quoted 4 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 175
Quoted 4 lines
include/linux/rculist_nulls.h +4
-0+{ +struct hlist_nulls_node *next = old->next; + + new->next = next;Do we need to use
WRITE_ONCE()here, as mentioned inefd04f8a8b45("rcu: UseWRITE_ONCE()for assignments to ->next forrculist_nulls")? I am more inclined to think that it is necessary.
Quoted 1 line
Good point, then
WRITE_ONCE()makes sense.
and it would be nice to have such reasoning in the commit message as we were able to learn from efd04f8a8b45.
Re: [PATCH net-next v3 1/3] rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
Quoted 46 lines · Kuniyuki Iwashima
Quoted 45 lines · Kuniyuki Iwashima
Quoted 44 lines · luoxuanqiang
Quoted 31 lines · Kuniyuki Iwashima
Quoted 30 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected
hlist_nullsentries.Signed-off-by Xuanqiang Luo <[email protected]>--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)include/linux/rculist_nulls.h +13
-0@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu Quoted 8 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 175
Quoted 8 lines
include/linux/rculist_nulls.h +8
-0+* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +struct hlist_nulls_node *next = old->next; + + new->next = next;Do we need to use
WRITE_ONCE()here, as mentioned inefd04f8a8b45("rcu: UseWRITE_ONCE()for assignments to ->next forrculist_nulls")? I am more inclined to think that it is necessary.
Quoted 3 lines
Quoted 1 line
Good point, then
WRITE_ONCE()makes sense.and it would be nice to have such reasoning in the commit message as we were able to learn from
efd04f8a8b45.
Okay, I'll add an explanation in the next version. Really appreciate you confirming this!
Re: [PATCH net-next v3 1/3] rculist: Add __hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
In reply to Kuniyuki IwashimaQuoted 106 lines · Kuniyuki Iwashima
Quoted 36 lines · luoxuanqiang
Quoted 34 lines · Kuniyuki Iwashima
Quoted 33 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
Add two functions to atomically replace RCU-protected
hlist_nullsentries.Signed-off-by Xuanqiang Luo <[email protected]>--- include/linux/rculist_nulls.h | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)include/linux/rculist_nulls.h +16
-0@@ -152,6 +152,67 @@ static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) n->next = (struct hlist_nulls_node *)NULLS_MARKER (NULL ); } +/** +* __hlist_nulls_replace_rcu - replace an old entry by a new one +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same +* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline void __hlist_nulls_replace_rcu(struct hlist_nulls_node *old,Quoted 8 lines
Quoted 5 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 175
Quoted 5 lines
include/linux/rculist_nulls.h +5
-0+ struct hlist_nulls_node *new) +{ +struct hlist_nulls_node *next = old->next; + + new->next = next;Do we need to use
WRITE_ONCE()here, as mentioned inefd04f8a8b45("rcu: UseWRITE_ONCE()for assignments to ->next forrculist_nulls")? I am more inclined to think that it is necessary.Good point, then
WRITE_ONCE()makes sense.Quoted 8 lines
Quoted 4 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 176
Quoted 1 line
include/linux/rculist_nulls.h +1
-0+WRITE_ONCE (new->pprev, old->pprev);As you don't use
WRITE_ONCE()for ->next, the new node must not be published yet, soWRITE_ONCE()is unnecessary for ->pprev too.I noticed that point. My understanding is that using
WRITE_ONCE()fornew->pprevfollows the approach inhlist_replace_rcu()to match theREAD_ONCE()inhlist_nulls_unhashed_lockless()andhlist_unhashed_lockless().Using
WRITE_ONCE()orREAD_ONCE()implies lockless readers or writers elsewhere.
sk_hashed()does not use the lockless version, and I think it's always called underlock_sock()orbh_. Perhaps run kernel w/ KCSAN and see if it complains.[ It seems
hlist_nulls_unhashed_locklessis not used at all and
hlist_unhashed_lockless()is only used by bpf and timer code. ]That said, it might be fair to use
WRITE_ONCE()here to make future users less error-prone.Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
include/linux/rculist_nulls.h +25
-0+rcu_assign_pointer (*(struct hlist_nulls_node __rcu **)new->pprev, new); +if (!is_a_nulls (next)) +WRITE_ONCE (new->next->pprev, &new->next); +} + +/** +* hlist_nulls_replace_init_rcu - replace an old entry by a new one and +* initialize the old +* @old: the element to be replaced +* @new: the new element to insert +* +* Description: +* Replace the old entry with the new one in a RCU-protected hlist_nulls, while +* permitting racing traversals, and reinitialize the old entry. +* +* Return: true if the old entry was hashed and was replaced successfully, false +* otherwise. +* +* Note: hlist_nulls_unhashed() on the old node returns true after this. +* It is useful for RCU based read lockfree traversal if the writer side must +* know if the list entry is still hashed or already unhashed. +* +* The caller must take whatever precautions are necessary (such as holding +* appropriate locks) to avoid racing with another list-mutation primitive, such +* as hlist_nulls_add_head_rcu() or hlist_nulls_del_rcu(), running on this same Quoted 8 lines
include/linux/rculist_nulls.h static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n) · line 208
Quoted 7 lines
include/linux/rculist_nulls.h +7
-0+* list. However, it is perfectly legal to run concurrently with the _rcu +* list-traversal primitives, such as hlist_nulls_for_each_entry_rcu(). +*/ +static inline bool hlist_nulls_replace_init_rcu (struct hlist_nulls_node *old, +struct hlist_nulls_node *new) +{ +if (!hlist_nulls_unhashed (old)) {As mentioned in v1, this check is redundant.
Quoted 2 lines
Apologies for bringing this up again. My understanding is that replacing a node requires checking if the old node is unhashed.
Only if the caller does not check it.
Quoted 8 lines
__sk_nulls_replace_node_init_rcu()has already checkedsk_hashed(old), which is !hlist_nulls_unhashed(old), no ?
__sk_nulls_replace_node_init_rcu(struct sock *old, ...)
if (sk_hashed(old))hlist_nulls_replace_init_rcu(&old->sk_nulls_node, ...) if (!hlist_nulls_unhashed(old))
I understand that sk_hashed(old) is equivalent to
!hlist_nulls_unhashed(old). However,hlist_nulls_replace_init_rcu() is also used ininet_twsk_hashdance_schedule().
If it's confirmed that the unhashed check is
unnecessary in inet_twsk_hashdance_schedule()
(as discussed in https://lore.kernel.org/all/CAAVpQUBY=h3gDfaX=J9vbSuhYTn8cfCsBGhPLqoer0OSYdihDg@mail.gmail.com/),
then for this specific patchset, this redundant check
can indeed be removed.
But I'm concerned that others might later use hlist_nulls_replace_init_rcu() standalone, similar to how hlist_nulls_del_init_rcu() is used. This could cause confusion since replace might not always succeed. Given this, might retaining the hlist_nulls_unhashed(old) check be safer?
Really appreciate your patient review and suggestions!
Thanks
Xuanqiang.
Quoted 16 lines
Quoted 16 lines
If so, we need a return value to inform the caller that the replace operation would fail.
Quoted 13 lines
Quoted 13 lines
include/linux/rculist_nulls.h +7
-1+__hlist_nulls_replace_rcu (old, new); +WRITE_ONCE (old->pprev,NULL ); +return true; + } +return false; +} +/** * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type * @tpos: the type * to use as a loop cursor. -- 2.25.1
PATCH v3 2/3 inet: Avoid ehash lookup race in inet_ehash_insert()
In reply to [email protected]From: Xuanqiang Luo <[email protected]>
Since ehash lookups are lockless, if one CPU performs a lookup while another concurrently deletes and inserts (removing reqsk and inserting sk), the lookup may fail to find the socket, an RST may be sent.
The call trace map is drawn as follows:
CPU 0 CPU 1
----- -----
inet_ehash_insert()
spin_lock()
sk_nulls_del_node_init_rcu(osk)
__inet_lookup_established()
(lookup failed)
__sk_nulls_add_node_rcu(sk, list)
spin_unlock()As both deletion and insertion operate on the same ehash chain, this patch introduces two new sk_nulls_replace_* helper functions to implement atomic replacement.
--- include/net/sock.h | 23 +++++++++++++++++++++++ net/ipv4/inet_hashtables.c | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-)
include/net/sock.h +23 -0
@@ -854,6 +854,29 @@ static inline bool sk_nulls_del_node_init_rcu(struct sock *sk)return rc; } +static inline bool __sk_nulls_replace_node_init_rcu (struct sock *old, +struct sock *new) +{ +if (sk_hashed (old)) { +hlist_nulls_replace_init_rcu (&old->sk_nulls_node, + &new->sk_nulls_node); +return true; + } +return false; +} + +static inline bool sk_nulls_replace_node_init_rcu (struct sock *old, +struct sock *new) +{ +bool rc =__sk_nulls_replace_node_init_rcu (old, new); + +if (rc) { +WARN_ON (refcount_read (&old->sk_refcnt) ==1 ); +__sock_put (old); + } +return rc; +} +static inline void __sk_add_node (struct sock *sk,struct hlist_head *list) {hlist_add_head (&sk->sk_node, list);
net/ipv4/inet_hashtables.c +3 -1
@@ -685,7 +685,8 @@ bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)spin_lock (lock);if (osk) {WARN_ON_ONCE (sk->sk_hash != osk->sk_hash); - ret =sk_nulls_del_node_init_rcu (osk); + ret =sk_nulls_replace_node_init_rcu (osk, sk); + goto unlock; }else if (found_dup_sk) { *found_dup_sk =inet_ehash_lookup_by_sk (sk, list);if (*found_dup_sk)
@@ -695,6 +696,7 @@ bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)if (ret)__sk_nulls_add_node_rcu (sk, list); +unlock:spin_unlock (lock);return ret;
Signature · 1 line
2.25.1
PATCH v3 3/3 inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
In reply to [email protected]From: Xuanqiang Luo <[email protected]>
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.
--- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
net/ipv4/inet_timewait_sock.c +6 -9
@@ -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);
Signature · 1 line
2.25.1
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v3 3/3] inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
Quoted 58 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
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 settingtw_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 <[email protected]>--- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)net/ipv4/inet_timewait_sock.c +1
-9@@ -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().
net/ipv4/inet_timewait_sock.c void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, · line 160
Quoted 7 lines
net/ipv4/inet_timewait_sock.c +4
-0@@ -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);
When hlist_nulls_replace_init_rcu() returns false ?
Quoted 7 lines
net/ipv4/inet_timewait_sock.c +1
-1+inet_twsk_schedule (tw, timeo);spin_unlock (lock); --2.25.1
Re: [PATCH net-next v3 3/3] inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
Quoted 61 lines · Kuniyuki Iwashima
Quoted 60 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
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 settingtw_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 <[email protected]>--- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)net/ipv4/inet_timewait_sock.c +1
-9@@ -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 );
Quoted 5 lines
net/ipv4/inet_timewait_sock.c void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, · line 160
Quoted 4 lines
net/ipv4/inet_timewait_sock.c +4
-0+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);When
hlist_nulls_replace_init_rcu()returns false ?
When hlist_nulls_replace_init_rcu() returns false, it means sk is unhashed, the replacement operation failed, we need to insert tw, and this doesn't change the original logic.
Quoted 8 lines
Quoted 8 lines
net/ipv4/inet_timewait_sock.c +1
-1+inet_twsk_schedule (tw, timeo);spin_unlock (lock); --2.25.1
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v3 3/3] inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
Quoted 64 lines · luoxuanqiang
Quoted 61 lines · Kuniyuki Iwashima
Quoted 60 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
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 settingtw_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 <[email protected]>--- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)net/ipv4/inet_timewait_sock.c +1
-9@@ -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 );
Quoted 8 lines
Quoted 5 lines
net/ipv4/inet_timewait_sock.c void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, · line 160
Quoted 4 lines
net/ipv4/inet_timewait_sock.c +4
-0+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);When
hlist_nulls_replace_init_rcu()returns false ?When
hlist_nulls_replace_init_rcu()returns false, it means sk is unhashed,
and how does this happen ?
Here is under lock_sock() I think, for example, you can find a lockdep annotation in the path:
tcp_time_wait_init
tp->af_specific->md5_lookup / tcp_v4_md5_lookup
tcp_md5_do_lookup
__tcp_md5_do_lookup
rcu_dereference_check(tp->md5sig_info, lockdep_sock_is_held(sk));So, is there a path that unhashes socket without holding lock_sock() ?
Quoted 11 lines
the replacement operation failed, we need
to insert tw, and this doesn't change the original logic.Quoted 8 lines
Quoted 8 lines
net/ipv4/inet_timewait_sock.c +1
-1+inet_twsk_schedule (tw, timeo);spin_unlock (lock); --2.25.1
Re: [PATCH net-next v3 3/3] inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
Quoted 76 lines · Kuniyuki Iwashima
Quoted 63 lines · luoxuanqiang
Quoted 61 lines · Kuniyuki Iwashima
Quoted 60 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
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 settingtw_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 <[email protected]>--- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)net/ipv4/inet_timewait_sock.c +1
-9@@ -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 );Quoted 7 lines
Quoted 5 lines
net/ipv4/inet_timewait_sock.c void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, · line 160
Quoted 4 lines
net/ipv4/inet_timewait_sock.c +4
-0+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);When
hlist_nulls_replace_init_rcu()returns false ?When
hlist_nulls_replace_init_rcu()returns false, it means sk is unhashed,and how does this happen ?
Here is under
lock_sock()I think, for example, you can find a lockdep annotation in the path:
Quoted 9 lines
tcp_time_wait_init
tp->af_specific->md5_lookup/tcp_v4_md5_lookuptcp_md5_do_lookup __tcp_md5_do_lookup rcu_dereference_check(tp->md5sig_info, lockdep_sock_is_held(sk));So, is there a path that unhashes socket without holding
lock_sock()?
I'm not entirely sure about this point yet, because inet_unhash() is called in too many places and uses __sk_nulls_del_node_init_rcu() to unhash sockets without explicitly requiring bh_lock_sock().
Until I can verify this, I'll keep the original check for old socket unhashed state to ensure safety.
It would be great if you could confirm this behavior.
Thanks
Xuanqiang.
Quoted 10 lines
Quoted 10 lines
the replacement operation failed, we need
to insert tw, and this doesn't change the original logic.Quoted 7 lines
Quoted 7 lines
net/ipv4/inet_timewait_sock.c +1
-1+inet_twsk_schedule (tw, timeo);spin_unlock (lock); --2.25.1
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v3 3/3] inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()
Quoted 93 lines · luoxuanqiang
Quoted 76 lines · Kuniyuki Iwashima
Quoted 63 lines · luoxuanqiang
Quoted 61 lines · Kuniyuki Iwashima
Quoted 60 lines · On Tue, Sep 16, 2025 at 3:31 AM <[email protected]> wrote:
From: Xuanqiang Luo <[email protected]>
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 settingtw_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 <[email protected]>--- net/ipv4/inet_timewait_sock.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)net/ipv4/inet_timewait_sock.c +1
-9@@ -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 );Quoted 7 lines
Quoted 5 lines
net/ipv4/inet_timewait_sock.c void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw, · line 160
Quoted 4 lines
net/ipv4/inet_timewait_sock.c +4
-0+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);When
hlist_nulls_replace_init_rcu()returns false ?When
hlist_nulls_replace_init_rcu()returns false, it means sk is unhashed,and how does this happen ?
Here is under
lock_sock()I think, for example, you can find a lockdep annotation in the path:Quoted 9 lines
tcp_time_wait_init
tp->af_specific->md5_lookup/tcp_v4_md5_lookuptcp_md5_do_lookup __tcp_md5_do_lookup rcu_dereference_check(tp->md5sig_info, lockdep_sock_is_held(sk));So, is there a path that unhashes socket without holding
lock_sock()?I'm not entirely sure about this point yet, because
inet_unhash()is called in too many places and uses__sk_nulls_del_node_init_rcu()to unhash sockets without explicitly requiringbh_lock_sock().
Quoted 4 lines
Until I can verify this, I'll keep the original check for old socket unhashed state to ensure safety.
It would be great if you could confirm this behavior.