<html><body>
<p><tt>Wayne Xia <xiawenc@linux.vnet.ibm.com> wrote on 08/29/2011 02:47:34 AM:<br>
<br>
> Wayne Xia <xiawenc@linux.vnet.ibm.com> </tt><br>
<tt>> 08/29/11 02:47 AM</tt><br>
<tt>> <br>
> To</tt><br>
<tt>> <br>
> libvirt-cim@redhat.com, Sharad Mishra/Beaverton/IBM@IBMUS</tt><br>
<tt>> <br>
> cc</tt><br>
<tt>> <br>
> Subject</tt><br>
<tt>> <br>
> Re: [Libvirt-cim] [PATCH] Workaround to fix race condition around libvirt init</tt><br>
<br>
<br>
<tt>Thanks for the comment, Wayne.</tt><br>
<br>
<tt>> <br>
> Is it possible that libvirt would invoke one VM twice?</tt><br>
<br>
<tt>that will be a bug. I have never seen that happen.</tt><br>
<tt><br>
> <br>
> also some comments below.<br>
> <br>
> $BP2(B 2011-8-27 1:32, Sharad Mishra $B<LF;(B:<br>
> > # HG changeset patch<br>
> > # User Sharad Mishra<snmishra@us.ibm.com><br>
> > # Date 1314379933 25200<br>
> > # Node ID a42b68361ed9cb4f7d6f15de5b58ccc68e88ef38<br>
> > # Parent a346baf140d64177a9dc1066677c307ee6518236<br>
> > Workaround to fix race condition around libvirt init.<br>
> ><br>
> > This patch fixes the race condition caused when mutiple<br>
> > threads try to start a VM. This patch also fixes the issue<br>
> > of incorrect mem allocation for VSSD property - emulator.<br>
> ><br>
> > Signed-off-by: Sharad Mishra<snmishra@us.ibm.com><br>
> ><br>
> > diff --git a/libxkutil/misc_util.c b/libxkutil/misc_util.c<br>
> > --- a/libxkutil/misc_util.c<br>
> > +++ b/libxkutil/misc_util.c<br>
> > @@ -28,6 +28,7 @@<br>
> > #include<stdbool.h><br>
> > #include<stdarg.h><br>
> > #include<unistd.h><br>
> > +#include<pthread.h><br>
> > #include<libvirt/libvirt.h><br>
> > #include<libvirt/virterror.h><br>
> ><br>
> > @@ -45,6 +46,9 @@<br>
> > #include "misc_util.h"<br>
> > #include "cs_util.h"<br>
> ><br>
> > +static pthread_mutex_t libvirt_mutex = PTHREAD_MUTEX_INITIALIZER;<br>
> > +/* libvirt library not initialized */<br>
> > +static int libvirt_initialized = 0;<br>
> ><br>
> > #define URI_ENV "HYPURI"<br>
> ><br>
> > @@ -114,11 +118,15 @@<br>
> ><br>
> > CU_DEBUG("Connecting to libvirt with uri `%s'", uri);<br>
> ><br>
> > + pthread_mutex_lock(&libvirt_mutex);<br>
> > +<br>
> > if (is_read_only())<br>
> > conn = virConnectOpenReadOnly(uri);<br>
> > else<br>
> > conn = virConnectOpen(uri);<br>
> ><br>
> > + pthread_mutex_unlock(&libvirt_mutex);<br>
> > +<br>
> > if (!conn) {<br>
> > CU_DEBUG("Unable to connect to `%s'", uri);<br>
> > return NULL;<br>
> > @@ -530,7 +538,19 @@<br>
> ><br>
> > bool libvirt_cim_init(void)<br>
> > {<br>
> > - return virInitialize() == 0;<br>
> > + int ret=0;<br>
> > +<br>
> > + /* double-check lock pattern used for performance reasons */<br>
> > + if (0 == libvirt_initialized) {<br>
> > + pthread_mutex_lock(&libvirt_mutex);<br>
> > + if (0 == libvirt_initialized) {<br>
> > + ret = virInitialize();<br>
> > + if (ret == 0)<br>
> > + libvirt_initialized=1;<br>
> > + }<br>
> > + pthread_mutex_unlock(&libvirt_mutex);<br>
> > + }<br>
> > + return (ret == 0);<br>
> > }<br>
> ><br>
> > bool check_refs_pfx_match(const CMPIObjectPath *refa,<br>
> > diff --git a/src/Virt_VirtualSystemManagementService.c b/src/<br>
> Virt_VirtualSystemManagementService.c<br>
> > --- a/src/Virt_VirtualSystemManagementService.c<br>
> > +++ b/src/Virt_VirtualSystemManagementService.c<br>
> > @@ -188,6 +188,20 @@<br>
> > return 1;<br>
> > }<br>
> ><br>
> > +static bool make_space(struct virt_device **list, int cur, int new)<br>
> > +{<br>
> > + struct virt_device *tmp;<br>
> > +<br>
> > + tmp = calloc(cur + new, sizeof(*tmp));<br>
> > + if (tmp == NULL)<br>
> > + return false;<br>
> > +<br>
> maybe some lines should be added here like this:<br>
> <br>
> if (*list!=NULL) {<br>
> memcpy(tmp, *list, sizeof(*tmp) * cur);<br>
> free(*list);<br>
> *list = tmp;<br>
> }<br>
> </tt><br>
<br>
<tt>Eduardo already submitted a patch for this. I will redo this patch to include Eduardo's fix.</tt><br>
<tt><br>
> > + memcpy(tmp, *list, sizeof(*tmp) * cur);<br>
> > + *list = tmp;<br>
> > +<br>
> > + return true;<br>
> > +}<br>
> > +<br>
> > static bool fv_set_emulator(struct domain *domain,<br>
> > const char *emu)<br>
> > {<br>
> > @@ -198,6 +212,11 @@<br>
> > if (emu == NULL)<br>
> > return true;<br>
> ><br>
> > + if (!make_space(&domain->dev_emu, 0, 1)) {<br>
> > + CU_DEBUG("Failed to alloc disk list");<br>
> > + return false;<br>
> > + }<br>
> > +<br>
> > cleanup_virt_device(domain->dev_emu);<br>
> ><br>
> > domain->dev_emu->type = CIM_RES_TYPE_EMU;<br>
> > @@ -1369,20 +1388,6 @@<br>
> > return msg;<br>
> > }<br>
> ><br>
> > -static bool make_space(struct virt_device **list, int cur, int new)<br>
> > -{<br>
> > - struct virt_device *tmp;<br>
> > -<br>
> > - tmp = calloc(cur + new, sizeof(*tmp));<br>
> > - if (tmp == NULL)<br>
> > - return false;<br>
> > -<br>
> > - memcpy(tmp, *list, sizeof(*tmp) * cur);<br>
> > - *list = tmp;<br>
> > -<br>
> > - return true;<br>
> > -}<br>
> > -<br>
> > static char *add_device_nodup(struct virt_device *dev,<br>
> > struct virt_device *list,<br>
> > int max,<br>
> ><br>
> > _______________________________________________<br>
> > Libvirt-cim mailing list<br>
> > Libvirt-cim@redhat.com<br>
> > <a href="https://www.redhat.com/mailman/listinfo/libvirt-cim">https://www.redhat.com/mailman/listinfo/libvirt-cim</a><br>
> <br>
> <br>
> -- <br>
> Best Regards<br>
> <br>
> Wayne Xia<br>
> mail:xiawenc@linux.vnet.ibm.com<br>
> tel:86-010-82450803<br>
> <br>
</tt></body></html>