Skip to content

Commit 61fcb51

Browse files
Alex Williamsonawilliam
authored andcommitted
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 <[email protected]> Reviewed-by: Yishai Hadas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 9035708 commit 61fcb51

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

drivers/vfio/pci/virtio/migrate.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ virtiovf_alloc_data_buffer(struct virtiovf_migration_file *migf, size_t length)
142142

143143
static void virtiovf_put_data_buffer(struct virtiovf_data_buffer *buf)
144144
{
145-
mutex_lock(&buf->migf->list_lock);
145+
guard(mutex)(&buf->migf->list_lock);
146146
list_add_tail(&buf->buf_elm, &buf->migf->avail_list);
147-
mutex_unlock(&buf->migf->list_lock);
148147
}
149148

150149
static int
@@ -306,32 +305,27 @@ virtiovf_get_data_buff_from_pos(struct virtiovf_migration_file *migf,
306305
loff_t pos, bool *end_of_data)
307306
{
308307
struct virtiovf_data_buffer *buf;
309-
bool found = false;
310308

311309
*end_of_data = false;
312-
mutex_lock(&migf->list_lock);
310+
guard(mutex)(&migf->list_lock);
311+
313312
if (list_empty(&migf->buf_list)) {
314313
*end_of_data = true;
315-
goto end;
314+
return NULL;
316315
}
317316

318317
buf = list_first_entry(&migf->buf_list, struct virtiovf_data_buffer,
319318
buf_elm);
320319
if (pos >= buf->start_pos &&
321-
pos < buf->start_pos + buf->length) {
322-
found = true;
323-
goto end;
324-
}
320+
pos < buf->start_pos + buf->length)
321+
return buf;
325322

326323
/*
327324
* As we use a stream based FD we may expect having the data always
328325
* on first chunk
329326
*/
330327
migf->state = VIRTIOVF_MIGF_STATE_ERROR;
331-
332-
end:
333-
mutex_unlock(&migf->list_lock);
334-
return found ? buf : NULL;
328+
return NULL;
335329
}
336330

337331
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,
370364
}
371365

372366
if (*pos >= vhca_buf->start_pos + vhca_buf->length) {
373-
mutex_lock(&vhca_buf->migf->list_lock);
367+
guard(mutex)(&vhca_buf->migf->list_lock);
374368
list_del_init(&vhca_buf->buf_elm);
375369
list_add_tail(&vhca_buf->buf_elm, &vhca_buf->migf->avail_list);
376-
mutex_unlock(&vhca_buf->migf->list_lock);
377370
}
378371

379372
return done;
@@ -550,9 +543,10 @@ virtiovf_add_buf_header(struct virtiovf_data_buffer *header_buf,
550543
header_buf->length = sizeof(header);
551544
header_buf->start_pos = header_buf->migf->max_pos;
552545
migf->max_pos += header_buf->length;
553-
mutex_lock(&migf->list_lock);
554-
list_add_tail(&header_buf->buf_elm, &migf->buf_list);
555-
mutex_unlock(&migf->list_lock);
546+
547+
scoped_guard(mutex, &migf->list_lock)
548+
list_add_tail(&header_buf->buf_elm, &migf->buf_list);
549+
556550
return 0;
557551
}
558552

@@ -617,9 +611,10 @@ virtiovf_read_device_context_chunk(struct virtiovf_migration_file *migf,
617611

618612
buf->start_pos = buf->migf->max_pos;
619613
migf->max_pos += buf->length;
620-
mutex_lock(&migf->list_lock);
621-
list_add_tail(&buf->buf_elm, &migf->buf_list);
622-
mutex_unlock(&migf->list_lock);
614+
615+
scoped_guard(mutex, &migf->list_lock)
616+
list_add_tail(&buf->buf_elm, &migf->buf_list);
617+
623618
return 0;
624619

625620
out_header:

0 commit comments

Comments
 (0)