Dont close shared handle on imported allocations

Related-To: LOCI-2272

Signed-off-by: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
This commit is contained in:
Jaime Arteaga 2022-03-02 03:43:59 +00:00 committed by Compute-Runtime-Automation
parent 7ec8d8ef91
commit a010fb3634
19 changed files with 94 additions and 4 deletions

View File

@ -439,6 +439,7 @@ void *DriverHandleImp::importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_
allocData.size = alloc->getUnderlyingBufferSize();
allocData.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY;
allocData.device = neoDevice;
allocData.isImportedAllocation = true;
if (flags & ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_UNCACHED) {
allocData.allocationFlagsProperty.flags.locallyUncachedResource = 1;
}

View File

@ -335,6 +335,7 @@ class MemoryManagerIpcMock : public NEO::MemoryManager {
AllocationStatus populateOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override { return AllocationStatus::Success; };
void cleanOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{};
void freeGraphicsMemoryImpl(NEO::GraphicsAllocation *gfxAllocation) override{};
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override{};
uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) override { return 0; };
uint64_t getLocalMemorySize(uint32_t rootDeviceIndex, uint32_t deviceBitfield) override { return 0; };
double getPercentOfGlobalMemoryAvailable(uint32_t rootDeviceIndex) override { return 0; }

View File

@ -51,6 +51,7 @@ class MemoryManagerEventPoolFailMock : public NEO::MemoryManager {
AllocationStatus populateOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override { return AllocationStatus::Success; };
void cleanOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{};
void freeGraphicsMemoryImpl(NEO::GraphicsAllocation *gfxAllocation) override{};
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override{};
uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) override { return 0; };
uint64_t getLocalMemorySize(uint32_t rootDeviceIndex, uint32_t deviceBitfield) override { return 0; };
double getPercentOfGlobalMemoryAvailable(uint32_t rootDeviceIndex) override { return 0; }

View File

@ -4891,6 +4891,36 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenCopyMemoryToAllocationThen
memoryManager->freeGraphicsMemory(allocation);
}
TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenFreeingImportedMemoryThenCloseSharedHandleIsNotCalled) {
mock->ioctl_expected.gemUserptr = 1;
mock->ioctl_expected.gemWait = 1;
mock->ioctl_expected.gemClose = 1;
std::vector<uint8_t> dataToCopy(MemoryConstants::pageSize, 1u);
auto allocation = memoryManager->allocateGraphicsMemoryWithProperties({rootDeviceIndex, dataToCopy.size(), AllocationType::BUFFER, device->getDeviceBitfield()});
ASSERT_NE(nullptr, allocation);
memoryManager->freeGraphicsMemory(allocation, true);
EXPECT_EQ(memoryManager->callsToCloseSharedHandle, 0u);
}
TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenFreeingNonImportedMemoryThenCloseSharedHandleIsCalled) {
mock->ioctl_expected.gemUserptr = 1;
mock->ioctl_expected.gemWait = 1;
mock->ioctl_expected.gemClose = 1;
std::vector<uint8_t> dataToCopy(MemoryConstants::pageSize, 1u);
auto allocation = memoryManager->allocateGraphicsMemoryWithProperties({rootDeviceIndex, dataToCopy.size(), AllocationType::BUFFER, device->getDeviceBitfield()});
ASSERT_NE(nullptr, allocation);
memoryManager->freeGraphicsMemory(allocation);
EXPECT_EQ(memoryManager->callsToCloseSharedHandle, 1u);
}
TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenGetLocalMemoryIsCalledThenSizeOfLocalMemoryIsReturned) {
EXPECT_EQ(0 * GB, memoryManager->getLocalMemorySize(rootDeviceIndex, 0xF));
}

View File

@ -383,6 +383,22 @@ TEST_F(WddmMemoryManagerSimpleTest, givenAllocateGraphicsMemoryForNonSvmHostPtrI
memoryManager->freeGraphicsMemory(allocation);
}
TEST_F(WddmMemoryManagerSimpleTest, givenAllocateGraphicsMemoryForNonSvmHostPtrIsCalledWhenNotAlignedPtrIsPassedAndImportedAllocationIsFalseThenAlignedGraphicsAllocationIsFreed) {
memoryManager.reset(new MockWddmMemoryManager(false, false, *executionEnvironment));
auto size = 13u;
auto hostPtr = reinterpret_cast<const void *>(0x10001);
AllocationData allocationData;
allocationData.size = size;
allocationData.hostPtr = hostPtr;
auto allocation = memoryManager->allocateGraphicsMemoryForNonSvmHostPtr(allocationData);
EXPECT_NE(nullptr, allocation);
EXPECT_EQ(hostPtr, allocation->getUnderlyingBuffer());
EXPECT_EQ(size, allocation->getUnderlyingBufferSize());
EXPECT_EQ(1u, allocation->getAllocationOffset());
memoryManager->freeGraphicsMemoryImpl(allocation, false);
}
TEST_F(WddmMemoryManagerTest, givenAllocateGraphicsMemoryForNonSvmHostPtrIsCalledWhencreateWddmAllocationFailsThenGraphicsAllocationIsNotCreated) {
char hostPtr[64];
memoryManager->setDeferredDeleter(nullptr);

View File

@ -195,6 +195,10 @@ void MemoryManager::freeSystemMemory(void *ptr) {
}
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
return freeGraphicsMemory(gfxAllocation, false);
}
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) {
if (!gfxAllocation) {
return;
}
@ -213,7 +217,7 @@ void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
}
getLocalMemoryUsageBankSelector(gfxAllocation->getAllocationType(), gfxAllocation->getRootDeviceIndex())->freeOnBanks(gfxAllocation->storageInfo.getMemoryBanks(), gfxAllocation->getUnderlyingBufferSize());
freeGraphicsMemoryImpl(gfxAllocation);
freeGraphicsMemoryImpl(gfxAllocation, isImportedAllocation);
}
//if not in use destroy in place
//if in use pass to temporary allocation list that is cleaned on blocking calls

View File

@ -128,7 +128,9 @@ class MemoryManager {
void freeSystemMemory(void *ptr);
virtual void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) = 0;
virtual void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) = 0;
MOCKABLE_VIRTUAL void freeGraphicsMemory(GraphicsAllocation *gfxAllocation);
MOCKABLE_VIRTUAL void freeGraphicsMemory(GraphicsAllocation *gfxAllocation, bool isImportedAllocation);
virtual void handleFenceCompletion(GraphicsAllocation *allocation){};
void checkGpuUsageAndDestroyGraphicsAllocations(GraphicsAllocation *gfxAllocation);

View File

@ -260,6 +260,10 @@ void OsAgnosticMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocat
}
}
void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) {
return freeGraphicsMemoryImpl(gfxAllocation);
}
void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
for (auto handleId = 0u; handleId < gfxAllocation->getNumGmms(); handleId++) {
delete gfxAllocation->getGmm(handleId);

View File

@ -75,6 +75,7 @@ class OsAgnosticMemoryManager : public MemoryManager {
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override;
AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override;
void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override;

View File

@ -477,12 +477,13 @@ void SVMAllocsManager::freeZeroCopySvmAllocation(SvmAllocationData *svmData) {
void SVMAllocsManager::freeSvmAllocationWithDeviceStorage(SvmAllocationData *svmData) {
auto graphicsAllocations = svmData->gpuAllocations.getGraphicsAllocations();
GraphicsAllocation *cpuAllocation = svmData->cpuAllocation;
bool isImportedAllocation = svmData->isImportedAllocation;
SVMAllocs.remove(*svmData);
for (auto gpuAllocation : graphicsAllocations) {
memoryManager->freeGraphicsMemory(gpuAllocation);
memoryManager->freeGraphicsMemory(gpuAllocation, isImportedAllocation);
}
memoryManager->freeGraphicsMemory(cpuAllocation);
memoryManager->freeGraphicsMemory(cpuAllocation, isImportedAllocation);
}
bool SVMAllocsManager::hasHostAllocations() {

View File

@ -36,6 +36,7 @@ struct SvmAllocationData {
this->memoryType = svmAllocData.memoryType;
this->allocId = svmAllocData.allocId;
this->pageSizeForAlignment = svmAllocData.pageSizeForAlignment;
this->isImportedAllocation = svmAllocData.isImportedAllocation;
for (auto allocation : svmAllocData.gpuAllocations.getGraphicsAllocations()) {
if (allocation) {
this->gpuAllocations.addAllocation(allocation);
@ -50,6 +51,7 @@ struct SvmAllocationData {
InternalMemoryType memoryType = InternalMemoryType::SVM;
MemoryProperties allocationFlagsProperty;
Device *device = nullptr;
bool isImportedAllocation = false;
void setAllocId(uint32_t id) {
allocId = id;
}

View File

@ -774,6 +774,10 @@ void DrmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gf
}
void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
return freeGraphicsMemoryImpl(gfxAllocation, false);
}
void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImported) {
if (DebugManager.flags.DoNotFreeResources.get()) {
return;
}
@ -800,7 +804,9 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation)
for (auto bo : bos) {
unreference(bo, bo && bo->peekIsReusableAllocation() ? false : true);
}
closeSharedHandle(gfxAllocation);
if (isImported == false) {
closeSharedHandle(gfxAllocation);
}
}
releaseGpuRange(gfxAllocation->getReservedAddressPtr(), gfxAllocation->getReservedAddressSize(), gfxAllocation->getRootDeviceIndex());

View File

@ -33,6 +33,7 @@ class DrmMemoryManager : public MemoryManager {
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override;
void handleFenceCompletion(GraphicsAllocation *allocation) override;
GraphicsAllocation *createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) override;
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override;

View File

@ -508,6 +508,10 @@ void WddmMemoryManager::freeAssociatedResourceImpl(GraphicsAllocation &graphicsA
}
}
void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) {
return freeGraphicsMemoryImpl(gfxAllocation);
}
void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
WddmAllocation *input = static_cast<WddmAllocation *>(gfxAllocation);
DEBUG_BREAK_IF(!validateAllocation(input));

View File

@ -41,6 +41,7 @@ class WddmMemoryManager : public MemoryManager {
WddmMemoryManager &operator=(const WddmMemoryManager &) = delete;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override;
void handleFenceCompletion(GraphicsAllocation *allocation) override;
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override;

View File

@ -78,6 +78,13 @@ DrmAllocation *TestedDrmMemoryManager::allocate32BitGraphicsMemory(uint32_t root
bool useLocalMemory = !allocationData.flags.useSystemMemory && this->localMemorySupported[rootDeviceIndex];
return allocate32BitGraphicsMemoryImpl(allocationData, useLocalMemory);
}
void TestedDrmMemoryManager::closeSharedHandle(GraphicsAllocation *gfxAllocation) {
std::unique_lock<std::mutex> lock(callsToCloseSharedHandleMtx);
DrmMemoryManager::closeSharedHandle(gfxAllocation);
callsToCloseSharedHandle++;
}
TestedDrmMemoryManager::~TestedDrmMemoryManager() {
DrmMemoryManager::commonCleanup();
}

View File

@ -149,12 +149,15 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
alignedFreeWrapperCalled++;
DrmMemoryManager::alignedFreeWrapper(ptr);
}
void closeSharedHandle(GraphicsAllocation *gfxAllocation) override;
uint32_t alignedFreeWrapperCalled = 0u;
uint32_t callsToCloseSharedHandle = 0;
protected:
std::mutex unreferenceMtx;
std::mutex releaseGpuRangeMtx;
std::mutex alignedFreeWrapperMtx;
std::mutex callsToCloseSharedHandleMtx;
};
struct MockDrmGemCloseWorker : DrmGemCloseWorker {

View File

@ -73,6 +73,10 @@ class MockWddmMemoryManager : public MemoryManagerCreate<WddmMemoryManager> {
BaseClass::freeGraphicsMemoryImpl(gfxAllocation);
}
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override {
BaseClass::freeGraphicsMemoryImpl(gfxAllocation, isImportedAllocation);
}
GraphicsAllocation *allocateHugeGraphicsMemory(const AllocationData &allocationData, bool sharedVirtualAddress) override {
allocateHugeGraphicsMemoryCalled = true;
return BaseClass::allocateHugeGraphicsMemory(allocationData, sharedVirtualAddress);

View File

@ -255,6 +255,7 @@ TEST_F(DeviceGetCapsTest, givenFlagEnabled64kbPagesWhenCallConstructorMemoryMana
AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override { return AllocationStatus::Success; };
void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{};
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override{};
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation, bool isImportedAllocation) override{};
uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) override {
return 0;
};