From patchwork Thu Mar 28 14:40:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13608686 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D544F82D7F for ; Thu, 28 Mar 2024 14:40:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636838; cv=none; b=Z5vSipqu67Af/BuJJ1gVUE548Rc7RWKcc87zlH9bOfTtmS/w0j+nVMtN19c2CIA/1ZuI/1lHRTVJbYmxc7hPZ6oZoD9lopitmOMW03vKs8fwHkcedtZlt08X4bmLC50aMcbyjCMDIRdSIUWgYjVf9tG1AFQvveVnZAtjqO5Rscs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636838; c=relaxed/simple; bh=+gdzjdAz3SmFxBm9Peu7OfEUlG2/6IYoqorF/m2CC3o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oQgIVaMlCZPQM/OhByQnbWmPnkwVpNOBazg7TF6NUiXtNY0Z/DFNiDvo9dJDEUnH6e1AOj8XazOVkRnK2DaOeUgIOCAuxajWcd6L6JD7J6dw8xbPObnudM4IpMz4oC0wdX/HM9OfqhjOgu+fPGc4j/daZS/JNdnGhAm9m8Fwc2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zrW22ih3; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zrW22ih3" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-611e89cf3b5so16742377b3.3 for ; Thu, 28 Mar 2024 07:40:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711636836; x=1712241636; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Y1Blf2tfA5r57pliiQCXKJixA+SeK/vqFtyh4abCJ18=; b=zrW22ih352RobK2NWX69gVxhpIA2RHvqbYdtfu340QspugvPclIj5gHRQ5N1BvAM/Y xhJohs9uzHeJxImRviBcdFWvnVFBdnAjSgIZ5uojFhPf5IEhia1gT+btShQLblzrLz3u 2grZycdkaHvvvAk17HmJe5FZo5QpBSu6Zum7U+0DnTapVfeRzI0/seVdYbNw6S79vdMA nXly9o3Gz0vASuz80Nl7IQmWw2dY1pv3ErmI9dAtCPIsbsLQrHgUWYnLpwB9ns1gMYWY VLfvgy6s+2L4IvAWq0Djeq86vxg/qBtkMqCYtfJs7eKi6NSFGpU6VVLnpdyg2/S3KEbw fLaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711636836; x=1712241636; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Y1Blf2tfA5r57pliiQCXKJixA+SeK/vqFtyh4abCJ18=; b=JStutj6YsmpcjVwj9TD4ydZvBICFvsLXzm2zHVOmOmNa0ZgsaPqosce0IY0fZtBQu5 pa53oWA0vtsC7PYbOntsVfqaIjHLbisOuc7+dJvqSHjkyUVxnOjA6VNSiXsYthMYyFCv LpAQAcOHXoAHEH5itrdlHl50oAqfg84NvkJewC6U9an/UlWh72wHYZk6f/I3XRbRQs5s h9661mmcs+CGfBemhmHqfSlKuSR+W1K50xV5QPc3z8trCdiaSqg3J+/ifI9jTxAphXC2 bIrttoPDoyR7pa/S0wNDvia7XQJttucFIL1Xz/YXHA27Xd+n7vrkWHWBXB6FyLsWNiSj gJ1A== X-Forwarded-Encrypted: i=1; AJvYcCWGsu3IQLJ3rKSEgaW6tfaiiI4+S8T3sxDfjmMgOcG+WkGLEBwlmkPDKmc7517zFkA4elhu8fNpcge4IJsj9ZuAsZYkmlkG X-Gm-Message-State: AOJu0YyClWWK90n4Yr3klfcrRGWz+1EetVBTqR/F63/t8QGq4KcT37eQ SByHo7MX3mgYysncK/2lOwpPtLdgqhZiCVKaEHjK9d9NfULkLcefeZgHRAEDktd4Crgif7eGpeE Jy4ZdMXrKRA== X-Google-Smtp-Source: AGHT+IHAqudW+dS7+SmI3Sc39hQ4EeL4UxtAyM2XVSSvEpfCnV/GM5VstfU1dMfOKyO9epjZklOWMqZ9TXh/dQ== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:2182:b0:dce:30f5:6bc5 with SMTP id dl2-20020a056902218200b00dce30f56bc5mr213520ybb.4.1711636835954; Thu, 28 Mar 2024 07:40:35 -0700 (PDT) Date: Thu, 28 Mar 2024 14:40:29 +0000 In-Reply-To: <20240328144032.1864988-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240328144032.1864988-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240328144032.1864988-2-edumazet@google.com> Subject: [PATCH net-next 1/4] udp: annotate data-race in __udp_enqueue_schedule_skb() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Willem de Bruijn , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org 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 --- 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); From patchwork Thu Mar 28 14:40:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13608687 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9287912D76A for ; Thu, 28 Mar 2024 14:40:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636840; cv=none; b=MtksoqEYayb/JJ5Q9Bvv18EMqpKWCTm47iFS5qZMxdDZZ2Vm3oZ1bdmlCouTCnLcSSIptNihdpIyloft7vL8yFAoRaPV2wZ/uEj6oIOJKzNWbvC9h49wWeEyxn+JMrXK0wGlW/KVoTq5eAkNW03xT7AxcIsg9WaS7pqlBW01k7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636840; c=relaxed/simple; bh=wIncWrswncQh1bRWLkOE4ukf9R7mdV4WgyoV/cDr3i4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ARyq9fJ+XDjhCoUnVTvg3c/WHpt5YZ874Ut3WOGNmx72J2ofOuQx4Efu3sns4PWyRN3QnN4hwvW34GsbizgSE3UBx6XpTDCWbI1sXRotL4wUXwKmwG86SCZji499uF4zWeYGjbUsmikLUYBcp6clvOJJt8XioOWnJ/69TGhIyJM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xImDMEGr; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xImDMEGr" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbf618042daso1433963276.0 for ; Thu, 28 Mar 2024 07:40:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711636837; x=1712241637; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=74e/xj321Yg8Kp/ugt0Wz2SNI5GKWEc3hdNqdlM7Rp4=; b=xImDMEGrPfTCl3hsG0TCswa0wZ9aBH5pHceC9TkDaGgcwmBllxK7xbtgbeESrH7a3O DNAdtmD9Xv+PstEaYt+vE1TQeJwtb5/ajxrbSR7/1OpwoKEbVTAm+qmcHvGir++uGyRJ sdr20fTE37LcX5MXyI19nCnN3XlepFdKWnts+jAVjB3IjH7rvIr4TkAdAvLTI5NMYlUe N5gt2ttq7L1nbtBTBoBt9Eh0Y6CPn1skNOxSbGmu/YRbbXiAc9s1ijJeAP+zbkEI/lcU AWsMVcmqxrdZPt0r9ORxkmfFsUwYshNDINtkLsK/g1YaVeGl/zcmb0fwRtFTm7DXk9gf dBKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711636837; x=1712241637; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=74e/xj321Yg8Kp/ugt0Wz2SNI5GKWEc3hdNqdlM7Rp4=; b=WG6yi0a5O/JxXvt9QZyivy4cqjYfDNcpKrfp+dObBsgjfsxzBppFlpv+WE2uN23DAA vWcN87xr6mKQyp1DSgI9LPMFazlhyh+vi8jlB3n6QOUTPoYR0e5T+UqGzXju2+PLpZXi jTTP0xabd92BPOCe2SIgjXWgcay3biZ9qtCdP6yYo6b37AikAeZ9spcrCIUL3lmBMkYV ASDcZt4B0Ff6dGbB/V3Xo8c9CnDygRaujC2HV0K7peRU/3WWLRESY36mBJwPxXJ0EFcV XPIqHOvXng5K58XKdsvornn39GciYixFdA4gWSLCdk/Z0euym6p8CsDhL/f7vzc9ic4U sOPQ== X-Forwarded-Encrypted: i=1; AJvYcCXiZalqxutM5TaCG4tZqio8HRq14tAC3LW/gRiikY0eCVlscbSIV0s2RD8+bqEW+6ZnbeGWZOFtg0Hg/eU06wnxAYwp7mzF X-Gm-Message-State: AOJu0Yz5WjYNF0ku8xYir7UDibqtmYOzS7H0fVenx4JZyDSvqN3BLKub pI7xo4P9GNm9jD1ci6NoKLu9JlYkhnCtU/tWciNvhZy/Tgwoye77YMU+pC12CqnO0zEnIMk3a5P VEL1OC2W2PQ== X-Google-Smtp-Source: AGHT+IF/Y0ofKB2DyDqUZAQTS65is5HxA4flQEVOy942Zd2HFD9Dil8dPIykb/bxzvZEKenSzQzfnI9ZBKeNIw== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:1b06:b0:dcc:94b7:a7a3 with SMTP id eh6-20020a0569021b0600b00dcc94b7a7a3mr206671ybb.12.1711636837606; Thu, 28 Mar 2024 07:40:37 -0700 (PDT) Date: Thu, 28 Mar 2024 14:40:30 +0000 In-Reply-To: <20240328144032.1864988-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240328144032.1864988-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240328144032.1864988-3-edumazet@google.com> Subject: [PATCH net-next 2/4] udp: relax atomic operation on sk->sk_rmem_alloc From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Willem de Bruijn , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org atomic_add_return() is more expensive than atomic_add() and seems overkill in UDP rx fast path. Signed-off-by: Eric Dumazet --- net/ipv4/udp.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index f2736e8958187e132ef45d8e25ab2b4ea7bcbc3d..d2fa9755727ce034c2b4bca82bd9e72130d588e6 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1516,12 +1516,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) size = skb->truesize; udp_set_dev_scratch(skb); - /* 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; + atomic_add(size, &sk->sk_rmem_alloc); spin_lock(&list->lock); err = udp_rmem_schedule(sk, size); From patchwork Thu Mar 28 14:40:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13608688 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EED6A82D7F for ; Thu, 28 Mar 2024 14:40:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636841; cv=none; b=a9L5v7xAbZw5tPR2k76sMiszNSIRfJodP6xErtgLIosiNGVyYFdQGOSdVnmW6GVo6uyi+5+e1ALtBqNDS5tRaTdElpXNYspurr8j6yCIVKapKzsgY7IokSchEgj+s4Dt80J8K6ZEupZvgxZ7QUJBeehT+vTDT2C2S9zJn3Royyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636841; c=relaxed/simple; bh=vXeEMX03o4zDxJWBnlqazfGrTS0sbNL3vB3FVfzOgWc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XfMY7mHBOrhVKXsa6nO/7vDtb83+rk6Hv4LpdewXvcqjU6NVarOHifsZoT0jEC+MSnNq4KdoefgEo/tNJGebTE39nEFS10oMmWXYsYsQWyBKPBA4vpR+3XtOlFFhcd0Mcue1PQDVGkFeH/rZit2HPmBeDHVurFst7b4EGtL7OaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cr0PRFoE; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cr0PRFoE" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60cd62fa20fso19935577b3.3 for ; Thu, 28 Mar 2024 07:40:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711636839; x=1712241639; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7yB6XxADH9yBrLmsEyQeqdJSGK3HyQrcUzUUKRfsk0E=; b=cr0PRFoEwtflTU1MsAT+Z/qbkj1rK9kwxB9L1SzQynG0N7BXQjgMkqwvbD77XdXiR/ 504OsIxUltXyR+UZikb6rrGQLQ7Zj/J4cpQPQuJSPXGweNmLP+J6HsrIQ9oTzZLRazM4 gQ5OM1SUK3ZbupA4ejcBwHdoYYpWSKRmCln4+d4x6nwQDmfRoPew668H7BxXrWvGAAdH fDjjMe4QXi0v6Si6WEmiWtDqRUW4pf8bMKGt1elaPaYdPTzpypDURxj9GlY2Yewgw0du mtoCzbMd/DjYJKbiTzN8dI+YrHw7OkCvTyyhGJi7o0rO1DnW9VdUDcFNm5D3PMFYRChX CCUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711636839; x=1712241639; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7yB6XxADH9yBrLmsEyQeqdJSGK3HyQrcUzUUKRfsk0E=; b=SXkd4To/dwc6Xr2VsihcqdwtSYJq1RlixRRRC5M9d9rDEaKyxVzsokEcRP5O4kS6A0 sP8JISOWvpYZAfWKCtOH7MnLTZLBN+ItAgWwTGBDox5AO1BRsYy6b7DrAdHgQagfJg4j pa5dqb9YNgvg4OLUmFQY4gPwXQY/KnBOdnPP5lDQS1uH6G3Fj4yGeHxTMjxnG+M2gyE/ HpOF6Y392sfOfk2bGFtYkRkY8nHZ6ZbsqO0m93S8hGNAlFbfNBqOkME9qa4awIvBTQgd 0Aadhqhs7sT74Dyw//w8x+wa9j83v6CUkm9DICy5PwctOjiSuyOY02jHA3tNjzg1c45Q XS4A== X-Forwarded-Encrypted: i=1; AJvYcCW539E/l7FF61oV12R8KqGtKgWzVsm3zj3d7uNWEm3LcJ+47IOl7KgSaiiro7zAdbXX4ZPmMV91tqVSZ/OVnlOTO/RN/Rax X-Gm-Message-State: AOJu0YytZj5xOnGO1WHotA74lAt3nGbnJTqMPjAr6dQvEZO+yZcQTuTs WKbksH476rvzrx2cs9IJyJcLhlu/o2NCOxgoZBqgBZE4bQywTOz7hUkARhEZOTBYoHw0bsg5Qx/ n6PYaB7b9Kg== X-Google-Smtp-Source: AGHT+IGfc+H0PdROU66qf1lhWBtM6Hj8BEMrzxSzSRc/IVUwVcGXDN9U7456u125VAxm5MKjAvvsSC/UV+s0LQ== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:1b0f:b0:dc2:2ace:860 with SMTP id eh15-20020a0569021b0f00b00dc22ace0860mr173088ybb.2.1711636839084; Thu, 28 Mar 2024 07:40:39 -0700 (PDT) Date: Thu, 28 Mar 2024 14:40:31 +0000 In-Reply-To: <20240328144032.1864988-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240328144032.1864988-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240328144032.1864988-4-edumazet@google.com> Subject: [PATCH net-next 3/4] udp: avoid calling sock_def_readable() if possible From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Willem de Bruijn , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org sock_def_readable() is quite expensive (particularly when ep_poll_callback() is in the picture). We must call sk->sk_data_ready() when : - receive queue was empty, or - SO_PEEK_OFF is enabled on the socket, or - sk->sk_data_ready is not sock_def_readable. We still need to call sk_wake_async(). Signed-off-by: Eric Dumazet Reviewed-by: Willem de Bruijn Acked-by: Paolo Abeni --- net/ipv4/udp.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index d2fa9755727ce034c2b4bca82bd9e72130d588e6..5dfbe4499c0f89f94af9ee1fb64559dd672c1439 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1492,6 +1492,7 @@ 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; + bool becomes_readable; int size, rcvbuf; /* Immediately drop when the receive queue is full. @@ -1532,12 +1533,19 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) */ sock_skb_set_dropcount(sk, skb); + becomes_readable = skb_queue_empty(list); __skb_queue_tail(list, skb); spin_unlock(&list->lock); - if (!sock_flag(sk, SOCK_DEAD)) - INDIRECT_CALL_1(sk->sk_data_ready, sock_def_readable, sk); - + if (!sock_flag(sk, SOCK_DEAD)) { + if (becomes_readable || + sk->sk_data_ready != sock_def_readable || + READ_ONCE(sk->sk_peek_off) >= 0) + INDIRECT_CALL_1(sk->sk_data_ready, + sock_def_readable, sk); + else + sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); + } busylock_release(busy); return 0; From patchwork Thu Mar 28 14:40:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13608689 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72BDA12D76A for ; Thu, 28 Mar 2024 14:40:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636843; cv=none; b=k9XulxDeTIJx76Mimz5zMkbnJJOaCJj7+n0etnu855YyHaB4mLhalkLEKuJhI5No0BsJE9V0vn3l3sqAT2o5orbmEs+0sO9JG0GHSpTmCquZ5U0P4BvuxRLOhTxsMzGymNhklur6WKB19pyVmONgzNz1OiERM+QpEdSCI4tyqrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711636843; c=relaxed/simple; bh=hH1R9nucTJno3soiryv3o2FiSWCSXHP2hnOZDfZ9SDU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uzc9b/H3CPSQzz/BZi88VWWfL5dtFBZgMNTqQjvH/7O/vHEHQBC5Yxu/K7wuzy3rCZPTp8So7Iq522goETGFBOx1rjxIplm0Un81THa2hP7H0tn9wBS0zepjtMV5h1ZhcNyaQ9Wyv0EVGEFGXngHVVhy9mBPjT/vQfMj7PNh08s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=o74ZV9CL; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o74ZV9CL" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60a2386e932so18714547b3.1 for ; Thu, 28 Mar 2024 07:40:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711636840; x=1712241640; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=V+p86SvCRADRmCtp38fnFrKEhLTcYAFsdvzNjPMNeyI=; b=o74ZV9CL5rsPPLbp/70E3aWmJ+eIyeUTqVcaczUmNhPNpWWKPRqC5IyXBoCLEQ3Dvg +Qa9ZUfMDgoA+GcFnQ4ZVVpwTDoLbpKmU5oju/f0ThWYvtd4dDIb/ZTE0tbVZyiWk0mx Zfx4InbRHSbTLtaO4b3+leRjc0OGslkFtg94/M+KHBKyzYr16MIZtOH28lvLd60ExgbF PrnQHCp5AVzGMqF5nkMhkB84KG10Vl1+LmHG/RQlR9/K30GGHFvDG/w/PL/8JPXo8JUK tU6xlPgR5vXmopbNZOUqYGdfn+rus+k/6+egG+kjoRnyegykSeBlCC/6QXTswKpxMwDN Ysjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711636840; x=1712241640; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=V+p86SvCRADRmCtp38fnFrKEhLTcYAFsdvzNjPMNeyI=; b=jcQuejLrIhuWDky7jmEqyF6Lb4i1CDPXWm5UKDBBlWBIlw2bmq2wPXRuGQGowT10AY CxYARrmYzlgVJ4T5ASO/AQK9RVEULie773OANg+wdwZ+SjfaxCuJ6DHbBfvAr86TvnAZ 5oR71RUZeAyakZuarDtyAIJyv8Go/A3xlwlfsf6zXJN3BztTlxed8GlpcjyDD94N0/NX pqhGXIVZHBLnH2d6OCYjubgnY104Rzl/KqnA3lC6Husp308TsQmrbF5w13Qz8HJMGASP AD/cMT/HSHHn9PkWGvit78eVFnxky89Rr9Om2FDNdeKsYAX+X7g6lsPROG0ExIBII0NR YXww== X-Forwarded-Encrypted: i=1; AJvYcCU1bleE2sFWzb4gqXHu6SMVcMOs7yTDjPe3rO3eT4dCMLr7sc+GBlA3lvH57pQkI+yyuoalISYdlh77AnaCF+IaHpcKmHsq X-Gm-Message-State: AOJu0YwTrJkRodgZQOXjFRcCZMrfuZ8zvDKYWBIrmdrT5UQHPyzq8yGg XyxtjdiO4Pr4CTMY1MaulRUm2AzMoUPVVMHTasGhQZ4Mco10po/ZBFE/ewJSli0HnsRxVMwwZAi BwiwYqOACKA== X-Google-Smtp-Source: AGHT+IEZYqHCpZDNMX/jR49tpTDjJmDcGTDoFb+s++TOGcFIZ05XLf5y0WOMTjdIn0OXgCkQ9ZRNv3RF6WoCCw== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:690c:64ca:b0:611:1f1c:1287 with SMTP id ht10-20020a05690c64ca00b006111f1c1287mr650924ywb.3.1711636840478; Thu, 28 Mar 2024 07:40:40 -0700 (PDT) Date: Thu, 28 Mar 2024 14:40:32 +0000 In-Reply-To: <20240328144032.1864988-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240328144032.1864988-1-edumazet@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240328144032.1864988-5-edumazet@google.com> Subject: [PATCH net-next 4/4] net: add sk_wake_async_rcu() helper From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Willem de Bruijn , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org While looking at UDP receive performance, I saw sk_wake_async() was no longer inlined. This matters at least on AMD Zen1-4 platforms (see SRSO) This might be because rcu_read_lock() and rcu_read_unlock() are no longer nops in recent kernels ? Add sk_wake_async_rcu() variant, which must be called from contexts already holding rcu lock. As SOCK_FASYNC is deprecated in modern days, use unlikely() to give a hint to the compiler. sk_wake_async_rcu() is properly inlined from __udp_enqueue_schedule_skb() and sock_def_readable(). Signed-off-by: Eric Dumazet --- crypto/af_alg.c | 4 ++-- include/net/sock.h | 6 ++++++ net/atm/common.c | 2 +- net/core/sock.c | 8 ++++---- net/dccp/output.c | 2 +- net/ipv4/udp.c | 2 +- net/iucv/af_iucv.c | 2 +- net/rxrpc/af_rxrpc.c | 2 +- net/sctp/socket.c | 2 +- net/smc/smc_rx.c | 4 ++-- net/unix/af_unix.c | 2 +- 11 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crypto/af_alg.c b/crypto/af_alg.c index 68cc9290cabe9a9f8a264908466897f2f93e039d..5bc6d0fa7498df30fdf002ec7bcfb46ed4344e8c 100644 --- a/crypto/af_alg.c +++ b/crypto/af_alg.c @@ -847,7 +847,7 @@ void af_alg_wmem_wakeup(struct sock *sk) wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLRDNORM | EPOLLRDBAND); - sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); + sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); rcu_read_unlock(); } EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup); @@ -914,7 +914,7 @@ static void af_alg_data_wakeup(struct sock *sk) wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | EPOLLRDNORM | EPOLLRDBAND); - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); rcu_read_unlock(); } diff --git a/include/net/sock.h b/include/net/sock.h index b5e00702acc1f037df7eb8ad085d00e0b18079a8..38adc3970500f4ae1b8d5ade343c5fbe1d04e085 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2506,6 +2506,12 @@ static inline void sk_wake_async(const struct sock *sk, int how, int band) } } +static inline void sk_wake_async_rcu(const struct sock *sk, int how, int band) +{ + if (unlikely(sock_flag(sk, SOCK_FASYNC))) + sock_wake_async(rcu_dereference(sk->sk_wq), how, band); +} + /* Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might * need sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak. * Note: for send buffers, TCP works better if we can build two skbs at diff --git a/net/atm/common.c b/net/atm/common.c index 2a1ec014e901d6549732e7bce35bce6a9eb467e0..9b75699992ff9244470c143433f444fb9d46c3b2 100644 --- a/net/atm/common.c +++ b/net/atm/common.c @@ -116,7 +116,7 @@ static void vcc_write_space(struct sock *sk) if (skwq_has_sleeper(wq)) wake_up_interruptible(&wq->wait); - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); } rcu_read_unlock(); diff --git a/net/core/sock.c b/net/core/sock.c index 43bf3818c19e829b47d3989d36e2e1b3bf985438..b9203fccaf1e29ba8e5f48b44987abb79f28fc60 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3338,7 +3338,7 @@ static void sock_def_error_report(struct sock *sk) wq = rcu_dereference(sk->sk_wq); if (skwq_has_sleeper(wq)) wake_up_interruptible_poll(&wq->wait, EPOLLERR); - sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR); + sk_wake_async_rcu(sk, SOCK_WAKE_IO, POLL_ERR); rcu_read_unlock(); } @@ -3353,7 +3353,7 @@ void sock_def_readable(struct sock *sk) if (skwq_has_sleeper(wq)) wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI | EPOLLRDNORM | EPOLLRDBAND); - sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); + sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); rcu_read_unlock(); } @@ -3373,7 +3373,7 @@ static void sock_def_write_space(struct sock *sk) EPOLLWRNORM | EPOLLWRBAND); /* Should agree with poll, otherwise some programs break */ - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); } rcu_read_unlock(); @@ -3398,7 +3398,7 @@ static void sock_def_write_space_wfree(struct sock *sk) EPOLLWRNORM | EPOLLWRBAND); /* Should agree with poll, otherwise some programs break */ - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); } } diff --git a/net/dccp/output.c b/net/dccp/output.c index fd2eb148d24de4d1b9e40c6721577ed7f11b5a6c..5c2e24f3c39b7ff4ee1d5d96d5e406c96609a022 100644 --- a/net/dccp/output.c +++ b/net/dccp/output.c @@ -204,7 +204,7 @@ void dccp_write_space(struct sock *sk) wake_up_interruptible(&wq->wait); /* Should agree with poll, otherwise some programs break */ if (sock_writeable(sk)) - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); rcu_read_unlock(); } diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 5dfbe4499c0f89f94af9ee1fb64559dd672c1439..4119e74fee02b3930075fe5b00c0fc753a620149 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1544,7 +1544,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) INDIRECT_CALL_1(sk->sk_data_ready, sock_def_readable, sk); else - sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); + sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); } busylock_release(busy); return 0; diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c index 7c8c3adcac6e94379360ef6e609c48e3b396ceaa..c951bb9cc2e044249ff7e4f86470b4035d60aeaa 100644 --- a/net/iucv/af_iucv.c +++ b/net/iucv/af_iucv.c @@ -184,7 +184,7 @@ static void iucv_sock_wake_msglim(struct sock *sk) wq = rcu_dereference(sk->sk_wq); if (skwq_has_sleeper(wq)) wake_up_interruptible_all(&wq->wait); - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); rcu_read_unlock(); } diff --git a/net/rxrpc/af_rxrpc.c b/net/rxrpc/af_rxrpc.c index 5222bc97d192e05e2169dcf5f548fdeb98e6b07b..f4844683e12039d636253cb06f622468593487eb 100644 --- a/net/rxrpc/af_rxrpc.c +++ b/net/rxrpc/af_rxrpc.c @@ -65,7 +65,7 @@ static void rxrpc_write_space(struct sock *sk) if (skwq_has_sleeper(wq)) wake_up_interruptible(&wq->wait); - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); } rcu_read_unlock(); } diff --git a/net/sctp/socket.c b/net/sctp/socket.c index c67679a41044fc8e801d175b235249f2c8b99dc0..e416b6d3d2705286d3e5af18b2314bceacfb98b1 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -9276,7 +9276,7 @@ void sctp_data_ready(struct sock *sk) if (skwq_has_sleeper(wq)) wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLRDNORM | EPOLLRDBAND); - sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); + sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); rcu_read_unlock(); } diff --git a/net/smc/smc_rx.c b/net/smc/smc_rx.c index 9a2f3638d161d2ff7d7261835a5b13be63b11701..f0cbe77a80b44046b880e5a7107f535507c76c7c 100644 --- a/net/smc/smc_rx.c +++ b/net/smc/smc_rx.c @@ -42,10 +42,10 @@ static void smc_rx_wake_up(struct sock *sk) if (skwq_has_sleeper(wq)) wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI | EPOLLRDNORM | EPOLLRDBAND); - sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); + sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); if ((sk->sk_shutdown == SHUTDOWN_MASK) || (sk->sk_state == SMC_CLOSED)) - sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); + sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_HUP); rcu_read_unlock(); } diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 5b41e2321209ae0a17ac97d7214eefd252ec0180..ee382cf55f2016d19e600b6fde75da12b53bea09 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -546,7 +546,7 @@ static void unix_write_space(struct sock *sk) if (skwq_has_sleeper(wq)) wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND); - sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT); } rcu_read_unlock(); }