Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp213216lqp; Tue, 11 Jun 2024 21:41:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+u5MQDDsRJ8AfuTPiotpjZ/6SB8WmMJ1O0gm7MHmT04dYpgih8NpfEzUUqtxwOlSqNix2awfQXpLGtXIhctn9mRC/UzRkUVY1hOGXMA== X-Google-Smtp-Source: AGHT+IG4CVoSybjmSZiZBRhPKVeEpoH/EpGAp6gNdlzX67ukwyzQeBcAdM2TvUzlNB1wAsa+OoVq X-Received: by 2002:a05:6214:3bc3:b0:6b0:7dc7:8948 with SMTP id 6a1803df08f44-6b1a62a3ca1mr7047156d6.30.1718167267529; Tue, 11 Jun 2024 21:41:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718167267; cv=pass; d=google.com; s=arc-20160816; b=L7qmgLt5No3F5DGBUNHSPIrzWAKg7HylJBBNTJ18i1LqWfew6j0AfMtHyCb+cDj843 k+Ep7dyCC/tG1Eh2KeTBq2LYlSHojKIV+86btsWkgw+hiW5pd+X171MONTOlDylSg6AA 5ZaTzx3rCG/AdQEnNNl8nUdr43Vufvn5e9shfwFMoBbp6w08M+iCO3bHvCRGS25ea3JF LIMwXdymyNoA0rLBSO6+jjAiAm3DL+vANkw5LbLmd34m2rE144klh0XYfFICKpWIRkL4 EMRGXUqu0sJBR1g1MK6gI/n6eGzQIz8R0f8WEPZfid+6BcJQXjZ6aKnTarwJIVlT/KoT /Ezg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=AGgkSoW1xRk7TfsRGt2QaxlKvCEt9TPGalxUOEqx2iw=; fh=x2hhG8i5Tzk5RobrjuahwqTI4zCYbKVNQU2ymXDybMI=; b=H8qh/pzYh4icDPQGFjFV6LtalyloGcTxMYfPGMviPy6MTTE/HymzrMuIc357jv9Y6h UKcNVFzi56mEC4jxuhPe5i5/Okih16bc9ITJ38jlmUuN0PkrYneMU1KxVvD67HJ/PyeO qFbDwomC0UUOP6jS0RZAnn7bNfpdAr5C/NxK6xHCqpY0vFXjAq02QSUhS5CRew6uKmZg rpqALcP0cyCaZ0srTcRLuooaSu5IDAcPW/kwhdqokQ4Uuhei2rAuYgmYMnOZLxIN2ZSH Ol8dU7KVl3MovCPdaWE9sUCTJVPpcEIjOvY/6DdaA4EHSav9X9ez1D2A1mrhgNEmTTtq IH/w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="ap3df75/"; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-210917-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210917-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6b07ab9280bsi71140916d6.14.2024.06.11.21.41.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 21:41:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-210917-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="ap3df75/"; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-210917-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210917-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 30C431C239E7 for ; Wed, 12 Jun 2024 04:41:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F80B2868D; Wed, 12 Jun 2024 04:40:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ap3df75/" Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 032FD20DFF for ; Wed, 12 Jun 2024 04:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718167257; cv=none; b=AhrVe/I8g5pufDlCKW0nXDy0aeiqCDbWsgMK8+OCs9hfNbvD/vv1+qe04FyDy23sfEWv56hqfYXvfc2wAq2o8kOk+gkX+EQi3vNEyBdoj12UZ5oeQawLNF6v9CLlul6bLKGmaGK24rByk4Hy9rYe71ylHj9I9OPDeWc1iWy2sjA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718167257; c=relaxed/simple; bh=1gLwKb9OxHOVAk7+EmjyO/V2qkvJVd8cvMeHIOPMXRc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dOka7lbOtBNXAV1qj5PKMcStt8M4T9PK/PwSIU5bcd/n/ECC1i1UzQhTOvAdrLXk3vbUjDno1rxmdKgwJherDYnYJKaZxp/eQw9SNoyj4JyOg8R4c+F1RycT9Q07rP05V17FKAEvfRVA6Gx1vXs/MidwS7Rz010q1RL4kRjxbIU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=ap3df75/; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-6e57506bb2dso1451475a12.0 for ; Tue, 11 Jun 2024 21:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718167255; x=1718772055; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=AGgkSoW1xRk7TfsRGt2QaxlKvCEt9TPGalxUOEqx2iw=; b=ap3df75/wCXHU8qY0th7cbYD8vX89MLozR6Jnx63zmSY4pBM2FmlqKDnJTzBtyGTZr LC4geehH3tM8YbltezthqKLxZJTEuot/ZnrsjBiFoC/hgpivtPNJOIHW2+bB7QewMI8W qRTQw4dvEM5NdiRStba1wiNO7Pz9CrA5VhvrXPzerWqFMReXKPDuUGqkrlRNx7RZxuog TYvOMvN5jmnqvd2BSY4mWJZKanlpfveXskXpJ0GpUk0mMHo+mMaQfy1w5F28NhAJV+v8 qrvtFslvuqZFUobC6ENOdfv29V+AWmfpFdEYgAGErDjeiZEoProFKA90MQwyf+s36NkA BwOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718167255; x=1718772055; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AGgkSoW1xRk7TfsRGt2QaxlKvCEt9TPGalxUOEqx2iw=; b=rMoVNIR5kx2r1wHyfuAQl911LrljRv0VerfePbfne5O3QdZY3VjN4P3dVRvfpXKWRx z9hIeQVieDyjzjeoj8z/ZCl2Orh0nCejZyBv0nZ6+JQFG25UTLgK+9foTd81rkk5FtQ3 vpetege0BcETuUTxx1Lmzj0kes+uV3aCB4hJk8vMaY/JvHEjCljkilHmLurtl/i3Ns5J gvA1Ie1gGEHD9em7S7g4gq5hRUWumWrjHkg8eLNyrnDG2k6snNFWgkwzw8IP139JV4UP LuraN+9cnY/1RIuF1n5+w8AGi58PhBYTGR1Vh9um1ceNlWKhOEkd1HinipQPnBCBGxZp wSTQ== X-Forwarded-Encrypted: i=1; AJvYcCXmOAg5F2tlhI5eLnX36ae49KVQ6XBDPBM3hSU5eeQ9U013Dj0P++YnIxxdQ4G/hcsZMrTMYGFUEe02E7Q0hcFqMWerkmGpVYbQpFJQ X-Gm-Message-State: AOJu0YynPjLLg6W5fhXgQZQPMTXhJHq1w/2d7oHkKwx7P/md07M07U9e G/yApLWmDUQIEthKjBxcLGzNCLac4OV5R9LSZapiRwMWvnhmJJPmbUd3rCq1pg== X-Received: by 2002:a05:6a20:8417:b0:1b5:d143:72e7 with SMTP id adf61e73a8af0-1b8a9b8c091mr959952637.32.1718167254831; Tue, 11 Jun 2024 21:40:54 -0700 (PDT) Received: from thinkpad ([120.60.129.29]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a9c2fa34sm481444a91.38.2024.06.11.21.40.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 21:40:54 -0700 (PDT) Date: Wed, 12 Jun 2024 10:10:47 +0530 From: Manivannan Sadhasivam To: Javier Carrasco Cc: Xiaowei Song , Binghui Wang , Lorenzo Pieralisi , Krzysztof =?utf-8?Q?Wilczy=C5=84ski?= , Rob Herring , Bjorn Helgaas , Linus Walleij , Andy Shevchenko , Krzysztof =?utf-8?Q?Wilczy=C5=84ski?= , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] PCI: kirin: fix memory leak in kirin_pcie_parse_port() Message-ID: <20240612044047.GD2645@thinkpad> References: <20240609-pcie-kirin-memleak-v1-1-62b45b879576@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240609-pcie-kirin-memleak-v1-1-62b45b879576@gmail.com> On Sun, Jun 09, 2024 at 12:56:14PM +0200, Javier Carrasco wrote: > The conversion of this file to use the agnostic GPIO API has introduced > a new early return where the refcounts of two device nodes (parent and > child) are not decremented. > > Given that the device nodes are not required outside the loops where > they are used, and to avoid potential bugs every time a new error path > is introduced to the loop, the _scoped() versions of the macros have > been applied. The bug was introduced recently, and the fix is not > relevant for old stable kernels that might not support the scoped() > variant. > > Fixes: 1d38f9d89f85 ("PCI: kirin: Convert to use agnostic GPIO API") > Signed-off-by: Javier Carrasco Reviewed-by: Manivannan Sadhasivam > --- > This bug was found while analyzing the code and I don't have hardware to > validate it beyond compilation and static analysis. Any test with real > hardware to make sure there are no regressions is always welcome. > > The dev_err() messages have not been converted into dev_err_probe() to > keep the current format, but I am open to convert them if preferred. Sure, please do it in a separate patch. - Mani > --- > drivers/pci/controller/dwc/pcie-kirin.c | 21 ++++++--------------- > 1 file changed, 6 insertions(+), 15 deletions(-) > > diff --git a/drivers/pci/controller/dwc/pcie-kirin.c b/drivers/pci/controller/dwc/pcie-kirin.c > index d1f54f188e71..0a29136491b8 100644 > --- a/drivers/pci/controller/dwc/pcie-kirin.c > +++ b/drivers/pci/controller/dwc/pcie-kirin.c > @@ -403,11 +403,10 @@ static int kirin_pcie_parse_port(struct kirin_pcie *pcie, > struct device_node *node) > { > struct device *dev = &pdev->dev; > - struct device_node *parent, *child; > int ret, slot, i; > > - for_each_available_child_of_node(node, parent) { > - for_each_available_child_of_node(parent, child) { > + for_each_available_child_of_node_scoped(node, parent) { > + for_each_available_child_of_node_scoped(parent, child) { > i = pcie->num_slots; > > pcie->id_reset_gpio[i] = devm_fwnode_gpiod_get_index(dev, > @@ -424,14 +423,13 @@ static int kirin_pcie_parse_port(struct kirin_pcie *pcie, > pcie->num_slots++; > if (pcie->num_slots > MAX_PCI_SLOTS) { > dev_err(dev, "Too many PCI slots!\n"); > - ret = -EINVAL; > - goto put_node; > + return -EINVAL; > } > > ret = of_pci_get_devfn(child); > if (ret < 0) { > dev_err(dev, "failed to parse devfn: %d\n", ret); > - goto put_node; > + return ret; > } > > slot = PCI_SLOT(ret); > @@ -439,10 +437,8 @@ static int kirin_pcie_parse_port(struct kirin_pcie *pcie, > pcie->reset_names[i] = devm_kasprintf(dev, GFP_KERNEL, > "pcie_perst_%d", > slot); > - if (!pcie->reset_names[i]) { > - ret = -ENOMEM; > - goto put_node; > - } > + if (!pcie->reset_names[i]) > + return -ENOMEM; > > gpiod_set_consumer_name(pcie->id_reset_gpio[i], > pcie->reset_names[i]); > @@ -450,11 +446,6 @@ static int kirin_pcie_parse_port(struct kirin_pcie *pcie, > } > > return 0; > - > -put_node: > - of_node_put(child); > - of_node_put(parent); > - return ret; > } > > static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie, > > --- > base-commit: d35b2284e966c0bef3e2182a5c5ea02177dd32e4 > change-id: 20240609-pcie-kirin-memleak-18c83a31d111 > > Best regards, > -- > Javier Carrasco > -- மணிவண்ணன் சதாசிவம்