Skip to contentSkip to conversation

libxl: make libxl_wait_for_device_model not racy

PATCH In review Stefano Stabellini 4 participants 8 messages Jun 23, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: make libxl_wait_for_device_model not racy

Hi all,
at the moment libxl_wait_for_device_model waits on a xenstore watch before checking the current value of the xenstore node, that might contain already the value the function was looking for. This patch changes libxl_wait_for_device_model so that it checks the value of the xenstore node first, then waits for the watch.

---

tools/libxl/libxl_device.c +20 -19
diff -r e2f5e4f3481c tools/libxl/libxl_device.c
@@ -418,7 +418,7 @@
     char *path;
     char *p;
     unsigned int len;
-    int rc;
+    int rc = 0;
     struct xs_handle *xsh;
     int nfds;
     fd_set rfds;
@@ -432,28 +432,29 @@
     tv.tv_sec = LIBXL_DEVICE_MODEL_START_TIMEOUT;
     tv.tv_usec = 0;
     nfds = xs_fileno(xsh) + 1;
-    while (tv.tv_sec > 0) {
+    while (rc > 0 || (!rc && tv.tv_sec > 0)) {
+        p = xs_read(xsh, XBT_NULL, path, &len);
+        if (p && (!state || !strcmp(state, p))) {
+            free(p);
+            xs_unwatch(xsh, path, path);
+            xs_daemon_close(xsh);
+            if (check_callback) {
+                rc = check_callback(ctx, check_callback_userdata);
+                if (rc) return rc;
+            }
+            return 0;
+        }
+        free(p);
+again:
         FD_ZERO(&rfds);
         FD_SET(xs_fileno(xsh), &rfds);
-        if (select(nfds, &rfds, NULL, NULL, &tv) > 0) {
+        rc = select(nfds, &rfds, NULL, NULL, &tv);
+        if (rc > 0) {
             l = xs_read_watch(xsh, &num);
-            if (l != NULL) {
+            if (l != NULL)
                 free(l);
-                p = xs_read(xsh, XBT_NULL, path, &len);
-                if (!p)
-                    continue;
-                if (!state || !strcmp(state, p)) {
-                    free(p);
-                    xs_unwatch(xsh, path, path);
-                    xs_daemon_close(xsh);
-                    if (check_callback) {
-                        rc = check_callback(ctx, check_callback_userdata);
-                        if (rc) return rc;
-                    }
-                    return 0;
-                }
-                free(p);
-            }
+            else
+                goto again;
         }
     }
     xs_unwatch(xsh, path, path);
#1 lore raw
Quoted 9 lines · Stefano Stabellini

Hi all,
at the moment libxl_wait_for_device_model waits on a xenstore watch before checking the current value of the xenstore node, that might contain already the value the function was looking for. This patch changes libxl_wait_for_device_model so that it checks the value of the xenstore node first, then waits for the watch.

xenstore watch automatically fire one time when you install them for this exact same purpose. Is this patch actually solving any issue you saw ?

Signature · 1 line
Vincent
#2 lore raw
Quoted 8 lines · Vincent Hanquez
Quoted 7 lines · Stefano Stabellini

Hi all,
at the moment libxl_wait_for_device_model waits on a xenstore watch before checking the current value of the xenstore node, that might contain already the value the function was looking for. This patch changes libxl_wait_for_device_model so that it checks the value of the xenstore node first, then waits for the watch.

Quoted 4 lines
Quoted 2 lines

xenstore watch automatically fire one time when you install them for this exact same purpose. Is this patch actually solving any issue you saw ?

No, it was just refactoring.

Are you sure that both xenstore implementations always do that? Is this a "feature" we can safely rely on?
If both answers are positive, then we can disregard this patch.

#3 lore raw
Quoted 4 lines · Stefano Stabellini

No, it was just refactoring.

Are you sure that both xenstore implementations always do that?

yes.

Quoted 2 lines

Is this a "feature" we can safely rely on?

yes. this feature has been here since xenstore has watches, for this exact purpose: not having to check before the watch fire that the values you're expecting is there already.

Signature · 1 line
Vincent
#4 lore raw

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH] libxl: make libxl_wait_for_device_model not racy"):

Quoted 3 lines

yes. this feature has been here since xenstore has watches, for this exact purpose: not having to check before the watch fire that the values you're expecting is there already.

I think the new code is preferable because it's clearer, even if we never undo that xenstore wrinkle. (I agree that we probably can't.)

Ian.

#5 lore raw
In reply to Stefano Stabellini
Quoted 7 lines · Stefano Stabellini

Hi all,
at the moment libxl_wait_for_device_model waits on a xenstore watch before checking the current value of the xenstore node, that might contain already the value the function was looking for. This patch changes libxl_wait_for_device_model so that it checks the value of the xenstore node first, then waits for the watch.

That can't help because it's still racy: what if the value changes between the first check and the wait? The watch must fire immediately if the value is already in the desired state, or there's an unavoidable deadlock.

On the other hand, the check-then-wait pattern reads more clearly, I think.

J

Quoted 71 lines

---

diff -r e2f5e4f3481c tools/libxl/libxl_device.c

tools/libxl/libxl_device.c +20 -19
@@ -418,7 +418,7 @@
     char *path;
     char *p;
     unsigned int len;
-    int rc;
+    int rc = 0;
     struct xs_handle *xsh;
     int nfds;
     fd_set rfds;
@@ -432,28 +432,29 @@
     tv.tv_sec = LIBXL_DEVICE_MODEL_START_TIMEOUT;
     tv.tv_usec = 0;
     nfds = xs_fileno(xsh) + 1;
-    while (tv.tv_sec > 0) {
+    while (rc > 0 || (!rc && tv.tv_sec > 0)) {
+        p = xs_read(xsh, XBT_NULL, path, &len);
+        if (p && (!state || !strcmp(state, p))) {
+            free(p);
+            xs_unwatch(xsh, path, path);
+            xs_daemon_close(xsh);
+            if (check_callback) {
+                rc = check_callback(ctx, check_callback_userdata);
+                if (rc) return rc;
+            }
+            return 0;
+        }
+        free(p);
+again:
         FD_ZERO(&rfds);
         FD_SET(xs_fileno(xsh), &rfds);
-        if (select(nfds, &rfds, NULL, NULL, &tv) > 0) {
+        rc = select(nfds, &rfds, NULL, NULL, &tv);
+        if (rc > 0) {
             l = xs_read_watch(xsh, &num);
-            if (l != NULL) {
+            if (l != NULL)
                 free(l);
-                p = xs_read(xsh, XBT_NULL, path, &len);
-                if (!p)
-                    continue;
-                if (!state || !strcmp(state, p)) {
-                    free(p);
-                    xs_unwatch(xsh, path, path);
-                    xs_daemon_close(xsh);
-                    if (check_callback) {
-                        rc = check_callback(ctx, check_callback_userdata);
-                        if (rc) return rc;
-                    }
-                    return 0;
-                }
-                free(p);
-            }
+            else
+                goto again;
         }
     }
     xs_unwatch(xsh, path, path);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#6 lore raw
Quoted 9 lines · Jeremy Fitzhardinge
Quoted 7 lines · Stefano Stabellini

Hi all,
at the moment libxl_wait_for_device_model waits on a xenstore watch before checking the current value of the xenstore node, that might contain already the value the function was looking for. This patch changes libxl_wait_for_device_model so that it checks the value of the xenstore node first, then waits for the watch.

Quoted 4 lines

That can't help because it's still racy: what if the value changes between the first check and the wait? The watch must fire immediately if the value is already in the desired state, or there's an unavoidable deadlock.

The check is done after the watch is set, so the wait would return immediately.

Quoted 3 lines

On the other hand, the check-then-wait pattern reads more clearly, I think.

I agree on this.

#7 lore raw
Quoted 3 lines · Stefano Stabellini

The check is done after the watch is set, so the wait would return immediately.

Yes, realized that just after sending.

J