kunit: Move kunit_abort() call out of kunit_do_failed_assertion()
RFC PATCH 1/1 kunit: Move kunit_abort() call out of kunit_do_failed_assertion()
KUnit aborts the current thread when an assertion fails. Currently, this is done conditionally as part of the kunit_do_failed_assertion() function, but this hides the kunit_abort() call from the compiler (particularly if it's in another module). This, in turn, can lead to both suboptimal code generation (the compiler can't know if kunit_do_failed_assertion() will return), and to static analysis tools like smatch giving false positives.
Moving the kunit_abort() call into the macro should give the compiler and tools a better chance at understanding what's going on. Doing so requires exporting kunit_abort(), though it's recommended to continue to use assertions in lieu of aborting directly.
--- include/kunit/test.h | 4 ++++ lib/kunit/test.c | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-)
include/kunit/test.h +4 -0
@@ -481,6 +481,8 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);*/ #define KUNIT_SUCCEED (test) do {} while (0) +void __noreturn kunit_abort (struct kunit *test); + void kunit_do_failed_assertion(struct kunit *test, const struct kunit_loc *loc, enum kunit_assert_type type,
@@ -498,6 +500,8 @@ void kunit_do_failed_assertion(struct kunit *test, assert_format, \ fmt, \ ##__VA_ARGS__); \ +if (assert_type ==KUNIT_ASSERTION ) \ +kunit_abort (test); \ }while (0 )
lib/kunit/test.c +1 -4
@@ -310,7 +310,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,string_stream_destroy (stream); } -static void __noreturn kunit_abort (struct kunit *test) +void __noreturn kunit_abort (struct kunit *test) {kunit_try_catch_throw (&test->try_catch);/* Does not return. */
@@ -340,9 +340,6 @@ void kunit_do_failed_assertion(struct kunit *test,kunit_fail (test, loc, type, assert, assert_format, &message);va_end (args); - -if (type ==KUNIT_ASSERTION ) -kunit_abort (test); }EXPORT_SYMBOL_GPL (kunit_do_failed_assertion);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Quoted 8 lines · David Gow
KUnit aborts the current thread when an assertion fails. Currently, this is done conditionally as part of the
kunit_do_failed_assertion()function, but this hides thekunit_abort()call from the compiler (particularly if it's in another module). This, in turn, can lead to both suboptimal code generation (the compiler can't know ifkunit_do_failed_assertion()will return), and to static analysis tools like smatch giving false positives.
Quoted 7 lines
Moving the
kunit_abort()call into the macro should give the compiler and tools a better chance at understanding what's going on. Doing so requires exportingkunit_abort(), though it's recommended to continue to use assertions in lieu of aborting directly.Suggested-by Dan Carpenter <[email protected]>Signed-off-by David Gow <[email protected]>
Awesome thanks! I had already hardcoded the current behavior but I'm happy to delete that code. This works automatically and will continue to work if we change the parameters to kunit_do_failed_assertion() in the future.
regards,
dan carpenter
kernel test robot
build botkernel test robot noticed the following build errors:
Hi David,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on linus/master]
[also build test ERROR on v6.4-rc3 next-20230525]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/David-Gow/kunit-Move-kunit_abort-call-out-of-kunit_do_failed_assertion/20230526-155531 base: linus/master patch link: https://lore.kernel.org/r/20230526075355.586335-1-davidgow%40google.com patch subject: [RFC PATCH] kunit: Move kunit_abort() call out of kunit_do_failed_assertion() config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20230526/[email protected]/config) compiler: powerpc-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
mkdir -p ~/bin
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/136a31fc735d9d77702b9e00bf1b45378b78f36e
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review David-Gow/kunit-Move-kunit_abort-call-out-of-kunit_do_failed_assertion/20230526-155531 git checkout 136a31fc735d9d77702b9e00bf1b45378b78f36e
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 ~/bin/make.cross W=1 O=build_dir ARCH=powerpc olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 ~/bin/make.cross W=1 O=build_dir ARCH=powerpc SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| 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 <<):
>> ERROR: modpost: "kunit_abort" [kernel/time/time_test.ko] undefined! >> ERROR: modpost: "kunit_abort" [kernel/sysctl-test.ko] undefined! >> ERROR: modpost: "kunit_abort" [mm/kfence/kfence_test.ko] undefined! >> ERROR: modpost: "kunit_abort" [security/apparmor/apparmor_policy_unpack_test.ko] undefined! >> ERROR: modpost: "kunit_abort" [lib/kunit/kunit-test.ko] undefined! >> ERROR: modpost: "kunit_abort" [lib/test_list_sort.ko] undefined! >> ERROR: modpost: "kunit_abort" [lib/test_sort.ko] undefined! >> ERROR: modpost: "kunit_abort" [lib/bitfield_kunit.ko] undefined! >> ERROR: modpost: "kunit_abort" [lib/hashtable_test.ko] undefined! >> ERROR: modpost: "kunit_abort" [lib/memcpy_kunit.ko] undefined!
WARNING: modpost: suppressed 29 unresolved symbol warnings because there were too many)
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 9 lines · David Gow
KUnit aborts the current thread when an assertion fails. Currently, this is done conditionally as part of the
kunit_do_failed_assertion()function, but this hides thekunit_abort()call from the compiler (particularly if it's in another module). This, in turn, can lead to both suboptimal code generation (the compiler can't know ifkunit_do_failed_assertion()will return), and to static analysis tools like smatch giving false positives.
Quoted 4 lines
Moving the
kunit_abort()call into the macro should give the compiler and tools a better chance at understanding what's going on. Doing so requires exportingkunit_abort(), though it's recommended to continue to use assertions in lieu of aborting directly.
Should we rename it to __kunit_abort() to discourage that? That would match what we do with __kunit_test_suites_init().
Daniel
Quoted 8 lines · David Gow
KUnit aborts the current thread when an assertion fails. Currently, this is done conditionally as part of the
kunit_do_failed_assertion()function, but this hides thekunit_abort()call from the compiler (particularly if it's in another module). This, in turn, can lead to both suboptimal code generation (the compiler can't know ifkunit_do_failed_assertion()will return), and to static analysis tools like smatch giving false positives.
Another thought: this impacts
https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/kunit.rs.
They're currently calling kunit_do_failed_assert() always with type=ASSERTION.
This change would actually make things better since they could handle shutting down the thread themselves instead of having it happen behind an opaque FFI layer.
But we'd just need to make sure we get that code updated around when this change goes in.
Daniel