From: Mateusz Guzik <mjguzik@gmail.com>
To: dennis@kernel.org
Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH] percpu_counter: reduce i-cache footprint of percpu_counter_add_batch() fast path
Date: Wed, 19 Nov 2025 22:33:58 +0100 [thread overview]
Message-ID: <CAGudoHGs+3A0TQ9q2jNGuphT=nDbfy9jBrF0-FHtV62E+cz19w@mail.gmail.com> (raw)
In-Reply-To: <20251119210820.2959128-1-mjguzik@gmail.com>
So I just verified gcc 15 has this problem fixed. So one can argue the
issue will go away on its own given enough time, hence the patch can
be dropped if there are any concerns.
On Wed, Nov 19, 2025 at 10:08 PM Mateusz Guzik <mjguzik@gmail.com> wrote:
>
> When compiled with gcc 14.2 for the x86-64 architecture with ORC frame
> unwinder the fast path still has the most unfortunate size of 66 bytes,
> in part from register spilling to falicitate the fallback.
>
> Moving it out solves the problem by keeping it just below 64 bytes.
>
> Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
> ---
> lib/percpu_counter.c | 30 ++++++++++++++++++++----------
> 1 file changed, 20 insertions(+), 10 deletions(-)
>
> diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c
> index 2891f94a11c6..0cf6f1101903 100644
> --- a/lib/percpu_counter.c
> +++ b/lib/percpu_counter.c
> @@ -89,24 +89,34 @@ EXPORT_SYMBOL(percpu_counter_set);
> * Safety against interrupts is achieved in 2 ways:
> * 1. the fast path uses local cmpxchg (note: no lock prefix)
> * 2. the slow path operates with interrupts disabled
> + *
> + * Slowpath is implemented as a separate routine to reduce register spillage by gcc.
> */
> -void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
> +static void noinline percpu_counter_add_batch_slowpath(struct percpu_counter *fbc,
> + s64 amount, s32 batch)
> {
> s64 count;
> unsigned long flags;
>
> + raw_spin_lock_irqsave(&fbc->lock, flags);
> + /*
> + * Note: by now we might have migrated to another CPU or the value
> + * might have changed.
> + */
> + count = __this_cpu_read(*fbc->counters);
> + fbc->count += count + amount;
> + __this_cpu_sub(*fbc->counters, count);
> + raw_spin_unlock_irqrestore(&fbc->lock, flags);
> +}
> +
> +void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
> +{
> + s64 count;
> +
> count = this_cpu_read(*fbc->counters);
> do {
> if (unlikely(abs(count + amount) >= batch)) {
> - raw_spin_lock_irqsave(&fbc->lock, flags);
> - /*
> - * Note: by now we might have migrated to another CPU
> - * or the value might have changed.
> - */
> - count = __this_cpu_read(*fbc->counters);
> - fbc->count += count + amount;
> - __this_cpu_sub(*fbc->counters, count);
> - raw_spin_unlock_irqrestore(&fbc->lock, flags);
> + percpu_counter_add_batch_slowpath(fbc, amount, batch);
> return;
> }
> } while (!this_cpu_try_cmpxchg(*fbc->counters, &count, count + amount));
> --
> 2.48.1
>
prev parent reply other threads:[~2025-11-19 21:34 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-19 21:08 Mateusz Guzik
2025-11-19 21:33 ` Mateusz Guzik [this message]
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='CAGudoHGs+3A0TQ9q2jNGuphT=nDbfy9jBrF0-FHtV62E+cz19w@mail.gmail.com' \
--to=mjguzik@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=dennis@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/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