From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 972522628D for ; Thu, 17 Apr 2025 13:46:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744897561; cv=none; b=Wp1RQ9qcWJl8Gzc1LICHkPe3XIwFEb5hvmBLRCu/d1W28n4oRhn/++Y2BqkrEjTDKY6fHsbaBnDKwSd3p/CyilPRL3LaIo53sYBP1EaEN/ZyE0aze1t45LUEcwRqM7XskGZz7Xj8i1BVreXmdHke0MbHp3tb4k2CM1EpkdJgf/c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744897561; c=relaxed/simple; bh=xDTLRbl0WZHZ6zo0BdnFdPiyGJKRjDitcLPuB8eKA0g=; h=Subject:To:Cc:From:Date:Message-ID:MIME-Version:Content-Type; b=PgvoCKj5k31zkQYuqegSUTlV8sfl9UiYKHLY3CPFaVnRuqfeZ9hqZeQt7lgfvpt+x9JlGPyTD99xihvaF8Q5/1q07849wjH5taRRybVrU4bMPkTcSFLGtJzrGTQPtIPj0CexRTT/RloWqYrIHr2gTdGDGlaJDNE1sM3CV50eFAs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=YY74+gIj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="YY74+gIj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95BF6C4CEEA; Thu, 17 Apr 2025 13:46:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1744897561; bh=xDTLRbl0WZHZ6zo0BdnFdPiyGJKRjDitcLPuB8eKA0g=; h=Subject:To:Cc:From:Date:From; b=YY74+gIjddf40P6G9ACXJmEJ+Rx54HFXCywcszBj/oTgbsjjr/36zpejmki+d5ef7 VT4ndIYeXi/5M/OX3Ya9Rg/DNM/do7jR7vzv18FjrWteEd3R1wrOKce0kNX6CVHv3o wF3/5jQ4Rzb9vevMqyjnNWteORocjBw0CWnFxSZU= Subject: FAILED: patch "[PATCH] PCI: Fix reference leak in pci_register_host_bridge()" failed to apply to 5.4-stable tree To: make24@iscas.ac.cn,bhelgaas@google.com Cc: From: Date: Thu, 17 Apr 2025 15:44:11 +0200 Message-ID: <2025041711-chaste-laxative-b7f3@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit The patch below does not apply to the 5.4-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.4.y git checkout FETCH_HEAD git cherry-pick -x 804443c1f27883926de94c849d91f5b7d7d696e9 # git commit -s git send-email --to '' --in-reply-to '2025041711-chaste-laxative-b7f3@gregkh' --subject-prefix 'PATCH 5.4.y' HEAD^.. Possible dependencies: thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From 804443c1f27883926de94c849d91f5b7d7d696e9 Mon Sep 17 00:00:00 2001 From: Ma Ke Date: Tue, 25 Feb 2025 10:14:40 +0800 Subject: [PATCH] PCI: Fix reference leak in pci_register_host_bridge() If device_register() fails, call put_device() to give up the reference to avoid a memory leak, per the comment at device_register(). Found by code review. Link: https://lore.kernel.org/r/20250225021440.3130264-1-make24@iscas.ac.cn Fixes: 37d6a0a6f470 ("PCI: Add pci_register_host_bridge() interface") Signed-off-by: Ma Ke [bhelgaas: squash Dan Carpenter's double free fix from https://lore.kernel.org/r/db806a6c-a91b-4e5a-a84b-6b7e01bdac85@stanley.mountain] Signed-off-by: Bjorn Helgaas Cc: stable@vger.kernel.org diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 5f04b8d9c736..dc37a3c0a977 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -953,6 +953,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) resource_size_t offset, next_offset; LIST_HEAD(resources); struct resource *res, *next_res; + bool bus_registered = false; char addr[64], *fmt; const char *name; int err; @@ -1017,6 +1018,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) name = dev_name(&bus->dev); err = device_register(&bus->dev); + bus_registered = true; if (err) goto unregister; @@ -1103,12 +1105,15 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) unregister: put_device(&bridge->dev); device_del(&bridge->dev); - free: #ifdef CONFIG_PCI_DOMAINS_GENERIC pci_bus_release_domain_nr(parent, bus->domain_nr); #endif - kfree(bus); + if (bus_registered) + put_device(&bus->dev); + else + kfree(bus); + return err; }