kvm: Faults which trigger IO release the mmap_sem
Subsystems (4)
PATCH 1/1 kvm: Faults which trigger IO release the mmap_sem
When KVM handles a tdp fault it uses FOLL_NOWAIT. If the guest memory has been swapped out or is behind a filemap, this will trigger async readahead and return immediately. The rationale is that KVM will kick back the guest with an "async page fault" and allow for some other guest process to take over.
If async PFs are enabled the fault is retried asap from a workqueue, or immediately if no async PFs. The retry will not relinquish the mmap semaphore and will block on the IO. This is a bad thing, as other mmap semaphore users now stall. The fault could take a long time, depending on swap or filemap latency.
This patch ensures both the regular and async PF path re-enter the fault allowing for the mmap semaphore to be relinquished in the case of IO wait.
--- include/linux/kvm_host.h | 9 +++++++++ include/linux/mm.h | 1 + mm/gup.c | 4 ++++ virt/kvm/async_pf.c | 4 +--- virt/kvm/kvm_main.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 57 insertions(+), 6 deletions(-)
include/linux/kvm_host.h +9 -0
@@ -198,6 +198,15 @@ int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif +/* +* Retry a fault after a gup with FOLL_NOWAIT. This properly relinquishes mmap +* semaphore if the filemap/swap has to wait on page lock (and retries the gup +* to completion after that). +*/ +int kvm_get_user_page_retry (struct task_struct *tsk,struct mm_struct *mm, +unsigned long addr,bool write_fault, +struct page **pagep); +enum {OUTSIDE_GUEST_MODE ,IN_GUEST_MODE ,
include/linux/mm.h +1 -0
@@ -2011,6 +2011,7 @@ static inline struct page *follow_page(struct vm_area_struct *vma,#define FOLL_HWPOISON 0x100/* check page is hwpoisoned */ #define FOLL_NUMA 0x200/* force NUMA hinting page fault */ #define FOLL_MIGRATION 0x400/* wait for page to replace migration entry */ +#define FOLL_TRIED 0x800/* a retry, previous pass started an IO */ typedef int (*pte_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr,void *data);
mm/gup.c +4 -0
@@ -281,6 +281,10 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_ALLOW_RETRY ;if (*flags &FOLL_NOWAIT ) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT ; +if (*flags &FOLL_TRIED ) { +WARN_ON_ONCE (fault_flags &FAULT_FLAG_ALLOW_RETRY ); + fault_flags |=FAULT_FLAG_TRIED ; + } ret =handle_mm_fault (mm, vma, address, fault_flags);if (ret &VM_FAULT_ERROR ) {
virt/kvm/async_pf.c +1 -3
@@ -80,9 +80,7 @@ static void async_pf_execute(struct work_struct *work)might_sleep (); -down_read (&mm->mmap_sem); -get_user_pages (NULL , mm, addr,1 ,1 ,0 ,NULL ,NULL ); -up_read (&mm->mmap_sem); +kvm_get_user_page_retry (NULL , mm, addr,1 ,NULL );kvm_async_page_present_sync (vcpu, apf);spin_lock (&vcpu->async_pf.lock);
virt/kvm/kvm_main.c +42 -3
57 lines not on this page · raw message
Signature · 7 lines
2.1.0.rc2.206.gedb03e5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerIl 15/09/2014 22:11, Andres Lagar-Cavilla ha scritto:
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1137
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+if (!locked) { +BUG_ON (npages != -EBUSY );
VM_BUG_ON perhaps?
Quoted 5 lines
virt/kvm/kvm_main.c +0
-1@@ -1177,9 +1210,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1220
Quoted 10 lines
virt/kvm/kvm_main.c +8
-2- npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possible async_pf, and we +* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. +*/ + npages =kvm_get_user_page_retry (current, current->mm, addr, + write_fault, page);
This is a separate logical change. Was this:
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);
the intention rather than get_user_pages_fast?
I think a first patch should introduce kvm_get_user_page_retry ("Retry a fault after a gup with FOLL_NOWAIT.") and the second would add FOLL_TRIED ("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").
Apart from this, the patch looks good. The mm/ parts are minimal, so I think it's best to merge it through the KVM tree with someone's Acked-by.
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Paolo Bonzini
Il 15/09/2014 22:11, Andres Lagar-Cavilla ha scritto:
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1137
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+if (!locked) { +BUG_ON (npages != -EBUSY );
VM_BUG_ONperhaps?
Sure.
Quoted 20 lines
Review static int hva_to_pfn_slow(unsigned long addr,
Quoted 1 line
(quoted diff) +0
-0@@ -1177,9 +1210,15 @@ static int hva_to_pfn_slow(unsigned long addr,bool *async, bool
write_fault,virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
Quoted 9 lines
virt/kvm/kvm_main.c +3
-3npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possible async_pf, and we
Review static int hva_to_pfn_slow(unsigned long addr, · line 1216
Quoted 1 line
(quoted diff) +1
-0+ * are certainly not atomic. Time to retry the gup,allowing
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+* mmap semaphore to be relinquished in the case of IO. +*/ + npages = kvm_get_user_page_retry(current, current->mm,addr,
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1220
Quoted 1 line
virt/kvm/kvm_main.c +1
-0+ write_fault, page);
Quoted 8 lines
This is a separate logical change. Was this:
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);the intention rather than
get_user_pages_fast?
Nope. The intention was to pass FAULT_FLAG_RETRY to the vma fault handler (without _NOWAIT). And once you do that, if you come back without holding the mmap sem, you need to call yet again.
By that point in the call chain I felt comfortable dropping the _fast. All paths that get there have already tried _fast (and some have tried _NOWAIT).
Quoted 6 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").
That's not what FOLL_TRIED does. The relinquishing of mmap semaphore is done by this patch minus the FOLL_TRIED bits. FOLL_TRIED will let the fault handler (e.g. filemap) know that we've been there and waited on the IO already, so in the common case we won't need to redo the IO.
Have a look at how FAULT_FLAG_TRIED is used in e.g. arch/x86/mm/fault.c.
Quoted 4 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so I think it's best to merge it through the KVM tree with someone's Acked-by.
Thanks!
Andres
Quoted 3 lines
Paolo
Signature · 2 lines
Andres Lagar-Cavilla | Google Cloud Platform | [email protected] | 647-778-4380
On Tue, Sep 16, 2014 at 9:52 AM, Andres Lagar-Cavilla <[email protected]> wrote:
Apologies to all. Resend as lists rejected my gmail-formatted version. Now on plain text. Won't happen again.
Quoted 81 lines · Paolo Bonzini
Quoted 6 lines
Il 15/09/2014 22:11, Andres Lagar-Cavilla ha scritto:
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1137
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+if (!locked) { +BUG_ON (npages != -EBUSY );
VM_BUG_ONperhaps?Sure.
Quoted 23 lines
Quoted 18 lines
virt/kvm/kvm_main.c +3
-3@@ -1177,9 +1210,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possible async_pf, and we
+ * are certainly not atomic. Time to retry the gup,
allowingvirt/kvm/kvm_main.c +3
-0+* mmap semaphore to be relinquished in the case of IO. +*/ + npages = kvm_get_user_page_retry(current, current->mm,Quoted 2 lines
addr,
+write_fault, page);This is a separate logical change. Was this:
Quoted 5 lines
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);the intention rather than
get_user_pages_fast?Nope. The intention was to pass
FAULT_FLAG_RETRYto the vma fault handler (without_NOWAIT). And once you do that, if you come back without holding the mmap sem, you need to call yet again.By that point in the call chain I felt comfortable dropping the
_fast. All paths that get there have already tried_fast(and some have tried_NOWAIT).Quoted 6 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").That's not what
FOLL_TRIEDdoes. The relinquishing of mmap semaphore is done by this patch minus theFOLL_TRIEDbits.FOLL_TRIEDwill let the fault handler (e.g. filemap) know that we've been there and waited on the IO already, so in the common case we won't need to redo the IO.Have a look at how
FAULT_FLAG_TRIEDis used in e.g.arch/x86/mm/fault.c.Quoted 4 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so I think it's best to merge it through the KVM tree with someone's Acked-by.Thanks!
AndresQuoted 3 lines
Paolo
--
Andres Lagar-Cavilla | Google Cloud Platform | [email protected] | 647-778-4380
Signature · 8 lines
Andres Lagar-Cavilla | Google Cloud Platform | [email protected] | 647-778-4380 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerIl 16/09/2014 18:52, Andres Lagar-Cavilla ha scritto:
Quoted 7 lines
Was this:
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);the intention rather than
get_user_pages_fast?
I meant the intention of the original author, not yours.
Quoted 3 lines
By that point in the call chain I felt comfortable dropping the
_fast. All paths that get there have already tried_fast(and some have tried_NOWAIT).
Yes, understood.
Quoted 10 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").That's not what
FOLL_TRIEDdoes. The relinquishing of mmap semaphore is done by this patch minus theFOLL_TRIEDbits.FOLL_TRIEDwill let the fault handler (e.g. filemap) know that we've been there and waited on the IO already, so in the common case we won't need to redo the IO.
Yes, that's not what FOLL_TRIED does. But it's the difference between get_user_pages and kvm_get_user_page_retry, right?
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Paolo Bonzini
Il 16/09/2014 18:52, Andres Lagar-Cavilla ha scritto:
Quoted 7 lines
Was this:
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);the intention rather than
get_user_pages_fast?I meant the intention of the original author, not yours.
Yes, in all likelihood. I hope!
Quoted 13 lines
Quoted 4 lines
By that point in the call chain I felt comfortable dropping the
_fast. All paths that get there have already tried_fast(and some have tried_NOWAIT).Yes, understood.
Quoted 6 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").
Quoted 7 lines
Quoted 4 lines
That's not what
FOLL_TRIEDdoes. The relinquishing of mmap semaphore is done by this patch minus theFOLL_TRIEDbits.FOLL_TRIEDwill let the fault handler (e.g. filemap) know that we've been there and waited on the IO already, so in the common case we won't need to redo the IO.Yes, that's not what
FOLL_TRIEDdoes. But it's the difference betweenget_user_pagesandkvm_get_user_page_retry, right?
Unfortunately get_user_pages does not expose the param (int *nonblocking) that __gup will use to set FAULT_FLAG_ALLOW_RETRY. So that's one difference. The second difference is that kvm_gup_retry will call two times if necessary (the second without _RETRY but with _TRIED).
Thanks
Andres
Quoted 2 lines
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerIl 16/09/2014 20:42, Andres Lagar-Cavilla ha scritto:
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines
Quoted 10 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").That's not what
FOLL_TRIEDdoes. The relinquishing of mmap semaphore is done by this patch minus theFOLL_TRIEDbits.FOLL_TRIEDwill let the fault handler (e.g. filemap) know that we've been there and waited on the IO already, so in the common case we won't need to redo the IO.
Quoted 8 lines
Quoted 2 lines
Yes, that's not what
FOLL_TRIEDdoes. But it's the difference betweenget_user_pagesandkvm_get_user_page_retry, right?Unfortunately
get_user_pagesdoes not expose the param (int *nonblocking) that__gupwill use to setFAULT_FLAG_ALLOW_RETRY. So that's one difference. The second difference is thatkvm_gup_retrywill call two times if necessary (the second without_RETRYbut with_TRIED).
Yeah, that's how it is in your patch. I can see that.
What I'm saying is that your patch is two changes in one:
1) do not use gup_fast in hva_to_pfn_slow, instead use gup as in async_pf_execute. This change can already introduce a function called kvm_get_user_page_retry, and can already use it in async_pf_execute and hva_to_pfn_slow
2) introduce the two-phase RETRY + TRIED mechanism in kvm_get_user_page_retry, so that the mmap semaphore is relinquished properly if the filemap or swap has to wait on the page lock.
I would prefer to split it in two patches. Is it clearer now?
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 31 lines · Paolo Bonzini
Il 16/09/2014 20:42, Andres Lagar-Cavilla ha scritto:
Quoted 12 lines · Paolo Bonzini
Quoted 11 lines
Quoted 10 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").That's not what
FOLL_TRIEDdoes. The relinquishing of mmap semaphore is done by this patch minus theFOLL_TRIEDbits.FOLL_TRIEDwill let the fault handler (e.g. filemap) know that we've been there and waited on the IO already, so in the common case we won't need to redo the IO.Quoted 8 lines
Quoted 2 lines
Yes, that's not what
FOLL_TRIEDdoes. But it's the difference betweenget_user_pagesandkvm_get_user_page_retry, right?Unfortunately
get_user_pagesdoes not expose the param (int *nonblocking) that__gupwill use to setFAULT_FLAG_ALLOW_RETRY. So that's one difference. The second difference is thatkvm_gup_retrywill call two times if necessary (the second without_RETRYbut with_TRIED).Yeah, that's how it is in your patch. I can see that.
What I'm saying is that your patch is two changes in one:
1) do not use
gup_fastinhva_to_pfn_slow, instead use gup as inasync_pf_execute. This change can already introduce a function calledkvm_get_user_page_retry, and can already use it inasync_pf_executeandhva_to_pfn_slow
Quoted 5 lines
2) introduce the two-phase RETRY + TRIED mechanism in
kvm_get_user_page_retry, so that the mmap semaphore is relinquished properly if the filemap or swap has to wait on the page lock.I would prefer to split it in two patches. Is it clearer now?
Understood. So in patch 1, would kvm_gup_retry be ... just a wrapper around gup? That looks thin to me, and the naming of the function will not be accurate. Plus, considering Radim's suggestion that the naming is not optimal.
I can have patch 1 just s/gup_fast/gup (one liner), and then patch 2 do the rest of the work.
Andres
Quoted 2 lines
Paolo
Signature · 1 line
Andres Lagar-Cavilla | Google Kernel Team | [email protected]
Paolo Bonzini
maintainerIl 17/09/2014 18:58, Andres Lagar-Cavilla ha scritto:
Quoted 3 lines
Understood. So in patch 1, would
kvm_gup_retrybe ... just a wrapper around gup? That looks thin to me, and the naming of the function will not be accurate.
Depends on how you interpret "retry" ("with retry" vs. "retry after _fast"). :)
My point was more to make possible future bisection easier, but I'm not going to insist. I'll queue the patch as soon as I get the required Acked-by.
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Radim Krčmář
Acked-by2014-09-15 13:11-0700, Andres Lagar-Cavilla:
Quoted 1 line
+int kvm_get_user_page_retry(struct
task_struct*tsk, structmm_struct*mm,
The suffix '_retry' is not best suited for this.
On first reading, I imagined we will be retrying something from before, possibly calling it in a loop, but we are actually doing the first and last try in one call.
Hard to find something that conveys our lock-dropping mechanic, '_polite' is my best candidate at the moment.
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1124
Quoted 1 line
virt/kvm/kvm_main.c +1
-0+ int flags = FOLL_TOUCH | FOLL_HWPOISON |
(FOLL_HWPOISON wasn't used before, but it's harmless.)
2014-09-16 15:51+0200, Paolo Bonzini:
Quoted 11 lines
Il 15/09/2014 22:11, Andres Lagar-Cavilla ha scritto:
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1215
Quoted 10 lines
virt/kvm/kvm_main.c +3
-3@@ -1177,9 +1210,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possible async_pf, and we
^
(If we really tried get_user_pages_fast, we wouldn't be here, so I'd
prepend two underscores here as well.)
Quoted 8 lines
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1220
Quoted 5 lines
virt/kvm/kvm_main.c +5
-0+* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. +*/ + npages =kvm_get_user_page_retry (current, current->mm, addr, + write_fault, page);This is a separate logical change. Was this:
Quoted 5 lines
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);the intention rather than
get_user_pages_fast?
I believe so as well.
(Looking at get_user_pages_fast and __get_user_pages_fast made my
abstraction detector very sad.)
Quoted 5 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").
Not sure if that would help to understand the goal ...
Quoted 2 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so I think it's best to merge it through the KVM tree with someone's Acked-by.
I would prefer to have the last hunk in a separate patch, but still,
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Radim Krčmář
2014-09-15 13:11-0700, Andres Lagar-Cavilla:
Quoted 1 line
+int kvm_get_user_page_retry(struct
task_struct*tsk, structmm_struct*mm,The suffix '
_retry' is not best suited for this.
On first reading, I imagined we will be retrying something from before, possibly calling it in a loop, but we are actually doing the first and last try in one call.
We are doing ... the second and third in most scenarios. async_pf did the first with _NOWAIT. We call this from the async pf retrier, or if async pf couldn't be notified to the guest.
Quoted 3 lines
Hard to find something that conveys our lock-dropping mechanic, '
_polite' is my best candidate at the moment.
I'm at a loss towards finding a better name than '_retry'.
Quoted 4 lines
Quoted 2 lines
+ int flags =
FOLL_TOUCH|FOLL_HWPOISON|(
FOLL_HWPOISONwasn't used before, but it's harmless.)
Ok. Wasn't 100% sure TBH.
Quoted 14 lines
2014-09-16 15:51+0200, Paolo Bonzini:
Quoted 11 lines
Il 15/09/2014 22:11, Andres Lagar-Cavilla ha scritto:
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1215
Quoted 10 lines
virt/kvm/kvm_main.c +3
-3@@ -1177,9 +1210,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possible async_pf, and we ^
Quoted 2 lines
(If we really tried
get_user_pages_fast, we wouldn't be here, so I'd
prepend two underscores here as well.)
Yes, async pf tries and fails to do fast, and then we fallback to slow, and so on.
Quoted 13 lines
Quoted 13 lines
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1220
Quoted 6 lines
virt/kvm/kvm_main.c +5
-0+* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. +*/ + npages =kvm_get_user_page_retry (current, current->mm, addr, + write_fault, page);This is a separate logical change. Was this:
down_read(&mm->mmap_sem); npages = get_user_pages(NULL, mm, addr, 1, 1, 0, NULL, NULL); up_read(&mm->mmap_sem);
Quoted 6 lines
Quoted 1 line
the intention rather than
get_user_pages_fast?I believe so as well.
(Looking at
get_user_pages_fastand__get_user_pages_fastmade my
abstraction detector very sad.)
It's clunky, but a separate battle.
Quoted 7 lines
Quoted 6 lines
I think a first patch should introduce
kvm_get_user_page_retry("Retry a fault after a gup withFOLL_NOWAIT.") and the second would addFOLL_TRIED("This properly relinquishes mmap semaphore if the filemap/swap has to wait on page lock (and retries the gup to completion after that").
Quoted 8 lines
Not sure if that would help to understand the goal ...
Quoted 2 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so I think it's best to merge it through the KVM tree with someone's Acked-by.I would prefer to have the last hunk in a separate patch, but still,
Acked-by Radim Krčmář <[email protected]>
Awesome, thanks much.
I'll recut with the VM_BUG_ON from Paolo and your Ack. LMK if anything else from this email should go into the recut.
Andres
Signature · 7 lines
Andres Lagar-Cavilla | Google Kernel Team | [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Radim Krčmář
Reviewed-by[Emergency posting to fix the tag and couldn't find unmangled Cc list,
so some recipients were dropped, sorry. (I guess you are glad though).]
2014-09-16 14:01-0700, Andres Lagar-Cavilla:
Quoted 11 lines · Radim Krčmář
Quoted 9 lines
2014-09-15 13:11-0700, Andres Lagar-Cavilla:
Quoted 2 lines
+int kvm_get_user_page_retry(struct
task_struct*tsk, structmm_struct*mm,The suffix '
_retry' is not best suited for this.
On first reading, I imagined we will be retrying something from before,
possibly calling it in a loop, but we are actually doing the first and last try in one call.
Quoted 3 lines
We are doing ... the second and third in most scenarios.
async_pfdid the first with_NOWAIT. We call this from the async pf retrier, or if async pf couldn't be notified to the guest.
I was thinking more about what the function does, not how we currently use it -- nothing prevents us from using it as first somewhere -- but yeah, even comments would be off then.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so I
think it's best to merge it through the KVM tree with someone's Acked-by.
Quoted 8 lines
Quoted 3 lines
I would prefer to have the last hunk in a separate patch, but still,
Acked-by Radim Krčmář <[email protected]>Awesome, thanks much.
I'll recut with the
VM_BUG_ONfrom Paolo and your Ack. LMK if anything else from this email should go into the recut.
Ah, sorry, I'm not maintaining mm ... what I meant was
and I had to leave before I could find a good apology for VM_WARN_ON_ONCE(), so if you are replacing BUG_ON, you might want to look at that one as well.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 15 lines · Radim Krčmář
[Emergency posting to fix the tag and couldn't find unmangled Cc list,
so some recipients were dropped, sorry. (I guess you are glad though).]2014-09-16 14:01-0700, Andres Lagar-Cavilla:
Quoted 11 lines · Radim Krčmář
Quoted 9 lines
2014-09-15 13:11-0700, Andres Lagar-Cavilla:
Quoted 2 lines
+int kvm_get_user_page_retry(struct
task_struct*tsk, structmm_struct*mm,The suffix '
_retry' is not best suited for this.
On first reading, I imagined we will be retrying something from before,
possibly calling it in a loop, but we are actually doing the first and last try in one call.
Quoted 8 lines
Quoted 3 lines
We are doing ... the second and third in most scenarios.
async_pfdid the first with_NOWAIT. We call this from the async pf retrier, or if async pf couldn't be notified to the guest.I was thinking more about what the function does, not how we currently use it -- nothing prevents us from using it as first somewhere -- but yeah, even comments would be off then.
Good point. Happy to expand comments. What about _complete? _io? _full?
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so I
think it's best to merge it through the KVM tree with someone's Acked-by.I would prefer to have the last hunk in a separate patch, but still,
Acked-by Radim Krčmář <[email protected]>
Quoted 8 lines
Quoted 4 lines
Awesome, thanks much.
I'll recut with the
VM_BUG_ONfrom Paolo and your Ack. LMK if anything else from this email should go into the recut.Ah, sorry, I'm not maintaining mm ... what I meant was
Reviewed-by Radim Krčmář <[email protected]>
Cool cool cool
Andres
Quoted 4 lines
and I had to leave before I could find a good apology for
VM_WARN_ON_ONCE(), so if you are replacingBUG_ON, you might want to look at that one as well.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Radim Krčmář
Reviewed-by[Repost for lists, the last mail was eaten by a security troll.]
2014-09-16 14:01-0700, Andres Lagar-Cavilla:
Quoted 13 lines
Quoted 10 lines · Radim Krčmář
2014-09-15 13:11-0700, Andres Lagar-Cavilla:
Quoted 2 lines
+int kvm_get_user_page_retry(struct
task_struct*tsk, structmm_struct*mm,The suffix '
_retry' is not best suited for this.
On first reading, I imagined we will be retrying something from before,
possibly calling it in a loop, but we are actually doing the first and
last try in one call.
Quoted 3 lines
We are doing ... the second and third in most scenarios.
async_pfdid the first with_NOWAIT. We call this from the async pf retrier, or if async pf couldn't be notified to the guest.
I was thinking more about what the function does, not how we currently use it -- nothing prevents us from using it as first somewhere -- but yeah, even comments would be off then.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Apart from this, the patch looks good. The
mm/parts are minimal, so
I
think it's best to merge it through the KVM tree with someone's Acked-by.
Quoted 8 lines
Quoted 3 lines
I would prefer to have the last hunk in a separate patch, but still,
Acked-by Radim Krčmář <[email protected]>Awesome, thanks much.
I'll recut with the
VM_BUG_ONfrom Paolo and your Ack. LMK if anything else from this email should go into the recut.
Ah, sorry, I'm not maintaining mm ... what I meant was
and I had to leave before I could find a good apology for VM_WARN_ON_ONCE(), so if you are replacing BUG_ON, you might want to look at that one as well.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 111 lines · Andres Lagar-Cavilla
When KVM handles a tdp fault it uses
FOLL_NOWAIT. If the guest memory has been swapped out or is behind a filemap, this will trigger async readahead and return immediately. The rationale is that KVM will kick back the guest with an "async page fault" and allow for some other guest process to take over.If async PFs are enabled the fault is retried asap from a workqueue, or immediately if no async PFs. The retry will not relinquish the mmap semaphore and will block on the IO. This is a bad thing, as other mmap semaphore users now stall. The fault could take a long time, depending on swap or filemap latency.
This patch ensures both the regular and async PF path re-enter the fault allowing for the mmap semaphore to be relinquished in the case of IO wait.
Signed-off-by Andres Lagar-Cavilla <[email protected]>--- include/linux/kvm_host.h | 9 +++++++++ include/linux/mm.h | 1 + mm/gup.c | 4 ++++ virt/kvm/async_pf.c | 4 +--- virt/kvm/kvm_main.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 57 insertions(+), 6 deletions(-)include/linux/kvm_host.h +9
-0@@ -198,6 +198,15 @@ int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif +/* +* Retry a fault after a gup with FOLL_NOWAIT. This properly relinquishes mmap +* semaphore if the filemap/swap has to wait on page lock (and retries the gup +* to completion after that). +*/ +int kvm_get_user_page_retry (struct task_struct *tsk,struct mm_struct *mm, +unsigned long addr,bool write_fault, +struct page **pagep); +enum {OUTSIDE_GUEST_MODE ,IN_GUEST_MODE ,include/linux/mm.h +1
-0@@ -2011,6 +2011,7 @@ static inline struct page *follow_page(struct vm_area_struct *vma,#define FOLL_HWPOISON 0x100/* check page is hwpoisoned */ #define FOLL_NUMA 0x200/* force NUMA hinting page fault */ #define FOLL_MIGRATION 0x400/* wait for page to replace migration entry */ +#define FOLL_TRIED 0x800/* a retry, previous pass started an IO */ typedef int (*pte_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr,void *data);mm/gup.c +4
-0@@ -281,6 +281,10 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_ALLOW_RETRY ;if (*flags &FOLL_NOWAIT ) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT ; +if (*flags &FOLL_TRIED ) { +WARN_ON_ONCE (fault_flags &FAULT_FLAG_ALLOW_RETRY ); + fault_flags |=FAULT_FLAG_TRIED ; + } ret =handle_mm_fault (mm, vma, address, fault_flags);if (ret &VM_FAULT_ERROR ) {virt/kvm/async_pf.c +1
-3@@ -80,9 +80,7 @@ static void async_pf_execute(struct work_struct *work)might_sleep (); -down_read (&mm->mmap_sem); -get_user_pages (NULL , mm, addr,1 ,1 ,0 ,NULL ,NULL ); -up_read (&mm->mmap_sem); +kvm_get_user_page_retry (NULL , mm, addr,1 ,NULL );kvm_async_page_present_sync (vcpu, apf);spin_lock (&vcpu->async_pf.lock);virt/kvm/kvm_main.c +18
-0@@ -1115,6 +1115,39 @@ static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,return __get_user_pages (tsk, mm, start,1 , flags, page,NULL ,NULL ); } +int kvm_get_user_page_retry (struct task_struct *tsk,struct mm_struct *mm, +unsigned long addr,bool write_fault, +struct page **pagep) +{ +int npages; +int locked =1 ; +int flags =FOLL_TOUCH |FOLL_HWPOISON | + (pagep ?FOLL_GET :0 ) | + (write_fault ?FOLL_WRITE :0 ); + +/* +* Retrying fault, we get here *not* having allowed the filemap to wait +* on the page lock. We should now allow waiting on the IO with the +* mmap semaphore released. +*/ +down_read (&mm->mmap_sem); + npages =__get_user_pages (tsk, mm, addr,1 , flags, pagep,NULL , + &locked);
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1145
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+if (!locked) { +BUG_ON (npages != -EBUSY ); +/* +* The previous call has now waited on the IO. Now we can +* retry and complete. Pass TRIED to ensure we do not re +* schedule async IO (see e.g. filemap_fault). +*/ +down_read (&mm->mmap_sem); + npages =__get_user_pages (tsk, mm, addr,1 , flags |FOLL_TRIED , + pagep,NULL ,NULL );
For async_pf_execute() you do not need to even retry. Next guest's page fault will retry it for you.
Quoted 35 lines
virt/kvm/kvm_main.c +14
-3+ } +up_read (&mm->mmap_sem); +return npages; +} +static inline int check_user_page_hwpoison (unsigned long addr) {int rc, flags =FOLL_TOUCH |FOLL_HWPOISON |FOLL_WRITE ;@@ -1177,9 +1210,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possible async_pf, and we +* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. +*/ + npages =kvm_get_user_page_retry (current, current->mm, addr, + write_fault, page); + }if (npages !=1 )return npages;Signature · 7 lines
2.1.0.rc2.206.gedb03e5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.
Wouldn't that be a waste of vmentries?
The guest might be able to handle interrupts while we are waiting, so if we used async-io-done notifier, this could work without CPU spinning. (Probably with added latency.)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 5 lines · Radim Krčmář
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.Wouldn't that be a waste of vmentries?
This is how it will work with or without this second gup. Page is not mapped into a shadow page table on this path, it happens on a next fault.
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 8 lines · Gleb Natapov
Quoted 5 lines · Radim Krčmář
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.Wouldn't that be a waste of vmentries?
This is how it will work with or without this second gup. Page is not mapped into a shadow page table on this path, it happens on a next fault.
The point is that the gup in the async pf completion from the work queue will not relinquish the mmap semaphore. And it most definitely should, given that we are likely looking at swap/filemap.
Andres
Quoted 3 lines
--
Gleb.
Signature · 7 lines
Andres Lagar-Cavilla | Google Kernel Team | [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Andres Lagar-Cavilla
Quoted 5 lines · Gleb Natapov
Quoted 4 lines · Radim Krčmář
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Wouldn't that be a waste of vmentries?
This is how it will work with or without this second gup. Page is not mapped into a shadow page table on this path, it happens on a next fault.
The point is that the gup in the async pf completion from the work queue will not relinquish the mmap semaphore. And it most definitely should, given that we are likely looking at swap/filemap.
I get this point and the patch looks good in general, but my point is that when _retry() is called from async_pf_execute() second gup is not needed. In the original code gup is called to do IO and nothing else. In your patch this is accomplished by the first gup already, so you can skip second gup if pagep == nullptr.
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 15 lines · Gleb Natapov
Quoted 6 lines · Andres Lagar-Cavilla
Quoted 5 lines · Gleb Natapov
Quoted 4 lines · Radim Krčmář
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Wouldn't that be a waste of vmentries?
This is how it will work with or without this second gup. Page is not mapped into a shadow page table on this path, it happens on a next fault.
The point is that the gup in the async pf completion from the work queue will not relinquish the mmap semaphore. And it most definitely should, given that we are likely looking at swap/filemap.
Quoted 5 lines
I get this point and the patch looks good in general, but my point is that when
_retry()is called fromasync_pf_execute()second gup is not needed. In the original code gup is called to do IO and nothing else. In your patch this is accomplished by the first gup already, so you can skip second gup if pagep == nullptr.
I see. However, if this function were to be used elsewhere in the future, then the "if pagep == NULL don't retry" semantics may not match the new caller's intention. Would you prefer an explicit flag?
Andres
Quoted 3 lines
--
Gleb.
Signature · 7 lines
Andres Lagar-Cavilla | Google Kernel Team | [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 22 lines · Andres Lagar-Cavilla
Quoted 15 lines · Gleb Natapov
Quoted 6 lines · Andres Lagar-Cavilla
Quoted 5 lines · Gleb Natapov
Quoted 4 lines · Radim Krčmář
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Wouldn't that be a waste of vmentries?
This is how it will work with or without this second gup. Page is not mapped into a shadow page table on this path, it happens on a next fault.
The point is that the gup in the async pf completion from the work queue will not relinquish the mmap semaphore. And it most definitely should, given that we are likely looking at swap/filemap.
Quoted 5 lines
I get this point and the patch looks good in general, but my point is that when
_retry()is called fromasync_pf_execute()second gup is not needed. In the original code gup is called to do IO and nothing else. In your patch this is accomplished by the first gup already, so you can skip second gup if pagep == nullptr.
Quoted 4 lines
I see. However, if this function were to be used elsewhere in the future, then the "if pagep == NULL don't retry" semantics may not match the new caller's intention. Would you prefer an explicit flag?
We can add explicit flag whenever such caller will be added, if ever.
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 23 lines · Gleb Natapov
Quoted 22 lines · Andres Lagar-Cavilla
Quoted 20 lines · Gleb Natapov
Quoted 6 lines · Andres Lagar-Cavilla
Quoted 5 lines · Gleb Natapov
Quoted 4 lines · Radim Krčmář
2014-09-17 13:26+0300, Gleb Natapov:
Quoted 2 lines
For
async_pf_execute()you do not need to even retry. Next guest's page fault will retry it for you.Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Wouldn't that be a waste of vmentries?
This is how it will work with or without this second gup. Page is not mapped into a shadow page table on this path, it happens on a next fault.
The point is that the gup in the async pf completion from the work queue will not relinquish the mmap semaphore. And it most definitely should, given that we are likely looking at swap/filemap.
I get this point and the patch looks good in general, but my point is that when
_retry()is called fromasync_pf_execute()second gup is not needed. In the original code gup is called to do IO and nothing else. In your patch this is accomplished by the first gup already, so you can skip second gup if pagep == nullptr.
Quoted 5 lines
Quoted 4 lines
I see. However, if this function were to be used elsewhere in the future, then the "if pagep == NULL don't retry" semantics may not match the new caller's intention. Would you prefer an explicit flag?
We can add explicit flag whenever such caller will be added, if ever.
Ok. Patch forthcoming.
Paolo, I'm not sure the split will buy anything, because the first patch will be a one-liner (no point in the new kvm_gup_something function if the impl won't match the intended semantics of the function). But if you push back, I'll cut a v3.
Thanks all,
Andres
Quoted 3 lines
--
Gleb.
Signature · 7 lines
Andres Lagar-Cavilla | Google Kernel Team | [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 1/1 kvm: Faults which trigger IO release the mmap_sem
In reply to Andres Lagar-CavillaWhen KVM handles a tdp fault it uses FOLL_NOWAIT. If the guest memory has been swapped out or is behind a filemap, this will trigger async readahead and return immediately. The rationale is that KVM will kick back the guest with an "async page fault" and allow for some other guest process to take over.
If async PFs are enabled the fault is retried asap from an async workqueue. If not, it's retried immediately in the same code path. In either case the retry will not relinquish the mmap semaphore and will block on the IO. This is a bad thing, as other mmap semaphore users now stall as a function of swap or filemap latency.
This patch ensures both the regular and async PF path re-enter the fault allowing for the mmap semaphore to be relinquished in the case of IO wait.
---
v1 -> v2
* WARN_ON_ONCE -> VM_WARN_ON_ONCE
* pagep == NULL skips the final retry
* kvm_gup_retry -> kvm_gup_io
* Comment updates throughout
--- include/linux/kvm_host.h | 11 +++++++++++ include/linux/mm.h | 1 + mm/gup.c | 4 ++++ virt/kvm/async_pf.c | 4 +--- virt/kvm/kvm_main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 63 insertions(+), 6 deletions(-)
include/linux/kvm_host.h +11 -0
@@ -198,6 +198,17 @@ int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif +/* +* Carry out a gup that requires IO. Allow the mm to relinquish the mmap +* semaphore if the filemap/swap has to wait on a page lock. pagep == NULL +* controls whether we retry the gup one more time to completion in that case. +* Typically this is called after a FAULT_FLAG_RETRY_NOWAIT in the main tdp +* handler. +*/ +int kvm_get_user_page_io (struct task_struct *tsk,struct mm_struct *mm, +unsigned long addr,bool write_fault, +struct page **pagep); +enum {OUTSIDE_GUEST_MODE ,IN_GUEST_MODE ,
include/linux/mm.h +1 -0
@@ -2011,6 +2011,7 @@ static inline struct page *follow_page(struct vm_area_struct *vma,#define FOLL_HWPOISON 0x100/* check page is hwpoisoned */ #define FOLL_NUMA 0x200/* force NUMA hinting page fault */ #define FOLL_MIGRATION 0x400/* wait for page to replace migration entry */ +#define FOLL_TRIED 0x800/* a retry, previous pass started an IO */ typedef int (*pte_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr,void *data);
mm/gup.c +4 -0
@@ -281,6 +281,10 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_ALLOW_RETRY ;if (*flags &FOLL_NOWAIT ) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT ; +if (*flags &FOLL_TRIED ) { +VM_WARN_ON_ONCE (fault_flags &FAULT_FLAG_ALLOW_RETRY ); + fault_flags |=FAULT_FLAG_TRIED ; + } ret =handle_mm_fault (mm, vma, address, fault_flags);if (ret &VM_FAULT_ERROR ) {
virt/kvm/async_pf.c +1 -3
@@ -80,9 +80,7 @@ static void async_pf_execute(struct work_struct *work)might_sleep (); -down_read (&mm->mmap_sem); -get_user_pages (NULL , mm, addr,1 ,1 ,0 ,NULL ,NULL ); -up_read (&mm->mmap_sem); +kvm_get_user_page_io (NULL , mm, addr,1 ,NULL );kvm_async_page_present_sync (vcpu, apf);spin_lock (&vcpu->async_pf.lock);
virt/kvm/kvm_main.c +46 -3
61 lines not on this page · raw message
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Hi Andres,
On Wed, Sep 17, 2014 at 10:51:48AM -0700, Andres Lagar-Cavilla wrote:
[...]
Quoted 9 lines
static inline int
check_user_page_hwpoison(unsigned long addr){ int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;virt/kvm/kvm_main.c +0
-2@@ -1177,9 +1214,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages = get_user_pages_fast(addr, 1, write_fault,
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1225
Quoted 10 lines
virt/kvm/kvm_main.c +9
-1- page); + }else { +/* +* By now we have tried gup_fast, and possibly async_pf, and we +* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. +*/ + npages =kvm_get_user_page_io (current, current->mm, addr, + write_fault, page); + }
try_async_pf
gfn_to_pfn_async
__gfn_to_pfn async = false
__gfn_to_pfn_memslot
hva_to_pfn
hva_to_pfn_fast
hva_to_pfn_slow
kvm_get_user_page_io
page will always be ready after kvm_get_user_page_io which leads to APF don't need to work any more.
Regards,
Wanpeng Li
Quoted 11 lines
virt/kvm/kvm_main.c +0
-0if (npages !=1 )return npages;Signature · 7 lines
2.1.0.rc2.206.gedb03e5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 18 lines · Wanpeng Li
Hi Andres,
On Wed, Sep 17, 2014 at 10:51:48AM -0700, Andres Lagar-Cavilla wrote:
[...]Quoted 15 lines
static inline int
check_user_page_hwpoison(unsigned long addr){ int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;virt/kvm/kvm_main.c +5
-3@@ -1177,9 +1214,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possibly async_pf, and we +* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO.
Quoted 8 lines
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1225
Quoted 4 lines
virt/kvm/kvm_main.c +4
-0+*/ + npages =kvm_get_user_page_io (current, current->mm, addr, + write_fault, page); + }
try_async_pf
gfn_to_pfn_async
__gfn_to_pfnasync = false
*async = false
Quoted 4 lines
__gfn_to_pfn_memslot
hva_to_pfn
hva_to_pfn_fast
hva_to_pfn_slow
hva_to_pfn_slow checks async not *async.
Quoted 19 lines
kvm_get_user_page_iopage will always be ready after
kvm_get_user_page_iowhich leads to APF don't need to work any more.Regards,
Wanpeng LiQuoted 11 lines
virt/kvm/kvm_main.c +0
-0if (npages !=1 )return npages;Signature · 7 lines
2.1.0.rc2.206.gedb03e5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Wanpeng Li
Reviewed-byQuoted 27 lines · Gleb Natapov
Quoted 18 lines · Wanpeng Li
Hi Andres,
On Wed, Sep 17, 2014 at 10:51:48AM -0700, Andres Lagar-Cavilla wrote:
[...]Quoted 15 lines
static inline int
check_user_page_hwpoison(unsigned long addr){ int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;virt/kvm/kvm_main.c +5
-3@@ -1177,9 +1214,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possibly async_pf, and we +* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. Quoted 8 lines
virt/kvm/kvm_main.c static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, · line 1225
Quoted 4 lines
virt/kvm/kvm_main.c +4
-0+*/ + npages =kvm_get_user_page_io (current, current->mm, addr, + write_fault, page); + }
try_async_pf
gfn_to_pfn_async
__gfn_to_pfnasync = false
Quoted 7 lines
*async = false
Quoted 4 lines
__gfn_to_pfn_memslot
hva_to_pfn
hva_to_pfn_fast
hva_to_pfn_slow
hva_to_pfn_slowchecks async not *async.
Got it, thanks for your pointing out.
Regards,
Wanpeng Li
Quoted 15 lines
Quoted 15 lines
kvm_get_user_page_iopage will always be ready after
kvm_get_user_page_iowhich leads to APF don't need to work any more.Regards,
Wanpeng LiQuoted 6 lines
virt/kvm/kvm_main.c +0
-0if (npages !=1 )return npages;Signature · 1 line
2.1.0.rc2.206.gedb03e5
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 8 lines · Wanpeng Li
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Wanpeng Li
Hi Andres,
On Wed, Sep 17, 2014 at 10:51:48AM -0700, Andres Lagar-Cavilla wrote:
[...]Quoted 3 lines
static inline int
check_user_page_hwpoison(unsigned long addr){ int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
Quoted 7 lines
Got it, thanks for your pointing out.
Reviewed-by Wanpeng Li <[email protected]>Regards,
Wanpeng Li
Thanks.
Paolo, should I recut including the recent Reviewed-by's?
Thanks
Andres
ps: shrunk cc
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
kvm_get_user_page_iopage will always be ready after
kvm_get_user_page_iowhich leads to APF don't need to work any more.Regards,
Wanpeng LiQuoted 6 lines
if (npages != 1) return npages;--
2.1.0.rc2.206.gedb03e5
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerIl 19/09/2014 05:58, Andres Lagar-Cavilla ha scritto:
Quoted 1 line
Paolo, should I recut including the recent Reviewed-by's?
No, I'll add them myself.
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 5 lines · Paolo Bonzini
Il 19/09/2014 05:58, Andres Lagar-Cavilla ha scritto:
Quoted 1 line
Paolo, should I recut including the recent Reviewed-by's?
No, I'll add them myself.
Paolo, is this patch waiting for something? Is Gleb's Reviewed-by enough?
Thanks much
Andres
Quoted 3 lines
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerIl 22/09/2014 22:49, Andres Lagar-Cavilla ha scritto:
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Paolo, should I recut including the recent Reviewed-by's?
No, I'll add them myself.
Paolo, is this patch waiting for something? Is Gleb's Reviewed-by enough?
It's waiting for an Acked-by on the mm/ changes.
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 8 lines · On Mon, 22 Sep 2014 23:32:36 +0200 Paolo Bonzini <[email protected]> wrote:
Il 22/09/2014 22:49, Andres Lagar-Cavilla ha scritto:
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Paolo, should I recut including the recent Reviewed-by's?
No, I'll add them myself.
Paolo, is this patch waiting for something? Is Gleb's Reviewed-by enough?
It's waiting for an Acked-by on the
mm/changes.
The MM changes look good to me.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 12 lines · Andres Lagar-Cavilla
When KVM handles a tdp fault it uses
FOLL_NOWAIT. If the guest memory has been swapped out or is behind a filemap, this will trigger async readahead and return immediately. The rationale is that KVM will kick back the guest with an "async page fault" and allow for some other guest process to take over.If async PFs are enabled the fault is retried asap from an async workqueue. If not, it's retried immediately in the same code path. In either case the retry will not relinquish the mmap semaphore and will block on the IO. This is a bad thing, as other mmap semaphore users now stall as a function of swap or filemap latency.
Quoted 4 lines
This patch ensures both the regular and async PF path re-enter the fault allowing for the mmap semaphore to be relinquished in the case of IO wait.
Quoted 152 lines
Reviewed-by Radim Krčmář <[email protected]>Signed-off-by Andres Lagar-Cavilla <[email protected]>---
v1 -> v2*
WARN_ON_ONCE->VM_WARN_ON_ONCE
* pagep == NULL skips the final retry
*kvm_gup_retry->kvm_gup_io
* Comment updates throughout--- include/linux/kvm_host.h | 11 +++++++++++ include/linux/mm.h | 1 + mm/gup.c | 4 ++++ virt/kvm/async_pf.c | 4 +--- virt/kvm/kvm_main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 63 insertions(+), 6 deletions(-)include/linux/kvm_host.h +11
-0@@ -198,6 +198,17 @@ int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif +/* +* Carry out a gup that requires IO. Allow the mm to relinquish the mmap +* semaphore if the filemap/swap has to wait on a page lock. pagep == NULL +* controls whether we retry the gup one more time to completion in that case. +* Typically this is called after a FAULT_FLAG_RETRY_NOWAIT in the main tdp +* handler. +*/ +int kvm_get_user_page_io (struct task_struct *tsk,struct mm_struct *mm, +unsigned long addr,bool write_fault, +struct page **pagep); +enum {OUTSIDE_GUEST_MODE ,IN_GUEST_MODE ,include/linux/mm.h +1
-0@@ -2011,6 +2011,7 @@ static inline struct page *follow_page(struct vm_area_struct *vma,#define FOLL_HWPOISON 0x100/* check page is hwpoisoned */ #define FOLL_NUMA 0x200/* force NUMA hinting page fault */ #define FOLL_MIGRATION 0x400/* wait for page to replace migration entry */ +#define FOLL_TRIED 0x800/* a retry, previous pass started an IO */ typedef int (*pte_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr,void *data);mm/gup.c +4
-0@@ -281,6 +281,10 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_ALLOW_RETRY ;if (*flags &FOLL_NOWAIT ) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT ; +if (*flags &FOLL_TRIED ) { +VM_WARN_ON_ONCE (fault_flags &FAULT_FLAG_ALLOW_RETRY ); + fault_flags |=FAULT_FLAG_TRIED ; + } ret =handle_mm_fault (mm, vma, address, fault_flags);if (ret &VM_FAULT_ERROR ) {virt/kvm/async_pf.c +1
-3@@ -80,9 +80,7 @@ static void async_pf_execute(struct work_struct *work)might_sleep (); -down_read (&mm->mmap_sem); -get_user_pages (NULL , mm, addr,1 ,1 ,0 ,NULL ,NULL ); -up_read (&mm->mmap_sem); +kvm_get_user_page_io (NULL , mm, addr,1 ,NULL );kvm_async_page_present_sync (vcpu, apf);spin_lock (&vcpu->async_pf.lock);virt/kvm/kvm_main.c +46
-3@@ -1115,6 +1115,43 @@ static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,return __get_user_pages (tsk, mm, start,1 , flags, page,NULL ,NULL ); } +int kvm_get_user_page_io (struct task_struct *tsk,struct mm_struct *mm, +unsigned long addr,bool write_fault, +struct page **pagep) +{ +int npages; +int locked =1 ; +int flags =FOLL_TOUCH |FOLL_HWPOISON | + (pagep ?FOLL_GET :0 ) | + (write_fault ?FOLL_WRITE :0 ); + +/* +* If retrying the fault, we get here *not* having allowed the filemap +* to wait on the page lock. We should now allow waiting on the IO with +* the mmap semaphore released. +*/ +down_read (&mm->mmap_sem); + npages =__get_user_pages (tsk, mm, addr,1 , flags, pagep,NULL , + &locked); +if (!locked) { +VM_BUG_ON (npages != -EBUSY ); + +if (!pagep) +return 0 ; + +/* +* The previous call has now waited on the IO. Now we can +* retry and complete. Pass TRIED to ensure we do not re +* schedule async IO (see e.g. filemap_fault). +*/ +down_read (&mm->mmap_sem); + npages =__get_user_pages (tsk, mm, addr,1 , flags |FOLL_TRIED , + pagep,NULL ,NULL ); + } +up_read (&mm->mmap_sem); +return npages; +} +static inline int check_user_page_hwpoison (unsigned long addr) {int rc, flags =FOLL_TOUCH |FOLL_HWPOISON |FOLL_WRITE ;@@ -1177,9 +1214,15 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, npages =get_user_page_nowait (current, current->mm, addr, write_fault, page);up_read (¤t->mm->mmap_sem); - }else - npages =get_user_pages_fast (addr,1 , write_fault, - page); + }else { +/* +* By now we have tried gup_fast, and possibly async_pf, and we +* are certainly not atomic. Time to retry the gup, allowing +* mmap semaphore to be relinquished in the case of IO. +*/ + npages =kvm_get_user_page_io (current, current->mm, addr, + write_fault, page); + }if (npages !=1 )return npages;Signature · 1 line
2.1.0.rc2.206.gedb03e5
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hi Andres,
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1138
Quoted 3 lines · Andres Lagar-Cavilla
virt/kvm/kvm_main.c +3
-0+if (!locked) { +VM_BUG_ON (npages != -EBUSY ); +
Shouldn't this be VM_BUG_ON(npages)?
Alternatively we could patch gup to do:
case -EHWPOISON:
+ case -EBUSY:
return i ? i : ret;
- case -EBUSY:
- return i;
I need to fix gup_fast slow path to start with FAULT_FLAG_ALLOW_RETRY similarly to what you did to the KVM slow path.
gup_fast is called without the mmap_sem (incidentally its whole point is to only disable irqs and not take the locks) so the enabling of FAULT_FLAG_ALLOW_RETRY initial pass inside gup_fast should be all self contained. It shouldn't concern KVM which should be already fine with your patch, but it will allow the userfaultfd to intercept all O_DIRECT gup_fast in addition to KVM with your patch.
Eventually get_user_pages should be obsoleted in favor of get_user_pages_locked (or whoever we decide to call it) so the userfaultfd can intercept all kind of gups. gup_locked is same as gup except for one more "locked" parameter at the end, I called the parameter locked instead of nonblocking because it'd be more proper to call "nonblocking" gup the FOLL_NOWAIT kind which is quite the opposite (in fact as the mmap_sem cannot be dropped in the non blocking version).
ptrace ironically is better off sticking with a NULL locked parameter and to get a sigbus instead of risking hanging on the userfaultfd (which would be safe as it can be killed, but it'd be annoying if erroneously poking into a hole during a gdb session). It's still possible to pass NULL as parameter to get_user_pages_locked to achieve that. So the fact some callers won't block in handle_userfault because FAULT_FLAG_ALLOW_RETRY is not set and the userfault cannot block, may come handy.
What I'm trying to solve in this context is that the userfault cannot safely block without FAULT_FLAG_ALLOW_RETRY because we can't allow userland to take the mmap_sem for an unlimited amount of time without requiring special privileges, so if handle_userfault wants to blocks within a gup invocation, it must first release the mmap_sem hence FAULT_FLAG_ALLOW_RETRY is always required at the first attempt for any virtual address.
With regard to the last sentence, there's actually a race with MADV_DONTNEED too, I'd need to change the code to always pass FAULT_FLAG_ALLOW_RETRY (your code also would need to loop and insisting with the __get_user_pages(locked) version to solve it). The KVM ioctl worst case would get an -EFAULT if the race materializes for example. It's non concerning though because that can be solved in userland somehow by separating ballooning and live migration activities.
Thanks,
Andrea
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 8 lines · Andrea Arcangeli
Hi Andres,
virt/kvm/kvm_main.c static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm, · line 1138
Quoted 3 lines · Andres Lagar-Cavilla
virt/kvm/kvm_main.c +3
-0+if (!locked) { +VM_BUG_ON (npages != -EBUSY ); +Shouldn't this be
VM_BUG_ON(npages)?
Oh shoot you're right. I was confused by the introduction of -EBUSY in the forward port.
if (ret & VM_FAULT_RETRY) {
if (nonblocking)
*nonblocking = 0;
return -EBUSY;
}(gaaah!!!)
Review
Quoted 9 lines
Alternatively we could patch gup to do:
case
-EHWPOISON:(quoted diff) +1
-2+ case -EBUSY: return i ? i : ret; - case -EBUSY: - return i;
No preference. Not a lot of semantics available given that we pass 1 as the count to gup. Want to cut the patch or I can just shoot one right away?
Quoted 10 lines
I need to fix
gup_fastslow path to start withFAULT_FLAG_ALLOW_RETRYsimilarly to what you did to the KVM slow path.
gup_fastis called without themmap_sem(incidentally its whole point is to only disable irqs and not take the locks) so the enabling ofFAULT_FLAG_ALLOW_RETRYinitial pass insidegup_fastshould be all self contained. It shouldn't concern KVM which should be already fine with your patch, but it will allow the userfaultfd to intercept allO_DIRECTgup_fastin addition to KVM with your patch.
Quoted 9 lines
Eventually
get_user_pagesshould be obsoleted in favor ofget_user_pages_locked(or whoever we decide to call it) so the userfaultfd can intercept all kind of gups.gup_lockedis same as gup except for one more "locked" parameter at the end, I called the parameter locked instead of nonblocking because it'd be more proper to call "nonblocking" gup theFOLL_NOWAITkind which is quite the opposite (in fact as themmap_semcannot be dropped in the non blocking version).
It's nearly impossible to name it right because 1) it indicates we can relinquish 2) it returns whether we still hold the mmap semaphore.
I'd prefer it'd be called mmap_sem_hold, which conveys immediately what this is about ("nonblocking" or "locked" could be about a whole lot of things)
Quoted 9 lines
ptrace ironically is better off sticking with a NULL locked parameter and to get a sigbus instead of risking hanging on the userfaultfd (which would be safe as it can be killed, but it'd be annoying if erroneously poking into a hole during a gdb session). It's still possible to pass NULL as parameter to
get_user_pages_lockedto achieve that. So the fact some callers won't block inhandle_userfaultbecauseFAULT_FLAG_ALLOW_RETRYis not set and the userfault cannot block, may come handy.
Quoted 7 lines
What I'm trying to solve in this context is that the userfault cannot safely block without
FAULT_FLAG_ALLOW_RETRYbecause we can't allow userland to take themmap_semfor an unlimited amount of time without requiring special privileges, so ifhandle_userfaultwants to blocks within a gup invocation, it must first release themmap_semhenceFAULT_FLAG_ALLOW_RETRYis always required at the first attempt for any virtual address.
I can see that. My background for coming into this is very similar: in a previous life we had a file system shim that would kick up into userspace for servicing VM memory. KVM just wouldn't let the file system give up the mmap semaphore. We had /proc readers hanging up all over the place while userspace was servicing. Not happy.
With KVM (now) and the standard x86 fault giving you ALLOW_RETRY, what stands in your way? Methinks that gup_fast has no slowpath fallback that turns on ALLOW_RETRY. What would oppose that being the global behavior?
Quoted 9 lines
With regard to the last sentence, there's actually a race with
MADV_DONTNEEDtoo, I'd need to change the code to always passFAULT_FLAG_ALLOW_RETRY(your code also would need to loop and insisting with the__get_user_pages(locked)version to solve it). The KVM ioctl worst case would get an-EFAULTif the race materializes for example. It's non concerning though because that can be solved in userland somehow by separating ballooning and live migration activities.
Well, IIUC every code path that has ALLOW_RETRY dives in the second time with FAULT_TRIED or similar. In the common case, you happily blaze through the second time, but if someone raced in while all locks were given up, one pays the price of the second time being a full fault hogging the mmap sem. At some point you need to not keep being polite otherwise the task starves. Presumably the risk of an extra retry drops steeply every new gup retry. Maybe just try three times is good enough. It makes for ugly logic though.
Thanks
Andres
Quoted 3 lines
Thanks,
Andrea
Signature · 7 lines
Andres Lagar-Cavilla | Google Kernel Team | [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>