Skip to content

Commit 22e87bf

Browse files
Yishai Hadasawilliam
authored andcommitted
vfio/mlx5: Fix an unwind issue in mlx5vf_add_migration_pages()
Fix an unwind issue in mlx5vf_add_migration_pages(). If a set of pages is allocated but fails to be added to the SG table, they need to be freed to prevent a memory leak. Any pages successfully added to the SG table will be freed as part of mlx5vf_free_data_buffer(). Fixes: 6fadb02 ("vfio/mlx5: Implement vfio_pci driver for mlx5 devices") Signed-off-by: Yishai Hadas <yishaih@nvidia.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20241114095318.16556-2-yishaih@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 40bcdb1 commit 22e87bf

File tree

1 file changed

+5
-1
lines changed
  • drivers/vfio/pci/mlx5

1 file changed

+5
-1
lines changed

drivers/vfio/pci/mlx5/cmd.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ static int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
423423
unsigned long filled;
424424
unsigned int to_fill;
425425
int ret;
426+
int i;
426427

427428
to_fill = min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list));
428429
page_list = kvzalloc(to_fill * sizeof(*page_list), GFP_KERNEL_ACCOUNT);
@@ -443,7 +444,7 @@ static int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
443444
GFP_KERNEL_ACCOUNT);
444445

445446
if (ret)
446-
goto err;
447+
goto err_append;
447448
buf->allocated_length += filled * PAGE_SIZE;
448449
/* clean input for another bulk allocation */
449450
memset(page_list, 0, filled * sizeof(*page_list));
@@ -454,6 +455,9 @@ static int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
454455
kvfree(page_list);
455456
return 0;
456457

458+
err_append:
459+
for (i = filled - 1; i >= 0; i--)
460+
__free_page(page_list[i]);
457461
err:
458462
kvfree(page_list);
459463
return ret;

0 commit comments

Comments
 (0)