Skip to content

Commit 79a61cc

Browse files
committed
mm: avoid leaving partial pfn mappings around in error case
As Jann points out, PFN mappings are special, because unlike normal memory mappings, there is no lifetime information associated with the mapping - it is just a raw mapping of PFNs with no reference counting of a 'struct page'. That's all very much intentional, but it does mean that it's easy to mess up the cleanup in case of errors. Yes, a failed mmap() will always eventually clean up any partial mappings, but without any explicit lifetime in the page table mapping itself, it's very easy to do the error handling in the wrong order. In particular, it's easy to mistakenly free the physical backing store before the page tables are actually cleaned up and (temporarily) have stale dangling PTE entries. To make this situation less error-prone, just make sure that any partial pfn mapping is torn down early, before any other error handling. Reported-and-tested-by: Jann Horn <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Jason Gunthorpe <[email protected]> Cc: Simona Vetter <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 77f5878 commit 79a61cc

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

mm/memory.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,11 +2632,7 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
26322632
return 0;
26332633
}
26342634

2635-
/*
2636-
* Variant of remap_pfn_range that does not call track_pfn_remap. The caller
2637-
* must have pre-validated the caching bits of the pgprot_t.
2638-
*/
2639-
int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
2635+
static int remap_pfn_range_internal(struct vm_area_struct *vma, unsigned long addr,
26402636
unsigned long pfn, unsigned long size, pgprot_t prot)
26412637
{
26422638
pgd_t *pgd;
@@ -2689,6 +2685,27 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
26892685
return 0;
26902686
}
26912687

2688+
/*
2689+
* Variant of remap_pfn_range that does not call track_pfn_remap. The caller
2690+
* must have pre-validated the caching bits of the pgprot_t.
2691+
*/
2692+
int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
2693+
unsigned long pfn, unsigned long size, pgprot_t prot)
2694+
{
2695+
int error = remap_pfn_range_internal(vma, addr, pfn, size, prot);
2696+
2697+
if (!error)
2698+
return 0;
2699+
2700+
/*
2701+
* A partial pfn range mapping is dangerous: it does not
2702+
* maintain page reference counts, and callers may free
2703+
* pages due to the error. So zap it early.
2704+
*/
2705+
zap_page_range_single(vma, addr, size, NULL);
2706+
return error;
2707+
}
2708+
26922709
/**
26932710
* remap_pfn_range - remap kernel memory to userspace
26942711
* @vma: user vma to map to

0 commit comments

Comments
 (0)