From f9307bb20ecae22a77f7111c168e479b33aaf5b8 Mon Sep 17 00:00:00 2001 From: Maciej Plewka Date: Wed, 16 Feb 2022 17:36:27 +0000 Subject: [PATCH] Fix don't store internal host mem in hostPtrMap Signed-off-by: Maciej Plewka --- level_zero/core/source/cmdlist/cmdlist.cpp | 4 +- level_zero/core/source/cmdlist/cmdlist_hw.inl | 10 +-- .../sources/cmdlist/test_cmdlist_3.cpp | 71 +++++++++++++++++++ .../common/mocks/mock_graphics_allocation.h | 3 +- 4 files changed, 82 insertions(+), 6 deletions(-) diff --git a/level_zero/core/source/cmdlist/cmdlist.cpp b/level_zero/core/source/cmdlist/cmdlist.cpp index 6b4e5e30a0..cd077dc085 100644 --- a/level_zero/core/source/cmdlist/cmdlist.cpp +++ b/level_zero/core/source/cmdlist/cmdlist.cpp @@ -80,8 +80,10 @@ NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64 UNRECOVERABLE_IF(alloc == nullptr); if (this->cmdListType == CommandListType::TYPE_IMMEDIATE && this->isFlushTaskSubmissionEnabled) { this->csr->getInternalAllocationStorage()->storeAllocation(std::unique_ptr(alloc), NEO::AllocationUsage::TEMPORARY_ALLOCATION); - } else { + } else if (alloc->getAllocationType() == NEO::AllocationType::EXTERNAL_HOST_PTR) { hostPtrMap.insert(std::make_pair(buffer, alloc)); + } else { + commandContainer.getDeallocationContainer().push_back(alloc); } return alloc; } diff --git a/level_zero/core/source/cmdlist/cmdlist_hw.inl b/level_zero/core/source/cmdlist/cmdlist_hw.inl index 100305434d..1f600c0010 100644 --- a/level_zero/core/source/cmdlist/cmdlist_hw.inl +++ b/level_zero/core/source/cmdlist/cmdlist_hw.inl @@ -1757,10 +1757,12 @@ inline AlignedAllocationData CommandListCoreFamily::getAlignedAll } else { alloc = getHostPtrAlloc(buffer, bufferSize, hostCopyAllowed); alignedPtr = static_cast(alignDown(alloc->getGpuAddress(), NEO::EncodeSurfaceState::getSurfaceBaseAddressAlignment())); - auto hostAllocCpuPtr = reinterpret_cast(alloc->getUnderlyingBuffer()); - hostAllocCpuPtr = alignDown(hostAllocCpuPtr, NEO::EncodeSurfaceState::getSurfaceBaseAddressAlignment()); - auto allignedPtrOffset = sourcePtr - hostAllocCpuPtr; - alignedPtr = ptrOffset(alignedPtr, allignedPtrOffset); + if (alloc->getAllocationType() == NEO::AllocationType::EXTERNAL_HOST_PTR) { + auto hostAllocCpuPtr = reinterpret_cast(alloc->getUnderlyingBuffer()); + hostAllocCpuPtr = alignDown(hostAllocCpuPtr, NEO::EncodeSurfaceState::getSurfaceBaseAddressAlignment()); + auto allignedPtrOffset = sourcePtr - hostAllocCpuPtr; + alignedPtr = ptrOffset(alignedPtr, allignedPtrOffset); + } } hostPointerNeedsFlush = true; diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp index 94cd88f96f..847533773d 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp @@ -1287,5 +1287,76 @@ HWTEST2_F(CommandListCreate, givenNonEmptyCommandsToPatchWhenClearCommandsToPatc EXPECT_TRUE(pCommandList->commandsToPatch.empty()); } +template +class MyDeviceMock : public Mock { + public: + NEO::GraphicsAllocation *allocateMemoryFromHostPtr(const void *buffer, size_t size, bool hostCopyAllowed) override { + auto alloc = std::make_unique(const_cast(buffer), reinterpret_cast(buffer), size); + alloc->allocationType = AllocType; + return alloc.release(); + } + const NEO::HardwareInfo &getHwInfo() const override { + return neoDevice->getHardwareInfo(); + } +}; + +HWTEST2_F(CommandListCreate, givenHostPtrAllocAllocWhenInternalMemCreatedThenNewAllocAddedToDealocationContainer, IsAtLeastSkl) { + auto myDevice = std::make_unique>(); + myDevice->neoDevice = device->getNEODevice(); + auto commandList = std::make_unique>>(); + commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u); + auto buffer = std::make_unique(0x100); + + auto deallocationSize = commandList->commandContainer.getDeallocationContainer().size(); + auto alloc = commandList->getHostPtrAlloc(buffer.get(), 0x80, true); + EXPECT_EQ(deallocationSize + 1, commandList->commandContainer.getDeallocationContainer().size()); + EXPECT_NE(alloc, nullptr); + driverHandle.get()->getMemoryManager()->freeGraphicsMemory(alloc); + commandList->commandContainer.getDeallocationContainer().clear(); +} + +HWTEST2_F(CommandListCreate, givenHostPtrAllocAllocWhenExternalMemCreatedThenNewAllocAddedToHostPtrMap, IsAtLeastSkl) { + auto myDevice = std::make_unique>(); + myDevice->neoDevice = device->getNEODevice(); + auto commandList = std::make_unique>>(); + commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u); + auto buffer = std::make_unique(0x100); + + auto hostPtrMapSize = commandList->getHostPtrMap().size(); + auto alloc = commandList->getHostPtrAlloc(buffer.get(), 0x100, true); + EXPECT_EQ(hostPtrMapSize + 1, commandList->getHostPtrMap().size()); + EXPECT_NE(alloc, nullptr); + driverHandle.get()->getMemoryManager()->freeGraphicsMemory(alloc); + commandList->hostPtrMap.clear(); +} + +HWTEST2_F(CommandListCreate, givenGetAlignedAllocationWhenInternalMemWithinDifferentAllocThenReturnNewAlloc, IsAtLeastSkl) { + auto myDevice = std::make_unique>(); + myDevice->neoDevice = device->getNEODevice(); + auto commandList = std::make_unique>>(); + commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u); + auto buffer = std::make_unique(0x100); + + auto outData1 = commandList->getAlignedAllocation(device, buffer.get(), 0x100, true); + auto outData2 = commandList->getAlignedAllocation(device, &buffer.get()[5], 0x1, true); + EXPECT_NE(outData1.alloc, outData2.alloc); + driverHandle.get()->getMemoryManager()->freeGraphicsMemory(outData1.alloc); + driverHandle.get()->getMemoryManager()->freeGraphicsMemory(outData2.alloc); + commandList->commandContainer.getDeallocationContainer().clear(); +} +HWTEST2_F(CommandListCreate, givenGetAlignedAllocationWhenExternalMemWithinDifferentAllocThenReturnPreviouslyAllocatedMem, IsAtLeastSkl) { + auto myDevice = std::make_unique>(); + myDevice->neoDevice = device->getNEODevice(); + auto commandList = std::make_unique>>(); + commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u); + auto buffer = std::make_unique(0x100); + + auto outData1 = commandList->getAlignedAllocation(device, buffer.get(), 0x100, true); + auto outData2 = commandList->getAlignedAllocation(device, &buffer.get()[5], 0x1, true); + EXPECT_EQ(outData1.alloc, outData2.alloc); + driverHandle.get()->getMemoryManager()->freeGraphicsMemory(outData1.alloc); + commandList->hostPtrMap.clear(); +} + } // namespace ult } // namespace L0 diff --git a/shared/test/common/mocks/mock_graphics_allocation.h b/shared/test/common/mocks/mock_graphics_allocation.h index 02e333b767..39eaf52e49 100644 --- a/shared/test/common/mocks/mock_graphics_allocation.h +++ b/shared/test/common/mocks/mock_graphics_allocation.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2021 Intel Corporation + * Copyright (C) 2018-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -18,6 +18,7 @@ constexpr DeviceBitfield mockDeviceBitfield(0b1); class MockGraphicsAllocation : public MemoryAllocation { public: using MemoryAllocation::allocationOffset; + using MemoryAllocation::allocationType; using MemoryAllocation::aubInfo; using MemoryAllocation::gpuAddress; using MemoryAllocation::MemoryAllocation;