mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] vfio/pds: Fix memory leak in pds_vfio_dirty_enable()
@ 2025-12-25 14:31 Zilin Guan
  2025-12-28 20:47 ` Alex Williamson
  2026-01-05 17:38 ` Creeley, Brett
  0 siblings, 2 replies; 3+ messages in thread
From: Zilin Guan @ 2025-12-25 14:31 UTC (permalink / raw)
  To: jgg
  Cc: yishaih, skolothumtho, kevin.tian, brett.creeley, alex, kvm,
	linux-kernel, jianhao.xu, Zilin Guan

pds_vfio_dirty_enable() allocates memory for region_info. If
interval_tree_iter_first() returns NULL, the function returns -EINVAL
immediately without freeing the allocated memory, causing a memory leak.

Fix this by jumping to the out_free_region_info label to ensure
region_info is freed.

Fixes: 2e7c6feb4ef52 ("vfio/pds: Add multi-region support")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 drivers/vfio/pci/pds/dirty.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/vfio/pci/pds/dirty.c b/drivers/vfio/pci/pds/dirty.c
index 481992142f79..4915a7c1c491 100644
--- a/drivers/vfio/pci/pds/dirty.c
+++ b/drivers/vfio/pci/pds/dirty.c
@@ -292,8 +292,11 @@ static int pds_vfio_dirty_enable(struct pds_vfio_pci_device *pds_vfio,
 	len = num_ranges * sizeof(*region_info);
 
 	node = interval_tree_iter_first(ranges, 0, ULONG_MAX);
-	if (!node)
-		return -EINVAL;
+	if (!node) {
+		err = -EINVAL;
+		goto out_free_region_info;
+	}
+
 	for (int i = 0; i < num_ranges; i++) {
 		struct pds_lm_dirty_region_info *ri = &region_info[i];
 		u64 region_size = node->last - node->start + 1;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] vfio/pds: Fix memory leak in pds_vfio_dirty_enable()
  2025-12-25 14:31 [PATCH] vfio/pds: Fix memory leak in pds_vfio_dirty_enable() Zilin Guan
@ 2025-12-28 20:47 ` Alex Williamson
  2026-01-05 17:38 ` Creeley, Brett
  1 sibling, 0 replies; 3+ messages in thread
From: Alex Williamson @ 2025-12-28 20:47 UTC (permalink / raw)
  To: Zilin Guan
  Cc: jgg, yishaih, skolothumtho, kevin.tian, brett.creeley, kvm,
	linux-kernel, jianhao.xu

On Thu, 25 Dec 2025 14:31:50 +0000
Zilin Guan <zilin@seu.edu.cn> wrote:

> pds_vfio_dirty_enable() allocates memory for region_info. If
> interval_tree_iter_first() returns NULL, the function returns -EINVAL
> immediately without freeing the allocated memory, causing a memory leak.
> 
> Fix this by jumping to the out_free_region_info label to ensure
> region_info is freed.
> 
> Fixes: 2e7c6feb4ef52 ("vfio/pds: Add multi-region support")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
>  drivers/vfio/pci/pds/dirty.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/vfio/pci/pds/dirty.c b/drivers/vfio/pci/pds/dirty.c
> index 481992142f79..4915a7c1c491 100644
> --- a/drivers/vfio/pci/pds/dirty.c
> +++ b/drivers/vfio/pci/pds/dirty.c
> @@ -292,8 +292,11 @@ static int pds_vfio_dirty_enable(struct pds_vfio_pci_device *pds_vfio,
>  	len = num_ranges * sizeof(*region_info);
>  
>  	node = interval_tree_iter_first(ranges, 0, ULONG_MAX);
> -	if (!node)
> -		return -EINVAL;
> +	if (!node) {
> +		err = -EINVAL;
> +		goto out_free_region_info;
> +	}
> +
>  	for (int i = 0; i < num_ranges; i++) {
>  		struct pds_lm_dirty_region_info *ri = &region_info[i];
>  		u64 region_size = node->last - node->start + 1;

Applied to vfio for-linus branch for v6.19.  Thanks,

Alex

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] vfio/pds: Fix memory leak in pds_vfio_dirty_enable()
  2025-12-25 14:31 [PATCH] vfio/pds: Fix memory leak in pds_vfio_dirty_enable() Zilin Guan
  2025-12-28 20:47 ` Alex Williamson
@ 2026-01-05 17:38 ` Creeley, Brett
  1 sibling, 0 replies; 3+ messages in thread
From: Creeley, Brett @ 2026-01-05 17:38 UTC (permalink / raw)
  To: Zilin Guan, jgg
  Cc: yishaih, skolothumtho, kevin.tian, brett.creeley, alex, kvm,
	linux-kernel, jianhao.xu



On 12/25/2025 6:31 AM, Zilin Guan wrote:
> Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
>
>
> pds_vfio_dirty_enable() allocates memory for region_info. If
> interval_tree_iter_first() returns NULL, the function returns -EINVAL
> immediately without freeing the allocated memory, causing a memory leak.
>
> Fix this by jumping to the out_free_region_info label to ensure
> region_info is freed.
>
> Fixes: 2e7c6feb4ef52 ("vfio/pds: Add multi-region support")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
>   drivers/vfio/pci/pds/dirty.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vfio/pci/pds/dirty.c b/drivers/vfio/pci/pds/dirty.c
> index 481992142f79..4915a7c1c491 100644
> --- a/drivers/vfio/pci/pds/dirty.c
> +++ b/drivers/vfio/pci/pds/dirty.c
> @@ -292,8 +292,11 @@ static int pds_vfio_dirty_enable(struct pds_vfio_pci_device *pds_vfio,
>          len = num_ranges * sizeof(*region_info);
>
>          node = interval_tree_iter_first(ranges, 0, ULONG_MAX);
> -       if (!node)
> -               return -EINVAL;
> +       if (!node) {
> +               err = -EINVAL;
> +               goto out_free_region_info;
> +       }
> +

Thanks for the fix. I was on vacation, but FWIW:

Reviewed-by: Brett Creeley <brett.creeley@amd.com>

>          for (int i = 0; i < num_ranges; i++) {
>                  struct pds_lm_dirty_region_info *ri = &region_info[i];
>                  u64 region_size = node->last - node->start + 1;
> --
> 2.34.1
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-01-05 17:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-25 14:31 [PATCH] vfio/pds: Fix memory leak in pds_vfio_dirty_enable() Zilin Guan
2025-12-28 20:47 ` Alex Williamson
2026-01-05 17:38 ` Creeley, Brett

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®