mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] siox: fix master memory leak on registration failure
@ 2026-09-19 18:10 Guangshuo Li
  2026-09-21 10:51 ` Markus Elfring
  0 siblings, 1 reply; 4+ messages in thread
From: Guangshuo Li @ 2026-09-19 18:10 UTC (permalink / raw)
  To: Thorsten Scherer, Pengutronix Kernel Team, Uwe Kleine-König,
	linux-kernel
  Cc: Guangshuo Li, stable

siox_master_register() takes an additional reference on the master
device with get_device() so that the SIOX core owns a reference until
siox_master_unregister() is called.

If kthread_run() fails, siox_master_register() returns without dropping
this reference. Similarly, if device_add() fails, the poll thread is
stopped but the reference acquired by siox_master_register() is not
released.

For devm-allocated masters, the devres cleanup only drops the original
allocation reference. The additional registration reference therefore
remains held, preventing siox_master_release() from being called and
leaking the siox_master allocation.

Drop the reference acquired by siox_master_register() on both failure
paths. On device_add() failure, kthread_stop() first lets the poll thread
drop its own reference before the registration reference is released.

The issue was identified by a static analysis tool I developed and
confirmed by manual review.

Fixes: 2c12932b8e65 ("siox: Don't pass the reference on a master in siox_master_register()")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/siox/siox-core.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/siox/siox-core.c b/drivers/siox/siox-core.c
index 3e8f3b6a4555..ea1ef0a5c991 100644
--- a/drivers/siox/siox-core.c
+++ b/drivers/siox/siox-core.c
@@ -753,14 +753,21 @@ int siox_master_register(struct siox_master *smaster)
 	smaster->poll_thread = kthread_run(siox_poll_thread, smaster,
 					   "siox-%d", smaster->busno);
 	if (IS_ERR(smaster->poll_thread)) {
+		ret = PTR_ERR(smaster->poll_thread);
 		smaster->active = 0;
-		return PTR_ERR(smaster->poll_thread);
+		goto err_put_device;
 	}
 
 	ret = device_add(&smaster->dev);
-	if (ret)
+	if (ret) {
 		kthread_stop(smaster->poll_thread);
+		goto err_put_device;
+	}
 
+	return 0;
+
+err_put_device:
+	put_device(&smaster->dev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(siox_master_register);
-- 
2.43.0


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

* Re: [PATCH] siox: fix master memory leak on registration failure
  2026-09-19 18:10 [PATCH] siox: fix master memory leak on registration failure Guangshuo Li
@ 2026-09-21 10:51 ` Markus Elfring
  2026-09-21 13:02   ` Uwe Kleine-König
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2026-09-21 10:51 UTC (permalink / raw)
  To: Guangshuo Li, kernel, Thorsten Scherer, Uwe Kleine-König
  Cc: stable, LKML

…
> +++ b/drivers/siox/siox-core.c
> @@ -753,14 +753,21 @@ int siox_master_register(struct siox_master *smaster)
>  	smaster->poll_thread = kthread_run(siox_poll_thread, smaster,
>  					   "siox-%d", smaster->busno);
>  	if (IS_ERR(smaster->poll_thread)) {
> +		ret = PTR_ERR(smaster->poll_thread);

Would it be nicer to use this variable assignment directly before
the goto statement?


>  		smaster->active = 0;
> -		return PTR_ERR(smaster->poll_thread);
> +		goto err_put_device;
>  	}
…


Regards,
Markus

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

* Re: [PATCH] siox: fix master memory leak on registration failure
  2026-09-21 10:51 ` Markus Elfring
@ 2026-09-21 13:02   ` Uwe Kleine-König
  2026-09-24 13:05     ` Thorsten Scherer
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2026-09-21 13:02 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Guangshuo Li, kernel, Thorsten Scherer, stable, LKML

[-- Attachment #1: Type: text/plain, Size: 1094 bytes --]

On Mon, Sep 21, 2026 at 12:51:00PM +0200, Markus Elfring wrote:
> …
> > +++ b/drivers/siox/siox-core.c
> > @@ -753,14 +753,21 @@ int siox_master_register(struct siox_master *smaster)
> >  	smaster->poll_thread = kthread_run(siox_poll_thread, smaster,
> >  					   "siox-%d", smaster->busno);
> >  	if (IS_ERR(smaster->poll_thread)) {
> > +		ret = PTR_ERR(smaster->poll_thread);
> 
> Would it be nicer to use this variable assignment directly before
> the goto statement?

No, please don't, IMHO it's fine to have IS_ERR and PTR_ERR together.

But I wonder if it's sensible to create a function to all the usual init
stuff such that the error handling in siox_master_register() can become
just:

	get_device(&smaster->dev);

	ret = siox_master_init(...);
	if (ret)
		put_device();

	return ret;

Having said that, I wonder about the smaster->active = 0 assignment.
It's quite some time ago that I wrote that code, but either it's
useless (that's where my bet is on), or this assignment is missing in
the error path of device_add()? Thorsten?

Best regards
Uwe

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] siox: fix master memory leak on registration failure
  2026-09-21 13:02   ` Uwe Kleine-König
@ 2026-09-24 13:05     ` Thorsten Scherer
  0 siblings, 0 replies; 4+ messages in thread
From: Thorsten Scherer @ 2026-09-24 13:05 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: Markus Elfring, Guangshuo Li, kernel, stable, LKML

Hello everyone,

thank you for the patch and the input.

On Mon, Sep 21, 2026 at 03:02:01PM +0200, Uwe Kleine-König wrote:
> On Mon, Sep 21, 2026 at 12:51:00PM +0200, Markus Elfring wrote:
> > …
> > > +++ b/drivers/siox/siox-core.c
> > > @@ -753,14 +753,21 @@ int siox_master_register(struct siox_master *smaster)
> > >  	smaster->poll_thread = kthread_run(siox_poll_thread, smaster,
> > >  					   "siox-%d", smaster->busno);
> > >  	if (IS_ERR(smaster->poll_thread)) {
> > > +		ret = PTR_ERR(smaster->poll_thread);
> > 
> > Would it be nicer to use this variable assignment directly before
> > the goto statement?
> 
> No, please don't, IMHO it's fine to have IS_ERR and PTR_ERR together.
> 
> But I wonder if it's sensible to create a function to all the usual init
> stuff such that the error handling in siox_master_register() can become
> just:
> 
> 	get_device(&smaster->dev);
> 
> 	ret = siox_master_init(...);
> 	if (ret)
> 		put_device();
> 
> 	return ret;
> 
> Having said that, I wonder about the smaster->active = 0 assignment.
> It's quite some time ago that I wrote that code, but either it's
> useless (that's where my bet is on), or this assignment is missing in
> the error path of device_add()? Thorsten?

At a first glance I'm with you.  Assignment doesn't change anything.
But I don't have a clear view (on the whole context) yet.  The patch
seems legit and fixes a real issue.  Having said that,  it seems that
there is an additional imbalance at siox_poll_thread.

I will have to take a little time to swap siox in and get a clearer picture.

Coming back to you soon.

> Best regards
> Uwe

Best regards
Thorsten

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

end of thread, other threads:[~2026-09-24 13:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-19 18:10 [PATCH] siox: fix master memory leak on registration failure Guangshuo Li
2026-09-21 10:51 ` Markus Elfring
2026-09-21 13:02   ` Uwe Kleine-König
2026-09-24 13:05     ` Thorsten Scherer

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®