Skip to contentSkip to conversation

iio: ssp_sensors: simplify cleanup using __free

PATCH In review Sanjay Chitroda 3 participants 3 messages Mar 11 – Mar 15 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: ssp_sensors: simplify cleanup using __free

From: Sanjay Chitroda <[email protected]>

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().

---
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_spi.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +3 -11
index 6c81c0385fb5..4320061475e0 100644
@@ -331,7 +331,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;
@@ -375,7 +374,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) {
 				ret = -ENOMEM;
 				goto _unlock;
@@ -386,8 +385,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);
 
@@ -420,22 +417,17 @@ 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);
+		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;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Sanjay Chitroda

From: Sanjay Chitroda <[email protected]>

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

Quoted 7 lines

No functional change intended for kmalloc().

---
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/[email protected]/

Yeah, but what about other comments?

First of all, consider using guard()() and friends as a prerequisite to this change. Second, look at the code for the absence of goto:s out of the scoped blocks. Third, does this driver use some buffer in the existing structure like ST drivers? Perhaps the same approach can be applied?

--
With Best Regards,
Andy Shevchenko

maintainer
#2 lore raw
In reply to Sanjay Chitroda
Quoted 9 lines · On Wed, 11 Mar 2026 23:11:51 +0530 Sanjay Chitroda…

From: Sanjay Chitroda <[email protected]>

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().

I think it is worth noting this is an ancient driver. I don't mind clear improvements but I would suggest perhaps looking for more recent code to see if this sort of cleanup applies.

Jonathan

Quoted 22 lines

---
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_spi.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +0 -1
index 6c81c0385fb5..4320061475e0 100644
@@ -331,7 +331,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;

drivers/iio/common/ssp_sensors/ssp_spi.c int ssp_irq_msg(struct ssp_data *data) · line 380

Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1 -1
@@ -375,7 +374,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) {
 				ret = -ENOMEM;
 				goto _unlock;

Read the documentation in cleanup.h and make sure you follow it.

Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +0 -2
@@ -386,8 +385,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);
 

drivers/iio/common/ssp_sensors/ssp_spi.c int ssp_irq_msg(struct ssp_data *data) · line 420

Quoted 5 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1 -1
@@ -420,22 +417,17 @@ 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);
+		char *buffer __free(kfree) = kzalloc(length, GFP_KERNEL | GFP_DMA);

What scope is this cleaning up at the end of?

Quoted 19 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +1 -6
 		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;