From 1ea7373026ece04ef493904d3ec73bc42ff4c309 Mon Sep 17 00:00:00 2001 From: Soren Soe <2106410+stsoe@users.noreply.github.com> Date: Thu, 27 Apr 2023 13:28:02 -0700 Subject: [PATCH] Fix race in obtaining index of CU (#7522) Add lock around accessing cu index in core device. Fixes a potential race when a thread access cu indices while another thread is updating the indices after an xclbin was loaded. The race can only occur when multiple hardware contexts are used because CU contexts are managed per hardware context by XRT coreutil. xrt_core::device::get_cuidx lazy gathers cu indices from KMD when a thread tries to open a CU as part of creating a kernel object. The first thread to access the index of a CU can cause all indices to be updated if the requested CU is not yet registered. --- src/runtime_src/core/common/device.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/runtime_src/core/common/device.cpp b/src/runtime_src/core/common/device.cpp index e86642bff06..8414ea28dff 100644 --- a/src/runtime_src/core/common/device.cpp +++ b/src/runtime_src/core/common/device.cpp @@ -348,6 +348,10 @@ const std::vector& device:: get_cus() const { + // This function returns a reference to internal data + // that is modified when an xclbin is loaded. Normally + // not an issue since only single xclbin is supported + // when using this API. if (m_cu2idx.size() > 1) throw error(std::errc::not_supported, "multiple xclbins not supported"); @@ -358,6 +362,7 @@ cuidx_type device:: get_cuidx(slot_id slot, const std::string& cuname) const { + std::lock_guard lk(m_mutex); auto slot_itr = m_cu2idx.find(slot); if (slot_itr == m_cu2idx.end()) throw error(EINVAL, "No such compute unit '" + cuname + "'");