public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] device-dax: Fix refcount leak in __devm_create_dev_dax() error path
@ 2026-04-11 14:57 Guangshuo Li
  2026-04-11 23:29 ` Dan Williams
  0 siblings, 1 reply; 3+ messages in thread
From: Guangshuo Li @ 2026-04-11 14:57 UTC (permalink / raw)
  To: Dan Williams, Vishal Verma, Dave Jiang, Andrew Morton, nvdimm,
	linux-cxl, linux-kernel
  Cc: Guangshuo Li, stable

After device_initialize(), the lifetime of the embedded struct device is
expected to be managed through the device core reference counting.

In __devm_create_dev_dax(), several failure paths after
device_initialize() free dev_dax directly instead of releasing the
device reference with put_device(). This bypasses the normal device
lifetime rules and may leave the reference count of the embedded struct
device unbalanced, resulting in a refcount leak and potentially leading
to a use-after-free.

Fix this by assigning dev->type before device_initialize(), so the
release callback is available for put_device(), and use put_device() in
the post-initialization error paths. Keep dev_dax range cleanup explicit
in the error path.

Fixes: c2f3011ee697f ("device-dax: add an allocation interface for device-dax instances")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/dax/bus.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
index fde29e0ad68b..8753115cd371 100644
--- a/drivers/dax/bus.c
+++ b/drivers/dax/bus.c
@@ -1453,6 +1453,7 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
 	}
 
 	dev = &dev_dax->dev;
+	dev->type = &dev_dax_type;
 	device_initialize(dev);
 	dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
 
@@ -1499,7 +1500,6 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
 	dev->devt = inode->i_rdev;
 	dev->bus = &dax_bus_type;
 	dev->parent = parent;
-	dev->type = &dev_dax_type;
 
 	rc = device_add(dev);
 	if (rc) {
@@ -1523,14 +1523,21 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
 
 err_alloc_dax:
 	kfree(dev_dax->pgmap);
+	dev_dax->pgmap = NULL;
+
 err_pgmap:
 	free_dev_dax_ranges(dev_dax);
+	put_device(dev);
+	return ERR_PTR(rc);
+
 err_range:
-	free_dev_dax_id(dev_dax);
+	put_device(dev);
+	return ERR_PTR(rc);
+
 err_id:
 	kfree(dev_dax);
-
 	return ERR_PTR(rc);
+
 }
 
 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] device-dax: Fix refcount leak in __devm_create_dev_dax() error path
  2026-04-11 14:57 [PATCH] device-dax: Fix refcount leak in __devm_create_dev_dax() error path Guangshuo Li
@ 2026-04-11 23:29 ` Dan Williams
  2026-04-12  6:40   ` Guangshuo Li
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Williams @ 2026-04-11 23:29 UTC (permalink / raw)
  To: Guangshuo Li, Vishal Verma, Dave Jiang, Andrew Morton, nvdimm,
	linux-cxl, linux-kernel
  Cc: Guangshuo Li, stable

Guangshuo Li wrote:
> After device_initialize(), the lifetime of the embedded struct device is
> expected to be managed through the device core reference counting.
> 
> In __devm_create_dev_dax(), several failure paths after
> device_initialize() free dev_dax directly instead of releasing the
> device reference with put_device(). This bypasses the normal device
> lifetime rules and may leave the reference count of the embedded struct
> device unbalanced, resulting in a refcount leak and potentially leading
> to a use-after-free.

Please do not list "theoretical" problems as justification. Point to
real problems.

> Fix this by assigning dev->type before device_initialize(), so the
> release callback is available for put_device(), and use put_device() in
> the post-initialization error paths. Keep dev_dax range cleanup explicit
> in the error path.

I see a more straightforward way to address just the practical problem
that also incorporates the other feedback I have below. Can you spot
that and fixup the changelog to address the practical impact?

> Fixes: c2f3011ee697f ("device-dax: add an allocation interface for device-dax instances")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  drivers/dax/bus.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> index fde29e0ad68b..8753115cd371 100644
> --- a/drivers/dax/bus.c
> +++ b/drivers/dax/bus.c
> @@ -1453,6 +1453,7 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
>  	}
>  
>  	dev = &dev_dax->dev;
> +	dev->type = &dev_dax_type;
>  	device_initialize(dev);
>  	dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
>  
> @@ -1499,7 +1500,6 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
>  	dev->devt = inode->i_rdev;
>  	dev->bus = &dax_bus_type;
>  	dev->parent = parent;
> -	dev->type = &dev_dax_type;
>  
>  	rc = device_add(dev);
>  	if (rc) {
> @@ -1523,14 +1523,21 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
>  
>  err_alloc_dax:
>  	kfree(dev_dax->pgmap);
> +	dev_dax->pgmap = NULL;
> +
>  err_pgmap:
>  	free_dev_dax_ranges(dev_dax);
> +	put_device(dev);
> +	return ERR_PTR(rc);
> +
>  err_range:
> -	free_dev_dax_id(dev_dax);
> +	put_device(dev);
> +	return ERR_PTR(rc);

Please no gotos with early returns, that makes a mess.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] device-dax: Fix refcount leak in __devm_create_dev_dax() error path
  2026-04-11 23:29 ` Dan Williams
@ 2026-04-12  6:40   ` Guangshuo Li
  0 siblings, 0 replies; 3+ messages in thread
From: Guangshuo Li @ 2026-04-12  6:40 UTC (permalink / raw)
  To: Dan Williams
  Cc: Vishal Verma, Dave Jiang, Andrew Morton, nvdimm, linux-cxl,
	linux-kernel, stable

Hi Dan,

Thank you for the review and for pointing that out.

You are right that my changelog overstated the impact. I do not have a
concrete use-after-free case here, and the practical issue is simply
that after device_initialize(), the embedded struct device should be
released through the device core with put_device(), rather than
freeing dev_dax directly.

I also took a closer look at the release path. Since dev_dax_release()
already handles free_dev_dax_id(), kfree(dev_dax->pgmap), and
kfree(dev_dax), and put_dax() is NULL-safe, the post-initialization
failure paths can be simplified to explicit range cleanup plus
put_device(), once dev->type is assigned before device_initialize().

I'll send a v2 that tightens the changelog around the actual lifecycle
issue and cleans up the error paths accordingly.

Thanks again for the guidance.

Best regards,
Guangshuo

Dan Williams <djbw@kernel.org> 于2026年4月12日周日 07:29写道:
>
> Guangshuo Li wrote:
> > After device_initialize(), the lifetime of the embedded struct device is
> > expected to be managed through the device core reference counting.
> >
> > In __devm_create_dev_dax(), several failure paths after
> > device_initialize() free dev_dax directly instead of releasing the
> > device reference with put_device(). This bypasses the normal device
> > lifetime rules and may leave the reference count of the embedded struct
> > device unbalanced, resulting in a refcount leak and potentially leading
> > to a use-after-free.
>
> Please do not list "theoretical" problems as justification. Point to
> real problems.
>
> > Fix this by assigning dev->type before device_initialize(), so the
> > release callback is available for put_device(), and use put_device() in
> > the post-initialization error paths. Keep dev_dax range cleanup explicit
> > in the error path.
>
> I see a more straightforward way to address just the practical problem
> that also incorporates the other feedback I have below. Can you spot
> that and fixup the changelog to address the practical impact?
>
> > Fixes: c2f3011ee697f ("device-dax: add an allocation interface for device-dax instances")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> > ---
> >  drivers/dax/bus.c | 13 ++++++++++---
> >  1 file changed, 10 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> > index fde29e0ad68b..8753115cd371 100644
> > --- a/drivers/dax/bus.c
> > +++ b/drivers/dax/bus.c
> > @@ -1453,6 +1453,7 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
> >       }
> >
> >       dev = &dev_dax->dev;
> > +     dev->type = &dev_dax_type;
> >       device_initialize(dev);
> >       dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
> >
> > @@ -1499,7 +1500,6 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
> >       dev->devt = inode->i_rdev;
> >       dev->bus = &dax_bus_type;
> >       dev->parent = parent;
> > -     dev->type = &dev_dax_type;
> >
> >       rc = device_add(dev);
> >       if (rc) {
> > @@ -1523,14 +1523,21 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
> >
> >  err_alloc_dax:
> >       kfree(dev_dax->pgmap);
> > +     dev_dax->pgmap = NULL;
> > +
> >  err_pgmap:
> >       free_dev_dax_ranges(dev_dax);
> > +     put_device(dev);
> > +     return ERR_PTR(rc);
> > +
> >  err_range:
> > -     free_dev_dax_id(dev_dax);
> > +     put_device(dev);
> > +     return ERR_PTR(rc);
>
> Please no gotos with early returns, that makes a mess.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-12  6:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-11 14:57 [PATCH] device-dax: Fix refcount leak in __devm_create_dev_dax() error path Guangshuo Li
2026-04-11 23:29 ` Dan Williams
2026-04-12  6:40   ` Guangshuo Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox