6

The question appears if we want to use a swap partition on an SSD drive. I think, it would be better (for the SSD) if the deallocated swap space could be trimmed, because trimming the not used blocks enables the underlying SSD electronics to tune its wear leveling.

Google seems silent on the matter.

Alternatively, a regular blkdiscard command (and re-creating the swap space with mkswap) would be a feasible workaround, although it would be sub-optimal.

peterh
  • 9,488
  • 16
  • 59
  • 88
  • Why do you believe that *"it would be obviously better (for the SSD) if the deallocated swap space would be trimmed"?* Quotation needed. If you have a heart-felt conviction that issuing TRIM commands to the swap file or partition is a Good and Holy Thing then you can always [add `discard` to the options](https://wiki.archlinux.org/index.php/swap) of the swap partition or file in `/etc/fstab`. – AlexP Dec 26 '17 at 15:04
  • @AlexP It is not a religious convinction, it is a rational knowledge, thus I am convincable with facts. To my best knowledge, trimming is useful because it enables the underlying SSD control electronic to optimize wear leveling of the drive. I inserted this into the post. Note: the drive has *no way* to know, which blocks are allocated and which are free. It can see only block read and write operations coming from your motherboard, and no more. It doesn't know the fs. The discard option what you suggest in the fstab, has only any effect if the filesystem (this time, linux swap) supports it. – peterh Dec 26 '17 at 15:10
  • 4
    Yes. `man swapon`, `/discard` – frostschutz Dec 26 '17 at 15:10
  • @frostschutz Oops! Sorry, it seems I checked it everywhere, except where it had been the most obvious :-) I made a self-answered post, for similar google-first mistakes, but I am considering to delete this question et al. Thank you very much! – peterh Dec 26 '17 at 15:13
  • The thing with swap is that the swap frames are *reused*, so the underlying firmware will see a write operation overwriting a previously written frame; this is enough for it to decide that the previous data is no longer useful. So if the swap area is lightly used TRIM doesn't matter, and if it is heavily used TRIM doesn't do anything which frame reuse won't do automatically. This is very different from some file systems, where the kernel tries hard to scatter the files all over the available space. – AlexP Dec 26 '17 at 15:18
  • @AlexP I think, the rarely used swap blocks, if they are trimmed, can be used by the ssd controller to decrease the load on other, highly used blocks (anywhere on the disk). Thus, I think it still helps wear leveling. Furthermore, all writing operation to a solid state media means in practice a trim-write cycle (because SSD blocks can't be rewritten, only trimmed). If the trim has happened already (just after the release), then I think it will result a performance improvement. – peterh Dec 26 '17 at 15:31

1 Answers1

10

Yes, as it is visible the manual of the swapon command, it has a discard option. It would be meaningless if the linux swap wouldn't support it. Of course the underlying device should support trimming.

peterh
  • 9,488
  • 16
  • 59
  • 88
  • Side note: usb block devices do not support trimming before usb 4.0! It is a disadvantage of the usb block device protocol. – peterh Feb 25 '21 at 15:23