Commit 61fcb51f authored by Alex Williamson's avatar Alex Williamson Committed by Alex Williamson
Browse files

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: default avatarAlex Williamson <alex.williamson@nvidia.com>
Reviewed-by: default avatarYishai Hadas <yishaih@nvidia.com>
Link: https://lore.kernel.org/r/20260414200625.3601509-3-alex.williamson@nvidia.com


Signed-off-by: default avatarAlex Williamson <alex@shazbot.org>
parent 90357083
Loading
Loading
Loading
Loading
+16 −21
Original line number Diff line number Diff line
@@ -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);

	scoped_guard(mutex, &migf->list_lock)
		list_add_tail(&header_buf->buf_elm, &migf->buf_list);
	mutex_unlock(&migf->list_lock);

	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);

	scoped_guard(mutex, &migf->list_lock)
		list_add_tail(&buf->buf_elm, &migf->buf_list);
	mutex_unlock(&migf->list_lock);

	return 0;

out_header: