2022-02-18 15:01:47

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 0/4] Fixes KASAN and other along the way

As reported by Aleksandr, syzbot riscv is broken since commit
54c5639d8f50 ("riscv: Fix asan-stack clang build"). This commit actually
breaks KASAN_INLINE which is not fixed in this series, that will come later
when found.

Nevertheless, this series fixes small things that made the syzbot
configuration + KASAN_OUTLINE fail to boot.

Note that even though the config at [1] boots fine with this series, I
was not able to boot the small config at [2] which fails because
kasan_poison receives a really weird address 0x4075706301000000 (maybe a
kasan person could provide some hint about what happens below in
do_ctors -> __asan_register_globals):

Thread 2 hit Breakpoint 1, kasan_poison (addr=<optimized out>, size=<optimized out>, value=<optimized out>, init=<optimized out>) at /home/alex/work/linux/mm/kasan/shadow.c:90
90 if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
1: x/i $pc
=> 0xffffffff80261712 <kasan_poison>: andi a4,a0,7
5: /x $a0 = 0x4075706301000000

Thread 2 hit Breakpoint 2, handle_exception () at /home/alex/work/linux/arch/riscv/kernel/entry.S:27
27 csrrw tp, CSR_SCRATCH, tp
1: x/i $pc
=> 0xffffffff80004098 <handle_exception>: csrrw tp,sscratch,tp
5: /x $a0 = 0xe80eae0b60200000
(gdb) bt
#0 handle_exception () at /home/alex/work/linux/arch/riscv/kernel/entry.S:27
#1 0xffffffff80261746 in kasan_poison (addr=<optimized out>, size=<optimized out>, value=<optimized out>, init=<optimized out>)
at /home/alex/work/linux/mm/kasan/shadow.c:98
#2 0xffffffff802618b4 in kasan_unpoison (addr=<optimized out>, size=<optimized out>, init=<optimized out>)
at /home/alex/work/linux/mm/kasan/shadow.c:138
#3 0xffffffff80260876 in register_global (global=<optimized out>) at /home/alex/work/linux/mm/kasan/generic.c:214
#4 __asan_register_globals (globals=<optimized out>, size=<optimized out>) at /home/alex/work/linux/mm/kasan/generic.c:226
#5 0xffffffff8125efac in _sub_I_65535_1 ()
#6 0xffffffff81201b32 in do_ctors () at /home/alex/work/linux/init/main.c:1156
#7 do_basic_setup () at /home/alex/work/linux/init/main.c:1407
#8 kernel_init_freeable () at /home/alex/work/linux/init/main.c:1613
#9 0xffffffff81153ddc in kernel_init (unused=<optimized out>) at /home/alex/work/linux/init/main.c:1502
#10 0xffffffff800041c0 in handle_exception () at /home/alex/work/linux/arch/riscv/kernel/entry.S:231


Thanks again to Aleksandr for narrowing down the issues fixed here.


[1] https://gist.github.com/a-nogikh/279c85c2d24f47efcc3e865c08844138
[2] https://gist.github.com/AlexGhiti/a5a0cab0227e2bf38f9d12232591c0e4

Alexandre Ghiti (4):
riscv: Fix is_linear_mapping with recent move of KASAN region
riscv: Fix config KASAN && SPARSEMEM && !SPARSE_VMEMMAP
riscv: Fix DEBUG_VIRTUAL false warnings
riscv: Fix config KASAN && DEBUG_VIRTUAL

arch/riscv/include/asm/page.h | 2 +-
arch/riscv/mm/Makefile | 3 +++
arch/riscv/mm/kasan_init.c | 3 +--
arch/riscv/mm/physaddr.c | 4 +---
4 files changed, 6 insertions(+), 6 deletions(-)

--
2.32.0


2022-02-18 15:29:49

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 1/4] riscv: Fix is_linear_mapping with recent move of KASAN region

KASAN region was recently moved between the linear mapping and the
kernel mapping, is_linear_mapping used to check the validity of an
address by using the start of the kernel mapping, which is now wrong.

Fix this by using the maximum size of the physical memory.

Fixes: f7ae02333d13 ("riscv: Move KASAN mapping next to the kernel mapping")
Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/include/asm/page.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index 160e3a1e8f8b..004372f8da54 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -119,7 +119,7 @@ extern phys_addr_t phys_ram_base;
((x) >= kernel_map.virt_addr && (x) < (kernel_map.virt_addr + kernel_map.size))

#define is_linear_mapping(x) \
- ((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < kernel_map.virt_addr))
+ ((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < PAGE_OFFSET + KERN_VIRT_SIZE))

#define linear_mapping_pa_to_va(x) ((void *)((unsigned long)(x) + kernel_map.va_pa_offset))
#define kernel_mapping_pa_to_va(y) ({ \
--
2.32.0

2022-02-18 21:28:03

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 2/4] riscv: Fix config KASAN && SPARSEMEM && !SPARSE_VMEMMAP

In order to get the pfn of a struct page* when sparsemem is enabled
without vmemmap, the mem_section structures need to be initialized which
happens in sparse_init.

But kasan_early_init calls pfn_to_page way before sparse_init is called,
which then tries to dereference a null mem_section pointer.

Fix this by removing the usage of this function in kasan_early_init.

Fixes: 8ad8b72721d0 ("riscv: Add KASAN support")
Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/mm/kasan_init.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index f61f7ca6fe0f..85e849318389 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -202,8 +202,7 @@ asmlinkage void __init kasan_early_init(void)

for (i = 0; i < PTRS_PER_PTE; ++i)
set_pte(kasan_early_shadow_pte + i,
- mk_pte(virt_to_page(kasan_early_shadow_page),
- PAGE_KERNEL));
+ pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL));

for (i = 0; i < PTRS_PER_PMD; ++i)
set_pmd(kasan_early_shadow_pmd + i,
--
2.32.0

2022-02-21 08:55:21

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH -fixes 1/4] riscv: Fix is_linear_mapping with recent move of KASAN region

Hi Alexandre,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.17-rc4 next-20220217]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Alexandre-Ghiti/Fixes-KASAN-and-other-along-the-way/20220220-181628
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 4f12b742eb2b3a850ac8be7dc4ed52976fc6cb0b
config: riscv-nommu_virt_defconfig (https://download.01.org/0day-ci/archive/20220221/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project d271fc04d5b97b12e6b797c6067d3c96a8d7470e)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install riscv cross compiling tool for clang build
# apt-get install binutils-riscv64-linux-gnu
# https://github.com/0day-ci/linux/commit/de8a909a9eabf9066802a3396b7009cbf4fa4369
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Alexandre-Ghiti/Fixes-KASAN-and-other-along-the-way/20220220-181628
git checkout de8a909a9eabf9066802a3396b7009cbf4fa4369
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=riscv prepare

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

In file included from arch/riscv/kernel/asm-offsets.c:10:
>> include/linux/mm.h:837:22: error: use of undeclared identifier 'KERN_VIRT_SIZE'; did you mean 'KERN_VERSION'?
struct page *page = virt_to_page(x);
^
arch/riscv/include/asm/page.h:165:42: note: expanded from macro 'virt_to_page'
#define virt_to_page(vaddr) (pfn_to_page(virt_to_pfn(vaddr)))
^
arch/riscv/include/asm/page.h:162:41: note: expanded from macro 'virt_to_pfn'
#define virt_to_pfn(vaddr) (phys_to_pfn(__pa(vaddr)))
^
arch/riscv/include/asm/page.h:156:18: note: expanded from macro '__pa'
#define __pa(x) __virt_to_phys((unsigned long)(x))
^
arch/riscv/include/asm/page.h:151:27: note: expanded from macro '__virt_to_phys'
#define __virt_to_phys(x) __va_to_pa_nodebug(x)
^
arch/riscv/include/asm/page.h:143:2: note: expanded from macro '__va_to_pa_nodebug'
is_linear_mapping(_x) ? \
^
arch/riscv/include/asm/page.h:122:75: note: expanded from macro 'is_linear_mapping'
((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < PAGE_OFFSET + KERN_VIRT_SIZE))
^
include/uapi/linux/sysctl.h:88:2: note: 'KERN_VERSION' declared here
KERN_VERSION=4, /* string: compile time info */
^
In file included from arch/riscv/kernel/asm-offsets.c:10:
include/linux/mm.h:844:22: error: use of undeclared identifier 'KERN_VIRT_SIZE'; did you mean 'KERN_VERSION'?
struct page *page = virt_to_page(x);
^
arch/riscv/include/asm/page.h:165:42: note: expanded from macro 'virt_to_page'
#define virt_to_page(vaddr) (pfn_to_page(virt_to_pfn(vaddr)))
^
arch/riscv/include/asm/page.h:162:41: note: expanded from macro 'virt_to_pfn'
#define virt_to_pfn(vaddr) (phys_to_pfn(__pa(vaddr)))
^
arch/riscv/include/asm/page.h:156:18: note: expanded from macro '__pa'
#define __pa(x) __virt_to_phys((unsigned long)(x))
^
arch/riscv/include/asm/page.h:151:27: note: expanded from macro '__virt_to_phys'
#define __virt_to_phys(x) __va_to_pa_nodebug(x)
^
arch/riscv/include/asm/page.h:143:2: note: expanded from macro '__va_to_pa_nodebug'
is_linear_mapping(_x) ? \
^
arch/riscv/include/asm/page.h:122:75: note: expanded from macro 'is_linear_mapping'
((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < PAGE_OFFSET + KERN_VIRT_SIZE))
^
include/uapi/linux/sysctl.h:88:2: note: 'KERN_VERSION' declared here
KERN_VERSION=4, /* string: compile time info */
^
2 errors generated.
make[2]: *** [scripts/Makefile.build:121: arch/riscv/kernel/asm-offsets.s] Error 1
make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1191: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:219: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.


vim +837 include/linux/mm.h

70b50f94f1644e Andrea Arcangeli 2011-11-02 834
b49af68ff9fc5d Christoph Lameter 2007-05-06 835 static inline struct page *virt_to_head_page(const void *x)
b49af68ff9fc5d Christoph Lameter 2007-05-06 836 {
b49af68ff9fc5d Christoph Lameter 2007-05-06 @837 struct page *page = virt_to_page(x);
ccaafd7fd039ae Joonsoo Kim 2015-02-10 838
1d798ca3f16437 Kirill A. Shutemov 2015-11-06 839 return compound_head(page);
b49af68ff9fc5d Christoph Lameter 2007-05-06 840 }
b49af68ff9fc5d Christoph Lameter 2007-05-06 841

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]