回复：[PATCH v10 net-next 02/11] net/nebula-matrix: add our driver architecture

Illusion Wang
2026-04-07T06:40:59Z

>> +
>> + chan_mgt = devm_kzalloc(dev, sizeof(*chan_mgt), GFP_KERNEL);
>> + if (!chan_mgt)
>> +  return ERR_PTR(-ENOMEM);
>> +
>> + chan_mgt->common = common;
>> + chan_mgt->hw_ops_tbl = hw_ops_tbl;
>> +
>> + mailbox = devm_kzalloc(dev, sizeof(*mailbox), GFP_KERNEL);
>> + if (!mailbox)
>> +  return ERR_PTR(-ENOMEM);

>Here, if mailbox allocation fails, we return without freeing chan_mgt
>resulting in a leak.

>> + mailbox->chan_type = NBL_CHAN_TYPE_MAILBOX;
>> + chan_mgt->chan_info[NBL_CHAN_TYPE_MAILBOX] = mailbox;
>> +
>> + return chan_mgt;
>> +}

Thanks for your feedback.
I've carefully considered your comment about the potential
memory leak in the provided code snippet,
but I use devm_kzalloc(), the area is guaranteed to be
freed whether initialization fails half-way or the device
gets detached.
ps:Documentation\driver-api\driver-model\devres.tst
my_init_one()
{
struct mydev *d;

d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
if (!d)
return -ENOMEM;

d->ring = dmam_alloc_coherent(...);
if (!d->ring)
return -ENOMEM;

if (check something)
return -EINVAL;
...
}
--illusion.wang