mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cxl/region: Balance device refcount when destroying devices
@ 2025-07-14  9:16 Ma Ke
  2025-07-14  9:29 ` Li Ming
  0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2025-07-14  9:16 UTC (permalink / raw)
  To: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, rrichter, ming.li,
	zijun.hu, fabio.m.de.francesco
  Cc: linux-cxl, linux-kernel, Ma Ke, stable

Using device_find_child() to lookup the proper device to destroy
causes an unbalance in device refcount, since device_find_child()
calls an implicit get_device() to increment the device's reference
count before returning the pointer. cxl_port_find_switch_decoder()
directly converts this pointer to a cxl_decoder and returns it without
releasing the reference properly. We should call put_device() to
decrement reference count.

As comment of device_find_child() says, 'NOTE: you will need to drop
the reference with put_device() after use'.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: d6879d8cfb81 ("cxl/region: Add function to find a port's switch decoder by range")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 drivers/cxl/core/region.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 6e5e1460068d..cae16d761261 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3234,6 +3234,9 @@ cxl_port_find_switch_decoder(struct cxl_port *port, struct range *hpa)
 	struct device *cxld_dev = device_find_child(&port->dev, hpa,
 						    match_decoder_by_range);
 
+	/* Drop the refcnt bumped implicitly by device_find_child */
+	put_device(cxld_dev);
+
 	return cxld_dev ? to_cxl_decoder(cxld_dev) : NULL;
 }
 
-- 
2.25.1


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

* Re: [PATCH] cxl/region: Balance device refcount when destroying devices
  2025-07-14  9:16 [PATCH] cxl/region: Balance device refcount when destroying devices Ma Ke
@ 2025-07-14  9:29 ` Li Ming
  0 siblings, 0 replies; 2+ messages in thread
From: Li Ming @ 2025-07-14  9:29 UTC (permalink / raw)
  To: Ma Ke
  Cc: linux-cxl, linux-kernel, stable, dave, jonathan.cameron,
	dave.jiang, alison.schofield, vishal.l.verma, ira.weiny,
	dan.j.williams, rrichter, zijun.hu, fabio.m.de.francesco

On 7/14/2025 5:16 PM, Ma Ke wrote:
> Using device_find_child() to lookup the proper device to destroy
> causes an unbalance in device refcount, since device_find_child()
> calls an implicit get_device() to increment the device's reference
> count before returning the pointer. cxl_port_find_switch_decoder()
> directly converts this pointer to a cxl_decoder and returns it without
> releasing the reference properly. We should call put_device() to
> decrement reference count.
>
> As comment of device_find_child() says, 'NOTE: you will need to drop
> the reference with put_device() after use'.
>
> Found by code review.
>
> Cc: stable@vger.kernel.org
> Fixes: d6879d8cfb81 ("cxl/region: Add function to find a port's switch decoder by range")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>  drivers/cxl/core/region.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 6e5e1460068d..cae16d761261 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -3234,6 +3234,9 @@ cxl_port_find_switch_decoder(struct cxl_port *port, struct range *hpa)
>  	struct device *cxld_dev = device_find_child(&port->dev, hpa,
>  						    match_decoder_by_range);
>  
> +	/* Drop the refcnt bumped implicitly by device_find_child */
> +	put_device(cxld_dev);
> +
>  	return cxld_dev ? to_cxl_decoder(cxld_dev) : NULL;
>  }
>  

It is not needed, because cxl_port_find_switch_decoder() is only invoked in cxl_find_root_decoder(), and cxl_find_root_decoder() is only called in cxl_add_to_region(), there is a __free(put_cxl_root_decoder) in cxl_add_to_region() for the device dereference.


Ming


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

end of thread, other threads:[~2025-07-14  9:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-14  9:16 [PATCH] cxl/region: Balance device refcount when destroying devices Ma Ke
2025-07-14  9:29 ` Li Ming

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

all inboxes | Powered by JetHome®