[PATCH 8/8] nilfs2: avoid infinite retry loop on GC failure
From: Ryusuke Konishi
Date: Thu Nov 26 2026 - 23:43:10 EST
Currently, the GC loop in nilfs_clean_segments() only breaks out when
nilfs_segctor_construct() returns -EROFS, continuing to retry on any
other error. When a persistent or non-retryable error such as -EIO or
-ENOSPC occurs during log writing due to filesystem corruption, I/O
errors, or free space exhaustion, this loop can spin indefinitely while
holding the transaction lock, leading to task hangs.
Now that GC-specific state changes can be selectively reverted on log
writing failure to preserve filesystem consistency for subsequent
operations, modify nilfs_clean_segments() to abort immediately on
errors in general, leaving only a minimal retry loop bounded by
NILFS_GC_RETRY_LIMIT for -EAGAIN and -EBUSY.
Fixes: ce5a5ad1a833 ("nilfs2: fix infinite loop in nilfs_clean_segments()")
Reported-by: Zheng Zheng <fengaojigui88@xxxxxxxxx>
Closes: https://lore.kernel.org/linux-nilfs/CAO-4RZ1Q-HzRCd+S1uHZJRdN-ZCtv47f=pXTxbq4YaJgn_DALQ@xxxxxxxxxxxxxx
Closes: https://lore.kernel.org/linux-nilfs/CAO-4RZ2pjEOpTBzqca5UxZHtaFS2Dj9zmNj1BtjvKm1yFE7_Jg@xxxxxxxxxxxxxx
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
---
fs/nilfs2/segment.c | 9 ++++++++-
fs/nilfs2/segment.h | 1 +
2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
index b306f09c9497..5765959617fe 100644
--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -2582,6 +2582,7 @@ int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
size_t susz = NILFS_MDT(nilfs->ns_sufile)->mi_entry_size;
__u64 *segnumv = kbufs[4];
void *subuf = NULL;
+ unsigned int retries = 0;
if (unlikely(!sci))
return -EROFS;
@@ -2644,7 +2645,13 @@ int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
nilfs_warn(sb, "error %d cleaning segments", err);
- if (unlikely(err == -EROFS))
+ /*
+ * Although the log writer should not have any paths returning
+ * retryable errors (-EAGAIN or -EBUSY), keep a bounded retry
+ * path conservatively.
+ */
+ if ((err != -EAGAIN && err != -EBUSY) ||
+ retries++ >= NILFS_GC_RETRY_LIMIT)
goto out_unlock;
set_current_state(TASK_INTERRUPTIBLE);
diff --git a/fs/nilfs2/segment.h b/fs/nilfs2/segment.h
index 711c69e24575..4158948d890d 100644
--- a/fs/nilfs2/segment.h
+++ b/fs/nilfs2/segment.h
@@ -204,6 +204,7 @@ enum {
* Retry count of construction when
* destroying segctord
*/
+#define NILFS_GC_RETRY_LIMIT 2 /* Number of internal GC retries */
/*
* Default values of timeout, in seconds.
--
2.53.0