Re: [PATCH] usb: typec: tcpm: kzalloc + kcalloc to kzalloc_flex

From: Rosen Penev

Date: Fri Mar 06 2026 - 14:16:58 EST


On Fri, Mar 6, 2026 at 10:54 AM Gustavo A. R. Silva
<gustavo@xxxxxxxxxxxxxx> wrote:
>
>
>
> On 3/6/26 12:16, Rosen Penev wrote:
> > Simplifies allocation and allows using __counted_by for extra runtime
> > analysis.
>
> Are you finding these with Coccinelle or any other tool?
git grep -B 10 zalloc_objs\( | grep zalloc\(
git grep -B 10 calloc\( | grep zalloc\(

And seeing if it's something that can easily be dealt with. The
__counted_by thing is usually clear in _probe if it's correct.
>
> >
> > Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
>
> Reviewed-by: Gustavo A. R. Silva <gustavoars@xxxxxxxxxx>
>
> Thanks
> -Gustavo
>
> > ---
> > drivers/usb/typec/tcpm/tcpm.c | 19 +++++--------------
> > 1 file changed, 5 insertions(+), 14 deletions(-)
> >
> > diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> > index 1d2f3af034c5..272f9187b12d 100644
> > --- a/drivers/usb/typec/tcpm/tcpm.c
> > +++ b/drivers/usb/typec/tcpm/tcpm.c
> > @@ -605,9 +605,9 @@ struct altmode_vdm_event {
> > struct kthread_work work;
> > struct tcpm_port *port;
> > u32 header;
> > - u32 *data;
> > int cnt;
> > enum tcpm_transmit_type tx_sop_type;
> > + u32 data[] __counted_by(cnt);
> > };
> >
> > static const char * const pd_rev[] = {
> > @@ -1653,7 +1653,6 @@ static void tcpm_queue_vdm_work(struct kthread_work *work)
> > tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
> >
> > port_unlock:
> > - kfree(event->data);
> > kfree(event);
> > mutex_unlock(&port->lock);
> > }
> > @@ -1662,35 +1661,27 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
> > const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
> > {
> > struct altmode_vdm_event *event;
> > - u32 *data_cpy;
> > int ret = -ENOMEM;
> >
> > - event = kzalloc_obj(*event);
> > + event = kzalloc_flex(*event, data, cnt);
> > if (!event)
> > goto err_event;
> >
> > - data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
> > - if (!data_cpy)
> > - goto err_data;
> > -
> > kthread_init_work(&event->work, tcpm_queue_vdm_work);
> > + event->cnt = cnt;
> > event->port = port;
> > event->header = header;
> > - memcpy(data_cpy, data, sizeof(u32) * cnt);
> > - event->data = data_cpy;
> > - event->cnt = cnt;
> > + memcpy(event->data, data, sizeof(u32) * cnt);
> > event->tx_sop_type = tx_sop_type;
> >
> > ret = kthread_queue_work(port->wq, &event->work);
> > if (!ret) {
> > ret = -EBUSY;
> > - goto err_queue;
> > + goto err_data;
> > }
> >
> > return 0;
> >
> > -err_queue:
> > - kfree(data_cpy);
> > err_data:
> > kfree(event);
> > err_event:
>