Skip to content

net: Avoid ehash lookup races

[email protected] netdev 9 versions – Applied

Status from this message: This series was applied to netdev/net-next.git (main)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v8 Latest3 patches 2 repliesApplied 3/3
  2. v7 posting 2/23 patches 4 repliesSuperseded
  3. v7 posting 1/23 patches 20 repliesSuperseded
Earlier versions · 6
VersionPostedPatchesRepliesStatus
v63 patches8 repliesSuperseded
v53 patches4 repliesSuperseded
v43 patches8 repliesSuperseded
v33 patches11 repliesSuperseded
v23 patches4 repliesSuperseded
v13 patches8 repliesSuperseded

Changes in v8 from the cover letter

* Patch2
* Remove the unnecessary DEBUG_NET_WARN_ON_ONCE() - thanks to Eric!

Patches in v8 All 3 applied

#Subject and filesSizeRepliesApplied
1/3rculist: Add hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()include/linux/rculist_nulls.h+59 -00 repliesApplied
2/3inet: Avoid ehash lookup race in inet_ehash_insert()include/net/sock.h net/ipv4/inet_hashtables.c+19 -21 replyApplied
3/3inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()net/ipv4/inet_timewait_sock.c+12 -231 replyApplied

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchKuniyuki IwashimaFrederic WeisbeckerEric DumazetJiayuan Chen
1/3R-b R-b R-b
2/3R-b R-b R-b
3/3R-b R-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v8 thread

Cover letter [PATCH net-next v8 0/3] 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:
v8:
* Patch2
* Remove the unnecessary DEBUG_NET_WARN_ON_ONCE() - thanks to Eric!

v7:https://lore.kernel.org/all/[email protected]/
* Patch1
* Fix the checkpatch complaints.
* Introduces hlist_nulls_pprev_rcu() to replace
(*((struct hlist_nulls_node __rcu __force **)(node)->pprev)).
* Use next->pprev instead of new->next->pprev.
* Patch2
* Remove else if in inet_ehash_insert(), use if instead.
* Patch3
* Fix legacy comment style issues in inet_twsk_hashdance_schedule().

v6: https://lore.kernel.org/all/[email protected]/
* Patch 1
* Send and CC to the RCU maintainers.
* Patch 3
* Remove the unused function inet_twsk_add_node_rcu() and variable ehead fix build warnings.

v5: https://lore.kernel.org/all/[email protected]/
* Patch 1
* Rename __hlist_nulls_replace_rcu() to hlist_nulls_replace_rcu() and update the description of hlist_nulls_replace_init_rcu().
* Patch 2
* Remove __sk_nulls_replace_node_init_rcu() and inline it into sk_nulls_replace_node_init_rcu().
* Use DEBUG_NET_WARN_ON_ONCE() instead of WARN_ON().
* Patch 3
* Move smp_wmb() after setting the refcount.

v4: https://lore.kernel.org/all/[email protected]/
* Patch 1
* Use WRITE_ONCE() for ->next in __hlist_nulls_replace_rcu(), and add why in the commit message.
* Remove the node hash check in hlist_nulls_replace_init_rcu() to avoid redundancy. Also remove the return value, as it serves no purpose in this patch series.
* Patch 3
* Remove the check of hlist_nulls_replace_init_rcu() return value in inet_twsk_hashdance_schedule() as it is unnecessary. Thanks to Kuni for clarifying this.

v3: https://lore.kernel.org/all/[email protected]/
* 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 | 59 +++++++++++++++++++++++++++++++++++
 include/net/sock.h            | 13 ++++++++
 net/ipv4/inet_hashtables.c    |  8 +++--
 net/ipv4/inet_timewait_sock.c | 35 +++++++--------------
 4 files changed, 90 insertions(+), 25 deletions(-)
Signature · 1 line
2.25.1