Skip to content

Commit 594aef6

Browse files
committed
Merge tag 'gpio-v4.9-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio
Pull GPIO fixes from Linus Walleij: "Some GPIO fixes for the v4.9 series: - Fix a nasty file descriptor leak when getting line handles. - A fix for a cleanup that seemed innocent but created a problem for drivers instantiating several gpiochips for one single OF node. - Fix a unpredictable problem using irq_domain_simple() in the mvebu driver by converting it to a lineas irqdomain" * tag 'gpio-v4.9-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: gpio/mvebu: Use irq_domain_add_linear gpio: of: fix GPIO drivers with multiple gpio_chip for a single node gpio: GPIO_GET_LINE{HANDLE,EVENT}_IOCTL: Fix file descriptor leak
2 parents fb415f2 + 812d478 commit 594aef6

3 files changed

Lines changed: 97 additions & 66 deletions

File tree

drivers/gpio/gpio-mvebu.c

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ static void mvebu_gpio_irq_ack(struct irq_data *d)
293293
{
294294
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
295295
struct mvebu_gpio_chip *mvchip = gc->private;
296-
u32 mask = ~(1 << (d->irq - gc->irq_base));
296+
u32 mask = d->mask;
297297

298298
irq_gc_lock(gc);
299-
writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
299+
writel_relaxed(~mask, mvebu_gpioreg_edge_cause(mvchip));
300300
irq_gc_unlock(gc);
301301
}
302302

@@ -305,7 +305,7 @@ static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
305305
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
306306
struct mvebu_gpio_chip *mvchip = gc->private;
307307
struct irq_chip_type *ct = irq_data_get_chip_type(d);
308-
u32 mask = 1 << (d->irq - gc->irq_base);
308+
u32 mask = d->mask;
309309

310310
irq_gc_lock(gc);
311311
ct->mask_cache_priv &= ~mask;
@@ -319,8 +319,7 @@ static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
319319
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
320320
struct mvebu_gpio_chip *mvchip = gc->private;
321321
struct irq_chip_type *ct = irq_data_get_chip_type(d);
322-
323-
u32 mask = 1 << (d->irq - gc->irq_base);
322+
u32 mask = d->mask;
324323

325324
irq_gc_lock(gc);
326325
ct->mask_cache_priv |= mask;
@@ -333,8 +332,7 @@ static void mvebu_gpio_level_irq_mask(struct irq_data *d)
333332
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
334333
struct mvebu_gpio_chip *mvchip = gc->private;
335334
struct irq_chip_type *ct = irq_data_get_chip_type(d);
336-
337-
u32 mask = 1 << (d->irq - gc->irq_base);
335+
u32 mask = d->mask;
338336

339337
irq_gc_lock(gc);
340338
ct->mask_cache_priv &= ~mask;
@@ -347,8 +345,7 @@ static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
347345
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
348346
struct mvebu_gpio_chip *mvchip = gc->private;
349347
struct irq_chip_type *ct = irq_data_get_chip_type(d);
350-
351-
u32 mask = 1 << (d->irq - gc->irq_base);
348+
u32 mask = d->mask;
352349

353350
irq_gc_lock(gc);
354351
ct->mask_cache_priv |= mask;
@@ -462,7 +459,7 @@ static void mvebu_gpio_irq_handler(struct irq_desc *desc)
462459
for (i = 0; i < mvchip->chip.ngpio; i++) {
463460
int irq;
464461

465-
irq = mvchip->irqbase + i;
462+
irq = irq_find_mapping(mvchip->domain, i);
466463

467464
if (!(cause & (1 << i)))
468465
continue;
@@ -655,6 +652,7 @@ static int mvebu_gpio_probe(struct platform_device *pdev)
655652
struct irq_chip_type *ct;
656653
struct clk *clk;
657654
unsigned int ngpios;
655+
bool have_irqs;
658656
int soc_variant;
659657
int i, cpu, id;
660658
int err;
@@ -665,6 +663,9 @@ static int mvebu_gpio_probe(struct platform_device *pdev)
665663
else
666664
soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
667665

666+
/* Some gpio controllers do not provide irq support */
667+
have_irqs = of_irq_count(np) != 0;
668+
668669
mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
669670
GFP_KERNEL);
670671
if (!mvchip)
@@ -697,7 +698,8 @@ static int mvebu_gpio_probe(struct platform_device *pdev)
697698
mvchip->chip.get = mvebu_gpio_get;
698699
mvchip->chip.direction_output = mvebu_gpio_direction_output;
699700
mvchip->chip.set = mvebu_gpio_set;
700-
mvchip->chip.to_irq = mvebu_gpio_to_irq;
701+
if (have_irqs)
702+
mvchip->chip.to_irq = mvebu_gpio_to_irq;
701703
mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
702704
mvchip->chip.ngpio = ngpios;
703705
mvchip->chip.can_sleep = false;
@@ -758,34 +760,30 @@ static int mvebu_gpio_probe(struct platform_device *pdev)
758760
devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
759761

760762
/* Some gpio controllers do not provide irq support */
761-
if (!of_irq_count(np))
763+
if (!have_irqs)
762764
return 0;
763765

764-
/* Setup the interrupt handlers. Each chip can have up to 4
765-
* interrupt handlers, with each handler dealing with 8 GPIO
766-
* pins. */
767-
for (i = 0; i < 4; i++) {
768-
int irq = platform_get_irq(pdev, i);
769-
770-
if (irq < 0)
771-
continue;
772-
irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
773-
mvchip);
774-
}
775-
776-
mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
777-
if (mvchip->irqbase < 0) {
778-
dev_err(&pdev->dev, "no irqs\n");
779-
return mvchip->irqbase;
766+
mvchip->domain =
767+
irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
768+
if (!mvchip->domain) {
769+
dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
770+
mvchip->chip.label);
771+
return -ENODEV;
780772
}
781773

782-
gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
783-
mvchip->membase, handle_level_irq);
784-
if (!gc) {
785-
dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
786-
return -ENOMEM;
774+
err = irq_alloc_domain_generic_chips(
775+
mvchip->domain, ngpios, 2, np->name, handle_level_irq,
776+
IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
777+
if (err) {
778+
dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
779+
mvchip->chip.label);
780+
goto err_domain;
787781
}
788782

783+
/* NOTE: The common accessors cannot be used because of the percpu
784+
* access to the mask registers
785+
*/
786+
gc = irq_get_domain_generic_chip(mvchip->domain, 0);
789787
gc->private = mvchip;
790788
ct = &gc->chip_types[0];
791789
ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
@@ -803,27 +801,23 @@ static int mvebu_gpio_probe(struct platform_device *pdev)
803801
ct->handler = handle_edge_irq;
804802
ct->chip.name = mvchip->chip.label;
805803

806-
irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
807-
IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
804+
/* Setup the interrupt handlers. Each chip can have up to 4
805+
* interrupt handlers, with each handler dealing with 8 GPIO
806+
* pins.
807+
*/
808+
for (i = 0; i < 4; i++) {
809+
int irq = platform_get_irq(pdev, i);
808810

809-
/* Setup irq domain on top of the generic chip. */
810-
mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
811-
mvchip->irqbase,
812-
&irq_domain_simple_ops,
813-
mvchip);
814-
if (!mvchip->domain) {
815-
dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
816-
mvchip->chip.label);
817-
err = -ENODEV;
818-
goto err_generic_chip;
811+
if (irq < 0)
812+
continue;
813+
irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
814+
mvchip);
819815
}
820816

821817
return 0;
822818

823-
err_generic_chip:
824-
irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
825-
IRQ_LEVEL | IRQ_NOPROBE);
826-
kfree(gc);
819+
err_domain:
820+
irq_domain_remove(mvchip->domain);
827821

828822
return err;
829823
}

drivers/gpio/gpiolib-of.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@
2626

2727
#include "gpiolib.h"
2828

29-
static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
29+
static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
3030
{
31-
return chip->gpiodev->dev.of_node == data;
31+
struct of_phandle_args *gpiospec = data;
32+
33+
return chip->gpiodev->dev.of_node == gpiospec->np &&
34+
chip->of_xlate(chip, gpiospec, NULL) >= 0;
3235
}
3336

34-
static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
37+
static struct gpio_chip *of_find_gpiochip_by_xlate(
38+
struct of_phandle_args *gpiospec)
3539
{
36-
return gpiochip_find(np, of_gpiochip_match_node);
40+
return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
3741
}
3842

3943
static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
@@ -79,7 +83,7 @@ struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
7983
return ERR_PTR(ret);
8084
}
8185

82-
chip = of_find_gpiochip_by_node(gpiospec.np);
86+
chip = of_find_gpiochip_by_xlate(&gpiospec);
8387
if (!chip) {
8488
desc = ERR_PTR(-EPROBE_DEFER);
8589
goto out;

drivers/gpio/gpiolib.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/uaccess.h>
2222
#include <linux/compat.h>
2323
#include <linux/anon_inodes.h>
24+
#include <linux/file.h>
2425
#include <linux/kfifo.h>
2526
#include <linux/poll.h>
2627
#include <linux/timekeeping.h>
@@ -423,6 +424,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
423424
{
424425
struct gpiohandle_request handlereq;
425426
struct linehandle_state *lh;
427+
struct file *file;
426428
int fd, i, ret;
427429

428430
if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
@@ -499,26 +501,41 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
499501
i--;
500502
lh->numdescs = handlereq.lines;
501503

502-
fd = anon_inode_getfd("gpio-linehandle",
503-
&linehandle_fileops,
504-
lh,
505-
O_RDONLY | O_CLOEXEC);
504+
fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
506505
if (fd < 0) {
507506
ret = fd;
508507
goto out_free_descs;
509508
}
510509

510+
file = anon_inode_getfile("gpio-linehandle",
511+
&linehandle_fileops,
512+
lh,
513+
O_RDONLY | O_CLOEXEC);
514+
if (IS_ERR(file)) {
515+
ret = PTR_ERR(file);
516+
goto out_put_unused_fd;
517+
}
518+
511519
handlereq.fd = fd;
512520
if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
513-
ret = -EFAULT;
514-
goto out_free_descs;
521+
/*
522+
* fput() will trigger the release() callback, so do not go onto
523+
* the regular error cleanup path here.
524+
*/
525+
fput(file);
526+
put_unused_fd(fd);
527+
return -EFAULT;
515528
}
516529

530+
fd_install(fd, file);
531+
517532
dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
518533
lh->numdescs);
519534

520535
return 0;
521536

537+
out_put_unused_fd:
538+
put_unused_fd(fd);
522539
out_free_descs:
523540
for (; i >= 0; i--)
524541
gpiod_free(lh->descs[i]);
@@ -721,6 +738,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
721738
struct gpioevent_request eventreq;
722739
struct lineevent_state *le;
723740
struct gpio_desc *desc;
741+
struct file *file;
724742
u32 offset;
725743
u32 lflags;
726744
u32 eflags;
@@ -815,23 +833,38 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
815833
if (ret)
816834
goto out_free_desc;
817835

818-
fd = anon_inode_getfd("gpio-event",
819-
&lineevent_fileops,
820-
le,
821-
O_RDONLY | O_CLOEXEC);
836+
fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
822837
if (fd < 0) {
823838
ret = fd;
824839
goto out_free_irq;
825840
}
826841

842+
file = anon_inode_getfile("gpio-event",
843+
&lineevent_fileops,
844+
le,
845+
O_RDONLY | O_CLOEXEC);
846+
if (IS_ERR(file)) {
847+
ret = PTR_ERR(file);
848+
goto out_put_unused_fd;
849+
}
850+
827851
eventreq.fd = fd;
828852
if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
829-
ret = -EFAULT;
830-
goto out_free_irq;
853+
/*
854+
* fput() will trigger the release() callback, so do not go onto
855+
* the regular error cleanup path here.
856+
*/
857+
fput(file);
858+
put_unused_fd(fd);
859+
return -EFAULT;
831860
}
832861

862+
fd_install(fd, file);
863+
833864
return 0;
834865

866+
out_put_unused_fd:
867+
put_unused_fd(fd);
835868
out_free_irq:
836869
free_irq(le->irq, le);
837870
out_free_desc:

0 commit comments

Comments
 (0)