mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
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:
1 parent
5bc5f68761
commit
b3f3e4fdaf
4 files changed
+100
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user