Linux – Cannot edit /proc/sys/kernel/threads-max


I am currently building a stress testing tool, and as such I need a pretty massive number of threads. I already went through all settings to raise the limit, but there is one last setting, the upper system-wide limit in /proc/sys/kernel/threads-max, that I can't seem to be able to change.

I tried

sysctl -w kernel.threads-max=200000

Editing manually with nano or echo

echo 200000 > /proc/sys/kernel/threads-max

Editing /etc/sysctl.conf and running

sysctl -f

If I run those as sudo, I have no error displayed (the new value is even displayed), but when checking again, the value hasn't changed either. When trying to edit the value with gedit, it spat an

invalid argument"

whatever the value I try, even the original one. I had no problem changing the pid_max value.

I really have no clue why it refuses my edits, and I haven't been able to find anybody with a similar problem, so I would be very grateful if someone could explain what is happening.

Best Answer

The repsonse lies in man proc(5), here is the interesting part:

 /proc/sys/kernel/threads-max (since Linux 2.3.11)
                     This file specifies the system-wide limit on the number
                     of threads (tasks) that can be created on the system.

                     Since Linux 4.1, the value that can be written to
                     threads-max is bounded.  The minimum value that can be
                     written is 20.  The maximum value that can be written
                     is given by the constant FUTEX_TID_MASK (0x3fffffff).
                     If a value outside of this range is written to threads-
                     max, the error EINVAL occurs.

                     The value written is checked against the available RAM
                     pages.  If the thread structures would occupy too much
                     (more than 1/8th) of the available RAM pages, threads-
                     max is reduced accordingly.

I assume your kernel version is > 4.1, so since 200000 (the number you try) is less than 0x3fffffff, the problem looks like the available RAM in not sufficient.