[Openmp-commits] [PATCH] D80480: [OpenMP] Fix a race in task queue reallocation

Joachim Protze via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Sat May 23 14:19:31 PDT 2020


protze.joachim created this revision.
protze.joachim added reviewers: AndreyChurbanov, jlpeyton.
protze.joachim added a project: OpenMP.
Herald added subscribers: sstefan1, guansong, yaxunl.
Herald added a reviewer: jdoerfert.

`__kmp_realloc_task_deque` implicitly assumes, that the task queue is full (ntasks == size), therefore `tail = size` in line 319.
I added an assertion to document this assumption.

The first check for a full queue is before the locking and might not hold when the lock is taken. So, we need to check again for this condition when we have the lock.

This fixes the issue for the simple reproducer provided by Raul on openmp-dev, but also my issue with a more complex code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80480

Files:
  openmp/runtime/src/kmp_tasking.cpp


Index: openmp/runtime/src/kmp_tasking.cpp
===================================================================
--- openmp/runtime/src/kmp_tasking.cpp
+++ openmp/runtime/src/kmp_tasking.cpp
@@ -298,6 +298,7 @@
 static void __kmp_realloc_task_deque(kmp_info_t *thread,
                                      kmp_thread_data_t *thread_data) {
   kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td);
+  KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size);
   kmp_int32 new_size = 2 * size;
 
   KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to "
@@ -382,7 +383,10 @@
       __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock);
       locked = 1;
       // expand deque to push the task which is not allowed to execute
-      __kmp_realloc_task_deque(thread, thread_data);
+      if (TCR_4(thread_data->td.td_deque_ntasks) >=
+          TASK_DEQUE_SIZE(thread_data->td)) {
+        __kmp_realloc_task_deque(thread, thread_data);
+      }
     }
   }
   // Lock the deque for the task push operation


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80480.265888.patch
Type: text/x-patch
Size: 1039 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20200523/4d399cd5/attachment-0001.bin>


More information about the Openmp-commits mailing list