Swap on tmpfs (Obviously a bad idea, but is it possible?)

This question originated with a joke between co-workers about increasing performance by moving swap files to a tmpfs. Clearly even if this is possible, it’s not a good idea. All I want to know is, can it be done?

I’m currently on Ubuntu 14.04, but I’d imagine the process is similar for most Linux/Unix machines. Here’s what I’m doing:

> mkdir /mnt/tmp
> mount -t tmpfs -o size=10m tmpfs /mnt/tmp
> dd if=/dev/zero of=/mnt/tmp/swapfile bs=1024 count=10240
> chmod 600 /mnt/tmp/swapfile
> mkswap /mnt/tmp/swapfile
# So far, so good!

> swapon /mnt/tmp/swapfile
swapon: /mnt/tmp/swapfile: swapon failed: Invalid argument

So, on either linux or unix (I’m interested in any solution) can you somehow set up swap on a file/partition residing in ram? Is there a way around the Invalid argument error I’m getting above?

Again, just want to emphasize that I’m not expecting this to be a solution to a real-world problem. Just a fun experiment, I guess.

From this Q&A https://superuser.com/questions/539287/swapon-failed-invalid-argument-on-a-linux-system-with-btrfs-filesystem (the original referenced site is not responding):

So “Invalid argument” should be read as “Your filesystem do not
support swap file”

The incompatibility reason I suspect is a “circular dependency”. From this article: http://www.jamescoyle.net/knowledge/951-the-difference-between-a-tmpfs-and-ramfs-ram-disk:

These two differences between ramfs and tmpfs make tmpfs much more
manageable however this is one major drawback; tmpfs may use SWAP
space. If your system runs out of physical RAM, files in your tmpfs
partitions may be written to disk based SWAP partitions and will have
to be read from disk when the file is next accessed.

Which would be pretty much impossible in your scenario.

It might work with ramfs, tho – which doesn’t have this problem.

Answered By: Dan Cornilescu

It shouldn’t be possible. swapon system call requires readpage and bmap (indirectly) calls being implemented by filesystem:

http://lxr.free-electrons.com/source/mm/swapfile.c?v=4.0#L2412

if (!mapping->a_ops->readpage) {
    error = -EINVAL;
    goto bad_swap;
}   

But none of them are implemented by tmpfs, such an entry is missing from corresponding address_space_operations: http://lxr.free-electrons.com/source/mm/shmem.c?v=4.0#L3104

For the same reason, tmpfs cannot hold loop mounts, and ramfs won’t work either (it doesn’t have bmap call)

Answered By: myaut

So, on either linux or unix (I’m interested in any solution) can you
somehow set up swap on a file/partition residing in ram?

Sure. On FreeBSD:

# swapinfo -h
Device          1024-blocks     Used    Avail Capacity
/dev/mirror/swap.eli     4194300       0B     4.0G     0%

That shows that currently, I have a 4G encrypted swap partition with mirrored redundancy. I’ll add another 4G of non-redundant, non-encrypted swap:

First create a 4G RAM-backed “memory disk” (md) device:

# mdconfig -a -t malloc -s 4g; mdconfig -lv
md0
md0     malloc   4096M  -

Then tell swapon to add that to the pool of available swap devices, and swapinfo confirms that I now have 8G of swap:

# swapon /dev/md0; swapinfo -h
Device          1024-blocks     Used    Avail Capacity
/dev/mirror/swap.eli     4194300       0B     4.0G     0%
/dev/md0            4194304       0B     4.0G     0%
Total               8388604       0B     8.0G     0%
Answered By: Jim L.

You can easily avoid the issues with missing functions and swap to tmpfs, though a bit indirectly:

dd if=/dev/zero bs=1M of=/tmp/swapfs count=128
chmod 0600 /tmp/swapfs
mkfs.ext2 /tmp/swapfs
mount -o loop /tmp/swapfs /mnt
dd if=/dev/zero bs=1M of=/mnt/swap
mkswap /mnt/swap
swapon /mnt/swap

Combined with zswap it looked like a great “memory expander” solution till my RasPi4 froze almost completely (ssh console froze, X froze, youtube in chrome kept talking for several minutes and froze as well, but the system was replying to pings and watchdog didn’t do anything useful. Launched via ssh (before that has happened) atop didn’t show anything outstanding as well – there seemed to be enough free ram and swap space, In fact, similar idea with OpenSolaris (swap to a compressed zvol) lead to similar outcome.

Answered By: unisol
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.