From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paolo Bonzini Subject: Re: [PATCH 1/2] hostmem: fix QEMU crash by 'info memdev' Date: Wed, 13 Jul 2016 12:45:26 +0200 Message-ID: <945b233e-286f-fc22-8837-761e1ac2e522@redhat.com> References: <1468383486-108169-1-git-send-email-guangrong.xiao@linux.intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Cc: ehabkost@redhat.com, kvm@vger.kernel.org, mst@redhat.com, gleb@kernel.org, mtosatti@redhat.com, qemu-devel@nongnu.org, Markus Armbruster , stefanha@redhat.com, rth@twiddle.net To: Xiao Guangrong , imammedo@redhat.com Return-path: In-Reply-To: <1468383486-108169-1-git-send-email-guangrong.xiao@linux.intel.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+gceq-qemu-devel=gmane.org@nongnu.org Sender: "Qemu-devel" List-Id: kvm.vger.kernel.org On 13/07/2016 06:18, Xiao Guangrong wrote: >=20 > Return MAX_NODES under this case to fix this bug >=20 > Signed-off-by: Xiao Guangrong > --- > backends/hostmem.c | 22 ++++++++++++++-------- > 1 file changed, 14 insertions(+), 8 deletions(-) >=20 > diff --git a/backends/hostmem.c b/backends/hostmem.c > index 6e28be1..8dede4d 100644 > --- a/backends/hostmem.c > +++ b/backends/hostmem.c > @@ -64,6 +64,14 @@ out: > error_propagate(errp, local_err); > } > =20 > +static uint16List **host_memory_append_node(uint16List **node, > + unsigned long value) > +{ > + *node =3D g_malloc0(sizeof(**node)); > + (*node)->value =3D value; > + return &(*node)->next; > +} > + > static void > host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char= *name, > void *opaque, Error **errp) > @@ -74,25 +82,23 @@ host_memory_backend_get_host_nodes(Object *obj, Vis= itor *v, const char *name, > unsigned long value; > =20 > value =3D find_first_bit(backend->host_nodes, MAX_NODES); > + > + node =3D host_memory_append_node(node, value); > + > if (value =3D=3D MAX_NODES) { > - return; > + goto out; > } > =20 > - *node =3D g_malloc0(sizeof(**node)); > - (*node)->value =3D value; > - node =3D &(*node)->next; > - > do { > value =3D find_next_bit(backend->host_nodes, MAX_NODES, value = + 1); > if (value =3D=3D MAX_NODES) { > break; > } > =20 > - *node =3D g_malloc0(sizeof(**node)); > - (*node)->value =3D value; > - node =3D &(*node)->next; > + node =3D host_memory_append_node(node, value); > } while (true); > =20 > +out: > visit_type_uint16List(v, name, &host_nodes, errp); This function is leaking host_nodes, so you need a qapi_free_uint16List(head); here (and saving the head pointer on the first call to host_memory_append_node). The bug is preexisting. I'm curious about one thing. Eric/Markus, it would be nice to open code the visit of the list with visit_start_list(v, name, NULL, 0, &err); if (err) { goto out; } ... visit_type_uint16(v, name, &value, &err); visit_next_list(v, NULL, 0); ... visit_end_list(v, NULL); We know here that on the other side there is an output visitor. However, it doesn't work because visit_next_list asserts that tail =3D=3D NULL. Would it be easy to support this idiom, and would it make sense to extend it to other kinds of visitor? Thanks, Paolo From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47240) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bNHfe-0001Ss-Bw for qemu-devel@nongnu.org; Wed, 13 Jul 2016 06:45:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bNHfa-0004S9-0R for qemu-devel@nongnu.org; Wed, 13 Jul 2016 06:45:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39159) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bNHfZ-0004Rx-Ew for qemu-devel@nongnu.org; Wed, 13 Jul 2016 06:45:33 -0400 References: <1468383486-108169-1-git-send-email-guangrong.xiao@linux.intel.com> From: Paolo Bonzini Message-ID: <945b233e-286f-fc22-8837-761e1ac2e522@redhat.com> Date: Wed, 13 Jul 2016 12:45:26 +0200 MIME-Version: 1.0 In-Reply-To: <1468383486-108169-1-git-send-email-guangrong.xiao@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 1/2] hostmem: fix QEMU crash by 'info memdev' List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Xiao Guangrong , imammedo@redhat.com Cc: gleb@kernel.org, mtosatti@redhat.com, stefanha@redhat.com, mst@redhat.com, rth@twiddle.net, ehabkost@redhat.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, Eric Blake , Markus Armbruster On 13/07/2016 06:18, Xiao Guangrong wrote: >=20 > Return MAX_NODES under this case to fix this bug >=20 > Signed-off-by: Xiao Guangrong > --- > backends/hostmem.c | 22 ++++++++++++++-------- > 1 file changed, 14 insertions(+), 8 deletions(-) >=20 > diff --git a/backends/hostmem.c b/backends/hostmem.c > index 6e28be1..8dede4d 100644 > --- a/backends/hostmem.c > +++ b/backends/hostmem.c > @@ -64,6 +64,14 @@ out: > error_propagate(errp, local_err); > } > =20 > +static uint16List **host_memory_append_node(uint16List **node, > + unsigned long value) > +{ > + *node =3D g_malloc0(sizeof(**node)); > + (*node)->value =3D value; > + return &(*node)->next; > +} > + > static void > host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char= *name, > void *opaque, Error **errp) > @@ -74,25 +82,23 @@ host_memory_backend_get_host_nodes(Object *obj, Vis= itor *v, const char *name, > unsigned long value; > =20 > value =3D find_first_bit(backend->host_nodes, MAX_NODES); > + > + node =3D host_memory_append_node(node, value); > + > if (value =3D=3D MAX_NODES) { > - return; > + goto out; > } > =20 > - *node =3D g_malloc0(sizeof(**node)); > - (*node)->value =3D value; > - node =3D &(*node)->next; > - > do { > value =3D find_next_bit(backend->host_nodes, MAX_NODES, value = + 1); > if (value =3D=3D MAX_NODES) { > break; > } > =20 > - *node =3D g_malloc0(sizeof(**node)); > - (*node)->value =3D value; > - node =3D &(*node)->next; > + node =3D host_memory_append_node(node, value); > } while (true); > =20 > +out: > visit_type_uint16List(v, name, &host_nodes, errp); This function is leaking host_nodes, so you need a qapi_free_uint16List(head); here (and saving the head pointer on the first call to host_memory_append_node). The bug is preexisting. I'm curious about one thing. Eric/Markus, it would be nice to open code the visit of the list with visit_start_list(v, name, NULL, 0, &err); if (err) { goto out; } ... visit_type_uint16(v, name, &value, &err); visit_next_list(v, NULL, 0); ... visit_end_list(v, NULL); We know here that on the other side there is an output visitor. However, it doesn't work because visit_next_list asserts that tail =3D=3D NULL. Would it be easy to support this idiom, and would it make sense to extend it to other kinds of visitor? Thanks, Paolo