From: Daniel Axtens <hidden> Date: 2021-06-16 08:02:52
Building on the work of Christophe, Aneesh and Balbir, I've ported
KASAN to 64-bit Book3S kernels running on the Radix MMU. I've been
trying this for a while, but we keep having collisions between the
kasan code in the mm tree and the code I want to put in to the ppc
tree.
So this series just contains the kasan core changes that we
need. These can go in via the mm tree. I will then propose the powerpc
changes for a later cycle. (The most recent RFC for the powerpc
changes is in the last series at
https://lore.kernel.org/linux-mm/20210615014705.2234866-1-dja@axtens.net/
)
v13 applies to next-20210611. There should be no noticeable changes to
other platforms.
Changes since v12: respond to Marco's review comments - clean up the
help for ARCH_DISABLE_KASAN_INLINE, and add an arch readiness check to
the new granule poisioning function. Thanks Marco.
Kind regards,
Daniel
Daniel Axtens (3):
kasan: allow an architecture to disable inline instrumentation
kasan: allow architectures to provide an outline readiness check
kasan: define and use MAX_PTRS_PER_* for early shadow tables
include/linux/kasan.h | 18 +++++++++++++++---
lib/Kconfig.kasan | 14 ++++++++++++++
mm/kasan/common.c | 4 ++++
mm/kasan/generic.c | 3 +++
mm/kasan/init.c | 6 +++---
mm/kasan/kasan.h | 4 ++++
mm/kasan/shadow.c | 8 ++++++++
7 files changed, 51 insertions(+), 6 deletions(-)
--
2.30.2
From: Daniel Axtens <hidden> Date: 2021-06-16 08:02:58
For annoying architectural reasons, it's very difficult to support inline
instrumentation on powerpc64.*
Add a Kconfig flag to allow an arch to disable inline. (It's a bit
annoying to be 'backwards', but I'm not aware of any way to have
an arch force a symbol to be 'n', rather than 'y'.)
We also disable stack instrumentation in this case as it does things that
are functionally equivalent to inline instrumentation, namely adding
code that touches the shadow directly without going through a C helper.
* on ppc64 atm, the shadow lives in virtual memory and isn't accessible in
real mode. However, before we turn on virtual memory, we parse the device
tree to determine which platform and MMU we're running under. That calls
generic DT code, which is instrumented. Inline instrumentation in DT would
unconditionally attempt to touch the shadow region, which we won't have
set up yet, and would crash. We can make outline mode wait for the arch to
be ready, but we can't change what the compiler inserts for inline mode.
Signed-off-by: Daniel Axtens <redacted>
---
lib/Kconfig.kasan | 14 ++++++++++++++
1 file changed, 14 insertions(+)
From: Daniel Axtens <hidden> Date: 2021-06-16 08:03:04
Allow architectures to define a kasan_arch_is_ready() hook that bails
out of any function that's about to touch the shadow unless the arch
says that it is ready for the memory to be accessed. This is fairly
uninvasive and should have a negligible performance penalty.
This will only work in outline mode, so an arch must specify
ARCH_DISABLE_KASAN_INLINE if it requires this.
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: Aneesh Kumar K.V <redacted>
Suggested-by: Christophe Leroy <redacted>
Signed-off-by: Daniel Axtens <redacted>
--
I discuss the justfication for this later in the series. Also,
both previous RFCs for ppc64 - by 2 different people - have
needed this trick! See:
- https://lore.kernel.org/patchwork/patch/592820/ # ppc64 hash series
- https://patchwork.ozlabs.org/patch/795211/ # ppc radix series
---
mm/kasan/common.c | 4 ++++
mm/kasan/generic.c | 3 +++
mm/kasan/kasan.h | 4 ++++
mm/kasan/shadow.c | 8 ++++++++
4 files changed, 19 insertions(+)
From: Daniel Axtens <hidden> Date: 2021-06-16 08:03:11
powerpc has a variable number of PTRS_PER_*, set at runtime based
on the MMU that the kernel is booted under.
This means the PTRS_PER_* are no longer constants, and therefore
breaks the build.
Define default MAX_PTRS_PER_*s in the same style as MAX_PTRS_PER_P4D.
As KASAN is the only user at the moment, just define them in the kasan
header, and have them default to PTRS_PER_* unless overridden in arch
code.
Suggested-by: Christophe Leroy <redacted>
Suggested-by: Balbir Singh <bsingharora@gmail.com>
Reviewed-by: Christophe Leroy <redacted>
Reviewed-by: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Daniel Axtens <redacted>
---
include/linux/kasan.h | 18 +++++++++++++++---
mm/kasan/init.c | 6 +++---
2 files changed, 18 insertions(+), 6 deletions(-)
From: Marco Elver <elver@google.com> Date: 2021-06-16 08:56:28
On Wed, 16 Jun 2021 at 10:02, Daniel Axtens [off-list ref] wrote:
Allow architectures to define a kasan_arch_is_ready() hook that bails
out of any function that's about to touch the shadow unless the arch
says that it is ready for the memory to be accessed. This is fairly
uninvasive and should have a negligible performance penalty.
This will only work in outline mode, so an arch must specify
ARCH_DISABLE_KASAN_INLINE if it requires this.
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: Aneesh Kumar K.V <redacted>
Suggested-by: Christophe Leroy <redacted>
Signed-off-by: Daniel Axtens <redacted>
Reviewed-by: Marco Elver <elver@google.com>
but also check if an assertion that this is only used with
KASAN_GENERIC might make sense (below). Depends on how much we want to
make sure kasan_arch_is_ready() could be useful for other modes (which
I don't think it makes sense).
quoted hunk
--
I discuss the justfication for this later in the series. Also,
both previous RFCs for ppc64 - by 2 different people - have
needed this trick! See:
- https://lore.kernel.org/patchwork/patch/592820/ # ppc64 hash series
- https://patchwork.ozlabs.org/patch/795211/ # ppc radix series
---
mm/kasan/common.c | 4 ++++
mm/kasan/generic.c | 3 +++
mm/kasan/kasan.h | 4 ++++
mm/kasan/shadow.c | 8 ++++++++
4 files changed, 19 insertions(+)
I've been trying to think of a way to make it clear this is only for
KASAN_GENERIC mode, and not the others. An arch can always define this
function, but of course it might not be used. One way would be to add
an '#ifndef CONFIG_KASAN_GENERIC' in the #else case and #error if it's
not generic mode.
I think trying to make this do anything useful for SW_TAGS or HW_TAGS
modes does not make sense (at least right now).
quoted hunk
/*
* Exported functions for interfaces called from assembly or from generated
* code. Declarations here to avoid warning about missing declarations.
@@ -99,6 +103,10 @@ EXPORT_SYMBOL(kasan_poison);#ifdef CONFIG_KASAN_GENERICvoidkasan_poison_last_granule(constvoid*addr,size_tsize){+/* Don't touch the shadow memory if arch isn't ready */+if(!kasan_arch_is_ready())+return;+if(size&KASAN_GRANULE_MASK){u8*shadow=(u8*)kasan_mem_to_shadow(addr+size);*shadow=size&KASAN_GRANULE_MASK;--
2.30.2
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210616080244.51236-3-dja%40axtens.net.
This is introducing new global constants in a <linux/..> header. It
feels like this should be in <linux/pgtable.h> together with a
comment. Because <linux/kasan.h> is actually included in
<linux/slab.h>, most of the kernel will get these new definitions.
That in itself is fine, but it feels wrong that the KASAN header
introduces these.
Thoughts?
Sorry for only realizing this now.
Thanks,
-- Marco
2.30.2
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210616080244.51236-4-dja%40axtens.net.
From: Marco Elver <elver@google.com> Date: 2021-06-16 09:10:13
On Wed, 16 Jun 2021 at 10:02, Daniel Axtens [off-list ref] wrote:
For annoying architectural reasons, it's very difficult to support inline
instrumentation on powerpc64.*
Add a Kconfig flag to allow an arch to disable inline. (It's a bit
annoying to be 'backwards', but I'm not aware of any way to have
an arch force a symbol to be 'n', rather than 'y'.)
We also disable stack instrumentation in this case as it does things that
are functionally equivalent to inline instrumentation, namely adding
code that touches the shadow directly without going through a C helper.
* on ppc64 atm, the shadow lives in virtual memory and isn't accessible in
real mode. However, before we turn on virtual memory, we parse the device
tree to determine which platform and MMU we're running under. That calls
generic DT code, which is instrumented. Inline instrumentation in DT would
unconditionally attempt to touch the shadow region, which we won't have
set up yet, and would crash. We can make outline mode wait for the arch to
be ready, but we can't change what the compiler inserts for inline mode.
Signed-off-by: Daniel Axtens <redacted>
Reviewed-by: Marco Elver <elver@google.com>
Thank you.
2.30.2
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210616080244.51236-2-dja%40axtens.net.
This is introducing new global constants in a <linux/..> header. It
feels like this should be in <linux/pgtable.h> together with a
comment. Because <linux/kasan.h> is actually included in
<linux/slab.h>, most of the kernel will get these new definitions.
That in itself is fine, but it feels wrong that the KASAN header
introduces these.
Thoughts?
Sorry for only realizing this now.
My idea here was to follow the same road as MAX_PTRS_PER_P4D, added by commit https://github.com/linuxppc/linux/commit/c65e774f
That commit spread MAX_PTRS_PER_P4D everywhere.
Instead of doing the same, we found that it would be better to define a fallback for when the architecture doesn't define MAX_PTRS_PER_PxD . Now, it can be made more global in pgtable.h, in that case I'd suggest to also include MAX_PTRS_PER_P4D in the dance and avoid architectures like s390 having to define it, or even not defining it either in asm-generic/pgtable-nop4d.h
Christophe
2.30.2
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210616080244.51236-4-dja%40axtens.net.