diff mbox series

[vfio,06/13] vfio/mlx5: Refactor total_length name and usage

Message ID 20221106174630.25909-7-yishaih@nvidia.com (mailing list archive)
State New, archived
Headers show
Series Add migration PRE_COPY support for mlx5 driver | expand

Commit Message

Yishai Hadas Nov. 6, 2022, 5:46 p.m. UTC
From: Shay Drory <shayd@nvidia.com>

On the source side flow, change the usage of total_length to keep only
the size of the image as returned from the FW and rename it to
image_length. This is eliminating the usage of total_length inside the
SAVE command.

This is a preparation step for the next patches from the series where
more than one image could be managed on the migration file.

Signed-off-by: Shay Drory <shayd@nvidia.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
---
 drivers/vfio/pci/mlx5/cmd.c  | 24 +++++++++++-------------
 drivers/vfio/pci/mlx5/cmd.h  |  2 +-
 drivers/vfio/pci/mlx5/main.c | 20 ++++++++++----------
 3 files changed, 22 insertions(+), 24 deletions(-)

Comments

Jason Gunthorpe Nov. 9, 2022, 6:11 p.m. UTC | #1
On Sun, Nov 06, 2022 at 07:46:23PM +0200, Yishai Hadas wrote:

> @@ -1047,7 +1045,7 @@ static int mlx5vf_alloc_qp_recv_resources(struct mlx5_core_dev *mdev,
>  	if (err)
>  		goto end;
>  
> -	err = _create_mkey(mdev, pdn, NULL, recv_buf, &recv_buf->mkey);
> +	err = _create_mkey(mdev, pdn, NULL, recv_buf, &recv_buf->mkey, 0);

This seems pretty goofy, a 0 length means use the length in the
recv_buf?

Jason
Yishai Hadas Nov. 10, 2022, 11:38 a.m. UTC | #2
On 09/11/2022 20:11, Jason Gunthorpe wrote:
> On Sun, Nov 06, 2022 at 07:46:23PM +0200, Yishai Hadas wrote:
>
>> @@ -1047,7 +1045,7 @@ static int mlx5vf_alloc_qp_recv_resources(struct mlx5_core_dev *mdev,
>>   	if (err)
>>   		goto end;
>>   
>> -	err = _create_mkey(mdev, pdn, NULL, recv_buf, &recv_buf->mkey);
>> +	err = _create_mkey(mdev, pdn, NULL, recv_buf, &recv_buf->mkey, 0);
> This seems pretty goofy, a 0 length means use the length in the
> recv_buf?
>
Right

In V1 will embed here the expected length (i.e. npages * PAGE_SIZE) and 
use it directly as part of _create_mkey().

Yishai
diff mbox series

Patch

diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c
index b9ed2f1c8689..24c6d2e4c2be 100644
--- a/drivers/vfio/pci/mlx5/cmd.c
+++ b/drivers/vfio/pci/mlx5/cmd.c
@@ -212,9 +212,9 @@  static int mlx5vf_cmd_get_vhca_id(struct mlx5_core_dev *mdev, u16 function_id,
 static int _create_mkey(struct mlx5_core_dev *mdev, u32 pdn,
 			struct mlx5_vf_migration_file *migf,
 			struct mlx5_vhca_recv_buf *recv_buf,
-			u32 *mkey)
+			u32 *mkey, size_t length)
 {
-	size_t npages = migf ? DIV_ROUND_UP(migf->total_length, PAGE_SIZE) :
+	size_t npages = migf ? DIV_ROUND_UP(length, PAGE_SIZE) :
 				recv_buf->npages;
 	int err = 0, inlen;
 	__be64 *mtt;
@@ -255,8 +255,7 @@  static int _create_mkey(struct mlx5_core_dev *mdev, u32 pdn,
 	MLX5_SET(mkc, mkc, qpn, 0xffffff);
 	MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
 	MLX5_SET(mkc, mkc, translations_octword_size, DIV_ROUND_UP(npages, 2));
-	MLX5_SET64(mkc, mkc, len,
-		   migf ? migf->total_length : (npages * PAGE_SIZE));
+	MLX5_SET64(mkc, mkc, len, migf ? length : (npages * PAGE_SIZE));
 	err = mlx5_core_create_mkey(mdev, mkey, in, inlen);
 	kvfree(in);
 	return err;
@@ -294,7 +293,7 @@  static void mlx5vf_save_callback(int status, struct mlx5_async_work *context)
 			struct mlx5_vf_migration_file, async_data);
 
 	if (!status) {
-		WRITE_ONCE(migf->total_length,
+		WRITE_ONCE(migf->image_length,
 			   MLX5_GET(save_vhca_state_out, async_data->out,
 				    actual_image_size));
 		wake_up_interruptible(&migf->poll_wait);
@@ -333,7 +332,8 @@  int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev,
 	if (err)
 		goto err_dma_map;
 
-	err = _create_mkey(mdev, pdn, migf, NULL, &mkey);
+	err = _create_mkey(mdev, pdn, migf, NULL,
+			   &mkey, migf->allocated_length);
 	if (err)
 		goto err_create_mkey;
 
@@ -342,7 +342,7 @@  int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev,
 	MLX5_SET(save_vhca_state_in, in, op_mod, 0);
 	MLX5_SET(save_vhca_state_in, in, vhca_id, mvdev->vhca_id);
 	MLX5_SET(save_vhca_state_in, in, mkey, mkey);
-	MLX5_SET(save_vhca_state_in, in, size, migf->total_length);
+	MLX5_SET(save_vhca_state_in, in, size, migf->allocated_length);
 
 	async_data = &migf->async_data;
 	async_data->out = kvzalloc(out_size, GFP_KERNEL);
@@ -351,8 +351,6 @@  int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev,
 		goto err_out;
 	}
 
-	/* no data exists till the callback comes back */
-	migf->total_length = 0;
 	get_file(migf->filp);
 	async_data->mkey = mkey;
 	async_data->pdn = pdn;
@@ -393,7 +391,7 @@  int mlx5vf_cmd_load_vhca_state(struct mlx5vf_pci_core_device *mvdev,
 		return -ENOTCONN;
 
 	mutex_lock(&migf->lock);
-	if (!migf->total_length) {
+	if (!migf->image_length) {
 		err = -EINVAL;
 		goto end;
 	}
@@ -407,7 +405,7 @@  int mlx5vf_cmd_load_vhca_state(struct mlx5vf_pci_core_device *mvdev,
 	if (err)
 		goto err_reg;
 
-	err = _create_mkey(mdev, pdn, migf, NULL, &mkey);
+	err = _create_mkey(mdev, pdn, migf, NULL, &mkey, migf->image_length);
 	if (err)
 		goto err_mkey;
 
@@ -416,7 +414,7 @@  int mlx5vf_cmd_load_vhca_state(struct mlx5vf_pci_core_device *mvdev,
 	MLX5_SET(load_vhca_state_in, in, op_mod, 0);
 	MLX5_SET(load_vhca_state_in, in, vhca_id, mvdev->vhca_id);
 	MLX5_SET(load_vhca_state_in, in, mkey, mkey);
-	MLX5_SET(load_vhca_state_in, in, size, migf->total_length);
+	MLX5_SET(load_vhca_state_in, in, size, migf->image_length);
 
 	err = mlx5_cmd_exec_inout(mdev, load_vhca_state, in, out);
 
@@ -1047,7 +1045,7 @@  static int mlx5vf_alloc_qp_recv_resources(struct mlx5_core_dev *mdev,
 	if (err)
 		goto end;
 
-	err = _create_mkey(mdev, pdn, NULL, recv_buf, &recv_buf->mkey);
+	err = _create_mkey(mdev, pdn, NULL, recv_buf, &recv_buf->mkey, 0);
 	if (err)
 		goto err_create_mkey;
 
diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h
index b1c5dd2ff144..b1fa1a0418a5 100644
--- a/drivers/vfio/pci/mlx5/cmd.h
+++ b/drivers/vfio/pci/mlx5/cmd.h
@@ -29,7 +29,7 @@  struct mlx5_vf_migration_file {
 	u8 save_cb_active:1;
 
 	struct sg_append_table table;
-	size_t total_length;
+	size_t image_length;
 	size_t allocated_length;
 
 	/* Optimize mlx5vf_get_migration_page() for sequential access */
diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c
index 5da278f3c31c..624b1a99dc21 100644
--- a/drivers/vfio/pci/mlx5/main.c
+++ b/drivers/vfio/pci/mlx5/main.c
@@ -116,7 +116,7 @@  static void mlx5vf_disable_fd(struct mlx5_vf_migration_file *migf)
 		__free_page(sg_page_iter_page(&sg_iter));
 	sg_free_append_table(&migf->table);
 	migf->disabled = true;
-	migf->total_length = 0;
+	migf->image_length = 0;
 	migf->allocated_length = 0;
 	migf->filp->f_pos = 0;
 	mutex_unlock(&migf->lock);
@@ -144,16 +144,16 @@  static ssize_t mlx5vf_save_read(struct file *filp, char __user *buf, size_t len,
 
 	if (!(filp->f_flags & O_NONBLOCK)) {
 		if (wait_event_interruptible(migf->poll_wait,
-			     READ_ONCE(migf->total_length) || migf->is_err))
+			     READ_ONCE(migf->image_length) || migf->is_err))
 			return -ERESTARTSYS;
 	}
 
 	mutex_lock(&migf->lock);
-	if ((filp->f_flags & O_NONBLOCK) && !READ_ONCE(migf->total_length)) {
+	if ((filp->f_flags & O_NONBLOCK) && !READ_ONCE(migf->image_length)) {
 		done = -EAGAIN;
 		goto out_unlock;
 	}
-	if (*pos > migf->total_length) {
+	if (*pos > migf->image_length) {
 		done = -EINVAL;
 		goto out_unlock;
 	}
@@ -162,7 +162,7 @@  static ssize_t mlx5vf_save_read(struct file *filp, char __user *buf, size_t len,
 		goto out_unlock;
 	}
 
-	len = min_t(size_t, migf->total_length - *pos, len);
+	len = min_t(size_t, migf->image_length - *pos, len);
 	while (len) {
 		size_t page_offset;
 		struct page *page;
@@ -208,7 +208,7 @@  static __poll_t mlx5vf_save_poll(struct file *filp,
 	mutex_lock(&migf->lock);
 	if (migf->disabled || migf->is_err)
 		pollflags = EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
-	else if (READ_ONCE(migf->total_length))
+	else if (READ_ONCE(migf->image_length))
 		pollflags = EPOLLIN | EPOLLRDNORM;
 	mutex_unlock(&migf->lock);
 
@@ -227,6 +227,7 @@  static struct mlx5_vf_migration_file *
 mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev)
 {
 	struct mlx5_vf_migration_file *migf;
+	size_t length;
 	int ret;
 
 	migf = kzalloc(sizeof(*migf), GFP_KERNEL);
@@ -248,13 +249,12 @@  mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev)
 	init_waitqueue_head(&migf->save_wait);
 	mlx5_cmd_init_async_ctx(mvdev->mdev, &migf->async_ctx);
 	INIT_WORK(&migf->async_data.work, mlx5vf_mig_file_cleanup_cb);
-	ret = mlx5vf_cmd_query_vhca_migration_state(mvdev,
-						    &migf->total_length);
+	ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &length);
 	if (ret)
 		goto out_free;
 
 	ret = mlx5vf_add_migration_pages(
-		migf, DIV_ROUND_UP_ULL(migf->total_length, PAGE_SIZE));
+		migf, DIV_ROUND_UP_ULL(length, PAGE_SIZE));
 	if (ret)
 		goto out_free;
 
@@ -328,7 +328,7 @@  static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
 		len -= page_len;
 		done += page_len;
 		buf += page_len;
-		migf->total_length += page_len;
+		migf->image_length += page_len;
 	}
 out_unlock:
 	mutex_unlock(&migf->lock);