public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] iio: trigger: Fix error handling in viio_trigger_alloc
@ 2025-11-07  2:02 Ma Ke
  2025-11-07  8:03 ` Andy Shevchenko
  2025-11-07 10:26 ` Nuno Sá
  0 siblings, 2 replies; 8+ messages in thread
From: Ma Ke @ 2025-11-07  2:02 UTC (permalink / raw)
  To: jic23, dlechner, nuno.sa, andy; +Cc: linux-iio, linux-kernel, akpm, Ma Ke

viio_trigger_alloc() initializes the device with device_initialize()
but uses kfree() directly in error paths, which bypasses the device's
release callback iio_trig_release(). This could lead to memory leaks
and inconsistent device state.

Additionally, the current error handling has the following issues:
1. Potential double-free of IRQ descriptors when kvasprintf() fails.
2. The release function may attempt to free negative subirq_base.
3. Missing mutex_destroy() in release function.

Fix these issues by:
1. Replacing kfree(trig) with put_device(&trig->dev) in error paths.
2. Removing the free_descs label and handling IRQ descriptor freeing
   directly in the kvasprintf() error path.
3. Adding missing mutex_destroy().

Found by code review.

Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 drivers/iio/industrialio-trigger.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 54416a384232..7576dedee68e 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -524,6 +524,7 @@ static void iio_trig_release(struct device *device)
 			       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
 	}
 	kfree(trig->name);
+	mutex_destroy(&trig->pool_lock);
 	kfree(trig);
 }
 
@@ -575,8 +576,10 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 		goto free_trig;
 
 	trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (trig->name == NULL)
-		goto free_descs;
+	if (trig->name == NULL) {
+		irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
+		goto free_trig;
+	}
 
 	INIT_LIST_HEAD(&trig->list);
 
@@ -594,10 +597,8 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
 
 	return trig;
 
-free_descs:
-	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
 free_trig:
-	kfree(trig);
+	put_device(&trig->dev);
 	return NULL;
 }
 
-- 
2.17.1


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

end of thread, other threads:[~2025-11-09 13:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-07  2:02 [PATCH v3] iio: trigger: Fix error handling in viio_trigger_alloc Ma Ke
2025-11-07  8:03 ` Andy Shevchenko
2025-11-07 10:26 ` Nuno Sá
2025-11-07 10:42   ` Andy Shevchenko
2025-11-07 16:48     ` Nuno Sá
2025-11-07 18:19       ` Andy Shevchenko
2025-11-08 10:26         ` Nuno Sá
2025-11-09 13:54           ` Jonathan Cameron

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