From patchwork Sat Feb 15 15:47:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 1238561 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LP8mi6bC; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48KZQ92N3Tz9sPJ for ; Sun, 16 Feb 2020 02:47:43 +1100 (AEDT) Received: from localhost ([::1]:52080 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2zfQ-00021i-2v for incoming@patchwork.ozlabs.org; Sat, 15 Feb 2020 10:47:40 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:44760) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2zf2-00021N-Sr for qemu-devel@nongnu.org; Sat, 15 Feb 2020 10:47:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2zf1-0001wI-5w for qemu-devel@nongnu.org; Sat, 15 Feb 2020 10:47:16 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:52003 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2zf1-0001uf-0g for qemu-devel@nongnu.org; Sat, 15 Feb 2020 10:47:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581781634; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XNbWLDpCo/qnGTCuXYHkPdCiNXWDiPn8sUeyQ6zeZVA=; b=LP8mi6bCXOIOe/Vo9Q8A+BN805uemSD5Xs/F91g/dCcC9gnzFELuRHMnEZqk4MZZRD0WoQ Q2lWf2o6XCLXCNdiFiftKH/KhO6zQTUBgXOn35VjtGsAij4Ne3Q2qOCWL4YxWUsCk9AD0Y GJFUQLGcgUd4XbzelbjdVDVkeIWQ6bg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-336-zQNTX_OuNcGhaS6xL9nZHw-1; Sat, 15 Feb 2020 10:47:12 -0500 Received: by mail-wr1-f71.google.com with SMTP id s13so5859685wru.7 for ; Sat, 15 Feb 2020 07:47:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bPBmIuXZ1qKaNiUhOTrGrZTnpv3fW77NVQ+xSDe0sYQ=; b=psbIZ6SbEu4H2b7KnfXHoB62FqXtuv0l/CuddGi/0pTZPcQiWFVNyccrwUnEvNLsx2 eXM4yvS+G8vYE3EcHU6eRNUV7JM79mgWln0Dt6VPo3//6f4cfssWUXkzWnUv37fdHHzx dhX7HZYHW1uClYDLp9uxChq6SOAqkx7Pw0f5QOl7VblYeBZcuWQxg0F34l6y/YET0XAp 8ZCaBrW0JhS0MrAaQl+hd7Aoyxe/GePaES6SayyWrEzzhEGSnz+RJysEgRVnokVirFCb P+z0B+IIetwVwbW6EkuYdkKgiw6dfI4aavbFzP7ajxzJr9sH3VTk+veFeevpJnM5zVpZ y6yA== X-Gm-Message-State: APjAAAXrfCjRZGyieCXPfq7GJnPSs1l8Uyj5A9SJfgk0wFSSDy72is9/ 1hW14QgHpdYejj60i9zJ+v8ASlRTr6kx6OXZ964yeHOFy15pWSOrW8mPHxNwvPMvTmIeNJ1on2S ZgkIFch+O4D4ZUpg= X-Received: by 2002:a7b:c94a:: with SMTP id i10mr11373841wml.88.1581781630819; Sat, 15 Feb 2020 07:47:10 -0800 (PST) X-Google-Smtp-Source: APXvYqyNY6nDnGoV0UOD0WO9OsoonEpBRwvAs7zK6s7tRA9Fxu8UJ2SmNpyLm3CW/shX62IjPDnm/g== X-Received: by 2002:a7b:c94a:: with SMTP id i10mr11373826wml.88.1581781630634; Sat, 15 Feb 2020 07:47:10 -0800 (PST) Received: from localhost.localdomain (78.red-88-21-202.staticip.rima-tde.net. [88.21.202.78]) by smtp.gmail.com with ESMTPSA id w26sm11766519wmi.8.2020.02.15.07.47.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Feb 2020 07:47:10 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH 1/2] hw/ipmi/bmc: Delay timer_new_ns() from init to realize to avoid memleaks Date: Sat, 15 Feb 2020 16:47:05 +0100 Message-Id: <20200215154706.19837-2-philmd@redhat.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200215154706.19837-1-philmd@redhat.com> References: <20200215154706.19837-1-philmd@redhat.com> MIME-Version: 1.0 X-MC-Unique: zQNTX_OuNcGhaS6xL9nZHw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pan Nengyuan , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?b?w6k=?= , Corey Minyard Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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é Acked-by: Corey Minyard --- Cc: Pan Nengyuan --- hw/ipmi/ipmi_bmc_extern.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/hw/ipmi/ipmi_bmc_extern.c b/hw/ipmi/ipmi_bmc_extern.c index f9a13e0a44..9144ac6c38 100644 --- a/hw/ipmi/ipmi_bmc_extern.c +++ b/hw/ipmi/ipmi_bmc_extern.c @@ -463,6 +463,15 @@ static void ipmi_bmc_extern_realize(DeviceState *dev, Error **errp) qemu_chr_fe_set_handlers(&ibe->chr, can_receive, receive, chr_event, NULL, ibe, NULL, true); + + ibe->extern_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, extern_timeout, ibe); +} + +static void ipmi_bmc_extern_unrealize(DeviceState *dev, Error **errp) +{ + IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(dev); + + timer_del(ibe->extern_timer); } static int ipmi_bmc_extern_post_migrate(void *opaque, int version_id) @@ -502,7 +511,6 @@ static void ipmi_bmc_extern_init(Object *obj) { IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(obj); - ibe->extern_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, extern_timeout, ibe); vmstate_register(NULL, 0, &vmstate_ipmi_bmc_extern, ibe); } @@ -510,7 +518,6 @@ static void ipmi_bmc_extern_finalize(Object *obj) { IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(obj); - timer_del(ibe->extern_timer); timer_free(ibe->extern_timer); } @@ -528,6 +535,7 @@ static void ipmi_bmc_extern_class_init(ObjectClass *oc, void *data) bk->handle_reset = ipmi_bmc_extern_handle_reset; dc->hotpluggable = false; dc->realize = ipmi_bmc_extern_realize; + dc->unrealize = ipmi_bmc_extern_unrealize; device_class_set_props(dc, ipmi_bmc_extern_properties); }