All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mdio-mux-gpio: Remove VLA usage
@ 2018-05-29 22:59 Kees Cook
  2018-05-29 23:19 ` Joe Perches
  2018-05-30  0:57 ` Andrew Lunn
  0 siblings, 2 replies; 3+ messages in thread
From: Kees Cook @ 2018-05-29 22:59 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Pramod Kumar, Andrew Lunn, Florian Fainelli, David S. Miller,
	linux-gpio, linux-kernel

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

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/net/phy/mdio-mux-gpio.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/mdio-mux-gpio.c b/drivers/net/phy/mdio-mux-gpio.c
index 082ffef0dec4..e99994f4ed50 100644
--- a/drivers/net/phy/mdio-mux-gpio.c
+++ b/drivers/net/phy/mdio-mux-gpio.c
@@ -26,18 +26,23 @@ 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];
+	int *values;
 	unsigned int n;
 
 	if (current_child == desired_child)
 		return 0;
 
+	values = kmalloc_array(s->gpios->ndescs, sizeof(*values), GFP_KERNEL);
+	if (!values)
+		return -ENOMEM;
+
 	for (n = 0; n < s->gpios->ndescs; n++)
 		values[n] = (desired_child >> n) & 1;
 
 	gpiod_set_array_value_cansleep(s->gpios->ndescs, s->gpios->desc,
 				       values);
 
+	kfree(values);
 	return 0;
 }
 
-- 
2.17.0


-- 
Kees Cook
Pixel Security

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mdio-mux-gpio: Remove VLA usage
  2018-05-29 22:59 [PATCH] mdio-mux-gpio: Remove VLA usage Kees Cook
@ 2018-05-29 23:19 ` Joe Perches
  2018-05-30  0:57 ` Andrew Lunn
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Perches @ 2018-05-29 23:19 UTC (permalink / raw)
  To: Kees Cook, Linus Walleij
  Cc: Pramod Kumar, Andrew Lunn, Florian Fainelli, David S. Miller,
	linux-gpio, linux-kernel

On Tue, 2018-05-29 at 15:59 -0700, Kees Cook wrote:
> 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
[]
> diff --git a/drivers/net/phy/mdio-mux-gpio.c b/drivers/net/phy/mdio-mux-gpio.c
[]
> @@ -26,18 +26,23 @@ 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];
> +	int *values;
>  	unsigned int n;
>  
>  	if (current_child == desired_child)
>  		return 0;
>  
> +	values = kmalloc_array(s->gpios->ndescs, sizeof(*values), GFP_KERNEL);
> +	if (!values)
> +		return -ENOMEM;

This function previously only returned 0
and now it can return -ENOMEM.

How do you know all the indirect callers
via the switch_fn stored in mdio_mux_init
can handle the new return value?

If you have explored these code paths, it
would be good to explain that in the commit
message.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mdio-mux-gpio: Remove VLA usage
  2018-05-29 22:59 [PATCH] mdio-mux-gpio: Remove VLA usage Kees Cook
  2018-05-29 23:19 ` Joe Perches
@ 2018-05-30  0:57 ` Andrew Lunn
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Lunn @ 2018-05-30  0:57 UTC (permalink / raw)
  To: Kees Cook
  Cc: Linus Walleij, Pramod Kumar, Florian Fainelli, David S. Miller,
	linux-gpio, linux-kernel

On Tue, May 29, 2018 at 03:59:01PM -0700, Kees Cook wrote:
> 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
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  drivers/net/phy/mdio-mux-gpio.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/phy/mdio-mux-gpio.c b/drivers/net/phy/mdio-mux-gpio.c
> index 082ffef0dec4..e99994f4ed50 100644
> --- a/drivers/net/phy/mdio-mux-gpio.c
> +++ b/drivers/net/phy/mdio-mux-gpio.c
> @@ -26,18 +26,23 @@ 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];
> +	int *values;
>  	unsigned int n;
>  
>  	if (current_child == desired_child)
>  		return 0;
>  
> +	values = kmalloc_array(s->gpios->ndescs, sizeof(*values), GFP_KERNEL);
> +	if (!values)
> +		return -ENOMEM;
> +
>  	for (n = 0; n < s->gpios->ndescs; n++)
>  		values[n] = (desired_child >> n) & 1;
>  
>  	gpiod_set_array_value_cansleep(s->gpios->ndescs, s->gpios->desc,
>  				       values);
>  
> +	kfree(values);
>  	return 0;
>  }

Hi Kees

This is the 'hot path' for this driver, and you are making it more
expensive. 

Please allocate the memory once in mdio_mux_gpio_probe and make it
part of mdio_mux_gpio_state.

	Thanks
		Andrew

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-05-30  0:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-29 22:59 [PATCH] mdio-mux-gpio: Remove VLA usage Kees Cook
2018-05-29 23:19 ` Joe Perches
2018-05-30  0:57 ` Andrew Lunn

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.