[PATCH] vfio/pds: Release migration files on allocation failure

From: Zhu Jun

Date: Wed Nov 11 2026 - 21:38:20 EST


pds_vfio_get_lm_file() takes an extra file reference before allocating its
backing storage. If an allocation fails, the error path drops only one
reference and frees the private data, leaving the anonymous file leaked.

Take the extra reference only after all allocations succeed. On failure,
drop the initial reference and let the file's release callback destroy the
mutex and free the private data.

OpenAI Codex assisted with analysis and patch preparation.

Fixes: bb500dbe2ac6 ("vfio/pds: Add VFIO live migration support")
Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
---
drivers/vfio/pci/pds/lm.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/vfio/pci/pds/lm.c b/drivers/vfio/pci/pds/lm.c
index 08487eab11b0..d96ec77402c0 100644
--- a/drivers/vfio/pci/pds/lm.c
+++ b/drivers/vfio/pci/pds/lm.c
@@ -37,9 +37,6 @@ pds_vfio_get_lm_file(const struct file_operations *fops, int flags, u64 size)
stream_open(lm_file->filep->f_inode, lm_file->filep);
mutex_init(&lm_file->lock);

- /* prevent file from being released before we are done with it */
- get_file(lm_file->filep);
-
/* Allocate memory for file pages */
npages = DIV_ROUND_UP_ULL(size, PAGE_SIZE);
pages = kmalloc_objs(*pages, npages);
@@ -73,6 +70,9 @@ pds_vfio_get_lm_file(const struct file_operations *fops, int flags, u64 size)
lm_file->page_mem = page_mem;
lm_file->alloc_size = npages * PAGE_SIZE;

+ /* prevent file from being released before we are done with it */
+ get_file(lm_file->filep);
+
return lm_file;

out_free_page_mem:
@@ -81,7 +81,8 @@ pds_vfio_get_lm_file(const struct file_operations *fops, int flags, u64 size)
kfree(pages);
out_put_file:
fput(lm_file->filep);
- mutex_destroy(&lm_file->lock);
+ return NULL;
+
out_free_file:
kfree(lm_file);

--
2.51.0