Skip to content

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

Open
IronsideXXVI wants to merge 1 commit into
aurora-silicon:mainfrom
IronsideXXVI:j604/btrfs-async-discard-sysfs-delays
Open

IronsideXXVI wants to merge 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

This branch has not been deployed

No deployments
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.

2 participants