11// SPDX-License-Identifier: GPL-2.0
22
3+ #include <linux/jiffies.h>
34#include <linux/kernel.h>
45#include <linux/preempt.h>
56#include <linux/printk.h>
@@ -31,12 +32,99 @@ static struct {
3132 int num_stack_entries ;
3233} other_info = { .ptr = NULL };
3334
35+ /*
36+ * Information about reported data races; used to rate limit reporting.
37+ */
38+ struct report_time {
39+ /*
40+ * The last time the data race was reported.
41+ */
42+ unsigned long time ;
43+
44+ /*
45+ * The frames of the 2 threads; if only 1 thread is known, one frame
46+ * will be 0.
47+ */
48+ unsigned long frame1 ;
49+ unsigned long frame2 ;
50+ };
51+
52+ /*
53+ * Since we also want to be able to debug allocators with KCSAN, to avoid
54+ * deadlock, report_times cannot be dynamically resized with krealloc in
55+ * rate_limit_report.
56+ *
57+ * Therefore, we use a fixed-size array, which at most will occupy a page. This
58+ * still adequately rate limits reports, assuming that a) number of unique data
59+ * races is not excessive, and b) occurrence of unique data races within the
60+ * same time window is limited.
61+ */
62+ #define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time))
63+ #define REPORT_TIMES_SIZE \
64+ (CONFIG_KCSAN_REPORT_ONCE_IN_MS > REPORT_TIMES_MAX ? \
65+ REPORT_TIMES_MAX : \
66+ CONFIG_KCSAN_REPORT_ONCE_IN_MS)
67+ static struct report_time report_times [REPORT_TIMES_SIZE ];
68+
3469/*
3570 * This spinlock protects reporting and other_info, since other_info is usually
3671 * required when reporting.
3772 */
3873static DEFINE_SPINLOCK (report_lock );
3974
75+ /*
76+ * Checks if the data race identified by thread frames frame1 and frame2 has
77+ * been reported since (now - KCSAN_REPORT_ONCE_IN_MS).
78+ */
79+ static bool rate_limit_report (unsigned long frame1 , unsigned long frame2 )
80+ {
81+ struct report_time * use_entry = & report_times [0 ];
82+ unsigned long invalid_before ;
83+ int i ;
84+
85+ BUILD_BUG_ON (CONFIG_KCSAN_REPORT_ONCE_IN_MS != 0 && REPORT_TIMES_SIZE == 0 );
86+
87+ if (CONFIG_KCSAN_REPORT_ONCE_IN_MS == 0 )
88+ return false;
89+
90+ invalid_before = jiffies - msecs_to_jiffies (CONFIG_KCSAN_REPORT_ONCE_IN_MS );
91+
92+ /* Check if a matching data race report exists. */
93+ for (i = 0 ; i < REPORT_TIMES_SIZE ; ++ i ) {
94+ struct report_time * rt = & report_times [i ];
95+
96+ /*
97+ * Must always select an entry for use to store info as we
98+ * cannot resize report_times; at the end of the scan, use_entry
99+ * will be the oldest entry, which ideally also happened before
100+ * KCSAN_REPORT_ONCE_IN_MS ago.
101+ */
102+ if (time_before (rt -> time , use_entry -> time ))
103+ use_entry = rt ;
104+
105+ /*
106+ * Initially, no need to check any further as this entry as well
107+ * as following entries have never been used.
108+ */
109+ if (rt -> time == 0 )
110+ break ;
111+
112+ /* Check if entry expired. */
113+ if (time_before (rt -> time , invalid_before ))
114+ continue ; /* before KCSAN_REPORT_ONCE_IN_MS ago */
115+
116+ /* Reported recently, check if data race matches. */
117+ if ((rt -> frame1 == frame1 && rt -> frame2 == frame2 ) ||
118+ (rt -> frame1 == frame2 && rt -> frame2 == frame1 ))
119+ return true;
120+ }
121+
122+ use_entry -> time = jiffies ;
123+ use_entry -> frame1 = frame1 ;
124+ use_entry -> frame2 = frame2 ;
125+ return false;
126+ }
127+
40128/*
41129 * Special rules to skip reporting.
42130 */
@@ -132,7 +220,9 @@ static bool print_report(const volatile void *ptr, size_t size, int access_type,
132220 unsigned long stack_entries [NUM_STACK_ENTRIES ] = { 0 };
133221 int num_stack_entries = stack_trace_save (stack_entries , NUM_STACK_ENTRIES , 1 );
134222 int skipnr = get_stack_skipnr (stack_entries , num_stack_entries );
135- int other_skipnr ;
223+ unsigned long this_frame = stack_entries [skipnr ];
224+ unsigned long other_frame = 0 ;
225+ int other_skipnr = 0 ; /* silence uninit warnings */
136226
137227 /*
138228 * Must check report filter rules before starting to print.
@@ -143,34 +233,34 @@ static bool print_report(const volatile void *ptr, size_t size, int access_type,
143233 if (type == KCSAN_REPORT_RACE_SIGNAL ) {
144234 other_skipnr = get_stack_skipnr (other_info .stack_entries ,
145235 other_info .num_stack_entries );
236+ other_frame = other_info .stack_entries [other_skipnr ];
146237
147238 /* @value_change is only known for the other thread */
148- if (skip_report (other_info .access_type , value_change ,
149- other_info .stack_entries [other_skipnr ]))
239+ if (skip_report (other_info .access_type , value_change , other_frame ))
150240 return false;
151241 }
152242
243+ if (rate_limit_report (this_frame , other_frame ))
244+ return false;
245+
153246 /* Print report header. */
154247 pr_err ("==================================================================\n" );
155248 switch (type ) {
156249 case KCSAN_REPORT_RACE_SIGNAL : {
157- void * this_fn = (void * )stack_entries [skipnr ];
158- void * other_fn = (void * )other_info .stack_entries [other_skipnr ];
159250 int cmp ;
160251
161252 /*
162253 * Order functions lexographically for consistent bug titles.
163254 * Do not print offset of functions to keep title short.
164255 */
165- cmp = sym_strcmp (other_fn , this_fn );
256+ cmp = sym_strcmp (( void * ) other_frame , ( void * ) this_frame );
166257 pr_err ("BUG: KCSAN: data-race in %ps / %ps\n" ,
167- cmp < 0 ? other_fn : this_fn ,
168- cmp < 0 ? this_fn : other_fn );
258+ ( void * )( cmp < 0 ? other_frame : this_frame ) ,
259+ ( void * )( cmp < 0 ? this_frame : other_frame ) );
169260 } break ;
170261
171262 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN :
172- pr_err ("BUG: KCSAN: data-race in %pS\n" ,
173- (void * )stack_entries [skipnr ]);
263+ pr_err ("BUG: KCSAN: data-race in %pS\n" , (void * )this_frame );
174264 break ;
175265
176266 default :
0 commit comments