2

I'm writing an article about swapfiles. I decided to check how many swapfiles can I create and use. According to man 2 swapon, I know that it should be 29 - https://man7.org/linux/man-pages/man2/swapon.2.html. I checked my kernel configuration options with grep CONFIG_MIGRATION /boot/config-$(uname -r) and grep CONFIG_MEMORY_FAILURE /boot/config-$(uname -r). But when I wrote a simple script to check:

for i in {1..33}; do 
    SWAP_FILE="/swapfile-$i"
    sudo dd if=/dev/zero of=$SWAP_FILE bs=1M count=10
    sudo chmod 600 $SWAP_FILE
    sudo mkswap $SWAP_FILE
    sudo swapon $SWAP_FILE
done

I was able to activate only 27 of them.

[root@localhost ~]# swapon
NAME         TYPE SIZE USED PRIO
/swapfile-1  file  10M   0B   -2
(...)
/swapfile-27 file  10M   0B  -28

It means that there is probably something missing in the documentation.

I'm using EuroLinux 8 (just another RHEL 8 clone). But I tried it also on Fedora with identical results. I tried to get why this happened by looking into the swap.h kernel file (https://github.com/torvalds/linux/blob/master/include/linux/swap.h) but it made me even more confused as there is:

#ifdef CONFIG_DEVICE_PRIVATE
#define SWP_DEVICE_NUM 4
#define SWP_DEVICE_WRITE (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM)
#define SWP_DEVICE_READ (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM+1)
#define SWP_DEVICE_EXCLUSIVE_WRITE (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM+2)
#define SWP_DEVICE_EXCLUSIVE_READ (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM+3)
#else
#define SWP_DEVICE_NUM 0
#endif
#ifdef CONFIG_MIGRATION
#define SWP_MIGRATION_NUM 2
#define SWP_MIGRATION_READ  (MAX_SWAPFILES + SWP_HWPOISON_NUM)
#define SWP_MIGRATION_WRITE (MAX_SWAPFILES + SWP_HWPOISON_NUM + 1)
#else
#define SWP_MIGRATION_NUM 0
#endif
#ifdef CONFIG_MEMORY_FAILURE
#define SWP_HWPOISON_NUM 1
#define SWP_HWPOISON        MAX_SWAPFILES
#else
#define SWP_HWPOISON_NUM 0
#endif

#define MAX_SWAPFILES \
    ((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
    SWP_MIGRATION_NUM - SWP_HWPOISON_NUM)
  • (1<< SWAPFILES_SHIFT) = 32 (SWAPFILES_SHIFT=5)
  • SWP_DEVICE_NUM is 4 or 0
  • SWP_MIGRATION is 2 or 0
  • SWP_HWPOISON_NUM is 1 or 0

32 - X = 27 adds only when SWP_DEVICE_NUM and SWP_HWPOISON_NUM is used. At the very same time, the man 2 swapon says that:

Since kernel 2.6.18, the  limit  is  decreased  by  2
       (thus:  30)  if  the  kernel is built with the CONFIG_MIGRATION

And configs are set to yes.

[root@fedora ~]# grep CONFIG_MIGRATION /boot/config-$(uname -r)
CONFIG_MIGRATION=y
[root@fedora ~]# grep CONFIG_MEMORY_FAILURE  /boot/config-$(uname -r)
CONFIG_MEMORY_FAILURE=y

Any help in understanding why only 27 swaps are available is appreciated.

0

1 Answer 1

1

Absolutely nothing wrong with the docs.

Your misunderstanding simply comes from the fact you are basing your calculus on definitions found in swap.h taken from Linus' git, that is the swap.h related to the master branch, in other words, nowadays ahead of 5.17-rc-1

While making your experiments under RHEL 8 which, AFAIK, is based on Linux-4.18

In swap.h of 4.18.20 if we can still read the same formula :

#define MAX_SWAPFILES \
    ((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
    SWP_MIGRATION_NUM - SWP_HWPOISON_NUM)

SWP_MIGRATION_NUM is however taking a different value :

#define SWP_MIGRATION_NUM 2

2 instead of 4.

Applying this value to the formula taking in account your config settings gives:

MAX_SWAPFILES = 32 - 2 - 2 - 1 = 27

Justifying both the results of your experiments and the documentation.

2
  • 1
    I accepted your answer. Based on yours, I will write my own answer later because it was missing from the documentation. I submitted a patch on the kernel manpages list (lore.kernel.org/linux-man/…) - once merged, I create a new answer - but still, keep yours as the accepted one. Many thanks. Commented Jan 18, 2022 at 9:46
  • Thx @AlexBaranowski. Happy to have been of some help. Wishing success for your publication. BTW no worry!, I'm just too old for running after Kudos and badges ;-)
    – MC68020
    Commented Jan 18, 2022 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .