diff mbox series

[RFC,1/4] kunit: Allocate assertion data with GFP_ATOMIC

Message ID 20250224-page-alloc-kunit-v1-1-d337bb440889@google.com (mailing list archive)
State New
Delegated to: Brendan Higgins
Headers show
Series mm: KUnit tests for the page allocator | expand

Commit Message

Brendan Jackman Feb. 24, 2025, 2:47 p.m. UTC
At present KUnit doesn't handle assertions happening in atomic contexts.
A later commit will add tests that make assertions with spinlocks held.

In preparation, switch to GFP_ATOMIC.

"Just use GFP_ATOMIC" is not generally a solution to this kind of
problem: since it uses up memory reserves, instead it should be only
used when truly needed.

However, for test code that should not be expected to run in production
systems it seems tolerable, given that it avoids creating more complex
APIs.

Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
 lib/kunit/assert.c   | 2 +-
 lib/kunit/resource.c | 2 +-
 lib/kunit/test.c     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
index 867aa5c4bccf764757e190948b8e3a2439116786..f08656c5fb247b510c4215445cc307ed1205a96c 100644
--- a/lib/kunit/assert.c
+++ b/lib/kunit/assert.c
@@ -101,7 +101,7 @@  VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
 	if (strlen(text) != len)
 		return false;
 
-	buffer = kmalloc(len+1, GFP_KERNEL);
+	buffer = kmalloc(len+1, GFP_ATOMIC);
 	if (!buffer)
 		return false;
 
diff --git a/lib/kunit/resource.c b/lib/kunit/resource.c
index f0209252b179f8b48d47ecc244c468ed80e23bdc..eac511af4f8d7843d58c4e3976c77a9c4def86a7 100644
--- a/lib/kunit/resource.c
+++ b/lib/kunit/resource.c
@@ -98,7 +98,7 @@  int kunit_add_action(struct kunit *test, void (*action)(void *), void *ctx)
 
 	KUNIT_ASSERT_NOT_NULL_MSG(test, action, "Tried to action a NULL function!");
 
-	action_ctx = kzalloc(sizeof(*action_ctx), GFP_KERNEL);
+	action_ctx = kzalloc(sizeof(*action_ctx), GFP_ATOMIC);
 	if (!action_ctx)
 		return -ENOMEM;
 
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 146d1b48a0965e8aaddb6162928f408bbb542645..08d0ff51bd85845a08b40cd3933dd588bd10bddf 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -279,7 +279,7 @@  static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
 
 	kunit_set_failure(test);
 
-	stream = kunit_alloc_string_stream(test, GFP_KERNEL);
+	stream = kunit_alloc_string_stream(test, GFP_ATOMIC);
 	if (IS_ERR(stream)) {
 		WARN(true,
 		     "Could not allocate stream to print failed assertion in %s:%d\n",