pinctrl: s32cc: fix uninitialized memory issues
Subsystems (4)
cover pinctrl: s32cc: fix uninitialized memory issues
This is a small series that fixes some uninitialized memory issues in pinctrl-s32cc. As an example of how these can affect the kernel, when probing i2c-imx, a memory allocation may fail because of the uninitialized memory giving a junk allocation size, which prevents chips on one of the I2C buses from being detected:
# i2cdetect -l
i2c-1 i2c 401ec000.i2c I2C adapter
i2c-2 i2c 402dc000.i2c I2C adapter
i2c-0 i2c 401e4000.i2c I2C adapter
# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --Compared to when no failure occurs:
# i2cdetect -l
i2c-1 i2c 401ec000.i2c I2C adapter
i2c-2 i2c 402dc000.i2c I2C adapter
i2c-0 i2c 401e4000.i2c I2C adapter
# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -----
Jared Kangas (2):
pinctrl: s32cc: fix uninitialized memory in s32_pinctrl_desc
pinctrl: s32cc: initialize gpio_pin_config::list after kmalloc()
drivers/pinctrl/nxp/pinctrl-s32cc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
---
base-commit: e9a6fb0bcdd7609be6969112f3fbfcce3b1d4a7c
change-id: 20251106-pinctrl-s32cc-alloc-init-cb04ebcc72f3
Best regards,
Signature · 1 line
Jared Kangas <[email protected]>
PATCH 1/2 pinctrl: s32cc: fix uninitialized memory in s32_pinctrl_desc
s32_pinctrl_desc is allocated with devm_kmalloc(), but not all of its fields are initialized. Notably, num_custom_params is used in pinconf_generic_parse_dt_config(), resulting in intermittent allocation errors, such as the following splat when probing i2c-imx:
WARNING: CPU: 0 PID: 176 at mm/page_alloc.c:4795 __alloc_pages_noprof+0x290/0x300
[...]
Hardware name: NXP S32G3 Reference Design Board 3 (S32G-VNP-RDB3) (DT)
[...]
Call trace:
__alloc_pages_noprof+0x290/0x300 (P)
___kmalloc_large_node+0x84/0x168
__kmalloc_large_node_noprof+0x34/0x120
__kmalloc_noprof+0x2ac/0x378
pinconf_generic_parse_dt_config+0x68/0x1a0
s32_dt_node_to_map+0x104/0x248
dt_to_map_one_config+0x154/0x1d8
pinctrl_dt_to_map+0x12c/0x280
create_pinctrl+0x6c/0x270
pinctrl_get+0xc0/0x170
devm_pinctrl_get+0x50/0xa0
pinctrl_bind_pins+0x60/0x2a0
really_probe+0x60/0x3a0
[...]
__platform_driver_register+0x2c/0x40
i2c_adap_imx_init+0x28/0xff8 [i2c_imx]
[...]This results in later parse failures that can cause issues in dependent drivers:
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c0-pins/i2c0-grp0: could not parse node property
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c0-pins/i2c0-grp0: could not parse node property
[...]
pca953x 0-0022: failed writing register: -6
i2c i2c-0: IMX I2C adapter registered
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c2-pins/i2c2-grp0: could not parse node property
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c2-pins/i2c2-grp0: could not parse node property
i2c i2c-1: IMX I2C adapter registered
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c4-pins/i2c4-grp0: could not parse node property
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c4-pins/i2c4-grp0: could not parse node property
i2c i2c-2: IMX I2C adapter registeredFix this by initializing s32_pinctrl_desc with devm_kzalloc() instead of devm_kmalloc() in s32_pinctrl_probe(), which sets the previously uninitialized fields to zero.
--- drivers/pinctrl/nxp/pinctrl-s32cc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/nxp/pinctrl-s32cc.c +1 -1
@@ -951,7 +951,7 @@ int s32_pinctrl_probe(struct platform_device *pdev,spin_lock_init (&ipctl->gpio_configs_lock); s32_pinctrl_desc = -devm_kmalloc (&pdev->dev,sizeof (*s32_pinctrl_desc),GFP_KERNEL ); +devm_kzalloc (&pdev->dev,sizeof (*s32_pinctrl_desc),GFP_KERNEL );if (!s32_pinctrl_desc)return -ENOMEM ;
Signature · 1 line
2.51.1
RE: [EXT] [PATCH 1/2] pinctrl: s32cc: fix uninitialized memory in s32_pinctrl_desc
Quoted 81 lines
s32_pinctrl_descis allocated withdevm_kmalloc(), but not all of its fields are initialized. Notably,num_custom_paramsis used inpinconf_generic_parse_dt_config(), resulting in intermittent allocation errors, such as the following splat when probing i2c-imx:WARNING: CPU: 0 PID: 176 at mm/page_alloc.c:4795 __alloc_pages_noprof+0x290/0x300 [...] Hardware name: NXP S32G3 Reference Design Board 3 (S32G-VNP-RDB3) (DT) [...] Call trace: __alloc_pages_noprof+0x290/0x300 (P) ___kmalloc_large_node+0x84/0x168 __kmalloc_large_node_noprof+0x34/0x120 __kmalloc_noprof+0x2ac/0x378 pinconf_generic_parse_dt_config+0x68/0x1a0 s32_dt_node_to_map+0x104/0x248 dt_to_map_one_config+0x154/0x1d8 pinctrl_dt_to_map+0x12c/0x280 create_pinctrl+0x6c/0x270 pinctrl_get+0xc0/0x170 devm_pinctrl_get+0x50/0xa0 pinctrl_bind_pins+0x60/0x2a0 really_probe+0x60/0x3a0 [...] __platform_driver_register+0x2c/0x40 i2c_adap_imx_init+0x28/0xff8 [i2c_imx] [...]This results in later parse failures that can cause issues in dependent drivers:
s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c0- pins/i2c0-grp0: could not parse node property s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c0- pins/i2c0-grp0: could not parse node property [...] pca953x 0-0022: failed writing register: -6 i2c i2c-0: IMX I2C adapter registered s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c2- pins/i2c2-grp0: could not parse node property s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c2-pins/i2c2-grp0: could not parse node property
i2c i2c-1: IMX I2C adapter registered s32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c4-
pins/i2c4-grp0: could not parse node propertys32g-siul2-pinctrl 4009c240.pinctrl: /soc@0/pinctrl@4009c240/i2c4-pins/i2c4-grp0: could not parse node property
i2c i2c-2: IMX I2C adapter registeredFix this by initializing
s32_pinctrl_descwithdevm_kzalloc()instead ofdevm_kmalloc()ins32_pinctrl_probe(), which sets the previously uninitialized fields to zero.Fixes fd84aaa8173d ("pinctrl: add NXP S32 SoC family support")Signed-off-by Jared Kangas <[email protected]>--- drivers/pinctrl/nxp/pinctrl-s32cc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pinctrl/nxp/pinctrl- renamed from drivers/pinctrl/nxp/pinctrl-s32cc.c +0
-0
s32cc.c
index501eb296c76050aa05386c51ef6ae0f97d4c76c3..51ecb8d0fb7e8a203e10cbe965dfec308eaa5f30100644drivers/pinctrl/nxp/pinctrl-s32cc.c +1
-2@@ -951,7 +951,7 @@ int s32_pinctrl_probe(struct platform_device *pdev,spin_lock_init (&ipctl->gpio_configs_lock); s32_pinctrl_desc = -devm_kmalloc (&pdev->dev,sizeof (*s32_pinctrl_desc), GFP_KERNEL); +devm_kzalloc (&pdev->dev,sizeof (*s32_pinctrl_desc),GFP_KERNEL );if (!s32_pinctrl_desc)return -ENOMEM ; --
Quoted 1 line
2.51.1
Thanks for the fix (I had it on my list too).
PATCH 2/2 pinctrl: s32cc: initialize gpio_pin_config::list after kmalloc()
In reply to Jared Kangass32_pmx_gpio_request_enable() does not initialize the newly-allocated gpio_pin_config::list before adding it to s32_pinctrl::gpio_configs. This could result in a linked list corruption.
Initialize the new list_head with INIT_LIST_HEAD() to fix this.
--- drivers/pinctrl/nxp/pinctrl-s32cc.c | 1 + 1 file changed, 1 insertion(+)
drivers/pinctrl/nxp/pinctrl-s32cc.c +1 -0
@@ -392,6 +392,7 @@ static int s32_pmx_gpio_request_enable(struct pinctrl_dev *pctldev, gpio_pin->pin_id = offset; gpio_pin->config = config; +INIT_LIST_HEAD (&gpio_pin->list);spin_lock_irqsave (&ipctl->gpio_configs_lock, flags);list_add (&gpio_pin->list, &ipctl->gpio_configs);
Signature · 1 line
2.51.1
Quoted 38 lines · Jared Kangas
This is a small series that fixes some uninitialized memory issues in pinctrl-s32cc. As an example of how these can affect the kernel, when probing i2c-imx, a memory allocation may fail because of the uninitialized memory giving a junk allocation size, which prevents chips on one of the I2C buses from being detected:
# i2cdetect -l i2c-1 i2c 401ec000.i2c I2C adapter i2c-2 i2c 402dc000.i2c I2C adapter i2c-0 i2c 401e4000.i2c I2C adapter # i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --Compared to when no failure occurs:
# i2cdetect -l i2c-1 i2c 401ec000.i2c I2C adapter i2c-2 i2c 402dc000.i2c I2C adapter i2c-0 i2c 401e4000.i2c I2C adapter # i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 50: -- 51 -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
Quoted 5 lines
Signed-off-by Jared Kangas <[email protected]>---
Jared Kangas (2):
pinctrl: s32cc: fix uninitialized memory ins32_pinctrl_desc
pinctrl: s32cc: initializegpio_pin_config::listafterkmalloc()
Patches applied for fixes!
Nice attention to detail here.
Yours,
Linus Walleij