Skip to contentSkip to conversation

scanf: convert self-test to KUnit

PATCH Superseded Tamir Duberstein 3 participants 10 messages Jan 31, 2025 – Feb 3, 2025

Subsystems (4)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scanf: convert self-test to KUnit

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't rename the file in accordance with usual kunit conventions nor does it refactor the tests into proper parameterized tests - it's all one big test case.

---
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.

I tested this using:

$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 scanf
---
 arch/m68k/configs/amiga_defconfig    |   1 -
 arch/m68k/configs/apollo_defconfig   |   1 -
 arch/m68k/configs/atari_defconfig    |   1 -
 arch/m68k/configs/bvme6000_defconfig |   1 -
 arch/m68k/configs/hp300_defconfig    |   1 -
 arch/m68k/configs/mac_defconfig      |   1 -
 arch/m68k/configs/multi_defconfig    |   1 -
 arch/m68k/configs/mvme147_defconfig  |   1 -
 arch/m68k/configs/mvme16x_defconfig  |   1 -
 arch/m68k/configs/q40_defconfig      |   1 -
 arch/m68k/configs/sun3_defconfig     |   1 -
 arch/m68k/configs/sun3x_defconfig    |   1 -
 arch/powerpc/configs/ppc64_defconfig |   1 -
 lib/Kconfig.debug                    |  20 +-
 lib/Makefile                         |   2 +-
 lib/test_scanf.c                     | 704 +++++++++++++++++------------------
 tools/testing/selftests/lib/Makefile |   2 +-
 tools/testing/selftests/lib/config   |   1 -
 tools/testing/selftests/lib/scanf.sh |   4 -
 19 files changed, 358 insertions(+), 388 deletions(-)
arch/m68k/configs/amiga_defconfig +0 -1
index dbf2ea561c85..e85a4b3a9847 100644
@@ -623,7 +623,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/apollo_defconfig +0 -1
index b0fd199cc0a4..232b885cc26e 100644
@@ -580,7 +580,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/atari_defconfig +0 -1
index bb5b2d3b6c10..228af5b4cd42 100644
@@ -600,7 +600,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/bvme6000_defconfig +0 -1
index 8315a13bab73..cbc59aed3d97 100644
@@ -572,7 +572,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/hp300_defconfig +0 -1
index 350370657e5f..8d3787b8fe6d 100644
@@ -582,7 +582,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/mac_defconfig +0 -1
index f942b4755702..a68893d83361 100644
@@ -599,7 +599,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/multi_defconfig +0 -1
index b1eaad02efab..9a17526cefd2 100644
@@ -686,7 +686,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/mvme147_defconfig +0 -1
index 6309a4442bb3..5cf2e2860044 100644
@@ -572,7 +572,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/mvme16x_defconfig +0 -1
index 3feb0731f814..eb395749bc75 100644
@@ -573,7 +573,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/q40_defconfig +0 -1
index ea04b1b0da7d..8891f12c2a18 100644
@@ -589,7 +589,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/sun3_defconfig +0 -1
index f52d9af92153..a49ff5330e5b 100644
@@ -569,7 +569,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/m68k/configs/sun3x_defconfig +0 -1
index f348447824da..3022ff492136 100644
@@ -570,7 +570,6 @@ CONFIG_ASYNC_RAID6_TEST=m
 CONFIG_TEST_HEXDUMP=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
arch/powerpc/configs/ppc64_defconfig +0 -1
index 465eb96c755e..819387cbbe69 100644
@@ -445,7 +445,6 @@ CONFIG_STRING_SELFTEST=m
 CONFIG_TEST_STRING_HELPERS=m
 CONFIG_TEST_KSTRTOX=m
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_TEST_UUID=m
 CONFIG_TEST_XARRAY=m
lib/Kconfig.debug +17 -3
index 1af972a92d06..716c4ef857bb 100644
@@ -2427,6 +2427,23 @@ config ASYNC_RAID6_TEST
 config TEST_HEXDUMP
 	tristate "Test functions located in the hexdump module at runtime"
 
+config SCANF_KUNIT_TEST
+	tristate "KUnit test scanf() family of functions at runtime" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Enable this option to test the scanf functions at boot.
+
+	  KUnit tests run during boot and output the results to the debug log
+	  in TAP format (http://testanything.org/). Only useful for kernel devs
+	  running the KUnit test harness, and not intended for inclusion into a
+	  production build.
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config STRING_KUNIT_TEST
 	tristate "KUnit test string functions at runtime" if !KUNIT_ALL_TESTS
 	depends on KUNIT
@@ -2443,9 +2460,6 @@ config TEST_KSTRTOX
 config TEST_PRINTF
 	tristate "Test printf() family of functions at runtime"
 
-config TEST_SCANF
-	tristate "Test scanf() family of functions at runtime"
-
 config TEST_BITMAP
 	tristate "Test bitmap_*() family of functions at runtime"
 	help
lib/Makefile +1 -1
index d5cfc7afbbb8..8f1cf22bd83b 100644
@@ -52,6 +52,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
 	 generic-radix-tree.o bitmap-str.o
+obj-$(CONFIG_SCANF_KUNIT_TEST) += test_scanf.o
 obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o
 obj-y += string_helpers.o
 obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o
@@ -85,7 +86,6 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
-obj-$(CONFIG_TEST_SCANF) += test_scanf.o
 
 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 ifeq ($(CONFIG_CC_IS_CLANG)$(CONFIG_KASAN),yy)
lib/test_scanf.c +339 -365

1,042 lines not on this page · raw message

tools/testing/selftests/lib/Makefile +1 -1
index c52fe3ad8e98..4afda556151f 100644
@@ -4,5 +4,5 @@
 # No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
 all:
 
-TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh scanf.sh
+TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh
 include ../lib.mk
tools/testing/selftests/lib/config +0 -1
index dc15aba8d0a3..80b55a504868 100644
@@ -1,5 +1,4 @@
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_PRIME_NUMBERS=m
 CONFIG_TEST_BITOPS=m
tools/testing/selftests/lib/scanf.sh deleted +0 -4
deleted file mode 100755
index b59b8ba561c3..000000000000
@@ -1,4 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-# Tests the scanf infrastructure using test_scanf kernel module.
-$(dirname $0)/../kselftest/module.sh "scanf" test_scanf

---
base-commit: 18fe18c99c8d90d77c7ed6b42f48fa5b4e5c99c3
change-id: 20250131-scanf-kunit-convert-f70dc33bb34c

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw
Quoted 6 lines · Tamir Duberstein

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't rename the file in accordance with usual kunit conventions nor does it refactor the tests into proper parameterized tests - it's all one big test case.

Rename of the file may be done, but you need to use -M -C when formatting patch, in such a case the diff will be moderate in side and easy to review.

P.S. The test modules in defconfig is something which puzzling me. I would remove all of them at once, if somebody wants debug configuration, they would specify it and use with help of merge_config or alike.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 6 lines · Tamir Duberstein

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't rename the file in accordance with usual kunit conventions nor does it refactor the tests into proper parameterized tests - it's all one big test case.

Rename of the file may be done, but you need to use -M -C when formatting patch, in such a case the diff will be moderate in side and easy to review.

If you prefer that I rename the file, I can do so in v2.

Can you explain what you mean by using -M -C? The formatting was done by hand, is there an automated tool? I tried using clang-format but the result was a change on almost every line.

Note also that though it looks like a lot of formatting has changed, in reality almost every changed line has non-formatting changes due to passing test around.

Quoted 3 lines

P.S. The test modules in defconfig is something which puzzling me. I would remove all of them at once, if somebody wants debug configuration, they would specify it and use with help of merge_config or alike.

I'm not sure what you mean? I removed instances of CONFIG_TEST_SCANF from defconfig because that option no longer exists.

Cheers.

Tamir

#3 lore raw
Quoted 17 lines · Tamir Duberstein
Quoted 11 lines · Andy Shevchenko
Quoted 6 lines · Tamir Duberstein

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't rename the file in accordance with usual kunit conventions nor does it refactor the tests into proper parameterized tests - it's all one big test case.

Rename of the file may be done, but you need to use -M -C when formatting patch, in such a case the diff will be moderate in side and easy to review.

If you prefer that I rename the file, I can do so in v2.

Quoted 7 lines

Can you explain what you mean by using -M -C? The formatting was done by hand, is there an automated tool? I tried using clang-format but the result was a change on almost every line.

Note also that though it looks like a lot of formatting has changed, in reality almost every changed line has non-formatting changes due to passing test around.

Ah, you were referring to git format-patch. Sure, if you'd like me to rename the file, I'll use those flags in v2.

Quoted 10 lines
Quoted 3 lines

P.S. The test modules in defconfig is something which puzzling me. I would remove all of them at once, if somebody wants debug configuration, they would specify it and use with help of merge_config or alike.

I'm not sure what you mean? I removed instances of CONFIG_TEST_SCANF from defconfig because that option no longer exists.

Cheers.

Tamir

#4 lore raw
Quoted 3 lines · Tamir Duberstein
Quoted 2 lines · Tamir Duberstein

On Fri, Jan 31, 2025 at 12:11 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 10 lines
Quoted 7 lines

Can you explain what you mean by using -M -C? The formatting was done by hand, is there an automated tool? I tried using clang-format but the result was a change on almost every line.

Note also that though it looks like a lot of formatting has changed, in reality almost every changed line has non-formatting changes due to passing test around.

Ah, you were referring to git format-patch. Sure, if you'd like me to rename the file, I'll use those flags in v2.

Yes.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Tamir Duberstein
Quoted 10 lines · Tamir Duberstein
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Tamir Duberstein

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't rename the file in accordance with usual kunit conventions nor does it refactor the tests into proper parameterized tests - it's all one big test case.

Quoted 4 lines
Quoted 2 lines

Rename of the file may be done, but you need to use -M -C when formatting patch, in such a case the diff will be moderate in side and easy to review.

If you prefer that I rename the file, I can do so in v2.

Yes, please.

Quoted 1 line

Can you explain what you mean by using -M -C?

Parameters to git format-patch.

Quoted 8 lines

The formatting was done
by hand, is there an automated tool? I tried using clang-format but the result was a change on almost every line.

Note also that though it looks like a lot of formatting has changed, in reality almost every changed line has non-formatting changes due to passing test around.

Quoted 6 lines
Quoted 3 lines

P.S. The test modules in defconfig is something which puzzling me. I would remove all of them at once, if somebody wants debug configuration, they would specify it and use with help of merge_config or alike.

I'm not sure what you mean? I removed instances of CONFIG_TEST_SCANF from defconfig because that option no longer exists.

There are other options like CONFIG_TEST_PRINTF or so in defconfig files. Why are they there to begin with? The answer to this Q will affect the change you have done in this patch.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Tamir Duberstein
Quoted 1 line · Andy Shevchenko

On Fri, Jan 31, 2025 at 10:47:49AM -0500, Tamir Duberstein wrote:

[...]

Quoted 3 lines

There are other options like CONFIG_TEST_PRINTF or so in defconfig files. Why are they there to begin with? The answer to this Q will affect the change you have done in this patch.

CONFIG_TEST_PRINTF still exists; that test has not been converted to kunit.

#7 lore raw
Quoted 9 lines · Tamir Duberstein
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Tamir Duberstein
Quoted 1 line · Andy Shevchenko

On Fri, Jan 31, 2025 at 10:47:49AM -0500, Tamir Duberstein wrote:

Quoted 7 lines

[...]

Quoted 3 lines

There are other options like CONFIG_TEST_PRINTF or so in defconfig files. Why are they there to begin with? The answer to this Q will affect the change you have done in this patch.

CONFIG_TEST_PRINTF still exists; that test has not been converted to kunit.

Ah, sorry, I had misunderstood your question. These were there to enable those tests; when the tests are ported to KUnit this is no longer necessary because of the presence of CONFIG_KUNIT_ALL_TESTS.

#8 lore raw
In reply to Andy Shevchenko

Hi Andy,

Quoted 3 lines · Andy Shevchenko

P.S. The test modules in defconfig is something which puzzling me. I would remove all of them at once, if somebody wants debug configuration, they would specify it and use with help of merge_config or alike.

I really like having all tests available as modules. Fortunately all of these will be gone, once all tests have been converted to kunit, and have a "default KUNIT_ALL_TESTS" line...

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
maintainer Acked-by on v1 1/1
#9 lore raw
In reply to Tamir Duberstein
Quoted 14 lines · Tamir Duberstein

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't rename the file in accordance with usual kunit conventions nor does it refactor the tests into proper parameterized tests - it's all one big test case.

---
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.

Quoted 3 lines

I tested this using:

$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 scanf

Sounds way too complicated: "modprobe test_scanf" ;-)

Quoted 4 lines
 arch/m68k/configs/amiga_defconfig    |   1 -
 arch/m68k/configs/apollo_defconfig   |   1 -
 arch/m68k/configs/atari_defconfig    |   1 -
 arch/m68k/configs/bvme6000_defconfig |   1 -
Quoted 8 lines
 arch/m68k/configs/hp300_defconfig    |   1 -
 arch/m68k/configs/mac_defconfig      |   1 -
 arch/m68k/configs/multi_defconfig    |   1 -
 arch/m68k/configs/mvme147_defconfig  |   1 -
 arch/m68k/configs/mvme16x_defconfig  |   1 -
 arch/m68k/configs/q40_defconfig      |   1 -
 arch/m68k/configs/sun3_defconfig     |   1 -
 arch/m68k/configs/sun3x_defconfig    |   1 -

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds