Skip to content

Commit cb04444

Browse files
Yishai Hadasawilliam
authored andcommitted
vfio/mlx5: Fix unwind flows in mlx5vf_pci_save/resume_device_data()
Fix unwind flows in mlx5vf_pci_save_device_data() and mlx5vf_pci_resume_device_data() to avoid freeing the migf pointer at the 'end' label, as this will be handled by fput(migf->filp) through mlx5vf_release_file(). To ensure mlx5vf_release_file() functions correctly, move the initialization of migf fields (such as migf->lock) to occur before any potential unwind flow, as these fields may be accessed within mlx5vf_release_file(). Fixes: 9945a67 ("vfio/mlx5: Refactor PD usage") Signed-off-by: Yishai Hadas <yishaih@nvidia.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20241114095318.16556-3-yishaih@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 22e87bf commit cb04444

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

drivers/vfio/pci/mlx5/main.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -640,14 +640,11 @@ mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev, bool track)
640640
O_RDONLY);
641641
if (IS_ERR(migf->filp)) {
642642
ret = PTR_ERR(migf->filp);
643-
goto end;
643+
kfree(migf);
644+
return ERR_PTR(ret);
644645
}
645646

646647
migf->mvdev = mvdev;
647-
ret = mlx5vf_cmd_alloc_pd(migf);
648-
if (ret)
649-
goto out_free;
650-
651648
stream_open(migf->filp->f_inode, migf->filp);
652649
mutex_init(&migf->lock);
653650
init_waitqueue_head(&migf->poll_wait);
@@ -663,6 +660,11 @@ mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev, bool track)
663660
INIT_LIST_HEAD(&migf->buf_list);
664661
INIT_LIST_HEAD(&migf->avail_list);
665662
spin_lock_init(&migf->list_lock);
663+
664+
ret = mlx5vf_cmd_alloc_pd(migf);
665+
if (ret)
666+
goto out;
667+
666668
ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &length, &full_size, 0);
667669
if (ret)
668670
goto out_pd;
@@ -692,10 +694,8 @@ mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev, bool track)
692694
mlx5vf_free_data_buffer(buf);
693695
out_pd:
694696
mlx5fv_cmd_clean_migf_resources(migf);
695-
out_free:
697+
out:
696698
fput(migf->filp);
697-
end:
698-
kfree(migf);
699699
return ERR_PTR(ret);
700700
}
701701

@@ -1016,13 +1016,19 @@ mlx5vf_pci_resume_device_data(struct mlx5vf_pci_core_device *mvdev)
10161016
O_WRONLY);
10171017
if (IS_ERR(migf->filp)) {
10181018
ret = PTR_ERR(migf->filp);
1019-
goto end;
1019+
kfree(migf);
1020+
return ERR_PTR(ret);
10201021
}
10211022

1023+
stream_open(migf->filp->f_inode, migf->filp);
1024+
mutex_init(&migf->lock);
1025+
INIT_LIST_HEAD(&migf->buf_list);
1026+
INIT_LIST_HEAD(&migf->avail_list);
1027+
spin_lock_init(&migf->list_lock);
10221028
migf->mvdev = mvdev;
10231029
ret = mlx5vf_cmd_alloc_pd(migf);
10241030
if (ret)
1025-
goto out_free;
1031+
goto out;
10261032

10271033
buf = mlx5vf_alloc_data_buffer(migf, 0, DMA_TO_DEVICE);
10281034
if (IS_ERR(buf)) {
@@ -1041,20 +1047,13 @@ mlx5vf_pci_resume_device_data(struct mlx5vf_pci_core_device *mvdev)
10411047
migf->buf_header[0] = buf;
10421048
migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
10431049

1044-
stream_open(migf->filp->f_inode, migf->filp);
1045-
mutex_init(&migf->lock);
1046-
INIT_LIST_HEAD(&migf->buf_list);
1047-
INIT_LIST_HEAD(&migf->avail_list);
1048-
spin_lock_init(&migf->list_lock);
10491050
return migf;
10501051
out_buf:
10511052
mlx5vf_free_data_buffer(migf->buf[0]);
10521053
out_pd:
10531054
mlx5vf_cmd_dealloc_pd(migf);
1054-
out_free:
1055+
out:
10551056
fput(migf->filp);
1056-
end:
1057-
kfree(migf);
10581057
return ERR_PTR(ret);
10591058
}
10601059

0 commit comments

Comments
 (0)