Skip to content

Fix fence completion race in virtio-gpu worker#604

Closed
aford173 wants to merge 1 commit intocontainers:stable-1.17.xfrom
aford173:upstream/fix-fence-race
Closed

Fix fence completion race in virtio-gpu worker#604
aford173 wants to merge 1 commit intocontainers:stable-1.17.xfrom
aford173:upstream/fix-fence-race

Conversation

@aford173
Copy link
Copy Markdown

Summary

Fix an out-of-order fence completion race that causes the guest to hang intermittently during heavy GPU
rendering through vDRM/virtio-gpu.

When virglrenderer immediately retires fence N+1 (no GPU work) before the timeline thread signals fence N,
the unconditional insert() in the fence handler overwrites completed_fences[ring] from N+1 back to N.
The worker's process_fence() then sees N+1 > N and adds fence N+1 to the pending list, where it stays
forever — the retirement callback already fired and won't fire again.

The fix uses a max check so only strictly higher fence_ids update the map.

Discovered while bringing up vDRM GPU passthrough on an MT8196 Chromebook (Mali-G725 / Panthor). The hang
was reproducible with glmark2 after ~100K-1.5M frames during multi-pass rendering scenes (desktop blur,
terrain, refract). Simple apps like glxgears ran indefinitely.

Test plan

  • glmark2 full benchmark completes all 33 scenes without hanging (previously hung intermittently at heavy
    multi-pass scenes)
  • glxgears runs indefinitely (unchanged)
  • Score: 674 (MT8196/Mali-G725 via vDRM)

@slp
Copy link
Copy Markdown
Collaborator

slp commented Mar 25, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the virtio_gpu module to correctly handle out-of-order fence completions. Specifically, it updates the logic for completed_fences to ensure that the highest fence_id is always retained for each ring, preventing a lower fence_id from overwriting a higher one. The reviewer suggests a more concise implementation for this max logic using u64::max.

Comment on lines +212 to +214
if completed_fence.fence_id > *entry {
*entry = completed_fence.fence_id;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic can be expressed more concisely by using u64::max.

            *entry = (*entry).max(completed_fence.fence_id);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slp - I have updated this accordingly.

When fences complete out of order (e.g., an immediate-retire for fence
N+1 arrives before the timeline signal for fence N), the unconditional
insert() would overwrite the higher fence_id with the lower one. This
causes fence N+1 to appear incomplete forever, hanging the guest.

Use entry().or_insert() with a max check so only strictly higher
fence_ids update the completed_fences map.

Signed-off-by: Adam Ford <adam.ford@anodize.com>
@aford173 aford173 force-pushed the upstream/fix-fence-race branch from b862bb1 to b9ae63e Compare March 25, 2026 15:50
Copy link
Copy Markdown
Collaborator

@mz-pdm mz-pdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.
The CI can be fixed by rebasing on main, I think.

@slp
Copy link
Copy Markdown
Collaborator

slp commented Mar 27, 2026

Let's merge #608 instead. It'll be cherry-picked into stable.

@slp slp closed this Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants