diff mbox series

[16/19] util: fixed tsan warnings in thread_pool.c

Message ID 20200522160755.886-17-robert.foley@linaro.org (mailing list archive)
State New, archived
Headers show
Series Add Thread Sanitizer support to QEMU | expand

Commit Message

Robert Foley May 22, 2020, 4:07 p.m. UTC
For example:
WARNING: ThreadSanitizer: data race (pid=14665)
  Write of size 4 at 0x7b1c00007890 by thread T99:
    #0 worker_thread util/thread-pool.c:112:20 (qemu-system-aarch64+0xd52108)
    #1 qemu_thread_start util/qemu-thread-posix.c:519:9 (qemu-system-aarch64+0xd5be30)

  Previous read of size 4 at 0x7b1c00007890 by main thread (mutexes: write M875, write M897):
    #0 thread_pool_completion_bh util/thread-pool.c:177:19 (qemu-system-aarch64+0xd51a73)
    #1 aio_bh_call util/async.c:136:5 (qemu-system-aarch64+0xd4f98e)
    #2 aio_bh_poll util/async.c:164:13 (qemu-system-aarch64+0xd4f98e)

Signed-off-by: Robert Foley <robert.foley@linaro.org>
---
 util/thread-pool.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Paolo Bonzini May 26, 2020, 8:18 p.m. UTC | #1
On 22/05/20 18:07, Robert Foley wrote:
>  #include "trace.h"
>  #include "block/thread-pool.h"
>  #include "qemu/main-loop.h"
> +#include "qemu/tsan.h"
>  
>  static void do_spawn_thread(ThreadPool *pool);
>  
> @@ -97,7 +98,9 @@ static void *worker_thread(void *opaque)
>          }
>  
>          req = QTAILQ_FIRST(&pool->request_list);
> +        TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
>          QTAILQ_REMOVE(&pool->request_list, req, reqs);
> +
>          req->state = THREAD_ACTIVE;
>          qemu_mutex_unlock(&pool->lock);
>  
> @@ -107,7 +110,7 @@ static void *worker_thread(void *opaque)
>          /* Write ret before state.  */
>          smp_wmb();
>          req->state = THREAD_DONE;
> -
> +        TSAN_ANNOTATE_IGNORE_WRITES_END();

You should instead use atomic_read/set for req->state and req->ret.

Paolo
diff mbox series

Patch

diff --git a/util/thread-pool.c b/util/thread-pool.c
index d763cea505..2403669827 100644
--- a/util/thread-pool.c
+++ b/util/thread-pool.c
@@ -21,6 +21,7 @@ 
 #include "trace.h"
 #include "block/thread-pool.h"
 #include "qemu/main-loop.h"
+#include "qemu/tsan.h"
 
 static void do_spawn_thread(ThreadPool *pool);
 
@@ -97,7 +98,9 @@  static void *worker_thread(void *opaque)
         }
 
         req = QTAILQ_FIRST(&pool->request_list);
+        TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
         QTAILQ_REMOVE(&pool->request_list, req, reqs);
+
         req->state = THREAD_ACTIVE;
         qemu_mutex_unlock(&pool->lock);
 
@@ -107,7 +110,7 @@  static void *worker_thread(void *opaque)
         /* Write ret before state.  */
         smp_wmb();
         req->state = THREAD_DONE;
-
+        TSAN_ANNOTATE_IGNORE_WRITES_END();
         qemu_mutex_lock(&pool->lock);
 
         qemu_bh_schedule(pool->completion_bh);