Ensure shared handle be closed once on Linux

Related-To: NEO-5644

Signed-off-by: Slawomir Milczarek <slawomir.milczarek@intel.com>
This commit is contained in:
Slawomir Milczarek
2021-03-18 15:16:58 +00:00
committed by Compute-Runtime-Automation
parent d60225d995
commit 693f2ff384
8 changed files with 31 additions and 11 deletions

View File

@@ -75,7 +75,7 @@ ze_result_t ImageCoreFamily<gfxCoreFamily>::initialize(Device *device, const ze_
NEO::AllocationProperties properties(device->getRootDeviceIndex(), true, imgInfo, NEO::GraphicsAllocation::AllocationType::SHARED_IMAGE, device->getNEODevice()->getDeviceBitfield());
allocation = device->getNEODevice()->getMemoryManager()->createGraphicsAllocationFromSharedHandle(externalMemoryImportDesc->fd, properties, false);
device->getNEODevice()->getMemoryManager()->closeSharedHandle(externalMemoryImportDesc->fd);
device->getNEODevice()->getMemoryManager()->closeSharedHandle(allocation);
} else {
return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
}

View File

@@ -118,7 +118,7 @@ Image *VASurface::createSharedVaSurface(Context *context, VASharingFunctions *sh
auto alloc = memoryManager->createGraphicsAllocationFromSharedHandle(sharedHandle, properties, false);
memoryManager->closeSharedHandle(sharedHandle);
memoryManager->closeSharedHandle(alloc);
lock.unlock();

View File

@@ -573,6 +573,25 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDrmMemoryManagerCreate
EXPECT_NE(nullptr, drmMemoryManger.peekGemCloseWorker());
}
TEST_F(DrmMemoryManagerTest, GivenAllocationWhenClosingSharedHandleThenSucceeds) {
mock->ioctl_expected.primeFdToHandle = 1;
mock->ioctl_expected.gemWait = 1;
mock->ioctl_expected.gemClose = 1;
osHandle handle = 1u;
this->mock->outputHandle = 2u;
size_t size = 4096u;
AllocationProperties properties(rootDeviceIndex, false, size, GraphicsAllocation::AllocationType::SHARED_BUFFER, false, {});
auto graphicsAllocation = memoryManager->createGraphicsAllocationFromSharedHandle(handle, properties, false);
EXPECT_EQ(handle, graphicsAllocation->peekSharedHandle());
memoryManager->closeSharedHandle(graphicsAllocation);
EXPECT_EQ(Sharing::nonSharedResource, graphicsAllocation->peekSharedHandle());
memoryManager->freeGraphicsMemory(graphicsAllocation);
}
TEST_F(DrmMemoryManagerTest, GivenAllocationWhenFreeingThenSucceeds) {
mock->ioctl_expected.gemUserptr = 1;
mock->ioctl_expected.gemWait = 1;

View File

@@ -183,6 +183,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
void decReuseCount() { sharingInfo.reuseCount--; }
uint32_t peekReuseCount() const { return sharingInfo.reuseCount; }
osHandle peekSharedHandle() const { return sharingInfo.sharedHandle; }
void setSharedHandle(osHandle handle) { sharingInfo.sharedHandle = handle; }
void setAllocationType(AllocationType allocationType);
AllocationType getAllocationType() const { return allocationType; }

View File

@@ -87,7 +87,7 @@ class MemoryManager {
virtual bool verifyHandle(osHandle handle, uint32_t rootDeviceIndex, bool) { return true; }
virtual GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) = 0;
virtual void closeSharedHandle(osHandle handle){};
virtual void closeSharedHandle(GraphicsAllocation *graphicsAllocation){};
virtual GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) = 0;
virtual bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation);

View File

@@ -18,8 +18,6 @@ class MemoryAllocation : public GraphicsAllocation {
size_t sizeToFree = 0;
const bool uncacheable;
void setSharedHandle(osHandle handle) { sharingInfo.sharedHandle = handle; }
MemoryAllocation(uint32_t rootDeviceIndex, AllocationType allocationType, void *cpuPtrIn, uint64_t gpuAddress, uint64_t baseAddress, size_t sizeIn,
MemoryPool::Type pool, size_t maxOsContextCount)
: MemoryAllocation(rootDeviceIndex, 1, allocationType, cpuPtrIn, gpuAddress, baseAddress, sizeIn, pool, maxOsContextCount) {}

View File

@@ -628,8 +628,12 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
return drmAllocation;
}
void DrmMemoryManager::closeSharedHandle(osHandle handle) {
closeFunction(handle);
void DrmMemoryManager::closeSharedHandle(GraphicsAllocation *gfxAllocation) {
DrmAllocation *drmAllocation = static_cast<DrmAllocation *>(gfxAllocation);
if (drmAllocation->peekSharedHandle() != Sharing::nonSharedResource) {
closeFunction(drmAllocation->peekSharedHandle());
drmAllocation->setSharedHandle(Sharing::nonSharedResource);
}
}
GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) {
@@ -707,9 +711,7 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation)
for (auto bo : bos) {
unreference(bo, bo && bo->isReused ? false : true);
}
if (gfxAllocation->peekSharedHandle() != Sharing::nonSharedResource) {
closeFunction(gfxAllocation->peekSharedHandle());
}
closeSharedHandle(gfxAllocation);
}
releaseGpuRange(gfxAllocation->getReservedAddressPtr(), gfxAllocation->getReservedAddressSize(), gfxAllocation->getRootDeviceIndex());

View File

@@ -36,7 +36,7 @@ class DrmMemoryManager : public MemoryManager {
void handleFenceCompletion(GraphicsAllocation *allocation) override;
GraphicsAllocation *createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) override;
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) override;
void closeSharedHandle(osHandle handle) override;
void closeSharedHandle(GraphicsAllocation *gfxAllocation) override;
GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) override;
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; }