The part of __swap_duplicate() with lock held is separated into a new
function __swap_duplicate_locked().  Because we will add more logic
about the PMD swap mapping into __swap_duplicate() and keep the most
PTE swap mapping related logic in __swap_duplicate_locked().

Just mechanical code refactoring, there is no any functional change in
this patch.

Signed-off-by: "Huang, Ying" <[email protected]>
Cc: "Kirill A. Shutemov" <[email protected]>
Cc: Andrea Arcangeli <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Shaohua Li <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Minchan Kim <[email protected]>
Cc: Rik van Riel <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Naoya Horiguchi <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Daniel Jordan <[email protected]>
---
 mm/swapfile.c | 63 +++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 35 insertions(+), 28 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 1996dcd732b6..532de6f8ff39 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3436,32 +3436,12 @@ void si_swapinfo(struct sysinfo *val)
        spin_unlock(&swap_lock);
 }
 
-/*
- * Verify that a swap entry is valid and increment its swap map count.
- *
- * Returns error code in following case.
- * - success -> 0
- * - swp_entry is invalid -> EINVAL
- * - swp_entry is migration entry -> EINVAL
- * - swap-cache reference is requested but there is already one. -> EEXIST
- * - swap-cache reference is requested but the entry is not used. -> ENOENT
- * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
- */
-static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
+static int __swap_duplicate_locked(struct swap_info_struct *p,
+                                  unsigned long offset, unsigned char usage)
 {
-       struct swap_info_struct *p;
-       struct swap_cluster_info *ci;
-       unsigned long offset;
        unsigned char count;
        unsigned char has_cache;
-       int err = -EINVAL;
-
-       p = get_swap_device(entry);
-       if (!p)
-               goto out;
-
-       offset = swp_offset(entry);
-       ci = lock_cluster_or_swap_info(p, offset);
+       int err = 0;
 
        count = p->swap_map[offset];
 
@@ -3471,12 +3451,11 @@ static int __swap_duplicate(swp_entry_t entry, unsigned 
char usage)
         */
        if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
                err = -ENOENT;
-               goto unlock_out;
+               goto out;
        }
 
        has_cache = count & SWAP_HAS_CACHE;
        count &= ~SWAP_HAS_CACHE;
-       err = 0;
 
        if (usage == SWAP_HAS_CACHE) {
 
@@ -3503,11 +3482,39 @@ static int __swap_duplicate(swp_entry_t entry, unsigned 
char usage)
 
        p->swap_map[offset] = count | has_cache;
 
-unlock_out:
+out:
+       return err;
+}
+
+/*
+ * Verify that a swap entry is valid and increment its swap map count.
+ *
+ * Returns error code in following case.
+ * - success -> 0
+ * - swp_entry is invalid -> EINVAL
+ * - swp_entry is migration entry -> EINVAL
+ * - swap-cache reference is requested but there is already one. -> EEXIST
+ * - swap-cache reference is requested but the entry is not used. -> ENOENT
+ * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
+ */
+static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
+{
+       struct swap_info_struct *p;
+       struct swap_cluster_info *ci;
+       unsigned long offset;
+       int err = -EINVAL;
+
+       p = get_swap_device(entry);
+       if (!p)
+               goto out;
+
+       offset = swp_offset(entry);
+       ci = lock_cluster_or_swap_info(p, offset);
+       err = __swap_duplicate_locked(p, offset, usage);
        unlock_cluster_or_swap_info(p, ci);
+
+       put_swap_device(p);
 out:
-       if (p)
-               put_swap_device(p);
        return err;
 }
 
-- 
2.16.4

Reply via email to