From: Kairui Song Restructure the error paths in pin_hibernation_swap_type() using a goto out pattern to avoid repeated spin_unlock() calls and simplify the return flow. Also simplify unpin_hibernation_swap_type() by making the si NULL check inline, and clean up find_first_swap() to avoid an early return inside the loop. Signed-off-by: Kairui Song Signed-off-by: Lian Wang (ProcessMission) Tested-by: Kunwu Chan --- mm/swapfile.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 0f962cdfa5c0..46772d0e3e68 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2260,21 +2260,18 @@ static int __find_hibernation_swap_type(dev_t device, sector_t offset) */ int pin_hibernation_swap_type(dev_t device, sector_t offset) { - int type; + int ret; struct swap_info_struct *si; spin_lock(&swap_lock); + ret = __find_hibernation_swap_type(device, offset); + if (ret < 0) + goto out; - type = __find_hibernation_swap_type(device, offset); - if (type < 0) { - spin_unlock(&swap_lock); - return type; - } - - si = swap_type_to_info(type); + si = swap_type_to_info(ret); if (WARN_ON_ONCE(!si)) { - spin_unlock(&swap_lock); - return -ENODEV; + ret = -ENODEV; + goto out; } /* @@ -2283,14 +2280,15 @@ int pin_hibernation_swap_type(dev_t device, sector_t offset) * the same session. */ if (WARN_ON_ONCE(si->flags & SWP_HIBERNATION)) { - spin_unlock(&swap_lock); - return -EBUSY; + ret = -EBUSY; + goto out; } si->flags |= SWP_HIBERNATION; +out: spin_unlock(&swap_lock); - return type; + return ret; } /** @@ -2309,11 +2307,8 @@ void unpin_hibernation_swap_type(int type) spin_lock(&swap_lock); si = swap_type_to_info(type); - if (!si) { - spin_unlock(&swap_lock); - return; - } - si->flags &= ~SWP_HIBERNATION; + if (si) + si->flags &= ~SWP_HIBERNATION; spin_unlock(&swap_lock); } @@ -2348,7 +2343,7 @@ int find_hibernation_swap_type(dev_t device, sector_t offset) int find_first_swap(dev_t *device) { - int type; + int type, ret = -ENODEV; spin_lock(&swap_lock); for (type = 0; type < nr_swapfiles; type++) { @@ -2357,11 +2352,11 @@ int find_first_swap(dev_t *device) if (!(sis->flags & SWP_WRITEOK)) continue; *device = sis->bdev->bd_dev; - spin_unlock(&swap_lock); - return type; + ret = type; + break; } spin_unlock(&swap_lock); - return -ENODEV; + return ret; } /* -- 2.55.0