Message ID | 20221203095858.612027-1-liuyongqiang13@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1297586wrr; Sat, 3 Dec 2022 02:17:19 -0800 (PST) X-Google-Smtp-Source: AA0mqf5AlYLpZxWVHNKsIEcASJ+yMUr8CHcdXyof1bDrRsp9a1DP2TC4ROJ46vlVOYAmw1Y3Pl4d X-Received: by 2002:a17:90b:2686:b0:218:bb0a:e295 with SMTP id pl6-20020a17090b268600b00218bb0ae295mr63411432pjb.80.1670062638898; Sat, 03 Dec 2022 02:17:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670062638; cv=none; d=google.com; s=arc-20160816; b=niBmklhut4fvEG4Gf7yfj3foQ4bNoqRjo99bf5n7AJVOZLzxyc6s4W1WpDpoWbBGaM wrc8lZNcG77JDY8PRlyxBMaq4L1e5/ISQnZf+lue2KRrHH/EFqkeJF/EoKNblYkrgF5Q J90Ds1PTyag0I6ML2lUfvce55DenRDla19tkYAYxHntAfoUG8mcmb8IyzXGyKWjgFssK WBxaURO9aNOVUBgHBVOh60AFirVUayBdFc333FhJECAyfH8LGp+b0jhbHXvSa8fQWV9v 9+3jS9aptIjN0cqR9rzYkiXx+DC/V/1isnskRTRdaiTVg6RqUT60SWInUOq7Ex/eB+x6 yXJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=oFg00KuOTSwhDW7kJBE1uIGg27+gjxsFj7JlkN/0YA0=; b=lK5jqb6h1mmSPcVGmGhMaNn9ueMYgf256yXQW/KNELDz4ojZrQ4r/iu8PGkDSlBb6s 6Q8nm+NvUk6QmgPbNqPms2FpluSWB/IGGc+CWCYzYjW4ygSwEj7DBfD3RrQ+bISyTLxh yGjL5z6uhqYeEXUBrGc/igI+oBwmTwoDIE7Dl2xEeuTDwn/+UkkG27z4E0YU+c9dQCL8 nzbTqlTnGImgEYzAwVDYOU/JgDXEnIgNDMGRb/Bb8xSJ1f4tfiQNmO3TD0U4YHTs15h0 qGBLzXYXQQ4gIHrvhNiRD1a0VMMeZcz8v5ujt6yHVTM80Sl84oBZaLF6CIXyAOIkvzJF pQWw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c8-20020a17090aa60800b00213120e0c85si11875726pjq.156.2022.12.03.02.17.05; Sat, 03 Dec 2022 02:17:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229606AbiLCJ7L (ORCPT <rfc822;lhua1029@gmail.com> + 99 others); Sat, 3 Dec 2022 04:59:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbiLCJ7I (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 3 Dec 2022 04:59:08 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE1B219001 for <linux-kernel@vger.kernel.org>; Sat, 3 Dec 2022 01:59:07 -0800 (PST) Received: from dggpeml500005.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NPQCS03vBzqScY; Sat, 3 Dec 2022 17:55:00 +0800 (CST) Received: from huawei.com (10.175.112.125) by dggpeml500005.china.huawei.com (7.185.36.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 3 Dec 2022 17:59:05 +0800 From: Yongqiang Liu <liuyongqiang13@huawei.com> To: <linux-kernel@vger.kernel.org>, <nvdimm@lists.linux.dev> CC: <dan.j.williams@intel.com>, <vishal.l.verma@intel.com>, <dave.jiang@intel.com>, <akpm@linux-foundation.org>, <joao.m.martins@oracle.com>, <zhangxiaoxu5@huawei.com>, <liuyongqiang13@huawei.com> Subject: [PATCH] dax/hmem: Fix refcount leak in dax_hmem_probe() Date: Sat, 3 Dec 2022 09:58:58 +0000 Message-ID: <20221203095858.612027-1-liuyongqiang13@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500005.china.huawei.com (7.185.36.59) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751187601713395365?= X-GMAIL-MSGID: =?utf-8?q?1751187601713395365?= |
Series |
dax/hmem: Fix refcount leak in dax_hmem_probe()
|
|
Commit Message
Yongqiang Liu
Dec. 3, 2022, 9:58 a.m. UTC
We should always call dax_region_put() whenever devm_create_dev_dax()
succeed or fail to avoid refcount leak of dax_region. Move the return
value check after dax_region_put().
Fixes: c01044cc8191 ("ACPI: HMAT: refactor hmat_register_target_device to hmem_register_device")
Signed-off-by: Yongqiang Liu <liuyongqiang13@huawei.com>
---
drivers/dax/hmem/hmem.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
Comments
On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: > We should always call dax_region_put() whenever devm_create_dev_dax() > succeed or fail to avoid refcount leak of dax_region. Move the return > value check after dax_region_put(). I think dax_region_put is called from dax_region_unregister() automatically on tear down. > > Fixes: c01044cc8191 ("ACPI: HMAT: refactor hmat_register_target_device to hmem_register_device") I'm also not sure how this patch is related to this fix. Ira > Signed-off-by: Yongqiang Liu <liuyongqiang13@huawei.com> > --- > drivers/dax/hmem/hmem.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c > index 1bf040dbc834..09f5cd7b6c8e 100644 > --- a/drivers/dax/hmem/hmem.c > +++ b/drivers/dax/hmem/hmem.c > @@ -36,12 +36,11 @@ static int dax_hmem_probe(struct platform_device *pdev) > .size = region_idle ? 0 : resource_size(res), > }; > dev_dax = devm_create_dev_dax(&data); > - if (IS_ERR(dev_dax)) > - return PTR_ERR(dev_dax); > > /* child dev_dax instances now own the lifetime of the dax_region */ > dax_region_put(dax_region); > - return 0; > + > + return IS_ERR(dev_dax) ? PTR_ERR(dev_dax) : 0; > } > > static int dax_hmem_remove(struct platform_device *pdev) > -- > 2.25.1 > >
在 2022/12/4 4:49, Ira Weiny 写道: > On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: >> We should always call dax_region_put() whenever devm_create_dev_dax() >> succeed or fail to avoid refcount leak of dax_region. Move the return >> value check after dax_region_put(). > I think dax_region_put is called from dax_region_unregister() automatically on > tear down. Yes, Thanks for your explanation. >> Fixes: c01044cc8191 ("ACPI: HMAT: refactor hmat_register_target_device to hmem_register_device") > I'm also not sure how this patch is related to this fix. > > Ira > >> Signed-off-by: Yongqiang Liu <liuyongqiang13@huawei.com> >> --- >> drivers/dax/hmem/hmem.c | 5 ++--- >> 1 file changed, 2 insertions(+), 3 deletions(-) >> >> diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c >> index 1bf040dbc834..09f5cd7b6c8e 100644 >> --- a/drivers/dax/hmem/hmem.c >> +++ b/drivers/dax/hmem/hmem.c >> @@ -36,12 +36,11 @@ static int dax_hmem_probe(struct platform_device *pdev) >> .size = region_idle ? 0 : resource_size(res), >> }; >> dev_dax = devm_create_dev_dax(&data); >> - if (IS_ERR(dev_dax)) >> - return PTR_ERR(dev_dax); >> >> /* child dev_dax instances now own the lifetime of the dax_region */ >> dax_region_put(dax_region); >> - return 0; >> + >> + return IS_ERR(dev_dax) ? PTR_ERR(dev_dax) : 0; >> } >> >> static int dax_hmem_remove(struct platform_device *pdev) >> -- >> 2.25.1 >> >> > .
On Sat, 3 Dec 2022, Ira Weiny wrote: > On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: > > We should always call dax_region_put() whenever devm_create_dev_dax() > > succeed or fail to avoid refcount leak of dax_region. Move the return > > value check after dax_region_put(). > I think dax_region_put is called from dax_region_unregister() automatically on > tear down. Hi Ira, Note the reference dax_region_unregister() will be putting is the one devm_create_dev_dax() takes by kref_get(&dax_region->kref). I think dax_hmem_probe() needs to put its reference in the error case, as in the successful case. Consider, devm_create_dev_dax() has error paths that return without involving dax_region_unregister(), prior to kref_get() and device_add(). dax_hmem_probe() is clearly responsible for freeing the region in those cases. dax_hmem_probe() drops its own reference in the successful case because (per the comment) "child dev_dax instances now own the lifetime of the dax_region". That ownership is the reference that the error-case dax_region_unregister() is dropping. dax_hmem_probe()'s initial reference also needs to be dropped in the error case, as in the successful case. > > Fixes: c01044cc8191 ("ACPI: HMAT: refactor hmat_register_target_device to hmem_register_device") > > I'm also not sure how this patch is related to this fix. > > Ira > > > Signed-off-by: Yongqiang Liu <liuyongqiang13@huawei.com> > > --- > > drivers/dax/hmem/hmem.c | 5 ++--- > > 1 file changed, 2 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c > > index 1bf040dbc834..09f5cd7b6c8e 100644 > > --- a/drivers/dax/hmem/hmem.c > > +++ b/drivers/dax/hmem/hmem.c > > @@ -36,12 +36,11 @@ static int dax_hmem_probe(struct platform_device *pdev) > > .size = region_idle ? 0 : resource_size(res), > > }; > > dev_dax = devm_create_dev_dax(&data); > > - if (IS_ERR(dev_dax)) > > - return PTR_ERR(dev_dax); > > > > /* child dev_dax instances now own the lifetime of the dax_region */ This comment should probably be updated now. :)
Paul Cassella wrote: > On Sat, 3 Dec 2022, Ira Weiny wrote: > > On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: > > > > We should always call dax_region_put() whenever devm_create_dev_dax() > > > succeed or fail to avoid refcount leak of dax_region. Move the return > > > value check after dax_region_put(). > > > I think dax_region_put is called from dax_region_unregister() automatically on > > tear down. > > Hi Ira, > > Note the reference dax_region_unregister() will be putting is the one > devm_create_dev_dax() takes by kref_get(&dax_region->kref). I think > dax_hmem_probe() needs to put its reference in the error case, as in the > successful case. Looking at this again I'm inclined to agree that something is wrong. But I'm not sure this patch fixes it. anything. When you say: > ... I think > dax_hmem_probe() needs to put its reference in the error case, as in the > successful case. ... which kref_get() is dax_hmem_probe() letting go? Here is what I see with the current code. dax_hmem_probe() alloc_dax_region() kref_get(&dax_region->kref) devm_add_action_or_reset(... dax_region_unregister, ...) ... kref_put() later... devm_create_dev_dax() ...may return error... kref_get() [dev_dax_release() set to call kref_put() later] ...may return error... if not error dax_region_put() => kref_put() I think this is an extra unneeded put??? Dan I see this pattern repeated in cxl and pmem. Is the intent to remove the need for dax_region_unregister() to be called when the platform device unwinds because the platform device is not intended to own the dax_region after success? If so it seems like the device managed call should be removed too. Not just calling dax_region_put()? :-/ But wouldn't that cause an issue with the sysfs entries created? > > Consider, devm_create_dev_dax() has error paths that return without > involving dax_region_unregister(), prior to kref_get() and device_add(). > dax_hmem_probe() is clearly responsible for freeing the region in those > cases. No the devm_add_action_or_reset(... dax_region_unregister, ...) will cause dax_region_unregister() to release the reference when the platform device unwinds. devm_create_dev_dax() configures a reference release through the dev_dax->type release. So when the dev_dax device is put the dax_region will be released through dev_dax_release()->dax_region_put(). > > > dax_hmem_probe() drops its own reference in the successful case because > (per the comment) "child dev_dax instances now own the lifetime of the > dax_region". That ownership is the reference that the error-case > dax_region_unregister() is dropping. No dax_region_unregister() is not just an error case flow. > > dax_hmem_probe()'s initial reference > also needs to be dropped in the error case, as in the successful case. > I don't follow this. Doesn't this now result in an invalid reference release in dax_region_unregister() when the platform device is unwound? Furthermore, that reference is required I think for the sysfs entries. > > > > Fixes: c01044cc8191 ("ACPI: HMAT: refactor hmat_register_target_device to hmem_register_device") > > > > I'm also not sure how this patch is related to this fix. > > > > Ira > > > > > Signed-off-by: Yongqiang Liu <liuyongqiang13@huawei.com> > > > --- > > > drivers/dax/hmem/hmem.c | 5 ++--- > > > 1 file changed, 2 insertions(+), 3 deletions(-) > > > > > > diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c > > > index 1bf040dbc834..09f5cd7b6c8e 100644 > > > --- a/drivers/dax/hmem/hmem.c > > > +++ b/drivers/dax/hmem/hmem.c > > > @@ -36,12 +36,11 @@ static int dax_hmem_probe(struct platform_device *pdev) > > > .size = region_idle ? 0 : resource_size(res), > > > }; > > > dev_dax = devm_create_dev_dax(&data); > > > - if (IS_ERR(dev_dax)) > > > - return PTR_ERR(dev_dax); > > > > > > /* child dev_dax instances now own the lifetime of the dax_region */ > > This comment should probably be updated now. :) > I think removed... Dan what do you think of this patch? Am I seriously off the rails here? I worry about this code being around for so long. But since it is in tear down perhaps it is just a race which has never been lost. Ira ---- 8< --------------------- From f63969c761b04fb5e646e7ba7df77a48bc26ba1c Mon Sep 17 00:00:00 2001 From: Ira Weiny <ira.weiny@intel.com> Date: Fri, 2 Jun 2023 11:17:10 -0700 Subject: [PATCH] dax: Avoid extra kref_put() of dax_regions In alloc_dax_region() sysfs attribute groups are created against the parent object the dax_region is being created under. A reference to the dax_region was thus obtained for the lifetime of the parent device via kobj_get() and released via dax_region_unregister(). The ownership of the dax_region was intended to be transferred to the device dax device but this transfer is not necessary and could be problematic if the sysfs entries are used after the dax device is unwound but before the parent device is. For the dax device the dax_region reference taken during creation in devm_create_dev_dax() is sufficient to ensure the dax_region lifetime for both the parent device and the dax device. Remove the extraneous dax_region_put() from all call paths with this pattern. Not-Yet-Signed-off-by: Ira Weiny <ira.weiny@intel.com> --- drivers/dax/cxl.c | 3 --- drivers/dax/hmem/hmem.c | 3 --- drivers/dax/pmem.c | 7 +------ 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/drivers/dax/cxl.c b/drivers/dax/cxl.c index ccdf8de85bd5..d3c3654842ba 100644 --- a/drivers/dax/cxl.c +++ b/drivers/dax/cxl.c @@ -31,9 +31,6 @@ static int cxl_dax_region_probe(struct device *dev) dev_dax = devm_create_dev_dax(&data); if (IS_ERR(dev_dax)) return PTR_ERR(dev_dax); - - /* child dev_dax instances now own the lifetime of the dax_region */ - dax_region_put(dax_region); return 0; } diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c index e5fe8b39fb94..d22d56964120 100644 --- a/drivers/dax/hmem/hmem.c +++ b/drivers/dax/hmem/hmem.c @@ -41,9 +41,6 @@ static int dax_hmem_probe(struct platform_device *pdev) dev_dax = devm_create_dev_dax(&data); if (IS_ERR(dev_dax)) return PTR_ERR(dev_dax); - - /* child dev_dax instances now own the lifetime of the dax_region */ - dax_region_put(dax_region); return 0; } diff --git a/drivers/dax/pmem.c b/drivers/dax/pmem.c index f050ea78bb83..efbdaac51e5f 100644 --- a/drivers/dax/pmem.c +++ b/drivers/dax/pmem.c @@ -65,12 +65,7 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev) .pgmap = &pgmap, .size = range_len(&range), }; - dev_dax = devm_create_dev_dax(&data); - - /* child dev_dax instances now own the lifetime of the dax_region */ - dax_region_put(dax_region); - - return dev_dax; + return devm_create_dev_dax(&data); } static int dax_pmem_probe(struct device *dev)
On Fri, 2 Jun 2023, Ira Weiny wrote: > Paul Cassella wrote: > > On Sat, 3 Dec 2022, Ira Weiny wrote: > > > On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: > > > > We should always call dax_region_put() whenever devm_create_dev_dax() > > > > succeed or fail to avoid refcount leak of dax_region. Move the return > > > > value check after dax_region_put(). > > > I think dax_region_put is called from dax_region_unregister() automatically on > > > tear down. > > Note the reference dax_region_unregister() will be putting is the one > > devm_create_dev_dax() takes by kref_get(&dax_region->kref). I think > > dax_hmem_probe() needs to put its reference in the error case, as in the > > successful case. > Looking at this again I'm inclined to agree that something is wrong. But > I'm not sure this patch fixes it. anything. > When you say: > > > ... I think > > dax_hmem_probe() needs to put its reference in the error case, as in the > > successful case. > > ... which kref_get() is dax_hmem_probe() letting go? Isn't it letting go of the initial kref_init() reference from alloc_dax_region()? Sorry, I had gone through the code a little carelessly yesterday. Now I think that kref_init() reference is the one that dax_hmem_probe() is dropping in the success case, and which still needs to be dropped in the error case. (If so, I think the alloc_dax_region() path that returns NULL on devm_add_action_or_reset() failure, releasing the kref_get reference, will leak the kref_init reference and the region.)
Paul Cassella wrote: > On Fri, 2 Jun 2023, Ira Weiny wrote: > > Paul Cassella wrote: > > > On Sat, 3 Dec 2022, Ira Weiny wrote: > > > > On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: > > > > > > We should always call dax_region_put() whenever devm_create_dev_dax() > > > > > succeed or fail to avoid refcount leak of dax_region. Move the return > > > > > value check after dax_region_put(). > > > > > I think dax_region_put is called from dax_region_unregister() automatically on > > > > tear down. > > > > Note the reference dax_region_unregister() will be putting is the one > > > devm_create_dev_dax() takes by kref_get(&dax_region->kref). I think > > > dax_hmem_probe() needs to put its reference in the error case, as in the > > > successful case. > > > Looking at this again I'm inclined to agree that something is wrong. But > > I'm not sure this patch fixes it. anything. > > > When you say: > > > > > ... I think > > > dax_hmem_probe() needs to put its reference in the error case, as in the > > > successful case. > > > > ... which kref_get() is dax_hmem_probe() letting go? > > Isn't it letting go of the initial kref_init() reference from > alloc_dax_region()? Oh wow! I did not realize that about kref_init()... :-( > > Sorry, I had gone through the code a little carelessly yesterday. Now I > think that kref_init() reference is the one that dax_hmem_probe() is > dropping in the success case, and which still needs to be dropped in the > error case. yea ok... > > (If so, I think the alloc_dax_region() path that returns NULL on > devm_add_action_or_reset() failure, releasing the kref_get reference, will > leak the kref_init reference and the region.) I see now. The ref is taken prior to the add action or reset to ensure the dax_region does not go away should the platform device go away... However, in all the call paths currently the device passed to alloc_dax_region() can't go away prior to the dev_dax taking a reference. So I don't think this extra reference is required. :-/ As you say the ref counting right now is not correct on error flows. But I'm torn on the correct solution. I think a small series broken out so it can be backported if needed would be best. Ira
Paul Cassella wrote: > On Fri, 2 Jun 2023, Ira Weiny wrote: > > Paul Cassella wrote: > > > On Sat, 3 Dec 2022, Ira Weiny wrote: > > > > On Sat, Dec 03, 2022 at 09:58:58AM +0000, Yongqiang Liu wrote: > > > > > > We should always call dax_region_put() whenever devm_create_dev_dax() > > > > > succeed or fail to avoid refcount leak of dax_region. Move the return > > > > > value check after dax_region_put(). > > > > > I think dax_region_put is called from dax_region_unregister() automatically on > > > > tear down. > > > > Note the reference dax_region_unregister() will be putting is the one > > > devm_create_dev_dax() takes by kref_get(&dax_region->kref). I think > > > dax_hmem_probe() needs to put its reference in the error case, as in the > > > successful case. > > > Looking at this again I'm inclined to agree that something is wrong. But > > I'm not sure this patch fixes it. anything. > > > When you say: > > > > > ... I think > > > dax_hmem_probe() needs to put its reference in the error case, as in the > > > successful case. > > > > ... which kref_get() is dax_hmem_probe() letting go? > > Isn't it letting go of the initial kref_init() reference from > alloc_dax_region()? > > Sorry, I had gone through the code a little carelessly yesterday. Now I > think that kref_init() reference is the one that dax_hmem_probe() is > dropping in the success case, and which still needs to be dropped in the > error case. > > (If so, I think the alloc_dax_region() path that returns NULL on > devm_add_action_or_reset() failure, releasing the kref_get reference, will > leak the kref_init reference and the region.) Yes, *this* one looks valid. All the other patches in this thread had me asking, "um, did you try it?". Now, that said, the games that this init path is playing are hard to follow and it should not be the case that alloc_dax_region() needs to hold a reference on behalf of some future code path that might need it. Lets make this clearer by moving the reference count management closer to the object that needs it dax_region->ida. Where the extra references were being taken on behalf of "static" regions just in case they needed to be reference in dev_dax_release(). I also found a dax_mapping lifetime issue while testing this, so I appreciate the focus here.
diff --git a/drivers/dax/hmem/hmem.c b/drivers/dax/hmem/hmem.c index 1bf040dbc834..09f5cd7b6c8e 100644 --- a/drivers/dax/hmem/hmem.c +++ b/drivers/dax/hmem/hmem.c @@ -36,12 +36,11 @@ static int dax_hmem_probe(struct platform_device *pdev) .size = region_idle ? 0 : resource_size(res), }; dev_dax = devm_create_dev_dax(&data); - if (IS_ERR(dev_dax)) - return PTR_ERR(dev_dax); /* child dev_dax instances now own the lifetime of the dax_region */ dax_region_put(dax_region); - return 0; + + return IS_ERR(dev_dax) ? PTR_ERR(dev_dax) : 0; } static int dax_hmem_remove(struct platform_device *pdev)