QEMU-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] pc: fix leak in pc_system_flash_cleanup_unused
@ 2020-07-01  1:58 Alexander Bulekov
  2020-07-01  5:49 ` Paolo Bonzini
  2020-07-01  7:34 ` Philippe Mathieu-Daudé
  0 siblings, 2 replies; 5+ messages in thread
From: Alexander Bulekov @ 2020-07-01  1:58 UTC (permalink / raw)
  To: qemu-devel
  Cc: Eduardo Habkost, Michael S. Tsirkin, Alexander Bulekov,
	Paolo Bonzini, Richard Henderson

fix a leak detected when building with --enable-sanitizers:
./i386-softmmu/qemu-system-i386
Upon exit:
==13576==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1216 byte(s) in 1 object(s) allocated from:
    #0 0x7f9d2ed5c628 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5)
    #1 0x7f9d2e963500 in g_malloc (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.)
    #2 0x55fa646d25cc in object_new_with_type /tmp/qemu/qom/object.c:686
    #3 0x55fa63dbaa88 in qdev_new /tmp/qemu/hw/core/qdev.c:140
    #4 0x55fa638a533f in pc_pflash_create /tmp/qemu/hw/i386/pc_sysfw.c:88
    #5 0x55fa638a54c4 in pc_system_flash_create /tmp/qemu/hw/i386/pc_sysfw.c:106
    #6 0x55fa646caa1d in object_init_with_type /tmp/qemu/qom/object.c:369
    #7 0x55fa646d20b5 in object_initialize_with_type /tmp/qemu/qom/object.c:511
    #8 0x55fa646d2606 in object_new_with_type /tmp/qemu/qom/object.c:687
    #9 0x55fa639431e9 in qemu_init /tmp/qemu/softmmu/vl.c:3878
    #10 0x55fa6335c1b8 in main /tmp/qemu/softmmu/main.c:48
    #11 0x7f9d2cf06e0a in __libc_start_main ../csu/libc-start.c:308
    #12 0x55fa6335f8e9 in _start (/tmp/qemu/build/i386-softmmu/qemu-system-i386)

Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
---

I am not very familiar with the QOM, so maybe this isn't the right way
of going about this. With the call to object_property_add_child in
pc_sysfw.c:pc_pflash_create, object_ref is called on the pflash device.
In the pc_system_flash_cleanup_unused function, there are calls to
object_propery_del and object_unparent, but it seems neither of these
calls object_unref. So do we need to manually decrement the refcount?

 hw/i386/pc_sysfw.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
index ec2a3b3e7e..f69a93671a 100644
--- a/hw/i386/pc_sysfw.c
+++ b/hw/i386/pc_sysfw.c
@@ -123,6 +123,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
             object_property_del(OBJECT(pcms), prop_name);
             g_free(prop_name);
             object_unparent(dev_obj);
+            object_unref(dev_obj);
             pcms->flash[i] = NULL;
         }
     }
-- 
2.26.2



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pc: fix leak in pc_system_flash_cleanup_unused
  2020-07-01  1:58 [PATCH] pc: fix leak in pc_system_flash_cleanup_unused Alexander Bulekov
@ 2020-07-01  5:49 ` Paolo Bonzini
  2020-07-01 13:06   ` Alexander Bulekov
  2020-07-01  7:34 ` Philippe Mathieu-Daudé
  1 sibling, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2020-07-01  5:49 UTC (permalink / raw)
  To: Alexander Bulekov, qemu-devel
  Cc: Michael S. Tsirkin, Eduardo Habkost, Richard Henderson

On 01/07/20 03:58, Alexander Bulekov wrote:
> fix a leak detected when building with --enable-sanitizers:
> ./i386-softmmu/qemu-system-i386
> Upon exit:
> ==13576==ERROR: LeakSanitizer: detected memory leaks
> 
> Direct leak of 1216 byte(s) in 1 object(s) allocated from:
>     #0 0x7f9d2ed5c628 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5)
>     #1 0x7f9d2e963500 in g_malloc (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.)
>     #2 0x55fa646d25cc in object_new_with_type /tmp/qemu/qom/object.c:686
>     #3 0x55fa63dbaa88 in qdev_new /tmp/qemu/hw/core/qdev.c:140
>     #4 0x55fa638a533f in pc_pflash_create /tmp/qemu/hw/i386/pc_sysfw.c:88
>     #5 0x55fa638a54c4 in pc_system_flash_create /tmp/qemu/hw/i386/pc_sysfw.c:106
>     #6 0x55fa646caa1d in object_init_with_type /tmp/qemu/qom/object.c:369
>     #7 0x55fa646d20b5 in object_initialize_with_type /tmp/qemu/qom/object.c:511
>     #8 0x55fa646d2606 in object_new_with_type /tmp/qemu/qom/object.c:687
>     #9 0x55fa639431e9 in qemu_init /tmp/qemu/softmmu/vl.c:3878
>     #10 0x55fa6335c1b8 in main /tmp/qemu/softmmu/main.c:48
>     #11 0x7f9d2cf06e0a in __libc_start_main ../csu/libc-start.c:308
>     #12 0x55fa6335f8e9 in _start (/tmp/qemu/build/i386-softmmu/qemu-system-i386)
> 
> Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
> ---
> 
> I am not very familiar with the QOM, so maybe this isn't the right way
> of going about this. With the call to object_property_add_child in
> pc_sysfw.c:pc_pflash_create, object_ref is called on the pflash device.
> In the pc_system_flash_cleanup_unused function, there are calls to
> object_propery_del and object_unparent, but it seems neither of these
> calls object_unref. So do we need to manually decrement the refcount?

Yes; you can also add it in pc_pflash_create, because

    /*
     * Since object_property_add_child added a reference to the child object,
     * we can drop the reference added by object_initialize(), so the child
     * property will own the only reference to the object.
     */

(from object_initialize_childv).

Paolo

>  hw/i386/pc_sysfw.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
> index ec2a3b3e7e..f69a93671a 100644
> --- a/hw/i386/pc_sysfw.c
> +++ b/hw/i386/pc_sysfw.c
> @@ -123,6 +123,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
>              object_property_del(OBJECT(pcms), prop_name);
>              g_free(prop_name);
>              object_unparent(dev_obj);
> +            object_unref(dev_obj);
>              pcms->flash[i] = NULL;
>          }
>      }
> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pc: fix leak in pc_system_flash_cleanup_unused
  2020-07-01  1:58 [PATCH] pc: fix leak in pc_system_flash_cleanup_unused Alexander Bulekov
  2020-07-01  5:49 ` Paolo Bonzini
@ 2020-07-01  7:34 ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-07-01  7:34 UTC (permalink / raw)
  To: Alexander Bulekov, qemu-devel
  Cc: Paolo Bonzini, Richard Henderson, Eduardo Habkost,
	Markus Armbruster, Michael S. Tsirkin

On 7/1/20 3:58 AM, Alexander Bulekov wrote:
> fix a leak detected when building with --enable-sanitizers:
> ./i386-softmmu/qemu-system-i386
> Upon exit:
> ==13576==ERROR: LeakSanitizer: detected memory leaks
> 
> Direct leak of 1216 byte(s) in 1 object(s) allocated from:
>     #0 0x7f9d2ed5c628 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5)
>     #1 0x7f9d2e963500 in g_malloc (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.)
>     #2 0x55fa646d25cc in object_new_with_type /tmp/qemu/qom/object.c:686
>     #3 0x55fa63dbaa88 in qdev_new /tmp/qemu/hw/core/qdev.c:140
>     #4 0x55fa638a533f in pc_pflash_create /tmp/qemu/hw/i386/pc_sysfw.c:88
>     #5 0x55fa638a54c4 in pc_system_flash_create /tmp/qemu/hw/i386/pc_sysfw.c:106
>     #6 0x55fa646caa1d in object_init_with_type /tmp/qemu/qom/object.c:369
>     #7 0x55fa646d20b5 in object_initialize_with_type /tmp/qemu/qom/object.c:511
>     #8 0x55fa646d2606 in object_new_with_type /tmp/qemu/qom/object.c:687
>     #9 0x55fa639431e9 in qemu_init /tmp/qemu/softmmu/vl.c:3878
>     #10 0x55fa6335c1b8 in main /tmp/qemu/softmmu/main.c:48
>     #11 0x7f9d2cf06e0a in __libc_start_main ../csu/libc-start.c:308
>     #12 0x55fa6335f8e9 in _start (/tmp/qemu/build/i386-softmmu/qemu-system-i386)
> 
> Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
> ---
> 
> I am not very familiar with the QOM, so maybe this isn't the right way
> of going about this. With the call to object_property_add_child in
> pc_sysfw.c:pc_pflash_create, object_ref is called on the pflash device.
> In the pc_system_flash_cleanup_unused function, there are calls to
> object_propery_del and object_unparent, but it seems neither of these
> calls object_unref. So do we need to manually decrement the refcount?

Hint in such case, look at who wrote the commit, and Cc the author
to get an explanation:

$ git blame hw/i386/pc_sysfw.c
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 119)     for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 120)         dev_obj = OBJECT(pcms->flash[i]);
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 121)         if (!object_property_get_bool(dev_obj,
"realized", &error_abort)) {
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 122)             prop_name = g_strdup_printf("pflash%d", i);
df4fe0b291b hw/i386/pc_sysfw.c  (Markus Armbruster      2020-05-05
17:29:26 +0200 123)             object_property_del(OBJECT(pcms),
prop_name);
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 124)             g_free(prop_name);
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 125)             object_unparent(dev_obj);
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 126)             pcms->flash[i] = NULL;
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 127)         }
ebc29e1beab hw/i386/pc_sysfw.c  (Markus Armbruster      2019-03-11
18:39:26 +0100 128)     }

> 
>  hw/i386/pc_sysfw.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
> index ec2a3b3e7e..f69a93671a 100644
> --- a/hw/i386/pc_sysfw.c
> +++ b/hw/i386/pc_sysfw.c
> @@ -123,6 +123,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
>              object_property_del(OBJECT(pcms), prop_name);
>              g_free(prop_name);
>              object_unparent(dev_obj);
> +            object_unref(dev_obj);
>              pcms->flash[i] = NULL;
>          }
>      }
> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pc: fix leak in pc_system_flash_cleanup_unused
  2020-07-01  5:49 ` Paolo Bonzini
@ 2020-07-01 13:06   ` Alexander Bulekov
  2020-07-01 14:18     ` Paolo Bonzini
  0 siblings, 1 reply; 5+ messages in thread
From: Alexander Bulekov @ 2020-07-01 13:06 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Michael S. Tsirkin, qemu-devel, Eduardo Habkost, Richard Henderson

On 200701 0749, Paolo Bonzini wrote:
> On 01/07/20 03:58, Alexander Bulekov wrote:
> > fix a leak detected when building with --enable-sanitizers:
> > ./i386-softmmu/qemu-system-i386
> > Upon exit:
> > ==13576==ERROR: LeakSanitizer: detected memory leaks
> > 
> > Direct leak of 1216 byte(s) in 1 object(s) allocated from:
> >     #0 0x7f9d2ed5c628 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5)
> >     #1 0x7f9d2e963500 in g_malloc (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.)
> >     #2 0x55fa646d25cc in object_new_with_type /tmp/qemu/qom/object.c:686
> >     #3 0x55fa63dbaa88 in qdev_new /tmp/qemu/hw/core/qdev.c:140
> >     #4 0x55fa638a533f in pc_pflash_create /tmp/qemu/hw/i386/pc_sysfw.c:88
> >     #5 0x55fa638a54c4 in pc_system_flash_create /tmp/qemu/hw/i386/pc_sysfw.c:106
> >     #6 0x55fa646caa1d in object_init_with_type /tmp/qemu/qom/object.c:369
> >     #7 0x55fa646d20b5 in object_initialize_with_type /tmp/qemu/qom/object.c:511
> >     #8 0x55fa646d2606 in object_new_with_type /tmp/qemu/qom/object.c:687
> >     #9 0x55fa639431e9 in qemu_init /tmp/qemu/softmmu/vl.c:3878
> >     #10 0x55fa6335c1b8 in main /tmp/qemu/softmmu/main.c:48
> >     #11 0x7f9d2cf06e0a in __libc_start_main ../csu/libc-start.c:308
> >     #12 0x55fa6335f8e9 in _start (/tmp/qemu/build/i386-softmmu/qemu-system-i386)
> > 
> > Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
> > ---
> > 
> > I am not very familiar with the QOM, so maybe this isn't the right way
> > of going about this. With the call to object_property_add_child in
> > pc_sysfw.c:pc_pflash_create, object_ref is called on the pflash device.
> > In the pc_system_flash_cleanup_unused function, there are calls to
> > object_propery_del and object_unparent, but it seems neither of these
> > calls object_unref. So do we need to manually decrement the refcount?
> 
> Yes; you can also add it in pc_pflash_create, because
> 
>     /*
>      * Since object_property_add_child added a reference to the child object,
>      * we can drop the reference added by object_initialize(), so the child
>      * property will own the only reference to the object.
>      */
> 
> (from object_initialize_childv).

Isn't that reference used at pc_sysfw.c:104 ?
pcms->flash[0] = pc_pflash_create(pcms, "system.flash0", "pflash0");

In this case, shouldn't we unref when
pc_sysfw.c:126  pcms->flash[0] = NULL
?
-Alex

> Paolo
> 
> >  hw/i386/pc_sysfw.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
> > index ec2a3b3e7e..f69a93671a 100644
> > --- a/hw/i386/pc_sysfw.c
> > +++ b/hw/i386/pc_sysfw.c
> > @@ -123,6 +123,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
> >              object_property_del(OBJECT(pcms), prop_name);
> >              g_free(prop_name);
> >              object_unparent(dev_obj);
> > +            object_unref(dev_obj);
> >              pcms->flash[i] = NULL;
> >          }
> >      }
> > 
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pc: fix leak in pc_system_flash_cleanup_unused
  2020-07-01 13:06   ` Alexander Bulekov
@ 2020-07-01 14:18     ` Paolo Bonzini
  0 siblings, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2020-07-01 14:18 UTC (permalink / raw)
  To: Alexander Bulekov
  Cc: Michael S. Tsirkin, qemu-devel, Eduardo Habkost, Richard Henderson

On 01/07/20 15:06, Alexander Bulekov wrote:
>> Yes; you can also add it in pc_pflash_create, because
>>
>>     /*
>>      * Since object_property_add_child added a reference to the child object,
>>      * we can drop the reference added by object_initialize(), so the child
>>      * property will own the only reference to the object.
>>      */
>>
>> (from object_initialize_childv).
> Isn't that reference used at pc_sysfw.c:104 ?
> pcms->flash[0] = pc_pflash_create(pcms, "system.flash0", "pflash0");
> 
> In this case, shouldn't we unref when
> pc_sysfw.c:126  pcms->flash[0] = NULL
> ?

object_unparent already drops a reference.  Therefore, it's okay to drop
the reference immediately after object_property_add_child (which adds a
reference) and match pc_pflash_create with just object_unparent.

Paolo



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-01  1:58 [PATCH] pc: fix leak in pc_system_flash_cleanup_unused Alexander Bulekov
2020-07-01  5:49 ` Paolo Bonzini
2020-07-01 13:06   ` Alexander Bulekov
2020-07-01 14:18     ` Paolo Bonzini
2020-07-01  7:34 ` Philippe Mathieu-Daudé

QEMU-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/qemu-devel/0 qemu-devel/git/0.git
	git clone --mirror https://lore.kernel.org/qemu-devel/1 qemu-devel/git/1.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 qemu-devel qemu-devel/ https://lore.kernel.org/qemu-devel \
		qemu-devel@nongnu.org
	public-inbox-index qemu-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.nongnu.qemu-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git