From b588da2e15298adc3385f9dc791589649fc3ce63 Mon Sep 17 00:00:00 2001 From: 7a72 Date: Mon, 24 Nov 2025 05:58:10 +0800 Subject: [PATCH] perf: cache KSU driver fd to avoid repeated syscalls Previously, grab_fd() was called on every send_unmountable() invocation, resulting in redundant syscalls. Now use pthread_once to ensure the fd is grabbed only once and cached for subsequent uses. - Add g_driver_fd static variable with pthread_once initialization - Modify grab_fd() to return cached fd after first call - Remove explicit grab_fd() call in magic_mount() - Add error checking for invalid fd values This reduces syscall overhead when registering multiple mount points. Co-Authored-By: Tools-app --- src/magic_mount.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/magic_mount.c b/src/magic_mount.c index 5f94af2..0a14766 100644 --- a/src/magic_mount.c +++ b/src/magic_mount.c @@ -16,6 +16,7 @@ #include #include #include +#include MountStats g_stats = { 0 }; @@ -28,23 +29,47 @@ int g_failed_modules_count = 0; char **g_extra_parts = NULL; int g_extra_parts_count = 0; -int global_fd = 0; +static atomic_int g_driver_fd = -1; +static atomic_flag g_driver_fd_initialized = ATOMIC_FLAG_INIT; -static void grab_fd(void) { syscall(SYS_reboot, KSU_INSTALL_MAGIC1, KSU_INSTALL_MAGIC2, 0, (void *)&global_fd); } +static void grab_fd_once(void) +{ + int fd = -1; + syscall(SYS_reboot, KSU_INSTALL_MAGIC1, KSU_INSTALL_MAGIC2, 0, (void *)&fd); + + if (fd < 0) { + LOGW("failed to grab KSU driver fd: %d", fd); + } else { + LOGD("grabbed KSU driver fd: %d", fd); + } + + atomic_store(&g_driver_fd, fd); +} + +static int grab_fd(void) +{ + if (!atomic_flag_test_and_set(&g_driver_fd_initialized)) { + grab_fd_once(); + } + + return atomic_load(&g_driver_fd); +} static void send_unmountable(const char *mntpoint) { struct ksu_add_try_umount_cmd cmd = {0}; + int fd = grab_fd(); - if (!global_fd) + if (fd < 0) return; cmd.arg = (uint64_t)mntpoint; cmd.flags = 0x2; cmd.mode = 1; - ioctl(global_fd, KSU_IOCTL_ADD_TRY_UMOUNT, &cmd); - + if (ioctl(fd, KSU_IOCTL_ADD_TRY_UMOUNT, &cmd) < 0) { + LOGE("ioctl KSU_IOCTL_ADD_TRY_UMOUNT failed: %s", strerror(errno)); + } } static void register_module_failure(const char *module_name) @@ -694,7 +719,6 @@ static Node *collect_root(void) return root; } - static int clone_symlink(const char *src, const char *dst) { char target[PATH_MAX]; @@ -1083,8 +1107,6 @@ int magic_mount(const char *tmp_root) node_free(root); return -1; } - - grab_fd(); LOGI("starting magic_mount core logic: tmpfs_source=%s tmp_dir=%s", g_mount_source, tmp_dir);