"discard" mount option for swap partition?

I read here that:

If using an SSD with TRIM support, consider using discard in the swap line in fstab.

I have been reading here and elsewhere that “discard” is not to be used as mount option for SSD partitions. Is a swap partition to be regarded as an exception?

From man swapon

-d , –discard [ = policy ]

Enable swap discards, if the swap backing device supports the discard or trim operation. This may improve performance on some Solid State Devices, but often it does not. The option allows one to select between two available swap discard policies: –discard=once to perform a single-time discard operation for the whole swap area at swapon; or –discard=pages to asynchronously discard freed swap pages before they are available for reuse. If no policy is selected, the default behavior is to enable both discard types. The /etc/fstab mount options discard , discard=once , or discard=pages may also be used to enable discard flags.

I’ll be grateful for any explanation and comment.

If you use periodic TRIM (recommended) - disable discard in /etc/fstab


On swap no comments, coz i don’t use it :slight_smile:

2 Likes

Use of the discard option basically been deprecated.

From the Arck Wiki

Note: Continuous TRIM is not the most preferred way to issue TRIM commands among the Linux community. For example, Ubuntu enables periodic TRIM by default, Debian does not recommend using continuous TRIM and Red Hat recommends using periodic TRIM over using continuous TRIM if feasible.

You can do a search and find various explanations of why this is; just be sure to ignore the older search results.

3 Likes

I don’t have “discard” in fstab. I do use fstrim.timer/service. But that Wiki line for swap partition made me wonder.

1 Like

I don’t really follow the discussions on SSD swap partitions and trim, because I don’t use swap partitions. But I was under the impression that fstrim would trim a swap partition; I could be wrong, but I thought that was the case (although it hasn’t always been that way). It turns out that fstrim doesn’t trim a swap partition, but it will be handled by the linux kernel.

1 Like

Me too. I just got confounded a bit by reading somewhat “contradictory” advice.

Well, I found this:

… it appears that the Linux kernel does the trimming for the swap when it is mounted automatically when the underlying device supports the operation. This is referenced in the Fedora project docs:

The Linux swap code will issue TRIM commands to TRIM-enabled devices, and there is no option to control this behaviour.

That might be worth investigating.

2 Likes

Checking journalctl, I can see logs for fstrim only for two partitions, swap not included.

set 27 21:59:08 linux systemd[1]: fstrim.timer: Succeeded.
set 27 23:01:27 linux systemd[1]: fstrim.timer: Succeeded.
set 27 23:04:51 linux systemd[1]: fstrim.timer: Succeeded.
set 28 10:13:39 linux systemd[1]: fstrim.timer: Succeeded.
set 28 11:17:49 linux fstrim[11774]: /: 18,7 GiB (20074356736 bytes) descartado em /dev/sda5
set 28 11:17:49 linux fstrim[11774]: /boot/efi: 70,6 MiB (74048512 bytes) descartado em /dev/sda2
set 28 11:17:49 linux systemd[1]: fstrim.service: Succeeded.
[marcelo@linux ~]$ lsblk 
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 223,6G  0 disk 
├─sda1   8:1    0   529M  0 part 
├─sda2   8:2    0   100M  0 part /boot/efi
├─sda3   8:3    0    16M  0 part 
├─sda4   8:4    0 182,9G  0 part 
├─sda5   8:5    0    32G  0 part /
└─sda6   8:6    0     8G  0 part [SWAP]
sdb      8:16   0 931,5G  0 disk 
├─sdb1   8:17   0 731,5G  0 part /run/media/marcelo/Data
├─sdb2   8:18   0   170G  0 part /run/media/marcelo/Data2
└─sdb3   8:19   0    30G  0 part 
1 Like

When I run fstrim manually with -a flag:

sudo fstrim -av 
/boot/efi: 91.8 MiB (96235520 bytes) trimmed on /dev/nvme0n1p1
/: 39.9 GiB (42856509440 bytes) trimmed on /dev/nvme0n1p2

While, from man fstrim:

-a, --all
Trim all mounted filesystems on devices that support the discard operation. The other supplied options, like --off‐
set, --length and --minimum, are applied to all these devices. Errors from filesystems that do not support the dis‐
card operation, read-only devices and read-only filesystems are silently ignored.

The swap partition is mounted/enabled on the system but fstrim seems to ignore it.

As far as I know, fstrim doesn’t trim swap partitions.

However, I thought the kernel automatically managed trim in swap partitions without any user intervention needed. Is that not the case?

2 Likes

Yes, it’s during the swapon.
Take a look at man swapon.

-d section

1 Like

Then this must be the case. So no need for “discard” mount option for swap partition in fstab? Does the Wiki need some revision in this regard?

1 Like

I do have a side question here. Is there actually an advantage to enabling discard/trim on a swap partition?

1 Like

If it’s already performed during swapon, I don’t see why enabling is a good thing.
Just using fstrim and fstab without any discards…

1 Like

My only comment!

2 Likes

It beats me!
:blush:

1 Like

Yeah, I reacted to that as well. Just couldn’t really understand why it was suggested by ArchWiki.

1 Like

Lot’s of things are suggested. One has to look at the facts. Does it work? Is it reliable? Is it only certain hardware? etc…

When i see but often it does not. Tells me it does not mostly.

2 Likes

I take everything suggested in ArchWiki as “The Truth, the whole Truth, nothing but the Truth”. :wink: :sweat_smile:

2 Likes

So help you on Arch?

3 Likes