Skip to contentSkip to conversation

tst_pids.c: fix fork failure on small memory systems

PATCH In review Leo Yu-Chi Liang 4 participants 7 messages Feb 7, 2023 – Feb 9, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tst_pids.c: fix fork failure on small memory systems

Running syscalls/msgstress03 on a system with less than ~1 GB of RAM fails:

msgstress03 1 TFAIL : msgstress03.c:163: Fork failed (may be OK if under stress)

The reason is that besides /proc/sys/kernel/pid_max, /proc/sys/kernel/threads-max is another factor determining how many processes a system could create.

Use the smaller number between pid_max and threads-max as the nprocs.

---
 lib/tst_pid.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/tst_pid.c +4 -1
index 21cadef2a..3fb3f39ad 100644
@@ -31,6 +31,7 @@
 #include "tst_safe_macros.h"
 
 #define PID_MAX_PATH "/proc/sys/kernel/pid_max"
+#define THREADS_MAX_PATH "/proc/sys/kernel/threads-max"
 #define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
 #define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
 /* Leave some available processes for the OS */
@@ -113,7 +114,7 @@ static int get_session_pids_limit(void (*cleanup_fn) (void))
 int tst_get_free_pids_(void (*cleanup_fn) (void))
 {
 	FILE *f;
-	int rc, used_pids, max_pids, max_session_pids;
+	int rc, used_pids, max_pids, max_session_pids, max_threads;
 
 	f = popen("ps -eT | wc -l", "r");
 	if (!f) {
@@ -129,6 +130,8 @@ int tst_get_free_pids_(void (*cleanup_fn) (void))
 	}
 
 	SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
+	SAFE_FILE_SCANF(cleanup_fn, THREADS_MAX_PATH, "%d", &max_threads);
+	max_pids = (max_pids < max_threads) ? max_pids : max_threads;
 
 	max_session_pids = get_session_pids_limit(cleanup_fn);
 	if ((max_session_pids > 0) && (max_session_pids < max_pids))
Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hi Leo,

Quoted 8 lines

Running syscalls/msgstress03 on a system with less than ~1 GB of RAM fails:

msgstress03 1 TFAIL : msgstress03.c:163: Fork failed (may be OK if under stress)

The reason is that besides /proc/sys/kernel/pid_max, /proc/sys/kernel/threads-max is another factor determining how many processes a system could create.

Use the smaller number between pid_max and threads-max as the nprocs.

Makes sense.

BTW IMHO it'd be better to rewrite
f = popen("ps -eT | wc -l", "r");
to list count of /proc/[0-9]*/ in plain C (TODO for us).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by
#2 lore raw
Quoted 7 lines · Petr Vorel

Hi Leo,

Quoted 3 lines

Running syscalls/msgstress03 on a system with less than ~1 GB of RAM fails:

msgstress03 1 TFAIL : msgstress03.c:163: Fork failed (may be OK if under stress)

Quoted 8 lines
Quoted 4 lines

The reason is that besides /proc/sys/kernel/pid_max, /proc/sys/kernel/threads-max is another factor determining how many processes a system could create.

Use the smaller number between pid_max and threads-max as the nprocs.

Makes sense.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw
In reply to Petr Vorel

Hi Petr,

Quoted 9 lines · Petr Vorel

Hi Leo,

Quoted 6 lines

Running syscalls/msgstress03 on a system with less than ~1 GB of RAM fails:

msgstress03 1 TFAIL : msgstress03.c:163: Fork failed (may be OK if under stress)

The reason is that besides /proc/sys/kernel/pid_max, /proc/sys/kernel/threads-max is another factor determining how many processes a system could create.

Quoted 9 lines
Quoted 1 line

Use the smaller number between pid_max and threads-max as the nprocs.

Makes sense.

BTW IMHO it'd be better to rewrite
f = popen("ps -eT | wc -l", "r");
to list count of /proc/[0-9]*/ in plain C (TODO for us).

Thanks for the review!
I could send a v2 to add this as well.

Best regards,
Leo

Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi all,

Thanks Leo, I merged it with MIN(max_pids, max_threads), thanks Cyril for catching it.

Quoted 7 lines
Quoted 3 lines

BTW IMHO it'd be better to rewrite
f = popen("ps -eT | wc -l", "r");
to list count of /proc/[0-9]*/ in plain C (TODO for us).

Thanks for the review!
I could send a v2 to add this as well.

Thank you, if you have time to send another patch to fix.

Kind regards,
Petr

Quoted 2 lines

Best regards,
Leo

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#5 lore raw
In reply to Leo Yu-Chi Liang

Hi!

Quoted 25 lines
 lib/tst_pid.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/tst_pid.c +2 -1
index 21cadef2a..3fb3f39ad 100644
@@ -31,6 +31,7 @@
 #include "tst_safe_macros.h"
 
 #define PID_MAX_PATH "/proc/sys/kernel/pid_max"
+#define THREADS_MAX_PATH "/proc/sys/kernel/threads-max"
 #define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
 #define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
 /* Leave some available processes for the OS */
@@ -113,7 +114,7 @@ static int get_session_pids_limit(void (*cleanup_fn) (void))
 int tst_get_free_pids_(void (*cleanup_fn) (void))
 {
 	FILE *f;
-	int rc, used_pids, max_pids, max_session_pids;
+	int rc, used_pids, max_pids, max_session_pids, max_threads;
 
 	f = popen("ps -eT | wc -l", "r");
 	if (!f) {

lib/tst_pid.c int tst_get_free_pids_(void (*cleanup_fn) (void)) · line 134

Quoted 5 lines
lib/tst_pid.c +2 -0
@@ -129,6 +130,8 @@ int tst_get_free_pids_(void (*cleanup_fn) (void))
 	}
 
 	SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
+	SAFE_FILE_SCANF(cleanup_fn, THREADS_MAX_PATH, "%d", &max_threads);
+	max_pids = (max_pids < max_threads) ? max_pids : max_threads;

BTW: We do have a MIN() macro definition in tst_minmax.h header so we

     can just do max_pids = MIN(max_pids, max_threads);

lib/tst_pid.c int tst_get_free_pids_(void (*cleanup_fn) (void)) · line 137

Quoted 2 lines
lib/tst_pid.c +0 -0
 	max_session_pids = get_session_pids_limit(cleanup_fn);
 	if ((max_session_pids > 0) && (max_session_pids < max_pids))

Otherwise:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#6 lore raw

Hi Cyril,

Quoted 28 lines · Cyril Hrubis

Hi!

Quoted 27 lines
 lib/tst_pid.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/tst_pid.c +2 -1
index 21cadef2a..3fb3f39ad 100644
@@ -31,6 +31,7 @@
 #include "tst_safe_macros.h"
 
 #define PID_MAX_PATH "/proc/sys/kernel/pid_max"
+#define THREADS_MAX_PATH "/proc/sys/kernel/threads-max"
 #define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
 #define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
 /* Leave some available processes for the OS */
@@ -113,7 +114,7 @@ static int get_session_pids_limit(void (*cleanup_fn) (void))
 int tst_get_free_pids_(void (*cleanup_fn) (void))
 {
 	FILE *f;
-	int rc, used_pids, max_pids, max_session_pids;
+	int rc, used_pids, max_pids, max_session_pids, max_threads;
 
 	f = popen("ps -eT | wc -l", "r");
 	if (!f) {
@@ -129,6 +130,8 @@ int tst_get_free_pids_(void (*cleanup_fn) (void))
 	}
 
Quoted 7 lines

lib/tst_pid.c int tst_get_free_pids_(void (*cleanup_fn) (void)) · line 134

Quoted 3 lines
lib/tst_pid.c +2 -0
 	SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
+	SAFE_FILE_SCANF(cleanup_fn, THREADS_MAX_PATH, "%d", &max_threads);
+	max_pids = (max_pids < max_threads) ? max_pids : max_threads;

BTW: We do have a MIN() macro definition in tst_minmax.h header so we

     can just do max_pids = MIN(max_pids, max_threads);

Thanks for the review!
Will send a v2 to fix this!

Best regards,
Leo

Quoted 10 lines

lib/tst_pid.c int tst_get_free_pids_(void (*cleanup_fn) (void)) · line 137

Quoted 2 lines
lib/tst_pid.c +0 -0
 	max_session_pids = get_session_pids_limit(cleanup_fn);
 	if ((max_session_pids > 0) && (max_session_pids < max_pids))

Otherwise:

Signature · 2 lines
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp