diff mbox series

[v3,3/4] mm/khugepaged: unify collapse pmd clear, flush and free

Message ID 20220126060514.1574935-4-pasha.tatashin@soleen.com (mailing list archive)
State New
Headers show
Series page table check fixes and cleanups | expand

Commit Message

Pasha Tatashin Jan. 26, 2022, 6:05 a.m. UTC
Unify the code that flushes, clears pmd entry, and frees the PTE table
level into a new function collapse_and_free_pmd().

This clean-up is useful as in the next patch we will add another call to
this function to iterate through PTE prior to freeing the level for page
table check.

Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
---
 mm/khugepaged.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

Comments

David Rientjes Jan. 26, 2022, 6:34 a.m. UTC | #1
On Wed, 26 Jan 2022, Pasha Tatashin wrote:

> Unify the code that flushes, clears pmd entry, and frees the PTE table
> level into a new function collapse_and_free_pmd().
> 
> This clean-up is useful as in the next patch we will add another call to
> this function to iterate through PTE prior to freeing the level for page
> table check.
> 
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>

Acked-by: David Rientjes <rientjes@google.com>

One nit below.

> ---
>  mm/khugepaged.c | 32 ++++++++++++++++----------------
>  1 file changed, 16 insertions(+), 16 deletions(-)
> 
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 35f14d0a00a6..440112355ffe 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -1416,6 +1416,17 @@ static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
>  	return 0;
>  }
>  
> +static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
> +				  unsigned long addr, pmd_t *pmdp)
> +{
> +	spinlock_t *ptl = pmd_lock(vma->vm_mm, pmdp);
> +	pmd_t pmd = pmdp_collapse_flush(vma, addr, pmdp);
> +
> +	spin_unlock(ptl);

No strong objection, but I think the typical style would be to declare the 
local variables separately and avoid mixing the code, especially in cases 
when taking locks (and not just initializing the local variables).

> +	mm_dec_nr_ptes(mm);
> +	pte_free(mm, pmd_pgtable(pmd));
> +}
> +
>  /**
>   * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
>   * address haddr.
> @@ -1433,7 +1444,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
>  	struct vm_area_struct *vma = find_vma(mm, haddr);
>  	struct page *hpage;
>  	pte_t *start_pte, *pte;
> -	pmd_t *pmd, _pmd;
> +	pmd_t *pmd;
>  	spinlock_t *ptl;
>  	int count = 0;
>  	int i;
> @@ -1509,12 +1520,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
>  	}
>  
>  	/* step 4: collapse pmd */
> -	ptl = pmd_lock(vma->vm_mm, pmd);
> -	_pmd = pmdp_collapse_flush(vma, haddr, pmd);
> -	spin_unlock(ptl);
> -	mm_dec_nr_ptes(mm);
> -	pte_free(mm, pmd_pgtable(_pmd));
> -
> +	collapse_and_free_pmd(mm, vma, haddr, pmd);
>  drop_hpage:
>  	unlock_page(hpage);
>  	put_page(hpage);
> @@ -1552,7 +1558,7 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
>  	struct vm_area_struct *vma;
>  	struct mm_struct *mm;
>  	unsigned long addr;
> -	pmd_t *pmd, _pmd;
> +	pmd_t *pmd;
>  
>  	i_mmap_lock_write(mapping);
>  	vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
> @@ -1591,14 +1597,8 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
>  		 * reverse order. Trylock is a way to avoid deadlock.
>  		 */
>  		if (mmap_write_trylock(mm)) {
> -			if (!khugepaged_test_exit(mm)) {
> -				spinlock_t *ptl = pmd_lock(mm, pmd);
> -				/* assume page table is clear */
> -				_pmd = pmdp_collapse_flush(vma, addr, pmd);
> -				spin_unlock(ptl);
> -				mm_dec_nr_ptes(mm);
> -				pte_free(mm, pmd_pgtable(_pmd));
> -			}
> +			if (!khugepaged_test_exit(mm))
> +				collapse_and_free_pmd(mm, vma, addr, pmd);
>  			mmap_write_unlock(mm);
>  		} else {
>  			/* Try again later */
> -- 
> 2.35.0.rc0.227.g00780c9af4-goog
> 
>
Pasha Tatashin Jan. 26, 2022, 12:34 p.m. UTC | #2
On Wed, Jan 26, 2022 at 1:34 AM David Rientjes <rientjes@google.com> wrote:
>
> On Wed, 26 Jan 2022, Pasha Tatashin wrote:
>
> > Unify the code that flushes, clears pmd entry, and frees the PTE table
> > level into a new function collapse_and_free_pmd().
> >
> > This clean-up is useful as in the next patch we will add another call to
> > this function to iterate through PTE prior to freeing the level for page
> > table check.
> >
> > Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
>
> Acked-by: David Rientjes <rientjes@google.com>

Thank you, David.

>
> One nit below.
>
> > ---
> >  mm/khugepaged.c | 32 ++++++++++++++++----------------
> >  1 file changed, 16 insertions(+), 16 deletions(-)
> >
> > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > index 35f14d0a00a6..440112355ffe 100644
> > --- a/mm/khugepaged.c
> > +++ b/mm/khugepaged.c
> > @@ -1416,6 +1416,17 @@ static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
> >       return 0;
> >  }
> >
> > +static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
> > +                               unsigned long addr, pmd_t *pmdp)
> > +{
> > +     spinlock_t *ptl = pmd_lock(vma->vm_mm, pmdp);
> > +     pmd_t pmd = pmdp_collapse_flush(vma, addr, pmdp);
> > +
> > +     spin_unlock(ptl);
>
> No strong objection, but I think the typical style would be to declare the
> local variables separately and avoid mixing the code, especially in cases
> when taking locks (and not just initializing the local variables).

I will address this in the next revision.

Thank you,
Pasha
diff mbox series

Patch

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 35f14d0a00a6..440112355ffe 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1416,6 +1416,17 @@  static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
 	return 0;
 }
 
+static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
+				  unsigned long addr, pmd_t *pmdp)
+{
+	spinlock_t *ptl = pmd_lock(vma->vm_mm, pmdp);
+	pmd_t pmd = pmdp_collapse_flush(vma, addr, pmdp);
+
+	spin_unlock(ptl);
+	mm_dec_nr_ptes(mm);
+	pte_free(mm, pmd_pgtable(pmd));
+}
+
 /**
  * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
  * address haddr.
@@ -1433,7 +1444,7 @@  void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
 	struct vm_area_struct *vma = find_vma(mm, haddr);
 	struct page *hpage;
 	pte_t *start_pte, *pte;
-	pmd_t *pmd, _pmd;
+	pmd_t *pmd;
 	spinlock_t *ptl;
 	int count = 0;
 	int i;
@@ -1509,12 +1520,7 @@  void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
 	}
 
 	/* step 4: collapse pmd */
-	ptl = pmd_lock(vma->vm_mm, pmd);
-	_pmd = pmdp_collapse_flush(vma, haddr, pmd);
-	spin_unlock(ptl);
-	mm_dec_nr_ptes(mm);
-	pte_free(mm, pmd_pgtable(_pmd));
-
+	collapse_and_free_pmd(mm, vma, haddr, pmd);
 drop_hpage:
 	unlock_page(hpage);
 	put_page(hpage);
@@ -1552,7 +1558,7 @@  static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
 	struct vm_area_struct *vma;
 	struct mm_struct *mm;
 	unsigned long addr;
-	pmd_t *pmd, _pmd;
+	pmd_t *pmd;
 
 	i_mmap_lock_write(mapping);
 	vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
@@ -1591,14 +1597,8 @@  static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
 		 * reverse order. Trylock is a way to avoid deadlock.
 		 */
 		if (mmap_write_trylock(mm)) {
-			if (!khugepaged_test_exit(mm)) {
-				spinlock_t *ptl = pmd_lock(mm, pmd);
-				/* assume page table is clear */
-				_pmd = pmdp_collapse_flush(vma, addr, pmd);
-				spin_unlock(ptl);
-				mm_dec_nr_ptes(mm);
-				pte_free(mm, pmd_pgtable(_pmd));
-			}
+			if (!khugepaged_test_exit(mm))
+				collapse_and_free_pmd(mm, vma, addr, pmd);
 			mmap_write_unlock(mm);
 		} else {
 			/* Try again later */