From 4976a8c4f278f5af9730e068ae6b20a4f3c19123 Mon Sep 17 00:00:00 2001 From: Blaise Tine Date: Thu, 12 Aug 2021 20:06:51 -0700 Subject: [PATCH] enabling automatic device cleanup for bad applications not releasing the device on exit --- driver/opae/vortex.cpp | 33 +++++++++++++++++++++++++++++++++ driver/rtlsim/vortex.cpp | 32 ++++++++++++++++++++++++++++++++ driver/simx/vortex.cpp | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/driver/opae/vortex.cpp b/driver/opae/vortex.cpp index 960d7cbe..a2ff3061 100755 --- a/driver/opae/vortex.cpp +++ b/driver/opae/vortex.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #if defined(USE_FPGA) || defined(USE_ASE) #include @@ -78,6 +79,34 @@ inline bool is_aligned(size_t addr, size_t alignment) { /////////////////////////////////////////////////////////////////////////////// +class AutoDeviceCleanup { +private: + std::list devices_; + +public: + AutoDeviceCleanup() {} + + ~AutoDeviceCleanup() { + for (auto it = devices_.begin(), it_end = devices_.end(); it != it_end;) { + auto device = *it; + it = devices_.erase(it); + vx_dev_close(device); + } + } + + void add_device(vx_device_h device) { + devices_.push_back(device); + } + + void remove_device(vx_device_h device) { + devices_.remove(device); + } +}; + +AutoDeviceCleanup gAutoDeviceCleanup; + +/////////////////////////////////////////////////////////////////////////////// + extern int vx_dev_caps(vx_device_h hdevice, unsigned caps_id, unsigned *value) { if (nullptr == hdevice) return -1; @@ -223,6 +252,8 @@ extern int vx_dev_open(vx_device_h* hdevice) { *hdevice = device; + gAutoDeviceCleanup.add_device(device); + return 0; } @@ -230,6 +261,8 @@ extern int vx_dev_close(vx_device_h hdevice) { if (nullptr == hdevice) return -1; + gAutoDeviceCleanup.remove_device(hdevice); + vx_device_t *device = ((vx_device_t*)hdevice); #ifdef SCOPE diff --git a/driver/rtlsim/vortex.cpp b/driver/rtlsim/vortex.cpp index 544f0f15..cc4b0caa 100644 --- a/driver/rtlsim/vortex.cpp +++ b/driver/rtlsim/vortex.cpp @@ -154,6 +154,34 @@ private: /////////////////////////////////////////////////////////////////////////////// +class AutoDeviceCleanup { +private: + std::list devices_; + +public: + AutoDeviceCleanup() {} + + ~AutoDeviceCleanup() { + for (auto it = devices_.begin(), it_end = devices_.end(); it != it_end;) { + auto device = *it; + it = devices_.erase(it); + vx_dev_close(device); + } + } + + void add_device(vx_device_h device) { + devices_.push_back(device); + } + + void remove_device(vx_device_h device) { + devices_.remove(device); + } +}; + +AutoDeviceCleanup gAutoDeviceCleanup; + +/////////////////////////////////////////////////////////////////////////////// + extern int vx_dev_caps(vx_device_h hdevice, unsigned caps_id, unsigned *value) { if (nullptr == hdevice) return -1; @@ -198,6 +226,8 @@ extern int vx_dev_open(vx_device_h* hdevice) { *hdevice = new vx_device(); + gAutoDeviceCleanup.add_device(*hdevice); + return 0; } @@ -205,6 +235,8 @@ extern int vx_dev_close(vx_device_h hdevice) { if (nullptr == hdevice) return -1; + gAutoDeviceCleanup.remove_device(hdevice); + vx_device *device = ((vx_device*)hdevice); #ifdef DUMP_PERF_STATS diff --git a/driver/simx/vortex.cpp b/driver/simx/vortex.cpp index d2a6ca76..e3db32b8 100644 --- a/driver/simx/vortex.cpp +++ b/driver/simx/vortex.cpp @@ -223,11 +223,41 @@ private: /////////////////////////////////////////////////////////////////////////////// +class AutoDeviceCleanup { +private: + std::list devices_; + +public: + AutoDeviceCleanup() {} + + ~AutoDeviceCleanup() { + for (auto it = devices_.begin(), it_end = devices_.end(); it != it_end;) { + auto device = *it; + it = devices_.erase(it); + vx_dev_close(device); + } + } + + void add_device(vx_device_h device) { + devices_.push_back(device); + } + + void remove_device(vx_device_h device) { + devices_.remove(device); + } +}; + +AutoDeviceCleanup gAutoDeviceCleanup; + +/////////////////////////////////////////////////////////////////////////////// + extern int vx_dev_open(vx_device_h* hdevice) { if (nullptr == hdevice) return -1; - *hdevice = new vx_device(); + *hdevice = new vx_device(); + + gAutoDeviceCleanup.add_device(*hdevice); return 0; } @@ -236,6 +266,8 @@ extern int vx_dev_close(vx_device_h hdevice) { if (nullptr == hdevice) return -1; + gAutoDeviceCleanup.remove_device(hdevice); + vx_device *device = ((vx_device*)hdevice); #ifdef DUMP_PERF_STATS