mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
perf: Wine's write watches stop costing a signal per page on arm64 (#1333)
Wine asks for userfaultfd with the i386 syscall number on arm64, so it never gets one and falls back to mprotect and a SIGSEGV per first write, which under proot is a ptrace stop each time. libblsession hands it the real userfaultfd and, where Android refuses the PAGEMAP_SCAN ioctl, answers it from /proc/self/pagemap and UFFDIO_WRITEPROTECT.
This commit is contained in:
1 parent
9a4a611d38
commit
f27d0b006f
9 files changed
+245
-4
No files matched your search
@@ -409,6 +409,8 @@ int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attrib
|
||||
int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
|
||||
void *arg, void *caller, int *rc) __attribute__((visibility("hidden")));
|
||||
int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg,
|
||||
int *rc) __attribute__((visibility("hidden")));
|
||||
|
||||
int ioctl(int fd, unsigned long request, ...) {
|
||||
typedef int (*ioctl_fn)(int, unsigned long, void *);
|
||||
@@ -424,6 +426,7 @@ int ioctl(int fd, unsigned long request, ...) {
|
||||
if (bl_drm_gem_close(fd, request, arg, &rc)) return rc;
|
||||
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
|
||||
if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc;
|
||||
if (bl_writewatch_ioctl(real, fd, request, arg, &rc)) return rc;
|
||||
if (!bl_kgsl_poll(real, fd, request, arg, &rc)) rc = quest_kgsl_ioctl(real, fd, request, arg);
|
||||
bl_kgsl_chip_id_fixup(request, arg, rc);
|
||||
if (arg == NULL || !is_link_query(request)) return rc;
|
||||
|
||||
@@ -329,6 +329,8 @@ static int execveat_is_execve(const long *args) {
|
||||
return path[0] == '/' || (int)args[0] == AT_FDCWD;
|
||||
}
|
||||
|
||||
int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) __attribute__((visibility("hidden")));
|
||||
|
||||
long syscall(long number, ...) {
|
||||
va_list ap;
|
||||
long args[6];
|
||||
@@ -354,6 +356,9 @@ long syscall(long number, ...) {
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
if (bl_writewatch_syscall(number, args, real_syscall, &handled)) {
|
||||
return handled;
|
||||
}
|
||||
if (number == SYS_openat2) {
|
||||
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
|
||||
long fd = real_syscall(number, args[0], args[1], args[2], args[3]);
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/userfaultfd.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define WINE_ARM64_USERFAULTFD 374
|
||||
#define PM_UFFD_WP (1ULL << 57)
|
||||
#define PM_SWAP (1ULL << 62)
|
||||
#define PM_PRESENT (1ULL << 63)
|
||||
|
||||
#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
static int ww_uffd = -1;
|
||||
static int scan_blocked;
|
||||
#endif
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) {
|
||||
#if defined(__aarch64__) && defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
if (number != WINE_ARM64_USERFAULTFD || (args[0] & ~(long)(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY))) return 0;
|
||||
*rc = real(SYS_userfaultfd, args[0]);
|
||||
if (*rc >= 0) __atomic_store_n(&ww_uffd, (int)*rc, __ATOMIC_RELEASE);
|
||||
return 1;
|
||||
#else
|
||||
(void)number;
|
||||
(void)args;
|
||||
(void)real;
|
||||
(void)rc;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
static int protect(int uffd, uint64_t start, uint64_t end) {
|
||||
if (start == end) return 0;
|
||||
struct uffdio_writeprotect wp = { .range = { start, end - start }, .mode = UFFDIO_WRITEPROTECT_MODE_WP };
|
||||
return ioctl(uffd, UFFDIO_WRITEPROTECT, &wp);
|
||||
}
|
||||
|
||||
static long emulate_scan(int fd, int uffd, struct pm_scan_arg *a) {
|
||||
uint64_t page = (uint64_t)getpagesize();
|
||||
if (a->size < sizeof(*a) || (a->flags & ~(uint64_t)PM_SCAN_WP_MATCHING) || a->category_inverted || a->category_anyof_mask
|
||||
|| a->category_mask != PAGE_IS_WRITTEN || (a->return_mask & ~(uint64_t)PAGE_IS_WRITTEN) || a->end < a->start
|
||||
|| ((a->start | a->end) & (page - 1)) || (a->vec_len && !a->vec)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
struct page_region *vec = (struct page_region *)(uintptr_t)a->vec;
|
||||
uint64_t regions = 0, pages = 0, addr = a->start, wp_start = 0, wp_end = 0;
|
||||
uint64_t entries[512];
|
||||
int wp = (a->flags & PM_SCAN_WP_MATCHING) != 0;
|
||||
while (addr < a->end) {
|
||||
uint64_t want = (a->end - addr) / page;
|
||||
if (want > 512) want = 512;
|
||||
ssize_t got = pread(fd, entries, want * sizeof(entries[0]), (off_t)(addr / page * sizeof(entries[0])));
|
||||
if (got < 0) return -1;
|
||||
if (got == 0) break;
|
||||
for (uint64_t i = 0; i < (uint64_t)got / sizeof(entries[0]); i++, addr += page) {
|
||||
uint64_t e = entries[i];
|
||||
if (!(e & (PM_PRESENT | PM_SWAP)) || (e & PM_UFFD_WP)) continue;
|
||||
if (a->max_pages && pages >= a->max_pages) goto done;
|
||||
if (a->vec_len) {
|
||||
if (regions && vec[regions - 1].end == addr) {
|
||||
vec[regions - 1].end += page;
|
||||
} else {
|
||||
if (regions == a->vec_len) goto done;
|
||||
vec[regions].start = addr;
|
||||
vec[regions].end = addr + page;
|
||||
vec[regions].categories = PAGE_IS_WRITTEN & a->return_mask;
|
||||
regions++;
|
||||
}
|
||||
}
|
||||
pages++;
|
||||
if (!wp) continue;
|
||||
if (wp_end != addr) {
|
||||
if (protect(uffd, wp_start, wp_end) < 0) return -1;
|
||||
wp_start = addr;
|
||||
}
|
||||
wp_end = addr + page;
|
||||
}
|
||||
}
|
||||
done:
|
||||
if (wp && protect(uffd, wp_start, wp_end) < 0) return -1;
|
||||
a->walk_end = addr < a->end ? addr : a->end;
|
||||
return (long)regions;
|
||||
}
|
||||
#endif
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
|
||||
void *arg, int *rc) {
|
||||
#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY)
|
||||
if (request != PAGEMAP_SCAN) return 0;
|
||||
int uffd = __atomic_load_n(&ww_uffd, __ATOMIC_ACQUIRE);
|
||||
if (uffd < 0) return 0;
|
||||
if (!__atomic_load_n(&scan_blocked, __ATOMIC_RELAXED)) {
|
||||
*rc = real(fd, request, arg);
|
||||
if (*rc >= 0 || errno != EACCES) return 1;
|
||||
__atomic_store_n(&scan_blocked, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
*rc = (int)emulate_scan(fd, uffd, (struct pm_scan_arg *)arg);
|
||||
return 1;
|
||||
#else
|
||||
(void)real;
|
||||
(void)fd;
|
||||
(void)request;
|
||||
(void)arg;
|
||||
(void)rc;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
@@ -319,7 +319,7 @@ class FsyncPreloadTest(unittest.TestCase):
|
||||
source = base / "fsync-test.c"
|
||||
source.write_text(PROGRAM)
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib),
|
||||
str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), "-ldl"], check=True)
|
||||
str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-pthread", "-o", str(cls.exe), str(source)], check=True)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -43,7 +43,7 @@ class NoexecPreloadTest(unittest.TestCase):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
cls.lib = cls.tmp / "libnoexec.so"
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-DNOEXEC_TEST", "-o", str(cls.lib),
|
||||
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), "-ldl"], check=True)
|
||||
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
|
||||
source = cls.tmp / "program.c"
|
||||
source.write_text(PROGRAM)
|
||||
for name in ("FEX", "game"):
|
||||
|
||||
@@ -230,7 +230,7 @@ class Openat2PreloadTest(unittest.TestCase):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
cls.lib = cls.tmp / "libopenat2.so"
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib),
|
||||
str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), "-ldl"], check=True)
|
||||
str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
|
||||
source = cls.tmp / "openat2.c"
|
||||
source.write_text(PROGRAM)
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-o", str(cls.tmp / "openat2"), str(source), "-ldl"], check=True)
|
||||
|
||||
@@ -32,6 +32,7 @@ int probe(void) {
|
||||
#include <assert.h>
|
||||
#include <dlfcn.h>
|
||||
int bl_fsync_syscall(long n, const long *a, long *r) { (void)n; (void)a; (void)r; return 0; }
|
||||
int bl_writewatch_syscall(long n, const long *a, long (*real)(long, ...), long *r) { (void)n; (void)a; (void)real; (void)r; return 0; }
|
||||
#include """ + '"' + robust.as_posix() + '"' + r"""
|
||||
int main(int argc, char **argv) {
|
||||
assert(argc == 3);
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
PROGRAM = r"""
|
||||
#include "WRITEWATCH"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#define PAGES 96
|
||||
|
||||
static int uffd, pagemap;
|
||||
static uint64_t page;
|
||||
|
||||
static char *region(void) {
|
||||
char *m = mmap(NULL, PAGES * page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
madvise(m, PAGES * page, MADV_NOHUGEPAGE);
|
||||
struct uffdio_register reg = { .range = { (uintptr_t)m, PAGES * page }, .mode = UFFDIO_REGISTER_MODE_WP };
|
||||
if (m == MAP_FAILED || ioctl(uffd, UFFDIO_REGISTER, ®)) exit(3);
|
||||
protect(uffd, (uintptr_t)m, (uintptr_t)m + PAGES * page);
|
||||
return m;
|
||||
}
|
||||
|
||||
static long scan(char *m, int emulated, uint64_t flags, uint64_t vec_len, uint64_t max_pages, uint64_t from, uint64_t to,
|
||||
struct page_region *vec, uint64_t *walk_end) {
|
||||
struct pm_scan_arg a = { .size = sizeof a, .flags = flags, .start = (uintptr_t)m + from * page, .end = (uintptr_t)m + to * page,
|
||||
.vec = vec_len ? (uintptr_t)vec : 0, .vec_len = vec_len, .max_pages = max_pages,
|
||||
.category_mask = PAGE_IS_WRITTEN, .return_mask = PAGE_IS_WRITTEN };
|
||||
long n = emulated ? emulate_scan(pagemap, uffd, &a) : ioctl(pagemap, PAGEMAP_SCAN, &a);
|
||||
*walk_end = (a.walk_end - (uintptr_t)m) / page;
|
||||
return n;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
page = (uint64_t)getpagesize();
|
||||
uffd = syscall(SYS_userfaultfd, O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY);
|
||||
struct uffdio_api api = { .api = UFFD_API, .features = UFFD_FEATURE_WP_ASYNC | UFFD_FEATURE_WP_UNPOPULATED };
|
||||
pagemap = open("/proc/self/pagemap", O_RDONLY | O_CLOEXEC);
|
||||
if (uffd < 0 || ioctl(uffd, UFFDIO_API, &api) || pagemap < 0) { puts("unsupported"); return 0; }
|
||||
char *kernel = region(), *ours = region();
|
||||
struct page_region probe[1];
|
||||
uint64_t end;
|
||||
if (scan(kernel, 0, 0, 1, 0, 0, PAGES, probe, &end) < 0) { puts("unsupported"); return 0; }
|
||||
srand(atoi(argv[1]));
|
||||
int rounds = atoi(argv[2]), differing = 0;
|
||||
for (int r = 0; r < rounds; r++) {
|
||||
int writes = rand() % 24;
|
||||
for (int w = 0; w < writes; w++) {
|
||||
int p = rand() % PAGES;
|
||||
if (rand() % 4 == 0) {
|
||||
volatile char x = kernel[p * page] + ours[p * page];
|
||||
(void)x;
|
||||
continue;
|
||||
}
|
||||
kernel[p * page + rand() % page] = (char)r;
|
||||
ours[p * page + rand() % page] = (char)r;
|
||||
}
|
||||
uint64_t flags = rand() % 2 ? PM_SCAN_WP_MATCHING : 0;
|
||||
uint64_t vec_len = rand() % 3 == 0 ? 0 : 1 + rand() % 5;
|
||||
uint64_t max_pages = rand() % 3 == 0 ? 1 + rand() % 8 : 0;
|
||||
if (!vec_len) flags = PM_SCAN_WP_MATCHING, max_pages = 0;
|
||||
uint64_t from = rand() % PAGES, to = from + rand() % (PAGES - from + 1);
|
||||
struct page_region kv[8], ov[8];
|
||||
uint64_t kend, oend;
|
||||
long kn = scan(kernel, 0, flags, vec_len, max_pages, from, to, kv, &kend);
|
||||
long on = scan(ours, 1, flags, vec_len, max_pages, from, to, ov, &oend);
|
||||
int same = kn == on && (kn < 0 || kend == oend);
|
||||
for (long i = 0; same && i < kn; i++)
|
||||
same = kv[i].start - (uintptr_t)kernel == ov[i].start - (uintptr_t)ours
|
||||
&& kv[i].end - (uintptr_t)kernel == ov[i].end - (uintptr_t)ours && kv[i].categories == ov[i].categories;
|
||||
struct page_region kall[PAGES], oall[PAGES];
|
||||
long ka = scan(kernel, 0, 0, PAGES, 0, 0, PAGES, kall, &kend), oa = scan(ours, 1, 0, PAGES, 0, 0, PAGES, oall, &oend);
|
||||
same = same && ka == oa;
|
||||
for (long i = 0; same && i < ka; i++)
|
||||
same = kall[i].start - (uintptr_t)kernel == oall[i].start - (uintptr_t)ours && kall[i].end - (uintptr_t)kernel == oall[i].end - (uintptr_t)ours;
|
||||
if (!same) {
|
||||
differing++;
|
||||
printf("round %d flags %llu vec %llu max %llu range %llu-%llu: kernel %ld end %llu, ours %ld end %llu\n", r,
|
||||
(unsigned long long)flags, (unsigned long long)vec_len, (unsigned long long)max_pages, (unsigned long long)from,
|
||||
(unsigned long long)to, kn, (unsigned long long)kend, on, (unsigned long long)oend);
|
||||
}
|
||||
}
|
||||
printf("rounds %d differing %d\n", rounds, differing);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class WriteWatchPreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if shutil.which("gcc") is None:
|
||||
raise unittest.SkipTest("gcc is not available")
|
||||
cls.work = Path(tempfile.mkdtemp())
|
||||
source = cls.work / "writewatch.c"
|
||||
source.write_text(PROGRAM.replace("WRITEWATCH", str(PRELOAD / "writewatch.c")))
|
||||
cls.program = cls.work / "writewatch"
|
||||
subprocess.run(["gcc", "-O1", "-Wall", "-o", str(cls.program), str(source)], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.work, ignore_errors=True)
|
||||
|
||||
def test_a_scan_android_refuses_answers_like_the_kernel_one(self):
|
||||
for seed in range(4):
|
||||
out = subprocess.run([str(self.program), str(seed), "400"], capture_output=True, text=True, timeout=60).stdout
|
||||
if out.strip() == "unsupported":
|
||||
self.skipTest("this kernel has no asynchronous userfaultfd write protection")
|
||||
self.assertTrue(out.rstrip().endswith("differing 0"), out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user