* [PATCH] pl031: add finalize function to avoid memleaks
@ 2020-02-03 7:47 pannengyuan
2020-02-03 7:59 ` Richard Henderson
2020-02-03 9:58 ` Peter Maydell
0 siblings, 2 replies; 5+ messages in thread
From: pannengyuan @ 2020-02-03 7:47 UTC (permalink / raw)
To: peter.maydell
Cc: zhang.zhanghailiang, Pan Nengyuan, qemu-devel, xuding42,
qemu-arm, Euler Robot
From: Pan Nengyuan <pannengyuan@huawei.com>
There is a memory leak when we call 'device_list_properties' with
typename = pl031. It's easy to reproduce as follow:
virsh qemu-monitor-command vm1 --pretty '{"execute": "device-list-properties", "arguments": {"typename": "pl031"}}'
The memory leak stack:
Direct leak of 48 byte(s) in 1 object(s) allocated from:
#0 0x7f6e0925a970 in __interceptor_calloc (/lib64/libasan.so.5+0xef970)
#1 0x7f6e06f4d49d in g_malloc0 (/lib64/libglib-2.0.so.0+0x5249d)
#2 0x564a0f7654ea in timer_new_full /mnt/sdb/qemu/include/qemu/timer.h:530
#3 0x564a0f76555d in timer_new /mnt/sdb/qemu/include/qemu/timer.h:551
#4 0x564a0f765589 in timer_new_ns /mnt/sdb/qemu/include/qemu/timer.h:569
#5 0x564a0f76747d in pl031_init /mnt/sdb/qemu/hw/rtc/pl031.c:198
#6 0x564a0fd4a19d in object_init_with_type /mnt/sdb/qemu/qom/object.c:360
#7 0x564a0fd4b166 in object_initialize_with_type /mnt/sdb/qemu/qom/object.c:467
#8 0x564a0fd4c8e6 in object_new_with_type /mnt/sdb/qemu/qom/object.c:636
#9 0x564a0fd4c98e in object_new /mnt/sdb/qemu/qom/object.c:646
#10 0x564a0fc69d43 in qmp_device_list_properties /mnt/sdb/qemu/qom/qom-qmp-cmds.c:204
#11 0x564a0ef18e64 in qdev_device_help /mnt/sdb/qemu/qdev-monitor.c:278
Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
---
hw/rtc/pl031.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/hw/rtc/pl031.c b/hw/rtc/pl031.c
index ae47f09635..50664ca000 100644
--- a/hw/rtc/pl031.c
+++ b/hw/rtc/pl031.c
@@ -194,6 +194,15 @@ static void pl031_init(Object *obj)
s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
}
+static void pl031_finalize(Object *obj)
+{
+ PL031State *s = PL031(obj);
+ if (s->timer) {
+ timer_del(s->timer);
+ timer_free(s->timer);
+ }
+}
+
static int pl031_pre_save(void *opaque)
{
PL031State *s = opaque;
@@ -329,6 +338,7 @@ static const TypeInfo pl031_info = {
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(PL031State),
.instance_init = pl031_init,
+ .instance_finalize = pl031_finalize,
.class_init = pl031_class_init,
};
--
2.21.0.windows.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] pl031: add finalize function to avoid memleaks
2020-02-03 7:47 [PATCH] pl031: add finalize function to avoid memleaks pannengyuan
@ 2020-02-03 7:59 ` Richard Henderson
2020-02-03 8:13 ` Pan Nengyuan
2020-02-03 9:58 ` Peter Maydell
1 sibling, 1 reply; 5+ messages in thread
From: Richard Henderson @ 2020-02-03 7:59 UTC (permalink / raw)
To: pannengyuan, peter.maydell
Cc: xuding42, Euler Robot, qemu-arm, zhang.zhanghailiang, qemu-devel
On 2/3/20 7:47 AM, pannengyuan@huawei.com wrote:
> +static void pl031_finalize(Object *obj)
> +{
> + PL031State *s = PL031(obj);
> + if (s->timer) {
As far as I can see, s->timer can never be null.
Otherwise,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
r~
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] pl031: add finalize function to avoid memleaks
2020-02-03 7:59 ` Richard Henderson
@ 2020-02-03 8:13 ` Pan Nengyuan
0 siblings, 0 replies; 5+ messages in thread
From: Pan Nengyuan @ 2020-02-03 8:13 UTC (permalink / raw)
To: Richard Henderson, peter.maydell
Cc: xuding42, Euler Robot, qemu-arm, zhang.zhanghailiang, qemu-devel
On 2/3/2020 3:59 PM, Richard Henderson wrote:
> On 2/3/20 7:47 AM, pannengyuan@huawei.com wrote:
>> +static void pl031_finalize(Object *obj)
>> +{
>> + PL031State *s = PL031(obj);
>> + if (s->timer) {
>
> As far as I can see, s->timer can never be null.
Yes, it's no instances, just seems safer.
Thanks.
>
> Otherwise,
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>
>
> r~
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] pl031: add finalize function to avoid memleaks
2020-02-03 7:47 [PATCH] pl031: add finalize function to avoid memleaks pannengyuan
2020-02-03 7:59 ` Richard Henderson
@ 2020-02-03 9:58 ` Peter Maydell
2020-02-04 0:49 ` Pan Nengyuan
1 sibling, 1 reply; 5+ messages in thread
From: Peter Maydell @ 2020-02-03 9:58 UTC (permalink / raw)
To: pannengyuan
Cc: xuding42, Euler Robot, qemu-arm, zhanghailiang, QEMU Developers
On Mon, 3 Feb 2020 at 07:47, <pannengyuan@huawei.com> wrote:
>
> From: Pan Nengyuan <pannengyuan@huawei.com>
>
> There is a memory leak when we call 'device_list_properties' with
> typename = pl031. It's easy to reproduce as follow:
>
> virsh qemu-monitor-command vm1 --pretty '{"execute": "device-list-properties", "arguments": {"typename": "pl031"}}'
>
> The memory leak stack:
> Direct leak of 48 byte(s) in 1 object(s) allocated from:
> #0 0x7f6e0925a970 in __interceptor_calloc (/lib64/libasan.so.5+0xef970)
> #1 0x7f6e06f4d49d in g_malloc0 (/lib64/libglib-2.0.so.0+0x5249d)
> #2 0x564a0f7654ea in timer_new_full /mnt/sdb/qemu/include/qemu/timer.h:530
> #3 0x564a0f76555d in timer_new /mnt/sdb/qemu/include/qemu/timer.h:551
> #4 0x564a0f765589 in timer_new_ns /mnt/sdb/qemu/include/qemu/timer.h:569
> #5 0x564a0f76747d in pl031_init /mnt/sdb/qemu/hw/rtc/pl031.c:198
> #6 0x564a0fd4a19d in object_init_with_type /mnt/sdb/qemu/qom/object.c:360
> #7 0x564a0fd4b166 in object_initialize_with_type /mnt/sdb/qemu/qom/object.c:467
> #8 0x564a0fd4c8e6 in object_new_with_type /mnt/sdb/qemu/qom/object.c:636
> #9 0x564a0fd4c98e in object_new /mnt/sdb/qemu/qom/object.c:646
> #10 0x564a0fc69d43 in qmp_device_list_properties /mnt/sdb/qemu/qom/qom-qmp-cmds.c:204
> #11 0x564a0ef18e64 in qdev_device_help /mnt/sdb/qemu/qdev-monitor.c:278
>
> Reported-by: Euler Robot <euler.robot@huawei.com>
> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
> ---
> hw/rtc/pl031.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/hw/rtc/pl031.c b/hw/rtc/pl031.c
> index ae47f09635..50664ca000 100644
> --- a/hw/rtc/pl031.c
> +++ b/hw/rtc/pl031.c
> @@ -194,6 +194,15 @@ static void pl031_init(Object *obj)
> s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
> }
>
> +static void pl031_finalize(Object *obj)
> +{
> + PL031State *s = PL031(obj);
> + if (s->timer) {
> + timer_del(s->timer);
> + timer_free(s->timer);
> + }
> +}
> +
> static int pl031_pre_save(void *opaque)
> {
> PL031State *s = opaque;
> @@ -329,6 +338,7 @@ static const TypeInfo pl031_info = {
> .parent = TYPE_SYS_BUS_DEVICE,
> .instance_size = sizeof(PL031State),
> .instance_init = pl031_init,
> + .instance_finalize = pl031_finalize,
> .class_init = pl031_class_init,
> };
The more usual way to fix this I think is to delay
the timer_new until realize rather than putting it
into instance_init, since the pl031 can't be
hotplugged.
thanks
-- PMM
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] pl031: add finalize function to avoid memleaks
2020-02-03 9:58 ` Peter Maydell
@ 2020-02-04 0:49 ` Pan Nengyuan
0 siblings, 0 replies; 5+ messages in thread
From: Pan Nengyuan @ 2020-02-04 0:49 UTC (permalink / raw)
To: Peter Maydell
Cc: xuding42, Euler Robot, qemu-arm, zhanghailiang, QEMU Developers
On 2/3/2020 5:58 PM, Peter Maydell wrote:
> On Mon, 3 Feb 2020 at 07:47, <pannengyuan@huawei.com> wrote:
>>
>> From: Pan Nengyuan <pannengyuan@huawei.com>
>>
>> There is a memory leak when we call 'device_list_properties' with
>> typename = pl031. It's easy to reproduce as follow:
>>
>> virsh qemu-monitor-command vm1 --pretty '{"execute": "device-list-properties", "arguments": {"typename": "pl031"}}'
>>
>> The memory leak stack:
>> Direct leak of 48 byte(s) in 1 object(s) allocated from:
>> #0 0x7f6e0925a970 in __interceptor_calloc (/lib64/libasan.so.5+0xef970)
>> #1 0x7f6e06f4d49d in g_malloc0 (/lib64/libglib-2.0.so.0+0x5249d)
>> #2 0x564a0f7654ea in timer_new_full /mnt/sdb/qemu/include/qemu/timer.h:530
>> #3 0x564a0f76555d in timer_new /mnt/sdb/qemu/include/qemu/timer.h:551
>> #4 0x564a0f765589 in timer_new_ns /mnt/sdb/qemu/include/qemu/timer.h:569
>> #5 0x564a0f76747d in pl031_init /mnt/sdb/qemu/hw/rtc/pl031.c:198
>> #6 0x564a0fd4a19d in object_init_with_type /mnt/sdb/qemu/qom/object.c:360
>> #7 0x564a0fd4b166 in object_initialize_with_type /mnt/sdb/qemu/qom/object.c:467
>> #8 0x564a0fd4c8e6 in object_new_with_type /mnt/sdb/qemu/qom/object.c:636
>> #9 0x564a0fd4c98e in object_new /mnt/sdb/qemu/qom/object.c:646
>> #10 0x564a0fc69d43 in qmp_device_list_properties /mnt/sdb/qemu/qom/qom-qmp-cmds.c:204
>> #11 0x564a0ef18e64 in qdev_device_help /mnt/sdb/qemu/qdev-monitor.c:278
>>
>> Reported-by: Euler Robot <euler.robot@huawei.com>
>> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
>> ---
>> hw/rtc/pl031.c | 10 ++++++++++
>> 1 file changed, 10 insertions(+)
>>
>> diff --git a/hw/rtc/pl031.c b/hw/rtc/pl031.c
>> index ae47f09635..50664ca000 100644
>> --- a/hw/rtc/pl031.c
>> +++ b/hw/rtc/pl031.c
>> @@ -194,6 +194,15 @@ static void pl031_init(Object *obj)
>> s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
>> }
>>
>> +static void pl031_finalize(Object *obj)
>> +{
>> + PL031State *s = PL031(obj);
>> + if (s->timer) {
>> + timer_del(s->timer);
>> + timer_free(s->timer);
>> + }
>> +}
>> +
>> static int pl031_pre_save(void *opaque)
>> {
>> PL031State *s = opaque;
>> @@ -329,6 +338,7 @@ static const TypeInfo pl031_info = {
>> .parent = TYPE_SYS_BUS_DEVICE,
>> .instance_size = sizeof(PL031State),
>> .instance_init = pl031_init,
>> + .instance_finalize = pl031_finalize,
>> .class_init = pl031_class_init,
>> };
>
> The more usual way to fix this I think is to delay
> the timer_new until realize rather than putting it
> into instance_init, since the pl031 can't be
> hotplugged.
Hmm, you are right, I will change it in next version.
Thanks.
>
> thanks
> -- PMM
> .
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-02-04 0:50 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-03 7:47 [PATCH] pl031: add finalize function to avoid memleaks pannengyuan
2020-02-03 7:59 ` Richard Henderson
2020-02-03 8:13 ` Pan Nengyuan
2020-02-03 9:58 ` Peter Maydell
2020-02-04 0:49 ` Pan Nengyuan
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.