Skip to contentSkip to conversation

libxl: fixes for device addition/removal

PATCH In review Roger Pau Monne 3 participants 12 messages Jul 26, 2012 – Jul 31, 2012

lore
Share & tools
lore
#0 lore raw

cover libxl: fixes for device addition/removal

Fixes for two regresions introduced by the hotplug series.

[PATCH 1/2] libxl: fix race in libxl__devices_destroy [PATCH 2/2] libxl: allow to create a domain without disks

#1 lore raw

PATCH 1/2 libxl: fix race in libxl__devices_destroy

Don't have a fixed number of devices in the aodevs array, and instead size it depending on the devices present in xenstore. Also change the console destroy path to a switch instead of an if.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxl_device.c   |  123 ++++++++++++++++++------------------------
 tools/libxl/libxl_internal.h |   10 +++-
 2 files changed, 62 insertions(+), 71 deletions(-)
tools/libxl/libxl_device.c +53 -70
index da0c3ea..4667261 100644
@@ -58,50 +58,6 @@ int libxl__parse_backend_path(libxl__gc *gc,
     return libxl__device_kind_from_string(strkind, &dev->backend_kind);
 }
 
-static int libxl__num_devices(libxl__gc *gc, uint32_t domid)
-{
-    char *path;
-    unsigned int num_kinds, num_devs;
-    char **kinds = NULL, **devs = NULL;
-    int i, j, rc = 0;
-    libxl__device dev;
-    libxl__device_kind kind;
-    int numdevs = 0;
-
-    path = GCSPRINTF("/local/domain/%d/device", domid);
-    kinds = libxl__xs_directory(gc, XBT_NULL, path, &num_kinds);
-    if (!kinds) {
-        if (errno != ENOENT) {
-            LOGE(ERROR, "unable to get xenstore device listing %s", path);
-            rc = ERROR_FAIL;
-            goto out;
-        }
-        num_kinds = 0;
-    }
-    for (i = 0; i < num_kinds; i++) {
-        if (libxl__device_kind_from_string(kinds[i], &kind))
-            continue;
-        if (kind == LIBXL__DEVICE_KIND_CONSOLE)
-            continue;
-
-        path = GCSPRINTF("/local/domain/%d/device/%s", domid, kinds[i]);
-        devs = libxl__xs_directory(gc, XBT_NULL, path, &num_devs);
-        if (!devs)
-            continue;
-        for (j = 0; j < num_devs; j++) {
-            path = GCSPRINTF("/local/domain/%d/device/%s/%s/backend",
-                             domid, kinds[i], devs[j]);
-            path = libxl__xs_read(gc, XBT_NULL, path);
-            if (path && libxl__parse_backend_path(gc, path, &dev) == 0) {
-                numdevs++;
-            }
-        }
-    }
-out:
-    if (rc) return rc;
-    return numdevs;
-}
-
 int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype)
 {
     char *snictype, *be_path;
@@ -451,9 +407,15 @@ void libxl__prepare_ao_devices(libxl__ao *ao, libxl__ao_devices *aodevs)
 
     GCNEW_ARRAY(aodevs->array, aodevs->size);
     for (int i = 0; i < aodevs->size; i++) {
-        aodevs->array[i].aodevs = aodevs;
-        libxl__prepare_ao_device(ao, &aodevs->array[i]);
+        GCNEW(aodevs->array[i]);
+        aodevs->array[i]->aodevs = aodevs;
+        libxl__prepare_ao_device(ao, aodevs->array[i]);
     }
+    /*
+     * Since we have already added all the aodevs, and marked them
+     * as active, we can mark the addition operation as finished.
+     */
+    aodevs->in_addition = 0;
 }
 
 void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
@@ -463,12 +425,15 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
     int i, error = 0;
 
     aodev->active = 0;
+    if (aodevs->in_addition)
+        return;
+
     for (i = 0; i < aodevs->size; i++) {
-        if (aodevs->array[i].active)
+        if (aodevs->array[i]->active)
             return;
 
-        if (aodevs->array[i].rc)
-            error = aodevs->array[i].rc;
+        if (aodevs->array[i]->rc)
+            error = aodevs->array[i]->rc;
     }
 
     aodevs->callback(egc, aodevs, error);
@@ -495,9 +460,9 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
         int i;                                                                 \
         int end = start + d_config->num_##type##s;                             \
         for (i = start; i < end; i++) {                                        \
-            aodevs->array[i].callback = libxl__ao_devices_callback;            \
+            aodevs->array[i]->callback = libxl__ao_devices_callback;           \
             libxl__device_##type##_add(egc, domid, &d_config->type##s[i-start],\
-                                       &aodevs->array[i]);                     \
+                                       aodevs->array[i]);                      \
         }                                                                      \
     }
 
@@ -545,20 +510,20 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)
     char *path;
     unsigned int num_kinds, num_dev_xsentries;
     char **kinds = NULL, **devs = NULL;
-    int i, j, numdev = 0, rc = 0;
+    int i, j, rc = 0;
     libxl__device *dev;
     libxl__ao_devices *aodevs = &drs->aodevs;
     libxl__ao_device *aodev;
     libxl__device_kind kind;
 
-    aodevs->size = libxl__num_devices(gc, drs->domid);
-    if (aodevs->size < 0) {
-        LOG(ERROR, "unable to get number of devices for domain %u", drs->domid);
-        rc = aodevs->size;
-        goto out;
-    }
-
-    libxl__prepare_ao_devices(drs->ao, aodevs);
+    /*
+     * Initialize values of the aodevs structure manually,
+     * since we don't know the number of devices we are going
+     * to unplug yet.
+     */
+    aodevs->in_addition = 1;
+    aodevs->size = 0;
+    aodevs->array = NULL;
     aodevs->callback = devices_remove_callback;
 
     path = libxl__sprintf(gc, "/local/domain/%d/device", domid);
@@ -588,21 +553,28 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)
                 dev->domid = domid;
                 dev->kind = kind;
                 dev->devid = atoi(devs[j]);
-                if (dev->backend_kind == LIBXL__DEVICE_KIND_CONSOLE) {
+                switch (dev->backend_kind) {
+                case LIBXL__DEVICE_KIND_CONSOLE:
                     /* Currently console devices can be destroyed
                      * synchronously by just removing xenstore entries,
                      * this is what libxl__device_destroy does.
                      */
                     libxl__device_destroy(gc, dev);
-                    continue;
+                    break;
+                default:
+                    GCREALLOC_ARRAY(aodevs->array, aodevs->size + 1);
+                    GCNEW(aodev);
+                    aodevs->array[aodevs->size] = aodev;
+                    libxl__prepare_ao_device(ao, aodev);
+                    aodev->aodevs = aodevs;
+                    aodev->action = DEVICE_DISCONNECT;
+                    aodev->dev = dev;
+                    aodev->callback = libxl__ao_devices_callback;
+                    aodev->force = drs->force;
+                    libxl__initiate_device_remove(egc, aodev);
+                    aodevs->size++;
+                    break;
                 }
-                aodev = &aodevs->array[numdev];
-                aodev->action = DEVICE_DISCONNECT;
-                aodev->dev = dev;
-                aodev->callback = libxl__ao_devices_callback;
-                aodev->force = drs->force;
-                libxl__initiate_device_remove(egc, aodev);
-                numdev++;
             }
         }
     }
@@ -624,7 +596,18 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)
     }
 
 out:
-    if (!numdev) drs->callback(egc, drs, rc);
+    if (!aodevs->size) drs->callback(egc, drs, rc);
+    else {
+        /*
+         * Create a dummy aodev to check if all the other aodevs are finished,
+         * and call the callback.
+         */
+        GCNEW(aodev);
+        libxl__prepare_ao_device(ao, aodev);
+        aodev->aodevs = aodevs;
+        aodevs->in_addition = 0;
+        libxl__ao_devices_callback(egc, aodev);
+    }
     return;
 }
 
tools/libxl/libxl_internal.h +9 -1
index 3e91529..3ee3a09 100644
@@ -1808,6 +1808,8 @@ _hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
 /* Prepare a bunch of devices for addition/removal. Every ao_device in
  * ao_devices is set to 'active', and the ao_device 'base' field is set to
  * the one pointed by aodevs.
+ * Once this function is called, the aodevs->size value cannot be changed,
+ * and no further aodevs can be added to the array.
  */
 _hidden void libxl__prepare_ao_devices(libxl__ao *ao,
                                        libxl__ao_devices *aodevs);
@@ -1849,9 +1851,15 @@ struct libxl__ao_device {
  */
 typedef void libxl__devices_callback(libxl__egc*, libxl__ao_devices*, int rc);
 struct libxl__ao_devices {
-    libxl__ao_device *array;
+    libxl__ao_device **array;
     int size;
     libxl__devices_callback *callback;
+    /*
+     * if in_addition == 1 means we are currently adding devices
+     * to the array, if in_addition == 0, means we have added all
+     * the devices.
+     */
+    int in_addition;
 };
 
 /*
Signature · 1 line
1.7.7.5 (Apple Git-26)
#2 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

Quoted 44 lines · Roger Pau Monne

Don't have a fixed number of devices in the aodevs array, and instead size it depending on the devices present in xenstore. Also change the console destroy path to a switch instead of an if.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxl_device.c   |  123 ++++++++++++++++++------------------------
 tools/libxl/libxl_internal.h |   10 +++-
 2 files changed, 62 insertions(+), 71 deletions(-)
tools/libxl/libxl_device.c +8 -8
index da0c3ea..4667261 100644
 int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype)
 {
     char *snictype, *be_path;
@@ -451,9 +407,15 @@ void libxl__prepare_ao_devices(libxl__ao *ao, libxl__ao_devices *aodevs)
 
     GCNEW_ARRAY(aodevs->array, aodevs->size);
     for (int i = 0; i < aodevs->size; i++) {
-        aodevs->array[i].aodevs = aodevs;
-        libxl__prepare_ao_device(ao, &aodevs->array[i]);
+        GCNEW(aodevs->array[i]);
+        aodevs->array[i]->aodevs = aodevs;
+        libxl__prepare_ao_device(ao, aodevs->array[i]);
     }
+    /*
+     * Since we have already added all the aodevs, and marked them
+     * as active, we can mark the addition operation as finished.
+     */
+    aodevs->in_addition = 0;
 }
 
 void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
 
-    aodevs->size = libxl__num_devices(gc, drs->domid);
-    if (aodevs->size < 0) {
-        LOG(ERROR, "unable to get number of devices for domain %u", drs->domid);
-        rc = aodevs->size;
-        goto out;
-    }

tools/libxl/libxl_device.c void libxl__prepare_ao_devices(libxl__ao *ao, libxl__ao_devices *aodevs)

Quoted 10 lines
tools/libxl/libxl_device.c +8 -2
-
-    libxl__prepare_ao_devices(drs->ao, aodevs);
+    /*
+     * Initialize values of the aodevs structure manually,
+     * since we don't know the number of devices we are going
+     * to unplug yet.
+     */
+    aodevs->in_addition = 1;
+    aodevs->size = 0;
+    aodevs->array = NULL;

This suggests to me that libxl__prepare_ao_devices should take the size as an argument and initialise that too, setting the array to NULL or allocating it as appropriate.

Quoted 12 lines
     aodevs->callback = devices_remove_callback;
 
     path = libxl__sprintf(gc, "/local/domain/%d/device", domid);
tools/libxl/libxl_device.c +2 -1
@@ -588,21 +553,28 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)
                 dev->domid = domid;
                 dev->kind = kind;
                 dev->devid = atoi(devs[j]);
-                if (dev->backend_kind == LIBXL__DEVICE_KIND_CONSOLE) {
+                switch (dev->backend_kind) {
+                case LIBXL__DEVICE_KIND_CONSOLE:
                     /* Currently console devices can be destroyed
                      * synchronously by just removing xenstore entries,

tools/libxl/libxl_device.c void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs) · line 568

Quoted 10 lines
tools/libxl/libxl_device.c +6 -1
                      * this is what libxl__device_destroy does.
                      */
                     libxl__device_destroy(gc, dev);
-                    continue;
+                    break;
+                default:
+                    GCREALLOC_ARRAY(aodevs->array, aodevs->size + 1);
+                    GCNEW(aodev);
+                    aodevs->array[aodevs->size] = aodev;
+                    libxl__prepare_ao_device(ao, aodev);

You could make this return a new aodev,

			aodev = libxl__prepare_ao_device(ao);

?

You might even encapsulate the array realloc into
aodev = libxl__ao_devices_append(aodevs)
?

Quoted 26 lines
tools/libxl/libxl_device.c +10 -8
+                    aodev->aodevs = aodevs;
+                    aodev->action = DEVICE_DISCONNECT;
+                    aodev->dev = dev;
+                    aodev->callback = libxl__ao_devices_callback;
+                    aodev->force = drs->force;
+                    libxl__initiate_device_remove(egc, aodev);
+                    aodevs->size++;
+                    break;
                 }
-                aodev = &aodevs->array[numdev];
-                aodev->action = DEVICE_DISCONNECT;
-                aodev->dev = dev;
-                aodev->callback = libxl__ao_devices_callback;
-                aodev->force = drs->force;
-                libxl__initiate_device_remove(egc, aodev);
-                numdev++;
             }
         }
     }
@@ -624,7 +596,18 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)
     }
 
 out:
-    if (!numdev) drs->callback(egc, drs, rc);
+    if (!aodevs->size) drs->callback(egc, drs, rc);
+    else {

tools/libxl/libxl_device.c void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs) · line 610

Quoted 10 lines
tools/libxl/libxl_device.c +10 -0
+        /*
+         * Create a dummy aodev to check if all the other aodevs are finished,
+         * and call the callback.
+         */
+        GCNEW(aodev);
+        libxl__prepare_ao_device(ao, aodev);
+        aodev->aodevs = aodevs;
+        aodevs->in_addition = 0;
+        libxl__ao_devices_callback(egc, aodev);
+    }

This dummy event strikes me as a bit odd.

Would it not be possible to loop over xenstore creating the aodevs array and only then have a second loop which calls libxl__initiate_device_remove(egc, aodev) for each in turn? Probably you would encapsulate this into a helper libxl__as_devices_initiate which would call the callback if size == 0.

That would do away with this whole "in_addition" phase since you wouldn't actually initiate anything until you had created the entire list.

Quoted 33 lines
tools/libxl/libxl_device.c +0 -0
     return;
 }
 
tools/libxl/libxl_internal.h +9 -1
index 3e91529..3ee3a09 100644
@@ -1808,6 +1808,8 @@ _hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
 /* Prepare a bunch of devices for addition/removal. Every ao_device in
  * ao_devices is set to 'active', and the ao_device 'base' field is set to
  * the one pointed by aodevs.
+ * Once this function is called, the aodevs->size value cannot be changed,
+ * and no further aodevs can be added to the array.
  */
 _hidden void libxl__prepare_ao_devices(libxl__ao *ao,
                                        libxl__ao_devices *aodevs);
@@ -1849,9 +1851,15 @@ struct libxl__ao_device {
  */
 typedef void libxl__devices_callback(libxl__egc*, libxl__ao_devices*, int rc);
 struct libxl__ao_devices {
-    libxl__ao_device *array;
+    libxl__ao_device **array;
     int size;
     libxl__devices_callback *callback;
+    /*
+     * if in_addition == 1 means we are currently adding devices
+     * to the array, if in_addition == 0, means we have added all
+     * the devices.
+     */
+    int in_addition;
 };
 
 /*
#3 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy"):

Quoted 1 line · Roger Pau Monne

...

Quoted 15 lines
Quoted 4 lines

out:

tools/libxl/libxl_device.c +2 -1
-    if (!numdev) drs->callback(egc, drs, rc);
+    if (!aodevs->size) drs->callback(egc, drs, rc);
+    else {

tools/libxl/libxl_device.c void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs) · line 610

Quoted 10 lines
tools/libxl/libxl_device.c +10 -0
+        /*
+         * Create a dummy aodev to check if all the other aodevs are finished,
+         * and call the callback.
+         */
+        GCNEW(aodev);
+        libxl__prepare_ao_device(ao, aodev);
+        aodev->aodevs = aodevs;
+        aodevs->in_addition = 0;
+        libxl__ao_devices_callback(egc, aodev);
+    }
Quoted 1 line

This dummy event strikes me as a bit odd.

It was my suggestion. However Roger hasn't done quite what I intended to suggest.

If we were to _first_ create an aodev which represents the scan for devices, then when we complete the scan we can simply complete that aodev in the entirely normal way and everything will work correctly.

Quoted 5 lines

Would it not be possible to loop over xenstore creating the aodevs array and only then have a second loop which calls libxl__initiate_device_remove(egc, aodev) for each in turn? Probably you would encapsulate this into a helper libxl__as_devices_initiate which would call the callback if size == 0.

IMO it's generally better to do everything at once, rather than multiple parallel loops. That avoids (a) races (b) mistakes arising from changes in one place but not the other.

Quoted 3 lines

That would do away with this whole "in_addition" phase since you wouldn't actually initiate anything until you had created the entire list.

Using an aodev to represent this, instead of an ad hoc "in_addition", would regularise this too.

I'll reply more fully to Roger's patches later.

Ian.

#4 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

In reply to Roger Pau Monne

Roger Pau Monne writes ("[PATCH 1/2] libxl: fix race in libxl__devices_destroy"):

Quoted 3 lines

Don't have a fixed number of devices in the aodevs array, and instead size it depending on the devices present in xenstore. Also change the console destroy path to a switch instead of an if.

...

tools/libxl/libxl_device.c void libxl__prepare_ao_devices(libxl__ao *ao, libxl__ao_devices *aodevs) · line 412

Quoted 7 lines
     GCNEW_ARRAY(aodevs->array, aodevs->size);
     for (int i = 0; i < aodevs->size; i++) {
tools/libxl/libxl_device.c +3 -2
-        aodevs->array[i].aodevs = aodevs;
-        libxl__prepare_ao_device(ao, &aodevs->array[i]);
+        GCNEW(aodevs->array[i]);
+        aodevs->array[i]->aodevs = aodevs;
+        libxl__prepare_ao_device(ao, aodevs->array[i]);

I agree with Ian C's comments on this.

tools/libxl/libxl_device.c void libxl__prepare_ao_devices(libxl__ao *ao, libxl__ao_devices *aodevs) · line 418

Quoted 5 lines
tools/libxl/libxl_device.c +5 -0
+    /*
+     * Since we have already added all the aodevs, and marked them
+     * as active, we can mark the addition operation as finished.
+     */
+    aodevs->in_addition = 0;

And this is rather messy; it requires thinking about to make sure everything is correct.

How about making libxl__prepare_ao_devices automatically create the "we are still searching for stuff to do" aodev ?

You might want a
libxl__finished_preparing_ao_devices
to hide the "searching" ao entirely.

If you do it this way then the "no disks" corner case comes out in the wash too.

Quoted 1 line
(quoted diff) +0 -0
@@ -588,21 +553,28 @@ void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)

...

tools/libxl/libxl_device.c void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs) · line 567

Quoted 4 lines
tools/libxl/libxl_device.c +4 -0
+                default:
+                    GCREALLOC_ARRAY(aodevs->array, aodevs->size + 1);
+                    GCNEW(aodev);
+                    aodevs->array[aodevs->size] = aodev;

Open coding this is pretty ugly. I agree with Ian C that libxl__prepare_ao_device should do this.

Ian.

#5 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

Ian Jackson writes ("Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy"):

Quoted 2 lines

How about making libxl__prepare_ao_devices automatically create the "we are still searching for stuff to do" aodev ?

Here is what I came up with. I have compiled it but not tested it yet. Despite me introducing some new functions the number of lines of code is unchanged - indeed, it shrinks if you don't count comments :-).

Ian.

From: Ian Jackson <[email protected]>
Subject: [PATCH RFC] libxl: fix device counting race in libxl__devices_destroy

Don't have a fixed number of devices in the aodevs array, and instead size it depending on the devices present in xenstore. Somewhat formalise the multiple device addition/removal machinery to make this clearer and easier to do.

As a side-effect we fix a few "lost thread of control" bug which would occur if there were no devices of a particular kind. (Various if statements which checked for there being no devices have become redundant, but are retained to avoid making the patch bigger.)

Specifically:

* Users of libxl__ao_devices are no longer expected to know in advance how many device operations they are going to do. Instead they can initiate them one at a time, between bracketing calls to "begin" and "prepared".

* The array of aodevs used for this is dynamically sized; to support this it's an array of pointers rather than of structs.

* Users of libxl__ao_devices are presented with a more opaque interface. They are are no longer expected to, themselves,
- look into the array of aodevs (this is now private)
- know that the individual addition/removal completions are handled by libxl__ao_devices_callback (this callback function is now a private function for the multidev machinery)
- ever deal with populating the contents of an aodevs

* The doc comments relating to some of the members of libxl__ao_device are clarified. (And the member `aodevs' is moved to put it with the other members with the same status.)

* The multidev machinery allocates an aodev to represent the operation of preparing all of the other operations. See the comment in libxl__multidev_begin.

A wrinkle is that the functions are called "multidev" but the structs are called "libxl__ao_devices" and "aodevs". I have given these functions this name to distinguish them from "libxl__ao_device" and "aodev" and so forth by more than just the use of the plural "s" suffix. Perhaps eventually we will rename the structs.

-
Changes in v3:
* New multidev interfaces - extensive changes.

---
 tools/libxl/libxl_create.c   |    8 +-
 tools/libxl/libxl_device.c   |  128 +++++++++++++++++++-----------------------
 tools/libxl/libxl_dm.c       |    8 +-
 tools/libxl/libxl_internal.h |   66 +++++++++++++---------
 4 files changed, 105 insertions(+), 105 deletions(-)
tools/libxl/libxl_create.c +4 -4
index aafacd8..3265d69 100644
@@ -909,10 +909,10 @@ static void domcreate_rebuild_done(libxl__egc *egc,
 
     store_libxl_entry(gc, domid, &d_config->b_info);
 
-    dcs->aodevs.size = d_config->num_disks;
+    libxl__multidev_begin(ao, &dcs->aodevs);
     dcs->aodevs.callback = domcreate_launch_dm;
-    libxl__prepare_ao_devices(ao, &dcs->aodevs);
     libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
+    libxl__multidev_prepared(egc, &dcs->aodevs, 0);
 
     return;
 
@@ -1039,10 +1039,10 @@ static void domcreate_devmodel_started(libxl__egc *egc,
     /* Plug nic interfaces */
     if (d_config->num_nics > 0) {
         /* Attach nics */
-        dcs->aodevs.size = d_config->num_nics;
+        libxl__multidev_begin(ao, &dcs->aodevs);
         dcs->aodevs.callback = domcreate_attach_pci;
-        libxl__prepare_ao_devices(ao, &dcs->aodevs);
         libxl__add_nics(egc, ao, domid, 0, d_config, &dcs->aodevs);
+        libxl__multidev_prepared(egc, &dcs->aodevs, 0);
         return;
     }
 
tools/libxl/libxl_device.c +57 -71

197 lines not on this page · raw message

tools/libxl/libxl_dm.c +4 -4
index f2e9572..177642b 100644
@@ -856,10 +856,10 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    sdss->aodevs.size = dm_config->num_disks;
+    libxl__multidev_begin(ao, &sdss->aodevs);
     sdss->aodevs.callback = spawn_stub_launch_dm;
-    libxl__prepare_ao_devices(ao, &sdss->aodevs);
     libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
+    libxl__multidev_prepared(egc, &sdss->aodevs, 0);
 
     free(args);
     return;
@@ -982,10 +982,10 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
     if (rc) goto out;
 
     if (d_config->num_nics > 0) {
-        sdss->aodevs.size = d_config->num_nics;
+        libxl__multidev_begin(ao, &sdss->aodevs);
         sdss->aodevs.callback = stubdom_pvqemu_cb;
-        libxl__prepare_ao_devices(ao, &sdss->aodevs);
         libxl__add_nics(egc, ao, dm_domid, 0, d_config, &sdss->aodevs);
+        libxl__multidev_prepared(egc, &sdss->aodevs, 0);
         return;
     }
 
tools/libxl/libxl_internal.h +41 -27

93 lines not on this page · raw message

Signature · 1 line
tg: (45b79a7..) t/xen/xl.devnum-race (depends on: t/xen/xl.pty-pollhup)
#6 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

tools/libxl/libxl_device.c void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev) · line 471

Quoted 4 lines · Ian Jackson
tools/libxl/libxl_device.c +4 -0
+void libxl__multidev_prepared(libxl__egc *egc, libxl__ao_devices *aodevs,
+                              int rc)
+{
+    multidev_one_callback(egc, aodevs->preparation);

Don't we need to propagate rc here? Perhaps with

	aodevs->preparation->rc= rc

?

Quoted 5 lines
tools/libxl/libxl_internal.h +0 -0
index f0c94e8..7072b09 100644
@@ -1810,20 +1810,6 @@ typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);

[...]

Quoted 4 lines

+/*
+ * Multiple devices "multidevs" handling.
+ *
+ * Firstly, you should

Quoted 8 lines

+ * libxl__multidev_begin
+ * multidevs->callback = ...
+ * Then zero or more times
+ * libxl__multidev_prepare
+ * libal__initiate_device_{remove/addition}.
+ * Finally, once
+ * libxl__multidev_prepared
+ * which will result (perhaps reentrantly) in one call to callback().

Briefly mention the multidev vs ao_devices naming? Or just do the rename now in a new patch.

Ian.

#7 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy"):

Quoted 10 lines · Ian Jackson

tools/libxl/libxl_device.c void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev) · line 471

Quoted 5 lines
tools/libxl/libxl_device.c +4 -0
+void libxl__multidev_prepared(libxl__egc *egc, libxl__ao_devices *aodevs,
+                              int rc)
+{
+    multidev_one_callback(egc, aodevs->preparation);

Don't we need to propagate rc here? Perhaps with

	aodevs->preparation->rc= rc

?

Yes, good catch. This pattern where we have to explicitly assign to the rc in the aodev is perhaps less than ideal. I spotted a similar bug in one of Roger's patches.

I wonder if there's a way to get around this.

Quoted 19 lines
Quoted 5 lines
tools/libxl/libxl_internal.h +0 -0
index f0c94e8..7072b09 100644
@@ -1810,20 +1810,6 @@ typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);

[...]

Quoted 4 lines

+/*
+ * Multiple devices "multidevs" handling.
+ *
+ * Firstly, you should

Quoted 8 lines

+ * libxl__multidev_begin
+ * multidevs->callback = ...
+ * Then zero or more times
+ * libxl__multidev_prepare
+ * libal__initiate_device_{remove/addition}.
+ * Finally, once
+ * libxl__multidev_prepared
+ * which will result (perhaps reentrantly) in one call to callback().

Quoted 2 lines

Briefly mention the multidev vs ao_devices naming? Or just do the rename now in a new patch.

If you're happy with that renaming now then I will do the latter.

Ian.

#8 lore raw

Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy

Quoted 37 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix race in libxl__devices_destroy"):

Quoted 10 lines · Ian Jackson

tools/libxl/libxl_device.c void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev) · line 471

Quoted 5 lines
tools/libxl/libxl_device.c +4 -0
+void libxl__multidev_prepared(libxl__egc *egc, libxl__ao_devices *aodevs,
+                              int rc)
+{
+    multidev_one_callback(egc, aodevs->preparation);

Don't we need to propagate rc here? Perhaps with

	aodevs->preparation->rc= rc

?

Yes, good catch. This pattern where we have to explicitly assign to the rc in the aodev is perhaps less than ideal. I spotted a similar bug in one of Roger's patches.

I wonder if there's a way to get around this.

Quoted 19 lines
Quoted 5 lines
tools/libxl/libxl_internal.h +0 -0
index f0c94e8..7072b09 100644
@@ -1810,20 +1810,6 @@ typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);

[...]

Quoted 12 lines

+/*
+ * Multiple devices "multidevs" handling.
+ *
+ * Firstly, you should
+ * libxl__multidev_begin
+ * multidevs->callback = ...
+ * Then zero or more times
+ * libxl__multidev_prepare
+ * libal__initiate_device_{remove/addition}.
+ * Finally, once
+ * libxl__multidev_prepared
+ * which will result (perhaps reentrantly) in one call to callback().

Quoted 4 lines
Quoted 2 lines

Briefly mention the multidev vs ao_devices naming? Or just do the rename now in a new patch.

If you're happy with that renaming now then I will do the latter.

It's mostly mechanical so its pretty safe. Lets do it.

#9 lore raw

PATCH 2/2 libxl: allow to create a domain without disks

In reply to Roger Pau Monne

Without this patch, the process gets stuck waiting for someone to call the callback, since no disk devices are added.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxl_create.c |   12 ++++++++----
 tools/libxl/libxl_dm.c     |   13 +++++++++----
 2 files changed, 17 insertions(+), 8 deletions(-)
tools/libxl/libxl_create.c +8 -4
index 5859c09..3f129bf 100644
@@ -907,11 +907,15 @@ static void domcreate_rebuild_done(libxl__egc *egc,
 
     store_libxl_entry(gc, domid, &d_config->b_info);
 
-    dcs->aodevs.size = d_config->num_disks;
-    dcs->aodevs.callback = domcreate_launch_dm;
-    libxl__prepare_ao_devices(ao, &dcs->aodevs);
-    libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
+    if (d_config->num_disks > 0) {
+        dcs->aodevs.size = d_config->num_disks;
+        dcs->aodevs.callback = domcreate_launch_dm;
+        libxl__prepare_ao_devices(ao, &dcs->aodevs);
+        libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
+        return;
+    }
 
+    domcreate_launch_dm(egc, &dcs->aodevs, 0);
     return;
 
  error_out:
tools/libxl/libxl_dm.c +9 -4
index f2e9572..667aec7 100644
@@ -856,11 +856,16 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    sdss->aodevs.size = dm_config->num_disks;
-    sdss->aodevs.callback = spawn_stub_launch_dm;
-    libxl__prepare_ao_devices(ao, &sdss->aodevs);
-    libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
+    if (dm_config->num_disks > 0) {
+        sdss->aodevs.size = dm_config->num_disks;
+        sdss->aodevs.callback = spawn_stub_launch_dm;
+        libxl__prepare_ao_devices(ao, &sdss->aodevs);
+        libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
+        free(args);
+        return;
+    }
 
+    spawn_stub_launch_dm(egc, &sdss->aodevs, 0);
     free(args);
     return;
 
Signature · 1 line
1.7.7.5 (Apple Git-26)
#10 lore raw

Re: [PATCH 2/2] libxl: allow to create a domain without disks

Quoted 22 lines · Roger Pau Monne

Without this patch, the process gets stuck waiting for someone to call the callback, since no disk devices are added.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxl_create.c |   12 ++++++++----
 tools/libxl/libxl_dm.c     |   13 +++++++++----
 2 files changed, 17 insertions(+), 8 deletions(-)
tools/libxl/libxl_create.c +0 -3
index 5859c09..3f129bf 100644
@@ -907,11 +907,15 @@ static void domcreate_rebuild_done(libxl__egc *egc,
 
     store_libxl_entry(gc, domid, &d_config->b_info);
 
-    dcs->aodevs.size = d_config->num_disks;
-    dcs->aodevs.callback = domcreate_launch_dm;
-    libxl__prepare_ao_devices(ao, &dcs->aodevs);

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 918

Quoted 10 lines
tools/libxl/libxl_create.c +8 -1
-    libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
+    if (d_config->num_disks > 0) {
+        dcs->aodevs.size = d_config->num_disks;
+        dcs->aodevs.callback = domcreate_launch_dm;
+        libxl__prepare_ao_devices(ao, &dcs->aodevs);
+        libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
+        return;
+    }
 
+    domcreate_launch_dm(egc, &dcs->aodevs, 0);

Would it make sense for libxl__add_FOO to detect aodevs.size == 0 and call the callback direct? Rather than open coding this pattern here, in the stub dom disk case and in both the NIC cases too?

Quoted 28 lines
tools/libxl/libxl_create.c +0 -0
     return;
 
  error_out:
tools/libxl/libxl_dm.c +9 -4
index f2e9572..667aec7 100644
@@ -856,11 +856,16 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    sdss->aodevs.size = dm_config->num_disks;
-    sdss->aodevs.callback = spawn_stub_launch_dm;
-    libxl__prepare_ao_devices(ao, &sdss->aodevs);
-    libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
+    if (dm_config->num_disks > 0) {
+        sdss->aodevs.size = dm_config->num_disks;
+        sdss->aodevs.callback = spawn_stub_launch_dm;
+        libxl__prepare_ao_devices(ao, &sdss->aodevs);
+        libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
+        free(args);
+        return;
+    }
 
+    spawn_stub_launch_dm(egc, &sdss->aodevs, 0);
     free(args);
     return;
 
#11 lore raw
In reply to Roger Pau Monne
Quoted 4 lines · Roger Pau Monne

Fixes for two regresions introduced by the hotplug series.

[PATCH 1/2] libxl: fix race in libxl__devices_destroy [PATCH 2/2] libxl: allow to create a domain without disks

Forgot to say, but the changes can also be found on:

git://xenbits.xen.org/people/royger/xen.git fixes_for_aodev

Thanks.