Skip to contentSkip to conversation

printk: ringbuffer: Add KUnit test

PATCH In review Thomas Weißschuh 2 participants 2 messages Jun 20, 2025 – Jul 1, 2025 Printk

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 v4:
- Add comment about possible prb_reserve() failures
- Pick up tags from Petr
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebase onto v6.16-rc1
- Fix build for PRINTK_RINGBUFFER_KUNIT_TEST=m
- Add comment about reinitialization
- Avoid strnlen() in prbtest_check_data() to
- Avoid out-of-bound reads with CONFIG_FORTIFY_SOURCE
- Work correctly with so many CPUs that "text_id" will be '\0'
- Link to v2: https://lore.kernel.org/r/[email protected]

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                                 |  13 ++
 kernel/printk/.kunitconfig                   |   3 +
 kernel/printk/Makefile                       |   2 +
 kernel/printk/printk_ringbuffer.c            |   5 +
 kernel/printk/printk_ringbuffer_kunit_test.c | 308 +++++++++++++++++++++++++++
 5 files changed, 331 insertions(+)
init/Kconfig +13 -0
index af4c2f0854554bbcdf193852cf5c1d2c2accc64f..e3973d1576a3ea3aa62bf8fe7c449b6b49518ea7 100644
@@ -1667,6 +1667,19 @@ 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 KUNIT
+	depends on PRINTK
+	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
new file mode 100644
index 0000000000000000000000000000000000000000..f31458fd1a92fe24a61f6a6ebd1078b7ea2cd137
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_PRINTK=y
+CONFIG_PRINTK_RINGBUFFER_KUNIT_TEST=y
kernel/printk/Makefile +2 -0
index 39a2b61c7232e7b65d64eafd3ceadcf72cd32760..f8004ac3983da27a6218705fc5273bf3bf572045 100644
@@ -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 +5 -0
index d9fb053cff67dbdb8741ee3cac60f51ab986e499..bc811de18316bb8bcb59b96a272addd3c0d46e40 100644
@@ -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
@@ -2333,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 new file +308 -0

308 lines not on this page · raw message

---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20241011-printk-ringbuffer-test-f47f87c6a88b

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
maintainer
#1 lore raw
Quoted 9 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.

Quoted 5 lines

---
Changes in v4:
- Add comment about possible prb_reserve() failures
- Pick up tags from Petr
- Link to v3: https://lore.kernel.org/r/[email protected]

I have already pushed v3. And I would prefer to avoid a rebase. I am going to send a patch which will add the comment.

Best Regards,
Petr