BPF List
 help / color / mirror / Atom feed
* [PATCH 0/2] percpu_ref: make gets, puts and releases NMI safe
@ 2026-12-04 22:40 Shakeel Butt
  2026-12-04 22:40 ` [PATCH 1/2] percpu_ref: make get and put NMI safe where this_cpu ops are not Shakeel Butt
  2026-12-04 22:40 ` [PATCH 2/2] percpu_ref: defer the release function when the last put is in NMI Shakeel Butt
  0 siblings, 2 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-12-04 22:40 UTC (permalink / raw)
  To: Dennis Zhou, Tejun Heo, Christoph Lameter
  Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Roman Gushchin,
	Muchun Song, Alexei Starovoitov, David Vernet, Vlastimil Babka,
	cgroups, linux-mm, bpf, linux-kernel, Meta kernel team

percpu_ref gets and puts can run in NMI. BPF tracing programs take and
drop cgroup references there, and memcg charging from kmalloc_nolock()
and alloc_pages_nolock() takes and drops css and objcg references.

This series fixes two problems:

1. Where this_cpu ops are not NMI safe, as on powerpc and sparc64, a get
   or put in NMI can interrupt one on the same counter, and an update is
   lost. Patch 1 keeps NMI updates in a separate atomic counter and adds
   it in when the ref switches to atomic mode.

2. On any architecture, the last put can happen in NMI, but release
   functions are not NMI safe. Patch 2 runs the release function from
   irq_work when the count drops to zero in NMI.

I fixed this in percpu_ref because the problem comes from both cgroup
core (BPF cgroup kptrs) and memcg. The other option is to defer the
release in css_release() and obj_cgroup_release() separately.

Cost: patch 2 puts its list node in the same memory as ->rcu, so struct
percpu_ref_data does not grow. Patch 1 adds a long to it and an in_nmi()
test to gets and puts, but only where atomics are NMI safe and this_cpu
ops are not. Nothing changes on x86, arm64, s390 or loongarch.

Not covered:

- Architectures without NMI safe atomics, such as mips. memcg already
  treats them as NMI unsafe (CONFIG_MEMCG_NMI_UNSAFE).
- Deadlocks outside NMI, when the caller holds a lock that the release
  function needs. For example, a BPF program on the
  workqueue_queue_work tracepoint runs with the pool lock held. If it
  drops the last cgroup reference, queue_work() in css_release() can
  deadlock. percpu_ref can't detect this.

The series is based on mm-nonmm-unstable, which has "percpu_ref:
replace the global switch lock with hashed locks". A separate memcg
series fixes memcg stat updates made in NMI. The two series don't
depend on each other.

Testing:

- Builds with W=1 on x86-64, and with LLVM on ppc64le (4K and 64K
  pages) and ppc32, where the new NMI code is compiled in. pahole shows
  struct percpu_ref_data at 56 bytes on x86-64 (unchanged), 64 on
  ppc64le (was 56) and 32 on ppc32 (was 28).
- An NMI test on x86-64, with ARCH_HAS_NMI_SAFE_THIS_CPU_OPS turned off
  so the new code runs. It enters NMI with a self NMI IPI. Each case ran
  three times:
  - Last put in NMI: the release runs once, from irq_work. Without
    patch 2 it runs in NMI.
  - Gets and puts in NMI in percpu mode: they go to the new counter,
    and the ref is released once, after percpu_ref_kill().
  - A get of LONG_MAX in NMI, put back outside, then percpu_ref_kill():
    one release, after the kill. If the NMI updates go to ->count
    instead, the ref is released during percpu_ref_kill() and again at
    the switch.
- cgroup selftests test_core, test_kmem and test_memcontrol in a VM:
  same results as the base kernel.

Shakeel Butt (2):
  percpu_ref: make get and put NMI safe where this_cpu ops are not
  percpu_ref: defer the release function when the last put is in NMI

 include/linux/percpu-refcount.h | 98 ++++++++++++++++++++++++++++++---
 lib/percpu-refcount.c           | 63 +++++++++++++++++++--
 2 files changed, 149 insertions(+), 12 deletions(-)


base-commit: d2cb9e321d95e790c9e7517b4c556e9d3f592cfe
-- 
2.53.1-Meta


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

end of thread, other threads:[~2026-12-04 23:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 22:40 [PATCH 0/2] percpu_ref: make gets, puts and releases NMI safe Shakeel Butt
2026-12-04 22:40 ` [PATCH 1/2] percpu_ref: make get and put NMI safe where this_cpu ops are not Shakeel Butt
2026-12-04 22:40 ` [PATCH 2/2] percpu_ref: defer the release function when the last put is in NMI Shakeel Butt
2026-12-04 23:00   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox