All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpio: virtio: simplify with cleanup API
@ 2026-12-01  5:21 Peng Fan
  2026-12-01  5:26 ` sashiko-bot
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Peng Fan @ 2026-12-01  5:21 UTC (permalink / raw)
  To: Viresh Kumar, Linus Walleij, Bartosz Golaszewski
  Cc: linux-gpio, virtualization, Peng Fan

From: Peng Fan <peng.fan@nxp.com>

Use the scope-based cleanup API to replace the manual lock/unlock pairs
and the goto-based error paths:

 - _virtio_gpio_req(): guard(mutex) holds the per-line lock over the
   whole function and scoped_guard(mutex) covers the virtqueue add/kick
   section, so the error paths can return directly instead of jumping
   to an unlock label.
 - virtio_gpio_irq_enable()/virtio_gpio_irq_disable(): scoped_guard()
   around the disabled/masked/queue_pending updates.
 - virtio_gpio_irq_mask()/virtio_gpio_irq_unmask() and ignore_irq():
   guard(raw_spinlock) with direct returns, dropping the 'ignore'
   variable and the unlock label.
 - virtio_gpio_irq_bus_sync_unlock(): scoped_guard() with the
   raw_spinlock_irqsave class.

No functional changes.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
 drivers/gpio/gpio-virtio.c | 84 +++++++++++++++++++---------------------------
 1 file changed, 34 insertions(+), 50 deletions(-)

diff --git a/drivers/gpio/gpio-virtio.c b/drivers/gpio/gpio-virtio.c
index 1bd532caffae..ff4b694db9da 100644
--- a/drivers/gpio/gpio-virtio.c
+++ b/drivers/gpio/gpio-virtio.c
@@ -9,6 +9,7 @@
  * Viresh Kumar <viresh.kumar@linaro.org>
  */
 
+#include <linux/cleanup.h>
 #include <linux/completion.h>
 #include <linux/dma-mapping.h>
 #include <linux/err.h>
@@ -79,7 +80,7 @@ static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
 	 * Linux always accesses a GPIO line sequentially, so this locking shall
 	 * always go through without any delays.
 	 */
-	mutex_lock(&line->lock);
+	guard(mutex)(&line->lock);
 
 	req->type = cpu_to_le16(type);
 	req->gpio = cpu_to_le16(gpio);
@@ -97,37 +98,31 @@ static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
 	 * Virtqueue callers need to ensure they don't call its APIs with other
 	 * virtqueue operations at the same time.
 	 */
-	mutex_lock(&vgpio->lock);
-	ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
-	if (ret) {
-		dev_err(dev, "failed to add request to vq\n");
-		mutex_unlock(&vgpio->lock);
-		goto out;
+	scoped_guard(mutex, &vgpio->lock) {
+		ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
+		if (ret) {
+			dev_err(dev, "failed to add request to vq\n");
+			return ret;
+		}
+		virtqueue_kick(vgpio->request_vq);
 	}
 
-	virtqueue_kick(vgpio->request_vq);
-	mutex_unlock(&vgpio->lock);
-
 	wait_for_completion(&line->completion);
 
 	if (unlikely(res->status != VIRTIO_GPIO_STATUS_OK)) {
 		dev_err(dev, "GPIO request failed: %d\n", gpio);
-		ret = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	if (unlikely(line->rxlen != rxlen)) {
 		dev_err(dev, "GPIO operation returned incorrect len (%u : %u)\n",
 			rxlen, line->rxlen);
-		ret = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	if (rxvalue)
 		*rxvalue = res->value;
 
-out:
-	mutex_unlock(&line->lock);
 	return ret;
 }
 
@@ -246,11 +241,11 @@ static void virtio_gpio_irq_enable(struct irq_data *d)
 
 	gpiochip_enable_irq(gc, d->hwirq);
 
-	raw_spin_lock(&vgpio->eventq_lock);
-	irq_line->disabled = false;
-	irq_line->masked = false;
-	irq_line->queue_pending = true;
-	raw_spin_unlock(&vgpio->eventq_lock);
+	scoped_guard(raw_spinlock, &vgpio->eventq_lock) {
+		irq_line->disabled = false;
+		irq_line->masked = false;
+		irq_line->queue_pending = true;
+	}
 
 	irq_line->update_pending = true;
 }
@@ -261,11 +256,11 @@ static void virtio_gpio_irq_disable(struct irq_data *d)
 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
 
-	raw_spin_lock(&vgpio->eventq_lock);
-	irq_line->disabled = true;
-	irq_line->masked = true;
-	irq_line->queue_pending = false;
-	raw_spin_unlock(&vgpio->eventq_lock);
+	scoped_guard(raw_spinlock, &vgpio->eventq_lock) {
+		irq_line->disabled = true;
+		irq_line->masked = true;
+		irq_line->queue_pending = false;
+	}
 
 	irq_line->update_pending = true;
 
@@ -278,9 +273,8 @@ static void virtio_gpio_irq_mask(struct irq_data *d)
 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
 
-	raw_spin_lock(&vgpio->eventq_lock);
+	guard(raw_spinlock)(&vgpio->eventq_lock);
 	irq_line->masked = true;
-	raw_spin_unlock(&vgpio->eventq_lock);
 }
 
 static void virtio_gpio_irq_unmask(struct irq_data *d)
@@ -289,12 +283,11 @@ static void virtio_gpio_irq_unmask(struct irq_data *d)
 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
 
-	raw_spin_lock(&vgpio->eventq_lock);
+	guard(raw_spinlock)(&vgpio->eventq_lock);
 	irq_line->masked = false;
 
 	/* Queue the buffer unconditionally on unmask */
 	virtio_gpio_irq_prepare(vgpio, d->hwirq);
-	raw_spin_unlock(&vgpio->eventq_lock);
 }
 
 static int virtio_gpio_irq_set_type(struct irq_data *d, unsigned int type)
@@ -344,7 +337,6 @@ static void virtio_gpio_irq_bus_sync_unlock(struct irq_data *d)
 	struct virtio_gpio *vgpio = gpiochip_get_data(gc);
 	struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
 	u8 type = irq_line->disabled ? VIRTIO_GPIO_IRQ_TYPE_NONE : irq_line->type;
-	unsigned long flags;
 
 	if (irq_line->update_pending) {
 		irq_line->update_pending = false;
@@ -352,12 +344,12 @@ static void virtio_gpio_irq_bus_sync_unlock(struct irq_data *d)
 				NULL);
 
 		/* Queue the buffer only after interrupt is enabled */
-		raw_spin_lock_irqsave(&vgpio->eventq_lock, flags);
-		if (irq_line->queue_pending) {
-			irq_line->queue_pending = false;
-			virtio_gpio_irq_prepare(vgpio, d->hwirq);
+		scoped_guard(raw_spinlock_irqsave, &vgpio->eventq_lock) {
+			if (irq_line->queue_pending) {
+				irq_line->queue_pending = false;
+				virtio_gpio_irq_prepare(vgpio, d->hwirq);
+			}
 		}
-		raw_spin_unlock_irqrestore(&vgpio->eventq_lock, flags);
 	}
 
 	mutex_unlock(&vgpio->irq_lock);
@@ -366,16 +358,12 @@ static void virtio_gpio_irq_bus_sync_unlock(struct irq_data *d)
 static bool ignore_irq(struct virtio_gpio *vgpio, int gpio,
 		       struct vgpio_irq_line *irq_line)
 {
-	bool ignore = false;
-
-	raw_spin_lock(&vgpio->eventq_lock);
+	guard(raw_spinlock)(&vgpio->eventq_lock);
 	irq_line->queued = false;
 
 	/* Interrupt is disabled currently */
-	if (irq_line->masked || irq_line->disabled) {
-		ignore = true;
-		goto unlock;
-	}
+	if (irq_line->masked || irq_line->disabled)
+		return true;
 
 	/*
 	 * Buffer is returned as the interrupt was disabled earlier, but is
@@ -383,17 +371,13 @@ static bool ignore_irq(struct virtio_gpio *vgpio, int gpio,
 	 */
 	if (irq_line->ires.status == VIRTIO_GPIO_IRQ_STATUS_INVALID) {
 		virtio_gpio_irq_prepare(vgpio, gpio);
-		ignore = true;
-		goto unlock;
+		return true;
 	}
 
 	if (WARN_ON(irq_line->ires.status != VIRTIO_GPIO_IRQ_STATUS_VALID))
-		ignore = true;
-
-unlock:
-	raw_spin_unlock(&vgpio->eventq_lock);
+		return true;
 
-	return ignore;
+	return false;
 }
 
 static void virtio_gpio_event_vq(struct virtqueue *vq)

---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261201-gpio-virtio-5854ed485b25

Best regards,
-- 
Peng Fan <peng.fan@nxp.com>


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-12-01 13:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  5:21 [PATCH] gpio: virtio: simplify with cleanup API Peng Fan
2026-12-01  5:26 ` sashiko-bot
2026-12-01  7:53 ` Viresh Kumar
2026-12-01  9:50   ` Bartosz Golaszewski
2026-12-01 10:09     ` Viresh Kumar
2026-12-01  9:52 ` Bartosz Golaszewski
2026-12-01 11:16   ` Peng Fan
2026-12-01 13:22     ` Bartosz Golaszewski
2026-12-01 13:22 ` Bartosz Golaszewski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.