From patchwork Mon Aug 26 15:30:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 13778071 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6CD8EC5472D for ; Mon, 26 Aug 2024 15:32:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=lsUAW421feHZLLrllyEzrWeyn6bG6Cl3vLexu1MBLcQ=; b=cVZNIydjkqbFK00Sm3bR+1m9zT Z0/ckbVfZQHzzzjDFyQcLsQAIVKB0guVPMeQufVNgkkyxYAG9CGx4Ym2TjlIKGDx/DSDF31nOpv/+ SA8EVvI+nzTNVXPdAUJuY8BaAxFE9FDJIOdPCmktYzWkONRMSXFRbikjRcBA5A428380MnhtzS97E qIoJzCuCImU6NU6uy9EcRTosy58vsyMtfhY71HqM2Gh5aY/rXESays7w58ehBd4HOFxLXcnlq9cP2 hfoZvP45tibPuh3LsENR1ZkNUXFGxWaKgu5xEijg2jln4buVMwIS297919vkdixfhhEhPtKlQK5ga HOhw2YjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sibhY-00000007pgY-0J9o; Mon, 26 Aug 2024 15:32:16 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sibfj-00000007p55-1fKz; Mon, 26 Aug 2024 15:30:25 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 686AAE0009; Mon, 26 Aug 2024 15:30:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1724686221; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=lsUAW421feHZLLrllyEzrWeyn6bG6Cl3vLexu1MBLcQ=; b=g58JW+ktxXELaoG423XuQGR7u0DVJX1EDzrb7kIO8lLpoIDgIm82owuF6qdbxI5FjxlMfe BnfkjRJq0G6nO1/RNJUCpvXTTYgM8N7pCvtVJPqFPpaDpstQmtwLFwAitgJvxzNMbMBxiU DXPeZxg2SF0rPTZB9njnOCxOkABUifmRKOLl0kLQXAK+THDmPhR3eCopgl7ozjH+e6u/jc YYzzUHfh4F8BWMaHWvXw9de0NdCmwJX+NGG9BCBxnIQTLEPqpf16FFA6pw1JxSRi+WxgSN VqoBVwXVwo6C5c6yEc4DBoJr8SmRzIIIs+VXIqmVtL1PYWsOrae+pEf3u4Us6g== From: Miquel Raynal To: Richard Weinberger , Vignesh Raghavendra , Tudor Ambarus , Pratyush Yadav , Michael Walle , Cc: Matthias Brugger , AngeloGioacchino Del Regno , , Thomas Petazzoni , Miquel Raynal Subject: [PATCH next 1/2] mtd: rawnand: mtk: Factorize out the logic cleaning mtk chips Date: Mon, 26 Aug 2024 17:30:18 +0200 Message-ID: <20240826153019.67106-1-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240826_083023_906991_5A3016A3 X-CRM114-Status: GOOD ( 11.42 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org There are some un-freed resources in one of the error path which would benefit from a helper going through all the registered mtk chips one by one and perform all the necessary cleanup. This is precisely what the remove path does, so let's extract the logic in a helper. There is no functional change. Signed-off-by: Miquel Raynal Reviewed-by: Pratyush Yadav Reviewed-by: Matthias Brugger --- drivers/mtd/nand/raw/mtk_nand.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/drivers/mtd/nand/raw/mtk_nand.c b/drivers/mtd/nand/raw/mtk_nand.c index d65e6371675b..bf845dd16737 100644 --- a/drivers/mtd/nand/raw/mtk_nand.c +++ b/drivers/mtd/nand/raw/mtk_nand.c @@ -1429,6 +1429,23 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc, return 0; } +static void mtk_nfc_nand_chips_cleanup(struct mtk_nfc *nfc) +{ + struct mtk_nfc_nand_chip *mtk_chip; + struct nand_chip *chip; + int ret; + + while (!list_empty(&nfc->chips)) { + mtk_chip = list_first_entry(&nfc->chips, + struct mtk_nfc_nand_chip, node); + chip = &mtk_chip->nand; + ret = mtd_device_unregister(nand_to_mtd(chip)); + WARN_ON(ret); + nand_cleanup(chip); + list_del(&mtk_chip->node); + } +} + static int mtk_nfc_nand_chips_init(struct device *dev, struct mtk_nfc *nfc) { struct device_node *np = dev->of_node; @@ -1567,20 +1584,8 @@ static int mtk_nfc_probe(struct platform_device *pdev) static void mtk_nfc_remove(struct platform_device *pdev) { struct mtk_nfc *nfc = platform_get_drvdata(pdev); - struct mtk_nfc_nand_chip *mtk_chip; - struct nand_chip *chip; - int ret; - - while (!list_empty(&nfc->chips)) { - mtk_chip = list_first_entry(&nfc->chips, - struct mtk_nfc_nand_chip, node); - chip = &mtk_chip->nand; - ret = mtd_device_unregister(nand_to_mtd(chip)); - WARN_ON(ret); - nand_cleanup(chip); - list_del(&mtk_chip->node); - } + mtk_nfc_nand_chips_cleanup(nfc); mtk_ecc_release(nfc->ecc); } From patchwork Mon Aug 26 15:30:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 13778070 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5F9C0C5321E for ; Mon, 26 Aug 2024 15:31:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=QM2d/CuDoeK7efPjvSGZdvRUED2mLtmBql6lzH7ZOJ0=; b=uPww1U7K91pSbQQCyKbu1TdDNT GW7bLjHdc5PjygggD3/DB85PJYh8ynBGw0pRS9q1e64d5Cworovr6mHaChRgqOy1V27x+6GDp772X u9HkYhSAPCSPMrry2RODJQqtldztvgQZhSM7aF4lz37WTsjcENr+byw+/DiTKi5PueO6N0WUVQuO3 HE+iXs9Gnfjb6LJHBynERHR6O5M0QL1AvZFFJfzFi5S3BzLNW8ZyBuBpBjaYVrEDSzWx6e3ONOPZ7 ePEK2giw1lBGKEM7plFthDo8bqA0V8mxuSWYf7pvNKm9NykyHENWkQ25MUrzYNzfwQ9gJTFMiFMTs oswcsmZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sibge-00000007pK7-0pyx; Mon, 26 Aug 2024 15:31:20 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sibfj-00000007p5D-2YkD; Mon, 26 Aug 2024 15:30:25 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 61763E0006; Mon, 26 Aug 2024 15:30:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1724686221; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QM2d/CuDoeK7efPjvSGZdvRUED2mLtmBql6lzH7ZOJ0=; b=DMTKuUKb8qV/8FWvf04ifbAzsP1p6kbaUjEKCAgIXkoy65CpvV5cFYaBEI0zVndJbwiIbe 13fHGeq3bjAuZ3GzUkWlEVsh+1rHPZuY2jXJqnlERXRoE1JaCFjfBe7n8+OCwKXidtzck2 Tyq1z3gZCAvDq/KQGIgNwixYO0XtKxBL9ptj91dpaKzY03PvdKHkBvEyTSUgvEWB3cqgAe ntkvEzto3U1tLrwZLhDGW3j9uuQ+1ThyEgKrUgEzyCTvJggsrPA/+NsWs776f4PApluwd5 P+S43f/pH551H+N8qcPr/QWCraOLYYcoK5B376+OqheQl0AZiKEdZltsFcztfQ== From: Miquel Raynal To: Richard Weinberger , Vignesh Raghavendra , Tudor Ambarus , Pratyush Yadav , Michael Walle , Cc: Matthias Brugger , AngeloGioacchino Del Regno , , Thomas Petazzoni , Miquel Raynal Subject: [PATCH next 2/2] mtd: rawnand: mtk: Fix init error path Date: Mon, 26 Aug 2024 17:30:19 +0200 Message-ID: <20240826153019.67106-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240826153019.67106-1-miquel.raynal@bootlin.com> References: <20240826153019.67106-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: miquel.raynal@bootlin.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240826_083023_907603_A53B5EFE X-CRM114-Status: GOOD ( 12.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Reviewing a series converting the for_each_chil_of_node() loops into their _scoped variants made me realize there was no cleanup of the already registered NAND devices upon error which may leak memory on systems with more than a chip when this error occurs. We should call the _nand_chips_cleanup() function when this happens. Fixes: Fixes: 1d6b1e464950 ("mtd: mediatek: driver for MTK Smart Device") Signed-off-by: Miquel Raynal Reviewed-by: Pratyush Yadav Reviewed-by: Matthias Brugger --- Backporting this change will probably fail. In practice, the memory leak is very unlikely to happen so I guess we can live without it. --- drivers/mtd/nand/raw/mtk_nand.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/nand/raw/mtk_nand.c b/drivers/mtd/nand/raw/mtk_nand.c index bf845dd16737..586868b4139f 100644 --- a/drivers/mtd/nand/raw/mtk_nand.c +++ b/drivers/mtd/nand/raw/mtk_nand.c @@ -1453,8 +1453,10 @@ static int mtk_nfc_nand_chips_init(struct device *dev, struct mtk_nfc *nfc) for_each_child_of_node_scoped(np, nand_np) { ret = mtk_nfc_nand_chip_init(dev, nfc, nand_np); - if (ret) + if (ret) { + mtk_nfc_nand_chips_cleanup(nfc); return ret; + } } return 0;