kcsan: Cleanups, readability, and cosmetic improvements
Marco Elver
maintainercover kcsan: Cleanups, readability, and cosmetic improvements
Cleanups, readability, and cosmetic improvements for KCSAN.
Marco Elver (5):
kcsan: Simplify debugfs counter to name mapping
kcsan: Simplify constant string handling
kcsan: Remove debugfs test command
kcsan: Show message if enabled early
kcsan: Use pr_fmt for consistency
kernel/kcsan/core.c | 8 ++- kernel/kcsan/debugfs.c | 111 ++++++++-------------------------------- kernel/kcsan/report.c | 4 +- kernel/kcsan/selftest.c | 8 +-- 4 files changed, 33 insertions(+), 98 deletions(-)
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Marco Elver
maintainerPATCH 1/5 kcsan: Simplify debugfs counter to name mapping
Simplify counter ID to name mapping by using an array with designated inits. This way, we can turn a run-time BUG() into a compile-time static assertion failure if a counter name is missing.
No functional change intended.
--- kernel/kcsan/debugfs.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
kernel/kcsan/debugfs.c +13 -20
@@ -19,6 +19,18 @@* Statistics counters. */ static atomic_long_t counters[KCSAN_COUNTER_COUNT ]; +static const char *const counter_names[] = { + [KCSAN_COUNTER_USED_WATCHPOINTS ] ="used_watchpoints" , + [KCSAN_COUNTER_SETUP_WATCHPOINTS ] ="setup_watchpoints" , + [KCSAN_COUNTER_DATA_RACES ] ="data_races" , + [KCSAN_COUNTER_ASSERT_FAILURES ] ="assert_failures" , + [KCSAN_COUNTER_NO_CAPACITY ] ="no_capacity" , + [KCSAN_COUNTER_REPORT_RACES ] ="report_races" , + [KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN ] ="races_unknown_origin" , + [KCSAN_COUNTER_UNENCODABLE_ACCESSES ] ="unencodable_accesses" , + [KCSAN_COUNTER_ENCODING_FALSE_POSITIVES ] ="encoding_false_positives" , +}; +static_assert (ARRAY_SIZE (counter_names) ==KCSAN_COUNTER_COUNT );/* * Addresses for filtering functions from reporting. This list can be used as a
@@ -39,24 +51,6 @@ static struct { };static DEFINE_SPINLOCK (report_filterlist_lock); -static const char *counter_to_name (enum kcsan_counter_id id) -{ -switch (id) { -case KCSAN_COUNTER_USED_WATCHPOINTS :return "used_watchpoints" ; -case KCSAN_COUNTER_SETUP_WATCHPOINTS :return "setup_watchpoints" ; -case KCSAN_COUNTER_DATA_RACES :return "data_races" ; -case KCSAN_COUNTER_ASSERT_FAILURES :return "assert_failures" ; -case KCSAN_COUNTER_NO_CAPACITY :return "no_capacity" ; -case KCSAN_COUNTER_REPORT_RACES :return "report_races" ; -case KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN :return "races_unknown_origin" ; -case KCSAN_COUNTER_UNENCODABLE_ACCESSES :return "unencodable_accesses" ; -case KCSAN_COUNTER_ENCODING_FALSE_POSITIVES :return "encoding_false_positives" ; -case KCSAN_COUNTER_COUNT : -BUG (); - } -return NULL ; -} -void kcsan_counter_inc (enum kcsan_counter_id id) {atomic_long_inc (&counters[id]);
@@ -271,8 +265,7 @@ static int show_info(struct seq_file *file, void *v)/* show stats */ seq_printf (file,"enabled: %i\n" ,READ_ONCE (kcsan_enabled));for (i =0 ; i <KCSAN_COUNTER_COUNT ; ++i) -seq_printf (file,"%s: %ld\n" ,counter_to_name (i), -atomic_long_read (&counters[i])); +seq_printf (file,"%s: %ld\n" , counter_names[i],atomic_long_read (&counters[i]));/* show filter functions, and filter type */ spin_lock_irqsave (&report_filterlist_lock, flags);
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Marco Elver
maintainerPATCH 2/5 kcsan: Simplify constant string handling
In reply to Marco ElverSimplify checking prefixes and length calculation of constant strings. For the former, the kernel provides str_has_prefix(), and the latter we should just use strlen("..") because GCC and Clang have optimizations that optimize these into constants.
No functional change intended.
--- kernel/kcsan/debugfs.c | 8 ++++---- kernel/kcsan/report.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)
kernel/kcsan/debugfs.c +4 -4
@@ -300,16 +300,16 @@ debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t *oWRITE_ONCE (kcsan_enabled, true); }else if (!strcmp (arg,"off" )) {WRITE_ONCE (kcsan_enabled, false); - }else if (!strncmp (arg,"microbench=" ,sizeof ("microbench=" ) -1 )) { + }else if (str_has_prefix (arg,"microbench=" )) {unsigned long iters; -if (kstrtoul (&arg[sizeof ("microbench=" ) -1 ],0 , &iters)) +if (kstrtoul (&arg[strlen ("microbench=" )],0 , &iters))return -EINVAL ;microbenchmark (iters); - }else if (!strncmp (arg,"test=" ,sizeof ("test=" ) -1 )) { + }else if (str_has_prefix (arg,"test=" )) {unsigned long iters; -if (kstrtoul (&arg[sizeof ("test=" ) -1 ],0 , &iters)) +if (kstrtoul (&arg[strlen ("test=" )],0 , &iters))return -EINVAL ;test_thread (iters); }else if (!strcmp (arg,"whitelist" )) {
kernel/kcsan/report.c +2 -2
@@ -279,8 +279,8 @@ static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries cur =strnstr (buf,"kcsan_" , len);if (cur) { - cur +=sizeof ("kcsan_" ) -1 ; -if (strncmp (cur,"test" ,sizeof ("test" ) -1 )) + cur +=strlen ("kcsan_" ); +if (!str_has_prefix (cur,"test" ))continue ;/* KCSAN runtime function. */ /* KCSAN related test. */ }
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Marco Elver
maintainerPATCH 3/5 kcsan: Remove debugfs test command
In reply to Marco ElverRemove the debugfs test command, as it is no longer needed now that we have the KUnit+Torture based kcsan-test module. This is to avoid confusion around how KCSAN should be tested, as only the kcsan-test module is maintained.
--- kernel/kcsan/debugfs.c | 66 ------------------------------------------ 1 file changed, 66 deletions(-)
kernel/kcsan/debugfs.c +0 -66
@@ -98,66 +98,6 @@ static noinline void microbenchmark(unsigned long iters) current->kcsan_ctx = ctx_save; } -/* -* Simple test to create conflicting accesses. Write 'test=<iters>' to KCSAN's -* debugfs file from multiple tasks to generate real conflicts and show reports. -*/ -static long test_dummy; -static long test_flags; -static long test_scoped; -static noinline void test_thread(unsigned long iters) -{ -const long CHANGE_BITS =0xff00ff00ff00ff00L ; -const struct kcsan_ctx ctx_save = current->kcsan_ctx; -cycles_t cycles; - -/* We may have been called from an atomic region; reset context. */ -memset (¤t->kcsan_ctx,0 ,sizeof (current->kcsan_ctx)); - -pr_info ("KCSAN: %s begin | iters: %lu\n" , __func__, iters); -pr_info ("test_dummy@%px, test_flags@%px, test_scoped@%px,\n" , - &test_dummy, &test_flags, &test_scoped); - - cycles =get_cycles (); -while (iters--) { -/* These all should generate reports. */ -__kcsan_check_read (&test_dummy,sizeof (test_dummy)); -ASSERT_EXCLUSIVE_WRITER (test_dummy); -ASSERT_EXCLUSIVE_ACCESS (test_dummy); - -ASSERT_EXCLUSIVE_BITS (test_flags, ~CHANGE_BITS );/* no report */ -__kcsan_check_read (&test_flags,sizeof (test_flags));/* no report */ - -ASSERT_EXCLUSIVE_BITS (test_flags,CHANGE_BITS );/* report */ -__kcsan_check_read (&test_flags,sizeof (test_flags));/* no report */ - -/* not actually instrumented */ -WRITE_ONCE (test_dummy, iters);/* to observe value-change */ -__kcsan_check_write (&test_dummy,sizeof (test_dummy)); - - test_flags ^=CHANGE_BITS ;/* generate value-change */ -__kcsan_check_write (&test_flags,sizeof (test_flags)); - -BUG_ON (current->kcsan_ctx.scoped_accesses.prev); - { -/* Should generate reports anywhere in this block. */ -ASSERT_EXCLUSIVE_WRITER_SCOPED (test_scoped); -ASSERT_EXCLUSIVE_ACCESS_SCOPED (test_scoped); -BUG_ON (!current->kcsan_ctx.scoped_accesses.prev); -/* Unrelated accesses. */ -__kcsan_check_access (&cycles,sizeof (cycles),0 ); -__kcsan_check_access (&cycles,sizeof (cycles),KCSAN_ACCESS_ATOMIC ); - } -BUG_ON (current->kcsan_ctx.scoped_accesses.prev); - } - cycles =get_cycles () - cycles; - -pr_info ("KCSAN: %s end | cycles: %llu\n" , __func__, cycles); - -/* restore context */ - current->kcsan_ctx = ctx_save; -} -static int cmp_filterlist_addrs (const void *rhs,const void *lhs) {const unsigned long a = *(const unsigned long *)rhs;
@@ -306,12 +246,6 @@ debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t *oif (kstrtoul (&arg[strlen ("microbench=" )],0 , &iters))return -EINVAL ;microbenchmark (iters); - }else if (str_has_prefix (arg,"test=" )) { -unsigned long iters; - -if (kstrtoul (&arg[strlen ("test=" )],0 , &iters)) -return -EINVAL ; -test_thread (iters); }else if (!strcmp (arg,"whitelist" )) {set_report_filterlist_whitelist (true); }else if (!strcmp (arg,"blacklist" )) {
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Marco Elver
maintainerPATCH 4/5 kcsan: Show message if enabled early
In reply to Marco ElverShow a message in the kernel log if KCSAN was enabled early.
--- kernel/kcsan/core.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/kcsan/core.c +6 -2
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt (fmt) "kcsan: " fmt +#include <linux/atomic.h> #include <linux/bug.h> #include <linux/delay.h>
@@ -442,7 +444,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)if (IS_ENABLED (CONFIG_KCSAN_DEBUG )) {kcsan_disable_current (); -pr_err ("KCSAN: watching %s, size: %zu, addr: %px [slot: %d, encoded: %lx]\n" , +pr_err ("watching %s, size: %zu, addr: %px [slot: %d, encoded: %lx]\n" , is_write ?"write" :"read" , size, ptr,watchpoint_slot ((unsigned long )ptr),encode_watchpoint ((unsigned long )ptr, size, is_write));
@@ -601,8 +603,10 @@ void __init kcsan_init(void)* We are in the init task, and no other tasks should be running; * WRITE_ONCE without memory barrier is sufficient. */ -if (kcsan_early_enable) +if (kcsan_early_enable) { +pr_info ("enabled early\n" );WRITE_ONCE (kcsan_enabled, true); + } }/* === Exported interface =================================================== */
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Marco Elver
maintainerPATCH 5/5 kcsan: Use pr_fmt for consistency
In reply to Marco ElverUse the same pr_fmt throughout for consistency. [ The only exception is report.c, where the format must be kept precisely as-is. ]
--- kernel/kcsan/debugfs.c | 8 +++++--- kernel/kcsan/selftest.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-)
kernel/kcsan/debugfs.c +5 -3
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt (fmt) "kcsan: " fmt +#include <linux/atomic.h> #include <linux/bsearch.h> #include <linux/bug.h>
@@ -80,7 +82,7 @@ static noinline void microbenchmark(unsigned long iters)*/ WRITE_ONCE (kcsan_enabled, false); -pr_info ("KCSAN: %s begin | iters: %lu\n" , __func__, iters); +pr_info ("%s begin | iters: %lu\n" , __func__, iters); cycles =get_cycles ();while (iters--) {
@@ -91,7 +93,7 @@ static noinline void microbenchmark(unsigned long iters) } cycles =get_cycles () - cycles; -pr_info ("KCSAN: %s end | cycles: %llu\n" , __func__, cycles); +pr_info ("%s end | cycles: %llu\n" , __func__, cycles);WRITE_ONCE (kcsan_enabled, was_enabled);/* restore context */
@@ -154,7 +156,7 @@ static ssize_t insert_report_filterlist(const char *func)ssize_t ret =0 ;if (!addr) { -pr_err ("KCSAN: could not find function: '%s'\n" , func); +pr_err ("could not find function: '%s'\n" , func);return -ENOENT ; }
kernel/kcsan/selftest.c +5 -3
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt (fmt) "kcsan: " fmt +#include <linux/init.h> #include <linux/kernel.h> #include <linux/printk.h>
@@ -116,16 +118,16 @@ static int __init kcsan_selftest(void)if (do_test ()) \ ++passed; \else \ - pr_err("KCSAN selftest: " #do_test " failed"); \ + pr_err("selftest: " #do_test " failed"); \ }while (0 )RUN_TEST (test_requires);RUN_TEST (test_encode_decode);RUN_TEST (test_matching_access); -pr_info ("KCSAN selftest: %d/%d tests passed\n" , passed, total); +pr_info ("selftest: %d/%d tests passed\n" , passed, total);if (passed != total) -panic ("KCSAN selftests failed" ); +panic ("selftests failed" );return 0 ; }postcore_initcall (kcsan_selftest);
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Paul E. McKenney
AppliedQuoted 8 lines · Marco Elver
Cleanups, readability, and cosmetic improvements for KCSAN.
Marco Elver (5):
kcsan: Simplify debugfs counter to name mapping
kcsan: Simplify constant string handling
kcsan: Remove debugfs test command
kcsan: Show message if enabled early
kcsan: Usepr_fmtfor consistency
Queued and pushed, thank you!
Thanx, Paul
Quoted 9 lines
kernel/kcsan/core.c | 8 ++- kernel/kcsan/debugfs.c | 111 ++++++++-------------------------------- kernel/kcsan/report.c | 4 +- kernel/kcsan/selftest.c | 8 +-- 4 files changed, 33 insertions(+), 98 deletions(-)Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog