Skip to content

Commit 1f531c0

Browse files
[SYCL][L0] Move command list cache usage under mutex (#5874)
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 <[email protected]>
1 parent 48c8cc1 commit 1f531c0

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,19 +1108,20 @@ _pi_context::getAvailableCommandList(pi_queue Queue,
11081108
// command list is available for reuse.
11091109
_pi_result pi_result = PI_OUT_OF_RESOURCES;
11101110
ZeStruct<ze_fence_desc_t> ZeFenceDesc;
1111-
1112-
auto &ZeCommandListCache =
1113-
UseCopyEngine
1114-
? Queue->Context->ZeCopyCommandListCache[Queue->Device->ZeDevice]
1115-
: Queue->Context->ZeComputeCommandListCache[Queue->Device->ZeDevice];
1116-
11171111
// Initally, we need to check if a command list has already been created
11181112
// on this device that is available for use. If so, then reuse that
11191113
// Level-Zero Command List and Fence for this PI call.
11201114
{
11211115
// Make sure to acquire the lock before checking the size, or there
11221116
// will be a race condition.
11231117
std::lock_guard<std::mutex> lock(Queue->Context->ZeCommandListCacheMutex);
1118+
// Under mutex since operator[] does insertion on the first usage for every
1119+
// unique ZeDevice.
1120+
auto &ZeCommandListCache =
1121+
UseCopyEngine
1122+
? Queue->Context->ZeCopyCommandListCache[Queue->Device->ZeDevice]
1123+
: Queue->Context
1124+
->ZeComputeCommandListCache[Queue->Device->ZeDevice];
11241125

11251126
if (ZeCommandListCache.size() > 0) {
11261127
auto &ZeCommandList = ZeCommandListCache.front();

0 commit comments

Comments
 (0)