Re: [PATCH v2 3/6] powerpc: Book3S 64-bit outline-only KASAN support

2022-06-02 Thread Guenter Roeck
Hi,

On Wed, May 18, 2022 at 08:05:31PM +1000, Paul Mackerras wrote:
> From: Daniel Axtens 
> 
> Implement a limited form of KASAN for Book3S 64-bit machines running under
> the Radix MMU, supporting only outline mode.
> 
>  - Enable the compiler instrumentation to check addresses and maintain the
>shadow region. (This is the guts of KASAN which we can easily reuse.)
> 
>  - Require kasan-vmalloc support to handle modules and anything else in
>vmalloc space.
> 
>  - KASAN needs to be able to validate all pointer accesses, but we can't
>instrument all kernel addresses - only linear map and vmalloc. On boot,
>set up a single page of read-only shadow that marks all iomap and
>vmemmap accesses as valid.
> 
>  - Document KASAN in powerpc docs.
> 

With this patch applied, powerpc:allmodconfig builds fail as follows.

Building powerpc:allmodconfig ... failed
--
Error log:
Error: External symbol 'memset' referenced from prom_init.c
make[3]: [arch/powerpc/kernel/Makefile:202: 
arch/powerpc/kernel/prom_init_check] Error 1 (ignored)
powerpc64-linux-ld: 
drivers/gpu/drm/amd/amdgpu/../display/dc/dml/display_mode_lib.o uses hard 
float, drivers/gpu/drm/amd/amdgpu/../display/dc/dcn31/dcn31_resource.o uses 
soft float
powerpc64-linux-ld: failed to merge target specific data of file 
drivers/gpu/drm/amd/amdgpu/../display/dc/dcn31/dcn31_resource.o
powerpc64-linux-ld: 
drivers/gpu/drm/amd/amdgpu/../display/dc/dml/display_mode_lib.o uses hard 
float, drivers/gpu/drm/amd/amdgpu/../display/dc/dcn315/dcn315_resource.o uses 
soft float
powerpc64-linux-ld: failed to merge target specific data of file 
drivers/gpu/drm/amd/amdgpu/../display/dc/dcn315/dcn315_resource.o
powerpc64-linux-ld: 
drivers/gpu/drm/amd/amdgpu/../display/dc/dml/display_mode_lib.o uses hard 
float, drivers/gpu/drm/amd/amdgpu/../display/dc/dcn316/dcn316_resource.o uses 
soft float
powerpc64-linux-ld: failed to merge target specific data of file 
drivers/gpu/drm/amd/amdgpu/../display/dc/dcn316/dcn316_resource.o
make[5]: [scripts/Makefile.build:435: drivers/gpu/drm/amd/amdgpu/amdgpu.o] 
Error 1 (ignored)
make[2]: *** No rule to make target 'drivers/gpu/drm/amd/amdgpu/amdgpu.o', 
needed by 'modules-only.symvers'.  Stop.
make[1]: [Makefile:1753: modules] Error 2 (ignored)

This is with gcc 11.3 and binutils 2.38. I also tried with gcc 11.2 and
binutils 2.36.1, with the same results.

Reverting this patch fixes the problem.

Guenter


[PATCH v2 3/6] powerpc: Book3S 64-bit outline-only KASAN support

2022-05-18 Thread Paul Mackerras
From: Daniel Axtens 

Implement a limited form of KASAN for Book3S 64-bit machines running under
the Radix MMU, supporting only outline mode.

 - Enable the compiler instrumentation to check addresses and maintain the
   shadow region. (This is the guts of KASAN which we can easily reuse.)

 - Require kasan-vmalloc support to handle modules and anything else in
   vmalloc space.

 - KASAN needs to be able to validate all pointer accesses, but we can't
   instrument all kernel addresses - only linear map and vmalloc. On boot,
   set up a single page of read-only shadow that marks all iomap and
   vmemmap accesses as valid.

 - Document KASAN in powerpc docs.

Background
--

KASAN support on Book3S is a bit tricky to get right:

 - It would be good to support inline instrumentation so as to be able to
   catch stack issues that cannot be caught with outline mode.

 - Inline instrumentation requires a fixed offset.

 - Book3S runs code with translations off ("real mode") during boot,
   including a lot of generic device-tree parsing code which is used to
   determine MMU features.

[ppc64 mm note: The kernel installs a linear mapping at effective
address c000...-c008 This is a one-to-one mapping with physical
memory from ... onward. Because of how memory accesses work on
powerpc 64-bit Book3S, a kernel pointer in the linear map accesses the
same memory both with translations on (accessing as an 'effective
address'), and with translations off (accessing as a 'real
address'). This works in both guests and the hypervisor. For more
details, see s5.7 of Book III of version 3 of the ISA, in particular
the Storage Control Overview, s5.7.3, and s5.7.5 - noting that this
KASAN implementation currently only supports Radix.]

 - Some code - most notably a lot of KVM code - also runs with translations
   off after boot.

 - Therefore any offset has to point to memory that is valid with
   translations on or off.

One approach is just to give up on inline instrumentation. This way
boot-time checks can be delayed until after the MMU is set is up, and we
can just not instrument any code that runs with translations off after
booting. Take this approach for now and require outline instrumentation.

Previous attempts allowed inline instrumentation. However, they came with
some unfortunate restrictions: only physically contiguous memory could be
used and it had to be specified at compile time. Maybe we can do better in
the future.

[pau...@ozlabs.org - Rebased onto 5.17.  Note that a kernel with
 CONFIG_KASAN=y will crash during boot on a machine using HPT
 translation because not all the entry points to the generic
 KASAN code are protected with a call to kasan_arch_is_ready().]

Originally-by: Balbir Singh  # ppc64 out-of-line radix 
version
Signed-off-by: Daniel Axtens 
Signed-off-by: Paul Mackerras 
---
 Documentation/powerpc/kasan.txt  |  48 -
 arch/powerpc/Kconfig |   5 +-
 arch/powerpc/Kconfig.debug   |   3 +-
 arch/powerpc/include/asm/book3s/64/hash.h|   4 +
 arch/powerpc/include/asm/book3s/64/pgtable.h |   3 +
 arch/powerpc/include/asm/book3s/64/radix.h   |  12 ++-
 arch/powerpc/include/asm/kasan.h |  22 
 arch/powerpc/kernel/Makefile |  11 ++
 arch/powerpc/kvm/Makefile|   5 +
 arch/powerpc/mm/book3s64/Makefile|   9 ++
 arch/powerpc/mm/kasan/Makefile   |   1 +
 arch/powerpc/mm/kasan/init_book3s_64.c   | 103 +++
 arch/powerpc/mm/ptdump/ptdump.c  |   3 +-
 arch/powerpc/platforms/Kconfig.cputype   |   1 +
 arch/powerpc/platforms/powernv/Makefile  |   8 ++
 arch/powerpc/platforms/pseries/Makefile  |   3 +
 16 files changed, 234 insertions(+), 7 deletions(-)
 create mode 100644 arch/powerpc/mm/kasan/init_book3s_64.c

diff --git a/Documentation/powerpc/kasan.txt b/Documentation/powerpc/kasan.txt
index 26bb0e8bb18c..f032b4eaf205 100644
--- a/Documentation/powerpc/kasan.txt
+++ b/Documentation/powerpc/kasan.txt
@@ -1,4 +1,4 @@
-KASAN is supported on powerpc on 32-bit only.
+KASAN is supported on powerpc on 32-bit and Radix 64-bit only.
 
 32 bit support
 ==
@@ -10,3 +10,49 @@ fixmap area and occupies one eighth of the total kernel 
virtual memory space.
 
 Instrumentation of the vmalloc area is optional, unless built with modules,
 in which case it is required.
+
+64 bit support
+==
+
+Currently, only the radix MMU is supported. There have been versions for hash
+and Book3E processors floating around on the mailing list, but nothing has been
+merged.
+
+KASAN support on Book3S is a bit tricky to get right:
+
+ - It would be good to support inline instrumentation so as to be able to catch
+   stack issues that cannot be caught with outline mode.
+
+ - Inline instrumentation requires a fixed offset.
+
+ - Book3S runs code with translations off ("real mode") during