Commit 26296ad2 authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

mm/swap.c: reorganize put_compound_page()

Tweak it so save a tab stop, make code layout slightly less nutty.
Signed-off-by: default avatarAndrea Arcangeli <aarcange@redhat.com>
Cc: Khalid Aziz <khalid.aziz@oracle.com>
Cc: Pravin Shelar <pshelar@nicira.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Ben Hutchings <bhutchings@solarflare.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Johannes Weiner <jweiner@redhat.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Minchan Kim <minchan@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 758f66a2
...@@ -81,61 +81,71 @@ static void __put_compound_page(struct page *page) ...@@ -81,61 +81,71 @@ static void __put_compound_page(struct page *page)
static void put_compound_page(struct page *page) static void put_compound_page(struct page *page)
{ {
if (unlikely(PageTail(page))) { struct page *page_head;
if (likely(!PageTail(page))) {
if (put_page_testzero(page)) {
/*
* By the time all refcounts have been released
* split_huge_page cannot run anymore from under us.
*/
if (PageHead(page))
__put_compound_page(page);
else
__put_single_page(page);
}
return;
}
/* __split_huge_page_refcount can run under us */ /* __split_huge_page_refcount can run under us */
struct page *page_head = compound_trans_head(page); page_head = compound_trans_head(page);
/* /*
* THP can not break up slab pages so avoid taking * THP can not break up slab pages so avoid taking
* compound_lock() and skip the tail page refcounting * compound_lock() and skip the tail page refcounting (in
* (in _mapcount) too. Slab performs non-atomic bit * _mapcount) too. Slab performs non-atomic bit ops on
* ops on page->flags for better performance. In * page->flags for better performance. In particular
* particular slab_unlock() in slub used to be a hot * slab_unlock() in slub used to be a hot path. It is still
* path. It is still hot on arches that do not support * hot on arches that do not support
* this_cpu_cmpxchg_double(). * this_cpu_cmpxchg_double().
* *
* If "page" is part of a slab or hugetlbfs page it * If "page" is part of a slab or hugetlbfs page it cannot be
* cannot be splitted and the head page cannot change * splitted and the head page cannot change from under us. And
* from under us. And if "page" is part of a THP page * if "page" is part of a THP page under splitting, if the
* under splitting, if the head page pointed by the * head page pointed by the THP tail isn't a THP head anymore,
* THP tail isn't a THP head anymore, we'll find * we'll find PageTail clear after smp_rmb() and we'll treat
* PageTail clear after smp_rmb() and we'll treat it * it as a single page.
* as a single page.
*/ */
if (!__compound_tail_refcounted(page_head)) { if (!__compound_tail_refcounted(page_head)) {
/* /*
* If "page" is a THP tail, we must read the tail page * If "page" is a THP tail, we must read the tail page
* flags after the head page flags. The * flags after the head page flags. The
* split_huge_page side enforces write memory * split_huge_page side enforces write memory barriers
* barriers between clearing PageTail and before the * between clearing PageTail and before the head page
* head page can be freed and reallocated. * can be freed and reallocated.
*/ */
smp_rmb(); smp_rmb();
if (likely(PageTail(page))) { if (likely(PageTail(page))) {
/* /*
* __split_huge_page_refcount * __split_huge_page_refcount cannot race
* cannot race here. * here.
*/ */
VM_BUG_ON(!PageHead(page_head)); VM_BUG_ON(!PageHead(page_head));
VM_BUG_ON(page_mapcount(page) != 0); VM_BUG_ON(page_mapcount(page) != 0);
if (put_page_testzero(page_head)) { if (put_page_testzero(page_head)) {
/* /*
* If this is the tail of a * If this is the tail of a slab
* slab compound page, the * compound page, the tail pin must
* tail pin must not be the * not be the last reference held on
* last reference held on the * the page, because the PG_slab
* page, because the PG_slab * cannot be cleared before all tail
* cannot be cleared before * pins (which skips the _mapcount
* all tail pins (which skips * tail refcounting) have been
* the _mapcount tail * released. For hugetlbfs the tail
* refcounting) have been * pin may be the last reference on
* released. For hugetlbfs the * the page instead, because
* tail pin may be the last * PageHeadHuge will not go away until
* reference on the page * the compound page enters the buddy
* instead, because
* PageHeadHuge will not go
* away until the compound
* page enters the buddy
* allocator. * allocator.
*/ */
VM_BUG_ON(PageSlab(page_head)); VM_BUG_ON(PageSlab(page_head));
...@@ -144,28 +154,23 @@ static void put_compound_page(struct page *page) ...@@ -144,28 +154,23 @@ static void put_compound_page(struct page *page)
return; return;
} else } else
/* /*
* __split_huge_page_refcount * __split_huge_page_refcount run before us,
* run before us, "page" was a * "page" was a THP tail. The split page_head
* THP tail. The split * has been freed and reallocated as slab or
* page_head has been freed * hugetlbfs page of smaller order (only
* and reallocated as slab or * possible if reallocated as slab on x86).
* hugetlbfs page of smaller
* order (only possible if
* reallocated as slab on
* x86).
*/ */
goto out_put_single; goto out_put_single;
} }
if (likely(page != page_head && if (likely(page != page_head && get_page_unless_zero(page_head))) {
get_page_unless_zero(page_head))) {
unsigned long flags; unsigned long flags;
/* /*
* page_head wasn't a dangling pointer but it * page_head wasn't a dangling pointer but it may not
* may not be a head page anymore by the time * be a head page anymore by the time we obtain the
* we obtain the lock. That is ok as long as it * lock. That is ok as long as it can't be freed from
* can't be freed from under us. * under us.
*/ */
flags = compound_lock_irqsave(page_head); flags = compound_lock_irqsave(page_head);
if (unlikely(!PageTail(page))) { if (unlikely(!PageTail(page))) {
...@@ -173,19 +178,16 @@ static void put_compound_page(struct page *page) ...@@ -173,19 +178,16 @@ static void put_compound_page(struct page *page)
compound_unlock_irqrestore(page_head, flags); compound_unlock_irqrestore(page_head, flags);
if (put_page_testzero(page_head)) { if (put_page_testzero(page_head)) {
/* /*
* The head page may have been * The head page may have been freed
* freed and reallocated as a * and reallocated as a compound page
* compound page of smaller * of smaller order and then freed
* order and then freed again. * again. All we know is that it
* All we know is that it * cannot have become: a THP page, a
* cannot have become: a THP * compound page of higher order, a
* page, a compound page of * tail page. That is because we
* higher order, a tail page. * still hold the refcount of the
* That is because we still * split THP tail and page_head was
* hold the refcount of the * the THP head before the split.
* split THP tail and
* page_head was the THP head
* before the split.
*/ */
if (PageHead(page_head)) if (PageHead(page_head))
__put_compound_page(page_head); __put_compound_page(page_head);
...@@ -201,8 +203,8 @@ static void put_compound_page(struct page *page) ...@@ -201,8 +203,8 @@ static void put_compound_page(struct page *page)
/* /*
* We can release the refcount taken by * We can release the refcount taken by
* get_page_unless_zero() now that * get_page_unless_zero() now that
* __split_huge_page_refcount() is blocked on * __split_huge_page_refcount() is blocked on the
* the compound_lock. * compound_lock.
*/ */
if (put_page_testzero(page_head)) if (put_page_testzero(page_head))
VM_BUG_ON(1); VM_BUG_ON(1);
...@@ -224,12 +226,6 @@ static void put_compound_page(struct page *page) ...@@ -224,12 +226,6 @@ static void put_compound_page(struct page *page)
VM_BUG_ON(PageTail(page)); VM_BUG_ON(PageTail(page));
goto out_put_single; goto out_put_single;
} }
} else if (put_page_testzero(page)) {
if (PageHead(page))
__put_compound_page(page);
else
__put_single_page(page);
}
} }
void put_page(struct page *page) void put_page(struct page *page)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment