Received: from eggs.gnu.org ([2001:4830:134:3::10]:45128) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buFsd-0002Mp-0i for qemu-devel@nongnu.org; Wed, 12 Oct 2016 05:31:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1buFsY-0008Sw-Rd for qemu-devel@nongnu.org; Wed, 12 Oct 2016 05:31:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35290) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buFsY-0008So-Ia for qemu-devel@nongnu.org; Wed, 12 Oct 2016 05:31:14 -0400 References: <1476107947-31430-1-git-send-email-pbonzini@redhat.com> <1476107947-31430-4-git-send-email-pbonzini@redhat.com> <87y41torbz.fsf@linaro.org> From: Paolo Bonzini Message-ID: Date: Wed, 12 Oct 2016 11:31:10 +0200 MIME-Version: 1.0 In-Reply-To: <87y41torbz.fsf@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 3/5] qemu-thread: use acquire/release to clarify semantics of QemuEvent List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?UTF-8?Q?Alex_Benn=c3=a9e?= Cc: qemu-devel@nongnu.org, cota@braap.org On 12/10/2016 11:21, Alex Benn=C3=A9e wrote: >=20 > Paolo Bonzini writes: >=20 >> Do not use the somewhat mysterious atomic_mb_read/atomic_mb_set, >> instead make sure that the operations on QemuEvent are annotated >> with the desired acquire and release semantics. >> >> In particular, qemu_event_set wakes up the waiting thread, so it must >> be a release from the POV of the waker (compare with qemu_mutex_unlock= ). >> And it actually needs a full barrier, because that's the only thing th= at >> provides something like a "load-release". >> >> Use smp_mb_acquire until we have atomic_load_acquire and >> atomic_store_release in atomic.h. >> >> Signed-off-by: Paolo Bonzini >> --- >> util/qemu-thread-posix.c | 15 ++++++++++++--- >> util/qemu-thread-win32.c | 15 ++++++++++++--- >> 2 files changed, 24 insertions(+), 6 deletions(-) >> >> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c >> index 74a3023..ce51b37 100644 >> --- a/util/qemu-thread-posix.c >> +++ b/util/qemu-thread-posix.c >> @@ -360,7 +360,11 @@ void qemu_event_destroy(QemuEvent *ev) >> >> void qemu_event_set(QemuEvent *ev) >> { >> - if (atomic_mb_read(&ev->value) !=3D EV_SET) { >> + /* qemu_event_set has release semantics, but because it *loads* >> + * ev->value we need a full memory barrier here. >> + */ >> + smp_mb(); >> + if (atomic_read(&ev->value) !=3D EV_SET) { >> if (atomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { >> /* There were waiters, wake them up. */ >> futex_wake(ev, INT_MAX); >> @@ -370,7 +374,11 @@ void qemu_event_set(QemuEvent *ev) >> >> void qemu_event_reset(QemuEvent *ev) >> { >> - if (atomic_mb_read(&ev->value) =3D=3D EV_SET) { >> + unsigned value; >> + >> + value =3D atomic_read(&ev->value); >> + smp_mb_acquire(); >> + if (value =3D=3D EV_SET) { >> /* >> * If there was a concurrent reset (or even reset+wait), >> * do nothing. Otherwise change EV_SET->EV_FREE. >> @@ -383,7 +391,8 @@ void qemu_event_wait(QemuEvent *ev) >> { >> unsigned value; >> >> - value =3D atomic_mb_read(&ev->value); >> + value =3D atomic_read(&ev->value); >> + smp_mb_acquire(); >> if (value !=3D EV_SET) { >> if (value =3D=3D EV_FREE) { >> /* >> diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c >> index 98a5ddf..dcdc014 100644 >> --- a/util/qemu-thread-win32.c >> +++ b/util/qemu-thread-win32.c >> @@ -274,7 +274,11 @@ void qemu_event_destroy(QemuEvent *ev) >> >> void qemu_event_set(QemuEvent *ev) >> { >> - if (atomic_mb_read(&ev->value) !=3D EV_SET) { >> + /* qemu_event_set has release semantics, but because it *loads* >> + * ev->value we need a full memory barrier here. >> + */ >> + smp_mb(); >> + if (atomic_read(&ev->value) !=3D EV_SET) { >> if (atomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { >> /* There were waiters, wake them up. */ >> SetEvent(ev->event); >> @@ -284,7 +288,11 @@ void qemu_event_set(QemuEvent *ev) >> >> void qemu_event_reset(QemuEvent *ev) >> { >> - if (atomic_mb_read(&ev->value) =3D=3D EV_SET) { >> + unsigned value; >> + >> + value =3D atomic_read(&ev->value); >> + smp_mb_acquire(); >> + if (atomic_read(&ev->value) =3D=3D EV_SET) { >> /* If there was a concurrent reset (or even reset+wait), >> * do nothing. Otherwise change EV_SET->EV_FREE. >=20 > Why are we saving value here? We never use it. It should be used in the "if", and patchew rightly complained. I didn't run the docker-test-mingw compilation on this series. Paolo >=20 >> */ >> @@ -296,7 +304,8 @@ void qemu_event_wait(QemuEvent *ev) >> { >> unsigned value; >> >> - value =3D atomic_mb_read(&ev->value); >> + value =3D atomic_read(&ev->value); >> + smp_mb_acquire(); >> if (value !=3D EV_SET) { >> if (value =3D=3D EV_FREE) { >> /* qemu_event_set is not yet going to call SetEvent, but = we are >=20 >=20 > -- > Alex Benn=C3=A9e >=20