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.

  • 2
    I'm surprised swap to CIFS works at all... Swap over network is hard w/o deadlock... Any chance you can use NBD? That is supposed to work. – derobert Aug 22 '17 at 18:32
  • It "worked" because I set up a loop device, tricking the operating system. NBD is not a choice because my NAS is an ARM and pretty closed as far as firmware is concerned – Warrior Aug 22 '17 at 23:15
  • If you can't use any of Austin Hemmelgarn's suggestions, you can probably use a separate machine to run a NBD server, with that server writing to the CIFS share... Then you can do swap on NBD. NBD servers are available for just about anything, even Windows. (Performance will no doubt be terrible. I suggest a USB disk attached to the Pi.) – derobert Aug 23 '17 at 1:11
  • I tried NFS and it works perfectly. No more freezing. – Warrior Aug 23 '17 at 19:30

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:

  1. 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.
  2. 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).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.