2021-09-15 18:16:51

by Peter Xu

[permalink] [raw]
Subject: [PATCH v4 0/4] mm: A few cleanup patches around zap, shmem and uffd

[Based on v5.14-rc1]

Hi, Andrew,

I dropped patch 5 and will do it later when it justifies itself better. Each
patch of this series now contains at least 1 R-b, would you consider merge it?

Thanks,

v4:
- Patch "mm: Drop first_index/last_index in zap_details"
- Put first_index and last_index into two lines [Liam]
- Pick up r-bs
- Drop patch 5 for future

v3:
- Patch "mm: Add zap_skip_check_mapping() helper"
- In zap_skip_check_mapping() check zap_mapping first [Alistair]
- Patch "mm: Add ZAP_FLAG_SKIP_SWAP and zap_flags":
- Fix English errors in commit message [David]
- Drop paragraph mentioning commit 22061a1ffabd in commit msg
- Set ZAP_FLAG_SKIP_SWAP for unmap_mapping_page() too
- Pick up r-bs

v2:
- Patch "mm: Clear vmf->pte after pte_unmap_same() returns"
- Remove one comment [David]
- Collect r-b for patch 2/3
- Rewrite the last two patches to drop ZAP_FLAG_CHECK_MAPPING, dropping
Alistair's r-b on patch 5 because it changed [David, Matthew]

===== v1 cover letter =====

I picked up these patches from uffd-wp v5 series here:

https://lore.kernel.org/lkml/[email protected]/

IMHO all of them are very nice cleanups to existing code already, they're all
small and self-contained. They'll be needed by uffd-wp coming series. I would
appreciate if they can be accepted earlier, so as to not carry them over always
with the uffd-wp series.

I removed some CC from the uffd-wp v5 series to reduce the noise, and added a
few more into it.

Reviews are greatly welcomed, thanks.

Peter Xu (4):
mm/shmem: Unconditionally set pte dirty in mfill_atomic_install_pte
mm: Clear vmf->pte after pte_unmap_same() returns
mm: Drop first_index/last_index in zap_details
mm: Add zap_skip_check_mapping() helper

include/linux/mm.h | 18 ++++++++++--
mm/memory.c | 72 +++++++++++++++++++---------------------------
mm/shmem.c | 1 -
mm/userfaultfd.c | 3 +-
4 files changed, 46 insertions(+), 48 deletions(-)

--
2.31.1


2021-09-15 18:17:29

by Peter Xu

[permalink] [raw]
Subject: [PATCH v4 2/4] mm: Clear vmf->pte after pte_unmap_same() returns

pte_unmap_same() will always unmap the pte pointer. After the unmap, vmf->pte
will not be valid any more, we should clear it.

It was safe only because no one is accessing vmf->pte after pte_unmap_same()
returns, since the only caller of pte_unmap_same() (so far) is do_swap_page(),
where vmf->pte will in most cases be overwritten very soon.

Directly pass in vmf into pte_unmap_same() and then we can also avoid the long
parameter list too, which should be a nice cleanup.

Reviewed-by: Miaohe Lin <[email protected]>
Reviewed-by: David Hildenbrand <[email protected]>
Reviewed-by: Liam Howlett <[email protected]>
Signed-off-by: Peter Xu <[email protected]>
---
mm/memory.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 25fc46e87214..7b095f07c4ef 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2724,19 +2724,19 @@ EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
* proceeding (but do_wp_page is only called after already making such a check;
* and do_anonymous_page can safely check later on).
*/
-static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
- pte_t *page_table, pte_t orig_pte)
+static inline int pte_unmap_same(struct vm_fault *vmf)
{
int same = 1;
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION)
if (sizeof(pte_t) > sizeof(unsigned long)) {
- spinlock_t *ptl = pte_lockptr(mm, pmd);
+ spinlock_t *ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
spin_lock(ptl);
- same = pte_same(*page_table, orig_pte);
+ same = pte_same(*vmf->pte, vmf->orig_pte);
spin_unlock(ptl);
}
#endif
- pte_unmap(page_table);
+ pte_unmap(vmf->pte);
+ vmf->pte = NULL;
return same;
}

@@ -3487,7 +3487,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
vm_fault_t ret = 0;
void *shadow = NULL;

- if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
+ if (!pte_unmap_same(vmf))
goto out;

entry = pte_to_swp_entry(vmf->orig_pte);
--
2.31.1

2021-09-15 18:18:31

by Peter Xu

[permalink] [raw]
Subject: [PATCH v4 3/4] mm: Drop first_index/last_index in zap_details

The first_index/last_index parameters in zap_details are actually only used in
unmap_mapping_range_tree(). At the meantime, this function is only called by
unmap_mapping_pages() once. Instead of passing these two variables through the
whole stack of page zapping code, remove them from zap_details and let them
simply be parameters of unmap_mapping_range_tree(), which is inlined.

Reviewed-by: Alistair Popple <[email protected]>
Reviewed-by: David Hildenbrand <[email protected]>
Reviewed-by: Liam Howlett <[email protected]>
Signed-off-by: Peter Xu <[email protected]>
---
include/linux/mm.h | 2 --
mm/memory.c | 31 ++++++++++++++++++-------------
2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 73a52aba448f..d1126f731221 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1722,8 +1722,6 @@ extern void user_shm_unlock(size_t, struct ucounts *);
*/
struct zap_details {
struct address_space *check_mapping; /* Check page->mapping if set */
- pgoff_t first_index; /* Lowest page->index to unmap */
- pgoff_t last_index; /* Highest page->index to unmap */
struct page *single_page; /* Locked page to be unmapped */
};

diff --git a/mm/memory.c b/mm/memory.c
index 7b095f07c4ef..a7e427177817 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3321,20 +3321,20 @@ static void unmap_mapping_range_vma(struct vm_area_struct *vma,
}

static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
+ pgoff_t first_index,
+ pgoff_t last_index,
struct zap_details *details)
{
struct vm_area_struct *vma;
pgoff_t vba, vea, zba, zea;

- vma_interval_tree_foreach(vma, root,
- details->first_index, details->last_index) {
-
+ vma_interval_tree_foreach(vma, root, first_index, last_index) {
vba = vma->vm_pgoff;
vea = vba + vma_pages(vma) - 1;
- zba = details->first_index;
+ zba = first_index;
if (zba < vba)
zba = vba;
- zea = details->last_index;
+ zea = last_index;
if (zea > vea)
zea = vea;

@@ -3360,18 +3360,22 @@ void unmap_mapping_page(struct page *page)
{
struct address_space *mapping = page->mapping;
struct zap_details details = { };
+ pgoff_t first_index;
+ pgoff_t last_index;

VM_BUG_ON(!PageLocked(page));
VM_BUG_ON(PageTail(page));

+ first_index = page->index;
+ last_index = page->index + thp_nr_pages(page) - 1;
+
details.check_mapping = mapping;
- details.first_index = page->index;
- details.last_index = page->index + thp_nr_pages(page) - 1;
details.single_page = page;

i_mmap_lock_write(mapping);
if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
- unmap_mapping_range_tree(&mapping->i_mmap, &details);
+ unmap_mapping_range_tree(&mapping->i_mmap, first_index,
+ last_index, &details);
i_mmap_unlock_write(mapping);
}

@@ -3391,16 +3395,17 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
pgoff_t nr, bool even_cows)
{
struct zap_details details = { };
+ pgoff_t first_index = start;
+ pgoff_t last_index = start + nr - 1;

details.check_mapping = even_cows ? NULL : mapping;
- details.first_index = start;
- details.last_index = start + nr - 1;
- if (details.last_index < details.first_index)
- details.last_index = ULONG_MAX;
+ if (last_index < first_index)
+ last_index = ULONG_MAX;

i_mmap_lock_write(mapping);
if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
- unmap_mapping_range_tree(&mapping->i_mmap, &details);
+ unmap_mapping_range_tree(&mapping->i_mmap, first_index,
+ last_index, &details);
i_mmap_unlock_write(mapping);
}

--
2.31.1

2021-09-15 18:20:59

by Peter Xu

[permalink] [raw]
Subject: [PATCH v4 4/4] mm: Add zap_skip_check_mapping() helper

Use the helper for the checks. Rename "check_mapping" into "zap_mapping"
because "check_mapping" looks like a bool but in fact it stores the mapping
itself. When it's set, we check the mapping (it must be non-NULL). When it's
cleared we skip the check, which works like the old way.

Move the duplicated comments to the helper too.

Reviewed-by: Alistair Popple <[email protected]>
Signed-off-by: Peter Xu <[email protected]>
---
include/linux/mm.h | 16 +++++++++++++++-
mm/memory.c | 29 ++++++-----------------------
2 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index d1126f731221..ed44f31615d9 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1721,10 +1721,24 @@ extern void user_shm_unlock(size_t, struct ucounts *);
* Parameter block passed down to zap_pte_range in exceptional cases.
*/
struct zap_details {
- struct address_space *check_mapping; /* Check page->mapping if set */
+ struct address_space *zap_mapping; /* Check page->mapping if set */
struct page *single_page; /* Locked page to be unmapped */
};

+/*
+ * We set details->zap_mappings when we want to unmap shared but keep private
+ * pages. Return true if skip zapping this page, false otherwise.
+ */
+static inline bool
+zap_skip_check_mapping(struct zap_details *details, struct page *page)
+{
+ if (!details || !page)
+ return false;
+
+ return details->zap_mapping &&
+ (details->zap_mapping != page_rmapping(page));
+}
+
struct page *vm_normal_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,
diff --git a/mm/memory.c b/mm/memory.c
index a7e427177817..8db8ce0ca6ce 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1333,16 +1333,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
struct page *page;

page = vm_normal_page(vma, addr, ptent);
- if (unlikely(details) && page) {
- /*
- * unmap_shared_mapping_pages() wants to
- * invalidate cache without truncating:
- * unmap shared but keep private pages.
- */
- if (details->check_mapping &&
- details->check_mapping != page_rmapping(page))
- continue;
- }
+ if (unlikely(zap_skip_check_mapping(details, page)))
+ continue;
ptent = ptep_get_and_clear_full(mm, addr, pte,
tlb->fullmm);
tlb_remove_tlb_entry(tlb, pte, addr);
@@ -1375,17 +1367,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
is_device_exclusive_entry(entry)) {
struct page *page = pfn_swap_entry_to_page(entry);

- if (unlikely(details && details->check_mapping)) {
- /*
- * unmap_shared_mapping_pages() wants to
- * invalidate cache without truncating:
- * unmap shared but keep private pages.
- */
- if (details->check_mapping !=
- page_rmapping(page))
- continue;
- }
-
+ if (unlikely(zap_skip_check_mapping(details, page)))
+ continue;
pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
rss[mm_counter(page)]--;

@@ -3369,7 +3352,7 @@ void unmap_mapping_page(struct page *page)
first_index = page->index;
last_index = page->index + thp_nr_pages(page) - 1;

- details.check_mapping = mapping;
+ details.zap_mapping = mapping;
details.single_page = page;

i_mmap_lock_write(mapping);
@@ -3398,7 +3381,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
pgoff_t first_index = start;
pgoff_t last_index = start + nr - 1;

- details.check_mapping = even_cows ? NULL : mapping;
+ details.zap_mapping = even_cows ? NULL : mapping;
if (last_index < first_index)
last_index = ULONG_MAX;

--
2.31.1

2021-09-24 04:00:02

by Hugh Dickins

[permalink] [raw]
Subject: Re: [PATCH v4 2/4] mm: Clear vmf->pte after pte_unmap_same() returns

On Wed, 15 Sep 2021, Peter Xu wrote:

> pte_unmap_same() will always unmap the pte pointer. After the unmap, vmf->pte
> will not be valid any more, we should clear it.
>
> It was safe only because no one is accessing vmf->pte after pte_unmap_same()
> returns, since the only caller of pte_unmap_same() (so far) is do_swap_page(),
> where vmf->pte will in most cases be overwritten very soon.
>
> Directly pass in vmf into pte_unmap_same() and then we can also avoid the long
> parameter list too, which should be a nice cleanup.
>
> Reviewed-by: Miaohe Lin <[email protected]>
> Reviewed-by: David Hildenbrand <[email protected]>
> Reviewed-by: Liam Howlett <[email protected]>
> Signed-off-by: Peter Xu <[email protected]>

This one seems fine, thanks.
Acked-by: Hugh Dickins <[email protected]>

> ---
> mm/memory.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 25fc46e87214..7b095f07c4ef 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2724,19 +2724,19 @@ EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
> * proceeding (but do_wp_page is only called after already making such a check;
> * and do_anonymous_page can safely check later on).
> */
> -static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
> - pte_t *page_table, pte_t orig_pte)
> +static inline int pte_unmap_same(struct vm_fault *vmf)
> {
> int same = 1;
> #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION)
> if (sizeof(pte_t) > sizeof(unsigned long)) {
> - spinlock_t *ptl = pte_lockptr(mm, pmd);
> + spinlock_t *ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
> spin_lock(ptl);
> - same = pte_same(*page_table, orig_pte);
> + same = pte_same(*vmf->pte, vmf->orig_pte);
> spin_unlock(ptl);
> }
> #endif
> - pte_unmap(page_table);
> + pte_unmap(vmf->pte);
> + vmf->pte = NULL;
> return same;
> }
>
> @@ -3487,7 +3487,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> vm_fault_t ret = 0;
> void *shadow = NULL;
>
> - if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
> + if (!pte_unmap_same(vmf))
> goto out;
>
> entry = pte_to_swp_entry(vmf->orig_pte);
> --
> 2.31.1

2021-09-24 04:16:59

by Hugh Dickins

[permalink] [raw]
Subject: Re: [PATCH v4 3/4] mm: Drop first_index/last_index in zap_details

On Wed, 15 Sep 2021, Peter Xu wrote:

> The first_index/last_index parameters in zap_details are actually only used in
> unmap_mapping_range_tree(). At the meantime, this function is only called by
> unmap_mapping_pages() once. Instead of passing these two variables through the
> whole stack of page zapping code, remove them from zap_details and let them
> simply be parameters of unmap_mapping_range_tree(), which is inlined.
>
> Reviewed-by: Alistair Popple <[email protected]>
> Reviewed-by: David Hildenbrand <[email protected]>
> Reviewed-by: Liam Howlett <[email protected]>
> Signed-off-by: Peter Xu <[email protected]>

And this one is fine too, thanks. I don't know whether it saves anything
(ah yes, with args in registers not on the stack, should save a little),
but it's helpful to limit the scope of those indices.

You may wonder how they came to be in zap_details: that dates from the
days of remap_file_pages(): nonlinear vmas, in which the zapper needed
to check each pte_file()'s offset against first and last index, to
decide whether to zap or not. They should have been removed in 4.0.

Acked-by: Hugh Dickins <[email protected]>

> ---
> include/linux/mm.h | 2 --
> mm/memory.c | 31 ++++++++++++++++++-------------
> 2 files changed, 18 insertions(+), 15 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 73a52aba448f..d1126f731221 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1722,8 +1722,6 @@ extern void user_shm_unlock(size_t, struct ucounts *);
> */
> struct zap_details {
> struct address_space *check_mapping; /* Check page->mapping if set */
> - pgoff_t first_index; /* Lowest page->index to unmap */
> - pgoff_t last_index; /* Highest page->index to unmap */
> struct page *single_page; /* Locked page to be unmapped */
> };
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 7b095f07c4ef..a7e427177817 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -3321,20 +3321,20 @@ static void unmap_mapping_range_vma(struct vm_area_struct *vma,
> }
>
> static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
> + pgoff_t first_index,
> + pgoff_t last_index,
> struct zap_details *details)
> {
> struct vm_area_struct *vma;
> pgoff_t vba, vea, zba, zea;
>
> - vma_interval_tree_foreach(vma, root,
> - details->first_index, details->last_index) {
> -
> + vma_interval_tree_foreach(vma, root, first_index, last_index) {
> vba = vma->vm_pgoff;
> vea = vba + vma_pages(vma) - 1;
> - zba = details->first_index;
> + zba = first_index;
> if (zba < vba)
> zba = vba;
> - zea = details->last_index;
> + zea = last_index;
> if (zea > vea)
> zea = vea;
>
> @@ -3360,18 +3360,22 @@ void unmap_mapping_page(struct page *page)
> {
> struct address_space *mapping = page->mapping;
> struct zap_details details = { };
> + pgoff_t first_index;
> + pgoff_t last_index;
>
> VM_BUG_ON(!PageLocked(page));
> VM_BUG_ON(PageTail(page));
>
> + first_index = page->index;
> + last_index = page->index + thp_nr_pages(page) - 1;
> +
> details.check_mapping = mapping;
> - details.first_index = page->index;
> - details.last_index = page->index + thp_nr_pages(page) - 1;
> details.single_page = page;
>
> i_mmap_lock_write(mapping);
> if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
> - unmap_mapping_range_tree(&mapping->i_mmap, &details);
> + unmap_mapping_range_tree(&mapping->i_mmap, first_index,
> + last_index, &details);
> i_mmap_unlock_write(mapping);
> }
>
> @@ -3391,16 +3395,17 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
> pgoff_t nr, bool even_cows)
> {
> struct zap_details details = { };
> + pgoff_t first_index = start;
> + pgoff_t last_index = start + nr - 1;
>
> details.check_mapping = even_cows ? NULL : mapping;
> - details.first_index = start;
> - details.last_index = start + nr - 1;
> - if (details.last_index < details.first_index)
> - details.last_index = ULONG_MAX;
> + if (last_index < first_index)
> + last_index = ULONG_MAX;
>
> i_mmap_lock_write(mapping);
> if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
> - unmap_mapping_range_tree(&mapping->i_mmap, &details);
> + unmap_mapping_range_tree(&mapping->i_mmap, first_index,
> + last_index, &details);
> i_mmap_unlock_write(mapping);
> }
>
> --
> 2.31.1

2021-09-24 04:47:24

by Hugh Dickins

[permalink] [raw]
Subject: Re: [PATCH v4 4/4] mm: Add zap_skip_check_mapping() helper

On Wed, 15 Sep 2021, Peter Xu wrote:

> Use the helper for the checks. Rename "check_mapping" into "zap_mapping"
> because "check_mapping" looks like a bool but in fact it stores the mapping
> itself. When it's set, we check the mapping (it must be non-NULL). When it's
> cleared we skip the check, which works like the old way.
>
> Move the duplicated comments to the helper too.
>
> Reviewed-by: Alistair Popple <[email protected]>
> Signed-off-by: Peter Xu <[email protected]>

Again, I won't NAK, but I have no enthusiasm for this at all: our tastes
clearly differ. I don't find the new name helpful, I don't find the
separated "helper" helpful, and you have hidden the helpful comment
(but I'd be on firmer ground if the unmap_shared_mapping_pages() it
referred to had ever existed! perhaps it was in an intermediate tree).

But then I would feel this way, wouldn't I?
See dd9fd0e03de ("[PATCH] rmap: nonlinear truncation") in
//git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git

I'm glad to see that you have dropped 5/5 for now:
I was not keen on that one either.

Hugh

> ---
> include/linux/mm.h | 16 +++++++++++++++-
> mm/memory.c | 29 ++++++-----------------------
> 2 files changed, 21 insertions(+), 24 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index d1126f731221..ed44f31615d9 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1721,10 +1721,24 @@ extern void user_shm_unlock(size_t, struct ucounts *);
> * Parameter block passed down to zap_pte_range in exceptional cases.
> */
> struct zap_details {
> - struct address_space *check_mapping; /* Check page->mapping if set */
> + struct address_space *zap_mapping; /* Check page->mapping if set */
> struct page *single_page; /* Locked page to be unmapped */
> };
>
> +/*
> + * We set details->zap_mappings when we want to unmap shared but keep private
> + * pages. Return true if skip zapping this page, false otherwise.
> + */
> +static inline bool
> +zap_skip_check_mapping(struct zap_details *details, struct page *page)
> +{
> + if (!details || !page)
> + return false;
> +
> + return details->zap_mapping &&
> + (details->zap_mapping != page_rmapping(page));
> +}
> +
> struct page *vm_normal_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,
> diff --git a/mm/memory.c b/mm/memory.c
> index a7e427177817..8db8ce0ca6ce 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -1333,16 +1333,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
> struct page *page;
>
> page = vm_normal_page(vma, addr, ptent);
> - if (unlikely(details) && page) {
> - /*
> - * unmap_shared_mapping_pages() wants to
> - * invalidate cache without truncating:
> - * unmap shared but keep private pages.
> - */
> - if (details->check_mapping &&
> - details->check_mapping != page_rmapping(page))
> - continue;
> - }
> + if (unlikely(zap_skip_check_mapping(details, page)))
> + continue;
> ptent = ptep_get_and_clear_full(mm, addr, pte,
> tlb->fullmm);
> tlb_remove_tlb_entry(tlb, pte, addr);
> @@ -1375,17 +1367,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
> is_device_exclusive_entry(entry)) {
> struct page *page = pfn_swap_entry_to_page(entry);
>
> - if (unlikely(details && details->check_mapping)) {
> - /*
> - * unmap_shared_mapping_pages() wants to
> - * invalidate cache without truncating:
> - * unmap shared but keep private pages.
> - */
> - if (details->check_mapping !=
> - page_rmapping(page))
> - continue;
> - }
> -
> + if (unlikely(zap_skip_check_mapping(details, page)))
> + continue;
> pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
> rss[mm_counter(page)]--;
>
> @@ -3369,7 +3352,7 @@ void unmap_mapping_page(struct page *page)
> first_index = page->index;
> last_index = page->index + thp_nr_pages(page) - 1;
>
> - details.check_mapping = mapping;
> + details.zap_mapping = mapping;
> details.single_page = page;
>
> i_mmap_lock_write(mapping);
> @@ -3398,7 +3381,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
> pgoff_t first_index = start;
> pgoff_t last_index = start + nr - 1;
>
> - details.check_mapping = even_cows ? NULL : mapping;
> + details.zap_mapping = even_cows ? NULL : mapping;
> if (last_index < first_index)
> last_index = ULONG_MAX;
>
> --
> 2.31.1

2021-09-24 11:47:16

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH v4 4/4] mm: Add zap_skip_check_mapping() helper

On 15.09.21 20:15, Peter Xu wrote:
> Use the helper for the checks. Rename "check_mapping" into "zap_mapping"
> because "check_mapping" looks like a bool but in fact it stores the mapping
> itself. When it's set, we check the mapping (it must be non-NULL). When it's
> cleared we skip the check, which works like the old way.
>
> Move the duplicated comments to the helper too.
>
> Reviewed-by: Alistair Popple <[email protected]>
> Signed-off-by: Peter Xu <[email protected]>
> ---
> include/linux/mm.h | 16 +++++++++++++++-
> mm/memory.c | 29 ++++++-----------------------
> 2 files changed, 21 insertions(+), 24 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index d1126f731221..ed44f31615d9 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1721,10 +1721,24 @@ extern void user_shm_unlock(size_t, struct ucounts *);
> * Parameter block passed down to zap_pte_range in exceptional cases.
> */
> struct zap_details {
> - struct address_space *check_mapping; /* Check page->mapping if set */
> + struct address_space *zap_mapping; /* Check page->mapping if set */
> struct page *single_page; /* Locked page to be unmapped */
> };
>
> +/*
> + * We set details->zap_mappings when we want to unmap shared but keep private
> + * pages. Return true if skip zapping this page, false otherwise.
> + */
> +static inline bool
> +zap_skip_check_mapping(struct zap_details *details, struct page *page)

I agree with Hugh that the name of this helper is suboptimal.

What about inverting the conditions and getting

static inline bool should_zap_page()
{
...
}

The calling code is then

if (unlikely(!should_zap_page(details, page)))
continue;


I don't really like renaming "zap_mapping", again, because it's
contained within "struct zap_details" already.

Factoring this out into a helper sounds like a good idea to me. Clear
case of code de-duplication.

--
Thanks,

David / dhildenb