iio: adc: ad799x: modernize resource management
Subsystems (2)
v3 cover iio: adc: ad799x: modernize resource management
This series modernizes the ad799x driver by converting all resource management to use the devm_ infrastructure, ultimately allowing for the complete removal of the ad799x_remove() function and simplifying the error paths in probe.
This series addresses the feedback from v2, where the changes were squashed into a single patch, by splitting the logical steps into individual, bisectable commits. It also incorporates feedback regarding the safe ordering of regulator disablement and IIO device unregistration.
Changes in v3:
- Split the monolithic v2 patch into 4 logical commits as requested by Andy Shevchenko.
Changes in v2:
- Incorporated feedback from David Lechner to eliminate dynamic buffer allocation.
- Incorporated feedback from Jonathan Cameron regarding the ordering hazards of mixing devm and manual teardown, utilizing devm_add_action and voltage caching.
Archit Anant (4):
iio: adc: ad799x: make rx_buf static and DMA-safe
iio: adc: ad799x: cache regulator voltages during probe
iio: adc: ad799x: use devm_add_action_or_reset for regulators
iio: adc: ad799x: use devm_iio_device_register and drop remove
drivers/iio/adc/ad799x.c | 86 ++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 43 deletions(-)
Signature · 1 line
2.39.5
PATCH v3 1/4 iio: adc: ad799x: make rx_buf static and DMA-safe
Currently, rx_buf is dynamically allocated using kmalloc() every time ad799x_update_scan_mode() is called. This can lead to memory leaks if the scan mask is updated multiple times.
Drop the dynamic allocation and replace it with a static, buffer at the end of the state structure using IIO_DECLARE_DMA_BUFFER_WITH_TS(). This eliminates the allocation overhead, prevents leaks, and removes the need for manual kfree() on driver removal.
--- drivers/iio/adc/ad799x.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/iio/adc/ad799x.c +4 -9
@@ -39,6 +39,7 @@#include <linux/iio/triggered_buffer.h> #define AD799X_CHANNEL_SHIFT 4 +#define AD799X_MAX_CHANNELS 8/* * AD7991, AD7995 and AD7999 defines
@@ -133,8 +134,8 @@ struct ad799x_state {unsigned int id;u16 config; -u8 *rx_buf;unsigned int transfer_size; +IIO_DECLARE_DMA_BUFFER_WITH_TS (__be16, rx_buf,AD799X_MAX_CHANNELS ); };static int ad799x_write_config (struct ad799x_state *st,u16 val)
@@ -217,11 +218,11 @@ static irqreturn_t ad799x_trigger_handler(int irq, void *p) } b_sent =i2c_smbus_read_i2c_block_data (st->client, - cmd, st->transfer_size, st->rx_buf); + cmd, st->transfer_size, (u8 *)st->rx_buf);if (b_sent <0 ) goto out; -iio_push_to_buffers_with_timestamp (indio_dev, st->rx_buf, +iio_push_to_buffers_with_timestamp (indio_dev, &st->rx_buf,iio_get_time_ns (indio_dev)); out:iio_trigger_notify_done (indio_dev->trig);
@@ -234,11 +235,6 @@ static int ad799x_update_scan_mode(struct iio_dev *indio_dev, {struct ad799x_state *st =iio_priv (indio_dev); -kfree (st->rx_buf); - st->rx_buf =kmalloc (indio_dev->scan_bytes,GFP_KERNEL ); -if (!st->rx_buf) -return -ENOMEM ; - st->transfer_size =bitmap_weight (scan_mask,iio_get_masklength (indio_dev)) *2 ;
@@ -895,7 +891,6 @@ static void ad799x_remove(struct i2c_client *client)if (st->vref)regulator_disable (st->vref);regulator_disable (st->reg); -kfree (st->rx_buf); }static int ad799x_suspend (struct device *dev)
Signature · 1 line
2.39.5
David Lechner
reviewerRe: [PATCH v3 1/4] iio: adc: ad799x: make rx_buf static and DMA-safe
Quoted 9 lines · Archit Anant
Currently,
rx_bufis dynamically allocated usingkmalloc()every timead799x_update_scan_mode()is called. This can lead to memory leaks if the scan mask is updated multiple times.Drop the dynamic allocation and replace it with a static, buffer at the end of the state structure using
IIO_DECLARE_DMA_BUFFER_WITH_TS(). This eliminates the allocation overhead, prevents leaks, and removes the need for manualkfree()on driver removal.
See replies in v1 and v2. DMA-safe is not important because it is I2C.
David Lechner
reviewerRe: [PATCH v3 1/4] iio: adc: ad799x: make rx_buf static and DMA-safe
In reply to Archit AnantQuoted 12 lines · Archit Anant
Currently,
rx_bufis dynamically allocated usingkmalloc()every timead799x_update_scan_mode()is called. This can lead to memory leaks if the scan mask is updated multiple times.Drop the dynamic allocation and replace it with a static, buffer at the end of the state structure using
IIO_DECLARE_DMA_BUFFER_WITH_TS(). This eliminates the allocation overhead, prevents leaks, and removes the need for manualkfree()on driver removal.Suggested-by David Lechner <[email protected]>Signed-off-by Archit Anant <[email protected]>---
...
drivers/iio/adc/ad799x.c static irqreturn_t ad799x_trigger_handler(int irq, void *p) · line 225
Quoted 11 lines
drivers/iio/adc/ad799x.c +2
-2@@ -217,11 +218,11 @@ static irqreturn_t ad799x_trigger_handler(int irq, void *p) } b_sent =i2c_smbus_read_i2c_block_data (st->client, - cmd, st->transfer_size, st->rx_buf); + cmd, st->transfer_size, (u8 *)st->rx_buf);if (b_sent <0 ) goto out; - iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, + iio_push_to_buffers_with_timestamp(indio_dev, &st->rx_buf,
Don't need & here since rx_buf is still an array.
Quoted 3 lines
drivers/iio/adc/ad799x.c +0
-0iio_get_time_ns(indio_dev)); out:iio_trigger_notify_done (indio_dev->trig);
PATCH v3 2/4 iio: adc: ad799x: cache regulator voltages during probe
In reply to Archit AnantReading the regulator voltage via regulator_get_voltage() can be a slow operation. Since the reference voltages for this ADC are not expected to change at runtime, it is inefficient to query the regulator API every time userspace reads the IIO_CHAN_INFO_SCALE attribute.
Cache the VCC and VREF voltages in the state structure during probe(). This improves the performance of ad799x_read_raw() and removes the dependency on the regulator pointers during fast-path reads.
--- drivers/iio/adc/ad799x.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iio/adc/ad799x.c +15 -2
@@ -135,6 +135,10 @@ struct ad799x_state {u16 config;unsigned int transfer_size; + +int vcc_uv; +int vref_uv; +IIO_DECLARE_DMA_BUFFER_WITH_TS (__be16, rx_buf,AD799X_MAX_CHANNELS ); };
@@ -303,9 +307,9 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,return IIO_VAL_INT ;case IIO_CHAN_INFO_SCALE :if (st->vref) - ret =regulator_get_voltage (st->vref); + ret = st->vref_uv;else - ret =regulator_get_voltage (st->reg); + ret = st->vcc_uv;if (ret <0 )return ret;
@@ -809,6 +813,10 @@ static int ad799x_probe(struct i2c_client *client) ret =regulator_enable (st->reg);if (ret)return ret; + ret =regulator_get_voltage (st->reg); +if (ret <0 ) + goto error_disable_reg; + st->vcc_uv = ret;/* check if an external reference is supplied */ if (chip_info->has_vref) {
@@ -827,6 +835,11 @@ static int ad799x_probe(struct i2c_client *client) ret =regulator_enable (st->vref);if (ret) goto error_disable_reg; + + ret =regulator_get_voltage (st->vref); +if (ret <0 ) + goto error_disable_vref; + st->vref_uv = ret; } }
Signature · 1 line
2.39.5
Re: [PATCH v3 2/4] iio: adc: ad799x: cache regulator voltages during probe
Quoted 8 lines · Archit Anant
Reading the regulator voltage via
regulator_get_voltage()can be a slow operation. Since the reference voltages for this ADC are not expected to change at runtime, it is inefficient to query the regulator API every time userspace reads theIIO_CHAN_INFO_SCALEattribute.Cache the VCC and VREF voltages in the state structure during
probe(). This improves the performance ofad799x_read_raw()and removes the dependency on the regulator pointers during fast-path reads.
...
drivers/iio/adc/ad799x.c struct ad799x_state { · line 140
Quoted 2 lines
drivers/iio/adc/ad799x.c +2
-0+int vcc_uv; +int vref_uv;
_uV in both cases, please. It's a unit suffix.
...
drivers/iio/adc/ad799x.c static int ad799x_read_raw(struct iio_dev *indio_dev, · line 315
Quoted 10 lines
drivers/iio/adc/ad799x.c +2
-2case IIO_CHAN_INFO_SCALE :if (st->vref) - ret =regulator_get_voltage (st->vref); + ret = st->vref_uv;else - ret =regulator_get_voltage (st->reg); + ret = st->vcc_uv;if (ret <0 )return ret;
Isn't it a dead check now?
Signature · 2 lines
With Best Regards, Andy Shevchenko
David Lechner
reviewerRe: [PATCH v3 2/4] iio: adc: ad799x: cache regulator voltages during probe
In reply to Archit AnantQuoted 32 lines · Archit Anant
Reading the regulator voltage via
regulator_get_voltage()can be a slow operation. Since the reference voltages for this ADC are not expected to change at runtime, it is inefficient to query the regulator API every time userspace reads theIIO_CHAN_INFO_SCALEattribute.Cache the VCC and VREF voltages in the state structure during
probe(). This improves the performance ofad799x_read_raw()and removes the dependency on the regulator pointers during fast-path reads.Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Archit Anant <[email protected]>--- drivers/iio/adc/ad799x.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)drivers/iio/adc/ad799x.c +4
-0@@ -135,6 +135,10 @@ struct ad799x_state {u16 config;unsigned int transfer_size; + +int vcc_uv; +int vref_uv; +IIO_DECLARE_DMA_BUFFER_WITH_TS (__be16, rx_buf,AD799X_MAX_CHANNELS ); };@@ -303,9 +307,9 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,return IIO_VAL_INT ;
drivers/iio/adc/ad799x.c static int ad799x_read_raw(struct iio_dev *indio_dev, · line 315
Quoted 10 lines
drivers/iio/adc/ad799x.c +2
-2case IIO_CHAN_INFO_SCALE :if (st->vref) - ret =regulator_get_voltage (st->vref); + ret = st->vref_uv;else - ret =regulator_get_voltage (st->reg); + ret = st->vcc_uv;if (ret <0 )return ret;
This can be simplified even more. See reply to v2.
We could even move the if statement to probe and only add one state variable.
PATCH v3 3/4 iio: adc: ad799x: use devm_add_action_or_reset for regulators
In reply to Archit AnantConvert the manual regulator_disable() handling to use devm_add_action_or_reset(). This guarantees that the regulators are disabled in the correct reverse order of allocation during device teardown or probe failure.
This removes the need to manually disable the regulators in the remove() function.
--- drivers/iio/adc/ad799x.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/iio/adc/ad799x.c +16 -3
@@ -781,6 +781,13 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, }; +static void ad799x_reg_disable (void *data) +{ +struct regulator *reg = data; + +regulator_disable (reg); +} +static int ad799x_probe (struct i2c_client *client) {const struct i2c_device_id *id =i2c_client_get_device_id (client);
@@ -818,6 +825,10 @@ static int ad799x_probe(struct i2c_client *client) goto error_disable_reg; st->vcc_uv = ret; + ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, st->reg); +if (ret) +return ret; +/* check if an external reference is supplied */ if (chip_info->has_vref) { st->vref =devm_regulator_get_optional (&client->dev,"vref" );
@@ -840,6 +851,11 @@ static int ad799x_probe(struct i2c_client *client)if (ret <0 ) goto error_disable_vref; st->vref_uv = ret; + + ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, + st->vref); +if (ret) + goto error_disable_reg; } }
@@ -901,9 +917,6 @@ static void ad799x_remove(struct i2c_client *client)iio_device_unregister (indio_dev);iio_triggered_buffer_cleanup (indio_dev); -if (st->vref) -regulator_disable (st->vref); -regulator_disable (st->reg); }static int ad799x_suspend (struct device *dev)
Signature · 1 line
2.39.5
Re: [PATCH v3 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators
Quoted 7 lines · Archit Anant
Convert the manual
regulator_disable()handling to usedevm_add_action_or_reset(). This guarantees that the regulators are disabled in the correct reverse order of allocation during device teardown or probe failure.This removes the need to manually disable the regulators in the
remove()function.
...
drivers/iio/adc/ad799x.c static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { · line 786
Quoted 4 lines
drivers/iio/adc/ad799x.c +3
-0+static void ad799x_reg_disable (void *data) +{ +struct regulator *reg = data;
Unneeded.
drivers/iio/adc/ad799x.c static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { · line 789
Quoted 2 lines
drivers/iio/adc/ad799x.c +2
-0+regulator_disable (reg); +}
static void ad799x_reg_disable(void *reg)
{
regulator_disable(reg);
}...
Quoted 1 line
static int
ad799x_probe(struct i2c_client *client)
With
struct device *dev = &client->dev;
it will look better.
...
drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 828
Quoted 1 line
drivers/iio/adc/ad799x.c +1
-0+ ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, st->reg);
ret = devm_add_action_or_reset(dev, ad799x_reg_disable, st->reg);
Quoted 2 lines
drivers/iio/adc/ad799x.c +2
-0+if (ret) +return ret;
...
drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 856
Quoted 2 lines
drivers/iio/adc/ad799x.c +2
-0+ ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, + st->vref);
ret = devm_add_action_or_reset(dev, ad799x_reg_disable, st->vref);
Quoted 2 lines
drivers/iio/adc/ad799x.c +2
-0+if (ret) + goto error_disable_reg;
Signature · 2 lines
With Best Regards, Andy Shevchenko
David Lechner
reviewerRe: [PATCH v3 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators
In reply to Archit AnantQuoted 44 lines · Archit Anant
Convert the manual
regulator_disable()handling to usedevm_add_action_or_reset(). This guarantees that the regulators are disabled in the correct reverse order of allocation during device teardown or probe failure.This removes the need to manually disable the regulators in the
remove()function.Suggested-by David Lechner <[email protected]>Signed-off-by Archit Anant <[email protected]>--- drivers/iio/adc/ad799x.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)drivers/iio/adc/ad799x.c +11
-0@@ -781,6 +781,13 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { }, }; +static void ad799x_reg_disable (void *data) +{ +struct regulator *reg = data; + +regulator_disable (reg); +} +static int ad799x_probe (struct i2c_client *client) {const struct i2c_device_id *id =i2c_client_get_device_id (client);@@ -818,6 +825,10 @@ static int ad799x_probe(struct i2c_client *client) goto error_disable_reg; st->vcc_uv = ret; + ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, st->reg); +if (ret) +return ret; +/* check if an external reference is supplied */ if (chip_info->has_vref) { st->vref =devm_regulator_get_optional (&client->dev,"vref" );
drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 858
Quoted 8 lines
drivers/iio/adc/ad799x.c +5
-0@@ -840,6 +851,11 @@ static int ad799x_probe(struct i2c_client *client)if (ret <0 ) goto error_disable_vref; st->vref_uv = ret; + + ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, + st->vref); +if (ret) + goto error_disable_reg;
This goto and others should be removed in this patch. devm handles it now.
Quoted 13 lines
drivers/iio/adc/ad799x.c +0
-3} }@@ -901,9 +917,6 @@ static void ad799x_remove(struct i2c_client *client)iio_device_unregister (indio_dev);iio_triggered_buffer_cleanup (indio_dev); -if (st->vref) -regulator_disable (st->vref); -regulator_disable (st->reg); }static int ad799x_suspend (struct device *dev)
PATCH v3 4/4 iio: adc: ad799x: use devm_iio_device_register and drop remove
In reply to Archit AnantConvert the driver to use the device-managed versions of iio_device_register() and iio_triggered_buffer_setup().
Because all resources (buffer, regulators, IRQs, IIO device) are now managed by the devm core, the unwinding order is guaranteed to be safe and correct. We can safely remove all manual error handling goto labels in ad799x_probe() and delete the ad799x_remove() function entirely.
This eliminates boilerplate code and prevents future resource leaks.
--- drivers/iio/adc/ad799x.c | 43 ++++++++++------------------------------ 1 file changed, 11 insertions(+), 32 deletions(-)
drivers/iio/adc/ad799x.c +11 -32
@@ -822,7 +822,7 @@ static int ad799x_probe(struct i2c_client *client)return ret; ret =regulator_get_voltage (st->reg);if (ret <0 ) - goto error_disable_reg; +return ret; st->vcc_uv = ret; ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, st->reg);
@@ -835,7 +835,7 @@ static int ad799x_probe(struct i2c_client *client) ret =PTR_ERR_OR_ZERO (st->vref);if (ret) {if (ret != -ENODEV ) - goto error_disable_reg; +return ret; st->vref =NULL ;dev_info (&client->dev,"Using VCC reference voltage\n" ); }
@@ -845,17 +845,17 @@ static int ad799x_probe(struct i2c_client *client) extra_config |=AD7991_REF_SEL ; ret =regulator_enable (st->vref);if (ret) - goto error_disable_reg; +return ret; ret =regulator_get_voltage (st->vref);if (ret <0 ) - goto error_disable_vref; +return ret; st->vref_uv = ret; ret =devm_add_action_or_reset (&client->dev, ad799x_reg_disable, st->vref);if (ret) - goto error_disable_reg; +return ret; } }
@@ -870,12 +870,12 @@ static int ad799x_probe(struct i2c_client *client) ret =ad799x_update_config (st, st->chip_config->default_config | extra_config);if (ret) - goto error_disable_vref; +return ret; - ret =iio_triggered_buffer_setup (indio_dev,NULL , + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev,NULL , &ad799x_trigger_handler,NULL );if (ret) - goto error_disable_vref; +return ret;if (client->irq >0 ) { ret = devm_request_threaded_irq(&client->dev,
@@ -887,36 +887,16 @@ static int ad799x_probe(struct i2c_client *client) client->name, indio_dev);if (ret) - goto error_cleanup_ring; +return ret; }mutex_init (&st->lock); - ret =iio_device_register (indio_dev); + ret =devm_iio_device_register (&client->dev, indio_dev);if (ret) - goto error_cleanup_ring; +return ret;return 0 ; - -error_cleanup_ring: -iio_triggered_buffer_cleanup (indio_dev); -error_disable_vref: -if (st->vref) -regulator_disable (st->vref); -error_disable_reg: -regulator_disable (st->reg); - -return ret; -} - -static void ad799x_remove (struct i2c_client *client) -{ -struct iio_dev *indio_dev =i2c_get_clientdata (client); -struct ad799x_state *st =iio_priv (indio_dev); - -iio_device_unregister (indio_dev); - -iio_triggered_buffer_cleanup (indio_dev); }static int ad799x_suspend (struct device *dev)
@@ -986,7 +966,6 @@ static struct i2c_driver ad799x_driver = { .pm = pm_sleep_ptr(&ad799x_pm_ops), }, .probe = ad799x_probe, - .remove = ad799x_remove, .id_table = ad799x_id, };module_i2c_driver (ad799x_driver);
Signature · 1 line
2.39.5
Re: [PATCH v3 4/4] iio: adc: ad799x: use devm_iio_device_register and drop remove
Quoted 10 lines · Archit Anant
Convert the driver to use the device-managed versions of
iio_device_register()andiio_triggered_buffer_setup().Because all resources (buffer, regulators, IRQs, IIO device) are now managed by the devm core, the unwinding order is guaranteed to be safe and correct. We can safely remove all manual error handling goto labels in
ad799x_probe()and delete thead799x_remove()function entirely.This eliminates boilerplate code and prevents future resource leaks.
...
Quoted 1 line
mutex_init(&st->lock);
Missed devm_mutex_init().
Signature · 2 lines
With Best Regards, Andy Shevchenko
David Lechner
reviewerQuoted 14 lines · Archit Anant
This series modernizes the ad799x driver by converting all resource management to use the
devm_infrastructure, ultimately allowing for the complete removal of thead799x_remove()function and simplifying the error paths in probe.This series addresses the feedback from v2, where the changes were squashed into a single patch, by splitting the logical steps into individual, bisectable commits. It also incorporates feedback regarding the safe ordering of regulator disablement and IIO device unregistration.
Changes in v3:
- Split the monolithic v2 patch into 4 logical commits as requested by Andy Shevchenko.
Quoted 7 lines
Changes in v2:
- Incorporated feedback from David Lechner to eliminate dynamic buffer allocation.
- Incorporated feedback from Jonathan Cameron regarding the ordering hazards of mixing devm and manual teardown, utilizingdevm_add_actionand voltage caching.
I just reviewed v2 because I didn't see there was a v3 yet. This is why we ask to wait ideally 1 week, but at a few days before sending a new revision.
Some of those comment will still apply to v3.
Hi David,
Quoted 24 lines · David Lechner
Quoted 14 lines · Archit Anant
This series modernizes the ad799x driver by converting all resource management to use the
devm_infrastructure, ultimately allowing for the complete removal of thead799x_remove()function and simplifying the error paths in probe.This series addresses the feedback from v2, where the changes were squashed into a single patch, by splitting the logical steps into individual, bisectable commits. It also incorporates feedback regarding the safe ordering of regulator disablement and IIO device unregistration.
Changes in v3:
- Split the monolithic v2 patch into 4 logical commits as requested by Andy Shevchenko.Quoted 7 lines
Changes in v2:
- Incorporated feedback from David Lechner to eliminate dynamic buffer allocation.
- Incorporated feedback from Jonathan Cameron regarding the ordering hazards of mixing devm and manual teardown, utilizingdevm_add_actionand voltage caching.
Quoted 3 lines
I just reviewed v2 because I didn't see there was a v3 yet. This is why we ask to wait ideally 1 week, but at a few days before sending a new revision.
I agree, I should have waited longer before sending the next revision. After Andy's feedback on v2, I realized that the monolithic patch was difficult to review and wanted to get the split series out as quickly as possible to save reviewers' time. However, I see now that sending it too fast caused a different kind of confusion.
Quoted 2 lines
Some of those comment will still apply to v3.
I will wait a few days to collect all feedback on v3 before preparing and sending v4.
Thanks for the patience and the review!
Signature · 2 lines
Sincerely, Archit Anant