mdio-mux-gpio: Remove VLA usage
Subsystems (3)
PATCH v3 1/1 mdio-mux-gpio: Remove VLA usage
In the quest to remove all stack VLA usage from the kernel[1], this allocates the values buffer during the callback instead of putting it on the stack.
[1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
---
v2: allocate array as part of structure (Andrew Lunn)
v3: resend to netdev with Reviewed-by
--- drivers/net/phy/mdio-mux-gpio.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/net/phy/mdio-mux-gpio.c +14 -8
@@ -20,23 +20,23 @@struct mdio_mux_gpio_state {struct gpio_descs *gpios;void *mux_handle; +int values[]; };static int mdio_mux_gpio_switch_fn (int current_child,int desired_child,void *data) {struct mdio_mux_gpio_state *s = data; -int values[s->gpios->ndescs];unsigned int n;if (current_child == desired_child)return 0 ;for (n =0 ; n < s->gpios->ndescs; n++) - values[n] = (desired_child >> n) &1 ; + s->values[n] = (desired_child >> n) &1 ;gpiod_set_array_value_cansleep (s->gpios->ndescs, s->gpios->desc, - values); + s->values);return 0 ; }
@@ -44,15 +44,21 @@ static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,static int mdio_mux_gpio_probe (struct platform_device *pdev) {struct mdio_mux_gpio_state *s; +struct gpio_descs *gpios;int r; - s =devm_kzalloc (&pdev->dev,sizeof (*s),GFP_KERNEL ); -if (!s) + gpios =gpiod_get_array (&pdev->dev,NULL ,GPIOD_OUT_LOW ); +if (IS_ERR (gpios)) +return PTR_ERR (gpios); + + s =devm_kzalloc (&pdev->dev,sizeof (*s->values) * gpios->ndescs + +sizeof (*s),GFP_KERNEL ); +if (!s) { +gpiod_put_array (gpios);return -ENOMEM ; + } - s->gpios =gpiod_get_array (&pdev->dev,NULL ,GPIOD_OUT_LOW ); -if (IS_ERR (s->gpios)) -return PTR_ERR (s->gpios); + s->gpios = gpios; r =mdio_mux_init (&pdev->dev, pdev->dev.of_node, mdio_mux_gpio_switch_fn, &s->mux_handle, s,NULL );
Signature · 6 lines
2.17.1 -- Kees Cook Pixel Security
Quoted 3 lines · Kees Cook
In the quest to remove all stack VLA usage from the kernel[1], this allocates the values buffer during the callback instead of putting it on the stack.
[]
drivers/net/phy/mdio-mux-gpio.c
Quoted 1 line
drivers/net/phy/mdio-mux-gpio.c +0
-0
[]
drivers/net/phy/mdio-mux-gpio.c static int mdio_mux_gpio_switch_fn(int current_child, int desired_child, · line 45
Quoted 3 lines
drivers/net/phy/mdio-mux-gpio.c +0
-0@@ -44,15 +44,21 @@ static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,static int mdio_mux_gpio_probe (struct platform_device *pdev) {
[]
drivers/net/phy/mdio-mux-gpio.c static int mdio_mux_gpio_switch_fn(int current_child, int desired_child, · line 55
Quoted 2 lines
drivers/net/phy/mdio-mux-gpio.c +2
-0+ s =devm_kzalloc (&pdev->dev,sizeof (*s->values) * gpios->ndescs + +sizeof (*s),GFP_KERNEL );
Isn't this supposed to use your new struct_size()
Quoted 9 lines · Joe Perches
Quoted 3 lines · Kees Cook
In the quest to remove all stack VLA usage from the kernel[1], this allocates the values buffer during the callback instead of putting it on the stack.
[]
drivers/net/phy/mdio-mux-gpio.c
Quoted 1 line
drivers/net/phy/mdio-mux-gpio.c +0
-0[]
Quoted 8 lines
drivers/net/phy/mdio-mux-gpio.c static int mdio_mux_gpio_switch_fn(int current_child, int desired_child, · line 45
Quoted 3 lines
drivers/net/phy/mdio-mux-gpio.c +0
-0@@ -44,15 +44,21 @@ static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,static int mdio_mux_gpio_probe (struct platform_device *pdev) {[]
drivers/net/phy/mdio-mux-gpio.c static int mdio_mux_gpio_switch_fn(int current_child, int desired_child, · line 55
Quoted 2 lines
drivers/net/phy/mdio-mux-gpio.c +2
-0+ s =devm_kzalloc (&pdev->dev,sizeof (*s->values) * gpios->ndescs + +sizeof (*s),GFP_KERNEL );Isn't this supposed to use your new
struct_size()
Why yes. Yes it is. :) When treewide changes meet in the night! :) I'll send a v4.
-Kees
Signature · 2 lines
Kees Cook Pixel Security