Skip to content

Commit a4a118f

Browse files
anadavtorvalds
authored andcommitted
hugetlbfs: flush TLBs correctly after huge_pmd_unshare
When __unmap_hugepage_range() calls to huge_pmd_unshare() succeed, a TLB flush is missing. This TLB flush must be performed before releasing the i_mmap_rwsem, in order to prevent an unshared PMDs page from being released and reused before the TLB flush took place. Arguably, a comprehensive solution would use mmu_gather interface to batch the TLB flushes and the PMDs page release, however it is not an easy solution: (1) try_to_unmap_one() and try_to_migrate_one() also call huge_pmd_unshare() and they cannot use the mmu_gather interface; and (2) deferring the release of the page reference for the PMDs page until after i_mmap_rwsem is dropeed can confuse huge_pmd_unshare() into thinking PMDs are shared when they are not. Fix __unmap_hugepage_range() by adding the missing TLB flush, and forcing a flush when unshare is successful. Fixes: 24669e5 ("hugetlb: use mmu_gather instead of a temporary linked list for accumulating pages)" # 3.6 Signed-off-by: Nadav Amit <namit@vmware.com> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1360572 commit a4a118f

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

mm/hugetlb.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4939,6 +4939,7 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct
49394939
struct hstate *h = hstate_vma(vma);
49404940
unsigned long sz = huge_page_size(h);
49414941
struct mmu_notifier_range range;
4942+
bool force_flush = false;
49424943

49434944
WARN_ON(!is_vm_hugetlb_page(vma));
49444945
BUG_ON(start & ~huge_page_mask(h));
@@ -4967,10 +4968,8 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct
49674968
ptl = huge_pte_lock(h, mm, ptep);
49684969
if (huge_pmd_unshare(mm, vma, &address, ptep)) {
49694970
spin_unlock(ptl);
4970-
/*
4971-
* We just unmapped a page of PMDs by clearing a PUD.
4972-
* The caller's TLB flush range should cover this area.
4973-
*/
4971+
tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE);
4972+
force_flush = true;
49744973
continue;
49754974
}
49764975

@@ -5027,6 +5026,22 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct
50275026
}
50285027
mmu_notifier_invalidate_range_end(&range);
50295028
tlb_end_vma(tlb, vma);
5029+
5030+
/*
5031+
* If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We
5032+
* could defer the flush until now, since by holding i_mmap_rwsem we
5033+
* guaranteed that the last refernece would not be dropped. But we must
5034+
* do the flushing before we return, as otherwise i_mmap_rwsem will be
5035+
* dropped and the last reference to the shared PMDs page might be
5036+
* dropped as well.
5037+
*
5038+
* In theory we could defer the freeing of the PMD pages as well, but
5039+
* huge_pmd_unshare() relies on the exact page_count for the PMD page to
5040+
* detect sharing, so we cannot defer the release of the page either.
5041+
* Instead, do flush now.
5042+
*/
5043+
if (force_flush)
5044+
tlb_flush_mmu_tlbonly(tlb);
50305045
}
50315046

50325047
void __unmap_hugepage_range_final(struct mmu_gather *tlb,

0 commit comments

Comments
 (0)