Re: [PATCH] [soc/renesas] renesas-soc: Add error handling in renesas_soc_init

From: Geert Uytterhoeven
Date: Thu Nov 30 2023 - 05:59:25 EST


Hi Haoran,

Thanks for your patch!

On Wed, Nov 29, 2023 at 3:34 PM Haoran Liu <liuhaoran14@xxxxxxx> wrote:
> This patch enhances the renesas_soc_init function in
> drivers/soc/renesas/renesas-soc.c by adding error handling for the
> of_property_read_string call. Previously, the function did not check
> for failure cases of of_property_read_string, which could lead to
> improper behavior if the required device tree properties were missing

Which improper behavior did you encounter? All of the
soc_device_attribute fields are optional, and drivers/base/soc.c
considers that when handling the machine field (sysfs_emit() handles
NULL pointer strings fine).

> or incorrect.

FTR, "model" is a required property of the root node.

> Although the error addressed by this patch may not occur in the current
> environment, I still suggest implementing these error handling routines
> if the function is not highly time-sensitive. As the environment evolves
> or the code gets reused in different contexts, there's a possibility that
> these errors might occur. Addressing them now can prevent potential
> debugging efforts in the future, which could be quite resource-intensive.
>
> Signed-off-by: Haoran Liu <liuhaoran14@xxxxxxx>

> --- a/drivers/soc/renesas/renesas-soc.c
> +++ b/drivers/soc/renesas/renesas-soc.c
> @@ -487,7 +487,13 @@ static int __init renesas_soc_init(void)
> }
>
> np = of_find_node_by_path("/");
> - of_property_read_string(np, "model", &soc_dev_attr->machine);
> + ret = of_property_read_string(np, "model", &soc_dev_attr->machine);
> + if (ret) {
> + dev_err(dev, "Failed to read model property: %d\n", ret);

As reported by the kernel test robot:

error: use of undeclared identifier 'dev'

Please do not submit completely untested patches.

> + kfree(soc_dev_attr);
> + return ret;

As the machine field is optional, there is no need for this check,
let alone to make this a fatal error condition.

> + }
> +
> of_node_put(np);
>
> soc_dev_attr->family = kstrdup_const(family->name, GFP_KERNEL);

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds