From patchwork Wed Nov 9 02:51:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yingliang X-Patchwork-Id: 623244 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E86E6C433FE for ; Wed, 9 Nov 2022 02:53:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229551AbiKICxF (ORCPT ); Tue, 8 Nov 2022 21:53:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229496AbiKICxE (ORCPT ); Tue, 8 Nov 2022 21:53:04 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30C8A1F61A for ; Tue, 8 Nov 2022 18:53:03 -0800 (PST) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4N6TvP1PZ0zpWHJ; Wed, 9 Nov 2022 10:49:21 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 10:53:01 +0800 Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 10:53:00 +0800 From: Yang Yingliang To: CC: , Subject: [PATCH v3 1/3] mmc: sdio: fix possible memory leak in sdio_init_func() Date: Wed, 9 Nov 2022 10:51:40 +0800 Message-ID: <20221109025142.1565445-2-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221109025142.1565445-1-yangyingliang@huawei.com> References: <20221109025142.1565445-1-yangyingliang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org If it fails in sdio_init_func(), sdio_remove_func() can not free the memory that allocated in sdio_alloc_func(), because sdio_add_func() is not called yet, the sdio function is not presented and sdio_remove_func() will return directly. In this error path, we can not call put_device(), because sdio_free_func_cis() is called in sdio_release_func(), if sdio_read_func_cis() fails, it will cause to put a reference that has not been got. So fix these leaks with calling kfree() instead of sdio_remove_func() in error path. Fixes: 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()") Signed-off-by: Yang Yingliang --- drivers/mmc/core/sdio.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/mmc/core/sdio.c b/drivers/mmc/core/sdio.c index f64b9ac76a5c..d1c6f18874aa 100644 --- a/drivers/mmc/core/sdio.c +++ b/drivers/mmc/core/sdio.c @@ -133,11 +133,8 @@ static int sdio_init_func(struct mmc_card *card, unsigned int fn) return 0; fail: - /* - * It is okay to remove the function here even though we hold - * the host lock as we haven't registered the device yet. - */ - sdio_remove_func(func); + kfree(func->tmpbuf); + kfree(func); return ret; } From patchwork Wed Nov 9 02:51:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yingliang X-Patchwork-Id: 624137 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F13BC43217 for ; Wed, 9 Nov 2022 02:53:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229447AbiKICxE (ORCPT ); Tue, 8 Nov 2022 21:53:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229551AbiKICxE (ORCPT ); Tue, 8 Nov 2022 21:53:04 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D8BC1FCC3 for ; Tue, 8 Nov 2022 18:53:03 -0800 (PST) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4N6Tw85PF2zHqVg; Wed, 9 Nov 2022 10:50:00 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 10:53:01 +0800 Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 10:53:01 +0800 From: Yang Yingliang To: CC: , Subject: [PATCH v3 2/3] mmc: sdio: fix of node refcount leak in sdio_add_func() Date: Wed, 9 Nov 2022 10:51:41 +0800 Message-ID: <20221109025142.1565445-3-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221109025142.1565445-1-yangyingliang@huawei.com> References: <20221109025142.1565445-1-yangyingliang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org If device_add() returns error in sdio_add_func(), sdio function is not presented, so the node refcount that hold in sdio_set_of_node() can not be put in sdio_remove_func() which is called from error path. Fix this by moving of_node_put() before present check in remove() function. Fixes: 25185f3f31c9 ("mmc: Add SDIO function devicetree subnode parsing") Signed-off-by: Yang Yingliang --- drivers/mmc/core/sdio_bus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c index babf21a0adeb..266639504a94 100644 --- a/drivers/mmc/core/sdio_bus.c +++ b/drivers/mmc/core/sdio_bus.c @@ -377,11 +377,11 @@ int sdio_add_func(struct sdio_func *func) */ void sdio_remove_func(struct sdio_func *func) { + of_node_put(func->dev.of_node); if (!sdio_func_present(func)) return; device_del(&func->dev); - of_node_put(func->dev.of_node); put_device(&func->dev); } From patchwork Wed Nov 9 02:51:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yingliang X-Patchwork-Id: 624136 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D84D7C4332F for ; Wed, 9 Nov 2022 02:54:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229496AbiKICxF (ORCPT ); Tue, 8 Nov 2022 21:53:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229582AbiKICxE (ORCPT ); Tue, 8 Nov 2022 21:53:04 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8F751F62F for ; Tue, 8 Nov 2022 18:53:03 -0800 (PST) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N6TzT25tFzRp5l; Wed, 9 Nov 2022 10:52:53 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500021.china.huawei.com (7.185.36.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 10:53:02 +0800 Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 10:53:01 +0800 From: Yang Yingliang To: CC: , Subject: [PATCH v3 3/3] mmc: sdio: fix possible memory leak in mmc_attach_sdio() Date: Wed, 9 Nov 2022 10:51:42 +0800 Message-ID: <20221109025142.1565445-4-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221109025142.1565445-1-yangyingliang@huawei.com> References: <20221109025142.1565445-1-yangyingliang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org If sdio_add_func() returns error in mmc_attach_sdio(), sdio_remove_func() can not free the memory that allocated in sdio_init_func(), because the sdio function is not presented and won't call put_device(). To fix these leaks, make sdio_func_present() only control whether device_del() needs to be called or not. Then, put_device() is called to give up the reference which was set in device_initialize(), the memory can be freed in sdio_release_func(). Fixes: 3d10a1ba0d37 ("sdio: fix reference counting in sdio_remove_func()") Signed-off-by: Yang Yingliang --- drivers/mmc/core/sdio_bus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c index 266639504a94..da561658bdae 100644 --- a/drivers/mmc/core/sdio_bus.c +++ b/drivers/mmc/core/sdio_bus.c @@ -377,11 +377,11 @@ int sdio_add_func(struct sdio_func *func) */ void sdio_remove_func(struct sdio_func *func) { - of_node_put(func->dev.of_node); - if (!sdio_func_present(func)) - return; + if (!sdio_func_present(func)) { + device_del(&func->dev); + } - device_del(&func->dev); + of_node_put(func->dev.of_node); put_device(&func->dev); }