* [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages
@ 2024-07-01 8:46 Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 1/7] mm: pgtable: make pte_offset_map_nolock() return pmdval Qi Zheng
` (8 more replies)
0 siblings, 9 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
Hi all,
Previously, we tried to use a completely asynchronous method to reclaim empty
user PTE pages [1]. After discussing with David Hildenbrand, we decided to
implement synchronous reclaimation in the case of madvise(MADV_DONTNEED) as the
first step.
So this series aims to synchronously scan and reclaim empty user PTE pages in
zap_page_range_single() (madvise(MADV_DONTNEED) etc will invoke this). In
zap_page_range_single(), mmu_gather is used to perform batch tlb flushing and
page freeing operations. Therefore, if we want to free the empty PTE page in
this path, the most natural way is to add it to mmu_gather as well. There are
two problems that need to be solved here:
1. Now, if CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected, mmu_gather will free
page table pages by semi RCU:
- batch table freeing: asynchronous free by RCU
- single table freeing: IPI + synchronous free
But this is not enough to free the empty PTE page table pages in paths other
that munmap and exit_mmap path, because IPI cannot be synchronized with
rcu_read_lock() in pte_offset_map{_lock}(). So we should let single table
also be freed by RCU like batch table freeing.
2. When we use mmu_gather to batch flush tlb and free PTE pages, the TLB is not
flushed before pmd lock is unlocked. This may result in the following two
situations:
1) Userland can trigger page fault and fill a huge page, which will cause
the existence of small size TLB and huge TLB for the same address.
2) Userland can also trigger page fault and fill a PTE page, which will
cause the existence of two small size TLBs, but the PTE page they map
are different.
For case 1), according to Intel's TLB Application note (317080), some CPUs of
x86 do not allow it:
```
If software modifies the paging structures so that the page size used for a
4-KByte range of linear addresses changes, the TLBs may subsequently contain
both ordinary and large-page translations for the address range.12 A reference
to a linear address in the address range may use either translation. Which of
the two translations is used may vary from one execution to another and the
choice may be implementation-specific.
Software wishing to prevent this uncertainty should not write to a paging-
structure entry in a way that would change, for any linear address, both the
page size and either the page frame or attributes. It can instead use the
following algorithm: first mark the relevant paging-structure entry (e.g.,
PDE) not present; then invalidate any translations for the affected linear
addresses (see Section 5.2); and then modify the relevant paging-structure
entry to mark it present and establish translation(s) for the new page size.
```
We can also learn more information from the comments above pmdp_invalidate()
in __split_huge_pmd_locked().
For case 2), we can see from the comments above ptep_clear_flush() in
wp_page_copy() that this situation is also not allowed. Even without
this patch series, madvise(MADV_DONTNEED) can also cause this situation:
CPU 0 CPU 1
madvise (MADV_DONTNEED)
--> clear pte entry
pte_unmap_unlock
touch and tlb miss
--> set pte entry
mmu_gather flush tlb
But strangely, I didn't see any relevant fix code, maybe I missed something,
or is this guaranteed by userland?
Anyway, this series defines the following two functions to be implemented by
the architecture. If the architecture does not allow the above two situations,
then define these two functions to flush the tlb before set_pmd_at().
- arch_flush_tlb_before_set_huge_page
- arch_flush_tlb_before_set_pte_page
As a first step, we supported this feature on x86_64 and selectd the newly
introduced CONFIG_ARCH_SUPPORTS_PT_RECLAIM.
In order to reduce overhead, we only handle the cases with a high probability
of generating empty PTE pages, and other cases will be filtered out, such as:
- hugetlb vma (unsuitable)
- userfaultfd_wp vma (may reinstall the pte entry)
- writable private file mapping case (COW-ed anon page is not zapped)
- etc
For userfaultfd_wp and writable private file mapping cases (and MADV_FREE case,
of course), consider scanning and freeing empty PTE pages asynchronously in
the future.
This series is based on next-20240627.
Comments and suggestions are welcome!
Thanks,
Qi
[1]. https://lore.kernel.org/lkml/cover.1718267194.git.zhengqi.arch@bytedance.com/
Qi Zheng (7):
mm: pgtable: make pte_offset_map_nolock() return pmdval
mm: introduce CONFIG_PT_RECLAIM
mm: pass address information to pmd_install()
mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single()
x86: mm: free page table pages by RCU instead of semi RCU
x86: mm: define arch_flush_tlb_before_set_huge_page
x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64
Documentation/mm/split_page_table_lock.rst | 3 +-
arch/arm/mm/fault-armv.c | 2 +-
arch/powerpc/mm/pgtable.c | 2 +-
arch/x86/Kconfig | 1 +
arch/x86/include/asm/pgtable.h | 6 +
arch/x86/include/asm/tlb.h | 23 ++++
arch/x86/kernel/paravirt.c | 7 ++
arch/x86/mm/pgtable.c | 15 ++-
include/linux/hugetlb.h | 2 +-
include/linux/mm.h | 13 +-
include/linux/pgtable.h | 14 +++
mm/Kconfig | 14 +++
mm/Makefile | 1 +
mm/debug_vm_pgtable.c | 2 +-
mm/filemap.c | 4 +-
mm/gup.c | 2 +-
mm/huge_memory.c | 3 +
mm/internal.h | 17 ++-
mm/khugepaged.c | 24 +++-
mm/memory.c | 21 ++--
mm/migrate_device.c | 2 +-
mm/mmu_gather.c | 2 +-
mm/mprotect.c | 8 +-
mm/mremap.c | 4 +-
mm/page_vma_mapped.c | 2 +-
mm/pgtable-generic.c | 21 ++--
mm/pt_reclaim.c | 131 +++++++++++++++++++++
mm/userfaultfd.c | 10 +-
mm/vmscan.c | 2 +-
29 files changed, 307 insertions(+), 51 deletions(-)
create mode 100644 mm/pt_reclaim.c
--
2.20.1
^ permalink raw reply [flat|nested] 13+ messages in thread
* [RFC PATCH 1/7] mm: pgtable: make pte_offset_map_nolock() return pmdval
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 2/7] mm: introduce CONFIG_PT_RECLAIM Qi Zheng
` (7 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
Make pte_offset_map_nolock() return pmdval so that we can recheck the
*pmd once the lock is taken. This is a preparation for freeing empty
PTE pages, no functional changes are expected.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
Documentation/mm/split_page_table_lock.rst | 3 ++-
arch/arm/mm/fault-armv.c | 2 +-
arch/powerpc/mm/pgtable.c | 2 +-
include/linux/mm.h | 4 ++--
mm/filemap.c | 2 +-
mm/khugepaged.c | 4 ++--
mm/memory.c | 4 ++--
mm/mremap.c | 2 +-
mm/page_vma_mapped.c | 2 +-
mm/pgtable-generic.c | 21 ++++++++++++---------
mm/userfaultfd.c | 4 ++--
mm/vmscan.c | 2 +-
12 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/Documentation/mm/split_page_table_lock.rst b/Documentation/mm/split_page_table_lock.rst
index e4f6972eb6c0..e6a47d57531c 100644
--- a/Documentation/mm/split_page_table_lock.rst
+++ b/Documentation/mm/split_page_table_lock.rst
@@ -18,7 +18,8 @@ There are helpers to lock/unlock a table and other accessor functions:
pointer to its PTE table lock, or returns NULL if no PTE table;
- pte_offset_map_nolock()
maps PTE, returns pointer to PTE with pointer to its PTE table
- lock (not taken), or returns NULL if no PTE table;
+ lock (not taken) and the value of its pmd entry, or returns NULL
+ if no PTE table;
- pte_offset_map()
maps PTE, returns pointer to PTE, or returns NULL if no PTE table;
- pte_unmap()
diff --git a/arch/arm/mm/fault-armv.c b/arch/arm/mm/fault-armv.c
index 2286c2ea60ec..3e4ed99b9330 100644
--- a/arch/arm/mm/fault-armv.c
+++ b/arch/arm/mm/fault-armv.c
@@ -117,7 +117,7 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
* must use the nested version. This also means we need to
* open-code the spin-locking.
*/
- pte = pte_offset_map_nolock(vma->vm_mm, pmd, address, &ptl);
+ pte = pte_offset_map_nolock(vma->vm_mm, pmd, NULL, address, &ptl);
if (!pte)
return 0;
diff --git a/arch/powerpc/mm/pgtable.c b/arch/powerpc/mm/pgtable.c
index 9e7ba9c3851f..ab0250f1b226 100644
--- a/arch/powerpc/mm/pgtable.c
+++ b/arch/powerpc/mm/pgtable.c
@@ -350,7 +350,7 @@ void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
*/
if (pmd_none(*pmd))
return;
- pte = pte_offset_map_nolock(mm, pmd, addr, &ptl);
+ pte = pte_offset_map_nolock(mm, pmd, NULL, addr, &ptl);
BUG_ON(!pte);
assert_spin_locked(ptl);
pte_unmap(pte);
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 7d044e737dba..396bdc3b3726 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2979,8 +2979,8 @@ static inline pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
return pte;
}
-pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
- unsigned long addr, spinlock_t **ptlp);
+pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdvalp,
+ unsigned long addr, spinlock_t **ptlp);
#define pte_unmap_unlock(pte, ptl) do { \
spin_unlock(ptl); \
diff --git a/mm/filemap.c b/mm/filemap.c
index 6835977ee99a..35bbba960447 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3231,7 +3231,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID))
return 0;
- ptep = pte_offset_map_nolock(vma->vm_mm, vmf->pmd, vmf->address,
+ ptep = pte_offset_map_nolock(vma->vm_mm, vmf->pmd, NULL, vmf->address,
&vmf->ptl);
if (unlikely(!ptep))
return VM_FAULT_NOPAGE;
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 2e017585f813..7b7c858d5f99 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -989,7 +989,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
};
if (!pte++) {
- pte = pte_offset_map_nolock(mm, pmd, address, &ptl);
+ pte = pte_offset_map_nolock(mm, pmd, NULL, address, &ptl);
if (!pte) {
mmap_read_unlock(mm);
result = SCAN_PMD_NULL;
@@ -1578,7 +1578,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
pml = pmd_lock(mm, pmd);
- start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+ start_pte = pte_offset_map_nolock(mm, pmd, NULL, haddr, &ptl);
if (!start_pte) /* mmap_lock + page lock should prevent this */
goto abort;
if (!pml)
diff --git a/mm/memory.c b/mm/memory.c
index 0a769f34bbb2..1c9068b0b067 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1108,7 +1108,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
ret = -ENOMEM;
goto out;
}
- src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl);
+ src_pte = pte_offset_map_nolock(src_mm, src_pmd, NULL, addr, &src_ptl);
if (!src_pte) {
pte_unmap_unlock(dst_pte, dst_ptl);
/* ret == 0 */
@@ -5507,7 +5507,7 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
* it into a huge pmd: just retry later if so.
*/
vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd,
- vmf->address, &vmf->ptl);
+ NULL, vmf->address, &vmf->ptl);
if (unlikely(!vmf->pte))
return 0;
vmf->orig_pte = ptep_get_lockless(vmf->pte);
diff --git a/mm/mremap.c b/mm/mremap.c
index e7ae140fc640..f672d0218a6f 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -175,7 +175,7 @@ static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
err = -EAGAIN;
goto out;
}
- new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl);
+ new_pte = pte_offset_map_nolock(mm, new_pmd, NULL, new_addr, &new_ptl);
if (!new_pte) {
pte_unmap_unlock(old_pte, old_ptl);
err = -EAGAIN;
diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c
index ae5cc42aa208..507701b7bcc1 100644
--- a/mm/page_vma_mapped.c
+++ b/mm/page_vma_mapped.c
@@ -33,7 +33,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
* Though, in most cases, page lock already protects this.
*/
pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd,
- pvmw->address, ptlp);
+ NULL, pvmw->address, ptlp);
if (!pvmw->pte)
return false;
diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c
index a78a4adf711a..443e3b34434a 100644
--- a/mm/pgtable-generic.c
+++ b/mm/pgtable-generic.c
@@ -305,7 +305,7 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
return NULL;
}
-pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
+pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdvalp,
unsigned long addr, spinlock_t **ptlp)
{
pmd_t pmdval;
@@ -314,6 +314,8 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
pte = __pte_offset_map(pmd, addr, &pmdval);
if (likely(pte))
*ptlp = pte_lockptr(mm, &pmdval);
+ if (pmdvalp)
+ *pmdvalp = pmdval;
return pte;
}
@@ -347,14 +349,15 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
* and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unlock()s
* afterwards.
*
- * pte_offset_map_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
- * but when successful, it also outputs a pointer to the spinlock in ptlp - as
- * pte_offset_map_lock() does, but in this case without locking it. This helps
- * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time
- * act on a changed *pmd: pte_offset_map_nolock() provides the correct spinlock
- * pointer for the page table that it returns. In principle, the caller should
- * recheck *pmd once the lock is taken; in practice, no callsite needs that -
- * either the mmap_lock for write, or pte_same() check on contents, is enough.
+ * pte_offset_map_nolock(mm, pmd, pmdvalp, addr, ptlp), above, is like
+ * pte_offset_map(); but when successful, it also outputs a pointer to the
+ * spinlock in ptlp - as pte_offset_map_lock() does, but in this case without
+ * locking it. This helps the caller to avoid a later pte_lockptr(mm, *pmd),
+ * which might by that time act on a changed *pmd: pte_offset_map_nolock()
+ * provides the correct spinlock pointer for the page table that it returns.
+ * In principle, the caller should recheck *pmd once the lock is taken; But in
+ * most cases, either the mmap_lock for write, or pte_same() check on contents,
+ * is enough.
*
* Note that free_pgtables(), used after unmapping detached vmas, or when
* exiting the whole mm, does not take page table lock before freeing a page
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 8dedaec00486..61c1d228d239 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -1143,7 +1143,7 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
src_addr, src_addr + PAGE_SIZE);
mmu_notifier_invalidate_range_start(&range);
retry:
- dst_pte = pte_offset_map_nolock(mm, dst_pmd, dst_addr, &dst_ptl);
+ dst_pte = pte_offset_map_nolock(mm, dst_pmd, NULL, dst_addr, &dst_ptl);
/* Retry if a huge pmd materialized from under us */
if (unlikely(!dst_pte)) {
@@ -1151,7 +1151,7 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
goto out;
}
- src_pte = pte_offset_map_nolock(mm, src_pmd, src_addr, &src_ptl);
+ src_pte = pte_offset_map_nolock(mm, src_pmd, NULL, src_addr, &src_ptl);
/*
* We held the mmap_lock for reading so MADV_DONTNEED
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 3d4c681c6d40..c9a4cd31e6b4 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3373,7 +3373,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
DEFINE_MAX_SEQ(walk->lruvec);
int old_gen, new_gen = lru_gen_from_seq(max_seq);
- pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl);
+ pte = pte_offset_map_nolock(args->mm, pmd, NULL, start & PMD_MASK, &ptl);
if (!pte)
return false;
if (!spin_trylock(ptl)) {
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 2/7] mm: introduce CONFIG_PT_RECLAIM
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 1/7] mm: pgtable: make pte_offset_map_nolock() return pmdval Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 3/7] mm: pass address information to pmd_install() Qi Zheng
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
This configuration variable will be used to build the code needed
to free empty user page table pages.
This feature is not available on all architectures yet, so
ARCH_SUPPORTS_PT_RECLAIM is needed. We can remove it once all
architectures support this feature.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
mm/Kconfig | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/mm/Kconfig b/mm/Kconfig
index 991fa9cf6137..7e2c87784d86 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -1256,6 +1256,20 @@ config IOMMU_MM_DATA
config EXECMEM
bool
+config ARCH_SUPPORTS_PT_RECLAIM
+ def_bool n
+
+config PT_RECLAIM
+ bool "reclaim empty user page table pages"
+ default y
+ depends on ARCH_SUPPORTS_PT_RECLAIM && MMU && SMP
+ select MMU_GATHER_RCU_TABLE_FREE
+ help
+ Try to reclaim empty user page table pages in paths other that munmap
+ and exit_mmap path.
+
+ Note: now only empty user PTE page table pages will be reclaimed.
+
source "mm/damon/Kconfig"
endmenu
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 3/7] mm: pass address information to pmd_install()
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 1/7] mm: pgtable: make pte_offset_map_nolock() return pmdval Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 2/7] mm: introduce CONFIG_PT_RECLAIM Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 4/7] mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single() Qi Zheng
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
In the subsequent implementation of freeing empty page table pages,
we need the address information to flush tlb, so pass address to
pmd_install() in advance.
No functional changes.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
include/linux/hugetlb.h | 2 +-
include/linux/mm.h | 9 +++++----
mm/debug_vm_pgtable.c | 2 +-
mm/filemap.c | 2 +-
mm/gup.c | 2 +-
mm/internal.h | 3 ++-
mm/memory.c | 15 ++++++++-------
mm/migrate_device.c | 2 +-
mm/mprotect.c | 8 ++++----
mm/mremap.c | 2 +-
mm/userfaultfd.c | 6 +++---
11 files changed, 28 insertions(+), 25 deletions(-)
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index a951c0d06061..55715eb5cb34 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -198,7 +198,7 @@ static inline pte_t *pte_offset_huge(pmd_t *pmd, unsigned long address)
static inline pte_t *pte_alloc_huge(struct mm_struct *mm, pmd_t *pmd,
unsigned long address)
{
- return pte_alloc(mm, pmd) ? NULL : pte_offset_huge(pmd, address);
+ return pte_alloc(mm, pmd, address) ? NULL : pte_offset_huge(pmd, address);
}
#endif
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 396bdc3b3726..880100a8b472 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2800,7 +2800,7 @@ static inline void mm_inc_nr_ptes(struct mm_struct *mm) {}
static inline void mm_dec_nr_ptes(struct mm_struct *mm) {}
#endif
-int __pte_alloc(struct mm_struct *mm, pmd_t *pmd);
+int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long addr);
int __pte_alloc_kernel(pmd_t *pmd);
#if defined(CONFIG_MMU)
@@ -2987,13 +2987,14 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdvalp,
pte_unmap(pte); \
} while (0)
-#define pte_alloc(mm, pmd) (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd))
+#define pte_alloc(mm, pmd, addr) \
+ (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd, addr))
#define pte_alloc_map(mm, pmd, address) \
- (pte_alloc(mm, pmd) ? NULL : pte_offset_map(pmd, address))
+ (pte_alloc(mm, pmd, address) ? NULL : pte_offset_map(pmd, address))
#define pte_alloc_map_lock(mm, pmd, address, ptlp) \
- (pte_alloc(mm, pmd) ? \
+ (pte_alloc(mm, pmd, address) ? \
NULL : pte_offset_map_lock(mm, pmd, address, ptlp))
#define pte_alloc_kernel(pmd, address) \
diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c
index e4969fb54da3..18375744e184 100644
--- a/mm/debug_vm_pgtable.c
+++ b/mm/debug_vm_pgtable.c
@@ -1246,7 +1246,7 @@ static int __init init_args(struct pgtable_debug_args *args)
args->start_pmdp = pmd_offset(args->pudp, 0UL);
WARN_ON(!args->start_pmdp);
- if (pte_alloc(args->mm, args->pmdp)) {
+ if (pte_alloc(args->mm, args->pmdp, args->vaddr)) {
pr_err("Failed to allocate pte entries\n");
ret = -ENOMEM;
goto error;
diff --git a/mm/filemap.c b/mm/filemap.c
index 35bbba960447..d8b936d87eb4 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3453,7 +3453,7 @@ static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,
}
if (pmd_none(*vmf->pmd) && vmf->prealloc_pte)
- pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
+ pmd_install(mm, vmf->pmd, vmf->address, &vmf->prealloc_pte);
return false;
}
diff --git a/mm/gup.c b/mm/gup.c
index 8bea9ad80984..b87b1ea9d008 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1105,7 +1105,7 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma,
spin_unlock(ptl);
split_huge_pmd(vma, pmd, address);
/* If pmd was left empty, stuff a page table in there quickly */
- return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) :
+ return pte_alloc(mm, pmd, address) ? ERR_PTR(-ENOMEM) :
follow_page_pte(vma, address, pmd, flags, &ctx->pgmap);
}
page = follow_huge_pmd(vma, address, pmd, flags, ctx);
diff --git a/mm/internal.h b/mm/internal.h
index 2ea9a88dcb95..1dfdad110a9a 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -320,7 +320,8 @@ void folio_activate(struct folio *folio);
void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
struct vm_area_struct *start_vma, unsigned long floor,
unsigned long ceiling, bool mm_wr_locked);
-void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte);
+void pmd_install(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
+ pgtable_t *pte);
struct zap_details;
void unmap_page_range(struct mmu_gather *tlb,
diff --git a/mm/memory.c b/mm/memory.c
index 1c9068b0b067..09db2c97cc5c 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -417,7 +417,8 @@ void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
} while (vma);
}
-void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte)
+void pmd_install(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
+ pgtable_t *pte)
{
spinlock_t *ptl = pmd_lock(mm, pmd);
@@ -443,13 +444,13 @@ void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte)
spin_unlock(ptl);
}
-int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
+int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long addr)
{
pgtable_t new = pte_alloc_one(mm);
if (!new)
return -ENOMEM;
- pmd_install(mm, pmd, &new);
+ pmd_install(mm, pmd, addr, &new);
if (new)
pte_free(mm, new);
return 0;
@@ -2115,7 +2116,7 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
/* Allocate the PTE if necessary; takes PMD lock once only. */
ret = -ENOMEM;
- if (pte_alloc(mm, pmd))
+ if (pte_alloc(mm, pmd, addr))
goto out;
while (pages_to_write_in_pmd) {
@@ -4521,7 +4522,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
* Use pte_alloc() instead of pte_alloc_map(), so that OOM can
* be distinguished from a transient failure of pte_offset_map().
*/
- if (pte_alloc(vma->vm_mm, vmf->pmd))
+ if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))
return VM_FAULT_OOM;
/* Use the zero-page for reads */
@@ -4868,8 +4869,8 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
}
if (vmf->prealloc_pte)
- pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
- else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
+ pmd_install(vma->vm_mm, vmf->pmd, vmf->address, &vmf->prealloc_pte);
+ else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address)))
return VM_FAULT_OOM;
}
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index 6d66dc1c6ffa..e4d2e19e6611 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -598,7 +598,7 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate,
goto abort;
if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
goto abort;
- if (pte_alloc(mm, pmdp))
+ if (pte_alloc(mm, pmdp, addr))
goto abort;
if (unlikely(anon_vma_prepare(vma)))
goto abort;
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 222ab434da54..1a1537ddffe4 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -330,11 +330,11 @@ pgtable_populate_needed(struct vm_area_struct *vma, unsigned long cp_flags)
* allocation failures during page faults by kicking OOM and returning
* error.
*/
-#define change_pmd_prepare(vma, pmd, cp_flags) \
+#define change_pmd_prepare(vma, pmd, addr, cp_flags) \
({ \
long err = 0; \
if (unlikely(pgtable_populate_needed(vma, cp_flags))) { \
- if (pte_alloc(vma->vm_mm, pmd)) \
+ if (pte_alloc(vma->vm_mm, pmd, addr)) \
err = -ENOMEM; \
} \
err; \
@@ -375,7 +375,7 @@ static inline long change_pmd_range(struct mmu_gather *tlb,
again:
next = pmd_addr_end(addr, end);
- ret = change_pmd_prepare(vma, pmd, cp_flags);
+ ret = change_pmd_prepare(vma, pmd, addr, cp_flags);
if (ret) {
pages = ret;
break;
@@ -402,7 +402,7 @@ static inline long change_pmd_range(struct mmu_gather *tlb,
* cleared; make sure pmd populated if
* necessary, then fall-through to pte level.
*/
- ret = change_pmd_prepare(vma, pmd, cp_flags);
+ ret = change_pmd_prepare(vma, pmd, addr, cp_flags);
if (ret) {
pages = ret;
break;
diff --git a/mm/mremap.c b/mm/mremap.c
index f672d0218a6f..7723d11e77cd 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -628,7 +628,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
}
if (pmd_none(*old_pmd))
continue;
- if (pte_alloc(new_vma->vm_mm, new_pmd))
+ if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
break;
if (move_ptes(vma, old_pmd, old_addr, old_addr + extent,
new_vma, new_pmd, new_addr, need_rmap_locks) < 0)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 61c1d228d239..e1674580b54f 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -796,7 +796,7 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx,
break;
}
if (unlikely(pmd_none(dst_pmdval)) &&
- unlikely(__pte_alloc(dst_mm, dst_pmd))) {
+ unlikely(__pte_alloc(dst_mm, dst_pmd, dst_addr))) {
err = -ENOMEM;
break;
}
@@ -1713,13 +1713,13 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
err = -ENOENT;
break;
}
- if (unlikely(__pte_alloc(mm, src_pmd))) {
+ if (unlikely(__pte_alloc(mm, src_pmd, src_addr))) {
err = -ENOMEM;
break;
}
}
- if (unlikely(pte_alloc(mm, dst_pmd))) {
+ if (unlikely(pte_alloc(mm, dst_pmd, dst_addr))) {
err = -ENOMEM;
break;
}
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 4/7] mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single()
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
` (2 preceding siblings ...)
2024-07-01 8:46 ` [RFC PATCH 3/7] mm: pass address information to pmd_install() Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 5/7] x86: mm: free page table pages by RCU instead of semi RCU Qi Zheng
` (4 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
Now in order to pursue high performance, applications mostly use some
high-performance user-mode memory allocators, such as jemalloc or
tcmalloc. These memory allocators use madvise(MADV_DONTNEED or MADV_FREE)
to release physical memory, but neither MADV_DONTNEED nor MADV_FREE will
release page table memory, which may cause huge page table memory usage.
The following are a memory usage snapshot of one process which actually
happened on our server:
VIRT: 55t
RES: 590g
VmPTE: 110g
In this case, most of the page table entries are empty. For such a PTE
page where all entries are empty, we can actually free it back to the
system for others to use.
As a first step, this commit attempts to synchronously free the empty PTE
pages in zap_page_range_single() (MADV_DONTNEED etc will invoke this). In
order to reduce overhead, we only handle the cases with a high probability
of generating empty PTE pages, and other cases will be filtered out, such
as:
- hugetlb vma (unsuitable)
- userfaultfd_wp vma (may reinstall the pte entry)
- writable private file mapping case (COW-ed anon page is not zapped)
- etc
For userfaultfd_wp and private file mapping cases (and MADV_FREE case, of
course), consider scanning and freeing empty PTE pages asynchronously in
the future.
The following code snippet can show the effect of optimization:
mmap 50G
while (1) {
for (; i < 1024 * 25; i++) {
touch 2M memory
madvise MADV_DONTNEED 2M
}
}
As we can see, the memory usage of VmPTE is reduced:
before after
VIRT 50.0 GB 50.0 GB
RES 3.1 MB 3.1 MB
VmPTE 102640 KB 240 KB
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
include/linux/pgtable.h | 14 +++++
mm/Makefile | 1 +
mm/huge_memory.c | 3 +
mm/internal.h | 14 +++++
mm/khugepaged.c | 22 ++++++-
mm/memory.c | 2 +
mm/pt_reclaim.c | 131 ++++++++++++++++++++++++++++++++++++++++
7 files changed, 186 insertions(+), 1 deletion(-)
create mode 100644 mm/pt_reclaim.c
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 2f32eaccf0b9..59e894f705a7 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -447,6 +447,20 @@ static inline void arch_check_zapped_pmd(struct vm_area_struct *vma,
}
#endif
+#ifndef arch_flush_tlb_before_set_huge_page
+static inline void arch_flush_tlb_before_set_huge_page(struct mm_struct *mm,
+ unsigned long addr)
+{
+}
+#endif
+
+#ifndef arch_flush_tlb_before_set_pte_page
+static inline void arch_flush_tlb_before_set_pte_page(struct mm_struct *mm,
+ unsigned long addr)
+{
+}
+#endif
+
#ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
unsigned long address,
diff --git a/mm/Makefile b/mm/Makefile
index d2915f8c9dc0..3cb3c1f5d090 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -141,3 +141,4 @@ obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
obj-$(CONFIG_EXECMEM) += execmem.o
+obj-$(CONFIG_PT_RECLAIM) += pt_reclaim.o
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index c7ce28f6b7f3..444a1cdaf06d 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -977,6 +977,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
folio_add_new_anon_rmap(folio, vma, haddr, RMAP_EXCLUSIVE);
folio_add_lru_vma(folio, vma);
pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
+ arch_flush_tlb_before_set_huge_page(vma->vm_mm, haddr);
set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
@@ -1044,6 +1045,7 @@ static void set_huge_zero_folio(pgtable_t pgtable, struct mm_struct *mm,
entry = mk_pmd(&zero_folio->page, vma->vm_page_prot);
entry = pmd_mkhuge(entry);
pgtable_trans_huge_deposit(mm, pmd, pgtable);
+ arch_flush_tlb_before_set_huge_page(mm, haddr);
set_pmd_at(mm, haddr, pmd, entry);
mm_inc_nr_ptes(mm);
}
@@ -1151,6 +1153,7 @@ static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
pgtable = NULL;
}
+ arch_flush_tlb_before_set_huge_page(mm, addr);
set_pmd_at(mm, addr, pmd, entry);
update_mmu_cache_pmd(vma, addr, pmd);
diff --git a/mm/internal.h b/mm/internal.h
index 1dfdad110a9a..ac1fdd4681dc 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1579,4 +1579,18 @@ void unlink_file_vma_batch_init(struct unlink_vma_file_batch *);
void unlink_file_vma_batch_add(struct unlink_vma_file_batch *, struct vm_area_struct *);
void unlink_file_vma_batch_final(struct unlink_vma_file_batch *);
+#ifdef CONFIG_PT_RECLAIM
+void try_to_reclaim_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
+ unsigned long start_addr, unsigned long end_addr,
+ struct zap_details *details);
+#else
+static inline void try_to_reclaim_pgtables(struct mmu_gather *tlb,
+ struct vm_area_struct *vma,
+ unsigned long start_addr,
+ unsigned long end_addr,
+ struct zap_details *details)
+{
+}
+#endif /* CONFIG_PT_RECLAIM */
+
#endif /* __MM_INTERNAL_H */
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 7b7c858d5f99..63551077795d 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1578,7 +1578,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
pml = pmd_lock(mm, pmd);
- start_pte = pte_offset_map_nolock(mm, pmd, NULL, haddr, &ptl);
+ start_pte = pte_offset_map_nolock(mm, pmd, &pgt_pmd, haddr, &ptl);
if (!start_pte) /* mmap_lock + page lock should prevent this */
goto abort;
if (!pml)
@@ -1586,6 +1586,11 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
else if (ptl != pml)
spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+ /* pmd entry may be changed by others */
+ if (unlikely(IS_ENABLED(CONFIG_PT_RECLAIM) && !pml &&
+ !pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+ goto abort;
+
/* step 2: clear page table and adjust rmap */
for (i = 0, addr = haddr, pte = start_pte;
i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1633,6 +1638,12 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
pml = pmd_lock(mm, pmd);
if (ptl != pml)
spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+
+ if (unlikely(IS_ENABLED(CONFIG_PT_RECLAIM) &&
+ !pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+ spin_unlock(ptl);
+ goto unlock;
+ }
}
pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
pmdp_get_lockless_sync();
@@ -1660,6 +1671,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
}
if (start_pte)
pte_unmap_unlock(start_pte, ptl);
+unlock:
if (pml && pml != ptl)
spin_unlock(pml);
if (notified)
@@ -1719,6 +1731,14 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
mmu_notifier_invalidate_range_start(&range);
pml = pmd_lock(mm, pmd);
+#ifdef CONFIG_PT_RECLAIM
+ /* check if the pmd is still valid */
+ if (check_pmd_still_valid(mm, addr, pmd) != SCAN_SUCCEED) {
+ spin_unlock(pml);
+ mmu_notifier_invalidate_range_end(&range);
+ continue;
+ }
+#endif
ptl = pte_lockptr(mm, pmd);
if (ptl != pml)
spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
diff --git a/mm/memory.c b/mm/memory.c
index 09db2c97cc5c..b07d63767d93 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -423,6 +423,7 @@ void pmd_install(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
spinlock_t *ptl = pmd_lock(mm, pmd);
if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
+ arch_flush_tlb_before_set_pte_page(mm, addr);
mm_inc_nr_ptes(mm);
/*
* Ensure all pte setup (eg. pte page lock and page clearing) are
@@ -1931,6 +1932,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
* could have been expanded for hugetlb pmd sharing.
*/
unmap_single_vma(&tlb, vma, address, end, details, false);
+ try_to_reclaim_pgtables(&tlb, vma, address, end, details);
mmu_notifier_invalidate_range_end(&range);
tlb_finish_mmu(&tlb);
hugetlb_zap_end(vma, details);
diff --git a/mm/pt_reclaim.c b/mm/pt_reclaim.c
new file mode 100644
index 000000000000..e375e7f2059f
--- /dev/null
+++ b/mm/pt_reclaim.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/pagewalk.h>
+#include <linux/hugetlb.h>
+#include <asm-generic/tlb.h>
+#include <asm/pgalloc.h>
+
+#include "internal.h"
+
+/*
+ * Locking:
+ * - already held the mmap read lock to traverse the pgtable
+ * - use pmd lock for clearing pmd entry
+ * - use pte lock for checking empty PTE page, and release it after clearing
+ * pmd entry, then we can capture the changed pmd in pte_offset_map_lock()
+ * etc after holding this pte lock. Thanks to this, we don't need to hold the
+ * rmap-related locks.
+ * - users of pte_offset_map_lock() etc all expect the PTE page to be stable by
+ * using rcu lock, so PTE pages should be freed by RCU.
+ */
+static int reclaim_pgtables_pmd_entry(pmd_t *pmd, unsigned long addr,
+ unsigned long next, struct mm_walk *walk)
+{
+ struct mm_struct *mm = walk->mm;
+ struct mmu_gather *tlb = walk->private;
+ pte_t *start_pte, *pte;
+ pmd_t pmdval;
+ spinlock_t *pml = NULL, *ptl;
+ int i;
+
+ start_pte = pte_offset_map_nolock(mm, pmd, &pmdval, addr, &ptl);
+ if (!start_pte)
+ return 0;
+
+ pml = pmd_lock(mm, pmd);
+ if (ptl != pml)
+ spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+
+ if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd))))
+ goto out_ptl;
+
+ /* Check if it is empty PTE page */
+ for (i = 0, pte = start_pte; i < PTRS_PER_PTE; i++, pte++) {
+ if (!pte_none(ptep_get(pte)))
+ goto out_ptl;
+ }
+ pte_unmap(start_pte);
+
+ pmd_clear(pmd);
+ if (ptl != pml)
+ spin_unlock(ptl);
+ spin_unlock(pml);
+
+ /*
+ * NOTE:
+ * In order to reuse mmu_gather to batch flush tlb and free PTE pages,
+ * here tlb is not flushed before pmd lock is unlocked. This may
+ * result in the following two situations:
+ *
+ * 1) Userland can trigger page fault and fill a huge page, which will
+ * cause the existence of small size TLB and huge TLB for the same
+ * address.
+ *
+ * 2) Userland can also trigger page fault and fill a PTE page, which
+ * will cause the existence of two small size TLBs, but the PTE
+ * page they map are different.
+ *
+ * Some CPUs do not allow these, to solve this, we can define
+ * arch_flush_tlb_before_set_{huge|pte}_page to detect this case and
+ * flush TLB before filling a huge page or a PTE page in page fault
+ * path.
+ */
+ pte_free_tlb(tlb, pmd_pgtable(pmdval), addr);
+ mm_dec_nr_ptes(mm);
+
+ return 0;
+
+out_ptl:
+ pte_unmap_unlock(start_pte, ptl);
+ if (pml != ptl)
+ spin_unlock(pml);
+
+ return 0;
+}
+
+static const struct mm_walk_ops reclaim_pgtables_walk_ops = {
+ .pmd_entry = reclaim_pgtables_pmd_entry,
+ .walk_lock = PGWALK_RDLOCK,
+};
+
+void try_to_reclaim_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
+ unsigned long start_addr, unsigned long end_addr,
+ struct zap_details *details)
+{
+ unsigned long start = max(vma->vm_start, start_addr);
+ unsigned long end;
+
+ if (start >= vma->vm_end)
+ return;
+ end = min(vma->vm_end, end_addr);
+ if (end <= vma->vm_start)
+ return;
+
+ /* Skip hugetlb case */
+ if (is_vm_hugetlb_page(vma))
+ return;
+
+ /* Leave this to the THP path to handle */
+ if (vma->vm_flags & VM_HUGEPAGE)
+ return;
+
+ /* userfaultfd_wp case may reinstall the pte entry, also skip */
+ if (userfaultfd_wp(vma))
+ return;
+
+ /*
+ * For private file mapping, the COW-ed page is an anon page, and it
+ * will not be zapped. For simplicity, skip the all writable private
+ * file mapping cases.
+ */
+ if (details && !vma_is_anonymous(vma) &&
+ !(vma->vm_flags & VM_MAYSHARE) &&
+ (vma->vm_flags & VM_WRITE))
+ return;
+
+ start = ALIGN(start, PMD_SIZE);
+ end = ALIGN_DOWN(end, PMD_SIZE);
+ if (end - start < PMD_SIZE)
+ return;
+
+ walk_page_range_vma(vma, start, end, &reclaim_pgtables_walk_ops, tlb);
+}
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 5/7] x86: mm: free page table pages by RCU instead of semi RCU
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
` (3 preceding siblings ...)
2024-07-01 8:46 ` [RFC PATCH 4/7] mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single() Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 6/7] x86: mm: define arch_flush_tlb_before_set_huge_page Qi Zheng
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
Now, if CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected, the page table pages
will be freed by semi RCU, that is:
- batch table freeing: asynchronous free by RCU
- single table freeing: IPI + synchronous free
In this way, the page table can be lockless traversed by disabling IRQ in
paths such as fast GUP. But this is not enough to free the empty PTE page
table pages in paths other that munmap and exit_mmap path, because IPI
cannot be synchronized with rcu_read_lock() in pte_offset_map{_lock}().
In preparation for supporting empty PTE page table pages reclaimation,
let single table also be freed by RCU like batch table freeing. Then we
can also use pte_offset_map() etc to prevent PTE page from being freed.
Like pte_free_defer(), we can also safely use ptdesc->pt_rcu_head to free
the page table pages:
- The pt_rcu_head is unioned with pt_list and pmd_huge_pte.
- For pt_list, it is used to manage the PGD page in x86. Fortunately
tlb_remove_table() will not be used for free PGD pages, so it is safe
to use pt_rcu_head.
- For pmd_huge_pte, we will do zap_deposited_table() before freeing the
PMD page, so it is also safe.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
arch/x86/include/asm/tlb.h | 23 +++++++++++++++++++++++
arch/x86/kernel/paravirt.c | 7 +++++++
arch/x86/mm/pgtable.c | 2 +-
mm/mmu_gather.c | 2 +-
4 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/arch/x86/include/asm/tlb.h b/arch/x86/include/asm/tlb.h
index 580636cdc257..9182db1e0264 100644
--- a/arch/x86/include/asm/tlb.h
+++ b/arch/x86/include/asm/tlb.h
@@ -34,4 +34,27 @@ static inline void __tlb_remove_table(void *table)
free_page_and_swap_cache(table);
}
+#ifndef CONFIG_PT_RECLAIM
+static inline void __tlb_remove_table_one(void *table)
+{
+ free_page_and_swap_cache(table);
+}
+#else
+static inline void __tlb_remove_table_one_rcu(struct rcu_head *head)
+{
+ struct page *page;
+
+ page = container_of(head, struct page, rcu_head);
+ free_page_and_swap_cache(page);
+}
+
+static inline void __tlb_remove_table_one(void *table)
+{
+ struct page *page;
+
+ page = table;
+ call_rcu(&page->rcu_head, __tlb_remove_table_one_rcu);
+}
+#endif /* CONFIG_PT_RECLAIM */
+
#endif /* _ASM_X86_TLB_H */
diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
index 5358d43886ad..199b9a3813b4 100644
--- a/arch/x86/kernel/paravirt.c
+++ b/arch/x86/kernel/paravirt.c
@@ -60,10 +60,17 @@ void __init native_pv_lock_init(void)
static_branch_disable(&virt_spin_lock_key);
}
+#ifndef CONFIG_PT_RECLAIM
static void native_tlb_remove_table(struct mmu_gather *tlb, void *table)
{
tlb_remove_page(tlb, table);
}
+#else
+static void native_tlb_remove_table(struct mmu_gather *tlb, void *table)
+{
+ tlb_remove_table(tlb, table);
+}
+#endif
struct static_key paravirt_steal_enabled;
struct static_key paravirt_steal_rq_enabled;
diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c
index 93e54ba91fbf..cd5bf2157611 100644
--- a/arch/x86/mm/pgtable.c
+++ b/arch/x86/mm/pgtable.c
@@ -18,7 +18,7 @@ EXPORT_SYMBOL(physical_mask);
#define PGTABLE_HIGHMEM 0
#endif
-#ifndef CONFIG_PARAVIRT
+#if !defined(CONFIG_PARAVIRT) && !defined(CONFIG_PT_RECLAIM)
static inline
void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table)
{
diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c
index 99b3e9408aa0..1a8f7b8781a2 100644
--- a/mm/mmu_gather.c
+++ b/mm/mmu_gather.c
@@ -314,7 +314,7 @@ static inline void tlb_table_invalidate(struct mmu_gather *tlb)
static void tlb_remove_table_one(void *table)
{
tlb_remove_table_sync_one();
- __tlb_remove_table(table);
+ __tlb_remove_table_one(table);
}
static void tlb_table_flush(struct mmu_gather *tlb)
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 6/7] x86: mm: define arch_flush_tlb_before_set_huge_page
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
` (4 preceding siblings ...)
2024-07-01 8:46 ` [RFC PATCH 5/7] x86: mm: free page table pages by RCU instead of semi RCU Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 7/7] x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64 Qi Zheng
` (2 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
When we use mmu_gather to batch flush tlb and free PTE pages, the TLB is
not flushed before pmd lock is unlocked. This may result in the following
two situations:
1) Userland can trigger page fault and fill a huge page, which will cause
the existence of small size TLB and huge TLB for the same address.
2) Userland can also trigger page fault and fill a PTE page, which will
cause the existence of two small size TLBs, but the PTE page they map
are different.
According to Intel's TLB Application note (317080), some CPUs of x86 do
not allow the 1) case, so define arch_flush_tlb_before_set_huge_page to
detect and fix this issue.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
arch/x86/include/asm/pgtable.h | 6 ++++++
arch/x86/mm/pgtable.c | 13 +++++++++++++
2 files changed, 19 insertions(+)
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index e39311a89bf4..f93d964ab6a3 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1668,6 +1668,12 @@ void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte);
#define arch_check_zapped_pmd arch_check_zapped_pmd
void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd);
+#ifdef CONFIG_PT_RECLAIM
+#define arch_flush_tlb_before_set_huge_page arch_flush_tlb_before_set_huge_page
+void arch_flush_tlb_before_set_huge_page(struct mm_struct *mm,
+ unsigned long addr);
+#endif
+
#ifdef CONFIG_XEN_PV
#define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young
static inline bool arch_has_hw_nonleaf_pmd_young(void)
diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c
index cd5bf2157611..d037f7425f82 100644
--- a/arch/x86/mm/pgtable.c
+++ b/arch/x86/mm/pgtable.c
@@ -926,3 +926,16 @@ void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd)
VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) &&
pmd_shstk(pmd));
}
+
+#ifdef CONFIG_PT_RECLAIM
+void arch_flush_tlb_before_set_huge_page(struct mm_struct *mm,
+ unsigned long addr)
+{
+ if (atomic_read(&mm->tlb_flush_pending)) {
+ unsigned long start = ALIGN_DOWN(addr, PMD_SIZE);
+ unsigned long end = start + PMD_SIZE;
+
+ flush_tlb_mm_range(mm, start, end, PAGE_SHIFT, false);
+ }
+}
+#endif /* CONFIG_PT_RECLAIM */
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 7/7] x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
` (5 preceding siblings ...)
2024-07-01 8:46 ` [RFC PATCH 6/7] x86: mm: define arch_flush_tlb_before_set_huge_page Qi Zheng
@ 2024-07-01 8:46 ` Qi Zheng
2024-07-04 7:16 ` [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
2024-07-26 9:07 ` Vlastimil Babka (SUSE)
8 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-01 8:46 UTC (permalink / raw)
To: david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel, Qi Zheng
Now, x86 has fully supported the CONFIG_PT_RECLAIM feature, and
reclaiming PTE pages is profitable only on 64-bit systems, so select
ARCH_SUPPORTS_PT_RECLAIM if X86_64.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
arch/x86/Kconfig | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index cbe5fac4b9dd..23ccd7c30adc 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -313,6 +313,7 @@ config X86
select FUNCTION_ALIGNMENT_4B
imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
select HAVE_DYNAMIC_FTRACE_NO_PATCHABLE
+ select ARCH_SUPPORTS_PT_RECLAIM if X86_64
config INSTRUCTION_DECODER
def_bool y
--
2.20.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
` (6 preceding siblings ...)
2024-07-01 8:46 ` [RFC PATCH 7/7] x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64 Qi Zheng
@ 2024-07-04 7:16 ` Qi Zheng
2024-07-07 8:26 ` David Hildenbrand
2024-07-26 9:07 ` Vlastimil Babka (SUSE)
8 siblings, 1 reply; 13+ messages in thread
From: Qi Zheng @ 2024-07-04 7:16 UTC (permalink / raw)
To: the arch/x86 maintainers
Cc: david, hughd, willy, mgorman, muchun.song, akpm, linux-mm,
linux-kernel
Add the x86 mailing list that I forgot to CC before.
On 2024/7/1 16:46, Qi Zheng wrote:
> Hi all,
>
> Previously, we tried to use a completely asynchronous method to reclaim empty
> user PTE pages [1]. After discussing with David Hildenbrand, we decided to
> implement synchronous reclaimation in the case of madvise(MADV_DONTNEED) as the
> first step.
>
> So this series aims to synchronously scan and reclaim empty user PTE pages in
> zap_page_range_single() (madvise(MADV_DONTNEED) etc will invoke this). In
> zap_page_range_single(), mmu_gather is used to perform batch tlb flushing and
> page freeing operations. Therefore, if we want to free the empty PTE page in
> this path, the most natural way is to add it to mmu_gather as well. There are
> two problems that need to be solved here:
>
> 1. Now, if CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected, mmu_gather will free
> page table pages by semi RCU:
>
> - batch table freeing: asynchronous free by RCU
> - single table freeing: IPI + synchronous free
>
> But this is not enough to free the empty PTE page table pages in paths other
> that munmap and exit_mmap path, because IPI cannot be synchronized with
> rcu_read_lock() in pte_offset_map{_lock}(). So we should let single table
> also be freed by RCU like batch table freeing.
>
> 2. When we use mmu_gather to batch flush tlb and free PTE pages, the TLB is not
> flushed before pmd lock is unlocked. This may result in the following two
> situations:
>
> 1) Userland can trigger page fault and fill a huge page, which will cause
> the existence of small size TLB and huge TLB for the same address.
>
> 2) Userland can also trigger page fault and fill a PTE page, which will
> cause the existence of two small size TLBs, but the PTE page they map
> are different.
>
> For case 1), according to Intel's TLB Application note (317080), some CPUs of
> x86 do not allow it:
>
> ```
> If software modifies the paging structures so that the page size used for a
> 4-KByte range of linear addresses changes, the TLBs may subsequently contain
> both ordinary and large-page translations for the address range.12 A reference
> to a linear address in the address range may use either translation. Which of
> the two translations is used may vary from one execution to another and the
> choice may be implementation-specific.
>
> Software wishing to prevent this uncertainty should not write to a paging-
> structure entry in a way that would change, for any linear address, both the
> page size and either the page frame or attributes. It can instead use the
> following algorithm: first mark the relevant paging-structure entry (e.g.,
> PDE) not present; then invalidate any translations for the affected linear
> addresses (see Section 5.2); and then modify the relevant paging-structure
> entry to mark it present and establish translation(s) for the new page size.
> ```
>
> We can also learn more information from the comments above pmdp_invalidate()
> in __split_huge_pmd_locked().
>
> For case 2), we can see from the comments above ptep_clear_flush() in
> wp_page_copy() that this situation is also not allowed. Even without
> this patch series, madvise(MADV_DONTNEED) can also cause this situation:
>
> CPU 0 CPU 1
>
> madvise (MADV_DONTNEED)
> --> clear pte entry
> pte_unmap_unlock
> touch and tlb miss
> --> set pte entry
> mmu_gather flush tlb
>
> But strangely, I didn't see any relevant fix code, maybe I missed something,
> or is this guaranteed by userland?
>
> Anyway, this series defines the following two functions to be implemented by
> the architecture. If the architecture does not allow the above two situations,
> then define these two functions to flush the tlb before set_pmd_at().
>
> - arch_flush_tlb_before_set_huge_page
> - arch_flush_tlb_before_set_pte_page
>
> As a first step, we supported this feature on x86_64 and selectd the newly
> introduced CONFIG_ARCH_SUPPORTS_PT_RECLAIM.
>
> In order to reduce overhead, we only handle the cases with a high probability
> of generating empty PTE pages, and other cases will be filtered out, such as:
>
> - hugetlb vma (unsuitable)
> - userfaultfd_wp vma (may reinstall the pte entry)
> - writable private file mapping case (COW-ed anon page is not zapped)
> - etc
>
> For userfaultfd_wp and writable private file mapping cases (and MADV_FREE case,
> of course), consider scanning and freeing empty PTE pages asynchronously in
> the future.
>
> This series is based on next-20240627.
>
> Comments and suggestions are welcome!
>
> Thanks,
> Qi
>
> [1]. https://lore.kernel.org/lkml/cover.1718267194.git.zhengqi.arch@bytedance.com/
>
> Qi Zheng (7):
> mm: pgtable: make pte_offset_map_nolock() return pmdval
> mm: introduce CONFIG_PT_RECLAIM
> mm: pass address information to pmd_install()
> mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single()
> x86: mm: free page table pages by RCU instead of semi RCU
> x86: mm: define arch_flush_tlb_before_set_huge_page
> x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64
>
> Documentation/mm/split_page_table_lock.rst | 3 +-
> arch/arm/mm/fault-armv.c | 2 +-
> arch/powerpc/mm/pgtable.c | 2 +-
> arch/x86/Kconfig | 1 +
> arch/x86/include/asm/pgtable.h | 6 +
> arch/x86/include/asm/tlb.h | 23 ++++
> arch/x86/kernel/paravirt.c | 7 ++
> arch/x86/mm/pgtable.c | 15 ++-
> include/linux/hugetlb.h | 2 +-
> include/linux/mm.h | 13 +-
> include/linux/pgtable.h | 14 +++
> mm/Kconfig | 14 +++
> mm/Makefile | 1 +
> mm/debug_vm_pgtable.c | 2 +-
> mm/filemap.c | 4 +-
> mm/gup.c | 2 +-
> mm/huge_memory.c | 3 +
> mm/internal.h | 17 ++-
> mm/khugepaged.c | 24 +++-
> mm/memory.c | 21 ++--
> mm/migrate_device.c | 2 +-
> mm/mmu_gather.c | 2 +-
> mm/mprotect.c | 8 +-
> mm/mremap.c | 4 +-
> mm/page_vma_mapped.c | 2 +-
> mm/pgtable-generic.c | 21 ++--
> mm/pt_reclaim.c | 131 +++++++++++++++++++++
> mm/userfaultfd.c | 10 +-
> mm/vmscan.c | 2 +-
> 29 files changed, 307 insertions(+), 51 deletions(-)
> create mode 100644 mm/pt_reclaim.c
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages
2024-07-04 7:16 ` [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
@ 2024-07-07 8:26 ` David Hildenbrand
2024-07-08 2:46 ` Qi Zheng
0 siblings, 1 reply; 13+ messages in thread
From: David Hildenbrand @ 2024-07-07 8:26 UTC (permalink / raw)
To: Qi Zheng, the arch/x86 maintainers
Cc: hughd, willy, mgorman, muchun.song, akpm, linux-mm, linux-kernel
On 04.07.24 09:16, Qi Zheng wrote:
> Add the x86 mailing list that I forgot to CC before.
Hi,
I'm planning on looking into this (again, I'm very interested!), but
I'll be a bit (understatement) busy the next 1.5 weeks.
If I don't look into this within the next 3 weeks, an you please remind
me? Thanks!
--
Cheers,
David / dhildenb
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages
2024-07-07 8:26 ` David Hildenbrand
@ 2024-07-08 2:46 ` Qi Zheng
0 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-08 2:46 UTC (permalink / raw)
To: David Hildenbrand
Cc: the arch/x86 maintainers, hughd, willy, mgorman, muchun.song,
akpm, linux-mm, linux-kernel
Hi David,
On 2024/7/7 16:26, David Hildenbrand wrote:
> On 04.07.24 09:16, Qi Zheng wrote:
>> Add the x86 mailing list that I forgot to CC before.
>
> Hi,
>
> I'm planning on looking into this (again, I'm very interested!), but
> I'll be a bit (understatement) busy the next 1.5 weeks.
Thank you very much! Looking forward to your feedback. :)
>
> If I don't look into this within the next 3 weeks, an you please remind
> me? Thanks!
My pleasure! ;)
Thanks,
Qi
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
` (7 preceding siblings ...)
2024-07-04 7:16 ` [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
@ 2024-07-26 9:07 ` Vlastimil Babka (SUSE)
2024-07-29 6:46 ` Qi Zheng
8 siblings, 1 reply; 13+ messages in thread
From: Vlastimil Babka (SUSE) @ 2024-07-26 9:07 UTC (permalink / raw)
To: Qi Zheng, david, hughd, willy, mgorman, muchun.song, akpm
Cc: linux-mm, linux-kernel
On 7/1/24 10:46 AM, Qi Zheng wrote:
> Hi all,
>
> Previously, we tried to use a completely asynchronous method to reclaim empty
> user PTE pages [1]. After discussing with David Hildenbrand, we decided to
> implement synchronous reclaimation in the case of madvise(MADV_DONTNEED) as the
> first step.
>
> So this series aims to synchronously scan and reclaim empty user PTE pages in
> zap_page_range_single() (madvise(MADV_DONTNEED) etc will invoke this). In
> zap_page_range_single(), mmu_gather is used to perform batch tlb flushing and
> page freeing operations. Therefore, if we want to free the empty PTE page in
> this path, the most natural way is to add it to mmu_gather as well. There are
> two problems that need to be solved here:
Hello,
I would be curious to know to what extent you are planning to pursue this
area, whether it's reclaim of empty page tables synchronously for now and
maybe again asychronously later, or you also plan on exploring reclaim of
non-empty page tables.
The reason is I have a master student interested in this topic, so it would
be good to know for the planning.
Thanks a lot,
Vlastimil
> 1. Now, if CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected, mmu_gather will free
> page table pages by semi RCU:
>
> - batch table freeing: asynchronous free by RCU
> - single table freeing: IPI + synchronous free
>
> But this is not enough to free the empty PTE page table pages in paths other
> that munmap and exit_mmap path, because IPI cannot be synchronized with
> rcu_read_lock() in pte_offset_map{_lock}(). So we should let single table
> also be freed by RCU like batch table freeing.
>
> 2. When we use mmu_gather to batch flush tlb and free PTE pages, the TLB is not
> flushed before pmd lock is unlocked. This may result in the following two
> situations:
>
> 1) Userland can trigger page fault and fill a huge page, which will cause
> the existence of small size TLB and huge TLB for the same address.
>
> 2) Userland can also trigger page fault and fill a PTE page, which will
> cause the existence of two small size TLBs, but the PTE page they map
> are different.
>
> For case 1), according to Intel's TLB Application note (317080), some CPUs of
> x86 do not allow it:
>
> ```
> If software modifies the paging structures so that the page size used for a
> 4-KByte range of linear addresses changes, the TLBs may subsequently contain
> both ordinary and large-page translations for the address range.12 A reference
> to a linear address in the address range may use either translation. Which of
> the two translations is used may vary from one execution to another and the
> choice may be implementation-specific.
>
> Software wishing to prevent this uncertainty should not write to a paging-
> structure entry in a way that would change, for any linear address, both the
> page size and either the page frame or attributes. It can instead use the
> following algorithm: first mark the relevant paging-structure entry (e.g.,
> PDE) not present; then invalidate any translations for the affected linear
> addresses (see Section 5.2); and then modify the relevant paging-structure
> entry to mark it present and establish translation(s) for the new page size.
> ```
>
> We can also learn more information from the comments above pmdp_invalidate()
> in __split_huge_pmd_locked().
>
> For case 2), we can see from the comments above ptep_clear_flush() in
> wp_page_copy() that this situation is also not allowed. Even without
> this patch series, madvise(MADV_DONTNEED) can also cause this situation:
>
> CPU 0 CPU 1
>
> madvise (MADV_DONTNEED)
> --> clear pte entry
> pte_unmap_unlock
> touch and tlb miss
> --> set pte entry
> mmu_gather flush tlb
>
> But strangely, I didn't see any relevant fix code, maybe I missed something,
> or is this guaranteed by userland?
>
> Anyway, this series defines the following two functions to be implemented by
> the architecture. If the architecture does not allow the above two situations,
> then define these two functions to flush the tlb before set_pmd_at().
>
> - arch_flush_tlb_before_set_huge_page
> - arch_flush_tlb_before_set_pte_page
>
> As a first step, we supported this feature on x86_64 and selectd the newly
> introduced CONFIG_ARCH_SUPPORTS_PT_RECLAIM.
>
> In order to reduce overhead, we only handle the cases with a high probability
> of generating empty PTE pages, and other cases will be filtered out, such as:
>
> - hugetlb vma (unsuitable)
> - userfaultfd_wp vma (may reinstall the pte entry)
> - writable private file mapping case (COW-ed anon page is not zapped)
> - etc
>
> For userfaultfd_wp and writable private file mapping cases (and MADV_FREE case,
> of course), consider scanning and freeing empty PTE pages asynchronously in
> the future.
>
> This series is based on next-20240627.
>
> Comments and suggestions are welcome!
>
> Thanks,
> Qi
>
> [1]. https://lore.kernel.org/lkml/cover.1718267194.git.zhengqi.arch@bytedance.com/
>
> Qi Zheng (7):
> mm: pgtable: make pte_offset_map_nolock() return pmdval
> mm: introduce CONFIG_PT_RECLAIM
> mm: pass address information to pmd_install()
> mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single()
> x86: mm: free page table pages by RCU instead of semi RCU
> x86: mm: define arch_flush_tlb_before_set_huge_page
> x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64
>
> Documentation/mm/split_page_table_lock.rst | 3 +-
> arch/arm/mm/fault-armv.c | 2 +-
> arch/powerpc/mm/pgtable.c | 2 +-
> arch/x86/Kconfig | 1 +
> arch/x86/include/asm/pgtable.h | 6 +
> arch/x86/include/asm/tlb.h | 23 ++++
> arch/x86/kernel/paravirt.c | 7 ++
> arch/x86/mm/pgtable.c | 15 ++-
> include/linux/hugetlb.h | 2 +-
> include/linux/mm.h | 13 +-
> include/linux/pgtable.h | 14 +++
> mm/Kconfig | 14 +++
> mm/Makefile | 1 +
> mm/debug_vm_pgtable.c | 2 +-
> mm/filemap.c | 4 +-
> mm/gup.c | 2 +-
> mm/huge_memory.c | 3 +
> mm/internal.h | 17 ++-
> mm/khugepaged.c | 24 +++-
> mm/memory.c | 21 ++--
> mm/migrate_device.c | 2 +-
> mm/mmu_gather.c | 2 +-
> mm/mprotect.c | 8 +-
> mm/mremap.c | 4 +-
> mm/page_vma_mapped.c | 2 +-
> mm/pgtable-generic.c | 21 ++--
> mm/pt_reclaim.c | 131 +++++++++++++++++++++
> mm/userfaultfd.c | 10 +-
> mm/vmscan.c | 2 +-
> 29 files changed, 307 insertions(+), 51 deletions(-)
> create mode 100644 mm/pt_reclaim.c
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages
2024-07-26 9:07 ` Vlastimil Babka (SUSE)
@ 2024-07-29 6:46 ` Qi Zheng
0 siblings, 0 replies; 13+ messages in thread
From: Qi Zheng @ 2024-07-29 6:46 UTC (permalink / raw)
To: Vlastimil Babka (SUSE)
Cc: david, hughd, willy, mgorman, muchun.song, akpm, linux-mm,
linux-kernel
Hi Vlastimil,
On 2024/7/26 17:07, Vlastimil Babka (SUSE) wrote:
> On 7/1/24 10:46 AM, Qi Zheng wrote:
>> Hi all,
>>
>> Previously, we tried to use a completely asynchronous method to reclaim empty
>> user PTE pages [1]. After discussing with David Hildenbrand, we decided to
>> implement synchronous reclaimation in the case of madvise(MADV_DONTNEED) as the
>> first step.
>>
>> So this series aims to synchronously scan and reclaim empty user PTE pages in
>> zap_page_range_single() (madvise(MADV_DONTNEED) etc will invoke this). In
>> zap_page_range_single(), mmu_gather is used to perform batch tlb flushing and
>> page freeing operations. Therefore, if we want to free the empty PTE page in
>> this path, the most natural way is to add it to mmu_gather as well. There are
>> two problems that need to be solved here:
>
> Hello,
Thank you for your attention to this patch series!
>
> I would be curious to know to what extent you are planning to pursue this
> area, whether it's reclaim of empty page tables synchronously for now and
> maybe again asychronously later, or you also plan on exploring reclaim of
> non-empty page tables.
As I discussed with David Hildenbrand, I am currently planning to
implement synchronous empty user page table reclamation first for the
following reasons:
1. It covers most of the known cases.
2. As a first step, it helps verify the lock protection scheme, tlb
flushing, and other infrastructure.
Later, I plan to implement asynchronous reclamation for MADV_FREE and
other situations. The initial idea is to mark vma first, then add the
corresponding mm to a global linked list, and then perform asynchronous
scanning and reclamation in the memory reclamation process.
For exploring reclaim of non-empty page tables, no plan yet. But I have
another plan, which is to remove the page table from the protection of
the mmap lock:
1. free all levels of page table pages by RCU, not just PTE pages, but
also pmd, pud, etc.
2. similar to pte_offset_map/pte_unmap, add
[pmd|pud]_offset_map/[pmd|pud]_unmap, and make them all contain
rcu_read_lock/rcu_read_unlcok, and make them accept failure.
In this way, we no longer need the mmap lcok. For readers, such as page
table wallers, we are already in the critical section of RCU. For
writers, we only need to hold the page table lock.
But there is a difficulty here, that is, the RCU critical section is not
allowed to sleep, but it is possible to sleep in the callback function
of .pmd_entry, such as mmu_notifier_invalidate_range_start().
Use SRCU instead? Not sure.
>
> The reason is I have a master student interested in this topic, so it would
> be good to know for the planning.
This is great, comments and suggestions are welcome!
Thanks,
Qi
>
> Thanks a lot,
> Vlastimil
>
>> 1. Now, if CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected, mmu_gather will free
>> page table pages by semi RCU:
>>
>> - batch table freeing: asynchronous free by RCU
>> - single table freeing: IPI + synchronous free
>>
>> But this is not enough to free the empty PTE page table pages in paths other
>> that munmap and exit_mmap path, because IPI cannot be synchronized with
>> rcu_read_lock() in pte_offset_map{_lock}(). So we should let single table
>> also be freed by RCU like batch table freeing.
>>
>> 2. When we use mmu_gather to batch flush tlb and free PTE pages, the TLB is not
>> flushed before pmd lock is unlocked. This may result in the following two
>> situations:
>>
>> 1) Userland can trigger page fault and fill a huge page, which will cause
>> the existence of small size TLB and huge TLB for the same address.
>>
>> 2) Userland can also trigger page fault and fill a PTE page, which will
>> cause the existence of two small size TLBs, but the PTE page they map
>> are different.
>>
>> For case 1), according to Intel's TLB Application note (317080), some CPUs of
>> x86 do not allow it:
>>
>> ```
>> If software modifies the paging structures so that the page size used for a
>> 4-KByte range of linear addresses changes, the TLBs may subsequently contain
>> both ordinary and large-page translations for the address range.12 A reference
>> to a linear address in the address range may use either translation. Which of
>> the two translations is used may vary from one execution to another and the
>> choice may be implementation-specific.
>>
>> Software wishing to prevent this uncertainty should not write to a paging-
>> structure entry in a way that would change, for any linear address, both the
>> page size and either the page frame or attributes. It can instead use the
>> following algorithm: first mark the relevant paging-structure entry (e.g.,
>> PDE) not present; then invalidate any translations for the affected linear
>> addresses (see Section 5.2); and then modify the relevant paging-structure
>> entry to mark it present and establish translation(s) for the new page size.
>> ```
>>
>> We can also learn more information from the comments above pmdp_invalidate()
>> in __split_huge_pmd_locked().
>>
>> For case 2), we can see from the comments above ptep_clear_flush() in
>> wp_page_copy() that this situation is also not allowed. Even without
>> this patch series, madvise(MADV_DONTNEED) can also cause this situation:
>>
>> CPU 0 CPU 1
>>
>> madvise (MADV_DONTNEED)
>> --> clear pte entry
>> pte_unmap_unlock
>> touch and tlb miss
>> --> set pte entry
>> mmu_gather flush tlb
>>
>> But strangely, I didn't see any relevant fix code, maybe I missed something,
>> or is this guaranteed by userland?
>>
>> Anyway, this series defines the following two functions to be implemented by
>> the architecture. If the architecture does not allow the above two situations,
>> then define these two functions to flush the tlb before set_pmd_at().
>>
>> - arch_flush_tlb_before_set_huge_page
>> - arch_flush_tlb_before_set_pte_page
>>
>> As a first step, we supported this feature on x86_64 and selectd the newly
>> introduced CONFIG_ARCH_SUPPORTS_PT_RECLAIM.
>>
>> In order to reduce overhead, we only handle the cases with a high probability
>> of generating empty PTE pages, and other cases will be filtered out, such as:
>>
>> - hugetlb vma (unsuitable)
>> - userfaultfd_wp vma (may reinstall the pte entry)
>> - writable private file mapping case (COW-ed anon page is not zapped)
>> - etc
>>
>> For userfaultfd_wp and writable private file mapping cases (and MADV_FREE case,
>> of course), consider scanning and freeing empty PTE pages asynchronously in
>> the future.
>>
>> This series is based on next-20240627.
>>
>> Comments and suggestions are welcome!
>>
>> Thanks,
>> Qi
>>
>> [1]. https://lore.kernel.org/lkml/cover.1718267194.git.zhengqi.arch@bytedance.com/
>>
>> Qi Zheng (7):
>> mm: pgtable: make pte_offset_map_nolock() return pmdval
>> mm: introduce CONFIG_PT_RECLAIM
>> mm: pass address information to pmd_install()
>> mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single()
>> x86: mm: free page table pages by RCU instead of semi RCU
>> x86: mm: define arch_flush_tlb_before_set_huge_page
>> x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64
>>
>> Documentation/mm/split_page_table_lock.rst | 3 +-
>> arch/arm/mm/fault-armv.c | 2 +-
>> arch/powerpc/mm/pgtable.c | 2 +-
>> arch/x86/Kconfig | 1 +
>> arch/x86/include/asm/pgtable.h | 6 +
>> arch/x86/include/asm/tlb.h | 23 ++++
>> arch/x86/kernel/paravirt.c | 7 ++
>> arch/x86/mm/pgtable.c | 15 ++-
>> include/linux/hugetlb.h | 2 +-
>> include/linux/mm.h | 13 +-
>> include/linux/pgtable.h | 14 +++
>> mm/Kconfig | 14 +++
>> mm/Makefile | 1 +
>> mm/debug_vm_pgtable.c | 2 +-
>> mm/filemap.c | 4 +-
>> mm/gup.c | 2 +-
>> mm/huge_memory.c | 3 +
>> mm/internal.h | 17 ++-
>> mm/khugepaged.c | 24 +++-
>> mm/memory.c | 21 ++--
>> mm/migrate_device.c | 2 +-
>> mm/mmu_gather.c | 2 +-
>> mm/mprotect.c | 8 +-
>> mm/mremap.c | 4 +-
>> mm/page_vma_mapped.c | 2 +-
>> mm/pgtable-generic.c | 21 ++--
>> mm/pt_reclaim.c | 131 +++++++++++++++++++++
>> mm/userfaultfd.c | 10 +-
>> mm/vmscan.c | 2 +-
>> 29 files changed, 307 insertions(+), 51 deletions(-)
>> create mode 100644 mm/pt_reclaim.c
>>
>
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2024-07-29 6:47 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-01 8:46 [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 1/7] mm: pgtable: make pte_offset_map_nolock() return pmdval Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 2/7] mm: introduce CONFIG_PT_RECLAIM Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 3/7] mm: pass address information to pmd_install() Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 4/7] mm: pgtable: try to reclaim empty PTE pages in zap_page_range_single() Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 5/7] x86: mm: free page table pages by RCU instead of semi RCU Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 6/7] x86: mm: define arch_flush_tlb_before_set_huge_page Qi Zheng
2024-07-01 8:46 ` [RFC PATCH 7/7] x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64 Qi Zheng
2024-07-04 7:16 ` [RFC PATCH 0/7] synchronously scan and reclaim empty user PTE pages Qi Zheng
2024-07-07 8:26 ` David Hildenbrand
2024-07-08 2:46 ` Qi Zheng
2024-07-26 9:07 ` Vlastimil Babka (SUSE)
2024-07-29 6:46 ` Qi Zheng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).