[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-stable] [PATCH 51/56] semaphore: fix a hangup problem under load o
From: |
Michael Roth |
Subject: |
[Qemu-stable] [PATCH 51/56] semaphore: fix a hangup problem under load on NetBSD hosts. |
Date: |
Tue, 13 Aug 2013 10:11:15 -0500 |
From: Izumi Tsutsui <address@hidden>
Fix following bugs in "fallback implementation of counting semaphores
with mutex+condvar" added in c166cb72f1676855816340666c3b618beef4b976:
- waiting threads are not restarted properly if more than one threads
are waiting unblock signals in qemu_sem_timedwait()
- possible missing pthread_cond_signal(3) calls when waiting threads
are returned by ETIMEDOUT
- fix an uninitialized variable
The problem is analyzed by and fix is provided by Noriyuki Soda.
Also put additional cleanup suggested by Laszlo Ersek:
- make QemuSemaphore.count unsigned (it won't be negative)
- check a return value of in pthread_cond_wait() in qemu_sem_wait()
Signed-off-by: Izumi Tsutsui <address@hidden>
Reviewed-by: Laszlo Ersek <address@hidden>
Message-id: address@hidden
Signed-off-by: Anthony Liguori <address@hidden>
(cherry picked from commit 79761c6681f0d1cc1c027116fcb4382d41ed3ece)
Signed-off-by: Michael Roth <address@hidden>
---
include/qemu/thread-posix.h | 2 +-
util/qemu-thread-posix.c | 28 ++++++++++++++++------------
2 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h
index 0f30dcc..361566a 100644
--- a/include/qemu/thread-posix.h
+++ b/include/qemu/thread-posix.h
@@ -15,7 +15,7 @@ struct QemuSemaphore {
#if defined(__APPLE__) || defined(__NetBSD__)
pthread_mutex_t lock;
pthread_cond_t cond;
- int count;
+ unsigned int count;
#else
sem_t sem;
#endif
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 4489abf..4de133e 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -170,12 +170,11 @@ void qemu_sem_post(QemuSemaphore *sem)
#if defined(__APPLE__) || defined(__NetBSD__)
pthread_mutex_lock(&sem->lock);
- if (sem->count == INT_MAX) {
+ if (sem->count == UINT_MAX) {
rc = EINVAL;
- } else if (sem->count++ < 0) {
- rc = pthread_cond_signal(&sem->cond);
} else {
- rc = 0;
+ sem->count++;
+ rc = pthread_cond_signal(&sem->cond);
}
pthread_mutex_unlock(&sem->lock);
if (rc != 0) {
@@ -207,19 +206,21 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
struct timespec ts;
#if defined(__APPLE__) || defined(__NetBSD__)
+ rc = 0;
compute_abs_deadline(&ts, ms);
pthread_mutex_lock(&sem->lock);
- --sem->count;
- while (sem->count < 0) {
+ while (sem->count == 0) {
rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
if (rc == ETIMEDOUT) {
- ++sem->count;
break;
}
if (rc != 0) {
error_exit(rc, __func__);
}
}
+ if (rc != ETIMEDOUT) {
+ --sem->count;
+ }
pthread_mutex_unlock(&sem->lock);
return (rc == ETIMEDOUT ? -1 : 0);
#else
@@ -249,16 +250,19 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
void qemu_sem_wait(QemuSemaphore *sem)
{
+ int rc;
+
#if defined(__APPLE__) || defined(__NetBSD__)
pthread_mutex_lock(&sem->lock);
- --sem->count;
- while (sem->count < 0) {
- pthread_cond_wait(&sem->cond, &sem->lock);
+ while (sem->count == 0) {
+ rc = pthread_cond_wait(&sem->cond, &sem->lock);
+ if (rc != 0) {
+ error_exit(rc, __func__);
+ }
}
+ --sem->count;
pthread_mutex_unlock(&sem->lock);
#else
- int rc;
-
do {
rc = sem_wait(&sem->sem);
} while (rc == -1 && errno == EINTR);
--
1.7.9.5
- [Qemu-stable] [PATCH 34/56] cpus: Let vm_stop[_force_state]() always flush block devices, (continued)
- [Qemu-stable] [PATCH 34/56] cpus: Let vm_stop[_force_state]() always flush block devices, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 45/56] chardev: fix CHR_EVENT_OPENED events for mux chardevs, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 54/56] dataplane: sync virtio.c and vring.c virtqueue state, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 55/56] virtio: clear signalled_used_valid when switching from dataplane, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 49/56] target-i386: Fix X86CPU error handling, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 47/56] seccomp: add additional asynchronous I/O syscalls, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 56/56] vhost: clear signalled_used_valid on vhost stop, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 53/56] i82801b11: Fix i82801b11 PCI host bridge config space, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 48/56] iov: handle EOF in iov_send_recv, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 52/56] Bugfix for loading multiboot kernels, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 51/56] semaphore: fix a hangup problem under load on NetBSD hosts.,
Michael Roth <=
- [Qemu-stable] [PATCH 50/56] ignore SIGPIPE in qemu-img and qemu-io, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 46/56] seccomp: add arch_prctl() to the syscall whitelist, Michael Roth, 2013/08/13
- [Qemu-stable] [PATCH 44/56] xhci: fix segfault, Michael Roth, 2013/08/13
- Re: [Qemu-stable] Patch Round-up for stable 1.5.3, freeze on 2013-08-16, Doug Goldstein, 2013/08/14