From 6a5183475b3be42143d53efd4fb5057a4d9c5701 Mon Sep 17 00:00:00 2001 From: "Tikhomirova, Kseniya" Date: Thu, 24 Mar 2022 11:10:41 +0300 Subject: [PATCH] [SYCL][L0] Move command list cache usage under mutex For the example of usage that may cause issue please refer to assert_in_simulteneous_kernels E2E test. n queues that are working on the same device and on default context will work with the same zeCompute/CopyCommandListCache without protection since mutexes for queues are different buwhile cache is the same. Operator[] may trigger insertion and rehash so simulteneous access is not thread-safe. In this code cache is not prefilled and insertion is done on the first access for every unique ZeDevice what may end up with simulteneous write access and memory corruption Signed-off-by: Tikhomirova, Kseniya --- sycl/plugins/level_zero/pi_level_zero.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 3610dd9a34b38..09a1e9b935d8e 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -1108,12 +1108,6 @@ _pi_context::getAvailableCommandList(pi_queue Queue, // command list is available for reuse. _pi_result pi_result = PI_OUT_OF_RESOURCES; ZeStruct ZeFenceDesc; - - auto &ZeCommandListCache = - UseCopyEngine - ? Queue->Context->ZeCopyCommandListCache[Queue->Device->ZeDevice] - : Queue->Context->ZeComputeCommandListCache[Queue->Device->ZeDevice]; - // Initally, we need to check if a command list has already been created // on this device that is available for use. If so, then reuse that // Level-Zero Command List and Fence for this PI call. @@ -1121,6 +1115,13 @@ _pi_context::getAvailableCommandList(pi_queue Queue, // Make sure to acquire the lock before checking the size, or there // will be a race condition. std::lock_guard lock(Queue->Context->ZeCommandListCacheMutex); + // Under mutex since operator[] does insertion on the first usage for every + // unique ZeDevice. + auto &ZeCommandListCache = + UseCopyEngine + ? Queue->Context->ZeCopyCommandListCache[Queue->Device->ZeDevice] + : Queue->Context + ->ZeComputeCommandListCache[Queue->Device->ZeDevice]; if (ZeCommandListCache.size() > 0) { auto &ZeCommandList = ZeCommandListCache.front();