Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1190238pxb; Fri, 26 Feb 2021 05:02:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJycuhkWXNSmPZSxaTxWp+7jhebdHEBXrQnEe11mCLxSHKc9O71PA22bKoa2Pq8D+xd3dS69 X-Received: by 2002:a17:907:2652:: with SMTP id ar18mr11766ejc.115.1614344526971; Fri, 26 Feb 2021 05:02:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614344526; cv=none; d=google.com; s=arc-20160816; b=VRormTkr5yR92yqU1t4UTAp0dQe+zkItSHoYoYdNL/qOMJYFazFF2Z3Dhfjw74cUGF AQZsbCjCH4eB9RnyrHH32ZQYI261BQwc+S4b4XaviGI0iITpo1M8KJdMXxODHKBxNhF+ d4abEjLqkeJL29QbSp9rC6S5qKdvM8v5L28LTTVa5mKojHztPM4h6EyEok/V4mlI8nh7 ytscxb9rnsqLn6OJfWXSlwsBhp8Glk5jO5SvPq/JWi1IYtw4Yz/kYFhLh3SnpnH/LM1A YCuT4xYXmL9zM0j1jUDl3eFHFWTTCPFzjF9b5JM4cKd4rZA4hVQVwnEnXQew8z9cANuS fAIg== 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=8iVx5TJSSQURCcQ3/XV/bgWPeBBQOqz0YJzvijvskzU=; b=wDlFcMc+YyS3o6J2cKEXmAHWSRH8U7Vzq5wpxfbtn+F6A4KwhNWpw7UDf7PdG6Cnjk UThGmjcbsa5tMpeoab5zOIONrX0m/fLSz1u7W9yQq+nBBv42TU6sZeA0z/J7oaOMggr6 5+vIl8b5zs2qgr1bYwKkU4XIMtP9JhqAXDAVVgGvQ3MAPPX3mYLQpkjrL6UnDCIWI7/i q5Agfe001zoEmGY+iEKdhup7x4jrhC0afrKF4ZylawObBIAE+22b2biMpNe4+Dlxw62y X818pNwZhNhriWfs+R4raySyCgMjY2/QClQmgWkCu/sqDMyFB8eIWgAGm7bH47P/uqWw AFIw== 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 b12si5891255ejq.58.2021.02.26.05.01.42; Fri, 26 Feb 2021 05:02:06 -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 S230144AbhBZNAm (ORCPT + 99 others); Fri, 26 Feb 2021 08:00:42 -0500 Received: from relay5-d.mail.gandi.net ([217.70.183.197]:54875 "EHLO relay5-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229953AbhBZNAj (ORCPT ); Fri, 26 Feb 2021 08:00:39 -0500 X-Originating-IP: 81.185.174.212 Received: from localhost.localdomain (212.174.185.81.rev.sfr.net [81.185.174.212]) (Authenticated sender: alex@ghiti.fr) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 4FAAF1C000A; Fri, 26 Feb 2021 12:59:49 +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: Improve KASAN_VMALLOC support Date: Fri, 26 Feb 2021 07:59:33 -0500 Message-Id: <20210226125933.32023-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 --- arch/riscv/mm/kasan_init.c | 59 ++++++++++++-------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c index e3d91f334b57..b0cee8d35938 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_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); - unsigned long pfn; - int index; - 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); - } - vaddr += PAGE_SIZE; - } + + kasan_shallow_populate_pgd(vaddr, vend); + + local_flush_tlb_all(); } void __init kasan_init(void) -- 2.20.1