Skip to contentSkip to conversation

printk: kunit: support offstack cpumask

PATCH In review Petr Mladek 1 participant 2 messages Sep 2, 2025 – Sep 10, 2025 Printk

lore
Share & tools
lore

Petr Mladek

author maintainer
#0 lore raw

PATCH v3 1/1 printk: kunit: support offstack cpumask

From: Arnd Bergmann <[email protected]>

For large values of CONFIG_NR_CPUS, the newly added kunit test fails to build:

kernel/printk/printk_ringbuffer_kunit_test.c: In function 'test_readerwriter':
kernel/printk/printk_ringbuffer_kunit_test.c:279:1: error: the frame size of 1432 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Change this to use cpumask_var_t and allocate it dynamically when CONFIG_CPUMASK_OFFSTACK is set.

The variable has to be released via a KUnit action wrapper so that it is freed when the test fails and gets aborted. The parameter type is hardcoded to "struct cpumask *" because the macro KUNIT_DEFINE_ACTION_WRAPPER() does not accept an array. But the function does nothing when CONFIG_CPUMASK_OFFSTACK is not set anyway.

Fixes 5ea2bcdfbf46 ("printk: ringbuffer: Add KUnit test")
[[email protected]: Correctly handle allocation failures and freeing using KUnit test API.]

---
This patch applies on top of "rework/ringbuffer-kunit-test" branch in printk/linux.git.

Changes against v2 [2]:

- Hardcode the pointer type to "struct cpumask *" when defining prbtest_cpumask_cleanup() action to avoid warning when CONFIG_CPUMASK_OFFSTACK is not set.

Changes against v1 [1]:

- Abort the test when the cpumask allocation fails.
- Free the cpumask when the tests exits.

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]

 kernel/printk/printk_ringbuffer_kunit_test.c | 24 +++++++++++++++-----
 1 file changed, 18 insertions(+), 6 deletions(-)
kernel/printk/printk_ringbuffer_kunit_test.c +18 -6
index e67e1815f4c8..2282348e869a 100644
@@ -223,8 +223,17 @@ static int prbtest_reader(struct prbtest_data *test_data, unsigned long timeout_
 	return 0;
 }
 
+KUNIT_DEFINE_ACTION_WRAPPER(prbtest_cpumask_cleanup, free_cpumask_var, struct cpumask *);
 KUNIT_DEFINE_ACTION_WRAPPER(prbtest_kthread_cleanup, kthread_stop, struct task_struct *);
 
+static void prbtest_add_cpumask_cleanup(struct kunit *test, cpumask_var_t mask)
+{
+	int err;
+
+	err = kunit_add_action_or_reset(test, prbtest_cpumask_cleanup, mask);
+	KUNIT_ASSERT_EQ(test, err, 0);
+}
+
 static void prbtest_add_kthread_cleanup(struct kunit *test, struct task_struct *kthread)
 {
 	int err;
@@ -247,9 +256,12 @@ static void test_readerwriter(struct kunit *test)
 	struct prbtest_thread_data *thread_data;
 	struct prbtest_data *test_data;
 	struct task_struct *thread;
-	cpumask_t test_cpus;
+	cpumask_var_t test_cpus;
 	int cpu, reader_cpu;
 
+	KUNIT_ASSERT_TRUE(test, alloc_cpumask_var(&test_cpus, GFP_KERNEL));
+	prbtest_add_cpumask_cleanup(test, test_cpus);
+
 	cpus_read_lock();
 	/*
 	 * Failure of KUNIT_ASSERT() kills the current task
@@ -257,15 +269,15 @@ static void test_readerwriter(struct kunit *test)
 	 * Instead use a snapshot of the online CPUs.
 	 * If they change during test execution it is unfortunate but not a grave error.
 	 */
-	cpumask_copy(&test_cpus, cpu_online_mask);
+	cpumask_copy(test_cpus, cpu_online_mask);
 	cpus_read_unlock();
 
 	/* One CPU is for the reader, all others are writers */
-	reader_cpu = cpumask_first(&test_cpus);
-	if (cpumask_weight(&test_cpus) == 1)
+	reader_cpu = cpumask_first(test_cpus);
+	if (cpumask_weight(test_cpus) == 1)
 		kunit_warn(test, "more than one CPU is recommended");
 	else
-		cpumask_clear_cpu(reader_cpu, &test_cpus);
+		cpumask_clear_cpu(reader_cpu, test_cpus);
 
 	/* KUnit test can get restarted more times. */
 	prbtest_prb_reinit(&test_rb);
@@ -278,7 +290,7 @@ static void test_readerwriter(struct kunit *test)
 
 	kunit_info(test, "running for %lu ms\n", runtime_ms);
 
-	for_each_cpu(cpu, &test_cpus) {
+	for_each_cpu(cpu, test_cpus) {
 		thread_data = kunit_kmalloc(test, sizeof(*thread_data), GFP_KERNEL);
 		KUNIT_ASSERT_NOT_NULL(test, thread_data);
 		thread_data->test_data = test_data;
Signature · 1 line
2.50.1
author maintainer
#1 lore raw
Quoted 17 lines · Petr Mladek

From: Arnd Bergmann <[email protected]>

For large values of CONFIG_NR_CPUS, the newly added kunit test fails to build:

kernel/printk/printk_ringbuffer_kunit_test.c: In function 'test_readerwriter':
kernel/printk/printk_ringbuffer_kunit_test.c:279:1: error: the frame size of 1432 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Change this to use cpumask_var_t and allocate it dynamically when CONFIG_CPUMASK_OFFSTACK is set.

The variable has to be released via a KUnit action wrapper so that it is freed when the test fails and gets aborted. The parameter type is hardcoded to "struct cpumask *" because the macro KUNIT_DEFINE_ACTION_WRAPPER() does not accept an array. But the function does nothing when CONFIG_CPUMASK_OFFSTACK is not set anyway.

Quoted 4 lines
Fixes 5ea2bcdfbf46 ("printk: ringbuffer: Add KUnit test")
[[email protected]: Correctly handle allocation failures and freeing using KUnit test API.]

JFYI, this patch have been committed into printk/linux.git, branch rework/ringbuffer-kunit-test.

It worked, I did it according to the feedback. Let's give it some spin in linux-next before the next merge window.

Feel free to ask me to revert it, ...

Best Regards,
Petr