diff --git a/opencl/source/sharings/va/va_surface.cpp b/opencl/source/sharings/va/va_surface.cpp index d1816851b8..ec860e57db 100644 --- a/opencl/source/sharings/va/va_surface.cpp +++ b/opencl/source/sharings/va/va_surface.cpp @@ -113,6 +113,8 @@ Image *VASurface::createSharedVaSurface(Context *context, VASharingFunctions *sh auto alloc = memoryManager->createGraphicsAllocationFromSharedHandle(sharedHandle, properties, false); + memoryManager->closeSharedHandle(sharedHandle); + imgDesc.image_row_pitch = imgInfo.rowPitch; imgDesc.image_slice_pitch = 0u; imgInfo.slicePitch = 0u; diff --git a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index ed49624d44..681312acd9 100644 --- a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -32,6 +32,7 @@ #include "opencl/source/mem_obj/buffer.h" #include "opencl/source/mem_obj/image.h" #include "opencl/source/os_interface/linux/drm_command_stream.h" +#include "opencl/source/sharings/va/va_surface.h" #include "opencl/test/unit_test/helpers/unit_test_helper.h" #include "opencl/test/unit_test/mocks/linux/mock_drm_allocation.h" #include "opencl/test/unit_test/mocks/mock_allocation_properties.h" @@ -40,6 +41,7 @@ #include "opencl/test/unit_test/mocks/mock_gmm.h" #include "opencl/test/unit_test/mocks/mock_platform.h" #include "opencl/test/unit_test/os_interface/linux/drm_mock.h" +#include "opencl/test/unit_test/sharings/va/mock_va_sharing.h" #include "test.h" #include "drm/i915_drm.h" @@ -2201,8 +2203,6 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleAndBi auto drmAllocation = static_cast(graphicsAllocation); EXPECT_TRUE(graphicsAllocation->is32BitAllocation()); EXPECT_EQ(1, lseekCalledCount); - EXPECT_EQ(1, closeCalledCount); - EXPECT_EQ(this->mock->inputFd, closeInputFd); EXPECT_EQ(GmmHelper::canonize(memoryManager->getExternalHeapBaseAddress(graphicsAllocation->getRootDeviceIndex(), drmAllocation->isAllocatedInLocalMemoryPool())), drmAllocation->getGpuBaseAddress()); memoryManager->freeGraphicsMemory(graphicsAllocation); } @@ -2221,8 +2221,6 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleIsCre EXPECT_FALSE(graphicsAllocation->is32BitAllocation()); EXPECT_EQ(1, lseekCalledCount); - EXPECT_EQ(1, closeCalledCount); - EXPECT_EQ(this->mock->inputFd, closeInputFd); EXPECT_EQ(0llu, drmAllocation->getGpuBaseAddress()); @@ -2244,8 +2242,6 @@ TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWhenBufferFromSharedHandl EXPECT_EQ(0llu, drmAllocation->getGpuBaseAddress()); EXPECT_EQ(1, lseekCalledCount); - EXPECT_EQ(1, closeCalledCount); - EXPECT_EQ(this->mock->inputFd, closeInputFd); memoryManager->freeGraphicsMemory(graphicsAllocation); } @@ -2262,8 +2258,6 @@ TEST_F(DrmMemoryManagerTest, givenNon32BitAddressingWhenBufferFromSharedHandleIs auto drmAllocation = static_cast(graphicsAllocation); EXPECT_FALSE(graphicsAllocation->is32BitAllocation()); EXPECT_EQ(1, lseekCalledCount); - EXPECT_EQ(1, closeCalledCount); - EXPECT_EQ(this->mock->inputFd, closeInputFd); EXPECT_EQ(0llu, drmAllocation->getGpuBaseAddress()); memoryManager->freeGraphicsMemory(graphicsAllocation); } @@ -2607,8 +2601,6 @@ TEST_F(DrmMemoryManagerTest, givenSharedAllocationWithSmallerThenRealSizeWhenCre EXPECT_EQ(1u, bo->getRefCount()); EXPECT_EQ(realSize, bo->peekSize()); EXPECT_EQ(1, lseekCalledCount); - EXPECT_EQ(1, closeCalledCount); - EXPECT_EQ(this->mock->inputFd, closeInputFd); memoryManager->freeGraphicsMemory(graphicsAllocation); } @@ -4155,4 +4147,24 @@ TEST(DrmAllocationTest, givenResourceRegistrationEnabledWhenIsaIsRegisteredThenC allocation.freeRegisteredBOBindExtHandles(&drm); EXPECT_EQ(2u, drm.unregisterCalledCount); } + +TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenSharedVaSurfaceIsImportedWithDrmPrimeFdToHandleThenDrmPrimeFdCanBeClosed) { + mock->ioctl_expected.total = -1; + + MockContext context(device); + MockVaSharing vaSharing; + VASurfaceID vaSurfaceId = 0u; + + vaSharing.updateAcquiredHandle(1); + std::unique_ptr sharedImage1(VASurface::createSharedVaSurface(&context, &vaSharing.sharingFunctions, + CL_MEM_READ_WRITE, 0, &vaSurfaceId, 0, nullptr)); + EXPECT_EQ(1, closeCalledCount); + EXPECT_EQ(1, closeInputFd); + + vaSharing.updateAcquiredHandle(2); + std::unique_ptr sharedImage2(VASurface::createSharedVaSurface(&context, &vaSharing.sharingFunctions, + CL_MEM_READ_WRITE, 0, &vaSurfaceId, 0, nullptr)); + EXPECT_EQ(2, closeCalledCount); + EXPECT_EQ(2, closeInputFd); +} } // namespace NEO diff --git a/shared/source/memory_manager/memory_manager.h b/shared/source/memory_manager/memory_manager.h index c183ba4333..4696ccc221 100644 --- a/shared/source/memory_manager/memory_manager.h +++ b/shared/source/memory_manager/memory_manager.h @@ -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 GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) = 0; virtual bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation); diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index 097161f353..f42e3e06cb 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -586,8 +586,6 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o pushSharedBufferObject(bo); } - closeFunction(handle); - lock.unlock(); auto drmAllocation = new DrmAllocation(properties.rootDeviceIndex, properties.allocationType, bo, reinterpret_cast(bo->gpuAddress), bo->size, @@ -615,6 +613,10 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o return drmAllocation; } +void DrmMemoryManager::closeSharedHandle(osHandle handle) { + closeFunction(handle); +} + GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) { uint64_t gpuRange = 0llu; diff --git a/shared/source/os_interface/linux/drm_memory_manager.h b/shared/source/os_interface/linux/drm_memory_manager.h index a31cd569fe..d860b9c609 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.h +++ b/shared/source/os_interface/linux/drm_memory_manager.h @@ -36,6 +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; GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) override; GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; }