Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp49161pxb; Tue, 12 Jan 2021 19:41:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJwa3ofZuXk20Yv2gnkGJ9XP8aDf9SIt8Ni5PHoBF/+EuRnPZXayA3Ma2i2R5V0iBoyJn0St X-Received: by 2002:a17:907:7251:: with SMTP id ds17mr101615ejc.448.1610509302056; Tue, 12 Jan 2021 19:41:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610509302; cv=none; d=google.com; s=arc-20160816; b=jIjLGswj1B9caycRB+LRRfUC4/RPGMDrXM6hMvpTjXHZLV/7gRIzDVnIQ37coLmvBr 0mGlL2GrXk659KxRUuzqFpgUJz70MHjLG8FQnMHRcl7MVCmFUkDGfKrqMywWnsF+fVWG Tae7IK3g3Vv/adfx950LXJS2MFmU3vlHYNhRQuWMCYLx7FcE/VhZ5YDFlWdeZlZRlohz TwS/0pRf3Z2Z4c1zVgBZR6cLwzQW77HMuUQgI6Yx0iJcK669TPRtDUoF1xp21M7E44Y+ DO7WudYabACNJ8YPL8+nOkoRDJqzN+wlmvvKpTE5lkxYrzXKQoVqUMYbPa6wUELE3ldT hL5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=8K2vjvGFwXzbsGBnQHMJDkfwmWttprDaTI3MY+9yp8s=; b=Q1nj+RvkTf/vlMCGItt13MhMZ9K0+SPnYpYMvl/ySEA2tKeWMG6ZOUZocz6Wsz3M36 2ryiZcqOW8QXYUgpV9N6POtYYHzYVzimN4XKmxjWaDKC+69Y5Zp644WPVujK+HT1Gogf h6Qr3EdzAzrqo6oQ8g6ou5uDJSXAgS06VhLNx33lZadWbHW8UOLQuiDatjA+2cst1dRk s1vhYKjkdNR1N0yUmvOpL7/ZmiAdhXH+MMQlQrwGOgwAvgxgtBnKt5Q68pC2CM3qMX87 KI8MSZ57r2mEY2tjmtdO4VahA0EUDZ6qv8vAgTCImmYuVomJLuAUzt6quHt4AWe21rgZ /H+Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id md16si316656ejb.563.2021.01.12.19.41.18; Tue, 12 Jan 2021 19:41:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727996AbhAMCei (ORCPT + 99 others); Tue, 12 Jan 2021 21:34:38 -0500 Received: from exmail.andestech.com ([60.248.187.195]:52278 "EHLO ATCSQR.andestech.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725843AbhAMCeh (ORCPT ); Tue, 12 Jan 2021 21:34:37 -0500 Received: from ATCSQR.andestech.com (localhost [127.0.0.2] (may be forged)) by ATCSQR.andestech.com with ESMTP id 10D2QxLF041839 for ; Wed, 13 Jan 2021 10:26:59 +0800 (GMT-8) (envelope-from nylon7@andestech.com) Received: from mail.andestech.com (atcpcs16.andestech.com [10.0.1.222]) by ATCSQR.andestech.com with ESMTP id 10D2Pk8f039670; Wed, 13 Jan 2021 10:25:46 +0800 (GMT-8) (envelope-from nylon7@andestech.com) Received: from atcfdc88.andestech.com (10.0.15.120) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.487.0; Wed, 13 Jan 2021 10:28:27 +0800 From: Nylon Chen To: , CC: , , , , , , , , , , Subject: [PATCH 1/1] riscv/kasan: add KASAN_VMALLOC support Date: Wed, 13 Jan 2021 10:28:22 +0800 Message-ID: <20210113022822.9230-2-nylon7@andestech.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210113022822.9230-1-nylon7@andestech.com> References: <20210113022822.9230-1-nylon7@andestech.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.0.15.120] X-DNSRBL: X-MAIL: ATCSQR.andestech.com 10D2Pk8f039670 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It's reference x86/s390 architecture. So, it's don't map the early shadow page to cover VMALLOC space. Prepopulate top level page table for the range that would otherwise be empty. lower levels are filled dynamically upon memory allocation while booting. Signed-off-by: Nylon Chen Signed-off-by: Nick Hu --- arch/riscv/Kconfig | 1 + arch/riscv/mm/kasan_init.c | 66 +++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 81b76d44725d..15a2c8088bbe 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -57,6 +57,7 @@ config RISCV select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if MMU && 64BIT + select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT select HAVE_ARCH_KGDB select HAVE_ARCH_KGDB_QXFER_PKT select HAVE_ARCH_MMAP_RND_BITS if MMU diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c index 12ddd1f6bf70..ee332513d728 100644 --- a/arch/riscv/mm/kasan_init.c +++ b/arch/riscv/mm/kasan_init.c @@ -9,6 +9,19 @@ #include #include #include +#include + +static __init void *early_alloc(size_t size, int node) +{ + void *ptr = memblock_alloc_try_nid(size, size, + __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, node); + + if (!ptr) + panic("%pS: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", + __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); + + return ptr; +} extern pgd_t early_pg_dir[PTRS_PER_PGD]; asmlinkage void __init kasan_early_init(void) @@ -83,6 +96,49 @@ static void __init populate(void *start, void *end) memset(start, 0, end - start); } +void __init kasan_shallow_populate(void *start, void *end) +{ + unsigned long vaddr = (unsigned long)start & PAGE_MASK; + unsigned long vend = PAGE_ALIGN((unsigned long)end); + unsigned long pfn; + int index; + void *p; + pud_t *pud_dir, *pud_k; + pmd_t *pmd_dir, *pmd_k; + pgd_t *pgd_dir, *pgd_k; + p4d_t *p4d_dir, *p4d_k; + + while (vaddr < vend) { + index = pgd_index(vaddr); + pfn = csr_read(CSR_SATP) & SATP_PPN; + pgd_dir = (pgd_t *)pfn_to_virt(pfn) + index; + pgd_k = init_mm.pgd + index; + pgd_dir = pgd_offset_k(vaddr); + set_pgd(pgd_dir, *pgd_k); + + p4d_dir = p4d_offset(pgd_dir, vaddr); + p4d_k = p4d_offset(pgd_k,vaddr); + + vaddr = (vaddr + PUD_SIZE) & PUD_MASK; + pud_dir = pud_offset(p4d_dir, vaddr); + pud_k = pud_offset(p4d_k,vaddr); + + if (pud_present(*pud_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pud_populate(&init_mm, pud_dir, p); + } + + pmd_dir = pmd_offset(pud_dir, vaddr); + pmd_k = pmd_offset(pud_k,vaddr); + set_pmd(pmd_dir, *pmd_k); + if (pmd_present(*pmd_dir)) { + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + pmd_populate(&init_mm, pmd_dir, p); + } + vaddr += PAGE_SIZE; + } +} + void __init kasan_init(void) { phys_addr_t _start, _end; @@ -90,7 +146,15 @@ void __init kasan_init(void) kasan_populate_early_shadow((void *)KASAN_SHADOW_START, (void *)kasan_mem_to_shadow((void *) - VMALLOC_END)); + VMEMMAP_END)); + if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) + kasan_shallow_populate( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); + else + kasan_populate_early_shadow( + (void *)kasan_mem_to_shadow((void *)VMALLOC_START), + (void *)kasan_mem_to_shadow((void *)VMALLOC_END)); for_each_mem_range(i, &_start, &_end) { void *start = (void *)_start; -- 2.17.1