Re: [PATCH v2] iio: adc: ti-ads7138: replace kmalloc() with stack allocation in i2c_write_block

From: Andy Shevchenko

Date: Mon Apr 27 2026 - 11:00:40 EST


On Mon, Apr 27, 2026 at 03:27:07PM +0400, Giorgi Tchankvetadze wrote:
> The ads7138_i2c_write_block() function currently utilizes kmalloc()
> to allocate a buffer for I2C transfers. However, the length
> parameter passed to this function is strictly 2 bytes across all
> driver invocations, making the total payload buffer size exactly 4 bytes.
> Invoking the heap allocator for a 4-byte buffer introduces

> unnecessary SLUB overhead.

Trailing space.

> Replace the kmalloc() call with a statically sized 4-byte stack array.
> Add a boundary check returning -EINVAL to ensure future driver
> modifications do not overflow the stack buffer if a length
> greater than 2 is requested.
> Furthermore, dropping the dynamic allocation entirely removes the need to
> include <linux/slab.h> (which was the original motivation for this patch)
> and streamlines the PIO transfer path.

...

> - ret = i2c_master_send(client, buf, len);
> + ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));

The 'buf' is of u8 type, hence sizeof() is enough.

...

> - if (ret != len)
> + if (ret != ARRAY_SIZE(buf))

Ditto.

--
With Best Regards,
Andy Shevchenko