diff mbox series

[v13,04/26] KVM: guest_memfd: Fix PTR_ERR() handling in __kvm_gmem_get_pfn()

Message ID 20240418194133.1452059-5-michael.roth@amd.com (mailing list archive)
State Not Applicable
Delegated to: Herbert Xu
Headers show
Series Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support | expand

Commit Message

Michael Roth April 18, 2024, 7:41 p.m. UTC
kvm_gmem_get_folio() may return a PTR_ERR() rather than just NULL. In
particular, for cases where EEXISTS is returned when FGP_CREAT_ONLY
flag is used. Handle this properly in __kvm_gmem_get_pfn().

Signed-off-by: Michael Roth <michael.roth@amd.com>
---
 virt/kvm/guest_memfd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

David Hildenbrand April 19, 2024, 12:58 p.m. UTC | #1
On 18.04.24 21:41, Michael Roth wrote:
> kvm_gmem_get_folio() may return a PTR_ERR() rather than just NULL. In
> particular, for cases where EEXISTS is returned when FGP_CREAT_ONLY
> flag is used. Handle this properly in __kvm_gmem_get_pfn().
> 
> Signed-off-by: Michael Roth <michael.roth@amd.com>
> ---
>   virt/kvm/guest_memfd.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> index ccf22e44f387..9d7c6a70c547 100644
> --- a/virt/kvm/guest_memfd.c
> +++ b/virt/kvm/guest_memfd.c
> @@ -580,8 +580,8 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
>   	}
>   
>   	folio = kvm_gmem_get_folio(file_inode(file), index, prepare);
> -	if (!folio)
> -		return -ENOMEM;
> +	if (IS_ERR_OR_NULL(folio))
> +		return folio ? PTR_ERR(folio) : -ENOMEM;

Will it even return NULL?  Staring at other filemap_grab_folio() users, 
they all check for IS_ERR().

>   
>   	if (folio_test_hwpoison(folio)) {
>   		r = -EHWPOISON;

Do we have a Fixes: tag?
Michael Roth April 19, 2024, 3:11 p.m. UTC | #2
On Fri, Apr 19, 2024 at 02:58:43PM +0200, David Hildenbrand wrote:
> On 18.04.24 21:41, Michael Roth wrote:
> > kvm_gmem_get_folio() may return a PTR_ERR() rather than just NULL. In
> > particular, for cases where EEXISTS is returned when FGP_CREAT_ONLY
> > flag is used. Handle this properly in __kvm_gmem_get_pfn().
> > 
> > Signed-off-by: Michael Roth <michael.roth@amd.com>
> > ---
> >   virt/kvm/guest_memfd.c | 4 ++--
> >   1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> > index ccf22e44f387..9d7c6a70c547 100644
> > --- a/virt/kvm/guest_memfd.c
> > +++ b/virt/kvm/guest_memfd.c
> > @@ -580,8 +580,8 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
> >   	}
> >   	folio = kvm_gmem_get_folio(file_inode(file), index, prepare);
> > -	if (!folio)
> > -		return -ENOMEM;
> > +	if (IS_ERR_OR_NULL(folio))
> > +		return folio ? PTR_ERR(folio) : -ENOMEM;
> 
> Will it even return NULL?  Staring at other filemap_grab_folio() users, they
> all check for IS_ERR().

Looks like the NULL case is handled with PTR_ERR(-ENOENT), so IS_ERR()
would be sufficient. I think in the past kvm_gmem_get_folio() itself
would return NULL in some cases, but as of commit 2b01b7e994e95 that's
no longer the case.

I'll fix this up to expect only PTR_ERR() when I re-spin v14, and also
address the other kvm_gmem_get_folio() / __filemap_get_folio() call
sites.

> 
> >   	if (folio_test_hwpoison(folio)) {
> >   		r = -EHWPOISON;
> 
> Do we have a Fixes: tag?

Fixes: 2b01b7e994e95 ("KVM: guest_memfd: pass error up from filemap_grab_folio")

Will add that in the re-spin as well.

Thanks!

-Mike

> 
> -- 
> Cheers,
> 
> David / dhildenb
>
Paolo Bonzini April 19, 2024, 4:17 p.m. UTC | #3
On Fri, Apr 19, 2024 at 5:11 PM Michael Roth <michael.roth@amd.com> wrote:
>
> On Fri, Apr 19, 2024 at 02:58:43PM +0200, David Hildenbrand wrote:
> > On 18.04.24 21:41, Michael Roth wrote:
> > > kvm_gmem_get_folio() may return a PTR_ERR() rather than just NULL. In
> > > particular, for cases where EEXISTS is returned when FGP_CREAT_ONLY
> > > flag is used. Handle this properly in __kvm_gmem_get_pfn().
> > >
> > > Signed-off-by: Michael Roth <michael.roth@amd.com>
> > > ---
> > >   virt/kvm/guest_memfd.c | 4 ++--
> > >   1 file changed, 2 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> > > index ccf22e44f387..9d7c6a70c547 100644
> > > --- a/virt/kvm/guest_memfd.c
> > > +++ b/virt/kvm/guest_memfd.c
> > > @@ -580,8 +580,8 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
> > >     }
> > >     folio = kvm_gmem_get_folio(file_inode(file), index, prepare);
> > > -   if (!folio)
> > > -           return -ENOMEM;
> > > +   if (IS_ERR_OR_NULL(folio))
> > > +           return folio ? PTR_ERR(folio) : -ENOMEM;
> >
> > Will it even return NULL?  Staring at other filemap_grab_folio() users, they
> > all check for IS_ERR().
>
> Looks like the NULL case is handled with PTR_ERR(-ENOENT), so IS_ERR()
> would be sufficient. I think in the past kvm_gmem_get_folio() itself
> would return NULL in some cases, but as of commit 2b01b7e994e95 that's
> no longer the case.
>
> I'll fix this up to expect only PTR_ERR() when I re-spin v14, and also
> address the other kvm_gmem_get_folio() / __filemap_get_folio() call
> sites.
>
> >
> > >     if (folio_test_hwpoison(folio)) {
> > >             r = -EHWPOISON;
> >
> > Do we have a Fixes: tag?
>
> Fixes: 2b01b7e994e95 ("KVM: guest_memfd: pass error up from filemap_grab_folio")

I'll squash it so when you rebase on the new kvm-coco-queue it will go
away. Thanks to both!

Paolo
diff mbox series

Patch

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index ccf22e44f387..9d7c6a70c547 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -580,8 +580,8 @@  static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
 	}
 
 	folio = kvm_gmem_get_folio(file_inode(file), index, prepare);
-	if (!folio)
-		return -ENOMEM;
+	if (IS_ERR_OR_NULL(folio))
+		return folio ? PTR_ERR(folio) : -ENOMEM;
 
 	if (folio_test_hwpoison(folio)) {
 		r = -EHWPOISON;