Skip to content

[VP] Fix vaSyncSurface race on a VP context's status table - #2074

Open
jwalton wants to merge 1 commit into
intel:masterfrom
SolinkCorp:status-table-mutex
Open

jwalton wants to merge 1 commit into
intel:masterfrom
SolinkCorp:status-table-mutex

Conversation

@jwalton

@jwalton jwalton commented Nov 17, 2026 •

Copy link
Copy Markdown

There's a memory safety issue in the VPHAL_STATUS_TABLE which causes vaSyncSurface to sometimes return VA_STATUS_ERROR_OPERATION_FAILED (0x1) for a job that has finished successfully. Basically we have an application with several threads that share one VA display and one vpp context. Each thread scales into its own target surface. The application serializes every call that names the shared context (vaCreateBuffer, vaBeginPicture, vaRenderPicture, vaEndPicture) with a mutex. It then releases the mutex and calls vaSyncSurface on its own target, so threads can wait for the GPU in parallel.

When this fails:

  • vaQuerySurfaceStatus reports VASurfaceReady for the target.
  • The target holds the correct output.
  • Calling vaSyncSurface on the same surface again succeeds, usually at once, sometimes after 1-10 ms.

While another thread is creating and destroying surfaces, as a video decoder does when it's reset, every run fails on about 0.5-0.8% of vaSyncSurface calls. It fails with as few as two threads. It doesn't fail with one thread, with a VPP context per thread, or when the test application holds a mutex across vaSyncSurface.

The cause is in DdiMedia_StatusCheck. For a VPP surface, it removes entries from the VPP context's status table, and marks the surface each entry names as completed, without holding any lock:

  1. Thread B, syncing its own surface, removes thread A's entry from the table.
  2. Thread B then needs SurfaceMutex to look up A's surface, and has to wait for it, because another thread is
    creating or destroying a surface.
  3. A's vaSyncSurface finds its own entry gone and its surface still marked pending. It returns
    VA_STATUS_ERROR_OPERATION_FAILED.

In a run of the program on an instrumented build of the driver, all 269 failing calls came from that return.

Reproduction

There's a (claude generated) minimal reproduction here: https://gist.github.com/jwalton/9ea6fcd2cfa0a8d66c0a8187b6f84046

The program has:

  • 8 worker threads, one VPP context and one mutex.
  • One more thread that creates and destroys 16 surfaces in a loop.

Each worker thread scales an 854x480 NV12 source into its own 764x430 NV12 target, 5,000 times. It submits with the
mutex held, then releases the mutex and calls vaSyncSurface. Each worker alternates between two sources: one
filled with luma 50, one with luma 200. When vaSyncSurface fails, the worker checks three things:

  • what vaQuerySurfaceStatus reports for the target;
  • that the middle of the target holds the luma it should;
  • whether vaSyncSurface succeeds if called again: at once, then after 1, 10 and 100 ms.

Options:

  • -c gives each worker thread its own VPP context.
  • -l holds the mutex across vaSyncSurface too.
  • -s turns off the thread that creates and destroys surfaces.

Build and run:

gcc -O2 -Wall -pthread -o vpp-shared-sync vpp-shared-sync.c -lva -lva-drm
./vpp-shared-sync

Output:

Intel iHD driver for Intel(R) Gen Graphics - 26.3.5 (1b5e662)
8 threads x 5000 jobs, one shared VPP context, vaSyncSurface outside the mutex, surfaces being created and destroyed
209 of 40000 vaSyncSurface calls failed (535 jobs/s)
  first error: operation failed (0x1)
  targets vaQuerySurfaceStatus didn't report ready: 0
  targets with the wrong output: 0
  vaSyncSurface called again succeeded at once: 185, after 1 ms: 19, after 10 ms: 5, after 100 ms: 0, never: 0

Expected: vaSyncSurface returns VA_STATUS_SUCCESS for every job, since every job finished and produced
the right output.

  DdiMedia_StatusCheck removes entries from a VP context's status table
  with GetStatusReport and marks the surface each entry names as
  completed, until it reaches the surface being synced. vaEndPicture adds
  an entry to the same table in Render. Neither holds a lock, but an
  application may call vaSyncSurface from any thread on surfaces that a
  shared VP context rendered.

  When one thread has removed another thread's entry but not yet marked
  its surface, the other thread's StatusCheck doesn't find its entry, its
  surface is still pending, and vaSyncSurface returns
  VA_STATUS_ERROR_OPERATION_FAILED although the job has finished. Calling
  vaSyncSurface again succeeds. The table's head and tail are also
  updated by several threads without synchronization.
  - Add StatusMutex to DDI_VP_CONTEXT, destroyed with the context.
    StatusCheck holds it while it removes entries and marks their
    surfaces, and EndPicture holds it around Render, which adds the job's
    entry.
  - Stop at the VPREP_NOTAVAILABLE placeholder that GetStatusReport
    returns for an empty table, instead of treating it as an entry for
    VA surface 0.
  - Skip an entry whose surface was destroyed after its job was
    submitted, instead of failing the caller's vaSyncSurface.
  - Make the same changes to DdiVpFunctions::StatusCheck and EndPicture
    in media_softlet.
  Verified on Jasper Lake (media_driver path) with threads sharing one VP
  context, submitting jobs under an application lock and calling
  vaSyncSurface outside it, while surfaces are created and destroyed.
  With 8 threads, 778 of 120000 vaSyncSurface calls failed before the
  change; after it, 0 of 544000 failed with 2 to 64 threads. Throughput
  without surface churn is unchanged (2952 vs 2960 jobs/s).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

VP Video Processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants