From: Petr Mladek <pmladek@suse.com>
To: Nathan Chancellor <nathan@kernel.org>
Cc: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"John Ogness" <john.ogness@linutronix.de>,
"Dan Carpenter" <dan.carpenter@linaro.org>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Sergey Senozhatsky" <senozhatsky@chromium.org>,
"Kees Cook" <kees@kernel.org>,
"Gustavo A . R . Silva" <gustavoars@kernel.org>,
"David Gow" <davidgow@google.com>,
"Arnd Bergmann" <arnd@kernel.org>,
"Arnd Bergmann" <arnd@arndb.de>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH 2/3] printk: kunit: support offstack cpumask
Date: Tue, 8 Jul 2025 16:24:45 +0200 [thread overview]
Message-ID: <aG0qLaeAoTGaRs0n@pathway.suse.cz> (raw)
In-Reply-To: <20250702202835.GA593751@ax162>
On Wed 2025-07-02 13:28:35, Nathan Chancellor wrote:
> On Wed, Jul 02, 2025 at 11:51:56AM +0200, Petr Mladek wrote:
> > From: Arnd Bergmann <arnd@arndb.de>
> >
> > 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@arndb.de>
> > [pmladek@suse.com: Correctly handle allocation failures and freeing using KUnit test API.]
> > Signed-off-by: Petr Mladek <pmladek@suse.com>
> > ---
> > 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);
>
> This appears to break the build for me when CONFIG_CPUMASK_OFFSTACK is not
> set, like when enabling this test on top of x86_64 defconfig:
>
> In file included from kernel/printk/printk_ringbuffer_kunit_test.c:14:
> kernel/printk/printk_ringbuffer_kunit_test.c: In function 'prbtest_cpumask_cleanup':
> include/kunit/resource.h:409:32: error: cast specifies array type
> 409 | arg_type arg = (arg_type)in; \
> | ^
> kernel/printk/printk_ringbuffer_kunit_test.c:226:1: note: in expansion of macro 'KUNIT_DEFINE_ACTION_WRAPPER'
> 226 | KUNIT_DEFINE_ACTION_WRAPPER(prbtest_cpumask_cleanup, free_cpumask_var, cpumask_var_t);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Clang's error might be a little clearer with the "aka" note it provides.
>
> kernel/printk/printk_ringbuffer_kunit_test.c:226:1: error: used type 'cpumask_var_t' (aka 'struct cpumask[1]') where arithmetic or pointer type is required
> 226 | KUNIT_DEFINE_ACTION_WRAPPER(prbtest_cpumask_cleanup, free_cpumask_var, cpumask_var_t);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> include/kunit/resource.h:409:18: note: expanded from macro 'KUNIT_DEFINE_ACTION_WRAPPER'
> 409 | arg_type arg = (arg_type)in; \
> | ^ ~~
>
> > 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)
Thanks a lot for the nice report.
The problem is how cpumask_var_t is defined in include/linux/cpumask_types.h:
#ifdef CONFIG_CPUMASK_OFFSTACK
typedef struct cpumask *cpumask_var_t;
#else
typedef struct cpumask cpumask_var_t[1];
#endif /* CONFIG_CPUMASK_OFFSTACK */
And KUNIT_DEFINE_ACTION_WRAPPER() expect that the 3rd parameter
is a pointer.
I am going to solve this by adding a wrapper over free_cpumask_var()
which would work with a pointer to cpumask_var_t.
It has another catch, though. It seems that the automatic clean up is
done after test_readerwriter() finishes. Therefore the variable
@test_cpus can't be on stack.
Anyway, the following seems to work with both CONFIG_CPUMASK_OFFSTACK
enabled and disabled:
--- a/kernel/printk/printk_ringbuffer_kunit_test.c
+++ b/kernel/printk/printk_ringbuffer_kunit_test.c
@@ -223,7 +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, cpumask_var_t);
+/*
+ * Add a custom wrapper around free_cpumask_var() to be used by
+ * KUNIT_DEFINE_ACTION_WRAPPER(). It allows to pass @mask using
+ * a pointer even when CONFIG_CPUMASK_OFFSTACK is disabled.
+ */
+static void prbtest_free_cpumask_var(cpumask_var_t *mask)
+{
+ free_cpumask_var(*mask);
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(prbtest_cpumask_cleanup, prbtest_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)
@@ -244,16 +254,19 @@ static void test_readerwriter(struct kunit *test)
{
/* Equivalent to CONFIG_LOG_BUF_SHIFT=13 */
DEFINE_PRINTKRB(test_rb, 8, 5);
-
+ /*
+ * @test_cpus can't be on stack. THe pointer to this variable is passed
+ * to an automatic clean up action, see prbtest_free_cpumask_var().
+ */
+ static cpumask_var_t test_cpus;
struct prbtest_thread_data *thread_data;
struct prbtest_data *test_data;
struct task_struct *thread;
- 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);
+ err = kunit_add_action_or_reset(test, prbtest_cpumask_cleanup, &test_cpus);
KUNIT_ASSERT_EQ(test, err, 0);
cpus_read_lock();
next prev parent reply other threads:[~2025-07-08 14:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-02 9:51 [PATCH 0/3] printk: KUnit: Followup fixes for the new KUnit test Petr Mladek
2025-07-02 9:51 ` [PATCH 1/3] printk: ringbuffer: Explain why the KUnit test ignores failed writes Petr Mladek
2025-07-04 11:28 ` John Ogness
2025-07-02 9:51 ` [PATCH 2/3] printk: kunit: support offstack cpumask Petr Mladek
2025-07-02 20:28 ` Nathan Chancellor
2025-07-08 14:24 ` Petr Mladek [this message]
2025-07-08 14:48 ` Arnd Bergmann
2025-07-09 11:36 ` Petr Mladek
2025-07-09 12:53 ` Thomas Weißschuh
2025-07-10 13:51 ` Petr Mladek
2025-07-10 14:08 ` Arnd Bergmann
2025-09-02 13:55 ` Petr Mladek
2025-07-03 14:36 ` kernel test robot
2025-07-02 9:51 ` [PATCH 3/3] printk: kunit: Fix __counted_by() in struct prbtest_rbdata Petr Mladek
2025-07-04 11:41 ` John Ogness
2025-07-02 15:48 ` [PATCH 0/3] printk: KUnit: Followup fixes for the new KUnit test Thomas Weißschuh
2025-07-10 15:29 ` Petr Mladek
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=aG0qLaeAoTGaRs0n@pathway.suse.cz \
--to=pmladek@suse.com \
--cc=arnd@arndb.de \
--cc=arnd@kernel.org \
--cc=dan.carpenter@linaro.org \
--cc=davidgow@google.com \
--cc=gustavoars@kernel.org \
--cc=john.ogness@linutronix.de \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nathan@kernel.org \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=thomas.weissschuh@linutronix.de \
/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