Honest question: why are we still defaulting to swap partitions in Linux distributions?
@karolherbst hibernation?
@elly Sounds like a implementation limitation. Other OS besides Linux support hibernation and generally use swap files instead of a swap partition, no?

@karolherbst @elly aren't they less efficient?

(Also, with RAM going for scarcity again, looks like swap is going to have a comeback.)

@elly @karolherbst ngl I haven't used hibernation in a looong time. I totally forgot about it.

@elly @karolherbst You can hibernate to swap files, no need for partitions.

Heck, systemd can even allocate swap as needed.

My take: don't use swap partitions.

@mupuf @elly I think this breaks down once you do full disk encryption.

Though that also should be fixable, just needs to do the "let's use linux as the bootloader" thing :D

@karolherbst @elly it would break only if you ask the kernel to do the resume, but the modern way is for the initramfs to trigger the resume after unlocking the disk.
@mupuf @elly ahh, guess that's fine then.
@karolherbst @elly In boot2container, I have support for suspend/resume over NBD. Yes, Network Block Devices :D
@mupuf @elly .... this feels illegal

@karolherbst @elly Wait until I use it for preemption in CI jobs 🤣

Hopefully coming to your favourite gitlab instance this year!

@karolherbst IIUC (big caveat) using a swapfile instead of a dedicated partition doesn't work for hibernation due to a combination of issues with grub, kernel_lockdown security, and / or allocating a swapfile on CoW filesystems like Btrfs

but if you don't need hibernation, you can simplify your setup considerably. that's just not (yet?) the default

@decathorpe okay, but that does sound fixable? Though I can also understand if that wouldn't be trivial to solve at all.
@karolherbst yeah, it appears to be in the "fixable but not trivial" category
@decathorpe @karolherbst hibernation to a swapfile on a LUKS-encrypted Btrfs filesystem works without issues (I'm doing it on Fedora 43, with TPM automatic decryption too), *if* you turn off secure boot that in turn turns off kernel_lockdown which is the only thing that prevents it from working by default. There may be some security reason for that but I fail to see what it could be (the filesystem is encrypted so the hibernation image can't be changed)
In defence of swap: common misconceptions

tl;dr: Having swap is a reasonably important part of a well functioning system. Without it, sane memory management becomes harder to achieve.

@karolherbst we had to enable swap on our cluster at work, the problem was that users would occasionally try to compile a project with too many threads (make -j will do that), and that causes the machine to quickly run out of memory. Without swap, oom killers don't have enough time to actually kill those processes.

@karolherbst to answer the question with a question: what's the math for when swapfiles are effective? There seems to be a ratio of disk read and write speeds vs total RAM (or maybe total overcommit?) and disk latency.

Back in the day with only a few MB RAM, the higher relative disk speeds seemed to make the offset worthwhile. Maybe it's worth tuning in an age where RAM prices are through the roof.

@bakuninboys Well swap partitions kinda prevent a big swap, because nobody wants to waste 16GB on swap that they are barely using.

But if it's more dynamic, then the OS could allocate more swap if it wants to page out more things if they are never used.

And a file cache is often a better use of RAM than memory that's never used. So it could in theory even lower IO operations if more storage is made available as swap.

@karolherbst ah you're talking about swap partition vs swap file. Yeah I'm guessing the fs ops would be CPU bound so in the modern world you'd immediately make the trade.

I wonder if there is some specific handling for swap files needed by the fs. Eg: you update the swap file and the is driver just keeps the COW operation / journal in memory, defeating the purpose, or at least confounding the operation.