From patchwork Thu Jun 9 17:43:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 12875870 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 3A022C433EF for ; Thu, 9 Jun 2022 17:44:54 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=GPBxwLkaMGmtSi6M5kV66ykM6H8nv5U1RPutjU2w21g=; b=1RD8UfA8N9GhAk 6cQbpWAtibmEF3U3AZNwuCFcX6OdZS9ha6jHIgBXsxP2PFzESQ5J+T/RPXV9Hy5NNkD3n4oktt+bQ xEP378UTIK2bTjb024n21TrkFrHnDIlQXLrrQImywCKAk4FpE+BhjQysSpX5pdmWAa1CBvEoC3cip nypIKyv5sDt7mECQFl7cLvaEHey1d8bTyKis+oiAYg8oyaIOwDwfLU8b4N9UYxHWzadypB2P3I/9P LEYPri2x/tTV9OP3oZ7S0higyo51FZ0bpeW1lo/jjbQfq2IOm8pX9p5cL2L5zLHtxKKM3ncpq4y0c FJsYEYwUaWxT4wgTiJIw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nzMC8-003GVY-8p; Thu, 09 Jun 2022 17:43:44 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nzMBw-003GPV-NU for linux-arm-kernel@lists.infradead.org; Thu, 09 Jun 2022 17:43:34 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0395761BED; Thu, 9 Jun 2022 17:43:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAFD2C341C0; Thu, 9 Jun 2022 17:43:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654796611; bh=rSWIgoS95v9XbMumsDTJrZZcltFcUDi3tRgBJdAZlMo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AO6LQ63gntoHLtEH/MX0eRRWw/IMys1z4oCVg3m2W/ciw4sxntkO9TirUzlTgobhY IQGUMfT6HA+c1JPs+piAurrZuM6PMOPBX1DBv5D2/iakVjx8FxoMDWQ3wS0Qp0F1VW sVE1ILfz+xKs3ZQJTJPlaRYxiPxW3iRb/0PlK4f8mIL2FmuyjEHUTZxElNgheXIHF3 bU0LvolkyvhijBhetjBKQCk20oi/644k0vrT18qANveZVzoRj896XNEX/h4vg1TiY+ sKxWKcA5j4ddLW0SuQtCoYl9oV0Kz7KZ9O0Q4mh6FbW39iRimNrzgnSakRShz6Xpv2 CzUPF26HaW2Og== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , will@kernel.org, maz@kernel.org, mark.rutland@arm.com, catalin.marinas@arm.com, keescook@chromium.org Subject: [PATCH v5 1/2] arm64: kpti-ng: simplify page table traversal logic Date: Thu, 9 Jun 2022 19:43:19 +0200 Message-Id: <20220609174320.4035379-2-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220609174320.4035379-1-ardb@kernel.org> References: <20220609174320.4035379-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5177; h=from:subject; bh=rSWIgoS95v9XbMumsDTJrZZcltFcUDi3tRgBJdAZlMo=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBiojE0SsuM40QdYDfYmwbNQLJBBkJq58a5uorv5Nif oVR2AHOJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYqIxNAAKCRDDTyI5ktmPJJ43C/ 4tPBSHhhfclzWLEPHoQVBWzclNPzEe+80z3lJINkwPbC+XbxmgaWIzc/9JbcSQNXI8XzYxezBGzhLp DA2d0nTDESU9VUr7VrsOl/hp0bR31Lf6K/Fy10aymZT6rq5ZI9Vhe2Ah8kmIme7i+DccTQIIw8A/iW Fnpug0YvRiEF6QYF0kUEQYToDuz4U5RXMpdg18z+vb4SdmUpkVxR1B9nZR35gNZv4Bftikv+ZmUbom G8Rt4s5FopVKXyjONKpsRu/oLXABjTKZvSEU7IR/XOXO8vDpq+h7m/ZCMuc2aQx1WhV+sCgH7moVa6 7Vxqd45bJIuRvIRNPIvJFdD9fnUNPFQ/vF67xwyGcnEvpjOSoWcWriiKGwF6Y5HH+iF6zm+3UgSP2A Efd/FUX4Umt2yGTI0eFM4qFzlm0Sh40GaI580j+Th6meg9W8MW6FzcMjFuStQtw6EaKCgv5aHehXJ4 vrTP6QYTWXSNB6BqZPic6kg+uiy+ktmbY/TPPv5ic0iZI= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220609_104332_902321_B5BE414F X-CRM114-Status: GOOD ( 14.90 ) 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 Simplify the KPTI G-to-nG asm helper code by: - pulling the 'table bit' test into the get/put macros so we can combine them and incorporate the entire loop; - moving the 'table bit' test after the update of bit #11 so we no longer need separate next_xxx and skip_xxx labels; - redefining the pmd/pud register aliases and the next_pmd/next_pud labels instead of branching to them if the number of configured page table levels is less than 3 or 4, respectively. No functional change intended, except for the fact that we now descend into a next level table after setting bit #11 on its descriptor but this should make no difference in practice. While at it, switch to .L prefixed local labels so they don't clutter up the symbol tables, kallsyms, etc, and clean up the indentation for legibility. Signed-off-by: Ard Biesheuvel Reviewed-by: Mark Rutland Tested-by: Mark Rutland --- arch/arm64/mm/proc.S | 100 +++++++------------- 1 file changed, 36 insertions(+), 64 deletions(-) diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S index 50bbed947bec..660887152dba 100644 --- a/arch/arm64/mm/proc.S +++ b/arch/arm64/mm/proc.S @@ -202,19 +202,25 @@ SYM_FUNC_END(idmap_cpu_replace_ttbr1) #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 .pushsection ".idmap.text", "awx" - .macro __idmap_kpti_get_pgtable_ent, type - dc cvac, cur_\()\type\()p // Ensure any existing dirty + .macro kpti_mk_tbl_ng, type, num_entries + add end_\type\()p, cur_\type\()p, #\num_entries * 8 +.Ldo_\type: + dc cvac, cur_\type\()p // Ensure any existing dirty dmb sy // lines are written back before - ldr \type, [cur_\()\type\()p] // loading the entry - tbz \type, #0, skip_\()\type // Skip invalid and - tbnz \type, #11, skip_\()\type // non-global entries - .endm - - .macro __idmap_kpti_put_pgtable_ent_ng, type + ldr \type, [cur_\type\()p] // loading the entry + tbz \type, #0, .Lnext_\type // Skip invalid and + tbnz \type, #11, .Lnext_\type // non-global entries orr \type, \type, #PTE_NG // Same bit for blocks and pages - str \type, [cur_\()\type\()p] // Update the entry and ensure + str \type, [cur_\type\()p] // Update the entry and ensure dmb sy // that it is visible to all dc civac, cur_\()\type\()p // CPUs. + .ifnc \type, pte + tbnz \type, #1, .Lderef_\type + .endif +.Lnext_\type: + add cur_\type\()p, cur_\type\()p, #8 + cmp cur_\type\()p, end_\type\()p + b.ne .Ldo_\type .endm /* @@ -235,10 +241,8 @@ SYM_FUNC_START(idmap_kpti_install_ng_mappings) pgd .req x7 cur_pudp .req x8 end_pudp .req x9 - pud .req x10 cur_pmdp .req x11 end_pmdp .req x12 - pmd .req x13 cur_ptep .req x14 end_ptep .req x15 pte .req x16 @@ -265,16 +269,8 @@ SYM_FUNC_START(idmap_kpti_install_ng_mappings) /* Everybody is enjoying the idmap, so we can rewrite swapper. */ /* PGD */ - mov cur_pgdp, swapper_pa - add end_pgdp, cur_pgdp, #(PTRS_PER_PGD * 8) -do_pgd: __idmap_kpti_get_pgtable_ent pgd - tbnz pgd, #1, walk_puds -next_pgd: - __idmap_kpti_put_pgtable_ent_ng pgd -skip_pgd: - add cur_pgdp, cur_pgdp, #8 - cmp cur_pgdp, end_pgdp - b.ne do_pgd + mov cur_pgdp, swapper_pa + kpti_mk_tbl_ng pgd, PTRS_PER_PGD /* Publish the updated tables and nuke all the TLBs */ dsb sy @@ -291,59 +287,35 @@ skip_pgd: str wzr, [flag_ptr] ret +.Lderef_pgd: /* PUD */ -walk_puds: - .if CONFIG_PGTABLE_LEVELS > 3 + .if CONFIG_PGTABLE_LEVELS > 3 + pud .req x10 pte_to_phys cur_pudp, pgd - add end_pudp, cur_pudp, #(PTRS_PER_PUD * 8) -do_pud: __idmap_kpti_get_pgtable_ent pud - tbnz pud, #1, walk_pmds -next_pud: - __idmap_kpti_put_pgtable_ent_ng pud -skip_pud: - add cur_pudp, cur_pudp, 8 - cmp cur_pudp, end_pudp - b.ne do_pud - b next_pgd - .else /* CONFIG_PGTABLE_LEVELS <= 3 */ - mov pud, pgd - b walk_pmds -next_pud: - b next_pgd + kpti_mk_tbl_ng pud, PTRS_PER_PUD + b .Lnext_pgd + .else /* CONFIG_PGTABLE_LEVELS <= 3 */ + pud .req pgd + .set .Lnext_pud, .Lnext_pgd .endif +.Lderef_pud: /* PMD */ -walk_pmds: - .if CONFIG_PGTABLE_LEVELS > 2 + .if CONFIG_PGTABLE_LEVELS > 2 + pmd .req x13 pte_to_phys cur_pmdp, pud - add end_pmdp, cur_pmdp, #(PTRS_PER_PMD * 8) -do_pmd: __idmap_kpti_get_pgtable_ent pmd - tbnz pmd, #1, walk_ptes -next_pmd: - __idmap_kpti_put_pgtable_ent_ng pmd -skip_pmd: - add cur_pmdp, cur_pmdp, #8 - cmp cur_pmdp, end_pmdp - b.ne do_pmd - b next_pud - .else /* CONFIG_PGTABLE_LEVELS <= 2 */ - mov pmd, pud - b walk_ptes -next_pmd: - b next_pud + kpti_mk_tbl_ng pmd, PTRS_PER_PMD + b .Lnext_pud + .else /* CONFIG_PGTABLE_LEVELS <= 2 */ + pmd .req pgd + .set .Lnext_pmd, .Lnext_pgd .endif +.Lderef_pmd: /* PTE */ -walk_ptes: pte_to_phys cur_ptep, pmd - add end_ptep, cur_ptep, #(PTRS_PER_PTE * 8) -do_pte: __idmap_kpti_get_pgtable_ent pte - __idmap_kpti_put_pgtable_ent_ng pte -skip_pte: - add cur_ptep, cur_ptep, #8 - cmp cur_ptep, end_ptep - b.ne do_pte - b next_pmd + kpti_mk_tbl_ng pte, PTRS_PER_PTE + b .Lnext_pmd .unreq cpu .unreq num_cpus