[PATCH v3 3/3] cxl/core: use cleanup.h for devm_cxl_add_dax_region

Gregory Price posted 3 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v3 3/3] cxl/core: use cleanup.h for devm_cxl_add_dax_region
Posted by Gregory Price 1 month, 2 weeks ago
Cleanup the gotos in the function.

No functional change intended.

Signed-off-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region_dax.c | 21 ++++++++-------------
 drivers/cxl/cxl.h             |  1 +
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/drivers/cxl/core/region_dax.c b/drivers/cxl/core/region_dax.c
index c8dd2bd1d9b9..49907c6c7620 100644
--- a/drivers/cxl/core/region_dax.c
+++ b/drivers/cxl/core/region_dax.c
@@ -81,29 +81,24 @@ static void cxlr_dax_unregister(void *_cxlr_dax)
 
 int devm_cxl_add_dax_region(struct cxl_region *cxlr)
 {
-	struct cxl_dax_region *cxlr_dax;
-	struct device *dev;
+	struct cxl_dax_region *cxlr_dax __free(put_cxl_dax_region) = NULL;
 	int rc;
 
 	cxlr_dax = cxl_dax_region_alloc(cxlr);
 	if (IS_ERR(cxlr_dax))
 		return PTR_ERR(cxlr_dax);
 
-	dev = &cxlr_dax->dev;
-	rc = dev_set_name(dev, "dax_region%d", cxlr->id);
+	rc = dev_set_name(&cxlr_dax->dev, "dax_region%d", cxlr->id);
 	if (rc)
-		goto err;
+		return rc;
 
-	rc = device_add(dev);
+	rc = device_add(&cxlr_dax->dev);
 	if (rc)
-		goto err;
+		return rc;
 
-	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
-		dev_name(dev));
+	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(cxlr_dax->dev.parent),
+		dev_name(&cxlr_dax->dev));
 
 	return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
-					cxlr_dax);
-err:
-	put_device(dev);
-	return rc;
+					no_free_ptr(cxlr_dax));
 }
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 04c673e7cdb0..0b59008ea45a 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -803,6 +803,7 @@ DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_device(&_T->port.dev))
 DEFINE_FREE(put_cxl_port, struct cxl_port *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
 DEFINE_FREE(put_cxl_root_decoder, struct cxl_root_decoder *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev))
 DEFINE_FREE(put_cxl_region, struct cxl_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
+DEFINE_FREE(put_cxl_dax_region, struct cxl_dax_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
 
 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
 void cxl_bus_rescan(void);
-- 
2.47.3
Re: [PATCH v3 3/3] cxl/core: use cleanup.h for devm_cxl_add_dax_region
Posted by Dave Jiang 1 month, 1 week ago

On 2/11/26 1:42 PM, Gregory Price wrote:
> Cleanup the gotos in the function.
> 
> No functional change intended.
> 
> Signed-off-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/region_dax.c | 21 ++++++++-------------
>  drivers/cxl/cxl.h             |  1 +
>  2 files changed, 9 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/cxl/core/region_dax.c b/drivers/cxl/core/region_dax.c
> index c8dd2bd1d9b9..49907c6c7620 100644
> --- a/drivers/cxl/core/region_dax.c
> +++ b/drivers/cxl/core/region_dax.c
> @@ -81,29 +81,24 @@ static void cxlr_dax_unregister(void *_cxlr_dax)
>  
>  int devm_cxl_add_dax_region(struct cxl_region *cxlr)
>  {
> -	struct cxl_dax_region *cxlr_dax;
> -	struct device *dev;
> +	struct cxl_dax_region *cxlr_dax __free(put_cxl_dax_region) = NULL;
>  	int rc;
>  
>  	cxlr_dax = cxl_dax_region_alloc(cxlr);

The typical __cleanup() pattern is to move the variable declaration here in order to minimize unintended issues between declare and check.

>  	if (IS_ERR(cxlr_dax))
>  		return PTR_ERR(cxlr_dax);
>  
> -	dev = &cxlr_dax->dev;

Given that this local var is used multiple times, maybe we should keep it?

DJ

> -	rc = dev_set_name(dev, "dax_region%d", cxlr->id);
> +	rc = dev_set_name(&cxlr_dax->dev, "dax_region%d", cxlr->id);
>  	if (rc)
> -		goto err;
> +		return rc;
>  
> -	rc = device_add(dev);
> +	rc = device_add(&cxlr_dax->dev);
>  	if (rc)
> -		goto err;
> +		return rc;
>  
> -	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
> -		dev_name(dev));
> +	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(cxlr_dax->dev.parent),
> +		dev_name(&cxlr_dax->dev));
>  
>  	return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
> -					cxlr_dax);
> -err:
> -	put_device(dev);
> -	return rc;
> +					no_free_ptr(cxlr_dax));
>  }
> diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
> index 04c673e7cdb0..0b59008ea45a 100644
> --- a/drivers/cxl/cxl.h
> +++ b/drivers/cxl/cxl.h
> @@ -803,6 +803,7 @@ DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_device(&_T->port.dev))
>  DEFINE_FREE(put_cxl_port, struct cxl_port *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
>  DEFINE_FREE(put_cxl_root_decoder, struct cxl_root_decoder *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev))
>  DEFINE_FREE(put_cxl_region, struct cxl_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
> +DEFINE_FREE(put_cxl_dax_region, struct cxl_dax_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
>  
>  int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
>  void cxl_bus_rescan(void);
Re: [PATCH v3 3/3] cxl/core: use cleanup.h for devm_cxl_add_dax_region
Posted by Gregory Price 1 month, 1 week ago
On Tue, Feb 17, 2026 at 04:55:13PM -0700, Dave Jiang wrote:
> 
> 
> On 2/11/26 1:42 PM, Gregory Price wrote:
> > Cleanup the gotos in the function.
> > 
> > No functional change intended.
> > 
> > Signed-off-by: Gregory Price <gourry@gourry.net>
> > ---
> >  drivers/cxl/core/region_dax.c | 21 ++++++++-------------
> >  drivers/cxl/cxl.h             |  1 +
> >  2 files changed, 9 insertions(+), 13 deletions(-)
> > 
> > diff --git a/drivers/cxl/core/region_dax.c b/drivers/cxl/core/region_dax.c
> > index c8dd2bd1d9b9..49907c6c7620 100644
> > --- a/drivers/cxl/core/region_dax.c
> > +++ b/drivers/cxl/core/region_dax.c
> > @@ -81,29 +81,24 @@ static void cxlr_dax_unregister(void *_cxlr_dax)
> >  
> >  int devm_cxl_add_dax_region(struct cxl_region *cxlr)
> >  {
> > -	struct cxl_dax_region *cxlr_dax;
> > -	struct device *dev;
> > +	struct cxl_dax_region *cxlr_dax __free(put_cxl_dax_region) = NULL;
> >  	int rc;
> >  
> >  	cxlr_dax = cxl_dax_region_alloc(cxlr);
> 
> The typical __cleanup() pattern is to move the variable declaration here in order to minimize unintended issues between declare and check.
>

Ah, relatively new pattern for me, i'll clean it up

> >  	if (IS_ERR(cxlr_dax))
> >  		return PTR_ERR(cxlr_dax);
> >  
> > -	dev = &cxlr_dax->dev;
> 
> Given that this local var is used multiple times, maybe we should keep it?
> 

Fair, will re-spin claned up

~Gregory