Skip to content

Commit 6882728

Browse files
yhuang-inteltorvalds
authored andcommitted
mm, gup: prevent pmd checking race in follow_pmd_mask()
mmap_sem will be read locked when calling follow_pmd_mask(). But this cannot prevent PMD from being changed for all cases when PTL is unlocked, for example, from pmd_trans_huge() to pmd_none() via MADV_DONTNEED. So it is possible for the pmd_present() check in follow_pmd_mask() to encounter an invalid PMD. This may cause an incorrect VM_BUG_ON() or an infinite loop. Fix this by reading the PMD entry into a local variable with READ_ONCE() and checking the local variable and pmd_none() in the retry loop. As Kirill pointed out, with PTL unlocked, the *pmd may be changed under us, so reading it directly again and again may incur weird bugs. So although using *pmd directly other than for pmd_present() checking may be safe, it is still better to replace them to read *pmd once and check the local variable multiple times. When PTL unlocked, replace all *pmd with local variable was suggested by Kirill. Link: http://lkml.kernel.org/r/20180419083514.1365-1-ying.huang@intel.com Signed-off-by: "Huang, Ying" <ying.huang@intel.com> Reviewed-by: Zi Yan <zi.yan@cs.rutgers.edu> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> 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 7854207 commit 6882728

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

‎mm/gup.c‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,53 +212,69 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma,
212212
unsigned long address, pud_t *pudp,
213213
unsigned int flags, unsigned int *page_mask)
214214
{
215-
pmd_t *pmd;
215+
pmd_t *pmd, pmdval;
216216
spinlock_t *ptl;
217217
struct page *page;
218218
struct mm_struct *mm = vma->vm_mm;
219219

220220
pmd = pmd_offset(pudp, address);
221-
if (pmd_none(*pmd))
221+
/*
222+
* The READ_ONCE() will stabilize the pmdval in a register or
223+
* on the stack so that it will stop changing under the code.
224+
*/
225+
pmdval = READ_ONCE(*pmd);
226+
if (pmd_none(pmdval))
222227
return no_page_table(vma, flags);
223-
if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
228+
if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) {
224229
page = follow_huge_pmd(mm, address, pmd, flags);
225230
if (page)
226231
return page;
227232
return no_page_table(vma, flags);
228233
}
229-
if (is_hugepd(__hugepd(pmd_val(*pmd)))) {
234+
if (is_hugepd(__hugepd(pmd_val(pmdval)))) {
230235
page = follow_huge_pd(vma, address,
231-
__hugepd(pmd_val(*pmd)), flags,
236+
__hugepd(pmd_val(pmdval)), flags,
232237
PMD_SHIFT);
233238
if (page)
234239
return page;
235240
return no_page_table(vma, flags);
236241
}
237242
retry:
238-
if (!pmd_present(*pmd)) {
243+
if (!pmd_present(pmdval)) {
239244
if (likely(!(flags & FOLL_MIGRATION)))
240245
return no_page_table(vma, flags);
241246
VM_BUG_ON(thp_migration_supported() &&
242-
!is_pmd_migration_entry(*pmd));
243-
if (is_pmd_migration_entry(*pmd))
247+
!is_pmd_migration_entry(pmdval));
248+
if (is_pmd_migration_entry(pmdval))
244249
pmd_migration_entry_wait(mm, pmd);
250+
pmdval = READ_ONCE(*pmd);
251+
/*
252+
* MADV_DONTNEED may convert the pmd to null because
253+
* mmap_sem is held in read mode
254+
*/
255+
if (pmd_none(pmdval))
256+
return no_page_table(vma, flags);
245257
goto retry;
246258
}
247-
if (pmd_devmap(*pmd)) {
259+
if (pmd_devmap(pmdval)) {
248260
ptl = pmd_lock(mm, pmd);
249261
page = follow_devmap_pmd(vma, address, pmd, flags);
250262
spin_unlock(ptl);
251263
if (page)
252264
return page;
253265
}
254-
if (likely(!pmd_trans_huge(*pmd)))
266+
if (likely(!pmd_trans_huge(pmdval)))
255267
return follow_page_pte(vma, address, pmd, flags);
256268

257-
if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
269+
if ((flags & FOLL_NUMA) && pmd_protnone(pmdval))
258270
return no_page_table(vma, flags);
259271

260272
retry_locked:
261273
ptl = pmd_lock(mm, pmd);
274+
if (unlikely(pmd_none(*pmd))) {
275+
spin_unlock(ptl);
276+
return no_page_table(vma, flags);
277+
}
262278
if (unlikely(!pmd_present(*pmd))) {
263279
spin_unlock(ptl);
264280
if (likely(!(flags & FOLL_MIGRATION)))

0 commit comments

Comments
 (0)