Skip to content

Commit c747ce7

Browse files
JianyuZhantorvalds
authored andcommitted
mm/swap.c: introduce put_[un]refcounted_compound_page helpers for splitting 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. This patch and the next patch refactor this function. Based on the code skeleton of put_compound_page: put_compound_pge: if !PageTail(page) put head page fastpath; return; /* else PageTail */ page_head = compound_head(page) if !__compound_tail_refcounted(page_head) put head page optimal path; <---(1) return; else put head page slowpath; <--- (2) return; This patch introduces two helpers, put_[un]refcounted_compound_page, handling the code path (1) and code path (2), respectively. They both are tagged __always_inline, thus elmiating function call overhead, making them operating the same way as before. They are almost copied verbatim(except one place, a "goto out_put_single" is expanded), with some comments rephrasing. 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 23c8902 commit c747ce7

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

‎mm/swap.c‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,148 @@ static void __put_compound_page(struct page *page)
7979
(*dtor)(page);
8080
}
8181

82+
/**
83+
* Two special cases here: we could avoid taking compound_lock_irqsave
84+
* and could skip the tail refcounting(in _mapcount).
85+
*
86+
* 1. Hugetlbfs page:
87+
*
88+
* PageHeadHuge will remain true until the compound page
89+
* is released and enters the buddy allocator, and it could
90+
* not be split by __split_huge_page_refcount().
91+
*
92+
* So if we see PageHeadHuge set, and we have the tail page pin,
93+
* then we could safely put head page.
94+
*
95+
* 2. Slab THP page:
96+
*
97+
* PG_slab is cleared before the slab frees the head page, and
98+
* tail pin cannot be the last reference left on the head page,
99+
* because the slab code is free to reuse the compound page
100+
* after a kfree/kmem_cache_free without having to check if
101+
* there's any tail pin left. In turn all tail pinsmust be always
102+
* released while the head is still pinned by the slab code
103+
* and so we know PG_slab will be still set too.
104+
*
105+
* So if we see PageSlab set, and we have the tail page pin,
106+
* then we could safely put head page.
107+
*/
108+
static __always_inline
109+
void put_unrefcounted_compound_page(struct page *page_head, struct page *page)
110+
{
111+
/*
112+
* If @page is a THP tail, we must read the tail page
113+
* flags after the head page flags. The
114+
* __split_huge_page_refcount side enforces write memory barriers
115+
* between clearing PageTail and before the head page
116+
* can be freed and reallocated.
117+
*/
118+
smp_rmb();
119+
if (likely(PageTail(page))) {
120+
/*
121+
* __split_huge_page_refcount cannot race
122+
* here, see the comment above this function.
123+
*/
124+
VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
125+
VM_BUG_ON_PAGE(page_mapcount(page) != 0, page);
126+
if (put_page_testzero(page_head)) {
127+
/*
128+
* If this is the tail of a slab THP page,
129+
* the tail pin must not be the last reference
130+
* held on the page, because the PG_slab cannot
131+
* be cleared before all tail pins (which skips
132+
* the _mapcount tail refcounting) have been
133+
* released.
134+
*
135+
* If this is the tail of a hugetlbfs page,
136+
* the tail pin may be the last reference on
137+
* the page instead, because PageHeadHuge will
138+
* not go away until the compound page enters
139+
* the buddy allocator.
140+
*/
141+
VM_BUG_ON_PAGE(PageSlab(page_head), page_head);
142+
__put_compound_page(page_head);
143+
}
144+
} else
145+
/*
146+
* __split_huge_page_refcount run before us,
147+
* @page was a THP tail. The split @page_head
148+
* has been freed and reallocated as slab or
149+
* hugetlbfs page of smaller order (only
150+
* possible if reallocated as slab on x86).
151+
*/
152+
if (put_page_testzero(page))
153+
__put_single_page(page);
154+
}
155+
156+
static __always_inline
157+
void put_refcounted_compound_page(struct page *page_head, struct page *page)
158+
{
159+
if (likely(page != page_head && get_page_unless_zero(page_head))) {
160+
unsigned long flags;
161+
162+
/*
163+
* @page_head wasn't a dangling pointer but it may not
164+
* be a head page anymore by the time we obtain the
165+
* lock. That is ok as long as it can't be freed from
166+
* under us.
167+
*/
168+
flags = compound_lock_irqsave(page_head);
169+
if (unlikely(!PageTail(page))) {
170+
/* __split_huge_page_refcount run before us */
171+
compound_unlock_irqrestore(page_head, flags);
172+
if (put_page_testzero(page_head)) {
173+
/*
174+
* The @page_head may have been freed
175+
* and reallocated as a compound page
176+
* of smaller order and then freed
177+
* again. All we know is that it
178+
* cannot have become: a THP page, a
179+
* compound page of higher order, a
180+
* tail page. That is because we
181+
* still hold the refcount of the
182+
* split THP tail and page_head was
183+
* the THP head before the split.
184+
*/
185+
if (PageHead(page_head))
186+
__put_compound_page(page_head);
187+
else
188+
__put_single_page(page_head);
189+
}
190+
out_put_single:
191+
if (put_page_testzero(page))
192+
__put_single_page(page);
193+
return;
194+
}
195+
VM_BUG_ON_PAGE(page_head != page->first_page, page);
196+
/*
197+
* We can release the refcount taken by
198+
* get_page_unless_zero() now that
199+
* __split_huge_page_refcount() is blocked on the
200+
* compound_lock.
201+
*/
202+
if (put_page_testzero(page_head))
203+
VM_BUG_ON_PAGE(1, page_head);
204+
/* __split_huge_page_refcount will wait now */
205+
VM_BUG_ON_PAGE(page_mapcount(page) <= 0, page);
206+
atomic_dec(&page->_mapcount);
207+
VM_BUG_ON_PAGE(atomic_read(&page_head->_count) <= 0, page_head);
208+
VM_BUG_ON_PAGE(atomic_read(&page->_count) != 0, page);
209+
compound_unlock_irqrestore(page_head, flags);
210+
211+
if (put_page_testzero(page_head)) {
212+
if (PageHead(page_head))
213+
__put_compound_page(page_head);
214+
else
215+
__put_single_page(page_head);
216+
}
217+
} else {
218+
/* @page_head is a dangling pointer */
219+
VM_BUG_ON_PAGE(PageTail(page), page);
220+
goto out_put_single;
221+
}
222+
}
223+
82224
static void put_compound_page(struct page *page)
83225
{
84226
struct page *page_head;

0 commit comments

Comments
 (0)