powerpc/64: Add VIRTUAL_BUG_ON checks for __va and __pa addresses
authorNicholas Piggin <npiggin@gmail.com>
Wed, 24 Jul 2019 08:46:37 +0000 (18:46 +1000)
committerMichael Ellerman <mpe@ellerman.id.au>
Tue, 20 Aug 2019 11:22:20 +0000 (21:22 +1000)
Ensure __va is given a physical address below PAGE_OFFSET, and __pa is
given a virtual address above PAGE_OFFSET.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Reviewed-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20190724084638.24982-4-npiggin@gmail.com
arch/powerpc/include/asm/page.h

index 0d52f57fca04fcda19a051188bb814ef890d5af4..c8bb14ff47135d7913c2392f10ff5b96d2c2a19d 100644 (file)
@@ -215,9 +215,19 @@ static inline bool pfn_valid(unsigned long pfn)
 /*
  * gcc miscompiles (unsigned long)(&static_var) - PAGE_OFFSET
  * with -mcmodel=medium, so we use & and | instead of - and + on 64-bit.
+ * This also results in better code generation.
  */
-#define __va(x) ((void *)(unsigned long)((phys_addr_t)(x) | PAGE_OFFSET))
-#define __pa(x) ((unsigned long)(x) & 0x0fffffffffffffffUL)
+#define __va(x)                                                                \
+({                                                                     \
+       VIRTUAL_BUG_ON((unsigned long)(x) >= PAGE_OFFSET);              \
+       (void *)(unsigned long)((phys_addr_t)(x) | PAGE_OFFSET);        \
+})
+
+#define __pa(x)                                                                \
+({                                                                     \
+       VIRTUAL_BUG_ON((unsigned long)(x) < PAGE_OFFSET);               \
+       (unsigned long)(x) & 0x0fffffffffffffffUL;                      \
+})
 
 #else /* 32-bit, non book E */
 #define __va(x) ((void *)(unsigned long)((phys_addr_t)(x) + PAGE_OFFSET - MEMORY_START))