[RFC PATCH v1 1/5] mm: swap: Simplify end-of-cluster calculation

From: Ryan Roberts
Date: Tue Jun 18 2024 - 19:27:16 EST


Its possible that a swap file will have a partial cluster at the end, if
the swap size is not a multiple of the cluster size. But this partial
cluster will never be marked free and so scan_swap_map_try_ssd_cluster()
will never see it. Therefore it can always consider that a cluster ends
at the next cluster boundary.

This leads to a simplification of the endpoint calculation and removal
of an unnecessary conditional.

This change has the useful side effect of making lock_cluster()
unconditional, which will be used in a later commit.

Signed-off-by: Ryan Roberts <ryan.roberts@xxxxxxx>
---
mm/swapfile.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index b3e5e384e330..30e79739dfdc 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -677,16 +677,14 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
* check if there is still free entry in the cluster, maintaining
* natural alignment.
*/
- max = min_t(unsigned long, si->max, ALIGN(tmp + 1, SWAPFILE_CLUSTER));
- if (tmp < max) {
- ci = lock_cluster(si, tmp);
- while (tmp < max) {
- if (swap_range_empty(si->swap_map, tmp, nr_pages))
- break;
- tmp += nr_pages;
- }
- unlock_cluster(ci);
+ max = ALIGN(tmp + 1, SWAPFILE_CLUSTER);
+ ci = lock_cluster(si, tmp);
+ while (tmp < max) {
+ if (swap_range_empty(si->swap_map, tmp, nr_pages))
+ break;
+ tmp += nr_pages;
}
+ unlock_cluster(ci);
if (tmp >= max) {
cluster->next[order] = SWAP_NEXT_INVALID;
goto new_cluster;
--
2.43.0