Skip to content

Commit 674ebb6

Browse files
rleonawilliam
authored andcommitted
vfio/mlx5: Explicitly use number of pages instead of allocated length
allocated_length is a multiple of page size and number of pages, so let's change the functions to accept number of pages. This improves code readability, simplifies buffer handling, and enables combining DMA send/receive operations, as will be introduced in the next patches. Tested-by: Jens Axboe <axboe@kernel.dk> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Leon Romanovsky <leonro@nvidia.com> Acked-by: Yishai Hadas <yishaih@nvidia.com> Link: https://lore.kernel.org/r/76f39993d2ca0311b3bcfe56038a669d03926815.1747747694.git.leon@kernel.org Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 6ef0455 commit 674ebb6

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

drivers/vfio/pci/mlx5/cmd.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ static int _create_mkey(struct mlx5_core_dev *mdev, u32 pdn,
318318
struct mlx5_vhca_recv_buf *recv_buf,
319319
u32 *mkey)
320320
{
321-
size_t npages = buf ? DIV_ROUND_UP(buf->allocated_length, PAGE_SIZE) :
322-
recv_buf->npages;
321+
size_t npages = buf ? buf->npages : recv_buf->npages;
323322
int err = 0, inlen;
324323
__be64 *mtt;
325324
void *mkc;
@@ -375,7 +374,7 @@ static int mlx5vf_dma_data_buffer(struct mlx5_vhca_data_buffer *buf)
375374
if (mvdev->mdev_detach)
376375
return -ENOTCONN;
377376

378-
if (buf->dmaed || !buf->allocated_length)
377+
if (buf->dmaed || !buf->npages)
379378
return -EINVAL;
380379

381380
ret = dma_map_sgtable(mdev->device, &buf->table.sgt, buf->dma_dir, 0);
@@ -445,7 +444,7 @@ static int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
445444

446445
if (ret)
447446
goto err_append;
448-
buf->allocated_length += filled * PAGE_SIZE;
447+
buf->npages += filled;
449448
/* clean input for another bulk allocation */
450449
memset(page_list, 0, filled * sizeof(*page_list));
451450
to_fill = min_t(unsigned int, to_alloc,
@@ -464,8 +463,7 @@ static int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
464463
}
465464

466465
struct mlx5_vhca_data_buffer *
467-
mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf,
468-
size_t length,
466+
mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf, u32 npages,
469467
enum dma_data_direction dma_dir)
470468
{
471469
struct mlx5_vhca_data_buffer *buf;
@@ -477,9 +475,8 @@ mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf,
477475

478476
buf->dma_dir = dma_dir;
479477
buf->migf = migf;
480-
if (length) {
481-
ret = mlx5vf_add_migration_pages(buf,
482-
DIV_ROUND_UP_ULL(length, PAGE_SIZE));
478+
if (npages) {
479+
ret = mlx5vf_add_migration_pages(buf, npages);
483480
if (ret)
484481
goto end;
485482

@@ -505,8 +502,8 @@ void mlx5vf_put_data_buffer(struct mlx5_vhca_data_buffer *buf)
505502
}
506503

507504
struct mlx5_vhca_data_buffer *
508-
mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf,
509-
size_t length, enum dma_data_direction dma_dir)
505+
mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf, u32 npages,
506+
enum dma_data_direction dma_dir)
510507
{
511508
struct mlx5_vhca_data_buffer *buf, *temp_buf;
512509
struct list_head free_list;
@@ -521,7 +518,7 @@ mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf,
521518
list_for_each_entry_safe(buf, temp_buf, &migf->avail_list, buf_elm) {
522519
if (buf->dma_dir == dma_dir) {
523520
list_del_init(&buf->buf_elm);
524-
if (buf->allocated_length >= length) {
521+
if (buf->npages >= npages) {
525522
spin_unlock_irq(&migf->list_lock);
526523
goto found;
527524
}
@@ -535,7 +532,7 @@ mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf,
535532
}
536533
}
537534
spin_unlock_irq(&migf->list_lock);
538-
buf = mlx5vf_alloc_data_buffer(migf, length, dma_dir);
535+
buf = mlx5vf_alloc_data_buffer(migf, npages, dma_dir);
539536

540537
found:
541538
while ((temp_buf = list_first_entry_or_null(&free_list,
@@ -716,7 +713,7 @@ int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev,
716713
MLX5_SET(save_vhca_state_in, in, op_mod, 0);
717714
MLX5_SET(save_vhca_state_in, in, vhca_id, mvdev->vhca_id);
718715
MLX5_SET(save_vhca_state_in, in, mkey, buf->mkey);
719-
MLX5_SET(save_vhca_state_in, in, size, buf->allocated_length);
716+
MLX5_SET(save_vhca_state_in, in, size, buf->npages * PAGE_SIZE);
720717
MLX5_SET(save_vhca_state_in, in, incremental, inc);
721718
MLX5_SET(save_vhca_state_in, in, set_track, track);
722719

@@ -738,8 +735,11 @@ int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev,
738735
}
739736

740737
if (!header_buf) {
741-
header_buf = mlx5vf_get_data_buffer(migf,
742-
sizeof(struct mlx5_vf_migration_header), DMA_NONE);
738+
header_buf = mlx5vf_get_data_buffer(
739+
migf,
740+
DIV_ROUND_UP(sizeof(struct mlx5_vf_migration_header),
741+
PAGE_SIZE),
742+
DMA_NONE);
743743
if (IS_ERR(header_buf)) {
744744
err = PTR_ERR(header_buf);
745745
goto err_free;

drivers/vfio/pci/mlx5/cmd.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct mlx5_vhca_data_buffer {
5656
struct sg_append_table table;
5757
loff_t start_pos;
5858
u64 length;
59-
u64 allocated_length;
59+
u32 npages;
6060
u32 mkey;
6161
enum dma_data_direction dma_dir;
6262
u8 dmaed:1;
@@ -217,12 +217,12 @@ int mlx5vf_cmd_alloc_pd(struct mlx5_vf_migration_file *migf);
217217
void mlx5vf_cmd_dealloc_pd(struct mlx5_vf_migration_file *migf);
218218
void mlx5fv_cmd_clean_migf_resources(struct mlx5_vf_migration_file *migf);
219219
struct mlx5_vhca_data_buffer *
220-
mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf,
221-
size_t length, enum dma_data_direction dma_dir);
220+
mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf, u32 npages,
221+
enum dma_data_direction dma_dir);
222222
void mlx5vf_free_data_buffer(struct mlx5_vhca_data_buffer *buf);
223223
struct mlx5_vhca_data_buffer *
224-
mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf,
225-
size_t length, enum dma_data_direction dma_dir);
224+
mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf, u32 npages,
225+
enum dma_data_direction dma_dir);
226226
void mlx5vf_put_data_buffer(struct mlx5_vhca_data_buffer *buf);
227227
struct page *mlx5vf_get_migration_page(struct mlx5_vhca_data_buffer *buf,
228228
unsigned long offset);

drivers/vfio/pci/mlx5/main.c

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,19 @@ static struct mlx5_vhca_data_buffer *
308308
mlx5vf_mig_file_get_stop_copy_buf(struct mlx5_vf_migration_file *migf,
309309
u8 index, size_t required_length)
310310
{
311+
u32 npages = DIV_ROUND_UP(required_length, PAGE_SIZE);
311312
struct mlx5_vhca_data_buffer *buf = migf->buf[index];
312313
u8 chunk_num;
313314

314315
WARN_ON(!buf);
315316
chunk_num = buf->stop_copy_chunk_num;
316317
buf->migf->buf[index] = NULL;
317318
/* Checking whether the pre-allocated buffer can fit */
318-
if (buf->allocated_length >= required_length)
319+
if (buf->npages >= npages)
319320
return buf;
320321

321322
mlx5vf_put_data_buffer(buf);
322-
buf = mlx5vf_get_data_buffer(buf->migf, required_length,
323-
DMA_FROM_DEVICE);
323+
buf = mlx5vf_get_data_buffer(buf->migf, npages, DMA_FROM_DEVICE);
324324
if (IS_ERR(buf))
325325
return buf;
326326

@@ -373,7 +373,8 @@ static int mlx5vf_add_stop_copy_header(struct mlx5_vf_migration_file *migf,
373373
u8 *to_buff;
374374
int ret;
375375

376-
header_buf = mlx5vf_get_data_buffer(migf, size, DMA_NONE);
376+
header_buf = mlx5vf_get_data_buffer(migf, DIV_ROUND_UP(size, PAGE_SIZE),
377+
DMA_NONE);
377378
if (IS_ERR(header_buf))
378379
return PTR_ERR(header_buf);
379380

@@ -388,7 +389,7 @@ static int mlx5vf_add_stop_copy_header(struct mlx5_vf_migration_file *migf,
388389
to_buff = kmap_local_page(page);
389390
memcpy(to_buff, &header, sizeof(header));
390391
header_buf->length = sizeof(header);
391-
data.stop_copy_size = cpu_to_le64(migf->buf[0]->allocated_length);
392+
data.stop_copy_size = cpu_to_le64(migf->buf[0]->npages * PAGE_SIZE);
392393
memcpy(to_buff + sizeof(header), &data, sizeof(data));
393394
header_buf->length += sizeof(data);
394395
kunmap_local(to_buff);
@@ -437,15 +438,20 @@ static int mlx5vf_prep_stop_copy(struct mlx5vf_pci_core_device *mvdev,
437438

438439
num_chunks = mvdev->chunk_mode ? MAX_NUM_CHUNKS : 1;
439440
for (i = 0; i < num_chunks; i++) {
440-
buf = mlx5vf_get_data_buffer(migf, inc_state_size, DMA_FROM_DEVICE);
441+
buf = mlx5vf_get_data_buffer(
442+
migf, DIV_ROUND_UP(inc_state_size, PAGE_SIZE),
443+
DMA_FROM_DEVICE);
441444
if (IS_ERR(buf)) {
442445
ret = PTR_ERR(buf);
443446
goto err;
444447
}
445448

446449
migf->buf[i] = buf;
447-
buf = mlx5vf_get_data_buffer(migf,
448-
sizeof(struct mlx5_vf_migration_header), DMA_NONE);
450+
buf = mlx5vf_get_data_buffer(
451+
migf,
452+
DIV_ROUND_UP(sizeof(struct mlx5_vf_migration_header),
453+
PAGE_SIZE),
454+
DMA_NONE);
449455
if (IS_ERR(buf)) {
450456
ret = PTR_ERR(buf);
451457
goto err;
@@ -553,7 +559,8 @@ static long mlx5vf_precopy_ioctl(struct file *filp, unsigned int cmd,
553559
* We finished transferring the current state and the device has a
554560
* dirty state, save a new state to be ready for.
555561
*/
556-
buf = mlx5vf_get_data_buffer(migf, inc_length, DMA_FROM_DEVICE);
562+
buf = mlx5vf_get_data_buffer(migf, DIV_ROUND_UP(inc_length, PAGE_SIZE),
563+
DMA_FROM_DEVICE);
557564
if (IS_ERR(buf)) {
558565
ret = PTR_ERR(buf);
559566
mlx5vf_mark_err(migf);
@@ -675,8 +682,8 @@ mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev, bool track)
675682

676683
if (track) {
677684
/* leave the allocated buffer ready for the stop-copy phase */
678-
buf = mlx5vf_alloc_data_buffer(migf,
679-
migf->buf[0]->allocated_length, DMA_FROM_DEVICE);
685+
buf = mlx5vf_alloc_data_buffer(migf, migf->buf[0]->npages,
686+
DMA_FROM_DEVICE);
680687
if (IS_ERR(buf)) {
681688
ret = PTR_ERR(buf);
682689
goto out_pd;
@@ -917,11 +924,14 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
917924
goto out_unlock;
918925
break;
919926
case MLX5_VF_LOAD_STATE_PREP_HEADER_DATA:
920-
if (vhca_buf_header->allocated_length < migf->record_size) {
927+
{
928+
u32 npages = DIV_ROUND_UP(migf->record_size, PAGE_SIZE);
929+
930+
if (vhca_buf_header->npages < npages) {
921931
mlx5vf_free_data_buffer(vhca_buf_header);
922932

923-
migf->buf_header[0] = mlx5vf_alloc_data_buffer(migf,
924-
migf->record_size, DMA_NONE);
933+
migf->buf_header[0] = mlx5vf_alloc_data_buffer(
934+
migf, npages, DMA_NONE);
925935
if (IS_ERR(migf->buf_header[0])) {
926936
ret = PTR_ERR(migf->buf_header[0]);
927937
migf->buf_header[0] = NULL;
@@ -934,6 +944,7 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
934944
vhca_buf_header->start_pos = migf->max_pos;
935945
migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER_DATA;
936946
break;
947+
}
937948
case MLX5_VF_LOAD_STATE_READ_HEADER_DATA:
938949
ret = mlx5vf_resume_read_header_data(migf, vhca_buf_header,
939950
&buf, &len, pos, &done);
@@ -944,12 +955,13 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
944955
{
945956
u64 size = max(migf->record_size,
946957
migf->stop_copy_prep_size);
958+
u32 npages = DIV_ROUND_UP(size, PAGE_SIZE);
947959

948-
if (vhca_buf->allocated_length < size) {
960+
if (vhca_buf->npages < npages) {
949961
mlx5vf_free_data_buffer(vhca_buf);
950962

951-
migf->buf[0] = mlx5vf_alloc_data_buffer(migf,
952-
size, DMA_TO_DEVICE);
963+
migf->buf[0] = mlx5vf_alloc_data_buffer(
964+
migf, npages, DMA_TO_DEVICE);
953965
if (IS_ERR(migf->buf[0])) {
954966
ret = PTR_ERR(migf->buf[0]);
955967
migf->buf[0] = NULL;
@@ -1037,8 +1049,11 @@ mlx5vf_pci_resume_device_data(struct mlx5vf_pci_core_device *mvdev)
10371049
}
10381050

10391051
migf->buf[0] = buf;
1040-
buf = mlx5vf_alloc_data_buffer(migf,
1041-
sizeof(struct mlx5_vf_migration_header), DMA_NONE);
1052+
buf = mlx5vf_alloc_data_buffer(
1053+
migf,
1054+
DIV_ROUND_UP(sizeof(struct mlx5_vf_migration_header),
1055+
PAGE_SIZE),
1056+
DMA_NONE);
10421057
if (IS_ERR(buf)) {
10431058
ret = PTR_ERR(buf);
10441059
goto out_buf;
@@ -1148,7 +1163,8 @@ mlx5vf_pci_step_device_state_locked(struct mlx5vf_pci_core_device *mvdev,
11481163
MLX5VF_QUERY_INC | MLX5VF_QUERY_CLEANUP);
11491164
if (ret)
11501165
return ERR_PTR(ret);
1151-
buf = mlx5vf_get_data_buffer(migf, size, DMA_FROM_DEVICE);
1166+
buf = mlx5vf_get_data_buffer(migf,
1167+
DIV_ROUND_UP(size, PAGE_SIZE), DMA_FROM_DEVICE);
11521168
if (IS_ERR(buf))
11531169
return ERR_CAST(buf);
11541170
/* pre_copy cleanup */

0 commit comments

Comments
 (0)