Fix compound_head() race
Subsystems (10)
PATCH v5 3/7 zsmalloc: use page->private instead of page->first_page
In reply to Kirill A. ShutemovWe are going to rework how compound_head() work. It will not use page->first_page as we have it now.
The only other user of page->first_page beyond compound pages is zsmalloc.
Let's use page->private instead of page->first_page here. It occupies the same storage space.
--- mm/zsmalloc.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
mm/zsmalloc.c +5 -6
@@ -16,7 +16,7 @@* struct page(s) to form a zspage. * * Usage of struct page fields: -* page->first_page: points to the first component (0-order) page +* page->private: points to the first component (0-order) page * page->index (union with page->freelist): offset of the first object * starting in this page. For the first page, this is * always 0, so we use this field (aka freelist) to point
@@ -26,8 +26,7 @@* * For _first_ page only: * -* page->private (union with page->first_page): refers to the -* component page after the first page +* page->private: refers to the component page after the first page * If the page is first_page for huge object, it stores handle. * Look at size_class->huge. * page->freelist: points to the first free object in zspage.
@@ -770,7 +769,7 @@ static struct page *get_first_page(struct page *page)if (is_first_page (page))return page;else -return page->first_page; +return (struct page *)page_private (page); }static struct page *get_next_page (struct page *page)
@@ -955,7 +954,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)* Allocate individual pages and link them together as: * 1. first page->private = first sub-page * 2. all sub-pages are linked together using page->lru -* 3. each sub-page is linked to the first page using page->first_page +* 3. each sub-page is linked to the first page using page->private * * For each size class, First/Head pages are linked together using * page->lru. Also, we set PG_private to identify the first page
@@ -980,7 +979,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)if (i ==1 )set_page_private (first_page, (unsigned long )page);if (i >=1 ) - page->first_page = first_page; +set_page_private (page, (unsigned long )first_page);if (i >=2 )list_add (&page->lru, &prev_page->lru);if (i == class->pages_per_zspage -1 )/* last page */
Signature · 7 lines
2.5.0 -- 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 v5 4/7 mm: pack compound_dtor and compound_order into one word in struct page
In reply to Kirill A. ShutemovThe patch halves space occupied by compound_dtor and compound_order in struct page.
For compound_order, it's trivial long -> short conversion.
For get_compound_page_dtor(), we now use hardcoded table for destructor lookup and store its index in the struct page instead of direct pointer to destructor. It shouldn't be a big trouble to maintain the table: we have only two destructor and NULL currently.
This patch free up one word in tail pages for reuse. This is preparation for the next patch.
--- include/linux/mm.h | 24 +++++++++++++++++++----- include/linux/mm_types.h | 6 ++---- mm/hugetlb.c | 8 ++++---- mm/page_alloc.c | 11 ++++++++++- 4 files changed, 35 insertions(+), 14 deletions(-)
include/linux/mm.h +19 -5
@@ -575,18 +575,32 @@ int split_free_page(struct page *page);/* * Compound pages have a destructor function. Provide a * prototype for that function and accessor functions. -* These are _only_ valid on the head of a PG_compound page. +* These are _only_ valid on the head of a compound page. */ +typedef void compound_page_dtor (struct page *); + +/* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */ +enum compound_dtor_id { +NULL_COMPOUND_DTOR , +COMPOUND_PAGE_DTOR , +#ifdef CONFIG_HUGETLB_PAGE +HUGETLB_PAGE_DTOR , +#endif +NR_COMPOUND_DTORS , +}; +extern compound_page_dtor *const compound_page_dtors[];static inline void set_compound_page_dtor (struct page *page, -compound_page_dtor *dtor) +enum compound_dtor_id compound_dtor) { - page[1 ].compound_dtor = dtor; +VM_BUG_ON_PAGE (compound_dtor >=NR_COMPOUND_DTORS , page); + page[1 ].compound_dtor = compound_dtor; }static inline compound_page_dtor *get_compound_page_dtor (struct page *page) { -return page[1 ].compound_dtor; +VM_BUG_ON_PAGE (page[1 ].compound_dtor >=NR_COMPOUND_DTORS , page); +return compound_page_dtors[page[1 ].compound_dtor]; }static inline int compound_order (struct page *page)
@@ -596,7 +610,7 @@ static inline int compound_order(struct page *page)return page[1 ].compound_order; } -static inline void set_compound_order (struct page *page,unsigned long order) +static inline void set_compound_order (struct page *page,unsigned int order) { page[1 ].compound_order = order; }
include/linux/mm_types.h +2 -4
@@ -28,8 +28,6 @@ struct mem_cgroup; IS_ENABLED(CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK))#define ALLOC_SPLIT_PTLOCKS (SPINLOCK_SIZE > BITS_PER_LONG/8) -typedef void compound_page_dtor (struct page *); -/* * Each physical page in the system has a struct page associated with * it to keep track of whatever it is we are using the page for at the
@@ -145,8 +143,8 @@ struct page {*/ /* First tail page of compound page */ struct { -compound_page_dtor *compound_dtor; -unsigned long compound_order; +unsigned short int compound_dtor; +unsigned short int compound_order; };#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) &&USE_SPLIT_PMD_PTLOCKS
mm/hugetlb.c +4 -4
@@ -969,7 +969,7 @@ static void update_and_free_page(struct hstate *h, struct page *page) 1 << PG_writeback); }VM_BUG_ON_PAGE (hugetlb_cgroup_from_page (page), page); -set_compound_page_dtor (page,NULL ); +set_compound_page_dtor (page,NULL_COMPOUND_DTOR );set_page_refcounted (page);if (hstate_is_gigantic (h)) {destroy_compound_gigantic_page (page,huge_page_order (h));
@@ -1065,7 +1065,7 @@ void free_huge_page(struct page *page)static void prep_new_huge_page (struct hstate *h,struct page *page,int nid) {INIT_LIST_HEAD (&page->lru); -set_compound_page_dtor (page, free_huge_page); +set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );spin_lock (&hugetlb_lock);set_hugetlb_cgroup (page,NULL ); h->nr_huge_pages++;
@@ -1117,7 +1117,7 @@ int PageHuge(struct page *page)return 0 ; page =compound_head (page); -return get_compound_page_dtor (page) == free_huge_page; +return page[1 ].compound_dtor ==HUGETLB_PAGE_DTOR ; }EXPORT_SYMBOL_GPL (PageHuge);
@@ -1314,7 +1314,7 @@ static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)if (page) {INIT_LIST_HEAD (&page->lru); r_nid =page_to_nid (page); -set_compound_page_dtor (page, free_huge_page); +set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );/* * We incremented the global counters already
mm/page_alloc.c +10 -1
@@ -208,6 +208,15 @@ static char * const zone_names[MAX_NR_ZONES] = { "Movable", }; +static void free_compound_page (struct page *page); +compound_page_dtor *const compound_page_dtors[] = { +NULL , + free_compound_page, +#ifdef CONFIG_HUGETLB_PAGE + free_huge_page, +#endif +}; +int min_free_kbytes =1024 ;int user_min_free_kbytes =-1 ;
@@ -437,7 +446,7 @@ void prep_compound_page(struct page *page, unsigned long order)int i;int nr_pages =1 << order; -set_compound_page_dtor (page, free_compound_page); +set_compound_page_dtor (page,COMPOUND_PAGE_DTOR );set_compound_order (page, order);__SetPageHead (page);for (i =1 ; i < nr_pages; i++) {
Signature · 7 lines
2.5.0 -- 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 v5 2/7 slub: use page->rcu_head instead of page->lru plus cast
In reply to Kirill A. ShutemovWe have properly typed page->rcu_head, no need to cast page->lru.
--- mm/slub.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
mm/slub.c +1 -4
@@ -1510,10 +1510,7 @@ static void free_slab(struct kmem_cache *s, struct page *page)VM_BUG_ON (s->reserved !=sizeof (*head)); head =page_address (page) + offset; }else { -/* -* RCU free overloads the RCU head over the LRU -*/ - head = (void *)&page->lru; + head = &page->rcu_head; }call_rcu (head, rcu_free_slab);
Signature · 7 lines
2.5.0 -- 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>
v5 cover Fix compound_head() race
Here's my attempt on fixing race in compound_head(). It should make compound_head() reliable in all contexts.
The last patch is optional.
It applies cleanly into mmotm patchstack just before my page-flags patchset.
As expected, it causes few conflicts with patches:
page-flags-introduce-page-flags-policies-wrt-compound-pages.patch mm-sanitize-page-mapping-for-tail-pages.patch include-linux-page-flagsh-rename-macros-to-avoid-collisions.patch
Updated patches with solved conflicts can be found here:
http://marc.info/?l=linux-kernel&m=144007388303804&q=p4
http://marc.info/?l=linux-kernel&m=144007388303804&q=p5
http://marc.info/?l=linux-kernel&m=144007388303804&q=p3
v5:
- Fix collision with hugetlb_cgroup (see updated patch description in
patch 5/7);
v4:
- init page->lru on init_reserved_page() for
DEFERRED_STRUCT_PAGE_INIT=n;
- fix zsmalloc breakage (repored by Sergey Senozhatsky);
- move #ifdef CONFIG_64BIT into separate patch;
- enum compound_dtor_id;
- move pmd_huge_pte to other word to avoid conflict with compound_head;
- compile-time LIST_POISON1 sanity check;
- few cleanups around page->rcu_head;
v3:
- Fix build without hugetlb;
- Drop page->first_page;
- Update comment for free_compound_page();
- Use 'unsigned int' for page order;
v2: Per Hugh's suggestion page->compound_head is moved into third double
word. This way we can avoid memory overhead which v1 had in some cases.
This place in struct page is rather overloaded. More testing is required to make sure we don't collide with anyone.
Kirill A. Shutemov (7):
mm: drop page->slab_page
slub: use page->rcu_head instead of page->lru plus cast
zsmalloc: use page->private instead of page->first_page
mm: pack compound_dtor and compound_order into one word in struct page
mm: make compound_head() robust
mm: use 'unsigned int' for page order
mm: use 'unsigned int' for compound_dtor/compound_order on 64BIT
Documentation/vm/split_page_table_lock | 4 +- arch/xtensa/configs/iss_defconfig | 1 - include/linux/hugetlb_cgroup.h | 4 +- include/linux/mm.h | 82 ++++++++++----------------------- include/linux/mm_types.h | 30 ++++++++---- include/linux/page-flags.h | 80 ++++++++------------------------ mm/Kconfig | 12 ----- mm/debug.c | 5 -- mm/huge_memory.c | 3 +- mm/hugetlb.c | 35 +++++++------- mm/hugetlb_cgroup.c | 2 +- mm/internal.h | 8 ++-- mm/memory-failure.c | 7 --- mm/page_alloc.c | 84 ++++++++++++++++++++++------------ mm/slab.c | 17 ++----- mm/slub.c | 5 +- mm/swap.c | 4 +- mm/zsmalloc.c | 11 ++--- 18 files changed, 156 insertions(+), 238 deletions(-)
Signature · 7 lines
2.5.0 -- 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 v5 1/7 mm: drop page->slab_page
Since 8456a648cf44 ("slab: use struct page for slab management") nobody uses slab_page field in struct page.
Let's drop it.
--- include/linux/mm_types.h | 1 - mm/slab.c | 17 +++-------------- 2 files changed, 3 insertions(+), 15 deletions(-)
include/linux/mm_types.h +0 -1
@@ -140,7 +140,6 @@ struct page {#endif }; -struct slab *slab_page;/* slab fields */ struct rcu_head rcu_head;/* Used by SLAB * when destroying via RCU */
mm/slab.c +3 -14
@@ -1888,21 +1888,10 @@ static void slab_destroy(struct kmem_cache *cachep, struct page *page) freelist = page->freelist;slab_destroy_debugcheck (cachep, page); -if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) { -struct rcu_head *head; - -/* -* RCU free overloads the RCU head over the LRU. -* slab_page has been overloeaded over the LRU, -* however it is not used from now on so that -* we can use it safely. -*/ - head = (void *)&page->rcu_head; -call_rcu (head, kmem_rcu_free); - - }else { +if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) +call_rcu (&page->rcu_head, kmem_rcu_free); +else kmem_freepages (cachep, page); - }/* * From now on, we don't use freelist
Signature · 7 lines
2.5.0 -- 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 v5 7/7 mm: use 'unsigned int' for compound_dtor/compound_order on 64BIT
In reply to Kirill A. ShutemovOn 64 bit system we have enough space in struct page to encode compound_dtor and compound_order with unsigned int.
On x86-64 it leads to slightly smaller code size due usesage of plain MOV instead of MOVZX (zero-extended move) or similar effect.
allyesconfig:
text data bss dec hex filename
159520446 48146736 72196096 279863278 10ae5fee vmlinux.pre
159520382 48146736 72196096 279863214 10ae5fae vmlinux.post
On other architectures without native support of 16-bit data types the difference can be bigger.
--- include/linux/mm_types.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/mm_types.h +5 -0
@@ -150,8 +150,13 @@ struct page {/* First tail page of compound page */ struct {unsigned long compound_head;/* If bit zero is set */ +#ifdef CONFIG_64BIT +unsigned int compound_dtor; +unsigned int compound_order; +#else unsigned short int compound_dtor;unsigned short int compound_order; +#endif };#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) &&USE_SPLIT_PMD_PTLOCKS
Signature · 7 lines
2.5.0 -- 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 v5 6/7 mm: use 'unsigned int' for page order
In reply to Kirill A. ShutemovLet's try to be consistent about data type of page order.
--- include/linux/mm.h | 5 +++-- mm/hugetlb.c | 19 ++++++++++--------- mm/internal.h | 4 ++-- mm/page_alloc.c | 27 +++++++++++++++------------ 4 files changed, 30 insertions(+), 25 deletions(-)
include/linux/mm.h +3 -2
@@ -557,7 +557,7 @@ static inline compound_page_dtor *get_compound_page_dtor(struct page *page)return compound_page_dtors[page[1 ].compound_dtor]; } -static inline int compound_order (struct page *page) +static inline unsigned int compound_order (struct page *page) {if (!PageHead (page))return 0 ;
@@ -1718,7 +1718,8 @@ extern void si_meminfo(struct sysinfo * val);extern void si_meminfo_node (struct sysinfo *val,int nid); extern __printf(3, 4) -void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...); +void warn_alloc_failed(gfp_t gfp_mask, unsigned int order, + const char *fmt, ...); extern void setup_per_cpu_pageset(void);
mm/hugetlb.c +10 -9
@@ -817,7 +817,7 @@ static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)#if defined(CONFIG_CMA ) && defined(CONFIG_X86_64 )static void destroy_compound_gigantic_page (struct page *page, -unsigned long order) +unsigned int order) {int i;int nr_pages =1 << order;
@@ -832,7 +832,7 @@ static void destroy_compound_gigantic_page(struct page *page,__ClearPageHead (page); } -static void free_gigantic_page (struct page *page,unsigned order) +static void free_gigantic_page (struct page *page,unsigned int order) {free_contig_range (page_to_pfn (page),1 << order); }
@@ -876,7 +876,7 @@ static bool zone_spans_last_pfn(const struct zone *zone,return zone_spans_pfn (zone, last_pfn); } -static struct page *alloc_gigantic_page (int nid,unsigned order) +static struct page *alloc_gigantic_page (int nid,unsigned int order) {unsigned long nr_pages =1 << order;unsigned long ret, pfn, flags;
@@ -912,7 +912,7 @@ static struct page *alloc_gigantic_page(int nid, unsigned order) }static void prep_new_huge_page (struct hstate *h,struct page *page,int nid); -static void prep_compound_gigantic_page (struct page *page,unsigned long order); +static void prep_compound_gigantic_page (struct page *page,unsigned int order);static struct page *alloc_fresh_gigantic_page_node (struct hstate *h,int nid) {
@@ -945,9 +945,9 @@ static int alloc_fresh_gigantic_page(struct hstate *h,static inline bool gigantic_page_supported (void ) {return true; }#else static inline bool gigantic_page_supported (void ) {return false; } -static inline void free_gigantic_page (struct page *page,unsigned order) { } +static inline void free_gigantic_page (struct page *page,unsigned int order) { }static inline void destroy_compound_gigantic_page (struct page *page, -unsigned long order) { } +unsigned int order) { }static inline int alloc_fresh_gigantic_page (struct hstate *h,nodemask_t *nodes_allowed) {return 0 ; }#endif
@@ -1073,7 +1073,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)put_page (page);/* free it into the hugepage allocator */ } -static void prep_compound_gigantic_page (struct page *page,unsigned long order) +static void prep_compound_gigantic_page (struct page *page,unsigned int order) {int i;int nr_pages =1 << order;
@@ -1640,7 +1640,8 @@ found:return 1 ; } -static void __init prep_compound_huge_page (struct page *page,int order) +static void __init prep_compound_huge_page (struct page *page, +unsigned int order) {if (unlikely (order > (MAX_ORDER -1 )))prep_compound_gigantic_page (page, order);
@@ -2351,7 +2352,7 @@ static int __init hugetlb_init(void)module_init (hugetlb_init);/* Should be called on processing a hugepagesz=... option */ -void __init hugetlb_add_hstate (unsigned order) +void __init hugetlb_add_hstate (unsigned int order) {struct hstate *h;unsigned long i;
mm/internal.h +2 -2
@@ -157,7 +157,7 @@ __find_buddy_index(unsigned long page_idx, unsigned int order)extern int __isolate_free_page (struct page *page,unsigned int order);extern void __free_pages_bootmem (struct page *page,unsigned long pfn,unsigned int order); -extern void prep_compound_page (struct page *page,unsigned long order); +extern void prep_compound_page (struct page *page,unsigned int order);#ifdef CONFIG_MEMORY_FAILURE extern bool is_free_buddy_page (struct page *page);#endif
@@ -214,7 +214,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,* page cannot be allocated or merged in parallel. Alternatively, it must * handle invalid values gracefully, and use page_order_unsafe() below. */ -static inline unsigned long page_order (struct page *page) +static inline unsigned int page_order (struct page *page) {/* PageBuddy() must be checked by the caller */ return page_private (page);
mm/page_alloc.c +15 -12
@@ -163,7 +163,7 @@ bool pm_suspended_storage(void)#endif /* CONFIG_PM_SLEEP */ #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE -int pageblock_order__read_mostly ; +unsigned int pageblock_order__read_mostly ;#endif static void __free_pages_ok (struct page *page,unsigned int order);
@@ -441,7 +441,7 @@ static void free_compound_page(struct page *page)__free_pages_ok (page,compound_order (page)); } -void prep_compound_page (struct page *page,unsigned long order) +void prep_compound_page (struct page *page,unsigned int order) {int i;int nr_pages =1 << order;
@@ -641,7 +641,7 @@ static inline void __free_one_page(struct page *page,unsigned long combined_idx;unsigned long uninitialized_var (buddy_idx );struct page *buddy; -int max_order =MAX_ORDER ; +unsigned int max_order =MAX_ORDER ;VM_BUG_ON (!zone_is_initialized (zone));VM_BUG_ON_PAGE (page->flags &PAGE_FLAGS_CHECK_AT_PREP , page);
@@ -1444,7 +1444,7 @@ int move_freepages(struct zone *zone, int migratetype) {struct page *page; -unsigned long order; +unsigned int order;int pages_moved =0 ;#ifndef CONFIG_HOLES_IN_ZONE
@@ -1558,7 +1558,7 @@ static bool can_steal_fallback(unsigned int order, int start_mt)static void steal_suitable_fallback (struct zone *zone,struct page *page,int start_type) { -int current_order =page_order (page); +unsigned int current_order =page_order (page);int pages;/* Take ownership for orders >= pageblock_order */
@@ -2665,7 +2665,7 @@ static DEFINE_RATELIMIT_STATE(nopage_rs,DEFAULT_RATELIMIT_INTERVAL , DEFAULT_RATELIMIT_BURST); -void warn_alloc_failed (gfp_t gfp_mask,int order,const char *fmt, ...) +void warn_alloc_failed (gfp_t gfp_mask,unsigned int order,const char *fmt, ...) {unsigned int filter =SHOW_MEM_FILTER_NODES ;
@@ -2699,7 +2699,7 @@ void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)va_end (args); } -pr_warn ("%s: page allocation failure: order:%d, mode:0x%x\n" , +pr_warn ("%s: page allocation failure: order:%u, mode:0x%x\n" , current->comm, order, gfp_mask);dump_stack ();
@@ -3458,7 +3458,8 @@ void free_kmem_pages(unsigned long addr, unsigned int order) } } -static void *make_alloc_exact (unsigned long addr,unsigned order,size_t size) +static void *make_alloc_exact (unsigned long addr,unsigned int order, +size_t size) {if (addr) {unsigned long alloc_end = addr + (PAGE_SIZE << order);
@@ -3510,7 +3511,7 @@ EXPORT_SYMBOL(alloc_pages_exact);*/ void *__meminit alloc_pages_exact_nid (int nid,size_t size,gfp_t gfp_mask) { -unsigned order =get_order (size); +unsigned int order =get_order (size);struct page *p =alloc_pages_node (nid, gfp_mask, order);if (!p)return NULL ;
@@ -3812,7 +3813,8 @@ void show_free_areas(unsigned int filter) }for_each_populated_zone (zone) { -unsigned long nr[MAX_ORDER ], flags, order, total =0 ; +unsigned int order; +unsigned long nr[MAX_ORDER ], flags, total =0 ;unsigned char types[MAX_ORDER ];if (skip_free_areas_node (filter,zone_to_nid (zone)))
@@ -4161,7 +4163,7 @@ static void build_zonelists(pg_data_t *pgdat)nodemask_t used_mask;int local_node, prev_node;struct zonelist *zonelist; -int order = current_zonelist_order; +unsigned int order = current_zonelist_order;/* initialize zonelists */ for (i =0 ; i <MAX_ZONELISTS ; i++) {
@@ -6826,7 +6828,8 @@ int alloc_contig_range(unsigned long start, unsigned long end, unsigned migratetype) {unsigned long outer_start, outer_end; -int ret =0 , order; +unsigned int order; +int ret =0 ;struct compact_control cc = { .nr_migratepages =0 ,
Signature · 7 lines
2.5.0 -- 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 v5 5/7 mm: make compound_head() robust
In reply to Kirill A. ShutemovHugh has pointed that compound_head() call can be unsafe in some context. There's one example:
CPU0 CPU1
isolate_migratepages_block()
page_count()
compound_head()
!!PageTail() == true
put_page()
tail->first_page = NULL
head = tail->first_page
alloc_pages(__GFP_COMP)
prep_compound_page()
tail->first_page = head
__SetPageTail(p);
!!PageTail() == true
<head == NULL dereferencing>The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.
We can fix the race by changing how encode PageTail() and compound_head() within struct page to be able to update them in one shot.
The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. Bit 0 encodes PageTail() and the rest bits are pointer to head page if bit zero is set.
The patch moves page->pmd_huge_pte out of word, just in case if an architecture defines pgtable_t into something what can have the bit 0 set.
hugetlb_cgroup uses page->lru.next in the second tail page to store pointer struct hugetlb_cgroup. The patch switch it to use page->private in the second tail page instead. The space is free since ->first_page is removed from the union.
The patch also opens possibility to remove HUGETLB_CGROUP_MIN_ORDER limitation, since there's now space in first tail page to store struct hugetlb_cgroup pointer. But that's out of scope of the patch.
That means page->compound_head shares storage space with:
- page->lru.next;
- page->next;
- page->rcu_head.next;
That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word.
--- Documentation/vm/split_page_table_lock | 4 +- arch/xtensa/configs/iss_defconfig | 1 - include/linux/hugetlb_cgroup.h | 4 +- include/linux/mm.h | 53 ++-------------------- include/linux/mm_types.h | 18 ++++++-- include/linux/page-flags.h | 80 ++++++++-------------------------- mm/Kconfig | 12 ----- mm/debug.c | 5 --- mm/huge_memory.c | 3 +- mm/hugetlb.c | 8 +--- mm/hugetlb_cgroup.c | 2 +- mm/internal.h | 4 +- mm/memory-failure.c | 7 --- mm/page_alloc.c | 46 +++++++++++-------- mm/swap.c | 4 +- 15 files changed, 77 insertions(+), 174 deletions(-)
Documentation/vm/split_page_table_lock +2 -2
@@ -54,8 +54,8 @@ everything required is done by pgtable_page_ctor() and pgtable_page_dtor(), which must be called on PTE table allocation / freeing. Make sure the architecture doesn't use slab allocator for page table -allocation: slab uses page->slab_cache and page->first_page for its pages. -These fields share storage with page->ptl. +allocation: slab uses page->slab_cache for its pages. +This field shares storage with page->ptl. PMD split lock only makes sense if you have more than two page table levels.
arch/xtensa/configs/iss_defconfig +0 -1
@@ -169,7 +169,6 @@ CONFIG_FLATMEM_MANUAL=y # CONFIG_SPARSEMEM_MANUAL is not set CONFIG_FLATMEM=y CONFIG_FLAT_NODE_MEM_MAP=y -CONFIG_PAGEFLAGS_EXTENDED=y CONFIG_SPLIT_PTLOCK_CPUS=4 # CONFIG_PHYS_ADDR_T_64BIT is not set CONFIG_ZONE_DMA_FLAG=1
include/linux/hugetlb_cgroup.h +2 -2
@@ -32,7 +32,7 @@ static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return NULL ; -return (struct hugetlb_cgroup *)page[2 ].lru.next; +return (struct hugetlb_cgroup *)page[2 ].private; } static inline
@@ -42,7 +42,7 @@ int set_hugetlb_cgroup(struct page *page, struct hugetlb_cgroup *h_cg)if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return -1 ; - page[2 ].lru.next = (void *)h_cg; + page[2 ].private = (unsigned long )h_cg;return 0 ; }
include/linux/mm.h +3 -50
77 lines not on this page · raw message
include/linux/mm_types.h +15 -3
@@ -120,7 +120,13 @@ struct page { }; }; -/* Third double word block */ +/* +* 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(). +*/ union {struct list_head lru;/* Pageout list, eg. active_list * protected by zone->lru_lock !
@@ -143,12 +149,19 @@ struct page {*/ /* First tail page of compound page */ struct { +unsigned long compound_head;/* If bit zero is set */ unsigned short int compound_dtor;unsigned short int compound_order; };#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) &&USE_SPLIT_PMD_PTLOCKS -pgtable_t pmd_huge_pte;/* protected by page->ptl */ +struct { +unsigned long __pad;/* do not overlay pmd_huge_pte +* with compound_head to avoid +* possible bit 0 collision. +*/ +pgtable_t pmd_huge_pte;/* protected by page->ptl */ + };#endif };
@@ -169,7 +182,6 @@ struct page {#endif #endif struct kmem_cache *slab_cache;/* SL[AU]B: Pointer to slab */ -struct page *first_page;/* Compound tail pages */ };#ifdef CONFIG_MEMCG
include/linux/page-flags.h +18 -62
115 lines not on this page · raw message
mm/Kconfig +0 -12
@@ -200,18 +200,6 @@ config MEMORY_HOTREMOVE depends on MEMORY_HOTPLUG && ARCH_ENABLE_MEMORY_HOTREMOVE depends on MIGRATION -# -# If we have space for more page flags then we can enable additional -# optimizations and functionality. -# -# Regular Sparsemem takes page flag bits for the sectionid if it does not -# use a virtual memmap. Disable extended page flags for 32 bit platforms -# that require the use of a sectionid in the page flags. -# -config PAGEFLAGS_EXTENDED - def_bool y - depends on 64BIT || SPARSEMEM_VMEMMAP || !SPARSEMEM - # Heavily threaded applications may benefit from splitting the mm-wide # page_table_lock, so that faults on different parts of the user address # space can be handled with less contention: split it at this NR_CPUS.
mm/debug.c +0 -5
@@ -25,12 +25,7 @@ static const struct trace_print_flags pageflag_names[] = { {1UL << PG_private, "private" }, {1UL << PG_private_2, "private_2" }, {1UL << PG_writeback, "writeback" }, -#ifdef CONFIG_PAGEFLAGS_EXTENDED {1UL << PG_head, "head" }, - {1UL << PG_tail, "tail" }, -#else - {1UL << PG_compound, "compound" }, -#endif {1UL << PG_swapcache, "swapcache" }, {1UL << PG_mappedtodisk, "mappedtodisk" }, {1UL << PG_reclaim, "reclaim" },
mm/huge_memory.c +1 -2
@@ -1686,8 +1686,7 @@ static void __split_huge_page_refcount(struct page *page, (1L << PG_unevictable))); page_tail->flags |= (1L << PG_dirty); -/* clear PageTail before overwriting first_page */ -smp_wmb (); +clear_compound_head (page_tail);/* * __split_huge_page_splitting() already set the
mm/hugetlb.c +2 -6
@@ -824,9 +824,8 @@ static void destroy_compound_gigantic_page(struct page *page,struct page *p = page +1 ;for (i =1 ; i < nr_pages; i++, p =mem_map_next (p, page, i)) { -__ClearPageTail (p); +clear_compound_head (p);set_page_refcounted (p); - p->first_page =NULL ; }set_compound_order (page,0 );
@@ -1099,10 +1098,7 @@ static void prep_compound_gigantic_page(struct page *page, unsigned long order)*/ __ClearPageReserved (p);set_page_count (p,0 ); - p->first_page = page; -/* Make sure p->first_page is always valid for PageTail() */ -smp_wmb (); -__SetPageTail (p); +set_compound_head (p, page); } }
mm/hugetlb_cgroup.c +1 -1
@@ -384,7 +384,7 @@ void __init hugetlb_cgroup_file_init(void)/* * Add cgroup control files only if the huge page consists * of more than two normal pages. This is because we use -* page[2].lru.next for storing cgroup details. +* page[2].private for storing cgroup details. */ if (huge_page_order (h) >=HUGETLB_CGROUP_MIN_ORDER )__hugetlb_cgroup_file_init (hstate_index (h));
mm/internal.h +2 -2
@@ -61,9 +61,9 @@ static inline void __get_page_tail_foll(struct page *page,* speculative page access (like in * page_cache_get_speculative()) on tail pages. */ -VM_BUG_ON_PAGE (atomic_read (&page->first_page->_count) <=0 , page); +VM_BUG_ON_PAGE (atomic_read (&compound_head (page)->_count) <=0 , page);if (get_page_head) -atomic_inc (&page->first_page->_count); +atomic_inc (&compound_head (page)->_count);get_huge_page_tail (page); }
mm/memory-failure.c +0 -7
@@ -787,8 +787,6 @@ static int me_huge_page(struct page *p, unsigned long pfn)#define lru (1UL << PG_lru)#define swapbacked (1UL << PG_swapbacked)#define head (1UL << PG_head) -#define tail (1UL << PG_tail) -#define compound (1UL << PG_compound)#define slab (1UL << PG_slab)#define reserved (1UL << PG_reserved)
@@ -811,12 +809,7 @@ static struct page_state {*/ { slab, slab, MF_MSG_SLAB, me_kernel }, -#ifdef CONFIG_PAGEFLAGS_EXTENDED { head, head, MF_MSG_HUGE, me_huge_page }, - { tail, tail, MF_MSG_HUGE, me_huge_page }, -#else - { compound, compound, MF_MSG_HUGE, me_huge_page }, -#endif { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty }, { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
mm/page_alloc.c +29 -17
77 lines not on this page · raw message
mm/swap.c +2 -2
@@ -200,7 +200,7 @@ out_put_single:__put_single_page (page);return ; } -VM_BUG_ON_PAGE (page_head != page->first_page, page); +VM_BUG_ON_PAGE (page_head !=compound_head (page), page);/* * We can release the refcount taken by * get_page_unless_zero() now that
@@ -261,7 +261,7 @@ static void put_compound_page(struct page *page)* Case 3 is possible, as we may race with * __split_huge_page_refcount tearing down a THP page. */ - page_head =compound_head_by_tail (page); + page_head =compound_head (page);if (!__compound_tail_refcounted (page_head))put_unrefcounted_compound_page (page_head, page);else
Signature · 7 lines
2.5.0 -- 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>
Quoted 8 lines · Kirill A. Shutemov
Kirill A. Shutemov (7):
mm: droppage->slab_page
slub: usepage->rcu_headinstead ofpage->lruplus cast
zsmalloc: usepage->privateinstead ofpage->first_page
mm: packcompound_dtorandcompound_orderinto one word in struct page
mm: makecompound_head()robust
mm: use 'unsigned int' for page order
mm: use 'unsigned int' for compound_dtor/compound_order on 64BIT
The only other alternative solution that doesn't require finding a bit zero at the LSB in a field unused in tail pages, is to drop both PG_head and PG_tail, and reserve 4 bits from page->flags.
This means a net loss of 2 bits from page->flags (and loss of 3 bits if !CONFIG_PAGEFLAGS_EXTENDED), but then everything becomes simple and there's no need of finding a LSB field that is guaranteed zero at all times.
With those 4 bits, you clear them for not compound pages. When you create a compound page you encode the compound_order in those 4 bits of page->flags, equal for for all head and tail
pages. compound_order() then becomes atomically available for tail pages too and compound_order goes away from struct page along with first_page (and there's no need to add a compound_head).
In PageCompound you read the 4 bits, if they're not all zero it's compound, otherwise it's not.
In PageHead/Tail, if the 4 bits are all zero it's not head/tail, otherwise you do the math on the page_to_pfn(page). If the pfn is naturally aligned against the order encoded in the 4 bits "!(pfn & (1<<order)-1)" it's a head, otherwise it's a tail.
If it's a tail, for the compound_head then it's just a matter of doing "return page - (pfn & ((1<<order)-1)" (no need of pfn_to_page).
This leverages the physical natural alignment of compound pages for all orders. It'd cover up to CONFIG_FORCE_MAX_ZONEORDER == 16 (128MBytes of order 15 with PAGE_SIZE 4kb).
page_to_pfn can actually be replaced with (&NODE_DATA(page_to_nid(page))->node_mem_map-page) which is faster as page_to_nid only need to accesses page->flags which is already in L1. So then it costs only one cacheline access in the pgdat and a sub.
Because of the two (or three) additional bits taken out of page->flags I doubt it's viable on 32bit, but I thought I'd mention it just in case.
Thanks,
Andrea
--
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: [PATCHv5 1/7] mm: drop page->slab_page
In reply to Kirill A. ShutemovQuoted 47 lines · Kirill A. Shutemov
Since
8456a648cf44("slab: use struct page for slab management") nobody usesslab_pagefield in struct page.Let's drop it.
Signed-off-by Kirill A. Shutemov <[email protected]>Acked-by David Rientjes <[email protected]>Acked-by Vlastimil Babka <[email protected]>Cc Joonsoo Kim <[email protected]>Cc Andi Kleen <[email protected]>--- include/linux/mm_types.h | 1 - mm/slab.c | 17 +++-------------- 2 files changed, 3 insertions(+), 15 deletions(-)include/linux/mm_types.h +0
-1@@ -140,7 +140,6 @@ struct page {#endif }; -struct slab *slab_page;/* slab fields */ struct rcu_head rcu_head;/* Used by SLAB * when destroying via RCU */ mm/slab.c +0
-11@@ -1888,21 +1888,10 @@ static void slab_destroy(struct kmem_cache *cachep, struct page *page) freelist = page->freelist;slab_destroy_debugcheck (cachep, page); -if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) { -struct rcu_head *head; - -/* -* RCU free overloads the RCU head over the LRU. -* slab_page has been overloeaded over the LRU, -* however it is not used from now on so that -* we can use it safely. -*/ - head = (void *)&page->rcu_head; -call_rcu (head, kmem_rcu_free);
mm/slab.c static void slab_destroy(struct kmem_cache *cachep, struct page *page) · line 1897
Quoted 10 lines
mm/slab.c +3
-3- - }else { +if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) +call_rcu (&page->rcu_head, kmem_rcu_free); +else kmem_freepages (cachep, page); - }/* * From now on, we don't use freelist
This second piece looks like it belongs in patch 2, not patch 1 based on the descriptions.
- Alex
--
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: [PATCHv5 1/7] mm: drop page->slab_page
Quoted 50 lines · Alexander Duyck
Quoted 49 lines · Kirill A. Shutemov
Since
8456a648cf44("slab: use struct page for slab management") nobody usesslab_pagefield in struct page.Let's drop it.
Signed-off-by Kirill A. Shutemov <[email protected]>Acked-by David Rientjes <[email protected]>Acked-by Vlastimil Babka <[email protected]>Cc Joonsoo Kim <[email protected]>Cc Andi Kleen <[email protected]>--- include/linux/mm_types.h | 1 - mm/slab.c | 17 +++-------------- 2 files changed, 3 insertions(+), 15 deletions(-)include/linux/mm_types.h +0
-1@@ -140,7 +140,6 @@ struct page {#endif }; -struct slab *slab_page;/* slab fields */ struct rcu_head rcu_head;/* Used by SLAB * when destroying via RCU */ mm/slab.c +2
-13@@ -1888,21 +1888,10 @@ static void slab_destroy(struct kmem_cache *cachep, struct page *page) freelist = page->freelist;slab_destroy_debugcheck (cachep, page); -if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) { -struct rcu_head *head; - -/* -* RCU free overloads the RCU head over the LRU. -* slab_page has been overloeaded over the LRU, -* however it is not used from now on so that -* we can use it safely. -*/ - head = (void *)&page->rcu_head; -call_rcu (head, kmem_rcu_free); - - }else { +if (unlikely (cachep->flags &SLAB_DESTROY_BY_RCU )) +call_rcu (&page->rcu_head, kmem_rcu_free);
Quoted 8 lines
mm/slab.c static void slab_destroy(struct kmem_cache *cachep, struct page *page) · line 1897
Quoted 5 lines
mm/slab.c +1
-1+else kmem_freepages (cachep, page); - }/* * From now on, we don't use freelist This second piece looks like it belongs in patch 2, not patch 1 based on the descriptions.
You're right.
Although I don't think I would re-spin the patchset just for this change. If any other change would be required, I'll fix this too.
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: [PATCHv5 4/7] mm: pack compound_dtor and compound_order into one word in struct page
In reply to Kirill A. ShutemovQuoted 10 lines · Kirill A. Shutemov
The patch halves space occupied by
compound_dtorandcompound_orderin struct page.For
compound_order, it's trivial long -> short conversion.For
get_compound_page_dtor(), we now use hardcoded table for destructor lookup and store its index in the struct page instead of direct pointer to destructor. It shouldn't be a big trouble to maintain the table: we have only two destructor and NULL currently.
Quoted 5 lines
This patch free up one word in tail pages for reuse. This is preparation for the next patch.
Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Michal Hocko <[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>
Re: [PATCHv5 5/7] mm: make compound_head() robust
In reply to Kirill A. ShutemovQuoted 42 lines · Kirill A. Shutemov
Hugh has pointed that
compound_head()call can be unsafe in some context. There's one example:CPU0 CPU1
isolate_migratepages_block()
page_count()compound_head() !!PageTail() == true put_page() tail->first_page = NULL head = tail->first_page alloc_pages(__GFP_COMP) prep_compound_page() tail->first_page = head __SetPageTail(p); !!PageTail() == true <head == NULL dereferencing>The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.
We can fix the race by changing how encode
PageTail()andcompound_head()within struct page to be able to update them in one shot.The patch introduces
page->compound_headinto third double word block in front ofcompound_dtorandcompound_order. Bit 0 encodesPageTail()and the rest bits are pointer to head page if bit zero is set.The patch moves
page->pmd_huge_pteout of word, just in case if an architecture definespgtable_tinto something what can have the bit 0 set.
hugetlb_cgroupusespage->lru.nextin the second tail page to store pointer structhugetlb_cgroup. The patch switch it to usepage->privatein the second tail page instead. The space is free since ->first_page is removed from the union.The patch also opens possibility to remove
HUGETLB_CGROUP_MIN_ORDERlimitation, since there's now space in first tail page to store structhugetlb_cgrouppointer. But that's out of scope of the patch.
Quoted 8 lines
That means
page->compound_headshares storage space with:-
page->lru.next;
-page->next;
-page->rcu_head.next;That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word.
Given the discussion about rcu_head, that should warrant some summary here :)
Quoted 18 lines
include/linux/mm_types.h +7
-1@@ -120,7 +120,13 @@ struct page { }; }; -/* Third double word block */ +/* +* 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(). +*/ union {struct list_head lru;/* Pageout list, eg. active_list * protected by zone->lru_lock !
include/linux/mm_types.h struct page { · line 150
Quoted 2 lines
include/linux/mm_types.h +0
-0@@ -143,12 +149,19 @@ struct page {*/ /* First tail page of compound page */
"First tail" doesn't apply for compound_head.
mm/huge_memory.c static void __split_huge_page_refcount(struct page *page, · line 1689
Quoted 10 lines
index
097c7a4bfbd9..330377f83ac7100644mm/huge_memory.c +1
-2@@ -1686,8 +1686,7 @@ static void __split_huge_page_refcount(struct page *page, (1L << PG_unevictable))); page_tail->flags |= (1L << PG_dirty); -/* clear PageTail before overwriting first_page */ -smp_wmb (); +clear_compound_head (page_tail);
I would sleep better if this was done before setting all the page->flags above, previously, PageTail was cleared by the first operation which is "page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;"
I do realize that it doesn't use WRITE_ONCE, so it might have been theoretically broken already, if it does matter.
mm/internal.h static inline void __get_page_tail_foll(struct page *page, · line 66
Quoted 13 lines
mm/internal.h +2
-2@@ -61,9 +61,9 @@ static inline void __get_page_tail_foll(struct page *page,* speculative page access (like in * page_cache_get_speculative()) on tail pages. */ -VM_BUG_ON_PAGE (atomic_read (&page->first_page->_count) <=0 , page); +VM_BUG_ON_PAGE (atomic_read (&compound_head (page)->_count) <=0 , page);if (get_page_head) -atomic_inc (&page->first_page->_count); +atomic_inc (&compound_head (page)->_count);
Doing another compound_head() seems like overkill when this code already assumes PageTail. All callers do it after if (PageTail()) which means they already did READ_ONCE(page->compound_head) and here they do another one. Potentially with different result in bit 0, which would be a subtle bug, that could be interesting to catch with some VM_BUG_ON. I don't know if a direct plain page->compound_head access here instead of compound_head() would also result in a re-read, since the previous access did use READ_ONCE(). Maybe it would be best to reorganize the code here and in the 3 call sites so that the READ_ONCE() used to determine PageTail also obtains the compound head pointer.
Some of that is probably made moot by your other series, but better let's think of this series as standalone first.
Quoted 86 lines
mm/internal.h +0
-0get_huge_page_tail (page); }mm/memory-failure.c +0
-7@@ -787,8 +787,6 @@ static int me_huge_page(struct page *p, unsigned long pfn)#define lru (1UL << PG_lru)#define swapbacked (1UL << PG_swapbacked)#define head (1UL << PG_head) -#define tail (1UL << PG_tail) -#define compound (1UL << PG_compound)#define slab (1UL << PG_slab)#define reserved (1UL << PG_reserved)@@ -811,12 +809,7 @@ static struct page_state {*/ { slab, slab, MF_MSG_SLAB, me_kernel }, -#ifdef CONFIG_PAGEFLAGS_EXTENDED { head, head, MF_MSG_HUGE, me_huge_page }, - { tail, tail, MF_MSG_HUGE, me_huge_page }, -#else - { compound, compound, MF_MSG_HUGE, me_huge_page }, -#endif { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty }, { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },mm/page_alloc.c +19
-12@@ -424,15 +424,15 @@ out:/* * Higher-order pages are called "compound pages". They are structured thusly: * -* The first PAGE_SIZE page is called the "head page". +* The first PAGE_SIZE page is called the "head page" and have PG_head set. * -* The remaining PAGE_SIZE pages are called "tail pages". +* The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded +* in bit 0 of page->compound_head. The rest of bits is pointer to head page. * -* All pages have PG_compound set. All tail pages have their ->first_page -* pointing at the head page. +* The first tail page's ->compound_dtor holds the offset in array of compound +* page destructors. See compound_page_dtors. * -* The first tail page's ->lru.next holds the address of the compound page's -* put_page() function. Its ->lru.prev holds the order of allocation. +* The first tail page's ->compound_order holds the order of allocation. * This usage means that zero-order pages may not be compound. */ @@ -452,10 +452,7 @@ void prep_compound_page(struct page *page, unsigned long order)for (i =1 ; i < nr_pages; i++) {struct page *p = page + i;set_page_count (p,0 ); - p->first_page = page; -/* Make sure p->first_page is always valid for PageTail() */ -smp_wmb (); -__SetPageTail (p); +set_compound_head (p, page); } }@@ -830,17 +827,30 @@ static void free_one_page(struct zone *zone,static int free_tail_pages_check (struct page *head_page,struct page *page) { -if (!IS_ENABLED (CONFIG_DEBUG_VM )) -return 0 ; +int ret =1 ; + +/* +* We rely page->lru.next never has bit 0 set, unless the page +* is PageTail(). Let's make sure that's true even for poisoned ->lru. +*/ +BUILD_BUG_ON ((unsigned long )LIST_POISON1 &1 ); + +if (!IS_ENABLED (CONFIG_DEBUG_VM )) { + ret =0 ; + goto out; + }if (unlikely (!PageTail (page))) {
mm/page_alloc.c static void free_one_page(struct zone *zone, · line 848
Quoted 10 lines
mm/page_alloc.c +4
-4bad_page (page,"PageTail not set" ,0 ); -return 1 ; + goto out; } -if (unlikely (page->first_page != head_page)) { -bad_page (page,"first_page not consistent" ,0 ); -return 1 ; +if (unlikely (compound_head (page) != head_page)) { +bad_page (page,"compound_head not consistent" ,0 ); + goto out;
Same here, although for a DEBUG_VM config only it's not as important.
Quoted 32 lines
mm/page_alloc.c +6
-1} -return 0 ; + ret =0 ; +out: +clear_compound_head (page); +return ret; } static void __meminit __init_single_page(struct page *page, unsigned long pfn,@@ -888,6 +898,8 @@ static void init_reserved_page(unsigned long pfn)#else static inline void init_reserved_page (unsigned long pfn) { +/* Avoid false-positive PageTail() */ +INIT_LIST_HEAD (&pfn_to_page (pfn)->lru); }#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ mm/swap.c +1
-1@@ -200,7 +200,7 @@ out_put_single:__put_single_page (page);return ; } -VM_BUG_ON_PAGE (page_head != page->first_page, page); +VM_BUG_ON_PAGE (page_head !=compound_head (page), page);/* * We can release the refcount taken by * get_page_unless_zero() now that
mm/swap.c static void put_compound_page(struct page *page) · line 264
Quoted 5 lines
mm/swap.c +1
-1@@ -261,7 +261,7 @@ static void put_compound_page(struct page *page)* Case 3 is possible, as we may race with * __split_huge_page_refcount tearing down a THP page. */ - page_head =compound_head_by_tail (page); + page_head =compound_head (page);
This is also in a path after PageTail() is true.
mm/swap.c static void put_compound_page(struct page *page) · line 267
Quoted 4 lines
mm/swap.c +0
-0if (!__compound_tail_refcounted (page_head))put_unrefcounted_compound_page (page_head, page);else
--
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: [PATCHv5 6/7] mm: use 'unsigned int' for page order
In reply to Kirill A. ShutemovQuoted 1 line · Kirill A. Shutemov
Let's try to be consistent about data type of page order.
Long overdue I'd say :) Thanks.
Quoted 2 lines
Signed-off-by Kirill A. Shutemov <[email protected]>Acked-by Michal Hocko <[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>
Re: [PATCHv5 7/7] mm: use 'unsigned int' for compound_dtor/compound_order on 64BIT
In reply to Kirill A. ShutemovQuoted 26 lines · Kirill A. Shutemov
On 64 bit system we have enough space in struct page to encode
compound_dtorandcompound_orderwith unsigned int.On x86-64 it leads to slightly smaller code size due usesage of plain MOV instead of MOVZX (zero-extended move) or similar effect.
allyesconfig:
text data bss dec hex filename
159520446 48146736 72196096 27986327810ae5feevmlinux.pre
159520382 48146736 72196096 27986321410ae5faevmlinux.postOn other architectures without native support of 16-bit data types the difference can be bigger.
Signed-off-by Kirill A. Shutemov <[email protected]>Acked-by Michal Hocko <[email protected]>--- include/linux/mm_types.h | 5 +++++ 1 file changed, 5 insertions(+)include/linux/mm_types.h +0
-0
include/linux/mm_types.h struct page { · line 152
Quoted 3 lines
include/linux/mm_types.h +0
-0@@ -150,8 +150,13 @@ struct page {/* First tail page of compound page */ struct {unsigned long compound_head;/* If bit zero is set */
I'm indifferent to this change. But some comment here to explain would avoid git blame to figure out why it's done?
include/linux/mm_types.h struct page { · line 162
Quoted 11 lines
include/linux/mm_types.h +5
-0+#ifdef CONFIG_64BIT +unsigned int compound_dtor; +unsigned int compound_order; +#else unsigned short int compound_dtor;unsigned short int compound_order; +#endif };#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) &&USE_SPLIT_PMD_PTLOCKS
--
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: [PATCHv5 5/7] mm: make compound_head() robust
In reply to Vlastimil BabkaQuoted 45 lines · Vlastimil Babka
Quoted 44 lines · Kirill A. Shutemov
Hugh has pointed that
compound_head()call can be unsafe in some context. There's one example:CPU0 CPU1
isolate_migratepages_block()
page_count()compound_head() !!PageTail() == true put_page() tail->first_page = NULL head = tail->first_page alloc_pages(__GFP_COMP) prep_compound_page() tail->first_page = head __SetPageTail(p); !!PageTail() == true <head == NULL dereferencing>The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.
We can fix the race by changing how encode
PageTail()andcompound_head()within struct page to be able to update them in one shot.The patch introduces
page->compound_headinto third double word block in front ofcompound_dtorandcompound_order. Bit 0 encodesPageTail()and the rest bits are pointer to head page if bit zero is set.The patch moves
page->pmd_huge_pteout of word, just in case if an architecture definespgtable_tinto something what can have the bit 0 set.
hugetlb_cgroupusespage->lru.nextin the second tail page to store pointer structhugetlb_cgroup. The patch switch it to usepage->privatein the second tail page instead. The space is free since ->first_page is removed from the union.The patch also opens possibility to remove
HUGETLB_CGROUP_MIN_ORDERlimitation, since there's now space in first tail page to store structhugetlb_cgrouppointer. But that's out of scope of the patch.That means
page->compound_headshares storage space with:
Quoted 8 lines
Quoted 6 lines
-
page->lru.next;
-page->next;
-page->rcu_head.next;That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word.
Given the discussion about
rcu_head, that should warrant some summary here :)
Agreed.
Quoted 6 lines
Quoted 6 lines
include/linux/mm_types.h +0
-0@@ -120,7 +120,13 @@ struct page { }; };
Quoted 16 lines
Quoted 12 lines
include/linux/mm_types.h +7
-1-/* Third double word block */ +/* +* 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(). +*/ union {struct list_head lru;/* Pageout list, eg. active_list * protected by zone->lru_lock ! include/linux/mm_types.h struct page { · line 150
Quoted 2 lines
include/linux/mm_types.h +0
-0@@ -143,12 +149,19 @@ struct page {*/ /* First tail page of compound page */ "First tail" doesn't apply for
compound_head.
I'll adjust comments.
Quoted 7 lines
Quoted 7 lines
index
097c7a4bfbd9..330377f83ac7100644mm/huge_memory.c +0
-0@@ -1686,8 +1686,7 @@ static void __split_huge_page_refcount(struct page *page, (1L << PG_unevictable))); page_tail->flags |= (1L << PG_dirty);
Quoted 9 lines
mm/huge_memory.c static void __split_huge_page_refcount(struct page *page, · line 1689
Quoted 3 lines
mm/huge_memory.c +1
-2-/* clear PageTail before overwriting first_page */ -smp_wmb (); +clear_compound_head (page_tail);I would sleep better if this was done before setting all the
page->flagsabove, previously, PageTail was cleared by the first operation which is "page_tail->flags&= ~PAGE_FLAGS_CHECK_AT_PREP;"
I do realize that it doesn't useWRITE_ONCE, so it might have been theoretically broken already, if it does matter.
Right. Nothing enforces particular order. If we really need to have some ordering on PageTail() vs. page->flags let's define it, but so far I don't see a reason to change this part.
Quoted 8 lines
Quoted 8 lines
mm/internal.h +0
-0@@ -61,9 +61,9 @@ static inline void __get_page_tail_foll(struct page *page,* speculative page access (like in * page_cache_get_speculative()) on tail pages. */
Quoted 8 lines
mm/internal.h static inline void __get_page_tail_foll(struct page *page, · line 66
Quoted 5 lines
mm/internal.h +2
-2-VM_BUG_ON_PAGE (atomic_read (&page->first_page->_count) <=0 , page); +VM_BUG_ON_PAGE (atomic_read (&compound_head (page)->_count) <=0 , page);if (get_page_head) -atomic_inc (&page->first_page->_count); +atomic_inc (&compound_head (page)->_count);Doing another
compound_head()seems like overkill when this code already assumes PageTail.
"Overkill"? It's too strong wording for re-read hot cache line.
Quoted 8 lines
All callers do it after if (
PageTail()) which means they already didREAD_ONCE(page->compound_head)and here they do another one. Potentially with different result in bit 0, which would be a subtle bug, that could be interesting to catch with someVM_BUG_ON. I don't know if a direct plainpage->compound_headaccess here instead ofcompound_head()would also result in a re-read, since the previous access did useREAD_ONCE(). Maybe it would be best to reorganize the code here and in the 3 call sites so that theREAD_ONCE()used to determine PageTail also obtains the compound head pointer.
All we would possbily win by the change is few bytes in code. Additional READ_ONCE() only affect code generation. It doesn't introduce any cpu barriers. The cache line with compound_head is in L1 anyway.
I don't see justification to change this part too. If you think we can gain something by reworking this code, feel free to propose patch on top.
Quoted 93 lines
Some of that is probably made moot by your other series, but better let's think of this series as standalone first.
Quoted 90 lines
mm/internal.h +0
-0get_huge_page_tail (page); }mm/memory-failure.c +0
-7@@ -787,8 +787,6 @@ static int me_huge_page(struct page *p, unsigned long pfn)#define lru (1UL << PG_lru)#define swapbacked (1UL << PG_swapbacked)#define head (1UL << PG_head) -#define tail (1UL << PG_tail) -#define compound (1UL << PG_compound)#define slab (1UL << PG_slab)#define reserved (1UL << PG_reserved)@@ -811,12 +809,7 @@ static struct page_state {*/ { slab, slab, MF_MSG_SLAB, me_kernel }, -#ifdef CONFIG_PAGEFLAGS_EXTENDED { head, head, MF_MSG_HUGE, me_huge_page }, - { tail, tail, MF_MSG_HUGE, me_huge_page }, -#else - { compound, compound, MF_MSG_HUGE, me_huge_page }, -#endif { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty }, { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },mm/page_alloc.c +20
-13@@ -424,15 +424,15 @@ out:/* * Higher-order pages are called "compound pages". They are structured thusly: * -* The first PAGE_SIZE page is called the "head page". +* The first PAGE_SIZE page is called the "head page" and have PG_head set. * -* The remaining PAGE_SIZE pages are called "tail pages". +* The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded +* in bit 0 of page->compound_head. The rest of bits is pointer to head page. * -* All pages have PG_compound set. All tail pages have their ->first_page -* pointing at the head page. +* The first tail page's ->compound_dtor holds the offset in array of compound +* page destructors. See compound_page_dtors. * -* The first tail page's ->lru.next holds the address of the compound page's -* put_page() function. Its ->lru.prev holds the order of allocation. +* The first tail page's ->compound_order holds the order of allocation. * This usage means that zero-order pages may not be compound. */ @@ -452,10 +452,7 @@ void prep_compound_page(struct page *page, unsigned long order)for (i =1 ; i < nr_pages; i++) {struct page *p = page + i;set_page_count (p,0 ); - p->first_page = page; -/* Make sure p->first_page is always valid for PageTail() */ -smp_wmb (); -__SetPageTail (p); +set_compound_head (p, page); } }@@ -830,17 +827,30 @@ static void free_one_page(struct zone *zone,static int free_tail_pages_check (struct page *head_page,struct page *page) { -if (!IS_ENABLED (CONFIG_DEBUG_VM )) -return 0 ; +int ret =1 ; + +/* +* We rely page->lru.next never has bit 0 set, unless the page +* is PageTail(). Let's make sure that's true even for poisoned ->lru. +*/ +BUILD_BUG_ON ((unsigned long )LIST_POISON1 &1 ); + +if (!IS_ENABLED (CONFIG_DEBUG_VM )) { + ret =0 ; + goto out; + }if (unlikely (!PageTail (page))) {bad_page (page,"PageTail not set" ,0 ); -return 1 ; + goto out; }
Quoted 8 lines
mm/page_alloc.c static void free_one_page(struct zone *zone, · line 848
Quoted 6 lines
mm/page_alloc.c +3
-3-if (unlikely (page->first_page != head_page)) { -bad_page (page,"first_page not consistent" ,0 ); -return 1 ; +if (unlikely (compound_head (page) != head_page)) { +bad_page (page,"compound_head not consistent" ,0 ); + goto out;Same here, although for a
DEBUG_VMconfig only it's not as important.
Ditto.
Quoted 32 lines
Quoted 32 lines
mm/page_alloc.c +6
-1} -return 0 ; + ret =0 ; +out: +clear_compound_head (page); +return ret; } static void __meminit __init_single_page(struct page *page, unsigned long pfn,@@ -888,6 +898,8 @@ static void init_reserved_page(unsigned long pfn)#else static inline void init_reserved_page (unsigned long pfn) { +/* Avoid false-positive PageTail() */ +INIT_LIST_HEAD (&pfn_to_page (pfn)->lru); }#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ mm/swap.c +1
-1@@ -200,7 +200,7 @@ out_put_single:__put_single_page (page);return ; } -VM_BUG_ON_PAGE (page_head != page->first_page, page); +VM_BUG_ON_PAGE (page_head !=compound_head (page), page);/* * We can release the refcount taken by * get_page_unless_zero() now that
Quoted 8 lines
mm/swap.c static void put_compound_page(struct page *page) · line 264
Quoted 6 lines
mm/swap.c +1
-1@@ -261,7 +261,7 @@ static void put_compound_page(struct page *page)* Case 3 is possible, as we may race with * __split_huge_page_refcount tearing down a THP page. */ - page_head =compound_head_by_tail (page); + page_head =compound_head (page);This is also in a path after
PageTail()is true.
We can only save one branch here: other PageTail() is most likely in other compilation unit and compiler would not be able to eliminate additional load.
Why bother?
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: [PATCHv5 5/7] mm: make compound_head() robust
Quoted 17 lines · Kirill A. Shutemov
Quoted 7 lines
Quoted 7 lines
index
097c7a4bfbd9..330377f83ac7100644mm/huge_memory.c +0
-0@@ -1686,8 +1686,7 @@ static void __split_huge_page_refcount(struct page *page, (1L << PG_unevictable))); page_tail->flags |= (1L << PG_dirty);Quoted 9 lines
mm/huge_memory.c static void __split_huge_page_refcount(struct page *page, · line 1689
Quoted 3 lines
mm/huge_memory.c +1
-2-/* clear PageTail before overwriting first_page */ -smp_wmb (); +clear_compound_head (page_tail);I would sleep better if this was done before setting all the
page->flagsabove, previously, PageTail was cleared by the first operation which is "page_tail->flags&= ~PAGE_FLAGS_CHECK_AT_PREP;"
I do realize that it doesn't useWRITE_ONCE, so it might have been theoretically broken already, if it does matter.
Quoted 3 lines
Right. Nothing enforces particular order. If we really need to have some ordering on
PageTail()vs.page->flagslet's define it, but so far I don't see a reason to change this part.
OK.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
mm/internal.h +0
-0@@ -61,9 +61,9 @@ static inline void __get_page_tail_foll(struct page *page,* speculative page access (like in * page_cache_get_speculative()) on tail pages. */ Quoted 8 lines
mm/internal.h static inline void __get_page_tail_foll(struct page *page, · line 66
Quoted 5 lines
mm/internal.h +2
-2-VM_BUG_ON_PAGE (atomic_read (&page->first_page->_count) <=0 , page); +VM_BUG_ON_PAGE (atomic_read (&compound_head (page)->_count) <=0 , page);if (get_page_head) -atomic_inc (&page->first_page->_count); +atomic_inc (&compound_head (page)->_count);Doing another
compound_head()seems like overkill when this code already assumes PageTail.
Quoted 1 line
"Overkill"? It's too strong wording for re-read hot cache line.
Hm good point.
Quoted 9 lines
Quoted 8 lines
All callers do it after if (
PageTail()) which means they already didREAD_ONCE(page->compound_head)and here they do another one. Potentially with different result in bit 0, which would be a subtle bug, that could be interesting to catch with someVM_BUG_ON. I don't know if a direct plainpage->compound_headaccess here instead ofcompound_head()would also result in a re-read, since the previous access did useREAD_ONCE(). Maybe it would be best to reorganize the code here and in the 3 call sites so that theREAD_ONCE()used to determine PageTail also obtains the compound head pointer.
Quoted 6 lines
All we would possbily win by the change is few bytes in code. Additional
READ_ONCE()only affect code generation. It doesn't introduce any cpu barriers. The cache line withcompound_headis in L1 anyway.I don't see justification to change this part too. If you think we can gain something by reworking this code, feel free to propose patch on top.
OK, it's probably not worth:
add/remove: 0/0 grow/shrink: 1/3 up/down: 7/-66 (-59)
function old new delta follow_trans_huge_pmd 516 523 +7 follow_page_pte 905 893 -12 follow_hugetlb_page 943 919 -24 __get_page_tail 440 410 -30
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
mm/swap.c +1
-1@@ -200,7 +200,7 @@ out_put_single:__put_single_page (page);return ; } -VM_BUG_ON_PAGE (page_head != page->first_page, page); +VM_BUG_ON_PAGE (page_head !=compound_head (page), page);/* * We can release the refcount taken by * get_page_unless_zero() now that Quoted 8 lines
mm/swap.c static void put_compound_page(struct page *page) · line 264
Quoted 6 lines
mm/swap.c +1
-1@@ -261,7 +261,7 @@ static void put_compound_page(struct page *page)* Case 3 is possible, as we may race with * __split_huge_page_refcount tearing down a THP page. */ - page_head =compound_head_by_tail (page); + page_head =compound_head (page);This is also in a path after
PageTail()is true.
Quoted 4 lines
We can only save one branch here: other
PageTail()is most likely in other compilation unit and compiler would not be able to eliminate additional load.
Why bother?
Hmm, right. Bah.
add/remove: 0/0 grow/shrink: 1/0 up/down: 8/0 (8)
function old new delta put_compound_page 500 508 +8
--
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>