[PATCH 8/9] xfs: don't run speculative preallocation gc when fs is frozen

Darrick J. Wong
2021-06-07T22:25:40Z

From: Darrick J. Wong <djwong@kernel.org>

Now that we have the infrastructure to switch background workers on and
off at will, fix the block gc worker code so that we don't actually run
the worker when the filesystem is frozen, same as we do for deferred
inactivation.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
fs/xfs/xfs_icache.c | 29 ++++++++++++++++++++++++++---
fs/xfs/xfs_mount.c | 1 +
fs/xfs/xfs_mount.h | 3 +++
fs/xfs/xfs_super.c | 5 +++--
4 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/fs/xfs/xfs_icache.c b/fs/xfs/xfs_icache.c
index 22090b318e58..5d5a0c137f32 100644
--- a/fs/xfs/xfs_icache.c
+++ b/fs/xfs/xfs_icache.c
@@ -215,6 +215,12 @@ xfs_reclaim_work_queue(
rcu_read_unlock();
}

+static inline bool
+xfs_blockgc_running(struct xfs_mount *mp)
+{
+ return test_bit(XFS_OPFLAG_BLOCKGC_RUNNING_BIT, &mp->m_opflags);
+}
+
/*
* Background scanning to trim preallocated space. This is queued based on the
* 'speculative_prealloc_lifetime' tunable (5m by default).
@@ -223,6 +229,9 @@ static inline void
xfs_blockgc_queue(
struct xfs_perag *pag)
{
+ if (!xfs_blockgc_running(pag->pag_mount))
+ return;
+
rcu_read_lock();
if (radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG))
queue_delayed_work(pag->pag_mount->m_gc_workqueue,
@@ -1557,7 +1566,8 @@ xfs_blockgc_stop(
struct xfs_perag *pag;
xfs_agnumber_t agno;

- for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG)
+ clear_bit(XFS_OPFLAG_BLOCKGC_RUNNING_BIT, &mp->m_opflags);
+ for_each_perag(mp, agno, pag)
cancel_delayed_work_sync(&pag->pag_blockgc_work);
}

@@ -1569,6 +1579,7 @@ xfs_blockgc_start(
struct xfs_perag *pag;
xfs_agnumber_t agno;

+ set_bit(XFS_OPFLAG_BLOCKGC_RUNNING_BIT, &mp->m_opflags);
for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG)
xfs_blockgc_queue(pag);
}
@@ -1626,6 +1637,13 @@ xfs_blockgc_scan_inode(
unsigned int lockflags = 0;
int error;

+ /*
+ * Speculative preallocation gc isn't supposed to run when the fs is
+ * frozen because we don't want kernel threads to block on transaction
+ * allocation.
+ */
+ ASSERT(ip->i_mount->m_super->s_writers.frozen < SB_FREEZE_FS);
+
error = xfs_inode_free_eofblocks(ip, icw, &lockflags);
if (error)
goto unlock;
@@ -1648,13 +1666,18 @@ xfs_blockgc_worker(
struct xfs_mount *mp = pag->pag_mount;
int error;

- if (!sb_start_write_trylock(mp->m_super))
+ /*
+ * Queueing of this blockgc worker can race with xfs_blockgc_stop,
+ * which means that we can be running after the opflag clears. Double
+ * check the flag state so that we don't trip asserts.
+ */
+ if (!xfs_blockgc_running(mp))
return;
+
error = xfs_icwalk_ag(pag, XFS_ICWALK_BLOCKGC, NULL);
if (error)
xfs_info(mp, "AG %u preallocation gc worker failed, err=%d",
pag->pag_agno, error);
- sb_end_write(mp->m_super);
xfs_blockgc_queue(pag);
}

diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index 24dc3b7026b7..d95432e4ac39 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -797,6 +797,7 @@ xfs_mountfs(

/* Enable background workers. */
xfs_inodegc_start(mp);
+ xfs_blockgc_start(mp);

/*
* Get and sanity-check the root inode.
diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h
index 4323aaa3e7b4..2da5bd55dd81 100644
--- a/fs/xfs/xfs_mount.h
+++ b/fs/xfs/xfs_mount.h
@@ -270,6 +270,9 @@ typedef struct xfs_mount {
inode inactivation worker? */
#define XFS_OPFLAG_INACTIVATE_NOW_BIT (1) /* force foreground inactivation
of unlinked inodes */
+#define XFS_OPFLAG_BLOCKGC_RUNNING_BIT (2) /* are we allowed to start the
+ speculative preallocation gc
+ worker? */

/*
* Max and min values for mount-option defined I/O
diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index 968176b35d13..730f8e960d98 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -852,8 +852,10 @@ xfs_fs_sync_fs(
* s_umount, which means we can't lock out a concurrent thaw request
* without adding another layer of locks to the freeze process.
*/
- if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT)
+ if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT) {
xfs_inodegc_stop(mp);
+ xfs_blockgc_stop(mp);
+ }

return 0;
}
@@ -970,7 +972,6 @@ xfs_fs_freeze(
* set a GFP_NOFS context here to avoid recursion deadlocks.
*/
flags = memalloc_nofs_save();
- xfs_blockgc_stop(mp);
xfs_save_resvblks(mp);
ret = xfs_log_quiesce(mp);
memalloc_nofs_restore(flags);