[PATCH v3 2/3] iio: ssp_sensors: simplify cleanup using __free

Sanjay Chitroda
2026-03-15T12:55:21Z

From: Sanjay Chitroda <sanjayembeddedse@gmail.com>

Replace manual cleanup logic with __free attribute from cleanup.h. This
removes explicit kfree() calls and simplifies the error handling paths.

No functional change intended for kmalloc().

Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
---
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/20260311174151.3441429-1-sanjayembedded@gmail.com/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/20260310200513.2162018-3-sanjayembedded@gmail.com/
---
drivers/iio/common/ssp_sensors/ssp_spi.c | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/drivers/iio/common/ssp_sensors/ssp_spi.c b/drivers/iio/common/ssp_sensors/ssp_spi.c
index 87a38002b218..9df94bd02e3a 100644
--- a/drivers/iio/common/ssp_sensors/ssp_spi.c
+++ b/drivers/iio/common/ssp_sensors/ssp_spi.c
@@ -326,7 +326,6 @@ static int ssp_parse_dataframe(struct ssp_data *data, char *dataframe, int len)
/* threaded irq */
int ssp_irq_msg(struct ssp_data *data)
{
- char *buffer;
u8 msg_type;
int ret;
u16 length, msg_options;
@@ -370,7 +369,7 @@ 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);
+ char *buffer __free(kfree) = kmalloc(length, GFP_KERNEL | GFP_DMA);
if (!buffer)
return -ENOMEM;

@@ -379,8 +378,6 @@ int ssp_irq_msg(struct ssp_data *data)
if (ret >= 0)
ret = -EPROTO;

- kfree(buffer);
-
dev_err(SSP_DEV, "No match error %x\n",
msg_options);

@@ -411,22 +408,17 @@ int ssp_irq_msg(struct ssp_data *data)
complete(msg->done);
break;
case SSP_HUB2AP_WRITE:
- buffer = kzalloc(length, GFP_KERNEL | GFP_DMA);
+ char *buffer __free(kfree) = kzalloc(length, GFP_KERNEL | GFP_DMA);
if (!buffer)
return -ENOMEM;

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;
--
2.34.1