Subject: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only

With DEVICE_COHERENT, we'll soon have vm_normal_pages() return
device-managed anonymous pages that are not LRU pages. Although they
behave like normal pages for purposes of mapping in CPU page, and for
COW. They do not support LRU lists, NUMA migration or THP. The
difference between new vm_normal_lru_pages vs vm_normal_pages() is,
the former makes sure to return pages that are LRU handled only.

We also introduced a FOLL_LRU flag that adds the same behaviour to
follow_page and related APIs, to allow callers to specify that they
expect to put pages on an LRU list.

Signed-off-by: Alex Sierra <[email protected]>
Acked-by: Felix Kuehling <[email protected]>
---
fs/proc/task_mmu.c | 2 +-
include/linux/mm.h | 9 ++++++---
mm/gup.c | 8 +++++---
mm/huge_memory.c | 2 +-
mm/khugepaged.c | 8 ++++----
mm/ksm.c | 4 ++--
mm/madvise.c | 4 ++--
mm/memory.c | 40 ++++++++++++++++++++++++++++++++++++++--
mm/mempolicy.c | 4 ++--
mm/migrate.c | 2 +-
mm/mlock.c | 6 +++---
mm/mprotect.c | 2 +-
12 files changed, 66 insertions(+), 25 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index f46060eb91b5..1791a86d9ecb 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1784,7 +1784,7 @@ static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
if (!pte_present(pte))
return NULL;

- page = vm_normal_page(vma, addr, pte);
+ page = vm_normal_lru_page(vma, addr, pte);
if (!page)
return NULL;

diff --git a/include/linux/mm.h b/include/linux/mm.h
index d507c32724c0..0299dbb35335 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -593,8 +593,8 @@ struct vm_operations_struct {
unsigned long addr);
#endif
/*
- * Called by vm_normal_page() for special PTEs to find the
- * page for @addr. This is useful if the default behavior
+ * Called by vm_normal_*_page() for special PTEs to find the
+ * page for @addr. This is useful if the default behavior
* (using pte_page()) would not find the correct page.
*/
struct page *(*find_special_page)(struct vm_area_struct *vma,
@@ -1783,6 +1783,8 @@ extern void user_shm_unlock(size_t, struct ucounts *);

struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
pte_t pte);
+struct page *vm_normal_lru_page(struct vm_area_struct *vma, unsigned long addr,
+ pte_t pte);
struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
pmd_t pmd);

@@ -2901,6 +2903,7 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
#define FOLL_SPLIT_PMD 0x20000 /* split huge pmd before returning */
#define FOLL_PIN 0x40000 /* pages must be released via unpin_user_page */
#define FOLL_FAST_ONLY 0x80000 /* gup_fast: prevent fall-back to slow gup */
+#define FOLL_LRU 0x100000 /* return only LRU (anon or page cache) */

/*
* FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
@@ -3227,7 +3230,7 @@ extern long copy_huge_page_from_user(struct page *dst_page,
* @vma: Pointer to the struct vm_area_struct to consider
*
* Whether transhuge page-table entries are considered "special" following
- * the definition in vm_normal_page().
+ * the definition in vm_normal_*_page().
*
* Return: true if transhuge page-table entries should be considered special,
* false otherwise.
diff --git a/mm/gup.c b/mm/gup.c
index 41349b685eaf..d0494428a0c3 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -539,8 +539,10 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
pte_unmap_unlock(ptep, ptl);
return NULL;
}
-
- page = vm_normal_page(vma, address, pte);
+ if (flags & (FOLL_MLOCK | FOLL_LRU))
+ page = vm_normal_lru_page(vma, address, pte);
+ else
+ page = vm_normal_page(vma, address, pte);
if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) {
/*
* Only return device mapping pages in the FOLL_GET or FOLL_PIN
@@ -824,7 +826,7 @@ static struct page *follow_p4d_mask(struct vm_area_struct *vma,
*
* Return: the mapped (struct page *), %NULL if no mapping exists, or
* an error pointer if there is a mapping to something not represented
- * by a page descriptor (see also vm_normal_page()).
+ * by a page descriptor (see also vm_normal_*_page()).
*/
static struct page *follow_page_mask(struct vm_area_struct *vma,
unsigned long address, unsigned int flags,
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 406a3c28c026..ea1efc825774 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2966,7 +2966,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
}

/* FOLL_DUMP to ignore special (like zero) pages */
- follflags = FOLL_GET | FOLL_DUMP;
+ follflags = FOLL_GET | FOLL_DUMP | FOLL_LRU;
page = follow_page(vma, addr, follflags);

if (IS_ERR(page))
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 131492fd1148..a7153db09afa 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -627,7 +627,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
result = SCAN_PTE_NON_PRESENT;
goto out;
}
- page = vm_normal_page(vma, address, pteval);
+ page = vm_normal_lru_page(vma, address, pteval);
if (unlikely(!page)) {
result = SCAN_PAGE_NULL;
goto out;
@@ -1286,7 +1286,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
if (pte_write(pteval))
writable = true;

- page = vm_normal_page(vma, _address, pteval);
+ page = vm_normal_lru_page(vma, _address, pteval);
if (unlikely(!page)) {
result = SCAN_PAGE_NULL;
goto out_unmap;
@@ -1494,7 +1494,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
if (!pte_present(*pte))
goto abort;

- page = vm_normal_page(vma, addr, *pte);
+ page = vm_normal_lru_page(vma, addr, *pte);

/*
* Note that uprobe, debugger, or MAP_PRIVATE may change the
@@ -1512,7 +1512,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)

if (pte_none(*pte))
continue;
- page = vm_normal_page(vma, addr, *pte);
+ page = vm_normal_lru_page(vma, addr, *pte);
page_remove_rmap(page, false);
}

diff --git a/mm/ksm.c b/mm/ksm.c
index c20bd4d9a0d9..352d37e44694 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -474,7 +474,7 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
do {
cond_resched();
page = follow_page(vma, addr,
- FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE);
+ FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE | FOLL_LRU);
if (IS_ERR_OR_NULL(page))
break;
if (PageKsm(page))
@@ -559,7 +559,7 @@ static struct page *get_mergeable_page(struct rmap_item *rmap_item)
if (!vma)
goto out;

- page = follow_page(vma, addr, FOLL_GET);
+ page = follow_page(vma, addr, FOLL_GET | FOLL_LRU);
if (IS_ERR_OR_NULL(page))
goto out;
if (PageAnon(page)) {
diff --git a/mm/madvise.c b/mm/madvise.c
index 38d0f515d548..8dcfb93fe0d5 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -411,7 +411,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
if (!pte_present(ptent))
continue;

- page = vm_normal_page(vma, addr, ptent);
+ page = vm_normal_lru_page(vma, addr, ptent);
if (!page)
continue;

@@ -621,7 +621,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
continue;
}

- page = vm_normal_page(vma, addr, ptent);
+ page = vm_normal_lru_page(vma, addr, ptent);
if (!page)
continue;

diff --git a/mm/memory.c b/mm/memory.c
index c125c4969913..5e83b65a74aa 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -621,6 +621,13 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
if (is_zero_pfn(pfn))
return NULL;
if (pte_devmap(pte))
+/*
+ * NOTE: Technically this should goto check_pfn label. However, page->_mapcount
+ * is never incremented for device pages that are mmap through DAX mechanism
+ * using pmem driver mounted into ext4 filesystem. When these pages are unmap,
+ * zap_pte_range is called and vm_normal_page return a valid page with
+ * page_mapcount() = 0, before page_remove_rmap is called.
+ */
return NULL;

print_bad_pte(vma, addr, pte, NULL);
@@ -661,6 +668,35 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
return pfn_to_page(pfn);
}

+/**
+ * vm_normal_lru_page - gets LRU handled page associated with a pte.
+ *
+ * @vma: user vma the page belongs to.
+ * @addr: user address the page belongs to.
+ * @pte: page table entry associated to the page.
+ *
+ * This function gets the "struct page" associated with a pte, only for pages
+ * that can be put on an LRU list and that support NUMA migration, KSM and
+ * THP.
+ *
+ * With DEVICE_COHERENT introduction, vm_normal_pages() could return
+ * device-managed anonymous pages that are not LRU pages. This
+ * vm_normal_lru_page function, makes sure to return LRU handled pages only.
+ *
+ * Return: "struct page" reference associated with the pte.
+ */
+struct page *vm_normal_lru_page(struct vm_area_struct *vma, unsigned long addr,
+ pte_t pte)
+{
+ struct page *page;
+
+ page = vm_normal_page(vma, addr, pte);
+ if (page && is_zone_device_page(page))
+ return NULL;
+
+ return page;
+}
+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
pmd_t pmd)
@@ -2168,7 +2204,7 @@ EXPORT_SYMBOL(vmf_insert_pfn);

static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
{
- /* these checks mirror the abort conditions in vm_normal_page */
+ /* these checks mirror the abort conditions in vm_normal_lru_page */
if (vma->vm_flags & VM_MIXEDMAP)
return true;
if (pfn_t_devmap(pfn))
@@ -4364,7 +4400,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
old_pte = ptep_get(vmf->pte);
pte = pte_modify(old_pte, vma->vm_page_prot);

- page = vm_normal_page(vma, vmf->address, pte);
+ page = vm_normal_lru_page(vma, vmf->address, pte);
if (!page)
goto out_map;

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 69284d3b5e53..651408f14b3e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -527,11 +527,11 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr,
for (; addr != end; pte++, addr += PAGE_SIZE) {
if (!pte_present(*pte))
continue;
- page = vm_normal_page(vma, addr, *pte);
+ page = vm_normal_lru_page(vma, addr, *pte);
if (!page)
continue;
/*
- * vm_normal_page() filters out zero pages, but there might
+ * vm_normal_lru_page() filters out zero pages, but there might
* still be PageReserved pages to skip, perhaps in a VDSO.
*/
if (PageReserved(page))
diff --git a/mm/migrate.c b/mm/migrate.c
index c31d04b46a5e..17d049311b78 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1614,7 +1614,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
goto out;

/* FOLL_DUMP to ignore special (like zero) pages */
- follflags = FOLL_GET | FOLL_DUMP;
+ follflags = FOLL_GET | FOLL_DUMP | FOLL_LRU;
page = follow_page(vma, addr, follflags);

err = PTR_ERR(page);
diff --git a/mm/mlock.c b/mm/mlock.c
index 25934e7db3e1..bb09926aeee7 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -342,7 +342,7 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
* a non-TPH page already pinned and in the @pvec, and that it belongs to @zone.
*
* The rest of @pvec is filled by subsequent pages within the same pmd and same
- * zone, as long as the pte's are present and vm_normal_page() succeeds. These
+ * zone, as long as the pte's are present and vm_normal_lru_page() succeeds. These
* pages also get pinned.
*
* Returns the address of the next page that should be scanned. This equals
@@ -373,7 +373,7 @@ static unsigned long __munlock_pagevec_fill(struct pagevec *pvec,
struct page *page = NULL;
pte++;
if (pte_present(*pte))
- page = vm_normal_page(vma, start, *pte);
+ page = vm_normal_lru_page(vma, start, *pte);
/*
* Break if page could not be obtained or the page's node+zone does not
* match
@@ -439,7 +439,7 @@ void munlock_vma_pages_range(struct vm_area_struct *vma,
* suits munlock very well (and if somehow an abnormal page
* has sneaked into the range, we won't oops here: great).
*/
- page = follow_page(vma, start, FOLL_GET | FOLL_DUMP);
+ page = follow_page(vma, start, FOLL_GET | FOLL_DUMP | FOLL_LRU);

if (page && !IS_ERR(page)) {
if (PageTransTail(page)) {
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 2887644fd150..bc3e75334aeb 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -88,7 +88,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
if (pte_protnone(oldpte))
continue;

- page = vm_normal_page(vma, addr, oldpte);
+ page = vm_normal_lru_page(vma, addr, oldpte);
if (!page || PageKsm(page))
continue;

--
2.32.0


2022-03-31 09:53:59

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only

On 31.03.22 10:53, Christoph Hellwig wrote:
>> - page = vm_normal_page(vma, addr, pte);
>> + page = vm_normal_lru_page(vma, addr, pte);
>
> Why can't this deal with ZONE_DEVICE pages? It certainly has
> nothing do with a LRU I think. In fact being able to have
> stats that count say the number of device pages here would
> probably be useful at some point.
>
> In general I find the vm_normal_lru_page vs vm_normal_page
> API highly confusing. An explicit check for zone device pages
> in the dozen or so spots that care has a much better documentation
> value, especially if accompanied by comments where it isn't entirely
> obvious.

What's your thought on FOLL_LRU?

--
Thanks,

David / dhildenb

2022-03-31 12:03:20

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only

On Thu, Mar 31, 2022 at 10:55:13AM +0200, David Hildenbrand wrote:
> > Why can't this deal with ZONE_DEVICE pages? It certainly has
> > nothing do with a LRU I think. In fact being able to have
> > stats that count say the number of device pages here would
> > probably be useful at some point.
> >
> > In general I find the vm_normal_lru_page vs vm_normal_page
> > API highly confusing. An explicit check for zone device pages
> > in the dozen or so spots that care has a much better documentation
> > value, especially if accompanied by comments where it isn't entirely
> > obvious.
>
> What's your thought on FOLL_LRU?

Also a bit confusing, but inbetween all these FOLL_ flags it doesn't
really matter any more.

2022-03-31 13:38:05

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only

> - page = vm_normal_page(vma, addr, pte);
> + page = vm_normal_lru_page(vma, addr, pte);

Why can't this deal with ZONE_DEVICE pages? It certainly has
nothing do with a LRU I think. In fact being able to have
stats that count say the number of device pages here would
probably be useful at some point.

In general I find the vm_normal_lru_page vs vm_normal_page
API highly confusing. An explicit check for zone device pages
in the dozen or so spots that care has a much better documentation
value, especially if accompanied by comments where it isn't entirely
obvious.

> page = follow_page(vma, addr,
> - FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE);
> + FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE | FOLL_LRU);

Overly long line here.

> +/*
> + * NOTE: Technically this should goto check_pfn label. However, page->_mapcount
> + * is never incremented for device pages that are mmap through DAX mechanism
> + * using pmem driver mounted into ext4 filesystem. When these pages are unmap,
> + * zap_pte_range is called and vm_normal_page return a valid page with
> + * page_mapcount() = 0, before page_remove_rmap is called.
> + */

Please properly indent comments.

> + * zone, as long as the pte's are present and vm_normal_lru_page() succeeds. These
> * pages also get pinned.

Another overly long line here.

2022-04-02 05:29:03

by Felix Kuehling

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only


On 2022-03-31 04:53, Christoph Hellwig wrote:
>> - page = vm_normal_page(vma, addr, pte);
>> + page = vm_normal_lru_page(vma, addr, pte);
> Why can't this deal with ZONE_DEVICE pages? It certainly has
> nothing do with a LRU I think. In fact being able to have
> stats that count say the number of device pages here would
> probably be useful at some point.

Maybe at some point. However, this is in a function called
"can_gather_numa_stats". There are no meaningful NUMA stats for device
pages. I agree that the name "vm_normal_lru_page" is not optimal in this
case.


>
> In general I find the vm_normal_lru_page vs vm_normal_page
> API highly confusing. An explicit check for zone device pages
> in the dozen or so spots that care has a much better documentation
> value, especially if accompanied by comments where it isn't entirely
> obvious.

OK. We can do that. It would solve the function naming problem, and we'd
have more visibility of device page handling in more places in the
kernel, which has educational value.

Regards,
  Felix


>
>> page = follow_page(vma, addr,
>> - FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE);
>> + FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE | FOLL_LRU);
> Overly long line here.
>
>> +/*
>> + * NOTE: Technically this should goto check_pfn label. However, page->_mapcount
>> + * is never incremented for device pages that are mmap through DAX mechanism
>> + * using pmem driver mounted into ext4 filesystem. When these pages are unmap,
>> + * zap_pte_range is called and vm_normal_page return a valid page with
>> + * page_mapcount() = 0, before page_remove_rmap is called.
>> + */
> Please properly indent comments.
>
>> + * zone, as long as the pte's are present and vm_normal_lru_page() succeeds. These
>> * pages also get pinned.
> Another overly long line here.

2022-04-05 01:07:52

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only

On Fri, Apr 01, 2022 at 04:08:35PM -0400, Felix Kuehling wrote:

> > In general I find the vm_normal_lru_page vs vm_normal_page
> > API highly confusing. An explicit check for zone device pages
> > in the dozen or so spots that care has a much better documentation
> > value, especially if accompanied by comments where it isn't entirely
> > obvious.
>
> OK. We can do that. It would solve the function naming problem, and we'd
> have more visibility of device page handling in more places in the kernel,
> which has educational value.

Personally I find the 'is page XYZ' pretty confusing, like I don't
know half of what the PageKsm annotations are for..

Testing against a specific property the code goes on to use right away
seems more descriptive to me.

Jason

Subject: Re: [PATCH v2 1/3] mm: add vm_normal_lru_pages for LRU handled pages only


On 4/4/2022 12:38 PM, Jason Gunthorpe wrote:
> On Fri, Apr 01, 2022 at 04:08:35PM -0400, Felix Kuehling wrote:
>
>>> In general I find the vm_normal_lru_page vs vm_normal_page
>>> API highly confusing. An explicit check for zone device pages
>>> in the dozen or so spots that care has a much better documentation
>>> value, especially if accompanied by comments where it isn't entirely
>>> obvious.
>> OK. We can do that. It would solve the function naming problem, and we'd
>> have more visibility of device page handling in more places in the kernel,
>> which has educational value.
> Personally I find the 'is page XYZ' pretty confusing, like I don't
> know half of what the PageKsm annotations are for..
>
> Testing against a specific property the code goes on to use right away
> seems more descriptive to me.

Hi Jason,

Are you referring to test for properties such as is_lru_page,
is_numa_page, is_lockable_page, etc?
Otherwise, could you provide an example?

Regards,
Alex Sierra

>
> Jason