diff mbox series

[net] net: fix sk_memory_allocated_{add|sub} vs softirqs

Message ID 20240421175248.1692552-1-edumazet@google.com (mailing list archive)
State Accepted
Commit 3584718cf2ec7e79b6814f2596dcf398c5fb2eca
Delegated to: Netdev Maintainers
Headers show
Series [net] net: fix sk_memory_allocated_{add|sub} vs softirqs | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3394 this patch: 3394
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 1 blamed authors not CCed: shakeel.butt@linux.dev; 1 maintainers not CCed: shakeel.butt@linux.dev
netdev/build_clang success Errors and warnings before: 980 this patch: 980
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 Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 3584 this patch: 3584
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 52 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 4 this patch: 4
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-24--00-00 (tests: 994)

Commit Message

Eric Dumazet April 21, 2024, 5:52 p.m. UTC
Jonathan Heathcote reported a regression caused by blamed commit
on aarch64 architecture.

x86 happens to have irq-safe __this_cpu_add_return()
and __this_cpu_sub(), but this is not generic.

I think my confusion came from "struct sock" argument,
because these helpers are called with a locked socket.
But the memory accounting is per-proto (and per-cpu after
the blamed commit). We might cleanup these helpers later
to directly accept a "struct proto *proto" argument.

Switch to this_cpu_add_return() and this_cpu_xchg()
operations, and get rid of preempt_disable()/preempt_enable() pairs.

Fast path becomes a bit faster as a result :)

Many thanks to Jonathan Heathcote for his awesome report and
investigations.

Fixes: 3cd3399dd7a8 ("net: implement per-cpu reserves for memory_allocated")
Reported-by: Jonathan Heathcote <jonathan.heathcote@bbc.co.uk>
Closes: https://lore.kernel.org/netdev/VI1PR01MB42407D7947B2EA448F1E04EFD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com/
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Soheil Hassas Yeganeh <soheil@google.com>
---
 include/net/sock.h | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

Comments

Soheil Hassas Yeganeh April 21, 2024, 9:06 p.m. UTC | #1
On Sun, Apr 21, 2024 at 1:52 PM Eric Dumazet <edumazet@google.com> wrote:
>
> Jonathan Heathcote reported a regression caused by blamed commit
> on aarch64 architecture.
>
> x86 happens to have irq-safe __this_cpu_add_return()
> and __this_cpu_sub(), but this is not generic.
>
> I think my confusion came from "struct sock" argument,
> because these helpers are called with a locked socket.
> But the memory accounting is per-proto (and per-cpu after
> the blamed commit). We might cleanup these helpers later
> to directly accept a "struct proto *proto" argument.
>
> Switch to this_cpu_add_return() and this_cpu_xchg()
> operations, and get rid of preempt_disable()/preempt_enable() pairs.
>
> Fast path becomes a bit faster as a result :)
>
> Many thanks to Jonathan Heathcote for his awesome report and
> investigations.
>
> Fixes: 3cd3399dd7a8 ("net: implement per-cpu reserves for memory_allocated")
> Reported-by: Jonathan Heathcote <jonathan.heathcote@bbc.co.uk>
> Closes: https://lore.kernel.org/netdev/VI1PR01MB42407D7947B2EA448F1E04EFD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com/
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: Soheil Hassas Yeganeh <soheil@google.com>

Acked-by: Soheil Hassas Yeganeh <soheil@google.com>

Very nice catch! and thank you for the fix!

> ---
>  include/net/sock.h | 38 ++++++++++++++++++++------------------
>  1 file changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/include/net/sock.h b/include/net/sock.h
> index f57bfd8a2ad2deaedf3f351325ab9336ae040504..b4b553df7870c0290ae632c51828ad7161ba332d 100644
> --- a/include/net/sock.h
> +++ b/include/net/sock.h
> @@ -1410,32 +1410,34 @@ sk_memory_allocated(const struct sock *sk)
>  #define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT))
>  extern int sysctl_mem_pcpu_rsv;
>
> +static inline void proto_memory_pcpu_drain(struct proto *proto)
> +{
> +       int val = this_cpu_xchg(*proto->per_cpu_fw_alloc, 0);
> +
> +       if (val)
> +               atomic_long_add(val, proto->memory_allocated);
> +}
> +
>  static inline void
> -sk_memory_allocated_add(struct sock *sk, int amt)
> +sk_memory_allocated_add(const struct sock *sk, int val)
>  {
> -       int local_reserve;
> +       struct proto *proto = sk->sk_prot;
>
> -       preempt_disable();
> -       local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
> -       if (local_reserve >= READ_ONCE(sysctl_mem_pcpu_rsv)) {
> -               __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
> -               atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
> -       }
> -       preempt_enable();
> +       val = this_cpu_add_return(*proto->per_cpu_fw_alloc, val);
> +
> +       if (unlikely(val >= READ_ONCE(sysctl_mem_pcpu_rsv)))
> +               proto_memory_pcpu_drain(proto);
>  }
>
>  static inline void
> -sk_memory_allocated_sub(struct sock *sk, int amt)
> +sk_memory_allocated_sub(const struct sock *sk, int val)
>  {
> -       int local_reserve;
> +       struct proto *proto = sk->sk_prot;
>
> -       preempt_disable();
> -       local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
> -       if (local_reserve <= -READ_ONCE(sysctl_mem_pcpu_rsv)) {
> -               __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
> -               atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
> -       }
> -       preempt_enable();
> +       val = this_cpu_sub_return(*proto->per_cpu_fw_alloc, val);
> +
> +       if (unlikely(val <= -READ_ONCE(sysctl_mem_pcpu_rsv)))
> +               proto_memory_pcpu_drain(proto);
>  }
>
>  #define SK_ALLOC_PERCPU_COUNTER_BATCH 16
> --
> 2.44.0.769.g3c40516874-goog
>
Shakeel Butt April 22, 2024, 7:48 p.m. UTC | #2
On Sun, Apr 21, 2024 at 05:52:48PM +0000, Eric Dumazet wrote:
> Jonathan Heathcote reported a regression caused by blamed commit
> on aarch64 architecture.
> 
> x86 happens to have irq-safe __this_cpu_add_return()
> and __this_cpu_sub(), but this is not generic.
> 
> I think my confusion came from "struct sock" argument,
> because these helpers are called with a locked socket.
> But the memory accounting is per-proto (and per-cpu after
> the blamed commit). We might cleanup these helpers later
> to directly accept a "struct proto *proto" argument.
> 
> Switch to this_cpu_add_return() and this_cpu_xchg()
> operations, and get rid of preempt_disable()/preempt_enable() pairs.
> 
> Fast path becomes a bit faster as a result :)
> 
> Many thanks to Jonathan Heathcote for his awesome report and
> investigations.
> 
> Fixes: 3cd3399dd7a8 ("net: implement per-cpu reserves for memory_allocated")
> Reported-by: Jonathan Heathcote <jonathan.heathcote@bbc.co.uk>
> Closes: https://lore.kernel.org/netdev/VI1PR01MB42407D7947B2EA448F1E04EFD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com/
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: Soheil Hassas Yeganeh <soheil@google.com>

Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
patchwork-bot+netdevbpf@kernel.org April 24, 2024, 2:20 a.m. UTC | #3
Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Sun, 21 Apr 2024 17:52:48 +0000 you wrote:
> Jonathan Heathcote reported a regression caused by blamed commit
> on aarch64 architecture.
> 
> x86 happens to have irq-safe __this_cpu_add_return()
> and __this_cpu_sub(), but this is not generic.
> 
> I think my confusion came from "struct sock" argument,
> because these helpers are called with a locked socket.
> But the memory accounting is per-proto (and per-cpu after
> the blamed commit). We might cleanup these helpers later
> to directly accept a "struct proto *proto" argument.
> 
> [...]

Here is the summary with links:
  - [net] net: fix sk_memory_allocated_{add|sub} vs softirqs
    https://git.kernel.org/netdev/net/c/3584718cf2ec

You are awesome, thank you!
diff mbox series

Patch

diff --git a/include/net/sock.h b/include/net/sock.h
index f57bfd8a2ad2deaedf3f351325ab9336ae040504..b4b553df7870c0290ae632c51828ad7161ba332d 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1410,32 +1410,34 @@  sk_memory_allocated(const struct sock *sk)
 #define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT))
 extern int sysctl_mem_pcpu_rsv;
 
+static inline void proto_memory_pcpu_drain(struct proto *proto)
+{
+	int val = this_cpu_xchg(*proto->per_cpu_fw_alloc, 0);
+
+	if (val)
+		atomic_long_add(val, proto->memory_allocated);
+}
+
 static inline void
-sk_memory_allocated_add(struct sock *sk, int amt)
+sk_memory_allocated_add(const struct sock *sk, int val)
 {
-	int local_reserve;
+	struct proto *proto = sk->sk_prot;
 
-	preempt_disable();
-	local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
-	if (local_reserve >= READ_ONCE(sysctl_mem_pcpu_rsv)) {
-		__this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
-		atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
-	}
-	preempt_enable();
+	val = this_cpu_add_return(*proto->per_cpu_fw_alloc, val);
+
+	if (unlikely(val >= READ_ONCE(sysctl_mem_pcpu_rsv)))
+		proto_memory_pcpu_drain(proto);
 }
 
 static inline void
-sk_memory_allocated_sub(struct sock *sk, int amt)
+sk_memory_allocated_sub(const struct sock *sk, int val)
 {
-	int local_reserve;
+	struct proto *proto = sk->sk_prot;
 
-	preempt_disable();
-	local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
-	if (local_reserve <= -READ_ONCE(sysctl_mem_pcpu_rsv)) {
-		__this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
-		atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
-	}
-	preempt_enable();
+	val = this_cpu_sub_return(*proto->per_cpu_fw_alloc, val);
+
+	if (unlikely(val <= -READ_ONCE(sysctl_mem_pcpu_rsv)))
+		proto_memory_pcpu_drain(proto);
 }
 
 #define SK_ALLOC_PERCPU_COUNTER_BATCH 16