kcsan: Optimize debugfs stats counters
Marco Elver
maintainerPATCH 1/1 kcsan: Optimize debugfs stats counters
Remove kcsan_counter_inc/dec() functions, as they perform no other logic, and are no longer needed.
This avoids several calls in kcsan_setup_watchpoint() and kcsan_found_watchpoint(), as well as lets the compiler warn us about potential out-of-bounds accesses as the array's size is known at all usage sites at compile-time.
--- kernel/kcsan/core.c | 22 +++++++++++----------- kernel/kcsan/debugfs.c | 21 +++++---------------- kernel/kcsan/kcsan.h | 12 ++++++------ kernel/kcsan/report.c | 2 +- 4 files changed, 23 insertions(+), 34 deletions(-)
kernel/kcsan/core.c +11 -11
@@ -351,13 +351,13 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,* already removed the watchpoint, or another thread consumed * the watchpoint before this thread. */ -kcsan_counter_inc (KCSAN_COUNTER_REPORT_RACES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_REPORT_RACES ]); }if ((type &KCSAN_ACCESS_ASSERT ) !=0 ) -kcsan_counter_inc (KCSAN_COUNTER_ASSERT_FAILURES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES ]);else -kcsan_counter_inc (KCSAN_COUNTER_DATA_RACES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_DATA_RACES ]);user_access_restore (flags); }
@@ -398,7 +398,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) goto out;if (!check_encodable ((unsigned long )ptr, size)) { -kcsan_counter_inc (KCSAN_COUNTER_UNENCODABLE_ACCESSES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_UNENCODABLE_ACCESSES ]); goto out; }
@@ -413,12 +413,12 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)* with which should_watch() returns true should be tweaked so * that this case happens very rarely. */ -kcsan_counter_inc (KCSAN_COUNTER_NO_CAPACITY ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_NO_CAPACITY ]); goto out_unlock; } -kcsan_counter_inc (KCSAN_COUNTER_SETUP_WATCHPOINTS ); -kcsan_counter_inc (KCSAN_COUNTER_USED_WATCHPOINTS ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_SETUP_WATCHPOINTS ]); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS ]);/* * Read the current value, to later check and infer a race if the data
@@ -520,16 +520,16 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)* increment this counter. */ if (is_assert && value_change ==KCSAN_VALUE_CHANGE_TRUE ) -kcsan_counter_inc (KCSAN_COUNTER_ASSERT_FAILURES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES ]);kcsan_report (ptr, size, type, value_change,KCSAN_REPORT_RACE_SIGNAL , watchpoint - watchpoints); }else if (value_change ==KCSAN_VALUE_CHANGE_TRUE ) {/* Inferring a race, since the value should not have changed. */ -kcsan_counter_inc (KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN ]);if (is_assert) -kcsan_counter_inc (KCSAN_COUNTER_ASSERT_FAILURES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES ]);if (IS_ENABLED (CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN ) || is_assert) kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_TRUE,
@@ -542,7 +542,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)* reused after this point. */ remove_watchpoint (watchpoint); -kcsan_counter_dec (KCSAN_COUNTER_USED_WATCHPOINTS ); +atomic_long_dec (&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS ]); out_unlock:if (!kcsan_interrupt_watcher)raw_local_irq_restore (irq_flags);
kernel/kcsan/debugfs.c +5 -16
@@ -17,10 +17,7 @@#include "kcsan.h" -/* -* Statistics counters. -*/ -static atomic_long_t counters[KCSAN_COUNTER_COUNT ]; +atomic_long_t kcsan_counters[KCSAN_COUNTER_COUNT ];static const char *const counter_names[] = { [KCSAN_COUNTER_USED_WATCHPOINTS ] ="used_watchpoints" , [KCSAN_COUNTER_SETUP_WATCHPOINTS ] ="setup_watchpoints" ,
@@ -53,16 +50,6 @@ static struct { };static DEFINE_SPINLOCK (report_filterlist_lock); -void kcsan_counter_inc (enum kcsan_counter_id id) -{ -atomic_long_inc (&counters[id]); -} - -void kcsan_counter_dec (enum kcsan_counter_id id) -{ -atomic_long_dec (&counters[id]); -} -/* * The microbenchmark allows benchmarking KCSAN core runtime only. To run * multiple threads, pipe 'microbench=<iters>' from multiple tasks into the
@@ -206,8 +193,10 @@ 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_names[i],atomic_long_read (&counters[i])); +for (i =0 ; i <KCSAN_COUNTER_COUNT ; ++i) { +seq_printf (file,"%s: %ld\n" , counter_names[i], +atomic_long_read (&kcsan_counters[i])); + }/* show filter functions, and filter type */ spin_lock_irqsave (&report_filterlist_lock, flags);
kernel/kcsan/kcsan.h +6 -6
@@ -8,6 +8,7 @@#ifndef _KERNEL_KCSAN_KCSAN_H#define _KERNEL_KCSAN_KCSAN_H +#include <linux/atomic.h> #include <linux/kcsan.h> /* The number of adjacent watchpoints to check. */
@@ -27,6 +28,10 @@ extern bool kcsan_enabled;*/ void kcsan_debugfs_init (void ); +/* +* Statistics counters displayed via debugfs; should only be modified in +* slow-paths. +*/ enum kcsan_counter_id {/* * Number of watchpoints currently in use.
@@ -79,12 +84,7 @@ enum kcsan_counter_id { KCSAN_COUNTER_COUNT, /* number of counters */ }; - -/* -* Increment/decrement counter with given id; avoid calling these in fast-path. -*/ -extern void kcsan_counter_inc (enum kcsan_counter_id id); -extern void kcsan_counter_dec (enum kcsan_counter_id id); +extern atomic_long_t kcsan_counters[KCSAN_COUNTER_COUNT ];/* * Returns true if data races in the function symbol that maps to func_addr
kernel/kcsan/report.c +1 -1
@@ -556,7 +556,7 @@ static bool prepare_report_consumer(unsigned long *flags,* If the actual accesses to not match, this was a false * positive due to watchpoint encoding. */ -kcsan_counter_inc (KCSAN_COUNTER_ENCODING_FALSE_POSITIVES ); +atomic_long_inc (&kcsan_counters[KCSAN_COUNTER_ENCODING_FALSE_POSITIVES ]); goto discard; }
Signature · 1 line
2.28.0.236.gb10cc79966-goog
Marco Elver
maintainerQuoted 8 lines · Marco Elver
Remove kcsan_counter_inc/dec() functions, as they perform no other logic, and are no longer needed.
This avoids several calls in
kcsan_setup_watchpoint()andkcsan_found_watchpoint(), as well as lets the compiler warn us about potential out-of-bounds accesses as the array's size is known at all usage sites at compile-time.
Quoted 7 lines
Signed-off-by Marco Elver <[email protected]>--- kernel/kcsan/core.c | 22 +++++++++++----------- kernel/kcsan/debugfs.c | 21 +++++---------------- kernel/kcsan/kcsan.h | 12 ++++++------ kernel/kcsan/report.c | 2 +- 4 files changed, 23 insertions(+), 34 deletions(-)
Hi Paul,
I think this one is good to apply. I do not expect conflicts with current -rcu.
Thanks,
-- Marco
Quoted 17 lines · Marco Elver
Quoted 8 lines · Marco Elver
Remove kcsan_counter_inc/dec() functions, as they perform no other logic, and are no longer needed.
This avoids several calls in
kcsan_setup_watchpoint()andkcsan_found_watchpoint(), as well as lets the compiler warn us about potential out-of-bounds accesses as the array's size is known at all usage sites at compile-time.Quoted 7 lines
Signed-off-by Marco Elver <[email protected]>--- kernel/kcsan/core.c | 22 +++++++++++----------- kernel/kcsan/debugfs.c | 21 +++++---------------- kernel/kcsan/kcsan.h | 12 ++++++------ kernel/kcsan/report.c | 2 +- 4 files changed, 23 insertions(+), 34 deletions(-)
Quoted 3 lines
Hi Paul,
I think this one is good to apply. I do not expect conflicts with current -rcu.
Applied and pushed, thank you! And as you say, no drama. ;-)
Thanx, Paul