mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Egor Shalashnov <shalasheg@gmail.com>
Cc: linux-mm@kvack.org, dennis@kernel.org, tj@kernel.org,
	cl@gentwo.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] percpu_ref: replace the global switch lock with hashed locks
Date: Sun, 29 Nov 2026 12:04:57 -0800	[thread overview]
Message-ID: <20261129120457.eb0a31459b5f61f2c20d14a7@linux-foundation.org> (raw)
In-Reply-To: <c54762a0-ad7a-47ca-b260-29f44c4e1a33@gmail.com>

On Sun, 29 Nov 2026 17:10:59 +0300 Egor Shalashnov <shalasheg@gmail.com> wrote:

> Currently, percpu_ref uses the global percpu_ref_switch_lock to
> protect the mode switching operation. When multiple percpu_ref
> perform mode switching at the same time, the lock may become a
> performance bottleneck.
> 
> This patch introduces a hashed array of 64 cacheline-separated
> spinlocks to fix this issue without introducing a separate
> spinlock for each percpu_ref.

Nice, thanks.

Please summarize the microbenchmark speedups in the changelog?

And briefly describe the real-world workload - tell readers "how come
Egor is the first person to report this".

It would be nice to summarize observed improvements in that workload,
but I wouldn't spend a lot of time over this - the patch will obviously
help a lot.

> --- a/lib/percpu-refcount.c
> +++ b/lib/percpu-refcount.c
> @@ -36,7 +39,26 @@
>  
>  #define PERCPU_COUNT_BIAS	(1LU << (BITS_PER_LONG - 1))
>  
> -static DEFINE_SPINLOCK(percpu_ref_switch_lock);
> +#define NR_LOCKS	64

Let's remember CONFIG_SMP=n.  I guess making this "1" on !SMP will
sufficiently take care of this.

Also

#else
static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
{
	return &percpu_switch_lock[0].lock;
}
#endif	/* CONFIG_SMP */

I suspect/hope gcc will do exactly this when NR_LOCKS=1, but not if we
switch lock_addr() to hash_ptr() (below).

> +
> +/*
> + * Ensure each lock is in a separate cacheline.
> + */
> +static union {
> +	/* Serializes mode switching and teardown within this hash bucket. */
> +	spinlock_t lock;
> +	char pad[L1_CACHE_BYTES];
> +} percpu_switch_lock[NR_LOCKS] __cacheline_aligned_in_smp;

I agree with Sashiko ;) Can we statically initialize this at compile
time and remove init_percpu_ref_lock().

L1_CACHE_BYTES is too small on machines where SMP_CACHE_BYTES is larger
(ARC and sparc64) - using SMP_CACHE_BYTES here would address that.

I think this:

static struct {
	spinlock_t lock;
} ____cacheline_aligned_in_smp percpu_switch_lock[NR_LOCKS] = {
	[0 ... NR_LOCKS - 1] = {
		.lock = __SPIN_LOCK_UNLOCKED(percpu_switch_lock.lock),
	},
};

will address these things.

> +static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
> +{
> +	unsigned long addr = (unsigned long)ref;
> +
> +	addr >>= L1_CACHE_SHIFT;
> +	addr ^= (addr >> 8) ^ (addr >> 16);
> +	return &percpu_switch_lock[addr & (NR_LOCKS - 1)].lock;
> +}

<ai>The hash is correct for serialization, but it omits address bits 12
and 13 with 64-byte L1 lines.  Four refs at the same offset in
consecutive 4 KiB pages can therefore collide.</ai>

Using hash_ptr() here should address.

      reply	other threads:[~2026-11-29 20:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 14:10 Egor Shalashnov
2026-11-29 20:04 ` Andrew Morton [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=20261129120457.eb0a31459b5f61f2c20d14a7@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=dennis@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=shalasheg@gmail.com \
    --cc=tj@kernel.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

all inboxes | Powered by JetHome®