netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Dumazet <edumazet@google.com>
To: Jonathan Heathcote <jonathan.heathcote@bbc.co.uk>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	 "regressions@lists.linux.dev" <regressions@lists.linux.dev>
Subject: Re: [REGRESSION] sk_memory_allocated counter leaking on aarch64
Date: Fri, 19 Apr 2024 19:31:58 +0200	[thread overview]
Message-ID: <CANn89iJDxA5nCTiw9SzHVfzhz2F124Z_PMEf_oJz3DGAp=UeOQ@mail.gmail.com> (raw)
In-Reply-To: <VI1PR01MB4240EBA3CE9986FC6A0A9C7DD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com>

On Fri, Apr 19, 2024 at 5:59 PM Jonathan Heathcote
<jonathan.heathcote@bbc.co.uk> wrote:
>
> I'm very sorry but I won't be able to give this a try until Wednesday next week (I'm currently working part time) but I'll give this a go first-thing. Thank you very much for your swift response!

SGTM, I rewrote the patch to remove the not needed
preempt_disable()/preempt_enable()

I will test it soon.

diff --git a/include/net/sock.h b/include/net/sock.h
index f57bfd8a2ad2deaedf3f351325ab9336ae040504..bae62604c5ffc8a3ecbe3e996b87c9fb25914c0f
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 sk_memory_pcpu_drain(const 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)
 {
-       int local_reserve;
+       int val;

-       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(*sk->sk_prot->per_cpu_fw_alloc, amt);
+
+       if (unlikely(val >= READ_ONCE(sysctl_mem_pcpu_rsv)))
+               sk_memory_pcpu_drain(sk->sk_prot);
 }

 static inline void
 sk_memory_allocated_sub(struct sock *sk, int amt)
 {
-       int local_reserve;
+       int val;

-       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(*sk->sk_prot->per_cpu_fw_alloc, amt);
+
+       if (unlikely(val <= -READ_ONCE(sysctl_mem_pcpu_rsv)))
+               sk_memory_pcpu_drain(sk->sk_prot);
 }

 #define SK_ALLOC_PERCPU_COUNTER_BATCH 16

  parent reply	other threads:[~2024-04-19 17:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-19 14:46 [REGRESSION] sk_memory_allocated counter leaking on aarch64 Jonathan Heathcote
2024-04-19 15:21 ` Eric Dumazet
     [not found]   ` <VI1PR01MB4240EBA3CE9986FC6A0A9C7DD10D2@VI1PR01MB4240.eurprd01.prod.exchangelabs.com>
2024-04-19 17:31     ` Eric Dumazet [this message]
2024-04-24 13:39       ` Jonathan Heathcote

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='CANn89iJDxA5nCTiw9SzHVfzhz2F124Z_PMEf_oJz3DGAp=UeOQ@mail.gmail.com' \
    --to=edumazet@google.com \
    --cc=jonathan.heathcote@bbc.co.uk \
    --cc=netdev@vger.kernel.org \
    --cc=regressions@lists.linux.dev \
    /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;
as well as URLs for NNTP newsgroup(s).