printk: ringbuffer: Add KUnit test
PATCH v2 1/1 printk: ringbuffer: Add KUnit test
The KUnit test validates the correct operation of the ringbuffer. A separate dedicated ringbuffer is used so that the global printk ringbuffer is not touched.
---
Changes in v2:
- Adapt to new MODULE_IMPORT_NS() syntax
- Handle platforms without u64 division
- Allocate resources through kunit wrappers
- Drop setup thread
- Allow multiple executions
- Make naming more consistent
- Use get_random_u32_below()
- Allow !CONFIG_SMP
- Follow KUNIT kconfig naming scheme
- Fix limited string formatting in prbtest_fail_record()
- Fix MODULE_DESCRIPTION
- Use get_random_u32_inclusive()
- Drop timing measurements, preemption and lost tracking
- Use plain jiffies variable
- Print warning if only one CPU is present
- Style cleanup
- Fix formatting of bad message payload
- Link to v1: https://lore.kernel.org/r/[email protected]
--- init/Kconfig | 12 ++ kernel/printk/.kunitconfig | 3 + kernel/printk/Makefile | 2 + kernel/printk/printk_ringbuffer.c | 4 + kernel/printk/printk_ringbuffer_kunit_test.c | 292 +++++++++++++++++++++++++++ 5 files changed, 313 insertions(+)
init/Kconfig +12 -0
@@ -1650,6 +1650,18 @@ config PRINTK very difficult to diagnose system problems, saying N here is strongly discouraged. +config PRINTK_RINGBUFFER_KUNIT_TEST + tristate "KUnit Test for the printk ringbuffer" if !KUNIT_ALL_TESTS + depends on PRINTK && KUNIT + default KUNIT_ALL_TESTS + help + This builds the printk ringbuffer KUnit test suite. + + For more information on KUnit and unit tests in general, please refer + to the KUnit documentation. + + If unsure, say N. + config BUG bool "BUG() support" if EXPERT default y
kernel/printk/.kunitconfig new file +3 -0
@@ -0,0 +1,3 @@ +CONFIG_KUNIT=y +CONFIG_PRINTK=y +CONFIG_PRINTK_RINGBUFFER_KUNIT_TEST=y
kernel/printk/Makefile +2 -0
@@ -7,3 +7,5 @@ obj-$(CONFIG_PRINTK_INDEX) += index.o obj-$(CONFIG_PRINTK) += printk_support.o printk_support-y := printk_ringbuffer.o printk_support-$(CONFIG_SYSCTL) += sysctl.o + +obj-$(CONFIG_PRINTK_RINGBUFFER_KUNIT_TEST) += printk_ringbuffer_kunit_test.o
kernel/printk/printk_ringbuffer.c +4 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +#include <kunit/visibility.h> #include <linux/kernel.h> #include <linux/irqflags.h> #include <linux/string.h>
@@ -1685,6 +1686,7 @@ bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb,memset (r,0 ,sizeof (*r));return false; } +EXPORT_SYMBOL_IF_KUNIT (prb_reserve);/* Commit the data (possibly finalizing it) and restore interrupts. */ static void _prb_commit (struct prb_reserved_entry *e,unsigned long state_val)
@@ -1759,6 +1761,7 @@ void prb_commit(struct prb_reserved_entry *e)if (head_id != e->id)desc_make_final (e->rb, e->id); } +EXPORT_SYMBOL_IF_KUNIT (prb_commit);/** * prb_final_commit() - Commit and finalize (previously reserved) data to
@@ -2184,6 +2187,7 @@ bool prb_read_valid(struct printk_ringbuffer *rb, u64 seq, {return _prb_read_valid (rb, &seq, r,NULL ); } +EXPORT_SYMBOL_IF_KUNIT (prb_read_valid);/** * prb_read_valid_info() - Non-blocking read of meta data for a requested
kernel/printk/printk_ringbuffer_kunit_test.c new file +292 -0
292 lines not on this page · raw message
---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20241011-printk-ringbuffer-test-f47f87c6a88b
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Thomas,
kernel test robot noticed the following build errors:
[auto build test ERROR on 0af2f6be1b4281385b618cb86ad946eded089ac8]
url: https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/printk-ringbuffer-Add-KUnit-test/20250506-200133 base: 0af2f6be1b4281385b618cb86ad946eded089ac8 patch link: https://lore.kernel.org/r/20250506-printk-ringbuffer-test-v2-1-152200569eb1%40linutronix.de patch subject: [PATCH v2] printk: ringbuffer: Add KUnit test config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20250508/[email protected]/config) compiler: m68k-linux-gcc (GCC) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250508/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>, old ones prefixed by <<):
WARNING: modpost: missing MODULE_DESCRIPTION() in lib/tests/slub_kunit.o
>> ERROR: modpost: "prb_init" [kernel/printk/printk_ringbuffer_kunit_test.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 10 lines · Thomas Weißschuh
The KUnit test validates the correct operation of the ringbuffer. A separate dedicated ringbuffer is used so that the global printk ringbuffer is not touched.
Co-developed-by John Ogness <[email protected]>Signed-off-by John Ogness <[email protected]>Signed-off-by Thomas Weißschuh <[email protected]>
kernel/printk/printk_ringbuffer.c bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb, · line 1692
Quoted 7 lines
kernel/printk/printk_ringbuffer.c +1
-0@@ -1685,6 +1686,7 @@ bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb,memset (r,0 ,sizeof (*r));return false; } +EXPORT_SYMBOL_IF_KUNIT (prb_reserve);/* Commit the data (possibly finalizing it) and restore interrupts. */ static void _prb_commit (struct prb_reserved_entry *e,unsigned long state_val)
[...]
I had to add:
@@ -2337,6 +2337,7 @@ void prb_init(struct printk_ringbuffer *rb,
infos[0].seq = -(u64)_DESCS_COUNT(descbits); infos[_DESCS_COUNT(descbits) - 1].seq = 0;
}
+EXPORT_SYMBOL_IF_KUNIT(prb_init);
/**
* prb_record_text_space() - Query the full actual used ringbuffer space for
Quoted 6 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +0
-0@@ -0,0 +1,292 @@
[...]
kernel/printk/printk_ringbuffer_kunit_test.c
Quoted 8 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +8
-0+struct prbtest_wakeup_timer { +struct timer_list timer; +struct task_struct *task; +}; + +static void prbtest_wakeup_callback (struct timer_list *timer) +{ +struct prbtest_wakeup_timer *wakeup =from_timer (wakeup, timer, timer);
Just for record. The function from_timer() has been renamed to timer_container_of() by the commit 41cb08555c4164996 ("treewide, timers: Rename from_timer() to timer_container_of()") in v6.16-rc1.
Quoted 30 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +30
-0+set_tsk_thread_flag (wakeup->task,TIF_NOTIFY_SIGNAL ); +wake_up_process (wakeup->task); +} + +static int prbtest_reader (struct prbtest_data *test_data,unsigned long timeout_ms) +{ +struct prbtest_wakeup_timer wakeup; +char text_buf[MAX_PRB_RECORD_SIZE ]; +unsigned long count =0 ; +struct printk_info info; +struct printk_record r; +u64 seq =0 ; + + wakeup.task = current; +timer_setup_on_stack (&wakeup.timer, prbtest_wakeup_callback,0 ); +mod_timer (&wakeup.timer, jiffies +msecs_to_jiffies (timeout_ms)); + +prb_rec_init_rd (&r, &info, text_buf,sizeof (text_buf)); + +kunit_info (test_data->test,"start reader\n" ); + +while (!wait_event_interruptible (test_data->new_record_wait, +prb_read_valid (test_data->ringbuffer, seq, &r))) { +/* check/track the sequence */ +if (info.seq < seq) +KUNIT_FAIL (test_data->test,"BAD SEQ READ: request=%llu read=%llu\n" , + seq, info.seq); + +if (!prbtest_check_data ((struct prbtest_rbdata *)r.text_buf)) + prbtest_fail_record(test_data->test,
kernel/printk/printk_ringbuffer_kunit_test.c
Quoted 10 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +10
-0+ (struct prbtest_rbdata *)r.text_buf, info.seq); + +if ((count++ &0x3fff ) ==0 ) +cond_resched (); + + seq = info.seq +1 ; + } + +timer_delete_sync (&wakeup.timer); +destroy_timer_on_stack (&wakeup.timer);
Also this function has been renamed by the commit aad823aa3a7d675a8d0 ("treewide, timers: Rename destroy_timer_on_stack() as timer_destroy_on_stack()") in v6.16-rc1.
Quoted 5 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +5
-0+ +kunit_info (test_data->test,"end reader: read=%lu seq=%llu\n" , count, info.seq); + +return 0 ; +}
[...]
Quoted 18 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +18
-0+static void test_readerwriter (struct kunit *test) +{ +/* Equivalent to CONFIG_LOG_BUF_SHIFT=13 */ +DEFINE_PRINTKRB (test_rb,8 ,5 ); + +struct prbtest_thread_data *thread_data; +struct prbtest_data *test_data; +struct task_struct *thread; +cpumask_t test_cpus; +int cpu, reader_cpu; + +cpus_read_lock (); +/* +* Failure of KUNIT_ASSERT() kills the current task +* so it can not be called while the CPU hotplug lock is held. +* Instead use a snapshot of the online CPUs. +* If they change during test execution it is unfortunate but not a grave error. +*/
kernel/printk/printk_ringbuffer_kunit_test.c
Quoted 10 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +10
-0+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 ) +kunit_warn (test,"more than one CPU is recommended" ); +else +cpumask_clear_cpu (reader_cpu, &test_cpus); +
I was curious why the reinit (below) was needed. We did not need this for the default printk log buffer (printk_rb_static).
My understanding is that we need this because the unit test might be started more times. It might be worth a comment. Something like:
/* KUnit tests cat be re-started more times. */
Quoted 19 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +19
-0+prbtest_prb_reinit (&test_rb); + + test_data =kunit_kmalloc (test,sizeof (*test_data),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, test_data); + test_data->test = test; + test_data->ringbuffer = &test_rb; +init_waitqueue_head (&test_data->new_record_wait); + +kunit_info (test,"running for %lu ms\n" , runtime_ms); + +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; + thread_data->num = cpu; + + thread =kthread_run_on_cpu (prbtest_writer, thread_data, cpu, +"prbtest writer %u" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, thread);
kernel/printk/printk_ringbuffer_kunit_test.c
Quoted 10 lines
kernel/printk/printk_ringbuffer_kunit_test.c new file +10
-0+prbtest_add_kthread_cleanup (test, thread); + } + +kunit_info (test,"starting test\n" ); + +set_cpus_allowed_ptr (current,cpumask_of (reader_cpu)); +prbtest_reader (test_data, runtime_ms); + +kunit_info (test,"completed test\n" ); +}
Otherwise, it looks good to me.
I would push it with the timer API renames so that it works in Linus' master and linux-next out of box. So, with the following changes:
kernel/printk/printk_ringbuffer.c +1 -0
@@ -2337,6 +2337,7 @@ void prb_init(struct printk_ringbuffer *rb, infos[0 ].seq = -(u64 )_DESCS_COUNT (descbits); infos[_DESCS_COUNT (descbits) -1 ].seq =0 ; } +EXPORT_SYMBOL_IF_KUNIT (prb_init);/** * prb_record_text_space() - Query the full actual used ringbuffer space for
kernel/printk/printk_ringbuffer_kunit_test.c +3 -2
@@ -153,7 +153,7 @@ struct prbtest_wakeup_timer {static void prbtest_wakeup_callback (struct timer_list *timer) { -struct prbtest_wakeup_timer *wakeup =from_timer (wakeup, timer, timer); +struct prbtest_wakeup_timer *wakeup =timer_container_of (wakeup, timer, timer);set_tsk_thread_flag (wakeup->task,TIF_NOTIFY_SIGNAL );wake_up_process (wakeup->task);
@@ -194,7 +194,7 @@ static int prbtest_reader(struct prbtest_data *test_data, unsigned long timeout_ }timer_delete_sync (&wakeup.timer); -destroy_timer_on_stack (&wakeup.timer); +timer_destroy_on_stack (&wakeup.timer);kunit_info (test_data->test,"end reader: read=%lu seq=%llu\n" , count, info.seq);
@@ -245,6 +245,7 @@ static void test_readerwriter(struct kunit *test)else cpumask_clear_cpu (reader_cpu, &test_cpus); +/* KUnit test can get restarted more times. */ prbtest_prb_reinit (&test_rb); test_data =kunit_kmalloc (test,sizeof (*test_data),GFP_KERNEL );
Best Regards,
Petr
On Wed, Jun 11, 2025 at 04:02:30PM +0200, Petr Mladek wrote:
<snip>
Quoted 8 lines
Otherwise, it looks good to me.
I would push it with the timer API renames so that it works in Linus' master and linux-next out of box. So, with the following changes:
Reviewed-by Petr Mladek <[email protected]>Tested-by Petr Mladek <[email protected]>
Thanks.
Please hold off applying this patch for now, please.
With 6.16-rc1/GCC15 it runs into fortify-string errors for me. I'll fix these up, integrate your changes and tags and will resubmit it.
Thomas