2017-03-13 16:33:07

by Andrey Ryabinin

[permalink] [raw]
Subject: [PATCH] x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y

The kernel doesn't boot with both PROFILE_ANNOTATED_BRANCHES=y and KASAN=y
options selected. With branch profiling enabled we end up calling
ftrace_likely_update() before kasan_early_init(). ftrace_likely_update()
built with KASAN instrumentation, so calling it before kasan has been
initialized leads to crash.

Use DISABLE_BRANCH_PROFILING define to make sure that we don't call
ftrace_likely_update() from early code before kasan_early_init().

Fixes: ef7f0d6a6ca8 ("x86_64: add KASan support")
Reported-by: Fengguang Wu <[email protected]>
Signed-off-by: Andrey Ryabinin <[email protected]>
Cc: <[email protected]>
---
arch/x86/kernel/head64.c | 1 +
arch/x86/mm/kasan_init_64.c | 1 +
2 files changed, 2 insertions(+)

diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c
index 54a2372..b5785c1 100644
--- a/arch/x86/kernel/head64.c
+++ b/arch/x86/kernel/head64.c
@@ -4,6 +4,7 @@
* Copyright (C) 2000 Andrea Arcangeli <[email protected]> SuSE
*/

+#define DISABLE_BRANCH_PROFILING
#include <linux/init.h>
#include <linux/linkage.h>
#include <linux/types.h>
diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c
index 8d63d7a..4c90cfd 100644
--- a/arch/x86/mm/kasan_init_64.c
+++ b/arch/x86/mm/kasan_init_64.c
@@ -1,3 +1,4 @@
+#define DISABLE_BRANCH_PROFILING
#define pr_fmt(fmt) "kasan: " fmt
#include <linux/bootmem.h>
#include <linux/kasan.h>
--
2.10.2


Subject: [tip:x86/urgent] x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y

Commit-ID: be3606ff739d1c1be36389f8737c577ad87e1f57
Gitweb: http://git.kernel.org/tip/be3606ff739d1c1be36389f8737c577ad87e1f57
Author: Andrey Ryabinin <[email protected]>
AuthorDate: Mon, 13 Mar 2017 19:33:37 +0300
Committer: Thomas Gleixner <[email protected]>
CommitDate: Tue, 14 Mar 2017 00:00:55 +0100

x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y

The kernel doesn't boot with both PROFILE_ANNOTATED_BRANCHES=y and KASAN=y
options selected. With branch profiling enabled we end up calling
ftrace_likely_update() before kasan_early_init(). ftrace_likely_update() is
built with KASAN instrumentation, so calling it before kasan has been
initialized leads to crash.

Use DISABLE_BRANCH_PROFILING define to make sure that we don't call
ftrace_likely_update() from early code before kasan_early_init().

Fixes: ef7f0d6a6ca8 ("x86_64: add KASan support")
Reported-by: Fengguang Wu <[email protected]>
Signed-off-by: Andrey Ryabinin <[email protected]>
Cc: [email protected]
Cc: Alexander Potapenko <[email protected]>
Cc: [email protected]
Cc: Andrew Morton <[email protected]>
Cc: [email protected]
Cc: Dmitry Vyukov <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Thomas Gleixner <[email protected]>

---
arch/x86/kernel/head64.c | 1 +
arch/x86/mm/kasan_init_64.c | 1 +
2 files changed, 2 insertions(+)

diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c
index 54a2372..b5785c1 100644
--- a/arch/x86/kernel/head64.c
+++ b/arch/x86/kernel/head64.c
@@ -4,6 +4,7 @@
* Copyright (C) 2000 Andrea Arcangeli <[email protected]> SuSE
*/

+#define DISABLE_BRANCH_PROFILING
#include <linux/init.h>
#include <linux/linkage.h>
#include <linux/types.h>
diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c
index 8d63d7a..4c90cfd 100644
--- a/arch/x86/mm/kasan_init_64.c
+++ b/arch/x86/mm/kasan_init_64.c
@@ -1,3 +1,4 @@
+#define DISABLE_BRANCH_PROFILING
#define pr_fmt(fmt) "kasan: " fmt
#include <linux/bootmem.h>
#include <linux/kasan.h>

2017-03-14 14:10:02

by Alexander Potapenko

[permalink] [raw]
Subject: Re: [tip:x86/urgent] x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y

On Tue, Mar 14, 2017 at 12:03 AM, tip-bot for Andrey Ryabinin
<[email protected]> wrote:
> Commit-ID: be3606ff739d1c1be36389f8737c577ad87e1f57
> Gitweb: http://git.kernel.org/tip/be3606ff739d1c1be36389f8737c577ad87e1f57
> Author: Andrey Ryabinin <[email protected]>
> AuthorDate: Mon, 13 Mar 2017 19:33:37 +0300
> Committer: Thomas Gleixner <[email protected]>
> CommitDate: Tue, 14 Mar 2017 00:00:55 +0100
>
> x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y
>
> The kernel doesn't boot with both PROFILE_ANNOTATED_BRANCHES=y and KASAN=y
> options selected. With branch profiling enabled we end up calling
> ftrace_likely_update() before kasan_early_init(). ftrace_likely_update() is
> built with KASAN instrumentation, so calling it before kasan has been
> initialized leads to crash.
>
> Use DISABLE_BRANCH_PROFILING define to make sure that we don't call
> ftrace_likely_update() from early code before kasan_early_init().
Won't it suffice to disable instrumentation of ftrace_likely_update() and alike?
> Fixes: ef7f0d6a6ca8 ("x86_64: add KASan support")
> Reported-by: Fengguang Wu <[email protected]>
> Signed-off-by: Andrey Ryabinin <[email protected]>
> Cc: [email protected]
> Cc: Alexander Potapenko <[email protected]>
> Cc: [email protected]
> Cc: Andrew Morton <[email protected]>
> Cc: [email protected]
> Cc: Dmitry Vyukov <[email protected]>
> Link: http://lkml.kernel.org/r/[email protected]
> Signed-off-by: Thomas Gleixner <[email protected]>
>
> ---
> arch/x86/kernel/head64.c | 1 +
> arch/x86/mm/kasan_init_64.c | 1 +
> 2 files changed, 2 insertions(+)
>
> diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c
> index 54a2372..b5785c1 100644
> --- a/arch/x86/kernel/head64.c
> +++ b/arch/x86/kernel/head64.c
> @@ -4,6 +4,7 @@
> * Copyright (C) 2000 Andrea Arcangeli <[email protected]> SuSE
> */
>
> +#define DISABLE_BRANCH_PROFILING
> #include <linux/init.h>
> #include <linux/linkage.h>
> #include <linux/types.h>
> diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c
> index 8d63d7a..4c90cfd 100644
> --- a/arch/x86/mm/kasan_init_64.c
> +++ b/arch/x86/mm/kasan_init_64.c
> @@ -1,3 +1,4 @@
> +#define DISABLE_BRANCH_PROFILING
> #define pr_fmt(fmt) "kasan: " fmt
> #include <linux/bootmem.h>
> #include <linux/kasan.h>



--
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

2017-03-14 14:58:45

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [tip:x86/urgent] x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y

On 03/14/2017 05:09 PM, Alexander Potapenko wrote:
> On Tue, Mar 14, 2017 at 12:03 AM, tip-bot for Andrey Ryabinin
> <[email protected]> wrote:
>> Commit-ID: be3606ff739d1c1be36389f8737c577ad87e1f57
>> Gitweb: http://git.kernel.org/tip/be3606ff739d1c1be36389f8737c577ad87e1f57
>> Author: Andrey Ryabinin <[email protected]>
>> AuthorDate: Mon, 13 Mar 2017 19:33:37 +0300
>> Committer: Thomas Gleixner <[email protected]>
>> CommitDate: Tue, 14 Mar 2017 00:00:55 +0100
>>
>> x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y
>>
>> The kernel doesn't boot with both PROFILE_ANNOTATED_BRANCHES=y and KASAN=y
>> options selected. With branch profiling enabled we end up calling
>> ftrace_likely_update() before kasan_early_init(). ftrace_likely_update() is
>> built with KASAN instrumentation, so calling it before kasan has been
>> initialized leads to crash.
>>
>> Use DISABLE_BRANCH_PROFILING define to make sure that we don't call
>> ftrace_likely_update() from early code before kasan_early_init().
> Won't it suffice to disable instrumentation of ftrace_likely_update() and alike?

Not just ftrace_likely_update(), but also all functions that ftrace_likely_update() might call.
That would suffice, but it would be unreasonable way to fix this, given that we can just
ftrace_likely_update() call.
Plus we don't have any sane way to disable instrumentation per-function.

2017-03-14 15:00:01

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [tip:x86/urgent] x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y



On 03/14/2017 05:59 PM, Andrey Ryabinin wrote:
> On 03/14/2017 05:09 PM, Alexander Potapenko wrote:
>> On Tue, Mar 14, 2017 at 12:03 AM, tip-bot for Andrey Ryabinin
>> <[email protected]> wrote:
>>> Commit-ID: be3606ff739d1c1be36389f8737c577ad87e1f57
>>> Gitweb: http://git.kernel.org/tip/be3606ff739d1c1be36389f8737c577ad87e1f57
>>> Author: Andrey Ryabinin <[email protected]>
>>> AuthorDate: Mon, 13 Mar 2017 19:33:37 +0300
>>> Committer: Thomas Gleixner <[email protected]>
>>> CommitDate: Tue, 14 Mar 2017 00:00:55 +0100
>>>
>>> x86/kasan: Fix boot with KASAN=y and PROFILE_ANNOTATED_BRANCHES=y
>>>
>>> The kernel doesn't boot with both PROFILE_ANNOTATED_BRANCHES=y and KASAN=y
>>> options selected. With branch profiling enabled we end up calling
>>> ftrace_likely_update() before kasan_early_init(). ftrace_likely_update() is
>>> built with KASAN instrumentation, so calling it before kasan has been
>>> initialized leads to crash.
>>>
>>> Use DISABLE_BRANCH_PROFILING define to make sure that we don't call
>>> ftrace_likely_update() from early code before kasan_early_init().
>> Won't it suffice to disable instrumentation of ftrace_likely_update() and alike?
>
> Not just ftrace_likely_update(), but also all functions that ftrace_likely_update() might call.
> That would suffice, but it would be unreasonable way to fix this, given that we can just
> ftrace_likely_update() call.

^^avoid

> Plus we don't have any sane way to disable instrumentation per-function.
>