[libvirt] [PATCH V5] Add libxenlight driver

Markus Groß gross at univention.de
Wed Mar 16 12:52:49 UTC 2011


Am Donnerstag 10 März 2011 07:45:49 schrieb Jim Fehlig:
> Add a new xen driver based on libxenlight [1], which is the primary
> toolstack starting with Xen 4.1.0.  The driver is stateful, runs
> privileged only, and is accessed with libxl:/// URI.
>
> V5:
>  - Ensure events are unregistered when domain private data
>    is destroyed.  Discovered and fixed by Markus Gross.
>
> V4:
>  - Handle restart of libvirtd, reconnecting to previously
>    started domains
>  - Rebased to current master
>  - Tested against Xen 4.1 RC7-pre (c/s 22961:c5d121fd35c0)
>
> V3:
>   - Reserve vnc port within driver when autoport=yes
>
> V2:
>   - Update to Xen 4.1 RC6-pre (c/s 22940:5a4710640f81)
>   - Rebased to current master
>   - Plug memory leaks found by Stefano Stabellini and valgrind
>   - Handle SHUTDOWN_crash domain death event
>
> [1]
> http://lists.xensource.com/archives/html/xen-devel/2009-11/msg00436.html

The libxlEventHandler needs to hold an extra reference to the 
corresponding "vm" object.
Otherwise the handler will try to access an invalid vm object under certain 
race conditions.

I fixed it using the following patch.
Now the libxl driver keeps an extra reference of the vm object for the event 
handler and specifies a free callback function.
This free callback function removes the reference to the vm object to avoid a 
memory leak.

diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 7559949..30f3700 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -235,6 +235,14 @@ cleanup:
     libxl_free_event(&event);
 }

+static void
+libxlDomainUnwatch(void *data)
+{
+    virDomainObjPtr vm = data;
+    /* Remove reference from handler */
+    virDomainObjUnref(vm);
+}
+
 /*
  * Register domain events with libxenlight and insert event handles
  * in libvirt's event loop.
@@ -257,17 +265,20 @@ libxlCreateDomEvents(virDomainObjPtr vm)
     if (fd < 0)
         goto error;

+    /* Hold an extra reference for the event handler */
+    virDomainObjRef(vm);
     priv->waiterFD = fd;
     if ((priv->eventHdl = virEventAddHandle(
              fd,
              VIR_EVENT_HANDLE_READABLE | VIR_EVENT_HANDLE_ERROR,
              libxlEventHandler,
-             vm, NULL)) < 0)
+             vm, libxlDomainUnwatch)) < 0)
         goto error;

     return 0;

 error:
+    virDomainObjUnref(vm);
     libxl_free_waiter(priv->dWaiter);
     VIR_FREE(priv->dWaiter);
     priv->eventHdl = -1;




More information about the libvir-list mailing list