Skip to contentSkip to conversation

Add PUD and kernel PTE level pagetable account

PATCH In review Baolin Wang 3 participants 10 messages Jul 6, 2022 – Jul 10, 2022

Subsystems (12)
Memory management x86 MIPS MM core +8 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Add PUD and kernel PTE level pagetable account

Hi,

Now we will miss to account the PUD level pagetable and kernel PTE level pagetable, as well as missing to set the PG_table flags for these pagetable pages, which will get an inaccurate pagetable accounting, and miss PageTable() validation in some cases. So this patch set introduces new helpers to help to account PUD and kernel PTE pagetable pages.

Note there are still some architectures specific pagetable allocation that need to account the pagetable pages, which need more investigation and cleanup in future.

Changes from RFC v3:
- Rebased on 20220706 linux-next.
- Introduce new pgtable_pud_page_ctor/dtor() and rename the helpers.
- Change back to use inc_lruvec_page_state()/dec_lruvec_page_state().
- Update some commit message.

Changes from RFC v2:
- Convert to use mod_lruvec_page_state() for non-order-0 case.
- Rename the helpers.
- Update some commit messages.
- Remove unnecessary __GFP_HIGHMEM clear.

Changes from RFC v1:
- Update some commit message.
- Add missing pgtable_clear_and_dec() on X86 arch.
- Use __free_page() to free pagetable which can avoid duplicated virt_to_page().

Baolin Wang (3):
mm: Factor out the pagetable pages account into new helper function
mm: Add PUD level pagetable account
mm: Add kernel PTE level pagetable pages account

 arch/arm64/include/asm/tlb.h         |  5 ++++-
 arch/csky/include/asm/pgalloc.h      |  2 +-
 arch/loongarch/include/asm/pgalloc.h | 12 +++++++++---
 arch/microblaze/mm/pgtable.c         |  2 +-
 arch/mips/include/asm/pgalloc.h      | 12 +++++++++---
 arch/openrisc/mm/ioremap.c           |  2 +-
 arch/x86/mm/pgtable.c                |  7 +++++--
 include/asm-generic/pgalloc.h        | 26 ++++++++++++++++++++++----
 include/linux/mm.h                   | 34 ++++++++++++++++++++++++++--------
 9 files changed, 78 insertions(+), 24 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 mm: Factor out the pagetable pages account into new helper function

Factor out the pagetable pages account and pagetable setting into new helper functions to avoid duplicated code. Meanwhile these helper functions also will be used to account pagetable pages which do not need split pagetable lock.

---
 include/linux/mm.h | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
include/linux/mm.h +16 -8
index d084ce5..7894bc5 100644
@@ -2352,20 +2352,30 @@ static inline void pgtable_init(void)
 	pgtable_cache_init();
 }
 
+static inline void page_set_pgtable(struct page *page)
+{
+	__SetPageTable(page);
+	inc_lruvec_page_state(page, NR_PAGETABLE);
+}
+
+static inline void page_clear_pgtable(struct page *page)
+{
+	__ClearPageTable(page);
+	dec_lruvec_page_state(page, NR_PAGETABLE);
+}
+
 static inline bool pgtable_pte_page_ctor(struct page *page)
 {
 	if (!ptlock_init(page))
 		return false;
-	__SetPageTable(page);
-	inc_lruvec_page_state(page, NR_PAGETABLE);
+	page_set_pgtable(page);
 	return true;
 }
 
 static inline void pgtable_pte_page_dtor(struct page *page)
 {
 	ptlock_free(page);
-	__ClearPageTable(page);
-	dec_lruvec_page_state(page, NR_PAGETABLE);
+	page_clear_pgtable(page);
 }
 
 #define pte_offset_map_lock(mm, pmd, address, ptlp)	\
@@ -2451,16 +2461,14 @@ static inline bool pgtable_pmd_page_ctor(struct page *page)
 {
 	if (!pmd_ptlock_init(page))
 		return false;
-	__SetPageTable(page);
-	inc_lruvec_page_state(page, NR_PAGETABLE);
+	page_set_pgtable(page);
 	return true;
 }
 
 static inline void pgtable_pmd_page_dtor(struct page *page)
 {
 	pmd_ptlock_free(page);
-	__ClearPageTable(page);
-	dec_lruvec_page_state(page, NR_PAGETABLE);
+	page_clear_pgtable(page);
 }
 
 /*
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 mm: Add PUD level pagetable account

In reply to Baolin Wang

Now the PUD level ptes are always protected by mm->page_table_lock, which means no split pagetable lock needed. So the generic PUD level pagetable pages allocation will not call pgtable_pte_page_ctor/dtor(), that means we will miss to account PUD level pagetable pages.

So introducing pgtable_pud_page_ctor/dtor(), which are just wrappers of page_{set,clear}_pgtable() to help to get an accurate PUD pagetable accounting, when allocating or freeing PUD level pagetable pages.

Moreover this patch will also mark the PUD level pagetable with PG_table flag, which will help to do sanity validation in unpoison_memory() and get more accurate pagetable accounting by /proc/kpageflags interface.

Meanwhile converting the architectures with using generic PUD pagatable allocation to add corresponding pgtable_pud_page_ctor() or pgtable_pud_page_dtor() to account PUD level pagetable.

---
 arch/arm64/include/asm/tlb.h         |  5 ++++-
 arch/loongarch/include/asm/pgalloc.h | 12 +++++++++---
 arch/mips/include/asm/pgalloc.h      | 12 +++++++++---
 arch/x86/mm/pgtable.c                |  5 ++++-
 include/asm-generic/pgalloc.h        | 12 ++++++++++--
 include/linux/mm.h                   | 10 ++++++++++
 6 files changed, 46 insertions(+), 10 deletions(-)
arch/arm64/include/asm/tlb.h +4 -1
index c995d1f..6665f33 100644
@@ -94,7 +94,10 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
 static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,
 				  unsigned long addr)
 {
-	tlb_remove_table(tlb, virt_to_page(pudp));
+	struct page *page = virt_to_page(pudp);
+
+	pgtable_pud_page_dtor(page);
+	tlb_remove_table(tlb, page);
 }
 #endif
 
arch/loongarch/include/asm/pgalloc.h +9 -3
index 4bfeb3c..8138101 100644
@@ -89,10 +89,16 @@ static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long address)
 {
 	pud_t *pud;
+	struct page *page;
+
+	page = alloc_page(GFP_KERNEL);
+	if (!page)
+		return NULL;
+
+	pgtable_pud_page_ctor(page);
+	pud = (pud_t *)page_address(page);
+	pud_init((unsigned long)pud, (unsigned long)invalid_pmd_table);
 
-	pud = (pud_t *) __get_free_page(GFP_KERNEL);
-	if (pud)
-		pud_init((unsigned long)pud, (unsigned long)invalid_pmd_table);
 	return pud;
 }
 
arch/mips/include/asm/pgalloc.h +9 -3
index 7960357..5da5880 100644
@@ -89,11 +89,17 @@ static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
 
 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long address)
 {
+	struct page *page;
 	pud_t *pud;
 
-	pud = (pud_t *) __get_free_pages(GFP_KERNEL, PUD_TABLE_ORDER);
-	if (pud)
-		pud_init((unsigned long)pud, (unsigned long)invalid_pmd_table);
+	page = alloc_pages(GFP_KERNEL, PUD_TABLE_ORDER);
+	if (!page)
+		return NULL;
+
+	pgtable_pud_page_ctor(page);
+	pud = (pud_t *)page_address(page);
+	pud_init((unsigned long)pud, (unsigned long)invalid_pmd_table);
+
 	return pud;
 }
 
arch/x86/mm/pgtable.c +4 -1
index a932d77..ea39670 100644
@@ -76,8 +76,11 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
 #if CONFIG_PGTABLE_LEVELS > 3
 void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
 {
+	struct page *page = virt_to_page(pud);
+
+	pgtable_pud_page_dtor(page);
 	paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
-	paravirt_tlb_remove_table(tlb, virt_to_page(pud));
+	paravirt_tlb_remove_table(tlb, page);
 }
 
 #if CONFIG_PGTABLE_LEVELS > 4
include/asm-generic/pgalloc.h +10 -2
index 977bea1..8ce8d7c 100644
@@ -149,11 +149,16 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
 
 static inline pud_t *__pud_alloc_one(struct mm_struct *mm, unsigned long addr)
 {
+	struct page *page;
 	gfp_t gfp = GFP_PGTABLE_USER;
 
 	if (mm == &init_mm)
 		gfp = GFP_PGTABLE_KERNEL;
-	return (pud_t *)get_zeroed_page(gfp);
+	page = alloc_pages(gfp, 0);
+	if (!page)
+		return NULL;
+	pgtable_pud_page_ctor(page);
+	return (pud_t *)page_address(page);
 }
 
 #ifndef __HAVE_ARCH_PUD_ALLOC_ONE
@@ -174,8 +179,11 @@ static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
 
 static inline void __pud_free(struct mm_struct *mm, pud_t *pud)
 {
+	struct page *page = virt_to_page(pud);
+
 	BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
-	free_page((unsigned long)pud);
+	pgtable_pud_page_dtor(page);
+	__free_page(page);
 }
 
 #ifndef __HAVE_ARCH_PUD_FREE
include/linux/mm.h +10 -0
index 7894bc5..54ed6f7 100644
@@ -2364,6 +2364,16 @@ static inline void page_clear_pgtable(struct page *page)
 	dec_lruvec_page_state(page, NR_PAGETABLE);
 }
 
+static inline void pgtable_pud_page_ctor(struct page *page)
+{
+	page_set_pgtable(page);
+}
+
+static inline void pgtable_pud_page_dtor(struct page *page)
+{
+	page_clear_pgtable(page);
+}
+
 static inline bool pgtable_pte_page_ctor(struct page *page)
 {
 	if (!ptlock_init(page))
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/3 mm: Add kernel PTE level pagetable pages account

In reply to Baolin Wang

Now the kernel PTE level ptes are always protected by mm->page_table_lock instead of split pagetable lock, so the kernel PTE level pagetable pages are not accounted. Especially the vmalloc()/vmap() can consume lots of kernel pagetable, so to get an accurate pagetable accounting, calling new helpers page_{set,clear}_pgtable() when allocating or freeing a kernel PTE level pagetable page.

Meanwhile converting architectures to use corresponding generic PTE pagetable allocation and freeing functions.

Note this patch only adds accounting to the page tables allocated after boot.

---
 arch/csky/include/asm/pgalloc.h |  2 +-
 arch/microblaze/mm/pgtable.c    |  2 +-
 arch/openrisc/mm/ioremap.c      |  2 +-
 arch/x86/mm/pgtable.c           |  2 +-
 include/asm-generic/pgalloc.h   | 14 ++++++++++++--
 5 files changed, 16 insertions(+), 6 deletions(-)
arch/csky/include/asm/pgalloc.h +1 -1
index 7d57e5d..56f8d25 100644
@@ -29,7 +29,7 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
 	pte_t *pte;
 	unsigned long i;
 
-	pte = (pte_t *) __get_free_page(GFP_KERNEL);
+	pte = __pte_alloc_one_kernel(mm);
 	if (!pte)
 		return NULL;
 
arch/microblaze/mm/pgtable.c +1 -1
index 9f73265..e96dd1b 100644
@@ -245,7 +245,7 @@ unsigned long iopa(unsigned long addr)
 __ref pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
 {
 	if (mem_init_done)
-		return (pte_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
+		return __pte_alloc_one_kernel(mm);
 	else
 		return memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
 					      MEMBLOCK_LOW_LIMIT,
arch/openrisc/mm/ioremap.c +1 -1
index daae13a..3453acc 100644
@@ -118,7 +118,7 @@ pte_t __ref *pte_alloc_one_kernel(struct mm_struct *mm)
 	pte_t *pte;
 
 	if (likely(mem_init_done)) {
-		pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
+		pte = __pte_alloc_one_kernel(mm);
 	} else {
 		pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
 		if (!pte)
arch/x86/mm/pgtable.c +1 -1
index ea39670..20f3076 100644
@@ -858,7 +858,7 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
 	/* INVLPG to clear all paging-structure caches */
 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE-1);
 
-	free_page((unsigned long)pte);
+	pte_free_kernel(NULL, pte);
 
 	return 1;
 }
include/asm-generic/pgalloc.h +12 -2
index 8ce8d7c..cd8420f 100644
@@ -18,7 +18,14 @@
  */
 static inline pte_t *__pte_alloc_one_kernel(struct mm_struct *mm)
 {
-	return (pte_t *)__get_free_page(GFP_PGTABLE_KERNEL);
+	struct page *page;
+	gfp_t gfp = GFP_PGTABLE_KERNEL;
+
+	page = alloc_pages(gfp, 0);
+	if (!page)
+		return NULL;
+	page_set_pgtable(page);
+	return (pte_t *)page_address(page);
 }
 
 #ifndef __HAVE_ARCH_PTE_ALLOC_ONE_KERNEL
@@ -41,7 +48,10 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
  */
 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
 {
-	free_page((unsigned long)pte);
+	struct page *page = virt_to_page(pte);
+
+	page_clear_pgtable(page);
+	__free_page(page);
 }
 
 /**
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [PATCH 3/3] mm: Add kernel PTE level pagetable pages account

Quoted 7 lines · Baolin Wang

Now the kernel PTE level ptes are always protected by mm->page_table_lock instead of split pagetable lock, so the kernel PTE level pagetable pages are not accounted. Especially the vmalloc()/vmap() can consume lots of kernel pagetable, so to get an accurate pagetable accounting, calling new helpers page_{set,clear}_pgtable() when allocating or freeing a kernel PTE level pagetable page.

Quoted 7 lines

Meanwhile converting architectures to use corresponding generic PTE pagetable allocation and freeing functions.

Note this patch only adds accounting to the page tables allocated after boot.

What does this Reported-by: even mean? the kernel test robot told you that the page tables weren't being accounted?

I don't understand why we want to start accounting kernel page tables. an we have a *discussion* about that with a sensible thread name instead of just trying to sneak it in as patch 3/3?

#5 lore raw

Re: [PATCH 3/3] mm: Add kernel PTE level pagetable pages account

Quoted 11 lines · Matthew Wilcox
Quoted 10 lines · Baolin Wang

Now the kernel PTE level ptes are always protected by mm->page_table_lock instead of split pagetable lock, so the kernel PTE level pagetable pages are not accounted. Especially the vmalloc()/vmap() can consume lots of kernel pagetable, so to get an accurate pagetable accounting, calling new helpers page_{set,clear}_pgtable() when allocating or freeing a kernel PTE level pagetable page.

Meanwhile converting architectures to use corresponding generic PTE pagetable allocation and freeing functions.

Quoted 7 lines
Quoted 4 lines

Note this patch only adds accounting to the page tables allocated after boot.

What does this Reported-by: even mean? the kernel test robot told you that the page tables weren't being accounted?

I fixed an issue reported by this robot. OK, I can remove the tag.

Quoted 3 lines

I don't understand why we want to start accounting kernel page tables. an we have a *discussion* about that with a sensible thread name instead of just trying to sneak it in as patch 3/3?

I think I have replied to you in below link [1]. The reason is we should keep consistent with PMD or PUD pagetable allocation.

[1] https://lore.kernel.org/all/[email protected]/

#6 lore raw
In reply to Baolin Wang
Quoted 5 lines · Baolin Wang

Now we will miss to account the PUD level pagetable and kernel PTE level pagetable, as well as missing to set the PG_table flags for these pagetable pages, which will get an inaccurate pagetable accounting, and miss PageTable() validation in some cases. So this patch set introduces new helpers to help to account PUD and kernel PTE pagetable pages.

Could you explain the motivation for this series a bit more? Is there a real-world problem that this fixes?

#7 lore raw
Quoted 9 lines · Dave Hansen
Quoted 5 lines · Baolin Wang

Now we will miss to account the PUD level pagetable and kernel PTE level pagetable, as well as missing to set the PG_table flags for these pagetable pages, which will get an inaccurate pagetable accounting, and miss PageTable() validation in some cases. So this patch set introduces new helpers to help to account PUD and kernel PTE pagetable pages.

Could you explain the motivation for this series a bit more? Is there a real-world problem that this fixes?

Not fix real problem. The motivation is that making the pagetable accounting more accurate, which helps us to analyse the consumption of the pagetable pages in some cases, and maybe help to do some empty pagetable reclaiming in future.

#8 lore raw
Quoted 9 lines · Baolin Wang
Quoted 8 lines · Dave Hansen
Quoted 6 lines · Baolin Wang

Now we will miss to account the PUD level pagetable and kernel PTE level pagetable, as well as missing to set the PG_table flags for these pagetable
pages, which will get an inaccurate pagetable accounting, and miss PageTable() validation in some cases. So this patch set introduces new helpers to help to account PUD and kernel PTE pagetable pages.

Quoted 7 lines
Quoted 2 lines

Could you explain the motivation for this series a bit more?  Is there a real-world problem that this fixes?

Not fix real problem. The motivation is that making the pagetable accounting more accurate, which helps us to analyse the consumption of the pagetable pages in some cases, and maybe help to do some empty pagetable reclaiming in future.

This accounting isn't free. It costs storage (and also parts of cachelines) in each mm and CPU time to maintain it, plus maintainer eyeballs to maintain. PUD pages are also fundamentally (on x86 at least) 0.0004% of the overhead of PTE and 0.2% of the overhead of PMD pages unless someone is using gigantic hugetlbfs mappings.

Even with 1G gigantic pages, you would need a quarter of a million (well, 262144 or 512*512) mappings of one 1G page to consume 1G of memory on PUD pages.

That just doesn't seem like something anyone is likely to actually do in practice. That makes the benefits of the PUD portion of this series rather unclear in the real world.

As for the kernel page tables, I'm not really aware of them causing any problems. We have a pretty good idea how much space they consume from the DirectMap* entries in meminfo:

	DirectMap4k:     2262720 kB
	DirectMap2M:    40507392 kB
	DirectMap1G:    24117248 kB

as well as our page table debugging infrastructure. I haven't found myself dying for more specific info on them.

So, nothing in this series seems like a *BAD* idea, but I'm not sure in the end it solves more problems than it creates.

#9 lore raw
Quoted 18 lines · Dave Hansen
Quoted 9 lines · Baolin Wang
Quoted 8 lines · Dave Hansen
Quoted 6 lines · Baolin Wang

Now we will miss to account the PUD level pagetable and kernel PTE level pagetable, as well as missing to set the PG_table flags for these pagetable
pages, which will get an inaccurate pagetable accounting, and miss PageTable() validation in some cases. So this patch set introduces new helpers to help to account PUD and kernel PTE pagetable pages.

Quoted 7 lines
Quoted 2 lines

Could you explain the motivation for this series a bit more?  Is there a real-world problem that this fixes?

Not fix real problem. The motivation is that making the pagetable accounting more accurate, which helps us to analyse the consumption of the pagetable pages in some cases, and maybe help to do some empty pagetable reclaiming in future.

Quoted 5 lines

This accounting isn't free. It costs storage (and also parts of cachelines) in each mm and CPU time to maintain it, plus maintainer eyeballs to maintain. PUD pages are also fundamentally (on x86 at least) 0.0004% of the overhead of PTE and 0.2% of the overhead of PMD pages unless someone is using gigantic hugetlbfs mappings.

Yes, agree. However I think the performence influence of this patch is small from some testing I did (like mysql, no obvious performance influence). Moreover the pagetable accounting gap is about 1% from below testing data.

Without this patchset, the pagetable consumption is about 110M with mysql testing.
flags page-count MB symbolic-flags
long-symbolic-flags
0x0000000004000000 28232 110 __________________________g__________________ pgtable

With this patchset, and the consumption is about 111M.
flags page-count MB symbolic-flags
long-symbolic-flags
0x0000000004000000 28459 111 __________________________g__________________ pgtable

Quoted 8 lines

Even with 1G gigantic pages, you would need a quarter of a million (well, 262144 or 512*512) mappings of one 1G page to consume 1G of memory on PUD pages.

That just doesn't seem like something anyone is likely to actually do in practice. That makes the benefits of the PUD portion of this series rather unclear in the real world.

Quoted 7 lines

As for the kernel page tables, I'm not really aware of them causing any problems. We have a pretty good idea how much space they consume from the DirectMap* entries in meminfo:

	DirectMap4k:     2262720 kB
	DirectMap2M:    40507392 kB
	DirectMap1G:    24117248 kB

However these statistics are arch-specific information, which only available on x86, s390 and powerpc.

Quoted 5 lines

as well as our page table debugging infrastructure. I haven't found myself dying for more specific info on them.

So, nothing in this series seems like a *BAD* idea, but I'm not sure in the end it solves more problems than it creates.

Thanks for your input.