[PATCH v2] virt: guest_memfd: fix reference leak on hwpoisoned page

Paolo Bonzini posted 1 patch 1 year, 8 months ago
virt/kvm/guest_memfd.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
[PATCH v2] virt: guest_memfd: fix reference leak on hwpoisoned page
Posted by Paolo Bonzini 1 year, 8 months ago
If __kvm_gmem_get_pfn() detects an hwpoisoned page, it returns -EHWPOISON
but it does not put back the reference that kvm_gmem_get_folio() had
grabbed.  Add the forgotten folio_put().

Fixes: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
Cc: stable@vger.kernel.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
	Sent v1 from the wrong directory, sorry about that.

 virt/kvm/guest_memfd.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 3bfe1824ec2d..19c220ec1efd 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -549,7 +549,6 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
 	struct kvm_gmem *gmem = file->private_data;
 	struct folio *folio;
 	struct page *page;
-	int r;
 
 	if (file != slot->gmem.file) {
 		WARN_ON_ONCE(slot->gmem.file);
@@ -567,8 +566,9 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
 		return PTR_ERR(folio);
 
 	if (folio_test_hwpoison(folio)) {
-		r = -EHWPOISON;
-		goto out_unlock;
+		folio_unlock(folio);
+		folio_put(folio);
+		return -EHWPOISON;
 	}
 
 	page = folio_file_page(folio, index);
@@ -577,12 +577,8 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
 	if (max_order)
 		*max_order = 0;
 
-	r = 0;
-
-out_unlock:
 	folio_unlock(folio);
-
-	return r;
+	return 0;
 }
 
 int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
-- 
2.43.0
Re: [PATCH v2] virt: guest_memfd: fix reference leak on hwpoisoned page
Posted by Isaku Yamahata 1 year, 8 months ago
On Tue, Jun 11, 2024 at 06:25:15AM -0400,
Paolo Bonzini <pbonzini@redhat.com> wrote:

> If __kvm_gmem_get_pfn() detects an hwpoisoned page, it returns -EHWPOISON
> but it does not put back the reference that kvm_gmem_get_folio() had
> grabbed.  Add the forgotten folio_put().
> 
> Fixes: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
> Cc: stable@vger.kernel.org
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com>

-- 
Isaku Yamahata <isaku.yamahata@intel.com>
Re: [PATCH v2] virt: guest_memfd: fix reference leak on hwpoisoned page
Posted by Liam Merwick 1 year, 8 months ago
On 11/06/2024 11:25, Paolo Bonzini wrote:
> If __kvm_gmem_get_pfn() detects an hwpoisoned page, it returns -EHWPOISON
> but it does not put back the reference that kvm_gmem_get_folio() had
> grabbed.  Add the forgotten folio_put().
> 
> Fixes: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
> Cc: stable@vger.kernel.org
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>


Reviewed-by: Liam Merwick <liam.merwick@oracle.com>