Skip to content

Commit b5b268c

Browse files
Alex Williamsonawilliam
authored andcommitted
vfio/virtio: Use guard() for migf->lock where applicable
Convert migf->lock acquisitions in virtiovf_disable_fd() and virtiovf_save_read() to use guard(). In virtiovf_save_read() this eliminates the out_unlock label and multiple goto paths by allowing direct returns, and removes the need for the done variable to double as an error carrier. 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-4-alex.williamson@nvidia.com Signed-off-by: Alex Williamson <alex@shazbot.org>
1 parent 61fcb51 commit b5b268c

1 file changed

Lines changed: 14 additions & 26 deletions

File tree

drivers/vfio/pci/virtio/migrate.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,9 @@ static void virtiovf_clean_migf_resources(struct virtiovf_migration_file *migf)
224224

225225
static void virtiovf_disable_fd(struct virtiovf_migration_file *migf)
226226
{
227-
mutex_lock(&migf->lock);
227+
guard(mutex)(&migf->lock);
228228
migf->state = VIRTIOVF_MIGF_STATE_ERROR;
229229
migf->filp->f_pos = 0;
230-
mutex_unlock(&migf->lock);
231230
}
232231

233232
static void virtiovf_disable_fds(struct virtiovf_pci_core_device *virtvdev)
@@ -385,11 +384,10 @@ static ssize_t virtiovf_save_read(struct file *filp, char __user *buf, size_t le
385384
return -ESPIPE;
386385
pos = &filp->f_pos;
387386

388-
mutex_lock(&migf->lock);
389-
if (migf->state == VIRTIOVF_MIGF_STATE_ERROR) {
390-
done = -ENODEV;
391-
goto out_unlock;
392-
}
387+
guard(mutex)(&migf->lock);
388+
389+
if (migf->state == VIRTIOVF_MIGF_STATE_ERROR)
390+
return -ENODEV;
393391

394392
while (len) {
395393
ssize_t count;
@@ -398,34 +396,24 @@ static ssize_t virtiovf_save_read(struct file *filp, char __user *buf, size_t le
398396
if (first_loop_call) {
399397
first_loop_call = false;
400398
/* Temporary end of file as part of PRE_COPY */
401-
if (end_of_data && migf->state == VIRTIOVF_MIGF_STATE_PRECOPY) {
402-
done = -ENOMSG;
403-
goto out_unlock;
404-
}
405-
if (end_of_data && migf->state != VIRTIOVF_MIGF_STATE_COMPLETE) {
406-
done = -EINVAL;
407-
goto out_unlock;
408-
}
399+
if (end_of_data && migf->state == VIRTIOVF_MIGF_STATE_PRECOPY)
400+
return -ENOMSG;
401+
if (end_of_data && migf->state != VIRTIOVF_MIGF_STATE_COMPLETE)
402+
return -EINVAL;
409403
}
410404

411405
if (end_of_data)
412-
goto out_unlock;
406+
return done;
413407

414-
if (!vhca_buf) {
415-
done = -EINVAL;
416-
goto out_unlock;
417-
}
408+
if (!vhca_buf)
409+
return -EINVAL;
418410

419411
count = virtiovf_buf_read(vhca_buf, &buf, &len, pos);
420-
if (count < 0) {
421-
done = count;
422-
goto out_unlock;
423-
}
412+
if (count < 0)
413+
return count;
424414
done += count;
425415
}
426416

427-
out_unlock:
428-
mutex_unlock(&migf->lock);
429417
return done;
430418
}
431419

0 commit comments

Comments
 (0)