2018-12-28 16:31:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-08-13 13:37:24 +08:00
|
|
|
/*
|
2020-12-23 04:00:28 +08:00
|
|
|
* This file contains KASAN shadow initialization code.
|
2015-08-13 13:37:24 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
|
|
|
|
* Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2018-10-31 06:09:49 +08:00
|
|
|
#include <linux/memblock.h>
|
2015-08-13 13:37:24 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kasan.h>
|
|
|
|
#include <linux/kernel.h>
|
2017-01-11 05:35:44 +08:00
|
|
|
#include <linux/mm.h>
|
2015-08-13 13:37:24 +08:00
|
|
|
#include <linux/pfn.h>
|
2018-08-18 06:47:04 +08:00
|
|
|
#include <linux/slab.h>
|
2015-08-13 13:37:24 +08:00
|
|
|
|
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
|
2018-08-18 06:47:04 +08:00
|
|
|
#include "kasan.h"
|
|
|
|
|
2015-08-13 13:37:24 +08:00
|
|
|
/*
|
|
|
|
* This page serves two purposes:
|
|
|
|
* - It used as early shadow memory. The entire shadow region populated
|
|
|
|
* with this page, before we will be able to setup normal shadow memory.
|
|
|
|
* - Latter it reused it as zero shadow to cover large ranges of memory
|
|
|
|
* that allowed to access, but not handled by kasan (vmalloc/vmemmap ...).
|
|
|
|
*/
|
2018-12-28 16:30:01 +08:00
|
|
|
unsigned char kasan_early_shadow_page[PAGE_SIZE] __page_aligned_bss;
|
2015-08-13 13:37:24 +08:00
|
|
|
|
2017-03-09 22:24:07 +08:00
|
|
|
#if CONFIG_PGTABLE_LEVELS > 4
|
2018-12-28 16:30:01 +08:00
|
|
|
p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D] __page_aligned_bss;
|
2018-08-18 06:47:04 +08:00
|
|
|
static inline bool kasan_p4d_table(pgd_t pgd)
|
|
|
|
{
|
2018-12-28 16:30:01 +08:00
|
|
|
return pgd_page(pgd) == virt_to_page(lm_alias(kasan_early_shadow_p4d));
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool kasan_p4d_table(pgd_t pgd)
|
|
|
|
{
|
2019-03-06 07:41:31 +08:00
|
|
|
return false;
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
2017-03-09 22:24:07 +08:00
|
|
|
#endif
|
2015-08-13 13:37:24 +08:00
|
|
|
#if CONFIG_PGTABLE_LEVELS > 3
|
2021-06-29 10:40:49 +08:00
|
|
|
pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss;
|
2018-08-18 06:47:04 +08:00
|
|
|
static inline bool kasan_pud_table(p4d_t p4d)
|
|
|
|
{
|
2018-12-28 16:30:01 +08:00
|
|
|
return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud));
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool kasan_pud_table(p4d_t p4d)
|
|
|
|
{
|
2019-03-06 07:41:31 +08:00
|
|
|
return false;
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
2015-08-13 13:37:24 +08:00
|
|
|
#endif
|
|
|
|
#if CONFIG_PGTABLE_LEVELS > 2
|
2021-06-29 10:40:49 +08:00
|
|
|
pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss;
|
2018-08-18 06:47:04 +08:00
|
|
|
static inline bool kasan_pmd_table(pud_t pud)
|
|
|
|
{
|
2018-12-28 16:30:01 +08:00
|
|
|
return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd));
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool kasan_pmd_table(pud_t pud)
|
|
|
|
{
|
2019-03-06 07:41:31 +08:00
|
|
|
return false;
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
2015-08-13 13:37:24 +08:00
|
|
|
#endif
|
2021-06-29 10:40:49 +08:00
|
|
|
pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]
|
2021-01-13 07:49:14 +08:00
|
|
|
__page_aligned_bss;
|
2015-08-13 13:37:24 +08:00
|
|
|
|
2018-08-18 06:47:04 +08:00
|
|
|
static inline bool kasan_pte_table(pmd_t pmd)
|
|
|
|
{
|
2018-12-28 16:30:01 +08:00
|
|
|
return pmd_page(pmd) == virt_to_page(lm_alias(kasan_early_shadow_pte));
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
|
2018-12-28 16:30:01 +08:00
|
|
|
static inline bool kasan_early_shadow_page_entry(pte_t pte)
|
2018-08-18 06:47:04 +08:00
|
|
|
{
|
2018-12-28 16:30:01 +08:00
|
|
|
return pte_page(pte) == virt_to_page(lm_alias(kasan_early_shadow_page));
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:37:24 +08:00
|
|
|
static __init void *early_alloc(size_t size, int node)
|
|
|
|
{
|
2019-03-12 14:30:31 +08:00
|
|
|
void *ptr = memblock_alloc_try_nid(size, size, __pa(MAX_DMA_ADDRESS),
|
|
|
|
MEMBLOCK_ALLOC_ACCESSIBLE, node);
|
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
panic("%s: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n",
|
|
|
|
__func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS));
|
|
|
|
|
|
|
|
return ptr;
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 06:47:04 +08:00
|
|
|
static void __ref zero_pte_populate(pmd_t *pmd, unsigned long addr,
|
2015-08-13 13:37:24 +08:00
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
pte_t *pte = pte_offset_kernel(pmd, addr);
|
|
|
|
pte_t zero_pte;
|
|
|
|
|
2018-12-28 16:30:01 +08:00
|
|
|
zero_pte = pfn_pte(PFN_DOWN(__pa_symbol(kasan_early_shadow_page)),
|
|
|
|
PAGE_KERNEL);
|
2015-08-13 13:37:24 +08:00
|
|
|
zero_pte = pte_wrprotect(zero_pte);
|
|
|
|
|
|
|
|
while (addr + PAGE_SIZE <= end) {
|
|
|
|
set_pte_at(&init_mm, addr, pte, zero_pte);
|
|
|
|
addr += PAGE_SIZE;
|
|
|
|
pte = pte_offset_kernel(pmd, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 06:47:04 +08:00
|
|
|
static int __ref zero_pmd_populate(pud_t *pud, unsigned long addr,
|
2015-08-13 13:37:24 +08:00
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
pmd_t *pmd = pmd_offset(pud, addr);
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pmd_addr_end(addr, end);
|
|
|
|
|
|
|
|
if (IS_ALIGNED(addr, PMD_SIZE) && end - addr >= PMD_SIZE) {
|
2018-12-28 16:30:01 +08:00
|
|
|
pmd_populate_kernel(&init_mm, pmd,
|
|
|
|
lm_alias(kasan_early_shadow_pte));
|
2015-08-13 13:37:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pmd_none(*pmd)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
pte_t *p;
|
|
|
|
|
|
|
|
if (slab_is_available())
|
mm: treewide: remove unused address argument from pte_alloc functions
Patch series "Add support for fast mremap".
This series speeds up the mremap(2) syscall by copying page tables at
the PMD level even for non-THP systems. There is concern that the extra
'address' argument that mremap passes to pte_alloc may do something
subtle architecture related in the future that may make the scheme not
work. Also we find that there is no point in passing the 'address' to
pte_alloc since its unused. This patch therefore removes this argument
tree-wide resulting in a nice negative diff as well. Also ensuring
along the way that the enabled architectures do not do anything funky
with the 'address' argument that goes unnoticed by the optimization.
Build and boot tested on x86-64. Build tested on arm64. The config
enablement patch for arm64 will be posted in the future after more
testing.
The changes were obtained by applying the following Coccinelle script.
(thanks Julia for answering all Coccinelle questions!).
Following fix ups were done manually:
* Removal of address argument from pte_fragment_alloc
* Removal of pte_alloc_one_fast definitions from m68k and microblaze.
// Options: --include-headers --no-includes
// Note: I split the 'identifier fn' line, so if you are manually
// running it, please unsplit it so it runs for you.
virtual patch
@pte_alloc_func_def depends on patch exists@
identifier E2;
identifier fn =~
"^(__pte_alloc|pte_alloc_one|pte_alloc|__pte_alloc_kernel|pte_alloc_one_kernel)$";
type T2;
@@
fn(...
- , T2 E2
)
{ ... }
@pte_alloc_func_proto_noarg depends on patch exists@
type T1, T2, T3, T4;
identifier fn =~ "^(__pte_alloc|pte_alloc_one|pte_alloc|__pte_alloc_kernel|pte_alloc_one_kernel)$";
@@
(
- T3 fn(T1, T2);
+ T3 fn(T1);
|
- T3 fn(T1, T2, T4);
+ T3 fn(T1, T2);
)
@pte_alloc_func_proto depends on patch exists@
identifier E1, E2, E4;
type T1, T2, T3, T4;
identifier fn =~
"^(__pte_alloc|pte_alloc_one|pte_alloc|__pte_alloc_kernel|pte_alloc_one_kernel)$";
@@
(
- T3 fn(T1 E1, T2 E2);
+ T3 fn(T1 E1);
|
- T3 fn(T1 E1, T2 E2, T4 E4);
+ T3 fn(T1 E1, T2 E2);
)
@pte_alloc_func_call depends on patch exists@
expression E2;
identifier fn =~
"^(__pte_alloc|pte_alloc_one|pte_alloc|__pte_alloc_kernel|pte_alloc_one_kernel)$";
@@
fn(...
-, E2
)
@pte_alloc_macro depends on patch exists@
identifier fn =~
"^(__pte_alloc|pte_alloc_one|pte_alloc|__pte_alloc_kernel|pte_alloc_one_kernel)$";
identifier a, b, c;
expression e;
position p;
@@
(
- #define fn(a, b, c) e
+ #define fn(a, b) e
|
- #define fn(a, b) e
+ #define fn(a) e
)
Link: http://lkml.kernel.org/r/20181108181201.88826-2-joelaf@google.com
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Suggested-by: Kirill A. Shutemov <kirill@shutemov.name>
Acked-by: Kirill A. Shutemov <kirill@shutemov.name>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Julia Lawall <Julia.Lawall@lip6.fr>
Cc: Kirill A. Shutemov <kirill@shutemov.name>
Cc: William Kucharski <william.kucharski@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 07:28:34 +08:00
|
|
|
p = pte_alloc_one_kernel(&init_mm);
|
2018-08-18 06:47:04 +08:00
|
|
|
else
|
|
|
|
p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
pmd_populate_kernel(&init_mm, pmd, p);
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|
|
|
|
zero_pte_populate(pmd, addr, next);
|
|
|
|
} while (pmd++, addr = next, addr != end);
|
2018-08-18 06:47:04 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 22:54:16 +08:00
|
|
|
void __weak __meminit pmd_init(void *addr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-18 06:47:04 +08:00
|
|
|
static int __ref zero_pud_populate(p4d_t *p4d, unsigned long addr,
|
2015-08-13 13:37:24 +08:00
|
|
|
unsigned long end)
|
|
|
|
{
|
2017-03-09 22:24:07 +08:00
|
|
|
pud_t *pud = pud_offset(p4d, addr);
|
2015-08-13 13:37:24 +08:00
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pud_addr_end(addr, end);
|
|
|
|
if (IS_ALIGNED(addr, PUD_SIZE) && end - addr >= PUD_SIZE) {
|
|
|
|
pmd_t *pmd;
|
|
|
|
|
2018-12-28 16:30:01 +08:00
|
|
|
pud_populate(&init_mm, pud,
|
|
|
|
lm_alias(kasan_early_shadow_pmd));
|
2015-08-13 13:37:24 +08:00
|
|
|
pmd = pmd_offset(pud, addr);
|
2018-12-28 16:30:01 +08:00
|
|
|
pmd_populate_kernel(&init_mm, pmd,
|
|
|
|
lm_alias(kasan_early_shadow_pte));
|
2015-08-13 13:37:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pud_none(*pud)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
pmd_t *p;
|
|
|
|
|
|
|
|
if (slab_is_available()) {
|
|
|
|
p = pmd_alloc(&init_mm, pud, addr);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
2023-09-06 22:54:16 +08:00
|
|
|
p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
|
|
|
|
pmd_init(p);
|
|
|
|
pud_populate(&init_mm, pud, p);
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|
|
|
|
zero_pmd_populate(pud, addr, next);
|
|
|
|
} while (pud++, addr = next, addr != end);
|
2018-08-18 06:47:04 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 22:54:16 +08:00
|
|
|
void __weak __meminit pud_init(void *addr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-18 06:47:04 +08:00
|
|
|
static int __ref zero_p4d_populate(pgd_t *pgd, unsigned long addr,
|
2017-03-09 22:24:07 +08:00
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
p4d_t *p4d = p4d_offset(pgd, addr);
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = p4d_addr_end(addr, end);
|
2017-07-11 06:50:21 +08:00
|
|
|
if (IS_ALIGNED(addr, P4D_SIZE) && end - addr >= P4D_SIZE) {
|
|
|
|
pud_t *pud;
|
|
|
|
pmd_t *pmd;
|
|
|
|
|
2018-12-28 16:30:01 +08:00
|
|
|
p4d_populate(&init_mm, p4d,
|
|
|
|
lm_alias(kasan_early_shadow_pud));
|
2017-07-11 06:50:21 +08:00
|
|
|
pud = pud_offset(p4d, addr);
|
2018-12-28 16:30:01 +08:00
|
|
|
pud_populate(&init_mm, pud,
|
|
|
|
lm_alias(kasan_early_shadow_pmd));
|
2017-07-11 06:50:21 +08:00
|
|
|
pmd = pmd_offset(pud, addr);
|
|
|
|
pmd_populate_kernel(&init_mm, pmd,
|
2018-12-28 16:30:01 +08:00
|
|
|
lm_alias(kasan_early_shadow_pte));
|
2017-07-11 06:50:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-09 22:24:07 +08:00
|
|
|
|
|
|
|
if (p4d_none(*p4d)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
pud_t *p;
|
|
|
|
|
|
|
|
if (slab_is_available()) {
|
|
|
|
p = pud_alloc(&init_mm, p4d, addr);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
2023-09-06 22:54:16 +08:00
|
|
|
p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
|
|
|
|
pud_init(p);
|
|
|
|
p4d_populate(&init_mm, p4d, p);
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
2017-03-09 22:24:07 +08:00
|
|
|
}
|
|
|
|
zero_pud_populate(p4d, addr, next);
|
|
|
|
} while (p4d++, addr = next, addr != end);
|
2018-08-18 06:47:04 +08:00
|
|
|
|
|
|
|
return 0;
|
2017-03-09 22:24:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:37:24 +08:00
|
|
|
/**
|
2018-12-28 16:30:01 +08:00
|
|
|
* kasan_populate_early_shadow - populate shadow memory region with
|
|
|
|
* kasan_early_shadow_page
|
2021-06-05 11:01:33 +08:00
|
|
|
* @shadow_start: start of the memory range to populate
|
|
|
|
* @shadow_end: end of the memory range to populate
|
2015-08-13 13:37:24 +08:00
|
|
|
*/
|
2018-12-28 16:30:01 +08:00
|
|
|
int __ref kasan_populate_early_shadow(const void *shadow_start,
|
|
|
|
const void *shadow_end)
|
2015-08-13 13:37:24 +08:00
|
|
|
{
|
|
|
|
unsigned long addr = (unsigned long)shadow_start;
|
|
|
|
unsigned long end = (unsigned long)shadow_end;
|
|
|
|
pgd_t *pgd = pgd_offset_k(addr);
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
do {
|
|
|
|
next = pgd_addr_end(addr, end);
|
|
|
|
|
|
|
|
if (IS_ALIGNED(addr, PGDIR_SIZE) && end - addr >= PGDIR_SIZE) {
|
2017-03-09 22:24:07 +08:00
|
|
|
p4d_t *p4d;
|
2015-08-13 13:37:24 +08:00
|
|
|
pud_t *pud;
|
|
|
|
pmd_t *pmd;
|
|
|
|
|
|
|
|
/*
|
2018-12-28 16:30:01 +08:00
|
|
|
* kasan_early_shadow_pud should be populated with pmds
|
2015-08-13 13:37:24 +08:00
|
|
|
* at this moment.
|
|
|
|
* [pud,pmd]_populate*() below needed only for
|
|
|
|
* 3,2 - level page tables where we don't have
|
|
|
|
* puds,pmds, so pgd_populate(), pud_populate()
|
|
|
|
* is noops.
|
|
|
|
*/
|
2018-12-28 16:30:01 +08:00
|
|
|
pgd_populate(&init_mm, pgd,
|
|
|
|
lm_alias(kasan_early_shadow_p4d));
|
2017-03-09 22:24:07 +08:00
|
|
|
p4d = p4d_offset(pgd, addr);
|
2018-12-28 16:30:01 +08:00
|
|
|
p4d_populate(&init_mm, p4d,
|
|
|
|
lm_alias(kasan_early_shadow_pud));
|
2017-03-09 22:24:07 +08:00
|
|
|
pud = pud_offset(p4d, addr);
|
2018-12-28 16:30:01 +08:00
|
|
|
pud_populate(&init_mm, pud,
|
|
|
|
lm_alias(kasan_early_shadow_pmd));
|
2015-08-13 13:37:24 +08:00
|
|
|
pmd = pmd_offset(pud, addr);
|
2018-12-28 16:30:01 +08:00
|
|
|
pmd_populate_kernel(&init_mm, pmd,
|
|
|
|
lm_alias(kasan_early_shadow_pte));
|
2015-08-13 13:37:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pgd_none(*pgd)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
p4d_t *p;
|
|
|
|
|
|
|
|
if (slab_is_available()) {
|
|
|
|
p = p4d_alloc(&init_mm, pgd, addr);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
|
|
|
pgd_populate(&init_mm, pgd,
|
|
|
|
early_alloc(PAGE_SIZE, NUMA_NO_NODE));
|
|
|
|
}
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|
2017-03-09 22:24:07 +08:00
|
|
|
zero_p4d_populate(pgd, addr, next);
|
2015-08-13 13:37:24 +08:00
|
|
|
} while (pgd++, addr = next, addr != end);
|
2018-08-18 06:47:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_free_pte(pte_t *pte_start, pmd_t *pmd)
|
|
|
|
{
|
|
|
|
pte_t *pte;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PTE; i++) {
|
|
|
|
pte = pte_start + i;
|
mm: ptep_get() conversion
Convert all instances of direct pte_t* dereferencing to instead use
ptep_get() helper. This means that by default, the accesses change from a
C dereference to a READ_ONCE(). This is technically the correct thing to
do since where pgtables are modified by HW (for access/dirty) they are
volatile and therefore we should always ensure READ_ONCE() semantics.
But more importantly, by always using the helper, it can be overridden by
the architecture to fully encapsulate the contents of the pte. Arch code
is deliberately not converted, as the arch code knows best. It is
intended that arch code (arm64) will override the default with its own
implementation that can (e.g.) hide certain bits from the core code, or
determine young/dirty status by mixing in state from another source.
Conversion was done using Coccinelle:
----
// $ make coccicheck \
// COCCI=ptepget.cocci \
// SPFLAGS="--include-headers" \
// MODE=patch
virtual patch
@ depends on patch @
pte_t *v;
@@
- *v
+ ptep_get(v)
----
Then reviewed and hand-edited to avoid multiple unnecessary calls to
ptep_get(), instead opting to store the result of a single call in a
variable, where it is correct to do so. This aims to negate any cost of
READ_ONCE() and will benefit arch-overrides that may be more complex.
Included is a fix for an issue in an earlier version of this patch that
was pointed out by kernel test robot. The issue arose because config
MMU=n elides definition of the ptep helper functions, including
ptep_get(). HUGETLB_PAGE=n configs still define a simple
huge_ptep_clear_flush() for linking purposes, which dereferences the ptep.
So when both configs are disabled, this caused a build error because
ptep_get() is not defined. Fix by continuing to do a direct dereference
when MMU=n. This is safe because for this config the arch code cannot be
trying to virtualize the ptes because none of the ptep helpers are
defined.
Link: https://lkml.kernel.org/r/20230612151545.3317766-4-ryan.roberts@arm.com
Reported-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/oe-kbuild-all/202305120142.yXsNEo6H-lkp@intel.com/
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Dave Airlie <airlied@gmail.com>
Cc: Dimitri Sivanich <dimitri.sivanich@hpe.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Lorenzo Stoakes <lstoakes@gmail.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
Cc: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: SeongJae Park <sj@kernel.org>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-06-12 23:15:45 +08:00
|
|
|
if (!pte_none(ptep_get(pte)))
|
2018-08-18 06:47:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pte_free_kernel(&init_mm, (pte_t *)page_to_virt(pmd_page(*pmd)));
|
|
|
|
pmd_clear(pmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_free_pmd(pmd_t *pmd_start, pud_t *pud)
|
|
|
|
{
|
|
|
|
pmd_t *pmd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PMD; i++) {
|
|
|
|
pmd = pmd_start + i;
|
|
|
|
if (!pmd_none(*pmd))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pmd_free(&init_mm, (pmd_t *)page_to_virt(pud_page(*pud)));
|
|
|
|
pud_clear(pud);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_free_pud(pud_t *pud_start, p4d_t *p4d)
|
|
|
|
{
|
|
|
|
pud_t *pud;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PUD; i++) {
|
|
|
|
pud = pud_start + i;
|
|
|
|
if (!pud_none(*pud))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pud_free(&init_mm, (pud_t *)page_to_virt(p4d_page(*p4d)));
|
|
|
|
p4d_clear(p4d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_free_p4d(p4d_t *p4d_start, pgd_t *pgd)
|
|
|
|
{
|
|
|
|
p4d_t *p4d;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_P4D; i++) {
|
|
|
|
p4d = p4d_start + i;
|
|
|
|
if (!p4d_none(*p4d))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p4d_free(&init_mm, (p4d_t *)page_to_virt(pgd_page(*pgd)));
|
|
|
|
pgd_clear(pgd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_remove_pte_table(pte_t *pte, unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
mm: ptep_get() conversion
Convert all instances of direct pte_t* dereferencing to instead use
ptep_get() helper. This means that by default, the accesses change from a
C dereference to a READ_ONCE(). This is technically the correct thing to
do since where pgtables are modified by HW (for access/dirty) they are
volatile and therefore we should always ensure READ_ONCE() semantics.
But more importantly, by always using the helper, it can be overridden by
the architecture to fully encapsulate the contents of the pte. Arch code
is deliberately not converted, as the arch code knows best. It is
intended that arch code (arm64) will override the default with its own
implementation that can (e.g.) hide certain bits from the core code, or
determine young/dirty status by mixing in state from another source.
Conversion was done using Coccinelle:
----
// $ make coccicheck \
// COCCI=ptepget.cocci \
// SPFLAGS="--include-headers" \
// MODE=patch
virtual patch
@ depends on patch @
pte_t *v;
@@
- *v
+ ptep_get(v)
----
Then reviewed and hand-edited to avoid multiple unnecessary calls to
ptep_get(), instead opting to store the result of a single call in a
variable, where it is correct to do so. This aims to negate any cost of
READ_ONCE() and will benefit arch-overrides that may be more complex.
Included is a fix for an issue in an earlier version of this patch that
was pointed out by kernel test robot. The issue arose because config
MMU=n elides definition of the ptep helper functions, including
ptep_get(). HUGETLB_PAGE=n configs still define a simple
huge_ptep_clear_flush() for linking purposes, which dereferences the ptep.
So when both configs are disabled, this caused a build error because
ptep_get() is not defined. Fix by continuing to do a direct dereference
when MMU=n. This is safe because for this config the arch code cannot be
trying to virtualize the ptes because none of the ptep helpers are
defined.
Link: https://lkml.kernel.org/r/20230612151545.3317766-4-ryan.roberts@arm.com
Reported-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/oe-kbuild-all/202305120142.yXsNEo6H-lkp@intel.com/
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Dave Airlie <airlied@gmail.com>
Cc: Dimitri Sivanich <dimitri.sivanich@hpe.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Lorenzo Stoakes <lstoakes@gmail.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
Cc: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: SeongJae Park <sj@kernel.org>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-06-12 23:15:45 +08:00
|
|
|
pte_t ptent;
|
2018-08-18 06:47:04 +08:00
|
|
|
|
|
|
|
for (; addr < end; addr = next, pte++) {
|
|
|
|
next = (addr + PAGE_SIZE) & PAGE_MASK;
|
|
|
|
if (next > end)
|
|
|
|
next = end;
|
|
|
|
|
mm: ptep_get() conversion
Convert all instances of direct pte_t* dereferencing to instead use
ptep_get() helper. This means that by default, the accesses change from a
C dereference to a READ_ONCE(). This is technically the correct thing to
do since where pgtables are modified by HW (for access/dirty) they are
volatile and therefore we should always ensure READ_ONCE() semantics.
But more importantly, by always using the helper, it can be overridden by
the architecture to fully encapsulate the contents of the pte. Arch code
is deliberately not converted, as the arch code knows best. It is
intended that arch code (arm64) will override the default with its own
implementation that can (e.g.) hide certain bits from the core code, or
determine young/dirty status by mixing in state from another source.
Conversion was done using Coccinelle:
----
// $ make coccicheck \
// COCCI=ptepget.cocci \
// SPFLAGS="--include-headers" \
// MODE=patch
virtual patch
@ depends on patch @
pte_t *v;
@@
- *v
+ ptep_get(v)
----
Then reviewed and hand-edited to avoid multiple unnecessary calls to
ptep_get(), instead opting to store the result of a single call in a
variable, where it is correct to do so. This aims to negate any cost of
READ_ONCE() and will benefit arch-overrides that may be more complex.
Included is a fix for an issue in an earlier version of this patch that
was pointed out by kernel test robot. The issue arose because config
MMU=n elides definition of the ptep helper functions, including
ptep_get(). HUGETLB_PAGE=n configs still define a simple
huge_ptep_clear_flush() for linking purposes, which dereferences the ptep.
So when both configs are disabled, this caused a build error because
ptep_get() is not defined. Fix by continuing to do a direct dereference
when MMU=n. This is safe because for this config the arch code cannot be
trying to virtualize the ptes because none of the ptep helpers are
defined.
Link: https://lkml.kernel.org/r/20230612151545.3317766-4-ryan.roberts@arm.com
Reported-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/oe-kbuild-all/202305120142.yXsNEo6H-lkp@intel.com/
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Dave Airlie <airlied@gmail.com>
Cc: Dimitri Sivanich <dimitri.sivanich@hpe.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Lorenzo Stoakes <lstoakes@gmail.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
Cc: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: SeongJae Park <sj@kernel.org>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-06-12 23:15:45 +08:00
|
|
|
ptent = ptep_get(pte);
|
|
|
|
|
|
|
|
if (!pte_present(ptent))
|
2018-08-18 06:47:04 +08:00
|
|
|
continue;
|
|
|
|
|
mm: ptep_get() conversion
Convert all instances of direct pte_t* dereferencing to instead use
ptep_get() helper. This means that by default, the accesses change from a
C dereference to a READ_ONCE(). This is technically the correct thing to
do since where pgtables are modified by HW (for access/dirty) they are
volatile and therefore we should always ensure READ_ONCE() semantics.
But more importantly, by always using the helper, it can be overridden by
the architecture to fully encapsulate the contents of the pte. Arch code
is deliberately not converted, as the arch code knows best. It is
intended that arch code (arm64) will override the default with its own
implementation that can (e.g.) hide certain bits from the core code, or
determine young/dirty status by mixing in state from another source.
Conversion was done using Coccinelle:
----
// $ make coccicheck \
// COCCI=ptepget.cocci \
// SPFLAGS="--include-headers" \
// MODE=patch
virtual patch
@ depends on patch @
pte_t *v;
@@
- *v
+ ptep_get(v)
----
Then reviewed and hand-edited to avoid multiple unnecessary calls to
ptep_get(), instead opting to store the result of a single call in a
variable, where it is correct to do so. This aims to negate any cost of
READ_ONCE() and will benefit arch-overrides that may be more complex.
Included is a fix for an issue in an earlier version of this patch that
was pointed out by kernel test robot. The issue arose because config
MMU=n elides definition of the ptep helper functions, including
ptep_get(). HUGETLB_PAGE=n configs still define a simple
huge_ptep_clear_flush() for linking purposes, which dereferences the ptep.
So when both configs are disabled, this caused a build error because
ptep_get() is not defined. Fix by continuing to do a direct dereference
when MMU=n. This is safe because for this config the arch code cannot be
trying to virtualize the ptes because none of the ptep helpers are
defined.
Link: https://lkml.kernel.org/r/20230612151545.3317766-4-ryan.roberts@arm.com
Reported-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/oe-kbuild-all/202305120142.yXsNEo6H-lkp@intel.com/
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Dave Airlie <airlied@gmail.com>
Cc: Dimitri Sivanich <dimitri.sivanich@hpe.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Lorenzo Stoakes <lstoakes@gmail.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naoya Horiguchi <naoya.horiguchi@nec.com>
Cc: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: SeongJae Park <sj@kernel.org>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-06-12 23:15:45 +08:00
|
|
|
if (WARN_ON(!kasan_early_shadow_page_entry(ptent)))
|
2018-08-18 06:47:04 +08:00
|
|
|
continue;
|
|
|
|
pte_clear(&init_mm, addr, pte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_remove_pmd_table(pmd_t *pmd, unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next, pmd++) {
|
|
|
|
pte_t *pte;
|
|
|
|
|
|
|
|
next = pmd_addr_end(addr, end);
|
|
|
|
|
|
|
|
if (!pmd_present(*pmd))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (kasan_pte_table(*pmd)) {
|
|
|
|
if (IS_ALIGNED(addr, PMD_SIZE) &&
|
2021-01-24 13:01:25 +08:00
|
|
|
IS_ALIGNED(next, PMD_SIZE)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
pmd_clear(pmd);
|
2021-01-24 13:01:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
pte = pte_offset_kernel(pmd, addr);
|
|
|
|
kasan_remove_pte_table(pte, addr, next);
|
|
|
|
kasan_free_pte(pte_offset_kernel(pmd, 0), pmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_remove_pud_table(pud_t *pud, unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next, pud++) {
|
|
|
|
pmd_t *pmd, *pmd_base;
|
|
|
|
|
|
|
|
next = pud_addr_end(addr, end);
|
|
|
|
|
|
|
|
if (!pud_present(*pud))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (kasan_pmd_table(*pud)) {
|
|
|
|
if (IS_ALIGNED(addr, PUD_SIZE) &&
|
2021-01-24 13:01:25 +08:00
|
|
|
IS_ALIGNED(next, PUD_SIZE)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
pud_clear(pud);
|
2021-01-24 13:01:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
pmd = pmd_offset(pud, addr);
|
|
|
|
pmd_base = pmd_offset(pud, 0);
|
|
|
|
kasan_remove_pmd_table(pmd, addr, next);
|
|
|
|
kasan_free_pmd(pmd_base, pud);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kasan_remove_p4d_table(p4d_t *p4d, unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next, p4d++) {
|
|
|
|
pud_t *pud;
|
|
|
|
|
|
|
|
next = p4d_addr_end(addr, end);
|
|
|
|
|
|
|
|
if (!p4d_present(*p4d))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (kasan_pud_table(*p4d)) {
|
|
|
|
if (IS_ALIGNED(addr, P4D_SIZE) &&
|
2021-01-24 13:01:25 +08:00
|
|
|
IS_ALIGNED(next, P4D_SIZE)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
p4d_clear(p4d);
|
2021-01-24 13:01:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
pud = pud_offset(p4d, addr);
|
|
|
|
kasan_remove_pud_table(pud, addr, next);
|
|
|
|
kasan_free_pud(pud_offset(p4d, 0), p4d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kasan_remove_zero_shadow(void *start, unsigned long size)
|
|
|
|
{
|
|
|
|
unsigned long addr, end, next;
|
|
|
|
pgd_t *pgd;
|
|
|
|
|
|
|
|
addr = (unsigned long)kasan_mem_to_shadow(start);
|
|
|
|
end = addr + (size >> KASAN_SHADOW_SCALE_SHIFT);
|
|
|
|
|
2020-12-23 04:00:35 +08:00
|
|
|
if (WARN_ON((unsigned long)start % KASAN_MEMORY_PER_SHADOW_PAGE) ||
|
|
|
|
WARN_ON(size % KASAN_MEMORY_PER_SHADOW_PAGE))
|
2018-08-18 06:47:04 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next) {
|
|
|
|
p4d_t *p4d;
|
|
|
|
|
|
|
|
next = pgd_addr_end(addr, end);
|
|
|
|
|
|
|
|
pgd = pgd_offset_k(addr);
|
|
|
|
if (!pgd_present(*pgd))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (kasan_p4d_table(*pgd)) {
|
|
|
|
if (IS_ALIGNED(addr, PGDIR_SIZE) &&
|
2021-01-24 13:01:25 +08:00
|
|
|
IS_ALIGNED(next, PGDIR_SIZE)) {
|
2018-08-18 06:47:04 +08:00
|
|
|
pgd_clear(pgd);
|
2021-01-24 13:01:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-18 06:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p4d = p4d_offset(pgd, addr);
|
|
|
|
kasan_remove_p4d_table(p4d, addr, next);
|
|
|
|
kasan_free_p4d(p4d_offset(pgd, 0), pgd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int kasan_add_zero_shadow(void *start, unsigned long size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
void *shadow_start, *shadow_end;
|
|
|
|
|
|
|
|
shadow_start = kasan_mem_to_shadow(start);
|
|
|
|
shadow_end = shadow_start + (size >> KASAN_SHADOW_SCALE_SHIFT);
|
|
|
|
|
2020-12-23 04:00:35 +08:00
|
|
|
if (WARN_ON((unsigned long)start % KASAN_MEMORY_PER_SHADOW_PAGE) ||
|
|
|
|
WARN_ON(size % KASAN_MEMORY_PER_SHADOW_PAGE))
|
2018-08-18 06:47:04 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-28 16:30:01 +08:00
|
|
|
ret = kasan_populate_early_shadow(shadow_start, shadow_end);
|
2018-08-18 06:47:04 +08:00
|
|
|
if (ret)
|
2021-01-24 13:01:29 +08:00
|
|
|
kasan_remove_zero_shadow(start, size);
|
2018-08-18 06:47:04 +08:00
|
|
|
return ret;
|
2015-08-13 13:37:24 +08:00
|
|
|
}
|