diff --git a/docs/development/proot-performance.md b/docs/development/proot-performance.md index 1cddb33976..634b7bc63a 100644 --- a/docs/development/proot-performance.md +++ b/docs/development/proot-performance.md @@ -204,7 +204,7 @@ call can be answered in-process with the 0014 design. | syscalls | why proot traps them | can it bypass proot? | |---|---|---| | `openat`/`open`/`creat`, `newfstatat`/`stat`/`lstat`, `statx`, `faccessat`/`access`, `readlinkat` (exit), `mkdirat`, `unlinkat`, `renameat(2)`, `symlinkat`, `linkat`, `fchmodat`, `fchownat`, `utimensat`, `truncate`, `*xattr`, `inotify_add_watch`, `name_to_handle_at` | path translation (guest → host), plus exit fixups for `readlink` and `rename` | **Yes, through libc wrappers.** The prototype covers open, stat, statx, access, readlink, opendir and fopen; the write family is next. Raw `svc` and glibc's internal callers (ld.so, fts, realpath, nss) still go to proot. | -| `chdir`/`fchdir`, `getcwd` (exit) | proot emulates the cwd; until 0014 the kernel's cwd never moved | With 0014 the kernel's cwd follows the guest's. `getcwd` stays in proot (cheap, rare). | +| `chdir`/`fchdir`, `getcwd` (exit) | proot emulates the cwd; until 0014 the kernel's cwd never moved | With 0014 the kernel's cwd follows the guest's. With 0028 `fchdir` takes the path of the open directory from `/proc//fd` instead of walking it again. `getcwd` stays in proot (cheap, rare). | | `execve`/`execveat` | runs the loader, maps `PT_INTERP` inside the rootfs, shebangs, tracks `/proc/self/exe` | **No.** This is the core of what proot is for. Costs 0.4–0.8 ms per exec; the session scripts already avoid exec-heavy loops (`nap`). | | `brk` (entry and exit) | heap emulation: programs are mapped by proot's loader, so the kernel's brk area is the loader's | No (`PR_SET_MM` needs `CAP_SYS_RESOURCE`). **Fewer calls:** `GLIBC_TUNABLES=glibc.malloc.top_pad=…` grows the heap in larger steps. | | `bind`, `connect`, `accept(4)`, `getsockname`, `getpeername` | `sun_path` of Unix sockets | Same-path binds (runtime dir, files dir) need no translation, but the filter can't see the address. Possible fast-path candidate. | diff --git a/tools/proot/PATCHES.md b/tools/proot/PATCHES.md index 3099009c57..6347023473 100644 --- a/tools/proot/PATCHES.md +++ b/tools/proot/PATCHES.md @@ -170,6 +170,18 @@ Prototype (inert unless `PROOT_FASTPATH` is set; see `docs/development/proot-per Old kernel fixes: +- `0028-fchdir-open-directory.patch` - `fchdir` resolved the directory again from its guest path, + one `lstat` per path component, then an `lstat` and an `O_PATH` open of the result. The + descriptor is already open, so `/proc//fd/` gives its host path and one `stat` through + that link confirms it is a live directory. The guest path it maps to is checked to map back to + the same host path, and the parent of the binding it falls under is checked to still resolve + to itself, so the cwd PRoot records is the one the old walk produced; a deleted directory, a + path outside the guest, a binding whose ancestor became a symlink, or anything else that does + not round-trip takes the old walk. Listing a directory in Wine does `fchdir` there and back: on a memory card behind + Android's FUSE daemon the old walk cost about 4 ms per call in the single tracer thread, 8.4 s + of an 8.8 s tracer total across one program start, and is now under 0.1 ms. + `bench/fchdir.py [directory...]` checks the cwd after `fchdir` to plain, bound, symlinked, + renamed and deleted directories and prints the per-call cost. - `0015-lost-fork-events.patch` - works around a problem reported on Xiaomi's msm 4.14 kernel: the kernel sometimes reports a new process or thread but gives its ID as zero. Without that ID, PRoot cannot finish setting it up, which can leave the session stuck. The patch searches `/proc` diff --git a/tools/proot/bench/fchdir.py b/tools/proot/bench/fchdir.py new file mode 100644 index 0000000000..e8d5c9141f --- /dev/null +++ b/tools/proot/bench/fchdir.py @@ -0,0 +1,105 @@ +import errno +import os +import sys +import tempfile +import time + + +def report(label, check): + try: + print(label, check()) + except OSError as error: + print(label, "ERR", errno.errorcode.get(error.errno, error.errno)) + + +def enter(path): + fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fchdir(fd) + return os.getcwd(), os.readlink("/proc/self/cwd"), os.path.samefile(".", path) + finally: + os.close(fd) + + +def enter_file(base): + fd = os.open(base + "/real/file", os.O_RDONLY) + try: + os.fchdir(fd) + finally: + os.close(fd) + + +def enter_deleted(base): + os.mkdir(base + "/gone") + fd = os.open(base + "/gone", os.O_RDONLY) + os.rmdir(base + "/gone") + try: + os.fchdir(fd) + return "ok" + finally: + os.close(fd) + os.chdir("/") + + +def enter_renamed(base): + os.mkdir(base + "/before") + fd = os.open(base + "/before", os.O_RDONLY) + os.rename(base + "/before", base + "/after") + try: + os.fchdir(fd) + return os.getcwd() == base + "/after" + finally: + os.close(fd) + os.chdir("/") + os.rmdir(base + "/after") + + +def relative(base): + fd = os.open(base, os.O_RDONLY) + os.fchdir(fd) + os.close(fd) + seen = os.path.exists("real/file"), os.path.exists("link/file") + os.chdir("real") + inside = os.getcwd() == base + "/real" + os.chdir("..") + return seen + (inside, os.getcwd() == base) + + +def cost(path, rounds=300): + fd = os.open(path, os.O_RDONLY) + back = os.open("/", os.O_RDONLY) + start = time.perf_counter() + for _ in range(rounds): + os.fchdir(fd) + os.fchdir(back) + elapsed = time.perf_counter() - start + os.close(fd) + os.close(back) + return round(elapsed / rounds / 2 * 1e6, 1) + + +def main(): + extra = sys.argv[1:] + base = tempfile.mkdtemp(prefix="fchdir-") + os.mkdir(base + "/real") + os.symlink(base + "/real", base + "/link") + os.mkdir(base + "/dot.") + open(base + "/real/file", "w").close() + for path in ["/", "/tmp", "/usr/lib", "/proc/self", "/dev", base + "/real", base + "/link", base + "/dot."] + extra: + report(path, lambda: enter(path)) + report("file", lambda: enter_file(base)) + report("deleted", lambda: enter_deleted(base)) + report("renamed", lambda: enter_renamed(base)) + report("badfd", lambda: os.fchdir(987)) + report("relative", lambda: relative(base)) + os.chdir("/") + for path in ["/usr/lib"] + extra: + print("fchdir_us", path, cost(path)) + os.unlink(base + "/real/file") + os.unlink(base + "/link") + os.rmdir(base + "/dot.") + os.rmdir(base + "/real") + os.rmdir(base) + + +main() diff --git a/tools/proot/patches/0028-fchdir-open-directory.patch b/tools/proot/patches/0028-fchdir-open-directory.patch new file mode 100644 index 0000000000..8e1a494386 --- /dev/null +++ b/tools/proot/patches/0028-fchdir-open-directory.patch @@ -0,0 +1,108 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: maxjivi05 +Date: Thu, 12 Nov 2026 22:00:00 -0500 +Subject: [PATCH] fchdir: take the path of the open directory instead of resolving + it again component by component + +--- +--- a/src/syscall/enter.c ++++ b/src/syscall/enter.c +@@ -46,8 +46,66 @@ + #include "tracee/abi.h" + #include "path/path.h" + #include "path/canon.h" ++#include "path/binding.h" + #include "arch.h" + ++static int translate_open_directory(Tracee *tracee, char path[PATH_MAX], int fd, struct stat *statl) ++{ ++ char guest[PATH_MAX]; ++ char mount[PATH_MAX]; ++ char parent[PATH_MAX]; ++ char link[32]; ++ char *slash; ++ const Binding *binding; ++ size_t length; ++ int status; ++ ++ status = readlink_proc_pid_fd(tracee->pid, fd, path); ++ if (status < 0 || path[0] != '/') ++ return 1; ++ ++ strcpy(guest, path); ++ status = detranslate_path(tracee, guest, NULL); ++ if (status < 0) ++ return 1; ++ ++ binding = get_binding(tracee, GUEST, guest); ++ if (binding == NULL) ++ return 1; ++ ++ strcpy(parent, binding->guest.path); ++ slash = strrchr(parent, '/'); ++ if (slash != NULL && slash != parent) { ++ if ((size_t) (slash - parent) + 3 > PATH_MAX) ++ return 1; ++ ++ strcpy(slash, "/."); ++ strcpy(mount, "/"); ++ status = canonicalize(tracee, parent, true, mount, 0); ++ if (status < 0 || strcmp(mount, parent) != 0) ++ return 1; ++ } ++ ++ status = substitute_binding(tracee, GUEST, guest); ++ if (status < 0 || strcmp(guest, path) != 0) ++ return 1; ++ ++ status = snprintf(link, sizeof(link), "/proc/%d/fd/%d", tracee->pid, fd); ++ if (status < 0 || (size_t) status >= sizeof(link)) ++ return 1; ++ ++ status = stat(link, statl); ++ if (status < 0 || !S_ISDIR(statl->st_mode) || statl->st_nlink == 0) ++ return 1; ++ ++ length = strlen(path); ++ if (length + 2 >= PATH_MAX) ++ return 1; ++ ++ strcpy(path + (length == 1 ? 0 : length), "/."); ++ return 0; ++} ++ + /** + * Translate @path and put the result in the @tracee's memory address + * space pointed to by the @reg argument of the current syscall. See +@@ -193,13 +251,16 @@ + dirfd = peek_reg(tracee, CURRENT, SYSARG_1); + } + +- status = translate_path(tracee, path, dirfd, oldpath, true); +- if (status < 0) +- break; ++ if (syscall_number == PR_chdir ++ || translate_open_directory(tracee, path, dirfd, &statl) != 0) { ++ status = translate_path(tracee, path, dirfd, oldpath, true); ++ if (status < 0) ++ break; + +- status = lstat(path, &statl); +- if (status < 0) +- break; ++ status = lstat(path, &statl); ++ if (status < 0) ++ break; ++ } + + /* Check this directory is accessible. */ + if ((statl.st_mode & S_IXUSR) == 0) +@@ -208,7 +269,7 @@ + + + +- if (getenv("PROOT_FASTPATH") != NULL) { ++ if (syscall_number == PR_chdir && getenv("PROOT_FASTPATH") != NULL) { + int fd = open(path, O_PATH | O_DIRECTORY | O_CLOEXEC); + +