In commit f3a508eb4e the Euler Robot reported calling timer_new()
in instance_init() can leak heap memory. The easier fix is to
delay the timer creation at instance realize(). Similarly move
timer_del() into a new instance unrealize() method.
This case was found with the following coccinelle script:
@ match @
identifier instance_init;
typedef Object;
identifier obj;
expression val, scale;
identifier clock_type, callback, opaque;
position pos;
@@
static void instance_init(Object *obj)
{
<...
(
val = timer_new@pos(clock_type, scale, callback, opaque);
|
val = timer_new_ns@pos(clock_type, callback, opaque);
|
val = timer_new_us@pos(clock_type, callback, opaque);
|
val = timer_new_ms@pos(clock_type, callback, opaque);
)
...>
}
@ script:python @
f << match.instance_init;
p << match.pos;
@@
print "check %s:%s:%s in %s()" % (p[0].file, p[0].line, p[0].column, f)
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
Cc: Pan Nengyuan <pannengyuan@huawei.com>
---
hw/sd/sd.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 71a9af09ab..d72cf3de2a 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -2058,14 +2058,12 @@ static void sd_instance_init(Object *obj)
SDState *sd = SD_CARD(obj);
sd->enable = true;
- sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
}
static void sd_instance_finalize(Object *obj)
{
SDState *sd = SD_CARD(obj);
- timer_del(sd->ocr_power_timer);
timer_free(sd->ocr_power_timer);
}
@@ -2098,6 +2096,15 @@ static void sd_realize(DeviceState *dev, Error **errp)
}
blk_set_dev_ops(sd->blk, &sd_block_ops, sd);
}
+
+ sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
+}
+
+static void sd_unrealize(DeviceState *dev, Error **errp)
+{
+ SDState *sd = SD_CARD(dev);
+
+ timer_del(sd->ocr_power_timer);
}
static Property sd_properties[] = {
@@ -2118,6 +2125,7 @@ static void sd_class_init(ObjectClass *klass, void *data)
SDCardClass *sc = SD_CARD_CLASS(klass);
dc->realize = sd_realize;
+ dc->unrealize = sd_unrealize;
device_class_set_props(dc, sd_properties);
dc->vmsd = &sd_vmstate;
dc->reset = sd_reset;
--
2.21.1
On Sat, 15 Feb 2020 at 15:48, Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
>
> In commit f3a508eb4e the Euler Robot reported calling timer_new()
> in instance_init() can leak heap memory. The easier fix is to
> delay the timer creation at instance realize(). Similarly move
> timer_del() into a new instance unrealize() method.
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index 71a9af09ab..d72cf3de2a 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -2058,14 +2058,12 @@ static void sd_instance_init(Object *obj)
> SDState *sd = SD_CARD(obj);
>
> sd->enable = true;
> - sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
> }
>
> static void sd_instance_finalize(Object *obj)
> {
> SDState *sd = SD_CARD(obj);
>
> - timer_del(sd->ocr_power_timer);
> timer_free(sd->ocr_power_timer);
> }
>
> @@ -2098,6 +2096,15 @@ static void sd_realize(DeviceState *dev, Error **errp)
> }
> blk_set_dev_ops(sd->blk, &sd_block_ops, sd);
> }
> +
> + sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
> +}
> +
> +static void sd_unrealize(DeviceState *dev, Error **errp)
> +{
> + SDState *sd = SD_CARD(dev);
> +
> + timer_del(sd->ocr_power_timer);
> }
Here too the old code was doing things correctly in that
it does a timer_del/timer_free on the timer it allocates
in instance_init, and the new code has weirdly split the
freeing between unrealize and finalize.
thanks
-- PMM
On 2/17/20 2:26 PM, Peter Maydell wrote:
> On Sat, 15 Feb 2020 at 15:48, Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
>>
>> In commit f3a508eb4e the Euler Robot reported calling timer_new()
>> in instance_init() can leak heap memory. The easier fix is to
>> delay the timer creation at instance realize(). Similarly move
>> timer_del() into a new instance unrealize() method.
>
>> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
>> index 71a9af09ab..d72cf3de2a 100644
>> --- a/hw/sd/sd.c
>> +++ b/hw/sd/sd.c
>> @@ -2058,14 +2058,12 @@ static void sd_instance_init(Object *obj)
>> SDState *sd = SD_CARD(obj);
>>
>> sd->enable = true;
>> - sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
>> }
>>
>> static void sd_instance_finalize(Object *obj)
>> {
>> SDState *sd = SD_CARD(obj);
>>
>> - timer_del(sd->ocr_power_timer);
>> timer_free(sd->ocr_power_timer);
>> }
>>
>> @@ -2098,6 +2096,15 @@ static void sd_realize(DeviceState *dev, Error **errp)
>> }
>> blk_set_dev_ops(sd->blk, &sd_block_ops, sd);
>> }
>> +
>> + sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
>> +}
>> +
>> +static void sd_unrealize(DeviceState *dev, Error **errp)
>> +{
>> + SDState *sd = SD_CARD(dev);
>> +
>> + timer_del(sd->ocr_power_timer);
>> }
>
> Here too the old code was doing things correctly in that
> it does a timer_del/timer_free on the timer it allocates
> in instance_init, and the new code has weirdly split the
> freeing between unrealize and finalize.
Indeed I now see it, thanks.
>
> thanks
> -- PMM
>
© 2016 - 2025 Red Hat, Inc.