All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] percpu_ref: deadlock when release calls exit on an atomic ref
@ 2026-11-30 16:47 Egor Shalashnov
  2026-12-01 16:48 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Egor Shalashnov @ 2026-11-30 16:47 UTC (permalink / raw)
  To: linux-mm; +Cc: akpm, dennis, tj, cl, linux-kernel

Hi,

After implementing hashed switch locks, I continued experimenting
with percpu_ref and came across a deadlock when killing a ref that
is already in atomic mode and whose release callback calls
percpu_ref_exit(). This also reproduces with the original global
switch lock.

A minimal example is:

static void release(struct percpu_ref *ref)
{
	percpu_ref_exit(ref);
}

static int reproduce(void)
{
	struct percpu_ref ref;
	int ret;

	ret = percpu_ref_init(&ref, release, PERCPU_REF_INIT_ATOMIC,
			      GFP_KERNEL);
	if (ret)
		return ret;

	percpu_ref_kill(&ref);
	return 0;
}

percpu_ref_kill_and_confirm() holds percpu_ref_switch_lock while
dropping the initial reference. With no additional references,
percpu_ref_put() decrements the count from 1 to 0 and invokes
release() synchronously. percpu_ref_exit() then tries to acquire
the same lock:

percpu_ref_kill()
  percpu_ref_kill_and_confirm()
    spin_lock_irqsave()
    percpu_ref_put()
      release()
        percpu_ref_exit()
          spin_lock_irqsave()  /* recursive acquisition */

The same situation can arise after explicitly switching a ref
to atomic mode. I have not identified an in-tree caller that
triggers this exact sequence, but the documentation 
for percpu_ref_exit() explicitly says:

  "The usual places to invoke this function from are the
   @ref->release() callback or in init failure path [...]"

When kill starts from percpu mode, the transition to atomic mode
takes an extra reference, which is dropped from the RCU callback.
This prevents the put in percpu_ref_kill_and_confirm() from
invoking release() while holding the switch lock. No such extra
reference is taken when the ref is already in atomic mode.

My codex agent suggested the following fix:
diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
--- a/lib/percpu-refcount.c
+++ b/lib/percpu-refcount.c
@@ -393,9 +393,10 @@

 	ref->percpu_count_ptr |= __PERCPU_REF_DEAD;
 	__percpu_ref_switch_mode(ref, confirm_kill);
+
+	spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
+
 	percpu_ref_put(ref);
-
-	spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
 }
 EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);

However, I'm not sure whether moving the put outside the lock
could introduce races with concurrent mode switching or
reinitialization. Is there a reason the initial reference needs
to be dropped while holding the switch lock?

If this usage is intentionally unsupported, should the
documentation explicitly warn that calling percpu_ref_exit()
from the release callback can deadlock when kill drops the last
reference on a ref already in atomic mode?

Thanks,
Egor


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFC] percpu_ref: deadlock when release calls exit on an atomic ref
  2026-11-30 16:47 [RFC] percpu_ref: deadlock when release calls exit on an atomic ref Egor Shalashnov
@ 2026-12-01 16:48 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 16:48 UTC (permalink / raw)
  To: Egor Shalashnov, linux-mm; +Cc: akpm, dennis, tj, cl

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] percpu_ref: missing Signed-off-by trailer in commit message
- [Low] percpu_ref: missing Fixes trailer on bug-fix commit
--

commit a5a8e856aaed90127d6381609b067a69b8547fd2
Author: Egor Shalashnov <shalasheg@gmail.com>

> percpu_ref: deadlock when release calls exit on an atomic ref
>
> Hi,
>
> After implementing hashed switch locks, I continued experimenting
> with percpu_ref and came across a deadlock when killing a ref that
> is already in atomic mode and whose release callback calls
> percpu_ref_exit(). This also reproduces with the original global
> switch lock.
>
> A minimal example is:
>
> static void release(struct percpu_ref *ref)
> {
>         percpu_ref_exit(ref);
> }
>
> static int reproduce(void)
> {
>         struct percpu_ref ref;
>         int ret;
>
>         ret = percpu_ref_init(&ref, release, PERCPU_REF_INIT_ATOMIC,
>                               GFP_KERNEL);
>         if (ret)
>                 return ret;
>
>         percpu_ref_kill(&ref);
>         return 0;
> }
>
> percpu_ref_kill_and_confirm() holds percpu_ref_switch_lock while
> dropping the initial reference. With no additional references,
> percpu_ref_put() decrements the count from 1 to 0 and invokes
> release() synchronously. percpu_ref_exit() then tries to acquire
> the same lock:
>
> percpu_ref_kill()
>   percpu_ref_kill_and_confirm()
>     spin_lock_irqsave()
>     percpu_ref_put()
>       release()
>         percpu_ref_exit()
>           spin_lock_irqsave()  /* recursive acquisition */
>
> The same situation can arise after explicitly switching a ref
> to atomic mode. I have not identified an in-tree caller that
> triggers this exact sequence, but the documentation
> for percpu_ref_exit() explicitly says:
>
>   "The usual places to invoke this function from are the
>    @ref->release() callback or in init failure path [...]"
>
> When kill starts from percpu mode, the transition to atomic mode
> takes an extra reference, which is dropped from the RCU callback.
> This prevents the put in percpu_ref_kill_and_confirm() from
> invoking release() while holding the switch lock. No such extra
> reference is taken when the ref is already in atomic mode.
>
> My codex agent suggested the following fix:

[Severity: High]
The commit message is missing a Signed-off-by: trailer, and it still
contains email greeting and lead-in text ("Hi,", "My codex agent
suggested the following fix:") instead of a formatted changelog.

[Severity: Low]
Since this fixes the recursive percpu_ref_switch_lock acquisition between
percpu_ref_kill_and_confirm() and percpu_ref_exit(), should it include:

  Fixes: 33e465ce7cb3 ("percpu_ref: allow operation mode switching operations to be called concurrently")
  Fixes: 2b0d3d3e4fcf ("percpu_ref: reduce memory footprint of percpu_ref in fast path")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/c94764bc-a8c9-4771-a649-ccebded0844e@gmail.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 16:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:47 [RFC] percpu_ref: deadlock when release calls exit on an atomic ref Egor Shalashnov
2026-12-01 16:48 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.