Skip to contentSkip to conversation

iio: trigger: Fix error handling in viio_trigger_alloc

PATCH In review Ma Ke 5 participants 8 messages Nov 7, 2025 – Nov 9, 2025 IIO

lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v3 1/1 iio: trigger: Fix error handling in viio_trigger_alloc

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.

---
 drivers/iio/industrialio-trigger.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/iio/industrialio-trigger.c +6 -5
index 54416a384232..7576dedee68e 100644
@@ -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;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 18 lines · 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.

...

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent,

Quoted 6 lines
        trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
drivers/iio/industrialio-trigger.c +2 -2
-       if (trig->name == NULL)
-               goto free_descs;
+       if (trig->name == NULL) {
 
+               irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);

Why?

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 582

Quoted 2 lines
drivers/iio/industrialio-trigger.c +2 -0
+               goto free_trig;
+       }

Please, slow down and think a bit. Also you may split the patch to at least two:
1) add missed mutex_destroy();
2) fix the clean up in the error path.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Ma Ke
Quoted 7 lines · 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.

How? If I'm not missing nothing, your patch is the one bringing the above issue.

Quoted 1 line

2. The release function may attempt to free negative subirq_base.

Same question, how?

Quoted 2 lines

3. Missing mutex_destroy() in release function.

Mostly important for debugging mutexes but not super important.

Quoted 19 lines

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.

---
 drivers/iio/industrialio-trigger.c | 11 ++++++-----

 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-trigger.c +0 -0

trigger.c
index 54416a384232..7576dedee68e 100644

drivers/iio/industrialio-trigger.c static void iio_trig_release(struct device *device) · line 526

Quoted 3 lines
drivers/iio/industrialio-trigger.c +0 -0
@@ -524,6 +524,7 @@ static void iio_trig_release(struct device *device)
  			       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  	}
  	kfree(trig->name);

Not that this is a problem, but now you can actually get in here with trig->name = NULL. And typically that's not a get pattern for your code.

Quoted 13 lines
drivers/iio/industrialio-trigger.c +1 -0
+	mutex_destroy(&trig->pool_lock);
  	kfree(trig);
  }

drivers/iio/industrialio-trigger.c +0 -0
@@ -575,8 +576,10 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,
  		goto free_trig;


  trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);

drivers/iio/industrialio-trigger.c +2 -2
-	if (trig->name == NULL)
-		goto free_descs;
+	if (trig->name == NULL) {
+		irq_free_descs(trig->subirq_base,

CONFIG_IIO_CONSUMERS_PER_TRIGGER);

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 581

Quoted 1 line
drivers/iio/industrialio-trigger.c +1 -0
+		goto free_trig;

I think now we do have double free of irq_free_descs(), or am I missing something?

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 601

Quoted 13 lines
drivers/iio/industrialio-trigger.c +1 -0
+	}


  INIT_LIST_HEAD(&trig->list);

drivers/iio/industrialio-trigger.c +0 -0
@@ -594,10 +597,8 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent,


  return trig;

drivers/iio/industrialio-trigger.c +0 -2
-free_descs:
-	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);

 free_trig:

drivers/iio/industrialio-trigger.c +1 -1
-	kfree(trig);
+	put_device(&trig->dev);

Yes, device_initialize() docs do say that we should give the reference instead of freeing the device but I'm not see how that helps in here. Maybe initializing the device should be done only after all the resources are allocated so the code is a bit more clear... But doing it like you're doing just means that we might get into the release function with things that might or might not be allocated which is a pattern I would prefer to avoid.

- Nuno Sá

Quoted 3 lines

  return NULL;
 }

#3 lore raw
Quoted 5 lines · Nuno Sá
Quoted 4 lines · 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.

...

Quoted 4 lines
Quoted 4 lines
drivers/iio/industrialio-trigger.c +0 -2
-free_descs:
-	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);

 free_trig:

drivers/iio/industrialio-trigger.c +0 -1
-	kfree(trig);
Quoted 8 lines

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 601

Quoted 1 line
drivers/iio/industrialio-trigger.c +1 -0
+	put_device(&trig->dev);

Yes, device_initialize() docs do say that we should give the reference instead of freeing the device but I'm not see how that helps in here. Maybe initializing the device should be done only after all the resources are allocated so the code is a bit more clear... But doing it like you're doing just means that we might get into the release function with things that might or might not be allocated which is a pattern I would prefer to avoid.

The put_device() here is the correct (and must) thing to do independently on the preferences. The problem is that device_initialise() and followed calls may do much more than just some initialisation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 25 lines · Andy Shevchenko
Quoted 5 lines · Nuno Sá
Quoted 4 lines · 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.

...

Quoted 7 lines

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 601

Quoted 5 lines
drivers/iio/industrialio-trigger.c +0 -2
-free_descs:
-	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);

 free_trig:

drivers/iio/industrialio-trigger.c +1 -1
-	kfree(trig);
+	put_device(&trig->dev);

Yes, device_initialize() docs do say that we should give the reference instead of

Quoted 8 lines

freeing the device but I'm not see how that helps in here. Maybe initializing the device should be done only after all the resources are allocated so the code is a bit
more clear... But doing it like you're doing just means that we might get into the
release function with things that might or might not be allocated which is a pattern
I would prefer to avoid.

Quoted 4 lines

The put_device() here is the correct (and must) thing to do independently on the preferences. The problem is that device_initialise() and followed calls may do much more than just some initialisation.

Well, I would argue against that (at least in the context the function is now implemented). To me, the right thing to do would be to move the device initialization code to this point:

https://elixir.bootlin.com/linux/v6.17.7/source/drivers/iio/industrialio-trigger.c#L594

trig->dev.parent = parent;
trig->dev.type = &iio_trig_type;
trig->dev.bus = &iio_bus_type;
device_initialize(&trig->dev);

Then we would not even need to think about put_device(). Like it is, using it, it's just prone to errors (I did mentioned a couple of things this patch introduced If I'm not overseeing it) or we do need to have lots of care in the release function to make sure we don't mess up. To me that's a bad sign on how the code is architectured.

FWIW, the pattern you find for example in SPI is the natural one for me:

You have a spi_alloc_device() [1] that initialises struct device right in the end. Above it, kfree() as usual. Then the callers, will indeed use put_device() in their error paths.

So the pattern to me is to do device_initialize() after all resources of your device are allocated. So that after that point put_device() does not get you into some odd handling in the release callback.

[1]: https://elixir.bootlin.com/linux/v6.17.7/source/drivers/spi/spi.c#L568

- Nuno Sá

#5 lore raw
Quoted 7 lines · Nuno Sá
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Nuno Sá
Quoted 4 lines · 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.

...

Quoted 38 lines
Quoted 16 lines
Quoted 7 lines

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 601

Quoted 5 lines
drivers/iio/industrialio-trigger.c +0 -2
-free_descs:
-	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);

 free_trig:

drivers/iio/industrialio-trigger.c +1 -1
-	kfree(trig);
+	put_device(&trig->dev);

Yes, device_initialize() docs do say that we should give the reference instead of

Quoted 8 lines

freeing the device but I'm not see how that helps in here. Maybe initializing the device should be done only after all the resources are allocated so the code is a bit
more clear... But doing it like you're doing just means that we might get into the
release function with things that might or might not be allocated which is a pattern
I would prefer to avoid.

Quoted 3 lines

The put_device() here is the correct (and must) thing to do independently on the preferences. The problem is that device_initialise() and followed calls may do much more than just some initialisation.

Well, I would argue against that (at least in the context the function is now implemented). To me, the right thing to do would be to move the device initialization code to this point:

https://elixir.bootlin.com/linux/v6.17.7/source/drivers/iio/industrialio-trigger.c#L594

trig->dev.parent = parent;
trig->dev.type = &iio_trig_type;
trig->dev.bus = &iio_bus_type;
device_initialize(&trig->dev);

Then we would not even need to think about put_device(). Like it is, using it, it's just prone to errors (I did mentioned a couple of things this patch introduced If I'm not overseeing it) or we do need to have lots of care in the release function to make sure we don't mess up. To me that's a bad sign on how the code is architectured.

FWIW, the pattern you find for example in SPI is the natural one for me:

Quoted 7 lines

You have a spi_alloc_device() [1] that initialises struct device right in the end. Above it, kfree() as usual. Then the callers, will indeed use put_device() in their error paths.

So the pattern to me is to do device_initialize() after all resources of your device are allocated. So that after that point put_device() does not get you into some odd handling in the release callback.

Sure, this can be another approach. Whatever you, folks, prefer. But at least the mutex_destroy() (separate) patch can be issued and accepted independently.

The bottom line is:
1) the current code has an issue;
2) the proposed fix has its own flaws;
3) but the idea in the current approach at least small (if implemented correctly) and makes sure that any new allocations won't be forgotten in the error patch, nor in the ->release() callback.

Quoted 1 line

[1]: https://elixir.bootlin.com/linux/v6.17.7/source/drivers/spi/spi.c#L568

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 64 lines · Andy Shevchenko
Quoted 7 lines · Nuno Sá
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Nuno Sá
Quoted 4 lines · 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.

...

Quoted 53 lines
Quoted 21 lines
Quoted 12 lines

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 601

Quoted 6 lines
(quoted diff) +0 -2
-free_descs:
-	irq_free_descs(trig->subirq_base,

CONFIG_IIO_CONSUMERS_PER_TRIGGER);
 free_trig:

drivers/iio/industrialio-trigger.c +1 -1
-	kfree(trig);
+	put_device(&trig->dev);

Yes, device_initialize() docs do say that we should give the reference instead of
freeing the device but I'm not see how that helps in here. Maybe initializing the
device should be done only after all the resources are allocated so the code

Quoted 8 lines

is a
bit
more clear... But doing it like you're doing just means that we might get into
the
release function with things that might or might not be allocated which is a pattern
I would prefer to avoid.

Quoted 3 lines

The put_device() here is the correct (and must) thing to do independently on the preferences. The problem is that device_initialise() and followed calls may do much more than just some initialisation.

Well, I would argue against that (at least in the context the function is now implemented). To me, the right thing to do would be to move the device initialization
code to this point:

https://elixir.bootlin.com/linux/v6.17.7/source/drivers/iio/industrialio-trigger.c#L594

trig->dev.parent = parent;
trig->dev.type = &iio_trig_type;
trig->dev.bus = &iio_bus_type;
device_initialize(&trig->dev);

Then we would not even need to think about put_device(). Like it is, using it, it's
just prone to errors (I did mentioned a couple of things this patch introduced If I'm
not overseeing it) or we do need to have lots of care in the release function to make
sure we don't mess up. To me that's a bad sign on how the code is architectured.

FWIW, the pattern you find for example in SPI is the natural one for me:

You have a spi_alloc_device() [1] that initialises struct device right in the end.
Above it, kfree() as usual. Then the callers, will indeed use put_device() in their
error paths.

Quoted 9 lines
Quoted 5 lines

So the pattern to me is to do device_initialize() after all resources of your device
are allocated. So that after that point put_device() does not get you into some odd
handling in the release callback.

Sure, this can be another approach. Whatever you, folks, prefer. But at least the mutex_destroy() (separate) patch can be issued and accepted independently.

Sure, agreed on that.

Quoted 8 lines

The bottom line is:
1) the current code has an issue;
2) the proposed fix has its own flaws;
3) but the idea in the current approach at least small (if implemented correctly) and makes sure that any new allocations won't be forgotten in the error patch, nor in the ->release() callback.

Quoted 1 line

[1]: https://elixir.bootlin.com/linux/v6.17.7/source/drivers/spi/spi.c#L568

FWIW and unless I'm missing something there's nothing fundamentally wrong in the current code (i.e any real bug). That said, I would ack a change that moved the device initialization code to it's natural place (at least in the way I see it).

- Nuno Sá

maintainer
#7 lore raw
Quoted 84 lines · On Sat, 08 Nov 2025 10:26:21 +0000 Nuno Sá <[email protected]> wrote:
Quoted 64 lines · Andy Shevchenko
Quoted 7 lines · Nuno Sá
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Nuno Sá
Quoted 4 lines · 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.

...

Quoted 53 lines
Quoted 21 lines
Quoted 12 lines

drivers/iio/industrialio-trigger.c struct iio_trigger *viio_trigger_alloc(struct device *parent, · line 601

Quoted 6 lines
(quoted diff) +0 -2
-free_descs:
-	irq_free_descs(trig->subirq_base,

CONFIG_IIO_CONSUMERS_PER_TRIGGER);
 free_trig:

drivers/iio/industrialio-trigger.c +1 -1
-	kfree(trig);
+	put_device(&trig->dev);  

Yes, device_initialize() docs do say that we should give the reference instead of
freeing the device but I'm not see how that helps in here. Maybe initializing the
device should be done only after all the resources are allocated so the code

Quoted 8 lines

is a
bit
more clear... But doing it like you're doing just means that we might get into
the
release function with things that might or might not be allocated which is a pattern
I would prefer to avoid.

Quoted 3 lines

The put_device() here is the correct (and must) thing to do independently on the preferences. The problem is that device_initialise() and followed calls may do much more than just some initialisation.

Well, I would argue against that (at least in the context the function is now implemented). To me, the right thing to do would be to move the device initialization
code to this point:

https://elixir.bootlin.com/linux/v6.17.7/source/drivers/iio/industrialio-trigger.c#L594

trig->dev.parent = parent;
trig->dev.type = &iio_trig_type;
trig->dev.bus = &iio_bus_type;
device_initialize(&trig->dev);

Then we would not even need to think about put_device(). Like it is, using it, it's
just prone to errors (I did mentioned a couple of things this patch introduced If I'm
not overseeing it) or we do need to have lots of care in the release function to make
sure we don't mess up. To me that's a bad sign on how the code is architectured.

FWIW, the pattern you find for example in SPI is the natural one for me:

You have a spi_alloc_device() [1] that initialises struct device right in the end.
Above it, kfree() as usual. Then the callers, will indeed use put_device() in their
error paths.

Quoted 9 lines
Quoted 5 lines

So the pattern to me is to do device_initialize() after all resources of your device
are allocated. So that after that point put_device() does not get you into some odd
handling in the release callback.

Sure, this can be another approach. Whatever you, folks, prefer. But at least the mutex_destroy() (separate) patch can be issued and accepted independently.

Sure, agreed on that.

Quoted 7 lines

The bottom line is:
1) the current code has an issue;
2) the proposed fix has its own flaws;
3) but the idea in the current approach at least small (if implemented correctly) and makes sure that any new allocations won't be forgotten in the error patch, nor in the ->release() callback.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

[1]: https://elixir.bootlin.com/linux/v6.17.7/source/drivers/spi/spi.c#L568

FWIW and unless I'm missing something there's nothing fundamentally wrong in the current code (i.e any real bug). That said, I would ack a change that moved the device initialization code to it's natural place (at least in the way I see it).

Agreed. I think this is simpler change. The other one that's be be happy with would be to adjust the release to always work so we only have a put_device() in here and no other error cleanup. Which means a flag or using whether the irq_chip is set to split the two things currently under the single condition.

Jonathan

Quoted 2 lines

- Nuno Sá