Skip to contentSkip to conversation

thread-pool: fix deadlock when callbacks depends on each other

PATCH Paolo Bonzini 2 participants 4 messages Jun 1, 2014 – Jun 2, 2014 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Il 31/05/2014 20:29, Marcin Gibuła ha scritto:

Quoted 6 lines
(quoted diff) +1 -0
@@ -185,6 +191,14 @@ restart:
         }
         if (elem->state == THREAD_DONE && elem->common.cb) {
             QLIST_REMOVE(elem, all);
+            /* If more completed requests are waiting, notifier needs

Review restart: · line 204

Quoted 10 lines
(quoted diff) +7 -0
+             * to be rearmed so callback can progress with aio_pool().
+             */
+            pool->pending_completions--;
+            if (pool->pending_completions) {
+                event_notifier_set(notifier);
+            }
+
             /* Read state before ret.  */
             smp_rmb();
             elem->common.cb(elem->common.opaque, elem->ret);

Good catch! The main problem with the patch is that you need to use atomic_inc/atomic_dec to increment and decrement pool->pending_completions.

Secondarily, event_notifier_set is pretty heavy-weight, does it work if you wrap the loop like this?

restart:

    QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
        ...
    }
    if (pool->pending_completions) {
        goto restart;
    }
    event_notifier_test_and_clear(notifier);
    if (pool->pending_completions) {
        event_notifier_set(notifier);
        goto restart;
    }

Finally, the same bug is also in block/linux-aio.c and block/win32-aio.c.

Paolo

#2 lore raw
Quoted 2 lines

Good catch! The main problem with the patch is that you need to use atomic_inc/atomic_dec to increment and decrement pool->pending_completions.

Ok.

Quoted 4 lines

Secondarily, event_notifier_set is pretty heavy-weight, does it work if you wrap the loop like this?

restart:

Quoted 11 lines
     QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
         ...
     }
     if (pool->pending_completions) {
         goto restart;
     }
     event_notifier_test_and_clear(notifier);
     if (pool->pending_completions) {
         event_notifier_set(notifier);
         goto restart;
     }

I'll test it tomorrow. I assume you want to avoid calling event_notifier_set() until function is reentered via aio_pool?

Quoted 2 lines

Finally, the same bug is also in block/linux-aio.c and block/win32-aio.c.

I can try with linux-aio, but my knowledge of windows api is zero...

Signature · 1 line
mg
#3 lore raw

Il 01/06/2014 21:02, Marcin Gibuła ha scritto:

Quoted 22 lines
Quoted 3 lines

Good catch! The main problem with the patch is that you need to use atomic_inc/atomic_dec to increment and decrement pool->pending_completions.

Ok.

Quoted 4 lines

Secondarily, event_notifier_set is pretty heavy-weight, does it work if you wrap the loop like this?

restart:

Quoted 11 lines
     QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
         ...
     }
     if (pool->pending_completions) {
         goto restart;
     }
     event_notifier_test_and_clear(notifier);
     if (pool->pending_completions) {
         event_notifier_set(notifier);
         goto restart;
     }
Quoted 2 lines

I'll test it tomorrow. I assume you want to avoid calling event_notifier_set() until function is reentered via aio_pool?

Yes. But actually, I need to check if it's possible to fix bdrv_drain_all. If you're in coroutine context, you can defer the draining to a safe point using a bottom half. If you're not in coroutine context, perhaps bdrv_drain_all has to be made illegal. Which means a bunch of code auditing...

Paolo

Quoted 5 lines
Quoted 2 lines

Finally, the same bug is also in block/linux-aio.c and block/win32-aio.c.

I can try with linux-aio, but my knowledge of windows api is zero...

#4 lore raw
Quoted 8 lines
Quoted 2 lines

I'll test it tomorrow. I assume you want to avoid calling event_notifier_set() until function is reentered via aio_pool?

Yes. But actually, I need to check if it's possible to fix bdrv_drain_all. If you're in coroutine context, you can defer the draining to a safe point using a bottom half. If you're not in coroutine context, perhaps bdrv_drain_all has to be made illegal. Which means a bunch of code auditing...

For what it's worth, your solution also works fine, I couldn't recreate hang with it. Updated patch proposal posted earlier today.

Signature · 1 line
mg