Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10430676ybi; Wed, 24 Jul 2019 23:05:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzRSV4tn8gycU4Cew1ne71Er1kpxPTgJSLu6r84Hmg9JQatg/PJdlOMKomfJegm5e8KT77m X-Received: by 2002:a17:902:2868:: with SMTP id e95mr83612514plb.319.1564034712257; Wed, 24 Jul 2019 23:05:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564034712; cv=none; d=google.com; s=arc-20160816; b=NUb+m6s8NTDQeZATIkPOMQtYnUXxc8slOmDn3Gu16JnLiHQaf94d1zgLaC7TuDfqcC ZOe4dayyuqq7Rfpm68MTdxqUBn8ZguRUGspVVvVdkFZ7TFWCAvpPjF/Gi0pr0h4h5Q27 AsufvZsIYc6CatJ+1Z5iVqBmW34dbDGaPKAPK8wYbHoDdz6ybBCcRedKteSdhw1QlfcR HyIcgvHHGSuticq4CKHuMI4KTPepe2WbMlnLaq4CHgIDHjko+Z69gF30LY9Xc97PTHkk GhXqrDVZi9FOlGcqRJkhGEV31RZCSOP8i3K7QL3vrpdnzLiuCh7EiQEnVRaug2A9TeC0 isUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2snbHuA9iU7cMHE1YbXlot4BTG8PIPuSFXizYtz/CXc=; b=N0/TTYkBMJEwjHKEo68QQyK0JnalzkQ2MDHh9L8Xt3xv856K/eNEtfkneOuS65AMXT LAwrIfNn2RftOp7qGb6z9igXJmax8iV3WjAbk8hH3Vd7q1oSAvgKxUf96URH0RpGBn7V vzqHn8WtPSpDQ2aXZEbAt7V/aIyLbvhqNoFMdBd2QNGEBlsIJpPqvABR4LwCXgjHIyro lokGD7UAE3JfeKS5x+VeAOAECJ+ofcqofS75ju8vWa32whNXA6+Ifz8LLvQz1tFovasa LOZZda1YCcDp3hyDY3Tz82VTZI9rvcBZv+WC8ey51jDbWu0PDBrDLYAXJ19D532MasT/ bNcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jgkMexdH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p10si14147482plk.9.2019.07.24.23.04.57; Wed, 24 Jul 2019 23:05:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jgkMexdH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729213AbfGYFrJ (ORCPT + 99 others); Thu, 25 Jul 2019 01:47:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:35058 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729127AbfGYFrI (ORCPT ); Thu, 25 Jul 2019 01:47:08 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EEB8C21880; Thu, 25 Jul 2019 05:47:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564033627; bh=Hx3GPueWs8BDnpYvzNlYXekwIYTPfzPqRDI2b1wPrIU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jgkMexdHd2qoLqplogts2gcXQcuoYGKSDj6M1nODmfXNA0i4C9WYYuuvU8UOHROv0 fPIK5ZGcSOj7tFpVknFXeuVxtzlppWdTnyHxU4NXRC5NfPNotmEROZXxb2LB0/AMxT B6LkW/GZztx6c5gP9lvpw47vbR6B8QXZZmXop2mU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Greg Kurz , Alexey Kardashevskiy , Michael Ellerman Subject: [PATCH 4.19 257/271] powerpc/powernv/npu: Fix reference leak Date: Wed, 24 Jul 2019 21:22:06 +0200 Message-Id: <20190724191717.232074055@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191655.268628197@linuxfoundation.org> References: <20190724191655.268628197@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kurz commit 02c5f5394918b9b47ff4357b1b18335768cd867d upstream. Since 902bdc57451c, get_pci_dev() calls pci_get_domain_bus_and_slot(). This has the effect of incrementing the reference count of the PCI device, as explained in drivers/pci/search.c: * Given a PCI domain, bus, and slot/function number, the desired PCI * device is located in the list of PCI devices. If the device is * found, its reference count is increased and this function returns a * pointer to its data structure. The caller must decrement the * reference count by calling pci_dev_put(). If no device is found, * %NULL is returned. Nothing was done to call pci_dev_put() and the reference count of GPU and NPU PCI devices rockets up. A natural way to fix this would be to teach the callers about the change, so that they call pci_dev_put() when done with the pointer. This turns out to be quite intrusive, as it affects many paths in npu-dma.c, pci-ioda.c and vfio_pci_nvlink2.c. Also, the issue appeared in 4.16 and some affected code got moved around since then: it would be problematic to backport the fix to stable releases. All that code never cared for reference counting anyway. Call pci_dev_put() from get_pci_dev() to revert to the previous behavior. Fixes: 902bdc57451c ("powerpc/powernv/idoa: Remove unnecessary pcidev from pci_dn") Cc: stable@vger.kernel.org # v4.16 Signed-off-by: Greg Kurz Reviewed-by: Alexey Kardashevskiy Signed-off-by: Michael Ellerman Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/platforms/powernv/npu-dma.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) --- a/arch/powerpc/platforms/powernv/npu-dma.c +++ b/arch/powerpc/platforms/powernv/npu-dma.c @@ -56,9 +56,22 @@ static struct dentry *atsd_threshold_den static struct pci_dev *get_pci_dev(struct device_node *dn) { struct pci_dn *pdn = PCI_DN(dn); + struct pci_dev *pdev; - return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus), + pdev = pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus), pdn->busno, pdn->devfn); + + /* + * pci_get_domain_bus_and_slot() increased the reference count of + * the PCI device, but callers don't need that actually as the PE + * already holds a reference to the device. Since callers aren't + * aware of the reference count change, call pci_dev_put() now to + * avoid leaks. + */ + if (pdev) + pci_dev_put(pdev); + + return pdev; } /* Given a NPU device get the associated PCI device. */