From 4367d0634f50a0ffd77a6d7b58b94933aef6ce92 Mon Sep 17 00:00:00 2001 From: The412Banner Date: Thu, 15 Oct 2026 19:47:18 -0400 Subject: [PATCH] Session preload: drop oom_score_adj raises so lmkd does not treat games as cached apps steam-launch-wrapper starts every game with --oom-score-adjust 900; on Android 900 is CACHED_APP_MIN_ADJ, so lmkd kills the running game first when memory runs short (and on Linux 6.1 the raise becomes the process's permanent minimum). A process in the app cannot go below the score it inherits, so the writes that succeed from inside are raises: every write-open of /proc/[/task/]/oom_score_adj (or oom_adj), through open/openat or fopen, goes to /dev/null, logged once. Based on the oom_score_adj redirect in the preload's opens.c by jdmg28 (https://github.com/jdmg28/DroidDeck-Mali/commit/6be00e00) --- tools/linuxfs/preload/netif.c | 3 ++ tools/linuxfs/preload/oomadj.c | 47 +++++++++++++++++++++++++++++ tools/linuxfs/preload/opens.c | 3 ++ tools/tests/test_oom_adj_preload.py | 47 +++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 tools/linuxfs/preload/oomadj.c create mode 100644 tools/tests/test_oom_adj_preload.py diff --git a/tools/linuxfs/preload/netif.c b/tools/linuxfs/preload/netif.c index bcd12e924f..f780ff449e 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -537,6 +537,8 @@ static FILE *synthesize(const char *table) { int bl_status_without_tracer(const char *path, int flags) __attribute__((visibility("hidden"))); +const char *bl_oom_adj_redirect(const char *path, int writing) __attribute__((visibility("hidden"))); + static FILE *open_stream(fopen_fn real, const char *path, const char *mode) { int status = mode && mode[0] == 'r' && !strchr(mode, '+') ? bl_status_without_tracer(path, O_RDONLY | O_CLOEXEC) : -1; @@ -545,6 +547,7 @@ static FILE *open_stream(fopen_fn real, const char *path, const char *mode) { if (copy == NULL) close(status); return copy; } + path = bl_oom_adj_redirect(path, mode && (mode[0] == 'w' || mode[0] == 'a' || strchr(mode, '+'))); FILE *f = real(path, mode); if (f || path == NULL || mode == NULL || mode[0] != 'r') return f; int saved = errno; diff --git a/tools/linuxfs/preload/oomadj.c b/tools/linuxfs/preload/oomadj.c new file mode 100644 index 0000000000..19e5f4251d --- /dev/null +++ b/tools/linuxfs/preload/oomadj.c @@ -0,0 +1,47 @@ +/* + * Based on jdmg28's DroidDeck-Mali fork (commit 6be00e00, GPL-3.0): steam-launch-wrapper runs every + * game with --oom-score-adjust 900. Android's lmkd reads 900 as CACHED_APP_MIN_ADJ and kills the + * foreground game first when memory runs short; on Linux 6.1 the raise also becomes the process's + * permanent minimum. An app's processes cannot go below the score they inherit, so the only writes + * that succeed from in here are raises: every write-open of an oom_score_adj file goes to /dev/null. + */ +#define _GNU_SOURCE +#include +#include +#include + +static int is_oom_score_adj(const char *path) { + if (path == NULL || strncmp(path, "/proc/", 6) != 0) return 0; + const char *p = path + 6; + if (strncmp(p, "self/", 5) == 0) { + p += 5; + } else if (strncmp(p, "thread-self/", 12) == 0) { + p += 12; + } else { + const char *digits = p; + while (*p >= '0' && *p <= '9') p++; + if (p == digits || *p != '/') return 0; + p++; + if (strncmp(p, "task/", 5) == 0) { + p += 5; + digits = p; + while (*p >= '0' && *p <= '9') p++; + if (p == digits || *p != '/') return 0; + p++; + } + } + return strcmp(p, "oom_score_adj") == 0 || strcmp(p, "oom_adj") == 0; +} + +__attribute__((visibility("hidden"))) const char *bl_oom_adj_redirect(const char *path, int writing) { + static int logged; + if (!writing || !is_oom_score_adj(path)) return path; + if (!__atomic_exchange_n(&logged, 1, __ATOMIC_RELAXED)) { + static const char msg[] = "droiddeck: an oom_score_adj raise was dropped (Android would treat this program as a cached app)\n"; + int saved = errno; + ssize_t ignored = write(STDERR_FILENO, msg, sizeof(msg) - 1); + (void)ignored; + errno = saved; + } + return "/dev/null"; +} diff --git a/tools/linuxfs/preload/opens.c b/tools/linuxfs/preload/opens.c index 7c0e766d2e..a2d7ea15d5 100644 --- a/tools/linuxfs/preload/opens.c +++ b/tools/linuxfs/preload/opens.c @@ -14,6 +14,7 @@ int bl_status_without_tracer(const char *path, int flags) __attribute__((visibility("hidden"))); int bl_writable_retry(int dirfd, const char *path, int flags) __attribute__((visibility("hidden"))); int bl_ntsync_open(const char *path, int flags) __attribute__((visibility("hidden"))); +const char *bl_oom_adj_redirect(const char *path, int writing) __attribute__((visibility("hidden"))); int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden"))); @@ -51,6 +52,7 @@ int open(const char *path, int flags, ...) { errno = EACCES; return -1; } + path = bl_oom_adj_redirect(path, (flags & O_ACCMODE) != O_RDONLY); if ((fd = bl_status_without_tracer(path, flags)) >= 0) return fd; if ((fd = bl_ntsync_open(path, flags)) != -2) return fd; if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open"); @@ -76,6 +78,7 @@ int openat(int dirfd, const char *path, int flags, ...) { errno = EACCES; return -1; } + path = bl_oom_adj_redirect(path, (flags & O_ACCMODE) != O_RDONLY); if ((fd = bl_status_without_tracer(path, flags)) >= 0) return fd; if ((fd = bl_ntsync_open(path, flags)) != -2) return fd; if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat"); diff --git a/tools/tests/test_oom_adj_preload.py b/tools/tests/test_oom_adj_preload.py new file mode 100644 index 0000000000..f6fc25ce16 --- /dev/null +++ b/tools/tests/test_oom_adj_preload.py @@ -0,0 +1,47 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[1] / "linuxfs/preload/oomadj.c" + +PROGRAM = r""" +#include +const char *bl_oom_adj_redirect(const char *path, int writing); +int main(int argc, char **argv) { + for (int i = 1; i + 1 < argc; i += 2) printf("%s\n", bl_oom_adj_redirect(argv[i + 1], argv[i][0] == 'w')); + return 0; +} +""" + + +@unittest.skipUnless(shutil.which("cc"), "needs cc") +class OomAdjPreloadTest(unittest.TestCase): + def test_only_writes_to_oom_files_go_to_dev_null(self): + with tempfile.TemporaryDirectory() as tmp: + main = Path(tmp) / "main.c" + main.write_text(PROGRAM) + binary = Path(tmp) / "probe" + subprocess.run(["cc", "-o", str(binary), str(main), str(SOURCE)], check=True) + cases = [ + ("w", "/proc/self/oom_score_adj", "/dev/null"), + ("w", "/proc/123/oom_score_adj", "/dev/null"), + ("w", "/proc/thread-self/oom_score_adj", "/dev/null"), + ("w", "/proc/12/task/34/oom_score_adj", "/dev/null"), + ("w", "/proc/self/oom_adj", "/dev/null"), + ("r", "/proc/self/oom_score_adj", "/proc/self/oom_score_adj"), + ("w", "/proc/self/oom_score", "/proc/self/oom_score"), + ("w", "/proc/abc/oom_score_adj", "/proc/abc/oom_score_adj"), + ("w", "/tmp/oom_score_adj", "/tmp/oom_score_adj"), + ] + args = [str(binary)] + for mode, path, _ in cases: + args += [mode, path] + out = subprocess.run(args, capture_output=True, text=True, check=True) + self.assertEqual(out.stdout.split(), [want for _, _, want in cases]) + self.assertIn("oom_score_adj raise was dropped", out.stderr) + + +if __name__ == "__main__": + unittest.main()