Skip to contentSkip to conversation

mm: Extend 'usage' parameter so that cluster_swap_free_nr() can be reused

PATCH Superseded Barry Song 1 participant 2 messages Jul 8, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

PATCH 1/1 mm: Extend 'usage' parameter so that cluster_swap_free_nr() can be reused

From: Barry Song <[email protected]>

Extend a usage parameter so that cluster_swap_free_nr() can be reused by both swapcache_clear() and swap_free().
__swap_entry_free() is quite similar but more tricky as it requires the return value of __swap_entry_free_locked() which cluster_swap_free_nr() doesn't support.

Cc "Huang, Ying" <[email protected]>
Cc Chris Li <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Kairui Song <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Chuanhua Han <[email protected]>
---
 mm/swapfile.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
mm/swapfile.c +5 -10
index f7224bc1320c..70c4a9132672 100644
@@ -1352,7 +1352,8 @@ static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
 }
 
 static void cluster_swap_free_nr(struct swap_info_struct *sis,
-		unsigned long offset, int nr_pages)
+		unsigned long offset, int nr_pages,
+		unsigned char usage)
 {
 	struct swap_cluster_info *ci;
 	DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 };
@@ -1363,7 +1364,7 @@ static void cluster_swap_free_nr(struct swap_info_struct *sis,
 		nr = min(BITS_PER_LONG, nr_pages);
 		for (i = 0; i < nr; i++) {
 			if (!__swap_entry_free_locked(sis, offset + i, 1))
-				bitmap_set(to_free, i, 1);
+				bitmap_set(to_free, i, usage);
 		}
 		if (!bitmap_empty(to_free, BITS_PER_LONG)) {
 			unlock_cluster_or_swap_info(sis, ci);
@@ -1396,7 +1397,7 @@ void swap_free_nr(swp_entry_t entry, int nr_pages)
 
 	while (nr_pages) {
 		nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);
-		cluster_swap_free_nr(sis, offset, nr);
+		cluster_swap_free_nr(sis, offset, nr, 1);
 		offset += nr;
 		nr_pages -= nr;
 	}
@@ -3492,15 +3493,9 @@ int swapcache_prepare(swp_entry_t entry)
 
 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry)
 {
-	struct swap_cluster_info *ci;
 	unsigned long offset = swp_offset(entry);
-	unsigned char usage;
 
-	ci = lock_cluster_or_swap_info(si, offset);
-	usage = __swap_entry_free_locked(si, offset, SWAP_HAS_CACHE);
-	unlock_cluster_or_swap_info(si, ci);
-	if (!usage)
-		free_swap_slot(entry);
+	cluster_swap_free_nr(si, offset, 1, SWAP_HAS_CACHE);
 }
 
 struct swap_info_struct *swp_swap_info(swp_entry_t entry)
Signature · 1 line
2.39.2
author
#1 lore raw
Quoted 35 lines · Barry Song

From: Barry Song <[email protected]>

Extend a usage parameter so that cluster_swap_free_nr() can be reused by both swapcache_clear() and swap_free().
__swap_entry_free() is quite similar but more tricky as it requires the return value of __swap_entry_free_locked() which cluster_swap_free_nr() doesn't support.

Cc "Huang, Ying" <[email protected]>
Cc Chris Li <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Kairui Song <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Chuanhua Han <[email protected]>
---
 mm/swapfile.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
mm/swapfile.c +2 -1
index f7224bc1320c..70c4a9132672 100644
@@ -1352,7 +1352,8 @@ static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
 }
 
 static void cluster_swap_free_nr(struct swap_info_struct *sis,
-               unsigned long offset, int nr_pages)
+               unsigned long offset, int nr_pages,
+               unsigned char usage)
 {
        struct swap_cluster_info *ci;
        DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 };

mm/swapfile.c static void cluster_swap_free_nr(struct swap_info_struct *sis, · line 1367

Quoted 5 lines
mm/swapfile.c +1 -1
@@ -1363,7 +1364,7 @@ static void cluster_swap_free_nr(struct swap_info_struct *sis,
                nr = min(BITS_PER_LONG, nr_pages);
                for (i = 0; i < nr; i++) {
                        if (!__swap_entry_free_locked(sis, offset + i, 1))
-                               bitmap_set(to_free, i, 1);
+                               bitmap_set(to_free, i, usage);

I'm terribly sorry for the mis-operation(sent the wrong patch), I actually mean:

if (!__swap_entry_free_locked(sis, offset + i, usage))

           bitmap_set(to_free, i, 1);
Quoted 32 lines
mm/swapfile.c +2 -9
                }
                if (!bitmap_empty(to_free, BITS_PER_LONG)) {
                        unlock_cluster_or_swap_info(sis, ci);
@@ -1396,7 +1397,7 @@ void swap_free_nr(swp_entry_t entry, int nr_pages)
 
        while (nr_pages) {
                nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);
-               cluster_swap_free_nr(sis, offset, nr);
+               cluster_swap_free_nr(sis, offset, nr, 1);
                offset += nr;
                nr_pages -= nr;
        }
@@ -3492,15 +3493,9 @@ int swapcache_prepare(swp_entry_t entry)
 
 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry)
 {
-       struct swap_cluster_info *ci;
        unsigned long offset = swp_offset(entry);
-       unsigned char usage;
 
-       ci = lock_cluster_or_swap_info(si, offset);
-       usage = __swap_entry_free_locked(si, offset, SWAP_HAS_CACHE);
-       unlock_cluster_or_swap_info(si, ci);
-       if (!usage)
-               free_swap_slot(entry);
+       cluster_swap_free_nr(si, offset, 1, SWAP_HAS_CACHE);
 }
 
 struct swap_info_struct *swp_swap_info(swp_entry_t entry)
--

2.39.2