I have a raspberry pi which swaps to an external NAS (please don't judge me :) ) over CIFS (link is a direct 100Mbit full duplex Ethernet connection). Swap works fine until a certain threshold which is about 100MB. The system then freezes and network connection with NAS stops. Samba share is mounted with automount. Where do I start if I want to find out why the Pi stops using the swapfile? I dont mind the slowdown because I'm just compiling heavy programs but when it freezes I'm forced to reboot.
Just looking at kernel code, this shouldn't even let you swapon a file that's on a CIFS mount, so I'm rather surprised this works at all. There are two generic issues with swap on a networked filesystem:
- Swap on Linux requires a static block mapping. This is why swap files can't be sparse unless they use unwritten extents, and why you need a loop device to put a swap file on copy-on-write or log structured filesystems. You can work around this in theory using a loop device, but that should be a last resort.
- Some protocols or authentication setups require userspace components, and if those don't mlock() their entire address space, you may end up in a situation where you can't swap anything because you can't run the userspace code because you have no memory to load it into.
CIFS suffers from the first issue, and also the second if you are using authentication and the system does not maintain a constant connection.
You mentioned you're using a Pi, I'd suggest looking into using ZRAM, and possibly a local swap file (decent SD cards are inexpensive). You can even use a USB flash drive if need be instead of the SD card.
Alternatively, and more generically, if you have a reasonable NAS system, you could look into swapping via (in general order of preference) NBD, ATA over Ethernet, or iSCSI (which depending on how it's set up on the Pi may also suffer from the second limitation listed above).