[PATCH 4/4] pinctrl: sunxi: use kcalloc() instead of kzalloc()

Qianfeng Rong posted 4 patches 1 month, 2 weeks ago
[PATCH 4/4] pinctrl: sunxi: use kcalloc() instead of kzalloc()
Posted by Qianfeng Rong 1 month, 2 weeks ago
Use devm_kcalloc() in init_pins_table() and prepare_function_table() to
gain built-in overflow protection, making memory allocation safer when
calculating allocation size compared to explicit multiplication.

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c b/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
index 4e34b0cd3b73..5f13315ebff3 100644
--- a/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
+++ b/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
@@ -103,7 +103,7 @@ static struct sunxi_desc_pin *init_pins_table(struct device *dev,
 		return ERR_PTR(-EINVAL);
 	}
 
-	pins = devm_kzalloc(dev, desc->npins * sizeof(*pins), GFP_KERNEL);
+	pins = devm_kcalloc(dev, desc->npins, sizeof(*pins), GFP_KERNEL);
 	if (!pins)
 		return ERR_PTR(-ENOMEM);
 
@@ -199,7 +199,7 @@ static int prepare_function_table(struct device *dev, struct device_node *pnode,
 	 * Allocate the memory needed for the functions in one table.
 	 * We later use pointers into this table to mark each pin.
 	 */
-	func = devm_kzalloc(dev, num_funcs * sizeof(*func), GFP_KERNEL);
+	func = devm_kcalloc(dev, num_funcs, sizeof(*func), GFP_KERNEL);
 	if (!func)
 		return -ENOMEM;
 
-- 
2.34.1
Re: [PATCH 4/4] pinctrl: sunxi: use kcalloc() instead of kzalloc()
Posted by Chen-Yu Tsai 1 month, 2 weeks ago
On Tue, Aug 19, 2025 at 10:40 PM Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>
> Use devm_kcalloc() in init_pins_table() and prepare_function_table() to
> gain built-in overflow protection, making memory allocation safer when
> calculating allocation size compared to explicit multiplication.
>
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

> ---
>  drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c b/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
> index 4e34b0cd3b73..5f13315ebff3 100644
> --- a/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
> +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
> @@ -103,7 +103,7 @@ static struct sunxi_desc_pin *init_pins_table(struct device *dev,
>                 return ERR_PTR(-EINVAL);
>         }
>
> -       pins = devm_kzalloc(dev, desc->npins * sizeof(*pins), GFP_KERNEL);
> +       pins = devm_kcalloc(dev, desc->npins, sizeof(*pins), GFP_KERNEL);
>         if (!pins)
>                 return ERR_PTR(-ENOMEM);
>
> @@ -199,7 +199,7 @@ static int prepare_function_table(struct device *dev, struct device_node *pnode,
>          * Allocate the memory needed for the functions in one table.
>          * We later use pointers into this table to mark each pin.
>          */
> -       func = devm_kzalloc(dev, num_funcs * sizeof(*func), GFP_KERNEL);
> +       func = devm_kcalloc(dev, num_funcs, sizeof(*func), GFP_KERNEL);
>         if (!func)
>                 return -ENOMEM;
>
> --
> 2.34.1
>