Skip to content

btrfs: expose async discard initial delays in sysfs - #4170

Merged
DjDeveloperr merged 1 commit into
aurora-silicon:mainfrom
IronsideXXVI:j604/btrfs-async-discard-sysfs-delays
Dec 3, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-silicon:mainfrom
IronsideXXVI:j604/btrfs-async-discard-sysfs-delays

Conversation

@IronsideXXVI

Copy link
Copy Markdown
Contributor

What: Expose Btrfs async-discard initial delays as per-filesystem read-write sysfs attributes under /sys/fs/btrfs/<uuid>/discard/ while keeping existing defaults unchanged:

  • Btrfs discard=async currently holds freed extents in active data block groups for a hardcoded 120 s (BTRFS_DISCARD_DELAY) and empty block groups for 10 s (BTRFS_DISCARD_UNUSED_DELAY). On fast NVMe SSDs with a dynamic pSLC write pool (such as Apple ANS2), holding multi-MiB freed extents untrimmed for up to two minutes leaves the FTL free pool under-provisioned during back-to-back write bursts and loses untrimmed state across reboots.
  • Add discard_delay_ms (default 120000 ms) and discard_unused_delay_ms (default 10000 ms) via standard BTRFS_ATTR_RW (bounded to 0..86400000 ms, zero module parameters).
  • When updated via sysfs, btrfs_discard_update_delay() stores the new delay under discard_ctl->lock, clamps already-queued block groups on the corresponding discard list(s) down to the new deadline, and reschedules discard work immediately.
  • Pairs with userspace tuning in omarchy-mac: tune Apple ANS2 Btrfs async discard and boot-time fstrim omacom/omarchy-mac-pkgs#251 (omarchy-mac-btrfs-discard.service, which tunes discard_delay_ms=1000 and discard_unused_delay_ms=100 strictly on nvme-apple-backed Btrfs filesystems).

Tested:

  • Offline at exact head 06d4fcfa8ac3 on origin/main (62c60d13d4d8, 0 merge-tree conflicts):
    • scripts/checkpatch.pl --strict --git origin/main..HEAD: 0 errors, 0 warnings, 0 checks (199 lines checked).
    • Clang 23.1.5 ARM64 LLVM=1 W=1 KCFLAGS="-Werror": fs/btrfs/discard.o (260664 bytes) and fs/btrfs/sysfs.o (464656 bytes), 0 warnings, 0 errors.
    • 0 module_param additions.
  • Live Apple M4 MacBook Pro 14" (J604, T8132) hardware validation on linux-aurora 7.3.0.aurora1-1.1693 and primary release ship linux-aurora 7.3.0.aurora1-1.1694 (957908b7bd99, boot 29379f09, paired with omarchy-mac PR net: apple: Add N1 protocol-27 station and data-ring consumer #251):
    • Confirmed /sys/fs/btrfs/<uuid>/discard/discard_delay_ms and discard_unused_delay_ms read/write cleanly at boot (1000 / 100 ms applied by omarchy-mac-btrfs-discard.service).
    • Integrated release ship 1.1694 passed all boot, ESP, thermal (34.0 C, PMPEV=0), display/GPU (1640.8 fps), and storage/runtime health gates with 0 failed units and 0 kernel/storage faults.

Risk: two-way door · all platforms (fs/btrfs/{discard.c,discard.h,fs.h,sysfs.c}, +116 / -8 lines). Default delays are unchanged (120000 ms / 10000 ms); behavior only changes when userspace explicitly writes the new per-filesystem sysfs attributes. A plain revert restores the compile-time constants.

Not yet:

  • Upstream submission to linux-btrfs mailing list and Documentation/ABI/testing/sysfs-fs-btrfs update.
  • Standalone isolated A/B measurement at exact origin/main head 06d4fcfa8ac3 separate from iops_limit/kbps_limit/max_discard_size tuning in omacom/omarchy-mac-pkgs#251.
Details and verification

Scope (j604/btrfs-async-discard-sysfs-delays, head 06d4fcfa8ac3 on main 62c60d13d4d8)

  • fs/btrfs/discard.c (+45 / -8)
  • fs/btrfs/discard.h (+8)
  • fs/btrfs/fs.h (+2)
  • fs/btrfs/sysfs.c (+61)
  • Sign-off: Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
  • scripts/checkpatch.pl --strict --git origin/main..HEAD: 0 errors, 0 warnings, 0 checks (199 lines checked).
  • git merge-tree --write-tree origin/main HEAD: clean (267b9bde95bd).
  • Clang 23.1.5 ARM64 LLVM=1 W=1 KCFLAGS="-Werror": fs/btrfs/discard.o (260664 bytes), fs/btrfs/sysfs.o (464656 bytes), 0 warnings.
  • Live J604 (linux-aurora 1.1694, boot 29379f09): discard_delay_ms=1000, discard_unused_delay_ms=100 active via omacom/omarchy-mac-pkgs#251, 0 failed units, 0 faults, G6 PMPEV=0, G7 34.0 C, G5b PASS.

Btrfs async discard holds freed extents in active data block groups for a
hardcoded 120 s (BTRFS_DISCARD_DELAY) and empty block groups for 10 s
(BTRFS_DISCARD_UNUSED_DELAY). On fast NVMe SSDs with a dynamic pSLC write
cache (such as Apple ANS2), holding multi-MiB freed extents untrimmed for
up to two minutes leaves the FTL free pool under-provisioned during
back-to-back write bursts and loses untrimmed state across quick reboots.

Expose discard_delay_ms (default 120000 ms) and discard_unused_delay_ms
(default 10000 ms) as read-write attributes under
/sys/fs/btrfs/<uuid>/discard/, preserving existing defaults identically.
When updated via sysfs, clamp already-queued block groups down to the new
deadline and reschedule discard work immediately.

Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
@IronsideXXVI
IronsideXXVI requested review from a team as code owners December 3, 2026 08:47
@github-actions github-actions Bot added blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Merge readiness at 06d4fcfa: not ready yet

Check Detail
CI at head ok 1 check passed
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head no no line with the head SHA 06d4fcfa, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · all platforms (fs/btrfs/{discard.c,discard.h,fs.h,sysfs.c}, +116 / -8 lines). Default delays are unchanged (120000 ms / 10000 ms); behavior only changes when userspace explicitly writes the new per-filesystem sysfs attributes. A plain revert restores the compile-time constants.

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default deadlines match the old constants, and a sysfs write clamps only the selected class of queued block groups under the discard lock before rescheduling the worker. Milliseconds are widened before conversion to nanoseconds. The live result is from the integrated kernel, while the exact-head verification is the build.

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Dec 3, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested at head 06d4fcfa8ac3cb3f0372d8d2a23ed0ae25f1022e merged on main a05d99c9dbc2 (clean merge, tree c546fc2dfb46), against the same tree with the four btrfs files at main. Main has since moved by one commit that only touches drivers/crypto/apple-aes.c.

Setup: x86_64 UML, btrfs + BTRFS_DEBUG/ASSERT, PROVE_LOCKING, DEBUG_ATOMIC_SLEEP, DEBUG_LIST, DEBUG_OBJECTS (timers, work, free). 1 GiB null_blk, memory_backed=1 discard=1, mount -o discard=async, data and metadata single. Discards counted from /sys/block/nullb0/stat. A static C program does the writes, deletes, sysfs writes and timing.

Row Base (main) Head
1 default values files absent 120000 / 10000, mode 0644
2 delete 200 MiB, defaults, 2 runs each: first discard after the commit +10.2 s, +10.7 s, all discarded +10.5 s, +10.5 s, all discarded
2b delete half the files in live block groups, defaults: discards of those groups from +121.6 s, done at +130.6 s from +123.2 s, done at +131.5 s
3 write discard_unused_delay_ms=100 with a queue pending 3 s - nothing before the write, first discard 0.1 s after, queue empty
3b write discard_delay_ms=100 with live groups pending 14 s - first discard 0.1 s after, queue empty
4 -1, 86400001, 4294967296, 99999999999999999999, abc, 12x, empty, 1 2, 0x10 on both files - all EINVAL, value unchanged
4 0, 1, 120000, 86400000 on both files - accepted, read back the same
6 both 86400000 - no discard in 13 s (no overflow); then 3000: first discard at +3.1 s
6 both 0 - discards within 2.4 s of the delete
nodiscard and back to discard=async - writes accepted, values kept
5 stress, 78 s, 2 CPUs: three threads writing sysfs (two keep the files open across umount), write/delete, remount discard / discard=async / nodiscard, umount + mount 27 umount cycles, 216 remounts 20 umount cycles, 160 remounts, 363,000 sysfs writes, 0 failures

No lockdep, atomic-sleep, list, ODEBUG or btrfs report in any finished run; debug_locks is 1 at the end of each.

KASAN: the same kernel with KASAN ran the bounds rows and two stress runs (about 63,000 sysfs writes, 48 remounts) with no report. It is too slow here to finish the timing rows, and it got through only 1 umount cycle.

checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks (199 lines).

Code read

  • Default path: 120000 * NSEC_PER_MSEC and 10000 * NSEC_PER_MSEC are the two old constants. 86400000 ms is 8.64e13 ns, far below u64.
  • The list walk and the new value are under discard_ctl->lock, as is every other list change (purge included). The clamp only lowers deadlines, so the lists stay in deadline order.
  • Umount: close_ctree() runs btrfs_discard_cleanup() before btrfs_sysfs_remove_mounted(). A store in that gap sees BTRFS_FS_DISCARD_RUNNING cleared and queues nothing. A store that tests the bit just before it is cleared could queue the work just after cancel_delayed_work_sync(). That is already so on main for iops_limit and kbps_limit, through the same __btrfs_discard_schedule_work(), so it isn't new here. Taking discard_ctl->lock around the clear in btrfs_discard_stop() would close it for all of them, as a separate patch.

The ABI document (Documentation/ABI/testing/sysfs-fs-btrfs) is still listed as not done. Approving.

@DjDeveloperr
DjDeveloperr merged commit 00c7aaf into aurora-silicon:main Dec 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants