Skip to contentSkip to conversation

mm/slab: introduce kfree_rcu_nolock() and improve slub_kunit coverage

PATCH Superseded Harry Yoo (Oracle) 7 participants 53 messages Jun 15 – Jun 29

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Harry Yoo (Oracle)

author maintainer
#0 lore raw

v3 cover mm/slab: introduce kfree_rcu_nolock() and improve slub_kunit coverage

Not the best time to post a series, but didn't want to delay posting the series for too long. no pressures ;) This is aimed to be queued for review and testing after the merge window closes.

This series is based on next-20260612, and is also available on git.kernel.org [3].

To RCU folks: It would be great if you could kindly take a quick look at patch 4 and either ack or nack the patch ;)

To BPF folks: Ulad asked to share workloads to measure performance of kfree_rcu_nolock(). Unfortunately, I focused more on correctness and have not spent much effort on that. It would be nice if BPF folks could help evaluate it on their relevant workloads.

To PREEMPT_RT folks: The most relevant part is allowing kfree_rcu_sheaf() on PREEMPT_RT (patch 6). It carefully avoids sleeping by acquiring the locks via local_trylock() or spin_trylock_irqsave() to avoid sleeping within a raw spinlock. When trylock or unlock is unsafe, kmalloc_nolock() always fails.

Changes since RFC v2
====================

Reduced complexity and intrusiveness (Uladzislau Rezki)
-------------------------------------------------------

While discussing concerns about the complexity of adding allow_spin handling with Ulad (Thanks!), I realized that adding complexity to the kvfree_rcu batching is not strictly necessary: only slab objects need to be batched, they are already batched by rcu sheaves, and slab already supports unknown context. So it is enough to implement only a minimal fallback for the sheaves path.

I tried to avoid making intrusive changes to the existing kvfree_rcu path as much as possible. struct rcu_ptr is renamed to kfree_rcu_head following Vlastimil's suggestion, and it is used only in the kfree_rcu_nolock() path for now.

As a result, the complexity is significantly reduced and the series became much less intrusive. This is also reflected well in the diffstat below.

RFC v2 diffstat:

  8 files changed, 514 insertions(+), 163 deletions(-)

v3 diffstat:

  6 files changed, 370 insertions(+), 105 deletions(-)

v3 diffstat (slub_kunit improvements - patch 1, 2, 9 excluded):

  5 files changed, 199 insertions(+), 66 deletions(-)

kfree_rcu_sheaf() PREEMPT_RT support (Vlastimil Babka)
------------------------------------------------------

As suggested by Vlastimil (Thanks!), kfree_rcu_sheaf() can now be used on PREEMPT_RT as well, by always assuming allow_spin is false on PREEMPT_RT.

slub_kunit enhancements
-----------------------

- Currently the test is skipped when there is no hardware PMU. This can happen on machines without a PMU, or in virtualized environments (e.g., automated testing or virtme). Implement a fallback based on SW perf events so that the test can still run in such environments, even though the coverage is slightly smaller.

- While testing on PREEMPT_RT, I found that kmalloc_nolock() fails every time, so the fallback path is not properly tested. This is a limitation of perf events: the handler is called in NMI (HW perf events) or interrupt context (SW perf events), where kmalloc_nolock() cannot succeed.

slub_kunit now registers a kprobe pre-handler at the points in the slab allocator where lockdep_assert_held() is invoked. The pre-handler calls kmalloc_nolock() and friends, to improve coverage on PREEMPT_RT instead of relying on perf events.

One thing that needs to be further explored
-------------------------------------------

The global deferred_free_by_rcu (introduced by patch 8) list for the fallback should probably be per-CPU [5].

Actual Cover Letter
===================

This series improves kmalloc_nolock() and kfree_nolock() coverage in slub_kunit (patch 1 and 2) and introduces kfree_rcu_nolock() for an unknown context as suggested by Alexei Starovoitov.

Unknown context means the caller does not know whether spinning on a lock is safe (e.g., a BPF program attached to an arbitrary kernel function or in NMI context).

The slab allocator already supports unknown context via kmalloc_nolock() and kfree_nolock(), but te slab allocator does not support freeing objects by RCU in unknown context.

It is not ideal to have completely separate batching for unknown context because the worst scenario where spinning on a lock would lead to deadlock is very rare, and in most cases, it is safe to use the existing mechanism (kfree_rcu_sheaf()).

Since most part of the slab allocator already supports unknown context and sheaves support batching kvfree_rcu() calls for slab objects, implement kfree_rcu_nolock() with minimal changes by teaching kfree_rcu_sheaf() how to support unknown context and making it a little bit harder to allocate an empty sheaf, instead of making intrusive changes to the existing kvfree_rcu batching logic.

kfree_rcu_nolock() tries to free the object to the rcu sheaf if trylock succeeds. Once the rcu sheaf becomes full, it is submitted to RCU via call_rcu() if spinning is allowed or IRQs are enabled (to avoid calling call_rcu() in the middle of call_rcu()). Otherwise, call_rcu() is deferred via irq work.

In unknown context, when there is no sheaf available, kfree_rcu_sheaf() falls back to defer_kfree_rcu(), which inserts the object to a global lockless list [5] and those objects are freed after synchronize_rcu() in a workqueue.

Unlike kfree_rcu(), only the 2-argument variant is supported. This is because the last resort of the 1-arg variant is synchronize_rcu(), which cannot be used in an unknown context.

As suggested by Alexei Starovoitov, kfree_rcu_nolock() can be used with struct kfree_rcu_head (8 bytes), which is smaller than struct rcu_head (16 bytes).

For more background and future plans, please see [4].

[1] RFC v1: https://lore.kernel.org/linux-mm/[email protected]

[2] RFC v2: https://lore.kernel.org/linux-mm/[email protected]

[3] https://git.kernel.org/pub/scm/linux/kernel/git/harry/linux.git/log/?h=kfree_rcu_nolock-v3r3

[4] kmalloc_nolock() follow-ups, including kfree_rcu_nolock(),
https://lore.kernel.org/linux-mm/esepccfhqg7m6jo76ns2znj2cnuaepx2xvw5zaygtwohq4psma@563ypprp6rr3

[5] However, we should probably make the list percpu because, unlike RFC v2, it can be triggered more frequently under memory pressure.

https://lore.kernel.org/linux-mm/805c33d7-3a7b-470c-bd9d-065717a3e3e2@paulmck-laptop

---
Harry Yoo (Oracle) (9):
slub_kunit: fall back to SW perf events when HW PMU is not available mm/slab, slub_kunit: register kprobe to trigger _nolock APIs mm/slab: handle the !allow_spin case in kfree_rcu_sheaf() mm/slab: use call_rcu() in unknown context if irqs are enabled mm/slab: extend deferred free mechanism to handle rcu sheaves mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT
mm/slab: introduce kfree_rcu_nolock()
mm/slab: introduce struct kfree_rcu_head and use in kfree_rcu_nolock()
slub_kunit: extend the test for kfree_rcu_nolock()

 include/linux/rcupdate.h |  12 +++
 include/linux/types.h    |   4 +
 lib/tests/slub_kunit.c   | 174 ++++++++++++++++++++++++++++------
 mm/slab.h                |   5 +-
 mm/slab_common.c         |  38 ++++++--
 mm/slub.c                | 242 ++++++++++++++++++++++++++++++++++-------------
 6 files changed, 370 insertions(+), 105 deletions(-)

---
base-commit: c425609d6ac4012c8bbf01ec2e10e801b1923a7b
change-id: 20260615-kfree_rcu_nolock-e5502555992f

Best regards,

Signature · 1 line
Harry Yoo (Oracle) <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/9 slub_kunit: fall back to SW perf events when HW PMU is not available

slub_kunit uses hardware perf events to invoke _nolock() APIs from NMI context. However, creating a HW perf event fails when there is no hardware PMU, or when running in a virtualized environment without a virtual PMU configured.

Since tests are often performed in virtualized environments, fall back to a SW perf event so that the test can still run, even if it cannot cover IRQ-disabled sections.

For the sheaves path, IRQs are not disabled until the barn is accessed. So some coverage is still expected.

---
 lib/tests/slub_kunit.c | 70 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 55 insertions(+), 15 deletions(-)
lib/tests/slub_kunit.c +55 -15
index fa6d31dbca16..11255fc8eb78 100644
@@ -303,6 +303,7 @@ struct test_nolock_context {
 	int alloc_ok;
 	int alloc_fail;
 	struct perf_event *event;
+	bool is_perf_type_hw;
 };
 
 static struct perf_event_attr hw_attr = {
@@ -315,9 +316,19 @@ static struct perf_event_attr hw_attr = {
 	.sample_freq = 100000,
 };
 
-static void overflow_handler_test_kmalloc_kfree_nolock(struct perf_event *event,
-						       struct perf_sample_data *data,
-						       struct pt_regs *regs)
+/* Fallback when hardware perf event is not available */
+static struct perf_event_attr sw_attr = {
+	.type = PERF_TYPE_SOFTWARE,
+	.config = PERF_COUNT_SW_CPU_CLOCK,
+	.size = sizeof(struct perf_event_attr),
+	.disabled = 1,
+	.freq = 1,
+	.sample_freq = 100000,
+};
+
+static void overflow_handler_test_nolock(struct perf_event *event,
+					 struct perf_sample_data *data,
+					 struct pt_regs *regs)
 {
 	void *objp;
 	gfp_t gfp;
@@ -336,20 +347,53 @@ static void overflow_handler_test_kmalloc_kfree_nolock(struct perf_event *event,
 	ctx->callback_count++;
 }
 
+static bool enable_perf_events(struct test_nolock_context *ctx)
+{
+	struct perf_event *event;
+
+	ctx->is_perf_type_hw = true;
+	event = perf_event_create_kernel_counter(&hw_attr, -1, current,
+						 overflow_handler_test_nolock,
+						 ctx);
+
+	if (!IS_ERR(event))
+		goto out;
+
+	ctx->is_perf_type_hw = false;
+	event = perf_event_create_kernel_counter(&sw_attr, -1, current,
+						 overflow_handler_test_nolock,
+						 ctx);
+	if (!IS_ERR(event))
+		goto out;
+
+	return false;
+out:
+	ctx->event = event;
+	perf_event_enable(ctx->event);
+	return true;
+}
+
+static void disable_perf_events(struct test_nolock_context *ctx)
+{
+	kunit_info(ctx->test, "%s perf events: callback_count: %d, alloc_ok: %d, alloc_fail: %d\n",
+		   ctx->is_perf_type_hw ? "HW" : "SW",
+		   ctx->callback_count, ctx->alloc_ok, ctx->alloc_fail);
+
+	perf_event_disable(ctx->event);
+	perf_event_release_kernel(ctx->event);
+}
+
 static void test_kmalloc_kfree_nolock(struct kunit *test)
 {
 	int i, j;
 	struct test_nolock_context ctx = { .test = test };
-	struct perf_event *event;
 	bool alloc_fail = false;
+	bool perf_events_enabled;
 
-	event = perf_event_create_kernel_counter(&hw_attr, -1, current,
-						 overflow_handler_test_kmalloc_kfree_nolock,
-						 &ctx);
-	if (IS_ERR(event))
+	perf_events_enabled = enable_perf_events(&ctx);
+	if (!perf_events_enabled)
 		kunit_skip(test, "Failed to create perf event");
-	ctx.event = event;
-	perf_event_enable(ctx.event);
+
 	for (i = 0; i < NR_ITERATIONS; i++) {
 		for (j = 0; j < NR_OBJECTS; j++) {
 			gfp_t gfp = (i % 2) ? GFP_KERNEL : GFP_KERNEL_ACCOUNT;
@@ -368,11 +412,7 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
 	}
 
 cleanup:
-	perf_event_disable(ctx.event);
-	perf_event_release_kernel(ctx.event);
-
-	kunit_info(test, "callback_count: %d, alloc_ok: %d, alloc_fail: %d\n",
-		   ctx.callback_count, ctx.alloc_ok, ctx.alloc_fail);
+	disable_perf_events(&ctx);
 
 	if (alloc_fail)
 		kunit_skip(test, "Allocation failed");
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH for-next v3 1/9] slub_kunit: fall back to SW perf events when HW PMU is not available

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The test cleanup function reads and logs test context variables before disabling the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test cleanup function reads and logs test context variables before disabling the asynchronous perf event, introducing a data race and causing the reported counts to be inaccurate. --

commit 4fccf2c0f65ae8a2788eab369ec35d223d9c897d
Author: Harry Yoo (Oracle) <[email protected]>

slub_kunit: fall back to SW perf events when HW PMU is not available

This commit introduces a fallback mechanism in the slub_kunit tests to use software perf events when a hardware PMU is unavailable. This ensures the tests can still execute in virtualized environments that lack virtual PMUs.

Quoted 3 lines
lib/tests/slub_kunit.c +0 -0

[ ... ]

lib/tests/slub_kunit.c static void overflow_handler_test_kmalloc_kfree_nolock(struct perf_event *event, · line 382

Quoted 7 lines
lib/tests/slub_kunit.c +7 -0
+static void disable_perf_events(struct test_nolock_context *ctx)
+{
+	kunit_info(ctx->test, "%s perf events: callback_count: %d, alloc_ok: %d, alloc_fail: %d\n",
+		   ctx->is_perf_type_hw ? "HW" : "SW",
+		   ctx->callback_count, ctx->alloc_ok, ctx->alloc_fail);
+
+	perf_event_disable(ctx->event);

[Severity: Medium]
Could reading the counters before disabling the perf event result in missed counts?

Since the perf event continues to fire asynchronously in NMI or IRQ context until perf_event_disable() is called, the variables might be incremented after kunit_info() has already read them.

Would it be safer to disable the perf event before reading and logging the results, restoring the original ordering from the previous cleanup block?

Quoted 2 lines
lib/tests/slub_kunit.c +2 -0
+	perf_event_release_kernel(ctx->event);
+}

[ ... ]

Quoted 13 lines
lib/tests/slub_kunit.c +1 -5
@@ -368,11 +412,7 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
 	}
 
 cleanup:
-	perf_event_disable(ctx.event);
-	perf_event_release_kernel(ctx.event);
-
-	kunit_info(test, "callback_count: %d, alloc_ok: %d, alloc_fail: %d\n",
-		   ctx.callback_count, ctx.alloc_ok, ctx.alloc_fail);
+	disable_perf_events(&ctx);
 
 	if (alloc_fail)
 		kunit_skip(test, "Allocation failed");
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [PATCH for-next v3 1/9] slub_kunit: fall back to SW perf events when HW PMU is not available

In reply to Harry Yoo (Oracle)
Quoted 22 lines · Harry Yoo (Oracle)

slub_kunit uses hardware perf events to invoke _nolock() APIs from NMI context. However, creating a HW perf event fails when there is no hardware PMU, or when running in a virtualized environment without a virtual PMU configured.

Since tests are often performed in virtualized environments, fall back to a SW perf event so that the test can still run, even if it cannot cover IRQ-disabled sections.

For the sheaves path, IRQs are not disabled until the barn is accessed. So some coverage is still expected.

---
 lib/tests/slub_kunit.c | 70 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 55 insertions(+), 15 deletions(-)
lib/tests/slub_kunit.c +0 -0
index fa6d31dbca16..11255fc8eb78 100644

lib/tests/slub_kunit.c struct test_nolock_context { · line 307

Quoted 5 lines
lib/tests/slub_kunit.c +1 -0
@@ -303,6 +303,7 @@ struct test_nolock_context {
 	int alloc_ok;
 	int alloc_fail;
 	struct perf_event *event;
+	bool is_perf_type_hw;
 };

nit: should have checked struct perf_event.type instead of introducing a new field. Will fix.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#4 lore raw

Re: [PATCH for-next v3 1/9] slub_kunit: fall back to SW perf events when HW PMU is not available

In reply to Harry Yoo (Oracle)
Quoted 8 lines · Harry Yoo (Oracle)

slub_kunit uses hardware perf events to invoke _nolock() APIs from NMI context. However, creating a HW perf event fails when there is no hardware PMU, or when running in a virtualized environment without a virtual PMU configured.

Since tests are often performed in virtualized environments, fall back to a SW perf event so that the test can still run, even if it cannot cover IRQ-disabled sections.

Frankly, I wouldn't bother with this fallback, since it completely misses irq disabled section which is the one to test with *_nolock(). VMs need to configured properly. Even nested VMs support PERF_COUNT_HW_CPU_CYCLES.

Also in overflow_handler_test_nolock()
I would add
even->hw.interrupts = 0;
to the end to make sure it actually stress tests things. Without it NMIs are not firing quickly enough.

author maintainer
#5 lore raw

Re: [PATCH for-next v3 1/9] slub_kunit: fall back to SW perf events when HW PMU is not available

Quoted 6 lines · Alexei Starovoitov
Quoted 5 lines · Harry Yoo (Oracle)

slub_kunit uses hardware perf events to invoke _nolock() APIs from NMI context. However, creating a HW perf event fails when there is no hardware PMU, or when running in a virtualized environment without a virtual PMU configured.

Quoted 6 lines
Quoted 3 lines

Since tests are often performed in virtualized environments, fall back to a SW perf event so that the test can still run, even if it cannot cover IRQ-disabled sections.

Frankly, I wouldn't bother with this fallback, since it completely misses irq disabled section which is the one to test with *_nolock().

Right, it only covers percpu sheaves locks.

Quoted 2 lines

VMs need to configured properly. Even nested VMs support PERF_COUNT_HW_CPU_CYCLES.

And now that even VMs that are not properly configured can test it using kprobe with _some_ coverage, we don't really need the fallback.

Will drop the fallback.

Quoted 5 lines

Also in overflow_handler_test_nolock()
I would add
even->hw.interrupts = 0;
to the end to make sure it actually stress tests things. Without it NMIs are not firing quickly enough.

Will do.

Thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#6 lore raw

PATCH v3 2/9 mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

In reply to Harry Yoo (Oracle)

Since kmalloc_nolock() always fails in NMI and hardirq contexts on PREEMPT_RT, slub_kunit cannot properly test _nolock() APIs.

Register a kprobe pre-handler to invoke kmalloc_nolock() and kfree_nolock() in the middle of the slab allocator. However, do not register the handler on UP kernels [1].

To attach the pre-handler while s->cpu_sheaves->lock or n->list_lock is held, add a wrapper function for lockdep_assert_held() that calls a no-op function slab_attach_kprobe_locked() on debug builds. The function is optimized away when neither CONFIG_PROVE_LOCKING nor CONFIG_DEBUG_VM is selected and register_kprobe() fails.

The function calls barrier() to prevent the compiler from optimizing away its callsites. Otherwise, the compiler may consider the function does not have any side effect and remove callsites.

---
 lib/tests/slub_kunit.c | 82 +++++++++++++++++++++++++++++++++++++++++++-------
 mm/slub.c              | 36 ++++++++++++++++------
 2 files changed, 98 insertions(+), 20 deletions(-)
lib/tests/slub_kunit.c +71 -11
index 11255fc8eb78..01d808cb77fa 100644
@@ -8,6 +8,7 @@
 #include <linux/rcupdate.h>
 #include <linux/delay.h>
 #include <linux/perf_event.h>
+#include <linux/kprobes.h>
 #include "../mm/slab.h"
 
 static struct kunit_resource resource;
@@ -292,7 +293,8 @@ static void test_krealloc_redzone_zeroing(struct kunit *test)
 	kmem_cache_destroy(s);
 }
 
-#ifdef CONFIG_PERF_EVENTS
+#if defined(CONFIG_PERF_EVENTS) || (defined(CONFIG_KPROBES) && defined(CONFIG_SMP))
+#define SLUB_KUNIT_TEST_KMALLOC_KFREE_NOLOCK
 #define NR_ITERATIONS 1000
 #define NR_OBJECTS 1000
 static void *objects[NR_OBJECTS];
@@ -302,10 +304,16 @@ struct test_nolock_context {
 	int callback_count;
 	int alloc_ok;
 	int alloc_fail;
+#ifdef CONFIG_PERF_EVENTS
 	struct perf_event *event;
 	bool is_perf_type_hw;
+#endif
+#ifdef CONFIG_KPROBES
+	struct kprobe kprobe;
+#endif
 };
 
+#ifdef CONFIG_PERF_EVENTS
 static struct perf_event_attr hw_attr = {
 	.type = PERF_TYPE_HARDWARE,
 	.config = PERF_COUNT_HW_CPU_CYCLES,
@@ -326,13 +334,10 @@ static struct perf_event_attr sw_attr = {
 	.sample_freq = 100000,
 };
 
-static void overflow_handler_test_nolock(struct perf_event *event,
-					 struct perf_sample_data *data,
-					 struct pt_regs *regs)
+static void test_nolock(struct test_nolock_context *ctx)
 {
 	void *objp;
 	gfp_t gfp;
-	struct test_nolock_context *ctx = event->overflow_handler_context;
 
 	/* __GFP_ACCOUNT to test kmalloc_nolock() in alloc_slab_obj_exts() */
 	gfp = (ctx->callback_count % 2) ? 0 : __GFP_ACCOUNT;
@@ -347,6 +352,15 @@ static void overflow_handler_test_nolock(struct perf_event *event,
 	ctx->callback_count++;
 }
 
+static void overflow_handler_test_nolock(struct perf_event *event,
+					 struct perf_sample_data *data,
+					 struct pt_regs *regs)
+{
+	struct test_nolock_context *ctx = event->overflow_handler_context;
+
+	test_nolock(ctx);
+}
+
 static bool enable_perf_events(struct test_nolock_context *ctx)
 {
 	struct perf_event *event;
@@ -382,17 +396,60 @@ static void disable_perf_events(struct test_nolock_context *ctx)
 	perf_event_disable(ctx->event);
 	perf_event_release_kernel(ctx->event);
 }
+#else
+static bool enable_perf_events(struct test_nolock_context *ctx) { return false; }
+static void disable_perf_events(struct test_nolock_context *ctx) { }
+#endif
+
+#if defined(CONFIG_KPROBES) && defined(CONFIG_SMP)
+static int slab_kprobe_pre_handler(struct kprobe *p, struct pt_regs *regs)
+{
+	struct test_nolock_context *ctx;
+
+	ctx = container_of(p, struct test_nolock_context, kprobe);
+	test_nolock(ctx);
+	return 0;
+}
+
+static bool register_slab_kprobes(struct test_nolock_context *ctx)
+{
+	ctx->kprobe.symbol_name = "slab_attach_kprobe_locked";
+	ctx->kprobe.pre_handler = slab_kprobe_pre_handler;
+
+	if (register_kprobe(&ctx->kprobe))
+		return false;
+	return true;
+}
+
+static void unregister_slab_kprobes(struct test_nolock_context *ctx)
+{
+	kunit_info(ctx->test, "kprobes: callback_count: %d, alloc_ok: %d, alloc_fail: %d\n",
+		   ctx->callback_count, ctx->alloc_ok, ctx->alloc_fail);
+	unregister_kprobe(&ctx->kprobe);
+}
+#else
+static bool register_slab_kprobes(struct test_nolock_context *ctx) { return false; }
+static void unregister_slab_kprobes(struct test_nolock_context *ctx) { }
+#endif
 
 static void test_kmalloc_kfree_nolock(struct kunit *test)
 {
 	int i, j;
-	struct test_nolock_context ctx = { .test = test };
+	struct test_nolock_context perf_ctx = { .test = test };
+	struct test_nolock_context kprobe_ctx = { .test = test };
 	bool alloc_fail = false;
 	bool perf_events_enabled;
+	bool slab_kprobes_enabled;
 
-	perf_events_enabled = enable_perf_events(&ctx);
-	if (!perf_events_enabled)
-		kunit_skip(test, "Failed to create perf event");
+	perf_events_enabled = enable_perf_events(&perf_ctx);
+	slab_kprobes_enabled = register_slab_kprobes(&kprobe_ctx);
+
+	if (!perf_events_enabled && !slab_kprobes_enabled)
+		kunit_skip(test, "Failed to enable perf event and kprobe, skipping");
+	else if (!perf_events_enabled)
+		kunit_info(test, "Failed to create perf event");
+	if (!slab_kprobes_enabled)
+		kunit_info(test, "Failed to register kprobe pre-handler");
 
 	for (i = 0; i < NR_ITERATIONS; i++) {
 		for (j = 0; j < NR_OBJECTS; j++) {
@@ -412,7 +469,10 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
 	}
 
 cleanup:
-	disable_perf_events(&ctx);
+	if (perf_events_enabled)
+		disable_perf_events(&perf_ctx);
+	if (slab_kprobes_enabled)
+		unregister_slab_kprobes(&kprobe_ctx);
 
 	if (alloc_fail)
 		kunit_skip(test, "Allocation failed");
@@ -444,7 +504,7 @@ static struct kunit_case test_cases[] = {
 	KUNIT_CASE(test_kfree_rcu_wq_destroy),
 	KUNIT_CASE(test_leak_destroy),
 	KUNIT_CASE(test_krealloc_redzone_zeroing),
-#ifdef CONFIG_PERF_EVENTS
+#ifdef SLUB_KUNIT_TEST_KMALLOC_KFREE_NOLOCK
 	KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock),
 #endif
 	{}
mm/slub.c +27 -9
index 813fb863254d..87ca154ccd80 100644
@@ -908,6 +908,24 @@ static inline unsigned int obj_exts_offset_in_object(struct kmem_cache *s)
 }
 #endif
 
+/*
+ * A no-op function used to attach kprobe handlers in slub_kunit tests.
+ * The barrier is needed to prevent the compiler from optimizing out callsites.
+ */
+#if defined(CONFIG_DEBUG_VM) || defined(CONFIG_PROVE_LOCKING)
+static noinline void slab_attach_kprobe_locked(void)
+{
+	barrier();
+}
+#else
+static inline void slab_attach_kprobe_locked(void) { }
+#endif
+
+#define slab_lockdep_assert_held(lock) do {	\
+	lockdep_assert_held(lock);		\
+	slab_attach_kprobe_locked();	\
+} while (0)
+
 #ifdef CONFIG_SLUB_DEBUG
 
 /*
@@ -1665,7 +1683,7 @@ static void add_full(struct kmem_cache *s,
 	if (!(s->flags & SLAB_STORE_USER))
 		return;
 
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	list_add(&slab->slab_list, &n->full);
 }
 
@@ -1674,7 +1692,7 @@ static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct
 	if (!(s->flags & SLAB_STORE_USER))
 		return;
 
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	list_del(&slab->slab_list);
 }
 
@@ -2866,7 +2884,7 @@ static unsigned int __sheaf_flush_main_batch(struct kmem_cache *s)
 	void *objects[PCS_BATCH_MAX];
 	struct slab_sheaf *sheaf;
 
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 	sheaf = pcs->main;
@@ -3545,7 +3563,7 @@ __add_partial(struct kmem_cache_node *n, struct slab *slab, enum add_mode mode)
 static inline void add_partial(struct kmem_cache_node *n,
 				struct slab *slab, enum add_mode mode)
 {
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	__add_partial(n, slab, mode);
 }
 
@@ -3559,7 +3577,7 @@ static inline void clear_node_partial_state(struct kmem_cache_node *n,
 static inline void remove_partial(struct kmem_cache_node *n,
 					struct slab *slab)
 {
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	list_del(&slab->slab_list);
 	clear_node_partial_state(n, slab);
 }
@@ -3575,7 +3593,7 @@ static void *alloc_single_from_partial(struct kmem_cache *s,
 {
 	void *object;
 
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 
 #ifdef CONFIG_SLUB_DEBUG
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
@@ -4646,7 +4664,7 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
 	struct node_barn *barn;
 	bool allow_spin;
 
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	/* Bootstrap or debug cache, back off */
 	if (unlikely(!cache_has_sheaves(s))) {
@@ -5786,7 +5804,7 @@ static void __pcs_install_empty_sheaf(struct kmem_cache *s,
 		struct slub_percpu_sheaves *pcs, struct slab_sheaf *empty,
 		struct node_barn *barn)
 {
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	/* This is what we expect to find if nobody interrupted us. */
 	if (likely(!pcs->spare)) {
@@ -5837,7 +5855,7 @@ __pcs_replace_full_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
 	bool put_fail;
 
 restart:
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	/* Bootstrap or debug cache, back off */
 	if (unlikely(!cache_has_sheaves(s))) {
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH for-next v3 2/9] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Global kprobe execution introduces a data race on shared KUnit context variables, which will…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Global kprobe execution introduces a data race on shared KUnit context variables, which will trigger KCSAN warnings.
- [Low] The test_nolock() function is conditionally compiled only when CONFIG_PERF_EVENTS is enabled, but it is called unconditionally by slab_kprobe_pre_handler() when CONFIG_KPROBES and CONFIG_SMP are enabled. --

commit d19f8bc8e3ea86ee712ab0dede8b68fd72d0fbcf
Author: Harry Yoo (Oracle) <[email protected]>

mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

This patch modifies the slub_kunit test to attach a kprobe pre-handler to test the kmalloc_nolock() and kfree_nolock() APIs. It introduces a wrapper function slab_attach_kprobe_locked() that is invoked when slab_lockdep_assert_held() is called.

Quoted 4 lines
lib/tests/slub_kunit.c +0 -0
index 11255fc8eb781..01d808cb77faf 100644

[ ... ]

Quoted 18 lines
lib/tests/slub_kunit.c +1 -4
@@ -326,13 +334,10 @@ static struct perf_event_attr sw_attr = {
 	.sample_freq = 100000,
 };
 
-static void overflow_handler_test_nolock(struct perf_event *event,
-					 struct perf_sample_data *data,
-					 struct pt_regs *regs)
+static void test_nolock(struct test_nolock_context *ctx)
 {
 	void *objp;
 	gfp_t gfp;
-	struct test_nolock_context *ctx = event->overflow_handler_context;
 
 	/* __GFP_ACCOUNT to test kmalloc_nolock() in alloc_slab_obj_exts() */
 	gfp = (ctx->callback_count % 2) ? 0 : __GFP_ACCOUNT;
@@ -347,6 +352,15 @@ static void overflow_handler_test_nolock(struct perf_event *event,
 	ctx->callback_count++;
 }

[ ... ]

lib/tests/slub_kunit.c static void disable_perf_events(struct test_nolock_context *ctx) · line 410

Quoted 7 lines
lib/tests/slub_kunit.c +7 -0
+#if defined(CONFIG_KPROBES) && defined(CONFIG_SMP)
+static int slab_kprobe_pre_handler(struct kprobe *p, struct pt_regs *regs)
+{
+	struct test_nolock_context *ctx;
+
+	ctx = container_of(p, struct test_nolock_context, kprobe);
+	test_nolock(ctx);

[Severity: Low]
If the kernel is built with CONFIG_KPROBES=y and CONFIG_SMP=y, but CONFIG_PERF_EVENTS=n, will this result in a build failure?

The test_nolock() function is defined inside an ifdef CONFIG_PERF_EVENTS block earlier in the file, but it is called here unconditionally when kprobes are enabled.

[Severity: Medium]
Because slab_attach_kprobe_locked() is a global function called by SLUB internals system-wide, could multiple CPUs hit this kprobe concurrently during the test?

If other CPUs perform slab operations, they will trigger slab_kprobe_pre_handler() and concurrently pass the shared kprobe_ctx to test_nolock(). This would cause multiple threads to modify shared integers like ctx->callback_count++, ctx->alloc_ok++, and ctx->alloc_fail++ without atomics or locks, potentially leading to lost updates and KCSAN warnings.

Quoted 2 lines
lib/tests/slub_kunit.c +2 -0
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH for-next v3 2/9] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

In reply to Harry Yoo (Oracle)
Quoted 8 lines · Harry Yoo (Oracle)
mm/slub.c +7 -0
+/*
+ * A no-op function used to attach kprobe handlers in slub_kunit tests.
+ * The barrier is needed to prevent the compiler from optimizing out callsites.
+ */
+#if defined(CONFIG_DEBUG_VM) || defined(CONFIG_PROVE_LOCKING)
+static noinline void slab_attach_kprobe_locked(void)
+{

mm/slub.c static inline unsigned int obj_exts_offset_in_object(struct kmem_cache *s) · line 927

Quoted 10 lines
mm/slub.c +10 -0
+	barrier();
+}
+#else
+static inline void slab_attach_kprobe_locked(void) { }
+#endif
+
+#define slab_lockdep_assert_held(lock) do {	\
+	lockdep_assert_held(lock);		\
+	slab_attach_kprobe_locked();	\
+} while (0)

Great idea. That should cover all kinds of cases.

maintainer Acked-by on v3 2/9
#9 lore raw

Re: [PATCH for-next v3 2/9] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

In reply to Harry Yoo (Oracle)
Quoted 13 lines · Harry Yoo (Oracle)

Since kmalloc_nolock() always fails in NMI and hardirq contexts on PREEMPT_RT, slub_kunit cannot properly test _nolock() APIs.

Register a kprobe pre-handler to invoke kmalloc_nolock() and kfree_nolock() in the middle of the slab allocator. However, do not register the handler on UP kernels [1].

To attach the pre-handler while s->cpu_sheaves->lock or n->list_lock is held, add a wrapper function for lockdep_assert_held() that calls a no-op function slab_attach_kprobe_locked() on debug builds. The function is optimized away when neither CONFIG_PROVE_LOCKING nor CONFIG_DEBUG_VM is selected and register_kprobe() fails.

Quoted 6 lines

The function calls barrier() to prevent the compiler from optimizing away its callsites. Otherwise, the compiler may consider the function does not have any side effect and remove callsites.

Looks very useful!

Quoted 272 lines
---
 lib/tests/slub_kunit.c | 82 +++++++++++++++++++++++++++++++++++++++++++-------
 mm/slub.c              | 36 ++++++++++++++++------
 2 files changed, 98 insertions(+), 20 deletions(-)
lib/tests/slub_kunit.c +71 -11
index 11255fc8eb78..01d808cb77fa 100644
@@ -8,6 +8,7 @@
 #include <linux/rcupdate.h>
 #include <linux/delay.h>
 #include <linux/perf_event.h>
+#include <linux/kprobes.h>
 #include "../mm/slab.h"
 
 static struct kunit_resource resource;
@@ -292,7 +293,8 @@ static void test_krealloc_redzone_zeroing(struct kunit *test)
 	kmem_cache_destroy(s);
 }
 
-#ifdef CONFIG_PERF_EVENTS
+#if defined(CONFIG_PERF_EVENTS) || (defined(CONFIG_KPROBES) && defined(CONFIG_SMP))
+#define SLUB_KUNIT_TEST_KMALLOC_KFREE_NOLOCK
 #define NR_ITERATIONS 1000
 #define NR_OBJECTS 1000
 static void *objects[NR_OBJECTS];
@@ -302,10 +304,16 @@ struct test_nolock_context {
 	int callback_count;
 	int alloc_ok;
 	int alloc_fail;
+#ifdef CONFIG_PERF_EVENTS
 	struct perf_event *event;
 	bool is_perf_type_hw;
+#endif
+#ifdef CONFIG_KPROBES
+	struct kprobe kprobe;
+#endif
 };
 
+#ifdef CONFIG_PERF_EVENTS
 static struct perf_event_attr hw_attr = {
 	.type = PERF_TYPE_HARDWARE,
 	.config = PERF_COUNT_HW_CPU_CYCLES,
@@ -326,13 +334,10 @@ static struct perf_event_attr sw_attr = {
 	.sample_freq = 100000,
 };
 
-static void overflow_handler_test_nolock(struct perf_event *event,
-					 struct perf_sample_data *data,
-					 struct pt_regs *regs)
+static void test_nolock(struct test_nolock_context *ctx)
 {
 	void *objp;
 	gfp_t gfp;
-	struct test_nolock_context *ctx = event->overflow_handler_context;
 
 	/* __GFP_ACCOUNT to test kmalloc_nolock() in alloc_slab_obj_exts() */
 	gfp = (ctx->callback_count % 2) ? 0 : __GFP_ACCOUNT;
@@ -347,6 +352,15 @@ static void overflow_handler_test_nolock(struct perf_event *event,
 	ctx->callback_count++;
 }
 
+static void overflow_handler_test_nolock(struct perf_event *event,
+					 struct perf_sample_data *data,
+					 struct pt_regs *regs)
+{
+	struct test_nolock_context *ctx = event->overflow_handler_context;
+
+	test_nolock(ctx);
+}
+
 static bool enable_perf_events(struct test_nolock_context *ctx)
 {
 	struct perf_event *event;
@@ -382,17 +396,60 @@ static void disable_perf_events(struct test_nolock_context *ctx)
 	perf_event_disable(ctx->event);
 	perf_event_release_kernel(ctx->event);
 }
+#else
+static bool enable_perf_events(struct test_nolock_context *ctx) { return false; }
+static void disable_perf_events(struct test_nolock_context *ctx) { }
+#endif
+
+#if defined(CONFIG_KPROBES) && defined(CONFIG_SMP)
+static int slab_kprobe_pre_handler(struct kprobe *p, struct pt_regs *regs)
+{
+	struct test_nolock_context *ctx;
+
+	ctx = container_of(p, struct test_nolock_context, kprobe);
+	test_nolock(ctx);
+	return 0;
+}
+
+static bool register_slab_kprobes(struct test_nolock_context *ctx)
+{
+	ctx->kprobe.symbol_name = "slab_attach_kprobe_locked";
+	ctx->kprobe.pre_handler = slab_kprobe_pre_handler;
+
+	if (register_kprobe(&ctx->kprobe))
+		return false;
+	return true;
+}
+
+static void unregister_slab_kprobes(struct test_nolock_context *ctx)
+{
+	kunit_info(ctx->test, "kprobes: callback_count: %d, alloc_ok: %d, alloc_fail: %d\n",
+		   ctx->callback_count, ctx->alloc_ok, ctx->alloc_fail);
+	unregister_kprobe(&ctx->kprobe);
+}
+#else
+static bool register_slab_kprobes(struct test_nolock_context *ctx) { return false; }
+static void unregister_slab_kprobes(struct test_nolock_context *ctx) { }
+#endif
 
 static void test_kmalloc_kfree_nolock(struct kunit *test)
 {
 	int i, j;
-	struct test_nolock_context ctx = { .test = test };
+	struct test_nolock_context perf_ctx = { .test = test };
+	struct test_nolock_context kprobe_ctx = { .test = test };
 	bool alloc_fail = false;
 	bool perf_events_enabled;
+	bool slab_kprobes_enabled;
 
-	perf_events_enabled = enable_perf_events(&ctx);
-	if (!perf_events_enabled)
-		kunit_skip(test, "Failed to create perf event");
+	perf_events_enabled = enable_perf_events(&perf_ctx);
+	slab_kprobes_enabled = register_slab_kprobes(&kprobe_ctx);
+
+	if (!perf_events_enabled && !slab_kprobes_enabled)
+		kunit_skip(test, "Failed to enable perf event and kprobe, skipping");
+	else if (!perf_events_enabled)
+		kunit_info(test, "Failed to create perf event");
+	if (!slab_kprobes_enabled)
+		kunit_info(test, "Failed to register kprobe pre-handler");
 
 	for (i = 0; i < NR_ITERATIONS; i++) {
 		for (j = 0; j < NR_OBJECTS; j++) {
@@ -412,7 +469,10 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
 	}
 
 cleanup:
-	disable_perf_events(&ctx);
+	if (perf_events_enabled)
+		disable_perf_events(&perf_ctx);
+	if (slab_kprobes_enabled)
+		unregister_slab_kprobes(&kprobe_ctx);
 
 	if (alloc_fail)
 		kunit_skip(test, "Allocation failed");
@@ -444,7 +504,7 @@ static struct kunit_case test_cases[] = {
 	KUNIT_CASE(test_kfree_rcu_wq_destroy),
 	KUNIT_CASE(test_leak_destroy),
 	KUNIT_CASE(test_krealloc_redzone_zeroing),
-#ifdef CONFIG_PERF_EVENTS
+#ifdef SLUB_KUNIT_TEST_KMALLOC_KFREE_NOLOCK
 	KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock),
 #endif
 	{}
mm/slub.c +27 -9
index 813fb863254d..87ca154ccd80 100644
@@ -908,6 +908,24 @@ static inline unsigned int obj_exts_offset_in_object(struct kmem_cache *s)
 }
 #endif
 
+/*
+ * A no-op function used to attach kprobe handlers in slub_kunit tests.
+ * The barrier is needed to prevent the compiler from optimizing out callsites.
+ */
+#if defined(CONFIG_DEBUG_VM) || defined(CONFIG_PROVE_LOCKING)
+static noinline void slab_attach_kprobe_locked(void)
+{
+	barrier();
+}
+#else
+static inline void slab_attach_kprobe_locked(void) { }
+#endif
+
+#define slab_lockdep_assert_held(lock) do {	\
+	lockdep_assert_held(lock);		\
+	slab_attach_kprobe_locked();	\
+} while (0)
+
 #ifdef CONFIG_SLUB_DEBUG
 
 /*
@@ -1665,7 +1683,7 @@ static void add_full(struct kmem_cache *s,
 	if (!(s->flags & SLAB_STORE_USER))
 		return;
 
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	list_add(&slab->slab_list, &n->full);
 }
 
@@ -1674,7 +1692,7 @@ static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct
 	if (!(s->flags & SLAB_STORE_USER))
 		return;
 
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	list_del(&slab->slab_list);
 }
 
@@ -2866,7 +2884,7 @@ static unsigned int __sheaf_flush_main_batch(struct kmem_cache *s)
 	void *objects[PCS_BATCH_MAX];
 	struct slab_sheaf *sheaf;
 
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 	sheaf = pcs->main;
@@ -3545,7 +3563,7 @@ __add_partial(struct kmem_cache_node *n, struct slab *slab, enum add_mode mode)
 static inline void add_partial(struct kmem_cache_node *n,
 				struct slab *slab, enum add_mode mode)
 {
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	__add_partial(n, slab, mode);
 }
 
@@ -3559,7 +3577,7 @@ static inline void clear_node_partial_state(struct kmem_cache_node *n,
 static inline void remove_partial(struct kmem_cache_node *n,
 					struct slab *slab)
 {
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 	list_del(&slab->slab_list);
 	clear_node_partial_state(n, slab);
 }
@@ -3575,7 +3593,7 @@ static void *alloc_single_from_partial(struct kmem_cache *s,
 {
 	void *object;
 
-	lockdep_assert_held(&n->list_lock);
+	slab_lockdep_assert_held(&n->list_lock);
 
 #ifdef CONFIG_SLUB_DEBUG
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
@@ -4646,7 +4664,7 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
 	struct node_barn *barn;
 	bool allow_spin;
 
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	/* Bootstrap or debug cache, back off */
 	if (unlikely(!cache_has_sheaves(s))) {
@@ -5786,7 +5804,7 @@ static void __pcs_install_empty_sheaf(struct kmem_cache *s,
 		struct slub_percpu_sheaves *pcs, struct slab_sheaf *empty,
 		struct node_barn *barn)
 {
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	/* This is what we expect to find if nobody interrupted us. */
 	if (likely(!pcs->spare)) {
@@ -5837,7 +5855,7 @@ __pcs_replace_full_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
 	bool put_fail;
 
 restart:
-	lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
+	slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
 
 	/* Bootstrap or debug cache, back off */
 	if (unlikely(!cache_has_sheaves(s))) {
#10 lore raw

Re: [PATCH for-next v3 2/9] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

In reply to Harry Yoo (Oracle)
Quoted 6 lines · Harry Yoo (Oracle)

Since kmalloc_nolock() always fails in NMI and hardirq contexts on PREEMPT_RT, slub_kunit cannot properly test _nolock() APIs.

Register a kprobe pre-handler to invoke kmalloc_nolock() and kfree_nolock() in the middle of the slab allocator. However, do not register the handler on UP kernels [1].

Maybe explain in the commit message why that is?

Quoted 3 lines

To attach the pre-handler while s->cpu_sheaves->lock or n->list_lock is held, add a wrapper function for lockdep_assert_held() that calls a no-op function slab_attach_kprobe_locked() on debug builds. The

Why lockdep? Wouldn't it make more sense to add these triggers after locking these locks?

Quoted 6 lines

function is optimized away when neither CONFIG_PROVE_LOCKING nor CONFIG_DEBUG_VM is selected and register_kprobe() fails.

The function calls barrier() to prevent the compiler from optimizing away its callsites. Otherwise, the compiler may consider the function does not have any side effect and remove callsites.

My wider comment is the following: this looks very useful but perhaps should be lifted to Kunit itself? There's already function redirection (which could be used for this as well). I don't know if that suits your purposes? Choosing to say "I want to hook this random function using kprobe and give it custom behavior" sounds lovely for all sorts of testing.

Signature · 1 line
Pedro
author maintainer
#11 lore raw

Re: [PATCH for-next v3 2/9] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

Quoted 9 lines · Pedro Falcato
Quoted 6 lines · Harry Yoo (Oracle)

Since kmalloc_nolock() always fails in NMI and hardirq contexts on PREEMPT_RT, slub_kunit cannot properly test _nolock() APIs.

Register a kprobe pre-handler to invoke kmalloc_nolock() and kfree_nolock() in the middle of the slab allocator. However, do not register the handler on UP kernels [1].

Maybe explain in the commit message why that is?

Because it's the case is broken on UP and we decided not bother to fix. Thought the cover letter in Link: would give enough context.

Quoted 5 lines
Quoted 3 lines

To attach the pre-handler while s->cpu_sheaves->lock or n->list_lock is held, add a wrapper function for lockdep_assert_held() that calls a no-op function slab_attach_kprobe_locked() on debug builds. The

Why lockdep?

I wanted to avoid hard-coding internal slab function names in the test module while having a good coverage.

Quoted 1 line

Wouldn't it make more sense to add these triggers after> locking these locks?

Good point.

Perhaps it'd make more sense to let _trylock() fail occasionally, because that what I actually want to test.

Quoted 10 lines
Quoted 6 lines

function is optimized away when neither CONFIG_PROVE_LOCKING nor CONFIG_DEBUG_VM is selected and register_kprobe() fails.

The function calls barrier() to prevent the compiler from optimizing away its callsites. Otherwise, the compiler may consider the function does not have any side effect and remove callsites.

My wider comment is the following: this looks very useful but perhaps should be lifted to Kunit itself? There's already function redirection (which could be used for this as well).

TIL kunit function redirection, thanks :)

Quoted 1 line

I don't know if that suits your purposes?

Hmm, but looking at the kunit function redirection, apparently the function needs to have a symbol to redirect.

Not sure how feasible it is to make all those lock helpers have symbols to use kunit function redirection.

Quoted 2 lines

Choosing to say "I want to hook this random function using kprobe and give it custom behavior" sounds lovely for all sorts of testing.

Sounds lovely.

Actually, I was thinking it would be lovely to have fault-injection-like feature for memcg because it's hard to trigger that in kunit...

Thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#12 lore raw

Re: [PATCH for-next v3 2/9] mm/slab, slub_kunit: register kprobe to trigger _nolock APIs

Quoted 7 lines · Harry Yoo
Quoted 4 lines · Pedro Falcato
Quoted 3 lines · Harry Yoo (Oracle)

Since kmalloc_nolock() always fails in NMI and hardirq contexts on PREEMPT_RT, slub_kunit cannot properly test _nolock() APIs.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Register a kprobe pre-handler to invoke kmalloc_nolock() and kfree_nolock() in the middle of the slab allocator. However, do not register the handler on UP kernels [1].

Maybe explain in the commit message why that is?

Because it's the case is broken on UP and we decided not bother to fix. Thought the cover letter in Link: would give enough context.

But I will clarify this on the commit message, thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#13 lore raw

PATCH v3 3/9 mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

In reply to Harry Yoo (Oracle)

Teach kfree_rcu_sheaf() how to handle the !allow_spin case. Try to get an empty sheaf from pcs->spare or the barn even when spinning is not allowed. Unlike __pcs_replace_full_main(), try harder to allocate an empty sheaf because the fallback path will be more expensive than kfree_nolock().

When trylock fails or the kernel observes non-NULL pcs->rcu_free after lock acquisition, free the sheaf instead of putting it to the barn. This is rare and not worth complicating the code.

Since call_rcu() cannot be called in an unknown context, kfree_rcu_sheaf() fails when the rcu sheaf becomes full.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  2 +-
 mm/slub.c        | 39 ++++++++++++++++++++++++++++++---------
 3 files changed, 32 insertions(+), 11 deletions(-)
mm/slab.h +1 -1
index 509f330654b8..b1bd33a16544 100644
@@ -429,7 +429,7 @@ static inline bool is_kmalloc_normal(struct kmem_cache *s)
 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
 }
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin);
 void flush_all_rcu_sheaves(void);
 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
 
mm/slab_common.c +1 -1
index b6426d7ceec9..bc1a8ec938d9 100644
@@ -1605,7 +1605,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj);
+		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);
 
 	return false;
 }
mm/slub.c +30 -9
index 87ca154ccd80..b0d38d515386 100644
@@ -2815,7 +2815,8 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 	return __alloc_empty_sheaf(s, gfp, alloc_flags, s->sheaf_capacity);
 }
 
-static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
+			       bool allow_spin)
 {
 	/*
 	 * If the sheaf was created with SLAB_ALLOC_NO_RECURSE flag then its
@@ -2827,11 +2828,20 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 		mark_obj_codetag_empty(sheaf);
 
 	VM_WARN_ON_ONCE(sheaf->size > 0);
-	kfree(sheaf);
+
+	if (likely(allow_spin))
+		kfree(sheaf);
+	else
+		kfree_nolock(sheaf);
 
 	stat(s, SHEAF_FREE);
 }
 
+static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+{
+	__free_empty_sheaf(s, sheaf, /* allow_spin = */ true);
+}
+
 static unsigned int
 refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
 	       unsigned int max);
@@ -3132,7 +3142,6 @@ static struct slab_sheaf *barn_get_empty_sheaf(struct node_barn *barn,
  * intended action due to a race or cpu migration. Thus they do not check the
  * empty or full sheaf limits for simplicity.
  */
-
 static void barn_put_empty_sheaf(struct node_barn *barn, struct slab_sheaf *sheaf)
 {
 	unsigned long flags;
@@ -6065,7 +6074,7 @@ static void rcu_free_sheaf(struct rcu_head *head)
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 {
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
@@ -6081,9 +6090,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 
 	if (unlikely(!pcs->rcu_free)) {
-
 		struct slab_sheaf *empty;
 		struct node_barn *barn;
+		unsigned int alloc_flags = SLAB_ALLOC_DEFAULT;
+		gfp_t gfp = GFP_NOWAIT;
 
 		/* Bootstrap or debug cache, fall back */
 		if (unlikely(!cache_has_sheaves(s))) {
@@ -6103,7 +6113,7 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 			goto fail;
 		}
 
-		empty = barn_get_empty_sheaf(barn, true);
+		empty = barn_get_empty_sheaf(barn, allow_spin);
 
 		if (empty) {
 			pcs->rcu_free = empty;
@@ -6112,20 +6122,25 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 
 		local_unlock(&s->cpu_sheaves->lock);
 
-		empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
+		if (unlikely(!allow_spin)) {
+			alloc_flags = SLAB_ALLOC_TRYLOCK;
+			gfp = 0;
+		}
+
+		empty = alloc_empty_sheaf(s, gfp, alloc_flags);
 
 		if (!empty)
 			goto fail;
 
 		if (!local_trylock(&s->cpu_sheaves->lock)) {
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);
 			goto fail;
 		}
 
 		pcs = this_cpu_ptr(s->cpu_sheaves);
 
 		if (unlikely(pcs->rcu_free))
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);
 		else
 			pcs->rcu_free = empty;
 	}
@@ -6143,6 +6158,12 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
+		if (unlikely(!allow_spin)) {
+			/* call_rcu() cannot be called in an unknown context */
+			rcu_sheaf->size--;
+			local_unlock(&s->cpu_sheaves->lock);
+			goto fail;
+		}
 		pcs->rcu_free = NULL;
 		rcu_sheaf->node = numa_node_id();
 	}
Signature · 1 line
2.53.0
#14 lore raw

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Kmemleak tracking symmetry violation for empty sheaves allocated with !allow_spin.

Pre-existing issues:
- [High] Deadlock under scheduler locks due to GFP_NOWAIT allocation in __kfree_rcu_sheaf(). --

commit 0ddecba01f906bc55d1c11049f09813fcc05200e
Author: Harry Yoo (Oracle) <[email protected]>

mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

This commit updates kfree_rcu_sheaf() to handle cases where spinning is not allowed, by allocating and freeing empty sheaves without taking locks. It also modifies the allocation flags and fallback paths depending on the allow_spin context.

Quoted 4 lines
mm/slub.c +0 -0
index 87ca154ccd80f..b0d38d5153865 100644

[ ... ]

Quoted 8 lines
mm/slub.c +3 -1
@@ -2827,11 +2828,20 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 		mark_obj_codetag_empty(sheaf);
 
 	VM_WARN_ON_ONCE(sheaf->size > 0);
-	kfree(sheaf);
+
+	if (likely(allow_spin))
+		kfree(sheaf);

mm/slub.c static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf) · line 2843

Quoted 10 lines
mm/slub.c +6 -0
+	else
+		kfree_nolock(sheaf);
 
 	stat(s, SHEAF_FREE);
 }
 
+static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+{
+	__free_empty_sheaf(s, sheaf, /* allow_spin = */ true);
+}

[Severity: High]
Does hardcoding allow_spin to true here cause a kmemleak tracking symmetry violation for sheaves allocated with allow_spin = false?

In __kfree_rcu_sheaf(), if allow_spin is false, the empty sheaf is allocated using SLAB_ALLOC_TRYLOCK and gfp = 0. Because __GFP_RECLAIM bits are missing, SLUB skips kmemleak registration for the object.

When this sheaf becomes full and is processed by RCU, rcu_free_sheaf() handles the cleanup and delegates to free_empty_sheaf(), which now unconditionally calls kfree(sheaf) because allow_spin is hardcoded to true. Calling kfree() on an object that wasn't registered with kmemleak triggers kmemleak warnings.

[ ... ]

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6130

Quoted 11 lines
mm/slub.c +6 -1
@@ -6112,20 +6122,25 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 
 		local_unlock(&s->cpu_sheaves->lock);
 
-		empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
+		if (unlikely(!allow_spin)) {
+			alloc_flags = SLAB_ALLOC_TRYLOCK;
+			gfp = 0;
+		}
+
+		empty = alloc_empty_sheaf(s, gfp, alloc_flags);

[Severity: High]
This is a pre-existing issue, but does the use of GFP_NOWAIT when allow_spin is true risk a deadlock?

Since GFP_NOWAIT contains the __GFP_KSWAPD_RECLAIM flag, the allocation can trigger wakeup_kswapd(). This enters the scheduler and attempts to acquire scheduler locks.

If kvfree_call_rcu() is invoked while a scheduler lock is already held, wouldn't this cause a lock recursion deadlock?

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

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

In reply to Harry Yoo (Oracle)
Quoted 5 lines · Harry Yoo (Oracle)

Teach kfree_rcu_sheaf() how to handle the !allow_spin case. Try to get an empty sheaf from pcs->spare or the barn even when spinning is not allowed. Unlike __pcs_replace_full_main(), try harder to allocate an empty sheaf because the fallback path will be more expensive than kfree_nolock().

You mean that it will try to allocate an empty sheaf, while __pcs_replace_full_main() doesn't with allow_spin == false. I just noticed the comment there is a bit stale... nevermind.

Quoted 56 lines

When trylock fails or the kernel observes non-NULL pcs->rcu_free after lock acquisition, free the sheaf instead of putting it to the barn. This is rare and not worth complicating the code.

Since call_rcu() cannot be called in an unknown context, kfree_rcu_sheaf() fails when the rcu sheaf becomes full.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  2 +-
 mm/slub.c        | 39 ++++++++++++++++++++++++++++++---------
 3 files changed, 32 insertions(+), 11 deletions(-)
mm/slab.h +1 -1
index 509f330654b8..b1bd33a16544 100644
@@ -429,7 +429,7 @@ static inline bool is_kmalloc_normal(struct kmem_cache *s)
 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
 }
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin);
 void flush_all_rcu_sheaves(void);
 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
 
mm/slab_common.c +1 -1
index b6426d7ceec9..bc1a8ec938d9 100644
@@ -1605,7 +1605,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj);
+		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);
 
 	return false;
 }
mm/slub.c +2 -1
index 87ca154ccd80..b0d38d515386 100644
@@ -2815,7 +2815,8 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 	return __alloc_empty_sheaf(s, gfp, alloc_flags, s->sheaf_capacity);
 }
 
-static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
+			       bool allow_spin)
 {
 	/*
 	 * If the sheaf was created with SLAB_ALLOC_NO_RECURSE flag then its

mm/slub.c static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf) · line 2835

Quoted 9 lines
mm/slub.c +5 -1
@@ -2827,11 +2828,20 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 		mark_obj_codetag_empty(sheaf);
 
 	VM_WARN_ON_ONCE(sheaf->size > 0);
-	kfree(sheaf);
+
+	if (likely(allow_spin))
+		kfree(sheaf);
+	else
+		kfree_nolock(sheaf);

Hmm what if the sheaf wasn't allocated with kmalloc_nolock()?

Quoted 42 lines
mm/slub.c +8 -3
 	stat(s, SHEAF_FREE);
 }
 
+static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+{
+	__free_empty_sheaf(s, sheaf, /* allow_spin = */ true);
+}
+
 static unsigned int
 refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
 	       unsigned int max);
@@ -3132,7 +3142,6 @@ static struct slab_sheaf *barn_get_empty_sheaf(struct node_barn *barn,
  * intended action due to a race or cpu migration. Thus they do not check the
  * empty or full sheaf limits for simplicity.
  */
-
 static void barn_put_empty_sheaf(struct node_barn *barn, struct slab_sheaf *sheaf)
 {
 	unsigned long flags;
@@ -6065,7 +6074,7 @@ static void rcu_free_sheaf(struct rcu_head *head)
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 {
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
@@ -6081,9 +6090,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 
 	if (unlikely(!pcs->rcu_free)) {
-
 		struct slab_sheaf *empty;
 		struct node_barn *barn;
+		unsigned int alloc_flags = SLAB_ALLOC_DEFAULT;
+		gfp_t gfp = GFP_NOWAIT;
 
 		/* Bootstrap or debug cache, fall back */
 		if (unlikely(!cache_has_sheaves(s))) {

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6116

Quoted 5 lines
mm/slub.c +1 -1
@@ -6103,7 +6113,7 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 			goto fail;
 		}
 
-		empty = barn_get_empty_sheaf(barn, true);
+		empty = barn_get_empty_sheaf(barn, allow_spin);

Here we might be getting a sheaf allocated previously with kmalloc().

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6127

Quoted 11 lines
mm/slub.c +3 -1
 		if (empty) {
 			pcs->rcu_free = empty;
@@ -6112,20 +6122,25 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 
 		local_unlock(&s->cpu_sheaves->lock);
 
-		empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
+		if (unlikely(!allow_spin)) {
+			alloc_flags = SLAB_ALLOC_TRYLOCK;
+			gfp = 0;

Maybe __GFP_NOWARN?
Here we would get a sheaf with kmalloc_nolock() so that's ok even if it's later freed by someone else by kfree(), right.

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6136

Quoted 10 lines
mm/slub.c +4 -1
+		}
+
+		empty = alloc_empty_sheaf(s, gfp, alloc_flags);
 
 		if (!empty)
 			goto fail;
 
 		if (!local_trylock(&s->cpu_sheaves->lock)) {
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);

Well we could still use the barn with allow_spin == true. But more crucially, here we might be freeing with kfree_nolock() a sheaf from the barn previously allocated with kmalloc()?
Maybe we need to track if it's the case and defer-free it or something.

Also maybe there could be a wrapper kfree_maybe_nolock() (~better name?) That means "I want to kfree safely in kfree_nolock() context something that MIGHT have been kmalloc()"
And maybe depending on the debugging options that make kmalloc() -> kfree_nolock() incompatible, if those are not enabled, it wouldn't have to defer, but proceed normally?

Quoted 25 lines
mm/slub.c +7 -1
 			goto fail;
 		}
 
 		pcs = this_cpu_ptr(s->cpu_sheaves);
 
 		if (unlikely(pcs->rcu_free))
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);
 		else
 			pcs->rcu_free = empty;
 	}
@@ -6143,6 +6158,12 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
+		if (unlikely(!allow_spin)) {
+			/* call_rcu() cannot be called in an unknown context */
+			rcu_sheaf->size--;
+			local_unlock(&s->cpu_sheaves->lock);
+			goto fail;
+		}
 		pcs->rcu_free = NULL;
 		rcu_sheaf->node = numa_node_id();
 	}
#16 lore raw

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

Quoted 7 lines · Vlastimil Babka (SUSE)

On 6/15/26 13:05, Harry Yoo (Oracle) wrote:
Also maybe there could be a wrapper kfree_maybe_nolock() (~better name?) That means "I want to kfree safely in kfree_nolock() context something that MIGHT have been kmalloc()"
And maybe depending on the debugging options that make kmalloc() -> kfree_nolock() incompatible, if those are not enabled, it wouldn't have to defer, but proceed normally?

Or just make kfree_nolock() handle all (non-large) kmalloc() objects. Seems the blockers are only kmemleak and kfence.

Kmemleak is a debugging option not for production so if it's enabled, just defer free everything.

KFENCE is a production option, but we could just defer_free for is_kfence_address() and that will be rare.

author maintainer
#17 lore raw

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

In reply to Vlastimil Babka (SUSE)
Quoted 10 lines · Vlastimil Babka (SUSE)
Quoted 5 lines · Harry Yoo (Oracle)

Teach kfree_rcu_sheaf() how to handle the !allow_spin case. Try to get an empty sheaf from pcs->spare or the barn even when spinning is not allowed. Unlike __pcs_replace_full_main(), try harder to allocate an empty sheaf because the fallback path will be more expensive than kfree_nolock().

You mean that it will try to allocate an empty sheaf, while __pcs_replace_full_main() doesn't with allow_spin == false. I just noticed the comment there is a bit stale... nevermind.

That function

Quoted 111 lines
Quoted 55 lines

When trylock fails or the kernel observes non-NULL pcs->rcu_free after lock acquisition, free the sheaf instead of putting it to the barn. This is rare and not worth complicating the code.

Since call_rcu() cannot be called in an unknown context, kfree_rcu_sheaf() fails when the rcu sheaf becomes full.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  2 +-
 mm/slub.c        | 39 ++++++++++++++++++++++++++++++---------
 3 files changed, 32 insertions(+), 11 deletions(-)
mm/slab.h +1 -1
index 509f330654b8..b1bd33a16544 100644
@@ -429,7 +429,7 @@ static inline bool is_kmalloc_normal(struct kmem_cache *s)
 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
 }
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin);
 void flush_all_rcu_sheaves(void);
 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
 
mm/slab_common.c +1 -1
index b6426d7ceec9..bc1a8ec938d9 100644
@@ -1605,7 +1605,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj);
+		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);
 
 	return false;
 }
mm/slub.c +2 -1
index 87ca154ccd80..b0d38d515386 100644
@@ -2815,7 +2815,8 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 	return __alloc_empty_sheaf(s, gfp, alloc_flags, s->sheaf_capacity);
 }
 
-static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
+			       bool allow_spin)
 {
 	/*
 	 * If the sheaf was created with SLAB_ALLOC_NO_RECURSE flag then its

mm/slub.c static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf) · line 2835

Quoted 9 lines
mm/slub.c +5 -1
@@ -2827,11 +2828,20 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 		mark_obj_codetag_empty(sheaf);
 
 	VM_WARN_ON_ONCE(sheaf->size > 0);
-	kfree(sheaf);
+
+	if (likely(allow_spin))
+		kfree(sheaf);
+	else
+		kfree_nolock(sheaf);

Hmm what if the sheaf wasn't allocated with kmalloc_nolock()?

Quoted 44 lines
mm/slub.c +8 -3
 	stat(s, SHEAF_FREE);
 }
 
+static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+{
+	__free_empty_sheaf(s, sheaf, /* allow_spin = */ true);
+}
+
 static unsigned int
 refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
 	       unsigned int max);
@@ -3132,7 +3142,6 @@ static struct slab_sheaf *barn_get_empty_sheaf(struct node_barn *barn,
  * intended action due to a race or cpu migration. Thus they do not check the
  * empty or full sheaf limits for simplicity.
  */
-
 static void barn_put_empty_sheaf(struct node_barn *barn, struct slab_sheaf *sheaf)
 {
 	unsigned long flags;
@@ -6065,7 +6074,7 @@ static void rcu_free_sheaf(struct rcu_head *head)
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 {
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
@@ -6081,9 +6090,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 
 	if (unlikely(!pcs->rcu_free)) {
-
 		struct slab_sheaf *empty;
 		struct node_barn *barn;
+		unsigned int alloc_flags = SLAB_ALLOC_DEFAULT;
+		gfp_t gfp = GFP_NOWAIT;
 
 		/* Bootstrap or debug cache, fall back */
 		if (unlikely(!cache_has_sheaves(s))) {
@@ -6103,7 +6113,7 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 			goto fail;
 		}
 
Quoted 4 lines

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6116

Quoted 2 lines
mm/slub.c +1 -1
-		empty = barn_get_empty_sheaf(barn, true);
+		empty = barn_get_empty_sheaf(barn, allow_spin);

Here we might be getting a sheaf allocated previously with kmalloc().

Right.

Quoted 5 lines
Quoted 5 lines
mm/slub.c +0 -0
 		if (empty) {
 			pcs->rcu_free = empty;
@@ -6112,20 +6122,25 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 
Quoted 8 lines

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6127

Quoted 6 lines
mm/slub.c +3 -1
 		local_unlock(&s->cpu_sheaves->lock);
 
-		empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
+		if (unlikely(!allow_spin)) {
+			alloc_flags = SLAB_ALLOC_TRYLOCK;
+			gfp = 0;

Maybe __GFP_NOWARN?

Ouch. Good catch. Thanks! Will do.

Quoted 7 lines

Here we would get a sheaf with kmalloc_nolock() so that's ok even if it's later freed by someone else by kfree(), right.

Quoted 4 lines
mm/slub.c +3 -0
+		}
+
+		empty = alloc_empty_sheaf(s, gfp, alloc_flags);
 
Quoted 8 lines

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6136

Quoted 6 lines
mm/slub.c +1 -1
 		if (!empty)
 			goto fail;
 
 		if (!local_trylock(&s->cpu_sheaves->lock)) {
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);

Well we could still use the barn with allow_spin == true.

Initially I did

if (!__barn_put_empty_sheaf(barn, empty, allow_spin))

	__free_empty_sheaf(s, empty, allow_spin);

but I ended up just calling __free_empty_sheaf() because it's pretty rare to hit... no strong opinion though.

Quoted 2 lines

But more crucially, here we might be freeing with kfree_nolock() a sheaf from the barn previously allocated with kmalloc()?

Well, we don't release and reacquire the local lock when we got an empty sheaf from the barn, so it doesn't free the sheaf from the barn?

That was indeed very subtle and I got confused :D.

When we free a sheaf in this function, it's always allocated in current context?

Quoted 8 lines

Maybe we need to track if it's the case and defer-free it or something.

Also maybe there could be a wrapper kfree_maybe_nolock() (~better name?) That means "I want to kfree safely in kfree_nolock() context something that MIGHT have been kmalloc()"
And maybe depending on the debugging options that make kmalloc() -> kfree_nolock() incompatible, if those are not enabled, it wouldn't have to defer, but proceed normally?

But I really like the idea of supporting kmalloc() -> kfree_nolock(), and I think it's worth exploring that.

Thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
Quoted 24 lines
Quoted 24 lines
mm/slub.c +7 -1
 			goto fail;
 		}
 
 		pcs = this_cpu_ptr(s->cpu_sheaves);
 
 		if (unlikely(pcs->rcu_free))
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);
 		else
 			pcs->rcu_free = empty;
 	}
@@ -6143,6 +6158,12 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
+		if (unlikely(!allow_spin)) {
+			/* call_rcu() cannot be called in an unknown context */
+			rcu_sheaf->size--;
+			local_unlock(&s->cpu_sheaves->lock);
+			goto fail;
+		}
 		pcs->rcu_free = NULL;
 		rcu_sheaf->node = numa_node_id();
 	}
#18 lore raw

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

Quoted 16 lines · Harry Yoo
Quoted 7 lines

Here we would get a sheaf with kmalloc_nolock() so that's ok even if it's later freed by someone else by kfree(), right.

Quoted 4 lines
mm/slub.c +3 -0
+		}
+
+		empty = alloc_empty_sheaf(s, gfp, alloc_flags);
Quoted 8 lines

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6136

Quoted 6 lines
mm/slub.c +1 -1
 		if (!empty)
 			goto fail;
 
 		if (!local_trylock(&s->cpu_sheaves->lock)) {
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);

Well we could still use the barn with allow_spin == true.

Quoted 7 lines

Initially I did

if (!__barn_put_empty_sheaf(barn, empty, allow_spin))

	__free_empty_sheaf(s, empty, allow_spin);

but I ended up just calling __free_empty_sheaf() because it's pretty rare to hit... no strong opinion though.

Yeah we can leave it simpler as it's rare.

Quoted 10 lines
Quoted 2 lines

But more crucially, here we might be freeing with kfree_nolock() a sheaf from the barn previously allocated with kmalloc()?

Well, we don't release and reacquire the local lock when we got an empty sheaf from the barn, so it doesn't free the sheaf from the barn?

That was indeed very subtle and I got confused :D.

When we free a sheaf in this function, it's always allocated in current context?

Indeed! It's fine then.

Quoted 11 lines
Quoted 8 lines

Maybe we need to track if it's the case and defer-free it or something.

Also maybe there could be a wrapper kfree_maybe_nolock() (~better name?) That means "I want to kfree safely in kfree_nolock() context something that MIGHT have been kmalloc()"
And maybe depending on the debugging options that make kmalloc() -> kfree_nolock() incompatible, if those are not enabled, it wouldn't have to defer, but proceed normally?

But I really like the idea of supporting kmalloc() -> kfree_nolock(), and I think it's worth exploring that.

At least my confusion potentially lead to something good then :)

Quoted 2 lines

Thanks!

#19 lore raw

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

In reply to Harry Yoo (Oracle)
Quoted 38 lines · Harry Yoo (Oracle)

Teach kfree_rcu_sheaf() how to handle the !allow_spin case. Try to get an empty sheaf from pcs->spare or the barn even when spinning is not allowed. Unlike __pcs_replace_full_main(), try harder to allocate an empty sheaf because the fallback path will be more expensive than kfree_nolock().

When trylock fails or the kernel observes non-NULL pcs->rcu_free after lock acquisition, free the sheaf instead of putting it to the barn. This is rare and not worth complicating the code.

Since call_rcu() cannot be called in an unknown context, kfree_rcu_sheaf() fails when the rcu sheaf becomes full.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  2 +-
 mm/slub.c        | 39 ++++++++++++++++++++++++++++++---------
 3 files changed, 32 insertions(+), 11 deletions(-)
mm/slab.h +1 -1
index 509f330654b8..b1bd33a16544 100644
@@ -429,7 +429,7 @@ static inline bool is_kmalloc_normal(struct kmem_cache *s)
 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
 }
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin);
 void flush_all_rcu_sheaves(void);
 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
 
mm/slab_common.c +0 -0
index b6426d7ceec9..bc1a8ec938d9 100644

mm/slab_common.c static bool kfree_rcu_sheaf(void *obj) · line 1608

Quoted 5 lines
mm/slab_common.c +1 -1
@@ -1605,7 +1605,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj);
+		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);

Since this is stacked on top of the slab alloc flags work, could it pass slab alloc flags instead of allow_spin?

Quoted 57 lines
mm/slab_common.c +0 -0
 	return false;
 }
mm/slub.c +13 -4
index 87ca154ccd80..b0d38d515386 100644
@@ -2815,7 +2815,8 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 	return __alloc_empty_sheaf(s, gfp, alloc_flags, s->sheaf_capacity);
 }
 
-static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
+			       bool allow_spin)
 {
 	/*
 	 * If the sheaf was created with SLAB_ALLOC_NO_RECURSE flag then its
@@ -2827,11 +2828,20 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 		mark_obj_codetag_empty(sheaf);
 
 	VM_WARN_ON_ONCE(sheaf->size > 0);
-	kfree(sheaf);
+
+	if (likely(allow_spin))
+		kfree(sheaf);
+	else
+		kfree_nolock(sheaf);
 
 	stat(s, SHEAF_FREE);
 }
 
+static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
+{
+	__free_empty_sheaf(s, sheaf, /* allow_spin = */ true);
+}
+
 static unsigned int
 refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
 	       unsigned int max);
@@ -3132,7 +3142,6 @@ static struct slab_sheaf *barn_get_empty_sheaf(struct node_barn *barn,
  * intended action due to a race or cpu migration. Thus they do not check the
  * empty or full sheaf limits for simplicity.
  */
-
 static void barn_put_empty_sheaf(struct node_barn *barn, struct slab_sheaf *sheaf)
 {
 	unsigned long flags;
@@ -6065,7 +6074,7 @@ static void rcu_free_sheaf(struct rcu_head *head)
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 {
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6095

Quoted 7 lines
mm/slub.c +1 -1
@@ -6081,9 +6090,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 
 	if (unlikely(!pcs->rcu_free)) {
-
 		struct slab_sheaf *empty;
 		struct node_barn *barn;
+		unsigned int alloc_flags = SLAB_ALLOC_DEFAULT;

which would make this logic more natural.

Quoted 14 lines
mm/slub.c +2 -1
+		gfp_t gfp = GFP_NOWAIT;
 
 		/* Bootstrap or debug cache, fall back */
 		if (unlikely(!cache_has_sheaves(s))) {
@@ -6103,7 +6113,7 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 			goto fail;
 		}
 
-		empty = barn_get_empty_sheaf(barn, true);
+		empty = barn_get_empty_sheaf(barn, allow_spin);
 
 		if (empty) {
 			pcs->rcu_free = empty;

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6127

Quoted 7 lines
mm/slub.c +3 -1
@@ -6112,20 +6122,25 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 
 		local_unlock(&s->cpu_sheaves->lock);
 
-		empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
+		if (unlikely(!allow_spin)) {
+			alloc_flags = SLAB_ALLOC_TRYLOCK;
+			gfp = 0;

and this as well (alloc_empty_sheaf() could derive gfp from whatever you passed it, by simply knowing alloc_flags = TRYLOCK -> gfp = 0 (or gfp &= ~__GFP_RECLAIM)).

Quoted 38 lines
mm/slub.c +11 -2
+		}
+
+		empty = alloc_empty_sheaf(s, gfp, alloc_flags);
 
 		if (!empty)
 			goto fail;
 
 		if (!local_trylock(&s->cpu_sheaves->lock)) {
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);
 			goto fail;
 		}
 
 		pcs = this_cpu_ptr(s->cpu_sheaves);
 
 		if (unlikely(pcs->rcu_free))
-			barn_put_empty_sheaf(barn, empty);
+			__free_empty_sheaf(s, empty, allow_spin);
 		else
 			pcs->rcu_free = empty;
 	}
@@ -6143,6 +6158,12 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
+		if (unlikely(!allow_spin)) {
+			/* call_rcu() cannot be called in an unknown context */
+			rcu_sheaf->size--;
+			local_unlock(&s->cpu_sheaves->lock);
+			goto fail;
+		}
 		pcs->rcu_free = NULL;
 		rcu_sheaf->node = numa_node_id();
 	}
Signature · 2 lines
-- 
2.53.0
Signature · 1 line
Pedro
author maintainer
#20 lore raw

Re: [PATCH for-next v3 3/9] mm/slab: handle the !allow_spin case in kfree_rcu_sheaf()

Quoted 40 lines · Pedro Falcato
Quoted 39 lines · Harry Yoo (Oracle)

Teach kfree_rcu_sheaf() how to handle the !allow_spin case. Try to get an empty sheaf from pcs->spare or the barn even when spinning is not allowed. Unlike __pcs_replace_full_main(), try harder to allocate an empty sheaf because the fallback path will be more expensive than kfree_nolock().

When trylock fails or the kernel observes non-NULL pcs->rcu_free after lock acquisition, free the sheaf instead of putting it to the barn. This is rare and not worth complicating the code.

Since call_rcu() cannot be called in an unknown context, kfree_rcu_sheaf() fails when the rcu sheaf becomes full.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  2 +-
 mm/slub.c        | 39 ++++++++++++++++++++++++++++++---------
 3 files changed, 32 insertions(+), 11 deletions(-)
mm/slab.h +1 -1
index 509f330654b8..b1bd33a16544 100644
@@ -429,7 +429,7 @@ static inline bool is_kmalloc_normal(struct kmem_cache *s)
 	return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
 }
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin);
 void flush_all_rcu_sheaves(void);
 void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
 
mm/slab_common.c +0 -0
index b6426d7ceec9..bc1a8ec938d9 100644
@@ -1605,7 +1605,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
Quoted 7 lines

mm/slab_common.c static bool kfree_rcu_sheaf(void *obj) · line 1608

Quoted 4 lines
mm/slab_common.c +1 -1
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj);
+		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);

Since this is stacked on top of the slab alloc flags work, could it pass slab alloc flags instead of allow_spin?

Good point.

Hmm, I thought we'll eventually introduce SLAB_FREE_ flags rather than reusing SLAB_ALLOC_ flags (and then translate SLAB_FREE_NOLOCK to SLAB_ALLOC_NOLOCK when allocating sheaves) rather than reusing SLAB_ALLOC_ flags...

Quoted 12 lines
Quoted 12 lines
mm/slab_common.c +1 -1
@@ -6065,7 +6074,7 @@ static void rcu_free_sheaf(struct rcu_head *head)
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
-bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
+bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 {
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
@@ -6081,9 +6090,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 	pcs = this_cpu_ptr(s->cpu_sheaves);
 
Quoted 7 lines

mm/slab_common.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6095

Quoted 5 lines
mm/slab_common.c +1 -1
 	if (unlikely(!pcs->rcu_free)) {
-
 		struct slab_sheaf *empty;
 		struct node_barn *barn;
+		unsigned int alloc_flags = SLAB_ALLOC_DEFAULT;

which would make this logic more natural.

Right.

Quoted 17 lines
Quoted 17 lines
mm/slab_common.c +2 -1
+		gfp_t gfp = GFP_NOWAIT;
 
 		/* Bootstrap or debug cache, fall back */
 		if (unlikely(!cache_has_sheaves(s))) {
@@ -6103,7 +6113,7 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 			goto fail;
 		}
 
-		empty = barn_get_empty_sheaf(barn, true);
+		empty = barn_get_empty_sheaf(barn, allow_spin);
 
 		if (empty) {
 			pcs->rcu_free = empty;
@@ -6112,20 +6122,25 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj)
 
 		local_unlock(&s->cpu_sheaves->lock);
 
Quoted 8 lines

mm/slab_common.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj) · line 6127

Quoted 4 lines
mm/slab_common.c +3 -1
-		empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
+		if (unlikely(!allow_spin)) {
+			alloc_flags = SLAB_ALLOC_TRYLOCK;
+			gfp = 0;

and this as well (alloc_empty_sheaf() could derive gfp from whatever you passed it, by simply knowing alloc_flags = TRYLOCK -> gfp = 0 (or gfp &= ~__GFP_RECLAIM)).

Right.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#21 lore raw

PATCH v3 4/9 mm/slab: use call_rcu() in unknown context if irqs are enabled

In reply to Harry Yoo (Oracle)

call_rcu() disables IRQs with local_irq_save() to protect its per-cpu data structures. Therefore, if IRQs are not disabled, they cannot be corrupted by reentrance into call_rcu(). So fall back to the deferred path only when !allow_spin && irqs_disabled().

The RCU subsystem does not guarantee this contractually, and this optimization relies on RCU's implementation details. Ideally, it should be removed once call_rcu_nolock() is supported by the RCU subsystem.

---
 mm/slub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/slub.c +2 -2
index b0d38d515386..6a3552b70683 100644
@@ -6158,8 +6158,8 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
-		if (unlikely(!allow_spin)) {
-			/* call_rcu() cannot be called in an unknown context */
+		/* call_rcu() disables IRQs to protect percpu data structures */
+		if (unlikely(!allow_spin && irqs_disabled())) {
 			rcu_sheaf->size--;
 			local_unlock(&s->cpu_sheaves->lock);
 			goto fail;
Signature · 1 line
2.53.0
#22 lore raw

Re: [PATCH for-next v3 4/9] mm/slab: use call_rcu() in unknown context if irqs are enabled

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Acquiring a local_trylock inside a raw_spinlock_t wait context triggers a lockdep Invalid wait context warning. --

commit 05f00697f28bd84542538d9e89bbc00e3f7a427a
Author: Harry Yoo (Oracle) <[email protected]>

mm/slab: use call_rcu() in unknown context if irqs are enabled

This commit modifies the fallback logic for deferred RCU freeing in the slab allocator. It now uses call_rcu() even in an unknown context as long as interrupts are enabled.

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin) · line 6161

Quoted 11 lines
mm/slub.c +1 -2
index b0d38d5153865..6a3552b70683d 100644
@@ -6158,8 +6158,8 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
-		if (unlikely(!allow_spin)) {
-			/* call_rcu() cannot be called in an unknown context */
+		/* call_rcu() disables IRQs to protect percpu data structures */

[Severity: High]
This isn't a bug introduced by this patch, but since you are modifying __kfree_rcu_sheaf() here, can this function trigger an invalid wait context warning in lockdep?

According to the RCU guidelines, kvfree_call_rcu() is frequently called while holding a raw_spinlock_t (such as pi_lock in kernel/sched/core.c) and from hardirq context:

mm/slab_common.c:kvfree_call_rcu() {
    ...
    if (kfree_rcu_sheaf(ptr))
        return;
    ...
}

This calls into __kfree_rcu_sheaf(), which unconditionally attempts to acquire a local_lock earlier in the function:

mm/slub.c:__kfree_rcu_sheaf() {
    ...
    if (!local_trylock(&s->cpu_sheaves->lock))
        goto fail;
    ...
}

Because local_lock has a lower lockdep wait type than raw_spinlock_t, does this wait context inversion violate nesting rules and trigger warnings?

Guarding lock_map_acquire_try with !IS_ENABLED(CONFIG_PREEMPT_RT) does not prevent this because local_trylock internally checks wait contexts regardless. Could this lead to panics on systems with panic_on_warn enabled?

Quoted 4 lines
mm/slub.c +1 -0
+		if (unlikely(!allow_spin && irqs_disabled())) {
 			rcu_sheaf->size--;
 			local_unlock(&s->cpu_sheaves->lock);
 			goto fail;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#23 lore raw

Re: [PATCH for-next v3 4/9] mm/slab: use call_rcu() in unknown context if irqs are enabled

In reply to Harry Yoo (Oracle)
Quoted 5 lines · Harry Yoo (Oracle)

call_rcu() disables IRQs with local_irq_save() to protect its per-cpu data structures. Therefore, if IRQs are not disabled, they cannot be corrupted by reentrance into call_rcu(). So fall back to the deferred path only when !allow_spin && irqs_disabled().

Quoted 7 lines

The RCU subsystem does not guarantee this contractually, and this optimization relies on RCU's implementation details. Ideally, it should be removed once call_rcu_nolock() is supported by the RCU subsystem.

Hope RCU people can approve this.

Quoted 9 lines
---
 mm/slub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/slub.c +0 -0
index b0d38d515386..6a3552b70683 100644

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin) · line 6161

Quoted 6 lines
mm/slub.c +1 -2
@@ -6158,8 +6158,8 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
-		if (unlikely(!allow_spin)) {
-			/* call_rcu() cannot be called in an unknown context */
+		/* call_rcu() disables IRQs to protect percpu data structures */

Maybe elaborate that with !allow_spin we might have preempted call_rcu()'s protected section, which disables IRQs, so we can't call_rcu(). But if IRQs are not disabled, we know it's not the case?

Quoted 5 lines
mm/slub.c +1 -0
+		if (unlikely(!allow_spin && irqs_disabled())) {
 			rcu_sheaf->size--;
 			local_unlock(&s->cpu_sheaves->lock);
 			goto fail;
#24 lore raw

Re: [PATCH for-next v3 4/9] mm/slab: use call_rcu() in unknown context if irqs are enabled

In reply to Harry Yoo (Oracle)
Quoted 21 lines · Harry Yoo (Oracle)

call_rcu() disables IRQs with local_irq_save() to protect its per-cpu data structures. Therefore, if IRQs are not disabled, they cannot be corrupted by reentrance into call_rcu(). So fall back to the deferred path only when !allow_spin && irqs_disabled().

The RCU subsystem does not guarantee this contractually, and this optimization relies on RCU's implementation details. Ideally, it should be removed once call_rcu_nolock() is supported by the RCU subsystem.

---
 mm/slub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/slub.c +0 -0
index b0d38d515386..6a3552b70683 100644

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin) · line 6162

Quoted 7 lines
mm/slub.c +2 -2
@@ -6158,8 +6158,8 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
-		if (unlikely(!allow_spin)) {
-			/* call_rcu() cannot be called in an unknown context */
+		/* call_rcu() disables IRQs to protect percpu data structures */
+		if (unlikely(!allow_spin && irqs_disabled())) {

Perhaps this could be more neatly exposed by RCU? something like maybe_in_call_rcu(). Also, it's a bit odd that you're introducing code and then subsequently patching over it with an improvement. Maybe this could be squashed into patch 3?

Quoted 7 lines
mm/slub.c +0 -0
 			rcu_sheaf->size--;
 			local_unlock(&s->cpu_sheaves->lock);
 			goto fail;
Signature · 2 lines
-- 
2.53.0
Signature · 1 line
Pedro
author maintainer
#25 lore raw

Re: [PATCH for-next v3 4/9] mm/slab: use call_rcu() in unknown context if irqs are enabled

Quoted 17 lines · Pedro Falcato
Quoted 16 lines · Harry Yoo (Oracle)

call_rcu() disables IRQs with local_irq_save() to protect its per-cpu data structures. Therefore, if IRQs are not disabled, they cannot be corrupted by reentrance into call_rcu(). So fall back to the deferred path only when !allow_spin && irqs_disabled().

The RCU subsystem does not guarantee this contractually, and this optimization relies on RCU's implementation details. Ideally, it should be removed once call_rcu_nolock() is supported by the RCU subsystem.

---
 mm/slub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 15 lines

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin) · line 6162

Quoted 12 lines
mm/slub.c +2 -2
index b0d38d515386..6a3552b70683 100644
@@ -6158,8 +6158,8 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
-		if (unlikely(!allow_spin)) {
-			/* call_rcu() cannot be called in an unknown context */
+		/* call_rcu() disables IRQs to protect percpu data structures */
+		if (unlikely(!allow_spin && irqs_disabled())) {

Perhaps this could be more neatly exposed by RCU? something like maybe_in_call_rcu().

Make sense if we need to keep this optimization for a long time, but hopefully RCU would support call_rcu_nolock() soon (apparently Paul has a plan [1]), rather than introducing maybe_in_call_rcu() just to drop it soon.

But I don't have a strong opinion on this and
I'd leave that to RCU folks.

Quoted 3 lines

Also, it's a bit odd that you're introducing code and
then subsequently patching over it with an improvement. Maybe this could be squashed into patch 3?

I was hoping having it as a separate patch to make it easier for RCU folks to take a look and provide ack or nack :)

[1]
https://lore.kernel.org/linux-mm/03da8742-3790-4428-84b8-11911bf44c9e@paulmck-laptop/

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#26 lore raw

PATCH v3 5/9 mm/slab: extend deferred free mechanism to handle rcu sheaves

In reply to Harry Yoo (Oracle)

__kfree_rcu_sheaf() cannot invoke call_rcu() when spinning is not allowed and IRQs are disabled. To relax the limitation, extend the deferred free fallback so that a full rcu sheaf can be submitted to call_rcu() via the existing IRQ work.

Since the deferred mechanism does more than deferred free of objects, rename the struct to deferred_percpu_work and adjust names accordingly.

When a sheaf is queued on an IRQ work, it is detached from pcs->rcu_free but call_rcu() is not invoked until the irq_work runs. To keep the kvfree_rcu barrier's promise, call irq_work_sync() on each CPU before calling rcu_barrier().

In the meantime, remove the TODO item as apparently there is no simple and effective way to achieve that.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  7 ++---
 mm/slub.c        | 79 ++++++++++++++++++++++++++++++++++----------------------
 3 files changed, 51 insertions(+), 37 deletions(-)
mm/slab.h +1 -1
index b1bd33a16544..961581e35ec8 100644
@@ -744,7 +744,7 @@ void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
 void __check_heap_object(const void *ptr, unsigned long n,
 			 const struct slab *slab, bool to_user);
 
-void defer_free_barrier(void);
+void deferred_work_barrier(void);
 
 static inline bool slub_debug_orig_size(struct kmem_cache *s)
 {
mm/slab_common.c +2 -5
index bc1a8ec938d9..55546b8385ff 100644
@@ -551,7 +551,7 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	}
 
 	/* Wait for deferred work from kmalloc/kfree_nolock() */
-	defer_free_barrier();
+	deferred_work_barrier();
 
 	cpus_read_lock();
 	mutex_lock(&slab_mutex);
@@ -2113,13 +2113,10 @@ void kvfree_rcu_barrier_on_cache(struct kmem_cache *s)
 		cpus_read_lock();
 		flush_rcu_sheaves_on_cache(s);
 		cpus_read_unlock();
+		deferred_work_barrier();
 		rcu_barrier();
 	}
 
-	/*
-	 * TODO: Introduce a version of __kvfree_rcu_barrier() that works
-	 * on a specific slab cache.
-	 */
 	__kvfree_rcu_barrier();
 }
 EXPORT_SYMBOL_GPL(kvfree_rcu_barrier_on_cache);
mm/slub.c +48 -31

144 lines not on this page · raw message

Signature · 1 line
2.53.0
#27 lore raw

Re: [PATCH for-next v3 5/9] mm/slab: extend deferred free mechanism to handle rcu sheaves

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Acquiring local_trylock inside __kfree_rcu_sheaf() violates lock nesting rules on PREEMPT_RT, triggering a lockdep "Invalid wait context" warning.

Pre-existing issues:
- [High] Concurrent lockless calls to deferred_work_barrier() cause an rcuwait race on PREEMPT_RT, leading to permanent task hangs. --

commit b0c903d6396ede7c44ac45135ec7adc7d173391f
Author: Harry Yoo (Oracle) <[email protected]>
Subject: mm/slab: extend deferred free mechanism to handle rcu sheaves

This patch extends the deferred free fallback mechanism so that a full RCU sheaf can be submitted to call_rcu() via the existing IRQ work when spinning is not allowed and IRQs are disabled. It also renames the defer_free structs to deferred_percpu_work and adds deferred_work_barrier() calls to keep the kvfree_rcu barrier's promise.

Quoted 4 lines
mm/slub.c +0 -0
index 6a3552b70683d..ba593c1c53d50 100644

[ ... ]

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin) · line 6177

Quoted 4 lines
mm/slub.c +0 -0
@@ -6158,12 +6175,6 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {

[Severity: High]
Does acquiring local_trylock inside __kfree_rcu_sheaf() violate lock nesting rules on PREEMPT_RT?

The kvfree_call_rcu() API is frequently invoked from hardirq context or while holding a raw_spinlock_t. This function attempts to acquire a local_trylock:

    if (!local_trylock(&s->cpu_sheaves->lock))

On PREEMPT_RT kernels, local_trylock_t maps to a sleepable spinlock_t. Attempting to acquire a sleepable lock (even via a trylock operation) while in a raw_spinlock or hardirq context violates the lock nesting rules. CONFIG_PROVE_RAW_LOCK_NESTING will catch this and trigger a lockdep warning for an invalid wait context.

Quoted 9 lines
mm/slub.c +0 -6
-		/* call_rcu() disables IRQs to protect percpu data structures */
-		if (unlikely(!allow_spin && irqs_disabled())) {
-			rcu_sheaf->size--;
-			local_unlock(&s->cpu_sheaves->lock);
-			goto fail;
-		}
 		pcs->rcu_free = NULL;
 		rcu_sheaf->node = numa_node_id();
 	}

[ ... ]

Quoted 1 line
mm/slub.c +0 -0
@@ -6408,27 +6418,34 @@ static void free_deferred_objects(struct irq_work *work)

[ ... ]

Quoted 14 lines

static void defer_free(struct kmem_cache *s, void *head) {

mm/slub.c +3 -4
-	struct defer_free *df;
+	struct deferred_percpu_work *dpw;
 
 	guard(preempt)();
 
 	head = kasan_reset_tag(head);
 
-	df = this_cpu_ptr(&defer_free_objects);
-	if (llist_add(head + s->offset, &df->objects))
-		irq_work_queue(&df->work);
+	dpw = this_cpu_ptr(&deferred_percpu_work);
+	if (llist_add(head + s->offset, &dpw->objects))

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6431

Quoted 10 lines
mm/slub.c +2 -2
+		irq_work_queue(&dpw->work);
 }
 
-void defer_free_barrier(void)
+void deferred_work_barrier(void)
 {
 	int cpu;
 
 	for_each_possible_cpu(cpu)
-		irq_work_sync(&per_cpu_ptr(&defer_free_objects, cpu)->work);

[Severity: High]
This is a pre-existing issue, but can concurrent lockless calls to deferred_work_barrier() cause an rcuwait race on PREEMPT_RT, leading to permanent task hangs?

The function iterates over all CPUs, invoking irq_work_sync() on each CPU's deferred work object. On PREEMPT_RT, irq_work_sync() relies on rcuwait_wait_event() to block until completion, and the rcuwait synchronization primitive strictly allows only one waiter at a time.

Because deferred_work_barrier() is called without any global serialization (for instance, in kmem_cache_destroy() and kvfree_rcu_barrier_on_cache(), and now in flush_all_rcu_sheaves()), multiple threads can enter irq_work_sync() for the same work object concurrently.

This overwrites the waiter task pointer, meaning only one task will be woken up when the work completes, leaving the other tasks hanging permanently in an uninterruptible sleep.

Quoted 4 lines
mm/slub.c +1 -0
+		irq_work_sync(&per_cpu_ptr(&deferred_percpu_work, cpu)->work);
 }
 
 static __fastpath_inline
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer Reviewed-by on v3 5/9
#28 lore raw

Re: [PATCH for-next v3 5/9] mm/slab: extend deferred free mechanism to handle rcu sheaves

In reply to Harry Yoo (Oracle)
Quoted 13 lines · Harry Yoo (Oracle)

__kfree_rcu_sheaf() cannot invoke call_rcu() when spinning is not allowed and IRQs are disabled. To relax the limitation, extend the deferred free fallback so that a full rcu sheaf can be submitted to call_rcu() via the existing IRQ work.

Since the deferred mechanism does more than deferred free of objects, rename the struct to deferred_percpu_work and adjust names accordingly.

When a sheaf is queued on an IRQ work, it is detached from pcs->rcu_free but call_rcu() is not invoked until the irq_work runs. To keep the kvfree_rcu barrier's promise, call irq_work_sync() on each CPU before calling rcu_barrier().

Quoted 5 lines

In the meantime, remove the TODO item as apparently there is no simple and effective way to achieve that.

LGTM.

Reviewed-by on v3 5/9
#29 lore raw

Re: [PATCH for-next v3 5/9] mm/slab: extend deferred free mechanism to handle rcu sheaves

In reply to Harry Yoo (Oracle)
Quoted 52 lines · Harry Yoo (Oracle)

__kfree_rcu_sheaf() cannot invoke call_rcu() when spinning is not allowed and IRQs are disabled. To relax the limitation, extend the deferred free fallback so that a full rcu sheaf can be submitted to call_rcu() via the existing IRQ work.

Since the deferred mechanism does more than deferred free of objects, rename the struct to deferred_percpu_work and adjust names accordingly.

When a sheaf is queued on an IRQ work, it is detached from pcs->rcu_free but call_rcu() is not invoked until the irq_work runs. To keep the kvfree_rcu barrier's promise, call irq_work_sync() on each CPU before calling rcu_barrier().

In the meantime, remove the TODO item as apparently there is no simple and effective way to achieve that.

---
 mm/slab.h        |  2 +-
 mm/slab_common.c |  7 ++---
 mm/slub.c        | 79 ++++++++++++++++++++++++++++++++++----------------------
 3 files changed, 51 insertions(+), 37 deletions(-)
mm/slab.h +1 -1
index b1bd33a16544..961581e35ec8 100644
@@ -744,7 +744,7 @@ void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
 void __check_heap_object(const void *ptr, unsigned long n,
 			 const struct slab *slab, bool to_user);
 
-void defer_free_barrier(void);
+void deferred_work_barrier(void);
 
 static inline bool slub_debug_orig_size(struct kmem_cache *s)
 {
mm/slab_common.c +1 -1
index bc1a8ec938d9..55546b8385ff 100644
@@ -551,7 +551,7 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	}
 
 	/* Wait for deferred work from kmalloc/kfree_nolock() */
-	defer_free_barrier();
+	deferred_work_barrier();
 
 	cpus_read_lock();
 	mutex_lock(&slab_mutex);
@@ -2113,13 +2113,10 @@ void kvfree_rcu_barrier_on_cache(struct kmem_cache *s)
 		cpus_read_lock();

mm/slab_common.c void kvfree_rcu_barrier_on_cache(struct kmem_cache *s) · line 2122

Quoted 10 lines
mm/slab_common.c +1 -4
 		flush_rcu_sheaves_on_cache(s);
 		cpus_read_unlock();
+		deferred_work_barrier();
 		rcu_barrier();
 	}
 
-	/*
-	 * TODO: Introduce a version of __kvfree_rcu_barrier() that works
-	 * on a specific slab cache.
-	 */

Perhaps could be worth detailing why this is not possible.

Quoted 118 lines
mm/slab_common.c +0 -0
 	__kvfree_rcu_barrier();
 }
 EXPORT_SYMBOL_GPL(kvfree_rcu_barrier_on_cache);
mm/slub.c +35 -25
index 6a3552b70683..ba593c1c53d5 100644
@@ -418,6 +418,8 @@ struct slab_sheaf {
 	union {
 		struct rcu_head rcu_head;
 		struct list_head barn_list;
+		/* only used to defer call_rcu() in unknown context */
+		struct llist_node llnode;
 		/* only used for prefilled sheafs */
 		struct {
 			unsigned int capacity;
@@ -4071,6 +4073,20 @@ static void flush_all(struct kmem_cache *s)
 	cpus_read_unlock();
 }
 
+struct deferred_percpu_work {
+	struct llist_head objects;
+	struct llist_head rcu_sheaves;
+	struct irq_work work;
+};
+
+static void deferred_percpu_work_fn(struct irq_work *work);
+
+static DEFINE_PER_CPU(struct deferred_percpu_work, deferred_percpu_work) = {
+	.objects = LLIST_HEAD_INIT(objects),
+	.rcu_sheaves = LLIST_HEAD_INIT(rcu_sheaves),
+	.work = IRQ_WORK_INIT(deferred_percpu_work_fn),
+};
+
 static void flush_rcu_sheaf(struct work_struct *w)
 {
 	struct slub_percpu_sheaves *pcs;
@@ -4142,6 +4158,7 @@ void flush_all_rcu_sheaves(void)
 	mutex_unlock(&slab_mutex);
 	cpus_read_unlock();
 
+	deferred_work_barrier();
 	rcu_barrier();
 }
 
@@ -6158,12 +6175,6 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
 		rcu_sheaf = NULL;
 	} else {
-		/* call_rcu() disables IRQs to protect percpu data structures */
-		if (unlikely(!allow_spin && irqs_disabled())) {
-			rcu_sheaf->size--;
-			local_unlock(&s->cpu_sheaves->lock);
-			goto fail;
-		}
 		pcs->rcu_free = NULL;
 		rcu_sheaf->node = numa_node_id();
 	}
@@ -6172,8 +6183,18 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	 * we flush before local_unlock to make sure a racing
 	 * flush_all_rcu_sheaves() doesn't miss this sheaf
 	 */
-	if (rcu_sheaf)
-		call_rcu(&rcu_sheaf->rcu_head, rcu_free_sheaf);
+	if (rcu_sheaf) {
+		/* call_rcu() disables IRQs to protect percpu data structures */
+		if (unlikely(!allow_spin && irqs_disabled())) {
+			struct deferred_percpu_work *dpw;
+
+			dpw = this_cpu_ptr(&deferred_percpu_work);
+			if (llist_add(&rcu_sheaf->llnode, &dpw->rcu_sheaves))
+				irq_work_queue(&dpw->work);
+		} else {
+			call_rcu(&rcu_sheaf->rcu_head, rcu_free_sheaf);
+		}
+	}
 
 	local_unlock(&s->cpu_sheaves->lock);
 
@@ -6360,31 +6381,20 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
 	}
 }
 
-struct defer_free {
-	struct llist_head objects;
-	struct irq_work work;
-};
-
-static void free_deferred_objects(struct irq_work *work);
-
-static DEFINE_PER_CPU(struct defer_free, defer_free_objects) = {
-	.objects = LLIST_HEAD_INIT(objects),
-	.work = IRQ_WORK_INIT(free_deferred_objects),
-};
-
 /*
  * In PREEMPT_RT irq_work runs in per-cpu kthread, so it's safe
  * to take sleeping spin_locks from __slab_free().
  * In !PREEMPT_RT irq_work will run after local_unlock_irqrestore().
  */
-static void free_deferred_objects(struct irq_work *work)
+static void deferred_percpu_work_fn(struct irq_work *work)
 {
-	struct defer_free *df = container_of(work, struct defer_free, work);
-	struct llist_head *objs = &df->objects;
+	struct deferred_percpu_work *dpw;
+	struct llist_head *objs, *rcu_sheaves;
 	struct llist_node *llnode, *pos, *t;
 
-	if (llist_empty(objs))
-		return;
+	dpw = container_of(work, struct deferred_percpu_work, work);
+	rcu_sheaves = &dpw->rcu_sheaves;
+	objs = &dpw->objects;
 
 	llnode = llist_del_all(objs);
 	llist_for_each_safe(pos, t, llnode) {

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6423

Quoted 6 lines
mm/slub.c +3 -0
@@ -6408,27 +6418,34 @@ static void free_deferred_objects(struct irq_work *work)
 		__slab_free(s, slab, x, x, 1, _THIS_IP_);
 		stat(s, FREE_SLOWPATH);
 	}
+
+	llnode = llist_del_all(rcu_sheaves);
+	llist_for_each_safe(pos, t, llnode) {

llist_for_each_entry_safe?

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6429

Quoted 6 lines
mm/slub.c +4 -0
+		struct slab_sheaf *rcu_sheaf = llist_entry(pos, struct slab_sheaf, llnode);
+
+		call_rcu(&rcu_sheaf->rcu_head, rcu_free_sheaf);
+	}
 }
 

Otherwise LGTM!

Signature · 1 line
Pedro
author maintainer
#30 lore raw

PATCH v3 6/9 mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

In reply to Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

---
 mm/slab_common.c | 11 +++++++++--
 mm/slub.c        | 17 ++++++++++-------
 2 files changed, 19 insertions(+), 9 deletions(-)
mm/slab_common.c +9 -2
index 55546b8385ff..807924a94fb0 100644
@@ -1595,6 +1595,13 @@ static bool kfree_rcu_sheaf(void *obj)
 {
 	struct kmem_cache *s;
 	struct slab *slab;
+	bool allow_spin;
+
+	/*
+	 * It is not safe to spin on PREEMPT_RT because the kernel might be
+	 * holding a raw spinlock and slab acquires sleeping locks.
+	 */
+	allow_spin = !IS_ENABLED(CONFIG_PREEMPT_RT);
 
 	if (is_vmalloc_addr(obj))
 		return false;
@@ -1605,7 +1612,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);
+		return __kfree_rcu_sheaf(s, obj, allow_spin);
 
 	return false;
 }
@@ -1954,7 +1961,7 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
 	if (!head)
 		might_sleep();
 
-	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && kfree_rcu_sheaf(ptr))
+	if (kfree_rcu_sheaf(ptr))
 		return;
 
 	// Queue the object but don't yet schedule the batch.
mm/slub.c +10 -7
index ba593c1c53d5..4850629774b2 100644
@@ -6082,12 +6082,13 @@ static void rcu_free_sheaf(struct rcu_head *head)
  * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since
  * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT),
  * this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids
- * this problem by bypassing the sheaves layer entirely on PREEMPT_RT.
+ * this problem by passing allow_spin = false on PREEMPT_RT.
  *
  * However, lockdep still complains that it is invalid to acquire spinlock_t
  * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a
  * spinning lock. Tell lockdep that acquiring spinlock_t is valid here
- * by temporarily raising the wait-type to LD_WAIT_CONFIG.
+ * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
+ * on PREEMPT_RT to avoid suppressing valid lockdep warnings.
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
@@ -6096,10 +6097,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
 
-	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
-		return false;
+	VM_WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT) && allow_spin);
 
-	lock_map_acquire_try(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_acquire_try(&kfree_rcu_sheaf_map);
 
 	if (!local_trylock(&s->cpu_sheaves->lock))
 		goto fail;
@@ -6199,12 +6200,14 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	local_unlock(&s->cpu_sheaves->lock);
 
 	stat(s, FREE_RCU_SHEAF);
-	lock_map_release(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_release(&kfree_rcu_sheaf_map);
 	return true;
 
 fail:
 	stat(s, FREE_RCU_SHEAF_FAIL);
-	lock_map_release(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_release(&kfree_rcu_sheaf_map);
 	return false;
 }
 
Signature · 1 line
2.53.0
#31 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Calling kfree_rcu_sheaf() on PREEMPT_RT from kvfree_call_rcu() causes an 'Invalid wait context'…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Calling kfree_rcu_sheaf() on PREEMPT_RT from kvfree_call_rcu() causes an 'Invalid wait context' lockdep splat and potential deadlocks due to acquiring a sleeping lock from raw_spinlock, hardirq, or NMI contexts. --

commit 67f0bb194ef2843a6fa5bfccfbb53884ff1a37b2
Author: Harry Yoo (Oracle) <[email protected]>

mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

This commit allows kfree_rcu_sheaf() to be used on kernels with CONFIG_PREEMPT_RT by assuming that spinning is not allowed. It removes the unconditional bailout and relaxes the limitation to try the sheaves path.

Quoted 4 lines
mm/slab_common.c +0 -0
index 55546b8385ff3..807924a94fb08 100644

[ ... ]

Quoted 9 lines
mm/slab_common.c +1 -1
@@ -1954,7 +1961,7 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
 	if (!head)
 		might_sleep();
 
-	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && kfree_rcu_sheaf(ptr))
+	if (kfree_rcu_sheaf(ptr))
 		return;
 
 	// Queue the object but don't yet schedule the batch.

[ ... ]

Quoted 4 lines
mm/slub.c +0 -0
index ba593c1c53d50..4850629774b2a 100644

[ ... ]

mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin) · line 6105

Quoted 13 lines
mm/slub.c +3 -3
@@ -6096,10 +6097,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
 
-	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
-		return false;
+	VM_WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT) && allow_spin);
 
-	lock_map_acquire_try(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_acquire_try(&kfree_rcu_sheaf_map);
 
 	if (!local_trylock(&s->cpu_sheaves->lock))

[Severity: High]
Does acquiring this local lock with CONFIG_PREEMPT_RT enabled from kvfree_call_rcu() trigger an invalid wait context lockdep splat?

Since kvfree_call_rcu() can be called from hardirq or nmi context, or while holding a raw_spinlock_t (like pi_lock), the caller is in a raw spin wait context. With CONFIG_PREEMPT_RT, local_lock maps to a spinlock_t (rt_mutex), which is a sleeping lock.

Even though this uses a trylock and allow_spin is false, acquiring a sleeping lock under a raw spinlock or in hardirq context violates lockdep's wait context rules because lockdep evaluates the declared wait types of the locks, not the runtime behavior.

Additionally, could the rt_mutex trylock implementation internally attempt to acquire the task's pi_lock, leading to a potential deadlock?

Quoted 1 line
mm/slub.c +0 -0
 		goto fail;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#32 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

In reply to Harry Yoo (Oracle)
Quoted 9 lines · Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Quoted 6 lines

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT? I thought (in the Link: above) we'd only need to downgrade allow_spin to false on PREEMPT_RT for handling sheaves movement from/to barn and alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on RT? Or is the irqs_disabled() condition rare enough so we don't care?

Quoted 93 lines
---
 mm/slab_common.c | 11 +++++++++--
 mm/slub.c        | 17 ++++++++++-------
 2 files changed, 19 insertions(+), 9 deletions(-)
mm/slab_common.c +9 -2
index 55546b8385ff..807924a94fb0 100644
@@ -1595,6 +1595,13 @@ static bool kfree_rcu_sheaf(void *obj)
 {
 	struct kmem_cache *s;
 	struct slab *slab;
+	bool allow_spin;
+
+	/*
+	 * It is not safe to spin on PREEMPT_RT because the kernel might be
+	 * holding a raw spinlock and slab acquires sleeping locks.
+	 */
+	allow_spin = !IS_ENABLED(CONFIG_PREEMPT_RT);
 
 	if (is_vmalloc_addr(obj))
 		return false;
@@ -1605,7 +1612,7 @@ static bool kfree_rcu_sheaf(void *obj)
 
 	s = slab->slab_cache;
 	if (likely(!IS_ENABLED(CONFIG_NUMA) || slab_nid(slab) == numa_mem_id()))
-		return __kfree_rcu_sheaf(s, obj, /* allow_spin = */ true);
+		return __kfree_rcu_sheaf(s, obj, allow_spin);
 
 	return false;
 }
@@ -1954,7 +1961,7 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
 	if (!head)
 		might_sleep();
 
-	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && kfree_rcu_sheaf(ptr))
+	if (kfree_rcu_sheaf(ptr))
 		return;
 
 	// Queue the object but don't yet schedule the batch.
mm/slub.c +10 -7
index ba593c1c53d5..4850629774b2 100644
@@ -6082,12 +6082,13 @@ static void rcu_free_sheaf(struct rcu_head *head)
  * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since
  * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT),
  * this would violate lock nesting rules. Therefore, kvfree_call_rcu() avoids
- * this problem by bypassing the sheaves layer entirely on PREEMPT_RT.
+ * this problem by passing allow_spin = false on PREEMPT_RT.
  *
  * However, lockdep still complains that it is invalid to acquire spinlock_t
  * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a
  * spinning lock. Tell lockdep that acquiring spinlock_t is valid here
- * by temporarily raising the wait-type to LD_WAIT_CONFIG.
+ * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
+ * on PREEMPT_RT to avoid suppressing valid lockdep warnings.
  */
 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
 
@@ -6096,10 +6097,10 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	struct slub_percpu_sheaves *pcs;
 	struct slab_sheaf *rcu_sheaf;
 
-	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
-		return false;
+	VM_WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT) && allow_spin);
 
-	lock_map_acquire_try(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_acquire_try(&kfree_rcu_sheaf_map);
 
 	if (!local_trylock(&s->cpu_sheaves->lock))
 		goto fail;
@@ -6199,12 +6200,14 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, bool allow_spin)
 	local_unlock(&s->cpu_sheaves->lock);
 
 	stat(s, FREE_RCU_SHEAF);
-	lock_map_release(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_release(&kfree_rcu_sheaf_map);
 	return true;
 
 fail:
 	stat(s, FREE_RCU_SHEAF_FAIL);
-	lock_map_release(&kfree_rcu_sheaf_map);
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		lock_map_release(&kfree_rcu_sheaf_map);
 	return false;
 }
 
author maintainer
#33 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Quoted 17 lines · Vlastimil Babka (SUSE)
Quoted 9 lines · Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Quoted 6 lines

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

Quoted 5 lines

LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT? I thought (in the Link: above) we'd only need to downgrade allow_spin to false on PREEMPT_RT for handling sheaves movement from/to barn and alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on RT?

Indeed. Good point, thanks!

Hmm, but I'm not sure that it's worth the complexity given that PREEMPT_RT tries very hard to avoid disabling IRQs...

Quoted 1 line

Or is the irqs_disabled() condition rare enough so we don't care?

Given that most users don't call kfree_rcu() under raw spinlock or IRQs-disabled section on PREEMPT_RT, I think it's okay to keep it as is (it's not making things worse, at least) and wait for call_rcu_nolock()?

On a side note, I don't have much idea on what needs to call kfree_rcu() under a raw spinlock, other than set_cpus_allowed_force(), which should really be using kfree_nolock() instead of kfree_rcu() once we support kmalloc() -> kfree_nolock():

Quoted 6 lines
	/*
	 * Because this is called with p->pi_lock held, it is not possible
	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
	 * kfree_rcu().
	 */
	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);

Any thoughts, RCU/RT folks?

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#34 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Quoted 28 lines · Harry Yoo
Quoted 17 lines · Vlastimil Babka (SUSE)
Quoted 9 lines · Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Quoted 6 lines

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

Quoted 5 lines

LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT? I thought (in the Link: above) we'd only need to downgrade allow_spin to false on PREEMPT_RT for handling sheaves movement from/to barn and alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on RT?

Indeed. Good point, thanks!

Quoted 8 lines

Hmm, but I'm not sure that it's worth the complexity given that PREEMPT_RT tries very hard to avoid disabling IRQs...

Quoted 1 line

Or is the irqs_disabled() condition rare enough so we don't care?

Given that most users don't call kfree_rcu() under raw spinlock or IRQs-disabled section on PREEMPT_RT, I think it's okay to keep it as is (it's not making things worse, at least) and wait for call_rcu_nolock()?

Sounds good.

Quoted 4 lines

On a side note, I don't have much idea on what needs to call kfree_rcu() under a raw spinlock, other than set_cpus_allowed_force(), which should really be using kfree_nolock() instead of kfree_rcu() once we support kmalloc() -> kfree_nolock():

Looks like the case. Well if the fallback path of kfree_nolock() that is irq_work_queue() is indeed safe here.

Quoted 9 lines
Quoted 6 lines
	/*
	 * Because this is called with p->pi_lock held, it is not possible
	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
	 * kfree_rcu().
	 */
	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);

Any thoughts, RCU/RT folks?

maintainer
#35 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Quoted 40 lines · Vlastimil Babka (SUSE)
Quoted 28 lines · Harry Yoo
Quoted 17 lines · Vlastimil Babka (SUSE)
Quoted 9 lines · Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Quoted 6 lines

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

Quoted 5 lines

LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT? I thought (in the Link: above) we'd only need to downgrade allow_spin to false on PREEMPT_RT for handling sheaves movement from/to barn and alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on RT?

Indeed. Good point, thanks!

Quoted 8 lines

Hmm, but I'm not sure that it's worth the complexity given that PREEMPT_RT tries very hard to avoid disabling IRQs...

Quoted 1 line

Or is the irqs_disabled() condition rare enough so we don't care?

Given that most users don't call kfree_rcu() under raw spinlock or IRQs-disabled section on PREEMPT_RT, I think it's okay to keep it as is (it's not making things worse, at least) and wait for call_rcu_nolock()?

Sounds good.

Quoted 16 lines
Quoted 4 lines

On a side note, I don't have much idea on what needs to call kfree_rcu() under a raw spinlock, other than set_cpus_allowed_force(), which should really be using kfree_nolock() instead of kfree_rcu() once we support kmalloc() -> kfree_nolock():

Looks like the case. Well if the fallback path of kfree_nolock() that is irq_work_queue() is indeed safe here.

Quoted 8 lines
Quoted 6 lines
	/*
	 * Because this is called with p->pi_lock held, it is not possible
	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
	 * kfree_rcu().
	 */
	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);

Any thoughts, RCU/RT folks?

For the call_rcu*() counterparts, I am currently considering making the existing functions check for interrupts disabled, using irq_work_queue() in that case. I suppose that I could use raise_softirq() in the use_softirq=1 case when in_hardirq(). Either way, the check should be cheap compared to rest of the processing. The additional rcu_barrier() work required is of course way down in the noise compared to acquiring a global mutex.

In your case, kfree_rcu() can be quite a bit lighter weight, though. So the extra checks might not be lost in the noise.

Thoughts?

Thanx, Paul

author maintainer
#36 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Quoted 47 lines · Paul E. McKenney
Quoted 46 lines · Vlastimil Babka (SUSE)
Quoted 26 lines · Harry Yoo
Quoted 17 lines · Vlastimil Babka (SUSE)
Quoted 9 lines · Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Quoted 6 lines

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

Quoted 5 lines

LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT? I thought (in the Link: above) we'd only need to downgrade allow_spin to false on PREEMPT_RT for handling sheaves movement from/to barn and alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on RT?

Indeed. Good point, thanks!

Quoted 8 lines

Hmm, but I'm not sure that it's worth the complexity given that PREEMPT_RT tries very hard to avoid disabling IRQs...

Quoted 1 line

Or is the irqs_disabled() condition rare enough so we don't care?

Given that most users don't call kfree_rcu() under raw spinlock or IRQs-disabled section on PREEMPT_RT, I think it's okay to keep it as is (it's not making things worse, at least) and wait for call_rcu_nolock()?

Sounds good.

Quoted 4 lines

On a side note, I don't have much idea on what needs to call kfree_rcu() under a raw spinlock, other than set_cpus_allowed_force(), which should really be using kfree_nolock() instead of kfree_rcu() once we support kmalloc() -> kfree_nolock():

Looks like the case. Well if the fallback path of kfree_nolock() that is irq_work_queue() is indeed safe here.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
	/*
	 * Because this is called with p->pi_lock held, it is not possible
	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
	 * kfree_rcu().
	 */
	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);

Any thoughts, RCU/RT folks?

Thanks for looking into it, Paul!

Perhaps I'm missing some context here... let me clarify.

Quoted 3 lines

For the call_rcu*() counterparts, I am currently considering making the existing functions check for interrupts disabled, using irq_work_queue() in that case.

Ack.

Quoted 2 lines

I suppose that I could use raise_softirq() in the
use_softirq=1 case when in_hardirq().

Ack.

Quoted 2 lines

Either way, the check should be
cheap compared to rest of the processing.

Agreed.

Quoted 1 line

The additional rcu_barrier() work required is of course way down in

Assuming "The additional rcu_barrier() work required" means rcu_barrier() now needs to wait for all CPUs to complete irq_work or softirq...

Quoted 1 line

the noise compared to acquiring a global mutex.

Are you referring to a specific mutex, or just in general?

Quoted 2 lines

In your case, kfree_rcu() can be quite a bit lighter weight, though. So the extra checks might not be lost in the noise.

Assuming "the extra checks" means checking whether interrupts are disabled in call_rcu()*...

It will probably be fine since we invoke call_rcu() only when sheaves become full, not once for each object?

Perhaps slightly off-topic; at some point, though, I think it'd be better to teach SLUB to use RCU polling API for RCU sheaves.

e.g.) put sheaves into barn->sheaves_pending instead of invoking call_rcu(), and the SLUB alloc slowpath checks if there are sheaves past grace period before allocating and refilling sheaves.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer
#37 lore raw

Re: [PATCH for-next v3 6/9] mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT

Quoted 75 lines · Harry Yoo
Quoted 47 lines · Paul E. McKenney
Quoted 46 lines · Vlastimil Babka (SUSE)
Quoted 26 lines · Harry Yoo
Quoted 17 lines · Vlastimil Babka (SUSE)
Quoted 9 lines · Harry Yoo (Oracle)

As suggested by Vlastimil Babka, kfree_rcu_sheaf() can be used on PREEMPT_RT if we always assume spinning is not allowed on PREEMPT_RT. This is because local_trylock and spinlock_t are safe to use with trylock variant as long as the kernel does not spin and the context is not NMI and not hardirq.

Now that __kfree_rcu_sheaf() knows how to handle allow_spin = false, relax the limitation and try the sheaves path on PREEMPT_RT as well.

Quoted 6 lines

Keep the lockdep map on non RT kernels. However, do not use the lockdep map on PREEMPT_RT to avoid suppressing valid lockdep warnings.

Quoted 5 lines

LGTM, but maybe unnecessary pessimistic wrt call_rcu() on PREEMPT_RT? I thought (in the Link: above) we'd only need to downgrade allow_spin to false on PREEMPT_RT for handling sheaves movement from/to barn and alloc_empty_sheaf(), but call_rcu() would be safe from kfree_rcu() even on RT?

Indeed. Good point, thanks!

Quoted 8 lines

Hmm, but I'm not sure that it's worth the complexity given that PREEMPT_RT tries very hard to avoid disabling IRQs...

Quoted 1 line

Or is the irqs_disabled() condition rare enough so we don't care?

Given that most users don't call kfree_rcu() under raw spinlock or IRQs-disabled section on PREEMPT_RT, I think it's okay to keep it as is (it's not making things worse, at least) and wait for call_rcu_nolock()?

Sounds good.

Quoted 4 lines

On a side note, I don't have much idea on what needs to call kfree_rcu() under a raw spinlock, other than set_cpus_allowed_force(), which should really be using kfree_nolock() instead of kfree_rcu() once we support kmalloc() -> kfree_nolock():

Looks like the case. Well if the fallback path of kfree_nolock() that is irq_work_queue() is indeed safe here.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
	/*
	 * Because this is called with p->pi_lock held, it is not possible
	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
	 * kfree_rcu().
	 */
	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);

Any thoughts, RCU/RT folks?

Thanks for looking into it, Paul!

Perhaps I'm missing some context here... let me clarify.

Quoted 3 lines

For the call_rcu*() counterparts, I am currently considering making the existing functions check for interrupts disabled, using irq_work_queue() in that case.

Ack.

Quoted 2 lines

I suppose that I could use raise_softirq() in the
use_softirq=1 case when in_hardirq().

Ack.

Quoted 2 lines

Either way, the check should be
cheap compared to rest of the processing.

Quoted 7 lines

Agreed.

Quoted 1 line

The additional rcu_barrier() work required is of course way down in

Assuming "The additional rcu_barrier() work required" means rcu_barrier() now needs to wait for all CPUs to complete irq_work or softirq...

What I would instead have rcu_barrier() do is to: (1) pull in the pending callbacks awaiting irq-work, (2) wait for an RCU grace period (and thus for any pending irq-work handlers), (3) invoke the underlying call_rcu() machinery on these callbacks (just as the irq-work handler would, and also the rcu_core() function if the raise_softirq() optimization is implemented), and (4) proceed as it currently does, which is the for_each_possible_cpu(cpu) just before the retry: label.

As in item #1 above would start just after this block of code:

	init_completion(&rcu_state.barrier_completion);
	atomic_set(&rcu_state.barrier_cpu_count, 2);
	raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
Quoted 3 lines
Quoted 1 line

the noise compared to acquiring a global mutex.

Are you referring to a specific mutex, or just in general?

A specific mutex, namely the one that rcu_barrier() already acquires:

	mutex_lock(&rcu_state.barrier_mutex);
Quoted 5 lines
Quoted 2 lines

In your case, kfree_rcu() can be quite a bit lighter weight, though. So the extra checks might not be lost in the noise.

Assuming "the extra checks" means checking whether interrupts are disabled in call_rcu()*...

Yes, exactly.

Quoted 2 lines

It will probably be fine since we invoke call_rcu() only when sheaves become full, not once for each object?

For the purposes only of my verbiage starting with "In your case, kfree_rcu()", I was thinking in terms of these extra checks potentially also being in kfree_rcu().

Quoted 6 lines

Perhaps slightly off-topic; at some point, though, I think it'd be better to teach SLUB to use RCU polling API for RCU sheaves.

e.g.) put sheaves into barn->sheaves_pending instead of invoking call_rcu(), and the SLUB alloc slowpath checks if there are sheaves past grace period before allocating and refilling sheaves.

That makes a great deal of sense to me!

In fact this one of the types of use case that I was thinking of back when I was implementing the RCU polling API. ;-)

Thanx, Paul

author maintainer
#38 lore raw

PATCH v3 7/9 mm/slab: introduce kfree_rcu_nolock()

In reply to Harry Yoo (Oracle)

Currently, k[v]free_rcu() cannot be called in unknown context since it could lead to a deadlock when called in the middle of k[v]free_rcu().

Make users' lives easier by introducing kfree_rcu_nolock() variant, now that kfree_rcu_sheaf() is available on PREEMPT_RT and __kfree_rcu_sheaf() handles unknown context.

Unlike k[v]free_rcu(), kfree_rcu_nolock() does not fall back to the kvfree_rcu batching when the sheaves path fails, and falls back to defer_kfree_rcu() instead. In most cases, the sheaves path is expected to succeed and it's unnecessary to add complexity to the existing kvfree_rcu batching.

Since defer_kfree_rcu() can be called on caches without sheaves, move deferred_work_barrier() and rcu_barrier() outside the branch in kvfree_rcu_barrier_on_cache().

---
 include/linux/rcupdate.h | 12 ++++++++++++
 mm/slab.h                |  1 +
 mm/slab_common.c         | 22 ++++++++++++++++++++--
 mm/slub.c                | 23 ++++++++++++++++++++++-
 4 files changed, 55 insertions(+), 3 deletions(-)
include/linux/rcupdate.h +12 -0
index 5e95acc33989..3025249bfcb5 100644
@@ -1099,6 +1099,7 @@ static inline void rcu_read_unlock_migrate(void)
  * In mm/slab_common.c, no suitable header to include here.
  */
 void kvfree_call_rcu(struct rcu_head *head, void *ptr);
+void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr);
 
 /*
  * The BUILD_BUG_ON() makes sure the rcu_head offset can be handled. See the
@@ -1122,6 +1123,17 @@ do {								\
 		kvfree_call_rcu(NULL, (void *) (___p));		\
 } while (0)
 
+/* kfree_rcu_nolock() supports 2-arg variant only */
+#define kfree_rcu_nolock(ptr, krhf)					\
+do {									\
+	typeof (ptr) ___p = (ptr);					\
+									\
+	if (___p) {							\
+		BUILD_BUG_ON(offsetof(typeof(*(ptr)), krhf) >= 4096);	\
+		kfree_call_rcu_nolock(&((___p)->krhf), (void *) (___p));\
+	}								\
+} while (0)
+
 /*
  * Place this after a lock-acquisition primitive to guarantee that
  * an UNLOCK+LOCK pair acts as a full barrier.  This guarantee applies
mm/slab.h +1 -0
index 961581e35ec8..a493c5201e96 100644
@@ -745,6 +745,7 @@ void __check_heap_object(const void *ptr, unsigned long n,
 			 const struct slab *slab, bool to_user);
 
 void deferred_work_barrier(void);
+void defer_kfree_rcu(struct rcu_head *head);
 
 static inline bool slub_debug_orig_size(struct kmem_cache *s)
 {
mm/slab_common.c +20 -2
index 807924a94fb0..5a39e6225160 100644
@@ -1263,6 +1263,23 @@ EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
 EXPORT_TRACEPOINT_SYMBOL(kfree);
 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
 
+void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr)
+{
+	struct slab *slab;
+	struct kmem_cache *s;
+
+	VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr));
+
+	slab = virt_to_slab(ptr);
+	s = slab->slab_cache;
+
+	if (__kfree_rcu_sheaf(s, ptr, /* allow_spin = */ false))
+		return;
+
+	defer_kfree_rcu(head);
+}
+EXPORT_SYMBOL_GPL(kfree_call_rcu_nolock);
+
 #ifndef CONFIG_KVFREE_RCU_BATCHED
 
 void kvfree_call_rcu(struct rcu_head *head, void *ptr)
@@ -2120,10 +2137,11 @@ void kvfree_rcu_barrier_on_cache(struct kmem_cache *s)
 		cpus_read_lock();
 		flush_rcu_sheaves_on_cache(s);
 		cpus_read_unlock();
-		deferred_work_barrier();
-		rcu_barrier();
 	}
 
+	/* kfree_rcu_nolock() might have deferred frees even without sheaves */
+	deferred_work_barrier();
+	rcu_barrier();
 	__kvfree_rcu_barrier();
 }
 EXPORT_SYMBOL_GPL(kvfree_rcu_barrier_on_cache);
mm/slub.c +22 -1
index 4850629774b2..19018a979445 100644
@@ -4075,6 +4075,7 @@ static void flush_all(struct kmem_cache *s)
 
 struct deferred_percpu_work {
 	struct llist_head objects;
+	struct llist_head objects_by_rcu;
 	struct llist_head rcu_sheaves;
 	struct irq_work work;
 };
@@ -4083,6 +4084,7 @@ static void deferred_percpu_work_fn(struct irq_work *work);
 
 static DEFINE_PER_CPU(struct deferred_percpu_work, deferred_percpu_work) = {
 	.objects = LLIST_HEAD_INIT(objects),
+	.objects_by_rcu = LLIST_HEAD_INIT(objects_by_rcu),
 	.rcu_sheaves = LLIST_HEAD_INIT(rcu_sheaves),
 	.work = IRQ_WORK_INIT(deferred_percpu_work_fn),
 };
@@ -6392,12 +6394,13 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
 static void deferred_percpu_work_fn(struct irq_work *work)
 {
 	struct deferred_percpu_work *dpw;
-	struct llist_head *objs, *rcu_sheaves;
+	struct llist_head *objs, *objs_by_rcu, *rcu_sheaves;
 	struct llist_node *llnode, *pos, *t;
 
 	dpw = container_of(work, struct deferred_percpu_work, work);
 	rcu_sheaves = &dpw->rcu_sheaves;
 	objs = &dpw->objects;
+	objs_by_rcu = &dpw->objects_by_rcu;
 
 	llnode = llist_del_all(objs);
 	llist_for_each_safe(pos, t, llnode) {
@@ -6428,6 +6431,13 @@ static void deferred_percpu_work_fn(struct irq_work *work)
 
 		call_rcu(&rcu_sheaf->rcu_head, rcu_free_sheaf);
 	}
+
+	llnode = llist_del_all(objs_by_rcu);
+	llist_for_each_safe(pos, t, llnode) {
+		struct rcu_head *head = (struct rcu_head *)pos;
+
+		call_rcu(head, kvfree_rcu_cb);
+	}
 }
 
 static void defer_free(struct kmem_cache *s, void *head)
@@ -6443,6 +6453,17 @@ static void defer_free(struct kmem_cache *s, void *head)
 		irq_work_queue(&dpw->work);
 }
 
+void defer_kfree_rcu(struct rcu_head *head)
+{
+	struct deferred_percpu_work *dpw;
+
+	guard(preempt)();
+
+	dpw = this_cpu_ptr(&deferred_percpu_work);
+	if (llist_add((struct llist_node *)head, &dpw->objects_by_rcu))
+		irq_work_queue(&dpw->work);
+}
+
 void deferred_work_barrier(void)
 {
 	int cpu;
Signature · 1 line
2.53.0
#39 lore raw

Re: [PATCH for-next v3 7/9] mm/slab: introduce kfree_rcu_nolock()

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] kfree_call_rcu_nolock() crashes when freeing large kmalloc objects or vmalloc addresses due to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] kfree_call_rcu_nolock() crashes when freeing large kmalloc objects or vmalloc addresses due to an unconditional dereference of slab->slab_cache.
- [High] Calling __kfree_rcu_sheaf() on PREEMPT_RT triggers an 'Invalid wait context' lockdep warning because it attempts a local_trylock() under a raw_spinlock_t. --

commit 8bccb182210df2707b079473bfa04aa463cafa86
Author: Harry Yoo (Oracle) <[email protected]>

mm/slab: introduce kfree_rcu_nolock()

This commit introduces kfree_rcu_nolock(), a variant of kfree_rcu() designed to be safely called from unknown contexts without falling back to batched processing. It primarily tries to use the sheaf path and falls back to defer_kfree_rcu() if that fails.

Quoted 7 lines
mm/slab_common.c +0 -0
index 807924a94fb08..5a39e62251609 100644
@@ -1263,6 +1263,23 @@ EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
 EXPORT_TRACEPOINT_SYMBOL(kfree);
 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);

mm/slab_common.c EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); · line 1274

Quoted 10 lines
mm/slab_common.c +9 -0
 
+void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr)
+{
+	struct slab *slab;
+	struct kmem_cache *s;
+
+	VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr));
+
+	slab = virt_to_slab(ptr);
+	s = slab->slab_cache;

[Severity: Critical]
Does this crash when freeing large kmalloc objects or vmalloc addresses?

If ptr is a large kmalloc object (which bypasses SLUB) or a vmalloc address, virt_to_slab(ptr) returns NULL. The code issues a VM_WARN_ON_ONCE but proceeds to blindly dereference slab->slab_cache, causing a deterministic NULL pointer dereference.

Standard kfree_rcu() and kvfree_rcu() support these types of addresses, so callers might pass them here expecting them to be handled safely. Should we return early or handle the fallback path directly if slab is NULL?

mm/slab_common.c EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); · line 1277

Quoted 3 lines
mm/slab_common.c +3 -0
+
+	if (__kfree_rcu_sheaf(s, ptr, /* allow_spin = */ false))
+		return;

[Severity: High]
Can this trigger an 'Invalid wait context' lockdep warning on PREEMPT_RT?

The purpose of kfree_call_rcu_nolock() is to handle unknown contexts, including hardirq or raw spinlock contexts. However, this unconditionally calls __kfree_rcu_sheaf(), which acquires a lock via local_trylock(&s->cpu_sheaves->lock).

On PREEMPT_RT, local_lock maps to a sleepable spinlock_t (RT mutex). Acquiring a sleepable lock inside a raw spinlock context causes a lockdep splat because CONFIG_PROVE_RAW_LOCK_NESTING checks declared wait-types even for trylocks.

Quoted 4 lines
mm/slab_common.c +4 -0
+
+	defer_kfree_rcu(head);
+}
+EXPORT_SYMBOL_GPL(kfree_call_rcu_nolock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer Reviewed-by on v3 7/9
#40 lore raw

Re: [PATCH for-next v3 7/9] mm/slab: introduce kfree_rcu_nolock()

In reply to Harry Yoo (Oracle)
Quoted 13 lines · Harry Yoo (Oracle)

Currently, k[v]free_rcu() cannot be called in unknown context since it could lead to a deadlock when called in the middle of k[v]free_rcu().

Make users' lives easier by introducing kfree_rcu_nolock() variant, now that kfree_rcu_sheaf() is available on PREEMPT_RT and __kfree_rcu_sheaf() handles unknown context.

Unlike k[v]free_rcu(), kfree_rcu_nolock() does not fall back to the kvfree_rcu batching when the sheaves path fails, and falls back to defer_kfree_rcu() instead. In most cases, the sheaves path is expected to succeed and it's unnecessary to add complexity to the existing kvfree_rcu batching.

Quoted 5 lines

Since defer_kfree_rcu() can be called on caches without sheaves, move deferred_work_barrier() and rcu_barrier() outside the branch in kvfree_rcu_barrier_on_cache().

LGTM, nice.

#41 lore raw

Re: [PATCH for-next v3 7/9] mm/slab: introduce kfree_rcu_nolock()

In reply to Harry Yoo (Oracle)

Hi,

I noticed the Sashiko AI review [1] in this thread flagged that kfree_call_rcu_nolock() dereferences slab->slab_cache even when virt_to_slab() returns NULL (for large kmalloc objects that bypass SLUB, or vmalloc addresses).  The VM_WARN_ON_ONCE fires but does not stop execution, and the subsequent NULL dereference is deterministic.

I was able to reproduce this in QEMU with KASAN.  The trigger is as simple as passing a large (>8KB) kmalloc buffer to the new function.

Quoted 3 lines · Harry Yoo (Oracle)

This commit introduces kfree_rcu_nolock(), a variant of kfree_rcu() designed to be safely called from unknown contexts without falling back to batched processing.

...

mm/slab_common.c EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); · line 1274

Quoted 9 lines
mm/slab_common.c +9 -0
+void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr)
+{
+    struct slab *slab;
+    struct kmem_cache *s;
+
+    VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr));
+
+    slab = virt_to_slab(ptr);
+    s = slab->slab_cache;

The problem: if ptr is a large kmalloc object (> KMALLOC_MAX_CACHE_SIZE, which is 8 KB on x86_64), the allocation bypasses SLUB and comes from the page allocator.  virt_to_slab() returns NULL.  VM_WARN_ON_ONCE prints a warning but does NOT return, and the next line dereferences NULL->slab_cache at offset 0x8.

[Reproduction]

I rebuilt the kernel with CONFIG_KASAN=y and added a small late_initcall that allocates a 16 KB buffer and passes it to kfree_call_rcu_nolock():

  static int __init kfree_rcu_nolock_poc_trigger(void) {
      void *p = kmalloc(16384, GFP_KERNEL);
      struct rcu_head *head = kmalloc(sizeof(*head), GFP_KERNEL); kfree_call_rcu_nolock(head, p);
      return 0;
  }
  late_initcall(kfree_rcu_nolock_poc_trigger);

[Crash log — kernel 6.19.0-rc5, CONFIG_KASAN=y, CONFIG_DEBUG_VM=y]

  kfree_rcu_nolock PoC: calling kfree_call_rcu_nolock on large obj ffff888026c5c000

  WARNING: mm/slab_common.c:1271 at kfree_call_rcu_nolock+0x1e/0xc0

  VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr))

  BUG: kernel NULL pointer dereference, address: 0000000000000008

  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page

  RIP: 0010:kfree_call_rcu_nolock+0x5c/0xc0
  Call Trace:
   <TASK>
   poc_trigger_init+0x2a/0x40
   do_one_initcall+0x131/0x730
   kernel_init_freeable+0x471/0x7e0
   kernel_init+0x28/0x300
   ret_from_fork+0x2c/0xc0
   </TASK>

  Kernel panic - not syncing: Fatal exception

The crash is at offset 0x5c inside kfree_call_rcu_nolock(), which corresponds to s = slab->slab_cache.  The fault address 0x8 is exactly offsetof(struct slab, slab_cache).

[1] https://sashiko.dev/#/patchset/20260615-kfree_rcu_nolock-v3-0-70a54f3775bb%40kernel.org
    (Sashiko AI code review — "Null Pointer Dereference", Severity: Critical)

Thanks,
XIAO

author maintainer
#42 lore raw

Re: [PATCH for-next v3 7/9] mm/slab: introduce kfree_rcu_nolock()

Quoted 1 line · XIAO WU

Hi,

Hi Xiao,

Quoted 5 lines

I noticed the Sashiko AI review [1] in this thread flagged that kfree_call_rcu_nolock() dereferences slab->slab_cache even when virt_to_slab() returns NULL (for large kmalloc objects that bypass SLUB, or vmalloc addresses). The VM_WARN_ON_ONCE fires but does not stop execution, and the subsequent NULL dereference is deterministic.

Thanks for taking a look, but this was intentional.

I should have documented that only kmalloc_nolock() -> kfree_rcu_nolock() is allowed and kmalloc() -> kfree_rcu_nolock() is not allowed (yet).

Quoted 8 lines

I was able to reproduce this in QEMU with KASAN. The trigger is as simple as passing a large (>8KB) kmalloc buffer to the new function.

Quoted 3 lines · Harry Yoo (Oracle)

This commit introduces kfree_rcu_nolock(), a variant of kfree_rcu() designed to be safely called from unknown contexts without falling back to batched processing.

...

Quoted 15 lines

mm/slab_common.c EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); · line 1274

Quoted 9 lines
mm/slab_common.c +9 -0
+void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr)
+{
+    struct slab *slab;
+    struct kmem_cache *s;
+
+    VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr));
+
+    slab = virt_to_slab(ptr);
+    s = slab->slab_cache;

The problem: if ptr is a large kmalloc object (> KMALLOC_MAX_CACHE_SIZE, which is 8 KB on x86_64), the allocation bypasses SLUB and comes from the page allocator. virt_to_slab() returns NULL. VM_WARN_ON_ONCE prints a warning but does NOT return, and the next line dereferences NULL->slab_cache at offset 0x8.

Since kmalloc_nolock() does not support large kmalloc, the warning is not supposed to trigger. That is why I added only debug warnings.

Quoted 10 lines

[Reproduction]

I rebuilt the kernel with CONFIG_KASAN=y and added a small late_initcall that allocates a 16 KB buffer and passes it to kfree_call_rcu_nolock():

  static int __init kfree_rcu_nolock_poc_trigger(void)
  {
      void *p = kmalloc(16384, GFP_KERNEL);
      struct rcu_head *head = kmalloc(sizeof(*head), GFP_KERNEL);
      kfree_call_rcu_nolock(head, p);

As mentioned ealier, kmalloc() -> kfree_rcu_nolock() is not supported.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
Quoted 39 lines
      return 0;

}
late_initcall(kfree_rcu_nolock_poc_trigger);

[Crash log — kernel 6.19.0-rc5, CONFIG_KASAN=y, CONFIG_DEBUG_VM=y]

kfree_rcu_nolock PoC: calling kfree_call_rcu_nolock on large obj
ffff888026c5c000

  WARNING: mm/slab_common.c:1271 at kfree_call_rcu_nolock+0x1e/0xc0
  VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr))

  BUG: kernel NULL pointer dereference, address: 0000000000000008
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page

  RIP: 0010:kfree_call_rcu_nolock+0x5c/0xc0
  Call Trace:
   <TASK>
   poc_trigger_init+0x2a/0x40
   do_one_initcall+0x131/0x730
   kernel_init_freeable+0x471/0x7e0
   kernel_init+0x28/0x300
   ret_from_fork+0x2c/0xc0
   </TASK>

  Kernel panic - not syncing: Fatal exception

The crash is at offset 0x5c inside kfree_call_rcu_nolock(), which corresponds to s = slab->slab_cache. The fault address 0x8 is exactly offsetof(struct slab, slab_cache).

[1] https://sashiko.dev/#/patchset/20260615-kfree_rcu_nolock-
v3-0-70a54f3775bb%40kernel.org
(Sashiko AI code review — "Null Pointer Dereference", Severity:
Critical)

Thanks,
XIAO

#43 lore raw

Re: [PATCH for-next v3 7/9] mm/slab: introduce kfree_rcu_nolock()

Hi Harry,

Quoted 4 lines · Harry Yoo
Quoted 2 lines · XIAO WU

I was able to reproduce this in QEMU with KASAN. The trigger is as simple as passing a large (>8KB) kmalloc buffer to the new function.

Quoted 8 lines

Thanks for taking a look, but this was intentional.

I should have documented that only kmalloc_nolock() -> kfree_rcu_nolock() is allowed and kmalloc() -> kfree_rcu_nolock() is not allowed (yet).

Since kmalloc_nolock() does not support large kmalloc, the warning is not supposed to trigger. That is why I added only debug warnings.

Thank you very much for taking the time to explain — I really appreciate it, especially since I'm still learning my way around the mm/ subsystem. You are absolutely right that kmalloc_nolock() returns NULL for sizes above KMALLOC_MAX_CACHE_SIZE, so a proper caller using the kmalloc_nolock() → kfree_rcu_nolock() pairing would never hit this.

I did notice one small thing that I wanted to gently bring up, though. Please forgive me if I'm missing something obvious here.

When I was reading through the surrounding code to understand the pattern better, I noticed that kfree_nolock() — which has the same "only for kmalloc_nolock()" constraint (documented in the comment at mm/slub.c:6828-6835) — actually does check for a NULL slab:

  void kfree_nolock(const void *object)
  {
  	...
  	slab = virt_to_slab(object);
  	if (unlikely(!slab)) {
  		WARN_ONCE(1, "large_kmalloc is not supported by kfree_nolock()");
  		return;
  	}
  	s = slab->slab_cache;
  	...

So kfree_nolock() gracefully returns with a warning even though it too expects only kmalloc_nolock() callers. That pattern seemed really sensible to me — it costs almost nothing and prevents a panic if someone ever passes the wrong pointer (which they shouldn't, but as you mentioned, the constraint isn't documented on kfree_call_rcu_nolock() yet).

I also wondered about the difference between WARN_ONCE (used in kfree_nolock) and VM_WARN_ON_ONCE (used in kfree_call_rcu_nolock). If I understand correctly, VM_WARN_ON_ONCE compiles away entirely on production kernels without CONFIG_DEBUG_VM, which would make the subsequent NULL dereference completely silent — no warning, just a panic. That seems a bit scary for something that's exported to modules via EXPORT_SYMBOL_GPL.

And since you mentioned that kmalloc() → kfree_rcu_nolock() support is planned for the future (the "yet") — wouldn't this code path need the NULL check at that point anyway?

I was thinking something like this would make the function consistent with kfree_nolock() and also make it forward-compatible with the planned kmalloc() support:

mm/slab_common.c +8 -2
@@ -1266,10 +1266,16 @@ void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr)
 {
 	struct slab *slab;
 	struct kmem_cache *s;
 
-	VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr));
-
 	slab = virt_to_slab(ptr);
+	/*
+	 * kmalloc_nolock() never produces large-kmalloc or vmalloc
+	 * addresses, but be defensive: fall back to defer_kfree_rcu()
+	 * for unsupported pointer types, consistent with kfree_nolock().
+	 */
+	if (unlikely(!slab))
+		goto fallback;
+
 	s = slab->slab_cache;
 
 	if (__kfree_rcu_sheaf(s, ptr, /* allow_spin = */ false))

return;

+fallback:
defer_kfree_rcu(head);
}

Of course, this is just a suggestion — you know this code far better than I do. If you feel the current code is fine as-is with proper documentation, I completely understand and won't press the point further.

Either way, thank you again for the explanation, and for working on this series — having kfree_rcu_nolock() available for BPF and other contexts will be really valuable.

Thanks,
XIAO

author maintainer
#44 lore raw

Re: [PATCH for-next v3 7/9] mm/slab: introduce kfree_rcu_nolock()

Quoted 48 lines · XIAO WU

Hi Harry,

Quoted 4 lines · Harry Yoo
Quoted 2 lines · XIAO WU

I was able to reproduce this in QEMU with KASAN.  The trigger is as simple as passing a large (>8KB) kmalloc buffer to the new function.

Quoted 8 lines

Thanks for taking a look, but this was intentional.

I should have documented that only kmalloc_nolock() -> kfree_rcu_nolock() is allowed and kmalloc() -> kfree_rcu_nolock() is not allowed (yet).

Since kmalloc_nolock() does not support large kmalloc, the warning is not supposed to trigger. That is why I added only debug warnings.

Thank you very much for taking the time to explain — I really appreciate it, especially since I'm still learning my way around the mm/ subsystem.  You are absolutely right that kmalloc_nolock() returns NULL for sizes above KMALLOC_MAX_CACHE_SIZE, so a proper caller using the kmalloc_nolock() → kfree_rcu_nolock() pairing would never hit this.

I did notice one small thing that I wanted to gently bring up, though. Please forgive me if I'm missing something obvious here.

When I was reading through the surrounding code to understand the pattern better, I noticed that kfree_nolock() — which has the same "only for kmalloc_nolock()" constraint (documented in the comment at mm/slub.c:6828-6835) — actually does check for a NULL slab:

  void kfree_nolock(const void *object)
  {
      ...
      slab = virt_to_slab(object);
      if (unlikely(!slab)) {
          WARN_ONCE(1, "large_kmalloc is not supported by kfree_nolock()"); return;
      }
      s = slab->slab_cache;
      ...

So kfree_nolock() gracefully returns with a warning even though it too expects only kmalloc_nolock() callers.  That pattern seemed really sensible to me — it costs almost nothing and prevents a panic if someone ever passes the wrong pointer (which they shouldn't, but as you mentioned, the constraint isn't documented on kfree_call_rcu_nolock() yet).

Quoted 6 lines

I also wondered about the difference between WARN_ONCE (used in kfree_nolock) and VM_WARN_ON_ONCE (used in kfree_call_rcu_nolock). If I understand correctly, VM_WARN_ON_ONCE compiles away entirely on production kernels without CONFIG_DEBUG_VM, which would make the subsequent NULL dereference completely silent — no warning, just a panic.

It would crash without debug option anyway, but warnings are there to make it easier to point what's gone wrong.

And not testing the code path at least once with debug option is a big problem :)

Quoted 16 lines

And since you mentioned that kmalloc() → kfree_rcu_nolock() support is planned for the future (the "yet") — wouldn't this code path need the NULL check at that point anyway?

I was thinking something like this would make the function consistent with kfree_nolock() and also make it forward-compatible with the planned kmalloc() support:

mm/slab_common.c +0 -0
@@ -1266,10 +1266,16 @@ void kfree_call_rcu_nolock(struct rcu_head
 *head, void *ptr)
  {
      struct slab *slab;
      struct kmem_cache *s;
 

mm/slab_common.c void kfree_call_rcu_nolock(struct rcu_head · line 1278

Quoted 10 lines
mm/slab_common.c +7 -2
-    VM_WARN_ON_ONCE(is_vmalloc_addr(ptr) || !virt_to_slab(ptr));
-
      slab = virt_to_slab(ptr);
+    /*
+     * kmalloc_nolock() never produces large-kmalloc or vmalloc
+     * addresses, but be defensive: fall back to defer_kfree_rcu()
+     * for unsupported pointer types, consistent with kfree_nolock().
+     */
+    if (unlikely(!slab))
+        goto fallback;

Just FYI, virt_to_slab() and virt_to_page()
don't work correctly for vmalloc addresses.

And I don't think silently making it work is good.

Quoted 21 lines
mm/slab_common.c +1 -0
+
      s = slab->slab_cache;
 

     if (__kfree_rcu_sheaf(s, ptr, /* allow_spin = */ false)) return;

+fallback:
     defer_kfree_rcu(head);
 }

Of course, this is just a suggestion — you know this code far better than I do.  If you feel the current code is fine as-is with proper documentation, I completely understand and won't press the point further.

Either way, thank you again for the explanation, and for working on this series — having kfree_rcu_nolock() available for BPF and other contexts will be really valuable.

Thanks,
XIAO

author maintainer
#45 lore raw

PATCH v3 8/9 mm/slab: introduce struct kfree_rcu_head and use in kfree_rcu_nolock()

In reply to Harry Yoo (Oracle)

struct rcu_head is overkill for kfree_rcu() because the callback function is always kfree() and thus there is no need for a function pointer.

It is enough to implement a linked list using a single pointer. Introduce a new struct called kfree_rcu_head (the name was suggested by Vlastimil Babka), which is similar to struct rcu_head but is only a single pointer. Use it only in kfree_rcu_nolock() for now.

It is intended that in most cases, kfree_rcu_nolock() goes through sheaves path and the field is not used at all. However, in the worst case where trylock fails or no empty sheaves are available, maintain a global list of objects that will later be freed after synchronize_rcu() in a workqueue. Since the fallback is intended to be the last resort, it focuses on minimizing complexity and memory usage.

To avoid crashing the kernel in a theoretical case where fallback path is taken before kmem_cache_init_late(), do not queue the work until the workqueue is actually allocated.

Factor out the logic that calculates object start address into object_start_address() from kvfree_rcu_cb() to avoid duplicating it.

---
 include/linux/rcupdate.h |  2 +-
 include/linux/types.h    |  4 +++
 mm/slab.h                |  2 +-
 mm/slab_common.c         |  2 +-
 mm/slub.c                | 92 ++++++++++++++++++++++++++++++++----------------
 5 files changed, 69 insertions(+), 33 deletions(-)
include/linux/rcupdate.h +1 -1
index 3025249bfcb5..83e66585001f 100644
@@ -1099,7 +1099,7 @@ static inline void rcu_read_unlock_migrate(void)
  * In mm/slab_common.c, no suitable header to include here.
  */
 void kvfree_call_rcu(struct rcu_head *head, void *ptr);
-void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr);
+void kfree_call_rcu_nolock(struct kfree_rcu_head *head, void *ptr);
 
 /*
  * The BUILD_BUG_ON() makes sure the rcu_head offset can be handled. See the
include/linux/types.h +4 -0
index 93166b0b0617..695f2a71411e 100644
@@ -255,6 +255,10 @@ struct callback_head {
 } __attribute__((aligned(sizeof(void *))));
 #define rcu_head callback_head
 
+struct kfree_rcu_head {
+	struct kfree_rcu_head *next;
+};
+
 typedef void (*rcu_callback_t)(struct rcu_head *head);
 typedef void (*call_rcu_func_t)(struct rcu_head *head, rcu_callback_t func);
 
mm/slab.h +1 -1
index a493c5201e96..19a2a819fd13 100644
@@ -745,7 +745,7 @@ void __check_heap_object(const void *ptr, unsigned long n,
 			 const struct slab *slab, bool to_user);
 
 void deferred_work_barrier(void);
-void defer_kfree_rcu(struct rcu_head *head);
+void defer_kfree_rcu(struct kfree_rcu_head *head);
 
 static inline bool slub_debug_orig_size(struct kmem_cache *s)
 {
mm/slab_common.c +1 -1
index 5a39e6225160..6b80b516c93f 100644
@@ -1263,7 +1263,7 @@ EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
 EXPORT_TRACEPOINT_SYMBOL(kfree);
 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
 
-void kfree_call_rcu_nolock(struct rcu_head *head, void *ptr)
+void kfree_call_rcu_nolock(struct kfree_rcu_head *head, void *ptr)
 {
 	struct slab *slab;
 	struct kmem_cache *s;
mm/slub.c +62 -30

161 lines not on this page · raw message

Signature · 1 line
2.53.0
#46 lore raw

Re: [PATCH for-next v3 8/9] mm/slab: introduce struct kfree_rcu_head and use in kfree_rcu_nolock()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unbounded iteration over deferred RCU objects in the workqueue lacks `cond_resched()`, risking…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unbounded iteration over deferred RCU objects in the workqueue lacks cond_resched(), risking soft lockups. --

commit 300f5714ccdf41bb1175c5f608a81c4624b37553
Author: Harry Yoo (Oracle) <[email protected]>

mm/slab: introduce struct kfree_rcu_head and use in kfree_rcu_nolock()

This commit introduces struct kfree_rcu_head to reduce memory overhead for kfree_rcu_nolock(). It adds a global fallback list and a deferred workqueue to handle cases where trylock fails or no empty sheaves are available.

Quoted 4 lines
mm/slub.c +0 -0
index 19018a979445e..853430c7fbe06 100644

[ ... ]

Quoted 1 line
mm/slub.c +0 -0
@@ -6431,12 +6435,50 @@ static void deferred_percpu_work_fn(struct irq_work *work)

[ ... ]

Quoted 9 lines
mm/slub.c +9 -0
+static void deferred_work_fn(struct work_struct *w)
+{
+	struct llist_node *llnode, *pos, *t;
+
+	llnode = llist_del_all(&deferred_free_by_rcu);
+	if (!llnode)
+		return;
+
+	synchronize_rcu();

mm/slub.c static void deferred_percpu_work_fn(struct irq_work *work) · line 6483

Quoted 10 lines
mm/slub.c +2 -3
 
-	llnode = llist_del_all(objs_by_rcu);
 	llist_for_each_safe(pos, t, llnode) {
-		struct rcu_head *head = (struct rcu_head *)pos;
+		void *obj = object_start_address(pos);
 
-		call_rcu(head, kvfree_rcu_cb);
+		kfree(obj);
 	}
 }

[Severity: Medium]
Could this loop cause soft lockups on kernels without preemption?

Since deferred_free_by_rcu is a global list aggregating objects from all CPUs when the fallback path is taken, it could potentially grow very large. Executing kfree() on an unbounded number of objects in a single continuous loop without yielding the CPU might trigger soft lockup warnings.

Would it be safer to add cond_resched() inside this loop to prevent hogging the CPU?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
maintainer Acked-by on v3 8/9
#47 lore raw

Re: [PATCH for-next v3 8/9] mm/slab: introduce struct kfree_rcu_head and use in kfree_rcu_nolock()

In reply to Harry Yoo (Oracle)
Quoted 20 lines · Harry Yoo (Oracle)

struct rcu_head is overkill for kfree_rcu() because the callback function is always kfree() and thus there is no need for a function pointer.

It is enough to implement a linked list using a single pointer. Introduce a new struct called kfree_rcu_head (the name was suggested by Vlastimil Babka), which is similar to struct rcu_head but is only a single pointer. Use it only in kfree_rcu_nolock() for now.

It is intended that in most cases, kfree_rcu_nolock() goes through sheaves path and the field is not used at all. However, in the worst case where trylock fails or no empty sheaves are available, maintain a global list of objects that will later be freed after synchronize_rcu() in a workqueue. Since the fallback is intended to be the last resort, it focuses on minimizing complexity and memory usage.

To avoid crashing the kernel in a theoretical case where fallback path is taken before kmem_cache_init_late(), do not queue the work until the workqueue is actually allocated.

Quoted 5 lines

Factor out the logic that calculates object start address into object_start_address() from kvfree_rcu_cb() to avoid duplicating it.

Looks reasonable, if it works fast enough in practice.

author maintainer
#48 lore raw

PATCH v3 9/9 slub_kunit: extend the test for kfree_rcu_nolock()

In reply to Harry Yoo (Oracle)

When slub_kunit is not built-in, call kfree_rcu() and kfree_rcu_nolock() to test kfree_rcu_nolock() in slub_kunit.

Rename the test case as the test covers more _nolock() APIs.

---
 lib/tests/slub_kunit.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)
lib/tests/slub_kunit.c +26 -12
index 01d808cb77fa..6ed4de88bf0b 100644
@@ -162,7 +162,10 @@ static void test_kmalloc_redzone_access(struct kunit *test)
 }
 
 struct test_kfree_rcu_struct {
-	struct rcu_head rcu;
+	union {
+		struct rcu_head rcu;
+		struct kfree_rcu_head krcu;
+	};
 };
 
 static void test_kfree_rcu(struct kunit *test)
@@ -294,10 +297,10 @@ static void test_krealloc_redzone_zeroing(struct kunit *test)
 }
 
 #if defined(CONFIG_PERF_EVENTS) || (defined(CONFIG_KPROBES) && defined(CONFIG_SMP))
-#define SLUB_KUNIT_TEST_KMALLOC_KFREE_NOLOCK
+#define SLUB_KUNIT_TEST_KMALLOC_NOLOCK_AND_FRIENDS
 #define NR_ITERATIONS 1000
 #define NR_OBJECTS 1000
-static void *objects[NR_OBJECTS];
+static struct test_kfree_rcu_struct *objects[NR_OBJECTS];
 
 struct test_nolock_context {
 	struct kunit *test;
@@ -336,19 +339,24 @@ static struct perf_event_attr sw_attr = {
 
 static void test_nolock(struct test_nolock_context *ctx)
 {
-	void *objp;
+	struct test_kfree_rcu_struct *objp;
 	gfp_t gfp;
+	bool can_use_kfree_rcu = !IS_BUILTIN(CONFIG_SLUB_KUNIT_TEST);
 
 	/* __GFP_ACCOUNT to test kmalloc_nolock() in alloc_slab_obj_exts() */
 	gfp = (ctx->callback_count % 2) ? 0 : __GFP_ACCOUNT;
-	objp = kmalloc_nolock(64, gfp, NUMA_NO_NODE);
+	objp = kmalloc_nolock(sizeof(*objp), gfp, NUMA_NO_NODE);
 
 	if (objp)
 		ctx->alloc_ok++;
 	else
 		ctx->alloc_fail++;
 
-	kfree_nolock(objp);
+	if (can_use_kfree_rcu && (ctx->callback_count % 2))
+		kfree_rcu_nolock(objp, krcu);
+	else
+		kfree_nolock(objp);
+
 	ctx->callback_count++;
 }
 
@@ -432,12 +440,13 @@ static bool register_slab_kprobes(struct test_nolock_context *ctx) { return fals
 static void unregister_slab_kprobes(struct test_nolock_context *ctx) { }
 #endif
 
-static void test_kmalloc_kfree_nolock(struct kunit *test)
+static void test_kmalloc_nolock_and_friends(struct kunit *test)
 {
 	int i, j;
 	struct test_nolock_context perf_ctx = { .test = test };
 	struct test_nolock_context kprobe_ctx = { .test = test };
 	bool alloc_fail = false;
+	bool can_use_kfree_rcu = !IS_BUILTIN(CONFIG_SLUB_KUNIT_TEST);
 	bool perf_events_enabled;
 	bool slab_kprobes_enabled;
 
@@ -455,7 +464,7 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
 		for (j = 0; j < NR_OBJECTS; j++) {
 			gfp_t gfp = (i % 2) ? GFP_KERNEL : GFP_KERNEL_ACCOUNT;
 
-			objects[j] = kmalloc(64, gfp);
+			objects[j] = kmalloc_obj(*objects[j], gfp);
 			if (!objects[j]) {
 				j--;
 				while (j >= 0)
@@ -464,8 +473,13 @@ static void test_kmalloc_kfree_nolock(struct kunit *test)
 				goto cleanup;
 			}
 		}
-		for (j = 0; j < NR_OBJECTS; j++)
-			kfree(objects[j]);
+
+		for (j = 0; j < NR_OBJECTS; j++) {
+			if (can_use_kfree_rcu && (i % 2))
+				kfree_rcu(objects[j], rcu);
+			else
+				kfree(objects[j]);
+		}
 	}
 
 cleanup:
@@ -504,8 +518,8 @@ static struct kunit_case test_cases[] = {
 	KUNIT_CASE(test_kfree_rcu_wq_destroy),
 	KUNIT_CASE(test_leak_destroy),
 	KUNIT_CASE(test_krealloc_redzone_zeroing),
-#ifdef SLUB_KUNIT_TEST_KMALLOC_KFREE_NOLOCK
-	KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock),
+#ifdef SLUB_KUNIT_TEST_KMALLOC_NOLOCK_AND_FRIENDS
+	KUNIT_CASE_SLOW(test_kmalloc_nolock_and_friends),
 #endif
 	{}
 };
Signature · 1 line
2.53.0
author maintainer
#50 lore raw
In reply to Harry Yoo (Oracle)

Now I'm investigating and fixing pre-existing two kmalloc_nolock() bugs and (hopefully) planning to post the fixes later this week.

I will later rebase the this series onto Vlastimil's slab_alloc_flags v3 and the kmalloc_nolock() fixes, and address human and sashiko's comments.

But this should be enough for review.

Thanks!

Quoted 173 lines · Harry Yoo (Oracle)

Not the best time to post a series, but didn't want to delay posting the series for too long. no pressures ;) This is aimed to be queued for review and testing after the merge window closes.

This series is based on next-20260612, and is also available on git.kernel.org [3].

To RCU folks: It would be great if you could kindly take a quick look at patch 4 and either ack or nack the patch ;)

To BPF folks: Ulad asked to share workloads to measure performance of kfree_rcu_nolock(). Unfortunately, I focused more on correctness and have not spent much effort on that. It would be nice if BPF folks could help evaluate it on their relevant workloads.

To PREEMPT_RT folks: The most relevant part is allowing kfree_rcu_sheaf() on PREEMPT_RT (patch 6). It carefully avoids sleeping by acquiring the locks via local_trylock() or spin_trylock_irqsave() to avoid sleeping within a raw spinlock. When trylock or unlock is unsafe, kmalloc_nolock() always fails.

Changes since RFC v2
====================

Reduced complexity and intrusiveness (Uladzislau Rezki)
-------------------------------------------------------

While discussing concerns about the complexity of adding allow_spin handling with Ulad (Thanks!), I realized that adding complexity to the kvfree_rcu batching is not strictly necessary: only slab objects need to be batched, they are already batched by rcu sheaves, and slab already supports unknown context. So it is enough to implement only a minimal fallback for the sheaves path.

I tried to avoid making intrusive changes to the existing kvfree_rcu path as much as possible. struct rcu_ptr is renamed to kfree_rcu_head following Vlastimil's suggestion, and it is used only in the kfree_rcu_nolock() path for now.

As a result, the complexity is significantly reduced and the series became much less intrusive. This is also reflected well in the diffstat below.

RFC v2 diffstat:

  8 files changed, 514 insertions(+), 163 deletions(-)

v3 diffstat:

  6 files changed, 370 insertions(+), 105 deletions(-)

v3 diffstat (slub_kunit improvements - patch 1, 2, 9 excluded):

  5 files changed, 199 insertions(+), 66 deletions(-)

kfree_rcu_sheaf() PREEMPT_RT support (Vlastimil Babka)
------------------------------------------------------

As suggested by Vlastimil (Thanks!), kfree_rcu_sheaf() can now be used on PREEMPT_RT as well, by always assuming allow_spin is false on PREEMPT_RT.

slub_kunit enhancements
-----------------------

- Currently the test is skipped when there is no hardware PMU. This can happen on machines without a PMU, or in virtualized environments (e.g., automated testing or virtme). Implement a fallback based on SW perf events so that the test can still run in such environments, even though the coverage is slightly smaller.

- While testing on PREEMPT_RT, I found that kmalloc_nolock() fails every time, so the fallback path is not properly tested. This is a limitation of perf events: the handler is called in NMI (HW perf events) or interrupt context (SW perf events), where kmalloc_nolock() cannot succeed.

slub_kunit now registers a kprobe pre-handler at the points in the slab allocator where lockdep_assert_held() is invoked. The pre-handler calls kmalloc_nolock() and friends, to improve coverage on PREEMPT_RT instead of relying on perf events.

One thing that needs to be further explored
-------------------------------------------

The global deferred_free_by_rcu (introduced by patch 8) list for the fallback should probably be per-CPU [5].

Actual Cover Letter
===================

This series improves kmalloc_nolock() and kfree_nolock() coverage in slub_kunit (patch 1 and 2) and introduces kfree_rcu_nolock() for an unknown context as suggested by Alexei Starovoitov.

Unknown context means the caller does not know whether spinning on a lock is safe (e.g., a BPF program attached to an arbitrary kernel function or in NMI context).

The slab allocator already supports unknown context via kmalloc_nolock() and kfree_nolock(), but te slab allocator does not support freeing objects by RCU in unknown context.

It is not ideal to have completely separate batching for unknown context because the worst scenario where spinning on a lock would lead to deadlock is very rare, and in most cases, it is safe to use the existing mechanism (kfree_rcu_sheaf()).

Since most part of the slab allocator already supports unknown context and sheaves support batching kvfree_rcu() calls for slab objects, implement kfree_rcu_nolock() with minimal changes by teaching kfree_rcu_sheaf() how to support unknown context and making it a little bit harder to allocate an empty sheaf, instead of making intrusive changes to the existing kvfree_rcu batching logic.

kfree_rcu_nolock() tries to free the object to the rcu sheaf if trylock succeeds. Once the rcu sheaf becomes full, it is submitted to RCU via call_rcu() if spinning is allowed or IRQs are enabled (to avoid calling call_rcu() in the middle of call_rcu()). Otherwise, call_rcu() is deferred via irq work.

In unknown context, when there is no sheaf available, kfree_rcu_sheaf() falls back to defer_kfree_rcu(), which inserts the object to a global lockless list [5] and those objects are freed after synchronize_rcu() in a workqueue.

Unlike kfree_rcu(), only the 2-argument variant is supported. This is because the last resort of the 1-arg variant is synchronize_rcu(), which cannot be used in an unknown context.

As suggested by Alexei Starovoitov, kfree_rcu_nolock() can be used with struct kfree_rcu_head (8 bytes), which is smaller than struct rcu_head (16 bytes).

For more background and future plans, please see [4].

[1] RFC v1: https://lore.kernel.org/linux-mm/[email protected]

[2] RFC v2: https://lore.kernel.org/linux-mm/[email protected]

[3] https://git.kernel.org/pub/scm/linux/kernel/git/harry/linux.git/log/?h=kfree_rcu_nolock-v3r3

[4] kmalloc_nolock() follow-ups, including kfree_rcu_nolock(),
https://lore.kernel.org/linux-mm/esepccfhqg7m6jo76ns2znj2cnuaepx2xvw5zaygtwohq4psma@563ypprp6rr3

[5] However, we should probably make the list percpu because, unlike RFC v2, it can be triggered more frequently under memory pressure.

https://lore.kernel.org/linux-mm/805c33d7-3a7b-470c-bd9d-065717a3e3e2@paulmck-laptop

---
Harry Yoo (Oracle) (9):
slub_kunit: fall back to SW perf events when HW PMU is not available mm/slab, slub_kunit: register kprobe to trigger _nolock APIs mm/slab: handle the !allow_spin case in kfree_rcu_sheaf() mm/slab: use call_rcu() in unknown context if irqs are enabled mm/slab: extend deferred free mechanism to handle rcu sheaves mm/slab: allow kfree_rcu_sheaf() on PREEMPT_RT
mm/slab: introduce kfree_rcu_nolock()
mm/slab: introduce struct kfree_rcu_head and use in kfree_rcu_nolock()
slub_kunit: extend the test for kfree_rcu_nolock()

 include/linux/rcupdate.h |  12 +++
 include/linux/types.h    |   4 +
 lib/tests/slub_kunit.c   | 174 ++++++++++++++++++++++++++++------
 mm/slab.h                |   5 +-
 mm/slab_common.c         |  38 ++++++--
 mm/slub.c                | 242 ++++++++++++++++++++++++++++++++++-------------
 6 files changed, 370 insertions(+), 105 deletions(-)

---
base-commit: c425609d6ac4012c8bbf01ec2e10e801b1923a7b
change-id: 20260615-kfree_rcu_nolock-e5502555992f

Best regards,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#51 lore raw
In reply to Harry Yoo (Oracle)
Quoted 7 lines · Harry Yoo (Oracle)

Not the best time to post a series, but didn't want to delay posting the series for too long. no pressures ;) This is aimed to be queued for review and testing after the merge window closes.

This series is based on next-20260612, and is also available on git.kernel.org [3].

Quoted 7 lines

To RCU folks: It would be great if you could kindly take a quick look at patch 4 and either ack or nack the patch ;)

To BPF folks: Ulad asked to share workloads to measure performance of kfree_rcu_nolock(). Unfortunately, I focused more on correctness and have not spent much effort on that. It would be nice if BPF folks could help evaluate it on their relevant workloads.

kfree_rcu_nolock() needs to replace bpf_mem_alloc which is backbone of bpf maps and bpf local storage.
So all of the selftests/bpf/benchs/run_bench_*.sh
will exercise it one way or the other the replacement is complete. In other words performance is absolutely critical.

Quoted 20 lines

To PREEMPT_RT folks: The most relevant part is allowing kfree_rcu_sheaf() on PREEMPT_RT (patch 6). It carefully avoids sleeping by acquiring the locks via local_trylock() or spin_trylock_irqsave() to avoid sleeping within a raw spinlock. When trylock or unlock is unsafe, kmalloc_nolock() always fails.

Changes since RFC v2
====================

Reduced complexity and intrusiveness (Uladzislau Rezki)
-------------------------------------------------------

While discussing concerns about the complexity of adding allow_spin handling with Ulad (Thanks!), I realized that adding complexity to the kvfree_rcu batching is not strictly necessary: only slab objects need to be batched, they are already batched by rcu sheaves, and slab already supports unknown context. So it is enough to implement only a minimal fallback for the sheaves path.

Quoted 8 lines

I tried to avoid making intrusive changes to the existing kvfree_rcu path as much as possible. struct rcu_ptr is renamed to kfree_rcu_head following Vlastimil's suggestion, and it is used only in the kfree_rcu_nolock() path for now.

As a result, the complexity is significantly reduced and the series became much less intrusive. This is also reflected well in the diffstat below.

Overall looks good to me.
btw sashiko was confused in few cases.
Not everything that it flags needs a fix. Sometimes it's not an issue at all. It only sounds like one.

author maintainer
#52 lore raw
Quoted 16 lines · Alexei Starovoitov
Quoted 7 lines · Harry Yoo (Oracle)

Not the best time to post a series, but didn't want to delay posting the series for too long. no pressures ;) This is aimed to be queued for review and testing after the merge window closes.

This series is based on next-20260612, and is also available on git.kernel.org [3].

Quoted 7 lines

To RCU folks: It would be great if you could kindly take a quick look at patch 4 and either ack or nack the patch ;)

To BPF folks: Ulad asked to share workloads to measure performance of kfree_rcu_nolock(). Unfortunately, I focused more on correctness and have not spent much effort on that. It would be nice if BPF folks could help evaluate it on their relevant workloads.

Quoted 4 lines

kfree_rcu_nolock() needs to replace bpf_mem_alloc which is backbone of bpf maps and bpf local storage.

So all of the selftests/bpf/benchs/run_bench_*.sh

Didn't notice it was a thing, thanks!

Quoted 27 lines

will exercise it one way or the other the replacement is complete. In other words performance is absolutely critical.

Quoted 24 lines

To PREEMPT_RT folks: The most relevant part is allowing kfree_rcu_sheaf() on PREEMPT_RT (patch 6). It carefully avoids sleeping by acquiring the locks via local_trylock() or spin_trylock_irqsave() to avoid sleeping within a raw spinlock. When trylock or unlock is unsafe, kmalloc_nolock() always fails.

Changes since RFC v2
====================

Reduced complexity and intrusiveness (Uladzislau Rezki)
-------------------------------------------------------

While discussing concerns about the complexity of adding allow_spin handling with Ulad (Thanks!), I realized that adding complexity to the kvfree_rcu batching is not strictly necessary: only slab objects need to be batched, they are already batched by rcu sheaves, and slab already supports unknown context. So it is enough to implement only a minimal fallback for the sheaves path.

I tried to avoid making intrusive changes to the existing kvfree_rcu path as much as possible. struct rcu_ptr is renamed to kfree_rcu_head following Vlastimil's suggestion, and it is used only in the kfree_rcu_nolock() path for now.

Quoted 5 lines
Quoted 3 lines

As a result, the complexity is significantly reduced and the series became much less intrusive. This is also reflected well in the diffstat below.

Overall looks good to me.

Thanks!

Quoted 3 lines

btw sashiko was confused in few cases.
Not everything that it flags needs a fix. Sometimes it's not an issue at all. It only sounds like one.

Right, most of the comments are false positives. But there is one comment that looks like a real bug...

Quoted 7 lines · Sashiko

This is a pre-existing issue, but can concurrent lockless calls to deferred_work_barrier() cause an rcuwait race on PREEMPT_RT, leading to permanent task hangs?

The function iterates over all CPUs, invoking irq_work_sync() on each CPU's deferred work object. On PREEMPT_RT, irq_work_sync() relies on rcuwait_wait_event() to block until completion,

Yes. If CONFIG_PREEPMT_RT is enabled and irq_work doesn't have IRQ_WORK_HARD_IRQ, irq_work_sync() calls rcuwait_wait_event().

Quoted 2 lines

and the rcuwait
synchronization primitive strictly allows only one waiter at a time.

Hmm yeah, include/linux/rcuwait.h says:

| The caller is responsible for locking around rcuwait_wait_event(),
| and [prepare_to/finish]_rcuwait() such that writes to @task are
| properly serialized.
Quoted 3 lines

Because deferred_work_barrier() is called without any global serialization (for instance, in kmem_cache_destroy() and kvfree_rcu_barrier_on_cache(), and now in flush_all_rcu_sheaves()),

Yes, without slab_mutex held.

Quoted 6 lines

multiple threads can enter irq_work_sync() for the same work object concurrently.

This overwrites the waiter task pointer, meaning only one task will be woken up when the work completes, leaving the other tasks hanging permanently in an uninterruptible sleep.

I think this is indeed a pre-existing issue that needs to be resolved. Looks like we now have one more bug to fix ;-)

Signature · 2 lines
Cheers,
Harry / Hyeonggon