From 099a3f30e35f763e9376afc292ba4afe2ad85d99 Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Mon, 9 Oct 2023 10:55:30 +0000 Subject: [PATCH] fix: add FileDescriptor class to ensure file descriptor is closed Related-To: NEO-9038 Signed-off-by: Mateusz Jablonski --- .../sysman/source/linux/zes_os_sysman_imp.cpp | 17 +--- .../sysman/source/linux/zes_os_sysman_imp.h | 2 - .../linux/test_zes_sysman_diagnostics.cpp | 81 ++++-------------- .../source/sysman/linux/os_sysman_imp.cpp | 17 +--- .../tools/source/sysman/linux/os_sysman_imp.h | 2 - .../source/sysman/pci/linux/os_pci_imp.cpp | 8 +- .../linux/test_zes_sysman_diagnostics.cpp | 82 ++++--------------- .../sources/sysman/pci/linux/test_zes_pci.cpp | 30 +------ .../source/os_interface/linux/CMakeLists.txt | 1 + .../os_interface/linux/file_descriptor.h | 33 ++++++++ shared/source/os_interface/linux/pmt_util.cpp | 36 ++++---- .../linux/sys_calls_linux_ult.cpp | 3 +- .../os_interface/linux/sys_calls_linux_ult.h | 2 + .../os_interface/linux/CMakeLists.txt | 1 + .../linux/file_descriptor_tests.cpp | 41 ++++++++++ 15 files changed, 139 insertions(+), 217 deletions(-) create mode 100644 shared/source/os_interface/linux/file_descriptor.h create mode 100644 shared/test/unit_test/os_interface/linux/file_descriptor_tests.cpp diff --git a/level_zero/sysman/source/linux/zes_os_sysman_imp.cpp b/level_zero/sysman/source/linux/zes_os_sysman_imp.cpp index 53a03a9806..840685770e 100644 --- a/level_zero/sysman/source/linux/zes_os_sysman_imp.cpp +++ b/level_zero/sysman/source/linux/zes_os_sysman_imp.cpp @@ -12,6 +12,7 @@ #include "shared/source/helpers/sleep.h" #include "shared/source/os_interface/driver_info.h" #include "shared/source/os_interface/linux/drm_neo.h" +#include "shared/source/os_interface/linux/file_descriptor.h" #include "shared/source/os_interface/linux/pci_path.h" #include "shared/source/os_interface/linux/system_info.h" #include "shared/source/os_interface/os_interface.h" @@ -370,8 +371,7 @@ ze_result_t LinuxSysmanImp::resizeVfBar(uint8_t size) { std::string pciConfigNode; pciConfigNode = gtDevicePath + "/config"; - int fdConfig = -1; - fdConfig = this->openFunction(pciConfigNode.c_str(), O_RDWR); + auto fdConfig = NEO::FileDescriptor(pciConfigNode.c_str(), O_RDWR); if (fdConfig < 0) { NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stdout, "Config node open failed\n"); @@ -397,11 +397,6 @@ ze_result_t LinuxSysmanImp::resizeVfBar(uint8_t size) { "Write to change VF bar size failed\n"); return ZE_RESULT_ERROR_UNKNOWN; } - if (this->closeFunction(fdConfig) < 0) { - NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stdout, - "Config node close failed\n"); - return ZE_RESULT_ERROR_UNKNOWN; - } return ZE_RESULT_SUCCESS; } @@ -414,9 +409,8 @@ ze_result_t LinuxSysmanImp::osWarmReset() { std::string rootPortPath; rootPortPath = getPciRootPortDirectoryPath(gtDevicePath); - int fd = 0; std::string configFilePath = rootPortPath + '/' + "config"; - fd = this->openFunction(configFilePath.c_str(), O_RDWR); + auto fd = NEO::FileDescriptor(configFilePath.c_str(), O_RDWR); if (fd < 0) { return ZE_RESULT_ERROR_UNKNOWN; } @@ -458,11 +452,6 @@ ze_result_t LinuxSysmanImp::osWarmReset() { } NEO::sleep(std::chrono::seconds(10)); // Sleep for 10seconds, allows the rescan to complete on all devices attached to the root port. - int ret = this->closeFunction(fd); - if (ret < 0) { - return ZE_RESULT_ERROR_UNKNOWN; - } - // PCIe port driver uses the BIOS allocated VF bars on bootup. A known bug exists in pcie port driver // and is causing VF bar allocation failure in PCIe port driver after an SBR - https://bugzilla.kernel.org/show_bug.cgi?id=216795 diff --git a/level_zero/sysman/source/linux/zes_os_sysman_imp.h b/level_zero/sysman/source/linux/zes_os_sysman_imp.h index d310f2a463..064034bfd5 100644 --- a/level_zero/sysman/source/linux/zes_os_sysman_imp.h +++ b/level_zero/sysman/source/linux/zes_os_sysman_imp.h @@ -64,8 +64,6 @@ class LinuxSysmanImp : public OsSysman, NEO::NonCopyableOrMovableClass { MOCKABLE_VIRTUAL ze_result_t osColdReset(); ze_result_t gpuProcessCleanup(); std::string getAddressFromPath(std::string &rootPortPath); - decltype(&NEO::SysCalls::open) openFunction = NEO::SysCalls::open; - decltype(&NEO::SysCalls::close) closeFunction = NEO::SysCalls::close; decltype(&NEO::SysCalls::pread) preadFunction = NEO::SysCalls::pread; decltype(&NEO::SysCalls::pwrite) pwriteFunction = NEO::SysCalls::pwrite; ze_result_t createPmtHandles(); diff --git a/level_zero/sysman/test/unit_tests/sources/diagnostics/linux/test_zes_sysman_diagnostics.cpp b/level_zero/sysman/test/unit_tests/sources/diagnostics/linux/test_zes_sysman_diagnostics.cpp index 4430626199..219b4f7a9b 100644 --- a/level_zero/sysman/test/unit_tests/sources/diagnostics/linux/test_zes_sysman_diagnostics.cpp +++ b/level_zero/sysman/test/unit_tests/sources/diagnostics/linux/test_zes_sysman_diagnostics.cpp @@ -40,23 +40,6 @@ inline static int gtPciConfigOpenFail(const char *pathname, int flags) { } } -inline static int closeMockDiag(int fd) { - if ((fd == mockFileDescriptor) || (fd == mockGtPciConfigFd)) { - return 0; - } - return -1; -} -inline static int closeMockDiagFail(int fd) { - return -1; -} - -inline static int mockGtConfigcloseFail(int fd) { - if (fd == mockGtPciConfigFd) { - return -1; - } - return 0; -} - ssize_t preadMockDiag(int fd, void *buf, size_t count, off_t offset) { uint8_t *mockBuf = static_cast(buf); if (fd == mockGtPciConfigFd) { @@ -555,8 +538,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetThen DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -565,8 +547,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetThen TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtPciConfigOpenFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = gtPciConfigOpenFail; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, gtPciConfigOpenFail); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -575,8 +556,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndConfigHeaderIsInvalidThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = mockGtConfigPreadInvalid; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -585,8 +565,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtConfigPreadFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = mockGtConfigPreadFail; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -595,28 +574,16 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtConfigPwriteFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = mockGtConfigPwriteFail; EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, pLinuxSysmanImp->osWarmReset()); } -TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtConfigCloseFailsThenCallReturnsFailure) { - pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = mockGtConfigcloseFail; - pLinuxSysmanImp->preadFunction = preadMockDiag; - pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; - - EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, pLinuxSysmanImp->osWarmReset()); -} - TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndCardBusRemoveFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -628,8 +595,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndRootPortRescanFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -641,8 +607,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetThenCallSucceeds) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -653,8 +618,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetfrom DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; pLinuxSysmanImp->diagnosticsReset = true; @@ -666,8 +630,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetfrom DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; pLinuxSysmanImp->diagnosticsReset = true; @@ -681,8 +644,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndDelayForPPRWhenCallin DebugManager.flags.DebugSetMemoryDiagnosticsDelay.set(7); DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; pLinuxSysmanImp->diagnosticsReset = true; @@ -695,20 +657,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndR DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiagFail; - pLinuxSysmanImp->closeFunction = closeMockDiag; - pLinuxSysmanImp->preadFunction = preadMockDiag; - pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; - - EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, pLinuxSysmanImp->osWarmReset()); -} - -TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndRootPortConfigFileFailsToCloseThenCallFails) { - DebugManagerStateRestore dbgRestore; - DebugManager.flags.VfBarResourceAllocationWa.set(false); - pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiagFail; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiagFail); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -719,8 +668,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndC DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -732,8 +680,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndR DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; diff --git a/level_zero/tools/source/sysman/linux/os_sysman_imp.cpp b/level_zero/tools/source/sysman/linux/os_sysman_imp.cpp index 08dd35befe..75b8a3b2c4 100644 --- a/level_zero/tools/source/sysman/linux/os_sysman_imp.cpp +++ b/level_zero/tools/source/sysman/linux/os_sysman_imp.cpp @@ -12,6 +12,7 @@ #include "shared/source/helpers/sleep.h" #include "shared/source/memory_manager/memory_manager.h" #include "shared/source/os_interface/device_factory.h" +#include "shared/source/os_interface/linux/file_descriptor.h" #include "shared/source/os_interface/linux/system_info.h" #include "level_zero/core/source/device/device_imp.h" @@ -391,8 +392,7 @@ ze_result_t LinuxSysmanImp::resizeVfBar(uint8_t size) { std::string pciConfigNode; pciConfigNode = gtDevicePath + "/config"; - int fdConfig = -1; - fdConfig = this->openFunction(pciConfigNode.c_str(), O_RDWR); + auto fdConfig = NEO::FileDescriptor(pciConfigNode.c_str(), O_RDWR); if (fdConfig < 0) { NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stdout, "Config node open failed\n"); @@ -418,11 +418,6 @@ ze_result_t LinuxSysmanImp::resizeVfBar(uint8_t size) { "Write to change VF bar size failed\n"); return ZE_RESULT_ERROR_UNKNOWN; } - if (this->closeFunction(fdConfig) < 0) { - NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stdout, - "Config node close failed\n"); - return ZE_RESULT_ERROR_UNKNOWN; - } return ZE_RESULT_SUCCESS; } @@ -435,9 +430,8 @@ ze_result_t LinuxSysmanImp::osWarmReset() { std::string rootPortPath; rootPortPath = getPciRootPortDirectoryPath(gtDevicePath); - int fd = 0; std::string configFilePath = rootPortPath + '/' + "config"; - fd = this->openFunction(configFilePath.c_str(), O_RDWR); + auto fd = NEO::FileDescriptor(configFilePath.c_str(), O_RDWR); if (fd < 0) { return ZE_RESULT_ERROR_UNKNOWN; } @@ -479,11 +473,6 @@ ze_result_t LinuxSysmanImp::osWarmReset() { } NEO::sleep(std::chrono::seconds(10)); // Sleep for 10seconds, allows the rescan to complete on all devices attached to the root port. - int ret = this->closeFunction(fd); - if (ret < 0) { - return ZE_RESULT_ERROR_UNKNOWN; - } - // PCIe port driver uses the BIOS allocated VF bars on bootup. A known bug exists in pcie port driver // and is causing VF bar allocation failure in PCIe port driver after an SBR - https://bugzilla.kernel.org/show_bug.cgi?id=216795 diff --git a/level_zero/tools/source/sysman/linux/os_sysman_imp.h b/level_zero/tools/source/sysman/linux/os_sysman_imp.h index d8f156f5c5..4c0fa6bc43 100644 --- a/level_zero/tools/source/sysman/linux/os_sysman_imp.h +++ b/level_zero/tools/source/sysman/linux/os_sysman_imp.h @@ -75,8 +75,6 @@ class LinuxSysmanImp : public OsSysman, NEO::NonCopyableOrMovableClass { MOCKABLE_VIRTUAL ze_result_t osColdReset(); ze_result_t gpuProcessCleanup(); std::string getAddressFromPath(std::string &rootPortPath); - decltype(&NEO::SysCalls::open) openFunction = NEO::SysCalls::open; - decltype(&NEO::SysCalls::close) closeFunction = NEO::SysCalls::close; decltype(&NEO::SysCalls::pread) preadFunction = NEO::SysCalls::pread; decltype(&NEO::SysCalls::pwrite) pwriteFunction = NEO::SysCalls::pwrite; std::string devicePciBdf = ""; diff --git a/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp b/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp index ea61670aca..9c904c2d1e 100644 --- a/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp +++ b/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp @@ -9,6 +9,7 @@ #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/memory_manager/memory_manager.h" +#include "shared/source/os_interface/linux/file_descriptor.h" #include "shared/source/utilities/directory.h" #include "level_zero/core/source/driver/driver_handle.h" @@ -294,8 +295,7 @@ bool LinuxPciImp::getPciConfigMemory(std::string pciPath, std::vector & return false; } - int fd = -1; - fd = this->openFunction(pciPath.c_str(), O_RDONLY); + auto fd = NEO::FileDescriptor(pciPath.c_str(), O_RDONLY); if (fd < 0) { NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Error@ %s() Config File Open Failed \n", __FUNCTION__); return false; @@ -304,10 +304,6 @@ bool LinuxPciImp::getPciConfigMemory(std::string pciPath, std::vector & NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Error@ %s() Config Mem Read Failed \n", __FUNCTION__); return false; } - if (this->closeFunction(fd) < 0) { - NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Error@ %s() Config file close Failed \n", __FUNCTION__); - return false; - } return true; } diff --git a/level_zero/tools/test/unit_tests/sources/sysman/diagnostics/linux/test_zes_sysman_diagnostics.cpp b/level_zero/tools/test/unit_tests/sources/sysman/diagnostics/linux/test_zes_sysman_diagnostics.cpp index 615d85524e..5cc576f1e9 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/diagnostics/linux/test_zes_sysman_diagnostics.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/diagnostics/linux/test_zes_sysman_diagnostics.cpp @@ -6,6 +6,7 @@ */ #include "shared/test/common/helpers/ult_hw_config.h" +#include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h" #include "level_zero/tools/test/unit_tests/sources/sysman/diagnostics/linux/mock_zes_sysman_diagnostics.h" @@ -40,23 +41,6 @@ inline static int gtPciConfigOpenFail(const char *pathname, int flags) { } } -inline static int closeMockDiag(int fd) { - if ((fd == mockFileDescriptor) || (fd == mockGtPciConfigFd)) { - return 0; - } - return -1; -} -inline static int closeMockDiagFail(int fd) { - return -1; -} - -inline static int mockGtConfigcloseFail(int fd) { - if (fd == mockGtPciConfigFd) { - return -1; - } - return 0; -} - ssize_t preadMockDiag(int fd, void *buf, size_t count, off_t offset) { uint8_t *mockBuf = static_cast(buf); if (fd == mockGtPciConfigFd) { @@ -592,8 +576,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetThen DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -602,8 +585,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetThen TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtPciConfigOpenFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = gtPciConfigOpenFail; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, gtPciConfigOpenFail); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -612,8 +594,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndConfigHeaderIsInvalidThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = mockGtConfigPreadInvalid; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -622,8 +603,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtConfigPreadFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = mockGtConfigPreadFail; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -632,28 +612,16 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtConfigPwriteFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = mockGtConfigPwriteFail; EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, pLinuxSysmanImp->osWarmReset()); } -TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndGtConfigCloseFailsThenCallReturnsFailure) { - pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = mockGtConfigcloseFail; - pLinuxSysmanImp->preadFunction = preadMockDiag; - pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; - - EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, pLinuxSysmanImp->osWarmReset()); -} - TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndCardBusRemoveFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -665,8 +633,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetAndRootPortRescanFailsThenCallReturnsFailure) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -678,8 +645,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCal TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndVfBarIsResizedWhenCallingWarmResetThenCallSucceeds) { pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -690,8 +656,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetfrom DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; pLinuxSysmanImp->diagnosticsReset = true; @@ -703,8 +668,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetfrom DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; pLinuxSysmanImp->diagnosticsReset = true; @@ -718,8 +682,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerAndDelayForPPRWhenCallin DebugManager.flags.DebugSetMemoryDiagnosticsDelay.set(7); DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; pLinuxSysmanImp->diagnosticsReset = true; @@ -732,20 +695,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndR DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiagFail; - pLinuxSysmanImp->closeFunction = closeMockDiag; - pLinuxSysmanImp->preadFunction = preadMockDiag; - pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; - - EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, pLinuxSysmanImp->osWarmReset()); -} - -TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndRootPortConfigFileFailsToCloseThenCallFails) { - DebugManagerStateRestore dbgRestore; - DebugManager.flags.VfBarResourceAllocationWa.set(false); - pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiagFail; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiagFail); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -756,8 +706,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndC DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; @@ -769,8 +718,7 @@ TEST_F(ZesDiagnosticsFixture, GivenValidSysmanImpPointerWhenCallingWarmResetAndR DebugManagerStateRestore dbgRestore; DebugManager.flags.VfBarResourceAllocationWa.set(false); pLinuxSysmanImp->gtDevicePath = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:01.0/0000:8c:00.0"; - pLinuxSysmanImp->openFunction = openMockDiag; - pLinuxSysmanImp->closeFunction = closeMockDiag; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag); pLinuxSysmanImp->preadFunction = preadMockDiag; pLinuxSysmanImp->pwriteFunction = pwriteMockDiag; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp index ab9131530d..8761aa0258 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp @@ -5,6 +5,8 @@ * */ +#include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h" + #include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" #include "level_zero/tools/source/sysman/pci/pci_utils.h" #include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" @@ -49,10 +51,6 @@ inline static int closeMock(int fd) { return -1; } -inline static int closeMockReturnFailure(int fd) { - return -1; -} - ssize_t preadMock(int fd, void *buf, size_t count, off_t offset) { uint8_t *mockBuf = static_cast(buf); // Sample config values @@ -433,7 +431,7 @@ TEST_F(ZesPciFixture, GivenSysmanHandleWhenInitializingPciAndPciConfigOpenFailsT memoryManager->localMemorySupported[0] = 1; OsPci *pOsPciOriginal = pPciImp->pOsPci; PublicLinuxPciImp *pLinuxPciImpTemp = new PublicLinuxPciImp(pOsSysman); - pLinuxPciImpTemp->openFunction = openMockReturnFailure; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnFailure); pLinuxPciImpTemp->closeFunction = closeMock; pLinuxPciImpTemp->preadFunction = preadMock; @@ -447,26 +445,6 @@ TEST_F(ZesPciFixture, GivenSysmanHandleWhenInitializingPciAndPciConfigOpenFailsT pPciImp->pOsPci = pOsPciOriginal; } -TEST_F(ZesPciFixture, GivenSysmanHandleWhenInitializingPciAndPciConfigCloseFailsThenInvalidSpeedAndWidthAreReturned) { - int32_t width = 0; - double speed = 0; - memoryManager->localMemorySupported[0] = 1; - OsPci *pOsPciOriginal = pPciImp->pOsPci; - PublicLinuxPciImp *pLinuxPciImpTemp = new PublicLinuxPciImp(pOsSysman); - pLinuxPciImpTemp->openFunction = openMock; - pLinuxPciImpTemp->closeFunction = closeMockReturnFailure; - pLinuxPciImpTemp->preadFunction = preadMock; - - pPciImp->pOsPci = static_cast(pLinuxPciImpTemp); - pPciImp->pciGetStaticFields(); - pPciImp->pOsPci->getMaxLinkCaps(speed, width); - EXPECT_EQ(width, -1); - EXPECT_EQ(speed, 0); - - delete pLinuxPciImpTemp; - pPciImp->pOsPci = pOsPciOriginal; -} - TEST_F(ZesPciFixture, GivenSysmanHandleWhenGettingPCIWidthAndSpeedAndPCIHeaderIsAbsentThenInvalidValuesAreReturned) { int32_t width = 0; double speed = 0; @@ -511,7 +489,7 @@ TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVe TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenInitializingPciAndPciConfigOpenFailsThenResizableBarSupportWillBeFalse) { OsPci *pOsPciOriginal = pPciImp->pOsPci; PublicLinuxPciImp *pLinuxPciImpTemp = new PublicLinuxPciImp(pOsSysman); - pLinuxPciImpTemp->openFunction = openMockReturnFailure; + VariableBackup openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnFailure); pLinuxPciImpTemp->closeFunction = closeMock; pLinuxPciImpTemp->preadFunction = preadMock; diff --git a/shared/source/os_interface/linux/CMakeLists.txt b/shared/source/os_interface/linux/CMakeLists.txt index 4b82c4e597..cda05eac04 100644 --- a/shared/source/os_interface/linux/CMakeLists.txt +++ b/shared/source/os_interface/linux/CMakeLists.txt @@ -45,6 +45,7 @@ set(NEO_CORE_OS_INTERFACE_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/drm_wrappers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drm_wrappers.h ${CMAKE_CURRENT_SOURCE_DIR}/product_helper_drm.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/file_descriptor.h ${CMAKE_CURRENT_SOURCE_DIR}/hw_device_id.h ${CMAKE_CURRENT_SOURCE_DIR}/hw_device_id_linux.cpp ${CMAKE_CURRENT_SOURCE_DIR}/kmd_notify_properties_linux.cpp diff --git a/shared/source/os_interface/linux/file_descriptor.h b/shared/source/os_interface/linux/file_descriptor.h new file mode 100644 index 0000000000..8953f131a7 --- /dev/null +++ b/shared/source/os_interface/linux/file_descriptor.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/debug_helpers.h" +#include "shared/source/helpers/non_copyable_or_moveable.h" +#include "shared/source/os_interface/linux/sys_calls.h" + +namespace NEO { + +class FileDescriptor : NonCopyableOrMovableClass { + public: + FileDescriptor(const char *file, int flags) : handle(SysCalls::open(file, flags)) {} + + ~FileDescriptor() { + if (handle >= 0) { + [[maybe_unused]] auto retVal = SysCalls::close(handle); + DEBUG_BREAK_IF(retVal != 0); + } + } + operator int() const { + return handle; + } + + protected: + const int handle; +}; + +} // namespace NEO diff --git a/shared/source/os_interface/linux/pmt_util.cpp b/shared/source/os_interface/linux/pmt_util.cpp index a8004a0ff7..1f8546afb8 100644 --- a/shared/source/os_interface/linux/pmt_util.cpp +++ b/shared/source/os_interface/linux/pmt_util.cpp @@ -7,6 +7,7 @@ #include "shared/source/os_interface/linux/pmt_util.h" +#include "shared/source/os_interface/linux/file_descriptor.h" #include "shared/source/os_interface/linux/sys_calls.h" #include "shared/source/utilities/directory.h" @@ -53,13 +54,12 @@ void PmtUtil::getTelemNodesInPciPath(std::string_view rootPciPath, std::map &guidString) { std::ostringstream guidFilename; guidFilename << telemDir << "/guid"; - int fd = SysCalls::open(guidFilename.str().c_str(), O_RDONLY); - if (fd <= 0) { - return false; + auto fd = FileDescriptor(guidFilename.str().c_str(), O_RDONLY); + ssize_t bytesRead = 0; + if (fd > 0) { + guidString.fill('\0'); + bytesRead = SysCalls::pread(fd, guidString.data(), guidString.size() - 1, 0); } - guidString.fill('\0'); - ssize_t bytesRead = SysCalls::pread(fd, guidString.data(), guidString.size() - 1, 0); - SysCalls::close(fd); if (bytesRead <= 0) { return false; } @@ -71,18 +71,19 @@ bool PmtUtil::readOffset(std::string_view telemDir, uint64_t &offset) { std::ostringstream offsetFilename; offsetFilename << telemDir << "/offset"; - int fd = SysCalls::open(offsetFilename.str().c_str(), O_RDONLY); - if (fd <= 0) { + auto fd = FileDescriptor(offsetFilename.str().c_str(), O_RDONLY); + ssize_t bytesRead = 0; + std::array offsetString = {'\0'}; + if (fd > 0) { + offset = ULONG_MAX; + bytesRead = SysCalls::pread(fd, offsetString.data(), offsetString.size() - 1, 0); + } + if (bytesRead <= 0) { return false; } - offset = ULONG_MAX; - std::array offsetString = {'\0'}; - ssize_t bytesRead = SysCalls::pread(fd, offsetString.data(), offsetString.size() - 1, 0); - if (bytesRead > 0) { - std::replace(offsetString.begin(), offsetString.end(), '\n', '\0'); - offset = std::strtoul(offsetString.data(), nullptr, 10); - } - SysCalls::close(fd); + + std::replace(offsetString.begin(), offsetString.end(), '\n', '\0'); + offset = std::strtoul(offsetString.data(), nullptr, 10); if (offset == ULONG_MAX) { return false; @@ -99,10 +100,9 @@ ssize_t PmtUtil::readTelem(std::string_view telemDir, const std::size_t count, c ssize_t bytesRead = 0; std::ostringstream telemFilename; telemFilename << telemDir << "/telem"; - int fd = SysCalls::open(telemFilename.str().c_str(), O_RDONLY); + auto fd = FileDescriptor(telemFilename.str().c_str(), O_RDONLY); if (fd > 0) { bytesRead = SysCalls::pread(fd, data, count, static_cast(offset)); - SysCalls::close(fd); } return bytesRead; } 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 9e7083fef9..445d91983e 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 @@ -32,6 +32,7 @@ namespace NEO { namespace SysCalls { uint32_t closeFuncCalled = 0u; uint32_t openFuncCalled = 0u; +int openFuncRetVal = 0; int closeFuncArgPassed = 0; int closeFuncRetVal = 0; int dlOpenFlags = 0; @@ -145,7 +146,7 @@ int open(const char *file, int flags) { return fakeFileDescriptor; } - return 0; + return openFuncRetVal; } int openWithMode(const char *file, int flags, int mode) { 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 8cb098c8c7..8a379abe19 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 @@ -48,6 +48,8 @@ extern int (*sysCallsClosedir)(DIR *dir); extern int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize); extern int flockRetVal; +extern int openFuncRetVal; +extern uint32_t openFuncCalled; extern int closeFuncRetVal; extern int closeFuncArgPassed; extern const char *drmVersion; diff --git a/shared/test/unit_test/os_interface/linux/CMakeLists.txt b/shared/test/unit_test/os_interface/linux/CMakeLists.txt index 16b689dc5e..4d9f9a01e6 100644 --- a/shared/test/unit_test/os_interface/linux/CMakeLists.txt +++ b/shared/test/unit_test/os_interface/linux/CMakeLists.txt @@ -35,6 +35,7 @@ set(NEO_CORE_OS_INTERFACE_TESTS_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/product_helper_uuid_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/product_helper_linux_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/product_helper_linux_tests.h + ${CMAKE_CURRENT_SOURCE_DIR}/file_descriptor_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_context_linux_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_interface_linux_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_library_linux_tests.cpp diff --git a/shared/test/unit_test/os_interface/linux/file_descriptor_tests.cpp b/shared/test/unit_test/os_interface/linux/file_descriptor_tests.cpp new file mode 100644 index 0000000000..19ceab1feb --- /dev/null +++ b/shared/test/unit_test/os_interface/linux/file_descriptor_tests.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/source/os_interface/linux/file_descriptor.h" +#include "shared/test/common/helpers/variable_backup.h" +#include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h" + +#include "gtest/gtest.h" + +using namespace NEO; + +TEST(FileDescriptorTest, whenOpeningFileReturnsNonNegativeFileDescriptorThenCloseIsCalled) { + int mockHandle = 12345; + VariableBackup openRetValBackup(&SysCalls::openFuncRetVal, mockHandle); + VariableBackup openCalledBackup(&SysCalls::openFuncCalled, 0u); + VariableBackup closeCalledBackup(&SysCalls::closeFuncCalled, 0u); + + { + auto fileDescriptor = FileDescriptor("", 0); + EXPECT_EQ(SysCalls::openFuncRetVal, fileDescriptor); + EXPECT_EQ(1u, SysCalls::openFuncCalled); + SysCalls::closeFuncArgPassed = 0; + } + EXPECT_EQ(1u, SysCalls::closeFuncCalled); + EXPECT_EQ(SysCalls::openFuncRetVal, SysCalls::closeFuncArgPassed); + + SysCalls::openFuncRetVal = -1; + + { + auto fileDescriptor = FileDescriptor("", 0); + EXPECT_EQ(SysCalls::openFuncRetVal, fileDescriptor); + EXPECT_EQ(2u, SysCalls::openFuncCalled); + SysCalls::closeFuncArgPassed = 0; + } + + EXPECT_EQ(1u, SysCalls::closeFuncCalled); +}