Skip to content

Commit 3e25d5a

Browse files
Christoph Hellwigarndb
authored andcommitted
asm-generic: add an optional pfn_valid check to page_to_phys
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>
1 parent c5c3238 commit 3e25d5a

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

include/asm-generic/memory_model.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ static inline int pfn_valid(unsigned long pfn)
6464
#define page_to_pfn __page_to_pfn
6565
#define pfn_to_page __pfn_to_page
6666

67+
#ifdef CONFIG_DEBUG_VIRTUAL
68+
#define page_to_phys(page) \
69+
({ \
70+
unsigned long __pfn = page_to_pfn(page); \
71+
\
72+
WARN_ON_ONCE(!pfn_valid(__pfn)); \
73+
PFN_PHYS(__pfn); \
74+
})
75+
#else
6776
#define page_to_phys(page) PFN_PHYS(page_to_pfn(page))
77+
#endif /* CONFIG_DEBUG_VIRTUAL */
6878
#define phys_to_page(phys) pfn_to_page(PHYS_PFN(phys))
6979

7080
#endif /* __ASSEMBLY__ */

0 commit comments

Comments
 (0)