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/<pid|self|thread-self>[/task/<tid>]/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)
This commit is contained in:
The412Banner committed 2026-10-15 19:47:18 -04:00
1 parent 5bc5f68761
commit b3f3e4fdaf
4 files changed
+100

No files matched your search

+3
View File
@@ -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;
+47
View File
@@ -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 <errno.h>
#include <string.h>
#include <unistd.h>
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";
}
+3
View File
@@ -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");
+47
View File
@@ -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 <stdio.h>
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()