Skip to content

Commit 5cbaefe

Browse files
committed
kcsan: Improve various small stylistic details
Tidy up a few bits: - Fix typos and grammar, improve wording. - Remove spurious newlines that are col80 warning artifacts where the resulting line-break is worse than the disease it's curing. - Use core kernel coding style to improve readability and reduce spurious code pattern variations. - Use better vertical alignment for structure definitions and initialization sequences. - Misc other small details. No change in functionality intended. Cc: linux-kernel@vger.kernel.org Cc: Marco Elver <elver@google.com> Cc: Paul E. McKenney <paulmck@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Paul E. McKenney <paulmck@kernel.org> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 8e1d58a commit 5cbaefe

15 files changed

Lines changed: 131 additions & 153 deletions

File tree

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ config X86
226226
select VIRT_TO_BUS
227227
select X86_FEATURE_NAMES if PROC_FS
228228
select PROC_PID_ARCH_STATUS if PROC_FS
229-
select HAVE_ARCH_KCSAN if X86_64
229+
select HAVE_ARCH_KCSAN if X86_64
230230

231231
config INSTRUCTION_DECODER
232232
def_bool y

‎include/linux/compiler-clang.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define KASAN_ABI_VERSION 5
1717

1818
#if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
19-
/* emulate gcc's __SANITIZE_ADDRESS__ flag */
19+
/* Emulate GCC's __SANITIZE_ADDRESS__ flag */
2020
#define __SANITIZE_ADDRESS__
2121
#define __no_sanitize_address \
2222
__attribute__((no_sanitize("address", "hwaddress")))

‎include/linux/compiler.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ unsigned long read_word_at_a_time(const void *addr)
313313
#include <linux/kcsan.h>
314314

315315
/*
316-
* data_race: macro to document that accesses in an expression may conflict with
316+
* data_race(): macro to document that accesses in an expression may conflict with
317317
* other concurrent accesses resulting in data races, but the resulting
318318
* behaviour is deemed safe regardless.
319319
*

‎include/linux/kcsan-checks.h‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
/*
99
* Access type modifiers.
1010
*/
11-
#define KCSAN_ACCESS_WRITE 0x1
11+
#define KCSAN_ACCESS_WRITE 0x1
1212
#define KCSAN_ACCESS_ATOMIC 0x2
1313

1414
/*
15-
* __kcsan_*: Always calls into runtime when KCSAN is enabled. This may be used
15+
* __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
1616
* even in compilation units that selectively disable KCSAN, but must use KCSAN
17-
* to validate access to an address. Never use these in header files!
17+
* to validate access to an address. Never use these in header files!
1818
*/
1919
#ifdef CONFIG_KCSAN
2020
/**
21-
* __kcsan_check_access - check generic access for data race
21+
* __kcsan_check_access - check generic access for data races
2222
*
2323
* @ptr address of access
2424
* @size size of access
@@ -32,7 +32,7 @@ static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
3232
#endif
3333

3434
/*
35-
* kcsan_*: Only calls into runtime when the particular compilation unit has
35+
* kcsan_*: Only calls into the runtime when the particular compilation unit has
3636
* KCSAN instrumentation enabled. May be used in header files.
3737
*/
3838
#ifdef __SANITIZE_THREAD__
@@ -77,16 +77,12 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size,
7777
kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE)
7878

7979
/*
80-
* Check for atomic accesses: if atomic access are not ignored, this simply
81-
* aliases to kcsan_check_access, otherwise becomes a no-op.
80+
* Check for atomic accesses: if atomic accesses are not ignored, this simply
81+
* aliases to kcsan_check_access(), otherwise becomes a no-op.
8282
*/
8383
#ifdef CONFIG_KCSAN_IGNORE_ATOMICS
84-
#define kcsan_check_atomic_read(...) \
85-
do { \
86-
} while (0)
87-
#define kcsan_check_atomic_write(...) \
88-
do { \
89-
} while (0)
84+
#define kcsan_check_atomic_read(...) do { } while (0)
85+
#define kcsan_check_atomic_write(...) do { } while (0)
9086
#else
9187
#define kcsan_check_atomic_read(ptr, size) \
9288
kcsan_check_access(ptr, size, KCSAN_ACCESS_ATOMIC)

‎include/linux/kcsan.h‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,14 @@ void kcsan_atomic_next(int n);
9494

9595
#else /* CONFIG_KCSAN */
9696

97-
static inline void kcsan_init(void) { }
98-
99-
static inline void kcsan_disable_current(void) { }
100-
101-
static inline void kcsan_enable_current(void) { }
102-
103-
static inline void kcsan_nestable_atomic_begin(void) { }
104-
105-
static inline void kcsan_nestable_atomic_end(void) { }
106-
107-
static inline void kcsan_flat_atomic_begin(void) { }
108-
109-
static inline void kcsan_flat_atomic_end(void) { }
110-
111-
static inline void kcsan_atomic_next(int n) { }
97+
static inline void kcsan_init(void) { }
98+
static inline void kcsan_disable_current(void) { }
99+
static inline void kcsan_enable_current(void) { }
100+
static inline void kcsan_nestable_atomic_begin(void) { }
101+
static inline void kcsan_nestable_atomic_end(void) { }
102+
static inline void kcsan_flat_atomic_begin(void) { }
103+
static inline void kcsan_flat_atomic_end(void) { }
104+
static inline void kcsan_atomic_next(int n) { }
112105

113106
#endif /* CONFIG_KCSAN */
114107

‎include/linux/seqlock.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*
4949
* As a consequence, we take the following best-effort approach for raw usage
5050
* via seqcount_t under KCSAN: upon beginning a seq-reader critical section,
51-
* pessimistically mark then next KCSAN_SEQLOCK_REGION_MAX memory accesses as
51+
* pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
5252
* atomics; if there is a matching read_seqcount_retry() call, no following
5353
* memory operations are considered atomic. Usage of seqlocks via seqlock_t
5454
* interface is not affected.
@@ -265,7 +265,7 @@ static inline void raw_write_seqcount_end(seqcount_t *s)
265265
* usual consistency guarantee. It is one wmb cheaper, because we can
266266
* collapse the two back-to-back wmb()s.
267267
*
268-
* Note that, writes surrounding the barrier should be declared atomic (e.g.
268+
* Note that writes surrounding the barrier should be declared atomic (e.g.
269269
* via WRITE_ONCE): a) to ensure the writes become visible to other threads
270270
* atomically, avoiding compiler optimizations; b) to document which writes are
271271
* meant to propagate to the reader critical section. This is necessary because
@@ -465,15 +465,15 @@ static inline unsigned read_seqbegin(const seqlock_t *sl)
465465
{
466466
unsigned ret = read_seqcount_begin(&sl->seqcount);
467467

468-
kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry */
468+
kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
469469
kcsan_flat_atomic_begin();
470470
return ret;
471471
}
472472

473473
static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
474474
{
475475
/*
476-
* Assume not nested: read_seqretry may be called multiple times when
476+
* Assume not nested: read_seqretry() may be called multiple times when
477477
* completing read critical section.
478478
*/
479479
kcsan_flat_atomic_end();

‎kernel/kcsan/atomic.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/jiffies.h>
77

88
/*
9-
* Helper that returns true if access to ptr should be considered as an atomic
9+
* Helper that returns true if access to @ptr should be considered an atomic
1010
* access, even though it is not explicitly atomic.
1111
*
1212
* List all volatile globals that have been observed in races, to suppress

‎kernel/kcsan/core.c‎

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ bool kcsan_enabled;
1919

2020
/* Per-CPU kcsan_ctx for interrupts */
2121
static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
22-
.disable_count = 0,
23-
.atomic_next = 0,
24-
.atomic_nest_count = 0,
25-
.in_flat_atomic = false,
22+
.disable_count = 0,
23+
.atomic_next = 0,
24+
.atomic_nest_count = 0,
25+
.in_flat_atomic = false,
2626
};
2727

2828
/*
@@ -50,11 +50,11 @@ static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
5050
* slot=9: [10, 11, 9]
5151
* slot=63: [64, 65, 63]
5252
*/
53-
#define NUM_SLOTS (1 + 2 * KCSAN_CHECK_ADJACENT)
53+
#define NUM_SLOTS (1 + 2*KCSAN_CHECK_ADJACENT)
5454
#define SLOT_IDX(slot, i) (slot + ((i + KCSAN_CHECK_ADJACENT) % NUM_SLOTS))
5555

5656
/*
57-
* SLOT_IDX_FAST is used in fast-path. Not first checking the address's primary
57+
* SLOT_IDX_FAST is used in the fast-path. Not first checking the address's primary
5858
* slot (middle) is fine if we assume that data races occur rarely. The set of
5959
* indices {SLOT_IDX(slot, i) | i in [0, NUM_SLOTS)} is equivalent to
6060
* {SLOT_IDX_FAST(slot, i) | i in [0, NUM_SLOTS)}.
@@ -68,17 +68,18 @@ static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
6868
* zero-initialized state matches INVALID_WATCHPOINT.
6969
*
7070
* Add NUM_SLOTS-1 entries to account for overflow; this helps avoid having to
71-
* use more complicated SLOT_IDX_FAST calculation with modulo in fast-path.
71+
* use more complicated SLOT_IDX_FAST calculation with modulo in the fast-path.
7272
*/
73-
static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS - 1];
73+
static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
7474

7575
/*
7676
* Instructions to skip watching counter, used in should_watch(). We use a
7777
* per-CPU counter to avoid excessive contention.
7878
*/
7979
static DEFINE_PER_CPU(long, kcsan_skip);
8080

81-
static inline atomic_long_t *find_watchpoint(unsigned long addr, size_t size,
81+
static inline atomic_long_t *find_watchpoint(unsigned long addr,
82+
size_t size,
8283
bool expect_write,
8384
long *encoded_watchpoint)
8485
{
@@ -110,8 +111,8 @@ static inline atomic_long_t *find_watchpoint(unsigned long addr, size_t size,
110111
return NULL;
111112
}
112113

113-
static inline atomic_long_t *insert_watchpoint(unsigned long addr, size_t size,
114-
bool is_write)
114+
static inline atomic_long_t *
115+
insert_watchpoint(unsigned long addr, size_t size, bool is_write)
115116
{
116117
const int slot = watchpoint_slot(addr);
117118
const long encoded_watchpoint = encode_watchpoint(addr, size, is_write);
@@ -120,21 +121,16 @@ static inline atomic_long_t *insert_watchpoint(unsigned long addr, size_t size,
120121

121122
/* Check slot index logic, ensuring we stay within array bounds. */
122123
BUILD_BUG_ON(SLOT_IDX(0, 0) != KCSAN_CHECK_ADJACENT);
123-
BUILD_BUG_ON(SLOT_IDX(0, KCSAN_CHECK_ADJACENT + 1) != 0);
124-
BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS - 1,
125-
KCSAN_CHECK_ADJACENT) !=
126-
ARRAY_SIZE(watchpoints) - 1);
127-
BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS - 1,
128-
KCSAN_CHECK_ADJACENT + 1) !=
129-
ARRAY_SIZE(watchpoints) - NUM_SLOTS);
124+
BUILD_BUG_ON(SLOT_IDX(0, KCSAN_CHECK_ADJACENT+1) != 0);
125+
BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT) != ARRAY_SIZE(watchpoints)-1);
126+
BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT+1) != ARRAY_SIZE(watchpoints) - NUM_SLOTS);
130127

131128
for (i = 0; i < NUM_SLOTS; ++i) {
132129
long expect_val = INVALID_WATCHPOINT;
133130

134131
/* Try to acquire this slot. */
135132
watchpoint = &watchpoints[SLOT_IDX(slot, i)];
136-
if (atomic_long_try_cmpxchg_relaxed(watchpoint, &expect_val,
137-
encoded_watchpoint))
133+
if (atomic_long_try_cmpxchg_relaxed(watchpoint, &expect_val, encoded_watchpoint))
138134
return watchpoint;
139135
}
140136

@@ -150,26 +146,24 @@ static inline atomic_long_t *insert_watchpoint(unsigned long addr, size_t size,
150146
* 2. the thread that set up the watchpoint already removed it;
151147
* 3. the watchpoint was removed and then re-used.
152148
*/
153-
static inline bool try_consume_watchpoint(atomic_long_t *watchpoint,
154-
long encoded_watchpoint)
149+
static inline bool
150+
try_consume_watchpoint(atomic_long_t *watchpoint, long encoded_watchpoint)
155151
{
156-
return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint,
157-
CONSUMED_WATCHPOINT);
152+
return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint, CONSUMED_WATCHPOINT);
158153
}
159154

160155
/*
161156
* Return true if watchpoint was not touched, false if consumed.
162157
*/
163158
static inline bool remove_watchpoint(atomic_long_t *watchpoint)
164159
{
165-
return atomic_long_xchg_relaxed(watchpoint, INVALID_WATCHPOINT) !=
166-
CONSUMED_WATCHPOINT;
160+
return atomic_long_xchg_relaxed(watchpoint, INVALID_WATCHPOINT) != CONSUMED_WATCHPOINT;
167161
}
168162

169163
static inline struct kcsan_ctx *get_ctx(void)
170164
{
171165
/*
172-
* In interrupt, use raw_cpu_ptr to avoid unnecessary checks, that would
166+
* In interrupts, use raw_cpu_ptr to avoid unnecessary checks, that would
173167
* also result in calls that generate warnings in uaccess regions.
174168
*/
175169
return in_task() ? &current->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx);
@@ -260,7 +254,8 @@ static inline unsigned int get_delay(void)
260254
*/
261255

262256
static noinline void kcsan_found_watchpoint(const volatile void *ptr,
263-
size_t size, bool is_write,
257+
size_t size,
258+
bool is_write,
264259
atomic_long_t *watchpoint,
265260
long encoded_watchpoint)
266261
{
@@ -296,8 +291,8 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
296291
user_access_restore(flags);
297292
}
298293

299-
static noinline void kcsan_setup_watchpoint(const volatile void *ptr,
300-
size_t size, bool is_write)
294+
static noinline void
295+
kcsan_setup_watchpoint(const volatile void *ptr, size_t size, bool is_write)
301296
{
302297
atomic_long_t *watchpoint;
303298
union {
@@ -346,8 +341,8 @@ static noinline void kcsan_setup_watchpoint(const volatile void *ptr,
346341
watchpoint = insert_watchpoint((unsigned long)ptr, size, is_write);
347342
if (watchpoint == NULL) {
348343
/*
349-
* Out of capacity: the size of `watchpoints`, and the frequency
350-
* with which `should_watch()` returns true should be tweaked so
344+
* Out of capacity: the size of 'watchpoints', and the frequency
345+
* with which should_watch() returns true should be tweaked so
351346
* that this case happens very rarely.
352347
*/
353348
kcsan_counter_inc(KCSAN_COUNTER_NO_CAPACITY);

0 commit comments

Comments
 (0)