Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 46 additions & 48 deletions linux/kwin_gpu_interposer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>

#include <algorithm>
#include <array>
#include <atomic>
Expand All @@ -10,20 +6,23 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <fcntl.h>
#include <gbm.h>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <unistd.h>
#include <unordered_map>
#include <vector>

#include <gbm.h>
#include <xf86drm.h>

#ifdef VIBESHINE_KWIN_NATIVE_ALLOCATOR
#include <core/graphicsbufferallocator.h>
#include "kwin_native_abi.h"

#include <core/graphicsbufferallocator.h>
#endif

namespace {
Expand All @@ -48,10 +47,12 @@ namespace {
std::atomic<bool> hooks_enabled {false};
std::atomic<bool> native_hooks_enabled {false};
std::mutex render_fds_mutex;

struct allocator_fds {
int render;
int kms;
};

std::unordered_map<gbm_device *, allocator_fds> owned_render_fds;
thread_local bool software_allocation = false;

Expand All @@ -69,13 +70,15 @@ namespace {
// GraphicsBufferOptions is a C++ ABI. Only read the layout of the exact
// KWin library whose public headers were used to build this bridge.
const auto probe = vibeshine::kwin::probe_native_abi(
native_allocate_symbol, VIBESHINE_KWIN_NATIVE_LIBRARY_NAME);
native_allocate_symbol,
VIBESHINE_KWIN_NATIVE_LIBRARY_NAME
);
if (report_failure) {
vibeshine::kwin::report_native_abi_failure(stderr, probe, VIBESHINE_KWIN_NATIVE_LIBRARY_NAME);
}
return probe.result == vibeshine::kwin::native_abi_result::compatible;
#else
(void)report_failure;
(void) report_failure;
#endif
return false;
}
Expand All @@ -95,14 +98,14 @@ namespace {

bool is_vibeshine_display(const drmDevicePtr device) {
return device && device->bustype == DRM_BUS_FAUX && device->businfo.faux &&
std::strcmp(device->businfo.faux->name, "vibeshine") == 0;
std::strcmp(device->businfo.faux->name, "vibeshine") == 0;
}

bool is_nvidia_render_device(const drmDevicePtr device) {
return device && device->bustype == DRM_BUS_PCI && device->businfo.pci &&
device->deviceinfo.pci && device->deviceinfo.pci->vendor_id == 0x10de &&
(device->available_nodes & (1 << DRM_NODE_RENDER)) != 0 &&
device->nodes && device->nodes[DRM_NODE_RENDER];
device->deviceinfo.pci && device->deviceinfo.pci->vendor_id == 0x10de &&
(device->available_nodes & (1 << DRM_NODE_RENDER)) != 0 &&
device->nodes && device->nodes[DRM_NODE_RENDER];
}

std::optional<drmPciBusInfo> parse_pci_device(const char *value) {
Expand All @@ -111,8 +114,7 @@ namespace {
unsigned int device = 0;
unsigned int function = 0;
char trailing = '\0';
if (std::sscanf(value, "%x:%x:%x.%x%c", &domain, &bus, &device, &function, &trailing) != 4 ||
domain > 0xffff || bus > 0xff || device > 0x1f || function > 7) {
if (std::sscanf(value, "%x:%x:%x.%x%c", &domain, &bus, &device, &function, &trailing) != 4 || domain > 0xffff || bus > 0xff || device > 0x1f || function > 7) {
return std::nullopt;
}
return drmPciBusInfo {
Expand All @@ -125,32 +127,32 @@ namespace {

bool pci_matches(const drmPciBusInfo &actual, const drmPciBusInfo &expected) {
return actual.domain == expected.domain && actual.bus == expected.bus &&
actual.dev == expected.dev && actual.func == expected.func;
actual.dev == expected.dev && actual.func == expected.func;
}

bool pci_precedes(const drmPciBusInfo &left, const drmPciBusInfo &right) {
return std::tie(left.domain, left.bus, left.dev, left.func) <
std::tie(right.domain, right.bus, right.dev, right.func);
std::tie(right.domain, right.bus, right.dev, right.func);
}

drmDevicePtr select_render_device(drmDevicePtr devices[], const int count) {
const auto *configured_value = std::getenv(kRenderPciEnvironment.data());
const auto configured = configured_value && *configured_value ?
parse_pci_device(configured_value) : std::optional<drmPciBusInfo> {};
parse_pci_device(configured_value) :
std::optional<drmPciBusInfo> {};
if (configured_value && *configured_value && !configured) {
if (!ambiguous_gpu_logged.exchange(true, std::memory_order_relaxed)) {
constexpr char message[] =
"[vibeshine-kwin-gpu] invalid VIBESHINE_KWIN_RENDER_PCI; refusing virtual GPU association\n";
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
(void) write(STDERR_FILENO, message, sizeof(message) - 1);
}
return nullptr;
}
drmDevicePtr selected = nullptr;
int matches = 0;
for (int index = 0; index < count; ++index) {
auto *const candidate = devices[index];
if (!is_nvidia_render_device(candidate) ||
(configured && !pci_matches(*candidate->businfo.pci, *configured))) {
if (!is_nvidia_render_device(candidate) || (configured && !pci_matches(*candidate->businfo.pci, *configured))) {
continue;
}
if (!selected || pci_precedes(*candidate->businfo.pci, *selected->businfo.pci)) {
Expand All @@ -164,7 +166,7 @@ namespace {
if (matches > 1 && !ambiguous_gpu_logged.exchange(true, std::memory_order_relaxed)) {
constexpr char message[] =
"[vibeshine-kwin-gpu] multiple NVIDIA render devices found; using lowest PCI address for virtual outputs unless VIBESHINE_KWIN_RENDER_PCI selects another\n";
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
(void) write(STDERR_FILENO, message, sizeof(message) - 1);
}
// GBM allocators are created before KWin chooses its primary renderer.
// A stable PCI default avoids software-only virtual outputs on headless
Expand Down Expand Up @@ -205,8 +207,8 @@ namespace {
// the verified real render node for the selected PCI device.
drmDevicePtr opened = nullptr;
const bool matches = drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER &&
get_device(fd, 0, &opened) == 0 && is_nvidia_render_device(opened) &&
pci_matches(*opened->businfo.pci, identity);
get_device(fd, 0, &opened) == 0 && is_nvidia_render_device(opened) &&
pci_matches(*opened->businfo.pci, identity);
drmFreeDevice(&opened);
if (!matches) {
close(fd);
Expand All @@ -220,12 +222,9 @@ namespace {
return;
}
char message[224] {};
const auto length = std::snprintf(message, sizeof(message),
"[vibeshine-kwin-gpu] virtual GBM uses NVIDIA render node %04x:%02x:%02x.%u; seat DRM devices unchanged\n",
identity.domain, identity.bus, identity.dev, identity.func);
const auto length = std::snprintf(message, sizeof(message), "[vibeshine-kwin-gpu] virtual GBM uses NVIDIA render node %04x:%02x:%02x.%u; seat DRM devices unchanged\n", identity.domain, identity.bus, identity.dev, identity.func);
if (length > 0) {
(void)write(STDERR_FILENO, message,
std::min(static_cast<std::size_t>(length), sizeof(message) - 1));
(void) write(STDERR_FILENO, message, std::min(static_cast<std::size_t>(length), sizeof(message) - 1));
}
}

Expand All @@ -251,26 +250,23 @@ namespace {
*
* @param lookup Dynamic symbol lookup, injectable for routing tests.
*/
void configure_allocator_routing(void *(*lookup)(void *, const char *),
bool native_abi_compatible = native_allocator_runtime_compatible()) {
const bool legacy_allocator = lookup(RTLD_DEFAULT,
"_ZN4KWin26GbmGraphicsBufferAllocatorC1EP10gbm_devicem") != nullptr;
const bool native_allocator = lookup(RTLD_DEFAULT,
"_ZN4KWin26GbmGraphicsBufferAllocatorC1EPNS_9DrmDeviceE") != nullptr;
void configure_allocator_routing(void *(*lookup)(void *, const char *), bool native_abi_compatible = native_allocator_runtime_compatible()) {
const bool legacy_allocator = lookup(RTLD_DEFAULT, "_ZN4KWin26GbmGraphicsBufferAllocatorC1EP10gbm_devicem") != nullptr;
const bool native_allocator = lookup(RTLD_DEFAULT, "_ZN4KWin26GbmGraphicsBufferAllocatorC1EPNS_9DrmDeviceE") != nullptr;
bool native_supported = false;
#ifdef VIBESHINE_KWIN_NATIVE_ALLOCATOR
native_supported = native_allocator && !legacy_allocator && native_abi_compatible;
#endif
native_hooks_enabled.store(native_supported, std::memory_order_release);
hooks_enabled.store((legacy_allocator && !native_allocator) || native_supported, std::memory_order_release);
// Always clear the preload, including when the native KMS allocator wins.
(void)unsetenv("LD_PRELOAD");
(void)unsetenv(kPreloadActiveEnvironment.data());
(void) unsetenv("LD_PRELOAD");
(void) unsetenv(kPreloadActiveEnvironment.data());
if (!hooks_enabled.load(std::memory_order_acquire)) {
(void)native_allocator_runtime_compatible(true);
(void) native_allocator_runtime_compatible(true);
constexpr char message[] =
"[vibeshine-kwin-gpu] preserving native KMS allocator for current KWin ABI\n";
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
(void) write(STDERR_FILENO, message, sizeof(message) - 1);
}
}

Expand All @@ -280,7 +276,7 @@ namespace {
}
configure_allocator_routing(dlsym);
}
}
} // namespace

extern "C" gbm_device *gbm_create_device(const int fd) {
const auto function = resolve_real_function(real_gbm_create_device, "gbm_create_device");
Expand All @@ -299,7 +295,7 @@ extern "C" gbm_device *gbm_create_device(const int fd) {
const auto get_device = resolve_real_function(real_drm_get_device2, "drmGetDevice2");
drmDevicePtr device = nullptr;
const bool virtual_display = get_device && get_device(fd, 0, &device) == 0 &&
is_vibeshine_display(device);
is_vibeshine_display(device);
drmFreeDevice(&device);
if (!virtual_display) {
return pass_through();
Expand Down Expand Up @@ -381,8 +377,7 @@ extern "C" int gbm_device_get_fd(gbm_device *device) {
}

#ifdef VIBESHINE_KWIN_NATIVE_ALLOCATOR
extern "C" void *vibeshine_kwin_render_device(const void *, drmDevicePtr)
asm("_ZNK4KWin10GpuManager26findCompatibleRenderDeviceEP10_drmDevice");
extern "C" void *vibeshine_kwin_render_device(const void *, drmDevicePtr) asm("_ZNK4KWin10GpuManager26findCompatibleRenderDeviceEP10_drmDevice");

extern "C" void *vibeshine_kwin_render_device(const void *manager, drmDevicePtr device) {
const auto function = resolve_real_function(real_native_render_device, native_render_device_symbol);
Expand All @@ -400,8 +395,7 @@ extern "C" void *vibeshine_kwin_render_device(const void *manager, drmDevicePtr
drmDevicePtr render = nullptr;
const auto get_device = resolve_real_function(real_drm_get_device2, "drmGetDevice2");
void *selected = nullptr;
if (get_device && get_device(fd, 0, &render) == 0 && is_nvidia_render_device(render) &&
pci_matches(*render->businfo.pci, identity)) {
if (get_device && get_device(fd, 0, &render) == 0 && is_nvidia_render_device(render) && pci_matches(*render->businfo.pci, identity)) {
selected = function(manager, render);
}
drmFreeDevice(&render);
Expand All @@ -414,8 +408,7 @@ extern "C" void *vibeshine_kwin_render_device(const void *manager, drmDevicePtr
return function(manager, device);
}

extern "C" KWin::GraphicsBuffer *vibeshine_kwin_allocate(void *, const KWin::GraphicsBufferOptions &)
asm("_ZN4KWin26GbmGraphicsBufferAllocator8allocateERKNS_21GraphicsBufferOptionsE");
extern "C" KWin::GraphicsBuffer *vibeshine_kwin_allocate(void *, const KWin::GraphicsBufferOptions &) asm("_ZN4KWin26GbmGraphicsBufferAllocator8allocateERKNS_21GraphicsBufferOptionsE");

extern "C" KWin::GraphicsBuffer *vibeshine_kwin_allocate(void *allocator, const KWin::GraphicsBufferOptions &options) {
const auto function = resolve_real_function(real_native_allocate, native_allocate_symbol);
Expand All @@ -428,10 +421,15 @@ extern "C" KWin::GraphicsBuffer *vibeshine_kwin_allocate(void *allocator, const
}
const bool previous = software_allocation;
software_allocation = options.software;

struct restore_scope {
bool previous;
~restore_scope() { software_allocation = previous; }

~restore_scope() {
software_allocation = previous;
}
} restore {previous};

return function(allocator, options);
}
#endif
Expand Down
32 changes: 17 additions & 15 deletions linux/kwin_native_abi.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include <dlfcn.h>
#include <limits.h>

#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <limits.h>

namespace vibeshine::kwin {
enum class native_abi_result {
Expand All @@ -26,10 +25,12 @@ namespace vibeshine::kwin {
// symlink that has changed since load; realpath describes its current target,
// not necessarily the mapped DSO. Do not report this as loaded identity.
inline native_abi_probe probe_native_abi(
const char *symbol, const char *expected_library,
const char *symbol,
const char *expected_library,
void *(*lookup)(void *, const char *) = dlsym,
int (*address)(const void *, Dl_info *) = dladdr,
char *(*resolve)(const char *, char *) = realpath) {
char *(*resolve)(const char *, char *) = realpath
) {
native_abi_probe probe {native_abi_result::missing_allocator};
const auto allocator = lookup(RTLD_NEXT, symbol);
if (!allocator) {
Expand All @@ -51,12 +52,12 @@ namespace vibeshine::kwin {
}
std::snprintf(probe.resolved_library.data(), probe.resolved_library.size(), "%s", name + 1);
probe.result = std::strcmp(name + 1, expected_library) == 0 ?
native_abi_result::compatible : native_abi_result::different_library;
native_abi_result::compatible :
native_abi_result::different_library;
return probe;
}

inline void report_native_abi_failure(FILE *output, const native_abi_probe &probe,
const char *expected_library) {
inline void report_native_abi_failure(FILE *output, const native_abi_probe &probe, const char *expected_library) {
if (probe.result == native_abi_result::compatible) {
return;
}
Expand All @@ -75,11 +76,12 @@ namespace vibeshine::kwin {
break;
}
std::fprintf(output,
"[vibeshine-kwin-gpu] native GPU bridge disabled: %s (built for %s; resolved current pathname %s; mapped library identity unverified). "
"Keeping native KMS allocation; virtual capture may fall back to CPU upload. "
"Install a bridge package rebuilt against the installed KWin, then log out and back in or reboot. "
"Restarting the streaming service alone does not reload the compositor bridge.\n",
reason, expected_library,
probe.resolved_library[0] ? probe.resolved_library.data() : "unknown");
"[vibeshine-kwin-gpu] native GPU bridge disabled: %s (built for %s; resolved current pathname %s; mapped library identity unverified). "
"Keeping native KMS allocation; virtual capture may fall back to CPU upload. "
"Install a bridge package rebuilt against the installed KWin, then log out and back in or reboot. "
"Restarting the streaming service alone does not reload the compositor bridge.\n",
reason,
expected_library,
probe.resolved_library[0] ? probe.resolved_library.data() : "unknown");
}
}
} // namespace vibeshine::kwin
Loading
Loading