[PATCH 8/9] mm: swap: factor out helper to drop cache of entries within a single cluster

Kemeng Shi posted 9 patches 9 months, 1 week ago
There is a newer version of this series
[PATCH 8/9] mm: swap: factor out helper to drop cache of entries within a single cluster
Posted by Kemeng Shi 9 months, 1 week ago
Factor out helper swap_entries_put_cache() from put_swap_folio() to server
as a general-purpose routine for dropping cache flag of entries within a
single cluster.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
 mm/swapfile.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index ebac9ff74ba7..343b34eb2a81 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1473,6 +1473,21 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
 	return NULL;
 }
 
+static void swap_entries_put_cache(struct swap_info_struct *si,
+				   swp_entry_t entry, int nr)
+{
+	unsigned long offset = swp_offset(entry);
+	struct swap_cluster_info *ci;
+
+	ci = lock_cluster(si, offset);
+	if (swap_only_has_cache(si, offset, nr))
+		swap_entries_free(si, ci, entry, nr);
+	else
+		for (int i = 0; i < nr; i++, entry.val++)
+			swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE);
+	unlock_cluster(ci);
+}
+
 static bool swap_entries_put_map(struct swap_info_struct *si,
 				 swp_entry_t entry, int nr)
 {
@@ -1597,8 +1612,6 @@ void swap_free_nr(swp_entry_t entry, int nr_pages)
  */
 void put_swap_folio(struct folio *folio, swp_entry_t entry)
 {
-	unsigned long offset = swp_offset(entry);
-	struct swap_cluster_info *ci;
 	struct swap_info_struct *si;
 	int size = 1 << swap_entry_order(folio_order(folio));
 
@@ -1606,13 +1619,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry)
 	if (!si)
 		return;
 
-	ci = lock_cluster(si, offset);
-	if (swap_only_has_cache(si, offset, size))
-		swap_entries_free(si, ci, entry, size);
-	else
-		for (int i = 0; i < size; i++, entry.val++)
-			swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE);
-	unlock_cluster(ci);
+	swap_entries_put_cache(si, entry, size);
 }
 
 int __swap_count(swp_entry_t entry)
-- 
2.30.0
Re: [PATCH 8/9] mm: swap: factor out helper to drop cache of entries within a single cluster
Posted by Tim Chen 9 months, 1 week ago
On Fri, 2025-03-14 at 05:05 +0800, Kemeng Shi wrote:
> Factor out helper swap_entries_put_cache() from put_swap_folio() to server
> as a general-purpose routine for dropping cache flag of entries within a
> single cluster.
> 
> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>

Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>

> ---
>  mm/swapfile.c | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index ebac9ff74ba7..343b34eb2a81 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1473,6 +1473,21 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
>  	return NULL;
>  }
>  
> +static void swap_entries_put_cache(struct swap_info_struct *si,
> +				   swp_entry_t entry, int nr)
> +{
> +	unsigned long offset = swp_offset(entry);
> +	struct swap_cluster_info *ci;
> +
> +	ci = lock_cluster(si, offset);
> +	if (swap_only_has_cache(si, offset, nr))
> +		swap_entries_free(si, ci, entry, nr);
> +	else
> +		for (int i = 0; i < nr; i++, entry.val++)
> +			swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE);
> +	unlock_cluster(ci);
> +}
> +
>  static bool swap_entries_put_map(struct swap_info_struct *si,
>  				 swp_entry_t entry, int nr)
>  {
> @@ -1597,8 +1612,6 @@ void swap_free_nr(swp_entry_t entry, int nr_pages)
>   */
>  void put_swap_folio(struct folio *folio, swp_entry_t entry)
>  {
> -	unsigned long offset = swp_offset(entry);
> -	struct swap_cluster_info *ci;
>  	struct swap_info_struct *si;
>  	int size = 1 << swap_entry_order(folio_order(folio));
>  
> @@ -1606,13 +1619,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry)
>  	if (!si)
>  		return;
>  
> -	ci = lock_cluster(si, offset);
> -	if (swap_only_has_cache(si, offset, size))
> -		swap_entries_free(si, ci, entry, size);
> -	else
> -		for (int i = 0; i < size; i++, entry.val++)
> -			swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE);
> -	unlock_cluster(ci);
> +	swap_entries_put_cache(si, entry, size);
>  }
>  
>  int __swap_count(swp_entry_t entry)