Skip to contentSkip to conversation

vhost-vdpa: fix memory leak in error path

PATCH In review Li Qiang 4 participants 4 messages Sep 9, 2020 – Sep 18, 2020

Subsystems (5)
lore
Share & tools
lore

Li Qiang

author
#0 lore raw

PATCH 1/1 vhost-vdpa: fix memory leak in error path

Free the 'page_list' when the 'npages' is zero.

---
 drivers/vhost/vdpa.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/vhost/vdpa.c +6 -2
index 3fab94f88894..6a9fcaf1831d 100644
@@ -609,8 +609,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 		gup_flags |= FOLL_WRITE;
 
 	npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >> PAGE_SHIFT;
-	if (!npages)
-		return -EINVAL;
+	if (!npages) {
+		ret = -EINVAL;
+		goto free_page;
+	}
 
 	mmap_read_lock(dev->mm);
 
@@ -666,6 +668,8 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
 		atomic64_sub(npages, &dev->mm->pinned_vm);
 	}
 	mmap_read_unlock(dev->mm);
+
+free_page:
 	free_page((unsigned long)page_list);
 	return ret;
 }
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 25 lines · Li Qiang

Free the 'page_list' when the 'npages' is zero.

---
  drivers/vhost/vdpa.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/vhost/vdpa.c +4 -2
index 3fab94f88894..6a9fcaf1831d 100644
@@ -609,8 +609,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
  		gup_flags |= FOLL_WRITE;
 
  	npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >> PAGE_SHIFT;
-	if (!npages)
-		return -EINVAL;
+	if (!npages) {
+		ret = -EINVAL;
+		goto free_page;
+	}
 
  	mmap_read_lock(dev->mm);
 

drivers/vhost/vdpa.c static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, · line 675

Quoted 8 lines
drivers/vhost/vdpa.c +2 -0
@@ -666,6 +668,8 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
  		atomic64_sub(npages, &dev->mm->pinned_vm);
  	}
  	mmap_read_unlock(dev->mm);
+
+free_page:
  	free_page((unsigned long)page_list);
  	return ret;
  }

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
In reply to Li Qiang

Any status update?

Thanks,
Li Qiang

Quoted 37 lines · Li Qiang <[email protected]> 于2020年9月9日周三 下午11:42写道:

Free the 'page_list' when the 'npages' is zero.

---
 drivers/vhost/vdpa.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/vhost/vdpa.c +6 -3
index 3fab94f88894..6a9fcaf1831d 100644
@@ -609,8 +609,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
                gup_flags |= FOLL_WRITE;
 
        npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >> PAGE_SHIFT;
-       if (!npages)
-               return -EINVAL;
+       if (!npages) {
+               ret = -EINVAL;
+               goto free_page;
+       }
 
        mmap_read_lock(dev->mm);
 
@@ -666,6 +668,8 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
                atomic64_sub(npages, &dev->mm->pinned_vm);
        }
        mmap_read_unlock(dev->mm);
+
+free_page:
        free_page((unsigned long)page_list);
        return ret;
 }
--

2.25.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Li Qiang

LGTM.

Thanks.

Quoted 34 lines · Li Qiang

Free the 'page_list' when the 'npages' is zero.

---
  drivers/vhost/vdpa.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/vhost/vdpa.c +6 -2
index 3fab94f88894..6a9fcaf1831d 100644
@@ -609,8 +609,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
  		gup_flags |= FOLL_WRITE;
 
  	npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >> PAGE_SHIFT;
-	if (!npages)
-		return -EINVAL;
+	if (!npages) {
+		ret = -EINVAL;
+		goto free_page;
+	}
 
  	mmap_read_lock(dev->mm);
 
@@ -666,6 +668,8 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
  		atomic64_sub(npages, &dev->mm->pinned_vm);
  	}
  	mmap_read_unlock(dev->mm);
+
+free_page:
  	free_page((unsigned long)page_list);
  	return ret;
  }