Skip to content

Commit 68cc444

Browse files
keesdavem330
authored andcommitted
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 Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a6e65e5 commit 68cc444

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

drivers/net/phy/mdio-mux-gpio.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,45 @@
2020
struct mdio_mux_gpio_state {
2121
struct gpio_descs *gpios;
2222
void *mux_handle;
23+
int values[];
2324
};
2425

2526
static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
2627
void *data)
2728
{
2829
struct mdio_mux_gpio_state *s = data;
29-
int values[s->gpios->ndescs];
3030
unsigned int n;
3131

3232
if (current_child == desired_child)
3333
return 0;
3434

3535
for (n = 0; n < s->gpios->ndescs; n++)
36-
values[n] = (desired_child >> n) & 1;
36+
s->values[n] = (desired_child >> n) & 1;
3737

3838
gpiod_set_array_value_cansleep(s->gpios->ndescs, s->gpios->desc,
39-
values);
39+
s->values);
4040

4141
return 0;
4242
}
4343

4444
static int mdio_mux_gpio_probe(struct platform_device *pdev)
4545
{
4646
struct mdio_mux_gpio_state *s;
47+
struct gpio_descs *gpios;
4748
int r;
4849

49-
s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
50-
if (!s)
50+
gpios = gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
51+
if (IS_ERR(gpios))
52+
return PTR_ERR(gpios);
53+
54+
s = devm_kzalloc(&pdev->dev, struct_size(s, values, gpios->ndescs),
55+
GFP_KERNEL);
56+
if (!s) {
57+
gpiod_put_array(gpios);
5158
return -ENOMEM;
59+
}
5260

53-
s->gpios = gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
54-
if (IS_ERR(s->gpios))
55-
return PTR_ERR(s->gpios);
61+
s->gpios = gpios;
5662

5763
r = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
5864
mdio_mux_gpio_switch_fn, &s->mux_handle, s, NULL);

0 commit comments

Comments
 (0)