Re: [PATCH] iio: common: scmi_iio: use kcalloc() instead of kzalloc()

From: Qianfeng Rong
Date: Tue Aug 19 2025 - 06:09:17 EST



在 2025/8/19 17:46, Andy Shevchenko 写道:
[You don't often get email from andy.shevchenko@xxxxxxxxx. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

On Tue, Aug 19, 2025 at 11:56 AM Qianfeng Rong <rongqianfeng@xxxxxxxx> wrote:
Replace calls of devm_kzalloc() with devm_kcalloc() in scmi_alloc_iiodev()
and scmi_iio_set_sampling_freq_avail() for safer memory allocation with
built-in overflow protection.
While this change is correct...

...

sensor->freq_avail =
- devm_kzalloc(&iio_dev->dev,
- sizeof(*sensor->freq_avail) *
- (sensor->sensor_info->intervals.count * 2),
+ devm_kcalloc(&iio_dev->dev,
+ sensor->sensor_info->intervals.count * 2,
...I would also switch this to use array_size() instead of explicit
multiplication as it will check for boundaries that are not static in
this case.

I don't understand what "will check for boundaries that are not static in
this case" means. Could you explain it to me?

I've experimented with the following command and found that kmalloc_array()
generates fewer instructions than kmalloc(array_size()):
objdump -dSl --prefix-addresses <changed module>.o

--
With Best Regards,
Andy Shevchenko
Best regards,
Qianfeng