qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts.
@ 2013-07-03  8:58 Izumi Tsutsui
  2013-07-03  9:41 ` Laszlo Ersek
  2013-08-14 16:29 ` [Qemu-devel] " Anthony Liguori
  0 siblings, 2 replies; 6+ messages in thread
From: Izumi Tsutsui @ 2013-07-03  8:58 UTC (permalink / raw)
  To: qemu-devel; +Cc: Izumi Tsutsui, lersek

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 <tsutsui@ceres.dti.ne.jp>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
---

 v3:
 - fix a missed assignment and actually check a retval of pthread_cond_wait()

 v2:
 - make QemuSemaphore.count unsigned (it won't be negative)
 - also eliminate checks for negative count values
 - check a return value of in pthread_cond_wait() in qemu_sem_wait()

 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.8.0.1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts.
  2013-07-03  8:58 [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts Izumi Tsutsui
@ 2013-07-03  9:41 ` Laszlo Ersek
  2013-08-01  3:24   ` Brad
  2013-08-14 16:29 ` [Qemu-devel] " Anthony Liguori
  1 sibling, 1 reply; 6+ messages in thread
From: Laszlo Ersek @ 2013-07-03  9:41 UTC (permalink / raw)
  To: Izumi Tsutsui; +Cc: qemu-devel

On 07/03/13 10:58, Izumi Tsutsui wrote:
> 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 <tsutsui@ceres.dti.ne.jp>
> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
> ---
> 
>  v3:
>  - fix a missed assignment and actually check a retval of pthread_cond_wait()

Compared v3 against v2.

Reviewed-by: Laszlo Ersek <lersek@redhat.com>

Laszlo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts.
  2013-07-03  9:41 ` Laszlo Ersek
@ 2013-08-01  3:24   ` Brad
  2013-08-01 22:33     ` Paolo Bonzini
  0 siblings, 1 reply; 6+ messages in thread
From: Brad @ 2013-08-01  3:24 UTC (permalink / raw)
  To: Laszlo Ersek; +Cc: Izumi Tsutsui, qemu-devel

On 03/07/13 5:41 AM, Laszlo Ersek wrote:
> On 07/03/13 10:58, Izumi Tsutsui wrote:
>> 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 <tsutsui@ceres.dti.ne.jp>
>> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
>> ---
>>
>>   v3:
>>   - fix a missed assignment and actually check a retval of pthread_cond_wait()
>
> Compared v3 against v2.
>
> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
>
> Laszlo

This patch seems to have been dropped.


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts.
  2013-08-01  3:24   ` Brad
@ 2013-08-01 22:33     ` Paolo Bonzini
  2013-08-05 16:22       ` [Qemu-devel] PING for-1.6 " Paolo Bonzini
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Bonzini @ 2013-08-01 22:33 UTC (permalink / raw)
  To: Brad; +Cc: Izumi Tsutsui, aliguori, Laszlo Ersek, qemu-devel, qemu-stable

On 08/01/2013 05:24 AM, Brad wrote:
> On 03/07/13 5:41 AM, Laszlo Ersek wrote:
>> On 07/03/13 10:58, Izumi Tsutsui wrote:
>>> 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 <tsutsui@ceres.dti.ne.jp>
>>> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
>>> ---
>>>
>>>   v3:
>>>   - fix a missed assignment and actually check a retval of
>>> pthread_cond_wait()
>>
>> Compared v3 against v2.
>>
>> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
>>
>> Laszlo
>
> This patch seems to have been dropped.

CCing Anthony and qemu-stable.

Paolo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Qemu-devel] PING for-1.6 Re: [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts.
  2013-08-01 22:33     ` Paolo Bonzini
@ 2013-08-05 16:22       ` Paolo Bonzini
  0 siblings, 0 replies; 6+ messages in thread
From: Paolo Bonzini @ 2013-08-05 16:22 UTC (permalink / raw)
  Cc: aliguori, qemu-stable, qemu-devel, Izumi Tsutsui, Laszlo Ersek,
	Brad

On 08/02/2013 12:33 AM, Paolo Bonzini wrote:
> On 08/01/2013 05:24 AM, Brad wrote:
>> On 03/07/13 5:41 AM, Laszlo Ersek wrote:
>>> On 07/03/13 10:58, Izumi Tsutsui wrote:
>>>> 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 <tsutsui@ceres.dti.ne.jp>
>>>> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
>>>> ---
>>>>
>>>>   v3:
>>>>   - fix a missed assignment and actually check a retval of
>>>> pthread_cond_wait()
>>>
>>> Compared v3 against v2.
>>>
>>> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
>>>
>>> Laszlo
>>
>> This patch seems to have been dropped.
>
> CCing Anthony and qemu-stable.

And bumping priority further.

Paolo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts.
  2013-07-03  8:58 [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts Izumi Tsutsui
  2013-07-03  9:41 ` Laszlo Ersek
@ 2013-08-14 16:29 ` Anthony Liguori
  1 sibling, 0 replies; 6+ messages in thread
From: Anthony Liguori @ 2013-08-14 16:29 UTC (permalink / raw)
  To: Izumi Tsutsui, qemu-devel; +Cc: aliguori, qemu-stable

Applied.  Thanks.

Regards,

Anthony Liguori

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-08-14 16:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-03  8:58 [Qemu-devel] [PATCH v3] semaphore: fix a hangup problem under load on NetBSD hosts Izumi Tsutsui
2013-07-03  9:41 ` Laszlo Ersek
2013-08-01  3:24   ` Brad
2013-08-01 22:33     ` Paolo Bonzini
2013-08-05 16:22       ` [Qemu-devel] PING for-1.6 " Paolo Bonzini
2013-08-14 16:29 ` [Qemu-devel] " Anthony Liguori

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).