From edbe0c7179abdd0fc8ab15732b04120146ed9fba Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:55:54 -0500 Subject: [PATCH] fix(quest): drop a KGSL context's tracking before its destroy ioctl and restore it on failure --- tools/quest/kgsl_compat.c | 41 ++++++++++++++++++++++++++-------- tools/quest/test_kgsl_compat.c | 8 +++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/tools/quest/kgsl_compat.c b/tools/quest/kgsl_compat.c index e834062e0..c8cf409c9 100644 --- a/tools/quest/kgsl_compat.c +++ b/tools/quest/kgsl_compat.c @@ -80,10 +80,29 @@ static void track_submit(unsigned int context, unsigned int timestamp) { tracked_release(); } -static void track_destroy(unsigned int context) { +static struct tracked_context track_detach(unsigned int context) { + struct tracked_context gone = {0}; tracked_acquire(); int slot = tracked_find(context, 0); - if (slot >= 0) tracked[slot].context = 0; + if (slot >= 0) { + gone = tracked[slot]; + tracked[slot].context = 0; + } + tracked_release(); + return gone; +} + +static void track_restore(struct tracked_context entry) { + if (entry.context == 0) return; + tracked_acquire(); + if (tracked_find(entry.context, 0) < 0) { + int slot = tracked_find(entry.context, 1); + if (slot >= 0) { + unsigned int generation = tracked[slot].generation; + tracked[slot] = entry; + tracked[slot].generation = generation; + } + } tracked_release(); } @@ -120,14 +139,18 @@ static int read_timestamp(quest_ioctl_fn real, int fd, struct kgsl_cmdstream_rea } int quest_kgsl_ioctl(quest_ioctl_fn real, int fd, unsigned long request, void *arg) { + struct tracked_context detached = {0}; + if (request == IOCTL_KGSL_DRAWCTXT_DESTROY && arg != NULL) + detached = track_detach(((const struct kgsl_drawctxt_destroy *)arg)->drawctxt_id); int rc = real(fd, request, arg); - if (rc == 0 && arg != NULL) { - if (request == IOCTL_KGSL_GPU_COMMAND) { - const struct kgsl_gpu_command *submit = arg; - track_submit(submit->context_id, submit->timestamp); - } else if (request == IOCTL_KGSL_DRAWCTXT_DESTROY) { - track_destroy(((const struct kgsl_drawctxt_destroy *)arg)->drawctxt_id); - } + if (request == IOCTL_KGSL_DRAWCTXT_DESTROY && rc != 0) { + int error = errno; + track_restore(detached); + errno = error; + } + if (rc == 0 && arg != NULL && request == IOCTL_KGSL_GPU_COMMAND) { + const struct kgsl_gpu_command *submit = arg; + track_submit(submit->context_id, submit->timestamp); } if (rc != -1 || (errno != EACCES && errno != EPERM) || arg == NULL) return rc; diff --git a/tools/quest/test_kgsl_compat.c b/tools/quest/test_kgsl_compat.c index 36aa240c2..52a4ef2ab 100644 --- a/tools/quest/test_kgsl_compat.c +++ b/tools/quest/test_kgsl_compat.c @@ -46,8 +46,10 @@ static struct kgsl_gpumem_alloc_id reset(void) { static int read_error, wait_error, waits; static unsigned int waited_for; static void (*during_wait)(void); +static int destroy_error; static int timestamp_mock(int fd, unsigned long request, void *arg) { assert(fd == 42); + if (request == IOCTL_KGSL_DRAWCTXT_DESTROY && destroy_error) { errno = destroy_error; return -1; } if (request == IOCTL_KGSL_GPU_COMMAND || request == IOCTL_KGSL_DRAWCTXT_DESTROY) return 0; if (request == IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_CTXTID) { errno = read_error; @@ -115,6 +117,12 @@ static void timestamp_scenarios(void) { assert(read_ts(8, KGSL_TIMESTAMP_RETIRED, &ts) == 0 && ts == 0 && waited_for == 20); wait_error = 0; assert(read_ts(8, KGSL_TIMESTAMP_RETIRED, &ts) == 0 && ts == 20); + submit(9, 4); + struct kgsl_drawctxt_destroy refused = {.drawctxt_id = 9}; + destroy_error = EBUSY; + assert(quest_kgsl_ioctl(timestamp_mock, 42, IOCTL_KGSL_DRAWCTXT_DESTROY, &refused) == -1 && errno == EBUSY); + destroy_error = 0; + assert(read_ts(9, KGSL_TIMESTAMP_QUEUED, &ts) == 0 && ts == 4); read_error = EIO; submit(6, 3); assert(read_ts(6, KGSL_TIMESTAMP_QUEUED, &ts) == -1 && errno == EIO);