You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Serialize cache admission and per-entry initialization to prevent duplicate
upstream IPC Gets. Coordinate retirement with Free and reject delayed Gets
for allocations that are no longer tracked.
Preserve entry identity across address reuse, clean up failed initialization,
and avoid forwarding thread-local provider errors to waiting callers. Skip
retirement allocation for uncached frees after successful tracker removal.
Keep IPC payloads aligned, limit test-helper exports to test builds, and add
deterministic coverage for concurrent Get, failure, Free, and address reuse.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A critical cleanup race can delete a replacement entry and free storage still in use, while waiter error reporting is incorrect and concurrency tests do not verify duplicate-Get prevention.
This PR makes the tracking provider’s IPC cache initialize handles once per allocation, avoiding duplicate upstream Get/Put calls that can crash some Level Zero drivers.
Changes:
Insert a mutex-protected placeholder before fetching an upstream IPC handle.
Make concurrent callers wait for initialization and share its result.
Make free wait for initialization before returning a ready handle, and destroy entry mutexes during cleanup.
File
Description
src/provider/provider_tracking.c
Adds per-entry initialization state and synchronization to IPC cache creation and cleanup.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A critical retirement race allows overlapping upstream Gets and leaves a cached entry after allocation free, while the tests contain a use-after-free and nondeterministic waiter synchronization.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Two critical issues remain: the new field misaligns IPC payloads, causing undefined accesses, and failed-Get cleanup can remove a retiring entry too early, allowing a concurrent Get to cache a handle for an allocation being freed.
Keep failed retiring entries until upstream Free completes
src/provider/provider_tracking.c:1519
If upstream Get fails after trackingFree marks this entry retiring, this cleanup can remove the tombstone before upstream Free returns. Another getter can then start upstream Get on the allocation being freed and leave a replacement handle cached, since free only removes its original entry. Preserve failed entries with is_retiring set and let trackingFree remove them after upstream Free finishes.
ldorau
changed the title
Make tracking IPC cache get-or-create single-flight per allocation
Harden tracking provider IPC cache concurrency
Nov 17, 2026
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A critical race between failed initialization and Free can allow a replacement entry to escape cleanup, leaving a stale cached handle without a matching upstream Put.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
A critical race removes a retiring cache entry after Get initialization fails, allowing a replacement to survive concurrent Free and retain a handle to freed memory.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
A critical race removes a retiring cache entry after a failed Get, allowing a concurrent Get to create a replacement that Free does not clean up and leaving stale IPC data cached across address reuse.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The tests lack deterministic coverage of address reuse while an old Free is blocked, leaving replacement-entry preservation and exactly-once upstream Put behavior unverified.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed to judge the performance impact of adding tombstone allocation, mutex lifecycle work, and tree updates under the cache lock to ordinary frees, even for providers without IPC support.
The before-PR baseline is d0e5e35f (1.3.0 release), the parent of this
commit. This explanation describes the combined result, rather than
treating intermediate problems introduced during PR development as behavior
that necessarily existed in the release.
The relevant implementation is in src/provider/provider_tracking.c; the
concurrency regressions are in test/provider_tracking.cpp.
1. What issue is the PR fixing?
The tracking memory provider keeps an IPC-handle cache keyed by allocation
address. The cache stores provider-specific IPC data and a UMF handle ID. The
same cached provider handle is reused when multiple callers ask for an IPC
handle for the same live allocation. The upstream provider is expected to
receive one successful GetIPCHandle for the cached handle and, when the
allocation is freed, one matching PutIPCHandle call. Failed Get attempts can
be retried, so "single-flight" does not mean only one attempt over the entire
allocation lifetime. An allocation that never exports an IPC handle normally
needs neither an upstream Get nor a Put.
Here, upstream means the real memory provider wrapped by the tracking
provider, such as Level Zero, CUDA, OS memory, or a test provider. Free
means the tracking-provider allocation-free operation, not merely disposal of
a caller's IPC-handle buffer. Public umfPutIPCHandle frees that caller-owned
buffer; it does not release the cached upstream handle. That ownership policy
already existed before this PR (src/ipc.c:119 and the provider's trackingPutIpcHandle).
The baseline had no synchronization coordinating the complete cache-miss,
handle-initialization, and allocation-free sequence. The combined PR addresses
the following problems:
Two concurrent cache misses could both ask the upstream provider to create
an IPC handle for one allocation. Only one result could be inserted under
the allocation's address; the losing result had to be returned immediately.
Some drivers do not tolerate duplicate handle creation reliably.
Free could remove the cache entry while another caller was initializing or
trying to use it. That could leave a handle without a matching Put, make a
caller access an entry that had been freed, or allow a stale entry to be
associated with a later allocation that reused the same address.
Even after adding retirement markers, a delayed Get could arrive after Free
had removed the marker. Without checking that the allocation was still
tracked, the Get could call the upstream provider on freed memory and
install a new cache entry for the old address.
The initial retirement implementation allocated a marker even when Free
found no cached IPC handle. This added allocation work to ordinary frees,
including allocations that never used IPC. A pre-existing unchecked
allocation failure in the base allocator could then abort Free under memory
pressure, before upstream Free was reached. The uncached-Free simplification
removes this newly introduced dependency from the successful, uncached
Free path.
Items 3 and 4 explain why the follow-up fixes were necessary during development:
a marker alone does not cover calls arriving after it is removed, and once
liveness checking exists, allocating a marker for every uncached Free is
unnecessary.
The core difficulty is that a pointer address is not a permanent allocation
identity. After a free, the allocator can immediately reuse that address for a
different allocation. Cache admission and retirement therefore have to agree
on whether an address still represents a live allocation, not just whether a
cache entry currently exists.
2. How did it work before this PR?
An allocation went through trackingAlloc, which called the upstream
allocator and added the returned region to the memory tracker. When a caller
requested an IPC handle, trackingGetIpcHandle roughly did the following:
Look up the allocation address in ipcCache.
On a miss, ask the upstream provider for the IPC-data size, allocate a cache
value, and call upstream GetIPCHandle.
Insert the completed cache value. If another thread had inserted one first,
return the just-created upstream handle with PutIPCHandle and retry/look up
the winner.
Copy the cached provider data and handle ID into the caller's IPC buffer.
Those operations were not coordinated with other Get or Free operations. Two
threads could both pass the miss lookup and enter upstream Get before either
inserted a value. Critnib prevented both values from winning the same key, but
it did not prevent the duplicate upstream work.
For example, the old sequence could be:
Thread A Thread B
lookup(P): miss lookup(P): miss
upstream Get(P) -> handle A upstream Get(P) -> handle B
insert(P, A): succeeds insert(P, B): key already exists
upstream Put(B)
look up A and return its data
Eventually retaining only one entry does not undo the duplicate driver calls.
Even balanced total Get/Put counts are therefore insufficient to prove that
this behavior is safe.
trackingFree removed allocation metadata from the tracker, removed the IPC
cache value by address, returned its upstream handle with PutIPCHandle, and
then called upstream Free. Because Get and Free did not share a synchronization
protocol, Free could race with a Get that was already underway or delayed
before its lookup. A cache entry keyed only by address could also become
ambiguous after address reuse.
3. How does it work with this fix?
Single-flight initialization
Each cache value now carries initialization state (IN_PROGRESS, READY, or FAILED), an initialization mutex, and waiter bookkeeping. A Get cache miss
holds the provider's ipcCacheMutex while it validates the allocation and
inserts an IN_PROGRESS placeholder. It then releases the cache mutex and
performs the upstream Get while holding the entry's initialization mutex.
Other Get callers that find the placeholder retain a critnib reference and wait
on that entry's initialization mutex. If initialization succeeds, they copy the
same provider data and handle ID. If it fails, they receive the failure result;
provider-specific errors are mapped to UNKNOWN for waiters because native
provider diagnostics are often thread-local to the creator.
Waiters also check the retirement flag. A waiter that observes retirement
returns INVALID_ARGUMENT, rather than returning a handle being retired.
The creator copies its successful result before unlocking initialization;
Free cannot Put that handle until the creator releases that lock. This orders
the provider calls, but it does not extend the application's allocation
lifetime after Free.
The new normal sequence is:
Thread A Thread B
lock cache mutex
verify P is tracked
insert locked IN_PROGRESS entry
unlock cache mutex
upstream Get(P) find entry and retain reference
wait for entry initialization
publish READY, data, handle ID
unlock entry copy the same data and handle ID
The cache mutex stays held through the upstream size query and placeholder
insertion, but not through the actual upstream Get. Thus miss setup is
serialized within a tracking provider, while expensive Gets for different
allocations can overlap.
Free and retirement
Free and cache-miss admission use the same ipcCacheMutex. Free removes the
allocation from the tracker while holding that mutex. What happens next
depends on whether a cache entry exists and whether tracker removal succeeded:
Condition
Current behavior
An IPC cache entry exists
Mark it retiring, retain its reference, and wait for initialization if necessary.
No entry exists and tracker removal succeeded
Release the cache mutex and call upstream Free; do not allocate or insert a tombstone.
No entry exists and tracker removal failed
Attempt to insert a retiring tombstone, because liveness checking cannot be relied on to reject Gets. If marker setup returns failure, hold the cache mutex through upstream Free as a fallback.
The tracker-removal failure is logged; the tracking provider still attempts
upstream Free rather than turning a bookkeeping error into an early return.
The uncached-Free simplification preserves this exceptional path, including
its allocation dependency. It does not repair the underlying base allocator's
unchecked allocation-failure behavior.
If Get is already initializing an entry, Free waits for initialization to
finish. It Puts the upstream handle only if initialization reached READY,
then calls upstream Free. After that it removes the retiring cache entry only
if the entry at the address is still the exact entry it retired. Failed-Get
cleanup likewise leaves a retiring entry for Free to remove.
When an allocation is returned at an address whose cache entry is still
retiring, trackingAlloc removes that old entry before registering the new
allocation. Identity checks in the old Free prevent its cleanup from deleting a
different entry inserted for the new allocation.
For example:
Old Free New allocation at the same address
mark entry E1 retiring
Put old upstream handle
enter upstream Free
address becomes reusable upstream Alloc returns P
Free has not returned yet trackingAlloc removes retiring E1
register new allocation P
Get creates entry E2 and a new handle ID
upstream Free returns
lookup(P) finds E2, not E1
leave E2 in the cache
release reference to E1
The reference keeps E1's storage alive while the old Free still needs it.
The identity comparison keeps that old Free from deleting E2. These solve
different problems and are both needed in this architecture.
Rejecting Gets delayed until after Free
The allocation-liveness check closes a gap that tombstones alone do not cover.
A Get can be delayed before it reaches the provider's first cache lookup. Free may complete
and remove the tombstone during that delay. When the Get resumes, the cache
would be empty, so it could otherwise create a new placeholder for a freed
allocation.
On a cache miss, while holding ipcCacheMutex, the provider now checks the
allocation tracker for the address in the relevant address space and pool. Free
removes tracker metadata under the same mutex. Thus:
If Get acquires the mutex first, it sees the live allocation and publishes
its placeholder before Free can retire it. Free will then find and coordinate
with that entry.
If Free acquires the mutex first and removes the allocation, and the address
has not been registered for a new allocation, a delayed Get
sees no live tracked allocation and returns INVALID_ARGUMENT without
calling upstream Get or inserting a cache entry.
Why ordinary uncached Free can omit tombstones
Suppose P has never had a cached IPC handle:
Free(P) Get(P)
lock cache mutex
remove P from tracker
cache lookup: no entry
unlock cache mutex
upstream Free(P) begins lock cache mutex
cache lookup: miss
tracker lookup: P is absent
return INVALID_ARGUMENT
There is no initialization to wait for and no upstream IPC handle to Put.
Successful tracker removal is already sufficient to close admission for P.
Creating a second representation of retirement in the cache adds no protection
in this case.
This removes marker allocation, mutex initialization, critnib insertion, and
later marker cleanup from the ordinary uncached path. It also avoids exposing
that path to the allocator failure reported in review. Existing cached entries
still need retirement flags because other callers may already hold references
to those entries.
If upstream Free fails, the existing recovery behavior remains: when tracker
removal succeeded, the provider attempts to restore the allocation to the
tracker. Failure of that restoration is logged.
Supporting changes and tests
The PR also:
Adds explicit cache cleanup to the invalid-parameter and cache-mutex
initialization failure paths, plus cleanup if opened-cache creation fails.
This is necessary because failed initialization does not trigger finalization;
it should not be read as a claim that every possible creation failure path
has been redesigned.
Keeps the flexible IPC payload suitably aligned for upstream provider data.
Exports the waiter-count test helper only in test-enabled shared builds, so
tests-disabled strict LLD builds do not see an undefined version-script
symbol.
Adds deterministic mock-provider tests for single-flight Get, failed Get and
retry, Free overlapping initialization, failed Get during retirement,
allocation-address reuse while the previous Free is blocked, and Get delayed
until after Free. The uncached-Free regression test covers Get rejection both
during and after an uncached Free, followed by successful address reuse.
The tests use mock callback gates and waiter-count observation to arrange the
important interleavings. They compare distinct upstream payloads and UMF
handle IDs, count upstream Gets, and record which handles receive Put.
The uncached-Free test verifies the concurrency behavior; the absence of
base-allocation calls on that path was additionally checked with a debugger
when implementing the uncached-Free simplification, rather than by an
allocation-failure injection facility in that test.
4. What is the difference?
Area
Before the PR
With the fix
Concurrent cache miss
Each caller could independently call upstream Get; insertion only selected a winner afterward.
One caller publishes an IN_PROGRESS entry; others wait and share its result.
Cache-entry lifetime
Get and Free could remove or use entries concurrently without coordinated ownership.
Entry mutexes and retained critnib references protect in-flight users; Free marks entries retiring and waits for initialization.
Cache miss versus Free
A Get could create an entry while Free was removing the allocation and cache state.
Both paths coordinate on ipcCacheMutex; tracker liveness and retirement flags close admission for the old allocation.
Delayed Get after Free
An empty cache did not prove that the allocation was still live, so a delayed call could create a stale entry for the freed address.
Cache misses verify tracked allocation liveness under the same mutex used by Free to remove tracker metadata.
Address reuse
Cleanup by address could confuse an old allocation with a new allocation at the same address.
Retirement and cleanup compare cache-entry identity; allocation setup clears an old retiring entry before the reused address is registered.
Failed initialization
There were no shared in-progress entries: concurrent callers could make independent upstream attempts and observe separate results.
Callers joining one flight observe its published result; failed cleanup removes only the original non-retiring entry, while Free owns retiring-entry removal.
Ordinary uncached Free
Did not allocate an IPC retirement marker, but lacked the new admission protection.
Again avoids marker allocation, now safely relying on successful tracker removal and the mutex-protected liveness check.
Uncached-Free simplification versus initial PR implementation
The initial PR allocated a tombstone even after successful tracker removal for an uncached allocation.
The current implementation removes that unnecessary allocation dependency while preserving handling of existing entries and tracker-removal failure.
Synchronization cost
No per-entry initialization wait or provider-wide cache-admission lock.
More entry metadata and locking, but no duplicate upstream Get for callers sharing an initialization flight; unrelated upstream Gets can overlap.
Evidence
Ordinary tests could miss duplicate Gets and timing-dependent races.
Barriers, mock callbacks, handle IDs, and call-count checks make the important interleavings testable and repeatable.
In short, the original cache was a best-effort address-keyed lookup around
upstream IPC calls. The PR turns it into a synchronized per-allocation
get-or-create lifecycle: a live allocation admits one initialization flight,
Free retires and drains that flight, and a cache miss for an untracked
allocation is rejected instead of reviving stale state. The uncached-Free
simplification removes redundant retirement state from the ordinary uncached
Free path.
Why retain this architecture rather than one large lock?
A single provider-wide mutex held through upstream Get and Free would reduce
the number of synchronization mechanisms, but it would serialize potentially
expensive provider calls for unrelated allocations. The current design uses
the shared lock for admission and cache membership, and entry-specific locking
for initialization.
The latest simplification takes advantage of information already held by the
tracker instead of adding another mechanism. It is a localized improvement,
not a new public API or a change to provider-specific IPC formats.
Scope note
The delayed-Get test invokes the tracking-provider operation with captured
allocation arguments and independently owned IPC storage. This isolates the
cache behavior from metadata lookup performed earlier by the public IPC API.
The change described here protects cache admission and tracking-provider
lifetime; it does not claim that arbitrary application-level use of an
allocation pointer remains valid after the application frees that allocation.
In particular, the liveness check observes the allocation currently registered
at an address. It is not a generation token carried by a caller: an old pointer
used after that address has been reallocated cannot be distinguished from a
pointer to the new allocation by this check alone.
The public umfGetIPCHandle path in src/ipc.c:67-111 also uses allocation
properties outside the cache's entry-reference protocol. This PR does not
pin those properties for the whole public call. Consequently, protecting cache
entries must not be equated with establishing safety for every possible race
between public Get, allocation Free, pool destruction, or subsequent use of an
exported handle.
Source guide
Baseline Get/Free: inspect src/provider/provider_tracking.c at d0e5e35f.
Current entry state: src/provider/provider_tracking.c, ipc_cache_value_t.
Allocation reuse: trackingAlloc.
Cached/uncached retirement and failure recovery: trackingFree.
Cache admission, liveness, shared results, and failed cleanup: trackingGetIpcHandle.
Public caller-buffer ownership: src/ipc.c, umfGetIPCHandle and umfPutIPCHandle.
Export generation: src/CMakeLists.txt and src/libumf.map.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The provider-wide/per-entry locking protocol interacts with deferred critnib reclamation across lifecycle paths, requiring human concurrency review and sanitizer-backed validation that was unavailable here.
Serialize cache admission and per-entry initialization to prevent duplicate
upstream IPC Gets. Coordinate retirement with Free and reject delayed Gets
for allocations that are no longer tracked.
Preserve entry identity across address reuse, clean up failed initialization,
and avoid forwarding thread-local provider errors to waiting callers. Skip
retirement allocation for uncached frees after successful tracker removal.
Keep IPC payloads aligned, limit test-helper exports to test builds, and add
deterministic coverage for concurrent Get, failure, Free, and address reuse.
Temporarily borrow the parallel stress loop from oneapi-src#1633 to validate
the tracking IPC cache concurrency fix under load.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Serialize cache admission and per-entry initialization to prevent duplicate
upstream IPC Gets. Coordinate retirement with Free and reject delayed Gets
for allocations that are no longer tracked.
Preserve entry identity across address reuse, clean up failed initialization,
and avoid forwarding thread-local provider errors to waiting callers. Skip
retirement allocation for uncached frees after successful tracker removal.
Keep IPC payloads aligned, limit test-helper exports to test builds, and add
deterministic coverage for concurrent Get, failure, Free, and address reuse.