Skip to content

[SYCL][L0] Fix memory leak when tracking indirect accesses #7105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5253,27 +5253,6 @@ pi_result piKernelRetain(pi_kernel Kernel) {
pi_result piKernelRelease(pi_kernel Kernel) {
PI_ASSERT(Kernel, PI_ERROR_INVALID_KERNEL);

if (IndirectAccessTrackingEnabled) {
// piKernelRelease is called by CleanupCompletedEvent(Event) as soon as
// kernel execution has finished. This is the place where we need to release
// memory allocations. If kernel is not in use (not submitted by some
// other thread) then release referenced memory allocations. As a result,
// memory can be deallocated and context can be removed from container in
// the platform. That's why we need to lock a mutex here.
pi_platform Plt = Kernel->Program->Context->getPlatform();
std::scoped_lock<pi_shared_mutex> ContextsLock(Plt->ContextsMutex);

if (--Kernel->SubmissionsCount == 0) {
// Kernel is not submitted for execution, release referenced memory
// allocations.
for (auto &MemAlloc : Kernel->MemAllocs) {
USMFreeHelper(MemAlloc->second.Context, MemAlloc->first,
MemAlloc->second.OwnZeMemHandle);
}
Kernel->MemAllocs.clear();
}
}

if (!Kernel->RefCount.decrementAndTest())
return PI_SUCCESS;

Expand Down Expand Up @@ -5821,9 +5800,35 @@ static pi_result CleanupCompletedEvent(pi_event Event, bool QueueLocked) {
Event->CleanedUp = true;
}

auto ReleaseIndirectMem = [](pi_kernel Kernel) {
if (IndirectAccessTrackingEnabled) {
// piKernelRelease is called by CleanupCompletedEvent(Event) as soon as
// kernel execution has finished. This is the place where we need to
// release memory allocations. If kernel is not in use (not submitted by
// some other thread) then release referenced memory allocations. As a
// result, memory can be deallocated and context can be removed from
// container in the platform. That's why we need to lock a mutex here.
pi_platform Plt = Kernel->Program->Context->getPlatform();
std::scoped_lock<pi_shared_mutex> ContextsLock(Plt->ContextsMutex);

if (--Kernel->SubmissionsCount == 0) {
// Kernel is not submitted for execution, release referenced memory
// allocations.
for (auto &MemAlloc : Kernel->MemAllocs) {
// std::pair<void *const, MemAllocRecord> *, Hash
USMFreeHelper(MemAlloc->second.Context, MemAlloc->first,
MemAlloc->second.OwnZeMemHandle);
}
Kernel->MemAllocs.clear();
}
}
};

// We've reset event data members above, now cleanup resources.
if (AssociatedKernel)
if (AssociatedKernel) {
ReleaseIndirectMem(AssociatedKernel);
PI_CALL(piKernelRelease(AssociatedKernel));
}

if (AssociatedQueue) {
{
Expand Down Expand Up @@ -5877,8 +5882,10 @@ static pi_result CleanupCompletedEvent(pi_event Event, bool QueueLocked) {
}
}
}
if (DepEventKernel)
PI_CALL(piKernelRelease(pi_cast<pi_kernel>(DepEvent->CommandData)));
if (DepEventKernel) {
ReleaseIndirectMem(DepEventKernel);
PI_CALL(piKernelRelease(DepEventKernel));
}
PI_CALL(piEventReleaseInternal(DepEvent));
}

Expand Down