@@ -86,45 +86,61 @@ static void put_compound_page(struct page *page)
8686 /* __split_huge_page_refcount can run under us */
8787 struct page * page_head = compound_trans_head (page );
8888
89+ /*
90+ * THP can not break up slab pages so avoid taking
91+ * compound_lock(). Slab performs non-atomic bit ops
92+ * on page->flags for better performance. In
93+ * particular slab_unlock() in slub used to be a hot
94+ * path. It is still hot on arches that do not support
95+ * this_cpu_cmpxchg_double().
96+ *
97+ * If "page" is part of a slab or hugetlbfs page it
98+ * cannot be splitted and the head page cannot change
99+ * from under us. And if "page" is part of a THP page
100+ * under splitting, if the head page pointed by the
101+ * THP tail isn't a THP head anymore, we'll find
102+ * PageTail clear after smp_rmb() and we'll treat it
103+ * as a single page.
104+ */
105+ if (PageSlab (page_head ) || PageHeadHuge (page_head )) {
106+ /*
107+ * If "page" is a THP tail, we must read the tail page
108+ * flags after the head page flags. The
109+ * split_huge_page side enforces write memory
110+ * barriers between clearing PageTail and before the
111+ * head page can be freed and reallocated.
112+ */
113+ smp_rmb ();
114+ if (likely (PageTail (page ))) {
115+ /*
116+ * __split_huge_page_refcount
117+ * cannot race here.
118+ */
119+ VM_BUG_ON (!PageHead (page_head ));
120+ VM_BUG_ON (page_mapcount (page ) <= 0 );
121+ atomic_dec (& page -> _mapcount );
122+ if (put_page_testzero (page_head ))
123+ __put_compound_page (page_head );
124+ return ;
125+ } else
126+ /*
127+ * __split_huge_page_refcount
128+ * run before us, "page" was a
129+ * THP tail. The split
130+ * page_head has been freed
131+ * and reallocated as slab or
132+ * hugetlbfs page of smaller
133+ * order (only possible if
134+ * reallocated as slab on
135+ * x86).
136+ */
137+ goto out_put_single ;
138+ }
139+
89140 if (likely (page != page_head &&
90141 get_page_unless_zero (page_head ))) {
91142 unsigned long flags ;
92143
93- /*
94- * THP can not break up slab pages so avoid taking
95- * compound_lock(). Slab performs non-atomic bit ops
96- * on page->flags for better performance. In particular
97- * slab_unlock() in slub used to be a hot path. It is
98- * still hot on arches that do not support
99- * this_cpu_cmpxchg_double().
100- */
101- if (PageSlab (page_head ) || PageHeadHuge (page_head )) {
102- if (likely (PageTail (page ))) {
103- /*
104- * __split_huge_page_refcount
105- * cannot race here.
106- */
107- VM_BUG_ON (!PageHead (page_head ));
108- atomic_dec (& page -> _mapcount );
109- if (put_page_testzero (page_head ))
110- VM_BUG_ON (1 );
111- if (put_page_testzero (page_head ))
112- __put_compound_page (page_head );
113- return ;
114- } else
115- /*
116- * __split_huge_page_refcount
117- * run before us, "page" was a
118- * THP tail. The split
119- * page_head has been freed
120- * and reallocated as slab or
121- * hugetlbfs page of smaller
122- * order (only possible if
123- * reallocated as slab on
124- * x86).
125- */
126- goto skip_lock ;
127- }
128144 /*
129145 * page_head wasn't a dangling pointer but it
130146 * may not be a head page anymore by the time
@@ -135,7 +151,6 @@ static void put_compound_page(struct page *page)
135151 if (unlikely (!PageTail (page ))) {
136152 /* __split_huge_page_refcount run before us */
137153 compound_unlock_irqrestore (page_head , flags );
138- skip_lock :
139154 if (put_page_testzero (page_head )) {
140155 /*
141156 * The head page may have been
@@ -221,36 +236,37 @@ bool __get_page_tail(struct page *page)
221236 * split_huge_page().
222237 */
223238 unsigned long flags ;
224- bool got = false ;
239+ bool got ;
225240 struct page * page_head = compound_trans_head (page );
226241
227- if (likely (page != page_head && get_page_unless_zero (page_head ))) {
228- /* Ref to put_compound_page() comment. */
229- if (PageSlab (page_head ) || PageHeadHuge (page_head )) {
230- if (likely (PageTail (page ))) {
231- /*
232- * This is a hugetlbfs page or a slab
233- * page. __split_huge_page_refcount
234- * cannot race here.
235- */
236- VM_BUG_ON (!PageHead (page_head ));
237- __get_page_tail_foll (page , false);
238- return true;
239- } else {
240- /*
241- * __split_huge_page_refcount run
242- * before us, "page" was a THP
243- * tail. The split page_head has been
244- * freed and reallocated as slab or
245- * hugetlbfs page of smaller order
246- * (only possible if reallocated as
247- * slab on x86).
248- */
249- put_page (page_head );
250- return false;
251- }
242+ /* Ref to put_compound_page() comment. */
243+ if (PageSlab (page_head ) || PageHeadHuge (page_head )) {
244+ smp_rmb ();
245+ if (likely (PageTail (page ))) {
246+ /*
247+ * This is a hugetlbfs page or a slab
248+ * page. __split_huge_page_refcount
249+ * cannot race here.
250+ */
251+ VM_BUG_ON (!PageHead (page_head ));
252+ __get_page_tail_foll (page , true);
253+ return true;
254+ } else {
255+ /*
256+ * __split_huge_page_refcount run
257+ * before us, "page" was a THP
258+ * tail. The split page_head has been
259+ * freed and reallocated as slab or
260+ * hugetlbfs page of smaller order
261+ * (only possible if reallocated as
262+ * slab on x86).
263+ */
264+ return false;
252265 }
266+ }
253267
268+ got = false;
269+ if (likely (page != page_head && get_page_unless_zero (page_head ))) {
254270 /*
255271 * page_head wasn't a dangling pointer but it
256272 * may not be a head page anymore by the time
0 commit comments