Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4179560ybb; Tue, 7 Apr 2020 02:19:33 -0700 (PDT) X-Google-Smtp-Source: APiQypIMEnTWo8KHjojpgnwVZZBEfaiSE1w362X4zWIb33GeNrZ0PbX0HtiaF/zTqBqSA+htevTD X-Received: by 2002:a9d:525:: with SMTP id 34mr741468otw.80.1586251172819; Tue, 07 Apr 2020 02:19:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586251172; cv=none; d=google.com; s=arc-20160816; b=eaVwvENFgf0D2aaIU6U/3uwPuSbL83N8XEqPBKWmrRgSfnK5J1ztPzB1bt7My6ZPPr bfaiS3en6fkWfv7CqafnLv96hPPwxOwGE+2aK3flEy88Wz3tKFgBMz7PnD/oVnSOLqOi eKa1wINhX9P5ruceM1rRAisduA2LFTpZZmr55Zcsmpe303eShr7dXdM2OwUqzlMsEZmr yqO904Bt9oUYG7mSmIo0yntvv4I8abd3BcqSmS7ep9WzSr5nHlgh+HNiL1ccHBQzIfVN RTc/eHJMN58+AaLmpyUrMlG03uRP4d7TyV4a2wKoPX8K+juhC3ZVf1mfdgbmjbN6sONn nCtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=D77AbQbLILoEautT7eBJR6pylqABbqtx1s3GXkCGPvU=; b=ujdnuawo7quImSyOf5Ellkd8dw7To28ZYohd4fAPAexKFcaBSykBnwYi4rtEgRO7u8 nv8hqgph08U39ZJSKMJVIlCmEHK3FEgOcPeQAykXXMOaCiszn4po6QJk6UVtS59pNkVI L+sxfDedecY0T23CPgvnTaELmtTeoEiEMlF/mxp9tpbMNPYBZCMQ+lrhdQa0yiAJPG1g HnwQ5w875ehx035ZokVEpsCtMGf4nSgtn7TkHeYU8tAIDapRHXIba8vTtqCk/ZB5IegC JeNyxL8kedfxO0ZZZKybjfOKWqiw5cwqy8vtrZixQISKkIwMIstGD2xaz2e674OFcTCo aM2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=L8h8JHWP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n22si1005400otq.10.2020.04.07.02.19.21; Tue, 07 Apr 2020 02:19:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=L8h8JHWP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726720AbgDGJRf (ORCPT + 99 others); Tue, 7 Apr 2020 05:17:35 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:40830 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725883AbgDGJRe (ORCPT ); Tue, 7 Apr 2020 05:17:34 -0400 Received: by mail-oi1-f194.google.com with SMTP id y71so871389oia.7 for ; Tue, 07 Apr 2020 02:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=D77AbQbLILoEautT7eBJR6pylqABbqtx1s3GXkCGPvU=; b=L8h8JHWPPE5e5IjKHj/KIrGUW7OQSTxyIdoYT0avlrAxkQPITdHDJTCss8bsl6BDVo Bv6BYIQZtU56B8GM2b23qbu4M3zaRj7HqQcUSVsJ7gb+IaWtYK8RsCUQBAo93GBtmvcx /e6n3xQUuo28Leh45AXBUL7HOcHn2lKhbwCZARkCSnCZz1brOLsHEgZiThmFPUt69rIb yrcRpoEY+NM2dP5So+XBvyqvoTrGcT3tEMVH82rAG2eKeSiKuLYXolsPDZDImYKl3R45 5IpPxLz4n4rTGmQvGoJe6y//E04Y2SU92MpczmEiKdtABlx96GLPxoXlaGY2RyAMAt4+ Ij1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=D77AbQbLILoEautT7eBJR6pylqABbqtx1s3GXkCGPvU=; b=ue4ESYVju2XyHvsmk2S/om5KUu2CB06g7bXLEI+UbU0a5RFMpczl+gHlAvWDg3sI/l Be7zWlN6ybgtZ8nKlU+KPKQk1/ndQi4DnTvrQwVQkl1VkZ5JSkVA5g/06d3IybH2aV7G El1sVbsBFpL45rqqYtKuhuqV5EB1qDm3g44275Bfa7qJ/nkMrGqhPrk1PJgu2+bwl6tT fQJimfrW7tVi+blOUghgaO/MinHJW90gfEU9PZk4LLKlaSFjrRAMQ82x8/xgRW7Lygzi y96NRV7Ti/PU5rorMxibAzhDD8ihMa6MMyr+AI4/zCHB1GYHB6+bw6GH1B22yOFhTMN0 273A== X-Gm-Message-State: AGi0PuaQqSFYoJAZ665eZFXf5Ulk9V55OoUeICszE5jDardaVQZqY+kV fPnh4tZ+tn9IqGsJbpAnXX5DuBzR/MQhMnQDeBzxwfXq X-Received: by 2002:a05:6808:a08:: with SMTP id n8mr929598oij.91.1586251052494; Tue, 07 Apr 2020 02:17:32 -0700 (PDT) MIME-Version: 1.0 References: <286a940ef8fbc4480c63679271eea440d167a258.1584352425.git.zong.li@sifive.com> <507f88a2-63c0-f615-2684-a200dd44c34a@ghiti.fr> In-Reply-To: <507f88a2-63c0-f615-2684-a200dd44c34a@ghiti.fr> From: Zong Li Date: Tue, 7 Apr 2020 17:17:22 +0800 Message-ID: Subject: Re: [PATCH RFC 2/8] riscv/kaslr: introduce functions to clear page table To: Alex Ghiti Cc: Palmer Dabbelt , Paul Walmsley , linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 7, 2020 at 1:09 PM Alex Ghiti wrote: > > On 3/24/20 3:30 AM, Zong Li wrote: > > In KASLR, we need to re-create page table after getting a random > > destination. Introduce clear function to clear old content. Also, the > > page table entries allow writing value when it's empty, so we have to > > clear the early page table. > > > > This patch is a preparation to support KASLR. > > > > Signed-off-by: Zong Li > > --- > > arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 54 insertions(+) > > > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > > index ace5d74fd939..51e263c04fa2 100644 > > --- a/arch/riscv/mm/init.c > > +++ b/arch/riscv/mm/init.c > > @@ -315,6 +315,7 @@ static void __init create_pmd_mapping(pmd_t *pmdp, > > #define get_pgd_next_virt(__pa) get_pmd_virt(__pa) > > #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ > > create_pmd_mapping(__nextp, __va, __pa, __sz, __prot) > > +#define clear_pgd_next_mapping(__nextp) clear_pmd(__nextp) > > #define fixmap_pgd_next fixmap_pmd > > #else > > #define pgd_next_t pte_t > > @@ -322,6 +323,7 @@ static void __init create_pmd_mapping(pmd_t *pmdp, > > #define get_pgd_next_virt(__pa) get_pte_virt(__pa) > > #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ > > create_pte_mapping(__nextp, __va, __pa, __sz, __prot) > > +#define clear_pgd_next_mapping(__nextp) clear_pte(__nextp) > > #define fixmap_pgd_next fixmap_pte > > #endif > > > > @@ -361,6 +363,58 @@ static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) > > return PMD_SIZE; > > } > > > > +#ifdef CONFIG_RANDOMIZE_BASE > > +static void __init clear_pte(pte_t *ptep) > > +{ > > + unsigned int i; > > + > > + for (i = 0; i < PTRS_PER_PTE; i++) > > + if (!pte_none(ptep[i])) > > + ptep[i] = __pte(0); > > +} > > + > > +static void __init clear_pmd(pmd_t *pmdp) > > +{ > > + unsigned int i; > > + pte_t *ptep; > > + phys_addr_t pte_phys; > > + uintptr_t kaslr_offset = get_kaslr_offset(); > > + > > + for (i = 0; i < PTRS_PER_PMD; i++) > > + if (!pmd_none(pmdp[i])) { > > + if (pmd_leaf(pmdp[i])) { > > + pmd_clear(&pmdp[i]); > > + } else { > > + pte_phys = PFN_PHYS(_pmd_pfn(pmdp[i])); > > + ptep = get_pte_virt(pte_phys + kaslr_offset); > > + clear_pte(ptep); > > + pmd_clear(&pmdp[i]); > > + } > > + } > > +} > > + > > +static void __init clear_pgd(pgd_t *pgdp) > > +{ > > + unsigned int i; > > + pgd_next_t *nextp; > > + phys_addr_t next_phys; > > + uintptr_t kaslr_offset = get_kaslr_offset(); > > + > > + for (i = 0; i < PTRS_PER_PGD; i++) > > + if (pgd_val(pgdp[i]) != 0) { > > + if (pgd_leaf(pgd_val(pgdp[i]))) { > > + set_pgd(&pgdp[i], __pgd(0)); > > + } else { > > + next_phys = PFN_PHYS(_pgd_pfn(pgdp[i])); > > + nextp = get_pgd_next_virt(next_phys + > > + kaslr_offset); > > + clear_pgd_next_mapping(nextp); > > + set_pgd(&pgdp[i], __pgd(0)); > > + } > > + } > > +} > > +#endif > > + > > /* > > * setup_vm() is called from head.S with MMU-off. > > * > > > > If this is only for clearing early page tables, a memset is way easier > as there is only one page per level to clear. > Yes, it's a better way. Thanks. > Alex