Repository navigation
btrfs: expose async discard initial delays in sysfs - #4170
DjDeveloperr merged 1 commit into
Conversation
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>
|
Merge readiness at
Areas: none matched in CODEOWNERS Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
RyanTheTide
left a comment
There was a problem hiding this comment.
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.
DjDeveloperr
left a comment
There was a problem hiding this comment.
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_MSECand10000 * NSEC_PER_MSECare 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()runsbtrfs_discard_cleanup()beforebtrfs_sysfs_remove_mounted(). A store in that gap seesBTRFS_FS_DISCARD_RUNNINGcleared and queues nothing. A store that tests the bit just before it is cleared could queue the work just aftercancel_delayed_work_sync(). That is already so on main foriops_limitandkbps_limit, through the same__btrfs_discard_schedule_work(), so it isn't new here. Takingdiscard_ctl->lockaround the clear inbtrfs_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.
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:discard=asynccurrently holds freed extents in active data block groups for a hardcoded120 s(BTRFS_DISCARD_DELAY) and empty block groups for10 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.discard_delay_ms(default120000ms) anddiscard_unused_delay_ms(default10000ms) via standardBTRFS_ATTR_RW(bounded to0..86400000ms, zero module parameters).btrfs_discard_update_delay()stores the new delay underdiscard_ctl->lock, clamps already-queued block groups on the corresponding discard list(s) down to the new deadline, and reschedules discard work immediately.omarchy-mac-btrfs-discard.service, which tunesdiscard_delay_ms=1000anddiscard_unused_delay_ms=100strictly onnvme-apple-backed Btrfs filesystems).Tested:
06d4fcfa8ac3onorigin/main(62c60d13d4d8,0merge-tree conflicts):scripts/checkpatch.pl --strict --git origin/main..HEAD:0errors,0warnings,0checks (199lines checked).LLVM=1 W=1 KCFLAGS="-Werror":fs/btrfs/discard.o(260664bytes) andfs/btrfs/sysfs.o(464656bytes),0warnings,0errors.0module_paramadditions.linux-aurora 7.3.0.aurora1-1.1693and primary release shiplinux-aurora 7.3.0.aurora1-1.1694(957908b7bd99, boot29379f09, paired withomarchy-macPR net: apple: Add N1 protocol-27 station and data-ring consumer #251):/sys/fs/btrfs/<uuid>/discard/discard_delay_msanddiscard_unused_delay_msread/write cleanly at boot (1000/100ms applied byomarchy-mac-btrfs-discard.service).1.1694passed all boot, ESP, thermal (34.0 C,PMPEV=0), display/GPU (1640.8 fps), and storage/runtime health gates with0failed units and0kernel/storage faults.Risk: two-way door · all platforms (
fs/btrfs/{discard.c,discard.h,fs.h,sysfs.c},+116 / -8lines). Default delays are unchanged (120000ms /10000ms); behavior only changes when userspace explicitly writes the new per-filesystem sysfs attributes. A plain revert restores the compile-time constants.Not yet:
linux-btrfsmailing list andDocumentation/ABI/testing/sysfs-fs-btrfsupdate.origin/mainhead06d4fcfa8ac3separate fromiops_limit/kbps_limit/max_discard_sizetuning inomacom/omarchy-mac-pkgs#251.Details and verification
Scope (
j604/btrfs-async-discard-sysfs-delays, head06d4fcfa8ac3onmain62c60d13d4d8)fs/btrfs/discard.c(+45 / -8)fs/btrfs/discard.h(+8)fs/btrfs/fs.h(+2)fs/btrfs/sysfs.c(+61)Signed-off-by: Dylan Ironside <dylan@dylanironside.io>scripts/checkpatch.pl --strict --git origin/main..HEAD:0 errors, 0 warnings, 0 checks(199lines checked).git merge-tree --write-tree origin/main HEAD: clean (267b9bde95bd).LLVM=1 W=1 KCFLAGS="-Werror":fs/btrfs/discard.o(260664bytes),fs/btrfs/sysfs.o(464656bytes),0warnings.linux-aurora 1.1694, boot29379f09):discard_delay_ms=1000,discard_unused_delay_ms=100active viaomacom/omarchy-mac-pkgs#251,0failed units,0faults, G6PMPEV=0, G734.0 C, G5b PASS.