Commit 241944d1 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Linus Torvalds

kasan: clean up feature flags for HW_TAGS mode

 - Untie kasan_init_hw_tags() code from the default values of
   kasan_arg_mode and kasan_arg_stacktrace.

 - Move static_branch_enable(&kasan_flag_enabled) to the end of
   kasan_init_hw_tags_cpu().

 - Remove excessive comments in kasan_arg_mode switch.

 - Add new comments.

Link: https://lkml.kernel.org/r/76ebb340265be57a218564a497e1f52ff36a3879.1643047180.git.andreyknvl@google.comSigned-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Acked-by: default avatarMarco Elver <elver@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Collingbourne <pcc@google.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 1eeac51e
...@@ -42,16 +42,22 @@ static enum kasan_arg kasan_arg __ro_after_init; ...@@ -42,16 +42,22 @@ static enum kasan_arg kasan_arg __ro_after_init;
static enum kasan_arg_mode kasan_arg_mode __ro_after_init; static enum kasan_arg_mode kasan_arg_mode __ro_after_init;
static enum kasan_arg_stacktrace kasan_arg_stacktrace __initdata; static enum kasan_arg_stacktrace kasan_arg_stacktrace __initdata;
/* Whether KASAN is enabled at all. */ /*
* Whether KASAN is enabled at all.
* The value remains false until KASAN is initialized by kasan_init_hw_tags().
*/
DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled);
EXPORT_SYMBOL(kasan_flag_enabled); EXPORT_SYMBOL(kasan_flag_enabled);
/* Whether the selected mode is synchronous/asynchronous/asymmetric.*/ /*
* Whether the selected mode is synchronous, asynchronous, or asymmetric.
* Defaults to KASAN_MODE_SYNC.
*/
enum kasan_mode kasan_mode __ro_after_init; enum kasan_mode kasan_mode __ro_after_init;
EXPORT_SYMBOL_GPL(kasan_mode); EXPORT_SYMBOL_GPL(kasan_mode);
/* Whether to collect alloc/free stack traces. */ /* Whether to collect alloc/free stack traces. */
DEFINE_STATIC_KEY_FALSE(kasan_flag_stacktrace); DEFINE_STATIC_KEY_TRUE(kasan_flag_stacktrace);
/* kasan=off/on */ /* kasan=off/on */
static int __init early_kasan_flag(char *arg) static int __init early_kasan_flag(char *arg)
...@@ -127,7 +133,11 @@ void kasan_init_hw_tags_cpu(void) ...@@ -127,7 +133,11 @@ void kasan_init_hw_tags_cpu(void)
* as this function is only called for MTE-capable hardware. * as this function is only called for MTE-capable hardware.
*/ */
/* If KASAN is disabled via command line, don't initialize it. */ /*
* If KASAN is disabled via command line, don't initialize it.
* When this function is called, kasan_flag_enabled is not yet
* set by kasan_init_hw_tags(). Thus, check kasan_arg instead.
*/
if (kasan_arg == KASAN_ARG_OFF) if (kasan_arg == KASAN_ARG_OFF)
return; return;
...@@ -154,42 +164,36 @@ void __init kasan_init_hw_tags(void) ...@@ -154,42 +164,36 @@ void __init kasan_init_hw_tags(void)
if (kasan_arg == KASAN_ARG_OFF) if (kasan_arg == KASAN_ARG_OFF)
return; return;
/* Enable KASAN. */
static_branch_enable(&kasan_flag_enabled);
switch (kasan_arg_mode) { switch (kasan_arg_mode) {
case KASAN_ARG_MODE_DEFAULT: case KASAN_ARG_MODE_DEFAULT:
/* /* Default is specified by kasan_mode definition. */
* Default to sync mode. break;
*/
fallthrough;
case KASAN_ARG_MODE_SYNC: case KASAN_ARG_MODE_SYNC:
/* Sync mode enabled. */
kasan_mode = KASAN_MODE_SYNC; kasan_mode = KASAN_MODE_SYNC;
break; break;
case KASAN_ARG_MODE_ASYNC: case KASAN_ARG_MODE_ASYNC:
/* Async mode enabled. */
kasan_mode = KASAN_MODE_ASYNC; kasan_mode = KASAN_MODE_ASYNC;
break; break;
case KASAN_ARG_MODE_ASYMM: case KASAN_ARG_MODE_ASYMM:
/* Asymm mode enabled. */
kasan_mode = KASAN_MODE_ASYMM; kasan_mode = KASAN_MODE_ASYMM;
break; break;
} }
switch (kasan_arg_stacktrace) { switch (kasan_arg_stacktrace) {
case KASAN_ARG_STACKTRACE_DEFAULT: case KASAN_ARG_STACKTRACE_DEFAULT:
/* Default to enabling stack trace collection. */ /* Default is specified by kasan_flag_stacktrace definition. */
static_branch_enable(&kasan_flag_stacktrace);
break; break;
case KASAN_ARG_STACKTRACE_OFF: case KASAN_ARG_STACKTRACE_OFF:
/* Do nothing, kasan_flag_stacktrace keeps its default value. */ static_branch_disable(&kasan_flag_stacktrace);
break; break;
case KASAN_ARG_STACKTRACE_ON: case KASAN_ARG_STACKTRACE_ON:
static_branch_enable(&kasan_flag_stacktrace); static_branch_enable(&kasan_flag_stacktrace);
break; break;
} }
/* KASAN is now initialized, enable it. */
static_branch_enable(&kasan_flag_enabled);
pr_info("KernelAddressSanitizer initialized (hw-tags, mode=%s, stacktrace=%s)\n", pr_info("KernelAddressSanitizer initialized (hw-tags, mode=%s, stacktrace=%s)\n",
kasan_mode_info(), kasan_mode_info(),
kasan_stack_collection_enabled() ? "on" : "off"); kasan_stack_collection_enabled() ? "on" : "off");
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include <linux/static_key.h> #include <linux/static_key.h>
#include "../slab.h" #include "../slab.h"
DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace); DECLARE_STATIC_KEY_TRUE(kasan_flag_stacktrace);
enum kasan_mode { enum kasan_mode {
KASAN_MODE_SYNC, KASAN_MODE_SYNC,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment