diff --git a/docs/development/low-memory.md b/docs/development/low-memory.md index 50e78363ef..a0fbb9f274 100644 --- a/docs/development/low-memory.md +++ b/docs/development/low-memory.md @@ -105,7 +105,11 @@ is left, so a process that is not paging out has no thread the preload added. Pr check they are single-threaded before they fork or call `unshare(CLONE_NEWUSER)`, such as Chromium's zygote, keep working. The worker is a bare `clone` with the flags of a normal thread, on a stack the preload maps itself, and makes only raw system calls, so starting it -from the signal handler is safe and seccomp filters that only allow thread clones accept it. A +from the signal handler is safe and seccomp filters that only allow thread clones accept it. +Because those calls bypass the native userland hook (Run without proot), which maps guest paths +only inside libc, the preload adds `DDNATV_ROOT` to `/tmp/.dd-zram` and its hold, trim and ack files +once at startup. Without it, no process of a native session installed the handler and every +request was ignored. A process that has a single thread, or whose signalled thread added a seccomp filter after the process started, as browser sandboxes do, gets no worker. When there is no worker, the process trims inline in steps of at most 32 MiB per request and looks at no more than 512 MiB of its address space each time. The page-out for a suspend and the read-back diff --git a/tools/linuxfs/preload/zram.c b/tools/linuxfs/preload/zram.c index ad13d8258b..7fc21d0cab 100644 --- a/tools/linuxfs/preload/zram.c +++ b/tools/linuxfs/preload/zram.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #ifndef ZR_CTL #define ZR_CTL "/tmp/.dd-zram" #endif +#define ZR_PATH 256 #define ZR_SWAPPED (1ULL << 62) #define ZR_PRESENT (1ULL << 63) #define ZR_BATCH 512 @@ -213,6 +215,9 @@ static char *zr_find(char *text, char c, size_t n) { return NULL; } +static char zr_ctl[ZR_PATH] = ZR_CTL; +static char zr_hold[ZR_PATH + sizeof(".hold")]; + static int zr_open(const char *path) { return (int)zr_sys(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC, 0); } @@ -220,13 +225,13 @@ static int zr_open(const char *path) { static int zr_mode(unsigned long *want) { char text[24]; long n = 0; - int fd = zr_open(ZR_CTL ".hold"); + int fd = zr_open(zr_hold); if (fd >= 0) { n = zr_sys(SYS_read, fd, text, sizeof(text)); zr_sys(SYS_close, fd); } if (n < 1) { - fd = zr_open(ZR_CTL); + fd = zr_open(zr_ctl); if (fd < 0) return -1; n = zr_sys(SYS_read, fd, text, sizeof(text)); zr_sys(SYS_close, fd); @@ -413,10 +418,10 @@ static unsigned long zr_walk(int mode, unsigned long want, unsigned long lo, uns static void zr_ack(long value, int trim) { const char *suffix = trim ? ".trim." : ".ack."; - char path[sizeof(ZR_CTL) + sizeof(".trim.") + 20]; + char path[sizeof(zr_ctl) + sizeof(".trim.") + 20]; char digits[20]; size_t n = 0, d = 0; - for (const char *p = ZR_CTL; *p; p++) path[n++] = *p; + for (const char *p = zr_ctl; *p; p++) path[n++] = *p; for (const char *p = suffix; *p; p++) path[n++] = *p; long pid = zr_sys(SYS_getpid); do digits[d++] = (char)('0' + pid % 10); while ((pid /= 10) > 0); @@ -547,8 +552,23 @@ static void zr_child(void) { __atomic_store_n(&zr_stack_busy, 0, __ATOMIC_RELEASE); } +// The native userland hook maps guest paths in libc only; these raw calls see the host's paths. +static void zr_paths(void) { + const char *root = getenv("DDNATV_ROOT"); + size_t r = root && root[0] == '/' ? strlen(root) : 0, c = sizeof(ZR_CTL) - 1; + while (r > 1 && root[r - 1] == '/') r--; + if (r > 1 && ZR_CTL[0] == '/' && r + c < sizeof(zr_ctl)) { + memcpy(zr_ctl, root, r); + memcpy(zr_ctl + r, ZR_CTL, c + 1); + } + size_t n = strlen(zr_ctl); + memcpy(zr_hold, zr_ctl, n); + memcpy(zr_hold + n, ".hold", sizeof(".hold")); +} + __attribute__((constructor)) static void zr_init(void) { - int fd = zr_open(ZR_CTL); + zr_paths(); + int fd = zr_open(zr_ctl); if (fd < 0) return; zr_sys(SYS_close, fd); long page = sysconf(_SC_PAGESIZE); diff --git a/tools/tests/test_zram_preload.py b/tools/tests/test_zram_preload.py index 8a9f813480..58a77fb9dd 100644 --- a/tools/tests/test_zram_preload.py +++ b/tools/tests/test_zram_preload.py @@ -524,6 +524,18 @@ class ZramPreloadTest(unittest.TestCase): self.assertEqual([1], self.run_program(self.game)["handler"]) self.assertEqual(2, len(list(self.dir.glob("ctl.ack.*")))) + def test_control_files_resolve_under_the_native_guest_root(self): + # The native userland hook maps guest paths in libc; the preload's raw calls need the root added. + lib = self.dir / "build" / "libddsession-guest.so" + if not lib.exists(): + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-DZR_CTL=\"/ctl\"", + "-o", str(lib), *map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True) + for root, handler in ((str(self.dir), 1), (str(self.dir) + "/", 1), ("", 0)): + environment = dict(os.environ, LD_PRELOAD=str(lib), DDNATV_ROOT=root) + result = subprocess.run([str(self.steam), str(self.ctl), "8", "0"], env=environment, + capture_output=True, text=True, check=True, timeout=120) + self.assertIn("handler %d" % handler, result.stdout.splitlines(), root) + def test_nothing_is_installed_without_the_control_file(self): self.ctl.unlink() try: