Skip to contentSkip to conversation

mm: mincore: misc cleanups

PATCH In review Kefeng Wang 2 participants 16 messages Jul 10 – Jul 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover mm: mincore: misc cleanups

This series cleans up and simplifies the mincore. Most importantly, it removes the historical special handling for VM_PFNMAP mappings, reporting them as resident memory instead of always being non-resident memory.

v2:
- Separate per-VMA lock changes as suggested by Pedro, it should be based on the new API [1], will resend once new API out.
- Check VM_PFNMAP explicitly in can_do_mincore() to avoid trigger a split in the page table walk, found in sashiko[[2]
- Replace GFP_USER with GFP_KERNEL, suggested by Andrew
- Add Pedro RB and mincore_page() refactor patch

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://sashiko.dev/#/patchset/20260701144047.3786939-1-wangkefeng.wang%40huawei.com?part=2

Kefeng Wang (5):
mm: mincore: remove special handling for VM_PFNMAP
mm: mincore: replace __get_free_page() with kmalloc()
mm: mincore: remove xa_is_value() in mincore_swap()
mm: mincore: improve mincore_hugetlb()
mm: mincore: refactor mincore_page()

 mm/mincore.c | 66 ++++++++++++++++++----------------------------------
 1 file changed, 22 insertions(+), 44 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/5 mm: mincore: remove special handling for VM_PFNMAP

As David pointed out, "it’s hard to believe that someone depends on pages in VM_PFNMAP to *not* be present", so remove the historical behavior that always reports VM_PFNMAP pages as non-resident. Instead, make them now reported as resident by checking the VMA_PFNMAP_BIT, simplifying the code.

---
 mm/mincore.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
mm/mincore.c +1 -9
index 53b982803771..0eb832ee0a30 100644
@@ -219,7 +219,7 @@ static inline bool can_do_mincore(struct vm_area_struct *vma)
 {
 	if (vma_is_anonymous(vma))
 		return true;
-	if (!vma->vm_file)
+	if (!vma->vm_file || vma_test(vma, VMA_PFNMAP_BIT))
 		return false;
 	/*
 	 * Reveal pagecache information only for non-anonymous mappings that
@@ -259,14 +259,6 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
 		return pages;
 	}
 
-	/*
-	 * mincore historically reports PFNMAP mappings as non-resident.
-	 */
-	if (vma->vm_flags & VM_PFNMAP) {
-		__mincore_unmapped_range(addr, end, vma, vec);
-		return (end - addr) >> PAGE_SHIFT;
-	}
-
 	err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
 	if (err < 0)
 		return err;
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/5] mm: mincore: remove special handling for VM_PFNMAP

Quoted 31 lines · Kefeng Wang

As David pointed out, "it’s hard to believe that someone depends on pages in VM_PFNMAP to *not* be present", so remove the historical behavior that always reports VM_PFNMAP pages as non-resident. Instead, make them now reported as resident by checking the VMA_PFNMAP_BIT, simplifying the code.

---
 mm/mincore.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
mm/mincore.c +1 -2
index 53b982803771..0eb832ee0a30 100644
@@ -219,7 +219,7 @@ static inline bool can_do_mincore(struct vm_area_struct *vma)
 {
 	if (vma_is_anonymous(vma))
 		return true;
-	if (!vma->vm_file)
+	if (!vma->vm_file || vma_test(vma, VMA_PFNMAP_BIT))
 		return false;
 	/*
 	 * Reveal pagecache information only for non-anonymous mappings that
@@ -259,14 +259,6 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
 		return pages;
 	}
 
-	/*

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 264

Quoted 10 lines
mm/mincore.c +0 -7
-	 * mincore historically reports PFNMAP mappings as non-resident.
-	 */
-	if (vma->vm_flags & VM_PFNMAP) {
-		__mincore_unmapped_range(addr, end, vma, vec);
-		return (end - addr) >> PAGE_SHIFT;
-	}
-
 	err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
 	if (err < 0)
 		return err;

What about MAP_PRIVATE PFN mappings with anonymous pages in there that could have migration entries?

I'd assume that we'd want to scan the thing and simply mark present ptes as ... present.

Signature · 3 lines
Cheers,

David
#3 lore raw

Re: [PATCH v2 1/5] mm: mincore: remove special handling for VM_PFNMAP

Quoted 47 lines · David Hildenbrand (Arm)
Quoted 31 lines · Kefeng Wang

As David pointed out, "it’s hard to believe that someone depends on pages in VM_PFNMAP to *not* be present", so remove the historical behavior that always reports VM_PFNMAP pages as non-resident. Instead, make them now reported as resident by checking the VMA_PFNMAP_BIT, simplifying the code.

---
  mm/mincore.c | 10 +---------
  1 file changed, 1 insertion(+), 9 deletions(-)
mm/mincore.c +1 -2
index 53b982803771..0eb832ee0a30 100644
@@ -219,7 +219,7 @@ static inline bool can_do_mincore(struct vm_area_struct *vma)
  {
  	if (vma_is_anonymous(vma))
  		return true;
-	if (!vma->vm_file)
+	if (!vma->vm_file || vma_test(vma, VMA_PFNMAP_BIT))
  		return false;
  	/*
  	 * Reveal pagecache information only for non-anonymous mappings that
@@ -259,14 +259,6 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
  		return pages;
  	}
 
-	/*

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 264

Quoted 10 lines
mm/mincore.c +0 -7
-	 * mincore historically reports PFNMAP mappings as non-resident.
-	 */
-	if (vma->vm_flags & VM_PFNMAP) {
-		__mincore_unmapped_range(addr, end, vma, vec);
-		return (end - addr) >> PAGE_SHIFT;
-	}
-
  	err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
  	if (err < 0)
  		return err;

What about MAP_PRIVATE PFN mappings with anonymous pages in there that could have migration entries?

Quoted 3 lines

I'd assume that we'd want to scan the thing and simply mark present ptes as ... present.

Sorry for the late, thanks for your review, the current modification is not very reasonable. After reconsidering the original Sahiko report, if we want to remove the special handling of the PFNMAP and not split the PUD page, we can add pud_entry processing and skip the PUD huge page, and mark pud as resident, the change is as follows,

mm/mincore.c +19 -0
index 53b982803771..dc976713ee5b 100644
@@ -160,6 +160,25 @@ static int mincore_unmapped_range(unsigned long addr, unsigned long end,
        return 0;
 }
 
+static int mincore_pud_range(pud_t *pud, unsigned long addr, unsigned long end,
+                       struct mm_walk *walk)
+{
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+       int nr = (end - addr) >> PAGE_SHIFT;
+       unsigned char *vec = walk->private;
+       spinlock_t *ptl;
+
+       ptl = pud_trans_huge_lock(pud, walk->vma);
+       if (!ptl)
+               return 0;
+
+       spin_unlock(ptl);
+       memset(vec, 1, nr);
+       walk->action = ACTION_CONTINUE;
+#endif
+       return 0;
+}
+
 static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned 

long end,
struct mm_walk *walk)
{
@@ -232,6 +251,7 @@ static inline bool can_do_mincore(struct vm_area_struct *vma)
}

static const struct mm_walk_ops mincore_walk_ops = {

+       .pud_entry              = mincore_pud_range,
        .pmd_entry              = mincore_pte_range,
        .pte_hole               = mincore_unmapped_range,
        .hugetlb_entry          = mincore_hugetlb,
@@ -259,14 +279,6 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *
v
                return pages;
        }

- /*
- * mincore historically reports PFNMAP mappings as non-resident.
- */
- if (vma->vm_flags & VM_PFNMAP) {
- __mincore_unmapped_range(addr, end, vma, vec);
- return (end - addr) >> PAGE_SHIFT;
- }
-

        err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
        if (err < 0)
                return err;

But more changes are needed, do you think this is appropriate, or should we keep things as they are, any better suggestions? Thanks.

#4 lore raw

Re: [PATCH v2 1/5] mm: mincore: remove special handling for VM_PFNMAP

Quoted 56 lines · Kefeng Wang
Quoted 49 lines · David Hildenbrand (Arm)
Quoted 35 lines · Kefeng Wang

As David pointed out, "it’s hard to believe that someone depends on pages in VM_PFNMAP to *not* be present", so remove the historical behavior that always reports VM_PFNMAP pages as non-resident. Instead, make them now reported as resident by checking the VMA_PFNMAP_BIT, simplifying the code.

mm/[email protected]/

---
  mm/mincore.c | 10 +---------

  1 file changed, 1 insertion(+), 9 deletions(-)
mm/mincore.c +1 -1
index 53b982803771..0eb832ee0a30 100644
@@ -219,7 +219,7 @@ static inline bool can_do_mincore(struct vm_area_struct
 *vma)
   {
       if (vma_is_anonymous(vma))
           return true;
-    if (!vma->vm_file)
+    if (!vma->vm_file || vma_test(vma, VMA_PFNMAP_BIT))
           return false;
       /*

       * Reveal pagecache information only for non-anonymous mappings that

mm/mincore.c +0 -0
@@ -259,14 +259,6 @@ static long do_mincore(unsigned long addr, unsigned long

pages, unsigned char *v
          return pages;
      }
  -    /*
-     * mincore historically reports PFNMAP mappings as non-resident. -     */

Quoted 8 lines

-    if (vma->vm_flags & VM_PFNMAP) {
-        __mincore_unmapped_range(addr, end, vma, vec);
-        return (end - addr) >> PAGE_SHIFT;
-    }
-
      err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec); if (err < 0)
          return err;

What about MAP_PRIVATE PFN mappings with anonymous pages in there that could have migration entries?

Quoted 3 lines

I'd assume that we'd want to scan the thing and simply mark present ptes as ... present.

Quoted 5 lines

Sorry for the late, thanks for your review, the current modification is not very reasonable. After reconsidering the original Sahiko report, if we want to remove the special handling of the PFNMAP and not split the PUD page, we can add pud_entry processing and skip the PUD huge page, and mark pud as resident, the change is as follows,

Yes, that sounds correct.

Quoted 16 lines
mm/mincore.c +6 -0
index 53b982803771..dc976713ee5b 100644
@@ -160,6 +160,25 @@ static int mincore_unmapped_range(unsigned long addr,
 unsigned long end,
         return 0;
  }
 
+static int mincore_pud_range(pud_t *pud, unsigned long addr, unsigned long end,
+                       struct mm_walk *walk)
+{
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+       int nr = (end - addr) >> PAGE_SHIFT;
+       unsigned char *vec = walk->private;

mm/mincore.c static int mincore_unmapped_range(unsigned long addr, · line 179

Quoted 10 lines
mm/mincore.c +10 -0
+       spinlock_t *ptl;
+
+       ptl = pud_trans_huge_lock(pud, walk->vma);
+       if (!ptl)
+               return 0;
+
+       spin_unlock(ptl);
+       memset(vec, 1, nr);
+       walk->action = ACTION_CONTINUE;
+#endif

Do we really need the lock?

pud = pudp_get(pudp);
if (!pud_none() && (!pud_present(pud) || pud_leaf(pud)))

	...

I'd prefer not using more of the pud_trans_huge() stuff directly.

I'm not really a friend of pmd_is_huge() either, but maybe we should just add a pud_is_huge() and use that here.

Signature · 3 lines
Cheers,

David
#5 lore raw

Re: [PATCH v2 1/5] mm: mincore: remove special handling for VM_PFNMAP

Quoted 94 lines · David Hildenbrand (Arm)
Quoted 56 lines · Kefeng Wang
Quoted 49 lines · David Hildenbrand (Arm)
Quoted 35 lines · Kefeng Wang

As David pointed out, "it’s hard to believe that someone depends on pages in VM_PFNMAP to *not* be present", so remove the historical behavior that always reports VM_PFNMAP pages as non-resident. Instead, make them now reported as resident by checking the VMA_PFNMAP_BIT, simplifying the code.

mm/[email protected]/

---
   mm/mincore.c | 10 +---------
   1 file changed, 1 insertion(+), 9 deletions(-)
mm/mincore.c +1 -1
index 53b982803771..0eb832ee0a30 100644
@@ -219,7 +219,7 @@ static inline bool can_do_mincore(struct vm_area_struct
 *vma)
   {
       if (vma_is_anonymous(vma))
           return true;
-    if (!vma->vm_file)
+    if (!vma->vm_file || vma_test(vma, VMA_PFNMAP_BIT))
           return false;
       /*

       * Reveal pagecache information only for non-anonymous mappings that

mm/mincore.c +0 -0
@@ -259,14 +259,6 @@ static long do_mincore(unsigned long addr, unsigned long

pages, unsigned char *v
          return pages;
      }
  -    /*
-     * mincore historically reports PFNMAP mappings as non-resident. -     */

Quoted 8 lines

-    if (vma->vm_flags & VM_PFNMAP) {
-        __mincore_unmapped_range(addr, end, vma, vec);
-        return (end - addr) >> PAGE_SHIFT;
-    }
-
      err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec); if (err < 0)
          return err;

What about MAP_PRIVATE PFN mappings with anonymous pages in there that could have migration entries?

Quoted 3 lines

I'd assume that we'd want to scan the thing and simply mark present ptes as ... present.

Quoted 5 lines

Sorry for the late, thanks for your review, the current modification is not very reasonable. After reconsidering the original Sahiko report, if we want to remove the special handling of the PFNMAP and not split the PUD page, we can add pud_entry processing and skip the PUD huge page, and mark pud as resident, the change is as follows,

Yes, that sounds correct.

Quoted 16 lines
mm/mincore.c +6 -0
index 53b982803771..dc976713ee5b 100644
@@ -160,6 +160,25 @@ static int mincore_unmapped_range(unsigned long addr,
 unsigned long end,
         return 0;
  }
 
+static int mincore_pud_range(pud_t *pud, unsigned long addr, unsigned long end,
+                       struct mm_walk *walk)
+{
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+       int nr = (end - addr) >> PAGE_SHIFT;
+       unsigned char *vec = walk->private;

mm/mincore.c static int mincore_unmapped_range(unsigned long addr, · line 179

Quoted 10 lines
mm/mincore.c +10 -0
+       spinlock_t *ptl;
+
+       ptl = pud_trans_huge_lock(pud, walk->vma);
+       if (!ptl)
+               return 0;
+
+       spin_unlock(ptl);
+       memset(vec, 1, nr);
+       walk->action = ACTION_CONTINUE;
+#endif

Do we really need the lock?

Quoted 8 lines

pud = pudp_get(pudp);
if (!pud_none() && (!pud_present(pud) || pud_leaf(pud)))

	...

I'd prefer not using more of the pud_trans_huge() stuff directly.

I'm not really a friend of pmd_is_huge() either, but maybe we should just add a pud_is_huge() and use that here.

To make it clear,let's introduce a pud_is_huge helper, still use pud_trans_huge(() in it,

static inline bool pud_is_huge(pud_t pud)

{
       if (pud_present(pud))
               return pud_trans_huge(pud);
       else if (!pud_none(pud))
               return true;
       return false;
}

and in mincore_pud_range(),

if (pud_is_huge(pudp_get(pudp)))

	...

I will update it, please correct me if I misunderstood. Thanks.

#6 lore raw

Re: [PATCH v2 1/5] mm: mincore: remove special handling for VM_PFNMAP

Quoted 48 lines · Kefeng Wang
Quoted 45 lines · David Hildenbrand (Arm)
Quoted 8 lines · Kefeng Wang

Sorry for the late, thanks for your review, the current modification is not very reasonable. After reconsidering the original Sahiko report, if we want to remove the special handling of the PFNMAP and not split the PUD page, we can add pud_entry processing and skip the PUD huge page, and mark pud as resident, the change is as follows,

Yes, that sounds correct.

Quoted 16 lines
mm/mincore.c +6 -0
index 53b982803771..dc976713ee5b 100644
@@ -160,6 +160,25 @@ static int mincore_unmapped_range(unsigned long addr,
 unsigned long end,
          return 0;
   }
 
+static int mincore_pud_range(pud_t *pud, unsigned long addr, unsigned long end,
+                       struct mm_walk *walk)
+{
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+       int nr = (end - addr) >> PAGE_SHIFT;
+       unsigned char *vec = walk->private;

mm/mincore.c static int mincore_unmapped_range(unsigned long addr, · line 179

Quoted 10 lines
mm/mincore.c +10 -0
+       spinlock_t *ptl;
+
+       ptl = pud_trans_huge_lock(pud, walk->vma);
+       if (!ptl)
+               return 0;
+
+       spin_unlock(ptl);
+       memset(vec, 1, nr);
+       walk->action = ACTION_CONTINUE;
+#endif

Do we really need the lock?

pud = pudp_get(pudp);
if (!pud_none() && (!pud_present(pud) || pud_leaf(pud)))
    ...

Quoted 7 lines
Quoted 4 lines

I'd prefer not using more of the pud_trans_huge() stuff directly.

I'm not really a friend of pmd_is_huge() either, but maybe we should just add a pud_is_huge() and use that here.

To make it clear,let's introduce a pud_is_huge helper, still use pud_trans_huge(() in it,

yes.

That's what I would do, maybe people reading along have better ideas.

Signature · 3 lines
Cheers,

David
#7 lore raw

PATCH v2 2/5 mm: mincore: replace __get_free_page() with kmalloc()

In reply to Kefeng Wang

Remove ugly casts by using the more natural kmalloc/kfree allocation, also replace GFP_USER(pointless here) with GFP_KERNEL.

---
 mm/mincore.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/mincore.c +3 -2
index 0eb832ee0a30..bff19a0df9f5 100644
@@ -12,6 +12,7 @@
 #include <linux/gfp.h>
 #include <linux/pagewalk.h>
 #include <linux/mman.h>
+#include <linux/slab.h>
 #include <linux/syscalls.h>
 #include <linux/swap.h>
 #include <linux/leafops.h>
@@ -313,7 +314,7 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
 	if (!access_ok(vec, pages))
 		return -EFAULT;
 
-	tmp = (void *) __get_free_page(GFP_USER);
+	tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!tmp)
 		return -EAGAIN;
 
@@ -338,6 +339,6 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
 		start += retval << PAGE_SHIFT;
 		retval = 0;
 	}
-	free_page((unsigned long) tmp);
+	kfree(tmp);
 	return retval;
 }
Signature · 1 line
2.55.0
#9 lore raw

PATCH v2 3/5 mm: mincore: remove xa_is_value() in mincore_swap()

In reply to Kefeng Wang

The swap_cache_get_folio() no longer returns shadow entries, so the xa_is_value() check is unnecessary.

---
 mm/mincore.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/mincore.c +1 -2
index bff19a0df9f5..8b5e6afefb91 100644
@@ -91,8 +91,7 @@ static unsigned char mincore_swap(swp_entry_t entry, bool shmem)
 	folio = swap_cache_get_folio(entry);
 	if (shmem)
 		put_swap_device(si);
-	/* The swap cache space contains either folio, shadow or NULL */
-	if (folio && !xa_is_value(folio)) {
+	if (folio) {
 		present = folio_test_uptodate(folio);
 		folio_put(folio);
 	}
Signature · 1 line
2.55.0
#11 lore raw

PATCH v2 4/5 mm: mincore: improve mincore_hugetlb()

In reply to Kefeng Wang

The walk_hugetlb_range() always passes a non-NULL pte, so remove the dead NULL check. Replace the per-page iteration loop with memset() for better performance.

---
 mm/mincore.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)
mm/mincore.c +7 -20
index 8b5e6afefb91..9952bc3edf76 100644
@@ -28,30 +28,17 @@ static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
 			unsigned long end, struct mm_walk *walk)
 {
 #ifdef CONFIG_HUGETLB_PAGE
-	unsigned char present;
+	unsigned long nr = (end - addr) >> PAGE_SHIFT;
 	unsigned char *vec = walk->private;
+	unsigned char present;
 	spinlock_t *ptl;
+	pte_t ptep;
 
 	ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
-
-	/*
-	 * Hugepages under user process are always in RAM and never
-	 * swapped out, but theoretically it needs to be checked.
-	 */
-	if (!pte) {
-		present = 0;
-	} else {
-		const pte_t ptep = huge_ptep_get(walk->mm, addr, pte);
-
-		if (huge_pte_none(ptep) || pte_is_marker(ptep))
-			present = 0;
-		else
-			present = 1;
-	}
-
-	for (; addr != end; vec++, addr += PAGE_SIZE)
-		*vec = present;
-	walk->private = vec;
+	ptep = huge_ptep_get(walk->mm, addr, pte);
+	present = !(huge_pte_none(ptep) || pte_is_marker(ptep));
+	memset(vec, present, nr);
+	walk->private += nr;
 	spin_unlock(ptl);
 #else
 	BUG();
Signature · 1 line
2.55.0
#12 lore raw

Re: [PATCH v2 4/5] mm: mincore: improve mincore_hugetlb()

Quoted 37 lines · Kefeng Wang

The walk_hugetlb_range() always passes a non-NULL pte, so remove the dead NULL check. Replace the per-page iteration loop with memset() for better performance.

---
 mm/mincore.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)
mm/mincore.c +3 -12
index 8b5e6afefb91..9952bc3edf76 100644
@@ -28,30 +28,17 @@ static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
 			unsigned long end, struct mm_walk *walk)
 {
 #ifdef CONFIG_HUGETLB_PAGE
-	unsigned char present;
+	unsigned long nr = (end - addr) >> PAGE_SHIFT;
 	unsigned char *vec = walk->private;
+	unsigned char present;
 	spinlock_t *ptl;
+	pte_t ptep;
 
 	ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
-
-	/*
-	 * Hugepages under user process are always in RAM and never
-	 * swapped out, but theoretically it needs to be checked.
-	 */
-	if (!pte) {
-		present = 0;
-	} else {
-		const pte_t ptep = huge_ptep_get(walk->mm, addr, pte);
-
-		if (huge_pte_none(ptep) || pte_is_marker(ptep))

mm/mincore.c static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, · line 39

Quoted 10 lines
mm/mincore.c +2 -8
-			present = 0;
-		else
-			present = 1;
-	}
-
-	for (; addr != end; vec++, addr += PAGE_SIZE)
-		*vec = present;
-	walk->private = vec;
+	ptep = huge_ptep_get(walk->mm, addr, pte);
+	present = !(huge_pte_none(ptep) || pte_is_marker(ptep));

Easier to read as

	present = !huge_pte_none(ptep) && !pte_is_marker(ptep);

But now I wonder "why not pte_present()".

Calling this "present" is misleading. It's maybe "resident"?

I guess we want to indicate migration entries and hwpoison entries as "1".

Signature · 3 lines
Cheers,

David
#13 lore raw

Re: [PATCH v2 4/5] mm: mincore: improve mincore_hugetlb()

Quoted 51 lines · David Hildenbrand (Arm)
Quoted 37 lines · Kefeng Wang

The walk_hugetlb_range() always passes a non-NULL pte, so remove the dead NULL check. Replace the per-page iteration loop with memset() for better performance.

---
  mm/mincore.c | 27 +++++++--------------------
  1 file changed, 7 insertions(+), 20 deletions(-)
mm/mincore.c +3 -12
index 8b5e6afefb91..9952bc3edf76 100644
@@ -28,30 +28,17 @@ static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
  			unsigned long end, struct mm_walk *walk)
  {
  #ifdef CONFIG_HUGETLB_PAGE
-	unsigned char present;
+	unsigned long nr = (end - addr) >> PAGE_SHIFT;
  	unsigned char *vec = walk->private;
+	unsigned char present;
  	spinlock_t *ptl;
+	pte_t ptep;
 
  	ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
-
-	/*
-	 * Hugepages under user process are always in RAM and never
-	 * swapped out, but theoretically it needs to be checked.
-	 */
-	if (!pte) {
-		present = 0;
-	} else {
-		const pte_t ptep = huge_ptep_get(walk->mm, addr, pte);
-
-		if (huge_pte_none(ptep) || pte_is_marker(ptep))

mm/mincore.c static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, · line 39

Quoted 10 lines
mm/mincore.c +2 -8
-			present = 0;
-		else
-			present = 1;
-	}
-
-	for (; addr != end; vec++, addr += PAGE_SIZE)
-		*vec = present;
-	walk->private = vec;
+	ptep = huge_ptep_get(walk->mm, addr, pte);
+	present = !(huge_pte_none(ptep) || pte_is_marker(ptep));

Easier to read as

Quoted 1 line
	present = !huge_pte_none(ptep) && !pte_is_marker(ptep);

OK.

Quoted 7 lines

But now I wonder "why not pte_present()".

Calling this "present" is misleading. It's maybe "resident"?

I guess we want to indicate migration entries and hwpoison entries as "1".

mincore - determine whether pages are resident in memory, so resident is much clear, will rename it, thanks.

#14 lore raw

PATCH v2 5/5 mm: mincore: refactor mincore_page()

In reply to Kefeng Wang

Use early returns to reduce indentation and improve readability.

---
 mm/mincore.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
mm/mincore.c +10 -11
index 9952bc3edf76..30a3317082d1 100644
@@ -94,7 +94,7 @@ static unsigned char mincore_swap(swp_entry_t entry, bool shmem)
  */
 static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
 {
-	unsigned char present = 0;
+	unsigned char present;
 	struct folio *folio;
 
 	/*
@@ -104,17 +104,16 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
 	 * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
 	 */
 	folio = filemap_get_entry(mapping, index);
-	if (folio) {
-		if (xa_is_value(folio)) {
-			if (shmem_mapping(mapping))
-				return mincore_swap(radix_to_swp_entry(folio),
-						    true);
-			else
-				return 0;
-		}
-		present = folio_test_uptodate(folio);
-		folio_put(folio);
+	if (!folio)
+		return 0;
+
+	if (xa_is_value(folio)) {
+		if (!shmem_mapping(mapping))
+			return 0;
+		return mincore_swap(radix_to_swp_entry(folio), true);
 	}
+	present = folio_test_uptodate(folio);
+	folio_put(folio);
 
 	return present;
 }
Signature · 1 line
2.55.0
Acked-by on v2 5/5
#15 lore raw

Re: [PATCH v2 5/5] mm: mincore: refactor mincore_page()

Quoted 34 lines · Kefeng Wang

Use early returns to reduce indentation and improve readability.

---
 mm/mincore.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
mm/mincore.c +1 -11
index 9952bc3edf76..30a3317082d1 100644
@@ -94,7 +94,7 @@ static unsigned char mincore_swap(swp_entry_t entry, bool shmem)
  */
 static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
 {
-	unsigned char present = 0;
+	unsigned char present;
 	struct folio *folio;
 
 	/*
@@ -104,17 +104,16 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
 	 * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
 	 */
 	folio = filemap_get_entry(mapping, index);
-	if (folio) {
-		if (xa_is_value(folio)) {
-			if (shmem_mapping(mapping))
-				return mincore_swap(radix_to_swp_entry(folio),
-						    true);
-			else
-				return 0;
-		}
-		present = folio_test_uptodate(folio);
-		folio_put(folio);

mm/mincore.c static unsigned char mincore_page(struct address_space *mapping, pgoff_t index) · line 116

Quoted 10 lines
mm/mincore.c +9 -0
+	if (!folio)
+		return 0;
+
+	if (xa_is_value(folio)) {
+		if (!shmem_mapping(mapping))
+			return 0;
+		return mincore_swap(radix_to_swp_entry(folio), true);
 	}
+	present = folio_test_uptodate(folio);
+	folio_put(folio);
Signature · 3 lines
Cheers,

David