[PATCH 2/4] vfio/virtio: Use guard() for list_lock where applicable
From: Alex Williamson
Date: Tue Apr 14 2026 - 16:09:13 EST
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@xxxxxxxxxx>
---
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