From f14e9c6f4c503218b046e3e6323cbd2ee350a687 Mon Sep 17 00:00:00 2001 From: njsyw1997 <44545837+njsyw1997@users.noreply.github.com> Date: Sat, 11 Apr 2026 16:32:52 -0700 Subject: [PATCH] hexagon: fix futex race in hmx_worker_drain Store the boolean to local variable avoid atomic load twice --- ggml/src/ggml-hexagon/htp/hmx-worker.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-hexagon/htp/hmx-worker.c b/ggml/src/ggml-hexagon/htp/hmx-worker.c index 6826dde7de..013173c7e5 100644 --- a/ggml/src/ggml-hexagon/htp/hmx-worker.c +++ b/ggml/src/ggml-hexagon/htp/hmx-worker.c @@ -98,8 +98,15 @@ static void hmx_worker_issue(struct hmx_worker_context * ctx, // Block until the worker has completed the most recently issued command. static void hmx_worker_drain(struct hmx_worker_context * ctx) { unsigned int expected = atomic_load_explicit(&ctx->cmd_seqn, memory_order_acquire); - while (atomic_load_explicit(&ctx->done_seqn, memory_order_acquire) != expected) { - qurt_futex_wait(&ctx->done_seqn, atomic_load_explicit(&ctx->done_seqn, memory_order_relaxed)); + for (;;) { + unsigned int seen = atomic_load_explicit(&ctx->done_seqn, memory_order_acquire); + if (seen == expected) { + return; + } + // Pass the same observed value to futex_wait(). If the worker completes + // between the load above and the futex call, the value mismatch makes the + // wait return immediately instead of sleeping forever on the new seqn. + qurt_futex_wait(&ctx->done_seqn, seen); } }