Skip to contentSkip to conversation

iio: adc: ad_sigma_delta: use __free(kfree) for temporary buffers

PATCH In review Adi Kadušić 5 participants 5 messages Nov 8 – Nov 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: adc: ad_sigma_delta: use __free(kfree) for temporary buffers

Replace manual kfree() calls with the __free(kfree) cleanup attribute. No functional change.

---
Changes in v2:
- Keep reset_length/size declarations in ad_sd_reset() as they were, only declare buf with __free(kfree) at its allocation (Andy)

v1: https://lore.kernel.org/linux-iio/[email protected]/T/#u

 drivers/iio/adc/ad_sigma_delta.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/iio/adc/ad_sigma_delta.c +4 -13
index 119ddc5a13e2..934c9965c69b 100644
@@ -205,19 +205,15 @@ int ad_sd_reset(struct ad_sigma_delta *sigma_delta)
 {
 	unsigned int reset_length = sigma_delta->info->num_resetclks;
 	unsigned int size;
-	u8 *buf;
-	int ret;
 
 	size = BITS_TO_BYTES(reset_length);
-	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
+	u8 *buf __free(kfree) = kcalloc(size, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
 	memset(buf, 0xff, size);
-	ret = spi_write(sigma_delta->spi, buf, size);
-	kfree(buf);
 
-	return ret;
+	return spi_write(sigma_delta->spi, buf, size);
 }
 EXPORT_SYMBOL_NS_GPL(ad_sd_reset, "IIO_AD_SIGMA_DELTA");
 
@@ -249,7 +245,6 @@ static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta
 {
 	bool pending_event;
 	unsigned int data_read_len = BITS_TO_BYTES(sigma_delta->info->num_resetclks);
-	u8 *data;
 	struct spi_transfer t[] = {
 		{
 			.len = 1,
@@ -317,7 +312,7 @@ static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta
 	 * shorter than the reset procedure.
 	 */
 
-	data = kzalloc(data_read_len + 1, GFP_KERNEL);
+	u8 *data __free(kfree) = kzalloc(data_read_len + 1, GFP_KERNEL);
 	if (!data)
 		return -ENOMEM;
 
@@ -340,11 +335,7 @@ static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta
 	memset(data + 2, 0xff, data_read_len - 1);
 	spi_message_add_tail(&t[1], &m);
 
-	ret = spi_sync_locked(sigma_delta->spi, &m);
-
-	kfree(data);
-
-	return ret;
+	return spi_sync_locked(sigma_delta->spi, &m);
 }
 
 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 2 lines · On Sun, 8 Nov 2026 21:15:22 +0100 Adi Kadušić <[email protected]> wrote:

Replace manual kfree() calls with the __free(kfree) cleanup attribute. No functional change.

I think that this is a functional change :)
This can be dropped while applying though.

Signature · 2 lines
Kind regards,
Joshua Crofts
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Adi Kadušić
Quoted 5 lines · Adi Kadušić

Replace manual kfree() calls with the __free(kfree) cleanup attribute. No functional change.

---

Yeps, like the change

Quoted 68 lines

Changes in v2:
- Keep reset_length/size declarations in ad_sd_reset() as they were, only declare buf with __free(kfree) at its allocation (Andy)

v1: https://lore.kernel.org/linux-iio/[email protected]/T/#u

 drivers/iio/adc/ad_sigma_delta.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/iio/adc/ad_sigma_delta.c +4 -13
index 119ddc5a13e2..934c9965c69b 100644
@@ -205,19 +205,15 @@ int ad_sd_reset(struct ad_sigma_delta *sigma_delta)
 {
 	unsigned int reset_length = sigma_delta->info->num_resetclks;
 	unsigned int size;
-	u8 *buf;
-	int ret;
 
 	size = BITS_TO_BYTES(reset_length);
-	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
+	u8 *buf __free(kfree) = kcalloc(size, sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
 	memset(buf, 0xff, size);
-	ret = spi_write(sigma_delta->spi, buf, size);
-	kfree(buf);
 
-	return ret;
+	return spi_write(sigma_delta->spi, buf, size);
 }
 EXPORT_SYMBOL_NS_GPL(ad_sd_reset, "IIO_AD_SIGMA_DELTA");
 
@@ -249,7 +245,6 @@ static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta
 {
 	bool pending_event;
 	unsigned int data_read_len = BITS_TO_BYTES(sigma_delta->info->num_resetclks);
-	u8 *data;
 	struct spi_transfer t[] = {
 		{
 			.len = 1,
@@ -317,7 +312,7 @@ static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta
 	 * shorter than the reset procedure.
 	 */
 
-	data = kzalloc(data_read_len + 1, GFP_KERNEL);
+	u8 *data __free(kfree) = kzalloc(data_read_len + 1, GFP_KERNEL);
 	if (!data)
 		return -ENOMEM;
 
@@ -340,11 +335,7 @@ static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta
 	memset(data + 2, 0xff, data_read_len - 1);
 	spi_message_add_tail(&t[1], &m);
 
-	ret = spi_sync_locked(sigma_delta->spi, &m);
-
-	kfree(data);
-
-	return ret;
+	return spi_sync_locked(sigma_delta->spi, &m);
 }
 
 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
Signature · 1 line
2.43.0
#3 lore raw
In reply to Adi Kadušić
Quoted 2 lines · Adi Kadušić

Replace manual kfree() calls with the __free(kfree) cleanup attribute. No functional change.

...

drivers/iio/adc/ad_sigma_delta.c static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta · line 315

Quoted 2 lines
drivers/iio/adc/ad_sigma_delta.c +1 -1
-	data = kzalloc(data_read_len + 1, GFP_KERNEL);
+	u8 *data __free(kfree) = kzalloc(data_read_len + 1, GFP_KERNEL);

Just a side note.
Would be good at some point use something from overflow.h for this.

Quoted 2 lines
drivers/iio/adc/ad_sigma_delta.c +0 -0
 	if (!data)
 		return -ENOMEM;
Signature · 2 lines
With Best Regards,
Andy Shevchenko