Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1416828pxb; Fri, 26 Feb 2021 10:06:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJxyD5u6iawhBA9ExD5TO2+JWtDwkjb8il4reLrFSMMFI30rBHq2dn38cioRfa3D8Jymuk8s X-Received: by 2002:a17:906:d1d5:: with SMTP id bs21mr4749056ejb.242.1614362807579; Fri, 26 Feb 2021 10:06:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614362807; cv=none; d=google.com; s=arc-20160816; b=TvZieiNGR/WmbudVL/bcukOKxdq13AcwNd2uql3+anyIu2Cf7dG2BDi75ESuZ3ExMM BusDCh3U7rNStJE+vWvKqRAqdDYzf43wNtD78joeR3+NR6ZV8xQYjlZbYwPkpfjSIHLf oQbhPCAcpmtHctENImdzjF6m95w+yAhfxKg3UfIY6taYwlLJ/ASlrLmREHRnL0mLcWVy 592x4HNAGVXHJ1vjwYP3ZlzMeVjsK0UOSaqwNJGgEBpJn+sbeJGeqzx4Nwq1CQmhJcnb h0P8O0Orn/FkkFItnF+2fmyUChJWh0uQPR536MUFr0RF0+jSemuQUVgT0izoaoTsVbh0 Cf6Q== 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=wtVhhSACxrBZ1RpGpdjSEdxD6z3ujgmu3ukxhj4wRUU=; b=Ls2N5ecm2T2uh2EPrJx3J0sGvpagvfkvSxCj++yIfdQNUrCQuECoj9RXV0ZQe5Pxt7 ysmMg7ERPIHKCaLbetAmylwLjS4X0+EaQWkXlYtsb2ja4fgLJcXS2nuQlyuDTqV/dpP3 U9YnIlEav0IrhzkxiXDSGcs/SOKC0Uygx8QSiUUvC9ZNYsm/cR86Ff1t4cOYHTuTcn1R 3hiFGx+V5X5atgGX9S+DAMrTJl5ZZ+IYn9olX3HH3dsmj7Pl5p8YhmCQDhcYHzzLB+bp f7y0sbLhk1qhIe1xbv3Dwp/4Yn9Rv7SIp44urDQfREuO+7MeViRpozPpHOzdBo6sqryq Z3wA== 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 z23si6510566edr.483.2021.02.26.10.06.24; Fri, 26 Feb 2021 10:06:47 -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 S230228AbhBZSDB (ORCPT + 99 others); Fri, 26 Feb 2021 13:03:01 -0500 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:58725 "EHLO relay4-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229953AbhBZSDA (ORCPT ); Fri, 26 Feb 2021 13:03:00 -0500 X-Originating-IP: 2.7.49.219 Received: from debian.home (lfbn-lyo-1-457-219.w2-7.abo.wanadoo.fr [2.7.49.219]) (Authenticated sender: alex@ghiti.fr) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 1F829E000B; Fri, 26 Feb 2021 18:01:55 +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 v2] riscv: Improve KASAN_VMALLOC support Date: Fri, 26 Feb 2021 13:01:54 -0500 Message-Id: <20210226180154.31533-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 When KASAN vmalloc region is populated, there is no userspace process and the page table in use is swapper_pg_dir, so there is no need to read SATP. Then we can use the same scheme used by kasan_populate_p*d functions to go through the page table, which harmonizes the code. In addition, make use of set_pgd that goes through all unused page table levels, contrary to p*d_populate functions, which makes this function work whatever the number of page table levels. And finally, make sure the writes to swapper_pg_dir are visible using an sfence.vma. Signed-off-by: Alexandre Ghiti --- Changes in v2: - Quiet kernel test robot warnings about missing prototypes by declaring the introduced functions as static. arch/riscv/mm/kasan_init.c | 61 +++++++++++++------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c index e3d91f334b57..aaa3bdc0ffc0 100644 --- a/arch/riscv/mm/kasan_init.c +++ b/arch/riscv/mm/kasan_init.c @@ -11,18 +11,6 @@ #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) { @@ -155,38 +143,29 @@ static void __init kasan_populate(void *start, void *end) memset(start, KASAN_SHADOW_INIT, end - start); } -void __init kasan_shallow_populate(void *start, void *end) +static void __init kasan_shallow_populate_pgd(unsigned long vaddr, unsigned long end) { - unsigned long vaddr = (unsigned long)start & PAGE_MASK; - unsigned long vend = PAGE_ALIGN((unsigned long)end); - unsigned long pfn; - int index; + unsigned long next; void *p; - pud_t *pud_dir, *pud_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); + 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)); } - vaddr += PAGE_SIZE; - } + } while (pgd_k++, vaddr = next, vaddr != end); +} + +static 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) -- 2.20.1