[PATCH AUTOSEL 5.4 11/12] gfs2: Refcounting fix in gfs2_thaw_super

From: Sasha Levin
Date: Mon Jan 15 2024 - 20:09:20 EST


From: Andreas Gruenbacher <agruenba@xxxxxxxxxx>

[ Upstream commit 4e58543e7da4859c4ba61d15493e3522b6ad71fd ]

It turns out that the .freeze_super and .thaw_super operations require
the filesystem to manage the superblock refcount itself. We are using
the freeze_super() and thaw_super() helpers to mostly take care of that
for us, but this means that the superblock may no longer be around by
when thaw_super() returns, and gfs2_thaw_super() will then access freed
memory. Take an extra superblock reference in gfs2_thaw_super() to fix
that.

Signed-off-by: Andreas Gruenbacher <agruenba@xxxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
fs/gfs2/super.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index 15e757f76380..e6aa7e279785 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -770,6 +770,7 @@ static int gfs2_freeze(struct super_block *sb)
goto out;
}

+ atomic_inc(&sb->s_active);
for (;;) {
error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
if (!error)
@@ -786,6 +787,7 @@ static int gfs2_freeze(struct super_block *sb)
set_bit(SDF_FS_FROZEN, &sdp->sd_flags);
out:
mutex_unlock(&sdp->sd_freeze_mutex);
+ deactivate_super(sb);
return error;
}

--
2.43.0