From 72196c3447a6794c0deb30bf79c553aa8d879370 Mon Sep 17 00:00:00 2001 From: The412Banner Date: Tue, 13 Oct 2026 11:47:23 -0400 Subject: [PATCH] Non-Adreno round 6: PowerVR PRIME on old kernels, thermal, hang-up names, bridge flippable images, Venus Zink variants - drm.c: PR #570's current PRIME ownership check (F_DUPFD_QUERY, then kcmp, then the F_SETOWN_EX owner marker): on a kernel before 6.10 with kcmp blocked, drmPrimeFDToHandle answered EOPNOTSUPP for every PowerVR import (Pixel 10, Android 17: Zink "failed drmPrimeFDToHandle", Xwayland error 7, gamescope X11 I/O error). The Mali stand-in node is kept. - A /sys/class/thermal the kernel will not list gets an empty directory, so mangoapp no longer aborts on it. - gamescope 0132: a hung-up waitable is named before the abort; 0133: the bridge's flippable images use supported-but-unexportable modifiers instead of asserting. - Venus: when Zink does not start, the loader's view is logged and Zink is retried without ZINK_DEBUG=compact, with ZINK_DESCRIPTORS=auto and without kopper before falling back to llvmpipe. --- .../launcher/runtime/LinuxRuntime.java | 4 + tools/gamescope/PATCHES.md | 8 ++ .../patches/0132-name-hung-up-waitable.patch | 57 ++++++++++++++ ...ippable-image-unexportable-modifiers.patch | 44 +++++++++++ .../overlay/usr/local/bin/droiddeck-session | 28 ++++++- tools/linuxfs/preload/drm.c | 63 ++++++++++++++- tools/tests/drm_handles_test.c | 76 +++++++++++++++++-- 7 files changed, 267 insertions(+), 13 deletions(-) create mode 100644 tools/gamescope/patches/0132-name-hung-up-waitable.patch create mode 100644 tools/gamescope/patches/0133-flippable-image-unexportable-modifiers.patch diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java index b524ab5532..01407d8092 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java @@ -214,6 +214,10 @@ public final class LinuxRuntime { bind(cmd, "/proc/self/fd/1:/dev/stdout"); bind(cmd, "/proc/self/fd/2:/dev/stderr"); bind(cmd, new File(root, "etc/droiddeck/empty").getPath() + ":/sys/fs/selinux"); + // A kernel that refuses to list /sys/class/thermal (Pixel 10, Android 17) made mangoapp abort + // at start ("filesystem_error ... Permission denied: '/sys/class/thermal/'") until its + // wrapper gave up: it lists the directory for CPU temperatures. Empty there instead. + if (new File("/sys/class/thermal").list() == null) bind(cmd, new File(root, "etc/droiddeck/empty").getPath() + ":/sys/class/thermal"); bind(cmd, context.getFilesDir().getPath()); bind(cmd, context.getCacheDir().getPath()); if (runtimeDir != null) { diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index 34dbdd5a9b..54cf9e3526 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -92,3 +92,11 @@ app's Wayland-hosted gamescope never reaches, or need a newer gamescope than the backend failed. Also: 0130's probe pipeline is kept instead of destroyed, and skipped when `GAMESCOPE_NO_YCBCR_SAMPLERS=1` (destroying a pipeline the Venus host had refused killed the render server). +- `0132-name-hung-up-waitable.patch`: "IWaitable hung up. Aborting." now names the waitable's class, + its fd and what the fd is (`/proc/self/fd`), and the Wayland backend's input thread says why the + compositor connection ended (protocol error or errno) first. An S24 FE aborted there after 60/120 Hz + flips and after a resume with nothing else in the log. +- `0133-flippable-image-unexportable-modifiers.patch`: a flippable (dma-buf exported) image whose + modifiers the driver supports but does not report EXPORTABLE uses them anyway, and an image with no + usable modifier fails with a log line instead of asserting in `CVulkanTexture::BInit`. The Mali + bridge forwards the phone driver's answer, which leaves EXPORTABLE out, though it exports dma-bufs. diff --git a/tools/gamescope/patches/0132-name-hung-up-waitable.patch b/tools/gamescope/patches/0132-name-hung-up-waitable.patch new file mode 100644 index 0000000000..51abe76eb4 --- /dev/null +++ b/tools/gamescope/patches/0132-name-hung-up-waitable.patch @@ -0,0 +1,57 @@ +diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp +index a927f69..2c9bd73 100644 +--- a/src/Backends/WaylandBackend.cpp ++++ b/src/Backends/WaylandBackend.cpp +@@ -2882,7 +2882,22 @@ namespace gamescope + abort(); + } + +- CFunctionWaitable waitable( nFD ); ++ // The compositor's connection: a hang-up says why (the protocol error, if one was posted). ++ class CDisplayWaitable final : public IWaitable ++ { ++ public: ++ CDisplayWaitable( int nFD, wl_display *pDisplay ) : m_nFD{ nFD }, m_pDisplay{ pDisplay } {} ++ int GetFD() final { return m_nFD; } ++ void OnPollHangUp() final ++ { ++ LogDisplayError( "The Wayland compositor closed gamescope's connection (input thread)", m_pDisplay ); ++ IWaitable::OnPollHangUp(); ++ } ++ private: ++ int m_nFD; ++ wl_display *m_pDisplay; ++ }; ++ CDisplayWaitable waitable( nFD, m_pBackend->GetDisplay() ); + m_Waiter.AddWaitable( &waitable ); + + int nRet = 0; +diff --git a/src/waitable.h b/src/waitable.h +index be54d46..b9116fe 100644 +--- a/src/waitable.h ++++ b/src/waitable.h +@@ -9,6 +9,8 @@ + + #include + #include ++#include ++#include + + #include "log.hpp" + +@@ -30,7 +32,14 @@ namespace gamescope + virtual void OnPollPri() {} + virtual void OnPollHangUp() + { +- g_WaitableLog.errorf( "IWaitable hung up. Aborting." ); ++ // Which one: its class and what its descriptor is (a socket, a pipe, an eventfd...). ++ int nFD = GetFD(); ++ char szPath[64], szTarget[256] = "?"; ++ snprintf( szPath, sizeof( szPath ), "/proc/self/fd/%d", nFD ); ++ ssize_t nLen = nFD >= 0 ? readlink( szPath, szTarget, sizeof( szTarget ) - 1 ) : -1; ++ if ( nLen >= 0 ) ++ szTarget[nLen] = '\0'; ++ g_WaitableLog.errorf( "IWaitable hung up: %s, fd %d -> %s. Aborting.", typeid( *this ).name(), nFD, szTarget ); + abort(); + } + diff --git a/tools/gamescope/patches/0133-flippable-image-unexportable-modifiers.patch b/tools/gamescope/patches/0133-flippable-image-unexportable-modifiers.patch new file mode 100644 index 0000000000..ab704f0318 --- /dev/null +++ b/tools/gamescope/patches/0133-flippable-image-unexportable-modifiers.patch @@ -0,0 +1,44 @@ +diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp +index e699c85..0ddd249 100644 +--- a/src/rendervulkan.cpp ++++ b/src/rendervulkan.cpp +@@ -2372,6 +2372,7 @@ bool CVulkanTexture::BInit( uint32_t width, uint32_t height, uint32_t depth, uin + } + + std::vector modifiers = {}; ++ std::vector unexportable = {}; + // TODO(JoshA): Move this code to backend for making flippable image. + if ( GetBackend()->UsesModifiers() && flags.bFlippable && g_device.supportsModifiers() && !pDMA ) + { +@@ -2410,12 +2411,30 @@ bool CVulkanTexture::BInit( uint32_t width, uint32_t height, uint32_t depth, uin + } + + if ( !( externalFormatProps.externalMemoryProperties.externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT ) ) ++ { ++ vk_log.infof( "flippable image: modifier 0x%" PRIx64 " for DRM format 0x%x supported but not reported exportable (features 0x%x)", ++ modifier, drmFormat, externalFormatProps.externalMemoryProperties.externalMemoryFeatures ); ++ unexportable.push_back( modifier ); + continue; ++ } + + modifiers.push_back( modifier ); + } + +- assert( modifiers.size() > 0 ); ++ // A driver that supports the modifier but leaves EXPORTABLE out (the Mali bridge forwards the ++ // phone's own driver's answer) can still export the memory as a dma-buf: the bridge's ++ // swapchains do exactly that. Try those rather than assert (rendervulkan.cpp, CVulkanTexture::BInit). ++ if ( modifiers.empty() && !unexportable.empty() ) ++ { ++ vk_log.errorf( "flippable image: no modifier reported exportable for DRM format 0x%x; trying %zu supported one(s) anyway", ++ drmFormat, unexportable.size() ); ++ modifiers = unexportable; ++ } ++ if ( modifiers.empty() ) ++ { ++ vk_log.errorf( "flippable image: no usable modifier for DRM format 0x%x (%zu offered by the backend)", drmFormat, numPossibleModifiers ); ++ return false; ++ } + + modifierListInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT, diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index bf98ca6a62..9d92f919bf 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -1184,11 +1184,31 @@ BUSCONF # the client quit ("UpdateUI GL not available"). One probe the way the client asks, with the # loader's reasons in the log; when GL does not start, the client's GL goes to llvmpipe. if [ "${BL_VENUS:-0}" = 1 ] && [ -x /usr/local/bin/droiddeck-glprobe ] && [ "${GALLIUM_DRIVER:-}" = zink ]; then - LIBGL_DEBUG=verbose MESA_LOG_LEVEL=debug timeout 20 /usr/local/bin/droiddeck-glprobe 2>&1 | head -60 | sed 's/^/== glprobe: /' + LIBGL_DEBUG=verbose MESA_LOG_LEVEL=debug timeout 20 /usr/local/bin/droiddeck-glprobe 2>&1 | grep -v drirc | head -60 | sed 's/^/== glprobe: /' if [ "${PIPESTATUS[0]}" = 1 ]; then - export GALLIUM_DRIVER=llvmpipe LIBGL_ALWAYS_SOFTWARE=1 - unset MESA_LOADER_DRIVER_OVERRIDE LIBGL_KOPPER_DRI2 - echo "== venus: GL through Zink did not start (see glprobe above); the Steam client's GL uses llvmpipe (software)" + # Zink failed without a word on a Mali-G615 (instance up, then "failed to load driver"). + # The loader's view first, then the variants that change what Zink asks of the device; + # the first that starts is what the client gets. + VK_LOADER_DEBUG=error,warn timeout 20 /usr/local/bin/droiddeck-glprobe 2>&1 | grep -v drirc | head -40 | sed 's/^/== glprobe (loader): /' + bl_gl_ok= + for bl_variant in "ZINK_DEBUG=" "ZINK_DEBUG= ZINK_DESCRIPTORS=auto" "ZINK_DEBUG= LIBGL_KOPPER_DISABLE=true" "ZINK_DEBUG= DROIDDECK_MALI_COMPAT=0"; do + if env $bl_variant timeout 20 /usr/local/bin/droiddeck-glprobe 2>&1 | grep -v drirc | head -20 | sed "s/^/== glprobe ($bl_variant): /"; [ "${PIPESTATUS[0]}" = 0 ]; then + bl_gl_ok=$bl_variant + break + fi + done + if [ -n "$bl_gl_ok" ] && [ "$bl_gl_ok" != "ZINK_DEBUG= DROIDDECK_MALI_COMPAT=0" ]; then + for bl_kv in $bl_gl_ok; do + case $bl_kv in *=) unset "${bl_kv%=}" ;; *) export "$bl_kv" ;; esac + done + echo "== venus: GL through Zink starts with: $bl_gl_ok (the Steam client gets that)" + else + [ -n "$bl_gl_ok" ] && echo "== venus: Zink starts only without the Mali compatibility layer (then without nullDescriptor); not used" + export GALLIUM_DRIVER=llvmpipe LIBGL_ALWAYS_SOFTWARE=1 + unset MESA_LOADER_DRIVER_OVERRIDE LIBGL_KOPPER_DRI2 + echo "== venus: GL through Zink did not start (see glprobe above); the Steam client's GL uses llvmpipe (software)" + fi + unset bl_gl_ok bl_variant bl_kv fi fi while :; do diff --git a/tools/linuxfs/preload/drm.c b/tools/linuxfs/preload/drm.c index e1c08a7005..7fbd724e3a 100644 --- a/tools/linuxfs/preload/drm.c +++ b/tools/linuxfs/preload/drm.c @@ -54,7 +54,7 @@ static int is_kgsl(int fd) { /* KGSL, or the device the Mali bridge's session names as its render node (BL_DRM_STANDIN). */ const char *standin = getenv("BL_DRM_STANDIN"); kgsl_state = stat("/dev/kgsl-3d0", &st) == 0 || (standin && *standin && stat(standin, &st) == 0) ? 1 : -1; - kgsl_dev = st.st_rdev; + if (kgsl_state > 0) kgsl_dev = st.st_rdev; } pthread_mutex_unlock(&lock); return kgsl_state > 0 && fstat(fd, &st) == 0 && S_ISCHR(st.st_mode) && st.st_rdev == kgsl_dev; @@ -110,12 +110,63 @@ static int same_file_description(int a, int b) { #endif } +/* + * Android kernels before 6.10 have no F_DUPFD_QUERY, while the app seccomp policy blocks kcmp. + * F_SETOWN_EX updates the open file description, so a duplicate observes a temporary owner marker + * and an independent open of the same render node does not. Restore the original owner before + * returning; no signal is armed because these DRM descriptors do not have O_ASYNC set. + */ +static int same_owner_description(int owned, int fd) { + struct f_owner_ex original_owned, original_fd, observed; + if (fcntl(owned, F_GETOWN_EX, &original_owned) < 0 || + fcntl(fd, F_GETOWN_EX, &original_fd) < 0) { + return -1; + } + const struct f_owner_ex candidates[] = { + {.type = F_OWNER_TID, .pid = getpid()}, + {.type = F_OWNER_PID, .pid = getpid()}, + {.type = F_OWNER_PGRP, .pid = getpgrp()}, + }; + const struct f_owner_ex *marker = NULL; + for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++) { + if (candidates[i].type != original_fd.type || candidates[i].pid != original_fd.pid) { + marker = &candidates[i]; + break; + } + } + if (!marker || fcntl(owned, F_SETOWN_EX, marker) < 0) return -1; + int observed_result = fcntl(fd, F_GETOWN_EX, &observed); + int observed_errno = errno; + int restore_result = fcntl(owned, F_SETOWN_EX, &original_owned); + int restore_errno = errno; + if (restore_result < 0) { + errno = restore_errno; + return -1; + } + if (observed_result < 0) { + errno = observed_errno; + return -1; + } + return observed.type == marker->type && observed.pid == marker->pid; +} + static int same_owned_description(int owned, int fd) { int saved_errno = errno; int result = fcntl(owned, F_DUPFD_QUERY, fd); + if (result >= 0) { + errno = saved_errno; + return result == 1; + } +#ifdef SYS_kcmp + long compared = syscall(SYS_kcmp, getpid(), getpid(), 0 /* KCMP_FILE */, owned, fd); + if (compared >= 0) { + errno = saved_errno; + return compared == 0; + } +#endif + int owner_match = same_owner_description(owned, fd); errno = saved_errno; - if (result >= 0) return result == 1; - return same_file_description(owned, fd); + return owner_match == 1; } static int find_handle_locked(int fd, uint32_t handle) { @@ -176,7 +227,11 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) { if (powervr) { owner = fcntl(fd, F_DUPFD_CLOEXEC, 0); if (owner < 0) return -errno; - if (!same_owned_description(owner, fd)) { + /* The old-kernel owner probe mutates shared OFD state briefly; serialize it with lookups. */ + pthread_mutex_lock(&lock); + int owner_matches = same_owned_description(owner, fd); + pthread_mutex_unlock(&lock); + if (!owner_matches) { close(owner); errno = EOPNOTSUPP; return -EOPNOTSUPP; diff --git a/tools/tests/drm_handles_test.c b/tools/tests/drm_handles_test.c index 2e5ce8c8f8..08d337f55e 100644 --- a/tools/tests/drm_handles_test.c +++ b/tools/tests/drm_handles_test.c @@ -20,15 +20,16 @@ * separately exercises the actual query on Android. */ #define TEST_FD_LIMIT 65536 static _Atomic unsigned identities[TEST_FD_LIMIT]; +static struct f_owner_ex owners[TEST_FD_LIMIT]; static unsigned identity_serial = 1; -static int identity_denied, kgsl_present; +static int identity_denied, owner_denied, owner_probe_delay, kgsl_present; static int kernel_error = EINVAL; static int native_imports, native_exports, native_closes; static const char *driver_name = "pvr"; static const char *pvr_node = "/dev/dri/renderD129"; static int fixture_stat(const char *path, struct stat *st); static long fixture_syscall(long number, ...); -static int fixture_fcntl(int fd, int op, int arg); +static int fixture_fcntl(int fd, int op, ...); static void *fixture_dlsym(void *object, const char *name); static int fixture_close(int fd); @@ -68,9 +69,27 @@ static int fixture_stat(const char *path, struct stat *st) { return stat(path, st); } -/* The intercepted DRM calls all take an integer argument. Keep that contract - * explicit; native_import's argument-free F_GETFD uses libc after #undef above. */ -static int fixture_fcntl(int fd, int op, int arg) { +static int fixture_fcntl(int fd, int op, ...) { + va_list ap; + va_start(ap, op); + if (op == F_GETOWN_EX || op == F_SETOWN_EX) { + struct f_owner_ex *owner = va_arg(ap, struct f_owner_ex *); + va_end(ap); + if (owner_denied) { errno = EINVAL; return -1; } + if (fd < 0 || fd >= TEST_FD_LIMIT || !identities[fd] || !owner) { + errno = EBADF; + return -1; + } + if (op == F_GETOWN_EX) { + *owner = owners[identities[fd]]; + } else { + owners[identities[fd]] = *owner; + if (owner_probe_delay && owner->pid) usleep(1000); + } + return 0; + } + int arg = va_arg(ap, int); + va_end(ap); if (op == F_DUPFD_QUERY) { if (identity_denied) { errno = EINVAL; return -1; } if (fd < 0 || arg < 0 || fd >= TEST_FD_LIMIT || arg >= TEST_FD_LIMIT || @@ -144,6 +163,24 @@ static void empty_table(void) { for (int i = 0; i < MAX_HANDLES; ++i) assert(!handle_entries[i].handle); } +struct fallback_stress { + int drm; + int dma; + int failures; +}; + +static void *stress_fallback_imports(void *opaque) { + struct fallback_stress *stress = opaque; + for (int i = 0; i < 20; i++) { + uint32_t handle = 0; + if (drmPrimeFDToHandle(stress->drm, stress->dma, &handle) != 0 || + drmCloseBufferHandle(stress->drm, handle) != 0) { + stress->failures++; + } + } + return NULL; +} + int main(void) { int drm = test_open("/dev/null"), dma = test_open("/dev/zero"); uint32_t handle = 0; @@ -176,8 +213,37 @@ int main(void) { int baseline = count_fds(); identity_denied = 1; + assert(drmPrimeFDToHandle(drm, dma, &handle) == 0 && (handle & PVR_HANDLE_BIT)); + int denied_alias = fixture_fcntl(drm, F_DUPFD_CLOEXEC, 0), denied_out = -1; + int denied_separate = test_open("/dev/null"); + assert(drmPrimeHandleToFD(denied_separate, handle, O_CLOEXEC, &denied_out) != 0); + assert(drmPrimeHandleToFD(denied_alias, handle, O_CLOEXEC, &denied_out) == 0); + fixture_close(denied_out); + assert(drmCloseBufferHandle(denied_alias, handle) == 0); + fixture_close(denied_alias); + fixture_close(denied_separate); + assert(count_fds() == baseline); + owner_denied = 1; assert(drmPrimeFDToHandle(drm, dma, &handle) == -EOPNOTSUPP); assert(count_fds() == baseline); + owner_denied = 0; + + /* Marker changes and restoration must be atomic across concurrent imports of one DRM OFD. */ + owner_probe_delay = 1; + struct fallback_stress stress[8]; + pthread_t workers[8]; + for (int i = 0; i < 8; i++) { + stress[i] = (struct fallback_stress){.drm = drm, .dma = dma}; + assert(pthread_create(&workers[i], NULL, stress_fallback_imports, &stress[i]) == 0); + } + for (int i = 0; i < 8; i++) { + assert(pthread_join(workers[i], NULL) == 0); + assert(stress[i].failures == 0); + } + owner_probe_delay = 0; + assert(owners[identities[drm]].pid == 0); + assert(count_fds() == baseline); + empty_table(); identity_denied = 0; assert(drmPrimeFDToHandle(drm, dma, &handle) == 0 && (handle & PVR_HANDLE_BIT));