Skip to contentSkip to conversation

lib/genalloc: fix a NULL pool check and add KUnit tests

PATCH In review Amit Patole 2 participants 4 messages Nov 6

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

cover lib/genalloc: fix a NULL pool check and add KUnit tests

genalloc has no unit tests, so patch 2 adds a KUnit suite. Writing it turned up that gen_pool_dma_alloc() and gen_pool_dma_zalloc() crash on a NULL pool, while the other DMA helpers return NULL as gen_pool_dma_alloc() originally did. Patch 1 restores the check; no in-tree caller passes a NULL pool, so it is not marked for stable.

Tested on UML with a kunitconfig of CONFIG_KUNIT=y and CONFIG_GENALLOC_KUNIT_TEST=y: 12/12 pass with patch 1, and genalloc_dma_test crashes without it.

---
Amit Patole (2):
lib/genalloc: don't dereference a NULL pool in gen_pool_dma_[z]alloc() lib/tests: add KUnit tests for genalloc

 lib/Kconfig.debug          |  15 ++
 lib/genalloc.c             |   6 +
 lib/tests/Makefile         |   1 +
 lib/tests/genalloc_kunit.c | 508 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 530 insertions(+)

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261106-kunit-genalloc-32ccb20f97a6

Best regards,

Signature · 1 line
Amit Patole <[email protected]>
#1 lore raw

PATCH 1/2 lib/genalloc: don't dereference a NULL pool in gen_pool_dma_[z]alloc()

gen_pool_dma_alloc() returned NULL for a NULL pool since it was added. Commit cf394fc5f715 ("lib/genalloc.c: Add algorithm, align and zeroed family of DMA allocators") moved that check into gen_pool_dma_alloc_algo(), but gen_pool_dma_alloc() now reads pool->algo and pool->data before calling it, so a NULL pool is dereferenced instead. gen_pool_dma_zalloc(), added by the same commit, has the same problem. The _algo() and _align() variants still return NULL.

All in-tree callers check the pool before calling, so this is not known to be reachable today, but the four entry points disagree and the original behaviour is easy to restore. Check for a NULL pool before dereferencing it.

Found by a KUnit test for lib/genalloc.c, which crashed in gen_pool_dma_alloc(NULL, ...) on UML. An AI coding assistant (Claude Opus 5.5) was used to write that test and this change; with it the test passes.

Fixes cf394fc5f715 ("lib/genalloc.c: Add algorithm, align and zeroed family of DMA allocators")
---
 lib/genalloc.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/genalloc.c +6 -0
index 841f29783833..40ca25cd87c8 100644
@@ -340,6 +340,9 @@ EXPORT_SYMBOL(gen_pool_alloc_algo_owner);
  */
 void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
 {
+	if (!pool)
+		return NULL;
+
 	return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data);
 }
 EXPORT_SYMBOL(gen_pool_dma_alloc);
@@ -418,6 +421,9 @@ EXPORT_SYMBOL(gen_pool_dma_alloc_align);
  */
 void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
 {
+	if (!pool)
+		return NULL;
+
 	return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data);
 }
 EXPORT_SYMBOL(gen_pool_dma_zalloc);
Signature · 1 line
2.43.5
#3 lore raw

PATCH 2/2 lib/tests: add KUnit tests for genalloc

In reply to Amit Patole

lib/genalloc.c is the allocator that drivers use for on-chip SRAM, IRAM and other special memory (i.MX SDMA, CODA, Tegra BPMP, s390 CIO, USB local memory and others), but it has no unit tests.

Add a KUnit suite that covers:

- gen_pool_size(), gen_pool_avail() and gen_pool_has_addr();
- allocation and freeing, rounding to the allocation granule, zero and oversized requests, and an exact fit;
- the first fit, best fit (including the tie-break between equal holes), first fit aligned, first fit order aligned and fixed offset algorithms, and gen_pool_set_algo(pool, NULL, NULL);
- aligned allocation from a chunk that does not start on the alignment boundary;
- pools with several chunks, gen_pool_virt_to_phys() and gen_pool_for_each_chunk();
- chunk owners;
- the DMA helpers, including zeroing, alignment and a NULL pool;
- devm_gen_pool_create() and gen_pool_get() with named and unnamed pools on a KUnit device.

gen_pool_destroy() BUGs on outstanding allocations, so the cleanup clears the chunk bitmaps first, and allocation results that are freed later are checked with assertions; a failing test then fails cleanly instead of crashing the kernel.

An AI coding assistant (Claude Opus 5.5) was used to draft the tests. They were run with "tools/testing/kunit/kunit.py run" on UML (12/12 pass), built with W=1 without warnings, and checked against deliberately broken versions of gen_pool_best_fit(), gen_pool_first_fit_align() and the accounting in gen_pool_free_owner(), each of which makes one test fail.

---
 lib/Kconfig.debug          |  15 ++
 lib/tests/Makefile         |   1 +
 lib/tests/genalloc_kunit.c | 508 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 524 insertions(+)
lib/Kconfig.debug +15 -0
index 2d22638ec13a..c66c3711e68c 100644
@@ -3558,6 +3558,21 @@ config PRIME_NUMBERS_KUNIT_TEST
 
 	  If unsure, say N
 
+config GENALLOC_KUNIT_TEST
+	tristate "KUnit test for the general purpose allocator" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select GENERIC_ALLOCATOR
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables the KUnit test suite for lib/genalloc.c, the
+	  allocator drivers use to manage on-chip SRAM and other special
+	  memory regions. The tests cover allocation and freeing, the
+	  first fit, best fit, aligned, order aligned and fixed offset
+	  algorithms, multiple chunks, virtual to physical translation,
+	  the DMA helpers and the device-managed pools.
+
+	  If unsure, say N.
+
 config GLOB_KUNIT_TEST
 	tristate "Glob matching test" if !KUNIT_ALL_TESTS
 	depends on GLOB
lib/tests/Makefile +1 -0
index df0537e1b91f..f17e4241e148 100644
@@ -23,6 +23,7 @@ CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)
 obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
 CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE)
 obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o
+obj-$(CONFIG_GENALLOC_KUNIT_TEST) += genalloc_kunit.o
 obj-$(CONFIG_GLOB_KUNIT_TEST) += glob_kunit.o
 obj-$(CONFIG_HASHTABLE_KUNIT_TEST) += hashtable_test.o
 obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o
lib/tests/genalloc_kunit.c new file +508 -0

508 lines not on this page · raw message

Signature · 1 line
2.43.5
#2 lore raw

Re: [PATCH 1/2] lib/genalloc: don't dereference a NULL pool in gen_pool_dma_[z]alloc()

In reply to Amit Patole

Hi Amit,

Quoted 9 lines · Amit Patole

gen_pool_dma_alloc() returned NULL for a NULL pool since it was added. Commit cf394fc5f715 ("lib/genalloc.c: Add algorithm, align and zeroed family of DMA allocators") moved that check into gen_pool_dma_alloc_algo(), but gen_pool_dma_alloc() now reads pool->algo and pool->data before calling it, so a NULL pool is dereferenced instead. gen_pool_dma_zalloc(), added by the same commit, has the same problem. The _algo() and _align() variants still return NULL.

Quoted 4 lines

All in-tree callers check the pool before calling, so this is not known to be reachable today, but the four entry points disagree and the original behaviour is easy to restore. Check for a NULL pool before dereferencing it.

If this change is accepted, may I suggest that the corresponding documentation

@pool: pool to allocate from

is updated to (something like)

@pool: pool to allocate from, or %NULL

as well? There seem to be 8 instances, but some of the others currently don't accept %NULL, for example gen_pool_virt_to_phys(), which I suppose could be made to return -1 in that case?

And then there's gen_pool_destroy(), which currently doesn't accept %NULL either. Error paths can often be simplified if free/destroy/release/etc. accept %NULL.

I'm not up-to-date with the latest NULL handling policy in the kernel, but I think some inconsistencies can be found still. :-)

Fredrik