Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

xnu-drm: avoid blocking XNU CPUs in hostcall 830 during GPU waits

Heyang Zhoub87a1a9d81f5
diff --git a/gpu/xnu-drm/darwin/xnudrm.c b/gpu/xnu-drm/darwin/xnudrm.c
index 66aa939..33b8b53 100644
--- a/gpu/xnu-drm/darwin/xnudrm.c
+++ b/gpu/xnu-drm/darwin/xnudrm.c
@@ -267,6 +267,30 @@ static int do_ioctl(unsigned long request, void *arg)
int drmIoctl(int fd, unsigned long request, void *arg)
{
+ /* A blocking VIRTGPU_WAIT would hold the calling XNU CPU inside fki_hostcall (deaf to IPIs):
+ * poll with VIRTGPU_WAIT_NOWAIT and sleep in macOS userspace with exponential backoff up to 2 ms. */
+ if ((request & 0xff) == DRM_COMMAND_BASE + DRM_VIRTGPU_WAIT && arg &&
+ !(((struct drm_virtgpu_3d_wait *)arg)->flags & VIRTGPU_WAIT_NOWAIT)) {
+ long delay_ns = 100000; /* 100 us -> 2 ms */
+ for (;;) {
+ struct drm_virtgpu_3d_wait nw = *(struct drm_virtgpu_3d_wait *)arg;
+ nw.flags |= VIRTGPU_WAIT_NOWAIT;
+ pthread_mutex_lock(&lk);
+ conn = node_conn(fd);
+ int r = conn ? do_ioctl(request, &nw) : -EBADF;
+ pthread_mutex_unlock(&lk);
+ if (r != -EBUSY) {
+ if (r) { errno = -r; return -1; }
+ return 0;
+ }
+ struct timespec d = { 0, delay_ns };
+ nanosleep(&d, NULL);
+ if (delay_ns < 2000000) {
+ delay_ns <<= 1;
+ if (delay_ns > 2000000) delay_ns = 2000000;
+ }
+ }
+ }
pthread_mutex_lock(&lk);
conn = node_conn(fd);
int r = conn ? do_ioctl(request, arg) : -EBADF;
@@ -353,6 +377,7 @@ static int fence_check(struct xfence *f)
int xnudrm_poll(struct pollfd *fds, nfds_t n, int timeout)
{
struct timespec t0, now;
+ long delay_ns = 100000; /* 100 us -> 2 ms */
clock_gettime(CLOCK_MONOTONIC, &t0);
for (;;) {
int pending = 0;
@@ -371,9 +396,13 @@ int xnudrm_poll(struct pollfd *fds, nfds_t n, int timeout)
clock_gettime(CLOCK_MONOTONIC, &now);
long ms = (now.tv_sec - t0.tv_sec) * 1000 + (now.tv_nsec - t0.tv_nsec) / 1000000;
if (timeout >= 0 && ms >= timeout) return poll(fds, n, 0);
- /* each check stops the hosted CPU for one host call: back off a little */
- struct timespec d = { 0, 200000 };
+ /* each check stops the hosted CPU for one host call: back off exponentially up to 2 ms */
+ struct timespec d = { 0, delay_ns };
nanosleep(&d, NULL);
+ if (delay_ns < 2000000) {
+ delay_ns <<= 1;
+ if (delay_ns > 2000000) delay_ns = 2000000;
+ }
}
}
diff --git a/gpu/xnu-drm/xnudrmd.c b/gpu/xnu-drm/xnudrmd.c
index 97772c1..1b40bf1 100644
--- a/gpu/xnu-drm/xnudrmd.c
+++ b/gpu/xnu-drm/xnudrmd.c
@@ -229,7 +229,7 @@ static void serve(uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
if (h->op == XNUDRM_FENCE_CLOSE) { close(f->fd - 1); f->fd = 0; break; }
struct pollfd p = { .fd = f->fd - 1, .events = POLLIN };
int ms = h->arg1 ? (int)((h->arg1 + 999999) / 1000000) : 0;
- int r = poll(&p, 1, ms > 60000 ? 60000 : ms); /* the hosted CPU waits in the call: never longer than a minute */
+ int r = poll(&p, 1, ms > 50 ? 50 : ms); /* never hold the hosted CPU in a host call longer than 50 ms */
st = r < 0 ? -errno : r == 0 ? -XNUDRM_ETIME : 0;
break;
}