1GB THP support on x86_64
Subsystems (29)
cover 1GB THP support on x86_64
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Design
=======
1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited. A new pagechain data structure is added to enable multi-page deposit.
2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and sub_compound_mapcount are introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP and sub_compound_mapcount counts the PMD mapping by using page[N*512 + 3].compound_mapcount.
3. Using CMA allocaiton for 1GB THP: instead of bump MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back to alloc_contig_pages for 1GB THP if necessary.
Patch Organization
=======
Patch 01 adds the new pagechain data structure.
Patch 02 to 13 adds 1GB THP support in variable places.
Patch 14 tries to use alloc_contig_pages for 1GB THP allocaiton.
Patch 15 moves hugetlb_cma reservation to cma.c and rename it to hugepage_cma.
Patch 16 use hugepage_cma reservation for 1GB THP allocation.
Any suggestions and comments are welcome.
Zi Yan (16):
mm: add pagechain container for storing multiple pages.
mm: thp: 1GB anonymous page implementation.
mm: proc: add 1GB THP kpageflag.
mm: thp: 1GB THP copy on write implementation.
mm: thp: handling 1GB THP reference bit.
mm: thp: add 1GB THP split_huge_pud_page() function.
mm: stats: make smap stats understand PUD THPs.
mm: page_vma_walk: teach it about PMD-mapped PUD THP.
mm: thp: 1GB THP support in try_to_unmap().
mm: thp: split 1GB THPs at page reclaim.
mm: thp: 1GB THP follow_p*d_page() support.
mm: support 1GB THP pagemap support.
mm: thp: add a knob to enable/disable 1GB THPs.
mm: page_alloc: >=MAX_ORDER pages allocation an deallocation.
hugetlb: cma: move cma reserve function to cma.c.
mm: thp: use cma reservation for pud thp allocation.
.../admin-guide/kernel-parameters.txt | 2 +- arch/arm64/mm/hugetlbpage.c | 2 +- arch/powerpc/mm/hugetlbpage.c | 2 +- arch/x86/include/asm/pgalloc.h | 68 ++ arch/x86/include/asm/pgtable.h | 26 + arch/x86/kernel/setup.c | 8 +- arch/x86/mm/pgtable.c | 38 + drivers/base/node.c | 3 + fs/proc/meminfo.c | 2 + fs/proc/page.c | 2 + fs/proc/task_mmu.c | 122 ++- include/linux/cma.h | 18 + include/linux/huge_mm.h | 84 +- include/linux/hugetlb.h | 12 - include/linux/memcontrol.h | 5 + include/linux/mm.h | 29 +- include/linux/mm_types.h | 1 + include/linux/mmu_notifier.h | 13 + include/linux/mmzone.h | 1 + include/linux/page-flags.h | 47 + include/linux/pagechain.h | 73 ++ include/linux/pgtable.h | 34 + include/linux/rmap.h | 10 +- include/linux/swap.h | 2 + include/linux/vm_event_item.h | 7 + include/uapi/linux/kernel-page-flags.h | 2 + kernel/events/uprobes.c | 4 +- kernel/fork.c | 5 + mm/cma.c | 119 +++ mm/gup.c | 60 +- mm/huge_memory.c | 939 +++++++++++++++++- mm/hugetlb.c | 114 +-- mm/internal.h | 2 + mm/khugepaged.c | 6 +- mm/ksm.c | 4 +- mm/memcontrol.c | 13 + mm/memory.c | 51 +- mm/mempolicy.c | 21 +- mm/migrate.c | 12 +- mm/page_alloc.c | 57 +- mm/page_vma_mapped.c | 129 ++- mm/pgtable-generic.c | 56 ++ mm/rmap.c | 289 ++++-- mm/swap.c | 31 + mm/swap_slots.c | 2 + mm/swapfile.c | 8 +- mm/userfaultfd.c | 2 +- mm/util.c | 16 +- mm/vmscan.c | 58 +- mm/vmstat.c | 8 + 50 files changed, 2270 insertions(+), 349 deletions(-) create mode 100644 include/linux/pagechain.h
--
2.28.0
RFC PATCH 1/16 mm: add pagechain container for storing multiple pages.
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
--- include/linux/pagechain.h | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/linux/pagechain.h
include/linux/pagechain.h new file +73 -0
@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* include/linux/pagechain.h +* +* In many places it is efficient to batch an operation up against multiple +* pages. A pagechain is a multipage container which is used for that. +*/ + +#ifndef _LINUX_PAGECHAIN_H +#define _LINUX_PAGECHAIN_H + +#include <linux/slab.h> + +/* 14 pointers + two long's align the pagechain structure to a power of two */ +#define PAGECHAIN_SIZE 13 + +struct page ; + +struct pagechain { +struct list_head list; +unsigned int nr; +struct page *pages[PAGECHAIN_SIZE ]; +}; + +static inline void pagechain_init (struct pagechain *pchain) +{ + pchain->nr =0 ; +INIT_LIST_HEAD (&pchain->list); +} + +static inline void pagechain_reinit (struct pagechain *pchain) +{ + pchain->nr =0 ; +} + +static inline unsigned int pagechain_count (struct pagechain *pchain) +{ +return pchain->nr; +} + +static inline unsigned int pagechain_space (struct pagechain *pchain) +{ +return PAGECHAIN_SIZE - pchain->nr; +} + +static inline bool pagechain_empty (struct pagechain *pchain) +{ +return pchain->nr ==0 ; +} + +/* +* Add a page to a pagechain. Returns the number of slots still available. +*/ +static inline unsigned int pagechain_deposit (struct pagechain *pchain,struct page *page) +{ +VM_BUG_ON (!pagechain_space (pchain)); + pchain->pages[pchain->nr++] = page; +return pagechain_space (pchain); +} + +static inline struct page *pagechain_withdraw (struct pagechain *pchain) +{ +if (!pagechain_count (pchain)) +return NULL ; +return pchain->pages[--pchain->nr]; +} + +void __init pagechain_cache_init (void ); +struct pagechain *pagechain_alloc (void ); +void pagechain_free (struct pagechain *pchain); + +#endif /* _LINUX_PAGECHAIN_H */ +
Signature · 1 line
2.28.0
Randy Dunlap
reviewerRe: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
Quoted 26 lines · Zi Yan
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
Signed-off-by Zi Yan <[email protected]>--- include/linux/pagechain.h | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/linux/pagechain.hinclude/linux/pagechain.h new file +5
-0@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* include/linux/pagechain.h +* +* In many places it is efficient to batch an operation up against multiple
include/linux/pagechain.h line 15
Quoted 10 lines
include/linux/pagechain.h new file +10
-0+* pages. A pagechain is a multipage container which is used for that. +*/ + +#ifndef _LINUX_PAGECHAIN_H +#define _LINUX_PAGECHAIN_H + +#include <linux/slab.h> + +/* 14 pointers + two long's align the pagechain structure to a power of two */ +#define PAGECHAIN_SIZE 13
OK, I'll bite. I see neither 14 pointers nor 2 longs below. Is the comment out of date or am I just confuzed?
Update: struct list_head is 2 pointers, so I see 15 pointers & one unsigned int. Where are the 2 longs?
include/linux/pagechain.h line 23
Quoted 8 lines
include/linux/pagechain.h new file +8
-0+ +struct page ; + +struct pagechain { +struct list_head list; +unsigned int nr; +struct page *pages[PAGECHAIN_SIZE ]; +};
thanks.
Signature · 1 line
~Randy
Zi Yan
reviewerRe: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
Quoted 41 lines · Randy Dunlap
Quoted 26 lines · Zi Yan
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
Signed-off-by Zi Yan <[email protected]>--- include/linux/pagechain.h | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/linux/pagechain.hinclude/linux/pagechain.h new file +5
-0@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* include/linux/pagechain.h +* +* In many places it is efficient to batch an operation up against multiple include/linux/pagechain.h line 15
Quoted 10 lines
include/linux/pagechain.h new file +10
-0+* pages. A pagechain is a multipage container which is used for that. +*/ + +#ifndef _LINUX_PAGECHAIN_H +#define _LINUX_PAGECHAIN_H + +#include <linux/slab.h> + +/* 14 pointers + two long's align the pagechain structure to a power of two */ +#define PAGECHAIN_SIZE 13OK, I'll bite. I see neither 14 pointers nor 2 longs below. Is the comment out of date or am I just confuzed?
Quoted 2 lines
Update: struct
list_headis 2 pointers, so I see 15 pointers & one unsigned int. Where are the 2 longs?
My bad. Will change this to:
/* 15 pointers + one long align the pagechain structure to a power of two */ #define PAGECHAIN_SIZE 13
struct page;
struct pagechain {
struct list_head list;
unsigned long nr;
struct page *pages[PAGECHAIN_SIZE];
};Thanks for checking.
—
Best Regards,
Yan Zi
Re: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
In reply to Zi YanQuoted 6 lines · Zi Yan
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
But you've allocated a page for the PMD table. Why can't you use that 4kB to store pointers to the 512 PTE tables?
You could also use an existing data structure like the XArray (although not a pagevec).
Re: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
In reply to Zi YanQuoted 10 lines · Zi Yan
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
Signed-off-by Zi Yan <[email protected]>
Just deposit it to a linked list in the mm_struct as we do for PMD if split ptl disabled.
Signature · 1 line
Kirill A. Shutemov
Zi Yan
reviewerRe: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Zi Yan
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
Quoted 5 lines
Quoted 1 line
Signed-off-by Zi Yan <[email protected]>Just deposit it to a linked list in the
mm_structas we do for PMD if split ptl disabled.
Thank you for checking the patches. Since we don’t have PUD split lock yet, I store the PMD page table pages in a newly added linked list head in mm_struct like you suggested above.
I was too vague about my pagechain design for depositing page table pages for PUD THPs. Sorry about the confusion. Let me clarify why
I am doing this pagechain here too. I am sure there would be some other designs and I am happy to change my code.
In my design, I did not store all page table pages in a single list. I first deposit 512 PTE pages in one PMD page table page’s pmd_huge_pte using pgtable_trans_huge_depsit(), then deposit the PMD page to a newly added linked list in mm_struct. Since pmd_huge_pte shares space with half of lru in struct page, we cannot use lru to link all PMD pages together. As a result, I added pagechain. Also in this way, we can avoid these things:
1. when we withdraw the PMD page during PUD THP split, we don’t need to withdraw 513 page, set up one PMD page, then, deposit 512 PTE pages in that PMD page.
2. we don’t mix PMD page table pages and PTE page table pages in a single list, since they are initialized in different ways. Otherwise, we need to maintain a subtle rule in the single page table page list that in every 513 pages, first one is PMD page table page and the rest are PTE page table pages.
As I am typing, I also realize that my current design does not work when PMD split lock is disabled, so I will fix it. I would store PMD pages and PTE pages in two separate lists in mm_struct.
Any comments?
—
Best Regards,
Yan Zi
Re: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
Quoted 45 lines · Zi Yan
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Zi Yan
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
Quoted 5 lines
Quoted 1 line
Signed-off-by Zi Yan <[email protected]>Just deposit it to a linked list in the
mm_structas we do for PMD if split ptl disabled.Thank you for checking the patches. Since we don’t have PUD split lock yet, I store the PMD page table pages in a newly added linked list head in
mm_structlike you suggested above.I was too vague about my pagechain design for depositing page table pages for PUD THPs. Sorry about the confusion. Let me clarify why
I am doing this pagechain here too. I am sure there would be some other designs and I am happy to change my code.In my design, I did not store all page table pages in a single list. I first deposit 512 PTE pages in one PMD page table page’s
pmd_huge_pteusingpgtable_trans_huge_depsit(), then deposit the PMD page to a newly added linked list inmm_struct. Sincepmd_huge_pteshares space with half of lru in struct page, we cannot use lru to link all PMD pages together. As a result, I added pagechain. Also in this way, we can avoid these things:1. when we withdraw the PMD page during PUD THP split, we don’t need to withdraw 513 page, set up one PMD page, then, deposit 512 PTE pages in that PMD page.
2. we don’t mix PMD page table pages and PTE page table pages in a single list, since they are initialized in different ways. Otherwise, we need to maintain a subtle rule in the single page table page list that in every 513 pages, first one is PMD page table page and the rest are PTE page table pages.
Quoted 6 lines
As I am typing, I also realize that my current design does not work when PMD split lock is disabled, so I will fix it. I would store PMD pages and PTE pages in two separate lists in
mm_struct.Any comments?
Okay, fair enough.
Although, I think you can get away without a new data structure. We don't need double-linked list to deposit page tables. You can rework PTE tables deposit code to have single-linked list and use one pointer of ->lru (with proper name) and make PMD tables deposit to use the other one. This way you can avoid conflict for ->lru.
Does it make sense?
Signature · 1 line
Kirill A. Shutemov
Zi Yan
reviewerRe: [RFC PATCH 01/16] mm: add pagechain container for storing multiple pages.
Quoted 55 lines · Kirill A. Shutemov
Quoted 45 lines · Zi Yan
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Zi Yan
From: Zi Yan <[email protected]>
When depositing page table pages for 1GB THPs, we need 512 PTE pages + 1 PMD page. Instead of counting and depositing 513 pages, we can use the PMD page as a leader page and chain the rest 512 PTE pages with ->lru. This, however, prevents us depositing PMD pages with ->lru, which is currently used by depositing PTE pages for 2MB THPs. So add a new pagechain container for PMD pages.
Quoted 5 lines
Quoted 1 line
Signed-off-by Zi Yan <[email protected]>Just deposit it to a linked list in the
mm_structas we do for PMD if split ptl disabled.Thank you for checking the patches. Since we don’t have PUD split lock yet, I store the PMD page table pages in a newly added linked list head in
mm_structlike you suggested above.I was too vague about my pagechain design for depositing page table pages for PUD THPs. Sorry about the confusion. Let me clarify why
I am doing this pagechain here too. I am sure there would be some other designs and I am happy to change my code.In my design, I did not store all page table pages in a single list. I first deposit 512 PTE pages in one PMD page table page’s
pmd_huge_pteusingpgtable_trans_huge_depsit(), then deposit the PMD page to a newly added linked list inmm_struct. Sincepmd_huge_pteshares space with half of lru in struct page, we cannot use lru to link all PMD pages together. As a result, I added pagechain. Also in this way, we can avoid these things:1. when we withdraw the PMD page during PUD THP split, we don’t need to withdraw 513 page, set up one PMD page, then, deposit 512 PTE pages in that PMD page.
2. we don’t mix PMD page table pages and PTE page table pages in a single list, since they are initialized in different ways. Otherwise, we need to maintain a subtle rule in the single page table page list that in every 513 pages, first one is PMD page table page and the rest are PTE page table pages.
Quoted 6 lines
As I am typing, I also realize that my current design does not work when PMD split lock is disabled, so I will fix it. I would store PMD pages and PTE pages in two separate lists in
mm_struct.Any comments?
Okay, fair enough.
Quoted 7 lines
Although, I think you can get away without a new data structure. We don't need double-linked list to deposit page tables. You can rework PTE tables deposit code to have single-linked list and use one pointer of ->lru (with proper name) and make PMD tables deposit to use the other one. This way you can avoid conflict for ->lru.
Does it make sense?
Yes. Thanks. Will do this in the next version. I think the single linked list from llist.h can be used.
—
Best Regards,
Yan Zi
RFC PATCH 2/16 mm: thp: 1GB anonymous page implementation.
In reply to Zi YanFrom: Zi Yan <[email protected]>
This adds 1GB THP support for anonymous pages. Applications can get 1GB pages during page faults when their VMAs are larger than 1GB. For read-only 1GB zero THP, a shared 1GB zero THP is created for all readers.
--- arch/x86/include/asm/pgalloc.h | 59 +++++++++++ arch/x86/include/asm/pgtable.h | 2 + arch/x86/mm/pgtable.c | 25 +++++ drivers/base/node.c | 3 + fs/proc/meminfo.c | 2 + include/linux/huge_mm.h | 13 ++- include/linux/mm.h | 4 + include/linux/mm_types.h | 1 + include/linux/mmzone.h | 1 + include/linux/pgtable.h | 3 + include/linux/vm_event_item.h | 3 + kernel/fork.c | 5 + mm/huge_memory.c | 188 +++++++++++++++++++++++++++++++-- mm/memory.c | 29 ++++- mm/page_alloc.c | 3 +- mm/pgtable-generic.c | 45 ++++++++ mm/rmap.c | 30 ++++-- mm/vmstat.c | 4 + 18 files changed, 396 insertions(+), 24 deletions(-)
arch/x86/include/asm/pgalloc.h +59 -0
@@ -52,6 +52,18 @@ extern pgd_t *pgd_alloc(struct mm_struct *);extern void pgd_free (struct mm_struct *mm,pgd_t *pgd);extern pgtable_t pte_alloc_one (struct mm_struct *); +extern pgtable_t pte_alloc_order (struct mm_struct *,unsigned long ,int ); + +static inline void pte_free_order (struct mm_struct *mm,struct page *pte, +int order) +{ +int i; + +for (i =0 ; i < (1 <<order); i++) { +pgtable_pte_page_dtor (&pte[i]); +__free_page (&pte[i]); + } +}extern void ___pte_free_tlb (struct mmu_gather *tlb,struct page *pte);
@@ -87,6 +99,53 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,#define pmd_pgtable (pmd) pmd_page(pmd)#if CONFIG_PGTABLE_LEVELS >2 +static inline pmd_t *pmd_alloc_one_page_with_ptes (struct mm_struct *mm,unsigned long addr) +{ +pgtable_t pte_pgtables; +pmd_t *pmd; +spinlock_t *pmd_ptl; +int i; + + pte_pgtables =pte_alloc_order (mm, addr, +HPAGE_PUD_ORDER -HPAGE_PMD_ORDER ); +if (!pte_pgtables) +return NULL ; + + pmd =pmd_alloc_one (mm, addr); +if (unlikely (!pmd)) { +pte_free_order (mm, pte_pgtables, +HPAGE_PUD_ORDER -HPAGE_PMD_ORDER ); +return NULL ; + } + pmd_ptl =pmd_lock (mm, pmd); + +for (i =0 ; i < (1 <<(HPAGE_PUD_ORDER -HPAGE_PMD_ORDER )); i++) +pgtable_trans_huge_deposit (mm, pmd, pte_pgtables + i); + +spin_unlock (pmd_ptl); + +return pmd; +} + +static inline void pmd_free_page_with_ptes (struct mm_struct *mm,pmd_t *pmd) +{ +spinlock_t *pmd_ptl; +int i; + +BUG_ON ((unsigned long )pmd & (PAGE_SIZE -1 )); + pmd_ptl =pmd_lock (mm, pmd); + +for (i =0 ; i < (1 <<(HPAGE_PUD_ORDER -HPAGE_PMD_ORDER )); i++) { +pgtable_t pte_pgtable; + + pte_pgtable =pgtable_trans_huge_withdraw (mm, pmd); +pte_free (mm, pte_pgtable); + } + +spin_unlock (pmd_ptl); +pmd_free (mm, pmd); +} +extern void ___pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd); static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
arch/x86/include/asm/pgtable.h +2 -0
@@ -1141,6 +1141,8 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned longreturn native_pmdp_get_and_clear (pmdp); } +#define mk_pud (page, pgprot) pfn_pud(page_to_pfn(page), (pgprot)) +#define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEARstatic inline pud_t pudp_huge_get_and_clear (struct mm_struct *mm,unsigned long addr,pud_t *pudp)
arch/x86/mm/pgtable.c +25 -0
@@ -33,6 +33,31 @@ pgtable_t pte_alloc_one(struct mm_struct *mm)return __pte_alloc_one (mm, __userpte_alloc_gfp); } +pgtable_t pte_alloc_order (struct mm_struct *mm,unsigned long address,int order) +{ +struct page *pte; +int i; + + pte =alloc_pages (__userpte_alloc_gfp, order); +if (!pte) +return NULL ; +split_page (pte, order); +for (i =1 ; i < (1 << order); i++) +set_page_private (pte + i,0 ); + +for (i =0 ; i < (1 <<order); i++) { +if (!pgtable_pte_page_ctor (&pte[i])) { +__free_page (&pte[i]); +while (--i >=0 ) { +pgtable_pte_page_dtor (&pte[i]); +__free_page (&pte[i]); + } +return NULL ; + } + } +return pte; +} +static int __init setup_userpte (char *arg) {if (!arg)
drivers/base/node.c +3 -0
@@ -428,6 +428,7 @@ static ssize_t node_read_meminfo(struct device *dev,"Node %d SUnreclaim: %8lu kB\n" #ifdef CONFIG_TRANSPARENT_HUGEPAGE"Node %d AnonHugePages: %8lu kB\n" +"Node %d AnonHugePUDPages: %8lu kB\n" "Node %d ShmemHugePages: %8lu kB\n" "Node %d ShmemPmdMapped: %8lu kB\n" "Node %d FileHugePages: %8lu kB\n"
@@ -457,6 +458,8 @@ static ssize_t node_read_meminfo(struct device *dev, , nid,K (node_page_state (pgdat,NR_ANON_THPS ) *HPAGE_PMD_NR ), + nid,K (node_page_state (pgdat,NR_ANON_THPS_PUD ) * +HPAGE_PUD_NR ), nid,K (node_page_state (pgdat,NR_SHMEM_THPS ) *HPAGE_PMD_NR ), nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) *
fs/proc/meminfo.c +2 -0
@@ -130,6 +130,8 @@ static int meminfo_proc_show(struct seq_file *m, void *v)#ifdef CONFIG_TRANSPARENT_HUGEPAGE show_val_kb (m,"AnonHugePages: " ,global_node_page_state (NR_ANON_THPS ) *HPAGE_PMD_NR ); +show_val_kb (m,"AnonHugePUDPages: " , +global_node_page_state (NR_ANON_THPS_PUD ) *HPAGE_PUD_NR );show_val_kb (m,"ShmemHugePages: " ,global_node_page_state (NR_SHMEM_THPS ) *HPAGE_PMD_NR ); show_val_kb(m, "ShmemPmdMapped: ",
include/linux/huge_mm.h +11 -2
@@ -18,10 +18,15 @@ extern int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD extern void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud); +extern int do_huge_pud_anonymous_page (struct vm_fault *vmf);#else static inline void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) { } +extern int do_huge_pud_anonymous_page (struct vm_fault *vmf) +{ +return VM_FAULT_FALLBACK ; +}#endif extern vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf,pmd_t orig_pmd);
@@ -115,6 +120,9 @@ extern struct kobj_attribute shmem_enabled_attr;#define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)#define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER) +#define HPAGE_PUD_ORDER (HPAGE_PUD_SHIFT-PAGE_SHIFT) +#define HPAGE_PUD_NR (1<<HPAGE_PUD_ORDER) +#ifdef CONFIG_TRANSPARENT_HUGEPAGE #define HPAGE_PMD_SHIFT PMD_SHIFT#define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT)
@@ -276,7 +284,7 @@ static inline unsigned int thp_order(struct page *page) {VM_BUG_ON_PGFLAGS (PageTail (page), page);if (PageHead (page)) -return HPAGE_PMD_ORDER ; +return page[1 ].compound_order;return 0 ; }
@@ -288,7 +296,7 @@ static inline int thp_nr_pages(struct page *page) {VM_BUG_ON_PGFLAGS (PageTail (page), page);if (PageHead (page)) -return HPAGE_PMD_NR ; +return (1 <<page[1 ].compound_order);return 1 ; }
@@ -320,6 +328,7 @@ struct page *mm_get_huge_zero_page(struct mm_struct *mm);void mm_put_huge_zero_page (struct mm_struct *mm);#define mk_huge_pmd (page, prot) pmd_mkhuge(mk_pmd(page, prot)) +#define mk_huge_pud (page, prot) pud_mkhuge(mk_pud(page, prot))static inline bool thp_migration_supported (void ) {
include/linux/mm.h +4 -0
@@ -31,6 +31,7 @@#include <linux/sizes.h> #include <linux/sched.h> #include <linux/pgtable.h> +#include <linux/pagechain.h> struct mempolicy ;struct anon_vma ;
@@ -2184,6 +2185,7 @@ static inline void pgtable_init(void) {ptlock_cache_init ();pgtable_cache_init (); +pagechain_cache_init (); }static inline bool pgtable_pte_page_ctor (struct page *page)
@@ -2316,6 +2318,8 @@ static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud)return ptl; } +#define pud_huge_pte (mm, pud) ((mm)->pud_huge_pte) +extern void __init pagecache_init (void );extern void __init free_area_init_memoryless_node (int nid);extern void free_initmem (void );
include/linux/mm_types.h +1 -0
@@ -513,6 +513,7 @@ struct mm_struct {#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS pgtable_t pmd_huge_pte;/* protected by page_table_lock */ #endif +struct list_head pud_huge_pte;/* protected by page_table_lock */ #ifdef CONFIG_NUMA_BALANCING /* * numa_next_scan is the next time that the PTEs will be marked
include/linux/mmzone.h +1 -0
@@ -196,6 +196,7 @@ enum node_stat_item {NR_FILE_THPS ,NR_FILE_PMDMAPPED ,NR_ANON_THPS , +NR_ANON_THPS_PUD ,NR_VMSCAN_WRITE ,NR_VMSCAN_IMMEDIATE ,/* Prioritise for reclaim when writeback ends */ NR_DIRTIED, /* page dirtyings since bootup */
include/linux/pgtable.h +3 -0
@@ -462,10 +462,13 @@ static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma,#ifndef __HAVE_ARCH_PGTABLE_DEPOSITextern void pgtable_trans_huge_deposit (struct mm_struct *mm,pmd_t *pmdp,pgtable_t pgtable); +extern void pgtable_trans_huge_pud_deposit (struct mm_struct *mm,pud_t *pudp, +pgtable_t pgtable);#endif #ifndef __HAVE_ARCH_PGTABLE_WITHDRAWextern pgtable_t pgtable_trans_huge_withdraw (struct mm_struct *mm,pmd_t *pmdp); +extern pgtable_t pgtable_trans_huge_pud_withdraw (struct mm_struct *mm,pud_t *pudp);#endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE
include/linux/vm_event_item.h +3 -0
@@ -92,6 +92,9 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, THP_DEFERRED_SPLIT_PAGE, THP_SPLIT_PMD,#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +THP_FAULT_ALLOC_PUD , +THP_FAULT_FALLBACK_PUD , +THP_FAULT_FALLBACK_PUD_CHARGE ,THP_SPLIT_PUD , #endif THP_ZERO_PAGE_ALLOC,
kernel/fork.c +5 -0
@@ -663,6 +663,10 @@ static void check_mm(struct mm_struct *mm)#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS VM_BUG_ON_MM (mm->pmd_huge_pte, mm);#endif +VM_BUG_ON_MM (!list_empty (&mm->pud_huge_pte) && + !pagechain_empty (list_first_entry (&mm->pud_huge_pte, + struct pagechain, list)), + mm); }#define allocate_mm () (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
@@ -1023,6 +1027,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS mm->pmd_huge_pte =NULL ;#endif +INIT_LIST_HEAD (&mm->pud_huge_pte);mm_init_uprobes_state (mm);if (current->mm) {
mm/huge_memory.c +177 -11
237 lines not on this page · raw message
mm/memory.c +27 -2
@@ -4147,14 +4147,13 @@ static vm_fault_t create_huge_pud(struct vm_fault *vmf) defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) /* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous(vmf->vma)) - goto split; + return do_huge_pud_anonymous_page(vmf);if (vmf->vma->vm_ops->huge_fault) {vm_fault_t ret = vmf->vma->vm_ops->huge_fault (vmf,PE_SIZE_PUD );if (!(ret &VM_FAULT_FALLBACK ))return ret; } -split:/* COW or write-notify not handled on PUD level: split pud.*/ __split_huge_pud (vmf->vma, vmf->pud, vmf->address);#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
@@ -5098,3 +5097,29 @@ void ptlock_free(struct page *page) kmem_cache_free(page_ptl_cachep, page->ptl); }#endif + +static struct kmem_cache *pagechain_cachep; + +void __init pagechain_cache_init (void ) +{ + pagechain_cachep =kmem_cache_create ("pagechain" , +sizeof (struct pagechain ),0 ,SLAB_PANIC ,NULL ); +} + +struct pagechain *pagechain_alloc (void ) +{ +struct pagechain *chain; + + chain =kmem_cache_alloc (pagechain_cachep,GFP_ATOMIC ); + +if (!chain) +return NULL ; + +pagechain_init (chain); +return chain; +} + +void pagechain_free (struct pagechain *pchain) +{ +kmem_cache_free (pagechain_cachep, pchain); +}
mm/page_alloc.c +2 -1
@@ -5443,7 +5443,8 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)K (node_page_state (pgdat,NR_SHMEM_THPS ) *HPAGE_PMD_NR ),K (node_page_state (pgdat,NR_SHMEM_PMDMAPPED ) *HPAGE_PMD_NR ), -K (node_page_state (pgdat,NR_ANON_THPS ) *HPAGE_PMD_NR ), +K (node_page_state (pgdat,NR_ANON_THPS ) *HPAGE_PMD_NR + +node_page_state (pgdat,NR_ANON_THPS_PUD ) *HPAGE_PUD_NR ), #endifK (node_page_state (pgdat,NR_WRITEBACK_TEMP )), node_page_state(pgdat, NR_KERNEL_STACK_KB),
mm/pgtable-generic.c +45 -0
63 lines not on this page · raw message
mm/rmap.c +22 -8
@@ -726,6 +726,7 @@ pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address)pgd_t *pgd;p4d_t *p4d;pud_t *pud; +pud_t pude;pmd_t *pmd =NULL ;pmd_t pmde;
@@ -738,7 +739,10 @@ pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) goto out; pud =pud_offset (p4d, address); -if (!pud_present (*pud)) + + pude = *pud; +barrier (); +if (!pud_present (pude) ||pud_trans_huge (pude)) goto out; pmd =pmd_offset (pud, address);
@@ -1033,7 +1037,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping * @exclusive: the page is exclusively owned by the current process */ static void __page_set_anon_rmap(struct page *page,
@@ -1137,8 +1141,12 @@ void do_page_add_anon_rmap(struct page *page,* pte lock(a spinlock) is held, which implies preemption * disabled. */ -if (compound) -__inc_lruvec_page_state (page,NR_ANON_THPS ); +if (compound) { +if (nr ==HPAGE_PMD_NR ) +__inc_lruvec_page_state (page,NR_ANON_THPS ); +else +__inc_lruvec_page_state (page,NR_ANON_THPS_PUD ); + }__mod_lruvec_page_state (page,NR_ANON_MAPPED , nr); }
@@ -1180,7 +1188,10 @@ void page_add_new_anon_rmap(struct page *page,if (hpage_pincount_available (page))atomic_set (compound_pincount_ptr (page),0 ); -__inc_lruvec_page_state (page,NR_ANON_THPS ); +if (nr ==HPAGE_PMD_NR ) +__inc_lruvec_page_state (page,NR_ANON_THPS ); +else +__inc_lruvec_page_state (page,NR_ANON_THPS_PUD ); }else {/* Anon THP always mapped first with PMD */ VM_BUG_ON_PAGE (PageTransCompound (page), page);
@@ -1286,14 +1297,17 @@ static void page_remove_anon_compound_rmap(struct page *page)if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ))return ; -__dec_lruvec_page_state (page,NR_ANON_THPS ); +if (thp_nr_pages (page) ==HPAGE_PMD_NR ) +__dec_lruvec_page_state (page,NR_ANON_THPS ); +else +__dec_lruvec_page_state (page,NR_ANON_THPS_PUD );if (TestClearPageDoubleMap (page)) {/* * Subpages can be mapped with PTEs too. Check how many of * them are still mapped. */ -for (i =0 , nr =0 ; i <HPAGE_PMD_NR ; i++) { +for (i =0 , nr =0 ; i <thp_nr_pages (page); i++) {if (atomic_add_negative (-1 , &page[i]._mapcount)) nr++; }
@@ -1306,7 +1320,7 @@ static void page_remove_anon_compound_rmap(struct page *page)if (nr && nr <HPAGE_PMD_NR )deferred_split_huge_page (page); }else { - nr =HPAGE_PMD_NR ; + nr =thp_nr_pages (page); }if (unlikely (PageMlocked (page)))
mm/vmstat.c +4 -0
@@ -1209,6 +1209,7 @@ const char * const vmstat_text[] = {"nr_file_hugepages" ,"nr_file_pmdmapped" ,"nr_anon_transparent_hugepages" , +"nr_anon_transparent_pud_hugepages" ,"nr_vmscan_write" ,"nr_vmscan_immediate_reclaim" , "nr_dirtied",
@@ -1325,6 +1326,9 @@ const char * const vmstat_text[] = {"thp_deferred_split_page" ,"thp_split_pmd" , #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +"thp_fault_alloc_pud" , +"thp_fault_fallback_pud" , +"thp_fault_fallback_pud_charge" ,"thp_split_pud" , #endif "thp_zero_page_alloc",
Signature · 1 line
2.28.0
RFC PATCH 3/16 mm: proc: add 1GB THP kpageflag.
In reply to Zi YanFrom: Zi Yan <[email protected]>
Bit 27 is used to identify 1GB THP.
--- fs/proc/page.c | 2 ++ include/uapi/linux/kernel-page-flags.h | 2 ++ 2 files changed, 4 insertions(+)
fs/proc/page.c +2 -0
@@ -161,6 +161,8 @@ u64 stable_page_flags(struct page *page) u |=BIT_ULL (KPF_ZERO_PAGE ); u |=BIT_ULL (KPF_THP ); } +if (compound_order (head) ==HPAGE_PUD_ORDER ) + u |=1 <<KPF_PUD_THP ; }else if (is_zero_pfn (page_to_pfn (page))) u |=BIT_ULL (KPF_ZERO_PAGE );
include/uapi/linux/kernel-page-flags.h +2 -0
@@ -36,5 +36,7 @@#define KPF_ZERO_PAGE 24#define KPF_IDLE 25#define KPF_PGTABLE 26 +#define KPF_PUD_THP 27 +#endif /* _UAPILINUX_KERNEL_PAGE_FLAGS_H */
Signature · 1 line
2.28.0
Re: [RFC PATCH 03/16] mm: proc: add 1GB THP kpageflag.
Quoted 28 lines · Zi Yan
From: Zi Yan <[email protected]>
Bit 27 is used to identify 1GB THP.
Signed-off-by Zi Yan <[email protected]>--- fs/proc/page.c | 2 ++ include/uapi/linux/kernel-page-flags.h | 2 ++ 2 files changed, 4 insertions(+)fs/proc/page.c +2
-0@@ -161,6 +161,8 @@ u64 stable_page_flags(struct page *page) u |=BIT_ULL (KPF_ZERO_PAGE ); u |=BIT_ULL (KPF_THP ); } +if (compound_order (head) ==HPAGE_PUD_ORDER ) + u |=1 <<KPF_PUD_THP ; }else if (is_zero_pfn (page_to_pfn (page))) u |=BIT_ULL (KPF_ZERO_PAGE );include/uapi/linux/kernel-page-flags.h +0
-0
include/uapi/linux/kernel-page-flags.h line 40
Quoted 5 lines
include/uapi/linux/kernel-page-flags.h +2
-0@@ -36,5 +36,7 @@#define KPF_ZERO_PAGE 24#define KPF_IDLE 25#define KPF_PGTABLE 26 +#define KPF_PUD_THP 27 +
Redundant newline.
Quoted 5 lines
include/uapi/linux/kernel-page-flags.h +0
-0#endif /* _UAPILINUX_KERNEL_PAGE_FLAGS_H */ Signature · 1 line
2.28.0
Signature · 1 line
Kirill A. Shutemov
RFC PATCH 4/16 mm: thp: 1GB THP copy on write implementation.
In reply to Zi YanFrom: Zi Yan <[email protected]>
COW on 1GB THPs will fall back to 2MB THPs if 1GB THP is not available.
--- arch/x86/include/asm/pgalloc.h | 9 ++++++ include/linux/huge_mm.h | 5 ++++ mm/huge_memory.c | 54 ++++++++++++++++++++++++++++++++++ mm/memory.c | 2 +- mm/swapfile.c | 4 ++- 5 files changed, 72 insertions(+), 2 deletions(-)
arch/x86/include/asm/pgalloc.h +9 -0
@@ -98,6 +98,15 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,#define pmd_pgtable (pmd) pmd_page(pmd) +static inline void pud_populate_with_pgtable (struct mm_struct *mm,pud_t *pud, +struct page *pte) +{ +unsigned long pfn =page_to_pfn (pte); + +paravirt_alloc_pmd (mm, pfn); +set_pud (pud,__pud (((pteval_t )pfn <<PAGE_SHIFT ) | _PAGE_TABLE)); +} +#if CONFIG_PGTABLE_LEVELS >2 static inline pmd_t *pmd_alloc_one_page_with_ptes (struct mm_struct *mm,unsigned long addr) {
include/linux/huge_mm.h +5 -0
@@ -19,6 +19,7 @@ extern int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD extern void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud);extern int do_huge_pud_anonymous_page (struct vm_fault *vmf); +extern vm_fault_t do_huge_pud_wp_page (struct vm_fault *vmf,pud_t orig_pud);#else static inline void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) {
@@ -27,6 +28,10 @@ extern int do_huge_pud_anonymous_page(struct vm_fault *vmf) {return VM_FAULT_FALLBACK ; } +extern vm_fault_t do_huge_pud_wp_page (struct vm_fault *vmf,pud_t orig_pud) +{ +return VM_FAULT_FALLBACK ; +}#endif extern vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf,pmd_t orig_pmd);
mm/huge_memory.c +54 -0
60 lines not on this page · raw message
mm/memory.c +1 -1
@@ -4165,7 +4165,7 @@ static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)#ifdef CONFIG_TRANSPARENT_HUGEPAGE /* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous (vmf->vma)) -return VM_FAULT_FALLBACK ; +return do_huge_pud_wp_page (vmf, orig_pud);if (vmf->vma->vm_ops->huge_fault)return vmf->vma->vm_ops->huge_fault (vmf,PE_SIZE_PUD );#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
mm/swapfile.c +3 -1
@@ -1635,7 +1635,9 @@ static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,/* hugetlbfs shouldn't call it */ VM_BUG_ON_PAGE (PageHuge (page), page); -if (!IS_ENABLED (CONFIG_THP_SWAP ) ||likely (!PageTransCompound (page))) { +if (!IS_ENABLED (CONFIG_THP_SWAP ) || +unlikely (compound_order (compound_head (page)) ==HPAGE_PUD_ORDER ) || +likely (!PageTransCompound (page))) { mapcount =page_trans_huge_mapcount (page, total_mapcount);if (PageSwapCache (page)) swapcount =page_swapcount (page);
Signature · 1 line
2.28.0
RFC PATCH 5/16 mm: thp: handling 1GB THP reference bit.
In reply to Zi YanFrom: Zi Yan <[email protected]>
Add PUD-level TLB flush ops and teach page_vma_mapped_talk about 1GB THPs.
--- arch/x86/include/asm/pgtable.h | 3 +++ arch/x86/mm/pgtable.c | 13 +++++++++++++ include/linux/mmu_notifier.h | 13 +++++++++++++ include/linux/pgtable.h | 14 ++++++++++++++ include/linux/rmap.h | 1 + mm/page_vma_mapped.c | 33 +++++++++++++++++++++++++++++---- mm/rmap.c | 12 +++++++++--- 7 files changed, 82 insertions(+), 7 deletions(-)
arch/x86/include/asm/pgtable.h +3 -0
@@ -1127,6 +1127,9 @@ extern int pudp_test_and_clear_young(struct vm_area_struct *vma,extern int pmdp_clear_flush_young (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp); +#define __HAVE_ARCH_PUDP_CLEAR_YOUNG_FLUSH +extern int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp);#define pmd_write pmd_writestatic inline int pmd_write (pmd_t pmd)
arch/x86/mm/pgtable.c +13 -0
@@ -633,6 +633,19 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma, return young; } +int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp) +{ +int young; + +VM_BUG_ON (address & ~HPAGE_PUD_MASK ); + + young =pudp_test_and_clear_young (vma, address, pudp); +if (young) +flush_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); + +return young; +}#endif /**
include/linux/mmu_notifier.h +13 -0
@@ -557,6 +557,19 @@ static inline void mmu_notifier_range_init_migrate( __young; \ }) +#define pudp_clear_flush_young_notify (__vma, __address, __pudp) \ +({ \ + int __young; \ + struct vm_area_struct *___vma = __vma; \ + unsigned long ___address = __address; \ + __young = pudp_clear_flush_young(___vma, ___address, __pudp); \ + __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ + ___address, \ + ___address + \ + PUD_SIZE); \ + __young; \ +}) +#define ptep_clear_young_notify (__vma, __address, __ptep) \ ({ \ int __young; \
include/linux/pgtable.h +14 -0
@@ -240,6 +240,20 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif +#ifndef __HAVE_ARCH_PUDP_CLEAR_YOUNG_FLUSH +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +extern int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp); +#else +int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp) +{ +BUILD_BUG (); +return 0 ; +} +#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ +#endif +#ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address,
include/linux/rmap.h +1 -0
@@ -206,6 +206,7 @@ struct page_vma_mapped_walk {struct page *page;struct vm_area_struct *vma;unsigned long address; +pud_t *pud;pmd_t *pmd;pte_t *pte;spinlock_t *ptl;
mm/page_vma_mapped.c +29 -4
@@ -145,9 +145,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)struct page *page = pvmw->page;pgd_t *pgd;p4d_t *p4d; -pud_t *pud; +pud_t pude;pmd_t pmde; +if (!pvmw->pte && !pvmw->pmd && pvmw->pud) +return not_found (pvmw); +/* The only possible pmd mapping has been handled on last iteration */ if (pvmw->pmd && !pvmw->pte)return not_found (pvmw);
@@ -174,10 +177,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) p4d =p4d_offset (pgd, pvmw->address);if (!p4d_present (*p4d))return false; - pud =pud_offset (p4d, pvmw->address); -if (!pud_present (*pud)) + pvmw->pud =pud_offset (p4d, pvmw->address); + +/* +* Make sure the pud value isn't cached in a register by the +* compiler and used as a stale value after we've observed a +* subsequent update. +*/ + pude =READ_ONCE (*pvmw->pud); +if (pud_trans_huge (pude)) { + pvmw->ptl =pud_lock (mm, pvmw->pud); +if (likely (pud_trans_huge (*pvmw->pud))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (pud_page (*pvmw->pud) != page) +return not_found (pvmw); +return true; + }else { +/* THP pud was split under us: handle on pmd level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + } + }else if (!pud_present (pude))return false; - pvmw->pmd =pmd_offset (pud, pvmw->address); + + pvmw->pmd =pmd_offset (pvmw->pud, pvmw->address);/* * Make sure the pmd value isn't cached in a register by the * compiler and used as a stale value after we've observed a
@@ -213,6 +237,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) }else if (!pmd_present (pmde)) {return false; } +if (!map_pte (pvmw)) goto next_pte;while (1 ) {
mm/rmap.c +9 -3
@@ -803,9 +803,15 @@ static bool page_referenced_one(struct page *page, struct vm_area_struct *vma, referenced++; } }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_flush_young_notify (vma, address, - pvmw.pmd)) - referenced++; +if (pvmw.pmd) { +if (pmdp_clear_flush_young_notify (vma, address, + pvmw.pmd)) + referenced++; + }else if (pvmw.pud) { +if (pudp_clear_flush_young_notify (vma, address, + pvmw.pud)) + referenced++; + } }else {/* unexpected pmd-mapped page? */ WARN_ON_ONCE (1 );
Signature · 1 line
2.28.0
Re: [RFC PATCH 05/16] mm: thp: handling 1GB THP reference bit.
Quoted 149 lines · Zi Yan
From: Zi Yan <[email protected]>
Add PUD-level TLB flush ops and teach
page_vma_mapped_talkabout 1GB THPs.Signed-off-by Zi Yan <[email protected]>--- arch/x86/include/asm/pgtable.h | 3 +++ arch/x86/mm/pgtable.c | 13 +++++++++++++ include/linux/mmu_notifier.h | 13 +++++++++++++ include/linux/pgtable.h | 14 ++++++++++++++ include/linux/rmap.h | 1 + mm/page_vma_mapped.c | 33 +++++++++++++++++++++++++++++---- mm/rmap.c | 12 +++++++++--- 7 files changed, 82 insertions(+), 7 deletions(-)arch/x86/include/asm/pgtable.h +3
-0@@ -1127,6 +1127,9 @@ extern int pudp_test_and_clear_young(struct vm_area_struct *vma,extern int pmdp_clear_flush_young (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp); +#define __HAVE_ARCH_PUDP_CLEAR_YOUNG_FLUSH +extern int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp);#define pmd_write pmd_writestatic inline int pmd_write (pmd_t pmd)arch/x86/mm/pgtable.c +13
-0@@ -633,6 +633,19 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma, return young; } +int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp) +{ +int young; + +VM_BUG_ON (address & ~HPAGE_PUD_MASK ); + + young =pudp_test_and_clear_young (vma, address, pudp); +if (young) +flush_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); + +return young; +}#endif /** include/linux/mmu_notifier.h +13
-0@@ -557,6 +557,19 @@ static inline void mmu_notifier_range_init_migrate( __young; \ }) +#define pudp_clear_flush_young_notify (__vma, __address, __pudp) \ +({ \ + int __young; \ + struct vm_area_struct *___vma = __vma; \ + unsigned long ___address = __address; \ + __young = pudp_clear_flush_young(___vma, ___address, __pudp); \ + __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ + ___address, \ + ___address + \ + PUD_SIZE); \ + __young; \ +}) +#define ptep_clear_young_notify (__vma, __address, __ptep) \ ({ \ int __young; \include/linux/pgtable.h +14
-0@@ -240,6 +240,20 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif +#ifndef __HAVE_ARCH_PUDP_CLEAR_YOUNG_FLUSH +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +extern int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp); +#else +int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp) +{ +BUILD_BUG (); +return 0 ; +} +#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ +#endif +#ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address,include/linux/rmap.h +1
-0@@ -206,6 +206,7 @@ struct page_vma_mapped_walk {struct page *page;struct vm_area_struct *vma;unsigned long address; +pud_t *pud;pmd_t *pmd;pte_t *pte;spinlock_t *ptl;mm/page_vma_mapped.c +14
-3@@ -145,9 +145,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)struct page *page = pvmw->page;pgd_t *pgd;p4d_t *p4d; -pud_t *pud; +pud_t pude;pmd_t pmde; +if (!pvmw->pte && !pvmw->pmd && pvmw->pud) +return not_found (pvmw); +/* The only possible pmd mapping has been handled on last iteration */ if (pvmw->pmd && !pvmw->pte)return not_found (pvmw);@@ -174,10 +177,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) p4d =p4d_offset (pgd, pvmw->address);if (!p4d_present (*p4d))return false; - pud =pud_offset (p4d, pvmw->address); -if (!pud_present (*pud)) + pvmw->pud =pud_offset (p4d, pvmw->address); + +/* +* Make sure the pud value isn't cached in a register by the +* compiler and used as a stale value after we've observed a +* subsequent update. +*/ + pude =READ_ONCE (*pvmw->pud); +if (pud_trans_huge (pude)) { + pvmw->ptl =pud_lock (mm, pvmw->pud);
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 199
Quoted 10 lines
mm/page_vma_mapped.c +10
-0+if (likely (pud_trans_huge (*pvmw->pud))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (pud_page (*pvmw->pud) != page) +return not_found (pvmw); +return true; + }else { +/* THP pud was split under us: handle on pmd level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ;
Hm. What makes you sure the pmd table is established here?
I have not looked at PUD THP handling of MADV_DONTNEED yet, but for PMD THP can became pmd_none() at any point (unless ptl is locked).
mm/rmap.c
Quoted 18 lines
mm/page_vma_mapped.c +5
-1+ } + }else if (!pud_present (pude))return false; - pvmw->pmd =pmd_offset (pud, pvmw->address); + + pvmw->pmd =pmd_offset (pvmw->pud, pvmw->address);/* * Make sure the pmd value isn't cached in a register by the * compiler and used as a stale value after we've observed a @@ -213,6 +237,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) }else if (!pmd_present (pmde)) {return false; } +if (!map_pte (pvmw)) goto next_pte;while (1 ) {mm/rmap.c +0
-0
Why?
Quoted 26 lines
index
10195a2421cf..77cec0658b76100644mm/rmap.c +9
-3@@ -803,9 +803,15 @@ static bool page_referenced_one(struct page *page, struct vm_area_struct *vma, referenced++; } }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_flush_young_notify (vma, address, - pvmw.pmd)) - referenced++; +if (pvmw.pmd) { +if (pmdp_clear_flush_young_notify (vma, address, + pvmw.pmd)) + referenced++; + }else if (pvmw.pud) { +if (pudp_clear_flush_young_notify (vma, address, + pvmw.pud)) + referenced++; + } }else {/* unexpected pmd-mapped page? */ WARN_ON_ONCE (1 );Signature · 1 line
2.28.0
Signature · 1 line
Kirill A. Shutemov
Zi Yan
reviewerRe: [RFC PATCH 05/16] mm: thp: handling 1GB THP reference bit.
Quoted 163 lines · Kirill A. Shutemov
Quoted 149 lines · Zi Yan
From: Zi Yan <[email protected]>
Add PUD-level TLB flush ops and teach
page_vma_mapped_talkabout 1GB THPs.Signed-off-by Zi Yan <[email protected]>--- arch/x86/include/asm/pgtable.h | 3 +++ arch/x86/mm/pgtable.c | 13 +++++++++++++ include/linux/mmu_notifier.h | 13 +++++++++++++ include/linux/pgtable.h | 14 ++++++++++++++ include/linux/rmap.h | 1 + mm/page_vma_mapped.c | 33 +++++++++++++++++++++++++++++---- mm/rmap.c | 12 +++++++++--- 7 files changed, 82 insertions(+), 7 deletions(-)arch/x86/include/asm/pgtable.h +3
-0@@ -1127,6 +1127,9 @@ extern int pudp_test_and_clear_young(struct vm_area_struct *vma,extern int pmdp_clear_flush_young (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp); +#define __HAVE_ARCH_PUDP_CLEAR_YOUNG_FLUSH +extern int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp);#define pmd_write pmd_writestatic inline int pmd_write (pmd_t pmd)arch/x86/mm/pgtable.c +13
-0@@ -633,6 +633,19 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma, return young; } +int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp) +{ +int young; + +VM_BUG_ON (address & ~HPAGE_PUD_MASK ); + + young =pudp_test_and_clear_young (vma, address, pudp); +if (young) +flush_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); + +return young; +}#endif /** include/linux/mmu_notifier.h +13
-0@@ -557,6 +557,19 @@ static inline void mmu_notifier_range_init_migrate( __young; \ }) +#define pudp_clear_flush_young_notify (__vma, __address, __pudp) \ +({ \ + int __young; \ + struct vm_area_struct *___vma = __vma; \ + unsigned long ___address = __address; \ + __young = pudp_clear_flush_young(___vma, ___address, __pudp); \ + __young |= mmu_notifier_clear_flush_young(___vma->vm_mm, \ + ___address, \ + ___address + \ + PUD_SIZE); \ + __young; \ +}) +#define ptep_clear_young_notify (__vma, __address, __ptep) \ ({ \ int __young; \include/linux/pgtable.h +14
-0@@ -240,6 +240,20 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif +#ifndef __HAVE_ARCH_PUDP_CLEAR_YOUNG_FLUSH +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +extern int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp); +#else +int pudp_clear_flush_young (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp) +{ +BUILD_BUG (); +return 0 ; +} +#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ +#endif +#ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address,include/linux/rmap.h +1
-0@@ -206,6 +206,7 @@ struct page_vma_mapped_walk {struct page *page;struct vm_area_struct *vma;unsigned long address; +pud_t *pud;pmd_t *pmd;pte_t *pte;spinlock_t *ptl;mm/page_vma_mapped.c +14
-3@@ -145,9 +145,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)struct page *page = pvmw->page;pgd_t *pgd;p4d_t *p4d; -pud_t *pud; +pud_t pude;pmd_t pmde; +if (!pvmw->pte && !pvmw->pmd && pvmw->pud) +return not_found (pvmw); +/* The only possible pmd mapping has been handled on last iteration */ if (pvmw->pmd && !pvmw->pte)return not_found (pvmw);@@ -174,10 +177,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) p4d =p4d_offset (pgd, pvmw->address);if (!p4d_present (*p4d))return false; - pud =pud_offset (p4d, pvmw->address); -if (!pud_present (*pud)) + pvmw->pud =pud_offset (p4d, pvmw->address); + +/* +* Make sure the pud value isn't cached in a register by the +* compiler and used as a stale value after we've observed a +* subsequent update. +*/ + pude =READ_ONCE (*pvmw->pud); +if (pud_trans_huge (pude)) { + pvmw->ptl =pud_lock (mm, pvmw->pud);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 199
Quoted 10 lines
mm/page_vma_mapped.c +10
-0+if (likely (pud_trans_huge (*pvmw->pud))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (pud_page (*pvmw->pud) != page) +return not_found (pvmw); +return true; + }else { +/* THP pud was split under us: handle on pmd level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ;Hm. What makes you sure the pmd table is established here?
Quoted 2 lines
I have not looked at PUD THP handling of
MADV_DONTNEEDyet, but for PMD THP can becamepmd_none()at any point (unless ptl is locked).
You are right. I need to check pud_present here and only
go to pmd level when pud is present, otherwise just return with not_found.
Thanks.
Quoted 13 lines
Quoted 13 lines
mm/page_vma_mapped.c +4
-1+ } + }else if (!pud_present (pude))return false; - pvmw->pmd =pmd_offset (pud, pvmw->address); + + pvmw->pmd =pmd_offset (pvmw->pud, pvmw->address);/* * Make sure the pmd value isn't cached in a register by the * compiler and used as a stale value after we've observed a @@ -213,6 +237,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) }else if (!pmd_present (pmde)) {return false;
Quoted 9 lines
mm/rmap.c
Quoted 6 lines
mm/page_vma_mapped.c +1
-0} +if (!map_pte (pvmw)) goto next_pte;while (1 ) {mm/rmap.c +0
-0Why?
The extra newline? Will remove it.
Quoted 18 lines
Quoted 18 lines
index
10195a2421cf..77cec0658b76100644mm/rmap.c +8
-3@@ -803,9 +803,15 @@ static bool page_referenced_one(struct page *page, struct vm_area_struct *vma, referenced++; } }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_flush_young_notify (vma, address, - pvmw.pmd)) - referenced++; +if (pvmw.pmd) { +if (pmdp_clear_flush_young_notify (vma, address, + pvmw.pmd)) + referenced++; + }else if (pvmw.pud) { +if (pudp_clear_flush_young_notify (vma, address, + pvmw.pud)) + referenced++;
Quoted 11 lines
Quoted 8 lines
mm/rmap.c +1
-0+ } }else {/* unexpected pmd-mapped page? */ WARN_ON_ONCE (1 );Signature · 1 line
2.28.0Signature · 1 line
Kirill A. Shutemov
—
Best Regards,
Yan Zi
RFC PATCH 6/16 mm: thp: add 1GB THP split_huge_pud_page() function.
In reply to Zi YanFrom: Zi Yan <[email protected]>
It mimics PMD-level THP split. In addition, to support PMD-mapped PUD THP, PMDPageInPUD() is used. For the mapcount of PMD-mapped PUD THP, sub_compound_mapcount() is used, which uses (head_page+3).compound_mapcount, since each base page's mapcount is used for PTE mapping. PagePUDDoubleMap() is used for both PUD-mapped and PMD-mapped PUD THPs.
page_xxx_rmap() functions now have an extra page order parameter to distinguish different THP sizes.
--- arch/x86/include/asm/pgtable.h | 21 ++ include/linux/huge_mm.h | 31 +- include/linux/memcontrol.h | 5 + include/linux/mm.h | 25 +- include/linux/page-flags.h | 47 +++ include/linux/pgtable.h | 17 ++ include/linux/rmap.h | 9 +- include/linux/swap.h | 2 + include/linux/vm_event_item.h | 4 + kernel/events/uprobes.c | 4 +- mm/huge_memory.c | 536 +++++++++++++++++++++++++++++++-- mm/hugetlb.c | 4 +- mm/khugepaged.c | 6 +- mm/ksm.c | 4 +- mm/memcontrol.c | 13 + mm/memory.c | 18 +- mm/migrate.c | 10 +- mm/page_alloc.c | 20 +- mm/pgtable-generic.c | 11 + mm/rmap.c | 106 +++++-- mm/swap.c | 31 ++ mm/swapfile.c | 4 +- mm/userfaultfd.c | 2 +- mm/util.c | 16 +- mm/vmstat.c | 4 + 25 files changed, 852 insertions(+), 98 deletions(-)
arch/x86/include/asm/pgtable.h +21 -0
@@ -630,6 +630,12 @@ static inline pmd_t pmd_mkinvalid(pmd_t pmd) __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); } +static inline pud_t pud_mknotpresent (pud_t pud) +{ +return pfn_pud (pud_pfn (pud), +__pgprot (pud_flags (pud) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); +} +static inline u64 flip_protnone_guard (u64 oldval,u64 val,u64 mask);static inline pte_t pte_modify (pte_t pte,pgprot_t newprot)
@@ -1246,6 +1252,21 @@ static inline p4d_t *user_to_kernel_p4dp(p4d_t *p4dp) }#endif /* CONFIG_PAGE_TABLE_ISOLATION */ +#ifndef pudp_establish +#define pudp_establish pudp_establish +static inline pud_t pudp_establish (struct vm_area_struct *vma, +unsigned long address,pud_t *pudp,pud_t pud) +{ +if (IS_ENABLED (CONFIG_SMP )) { +return xchg (pudp, pud); + }else { +pud_t old = *pudp; + *pudp = pud; +return old; + } +} +#endif +/* * clone_pgd_range(pgd_t *dst, pgd_t *src, int count); *
include/linux/huge_mm.h +29 -2
@@ -227,17 +227,27 @@ void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,void split_huge_pmd_address (struct vm_area_struct *vma,unsigned long address,bool freeze,struct page *page); +bool can_split_huge_pud_page (struct page *page,int *pextra_pins); +int split_huge_pud_page_to_list (struct page *page,struct list_head *list); +static inline int split_huge_pud_page (struct page *page) +{ +return split_huge_pud_page_to_list (page,NULL ); +}void __split_huge_pud (struct vm_area_struct *vma,pud_t *pud, -unsigned long address); +unsigned long address,bool freeze,struct page *page);#define split_huge_pud (__vma, __pud, __address) \ do { \ pud_t *____pud = (__pud); \ if (pud_trans_huge(*____pud) \ || pud_devmap(*____pud)) \ - __split_huge_pud(__vma, __pud, __address); \ + __split_huge_pud(__vma, __pud, __address, \ + false, NULL); \ } while (0) +void split_huge_pud_address (struct vm_area_struct *vma,unsigned long address, +bool freeze,struct page *page); +extern int hugepage_madvise (struct vm_area_struct *vma,unsigned long *vm_flags,int advice); extern void vma_adjust_trans_huge(struct vm_area_struct *vma,
@@ -427,8 +437,25 @@ static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,static inline void split_huge_pmd_address (struct vm_area_struct *vma,unsigned long address,bool freeze,struct page *page) {} +static inline bool +can_split_huge_pud_page (struct page *page,int *pextra_pins) +{ +BUILD_BUG (); +return false; +} +static inline int +split_huge_pud_page_to_list (struct page *page,struct list_head *list) +{ +return 0 ; +} +static inline int split_huge_pud_page (struct page *page) +{ +return 0 ; +}#define split_huge_pud (__vma, __pmd, __address) \ do { } while (0) +static inline void split_huge_pud_address (struct vm_area_struct *vma, +unsigned long address,bool freeze,struct page *page) {}static inline int hugepage_madvise (struct vm_area_struct *vma,unsigned long *vm_flags,int advice)
include/linux/memcontrol.h +5 -0
@@ -929,6 +929,7 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm,#ifdef CONFIG_TRANSPARENT_HUGEPAGE void mem_cgroup_split_huge_fixup (struct page *head); +void mem_cgroup_split_huge_pud_fixup (struct page *head);#endif #else /* CONFIG_MEMCG */
@@ -1261,6 +1262,10 @@ static inline void mem_cgroup_split_huge_fixup(struct page *head) { } +static inline void mem_cgroup_split_huge_pud_fixup (struct page *head) +{ +} +static inline void count_memcg_events (struct mem_cgroup *memcg,enum vm_event_item idx,unsigned long count)
include/linux/mm.h +18 -7
@@ -797,6 +797,24 @@ static inline int compound_mapcount(struct page *page)return head_compound_mapcount (page); } +static inline unsigned int compound_order (struct page *page); +static inline atomic_t *sub_compound_mapcount_ptr (struct page *page,int sub_level) +{ +struct page *head =compound_head (page); + +VM_BUG_ON_PAGE (!PageCompound (page), page); +VM_BUG_ON_PAGE (compound_order (head) !=HPAGE_PUD_ORDER , page); +VM_BUG_ON_PAGE ((page - head) %HPAGE_PMD_NR , page); +VM_BUG_ON_PAGE (sub_level !=1 , page); +return &page[2 + sub_level].compound_mapcount; +} + +/* Only works for PUD pages */ +static inline int sub_compound_mapcount (struct page *page) +{ +return atomic_read (sub_compound_mapcount_ptr (page,1 )) +1 ; +} +/* * The atomic page->_mapcount, starts from -1: so that transitions * both from it and to it can be tracked, using atomic_inc_and_test
@@ -889,13 +907,6 @@ static inline void destroy_compound_page(struct page *page) compound_page_dtors[page[1 ].compound_dtor](page); } -static inline unsigned int compound_order (struct page *page) -{ -if (!PageHead (page)) -return 0 ; -return page[1 ].compound_order; -} -static inline bool hpage_pincount_available (struct page *page) {/*
include/linux/page-flags.h +47 -0
78 lines not on this page · raw message
include/linux/pgtable.h +17 -0
@@ -505,6 +505,11 @@ extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp);#endif +#ifndef __HAVE_ARCH_PUDP_INVALIDATE +extern pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, +pud_t *pudp); +#endif +#ifndef __HAVE_ARCH_PTE_SAMEstatic inline int pte_same (pte_t pte_a,pte_t pte_b) {
@@ -1158,6 +1163,18 @@ static inline pmd_t pmd_read_atomic(pmd_t *pmdp) }#endif +#ifndef pud_read_atomic +static inline pud_t pud_read_atomic (pud_t *pudp) +{ +/* +* Depend on compiler for an atomic pmd read. NOTE: this is +* only going to work, if the pmdval_t isn't larger than +* an unsigned long. +*/ +return *pudp; +} +#endif +#ifndef arch_needs_pgtable_deposit#define arch_needs_pgtable_deposit () (false)#endif
include/linux/rmap.h +5 -4
@@ -99,6 +99,7 @@ enum ttu_flags {TTU_RMAP_LOCKED =0x80 ,/* do not grab rmap lock: * caller holds it */ TTU_SPLIT_FREEZE =0x100 ,/* freeze pte under splitting thp */ + TTU_SPLIT_HUGE_PUD = 0x200, /* split huge PUD if any */ };#ifdef CONFIG_MMU
@@ -171,13 +172,13 @@ struct anon_vma *page_get_anon_vma(struct page *page);*/ void page_move_anon_rmap (struct page *,struct vm_area_struct *);void page_add_anon_rmap (struct page *,struct vm_area_struct *, -unsigned long ,bool ); +unsigned long ,bool ,int );void do_page_add_anon_rmap (struct page *,struct vm_area_struct *, -unsigned long ,int ); +unsigned long ,int ,int );void page_add_new_anon_rmap (struct page *,struct vm_area_struct *, -unsigned long ,bool ); +unsigned long ,bool ,int );void page_add_file_rmap (struct page *,bool ); -void page_remove_rmap (struct page *,bool ); +void page_remove_rmap (struct page *,bool ,int );void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long );
include/linux/swap.h +2 -0
@@ -340,6 +340,8 @@ extern void lru_note_cost_page(struct page *);extern void lru_cache_add (struct page *);extern void lru_add_page_tail (struct page *page,struct page *page_tail,struct lruvec *lruvec,struct list_head *head); +extern void lru_add_pud_page_tail (struct page *page,struct page *page_tail, +struct lruvec *lruvec,struct list_head *head);extern void mark_page_accessed (struct page *);extern void lru_add_drain (void );extern void lru_add_drain_cpu (int cpu);
include/linux/vm_event_item.h +4 -0
@@ -96,6 +96,10 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,THP_FAULT_FALLBACK_PUD ,THP_FAULT_FALLBACK_PUD_CHARGE ,THP_SPLIT_PUD , +THP_SPLIT_PUD_PAGE , +THP_SPLIT_PUD_PAGE_FAILED , +THP_ZERO_PUD_PAGE_ALLOC , +THP_ZERO_PUD_PAGE_ALLOC_FAILED , #endifTHP_ZERO_PAGE_ALLOC , THP_ZERO_PAGE_ALLOC_FAILED,
kernel/events/uprobes.c +2 -2
@@ -183,7 +183,7 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,if (new_page) {get_page (new_page); -page_add_new_anon_rmap (new_page, vma, addr, false); +page_add_new_anon_rmap (new_page, vma, addr, false,0 );lru_cache_add_inactive_or_unevictable (new_page, vma); }else /* no new page, just dec_mm_counter for old_page */
@@ -200,7 +200,7 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,set_pte_at_notify (mm, addr, pvmw.pte,mk_pte (new_page, vma->vm_page_prot)); -page_remove_rmap (old_page, false); +page_remove_rmap (old_page, false,0 );if (!page_mapped (old_page))try_to_free_swap (old_page);page_vma_mapped_walk_done (&pvmw);
mm/huge_memory.c +504 -32
695 lines not on this page · raw message
mm/hugetlb.c +2 -2
@@ -3993,7 +3993,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,set_page_dirty (page);hugetlb_count_sub (pages_per_huge_page (h), mm); -page_remove_rmap (page, true); +page_remove_rmap (page, true,huge_page_order (h));spin_unlock (ptl);tlb_remove_page_size (tlb, page,huge_page_size (h));
@@ -4218,7 +4218,7 @@ static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);set_huge_pte_at (mm, haddr, ptep,make_huge_pte (vma, new_page,1 )); -page_remove_rmap (old_page, true); +page_remove_rmap (old_page, true,huge_page_order (h));hugepage_add_new_anon_rmap (new_page, vma, haddr);set_page_huge_active (new_page);/* Make the old page be freed below */
mm/khugepaged.c +3 -3
@@ -762,7 +762,7 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,* superfluous. */ pte_clear (vma->vm_mm, address, _pte); -page_remove_rmap (src_page, false); +page_remove_rmap (src_page, false,0 );spin_unlock (ptl);free_page_and_swap_cache (src_page); }
@@ -1172,7 +1172,7 @@ static void collapse_huge_page(struct mm_struct *mm,spin_lock (pmd_ptl);BUG_ON (!pmd_none (*pmd)); -page_add_new_anon_rmap (new_page, vma, address, true); +page_add_new_anon_rmap (new_page, vma, address, true,HPAGE_PMD_ORDER );lru_cache_add_inactive_or_unevictable (new_page, vma);pgtable_trans_huge_deposit (mm, pmd, pgtable);set_pmd_at (mm, address, pmd, _pmd);
@@ -1475,7 +1475,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)if (pte_none (*pte))continue ; page =vm_normal_page (vma, addr, *pte); -page_remove_rmap (page, false); +page_remove_rmap (page, false,HPAGE_PMD_ORDER ); }pte_unmap_unlock (start_pte, ptl);
mm/ksm.c +2 -2
@@ -1153,7 +1153,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,*/ if (!is_zero_pfn (page_to_pfn (kpage))) {get_page (kpage); -page_add_anon_rmap (kpage, vma, addr, false); +page_add_anon_rmap (kpage, vma, addr, false,0 ); newpte =mk_pte (kpage, vma->vm_page_prot); }else { newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage),
@@ -1177,7 +1177,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,ptep_clear_flush (vma, addr, ptep);set_pte_at_notify (mm, addr, ptep, newpte); -page_remove_rmap (page, false); +page_remove_rmap (page, false,0 );if (!page_mapped (page))try_to_free_swap (page);put_page (page);
mm/memcontrol.c +13 -0
@@ -3232,6 +3232,19 @@ void mem_cgroup_split_huge_fixup(struct page *head) head[i].mem_cgroup = memcg; } } + +void mem_cgroup_split_huge_pud_fixup (struct page *head) +{ +int i; + +if (mem_cgroup_disabled ()) +return ; + +for (i =HPAGE_PMD_NR ; i <HPAGE_PUD_NR ; i +=HPAGE_PMD_NR ) + head[i].mem_cgroup = head->mem_cgroup; + +/*__mod_memcg_state(head->mem_cgroup, MEMCG_RSS_HUGE, -HPAGE_PUD_NR);*/ +}#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #ifdef CONFIG_MEMCG_SWAP
mm/memory.c +9 -9
@@ -1090,7 +1090,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,mark_page_accessed (page); } rss[mm_counter (page)]--; -page_remove_rmap (page, false); +page_remove_rmap (page, false,0 );if (unlikely (page_mapcount (page) <0 ))print_bad_pte (vma, addr, ptent, page);if (unlikely (__tlb_remove_page (tlb, page))) {
@@ -1118,7 +1118,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,pte_clear_not_present_full (mm, addr, pte, tlb->fullmm); rss[mm_counter (page)]--; -page_remove_rmap (page, false); +page_remove_rmap (page, false,0 );put_page (page);continue ; }
@@ -2725,7 +2725,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)* thread doing COW. */ ptep_clear_flush_notify (vma, vmf->address, vmf->pte); -page_add_new_anon_rmap (new_page, vma, vmf->address, false); +page_add_new_anon_rmap (new_page, vma, vmf->address, false,0 );lru_cache_add_inactive_or_unevictable (new_page, vma);/* * We call the notify macro here because, when using secondary
@@ -2757,7 +2757,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)* mapcount is visible. So transitively, TLBs to * old page will be flushed before it can be reused. */ -page_remove_rmap (old_page, false); +page_remove_rmap (old_page, false,0 ); }/* Free the old page.. */
@@ -3273,10 +3273,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)/* ksm created a completely new copy */ if (unlikely (page != swapcache && swapcache)) { -page_add_new_anon_rmap (page, vma, vmf->address, false); +page_add_new_anon_rmap (page, vma, vmf->address, false,0 );lru_cache_add_inactive_or_unevictable (page, vma); }else { -do_page_add_anon_rmap (page, vma, vmf->address, exclusive); +do_page_add_anon_rmap (page, vma, vmf->address, exclusive,0 ); }swap_free (entry);
@@ -3420,7 +3420,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) }inc_mm_counter_fast (vma->vm_mm,MM_ANONPAGES ); -page_add_new_anon_rmap (page, vma, vmf->address, false); +page_add_new_anon_rmap (page, vma, vmf->address, false,0 );lru_cache_add_inactive_or_unevictable (page, vma); setpte:set_pte_at (vma->vm_mm, vmf->address, vmf->pte, entry);
@@ -3678,7 +3678,7 @@ vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct page *page)/* copy-on-write page */ if (write && !(vma->vm_flags &VM_SHARED )) {inc_mm_counter_fast (vma->vm_mm,MM_ANONPAGES ); -page_add_new_anon_rmap (page, vma, vmf->address, false); +page_add_new_anon_rmap (page, vma, vmf->address, false,0 );lru_cache_add_inactive_or_unevictable (page, vma); }else {inc_mm_counter_fast (vma->vm_mm,mm_counter_file (page));
@@ -4155,7 +4155,7 @@ static vm_fault_t create_huge_pud(struct vm_fault *vmf) return ret; }/* COW or write-notify not handled on PUD level: split pud.*/ -__split_huge_pud (vmf->vma, vmf->pud, vmf->address); +split_huge_pud (vmf->vma, vmf->pud, vmf->address);#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ return VM_FAULT_FALLBACK ; }
mm/migrate.c +5 -5
@@ -270,7 +270,7 @@ static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,set_pte_at (vma->vm_mm, pvmw.address, pvmw.pte, pte);if (PageAnon (new)) -page_add_anon_rmap (new, vma, pvmw.address, false); +page_add_anon_rmap (new, vma, pvmw.address, false,0 );else page_add_file_rmap (new, false); }
@@ -2194,7 +2194,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,* new page and page_add_new_anon_rmap guarantee the copy is * visible before the pagetable update. */ -page_add_anon_rmap (new_page, vma, start, true); +page_add_anon_rmap (new_page, vma, start, true,HPAGE_PMD_ORDER );/* * At this point the pmd is numa/protnone (i.e. non present) and the TLB * has already been flushed globally. So no TLB can be currently
@@ -2211,7 +2211,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,page_ref_unfreeze (page,2 );mlock_migrate_page (new_page, page); -page_remove_rmap (page, true); +page_remove_rmap (page, true,HPAGE_PMD_ORDER );set_page_owner_migrate_reason (new_page,MR_NUMA_MISPLACED );spin_unlock (ptl);
@@ -2455,7 +2455,7 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* drop page refcount. Page won't be freed, as we took * a reference just above. */ -page_remove_rmap (page, false); +page_remove_rmap (page, false,0 );put_page (page);if (pte_present (pte))
@@ -2940,7 +2940,7 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate, goto unlock_abort;inc_mm_counter (mm,MM_ANONPAGES ); -page_add_new_anon_rmap (page, vma, addr, false); +page_add_new_anon_rmap (page, vma, addr, false,0 );if (!is_zone_device_page (page))lru_cache_add_inactive_or_unevictable (page, vma);get_page (page);
mm/page_alloc.c +19 -1
@@ -679,6 +679,9 @@ void prep_compound_page(struct page *page, unsigned int order)atomic_set (compound_mapcount_ptr (page),-1 );if (hpage_pincount_available (page))atomic_set (compound_pincount_ptr (page),0 ); +if (order ==HPAGE_PUD_ORDER ) +for (i =0 ; i <HPAGE_PUD_NR ; i +=HPAGE_PMD_NR ) +atomic_set (sub_compound_mapcount_ptr (&page[i],1 ),-1 ); }#ifdef CONFIG_DEBUG_PAGEALLOC
@@ -1132,6 +1135,15 @@ static int free_tail_pages_check(struct page *head_page, struct page *page)*/ break ;default : +/* sub_compound_map_ptr store here */ +if (compound_order (head_page) ==HPAGE_PUD_ORDER && + (page - head_page) %HPAGE_PMD_NR ==3 ) { +if (unlikely (atomic_read (&page->compound_mapcount) !=-1 )) { +pr_err ("sub_compound_mapcount: %d\n" ,atomic_read (&page->compound_mapcount) +1 ); +bad_page (page,"nonzero sub_compound_mapcount" ); + } +break ; + }if (page->mapping !=TAIL_MAPPING ) {bad_page (page,"corrupted mapping in tail page" ); goto out;
@@ -1183,8 +1195,14 @@ static __always_inline bool free_pages_prepare(struct page *page,VM_BUG_ON_PAGE (compound &&compound_order (page) != order, page); -if (compound) +if (compound) {ClearPageDoubleMap (page); +if (order ==HPAGE_PUD_ORDER ) { +ClearPagePUDDoubleMap (page); +for (i =0 ; i <HPAGE_PUD_NR ; i +=HPAGE_PMD_NR ) +ClearPageDoubleMap (&page[i]); + } + }for (i =1 ; i < (1 << order); i++) {if (compound) bad +=free_tail_pages_check (page, page + i);
mm/pgtable-generic.c +11 -0
@@ -245,6 +245,17 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, }#endif +#ifndef __HAVE_ARCH_PUDP_INVALIDATE +pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, +pud_t *pudp) +{ +pud_t old =pudp_establish (vma, address, pudp,pud_mknotpresent (*pudp)); + +flush_pud_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); +return old; +} +#endif +#ifndef pmdp_collapse_flushpmd_t pmdp_collapse_flush (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp)
mm/rmap.c +83 -23
186 lines not on this page · raw message
mm/swap.c +31 -0
@@ -964,6 +964,37 @@ void lru_add_page_tail(struct page *page, struct page *page_tail, page_lru(page_tail)); } } + +/* used by __split_pud_huge_page_tail() */ +void lru_add_pud_page_tail (struct page *page,struct page *page_tail, +struct lruvec *lruvec,struct list_head *list) +{ +VM_BUG_ON_PAGE (!PageHead (page), page); +VM_BUG_ON_PAGE (PageLRU (page_tail), page); +VM_BUG_ON (NR_CPUS !=1 && + !spin_is_locked (&lruvec_pgdat (lruvec)->lru_lock)); + +if (!list) +SetPageLRU (page_tail); + +if (likely (PageLRU (page))) +list_add_tail (&page_tail->lru, &page->lru); +else if (list) { +/* page reclaim is reclaiming a huge page */ +get_page (page_tail); +list_add_tail (&page_tail->lru, list); + }else { +/* +* Head page has not yet been counted, as an hpage, +* so we must account for each subpage individually. +* +* Put page_tail on the list at the correct position +* so they all end up in order. +*/ +add_page_to_lru_list_tail (page_tail, lruvec, +page_lru (page_tail)); + } +}#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
mm/swapfile.c +2 -2
@@ -1921,9 +1921,9 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,set_pte_at (vma->vm_mm, addr, pte,pte_mkold (mk_pte (page, vma->vm_page_prot)));if (page == swapcache) { -page_add_anon_rmap (page, vma, addr, false); +page_add_anon_rmap (page, vma, addr, false,0 ); }else {/* ksm created a completely new copy */ -page_add_new_anon_rmap (page, vma, addr, false); +page_add_new_anon_rmap (page, vma, addr, false,0 );lru_cache_add_inactive_or_unevictable (page, vma); }swap_free (entry);
mm/userfaultfd.c +1 -1
@@ -122,7 +122,7 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm, goto out_release_uncharge_unlock;inc_mm_counter (dst_mm,MM_ANONPAGES ); -page_add_new_anon_rmap (page, dst_vma, dst_addr, false); +page_add_new_anon_rmap (page, dst_vma, dst_addr, false,0 );lru_cache_add_inactive_or_unevictable (page, dst_vma);set_pte_at (dst_mm, dst_addr, dst_pte, _dst_pte);
mm/util.c +13 -3
@@ -713,17 +713,27 @@ struct address_space *page_mapping_file(struct page *page)int __page_mapcount (struct page *page) {int ret; +struct page *head =compound_head (page); +/* base page mapping */ ret =atomic_read (&page->_mapcount) +1 ; + +/* PMDInPUD mapping */ +if (compound_order (head) ==HPAGE_PUD_ORDER ) { +struct page *sub_compound_page = head + + (((page - head) /HPAGE_PMD_NR ) *HPAGE_PMD_NR ); + + ret +=sub_compound_mapcount (sub_compound_page); + }/* * For file THP page->_mapcount contains total number of mapping * of the page: no need to look into compound_mapcount. */ if (!PageAnon (page) && !PageHuge (page))return ret; - page =compound_head (page); - ret +=atomic_read (compound_mapcount_ptr (page)) +1 ; -if (PageDoubleMap (page)) +/* highest compound mapping */ + ret +=atomic_read (compound_mapcount_ptr (head)) +1 ; +if (PageDoubleMap (head)) ret--;return ret; }
mm/vmstat.c +4 -0
@@ -1330,6 +1330,10 @@ const char * const vmstat_text[] = {"thp_fault_fallback_pud" ,"thp_fault_fallback_pud_charge" ,"thp_split_pud" , +"thp_split_pud_page" , +"thp_split_pud_page_failed" , +"thp_zero_pud_page_alloc" , +"thp_zero_pud_page_alloc_failed" , #endif"thp_zero_page_alloc" , "thp_zero_page_alloc_failed",
Signature · 1 line
2.28.0
Re: [RFC PATCH 06/16] mm: thp: add 1GB THP split_huge_pud_page() function.
Quoted 1 line · Zi Yan
25 files changed, 852 insertions(+), 98 deletions(-)
It's way too big to have meaningful review.
Signature · 1 line
Kirill A. Shutemov
Zi Yan
reviewerRe: [RFC PATCH 06/16] mm: thp: add 1GB THP split_huge_pud_page() function.
Quoted 4 lines · Kirill A. Shutemov
Quoted 1 line · Zi Yan
25 files changed, 852 insertions(+), 98 deletions(-)It's way too big to have meaningful review.
Will split it into small patches in the next version.
—
Best Regards,
Yan Zi
RFC PATCH 7/16 mm: stats: make smap stats understand PUD THPs.
In reply to Zi YanFrom: Zi Yan <[email protected]>
--- fs/proc/task_mmu.c | 63 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-)
fs/proc/task_mmu.c +58 -5
@@ -430,10 +430,9 @@ static void smaps_page_accumulate(struct mem_size_stats *mss, }static void smaps_account (struct mem_size_stats *mss,struct page *page, -bool compound,bool young,bool dirty,bool locked) +unsigned long size,bool young,bool dirty,bool locked) { -int i, nr = compound ?compound_nr (page) :1 ; -unsigned long size = nr *PAGE_SIZE ; +int i, nr = size /PAGE_SIZE ;/* * First accumulate quantities that depend only on |size| and the type
@@ -536,7 +535,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,if (!page)return ; -smaps_account (mss, page, false,pte_young (*pte),pte_dirty (*pte), locked); +smaps_account (mss, page,PAGE_SIZE ,pte_young (*pte),pte_dirty (*pte), locked); }#ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -567,8 +566,44 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,/* pass */ ; else mss->file_thp += HPAGE_PMD_SIZE; - smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked); +smaps_account (mss, page,HPAGE_PMD_SIZE ,pmd_young (*pmd), +pmd_dirty (*pmd), locked); } + +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +static void smaps_pud_entry (pud_t *pud,unsigned long addr, +struct mm_walk *walk) +{ +struct mem_size_stats *mss = walk->private; +struct vm_area_struct *vma = walk->vma; +bool locked = !!(vma->vm_flags &VM_LOCKED ); +struct page *page =NULL ; + +if (pud_present (*pud)) { +/* FOLL_DUMP will return -EFAULT on huge zero page */ + page =follow_trans_huge_pud (vma, addr, pud,FOLL_DUMP ); + } +if (IS_ERR_OR_NULL (page)) +return ; +if (PageAnon (page)) + mss->anonymous_thp +=HPAGE_PUD_SIZE ; +else if (PageSwapBacked (page)) + mss->shmem_thp +=HPAGE_PUD_SIZE ; +else if (is_zone_device_page (page)) +/* pass */ ; +else + mss->file_thp +=HPAGE_PUD_SIZE ; +smaps_account (mss, page,HPAGE_PUD_SIZE ,pud_young (*pud), +pud_dirty (*pud), locked); +} +#else +static void smaps_pud_entry (pud_t *pud,unsigned long addr, +struct mm_walk *walk) +{ +} +#endif + +#else static void smaps_pmd_entry (pmd_t *pmd,unsigned long addr,struct mm_walk *walk)
@@ -576,6 +611,23 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, }#endif +static int smaps_pud_range (pud_t *pud,unsigned long addr,unsigned long end, +struct mm_walk *walk) +{ +struct vm_area_struct *vma = walk->vma; +spinlock_t *ptl; + + ptl =pud_trans_huge_lock (pud, vma); +if (ptl) { +smaps_pud_entry (pud, addr, walk); +spin_unlock (ptl); + walk->action =ACTION_CONTINUE ; + } + +cond_resched (); +return 0 ; +} +static int smaps_pte_range (pmd_t *pmd,unsigned long addr,unsigned long end,struct mm_walk *walk) {
@@ -713,6 +765,7 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,#endif /* HUGETLB_PAGE */ static const struct mm_walk_ops smaps_walk_ops = { + .pud_entry = smaps_pud_range, .pmd_entry = smaps_pte_range, .hugetlb_entry = smaps_hugetlb_range, };
Signature · 1 line
2.28.0
RFC PATCH 8/16 mm: page_vma_walk: teach it about PMD-mapped PUD THP.
In reply to Zi YanFrom: Zi Yan <[email protected]>
We now have PMD-mapped PUD THP and PTE-mapped PUD THP, page_vma_walk should handle them properly.
--- mm/page_vma_mapped.c | 116 ++++++++++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 34 deletions(-)
mm/page_vma_mapped.c +82 -34
@@ -52,6 +52,22 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw)return true; } +static bool map_pmd (struct page_vma_mapped_walk *pvmw) +{ +pmd_t pmde; + + pvmw->pmd =pmd_offset (pvmw->pud, pvmw->address); + pmde =READ_ONCE (*pvmw->pmd); +if (pmd_trans_huge (pmde) ||is_pmd_migration_entry (pmde)) { + pvmw->ptl =pmd_lock (pvmw->vma->vm_mm, pvmw->pmd); +return true; + }else if (!pmd_present (pmde)) +return false; + + pvmw->ptl =pmd_lock (pvmw->vma->vm_mm, pvmw->pmd); +return true; +} +static inline bool pfn_is_match (struct page *page,unsigned long pfn) {unsigned long page_pfn =page_to_pfn (page);
@@ -115,6 +131,38 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw)return pfn_is_match (pvmw->page, pfn); } +/* 0: not mapped, 1: pmd_page, 2: pmd */ +static int check_pmd (struct page_vma_mapped_walk *pvmw) +{ +unsigned long pfn; + +if (likely (pmd_trans_huge (*pvmw->pmd))) { +if (pvmw->flags &PVMW_MIGRATION ) +return 0 ; + pfn =pmd_pfn (*pvmw->pmd); +if (!pfn_is_match (pvmw->page, pfn)) +return 0 ; +return 1 ; + }else if (!pmd_present (*pvmw->pmd)) { +if (thp_migration_supported ()) { +if (!(pvmw->flags &PVMW_MIGRATION )) +return 0 ; +if (is_migration_entry (pmd_to_swp_entry (*pvmw->pmd))) { +swp_entry_t entry =pmd_to_swp_entry (*pvmw->pmd); + + pfn =migration_entry_to_pfn (entry); +if (!pfn_is_match (pvmw->page, pfn)) +return 0 ; +return 1 ; + } + } +return 0 ; + } +/* THP pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; +return 2 ; +}/** * page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at * @pvmw->address
@@ -146,14 +194,14 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)pgd_t *pgd;p4d_t *p4d;pud_t pude; -pmd_t pmde; +int pmd_res;if (!pvmw->pte && !pvmw->pmd && pvmw->pud)return not_found (pvmw);/* The only possible pmd mapping has been handled on last iteration */ if (pvmw->pmd && !pvmw->pte) -return not_found (pvmw); + goto next_pmd;if (pvmw->pte) goto next_pte;
@@ -201,43 +249,43 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) }else if (!pud_present (pude))return false; - pvmw->pmd =pmd_offset (pvmw->pud, pvmw->address); -/* -* Make sure the pmd value isn't cached in a register by the -* compiler and used as a stale value after we've observed a -* subsequent update. -*/ - pmde =READ_ONCE (*pvmw->pmd); -if (pmd_trans_huge (pmde) ||is_pmd_migration_entry (pmde)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -if (likely (pmd_trans_huge (*pvmw->pmd))) { -if (pvmw->flags &PVMW_MIGRATION ) -return not_found (pvmw); -if (pmd_page (*pvmw->pmd) != page) -return not_found (pvmw); +if (!map_pmd (pvmw)) + goto next_pmd; +/* pmd locked after map_pmd */ +while (1 ) { + pmd_res =check_pmd (pvmw); +if (pmd_res ==1 )/* pmd_page */ return true; - }else if (!pmd_present (*pvmw->pmd)) { -if (thp_migration_supported ()) { -if (!(pvmw->flags &PVMW_MIGRATION )) -return not_found (pvmw); -if (is_migration_entry (pmd_to_swp_entry (*pvmw->pmd))) { -swp_entry_t entry =pmd_to_swp_entry (*pvmw->pmd); - -if (migration_entry_to_page (entry) != page) -return not_found (pvmw); -return true; +else if (pmd_res ==2 )/* pmd entry */ + goto pte_level; +next_pmd: +/* Only PMD-mapped PUD THP has next pmd */ +if (!(PageTransHuge (pvmw->page) &&compound_order (pvmw->page) ==HPAGE_PUD_ORDER )) +return not_found (pvmw); +do { + pvmw->address +=HPAGE_PMD_SIZE ; +if (pvmw->address >= pvmw->vma->vm_end || + pvmw->address >= +__vma_address (pvmw->page, pvmw->vma) + +thp_nr_pages (pvmw->page) *PAGE_SIZE ) +return not_found (pvmw); +/* Did we cross page table boundary? */ +if (pvmw->address %PUD_SIZE ==0 ) { +if (pvmw->ptl) { +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; } + goto restart; + }else { + pvmw->pmd++; } -return not_found (pvmw); - }else { -/* THP pmd was split under us: handle on pte level */ -spin_unlock (pvmw->ptl); - pvmw->ptl =NULL ; - } - }else if (!pmd_present (pmde)) { -return false; + }while (pmd_none (*pvmw->pmd)); + +if (!pvmw->ptl) + pvmw->ptl =pmd_lock (mm, pvmw->pmd); } +pte_level:if (!map_pte (pvmw)) goto next_pte;while (1 ) {
Signature · 1 line
2.28.0
RFC PATCH 9/16 mm: thp: 1GB THP support in try_to_unmap().
In reply to Zi YanFrom: Zi Yan <[email protected]>
Unmap different subpages in different sized THPs properly in the try_to_unmap() function.
--- mm/migrate.c | 2 +- mm/rmap.c | 159 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 116 insertions(+), 45 deletions(-)
mm/migrate.c +1 -1
@@ -225,7 +225,7 @@ static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION /* PMD-mapped THP migration entry */ -if (!pvmw.pte) { +if (!pvmw.pte && pvmw.pmd) {VM_BUG_ON_PAGE (PageHuge (page) || !PageTransCompound (page), page);remove_migration_pmd (&pvmw, new);continue ;
mm/rmap.c +115 -44
249 lines not on this page · raw message
Signature · 1 line
2.28.0
RFC PATCH 10/16 mm: thp: split 1GB THPs at page reclaim.
In reply to Zi YanFrom: Zi Yan <[email protected]>
We cannot swap 1GB THPs, so split them before swap them out.
--- mm/swap_slots.c | 2 ++ mm/vmscan.c | 58 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 14 deletions(-)
mm/swap_slots.c +2 -0
@@ -312,6 +312,8 @@ swp_entry_t get_swap_page(struct page *page) entry.val =0 ;if (PageTransHuge (page)) { +if (compound_order (page) ==HPAGE_PUD_ORDER ) +return entry;if (IS_ENABLED (CONFIG_THP_SWAP ))get_swap_pages (1 , &entry,HPAGE_PMD_NR ); goto out;
mm/vmscan.c +44 -14
82 lines not on this page · raw message
Signature · 1 line
2.28.0
RFC PATCH 11/16 mm: thp: 1GB THP follow_p*d_page() support.
In reply to Zi YanFrom: Zi Yan <[email protected]>
Add follow_page support for 1GB THPs.
--- include/linux/huge_mm.h | 11 +++++++ mm/gup.c | 60 ++++++++++++++++++++++++++++++++- mm/huge_memory.c | 73 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 142 insertions(+), 2 deletions(-)
include/linux/huge_mm.h +11 -0
@@ -20,6 +20,10 @@ extern int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,extern void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud);extern int do_huge_pud_anonymous_page (struct vm_fault *vmf);extern vm_fault_t do_huge_pud_wp_page (struct vm_fault *vmf,pud_t orig_pud); +extern struct page *follow_trans_huge_pud (struct vm_area_struct *vma, +unsigned long addr, +pud_t *pud, +unsigned int flags);#else static inline void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) {
@@ -32,6 +36,13 @@ extern vm_fault_t do_huge_pud_wp_page(struct vm_fault *vmf, pud_t orig_pud) {return VM_FAULT_FALLBACK ; } +struct page *follow_trans_huge_pud (struct vm_area_struct *vma, +unsigned long addr, +pud_t *pud, +unsigned int flags) +{ +return NULL ; +}#endif extern vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf,pmd_t orig_pmd);
mm/gup.c +59 -1
69 lines not on this page · raw message
mm/huge_memory.c +72 -1
85 lines not on this page · raw message
Signature · 1 line
2.28.0
RFC PATCH 12/16 mm: support 1GB THP pagemap support.
In reply to Zi YanFrom: Zi Yan <[email protected]>
Print page flags properly.
--- fs/proc/task_mmu.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
fs/proc/task_mmu.c +59 -0
@@ -1557,6 +1557,64 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,return err; } +static int pagemap_pud_range (pud_t *pudp,unsigned long addr,unsigned long end, +struct mm_walk *walk) +{ +struct vm_area_struct *vma = walk->vma; +struct pagemapread *pm = walk->private; +spinlock_t *ptl; +int err =0 ; + +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD + ptl =pud_trans_huge_lock (pudp, vma); +if (ptl) { +u64 flags =0 , frame =0 ; +pud_t pud = *pudp; +struct page *page =NULL ; + +if (vma->vm_flags &VM_SOFTDIRTY ) + flags |=PM_SOFT_DIRTY ; + +if (pud_present (pud)) { + page =pud_page (pud); + + flags |=PM_PRESENT ; +if (pud_soft_dirty (pud)) + flags |=PM_SOFT_DIRTY ; +if (pm->show_pfn) + frame =pud_pfn (pud) + + ((addr & ~PUD_MASK ) >>PAGE_SHIFT ); + } + +if (page &&page_mapcount (page) ==1 ) + flags |=PM_MMAP_EXCLUSIVE ; + +for (; addr != end; addr +=PAGE_SIZE ) { +pagemap_entry_t pme =make_pme (frame, flags); + + err =add_to_pagemap (addr, &pme, pm); +if (err) +break ; +if (pm->show_pfn) { +if (flags &PM_PRESENT ) + frame++; +else if (flags &PM_SWAP ) + frame += (1 <<MAX_SWAPFILES_SHIFT ); + } + } +spin_unlock (ptl); + walk->action =ACTION_CONTINUE ; +return err; + } + +if (pud_trans_unstable (pudp)) { + walk->action =ACTION_AGAIN ; +return 0 ; + } +#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ +return err; +} +#ifdef CONFIG_HUGETLB_PAGE /* This function walks within one hugetlb entry in the single call */ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
@@ -1607,6 +1665,7 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,#endif /* HUGETLB_PAGE */ static const struct mm_walk_ops pagemap_ops = { + .pud_entry = pagemap_pud_range, .pmd_entry = pagemap_pmd_range, .pte_hole = pagemap_pte_hole, .hugetlb_entry = pagemap_hugetlb_range,
Signature · 1 line
2.28.0
RFC PATCH 13/16 mm: thp: add a knob to enable/disable 1GB THPs.
In reply to Zi YanFrom: Zi Yan <[email protected]>
It does not affect existing 1GB THPs. It is similar to the knob for 2MB THPs.
--- include/linux/huge_mm.h | 14 ++++++++++++++ mm/huge_memory.c | 40 ++++++++++++++++++++++++++++++++++++++++ mm/memory.c | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-)
include/linux/huge_mm.h +14 -0
@@ -119,6 +119,8 @@ enum transparent_hugepage_flag {#ifdef CONFIG_DEBUG_VM TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG , #endif +TRANSPARENT_PUD_HUGEPAGE_FLAG , + TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG, };struct kobject ;
@@ -184,6 +186,18 @@ static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma) }bool transparent_hugepage_enabled (struct vm_area_struct *vma); +static inline bool transparent_pud_hugepage_enabled (struct vm_area_struct *vma) +{ +if (transparent_hugepage_enabled (vma)) { +if (transparent_hugepage_flags & (1 <<TRANSPARENT_PUD_HUGEPAGE_FLAG )) +return true; +if (transparent_hugepage_flags & + (1 <<TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG )) +return !!(vma->vm_flags &VM_HUGEPAGE ); + } + +return false; +}#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
mm/huge_memory.c +40 -0
@@ -49,9 +49,11 @@unsigned long transparent_hugepage_flags__read_mostly = #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS (1 <<TRANSPARENT_HUGEPAGE_FLAG )| + (1 <<TRANSPARENT_PUD_HUGEPAGE_FLAG )| #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE (1 <<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG )| + (1 <<TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG )| #endif (1 <<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG )| (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
@@ -199,6 +201,43 @@ static ssize_t enabled_store(struct kobject *kobj,static struct kobj_attribute enabled_attr =__ATTR (enabled,0644 , enabled_show, enabled_store); +static ssize_t enabled_1gb_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +if (test_bit (TRANSPARENT_PUD_HUGEPAGE_FLAG , &transparent_hugepage_flags)) +return sprintf (buf,"[always] madvise never\n" ); +else if (test_bit (TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags)) +return sprintf (buf,"always [madvise] never\n" ); +else +return sprintf (buf,"always madvise [never]\n" ); +} + +static ssize_t enabled_1gb_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +ssize_t ret = count; + +if (!memcmp ("always" , buf, +min (sizeof ("always" )-1 , count))) { +clear_bit (TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags); +set_bit (TRANSPARENT_PUD_HUGEPAGE_FLAG , &transparent_hugepage_flags); + }else if (!memcmp ("madvise" , buf, +min (sizeof ("madvise" )-1 , count))) { +clear_bit (TRANSPARENT_PUD_HUGEPAGE_FLAG , &transparent_hugepage_flags); +set_bit (TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags); + }else if (!memcmp ("never" , buf, +min (sizeof ("never" )-1 , count))) { +clear_bit (TRANSPARENT_PUD_HUGEPAGE_FLAG , &transparent_hugepage_flags); +clear_bit (TRANSPARENT_PUD_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags); + }else + ret = -EINVAL ; + +return ret; +} +static struct kobj_attribute enabled_1gb_attr = +__ATTR (enabled_1gb,0644 , enabled_1gb_show, enabled_1gb_store); +ssize_t single_hugepage_flag_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf,enum transparent_hugepage_flag flag)
@@ -305,6 +344,7 @@ static struct kobj_attribute hpage_pmd_size_attr =static struct attribute *hugepage_attr[] = { &enabled_attr.attr, + &enabled_1gb_attr.attr, &defrag_attr.attr, &use_zero_page_attr.attr, &hpage_pmd_size_attr.attr,
mm/memory.c +1 -1
@@ -4305,7 +4305,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,if (!vmf.pud)return VM_FAULT_OOM ; retry_pud: -if (pud_none (*vmf.pud) &&__transparent_hugepage_enabled (vma)) { +if (pud_none (*vmf.pud) &&transparent_pud_hugepage_enabled (vma)) { ret =create_huge_pud (&vmf);if (!(ret &VM_FAULT_FALLBACK ))return ret;
Signature · 1 line
2.28.0
RFC PATCH 14/16 mm: page_alloc: >=MAX_ORDER pages allocation an deallocation.
In reply to Zi YanFrom: Zi Yan <[email protected]>
Use alloc_contig_pages for allocation and destroy_compound_gigantic_page for deallocation, so 1GB THP can be created and destroyed without changing MAX_ORDER.
--- mm/hugetlb.c | 22 ---------------------- mm/internal.h | 2 ++ mm/mempolicy.c | 15 ++++++++++++++- mm/page_alloc.c | 33 ++++++++++++++++++++++++++++----- 4 files changed, 44 insertions(+), 28 deletions(-)
mm/hugetlb.c +0 -22
@@ -1211,26 +1211,6 @@ static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) nr_nodes--)#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE -static void destroy_compound_gigantic_page (struct page *page, -unsigned int order) -{ -int i; -int nr_pages =1 << order; -struct page *p = page +1 ; - -atomic_set (compound_mapcount_ptr (page),0 ); -if (hpage_pincount_available (page)) -atomic_set (compound_pincount_ptr (page),0 ); - -for (i =1 ; i < nr_pages; i++, p =mem_map_next (p, page, i)) { -clear_compound_head (p); -set_page_refcounted (p); - } - -set_compound_order (page,0 ); -__ClearPageHead (page); -} -static void free_gigantic_page (struct page *page,unsigned int order) {/*
@@ -1288,8 +1268,6 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask, return NULL; }static inline void free_gigantic_page (struct page *page,unsigned int order) { } -static inline void destroy_compound_gigantic_page (struct page *page, -unsigned int order) { }#endif static void update_and_free_page (struct hstate *h,struct page *page)
mm/internal.h +2 -0
@@ -620,4 +620,6 @@ struct migration_target_control { gfp_t gfp_mask; }; +void destroy_compound_gigantic_page (struct page *page, +unsigned int order);#endif /* __MM_INTERNAL_H */
mm/mempolicy.c +14 -1
@@ -2138,7 +2138,12 @@ static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, {struct page *page; - page =__alloc_pages (gfp, order, nid); +if (order >MAX_ORDER ) { + page =alloc_contig_pages (1UL <<order, gfp, nid,NULL ); +if (page && (gfp & __GFP_COMP)) +prep_compound_page (page, order); + }else + page =__alloc_pages (gfp, order, nid);/* skip NUMA_INTERLEAVE_HIT counter update if numa stats is disabled */ if (!static_branch_likely (&vm_numa_stat_key))return page;
@@ -2212,6 +2217,14 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, nmask =policy_nodemask (gfp, pol);if (!nmask ||node_isset (hpage_node, *nmask)) {mpol_cond_put (pol); + +if (order >MAX_ORDER ) { + page =alloc_contig_pages (1UL <<order, gfp, + hpage_node,NULL ); +if (page && (gfp & __GFP_COMP)) +prep_compound_page (page, order); + goto out; + }/* * First, try to allocate THP only on local node, but * don't reclaim unnecessarily, just compact.
mm/page_alloc.c +28 -5
@@ -1480,6 +1480,24 @@ void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end) } } +void destroy_compound_gigantic_page (struct page *page, +unsigned int order) +{ +int i; +int nr_pages =1 << order; +struct page *p = page +1 ; + +atomic_set (compound_mapcount_ptr (page),0 ); +for (i =1 ; i < nr_pages; i++, p =mem_map_next (p, page, i)) { +clear_compound_head (p); +set_page_refcounted (p); + } + +set_compound_order (page,0 ); +__ClearPageHead (page); +set_page_refcounted (page); +} +static void __free_pages_ok (struct page *page,unsigned int order) {unsigned long flags;
@@ -1489,11 +1507,16 @@ static void __free_pages_ok(struct page *page, unsigned int order)if (!free_pages_prepare (page, order, true))return ; - migratetype =get_pfnblock_migratetype (page, pfn); -local_irq_save (flags); -__count_vm_events (PGFREE ,1 << order); -free_one_page (page_zone (page), page, pfn, order, migratetype); -local_irq_restore (flags); +if (order >=MAX_ORDER ) { +destroy_compound_gigantic_page (page, order); +free_contig_range (page_to_pfn (page),1 << order); + }else { + migratetype =get_pfnblock_migratetype (page, pfn); +local_irq_save (flags); +__count_vm_events (PGFREE ,1 << order); +free_one_page (page_zone (page), page, pfn, order, migratetype); +local_irq_restore (flags); + } }void __free_pages_core (struct page *page,unsigned int order)
Signature · 1 line
2.28.0
RFC PATCH 15/16 hugetlb: cma: move cma reserve function to cma.c.
In reply to Zi YanFrom: Zi Yan <[email protected]>
It will be used by other allocations, like 1GB THP allocation in the upcoming commit.
--- .../admin-guide/kernel-parameters.txt | 2 +- arch/arm64/mm/hugetlbpage.c | 2 +- arch/powerpc/mm/hugetlbpage.c | 2 +- arch/x86/kernel/setup.c | 8 +- include/linux/cma.h | 15 ++++ include/linux/hugetlb.h | 12 --- mm/cma.c | 88 +++++++++++++++++++ mm/hugetlb.c | 88 ++----------------- 8 files changed, 118 insertions(+), 99 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +1 -1
@@ -1507,7 +1507,7 @@ hpet_mmap= [X86, HPET_MMAP] Allow userspace to mmap HPET registers. Default set by CONFIG_HPET_MMAP_DEFAULT. - hugetlb_cma= [HW] The size of a cma area used for allocation + hugepage_cma= [HW] The size of a cma area used for allocation of gigantic hugepages. Format: nn[KMGTPE]
arch/arm64/mm/hugetlbpage.c +1 -1
@@ -52,7 +52,7 @@ void __init arm64_hugetlb_cma_reserve(void)* breaking this assumption. */ WARN_ON (order <=MAX_ORDER ); - hugetlb_cma_reserve(order); + hugepage_cma_reserve(order); }#endif /* CONFIG_CMA */
arch/powerpc/mm/hugetlbpage.c +1 -1
@@ -699,6 +699,6 @@ void __init gigantic_hugetlb_cma_reserve(void)if (order) {VM_WARN_ON (order <MAX_ORDER ); -hugetlb_cma_reserve (order); +hugepage_cma_reserve (order); } }
arch/x86/kernel/setup.c +4 -4
@@ -16,7 +16,7 @@#include <linux/pci.h> #include <linux/root_dev.h> #include <linux/sfi.h> -#include <linux/hugetlb.h> +#include <linux/cma.h> #include <linux/tboot.h> #include <linux/usb/xhci-dbgp.h>
@@ -640,7 +640,7 @@ static void __init trim_snb_memory(void)* already been reserved. */ memblock_reserve (0 ,1 <<20 ); - +for (i =0 ; i <ARRAY_SIZE (bad_pages); i++) {if (memblock_reserve (bad_pages[i],PAGE_SIZE )) printk(KERN_WARNING "failed to reserve 0x%08lx\n",
@@ -732,7 +732,7 @@ static void __init trim_low_memory_range(void) {memblock_reserve (0 ,ALIGN (reserve_low,PAGE_SIZE )); } - +/* * Dump out kernel offset information on panic. */
@@ -1142,7 +1142,7 @@ void __init setup_arch(char **cmdline_p)dma_contiguous_reserve (max_pfn_mapped <<PAGE_SHIFT );if (boot_cpu_has (X86_FEATURE_GBPAGES )) -hugetlb_cma_reserve (PUD_SHIFT -PAGE_SHIFT ); +hugepage_cma_reserve (PUD_SHIFT -PAGE_SHIFT );/* * Reserve memory for crash kernel after SRAT is parsed so that it
include/linux/cma.h +15 -0
@@ -47,4 +47,19 @@ extern struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,extern bool cma_release (struct cma *cma,const struct page *pages,unsigned int count);extern int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data); + +extern void cma_reserve (int min_order,unsigned long requested_size, +const char *name,struct cma *cma_struct[N_MEMORY ]); +#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) || defined(CONFIG_HUGETLBFS ) +extern void __init hugepage_cma_reserve (int order); +extern void __init hugepage_cma_check (void ); +#else +static inline void __init hugepage_cma_check (void ) +{ +} +static inline void __init hugepage_cma_reserve (int order) +{ +} +#endif +#endif
include/linux/hugetlb.h +0 -12
@@ -935,16 +935,4 @@ static inline spinlock_t *huge_pte_lock(struct hstate *h, return ptl; } -#if defined(CONFIG_HUGETLB_PAGE ) && defined(CONFIG_CMA ) -extern void __init hugetlb_cma_reserve (int order); -extern void __init hugetlb_cma_check (void ); -#else -static inline __init void hugetlb_cma_reserve (int order) -{ -} -static inline __init void hugetlb_cma_check (void ) -{ -} -#endif -#endif /* _LINUX_HUGETLB_H */
mm/cma.c +88 -0
97 lines not on this page · raw message
mm/hugetlb.c +8 -80
124 lines not on this page · raw message
Signature · 1 line
2.28.0
RFC PATCH 16/16 mm: thp: use cma reservation for pud thp allocation.
In reply to Zi YanFrom: Zi Yan <[email protected]>
Sharing hugepage_cma reservation with hugetlb for pud thp allocaiton. The reserved cma regions still can be used for moveable page allocations.
During 1GB page split, all subpages are cleared from the CMA bitmap, since they are no more 1GB pages and will be freed via the normal path instead of cma_release().
--- include/linux/cma.h | 3 +++ include/linux/huge_mm.h | 10 ++++++++++ mm/cma.c | 31 +++++++++++++++++++++++++++++++ mm/huge_memory.c | 30 ++++++++++++++++++++++++++++++ mm/mempolicy.c | 12 +++++++++--- mm/page_alloc.c | 3 ++- 6 files changed, 85 insertions(+), 4 deletions(-)
include/linux/cma.h +3 -0
@@ -46,6 +46,9 @@ extern struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, bool no_warn);extern bool cma_release (struct cma *cma,const struct page *pages,unsigned int count); +extern bool cma_clear_bitmap_if_in_range (struct cma *cma,const struct page *page, +unsigned int count); +extern int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data); extern void cma_reserve(int min_order, unsigned long requested_size,
include/linux/huge_mm.h +10 -0
@@ -24,6 +24,8 @@ extern struct page *follow_trans_huge_pud(struct vm_area_struct *vma,unsigned long addr, pud_t *pud, unsigned int flags); +extern struct page *alloc_thp_pud_page (int nid); +extern bool free_thp_pud_page (struct page *page,int order);#else static inline void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) {
@@ -43,6 +45,14 @@ struct page *follow_trans_huge_pud(struct vm_area_struct *vma, {return NULL ; } +struct page *alloc_thp_pud_page (int nid) +{ +return NULL ; +} +extern bool free_thp_pud_page (struct page *page,int order); +{ +return false; +}#endif extern vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf,pmd_t orig_pmd);
mm/cma.c +31 -0
@@ -532,6 +532,37 @@ bool cma_release(struct cma *cma, const struct page *pages, unsigned int count)return true; } +/** +* cma_clear_bitmap_if_in_range() - clear bitmap for a given page +* @cma: Contiguous memory region for which the allocation is performed. +* @pages: Allocated pages. +* @count: Number of allocated pages. +* +* This function clears bitmap of memory allocated by cma_alloc(). +* It returns false when provided pages do not belong to contiguous area and +* true otherwise. +*/ +bool cma_clear_bitmap_if_in_range (struct cma *cma,const struct page *pages, +unsigned int count) +{ +unsigned long pfn; + +if (!cma || !pages) +return false; + + pfn =page_to_pfn (pages); + +if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) +return false; + +if (pfn + count > cma->base_pfn + cma->count) +return false; + +cma_clear_bitmap (cma, pfn, count); + +return true; +} +int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data) {int i;
mm/huge_memory.c +30 -0
@@ -33,6 +33,7 @@#include <linux/oom.h> #include <linux/numa.h> #include <linux/page_owner.h> +#include <linux/cma.h> #include <asm/tlb.h> #include <asm/pgalloc.h>
@@ -64,6 +65,10 @@ static struct shrinker deferred_split_shrinker;static atomic_t huge_zero_refcount;struct page *huge_zero_page__read_mostly ; +#ifdef CONFIG_CMA +extern struct cma *hugepage_cma[MAX_NUMNODES ]; +#endif +bool transparent_hugepage_enabled (struct vm_area_struct *vma) {/* The addr is used to check if the vma size fits */
@@ -2526,6 +2531,13 @@ static void __split_huge_pud_page(struct page *page, struct list_head *list,/* no file-back page support yet */ VM_BUG_ON (!PageAnon (page)); +/* */ +if (IS_ENABLED (CONFIG_CMA )) { +struct cma *cma = hugepage_cma[page_to_nid (head)]; +VM_BUG_ON (!cma_clear_bitmap_if_in_range (cma, head, +thp_nr_pages (head))); + } +for (i =HPAGE_PUD_NR -HPAGE_PMD_NR ; i >=1 ; i -=HPAGE_PMD_NR ) {__split_huge_pud_page_tail (head, i, lruvec, list); }
@@ -3753,3 +3765,21 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) update_mmu_cache_pmd(vma, address, pvmw->pmd); }#endif + +struct page *alloc_thp_pud_page (int nid) +{ +struct page *page =NULL ; +#ifdef CONFIG_CMA + page =cma_alloc (hugepage_cma[nid],HPAGE_PUD_NR ,HPAGE_PUD_ORDER , true); +#endif +return page; +} + +bool free_thp_pud_page (struct page *page,int order) +{ +bool ret = false; +#ifdef CONFIG_CMA + ret =cma_release (hugepage_cma[page_to_nid (page)], page,1 <<order); +#endif +return ret; +}
mm/mempolicy.c +9 -3
@@ -2139,7 +2139,10 @@ static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,struct page *page;if (order >MAX_ORDER ) { - page =alloc_contig_pages (1UL <<order, gfp, nid,NULL ); +if (order ==HPAGE_PUD_ORDER ) + page =alloc_thp_pud_page (nid); +if (!page) + page =alloc_contig_pages (1UL <<order, gfp, nid,NULL );if (page && (gfp & __GFP_COMP))prep_compound_page (page, order); }else
@@ -2219,8 +2222,11 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,mpol_cond_put (pol);if (order >MAX_ORDER ) { - page =alloc_contig_pages (1UL <<order, gfp, - hpage_node,NULL ); +if (order ==HPAGE_PUD_ORDER ) + page =alloc_thp_pud_page (hpage_node); +if (!page) + page =alloc_contig_pages (1UL <<order, + gfp, hpage_node,NULL );if (page && (gfp & __GFP_COMP))prep_compound_page (page, order); goto out;
mm/page_alloc.c +2 -1
@@ -1509,7 +1509,8 @@ static void __free_pages_ok(struct page *page, unsigned int order)if (order >=MAX_ORDER ) {destroy_compound_gigantic_page (page, order); -free_contig_range (page_to_pfn (page),1 << order); +if (!free_thp_pud_page (page, order)) +free_contig_range (page_to_pfn (page),1 << order); }else { migratetype =get_pfnblock_migratetype (page, pfn);local_irq_save (flags);
Signature · 1 line
2.28.0
Quoted 64 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Design
=======1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited. A new pagechain data structure is added to enable multi-page deposit.2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and
sub_compound_mapcountare introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP andsub_compound_mapcountcounts the PMD mapping by using page[N*512 + 3].compound_mapcount.3. Using CMA allocaiton for 1GB THP: instead of bump
MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back toalloc_contig_pagesfor 1GB THP if necessary.Patch Organization
=======Patch 01 adds the new pagechain data structure.
Patch 02 to 13 adds 1GB THP support in variable places.
Patch 14 tries to use
alloc_contig_pagesfor 1GB THP allocaiton.Patch 15 moves
hugetlb_cmareservation tocma.cand rename it tohugepage_cma.Patch 16 use
hugepage_cmareservation for 1GB THP allocation.Any suggestions and comments are welcome.
Zi Yan (16):
mm: add pagechain container for storing multiple pages.
mm: thp: 1GB anonymous page implementation.
mm: proc: add 1GB THP kpageflag.
mm: thp: 1GB THP copy on write implementation.
mm: thp: handling 1GB THP reference bit.
mm: thp: add 1GB THPsplit_huge_pud_page()function.
mm: stats: make smap stats understand PUD THPs.
mm:page_vma_walk: teach it about PMD-mapped PUD THP.
Quoted 8 lines
mm: thp: 1GB THP support in
try_to_unmap().
mm: thp: split 1GB THPs at page reclaim.
mm: thp: 1GB THP follow_p*d_page() support.
mm: support 1GB THP pagemap support.
mm: thp: add a knob to enable/disable 1GB THPs.
mm:page_alloc: >=MAX_ORDER pages allocation an deallocation.
hugetlb: cma: move cma reserve function tocma.c.
mm: thp: use cma reservation for pud thp allocation.
Surprised this doesn't touch mm/pagewalk.c ?
Jason
Zi Yan
reviewerQuoted 74 lines · Jason Gunthorpe
Quoted 64 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Design
=======1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited. A new pagechain data structure is added to enable multi-page deposit.2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and
sub_compound_mapcountare introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP andsub_compound_mapcountcounts the PMD mapping by using page[N*512 + 3].compound_mapcount.3. Using CMA allocaiton for 1GB THP: instead of bump
MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back toalloc_contig_pagesfor 1GB THP if necessary.Patch Organization
=======Patch 01 adds the new pagechain data structure.
Patch 02 to 13 adds 1GB THP support in variable places.
Patch 14 tries to use
alloc_contig_pagesfor 1GB THP allocaiton.Patch 15 moves
hugetlb_cmareservation tocma.cand rename it tohugepage_cma.Patch 16 use
hugepage_cmareservation for 1GB THP allocation.Any suggestions and comments are welcome.
Zi Yan (16):
mm: add pagechain container for storing multiple pages.
mm: thp: 1GB anonymous page implementation.
mm: proc: add 1GB THP kpageflag.
mm: thp: 1GB THP copy on write implementation.
mm: thp: handling 1GB THP reference bit.
mm: thp: add 1GB THPsplit_huge_pud_page()function.
mm: stats: make smap stats understand PUD THPs.
mm:page_vma_walk: teach it about PMD-mapped PUD THP.Quoted 8 lines
mm: thp: 1GB THP support in
try_to_unmap().
mm: thp: split 1GB THPs at page reclaim.
mm: thp: 1GB THP follow_p*d_page() support.
mm: support 1GB THP pagemap support.
mm: thp: add a knob to enable/disable 1GB THPs.
mm:page_alloc: >=MAX_ORDER pages allocation an deallocation.
hugetlb: cma: move cma reserve function tocma.c.
mm: thp: use cma reservation for pud thp allocation.
Quoted 1 line
Surprised this doesn't touch
mm/pagewalk.c?
1GB PUD page support is present for DAX purpose, so the code is there in mm/pagewalk.c already. I only needed to supply ops->pud_entry when using the functions in mm/pagewalk.c. :)
—
Best Regards,
Yan Zi
Quoted 5 lines · Zi Yan
Quoted 1 line
Surprised this doesn't touch
mm/pagewalk.c?1GB PUD page support is present for DAX purpose, so the code is there in
mm/pagewalk.calready. I only needed to supplyops->pud_entrywhen using the functions inmm/pagewalk.c. :)
Yes, but doesn't this change what is possible under the mmap_sem without the page table locks?
ie I would expect some thing like pmd_trans_unstable() to be required as well for lockless walkers. (and I don't think the pmd code is 100% right either)
Jason
Zi Yan
reviewerQuoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Zi Yan
Quoted 1 line
Surprised this doesn't touch
mm/pagewalk.c?1GB PUD page support is present for DAX purpose, so the code is there in
mm/pagewalk.calready. I only needed to supplyops->pud_entrywhen using the functions inmm/pagewalk.c. :)
Quoted 7 lines
Yes, but doesn't this change what is possible under the
mmap_semwithout the page table locks?ie I would expect some thing like
pmd_trans_unstable()to be required as well for lockless walkers. (and I don't think the pmd code is 100% right either)
Right. I missed that. Thanks for pointing it out.
The code like this, right?
mm/pagewalk.c +4 -3
@@ -152,10 +152,11 @@ static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, !(ops->pmd_entry || ops->pte_entry))continue ; -if (walk->vma) +if (walk->vma) {split_huge_pud (walk->vma, pud, addr); -if (pud_none (*pud)) - goto again; +if (pud_trans_unstable (pud)) + goto again; + } err =walk_pmd_range (pud, addr, next, walk);if (err)
—
Best Regards,
Yan Zi
Quoted 13 lines · Zi Yan
Quoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Zi Yan
Quoted 1 line
Surprised this doesn't touch
mm/pagewalk.c?1GB PUD page support is present for DAX purpose, so the code is there in
mm/pagewalk.calready. I only needed to supplyops->pud_entrywhen using the functions inmm/pagewalk.c. :)Yes, but doesn't this change what is possible under the
mmap_semwithout the page table locks?
Quoted 7 lines
Quoted 4 lines
ie I would expect some thing like
pmd_trans_unstable()to be required as well for lockless walkers. (and I don't think the pmd code is 100% right either)Right. I missed that. Thanks for pointing it out.
The code like this, right?
Technically all those *pud's are racy too, the design here with the _unstable function call always seemed weird. I strongly suspect it should mirror how get_user_pages_fast works for lockless walking
Jason
Zi Yan
reviewerQuoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Zi Yan
Quoted 15 lines · Jason Gunthorpe
Quoted 5 lines · Zi Yan
Quoted 1 line
Surprised this doesn't touch
mm/pagewalk.c?1GB PUD page support is present for DAX purpose, so the code is there in
mm/pagewalk.calready. I only needed to supplyops->pud_entrywhen using the functions inmm/pagewalk.c. :)Yes, but doesn't this change what is possible under the
mmap_semwithout the page table locks?ie I would expect some thing like
pmd_trans_unstable()to be required as well for lockless walkers. (and I don't think the pmd code is 100% right either)
Quoted 6 lines
Quoted 2 lines
Right. I missed that. Thanks for pointing it out.
The code like this, right?Technically all those *pud's are racy too, the design here with the
_unstablefunction call always seemed weird. I strongly suspect it should mirror howget_user_pages_fastworks for lockless walking
You mean READ_ONCE on page table entry pointer first, then use the value for the rest of the loop? I am not quite familiar with this racy check part of the code and happy to hear more about it.
—
Best Regards,
Yan Zi
Jason Gunthorpe
maintainerQuoted 24 lines · Zi Yan
Quoted 22 lines · Jason Gunthorpe
Quoted 20 lines · Zi Yan
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Zi Yan
Quoted 1 line
Surprised this doesn't touch
mm/pagewalk.c?1GB PUD page support is present for DAX purpose, so the code is there in
mm/pagewalk.calready. I only needed to supplyops->pud_entrywhen using the functions inmm/pagewalk.c. :)Quoted 7 lines
Yes, but doesn't this change what is possible under the
mmap_semwithout the page table locks?ie I would expect some thing like
pmd_trans_unstable()to be required as well for lockless walkers. (and I don't think the pmd code is 100% right either)Right. I missed that. Thanks for pointing it out.
The code like this, right?
Quoted 7 lines
Quoted 3 lines
Technically all those *pud's are racy too, the design here with the
_unstablefunction call always seemed weird. I strongly suspect it should mirror howget_user_pages_fastworks for lockless walkingYou mean
READ_ONCEon page table entry pointer first, then use the value for the rest of the loop? I am not quite familiar with this racy check part of the code and happy to hear more about it.
There are two main issues with the THPs and lockless walks
- The *pXX value can change at any time, as THPs can be split at any moment. However, once observed to be a sub page table pointer the value is fixed under the read side of the mmap (I think, I never did find the code path supporting this, but everything is busted if it isn't true...)
- Reading the *pXX without load tearing is difficult on 32 bit arches
So if you do READ_ONCE() it defeats the first problem.
However if the sizeof(*pXX) is 8 on a 32 bit platform then load tearing is a problem. At lest the various pXX_*() test functions operate on a single 32 bit word so don't tear, but to to convert the *pXX to a lower level page table pointer a coherent, untorn, read is required.
So, looking again, I remember now, I could never quite figure out why gup_pmd_range() was safe to do:
pmd_t pmd = READ_ONCE(*pmdp);
[..]
} else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
[..]
ptem = ptep = pte_offset_map(&pmd, addr);As I don't see what prevents load tearing a 64 bit pmd.. Eg no pmd_trans_unstable() or equivalent here.
But we see gup_get_pte() using an anti-load tearing technique..
Jason
Quoted 9 lines · Jason Gunthorpe
However if the
sizeof(*pXX)is 8 on a 32 bit platform then load tearing is a problem. At lest the various pXX_*() test functions operate on a single 32 bit word so don't tear, but to to convert the *pXX to a lower level page table pointer a coherent, untorn, read is required.So, looking again, I remember now, I could never quite figure out why
gup_pmd_range()was safe to do:
Quoted 8 lines
pmd_t pmd = READ_ONCE(*pmdp); [..] } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) [..] ptem = ptep = pte_offset_map(&pmd, addr);As I don't see what prevents load tearing a 64 bit pmd.. Eg no
pmd_trans_unstable()or equivalent here.
I don't think there are any 32-bit page tables which support a PUD-sized page. Pretty sure x86 doesn't until you get to 4- or 5- level page tables (which need you to be running in 64-bit mode). There's not much utility in having 1GB of your 3GB process address space taken up by a single page.
I'm OK if there are some oddball architectures which support it, but Linux doesn't.
Jason Gunthorpe
maintainerQuoted 16 lines · Matthew Wilcox
Quoted 15 lines · Jason Gunthorpe
However if the
sizeof(*pXX)is 8 on a 32 bit platform then load tearing is a problem. At lest the various pXX_*() test functions operate on a single 32 bit word so don't tear, but to to convert the *pXX to a lower level page table pointer a coherent, untorn, read is required.So, looking again, I remember now, I could never quite figure out why
gup_pmd_range()was safe to do:pmd_t pmd = READ_ONCE(*pmdp); [..] } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) [..] ptem = ptep = pte_offset_map(&pmd, addr);
Quoted 7 lines
Quoted 2 lines
As I don't see what prevents load tearing a 64 bit pmd.. Eg no
pmd_trans_unstable()or equivalent here.I don't think there are any 32-bit page tables which support a PUD-sized page. Pretty sure x86 doesn't until you get to 4- or 5- level page tables (which need you to be running in 64-bit mode). There's not much utility in having 1GB of your 3GB process address space taken up by a single page.
Make sense for PUD, but why is the above GUP code OK for PMD? pmd_trans_unstable() exists specifically to close read tearing races, so it looks like a real problem?
Quoted 2 lines
I'm OK if there are some oddball architectures which support it, but Linux doesn't.
So, based on that observation, I think something approximately like this is needed for the page walker for PUD: (this has been on my backlog to return to these patches..)
From 00a361ecb2d9e1226600d9e78e6e1803a886f2d6 Mon Sep 17 00:00:00 2001
From: Jason Gunthorpe <[email protected]>
Date: Fri, 13 Mar 2020 13:15:36 -0300
Subject: [RFC] mm/pagewalk: use READ_ONCE when reading the PUD entry
unlocked
The pagewalker runs while only holding the mmap_sem for read. The pud can be set asynchronously, while also holding the mmap_sem for read
eg from:
handle_mm_fault()
__handle_mm_fault()
create_huge_pmd()
dev_dax_huge_fault()
__dev_dax_pud_fault()
vmf_insert_pfn_pud()
insert_pfn_pud()
pud_lock()
set_pud_at()At least x86 sets the PUD using WRITE_ONCE(), so an unlocked read of unstable data should be paired to use READ_ONCE().
For the pagewalker to work locklessly the PUD must work similarly to the
PMD: once the PUD entry becomes a pointer to a PMD, it must be stable, and safe to pass to pmd_offset()
Passing the value from READ_ONCE into the callbacks prevents the callers from seeing inconsistencies after they re-read, such as seeing pud_none().
If a callback does obtain the pud_lock then it should trigger ACTION_AGAIN if a data race caused the original value to change.
Use the same pattern as gup_pmd_range() and pass in the address of the local READ_ONCE stack variable to pmd_offset() to avoid reading it again.
--- include/linux/pagewalk.h | 2 +- mm/hmm.c | 16 +++++++--------- mm/mapping_dirty_helpers.c | 6 ++---- mm/pagewalk.c | 28 ++++++++++++++++------------ mm/ptdump.c | 3 +-- 5 files changed, 27 insertions(+), 28 deletions(-)
include/linux/pagewalk.h +1 -1
@@ -39,7 +39,7 @@ struct mm_walk_ops { unsigned long next, struct mm_walk *walk);int (*p4d_entry)(p4d_t *p4d,unsigned long addr,unsigned long next,struct mm_walk *walk); -int (*pud_entry)(pud_t *pud,unsigned long addr, +int (*pud_entry)(pud_t pud,pud_t *pudp,unsigned long addr,unsigned long next,struct mm_walk *walk);int (*pmd_entry)(pmd_t *pmd,unsigned long addr,unsigned long next,struct mm_walk *walk);
mm/hmm.c +7 -9
@@ -459,28 +459,26 @@ static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud) range->flags[HMM_PFN_VALID ]; } -static int hmm_vma_walk_pud (pud_t *pudp,unsigned long start,unsigned long end, -struct mm_walk *walk) +static int hmm_vma_walk_pud (pud_t pud,pud_t *pudp,unsigned long start, +unsigned long end,struct mm_walk *walk) {struct hmm_vma_walk *hmm_vma_walk = walk->private;struct hmm_range *range = hmm_vma_walk->range;unsigned long addr = start; -pud_t pud;int ret =0 ;spinlock_t *ptl =pud_trans_huge_lock (pudp, walk->vma);if (!ptl)return 0 ; +if (memcmp (pudp, &pud,sizeof (pud)) !=0 ) { + walk->action =ACTION_AGAIN ; +spin_unlock (ptl); +return 0 ; + }/* Normally we don't want to split the huge page */ walk->action =ACTION_CONTINUE ; - pud =READ_ONCE (*pudp); -if (pud_none (pud)) { -spin_unlock (ptl); -return hmm_vma_walk_hole (start, end,-1 , walk); - } -if (pud_huge (pud) &&pud_devmap (pud)) {unsigned long i, npages, pfn;uint64_t *pfns, cpu_flags;
mm/mapping_dirty_helpers.c +2 -4
@@ -125,12 +125,10 @@ static int wp_clean_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long end, }/* wp_clean_pud_entry - The pagewalk pud callback. */ -static int wp_clean_pud_entry (pud_t *pud,unsigned long addr,unsigned long end, -struct mm_walk *walk) +static int wp_clean_pud_entry (pud_t pudval,pud_t *pudp,unsigned long addr, +unsigned long end,struct mm_walk *walk) {/* Dirty-tracking should be handled on the pte level */ -pud_t pudval =READ_ONCE (*pud); -if (pud_trans_huge (pudval) ||pud_devmap (pudval))WARN_ON (pud_write (pudval) ||pud_dirty (pudval));
mm/pagewalk.c +16 -12
@@ -58,7 +58,7 @@ static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,return err; } -static int walk_pmd_range (pud_t *pud,unsigned long addr,unsigned long end, +static int walk_pmd_range (pud_t pud,unsigned long addr,unsigned long end,struct mm_walk *walk) {pmd_t *pmd;
@@ -67,7 +67,7 @@ static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,int err =0 ;int depth =real_depth (3 ); - pmd =pmd_offset (pud, addr); + pmd =pmd_offset (&pud, addr); do { again: next = pmd_addr_end(addr, end);
@@ -119,17 +119,19 @@ static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,static int walk_pud_range (p4d_t *p4d,unsigned long addr,unsigned long end,struct mm_walk *walk) { -pud_t *pud; +pud_t *pudp; +pud_t pud;unsigned long next;const struct mm_walk_ops *ops = walk->ops;int err =0 ;int depth =real_depth (2 ); - pud =pud_offset (p4d, addr); + pudp =pud_offset (p4d, addr); do { again: + pud = READ_ONCE(*pudp); next = pud_addr_end(addr, end); - if (pud_none(*pud) || (!walk->vma && !walk->no_vma)) { + if (pud_none(pud) || (!walk->vma && !walk->no_vma)) { if (ops->pte_hole) err = ops->pte_hole(addr, next, depth, walk); if (err)
@@ -140,27 +142,29 @@ static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, walk->action =ACTION_SUBTREE ;if (ops->pud_entry) - err = ops->pud_entry (pud, addr, next, walk); + err = ops->pud_entry (pud, pudp, addr, next, walk);if (err)break ;if (walk->action ==ACTION_AGAIN ) goto again; -if ((!walk->vma && (pud_leaf (*pud) || !pud_present (*pud))) || +if ((!walk->vma && (pud_leaf (pud) || !pud_present (pud))) || walk->action ==ACTION_CONTINUE || !(ops->pmd_entry || ops->pte_entry))continue ; -if (walk->vma) -split_huge_pud (walk->vma, pud, addr); -if (pud_none (*pud)) - goto again; +if (walk->vma) { +split_huge_pud (walk->vma, pudp, addr); + pud =READ_ONCE (*pudp); +if (pud_none (pud)) + goto again; + } err =walk_pmd_range (pud, addr, next, walk);if (err)break ; - }while (pud++, addr = next, addr != end); + }while (pudp++, addr = next, addr != end);return err; }
mm/ptdump.c +1 -2
@@ -59,11 +59,10 @@ static int ptdump_p4d_entry(p4d_t *p4d, unsigned long addr,return 0 ; } -static int ptdump_pud_entry (pud_t *pud,unsigned long addr, +static int ptdump_pud_entry (pud_t val,pud_t *pudp,unsigned long addr,unsigned long next,struct mm_walk *walk) {struct ptdump_state *st = walk->private; -pud_t val =READ_ONCE (*pud);#if CONFIG_PGTABLE_LEVELS >2 && defined(CONFIG_KASAN )if (pud_page (val) ==virt_to_page (lm_alias (kasan_early_shadow_pmd)))
Signature · 1 line
2.28.0
Michal Hocko
reviewerQuoted 10 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Quoted 20 lines
Design
=======1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited. A new pagechain data structure is added to enable multi-page deposit.2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and
sub_compound_mapcountare introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP andsub_compound_mapcountcounts the PMD mapping by using page[N*512 + 3].compound_mapcount.
Quoted 5 lines
3. Using CMA allocaiton for 1GB THP: instead of bump
MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back toalloc_contig_pagesfor 1GB THP if necessary.
Do those pages get instantiated during the page fault or only via khugepaged? This is an important design detail because then we have to think carefully about how much automatic we want this to be. Memory overhead can be quite large with 2MB THPs already. Also what about the allocation overhead? Do you have any numbers?
Maybe all these details are described in the patcheset but the cover letter should contain all that information. It doesn't make much sense to dig into details in a patchset this large without having an idea how feasible this is.
Thanks.
Quoted 91 lines
Patch Organization
=======Patch 01 adds the new pagechain data structure.
Patch 02 to 13 adds 1GB THP support in variable places.
Patch 14 tries to use
alloc_contig_pagesfor 1GB THP allocaiton.Patch 15 moves
hugetlb_cmareservation tocma.cand rename it tohugepage_cma.Patch 16 use
hugepage_cmareservation for 1GB THP allocation.Any suggestions and comments are welcome.
Zi Yan (16):
mm: add pagechain container for storing multiple pages.
mm: thp: 1GB anonymous page implementation.
mm: proc: add 1GB THP kpageflag.
mm: thp: 1GB THP copy on write implementation.
mm: thp: handling 1GB THP reference bit.
mm: thp: add 1GB THPsplit_huge_pud_page()function.
mm: stats: make smap stats understand PUD THPs.
mm:page_vma_walk: teach it about PMD-mapped PUD THP.
mm: thp: 1GB THP support intry_to_unmap().
mm: thp: split 1GB THPs at page reclaim.
mm: thp: 1GB THP follow_p*d_page() support.
mm: support 1GB THP pagemap support.
mm: thp: add a knob to enable/disable 1GB THPs.
mm:page_alloc: >=MAX_ORDER pages allocation an deallocation.
hugetlb: cma: move cma reserve function tocma.c.
mm: thp: use cma reservation for pud thp allocation..../admin-guide/kernel-parameters.txt | 2 +- arch/arm64/mm/hugetlbpage.c | 2 +- arch/powerpc/mm/hugetlbpage.c | 2 +- arch/x86/include/asm/pgalloc.h | 68 ++ arch/x86/include/asm/pgtable.h | 26 + arch/x86/kernel/setup.c | 8 +- arch/x86/mm/pgtable.c | 38 + drivers/base/node.c | 3 + fs/proc/meminfo.c | 2 + fs/proc/page.c | 2 + fs/proc/task_mmu.c | 122 ++- include/linux/cma.h | 18 + include/linux/huge_mm.h | 84 +- include/linux/hugetlb.h | 12 - include/linux/memcontrol.h | 5 + include/linux/mm.h | 29 +- include/linux/mm_types.h | 1 + include/linux/mmu_notifier.h | 13 + include/linux/mmzone.h | 1 + include/linux/page-flags.h | 47 + include/linux/pagechain.h | 73 ++ include/linux/pgtable.h | 34 + include/linux/rmap.h | 10 +- include/linux/swap.h | 2 + include/linux/vm_event_item.h | 7 + include/uapi/linux/kernel-page-flags.h | 2 + kernel/events/uprobes.c | 4 +- kernel/fork.c | 5 + mm/cma.c | 119 +++ mm/gup.c | 60 +- mm/huge_memory.c | 939 +++++++++++++++++- mm/hugetlb.c | 114 +-- mm/internal.h | 2 + mm/khugepaged.c | 6 +- mm/ksm.c | 4 +- mm/memcontrol.c | 13 + mm/memory.c | 51 +- mm/mempolicy.c | 21 +- mm/migrate.c | 12 +- mm/page_alloc.c | 57 +- mm/page_vma_mapped.c | 129 ++- mm/pgtable-generic.c | 56 ++ mm/rmap.c | 289 ++++-- mm/swap.c | 31 + mm/swap_slots.c | 2 + mm/swapfile.c | 8 +- mm/userfaultfd.c | 2 +- mm/util.c | 16 +- mm/vmscan.c | 58 +- mm/vmstat.c | 8 + 50 files changed, 2270 insertions(+), 349 deletions(-) create mode 100644 include/linux/pagechain.h--
2.28.0
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 8 lines · Michal Hocko
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.
Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Hello, Michal!
We at Facebook are using 1 GB hugetlbfs pages and are getting noticeable performance wins on some workloads.
Historically we allocated gigantic pages at the boot time, but recently moved to cma-based dynamic approach. Still, hugetlbfs interface requires more management than we would like to do. 1 GB THP seems to be a better alternative. So I definitely see it as a very useful feature.
Given the cost of an allocation, I'm slightly skeptical about an automatic heuristics-based approach, but if an application can explicitly mark target areas with madvise(), I don't see why it wouldn't work.
In our case we'd like to have a reliable way to get 1 GB THPs at some point (usually at the start of an application), and transparently destroy them on the application exit.
Once we'll have the patchset in a relatively good shape, I'll be happy to give it a test in our environment and share results.
Thanks!
Quoted 135 lines
Quoted 21 lines
Design
=======1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited. A new pagechain data structure is added to enable multi-page deposit.2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and
sub_compound_mapcountare introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP andsub_compound_mapcountcounts the PMD mapping by using page[N*512 + 3].compound_mapcount.Quoted 5 lines
3. Using CMA allocaiton for 1GB THP: instead of bump
MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back toalloc_contig_pagesfor 1GB THP if necessary.Do those pages get instantiated during the page fault or only via khugepaged? This is an important design detail because then we have to think carefully about how much automatic we want this to be. Memory overhead can be quite large with 2MB THPs already. Also what about the allocation overhead? Do you have any numbers?
Maybe all these details are described in the patcheset but the cover letter should contain all that information. It doesn't make much sense to dig into details in a patchset this large without having an idea how feasible this is.
Thanks.
Quoted 91 lines
Patch Organization
=======Patch 01 adds the new pagechain data structure.
Patch 02 to 13 adds 1GB THP support in variable places.
Patch 14 tries to use
alloc_contig_pagesfor 1GB THP allocaiton.Patch 15 moves
hugetlb_cmareservation tocma.cand rename it tohugepage_cma.Patch 16 use
hugepage_cmareservation for 1GB THP allocation.Any suggestions and comments are welcome.
Zi Yan (16):
mm: add pagechain container for storing multiple pages.
mm: thp: 1GB anonymous page implementation.
mm: proc: add 1GB THP kpageflag.
mm: thp: 1GB THP copy on write implementation.
mm: thp: handling 1GB THP reference bit.
mm: thp: add 1GB THPsplit_huge_pud_page()function.
mm: stats: make smap stats understand PUD THPs.
mm:page_vma_walk: teach it about PMD-mapped PUD THP.
mm: thp: 1GB THP support intry_to_unmap().
mm: thp: split 1GB THPs at page reclaim.
mm: thp: 1GB THP follow_p*d_page() support.
mm: support 1GB THP pagemap support.
mm: thp: add a knob to enable/disable 1GB THPs.
mm:page_alloc: >=MAX_ORDER pages allocation an deallocation.
hugetlb: cma: move cma reserve function tocma.c.
mm: thp: use cma reservation for pud thp allocation..../admin-guide/kernel-parameters.txt | 2 +- arch/arm64/mm/hugetlbpage.c | 2 +- arch/powerpc/mm/hugetlbpage.c | 2 +- arch/x86/include/asm/pgalloc.h | 68 ++ arch/x86/include/asm/pgtable.h | 26 + arch/x86/kernel/setup.c | 8 +- arch/x86/mm/pgtable.c | 38 + drivers/base/node.c | 3 + fs/proc/meminfo.c | 2 + fs/proc/page.c | 2 + fs/proc/task_mmu.c | 122 ++- include/linux/cma.h | 18 + include/linux/huge_mm.h | 84 +- include/linux/hugetlb.h | 12 - include/linux/memcontrol.h | 5 + include/linux/mm.h | 29 +- include/linux/mm_types.h | 1 + include/linux/mmu_notifier.h | 13 + include/linux/mmzone.h | 1 + include/linux/page-flags.h | 47 + include/linux/pagechain.h | 73 ++ include/linux/pgtable.h | 34 + include/linux/rmap.h | 10 +- include/linux/swap.h | 2 + include/linux/vm_event_item.h | 7 + include/uapi/linux/kernel-page-flags.h | 2 + kernel/events/uprobes.c | 4 +- kernel/fork.c | 5 + mm/cma.c | 119 +++ mm/gup.c | 60 +- mm/huge_memory.c | 939 +++++++++++++++++- mm/hugetlb.c | 114 +-- mm/internal.h | 2 + mm/khugepaged.c | 6 +- mm/ksm.c | 4 +- mm/memcontrol.c | 13 + mm/memory.c | 51 +- mm/mempolicy.c | 21 +- mm/migrate.c | 12 +- mm/page_alloc.c | 57 +- mm/page_vma_mapped.c | 129 ++- mm/pgtable-generic.c | 56 ++ mm/rmap.c | 289 ++++-- mm/swap.c | 31 + mm/swap_slots.c | 2 + mm/swapfile.c | 8 +- mm/userfaultfd.c | 2 +- mm/util.c | 16 +- mm/vmscan.c | 58 +- mm/vmstat.c | 8 + 50 files changed, 2270 insertions(+), 349 deletions(-) create mode 100644 include/linux/pagechain.h--
2.28.0Signature · 2 lines
Michal Hocko SUSE Labs
Jason Gunthorpe
maintainerQuoted 13 lines · Roman Gushchin
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Quoted 8 lines
Quoted 3 lines
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Hello, Michal!
We at Facebook are using 1 GB hugetlbfs pages and are getting noticeable performance wins on some workloads.
At least from a RDMA NIC perspective I've heard from a lot of users that higher order pages at the DMA level is giving big speed ups too.
It is basically the same dynamic as CPU TLB, except missing a 'TLB' cache in a PCI-E device is dramatically more expensive to refill. With 200G and soon 400G networking these misses are a growing problem.
With HPC nodes now pushing 1TB of actual physical RAM and single applications basically using all of it, there is definately some meaningful return - if pages can be reliably available.
At least for HPC where the node returns to an idle state after each job and most of the 1TB memory becomes freed up again, it seems more believable to me that a large cache of 1G pages could be available?
Even triggering some kind of cleaner between jobs to defragment could be a reasonable approach..
Jason
Quoted 7 lines · Jason Gunthorpe
At least from a RDMA NIC perspective I've heard from a lot of users that higher order pages at the DMA level is giving big speed ups too.
It is basically the same dynamic as CPU TLB, except missing a 'TLB' cache in a PCI-E device is dramatically more expensive to refill. With 200G and soon 400G networking these misses are a growing problem.
Quoted 7 lines
With HPC nodes now pushing 1TB of actual physical RAM and single applications basically using all of it, there is definately some meaningful return - if pages can be reliably available.
At least for HPC where the node returns to an idle state after each job and most of the 1TB memory becomes freed up again, it seems more believable to me that a large cache of 1G pages could be available?
You may be interested in trying out my current THP patchset:
http://git.infradead.org/users/willy/pagecache.git
It doesn't allocate pages larger than PMD size, but it does allocate pages *up to* PMD size for the page cache which means that larger pages are easier to create as larger pages aren't fragmented all over the system.
If someone wants to opportunistically allocate pages larger than PMD size, I've put some preliminary support in for that, but I've never tested any of it. That's not my goal at the moment.
I'm not clear whether these HPC users primarily use page cache or anonymous memory (with O_DIRECT). Probably a mixture.
Jason Gunthorpe
maintainerQuoted 16 lines · Matthew Wilcox
Quoted 7 lines · Jason Gunthorpe
At least from a RDMA NIC perspective I've heard from a lot of users that higher order pages at the DMA level is giving big speed ups too.
It is basically the same dynamic as CPU TLB, except missing a 'TLB' cache in a PCI-E device is dramatically more expensive to refill. With 200G and soon 400G networking these misses are a growing problem.
Quoted 7 lines
With HPC nodes now pushing 1TB of actual physical RAM and single applications basically using all of it, there is definately some meaningful return - if pages can be reliably available.
At least for HPC where the node returns to an idle state after each job and most of the 1TB memory becomes freed up again, it seems more believable to me that a large cache of 1G pages could be available?
Quoted 7 lines
You may be interested in trying out my current THP patchset:
http://git.infradead.org/users/willy/pagecache.git
It doesn't allocate pages larger than PMD size, but it does allocate pages *up to* PMD size for the page cache which means that larger pages are easier to create as larger pages aren't fragmented all over the system.
Yeah, I saw that, it looks like a great direction.
Quoted 6 lines
If someone wants to opportunistically allocate pages larger than PMD size, I've put some preliminary support in for that, but I've never tested any of it. That's not my goal at the moment.
I'm not clear whether these HPC users primarily use page cache or anonymous memory (with
O_DIRECT). Probably a mixture.
There are defiantly HPC systems now that are filesystem-less - they import data for computation from the network using things like blob storage or some other kind of non-POSIX userspace based data storage scheme.
Jason
Quoted 27 lines · Roman Gushchin
Quoted 8 lines · Michal Hocko
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Hello, Michal!
We at Facebook are using 1 GB hugetlbfs pages and are getting noticeable performance wins on some workloads.
Historically we allocated gigantic pages at the boot time, but recently moved to cma-based dynamic approach. Still, hugetlbfs interface requires more management than we would like to do. 1 GB THP seems to be a better alternative. So I definitely see it as a very useful feature.
Quoted 7 lines
Given the cost of an allocation, I'm slightly skeptical about an automatic heuristics-based approach, but if an application can explicitly mark target areas with
madvise(), I don't see why it wouldn't work.In our case we'd like to have a reliable way to get 1 GB THPs at some point (usually at the start of an application), and transparently destroy them on the application exit.
Hi Roman,
In your current use case at Facebook, are you adding 1G hugetlb pages to the hugetlb pool and then using them within applications? Or, are you dynamically allocating them at fault time (hugetlb overcommit/surplus)?
Latency time for use of such pages includes:
- Putting together 1G contiguous
- Clearing 1G memory
In the 'allocation at fault time' mode you incur both costs at fault time. If using pages from the pool, your only cost at fault time is clearing the page.
Signature · 1 line
Mike Kravetz
Quoted 42 lines · Mike Kravetz
Quoted 27 lines · Roman Gushchin
Quoted 8 lines · Michal Hocko
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Hello, Michal!
We at Facebook are using 1 GB hugetlbfs pages and are getting noticeable performance wins on some workloads.
Historically we allocated gigantic pages at the boot time, but recently moved to cma-based dynamic approach. Still, hugetlbfs interface requires more management than we would like to do. 1 GB THP seems to be a better alternative. So I definitely see it as a very useful feature.
Quoted 7 lines
Given the cost of an allocation, I'm slightly skeptical about an automatic heuristics-based approach, but if an application can explicitly mark target areas with
madvise(), I don't see why it wouldn't work.In our case we'd like to have a reliable way to get 1 GB THPs at some point (usually at the start of an application), and transparently destroy them on the application exit.
Hi Roman,
In your current use case at Facebook, are you adding 1G hugetlb pages to the hugetlb pool and then using them within applications? Or, are you dynamically allocating them at fault time (hugetlb overcommit/surplus)?
Quoted 7 lines
Latency time for use of such pages includes:
- Putting together 1G contiguous
- Clearing 1G memoryIn the 'allocation at fault time' mode you incur both costs at fault time. If using pages from the pool, your only cost at fault time is clearing the page.
Hi Mike,
We're using a pool. Under dynamic I mean that gigantic pages are not allocated at a boot time.
Thanks!
Michal Hocko
reviewerQuoted 13 lines · Roman Gushchin
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Quoted 8 lines
Quoted 3 lines
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Hello, Michal!
We at Facebook are using 1 GB hugetlbfs pages and are getting noticeable performance wins on some workloads.
Let me clarify. I am not questioning 1GB (or large) pages in general. I believe it is quite clear that there are usecases which hugely benefit from them. I am mostly asking for the transparent part of it which traditionally means that userspace mostly doesn't have to care and get them. 2MB THPs have established certain expectations mostly a really aggressive pro-active instanciation. This has bitten us many times and create a "you need to disable THP to fix your problem whatever that is" cargo cult. I hope we do not want to repeat that mistake here again.
Quoted 8 lines
Historically we allocated gigantic pages at the boot time, but recently moved to cma-based dynamic approach. Still, hugetlbfs interface requires more management than we would like to do. 1 GB THP seems to be a better alternative. So I definitely see it as a very useful feature.
Given the cost of an allocation, I'm slightly skeptical about an automatic heuristics-based approach, but if an application can explicitly mark target areas with
madvise(), I don't see why it wouldn't work.
An explicit opt-in sounds much more appropriate to me as well. If we go with a specific API then I would not make it 1GB pages specific. Why cannot we have an explicit interface to "defragment" address space range into large pages and the kernel would use large pages where appropriate? Or is the additional copying prohibitively expensive?
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 23 lines · Michal Hocko
Quoted 13 lines · Roman Gushchin
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
Quoted 8 lines
Quoted 3 lines
Please be more specific about usecases. This better have some strong ones because THP code is complex enough already to add on top solely based on a generic TLB pressure easing.
Hello, Michal!
We at Facebook are using 1 GB hugetlbfs pages and are getting noticeable performance wins on some workloads.
Quoted 8 lines
Let me clarify. I am not questioning 1GB (or large) pages in general. I believe it is quite clear that there are usecases which hugely benefit from them. I am mostly asking for the transparent part of it which traditionally means that userspace mostly doesn't have to care and get them. 2MB THPs have established certain expectations mostly a really aggressive pro-active instanciation. This has bitten us many times and create a "you need to disable THP to fix your problem whatever that is" cargo cult. I hope we do not want to repeat that mistake here again.
Absolutely, I agree with all above. 1 GB THPs have even fewer chances to be allocated automatically without hurting overall performance.
I believe that historically the THP allocation success rate and cost were not good enough to have a strict interface, that's why the "best effort" approach was used. Maybe I'm wrong here. Also in some cases (e.g. desktop) an opportunistic approach looks like "it's some perf boost for free". However in case of large distributed systems it's important to get a predictable and uniform performance across nodes, so "maybe some hosts will perform better" is not giving much.
Quoted 10 lines
Quoted 9 lines
Historically we allocated gigantic pages at the boot time, but recently moved to cma-based dynamic approach. Still, hugetlbfs interface requires more management than we would like to do. 1 GB THP seems to be a better alternative. So I definitely see it as a very useful feature.
Given the cost of an allocation, I'm slightly skeptical about an automatic heuristics-based approach, but if an application can explicitly mark target areas with
madvise(), I don't see why it wouldn't work.
Quoted 5 lines
An explicit opt-in sounds much more appropriate to me as well. If we go with a specific API then I would not make it 1GB pages specific. Why cannot we have an explicit interface to "defragment" address space range into large pages and the kernel would use large pages where appropriate? Or is the additional copying prohibitively expensive?
Can you, please, elaborate a bit more here? It seems like madvise(MADV_HUGEPAGE) provides something similar to what you're describing, but there are lot of details here, so I'm probably missing something.
Thank you!
Michal Hocko
reviewerQuoted 1 line · Roman Gushchin
On Fri, Sep 04, 2020 at 09:42:07AM +0200, Michal Hocko wrote:
[...]
Quoted 9 lines
Quoted 5 lines
An explicit opt-in sounds much more appropriate to me as well. If we go with a specific API then I would not make it 1GB pages specific. Why cannot we have an explicit interface to "defragment" address space range into large pages and the kernel would use large pages where appropriate? Or is the additional copying prohibitively expensive?
Can you, please, elaborate a bit more here? It seems like
madvise(MADV_HUGEPAGE)provides something similar to what you're describing, but there are lot of details here, so I'm probably missing something.
MADV_HUGEPAGE is controlling a preference for THP to be used for a particular address range. So it looks similar but the historical behavior is to control page faults as well and the behavior depends on the global setup.
I've had in mind something much simpler. Effectively an API to invoke khugepaged (like) functionality synchronously from the calling context on the specific address range. It could be more aggressive than the regular khugepaged and create even 1G pages (or as large THPs as page tables can handle on the particular arch for that matter).
As this would be an explicit call we do not have to be worried about the resulting latency because it would be an explicit call by the userspace. The default khugepaged has a harder position there because has no understanding of the target address space and cannot make any cost/benefit evaluation so it has to be more conservative.
Signature · 2 lines
Michal Hocko SUSE Labs
Zi Yan
reviewerQuoted 24 lines · Michal Hocko
Quoted 1 line · Roman Gushchin
On Fri, Sep 04, 2020 at 09:42:07AM +0200, Michal Hocko wrote:
[...]
Quoted 9 lines
Quoted 5 lines
An explicit opt-in sounds much more appropriate to me as well. If we go with a specific API then I would not make it 1GB pages specific. Why cannot we have an explicit interface to "defragment" address space range into large pages and the kernel would use large pages where appropriate? Or is the additional copying prohibitively expensive?
Can you, please, elaborate a bit more here? It seems like
madvise(MADV_HUGEPAGE)provides something similar to what you're describing, but there are lot of details here, so I'm probably missing something.
MADV_HUGEPAGEis controlling a preference for THP to be used for a particular address range. So it looks similar but the historical behavior is to control page faults as well and the behavior depends on the global setup.I've had in mind something much simpler. Effectively an API to invoke khugepaged (like) functionality synchronously from the calling context on the specific address range. It could be more aggressive than the regular khugepaged and create even 1G pages (or as large THPs as page tables can handle on the particular arch for that matter).
Quoted 5 lines
As this would be an explicit call we do not have to be worried about the resulting latency because it would be an explicit call by the userspace. The default khugepaged has a harder position there because has no understanding of the target address space and cannot make any cost/benefit evaluation so it has to be more conservative.
Something like MADV_HUGEPAGE_SYNC? It would be useful, since users have better and clearer control of getting huge pages from the kernel and know when they will pay the cost of getting the huge pages.
I would think the suggestion is more about the huge page control options currently provided by the kernel do not have predictable performance outcome, since MADV_HUGEPAGE is a best-effort option and does not tell users whether the marked virtual address range is backed by huge pages or not when the madvise returns. MADV_HUGEPAGE_SYNC would provide a deterministic result to users on whether the huge page(s) are formed or not.
—
Best Regards,
Yan Zi
Quoted 36 lines · Zi Yan
Quoted 24 lines · Michal Hocko
Quoted 1 line · Roman Gushchin
On Fri, Sep 04, 2020 at 09:42:07AM +0200, Michal Hocko wrote:
[...]
Quoted 9 lines
Quoted 5 lines
An explicit opt-in sounds much more appropriate to me as well. If we go with a specific API then I would not make it 1GB pages specific. Why cannot we have an explicit interface to "defragment" address space range into large pages and the kernel would use large pages where appropriate? Or is the additional copying prohibitively expensive?
Can you, please, elaborate a bit more here? It seems like
madvise(MADV_HUGEPAGE)provides something similar to what you're describing, but there are lot of details here, so I'm probably missing something.
MADV_HUGEPAGEis controlling a preference for THP to be used for a particular address range. So it looks similar but the historical behavior is to control page faults as well and the behavior depends on the global setup.I've had in mind something much simpler. Effectively an API to invoke khugepaged (like) functionality synchronously from the calling context on the specific address range. It could be more aggressive than the regular khugepaged and create even 1G pages (or as large THPs as page tables can handle on the particular arch for that matter).
Quoted 5 lines
As this would be an explicit call we do not have to be worried about the resulting latency because it would be an explicit call by the userspace. The default khugepaged has a harder position there because has no understanding of the target address space and cannot make any cost/benefit evaluation so it has to be more conservative.
Something like
MADV_HUGEPAGE_SYNC? It would be useful, since users have better and clearer control of getting huge pages from the kernel and know when they will pay the cost of getting the huge pages.
Quoted 7 lines
I would think the suggestion is more about the huge page control options currently provided by the kernel do not have predictable performance outcome, since
MADV_HUGEPAGEis a best-effort option and does not tell users whether the marked virtual address range is backed by huge pages or not when the madvise returns.MADV_HUGEPAGE_SYNCwould provide a deterministic result to users on whether the huge page(s) are formed or not.
Yeah, I agree with Michal here, we need a more straightforward interface.
The hard question here is how hard the kernel should try to allocate a gigantic page and how fast it should give up and return an error? I'd say to try really hard if there are some chances to succeed, so that if an error is returned, there are no more reasons to retry. Any objections/better ideas here?
Given that we need to pass a page size, we probably need either to introduce a new syscall (madvise2?) with an additional argument, or add a bunch of new madvise flags, like MADV_HUGEPAGE_SYNC + encoded 2MB, 1GB etc.
Idk what is better long-term, but new madvise flags are probably slightly easier to deal with in the development process.
Thanks!
John Hubbard
reviewerQuoted 17 lines · Roman Gushchin
Quoted 8 lines · Zi Yan
Quoted 3 lines · Michal Hocko
Quoted 1 line · Roman Gushchin
On Fri, Sep 04, 2020 at 09:42:07AM +0200, Michal Hocko wrote:
[...]
Something like
MADV_HUGEPAGE_SYNC? It would be useful, since users have better and clearer control of getting huge pages from the kernel and know when they will pay the cost of getting the huge pages.Quoted 7 lines
I would think the suggestion is more about the huge page control options currently provided by the kernel do not have predictable performance outcome, since
MADV_HUGEPAGEis a best-effort option and does not tell users whether the marked virtual address range is backed by huge pages or not when the madvise returns.MADV_HUGEPAGE_SYNCwould provide a deterministic result to users on whether the huge page(s) are formed or not.
Quoted 7 lines
Yeah, I agree with Michal here, we need a more straightforward interface.
The hard question here is how hard the kernel should try to allocate a gigantic page and how fast it should give up and return an error? I'd say to try really hard if there are some chances to succeed, so that if an error is returned, there are no more reasons to retry. Any objections/better ideas here?
I agree, especially because this is starting to look a lot more like an allocation call. And I think it would be appropriate for the kernel to try approximately as hard to provide these 1GB pages, as it would to allocate normal memory to a process.
In fact, for a moment I thought, why not go all the way and make this actually be a true allocation? However, given that we still have operations that require page splitting, with no good way to call back user space to notify it that its "allocated" huge pages are being split, that fails. But it's still pretty close.
Quoted 8 lines
Given that we need to pass a page size, we probably need either to introduce a new syscall (madvise2?) with an additional argument, or add a bunch of new madvise flags, like
MADV_HUGEPAGE_SYNC+ encoded 2MB, 1GB etc.Idk what is better long-term, but new madvise flags are probably slightly easier to deal with in the development process.
Probably either an MADV_* flag or a new syscall would work fine. But given that this seems like a pretty distinct new capability, one with options and man page documentation and possibly future flags itself, I'd lean toward making it its own new syscall, maybe:
compact_huge_pages(nbytes or npages, flags /* page size, etc */);
...thus leaving madvise() and it's remaining flags still available, to further refine things.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Michal Hocko
reviewerQuoted 33 lines · Roman Gushchin
Quoted 32 lines · Zi Yan
Quoted 29 lines · Michal Hocko
Quoted 1 line · Roman Gushchin
On Fri, Sep 04, 2020 at 09:42:07AM +0200, Michal Hocko wrote:
[...]
Quoted 9 lines
Quoted 5 lines
An explicit opt-in sounds much more appropriate to me as well. If we go with a specific API then I would not make it 1GB pages specific. Why cannot we have an explicit interface to "defragment" address space range into large pages and the kernel would use large pages where appropriate? Or is the additional copying prohibitively expensive?
Can you, please, elaborate a bit more here? It seems like
madvise(MADV_HUGEPAGE)provides something similar to what you're describing, but there are lot of details here, so I'm probably missing something.
MADV_HUGEPAGEis controlling a preference for THP to be used for a particular address range. So it looks similar but the historical behavior is to control page faults as well and the behavior depends on the global setup.I've had in mind something much simpler. Effectively an API to invoke khugepaged (like) functionality synchronously from the calling context on the specific address range. It could be more aggressive than the regular khugepaged and create even 1G pages (or as large THPs as page tables can handle on the particular arch for that matter).
As this would be an explicit call we do not have to be worried about the resulting latency because it would be an explicit call by the userspace. The default khugepaged has a harder position there because has no understanding of the target address space and cannot make any cost/benefit evaluation so it has to be more conservative.
Quoted 3 lines
Quoted 3 lines
Something like
MADV_HUGEPAGE_SYNC? It would be useful, since users have better and clearer control of getting huge pages from the kernel and know when they will pay the cost of getting the huge pages.
The name is not really that important. The crucial design decisions are
- THP allocation time - #PF and/or madvise context
- lazy/sync instantiation
- huge page sizes controllable by the userspace?
- aggressiveness - how hard to try
- internal fragmentation - allow to create THPs on sparsely or unpopulated ranges
- do we need some sort of access control or privilege check as some THPs would be a really scarce (like those that require pre-reservation).
Quoted 8 lines
Quoted 7 lines
I would think the suggestion is more about the huge page control options currently provided by the kernel do not have predictable performance outcome, since
MADV_HUGEPAGEis a best-effort option and does not tell users whether the marked virtual address range is backed by huge pages or not when the madvise returns.MADV_HUGEPAGE_SYNCwould provide a deterministic result to users on whether the huge page(s) are formed or not.
Quoted 7 lines
Yeah, I agree with Michal here, we need a more straightforward interface.
The hard question here is how hard the kernel should try to allocate a gigantic page and how fast it should give up and return an error? I'd say to try really hard if there are some chances to succeed, so that if an error is returned, there are no more reasons to retry. Any objections/better ideas here?
If this is going to be an explicit interface like madvise then I would follow the same semantic as hugetlb pages allocation - aka try as hard as feasible (whatever that means).
Quoted 3 lines
Given that we need to pass a page size, we probably need either to introduce a new syscall (madvise2?) with an additional argument, or add a bunch of new madvise flags, like
MADV_HUGEPAGE_SYNC+ encoded 2MB, 1GB etc.
Do we really need to bother userspace with making decision about the page size? I would expect that the userspace only cares to get huge pages backed memory range. The larger the pages the better. It is up to the kernel to make the resource control here. Afterall THPs can be split/reclaimed under a memory pressure so we do not want to make any promises about pages backing any mapping.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 10 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Quoted 11 lines
Design
=======1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited.
False. Current code can deposit arbitrary number of page tables.
What can be problem to you is that these page tables tied to struct page of PMD page table.
Quoted 8 lines
A new pagechain data structure is added to enable multi-page deposit.
2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and
sub_compound_mapcountare introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP andsub_compound_mapcountcounts the PMD mapping by using page[N*512 + 3].compound_mapcount.
I had hard time reasoning about DoubleMap vs. rmap. Good for you if you get it right.
Quoted 6 lines
3. Using CMA allocaiton for 1GB THP: instead of bump
MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back toalloc_contig_pagesfor 1GB THP if necessary.
Signature · 1 line
Kirill A. Shutemov
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.
Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Hello, Kirill!
I share your skepticism about opportunistic 1 GB allocations, however it might be useful if backed by an madvise() annotations from userspace application. In this case, 1 GB THPs might be an alternative to 1 GB hugetlbfs pages, but with a more convenient interface.
Thanks!
Quoted 39 lines
Quoted 4 lines
Design
=======Quoted 8 lines
1GB THP implementation looks similar to exiting THP code except some new designs for the additional page table level.
1. Page table deposit and withdraw using a new pagechain data structure:
instead of one PTE page table page, 1GB THP requires 513 page table pages (one PMD page table page and 512 PTE page table pages) to be deposited at the page allocaiton time, so that we can split the page later. Currently, the page table deposit is using ->lru, thus only one page can be deposited.False. Current code can deposit arbitrary number of page tables.
What can be problem to you is that these page tables tied to struct page of PMD page table.
Quoted 8 lines
A new pagechain data structure is added to enable multi-page deposit.
2. Triple mapped 1GB THP : 1GB THP can be mapped by a combination of PUD, PMD, and PTE entries. Mixing PUD an PTE mapping can be achieved with existing PageDoubleMap mechanism. To add PMD mapping, PMDPageInPUD and
sub_compound_mapcountare introduced. PMDPageInPUD is the 512-aligned base page in a 1GB THP andsub_compound_mapcountcounts the PMD mapping by using page[N*512 + 3].compound_mapcount.I had hard time reasoning about DoubleMap vs. rmap. Good for you if you get it right.
Quoted 6 lines
3. Using CMA allocaiton for 1GB THP: instead of bump
MAX_ORDER, it is more sane to use something less intrusive. So all 1GB THPs are allocated from reserved CMA areas shared with hugetlb. At page splitting time, the bitmap for the 1GB THP is cleared as the resulting pages can be freed via normal page free path. We can fall back toalloc_contig_pagesfor 1GB THP if necessary.Signature · 1 line
Kirill A. Shutemov
Quoted 17 lines · Roman Gushchin
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Quoted 6 lines
Hello, Kirill!
I share your skepticism about opportunistic 1 GB allocations, however it might be useful if backed by an
madvise()annotations from userspace application. In this case, 1 GB THPs might be an alternative to 1 GB hugetlbfs pages, but with a more convenient interface.
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that - MADV_HUGEPAGE is not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).
E.g., read via man page
"This feature is primarily aimed at applications that use large mappings of data and access large regions of that memory at a time (e.g., virtualization systems such as QEMU). It can very easily waste memory (e.g., a 2 MB mapping that only ever accesses 1 byte will result in 2 MB of wired memory instead of one 4 KB page)."
Having that said, I consider having 1~GB THP - similar to 512~MP THP on arm64 - useless in most setup and I am not sure if it is worth the trouble. Just use hugetlbfs for the handful of applications where it makes sense.
Signature · 3 lines
Thanks, David / dhildenb
Zi Yan
reviewerQuoted 25 lines · David Hildenbrand
Quoted 17 lines · Roman Gushchin
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Quoted 6 lines
Hello, Kirill!
I share your skepticism about opportunistic 1 GB allocations, however it might be useful if backed by an
madvise()annotations from userspace application. In this case, 1 GB THPs might be an alternative to 1 GB hugetlbfs pages, but with a more convenient interface.
Quoted 5 lines
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob /sys/kernel/mm/transparent_hugepage/enable_1GB to enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I had alloc_contig_pages as a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a new MADV_HUGEPAGE_1GB for 1GB THP.
—
Best Regards,
Yan Zi
Quoted 33 lines · Zi Yan
Quoted 25 lines · David Hildenbrand
Quoted 17 lines · Roman Gushchin
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Quoted 6 lines
Hello, Kirill!
I share your skepticism about opportunistic 1 GB allocations, however it might be useful if backed by an
madvise()annotations from userspace application. In this case, 1 GB THPs might be an alternative to 1 GB hugetlbfs pages, but with a more convenient interface.Quoted 5 lines
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).
Quoted 7 lines
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob
/sys/kernel/mm/transparent_hugepage/enable_1GBto enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I hadalloc_contig_pagesas a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a newMADV_HUGEPAGE_1GBfor 1GB THP.
Thanks for the information - I would have loved to see important information like that (esp. how to use) in the cover letter.
So what you propose is (excluding alloc_contig_pages()) really just automatically using (previously reserved) 1GB huge pages as 1GB THP instead of explicitly using them in an application using hugetlbfs. Still, not convinced how helpful that actually is - most certainly you really want a mechanism to control this per application (+ maybe make the application indicate actual ranges where it makes sense - but then you can directly modify the application to use hugetlbfs).
I guess the interesting thing of this approach is that we can mix-and-match THP of differing granularity within a single mapping - whereby a hugetlbfs allocation would fail in case there isn't sufficient 1GB pages available. However, there are no guarantees for applications anymore (thinking about RT KVM and similar, we really want gigantic pages and cannot tolerate falling back to smaller granularity).
What are intended use cases/applications that could benefit? I doubt databases and virtualization are really a good fit - they know how to handle hugetlbfs just fine.
Signature · 3 lines
Thanks, David / dhildenb
Zi Yan
reviewerQuoted 53 lines · David Hildenbrand
Quoted 33 lines · Zi Yan
Quoted 25 lines · David Hildenbrand
Quoted 17 lines · Roman Gushchin
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Quoted 6 lines
Hello, Kirill!
I share your skepticism about opportunistic 1 GB allocations, however it might be useful if backed by an
madvise()annotations from userspace application. In this case, 1 GB THPs might be an alternative to 1 GB hugetlbfs pages, but with a more convenient interface.Quoted 5 lines
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).Quoted 7 lines
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob
/sys/kernel/mm/transparent_hugepage/enable_1GBto enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I hadalloc_contig_pagesas a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a newMADV_HUGEPAGE_1GBfor 1GB THP.Thanks for the information - I would have loved to see important information like that (esp. how to use) in the cover letter.
So what you propose is (excluding
alloc_contig_pages()) really just automatically using (previously reserved) 1GB huge pages as 1GB THP instead of explicitly using them in an application using hugetlbfs. Still, not convinced how helpful that actually is - most certainly you really want a mechanism to control this per application (+ maybe make the application indicate actual ranges where it makes sense - but then you can directly modify the application to use hugetlbfs).
Quoted 6 lines
I guess the interesting thing of this approach is that we can mix-and-match THP of differing granularity within a single mapping - whereby a hugetlbfs allocation would fail in case there isn't sufficient 1GB pages available. However, there are no guarantees for applications anymore (thinking about RT KVM and similar, we really want gigantic pages and cannot tolerate falling back to smaller granularity).
I agree that currently THP allocation does not provide a strong guarantee like hugetlbfs, which can pre-allocate pages at boot time. For users like RT KVM and such, pre-allocated hugetlb might be the only choice, since allocating huge pages from CMA (either hugetlb or 1GB THP) would fail if some pages are pinned and scattered in the CMA that could prevent huge page allocation.
In other cases, if the user can tolerate fall backs but do not like the unpredictable huge page formation outcome, we could add an madvise() option like Michal suggested [1], so the user will know whether he gets huge pages or not and can act accordingly.
Quoted 3 lines
What are intended use cases/applications that could benefit? I doubt databases and virtualization are really a good fit - they know how to handle hugetlbfs just fine.
Romand and Jason have provided some use cases [2,3]
[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/linux-mm/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/
—
Best Regards,
Yan Zi
Quoted 7 lines · Zi Yan
Quoted 5 lines · David Hildenbrand
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).
Quoted 7 lines
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob
/sys/kernel/mm/transparent_hugepage/enable_1GBto enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I hadalloc_contig_pagesas a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a newMADV_HUGEPAGE_1GBfor 1GB THP.
I think we do need that flag. Machines don't run a single workload (arguably with VMs, we're getting closer to going back to the single workload per machine, but that's a different matter). So if there's one app that wants 2MB pages and one that wants 1GB pages, we need to be able to distinguish them.
I could also see there being an app which benefits from 1GB for one mapping and prefers 2GB for a different mapping, so I think the per-mapping madvise flag is best.
I'm a little wary of encoding the size of an x86 PUD in the Linux API though. Probably best to follow the example set in include/uapi/asm-generic/hugetlb_encode.h, but I don't love it. I don't have a better suggestion though.
Zi Yan
reviewerQuoted 22 lines · Matthew Wilcox
Quoted 7 lines · Zi Yan
Quoted 5 lines · David Hildenbrand
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).Quoted 7 lines
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob
/sys/kernel/mm/transparent_hugepage/enable_1GBto enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I hadalloc_contig_pagesas a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a newMADV_HUGEPAGE_1GBfor 1GB THP.I think we do need that flag. Machines don't run a single workload (arguably with VMs, we're getting closer to going back to the single workload per machine, but that's a different matter). So if there's one app that wants 2MB pages and one that wants 1GB pages, we need to be able to distinguish them.
Quoted 8 lines
I could also see there being an app which benefits from 1GB for one mapping and prefers 2GB for a different mapping, so I think the per-mapping madvise flag is best.
I'm a little wary of encoding the size of an x86 PUD in the Linux API though. Probably best to follow the example set in
include/uapi/asm-generic/hugetlb_encode.h, but I don't love it. I don't have a better suggestion though.
Using hugeltb_encode.h makes sense to me. I will add it in the next version.
Thanks for the suggestion.
—
Best Regards,
Yan Zi
Quoted 22 lines · Matthew Wilcox
Quoted 7 lines · Zi Yan
Quoted 5 lines · David Hildenbrand
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).Quoted 7 lines
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob
/sys/kernel/mm/transparent_hugepage/enable_1GBto enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I hadalloc_contig_pagesas a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a newMADV_HUGEPAGE_1GBfor 1GB THP.I think we do need that flag. Machines don't run a single workload (arguably with VMs, we're getting closer to going back to the single workload per machine, but that's a different matter). So if there's one app that wants 2MB pages and one that wants 1GB pages, we need to be able to distinguish them.
Quoted 3 lines
I could also see there being an app which benefits from 1GB for one mapping and prefers 2GB for a different mapping, so I think the per-mapping madvise flag is best.
I wonder if apps really care about the specific page size? Particularly from a portability view?
The general app desire seems to be the need for 'efficient' memory (eg because it is highly accessed) and I suspect comes with a desire to populate the pages too.
Maybe doing something with MAP_POPULATE is an idea?
eg if I ask for 1GB of MAP_POPULATE it seems fairly natural the thing that comes back should be a 1GB THP? If I ask for only .5GB then it could be 2M pages, or whatever depending on arch support.
Jason
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Matthew Wilcox
I could also see there being an app which benefits from 1GB for one mapping and prefers 2GB for a different mapping, so I think the per-mapping madvise flag is best.
I wonder if apps really care about the specific page size? Particularly from a portability view?
No, they don't. They just want to run as fast as possible ;-)
Quoted 3 lines
The general app desire seems to be the need for 'efficient' memory (eg because it is highly accessed) and I suspect comes with a desire to populate the pages too.
The problem with a MAP_GOES_FASTER flag is that everybody sets it. Any flag name needs to convey its drawbacks as well as its advantages. Maybe MAP_EXTREMELY_COARSE_WORKINGSET would do that -- the VM will work in terms of 1GB pages for this mapping, so any swap-out is going to take out an entire 1GB at once.
But here's the thing ... we already allow
mmap(MAP_POPULATE | MAP_HUGETLB | MAP_HUGE_1GB)
So if we're not doing THP, what's the point of this thread? My understanding of THP is "Application doesn't need to change, kernel makes a decision about what page size is best based on entire system state and process's behaviour".
An madvise flag is a different beast; that's just letting the kernel know what the app thinks its behaviour will be. The kernel can pay as much (or as little) attention to that hint as it sees fit. And of course, it can change over time (either by kernel release as we change the algorithms, or simple from one minute to the next as more or less memory comes available).
Quoted 4 lines · Matthew Wilcox
But here's the thing ... we already allow
mmap(MAP_POPULATE | MAP_HUGETLB | MAP_HUGE_1GB)So if we're not doing THP, what's the point of this thread?
I wondered that too..
Quoted 2 lines
An madvise flag is a different beast; that's just letting the kernel know what the app thinks its behaviour will be. The kernel can pay
But madvise is too late, the VMA already has an address, if it is not 1G aligned it cannot be 1G THP already.
Jason
Quoted 7 lines · Jason Gunthorpe
Quoted 4 lines · Matthew Wilcox
But here's the thing ... we already allow
mmap(MAP_POPULATE | MAP_HUGETLB | MAP_HUGE_1GB)So if we're not doing THP, what's the point of this thread?
Quoted 7 lines
I wondered that too..
Quoted 2 lines
An madvise flag is a different beast; that's just letting the kernel know what the app thinks its behaviour will be. The kernel can pay
But madvise is too late, the VMA already has an address, if it is not 1G aligned it cannot be 1G THP already.
That's why user space (like QEMU) is THP-aware and selects an address that is aligned to the expected THP granularity (e.g., 2MB on x86_64).
Signature · 3 lines
Thanks, David / dhildenb
Quoted 14 lines · David Hildenbrand
Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Matthew Wilcox
But here's the thing ... we already allow
mmap(MAP_POPULATE | MAP_HUGETLB | MAP_HUGE_1GB)So if we're not doing THP, what's the point of this thread?
I wondered that too..
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
An madvise flag is a different beast; that's just letting the kernel know what the app thinks its behaviour will be. The kernel can pay
But madvise is too late, the VMA already has an address, if it is not 1G aligned it cannot be 1G THP already.
That's why user space (like QEMU) is THP-aware and selects an address that is aligned to the expected THP granularity (e.g., 2MB on
x86_64).
To me it's always seemed like there are two major divisions among THP use cases:
1) Applications that KNOW they would benefit from use of THPs, so they call madvise() with an appropriate parameter and explicitly inform the kernel of such
2) Applications that know nothing about THP but there may be an advantage that comes from "automatic" THP mapping when possible.
This is an approach that I am more familiar with that comes down to:
1) Is a VMA properly aligned for a (whatever size) THP?
2) Is the mapping request for a length >= (whatever size) THP?
3) Let's try allocating memory to map the space using (whatever size) THP, and:
-- If we succeed, great, awesome, let's do it.
-- If not, no big deal, map using as large a page as we CAN get.
There of course are myriad performance implications to this. Processes that start early after boot have a better chance of getting a THP, but that also means frequently mapped large memory spaces have a better chance of being mapped in a shared manner via a THP, e.g. libc, X servers or Firefox/Chrome. It also means that processes that would be mapped using THPs early in boot may not be if they should crash and need to be restarted.
There are all sorts of tunables that would likely need to be in place to make the second approach more viable, but I think it's certainly worth investigating.
The address selection you suggest is the basis of one of the patches I wrote for a previous iteration of THP support (and that is in Matthew's THP tree) that will try to round VM addresses to the proper alignment if possible so a THP can then be used to map the area.
Michal Hocko
reviewerQuoted 33 lines · Zi Yan
Quoted 25 lines · David Hildenbrand
Quoted 17 lines · Roman Gushchin
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Zi Yan
From: Zi Yan <[email protected]>
Hi all,
This patchset adds support for 1GB THP on
x86_64. It is on top of v5.9-rc2-mmots-2020-08-25-21-13.Quoted 7 lines
Quoted 3 lines
1GB THP is more flexible for reducing translation overhead and increasing the performance of applications with large memory footprint without application changes compared to hugetlb.
This statement needs a lot of justification. I don't see 1GB THP as viable for any workload. Opportunistic 1GB allocation is very questionable strategy.
Quoted 6 lines
Hello, Kirill!
I share your skepticism about opportunistic 1 GB allocations, however it might be useful if backed by an
madvise()annotations from userspace application. In this case, 1 GB THPs might be an alternative to 1 GB hugetlbfs pages, but with a more convenient interface.Quoted 5 lines
I have concerns if we would silently use 1~GB THPs in most scenarios where be would have used 2~MB THP. I'd appreciate a trigger to explicitly enable that -
MADV_HUGEPAGEis not sufficient because some applications relying on that assume that the THP size will be 2~MB (especially, if you want sparse, large VMAs).
Quoted 7 lines
This patchset is not intended to silently use 1GB THP in place of 2MB THP. First of all, there is a knob
/sys/kernel/mm/transparent_hugepage/enable_1GBto enable 1GB THP explicitly. Also, 1GB THP is allocated from a reserved CMA region (although I hadalloc_contig_pagesas a fallback, which can be removed in next version), so users need to add hugepage_cma=nG kernel parameter to enable 1GB THP allocation. If a finer control is necessary, we can add a newMADV_HUGEPAGE_1GBfor 1GB THP.
A global knob is insufficient. 1G pages will become a very precious resource as it requires a pre-allocation (reservation). So it really has to be an opt-in and the question is whether there is also some sort of access control needed.
Signature · 2 lines
Michal Hocko SUSE Labs
Rik van Riel
reviewerQuoted 6 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious resource as it requires a pre-allocation (reservation). So it really has
to be an opt-in and the question is whether there is also some sort of
access control needed.
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.
That makes it relatively easy to set aside
some fraction
of system memory in every system for 1GB and movable allocations, and use it for whatever way it is needed depending on what workload(s) end up running on a system.
Signature · 1 line
All Rights Reversed.
Quoted 15 lines · Rik van Riel
Quoted 6 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious resource as it requires a pre-allocation (reservation). So it really has
to be an opt-in and the question is whether there is also some sort of
access control needed.The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.
Quoted 6 lines
That makes it relatively easy to set aside
some fraction
of system memory in every system for 1GB and movable allocations, and use it for whatever way it is needed depending on what workload(s) end up running on a system.
Linking secretmem discussion
https://lkml.kernel.org/r/[email protected]
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerQuoted 9 lines · Rik van Riel
Quoted 6 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious resource as it requires a pre-allocation (reservation). So it really has
to be an opt-in and the question is whether there is also some sort of
access control needed.
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.
That CMA has to be pre-reserved, right? That requires a configuration.
Quoted 5 lines
That makes it relatively easy to set aside
some fraction
of system memory in every system for 1GB and movable allocations, and use it for whatever way it is needed depending on what workload(s) end up running on a system.
I was not talking about how easy or hard it is. My main concern is that this is effectively a pre-reserved pool and a global knob is a very suboptimal way to control access to it. I (rather) strongly believe this should be an explicit opt-in and ideally not 1GB specific but rather something to allow large pages to be created as there is a fit. See other subthread for more details.
Signature · 2 lines
Michal Hocko SUSE Labs
Rik van Riel
reviewerQuoted 13 lines · Michal Hocko
Quoted 12 lines · Rik van Riel
Quoted 9 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious
resource as it requires a pre-allocation (reservation). So it really
has
to be an opt-in and the question is whether there is also some sort
of
access control needed.
Quoted 8 lines
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
Signature · 1 line
All Rights Reversed.
Quoted 31 lines · Rik van Riel
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Rik van Riel
Quoted 9 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious
resource as it requires a pre-allocation (reservation). So it really
has
to be an opt-in and the question is whether there is also some sort
of
access control needed.Quoted 8 lines
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.
Quoted 4 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
I am not sure I like the trend towards CMA that we are seeing, reserving huge buffers for specific users (and eventually even doing it automatically).
What we actually want is ZONE_MOVABLE with relaxed guarantees, such that anybody who requires large, unmovable allocations can use it.
I once played with the idea of having ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback for ZONE_NORMAL for unmovable allocations, instead of running out of memory
If someone messes up the zone ratio, issues known from zone imbalances are avoided - large allocations simply become less likely to succeed. In contrast to ZONE_MOVABLE, memory offlining is not guaranteed to work.
Signature · 3 lines
Thanks, David / dhildenb
Rik van Riel
reviewerQuoted 35 lines · David Hildenbrand
Quoted 13 lines · Rik van Riel
Quoted 2 lines · Michal Hocko
That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.Quoted 4 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
I am not sure I like the trend towards CMA that we are seeing, reserving
huge buffers for specific users (and eventually even doing it automatically).What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that
anybody who requires large, unmovable allocations can use it.I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of
running out of memory
Quoted 5 lines
If someone messes up the zone ratio, issues known from zone imbalances
are avoided - large allocations simply become less likely to succeed. In
contrast toZONE_MOVABLE, memory offlining is not guaranteed to work.
I really like that idea. This will be easier to deal with than a "just the right size" CMA area, and seems like it would be pretty forgiving in both directions.
Keeping unmovable allocations
contained to one part of memory
should also make compaction within the ZONE_PREFER_MOVABLE area a lot easier than compaction for higher order allocations is today.
I suspect your proposal solves a lot of issues at once.
For (c) from your proposal, we could even claim a whole 2MB or even 1GB area at once for unmovable allocations, keeping those contained in a limited amount of physical memory again, to make life easier on compaction.
Signature · 1 line
All Rights Reversed.
Quoted 42 lines · Rik van Riel
Quoted 35 lines · David Hildenbrand
Quoted 13 lines · Rik van Riel
Quoted 2 lines · Michal Hocko
That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.Quoted 4 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
I am not sure I like the trend towards CMA that we are seeing, reserving
huge buffers for specific users (and eventually even doing it automatically).What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that
anybody who requires large, unmovable allocations can use it.I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of
running out of memoryQuoted 5 lines
If someone messes up the zone ratio, issues known from zone imbalances
are avoided - large allocations simply become less likely to succeed. In
contrast toZONE_MOVABLE, memory offlining is not guaranteed to work.
Quoted 4 lines
I really like that idea. This will be easier to deal with than a "just the right size" CMA area, and seems like it would be pretty forgiving in both directions.
Yes, and can be extended using memory hotplug.
Quoted 6 lines
Keeping unmovable allocations
contained to one part of memory
should also make compaction within theZONE_PREFER_MOVABLEarea a lot easier than compaction for higher order allocations is today.
Quoted 7 lines
I suspect your proposal solves a lot of issues at once.
For (c) from your proposal, we could even claim a whole 2MB or even 1GB area at once for unmovable allocations, keeping those contained in a limited amount of physical memory again, to make life easier on compaction.
Exactly, locally limiting unmovable allocations to a sane minimum.
(with some smart extra work, we could even convert ZONE_PREFER_MOVABLE to ZONE_NORMAL, one memory section/block at a time where needed, that direction always works. But that's very tricky.)
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewer[Cc Vlastimil and Mel - the whole email thread starts
http://lkml.kernel.org/r/[email protected]
but this particular subthread has diverged a bit and you might find it interesting]
Quoted 41 lines · David Hildenbrand
Quoted 31 lines · Rik van Riel
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Rik van Riel
Quoted 9 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious
resource as it requires a pre-allocation (reservation). So it really
has
to be an opt-in and the question is whether there is also some sort
of
access control needed.Quoted 8 lines
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.Quoted 4 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
I am not sure I like the trend towards CMA that we are seeing, reserving huge buffers for specific users (and eventually even doing it automatically).
Quoted 8 lines
What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that anybody who requires large, unmovable allocations can use it.I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of running out of memory
I might be missing something but how can this work longterm? Or put in another words why would this work any better than existing fragmentation avoidance techniques that page allocator implements already - movability grouping etc. Please note that I am not deeply familiar with those but my high level understanding is that we already try hard to not mix movable and unmovable objects in same page blocks as much as we can.
My suspicion is that a separate zone would work in a similar fashion. As long as there is a lot of free memory then zone will be effectively MOVABLE. Similar applies to normal zone when unmovable allocations are in minority. As long as the Normal zone gets full of unmovable objects they start overflowing to ZONE_PREFER_MOVABLE and it will resemble page block stealing when unmovable objects start spreading over movable page blocks.
Again, my level of expertise to page allocator is quite low so all the above might be simply wrong...
Quoted 3 lines
If someone messes up the zone ratio, issues known from zone imbalances are avoided - large allocations simply become less likely to succeed. In contrast to
ZONE_MOVABLE, memory offlining is not guaranteed to work.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 56 lines · Michal Hocko
[Cc Vlastimil and Mel - the whole email thread starts
http://lkml.kernel.org/r/[email protected]
but this particular subthread has diverged a bit and you might find it interesting]Quoted 41 lines · David Hildenbrand
Quoted 31 lines · Rik van Riel
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Rik van Riel
Quoted 9 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious
resource as it requires a pre-allocation (reservation). So it really
has
to be an opt-in and the question is whether there is also some sort
of
access control needed.Quoted 8 lines
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.Quoted 4 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
I am not sure I like the trend towards CMA that we are seeing, reserving huge buffers for specific users (and eventually even doing it automatically).
Quoted 8 lines
What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that anybody who requires large, unmovable allocations can use it.I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of running out of memory
Quoted 6 lines
I might be missing something but how can this work longterm? Or put in another words why would this work any better than existing fragmentation avoidance techniques that page allocator implements already - movability grouping etc. Please note that I am not deeply familiar with those but my high level understanding is that we already try hard to not mix movable and unmovable objects in same page blocks as much as we can.
Note that we group in pageblock granularity, which avoids fragmentation on a pageblock level, not on anything bigger than that. Especially MAX_ORDER - 1 pages (e.g., on x86-64) and gigantic pages.
So once you run for some time on a system (especially thinking about page shuffling *within* a zone), trying to allocate a gigantic page will simply always fail - even if you always had plenty of free memory in your single zone.
Quoted 4 lines
My suspicion is that a separate zone would work in a similar fashion. As long as there is a lot of free memory then zone will be effectively MOVABLE. Similar applies to normal zone when unmovable allocations are
Note the difference to MOVABLE: if you really want, you *can* put movable allocations into that zone. So you can happily allocate gigantic pages from it. Or anything else you like. As the name suggests "prefer movable allocations".
Quoted 4 lines
in minority. As long as the Normal zone gets full of unmovable objects they start overflowing to
ZONE_PREFER_MOVABLEand it will resemble page block stealing when unmovable objects start spreading over movable page blocks.
Right, the long-term goal would be
1. To limit the chance of that happening. (e.g., size it in a way that's safe for 99.9% of all setups, resize dynamically on demand)
2. To limit the physical area where that is happening (e.g., find lowest possible pageblock etc.). That's more tricky but I consider this a pure optimization on top.
As long as we stay in safe zone boundaries you get a benefit in most scenarios. As soon as we would have a (temporary) workload that would require more unmovable allocations we would fallback to polluting some pageblocks only.
Quoted 3 lines
Again, my level of expertise to page allocator is quite low so all the above might be simply wrong...
Same over here. I had this idea in my mind for quite a while but obviously didn't get to figure out the details/implement yet - that's why I decided to share the basic idea just now.
Signature · 3 lines
Thanks, David / dhildenb
Zi Yan
reviewerQuoted 95 lines · David Hildenbrand
Quoted 56 lines · Michal Hocko
[Cc Vlastimil and Mel - the whole email thread starts
http://lkml.kernel.org/r/[email protected]
but this particular subthread has diverged a bit and you might find it interesting]Quoted 41 lines · David Hildenbrand
Quoted 31 lines · Rik van Riel
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Rik van Riel
Quoted 9 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious
resource as it requires a pre-allocation (reservation). So it really
has
to be an opt-in and the question is whether there is also some sort
of
access control needed.Quoted 8 lines
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.Quoted 4 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
I am not sure I like the trend towards CMA that we are seeing, reserving huge buffers for specific users (and eventually even doing it automatically).
Quoted 8 lines
What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that anybody who requires large, unmovable allocations can use it.I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of running out of memoryQuoted 6 lines
I might be missing something but how can this work longterm? Or put in another words why would this work any better than existing fragmentation avoidance techniques that page allocator implements already - movability grouping etc. Please note that I am not deeply familiar with those but my high level understanding is that we already try hard to not mix movable and unmovable objects in same page blocks as much as we can.
Note that we group in pageblock granularity, which avoids fragmentation on a pageblock level, not on anything bigger than that. Especially
MAX_ORDER- 1 pages (e.g., on x86-64) and gigantic pages.So once you run for some time on a system (especially thinking about page shuffling *within* a zone), trying to allocate a gigantic page will simply always fail - even if you always had plenty of free memory in your single zone.
Quoted 4 lines
My suspicion is that a separate zone would work in a similar fashion. As long as there is a lot of free memory then zone will be effectively MOVABLE. Similar applies to normal zone when unmovable allocations are
Note the difference to MOVABLE: if you really want, you *can* put movable allocations into that zone. So you can happily allocate gigantic pages from it. Or anything else you like. As the name suggests "prefer movable allocations".
Quoted 4 lines
in minority. As long as the Normal zone gets full of unmovable objects they start overflowing to
ZONE_PREFER_MOVABLEand it will resemble page block stealing when unmovable objects start spreading over movable page blocks.Right, the long-term goal would be
1. To limit the chance of that happening. (e.g., size it in a way that's safe for 99.9% of all setups, resize dynamically on demand)
2. To limit the physical area where that is happening (e.g., find lowest possible pageblock etc.). That's more tricky but I consider this a pure optimization on top.
Quoted 4 lines
As long as we stay in safe zone boundaries you get a benefit in most scenarios. As soon as we would have a (temporary) workload that would require more unmovable allocations we would fallback to polluting some pageblocks only.
The idea would work well until unmoveable pages begin to overflow into ZONE_PREFER_MOVABLE or we move the boundary of ZONE_PREFER_MOVABLE to avoid unmoveable page overflow. The issue comes from the lifetime of the unmoveable pages. Since some long-live ones can be around the boundary, there is no guarantee that ZONE_PREFER_MOVABLE cannot grow back even if other unmoveable pages are deallocated. Ultimately, ZONE_PREFER_MOVABLE would be shrink to a small size and the situation is back to what we have now.
OK. I have a stupid question here. Why not just grow pageblock to a larger size, like 1GB? So the fragmentation of unmoveable pages will be at larger granularity. But it is less likely unmoveable pages will be allocated at a movable pageblock, since the kernel has 1GB pageblock for them after a pageblock stealing. If other kinds of pageblocks run out, moveable and reclaimable pages can fall back to unmoveable pageblocks.
What am I missing here?
Thanks.
—
Best Regards,
Yan Zi
Quoted 5 lines
Quoted 4 lines
As long as we stay in safe zone boundaries you get a benefit in most scenarios. As soon as we would have a (temporary) workload that would require more unmovable allocations we would fallback to polluting some pageblocks only.
Quoted 8 lines
The idea would work well until unmoveable pages begin to overflow into
ZONE_PREFER_MOVABLEor we move the boundary ofZONE_PREFER_MOVABLEto avoid unmoveable page overflow. The issue comes from the lifetime of the unmoveable pages. Since some long-live ones can be around the boundary, there is no guarantee thatZONE_PREFER_MOVABLEcannot grow back even if other unmoveable pages are deallocated. Ultimately,ZONE_PREFER_MOVABLEwould be shrink to a small size and the situation is back to what we have now.
As discussed this would not happen in the usual case in case we size it reasonable. Of course, if you push it to the extreme (which was never suggested!), you would create mess. There is always a way to create a mess if you abuse such mechanism. Also see Rik's reply regarding reclaim.
Quoted 8 lines
OK. I have a stupid question here. Why not just grow pageblock to a larger size, like 1GB? So the fragmentation of unmoveable pages will be at larger granularity. But it is less likely unmoveable pages will be allocated at a movable pageblock, since the kernel has 1GB pageblock for them after a pageblock stealing. If other kinds of pageblocks run out, moveable and reclaimable pages can fall back to unmoveable pageblocks.
What am I missing here?
Oh no. For example pageblocks have to completely fit into a single section (that's where metadata is maintained). Please refrain from suggesting to increase the section size ;)
There is plenty of code relying on pageblocks/MAX_ORDER - 1 to be reasonable in size. Examples in VMs are free page reporting or virtio-mem.
Signature · 3 lines
Thanks, David / dhildenb
Zi Yan
reviewerQuoted 28 lines · David Hildenbrand
Quoted 5 lines
Quoted 4 lines
As long as we stay in safe zone boundaries you get a benefit in most scenarios. As soon as we would have a (temporary) workload that would require more unmovable allocations we would fallback to polluting some pageblocks only.
Quoted 8 lines
The idea would work well until unmoveable pages begin to overflow into
ZONE_PREFER_MOVABLEor we move the boundary ofZONE_PREFER_MOVABLEto avoid unmoveable page overflow. The issue comes from the lifetime of the unmoveable pages. Since some long-live ones can be around the boundary, there is no guarantee thatZONE_PREFER_MOVABLEcannot grow back even if other unmoveable pages are deallocated. Ultimately,ZONE_PREFER_MOVABLEwould be shrink to a small size and the situation is back to what we have now.As discussed this would not happen in the usual case in case we size it reasonable. Of course, if you push it to the extreme (which was never suggested!), you would create mess. There is always a way to create a mess if you abuse such mechanism. Also see Rik's reply regarding reclaim.
Quoted 8 lines
OK. I have a stupid question here. Why not just grow pageblock to a larger size, like 1GB? So the fragmentation of unmoveable pages will be at larger granularity. But it is less likely unmoveable pages will be allocated at a movable pageblock, since the kernel has 1GB pageblock for them after a pageblock stealing. If other kinds of pageblocks run out, moveable and reclaimable pages can fall back to unmoveable pageblocks.
What am I missing here?
Quoted 3 lines
Oh no. For example pageblocks have to completely fit into a single section (that's where metadata is maintained). Please refrain from suggesting to increase the section size ;)
Thank you for the explanation. I have no idea about the restrictions on pageblock and section. Out of curiosity, what prevents the growth of the section size?
—
Best Regards,
Yan Zi
Quoted 30 lines · Zi Yan
Quoted 28 lines · David Hildenbrand
Quoted 5 lines
Quoted 4 lines
As long as we stay in safe zone boundaries you get a benefit in most scenarios. As soon as we would have a (temporary) workload that would require more unmovable allocations we would fallback to polluting some pageblocks only.
Quoted 8 lines
The idea would work well until unmoveable pages begin to overflow into
ZONE_PREFER_MOVABLEor we move the boundary ofZONE_PREFER_MOVABLEto avoid unmoveable page overflow. The issue comes from the lifetime of the unmoveable pages. Since some long-live ones can be around the boundary, there is no guarantee thatZONE_PREFER_MOVABLEcannot grow back even if other unmoveable pages are deallocated. Ultimately,ZONE_PREFER_MOVABLEwould be shrink to a small size and the situation is back to what we have now.As discussed this would not happen in the usual case in case we size it reasonable. Of course, if you push it to the extreme (which was never suggested!), you would create mess. There is always a way to create a mess if you abuse such mechanism. Also see Rik's reply regarding reclaim.
Quoted 8 lines
OK. I have a stupid question here. Why not just grow pageblock to a larger size, like 1GB? So the fragmentation of unmoveable pages will be at larger granularity. But it is less likely unmoveable pages will be allocated at a movable pageblock, since the kernel has 1GB pageblock for them after a pageblock stealing. If other kinds of pageblocks run out, moveable and reclaimable pages can fall back to unmoveable pageblocks.
What am I missing here?
Quoted 7 lines
Quoted 3 lines
Oh no. For example pageblocks have to completely fit into a single section (that's where metadata is maintained). Please refrain from suggesting to increase the section size ;)
Thank you for the explanation. I have no idea about the restrictions on pageblock and section. Out of curiosity, what prevents the growth of the section size?
The section size (and based on that the Linux memory block size) defines
- the minimum size in which we can add_memory()
- the alignment requirement in which we can add_memory()
This is applicable
- in physical environments, where the bios will decide where to place DIMMs/NVDIMMs. The coarser the granularity, the less memory we might be able to make use of in corner cases.
- in virtualized environments, where we want to add memory in fairly small granularity. The coarser the granularity, the less flexibility we have.
arm64 has a section size of 1GB (and a THP/MAX_ORDER - 1 size of 512MB with 64k base pages :/ ). That already turned out to be a problem - see
[1] regarding thoughts on how to shrink the section size. I once read about thoughts of switching to 2MB THP on arm64 with any base page size, not sure if that will become real at one point (and we might be able to reduce the pageblock size there as well ... )
[1]
https://lkml.kernel.org/r/AM6PR08MB40690714A2E77A7128B2B2ADF7700@AM6PR08MB4069.eurprd08.prod.outlook.com
See [1] as
Quoted 5 lines
—
Best Regards,
Yan Zi
Signature · 3 lines
Thanks, David / dhildenb
Rik van Riel
reviewerQuoted 25 lines · Michal Hocko
[Cc Vlastimil and Mel - the whole email thread starts
http://lkml.kernel.org/r/[email protected]
but this particular subthread has diverged a bit and you might find
it
interesting]Quoted 10 lines · David Hildenbrand
I am not sure I like the trend towards CMA that we are seeing, reserving
huge buffers for specific users (and eventually even doing it automatically).What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that
anybody who requires large, unmovable allocations can use it.Quoted 7 lines
I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of
running out of memory
Quoted 5 lines
I might be missing something but how can this work longterm? Or put in
another words why would this work any better than existing fragmentation
avoidance techniques that page allocator implements already -
One big difference is reclaim. If ZONE_NORMAL runs low on free memory, page reclaim would kick in and evict some movable/reclaimable things, to free up more space for unmovable allocations.
The current fragmentation avoidance techniques don't do things like reclaim, or proactively migrating movable pages out of unmovable page blocks to prevent unmovable allocations in currently movable page blocks.
Quoted 4 lines
My suspicion is that a separate zone would work in a similar fashion. As
long as there is a lot of free memory then zone will be effectively MOVABLE. Similar applies to normal zone when unmovable allocations
Quoted 8 lines
are
in minority. As long as the Normal zone gets full of unmovable objects
they start overflowing toZONE_PREFER_MOVABLEand it will resemble page
block stealing when unmovable objects start spreading over movable page
blocks.
You are right, with the difference being reclaim and/or migration, which could make a real difference in limiting the number of pageblocks that have unmovable allocations.
Signature · 1 line
All Rights Reversed.
Zi Yan
reviewerQuoted 37 lines · Rik van Riel
Quoted 25 lines · Michal Hocko
[Cc Vlastimil and Mel - the whole email thread starts
http://lkml.kernel.org/r/[email protected]
but this particular subthread has diverged a bit and you might find
it
interesting]Quoted 10 lines · David Hildenbrand
I am not sure I like the trend towards CMA that we are seeing, reserving
huge buffers for specific users (and eventually even doing it automatically).What we actually want is
ZONE_MOVABLEwith relaxed guarantees, such that
anybody who requires large, unmovable allocations can use it.Quoted 7 lines
I once played with the idea of having
ZONE_PREFER_MOVABLE, which
a) Is the primary choice for movable allocations
b) Is allowed to contain unmovable allocations (esp., gigantic pages)
c) Is the fallback forZONE_NORMALfor unmovable allocations, instead of
running out of memoryQuoted 5 lines
I might be missing something but how can this work longterm? Or put in
another words why would this work any better than existing fragmentation
avoidance techniques that page allocator implements already -One big difference is reclaim. If
ZONE_NORMALruns low on free memory, page reclaim would kick in and evict some movable/reclaimable things, to free up more space for unmovable allocations.
Quoted 4 lines
The current fragmentation avoidance techniques don't do things like reclaim, or proactively migrating movable pages out of unmovable page blocks to prevent unmovable allocations in currently movable page blocks.
Isn’t Mel Gorman’s watermark boost patch[1] (merged about a year ago) doing what you are describing?
[1]https://lore.kernel.org/linux-mm/[email protected]/
—
Best Regards,
Yan Zi
Michal Hocko
reviewerQuoted 31 lines · Rik van Riel
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Rik van Riel
Quoted 9 lines · Michal Hocko
A global knob is insufficient. 1G pages will become a very precious
resource as it requires a pre-allocation (reservation). So it really
has
to be an opt-in and the question is whether there is also some sort
of
access control needed.Quoted 8 lines
Quoted 5 lines
The 1GB pages do not require that much in the way of pre-allocation. The memory can be obtained through CMA, which means it can be used for movable 4kB and 2MB allocations when not
being used for 1GB pages.That CMA has to be pre-reserved, right? That requires a configuration.
To some extent, yes.
However, because that pool can be used for movable
4kB and 2MB
pages as well as for 1GB pages, it would be easy to just set the size of that pool to eg. 1/3 or even 1/2 of memory for every system.
Quoted 3 lines
It isn't like the pool needs to be the exact right size. We just need to avoid the "highmem problem" of having too little memory for kernel allocations.
Which is the problem why this is not really suitable for an uneducated guesses. It is really hard to guess the right amount of lowmem. Think of heavy fs metadata workloads and their memory demand. Memory reclaim usually struggles when zones are imbalanced from my experience.
Signature · 2 lines
Michal Hocko SUSE Labs