xfs: add tunable threshold parameter for triggering zone GC
authorHans Holmberg <Hans.Holmberg@wdc.com>
Tue, 25 Mar 2025 09:10:49 +0000 (09:10 +0000)
committerCarlos Maiolino <cem@kernel.org>
Mon, 14 Apr 2025 08:41:33 +0000 (10:41 +0200)
Presently we start garbage collection late - when we start running
out of free zones to backfill max_open_zones. This is a reasonable
default as it minimizes write amplification. The longer we wait,
the more blocks are invalidated and reclaim cost less in terms
of blocks to relocate.

Starting this late however introduces a risk of GC being outcompeted
by user writes. If GC can't keep up, user writes will be forced to
wait for free zones with high tail latencies as a result.

This is not a problem under normal circumstances, but if fragmentation
is bad and user write pressure is high (multiple full-throttle
writers) we will "bottom out" of free zones.

To mitigate this, introduce a zonegc_low_space tunable that lets the
user specify a percentage of how much of the unused space that GC
should keep available for writing. A high value will reclaim more of
the space occupied by unused blocks, creating a larger buffer against
write bursts.

This comes at a cost as write amplification is increased. To
illustrate this using a sample workload, setting zonegc_low_space to
60% avoids high (500ms) max latencies while increasing write
amplification by 15%.

Signed-off-by: Hans Holmberg <hans.holmberg@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
Documentation/admin-guide/xfs.rst
fs/xfs/xfs_mount.h
fs/xfs/xfs_sysfs.c
fs/xfs/xfs_zone_alloc.c
fs/xfs/xfs_zone_gc.c

index b67772c..7b0811d 100644 (file)
@@ -542,3 +542,24 @@ The interesting knobs for XFS workqueues are as follows:
   nice           Relative priority of scheduling the threads.  These are the
                  same nice levels that can be applied to userspace processes.
 ============     ===========
+
+Zoned Filesystems
+=================
+
+For zoned file systems, the following attributes are exposed in:
+
+ /sys/fs/xfs/<dev>/zoned/
+
+ max_open_zones                 (Min:  1  Default:  Varies  Max:  UINTMAX)
+        This read-only attribute exposes the maximum number of open zones
+        available for data placement. The value is determined at mount time and
+        is limited by the capabilities of the backing zoned device, file system
+        size and the max_open_zones mount option.
+
+ zonegc_low_space               (Min:  0  Default:  0  Max:  100)
+        Define a percentage for how much of the unused space that GC should keep
+        available for writing. A high value will reclaim more of the space
+        occupied by unused blocks, creating a larger buffer against write
+        bursts at the cost of increased write amplification.  Regardless
+        of this value, garbage collection will always aim to free a minimum
+        amount of blocks to keep max_open_zones open for data placement purposes.
index 799b842..e5192c1 100644 (file)
@@ -229,6 +229,7 @@ typedef struct xfs_mount {
        bool                    m_finobt_nores; /* no per-AG finobt resv. */
        bool                    m_update_sb;    /* sb needs update in mount */
        unsigned int            m_max_open_zones;
+       unsigned int            m_zonegc_low_space;
 
        /*
         * Bitsets of per-fs metadata that have been checked and/or are sick.
index b7e82d8..7a5c5ef 100644 (file)
@@ -718,8 +718,40 @@ max_open_zones_show(
 }
 XFS_SYSFS_ATTR_RO(max_open_zones);
 
+static ssize_t
+zonegc_low_space_store(
+       struct kobject          *kobj,
+       const char              *buf,
+       size_t                  count)
+{
+       int                     ret;
+       unsigned int            val;
+
+       ret = kstrtouint(buf, 0, &val);
+       if (ret)
+               return ret;
+
+       if (val > 100)
+               return -EINVAL;
+
+       zoned_to_mp(kobj)->m_zonegc_low_space = val;
+
+       return count;
+}
+
+static ssize_t
+zonegc_low_space_show(
+       struct kobject          *kobj,
+       char                    *buf)
+{
+       return sysfs_emit(buf, "%u\n",
+                       zoned_to_mp(kobj)->m_zonegc_low_space);
+}
+XFS_SYSFS_ATTR_RW(zonegc_low_space);
+
 static struct attribute *xfs_zoned_attrs[] = {
        ATTR_LIST(max_open_zones),
+       ATTR_LIST(zonegc_low_space),
        NULL,
 };
 ATTRIBUTE_GROUPS(xfs_zoned);
index 52af234..d509e49 100644 (file)
@@ -1201,6 +1201,13 @@ xfs_mount_zones(
        xfs_set_freecounter(mp, XC_FREE_RTEXTENTS,
                        iz.available + iz.reclaimable);
 
+       /*
+        * The user may configure GC to free up a percentage of unused blocks.
+        * By default this is 0. GC will always trigger at the minimum level
+        * for keeping max_open_zones available for data placement.
+        */
+       mp->m_zonegc_low_space = 0;
+
        error = xfs_zone_gc_mount(mp);
        if (error)
                goto out_free_zone_info;
index c5136ea..8c541ca 100644 (file)
@@ -162,18 +162,30 @@ struct xfs_zone_gc_data {
 
 /*
  * We aim to keep enough zones free in stock to fully use the open zone limit
- * for data placement purposes.
+ * for data placement purposes. Additionally, the m_zonegc_low_space tunable
+ * can be set to make sure a fraction of the unused blocks are available for
+ * writing.
  */
 bool
 xfs_zoned_need_gc(
        struct xfs_mount        *mp)
 {
+       s64                     available, free;
+
        if (!xfs_group_marked(mp, XG_TYPE_RTG, XFS_RTG_RECLAIMABLE))
                return false;
-       if (xfs_estimate_freecounter(mp, XC_FREE_RTAVAILABLE) <
+
+       available = xfs_estimate_freecounter(mp, XC_FREE_RTAVAILABLE);
+
+       if (available <
            mp->m_groups[XG_TYPE_RTG].blocks *
            (mp->m_max_open_zones - XFS_OPEN_GC_ZONES))
                return true;
+
+       free = xfs_estimate_freecounter(mp, XC_FREE_RTEXTENTS);
+       if (available < mult_frac(free, mp->m_zonegc_low_space, 100))
+               return true;
+
        return false;
 }