Skip to contentSkip to conversation

printk: kunit: support offstack cpumask

PATCH In review Arnd Bergmann 4 participants 4 messages Jun 20, 2025 – Jun 23, 2025 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 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.

Fixes 5ea2bcdfbf46 ("printk: ringbuffer: Add KUnit test")
---
 kernel/printk/printk_ringbuffer_kunit_test.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
kernel/printk/printk_ringbuffer_kunit_test.c +11 -6
index 4081ae051d8e..9f79bc91246e 100644
@@ -227,9 +227,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;
 
+	if (alloc_cpumask_var(&test_cpus, GFP_KERNEL))
+		return;
+
 	cpus_read_lock();
 	/*
 	 * Failure of KUNIT_ASSERT() kills the current task
@@ -237,15 +240,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);
@@ -258,7 +261,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;
@@ -276,6 +279,8 @@ static void test_readerwriter(struct kunit *test)
 	prbtest_reader(test_data, runtime_ms);
 
 	kunit_info(test, "completed test\n");
+
+	free_cpumask_var(test_cpus);
 }
 
 static struct kunit_case prb_test_cases[] = {
Signature · 1 line
2.39.5
#1 lore raw

kernel/printk/printk_ringbuffer_kunit_test.c static void test_readerwriter(struct kunit *test) · line 235

Quoted 4 lines · Arnd Bergmann
kernel/printk/printk_ringbuffer_kunit_test.c +3 -0
+	if (alloc_cpumask_var(&test_cpus, GFP_KERNEL))
+		return;
+

Sorry, I got the polarity wrong here, alloc_cpumask_var() returns true on success.

Arnd

#2 lore raw
In reply to Arnd Bergmann
Quoted 22 lines · Arnd Bergmann

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.

Fixes 5ea2bcdfbf46 ("printk: ringbuffer: Add KUnit test")
---
 kernel/printk/printk_ringbuffer_kunit_test.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
kernel/printk/printk_ringbuffer_kunit_test.c +0 -0
index 4081ae051d8e..9f79bc91246e 100644

kernel/printk/printk_ringbuffer_kunit_test.c static void test_readerwriter(struct kunit *test) · line 234

Quoted 9 lines
kernel/printk/printk_ringbuffer_kunit_test.c +3 -1
@@ -227,9 +227,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;
 
+	if (alloc_cpumask_var(&test_cpus, GFP_KERNEL))
+		return;

IMO this shouldn't fail silently and instead should do:

KUNIT_FAIL_AND_ABORT(test, "Unable to allocate cpumask");

Quoted 45 lines
kernel/printk/printk_ringbuffer_kunit_test.c +8 -5
+
 	cpus_read_lock();
 	/*
 	 * Failure of KUNIT_ASSERT() kills the current task
@@ -237,15 +240,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);
@@ -258,7 +261,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;
@@ -276,6 +279,8 @@ static void test_readerwriter(struct kunit *test)
 	prbtest_reader(test_data, runtime_ms);
 
 	kunit_info(test, "completed test\n");
+
+	free_cpumask_var(test_cpus);
 }
 
 static struct kunit_case prb_test_cases[] = {
Signature · 1 line
2.39.5
maintainer
#3 lore raw
Quoted 30 lines · Thomas Weißschuh
Quoted 29 lines · Arnd Bergmann

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.

Fixes 5ea2bcdfbf46 ("printk: ringbuffer: Add KUnit test")
---
 kernel/printk/printk_ringbuffer_kunit_test.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
kernel/printk/printk_ringbuffer_kunit_test.c +1 -1
index 4081ae051d8e..9f79bc91246e 100644
@@ -227,9 +227,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;
 
Quoted 6 lines

kernel/printk/printk_ringbuffer_kunit_test.c static void test_readerwriter(struct kunit *test) · line 234

Quoted 2 lines
kernel/printk/printk_ringbuffer_kunit_test.c +2 -0
+	if (alloc_cpumask_var(&test_cpus, GFP_KERNEL))
+		return;

IMO this shouldn't fail silently and instead should do:

KUNIT_FAIL_AND_ABORT(test, "Unable to allocate cpumask");

Also we need to call kunit_add_action_or_reset() to free the mask when the test fails (aborts) instead of the free_cpumask_var() below.

The following changes on it top of this patch worked for me:

kernel/printk/printk_ringbuffer_kunit_test.c +5 -4
index 9f79bc91246e..850e5240222c 100644
@@ -203,6 +203,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)
@@ -229,9 +230,11 @@ static void test_readerwriter(struct kunit *test)
 	struct task_struct *thread;
 	cpumask_var_t test_cpus;
 	int cpu, reader_cpu;
+	int err;
 
-	if (alloc_cpumask_var(&test_cpus, GFP_KERNEL))
-		return;
+	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();
 	/*
@@ -279,8 +282,6 @@ static void test_readerwriter(struct kunit *test)
 	prbtest_reader(test_data, runtime_ms);
 
 	kunit_info(test, "completed test\n");
-
-	free_cpumask_var(test_cpus);
 }
 
 static struct kunit_case prb_test_cases[] = {

Arnd, could you pleae send v2 with the above changes?

Best Regards,
Petr