re-shrink 'struct page' when SLUB is on.
Subsystems (7)
cover re-shrink 'struct page' when SLUB is on.
This is a minor update from the last version. The most notable thing is that I was able to demonstrate that maintaining the cmpxchg16 optimization has _some_ value.
These are still of RFC quality. They're stable, but definitely needs some wider testing, especially on 32-bit. Mostly just resending for Christoph to take a look.
These currently apply on top of linux-next.
Otherwise, the code changes are just a few minor cleanups.
---
SLUB depends on a 16-byte cmpxchg for an optimization which allows it to not disable interrupts in its fast path. This optimization has some small but measurable benefits stemming from the cmpxchg code not needing to disable interrrupts:
http://www.sr71.net/~dave/intel/slub/slub-perf-20140109.png
In order to get guaranteed 16-byte alignment (required by the hardware on x86), 'struct page' is padded out from 56 to 64 bytes.
Those 8-bytes matter. We've gone to great lengths to keep 'struct page' small in the past. It's a shame that we bloat it now just for alignment reasons when we have extra space. Plus, bloating such a commonly-touched structure *HAS* cache footprint implications. The implications can be easily shown with 'proc stat' when doing 16.8M kmalloc(32)/kfree() pairs:
vanilla 64-byte struct page:
Quoted 2 lines
883,412 LLC-loads # 0.296 M/sec
566,546 LLC-load-misses # 64.13% of all LL-cache hitspatched 56-byte struct page:
Quoted 2 lines
556,751 LLC-loads # 0.186 M/sec
339,106 LLC-load-misses # 60.91% of all LL-cache hitsThese patches attempt _internal_ alignment instead of external alignment for slub.
I also got a bug report from some folks running a large database benchmark. Their old kernel uses slab and their new one uses slub. They were swapping and couldn't figure out why. It turned out to be the 2GB of RAM that the slub padding wastes on their system.
On my box, that 2GB cost about $200 to populate back when we bought it. I want my $200 back.
This set takes me from 16909584K of reserved memory at boot down to 14814472K, so almost *exactly* 2GB of savings! It also helps performance, presumably because it touches 14% fewer struct page cachelines. A 30GB dd to a ramfs file:
dd if=/dev/zero of=bigfile bs=$((1<<30)) count=30
is sped up by about 4.4% in my testing.
I've run this through its paces and have not had stability issues with it. It definitely needs some more testing, but it's definitely ready for a wider audience.
I also wrote up a document describing 'struct page's layout:
--
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>
RFC PATCH 1/9 mm: slab/slub: use page->list consistently instead of page->lru
From: Dave Hansen <[email protected]>
'struct page' has two list_head fields: 'lru' and 'list'. Conveniently, they are unioned together. This means that code can use them interchangably, which gets horribly confusing like with this nugget from slab.c:
Quoted 3 lines
list_del(&page->lru); if (page->active == cachep->num) list_add(&page->list, &n->slabs_full);
This patch makes the slab and slub code use page->lru universally instead of mixing ->list and ->lru.
So, the new rule is: page->lru is what the you use if you want to keep your page on a list. Don't like the fact that it's not called ->list? Too bad.
---
b/include/linux/mm_types.h | 3 ++- b/mm/slab.c | 4 ++-- b/mm/slob.c | 10 +++++----- 3 files changed, 9 insertions(+), 8 deletions(-)
include/linux/mm_types.h renamed from include/linux/mm_types.h~make-slab-use-page-lru-vs-list-consistently +2 -1
@@ -124,6 +124,8 @@ struct page {union {struct list_head lru;/* Pageout list, eg. active_list * protected by zone->lru_lock ! +* Can be used as a generic list +* by the page owner. */ struct {/* slub per cpu partial pages */ struct page *next;/* Next partial slab */
@@ -136,7 +138,6 @@ struct page {#endif }; -struct list_head list;/* slobs list of pages */ struct slab *slab_page;/* slab fields */ struct rcu_head rcu_head;/* Used by SLAB * when destroying via RCU
mm/slab.c renamed from mm/slab.c~make-slab-use-page-lru-vs-list-consistently +2 -2
@@ -2886,9 +2886,9 @@ retry:/* move slabp to correct slabp list: */ list_del (&page->lru);if (page->active == cachep->num) -list_add (&page->list, &n->slabs_full); +list_add (&page->lru, &n->slabs_full);else -list_add (&page->list, &n->slabs_partial); +list_add (&page->lru, &n->slabs_partial); } must_grow:
mm/slob.c renamed from mm/slob.c~make-slab-use-page-lru-vs-list-consistently +5 -5
@@ -111,13 +111,13 @@ static inline int slob_page_free(structstatic void set_slob_page_free (struct page *sp,struct list_head *list) { -list_add (&sp->list, list); +list_add (&sp->lru, list);__SetPageSlobFree (sp); }static inline void clear_slob_page_free (struct page *sp) { -list_del (&sp->list); +list_del (&sp->lru);__ClearPageSlobFree (sp); }
@@ -282,7 +282,7 @@ static void *slob_alloc(size_t size, gfpspin_lock_irqsave (&slob_lock, flags);/* Iterate through each partially free page, try to find room */ -list_for_each_entry (sp, slob_list, list) { +list_for_each_entry (sp, slob_list, lru) {#ifdef CONFIG_NUMA /* * If there's a node specification, search for a partial
@@ -296,7 +296,7 @@ static void *slob_alloc(size_t size, gfpcontinue ;/* Attempt to alloc */ - prev = sp->list.prev; + prev = sp->lru.prev; b =slob_page_alloc (sp, size, align);if (!b)continue ;
@@ -322,7 +322,7 @@ static void *slob_alloc(size_t size, gfpspin_lock_irqsave (&slob_lock, flags); sp->units =SLOB_UNITS (PAGE_SIZE ); sp->freelist = b; -INIT_LIST_HEAD (&sp->list); +INIT_LIST_HEAD (&sp->lru);set_slob (b,SLOB_UNITS (PAGE_SIZE ), b +SLOB_UNITS (PAGE_SIZE ));set_slob_page_free (sp, slob_list); b =slob_page_alloc (sp, size, align);
_
--
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: [RFC][PATCH 1/9] mm: slab/slub: use page->list consistently instead of page->lru
Quoted 2 lines · Dave Hansen
This patch makes the slab and slub code use
page->lruuniversally instead of mixing ->list and ->lru.
--
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>
David Rientjes
reviewerRe: [RFC][PATCH 1/9] mm: slab/slub: use page->list consistently instead of page->lru
In reply to Dave HansenQuoted 13 lines · Dave Hansen
diff -puN
include/linux/mm_types.h~make-slab-use-page-lru-vs-list-consistentlyinclude/linux/mm_types.hinclude/linux/mm_types.h renamed from include/linux/mm_types.h~make-slab-use-page-lru-vs-list-consistently +2
-0@@ -124,6 +124,8 @@ struct page {union {struct list_head lru;/* Pageout list, eg. active_list * protected by zone->lru_lock ! +* Can be used as a generic list +* by the page owner. */ struct {/* slub per cpu partial pages */ struct page *next;/* Next partial slab */
include/linux/mm_types.h struct page { · line 143
Quoted 7 lines
include/linux/mm_types.h renamed from include/linux/mm_types.h~make-slab-use-page-lru-vs-list-consistently +0
-1@@ -136,7 +138,6 @@ struct page {#endif }; -struct list_head list;/* slobs list of pages */ struct slab *slab_page;/* slab fields */ struct rcu_head rcu_head;/* Used by SLAB * when destroying via RCU
Did you try with a CONFIG_BLOCK config?
block/blk-mq.c: In function a??blk_mq_free_rq_mapa??:block/blk-mq.c:1094:10: error: a??struct pagea?? has no member named a??lista?? block/blk-mq.c:1094:10: warning: initialization from incompatible pointer type [enabled by default] block/blk-mq.c:1094:10: error: a??struct pagea?? has no member named a??lista?? block/blk-mq.c:1095:22: error: a??struct pagea?? has no member named a??lista?? block/blk-mq.c: In function a??blk_mq_init_rq_mapa??:block/blk-mq.c:1159:22: error: a??struct pagea?? has no member named a??lista??
Re: [RFC][PATCH 1/9] mm: slab/slub: use page->list consistently instead of page->lru
Quoted 9 lines · David Rientjes
Did you try with a
CONFIG_BLOCKconfig?
block/blk-mq.c: In function ‘blk_mq_free_rq_map’:block/blk-mq.c:1094:10: error: ‘struct page’ has no member named ‘list’block/blk-mq.c:1094:10: warning: initialization from incompatible pointer type [enabled by default]block/blk-mq.c:1094:10: error: ‘struct page’ has no member named ‘list’block/blk-mq.c:1095:22: error: ‘struct page’ has no member named ‘list’block/blk-mq.c: In function ‘blk_mq_init_rq_map’:block/blk-mq.c:1159:22: error: ‘struct page’ has no member named ‘list’
As I mentioned in the introduction, these are against linux-next. There's a patch in there at the moment which fixed this.
Re: [RFC][PATCH 1/9] mm: slab/slub: use page->list consistently instead of page->lru
Quoted 11 lines · Dave Hansen
Quoted 7 lines
block/blk-mq.c: In function a??blk_mq_free_rq_mapa??:block/blk-mq.c:1094:10: error: a??struct pagea?? has no member named a??lista??block/blk-mq.c:1094:10: warning: initialization from incompatible pointer type [enabled by default]block/blk-mq.c:1094:10: error: a??struct pagea?? has no member named a??lista??block/blk-mq.c:1095:22: error: a??struct pagea?? has no member named a??lista??block/blk-mq.c: In function a??blk_mq_init_rq_mapa??:block/blk-mq.c:1159:22: error: a??struct pagea?? has no member named a??lista??As I mentioned in the introduction, these are against linux-next. There's a patch in there at the moment which fixed this.
Ok, thanks, I like this patch.
Re: [RFC][PATCH 1/9] mm: slab/slub: use page->list consistently instead of page->lru
In reply to Dave HansenQuoted 12 lines · Dave Hansen
From: Dave Hansen <[email protected]>
'struct page' has two
list_headfields: 'lru' and 'list'. Conveniently, they are unioned together. This means that code can use them interchangably, which gets horribly confusing like with this nugget fromslab.c:Quoted 3 lines
list_del(&page->lru); if (page->active == cachep->num) list_add(&page->list, &n->slabs_full);
Quoted 8 lines
This patch makes the slab and slub code use
page->lruuniversally instead of mixing ->list and ->lru.So, the new rule is:
page->lruis what the you use if you want to keep your page on a list. Don't like the fact that it's not called ->list? Too bad.Signed-off-by Dave Hansen <[email protected]>
Signature · 1 line
Kirill A. Shutemov
RFC PATCH 2/9 mm: slub: abstract out double cmpxchg option
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
I found this useful to have in my testing. I would like to have it available for a bit, at least until other folks have had a chance to do some testing with it.
We should probably just pull the help text and the description out of this so that folks are not prompted for it instead of ripping it out entirely.
---
b/include/linux/mm_types.h | 3 +-- b/init/Kconfig | 11 +++++++++++ b/mm/slub.c | 9 +++------ 3 files changed, 15 insertions(+), 8 deletions(-)
include/linux/mm_types.h renamed from include/linux/mm_types.h~abstract-out-slub-double-cmpxchg-operation +1 -2
@@ -73,8 +73,7 @@ struct page { };union { -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE ) +#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE )/* Used for cmpxchg_double in slub */ unsigned long counters;#else
init/Kconfig renamed from init/Kconfig~abstract-out-slub-double-cmpxchg-operation +11 -0
@@ -1603,6 +1603,17 @@ config SLUB_CPU_PARTIAL which requires the taking of locks that may cause latency spikes. Typically one would choose no for a realtime system. +config SLUB_ATTEMPT_CMPXCHG_DOUBLE + default y + depends on SLUB && HAVE_CMPXCHG_DOUBLE && HAVE_ALIGNED_STRUCT_PAGE + bool "SLUB: attempt to use double-cmpxchg operations" + help + Some CPUs support instructions that let you do a large double-word + atomic cmpxchg operation. This keeps the SLUB fastpath from + needing to disable interrupts. + + If you are unsure, say y. + config MMAP_ALLOW_UNINITIALIZED bool "Allow mmapped anonymous memory to be uninitialized" depends on EXPERT && !MMU
mm/slub.c renamed from mm/slub.c~abstract-out-slub-double-cmpxchg-operation +3 -6
@@ -362,8 +362,7 @@ static inline bool __cmpxchg_double_slab const char *n) { VM_BUG_ON(!irqs_disabled()); -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) +#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE) if (s->flags & __CMPXCHG_DOUBLE) { if (cmpxchg_double(&page->freelist, &page->counters, freelist_old, counters_old,
@@ -398,8 +397,7 @@ static inline bool cmpxchg_double_slab(s void *freelist_new, unsigned long counters_new, const char *n) { -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) +#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE) if (s->flags & __CMPXCHG_DOUBLE) { if (cmpxchg_double(&page->freelist, &page->counters, freelist_old, counters_old,
@@ -3078,8 +3076,7 @@ static int kmem_cache_open(struct kmem_c } } -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE ) +#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE )if (system_has_cmpxchg_double () && (s->flags &SLAB_DEBUG_FLAGS ) ==0 )/* Enable fast mode */ s->flags |= __CMPXCHG_DOUBLE;
_
--
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: [RFC][PATCH 2/9] mm: slub: abstract out double cmpxchg option
Quoted 3 lines · Dave Hansen
I found this useful to have in my testing. I would like to have it available for a bit, at least until other folks have had a chance to do some testing with it.
I dont really see the point of this patch since we already have CONFIG_HAVE_ALIGNED_STRUCT_PAGE to play with.
--
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: [RFC][PATCH 2/9] mm: slub: abstract out double cmpxchg option
Quoted 7 lines · Christoph Lameter
Quoted 3 lines · Dave Hansen
I found this useful to have in my testing. I would like to have it available for a bit, at least until other folks have had a chance to do some testing with it.
I dont really see the point of this patch since we already have
CONFIG_HAVE_ALIGNED_STRUCT_PAGEto play with.
With the current code, if you wanted to turn off the double-cmpxchg abd get a 56-byte 'struct page' how would you do it? Can you do it with a .config, or do you need to hack the code?
David Rientjes
reviewerRe: [RFC][PATCH 2/9] mm: slub: abstract out double cmpxchg option
Quoted 5 lines · Dave Hansen
Quoted 5 lines · Dave Hansen
Quoted 3 lines
I found this useful to have in my testing. I would like to have it available for a bit, at least until other folks have had a chance to do some testing with it.
Quoted 7 lines
Quoted 2 lines
I dont really see the point of this patch since we already have
CONFIG_HAVE_ALIGNED_STRUCT_PAGEto play with.With the current code, if you wanted to turn off the double-cmpxchg abd get a 56-byte 'struct page' how would you do it? Can you do it with a .config, or do you need to hack the code?
If that's the intention of disabling this new config option, then it should probably mention the savings in the "help" section, similar to what CONFIG_HAVE_ALIGNED_STRUCT_PAGE says.
--
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: [RFC][PATCH 2/9] mm: slub: abstract out double cmpxchg option
In reply to Dave HansenQuoted 6 lines · Dave Hansen
Quoted 5 lines · Christoph Lameter
Quoted 3 lines · Dave Hansen
I found this useful to have in my testing. I would like to have it available for a bit, at least until other folks have had a chance to do some testing with it.
Quoted 6 lines
Quoted 2 lines
I dont really see the point of this patch since we already have
CONFIG_HAVE_ALIGNED_STRUCT_PAGEto play with.With the current code, if you wanted to turn off the double-cmpxchg abd get a 56-byte 'struct page' how would you do it? Can you do it with a .config, or do you need to hack the code?
Remove HAVE_ALIGNED_STRUCT_PAGE from a Kconfig file.
--
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: [RFC][PATCH 2/9] mm: slub: abstract out double cmpxchg option
Quoted 6 lines · Christoph Lameter
Quoted 3 lines · Dave Hansen
With the current code, if you wanted to turn off the double-cmpxchg abd get a 56-byte 'struct page' how would you do it? Can you do it with a .config, or do you need to hack the code?
Remove
HAVE_ALIGNED_STRUCT_PAGEfrom aKconfigfile.
SLUB 'selects' it, so it seems to pop back up whenever SLUB is on:
$ grep STRUCT_PAGE ~/build/64bit/.config CONFIG_HAVE_ALIGNED_STRUCT_PAGE=y $ vi ~/build/64bit/.config ... remove from Kconfig file $ grep STRUCT_PAGE ~/build/64bit/.config $ make oldconfig ... no prompt $ grep STRUCT_PAGE ~/build/64bit/.config CONFIG_HAVE_ALIGNED_STRUCT_PAGE=y
RFC PATCH 3/9 mm: page->pfmemalloc only used by slab/skb
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
page->pfmemalloc does not deserve a spot in 'struct page'. It is only used transiently _just_ after a page leaves the buddy allocator.
Instead of declaring a union, we move its functionality behind a few quick accessor functions. This way we could also much more easily audit that it is being used correctly in debugging scenarios. For instance, we could store a magic number in there which could never get reused as a page->index and check that the magic number exists in page_pfmemalloc().
---
b/include/linux/mm.h | 17 +++++++++++++++++ b/include/linux/mm_types.h | 9 --------- b/include/linux/skbuff.h | 10 +++++----- b/mm/page_alloc.c | 2 +- b/mm/slab.c | 4 ++-- b/mm/slub.c | 2 +- 6 files changed, 26 insertions(+), 18 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~page_pfmemalloc-only-used-by-slab +17 -0
@@ -2059,5 +2059,22 @@ void __init setup_nr_node_ids(void);static inline void setup_nr_node_ids (void ) {}#endif +/* +* If set by the page allocator, ALLOC_NO_WATERMARKS was set and the +* low watermark was not met implying that the system is under some +* pressure. The caller should try ensure this page is only used to +* free other pages. Currently only used by sl[au]b. Note that +* this is only valid for a short time after the page returns +* from the allocator. +*/ +static inline int page_pfmemalloc (struct page *page) +{ +return !!page->index; +} +static inline void set_page_pfmemalloc (struct page *page,int pfmemalloc) +{ + page->index = pfmemalloc; +} +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
include/linux/mm_types.h renamed from include/linux/mm_types.h~page_pfmemalloc-only-used-by-slab +0 -9
@@ -61,15 +61,6 @@ struct page {union {pgoff_t index;/* Our offset within mapping. */ void *freelist;/* sl[aou]b first free object */ -bool pfmemalloc;/* If set by the page allocator, -* ALLOC_NO_WATERMARKS was set -* and the low watermark was not -* met implying that the system -* is under some pressure. The -* caller should try ensure -* this page is only used to -* free other pages. -*/ };union {
include/linux/skbuff.h renamed from include/linux/skbuff.h~page_pfmemalloc-only-used-by-slab +5 -5
@@ -1399,11 +1399,11 @@ static inline void __skb_fill_page_desc(skb_frag_t *frag = &skb_shinfo (skb)->frags[i];/* -* Propagate page->pfmemalloc to the skb if we can. The problem is +* Propagate page_pfmemalloc() to the skb if we can. The problem is * that not all callers have unique ownership of the page. If * pfmemalloc is set, we check the mapping as a mapping implies * page->index is set (index and pfmemalloc share space). -* If it's a valid mapping, we cannot use page->pfmemalloc but we +* If it's a valid mapping, we cannot use page_pfmemalloc() but we * do not lose pfmemalloc information as the pages would not be * allocated using __GFP_MEMALLOC. */
@@ -1412,7 +1412,7 @@ static inline void __skb_fill_page_desc(skb_frag_size_set (frag, size); page =compound_head (page); -if (page->pfmemalloc && !page->mapping) +if (page_pfmemalloc (page) && !page->mapping) skb->pfmemalloc = true; }
@@ -1999,7 +1999,7 @@ static inline struct page *__skb_alloc_p gfp_mask |= __GFP_MEMALLOC; page =alloc_pages_node (NUMA_NO_NODE , gfp_mask, order); -if (skb && page && page->pfmemalloc) +if (skb && page &&page_pfmemalloc (page)) skb->pfmemalloc = true;return page;
@@ -2028,7 +2028,7 @@ static inline struct page *__skb_alloc_pstatic inline void skb_propagate_pfmemalloc (struct page *page,struct sk_buff *skb) { -if (page && page->pfmemalloc) +if (page &&page_pfmemalloc (page)) skb->pfmemalloc = true; }
mm/page_alloc.c renamed from mm/page_alloc.c~page_pfmemalloc-only-used-by-slab +1 -1
@@ -2073,7 +2073,7 @@ this_zone_full:* memory. The caller should avoid the page being used * for !PFMEMALLOC purposes. */ - page->pfmemalloc = !!(alloc_flags &ALLOC_NO_WATERMARKS ); +set_page_pfmemalloc (page, alloc_flags &ALLOC_NO_WATERMARKS );return page; }
mm/slab.c renamed from mm/slab.c~page_pfmemalloc-only-used-by-slab +2 -2
@@ -1672,7 +1672,7 @@ static struct page *kmem_getpages(struct }/* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */ -if (unlikely (page->pfmemalloc)) +if (unlikely (page_pfmemalloc (page))) pfmemalloc_active = true; nr_pages = (1 << cachep->gfporder);
@@ -1683,7 +1683,7 @@ static struct page *kmem_getpages(structadd_zone_page_state (page_zone (page),NR_SLAB_UNRECLAIMABLE , nr_pages);__SetPageSlab (page); -if (page->pfmemalloc) +if (page_pfmemalloc (page))SetPageSlabPfmemalloc (page);memcg_bind_pages (cachep, cachep->gfporder);
mm/slub.c renamed from mm/slub.c~page_pfmemalloc-only-used-by-slab +1 -1
@@ -1401,7 +1401,7 @@ static struct page *new_slab(struct kmemmemcg_bind_pages (s, order); page->slab_cache = s;__SetPageSlab (page); -if (page->pfmemalloc) +if (page_pfmemalloc (page))SetPageSlabPfmemalloc (page); start =page_address (page);
_
Re: [RFC][PATCH 3/9] mm: page->pfmemalloc only used by slab/skb
Quoted 3 lines · Dave Hansen
page->pfmemallocdoes not deserve a spot in 'struct page'. It is only used transiently _just_ after a page leaves the buddy allocator.
Why would we need to do this if we are removing the cmpxchg_double?
--
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: [RFC][PATCH 3/9] mm: page->pfmemalloc only used by slab/skb
Quoted 6 lines · Christoph Lameter
Quoted 3 lines · Dave Hansen
page->pfmemallocdoes not deserve a spot in 'struct page'. It is only used transiently _just_ after a page leaves the buddy allocator.Why would we need to do this if we are removing the
cmpxchg_double?
Why do we need the patch?
'struct page' is a mess. It's really hard to follow, and the space in the definition is a limited resource. We should not waste that space on such a transient and unimportant value as pfmemalloc.
David Rientjes
reviewerRe: [RFC][PATCH 3/9] mm: page->pfmemalloc only used by slab/skb
Quoted 4 lines · Dave Hansen
Quoted 4 lines
Quoted 3 lines
page->pfmemallocdoes not deserve a spot in 'struct page'. It is only used transiently _just_ after a page leaves the buddy allocator.
Quoted 8 lines
Quoted 1 line
Why would we need to do this if we are removing the
cmpxchg_double?Why do we need the patch?
'struct page' is a mess. It's really hard to follow, and the space in the definition is a limited resource. We should not waste that space on such a transient and unimportant value as pfmemalloc.
I don't have any strong opinions on whether this patch is merged or not, but I'm not sure it's cleaner to do it with an accessor function that overloads page->index when its placement within the union inside struct page makes that obvious, nor is it good that the patch adds more code than it removes solely because it introduces those accessor functions.
--
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: [RFC][PATCH 3/9] mm: page->pfmemalloc only used by slab/skb
In reply to Dave HansenQuoted 7 lines · Dave Hansen
From: Dave Hansen <[email protected]>
page->pfmemallocdoes not deserve a spot in 'struct page'. It is only used transiently _just_ after a page leaves the buddy allocator.
Quoted 8 lines
Instead of declaring a union, we move its functionality behind a few quick accessor functions. This way we could also much more easily audit that it is being used correctly in debugging scenarios. For instance, we could store a magic number in there which could never get reused as a
page->indexand check that the magic number exists inpage_pfmemalloc().Signed-off-by Dave Hansen <[email protected]>
Signature · 7 lines
Kirill A. Shutemov -- 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>
RFC PATCH 4/9 mm: slabs: reset page at free
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
In order to simplify 'struct page', we will shortly be moving some fields around. This causes slub's ->freelist usage to impinge on page->mapping's storage space. The buddy allocator wants ->mapping to be NULL when a page is handed back, so we have to make sure that it is cleared.
Note that slab already doeds this, so just create a common helper and have all the slabs do it this way. ->mapping is right next to ->flags, so it's virtually guaranteed to be in the L1 at this point, so this shouldn't cost very much to do in practice.
Other allocators and users of 'struct page' may also want to call this if they use parts of 'struct page' for nonstandard purposes.
---
b/include/linux/mm.h | 11 +++++++++++ b/mm/slab.c | 3 +-- b/mm/slob.c | 2 +- b/mm/slub.c | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~slub-reset-page-at-free +11 -0
@@ -2076,5 +2076,16 @@ static inline void set_page_pfmemalloc(s page->index = pfmemalloc; } +/* +* Custom allocators (like the slabs) use 'struct page' fields +* for all kinds of things. This resets the page's state so that +* the buddy allocator will be happy with it. +*/ +static inline void allocator_reset_page (struct page *page) +{ + page->mapping =NULL ; +page_mapcount_reset (page); +} +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
mm/slab.c renamed from mm/slab.c~slub-reset-page-at-free +1 -2
@@ -1718,8 +1718,7 @@ static void kmem_freepages(struct kmem_cBUG_ON (!PageSlab (page));__ClearPageSlabPfmemalloc (page);__ClearPageSlab (page); -page_mapcount_reset (page); - page->mapping =NULL ; +allocator_reset_page (page);memcg_release_pages (cachep, cachep->gfporder);if (current->reclaim_state)
mm/slob.c renamed from mm/slob.c~slub-reset-page-at-free +1 -1
@@ -360,7 +360,7 @@ static void slob_free(void *block, int sclear_slob_page_free (sp);spin_unlock_irqrestore (&slob_lock, flags);__ClearPageSlab (sp); -page_mapcount_reset (sp); +allocator_reset_page (sp);slob_free_pages (b,0 );return ; }
mm/slub.c renamed from mm/slub.c~slub-reset-page-at-free +1 -1
@@ -1450,7 +1450,7 @@ static void __free_slab(struct kmem_cach__ClearPageSlab (page);memcg_release_pages (s, order); -page_mapcount_reset (page); +allocator_reset_page (page);if (current->reclaim_state) current->reclaim_state->reclaimed_slab += pages;__free_memcg_kmem_pages (page, order);
_
--
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>
David Rientjes
reviewerRe: [RFC][PATCH 4/9] mm: slabs: reset page at free
include/linux/mm.h static inline void set_page_pfmemalloc(s · line 2084
Quoted 13 lines · Dave Hansen
diff -puN
include/linux/mm.h~slub-reset-page-at-freeinclude/linux/mm.hinclude/linux/mm.h renamed from include/linux/mm.h~slub-reset-page-at-free +6
-0@@ -2076,5 +2076,16 @@ static inline void set_page_pfmemalloc(s page->index = pfmemalloc; } +/* +* Custom allocators (like the slabs) use 'struct page' fields +* for all kinds of things. This resets the page's state so that +* the buddy allocator will be happy with it. +*/ +static inline void allocator_reset_page (struct page *page)
This is ambiguous as to what "allocator" you're referring to unless we look at the comment. I think it would be better to name it slab_reset_page() or something similar.
include/linux/mm.h static inline void set_page_pfmemalloc(s · line 2091
Quoted 7 lines
include/linux/mm.h renamed from include/linux/mm.h~slub-reset-page-at-free +5
-0+{ + page->mapping =NULL ; +page_mapcount_reset (page); +} +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
--
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: [RFC][PATCH 4/9] mm: slabs: reset page at free
Quoted 9 lines · David Rientjes
Quoted 6 lines
include/linux/mm.h static inline void set_page_pfmemalloc(s · line 2084
Quoted 6 lines
include/linux/mm.h renamed from include/linux/mm.h~slub-reset-page-at-free +6
-0+/* +* Custom allocators (like the slabs) use 'struct page' fields +* for all kinds of things. This resets the page's state so that +* the buddy allocator will be happy with it. +*/ +static inline void allocator_reset_page (struct page *page)This is ambiguous as to what "allocator" you're referring to unless we look at the comment. I think it would be better to name it
slab_reset_page()or something similar.
I stuck it in mm.h and deliberately didn't call it 'slab_something' so that zsmalloc (in staging) could use this as well. The "allocator" part of the name was to indicate that any allocator could use it.
Re: [RFC][PATCH 4/9] mm: slabs: reset page at free
In reply to Dave HansenQuoted 10 lines · Dave Hansen
diff -puN
include/linux/mm.h~slub-reset-page-at-freeinclude/linux/mm.hinclude/linux/mm.h renamed from include/linux/mm.h~slub-reset-page-at-free +3
-0@@ -2076,5 +2076,16 @@ static inline void set_page_pfmemalloc(s page->index = pfmemalloc; } +/* +* Custom allocators (like the slabs) use 'struct page' fields +* for all kinds of things. This resets the page's state so that
include/linux/mm.h static inline void set_page_pfmemalloc(s · line 2091
Quoted 10 lines
include/linux/mm.h renamed from include/linux/mm.h~slub-reset-page-at-free +8
-0+* the buddy allocator will be happy with it. +*/ +static inline void allocator_reset_page (struct page *page) +{ + page->mapping =NULL ; +page_mapcount_reset (page); +} +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
This belongs into mm/slab.h
Otherwise Acked-by: Christoph Lameter <[email protected]>
--
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>
RFC PATCH 5/9 mm: rearrange struct page
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
To make the layout of 'struct page' look nicer, I broke up a few of the unions. But, this has a cost: things that were guaranteed to line up before might not any more. To make up for that, some BUILD_BUG_ON()s are added to manually check for the alignment dependencies.
This makes it *MUCH* more clear how the first few fields of 'struct page' get used by the slab allocators.
---
b/include/linux/mm_types.h | 99 ++++++++++++++++++++++----------------------- b/mm/slab.c | 6 +- b/mm/slab_common.c | 17 +++++++ b/mm/slob.c | 25 +++++------ 4 files changed, 83 insertions(+), 64 deletions(-)
include/linux/mm_types.h renamed from include/linux/mm_types.h~rearrange-struct-page +50 -49
@@ -46,26 +46,59 @@ 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: -* see PAGE_MAPPING_ANON below. -*/ -void *s_mem;/* slab first object */ - }; - -/* Second double word */ -struct { -union { + struct /* the normal uses */ { pgoff_t index; /* Our offset within mapping. */ -void *freelist;/* sl[aou]b first free object */ + /* + * 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: + * see PAGE_MAPPING_ANON below. + */ + struct address_space *mapping; + /* + * Count of ptes mapped in mms, to show when page + * is mapped & limit reverse map searches. + * + * Used also for tail pages refcounting instead + * of _count. Tail pages cannot be mapped and + * keeping the tail page _count zero at all times + * guarantees get_page_unless_zero() will never + * succeed on tail pages. + */ + atomic_t _mapcount; + atomic_t _count; + }; /* end of the "normal" use */ + + struct { /* SLUB */ + void *unused; + void *freelist; + unsigned inuse:16; + unsigned objects:15; + unsigned frozen:1; + atomic_t dontuse_slub_count; }; - -union { + struct { /* SLAB */ + void *s_mem; + void *slab_freelist; + unsigned int active; + atomic_t dontuse_slab_count; + }; + struct { /* SLOB */ + void *slob_unused; + void *slob_freelist; + unsigned int units; + atomic_t dontuse_slob_count; + }; + /* + * This is here to help the slub code deal with + * its inuse/objects/frozen bitfields as a single + * blob. + */ + struct { /* slub helpers */ + void *slubhelp_unused; + void *slubhelp_freelist; #if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE) -/* Used for cmpxchg_double in slub */ unsigned long counters; #else /*
@@ -75,38 +108,6 @@ struct page {*/ unsigned counters;#endif - -struct { - -union { -/* -* Count of ptes mapped in -* mms, to show when page is -* mapped & limit reverse map -* searches. -* -* Used also for tail pages -* refcounting instead of -* _count. Tail pages cannot -* be mapped and keeping the -* tail page _count zero at -* all times guarantees -* get_page_unless_zero() will -* never succeed on tail -* pages. -*/ -atomic_t _mapcount; - -struct {/* SLUB */ -unsigned inuse:16 ; -unsigned objects:15 ; -unsigned frozen:1 ; - }; -int units;/* SLOB */ - }; -atomic_t _count;/* Usage count, see below. */ - }; -unsigned int active;/* SLAB */ }; };
mm/slab.c renamed from mm/slab.c~rearrange-struct-page +3 -3
@@ -1955,7 +1955,7 @@ static void slab_destroy(struct kmem_cac {void *freelist; - freelist = page->freelist; + freelist = page->slab_freelist;slab_destroy_debugcheck (cachep, page);if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) {struct rcu_head *head;
@@ -2543,7 +2543,7 @@ static void *alloc_slabmgmt(struct kmem_static inline unsigned int *slab_freelist (struct page *page) { -return (unsigned int *)(page->freelist); +return (unsigned int *)(page->slab_freelist); } static void cache_init_objs(struct kmem_cache *cachep,
@@ -2648,7 +2648,7 @@ static void slab_map_pages(struct kmem_c void *freelist) { page->slab_cache = cache; - page->freelist = freelist; + page->slab_freelist = freelist; }/*
mm/slab_common.c renamed from mm/slab_common.c~rearrange-struct-page +17 -0
@@ -676,3 +676,20 @@ static int __init slab_proc_init(void) }module_init (slab_proc_init);#endif /* CONFIG_SLABINFO */ +#define SLAB_PAGE_CHECK (field1, field2) \ + BUILD_BUG_ON(offsetof(struct page, field1) != \ + offsetof(struct page, field2)) +/* +* To make the layout of 'struct page' look nicer, we've broken +* up a few of the unions. Folks declaring their own use of the +* first few fields need to make sure that their use does not +* interfere with page->_count. This ensures that the individual +* users' use actually lines up with the real ->_count. +*/ +void slab_build_checks (void ) +{ +SLAB_PAGE_CHECK (_count, dontuse_slab_count); +SLAB_PAGE_CHECK (_count, dontuse_slub_count); +SLAB_PAGE_CHECK (_count, dontuse_slob_count); +} +
mm/slob.c renamed from mm/slob.c~rearrange-struct-page +13 -12
@@ -219,7 +219,8 @@ static void *slob_page_alloc(struct pageslob_t *prev, *cur, *aligned =NULL ;int delta =0 , units =SLOB_UNITS (size); -for (prev =NULL , cur = sp->freelist; ; prev = cur, cur =slob_next (cur)) { +for (prev =NULL , cur = sp->slob_freelist; ; + prev = cur, cur =slob_next (cur)) {slobidx_t avail =slob_units (cur);if (align) {
@@ -243,12 +244,12 @@ static void *slob_page_alloc(struct pageif (prev)set_slob (prev,slob_units (prev), next);else - sp->freelist = next; + sp->slob_freelist = next; }else {/* fragment */ if (prev)set_slob (prev,slob_units (prev), cur + units);else - sp->freelist = cur + units; + sp->slob_freelist = cur + units;set_slob (cur + units, avail - units, next); }
@@ -321,7 +322,7 @@ static void *slob_alloc(size_t size, gfpspin_lock_irqsave (&slob_lock, flags); sp->units =SLOB_UNITS (PAGE_SIZE ); - sp->freelist = b; + sp->slob_freelist = b;INIT_LIST_HEAD (&sp->lru);set_slob (b,SLOB_UNITS (PAGE_SIZE ), b +SLOB_UNITS (PAGE_SIZE ));set_slob_page_free (sp, slob_list);
@@ -368,7 +369,7 @@ static void slob_free(void *block, int sif (!slob_page_free (sp)) {/* This slob page is about to become partially free. Easy! */ sp->units = units; - sp->freelist = b; + sp->slob_freelist = b;set_slob (b, units, (void *)((unsigned long )(b +SLOB_UNITS (PAGE_SIZE )) &PAGE_MASK ));
@@ -388,15 +389,15 @@ static void slob_free(void *block, int s*/ sp->units += units; -if (b < (slob_t *)sp->freelist) { -if (b + units == sp->freelist) { - units +=slob_units (sp->freelist); - sp->freelist =slob_next (sp->freelist); +if (b < (slob_t *)sp->slob_freelist) { +if (b + units == sp->slob_freelist) { + units +=slob_units (sp->slob_freelist); + sp->slob_freelist =slob_next (sp->slob_freelist); } -set_slob (b, units, sp->freelist); - sp->freelist = b; +set_slob (b, units, sp->slob_freelist); + sp->slob_freelist = b; }else { - prev = sp->freelist; + prev = sp->slob_freelist; next =slob_next (prev);while (b > next) { prev = next;
_
--
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: [RFC][PATCH 5/9] mm: rearrange struct page
Quoted 9 lines · Dave Hansen
From: Dave Hansen <[email protected]>
To make the layout of 'struct page' look nicer, I broke up a few of the unions. But, this has a cost: things that were guaranteed to line up before might not any more. To make up for that, some BUILD_BUG_ON()s are added to manually check for the alignment dependencies.
Quoted 4 lines
This makes it *MUCH* more clear how the first few fields of 'struct page' get used by the slab allocators.
Signed-off-by Dave Hansen <[email protected]>
Looks much cleaner!
Signature · 7 lines
Kirill A. Shutemov -- 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: [RFC][PATCH 5/9] mm: rearrange struct page
In reply to Dave HansenQuoted 3 lines · Dave Hansen
This makes it *MUCH* more clear how the first few fields of 'struct page' get used by the slab allocators.
I think this adds to the confusion. What you want to know is which other fields overlap a certain field. After this patch you wont have that anymore.
Re: [RFC][PATCH 5/9] mm: rearrange struct page
Quoted 7 lines · Christoph Lameter
Quoted 2 lines · Dave Hansen
This makes it *MUCH* more clear how the first few fields of 'struct page' get used by the slab allocators.
I think this adds to the confusion. What you want to know is which other fields overlap a certain field. After this patch you wont have that anymore.
Why does it matter *specifically* that "index shares space with freelist", or that "mapping shares space with s_mem"? No data is ever handed off in those fields.
All that matters is that we know the _set_ of fields that gets reused, and that we preserve the ones which *need* their contents preserved (only flags and _count as far as I can tell).
Re: [RFC][PATCH 5/9] mm: rearrange struct page
Quoted 3 lines · Dave Hansen
Why does it matter *specifically* that "index shares space with freelist", or that "mapping shares space with
s_mem"? No data is ever handed off in those fields.
If the field is corrupted then one needs to figure out what other ways of using this field exists. If one wants to change the page struct fields then also knowlege about possible interconnections are important.
--
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>
RFC PATCH 6/9 mm: slub: rearrange 'struct page' fields
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
SLUB has some very unique alignment constraints it places on 'struct page'. Break those out in to a separate structure which will not pollute 'struct page'.
This structure will be moved around inside 'struct page' at runtime in the next patch, so it is necessary to break it out for those uses as well.
Vim pattern used for the renames:
%s/\(page\|new\)\(->\|\.\)\(freelist\|counters\)/slub_data(\1)\2\3/g
---
b/include/linux/mm_types.h | 46 ++++++-- b/mm/slab_common.c | 29 ++++- b/mm/slub.c | 253 +++++++++++++++++++++++---------------------- 3 files changed, 192 insertions(+), 136 deletions(-)
include/linux/mm_types.h renamed from include/linux/mm_types.h~slub-rearrange +38 -8
@@ -23,6 +23,43 @@struct address_space ; +struct slub_data { +void *unused; +void *freelist; +union { +struct { +unsigned inuse:16 ; +unsigned objects:15 ; +unsigned frozen:1 ; +atomic_t dontuse_slub_count; + }; +/* +* ->counters is used to make it easier to copy +* all of the above counters in one chunk. +* The actual counts are never accessed via this. +*/ +#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) && \ + defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE ) +unsigned long counters; +#else +/* +* Keep _count separate from slub cmpxchg_double data. +* As the rest of the double word is protected by +* slab_lock but _count is not. +*/ +struct { +unsigned counters; +/* +* This isn't used directly, but declare it here +* for clarity since it must line up with _count +* from 'struct page' +*/ +atomic_t separate_count; + }; +#endif + }; +}; +#define USE_SPLIT_PTE_PTLOCKS (NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS)#define USE_SPLIT_PMD_PTLOCKS (USE_SPLIT_PTE_PTLOCKS && \ IS_ENABLED(CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK))
@@ -70,14 +107,7 @@ struct page {atomic_t _count; };/* end of the "normal" use */ -struct {/* SLUB */ -void *unused; -void *freelist; -unsigned inuse:16 ; -unsigned objects:15 ; -unsigned frozen:1 ; -atomic_t dontuse_slub_count; - }; +struct slub_data slub_data;struct {/* SLAB */ void *s_mem;void *slab_freelist;
mm/slab_common.c renamed from mm/slab_common.c~slub-rearrange +24 -5
@@ -676,20 +676,39 @@ static int __init slab_proc_init(void) }module_init (slab_proc_init);#endif /* CONFIG_SLABINFO */ +#define SLAB_PAGE_CHECK (field1, field2) \ BUILD_BUG_ON(offsetof(struct page, field1) != \ offsetof(struct page, field2))/* * To make the layout of 'struct page' look nicer, we've broken -* up a few of the unions. Folks declaring their own use of the -* first few fields need to make sure that their use does not -* interfere with page->_count. This ensures that the individual -* users' use actually lines up with the real ->_count. +* up a few of the unions. But, this has made it hard to see if +* any given use will interfere with page->_count. +* +* To work around this, each user declares their own _count field +* and we check them at build time to ensure that the independent +* definitions actually line up with the real ->_count. */ void slab_build_checks (void ) {SLAB_PAGE_CHECK (_count, dontuse_slab_count); -SLAB_PAGE_CHECK (_count, dontuse_slub_count); +SLAB_PAGE_CHECK (_count, slub_data.dontuse_slub_count);SLAB_PAGE_CHECK (_count, dontuse_slob_count); + +/* +* When doing a double-cmpxchg, the slub code sucks in +* _count. But, this is harmless since if _count is +* modified, the cmpxchg will fail. When not using a +* real cmpxchg, the slub code uses a lock. But, _count +* is not modified under that lock and updates can be +* lost if they race with one of the "faked" cmpxchg +* under that lock. This makes sure that the space we +* carve out for _count in that case actually lines up +* with the real _count. +*/ +#if !(defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) && \ + defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE )) +SLAB_PAGE_CHECK (_count, slub_data.separate_count); +#endif }
mm/slub.c renamed from mm/slub.c~slub-rearrange +130 -123
596 lines not on this page · raw message
_
RFC PATCH 7/9 mm: slub: remove 'struct page' alignment restrictions
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
SLUB depends on a 16-byte cmpxchg for an optimization. In order to get guaranteed 16-byte alignment (required by the hardware on x86), 'struct page' is padded out from 56 to 64 bytes.
Those 8-bytes matter. We've gone to great lengths to keep 'struct page' small in the past. It's a shame that we bloat it now just for alignment reasons when we have *extra* space. Also, increasing the size of 'struct page' by 14% makes it 14% more likely that we will miss a cacheline when fetching it.
This patch takes an unused 8-byte area of slub's 'struct page' and reuses it to internally align to the 16-bytes that we need.
Note that this also gets rid of the ugly slub #ifdef that we use to segregate ->counters and ->_count for cases where we need to manipulate ->counters without the benefit of a hardware cmpxchg.
This patch takes me from 16909584K of reserved memory at boot down to 14814472K, so almost *exactly* 2GB of savings! It also helps performance, presumably because of that 14% fewer cacheline effect. A 30GB dd to a ramfs file:
dd if=/dev/zero of=bigfile bs=$((1<<30)) count=30
is sped up by about 4.4% in my testing.
The value of maintaining the cmpxchg16 operation can be demonstrated in some tiny little microbenchmarks, so it is probably something we should keep around instead of just using the spinlock for everything:
http://lkml.kernel.org/r/[email protected]
---
b/arch/Kconfig | 8 ------- b/arch/s390/Kconfig | 1 b/arch/x86/Kconfig | 1 b/include/linux/mm_types.h | 51 +++++++++++++-------------------------------- b/init/Kconfig | 2 - b/mm/slab_common.c | 10 +++++--- b/mm/slub.c | 4 +++ 7 files changed, 26 insertions(+), 51 deletions(-)
arch/Kconfig renamed from arch/Kconfig~remove-struct-page-alignment-restrictions +0 -8
@@ -289,14 +289,6 @@ config HAVE_RCU_TABLE_FREE config ARCH_HAVE_NMI_SAFE_CMPXCHG bool -config HAVE_ALIGNED_STRUCT_PAGE - bool - help - This makes sure that struct pages are double word aligned and that - e.g. the SLUB allocator can perform double word atomic operations - on a struct page for better performance. However selecting this - might increase the size of a struct page by a word. - config HAVE_CMPXCHG_LOCAL bool
arch/s390/Kconfig renamed from arch/s390/Kconfig~remove-struct-page-alignment-restrictions +0 -1
@@ -102,7 +102,6 @@ config S390 select GENERIC_FIND_FIRST_BIT select GENERIC_SMP_IDLE_THREAD select GENERIC_TIME_VSYSCALL - select HAVE_ALIGNED_STRUCT_PAGE if SLUB select HAVE_ARCH_JUMP_LABEL if !MARCH_G5 select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK
arch/x86/Kconfig renamed from arch/x86/Kconfig~remove-struct-page-alignment-restrictions +0 -1
@@ -78,7 +78,6 @@ config X86 select HAVE_PERF_USER_STACK_DUMP select HAVE_DEBUG_KMEMLEAK select ANON_INODES - select HAVE_ALIGNED_STRUCT_PAGE if SLUB select HAVE_CMPXCHG_LOCAL select HAVE_CMPXCHG_DOUBLE select HAVE_ARCH_KMEMCHECK
include/linux/mm_types.h renamed from include/linux/mm_types.h~remove-struct-page-alignment-restrictions +15 -36
98 lines not on this page · raw message
init/Kconfig renamed from init/Kconfig~remove-struct-page-alignment-restrictions +1 -1
@@ -1605,7 +1605,7 @@ config SLUB_CPU_PARTIAL config SLUB_ATTEMPT_CMPXCHG_DOUBLE default y - depends on SLUB && HAVE_CMPXCHG_DOUBLE && HAVE_ALIGNED_STRUCT_PAGE + depends on SLUB && HAVE_CMPXCHG_DOUBLE bool "SLUB: attempt to use double-cmpxchg operations" help Some CPUs support instructions that let you do a large double-word
mm/slab_common.c renamed from mm/slab_common.c~remove-struct-page-alignment-restrictions +6 -4
@@ -692,7 +692,6 @@ module_init(slab_proc_init);void slab_build_checks (void ) {SLAB_PAGE_CHECK (_count, dontuse_slab_count); -SLAB_PAGE_CHECK (_count, slub_data.dontuse_slub_count);SLAB_PAGE_CHECK (_count, dontuse_slob_count);/*
@@ -706,9 +705,12 @@ void slab_build_checks(void)* carve out for _count in that case actually lines up * with the real _count. */ -#if !(defined(CONFIG_HAVE_CMPXCHG_DOUBLE ) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE ))SLAB_PAGE_CHECK (_count, slub_data.separate_count); -#endif + +/* +* We need at least three double-words worth of space to +* ensure that we can align to a double-wordk internally. +*/ +BUILD_BUG_ON (sizeof (struct slub_data ) !=sizeof (unsigned long ) *3 ); }
mm/slub.c renamed from mm/slub.c~remove-struct-page-alignment-restrictions +4 -0
@@ -239,7 +239,11 @@ static inline struct kmem_cache_node *gestatic inline struct slub_data *slub_data (struct page *page) { +int doubleword_bytes =BITS_PER_LONG *2 /8 ;void *ptr = &page->slub_data; +#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE ) + ptr =PTR_ALIGN (ptr, doubleword_bytes); +#endif return ptr; }
_
RFC PATCH 8/9 mm: slub: cleanups after code churn
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
I added a bunch of longer than 80 column lines and other various messes. But, doing line-to-line code replacements makes the previous patch much easier to audit. I stuck the cleanups in here instead.
The slub code also delcares a bunch of 'struct page's on the stack. Now that 'struct slub_data' is separate, we can declare those smaller structures instead. This ends up saving us a couple hundred bytes in object size.
Doing all the work of doing the pointer alignment operations over and over costs us some code size. In the end (not this patch alone), we take slub.o's size from 26672->27168, so about 500 bytes. But, on an 8GB system, we saved about 256k in 'struct page' overhead. That's a pretty good tradeoff.
---
b/mm/slub.c | 149 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 78 insertions(+), 71 deletions(-)
mm/slub.c renamed from mm/slub.c~slub-cleanups +78 -71
@@ -257,7 +257,8 @@ static inline int check_valid_pointer(streturn 1 ; base =page_address (page); -if (object < base || object >= base +slub_data (page)->objects * s->size || +if (object < base || + object >= base +slub_data (page)->objects * s->size || (object - base) % s->size) {return 0 ; }
@@ -374,16 +375,17 @@ static inline bool __cmpxchg_double_slabVM_BUG_ON (!irqs_disabled ());#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE )if (s->flags & __CMPXCHG_DOUBLE) { -if (cmpxchg_double (&slub_data (page)->freelist, &slub_data (page)->counters, - freelist_old, counters_old, - freelist_new, counters_new)) -return 1 ; +if (cmpxchg_double (&slub_data (page)->freelist, + &slub_data (page)->counters, + freelist_old, counters_old, + freelist_new, counters_new)) +return 1 ; } else#endif {slab_lock (page);if (slub_data (page)->freelist == freelist_old && -slub_data (page)->counters == counters_old) { +slub_data (page)->counters == counters_old) {slub_data (page)->freelist = freelist_new;slub_data (page)->counters = counters_new;slab_unlock (page);
@@ -407,11 +409,12 @@ static inline bool cmpxchg_double_slab(s void *freelist_new, unsigned long counters_new, const char *n) { + struct slub_data *sd = slub_data(page);#if defined(CONFIG_SLUB_ATTEMPT_CMPXCHG_DOUBLE )if (s->flags & __CMPXCHG_DOUBLE) { - if (cmpxchg_double(&slub_data(page)->freelist, &slub_data(page)->counters, -freelist_old ,counters_old , - freelist_new, counters_new)) +if (cmpxchg_double (&sd->freelist, &sd->counters, + freelist_old, counters_old, + freelist_new, counters_new))return 1 ; } else#endif
@@ -420,10 +423,10 @@ static inline bool cmpxchg_double_slab(slocal_irq_save (flags);slab_lock (page); -if (slub_data (page)->freelist == freelist_old && -slub_data (page)->counters == counters_old) { -slub_data (page)->freelist = freelist_new; -slub_data (page)->counters = counters_new; +if (sd->freelist == freelist_old && + sd->counters == counters_old) { + sd->freelist = freelist_new; + sd->counters = counters_new;slab_unlock (page);local_irq_restore (flags);return 1 ;
@@ -859,7 +862,8 @@ static int check_slab(struct kmem_cache }if (slub_data (page)->inuse >slub_data (page)->objects) {slab_err (s, page,"inuse %u > max %u" , - s->name,slub_data (page)->inuse,slub_data (page)->objects); + s->name,slub_data (page)->inuse, +slub_data (page)->objects);return 0 ; }/* Slab_pad_check fixes things up after itself */
@@ -890,7 +894,8 @@ static int on_freelist(struct kmem_cache }else {slab_err (s, page,"Freepointer corrupt" );slub_data (page)->freelist =NULL ; -slub_data (page)->inuse =slub_data (page)->objects; +slub_data (page)->inuse = +slub_data (page)->objects;slab_fix (s,"Freelist cleared" );return 0 ; }
@@ -913,7 +918,8 @@ static int on_freelist(struct kmem_cache }if (slub_data (page)->inuse !=slub_data (page)->objects - nr) {slab_err (s, page,"Wrong object count. Counter is %d but " -"counted were %d" ,slub_data (page)->inuse,slub_data (page)->objects - nr); +"counted were %d" ,slub_data (page)->inuse, +slub_data (page)->objects - nr);slub_data (page)->inuse =slub_data (page)->objects - nr;slab_fix (s,"Object count adjusted." ); }
@@ -1547,7 +1553,7 @@ static inline void *acquire_slab(struct {void *freelist;unsigned long counters; -struct page new; +struct slub_data new;lockdep_assert_held (&n->list_lock);
@@ -1558,21 +1564,21 @@ static inline void *acquire_slab(struct*/ freelist =slub_data (page)->freelist; counters =slub_data (page)->counters; -slub_data (&new)->counters = counters; - *objects =slub_data (&new)->objects -slub_data (&new)->inuse; + new.counters = counters; + *objects = new.objects - new.inuse;if (mode) { -slub_data (&new)->inuse =slub_data (page)->objects; -slub_data (&new)->freelist =NULL ; + new.inuse =slub_data (page)->objects; + new.freelist =NULL ; }else { -slub_data (&new)->freelist = freelist; + new.freelist = freelist; } -VM_BUG_ON_PAGE (slub_data (&new)->frozen, &new); -slub_data (&new)->frozen =1 ; +VM_BUG_ON_PAGE (new.frozen, &new); + new.frozen =1 ;if (!__cmpxchg_double_slab (s, page, freelist, counters, -slub_data (&new)->freelist,slub_data (&new)->counters, + new.freelist, new.counters,"acquire_slab" ))return NULL ;
@@ -1794,8 +1800,8 @@ static void deactivate_slab(struct kmem_enum slab_modes l =M_NONE , m =M_NONE ;void *nextfree;int tail =DEACTIVATE_TO_HEAD ; -struct page new; -struct page old; +struct slub_data new; +struct slub_data old;if (slub_data (page)->freelist) {stat (s,DEACTIVATE_REMOTE_FREES );
@@ -1818,13 +1824,13 @@ static void deactivate_slab(struct kmem_ prior =slub_data (page)->freelist; counters =slub_data (page)->counters;set_freepointer (s, freelist, prior); -slub_data (&new)->counters = counters; -slub_data (&new)->inuse--; -VM_BUG_ON_PAGE (!slub_data (&new)->frozen, &new); + new.counters = counters; + new.inuse--; +VM_BUG_ON_PAGE (!new.frozen, &new); }while (!__cmpxchg_double_slab (s, page, prior, counters, - freelist,slub_data (&new)->counters, + freelist, new.counters,"drain percpu freelist" )); freelist = nextfree;
@@ -1846,24 +1852,24 @@ static void deactivate_slab(struct kmem_*/ redo: -slub_data (&old)->freelist =slub_data (page)->freelist; -slub_data (&old)->counters =slub_data (page)->counters; -VM_BUG_ON_PAGE (!slub_data (&old)->frozen, &old); + old.freelist =slub_data (page)->freelist; + old.counters =slub_data (page)->counters; +VM_BUG_ON_PAGE (!old.frozen, &old);/* Determine target state of the slab */ -slub_data (&new)->counters =slub_data (&old)->counters; + new.counters = old.counters;if (freelist) { -slub_data (&new)->inuse--; -set_freepointer (s, freelist,slub_data (&old)->freelist); -slub_data (&new)->freelist = freelist; + new.inuse--; +set_freepointer (s, freelist, old.freelist); + new.freelist = freelist; }else -slub_data (&new)->freelist =slub_data (&old)->freelist; + new.freelist = old.freelist; -slub_data (&new)->frozen =0 ; + new.frozen =0 ; -if (!slub_data (&new)->inuse && n->nr_partial > s->min_partial) +if (!new.inuse && n->nr_partial > s->min_partial) m =M_FREE ; -else if (slub_data (&new)->freelist) { +else if (new.freelist) { m =M_PARTIAL ;if (!lock) { lock =1 ;
@@ -1912,8 +1918,8 @@ redo: l = m;if (!__cmpxchg_double_slab (s, page, -slub_data (&old)->freelist,slub_data (&old)->counters, -slub_data (&new)->freelist,slub_data (&new)->counters, + old.freelist, old.counters, + new.freelist, new.counters,"unfreezing slab" )) goto redo;
@@ -1942,8 +1948,8 @@ static void unfreeze_partials(struct kmestruct page *page, *discard_page =NULL ;while ((page = c->partial)) { -struct page new; -struct page old; +struct slub_data new; +struct slub_data old; c->partial = page->next;
@@ -1958,21 +1964,21 @@ static void unfreeze_partials(struct kmedo { -slub_data (&old)->freelist =slub_data (page)->freelist; -slub_data (&old)->counters =slub_data (page)->counters; -VM_BUG_ON_PAGE (!slub_data (&old)->frozen, &old); + old.freelist =slub_data (page)->freelist; + old.counters =slub_data (page)->counters; +VM_BUG_ON_PAGE (!old.frozen, &old); -slub_data (&new)->counters =slub_data (&old)->counters; -slub_data (&new)->freelist =slub_data (&old)->freelist; + new.counters = old.counters; + new.freelist = old.freelist; -slub_data (&new)->frozen =0 ; + new.frozen =0 ; }while (!__cmpxchg_double_slab (s, page, -slub_data (&old)->freelist,slub_data (&old)->counters, -slub_data (&new)->freelist,slub_data (&new)->counters, + old.freelist, old.counters, + new.freelist, new.counters,"unfreezing slab" )); -if (unlikely (!slub_data (&new)->inuse && n->nr_partial > s->min_partial)) { +if (unlikely (!new.inuse && n->nr_partial > s->min_partial)) { page->next = discard_page; discard_page = page; }else {
@@ -2224,7 +2230,7 @@ static inline bool pfmemalloc_match(stru*/ static inline void *get_freelist (struct kmem_cache *s,struct page *page) { -struct page new; +struct slub_data new;unsigned long counters;void *freelist;
@@ -2232,15 +2238,15 @@ static inline void *get_freelist(struct freelist =slub_data (page)->freelist; counters =slub_data (page)->counters; -slub_data (&new)->counters = counters; -VM_BUG_ON_PAGE (!slub_data (&new)->frozen, &new); + new.counters = counters; +VM_BUG_ON_PAGE (!new.frozen, &new); -slub_data (&new)->inuse =slub_data (page)->objects; -slub_data (&new)->frozen = freelist !=NULL ; + new.inuse =slub_data (page)->objects; + new.frozen = freelist !=NULL ; }while (!__cmpxchg_double_slab (s, page, freelist, counters, -NULL ,slub_data (&new)->counters, +NULL , new.counters,"get_freelist" ));return freelist;
@@ -2525,7 +2531,7 @@ static void __slab_free(struct kmem_cachvoid *prior;void **object = (void *)x;int was_frozen; -struct page new; +struct slub_data new;unsigned long counters;struct kmem_cache_node *n =NULL ;unsigned long uninitialized_var (flags );
@@ -2544,10 +2550,10 @@ static void __slab_free(struct kmem_cach prior =slub_data (page)->freelist; counters =slub_data (page)->counters;set_freepointer (s, object, prior); -slub_data (&new)->counters = counters; - was_frozen =slub_data (&new)->frozen; -slub_data (&new)->inuse--; -if ((!slub_data (&new)->inuse || !prior) && !was_frozen) { + new.counters = counters; + was_frozen = new.frozen; + new.inuse--; +if ((!new.inuse || !prior) && !was_frozen) {if (kmem_cache_has_cpu_partial (s) && !prior) {
@@ -2557,7 +2563,7 @@ static void __slab_free(struct kmem_cach* We can defer the list move and instead * freeze it. */ -slub_data (&new)->frozen =1 ; + new.frozen =1 ; }else {/* Needs to be taken off a list */
@@ -2577,7 +2583,7 @@ static void __slab_free(struct kmem_cach }while (!cmpxchg_double_slab (s, page, prior, counters, - object,slub_data (&new)->counters, + object, new.counters,"__slab_free" ));if (likely (!n)) {
@@ -2586,7 +2592,7 @@ static void __slab_free(struct kmem_cach* If we just froze the page then put it onto the * per cpu partial list. */ -if (slub_data (&new)->frozen && !was_frozen) { +if (new.frozen && !was_frozen) {put_cpu_partial (s, page,1 );stat (s,CPU_PARTIAL_FREE ); }
@@ -2599,7 +2605,7 @@ static void __slab_free(struct kmem_cachreturn ; } -if (unlikely (!slub_data (&new)->inuse && n->nr_partial > s->min_partial)) +if (unlikely (!new.inuse && n->nr_partial > s->min_partial)) goto slab_empty;/*
@@ -3423,7 +3429,8 @@ int kmem_cache_shrink(struct kmem_cache* list_lock. ->inuse here is the upper limit. */ list_for_each_entry_safe (page, t, &n->partial, lru) { -list_move (&page->lru, slabs_by_inuse +slub_data (page)->inuse); +list_move (&page->lru, slabs_by_inuse + +slub_data (page)->inuse);if (!slub_data (page)->inuse) n->nr_partial--; }
_
RFC PATCH 9/9 mm: fix alignment checks on 32-bit
In reply to Dave HansenThe checks fail on 32-bit. This needs to get folded back in to the original patches. Please ignore this for now.
---
b/include/linux/mm_types.h | 14 ++++++++------ b/mm/slab_common.c | 16 ++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-)
include/linux/mm_types.h renamed from include/linux/mm_types.h~fix-slub-size-32-bit +8 -6
@@ -37,19 +37,21 @@ struct slub_data {* slab_lock but _count is not. */ struct { -/* counters is just a helperfor the above bitfield */ +/* counters is just a helper for the above bitfield */ unsigned long counters; -atomic_t padding; +atomic_t mapcount_padding;atomic_t separate_count; };/* -* the double-cmpxchg case: -* counters and _count overlap +* the double-cmpxchg case (never used on 32-bit): +* counters overlaps _count, and we are careful +* to only use 32-bits of 'counters' so that we +* do not interfere with _count. */ union {unsigned long counters2;struct { -atomic_t padding2; +atomic_t _unused_mapcount;atomic_t _count; }; };
@@ -73,7 +75,6 @@ struct page {* updated asynchronously */ union {struct /* the normal uses */ { -pgoff_t index;/* Our offset within mapping. */ /* * mapping: If low bit clear, points to * inode address_space, or NULL. If page
@@ -82,6 +83,7 @@ struct page {* see PAGE_MAPPING_ANON below. */ struct address_space *mapping; +pgoff_t index;/* Our offset within mapping. */ /* * Count of ptes mapped in mms, to show when page * is mapped & limit reverse map searches.
mm/slab_common.c renamed from mm/slab_common.c~fix-slub-size-32-bit +6 -10
@@ -695,15 +695,11 @@ void slab_build_checks(void)SLAB_PAGE_CHECK (_count, dontuse_slob_count);/* -* When doing a double-cmpxchg, the slub code sucks in -* _count. But, this is harmless since if _count is -* modified, the cmpxchg will fail. When not using a -* real cmpxchg, the slub code uses a lock. But, _count -* is not modified under that lock and updates can be -* lost if they race with one of the "faked" cmpxchg -* under that lock. This makes sure that the space we -* carve out for _count in that case actually lines up -* with the real _count. +* The slub code uses page->_mapcount's space for some +* internal counters. But, since ->_count and +* ->_mapcount are 32-bit everywhere and the slub +* counters are an unsigned long which changes size, +* we need to change the checks on 32 vs. 64-bit. */ SLAB_PAGE_CHECK (_count, slub_data.separate_count);
@@ -711,6 +707,6 @@ void slab_build_checks(void)* We need at least three double-words worth of space to * ensure that we can align to a double-wordk internally. */ -BUILD_BUG_ON (sizeof (struct slub_data ) !=sizeof (unsigned long ) *3 ); +BUILD_BUG_ON (sizeof (struct slub_data ) <sizeof (unsigned long ) *3 ); }
_