* [PATCH] vfio/pds: Release migration files on allocation failure
@ 2026-11-12 2:31 Zhu Jun
2026-12-01 21:19 ` Alex Williamson
0 siblings, 1 reply; 4+ messages in thread
From: Zhu Jun @ 2026-11-12 2:31 UTC (permalink / raw)
To: brett.creeley
Cc: jgg, yishaih, skolothumtho, kevin.tian, alex, kvm, linux-kernel,
Zhu Jun
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@cmss.chinamobile.com>
---
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] vfio/pds: Release migration files on allocation failure
2026-11-12 2:31 [PATCH] vfio/pds: Release migration files on allocation failure Zhu Jun
@ 2026-12-01 21:19 ` Alex Williamson
2026-12-01 21:25 ` Creeley, Brett
2026-12-01 22:03 ` Creeley, Brett
0 siblings, 2 replies; 4+ messages in thread
From: Alex Williamson @ 2026-12-01 21:19 UTC (permalink / raw)
To: Zhu Jun
Cc: brett.creeley, jgg, yishaih, skolothumtho, kevin.tian, kvm,
linux-kernel, alex
On Thu, 12 Nov 2026 10:31:01 +0800
Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
> 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@cmss.chinamobile.com>
> ---
> 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);
>
I might opt to do the lm_file free+return in the anon_inode_getfile()
error branch, rather than create this split unwind chain. There's a
natural inflection after that where we rely on the release callback for
the final cleanup anyway. The change itself looks correct as is to me
otherwise. Brett? Thanks,
Alex
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] vfio/pds: Release migration files on allocation failure
2026-12-01 21:19 ` Alex Williamson
@ 2026-12-01 21:25 ` Creeley, Brett
2026-12-01 22:03 ` Creeley, Brett
1 sibling, 0 replies; 4+ messages in thread
From: Creeley, Brett @ 2026-12-01 21:25 UTC (permalink / raw)
To: Alex Williamson, Zhu Jun
Cc: brett.creeley, jgg, yishaih, skolothumtho, kevin.tian, kvm,
linux-kernel
On 12/1/2026 1:19 PM, Alex Williamson wrote:
> Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
>
>
> On Thu, 12 Nov 2026 10:31:01 +0800
> Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
>
>> 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@cmss.chinamobile.com>
>> ---
>> 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);
>>
> I might opt to do the lm_file free+return in the anon_inode_getfile()
> error branch, rather than create this split unwind chain. There's a
> natural inflection after that where we rely on the release callback for
> the final cleanup anyway. The change itself looks correct as is to me
> otherwise. Brett? Thanks,
I meant to get back to this, thanks for the reminder. I had some changes
I was working on and our local Sashiko complained about this. Let me
look what I ended up doing there and get back to you on this.
Thanks,
Brett
>
> Alex
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] vfio/pds: Release migration files on allocation failure
2026-12-01 21:19 ` Alex Williamson
2026-12-01 21:25 ` Creeley, Brett
@ 2026-12-01 22:03 ` Creeley, Brett
1 sibling, 0 replies; 4+ messages in thread
From: Creeley, Brett @ 2026-12-01 22:03 UTC (permalink / raw)
To: Alex Williamson, Zhu Jun
Cc: brett.creeley, jgg, yishaih, skolothumtho, kevin.tian, kvm,
linux-kernel
On 12/1/2026 1:19 PM, Alex Williamson wrote:
> Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
>
>
> On Thu, 12 Nov 2026 10:31:01 +0800
> Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
>
>> 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@cmss.chinamobile.com>
>> ---
>> 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);
>>
> I might opt to do the lm_file free+return in the anon_inode_getfile()
> error branch, rather than create this split unwind chain. There's a
> natural inflection after that where we rely on the release callback for
> the final cleanup anyway. The change itself looks correct as is to me
> otherwise. Brett? Thanks,
I agree that you should do the restructure based on Alex's comment.
Please submit a v2 based on his suggestion and I will be happy to add my
Reviewed-by on that one.
Thanks for the fix,
Brett
> Alex
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 22:03 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 2:31 [PATCH] vfio/pds: Release migration files on allocation failure Zhu Jun
2026-12-01 21:19 ` Alex Williamson
2026-12-01 21:25 ` Creeley, Brett
2026-12-01 22:03 ` Creeley, Brett
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox