[PATCH 2/3] printk: kunit: support offstack cpumask

From: Petr Mladek
Date: Wed Jul 02 2025 - 05:53:52 EST


From: Arnd Bergmann <arnd@xxxxxxxx>

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.

Fixes: 5ea2bcdfbf46 ("printk: ringbuffer: Add KUnit test")
Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>
[pmladek@xxxxxxxx: Correctly handle allocation failures and freeing using KUnit test API.]
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk_ringbuffer_kunit_test.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/kernel/printk/printk_ringbuffer_kunit_test.c b/kernel/printk/printk_ringbuffer_kunit_test.c
index 217dcc14670c..0c3030fde8c2 100644
--- a/kernel/printk/printk_ringbuffer_kunit_test.c
+++ b/kernel/printk/printk_ringbuffer_kunit_test.c
@@ -216,6 +216,7 @@ static int prbtest_reader(struct prbtest_data *test_data, unsigned long timeout_
return 0;
}

+KUNIT_DEFINE_ACTION_WRAPPER(prbtest_cpumask_cleanup, free_cpumask_var, cpumask_var_t);
KUNIT_DEFINE_ACTION_WRAPPER(prbtest_kthread_cleanup, kthread_stop, struct task_struct *);

static void prbtest_add_kthread_cleanup(struct kunit *test, struct task_struct *kthread)
@@ -240,8 +241,13 @@ 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;
+ int err;
+
+ KUNIT_ASSERT_TRUE(test, alloc_cpumask_var(&test_cpus, GFP_KERNEL));
+ err = kunit_add_action_or_reset(test, prbtest_cpumask_cleanup, test_cpus);
+ KUNIT_ASSERT_EQ(test, err, 0);

cpus_read_lock();
/*
@@ -250,15 +256,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);
@@ -271,7 +277,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;
--
2.50.0