Skip to content

fix(usm): rebind stale svm kernel args at enqueue - #1063

Draft
rajathkumarravi wants to merge 1 commit into
intel:masterfrom
rajathkumarravi:ravirajath/usm_stale_kernel_arg
Draft

rajathkumarravi wants to merge 1 commit into
intel:masterfrom
rajathkumarravi:ravirajath/usm_stale_kernel_arg

Conversation

@rajathkumarravi

Copy link
Copy Markdown

Summary

USM/SVM kernel arguments cache the GraphicsAllocation behind the pointer. If the app frees that memory and a new allocation lands at the same address (for example after an empty host USM pool is trimmed), the next enqueue used the freed allocation: a crash, or a GPU fault because the new memory was never made resident.

How it is fixed: SVMAllocsManager now counts frees. At enqueue, a USM/SVM argument is looked up again by its pointer, but only if something was freed since it was set, and is re-bound to the current allocation (or dropped if nothing is there). If nothing was allocated or freed since the kernel's last enqueue, the check is just two atomic loads.

Reproduction

  1. Set a host USM pointer (from a USM pool) as a kernel argument, enqueue, finish.
  2. Free it. The empty pool is trimmed; with the reuse cache on, the memory is released after ~10 s idle (NEOReadDebugKeys=1 ExperimentalEnableHostAllocationCache=0 makes it immediate).
  3. Allocate again and get the same address. Enqueue without setting the argument again: SIGSEGV in Kernel::makeArgsResident, or CL_OUT_OF_RESOURCES at finish.

Frameworks that skip clSetKernelArg* when the pointer did not change (e.g. the OpenVINO GPU plugin) hit this. Seen on PTL (Windows); reproduced on Linux with current master.

Changes

  • SVMAllocsManager::freedAllocationsCounter: bumped in removeSVMAlloc / freeSVMData. Moving an allocation into the reuse cache does not count; it stays alive there.
  • Kernel::revalidateSvmAllocArgs(): called from CommandQueueHw::enqueueKernel, next to the shared-object re-patch. It re-binds through setArgSvmAlloc, so surface state and allocId are refreshed.
  • cloneKernel and getAllocationsInfo resolve such arguments by pointer instead of using a possibly freed allocation.
  • Kernel state is only updated inside enqueue, like the existing work-size and shared-object patching; the counters are atomics.

Testing

  • 15 new ULTs (14 OpenCL, 1 shared). gcov: every new line and branch is covered. Each of 11 single-condition mutations of the new code makes at least one test fail.
  • All ULT suites (OpenCL, shared, L0 core/tools/sysman, incl. mt) on all 16 target_unit_tests configs: ~558k test runs, 0 failures.
  • gcc 13 and clang 18 with -Werror; clang-format and clang-tidy (repo configs) clean.
  • PTL GPU (Linux driver via WSL2): master crashes (SIGSEGV / CL_OUT_OF_RESOURCES). With the fix, ~1,000 stress cycles pass and ASan is clean.
  • Enqueue cost with 8 USM arguments:
    • steady state: +16 ns on ~1.64 us (within noise);
    • worst case, a free before every enqueue: +0.5 us on ~25 us.

Not in this PR

  • clSetKernelExecInfo pointer lists and Level Zero kernel arguments cache allocations the same way.
  • Windows build not verified (needs Intel's build environment).

clSetKernelArgMemPointerINTEL and clSetKernelArgSVMPointer cache the
GraphicsAllocation behind the pointer. When that allocation is released
(e.g. an emptied host usm pool is trimmed) and a new one is created at the
same address, enqueueing the kernel without setting the argument again used
the released allocation: makeArgsResident and getResidency read freed
memory and the new allocation was never made resident.

Count allocations leaving the svm tracker and, at enqueue, re-resolve
pointer arguments whose allocation may have been released since they were
resolved. An argument whose pointer is no longer allocated drops the cached
allocation and is looked up again once something is allocated. Arguments
are not checked at all when no allocation was created or released since
the previous check. clCloneKernel resolves outdated arguments of the source
kernel instead of copying their allocation, and
CL_KERNEL_ALLOCATIONS_INFO_INTEL resolves arguments by pointer value.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant