The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Philo Lu <lulie@linux.alibaba.com>
To: Gur Stavi <gur.stavi@huawei.com>
Cc: antony.antony@secunet.com, davem@davemloft.net,
	dsahern@kernel.org, dust.li@linux.alibaba.com,
	edumazet@google.com, fred.cc@alibaba-inc.com,
	jakub@cloudflare.com, kuba@kernel.org,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	pabeni@redhat.com, steffen.klassert@secunet.com,
	willemdebruijn.kernel@gmail.com,
	yubing.qiuyubing@alibaba-inc.com
Subject: Re: [RFC PATCHv2 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket
Date: Wed, 25 Sep 2024 11:16:53 +0800	[thread overview]
Message-ID: <0fb425e0-5482-4cdf-9dc1-3906751f8f81@linux.alibaba.com> (raw)
In-Reply-To: <20240924123113.1688315-1-gur.stavi@huawei.com>


On 2024/9/24 20:31, Gur Stavi wrote:
>> +/* In hash4, rehash can also happen in connect(), where hash4_cnt keeps unchanged. */
>> +static void udp4_rehash4(struct udp_table *udptable, struct sock *sk, u16 newhash4)
>> +{
>> +	struct udp_hslot *hslot4, *nhslot4;
>> +
>> +	hslot4 = udp_hashslot4(udptable, udp_sk(sk)->udp_lrpa_hash);
>> +	nhslot4 = udp_hashslot4(udptable, newhash4);
>> +	udp_sk(sk)->udp_lrpa_hash = newhash4;
>> +
>> +	if (hslot4 != nhslot4) {
>> +		spin_lock_bh(&hslot4->lock);
>> +		hlist_del_init_rcu(&udp_sk(sk)->udp_lrpa_node);
>> +		hslot4->count--;
>> +		spin_unlock_bh(&hslot4->lock);
> 
> I realize this is copied from udp_lib_rehash, but isn't it an RCU bug?
> Once a node is removed from a list, shouldn't synchronize_rcu be called
> before it is reused for a new list? A reader that was traversing the
> old list may find itself on the new list.
> 
>> +
>> +		spin_lock_bh(&nhslot4->lock);
>> +		hlist_add_head_rcu(&udp_sk(sk)->udp_lrpa_node, &nhslot4->head);
>> +		nhslot4->count++;
>> +		spin_unlock_bh(&nhslot4->lock);
>> +	}
>> +}
>> +

Good catch! IIUC, synchronize_rcu() is needed here, or otherwise, this 
could happen:

    Reader(lookup)     Writer(rehash)
    -----------------  ---------------
1. rcu_read_lock()
2. pos = sk;
3.                     hlist_del_init_rcu(sk, old_slot)
4.                     hlist_add_head_rcu(sk, new_slot)
5. pos = pos->next; <=
6. rcu_read_unlock()

In step 5, we wrongly moved from old_slot to new_slot.

Perhaps the similar codes in udp_lib_rehash() for hslot2 also need a fix.

Thanks.
-- 
Philo


      reply	other threads:[~2024-09-25  3:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-24 11:04 [RFC PATCHv2 net-next 0/3] udp: Add 4-tuple hash for connected sockets Philo Lu
2024-09-24 11:04 ` [RFC PATCHv2 net-next 1/3] net/udp: Add a new struct for hash2 slot Philo Lu
2024-09-24 12:30   ` Gur Stavi
2024-09-25  2:43     ` Philo Lu
2024-09-24 12:51   ` Gur Stavi
2024-09-25  2:57     ` Philo Lu
2024-09-24 11:04 ` [RFC PATCHv2 net-next 2/3] net/udp: Add 4-tuple hash list basis Philo Lu
2024-09-24 11:04 ` [RFC PATCHv2 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket Philo Lu
2024-09-24 12:31   ` Gur Stavi
2024-09-25  3:16     ` Philo Lu [this message]

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=0fb425e0-5482-4cdf-9dc1-3906751f8f81@linux.alibaba.com \
    --to=lulie@linux.alibaba.com \
    --cc=antony.antony@secunet.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=dust.li@linux.alibaba.com \
    --cc=edumazet@google.com \
    --cc=fred.cc@alibaba-inc.com \
    --cc=gur.stavi@huawei.com \
    --cc=jakub@cloudflare.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=steffen.klassert@secunet.com \
    --cc=willemdebruijn.kernel@gmail.com \
    --cc=yubing.qiuyubing@alibaba-inc.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