Skip to contentSkip to conversation

Implement a new generic pagewalk API

RFC PATCH In review Oscar Salvador 6 participants 16 messages May 25 – Jun 11

Subsystems (14)
Memory management x86 PowerPC MM core +10 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover Implement a new generic pagewalk API

Changelog:
rfcv2 -> rfcv3:
- Fix an out-of-bounds write
- Convert clear_refs to the new API
- Fix issue when reading cont-PMDs
rfc -> rfcv2:
- Add pte_hole functionality
- Fix pagemap issues
- Fix shmem in smap
- Testing with pagemap "testsuite"

[WARNING]

This is not yet fully complete, but before investing more time into it I would like to know whether 1) this is heading into the right direction and 2) this is something we are still interested in.
There are still things that need work:

- convert make_uffd_wp_huge_pte: Since hugetlb is being dealt like a pte, we inherited PTE_MARKERs for it when those came into play, and AFAIK, those are being used mostly for UFFD.
From here on we have two options: 1) find another way to deal with UFFD without markers or 2) introduce markers for PMD and PUD level. I am leaning towards option 1), because 2) seems a bit unfair. I still need to put some thought into it and see how we can achieve that.

- Teach the new API how to use other kind of locks. E.g: pagemap scan needs to take i_mmap_lock during the scanning, so we need to able to take that lock. I have some ideas to do that, but something for the new version.

- Find corner-cases and fix them.

Kudos go to David, who was the person suggesting the interface and he gave me some ideas where to begin, besides providing feedback on early stages (in case there is something stupid don't blame him, blame me)

Also, I would like to thank Vlastimil, who helped me running this patchset quite a few times through Claude, to catch some fixes.

[/WARNING]

[TESTING]
Part of the testing has been to duplicate /proc/$$/(pagemap,smaps,numa_maps,clear_refs) and have the same with _lab extension linked to the old API.
In that way I could check whether the outcome from e.g: /proc/$$/smaps and /proc/$$/smaps_lab was the same for any given program. The same I did for pagemap and numa_maps.

Also, regarding pagemap:
So far, tools/mm/page-types.c reports the right outcome (compared to the old API), and tools/testing/selftests/mm/pagemap_ioctl.c only reports 4 failing tests. Although to be honest, I do not how much should I trust that one because if I add a few delays in the userspace code, some tests that were failing before are not now, so yeah.

localhost:~/workspace # ./page-types -p 1168

              flags     page-count       MB  symbolic-flags                     long-symbolic-flags
 0x0000000000000800              1        0  ___________M_______________________________        mmap
 0x0000000000000828              2        0  ___U_l_____M_______________________________        uptodate,lru,mmap
 0x000000000000082c              1        0  __RU_l_____M_______________________________        referenced,uptodate,lru,mmap
 0x0000000000004838              1        0  ___UDl_____M__b____________________________        uptodate,dirty,lru,mmap,swapbacked
 0x000000000000086c            423        1  __RU_lA____M_______________________________        referenced,uptodate,lru,active,mmap
 0x0000000000205828             29        0  ___U_l_____Ma_b______x_____________________        uptodate,lru,mmap,anonymous,swapbacked,ksm
 0x000000000020586c              1        0  __RU_lA____Ma_b______x_____________________        referenced,uptodate,lru,active,mmap,anonymous,swapbacked,ksm

total 458 1

localhost:~/workspace # ./page-types_lab -p 1168

              flags     page-count       MB  symbolic-flags                     long-symbolic-flags
 0x0000000000000804              1        0  __R________M_______________________________        referenced,mmap
 0x0000000000000828              2        0  ___U_l_____M_______________________________        uptodate,lru,mmap
 0x000000000000082c              1        0  __RU_l_____M_______________________________        referenced,uptodate,lru,mmap
 0x0000000000004838              1        0  ___UDl_____M__b____________________________        uptodate,dirty,lru,mmap,swapbacked
 0x000000000000086c            423        1  __RU_lA____M_______________________________        referenced,uptodate,lru,active,mmap
 0x0000000000205828             29        0  ___U_l_____Ma_b______x_____________________        uptodate,lru,mmap,anonymous,swapbacked,ksm
 0x000000000020586c              1        0  __RU_lA____Ma_b______x_____________________        referenced,uptodate,lru,active,mmap,anonymous,swapbacked,ksm

total 458 1

page-types being using the new API and page-types_lab the old one.

 # ./pagemap_ioctl
 TAP version 13
 1..117
 ok 1 sanity_tests_sd Zero range size is valid
 ok 2 sanity_tests_sd output bu
 ok 35 Walk_end: 1 max page
 ok 36 Page testing: all new pages must not be written (dirty)
 ok 37 Page testing: all pages must be written (dirty)
 ok 38 Page testing: all pages dirty other than first and the last one
 ok 39 Page testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 40 Page testing: only middle page dirty
 ok 41 Page testing: only two middle pages dirty
 ok 42 Large Page testing: all new pages must not be written (dirty)
 ok 43 Large Page testing: all pages must be written (dirty)
 ok 44 Large Page testing: all pages dirty other than first and the last one
 ok 45 Large Page testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 46 Large Page testing: only middle page dirty
 ok 47 Large Page testing: only two middle pages dirty
 ok 48 Huge page testing: all new pages must not be written (dirty)
 ok 49 Huge page testing: all pages must be written (dirty)
 ok 50 Huge page testing: all pages dirty other than first and the last one
 ok 51 Huge page testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 52 Huge page testing: only middle page dirty
 ok 53 Huge page testing: only two middle pages dirty
 ok 54 Hugetlb shmem testing: all new pages must not be written (dirty)
 ok 55 Hugetlb shmem testing: all pages must be written (dirty)
 ok 56 Hugetlb shmem testing: all pages dirty other than first and the last one
 ok 57 Hugetlb shmem testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 58 Hugetlb shmem testing: only middle page dirty
 not ok 59 Hugetlb shmem testing: only two middle pages dirty
 ok 60 Hugetlb mem testing: all new pages must not be written (dirty)
 ok 61 Hugetlb mem testing: all pages must be written (dirty)
 ok 62 Hugetlb mem testing: all pages dirty other than first and the last one
 ok 63 Hugetlb mem testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 64 Hugetlb mem testing: only middle page dirty
 not ok 65 Hugetlb mem testing: only two middle pages dirty
 ok 66 Hugetlb shmem testing: all new pages must not be written (dirty)
 ok 67 Hugetlb shmem testing: all pages must be written (dirty)
 ok 68 Hugetlb shmem testing: all pages dirty other than first and the last one
 ok 69 Hugetlb shmem testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 70 Hugetlb shmem testing: only middle page dirty
 not ok 71 Hugetlb shmem testing: only two middle pages dirty
 ok 72 File memory testing: all new pages must not be written (dirty)
 ok 73 File memory testing: all p
 # Totals: pass:113 fail:4 xfail:0 xpass:0 skip:0 error:0

[/TESTING]

In the LSFMM/BFP 2025, there was a general agreement that we 1) would like to have a generic pagewalk API 2) that replaces the existing one with callbacks if possible and 3) that HugeTLB can use without the need to special case it (e.g: not having to depend on .hugetlb_entry callbacks)., which means having a lot of duplicated code and also having a lot of special casing just because hugetlb lore.

pt_range_walk API tries to do that and replaces the old behaviour of "in HugeTLB world everything reads as a PTE" and starts reading HugeTLB entries the way they really are, that means interpreting them as PMD/PUD entries and contiguous-PMD/PTE entries.

In order to achieve that, we need some infrastructure we did not really need until know, in order to be able to read HugeTLB pages as PUD/PMD entries. E.g: softleaf_from_pud had to be added and some other pud_* functions.

In a few words, this API goes through an address range and returns whatever it is in there (swap/hwpoison/migration/marker entries, folios, pfn and device entries, or nothing).

These are the internal return types the API uses:

PT_TYPE_NONE
PT_TYPE_FOLIO
PT_TYPE_MARKER
PT_TYPE_PFN
PT_TYPE_SWAP
PT_TYPE_MIGRATION
PT_TYPE_DEVICE
PT_TYPE_HWPOISON

The API also handles locking and batching itself, so the caller does not really need to bother with that.

In order to handle contiguous-PMD mapped hugetlb pages, folio_pmd_batch, which is an analogous of folio_pte_batch, has been implemented.

More information about the API can be found in patch #4.

This was tested on x86_64 and arm64, but as I said, it is still incomplete, therefore the RFC, to gather some initial feedback before investing more time into this.

For now, all users of the old API from fs/proc/task_mmu.c have been
converted: /proc/pid/(smaps|numa_maps|pagemap|clear_refs).

Thanks in advance

Oscar Salvador (8):
mm: Add softleaf_from_pud
mm: Add {pmd,pud}_huge_lock helper
mm: Implement folio_pmd_batch
mm: Implement pt_range_walk
mm: Make /proc/pid/smaps use the new generic pagewalk API
mm: Make /proc/pid/numa_maps use the new generic pagewalk API
mm: Make /proc/pid/pagemap use the new generic pagewalk API
mm: Make /proc/pid/clear_refs use the new generic pagewalk API

 arch/arm64/include/asm/pgtable.h             |   41 +
 arch/loongarch/include/asm/pgtable.h         |    1 +
 arch/powerpc/include/asm/book3s/64/pgtable.h |    7 +
 arch/s390/include/asm/pgtable.h              |   38 +
 arch/x86/include/asm/pgtable.h               |   53 +
 arch/x86/include/asm/pgtable_64.h            |    2 +
 arch/x86/mm/pgtable.c                        |   18 +-
 fs/proc/task_mmu.c                           | 2295 ++++++++----------
 include/asm-generic/pgtable_uffd.h           |   15 +
 include/linux/leafops.h                      |   46 +
 include/linux/mm.h                           |    2 +
 include/linux/mm_inline.h                    |   32 +
 include/linux/pagewalk.h                     |  106 +
 include/linux/pgtable.h                      |   95 +
 mm/internal.h                                |   75 +-
 mm/memory.c                                  |   22 +
 mm/pagewalk.c                                |  400 +++
 mm/pgtable-generic.c                         |   21 +
 18 files changed, 2039 insertions(+), 1230 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

RFC PATCH v3 1/8 mm: Add softleaf_from_pud

We want to be able to operate on HugeTLB pages as we do with normal pages, which means stop pretending everyting is a pte in HugeTLB world and be able to operate on the right entry level.

Since we can have HugeTLB as PUD entries, we need the infrastructure that allows us to operate on them, so add softleaf_from_pud(), and the infrastructure that comes with it.

---
 arch/arm64/include/asm/pgtable.h             | 12 +++++
 arch/loongarch/include/asm/pgtable.h         |  1 +
 arch/powerpc/include/asm/book3s/64/pgtable.h |  7 +++
 arch/s390/include/asm/pgtable.h              | 38 ++++++++++++++++
 arch/x86/include/asm/pgtable.h               | 48 ++++++++++++++++++++
 arch/x86/include/asm/pgtable_64.h            |  2 +
 include/asm-generic/pgtable_uffd.h           | 15 ++++++
 include/linux/leafops.h                      | 33 ++++++++++++++
 include/linux/pgtable.h                      | 37 +++++++++++++++
 9 files changed, 193 insertions(+)
arch/arm64/include/asm/pgtable.h +12 -0
index 4dfa42b7d053..ca0f1fcae7e8 100644
@@ -593,6 +593,13 @@ static inline int pmd_protnone(pmd_t pmd)
 #define pmd_mkvalid_k(pmd)	pte_pmd(pte_mkvalid_k(pmd_pte(pmd)))
 #define pmd_mkinvalid(pmd)	pte_pmd(pte_mkinvalid(pmd_pte(pmd)))
 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
+#define pud_uffd_wp(pud)	pte_uffd_wp(pud_pte(pud))
+#define pud_mkuffd_wp(pud)	pte_pud(pte_mkuffd_wp(pud_pte(pud)))
+#define pud_clear_uffd_wp(pud)	pte_pud(pte_clear_uffd_wp(pud_pte(pud)))
+#define pud_swp_uffd_wp(pud)	pte_swp_uffd_wp(pud_pte(pud))
+#define pud_swp_mkuffd_wp(pud)	pte_pud(pte_swp_mkuffd_wp(pud_pte(pud)))
+#define pud_swp_clear_uffd_wp(pud) \
+				pte_pud(pte_swp_clear_uffd_wp(pud_pte(pud)))
 #define pmd_uffd_wp(pmd)	pte_uffd_wp(pmd_pte(pmd))
 #define pmd_mkuffd_wp(pmd)	pte_pmd(pte_mkuffd_wp(pmd_pte(pmd)))
 #define pmd_clear_uffd_wp(pmd)	pte_pmd(pte_clear_uffd_wp(pmd_pte(pmd)))
@@ -1539,6 +1546,11 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
 #define __swp_entry_to_pmd(swp)		__pmd((swp).val)
 #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
 
+#ifdef CONFIG_HUGETLB_PAGE
+#define __pud_to_swp_entry(pud)         ((swp_entry_t) { pud_val(pud) })
+#define __swp_entry_to_pud(swp)         __pud((swp).val)
+#endif
+
 /*
  * Ensure that there are not more swap files than can be encoded in the kernel
  * PTEs.
arch/loongarch/include/asm/pgtable.h +1 -0
index 2a0b63ae421f..dc6d841ea269 100644
@@ -339,6 +339,7 @@ static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
 #define __swp_entry_to_pmd(x)	__pmd((x).val | _PAGE_HUGE)
 #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
 #define __pmd_to_swp_entry(pmd) ((swp_entry_t) { pmd_val(pmd) })
+#define __pud_to_swp_entry(pud) ((swp_entry_t) { pud_val(pud) })
 
 static inline bool pte_swp_exclusive(pte_t pte)
 {
arch/powerpc/include/asm/book3s/64/pgtable.h +7 -0
index e67e64ac6e8c..fb43e7cc09b6 100644
@@ -1065,6 +1065,13 @@ static inline pte_t *pmdp_ptep(pmd_t *pmd)
 #define pmd_swp_soft_dirty(pmd)		pte_swp_soft_dirty(pmd_pte(pmd))
 #define pmd_swp_clear_soft_dirty(pmd)	pte_pmd(pte_swp_clear_soft_dirty(pmd_pte(pmd)))
 #endif
+
+#ifdef CONFIG_HUGETLB_PAGE
+#define pud_swp_mksoft_dirty(pud)	pte_pud(pte_swp_mksoft_dirty(pud_pte(pud)))
+#define pud_swp_soft_dirty(pud)		pte_swp_soft_dirty(pud_pte(pud))
+#define pud_swp_clear_soft_dirty(pud)	pte_pud(pte_swp_clear_soft_dirty(pud_pte(pud)))
+#endif
+
 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
 
 #ifdef CONFIG_NUMA_BALANCING
arch/s390/include/asm/pgtable.h +38 -0
index 2c6cee8241e0..c0ebf827fdb0 100644
@@ -903,11 +903,31 @@ static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
 	return clear_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_SOFT_DIRTY));
 }
 
+static inline int pud_soft_dirty(pud_t pud)
+{
+	return pud_val(pud) & _REGION3_ENTRY_SOFT_DIRTY;
+}
+
+static inline pud_t pud_mksoft_dirty(pud_t pud)
+{
+	return set_pud_bit(pud, __pgprot(_REGION3_ENTRY_SOFT_DIRTY));
+}
+
+static inline pud_t pud_clear_soft_dirty(pud_t pud)
+{
+	return clear_pud_bit(pud, __pgprot(_REGION3_ENTRY_SOFT_DIRTY));
+}
+
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
 #define pmd_swp_soft_dirty(pmd)		pmd_soft_dirty(pmd)
 #define pmd_swp_mksoft_dirty(pmd)	pmd_mksoft_dirty(pmd)
 #define pmd_swp_clear_soft_dirty(pmd)	pmd_clear_soft_dirty(pmd)
 #endif
+#ifdef CONFIG_HUGETLB_PAGE
+#define pud_swp_soft_dirty(pud)		pud_soft_dirty(pud)
+#define pud_swp_mksoft_dirty(pud)	pud_mksoft_dirty(pud)
+#define pud_swp_clear_soft_dirty(pud)	pud_clear_soft_dirty(pud)
+#endif
 
 /*
  * query functions pte_write/pte_dirty/pte_young only work if
@@ -1947,6 +1967,24 @@ static inline unsigned long __swp_offset_rste(swp_entry_t entry)
  * requires conversion of the swap type and offset, and not all the possible
  * PTE bits.
  */
+static inline swp_entry_t __pud_to_swp_entry(pud_t pud)
+{
+	swp_entry_t arch_entry;
+	pte_t pte;
+
+	arch_entry = __rste_to_swp_entry(pud_val(pud));
+	pte = mk_swap_pte(__swp_type_rste(arch_entry), __swp_offset_rste(arch_entry));
+	return __pte_to_swp_entry(pte);
+}
+
+static inline pud_t __swp_entry_to_pud(swp_entry_t arch_entry)
+{
+	pud_t pud;
+
+	pud = __pud(mk_swap_rste(__swp_type(arch_entry), __swp_offset(arch_entry)));
+	return pud;
+}
+
 static inline swp_entry_t __pmd_to_swp_entry(pmd_t pmd)
 {
 	swp_entry_t arch_entry;
arch/x86/include/asm/pgtable.h +48 -0

66 lines not on this page · raw message

arch/x86/include/asm/pgtable_64.h +2 -0
index ce45882ccd07..0709dee52813 100644
@@ -234,8 +234,10 @@ static inline void native_pgd_clear(pgd_t *pgd)
 
 #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val((pte)) })
 #define __pmd_to_swp_entry(pmd)		((swp_entry_t) { pmd_val((pmd)) })
+#define __pud_to_swp_entry(pud)		((swp_entry_t) { pud_val((pud)) })
 #define __swp_entry_to_pte(x)		(__pte((x).val))
 #define __swp_entry_to_pmd(x)		(__pmd((x).val))
+#define __swp_entry_to_pud(x)		(__pud((x).val))
 
 extern void cleanup_highmap(void);
 
include/asm-generic/pgtable_uffd.h +15 -0
index 0d85791efdf7..59c9d6762ec8 100644
@@ -78,6 +78,21 @@ static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd)
 {
 	return pmd;
 }
+
+static inline pud_t pud_swp_mkuffd_wp(pud_t pud)
+{
+	return pud;
+}
+
+static inline int pud_swp_uffd_wp(pud_t pud)
+{
+	return 0;
+}
+
+static inline pud_t pud_swp_clear_uffd_wp(pud_t pud)
+{
+	return pud;
+}
 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
 
 #endif /* _ASM_GENERIC_PGTABLE_UFFD_H */
include/linux/leafops.h +33 -0
index 992cd8bd8ed0..08646398b0fe 100644
@@ -117,6 +117,39 @@ static inline softleaf_t softleaf_from_pmd(pmd_t pmd)
 
 #endif
 
+#ifdef CONFIG_HUGETLB_PAGE
+/**
+ * softleaf_from_pud() - Obtain a leaf entry from a PUD entry.
+ * @pud: PUD entry.
+ *
+ * If @pud is present (therefore not a leaf entry) the function returns an empty
+ * leaf entry. Otherwise, it returns a leaf entry.
+ *
+ * Returns: Leaf entry.
+ */
+static inline softleaf_t softleaf_from_pud(pud_t pud)
+{
+	softleaf_t arch_entry;
+
+	if (pud_present(pud) || pud_none(pud))
+		return softleaf_mk_none();
+
+	if (pud_swp_soft_dirty(pud))
+		pud = pud_swp_clear_soft_dirty(pud);
+	if (pud_swp_uffd_wp(pud))
+		pud = pud_swp_clear_uffd_wp(pud);
+	arch_entry = __pud_to_swp_entry(pud);
+
+	/* Temporary until swp_entry_t eliminated. */
+	return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
+}
+#else
+static inline softleaf_t softleaf_from_pud(pud_t pud)
+{
+	return softleaf_mk_none();
+}
+#endif
+
 /**
  * softleaf_is_none() - Is the leaf entry empty?
  * @entry: Leaf entry.
include/linux/pgtable.h +37 -0
index cdd68ed3ae1a..70aae957be5b 100644
@@ -1797,6 +1797,22 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
 	return pmd;
 }
 #endif
+#ifndef CONFIG_HUGETLB_PAGE
+static inline pud_t pud_swp_mksoft_dirty(pud_t pud)
+{
+	return pud;
+}
+
+static inline int pud_swp_soft_dirty(pud_t pud)
+{
+	return 0;
+}
+
+static inline pud_t pud_swp_clear_soft_dirty(pud_t pud)
+{
+	return pud;
+}
+#endif
 #else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */
 static inline int pte_soft_dirty(pte_t pte)
 {
@@ -1857,6 +1873,21 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
 {
 	return pmd;
 }
+
+static inline pud_t pud_swp_mksoft_dirty(pud_t pud)
+{
+	return pud;
+}
+
+static inline int pud_swp_soft_dirty(pud_t pud)
+{
+	return 0;
+}
+
+static inline pud_t pud_swp_clear_soft_dirty(pud_t pud)
+{
+	return pud;
+}
 #endif
 
 #ifndef __HAVE_PFNMAP_TRACKING
@@ -2420,4 +2451,10 @@ pgprot_t vm_get_page_prot(vm_flags_t vm_flags)				\
 }									\
 EXPORT_SYMBOL(vm_get_page_prot);
 
+#ifdef CONFIG_HUGETLB_PAGE
+#ifndef __pud_to_swp_entry
+#define __pud_to_swp_entry(pud) ((swp_entry_t) { pud_val(pud) })
+#endif
+#endif
+
 #endif /* _LINUX_PGTABLE_H */
Signature · 1 line
2.53.0
#2 lore raw

RFC PATCH v3 2/8 mm: Add {pmd,pud}_huge_lock helper

In reply to Oscar Salvador

HugeTLB and THP use the same lock for pud and pmd,
so create two helpers that can be directly used by both of them, as they will be used in the generic pagewalkers.

---
 include/linux/mm_inline.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
include/linux/mm_inline.h +32 -0
index a171070e15f0..93637890cbeb 100644
@@ -667,4 +667,36 @@ static inline size_t num_pages_contiguous(struct page **pages, size_t nr_pages)
 	return i;
 }
 
+static inline spinlock_t *pmd_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
+{
+	spinlock_t *ptl;
+
+	if (pmd_present(*pmd) || !pmd_none(*pmd)) {
+		ptl = pmd_lock(vma->vm_mm, pmd);
+		if (pmd_present(*pmd) && pmd_leaf(*pmd))
+			return ptl;
+		else if (!pmd_present(*pmd) && !pmd_none(*pmd))
+			return ptl;
+		spin_unlock(ptl);
+	}
+
+	return NULL;
+}
+
+static inline spinlock_t *pud_huge_lock(pud_t *pud, struct vm_area_struct *vma)
+{
+	spinlock_t *ptl;
+
+	if (pud_present(*pud) || !pud_none(*pud)) {
+		ptl = pud_lock(vma->vm_mm, pud);
+		if (pud_present(*pud) && pud_leaf(*pud))
+			return ptl;
+		else if (!pud_present(*pud) && !pud_none(*pud))
+			return ptl;
+		spin_unlock(ptl);
+	}
+
+	return NULL;
+}
+
 #endif
Signature · 1 line
2.53.0
#3 lore raw

RFC PATCH v3 3/8 mm: Implement folio_pmd_batch

In reply to Oscar Salvador

HugeTLB can be mapped as contiguous PMDs, so we need a way to be able to batch them as we do for contiguous PTEs.
Implement folio_pmd_batch in order to do that.

---
 arch/arm64/include/asm/pgtable.h | 19 ++++++++
 include/linux/pgtable.h          | 28 ++++++++++++
 mm/internal.h                    | 75 +++++++++++++++++++++++++++++++-
 3 files changed, 121 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +19 -0
index ca0f1fcae7e8..08ae4ee7d1da 100644
@@ -164,6 +164,8 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
 	(__boundary - 1 < (end) - 1) ? __boundary : (end);			\
 })
 
+#define pmd_valid_cont(pmd)    (pmd_valid(pmd) && pmd_cont(pmd))
+
 #define pte_hw_dirty(pte)	(pte_write(pte) && !pte_rdonly(pte))
 #define pte_sw_dirty(pte)	(!!(pte_val(pte) & PTE_DIRTY))
 #define pte_dirty(pte)		(pte_sw_dirty(pte) || pte_hw_dirty(pte))
@@ -669,6 +671,12 @@ static inline pgprot_t pmd_pgprot(pmd_t pmd)
 	return __pgprot(pmd_val(pfn_pmd(pfn, __pgprot(0))) ^ pmd_val(pmd));
 }
 
+#define pmd_advance_pfn pmd_advance_pfn
+static inline pmd_t pmd_advance_pfn(pmd_t pmd, unsigned long nr)
+{
+	return pfn_pmd(pmd_pfn(pmd) + nr, pmd_pgprot(pmd));
+}
+
 #define pud_pgprot pud_pgprot
 static inline pgprot_t pud_pgprot(pud_t pud)
 {
@@ -1656,6 +1664,17 @@ extern void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long ad
 				    pte_t *ptep, pte_t old_pte, pte_t pte,
 				    unsigned int nr);
 
+#ifdef CONFIG_HUGETLB_PAGE
+#define pmd_batch_hint pmd_batch_hint
+static inline unsigned int pmd_batch_hint(pmd_t *pmdp, pmd_t pmd)
+{
+	if (!pmd_valid_cont(pmd))
+		return 1;
+
+	return CONT_PMDS - (((unsigned long)pmdp >> 3) & (CONT_PMDS - 1));
+}
+#endif
+
 #ifdef CONFIG_ARM64_CONTPTE
 
 /*
include/linux/pgtable.h +28 -0
index 70aae957be5b..f5291f9ce583 100644
@@ -358,6 +358,34 @@ static inline void lazy_mmu_mode_pause(void) {}
 static inline void lazy_mmu_mode_resume(void) {}
 #endif
 
+#ifndef pmd_batch_hint
+/**
+ * pmd_batch_hint - Number of PMD entries that can be added to batch without scanning.
+ * @pmdp: Page table pointer for the entry.
+ * @pmd: Page table entry.
+ *
+ * Some architectures know that a set of contiguous pmds all map the same
+ * contiguous memory with the same permissions. In this case, it can provide a
+ * hint to aid pmd batching without the core code needing to scan every pmd.
+ *
+ * An architecture implementation may ignore the PMD accessed state. Further,
+ * the dirty state must apply atomically to all the PMDs described by the hint.
+ *
+ * May be overridden by the architecture, else pmd_batch_hint is always 1.
+ */
+static inline unsigned int pmd_batch_hint(pmd_t *pmdp, pmd_t pmd)
+{
+	return 1;
+}
+#endif
+
+#ifndef pmd_advance_pfn
+static inline pmd_t pmd_advance_pfn(pmd_t pmd, unsigned long nr)
+{
+	return __pmd(pmd_val(pmd) + (nr << PFN_PTE_SHIFT));
+}
+#endif
+
 #ifndef pte_batch_hint
 /**
  * pte_batch_hint - Number of pages that can be added to batch without scanning.
mm/internal.h +74 -1

87 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

RFC PATCH v3 4/8 mm: Implement pt_range_walk

In reply to Oscar Salvador

Implement pt_range_walk, which is a pagewalk API that implements locking and batching itself, and returns a struct containing information about the address space which is backed by the vma.
It goes through the address range provided, and returns whatever it find there, softleaf entries, folios, etc. and information about the entry itself like whether it is dirty, shared, present, size of the entry, pagetable level of the entry, number of batched entries, etc.

It defines the following types:

    #define PT_TYPE_NONE
    #define PT_TYPE_FOLIO
    #define PT_TYPE_MARKER
    #define PT_TYPE_PFN
    #define PT_TYPE_SWAP
    #define PT_TYPE_MIGRATION
    #define PT_TYPE_DEVICE
    #define PT_TYPE_HWPOISON
    #define PT_TYPE_ALL

and it lets the caller be explicit about what types it is interested in. If it finds a type, but the caller stated it is not of importance, it keeps scanning the address range till the next type is found, or till we exhaust the range.

We have three functions:

    .pt_range_walk_start()
    .pt_range_walk_next()
    .pt_range_walk_done()

pt_range_walk_start() starts scanning the range and it returns the first type it finds, then we keep calling pt_range_walk_next() until we get PTW_DONE, which means we exhausted the range, and once that happens we have to call pt_range_walk_done() in order to cleanup the pt_range_walk internal state, like locking.

An example below:

     ´´´´
     pt_type_flags_t flags = PT_TYPE_ALL;

     type = pt_range_walk_start(&ptw, vma, start, vma->vm_end, flags);
     while (type != PTW_DONE) {
           do_something
           type = pt_range_walk_next(&ptw, vma, start, vma->vm_end, flags);
     }
     pt_range_walk_done(&ptw);
     ´´´´

The API manages locking within the interface, and also batching, which means that it can handle contiguous ptes (or pmds in the case of hugetlb) itself.

---
 arch/arm64/include/asm/pgtable.h |   1 +
 include/linux/mm.h               |   2 +
 include/linux/pagewalk.h         | 106 ++++++++
 mm/memory.c                      |  22 ++
 mm/pagewalk.c                    | 400 +++++++++++++++++++++++++++++++
 5 files changed, 531 insertions(+)
arch/arm64/include/asm/pgtable.h +1 -0
index 08ae4ee7d1da..7a3c109b9e99 100644
@@ -639,6 +639,7 @@ static inline pmd_t pmd_mkspecial(pmd_t pmd)
 #define pmd_pfn(pmd)		((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT)
 #define pfn_pmd(pfn,prot)	__pmd(__phys_to_pmd_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
 
+#define pud_dirty(pud)		pte_dirty(pud_pte(pud))
 #define pud_young(pud)		pte_young(pud_pte(pud))
 #define pud_mkyoung(pud)	pte_pud(pte_mkyoung(pud_pte(pud)))
 #define pud_mkwrite_novma(pud)	pte_pud(pte_mkwrite_novma(pud_pte(pud)))
include/linux/mm.h +2 -0
index af23453e9dbd..0f236363c981 100644
@@ -3073,6 +3073,8 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma,
 				  unsigned long addr, pmd_t pmd);
 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
 				pmd_t pmd);
+struct folio *vm_normal_folio_pud(struct vm_area_struct *vma,
+				  unsigned long addr, pud_t pud);
 struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,
 		pud_t pud);
 
include/linux/pagewalk.h +106 -0

110 lines not on this page · raw message

mm/memory.c +22 -0
index ea6568571131..64457a447224 100644
@@ -837,6 +837,28 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma,
 	return __vm_normal_page(vma, addr, pud_pfn(pud), pud_special(pud),
 				pud_val(pud), PGTABLE_LEVEL_PUD);
 }
+
+/**
+ * vm_normal_folio_pud() - Get the "struct folio" associated with a PUD
+ * @vma: The VMA mapping the @pud.
+ * @addr: The address where the @pud is mapped.
+ * @pud: The PUD.
+ *
+ * Get the "struct folio" associated with a PUD. See __vm_normal_page()
+ * for details on "normal" and "special" mappings.
+ *
+ * Return: Returns the "struct folio" if this is a "normal" mapping. Returns
+ *         NULL if this is a "special" mapping.
+ */
+struct folio *vm_normal_folio_pud(struct vm_area_struct *vma,
+				  unsigned long addr, pud_t pud)
+{
+	struct page *page = vm_normal_page_pud(vma, addr, pud);
+
+	if (page)
+		return page_folio(page);
+	return NULL;
+}
 #endif
 
 /**
mm/pagewalk.c +400 -0

403 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

RFC PATCH v3 5/8 mm: Make /proc/pid/smaps use the new generic pagewalk API

In reply to Oscar Salvador

Have /proc/pid/smaps make use of the new generic API, and remove the code which was using the old one.

---
 fs/proc/task_mmu.c | 306 +++++++++++++++------------------------------
 1 file changed, 100 insertions(+), 206 deletions(-)
fs/proc/task_mmu.c +100 -206
index 751b9ba160fb..ce958f208b5e 100644
@@ -915,7 +915,7 @@ static void smaps_page_accumulate(struct mem_size_stats *mss,
 
 static void smaps_account(struct mem_size_stats *mss, struct page *page,
 		bool compound, bool young, bool dirty, bool locked,
-		bool present)
+		bool present, int ssize)
 {
 	struct folio *folio = page_folio(page);
 	int i, nr = compound ? compound_nr(page) : 1;
@@ -923,6 +923,11 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page,
 	bool exclusive;
 	int mapcount;
 
+	if (ssize) {
+		nr = ssize / PAGE_SIZE;
+		size = ssize;
+	}
+
 	/*
 	 * First accumulate quantities that depend only on |size| and the type
 	 * of the compound page.
@@ -988,150 +993,6 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page,
 	}
 }
 
-#ifdef CONFIG_SHMEM
-static int smaps_pte_hole(unsigned long addr, unsigned long end,
-			  __always_unused int depth, struct mm_walk *walk)
-{
-	struct mem_size_stats *mss = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-
-	mss->swap += shmem_partial_swap_usage(walk->vma->vm_file->f_mapping,
-					      linear_page_index(vma, addr),
-					      linear_page_index(vma, end));
-
-	return 0;
-}
-#else
-#define smaps_pte_hole		NULL
-#endif /* CONFIG_SHMEM */
-
-static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
-{
-#ifdef CONFIG_SHMEM
-	if (walk->ops->pte_hole) {
-		/* depth is not used */
-		smaps_pte_hole(addr, addr + PAGE_SIZE, 0, walk);
-	}
-#endif
-}
-
-static void smaps_pte_entry(pte_t *pte, unsigned long addr,
-		struct mm_walk *walk)
-{
-	struct mem_size_stats *mss = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-	bool locked = !!(vma->vm_flags & VM_LOCKED);
-	struct page *page = NULL;
-	bool present = false, young = false, dirty = false;
-	pte_t ptent = ptep_get(pte);
-
-	if (pte_present(ptent)) {
-		page = vm_normal_page(vma, addr, ptent);
-		young = pte_young(ptent);
-		dirty = pte_dirty(ptent);
-		present = true;
-	} else if (pte_none(ptent)) {
-		smaps_pte_hole_lookup(addr, walk);
-	} else {
-		const softleaf_t entry = softleaf_from_pte(ptent);
-
-		if (softleaf_is_swap(entry)) {
-			int mapcount;
-
-			mss->swap += PAGE_SIZE;
-			mapcount = swp_swapcount(entry);
-			if (mapcount >= 2) {
-				u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
-
-				do_div(pss_delta, mapcount);
-				mss->swap_pss += pss_delta;
-			} else {
-				mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
-			}
-		} else if (softleaf_has_pfn(entry)) {
-			if (softleaf_is_device_private(entry))
-				present = true;
-			page = softleaf_to_page(entry);
-		}
-	}
-
-	if (!page)
-		return;
-
-	smaps_account(mss, page, false, young, dirty, locked, present);
-}
-
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
-		struct mm_walk *walk)
-{
-	struct mem_size_stats *mss = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-	bool locked = !!(vma->vm_flags & VM_LOCKED);
-	struct page *page = NULL;
-	bool present = false;
-	struct folio *folio;
-
-	if (pmd_none(*pmd))
-		return;
-	if (pmd_present(*pmd)) {
-		page = vm_normal_page_pmd(vma, addr, *pmd);
-		present = true;
-	} else if (unlikely(thp_migration_supported())) {
-		const softleaf_t entry = softleaf_from_pmd(*pmd);
-
-		if (softleaf_has_pfn(entry))
-			page = softleaf_to_page(entry);
-	}
-	if (IS_ERR_OR_NULL(page))
-		return;
-	folio = page_folio(page);
-	if (folio_test_anon(folio))
-		mss->anonymous_thp += HPAGE_PMD_SIZE;
-	else if (folio_test_swapbacked(folio))
-		mss->shmem_thp += HPAGE_PMD_SIZE;
-	else if (folio_is_zone_device(folio))
-		/* pass */;
-	else
-		mss->file_thp += HPAGE_PMD_SIZE;
-
-	smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
-		      locked, present);
-}
-#else
-static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
-		struct mm_walk *walk)
-{
-}
-#endif
-
-static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
-			   struct mm_walk *walk)
-{
-	struct vm_area_struct *vma = walk->vma;
-	pte_t *pte;
-	spinlock_t *ptl;
-
-	ptl = pmd_trans_huge_lock(pmd, vma);
-	if (ptl) {
-		smaps_pmd_entry(pmd, addr, walk);
-		spin_unlock(ptl);
-		goto out;
-	}
-
-	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
-	if (!pte) {
-		walk->action = ACTION_AGAIN;
-		return 0;
-	}
-	for (; addr != end; pte++, addr += PAGE_SIZE)
-		smaps_pte_entry(pte, addr, walk);
-	pte_unmap_unlock(pte - 1, ptl);
-out:
-	cond_resched();
-	return 0;
-}
-
 static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
 {
 	/*
@@ -1228,58 +1089,6 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
 	seq_putc(m, '\n');
 }
 
-#ifdef CONFIG_HUGETLB_PAGE
-static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
-				 unsigned long addr, unsigned long end,
-				 struct mm_walk *walk)
-{
-	struct mem_size_stats *mss = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-	struct folio *folio = NULL;
-	bool present = false;
-	spinlock_t *ptl;
-	pte_t ptent;
-
-	ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
-	ptent = huge_ptep_get(walk->mm, addr, pte);
-	if (pte_present(ptent)) {
-		folio = page_folio(pte_page(ptent));
-		present = true;
-	} else {
-		const softleaf_t entry = softleaf_from_pte(ptent);
-
-		if (softleaf_has_pfn(entry))
-			folio = softleaf_to_folio(entry);
-	}
-
-	if (folio) {
-		/* We treat non-present entries as "maybe shared". */
-		if (!present || folio_maybe_mapped_shared(folio) ||
-		    hugetlb_pmd_shared(pte))
-			mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
-		else
-			mss->private_hugetlb += huge_page_size(hstate_vma(vma));
-	}
-	spin_unlock(ptl);
-	return 0;
-}
-#else
-#define smaps_hugetlb_range	NULL
-#endif /* HUGETLB_PAGE */
-
-static const struct mm_walk_ops smaps_walk_ops = {
-	.pmd_entry		= smaps_pte_range,
-	.hugetlb_entry		= smaps_hugetlb_range,
-	.walk_lock		= PGWALK_RDLOCK,
-};
-
-static const struct mm_walk_ops smaps_shmem_walk_ops = {
-	.pmd_entry		= smaps_pte_range,
-	.hugetlb_entry		= smaps_hugetlb_range,
-	.pte_hole		= smaps_pte_hole,
-	.walk_lock		= PGWALK_RDLOCK,
-};
-
 /*
  * Gather mem stats from @vma with the indicated beginning
  * address @start, and keep them in @mss.
@@ -1287,14 +1096,20 @@ static const struct mm_walk_ops smaps_shmem_walk_ops = {
  * Use vm_start of @vma as the beginning address if @start is 0.
  */
 static void smap_gather_stats(struct vm_area_struct *vma,
-		struct mem_size_stats *mss, unsigned long start)
+				  struct mem_size_stats *mss,
+				  unsigned long start)
 {
-	const struct mm_walk_ops *ops = &smaps_walk_ops;
+	struct pt_range_walk ptw = {
+		.mm = vma->vm_mm
+	};
+	enum pt_range_walk_type type;
+	pt_type_flags_t flags = PT_TYPE_ALL;
 
-	/* Invalid start */
 	if (start >= vma->vm_end)
 		return;
 
+	flags &= ~(PT_TYPE_NONE|PT_TYPE_PFN);
+
 	if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
 		/*
 		 * For shared or readonly shmem mappings we know that all
@@ -1309,18 +1124,97 @@ static void smap_gather_stats(struct vm_area_struct *vma,
 		unsigned long shmem_swapped = shmem_swap_usage(vma);
 
 		if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
-					!(vma->vm_flags & VM_WRITE))) {
+		    !(vma->vm_flags & VM_WRITE))) {
 			mss->swap += shmem_swapped;
 		} else {
-			ops = &smaps_shmem_walk_ops;
+			flags |= PT_TYPE_NONE;
 		}
 	}
 
-	/* mmap_lock is held in m_start */
 	if (!start)
-		walk_page_vma(vma, ops, mss);
-	else
-		walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
+		start = vma->vm_start;
+
+	type = pt_range_walk_start(&ptw, vma, start, vma->vm_end, flags);
+	while (type != PTW_DONE) {
+		bool locked = !!(vma->vm_flags & VM_LOCKED);
+		bool compound = false, account = false;
+		unsigned long swap_size;
+		int mapcount;
+
+		switch (type) {
+		case PTW_FOLIO:
+		case PTW_MIGRATION:
+		case PTW_HWPOISON:
+		case PTW_DEVICE:
+			/*
+			 * We either have a folio because vm_normal_folio was
+			 * successful, or because we had a special swap entry
+			 * and could retrieve it with softleaf_to_page.
+			 */
+			if (is_vm_hugetlb_page(vma)) {
+				/* HugeTLB */
+				unsigned long size = huge_page_size(hstate_vma(ptw.vma));
+
+				if (!ptw.present || folio_maybe_mapped_shared(ptw.folio) ||
+				    ptw.pmd_shared)
+					mss->shared_hugetlb += size;
+				else
+					mss->private_hugetlb += size;
+			} else {
+				account = true;
+				if (ptw.level == PTW_PMD_LEVEL) {
+					/* THP */
+					compound = true;
+					if (folio_test_anon(ptw.folio))
+						mss->anonymous_thp += ptw.size;
+					else if (folio_test_swapbacked(ptw.folio))
+						mss->shmem_thp += ptw.size;
+					else if (folio_is_zone_device(ptw.folio))
+						/* pass */;
+					else
+						mss->file_thp += ptw.size;
+				} else if (ptw.level == PTW_PTE_LEVEL && ptw.nr_entries > 1) {
+					compound = true;
+				}
+			}
+			break;
+		case PTW_SWAP:
+			account = true;
+			swap_size = PAGE_SIZE * ptw.nr_entries;
+			mss->swap += swap_size;
+			mapcount = swp_swapcount(ptw.softleaf_entry);
+			if (mapcount >= 2) {
+				u64 pss_delta = (u64)swap_size << PSS_SHIFT;
+
+				do_div(pss_delta, mapcount);
+				mss->swap_pss += pss_delta;
+			} else {
+				mss->swap_pss += (u64)swap_size << PSS_SHIFT;
+			}
+			break;
+		case PTW_NONE:
+#ifdef CONFIG_SHMEM
+			unsigned long addr = ptw.curr_addr;
+			unsigned long end = ptw.next_addr;
+
+			if (ptw.level == PTW_PMD_LEVEL || ptw.level == PTW_PTE_LEVEL)
+				mss->swap += shmem_partial_swap_usage(vma->vm_file->f_mapping,
+							      linear_page_index(vma, addr),
+							      linear_page_index(vma, end));
+#endif
+			break;
+		default:
+			/* Ooops */
+			break;
+		}
+
+		if (account && ptw.folio)
+			smaps_account(mss, ptw.page, compound, ptw.young,
+				      ptw.dirty, locked, ptw.present, ptw.size);
+		type = pt_range_walk_next(&ptw, vma, start, vma->vm_end, flags);
+	}
+
+	pt_range_walk_done(&ptw);
 }
 
 #define SEQ_PUT_DEC(str, val) \
Signature · 1 line
2.53.0
#6 lore raw

RFC PATCH v3 6/8 mm: Make /proc/pid/numa_maps use the new generic pagewalk API

In reply to Oscar Salvador

Have /proc/pid/numa_maps make use of the new generic API, and remove the code which was using the old one

---
 fs/proc/task_mmu.c | 159 +++++++++------------------------------------
 1 file changed, 32 insertions(+), 127 deletions(-)
fs/proc/task_mmu.c +32 -127
index ce958f208b5e..b9ca47f0bc18 100644
@@ -3060,131 +3060,6 @@ static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
 	md->node[folio_nid(folio)] += nr_pages;
 }
 
-static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
-		unsigned long addr)
-{
-	struct page *page;
-	int nid;
-
-	if (!pte_present(pte))
-		return NULL;
-
-	page = vm_normal_page(vma, addr, pte);
-	if (!page || is_zone_device_page(page))
-		return NULL;
-
-	if (PageReserved(page))
-		return NULL;
-
-	nid = page_to_nid(page);
-	if (!node_isset(nid, node_states[N_MEMORY]))
-		return NULL;
-
-	return page;
-}
-
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
-					      struct vm_area_struct *vma,
-					      unsigned long addr)
-{
-	struct page *page;
-	int nid;
-
-	if (!pmd_present(pmd))
-		return NULL;
-
-	page = vm_normal_page_pmd(vma, addr, pmd);
-	if (!page)
-		return NULL;
-
-	if (PageReserved(page))
-		return NULL;
-
-	nid = page_to_nid(page);
-	if (!node_isset(nid, node_states[N_MEMORY]))
-		return NULL;
-
-	return page;
-}
-#endif
-
-static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
-		unsigned long end, struct mm_walk *walk)
-{
-	struct numa_maps *md = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-	spinlock_t *ptl;
-	pte_t *orig_pte;
-	pte_t *pte;
-
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	ptl = pmd_trans_huge_lock(pmd, vma);
-	if (ptl) {
-		struct page *page;
-
-		page = can_gather_numa_stats_pmd(*pmd, vma, addr);
-		if (page)
-			gather_stats(page, md, pmd_dirty(*pmd),
-				     HPAGE_PMD_SIZE/PAGE_SIZE);
-		spin_unlock(ptl);
-		return 0;
-	}
-#endif
-	orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
-	if (!pte) {
-		walk->action = ACTION_AGAIN;
-		return 0;
-	}
-	do {
-		pte_t ptent = ptep_get(pte);
-		struct page *page = can_gather_numa_stats(ptent, vma, addr);
-		if (!page)
-			continue;
-		gather_stats(page, md, pte_dirty(ptent), 1);
-
-	} while (pte++, addr += PAGE_SIZE, addr != end);
-	pte_unmap_unlock(orig_pte, ptl);
-	cond_resched();
-	return 0;
-}
-#ifdef CONFIG_HUGETLB_PAGE
-static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
-		unsigned long addr, unsigned long end, struct mm_walk *walk)
-{
-	pte_t huge_pte;
-	struct numa_maps *md;
-	struct page *page;
-	spinlock_t *ptl;
-
-	ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
-	huge_pte = huge_ptep_get(walk->mm, addr, pte);
-	if (!pte_present(huge_pte))
-		goto out;
-
-	page = pte_page(huge_pte);
-
-	md = walk->private;
-	gather_stats(page, md, pte_dirty(huge_pte), 1);
-out:
-	spin_unlock(ptl);
-	return 0;
-}
-
-#else
-static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
-		unsigned long addr, unsigned long end, struct mm_walk *walk)
-{
-	return 0;
-}
-#endif
-
-static const struct mm_walk_ops show_numa_ops = {
-	.hugetlb_entry = gather_hugetlb_stats,
-	.pmd_entry = gather_pte_stats,
-	.walk_lock = PGWALK_RDLOCK,
-};
-
 /*
  * Display pages allocated per node and memory policy via /proc.
  */
@@ -3196,9 +3071,15 @@ static int show_numa_map(struct seq_file *m, void *v)
 	struct numa_maps *md = &numa_priv->md;
 	struct file *file = vma->vm_file;
 	struct mm_struct *mm = vma->vm_mm;
+	struct pt_range_walk ptw = {
+		.mm = mm
+	};
+	enum pt_range_walk_type type;
+	pt_type_flags_t flags;
 	char buffer[64];
 	struct mempolicy *pol;
 	pgoff_t ilx;
+	int nr_pages;
 	int nid;
 
 	if (!mm)
@@ -3229,8 +3110,32 @@ static int show_numa_map(struct seq_file *m, void *v)
 	if (is_vm_hugetlb_page(vma))
 		seq_puts(m, " huge");
 
-	/* mmap_lock is held by m_start */
-	walk_page_vma(vma, &show_numa_ops, md);
+	flags = PT_TYPE_FOLIO;
+	type = pt_range_walk_start(&ptw, vma, vma->vm_start, vma->vm_end, flags);
+	while (type != PTW_DONE) {
+
+		if (!ptw.folio || !ptw.page || PageReserved(ptw.page))
+			goto not_found;
+
+		nid = page_to_nid(ptw.page);
+		if (!node_isset(nid, node_states[N_MEMORY]))
+			goto not_found;
+
+		if (is_vm_hugetlb_page(vma))
+			/*
+			 * As opposed to THP, HugeTLB counts the entire huge
+			 * page as one unit size.
+			 */
+			nr_pages = 1;
+		else
+			nr_pages = ptw.size / PAGE_SIZE;
+
+		gather_stats(ptw.page, md, ptw.dirty, nr_pages);
+not_found:
+		type = pt_range_walk_next(&ptw, vma, vma->vm_start, vma->vm_end, flags);
+
+	}
+	pt_range_walk_done(&ptw);
 
 	if (!md->pages)
 		goto out;
Signature · 1 line
2.53.0
#7 lore raw

RFC PATCH v3 7/8 mm: Make /proc/pid/pagemap use the new generic pagewalk API

In reply to Oscar Salvador

Have /proc/pid/pagemap make use of the new generic API, and remove the code which was using the old one.

---
 arch/arm64/include/asm/pgtable.h |    9 +
 arch/x86/include/asm/pgtable.h   |    5 +
 arch/x86/mm/pgtable.c            |   18 +-
 fs/proc/task_mmu.c               | 1749 +++++++++++++++---------------
 include/linux/leafops.h          |   13 +
 include/linux/pgtable.h          |   30 +
 mm/pgtable-generic.c             |   21 +
 7 files changed, 986 insertions(+), 859 deletions(-)
arch/arm64/include/asm/pgtable.h +9 -0
index 7a3c109b9e99..71719929e2e8 100644
@@ -639,6 +639,7 @@ static inline pmd_t pmd_mkspecial(pmd_t pmd)
 #define pmd_pfn(pmd)		((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT)
 #define pfn_pmd(pfn,prot)	__pmd(__phys_to_pmd_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
 
+#define pud_mkinvalid(pud)	pte_pud(pte_mkinvalid(pud_pte(pud)))
 #define pud_dirty(pud)		pte_dirty(pud_pte(pud))
 #define pud_young(pud)		pte_young(pud_pte(pud))
 #define pud_mkyoung(pud)	pte_pud(pte_mkyoung(pud_pte(pud)))
@@ -1527,6 +1528,14 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
 }
 #endif
 
+#define pudp_establish pudp_establish
+static inline pud_t pudp_establish(struct vm_area_struct *vma,
+				   unsigned long address, pud_t *pudp, pud_t pud)
+{
+	page_table_check_pud_set(vma->vm_mm, address, pudp, pud);
+	return __pud(xchg_relaxed(&pud_val(*pudp), pud_val(pud)));
+}
+
 /*
  * Encode and decode a swap entry:
  *	bits 0-1:	present (must be zero)
arch/x86/include/asm/pgtable.h +5 -0
index a3cf289948a0..7903485df8c3 100644
@@ -1392,10 +1392,15 @@ static inline pud_t pudp_establish(struct vm_area_struct *vma,
 }
 #endif
 
+#define __HAVE_ARCH_PUDP_INVALIDATE_AD
+extern pud_t pudp_invalidate_ad(struct vm_area_struct *vma,
+				unsigned long address, pud_t *pudp);
+
 #define __HAVE_ARCH_PMDP_INVALIDATE_AD
 extern pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma,
 				unsigned long address, pmd_t *pmdp);
 
+#define __HAVE_ARCH_PUDP_INVALIDATE
 pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
 		      pud_t *pudp);
 
arch/x86/mm/pgtable.c +16 -2
index da7f0a03cf90..85b912397afa 100644
@@ -530,8 +530,22 @@ pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
 }
 #endif
 
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
-	defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
+#if (defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
+	defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)) || \
+	defined CONFIG_HUGETLB_PAGE
+
+pud_t pudp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
+			 pud_t *pudp)
+{
+	VM_WARN_ON_ONCE(!pud_present(*pudp));
+
+	/*
+	 * No flush is necessary. Once an invalid PUD is established, the PUD's
+	 * access and dirty bits cannot be updated.
+	 */
+	return pudp_establish(vma, address, pudp, pud_mkinvalid(*pudp));
+}
+
 pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
 		     pud_t *pudp)
 {
fs/proc/task_mmu.c +892 -857

1,940 lines not on this page · raw message

include/linux/leafops.h +13 -0
index 08646398b0fe..bda9e7971732 100644
@@ -628,6 +628,19 @@ static inline bool pmd_is_device_private_entry(pmd_t pmd)
 
 #endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */
 
+#ifdef CONFIG_HUGETLB_PAGE
+/**
+ * pud_is_migration_entry() - Does this PUD entry encode a migration entry?
+ * @pud: PUD entry.
+ *
+ * Returns: true if the PUD encodes a migration entry, otherwise false.
+ */
+static inline bool pud_is_migration_entry(pud_t pud)
+{
+	return softleaf_is_migration(softleaf_from_pud(pud));
+}
+#endif
+
 /**
  * pmd_is_migration_entry() - Does this PMD entry encode a migration entry?
  * @pmd: PMD entry.
include/linux/pgtable.h +30 -0
index f5291f9ce583..1512b1bb49e3 100644
@@ -1263,11 +1263,21 @@ static inline pmd_t generic_pmdp_establish(struct vm_area_struct *vma,
 }
 #endif
 
+#ifndef __HAVE_ARCH_PUDP_INVALIDATE
+extern pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
+			    pud_t *pudp);
+#endif
+
 #ifndef __HAVE_ARCH_PMDP_INVALIDATE
 extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
 			    pmd_t *pmdp);
 #endif
 
+#ifndef __HAVE_ARCH_PUDP_INVALIDATE_AD
+extern pud_t pudp_invalidate_ad(struct vm_area_struct *vma,
+				unsigned long address, pud_t *pudp);
+#endif
+
 #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD
 
 /*
@@ -1810,6 +1820,21 @@ static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
 
 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
 #ifndef CONFIG_ARCH_ENABLE_THP_MIGRATION
+static inline pud_t pud_swp_mksoft_dirty(pud_t pud)
+{
+	return pud;
+}
+
+static inline int pud_swp_soft_dirty(pud_t pud)
+{
+	return 0;
+}
+
+static inline pud_t pud_swp_clear_soft_dirty(pud_t pud)
+{
+	return pud;
+}
+
 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
 {
 	return pmd;
@@ -1852,6 +1877,11 @@ static inline int pmd_soft_dirty(pmd_t pmd)
 	return 0;
 }
 
+static inline int pud_soft_dirty(pud_t pud)
+{
+	return 0;
+}
+
 static inline pte_t pte_mksoft_dirty(pte_t pte)
 {
 	return pte;
mm/pgtable-generic.c +21 -0
index b91b1a98029c..89010192c969 100644
@@ -208,6 +208,27 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
 }
 #endif
 
+#ifndef __HAVE_ARCH_PUDP_INVALIDATE
+pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
+		      pud_t *pudp)
+{
+	VM_WARN_ON_ONCE(!pud_present(*pudp));
+	pud_t old = pudp_establish(vma, address, pudp, pud_mkinvalid(*pudp));
+	flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE);
+	return old;
+}
+#endif
+
+#ifndef __HAVE_ARCH_PUDP_INVALIDATE_AD
+pud_t pudp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
+			 pud_t *pudp)
+
+{
+	VM_WARN_ON_ONCE(!pud_present(*pudp));
+	return pudp_invalidate(vma, address, pudp);
+}
+#endif
+
 #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD
 pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
 			 pmd_t *pmdp)
Signature · 1 line
2.53.0
#8 lore raw

RFC PATCH v3 8/8 mm: Make /proc/pid/clear_refs use the new generic pagewalk API

In reply to Oscar Salvador

Have /proc/pid/clear_refs make use of the new generic API, and remove the code which was using the old one.

---
 fs/proc/task_mmu.c | 133 ++++++++++++++++++++++++---------------------
 1 file changed, 70 insertions(+), 63 deletions(-)
fs/proc/task_mmu.c +70 -63
index 20ffb26692cc..5f09a5b26b61 100644
@@ -1571,72 +1571,54 @@ static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
 }
 #endif
 
-static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
-				unsigned long end, struct mm_walk *walk)
+static void clear_refs_pmd_range(struct pt_range_walk *ptw,
+				 enum clear_refs_types type)
 {
-	struct clear_refs_private *cp = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-	pte_t *pte, ptent;
-	spinlock_t *ptl;
-	struct folio *folio;
+	struct vm_area_struct *vma = ptw->vma;
+	unsigned long addr = ptw->curr_addr;
 
-	ptl = pmd_trans_huge_lock(pmd, vma);
-	if (ptl) {
-		if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
-			clear_soft_dirty_pmd(vma, addr, pmd);
-			goto out;
-		}
-
-		if (!pmd_present(*pmd))
-			goto out;
+	if (type == CLEAR_REFS_SOFT_DIRTY) {
+		clear_soft_dirty_pmd(vma, addr, ptw->pmdp);
+		return;
+	}
 
-		folio = pmd_folio(*pmd);
+	if (!pmd_present(ptw->pmd) || !ptw->folio)
+		return;
 
-		/* Clear accessed and referenced bits. */
-		pmdp_test_and_clear_young(vma, addr, pmd);
-		folio_test_clear_young(folio);
-		folio_clear_referenced(folio);
-out:
-		spin_unlock(ptl);
-		return 0;
-	}
+	pmdp_test_and_clear_young(vma, addr, ptw->pmdp);
+	folio_test_clear_young(ptw->folio);
+	folio_clear_referenced(ptw->folio);
+}
 
-	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
-	if (!pte) {
-		walk->action = ACTION_AGAIN;
-		return 0;
-	}
-	for (; addr != end; pte++, addr += PAGE_SIZE) {
-		ptent = ptep_get(pte);
+static void clear_refs_pte_range(struct pt_range_walk *ptw,
+				enum clear_refs_types type)
+{
+	struct vm_area_struct *vma = ptw->vma;
+	unsigned long addr = ptw->curr_addr;
+	unsigned long end = pmd_addr_end(addr, vma->vm_end);
+	pte_t *ptep = ptw->ptep;
 
-		if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
-			clear_soft_dirty(vma, addr, pte);
+	for (; addr != end; ptep++, addr += PAGE_SIZE) {
+		if (type == CLEAR_REFS_SOFT_DIRTY) {
+			clear_soft_dirty(vma, addr, ptep);
 			continue;
 		}
 
-		if (!pte_present(ptent))
-			continue;
-
-		folio = vm_normal_folio(vma, addr, ptent);
-		if (!folio)
+		if (!ptw->present || !ptw->folio)
 			continue;
 
 		/* Clear accessed and referenced bits. */
-		ptep_test_and_clear_young(vma, addr, pte);
-		folio_test_clear_young(folio);
-		folio_clear_referenced(folio);
+		ptep_test_and_clear_young(vma, addr, ptep);
+		folio_test_clear_young(ptw->folio);
+		folio_clear_referenced(ptw->folio);
 	}
-	pte_unmap_unlock(pte - 1, ptl);
-	cond_resched();
-	return 0;
+
+	ptw->next_addr = end;
 }
 
-static int clear_refs_test_walk(unsigned long start, unsigned long end,
-				struct mm_walk *walk)
+static int clear_refs_test_vma(struct vm_area_struct *vma,
+			       enum clear_refs_types type)
 {
-	struct clear_refs_private *cp = walk->private;
-	struct vm_area_struct *vma = walk->vma;
-
 	if (vma->vm_flags & VM_PFNMAP)
 		return 1;
 
@@ -1646,19 +1628,13 @@ static int clear_refs_test_walk(unsigned long start, unsigned long end,
 	 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
 	 * Writing 4 to /proc/pid/clear_refs affects all pages.
 	 */
-	if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
+	if (type == CLEAR_REFS_ANON && vma->vm_file)
 		return 1;
-	if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
+	if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
 		return 1;
 	return 0;
 }
 
-static const struct mm_walk_ops clear_refs_walk_ops = {
-	.pmd_entry		= clear_refs_pte_range,
-	.test_walk		= clear_refs_test_walk,
-	.walk_lock		= PGWALK_WRLOCK,
-};
-
 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 				size_t count, loff_t *ppos)
 {
@@ -1688,9 +1664,6 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 	if (mm) {
 		VMA_ITERATOR(vmi, mm, 0);
 		struct mmu_notifier_range range;
-		struct clear_refs_private cp = {
-			.type = type,
-		};
 
 		if (mmap_write_lock_killable(mm)) {
 			count = -EINTR;
@@ -1712,13 +1685,47 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 				vm_flags_clear(vma, VM_SOFTDIRTY);
 				vma_set_page_prot(vma);
 			}
-
+			vma_iter_init(&vmi, mm, 0);
 			inc_tlb_flush_pending(mm);
 			mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
 						0, mm, 0, -1UL);
 			mmu_notifier_invalidate_range_start(&range);
 		}
-		walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+
+		for_each_vma(vmi, vma) {
+			struct pt_range_walk ptw = {
+				.mm = mm,
+			};
+			enum pt_range_walk_type pt_type;
+			pt_type_flags_t flags = PT_TYPE_ALL;
+			int ret;
+
+			/* We ignore hugetlb vmas */
+			if (is_vm_hugetlb_page(vma))
+				continue;
+
+			ret = clear_refs_test_vma(vma, type);
+			if (ret > 0)
+				continue;
+			else if (ret < 0)
+				break;
+
+			pt_type = pt_range_walk_start(&ptw, vma, vma->vm_start, vma->vm_end, flags);
+			while (pt_type != PTW_DONE) {
+
+				/*
+				 * Since we ignore hugetlb vmas, we just care for PMD
+				 * or PTE mapped pages.
+				 */
+				if (ptw.level == PTW_PMD_LEVEL)
+					clear_refs_pmd_range(&ptw, type);
+				else if (ptw.level == PTW_PTE_LEVEL)
+					clear_refs_pte_range(&ptw, type);
+
+				pt_type = pt_range_walk_next(&ptw, vma, vma->vm_start, vma->vm_end, flags);
+			}
+			pt_range_walk_done(&ptw);
+		}
 		if (type == CLEAR_REFS_SOFT_DIRTY) {
 			mmu_notifier_invalidate_range_end(&range);
 			flush_tlb_mm(mm);
Signature · 1 line
2.53.0
syzbot Tested-by [email protected] on the v3 series
#9 lore raw
In reply to Oscar Salvador
syzbot ci has tested the following series

syzbot ci has tested the following series

[v3] Implement a new generic pagewalk API
https://lore.kernel.org/all/[email protected]
* [RFC PATCH v3 1/8] mm: Add softleaf_from_pud
* [RFC PATCH v3 2/8] mm: Add {pmd,pud}_huge_lock helper
* [RFC PATCH v3 3/8] mm: Implement folio_pmd_batch
* [RFC PATCH v3 4/8] mm: Implement pt_range_walk
* [RFC PATCH v3 5/8] mm: Make /proc/pid/smaps use the new generic pagewalk API
* [RFC PATCH v3 6/8] mm: Make /proc/pid/numa_maps use the new generic pagewalk API
* [RFC PATCH v3 7/8] mm: Make /proc/pid/pagemap use the new generic pagewalk API
* [RFC PATCH v3 8/8] mm: Make /proc/pid/clear_refs use the new generic pagewalk API

and found the following issue:

general protection fault in clear_refs_write

Full report is available here:
https://ci.syzbot.org/series/0beab2d0-9d88-4288-ac81-b294e51c057d

***

general protection fault in clear_refs_write
tree:      mm-new
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git
base:      5200f5f493f79f14bbdc349e402a40dfb32f23c8
arch:      amd64
compiler:  Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8
config:    https://ci.syzbot.org/builds/8f20e88b-808c-4c60-9b8e-0af4c9fce5ff/config
syz repro: https://ci.syzbot.org/findings/634b1f0a-cf64-4607-928a-ef3ad40f765c/syz_repro
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN PTI
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
CPU: 1 UID: 0 PID: 5863 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
RIP: 0010:clear_soft_dirty_pmd fs/proc/task_mmu.c:1545 [inline]
RIP: 0010:clear_refs_pmd_range fs/proc/task_mmu.c:1581 [inline]
RIP: 0010:clear_refs_write+0xaa7/0x1590 fs/proc/task_mmu.c:1721
Code: 44 24 10 4c 8b bc 24 20 02 00 00 83 7c 24 28 04 0f 85 aa 05 00 00 48 8b 9c 24 08 02 00 00 48 89 d8 48 c1 e8 03 48 89 44 24 58 <42> 80 3c 20 00 74 08 48 89 df e8 fa 2c c6 ff 4c 8b 23 4c 89 e6 48
RSP: 0018:ffffc900037bf960 EFLAGS: 00010246
RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000005
RDX: ffffffff826b21ce RSI: ffffffff8eb17b50 RDI: 0000000000000001
RBP: ffffc900037bfc10 R08: ffff8881047c0000 R09: 0000000000000002
R10: 0000000000000002 R11: 0000000000000000 R12: dffffc0000000000
R13: fffff520006f7f40 R14: ffff8881141c8308 R15: ffff8881141c8300
FS:  00007ff8ffc1d6c0(0000) GS:ffff8882a928a000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000001b33e63fff CR3: 000000010476e000 CR4: 00000000000006f0
Call Trace:
 <TASK>
 do_loop_readv_writev fs/read_write.c:852 [inline]
 vfs_writev+0x4bd/0x990 fs/read_write.c:1061
 do_writev+0x154/0x2e0 fs/read_write.c:1105
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x15f/0xf80 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7ff8fed9ce59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007ff8ffc1d028 EFLAGS: 00000246 ORIG_RAX: 0000000000000014
RAX: ffffffffffffffda RBX: 00007ff8ff015fa0 RCX: 00007ff8fed9ce59
RDX: 0000000000000001 RSI: 00002000000000c0 RDI: 0000000000000003
RBP: 00007ff8fee32d6f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007ff8ff016038 R14: 00007ff8ff015fa0 R15: 00007fffd02796e8
 </TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
RIP: 0010:clear_soft_dirty_pmd fs/proc/task_mmu.c:1545 [inline]
RIP: 0010:clear_refs_pmd_range fs/proc/task_mmu.c:1581 [inline]
RIP: 0010:clear_refs_write+0xaa7/0x1590 fs/proc/task_mmu.c:1721
Code: 44 24 10 4c 8b bc 24 20 02 00 00 83 7c 24 28 04 0f 85 aa 05 00 00 48 8b 9c 24 08 02 00 00 48 89 d8 48 c1 e8 03 48 89 44 24 58 <42> 80 3c 20 00 74 08 48 89 df e8 fa 2c c6 ff 4c 8b 23 4c 89 e6 48
RSP: 0018:ffffc900037bf960 EFLAGS: 00010246
RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000005
RDX: ffffffff826b21ce RSI: ffffffff8eb17b50 RDI: 0000000000000001
RBP: ffffc900037bfc10 R08: ffff8881047c0000 R09: 0000000000000002
R10: 0000000000000002 R11: 0000000000000000 R12: dffffc0000000000
R13: fffff520006f7f40 R14: ffff8881141c8308 R15: ffff8881141c8300
FS:  00007ff8ffc1d6c0(0000) GS:ffff8882a928a000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000001b34063fff CR3: 000000010476e000 CR4: 00000000000006f0
----------------
Code disassembly (best guess):
   0:	44 24 10             	rex.R and $0x10,%al
   3:	4c 8b bc 24 20 02 00 	mov    0x220(%rsp),%r15
   a:	00
   b:	83 7c 24 28 04       	cmpl   $0x4,0x28(%rsp)
  10:	0f 85 aa 05 00 00    	jne    0x5c0
  16:	48 8b 9c 24 08 02 00 	mov    0x208(%rsp),%rbx
  1d:	00
  1e:	48 89 d8             	mov    %rbx,%rax
  21:	48 c1 e8 03          	shr    $0x3,%rax
  25:	48 89 44 24 58       	mov    %rax,0x58(%rsp)
* 2a:	42 80 3c 20 00       	cmpb   $0x0,(%rax,%r12,1) <-- trapping instruction
  2f:	74 08                	je     0x39
  31:	48 89 df             	mov    %rbx,%rdi
  34:	e8 fa 2c c6 ff       	call   0xffc62d33
  39:	4c 8b 23             	mov    (%rbx),%r12
  3c:	4c 89 e6             	mov    %r12,%rsi
  3f:	48                   	rex.W

***

If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].

To test a patch for this bug, please reply with #syz test (should be on a separate line).

The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.

#10 lore raw
Quoted 13 lines · syzbot ci

syzbot ci has tested the following series

[v3] Implement a new generic pagewalk API
https://lore.kernel.org/all/[email protected]
* [RFC PATCH v3 1/8] mm: Add softleaf_from_pud
* [RFC PATCH v3 2/8] mm: Add {pmd,pud}_huge_lock helper
* [RFC PATCH v3 3/8] mm: Implement folio_pmd_batch
* [RFC PATCH v3 4/8] mm: Implement pt_range_walk
* [RFC PATCH v3 5/8] mm: Make /proc/pid/smaps use the new generic pagewalk API
* [RFC PATCH v3 6/8] mm: Make /proc/pid/numa_maps use the new generic pagewalk API
* [RFC PATCH v3 7/8] mm: Make /proc/pid/pagemap use the new generic pagewalk API
* [RFC PATCH v3 8/8] mm: Make /proc/pid/clear_refs use the new generic pagewalk API

Quoted 5 lines

and found the following issue:

general protection fault in clear_refs_write

Full report is available here:
https://ci.syzbot.org/series/0beab2d0-9d88-4288-ac81-b294e51c057d

#syz test

Signature · 2 lines
Oscar Salvador
SUSE Labs

From 252fd5679cc74ebe82d01d5bf1523184f7e323be Mon Sep 17 00:00:00 2001
From: Oscar Salvador <[email protected]>
Date: Thu, 11 Jun 2026 13:58:44 +0200
Subject: [PATCH] fix

---
 fs/proc/task_mmu.c | 1 +
 1 file changed, 1 insertion(+)
fs/proc/task_mmu.c +1 -0
index 1134dfb3dab0..87515949b09d 100644
@@ -1710,6 +1710,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 			else if (ret < 0)
 				break;
 
+			flags &= ~PT_TYPE_NONE
 			pt_type = pt_range_walk_start(&ptw, vma, vma->vm_start, vma->vm_end, flags);
 			while (pt_type != PTW_DONE) {
 
Signature · 1 line
2.35.3
syzbot
#11 lore raw
syzbot ci has tested the suggested fix patch on top of the following series:

syzbot ci has tested the suggested fix patch on top of the following series:

[v3] Implement a new generic pagewalk API
https://lore.kernel.org/all/[email protected]

Patch: https://ci.syzbot.org/jobs/ef5ed4ec-3733-48d9-af67-b53dff1c8b20/patch
Testing results:
* [build 0] Build Patched: failed

The following issues were found:
* kernel build error

Full report is available here:
https://ci.syzbot.org/session/90e7add4-2247-4f1b-9ca6-89dc339fdf98

***

kernel build error

tree:      mm-new
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git
base:      5200f5f493f79f14bbdc349e402a40dfb32f23c8
arch:      amd64
compiler:  Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8
config:    https://ci.syzbot.org/builds/f29ebb48-eb43-476c-8d44-aa077ea8eca3/config

fs/proc/task_mmu.c:1713:26: error: expected ';' after expression

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].

#12 lore raw
In reply to syzbot ci
Quoted 13 lines · syzbot ci

syzbot ci has tested the following series

[v3] Implement a new generic pagewalk API
https://lore.kernel.org/all/[email protected]
* [RFC PATCH v3 1/8] mm: Add softleaf_from_pud
* [RFC PATCH v3 2/8] mm: Add {pmd,pud}_huge_lock helper
* [RFC PATCH v3 3/8] mm: Implement folio_pmd_batch
* [RFC PATCH v3 4/8] mm: Implement pt_range_walk
* [RFC PATCH v3 5/8] mm: Make /proc/pid/smaps use the new generic pagewalk API
* [RFC PATCH v3 6/8] mm: Make /proc/pid/numa_maps use the new generic pagewalk API
* [RFC PATCH v3 7/8] mm: Make /proc/pid/pagemap use the new generic pagewalk API
* [RFC PATCH v3 8/8] mm: Make /proc/pid/clear_refs use the new generic pagewalk API

Quoted 5 lines

and found the following issue:

general protection fault in clear_refs_write

Full report is available here:
https://ci.syzbot.org/series/0beab2d0-9d88-4288-ac81-b294e51c057d

#syz test

Signature · 2 lines
Oscar Salvador
SUSE Labs

From 59776435d1beca2bd9446050450afe04453a99e8 Mon Sep 17 00:00:00 2001
From: Oscar Salvador <[email protected]>
Date: Thu, 11 Jun 2026 13:58:44 +0200
Subject: [PATCH] fix

---
 fs/proc/task_mmu.c | 1 +
 1 file changed, 1 insertion(+)
fs/proc/task_mmu.c +1 -0
index 1134dfb3dab0..fadc6356f84e 100644
@@ -1710,6 +1710,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 			else if (ret < 0)
 				break;
 
+			flags &= ~PT_TYPE_NONE;
 			pt_type = pt_range_walk_start(&ptw, vma, vma->vm_start, vma->vm_end, flags);
 			while (pt_type != PTW_DONE) {
 
Signature · 1 line
2.35.3
syzbot
#13 lore raw
syzbot ci has tested the suggested fix patch on top of the following series:

syzbot ci has tested the suggested fix patch on top of the following series:

[v3] Implement a new generic pagewalk API
https://lore.kernel.org/all/[email protected]

Patch: https://ci.syzbot.org/jobs/73cb101f-23d1-4b01-a2fa-2bb80c75788a/patch
Testing results:
* [build 0] Build Patched: passed
* [build 0] Boot test: Patched: passed

Full report is available here:
https://ci.syzbot.org/session/98c0ca07-99bd-49e1-8b0d-a47659fee85b

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].

maintainer
#14 lore raw
In reply to Oscar Salvador

-cc wrong email

Sorry to be a pain but I only just noticed this because it's going to the wrong email address :)

Could you make sure to send future revisions to [email protected]?

Thanks, Lorenzo

Quoted 211 lines · Oscar Salvador

Changelog:
rfcv2 -> rfcv3:
- Fix an out-of-bounds write
- Convert clear_refs to the new API
- Fix issue when reading cont-PMDs
rfc -> rfcv2:
- Add pte_hole functionality
- Fix pagemap issues
- Fix shmem in smap
- Testing with pagemap "testsuite"

[WARNING]

This is not yet fully complete, but before investing more time into it I would like to know whether 1) this is heading into the right direction and 2) this is something we are still interested in.
There are still things that need work:

- convert make_uffd_wp_huge_pte: Since hugetlb is being dealt like a pte, we inherited PTE_MARKERs for it when those came into play, and AFAIK, those are being used mostly for UFFD.
From here on we have two options: 1) find another way to deal with UFFD without markers or 2) introduce markers for PMD and PUD level. I am leaning towards option 1), because 2) seems a bit unfair. I still need to put some thought into it and see how we can achieve that.

- Teach the new API how to use other kind of locks. E.g: pagemap scan needs to take i_mmap_lock during the scanning, so we need to able to take that lock. I have some ideas to do that, but something for the new version.

- Find corner-cases and fix them.

Kudos go to David, who was the person suggesting the interface and he gave me some ideas where to begin, besides providing feedback on early stages (in case there is something stupid don't blame him, blame me)

Also, I would like to thank Vlastimil, who helped me running this patchset quite a few times through Claude, to catch some fixes.

[/WARNING]

[TESTING]
Part of the testing has been to duplicate /proc/$$/(pagemap,smaps,numa_maps,clear_refs) and have the same with _lab extension linked to the old API.
In that way I could check whether the outcome from e.g: /proc/$$/smaps and /proc/$$/smaps_lab was the same for any given program. The same I did for pagemap and numa_maps.

Also, regarding pagemap:
So far, tools/mm/page-types.c reports the right outcome (compared to the old API), and tools/testing/selftests/mm/pagemap_ioctl.c only reports 4 failing tests. Although to be honest, I do not how much should I trust that one because if I add a few delays in the userspace code, some tests that were failing before are not now, so yeah.

localhost:~/workspace # ./page-types -p 1168

              flags     page-count       MB  symbolic-flags                     long-symbolic-flags
 0x0000000000000800              1        0  ___________M_______________________________        mmap
 0x0000000000000828              2        0  ___U_l_____M_______________________________        uptodate,lru,mmap
 0x000000000000082c              1        0  __RU_l_____M_______________________________        referenced,uptodate,lru,mmap
 0x0000000000004838              1        0  ___UDl_____M__b____________________________        uptodate,dirty,lru,mmap,swapbacked
 0x000000000000086c            423        1  __RU_lA____M_______________________________        referenced,uptodate,lru,active,mmap
 0x0000000000205828             29        0  ___U_l_____Ma_b______x_____________________        uptodate,lru,mmap,anonymous,swapbacked,ksm
 0x000000000020586c              1        0  __RU_lA____Ma_b______x_____________________        referenced,uptodate,lru,active,mmap,anonymous,swapbacked,ksm

total 458 1

localhost:~/workspace # ./page-types_lab -p 1168

              flags     page-count       MB  symbolic-flags                     long-symbolic-flags
 0x0000000000000804              1        0  __R________M_______________________________        referenced,mmap
 0x0000000000000828              2        0  ___U_l_____M_______________________________        uptodate,lru,mmap
 0x000000000000082c              1        0  __RU_l_____M_______________________________        referenced,uptodate,lru,mmap
 0x0000000000004838              1        0  ___UDl_____M__b____________________________        uptodate,dirty,lru,mmap,swapbacked
 0x000000000000086c            423        1  __RU_lA____M_______________________________        referenced,uptodate,lru,active,mmap
 0x0000000000205828             29        0  ___U_l_____Ma_b______x_____________________        uptodate,lru,mmap,anonymous,swapbacked,ksm
 0x000000000020586c              1        0  __RU_lA____Ma_b______x_____________________        referenced,uptodate,lru,active,mmap,anonymous,swapbacked,ksm

total 458 1

page-types being using the new API and page-types_lab the old one.

 # ./pagemap_ioctl
 TAP version 13
 1..117
 ok 1 sanity_tests_sd Zero range size is valid
 ok 2 sanity_tests_sd output bu
 ok 35 Walk_end: 1 max page
 ok 36 Page testing: all new pages must not be written (dirty)
 ok 37 Page testing: all pages must be written (dirty)
 ok 38 Page testing: all pages dirty other than first and the last one
 ok 39 Page testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 40 Page testing: only middle page dirty
 ok 41 Page testing: only two middle pages dirty
 ok 42 Large Page testing: all new pages must not be written (dirty)
 ok 43 Large Page testing: all pages must be written (dirty)
 ok 44 Large Page testing: all pages dirty other than first and the last one
 ok 45 Large Page testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 46 Large Page testing: only middle page dirty
 ok 47 Large Page testing: only two middle pages dirty
 ok 48 Huge page testing: all new pages must not be written (dirty)
 ok 49 Huge page testing: all pages must be written (dirty)
 ok 50 Huge page testing: all pages dirty other than first and the last one
 ok 51 Huge page testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 52 Huge page testing: only middle page dirty
 ok 53 Huge page testing: only two middle pages dirty
 ok 54 Hugetlb shmem testing: all new pages must not be written (dirty)
 ok 55 Hugetlb shmem testing: all pages must be written (dirty)
 ok 56 Hugetlb shmem testing: all pages dirty other than first and the last one
 ok 57 Hugetlb shmem testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 58 Hugetlb shmem testing: only middle page dirty
 not ok 59 Hugetlb shmem testing: only two middle pages dirty
 ok 60 Hugetlb mem testing: all new pages must not be written (dirty)
 ok 61 Hugetlb mem testing: all pages must be written (dirty)
 ok 62 Hugetlb mem testing: all pages dirty other than first and the last one
 ok 63 Hugetlb mem testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 64 Hugetlb mem testing: only middle page dirty
 not ok 65 Hugetlb mem testing: only two middle pages dirty
 ok 66 Hugetlb shmem testing: all new pages must not be written (dirty)
 ok 67 Hugetlb shmem testing: all pages must be written (dirty)
 ok 68 Hugetlb shmem testing: all pages dirty other than first and the last one
 ok 69 Hugetlb shmem testing: PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC
 ok 70 Hugetlb shmem testing: only middle page dirty
 not ok 71 Hugetlb shmem testing: only two middle pages dirty
 ok 72 File memory testing: all new pages must not be written (dirty)
 ok 73 File memory testing: all p
 # Totals: pass:113 fail:4 xfail:0 xpass:0 skip:0 error:0

[/TESTING]

In the LSFMM/BFP 2025, there was a general agreement that we 1) would like to have a generic pagewalk API 2) that replaces the existing one with callbacks if possible and 3) that HugeTLB can use without the need to special case it (e.g: not having to depend on .hugetlb_entry callbacks)., which means having a lot of duplicated code and also having a lot of special casing just because hugetlb lore.

pt_range_walk API tries to do that and replaces the old behaviour of "in HugeTLB world everything reads as a PTE" and starts reading HugeTLB entries the way they really are, that means interpreting them as PMD/PUD entries and contiguous-PMD/PTE entries.

In order to achieve that, we need some infrastructure we did not really need until know, in order to be able to read HugeTLB pages as PUD/PMD entries. E.g: softleaf_from_pud had to be added and some other pud_* functions.

In a few words, this API goes through an address range and returns whatever it is in there (swap/hwpoison/migration/marker entries, folios, pfn and device entries, or nothing).

These are the internal return types the API uses:

PT_TYPE_NONE
PT_TYPE_FOLIO
PT_TYPE_MARKER
PT_TYPE_PFN
PT_TYPE_SWAP
PT_TYPE_MIGRATION
PT_TYPE_DEVICE
PT_TYPE_HWPOISON

The API also handles locking and batching itself, so the caller does not really need to bother with that.

In order to handle contiguous-PMD mapped hugetlb pages, folio_pmd_batch, which is an analogous of folio_pte_batch, has been implemented.

More information about the API can be found in patch #4.

This was tested on x86_64 and arm64, but as I said, it is still incomplete, therefore the RFC, to gather some initial feedback before investing more time into this.

For now, all users of the old API from fs/proc/task_mmu.c have been
converted: /proc/pid/(smaps|numa_maps|pagemap|clear_refs).

Thanks in advance

Oscar Salvador (8):
mm: Add softleaf_from_pud
mm: Add {pmd,pud}_huge_lock helper
mm: Implement folio_pmd_batch
mm: Implement pt_range_walk
mm: Make /proc/pid/smaps use the new generic pagewalk API
mm: Make /proc/pid/numa_maps use the new generic pagewalk API
mm: Make /proc/pid/pagemap use the new generic pagewalk API
mm: Make /proc/pid/clear_refs use the new generic pagewalk API

 arch/arm64/include/asm/pgtable.h             |   41 +
 arch/loongarch/include/asm/pgtable.h         |    1 +
 arch/powerpc/include/asm/book3s/64/pgtable.h |    7 +
 arch/s390/include/asm/pgtable.h              |   38 +
 arch/x86/include/asm/pgtable.h               |   53 +
 arch/x86/include/asm/pgtable_64.h            |    2 +
 arch/x86/mm/pgtable.c                        |   18 +-
 fs/proc/task_mmu.c                           | 2295 ++++++++----------
 include/asm-generic/pgtable_uffd.h           |   15 +
 include/linux/leafops.h                      |   46 +
 include/linux/mm.h                           |    2 +
 include/linux/mm_inline.h                    |   32 +
 include/linux/pagewalk.h                     |  106 +
 include/linux/pgtable.h                      |   95 +
 mm/internal.h                                |   75 +-
 mm/memory.c                                  |   22 +
 mm/pagewalk.c                                |  400 +++
 mm/pgtable-generic.c                         |   21 +
 18 files changed, 2039 insertions(+), 1230 deletions(-)

--
2.53.0

#15 lore raw
Quoted 6 lines · Lorenzo Stoakes

-cc wrong email

Sorry to be a pain but I only just noticed this because it's going to the wrong email address :)

Could you make sure to send future revisions to [email protected]?

I would have sworn that I used '[email protected]' but I guess I did not.

FWIW, I did use the right one here [1], so it might have just spaced out :-)

[1] https://lore.kernel.org/linux-mm/[email protected]/

Signature · 2 lines
Oscar Salvador
SUSE Labs