]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
xfs: avoid unnecessary calculations in xfs_zoned_need_gc()
authorDamien Le Moal <dlemoal@kernel.org>
Mon, 16 Mar 2026 11:40:20 +0000 (20:40 +0900)
committerCarlos Maiolino <cem@kernel.org>
Wed, 18 Mar 2026 09:08:07 +0000 (10:08 +0100)
If zonegc_low_space is set to zero (which is the default), the second
condition in xfs_zoned_need_gc() that triggers GC never evaluates to
true because the calculated threshold will always be 0. So there is no
need to calculate the threshold and to evaluate that condition. Return
early when zonegc_low_space is zero.

While at it, add comments to document the intent of each of the 3 tests
used to determine the return value to control the execution of garbage
collection.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Hans Holmberg <hans.holmberg@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
fs/xfs/xfs_zone_gc.c

index 7efeecd2d85f5162d1dfab227e3a5118e15042c1..aaa0a3119d91944689dddf77bf3467fe1c7307bf 100644 (file)
@@ -171,25 +171,37 @@ xfs_zoned_need_gc(
        s64                     available, free, threshold;
        s32                     remainder;
 
+       /* If we have no reclaimable blocks, running GC is useless. */
        if (!xfs_zoned_have_reclaimable(mp->m_zone_info))
                return false;
 
+       /*
+        * In order to avoid file fragmentation as much as possible, we should
+        * make sure that we can open enough zones. So trigger GC if the number
+        * of blocks immediately available for writes is lower than the total
+        * number of blocks from all possible open zones.
+        */
        available = xfs_estimate_freecounter(mp, XC_FREE_RTAVAILABLE);
-
        if (available <
            xfs_rtgs_to_rfsbs(mp, mp->m_max_open_zones - XFS_OPEN_GC_ZONES))
                return true;
 
-       free = xfs_estimate_freecounter(mp, XC_FREE_RTEXTENTS);
+       /*
+        * For cases where the user wants to be more aggressive with GC,
+        * the sysfs attribute zonegc_low_space may be set to a non zero value,
+        * to indicate that GC should try to maintain at least zonegc_low_space
+        * percent of the free space to be directly available for writing. Check
+        * this here.
+        */
+       if (!mp->m_zonegc_low_space)
+               return false;
 
+       free = xfs_estimate_freecounter(mp, XC_FREE_RTEXTENTS);
        threshold = div_s64_rem(free, 100, &remainder);
        threshold = threshold * mp->m_zonegc_low_space +
                    remainder * div_s64(mp->m_zonegc_low_space, 100);
 
-       if (available < threshold)
-               return true;
-
-       return false;
+       return available < threshold;
 }
 
 static struct xfs_zone_gc_data *