Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 42db5ed8 authored by Vasily Gorbik's avatar Vasily Gorbik Committed by Martin Schwidefsky
Browse files

s390/kasan: add initialization code and enable it



Kasan needs 1/8 of kernel virtual address space to be reserved as the
shadow area. And eventually it requires the shadow memory offset to be
known at compile time (passed to the compiler when full instrumentation
is enabled).  Any value picked as the shadow area offset for 3-level
paging would eat up identity mapping on 4-level paging (with 1PB
shadow area size). So, the kernel sticks to 3-level paging when kasan
is enabled. 3TB border is picked as the shadow offset.  The memory
layout is adjusted so, that physical memory border does not exceed
KASAN_SHADOW_START and vmemmap does not go below KASAN_SHADOW_END.

Due to the fact that on s390 paging is set up very late and to cover
more code with kasan instrumentation, temporary identity mapping and
final shadow memory are set up early. The shadow memory mapping is
later carried over to init_mm.pgd during paging_init.

For the needs of paging structures allocation and shadow memory
population a primitive allocator is used, which simply chops off
memory blocks from the end of the physical memory.

Kasan currenty doesn't track vmemmap and vmalloc areas.

Current memory layout (for 3-level paging, 2GB physical memory).

---[ Identity Mapping ]---
0x0000000000000000-0x0000000000100000
---[ Kernel Image Start ]---
0x0000000000100000-0x0000000002b00000
---[ Kernel Image End ]---
0x0000000002b00000-0x0000000080000000        2G <- physical memory border
0x0000000080000000-0x0000030000000000     3070G PUD I
---[ Kasan Shadow Start ]---
0x0000030000000000-0x0000030010000000      256M PMD RW X  <- shadow for 2G memory
0x0000030010000000-0x0000037ff0000000   523776M PTE RO NX <- kasan zero ro page
0x0000037ff0000000-0x0000038000000000      256M PMD RW X  <- shadow for 2G modules
---[ Kasan Shadow End ]---
0x0000038000000000-0x000003d100000000      324G PUD I
---[ vmemmap Area ]---
0x000003d100000000-0x000003e080000000
---[ vmalloc Area ]---
0x000003e080000000-0x000003ff80000000
---[ Modules Area ]---
0x000003ff80000000-0x0000040000000000        2G

Acked-by: default avatarMartin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: default avatarVasily Gorbik <gor@linux.ibm.com>
Signed-off-by: default avatarMartin Schwidefsky <schwidefsky@de.ibm.com>
parent d0e2eb0a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -120,6 +120,7 @@ config S390
	select HAVE_ALIGNED_STRUCT_PAGE if SLUB
	select HAVE_ARCH_AUDITSYSCALL
	select HAVE_ARCH_JUMP_LABEL
	select HAVE_ARCH_KASAN
	select CPU_NO_EFFICIENT_FFS if !HAVE_MARCH_Z9_109_FEATURES
	select HAVE_ARCH_SECCOMP_FILTER
	select HAVE_ARCH_SOFT_DIRTY
+23 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_KASAN_H
#define __ASM_KASAN_H

#include <asm/pgtable.h>

#ifdef CONFIG_KASAN

#define KASAN_SHADOW_SCALE_SHIFT 3
#define KASAN_SHADOW_SIZE						       \
	(_AC(1, UL) << (_REGION2_SHIFT - KASAN_SHADOW_SCALE_SHIFT))
#define KASAN_SHADOW_OFFSET	_AC(0x30000000000, UL)
#define KASAN_SHADOW_START	KASAN_SHADOW_OFFSET
#define KASAN_SHADOW_END	(KASAN_SHADOW_START + KASAN_SHADOW_SIZE)

extern void kasan_early_init(void);
extern void kasan_copy_shadow(pgd_t *dst);
#else
static inline void kasan_early_init(void) { }
static inline void kasan_copy_shadow(pgd_t *dst) { }
#endif

#endif
+1 −0
Original line number Diff line number Diff line
@@ -1181,6 +1181,7 @@ static inline pte_t mk_pte(struct page *page, pgprot_t pgprot)

#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
#define pgd_offset_k(address) pgd_offset(&init_mm, address)
#define pgd_offset_raw(pgd, addr) ((pgd) + pgd_index(addr))

#define pmd_deref(pmd) (pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN)
#define pud_deref(pud) (pud_val(pud) & _REGION_ENTRY_ORIGIN)
+2 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include <asm/sections.h>
#include <asm/lowcore.h>
#include <asm/timex.h>
#include <asm/kasan.h>
#include "entry.h"

static void __init reset_tod_clock(void)
@@ -40,4 +41,5 @@ void __init startup_init_nobss(void)
{
	reset_tod_clock();
	clear_bss_section();
	kasan_early_init();
}
+16 −6
Original line number Diff line number Diff line
@@ -531,14 +531,19 @@ static void __init setup_memory_end(void)
{
	unsigned long vmax, vmalloc_size, tmp;

	/* Choose kernel address space layout: 2, 3, or 4 levels. */
	/* Choose kernel address space layout: 3 or 4 levels. */
	vmalloc_size = VMALLOC_END ?: (128UL << 30) - MODULES_LEN;
	if (IS_ENABLED(CONFIG_KASAN)) {
		vmax = _REGION2_SIZE; /* 3-level kernel page table */
	} else {
		tmp = (memory_end ?: max_physmem_end) / PAGE_SIZE;
		tmp = tmp * (sizeof(struct page) + PAGE_SIZE);
		if (tmp + vmalloc_size + MODULES_LEN <= _REGION2_SIZE)
			vmax = _REGION2_SIZE; /* 3-level kernel page table */
		else
			vmax = _REGION1_SIZE; /* 4-level kernel page table */
	}

	/* module area is at the end of the kernel address space. */
	MODULES_END = vmax;
	MODULES_VADDR = MODULES_END - MODULES_LEN;
@@ -556,6 +561,11 @@ static void __init setup_memory_end(void)

	/* Take care that memory_end is set and <= vmemmap */
	memory_end = min(memory_end ?: max_physmem_end, tmp);
#ifdef CONFIG_KASAN
	/* fit in kasan shadow memory region between 1:1 and vmemmap */
	memory_end = min(memory_end, KASAN_SHADOW_START);
	vmemmap = max(vmemmap, (struct page *)KASAN_SHADOW_END);
#endif
	max_pfn = max_low_pfn = PFN_DOWN(memory_end);
	memblock_remove(memory_end, ULONG_MAX);

Loading