From af275ed3419205c716eca0751bd39acad7e7fdce Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Tue, 10 Oct 2023 14:15:42 +0000 Subject: [PATCH] fix: handle failure on set cache region Related-To: NEO-9038 Signed-off-by: Mateusz Jablonski --- .../os_interface/linux/drm_memory_manager.cpp | 11 ++++++---- .../linux/drm_memory_manager_tests.cpp | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index 822bf4b3c6..9943aaf380 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -2442,14 +2442,17 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl pushSharedBufferObject(bo); - DrmAllocation *drmAllocation = nullptr; - drmAllocation = new DrmAllocation(properties.rootDeviceIndex, properties.allocationType, bo, cpuPointer, bo->peekAddress(), bo->peekSize(), MemoryPool::System4KBPages); + auto drmAllocation = std::make_unique(properties.rootDeviceIndex, properties.allocationType, bo, cpuPointer, bo->peekAddress(), bo->peekSize(), MemoryPool::System4KBPages); drmAllocation->setMmapPtr(cpuPointer); drmAllocation->setMmapSize(size); drmAllocation->setReservedAddressRange(reinterpret_cast(cpuPointer), size); - drmAllocation->setCacheRegion(&drm, static_cast(properties.cacheRegion)); + if (!drmAllocation->setCacheRegion(&drm, static_cast(properties.cacheRegion))) { + this->munmapFunction(cpuPointer, size); + delete bo; + return nullptr; + } - return drmAllocation; + return drmAllocation.release(); } auto gmmHelper = getGmmHelper(properties.rootDeviceIndex); diff --git a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index b887dad627..db20327e86 100644 --- a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -1076,6 +1076,28 @@ TEST_F(DrmMemoryManagerTest, GivenDeviceSharedAllocationWhichRequiresHostMapThen memoryManager->freeGraphicsMemory(graphicsAllocation); } +TEST_F(DrmMemoryManagerTest, GivenDeviceSharedAllocationWhenSetCacheRegionFailsThenNullptrIsReturned) { + mock->ioctlExpected.primeFdToHandle = 1; + mock->ioctlExpected.gemWait = 0; + mock->ioctlExpected.gemClose = 0; + mock->ioctlExpected.gemMmapOffset = 1; + + osHandle handle = 1u; + this->mock->outputHandle = 2u; + size_t size = 4096u; + std::vector regionInfo(1); + regionInfo[0].region = {drm_i915_gem_memory_class::I915_MEMORY_CLASS_SYSTEM, 0}; + + this->mock->memoryInfo.reset(new MemoryInfo(regionInfo, *mock)); + this->mock->queryEngineInfo(); + AllocationProperties properties(rootDeviceIndex, false, size, AllocationType::GPU_TIMESTAMP_DEVICE_BUFFER, false, {}); + properties.cacheRegion = std::numeric_limits::max(); + + auto graphicsAllocation = memoryManager->createGraphicsAllocationFromSharedHandle(handle, properties, false, true, false, nullptr); + + EXPECT_EQ(nullptr, graphicsAllocation); +} + TEST_F(DrmMemoryManagerTest, GivenAllocationWhenFreeingThenSucceeds) { mock->ioctlExpected.gemUserptr = 1; mock->ioctlExpected.gemWait = 1;