Re: [PATCH v5 02/19] mm/swap: clean up hibernation error handling
From: Chris Li
Date: Thu Nov 26 2026 - 00:59:41 EST
On Mon, Nov 23, 2026 at 6:16 AM Lian Wang <lianux.mm@xxxxxxxxx> wrote:
>
> 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>
> Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> Signed-off-by: Lian Wang <lianux.mm@xxxxxxxxx>
Acked-by: Chris Li <chrisl@xxxxxxxxxx>
> ---
> mm/swapfile.c | 39 +++++++++++++++++----------------------
> 1 file changed, 17 insertions(+), 22 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index c06f9f532e74..0ae901296731 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;
Nitpick: Here "ret" only carries error code. Maybe we can call it "err".
> 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:
Nitpick: Maybe "unlock"?
Chris
> 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;
> }
>
> /*