History

hugetlbfs: remove call to huge_pte_alloc without i_mmap_rwsem

Commit c0d0381ade79 ("hugetlbfs: use i_mmap_rwsem for more pmd sharing synchronization") requires callers of huge_pte_alloc to hold i_mmap_rwsem in at least read mode. This is because the explicit locking in huge_pmd_share (called by huge_pte_alloc) was removed. When restructuring the code, the call to huge_pte_alloc in the else block at the beginning of hugetlb_fault was missed. Unfortunately, that else clause is exercised when there is no page table entry. This will likely lead to a call to huge_pmd_share. If huge_pmd_share thinks pmd sharing is possible, it will traverse the mapping tree (i_mmap) without holding i_mmap_rwsem. If someone else is modifying the tree, bad things such as addressing exceptions or worse could happen. Simply remove the else clause. It should have been removed previously. The code following the else will call huge_pte_alloc with the appropriate locking. To prevent this type of issue in the future, add routines to assert that i_mmap_rwsem is held, and call these routines in huge pmd sharing routines.

Mike Kravetz committed 6 years ago

Commit 34ae2041 parent 15568294 files · +23 −12Browse files

Unified · Split

4 files
  1. include/linux/fs.h +10 −0
  2. include/linux/hugetlb.h +5 −3
  3. mm/hugetlb.c +7 −8
  4. mm/rmap.c +1 −1
include/linux/fs.h+10−0
@@ -518,6 +518,16 @@ static inline void i_mmap_unlock_read(struct address_space *mapping) · 517 unmodified lines
518518 up_read(&mapping->i_mmap_rwsem);
519519 }
520520
521+static inline void i_mmap_assert_locked(struct address_space *mapping)
522+{
523+ lockdep_assert_held(&mapping->i_mmap_rwsem);
524+}
525+
526+static inline void i_mmap_assert_write_locked(struct address_space *mapping)
527+{
528+ lockdep_assert_held_write(&mapping->i_mmap_rwsem);
529+}
530+
521531 /*
522532 * Might pages of this file be mapped into userspace?
523533 */
include/linux/hugetlb.h+5−3
@@ -164,7 +164,8 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, · 163 unmodified lines
164164 unsigned long addr, unsigned long sz);
165165 pte_t *huge_pte_offset(struct mm_struct *mm,
166166 unsigned long addr, unsigned long sz);
167−int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep);
167+int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
168+ unsigned long *addr, pte_t *ptep);
168169 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
169170 unsigned long *start, unsigned long *end);
170171 struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address,
@@ -203,8 +204,9 @@ static inline struct address_space *hugetlb_page_mapping_lock_write( · 32 unmodified lines
203204 return NULL;
204205 }
205206
206−static inline int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr,
207− pte_t *ptep)
207+static inline int huge_pmd_unshare(struct mm_struct *mm,
208+ struct vm_area_struct *vma,
209+ unsigned long *addr, pte_t *ptep)
208210 {
209211 return 0;
210212 }
mm/hugetlb.c+7−8
@@ -3967,7 +3967,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · 3966 unmodified lines
39673967 continue;
39683968
39693969 ptl = huge_pte_lock(h, mm, ptep);
3970− if (huge_pmd_unshare(mm, &address, ptep)) {
3970+ if (huge_pmd_unshare(mm, vma, &address, ptep)) {
39713971 spin_unlock(ptl);
39723972 /*
39733973 * We just unmapped a page of PMDs by clearing a PUD.
@@ -4554,10 +4554,6 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, · 580 unmodified lines
45544554 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
45554555 return VM_FAULT_HWPOISON_LARGE |
45564556 VM_FAULT_SET_HINDEX(hstate_index(h));
4557− } else {
4558− ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
4559− if (!ptep)
4560− return VM_FAULT_OOM;
45614557 }
45624558
45634559 /*
@@ -5034,7 +5030,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, · 470 unmodified lines
50345030 if (!ptep)
50355031 continue;
50365032 ptl = huge_pte_lock(h, mm, ptep);
5037− if (huge_pmd_unshare(mm, &address, ptep)) {
5033+ if (huge_pmd_unshare(mm, vma, &address, ptep)) {
50385034 pages++;
50395035 spin_unlock(ptl);
50405036 shared_pmd = true;
@@ -5415,12 +5411,14 @@ out: · 374 unmodified lines
54155411 * returns: 1 successfully unmapped a shared pte page
54165412 * 0 the underlying pte page is not shared, or it is the last user
54175413 */
5418−int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
5414+int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
5415+ unsigned long *addr, pte_t *ptep)
54195416 {
54205417 pgd_t *pgd = pgd_offset(mm, *addr);
54215418 p4d_t *p4d = p4d_offset(pgd, *addr);
54225419 pud_t *pud = pud_offset(p4d, *addr);
54235420
5421+ i_mmap_assert_write_locked(vma->vm_file->f_mapping);
54245422 BUG_ON(page_count(virt_to_page(ptep)) == 0);
54255423 if (page_count(virt_to_page(ptep)) == 1)
54265424 return 0;
@@ -5438,7 +5436,8 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · 11 unmodified lines
54385436 return NULL;
54395437 }
54405438
5441−int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
5439+int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
5440+ unsigned long *addr, pte_t *ptep)
54425441 {
54435442 return 0;
54445443 }
mm/rmap.c+1−1
@@ -1469,7 +1469,7 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, · 1468 unmodified lines
14691469 * do this outside rmap routines.
14701470 */
14711471 VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
1472− if (huge_pmd_unshare(mm, &address, pvmw.pte)) {
1472+ if (huge_pmd_unshare(mm, vma, &address, pvmw.pte)) {
14731473 /*
14741474 * huge_pmd_unshare unmapped an entire PMD
14751475 * page. There is no way of knowing exactly