Skip to content

Commit 4194783

Browse files
krzklag-linaro
authored andcommitted
mfd: mt6360: Use scoped variables with memory allocators to simplify error paths
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler. Signed-off-by: Krzysztof Kozlowski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent 691277c commit 4194783

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

drivers/mfd/mt6360-core.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Author: Gene Chen <[email protected]>
66
*/
77

8+
#include <linux/cleanup.h>
89
#include <linux/crc8.h>
910
#include <linux/i2c.h>
1011
#include <linux/init.h>
@@ -404,7 +405,6 @@ static int mt6360_regmap_read(void *context, const void *reg, size_t reg_size,
404405
u8 reg_addr = *(u8 *)(reg + 1);
405406
struct i2c_client *i2c;
406407
bool crc_needed = false;
407-
u8 *buf;
408408
int buf_len = MT6360_ALLOC_READ_SIZE(val_size);
409409
int read_size = val_size;
410410
u8 crc;
@@ -423,7 +423,7 @@ static int mt6360_regmap_read(void *context, const void *reg, size_t reg_size,
423423
read_size += MT6360_CRC_CRC8_SIZE;
424424
}
425425

426-
buf = kzalloc(buf_len, GFP_KERNEL);
426+
u8 *buf __free(kfree) = kzalloc(buf_len, GFP_KERNEL);
427427
if (!buf)
428428
return -ENOMEM;
429429

@@ -433,24 +433,19 @@ static int mt6360_regmap_read(void *context, const void *reg, size_t reg_size,
433433
ret = i2c_smbus_read_i2c_block_data(i2c, reg_addr, read_size,
434434
buf + MT6360_CRC_PREDATA_OFFSET);
435435
if (ret < 0)
436-
goto out;
437-
else if (ret != read_size) {
438-
ret = -EIO;
439-
goto out;
440-
}
436+
return ret;
437+
else if (ret != read_size)
438+
return -EIO;
441439

442440
if (crc_needed) {
443441
crc = crc8(ddata->crc8_tbl, buf, val_size + MT6360_CRC_PREDATA_OFFSET, 0);
444-
if (crc != buf[val_size + MT6360_CRC_PREDATA_OFFSET]) {
445-
ret = -EIO;
446-
goto out;
447-
}
442+
if (crc != buf[val_size + MT6360_CRC_PREDATA_OFFSET])
443+
return -EIO;
448444
}
449445

450446
memcpy(val, buf + MT6360_CRC_PREDATA_OFFSET, val_size);
451-
out:
452-
kfree(buf);
453-
return (ret < 0) ? ret : 0;
447+
448+
return 0;
454449
}
455450

456451
static int mt6360_regmap_write(void *context, const void *val, size_t val_size)

0 commit comments

Comments
 (0)