From 0855219087ef9590ba8f61d9a622f7ba5da2f9db Mon Sep 17 00:00:00 2001 From: Compute-Runtime-Validation Date: Fri, 15 Aug 2025 01:46:52 +0200 Subject: [PATCH] Revert "feature: Support for opaque IPC handles on Windows and Linux" This reverts commit 46b1b2783bdcf40de978d1a0b213ce0c5d222db4. Signed-off-by: Compute-Runtime-Validation --- level_zero/core/source/context/context.h | 46 +- .../core/source/context/context_imp.cpp | 53 +-- level_zero/core/source/context/context_imp.h | 20 +- .../context_imp_drm/context_imp_drm.cpp | 51 +- .../context_imp_drm_or_wddm.cpp | 81 +--- .../context_imp_wddm/context_imp_wddm.cpp | 23 +- .../core/source/driver/driver_handle_imp.cpp | 3 +- .../core/source/driver/driver_handle_imp.h | 40 +- .../test/unit_tests/fixtures/device_fixture.h | 2 +- .../fixtures/memory_ipc_fixture.cpp | 6 +- .../unit_tests/fixtures/memory_ipc_fixture.h | 4 +- .../core/test/unit_tests/mocks/mock_context.h | 8 +- .../sources/context/test_context.cpp | 263 ----------- .../sources/context/test_context_drm.cpp | 17 +- .../context/test_context_drm_or_wddm.cpp | 437 +----------------- .../sources/context/test_context_wddm.cpp | 8 +- .../unit_tests/sources/driver/test_driver.cpp | 4 +- .../unit_tests/sources/memory/test_memory.cpp | 2 +- .../sources/memory/test_memory_pooling.cpp | 1 - .../debug_settings/debug_variables_base.inl | 1 - .../memory_manager/allocation_properties.h | 6 +- .../source/memory_manager/memory_manager.cpp | 1 - shared/source/memory_manager/memory_manager.h | 1 - .../memory_manager/unified_memory_manager.cpp | 1 - .../memory_properties_flags_common.inl | 1 - shared/source/os_interface/linux/sys_calls.h | 3 - .../os_interface/linux/sys_calls_linux.cpp | 4 - .../source/os_interface/windows/sys_calls.cpp | 8 - .../source/os_interface/windows/sys_calls.h | 2 - .../windows/wddm/helper_drm_or_wddm.cpp | 4 - .../os_interface/windows/wddm/helper_wddm.cpp | 38 -- .../source/os_interface/windows/wddm/wddm.cpp | 11 +- .../source/os_interface/windows/wddm/wddm.h | 2 - .../os_interface/windows/wddm_allocation.h | 4 - .../windows/wddm_memory_manager.cpp | 10 +- shared/test/common/mocks/mock_wddm.cpp | 24 - shared/test/common/mocks/mock_wddm.h | 1 - .../linux/sys_calls_linux_ult.cpp | 11 - .../os_interface/linux/sys_calls_linux_ult.h | 2 - .../os_interface/windows/mock_sys_calls.h | 2 - .../common/os_interface/windows/sys_calls.cpp | 20 - shared/test/common/test_files/igdrcl.config | 1 - .../windows/wddm_shared_allocations_test.cpp | 39 +- .../os_interface/windows/wddm_tests.cpp | 161 ------- .../windows/wddm_windows_tests.cpp | 99 ---- 45 files changed, 125 insertions(+), 1401 deletions(-) diff --git a/level_zero/core/source/context/context.h b/level_zero/core/source/context/context.h index feaff4085d..68cb56433d 100644 --- a/level_zero/core/source/context/context.h +++ b/level_zero/core/source/context/context.h @@ -8,7 +8,6 @@ #pragma once #include "shared/source/memory_manager/allocation_type.h" -#include "shared/source/memory_manager/graphics_allocation.h" #include "shared/source/unified_memory/unified_memory.h" #include "level_zero/core/source/helpers/api_handle_helper.h" @@ -31,45 +30,6 @@ struct Image; class ContextExt; -#pragma pack(1) -struct IpcMemoryData { - uint64_t handle = 0; - uint64_t poolOffset = 0; - uint8_t type = 0; -}; -#pragma pack() -static_assert(sizeof(IpcMemoryData) <= ZE_MAX_IPC_HANDLE_SIZE, "IpcMemoryData is bigger than ZE_MAX_IPC_HANDLE_SIZE"); - -enum class IpcHandleType : uint8_t { - fdHandle = 0, - ntHandle = 1, - maxHandle -}; - -#pragma pack(1) -struct IpcOpaqueMemoryData { - union IpcHandle { - int fd; - uint64_t reserved; - }; - IpcHandle handle = {}; - uint64_t poolOffset = 0; - unsigned int processId = 0; - IpcHandleType type = IpcHandleType::maxHandle; - uint8_t memoryType = 0; -}; -#pragma pack() -static_assert(sizeof(IpcOpaqueMemoryData) <= ZE_MAX_IPC_HANDLE_SIZE, "IpcOpaqueMemoryData is bigger than ZE_MAX_IPC_HANDLE_SIZE"); - -struct IpcHandleTracking { - uint64_t refcnt = 0; - NEO::GraphicsAllocation *alloc = nullptr; - uint32_t handleId = 0; - uint64_t handle = 0; - uint64_t ptr = 0; - struct IpcMemoryData ipcData = {}; -}; - struct Context : _ze_context_handle_t { inline static ze_memory_type_t parseUSMType(InternalMemoryType memoryType) { switch (memoryType) { @@ -210,10 +170,8 @@ struct Context : _ze_context_handle_t { ze_ipc_mem_handle_t *pIpcHandle) = 0; virtual ze_result_t putVirtualAddressSpaceIpcHandle(ze_ipc_mem_handle_t ipcHandle) = 0; virtual ze_result_t lockMemory(ze_device_handle_t hDevice, void *ptr, size_t size) = 0; - virtual bool isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) = 0; - virtual void *getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, unsigned int processId, ze_ipc_memory_flags_t flags) = 0; - virtual void getDataFromIpcHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset) = 0; - virtual bool isOpaqueHandleSupported(IpcHandleType *handleType) = 0; + virtual bool isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice) = 0; + virtual void *getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, ze_ipc_memory_flags_t flags) = 0; virtual ze_result_t getPitchFor2dImage( ze_device_handle_t hDevice, diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index 8798415475..89cc58aed2 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -9,17 +9,14 @@ #include "shared/source/command_container/implicit_scaling.h" #include "shared/source/command_stream/command_stream_receiver.h" -#include "shared/source/execution_environment/execution_environment.h" #include "shared/source/execution_environment/root_device_environment.h" #include "shared/source/helpers/aligned_memory.h" -#include "shared/source/helpers/driver_model_type.h" #include "shared/source/helpers/gfx_core_helper.h" #include "shared/source/helpers/ptr_math.h" #include "shared/source/memory_manager/allocation_properties.h" #include "shared/source/memory_manager/memory_manager.h" #include "shared/source/memory_manager/memory_operations_handler.h" #include "shared/source/memory_manager/unified_memory_manager.h" -#include "shared/source/os_interface/os_interface.h" #include "shared/source/utilities/cpu_info.h" #include "level_zero/core/source/cmdlist/cmdlist.h" @@ -78,15 +75,15 @@ ContextImp::ContextImp(DriverHandle *driverHandle) { } contextSettings.enableSvmHeapReservation = platformSupportSvmHeapReservation; - bool useOpaqueHandle = true; + bool pidfdOrSocket = true; for (auto &device : this->driverHandle->devices) { auto &productHelper = device->getNEODevice()->getProductHelper(); if (!productHelper.isPidFdOrSocketForIpcSupported()) { - useOpaqueHandle = false; + pidfdOrSocket = false; break; } } - contextSettings.enablePidfdOrSockets = useOpaqueHandle; + contextSettings.enablePidfdOrSockets = pidfdOrSocket; } ContextImp::~ContextImp() { @@ -138,7 +135,6 @@ ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc, *ptr = getMemHandlePtr(this->devices.begin()->second, lookupTable.sharedHandleType.fd, NEO::AllocationType::bufferHostMemory, - 0u, flags); if (nullptr == *ptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; @@ -147,7 +143,7 @@ ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc, UNRECOVERABLE_IF(!lookupTable.sharedHandleType.isNTHandle); *ptr = this->driverHandle->importNTHandle(this->devices.begin()->second, lookupTable.sharedHandleType.ntHandle, - NEO::AllocationType::bufferHostMemory, 0); + NEO::AllocationType::bufferHostMemory); if (*ptr == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -270,7 +266,6 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, *ptr = getMemHandlePtr(hDevice, lookupTable.sharedHandleType.fd, NEO::AllocationType::buffer, - 0u, flags); if (nullptr == *ptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; @@ -279,8 +274,7 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, UNRECOVERABLE_IF(!lookupTable.sharedHandleType.isNTHandle); *ptr = this->driverHandle->importNTHandle(hDevice, lookupTable.sharedHandleType.ntHandle, - NEO::AllocationType::buffer, - 0); + NEO::AllocationType::buffer); if (*ptr == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -295,10 +289,7 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield(); NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::deviceUnifiedMemory, alignment, this->driverHandle->rootDeviceIndices, deviceBitfields); - if (NEO::debugManager.flags.EnableShareableWithoutNTHandle.get()) { - unifiedMemoryProperties.allocationFlags.flags.shareableWithoutNTHandle = 1; - } - unifiedMemoryProperties.allocationFlags.flags.shareable = isShareableMemory(deviceMemDesc->pNext, static_cast(lookupTable.exportMemory), neoDevice, unifiedMemoryProperties.allocationFlags.flags.shareableWithoutNTHandle); + unifiedMemoryProperties.allocationFlags.flags.shareable = isShareableMemory(deviceMemDesc->pNext, static_cast(lookupTable.exportMemory), neoDevice); unifiedMemoryProperties.device = neoDevice; unifiedMemoryProperties.allocationFlags.flags.compressedHint = isAllocationSuitableForCompression(lookupTable, *device, size); @@ -808,8 +799,7 @@ ze_result_t ContextImp::getIpcMemHandlesImpl(const void *ptr, ipcType = InternalIpcMemoryType::hostUnifiedMemory; } - IpcHandleType handleType = IpcHandleType::maxHandle; - bool useOpaqueHandle = isOpaqueHandleSupported(&handleType); + bool pidfdOrSocket = contextSettings.enablePidfdOrSockets; uint32_t loopCount = numIpcHandles ? *numIpcHandles : 1u; for (uint32_t i = 0u; i < loopCount; i++) { uint64_t handle = 0; @@ -820,12 +810,12 @@ ze_result_t ContextImp::getIpcMemHandlesImpl(const void *ptr, memoryManager->registerIpcExportedAllocation(alloc); - if (useOpaqueHandle) { + if (pidfdOrSocket) { IpcOpaqueMemoryData &ipcData = *reinterpret_cast(pIpcHandles[i].data); - setIPCHandleData(alloc, handle, ipcData, reinterpret_cast(ptr), static_cast(ipcType), usmPool, handleType); + setIPCHandleData(alloc, handle, ipcData, reinterpret_cast(ptr), static_cast(ipcType), usmPool); } else { IpcMemoryData &ipcData = *reinterpret_cast(pIpcHandles[i].data); - setIPCHandleData(alloc, handle, ipcData, reinterpret_cast(ptr), static_cast(ipcType), usmPool, handleType); + setIPCHandleData(alloc, handle, ipcData, reinterpret_cast(ptr), static_cast(ipcType), usmPool); } } return ZE_RESULT_SUCCESS; @@ -847,12 +837,11 @@ ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t &pIpcHandle, ze_ipc_memory_flags_t flags, void **ptr) { - uint64_t handle; - uint8_t type; - unsigned int processId; - uint64_t poolOffset; + using IpcDataT = IpcMemoryData; + const IpcDataT &ipcData = *reinterpret_cast(pIpcHandle.data); - getDataFromIpcHandle(hDevice, pIpcHandle, handle, type, processId, poolOffset); + uint64_t handle = ipcData.handle; + uint8_t type = ipcData.type; NEO::AllocationType allocationType = NEO::AllocationType::unknown; if (type == static_cast(InternalIpcMemoryType::deviceUnifiedMemory)) { @@ -866,13 +855,12 @@ ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice, *ptr = getMemHandlePtr(hDevice, handle, allocationType, - processId, flags); if (nullptr == *ptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } - *ptr = ptrOffset(*ptr, poolOffset); + *ptr = ptrOffset(*ptr, ipcData.poolOffset); return ZE_RESULT_SUCCESS; } @@ -885,15 +873,12 @@ ze_result_t ContextImp::openIpcMemHandles(ze_device_handle_t hDevice, std::vector handles; handles.reserve(numIpcHandles); + using IpcDataT = IpcMemoryData; for (uint32_t i = 0; i < numIpcHandles; i++) { - uint64_t handle; - uint8_t type; - unsigned int processId; - [[maybe_unused]] uint64_t poolOffset; + const IpcDataT &ipcData = *reinterpret_cast(pIpcHandles[i].data); + uint64_t handle = ipcData.handle; - getDataFromIpcHandle(hDevice, pIpcHandles[i], handle, type, processId, poolOffset); - - if (type != static_cast(InternalIpcMemoryType::deviceUnifiedMemory)) { + if (ipcData.type != static_cast(InternalIpcMemoryType::deviceUnifiedMemory)) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } diff --git a/level_zero/core/source/context/context_imp.h b/level_zero/core/source/context/context_imp.h index b0e4de647a..6ab486e6ea 100644 --- a/level_zero/core/source/context/context_imp.h +++ b/level_zero/core/source/context/context_imp.h @@ -174,10 +174,8 @@ struct ContextImp : Context, NEO::NonCopyableAndNonMovableClass { ContextSettings contextSettings; bool isDeviceDefinedForThisContext(Device *inDevice); - bool isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) override; - void *getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, unsigned int processId, ze_ipc_memory_flags_t flags) override; - void getDataFromIpcHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset) override; - bool isOpaqueHandleSupported(IpcHandleType *handleType) override; + bool isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice) override; + void *getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, ze_ipc_memory_flags_t flags) override; void initDeviceHandles(uint32_t numDevices, ze_device_handle_t *deviceHandles) { this->numDevices = numDevices; @@ -209,24 +207,18 @@ struct ContextImp : Context, NEO::NonCopyableAndNonMovableClass { protected: ze_result_t getIpcMemHandlesImpl(const void *ptr, uint32_t *numIpcHandles, ze_ipc_mem_handle_t *pIpcHandles); template - void setIPCHandleData(NEO::GraphicsAllocation *graphicsAllocation, uint64_t handle, IpcDataT &ipcData, uint64_t ptrAddress, uint8_t type, NEO::UsmMemAllocPool *usmPool, IpcHandleType handleType) { + void setIPCHandleData(NEO::GraphicsAllocation *graphicsAllocation, uint64_t handle, IpcDataT &ipcData, uint64_t ptrAddress, uint8_t type, NEO::UsmMemAllocPool *usmPool) { std::map::iterator ipcHandleIterator; ipcData = {}; if constexpr (std::is_same_v) { ipcData.handle = handle; ipcData.type = type; - } - if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { ipcData.memoryType = type; ipcData.processId = NEO::SysCalls::getCurrentProcessId(); - ipcData.type = handleType; - if (handleType == IpcHandleType::ntHandle) { - ipcData.handle.reserved = handle; - } else if (handleType == IpcHandleType::fdHandle) { - // For fdHandle, we store the handle as an int - ipcData.handle.fd = static_cast(handle); - } + ipcData.type = IpcHandleType::fdHandle; + ipcData.handle.fd = static_cast(handle); } if (usmPool) { diff --git a/level_zero/core/source/context/context_imp_drm/context_imp_drm.cpp b/level_zero/core/source/context/context_imp_drm/context_imp_drm.cpp index 8f0cc699d5..a850cabd76 100644 --- a/level_zero/core/source/context/context_imp_drm/context_imp_drm.cpp +++ b/level_zero/core/source/context/context_imp_drm/context_imp_drm.cpp @@ -12,23 +12,9 @@ #include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/source/device/device.h" #include "level_zero/core/source/driver/driver_handle_imp.h" - -#include namespace L0 { -bool ContextImp::isOpaqueHandleSupported(IpcHandleType *handleType) { - bool useOpaqueHandle = contextSettings.enablePidfdOrSockets; - *handleType = IpcHandleType::fdHandle; - if (useOpaqueHandle) { - if (NEO::SysCalls::prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) == -1) { - PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "prctl Syscall for PR_SET_PTRACER, PR_SET_PTRACER_ANY failed, using fallback mechanism for IPC handle exchange\n"); - return false; - } - } - return useOpaqueHandle; -} - -bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) { +bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice) { if (exportableMemory) { return true; } @@ -36,48 +22,31 @@ bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory return false; } -void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, unsigned int processId, ze_ipc_memory_flags_t flags) { +void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, ze_ipc_memory_flags_t flags) { auto neoDevice = Device::fromHandle(hDevice)->getNEODevice(); - bool useOpaqueHandle = contextSettings.enablePidfdOrSockets; - uint64_t importHandle = handle; + auto &productHelper = neoDevice->getProductHelper(); - if (useOpaqueHandle) { + bool pidfdOrSocket = false; + pidfdOrSocket = productHelper.isPidFdOrSocketForIpcSupported(); + + if (pidfdOrSocket) { // With pidfd approach extract parent pid and target fd before importing handle - pid_t exporterPid = static_cast(processId); + pid_t exporterPid = 0; unsigned int flags = 0u; int pidfd = NEO::SysCalls::pidfdopen(exporterPid, flags); if (pidfd == -1) { PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "pidfd_open Syscall failed, using fallback mechanism for IPC handle exchange\n"); } else { unsigned int flags = 0u; - int newfd = NEO::SysCalls::pidfdgetfd(pidfd, static_cast(handle), flags); + int newfd = NEO::SysCalls::pidfdgetfd(pidfd, 0, flags); if (newfd < 0) { PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "pidfd_getfd Syscall failed, using fallback mechanism for IPC handle exchange\n"); - } else { - importHandle = static_cast(newfd); } } } NEO::SvmAllocationData allocDataInternal(neoDevice->getRootDeviceIndex()); - return this->driverHandle->importFdHandle(neoDevice, flags, importHandle, allocationType, nullptr, nullptr, allocDataInternal); -} - -void ContextImp::getDataFromIpcHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset) { - bool useOpaqueHandle = contextSettings.enablePidfdOrSockets; - - if (useOpaqueHandle) { - const IpcOpaqueMemoryData *ipcData = reinterpret_cast(ipcHandle.data); - handle = static_cast(ipcData->handle.fd); - type = ipcData->memoryType; - processId = ipcData->processId; - poolOffset = ipcData->poolOffset; - } else { - const IpcMemoryData *ipcData = reinterpret_cast(ipcHandle.data); - handle = ipcData->handle; - type = ipcData->type; - poolOffset = ipcData->poolOffset; - } + return this->driverHandle->importFdHandle(neoDevice, flags, handle, allocationType, nullptr, nullptr, allocDataInternal); } } // namespace L0 diff --git a/level_zero/core/source/context/context_imp_drm_or_wddm/context_imp_drm_or_wddm.cpp b/level_zero/core/source/context/context_imp_drm_or_wddm/context_imp_drm_or_wddm.cpp index 50a9d9222a..cb03609332 100644 --- a/level_zero/core/source/context/context_imp_drm_or_wddm/context_imp_drm_or_wddm.cpp +++ b/level_zero/core/source/context/context_imp_drm_or_wddm/context_imp_drm_or_wddm.cpp @@ -6,45 +6,19 @@ */ #include "shared/source/device/device.h" -#include "shared/source/execution_environment/execution_environment.h" #include "shared/source/execution_environment/root_device_environment.h" #include "shared/source/helpers/driver_model_type.h" #include "shared/source/memory_manager/memory_manager.h" #include "shared/source/memory_manager/unified_memory_manager.h" -#include "shared/source/os_interface/linux/sys_calls.h" #include "shared/source/os_interface/os_interface.h" #include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/source/device/device.h" #include "level_zero/core/source/driver/driver_handle_imp.h" -#include - namespace L0 { -bool ContextImp::isOpaqueHandleSupported(IpcHandleType *handleType) { - bool useOpaqueHandle = contextSettings.enablePidfdOrSockets; - NEO::DriverModelType driverModelType = NEO::DriverModelType::unknown; - auto rootDeviceEnvironment = this->getDriverHandle()->getMemoryManager()->peekExecutionEnvironment().rootDeviceEnvironments[0].get(); - if (rootDeviceEnvironment->osInterface) { - driverModelType = rootDeviceEnvironment->osInterface->getDriverModel()->getDriverModelType(); - } - if (driverModelType == NEO::DriverModelType::wddm) { - *handleType = IpcHandleType::ntHandle; - useOpaqueHandle = true; - } else { - *handleType = IpcHandleType::fdHandle; - if (useOpaqueHandle) { - if (NEO::SysCalls::prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) == -1) { - PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "prctl Syscall for PR_SET_PTRACER, PR_SET_PTRACER_ANY failed, using fallback mechanism for IPC handle exchange\n"); - return false; - } - } - } - return useOpaqueHandle; -} - -bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) { +bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice) { if (exportableMemory) { return true; } @@ -61,11 +35,9 @@ bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, - unsigned int processId, ze_ipc_memory_flags_t flags) { L0::Device *device = L0::Device::fromHandle(hDevice); auto neoDevice = device->getNEODevice(); - bool useOpaqueHandle = contextSettings.enablePidfdOrSockets; NEO::DriverModelType driverType = NEO::DriverModelType::unknown; if (neoDevice->getRootDeviceEnvironment().osInterface) { driverType = neoDevice->getRootDeviceEnvironment().osInterface->getDriverModel()->getDriverModelType(); @@ -74,34 +46,13 @@ void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, if (isNTHandle) { return this->driverHandle->importNTHandle(hDevice, reinterpret_cast(handle), - allocationType, - processId); + allocationType); } else if (driverType == NEO::DriverModelType::drm) { auto neoDevice = Device::fromHandle(hDevice)->getNEODevice(); - NEO::SvmAllocationData allocDataInternal(neoDevice->getRootDeviceIndex()); - uint64_t importHandle = handle; - if (useOpaqueHandle) { - // With pidfd approach extract parent pid and target fd before importing handle - pid_t exporterPid = static_cast(processId); - unsigned int flags = 0u; - int pidfd = NEO::SysCalls::pidfdopen(exporterPid, flags); - if (pidfd == -1) { - PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "pidfd_open Syscall failed, using fallback mechanism for IPC handle exchange\n"); - } else { - unsigned int flags = 0u; - int newfd = NEO::SysCalls::pidfdgetfd(pidfd, static_cast(handle), flags); - if (newfd < 0) { - PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "pidfd_getfd Syscall failed, using fallback mechanism for IPC handle exchange\n"); - } else { - importHandle = static_cast(newfd); - } - } - } - return this->driverHandle->importFdHandle(neoDevice, flags, - importHandle, + handle, allocationType, nullptr, nullptr, @@ -111,30 +62,4 @@ void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, } } -void ContextImp::getDataFromIpcHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset) { - bool useOpaqueHandle = contextSettings.enablePidfdOrSockets; - L0::Device *device = L0::Device::fromHandle(hDevice); - auto neoDevice = device->getNEODevice(); - NEO::DriverModelType driverModelType = NEO::DriverModelType::unknown; - if (neoDevice->getRootDeviceEnvironment().osInterface) { - driverModelType = neoDevice->getRootDeviceEnvironment().osInterface->getDriverModel()->getDriverModelType(); - } - if (driverModelType == NEO::DriverModelType::wddm) { - useOpaqueHandle = true; - } - - if (useOpaqueHandle) { - const IpcOpaqueMemoryData *ipcData = reinterpret_cast(ipcHandle.data); - handle = static_cast(ipcData->handle.fd); - type = ipcData->memoryType; - processId = ipcData->processId; - poolOffset = ipcData->poolOffset; - } else { - const IpcMemoryData *ipcData = reinterpret_cast(ipcHandle.data); - handle = ipcData->handle; - type = ipcData->type; - poolOffset = ipcData->poolOffset; - } -} - } // namespace L0 diff --git a/level_zero/core/source/context/context_imp_wddm/context_imp_wddm.cpp b/level_zero/core/source/context/context_imp_wddm/context_imp_wddm.cpp index ac60427f54..259f959f13 100644 --- a/level_zero/core/source/context/context_imp_wddm/context_imp_wddm.cpp +++ b/level_zero/core/source/context/context_imp_wddm/context_imp_wddm.cpp @@ -10,33 +10,16 @@ namespace L0 { -bool ContextImp::isOpaqueHandleSupported(IpcHandleType *handleType) { - *handleType = IpcHandleType::ntHandle; - return true; -} - -bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) { +bool ContextImp::isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice) { if (exportableMemory) { return true; } - if (shareableWithoutNTHandle) { - return true; - } - return false; } -void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, unsigned int processId, ze_ipc_memory_flags_t flags) { - return this->driverHandle->importNTHandle(hDevice, reinterpret_cast(handle), allocationType, processId); -} - -void ContextImp::getDataFromIpcHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset) { - const IpcOpaqueMemoryData *ipcData = reinterpret_cast(ipcHandle.data); - handle = static_cast(ipcData->handle.reserved); - type = ipcData->memoryType; - processId = ipcData->processId; - poolOffset = ipcData->poolOffset; +void *ContextImp::getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, ze_ipc_memory_flags_t flags) { + return this->driverHandle->importNTHandle(hDevice, reinterpret_cast(handle), allocationType); } } // namespace L0 diff --git a/level_zero/core/source/driver/driver_handle_imp.cpp b/level_zero/core/source/driver/driver_handle_imp.cpp index 11ee6ae7c9..a564cfcfb2 100644 --- a/level_zero/core/source/driver/driver_handle_imp.cpp +++ b/level_zero/core/source/driver/driver_handle_imp.cpp @@ -886,13 +886,12 @@ NEO::GraphicsAllocation *DriverHandleImp::getPeerAllocation(Device *device, return alloc; } -void *DriverHandleImp::importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId) { +void *DriverHandleImp::importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType) { auto neoDevice = Device::fromHandle(hDevice)->getNEODevice(); bool isHostIpcAllocation = (allocationType == NEO::AllocationType::bufferHostMemory) ? true : false; NEO::MemoryManager::OsHandleData osHandleData{handle}; - osHandleData.parentProcessId = parentProcessId; NEO::AllocationProperties properties{neoDevice->getRootDeviceIndex(), MemoryConstants::pageSize, allocationType, diff --git a/level_zero/core/source/driver/driver_handle_imp.h b/level_zero/core/source/driver/driver_handle_imp.h index 6a22eeb093..d56b189ac1 100644 --- a/level_zero/core/source/driver/driver_handle_imp.h +++ b/level_zero/core/source/driver/driver_handle_imp.h @@ -14,7 +14,6 @@ #include "shared/source/os_interface/sys_calls_common.h" #include "level_zero/api/extensions/public/ze_exp_ext.h" -#include "level_zero/core/source/context/context.h" #include "level_zero/core/source/driver/driver_handle.h" #include "level_zero/ze_intel_gpu.h" @@ -28,6 +27,43 @@ struct FabricVertex; struct FabricEdge; struct Image; class ExternalSemaphoreController; + +#pragma pack(1) +struct IpcMemoryData { + uint64_t handle = 0; + uint64_t poolOffset = 0; + uint8_t type = 0; +}; +#pragma pack() +static_assert(sizeof(IpcMemoryData) <= ZE_MAX_IPC_HANDLE_SIZE, "IpcMemoryData is bigger than ZE_MAX_IPC_HANDLE_SIZE"); + +enum class IpcHandleType : uint8_t { + fdHandle = 0, + maxHandle +}; + +struct IpcOpaqueMemoryData { + union IpcHandle { + int fd; + uint64_t reserved; + }; + IpcHandle handle = {}; + uint64_t poolOffset = 0; + unsigned int processId = 0; + IpcHandleType type = IpcHandleType::maxHandle; + uint8_t memoryType = 0; +}; +static_assert(sizeof(IpcOpaqueMemoryData) <= ZE_MAX_IPC_HANDLE_SIZE, "IpcOpaqueMemoryData is bigger than ZE_MAX_IPC_HANDLE_SIZE"); + +struct IpcHandleTracking { + uint64_t refcnt = 0; + NEO::GraphicsAllocation *alloc = nullptr; + uint32_t handleId = 0; + uint64_t handle = 0; + uint64_t ptr = 0; + struct IpcMemoryData ipcData = {}; +}; + struct DriverHandleImp : public DriverHandle { ~DriverHandleImp() override; DriverHandleImp(); @@ -50,7 +86,7 @@ struct DriverHandleImp : public DriverHandle { void setMemoryManager(NEO::MemoryManager *memoryManager) override; MOCKABLE_VIRTUAL void *importFdHandle(NEO::Device *neoDevice, ze_ipc_memory_flags_t flags, uint64_t handle, NEO::AllocationType allocationType, void *basePointer, NEO::GraphicsAllocation **pAlloc, NEO::SvmAllocationData &mappedPeerAllocData); MOCKABLE_VIRTUAL void *importFdHandles(NEO::Device *neoDevice, ze_ipc_memory_flags_t flags, const std::vector &handles, void *basePointer, NEO::GraphicsAllocation **pAlloc, NEO::SvmAllocationData &mappedPeerAllocData); - MOCKABLE_VIRTUAL void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId); + MOCKABLE_VIRTUAL void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType); NEO::SVMAllocsManager *getSvmAllocsManager() override; NEO::StagingBufferManager *getStagingBufferManager() override; ze_result_t initialize(std::vector> neoDevices); diff --git a/level_zero/core/test/unit_tests/fixtures/device_fixture.h b/level_zero/core/test/unit_tests/fixtures/device_fixture.h index 1dc3844bb9..de94a68f3c 100644 --- a/level_zero/core/test/unit_tests/fixtures/device_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/device_fixture.h @@ -85,7 +85,7 @@ struct DriverHandleGetMemHandlePtrMock : public L0::DriverHandleImp { return &mockFd; } - void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId) override { + void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType) override { if (failHandleLookup) { return nullptr; } diff --git a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp index f9154bc22e..6a2101f8e1 100644 --- a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp +++ b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp @@ -108,7 +108,7 @@ void MemoryExportImportTest::SetUp() { context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); } -void *DriverHandleGetMemHandleMock::importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId) { +void *DriverHandleGetMemHandleMock::importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType) { if (mockHandle == allocationHandleMap.second) { return allocationHandleMap.first; } @@ -199,7 +199,7 @@ void MemoryExportImportWSLTest::TearDown() { delete currMemoryManager; } -void *DriverHandleGetWinHandleMock::importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId) { +void *DriverHandleGetWinHandleMock::importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType) { if (mockHandle == allocationMap.second) { return allocationMap.first; } @@ -572,4 +572,4 @@ void MemoryGetIpcHandlePidfdTest::TearDown() { } } // namespace ult -} // namespace L0 +} // namespace L0 \ No newline at end of file diff --git a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h index b664cc93a9..2a12ea2761 100644 --- a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h @@ -79,7 +79,7 @@ struct MemoryExportImportTest : public ::testing::Test { }; struct DriverHandleGetMemHandleMock : public L0::DriverHandleImp { - void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId) override; + void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType) override; void *importFdHandle(NEO::Device *neoDevice, ze_ipc_memory_flags_t flags, uint64_t handle, NEO::AllocationType allocationType, void *basePointer, NEO::GraphicsAllocation **pAloc, NEO::SvmAllocationData &mappedPeerAllocData) override; @@ -128,7 +128,7 @@ struct MemoryExportImportWSLTest : public ::testing::Test { }; struct DriverHandleGetWinHandleMock : public L0::DriverHandleImp { - void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType, uint32_t parentProcessId) override; + void *importNTHandle(ze_device_handle_t hDevice, void *handle, NEO::AllocationType allocationType) override; uint64_t mockHandle = 57; std::pair allocationMap; diff --git a/level_zero/core/test/unit_tests/mocks/mock_context.h b/level_zero/core/test/unit_tests/mocks/mock_context.h index a8e95f8f80..e1bb2f88cb 100644 --- a/level_zero/core/test/unit_tests/mocks/mock_context.h +++ b/level_zero/core/test/unit_tests/mocks/mock_context.h @@ -79,17 +79,15 @@ struct Mock : public Context { ADDMETHOD_NOBASE(getVirtualAddressSpaceIpcHandle, ze_result_t, ZE_RESULT_SUCCESS, (ze_device_handle_t, ze_ipc_mem_handle_t *)); ADDMETHOD_NOBASE(putVirtualAddressSpaceIpcHandle, ze_result_t, ZE_RESULT_SUCCESS, (ze_ipc_mem_handle_t ipcHandle)); ADDMETHOD_NOBASE(lockMemory, ze_result_t, ZE_RESULT_SUCCESS, (ze_device_handle_t hDevice, void *ptr, size_t size)); - ADDMETHOD_NOBASE(isShareableMemory, bool, true, (const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle)); - ADDMETHOD_NOBASE(isOpaqueHandleSupported, bool, true, (IpcHandleType * handleType)); - ADDMETHOD_NOBASE(getMemHandlePtr, void *, nullptr, (ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, unsigned int processId, ze_ipc_memory_flags_t flags)); - ADDMETHOD_NOBASE_VOIDRETURN(getDataFromIpcHandle, (ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset)); + ADDMETHOD_NOBASE(isShareableMemory, bool, true, (const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice)); + ADDMETHOD_NOBASE(getMemHandlePtr, void *, nullptr, (ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, ze_ipc_memory_flags_t flags)); ADDMETHOD_NOBASE(getPitchFor2dImage, ze_result_t, ZE_RESULT_SUCCESS, (ze_device_handle_t, size_t, size_t, unsigned int, size_t *)); ADDMETHOD_NOBASE(getContextExt, ContextExt *, nullptr, ()); }; struct ContextShareableMock : public L0::ContextImp { ContextShareableMock(L0::DriverHandle *driverHandle) : L0::ContextImp(driverHandle) {} - bool isShareableMemory(const void *pNext, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) override { + bool isShareableMemory(const void *pNext, bool exportableMemory, NEO::Device *neoDevice) override { return true; } }; diff --git a/level_zero/core/test/unit_tests/sources/context/test_context.cpp b/level_zero/core/test/unit_tests/sources/context/test_context.cpp index 1c3f853a6e..8d87797b4f 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context.cpp @@ -8,7 +8,6 @@ #include "shared/source/gmm_helper/gmm.h" #include "shared/source/memory_manager/gfx_partition.h" #include "shared/source/os_interface/device_factory.h" -#include "shared/source/unified_memory/unified_memory.h" #include "shared/source/utilities/cpu_info.h" #include "shared/test/common/mocks/mock_bindless_heaps_helper.h" #include "shared/test/common/mocks/mock_command_stream_receiver.h" @@ -2798,267 +2797,5 @@ HWTEST_F(ContextTest, givenMakeImageResidentThenMakeImageResidentIsCalledWithFor EXPECT_EQ(ZE_RESULT_SUCCESS, res); } -class ContextWhiteboxForIpcTesting : public ::L0::ContextImp { - public: - ContextWhiteboxForIpcTesting(L0::DriverHandle *driverHandle) : L0::ContextImp(driverHandle) {} - - using ::L0::ContextImp::setIPCHandleData; -}; - -TEST_F(ContextTest, whenCallingSetIPCHandleDataWithIpcMemoryDataTypeThenIpcDataIsSetInHandleTracking) { - ze_context_handle_t hContext; - ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0}; - - ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); - - ContextWhiteboxForIpcTesting contextWhitebox(driverHandle.get()); - - // Create a mock graphics allocation - NEO::MockGraphicsAllocation mockAllocation; - - // Set up test data - uint64_t handle = 12345; - L0::IpcMemoryData ipcData; - ipcData.handle = handle; - ipcData.type = static_cast(InternalMemoryType::deviceUnifiedMemory); - ipcData.poolOffset = 0; - - uint64_t ptrAddress = 0x1000; - uint8_t type = static_cast(InternalMemoryType::deviceUnifiedMemory); - - // Verify IPC handle map is initially empty - EXPECT_TRUE(driverHandle->getIPCHandleMap().empty()); - - // Call setIPCHandleData with IpcMemoryData type - contextWhitebox.setIPCHandleData(&mockAllocation, handle, ipcData, ptrAddress, type, nullptr, L0::IpcHandleType::fdHandle); - - // Verify the handle was added to the IPC handle map - auto &ipcHandleMap = driverHandle->getIPCHandleMap(); - EXPECT_EQ(1u, ipcHandleMap.size()); - - // Verify the handle tracking entry - auto handleIterator = ipcHandleMap.find(handle); - ASSERT_NE(handleIterator, ipcHandleMap.end()); - - L0::IpcHandleTracking *handleTracking = handleIterator->second; - EXPECT_NE(nullptr, handleTracking); - EXPECT_EQ(&mockAllocation, handleTracking->alloc); - EXPECT_EQ(1u, handleTracking->refcnt); - EXPECT_EQ(ptrAddress, handleTracking->ptr); - EXPECT_EQ(handle, handleTracking->handle); - - // Verify that the ipcData field is set - EXPECT_EQ(handle, handleTracking->ipcData.handle); - EXPECT_EQ(type, handleTracking->ipcData.type); - EXPECT_EQ(0u, handleTracking->ipcData.poolOffset); - - // Clean up - remove the entry from the map to avoid issues in teardown - delete handleTracking; - driverHandle->getIPCHandleMap().clear(); - - ContextImp *contextImp = static_cast(L0::Context::fromHandle(hContext)); - res = contextImp->destroy(); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); -} - -TEST_F(ContextTest, whenCallingSetIPCHandleDataWithIpcOpaqueMemoryDataTypeThenIpcDataIsNotSetInHandleTracking) { - ze_context_handle_t hContext; - ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0}; - - ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); - - ContextWhiteboxForIpcTesting contextWhitebox(driverHandle.get()); - - // Create a mock graphics allocation - NEO::MockGraphicsAllocation mockAllocation; - - // Set up test data for IpcOpaqueMemoryData - uint64_t handle = 67890; - L0::IpcOpaqueMemoryData opaqueIpcData; - opaqueIpcData.handle.fd = static_cast(handle); - opaqueIpcData.memoryType = static_cast(InternalMemoryType::sharedUnifiedMemory); - opaqueIpcData.processId = 1234; - opaqueIpcData.type = L0::IpcHandleType::fdHandle; - opaqueIpcData.poolOffset = 0; - - uint64_t ptrAddress = 0x2000; - uint8_t type = static_cast(InternalMemoryType::sharedUnifiedMemory); - - // Verify IPC handle map is initially empty - EXPECT_TRUE(driverHandle->getIPCHandleMap().empty()); - - // Call setIPCHandleData with IpcOpaqueMemoryData type - contextWhitebox.setIPCHandleData(&mockAllocation, handle, opaqueIpcData, ptrAddress, type, nullptr, L0::IpcHandleType::fdHandle); - - // Verify the handle was added to the IPC handle map - auto &ipcHandleMap = driverHandle->getIPCHandleMap(); - EXPECT_EQ(1u, ipcHandleMap.size()); - - // Verify the handle tracking entry - auto handleIterator = ipcHandleMap.find(handle); - ASSERT_NE(handleIterator, ipcHandleMap.end()); - - L0::IpcHandleTracking *handleTracking = handleIterator->second; - EXPECT_NE(nullptr, handleTracking); - EXPECT_EQ(&mockAllocation, handleTracking->alloc); - EXPECT_EQ(1u, handleTracking->refcnt); - EXPECT_EQ(ptrAddress, handleTracking->ptr); - EXPECT_EQ(handle, handleTracking->handle); - - // Verify that the ipcData field is NOT set when using IpcOpaqueMemoryData - // The ipcData field should retain its default/empty values since the constexpr condition is false - EXPECT_EQ(0u, handleTracking->ipcData.handle); // Should be default value (0) - EXPECT_EQ(0u, handleTracking->ipcData.type); // Should be default value (0) - EXPECT_EQ(0u, handleTracking->ipcData.poolOffset); // Should be default value (0) - - // Clean up - remove the entry from the map to avoid issues in teardown - delete handleTracking; - driverHandle->getIPCHandleMap().clear(); - - ContextImp *contextImp = static_cast(L0::Context::fromHandle(hContext)); - res = contextImp->destroy(); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); -} - -TEST_F(ContextTest, whenCallingSetIPCHandleDataWithInvalidIpcHandleTypeThenHandleUnionIsNotSet) { - ze_context_handle_t hContext; - ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0}; - - ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); - - ContextWhiteboxForIpcTesting contextWhitebox(driverHandle.get()); - - // Create a mock graphics allocation - NEO::MockGraphicsAllocation mockAllocation; - - // Set up test data with invalid IpcHandleType - uint64_t handle = 11111; - L0::IpcOpaqueMemoryData opaqueIpcData; - uint64_t ptrAddress = 0x3000; - uint8_t type = static_cast(InternalMemoryType::deviceUnifiedMemory); - - // Test with maxHandle (invalid but defined) - L0::IpcHandleType invalidHandleType1 = L0::IpcHandleType::maxHandle; - - // Verify IPC handle map is initially empty - EXPECT_TRUE(driverHandle->getIPCHandleMap().empty()); - - // Call setIPCHandleData with invalid handle type (maxHandle) - contextWhitebox.setIPCHandleData(&mockAllocation, handle, opaqueIpcData, ptrAddress, type, nullptr, invalidHandleType1); - - // Verify the handle was added to the IPC handle map - auto &ipcHandleMap = driverHandle->getIPCHandleMap(); - EXPECT_EQ(1u, ipcHandleMap.size()); - - // Verify the IpcOpaqueMemoryData values - EXPECT_EQ(type, opaqueIpcData.memoryType); - EXPECT_EQ(invalidHandleType1, opaqueIpcData.type); - EXPECT_NE(0u, opaqueIpcData.processId); // Should be set to current process ID - - // Clean up - auto handleIterator = ipcHandleMap.find(handle); - ASSERT_NE(handleIterator, ipcHandleMap.end()); - delete handleIterator->second; - driverHandle->getIPCHandleMap().clear(); - - // Test with completely invalid handle type (beyond enum range) - handle = 22222; - opaqueIpcData = {}; // Reset - L0::IpcHandleType invalidHandleType2 = static_cast(255); - - contextWhitebox.setIPCHandleData(&mockAllocation, handle, opaqueIpcData, ptrAddress, type, nullptr, invalidHandleType2); - - // Verify the handle was added to the IPC handle map - EXPECT_EQ(1u, ipcHandleMap.size()); - - // Verify the IpcOpaqueMemoryData values - EXPECT_EQ(type, opaqueIpcData.memoryType); - EXPECT_EQ(invalidHandleType2, opaqueIpcData.type); - EXPECT_NE(0u, opaqueIpcData.processId); // Should be set to current process ID - - // Clean up - handleIterator = ipcHandleMap.find(handle); - ASSERT_NE(handleIterator, ipcHandleMap.end()); - delete handleIterator->second; - driverHandle->getIPCHandleMap().clear(); - - ContextImp *contextImp = static_cast(L0::Context::fromHandle(hContext)); - res = contextImp->destroy(); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); -} - -TEST_F(ContextTest, whenCallingSetIPCHandleDataWithNtHandleTypeThenHandleUnionIsSetCorrectly) { - ze_context_handle_t hContext; - ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0}; - - ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); - - ContextWhiteboxForIpcTesting contextWhitebox(driverHandle.get()); - - // Create a mock graphics allocation - NEO::MockGraphicsAllocation mockAllocation; - - // Set up test data for ntHandle - uint64_t handle = 98765; - L0::IpcOpaqueMemoryData opaqueIpcData; - opaqueIpcData.handle.reserved = handle; - opaqueIpcData.memoryType = static_cast(InternalMemoryType::deviceUnifiedMemory); - opaqueIpcData.processId = 5678; - opaqueIpcData.type = L0::IpcHandleType::ntHandle; - opaqueIpcData.poolOffset = 0; - - uint64_t ptrAddress = 0x4000; - uint8_t type = static_cast(InternalMemoryType::deviceUnifiedMemory); - - // Verify IPC handle map is initially empty - EXPECT_TRUE(driverHandle->getIPCHandleMap().empty()); - - // Call setIPCHandleData with ntHandle type - contextWhitebox.setIPCHandleData(&mockAllocation, handle, opaqueIpcData, ptrAddress, type, nullptr, L0::IpcHandleType::ntHandle); - - // Verify the handle was added to the IPC handle map - auto &ipcHandleMap = driverHandle->getIPCHandleMap(); - EXPECT_EQ(1u, ipcHandleMap.size()); - - // Verify the handle tracking entry - auto handleIterator = ipcHandleMap.find(handle); - ASSERT_NE(handleIterator, ipcHandleMap.end()); - - L0::IpcHandleTracking *handleTracking = handleIterator->second; - EXPECT_NE(nullptr, handleTracking); - EXPECT_EQ(&mockAllocation, handleTracking->alloc); - EXPECT_EQ(1u, handleTracking->refcnt); - EXPECT_EQ(ptrAddress, handleTracking->ptr); - EXPECT_EQ(handle, handleTracking->handle); - - // Verify that the ipcData field is NOT set when using IpcOpaqueMemoryData - // The ipcData field should retain its default/empty values since the constexpr condition is false - EXPECT_EQ(0u, handleTracking->ipcData.handle); // Should be default value (0) - EXPECT_EQ(0u, handleTracking->ipcData.type); // Should be default value (0) - EXPECT_EQ(0u, handleTracking->ipcData.poolOffset); // Should be default value (0) - - // Verify the IpcOpaqueMemoryData values are set correctly - EXPECT_EQ(type, opaqueIpcData.memoryType); - EXPECT_EQ(L0::IpcHandleType::ntHandle, opaqueIpcData.type); - uint64_t reservedValue = 0; - memcpy(&reservedValue, &opaqueIpcData.handle.reserved, sizeof(reservedValue)); - EXPECT_EQ(handle, reservedValue); - unsigned int processID = 0; - memcpy(&processID, &opaqueIpcData.processId, sizeof(processID)); - EXPECT_NE(0u, processID); // Should be set to current process ID - - // Clean up - remove the entry from the map to avoid issues in teardown - delete handleTracking; - driverHandle->getIPCHandleMap().clear(); - - ContextImp *contextImp = static_cast(L0::Context::fromHandle(hContext)); - res = contextImp->destroy(); - EXPECT_EQ(ZE_RESULT_SUCCESS, res); -} - } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/context/test_context_drm.cpp b/level_zero/core/test/unit_tests/sources/context/test_context_drm.cpp index 4b43034918..5d8321222e 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context_drm.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context_drm.cpp @@ -41,8 +41,8 @@ TEST_F(ContextIsShareable, whenCallingisSharedMemoryThenCorrectResultIsReturned) bool exportableMemoryFalse = false; bool exportableMemoryTrue = true; - EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice, false)); - EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryTrue, neoDevice, false)); + EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice)); + EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryTrue, neoDevice)); res = contextImp->destroy(); EXPECT_EQ(ZE_RESULT_SUCCESS, res); @@ -56,7 +56,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithValidHandleThenSuccess // Test Successfully returning fd Handle fixtureMemoryManager->ntHandle = false; - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithInvalidHandleThenNullptrIsReturned) { @@ -68,20 +68,19 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithInvalidHandleThenNullp // Test Failing returning fd Handle fixtureMemoryManager->ntHandle = false; - EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndSyscallsReturnSuccessThenValidHandleIsReturned) { DebugManagerStateRestore restorer; debugManager.flags.EnablePidFdOrSocketsForIpc.set(1); - context->contextSettings.enablePidfdOrSockets = true; VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); uint64_t handle = 57; // Test Successfully returning fd Handle - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); EXPECT_EQ(1, NEO::SysCalls::pidfdgetfdCalled); } @@ -89,7 +88,6 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndSyscalls TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdOpenSyscallReturnFailThenPidfdGetNotCalled) { DebugManagerStateRestore restorer; debugManager.flags.EnablePidFdOrSocketsForIpc.set(1); - context->contextSettings.enablePidfdOrSockets = true; VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); @@ -99,7 +97,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdOpe uint64_t handle = 57; - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); EXPECT_EQ(0, NEO::SysCalls::pidfdgetfdCalled); } @@ -107,7 +105,6 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdOpe TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdGetSyscallReturnFailThenCorrectHandleIsReturned) { DebugManagerStateRestore restorer; debugManager.flags.EnablePidFdOrSocketsForIpc.set(1); - context->contextSettings.enablePidfdOrSockets = true; VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); @@ -116,7 +113,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdGet }); uint64_t handle = 57; - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); EXPECT_EQ(1, NEO::SysCalls::pidfdgetfdCalled); } diff --git a/level_zero/core/test/unit_tests/sources/context/test_context_drm_or_wddm.cpp b/level_zero/core/test/unit_tests/sources/context/test_context_drm_or_wddm.cpp index d509f06dab..7c17fcefe3 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context_drm_or_wddm.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context_drm_or_wddm.cpp @@ -5,15 +5,11 @@ * */ -#include "shared/source/os_interface/linux/sys_calls.h" -#include "shared/test/common/helpers/variable_backup.h" #include "shared/test/common/mocks/mock_device.h" #include "shared/test/common/mocks/mock_driver_model.h" #include "shared/test/common/mocks/mock_memory_manager.h" -#include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h" #include "shared/test/common/test_macros/test.h" -#include "level_zero/core/source/context/context.h" #include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" @@ -37,17 +33,17 @@ TEST_F(ContextIsShareable, whenCallingisSharedMemoryThenCorrectResultIsReturned) neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice, false)); - EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryTrue, neoDevice, false)); + EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice)); + EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryTrue, neoDevice)); // exportDesc set && neoDevice is NOT WDDM - EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(&desc, exportableMemoryFalse, neoDevice, false)); + EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(&desc, exportableMemoryFalse, neoDevice)); // exportDesc unset && neoDevice is NOT WDDM - EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice, false)); + EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice)); // exportDesc unset && neoDevice is WDDM neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice, false)); + EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice)); // exportDesc is set && Exportable Memory is False && neoDevice is WDDM - EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(&desc, exportableMemoryFalse, neoDevice, false)); + EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(&desc, exportableMemoryFalse, neoDevice)); res = contextImp->destroy(); EXPECT_EQ(ZE_RESULT_SUCCESS, res); @@ -61,7 +57,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithValidNTHandleThenSucce // Test Successfully returning NT Handle fixtureMemoryManager->ntHandle = true; - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithInvalidHandleThenNullptrIsReturned) { @@ -73,11 +69,11 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithInvalidHandleThenNullp // Test Failing returning NT Handle fixtureMemoryManager->ntHandle = true; - EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); // Test Failing returning fd Handle fixtureMemoryManager->ntHandle = false; - EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithDRMDriverTypeWithNonNTHandleThenSuccessIsReturned) { @@ -88,7 +84,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithDRMDriverTypeWithNonNT // Test Successfully returning fd Handle fixtureMemoryManager->ntHandle = false; - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithWDDMDriverTypeWithNonNTHandleThenNullPtrIsReturned) { @@ -99,418 +95,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithWDDMDriverTypeWithNonN // Test Successfully returning fd Handle fixtureMemoryManager->ntHandle = false; - EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); -} - -TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdOpenSyscallReturnFailThenPidfdGetNotCalled) { - // Enable pidfd/sockets for IPC - context->contextSettings.enablePidfdOrSockets = true; - - // Set up DRM driver model to use pidfd method - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - MemoryManagerMemHandleMock *fixtureMemoryManager = static_cast(currMemoryManager); - fixtureMemoryManager->ntHandle = false; - - VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); - VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); - VariableBackup mockPidfdOpen(&NEO::SysCalls::sysCallsPidfdOpen, [](pid_t, unsigned int) -> int { - return -1; - }); - - uint64_t handle = 57; - - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); - EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); - EXPECT_EQ(0, NEO::SysCalls::pidfdgetfdCalled); -} - -TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdGetSyscallReturnFailThenCorrectHandleIsReturned) { - // Enable pidfd/sockets for IPC - context->contextSettings.enablePidfdOrSockets = true; - - // Set up DRM driver model to use pidfd method - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - MemoryManagerMemHandleMock *fixtureMemoryManager = static_cast(currMemoryManager); - fixtureMemoryManager->ntHandle = false; - - VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); - VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); - VariableBackup mockPidfdGet(&NEO::SysCalls::sysCallsPidfdGetfd, [](int, int, unsigned int) -> int { - return -1; - }); - uint64_t handle = 57; - - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); - EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); - EXPECT_EQ(1, NEO::SysCalls::pidfdgetfdCalled); -} - -TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdGetSyscallFailsWithNegativeValueThenFallbackHandleIsUsed) { - // Enable pidfd/sockets for IPC - context->contextSettings.enablePidfdOrSockets = true; - - // Set up DRM driver model to use pidfd method - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - MemoryManagerMemHandleMock *fixtureMemoryManager = static_cast(currMemoryManager); - fixtureMemoryManager->ntHandle = false; - - uint64_t originalHandle = 42; - static bool pidfdGetFdCalled = false; - pidfdGetFdCalled = false; - - VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); - VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); - - // Mock pidfd_open to succeed - VariableBackup mockPidfdOpen(&NEO::SysCalls::sysCallsPidfdOpen, [](pid_t, unsigned int) -> int { - return 100; // Valid pidfd - }); - - // Mock pidfd_getfd to fail with different negative values - VariableBackup mockPidfdGet(&NEO::SysCalls::sysCallsPidfdGetfd, [](int pidfd, int fd, unsigned int flags) -> int { - pidfdGetFdCalled = true; - EXPECT_EQ(100, pidfd); // Should receive the pidfd from pidfd_open - EXPECT_EQ(42, fd); // Should receive the original handle - EXPECT_EQ(0u, flags); // Flags should be 0 - return -2; // Fail with a different negative value - }); - - void *result = context->getMemHandlePtr(device, originalHandle, NEO::AllocationType::buffer, 0u, 0); - - EXPECT_NE(nullptr, result); - EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); - EXPECT_EQ(1, NEO::SysCalls::pidfdgetfdCalled); - EXPECT_TRUE(pidfdGetFdCalled); -} - -TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithPidfdMethodAndPidfdGetSyscallReturnsZeroThenSuccessfulHandleIsUsed) { - // Enable pidfd/sockets for IPC - context->contextSettings.enablePidfdOrSockets = true; - - // Set up DRM driver model to use pidfd method - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - MemoryManagerMemHandleMock *fixtureMemoryManager = static_cast(currMemoryManager); - fixtureMemoryManager->ntHandle = false; - - uint64_t originalHandle = 123; - static bool pidfdGetFdCalled = false; - pidfdGetFdCalled = false; - - VariableBackup pidfdOpenCalledBackup(&NEO::SysCalls::pidfdopenCalled, 0u); - VariableBackup pidfdGetFdCalledBackup(&NEO::SysCalls::pidfdgetfdCalled, 0u); - - // Mock pidfd_open to succeed - VariableBackup mockPidfdOpen(&NEO::SysCalls::sysCallsPidfdOpen, [](pid_t, unsigned int) -> int { - return 200; // Valid pidfd - }); - - // Mock pidfd_getfd to return 0 (which is a valid fd but edge case) - VariableBackup mockPidfdGet(&NEO::SysCalls::sysCallsPidfdGetfd, [](int pidfd, int fd, unsigned int flags) -> int { - pidfdGetFdCalled = true; - EXPECT_EQ(200, pidfd); - EXPECT_EQ(123, fd); - EXPECT_EQ(0u, flags); - return 0; // Return 0 (valid fd) - }); - - void *result = context->getMemHandlePtr(device, originalHandle, NEO::AllocationType::buffer, 0u, 0); - - EXPECT_NE(nullptr, result); - EXPECT_EQ(1, NEO::SysCalls::pidfdopenCalled); - EXPECT_EQ(1, NEO::SysCalls::pidfdgetfdCalled); - EXPECT_TRUE(pidfdGetFdCalled); -} - -using GetDataFromIpcHandleTest = Test; - -TEST_F(GetDataFromIpcHandleTest, whenCallingGetDataFromIpcHandleWithOpaqueHandleEnabledThenOpaqueDataIsExtracted) { - // Enable opaque handles - context->contextSettings.enablePidfdOrSockets = true; - - ze_ipc_mem_handle_t ipcHandle = {}; - IpcOpaqueMemoryData *opaqueData = reinterpret_cast(ipcHandle.data); - opaqueData->handle.fd = 123; - opaqueData->memoryType = 42; - opaqueData->processId = 456; - opaqueData->poolOffset = 789; - - uint64_t handle = 0; - uint8_t type = 0; - unsigned int processId = 0; - uint64_t poolOffset = 0; - - context->getDataFromIpcHandle(device, ipcHandle, handle, type, processId, poolOffset); - - EXPECT_EQ(123u, handle); - EXPECT_EQ(42u, type); - EXPECT_EQ(456u, processId); - EXPECT_EQ(789u, poolOffset); -} - -TEST_F(GetDataFromIpcHandleTest, whenCallingGetDataFromIpcHandleWithOpaqueHandleDisabledThenRegularDataIsExtracted) { - // Disable opaque handles - context->contextSettings.enablePidfdOrSockets = false; - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - ze_ipc_mem_handle_t ipcHandle = {}; - IpcMemoryData *regularData = reinterpret_cast(ipcHandle.data); - regularData->handle = 987; - regularData->type = 65; - regularData->poolOffset = 321; - - uint64_t handle = 0; - uint8_t type = 0; - unsigned int processId = 0; - uint64_t poolOffset = 0; - - context->getDataFromIpcHandle(device, ipcHandle, handle, type, processId, poolOffset); - - EXPECT_EQ(987u, handle); - EXPECT_EQ(65u, type); - EXPECT_EQ(321u, poolOffset); - // processId should remain 0 for regular data - EXPECT_EQ(0u, processId); -} - -TEST_F(GetDataFromIpcHandleTest, whenCallingGetDataFromIpcHandleWithWDDMDriverThenOpaqueHandleIsForced) { - // Initially disable opaque handles - context->contextSettings.enablePidfdOrSockets = false; - // Set WDDM driver model which should force opaque handles - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - ze_ipc_mem_handle_t ipcHandle = {}; - IpcOpaqueMemoryData *opaqueData = reinterpret_cast(ipcHandle.data); - opaqueData->handle.fd = 555; - opaqueData->memoryType = 77; - opaqueData->processId = 888; - opaqueData->poolOffset = 999; - - uint64_t handle = 0; - uint8_t type = 0; - unsigned int processId = 0; - uint64_t poolOffset = 0; - - context->getDataFromIpcHandle(device, ipcHandle, handle, type, processId, poolOffset); - - // Should extract opaque data even though enablePidfdOrSockets was false - EXPECT_EQ(555u, handle); - EXPECT_EQ(77u, type); - EXPECT_EQ(888u, processId); - EXPECT_EQ(999u, poolOffset); -} - -TEST_F(GetDataFromIpcHandleTest, whenCallingGetDataFromIpcHandleWithNullOSInterfaceThenRegularDataIsExtracted) { - // Disable opaque handles and set null osInterface - context->contextSettings.enablePidfdOrSockets = false; - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(); - - ze_ipc_mem_handle_t ipcHandle = {}; - IpcMemoryData *regularData = reinterpret_cast(ipcHandle.data); - regularData->handle = 111; - regularData->type = 22; - regularData->poolOffset = 333; - - uint64_t handle = 0; - uint8_t type = 0; - unsigned int processId = 0; - uint64_t poolOffset = 0; - - context->getDataFromIpcHandle(device, ipcHandle, handle, type, processId, poolOffset); - - EXPECT_EQ(111u, handle); - EXPECT_EQ(22u, type); - EXPECT_EQ(333u, poolOffset); - EXPECT_EQ(0u, processId); -} - -TEST_F(GetDataFromIpcHandleTest, whenCallingGetDataFromIpcHandleWithZeroValuesThenZeroValuesAreExtracted) { - context->contextSettings.enablePidfdOrSockets = true; - - ze_ipc_mem_handle_t ipcHandle = {}; - // All values should be zero by default - - uint64_t handle = 999; // Initialize with non-zero to verify it gets overwritten - uint8_t type = 99; - unsigned int processId = 888; - uint64_t poolOffset = 777; - - context->getDataFromIpcHandle(device, ipcHandle, handle, type, processId, poolOffset); - - EXPECT_EQ(0u, handle); - EXPECT_EQ(0u, type); - EXPECT_EQ(0u, processId); - EXPECT_EQ(0u, poolOffset); -} - -inline int mockPrctl(int option, unsigned long arg) { - return 0; -} - -using IsOpaqueHandleSupportedTest = Test; - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithWDDMDriverThenTrueIsReturnedAndHandleTypeIsNT) { - // Set WDDM driver model on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - // Disable opaque handles in settings (should be overridden by WDDM) - context->contextSettings.enablePidfdOrSockets = false; - - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_TRUE(result); - EXPECT_EQ(IpcHandleType::ntHandle, handleType); -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithDRMDriverAndOpaqueEnabledThenTrueIsReturnedAndHandleTypeIsFd) { - // Set DRM driver model on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - VariableBackup sysCallsPrctlBackup{&NEO::SysCalls::sysCallsPrctl, mockPrctl}; - - // Enable opaque handles in settings - context->contextSettings.enablePidfdOrSockets = true; - - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_TRUE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithDRMDriverAndOpaqueDisabledThenFalseIsReturnedAndHandleTypeIsFd) { - // Set DRM driver model on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - VariableBackup sysCallsPrctlBackup{&NEO::SysCalls::sysCallsPrctl, mockPrctl}; - - // Disable opaque handles in settings - context->contextSettings.enablePidfdOrSockets = false; - - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_FALSE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithNullOSInterfaceThenSettingsValueIsReturnedAndHandleTypeIsFd) { - // Set null OS interface (unknown driver model) on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(); - - // Test with opaque handles enabled - context->contextSettings.enablePidfdOrSockets = true; - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_TRUE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); - - // Test with opaque handles disabled - context->contextSettings.enablePidfdOrSockets = false; - handleType = IpcHandleType::maxHandle; - result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_FALSE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithUnknownDriverModelThenSettingsValueIsReturnedAndHandleTypeIsFd) { - // Set mock driver model (unknown type) on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - // Test with opaque handles enabled - context->contextSettings.enablePidfdOrSockets = true; - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_TRUE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); - - // Test with opaque handles disabled - context->contextSettings.enablePidfdOrSockets = false; - handleType = IpcHandleType::maxHandle; - result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_FALSE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedMultipleTimesWithSameConfigurationThenConsistentResultsAreReturned) { - // Set WDDM driver model on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - context->contextSettings.enablePidfdOrSockets = false; - - // Call multiple times and verify consistent results - for (int i = 0; i < 3; i++) { - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_TRUE(result); - EXPECT_EQ(IpcHandleType::ntHandle, handleType); - } -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithDRMDriverAndOpaqueEnabledThenPrctlIsCalledSuccessfully) { - // Set DRM driver model on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - VariableBackup sysCallsPrctlBackup{&NEO::SysCalls::sysCallsPrctl, mockPrctl}; - - // Enable opaque handles in settings (this will trigger the prctl call) - context->contextSettings.enablePidfdOrSockets = true; - - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_TRUE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); -} - -TEST_F(IsOpaqueHandleSupportedTest, whenCallingIsOpaqueHandleSupportedWithDRMDriverAndOpaqueEnabledAndPrctlFailsThenFalseIsReturnedAndHandleTypeIsFd) { - // Set DRM driver model on the execution environment that the context will actually use - auto &executionEnvironment = driverHandle->getMemoryManager()->peekExecutionEnvironment(); - executionEnvironment.rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - executionEnvironment.rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - // Enable opaque handles in settings (this will trigger the prctl call) - context->contextSettings.enablePidfdOrSockets = true; - - // Save original sysCallsPrctl and override to simulate failure - auto originalSysCallsPrctl = NEO::SysCalls::sysCallsPrctl; - NEO::SysCalls::sysCallsPrctl = [](int, unsigned long) -> int { - return -1; // Simulate failure - }; - - IpcHandleType handleType = IpcHandleType::maxHandle; - bool result = context->isOpaqueHandleSupported(&handleType); - - EXPECT_FALSE(result); - EXPECT_EQ(IpcHandleType::fdHandle, handleType); - - // Restore original sysCallsPrctl - NEO::SysCalls::sysCallsPrctl = originalSysCallsPrctl; + EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } } // namespace ult diff --git a/level_zero/core/test/unit_tests/sources/context/test_context_wddm.cpp b/level_zero/core/test/unit_tests/sources/context/test_context_wddm.cpp index cef8b0e6cb..98e1d2e578 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context_wddm.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context_wddm.cpp @@ -39,8 +39,8 @@ TEST_F(ContextIsShareable, whenCallingisSharedMemoryThenCorrectResultIsReturned) bool exportableMemoryFalse = false; bool exportableMemoryTrue = true; - EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice, false)); - EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryTrue, neoDevice, false)); + EXPECT_EQ(exportableMemoryFalse, contextImp->isShareableMemory(nullptr, exportableMemoryFalse, neoDevice)); + EXPECT_EQ(exportableMemoryTrue, contextImp->isShareableMemory(nullptr, exportableMemoryTrue, neoDevice)); res = contextImp->destroy(); EXPECT_EQ(ZE_RESULT_SUCCESS, res); @@ -54,7 +54,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithValidHandleThenSuccess // Test Successfully returning NT Handle fixtureMemoryManager->ntHandle = true; - EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_NE(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithInvalidHandleThenNullptrIsReturned) { @@ -66,7 +66,7 @@ TEST_F(GetMemHandlePtrTest, whenCallingGetMemHandlePtrWithInvalidHandleThenNullp // Test Failing returning NT Handle fixtureMemoryManager->ntHandle = true; - EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0u, 0)); + EXPECT_EQ(nullptr, context->getMemHandlePtr(device, handle, NEO::AllocationType::buffer, 0)); } } // namespace ult 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 63b86b54bd..51ef7713a3 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 @@ -358,7 +358,7 @@ HWTEST_F(ImportNTHandleWithMockMemoryManager, givenCallToImportNTHandleWithHostB uint64_t imageHandle = 0x1; NEO::AllocationType allocationType = NEO::AllocationType::bufferHostMemory; - void *ptr = driverHandle->importNTHandle(device->toHandle(), &imageHandle, allocationType, 0u); + void *ptr = driverHandle->importNTHandle(device->toHandle(), &imageHandle, allocationType); EXPECT_NE(ptr, nullptr); auto allocData = driverHandle->svmAllocsManager->getSVMAlloc(ptr); @@ -376,7 +376,7 @@ HWTEST_F(ImportNTHandleWithMockMemoryManager, givenCallToImportNTHandleWithBuffe uint64_t imageHandle = 0x1; NEO::AllocationType allocationType = NEO::AllocationType::buffer; - void *ptr = driverHandle->importNTHandle(device->toHandle(), &imageHandle, allocationType, 0u); + void *ptr = driverHandle->importNTHandle(device->toHandle(), &imageHandle, allocationType); EXPECT_NE(ptr, nullptr); auto allocData = driverHandle->svmAllocsManager->getSVMAlloc(ptr); diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 8db77f3d08..48888eb6a6 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -5879,7 +5879,7 @@ struct ContextMultiDeviceMock : public L0::ContextImp { alignedFree(const_cast(ptr)); return ZE_RESULT_SUCCESS; } - bool isShareableMemory(const void *pNext, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) override { + bool isShareableMemory(const void *pNext, bool exportableMemory, NEO::Device *neoDevice) override { return true; } }; diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory_pooling.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory_pooling.cpp index 9cb00deb21..a3180bba75 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory_pooling.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory_pooling.cpp @@ -30,7 +30,6 @@ struct AllocUsmPoolMemoryTest : public ::testing::Test { NEO::debugManager.flags.EnableHostUsmAllocationPool.set(hostUsmPoolFlag); NEO::debugManager.flags.EnableDeviceUsmAllocationPool.set(deviceUsmPoolFlag); NEO::debugManager.flags.ExperimentalUSMAllocationReuseVersion.set(poolingVersionFlag); - NEO::debugManager.flags.EnableShareableWithoutNTHandle.set(false); executionEnvironment = new NEO::ExecutionEnvironment(); executionEnvironment->prepareRootDeviceEnvironments(numRootDevices); diff --git a/shared/source/debug_settings/debug_variables_base.inl b/shared/source/debug_settings/debug_variables_base.inl index 8cca255590..f921f12fe5 100644 --- a/shared/source/debug_settings/debug_variables_base.inl +++ b/shared/source/debug_settings/debug_variables_base.inl @@ -78,7 +78,6 @@ DECLARE_DEBUG_VARIABLE(bool, DisableForceToStateless, false, "Do not force state DECLARE_DEBUG_VARIABLE(bool, ForceTheoreticalMaxWorkGroupCount, false, "Do not apply any limitation to max cooperative/concurrent work-group count queries") DECLARE_DEBUG_VARIABLE(bool, AppendMemoryPrefetchForKmdMigratedSharedAllocations, true, "Allow prefetching shared memory to the device associated with the specified command list") DECLARE_DEBUG_VARIABLE(bool, ForceMemoryPrefetchForKmdMigratedSharedAllocations, false, "Force prefetch of shared memory in command queue execute command lists") -DECLARE_DEBUG_VARIABLE(bool, EnableShareableWithoutNTHandle, true, "Enable creating shareable allocations without NT handle on Windows") DECLARE_DEBUG_VARIABLE(bool, ClKhrExternalMemoryExtension, true, "Enable cl_khr_external_memory extension") DECLARE_DEBUG_VARIABLE(bool, WaitForMemoryRelease, false, "Wait for memory release when out of memory") DECLARE_DEBUG_VARIABLE(bool, RemoveRestrictionsOnNumberOfThreadsInGpgpuThreadGroup, 0, "0 - default disabled, 1- remove restrictions on NumberOfThreadsInGpgpuThreadGroup in INTERFACE_DESCRIPTOR_DATA") diff --git a/shared/source/memory_manager/allocation_properties.h b/shared/source/memory_manager/allocation_properties.h index 43c39936f5..00274d590e 100644 --- a/shared/source/memory_manager/allocation_properties.h +++ b/shared/source/memory_manager/allocation_properties.h @@ -28,8 +28,7 @@ struct AllocationProperties { uint32_t forceSystemMemory : 1; uint32_t preferCompressed : 1; uint32_t cantBeReadOnly : 1; - uint32_t shareableWithoutNTHandle : 1; - uint32_t reserved : 16; + uint32_t reserved : 18; } flags; uint32_t allFlags = 0; }; @@ -113,8 +112,7 @@ struct AllocationData { uint32_t isUSMDeviceMemory : 1; uint32_t zeroMemory : 1; uint32_t cantBeReadOnly : 1; - uint32_t shareableWithoutNTHandle : 1; - uint32_t reserved : 14; + uint32_t reserved : 15; } flags; uint32_t allFlags = 0; }; diff --git a/shared/source/memory_manager/memory_manager.cpp b/shared/source/memory_manager/memory_manager.cpp index f3447c7056..9e246e9b2e 100644 --- a/shared/source/memory_manager/memory_manager.cpp +++ b/shared/source/memory_manager/memory_manager.cpp @@ -643,7 +643,6 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo allocationData.forceKMDAllocation = properties.forceKMDAllocation; allocationData.makeGPUVaDifferentThanCPUPtr = properties.makeGPUVaDifferentThanCPUPtr; allocationData.flags.shareable = properties.flags.shareable; - allocationData.flags.shareableWithoutNTHandle = properties.flags.shareableWithoutNTHandle; allocationData.flags.isUSMDeviceMemory = properties.flags.isUSMDeviceAllocation; allocationData.flags.requiresCpuAccess = GraphicsAllocation::isCpuAccessRequired(properties.allocationType); allocationData.flags.allocateMemory = properties.flags.allocateMemory; diff --git a/shared/source/memory_manager/memory_manager.h b/shared/source/memory_manager/memory_manager.h index 73c7158777..f12185d3b5 100644 --- a/shared/source/memory_manager/memory_manager.h +++ b/shared/source/memory_manager/memory_manager.h @@ -111,7 +111,6 @@ class MemoryManager { struct OsHandleData { osHandle handle; uint32_t arrayIndex; - uint32_t parentProcessId = 0; OsHandleData(uint64_t handle, uint32_t arrayIndex = 0) : handle(static_cast(handle)), arrayIndex(arrayIndex){}; OsHandleData(void *handle, uint32_t arrayIndex = 0) : handle(toOsHandle(handle)), arrayIndex(arrayIndex){}; diff --git a/shared/source/memory_manager/unified_memory_manager.cpp b/shared/source/memory_manager/unified_memory_manager.cpp index 96a0a35508..611f814cf3 100644 --- a/shared/source/memory_manager/unified_memory_manager.cpp +++ b/shared/source/memory_manager/unified_memory_manager.cpp @@ -506,7 +506,6 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size, unifiedMemoryProperties.alignment = alignUpNonZero(memoryProperties.alignment, pageSizeForAlignment); unifiedMemoryProperties.flags.isUSMDeviceAllocation = false; unifiedMemoryProperties.flags.shareable = memoryProperties.allocationFlags.flags.shareable; - unifiedMemoryProperties.flags.shareableWithoutNTHandle = memoryProperties.allocationFlags.flags.shareableWithoutNTHandle; unifiedMemoryProperties.cacheRegion = MemoryPropertiesHelper::getCacheRegion(memoryProperties.allocationFlags); unifiedMemoryProperties.flags.uncacheable = memoryProperties.allocationFlags.flags.locallyUncachedResource; unifiedMemoryProperties.flags.preferCompressed = compressionEnabled || memoryProperties.allocationFlags.flags.compressedHint; diff --git a/shared/source/memory_properties/memory_properties_flags_common.inl b/shared/source/memory_properties/memory_properties_flags_common.inl index ee72b4509e..3a9b9b96f1 100644 --- a/shared/source/memory_properties/memory_properties_flags_common.inl +++ b/shared/source/memory_properties/memory_properties_flags_common.inl @@ -30,7 +30,6 @@ struct MemoryFlags { uint32_t resource48Bit : 1; uint32_t compressedHint : 1; uint32_t uncompressedHint : 1; - uint32_t shareableWithoutNTHandle : 1; }; struct MemoryAllocFlags { diff --git a/shared/source/os_interface/linux/sys_calls.h b/shared/source/os_interface/linux/sys_calls.h index d69cdfb4a7..c8ea7f3d8d 100644 --- a/shared/source/os_interface/linux/sys_calls.h +++ b/shared/source/os_interface/linux/sys_calls.h @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -60,8 +59,6 @@ long sysconf(int name); int mkfifo(const char *pathname, mode_t mode); int pidfdopen(pid_t pid, unsigned int flags); int pidfdgetfd(int pidfd, int targetfd, unsigned int flags); -int prctl(int option, unsigned long arg); char **getEnviron(); - } // namespace SysCalls } // namespace NEO diff --git a/shared/source/os_interface/linux/sys_calls_linux.cpp b/shared/source/os_interface/linux/sys_calls_linux.cpp index 869912521c..184c147e0e 100644 --- a/shared/source/os_interface/linux/sys_calls_linux.cpp +++ b/shared/source/os_interface/linux/sys_calls_linux.cpp @@ -205,10 +205,6 @@ int pidfdgetfd(int pidfd, int targetfd, unsigned int flags) { return static_cast(retval); } -int prctl(int option, unsigned long arg) { - return ::prctl(option, arg); -} - off_t lseek(int fd, off_t offset, int whence) noexcept { return ::lseek(fd, offset, whence); } diff --git a/shared/source/os_interface/windows/sys_calls.cpp b/shared/source/os_interface/windows/sys_calls.cpp index 03b12ea866..aede89ec3b 100644 --- a/shared/source/os_interface/windows/sys_calls.cpp +++ b/shared/source/os_interface/windows/sys_calls.cpp @@ -36,14 +36,6 @@ unsigned int getCurrentProcessId() { return GetCurrentProcessId(); } -BOOL duplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions) { - return DuplicateHandle(hSourceProcessHandle, hSourceHandle, hTargetProcessHandle, lpTargetHandle, dwDesiredAccess, bInheritHandle, dwOptions); -} - -HANDLE openProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) { - return OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); -} - unsigned long getNumThreads() { return 1; } diff --git a/shared/source/os_interface/windows/sys_calls.h b/shared/source/os_interface/windows/sys_calls.h index acc450cbee..fd43392efd 100644 --- a/shared/source/os_interface/windows/sys_calls.h +++ b/shared/source/os_interface/windows/sys_calls.h @@ -45,8 +45,6 @@ BOOL findNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData); BOOL findClose(HANDLE hFindFile); DWORD getFileAttributesA(LPCSTR lpFileName); DWORD setFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod); -BOOL duplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions); -HANDLE openProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId); void setProcessPowerThrottlingState(ProcessPowerThrottlingState state); void setThreadPriority(ThreadPriority priority); diff --git a/shared/source/os_interface/windows/wddm/helper_drm_or_wddm.cpp b/shared/source/os_interface/windows/wddm/helper_drm_or_wddm.cpp index 5fa706db20..974c636d7a 100644 --- a/shared/source/os_interface/windows/wddm/helper_drm_or_wddm.cpp +++ b/shared/source/os_interface/windows/wddm/helper_drm_or_wddm.cpp @@ -18,8 +18,4 @@ bool Wddm::getReadOnlyFlagValue(const void *cpuPtr) const { bool Wddm::isReadOnlyFlagFallbackSupported() const { return false; } -HANDLE Wddm::getSharedHandle(const MemoryManager::OsHandleData &osHandleData) { - HANDLE sharedNtHandle = reinterpret_cast(static_cast(osHandleData.handle)); - return sharedNtHandle; -} } // namespace NEO \ No newline at end of file diff --git a/shared/source/os_interface/windows/wddm/helper_wddm.cpp b/shared/source/os_interface/windows/wddm/helper_wddm.cpp index 29f93cfb4c..48ee720223 100644 --- a/shared/source/os_interface/windows/wddm/helper_wddm.cpp +++ b/shared/source/os_interface/windows/wddm/helper_wddm.cpp @@ -6,7 +6,6 @@ */ #include "shared/source/os_interface/windows/gdi_interface.h" -#include "shared/source/os_interface/windows/sys_calls.h" #include "shared/source/os_interface/windows/wddm/wddm.h" namespace NEO { @@ -24,41 +23,4 @@ bool Wddm::getReadOnlyFlagValue(const void *cpuPtr) const { bool Wddm::isReadOnlyFlagFallbackSupported() const { return true; } - -HANDLE Wddm::getSharedHandle(const MemoryManager::OsHandleData &osHandleData) { - HANDLE sharedNtHandle = reinterpret_cast(static_cast(osHandleData.handle)); - if (osHandleData.parentProcessId != 0) { - // Open the parent process handle with required access rights - HANDLE parentProcessHandle = NEO::SysCalls::openProcess(PROCESS_DUP_HANDLE, FALSE, static_cast(osHandleData.parentProcessId)); - if (parentProcessHandle == nullptr) { - DEBUG_BREAK_IF(true); - return sharedNtHandle; - } - - // Duplicate the handle from the parent process to the current process - // This is necessary to ensure that the handle can be used in the current process context - // We use GENERIC_READ | GENERIC_WRITE to ensure we can perform operations on the handle - HANDLE duplicatedHandle = nullptr; - BOOL duplicateResult = NEO::SysCalls::duplicateHandle( - parentProcessHandle, - reinterpret_cast(static_cast(osHandleData.handle)), - GetCurrentProcess(), - &duplicatedHandle, - GENERIC_READ | GENERIC_WRITE, - FALSE, - 0); - - // Close the parent process handle as we no longer need it - // The duplicated handle will be used for further operations - NEO::SysCalls::closeHandle(parentProcessHandle); - - if (!duplicateResult) { - DEBUG_BREAK_IF(true); - return sharedNtHandle; - } - sharedNtHandle = duplicatedHandle; - } - return sharedNtHandle; -} - } // namespace NEO diff --git a/shared/source/os_interface/windows/wddm/wddm.cpp b/shared/source/os_interface/windows/wddm/wddm.cpp index 50a8832f92..7a3e23542c 100644 --- a/shared/source/os_interface/windows/wddm/wddm.cpp +++ b/shared/source/os_interface/windows/wddm/wddm.cpp @@ -664,10 +664,6 @@ bool Wddm::isReadOnlyFlagFallbackAvailable(const D3DKMT_CREATEALLOCATION &create } NTSTATUS Wddm::createAllocation(const void *cpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle) { - return createAllocation(cpuPtr, gmm, outHandle, outResourceHandle, outSharedHandle, true); -} - -NTSTATUS Wddm::createAllocation(const void *cpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle, bool createNTHandle) { NTSTATUS status = STATUS_UNSUCCESSFUL; D3DDDI_ALLOCATIONINFO2 allocationInfo = {}; D3DKMT_CREATEALLOCATION createAllocation = {}; @@ -708,7 +704,7 @@ NTSTATUS Wddm::createAllocation(const void *cpuPtr, const Gmm *gmm, D3DKMT_HANDL outHandle = allocationInfo.hAllocation; outResourceHandle = createAllocation.hResource; - if (outSharedHandle && createNTHandle) { + if (outSharedHandle) { HANDLE ntSharedHandle = NULL; status = this->createNTHandle(&outResourceHandle, &ntSharedHandle); if (status != STATUS_SUCCESS) { @@ -926,8 +922,7 @@ bool Wddm::verifyNTHandle(HANDLE handle) { bool Wddm::openNTHandle(const MemoryManager::OsHandleData &osHandleData, WddmAllocation *alloc) { D3DKMT_QUERYRESOURCEINFOFROMNTHANDLE queryResourceInfoFromNtHandle = {}; queryResourceInfoFromNtHandle.hDevice = device; - HANDLE sharedNtHandle = this->getSharedHandle(osHandleData); - queryResourceInfoFromNtHandle.hNtHandle = sharedNtHandle; + queryResourceInfoFromNtHandle.hNtHandle = reinterpret_cast(static_cast(osHandleData.handle)); [[maybe_unused]] auto status = getGdi()->queryResourceInfoFromNtHandle(&queryResourceInfoFromNtHandle); DEBUG_BREAK_IF(status != STATUS_SUCCESS); @@ -943,7 +938,7 @@ bool Wddm::openNTHandle(const MemoryManager::OsHandleData &osHandleData, WddmAll D3DKMT_OPENRESOURCEFROMNTHANDLE openResourceFromNtHandle = {}; openResourceFromNtHandle.hDevice = device; - openResourceFromNtHandle.hNtHandle = sharedNtHandle; + openResourceFromNtHandle.hNtHandle = reinterpret_cast(static_cast(osHandleData.handle)); openResourceFromNtHandle.NumAllocations = queryResourceInfoFromNtHandle.NumAllocations; openResourceFromNtHandle.pOpenAllocationInfo2 = allocationInfo2.get(); openResourceFromNtHandle.pTotalPrivateDriverDataBuffer = allocPrivateData.get(); diff --git a/shared/source/os_interface/windows/wddm/wddm.h b/shared/source/os_interface/windows/wddm/wddm.h index 920e84291d..3aec3d443f 100644 --- a/shared/source/os_interface/windows/wddm/wddm.h +++ b/shared/source/os_interface/windows/wddm/wddm.h @@ -74,7 +74,6 @@ class Wddm : public DriverModel { MOCKABLE_VIRTUAL uint64_t freeGmmGpuVirtualAddress(Gmm *gmm, D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size); MOCKABLE_VIRTUAL bool freeGpuVirtualAddress(D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size); MOCKABLE_VIRTUAL NTSTATUS createAllocation(const void *cpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle); - MOCKABLE_VIRTUAL NTSTATUS createAllocation(const void *cpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle, bool createNTHandle); MOCKABLE_VIRTUAL bool createAllocation(const Gmm *gmm, D3DKMT_HANDLE &outHandle); MOCKABLE_VIRTUAL NTSTATUS createAllocationsAndMapGpuVa(OsHandleStorage &osHandles); MOCKABLE_VIRTUAL bool destroyAllocations(const D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle); @@ -92,7 +91,6 @@ class Wddm : public DriverModel { MOCKABLE_VIRTUAL bool destroyContext(D3DKMT_HANDLE context); MOCKABLE_VIRTUAL bool queryAdapterInfo(); MOCKABLE_VIRTUAL NTSTATUS createNTHandle(const D3DKMT_HANDLE *resourceHandle, HANDLE *ntHandle); - MOCKABLE_VIRTUAL HANDLE getSharedHandle(const MemoryManager::OsHandleData &osHandleData); MOCKABLE_VIRTUAL bool submit(uint64_t commandBuffer, size_t size, void *commandHeader, WddmSubmitArguments &submitArguments); MOCKABLE_VIRTUAL bool waitFromCpu(uint64_t lastFenceValue, const MonitoredFence &monitoredFence, bool busyWait); diff --git a/shared/source/os_interface/windows/wddm_allocation.h b/shared/source/os_interface/windows/wddm_allocation.h index 2623957d9d..e3aa9f060f 100644 --- a/shared/source/os_interface/windows/wddm_allocation.h +++ b/shared/source/os_interface/windows/wddm_allocation.h @@ -90,9 +90,6 @@ class WddmAllocation : public GraphicsAllocation { void setMakeResidentBeforeLockRequired(bool makeResidentBeforeLockRequired) { this->makeResidentBeforeLockRequired = makeResidentBeforeLockRequired; } bool isAllocInFrontWindowPool() const { return allocInFrontWindowPool; } void setAllocInFrontWindowPool(bool allocInFrontWindowPool) { this->allocInFrontWindowPool = allocInFrontWindowPool; } - bool isShareable() const { return shareable; } - bool isShareableWithoutNTHandle() const { return shareableWithoutNTHandle; } - void setShareableWithoutNTHandle(bool shareableWithoutNTHandle) { this->shareableWithoutNTHandle = shareableWithoutNTHandle; } bool isPhysicalMemoryReservation() const { return physicalMemoryReservation; } void setPhysicalMemoryReservation(bool physicalMemoryReservation) { this->physicalMemoryReservation = physicalMemoryReservation; } bool isMappedPhysicalMemoryReservation() const { return mappedPhysicalMemoryReservation; } @@ -128,6 +125,5 @@ class WddmAllocation : public GraphicsAllocation { bool physicalMemoryReservation = false; bool mappedPhysicalMemoryReservation = false; bool makeResidentBeforeLockRequired = false; - bool shareableWithoutNTHandle = false; }; } // namespace NEO diff --git a/shared/source/os_interface/windows/wddm_memory_manager.cpp b/shared/source/os_interface/windows/wddm_memory_manager.cpp index c353eaf029..5dfa5d506c 100644 --- a/shared/source/os_interface/windows/wddm_memory_manager.cpp +++ b/shared/source/os_interface/windows/wddm_memory_manager.cpp @@ -147,7 +147,6 @@ GraphicsAllocation *WddmMemoryManager::allocatePhysicalDeviceMemory(const Alloca 1u, // numGmms allocationData.type, nullptr, 0, allocationData.size, nullptr, MemoryPool::systemCpuInaccessible, allocationData.flags.shareable, maxOsContextCount); - allocation->setShareableWithoutNTHandle(allocationData.flags.shareableWithoutNTHandle); allocation->setDefaultGmm(gmm.get()); if (!createPhysicalAllocation(allocation.get())) { return nullptr; @@ -176,7 +175,6 @@ GraphicsAllocation *WddmMemoryManager::allocateMemoryByKMD(const AllocationData 1u, // numGmms allocationData.type, nullptr, 0, allocationData.size, nullptr, MemoryPool::systemCpuInaccessible, allocationData.flags.shareable, maxOsContextCount); - allocation->setShareableWithoutNTHandle(allocationData.flags.shareableWithoutNTHandle); allocation->setDefaultGmm(gmm.get()); void *requiredGpuVa = nullptr; adjustGpuPtrToHostAddressSpace(*allocation.get(), requiredGpuVa); @@ -618,6 +616,7 @@ bool WddmMemoryManager::isNTHandle(osHandle handle, uint32_t rootDeviceIndex) { GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) { auto allocation = std::make_unique(properties.rootDeviceIndex, 1u /*num gmms*/, properties.allocationType, nullptr, 0, osHandleData.handle, MemoryPool::systemCpuInaccessible, maxOsContextCount, 0llu); + bool status; if (verifyHandle(osHandleData.handle, properties.rootDeviceIndex, false)) status = getWddm(properties.rootDeviceIndex).openSharedHandle(osHandleData, allocation.get()); @@ -1132,11 +1131,10 @@ bool WddmMemoryManager::mapMultiHandleAllocationWithRetry(WddmAllocation *alloca bool WddmMemoryManager::createGpuAllocationsWithRetry(WddmAllocation *allocation) { for (auto handleId = 0u; handleId < allocation->getNumGmms(); handleId++) { auto gmm = allocation->getGmm(handleId); - bool createNTHandle = allocation->isShareable() && !allocation->isShareableWithoutNTHandle(); - auto status = getWddm(allocation->getRootDeviceIndex()).createAllocation(allocation->getUnderlyingBuffer(), gmm, allocation->getHandleToModify(handleId), allocation->getResourceHandleToModify(), allocation->getSharedHandleToModify(), createNTHandle); + auto status = getWddm(allocation->getRootDeviceIndex()).createAllocation(allocation->getUnderlyingBuffer(), gmm, allocation->getHandleToModify(handleId), allocation->getResourceHandleToModify(), allocation->getSharedHandleToModify()); if (status == STATUS_GRAPHICS_NO_VIDEO_MEMORY && deferredDeleter) { deferredDeleter->drain(true, false); - status = getWddm(allocation->getRootDeviceIndex()).createAllocation(allocation->getUnderlyingBuffer(), gmm, allocation->getHandleToModify(handleId), allocation->getResourceHandleToModify(), allocation->getSharedHandleToModify(), createNTHandle); + status = getWddm(allocation->getRootDeviceIndex()).createAllocation(allocation->getUnderlyingBuffer(), gmm, allocation->getHandleToModify(handleId), allocation->getResourceHandleToModify(), allocation->getSharedHandleToModify()); } if (status != STATUS_SUCCESS) { getWddm(allocation->getRootDeviceIndex()).destroyAllocations(&allocation->getHandles()[0], handleId, allocation->getResourceHandle()); @@ -1370,7 +1368,6 @@ GraphicsAllocation *WddmMemoryManager::allocatePhysicalLocalDeviceMemory(const A auto wddmAllocation = std::make_unique(allocationData.rootDeviceIndex, singleBankAllocation ? numGmms : numBanks, allocationData.type, nullptr, 0, sizeAligned, nullptr, MemoryPool::localMemory, allocationData.flags.shareable, maxOsContextCount); - wddmAllocation->setShareableWithoutNTHandle(allocationData.flags.shareableWithoutNTHandle); if (singleBankAllocation) { if (numGmms > 1) { splitGmmsInAllocation(gmmHelper, wddmAllocation.get(), alignment, chunkSize, const_cast(allocationData.storageInfo)); @@ -1466,7 +1463,6 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryInDevicePool(const auto wddmAllocation = std::make_unique(allocationData.rootDeviceIndex, singleBankAllocation ? numGmms : numBanks, allocationData.type, nullptr, 0, sizeAligned, nullptr, MemoryPool::localMemory, allocationData.flags.shareable, maxOsContextCount); - wddmAllocation->setShareableWithoutNTHandle(allocationData.flags.shareableWithoutNTHandle); if (singleBankAllocation) { if (numGmms > 1) { splitGmmsInAllocation(gmmHelper, wddmAllocation.get(), alignment, chunkSize, const_cast(allocationData.storageInfo)); diff --git a/shared/test/common/mocks/mock_wddm.cpp b/shared/test/common/mocks/mock_wddm.cpp index e56bcb6b13..f35b3d37f0 100644 --- a/shared/test/common/mocks/mock_wddm.cpp +++ b/shared/test/common/mocks/mock_wddm.cpp @@ -141,30 +141,6 @@ NTSTATUS WddmMock::createAllocation(const void *alignedCpuPtr, const Gmm *gmm, D return createAllocationStatus; } -NTSTATUS WddmMock::createAllocation(const void *alignedCpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResourceHandle, uint64_t *outSharedHandle, bool createNTHandle) { - createAllocationResult.called++; - if (failCreateAllocation) { - return STATUS_NO_MEMORY; - } - if (callBaseDestroyAllocations) { - createAllocationStatus = Wddm::createAllocation(alignedCpuPtr, gmm, outHandle, outResourceHandle, outSharedHandle, createNTHandle); - createAllocationResult.success = createAllocationStatus == STATUS_SUCCESS; - if (createAllocationStatus != STATUS_SUCCESS) { - destroyAllocationResult.called++; - } - } else { - createAllocationResult.success = true; - outHandle = ALLOCATION_HANDLE; - outResourceHandle = ALLOCATION_HANDLE; - if (outSharedHandle && !createNTHandle) { - // For shared allocations without NT handle, set a special value - *outSharedHandle = 1u; - } - return createAllocationStatus; - } - return createAllocationStatus; -} - bool WddmMock::createAllocation64k(WddmAllocation *wddmAllocation) { if (wddmAllocation) { return createAllocation(wddmAllocation->getDefaultGmm(), wddmAllocation->getHandleToModify(0u)); diff --git a/shared/test/common/mocks/mock_wddm.h b/shared/test/common/mocks/mock_wddm.h index ed9babbe00..79cb8abb94 100644 --- a/shared/test/common/mocks/mock_wddm.h +++ b/shared/test/common/mocks/mock_wddm.h @@ -82,7 +82,6 @@ class WddmMock : public Wddm { bool mapGpuVirtualAddress(WddmAllocation *allocation); bool freeGpuVirtualAddress(D3DGPU_VIRTUAL_ADDRESS &gpuPtr, uint64_t size) override; NTSTATUS createAllocation(const void *alignedCpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResource, uint64_t *outSharedHandle) override; - NTSTATUS createAllocation(const void *alignedCpuPtr, const Gmm *gmm, D3DKMT_HANDLE &outHandle, D3DKMT_HANDLE &outResource, uint64_t *outSharedHandle, bool createNTHandle) override; bool createAllocation(const Gmm *gmm, D3DKMT_HANDLE &outHandle) override; bool destroyAllocations(const D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle) override; diff --git a/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp b/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp index e285474c4e..ed1cc63e62 100644 --- a/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp +++ b/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp @@ -68,7 +68,6 @@ int readdirCalled = 0; int closedirCalled = 0; int pidfdopenCalled = 0; int pidfdgetfdCalled = 0; -int prctlCalled = 0; int fsyncCalled = 0; int fsyncArgPassed = 0; int fsyncRetVal = 0; @@ -117,7 +116,6 @@ int (*sysCallsClosedir)(DIR *dir) = nullptr; int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize) = nullptr; int (*sysCallsPidfdOpen)(pid_t pid, unsigned int flags) = nullptr; int (*sysCallsPidfdGetfd)(int pidfd, int fd, unsigned int flags) = nullptr; -int (*sysCallsPrctl)(int option, unsigned long arg) = nullptr; off_t lseekReturn = 4096u; std::atomic lseekCalledCount(0); long sysconfReturn = 1ull << 30; @@ -557,15 +555,6 @@ int pidfdgetfd(int pid, int targetfd, unsigned int flags) { } return 0; } - -int prctl(int option, unsigned long arg) { - prctlCalled++; - if (sysCallsPrctl != nullptr) { - return sysCallsPrctl(option, arg); - } - return 0; -} - char **getEnviron() { return NEO::ULT::getCurrentEnviron(); } diff --git a/shared/test/common/os_interface/linux/sys_calls_linux_ult.h b/shared/test/common/os_interface/linux/sys_calls_linux_ult.h index 2dba705c1a..8e709731ff 100644 --- a/shared/test/common/os_interface/linux/sys_calls_linux_ult.h +++ b/shared/test/common/os_interface/linux/sys_calls_linux_ult.h @@ -57,7 +57,6 @@ extern int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize); extern int (*sysCallsClose)(int fileDescriptor); extern int (*sysCallsPidfdOpen)(pid_t pid, unsigned int flags); extern int (*sysCallsPidfdGetfd)(int pidfd, int fd, unsigned int flags); -extern int (*sysCallsPrctl)(int option, unsigned long arg); extern bool allowFakeDevicePath; extern int flockRetVal; @@ -94,7 +93,6 @@ extern bool failAccess; extern int setErrno; extern int pidfdopenCalled; extern int pidfdgetfdCalled; -extern int prctlCalled; extern std::vector mmapVector; extern std::vector mmapCapturedExtendedPointers; diff --git a/shared/test/common/os_interface/windows/mock_sys_calls.h b/shared/test/common/os_interface/windows/mock_sys_calls.h index 4d62ca688d..0abc01ab04 100644 --- a/shared/test/common/os_interface/windows/mock_sys_calls.h +++ b/shared/test/common/os_interface/windows/mock_sys_calls.h @@ -67,8 +67,6 @@ extern CONFIGRET (*sysCallsCmGetDeviceInterfaceListSize)(PULONG pulLen, LPGUID i extern CONFIGRET (*sysCallsCmGetDeviceInterfaceList)(LPGUID interfaceClassGuid, DEVINSTID_W pDeviceID, PZZWSTR buffer, ULONG bufferLen, ULONG ulFlags); extern LPVOID (*sysCallsHeapAlloc)(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes); extern BOOL (*sysCallsHeapFree)(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem); -extern BOOL (*sysCallsDuplicateHandle)(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions); -extern HANDLE (*sysCallsOpenProcess)(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId); extern BOOL (*sysCallsGetModuleHandleExW)(DWORD dwFlags, LPCWSTR lpModuleName, HMODULE *phModule); extern DWORD (*sysCallsGetModuleFileNameW)(HMODULE hModule, LPWSTR lpFilename, DWORD nSize); diff --git a/shared/test/common/os_interface/windows/sys_calls.cpp b/shared/test/common/os_interface/windows/sys_calls.cpp index 2c48b54294..4f04619f2b 100644 --- a/shared/test/common/os_interface/windows/sys_calls.cpp +++ b/shared/test/common/os_interface/windows/sys_calls.cpp @@ -120,12 +120,6 @@ LPVOID(*sysCallsHeapAlloc) BOOL(*sysCallsHeapFree) (HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) = nullptr; -BOOL(*sysCallsDuplicateHandle) -(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions) = nullptr; - -HANDLE(*sysCallsOpenProcess) -(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) = nullptr; - void exit(int code) { } @@ -350,20 +344,6 @@ BOOL heapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) { return HeapFree(hHeap, dwFlags, lpMem); } -BOOL duplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions) { - if (sysCallsDuplicateHandle != nullptr) { - return sysCallsDuplicateHandle(hSourceProcessHandle, hSourceHandle, hTargetProcessHandle, lpTargetHandle, dwDesiredAccess, bInheritHandle, dwOptions); - } - return FALSE; -} - -HANDLE openProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) { - if (sysCallsOpenProcess != nullptr) { - return sysCallsOpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); - } - return nullptr; -} - LSTATUS regOpenKeyExA(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult) { if (regOpenKeySuccessCount > 0) { regOpenKeySuccessCount--; diff --git a/shared/test/common/test_files/igdrcl.config b/shared/test/common/test_files/igdrcl.config index 88a9c40660..cb52a67974 100644 --- a/shared/test/common/test_files/igdrcl.config +++ b/shared/test/common/test_files/igdrcl.config @@ -666,5 +666,4 @@ SplitBcsAggregatedEventsMode = -1 SplitBcsRequiredTileCount = -1 SplitBcsRequiredEnginesCount = -1 SplitBcsTransferDirectionMask = -1 -EnableShareableWithoutNTHandle = -1 # Please don't edit below this line diff --git a/shared/test/unit_test/os_interface/windows/wddm_shared_allocations_test.cpp b/shared/test/unit_test/os_interface/windows/wddm_shared_allocations_test.cpp index 4992f407cd..fca10cdc1d 100644 --- a/shared/test/unit_test/os_interface/windows/wddm_shared_allocations_test.cpp +++ b/shared/test/unit_test/os_interface/windows/wddm_shared_allocations_test.cpp @@ -73,44 +73,7 @@ TEST_F(WdmmSharedTests, WhenCreatingSharedAllocationAndGetNTHandleFailedThenAllo Gmm gmm(executionEnvironment->rootDeviceEnvironments[0]->getGmmHelper(), nullptr, 20, 0, GMM_RESOURCE_USAGE_OCL_BUFFER, {}, gmmRequirements); EXPECT_NE(STATUS_SUCCESS, wddm->createAllocation(nullptr, &gmm, handle, resourceHandle, &ntHandle)); - EXPECT_EQ(wddm->destroyAllocationResult.called++, 2u); - EXPECT_EQ(handle, 0u); - EXPECT_EQ(resourceHandle, 0u); -} - -TEST_F(WdmmSharedTests, WhenCreatingSharedAllocationWithShareableWithoutNTHandleFlagThenNTHandleIsNotCreated) { - init(); - - D3DKMT_HANDLE handle = 32u; - D3DKMT_HANDLE resourceHandle = 32u; - uint64_t ntHandle = 0u; - GmmRequirements gmmRequirements{}; - gmmRequirements.allowLargePages = true; - gmmRequirements.preferCompressed = true; - Gmm gmm(executionEnvironment->rootDeviceEnvironments[0]->getGmmHelper(), nullptr, 20, 0, GMM_RESOURCE_USAGE_OCL_BUFFER, {}, gmmRequirements); - - // Test with createNTHandle = false (shareableWithoutNTHandle = true) - EXPECT_EQ(STATUS_SUCCESS, wddm->createAllocation(nullptr, &gmm, handle, resourceHandle, &ntHandle, false)); - EXPECT_NE(handle, 0u); - EXPECT_NE(resourceHandle, 0u); - EXPECT_EQ(ntHandle, 0u); // Should be set to 1 to indicate shared resource without NT handle - EXPECT_EQ(wddm->destroyAllocationResult.called, 0u); -} - -TEST_F(WdmmSharedTests, WhenCreatingSharedAllocationWithNormalShareableFlagThenNTHandleCreationIsAttempted) { - init(); - - D3DKMT_HANDLE handle = 32u; - D3DKMT_HANDLE resourceHandle = 32u; - uint64_t ntHandle = 0u; - GmmRequirements gmmRequirements{}; - gmmRequirements.allowLargePages = true; - gmmRequirements.preferCompressed = true; - Gmm gmm(executionEnvironment->rootDeviceEnvironments[0]->getGmmHelper(), nullptr, 20, 0, GMM_RESOURCE_USAGE_OCL_BUFFER, {}, gmmRequirements); - - // Test with createNTHandle = true (shareableWithoutNTHandle = false) - EXPECT_NE(STATUS_SUCCESS, wddm->createAllocation(nullptr, &gmm, handle, resourceHandle, &ntHandle, true)); - EXPECT_EQ(wddm->destroyAllocationResult.called, 2u); // Should be called because NT handle creation failed + EXPECT_EQ(wddm->destroyAllocationResult.called++, 1u); EXPECT_EQ(handle, 0u); EXPECT_EQ(resourceHandle, 0u); } diff --git a/shared/test/unit_test/os_interface/windows/wddm_tests.cpp b/shared/test/unit_test/os_interface/windows/wddm_tests.cpp index 1db7d07540..1e394a7bbc 100644 --- a/shared/test/unit_test/os_interface/windows/wddm_tests.cpp +++ b/shared/test/unit_test/os_interface/windows/wddm_tests.cpp @@ -1200,15 +1200,6 @@ TEST_F(WddmTests, givenPageAlignedReadOnlyMemoryPassedToCreateAllocationsAndMapG EXPECT_FALSE(readWriteExistingSysMemSupportedForTest); } -TEST_F(WddmTests, givenOsHandleDataWithoutOpaqueInformationWhenGettingSharedHandleThenReturnOriginalHandle) { - uint64_t originalHandle = 0x12345678; - MemoryManager::OsHandleData osHandleData(originalHandle); - - HANDLE sharedHandle = wddm->getSharedHandle(osHandleData); - - EXPECT_EQ(reinterpret_cast(static_cast(originalHandle)), sharedHandle); -} - TEST_F(WddmTests, whenThereIsNoExisitngSysMemoryThenReadOnlyFallbackIsNotAvailable) { D3DKMT_CREATEALLOCATION createAllocation{}; D3DDDI_ALLOCATIONINFO2 allocationInfo2{}; @@ -1240,156 +1231,4 @@ TEST_F(WddmTests, givenSysMemoryPointerAndReadOnlyFlagNotSetInCreateAllocationFl auto readOnlyFallbackSupported = wddm->isReadOnlyFlagFallbackSupported(); EXPECT_EQ(readOnlyFallbackSupported, wddm->isReadOnlyFlagFallbackAvailable(createAllocation)); } - -// Mock class for testing createNTHandle scenarios -class WddmCreateNTHandleMock : public WddmMock { - public: - using WddmMock::WddmMock; - - NTSTATUS createNTHandle(const D3DKMT_HANDLE *resourceHandle, HANDLE *ntHandle) override { - createNTHandleCalled = true; - lastResourceHandle = resourceHandle ? *resourceHandle : 0; - - if (shouldCreateNTHandleFail) { - return STATUS_UNSUCCESSFUL; - } - - *ntHandle = reinterpret_cast(0x12345678); - return STATUS_SUCCESS; - } - - bool createNTHandleCalled = false; - D3DKMT_HANDLE lastResourceHandle = 0; - bool shouldCreateNTHandleFail = false; -}; - -class WddmCreateAllocationNTHandleTests : public WddmTestWithMockGdiDll { - public: - void SetUp() override { - WddmTestWithMockGdiDll::SetUp(); - - // Replace the wddm with our mock - mockWddm = new WddmCreateNTHandleMock(*rootDeviceEnvironment); - auto wddmMockInterface = new WddmMockInterface20(*mockWddm); - mockWddm->wddmInterface.reset(wddmMockInterface); - rootDeviceEnvironment->osInterface = std::make_unique(); - rootDeviceEnvironment->osInterface->setDriverModel(std::unique_ptr(mockWddm)); - rootDeviceEnvironment->memoryOperationsInterface = std::make_unique(mockWddm); - - // Initialize the mock WDDM - mockWddm->init(); - } - - void initGmm() { - GmmRequirements gmmRequirements{}; - gmmRequirements.allowLargePages = true; - gmmRequirements.preferCompressed = true; - gmm = std::make_unique(executionEnvironment->rootDeviceEnvironments[0]->getGmmHelper(), - nullptr, 20, 0, GMM_RESOURCE_USAGE_OCL_BUFFER, StorageInfo{}, gmmRequirements); - } - - WddmCreateNTHandleMock *mockWddm = nullptr; - std::unique_ptr gmm; -}; - -TEST_F(WddmCreateAllocationNTHandleTests, givenOutSharedHandleAndCreateNTHandleTrueThenCreateNTHandleIsCalled) { - initGmm(); - - D3DKMT_HANDLE handle = 0; - D3DKMT_HANDLE resourceHandle = 0; - uint64_t sharedHandle = 0; - - // Test the condition: outSharedHandle && createNTHandle (both true) - auto result = mockWddm->createAllocation(nullptr, gmm.get(), handle, resourceHandle, &sharedHandle, true); - - EXPECT_EQ(STATUS_SUCCESS, result); - EXPECT_TRUE(mockWddm->createNTHandleCalled); - EXPECT_NE(0u, handle); - EXPECT_NE(0u, resourceHandle); - EXPECT_NE(0u, sharedHandle); - - // Cleanup - EXPECT_TRUE(mockWddm->destroyAllocations(&handle, 1, resourceHandle)); -} - -TEST_F(WddmCreateAllocationNTHandleTests, givenOutSharedHandleNullAndCreateNTHandleTrueThenCreateNTHandleIsNotCalled) { - initGmm(); - - D3DKMT_HANDLE handle = 0; - D3DKMT_HANDLE resourceHandle = 0; - - // Test the condition: outSharedHandle is null, createNTHandle is true - auto result = mockWddm->createAllocation(nullptr, gmm.get(), handle, resourceHandle, nullptr, true); - - EXPECT_EQ(STATUS_SUCCESS, result); - EXPECT_FALSE(mockWddm->createNTHandleCalled); - EXPECT_NE(0u, handle); - // When outSharedHandle is null, CreateResource flag is FALSE, so resourceHandle stays 0 - EXPECT_EQ(0u, resourceHandle); - - // Cleanup only the handle (no resource handle to clean up) - EXPECT_TRUE(mockWddm->destroyAllocations(&handle, 1, 0)); -} - -TEST_F(WddmCreateAllocationNTHandleTests, givenOutSharedHandleAndCreateNTHandleFalseThenCreateNTHandleIsNotCalled) { - initGmm(); - - D3DKMT_HANDLE handle = 0; - D3DKMT_HANDLE resourceHandle = 0; - uint64_t sharedHandle = 0; - - // Test the condition: outSharedHandle is valid, createNTHandle is false - auto result = mockWddm->createAllocation(nullptr, gmm.get(), handle, resourceHandle, &sharedHandle, false); - - EXPECT_EQ(STATUS_SUCCESS, result); - EXPECT_FALSE(mockWddm->createNTHandleCalled); - EXPECT_NE(0u, handle); - EXPECT_NE(0u, resourceHandle); - EXPECT_EQ(0u, sharedHandle); // Should remain 0 since no NT handle was created - - // Cleanup - EXPECT_TRUE(mockWddm->destroyAllocations(&handle, 1, resourceHandle)); -} - -TEST_F(WddmCreateAllocationNTHandleTests, givenCreateNTHandleFailsThenAllocationIsDestroyedAndHandlesAreReset) { - initGmm(); - - D3DKMT_HANDLE handle = 0; - D3DKMT_HANDLE resourceHandle = 0; - uint64_t sharedHandle = 0; - - // Set up the mock to fail createNTHandle - mockWddm->shouldCreateNTHandleFail = true; - - // Test the condition: outSharedHandle && createNTHandle (both true) but createNTHandle fails - auto result = mockWddm->createAllocation(nullptr, gmm.get(), handle, resourceHandle, &sharedHandle, true); - - EXPECT_NE(STATUS_SUCCESS, result); - EXPECT_TRUE(mockWddm->createNTHandleCalled); - EXPECT_EQ(0u, handle); // Should be reset to NULL_HANDLE - EXPECT_EQ(0u, resourceHandle); // Should be reset to NULL_HANDLE - // sharedHandle value is not modified on failure in this path -} - -TEST_F(WddmCreateAllocationNTHandleTests, givenCreateNTHandleSucceedsThenSharedHandleIsSet) { - initGmm(); - - D3DKMT_HANDLE handle = 0; - D3DKMT_HANDLE resourceHandle = 0; - uint64_t sharedHandle = 0; - - // Test successful createNTHandle path - auto result = mockWddm->createAllocation(nullptr, gmm.get(), handle, resourceHandle, &sharedHandle, true); - - EXPECT_EQ(STATUS_SUCCESS, result); - EXPECT_TRUE(mockWddm->createNTHandleCalled); - EXPECT_EQ(mockWddm->lastResourceHandle, resourceHandle); - EXPECT_NE(0u, handle); - EXPECT_NE(0u, resourceHandle); - EXPECT_EQ(0x12345678u, sharedHandle); // Should be set to the mock handle value - - // Cleanup - EXPECT_TRUE(mockWddm->destroyAllocations(&handle, 1, resourceHandle)); -} - } // namespace NEO diff --git a/shared/test/unit_test/os_interface/windows/wddm_windows_tests.cpp b/shared/test/unit_test/os_interface/windows/wddm_windows_tests.cpp index 12de8ecf45..c27b7b73f3 100644 --- a/shared/test/unit_test/os_interface/windows/wddm_windows_tests.cpp +++ b/shared/test/unit_test/os_interface/windows/wddm_windows_tests.cpp @@ -34,9 +34,6 @@ extern SysCalls::ProcessPowerThrottlingState setProcessPowerThrottlingStateLastV extern size_t setThreadPriorityCalled; extern SysCalls::ThreadPriority setThreadPriorityLastValue; extern MEMORY_BASIC_INFORMATION virtualQueryMemoryBasicInformation; -extern size_t closeHandleCalled; -extern BOOL (*sysCallsDuplicateHandle)(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions); -extern HANDLE (*sysCallsOpenProcess)(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId); } // namespace SysCalls extern uint32_t numRootDevicesToEnum; extern bool gCreateAllocation2FailOnReadOnlyAllocation; @@ -462,99 +459,3 @@ TEST_F(WddmTestWithMockGdiDll, whenGettingReadOnlyFlagThenReturnTrueOnlyForPageM TEST_F(WddmTestWithMockGdiDll, whenGettingReadOnlyFlagFallbackSupportThenTrueIsReturned) { EXPECT_TRUE(wddm->isReadOnlyFlagFallbackSupported()); } - -TEST_F(WddmTestWithMockGdiDll, givenOsHandleDataWithoutParentProcessWhenGettingSharedHandleThenReturnOriginalHandle) { - uint64_t originalHandle = 0x12345678; - MemoryManager::OsHandleData osHandleData(originalHandle); - - HANDLE sharedHandle = wddm->getSharedHandle(osHandleData); - - EXPECT_EQ(reinterpret_cast(static_cast(originalHandle)), sharedHandle); -} - -TEST_F(WddmTestWithMockGdiDll, givenOsHandleDataWithParentProcessWhenGettingSharedHandleThenDuplicateHandleFromParentProcess) { - uint64_t originalHandle = 0x12345678; - uint32_t parentProcessId = 1234; - MemoryManager::OsHandleData osHandleData(originalHandle); - osHandleData.parentProcessId = parentProcessId; - - HANDLE mockDuplicatedHandle = reinterpret_cast(0x8888); - - // Mock openProcess to return a valid handle - SysCalls::sysCallsOpenProcess = [](DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) -> HANDLE { - EXPECT_EQ(static_cast(PROCESS_DUP_HANDLE), dwDesiredAccess); - EXPECT_EQ(FALSE, bInheritHandle); - EXPECT_EQ(1234u, dwProcessId); - return reinterpret_cast(0x9999); - }; - - // Mock duplicateHandle to succeed - SysCalls::sysCallsDuplicateHandle = [](HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions) -> BOOL { - EXPECT_EQ(reinterpret_cast(0x9999), hSourceProcessHandle); - EXPECT_EQ(reinterpret_cast(static_cast(0x12345678)), hSourceHandle); - EXPECT_EQ(GetCurrentProcess(), hTargetProcessHandle); - EXPECT_EQ(GENERIC_READ | GENERIC_WRITE, dwDesiredAccess); - EXPECT_EQ(FALSE, bInheritHandle); - EXPECT_EQ(0u, dwOptions); - *lpTargetHandle = reinterpret_cast(0x8888); - return TRUE; - }; - - size_t closeHandleCallsBefore = SysCalls::closeHandleCalled; - - HANDLE sharedHandle = wddm->getSharedHandle(osHandleData); - - EXPECT_EQ(mockDuplicatedHandle, sharedHandle); - EXPECT_EQ(closeHandleCallsBefore + 1, SysCalls::closeHandleCalled); // Parent process handle should be closed - - // Cleanup - SysCalls::sysCallsOpenProcess = nullptr; - SysCalls::sysCallsDuplicateHandle = nullptr; -} - -TEST_F(WddmTestWithMockGdiDll, givenOsHandleDataWithParentProcessWhenOpenProcessFailsThenReturnOriginalHandle) { - uint64_t originalHandle = 0x12345678; - uint32_t parentProcessId = 1234; - MemoryManager::OsHandleData osHandleData(originalHandle); - osHandleData.parentProcessId = parentProcessId; - - // Mock openProcess to fail - SysCalls::sysCallsOpenProcess = [](DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) -> HANDLE { - return nullptr; - }; - - HANDLE sharedHandle = wddm->getSharedHandle(osHandleData); - - EXPECT_EQ(reinterpret_cast(static_cast(originalHandle)), sharedHandle); - - // Cleanup - SysCalls::sysCallsOpenProcess = nullptr; -} - -TEST_F(WddmTestWithMockGdiDll, givenOsHandleDataWithParentProcessWhenDuplicateHandleFailsThenReturnOriginalHandle) { - uint64_t originalHandle = 0x12345678; - uint32_t parentProcessId = 1234; - MemoryManager::OsHandleData osHandleData(originalHandle); - osHandleData.parentProcessId = parentProcessId; - - // Mock openProcess to succeed - SysCalls::sysCallsOpenProcess = [](DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) -> HANDLE { - return reinterpret_cast(0x9999); - }; - - // Mock duplicateHandle to fail - SysCalls::sysCallsDuplicateHandle = [](HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions) -> BOOL { - return FALSE; - }; - - size_t closeHandleCallsBefore = SysCalls::closeHandleCalled; - - HANDLE sharedHandle = wddm->getSharedHandle(osHandleData); - - EXPECT_EQ(reinterpret_cast(static_cast(originalHandle)), sharedHandle); - EXPECT_EQ(closeHandleCallsBefore + 1, SysCalls::closeHandleCalled); // Parent process handle should still be closed - - // Cleanup - SysCalls::sysCallsOpenProcess = nullptr; - SysCalls::sysCallsDuplicateHandle = nullptr; -}