Re: [PATCH v2 2/2] cxl/region: Use __free(put_device) in find_pos_and_ways()

Jonathan Cameron <[email protected]>
Newsgroups org.kernel.vger.linux-cxl
Message-ID <20260722005648.360ddf68@jic23-huawei>
On Tue, 21 Jul 2026 10:37:04 -0700
Alison Schofield <[email protected]> wrote:

> Use __free(put_device) for the switch decoder reference returned by
> device_find_child() instead of releasing it with an open-coded
> put_device().
> 
> This matches the scoped device reference handling used elsewhere in
> the file.
> 
> Suggested-by: Li Ming <[email protected]>
> Signed-off-by: Alison Schofield <[email protected]>
> ---
> 
> This is a new patch added in series after the UAF fixup in patch 1/2.
> 
> 
>  drivers/cxl/core/region.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index c86dd9d55e93..4d7d96d03ece 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -1911,9 +1911,9 @@ static int match_switch_decoder_by_range(struct device *dev,
>  static int find_pos_and_ways(struct cxl_port *port, struct range *range,
>  			     int *pos, int *ways)
>  {
> +	struct device *dev __free(put_device) = NULL;

No to this.  See rules in cleanup.h.
This is the path to grumpy Linus if he happens to spot it.

Short story is declaration and destructor must be in same statement
as the constructor.  Here that means 

	struct device *dev __free(put_device) =
		device_find_child(&parent->dev, range, match_switch_decoder_by_range);

I would go long on that second line given it's only a bit beyond 80 chars
and the two lines are horrible enough to read without becoming 3.



>  	struct cxl_switch_decoder *cxlsd;
>  	struct cxl_port *parent;
> -	struct device *dev;
>  	int rc = -ENXIO;
>  
>  	parent = parent_port_of(port);
> @@ -1945,8 +1945,6 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
>  			dev_name(port->parent_dport->dport_dev),
>  			dev_name(&cxlsd->cxld.dev));
>  
> -	put_device(dev);
> -
>  	return rc;
>  }
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.