Skip to contentSkip to conversation

[6631] Avoid infinite loop around timed condition variable

Qemu-devel malc 3 participants 5 messages Feb 21, 2009 – Feb 23, 2009

lore
Share & tools
lore

malc

author
#0 lore raw

Revision: 6631

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6631
Author:   malc
Date:     2009-02-21 05:48:13 +0000 (Sat, 21 Feb 2009)

Log Message:
-----------
Avoid infinite loop around timed condition variable

This can happen due to spurious wakeups

Modified Paths:
--------------
trunk/posix-aio-compat.c

Modified: trunk/posix-aio-compat.c
===================================================================

posix-aio-compat.c +7 -6
@@ -14,7 +14,7 @@
 #include <pthread.h>
 #include <unistd.h>
 #include <errno.h>
-#include <sys/time.h>
+#include <time.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -86,16 +86,17 @@
         struct qemu_paiocb *aiocb;
         size_t offset;
         int ret = 0;
+        qemu_timeval tv;
+        struct timespec ts;
 
+        qemu_gettimeofday(&tv);
+        ts.tv_sec = tv.tv_sec + 10;
+        ts.tv_nsec = 0;
+
         mutex_lock(&lock);
 
         while (TAILQ_EMPTY(&request_list) &&
                !(ret == ETIMEDOUT)) {
-            struct timespec ts = { 0 };
-            qemu_timeval tv;
-
-            qemu_gettimeofday(&tv);
-            ts.tv_sec = tv.tv_sec + 10;
             ret = cond_timedwait(&cond, &lock, &ts);
         }
 
#1 lore raw

Will these patches help with the OS X hang?

Quoted 56 lines · malc

Revision: 6631

         http://svn.sv.gnu.org/viewvc/? 
view=rev&root=qemu&revision=6631
Author:   malc
Date:     2009-02-21 05:48:13 +0000 (Sat, 21 Feb 2009)

Log Message:
-----------
Avoid infinite loop around timed condition variable

This can happen due to spurious wakeups

Modified Paths:
--------------
trunk/posix-aio-compat.c

Modified: trunk/posix-aio-compat.c
===================================================================

posix-aio-compat.c +7 -6
@@ -14,7 +14,7 @@
 #include <pthread.h>
 #include <unistd.h>
 #include <errno.h>
-#include <sys/time.h>
+#include <time.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -86,16 +86,17 @@
        struct qemu_paiocb *aiocb;
        size_t offset;
        int ret = 0;
+        qemu_timeval tv;
+        struct timespec ts;
 
+        qemu_gettimeofday(&tv);
+        ts.tv_sec = tv.tv_sec + 10;
+        ts.tv_nsec = 0;
+
        mutex_lock(&lock);
 
        while (TAILQ_EMPTY(&request_list) &&
               !(ret == ETIMEDOUT)) {
-            struct timespec ts = { 0 };
-            qemu_timeval tv;
-
-            qemu_gettimeofday(&tv);
-            ts.tv_sec = tv.tv_sec + 10;
            ret = cond_timedwait(&cond, &lock, &ts);
        }
 
author
#2 lore raw
Quoted 1 line · C.W. Betts

Will these patches help with the OS X hang?

No idea.

Signature · 1 line
mailto:[email protected]
#3 lore raw
In reply to C.W. Betts
Quoted 1 line · C.W. Betts

Will these patches help with the OS X hang?

No. I thought that got resolved? Maybe I forgot to push the patch?

Regards,

Anthony Liguori

Quoted 59 lines · malc
Quoted 57 lines

Revision: 6631

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6631 
<http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6631>
Author:   malc
Date:     2009-02-21 05:48:13 +0000 (Sat, 21 Feb 2009)

Log Message:
-----------
Avoid infinite loop around timed condition variable

This can happen due to spurious wakeups

Modified Paths:
--------------
trunk/posix-aio-compat.c

Modified: trunk/posix-aio-compat.c
===================================================================

posix-aio-compat.c 2009-02-21 05:48:13 UTC (rev 6631) renamed from posix-aio-compat.c 2009-02-21 05:48:11 UTC (rev 6630) +7 -6
@@ -14,7 +14,7 @@
 #include <pthread.h>
 #include <unistd.h>
 #include <errno.h>
-#include <sys/time.h>
+#include <time.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -86,16 +86,17 @@
        struct qemu_paiocb *aiocb;
        size_t offset;
        int ret = 0;
+        qemu_timeval tv;
+        struct timespec ts;
 
+        qemu_gettimeofday(&tv);
+        ts.tv_sec = tv.tv_sec + 10;
+        ts.tv_nsec = 0;
+
        mutex_lock(&lock);
 
        while (TAILQ_EMPTY(&request_list) &&
               !(ret == ETIMEDOUT)) {
-            struct timespec ts = { 0 };
-            qemu_timeval tv;
-
-            qemu_gettimeofday(&tv);
-            ts.tv_sec = tv.tv_sec + 10;
            ret = cond_timedwait(&cond, &lock, &ts);
        }
 
#4 lore raw
In reply to C.W. Betts
Quoted 1 line · C.W. Betts

Will these patches help with the OS X hang?

Can you try the attached patch? I never got any feedback about whether it fixed the problem.

Regards,

Anthony Liguori

Quoted 3 lines

On Feb 20, 2009, at 10:48 PM, malc wrote

Quoted 1 line

Subject: [PATCH] Do not use signals in posix-aio

The signal-select race forces us to use a file descriptor for posix-aio notification. Currently, we use a pipe and write to it from the signal handler because normal posix-aio does not provide file descriptor notification support.

Since we now emulation posix-aio on our own, we can clean things up and just use a file descriptor for notification.

N.B. This is almost certainly going to cause regressions until we fix the select loop to run in a separate thread since there will no longer be a signal that breaks us out of the TCG run loop. We need to hold off on applying this patch until that is fixed properly.

block-raw-posix.c +3 -17
index 4404eb1..cae4408 100644
@@ -511,20 +511,11 @@ static int posix_aio_flush(void *opaque)
 
 static PosixAioState *posix_aio_state;
 
-static void aio_signal_handler(int signum)
-{
-    if (posix_aio_state) {
-        char byte = 0;
-
-        write(posix_aio_state->wfd, &byte, sizeof(byte));
-    }
-
-    qemu_service_io();
-}
+/* FIXME need to fix the select() loop issue b/c this IO will not
+ * necessarily break out us out the TCG loop */
 
 static int posix_aio_init(void)
 {
-    struct sigaction act;
     PosixAioState *s;
     int fds[2];
     struct qemu_paioinit ai;
@@ -536,11 +527,6 @@ static int posix_aio_init(void)
     if (s == NULL)
         return -ENOMEM;
 
-    sigfillset(&act.sa_mask);
-    act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
-    act.sa_handler = aio_signal_handler;
-    sigaction(SIGUSR2, &act, NULL);
-
     s->first_aio = NULL;
     if (pipe(fds) == -1) {
         fprintf(stderr, "failed to create pipe\n");
@@ -579,7 +565,7 @@ static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
     if (!acb)
         return NULL;
     acb->aiocb.aio_fildes = s->fd;
-    acb->aiocb.ev_signo = SIGUSR2;
+    acb->aiocb.ev_fd = posix_aio_state->wfd;
     acb->aiocb.aio_buf = buf;
     if (nb_sectors < 0)
         acb->aiocb.aio_nbytes = -nb_sectors;
posix-aio-compat.c +10 -2
index f141cd9..4409899 100644
@@ -27,6 +27,15 @@ static int cur_threads = 0;
 static int idle_threads = 0;
 static TAILQ_HEAD(, qemu_paiocb) request_list;
 
+static void signal_fd(int fd)
+{
+    ssize_t len;
+    do {
+        char value = 1;
+        len = write(fd, &value, 1);
+    } while (len == -1 && errno == EINTR);
+}
+
 static void *aio_thread(void *unused)
 {
     sigset_t set;
@@ -92,9 +101,8 @@ static void *aio_thread(void *unused)
         pthread_mutex_lock(&lock);
         aiocb->ret = offset;
         idle_threads++;
+        signal_fd(aiocb->ev_fd);
         pthread_mutex_unlock(&lock);
-
-        kill(getpid(), aiocb->ev_signo);
     }
 
     idle_threads--;
posix-aio-compat.h +1 -1
index 0bc10f5..ec17bb6 100644
@@ -29,7 +29,7 @@ struct qemu_paiocb
     int aio_fildes;
     void *aio_buf;
     size_t aio_nbytes;
-    int ev_signo;
+    int ev_fd;
     off_t aio_offset;
 
     /* private */