[PATCH RFC 02/13] mm/swap: slightly cleanup the code for hibernation error handling

From: Kairui Song via B4 Relay

Date: Mon Jul 13 2026 - 13:30:02 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>
---
mm/swapfile.c | 39 +++++++++++++++++----------------------
1 file changed, 17 insertions(+), 22 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 27b4d11125d6..30ee75bdec40 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2247,21 +2247,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;
}

/*
@@ -2270,14 +2267,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;
}

/**
@@ -2296,11 +2294,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);
}

@@ -2335,7 +2330,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++) {
@@ -2344,11 +2339,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