]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
mm, swap: introduce a helper for retrieving cluster from offset
authorKairui Song <kasong@tencent.com>
Mon, 13 Jan 2025 17:57:30 +0000 (01:57 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 26 Jan 2025 04:22:37 +0000 (20:22 -0800)
It's a common operation to retrieve the cluster info from offset,
introduce a helper for this.

Link: https://lkml.kernel.org/r/20250113175732.48099-12-ryncsn@gmail.com
Signed-off-by: Kairui Song <kasong@tencent.com>
Suggested-by: Chris Li <chrisl@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Barry Song <v-songbaohua@oppo.com>
Cc: "Huang, Ying" <ying.huang@linux.alibaba.com>
Cc: Hugh Dickens <hughd@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Yosry Ahmed <yosryahmed@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/swapfile.c

index 6da2f3aa55fbe4e5e482a9eb6d1f4b559b32eb34..37d540fa03101a82658e0314998c9e0cf9e9ac54 100644 (file)
@@ -424,6 +424,12 @@ static inline unsigned int cluster_index(struct swap_info_struct *si,
        return ci - si->cluster_info;
 }
 
+static inline struct swap_cluster_info *offset_to_cluster(struct swap_info_struct *si,
+                                                         unsigned long offset)
+{
+       return &si->cluster_info[offset / SWAPFILE_CLUSTER];
+}
+
 static inline unsigned int cluster_offset(struct swap_info_struct *si,
                                          struct swap_cluster_info *ci)
 {
@@ -435,7 +441,7 @@ static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si
 {
        struct swap_cluster_info *ci;
 
-       ci = &si->cluster_info[offset / SWAPFILE_CLUSTER];
+       ci = offset_to_cluster(si, offset);
        spin_lock(&ci->lock);
 
        return ci;
@@ -1480,10 +1486,10 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry
        unsigned char *map_end = map + nr_pages;
        struct swap_cluster_info *ci;
 
-       /* It should never free entries across different clusters */
-       VM_BUG_ON((offset / SWAPFILE_CLUSTER) != ((offset + nr_pages - 1) / SWAPFILE_CLUSTER));
-
        ci = lock_cluster(si, offset);
+
+       /* It should never free entries across different clusters */
+       VM_BUG_ON(ci != offset_to_cluster(si, offset + nr_pages - 1));
        VM_BUG_ON(cluster_is_empty(ci));
        VM_BUG_ON(ci->count < nr_pages);