Skip to content

Commit 306ba24

Browse files
committed
Merge tag 'gpio-fixes-for-v6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux
Pull gpio fixes from Bartosz Golaszewski: - fix a memory leak in gpiolib core - fix reference leaks in gpio-amd8111 and gpio-rockchip * tag 'gpio-fixes-for-v6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux: gpio/rockchip: fix refcount leak in rockchip_gpiolib_register() gpio: amd8111: Fix PCI device reference count leak gpiolib: fix memory leak in gpiochip_setup_dev()
2 parents 57fb3f6 + 63ff545 commit 306ba24

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

drivers/gpio/gpio-amd8111.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,18 @@ static int __init amd_gpio_init(void)
226226
ioport_unmap(gp.pm);
227227
goto out;
228228
}
229+
return 0;
230+
229231
out:
232+
pci_dev_put(pdev);
230233
return err;
231234
}
232235

233236
static void __exit amd_gpio_exit(void)
234237
{
235238
gpiochip_remove(&gp.chip);
236239
ioport_unmap(gp.pm);
240+
pci_dev_put(gp.pdev);
237241
}
238242

239243
module_init(amd_gpio_init);

drivers/gpio/gpio-rockchip.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)
610610
return -ENODATA;
611611

612612
pctldev = of_pinctrl_get(pctlnp);
613+
of_node_put(pctlnp);
613614
if (!pctldev)
614615
return -ENODEV;
615616

drivers/gpio/gpiolib.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,13 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
526526
if (ret)
527527
return ret;
528528

529+
/* From this point, the .release() function cleans up gpio_device */
530+
gdev->dev.release = gpiodevice_release;
531+
529532
ret = gpiochip_sysfs_register(gdev);
530533
if (ret)
531534
goto err_remove_device;
532535

533-
/* From this point, the .release() function cleans up gpio_device */
534-
gdev->dev.release = gpiodevice_release;
535536
dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
536537
gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
537538

@@ -597,10 +598,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
597598
struct fwnode_handle *fwnode = NULL;
598599
struct gpio_device *gdev;
599600
unsigned long flags;
600-
int base = gc->base;
601601
unsigned int i;
602+
u32 ngpios = 0;
603+
int base = 0;
602604
int ret = 0;
603-
u32 ngpios;
604605

605606
if (gc->fwnode)
606607
fwnode = gc->fwnode;
@@ -647,17 +648,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
647648
else
648649
gdev->owner = THIS_MODULE;
649650

650-
gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
651-
if (!gdev->descs) {
652-
ret = -ENOMEM;
653-
goto err_free_dev_name;
654-
}
655-
656651
/*
657652
* Try the device properties if the driver didn't supply the number
658653
* of GPIO lines.
659654
*/
660-
if (gc->ngpio == 0) {
655+
ngpios = gc->ngpio;
656+
if (ngpios == 0) {
661657
ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
662658
if (ret == -ENODATA)
663659
/*
@@ -668,21 +664,27 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
668664
*/
669665
ngpios = 0;
670666
else if (ret)
671-
goto err_free_descs;
667+
goto err_free_dev_name;
672668

673669
gc->ngpio = ngpios;
674670
}
675671

676672
if (gc->ngpio == 0) {
677673
chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
678674
ret = -EINVAL;
679-
goto err_free_descs;
675+
goto err_free_dev_name;
680676
}
681677

682678
if (gc->ngpio > FASTPATH_NGPIO)
683679
chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
684680
gc->ngpio, FASTPATH_NGPIO);
685681

682+
gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
683+
if (!gdev->descs) {
684+
ret = -ENOMEM;
685+
goto err_free_dev_name;
686+
}
687+
686688
gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
687689
if (!gdev->label) {
688690
ret = -ENOMEM;
@@ -701,11 +703,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
701703
* it may be a pipe dream. It will not happen before we get rid
702704
* of the sysfs interface anyways.
703705
*/
706+
base = gc->base;
704707
if (base < 0) {
705708
base = gpiochip_find_base(gc->ngpio);
706709
if (base < 0) {
707-
ret = base;
708710
spin_unlock_irqrestore(&gpio_lock, flags);
711+
ret = base;
712+
base = 0;
709713
goto err_free_label;
710714
}
711715
/*
@@ -816,6 +820,11 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
816820
err_free_gpiochip_mask:
817821
gpiochip_remove_pin_ranges(gc);
818822
gpiochip_free_valid_mask(gc);
823+
if (gdev->dev.release) {
824+
/* release() has been registered by gpiochip_setup_dev() */
825+
put_device(&gdev->dev);
826+
goto err_print_message;
827+
}
819828
err_remove_from_list:
820829
spin_lock_irqsave(&gpio_lock, flags);
821830
list_del(&gdev->list);
@@ -829,13 +838,14 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
829838
err_free_ida:
830839
ida_free(&gpio_ida, gdev->id);
831840
err_free_gdev:
841+
kfree(gdev);
842+
err_print_message:
832843
/* failures here can mean systems won't boot... */
833844
if (ret != -EPROBE_DEFER) {
834845
pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
835-
gdev->base, gdev->base + gdev->ngpio - 1,
846+
base, base + (int)ngpios - 1,
836847
gc->label ? : "generic", ret);
837848
}
838-
kfree(gdev);
839849
return ret;
840850
}
841851
EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);

0 commit comments

Comments
 (0)