[LTP] [PATCH v2] lib: Do not fail a test if oom score cannot be adjusted.

Cyril Hrubis chrubis@suse.cz
Wed Dec 22 14:52:34 CET 2021


From: Petr Vorel <pvorel@suse.cz>

Setting value < 0 in /proc/*/oom_score_adj requires CAP_SYS_RESOURCE or
CAP_SYS_ADMIN. However setting the library process score is a best
effort operation, so let's skip it silently when the user is not
privileged to do so.

Fixes: 8a0827766d ("lib: add functions to adjust oom score")
Signed-off-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_memutils.h |  6 ++++-
 lib/tst_memutils.c     | 55 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/include/tst_memutils.h b/include/tst_memutils.h
index 68a6e3771..855c6f289 100644
--- a/include/tst_memutils.h
+++ b/include/tst_memutils.h
@@ -28,13 +28,17 @@ 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
+ *
  * If the pid is 0 which means it will set on current(self) process.
  *
+ *  Unless the process has CAP_SYS_RESOURCE or CAP_SYS_ADMIN this call will be
+ *  no-op because setting adj value < 0 requires it.
+ *
  * 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_disable_oom_protection
+ *  ignored by OOM Killer as well. So that's why tst_disable_oom_protection()
  *  to be used in combination.
  */
 void tst_enable_oom_protection(pid_t pid);
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
index 4dea30330..4a47bbb33 100644
--- a/lib/tst_memutils.c
+++ b/lib/tst_memutils.c
@@ -11,6 +11,8 @@
 
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
+#include "tst_capability.h"
+#include "lapi/syscalls.h"
 
 #define BLOCKSIZE (16 * 1024 * 1024)
 
@@ -93,6 +95,42 @@ long long tst_available_mem(void)
 	return mem_available;
 }
 
+static int has_caps(void)
+{
+	struct tst_cap_user_header hdr = {
+		.version = 0x20080522,
+		.pid = tst_syscall(__NR_gettid),
+	};
+
+	struct tst_cap_user_data caps[2];
+
+	if (tst_capget(&hdr, caps))
+		tst_brk(TBROK | TERRNO, "tst_capget()");
+
+	if ((caps[0].effective & (1U << CAP_SYS_ADMIN)) ||
+	    (caps[0].effective & (1U << CAP_SYS_RESOURCE)))
+		return 1;
+
+	return 0;
+}
+
+static int write_score(const char *path, int score)
+{
+	FILE *f;
+
+	f = fopen(path, "w");
+	if (!f)
+		return 1;
+
+	if (fprintf(f, "%d", score) <= 0)
+		return 1;
+
+	if (fclose(f))
+		return 1;
+
+	return 0;
+}
+
 static void set_oom_score_adj(pid_t pid, int value)
 {
 	int val;
@@ -111,17 +149,18 @@ static void set_oom_score_adj(pid_t pid, int value)
 			tst_brk(TBROK, "%s does not exist, please check if PID is valid", score_path);
 	}
 
-	FILE_PRINTF(score_path, "%d", value);
+	if (write_score(score_path, value)) {
+		if (!has_caps())
+			return;
+
+		tst_res(TWARN, "Can't adjust score, even with capabilities!?");
+		return;
+	}
+
 	FILE_SCANF(score_path, "%d", &val);
 
-	if (val != value) {
-		if (value < 0) {
-			tst_res(TWARN, "'%s' cannot be set to %i, are you root?",
-				score_path, value);
-			return;
-		}
+	if (val != value)
 		tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
-	}
 }
 
 void tst_enable_oom_protection(pid_t pid)
-- 
2.32.0



More information about the ltp mailing list