Skip to content

Commit 605ca5e

Browse files
koct9itorvalds
authored andcommitted
mm/huge_memory.c: reorder operations in __split_huge_page_tail()
THP split makes non-atomic change of tail page flags. This is almost ok because tail pages are locked and isolated but this breaks recent changes in page locking: non-atomic operation could clear bit PG_waiters. As a result concurrent sequence get_page_unless_zero() -> lock_page() might block forever. Especially if this page was truncated later. Fix is trivial: clone flags before unfreezing page reference counter. This race exists since commit 6290602 ("mm: add PageWaiters indicating tasks are waiting for a page bit") while unsave unfreeze itself was added in commit 8df651c ("thp: cleanup split_huge_page()"). clear_compound_head() also must be called before unfreezing page reference because after successful get_page_unless_zero() might follow put_page() which needs correct compound_head(). And replace page_ref_inc()/page_ref_add() with page_ref_unfreeze() which is made especially for that and has semantic of smp_store_release(). Link: http://lkml.kernel.org/r/151844393341.210639.13162088407980624477.stgit@buzz Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Nicholas Piggin <npiggin@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 03f5d58 commit 605ca5e

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

mm/huge_memory.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,26 +2356,13 @@ static void __split_huge_page_tail(struct page *head, int tail,
23562356
struct page *page_tail = head + tail;
23572357

23582358
VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
2359-
VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
23602359

23612360
/*
2362-
* tail_page->_refcount is zero and not changing from under us. But
2363-
* get_page_unless_zero() may be running from under us on the
2364-
* tail_page. If we used atomic_set() below instead of atomic_inc() or
2365-
* atomic_add(), we would then run atomic_set() concurrently with
2366-
* get_page_unless_zero(), and atomic_set() is implemented in C not
2367-
* using locked ops. spin_unlock on x86 sometime uses locked ops
2368-
* because of PPro errata 66, 92, so unless somebody can guarantee
2369-
* atomic_set() here would be safe on all archs (and not only on x86),
2370-
* it's safer to use atomic_inc()/atomic_add().
2361+
* Clone page flags before unfreezing refcount.
2362+
*
2363+
* After successful get_page_unless_zero() might follow flags change,
2364+
* for exmaple lock_page() which set PG_waiters.
23712365
*/
2372-
if (PageAnon(head) && !PageSwapCache(head)) {
2373-
page_ref_inc(page_tail);
2374-
} else {
2375-
/* Additional pin to radix tree */
2376-
page_ref_add(page_tail, 2);
2377-
}
2378-
23792366
page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
23802367
page_tail->flags |= (head->flags &
23812368
((1L << PG_referenced) |
@@ -2388,14 +2375,21 @@ static void __split_huge_page_tail(struct page *head, int tail,
23882375
(1L << PG_unevictable) |
23892376
(1L << PG_dirty)));
23902377

2391-
/*
2392-
* After clearing PageTail the gup refcount can be released.
2393-
* Page flags also must be visible before we make the page non-compound.
2394-
*/
2378+
/* Page flags must be visible before we make the page non-compound. */
23952379
smp_wmb();
23962380

2381+
/*
2382+
* Clear PageTail before unfreezing page refcount.
2383+
*
2384+
* After successful get_page_unless_zero() might follow put_page()
2385+
* which needs correct compound_head().
2386+
*/
23972387
clear_compound_head(page_tail);
23982388

2389+
/* Finally unfreeze refcount. Additional reference from page cache. */
2390+
page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2391+
PageSwapCache(head)));
2392+
23992393
if (page_is_young(head))
24002394
set_page_young(page_tail);
24012395
if (page_is_idle(head))

0 commit comments

Comments
 (0)