vfio/virtio: Use guard() for list_lock where applicable

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>
Reviewed-by: Yishai Hadas <yishaih@nvidia.com>
Link: https://lore.kernel.org/r/20260414200625.3601509-3-alex.williamson@nvidia.com
Signed-off-by: Alex Williamson <alex@shazbot.org>
This commit is contained in:
Alex Williamson 2026-04-14 14:06:20 -06:00 committed by Alex Williamson
parent 903570835f
commit 61fcb51fc9

View File

@ -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;
@ -550,9 +543,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;
}
@ -617,9 +611,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: