From mboxrd@z Thu Jan 1 00:00:00 1970 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 F0126C433EF for ; Tue, 24 May 2022 03:15:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233049AbiEXDPx (ORCPT ); Mon, 23 May 2022 23:15:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229946AbiEXDPs (ORCPT ); Mon, 23 May 2022 23:15:48 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83C5A8A045 for ; Mon, 23 May 2022 20:15:47 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4L6fT35gZZzjWws; Tue, 24 May 2022 11:15:03 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 11:15:45 +0800 Received: from [10.174.177.243] (10.174.177.243) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 11:15:44 +0800 Message-ID: Date: Tue, 24 May 2022 11:15:44 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.5.1 Subject: Re: [PATCH] amba: fix refcount underflow if amba_device_add() fails Content-Language: en-US To: Guenter Roeck CC: , , , References: <20220524030941.GA290608@roeck-us.net> From: Kefeng Wang In-Reply-To: <20220524030941.GA290608@roeck-us.net> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.243] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/5/24 11:09, Guenter Roeck wrote: > On Tue, May 24, 2022 at 10:51:39AM +0800, Kefeng Wang wrote: >> "ARM: 9192/1: amba: fix memory leak in amba_device_try_add()" leads >> to a refcount underflow if amba_device_add() fails, which called by >> of_amba_device_create(), the of_amba_device_create() already exists >> the error handling, so amba_put_device() only need to be added into >> amba_deferred_retry(). >> >> Reported-by: Guenter Roeck >> Signed-off-by: Kefeng Wang > Tested-by: Guenter Roeck Thanks. PS: I also test with kmemleak, this could also fix previous memory leak issue. > > Thanks, > Guenter > >> --- >> drivers/amba/bus.c | 8 ++------ >> 1 file changed, 2 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c >> index 0e3ed5eb367b..0cb20324da16 100644 >> --- a/drivers/amba/bus.c >> +++ b/drivers/amba/bus.c >> @@ -493,13 +493,8 @@ static int amba_device_try_add(struct amba_device *dev, struct resource *parent) >> goto skip_probe; >> >> ret = amba_read_periphid(dev); >> - if (ret) { >> - if (ret != -EPROBE_DEFER) { >> - amba_device_put(dev); >> - goto err_out; >> - } >> + if (ret) >> goto err_release; >> - } >> >> skip_probe: >> ret = device_add(&dev->dev); >> @@ -546,6 +541,7 @@ static int amba_deferred_retry(void) >> continue; >> >> list_del_init(&ddev->node); >> + amba_device_put(ddev->dev); >> kfree(ddev); >> } >> >> -- >> 2.35.3 >> > .