| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_KASAN_ENABLED_H |
| 3 | #define _LINUX_KASAN_ENABLED_H |
| 4 | |
| 5 | #include <linux/static_key.h> |
| 6 | |
| 7 | #if defined(CONFIG_ARCH_DEFER_KASAN) || defined(CONFIG_KASAN_HW_TAGS) |
| 8 | /* |
| 9 | * Global runtime flag for KASAN modes that need runtime control. |
| 10 | * Used by ARCH_DEFER_KASAN architectures and HW_TAGS mode. |
| 11 | */ |
| 12 | DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); |
| 13 | |
| 14 | /* |
| 15 | * Runtime control for shadow memory initialization or HW_TAGS mode. |
| 16 | * Uses static key for architectures that need deferred KASAN or HW_TAGS. |
| 17 | */ |
| 18 | static __always_inline bool kasan_enabled(void) |
| 19 | { |
| 20 | return static_branch_likely(&kasan_flag_enabled); |
| 21 | } |
| 22 | |
| 23 | static inline void kasan_enable(void) |
| 24 | { |
| 25 | static_branch_enable(&kasan_flag_enabled); |
| 26 | } |
| 27 | #else |
| 28 | /* For architectures that can enable KASAN early, use compile-time check. */ |
| 29 | static __always_inline bool kasan_enabled(void) |
| 30 | { |
| 31 | return IS_ENABLED(CONFIG_KASAN); |
| 32 | } |
| 33 | |
| 34 | static inline void kasan_enable(void) {} |
| 35 | #endif /* CONFIG_ARCH_DEFER_KASAN || CONFIG_KASAN_HW_TAGS */ |
| 36 | |
| 37 | #ifdef CONFIG_KASAN_HW_TAGS |
| 38 | static inline bool kasan_hw_tags_enabled(void) |
| 39 | { |
| 40 | return kasan_enabled(); |
| 41 | } |
| 42 | #else |
| 43 | static inline bool kasan_hw_tags_enabled(void) |
| 44 | { |
| 45 | return false; |
| 46 | } |
| 47 | #endif /* CONFIG_KASAN_HW_TAGS */ |
| 48 | |
| 49 | #endif /* LINUX_KASAN_ENABLED_H */ |
| 50 | |