Linux virtualization list
 help / color / mirror / Atom feed
From: Viresh Kumar <viresh.kumar@linaro.org>
To: Peng Fan <peng.fan@linux.dev>
Cc: Viresh Kumar <vireshk@kernel.org>,
	Linus Walleij <linusw@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>,
	linux-gpio@vger.kernel.org, virtualization@lists.linux.dev,
	Peng Fan <peng.fan@nxp.com>
Subject: Re: [PATCH] gpio: virtio: simplify with cleanup API
Date: Tue, 1 Dec 2026 13:23:36 +0530	[thread overview]
Message-ID: <aw59ALssPmsvA4Wq@vireshk-B250M-D3H> (raw)
In-Reply-To: <20261201-gpio-virtio-v1-1-3038754e9b26@nxp.com>

On 01-12-26, 13:21, Peng Fan wrote:
> 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)

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

-- 
viresh

  parent reply	other threads:[~2026-12-01  7:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aw59ALssPmsvA4Wq@vireshk-B250M-D3H \
    --to=viresh.kumar@linaro.org \
    --cc=brgl@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=peng.fan@linux.dev \
    --cc=peng.fan@nxp.com \
    --cc=vireshk@kernel.org \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox