diff mbox series

[mlx5-next,1/5] vfio/mlx5: Reorganize the VF is migratable code

Message ID 20220427093120.161402-2-yishaih@nvidia.com (mailing list archive)
State New, archived
Headers show
Series Improve mlx5 live migration driver | expand

Commit Message

Yishai Hadas April 27, 2022, 9:31 a.m. UTC
Reorganize the VF is migratable code to be in a separate function, next
patches from the series may use this.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
 drivers/vfio/pci/mlx5/cmd.c  | 18 ++++++++++++++++++
 drivers/vfio/pci/mlx5/cmd.h  |  1 +
 drivers/vfio/pci/mlx5/main.c | 22 +++++++---------------
 3 files changed, 26 insertions(+), 15 deletions(-)

Comments

Alex Williamson May 4, 2022, 8:13 p.m. UTC | #1
On Wed, 27 Apr 2022 12:31:16 +0300
Yishai Hadas <yishaih@nvidia.com> wrote:

> Reorganize the VF is migratable code to be in a separate function, next
> patches from the series may use this.
> 
> Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
> Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
> ---
>  drivers/vfio/pci/mlx5/cmd.c  | 18 ++++++++++++++++++
>  drivers/vfio/pci/mlx5/cmd.h  |  1 +
>  drivers/vfio/pci/mlx5/main.c | 22 +++++++---------------
>  3 files changed, 26 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c
> index 5c9f9218cc1d..d608b8167f58 100644
> --- a/drivers/vfio/pci/mlx5/cmd.c
> +++ b/drivers/vfio/pci/mlx5/cmd.c
> @@ -71,6 +71,24 @@ int mlx5vf_cmd_query_vhca_migration_state(struct pci_dev *pdev, u16 vhca_id,
>  	return ret;
>  }
>  
> +bool mlx5vf_cmd_is_migratable(struct pci_dev *pdev)
> +{
> +	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
> +	bool migratable = false;
> +
> +	if (!mdev)
> +		return false;
> +
> +	if (!MLX5_CAP_GEN(mdev, migration))
> +		goto end;
> +
> +	migratable = true;
> +
> +end:
> +	mlx5_vf_put_core_dev(mdev);
> +	return migratable;
> +}

This goto seems unnecessary, couldn't it instead be written:

{
	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
	boot migratable = true;

	if (!mdev)
		return false;

	if (!MLX5_CAP_GEN(mdev, migration))
		migratable = false;

	mlx5_vf_put_core_mdev(mdev);
	return migratable;
}

Thanks,
Alex

> +
>  int mlx5vf_cmd_get_vhca_id(struct pci_dev *pdev, u16 function_id, u16 *vhca_id)
>  {
>  	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
> diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h
> index 1392a11a9cc0..2da6a1c0ec5c 100644
> --- a/drivers/vfio/pci/mlx5/cmd.h
> +++ b/drivers/vfio/pci/mlx5/cmd.h
> @@ -29,6 +29,7 @@ int mlx5vf_cmd_resume_vhca(struct pci_dev *pdev, u16 vhca_id, u16 op_mod);
>  int mlx5vf_cmd_query_vhca_migration_state(struct pci_dev *pdev, u16 vhca_id,
>  					  size_t *state_size);
>  int mlx5vf_cmd_get_vhca_id(struct pci_dev *pdev, u16 function_id, u16 *vhca_id);
> +bool mlx5vf_cmd_is_migratable(struct pci_dev *pdev);
>  int mlx5vf_cmd_save_vhca_state(struct pci_dev *pdev, u16 vhca_id,
>  			       struct mlx5_vf_migration_file *migf);
>  int mlx5vf_cmd_load_vhca_state(struct pci_dev *pdev, u16 vhca_id,
> diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
> index bbec5d288fee..2578f61eaeae 100644
> --- a/drivers/vfio/pci/mlx5/main.c
> +++ b/drivers/vfio/pci/mlx5/main.c
> @@ -597,21 +597,13 @@ static int mlx5vf_pci_probe(struct pci_dev *pdev,
>  		return -ENOMEM;
>  	vfio_pci_core_init_device(&mvdev->core_device, pdev, &mlx5vf_pci_ops);
>  
> -	if (pdev->is_virtfn) {
> -		struct mlx5_core_dev *mdev =
> -			mlx5_vf_get_core_dev(pdev);
> -
> -		if (mdev) {
> -			if (MLX5_CAP_GEN(mdev, migration)) {
> -				mvdev->migrate_cap = 1;
> -				mvdev->core_device.vdev.migration_flags =
> -					VFIO_MIGRATION_STOP_COPY |
> -					VFIO_MIGRATION_P2P;
> -				mutex_init(&mvdev->state_mutex);
> -				spin_lock_init(&mvdev->reset_lock);
> -			}
> -			mlx5_vf_put_core_dev(mdev);
> -		}
> +	if (pdev->is_virtfn && mlx5vf_cmd_is_migratable(pdev)) {
> +		mvdev->migrate_cap = 1;
> +		mvdev->core_device.vdev.migration_flags =
> +			VFIO_MIGRATION_STOP_COPY |
> +			VFIO_MIGRATION_P2P;
> +		mutex_init(&mvdev->state_mutex);
> +		spin_lock_init(&mvdev->reset_lock);
>  	}
>  
>  	ret = vfio_pci_core_register_device(&mvdev->core_device);
Yishai Hadas May 8, 2022, 12:56 p.m. UTC | #2
On 04/05/2022 23:13, Alex Williamson wrote:
> On Wed, 27 Apr 2022 12:31:16 +0300
> Yishai Hadas <yishaih@nvidia.com> wrote:
>
>> Reorganize the VF is migratable code to be in a separate function, next
>> patches from the series may use this.
>>
>> Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
>> Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
>> ---
>>   drivers/vfio/pci/mlx5/cmd.c  | 18 ++++++++++++++++++
>>   drivers/vfio/pci/mlx5/cmd.h  |  1 +
>>   drivers/vfio/pci/mlx5/main.c | 22 +++++++---------------
>>   3 files changed, 26 insertions(+), 15 deletions(-)
>>
>> diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c
>> index 5c9f9218cc1d..d608b8167f58 100644
>> --- a/drivers/vfio/pci/mlx5/cmd.c
>> +++ b/drivers/vfio/pci/mlx5/cmd.c
>> @@ -71,6 +71,24 @@ int mlx5vf_cmd_query_vhca_migration_state(struct pci_dev *pdev, u16 vhca_id,
>>   	return ret;
>>   }
>>   
>> +bool mlx5vf_cmd_is_migratable(struct pci_dev *pdev)
>> +{
>> +	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
>> +	bool migratable = false;
>> +
>> +	if (!mdev)
>> +		return false;
>> +
>> +	if (!MLX5_CAP_GEN(mdev, migration))
>> +		goto end;
>> +
>> +	migratable = true;
>> +
>> +end:
>> +	mlx5_vf_put_core_dev(mdev);
>> +	return migratable;
>> +}
> This goto seems unnecessary, couldn't it instead be written:
>
> {
> 	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
> 	boot migratable = true;
>
> 	if (!mdev)
> 		return false;
>
> 	if (!MLX5_CAP_GEN(mdev, migration))
> 		migratable = false;
>
> 	mlx5_vf_put_core_mdev(mdev);
> 	return migratable;
> }
>
> Thanks,
> Alex


V1 will handle that as part of some refactoring and combing this patch 
and patch #3 based on your notes there.

Thanks.

>
>> +
>>   int mlx5vf_cmd_get_vhca_id(struct pci_dev *pdev, u16 function_id, u16 *vhca_id)
>>   {
>>   	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
>> diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h
>> index 1392a11a9cc0..2da6a1c0ec5c 100644
>> --- a/drivers/vfio/pci/mlx5/cmd.h
>> +++ b/drivers/vfio/pci/mlx5/cmd.h
>> @@ -29,6 +29,7 @@ int mlx5vf_cmd_resume_vhca(struct pci_dev *pdev, u16 vhca_id, u16 op_mod);
>>   int mlx5vf_cmd_query_vhca_migration_state(struct pci_dev *pdev, u16 vhca_id,
>>   					  size_t *state_size);
>>   int mlx5vf_cmd_get_vhca_id(struct pci_dev *pdev, u16 function_id, u16 *vhca_id);
>> +bool mlx5vf_cmd_is_migratable(struct pci_dev *pdev);
>>   int mlx5vf_cmd_save_vhca_state(struct pci_dev *pdev, u16 vhca_id,
>>   			       struct mlx5_vf_migration_file *migf);
>>   int mlx5vf_cmd_load_vhca_state(struct pci_dev *pdev, u16 vhca_id,
>> diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
>> index bbec5d288fee..2578f61eaeae 100644
>> --- a/drivers/vfio/pci/mlx5/main.c
>> +++ b/drivers/vfio/pci/mlx5/main.c
>> @@ -597,21 +597,13 @@ static int mlx5vf_pci_probe(struct pci_dev *pdev,
>>   		return -ENOMEM;
>>   	vfio_pci_core_init_device(&mvdev->core_device, pdev, &mlx5vf_pci_ops);
>>   
>> -	if (pdev->is_virtfn) {
>> -		struct mlx5_core_dev *mdev =
>> -			mlx5_vf_get_core_dev(pdev);
>> -
>> -		if (mdev) {
>> -			if (MLX5_CAP_GEN(mdev, migration)) {
>> -				mvdev->migrate_cap = 1;
>> -				mvdev->core_device.vdev.migration_flags =
>> -					VFIO_MIGRATION_STOP_COPY |
>> -					VFIO_MIGRATION_P2P;
>> -				mutex_init(&mvdev->state_mutex);
>> -				spin_lock_init(&mvdev->reset_lock);
>> -			}
>> -			mlx5_vf_put_core_dev(mdev);
>> -		}
>> +	if (pdev->is_virtfn && mlx5vf_cmd_is_migratable(pdev)) {
>> +		mvdev->migrate_cap = 1;
>> +		mvdev->core_device.vdev.migration_flags =
>> +			VFIO_MIGRATION_STOP_COPY |
>> +			VFIO_MIGRATION_P2P;
>> +		mutex_init(&mvdev->state_mutex);
>> +		spin_lock_init(&mvdev->reset_lock);
>>   	}
>>   
>>   	ret = vfio_pci_core_register_device(&mvdev->core_device);
diff mbox series

Patch

diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c
index 5c9f9218cc1d..d608b8167f58 100644
--- a/drivers/vfio/pci/mlx5/cmd.c
+++ b/drivers/vfio/pci/mlx5/cmd.c
@@ -71,6 +71,24 @@  int mlx5vf_cmd_query_vhca_migration_state(struct pci_dev *pdev, u16 vhca_id,
 	return ret;
 }
 
+bool mlx5vf_cmd_is_migratable(struct pci_dev *pdev)
+{
+	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
+	bool migratable = false;
+
+	if (!mdev)
+		return false;
+
+	if (!MLX5_CAP_GEN(mdev, migration))
+		goto end;
+
+	migratable = true;
+
+end:
+	mlx5_vf_put_core_dev(mdev);
+	return migratable;
+}
+
 int mlx5vf_cmd_get_vhca_id(struct pci_dev *pdev, u16 function_id, u16 *vhca_id)
 {
 	struct mlx5_core_dev *mdev = mlx5_vf_get_core_dev(pdev);
diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h
index 1392a11a9cc0..2da6a1c0ec5c 100644
--- a/drivers/vfio/pci/mlx5/cmd.h
+++ b/drivers/vfio/pci/mlx5/cmd.h
@@ -29,6 +29,7 @@  int mlx5vf_cmd_resume_vhca(struct pci_dev *pdev, u16 vhca_id, u16 op_mod);
 int mlx5vf_cmd_query_vhca_migration_state(struct pci_dev *pdev, u16 vhca_id,
 					  size_t *state_size);
 int mlx5vf_cmd_get_vhca_id(struct pci_dev *pdev, u16 function_id, u16 *vhca_id);
+bool mlx5vf_cmd_is_migratable(struct pci_dev *pdev);
 int mlx5vf_cmd_save_vhca_state(struct pci_dev *pdev, u16 vhca_id,
 			       struct mlx5_vf_migration_file *migf);
 int mlx5vf_cmd_load_vhca_state(struct pci_dev *pdev, u16 vhca_id,
diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
index bbec5d288fee..2578f61eaeae 100644
--- a/drivers/vfio/pci/mlx5/main.c
+++ b/drivers/vfio/pci/mlx5/main.c
@@ -597,21 +597,13 @@  static int mlx5vf_pci_probe(struct pci_dev *pdev,
 		return -ENOMEM;
 	vfio_pci_core_init_device(&mvdev->core_device, pdev, &mlx5vf_pci_ops);
 
-	if (pdev->is_virtfn) {
-		struct mlx5_core_dev *mdev =
-			mlx5_vf_get_core_dev(pdev);
-
-		if (mdev) {
-			if (MLX5_CAP_GEN(mdev, migration)) {
-				mvdev->migrate_cap = 1;
-				mvdev->core_device.vdev.migration_flags =
-					VFIO_MIGRATION_STOP_COPY |
-					VFIO_MIGRATION_P2P;
-				mutex_init(&mvdev->state_mutex);
-				spin_lock_init(&mvdev->reset_lock);
-			}
-			mlx5_vf_put_core_dev(mdev);
-		}
+	if (pdev->is_virtfn && mlx5vf_cmd_is_migratable(pdev)) {
+		mvdev->migrate_cap = 1;
+		mvdev->core_device.vdev.migration_flags =
+			VFIO_MIGRATION_STOP_COPY |
+			VFIO_MIGRATION_P2P;
+		mutex_init(&mvdev->state_mutex);
+		spin_lock_init(&mvdev->reset_lock);
 	}
 
 	ret = vfio_pci_core_register_device(&mvdev->core_device);