* [PATCH] lib/sbitmap: test that free() clears alloc_hint and ws
@ 2026-12-03 8:35 Wang Yibo
2026-12-03 8:43 ` sashiko-bot
2026-12-03 9:31 ` [PATCH v2] " Wang Yibo
0 siblings, 2 replies; 4+ messages in thread
From: Wang Yibo @ 2026-12-03 8:35 UTC (permalink / raw)
To: Jens Axboe
Cc: Andrew Morton, Bart Van Assche, Ming Lei, David Gow, Keith Teeple,
thanhnguyentuan2007, linux-block, linux-scsi, linux-kernel
Bart Van Assche's commit ddca9a368588 ("sbitmap: Clear alloc_hint and
ws pointers after freeing") made sbitmap_free() and sbitmap_queue_free()
clear the pointers they free, so that repeated teardown is safe. Andrew
Morton asked for the sbitmap KUnit suite to be updated to cover this.
Extend the suite to check that sbitmap_free() clears alloc_hint and that
sbitmap_queue_free() clears ws, including a second free of each object
to pin down the idempotency that the scsi error-cleanup path relies on.
Link: https://lore.kernel.org/r/20261202112938.a596225d374bdf002bd1ad81@linux-foundation.org
Signed-off-by: Wang Yibo <wangyibo@kylinos.cn>
---
lib/tests/sbitmap_kunit.c | 41 +++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/lib/tests/sbitmap_kunit.c b/lib/tests/sbitmap_kunit.c
index 284dcb5..935d143 100644
--- a/lib/tests/sbitmap_kunit.c
+++ b/lib/tests/sbitmap_kunit.c
@@ -45,6 +45,7 @@ static void sbitmap_test_init(struct kunit *test)
KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), 0U);
sbitmap_free(&sb);
+ KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
/* An explicitly requested shift is honoured. */
ret = sbitmap_init_node(&sb, depth, 3, GFP_KERNEL, NUMA_NO_NODE,
@@ -285,6 +286,44 @@ static void sbitmap_test_iterate_set(struct kunit *test)
sbitmap_free(&sb);
}
+static void sbitmap_test_free_clears_alloc_hint(struct kunit *test)
+{
+ struct sbitmap sb;
+
+ KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, BITS_PER_LONG, -1,
+ GFP_KERNEL, NUMA_NO_NODE,
+ false, true), 0);
+ KUNIT_EXPECT_TRUE(test, sb.alloc_hint);
+
+ sbitmap_free(&sb);
+
+ /*
+ * alloc_hint is a __percpu pointer, so test it as a boolean, as in
+ * sbitmap_test_init(). Clearing it makes repeated teardown safe,
+ * which is what the scsi error-cleanup path relies on.
+ */
+ KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
+ sbitmap_free(&sb);
+ KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
+}
+
+static void sbitmap_test_queue_free_clears_ws(struct kunit *test)
+{
+ struct sbitmap_queue sbq;
+
+ KUNIT_ASSERT_EQ(test, sbitmap_queue_init_node(&sbq, BITS_PER_LONG, -1,
+ false, GFP_KERNEL,
+ NUMA_NO_NODE), 0);
+ KUNIT_EXPECT_TRUE(test, sbq.ws);
+
+ sbitmap_queue_free(&sbq);
+
+ KUNIT_EXPECT_NULL(test, sbq.ws);
+ /* Repeated teardown must not pass a stale ws to kfree(). */
+ sbitmap_queue_free(&sbq);
+ KUNIT_EXPECT_NULL(test, sbq.ws);
+}
+
static struct kunit_case sbitmap_test_cases[] = {
KUNIT_CASE(sbitmap_test_init),
KUNIT_CASE(sbitmap_test_calculate_shift),
@@ -294,6 +333,8 @@ static struct kunit_case sbitmap_test_cases[] = {
KUNIT_CASE(sbitmap_test_resize),
KUNIT_CASE(sbitmap_test_set_clear_bit),
KUNIT_CASE(sbitmap_test_iterate_set),
+ KUNIT_CASE(sbitmap_test_free_clears_alloc_hint),
+ KUNIT_CASE(sbitmap_test_queue_free_clears_ws),
{}
};
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] lib/sbitmap: test that free() clears alloc_hint and ws
2026-12-03 8:35 [PATCH] lib/sbitmap: test that free() clears alloc_hint and ws Wang Yibo
@ 2026-12-03 8:43 ` sashiko-bot
2026-12-03 9:31 ` [PATCH v2] " Wang Yibo
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 8:43 UTC (permalink / raw)
To: Wang Yibo; +Cc: linux-scsi
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] lib/sbitmap: kunit tests fail and double-free without commit
ddca9a368588
- [Medium] lib/sbitmap: KUNIT_EXPECT before second free allows
double-free on failure
--
commit 5850549a9d340c961f2a0bf4176c791ec3354fa2
Author: Wang Yibo <wangyibo@kylinos.cn>
lib/sbitmap: test that free() clears alloc_hint and ws
This commit extends the sbitmap KUnit suite to check that sbitmap_free()
clears alloc_hint and sbitmap_queue_free() clears ws, including repeated
free calls on each object.
Link: https://lore.kernel.org/r/20261202112938.a596225d374bdf002bd1ad81@linux-foundation.org
> diff --git a/lib/tests/sbitmap_kunit.c b/lib/tests/sbitmap_kunit.c
> index 284dcb5d7188b..935d143316481 100644
> --- a/lib/tests/sbitmap_kunit.c
> +++ b/lib/tests/sbitmap_kunit.c
> @@ -45,6 +45,7 @@ static void sbitmap_test_init(struct kunit *test)
> KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
> KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), 0U);
> sbitmap_free(&sb);
> + KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
[Severity: High]
Does this commit depend on ddca9a368588 ("sbitmap: Clear alloc_hint and ws
pointers after freeing"), which is missing from this branch?
In this tree, sbitmap_free() leaves sb->alloc_hint non-NULL and
sbitmap_queue_free() leaves sbq->ws non-NULL. Running the sbitmap KUnit
suite here fails sbitmap_test_init(), sbitmap_test_free_clears_alloc_hint(),
and sbitmap_test_queue_free_clears_ws(), and the repeated teardown calls
double free sb.alloc_hint via free_percpu() and sbq.ws via kfree(),
triggering WARN_ON_ONCE(1) in free_percpu() or slab corruption.
This patch needs to be queued on top of ddca9a368588 ("sbitmap: Clear
alloc_hint and ws pointers after freeing") so that neither the branch head
nor git bisect runs these tests without the pointer-clearing fix.
> @@ -285,6 +286,44 @@ static void sbitmap_test_iterate_set(struct kunit *test)
> sbitmap_free(&sb);
> }
>
> +static void sbitmap_test_free_clears_alloc_hint(struct kunit *test)
> +{
> + struct sbitmap sb;
> +
> + KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, BITS_PER_LONG, -1,
> + GFP_KERNEL, NUMA_NO_NODE,
> + false, true), 0);
> + KUNIT_EXPECT_TRUE(test, sb.alloc_hint);
> +
> + sbitmap_free(&sb);
> +
> + /*
> + * alloc_hint is a __percpu pointer, so test it as a boolean, as in
> + * sbitmap_test_init(). Clearing it makes repeated teardown safe,
> + * which is what the scsi error-cleanup path relies on.
> + */
> + KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
> + sbitmap_free(&sb);
> + KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
> +}
> +
> +static void sbitmap_test_queue_free_clears_ws(struct kunit *test)
> +{
> + struct sbitmap_queue sbq;
> +
> + KUNIT_ASSERT_EQ(test, sbitmap_queue_init_node(&sbq, BITS_PER_LONG, -1,
> + false, GFP_KERNEL,
> + NUMA_NO_NODE), 0);
> + KUNIT_EXPECT_TRUE(test, sbq.ws);
> +
> + sbitmap_queue_free(&sbq);
> +
> + KUNIT_EXPECT_NULL(test, sbq.ws);
> + /* Repeated teardown must not pass a stale ws to kfree(). */
> + sbitmap_queue_free(&sbq);
> + KUNIT_EXPECT_NULL(test, sbq.ws);
> +}
[Severity: Medium]
Can KUNIT_EXPECT_FALSE() and KUNIT_EXPECT_NULL() allow a double free if the
tested invariant regresses?
Unlike KUNIT_ASSERT_*(), KUNIT_EXPECT_*() records a failure and continues
executing the test, and neither test checks sb.map (or sbq.sb.alloc_hint in
sbitmap_test_queue_free_clears_ws()) before calling the second free. If a
regression or incomplete backport leaves any of those pointers set, the
second sbitmap_free() or sbitmap_queue_free() passes a dangling pointer to
free_percpu(), kfree(), or kvfree() instead of aborting cleanly.
Guarding the repeated teardown with KUNIT_ASSERT_FALSE() on alloc_hint and
KUNIT_ASSERT_NULL() on map (and ws in sbitmap_test_queue_free_clears_ws())
would turn a failure into a clean KUnit abort.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203083549.257985-1-wangyibo@kylinos.cn?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] lib/sbitmap: test that free() clears alloc_hint and ws
2026-12-03 8:35 [PATCH] lib/sbitmap: test that free() clears alloc_hint and ws Wang Yibo
2026-12-03 8:43 ` sashiko-bot
@ 2026-12-03 9:31 ` Wang Yibo
2026-12-03 9:45 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Wang Yibo @ 2026-12-03 9:31 UTC (permalink / raw)
To: Jens Axboe
Cc: Andrew Morton, Bart Van Assche, Ming Lei, David Gow, Keith Teeple,
thanhnguyentuan2007, linux-block, linux-scsi, linux-kernel
Bart Van Assche's commit ddca9a368588 ("sbitmap: Clear alloc_hint and
ws pointers after freeing") made sbitmap_free() and sbitmap_queue_free()
clear the pointers they free, so that repeated teardown is safe. Andrew
Morton asked for the sbitmap KUnit suite to be updated to cover this.
Extend the suite to check that sbitmap_free() clears alloc_hint and that
sbitmap_queue_free() clears ws, including a second free of each object
to pin down the idempotency that the scsi error-cleanup path relies on.
This patch requires ddca9a368588 to be present in the tree. The
assertions before the second free make a missing dependency fail cleanly
rather than passing a stale pointer to free_percpu()/kfree().
Link: https://lore.kernel.org/r/20261202112938.a596225d374bdf002bd1ad81@linux-foundation.org
Signed-off-by: Wang Yibo <wangyibo@kylinos.cn>
---
lib/tests/sbitmap_kunit.c | 44 +++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/lib/tests/sbitmap_kunit.c b/lib/tests/sbitmap_kunit.c
index 284dcb5..98a544e 100644
--- a/lib/tests/sbitmap_kunit.c
+++ b/lib/tests/sbitmap_kunit.c
@@ -45,6 +45,7 @@ static void sbitmap_test_init(struct kunit *test)
KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), 0U);
sbitmap_free(&sb);
+ KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
/* An explicitly requested shift is honoured. */
ret = sbitmap_init_node(&sb, depth, 3, GFP_KERNEL, NUMA_NO_NODE,
@@ -285,6 +286,47 @@ static void sbitmap_test_iterate_set(struct kunit *test)
sbitmap_free(&sb);
}
+static void sbitmap_test_free_clears_alloc_hint(struct kunit *test)
+{
+ struct sbitmap sb;
+
+ KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, BITS_PER_LONG, -1,
+ GFP_KERNEL, NUMA_NO_NODE,
+ false, true), 0);
+ KUNIT_EXPECT_TRUE(test, sb.alloc_hint);
+
+ sbitmap_free(&sb);
+
+ /*
+ * alloc_hint is a __percpu pointer, so test it as a boolean, as in
+ * sbitmap_test_init(). Clearing it makes repeated teardown safe,
+ * which is what the scsi error-cleanup path relies on. Assert
+ * before freeing again: EXPECT would continue on failure and pass
+ * the stale alloc_hint to free_percpu() on a tree without
+ * ddca9a368588.
+ */
+ KUNIT_ASSERT_FALSE(test, sb.alloc_hint);
+ sbitmap_free(&sb);
+ KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
+}
+
+static void sbitmap_test_queue_free_clears_ws(struct kunit *test)
+{
+ struct sbitmap_queue sbq;
+
+ KUNIT_ASSERT_EQ(test, sbitmap_queue_init_node(&sbq, BITS_PER_LONG, -1,
+ false, GFP_KERNEL,
+ NUMA_NO_NODE), 0);
+ KUNIT_EXPECT_TRUE(test, sbq.ws);
+
+ sbitmap_queue_free(&sbq);
+
+ /* Assert before freeing again so a stale ws never reaches kfree(). */
+ KUNIT_ASSERT_NULL(test, sbq.ws);
+ sbitmap_queue_free(&sbq);
+ KUNIT_EXPECT_NULL(test, sbq.ws);
+}
+
static struct kunit_case sbitmap_test_cases[] = {
KUNIT_CASE(sbitmap_test_init),
KUNIT_CASE(sbitmap_test_calculate_shift),
@@ -294,6 +336,8 @@ static struct kunit_case sbitmap_test_cases[] = {
KUNIT_CASE(sbitmap_test_resize),
KUNIT_CASE(sbitmap_test_set_clear_bit),
KUNIT_CASE(sbitmap_test_iterate_set),
+ KUNIT_CASE(sbitmap_test_free_clears_alloc_hint),
+ KUNIT_CASE(sbitmap_test_queue_free_clears_ws),
{}
};
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] lib/sbitmap: test that free() clears alloc_hint and ws
2026-12-03 9:31 ` [PATCH v2] " Wang Yibo
@ 2026-12-03 9:45 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 9:45 UTC (permalink / raw)
To: Wang Yibo; +Cc: linux-scsi
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] lib/sbitmap: KUnit tests fail without prerequisite commit
ddca9a368588
--
commit 44070cee85e0c098e00fe8a98688018abe397d58
Author: Wang Yibo <wangyibo@kylinos.cn>
lib/sbitmap: test that free() clears alloc_hint and ws
This commit extends the sbitmap KUnit suite to check that sbitmap_free()
clears alloc_hint, sbitmap_queue_free() clears ws, and repeated free
calls are safe.
Link: https://lore.kernel.org/r/20261202112938.a596225d374bdf002bd1ad81@linux-foundation.org
> diff --git a/lib/tests/sbitmap_kunit.c b/lib/tests/sbitmap_kunit.c
> index 284dcb5d7188b..98a544e4898a2 100644
> --- a/lib/tests/sbitmap_kunit.c
> +++ b/lib/tests/sbitmap_kunit.c
> @@ -45,6 +45,7 @@ static void sbitmap_test_init(struct kunit *test)
> KUNIT_EXPECT_FALSE(test, sbitmap_any_bit_set(&sb));
> KUNIT_EXPECT_EQ(test, sbitmap_weight(&sb), 0U);
> sbitmap_free(&sb);
> + KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
>
> /* An explicitly requested shift is honoured. */
> ret = sbitmap_init_node(&sb, depth, 3, GFP_KERNEL, NUMA_NO_NODE,
> @@ -285,6 +286,47 @@ static void sbitmap_test_iterate_set(struct kunit *test)
> sbitmap_free(&sb);
> }
>
> +static void sbitmap_test_free_clears_alloc_hint(struct kunit *test)
> +{
> + struct sbitmap sb;
> +
> + KUNIT_ASSERT_EQ(test, sbitmap_init_node(&sb, BITS_PER_LONG, -1,
> + GFP_KERNEL, NUMA_NO_NODE,
> + false, true), 0);
> + KUNIT_EXPECT_TRUE(test, sb.alloc_hint);
> +
> + sbitmap_free(&sb);
> +
> + /*
> + * alloc_hint is a __percpu pointer, so test it as a boolean, as in
> + * sbitmap_test_init(). Clearing it makes repeated teardown safe,
> + * which is what the scsi error-cleanup path relies on. Assert
> + * before freeing again: EXPECT would continue on failure and pass
> + * the stale alloc_hint to free_percpu() on a tree without
> + * ddca9a368588.
> + */
> + KUNIT_ASSERT_FALSE(test, sb.alloc_hint);
> + sbitmap_free(&sb);
> + KUNIT_EXPECT_FALSE(test, sb.alloc_hint);
> +}
> +
> +static void sbitmap_test_queue_free_clears_ws(struct kunit *test)
> +{
> + struct sbitmap_queue sbq;
> +
> + KUNIT_ASSERT_EQ(test, sbitmap_queue_init_node(&sbq, BITS_PER_LONG, -1,
> + false, GFP_KERNEL,
> + NUMA_NO_NODE), 0);
> + KUNIT_EXPECT_TRUE(test, sbq.ws);
> +
> + sbitmap_queue_free(&sbq);
> +
> + /* Assert before freeing again so a stale ws never reaches kfree(). */
> + KUNIT_ASSERT_NULL(test, sbq.ws);
> + sbitmap_queue_free(&sbq);
> + KUNIT_EXPECT_NULL(test, sbq.ws);
> +}
[Severity: Medium]
Will these KUnit tests fail in this tree because prerequisite commit
ddca9a368588 ("sbitmap: Clear alloc_hint and ws pointers after freeing")
is not present?
In include/linux/sbitmap.h, sbitmap_free() does not clear sb->alloc_hint
and sbitmap_queue_free() does not clear sbq->ws yet. Running the suite
fails all three tests, breaking KUnit runs (enabled via KUNIT_ALL_TESTS)
and git bisect until this is queued on top of that prerequisite.
Also, the comment in sbitmap_test_free_clears_alloc_hint() hardcodes the
unmerged commit hash, which can go stale if that commit is rebased and
is unnecessary once this patch is ordered after it.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203093139.283431-1-wangyibo@kylinos.cn?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 9:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 8:35 [PATCH] lib/sbitmap: test that free() clears alloc_hint and ws Wang Yibo
2026-12-03 8:43 ` sashiko-bot
2026-12-03 9:31 ` [PATCH v2] " Wang Yibo
2026-12-03 9:45 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox