asm-generic: add an optional pfn_valid check to page_to_phys
authorChristoph Hellwig <hch@lst.de>
Wed, 23 Oct 2024 05:36:37 +0000 (07:36 +0200)
committerArnd Bergmann <arnd@arndb.de>
Mon, 28 Oct 2024 21:44:28 +0000 (21:44 +0000)
page_to_pfn is usually implemented by pointer arithmetics on the memory
map, which means that bogus input can lead to even more bogus output.

Powerpc had a pfn_valid check on the intermediate pfn in the page_to_phys
implementation when CONFIG_DEBUG_VIRTUAL is defined, which seems
generally useful, so add that to the generic version.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
include/asm-generic/memory_model.h

index a73a140..6d1fb61 100644 (file)
@@ -64,7 +64,17 @@ static inline int pfn_valid(unsigned long pfn)
 #define page_to_pfn __page_to_pfn
 #define pfn_to_page __pfn_to_page
 
+#ifdef CONFIG_DEBUG_VIRTUAL
+#define page_to_phys(page)                                             \
+({                                                                     \
+       unsigned long __pfn = page_to_pfn(page);                        \
+                                                                       \
+       WARN_ON_ONCE(!pfn_valid(__pfn));                                \
+       PFN_PHYS(__pfn);                                                \
+})
+#else
 #define page_to_phys(page)     PFN_PHYS(page_to_pfn(page))
+#endif /* CONFIG_DEBUG_VIRTUAL */
 #define phys_to_page(phys)     pfn_to_page(PHYS_PFN(phys))
 
 #endif /* __ASSEMBLY__ */