summaryrefslogtreecommitdiffstats
path: root/mm/khugepaged.c
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2016-12-12 19:43:32 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2016-12-12 21:55:08 -0500
commit91a45f71078a6569ec3ca5bef74e1ab58121d80e (patch)
tree0a01f6fdf31e21564404db162bc47a96b21e5439 /mm/khugepaged.c
parent8db378a570330fa0aaa9d75299fe264e4a5b6348 (diff)
mm: khugepaged: close use-after-free race during shmem collapsing
Patch series "mm: workingset: radix tree subtleties & single-page file refaults", v3. This is another revision of the radix tree / workingset patches based on feedback from Jan and Kirill. This is a follow-up to d3798ae8c6f3 ("mm: filemap: don't plant shadow entries without radix tree node"). That patch fixed an issue that was caused mainly by the page cache sneaking special shadow page entries into the radix tree and relying on subtleties in the radix tree code to make that work. The fix also had to stop tracking refaults for single-page files because shadow pages stored as direct pointers in radix_tree_root->rnode weren't properly handled during tree extension. These patches make the radix tree code explicitely support and track such special entries, to eliminate the subtleties and to restore the thrash detection for single-page files. This patch (of 9): When a radix tree iteration drops the tree lock, another thread might swoop in and free the node holding the current slot. The iteration needs to do another tree lookup from the current index to continue. [kirill.shutemov@linux.intel.com: re-lookup for replacement] Fixes: f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem pages") Link: http://lkml.kernel.org/r/20161117191138.22769-2-hannes@cmpxchg.org Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Reviewed-by: Jan Kara <jack@suse.cz> Cc: Hugh Dickins <hughd@google.com> Cc: Matthew Wilcox <mawilcox@linuxonhyperv.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/khugepaged.c')
-rw-r--r--mm/khugepaged.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 87e1a7ca3846..2779c63bdea0 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1403,6 +1403,9 @@ static void collapse_shmem(struct mm_struct *mm,
1403 1403
1404 spin_lock_irq(&mapping->tree_lock); 1404 spin_lock_irq(&mapping->tree_lock);
1405 1405
1406 slot = radix_tree_lookup_slot(&mapping->page_tree, index);
1407 VM_BUG_ON_PAGE(page != radix_tree_deref_slot_protected(slot,
1408 &mapping->tree_lock), page);
1406 VM_BUG_ON_PAGE(page_mapped(page), page); 1409 VM_BUG_ON_PAGE(page_mapped(page), page);
1407 1410
1408 /* 1411 /*
@@ -1426,6 +1429,7 @@ static void collapse_shmem(struct mm_struct *mm,
1426 radix_tree_replace_slot(slot, 1429 radix_tree_replace_slot(slot,
1427 new_page + (index % HPAGE_PMD_NR)); 1430 new_page + (index % HPAGE_PMD_NR));
1428 1431
1432 slot = radix_tree_iter_next(&iter);
1429 index++; 1433 index++;
1430 continue; 1434 continue;
1431out_lru: 1435out_lru:
@@ -1537,6 +1541,7 @@ tree_unlocked:
1537 putback_lru_page(page); 1541 putback_lru_page(page);
1538 unlock_page(page); 1542 unlock_page(page);
1539 spin_lock_irq(&mapping->tree_lock); 1543 spin_lock_irq(&mapping->tree_lock);
1544 slot = radix_tree_iter_next(&iter);
1540 } 1545 }
1541 VM_BUG_ON(nr_none); 1546 VM_BUG_ON(nr_none);
1542 spin_unlock_irq(&mapping->tree_lock); 1547 spin_unlock_irq(&mapping->tree_lock);