]> xenbits.xensource.com Git - xenclient/kernel.git/commitdiff
Avoid deadlock when unregistering a xenbus watch.
authorIan Campbell <ian.campbell@citrix.com>
Fri, 28 Mar 2008 09:49:22 +0000 (09:49 +0000)
committerIan Campbell <ian.campbell@citrix.com>
Fri, 28 Mar 2008 09:49:22 +0000 (09:49 +0000)
Watch handlers which run in a separate thread (XBWF_new_thread) should
run without the xenbus_mutex held since kthread_run can block waiting
for memory which causes a deadlock if further watches need to be
unregistered in order to activate the swap device on resume.

XBWF_new_thread cannot be safely unregistered anyway since the mutex
only protects thread startup.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
drivers/xen/xenbus/xenbus_xs.c

index f72714da1c36aafdedbe25290d2a7666f89f4a0f..6d21179d979eecb979aaa132faef3deb2c3b0fb3 100644 (file)
@@ -629,6 +629,8 @@ void unregister_xenbus_watch(struct xenbus_watch *watch)
        char token[sizeof(watch) * 2 + 1];
        int err;
 
+       BUG_ON(watch->flags & XBWF_new_thread);
+
        sprintf(token, "%lX", (long)watch);
 
        down_read(&xs_state.watch_mutex);
@@ -738,16 +740,29 @@ static int xenwatch_thread(void *unused)
                        list_del(ent);
                spin_unlock(&watch_events_lock);
 
-               if (ent != &watch_events) {
-                       msg = list_entry(ent, struct xs_stored_msg, list);
-                       if (msg->u.watch.handle->flags & XBWF_new_thread)
-                               kthread_run(xenwatch_handle_callback,
-                                           msg, "xenwatch_cb");
-                       else
-                               xenwatch_handle_callback(msg);
+               if (ent == &watch_events) {
+                       mutex_unlock(&xenwatch_mutex);
+                       continue;
                }
 
-               mutex_unlock(&xenwatch_mutex);
+               msg = list_entry(ent, struct xs_stored_msg, list);
+
+               /*
+                * Unlock the mutex before running an XBWF_new_thread
+                * handler. kthread_run can block which can deadlock
+                * against unregister_xenbus_watch() if we need to
+                * unregister other watches in order to make
+                * progress. This can occur on resume before the swap
+                * device is attached.
+                */
+               if (msg->u.watch.handle->flags & XBWF_new_thread) {
+                       mutex_unlock(&xenwatch_mutex);
+                       kthread_run(xenwatch_handle_callback,
+                                   msg, "xenwatch_cb");
+               } else {
+                       xenwatch_handle_callback(msg);
+                       mutex_unlock(&xenwatch_mutex);
+               }
        }
 
        return 0;