[PATCH v3 1/3] mm: add get_and_clear_ptes() and clear_ptes()

Dev Jain posted 3 patches 2 months, 2 weeks ago
There is a newer version of this series
[PATCH v3 1/3] mm: add get_and_clear_ptes() and clear_ptes()
Posted by Dev Jain 2 months, 2 weeks ago
From: David Hildenbrand <david@redhat.com>

From: David Hildenbrand <david@redhat.com>

Let's add variants to be used where "full" does not apply -- which will
be the majority of cases in the future. "full" really only applies if
we are about to tear down a full MM.

Use get_and_clear_ptes() in existing code, clear_ptes() users will
be added next.

Should we make these inline functions instead and add separate docs?
Probably not worth it for now.

Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
 arch/arm64/mm/mmu.c     | 2 +-
 include/linux/pgtable.h | 6 ++++++
 mm/mremap.c             | 2 +-
 mm/rmap.c               | 2 +-
 4 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index abd9725796e9..20a89ab97dc5 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1528,7 +1528,7 @@ early_initcall(prevent_bootmem_remove_init);
 pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t *ptep, unsigned int nr)
 {
-	pte_t pte = get_and_clear_full_ptes(vma->vm_mm, addr, ptep, nr, /* full = */ 0);
+	pte_t pte = get_and_clear_ptes(vma->vm_mm, addr, ptep, nr);
 
 	if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
 		/*
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index e3b99920be05..e45986b54277 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -736,6 +736,9 @@ static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm,
 }
 #endif
 
+#define get_and_clear_ptes(_mm, _addr, _ptep, _nr) \
+	get_and_clear_full_ptes(_mm, _addr, _ptep, _nr, 0)
+
 #ifndef clear_full_ptes
 /**
  * clear_full_ptes - Clear present PTEs that map consecutive pages of the same
@@ -768,6 +771,9 @@ static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr,
 }
 #endif
 
+#define clear_ptes(_mm, _addr, _ptep, _nr) \
+	clear_full_ptes(_mm, _addr, _ptep, _nr, 0)
+
 /*
  * If two threads concurrently fault at the same page, the thread that
  * won the race updates the PTE and its local TLB/Cache. The other thread
diff --git a/mm/mremap.c b/mm/mremap.c
index ac39845e9718..677a4d744df9 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -280,7 +280,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
 							 old_pte, max_nr_ptes);
 			force_flush = true;
 		}
-		pte = get_and_clear_full_ptes(mm, old_addr, old_ptep, nr_ptes, 0);
+		pte = get_and_clear_ptes(mm, old_addr, old_ptep, nr_ptes);
 		pte = move_pte(pte, old_addr, new_addr);
 		pte = move_soft_dirty_pte(pte);
 
diff --git a/mm/rmap.c b/mm/rmap.c
index f93ce27132ab..568198e9efc2 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2036,7 +2036,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			flush_cache_range(vma, address, end_addr);
 
 			/* Nuke the page table entry. */
-			pteval = get_and_clear_full_ptes(mm, address, pvmw.pte, nr_pages, 0);
+			pteval = get_and_clear_ptes(mm, address, pvmw.pte, nr_pages);
 			/*
 			 * We clear the PTE but do not flush so potentially
 			 * a remote CPU could still be writing to the folio.
-- 
2.30.2
Re: [PATCH v3 1/3] mm: add get_and_clear_ptes() and clear_ptes()
Posted by Baolin Wang 2 months, 2 weeks ago

On 2025/7/22 23:05, Dev Jain wrote:
> From: David Hildenbrand <david@redhat.com>
> 
> From: David Hildenbrand <david@redhat.com>
> 
> Let's add variants to be used where "full" does not apply -- which will
> be the majority of cases in the future. "full" really only applies if
> we are about to tear down a full MM.
> 
> Use get_and_clear_ptes() in existing code, clear_ptes() users will
> be added next.
> 
> Should we make these inline functions instead and add separate docs?
> Probably not worth it for now.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>
> Signed-off-by: Dev Jain <dev.jain@arm.com>

LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>

> ---
>   arch/arm64/mm/mmu.c     | 2 +-
>   include/linux/pgtable.h | 6 ++++++
>   mm/mremap.c             | 2 +-
>   mm/rmap.c               | 2 +-
>   4 files changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index abd9725796e9..20a89ab97dc5 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1528,7 +1528,7 @@ early_initcall(prevent_bootmem_remove_init);
>   pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
>   			     pte_t *ptep, unsigned int nr)
>   {
> -	pte_t pte = get_and_clear_full_ptes(vma->vm_mm, addr, ptep, nr, /* full = */ 0);
> +	pte_t pte = get_and_clear_ptes(vma->vm_mm, addr, ptep, nr);
>   
>   	if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
>   		/*
> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
> index e3b99920be05..e45986b54277 100644
> --- a/include/linux/pgtable.h
> +++ b/include/linux/pgtable.h
> @@ -736,6 +736,9 @@ static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm,
>   }
>   #endif
>   
> +#define get_and_clear_ptes(_mm, _addr, _ptep, _nr) \
> +	get_and_clear_full_ptes(_mm, _addr, _ptep, _nr, 0)
> +
>   #ifndef clear_full_ptes
>   /**
>    * clear_full_ptes - Clear present PTEs that map consecutive pages of the same
> @@ -768,6 +771,9 @@ static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr,
>   }
>   #endif
>   
> +#define clear_ptes(_mm, _addr, _ptep, _nr) \
> +	clear_full_ptes(_mm, _addr, _ptep, _nr, 0)
> +
>   /*
>    * If two threads concurrently fault at the same page, the thread that
>    * won the race updates the PTE and its local TLB/Cache. The other thread
> diff --git a/mm/mremap.c b/mm/mremap.c
> index ac39845e9718..677a4d744df9 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -280,7 +280,7 @@ static int move_ptes(struct pagetable_move_control *pmc,
>   							 old_pte, max_nr_ptes);
>   			force_flush = true;
>   		}
> -		pte = get_and_clear_full_ptes(mm, old_addr, old_ptep, nr_ptes, 0);
> +		pte = get_and_clear_ptes(mm, old_addr, old_ptep, nr_ptes);
>   		pte = move_pte(pte, old_addr, new_addr);
>   		pte = move_soft_dirty_pte(pte);
>   
> diff --git a/mm/rmap.c b/mm/rmap.c
> index f93ce27132ab..568198e9efc2 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2036,7 +2036,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>   			flush_cache_range(vma, address, end_addr);
>   
>   			/* Nuke the page table entry. */
> -			pteval = get_and_clear_full_ptes(mm, address, pvmw.pte, nr_pages, 0);
> +			pteval = get_and_clear_ptes(mm, address, pvmw.pte, nr_pages);
>   			/*
>   			 * We clear the PTE but do not flush so potentially
>   			 * a remote CPU could still be writing to the folio.
Re: [PATCH v3 1/3] mm: add get_and_clear_ptes() and clear_ptes()
Posted by Andrew Morton 2 months, 2 weeks ago
On Tue, 22 Jul 2025 20:35:57 +0530 Dev Jain <dev.jain@arm.com> wrote:

> Let's add variants to be used where "full" does not apply -- which will
> be the majority of cases in the future. "full" really only applies if
> we are about to tear down a full MM.
> 
> Use get_and_clear_ptes() in existing code, clear_ptes() users will
> be added next.
> 
> Should we make these inline functions instead and add separate docs?

inlined functions would be preferable.

> Probably not worth it for now.

Well, as David has called for a v4 I suggest you make that change.

Thanks, I'll queue the series in mm-new for a bit of testing.
Re: [PATCH v3 1/3] mm: add get_and_clear_ptes() and clear_ptes()
Posted by Dev Jain 2 months, 2 weeks ago
On 23/07/25 2:17 am, Andrew Morton wrote:
> On Tue, 22 Jul 2025 20:35:57 +0530 Dev Jain <dev.jain@arm.com> wrote:
>
>> Let's add variants to be used where "full" does not apply -- which will
>> be the majority of cases in the future. "full" really only applies if
>> we are about to tear down a full MM.
>>
>> Use get_and_clear_ptes() in existing code, clear_ptes() users will
>> be added next.
>>
>> Should we make these inline functions instead and add separate docs?
> inlined functions would be preferable.
>
>> Probably not worth it for now.
> Well, as David has called for a v4 I suggest you make that change.
>
> Thanks, I'll queue the series in mm-new for a bit of testing.

Thanks. I'll post v4 later in the day.