* [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count
@ 2025-09-28 5:40 Alex Tran
2025-10-01 8:29 ` Linus Walleij
2025-10-01 12:24 ` Bartosz Golaszewski
0 siblings, 2 replies; 4+ messages in thread
From: Alex Tran @ 2025-09-28 5:40 UTC (permalink / raw)
To: Linus Walleij; +Cc: Bartosz Golaszewski, linux-gpio, linux-kernel, Alex Tran
Remove extraneous dropping of the lock just to call 'request_irq'
and locking again afterwards. Increment reference count
before calling 'request_irq'. Rollback reference count if
'request_irq' fails.
Signed-off-by: Alex Tran <alex.t.tran@gmail.com>
---
drivers/gpio/gpio-grgpio.c | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/drivers/gpio/gpio-grgpio.c b/drivers/gpio/gpio-grgpio.c
index 0c0f97fa14fc..18d972fddfac 100644
--- a/drivers/gpio/gpio-grgpio.c
+++ b/drivers/gpio/gpio-grgpio.c
@@ -227,6 +227,7 @@ static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
struct grgpio_priv *priv = d->host_data;
struct grgpio_lirq *lirq;
struct grgpio_uirq *uirq;
+ bool needs_req = false;
unsigned long flags;
int offset = hwirq;
int ret = 0;
@@ -242,30 +243,28 @@ static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
irq, offset);
gpio_generic_chip_lock_irqsave(&priv->chip, flags);
-
- /* Request underlying irq if not already requested */
lirq->irq = irq;
uirq = &priv->uirqs[lirq->index];
- if (uirq->refcnt == 0) {
- /*
- * FIXME: This is not how locking works at all, you can't just
- * release the lock for a moment to do something that can't
- * sleep...
- */
- gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
+ needs_req = (uirq->refcnt == 0);
+ uirq->refcnt++;
+ gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
+
+ /* Request underlying irq if not already requested */
+ if (needs_req) {
ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
dev_name(priv->dev), priv);
if (ret) {
dev_err(priv->dev,
"Could not request underlying irq %d\n",
uirq->uirq);
+
+ // rollback
+ gpio_generic_chip_lock_irqsave(&priv->chip, flags);
+ uirq->refcnt--;
+ gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
return ret;
}
- gpio_generic_chip_lock_irqsave(&priv->chip, flags);
}
- uirq->refcnt++;
-
- gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
/* Setup irq */
irq_set_chip_data(irq, priv);
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count
2025-09-28 5:40 [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count Alex Tran
@ 2025-10-01 8:29 ` Linus Walleij
2025-10-01 12:24 ` Bartosz Golaszewski
1 sibling, 0 replies; 4+ messages in thread
From: Linus Walleij @ 2025-10-01 8:29 UTC (permalink / raw)
To: Alex Tran; +Cc: Bartosz Golaszewski, linux-gpio, linux-kernel
On Sun, Sep 28, 2025 at 7:41 AM Alex Tran <alex.t.tran@gmail.com> wrote:
> Remove extraneous dropping of the lock just to call 'request_irq'
> and locking again afterwards. Increment reference count
> before calling 'request_irq'. Rollback reference count if
> 'request_irq' fails.
>
> Signed-off-by: Alex Tran <alex.t.tran@gmail.com>
This looks right.
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count
2025-09-28 5:40 [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count Alex Tran
2025-10-01 8:29 ` Linus Walleij
@ 2025-10-01 12:24 ` Bartosz Golaszewski
2025-10-02 5:41 ` Alex Tran
1 sibling, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2025-10-01 12:24 UTC (permalink / raw)
To: Alex Tran; +Cc: Bartosz Golaszewski, linux-gpio, linux-kernel, Linus Walleij
On Sun, 28 Sep 2025 07:40:19 +0200, Alex Tran <alex.t.tran@gmail.com> said:
> Remove extraneous dropping of the lock just to call 'request_irq'
> and locking again afterwards. Increment reference count
> before calling 'request_irq'. Rollback reference count if
> 'request_irq' fails.
>
> Signed-off-by: Alex Tran <alex.t.tran@gmail.com>
> ---
> drivers/gpio/gpio-grgpio.c | 25 ++++++++++++-------------
> 1 file changed, 12 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/gpio/gpio-grgpio.c b/drivers/gpio/gpio-grgpio.c
> index 0c0f97fa14fc..18d972fddfac 100644
> --- a/drivers/gpio/gpio-grgpio.c
> +++ b/drivers/gpio/gpio-grgpio.c
> @@ -227,6 +227,7 @@ static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
> struct grgpio_priv *priv = d->host_data;
> struct grgpio_lirq *lirq;
> struct grgpio_uirq *uirq;
> + bool needs_req = false;
> unsigned long flags;
> int offset = hwirq;
> int ret = 0;
> @@ -242,30 +243,28 @@ static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
> irq, offset);
>
> gpio_generic_chip_lock_irqsave(&priv->chip, flags);
> -
> - /* Request underlying irq if not already requested */
> lirq->irq = irq;
> uirq = &priv->uirqs[lirq->index];
> - if (uirq->refcnt == 0) {
> - /*
> - * FIXME: This is not how locking works at all, you can't just
> - * release the lock for a moment to do something that can't
> - * sleep...
> - */
> - gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
> + needs_req = (uirq->refcnt == 0);
> + uirq->refcnt++;
Any chance this could be made to use atomic_t instead?
Like:
if (atomic_fetch_add(1, &priv->uirqs) == 0) {
request_irq()
...
}
?
Bart
> + gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
> +
> + /* Request underlying irq if not already requested */
> + if (needs_req) {
> ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
> dev_name(priv->dev), priv);
> if (ret) {
> dev_err(priv->dev,
> "Could not request underlying irq %d\n",
> uirq->uirq);
> +
> + // rollback
> + gpio_generic_chip_lock_irqsave(&priv->chip, flags);
> + uirq->refcnt--;
> + gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
> return ret;
> }
> - gpio_generic_chip_lock_irqsave(&priv->chip, flags);
> }
> - uirq->refcnt++;
> -
> - gpio_generic_chip_unlock_irqrestore(&priv->chip, flags);
>
> /* Setup irq */
> irq_set_chip_data(irq, priv);
> --
> 2.51.0
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count
2025-10-01 12:24 ` Bartosz Golaszewski
@ 2025-10-02 5:41 ` Alex Tran
0 siblings, 0 replies; 4+ messages in thread
From: Alex Tran @ 2025-10-02 5:41 UTC (permalink / raw)
To: Bartosz Golaszewski; +Cc: linux-gpio, linux-kernel, Linus Walleij
> Any chance this could be made to use atomic_t instead?
>
> Like:
>
> if (atomic_fetch_add(1, &priv->uirqs) == 0) {
> request_irq()
> ...
> }
>
> ?
>
> Bart
Sure, the reference count can be converted to atomic_t. I'll follow up
with a v2 patch.
Thanks,
--
Alex Tran
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-10-02 5:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-28 5:40 [PATCH v1] gpio: gpio-grgpio: call request_irq after incrementing the reference count Alex Tran
2025-10-01 8:29 ` Linus Walleij
2025-10-01 12:24 ` Bartosz Golaszewski
2025-10-02 5:41 ` Alex Tran
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).