Skip to content

Commit 4bd3e8f

Browse files
JianyuZhantorvalds
authored andcommitted
mm/swap.c: split put_compound_page()
Currently, put_compound_page() carefully handles tricky cases to avoid racing with compound page releasing or splitting, which makes it quite lenthy (about 200+ lines) and needs deep tab indention, which makes it quite hard to follow and maintain. Now based on two helpers introduced in the previous patch ("mm/swap.c: introduce put_[un]refcounted_compound_page helpers for spliting put_compound_page"), this patch replaces those two lengthy code paths with these two helpers, respectively. Also, it has some comment rephrasing. After this patch, the put_compound_page() is very compact, thus easy to read and maintain. After splitting, the object file is of same size as the original one. Actually, I've diff'ed put_compound_page()'s orginal disassemble code and the patched disassemble code, the are 100% the same! This fact shows that this splitting has no functional change, but it brings readability. This patch and the previous one blow the code by 32 lines, mostly due to comments. Signed-off-by: Jianyu Zhan <nasa4836@gmail.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Rik van Riel <riel@redhat.com> Cc: Jiang Liu <liuj97@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Mel Gorman <mgorman@suse.de> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Sasha Levin <sasha.levin@oracle.com> Cc: Wanpeng Li <liwanp@linux.vnet.ibm.com> Cc: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c747ce7 commit 4bd3e8f

1 file changed

Lines changed: 16 additions & 126 deletions

File tree

‎mm/swap.c‎

Lines changed: 16 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ static void put_compound_page(struct page *page)
225225
{
226226
struct page *page_head;
227227

228+
/*
229+
* We see the PageCompound set and PageTail not set, so @page maybe:
230+
* 1. hugetlbfs head page, or
231+
* 2. THP head page.
232+
*/
228233
if (likely(!PageTail(page))) {
229234
if (put_page_testzero(page)) {
230235
/*
@@ -239,135 +244,20 @@ static void put_compound_page(struct page *page)
239244
return;
240245
}
241246

242-
/* __split_huge_page_refcount can run under us */
243-
page_head = compound_head(page);
244-
245247
/*
246-
* THP can not break up slab pages so avoid taking
247-
* compound_lock() and skip the tail page refcounting (in
248-
* _mapcount) too. Slab performs non-atomic bit ops on
249-
* page->flags for better performance. In particular
250-
* slab_unlock() in slub used to be a hot path. It is still
251-
* hot on arches that do not support
252-
* this_cpu_cmpxchg_double().
248+
* We see the PageCompound set and PageTail set, so @page maybe:
249+
* 1. a tail hugetlbfs page, or
250+
* 2. a tail THP page, or
251+
* 3. a split THP page.
253252
*
254-
* If "page" is part of a slab or hugetlbfs page it cannot be
255-
* splitted and the head page cannot change from under us. And
256-
* if "page" is part of a THP page under splitting, if the
257-
* head page pointed by the THP tail isn't a THP head anymore,
258-
* we'll find PageTail clear after smp_rmb() and we'll treat
259-
* it as a single page.
253+
* Case 3 is possible, as we may race with
254+
* __split_huge_page_refcount tearing down a THP page.
260255
*/
261-
if (!__compound_tail_refcounted(page_head)) {
262-
/*
263-
* If "page" is a THP tail, we must read the tail page
264-
* flags after the head page flags. The
265-
* split_huge_page side enforces write memory barriers
266-
* between clearing PageTail and before the head page
267-
* can be freed and reallocated.
268-
*/
269-
smp_rmb();
270-
if (likely(PageTail(page))) {
271-
/*
272-
* __split_huge_page_refcount cannot race
273-
* here.
274-
*/
275-
VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
276-
VM_BUG_ON_PAGE(page_mapcount(page) != 0, page);
277-
if (put_page_testzero(page_head)) {
278-
/*
279-
* If this is the tail of a slab
280-
* compound page, the tail pin must
281-
* not be the last reference held on
282-
* the page, because the PG_slab
283-
* cannot be cleared before all tail
284-
* pins (which skips the _mapcount
285-
* tail refcounting) have been
286-
* released. For hugetlbfs the tail
287-
* pin may be the last reference on
288-
* the page instead, because
289-
* PageHeadHuge will not go away until
290-
* the compound page enters the buddy
291-
* allocator.
292-
*/
293-
VM_BUG_ON_PAGE(PageSlab(page_head), page_head);
294-
__put_compound_page(page_head);
295-
}
296-
return;
297-
} else
298-
/*
299-
* __split_huge_page_refcount run before us,
300-
* "page" was a THP tail. The split page_head
301-
* has been freed and reallocated as slab or
302-
* hugetlbfs page of smaller order (only
303-
* possible if reallocated as slab on x86).
304-
*/
305-
goto out_put_single;
306-
}
307-
308-
if (likely(page != page_head && get_page_unless_zero(page_head))) {
309-
unsigned long flags;
310-
311-
/*
312-
* page_head wasn't a dangling pointer but it may not
313-
* be a head page anymore by the time we obtain the
314-
* lock. That is ok as long as it can't be freed from
315-
* under us.
316-
*/
317-
flags = compound_lock_irqsave(page_head);
318-
if (unlikely(!PageTail(page))) {
319-
/* __split_huge_page_refcount run before us */
320-
compound_unlock_irqrestore(page_head, flags);
321-
if (put_page_testzero(page_head)) {
322-
/*
323-
* The head page may have been freed
324-
* and reallocated as a compound page
325-
* of smaller order and then freed
326-
* again. All we know is that it
327-
* cannot have become: a THP page, a
328-
* compound page of higher order, a
329-
* tail page. That is because we
330-
* still hold the refcount of the
331-
* split THP tail and page_head was
332-
* the THP head before the split.
333-
*/
334-
if (PageHead(page_head))
335-
__put_compound_page(page_head);
336-
else
337-
__put_single_page(page_head);
338-
}
339-
out_put_single:
340-
if (put_page_testzero(page))
341-
__put_single_page(page);
342-
return;
343-
}
344-
VM_BUG_ON_PAGE(page_head != page->first_page, page);
345-
/*
346-
* We can release the refcount taken by
347-
* get_page_unless_zero() now that
348-
* __split_huge_page_refcount() is blocked on the
349-
* compound_lock.
350-
*/
351-
if (put_page_testzero(page_head))
352-
VM_BUG_ON_PAGE(1, page_head);
353-
/* __split_huge_page_refcount will wait now */
354-
VM_BUG_ON_PAGE(page_mapcount(page) <= 0, page);
355-
atomic_dec(&page->_mapcount);
356-
VM_BUG_ON_PAGE(atomic_read(&page_head->_count) <= 0, page_head);
357-
VM_BUG_ON_PAGE(atomic_read(&page->_count) != 0, page);
358-
compound_unlock_irqrestore(page_head, flags);
359-
360-
if (put_page_testzero(page_head)) {
361-
if (PageHead(page_head))
362-
__put_compound_page(page_head);
363-
else
364-
__put_single_page(page_head);
365-
}
366-
} else {
367-
/* page_head is a dangling pointer */
368-
VM_BUG_ON_PAGE(PageTail(page), page);
369-
goto out_put_single;
370-
}
256+
page_head = compound_head(page);
257+
if (!__compound_tail_refcounted(page_head))
258+
put_unrefcounted_compound_page(page_head, page);
259+
else
260+
put_refcounted_compound_page(page_head, page);
371261
}
372262

373263
void put_page(struct page *page)

0 commit comments

Comments
 (0)