Skip to content

Support discard in "block" and "swapon"#13

Open
mstorchak wants to merge 4 commits into
openwrt:masterfrom
mstorchak:master
Open

Support discard in "block" and "swapon"#13
mstorchak wants to merge 4 commits into
openwrt:masterfrom
mstorchak:master

Conversation

@mstorchak

Copy link
Copy Markdown

SSD drives are quite common, so following their rules (discarding the unused blocks) can extend their life time. In addition, even if the device doesn't support discard, kernel can always "downgrade" the flags.

This PR adds discard options to block by extending the swap entries of fstab config with the discard parameter that provides full control over the discard feature.
swapon is another entry point for swap activation, so it gets an extra flag -d. The implementation is inspired by busybox.

@mstorchak

Copy link
Copy Markdown
Author

Define swap flags according to the kernel definitions:
musl and glibc provide SWAP_FLAG_DISCARD, but not discard policies.
Based on busybox's swaponoff.c

Signed-off-by: Maxim Storchak <m.storchak@gmail.com>
Intorduce a new fstab.@swap[] attribute, "discard" that can take values
- on, 1, yes -- to enable the default discard mode
- once, pages -- to enable the corresponding discard policy
- anythinge else -- to disable discard
Discard is enabled by default.

Signed-off-by: Maxim Storchak <m.storchak@gmail.com>
Add a new option, -d, with an optional argument "once" or "pages".
-d with no arguments uses the default kernel discarding policy which enables
both the initial discard (once) and discards the released swap pages.
By default discard is disabled.
Inspired by busybox's swaponoff.c

Signed-off-by: Maxim Storchak <m.storchak@gmail.com>
It's safe to enable discard on the devices that don't support it.
In such case kernel ignores the discard flags.

Signed-off-by: Maxim Storchak <m.storchak@gmail.com>
@dangowrt

dangowrt commented Jun 2, 2026

Copy link
Copy Markdown
Member

Thank you for your work on this feature, and sorry for the delay in dealing with your contribution. As the kernel gracefully handles devices which do support discard, imho it should be enabled by default. If at all, then we should have an opt-out option in /etc/config/fstab or as parameter for swapon. What do you think?

@mstorchak

mstorchak commented Jun 2, 2026

Copy link
Copy Markdown
Author

@dangowrt , thank you for getting back to me!
Both busybox and util-linux treat discard as an opt-in swap feature. To introduce the least surprise, I'd rather keep it as opt-in here too.

If you still think it should be opt-out, I can update the PR.

the following doesn't apply to swaps

In fact, the same is true for fstab: it's an opt-in feature. Note that if the filesystem has discard mount flag, block mount honors it, even without a hint in fstab. This is not visible in /proc/mounts, but there is indirect evidence in dmesg

[220188.782941] EXT4-fs (dm-1): mounting with "discard" option, but the device does not support discard
[220188.783106] EXT4-fs (dm-1): mounted filesystem 0124413e-fc2a-4e03-8f9d-e05e41b46600 r/w with ordered data mode. Quota mode: disabled.

dm-1 is located on hdd can doesn't support discard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants