Skip to content

Commit ac343e8

Browse files
Ard Biesheuveltorvalds
authored andcommitted
memremap: check pfn validity before passing to pfn_to_page()
In memremap's helper function try_ram_remap(), we dereference a struct page pointer that was derived from a PFN that is known to be covered by a 'System RAM' iomem region, and is thus assumed to be a 'valid' PFN, i.e., a PFN that has a struct page associated with it and is covered by the kernel direct mapping. However, the assumption that there is a 1:1 relation between the System RAM iomem region and the kernel direct mapping is not universally valid on all architectures, and on ARM and arm64, 'System RAM' may include regions for which pfn_valid() returns false. Generally speaking, both __va() and pfn_to_page() should only ever be called on PFNs/physical addresses for which pfn_valid() returns true, so add that check to try_ram_remap(). Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0a2e280 commit ac343e8

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

kernel/memremap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
2929

3030
static void *try_ram_remap(resource_size_t offset, size_t size)
3131
{
32-
struct page *page = pfn_to_page(offset >> PAGE_SHIFT);
32+
unsigned long pfn = PHYS_PFN(offset);
3333

3434
/* In the simple case just return the existing linear address */
35-
if (!PageHighMem(page))
35+
if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)))
3636
return __va(offset);
3737
return NULL; /* fallback to ioremap_cache */
3838
}

0 commit comments

Comments
 (0)