Skip to content

Commit ac8dcdb

Browse files
fix: do not unregister shared allocation
Allocations imported from shared handle are not using registerAlloc in drm manager but on free, unregister was called. This could lead to problems with allocation size tracking. This change will skip the unregisterAllocation call if allocation is imported from shared handle. Signed-off-by: Dominik Dabek <[email protected]>
1 parent 14c8f1f commit ac8dcdb

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

shared/source/os_interface/linux/drm_memory_manager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,9 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation,
12291229
return;
12301230
}
12311231
DrmAllocation *drmAlloc = static_cast<DrmAllocation *>(gfxAllocation);
1232-
this->unregisterAllocation(gfxAllocation);
1232+
if (Sharing::nonSharedResource == gfxAllocation->peekSharedHandle()) {
1233+
this->unregisterAllocation(gfxAllocation);
1234+
}
12331235
auto rootDeviceIndex = gfxAllocation->getRootDeviceIndex();
12341236
for (auto &engine : getRegisteredEngines(rootDeviceIndex)) {
12351237
auto memoryOperationsInterface = static_cast<DrmMemoryOperationsHandler *>(executionEnvironment.rootDeviceEnvironments[rootDeviceIndex]->memoryOperationsInterface.get());

shared/source/os_interface/linux/drm_memory_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class DrmMemoryManager : public MemoryManager {
9393
std::vector<GraphicsAllocation *> &getLocalMemAllocs(uint32_t rootDeviceIndex);
9494
AllocationStatus registerSysMemAlloc(GraphicsAllocation *allocation) override;
9595
AllocationStatus registerLocalMemAlloc(GraphicsAllocation *allocation, uint32_t rootDeviceIndex) override;
96-
void unregisterAllocation(GraphicsAllocation *allocation);
96+
MOCKABLE_VIRTUAL void unregisterAllocation(GraphicsAllocation *allocation);
9797

9898
static std::unique_ptr<MemoryManager> create(ExecutionEnvironment &executionEnvironment);
9999

shared/test/common/mocks/linux/mock_drm_memory_manager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,28 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
175175
return MemoryManager::computeStorageInfoMemoryBanks(properties, preferredBank, allBanks);
176176
}
177177

178+
AllocationStatus registerSysMemAlloc(GraphicsAllocation *allocation) override {
179+
++registerSysMemAllocCalled;
180+
return DrmMemoryManager::registerSysMemAlloc(allocation);
181+
}
182+
183+
AllocationStatus registerLocalMemAlloc(GraphicsAllocation *allocation, uint32_t rootDeviceIndex) override {
184+
++registerLocalMemAllocCalled;
185+
return DrmMemoryManager::registerLocalMemAlloc(allocation, rootDeviceIndex);
186+
}
187+
188+
void unregisterAllocation(GraphicsAllocation *allocation) override {
189+
++unregisterAllocationCalled;
190+
DrmMemoryManager::unregisterAllocation(allocation);
191+
}
192+
178193
uint32_t acquireGpuRangeCalledTimes = 0u;
179194
uint32_t acquireGpuRangeWithCustomAlignmenCalledTimes = 0u;
180195
size_t acquireGpuRangeWithCustomAlignmenPassedAlignment = 0u;
181196
size_t computeStorageInfoMemoryBanksCalled = 0u;
197+
size_t registerSysMemAllocCalled = 0u;
198+
size_t registerLocalMemAllocCalled = 0u;
199+
size_t unregisterAllocationCalled = 0u;
182200
ExecutionEnvironment *executionEnvironment = nullptr;
183201

184202
protected:

shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,6 +3055,22 @@ TEST_F(DrmMemoryManagerBasic, givenMemoryManagerWhenCreateAllocationFromHandleIs
30553055
memoryManager->freeGraphicsMemory(allocation);
30563056
}
30573057

3058+
TEST_F(DrmMemoryManagerBasic, givenMemoryManagerWhenCreateAllocationFromHandleIsCalledThenAllocationIsNotRegisteredNorUnregistered) {
3059+
std::unique_ptr<TestedDrmMemoryManager> memoryManager(new (std::nothrow) TestedDrmMemoryManager(false,
3060+
false,
3061+
true,
3062+
executionEnvironment));
3063+
TestedDrmMemoryManager::OsHandleData osHandleData{1u};
3064+
AllocationProperties properties(rootDeviceIndex, false, MemoryConstants::pageSize, AllocationType::sharedBuffer, false, {});
3065+
3066+
auto allocation = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, properties, false, false, true, nullptr);
3067+
EXPECT_NE(nullptr, allocation);
3068+
memoryManager->freeGraphicsMemory(allocation);
3069+
EXPECT_EQ(0u, memoryManager->registerSysMemAllocCalled);
3070+
EXPECT_EQ(0u, memoryManager->registerLocalMemAllocCalled);
3071+
EXPECT_EQ(0u, memoryManager->unregisterAllocationCalled);
3072+
}
3073+
30583074
TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEnabledValidateHostMemoryWhenPinBBAllocationFailsThenUnrecoverableIsCalled) {
30593075
this->mock = static_cast<DrmMockCustom *>(executionEnvironment->rootDeviceEnvironments[0]->osInterface->getDriverModel()->as<Drm>());
30603076
this->mock->reset();

0 commit comments

Comments
 (0)