Skip to content

Commit 19acd03

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Add __kcsan_{enable,disable}_current() variants
The __kcsan_{enable,disable}_current() variants only call into KCSAN if KCSAN is enabled for the current compilation unit. Note: This is typically not what we want, as we usually want to ensure that even calls into other functions still have KCSAN disabled. These variants may safely be used in header files that are shared between regular kernel code and code that does not link the KCSAN runtime. Signed-off-by: Marco Elver <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 5099a72 commit 19acd03

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

include/linux/kcsan-checks.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void kcsan_disable_current(void);
4949
* Supports nesting.
5050
*/
5151
void kcsan_enable_current(void);
52+
void kcsan_enable_current_nowarn(void); /* Safe in uaccess regions. */
5253

5354
/**
5455
* kcsan_nestable_atomic_begin - begin nestable atomic region
@@ -149,6 +150,7 @@ static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
149150

150151
static inline void kcsan_disable_current(void) { }
151152
static inline void kcsan_enable_current(void) { }
153+
static inline void kcsan_enable_current_nowarn(void) { }
152154
static inline void kcsan_nestable_atomic_begin(void) { }
153155
static inline void kcsan_nestable_atomic_end(void) { }
154156
static inline void kcsan_flat_atomic_begin(void) { }
@@ -165,15 +167,24 @@ static inline void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) { }
165167

166168
#endif /* CONFIG_KCSAN */
167169

170+
#ifdef __SANITIZE_THREAD__
168171
/*
169-
* kcsan_*: Only calls into the runtime when the particular compilation unit has
170-
* KCSAN instrumentation enabled. May be used in header files.
172+
* Only calls into the runtime when the particular compilation unit has KCSAN
173+
* instrumentation enabled. May be used in header files.
171174
*/
172-
#ifdef __SANITIZE_THREAD__
173175
#define kcsan_check_access __kcsan_check_access
176+
177+
/*
178+
* Only use these to disable KCSAN for accesses in the current compilation unit;
179+
* calls into libraries may still perform KCSAN checks.
180+
*/
181+
#define __kcsan_disable_current kcsan_disable_current
182+
#define __kcsan_enable_current kcsan_enable_current_nowarn
174183
#else
175184
static inline void kcsan_check_access(const volatile void *ptr, size_t size,
176185
int type) { }
186+
static inline void __kcsan_enable_current(void) { }
187+
static inline void __kcsan_disable_current(void) { }
177188
#endif
178189

179190
/**

kernel/kcsan/core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,13 @@ void kcsan_enable_current(void)
625625
}
626626
EXPORT_SYMBOL(kcsan_enable_current);
627627

628+
void kcsan_enable_current_nowarn(void)
629+
{
630+
if (get_ctx()->disable_count-- == 0)
631+
kcsan_disable_current();
632+
}
633+
EXPORT_SYMBOL(kcsan_enable_current_nowarn);
634+
628635
void kcsan_nestable_atomic_begin(void)
629636
{
630637
/*

0 commit comments

Comments
 (0)