[PATCH 7/8] lm32: do not leak memory on object_new/object_unref

Paolo Bonzini posted 8 patches 6 years, 2 months ago
Maintainers: Fam Zheng <fam@euphon.net>, Aleksandar Markovic <amarkovic@wavecomp.com>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Thomas Huth <huth@tuxfamily.org>, Helge Deller <deller@gmx.de>, John Snow <jsnow@redhat.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, Richard Henderson <rth@twiddle.net>, Aleksandar Rikalo <arikalo@wavecomp.com>, Michael Walle <michael@walle.cc>, "Hervé Poussineau" <hpoussin@reactos.org>, "Alex Bennée" <alex.bennee@linaro.org>, Aurelien Jarno <aurelien@aurel32.net>
[PATCH 7/8] lm32: do not leak memory on object_new/object_unref
Posted by Paolo Bonzini 6 years, 2 months ago
Bottom halves and ptimers are malloced, but nothing in these
files is freeing memory allocated by instance_init.  Since
these are sysctl devices that are never unrealized, just moving
the allocations to realize is enough to avoid the leak in
practice (and also to avoid upsetting asan when running
device-introspect-test).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/timer/lm32_timer.c       |  6 +++---
 hw/timer/milkymist-sysctl.c | 10 +++++-----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/hw/timer/lm32_timer.c b/hw/timer/lm32_timer.c
index ac3edaf..cf316ed 100644
--- a/hw/timer/lm32_timer.c
+++ b/hw/timer/lm32_timer.c
@@ -186,9 +186,6 @@ static void lm32_timer_init(Object *obj)
 
     sysbus_init_irq(dev, &s->irq);
 
-    s->bh = qemu_bh_new(timer_hit, s);
-    s->ptimer = ptimer_init(s->bh, PTIMER_POLICY_DEFAULT);
-
     memory_region_init_io(&s->iomem, obj, &timer_ops, s,
                           "timer", R_MAX * 4);
     sysbus_init_mmio(dev, &s->iomem);
@@ -198,6 +195,9 @@ static void lm32_timer_realize(DeviceState *dev, Error **errp)
 {
     LM32TimerState *s = LM32_TIMER(dev);
 
+    s->bh = qemu_bh_new(timer_hit, s);
+    s->ptimer = ptimer_init(s->bh, PTIMER_POLICY_DEFAULT);
+
     ptimer_set_freq(s->ptimer, s->freq_hz);
 }
 
diff --git a/hw/timer/milkymist-sysctl.c b/hw/timer/milkymist-sysctl.c
index 9583507..6aedc11 100644
--- a/hw/timer/milkymist-sysctl.c
+++ b/hw/timer/milkymist-sysctl.c
@@ -283,11 +283,6 @@ static void milkymist_sysctl_init(Object *obj)
     sysbus_init_irq(dev, &s->timer0_irq);
     sysbus_init_irq(dev, &s->timer1_irq);
 
-    s->bh0 = qemu_bh_new(timer0_hit, s);
-    s->bh1 = qemu_bh_new(timer1_hit, s);
-    s->ptimer0 = ptimer_init(s->bh0, PTIMER_POLICY_DEFAULT);
-    s->ptimer1 = ptimer_init(s->bh1, PTIMER_POLICY_DEFAULT);
-
     memory_region_init_io(&s->regs_region, obj, &sysctl_mmio_ops, s,
             "milkymist-sysctl", R_MAX * 4);
     sysbus_init_mmio(dev, &s->regs_region);
@@ -297,6 +292,11 @@ static void milkymist_sysctl_realize(DeviceState *dev, Error **errp)
 {
     MilkymistSysctlState *s = MILKYMIST_SYSCTL(dev);
 
+    s->bh0 = qemu_bh_new(timer0_hit, s);
+    s->bh1 = qemu_bh_new(timer1_hit, s);
+    s->ptimer0 = ptimer_init(s->bh0, PTIMER_POLICY_DEFAULT);
+    s->ptimer1 = ptimer_init(s->bh1, PTIMER_POLICY_DEFAULT);
+
     ptimer_set_freq(s->ptimer0, s->freq_hz);
     ptimer_set_freq(s->ptimer1, s->freq_hz);
 }
-- 
1.8.3.1



Re: [PATCH 7/8] lm32: do not leak memory on object_new/object_unref
Posted by Philippe Mathieu-Daudé 6 years, 2 months ago
On 10/1/19 3:36 PM, Paolo Bonzini wrote:
> Bottom halves and ptimers are malloced, but nothing in these
> files is freeing memory allocated by instance_init.  Since
> these are sysctl devices that are never unrealized, just moving
> the allocations to realize is enough to avoid the leak in
> practice (and also to avoid upsetting asan when running
> device-introspect-test).
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   hw/timer/lm32_timer.c       |  6 +++---
>   hw/timer/milkymist-sysctl.c | 10 +++++-----
>   2 files changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/timer/lm32_timer.c b/hw/timer/lm32_timer.c
> index ac3edaf..cf316ed 100644
> --- a/hw/timer/lm32_timer.c
> +++ b/hw/timer/lm32_timer.c
> @@ -186,9 +186,6 @@ static void lm32_timer_init(Object *obj)
>   
>       sysbus_init_irq(dev, &s->irq);
>   
> -    s->bh = qemu_bh_new(timer_hit, s);
> -    s->ptimer = ptimer_init(s->bh, PTIMER_POLICY_DEFAULT);
> -
>       memory_region_init_io(&s->iomem, obj, &timer_ops, s,
>                             "timer", R_MAX * 4);
>       sysbus_init_mmio(dev, &s->iomem);
> @@ -198,6 +195,9 @@ static void lm32_timer_realize(DeviceState *dev, Error **errp)
>   {
>       LM32TimerState *s = LM32_TIMER(dev);
>   
> +    s->bh = qemu_bh_new(timer_hit, s);
> +    s->ptimer = ptimer_init(s->bh, PTIMER_POLICY_DEFAULT);
> +
>       ptimer_set_freq(s->ptimer, s->freq_hz);
>   }
>   
> diff --git a/hw/timer/milkymist-sysctl.c b/hw/timer/milkymist-sysctl.c
> index 9583507..6aedc11 100644
> --- a/hw/timer/milkymist-sysctl.c
> +++ b/hw/timer/milkymist-sysctl.c
> @@ -283,11 +283,6 @@ static void milkymist_sysctl_init(Object *obj)
>       sysbus_init_irq(dev, &s->timer0_irq);
>       sysbus_init_irq(dev, &s->timer1_irq);
>   
> -    s->bh0 = qemu_bh_new(timer0_hit, s);
> -    s->bh1 = qemu_bh_new(timer1_hit, s);
> -    s->ptimer0 = ptimer_init(s->bh0, PTIMER_POLICY_DEFAULT);
> -    s->ptimer1 = ptimer_init(s->bh1, PTIMER_POLICY_DEFAULT);
> -
>       memory_region_init_io(&s->regs_region, obj, &sysctl_mmio_ops, s,
>               "milkymist-sysctl", R_MAX * 4);
>       sysbus_init_mmio(dev, &s->regs_region);
> @@ -297,6 +292,11 @@ static void milkymist_sysctl_realize(DeviceState *dev, Error **errp)
>   {
>       MilkymistSysctlState *s = MILKYMIST_SYSCTL(dev);
>   
> +    s->bh0 = qemu_bh_new(timer0_hit, s);
> +    s->bh1 = qemu_bh_new(timer1_hit, s);
> +    s->ptimer0 = ptimer_init(s->bh0, PTIMER_POLICY_DEFAULT);
> +    s->ptimer1 = ptimer_init(s->bh1, PTIMER_POLICY_DEFAULT);
> +
>       ptimer_set_freq(s->ptimer0, s->freq_hz);
>       ptimer_set_freq(s->ptimer1, s->freq_hz);
>   }
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>