Skip to content

Commit

Permalink
udp: annotate data-race in __udp_enqueue_schedule_skb()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jakub Kicinski <[email protected]>
  • Loading branch information
Eric Dumazet authored and kuba-moo committed Mar 29, 2024
1 parent 46dc11b commit 6055796
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions net/ipv4/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down

0 comments on commit 6055796

Please sign in to comment.