Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp264954pxb; Thu, 25 Feb 2021 01:43:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWqCOojo9l1KRPGUpBMqLG/hdq2RA8PPr7DWASr3uNvHkE3+aXD7V1ZUoPWMJtw91F85Vd X-Received: by 2002:a17:906:b819:: with SMTP id dv25mr1896092ejb.476.1614246216117; Thu, 25 Feb 2021 01:43:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614246216; cv=none; d=google.com; s=arc-20160816; b=jZNKVgnw5LjBFIwrjOTcjAHqiGmSC0gxGSVQPnmBSmZ2jCE6fh/AShHLpZUeeyZkWX c/uIMzKE6IVafzI7DfJjt5nTeBaE5qz93sAadPhMQUj+u1eOxtAh/cB6AmuSop6BFvIL mlte28mYf4U6HAFQQKVhFkCKSKJ3Kq9+WqpwLjd5U6mb2GRDR9SmoUG4zeiVXzuYliRz CR5fzmbep+o6zQNNhtYa5zJNsNljHbo9zYs9MFR74Peqc5HecwZvPn+u+3YTU5+5z1Rl GqGwzT0ejhCjwwM07z+rYO/mYfT1mGbRO+babT9YPijSqpiByav0gGZ9zRT8rxKeEeo0 Zoqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=WtLdaRsQDtf5KNwEcl19eocGURxt4ezGs033OQAILNY=; b=iN6uWxmNW7cqijNPPyNqE52r3+rUSQdqR2rNufM/nAs2f36DeJw/Z2Spnqz8jnj8f6 rgkyxLRE8hEGrk5cBoCPjScVLyJBXz6C/wKlOo0Dhahmv3bXp+UhD+jVhOyBjAls/9kw rowJMr2r31Ni/WLAV71fM6ihKEHuV0UvKaa9HoVZCugFDTl2nnnTy6nPCncQR+0hG831 KT0/HlzatpIwd7Aa/hLtyN1n9g/Lhl8115bxhhRaJOpcOCHFD4ifmIObCjwpvWEnWFpB K79D8V4kQh5GzUR0gF9HB7pNxSlRtkL1ShQCnQAS0y5dGXGYib4gHZ9GJy2U+0+VDLEo MZYw== 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 a24si2990102ejy.54.2021.02.25.01.43.11; Thu, 25 Feb 2021 01:43:36 -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 S234811AbhBYHoQ (ORCPT + 99 others); Thu, 25 Feb 2021 02:44:16 -0500 Received: from relay7-d.mail.gandi.net ([217.70.183.200]:53429 "EHLO relay7-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233153AbhBYHnj (ORCPT ); Thu, 25 Feb 2021 02:43:39 -0500 X-Originating-IP: 81.185.161.35 Received: from localhost.localdomain (35.161.185.81.rev.sfr.net [81.185.161.35]) (Authenticated sender: alex@ghiti.fr) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 5FF4F20003; Thu, 25 Feb 2021 07:42:39 +0000 (UTC) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Nylon Chen , Nick Hu , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com Cc: Alexandre Ghiti Subject: [PATCH] riscv: Add KASAN_VMALLOC support Date: Thu, 25 Feb 2021 02:42:27 -0500 Message-Id: <20210225074227.3176-1-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Populate the top-level of the kernel page table to implement KASAN_VMALLOC, lower levels are filled dynamically upon memory allocation at runtime. Co-developed-by: Nylon Chen Signed-off-by: Nylon Chen Co-developed-by: Nick Hu Signed-off-by: Nick Hu Signed-off-by: Alexandre Ghiti --- arch/riscv/Kconfig | 1 + arch/riscv/mm/kasan_init.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 8eadd1cbd524..3832a537c5d6 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 719b6e4d6075..171569df4334 100644 --- a/arch/riscv/mm/kasan_init.c +++ b/arch/riscv/mm/kasan_init.c @@ -142,6 +142,31 @@ static void __init kasan_populate(void *start, void *end) memset(start, KASAN_SHADOW_INIT, end - start); } +void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end) +{ + unsigned long next; + void *p; + pgd_t *pgd_k = pgd_offset_k(vaddr); + + do { + next = pgd_addr_end(vaddr, end); + if (pgd_page_vaddr(*pgd_k) == (unsigned long)lm_alias(kasan_early_shadow_pmd)) { + p = memblock_alloc(PAGE_SIZE, PAGE_SIZE); + set_pgd(pgd_k, pfn_pgd(PFN_DOWN(__pa(p)), PAGE_TABLE)); + } + } while (pgd_k++, vaddr = next, vaddr != end); +} + +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); + + kasan_shallow_populate_pgd(vaddr, vend); + + local_flush_tlb_all(); +} + void __init kasan_init(void) { phys_addr_t _start, _end; @@ -149,7 +174,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.20.1