Skip to contentSkip to conversation

arm64: mm: don't assume struct page is always 64 bytes

PATCH Applied Ard Biesheuvel 3 participants 3 messages Nov 10, 2020 – Nov 12, 2020 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mm: don't assume struct page is always 64 bytes

Commit 8c96400d6a39be7 simplified the page-to-virt and virt-to-page conversions, based on the assumption that struct page is always 64 bytes in size, in which case we can use a single signed shift to perform the conversion (provided that the vmemmap array is placed appropriately in the kernel VA space)

Unfortunately, this assumption turns out not to hold, and so we need to revert part of this commit, and go back to an affine transformation. Given that all the quantities involved are compile time constants, this should not make any practical difference.

---
 arch/arm64/include/asm/memory.h | 8 +++++---
 arch/arm64/mm/init.c            | 2 --
 2 files changed, 5 insertions(+), 5 deletions(-)
arch/arm64/include/asm/memory.h +5 -3
index 03e9b112bd94..556cb2d62b5b 100644
@@ -308,13 +308,15 @@ static inline void *phys_to_virt(phys_addr_t x)
 #else
 #define page_to_virt(x)	({						\
 	__typeof__(x) __page = x;					\
-	u64 __addr = (u64)__page << VMEMMAP_SHIFT;			\
+	u64 __idx = ((u64)__page - VMEMMAP_START) / sizeof(struct page);\
+	u64 __addr = PAGE_OFFSET + (__idx * PAGE_SIZE);			\
 	(void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
 })
 
 #define virt_to_page(x)	({						\
-	u64 __addr = __tag_reset((u64)(x)) & PAGE_MASK;			\
-	(struct page *)((s64)__addr >> VMEMMAP_SHIFT);			\
+	u64 __idx = (__tag_reset((u64)x) - PAGE_OFFSET) / PAGE_SIZE;	\
+	u64 __addr = VMEMMAP_START + (__idx * sizeof(struct page));	\
+	(struct page *)__addr;						\
 })
 #endif /* !CONFIG_SPARSEMEM_VMEMMAP || CONFIG_DEBUG_VIRTUAL */
 
arch/arm64/mm/init.c +0 -2
index 3a5e9f9298e9..7e15d92836d8 100644
@@ -502,8 +502,6 @@ static void __init free_unused_memmap(void)
  */
 void __init mem_init(void)
 {
-	BUILD_BUG_ON(!is_power_of_2(sizeof(struct page)));
-
 	if (swiotlb_force == SWIOTLB_FORCE ||
 	    max_pfn > PFN_DOWN(arm64_dma_phys_limit ? : arm64_dma32_phys_limit))
 		swiotlb_init(1);
Signature · 7 lines
2.20.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Applied
#1 lore raw
Quoted 10 lines · Ard Biesheuvel

Commit 8c96400d6a39be7 simplified the page-to-virt and virt-to-page conversions, based on the assumption that struct page is always 64 bytes in size, in which case we can use a single signed shift to perform the conversion (provided that the vmemmap array is placed appropriately in the kernel VA space)

Unfortunately, this assumption turns out not to hold, and so we need to revert part of this commit, and go back to an affine transformation. Given that all the quantities involved are compile time constants, this should not make any practical difference.

Applied to arm64 (for-next/va-refactor), thanks!

[1/1] arm64: mm: don't assume struct page is always 64 bytes

      https://git.kernel.org/arm64/c/50af6f00c7d7
Signature · 7 lines
Catalin


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Tested-by on v1 1/1
#2 lore raw
In reply to Ard Biesheuvel

Hi Ard,

Quoted 6 lines · Ard Biesheuvel

Commit 8c96400d6a39be7 simplified the page-to-virt and virt-to-page conversions, based on the assumption that struct page is always 64 bytes in size, in which case we can use a single signed shift to perform the conversion (provided that the vmemmap array is placed appropriately in the kernel VA space)

Quoted 7 lines

Unfortunately, this assumption turns out not to hold, and so we need to revert part of this commit, and go back to an affine transformation. Given that all the quantities involved are compile time constants, this should not make any practical difference.

Thanks!

FTR:

Gr{oetje,eeting}s,

Geert

Signature · 10 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel