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

From: David Laight

Date: Tue Apr 28 2026 - 05:44:53 EST


On Mon, 27 Apr 2026 10:21:37 -0500
David Lechner <dlechner@xxxxxxxxxxxx> wrote:

> On 4/27/26 8:31 AM, David Laight wrote:
> > On Mon, 27 Apr 2026 15:27:07 +0400
> > Giorgi Tchankvetadze <giorgitchankvetadze1997@xxxxxxxxx> 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.
> >
> > Have you confirmed that the buffer is never used for DMA?
> >
> > Provided the lock that blocks concurrent access from two threads
> > is actually outside this code, a buffer for short transfers could
> > be allocated within 'struct i2c_client'.
> >
> > David
> >
> Giorgi,
>
> This is why it is important to include a changelog and a link to the
> previous discussion [1] in the cover letter or after --- in a single patch
> when you submit a new revision. I think that would have answered David's
> question.
>
> [1]: https://lore.kernel.org/linux-iio/20260424081809.61841-2-giorgitchankvetadze1997@xxxxxxxxx/
>
>

The thing is I remember issues with on-stack buffers being used for
dma - which worked before kernel stacks were allocated using vmalloc().
One of the solutions to that is to use kmalloc() for buffers that
could be on stack.

I think I found exactly 1 call to this function.
Indeed, just inlining the logic would make everything clearer.

David