From patchwork Wed Feb 14 12:29:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13556552 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8994CC48BC3 for ; Wed, 14 Feb 2024 13:46:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=M4R/Svlk5P7z/bNEavLS7JtNoJc1MIgBqO46LyLqCK0=; b=vRwfoJTjNvZQT6CpPi+qLDn+oW fiGX3ClXiAD0C+cZheBpjUsxiRONs/NI08rUR/TDKNxVGNUsNhZCYJU3v8XDXUu9XVfiTqXz3Ba5w UeZ/5WYSAhKC42zxIKqgcBdMjJC29BbVh5+PkyxFuFsXfkcVn/ewZfwOtbj5PNxK1aK9dE0VC/bsz BOLvghxrH9DPzhlq/Z6kuv6EsQ6mPURXcRQS2wxEFFle3UxdSiZUKjzLuaUFiwqzfXmEVAKrIzNPJ 9YIJG98tZP2o0t21LHlD55lA9P+nUA1aDjZBFTNgv/SCIwGgjRHhokbE/Sfb7jd5s7I7PHBHEtzh7 tZHfOkSA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1raFaC-0000000D3jy-0nuO; Wed, 14 Feb 2024 13:45:52 +0000 Received: from mail-wm1-f74.google.com ([209.85.128.74]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1raEPk-0000000Cp0M-0oet for linux-arm-kernel@lists.infradead.org; Wed, 14 Feb 2024 12:31:16 +0000 Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-41082621642so25907985e9.1 for ; Wed, 14 Feb 2024 04:30:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707913857; x=1708518657; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dHzoJTSpME10fT7efs3c2Z+eBzrf+W2skzONvU/iPLI=; b=JkgR+yddIlxKVfilhecBpMKlcITG+SPqRze0QA39UH6sC23Tje+C4TPpOriEDo8Wm9 FmSV5hyLXSSG29bwVKeAaClEYXAbmsOb49pUzK5lHcYAOgYkMURRacoN8TD4u9L5T9DR RH54WiLfQe4+0P+RDjnm6Sk2GL+8Ab8uIJS3LUV4Lk01t+oiq2lCJ+tLmNs+KQHZDQIE tbqeexDr4LMN84LnQLuoc2T+UAIt91wrX7WxIJgug9WX3vFWSkhfBo35hPYb96LM8o4j fERZ//Z3twBa3iESwFhcNuFU5k9dnGCi6cpXylWJp5YF04rhbSWYwcwUPGhdn1ipGYb9 JNtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707913857; x=1708518657; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dHzoJTSpME10fT7efs3c2Z+eBzrf+W2skzONvU/iPLI=; b=qgPnoSVSCUdw/dGp5WnFKqOIKirLkZthAP/ls4SxNppHbH31z43+pdIW9+bAIC2+Qs rcVbnXgwbqNOgq4y+B7hk8upz5Y9/0Ak/3QCIkFc4+sI4czuxonAJM65O9LqYC8i8egP gWYSgFMEimV7a7t0yvKnn5ANcskR4J6wJChtz1CEcE/i/PTkSYI2cu9gIZGKowOx3Hsd tR5L31Zj+hIaR+zASsH7MIL1BXVQ/HRYwRNIAljs004f4C40cs8DvCicx7+i+/PuszjS bj7VJtO0BGLLS53ZRCEEDbB+NQi8VzgpljB2TAYhF3YLuykuJzjzBrb/CRdWvsJ2m+O3 dOsg== X-Gm-Message-State: AOJu0YwyY0GloUTp78UmKkc8Z/rhy4yD9xxnsH3kCBiF0w+ir/eftUxC l7ujC7g9VeyY/+mhwvzbdbvQ2zyPcMsSmzLIWCaO0j1QGBCgQ4nGCn4xtz8H7B9PuZJG8p1m4/K x2/EtRj6eX/Th5cy00WmhDm5W/w4cXrw2JICLs23oIxjo7DVvz7puBlAt2uEyZXjWql7DLGUJco F7nA2C8TRte3qQyEMh7Me9FDNfbNXFXX5J3ZsnAdPv X-Google-Smtp-Source: AGHT+IG7V/6yak0oYBtkkSR8n6QBnoQqvh4yuYpFiRdf0f5mVGuF7g+sHJBBArb+cfltYOGiOjxyWIpu X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:54e3:b0:411:eed4:a396 with SMTP id jb3-20020a05600c54e300b00411eed4a396mr5090wmb.2.1707913856788; Wed, 14 Feb 2024 04:30:56 -0800 (PST) Date: Wed, 14 Feb 2024 13:29:20 +0100 In-Reply-To: <20240214122845.2033971-45-ardb+git@google.com> Mime-Version: 1.0 References: <20240214122845.2033971-45-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5970; i=ardb@kernel.org; h=from:subject; bh=bM2TkRl5rFMQ7ywOQnXc9Yw6neLXbIQxwtUHp5mgOM0=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfXMJuV5Ae+m3+2SZemOrj39p2QK54pPIYaJ2yZ//Z+jw 5L7ReZPRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZhIbwUjw7muWRaFbMXMfkm6 crULn07RtmYv38y9fvrjdY+E+02v5TD801mTOsE958opNc1N2/3Vfr+Ne+H27WnKZ8EJkvPOuE9 9yAAA X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214122845.2033971-79-ardb+git@google.com> Subject: [PATCH v8 34/43] arm64: mm: Add 5 level paging support to fixmap and swapper handling From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240214_043100_536359_5AB50903 X-CRM114-Status: GOOD ( 15.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ard Biesheuvel Add support for using 5 levels of paging in the fixmap, as well as in the kernel page table handling code which uses fixmaps internally. This also handles the case where a 5 level build runs on hardware that only supports 4 levels of paging. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/fixmap.h | 1 + arch/arm64/include/asm/pgtable.h | 45 ++++++++++++++++--- arch/arm64/mm/fixmap.c | 2 +- arch/arm64/mm/mmu.c | 47 ++++++++++++++++++-- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h index 8aabd45e9a13..87e307804b99 100644 --- a/arch/arm64/include/asm/fixmap.h +++ b/arch/arm64/include/asm/fixmap.h @@ -87,6 +87,7 @@ enum fixed_addresses { FIX_PTE, FIX_PMD, FIX_PUD, + FIX_P4D, FIX_PGD, __end_of_fixed_addresses diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 7eb2b933ed3c..3d7fb3cde83d 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -621,12 +621,12 @@ static inline bool pud_table(pud_t pud) { return true; } PUD_TYPE_TABLE) #endif -extern pgd_t init_pg_dir[PTRS_PER_PGD]; +extern pgd_t init_pg_dir[]; extern pgd_t init_pg_end[]; -extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; -extern pgd_t idmap_pg_dir[PTRS_PER_PGD]; -extern pgd_t tramp_pg_dir[PTRS_PER_PGD]; -extern pgd_t reserved_pg_dir[PTRS_PER_PGD]; +extern pgd_t swapper_pg_dir[]; +extern pgd_t idmap_pg_dir[]; +extern pgd_t tramp_pg_dir[]; +extern pgd_t reserved_pg_dir[]; extern void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd); @@ -891,12 +891,47 @@ static inline p4d_t *p4d_offset(pgd_t *pgdp, unsigned long addr) return p4d_offset_lockless(pgdp, READ_ONCE(*pgdp), addr); } +static inline p4d_t *p4d_set_fixmap(unsigned long addr) +{ + if (!pgtable_l5_enabled()) + return NULL; + return (p4d_t *)set_fixmap_offset(FIX_P4D, addr); +} + +static inline p4d_t *p4d_set_fixmap_offset(pgd_t *pgdp, unsigned long addr) +{ + if (!pgtable_l5_enabled()) + return pgd_to_folded_p4d(pgdp, addr); + return p4d_set_fixmap(p4d_offset_phys(pgdp, addr)); +} + +static inline void p4d_clear_fixmap(void) +{ + if (pgtable_l5_enabled()) + clear_fixmap(FIX_P4D); +} + +/* use ONLY for statically allocated translation tables */ +static inline p4d_t *p4d_offset_kimg(pgd_t *pgdp, u64 addr) +{ + if (!pgtable_l5_enabled()) + return pgd_to_folded_p4d(pgdp, addr); + return (p4d_t *)__phys_to_kimg(p4d_offset_phys(pgdp, addr)); +} + #define pgd_page(pgd) pfn_to_page(__phys_to_pfn(__pgd_to_phys(pgd))) #else static inline bool pgtable_l5_enabled(void) { return false; } +/* Match p4d_offset folding in */ +#define p4d_set_fixmap(addr) NULL +#define p4d_set_fixmap_offset(p4dp, addr) ((p4d_t *)p4dp) +#define p4d_clear_fixmap() + +#define p4d_offset_kimg(dir,addr) ((p4d_t *)dir) + #endif /* CONFIG_PGTABLE_LEVELS > 4 */ #define pgd_ERROR(e) \ diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c index 9404f282f829..d22506e9c7fd 100644 --- a/arch/arm64/mm/fixmap.c +++ b/arch/arm64/mm/fixmap.c @@ -104,7 +104,7 @@ void __init early_fixmap_init(void) unsigned long end = FIXADDR_TOP; pgd_t *pgdp = pgd_offset_k(addr); - p4d_t *p4dp = p4d_offset(pgdp, addr); + p4d_t *p4dp = p4d_offset_kimg(pgdp, addr); early_fixmap_init_pud(p4dp, addr, end); } diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index d30ae4d3fdd9..8e5b3a7c5afd 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -313,15 +313,14 @@ static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, } while (addr = next, addr != end); } -static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, +static void alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end, phys_addr_t phys, pgprot_t prot, phys_addr_t (*pgtable_alloc)(int), int flags) { unsigned long next; - pud_t *pudp; - p4d_t *p4dp = p4d_offset(pgdp, addr); p4d_t p4d = READ_ONCE(*p4dp); + pud_t *pudp; if (p4d_none(p4d)) { p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN; @@ -369,6 +368,46 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, pud_clear_fixmap(); } +static void alloc_init_p4d(pgd_t *pgdp, unsigned long addr, unsigned long end, + phys_addr_t phys, pgprot_t prot, + phys_addr_t (*pgtable_alloc)(int), + int flags) +{ + unsigned long next; + pgd_t pgd = READ_ONCE(*pgdp); + p4d_t *p4dp; + + if (pgd_none(pgd)) { + pgdval_t pgdval = PGD_TYPE_TABLE | PGD_TABLE_UXN; + phys_addr_t p4d_phys; + + if (flags & NO_EXEC_MAPPINGS) + pgdval |= PGD_TABLE_PXN; + BUG_ON(!pgtable_alloc); + p4d_phys = pgtable_alloc(P4D_SHIFT); + __pgd_populate(pgdp, p4d_phys, pgdval); + pgd = READ_ONCE(*pgdp); + } + BUG_ON(pgd_bad(pgd)); + + p4dp = p4d_set_fixmap_offset(pgdp, addr); + do { + p4d_t old_p4d = READ_ONCE(*p4dp); + + next = p4d_addr_end(addr, end); + + alloc_init_pud(p4dp, addr, next, phys, prot, + pgtable_alloc, flags); + + BUG_ON(p4d_val(old_p4d) != 0 && + p4d_val(old_p4d) != READ_ONCE(p4d_val(*p4dp))); + + phys += next - addr; + } while (p4dp++, addr = next, addr != end); + + p4d_clear_fixmap(); +} + static void __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys, unsigned long virt, phys_addr_t size, pgprot_t prot, @@ -391,7 +430,7 @@ static void __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys, do { next = pgd_addr_end(addr, end); - alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc, + alloc_init_p4d(pgdp, addr, next, phys, prot, pgtable_alloc, flags); phys += next - addr; } while (pgdp++, addr = next, addr != end);