]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
mdio-mux-gpio: Remove VLA usage
authorKees Cook <keescook@chromium.org>
Mon, 25 Jun 2018 22:49:49 +0000 (15:49 -0700)
committerDavid S. Miller <davem@davemloft.net>
Tue, 26 Jun 2018 14:24:07 +0000 (23:24 +0900)
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>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/mdio-mux-gpio.c

index 082ffef0dec4e31e6f92f7b11ad020a7b9a16376..bc90764a8b8dcd7dd7140c20f39e8538d47de23e 100644 (file)
 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, struct_size(s, values, gpios->ndescs),
+                        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);