Skip to content

Commit 6d77795

Browse files
Christoph LameterLinus Torvalds
authored andcommitted
mm: optimize compound_head() by avoiding a shared page flag
The patch adds PageTail(page) and PageHead(page) to check if a page is the head or the tail of a compound page. This is done by masking the two bits describing the state of a compound page and then comparing them. So one comparision and a branch instead of two bit checks and two branches. Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d85f338 commit 6d77795

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

include/linux/mm.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,7 @@ static inline int get_page_unless_zero(struct page *page)
269269

270270
static inline struct page *compound_head(struct page *page)
271271
{
272-
/*
273-
* We could avoid the PageCompound(page) check if
274-
* we would not overload PageTail().
275-
*
276-
* This check has to be done in several performance critical
277-
* paths of the slab etc. IMHO PageTail deserves its own flag.
278-
*/
279-
if (unlikely(PageCompound(page) && PageTail(page)))
272+
if (unlikely(PageTail(page)))
280273
return page->first_page;
281274
return page;
282275
}
@@ -327,7 +320,7 @@ static inline compound_page_dtor *get_compound_page_dtor(struct page *page)
327320

328321
static inline int compound_order(struct page *page)
329322
{
330-
if (!PageCompound(page) || PageTail(page))
323+
if (!PageHead(page))
331324
return 0;
332325
return (unsigned long)page[1].lru.prev;
333326
}

include/linux/page-flags.h

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define PAGE_FLAGS_H
77

88
#include <linux/types.h>
9+
#include <linux/mm_types.h>
910

1011
/*
1112
* Various page->flags bits:
@@ -94,12 +95,6 @@
9495
/* PG_owner_priv_1 users should have descriptive aliases */
9596
#define PG_checked PG_owner_priv_1 /* Used by some filesystems */
9697

97-
/*
98-
* Marks tail portion of a compound page. We currently do not reclaim
99-
* compound pages so we can reuse a flag only used for reclaim here.
100-
*/
101-
#define PG_tail PG_reclaim
102-
10398
#if (BITS_PER_LONG > 32)
10499
/*
105100
* 64-bit-only flags build down from bit 31
@@ -248,12 +243,32 @@ static inline void SetPageUptodate(struct page *page)
248243
#define __ClearPageCompound(page) __clear_bit(PG_compound, &(page)->flags)
249244

250245
/*
251-
* Note: PG_tail is an alias of another page flag. The result of PageTail()
252-
* is only valid if PageCompound(page) is true.
246+
* PG_reclaim is used in combination with PG_compound to mark the
247+
* head and tail of a compound page
248+
*
249+
* PG_compound & PG_reclaim => Tail page
250+
* PG_compound & ~PG_reclaim => Head page
253251
*/
254-
#define PageTail(page) test_bit(PG_tail, &(page)->flags)
255-
#define __SetPageTail(page) __set_bit(PG_tail, &(page)->flags)
256-
#define __ClearPageTail(page) __clear_bit(PG_tail, &(page)->flags)
252+
253+
#define PG_head_tail_mask ((1L << PG_compound) | (1L << PG_reclaim))
254+
255+
#define PageTail(page) ((page->flags & PG_head_tail_mask) \
256+
== PG_head_tail_mask)
257+
258+
static inline void __SetPageTail(struct page *page)
259+
{
260+
page->flags |= PG_head_tail_mask;
261+
}
262+
263+
static inline void __ClearPageTail(struct page *page)
264+
{
265+
page->flags &= ~PG_head_tail_mask;
266+
}
267+
268+
#define PageHead(page) ((page->flags & PG_head_tail_mask) \
269+
== (1L << PG_compound))
270+
#define __SetPageHead(page) __SetPageCompound(page)
271+
#define __ClearPageHead(page) __ClearPageCompound(page)
257272

258273
#ifdef CONFIG_SWAP
259274
#define PageSwapCache(page) test_bit(PG_swapcache, &(page)->flags)

mm/page_alloc.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,11 @@ static void prep_compound_page(struct page *page, unsigned long order)
235235

236236
set_compound_page_dtor(page, free_compound_page);
237237
set_compound_order(page, order);
238-
__SetPageCompound(page);
238+
__SetPageHead(page);
239239
for (i = 1; i < nr_pages; i++) {
240240
struct page *p = page + i;
241241

242242
__SetPageTail(p);
243-
__SetPageCompound(p);
244243
p->first_page = page;
245244
}
246245
}
@@ -253,17 +252,16 @@ static void destroy_compound_page(struct page *page, unsigned long order)
253252
if (unlikely(compound_order(page) != order))
254253
bad_page(page);
255254

256-
if (unlikely(!PageCompound(page)))
255+
if (unlikely(!PageHead(page)))
257256
bad_page(page);
258-
__ClearPageCompound(page);
257+
__ClearPageHead(page);
259258
for (i = 1; i < nr_pages; i++) {
260259
struct page *p = page + i;
261260

262-
if (unlikely(!PageCompound(p) | !PageTail(p) |
261+
if (unlikely(!PageTail(p) |
263262
(p->first_page != page)))
264263
bad_page(page);
265264
__ClearPageTail(p);
266-
__ClearPageCompound(p);
267265
}
268266
}
269267

0 commit comments

Comments
 (0)