Re: [RFC PATCH v4 02/18] mm/swap: clean up hibernation error handling

From: Kunwu Chan

Date: Thu Nov 19 2026 - 10:57:52 EST


>
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> 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 <kasong@xxxxxxxxxxx>
> Signed-off-by: Lian Wang <lianux.mm@xxxxxxxxx>
> ---
> mm/swapfile.c | 39 +++++++++++++++++----------------------
> 1 file changed, 17 insertions(+), 22 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index c41c4d26a7e6..c69a924acb74 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2331,21 +2331,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;
> }
>
> /*
> @@ -2354,14 +2351,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;
> }
>
> /**
> @@ -2441,11 +2439,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);
> }
>
> @@ -2480,7 +2475,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++) {
> @@ -2489,11 +2484,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;
> }
>
> /*
>

Thanks!

Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>