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 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 50137C30624 for ; Thu, 17 Aug 2023 14:32:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To: Subject:MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=L29dsfCuGdQPkYFwdjC+8YztBIyXhMZzGIUuxEyTatE=; b=HWnS7kaOV0RbJ1 17DXQ0A0/rCrksk6q42Oya5clliDuGJOJsG7hRytoSiZF1D6KDnSE9gcJ0fyDuQ80JTJcQ+2TQncU 1EpiVQUYpinbOSu7W8nX2Up4HakipkHKAjpHg8v4zSEwtOnRnut6csFtlleH0flsQSdS1SPF4kWxr tYmqkpOlWJGybrNAYC7Yai8ax760sw59TFhjVhFDGL/wisYLYwgmqOuAgAgXlje8eNT6QfJi4fuj5 DlGc4TomMg2G2haSy6LkZNcc8tL35XtngYeEXP/Yfu/nizZZ3wJrg8yIifD/x8hWlzv2gXIhlin0p cwVw9A9rwMkhT3Xa9r7w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qWe2X-006YQB-24; Thu, 17 Aug 2023 14:31:57 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qWe2U-006YPa-2b for linux-arm-kernel@lists.infradead.org; Thu, 17 Aug 2023 14:31:56 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 891BFD75; Thu, 17 Aug 2023 07:32:31 -0700 (PDT) Received: from [192.168.1.3] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2B4583F6C4; Thu, 17 Aug 2023 07:31:49 -0700 (PDT) Message-ID: Date: Thu, 17 Aug 2023 15:31:40 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH 2/2] coresight: core: fix memory leak in dict->fwnode_list Content-Language: en-US To: Junhao He , suzuki.poulose@arm.com, mike.leach@linaro.org, leo.yan@linaro.org Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linuxarm@huawei.com, jonathan.cameron@huawei.com, yangyicong@huawei.com, prime.zeng@hisilicon.com References: <20230817085937.55590-1-hejunhao3@huawei.com> <20230817085937.55590-3-hejunhao3@huawei.com> From: James Clark In-Reply-To: <20230817085937.55590-3-hejunhao3@huawei.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230817_073154_958738_7F73B626 X-CRM114-Status: GOOD ( 26.25 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 17/08/2023 09:59, Junhao He wrote: > There are memory leaks reported by kmemleak: > ... > unreferenced object 0xffff2020103c3200 (size 256): > comm "insmod", pid 4476, jiffies 4294978252 (age 50072.536s) > hex dump (first 32 bytes): > 10 60 40 06 28 20 ff ff 10 c0 59 06 20 20 ff ff .`@.( ....Y. .. > 10 e0 47 06 28 20 ff ff 10 00 49 06 28 20 ff ff ..G.( ....I.( .. > backtrace: > [<0000000034ec4724>] __kmem_cache_alloc_node+0x2f8/0x348 > [<0000000057fbc15d>] __kmalloc_node_track_caller+0x5c/0x110 > [<00000055d5e34b>] krealloc+0x8c/0x178 > [<00000000a4635beb>] coresight_alloc_device_name+0x128/0x188 [coresight] > [<00000000a92ddfee>] funnel_cs_ops+0x10/0xfffffffffffedaa0 [coresight_funnel] > [<00000000449e20f8>] dynamic_funnel_ids+0x80/0xfffffffffffed840 [coresight_funnel] > ... > > when remove driver, the golab variables defined by the macro > DEFINE_CORESIGHT_DEVLIST will be released, dict->nr_idx and > dict->fwnode_list are cleared to 0. The lifetime of the golab > variable has ended. So the buffer pointer is lost. > > Use the callback of devm_add_action_or_reset() to free memory. > > Fixes: 0f5f9b6ba9e1 ("coresight: Use platform agnostic names") > Signed-off-by: Junhao He > --- > drivers/hwtracing/coresight/coresight-core.c | 20 +++++++++++++++++++- > 1 file changed, 19 insertions(+), 1 deletion(-) > > diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c > index 9fabe00a40d6..6849faad697d 100644 > --- a/drivers/hwtracing/coresight/coresight-core.c > +++ b/drivers/hwtracing/coresight/coresight-core.c > @@ -1756,6 +1756,20 @@ bool coresight_loses_context_with_cpu(struct device *dev) > } > EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu); > > +void coresight_release_dev_list(void *data) > +{ > + struct coresight_dev_list *dict = data; > + > + mutex_lock(&coresight_mutex); > + > + if (dict->nr_idx) { > + kfree(dict->fwnode_list); > + dict->nr_idx = 0; > + } > + > + mutex_unlock(&coresight_mutex); > +} > + > /* > * coresight_alloc_device_name - Get an index for a given device in the > * device index list specific to a driver. An index is allocated for a > @@ -1766,12 +1780,16 @@ EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu); > char *coresight_alloc_device_name(struct coresight_dev_list *dict, > struct device *dev) > { > - int idx; > + int idx, ret; > char *name = NULL; > struct fwnode_handle **list; > > mutex_lock(&coresight_mutex); > > + ret = devm_add_action_or_reset(dev, coresight_release_dev_list, dict); > + if (ret) > + goto done; > + Hi Junhao, Changing the list allocator to a devm one fixes the issue without having to add the callback: - list = krealloc_array(dict->fwnode_list, + list = devm_krealloc_array(dev, dict->fwnode_list, The callback stands out a bit and would make someone reading it wonder why only that one is done that way but all other allocations in Coresight avoid it. The nr_idx variable doesn't need to be zeroed because its backed by a static variable and is zeroed when the module is reloaded as far as I can see. Thanks James > idx = coresight_search_device_idx(dict, dev_fwnode(dev)); > if (idx < 0) { > /* Make space for the new entry */ _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel 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 6E178C30624 for ; Thu, 17 Aug 2023 14:32:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351948AbjHQOcO (ORCPT ); Thu, 17 Aug 2023 10:32:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351955AbjHQObw (ORCPT ); Thu, 17 Aug 2023 10:31:52 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F41DC2D76 for ; Thu, 17 Aug 2023 07:31:50 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 891BFD75; Thu, 17 Aug 2023 07:32:31 -0700 (PDT) Received: from [192.168.1.3] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2B4583F6C4; Thu, 17 Aug 2023 07:31:49 -0700 (PDT) Message-ID: Date: Thu, 17 Aug 2023 15:31:40 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH 2/2] coresight: core: fix memory leak in dict->fwnode_list Content-Language: en-US To: Junhao He , suzuki.poulose@arm.com, mike.leach@linaro.org, leo.yan@linaro.org Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linuxarm@huawei.com, jonathan.cameron@huawei.com, yangyicong@huawei.com, prime.zeng@hisilicon.com References: <20230817085937.55590-1-hejunhao3@huawei.com> <20230817085937.55590-3-hejunhao3@huawei.com> From: James Clark In-Reply-To: <20230817085937.55590-3-hejunhao3@huawei.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 17/08/2023 09:59, Junhao He wrote: > There are memory leaks reported by kmemleak: > ... > unreferenced object 0xffff2020103c3200 (size 256): > comm "insmod", pid 4476, jiffies 4294978252 (age 50072.536s) > hex dump (first 32 bytes): > 10 60 40 06 28 20 ff ff 10 c0 59 06 20 20 ff ff .`@.( ....Y. .. > 10 e0 47 06 28 20 ff ff 10 00 49 06 28 20 ff ff ..G.( ....I.( .. > backtrace: > [<0000000034ec4724>] __kmem_cache_alloc_node+0x2f8/0x348 > [<0000000057fbc15d>] __kmalloc_node_track_caller+0x5c/0x110 > [<00000055d5e34b>] krealloc+0x8c/0x178 > [<00000000a4635beb>] coresight_alloc_device_name+0x128/0x188 [coresight] > [<00000000a92ddfee>] funnel_cs_ops+0x10/0xfffffffffffedaa0 [coresight_funnel] > [<00000000449e20f8>] dynamic_funnel_ids+0x80/0xfffffffffffed840 [coresight_funnel] > ... > > when remove driver, the golab variables defined by the macro > DEFINE_CORESIGHT_DEVLIST will be released, dict->nr_idx and > dict->fwnode_list are cleared to 0. The lifetime of the golab > variable has ended. So the buffer pointer is lost. > > Use the callback of devm_add_action_or_reset() to free memory. > > Fixes: 0f5f9b6ba9e1 ("coresight: Use platform agnostic names") > Signed-off-by: Junhao He > --- > drivers/hwtracing/coresight/coresight-core.c | 20 +++++++++++++++++++- > 1 file changed, 19 insertions(+), 1 deletion(-) > > diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c > index 9fabe00a40d6..6849faad697d 100644 > --- a/drivers/hwtracing/coresight/coresight-core.c > +++ b/drivers/hwtracing/coresight/coresight-core.c > @@ -1756,6 +1756,20 @@ bool coresight_loses_context_with_cpu(struct device *dev) > } > EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu); > > +void coresight_release_dev_list(void *data) > +{ > + struct coresight_dev_list *dict = data; > + > + mutex_lock(&coresight_mutex); > + > + if (dict->nr_idx) { > + kfree(dict->fwnode_list); > + dict->nr_idx = 0; > + } > + > + mutex_unlock(&coresight_mutex); > +} > + > /* > * coresight_alloc_device_name - Get an index for a given device in the > * device index list specific to a driver. An index is allocated for a > @@ -1766,12 +1780,16 @@ EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu); > char *coresight_alloc_device_name(struct coresight_dev_list *dict, > struct device *dev) > { > - int idx; > + int idx, ret; > char *name = NULL; > struct fwnode_handle **list; > > mutex_lock(&coresight_mutex); > > + ret = devm_add_action_or_reset(dev, coresight_release_dev_list, dict); > + if (ret) > + goto done; > + Hi Junhao, Changing the list allocator to a devm one fixes the issue without having to add the callback: - list = krealloc_array(dict->fwnode_list, + list = devm_krealloc_array(dev, dict->fwnode_list, The callback stands out a bit and would make someone reading it wonder why only that one is done that way but all other allocations in Coresight avoid it. The nr_idx variable doesn't need to be zeroed because its backed by a static variable and is zeroed when the module is reloaded as far as I can see. Thanks James > idx = coresight_search_device_idx(dict, dev_fwnode(dev)); > if (idx < 0) { > /* Make space for the new entry */