x86/altp2m: cleanup p2m_altp2m_lazy_copy
PATCH v2 1/1 x86/altp2m: cleanup p2m_altp2m_lazy_copy
The p2m_altp2m_lazy_copy is responsible for lazily populating an altp2m view when the guest traps out due to no EPT entry being present in the active view. Currently the function took several inputs that it didn't use and also locked/unlocked gfns when it didn't need to.
---
v2: return bool; return early if hp2m entry is invalid; use gprintk and use
more useful debug message
--- xen/arch/x86/hvm/hvm.c | 7 +++-- xen/arch/x86/mm/p2m.c | 61 +++++++++++++++++++-------------------- xen/include/asm-x86/p2m.h | 5 ++-- 3 files changed, 37 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/hvm.c +5 -2
@@ -1688,6 +1688,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,int sharing_enomem =0 ;vm_event_request_t *req_ptr =NULL ;bool_t ap2m_active, sync =0 ; +unsigned int page_order;/* On Nested Virtualization, walk the guest page table. * If this succeeds, all is fine.
@@ -1754,11 +1755,13 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, hostp2m =p2m_get_hostp2m (currd); mfn =get_gfn_type_access (hostp2m, gfn, &p2mt, &p2ma,P2M_ALLOC | (npfec.write_access ?P2M_UNSHARE :0 ), -NULL ); + &page_order);if ( ap2m_active ) { -if (p2m_altp2m_lazy_copy (curr, gpa, gla, npfec, &p2m) ) + p2m =p2m_get_altp2m (curr); + +if (p2m_altp2m_lazy_copy (p2m, gfn, mfn, p2mt, p2ma, page_order) ) {/* entry was lazily copied from host -- retry */ __put_gfn (hostp2m, gfn);
xen/arch/x86/mm/p2m.c +29 -32
86 lines not on this page · raw message
xen/include/asm-x86/p2m.h +3 -2
@@ -867,8 +867,9 @@ void p2m_altp2m_check(struct vcpu *v, uint16_t idx);void p2m_flush_altp2m (struct domain *d);/* Alternate p2m paging */ -bool_t p2m_altp2m_lazy_copy (struct vcpu *v,paddr_t gpa, -unsigned long gla,struct npfec npfec,struct p2m_domain **ap2m); +bool p2m_altp2m_lazy_copy (struct p2m_domain *ap2m,unsigned long gfn_l, +mfn_t hmfn,p2m_type_t hp2mt,p2m_access_t hp2ma, +unsigned int page_order);/* Make a specific alternate p2m valid */ int p2m_init_altp2m_by_id (struct domain *d,unsigned int idx);
Signature · 7 lines
2.20.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 6 lines · Tamas K Lengyel
The
p2m_altp2m_lazy_copyis responsible for lazily populating an altp2m view when the guest traps out due to no EPT entry being present in the active view. Currently the function took several inputs that it didn't use and also locked/unlocked gfns when it didn't need to.Signed-off-by Tamas K Lengyel <[email protected]>
Sorry - this fell off my radar. I've got a separate series which partially overlaps with this.
If George is happy with both, I'll see about taking this and rebasing/splicing my fixes around it on commit.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 4 lines · Tamas K Lengyel
The
p2m_altp2m_lazy_copyis responsible for lazily populating an altp2m view when the guest traps out due to no EPT entry being present in the active view. Currently the function took several inputs that it didn't use and also locked/unlocked gfns when it didn't need to.
Wow, the code you're cleaning up was really all over the place. Thanks for this.
The code in your patch looks correct; but while you've gotten rid of the redundant host p2m lookup, there's still a redundant altp2m lookup. Is there any reason not to take it to its logical conclusion, like the attached patch?
NB this is compile-tested only; definitely double-check it for logic errors.
-George
From 41c89a229c8cfbf2f747a56482b8cc805158af81 Mon Sep 17 00:00:00 2001
From: Tamas K Lengyel <[email protected]>
Date: Fri, 12 Apr 2019 14:08:13 -0600
Subject: [PATCH] x86/altp2m: cleanup p2m_altp2m_lazy_copy
The p2m_altp2m_lazy_copy is responsible for lazily populating an altp2m view when the guest traps out due to no EPT entry being present in the active view. Currently, in addition to taking a number of unused argements, the whole calling convention has a number of redundant p2m lookups: the function reads the hostp2m, even though the caller has just read the same hostp2m entry; and then the caller re-reads the altp2m entry that the function has just read (and possibly set).
Rework this function to make it a bit more rational. Specifically:
- Pass the current hostp2m entry values we have just read for it to use to populate the altp2m entry if it finds the entry empty.
- If the altp2m entry is not empty, pass out the values we've read so the caller doesn't need to re-walk the tables
- Either way, return with the gfn 'locked', to make clean-up handling more consistent.
Rename the function to better reflect this functionality.
While we're here, change bool_t to bool, and return true/false rather than 1/0.
It's a bit grating to do both the p2m_lock() and the get_gfn(), knowing that they boil down to the same thing at the moment; but we have to maintain the fiction until such time as we decide to get rid of it entirely.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Roger Pau Monne <[email protected]>
Cc: George Dunlap <[email protected]>
--- xen/arch/x86/hvm/hvm.c | 19 +++++--- xen/arch/x86/mm/p2m.c | 95 +++++++++++++++++++++------------------ xen/include/asm-x86/p2m.h | 5 ++- 3 files changed, 67 insertions(+), 52 deletions(-)
xen/arch/x86/hvm/hvm.c +12 -7
@@ -1691,6 +1691,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,int sharing_enomem =0 ;vm_event_request_t *req_ptr =NULL ;bool_t ap2m_active, sync =0 ; +unsigned int page_order;/* On Nested Virtualization, walk the guest page table. * If this succeeds, all is fine.
@@ -1757,19 +1758,23 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, hostp2m =p2m_get_hostp2m (currd); mfn =get_gfn_type_access (hostp2m, gfn, &p2mt, &p2ma,P2M_ALLOC | (npfec.write_access ?P2M_UNSHARE :0 ), -NULL ); + &page_order);if ( ap2m_active ) { -if (p2m_altp2m_lazy_copy (curr, gpa, gla, npfec, &p2m) ) + p2m =p2m_get_altp2m (curr); + +/* +* Get the altp2m entry if present; or if not, propagate from +* the host p2m. NB that this returns with gfn locked in the +* altp2m. +*/ +if (p2m_altp2m_get_or_propagate (p2m, gfn, &mfn, &p2mt, &p2ma, page_order) ) { -/* entry was lazily copied from host -- retry */ -__put_gfn (hostp2m, gfn); +/* Entry was copied from host -- retry fault */ rc =1 ; - goto out; + goto out_put_gfn; } - - mfn =get_gfn_type_access (p2m, gfn, &p2mt, &p2ma,0 ,NULL ); }else p2m = hostp2m;
xen/arch/x86/mm/p2m.c +52 -43
117 lines not on this page · raw message
xen/include/asm-x86/p2m.h +3 -2
@@ -879,8 +879,9 @@ void p2m_altp2m_check(struct vcpu *v, uint16_t idx);void p2m_flush_altp2m (struct domain *d);/* Alternate p2m paging */ -bool_t p2m_altp2m_lazy_copy (struct vcpu *v,paddr_t gpa, -unsigned long gla,struct npfec npfec,struct p2m_domain **ap2m); +bool p2m_altp2m_get_or_propagate (struct p2m_domain *ap2m,unsigned long gfn_l, +mfn_t *mfn,p2m_type_t *p2mt, +p2m_access_t *p2ma,unsigned int page_order);/* Make a specific alternate p2m valid */ int p2m_init_altp2m_by_id (struct domain *d,unsigned int idx);
Signature · 1 line
2.20.1
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 7 lines · George Dunlap
Quoted 4 lines · Tamas K Lengyel
The
p2m_altp2m_lazy_copyis responsible for lazily populating an altp2m view when the guest traps out due to no EPT entry being present in the active view. Currently the function took several inputs that it didn't use and also locked/unlocked gfns when it didn't need to.
Quoted 7 lines
Wow, the code you're cleaning up was really all over the place. Thanks for this.
The code in your patch looks correct; but while you've gotten rid of the redundant host p2m lookup, there's still a redundant altp2m lookup. Is there any reason not to take it to its logical conclusion, like the attached patch?
Looks good to me.
Quoted 2 lines
NB this is compile-tested only; definitely double-check it for logic errors.
I did a live test and everything works fine.
Thanks,
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel