diff mbox series

[net-next,1/4] udp: annotate data-race in __udp_enqueue_schedule_skb()

Message ID 20240328144032.1864988-2-edumazet@google.com (mailing list archive)
State Accepted
Commit 60557969951304dad829f2829019907dfb43ecb3
Delegated to: Netdev Maintainers
Headers show
Series udp: small changes on receive path | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 952 this patch: 952
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: dsahern@kernel.org
netdev/build_clang success Errors and warnings before: 956 this patch: 956
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 963 this patch: 963
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 26 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-03-29--06-00 (tests: 951)

Commit Message

Eric Dumazet March 28, 2024, 2:40 p.m. UTC
sk->sk_rcvbuf is read locklessly twice, while other threads
could change its value.

Use a READ_ONCE() to annotate the race.

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 net/ipv4/udp.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Willem de Bruijn March 28, 2024, 11:52 p.m. UTC | #1
Eric Dumazet wrote:
> sk->sk_rcvbuf is read locklessly twice, while other threads
> could change its value.
> 
> Use a READ_ONCE() to annotate the race.
> 
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> ---
>  net/ipv4/udp.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 661d0e0d273f616ad82746b69b2c76d056633017..f2736e8958187e132ef45d8e25ab2b4ea7bcbc3d 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -1492,13 +1492,14 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
>  	struct sk_buff_head *list = &sk->sk_receive_queue;
>  	int rmem, err = -ENOMEM;
>  	spinlock_t *busy = NULL;
> -	int size;
> +	int size, rcvbuf;
>  
> -	/* try to avoid the costly atomic add/sub pair when the receive
> -	 * queue is full; always allow at least a packet
> +	/* Immediately drop when the receive queue is full.
> +	 * Always allow at least one packet.
>  	 */
>  	rmem = atomic_read(&sk->sk_rmem_alloc);
> -	if (rmem > sk->sk_rcvbuf)
> +	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
> +	if (rmem > rcvbuf)
>  		goto drop;
>  
>  	/* Under mem pressure, it might be helpful to help udp_recvmsg()
> @@ -1507,7 +1508,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
>  	 * - Less cache line misses at copyout() time
>  	 * - Less work at consume_skb() (less alien page frag freeing)
>  	 */
> -	if (rmem > (sk->sk_rcvbuf >> 1)) {
> +	if (rmem > (rcvbuf >> 1)) {
>  		skb_condense(skb);
>  
>  		busy = busylock_acquire(sk);

There's a third read in this function:

        /* we drop only if the receive buf is full and the receive
         * queue contains some other skb
         */
        rmem = atomic_add_return(size, &sk->sk_rmem_alloc);
        if (rmem > (size + (unsigned int)sk->sk_rcvbuf))
                goto uncharge_drop;

Another READ_ONCE if intent is to not use the locally cached copy?
Willem de Bruijn March 29, 2024, 12:05 a.m. UTC | #2
Willem de Bruijn wrote:
> Eric Dumazet wrote:
> > sk->sk_rcvbuf is read locklessly twice, while other threads
> > could change its value.
> > 
> > Use a READ_ONCE() to annotate the race.
> > 
> > Signed-off-by: Eric Dumazet <edumazet@google.com>
> > ---
> >  net/ipv4/udp.c | 11 ++++++-----
> >  1 file changed, 6 insertions(+), 5 deletions(-)
> > 
> > diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> > index 661d0e0d273f616ad82746b69b2c76d056633017..f2736e8958187e132ef45d8e25ab2b4ea7bcbc3d 100644
> > --- a/net/ipv4/udp.c
> > +++ b/net/ipv4/udp.c
> > @@ -1492,13 +1492,14 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
> >  	struct sk_buff_head *list = &sk->sk_receive_queue;
> >  	int rmem, err = -ENOMEM;
> >  	spinlock_t *busy = NULL;
> > -	int size;
> > +	int size, rcvbuf;
> >  
> > -	/* try to avoid the costly atomic add/sub pair when the receive
> > -	 * queue is full; always allow at least a packet
> > +	/* Immediately drop when the receive queue is full.
> > +	 * Always allow at least one packet.
> >  	 */
> >  	rmem = atomic_read(&sk->sk_rmem_alloc);
> > -	if (rmem > sk->sk_rcvbuf)
> > +	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
> > +	if (rmem > rcvbuf)
> >  		goto drop;
> >  
> >  	/* Under mem pressure, it might be helpful to help udp_recvmsg()
> > @@ -1507,7 +1508,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
> >  	 * - Less cache line misses at copyout() time
> >  	 * - Less work at consume_skb() (less alien page frag freeing)
> >  	 */
> > -	if (rmem > (sk->sk_rcvbuf >> 1)) {
> > +	if (rmem > (rcvbuf >> 1)) {
> >  		skb_condense(skb);
> >  
> >  		busy = busylock_acquire(sk);
> 
> There's a third read in this function:

But you remove that in the next patch. Ok.
diff mbox series

Patch

diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 661d0e0d273f616ad82746b69b2c76d056633017..f2736e8958187e132ef45d8e25ab2b4ea7bcbc3d 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1492,13 +1492,14 @@  int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
 	struct sk_buff_head *list = &sk->sk_receive_queue;
 	int rmem, err = -ENOMEM;
 	spinlock_t *busy = NULL;
-	int size;
+	int size, rcvbuf;
 
-	/* try to avoid the costly atomic add/sub pair when the receive
-	 * queue is full; always allow at least a packet
+	/* Immediately drop when the receive queue is full.
+	 * Always allow at least one packet.
 	 */
 	rmem = atomic_read(&sk->sk_rmem_alloc);
-	if (rmem > sk->sk_rcvbuf)
+	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
+	if (rmem > rcvbuf)
 		goto drop;
 
 	/* Under mem pressure, it might be helpful to help udp_recvmsg()
@@ -1507,7 +1508,7 @@  int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
 	 * - Less cache line misses at copyout() time
 	 * - Less work at consume_skb() (less alien page frag freeing)
 	 */
-	if (rmem > (sk->sk_rcvbuf >> 1)) {
+	if (rmem > (rcvbuf >> 1)) {
 		skb_condense(skb);
 
 		busy = busylock_acquire(sk);