diff mbox series

[1/2] mm: Add pvmw_set_page()

Message ID 20220203171904.609984-1-willy@infradead.org (mailing list archive)
State New
Headers show
Series [1/2] mm: Add pvmw_set_page() | expand

Commit Message

Matthew Wilcox Feb. 3, 2022, 5:19 p.m. UTC
Instead of setting the page directly in struct page_vma_mapped_walk,
use this helper to allow us to transition to a PFN approach in the
next patch.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/rmap.h    |  6 ++++++
 kernel/events/uprobes.c |  2 +-
 mm/damon/paddr.c        |  4 ++--
 mm/ksm.c                |  2 +-
 mm/migrate.c            |  2 +-
 mm/page_idle.c          |  2 +-
 mm/rmap.c               | 12 ++++++------
 7 files changed, 18 insertions(+), 12 deletions(-)

Comments

Muchun Song Feb. 8, 2022, 8:04 a.m. UTC | #1
On Fri, Feb 4, 2022 at 1:19 AM Matthew Wilcox (Oracle)
<willy@infradead.org> wrote:
>
> Instead of setting the page directly in struct page_vma_mapped_walk,
> use this helper to allow us to transition to a PFN approach in the
> next patch.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  include/linux/rmap.h    |  6 ++++++
>  kernel/events/uprobes.c |  2 +-
>  mm/damon/paddr.c        |  4 ++--
>  mm/ksm.c                |  2 +-
>  mm/migrate.c            |  2 +-
>  mm/page_idle.c          |  2 +-
>  mm/rmap.c               | 12 ++++++------
>  7 files changed, 18 insertions(+), 12 deletions(-)
>
> diff --git a/include/linux/rmap.h b/include/linux/rmap.h
> index e704b1a4c06c..003bb5775bb1 100644
> --- a/include/linux/rmap.h
> +++ b/include/linux/rmap.h
> @@ -213,6 +213,12 @@ struct page_vma_mapped_walk {
>         unsigned int flags;
>  };
>
> +static inline void pvmw_set_page(struct page_vma_mapped_walk *pvmw,
> +               struct page *page)
> +{
> +       pvmw->page = page;
> +}
> +

Hi Matthew,

How about the name pvmw_set_pfn_from_page()?
When I looked at your next patch, I'm feeling this helper
is used to set up the pfn from a page instead of setting up
the page.

Thanks.
Matthew Wilcox Feb. 8, 2022, 2:49 p.m. UTC | #2
On Tue, Feb 08, 2022 at 04:04:32PM +0800, Muchun Song wrote:
> How about the name pvmw_set_pfn_from_page()?
> When I looked at your next patch, I'm feeling this helper
> is used to set up the pfn from a page instead of setting up
> the page.

But it doesn't just set up the pfn, it also sets up the range and
the pgoff.

I wonder if we wouldn't be better off with this pattern:

	DEFINE_PAGE_VMA_WALK(pvmw, page, vma, address, flags);

... which wouldn't get used by KSM, but could be used by
most other callers.
Muchun Song Feb. 9, 2022, 3:11 a.m. UTC | #3
On Tue, Feb 8, 2022 at 10:49 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Tue, Feb 08, 2022 at 04:04:32PM +0800, Muchun Song wrote:
> > How about the name pvmw_set_pfn_from_page()?
> > When I looked at your next patch, I'm feeling this helper
> > is used to set up the pfn from a page instead of setting up
> > the page.
>
> But it doesn't just set up the pfn, it also sets up the range and
> the pgoff.
>
> I wonder if we wouldn't be better off with this pattern:
>
>         DEFINE_PAGE_VMA_WALK(pvmw, page, vma, address, flags);

The approach sounds good to me.  How about the following names?

        DEFINE_PAGE_VMA_WALK_FROM_PAGE(pvmw, page, vma, address, flags);

This is used for most users that want to walk the whole page.

        DEFINE_PAGE_VMA_WALK(pvmw, pfn, nr_pages, pgoff, vma, address, flags);

This is used for users that have no struct page or the user like
page_mapped_in_vma() which only wants to walk the part of
compound page.

Thanks.

>
> ... which wouldn't get used by KSM, but could be used by
> most other callers.
>
diff mbox series

Patch

diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index e704b1a4c06c..003bb5775bb1 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -213,6 +213,12 @@  struct page_vma_mapped_walk {
 	unsigned int flags;
 };
 
+static inline void pvmw_set_page(struct page_vma_mapped_walk *pvmw,
+		struct page *page)
+{
+	pvmw->page = page;
+}
+
 static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw)
 {
 	/* HugeTLB pte is set to the relevant page table entry without pte_mapped. */
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 6357c3580d07..5f74671b0066 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -156,13 +156,13 @@  static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct page_vma_mapped_walk pvmw = {
-		.page = compound_head(old_page),
 		.vma = vma,
 		.address = addr,
 	};
 	int err;
 	struct mmu_notifier_range range;
 
+	pvmw_set_page(&pvmw, compound_head(old_page));
 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, addr,
 				addr + PAGE_SIZE);
 
diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c
index 5e8244f65a1a..4e27d64abbb7 100644
--- a/mm/damon/paddr.c
+++ b/mm/damon/paddr.c
@@ -20,11 +20,11 @@  static bool __damon_pa_mkold(struct page *page, struct vm_area_struct *vma,
 		unsigned long addr, void *arg)
 {
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = addr,
 	};
 
+	pvmw_set_page(&pvmw, page);
 	while (page_vma_mapped_walk(&pvmw)) {
 		addr = pvmw.address;
 		if (pvmw.pte)
@@ -94,11 +94,11 @@  static bool __damon_pa_young(struct page *page, struct vm_area_struct *vma,
 {
 	struct damon_pa_access_chk_result *result = arg;
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = addr,
 	};
 
+	pvmw_set_page(&pvmw, page);
 	result->accessed = false;
 	result->page_sz = PAGE_SIZE;
 	while (page_vma_mapped_walk(&pvmw)) {
diff --git a/mm/ksm.c b/mm/ksm.c
index c20bd4d9a0d9..1639160c9e9a 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1035,13 +1035,13 @@  static int write_protect_page(struct vm_area_struct *vma, struct page *page,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 	};
 	int swapped;
 	int err = -EFAULT;
 	struct mmu_notifier_range range;
 
+	pvmw_set_page(&pvmw, page);
 	pvmw.address = page_address_in_vma(page, vma);
 	if (pvmw.address == -EFAULT)
 		goto out;
diff --git a/mm/migrate.c b/mm/migrate.c
index c7da064b4781..07464fd45925 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -177,7 +177,6 @@  static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
 				 unsigned long addr, void *old)
 {
 	struct page_vma_mapped_walk pvmw = {
-		.page = old,
 		.vma = vma,
 		.address = addr,
 		.flags = PVMW_SYNC | PVMW_MIGRATION,
@@ -187,6 +186,7 @@  static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
 	swp_entry_t entry;
 
 	VM_BUG_ON_PAGE(PageTail(page), page);
+	pvmw_set_page(&pvmw, old);
 	while (page_vma_mapped_walk(&pvmw)) {
 		if (PageKsm(page))
 			new = page;
diff --git a/mm/page_idle.c b/mm/page_idle.c
index edead6a8a5f9..20d35d720872 100644
--- a/mm/page_idle.c
+++ b/mm/page_idle.c
@@ -49,12 +49,12 @@  static bool page_idle_clear_pte_refs_one(struct page *page,
 					unsigned long addr, void *arg)
 {
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = addr,
 	};
 	bool referenced = false;
 
+	pvmw_set_page(&pvmw, page);
 	while (page_vma_mapped_walk(&pvmw)) {
 		addr = pvmw.address;
 		if (pvmw.pte) {
diff --git a/mm/rmap.c b/mm/rmap.c
index a531b64d53fa..fa8478372e94 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -803,12 +803,12 @@  static bool page_referenced_one(struct page *page, struct vm_area_struct *vma,
 {
 	struct page_referenced_arg *pra = arg;
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = address,
 	};
 	int referenced = 0;
 
+	pvmw_set_page(&pvmw, page);
 	while (page_vma_mapped_walk(&pvmw)) {
 		address = pvmw.address;
 
@@ -932,7 +932,6 @@  static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,
 			    unsigned long address, void *arg)
 {
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = address,
 		.flags = PVMW_SYNC,
@@ -940,6 +939,7 @@  static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,
 	struct mmu_notifier_range range;
 	int *cleaned = arg;
 
+	pvmw_set_page(&pvmw, page);
 	/*
 	 * We have to assume the worse case ie pmd for invalidation. Note that
 	 * the page can not be free from this function.
@@ -1423,7 +1423,6 @@  static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = address,
 	};
@@ -1433,6 +1432,7 @@  static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 	struct mmu_notifier_range range;
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 
+	pvmw_set_page(&pvmw, page);
 	/*
 	 * When racing against e.g. zap_pte_range() on another cpu,
 	 * in between its ptep_get_and_clear_full() and page_remove_rmap(),
@@ -1723,7 +1723,6 @@  static bool try_to_migrate_one(struct page *page, struct vm_area_struct *vma,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = address,
 	};
@@ -1733,6 +1732,7 @@  static bool try_to_migrate_one(struct page *page, struct vm_area_struct *vma,
 	struct mmu_notifier_range range;
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 
+	pvmw_set_page(&pvmw, page);
 	/*
 	 * When racing against e.g. zap_pte_range() on another cpu,
 	 * in between its ptep_get_and_clear_full() and page_remove_rmap(),
@@ -2003,11 +2003,11 @@  static bool page_mlock_one(struct page *page, struct vm_area_struct *vma,
 				 unsigned long address, void *unused)
 {
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = address,
 	};
 
+	pvmw_set_page(&pvmw, page);
 	/* An un-locked vma doesn't have any pages to lock, continue the scan */
 	if (!(vma->vm_flags & VM_LOCKED))
 		return true;
@@ -2078,7 +2078,6 @@  static bool page_make_device_exclusive_one(struct page *page,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct page_vma_mapped_walk pvmw = {
-		.page = page,
 		.vma = vma,
 		.address = address,
 	};
@@ -2090,6 +2089,7 @@  static bool page_make_device_exclusive_one(struct page *page,
 	swp_entry_t entry;
 	pte_t swp_pte;
 
+	pvmw_set_page(&pvmw, page);
 	mmu_notifier_range_init_owner(&range, MMU_NOTIFY_EXCLUSIVE, 0, vma,
 				      vma->vm_mm, address, min(vma->vm_end,
 				      address + page_size(page)), args->owner);