fix: add FileDescriptor class to ensure file descriptor is closed

Related-To: NEO-9038
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski
2023-10-09 10:55:30 +00:00
committed by Compute-Runtime-Automation
parent c2d69e5857
commit 099a3f30e3
15 changed files with 139 additions and 217 deletions

View File

@@ -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

View File

@@ -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 = "";

View File

@@ -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<uint8_t> &
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<uint8_t> &
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;
}

View File

@@ -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<uint8_t *>(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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> 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<decltype(NEO::SysCalls::sysCallsOpen)> openBackup(&NEO::SysCalls::sysCallsOpen, openMockDiag);
pLinuxSysmanImp->preadFunction = preadMockDiag;
pLinuxSysmanImp->pwriteFunction = pwriteMockDiag;

View File

@@ -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<uint8_t *>(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<decltype(NEO::SysCalls::sysCallsOpen)> 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<OsPci *>(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<decltype(NEO::SysCalls::sysCallsOpen)> openBackup(&NEO::SysCalls::sysCallsOpen, openMockReturnFailure);
pLinuxPciImpTemp->closeFunction = closeMock;
pLinuxPciImpTemp->preadFunction = preadMock;