Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1159123pxa; Thu, 13 Aug 2020 01:59:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4b5fKDEImIdY5YrHX9W1Tvz5s+uMgKzs/F91vCtk8ozVbB2I2KDOn/Dot1V85FjvG9FoN X-Received: by 2002:a17:906:e50:: with SMTP id q16mr3913058eji.155.1597309161781; Thu, 13 Aug 2020 01:59:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597309161; cv=none; d=google.com; s=arc-20160816; b=Q8bVwK7oBKlRGs+yP/E/XIarVnZarh2dx+UfQqMOU6WTApN4jNuTp7sUi9AlX5Lema UizOPdLNY8/4GmfEMex5sagnrC05vkN/y1/r88Mr89Tz/pF5NG+I/Kn27T+4l3gKgogI Bt4vp8upoSpE0xHET7DbBPmuHs5+pqisUW8CPHYvJgFKc03akLpngaJx5SKEEgB1jIfs eloRRlDu6cnNY+cPtT87MB29A1KMBBq0F/6qpDYwhlumcGg3Bws4e29Bbxw8QieCFlRw XpFdcaCYFZq4MZV5NNgUHIjfyNA7k9reKJ5Q7jRHXrYO7rLjTfjoIx4WmFFibrJjaD77 k2rw== 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=7ejOXH1LQTXrGTOxhVaMU2TYELrccVatdEWcGAK3Sjw=; b=b9QiM227rRKwXk6+KR5d/8eNkU+48pPAVy5EO1r2Wuui5Mf/GcAhYrPTnl9GxlNsF6 ydhdNT6EZdgahm10B9zv8B0JwrYA3eoKgmkjZSzHKEkeBAE2IIaGy5tOwv/c77ueLM0e +WUt5optgJENscOP9RA2yH5jE4K5PnPR9hZBKChrPUM9z1a5YJj2+HXxpcX4fa0BvITw Dgx74bS53qElAFddMWCbVisIa7okFZVfcI4hikTvoqhey+S0t3sh/si7SqW5Ddn37+wz xplsOKc+5ECMRSXGBnYCAox1IWSiYomLxUFThIaRIVDDiIMiAprGjcxjIZAq/BD16WZS Y3kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=P0vxc039; 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 v12si2857035ejg.269.2020.08.13.01.58.58; Thu, 13 Aug 2020 01:59:21 -0700 (PDT) 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; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=P0vxc039; 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 S1726072AbgHMI6S (ORCPT + 99 others); Thu, 13 Aug 2020 04:58:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726053AbgHMI6R (ORCPT ); Thu, 13 Aug 2020 04:58:17 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A79DC061757 for ; Thu, 13 Aug 2020 01:58:16 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id g8so4015881wmk.3 for ; Thu, 13 Aug 2020 01:58:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7ejOXH1LQTXrGTOxhVaMU2TYELrccVatdEWcGAK3Sjw=; b=P0vxc039Nc76V/6Rh0lKYHO+QHOD59pM77+iDKGJHEdV1gXadGYcNlamm6GJok7Lcx erNEgkh194Mu2P90joK7JlyIHPQxnUZW8mDcF2HS6T+L45gkx1fqoPO/GvKZ3D2yFBSc kBnDZh31uDw6IJCEf9aP89Ee6TwK0F7xVcrUWFWeHGmk7BibunPOcqRp7ddlTdcbsEPp uWeYrjGmuC3umaZYRvPVjAmViw67Fs8d1CUOuZnpGVyCZD8Po+bcK9HV7xqyAEATcXFy HOTJ7a+qFjtIE3/Ldo5+Z1Yox4Y5EFnDve9qnTUVQ54+7jO7PfJHuhI4cb0dQn/Y7Aic 3qOA== 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=7ejOXH1LQTXrGTOxhVaMU2TYELrccVatdEWcGAK3Sjw=; b=TacbtGe1Vh42zseciWnDEB6US7v6c9Atn9idK+5y2zXcDfEtoWoeKewe47MAzhIcPo +pe4eFRf5oMEI8AsANVbyNwlVXQFn/yCVJqtFMJpOu3k/x29hRCwCK178xpW5sc7edZn dGLfXxSQ8/VwShl4HqxZtW+t/68B8PdSPAuZRU7G5smXlQubGR5ah7Vzi8qRrAraZzMx b+qZJeFZDPg2qTutDofxWm5gt+U6YFWzY6ksYz9rYheNkNFLjH/38CD2rSst7NHFHm7z vUBFE2qzAaRkNmF4hkPC/CEqJ96GHwjDeUsb9W6jP6mLK/IREqI8LgqTS3tW60Hhs+X4 YV3w== X-Gm-Message-State: AOAM531+9sOEx3LNZjjkUS6Sqm0NExkK7LcrjYpgyY34+1++IWbghrzS iHnQkXUDNVq9hEC3kFCVMFGegt5J7/9j+Ibq+Y4ZBA== X-Received: by 2002:a7b:c4d5:: with SMTP id g21mr3596831wmk.185.1597309095228; Thu, 13 Aug 2020 01:58:15 -0700 (PDT) MIME-Version: 1.0 References: <20200812234758.3563-1-atish.patra@wdc.com> <20200812234758.3563-4-atish.patra@wdc.com> In-Reply-To: <20200812234758.3563-4-atish.patra@wdc.com> From: Anup Patel Date: Thu, 13 Aug 2020 14:28:03 +0530 Message-ID: Subject: Re: [PATCH v5 3/9] RISC-V: Implement late mapping page table allocation functions To: Atish Patra Cc: "linux-kernel@vger.kernel.org List" , Albert Ou , Alistair Francis , Andrew Morton , Anup Patel , Ard Biesheuvel , Arvind Sankar , Borislav Petkov , Greentime Hu , Ingo Molnar , Kees Cook , linux-efi , linux-riscv , fwts-devel@lists.ubuntu.com, Mao Han , Masahiro Yamada , Michal Simek , Mike Rapoport , Nick Desaulniers , Palmer Dabbelt , Paul Walmsley , Steven Price , Waiman Long , Will Deacon , Daniel Schaefer , "abner.chang@hpe.com" 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 Thu, Aug 13, 2020 at 5:19 AM Atish Patra wrote: > > Currently, page table setup is done during setup_va_final where fixmap can > be used to create the temporary mappings. The physical frame is allocated > from memblock_alloc_* functions. However, this won't work if page table > mapping needs to be created for a different mm context (i.e. efi mm) at > a later point of time. > > Use generic kernel page allocation function & macros for any mapping > after setup_vm_final. > > Signed-off-by: Atish Patra > --- > arch/riscv/mm/init.c | 130 ++++++++++++++++++++++++++++++++----------- > 1 file changed, 99 insertions(+), 31 deletions(-) > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > index b75ebe8e7a92..d238cdc501ee 100644 > --- a/arch/riscv/mm/init.c > +++ b/arch/riscv/mm/init.c > @@ -32,6 +32,17 @@ extern char _start[]; > void *dtb_early_va __initdata; > uintptr_t dtb_early_pa __initdata; > > +struct pt_alloc_ops { > + pte_t *(*get_pte_virt)(phys_addr_t pa); > + phys_addr_t (*alloc_pte)(uintptr_t va); > +#ifndef __PAGETABLE_PMD_FOLDED > + pmd_t *(*get_pmd_virt)(phys_addr_t pa); > + phys_addr_t (*alloc_pmd)(uintptr_t va); > +#endif > +}; > + > +struct pt_alloc_ops pt_ops; > + > static void __init zone_sizes_init(void) > { > unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, }; > @@ -211,7 +222,6 @@ EXPORT_SYMBOL(pfn_base); > pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; > pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; > pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; > -static bool mmu_enabled; > > #define MAX_EARLY_MAPPING_SIZE SZ_128M > > @@ -234,27 +244,46 @@ void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) > } > } > > -static pte_t *__init get_pte_virt(phys_addr_t pa) > +static inline pte_t *__init get_pte_virt_early(phys_addr_t pa) > { > - if (mmu_enabled) { > - clear_fixmap(FIX_PTE); > - return (pte_t *)set_fixmap_offset(FIX_PTE, pa); > - } else { > - return (pte_t *)((uintptr_t)pa); > - } > + return (pte_t *)((uintptr_t)pa); > } > > -static phys_addr_t __init alloc_pte(uintptr_t va) > +static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa) > +{ > + clear_fixmap(FIX_PTE); > + return (pte_t *)set_fixmap_offset(FIX_PTE, pa); > +} > + > +static inline pte_t *get_pte_virt_late(phys_addr_t pa) > +{ > + return (pte_t *) __va(pa); > +} > + > +static inline phys_addr_t __init alloc_pte_early(uintptr_t va) > { > /* > * We only create PMD or PGD early mappings so we > * should never reach here with MMU disabled. > */ > - BUG_ON(!mmu_enabled); > + BUG(); > +} > > +static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va) > +{ > return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); > } > > +static phys_addr_t alloc_pte_late(uintptr_t va) > +{ > + unsigned long vaddr; > + > + vaddr = __get_free_page(GFP_KERNEL); > + if (!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr))) > + BUG(); > + return __pa(vaddr); > +} > + > static void __init create_pte_mapping(pte_t *ptep, > uintptr_t va, phys_addr_t pa, > phys_addr_t sz, pgprot_t prot) > @@ -279,28 +308,46 @@ pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; > #endif > pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE); > > -static pmd_t *__init get_pmd_virt(phys_addr_t pa) > +static pmd_t *__init get_pmd_virt_early(phys_addr_t pa) > { > - if (mmu_enabled) { > - clear_fixmap(FIX_PMD); > - return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); > - } else { > - return (pmd_t *)((uintptr_t)pa); > - } > + /* Before MMU is enabled */ > + return (pmd_t *)((uintptr_t)pa); > } > > -static phys_addr_t __init alloc_pmd(uintptr_t va) > +static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa) > { > - uintptr_t pmd_num; > + clear_fixmap(FIX_PMD); > + return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); > +} > + > +static pmd_t *get_pmd_virt_late(phys_addr_t pa) > +{ > + return (pmd_t *) __va(pa); > +} > > - if (mmu_enabled) > - return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); > +static phys_addr_t __init alloc_pmd_early(uintptr_t va) > +{ > + uintptr_t pmd_num; > > pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT; > BUG_ON(pmd_num >= NUM_EARLY_PMDS); > return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD]; > } > > +static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va) > +{ > + return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); > +} > + > +static phys_addr_t alloc_pmd_late(uintptr_t va) > +{ > + unsigned long vaddr; > + > + vaddr = __get_free_page(GFP_KERNEL); > + BUG_ON(!vaddr); > + return __pa(vaddr); > +} > + > static void __init create_pmd_mapping(pmd_t *pmdp, > uintptr_t va, phys_addr_t pa, > phys_addr_t sz, pgprot_t prot) > @@ -316,28 +363,28 @@ static void __init create_pmd_mapping(pmd_t *pmdp, > } > > if (pmd_none(pmdp[pmd_idx])) { > - pte_phys = alloc_pte(va); > + pte_phys = pt_ops.alloc_pte(va); > pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE); > - ptep = get_pte_virt(pte_phys); > + ptep = pt_ops.get_pte_virt(pte_phys); > memset(ptep, 0, PAGE_SIZE); > } else { > pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx])); > - ptep = get_pte_virt(pte_phys); > + ptep = pt_ops.get_pte_virt(pte_phys); > } > > create_pte_mapping(ptep, va, pa, sz, prot); > } > > #define pgd_next_t pmd_t > -#define alloc_pgd_next(__va) alloc_pmd(__va) > -#define get_pgd_next_virt(__pa) get_pmd_virt(__pa) > +#define alloc_pgd_next(__va) pt_ops.alloc_pmd(__va) > +#define get_pgd_next_virt(__pa) pt_ops.get_pmd_virt(__pa) > #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ > create_pmd_mapping(__nextp, __va, __pa, __sz, __prot) > #define fixmap_pgd_next fixmap_pmd > #else > #define pgd_next_t pte_t > -#define alloc_pgd_next(__va) alloc_pte(__va) > -#define get_pgd_next_virt(__pa) get_pte_virt(__pa) > +#define alloc_pgd_next(__va) pt_ops.alloc_pte(__va) > +#define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa) > #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ > create_pte_mapping(__nextp, __va, __pa, __sz, __prot) > #define fixmap_pgd_next fixmap_pte > @@ -421,6 +468,12 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) > BUG_ON((load_pa % map_size) != 0); > BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE); > > + pt_ops.alloc_pte = alloc_pte_early; > + pt_ops.get_pte_virt = get_pte_virt_early; > +#ifndef __PAGETABLE_PMD_FOLDED > + pt_ops.alloc_pmd = alloc_pmd_early; > + pt_ops.get_pmd_virt = get_pmd_virt_early; > +#endif > /* Setup early PGD for fixmap */ > create_pgd_mapping(early_pg_dir, FIXADDR_START, > (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE); > @@ -497,9 +550,16 @@ static void __init setup_vm_final(void) > phys_addr_t pa, start, end; > struct memblock_region *reg; > > - /* Set mmu_enabled flag */ > - mmu_enabled = true; > - > + /** > + * MMU is enabled at this point. But page table setup is not complete yet. > + * fixmap page table alloc functions should be used at this point > + */ > + pt_ops.alloc_pte = alloc_pte_fixmap; > + pt_ops.get_pte_virt = get_pte_virt_fixmap; > +#ifndef __PAGETABLE_PMD_FOLDED > + pt_ops.alloc_pmd = alloc_pmd_fixmap; > + pt_ops.get_pmd_virt = get_pmd_virt_fixmap; > +#endif > /* Setup swapper PGD for fixmap */ > create_pgd_mapping(swapper_pg_dir, FIXADDR_START, > __pa_symbol(fixmap_pgd_next), > @@ -533,6 +593,14 @@ static void __init setup_vm_final(void) > /* Move to swapper page table */ > csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE); > local_flush_tlb_all(); > + > + /* generic page allocation functions must be used to setup page table */ > + pt_ops.alloc_pte = alloc_pte_late; > + pt_ops.get_pte_virt = get_pte_virt_late; > +#ifndef __PAGETABLE_PMD_FOLDED > + pt_ops.alloc_pmd = alloc_pmd_late; > + pt_ops.get_pmd_virt = get_pmd_virt_late; > +#endif > } > #else > asmlinkage void __init setup_vm(uintptr_t dtb_pa) > -- > 2.24.0 > Looks good to me. Reviewed-by: Anup Patel Regards, Anup