qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Marc Morcos <marcmorcos@google.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	Eduardo Habkost <eduardo@habkost.net>,
	"Dr . David Alan Gilbert" <dave@treblig.org>
Cc: "Michael S . Tsirkin" <mst@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
	Markus Armbruster <armbru@redhat.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	qemu-devel@nongnu.org, kvm@vger.kernel.org
Subject: Re: [PATCH 2/4] thread-pool: Fix thread race
Date: Mon, 15 Dec 2025 15:43:51 +0100	[thread overview]
Message-ID: <159ced33-46e4-4b86-85e7-eda01406f768@redhat.com> (raw)
In-Reply-To: <20251213001443.2041258-3-marcmorcos@google.com>

On 12/13/25 01:14, Marc Morcos wrote:
>   
>           req->ret = ret;

Better use qatomic_set here---will fix it myself, thanks!

Paolo

> -        /* Write ret before state.  */
> -        smp_wmb();
> -        req->state = THREAD_DONE;
> +        /* _release to write ret before state.  */
> +        qatomic_store_release(&req->state, THREAD_DONE);
>   
>           qemu_bh_schedule(pool->completion_bh);
>           qemu_mutex_lock(&pool->lock);
> @@ -180,7 +184,8 @@ static void thread_pool_completion_bh(void *opaque)
>   
>   restart:
>       QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
> -        if (elem->state != THREAD_DONE) {
> +        /* _acquire to read state before ret.  */
> +        if (qatomic_load_acquire(&elem->state) != THREAD_DONE) {
>               continue;
>           }
>   
> @@ -189,9 +194,6 @@ restart:
>           QLIST_REMOVE(elem, all);
>   
>           if (elem->common.cb) {
> -            /* Read state before ret.  */
> -            smp_rmb();
> -
>               /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
>                * wait for another request that completed at the same time.
>                */
> @@ -223,12 +225,12 @@ static void thread_pool_cancel(BlockAIOCB *acb)
>       trace_thread_pool_cancel_aio(elem, elem->common.opaque);
>   
>       QEMU_LOCK_GUARD(&pool->lock);
> -    if (elem->state == THREAD_QUEUED) {
> +    if (qatomic_read(&elem->state) == THREAD_QUEUED) {
>           QTAILQ_REMOVE(&pool->request_list, elem, reqs);
>           qemu_bh_schedule(pool->completion_bh);
>   
> -        elem->state = THREAD_DONE;
> -        elem->ret = -ECANCELED;
> +        qatomic_set(&elem->ret, -ECANCELED);
> +        qatomic_store_release(&elem->state, THREAD_DONE);
>       }
>   
>   }



  reply	other threads:[~2025-12-15 14:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-13  0:14 [PATCH 0/4] Clean up TSAN warnings Marc Morcos
2025-12-13  0:14 ` [PATCH 1/4] apic: Resize APICBASE Marc Morcos
2025-12-15  9:48   ` Alex Bennée
2025-12-13  0:14 ` [PATCH 2/4] thread-pool: Fix thread race Marc Morcos
2025-12-15 14:43   ` Paolo Bonzini [this message]
2025-12-15 17:51     ` Marc Morcos
2025-12-13  0:14 ` [PATCH 3/4] qmp: " Marc Morcos
2025-12-15 14:52   ` Paolo Bonzini
2025-12-15 17:48     ` Marc Morcos
2025-12-13  0:14 ` [PATCH 4/4] apic: Make apicbase accesses atomic to fix data race Marc Morcos
  -- strict thread matches above, loose matches on Subject: below --
2025-12-13  0:12 [PATCH 0/4] Clean up TSAN warnings Marc Morcos
2025-12-13  0:12 ` [PATCH 2/4] thread-pool: Fix thread race Marc Morcos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=159ced33-46e4-4b86-85e7-eda01406f768@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dave@treblig.org \
    --cc=eduardo@habkost.net \
    --cc=kvm@vger.kernel.org \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=marcmorcos@google.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).