iio: ssp_sensors: improve resource cleanup
v5 cover iio: ssp_sensors: improve resource cleanup
From: Sanjay Chitroda <[email protected]>
Hi all,
This patch series improves resource cleanup and error handling in the SSP IIO SPI driver by adopting the recently introduced cleanup helpers.
The changes focus on making probe/remove paths more robust and easier to reason about by reducing manual unwind logic and ensuring that locks and dynamically allocated resources are released consistently across all exit paths.
Key highlights of this series:
- Reuse preallocate rx buffer for SPI transfer instead of allocating new DMA memory in interrupt context for write transaction between AP <-> HUB.
- Replace dynamic resource lifetime management with device‑managed (devm_) APIs to simplify cleanup and error paths.
- Refactor common helper API, improving readability and reducing duplicatation.
- Address minor codestyle warnings introduced or exposed by the cleanup refactoring.
Changes in v5:
- Drop usage of guard() helpers to avoid mixing mutex_lock() with guard()(), based on reviewer feedback.
- 0003: Refactor shared helper API, reducing duplication.
- 0004: Convert resource allocation and teardown to devm_ managed APIs to simplify error handling and probe/remove paths.
Changes in v4:
- Update sequence and make checkpatch style fix in base change
- Split WARNING and CHECK change with input of Andy as 0001 and 0002
- 0003: Use guard() instead of scoped_guard()
- 0004: Use preallocated buffer to stash memory allocation
No functional behavior changes are intended.
Testing:
- Compiled with W=1
- Build-tested on QEMU x86_64
Based on:
<linux-v7.0-rc5>
Feedback and reviews are very welcome.
Thanks,
Sanjay Chitroda
Sanjay Chitroda (5):
iio: ssp_sensors: cleanup codestyle warning
iio: ssp_sensors: cleanup codestyle check
iio: ssp_sensors: factor out pending list add/remove helpers
iio: ssp_sensors: use devm APIs for mutex and IRQ resources
iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
drivers/iio/common/ssp_sensors/ssp.h | 5 ++ drivers/iio/common/ssp_sensors/ssp_dev.c | 48 +++++++++++-------- drivers/iio/common/ssp_sensors/ssp_spi.c | 61 +++++++++++------------- 3 files changed, 62 insertions(+), 52 deletions(-)
Signature · 1 line
2.34.1
PATCH v5 1/5 iio: ssp_sensors: cleanup codestyle warning
From: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE: drivers/iio/common/ssp_sensors/ssp_spi.c
WARNING: Prefer __packed over __attribute__((__packed__)) +} __attribute__((__packed__));
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +1 -1
@@ -29,7 +29,7 @@ struct ssp_msg_header {__le16 length;__le16 options;__le32 data; -} __attribute__((__packed__)); +} __packed;struct ssp_msg {u16 length;
Signature · 1 line
2.34.1
PATCH v5 2/5 iio: ssp_sensors: cleanup codestyle check
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE: drivers/iio/common/ssp_sensors/ssp_spi.c
CHECK: Macro argument '...' may be better as '(...)' to avoid precedence issues
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +3 -3
@@ -6,7 +6,7 @@#include "ssp.h" #define SSP_DEV (&data->spi->dev) -#define SSP_GET_MESSAGE_TYPE (data) (data & (3 << SSP_RW)) +#define SSP_GET_MESSAGE_TYPE (data) ((data) & (3 << SSP_RW))/* * SSP -> AP Instruction
@@ -119,9 +119,9 @@ static inline void ssp_get_buffer(struct ssp_msg *m, unsigned int offset, }#define SSP_GET_BUFFER_AT_INDEX (m, index) \ - (m->buffer[SSP_HEADER_SIZE_ALIGNED + index]) + ((m)->buffer[SSP_HEADER_SIZE_ALIGNED + (index)])#define SSP_SET_BUFFER_AT_INDEX (m, index, val) \ - (m->buffer[SSP_HEADER_SIZE_ALIGNED + index] = val) + ((m)->buffer[SSP_HEADER_SIZE_ALIGNED + (index)] = val)static void ssp_clean_msg (struct ssp_msg *m) {
Signature · 1 line
2.34.1
PATCH v5 3/5 iio: ssp_sensors: factor out pending list add/remove helpers
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
The SSP SPI transfer path manipulates the pending message list in multiple places, each time open-coding the same locking and list operations.
Re-factor the pending list add and delete logic into small helper functions to avoid duplication and simplify transfer flow to follow.
No functional change intended.
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 34 +++++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +21 -13
@@ -174,6 +174,22 @@ static int ssp_check_lines(struct ssp_data *data, bool state)return 0 ; } +static inline void ssp_pending_add (struct ssp_data *data, +struct ssp_msg *msg) +{ +mutex_lock (&data->pending_lock); +list_add_tail (&msg->list, &data->pending_list); +mutex_unlock (&data->pending_lock); +} + +static inline void ssp_pending_del (struct ssp_data *data, +struct ssp_msg *msg) +{ +mutex_lock (&data->pending_lock); +list_del (&msg->list); +mutex_unlock (&data->pending_lock); +} +static int ssp_do_transfer (struct ssp_data *data,struct ssp_msg *msg,struct completion *done,int timeout) {
@@ -202,19 +218,13 @@ static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg, goto _error_locked; } -if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_add_tail (&msg->list, &data->pending_list); -mutex_unlock (&data->pending_lock); - } +if (!use_no_irq) +ssp_pending_add (data, msg); status =ssp_check_lines (data, true);if (status <0 ) { -if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); - } +if (!use_no_irq) +ssp_pending_del (data, msg); goto _error_locked; }
@@ -224,9 +234,7 @@ static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg,if (wait_for_completion_timeout (done,msecs_to_jiffies (timeout)) ==0 ) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); +ssp_pending_del (data, msg); data->timeout_cnt++;return -ETIMEDOUT ;
Signature · 1 line
2.34.1
PATCH v5 4/5 iio: ssp_sensors: use devm APIs for mutex and IRQ resources
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
Convert mutex initialization and IRQ registration to use devm-managed helpers, tying their lifetime to the device.
This removes the need for explicit cleanup in the error and remove paths, as the resources are automatically released on probe failure or device unbind.
--- drivers/iio/common/ssp_sensors/ssp_dev.c | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-)
drivers/iio/common/ssp_sensors/ssp_dev.c +17 -19
@@ -510,7 +510,11 @@ static int ssp_probe(struct spi_device *spi) data->spi = spi;spi_set_drvdata (spi, data); -mutex_init (&data->comm_lock); + ret =devm_mutex_init (&spi->dev, &data->comm_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init comm_lock mutex\n" ); + goto err_setup_spi; + }for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ;
@@ -523,7 +527,11 @@ static int ssp_probe(struct spi_device *spi) data->time_syncing = true; -mutex_init (&data->pending_lock); + ret =devm_mutex_init (&spi->dev, &data->pending_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init pending_lock mutex\n" ); + goto err_setup_spi; + }INIT_LIST_HEAD (&data->pending_list);atomic_set (&data->enable_refcount,0 );
@@ -533,13 +541,13 @@ static int ssp_probe(struct spi_device *spi)timer_setup (&data->wdt_timer, ssp_wdt_timer_func,0 ); - ret =request_threaded_irq (data->spi->irq,NULL , - ssp_irq_thread_fn, -IRQF_TRIGGER_FALLING |IRQF_ONESHOT , -"SSP_Int" , data); + ret =devm_request_threaded_irq (&spi->dev, data->spi->irq,NULL , + ssp_irq_thread_fn, +IRQF_TRIGGER_FALLING |IRQF_ONESHOT , +"SSP_Int" , data);if (ret <0 ) {dev_err (&spi->dev,"Irq request fail\n" ); - goto err_setup_irq; + goto err_setup_spi; }/* Let's start with enabled one so irq balance could be ok */
@@ -553,21 +561,16 @@ static int ssp_probe(struct spi_device *spi) ret =ssp_initialize_mcu (data);if (ret <0 ) {dev_err (&spi->dev,"Initialize_mcu failed\n" ); - goto err_read_reg; + goto err_setup_spi; } }else {dev_err (&spi->dev,"Firmware version not supported\n" ); ret = -EPERM ; - goto err_read_reg; + goto err_setup_spi; }return 0 ; -err_read_reg: -free_irq (data->spi->irq, data); -err_setup_irq: -mutex_destroy (&data->pending_lock); -mutex_destroy (&data->comm_lock); err_setup_spi:mfd_remove_devices (&spi->dev);
@@ -589,14 +592,9 @@ static void ssp_remove(struct spi_device *spi)ssp_clean_pending_list (data); -free_irq (data->spi->irq, data); -timer_delete_sync (&data->wdt_timer);cancel_work_sync (&data->work_wdt); -mutex_destroy (&data->comm_lock); -mutex_destroy (&data->pending_lock); -mfd_remove_devices (&spi->dev); }
Signature · 1 line
2.34.1
PATCH v5 5/5 iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
Avoid allocating a temporary DMA buffer in the interrupt context when handling hub-to-AP and AP-to-hub SPI write messages.
Preallocate RX buffer during probe and reuse it for SPI receive operations. This removes repeated kzalloc() calls from the IRQ path, reduces allocation overhead, and avoids potential allocation failures under memory pressure.
The RX buffer size is tracked and allocated using devm_kzalloc(), ensuring proper lifetime management tied to the device.
No functional change intended; this is an internal optimization and robustness improvement.
---
Changes in v5:
- Rebase change on top of latest v5 patch series.
Changes in v4:
- Use preallocated buffer and stash a buffer that gets reused each time instead of a fresh allocation.
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- prepare series to have all respective cleanup API support for the ssp_sensors following input from Andy Shevchenko
- Link to v2 https://lore.kernel.org/all/[email protected]/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/[email protected]/
--- drivers/iio/common/ssp_sensors/ssp.h | 5 +++++ drivers/iio/common/ssp_sensors/ssp_dev.c | 12 ++++++++++++ drivers/iio/common/ssp_sensors/ssp_spi.c | 19 +++---------------- 3 files changed, 20 insertions(+), 16 deletions(-)
drivers/iio/common/ssp_sensors/ssp.h +5 -0
@@ -175,6 +175,8 @@ struct ssp_sensorhub_info {* @sensor_devs: registered IIO devices table * @enable_refcount: enable reference count for wdt (watchdog timer) * @header_buffer: cache aligned buffer for packet header +* @rx_buf: buffer to receive SPI data +* @rx_buf_size: allocated size of rx_buf */ struct ssp_data {struct spi_device *spi;
@@ -222,6 +224,9 @@ struct ssp_data {atomic_t enable_refcount;__le16 header_buffer[SSP_HEADER_BUFFER_SIZE /sizeof (__le16)]__aligned(IIO_DMA_MINALIGN) ; + +u8 *rx_buf; +size_t rx_buf_size; };void ssp_clean_pending_list (struct ssp_data *data);
drivers/iio/common/ssp_sensors/ssp_dev.c +12 -0
@@ -516,6 +516,18 @@ static int ssp_probe(struct spi_device *spi) goto err_setup_spi; } + data->rx_buf_size =SSP_DATA_PACKET_SIZE ; + data->rx_buf =devm_kzalloc (&spi->dev, + data->rx_buf_size, +GFP_KERNEL |GFP_DMA ); + +if (!data->rx_buf) { +dev_err (&spi->dev, +"Failed to allocate memory for rx_buf\n" ); + ret = -ENOMEM ; + goto err_setup_spi; + } +for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ; data->batch_latency_buf[i] =0 ;
drivers/iio/common/ssp_sensors/ssp_spi.c +3 -16
@@ -383,19 +383,13 @@ int ssp_irq_msg(struct ssp_data *data)* but the slave should not send such ones - it is to * check but let's handle this */ - buffer =kmalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) { - ret = -ENOMEM ; - goto _unlock; - } + buffer = data->rx_buf;/* got dead packet so it is always an error */ ret =spi_read (data->spi, buffer, length);if (ret >=0 ) ret = -EPROTO ; -kfree (buffer); -dev_err (SSP_DEV ,"No match error %x\n" , msg_options);
@@ -428,22 +422,15 @@ int ssp_irq_msg(struct ssp_data *data)mutex_unlock (&data->pending_lock);break ;case SSP_HUB2AP_WRITE : - buffer =kzalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) -return -ENOMEM ; + buffer = data->rx_buf; ret =spi_read (data->spi, buffer, length);if (ret <0 ) {dev_err (SSP_DEV ,"spi read fail\n" ); -kfree (buffer);break ; } - ret =ssp_parse_dataframe (data, buffer, length); - -kfree (buffer); -break ; - +return ssp_parse_dataframe (data, buffer, length);default :dev_err (SSP_DEV ,"unknown msg type\n" );return -EPROTO ;
Signature · 1 line
2.34.1
David Lechner
reviewerRe: [PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
Quoted 10 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid allocating a temporary DMA buffer in the interrupt context when handling hub-to-AP and AP-to-hub SPI write messages.
Preallocate RX buffer during probe and reuse it for SPI receive operations. This removes repeated
kzalloc()calls from the IRQ path, reduces allocation overhead, and avoids potential allocation failures under memory pressure.
Quoted 5 lines
The RX buffer size is tracked and allocated using
devm_kzalloc(), ensuring proper lifetime management tied to the device.No functional change intended; this is an internal optimization and robustness improvement.
If we are going to claim this is an optimization, we should have some measurements to back that up.
Quoted 35 lines
Signed-off-by Sanjay Chitroda <[email protected]>---
Changes in v5:
- Rebase change on top of latest v5 patch series.
Changes in v4:
- Use preallocated buffer and stash a buffer that gets reused each time instead of a fresh allocation.
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- prepare series to have all respective cleanup API support for thessp_sensorsfollowing input from Andy Shevchenko
- Link to v2 https://lore.kernel.org/all/[email protected]/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/[email protected]/--- drivers/iio/common/ssp_sensors/ssp.h | 5 +++++ drivers/iio/common/ssp_sensors/ssp_dev.c | 12 ++++++++++++ drivers/iio/common/ssp_sensors/ssp_spi.c | 19 +++---------------- 3 files changed, 20 insertions(+), 16 deletions(-)drivers/iio/common/ssp_sensors/ssp.h +2
-0@@ -175,6 +175,8 @@ struct ssp_sensorhub_info {* @sensor_devs: registered IIO devices table * @enable_refcount: enable reference count for wdt (watchdog timer) * @header_buffer: cache aligned buffer for packet header +* @rx_buf: buffer to receive SPI data +* @rx_buf_size: allocated size of rx_buf */ struct ssp_data {struct spi_device *spi;
drivers/iio/common/ssp_sensors/ssp.h struct ssp_data { · line 229
Quoted 6 lines
drivers/iio/common/ssp_sensors/ssp.h +3
-0@@ -222,6 +224,9 @@ struct ssp_data {atomic_t enable_refcount;__le16 header_buffer[SSP_HEADER_BUFFER_SIZE /sizeof (__le16)]__aligned(IIO_DMA_MINALIGN) ; + +u8 *rx_buf; +size_t rx_buf_size;
No, these can't be after _aligned(IIO_DMA_MINALIGN); without causing problems.
What would work here though is:
u8 rx_buf[SSP_DATA_PACKET_SIZE];
Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp.h +0
-0};void ssp_clean_pending_list (struct ssp_data *data);drivers/iio/common/ssp_sensors/ssp_dev.c +0
-0
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 522
Quoted 7 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +4
-0@@ -516,6 +516,18 @@ static int ssp_probe(struct spi_device *spi) goto err_setup_spi; } + data->rx_buf_size =SSP_DATA_PACKET_SIZE ; + data->rx_buf =devm_kzalloc (&spi->dev, + data->rx_buf_size, +GFP_KERNEL |GFP_DMA );
Since this is a fixed size, we don't need a separate alloc here. We can just embed the array in the data struct.
Quoted 16 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +8
-0+ +if (!data->rx_buf) { +dev_err (&spi->dev, +"Failed to allocate memory for rx_buf\n" ); + ret = -ENOMEM ; + goto err_setup_spi; + } +for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ; data->batch_latency_buf[i] =0 ;drivers/iio/common/ssp_sensors/ssp_spi.c +0
-0
drivers/iio/common/ssp_sensors/ssp_spi.c int ssp_irq_msg(struct ssp_data *data) · line 386
Quoted 9 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1
-5@@ -383,19 +383,13 @@ int ssp_irq_msg(struct ssp_data *data)* but the slave should not send such ones - it is to * check but let's handle this */ - buffer =kmalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) { - ret = -ENOMEM ; - goto _unlock; - } + buffer = data->rx_buf;
I don't think it is helpful to keep the buffer local variable.
Quoted 36 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-11/* got dead packet so it is always an error */ ret =spi_read (data->spi, buffer, length);if (ret >=0 ) ret = -EPROTO ; -kfree (buffer); -dev_err (SSP_DEV ,"No match error %x\n" , msg_options);@@ -428,22 +422,15 @@ int ssp_irq_msg(struct ssp_data *data)mutex_unlock (&data->pending_lock);break ;case SSP_HUB2AP_WRITE : - buffer =kzalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) -return -ENOMEM ; + buffer = data->rx_buf; ret =spi_read (data->spi, buffer, length);if (ret <0 ) {dev_err (SSP_DEV ,"spi read fail\n" ); -kfree (buffer);break ; } - ret =ssp_parse_dataframe (data, buffer, length); - -kfree (buffer); -break ; - +return ssp_parse_dataframe (data, buffer, length);default :dev_err (SSP_DEV ,"unknown msg type\n" );return -EPROTO ;
David Lechner
reviewerRe: [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources
In reply to Sanjay ChitrodaQuoted 88 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Convert mutex initialization and IRQ registration to use devm-managed helpers, tying their lifetime to the device.
This removes the need for explicit cleanup in the error and remove paths, as the resources are automatically released on probe failure or device unbind.
Signed-off-by Sanjay Chitroda <[email protected]>--- drivers/iio/common/ssp_sensors/ssp_dev.c | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-)drivers/iio/common/ssp_sensors/ssp_dev.c +17
-14@@ -510,7 +510,11 @@ static int ssp_probe(struct spi_device *spi) data->spi = spi;spi_set_drvdata (spi, data); -mutex_init (&data->comm_lock); + ret =devm_mutex_init (&spi->dev, &data->comm_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init comm_lock mutex\n" ); + goto err_setup_spi; + }for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ;@@ -523,7 +527,11 @@ static int ssp_probe(struct spi_device *spi) data->time_syncing = true; -mutex_init (&data->pending_lock); + ret =devm_mutex_init (&spi->dev, &data->pending_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init pending_lock mutex\n" ); + goto err_setup_spi; + }INIT_LIST_HEAD (&data->pending_list);atomic_set (&data->enable_refcount,0 );@@ -533,13 +541,13 @@ static int ssp_probe(struct spi_device *spi)timer_setup (&data->wdt_timer, ssp_wdt_timer_func,0 ); - ret =request_threaded_irq (data->spi->irq,NULL , - ssp_irq_thread_fn, -IRQF_TRIGGER_FALLING |IRQF_ONESHOT , -"SSP_Int" , data); + ret =devm_request_threaded_irq (&spi->dev, data->spi->irq,NULL , + ssp_irq_thread_fn, +IRQF_TRIGGER_FALLING |IRQF_ONESHOT , +"SSP_Int" , data);if (ret <0 ) {dev_err (&spi->dev,"Irq request fail\n" ); - goto err_setup_irq; + goto err_setup_spi; }/* Let's start with enabled one so irq balance could be ok */ @@ -553,21 +561,16 @@ static int ssp_probe(struct spi_device *spi) ret =ssp_initialize_mcu (data);if (ret <0 ) {dev_err (&spi->dev,"Initialize_mcu failed\n" ); - goto err_read_reg; + goto err_setup_spi; } }else {dev_err (&spi->dev,"Firmware version not supported\n" ); ret = -EPERM ; - goto err_read_reg; + goto err_setup_spi; }return 0 ; -err_read_reg: -free_irq (data->spi->irq, data); -err_setup_irq: -mutex_destroy (&data->pending_lock); -mutex_destroy (&data->comm_lock); err_setup_spi:mfd_remove_devices (&spi->dev);
drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_remove(struct spi_device *spi) · line 592
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +0
-1@@ -589,14 +592,9 @@ static void ssp_remove(struct spi_device *spi)ssp_clean_pending_list (data); -free_irq (data->spi->irq, data);
Do we need to replace this with an irq_disable() so make sure no more timers/work is scheduled after we delete them below?
If it is safe the way it is, add an explanation to the commit message.
Alternative would be to use devm_add_action_or_reset() to handle all of the other cleanup functions in the correct order still and get rid of the remove function entirely.
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +0
-4-timer_delete_sync (&data->wdt_timer);cancel_work_sync (&data->work_wdt); -mutex_destroy (&data->comm_lock); -mutex_destroy (&data->pending_lock); -mfd_remove_devices (&spi->dev); }
Re: [PATCH v5 1/5] iio: ssp_sensors: cleanup codestyle warning
In reply to Sanjay ChitrodaQuoted 7 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.cWARNING: Prefer
__packedover __attribute__((__packed__)) +} __attribute__((__packed__));
Do we need __packed to be there in the first place? Isn't it naturally aligned?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 3/5] iio: ssp_sensors: factor out pending list add/remove helpers
In reply to Sanjay ChitrodaQuoted 8 lines · Sanjay Chitroda
The SSP SPI transfer path manipulates the pending message list in multiple places, each time open-coding the same locking and list operations.
Re-factor the pending list add and delete logic into small helper functions to avoid duplication and simplify transfer flow to follow.
No functional change intended.
Suggested-by?
...
Review
Quoted 2 lines
(quoted diff) +2
-0+static inline void ssp_pending_add(struct ssp_data *data, + struct ssp_msg *msg)
Make it rather:
static inline void ssp_pending_add(struct ssp_data *data, struct ssp_msg *msg)
{
if (msg->length)
return;
...
}drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_check_lines(struct ssp_data *data, bool state) · line 191
Quoted 13 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +13
-0+{ +mutex_lock (&data->pending_lock); +list_add_tail (&msg->list, &data->pending_list); +mutex_unlock (&data->pending_lock); +} + +static inline void ssp_pending_del (struct ssp_data *data, +struct ssp_msg *msg) +{ +mutex_lock (&data->pending_lock); +list_del (&msg->list); +mutex_unlock (&data->pending_lock); +}
(in the same manner)
...
drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg, · line 222
Quoted 7 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-5-if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_add_tail (&msg->list, &data->pending_list); -mutex_unlock (&data->pending_lock); - } +if (!use_no_irq) +ssp_pending_add (data, msg);
...and then it will become
ssp_pending_add(data, msg);
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-5status =ssp_check_lines (data, true);if (status <0 ) { -if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); - } +if (!use_no_irq) +ssp_pending_del (data, msg); goto _error_locked; }
...
drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg, · line 240
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1
-3if (wait_for_completion_timeout (done,msecs_to_jiffies (timeout)) ==0 ) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); +ssp_pending_del (data, msg); data->timeout_cnt++;return -ETIMEDOUT ;
Also rewrite this to follow the style (missing {}, better indentation):
if (msg->length && done &&
!wait_for_completion_timeout(done, msecs_to_jiffies(timeout))) {
ssp_pending_del(data, msg);
data->timeout_cnt++;
return -ETIMEDOUT;
}And kill that use_no_urq altogether. Move the comment rather to a helper(s).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources
In reply to Sanjay ChitrodaQuoted 6 lines · Sanjay Chitroda
Convert mutex initialization and IRQ registration to use devm-managed helpers, tying their lifetime to the device.
This removes the need for explicit cleanup in the error and remove paths, as the resources are automatically released on probe failure or device unbind.
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 515
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +3
-1-mutex_init (&data->comm_lock); + ret =devm_mutex_init (&spi->dev, &data->comm_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init comm_lock mutex\n" );
The message is not needed here.
Review
Quoted 1 line
(quoted diff) +1
-0+ goto err_setup_spi;
This is half-baked solution. When devm is in use there is shouldn't be goto. after it.
Quoted 1 line
(quoted diff) +1
-0+ }
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi)
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +4
-4- ret =request_threaded_irq (data->spi->irq,NULL , - ssp_irq_thread_fn, -IRQF_TRIGGER_FALLING |IRQF_ONESHOT , -"SSP_Int" , data); + ret =devm_request_threaded_irq (&spi->dev, data->spi->irq,NULL , + ssp_irq_thread_fn, +IRQF_TRIGGER_FALLING |IRQF_ONESHOT , +"SSP_Int" , data);if (ret <0 ) {dev_err (&spi->dev,"Irq request fail\n" );
Now you should also remove this dup message. devm APIs in IRQ core print a message for you.
Quoted 3 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +1
-1- goto err_setup_irq; + goto err_setup_spi; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 1/5] iio: ssp_sensors: cleanup codestyle warning
In reply to Andy ShevchenkoQuoted 11 lines · Andy Shevchenko
Quoted 7 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.cWARNING: Prefer
__packedover __attribute__((__packed__)) +} __attribute__((__packed__));Do we need
__packedto be there in the first place? Isn't it naturally aligned?
This structure isn't naturally aligned as in header structure u8 is followed by __le16 and __le32 fields. Without packing, compiler inserts padding to satisfy alignment requirement.
Andy Shevchenko
Reviewed-byRe: [PATCH v5 1/5] iio: ssp_sensors: cleanup codestyle warning
Quoted 6 lines · Sanjay Chitroda
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Sanjay Chitroda
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.c
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
WARNING: Prefer
__packedover __attribute__((__packed__)) +} __attribute__((__packed__));Do we need
__packedto be there in the first place? Isn't it naturally aligned?This structure isn't naturally aligned as in header structure u8 is followed by
__le16and__le32fields. Without packing, compiler inserts padding to satisfy alignment requirement.
Thank you for confirming this.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 3/5] iio: ssp_sensors: factor out pending list add/remove helpers
In reply to Andy ShevchenkoQuoted 6 lines · Andy Shevchenko
Quoted 4 lines · Sanjay Chitroda
The SSP SPI transfer path manipulates the pending message list in multiple places, each time open-coding the same locking and list operations.
Quoted 7 lines
Quoted 4 lines
Re-factor the pending list add and delete logic into small helper functions to avoid duplication and simplify transfer flow to follow.
No functional change intended.
Suggested-by?
Requesting your input on which email is recommended to tag with your name.
Shall I use intel.com ?
Quoted 7 lines
...
Review
Quoted 2 lines
(quoted diff) +2
-0+static inline void ssp_pending_add(struct ssp_data *data, + struct ssp_msg *msg)Make it rather:
Quoted 6 lines
static inline void
ssp_pending_add(struct ssp_data *data, struct ssp_msg *msg){ if (msg->length) return; ... }
It should be not, right ?
{
if (!msg->length)
return;
...
}
const bool use_no_irq = msg->length == 0;
if (!use_no_irq) {add/del}
If message length is 0 then no interrupt
Quoted 66 lines
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +3
-0+{ +mutex_lock (&data->pending_lock); +list_add_tail (&msg->list, &data->pending_list);drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_check_lines(struct ssp_data *data, bool state) · line 191
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +10
-0+mutex_unlock (&data->pending_lock); +} + +static inline void ssp_pending_del (struct ssp_data *data, +struct ssp_msg *msg) +{ +mutex_lock (&data->pending_lock); +list_del (&msg->list); +mutex_unlock (&data->pending_lock); +}(in the same manner)
...
drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg, · line 222
Quoted 7 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-5-if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_add_tail (&msg->list, &data->pending_list); -mutex_unlock (&data->pending_lock); - } +if (!use_no_irq) +ssp_pending_add (data, msg);...and then it will become
ssp_pending_add(data, msg);Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-5status =ssp_check_lines (data, true);if (status <0 ) { -if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); - } +if (!use_no_irq) +ssp_pending_del (data, msg); goto _error_locked; }...
drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg, · line 240
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1
-3if (wait_for_completion_timeout (done,msecs_to_jiffies (timeout)) ==0 ) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); +ssp_pending_del (data, msg); data->timeout_cnt++;return -ETIMEDOUT ;Also rewrite this to follow the style (missing {}, better indentation):
if (msg->length && done && !wait_for_completion_timeout(done, msecs_to_jiffies(timeout))) { ssp_pending_del(data, msg); data->timeout_cnt++; return -ETIMEDOUT; }
Quoted 1 line
And kill that
use_no_urqaltogether. Move the comment rather to a helper(s).
Understood, will follow and drop this variable in next series.
Quoted 1 line
Re: [PATCH v5 3/5] iio: ssp_sensors: factor out pending list add/remove helpers
In reply to a message not in the archiveQuoted 2 lines · Sanjay Embedded_SE
Quoted 1 line · Andy Shevchenko
On Mon, Apr 06, 2026 at 01:38:50PM +0530, Sanjay Chitroda wrote:
...
Quoted 5 lines
Quoted 1 line
Suggested-by?
Requesting your input on which email is recommended to tag with your name.
Shall I use intel.com ?
The one that was used in the message which suggested this change. I don't remember which one I have used.
...
Quoted 5 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+static inline void ssp_pending_add(struct ssp_data *data, + struct ssp_msg *msg)Make it rather:
Quoted 8 lines
Quoted 6 lines
static inline void
ssp_pending_add(struct ssp_data *data, struct ssp_msg *msg){ if (msg->length) return; ... }It should be not, right ?
Yes, too many negations, perhaps even better to spell it as msg->length == 0.
Quoted 10 lines
{
if (!msg->length)return;...
}const bool
use_no_irq=msg->length== 0;
if (!use_no_irq) {add/del}If message length is 0 then no interrupt
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources
In reply to Andy ShevchenkoQuoted 18 lines · Andy Shevchenko
Quoted 6 lines · Sanjay Chitroda
Convert mutex initialization and IRQ registration to use devm-managed helpers, tying their lifetime to the device.
This removes the need for explicit cleanup in the error and remove paths, as the resources are automatically released on probe failure or device unbind.
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 515
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +3
-1-mutex_init (&data->comm_lock); + ret =devm_mutex_init (&spi->dev, &data->comm_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init comm_lock mutex\n" );The message is not needed here.
Quoted 7 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ goto err_setup_spi;This is half-baked solution. When devm is in use there is shouldn't be goto. after it.
Review
Quoted 1 line
(quoted diff) +1
-0+ }
Hi Andy,
Thank you for your input.
I will replace pending goto and drop newly added redundant error message and send next series with change.
Quoted 21 lines
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi)
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +4
-4- ret =request_threaded_irq (data->spi->irq,NULL , - ssp_irq_thread_fn, -IRQF_TRIGGER_FALLING |IRQF_ONESHOT , -"SSP_Int" , data); + ret =devm_request_threaded_irq (&spi->dev, data->spi->irq,NULL , + ssp_irq_thread_fn, +IRQF_TRIGGER_FALLING |IRQF_ONESHOT , +"SSP_Int" , data);if (ret <0 ) {dev_err (&spi->dev,"Irq request fail\n" );Now you should also remove this dup message. devm APIs in IRQ core print a message for you.
Quoted 3 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +1
-1- goto err_setup_irq; + goto err_setup_spi; }
Re: [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources
In reply to David LechnerQuoted 97 lines · David Lechner
Quoted 88 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Convert mutex initialization and IRQ registration to use devm-managed helpers, tying their lifetime to the device.
This removes the need for explicit cleanup in the error and remove paths, as the resources are automatically released on probe failure or device unbind.
Signed-off-by Sanjay Chitroda <[email protected]>--- drivers/iio/common/ssp_sensors/ssp_dev.c | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-)drivers/iio/common/ssp_sensors/ssp_dev.c +17
-14@@ -510,7 +510,11 @@ static int ssp_probe(struct spi_device *spi) data->spi = spi;spi_set_drvdata (spi, data); -mutex_init (&data->comm_lock); + ret =devm_mutex_init (&spi->dev, &data->comm_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init comm_lock mutex\n" ); + goto err_setup_spi; + }for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ;@@ -523,7 +527,11 @@ static int ssp_probe(struct spi_device *spi) data->time_syncing = true; -mutex_init (&data->pending_lock); + ret =devm_mutex_init (&spi->dev, &data->pending_lock); +if (ret <0 ) { +dev_err (&spi->dev,"Failed to init pending_lock mutex\n" ); + goto err_setup_spi; + }INIT_LIST_HEAD (&data->pending_list);atomic_set (&data->enable_refcount,0 );@@ -533,13 +541,13 @@ static int ssp_probe(struct spi_device *spi)timer_setup (&data->wdt_timer, ssp_wdt_timer_func,0 ); - ret =request_threaded_irq (data->spi->irq,NULL , - ssp_irq_thread_fn, -IRQF_TRIGGER_FALLING |IRQF_ONESHOT , -"SSP_Int" , data); + ret =devm_request_threaded_irq (&spi->dev, data->spi->irq,NULL , + ssp_irq_thread_fn, +IRQF_TRIGGER_FALLING |IRQF_ONESHOT , +"SSP_Int" , data);if (ret <0 ) {dev_err (&spi->dev,"Irq request fail\n" ); - goto err_setup_irq; + goto err_setup_spi; }/* Let's start with enabled one so irq balance could be ok */ @@ -553,21 +561,16 @@ static int ssp_probe(struct spi_device *spi) ret =ssp_initialize_mcu (data);if (ret <0 ) {dev_err (&spi->dev,"Initialize_mcu failed\n" ); - goto err_read_reg; + goto err_setup_spi; } }else {dev_err (&spi->dev,"Firmware version not supported\n" ); ret = -EPERM ; - goto err_read_reg; + goto err_setup_spi; }return 0 ; -err_read_reg: -free_irq (data->spi->irq, data); -err_setup_irq: -mutex_destroy (&data->pending_lock); -mutex_destroy (&data->comm_lock); err_setup_spi:mfd_remove_devices (&spi->dev);drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_remove(struct spi_device *spi) · line 592
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +0
-1@@ -589,14 +592,9 @@ static void ssp_remove(struct spi_device *spi)ssp_clean_pending_list (data); -free_irq (data->spi->irq, data);Do we need to replace this with an
irq_disable()so make sure no more timers/work is scheduled after we delete them below?
Quoted 6 lines
If it is safe the way it is, add an explanation to the commit message.
Alternative would be to use
devm_add_action_or_reset()to handle all of the other cleanup functions in the correct order still and get rid of the remove function entirely.
Hi David,
Thank you for the input and feedback.
Understood, will check and accordingly plan to update commit message or have devm_add_action_or_reset().
In my opinion usage of devm API to drop remove() would be better one.
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_remove(struct spi_device *spi) · line 600
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +0
-4-timer_delete_sync (&data->wdt_timer);cancel_work_sync (&data->work_wdt); -mutex_destroy (&data->comm_lock); -mutex_destroy (&data->pending_lock); -mfd_remove_devices (&spi->dev); }
Also, I observed that in remove() path, there is duplicate of following with ssp_disable_wdt_timer():
static void ssp_remove(struct spi_device *spi)
{
...
ssp_disable_wdt_timer(data);
....
timer_delete_sync(&data->wdt_timer);
cancel_work_sync(&data->work_wdt);
...
}static void ssp_disable_wdt_timer(data)(struct spi_device *spi)
{
timer_delete_sync(&data->wdt_timer);
cancel_work_sync(&data->work_wdt);
}At first glance looks redundant, do we require both ?
Quoted 1 line
Re: [PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
In reply to David LechnerQuoted 11 lines · David Lechner
Quoted 10 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid allocating a temporary DMA buffer in the interrupt context when handling hub-to-AP and AP-to-hub SPI write messages.
Preallocate RX buffer during probe and reuse it for SPI receive operations. This removes repeated
kzalloc()calls from the IRQ path, reduces allocation overhead, and avoids potential allocation failures under memory pressure.
Quoted 9 lines
Quoted 5 lines
The RX buffer size is tracked and allocated using
devm_kzalloc(), ensuring proper lifetime management tied to the device.No functional change intended; this is an internal optimization and robustness improvement.
If we are going to claim this is an optimization, we should have some measurements to back that up.
Hi David,
Thank you for your inputs and feedback.
Quoted 44 lines
Quoted 35 lines
Signed-off-by Sanjay Chitroda <[email protected]>---
Changes in v5:
- Rebase change on top of latest v5 patch series.
Changes in v4:
- Use preallocated buffer and stash a buffer that gets reused each time instead of a fresh allocation.
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- prepare series to have all respective cleanup API support for thessp_sensorsfollowing input from Andy Shevchenko
- Link to v2 https://lore.kernel.org/all/[email protected]/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/[email protected]/--- drivers/iio/common/ssp_sensors/ssp.h | 5 +++++ drivers/iio/common/ssp_sensors/ssp_dev.c | 12 ++++++++++++ drivers/iio/common/ssp_sensors/ssp_spi.c | 19 +++---------------- 3 files changed, 20 insertions(+), 16 deletions(-)drivers/iio/common/ssp_sensors/ssp.h +2
-0@@ -175,6 +175,8 @@ struct ssp_sensorhub_info {* @sensor_devs: registered IIO devices table * @enable_refcount: enable reference count for wdt (watchdog timer) * @header_buffer: cache aligned buffer for packet header +* @rx_buf: buffer to receive SPI data +* @rx_buf_size: allocated size of rx_buf */ struct ssp_data {struct spi_device *spi;drivers/iio/common/ssp_sensors/ssp.h struct ssp_data { · line 229
Quoted 6 lines
drivers/iio/common/ssp_sensors/ssp.h +3
-0@@ -222,6 +224,9 @@ struct ssp_data {atomic_t enable_refcount;__le16 header_buffer[SSP_HEADER_BUFFER_SIZE /sizeof (__le16)]__aligned(IIO_DMA_MINALIGN) ; + +u8 *rx_buf; +size_t rx_buf_size;No, these can't be after
_aligned(IIO_DMA_MINALIGN); without causing problems.
Quoted 4 lines
What would work here though is:
u8 rx_buf[SSP_DATA_PACKET_SIZE];
Okay, packet size is 960 bytes; to avoid using static memory i have used dynamic memory with devm API.
However following your input I will update change in next series with static allocation.
Quoted 11 lines
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp.h +0
-0};void ssp_clean_pending_list (struct ssp_data *data);drivers/iio/common/ssp_sensors/ssp_dev.c +0
-0@@ -516,6 +516,18 @@ static int ssp_probe(struct spi_device *spi) goto err_setup_spi; }
Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 522
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +4
-0+ data->rx_buf_size =SSP_DATA_PACKET_SIZE ; + data->rx_buf =devm_kzalloc (&spi->dev, + data->rx_buf_size, +GFP_KERNEL |GFP_DMA );Since this is a fixed size, we don't need a separate alloc here. We can just embed the array in the data struct.
Yes, understood.
Quoted 20 lines
Quoted 20 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +8
-0+ +if (!data->rx_buf) { +dev_err (&spi->dev, +"Failed to allocate memory for rx_buf\n" ); + ret = -ENOMEM ; + goto err_setup_spi; + } +for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ; data->batch_latency_buf[i] =0 ;drivers/iio/common/ssp_sensors/ssp_spi.c +0
-1@@ -383,19 +383,13 @@ int ssp_irq_msg(struct ssp_data *data)* but the slave should not send such ones - it is to * check but let's handle this */ - buffer =kmalloc (length,GFP_KERNEL |GFP_DMA );
Quoted 9 lines
drivers/iio/common/ssp_sensors/ssp_spi.c int ssp_irq_msg(struct ssp_data *data) · line 386
Quoted 5 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1
-4-if (!buffer) { - ret = -ENOMEM ; - goto _unlock; - } + buffer = data->rx_buf;I don't think it is helpful to keep the buffer local variable.
I will keep buffer at top of ssp_irq_msg() instead of local to specific switch case.
Quoted 37 lines
Quoted 36 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-11/* got dead packet so it is always an error */ ret =spi_read (data->spi, buffer, length);if (ret >=0 ) ret = -EPROTO ; -kfree (buffer); -dev_err (SSP_DEV ,"No match error %x\n" , msg_options);@@ -428,22 +422,15 @@ int ssp_irq_msg(struct ssp_data *data)mutex_unlock (&data->pending_lock);break ;case SSP_HUB2AP_WRITE : - buffer =kzalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) -return -ENOMEM ; + buffer = data->rx_buf; ret =spi_read (data->spi, buffer, length);if (ret <0 ) {dev_err (SSP_DEV ,"spi read fail\n" ); -kfree (buffer);break ; } - ret =ssp_parse_dataframe (data, buffer, length); - -kfree (buffer); -break ; - +return ssp_parse_dataframe (data, buffer, length);default :dev_err (SSP_DEV ,"unknown msg type\n" );return -EPROTO ;
Re: [PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
In reply to David LechnerQuoted 57 lines · David Lechner
Quoted 10 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid allocating a temporary DMA buffer in the interrupt context when handling hub-to-AP and AP-to-hub SPI write messages.
Preallocate RX buffer during probe and reuse it for SPI receive operations. This removes repeated
kzalloc()calls from the IRQ path, reduces allocation overhead, and avoids potential allocation failures under memory pressure.Quoted 5 lines
The RX buffer size is tracked and allocated using
devm_kzalloc(), ensuring proper lifetime management tied to the device.No functional change intended; this is an internal optimization and robustness improvement.
If we are going to claim this is an optimization, we should have some measurements to back that up.
Quoted 37 lines
Signed-off-by Sanjay Chitroda <[email protected]>---
Changes in v5:
- Rebase change on top of latest v5 patch series.
Changes in v4:
- Use preallocated buffer and stash a buffer that gets reused each time instead of a fresh allocation.
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- prepare series to have all respective cleanup API support for thessp_sensorsfollowing input from Andy Shevchenko
- Link to v2 https://lore.kernel.org/all/[email protected]/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/[email protected]/--- drivers/iio/common/ssp_sensors/ssp.h | 5 +++++ drivers/iio/common/ssp_sensors/ssp_dev.c | 12 ++++++++++++ drivers/iio/common/ssp_sensors/ssp_spi.c | 19 +++---------------- 3 files changed, 20 insertions(+), 16 deletions(-)drivers/iio/common/ssp_sensors/ssp.h +2
-0@@ -175,6 +175,8 @@ struct ssp_sensorhub_info {* @sensor_devs: registered IIO devices table * @enable_refcount: enable reference count for wdt (watchdog timer) * @header_buffer: cache aligned buffer for packet header +* @rx_buf: buffer to receive SPI data +* @rx_buf_size: allocated size of rx_buf */ struct ssp_data {struct spi_device *spi;@@ -222,6 +224,9 @@ struct ssp_data {atomic_t enable_refcount;
Quoted 7 lines
drivers/iio/common/ssp_sensors/ssp.h struct ssp_data { · line 229
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp.h +3
-0__le16 header_buffer[SSP_HEADER_BUFFER_SIZE /sizeof (__le16)]__aligned(IIO_DMA_MINALIGN) ; + +u8 *rx_buf; +size_t rx_buf_size;No, these can't be after
_aligned(IIO_DMA_MINALIGN); without causing problems.
I misunderstood the __aligned() constraints.
I have reworked the struct so the DMA aligned buffer is the last member and replaced pointer with fixed size buffer as suggested.
Quoted 89 lines
What would work here though is:
u8 rx_buf[SSP_DATA_PACKET_SIZE];Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp.h +0
-0};void ssp_clean_pending_list (struct ssp_data *data);drivers/iio/common/ssp_sensors/ssp_dev.c +0
-0drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 522
Quoted 7 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +4
-0@@ -516,6 +516,18 @@ static int ssp_probe(struct spi_device *spi) goto err_setup_spi; } + data->rx_buf_size =SSP_DATA_PACKET_SIZE ; + data->rx_buf =devm_kzalloc (&spi->dev, + data->rx_buf_size, +GFP_KERNEL |GFP_DMA );Since this is a fixed size, we don't need a separate alloc here. We can just embed the array in the data struct.
Quoted 16 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +8
-0+ +if (!data->rx_buf) { +dev_err (&spi->dev, +"Failed to allocate memory for rx_buf\n" ); + ret = -ENOMEM ; + goto err_setup_spi; + } +for (i =0 ; i <SSP_SENSOR_MAX ; ++i) { data->delay_buf[i] =SSP_DEFAULT_POLLING_DELAY ; data->batch_latency_buf[i] =0 ;drivers/iio/common/ssp_sensors/ssp_spi.c +0
-0drivers/iio/common/ssp_sensors/ssp_spi.c int ssp_irq_msg(struct ssp_data *data) · line 386
Quoted 9 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1
-5@@ -383,19 +383,13 @@ int ssp_irq_msg(struct ssp_data *data)* but the slave should not send such ones - it is to * check but let's handle this */ - buffer =kmalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) { - ret = -ENOMEM ; - goto _unlock; - } + buffer = data->rx_buf;I don't think it is helpful to keep the buffer local variable.
Quoted 36 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-11/* got dead packet so it is always an error */ ret =spi_read (data->spi, buffer, length);if (ret >=0 ) ret = -EPROTO ; -kfree (buffer); -dev_err (SSP_DEV ,"No match error %x\n" , msg_options);@@ -428,22 +422,15 @@ int ssp_irq_msg(struct ssp_data *data)mutex_unlock (&data->pending_lock);break ;case SSP_HUB2AP_WRITE : - buffer =kzalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) -return -ENOMEM ; + buffer = data->rx_buf; ret =spi_read (data->spi, buffer, length);if (ret <0 ) {dev_err (SSP_DEV ,"spi read fail\n" ); -kfree (buffer);break ; } - ret =ssp_parse_dataframe (data, buffer, length); - -kfree (buffer); -break ; - +return ssp_parse_dataframe (data, buffer, length);default :dev_err (SSP_DEV ,"unknown msg type\n" );return -EPROTO ;
David Lechner
reviewerRe: [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources
In reply to Sanjay ChitrodaQuoted 17 lines · Sanjay Chitroda
Also, I observed that in
remove()path, there is duplicate of following withssp_disable_wdt_timer():static void
ssp_remove(struct spi_device *spi){ ... ssp_disable_wdt_timer(data); .... timer_delete_sync(&data->wdt_timer); cancel_work_sync(&data->work_wdt); ... }
Quoted 7 lines
static void ssp_disable_wdt_timer(data)(struct
spi_device*spi){ timer_delete_sync(&data->wdt_timer); cancel_work_sync(&data->work_wdt); }At first glance looks redundant, do we require both ?
No, this sounds like a double-free bug.
Re: [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources
Quoted 24 lines · David Lechner
Quoted 23 lines · Sanjay Chitroda
Also, I observed that in
remove()path, there is duplicate of following withssp_disable_wdt_timer():static void
ssp_remove(struct spi_device *spi){ ... ssp_disable_wdt_timer(data); .... timer_delete_sync(&data->wdt_timer); cancel_work_sync(&data->work_wdt); ... }static void ssp_disable_wdt_timer(data)(struct
spi_device*spi){ timer_delete_sync(&data->wdt_timer); cancel_work_sync(&data->work_wdt); }
Quoted 4 lines
Quoted 1 line
At first glance looks redundant, do we require both ?
No, this sounds like a double-free bug.
Hi David,
Thank you for your input.
I have created a change to drop duplicated wdt timer and work cleanup. Will send changes in next series in few days.