2023-10-16 05:36:12

by Stanley Chang[昌育德]

[permalink] [raw]
Subject: [PATCH extcon-next] extcon: realtek: add the error handler for nvmem_cell_read

There are following smatch warning:
drivers/extcon/extcon-rtk-type-c.c:905 __updated_type_c_parameter_by_efuse()
error: 'buf' dereferencing possible ERR_PTR()

The nvmem_cell_read may fail to read. So, driver must handle failure cases.

Fixes: 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")
Reported-by: Dan Carpenter <[email protected]>
Closes: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Stanley Chang <[email protected]>
---
drivers/extcon/extcon-rtk-type-c.c | 86 +++++++++++++++---------------
1 file changed, 44 insertions(+), 42 deletions(-)

diff --git a/drivers/extcon/extcon-rtk-type-c.c b/drivers/extcon/extcon-rtk-type-c.c
index 00465cfba23e..a592bab77538 100644
--- a/drivers/extcon/extcon-rtk-type-c.c
+++ b/drivers/extcon/extcon-rtk-type-c.c
@@ -901,25 +901,26 @@ static int __updated_type_c_parameter_by_efuse(struct type_c_data *type_c)
int value_mask = (BIT(value_size) - 1);

buf = nvmem_cell_read(cell, &buf_size);
-
- cc1_0p2v = get_value((buf[0] >> value_size * 0) & value_mask);
- cc1_0p8v = get_value((buf[0] >> value_size * 1) & value_mask);
- cc1_2p6v = get_value((buf[1] >> value_size * 0) & value_mask);
- cc1_0p66v = get_value((buf[1] >> value_size * 1) & value_mask);
- cc1_1p23v = get_value((buf[2] >> value_size * 0) & value_mask);
-
- cc2_0p2v = get_value((buf[3] >> value_size * 0) & value_mask);
- cc2_0p8v = get_value((buf[3] >> value_size * 1) & value_mask);
- cc2_2p6v = get_value((buf[4] >> value_size * 0) & value_mask);
- cc2_0p66v = get_value((buf[4] >> value_size * 1) & value_mask);
- cc2_1p23v = get_value((buf[5] >> value_size * 0) & value_mask);
-
- cc1_4p7k = get_value((buf[6] >> value_size * 0) & value_mask);
- cc1_12k = get_value((buf[6] >> value_size * 1) & value_mask);
- cc2_4p7k = get_value((buf[7] >> value_size * 0) & value_mask);
- cc2_12k = get_value((buf[7] >> value_size * 1) & value_mask);
-
- kfree(buf);
+ if (!IS_ERR(buf)) {
+ cc1_0p2v = get_value((buf[0] >> value_size * 0) & value_mask);
+ cc1_0p8v = get_value((buf[0] >> value_size * 1) & value_mask);
+ cc1_2p6v = get_value((buf[1] >> value_size * 0) & value_mask);
+ cc1_0p66v = get_value((buf[1] >> value_size * 1) & value_mask);
+ cc1_1p23v = get_value((buf[2] >> value_size * 0) & value_mask);
+
+ cc2_0p2v = get_value((buf[3] >> value_size * 0) & value_mask);
+ cc2_0p8v = get_value((buf[3] >> value_size * 1) & value_mask);
+ cc2_2p6v = get_value((buf[4] >> value_size * 0) & value_mask);
+ cc2_0p66v = get_value((buf[4] >> value_size * 1) & value_mask);
+ cc2_1p23v = get_value((buf[5] >> value_size * 0) & value_mask);
+
+ cc1_4p7k = get_value((buf[6] >> value_size * 0) & value_mask);
+ cc1_12k = get_value((buf[6] >> value_size * 1) & value_mask);
+ cc2_4p7k = get_value((buf[7] >> value_size * 0) & value_mask);
+ cc2_12k = get_value((buf[7] >> value_size * 1) & value_mask);
+
+ kfree(buf);
+ }
nvmem_cell_put(cell);
}

@@ -984,29 +985,30 @@ static int __updated_type_c_parameter_by_efuse_v2(struct type_c_data *type_c)
int value_mask = (BIT(value_size) - 1);

buf = nvmem_cell_read(cell, &buf_size);
-
- value_size = 5;
- value_mask = (BIT(value_size) - 1);
- cc1_4p7k = buf[0] & value_mask;
- cc1_12k = buf[1] & value_mask;
- cc2_4p7k = buf[2] & value_mask;
- cc2_12k = buf[3] & value_mask;
-
- value_size = 4;
- value_mask = (BIT(value_size) - 1);
- cc1_0p2v = (buf[4] >> value_size * 0) & value_mask;
- cc1_0p66v = (buf[4] >> value_size * 1) & value_mask;
- cc1_0p8v = (buf[5] >> value_size * 0) & value_mask;
- cc1_1p23v = (buf[5] >> value_size * 1) & value_mask;
- cc1_2p6v = (buf[6] >> value_size * 0) & value_mask;
-
- cc2_0p2v = (buf[6] >> value_size * 1) & value_mask;
- cc2_0p66v = (buf[7] >> value_size * 0) & value_mask;
- cc2_0p8v = (buf[7] >> value_size * 1) & value_mask;
- cc2_1p23v = (buf[8] >> value_size * 0) & value_mask;
- cc2_2p6v = (buf[8] >> value_size * 1) & value_mask;
-
- kfree(buf);
+ if (!IS_ERR(buf)) {
+ value_size = 5;
+ value_mask = (BIT(value_size) - 1);
+ cc1_4p7k = buf[0] & value_mask;
+ cc1_12k = buf[1] & value_mask;
+ cc2_4p7k = buf[2] & value_mask;
+ cc2_12k = buf[3] & value_mask;
+
+ value_size = 4;
+ value_mask = (BIT(value_size) - 1);
+ cc1_0p2v = (buf[4] >> value_size * 0) & value_mask;
+ cc1_0p66v = (buf[4] >> value_size * 1) & value_mask;
+ cc1_0p8v = (buf[5] >> value_size * 0) & value_mask;
+ cc1_1p23v = (buf[5] >> value_size * 1) & value_mask;
+ cc1_2p6v = (buf[6] >> value_size * 0) & value_mask;
+
+ cc2_0p2v = (buf[6] >> value_size * 1) & value_mask;
+ cc2_0p66v = (buf[7] >> value_size * 0) & value_mask;
+ cc2_0p8v = (buf[7] >> value_size * 1) & value_mask;
+ cc2_1p23v = (buf[8] >> value_size * 0) & value_mask;
+ cc2_2p6v = (buf[8] >> value_size * 1) & value_mask;
+
+ kfree(buf);
+ }
nvmem_cell_put(cell);
}

--
2.34.1


2023-10-17 08:41:35

by Chanwoo Choi

[permalink] [raw]
Subject: Re: [PATCH extcon-next] extcon: realtek: add the error handler for nvmem_cell_read

On 23. 10. 16. 14:35, Stanley Chang wrote:
> There are following smatch warning:
> drivers/extcon/extcon-rtk-type-c.c:905 __updated_type_c_parameter_by_efuse()
> error: 'buf' dereferencing possible ERR_PTR()
>
> The nvmem_cell_read may fail to read. So, driver must handle failure cases.
>
> Fixes: 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")
> Reported-by: Dan Carpenter <[email protected]>
> Closes: https://lore.kernel.org/all/[email protected]/
> Signed-off-by: Stanley Chang <[email protected]>
> ---
> drivers/extcon/extcon-rtk-type-c.c | 86 +++++++++++++++---------------
> 1 file changed, 44 insertions(+), 42 deletions(-)
>
> diff --git a/drivers/extcon/extcon-rtk-type-c.c b/drivers/extcon/extcon-rtk-type-c.c
> index 00465cfba23e..a592bab77538 100644
> --- a/drivers/extcon/extcon-rtk-type-c.c
> +++ b/drivers/extcon/extcon-rtk-type-c.c
> @@ -901,25 +901,26 @@ static int __updated_type_c_parameter_by_efuse(struct type_c_data *type_c)
> int value_mask = (BIT(value_size) - 1);
>
> buf = nvmem_cell_read(cell, &buf_size);
> -
> - cc1_0p2v = get_value((buf[0] >> value_size * 0) & value_mask);
> - cc1_0p8v = get_value((buf[0] >> value_size * 1) & value_mask);
> - cc1_2p6v = get_value((buf[1] >> value_size * 0) & value_mask);
> - cc1_0p66v = get_value((buf[1] >> value_size * 1) & value_mask);
> - cc1_1p23v = get_value((buf[2] >> value_size * 0) & value_mask);
> -
> - cc2_0p2v = get_value((buf[3] >> value_size * 0) & value_mask);
> - cc2_0p8v = get_value((buf[3] >> value_size * 1) & value_mask);
> - cc2_2p6v = get_value((buf[4] >> value_size * 0) & value_mask);
> - cc2_0p66v = get_value((buf[4] >> value_size * 1) & value_mask);
> - cc2_1p23v = get_value((buf[5] >> value_size * 0) & value_mask);
> -
> - cc1_4p7k = get_value((buf[6] >> value_size * 0) & value_mask);
> - cc1_12k = get_value((buf[6] >> value_size * 1) & value_mask);
> - cc2_4p7k = get_value((buf[7] >> value_size * 0) & value_mask);
> - cc2_12k = get_value((buf[7] >> value_size * 1) & value_mask);
> -
> - kfree(buf);
> + if (!IS_ERR(buf)) {
> + cc1_0p2v = get_value((buf[0] >> value_size * 0) & value_mask);
> + cc1_0p8v = get_value((buf[0] >> value_size * 1) & value_mask);
> + cc1_2p6v = get_value((buf[1] >> value_size * 0) & value_mask);
> + cc1_0p66v = get_value((buf[1] >> value_size * 1) & value_mask);
> + cc1_1p23v = get_value((buf[2] >> value_size * 0) & value_mask);
> +
> + cc2_0p2v = get_value((buf[3] >> value_size * 0) & value_mask);
> + cc2_0p8v = get_value((buf[3] >> value_size * 1) & value_mask);
> + cc2_2p6v = get_value((buf[4] >> value_size * 0) & value_mask);
> + cc2_0p66v = get_value((buf[4] >> value_size * 1) & value_mask);
> + cc2_1p23v = get_value((buf[5] >> value_size * 0) & value_mask);
> +
> + cc1_4p7k = get_value((buf[6] >> value_size * 0) & value_mask);
> + cc1_12k = get_value((buf[6] >> value_size * 1) & value_mask);
> + cc2_4p7k = get_value((buf[7] >> value_size * 0) & value_mask);
> + cc2_12k = get_value((buf[7] >> value_size * 1) & value_mask);
> +
> + kfree(buf);
> + }
> nvmem_cell_put(cell);
> }
>
> @@ -984,29 +985,30 @@ static int __updated_type_c_parameter_by_efuse_v2(struct type_c_data *type_c)
> int value_mask = (BIT(value_size) - 1);
>
> buf = nvmem_cell_read(cell, &buf_size);
> -
> - value_size = 5;
> - value_mask = (BIT(value_size) - 1);
> - cc1_4p7k = buf[0] & value_mask;
> - cc1_12k = buf[1] & value_mask;
> - cc2_4p7k = buf[2] & value_mask;
> - cc2_12k = buf[3] & value_mask;
> -
> - value_size = 4;
> - value_mask = (BIT(value_size) - 1);
> - cc1_0p2v = (buf[4] >> value_size * 0) & value_mask;
> - cc1_0p66v = (buf[4] >> value_size * 1) & value_mask;
> - cc1_0p8v = (buf[5] >> value_size * 0) & value_mask;
> - cc1_1p23v = (buf[5] >> value_size * 1) & value_mask;
> - cc1_2p6v = (buf[6] >> value_size * 0) & value_mask;
> -
> - cc2_0p2v = (buf[6] >> value_size * 1) & value_mask;
> - cc2_0p66v = (buf[7] >> value_size * 0) & value_mask;
> - cc2_0p8v = (buf[7] >> value_size * 1) & value_mask;
> - cc2_1p23v = (buf[8] >> value_size * 0) & value_mask;
> - cc2_2p6v = (buf[8] >> value_size * 1) & value_mask;
> -
> - kfree(buf);
> + if (!IS_ERR(buf)) {
> + value_size = 5;
> + value_mask = (BIT(value_size) - 1);
> + cc1_4p7k = buf[0] & value_mask;
> + cc1_12k = buf[1] & value_mask;
> + cc2_4p7k = buf[2] & value_mask;
> + cc2_12k = buf[3] & value_mask;
> +
> + value_size = 4;
> + value_mask = (BIT(value_size) - 1);
> + cc1_0p2v = (buf[4] >> value_size * 0) & value_mask;
> + cc1_0p66v = (buf[4] >> value_size * 1) & value_mask;
> + cc1_0p8v = (buf[5] >> value_size * 0) & value_mask;
> + cc1_1p23v = (buf[5] >> value_size * 1) & value_mask;
> + cc1_2p6v = (buf[6] >> value_size * 0) & value_mask;
> +
> + cc2_0p2v = (buf[6] >> value_size * 1) & value_mask;
> + cc2_0p66v = (buf[7] >> value_size * 0) & value_mask;
> + cc2_0p8v = (buf[7] >> value_size * 1) & value_mask;
> + cc2_1p23v = (buf[8] >> value_size * 0) & value_mask;
> + cc2_2p6v = (buf[8] >> value_size * 1) & value_mask;
> +
> + kfree(buf);
> + }
> nvmem_cell_put(cell);
> }
>

Applied it. Thanks.

--
Best Regards,
Samsung Electronics
Chanwoo Choi

2023-10-18 03:34:58

by Stanley Chang[昌育德]

[permalink] [raw]
Subject: RE: [PATCH extcon-next] extcon: realtek: add the error handler for nvmem_cell_read

Hi Chanwoo,

> >
>
> Applied it. Thanks.
>
Thank you so much.

Stanley