Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-26 Thread Alex Williamson
On Tue, 26 May 2020 12:53:31 -0300
Jason Gunthorpe  wrote:

> On Tue, May 26, 2020 at 08:32:18AM -0600, Alex Williamson wrote:
> > > > Certainly there is no reason to optimize the fringe case of vfio
> > > > sleeping if there is and incorrect concurrnent attempt to disable the
> > > > a BAR.
> > > 
> > > If fixup_user_fault() (which is always with ALLOW_RETRY && !RETRY_NOWAIT) 
> > > is
> > > the only path for the new fault(), then current way seems ok.  Not sure 
> > > whether
> > > this would worth a WARN_ON_ONCE(RETRY_NOWAIT) in the fault() to be clear 
> > > of
> > > that fact.  
> > 
> > Thanks for the discussion over the weekend folks.  Peter, I take it
> > you'd be satisfied if this patch were updated as:
> > 
> > diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
> > index aabba6439a5b..35bd7cd4e268 100644
> > +++ b/drivers/vfio/pci/vfio_pci.c
> > @@ -1528,6 +1528,13 @@ static vm_fault_t vfio_pci_mmap_fault(struct 
> > vm_fault *vmf)
> > struct vfio_pci_device *vdev = vma->vm_private_data;
> > vm_fault_t ret = VM_FAULT_NOPAGE;
> >  
> > +   /*
> > +* We don't expect to be called with NOWAIT and there are conflicting
> > +* opinions on whether NOWAIT suggests we shouldn't wait for locks or
> > +* just shouldn't wait for I/O.
> > +*/
> > +   WARN_ON_ONCE(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);  
> 
> I don't think this is right, this implies there is some reason this
> code fails with FAULT_FLAG_RETRY_NOWAIT - but it is fine as written,
> AFAICT

Ok, Peter said he's fine either way, I'll use the patch as originally
posted and include Peter's R-b.  Thanks,

Alex



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-26 Thread Jason Gunthorpe
On Tue, May 26, 2020 at 08:32:18AM -0600, Alex Williamson wrote:
> > > Certainly there is no reason to optimize the fringe case of vfio
> > > sleeping if there is and incorrect concurrnent attempt to disable the
> > > a BAR.  
> > 
> > If fixup_user_fault() (which is always with ALLOW_RETRY && !RETRY_NOWAIT) is
> > the only path for the new fault(), then current way seems ok.  Not sure 
> > whether
> > this would worth a WARN_ON_ONCE(RETRY_NOWAIT) in the fault() to be clear of
> > that fact.
> 
> Thanks for the discussion over the weekend folks.  Peter, I take it
> you'd be satisfied if this patch were updated as:
> 
> diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
> index aabba6439a5b..35bd7cd4e268 100644
> +++ b/drivers/vfio/pci/vfio_pci.c
> @@ -1528,6 +1528,13 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault 
> *vmf)
>   struct vfio_pci_device *vdev = vma->vm_private_data;
>   vm_fault_t ret = VM_FAULT_NOPAGE;
>  
> + /*
> +  * We don't expect to be called with NOWAIT and there are conflicting
> +  * opinions on whether NOWAIT suggests we shouldn't wait for locks or
> +  * just shouldn't wait for I/O.
> +  */
> + WARN_ON_ONCE(vmf->flags & FAULT_FLAG_RETRY_NOWAIT);

I don't think this is right, this implies there is some reason this
code fails with FAULT_FLAG_RETRY_NOWAIT - but it is fine as written,
AFAICT

Jason


Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-26 Thread Peter Xu
On Tue, May 26, 2020 at 08:32:18AM -0600, Alex Williamson wrote:
> On Tue, 26 May 2020 09:49:54 -0400
> Peter Xu  wrote:
> 
> > On Mon, May 25, 2020 at 09:37:05PM -0300, Jason Gunthorpe wrote:
> > > On Mon, May 25, 2020 at 01:56:28PM -0700, John Hubbard wrote:  
> > > > On 2020-05-25 09:56, Jason Gunthorpe wrote:  
> > > > > On Mon, May 25, 2020 at 11:11:42AM -0400, Peter Xu wrote:  
> > > > > > On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:  
> > > > > > > On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:  
> > > > > > > > On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe 
> > > > > > > > wrote:  
> > > > > > > > > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > > > > > > > >   
> > > > > > > > > > For what I understand now, IMHO we should still need all 
> > > > > > > > > > those handlings of
> > > > > > > > > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., 
> > > > > > > > > > IIUC KVM gup will
> > > > > > > > > > try with FOLL_NOWAIT when async is allowed, before the 
> > > > > > > > > > complete slow path.  I'm
> > > > > > > > > > not sure what would be the side effect of that if fault() 
> > > > > > > > > > blocked it.  E.g.,
> > > > > > > > > > the caller could be in an atomic context.  
> > > > > > > > > 
> > > > > > > > > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > > > > > > > > VM_FAULT_RETRY is returned, which this doesn't do?  
> > > > > > > > 
> > > > > > > > Yes, that's why I think we should still properly return 
> > > > > > > > VM_FAULT_RETRY if
> > > > > > > > needed..  because IMHO it is still possible that the caller 
> > > > > > > > calls with
> > > > > > > > FAULT_FLAG_RETRY_NOWAIT.
> > > > > > > > 
> > > > > > > > My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> > > > > > > > 
> > > > > > > >- We cannot release the mmap_sem, and,
> > > > > > > >- We cannot sleep  
> > > > > > > 
> > > > > > > Sleeping looks fine, look at any FS implementation of fault, say,
> > > > > > > xfs. The first thing it does is xfs_ilock() which does 
> > > > > > > down_write().  
> > > > > > 
> > > > > > Yeah.  My wild guess is that maybe fs code will always be without
> > > > > > FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally 
> > > > > > (e.g., I think
> > > > > > the general #PF should be fine to sleep in fault(); gup should be 
> > > > > > special, but
> > > > > > I didn't observe any gup code called upon file systems)?  
> > > > > 
> > > > > get_user_pages is called on filesystem backed pages.
> > > > > 
> > > > > I have no idea what FAULT_FLAG_RETRY_NOWAIT is supposed to do. Maybe
> > > > > John was able to guess when he reworked that stuff?
> > > > >   
> > > > 
> > > > Although I didn't end up touching that particular area, I'm sure it's 
> > > > going
> > > > to come up sometime soon, so I poked around just now, and found that
> > > > FAULT_FLAG_RETRY_NOWAIT was added almost exactly 9 years ago. This flag 
> > > > was
> > > > intended to make KVM and similar things behave better when doing GUP on
> > > > file-backed pages that might, or might not be in memory.
> > > > 
> > > > The idea is described in the changelog, but not in the code comments or
> > > > Documentation, sigh:
> > > > 
> > > > commit 318b275fbca1ab9ec0862de71420e0e92c3d1aa7
> > > > Author: Gleb Natapov 
> > > > Date:   Tue Mar 22 16:30:51 2011 -0700
> > > > 
> > > > mm: allow GUP to fail instead of waiting on a page
> > > > 
> > > > GUP user may want to try to acquire a reference to a page if it is 
> > > > already
> > > > in memory, but not if IO, to bring it in, is needed.  For example 
> > > > KVM may
> > > > tell vcpu to schedule another guest process if current one is 
> > > > trying to
> > > > access swapped out page.  Meanwhile, the page will be swapped in 
> > > > and the
> > > > guest process, that depends on it, will be able to run again.
> > > > 
> > > > This patch adds FAULT_FLAG_RETRY_NOWAIT (suggested by Linus) and
> > > > FOLL_NOWAIT follow_page flags.  FAULT_FLAG_RETRY_NOWAIT, when used 
> > > > in
> > > > conjunction with VM_FAULT_ALLOW_RETRY, indicates to handle_mm_fault 
> > > > that
> > > > it shouldn't drop mmap_sem and wait on a page, but return 
> > > > VM_FAULT_RETRY
> > > > instead.  
> > > 
> > > So, from kvm's perspective it was to avoid excessively long blocking in
> > > common paths when it could rejoin the completed IO by somehow waiting
> > > on a page itself?
> > > 
> > > It all seems like it should not be used unless the page is going to go
> > > to IO?  
> > 
> > I think NOWAIT is used as a common flag for kvm for its initial attempt to
> > fault in a normal page, however...  I just noticed another fact that 
> > actually
> > __get_user_pages() won't work with PFNMAP (check_vma_flags should fail), but
> > KVM just started to support fault() for PFNMAP from commit add6a0cd1c5b 
> > (2016)
> > using fixup_user_fault(), where nvidia seems 

Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-26 Thread Alex Williamson
On Tue, 26 May 2020 09:49:54 -0400
Peter Xu  wrote:

> On Mon, May 25, 2020 at 09:37:05PM -0300, Jason Gunthorpe wrote:
> > On Mon, May 25, 2020 at 01:56:28PM -0700, John Hubbard wrote:  
> > > On 2020-05-25 09:56, Jason Gunthorpe wrote:  
> > > > On Mon, May 25, 2020 at 11:11:42AM -0400, Peter Xu wrote:  
> > > > > On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:  
> > > > > > On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:  
> > > > > > > On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:  
> > > > > > > > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > > > > > > >   
> > > > > > > > > For what I understand now, IMHO we should still need all 
> > > > > > > > > those handlings of
> > > > > > > > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., 
> > > > > > > > > IIUC KVM gup will
> > > > > > > > > try with FOLL_NOWAIT when async is allowed, before the 
> > > > > > > > > complete slow path.  I'm
> > > > > > > > > not sure what would be the side effect of that if fault() 
> > > > > > > > > blocked it.  E.g.,
> > > > > > > > > the caller could be in an atomic context.  
> > > > > > > > 
> > > > > > > > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > > > > > > > VM_FAULT_RETRY is returned, which this doesn't do?  
> > > > > > > 
> > > > > > > Yes, that's why I think we should still properly return 
> > > > > > > VM_FAULT_RETRY if
> > > > > > > needed..  because IMHO it is still possible that the caller calls 
> > > > > > > with
> > > > > > > FAULT_FLAG_RETRY_NOWAIT.
> > > > > > > 
> > > > > > > My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> > > > > > > 
> > > > > > >- We cannot release the mmap_sem, and,
> > > > > > >- We cannot sleep  
> > > > > > 
> > > > > > Sleeping looks fine, look at any FS implementation of fault, say,
> > > > > > xfs. The first thing it does is xfs_ilock() which does 
> > > > > > down_write().  
> > > > > 
> > > > > Yeah.  My wild guess is that maybe fs code will always be without
> > > > > FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally (e.g., 
> > > > > I think
> > > > > the general #PF should be fine to sleep in fault(); gup should be 
> > > > > special, but
> > > > > I didn't observe any gup code called upon file systems)?  
> > > > 
> > > > get_user_pages is called on filesystem backed pages.
> > > > 
> > > > I have no idea what FAULT_FLAG_RETRY_NOWAIT is supposed to do. Maybe
> > > > John was able to guess when he reworked that stuff?
> > > >   
> > > 
> > > Although I didn't end up touching that particular area, I'm sure it's 
> > > going
> > > to come up sometime soon, so I poked around just now, and found that
> > > FAULT_FLAG_RETRY_NOWAIT was added almost exactly 9 years ago. This flag 
> > > was
> > > intended to make KVM and similar things behave better when doing GUP on
> > > file-backed pages that might, or might not be in memory.
> > > 
> > > The idea is described in the changelog, but not in the code comments or
> > > Documentation, sigh:
> > > 
> > > commit 318b275fbca1ab9ec0862de71420e0e92c3d1aa7
> > > Author: Gleb Natapov 
> > > Date:   Tue Mar 22 16:30:51 2011 -0700
> > > 
> > > mm: allow GUP to fail instead of waiting on a page
> > > 
> > > GUP user may want to try to acquire a reference to a page if it is 
> > > already
> > > in memory, but not if IO, to bring it in, is needed.  For example KVM 
> > > may
> > > tell vcpu to schedule another guest process if current one is trying 
> > > to
> > > access swapped out page.  Meanwhile, the page will be swapped in and 
> > > the
> > > guest process, that depends on it, will be able to run again.
> > > 
> > > This patch adds FAULT_FLAG_RETRY_NOWAIT (suggested by Linus) and
> > > FOLL_NOWAIT follow_page flags.  FAULT_FLAG_RETRY_NOWAIT, when used in
> > > conjunction with VM_FAULT_ALLOW_RETRY, indicates to handle_mm_fault 
> > > that
> > > it shouldn't drop mmap_sem and wait on a page, but return 
> > > VM_FAULT_RETRY
> > > instead.  
> > 
> > So, from kvm's perspective it was to avoid excessively long blocking in
> > common paths when it could rejoin the completed IO by somehow waiting
> > on a page itself?
> > 
> > It all seems like it should not be used unless the page is going to go
> > to IO?  
> 
> I think NOWAIT is used as a common flag for kvm for its initial attempt to
> fault in a normal page, however...  I just noticed another fact that actually
> __get_user_pages() won't work with PFNMAP (check_vma_flags should fail), but
> KVM just started to support fault() for PFNMAP from commit add6a0cd1c5b (2016)
> using fixup_user_fault(), where nvidia seems to have a similar request to have
> a fault handler on some mapped BARs.
> 
> > 
> > Certainly there is no reason to optimize the fringe case of vfio
> > sleeping if there is and incorrect concurrnent attempt to disable the
> > a BAR.  
> 
> If fixup_user_fault() (which is always with ALLOW_RETRY && 

Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-26 Thread Peter Xu
On Mon, May 25, 2020 at 09:37:05PM -0300, Jason Gunthorpe wrote:
> On Mon, May 25, 2020 at 01:56:28PM -0700, John Hubbard wrote:
> > On 2020-05-25 09:56, Jason Gunthorpe wrote:
> > > On Mon, May 25, 2020 at 11:11:42AM -0400, Peter Xu wrote:
> > > > On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:
> > > > > On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:
> > > > > > On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:
> > > > > > > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > > > > > > 
> > > > > > > > For what I understand now, IMHO we should still need all those 
> > > > > > > > handlings of
> > > > > > > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., 
> > > > > > > > IIUC KVM gup will
> > > > > > > > try with FOLL_NOWAIT when async is allowed, before the complete 
> > > > > > > > slow path.  I'm
> > > > > > > > not sure what would be the side effect of that if fault() 
> > > > > > > > blocked it.  E.g.,
> > > > > > > > the caller could be in an atomic context.
> > > > > > > 
> > > > > > > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > > > > > > VM_FAULT_RETRY is returned, which this doesn't do?
> > > > > > 
> > > > > > Yes, that's why I think we should still properly return 
> > > > > > VM_FAULT_RETRY if
> > > > > > needed..  because IMHO it is still possible that the caller calls 
> > > > > > with
> > > > > > FAULT_FLAG_RETRY_NOWAIT.
> > > > > > 
> > > > > > My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> > > > > > 
> > > > > >- We cannot release the mmap_sem, and,
> > > > > >- We cannot sleep
> > > > > 
> > > > > Sleeping looks fine, look at any FS implementation of fault, say,
> > > > > xfs. The first thing it does is xfs_ilock() which does down_write().
> > > > 
> > > > Yeah.  My wild guess is that maybe fs code will always be without
> > > > FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally (e.g., I 
> > > > think
> > > > the general #PF should be fine to sleep in fault(); gup should be 
> > > > special, but
> > > > I didn't observe any gup code called upon file systems)?
> > > 
> > > get_user_pages is called on filesystem backed pages.
> > > 
> > > I have no idea what FAULT_FLAG_RETRY_NOWAIT is supposed to do. Maybe
> > > John was able to guess when he reworked that stuff?
> > > 
> > 
> > Although I didn't end up touching that particular area, I'm sure it's going
> > to come up sometime soon, so I poked around just now, and found that
> > FAULT_FLAG_RETRY_NOWAIT was added almost exactly 9 years ago. This flag was
> > intended to make KVM and similar things behave better when doing GUP on
> > file-backed pages that might, or might not be in memory.
> > 
> > The idea is described in the changelog, but not in the code comments or
> > Documentation, sigh:
> > 
> > commit 318b275fbca1ab9ec0862de71420e0e92c3d1aa7
> > Author: Gleb Natapov 
> > Date:   Tue Mar 22 16:30:51 2011 -0700
> > 
> > mm: allow GUP to fail instead of waiting on a page
> > 
> > GUP user may want to try to acquire a reference to a page if it is 
> > already
> > in memory, but not if IO, to bring it in, is needed.  For example KVM 
> > may
> > tell vcpu to schedule another guest process if current one is trying to
> > access swapped out page.  Meanwhile, the page will be swapped in and the
> > guest process, that depends on it, will be able to run again.
> > 
> > This patch adds FAULT_FLAG_RETRY_NOWAIT (suggested by Linus) and
> > FOLL_NOWAIT follow_page flags.  FAULT_FLAG_RETRY_NOWAIT, when used in
> > conjunction with VM_FAULT_ALLOW_RETRY, indicates to handle_mm_fault that
> > it shouldn't drop mmap_sem and wait on a page, but return VM_FAULT_RETRY
> > instead.
> 
> So, from kvm's perspective it was to avoid excessively long blocking in
> common paths when it could rejoin the completed IO by somehow waiting
> on a page itself?
> 
> It all seems like it should not be used unless the page is going to go
> to IO?

I think NOWAIT is used as a common flag for kvm for its initial attempt to
fault in a normal page, however...  I just noticed another fact that actually
__get_user_pages() won't work with PFNMAP (check_vma_flags should fail), but
KVM just started to support fault() for PFNMAP from commit add6a0cd1c5b (2016)
using fixup_user_fault(), where nvidia seems to have a similar request to have
a fault handler on some mapped BARs.

> 
> Certainly there is no reason to optimize the fringe case of vfio
> sleeping if there is and incorrect concurrnent attempt to disable the
> a BAR.

If fixup_user_fault() (which is always with ALLOW_RETRY && !RETRY_NOWAIT) is
the only path for the new fault(), then current way seems ok.  Not sure whether
this would worth a WARN_ON_ONCE(RETRY_NOWAIT) in the fault() to be clear of
that fact.

Thanks,

-- 
Peter Xu



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread John Hubbard

On 2020-05-25 17:37, Jason Gunthorpe wrote:
...

commit 318b275fbca1ab9ec0862de71420e0e92c3d1aa7
Author: Gleb Natapov 
Date:   Tue Mar 22 16:30:51 2011 -0700

 mm: allow GUP to fail instead of waiting on a page

 GUP user may want to try to acquire a reference to a page if it is already
 in memory, but not if IO, to bring it in, is needed.  For example KVM may
 tell vcpu to schedule another guest process if current one is trying to
 access swapped out page.  Meanwhile, the page will be swapped in and the
 guest process, that depends on it, will be able to run again.

 This patch adds FAULT_FLAG_RETRY_NOWAIT (suggested by Linus) and
 FOLL_NOWAIT follow_page flags.  FAULT_FLAG_RETRY_NOWAIT, when used in
 conjunction with VM_FAULT_ALLOW_RETRY, indicates to handle_mm_fault that
 it shouldn't drop mmap_sem and wait on a page, but return VM_FAULT_RETRY
 instead.


So, from kvm's perspective it was to avoid excessively long blocking in
common paths when it could rejoin the completed IO by somehow waiting
on a page itself?



Or perhaps some variation on that, such as just retrying with an intervening
schedule() call. It's not clear just from that commit.



It all seems like it should not be used unless the page is going to go
to IO?



That's my conclusion so far, yes.




Certainly there is no reason to optimize the fringe case of vfio
sleeping if there is and incorrect concurrnent attempt to disable the
a BAR.



Definitely agree with that position.




diff --git a/include/linux/mm.h b/include/linux/mm.h
index 8429d5aa31e44..e32e8e52a57ac 100644
+++ b/include/linux/mm.h
@@ -430,6 +430,15 @@ extern pgprot_t protection_map[16];
   * continuous faults with flags (b).  We should always try to detect pending
   * signals before a retry to make sure the continuous page faults can still be
   * interrupted if necessary.
+ *
+ * About @FAULT_FLAG_RETRY_NOWAIT: this is intended for callers who would like
+ * to acquire a page, but only if the page is already in memory. If, on the
+ * other hand, the page requires IO in order to bring it into memory, then 
fault
+ * handlers will immediately return VM_FAULT_RETRY ("don't wait"), while 
leaving
+ * mmap_lock held ("don't drop mmap_lock"). For example, this is useful for
+ * virtual machines that have multiple guests running: if guest A attempts
+ * get_user_pages() on a swapped out page, another guest can be scheduled while
+ * waiting for IO to swap in guest A's page.
   */
  #define FAULT_FLAG_WRITE   0x01
  #define FAULT_FLAG_MKWRITE 0x02


It seems reasonable but people might complain about the kvm
specifics of the explanation.

It might be better to explain how the caller is supposed to know when
it is OK to try GUP again and expect success, as it seems to me this
is really about externalizing the sleep for page wait?



OK, good point. The example was helpful in the commit log, but not quite
appropriate in mm.h, yes.

thanks,
--
John Hubbard
NVIDIA


Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread Jason Gunthorpe
On Mon, May 25, 2020 at 01:56:28PM -0700, John Hubbard wrote:
> On 2020-05-25 09:56, Jason Gunthorpe wrote:
> > On Mon, May 25, 2020 at 11:11:42AM -0400, Peter Xu wrote:
> > > On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:
> > > > On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:
> > > > > On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:
> > > > > > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > > > > > 
> > > > > > > For what I understand now, IMHO we should still need all those 
> > > > > > > handlings of
> > > > > > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC 
> > > > > > > KVM gup will
> > > > > > > try with FOLL_NOWAIT when async is allowed, before the complete 
> > > > > > > slow path.  I'm
> > > > > > > not sure what would be the side effect of that if fault() blocked 
> > > > > > > it.  E.g.,
> > > > > > > the caller could be in an atomic context.
> > > > > > 
> > > > > > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > > > > > VM_FAULT_RETRY is returned, which this doesn't do?
> > > > > 
> > > > > Yes, that's why I think we should still properly return 
> > > > > VM_FAULT_RETRY if
> > > > > needed..  because IMHO it is still possible that the caller calls with
> > > > > FAULT_FLAG_RETRY_NOWAIT.
> > > > > 
> > > > > My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> > > > > 
> > > > >- We cannot release the mmap_sem, and,
> > > > >- We cannot sleep
> > > > 
> > > > Sleeping looks fine, look at any FS implementation of fault, say,
> > > > xfs. The first thing it does is xfs_ilock() which does down_write().
> > > 
> > > Yeah.  My wild guess is that maybe fs code will always be without
> > > FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally (e.g., I 
> > > think
> > > the general #PF should be fine to sleep in fault(); gup should be 
> > > special, but
> > > I didn't observe any gup code called upon file systems)?
> > 
> > get_user_pages is called on filesystem backed pages.
> > 
> > I have no idea what FAULT_FLAG_RETRY_NOWAIT is supposed to do. Maybe
> > John was able to guess when he reworked that stuff?
> > 
> 
> Although I didn't end up touching that particular area, I'm sure it's going
> to come up sometime soon, so I poked around just now, and found that
> FAULT_FLAG_RETRY_NOWAIT was added almost exactly 9 years ago. This flag was
> intended to make KVM and similar things behave better when doing GUP on
> file-backed pages that might, or might not be in memory.
> 
> The idea is described in the changelog, but not in the code comments or
> Documentation, sigh:
> 
> commit 318b275fbca1ab9ec0862de71420e0e92c3d1aa7
> Author: Gleb Natapov 
> Date:   Tue Mar 22 16:30:51 2011 -0700
> 
> mm: allow GUP to fail instead of waiting on a page
> 
> GUP user may want to try to acquire a reference to a page if it is already
> in memory, but not if IO, to bring it in, is needed.  For example KVM may
> tell vcpu to schedule another guest process if current one is trying to
> access swapped out page.  Meanwhile, the page will be swapped in and the
> guest process, that depends on it, will be able to run again.
> 
> This patch adds FAULT_FLAG_RETRY_NOWAIT (suggested by Linus) and
> FOLL_NOWAIT follow_page flags.  FAULT_FLAG_RETRY_NOWAIT, when used in
> conjunction with VM_FAULT_ALLOW_RETRY, indicates to handle_mm_fault that
> it shouldn't drop mmap_sem and wait on a page, but return VM_FAULT_RETRY
> instead.

So, from kvm's perspective it was to avoid excessively long blocking in
common paths when it could rejoin the completed IO by somehow waiting
on a page itself?

It all seems like it should not be used unless the page is going to go
to IO?

Certainly there is no reason to optimize the fringe case of vfio
sleeping if there is and incorrect concurrnent attempt to disable the
a BAR.

> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 8429d5aa31e44..e32e8e52a57ac 100644
> +++ b/include/linux/mm.h
> @@ -430,6 +430,15 @@ extern pgprot_t protection_map[16];
>   * continuous faults with flags (b).  We should always try to detect pending
>   * signals before a retry to make sure the continuous page faults can still 
> be
>   * interrupted if necessary.
> + *
> + * About @FAULT_FLAG_RETRY_NOWAIT: this is intended for callers who would 
> like
> + * to acquire a page, but only if the page is already in memory. If, on the
> + * other hand, the page requires IO in order to bring it into memory, then 
> fault
> + * handlers will immediately return VM_FAULT_RETRY ("don't wait"), while 
> leaving
> + * mmap_lock held ("don't drop mmap_lock"). For example, this is useful for
> + * virtual machines that have multiple guests running: if guest A attempts
> + * get_user_pages() on a swapped out page, another guest can be scheduled 
> while
> + * waiting for IO to swap in guest A's page.
>   */
>  #define FAULT_FLAG_WRITE   

Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread John Hubbard

On 2020-05-25 09:56, Jason Gunthorpe wrote:

On Mon, May 25, 2020 at 11:11:42AM -0400, Peter Xu wrote:

On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:

On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:

On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:

On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:


For what I understand now, IMHO we should still need all those handlings of
FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM gup will
try with FOLL_NOWAIT when async is allowed, before the complete slow path.  I'm
not sure what would be the side effect of that if fault() blocked it.  E.g.,
the caller could be in an atomic context.


AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
VM_FAULT_RETRY is returned, which this doesn't do?


Yes, that's why I think we should still properly return VM_FAULT_RETRY if
needed..  because IMHO it is still possible that the caller calls with
FAULT_FLAG_RETRY_NOWAIT.

My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:

   - We cannot release the mmap_sem, and,
   - We cannot sleep


Sleeping looks fine, look at any FS implementation of fault, say,
xfs. The first thing it does is xfs_ilock() which does down_write().


Yeah.  My wild guess is that maybe fs code will always be without
FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally (e.g., I think
the general #PF should be fine to sleep in fault(); gup should be special, but
I didn't observe any gup code called upon file systems)?


get_user_pages is called on filesystem backed pages.

I have no idea what FAULT_FLAG_RETRY_NOWAIT is supposed to do. Maybe
John was able to guess when he reworked that stuff?



Although I didn't end up touching that particular area, I'm sure it's going
to come up sometime soon, so I poked around just now, and found that
FAULT_FLAG_RETRY_NOWAIT was added almost exactly 9 years ago. This flag was
intended to make KVM and similar things behave better when doing GUP on
file-backed pages that might, or might not be in memory.

The idea is described in the changelog, but not in the code comments or
Documentation, sigh:

commit 318b275fbca1ab9ec0862de71420e0e92c3d1aa7
Author: Gleb Natapov 
Date:   Tue Mar 22 16:30:51 2011 -0700

mm: allow GUP to fail instead of waiting on a page

GUP user may want to try to acquire a reference to a page if it is already
in memory, but not if IO, to bring it in, is needed.  For example KVM may
tell vcpu to schedule another guest process if current one is trying to
access swapped out page.  Meanwhile, the page will be swapped in and the
guest process, that depends on it, will be able to run again.

This patch adds FAULT_FLAG_RETRY_NOWAIT (suggested by Linus) and
FOLL_NOWAIT follow_page flags.  FAULT_FLAG_RETRY_NOWAIT, when used in
conjunction with VM_FAULT_ALLOW_RETRY, indicates to handle_mm_fault that
it shouldn't drop mmap_sem and wait on a page, but return VM_FAULT_RETRY
instead.

If that helps, maybe documentation approximately like this might be welcome
(against linux-next, so I'm using mmap_lock, instead of mmap_sem), below.
Or is this overkill? People like minimal documentation in the code, so maybe
this belongs in Documentation, if anywhere:

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 8429d5aa31e44..e32e8e52a57ac 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -430,6 +430,15 @@ extern pgprot_t protection_map[16];
  * continuous faults with flags (b).  We should always try to detect pending
  * signals before a retry to make sure the continuous page faults can still be
  * interrupted if necessary.
+ *
+ * About @FAULT_FLAG_RETRY_NOWAIT: this is intended for callers who would like
+ * to acquire a page, but only if the page is already in memory. If, on the
+ * other hand, the page requires IO in order to bring it into memory, then 
fault
+ * handlers will immediately return VM_FAULT_RETRY ("don't wait"), while 
leaving
+ * mmap_lock held ("don't drop mmap_lock"). For example, this is useful for
+ * virtual machines that have multiple guests running: if guest A attempts
+ * get_user_pages() on a swapped out page, another guest can be scheduled while
+ * waiting for IO to swap in guest A's page.
  */
 #define FAULT_FLAG_WRITE   0x01
 #define FAULT_FLAG_MKWRITE 0x02


thanks,
--
John Hubbard
NVIDIA


Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread Jason Gunthorpe
On Mon, May 25, 2020 at 11:11:42AM -0400, Peter Xu wrote:
> On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:
> > On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:
> > > On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:
> > > > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > > > 
> > > > > For what I understand now, IMHO we should still need all those 
> > > > > handlings of
> > > > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM 
> > > > > gup will
> > > > > try with FOLL_NOWAIT when async is allowed, before the complete slow 
> > > > > path.  I'm
> > > > > not sure what would be the side effect of that if fault() blocked it. 
> > > > >  E.g.,
> > > > > the caller could be in an atomic context.
> > > > 
> > > > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > > > VM_FAULT_RETRY is returned, which this doesn't do?
> > > 
> > > Yes, that's why I think we should still properly return VM_FAULT_RETRY if
> > > needed..  because IMHO it is still possible that the caller calls with
> > > FAULT_FLAG_RETRY_NOWAIT.
> > > 
> > > My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> > > 
> > >   - We cannot release the mmap_sem, and,
> > >   - We cannot sleep
> > 
> > Sleeping looks fine, look at any FS implementation of fault, say,
> > xfs. The first thing it does is xfs_ilock() which does down_write().
> 
> Yeah.  My wild guess is that maybe fs code will always be without
> FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally (e.g., I think
> the general #PF should be fine to sleep in fault(); gup should be special, but
> I didn't observe any gup code called upon file systems)?

get_user_pages is called on filesystem backed pages.

I have no idea what FAULT_FLAG_RETRY_NOWAIT is supposed to do. Maybe
John was able to guess when he reworked that stuff?

Jason


Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread Peter Xu
On Mon, May 25, 2020 at 11:46:51AM -0300, Jason Gunthorpe wrote:
> On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:
> > On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:
> > > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > > 
> > > > For what I understand now, IMHO we should still need all those 
> > > > handlings of
> > > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM 
> > > > gup will
> > > > try with FOLL_NOWAIT when async is allowed, before the complete slow 
> > > > path.  I'm
> > > > not sure what would be the side effect of that if fault() blocked it.  
> > > > E.g.,
> > > > the caller could be in an atomic context.
> > > 
> > > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > > VM_FAULT_RETRY is returned, which this doesn't do?
> > 
> > Yes, that's why I think we should still properly return VM_FAULT_RETRY if
> > needed..  because IMHO it is still possible that the caller calls with
> > FAULT_FLAG_RETRY_NOWAIT.
> > 
> > My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> > 
> >   - We cannot release the mmap_sem, and,
> >   - We cannot sleep
> 
> Sleeping looks fine, look at any FS implementation of fault, say,
> xfs. The first thing it does is xfs_ilock() which does down_write().

Yeah.  My wild guess is that maybe fs code will always be without
FAULT_FLAG_RETRY_NOWAIT so it's safe to sleep unconditionally (e.g., I think
the general #PF should be fine to sleep in fault(); gup should be special, but
I didn't observe any gup code called upon file systems)?

Or I must have missed something important...

Thanks,

-- 
Peter Xu



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread Jason Gunthorpe
On Mon, May 25, 2020 at 10:28:06AM -0400, Peter Xu wrote:
> On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:
> > On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> > 
> > > For what I understand now, IMHO we should still need all those handlings 
> > > of
> > > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM gup 
> > > will
> > > try with FOLL_NOWAIT when async is allowed, before the complete slow 
> > > path.  I'm
> > > not sure what would be the side effect of that if fault() blocked it.  
> > > E.g.,
> > > the caller could be in an atomic context.
> > 
> > AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> > VM_FAULT_RETRY is returned, which this doesn't do?
> 
> Yes, that's why I think we should still properly return VM_FAULT_RETRY if
> needed..  because IMHO it is still possible that the caller calls with
> FAULT_FLAG_RETRY_NOWAIT.
> 
> My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:
> 
>   - We cannot release the mmap_sem, and,
>   - We cannot sleep

Sleeping looks fine, look at any FS implementation of fault, say,
xfs. The first thing it does is xfs_ilock() which does down_write().

I can't say when VM_FAULT_RETRY comes into play, but it is not so
simple as just sleeping..

Jason


Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread Peter Xu
On Mon, May 25, 2020 at 09:26:07AM -0300, Jason Gunthorpe wrote:
> On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> 
> > For what I understand now, IMHO we should still need all those handlings of
> > FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM gup 
> > will
> > try with FOLL_NOWAIT when async is allowed, before the complete slow path.  
> > I'm
> > not sure what would be the side effect of that if fault() blocked it.  E.g.,
> > the caller could be in an atomic context.
> 
> AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
> VM_FAULT_RETRY is returned, which this doesn't do?

Yes, that's why I think we should still properly return VM_FAULT_RETRY if
needed..  because IMHO it is still possible that the caller calls with
FAULT_FLAG_RETRY_NOWAIT.

My understanding is that FAULT_FLAG_RETRY_NOWAIT majorly means:

  - We cannot release the mmap_sem, and,
  - We cannot sleep

But we're allowed to return VM_FAULT_RETRY if any of the above is necessary.

Thanks,

-- 
Peter Xu



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-25 Thread Jason Gunthorpe
On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:

> For what I understand now, IMHO we should still need all those handlings of
> FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM gup will
> try with FOLL_NOWAIT when async is allowed, before the complete slow path.  
> I'm
> not sure what would be the side effect of that if fault() blocked it.  E.g.,
> the caller could be in an atomic context.

AFAICT FAULT_FLAG_RETRY_NOWAIT only impacts what happens when
VM_FAULT_RETRY is returned, which this doesn't do?

It is not a generic 'do not sleep'

Do you know different?

Jason


Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-23 Thread Peter Xu
(CC Andrea too)

On Sat, May 23, 2020 at 07:52:57PM -0400, Peter Xu wrote:
> On Sat, May 23, 2020 at 05:06:02PM -0600, Alex Williamson wrote:
> > On Sat, 23 May 2020 15:34:17 -0400
> > Peter Xu  wrote:
> > 
> > > Hi, Alex,
> > > 
> > > On Fri, May 22, 2020 at 01:17:43PM -0600, Alex Williamson wrote:
> > > > @@ -1346,15 +1526,32 @@ static vm_fault_t vfio_pci_mmap_fault(struct 
> > > > vm_fault *vmf)
> > > >  {
> > > > struct vm_area_struct *vma = vmf->vma;
> > > > struct vfio_pci_device *vdev = vma->vm_private_data;
> > > > +   vm_fault_t ret = VM_FAULT_NOPAGE;
> > > > +
> > > > +   mutex_lock(>vma_lock);
> > > > +   down_read(>memory_lock);  
> > > 
> > > I remembered to have seen the fault() handling FAULT_FLAG_RETRY_NOWAIT at 
> > > least
> > > in the very first version, but it's not here any more...  Could I ask 
> > > what's
> > > the reason behind?  I probably have missed something along with the 
> > > versions,
> > > I'm just not sure whether e.g. this would potentially block a GUP caller 
> > > even
> > > if it's with FOLL_NOWAIT.
> > 
> > This is largely what v2 was about, from the cover letter:
> > 
> > Locking in 3/ is substantially changed to avoid the retry scenario
> > within the fault handler, therefore a caller who does not allow
> > retry will no longer receive a SIGBUS on contention.
> > 
> > The discussion thread starts here:
> > 
> > https://lore.kernel.org/kvm/20200501234849.gq26...@ziepe.ca/
> 
> [1]
> 
> > 
> > Feel free to interject if there's something that doesn't make sense,
> > the idea is that since we've fixed the lock ordering we never need to
> > release one lock to wait for another, therefore we can wait for the
> > lock.  I'm under the impression that we can wait for the lock
> > regardless of the flags under these conditions.
> 
> I see; thanks for the link.  Sorry I should probably follow up the discussion
> and ask the question earlier, anyway...
> 
> For what I understand now, IMHO we should still need all those handlings of
> FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM gup will
> try with FOLL_NOWAIT when async is allowed, before the complete slow path.  
> I'm
> not sure what would be the side effect of that if fault() blocked it.  E.g.,
> the caller could be in an atomic context.
> 
> But now I also agree that VM_FAULT_SIGBUS is probably not correct there in the
> initial version [1] - I thought it was OK initially (after all after the
> multiple fault retry series we should always be with 
> FAULT_FLAG_ALLOW_RETRY..).
> However after some thinking... it should be the common slow path where retry 
> is
> simply not allowed.  So IMHO instead of SIGBUS there, we should also use all
> the slow path of the locks.  That'll be safe then because it's never going to
> be with FAULT_FLAG_RETRY_NOWAIT (FAULT_FLAG_RETRY_NOWAIT depends on
> FAULT_FLAG_ALLOW_RETRY).
> 
> A reference code could be __lock_page_or_retry() where the lock_page could 
> wait
> just like we taking the sems/mutexes, and the previous SIGBUS case would
> corresponds to this chunk of __lock_page_or_retry():
> 
>   } else {
>   if (flags & FAULT_FLAG_KILLABLE) {
>   int ret;
> 
>   ret = __lock_page_killable(page);
>   if (ret) {
>   up_read(>mmap_sem);
>   return 0;
>   }
>   } else
>   __lock_page(page);
>   return 1;
>   }
> 
> Thanks,
> 
> -- 
> Peter Xu

-- 
Peter Xu



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-23 Thread Peter Xu
On Sat, May 23, 2020 at 05:06:02PM -0600, Alex Williamson wrote:
> On Sat, 23 May 2020 15:34:17 -0400
> Peter Xu  wrote:
> 
> > Hi, Alex,
> > 
> > On Fri, May 22, 2020 at 01:17:43PM -0600, Alex Williamson wrote:
> > > @@ -1346,15 +1526,32 @@ static vm_fault_t vfio_pci_mmap_fault(struct 
> > > vm_fault *vmf)
> > >  {
> > >   struct vm_area_struct *vma = vmf->vma;
> > >   struct vfio_pci_device *vdev = vma->vm_private_data;
> > > + vm_fault_t ret = VM_FAULT_NOPAGE;
> > > +
> > > + mutex_lock(>vma_lock);
> > > + down_read(>memory_lock);  
> > 
> > I remembered to have seen the fault() handling FAULT_FLAG_RETRY_NOWAIT at 
> > least
> > in the very first version, but it's not here any more...  Could I ask what's
> > the reason behind?  I probably have missed something along with the 
> > versions,
> > I'm just not sure whether e.g. this would potentially block a GUP caller 
> > even
> > if it's with FOLL_NOWAIT.
> 
> This is largely what v2 was about, from the cover letter:
> 
> Locking in 3/ is substantially changed to avoid the retry scenario
> within the fault handler, therefore a caller who does not allow
> retry will no longer receive a SIGBUS on contention.
> 
> The discussion thread starts here:
> 
> https://lore.kernel.org/kvm/20200501234849.gq26...@ziepe.ca/

[1]

> 
> Feel free to interject if there's something that doesn't make sense,
> the idea is that since we've fixed the lock ordering we never need to
> release one lock to wait for another, therefore we can wait for the
> lock.  I'm under the impression that we can wait for the lock
> regardless of the flags under these conditions.

I see; thanks for the link.  Sorry I should probably follow up the discussion
and ask the question earlier, anyway...

For what I understand now, IMHO we should still need all those handlings of
FAULT_FLAG_RETRY_NOWAIT like in the initial version.  E.g., IIUC KVM gup will
try with FOLL_NOWAIT when async is allowed, before the complete slow path.  I'm
not sure what would be the side effect of that if fault() blocked it.  E.g.,
the caller could be in an atomic context.

But now I also agree that VM_FAULT_SIGBUS is probably not correct there in the
initial version [1] - I thought it was OK initially (after all after the
multiple fault retry series we should always be with FAULT_FLAG_ALLOW_RETRY..).
However after some thinking... it should be the common slow path where retry is
simply not allowed.  So IMHO instead of SIGBUS there, we should also use all
the slow path of the locks.  That'll be safe then because it's never going to
be with FAULT_FLAG_RETRY_NOWAIT (FAULT_FLAG_RETRY_NOWAIT depends on
FAULT_FLAG_ALLOW_RETRY).

A reference code could be __lock_page_or_retry() where the lock_page could wait
just like we taking the sems/mutexes, and the previous SIGBUS case would
corresponds to this chunk of __lock_page_or_retry():

} else {
if (flags & FAULT_FLAG_KILLABLE) {
int ret;

ret = __lock_page_killable(page);
if (ret) {
up_read(>mmap_sem);
return 0;
}
} else
__lock_page(page);
return 1;
}

Thanks,

-- 
Peter Xu



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-23 Thread Alex Williamson
On Sat, 23 May 2020 15:34:17 -0400
Peter Xu  wrote:

> Hi, Alex,
> 
> On Fri, May 22, 2020 at 01:17:43PM -0600, Alex Williamson wrote:
> > @@ -1346,15 +1526,32 @@ static vm_fault_t vfio_pci_mmap_fault(struct 
> > vm_fault *vmf)
> >  {
> > struct vm_area_struct *vma = vmf->vma;
> > struct vfio_pci_device *vdev = vma->vm_private_data;
> > +   vm_fault_t ret = VM_FAULT_NOPAGE;
> > +
> > +   mutex_lock(>vma_lock);
> > +   down_read(>memory_lock);  
> 
> I remembered to have seen the fault() handling FAULT_FLAG_RETRY_NOWAIT at 
> least
> in the very first version, but it's not here any more...  Could I ask what's
> the reason behind?  I probably have missed something along with the versions,
> I'm just not sure whether e.g. this would potentially block a GUP caller even
> if it's with FOLL_NOWAIT.

This is largely what v2 was about, from the cover letter:

Locking in 3/ is substantially changed to avoid the retry scenario
within the fault handler, therefore a caller who does not allow
retry will no longer receive a SIGBUS on contention.

The discussion thread starts here:

https://lore.kernel.org/kvm/20200501234849.gq26...@ziepe.ca/

Feel free to interject if there's something that doesn't make sense,
the idea is that since we've fixed the lock ordering we never need to
release one lock to wait for another, therefore we can wait for the
lock.  I'm under the impression that we can wait for the lock
regardless of the flags under these conditions.

> Side note: Another thing I thought about when reading this patch - there seems
> to have quite some possibility that the VFIO_DEVICE_PCI_HOT_RESET ioctl will
> start to return -EBUSY now.  Not a problem for this series, but maybe we 
> should
> rememeber to let the userspace handle -EBUSY properly as follow up too, since 
> I
> saw QEMU seemed to not handle -EBUSY for host reset path right now.

I think this has always been the case, whether it's the device lock or
this lock, the only way I know to avoid potential deadlock is to use
the 'try' locking semantics.  In normal scenarios I expect access to
sibling devices is quiesced at this point, so a user driver actually
wanting to achieve a reset shouldn't be affected by this.  Thanks,

Alex



Re: [PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-23 Thread Peter Xu
Hi, Alex,

On Fri, May 22, 2020 at 01:17:43PM -0600, Alex Williamson wrote:
> @@ -1346,15 +1526,32 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault 
> *vmf)
>  {
>   struct vm_area_struct *vma = vmf->vma;
>   struct vfio_pci_device *vdev = vma->vm_private_data;
> + vm_fault_t ret = VM_FAULT_NOPAGE;
> +
> + mutex_lock(>vma_lock);
> + down_read(>memory_lock);

I remembered to have seen the fault() handling FAULT_FLAG_RETRY_NOWAIT at least
in the very first version, but it's not here any more...  Could I ask what's
the reason behind?  I probably have missed something along with the versions,
I'm just not sure whether e.g. this would potentially block a GUP caller even
if it's with FOLL_NOWAIT.

Side note: Another thing I thought about when reading this patch - there seems
to have quite some possibility that the VFIO_DEVICE_PCI_HOT_RESET ioctl will
start to return -EBUSY now.  Not a problem for this series, but maybe we should
rememeber to let the userspace handle -EBUSY properly as follow up too, since I
saw QEMU seemed to not handle -EBUSY for host reset path right now.

Thanks,

-- 
Peter Xu



[PATCH v3 3/3] vfio-pci: Invalidate mmaps and block MMIO access on disabled memory

2020-05-22 Thread Alex Williamson
Accessing the disabled memory space of a PCI device would typically
result in a master abort response on conventional PCI, or an
unsupported request on PCI express.  The user would generally see
these as a -1 response for the read return data and the write would be
silently discarded, possibly with an uncorrected, non-fatal AER error
triggered on the host.  Some systems however take it upon themselves
to bring down the entire system when they see something that might
indicate a loss of data, such as this discarded write to a disabled
memory space.

To avoid this, we want to try to block the user from accessing memory
spaces while they're disabled.  We start with a semaphore around the
memory enable bit, where writers modify the memory enable state and
must be serialized, while readers make use of the memory region and
can access in parallel.  Writers include both direct manipulation via
the command register, as well as any reset path where the internal
mechanics of the reset may both explicitly and implicitly disable
memory access, and manipulation of the MSI-X configuration, where the
MSI-X vector table resides in MMIO space of the device.  Readers
include the read and write file ops to access the vfio device fd
offsets as well as memory mapped access.  In the latter case, we make
use of our new vma list support to zap, or invalidate, those memory
mappings in order to force them to be faulted back in on access.

Our semaphore usage will stall user access to MMIO spaces across
internal operations like reset, but the user might experience new
behavior when trying to access the MMIO space while disabled via the
PCI command register.  Access via read or write while disabled will
return -EIO and access via memory maps will result in a SIGBUS.  This
is expected to be compatible with known use cases and potentially
provides better error handling capabilities than present in the
hardware, while avoiding the more readily accessible and severe
platform error responses that might otherwise occur.

Fixes: CVE-2020-12888
Signed-off-by: Alex Williamson 
---
 drivers/vfio/pci/vfio_pci.c |  291 +++
 drivers/vfio/pci/vfio_pci_config.c  |   36 
 drivers/vfio/pci/vfio_pci_intrs.c   |   14 ++
 drivers/vfio/pci/vfio_pci_private.h |8 +
 drivers/vfio/pci/vfio_pci_rdwr.c|   24 ++-
 5 files changed, 330 insertions(+), 43 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 66a545a01f8f..aabba6439a5b 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "vfio_pci_private.h"
 
@@ -184,6 +185,7 @@ static void vfio_pci_probe_mmaps(struct vfio_pci_device 
*vdev)
 
 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
 static void vfio_pci_disable(struct vfio_pci_device *vdev);
+static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data);
 
 /*
  * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
@@ -736,6 +738,12 @@ int vfio_pci_register_dev_region(struct vfio_pci_device 
*vdev,
return 0;
 }
 
+struct vfio_devices {
+   struct vfio_device **devices;
+   int cur_index;
+   int max_index;
+};
+
 static long vfio_pci_ioctl(void *device_data,
   unsigned int cmd, unsigned long arg)
 {
@@ -809,7 +817,7 @@ static long vfio_pci_ioctl(void *device_data,
{
void __iomem *io;
size_t size;
-   u16 orig_cmd;
+   u16 cmd;
 
info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
info.flags = 0;
@@ -829,10 +837,7 @@ static long vfio_pci_ioctl(void *device_data,
 * Is it really there?  Enable memory decode for
 * implicit access in pci_map_rom().
 */
-   pci_read_config_word(pdev, PCI_COMMAND, _cmd);
-   pci_write_config_word(pdev, PCI_COMMAND,
- orig_cmd | PCI_COMMAND_MEMORY);
-
+   cmd = vfio_pci_memory_lock_and_enable(vdev);
io = pci_map_rom(pdev, );
if (io) {
info.flags = VFIO_REGION_INFO_FLAG_READ;
@@ -840,8 +845,8 @@ static long vfio_pci_ioctl(void *device_data,
} else {
info.size = 0;
}
+   vfio_pci_memory_unlock_and_restore(vdev, cmd);
 
-   pci_write_config_word(pdev, PCI_COMMAND, orig_cmd);
break;
}
case VFIO_PCI_VGA_REGION_INDEX:
@@ -984,8 +989,16 @@ static long vfio_pci_ioctl(void *device_data,
return ret;
 
} else if (cmd == VFIO_DEVICE_RESET) {
-