From: Amit Patole <amit.patole@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Fredrik Noring <noring@nocrew.org>
Cc: Christoph Hellwig <hch@lst.de>,
Brendan Higgins <brendan.higgins@linux.dev>,
David Gow <david@davidgow.net>, Rae Moar <raemoar63@gmail.com>,
linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
linux-kernel@vger.kernel.org,
Amit Patole <amit.patole@gmail.com>
Subject: [PATCH 1/2] lib/genalloc: don't dereference a NULL pool in gen_pool_dma_[z]alloc()
Date: Fri, 06 Nov 2026 09:02:32 -0500 [thread overview]
Message-ID: <20261106-kunit-genalloc-v1-1-ba1ea397dfd8@gmail.com> (raw)
In-Reply-To: <20261106-kunit-genalloc-v1-0-ba1ea397dfd8@gmail.com>
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")
Signed-off-by: Amit Patole <amit.patole@gmail.com>
---
lib/genalloc.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/lib/genalloc.c b/lib/genalloc.c
index 841f29783833..40ca25cd87c8 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -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);
--
2.43.5
next prev parent reply other threads:[~2026-11-06 14:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 14:02 [PATCH 0/2] lib/genalloc: fix a NULL pool check and add KUnit tests Amit Patole
2026-11-06 14:02 ` Amit Patole [this message]
2026-11-06 18:46 ` [PATCH 1/2] lib/genalloc: don't dereference a NULL pool in gen_pool_dma_[z]alloc() Fredrik Noring
2026-11-06 14:02 ` [PATCH 2/2] lib/tests: add KUnit tests for genalloc Amit Patole
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261106-kunit-genalloc-v1-1-ba1ea397dfd8@gmail.com \
--to=amit.patole@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=brendan.higgins@linux.dev \
--cc=david@davidgow.net \
--cc=hch@lst.de \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=noring@nocrew.org \
--cc=raemoar63@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®