netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "'willemdebruijn.kernel@gmail.com'"
	<willemdebruijn.kernel@gmail.com>,
	"'davem@davemloft.net'" <davem@davemloft.net>,
	"'dsahern@kernel.org'" <dsahern@kernel.org>,
	'Eric Dumazet' <edumazet@google.com>,
	"'kuba@kernel.org'" <kuba@kernel.org>,
	"'pabeni@redhat.com'" <pabeni@redhat.com>,
	"'netdev@vger.kernel.org'" <netdev@vger.kernel.org>
Subject: [PATCH 2/2] Rescan the hash2 list if the hash chains have got cross-linked.
Date: Wed, 26 Jul 2023 12:05:59 +0000	[thread overview]
Message-ID: <c45337a3d46641dc8c4c66bd49fb55b6@AcuMS.aculab.com> (raw)
In-Reply-To: <fbcaa54791cd44999de5fec7c6cf0b3c@AcuMS.aculab.com>

udp_lib_rehash() can get called at any time and will move a
socket to a different hash2 chain.
This can cause udp4_lib_lookup2() (processing incoming UDP) to
fail to find a socket and an ICMP port unreachable be sent.

Prior to ca065d0cf80fa the lookup used 'hlist_nulls' and checked
that the 'end if list' marker was on the correct list.

Rather than re-instate the 'nulls' list just check that the final
socket is on the correct list.

The cross-linking can definitely happen (see earlier issues with
it looping forever because gcc cached the list head).

Fixes: ca065d0cf80fa ("udp: no longer use SLAB_DESTROY_BY_RCU")
Signed-off-by: David Laight <david.laight@aculab.com>
---
 net/ipv4/udp.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index ad64d6c4cd99..ed92ba7610b0 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -443,6 +443,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
 				     struct sk_buff *skb)
 {
 	unsigned int hash2, slot2;
+	unsigned int hash2_rescan;
 	struct udp_hslot *hslot2;
 	struct sock *sk, *result;
 	int score, badness;
@@ -451,9 +452,12 @@ static struct sock *udp4_lib_lookup2(struct net *net,
 	slot2 = hash2 & udptable->mask;
 	hslot2 = &udptable->hash2[slot2];
 
+rescan:
+	hash2_rescan = hash2;
 	result = NULL;
 	badness = 0;
 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
+		hash2_rescan = udp_sk(sk)->udp_portaddr_hash;
 		score = compute_score(sk, net, saddr, sport,
 				      daddr, hnum, dif, sdif);
 		if (score > badness) {
@@ -467,6 +471,16 @@ static struct sock *udp4_lib_lookup2(struct net *net,
 			badness = score;
 		}
 	}
+
+	/* udp sockets can get moved to a different hash chain.
+	 * If the chains have got crossed then rescan.
+	 */
+	if ((hash2_rescan & udptable->mask) != slot2) {
+		/* Ensure hslot2->head is reread */
+		barrier();
+		goto rescan;
+	}
+
 	return result;
 }
 
-- 
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


  parent reply	other threads:[~2023-07-26 12:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-26 11:51 [PATCH 0/2] udp: rescan hash2 list if chains crossed David Laight
2023-07-26 12:05 ` [PATCH 1/2] Move hash calculation inside udp4_lib_lookup2() David Laight
2023-07-26 13:44   ` Paolo Abeni
2023-07-26 14:02     ` David Laight
2023-07-26 14:03       ` Eric Dumazet
2023-07-26 14:06         ` David Laight
2023-07-26 15:42     ` David Laight
2023-07-26 12:05 ` David Laight [this message]
2023-07-26 13:36   ` [PATCH 2/2] Rescan the hash2 list if the hash chains have got cross-linked Paolo Abeni
2023-07-26 13:37   ` Eric Dumazet
2023-07-26 14:13     ` David Laight
2023-07-26 14:21       ` Eric Dumazet
2023-07-26 14:39         ` David Laight
2023-07-26 14:48           ` Eric Dumazet

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=c45337a3d46641dc8c4c66bd49fb55b6@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemdebruijn.kernel@gmail.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;
as well as URLs for NNTP newsgroup(s).