From d1ee840ba56543eb376bf4164d637531485afaa8 Mon Sep 17 00:00:00 2001 From: "Spruit, Neil R" Date: Thu, 12 Jan 2023 20:08:12 +0000 Subject: [PATCH] Enforce 64KB alignment when mmaping GPU_TIMESTAMP_DEVICE_BUFFER Related-To: LOCI-3866 Signed-off-by: Spruit, Neil R --- .../os_interface/linux/drm_memory_manager.cpp | 13 +++++++++- .../linux/drm_memory_manager_tests.cpp | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index da401f5dd8..8c1a3bbdf5 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -2019,7 +2019,18 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl size_t size = lseekFunction(handle, 0, SEEK_END); bo = new BufferObject(&drm, patIndex, boHandle, size, maxOsContextCount); - cpuPointer = this->mmapFunction(0, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + + if (properties.allocationType == AllocationType::GPU_TIMESTAMP_DEVICE_BUFFER) { + cpuPointer = this->mmapFunction(0, size + MemoryConstants::pageSize64k, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + auto alignedAddr = alignUp(cpuPointer, MemoryConstants::pageSize64k); + auto notUsedSize = ptrDiff(alignedAddr, cpuPointer); + // call unmap to free the unaligned pages preceding the system allocation and + // adjust the pointer to the correct aligned Address. + munmapFunction(cpuPointer, notUsedSize); + cpuPointer = alignedAddr; + } else { + cpuPointer = this->mmapFunction(0, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + } if (cpuPointer == MAP_FAILED) { PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "mmap return of MAP_FAILED\n"); 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 5731e97f3d..2a295b2103 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 @@ -823,6 +823,32 @@ TEST_F(DrmMemoryManagerTest, GivenAllocationWhenClosingSharedHandleThenSucceeds) memoryManager->freeGraphicsMemory(graphicsAllocation); } +TEST_F(DrmMemoryManagerTest, GivenDeviceSharedAllocationWhichRequiresHostMapThenCorrectAlignmentReturned) { + mock->ioctl_expected.primeFdToHandle = 1; + mock->ioctl_expected.gemWait = 1; + mock->ioctl_expected.gemClose = 1; + mock->ioctl_expected.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, {}); + + auto graphicsAllocation = memoryManager->createGraphicsAllocationFromSharedHandle(handle, properties, false, true, false); + DrmAllocation *drmAlloc = (DrmAllocation *)graphicsAllocation; + + EXPECT_TRUE(isAligned(drmAlloc->getMmapPtr())); + + memoryManager->closeSharedHandle(graphicsAllocation); + + memoryManager->freeGraphicsMemory(graphicsAllocation); +} + TEST_F(DrmMemoryManagerTest, GivenAllocationWhenFreeingThenSucceeds) { mock->ioctl_expected.gemUserptr = 1; mock->ioctl_expected.gemWait = 1;