From: Alex Williamson <alex.williamson@nvidia.com>
To: <alex@shazbot.org>
Cc: Alex Williamson <alex.williamson@nvidia.com>,
<yishaih@nvidia.com>, <kvm@vger.kernel.org>,
<guojinhui.liam@bytedance.com>, <linux-kernel@vger.kernel.org>,
<virtualization@lists.linux.dev>
Subject: [PATCH 2/4] vfio/virtio: Use guard() for list_lock where applicable
Date: Tue, 14 Apr 2026 14:06:20 -0600 [thread overview]
Message-ID: <20260414200625.3601509-3-alex.williamson@nvidia.com> (raw)
In-Reply-To: <20260414200625.3601509-1-alex.williamson@nvidia.com>
Convert list_lock mutex acquisitions to use guard() and scoped_guard()
where the lock scope aligns with the function or block scope. This
simplifies virtiovf_get_data_buff_from_pos() by replacing goto-based
unwinding with direct returns.
Assisted-by: Claude:claude-opus-4-6
Signed-off-by: Alex Williamson <alex.williamson@nvidia.com>
---
drivers/vfio/pci/virtio/migrate.c | 37 +++++++++++++------------------
1 file changed, 16 insertions(+), 21 deletions(-)
diff --git a/drivers/vfio/pci/virtio/migrate.c b/drivers/vfio/pci/virtio/migrate.c
index 15fcd936528b..058d359c9b5a 100644
--- a/drivers/vfio/pci/virtio/migrate.c
+++ b/drivers/vfio/pci/virtio/migrate.c
@@ -142,9 +142,8 @@ virtiovf_alloc_data_buffer(struct virtiovf_migration_file *migf, size_t length)
static void virtiovf_put_data_buffer(struct virtiovf_data_buffer *buf)
{
- mutex_lock(&buf->migf->list_lock);
+ guard(mutex)(&buf->migf->list_lock);
list_add_tail(&buf->buf_elm, &buf->migf->avail_list);
- mutex_unlock(&buf->migf->list_lock);
}
static int
@@ -306,32 +305,27 @@ virtiovf_get_data_buff_from_pos(struct virtiovf_migration_file *migf,
loff_t pos, bool *end_of_data)
{
struct virtiovf_data_buffer *buf;
- bool found = false;
*end_of_data = false;
- mutex_lock(&migf->list_lock);
+ guard(mutex)(&migf->list_lock);
+
if (list_empty(&migf->buf_list)) {
*end_of_data = true;
- goto end;
+ return NULL;
}
buf = list_first_entry(&migf->buf_list, struct virtiovf_data_buffer,
buf_elm);
if (pos >= buf->start_pos &&
- pos < buf->start_pos + buf->length) {
- found = true;
- goto end;
- }
+ pos < buf->start_pos + buf->length)
+ return buf;
/*
* As we use a stream based FD we may expect having the data always
* on first chunk
*/
migf->state = VIRTIOVF_MIGF_STATE_ERROR;
-
-end:
- mutex_unlock(&migf->list_lock);
- return found ? buf : NULL;
+ return NULL;
}
static ssize_t virtiovf_buf_read(struct virtiovf_data_buffer *vhca_buf,
@@ -370,10 +364,9 @@ static ssize_t virtiovf_buf_read(struct virtiovf_data_buffer *vhca_buf,
}
if (*pos >= vhca_buf->start_pos + vhca_buf->length) {
- mutex_lock(&vhca_buf->migf->list_lock);
+ guard(mutex)(&vhca_buf->migf->list_lock);
list_del_init(&vhca_buf->buf_elm);
list_add_tail(&vhca_buf->buf_elm, &vhca_buf->migf->avail_list);
- mutex_unlock(&vhca_buf->migf->list_lock);
}
return done;
@@ -555,9 +548,10 @@ virtiovf_add_buf_header(struct virtiovf_data_buffer *header_buf,
header_buf->length = sizeof(header);
header_buf->start_pos = header_buf->migf->max_pos;
migf->max_pos += header_buf->length;
- mutex_lock(&migf->list_lock);
- list_add_tail(&header_buf->buf_elm, &migf->buf_list);
- mutex_unlock(&migf->list_lock);
+
+ scoped_guard(mutex, &migf->list_lock)
+ list_add_tail(&header_buf->buf_elm, &migf->buf_list);
+
return 0;
}
@@ -622,9 +616,10 @@ virtiovf_read_device_context_chunk(struct virtiovf_migration_file *migf,
buf->start_pos = buf->migf->max_pos;
migf->max_pos += buf->length;
- mutex_lock(&migf->list_lock);
- list_add_tail(&buf->buf_elm, &migf->buf_list);
- mutex_unlock(&migf->list_lock);
+
+ scoped_guard(mutex, &migf->list_lock)
+ list_add_tail(&buf->buf_elm, &migf->buf_list);
+
return 0;
out_header:
--
2.51.0
next prev parent reply other threads:[~2026-04-14 20:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-14 20:06 [PATCH 0/4] vfio/virtio: Fix list_lock type and modernize locking Alex Williamson
2026-04-14 20:06 ` [PATCH 1/4] vfio/virtio: Convert list_lock from spinlock to mutex Alex Williamson
2026-04-14 20:06 ` Alex Williamson [this message]
2026-04-14 20:06 ` [PATCH 3/4] vfio/virtio: Use guard() for migf->lock where applicable Alex Williamson
2026-04-14 20:06 ` [PATCH 4/4] vfio/virtio: Use guard() for bar_mutex in legacy I/O Alex Williamson
2026-04-15 15:12 ` [PATCH 0/4] vfio/virtio: Fix list_lock type and modernize locking Yishai Hadas
2026-04-15 15:39 ` Alex Williamson
2026-04-15 17:23 ` Yishai Hadas
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=20260414200625.3601509-3-alex.williamson@nvidia.com \
--to=alex.williamson@nvidia.com \
--cc=alex@shazbot.org \
--cc=guojinhui.liam@bytedance.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=virtualization@lists.linux.dev \
--cc=yishaih@nvidia.com \
/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