All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score
@ 2021-12-17 11:37 Li Wang
  2021-12-17 11:37 ` [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process Li Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Li Wang @ 2021-12-17 11:37 UTC (permalink / raw)
  To: ltp

This introduces function to LTP for adjusting the oom_score_adj of
target process, which may be helpful in OOM tests to prevent kernel
killing the main or lib process during test running.

The exported global tst_enable_oom_protection function can be used
at anywhere you want to protect, but please remember that if you
do enable protection on a process($PID) that all the children will
inherit its score and be ignored by OOM Killer as well. So that's
why tst_cancel_oom_protection is recommended to combination in use.

Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---

Notes:
    Changes v1->v2:
        * Move commit messages to source code comment
        * correct typos

 include/tst_memutils.h | 19 +++++++++++++++++++
 lib/tst_memutils.c     | 29 +++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/include/tst_memutils.h b/include/tst_memutils.h
index f605f544e..dc18df6d2 100644
--- a/include/tst_memutils.h
+++ b/include/tst_memutils.h
@@ -25,4 +25,23 @@ void tst_pollute_memory(size_t maxsize, int fillchar);
  */
 long long tst_available_mem(void);
 
+/*
+ * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
+ *   echo -1000 >/proc/$PID/oom_score_adj
+ *
+ * Note:
+ *  This exported tst_enable_oom_protection function can be used at anywhere
+ *  you want to protect, but please remember that if you do enable protection
+ *  on a process($PID) that all the children will inherit its score and be
+ *  ignored by OOM Killer as well. So that's why tst_cancel_oom_protection is
+ *  recommended to combination in use.
+ */
+void tst_enable_oom_protection(pid_t pid);
+
+/*
+ * Cancel the OOM protection for the process($PID).
+ *   echo 0 >/proc/$PID/oom_score_adj
+ */
+void tst_cancel_oom_protection(pid_t pid);
+
 #endif /* TST_MEMUTILS_H__ */
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
index bd09cf6fa..d97b35007 100644
--- a/lib/tst_memutils.c
+++ b/lib/tst_memutils.c
@@ -3,6 +3,7 @@
  * Copyright (c) 2020 SUSE LLC <mdoucha@suse.cz>
  */
 
+#include <stdio.h>
 #include <unistd.h>
 #include <limits.h>
 #include <sys/sysinfo.h>
@@ -91,3 +92,31 @@ long long tst_available_mem(void)
 
 	return mem_available;
 }
+
+static void set_oom_score_adj(pid_t pid, int value)
+{
+	int val;
+	char score_path[64];
+
+	if (access("/proc/self/oom_score_adj", F_OK) == -1) {
+		tst_res(TINFO, "Warning: oom_score_adj does not exist");
+		return;
+	}
+
+	sprintf(score_path, "/proc/%d/oom_score_adj", pid);
+	SAFE_FILE_PRINTF(score_path, "%d", value);
+
+	SAFE_FILE_SCANF(score_path, "%d", &val);
+	if (val != value)
+		tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
+}
+
+void tst_enable_oom_protection(pid_t pid)
+{
+	set_oom_score_adj(pid, -1000);
+}
+
+void tst_cancel_oom_protection(pid_t pid)
+{
+	set_oom_score_adj(pid, 0);
+}
-- 
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process
  2021-12-17 11:37 [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Li Wang
@ 2021-12-17 11:37 ` Li Wang
  2021-12-17 14:49   ` Cyril Hrubis
  2021-12-17 11:37 ` [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process Li Wang
  2021-12-17 14:36 ` [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Cyril Hrubis
  2 siblings, 1 reply; 7+ messages in thread
From: Li Wang @ 2021-12-17 11:37 UTC (permalink / raw)
  To: ltp

Here invoke OOM protection in fork_testrun, since it is the key point
to distiguish many process branches. We do protect main ($PID) process
from killing by OOM Killer, hope this can help to get the completed
correct report for all of LTP tests.

Fundamental principle:

                   ltp test harness --> library process
  (oom protection)   main --> tst_run_tcases --> ... --> fork_testrun
  (cancel protection)  testrun --> run_tests --> ... --> testname
                         child_test --> ... --> end

Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---

Notes:
    Changes v1->v2:
        * remove the protection for parent process as suggested by Martin

 lib/tst_test.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/tst_test.c b/lib/tst_test.c
index ce2b8239d..0b370e691 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -1441,11 +1441,14 @@ static int fork_testrun(void)
 
 	SAFE_SIGNAL(SIGINT, sigint_handler);
 
+	tst_enable_oom_protection(getpid());
+
 	test_pid = fork();
 	if (test_pid < 0)
 		tst_brk(TBROK | TERRNO, "fork()");
 
 	if (!test_pid) {
+		tst_cancel_oom_protection(getpid());
 		SAFE_SIGNAL(SIGALRM, SIG_DFL);
 		SAFE_SIGNAL(SIGUSR1, SIG_DFL);
 		SAFE_SIGNAL(SIGINT, SIG_DFL);
-- 
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process
  2021-12-17 11:37 [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Li Wang
  2021-12-17 11:37 ` [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process Li Wang
@ 2021-12-17 11:37 ` Li Wang
  2021-12-17 14:51   ` Cyril Hrubis
  2021-12-17 14:36 ` [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Cyril Hrubis
  2 siblings, 1 reply; 7+ messages in thread
From: Li Wang @ 2021-12-17 11:37 UTC (permalink / raw)
  To: ltp

Just simply invoke oom protection on mem library to make
it can collect full state of children.

Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/kernel/mem/lib/mem.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index ac890491c..566e29055 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -129,8 +129,11 @@ void oom(int testcase, int lite, int retcode, int allow_sigkill)
 	pid_t pid;
 	int status, threads;
 
+	tst_enable_oom_protection(getpid());
+
 	switch (pid = SAFE_FORK()) {
 	case 0:
+		tst_cancel_oom_protection(getpid());
 		threads = MAX(1, tst_ncpus() - 1);
 		child_alloc(testcase, lite, threads);
 	default:
-- 
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score
  2021-12-17 11:37 [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Li Wang
  2021-12-17 11:37 ` [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process Li Wang
  2021-12-17 11:37 ` [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process Li Wang
@ 2021-12-17 14:36 ` Cyril Hrubis
  2 siblings, 0 replies; 7+ messages in thread
From: Cyril Hrubis @ 2021-12-17 14:36 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> +/*
> + * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
> + *   echo -1000 >/proc/$PID/oom_score_adj
> + *
> + * Note:
> + *  This exported tst_enable_oom_protection function can be used at anywhere
> + *  you want to protect, but please remember that if you do enable protection
> + *  on a process($PID) that all the children will inherit its score and be
> + *  ignored by OOM Killer as well. So that's why tst_cancel_oom_protection is
> + *  recommended to combination in use.
> + */
> +void tst_enable_oom_protection(pid_t pid);
> +
> +/*
> + * Cancel the OOM protection for the process($PID).
> + *   echo 0 >/proc/$PID/oom_score_adj
> + */
> +void tst_cancel_oom_protection(pid_t pid);

Minor nit: opposite of enable is disable not cancel.

>  #endif /* TST_MEMUTILS_H__ */
> diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
> index bd09cf6fa..d97b35007 100644
> --- a/lib/tst_memutils.c
> +++ b/lib/tst_memutils.c
> @@ -3,6 +3,7 @@
>   * Copyright (c) 2020 SUSE LLC <mdoucha@suse.cz>
>   */
>  
> +#include <stdio.h>
>  #include <unistd.h>
>  #include <limits.h>
>  #include <sys/sysinfo.h>
> @@ -91,3 +92,31 @@ long long tst_available_mem(void)
>  
>  	return mem_available;
>  }
> +
> +static void set_oom_score_adj(pid_t pid, int value)
> +{
> +	int val;
> +	char score_path[64];
> +
> +	if (access("/proc/self/oom_score_adj", F_OK) == -1) {
> +		tst_res(TINFO, "Warning: oom_score_adj does not exist");
> +		return;
> +	}
> +
> +	sprintf(score_path, "/proc/%d/oom_score_adj", pid);
> +	SAFE_FILE_PRINTF(score_path, "%d", value);
> +
> +	SAFE_FILE_SCANF(score_path, "%d", &val);
> +	if (val != value)
> +		tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
> +}
> +
> +void tst_enable_oom_protection(pid_t pid)
> +{
> +	set_oom_score_adj(pid, -1000);
> +}
> +
> +void tst_cancel_oom_protection(pid_t pid)
> +{
> +	set_oom_score_adj(pid, 0);
> +}
> -- 
> 2.31.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process
  2021-12-17 11:37 ` [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process Li Wang
@ 2021-12-17 14:49   ` Cyril Hrubis
  0 siblings, 0 replies; 7+ messages in thread
From: Cyril Hrubis @ 2021-12-17 14:49 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> Here invoke OOM protection in fork_testrun, since it is the key point
> to distiguish many process branches. We do protect main ($PID) process
> from killing by OOM Killer, hope this can help to get the completed
> correct report for all of LTP tests.
> 
> Fundamental principle:
> 
>                    ltp test harness --> library process
>   (oom protection)   main --> tst_run_tcases --> ... --> fork_testrun
>   (cancel protection)  testrun --> run_tests --> ... --> testname
>                          child_test --> ... --> end
> 
> Signed-off-by: Li Wang <liwang@redhat.com>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
> ---
> 
> Notes:
>     Changes v1->v2:
>         * remove the protection for parent process as suggested by Martin
> 
>  lib/tst_test.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/lib/tst_test.c b/lib/tst_test.c
> index ce2b8239d..0b370e691 100644
> --- a/lib/tst_test.c
> +++ b/lib/tst_test.c
> @@ -1441,11 +1441,14 @@ static int fork_testrun(void)
>  
>  	SAFE_SIGNAL(SIGINT, sigint_handler);
>  
> +	tst_enable_oom_protection(getpid());
> +
>  	test_pid = fork();
>  	if (test_pid < 0)
>  		tst_brk(TBROK | TERRNO, "fork()");
>  
>  	if (!test_pid) {
> +		tst_cancel_oom_protection(getpid());
>  		SAFE_SIGNAL(SIGALRM, SIG_DFL);
>  		SAFE_SIGNAL(SIGUSR1, SIG_DFL);
>  		SAFE_SIGNAL(SIGINT, SIG_DFL);

The fork_testrun() function is called in a loop, it would be a bit
cleaner to put the tst_enable_protection() call to the tst_run_tcases()
instead since we don't have to enable it over and over.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process
  2021-12-17 11:37 ` [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process Li Wang
@ 2021-12-17 14:51   ` Cyril Hrubis
  2021-12-20  5:01     ` Li Wang
  0 siblings, 1 reply; 7+ messages in thread
From: Cyril Hrubis @ 2021-12-17 14:51 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> Just simply invoke oom protection on mem library to make
> it can collect full state of children.
> 
> Signed-off-by: Li Wang <liwang@redhat.com>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
> ---
>  testcases/kernel/mem/lib/mem.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
> index ac890491c..566e29055 100644
> --- a/testcases/kernel/mem/lib/mem.c
> +++ b/testcases/kernel/mem/lib/mem.c
> @@ -129,8 +129,11 @@ void oom(int testcase, int lite, int retcode, int allow_sigkill)
>  	pid_t pid;
>  	int status, threads;
>  
> +	tst_enable_oom_protection(getpid());
> +
>  	switch (pid = SAFE_FORK()) {
>  	case 0:
> +		tst_cancel_oom_protection(getpid());
>  		threads = MAX(1, tst_ncpus() - 1);
>  		child_alloc(testcase, lite, threads);
>  	default:

Looking at the code we have, we always pass getpid(). We may as well
change the library to use /proc/self/oom_score_adj if 0 is passed
instead of the pid.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process
  2021-12-17 14:51   ` Cyril Hrubis
@ 2021-12-20  5:01     ` Li Wang
  0 siblings, 0 replies; 7+ messages in thread
From: Li Wang @ 2021-12-20  5:01 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp


[-- Attachment #1.1: Type: text/plain, Size: 619 bytes --]

On Fri, Dec 17, 2021 at 10:49 PM Cyril Hrubis <chrubis@suse.cz> wrote:


>
> > +     tst_enable_oom_protection(getpid());
> > +
> >       switch (pid = SAFE_FORK()) {
> >       case 0:
> > +             tst_cancel_oom_protection(getpid());
> >               threads = MAX(1, tst_ncpus() - 1);
> >               child_alloc(testcase, lite, threads);
> >       default:
>
> Looking at the code we have, we always pass getpid(). We may as well
> change the library to use /proc/self/oom_score_adj if 0 is passed
> instead of the pid.
>

Yes, agree with all suggestions (include comments in 1/3 2/3).

-- 
Regards,
Li Wang

[-- Attachment #1.2: Type: text/html, Size: 1273 bytes --]

[-- Attachment #2: Type: text/plain, Size: 60 bytes --]


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-12-20  5:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-17 11:37 [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Li Wang
2021-12-17 11:37 ` [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process Li Wang
2021-12-17 14:49   ` Cyril Hrubis
2021-12-17 11:37 ` [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process Li Wang
2021-12-17 14:51   ` Cyril Hrubis
2021-12-20  5:01     ` Li Wang
2021-12-17 14:36 ` [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.