Re: [PATCH] staging: media: atomisp: refactor sizeof(struct type) to sizeof(*ptr)

From: Andy Shevchenko

Date: Tue Jan 13 2026 - 05:46:20 EST


On Tue, Jan 13, 2026 at 12:37 PM Anubhav Kokane <dev.anubhavk@xxxxxxxxx> wrote:
> On Tue, Jan 13, 2026 at 12:44 AM Andy Shevchenko wrote:
> > the preferred way is to switch to use kcalloc() in all three places.
> >
> > x = kcalloc(count, sizeof(*...), ...);
> > if (!x)
> > ...handle error...
> >
> > while (count--) {
> > ...
> > }
> > ...
> > err:
> > list_for_each... {
> > ...
> > }
> > kfree(x);

> Thank you for the feedback.

You're welcome!

> I looked into implementing kcalloc() as suggested. But the issue is struct
> atomisp_s3a_buf (and the other buffers) are defined as list nodes with
> struct list_head list embedded in them.

Yes, and how does it affect the allocation?

> The driver relies on adding these
> individually to asd->s3a_stats and freeing them individually using kfree()
> in multiple cleanup paths (including error path here).

Is it the issue? Instead of incrementing by a pointer size, you will
increment an address by a structure size, this is how + operator works
in C from the beginning (or close enough to that time).

> Switching to kcalloc() would mean the s3a_buf is no longer a standalone
> object but a slice of an array. This would lead to invalid or double frees
> if the existing code tries kfree() on this array element.

How? As I showed above you need to carefully move and replace
individual handling by a common one. So, instead of allocation per
item it will be an allocation per bucket.

> Addressing this requires a larger refactor of the buffer management logic
> across the driver,

Exactly! And that's what I think is the best way moving forward. You
will kill two birds with one stone: fixing the issue at hand and
improving the memory allocations in the driver in this area a lot.

> would you prefer I stick to the sizeof(*ptr) hardening for
> now to fix the checkpatch warning?

See above. As now I think this is unneeded churn as the idea would
still be the same — moving towards kcalloc().

--
With Best Regards,
Andy Shevchenko