From 97ea332a9fc8c74b7e8d8a668c807f9e06a26672 Mon Sep 17 00:00:00 2001 From: Kamil Diedrich Date: Wed, 15 Jun 2022 11:07:49 +0000 Subject: [PATCH] Add support for ntHandles in OCL Signed-off-by: Kamil Diedrich --- .../unit_tests/sources/driver/test_driver.cpp | 5 ++-- .../helpers/cl_memory_properties_helpers.cpp | 4 +++ opencl/source/mem_obj/buffer_windows.cpp | 4 +++ .../sharings/unified/unified_sharing.cpp | 2 +- .../mem_obj/linux/buffer_linux_tests.cpp | 10 +++++++ .../mem_obj/windows/buffer_windows_tests.cpp | 28 ++++++++++++++++++- .../test/common/mocks/mock_memory_manager.cpp | 14 ++++++++++ .../test/common/mocks/mock_memory_manager.h | 1 + 8 files changed, 64 insertions(+), 4 deletions(-) diff --git a/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp b/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp index b620fd2084..0ed56b7111 100644 --- a/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp +++ b/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp @@ -223,15 +223,16 @@ HWTEST_F(ImportNTHandle, whenCallingCreateGraphicsAllocationFromMultipleSharedHa EXPECT_EQ(nullptr, ptr); } +constexpr size_t invalidHandle = 0xffffffff; + HWTEST_F(ImportNTHandle, givenNotExistingNTHandleWhenCreatingDeviceMemoryThenErrorIsReturned) { using RENDER_SURFACE_STATE = typename FamilyType::RENDER_SURFACE_STATE; ze_device_mem_alloc_desc_t devProperties = {}; devProperties.stype = ZE_STRUCTURE_TYPE_DEVICE_MEMORY_PROPERTIES; - uint64_t imageHandle = 0x1; ze_external_memory_import_win32_handle_t importNTHandle = {}; - importNTHandle.handle = &imageHandle; + importNTHandle.handle = reinterpret_cast(invalidHandle); importNTHandle.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32; importNTHandle.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_WIN32; devProperties.pNext = &importNTHandle; diff --git a/opencl/source/helpers/cl_memory_properties_helpers.cpp b/opencl/source/helpers/cl_memory_properties_helpers.cpp index dc3b730e94..6ba78ac4e3 100644 --- a/opencl/source/helpers/cl_memory_properties_helpers.cpp +++ b/opencl/source/helpers/cl_memory_properties_helpers.cpp @@ -39,6 +39,10 @@ bool ClMemoryPropertiesHelper::parseMemoryProperties(const cl_mem_properties_int handle = static_cast(properties[i + 1]); handleType = static_cast(UnifiedSharingHandleType::LinuxFd); break; + case CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR: + handle = static_cast(properties[i + 1]); + handleType = static_cast(UnifiedSharingHandleType::Win32Nt); + break; default: return false; } diff --git a/opencl/source/mem_obj/buffer_windows.cpp b/opencl/source/mem_obj/buffer_windows.cpp index 3bbfceed66..f000c5974a 100644 --- a/opencl/source/mem_obj/buffer_windows.cpp +++ b/opencl/source/mem_obj/buffer_windows.cpp @@ -9,6 +9,10 @@ namespace NEO { bool Buffer::validateHandleType(MemoryProperties &memoryProperties, UnifiedSharingMemoryDescription &extMem) { + if (memoryProperties.handleType == static_cast(UnifiedSharingHandleType::Win32Nt)) { + extMem.type = UnifiedSharingHandleType::Win32Nt; + return true; + } return false; } } // namespace NEO diff --git a/opencl/source/sharings/unified/unified_sharing.cpp b/opencl/source/sharings/unified/unified_sharing.cpp index 024e063ffd..bd621c1a58 100644 --- a/opencl/source/sharings/unified/unified_sharing.cpp +++ b/opencl/source/sharings/unified/unified_sharing.cpp @@ -36,7 +36,7 @@ GraphicsAllocation *UnifiedSharing::createGraphicsAllocation(Context *context, U auto memoryManager = context->getMemoryManager(); switch (description.type) { case UnifiedSharingHandleType::Win32Nt: { - return memoryManager->createGraphicsAllocationFromNTHandle(description.handle, context->getDevice(0)->getRootDeviceIndex(), AllocationType::SHARED_IMAGE); + return memoryManager->createGraphicsAllocationFromNTHandle(description.handle, context->getDevice(0)->getRootDeviceIndex(), allocationType); } case UnifiedSharingHandleType::LinuxFd: case UnifiedSharingHandleType::Win32Shared: { diff --git a/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp b/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp index 40092d7160..192b44a39e 100644 --- a/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp +++ b/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp @@ -115,6 +115,16 @@ TEST_F(ValidExportHostPtr, givenInvalidPropertiesWithDmaBufWhenValidateInputAndC clReleaseMemObject(buffer); } +TEST_F(ValidExportHostPtr, givenInvalidPropertiesWithOpaqueWin32WhenValidateInputAndCreateBufferThenNullptrIsReturned) { + cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR, 0x1234, 0}; + cl_mem buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); + + EXPECT_EQ(retVal, CL_INVALID_PROPERTY); + EXPECT_EQ(buffer, nullptr); + + clReleaseMemObject(buffer); +} + TEST_F(ValidExportHostPtr, givenPropertiesWithDmaBufWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR, 0x1234, 0}; diff --git a/opencl/test/unit_test/mem_obj/windows/buffer_windows_tests.cpp b/opencl/test/unit_test/mem_obj/windows/buffer_windows_tests.cpp index 663ada095e..cc09a29994 100644 --- a/opencl/test/unit_test/mem_obj/windows/buffer_windows_tests.cpp +++ b/opencl/test/unit_test/mem_obj/windows/buffer_windows_tests.cpp @@ -102,7 +102,7 @@ struct ValidExportHostPtr Buffer *buffer = nullptr; }; -TEST_F(ValidExportHostPtr, givenPropertiesWithDmaBufWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { +TEST_F(ValidExportHostPtr, givenPropertiesWithDmaBufWhenValidateInputAndCreateBufferThenInvalidPropertyIsSet) { cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR, 0x1234, 0}; auto buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); EXPECT_EQ(retVal, CL_INVALID_PROPERTY); @@ -110,3 +110,29 @@ TEST_F(ValidExportHostPtr, givenPropertiesWithDmaBufWhenValidateInputAndCreateBu clReleaseMemObject(buffer); } + +TEST_F(ValidExportHostPtr, givenInvalidPropertiesWithNtHandleWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { + + osHandle invalidHandle = static_cast(pClExecutionEnvironment->memoryManager.get())->invalidSharedHandle; + cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR, invalidHandle, 0}; + cl_mem buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); + + EXPECT_EQ(retVal, CL_INVALID_MEM_OBJECT); + EXPECT_EQ(static_cast(pClExecutionEnvironment->memoryManager.get())->capturedSharedHandle, properties[1]); + EXPECT_EQ(buffer, nullptr); + + clReleaseMemObject(buffer); +} + +TEST_F(ValidExportHostPtr, givenPropertiesWithNtHandleWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { + + cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR, 0x1234, 0}; + cl_mem buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); + + EXPECT_EQ(retVal, CL_SUCCESS); + EXPECT_EQ(static_cast(pClExecutionEnvironment->memoryManager.get())->capturedSharedHandle, properties[1]); + EXPECT_EQ(static_cast(buffer)->getGraphicsAllocation(0u)->getAllocationType(), NEO::AllocationType::SHARED_BUFFER); + EXPECT_NE(buffer, nullptr); + + clReleaseMemObject(buffer); +} diff --git a/shared/test/common/mocks/mock_memory_manager.cpp b/shared/test/common/mocks/mock_memory_manager.cpp index d9f2fda7e7..325033da80 100644 --- a/shared/test/common/mocks/mock_memory_manager.cpp +++ b/shared/test/common/mocks/mock_memory_manager.cpp @@ -163,6 +163,20 @@ GraphicsAllocation *MockMemoryManager::createGraphicsAllocationFromSharedHandle( } } +GraphicsAllocation *MockMemoryManager::createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, AllocationType allocType) { + if (toOsHandle(handle) != invalidSharedHandle) { + auto graphicsAllocation = createMemoryAllocation(NEO::AllocationType::SHARED_BUFFER, nullptr, reinterpret_cast(1), 1, + 4096u, toOsHandle(handle), MemoryPool::SystemCpuInaccessible, rootDeviceIndex, + false, false, false); + graphicsAllocation->setSharedHandle(toOsHandle(handle)); + this->capturedSharedHandle = toOsHandle(handle); + return graphicsAllocation; + } else { + this->capturedSharedHandle = toOsHandle(handle); + return nullptr; + } +} + bool MockMemoryManager::copyMemoryToAllocationBanks(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy, DeviceBitfield handleMask) { copyMemoryToAllocationBanksCalled++; copyMemoryToAllocationBanksParamsPassed.push_back({graphicsAllocation, destinationOffset, memoryToCopy, sizeToCopy, handleMask}); diff --git a/shared/test/common/mocks/mock_memory_manager.h b/shared/test/common/mocks/mock_memory_manager.h index bcd4dda2f1..134c1726d5 100644 --- a/shared/test/common/mocks/mock_memory_manager.h +++ b/shared/test/common/mocks/mock_memory_manager.h @@ -81,6 +81,7 @@ class MockMemoryManager : public MemoryManagerCreate { GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *ptr) override; GraphicsAllocation *createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) override; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override; + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, AllocationType allocType) override; void *allocateSystemMemory(size_t size, size_t alignment) override;