Skip to contentSkip to conversation

objpool: fix nested pushes from NMI context

PATCH In review Shashank Mohan Jain 3 participants 6 messages Sep 28 – Sep 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover objpool: fix nested pushes from NMI context

objpool_push() runs with interrupts disabled, but a kretprobe that returns in NMI context can push to the same per-CPU slot in the middle of it. With rethook-based kretprobes (and, before v6.14, fprobe) this is reachable since kretprobes moved to objpool in v6.7. The nested push publishes slot->last past the unwritten entry of the interrupted push, so a pop on another CPU can take a NULL or stale pointer, which hands an object out twice and loses another, and slot->last can move backwards, after which a pop on the owning CPU spins with interrupts disabled.

Patch 1 publishes the entries in order with a cmpxchg() on slot->last. Patch 2 adds a KUnit test in which a pinned hard hrtimer stands in for the NMI. It fails without patch 1 and passes with it.

The race was found with a TLA+ model of __objpool_try_add_slot() and __objpool_try_get_slot() on one slot: a task push that an NMI push can interrupt at every step, pops on another CPU, and recycled objects. Memory is sequentially consistent, with one level of nesting. TLC finds the bug in current code, no violation with patch 1 (up to five objects, four task pushes, three nested pushes, five remote pops), and shows that a plain-store version of the fix lets last fall behind head. The model (Objpool.tla and its .cfg files) can be posted if that is useful.

No earlier report of this was found: searches of patchwork (objpool, rethook, kretprobe, objpool_push) and of the linux-kernel archive on marc.info (objpool NMI, objpool_push, rethook NMI, kretprobe NMI) turned up nothing related. lore was not searched.

Testing is described under "---" in each patch. In short: KUnit on UML x86_64 (4 CPUs, 3 runs before and after, plus 1 CPU and CONFIG_SMP=n), W=1 builds for x86_64 and i386, checkpatch --strict. Not tested: real NMIs through a kretprobe on hardware, weakly ordered architectures, and performance in the kernel.

This series was prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5). The code, the test, the changelogs and this cover letter were written with the assistant; the TLA+ model checker TLC found the race.

Shashank Mohan Jain (2):
objpool: keep objpool_push() correct when a push from NMI nests in it lib/tests: add KUnit test for nested objpool pushes

 MAINTAINERS               |   1 +
 include/linux/objpool.h   |  37 ++++-
 lib/Kconfig.debug         |  13 ++
 lib/tests/Makefile        |   1 +
 lib/tests/objpool_kunit.c | 329 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 373 insertions(+), 8 deletions(-)
 create mode 100644 lib/tests/objpool_kunit.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 objpool: keep objpool_push() correct when a push from NMI nests in it

objpool_push() adds an object to the slot of the local CPU with interrupts disabled. It reserves an entry with a cmpxchg() on slot->tail, writes the entry, and publishes it with smp_store_release(&slot->last, tail + 1).

A push from NMI context can still interrupt it and push to the same slot. kretprobes may run in NMI context since commit e03b4a084ea6 ("kprobes: Remove NMI context check"). At the time, kretprobe instances came from a CAS-based lockless freelist, which tolerates that. Commit 4bbd93455659 ("kprobes: kretprobe scalability improvement") moved kretprobes and rethook to objpool.

With rethook, rethook_trampoline_handler() recycles instances with objpool_push() after the user handler has run, when no kprobe is marked running anymore; rethook_flush_task() does the same. An NMI that arrives during such a push and runs a function probed by the same kretprobe takes an instance in pre_handler_kretprobe(), and when the function returns inside the NMI, pushes it back to the same slot. This needs a kretprobe on a function that runs both in NMI context and outside it, for instance one that perf calls from the PMU NMI handler on x86. Before v6.14, fprobe also used rethook and could push from NMI the same way, with one pool shared by all functions of an fprobe.

kretprobes without rethook are not affected: kretprobe_trampoline_handler() and kprobe_flush_task() run under kprobe_busy_begin(), so a kprobe hit in NMI context is counted as missed.

If the NMI lands between the reservation and the publication of the interrupted push, it reserves the next entry, writes it and moves slot->last past the entry of the interrupted push, which is not written yet:

  CPU0 (irqs off)          CPU0 NMI              CPU1
  ---------------          --------              ----

reserve entry t
reserve entry t+1
write entry t+1
last = t+2
pop: t < last, returns entries[t]
write entry t
last = t+1

This has three effects:

- A pop on another CPU can take entry t before it is written. It gets whatever the ring held at that position: NULL, or a stale pointer to an object that was popped earlier and may still be in use, so the same object is handed out twice. The object pushed at t can never be popped again, so it is lost to the pool.
- The interrupted push moves slot->last backwards. If both entries were popped in between, head is now ahead of last, and __objpool_try_get_slot() spins with interrupts disabled until the next push to that slot. Only the owning CPU pushes there, so a pop on that CPU never comes back.
- Even without a concurrent pop, the entry of the NMI stays invisible until the next push. The WARN_ON_ONCE() sanity check can also fire spuriously, because it compares a snapshot of tail taken before the NMI with head read after it.

During the review of objpool, nested pushes were assumed not to happen because the push runs with interrupts disabled. kretprobes push from NMI context, though.

Publish the entries in order instead:

- A push advances slot->last only while last points at its own entry, meaning all earlier entries are written. It then also publishes the entries of pushes that interrupted it, which have completed by the time it resumes.
- A push that interrupted another one leaves slot->last alone. The interrupted push publishes both entries when it resumes.
- This needs a cmpxchg(). Masami sketched a plain-store version of this catch-up loop during the objpool review [1]. With a plain store, a nested push can take over as soon as last reaches its entry and publish it, and the store of an older tail by the interrupted push then moves last backwards, below head if a remote pop took the entries meanwhile. A pop from NMI on this CPU would then spin forever, as the interrupted push cannot resume to repair last.
- Read head for the sanity check only once the entry is reserved.

Without nesting, the push now costs one cmpxchg() and one extra read of tail instead of one store.

Found with a TLA+ model of the push and pop paths (sequentially consistent memory, one level of nesting), with a task push that an NMI push can interrupt at every step and pops on another CPU. TLC finds pops of unwritten entries, objects handed out twice and last moving backwards with the current code. With this change it finds no violation for up to five objects, four task pushes, three nested pushes and five remote pops. In the same model, the plain-store variant hands out no bad objects but lets last fall behind head.

On 4-CPU UML, the KUnit test added in the next patch lets an hrtimer push while a task push is in progress. Before this change, it leaves 6960 to 7053 entries unpublished in 5 seconds. With a popper on another CPU, it hands out objects twice and loses them until all 30 objects of the task are gone. After this change, none of these happen (3 runs each).

Fixes b4edb8d2d464 ("lib: objpool added: ring-array based lockless MPMC")
Assisted-by LLM TLC

---
Notes (not part of the changelog):

Tested (master 72d3fcf802c4, v7.3-rc5):
- KUnit on UML x86_64 with ncpus=4 (CONFIG_SMP=y, HIGH_RES_TIMERS), with patch 2/2 applied, 3 runs each:
- Without this patch, both cases fail every run. Local case: 6960-7053 unpublished entries (8230-8391 timer pushes nested in a task push). Remote-pop case: 142-187 unpublished entries, 30 double handouts and 30 objects lost (all of the task's objects). The spurious WARN_ON_ONCE() at objpool.h:202 fires in every run.
- With this patch, both cases pass every run: 0 unpublished, 0 double handouts, 0 lost, with 5013-5237 (local) and 32994-35223 (remote pop) real nestings, and no WARN.
- KUnit on UML with ncpus=1: the local case fails without this patch (7112 unpublished entries) and passes with it (4972 nestings); the remote-pop case is skipped. With CONFIG_SMP=n it passes with this patch (72123 nestings).
- W=1 build of lib/objpool.o, lib/test_objpool.o, lib/tests/objpool_kunit.o, kernel/kprobes.o and kernel/trace/rethook.o for x86_64_defconfig and i386_defconfig (plus KPROBES, KRETPROBES, KRETPROBE_ON_RETHOOK, KUNIT, OBJPOOL_KUNIT_TEST=m, TEST_OBJPOOL=m):
no warnings.
- checkpatch.pl --strict: clean apart from the missing Signed-off-by.
- git apply --check of this patch against include/linux/objpool.h of v6.12, v6.18 and v7.2: applies. It was not built or tested there.

Not tested:
- Real NMIs. The failure was not reproduced through a kretprobe on real hardware; an hrtimer on UML stands in for the NMI. The reachability argument (rethook recycles with no kprobe marked running) comes from reading the code. The hard lockup of a pop on the owning CPU follows from the code and the TLA+ model; it was not reproduced.
- Weakly ordered architectures. The pop side still reads slot->last without acquire; that is pre-existing and not addressed here.
- Performance in the kernel. A userspace model of the fast path on an i7-11700F goes from ~14 to ~22 ns per push+pop pair (one more locked cmpxchg); not measured in the kernel. lib/test_objpool.c was not run.
- The Link: message id was taken from patchwork; the lore URL was not opened.

An alternative would be to mark a kprobe busy around the recycle loops in rethook_trampoline_handler() and rethook_flush_task(), as the non-rethook kretprobe path does. That would not cover fprobe before v6.14 and would keep objpool unsafe for pushes from NMI, so this fixes objpool itself.

This patch was prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5): the code and the changelog were written with the assistant. The TLA+ model checker TLC found the race.

 include/linux/objpool.h | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)
include/linux/objpool.h +29 -8
index b713a1fe7521..b86225e32ebe 100644
@@ -193,19 +193,40 @@ __objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu)
 	struct objpool_slot *slot = pool->cpu_slots[cpu];
 	uint32_t head, tail;
 
-	/* loading tail and head as a local snapshot, tail first */
+	/*
+	 * Only the local CPU pushes to its slot, with irqs disabled, but a
+	 * push from NMI context (a kretprobe'd function returning in NMI)
+	 * can interrupt this one at any point.
+	 */
 	tail = READ_ONCE(slot->tail);
+	while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1))
+		;
 
-	do {
-		head = READ_ONCE(slot->head);
-		/* fault caught: something must be wrong */
-		WARN_ON_ONCE(tail - head > pool->nr_objs);
-	} while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1));
+	/*
+	 * fault caught: something must be wrong.  Read head only after the
+	 * reservation: a nested push and a pop on another CPU could have
+	 * moved head past an older snapshot of tail.
+	 */
+	head = READ_ONCE(slot->head);
+	WARN_ON_ONCE(tail - head > pool->nr_objs);
 
 	/* now the tail position is reserved for the given obj */
 	WRITE_ONCE(slot->entries[tail & slot->mask], obj);
-	/* update sequence to make this obj available for pop() */
-	smp_store_release(&slot->last, tail + 1);
+
+	/*
+	 * Update 'last' to make this obj available for pop(), in order:
+	 * 'last' must never move past an entry that is not written yet.
+	 * If this push interrupted another push to this slot, the entry of
+	 * the interrupted push comes first and is not written yet: leave
+	 * 'last' alone, the interrupted push publishes both when it resumes.
+	 * Pushes that interrupted this one have completed by now, so publish
+	 * their entries too.  A plain store is not enough: another nested
+	 * push can take over publishing as soon as 'last' reaches its entry.
+	 */
+	while (try_cmpxchg_release(&slot->last, &tail, tail + 1)) {
+		if (++tail == READ_ONCE(slot->tail))
+			break;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
#4 lore raw

PATCH 2/2 lib/tests: add KUnit test for nested objpool pushes

In reply to Shashank Mohan Jain

objpool_push() only pushes to the slot of the local CPU, but a push from NMI context (a kretprobe'd function returning in NMI) can interrupt it and push to the same slot.

UML has no NMIs. So the test pushes from task context with interrupts enabled, through __objpool_try_add_slot(). A pinned hard hrtimer on the same CPU pushes with objpool_push() every 10 us. That nests the same way an NMI would. HRTIMER_MODE_REL_PINNED_HARD keeps the timer in hardirq context on PREEMPT_RT as well.

- objpool_test_nested_push checks that no entry is left unpublished once the task push returns (slot->last == slot->tail).
- objpool_test_nested_push_remote_pop pops from another CPU with objpool_pop(). It checks that every popped object was in the pool, and that no object is lost from the pool. It is skipped when only one CPU is online.

Only a few timer pushes land inside a task push. The test counts those and fails if there were none, as it would then have tested nothing.

Each case runs for 5 seconds.

Assisted-by LLM

---
Notes (not part of the changelog):

Tested (master 72d3fcf802c4, v7.3-rc5):
- UML x86_64 with ncpus=4, 3 runs without patch 1/2 (both cases fail) and 3 runs with it (both pass). Real nestings per case with patch 1/2:
5013-5237 (local) and 32994-35223 (remote pop).
- UML with ncpus=1: the local case fails without patch 1/2 and passes with it; the remote-pop case is skipped. CONFIG_SMP=n: the local case passes with patch 1/2.
- W=1 build for x86_64 and i386, with OBJPOOL_KUNIT_TEST=m: no warnings.
- checkpatch.pl --strict: clean apart from the missing Signed-off-by.

Not tested:
- Architectures other than UML x86_64 (the test was only built for x86_64 and i386), and PREEMPT_RT.

It calls the internal __objpool_try_add_slot() with interrupts enabled on purpose: that is what lets the hrtimer nest.

This patch was prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5): the test and the changelog were written with the assistant.

 MAINTAINERS               |   1 +
 lib/Kconfig.debug         |  13 ++
 lib/tests/Makefile        |   1 +
 lib/tests/objpool_kunit.c | 329 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 344 insertions(+)
 create mode 100644 lib/tests/objpool_kunit.c
MAINTAINERS +1 -0
index 72294ddfa5b7..0d319f2c2948 100644
@@ -19872,6 +19872,7 @@ S:	Supported
 F:	include/linux/objpool.h
 F:	lib/objpool.c
 F:	lib/test_objpool.c
+F:	lib/tests/objpool_kunit.c
 
 OBJTOOL
 M:	Josh Poimboeuf <[email protected]>
lib/Kconfig.debug +13 -0
index 134b15a44625..3d9c2783ac9b 100644
@@ -3378,6 +3378,19 @@ config TEST_OBJPOOL
 
 	  If unsure, say N.
 
+config OBJPOOL_KUNIT_TEST
+	tristate "KUnit test for nested objpool pushes" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Stress test for an objpool push that is interrupted by another
+	  push to the same per-CPU slot, as happens when a kretprobe'd
+	  function returns in NMI context.  An hrtimer stands in for the
+	  NMI.  The test runs for about ten seconds.  The case that pops
+	  from another CPU is skipped when only one CPU is online.
+
+	  If unsure, say N.
+
 config TEST_KEXEC_HANDOVER
 	bool "Test for Kexec HandOver"
 	default n
lib/tests/Makefile +1 -0
index 3cac3b63a752..f0238587cc8a 100644
@@ -29,6 +29,7 @@ obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o
 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LIST_PRIVATE_KUNIT_TEST) += list-private-test.o
+obj-$(CONFIG_OBJPOOL_KUNIT_TEST) += objpool_kunit.o
 obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
 obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
lib/tests/objpool_kunit.c new file +329 -0

329 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/2] lib/tests: add KUnit test for nested objpool pushes

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] lib/tests: UAF and memory leaks on KUnit assertion failure or timeout - [High] lib/tests: module…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] lib/tests: UAF and memory leaks on KUnit assertion failure or timeout
- [High] lib/tests: module unload UAF due to unsafe kthread completion
- [Medium] lib/tests: missing memory barrier causes stale reads of test statistics --

commit 6bf48aebedba8949a86524af4e186a561332ca36
Author: Shashank Mohan Jain <[email protected]>

lib/tests: add KUnit test for nested objpool pushes

objpool_push() only pushes to the slot of the local CPU, but a push from NMI context (a kretprobe'd function returning in NMI) can interrupt it and push to the same slot.

lib/tests/objpool_kunit.c new file +0 -0
new file mode 100644
index 0000000000000..d86ef00dd0887
@@ -0,0 +1,329 @@

[ ... ]

Quoted 2 lines
(quoted diff) +2 -0
+static int op_task_fn(void *data)
+{

[ ... ]

Review

Quoted 1 line
(quoted diff) +1 -0
+	while (ctx->remote_pop && !READ_ONCE(ctx->popper_done)) {

[Severity: Medium]
Will this polling cause stale reads of test statistics?

The flag is polled using READ_ONCE without an acquire barrier.

Quoted 4 lines
(quoted diff) +4 -0
+		/* keep one reserve object for the teardown */
+		if (ctx->nreserve > 1) {
+			obj = ctx->reserve[--ctx->nreserve];
+			atomic_set(&obj->held, 0);

Review

Quoted 10 lines
(quoted diff) +10 -0
+			local_irq_save(flags);
+			__objpool_try_add_slot(obj, &ctx->pool, ctx->cpu);
+			local_irq_restore(flags);
+		}
+		schedule_timeout_uninterruptible(1);
+	}
+
+	complete(&ctx->done);
+	return 0;
+}

[Severity: High]
Can this create a module unload race condition?

The kthread signals completion before fully exiting. The thread can be preempted before returning, and because the main test thread returns immediately after wait_for_completion(), module unload could proceed before this kthread has fully exited module text. If the module is removed, the kthread will resume execution in unmapped memory.

[ ... ]

Quoted 2 lines
(quoted diff) +2 -0
+static int op_popper_fn(void *data)
+{

[ ... ]

Review

Quoted 7 lines
(quoted diff) +7 -0
+	while (!READ_ONCE(ctx->stop)) {
+		obj = objpool_pop(&ctx->pool);
+		if (obj)
+			op_popped(ctx, obj);
+		cond_resched();
+	}
+	WRITE_ONCE(ctx->popper_done, true);

[Severity: Medium]
Does this need a release barrier?

The completion flag is published using WRITE_ONCE without a release barrier, allowing prior statistic updates to be reordered. On weakly ordered architectures, op_task_fn might observe the completion state before the statistic updates (like ctx->pops and ctx->doubles) are globally visible.

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

[ ... ]

Quoted 2 lines
(quoted diff) +2 -0
+static void op_run(struct kunit *test, bool remote_pop)
+{

[ ... ]

Review

Quoted 4 lines
(quoted diff) +4 -0
+	/* take every object out of the pool; hand them to the stocks */
+	while (n < OP_NR_OBJS && (obj = objpool_pop(&ctx->pool)))
+		ctx->objs[n++] = obj;
+	KUNIT_ASSERT_EQ(test, n, OP_NR_OBJS);

[Severity: High]
Does an assertion failure here leak memory?

If this assertion fails, the test aborts and skips objpool_fini(), leaking the pool internals.

[ ... ]

Review

Quoted 6 lines
(quoted diff) +6 -0
+	task = kthread_create(op_task_fn, ctx, "objpool_kunit_push");
+	KUNIT_ASSERT_FALSE(test, IS_ERR(task));
+	kthread_bind(task, cpu);
+	if (remote_pop) {
+		popper = kthread_create(op_popper_fn, ctx, "objpool_kunit_pop");
+		KUNIT_ASSERT_FALSE(test, IS_ERR(popper));

[Severity: High]
Would an assertion failure here leak the kthread and the objpool?

If the KUNIT_ASSERT_FALSE macro triggers a test abort, the already-created 'task' thread is never stopped or freed, and the objpool is leaked.

Review

Quoted 5 lines
(quoted diff) +5 -0
+		kthread_bind(popper, pcpu);
+		wake_up_process(popper);
+	}
+	wake_up_process(task);
+	wait_for_completion(&ctx->done);

[Severity: High]
Is there a use-after-free risk if the test times out here?

If the test times out while waiting for completion, KUnit will free ctx without stopping the kthreads. The background kthreads will continue running, access the freed ctx memory, and trigger a use-after-free.

Additionally, because the main thread returns immediately after this completion, it does not wait for the kthreads to fully exit. As mentioned above, this exposes the test to a module unload crash where the kthreads resume execution in unmapped memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#2 lore raw

Re: [PATCH 1/2] objpool: keep objpool_push() correct when a push from NMI nests in it

In reply to Shashank Mohan Jain
Quoted 23 lines · On Mon, 28 Sep 2026 14:11:24 +0530 Shashank Mohan Jain <[email protected]>…

objpool_push() adds an object to the slot of the local CPU with interrupts disabled. It reserves an entry with a cmpxchg() on slot->tail, writes the entry, and publishes it with smp_store_release(&slot->last, tail + 1).

A push from NMI context can still interrupt it and push to the same slot. kretprobes may run in NMI context since commit e03b4a084ea6 ("kprobes: Remove NMI context check"). At the time, kretprobe instances came from a CAS-based lockless freelist, which tolerates that. Commit 4bbd93455659 ("kprobes: kretprobe scalability improvement") moved kretprobes and rethook to objpool.

With rethook, rethook_trampoline_handler() recycles instances with objpool_push() after the user handler has run, when no kprobe is marked running anymore; rethook_flush_task() does the same. An NMI that arrives during such a push and runs a function probed by the same kretprobe takes an instance in pre_handler_kretprobe(), and when the function returns inside the NMI, pushes it back to the same slot. This needs a kretprobe on a function that runs both in NMI context and outside it, for instance one that perf calls from the PMU NMI handler on x86. Before v6.14, fprobe also used rethook and could push from NMI the same way, with one pool shared by all functions of an fprobe.

Quoted 3 lines

...

Publish the entries in order instead:

Thanks.

Quoted 18 lines
include/linux/objpool.h +7 -3
@@ -193,19 +193,40 @@ __objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu)
 	struct objpool_slot *slot = pool->cpu_slots[cpu];
 	uint32_t head, tail;
 
-	/* loading tail and head as a local snapshot, tail first */
+	/*
+	 * Only the local CPU pushes to its slot, with irqs disabled, but a
+	 * push from NMI context (a kretprobe'd function returning in NMI)
+	 * can interrupt this one at any point.
+	 */
 	tail = READ_ONCE(slot->tail);
+	while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1))
+		;
 
-	do {
-		head = READ_ONCE(slot->head);

include/linux/objpool.h __objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu) · line 211

Quoted 10 lines
include/linux/objpool.h +7 -3
-		/* fault caught: something must be wrong */
-		WARN_ON_ONCE(tail - head > pool->nr_objs);
-	} while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1));
+	/*
+	 * fault caught: something must be wrong.  Read head only after the
+	 * reservation: a nested push and a pop on another CPU could have
+	 * moved head past an older snapshot of tail.
+	 */
+	head = READ_ONCE(slot->head);
+	WARN_ON_ONCE(tail - head > pool->nr_objs);

This code can run in NMI?

Calling WARN_ON from NMI sounds quite sketchy - the warning handler does all sorts of stuff. I see this is pre-existing but perhaps this is a chance to address it.

Sashiko liked [1/2] but had a lot to say about the test module:

	https://sashiko.dev/#/patchset/[email protected]
#3 lore raw

Re: [PATCH 1/2] objpool: keep objpool_push() correct when a push from NMI nests in it

Thanks for looking at it.

Quoted 1 line · On Tue, 29 Sep 2026 Andrew Morton wrote:

This code can run in NMI?

Yes: on the rethook architectures a kretprobe on a function that also runs in NMI context (for example one that perf calls from the PMU NMI handler on x86) takes and recycles instances in NMI context, so objpool_push() can run in NMI and nest in another push on the same CPU.

Quoted 2 lines

Calling WARN_ON from NMI sounds quite sketchy [...] perhaps this is a chance to address it.

Agreed. With the in-order publish a nested push can no longer trigger that sanity check, so for v2 I'd add a patch that drops the WARN_ON_ONCE() from the push path (or skips it in_nmi()). Masami, would you prefer one of these?

Quoted 1 line

Sashiko liked [1/2] but had a lot to say about the test module

v2 reworks the test: cleanup actions so an assertion failure or a timeout can't leak or use freed memory, the threads are stopped before the test returns, and release/acquire for the flags.

Shashank

Quoted 69 lines · Andrew Morton
Quoted 23 lines · On Mon, 28 Sep 2026 14:11:24 +0530 Shashank Mohan Jain <[email protected]>…

objpool_push() adds an object to the slot of the local CPU with interrupts disabled. It reserves an entry with a cmpxchg() on slot->tail, writes the entry, and publishes it with smp_store_release(&slot->last, tail + 1).

A push from NMI context can still interrupt it and push to the same slot. kretprobes may run in NMI context since commit e03b4a084ea6 ("kprobes: Remove NMI context check"). At the time, kretprobe instances came from a CAS-based lockless freelist, which tolerates that. Commit 4bbd93455659 ("kprobes: kretprobe scalability improvement") moved kretprobes and rethook to objpool.

With rethook, rethook_trampoline_handler() recycles instances with objpool_push() after the user handler has run, when no kprobe is marked running anymore; rethook_flush_task() does the same. An NMI that arrives during such a push and runs a function probed by the same kretprobe takes an instance in pre_handler_kretprobe(), and when the function returns inside the NMI, pushes it back to the same slot. This needs a kretprobe on a function that runs both in NMI context and outside it, for instance one that perf calls from the PMU NMI handler on x86. Before v6.14, fprobe also used rethook and could push from NMI the same way, with one pool shared by all functions of an fprobe.

Quoted 3 lines

...

Publish the entries in order instead:

Thanks.

Quoted 18 lines
include/linux/objpool.h +7 -3
@@ -193,19 +193,40 @@ __objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu)
      struct objpool_slot *slot = pool->cpu_slots[cpu];
      uint32_t head, tail;
 
-     /* loading tail and head as a local snapshot, tail first */
+     /*
+      * Only the local CPU pushes to its slot, with irqs disabled, but a
+      * push from NMI context (a kretprobe'd function returning in NMI)
+      * can interrupt this one at any point.
+      */
      tail = READ_ONCE(slot->tail);
+     while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1))
+             ;
 
-     do {
-             head = READ_ONCE(slot->head);

include/linux/objpool.h __objpool_try_add_slot(void *obj, struct objpool_head *pool, int cpu) · line 211

Quoted 10 lines
include/linux/objpool.h +7 -3
-             /* fault caught: something must be wrong */
-             WARN_ON_ONCE(tail - head > pool->nr_objs);
-     } while (!try_cmpxchg_acquire(&slot->tail, &tail, tail + 1));
+     /*
+      * fault caught: something must be wrong.  Read head only after the
+      * reservation: a nested push and a pop on another CPU could have
+      * moved head past an older snapshot of tail.
+      */
+     head = READ_ONCE(slot->head);
+     WARN_ON_ONCE(tail - head > pool->nr_objs);

This code can run in NMI?

Calling WARN_ON from NMI sounds quite sketchy - the warning handler does all sorts of stuff. I see this is pre-existing but perhaps this is a chance to address it.

Sashiko liked [1/2] but had a lot to say about the test module:

        https://sashiko.dev/#/patchset/[email protected]