dax/hmem: Fix refcount leak in dax_hmem_probe()
PATCH 1/1 dax/hmem: Fix refcount leak in dax_hmem_probe()
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().
--- drivers/dax/hmem/hmem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/dax/hmem/hmem.c +2 -3
@@ -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)
Signature · 1 line
2.25.1
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().
I think dax_region_put is called from dax_region_unregister() automatically on tear down.
Quoted 2 lines
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
Quoted 28 lines
Signed-off-by Yongqiang Liu <[email protected]>--- drivers/dax/hmem/hmem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/dax/hmem/hmem.c +2
-3@@ -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)Signature · 1 line
2.25.1
Quoted 6 lines · Ira Weiny
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().I think
dax_region_putis called fromdax_region_unregister()automatically on tear down.
Yes, Thanks for your explanation.
Quoted 34 lines
Quoted 1 line
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
Quoted 28 lines
Signed-off-by Yongqiang Liu <[email protected]>--- drivers/dax/hmem/hmem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/dax/hmem/hmem.c +2
-3@@ -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)Signature · 1 line
2.25.1.
Quoted 8 lines · Ira Weiny
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().I think
dax_region_putis called fromdax_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.
Quoted 22 lines
Quoted 1 line
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
Quoted 16 lines
Signed-off-by Yongqiang Liu <[email protected]>--- drivers/dax/hmem/hmem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/dax/hmem/hmem.c +0
-2@@ -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);
Quoted 1 line
drivers/dax/hmem/hmem.c static int dax_hmem_probe(struct platform_device *pdev) · line 40
Quoted 1 line
drivers/dax/hmem/hmem.c +0
-0/* child dev_dax instances now own the lifetime of the dax_region */
This comment should probably be updated now. :)
Signature · 1 line
Paul Cassella
Quoted 12 lines
Quoted 11 lines
drivers/dax/hmem/hmem.c +2
-1dax_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)Signature · 1 line
2.25.1
Quoted 10 lines · Paul Cassella
Quoted 8 lines · Ira Weiny
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().I think
dax_region_putis called fromdax_region_unregister()automatically on tear down.
Quoted 6 lines
Hi Ira,
Note the reference
dax_region_unregister()will be putting is the onedevm_create_dev_dax()takes bykref_get(&dax_region->kref). I thinkdax_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:
Quoted 3 lines
... 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?
Quoted 5 lines
Consider,
devm_create_dev_dax()has error paths that return without involvingdax_region_unregister(), prior tokref_get()anddevice_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().
Quoted 6 lines
dax_hmem_probe()drops its own reference in the successful case because (per the comment) "childdev_daxinstances now own the lifetime of thedax_region". That ownership is the reference that the error-casedax_region_unregister()is dropping.
No dax_region_unregister() is not just an error case flow.
Quoted 4 lines
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.
Quoted 23 lines
Quoted 23 lines
Quoted 2 lines
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
Quoted 16 lines
Signed-off-by Yongqiang Liu <[email protected]>--- drivers/dax/hmem/hmem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/dax/hmem/hmem.c +0
-2@@ -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);
Quoted 4 lines
Quoted 1 line
drivers/dax/hmem/hmem.c static int dax_hmem_probe(struct platform_device *pdev) · line 40
Quoted 1 line
drivers/dax/hmem/hmem.c +0
-0/* 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 <[email protected]>
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.
--- drivers/dax/cxl.c | 3 --- drivers/dax/hmem/hmem.c | 3 --- drivers/dax/pmem.c | 7 +------ 3 files changed, 1 insertion(+), 12 deletions(-)
drivers/dax/cxl.c +0 -3
@@ -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 ; }
drivers/dax/hmem/hmem.c +0 -3
@@ -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 ; }
drivers/dax/pmem.c +1 -6
@@ -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)
Signature · 1 line
2.40.0
Quoted 19 lines · Ira Weiny
Quoted 7 lines · Paul Cassella
Quoted 5 lines · Ira Weiny
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().Quoted 7 lines
Quoted 2 lines
I think
dax_region_putis called fromdax_region_unregister()automatically on tear down.Note the reference
dax_region_unregister()will be putting is the onedevm_create_dev_dax()takes bykref_get(&dax_region->kref). I thinkdax_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.
Quoted 7 lines
When you say:
Quoted 3 lines
... I think
dax_hmem_probe()needs to put its reference in the error case, as in the successful case.... which
kref_get()isdax_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.)
Signature · 1 line
Paul Cassella
Quoted 176 lines
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 thedax_regionafter success? If so it seems like the device managed call should be removed too. Not just callingdax_region_put()? :-/But wouldn't that cause an issue with the sysfs entries created?
Quoted 5 lines
Consider,
devm_create_dev_dax()has error paths that return without involvingdax_region_unregister(), prior tokref_get()anddevice_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 causedax_region_unregister()to release the reference when the platform device unwinds.
devm_create_dev_dax()configures a reference release through thedev_dax->typerelease. So when thedev_daxdevice is put thedax_regionwill be released through dev_dax_release()->dax_region_put().Quoted 6 lines
dax_hmem_probe()drops its own reference in the successful case because (per the comment) "childdev_daxinstances now own the lifetime of thedax_region". That ownership is the reference that the error-casedax_region_unregister()is dropping.No
dax_region_unregister()is not just an error case flow.Quoted 4 lines
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.Quoted 23 lines
Quoted 23 lines
Quoted 2 lines
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
Quoted 16 lines
Signed-off-by Yongqiang Liu <[email protected]>--- drivers/dax/hmem/hmem.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/dax/hmem/hmem.c +0
-2@@ -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);Quoted 4 lines
Quoted 1 line
drivers/dax/hmem/hmem.c static int dax_hmem_probe(struct platform_device *pdev) · line 40
Quoted 1 line
drivers/dax/hmem/hmem.c +0
-0/* 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
f63969c761b04fb5e646e7ba7df77a48bc26ba1cMon Sep 17 00:00:00 2001
From: Ira Weiny <[email protected]>
Date: Fri, 2 Jun 2023 11:17:10 -0700
Subject: [PATCH] dax: Avoid extrakref_put()ofdax_regionsIn
alloc_dax_region()sysfs attribute groups are created against the parent object thedax_regionis being created under. A reference to thedax_regionwas thus obtained for the lifetime of the parent device viakobj_get()and released viadax_region_unregister().The ownership of the
dax_regionwas 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_regionreference taken during creation indevm_create_dev_dax()is sufficient to ensure thedax_regionlifetime 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 <[email protected]>--- drivers/dax/cxl.c | 3 --- drivers/dax/hmem/hmem.c | 3 --- drivers/dax/pmem.c | 7 +------ 3 files changed, 1 insertion(+), 12 deletions(-)drivers/dax/cxl.c +0
-3@@ -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 ; }drivers/dax/hmem/hmem.c +0
-3@@ -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 ; }drivers/dax/pmem.c +1
-6@@ -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)Signature · 1 line
2.40.0
Quoted 22 lines · Paul Cassella
Quoted 21 lines · Ira Weiny
Quoted 7 lines · Paul Cassella
Quoted 5 lines · Ira Weiny
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().Quoted 7 lines
Quoted 2 lines
I think
dax_region_putis called fromdax_region_unregister()automatically on tear down.Note the reference
dax_region_unregister()will be putting is the onedevm_create_dev_dax()takes bykref_get(&dax_region->kref). I thinkdax_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:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
... I think
dax_hmem_probe()needs to put its reference in the error case, as in the successful case.... which
kref_get()isdax_hmem_probe()letting go?Isn't it letting go of the initial
kref_init()reference fromalloc_dax_region()?
Oh wow! I did not realize that about kref_init()... :-(
Quoted 5 lines
Sorry, I had gone through the code a little carelessly yesterday. Now I think that
kref_init()reference is the one thatdax_hmem_probe()is dropping in the success case, and which still needs to be dropped in the error case.
yea ok...
Quoted 4 lines
(If so, I think the
alloc_dax_region()path that returns NULL ondevm_add_action_or_reset()failure, releasing thekref_getreference, will leak thekref_initreference 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
Quoted 31 lines · Paul Cassella
Quoted 19 lines · Ira Weiny
Quoted 7 lines · Paul Cassella
Quoted 5 lines · Ira Weiny
Quoted 3 lines · Yongqiang Liu
We should always call
dax_region_put()wheneverdevm_create_dev_dax()succeed or fail to avoid refcount leak ofdax_region. Move the return value check afterdax_region_put().Quoted 7 lines
Quoted 2 lines
I think
dax_region_putis called fromdax_region_unregister()automatically on tear down.Note the reference
dax_region_unregister()will be putting is the onedevm_create_dev_dax()takes bykref_get(&dax_region->kref). I thinkdax_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.
Quoted 7 lines
When you say:
Quoted 3 lines
... I think
dax_hmem_probe()needs to put its reference in the error case, as in the successful case.... which
kref_get()isdax_hmem_probe()letting go?Isn't it letting go of the initial
kref_init()reference fromalloc_dax_region()?
Quoted 8 lines
Sorry, I had gone through the code a little carelessly yesterday. Now I think that
kref_init()reference is the one thatdax_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 ondevm_add_action_or_reset()failure, releasing thekref_getreference, will leak thekref_initreference 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.