Restructure struct page
Subsystems (2)
v2 cover Restructure struct page
From: Matthew Wilcox <[email protected]>
This series does not attempt any grand restructuring as I proposed last week. Instead, it cures the worst of the indentitis, fixes the documentation and reduces the ifdeffery. The only layout change is compound_dtor and compound_order are each reduced to one byte.
Changes v2:
- Add acks
- Improve changelogs (mostly at Michal's suggestion)
- Improve documentation (patch 7) with Randy's suggestion and add a note about the treatment of _mapcount.
Matthew Wilcox (8):
mm: Align struct page more aesthetically
mm: De-indent struct page
mm: Remove misleading alignment claims
mm: Improve comment on page->mapping
mm: Introduce _slub_counter_t
mm: Store compound_dtor / compound_order as bytes
mm: Document how to use struct page
mm: Remove reference to PG_buddy
include/linux/mm_types.h | 153 ++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 80 deletions(-)
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 1/8 mm: Align struct page more aesthetically
From: Matthew Wilcox <[email protected]>
instead of an ifdef block at the end of the struct, which needed its own comment, define _struct_page_alignment up at the top where it fits nicely with the existing comment.
--- include/linux/mm_types.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
include/linux/mm_types.h +7 -9
@@ -39,6 +39,12 @@ struct hmm;* allows the use of atomic double word operations on the flags/mapping * and lru list pointers also. */ +#ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE +#define _struct_page_alignment __aligned(2 * sizeof(unsigned long)) +#else +#define _struct_page_alignment +#endif +struct page {/* First double word block */ unsigned long flags;/* Atomic flags, some possibly
@@ -212,15 +218,7 @@ struct page {#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS int _last_cpupid;#endif -} -/* -* The struct page can be forced to be double word aligned so that atomic ops -* on double words work. The SLUB allocator can make use of such a feature. -*/ -#ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE -__aligned(2 * sizeof(unsigned long)) -#endif -; +} _struct_page_alignment;#define PAGE_FRAG_CACHE_MAX_SIZE __ALIGN_MASK(32768, ~PAGE_MASK)#define PAGE_FRAG_CACHE_MAX_ORDER get_order(PAGE_FRAG_CACHE_MAX_SIZE)
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 2/8 mm: De-indent struct page
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
I found the struct { union { struct { union { struct { } } } } } layout rather confusing. Fortunately, there is an easier way to write this. The innermost union is of four things which are the size of an int, so the ones which are used by slab/slob/slub can be pulled up two levels to be in the outermost union with 'counters'. That leaves us with struct { union { struct { atomic_t; atomic_t; } } } which has the same layout, but is easier to read.
Output from the current git version of pahole, diffed with -uw to ignore the whitespace changes from the indentation:
@@ -11,9 +11,6 @@
}; /* 16 8 */
union {
long unsigned int counters; /* 24 8 */
- struct {
- union {
- atomic_t _mapcount; /* 24 4 */
unsigned int active; /* 24 4 */
struct {
unsigned int inuse:16; /* 24:16 4 */
@@ -21,7 +18,8 @@
unsigned int frozen:1; /* 24: 0 4 */
}; /* 24 4 */
int units; /* 24 4 */
- }; /* 24 4 */
+ struct {
+ atomic_t _mapcount; /* 24 4 */
atomic_t _refcount; /* 28 4 */
}; /* 24 8 */
}; /* 24 8 */--- include/linux/mm_types.h | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-)
include/linux/mm_types.h +19 -21
@@ -84,28 +84,26 @@ struct page {*/ unsigned counters;#endif -struct { +unsigned int active;/* SLAB */ +struct {/* SLUB */ +unsigned inuse:16 ; +unsigned objects:15 ; +unsigned frozen:1 ; + }; +int units;/* SLOB */ + +struct {/* Page cache */ +/* +* Count of ptes mapped in mms, to show when +* page is mapped & limit reverse map searches. +* +* Extra information about page type may be +* stored here for pages that are never mapped, +* in which case the value MUST BE <= -2. +* See page-flags.h for more details. +*/ +atomic_t _mapcount; -union { -/* -* Count of ptes mapped in mms, to show when -* page is mapped & limit reverse map searches. -* -* Extra information about page type may be -* stored here for pages that are never mapped, -* in which case the value MUST BE <= -2. -* See page-flags.h for more details. -*/ -atomic_t _mapcount; - -unsigned int active;/* SLAB */ -struct {/* SLUB */ -unsigned inuse:16 ; -unsigned objects:15 ; -unsigned frozen:1 ; - }; -int units;/* SLOB */ - };/* * Usage count, *USE WRAPPER FUNCTION* when manual * accounting. See page_ref.h
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 3/8 mm: Remove misleading alignment claims
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
The "third double word block" isn't on 32-bit systems. The layout looks like this:
unsigned long flags; struct address_space *mapping pgoff_t index; atomic_t _mapcount; atomic_t _refcount;
which is 32 bytes on 64-bit, but 20 bytes on 32-bit. Nobody is trying to use the fact that it's double-word aligned today, so just remove the misleading claims.
--- include/linux/mm_types.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
include/linux/mm_types.h +5 -8
@@ -33,11 +33,11 @@ struct hmm;* a page, though if it is a pagecache page, rmap structures can tell us * who is mapping it. * -* The objects in struct page are organized in double word blocks in -* order to allows us to use atomic double word operations on portions -* of struct page. That is currently only used by slub but the arrangement -* allows the use of atomic double word operations on the flags/mapping -* and lru list pointers also. +* SLUB uses cmpxchg_double() to atomically update its freelist and +* counters. That requires that freelist & counters be adjacent and +* double-word aligned. We align all struct pages to double-word +* boundaries, and ensure that 'freelist' is aligned within the +* struct. */ #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE #define _struct_page_alignment __aligned(2 * sizeof(unsigned long))
@@ -113,8 +113,6 @@ struct page { };/* -* Third double word block -* * WARNING: bit 0 of the first word encode PageTail(). That means * the rest users of the storage space MUST NOT use the bit to * avoid collision and false-positive PageTail().
@@ -175,7 +173,6 @@ struct page {#endif }; -/* Remainder is not double word aligned */ union {unsigned long private;/* Mapping-private opaque data: * usually used for buffer_heads
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 4/8 mm: Improve comment on page->mapping
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
The comment on page->mapping is terse, and out of date (it does not mention the possibility of PAGE_MAPPING_MOVABLE). Instead, point the interested reader to page-flags.h where there is a much better comment.
--- include/linux/mm_types.h | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
include/linux/mm_types.h +3 -9
@@ -50,15 +50,9 @@ struct page {unsigned long flags;/* Atomic flags, some possibly * updated asynchronously */ union { -struct address_space *mapping;/* If low bit clear, points to -* inode address_space, or NULL. -* If page mapped as anonymous -* memory, low bit is set, and -* it points to anon_vma object -* or KSM private structure. See -* PAGE_MAPPING_ANON and -* PAGE_MAPPING_KSM. -*/ +/* See page-flags.h for the definition of PAGE_MAPPING_FLAGS */ +struct address_space *mapping; +void *s_mem;/* slab first object */ atomic_t compound_mapcount;/* first tail page */ /* page_deferred_list().next -- second tail page */
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 5/8 mm: Introduce _slub_counter_t
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
Instead of putting the ifdef in the middle of the definition of struct page, pull it forward to the rest of the ifdeffery around the SLUB cmpxchg_double optimisation.
--- include/linux/mm_types.h | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
include/linux/mm_types.h +8 -13
@@ -41,9 +41,15 @@ struct hmm;*/ #ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE #define _struct_page_alignment __aligned(2 * sizeof(unsigned long)) +#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) +#define _slub_counter_t unsigned long#else -#define _struct_page_alignment +#define _slub_counter_t unsigned int#endif +#else /* !CONFIG_HAVE_ALIGNED_STRUCT_PAGE */ +#define _struct_page_alignment +#define _slub_counter_t unsigned int +#endif /* !CONFIG_HAVE_ALIGNED_STRUCT_PAGE */ struct page {/* First double word block */
@@ -66,18 +72,7 @@ struct page { };union { -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE ) -/* Used for cmpxchg_double in slub */ -unsigned long counters; -#else -/* -* Keep _refcount separate from slub cmpxchg_double data. -* As the rest of the double word is protected by slab_lock -* but _refcount is not. -*/ -unsigned counters; -#endif +_slub_counter_t counters;unsigned int active;/* SLAB */ struct {/* SLUB */ unsigned inuse:16 ;
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 6/8 mm: Store compound_dtor / compound_order as bytes
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
Neither of these values get even close to 256; compound_dtor is currently at a maximum of 3, and compound_order can't be over 64. No machine has inefficient access to bytes since EV5, and while those are still supported, we don't optimise for them any more. This does not shrink struct page, but it removes an ifdef and frees up 2-6 bytes for future use.
diff of pahole output:
@@ -34,8 +34,8 @@
struct callback_head callback_head; /* 32 16 */
struct {
long unsigned int compound_head; /* 32 8 */ - unsigned int compound_dtor; /* 40 4 */ - unsigned int compound_order; /* 44 4 */ + unsigned char compound_dtor; /* 40 1 */ + unsigned char compound_order; /* 41 1 */ }; /* 32 16 */
}; /* 32 16 */
union {
--- include/linux/mm_types.h | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-)
include/linux/mm_types.h +2 -13
@@ -136,19 +136,8 @@ struct page {unsigned long compound_head;/* If bit zero is set */ /* First tail page only */ -#ifdef CONFIG_64BIT -/* -* On 64 bit system we have enough space in struct page -* to encode compound_dtor and compound_order with -* unsigned int. It can help compiler generate better or -* smaller code on some archtectures. -*/ -unsigned int compound_dtor; -unsigned int compound_order; -#else -unsigned short int compound_dtor; -unsigned short int compound_order; -#endif +unsigned char compound_dtor; +unsigned char compound_order; };#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) &&USE_SPLIT_PMD_PTLOCKS
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerRe: [PATCH v2 6/8] mm: Store compound_dtor / compound_order as bytes
Quoted 6 lines · On Wed, 20 Dec 2017 07:55:50 -0800 Matthew Wilcox <[email protected]> wrote:
From: Matthew Wilcox <[email protected]>
Neither of these values get even close to 256;
compound_dtoris currently at a maximum of 3, andcompound_ordercan't be over 64. No machine has inefficient access to bytes since EV5, and while those are still supported, we don't optimise for them any more.
So we couild fit compound_dtor and compound_order into a single byte if desperate?
Quoted 2 lines
This does not shrink struct page, but it removes an ifdef and frees up 2-6 bytes for future use.
Can we add a little comment telling readers "hey there's a gap here!"?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 6/8] mm: Store compound_dtor / compound_order as bytes
Quoted 11 lines · Andrew Morton
Quoted 6 lines · On Wed, 20 Dec 2017 07:55:50 -0800 Matthew Wilcox <[email protected]> wrote:
From: Matthew Wilcox <[email protected]>
Neither of these values get even close to 256;
compound_dtoris currently at a maximum of 3, andcompound_ordercan't be over 64. No machine has inefficient access to bytes since EV5, and while those are still supported, we don't optimise for them any more.So we couild fit
compound_dtorandcompound_orderinto a single byte if desperate?
Yes ... unless we find another kind of destructor we need for compound pages.
Quoted 4 lines
Quoted 2 lines
This does not shrink struct page, but it removes an ifdef and frees up 2-6 bytes for future use.
Can we add a little comment telling readers "hey there's a gap here!"?
I think they should have to work to find it!
Here's a replacement patch:
From: Matthew Wilcox <[email protected]>
Date: Fri, 15 Dec 2017 23:29:11 -0500
Subject: [PATCH] mm: Store compound_dtor / compound_order as bytes
Neither of these values get even close to 256; compound_dtor is currently at a maximum of 3, and compound_order can't be over 64. No machine has inefficient access to bytes since EV5, and while those are still supported, we don't optimise for them any more. This does not shrink struct page, but it removes an ifdef and frees up 2-6 bytes for future use.
diff of pahole output:
@@ -34,8 +34,8 @@
struct callback_head callback_head; /* 32 16 */
struct {
long unsigned int compound_head; /* 32 8 */ - unsigned int compound_dtor; /* 40 4 */ - unsigned int compound_order; /* 44 4 */ + unsigned char compound_dtor; /* 40 1 */ + unsigned char compound_order; /* 41 1 */ }; /* 32 16 */
}; /* 32 16 */
union {
--- include/linux/mm_types.h | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
include/linux/mm_types.h +3 -13
@@ -136,19 +136,9 @@ struct page {unsigned long compound_head;/* If bit zero is set */ /* First tail page only */ -#ifdef CONFIG_64BIT -/* -* On 64 bit system we have enough space in struct page -* to encode compound_dtor and compound_order with -* unsigned int. It can help compiler generate better or -* smaller code on some archtectures. -*/ -unsigned int compound_dtor; -unsigned int compound_order; -#else -unsigned short int compound_dtor; -unsigned short int compound_order; -#endif +unsigned char compound_dtor; +unsigned char compound_order; +/* two/six bytes available here */ };#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) &&USE_SPLIT_PMD_PTLOCKS
Signature · 11 lines
2.15.1 Seriously, this is only available in the first tail page of a compound page, so they'll have to go through Kirill to have it assigned to them ... I don't want to pretend like it's available for general use. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 7/8 mm: Document how to use struct page
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
Be really explicit about what bits / bytes are reserved for users that want to store extra information about the pages they allocate.
--- include/linux/mm_types.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
include/linux/mm_types.h +23 -1
@@ -31,7 +31,29 @@ struct hmm;* it to keep track of whatever it is we are using the page for at the * moment. Note that we have no way to track which tasks are using * a page, though if it is a pagecache page, rmap structures can tell us -* who is mapping it. +* who is mapping it. If you allocate the page using alloc_pages(), you +* can use some of the space in struct page for your own purposes. +* +* Pages that were once in the page cache may be found under the RCU lock +* even after they have been recycled to a different purpose. The page +* cache reads and writes some of the fields in struct page to pin the +* page before checking that it's still in the page cache. It is vital +* that all users of struct page: +* 1. Use the first word as PageFlags. +* 2. Clear or preserve bit 0 of page->compound_head. It is used as +* PageTail for compound pages, and the page cache must not see false +* positives. Some users put a pointer here (guaranteed to be at least +* 4-byte aligned), other users avoid using the field altogether. +* 3. page->_refcount must either not be used, or must be used in such a +* way that other CPUs temporarily incrementing and then decrementing the +* refcount does not cause problems. On receiving the page from +* alloc_pages(), the refcount will be positive. +* 4. Either preserve page->_mapcount or restore it to -1 before freeing it. +* +* If you allocate pages of order > 0, you can use the fields in the struct +* page associated with each page, but bear in mind that the pages may have +* been inserted individually into the page cache, so you must use the above +* four fields in a compatible way for each struct page. * * SLUB uses cmpxchg_double() to atomically update its freelist and * counters. That requires that freelist & counters be adjacent and
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 8/8 mm: Remove reference to PG_buddy
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
PG_buddy doesn't exist any more. It's called PageBuddy now.
--- include/linux/mm_types.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
include/linux/mm_types.h +7 -7
@@ -174,13 +174,13 @@ struct page { };union { -unsigned long private;/* Mapping-private opaque data: -* usually used for buffer_heads -* if PagePrivate set; used for -* swp_entry_t if PageSwapCache; -* indicates order in the buddy -* system if PG_buddy is set. -*/ +/* +* Mapping-private opaque data: +* Usually used for buffer_heads if PagePrivate +* Used for swp_entry_t if PageSwapCache +* Indicates order in the buddy system if PageBuddy +*/ +unsigned long private;#if USE_SPLIT_PTE_PTLOCKS #if ALLOC_SPLIT_PTLOCKS spinlock_t *ptl;
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>