[PATCH] regulator: bd718x7: Use kcalloc() instead of kzalloc()

Qianfeng Rong posted 1 patch 1 month, 2 weeks ago
drivers/regulator/bd718x7-regulator.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] regulator: bd718x7: Use kcalloc() instead of kzalloc()
Posted by Qianfeng Rong 1 month, 2 weeks ago
Replace calls of 'devm_kzalloc(dev, count * sizeof([type]), flags)'
with 'devm_kcalloc(dev, count, sizeof([type]), flags)' in
setup_feedback_loop() for safer memory allocation with built-in
overflow protection.

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/regulator/bd718x7-regulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/regulator/bd718x7-regulator.c b/drivers/regulator/bd718x7-regulator.c
index e803cc59d68a..022d98f3c32a 100644
--- a/drivers/regulator/bd718x7-regulator.c
+++ b/drivers/regulator/bd718x7-regulator.c
@@ -1598,7 +1598,7 @@ static int setup_feedback_loop(struct device *dev, struct device_node *np,
 		if (desc->n_linear_ranges && desc->linear_ranges) {
 			struct linear_range *new;
 
-			new = devm_kzalloc(dev, desc->n_linear_ranges *
+			new = devm_kcalloc(dev, desc->n_linear_ranges,
 					   sizeof(struct linear_range),
 					   GFP_KERNEL);
 			if (!new)
-- 
2.34.1
Re: [PATCH] regulator: bd718x7: Use kcalloc() instead of kzalloc()
Posted by Mark Brown 1 month, 2 weeks ago
On Sun, 17 Aug 2025 22:23:27 +0800, Qianfeng Rong wrote:
> Replace calls of 'devm_kzalloc(dev, count * sizeof([type]), flags)'
> with 'devm_kcalloc(dev, count, sizeof([type]), flags)' in
> setup_feedback_loop() for safer memory allocation with built-in
> overflow protection.
> 
> 

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next

Thanks!

[1/1] regulator: bd718x7: Use kcalloc() instead of kzalloc()
      commit: 550bc517e59347b3b1af7d290eac4fb1411a3d4e

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark
Re: [PATCH] regulator: bd718x7: Use kcalloc() instead of kzalloc()
Posted by Matti Vaittinen 1 month, 2 weeks ago
On 17/08/2025 17:23, Qianfeng Rong wrote:
> Replace calls of 'devm_kzalloc(dev, count * sizeof([type]), flags)'
> with 'devm_kcalloc(dev, count, sizeof([type]), flags)' in
> setup_feedback_loop() for safer memory allocation with built-in
> overflow protection.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>   drivers/regulator/bd718x7-regulator.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/regulator/bd718x7-regulator.c b/drivers/regulator/bd718x7-regulator.c
> index e803cc59d68a..022d98f3c32a 100644
> --- a/drivers/regulator/bd718x7-regulator.c
> +++ b/drivers/regulator/bd718x7-regulator.c
> @@ -1598,7 +1598,7 @@ static int setup_feedback_loop(struct device *dev, struct device_node *np,
>   		if (desc->n_linear_ranges && desc->linear_ranges) {
>   			struct linear_range *new;
>   
> -			new = devm_kzalloc(dev, desc->n_linear_ranges *
> +			new = devm_kcalloc(dev, desc->n_linear_ranges,
>   					   sizeof(struct linear_range),
>   					   GFP_KERNEL);
>   			if (!new)

Thanks Qianfeng.

I don't think this is particularly hazardous, because the 
n_linear_ranges is known to be small. (It's populated in this same file, 
with a size of pre-defined array). Still, this seems like a valid change 
to me. I know some would say we should use sizeof(*new), but I kind of 
like the sizeof(struct linear_range).

Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>

Yours,
	-- Matti
Re: [PATCH] regulator: bd718x7: Use kcalloc() instead of kzalloc()
Posted by Qianfeng Rong 1 month, 2 weeks ago
在 2025/8/18 13:30, Matti Vaittinen 写道:
> On 17/08/2025 17:23, Qianfeng Rong wrote:
>> Replace calls of 'devm_kzalloc(dev, count * sizeof([type]), flags)'
>> with 'devm_kcalloc(dev, count, sizeof([type]), flags)' in
>> setup_feedback_loop() for safer memory allocation with built-in
>> overflow protection.
>>
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>> ---
>>   drivers/regulator/bd718x7-regulator.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/regulator/bd718x7-regulator.c 
>> b/drivers/regulator/bd718x7-regulator.c
>> index e803cc59d68a..022d98f3c32a 100644
>> --- a/drivers/regulator/bd718x7-regulator.c
>> +++ b/drivers/regulator/bd718x7-regulator.c
>> @@ -1598,7 +1598,7 @@ static int setup_feedback_loop(struct device 
>> *dev, struct device_node *np,
>>           if (desc->n_linear_ranges && desc->linear_ranges) {
>>               struct linear_range *new;
>>   -            new = devm_kzalloc(dev, desc->n_linear_ranges *
>> +            new = devm_kcalloc(dev, desc->n_linear_ranges,
>>                          sizeof(struct linear_range),
>>                          GFP_KERNEL);
>>               if (!new)
>
> Thanks Qianfeng.
>
> I don't think this is particularly hazardous, because the 
> n_linear_ranges is known to be small. (It's populated in this same 
> file, with a size of pre-defined array). Still, this seems like a 
> valid change to me. I know some would say we should use sizeof(*new), 
> but I kind of like the sizeof(struct linear_range).


Thanks for taking the time to explain!

I also prefer sizeof(struct linear_range) to sizeof(*new) because it is 
more readable.  :)

>
> Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
>
> Yours,
>     -- Matti
Best regards,
Qianfeng