From nobody Tue Nov 26 04:23:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1709592169; cv=none; d=zohomail.com; s=zohoarc; b=f4Z38ii5L8BP6oVzVjR2YC2pXDcbtc59608rkztIKx7jBpfAUKxDk9F0DXptHZUq3QVLqtgNYmDJvx2rEIXojBasQSHzgFoNacHn6LTfIk4UvpiglOdZe85yrOWVdCIBRxbU9VRQut37r6lVtzMumEkWGc8480pHNzrt+2AKTGI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1709592169; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=aebmdz72auw7FdWgPSUo/EZDnyUGWLM7RYqlprxy43U=; b=YQMIEF2bE7IGTQL+U3yBSjLojrYzlY/leu04D2F2Ht6y2uoZ599bRXtlXk7tIfyThCm897+lSXA07rFRd1M2Vds8s4LHuvMGg3JxoFdqSRYrMJ93pZ6R1OGRJMLn/WmNw6VT/B3cl25REf8g/gEGA95o4OfgAKvi3DNDEPwL3v0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1709592169766282.964448707335; Mon, 4 Mar 2024 14:42:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rhH0W-0005Qa-Hh; Mon, 04 Mar 2024 17:42:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rhH0T-0005QN-Kt for qemu-devel@nongnu.org; Mon, 04 Mar 2024 17:42:01 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rhH0Q-0008WS-J1 for qemu-devel@nongnu.org; Mon, 04 Mar 2024 17:41:59 -0500 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-316-WKBbMBpgPhKaWzvoJ_9lFw-1; Mon, 04 Mar 2024 17:41:56 -0500 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40e53200380so32919455e9.3 for ; Mon, 04 Mar 2024 14:41:56 -0800 (PST) Received: from [192.168.10.118] ([151.49.77.21]) by smtp.gmail.com with ESMTPSA id bg14-20020a05600c3c8e00b004122b7a680dsm16130417wmb.21.2024.03.04.14.41.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 14:41:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709592117; 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; bh=aebmdz72auw7FdWgPSUo/EZDnyUGWLM7RYqlprxy43U=; b=iLTJWsynFyy4VZJ77Lb3sbYd0XzNANtPMHMdHuwFefUPYKIhNV/O5TyONDpdF/VfZJMCH2 VCqjIpZsOXz/aUiv/8cL2aelHj4O0c9HoD7+IEYBiImrKwUzUBZtbWKBI2+Br8jvgLv7xQ OG9bc6Y5/9vmRdJDMgfCP4tGHOPvxiw= X-MC-Unique: WKBbMBpgPhKaWzvoJ_9lFw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709592095; x=1710196895; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aebmdz72auw7FdWgPSUo/EZDnyUGWLM7RYqlprxy43U=; b=mZ5SRvE6J6HezyjDok1aKHTS1dInQNnZMtMvpHctTnYgSIrG/7QqC2W4X3YLpHII+T Py5Lsk0yuNS6Ila5SQfsqpKB/LjsJjBaIVv1+PORHSsGRyEcECSF0+sjW6cWw+tMYkIY Usxle6gYfKe7lYZmwKNIfSer0u5ciUJLL5et+A7Jw6be0y7G2lj56/25OUPXdHUdKNb6 XT9kVFdoDUi1uF3fZVy8bEZCwZoTGWgs7bWYazUN034fS5Y4ATA8aNCa6h+0ZsDUW747 Ya5JmA+eTqoX7vcidedQPT8h6fLda524wEEWKpOEfC5e8WoDvpmGVC72MOWD01TOpIbD TLlQ== X-Gm-Message-State: AOJu0YyONHfFUdhx258gBa4nfJMMtOlop6vkFPS8LAzSZObE2j//bG5z qQaJQ0Rd8NfgCfn2PJ0WF43lZ0T8j9w5RihHYW9ZdelN6n/n6bbtJjggVaBDrBPXiZ03egKVKdc Ctn/EbrxbWv/fNFrTtrMt5d6DisD8ke62aCDdQEoDJskxEBTIXBxSK0NNWLsntR2HKXR+el9gNt dkeSfm+PNqCZO+u6hpMheM+WJtUqygWA6BUN03 X-Received: by 2002:a05:600c:3b8e:b0:412:e4f9:2f4e with SMTP id n14-20020a05600c3b8e00b00412e4f92f4emr2222936wms.0.1709592095059; Mon, 04 Mar 2024 14:41:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqnVdE7GicBsJMudjUKdM0OPrSrWv+AnfO736J+RNN0P52ZveQ0Oq1sKjHfixBhjL6uSsyOQ== X-Received: by 2002:a05:600c:3b8e:b0:412:e4f9:2f4e with SMTP id n14-20020a05600c3b8e00b00412e4f92f4emr2222929wms.0.1709592094648; Mon, 04 Mar 2024 14:41:34 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: minhquangbui99@gmail.com Subject: [PATCH] hw/intc/apic: fix memory leak Date: Mon, 4 Mar 2024 23:41:33 +0100 Message-ID: <20240304224133.267640-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.43.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.571, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1709592171161100001 Content-Type: text/plain; charset="utf-8" deliver_bitmask is allocated on the heap in apic_deliver(), but there are many paths in the function that return before the corresponding g_free() is reached. Fix this by switching to g_autofree and, while at it, also switch to g_new. Do the same in apic_deliver_irq() as well for consistency. Fixes: b5ee0468e9d ("apic: add support for x2APIC mode", 2024-02-14) Signed-off-by: Paolo Bonzini Reviewed-by: Alex Benn=C3=A9e Reviewed-by: Bui Quang Minh Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/intc/apic.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/hw/intc/apic.c b/hw/intc/apic.c index 1d887d66b86..4186c57b34c 100644 --- a/hw/intc/apic.c +++ b/hw/intc/apic.c @@ -291,14 +291,13 @@ static void apic_deliver_irq(uint32_t dest, uint8_t d= est_mode, uint8_t delivery_mode, uint8_t vector_num, uint8_t trigger_mode) { - uint32_t *deliver_bitmask =3D g_malloc(max_apic_words * sizeof(uint32_= t)); + g_autofree uint32_t *deliver_bitmask =3D g_new(uint32_t, max_apic_word= s); =20 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num, trigger_mode); =20 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_m= ode); - g_free(deliver_bitmask); } =20 bool is_x2apic_mode(DeviceState *dev) @@ -662,7 +661,7 @@ static void apic_deliver(DeviceState *dev, uint32_t des= t, uint8_t dest_mode, APICCommonState *s =3D APIC(dev); APICCommonState *apic_iter; uint32_t deliver_bitmask_size =3D max_apic_words * sizeof(uint32_t); - uint32_t *deliver_bitmask =3D g_malloc(deliver_bitmask_size); + g_autofree uint32_t *deliver_bitmask =3D g_new(uint32_t, max_apic_word= s); uint32_t current_apic_id; =20 if (is_x2apic_mode(dev)) { @@ -708,7 +707,6 @@ static void apic_deliver(DeviceState *dev, uint32_t des= t, uint8_t dest_mode, } =20 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_m= ode); - g_free(deliver_bitmask); } =20 static bool apic_check_pic(APICCommonState *s) --=20 2.43.2