Skip to contentSkip to conversation

arm64/mm: Simplify and document pte_to_phys() for 52 bit addresses

PATCH Superseded Anshuman Khandual 2 participants 3 messages Nov 7, 2022 arm64

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm64/mm: Simplify and document pte_to_phys() for 52 bit addresses

pte_to_phys() assembly definition does multiple bits field transformations to derive physical address, embedded inside a page table entry. Unlike its C counter part i.e __pte_to_phys(), pte_to_phys() is not very apparent. It simplifies these operations via a new macro PTE_ADDR_HIGH_SHIFT indicating how far the pte encoded higher address bits need to be left shifted. While here, this also updates __pte_to_phys() and __phys_to_pte_val().

---
This applies on v6.1-rc4

Changes in V2:

- Added PTE_ADDR_HIGH_SHIFT based method per Ard

Changes in V1:

https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/assembler.h     | 8 +++-----
 arch/arm64/include/asm/pgtable-hwdef.h | 1 +
 arch/arm64/include/asm/pgtable.h       | 4 ++--
 3 files changed, 6 insertions(+), 7 deletions(-)
arch/arm64/include/asm/assembler.h +3 -5
index e5957a53be39..6a39a3601cf7 100644
@@ -660,12 +660,10 @@ alternative_endif
 	.endm
 
 	.macro	pte_to_phys, phys, pte
-#ifdef CONFIG_ARM64_PA_BITS_52
-	ubfiz	\phys, \pte, #(48 - 16 - 12), #16
-	bfxil	\phys, \pte, #16, #32
-	lsl	\phys, \phys, #16
-#else
 	and	\phys, \pte, #PTE_ADDR_MASK
+#ifdef CONFIG_ARM64_PA_BITS_52
+	orr \phys, \phys, \phys, lsl #PTE_ADDR_HIGH_SHIFT
+	and \phys, \phys, GENMASK_ULL(PHYS_MASK_SHIFT - 1, PAGE_SHIFT)
 #endif
 	.endm
 
arch/arm64/include/asm/pgtable-hwdef.h +1 -0
index 5ab8d163198f..f658aafc47df 100644
@@ -159,6 +159,7 @@
 #ifdef CONFIG_ARM64_PA_BITS_52
 #define PTE_ADDR_HIGH		(_AT(pteval_t, 0xf) << 12)
 #define PTE_ADDR_MASK		(PTE_ADDR_LOW | PTE_ADDR_HIGH)
+#define PTE_ADDR_HIGH_SHIFT	36
 #else
 #define PTE_ADDR_MASK		PTE_ADDR_LOW
 #endif
arch/arm64/include/asm/pgtable.h +2 -2
index 71a1af42f0e8..daedd6172227 100644
@@ -77,11 +77,11 @@ extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
 static inline phys_addr_t __pte_to_phys(pte_t pte)
 {
 	return (pte_val(pte) & PTE_ADDR_LOW) |
-		((pte_val(pte) & PTE_ADDR_HIGH) << 36);
+		((pte_val(pte) & PTE_ADDR_HIGH) << PTE_ADDR_HIGH_SHIFT);
 }
 static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
 {
-	return (phys | (phys >> 36)) & PTE_ADDR_MASK;
+	return (phys | (phys >> PTE_ADDR_HIGH_SHIFT)) & PTE_ADDR_MASK;
 }
 #else
 #define __pte_to_phys(pte)	(pte_val(pte) & PTE_ADDR_MASK)
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw

Hello Anshuman,

Quoted 8 lines · Anshuman Khandual

pte_to_phys() assembly definition does multiple bits field transformations to derive physical address, embedded inside a page table entry. Unlike its C counter part i.e __pte_to_phys(), pte_to_phys() is not very apparent. It simplifies these operations via a new macro PTE_ADDR_HIGH_SHIFT indicating how far the pte encoded higher address bits need to be left shifted. While here, this also updates __pte_to_phys() and __phys_to_pte_val().

Quoted 9 lines

With the nit below fixed, this looks good to me

Quoted 23 lines

---
This applies on v6.1-rc4

Changes in V2:

- Added PTE_ADDR_HIGH_SHIFT based method per Ard

Changes in V1:

https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/assembler.h     | 8 +++-----
 arch/arm64/include/asm/pgtable-hwdef.h | 1 +
 arch/arm64/include/asm/pgtable.h       | 4 ++--
 3 files changed, 6 insertions(+), 7 deletions(-)
arch/arm64/include/asm/assembler.h +0 -0
index e5957a53be39..6a39a3601cf7 100644
@@ -660,12 +660,10 @@ alternative_endif
        .endm
 

arch/arm64/include/asm/assembler.h alternative_endif · line 666

Quoted 10 lines
arch/arm64/include/asm/assembler.h +3 -5
        .macro  pte_to_phys, phys, pte
-#ifdef CONFIG_ARM64_PA_BITS_52
-       ubfiz   \phys, \pte, #(48 - 16 - 12), #16
-       bfxil   \phys, \pte, #16, #32
-       lsl     \phys, \phys, #16
-#else
        and     \phys, \pte, #PTE_ADDR_MASK
+#ifdef CONFIG_ARM64_PA_BITS_52
+       orr \phys, \phys, \phys, lsl #PTE_ADDR_HIGH_SHIFT
+       and \phys, \phys, GENMASK_ULL(PHYS_MASK_SHIFT - 1, PAGE_SHIFT)

Please use tabs between the mnemonics and the arguments.

Quoted 36 lines
arch/arm64/include/asm/assembler.h +0 -0
 #endif
        .endm
 
arch/arm64/include/asm/pgtable-hwdef.h +1 -0
index 5ab8d163198f..f658aafc47df 100644
@@ -159,6 +159,7 @@
 #ifdef CONFIG_ARM64_PA_BITS_52
 #define PTE_ADDR_HIGH          (_AT(pteval_t, 0xf) << 12)
 #define PTE_ADDR_MASK          (PTE_ADDR_LOW | PTE_ADDR_HIGH)
+#define PTE_ADDR_HIGH_SHIFT    36
 #else
 #define PTE_ADDR_MASK          PTE_ADDR_LOW
 #endif
arch/arm64/include/asm/pgtable.h +2 -3
index 71a1af42f0e8..daedd6172227 100644
@@ -77,11 +77,11 @@ extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
 static inline phys_addr_t __pte_to_phys(pte_t pte)
 {
        return (pte_val(pte) & PTE_ADDR_LOW) |
-               ((pte_val(pte) & PTE_ADDR_HIGH) << 36);
+               ((pte_val(pte) & PTE_ADDR_HIGH) << PTE_ADDR_HIGH_SHIFT);
 }
 static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
 {
-       return (phys | (phys >> 36)) & PTE_ADDR_MASK;
+       return (phys | (phys >> PTE_ADDR_HIGH_SHIFT)) & PTE_ADDR_MASK;
 }
 #else
 #define __pte_to_phys(pte)     (pte_val(pte) & PTE_ADDR_MASK)
--

2.25.1

#2 lore raw
Quoted 49 lines · Ard Biesheuvel

Hello Anshuman,

Quoted 8 lines · Anshuman Khandual

pte_to_phys() assembly definition does multiple bits field transformations to derive physical address, embedded inside a page table entry. Unlike its C counter part i.e __pte_to_phys(), pte_to_phys() is not very apparent. It simplifies these operations via a new macro PTE_ADDR_HIGH_SHIFT indicating how far the pte encoded higher address bits need to be left shifted. While here, this also updates __pte_to_phys() and __phys_to_pte_val().

Quoted 9 lines

With the nit below fixed, this looks good to me

Quoted 23 lines

---
This applies on v6.1-rc4

Changes in V2:

- Added PTE_ADDR_HIGH_SHIFT based method per Ard

Changes in V1:

https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/assembler.h     | 8 +++-----
 arch/arm64/include/asm/pgtable-hwdef.h | 1 +
 arch/arm64/include/asm/pgtable.h       | 4 ++--
 3 files changed, 6 insertions(+), 7 deletions(-)
arch/arm64/include/asm/assembler.h +0 -0
index e5957a53be39..6a39a3601cf7 100644
@@ -660,12 +660,10 @@ alternative_endif
        .endm
 
Quoted 12 lines

arch/arm64/include/asm/assembler.h alternative_endif · line 666

Quoted 10 lines
arch/arm64/include/asm/assembler.h +3 -5
        .macro  pte_to_phys, phys, pte
-#ifdef CONFIG_ARM64_PA_BITS_52
-       ubfiz   \phys, \pte, #(48 - 16 - 12), #16
-       bfxil   \phys, \pte, #16, #32
-       lsl     \phys, \phys, #16
-#else
        and     \phys, \pte, #PTE_ADDR_MASK
+#ifdef CONFIG_ARM64_PA_BITS_52
+       orr \phys, \phys, \phys, lsl #PTE_ADDR_HIGH_SHIFT
+       and \phys, \phys, GENMASK_ULL(PHYS_MASK_SHIFT - 1, PAGE_SHIFT)

Please use tabs between the mnemonics and the arguments.

Sure, will do that.