Fix performance problems with mprotect()
Subsystems (3)
PATCH 1/1 Fix performance problems with mprotect()
While working on a database scaling problem using a SAP database test suite we discovered that these enterprise level large shared memory databases are very heavy users of mprotect(), to the extent that the performance overhead in current Xenolinux impacts scaling beyond a few cpus quite badly. A single cpu run under Xen has a nominal impact, but scaling out to 8 cpus results in a performance of less than 10% of native throughput. Ouch!
The problem was isolated to the relatively high overhead of how mprotect() updates ptes, and the lack of efficient batching opportunities as presently implemented. By adding a hypercall which batches pte updates such that hardware flags updates (accessed or dirty bits) are not lost, and redoing the performance critical portion of mprotect() to take advantage of that, performance has improved to where it is essentially equivalent to native performance with this SAP database test suite. I haven't tested other database implementations, but I would imagine they would have similar results.
This is obviously quite an improvement and should allow Xen to be deployed for more enterprise level workloads.
There are actually 2 new hypercalls included in this patch - one that batches flags-only changes to pte's within a single page table, and another which allows the full pte to be changed - both "atomically" as described above.
I imagine there are other places in Linux that could benefit from these hypercalls, but these patches focus on the mprotect performance problem only.
The hypervisor patch was mostly written and tested with the 3.0.4 era hypervisor that ships with SLES 10, and then ported and tested on current xen-unstable. The Linux patch was written and tested on 2.6.16 and made apply to the 2.6.18 tree without further testing.
Feedback and comments are appreciated.
- Bruce Rogers
xen/include/public/xen.h +31 -0
@@ -168,9 +168,40 @@* ptr[:2] -- Machine address within the frame whose mapping to modify. * The frame must belong to the FD, if one is specified. * val -- Value to write into the mapping entry. +* +* ptr[1:0] == MMU_ATOMIC_PT_UPDATE: +* Updates a range of entries in an L1 page table atomically such that any +* bits which the hardware mmu could have modified are not lost. The source of +* the updates is a range of values specified by val. If the new table entry is +* valid/present, the mapped frame must belong to the FD, if an FD has been +* specified. If attempting to map an I/O page then the caller assumes the +* privilege of the FD. +* FD == DOMID_IO: Permit /only/ I/O mappings, at the priv level of the caller. +* FD == DOMID_XEN: Map restricted areas of Xen's heap space. +* ptr[:2] -- Machine address of the page-table entry to modify. +* val -- Value to write. +* +* ptr[1:0] == MMU_FLAG_RANGE_UPDATE: +* Updates a range of entries in an L1 page table atomically such that any +* bits which the hardware mmu could have modified are not lost. The update +* value created by updating certain buts in the the current page table entry, +* as specified by val. If the new table entry is valid/present, the mapped +* frame must belong to the FD, if an FD has been specified. If attempting to +* map an I/O page then the caller assumes the privilege of the FD. +* FD == DOMID_IO: Permit /only/ I/O mappings, at the priv level of the caller. +* FD == DOMID_XEN: Map restricted areas of Xen's heap space. +* ptr[:2] -- Machine address of the first page-table entry to modify. +* val[11:0] = mask pte bits 11:0 (1 means update; 0 means don't change) +* val[23:12] = mask pte bits 63:52 +* val[35:24] = new pte bits 11:0 (where corresp. Mask bit == 1) +* val[47:36] = new pte bits 63:52 +* val[57:48] = number of ptes in range +* val[63:58] = MBZ */ #define MMU_NORMAL_PT_UPDATE 0/* checked '*ptr = val'. ptr is MA. */ #define MMU_MACHPHYS_UPDATE 1/* ptr = MA of frame to modify entry for */ +#define MMU_ATOMIC_PT_UPDATE 2/* checked atomic '*ptr = val'. ptr is MA.*/ +#define MMU_FLAG_RANGE_UPDATE 3/* range atomic checked '*ptr flags = val'*/ /* * MMU EXTENDED OPERATIONS
xen/arch/x86/mm.c +305 -1
328 lines not on this page · raw message
xen/include/xsm/xsm.h +12 -0
@@ -136,6 +136,8 @@ struct xsm_operations {int (*mmu_machphys_update) (struct domain *d,unsigned long mfn);int (*update_va_mapping) (struct domain *d,l1_pgentry_t pte);int (*add_to_physmap) (struct domain *d1,struct domain *d2); +int (*mmu_atomic_update) (struct domain *d,intpte_t fpte); +int (*mmu_flag_range_update) (struct domain *d,intpte_t fpte);#endif };
@@ -532,6 +534,16 @@ static inline int xsm_add_to_physmap(str {return xsm_call (add_to_physmap (d1, d2)); } + +static inline int xsm_mmu_atomic_update (struct domain *d,intpte_t fpte) +{ +return xsm_call (mmu_atomic_update (d, fpte)); +} + +static inline int xsm_mmu_flag_range_update (struct domain *d,intpte_t fpte) +{ +return xsm_call (mmu_flag_range_update (d, fpte)); +}#endif /* CONFIG_X86 */ #endif /* __XSM_H */
xen/xsm/dummy.c +12 -0
@@ -385,6 +385,16 @@ static int dummy_add_to_physmap (struct {return 0 ; } + +static int dummy_mmu_atomic_update (struct domain *d,intpte_t fpte) +{ +return 0 ; +} + +static int dummy_mmu_flag_range_update (struct domain *d,intpte_t fpte) +{ +return 0 ; +}#endif struct xsm_operations dummy_xsm_ops;
@@ -484,5 +494,7 @@ void xsm_fixup_ops (struct xsm_operationset_to_dummy_if_null (ops, mmu_machphys_update);set_to_dummy_if_null (ops, update_va_mapping);set_to_dummy_if_null (ops, add_to_physmap); +set_to_dummy_if_null (ops, mmu_atomic_update); +set_to_dummy_if_null (ops, mmu_flag_range_update);#endif }
include/xen/interface/xen.h +31 -0
@@ -168,9 +168,40 @@* ptr[:2] -- Machine address within the frame whose mapping to modify. * The frame must belong to the FD, if one is specified. * val -- Value to write into the mapping entry. +* +* ptr[1:0] == MMU_ATOMIC_PT_UPDATE: +* Updates a range of entries in an L1 page table atomically such that any +* bits which the hardware mmu could have modified are not lost. The source of +* the updates is a range of values specified by val. If the new table entry is +* valid/present, the mapped frame must belong to the FD, if an FD has been +* specified. If attempting to map an I/O page then the caller assumes the +* privilege of the FD. +* FD == DOMID_IO: Permit /only/ I/O mappings, at the priv level of the caller. +* FD == DOMID_XEN: Map restricted areas of Xen's heap space. +* ptr[:2] -- Machine address of the page-table entry to modify. +* val -- Value to write. +* +* ptr[1:0] == MMU_FLAG_RANGE_UPDATE: +* Updates a range of entries in an L1 page table atomically such that any +* bits which the hardware mmu could have modified are not lost. The update +* value created by updating certain buts in the the current page table entry, +* as specified by val. If the new table entry is valid/present, the mapped +* frame must belong to the FD, if an FD has been specified. If attempting to +* map an I/O page then the caller assumes the privilege of the FD. +* FD == DOMID_IO: Permit /only/ I/O mappings, at the priv level of the caller. +* FD == DOMID_XEN: Map restricted areas of Xen's heap space. +* ptr[:2] -- Machine address of the first page-table entry to modify. +* val[11:0] = mask pte bits 11:0 (1 means update; 0 means don't change) +* val[23:12] = mask pte bits 63:52 +* val[35:24] = new pte bits 11:0 (where corresp. Mask bit == 1) +* val[47:36] = new pte bits 63:52 +* val[57:48] = number of ptes in range +* val[63:58] = MBZ */ #define MMU_NORMAL_PT_UPDATE 0/* checked '*ptr = val'. ptr is MA. */ #define MMU_MACHPHYS_UPDATE 1/* ptr = MA of frame to modify entry for */ +#define MMU_ATOMIC_PT_UPDATE 2/* checked atomic '*ptr = val'. ptr is MA.*/ +#define MMU_FLAG_RANGE_UPDATE 3/* range atomic checked '*ptr flags = val'*/ /* * MMU EXTENDED OPERATIONS
include/asm-i386/mach-xen/asm/hypervisor.h +5 -0
@@ -120,6 +120,11 @@ int xen_create_contiguous_region( unsigned long vstart, unsigned int order, unsigned int address_bits);void xen_destroy_contiguous_region (unsigned long vstart,unsigned int order); + +struct mm_struct ; + +int xen_change_pte_range (struct mm_struct *mm,pmd_t *pmd, +unsigned long addr,unsigned long end,pgprot_t newprot);struct page ;
arch/i386/mm/hypervisor.c +74 -0
77 lines not on this page · raw message
mm/mprotect.c +13 -0
@@ -76,7 +76,20 @@ static inline void change_pmd_range(stru next =pmd_addr_end (addr, end);if (pmd_none_or_clear_bad (pmd))continue ; +#if CONFIG_XEN + { +int rc; + rc =xen_change_pte_range (mm, pmd, addr, next, newprot); +if (rc) + { +if (rc != -ENOSYS ) +BUG (); +change_pte_range (mm, pmd, addr, next, newprot); + } + } +#else change_pte_range (mm, pmd, addr, next, newprot); +#endif }while (pmd++, addr = next, addr != end); }
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 1 line · Bruce Rogers
While working on a database scaling problem
The changelog entry for this patch should have a suitable entry for
http://wiki.xensource.com/xenwiki/APIChangelog, as suggested by Ian Jackson.
(Regarding the changes, is there a sane way to detect whether the new API is present or not? IE a sensible unique errno return or something else.)
regards
john
Thanks, I'll need a little while to look into this patch. It's fairly obviously not a candidate for 3.2.0 at this point anyway (merge window is long gone by).
-- Keir
Quoted 41 lines · Bruce Rogers
While working on a database scaling problem using a SAP database test suite we discovered that these enterprise level large shared memory databases are very heavy users of
mprotect(), to the extent that the performance overhead in current Xenolinux impacts scaling beyond a few cpus quite badly. A single cpu run under Xen has a nominal impact, but scaling out to 8 cpus results in a performance of less than 10% of native throughput. Ouch!The problem was isolated to the relatively high overhead of how
mprotect()updates ptes, and the lack of efficient batching opportunities as presently implemented. By adding a hypercall which batches pte updates such that hardware flags updates (accessed or dirty bits) are not lost, and redoing the performance critical portion ofmprotect()to take advantage of that, performance has improved to where it is essentially equivalent to native performance with this SAP database test suite. I haven't tested other database implementations, but I would imagine they would have similar results.This is obviously quite an improvement and should allow Xen to be deployed for more enterprise level workloads.
There are actually 2 new hypercalls included in this patch - one that batches flags-only changes to pte's within a single page table, and another which allows the full pte to be changed - both "atomically" as described above.
I imagine there are other places in Linux that could benefit from these hypercalls, but these patches focus on the mprotect performance problem only.
The hypervisor patch was mostly written and tested with the 3.0.4 era hypervisor that ships with SLES 10, and then ported and tested on current xen-unstable.
The Linux patch was written and tested on 2.6.16 and made apply to the 2.6.18 tree without further testing.Feedback and comments are appreciated.
Signed-off-by Bruce Rogers <[email protected]>- Bruce Rogers
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
On the Xen patch:
Both added case blocks in do_mmu_update() have a nested switch statement that seems redundant (i.e. the outer switch already only handle PGT_l1_page_table pages, but the inner switch check this same value again.
The same two case block access addresses obtained from map_domain_page_with_cache() through __copy_from_user(). Is there any particular reason to not use direct accesses here? (Note that mod_l1_entry() has to use __copy_from_user() as it may be called from do_update_va_mapping()). Likewise I would think that there's no strict need for update_intpte_sync() to use paging_cmpxchg_guest_entry(), but here I would agree that it's easier to re-use the existing function than to create and use a new one.
An additional piece of concern regarding the bit assignments of MMU_FLAG_RANGE_UPDATE's val parameter (Keir, maybe you need to comment on this one): The whole mmu_update interface, being defined in public/xen.h, is supposed to be sufficiently architecture neutral, which it won't be anymore in the way it currently is being modified. But maybe I'm mistaken and the interface's declaration is just badly placed (would apply to the mmuext interface then, too)?
In the Linux patch, I'd just like to see the abstraction to be less Xen- specific, i.e. something like
(perhaps in include/asm-generic/pgtable.h)
#ifndef arch_change_pte_range
# define arch_change_pte_range(mm, pmd, addr, end, newprot) 0 #endif
and then in change_pmd_range():
...
if (pmd_none_or_clear_bad(pmd))
continue;
if (arch_change_pte_range(mm, pmd, addr, next, newprot))
continue;
change_pte_range(mm, pmd, addr, next, newprot);
} while (pmd++, addr = next, addr != end);
...
The BUG() (which really can be BUG_ON() here) would go into the actual function then.
Jan
xen/arch/x86/mm.c static void free_l4_table(struct page_in · line 1363
Quoted 3 lines · Bruce Rogers
xen/arch/x86/mm.c +3
-0+ +/* Allowed to change in Accessed/Dirty flags only. */ +BUG_ON ((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));
Are you sure that BUG_ON can't be triggered from the hypercall? It should error out in this case I think.
-Andi
I'm not sure how this BUG_ON would get triggered with current x86 paging semantics, and probably isn't needed I think. Am I missing something?
The code is almost exactly the same as the existing update_intpte, which has the same BUG_ON, and so I just left it in. (BTW:I considered merging the new functions into the non "sync" versions but opted not to.)
- Bruce
Quoted 1 line
Quoted 1 line
Quoted 1 line
On 1/7/2008 at 8:18 AM, in message <[email protected]>, Andi Kleen
Quoted 10 lines · <[email protected]> wrote:
xen/arch/x86/mm.c static void free_l4_table(struct page_in · line 1363
Quoted 3 lines · Bruce Rogers
xen/arch/x86/mm.c +3
-0+ +/* Allowed to change in Accessed/Dirty flags only. */ +BUG_ON ((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));Are you sure that
BUG_ONcan't be triggered from the hypercall? It should error out in this case I think.-Andi
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
On 1/7/2008 at 8:03 AM, in message <[email protected]>, "Jan
Quoted 6 lines · Beulich
On the Xen patch:
Both added case blocks in
do_mmu_update()have a nested switch statement that seems redundant (i.e. the outer switch already only handlePGT_l1_page_tablepages, but the inner switch check this same value again.
Right - a silly oversight - will fix.
Quoted 9 lines
The same two case block access addresses obtained from
map_domain_page_with_cache()through__copy_from_user(). Is there any particular reason to not use direct accesses here? (Note thatmod_l1_entry()has to use__copy_from_user()as it may be called fromdo_update_va_mapping()). Likewise I would think that there's no strict need forupdate_intpte_sync()to usepaging_cmpxchg_guest_entry(), but here I would agree that it's easier to re-use the existing function than to create and use a new one.
I'll change to use direct access.
Quoted 8 lines
An additional piece of concern regarding the bit assignments of MMU_FLAG_RANGE_UPDATE's val parameter (Keir, maybe you need to comment on this one): The whole
mmu_updateinterface, being defined inpublic/xen.h, is supposed to be sufficiently architecture neutral, which it won't be anymore in the way it currently is being modified. But maybe I'm mistaken and the interface's declaration is just badly placed (would apply to the mmuext interface then, too)?
I should point out that the MMU_FLAG_RANGE_UPDATE hypercall didn't end up being needed for improving the SAP test, but does get invoked by other uses of mprotect() and feel that it is useful. As it stands I've only implemented x86 version of this, other architecture owners would need to do the same. Perhaps as far as what is presented in public/xen.h, we should just have a more generic description (not pointing out the format) and leave it up to each architecture as to what specific format for the val member makes sense.
Quoted 21 lines
In the Linux patch, I'd just like to see the abstraction to be less Xen- specific, i.e. something like
(perhaps in
include/asm-generic/pgtable.h)#ifndef
arch_change_pte_range# define arch_change_pte_range(mm, pmd, addr, end, newprot) 0 #endifand then in
change_pmd_range():...
mm/mprotect.c +0
-0if (pmd_none_or_clear_bad (pmd))continue ;if (arch_change_pte_range (mm, pmd, addr, next, newprot))continue ;change_pte_range (mm, pmd, addr, next, newprot); }while (pmd++, addr = next, addr != end);
Quoted 2 lines
The
BUG()(which really can beBUG_ON()here) would go into the actual function then.
Yes, that approach does look better. I'll respin the patch.
Quoted 8 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
The BUG_ON() is valid until the per-domain biglock is removed.
-- Keir
Quoted 29 lines · Bruce Rogers
I'm not sure how this
BUG_ONwould get triggered with current x86 paging semantics, and probably isn't needed I think.
Am I missing something?
The code is almost exactly the same as the existingupdate_intpte, which has the sameBUG_ON, and so I just left it in.
(BTW:I considered merging the new functions into the non "sync" versions but opted not to.)- Bruce
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On 1/7/2008 at 8:18 AM, in message <[email protected]>, Andi Kleen
Quoted 10 lines · <[email protected]> wrote:
xen/arch/x86/mm.c static void free_l4_table(struct page_in · line 1363
Quoted 3 lines · Bruce Rogers
xen/arch/x86/mm.c +3
-0+ +/* Allowed to change in Accessed/Dirty flags only. */ +BUG_ON ((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));Are you sure that
BUG_ONcan't be triggered from the hypercall? It should error out in this case I think.-Andi
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 7 lines · Bruce Rogers
Quoted 7 lines
An additional piece of concern regarding the bit assignments of MMU_FLAG_RANGE_UPDATE's val parameter (Keir, maybe you need to comment on this one): The whole
mmu_updateinterface, being defined inpublic/xen.h, is supposed to be sufficiently architecture neutral, which it won't be anymore in the way it currently is being modified. But maybe I'm mistaken and the interface's declaration is just badly placed (would apply to the mmuext interface then, too)?
Quoted 7 lines
I should point out that the
MMU_FLAG_RANGE_UPDATEhypercall didn't end up being needed for improving the SAP test, but does get invoked by other uses ofmprotect()and feel that it is useful. As it stands I've only implemented x86 version of this, other architecture owners would need to do the same. Perhaps as far as what is presented inpublic/xen.h, we should just have a more generic description (not pointing out the format) and leave it up to each architecture as to what specific format for the val member makes sense.
No other architecture uses do_mmu_update(). The public definitions are simply in the wrong header file.
Is MMU_FLAG_RANGE_UPDATE really useful if you have MMU_ATOMIC_PT_UPDATE? If it is it's presumably a performance issue, and the question should be: why is a sequence MMU_ATOMIC_PT_UPDATE slower than MMU_FLAG_RANGE_UPDATE, and can we make it fast enough that MMU_FLAG_RANGE_UPDATE is redundant?
Also, the documentation for MMU_ATOMIC_PT_UDATE in xen.h is possibly incorrect. It doesn't make sense to me -- are your meanings for ptr[2:] and val actually correct?
-- Keir
Quoted 1 line
Quoted 1 line
Quoted 1 line
On 1/7/2008 at 10:45 AM, in message <C3A815C0.11D69%[email protected]>,
Quoted 20 lines · Keir Fraser
Quoted 9 lines · Bruce Rogers
Quoted 7 lines
An additional piece of concern regarding the bit assignments of MMU_FLAG_RANGE_UPDATE's val parameter (Keir, maybe you need to comment on this one): The whole
mmu_updateinterface, being defined inpublic/xen.h, is supposed to be sufficiently architecture neutral, which it won't be anymore in the way it currently is being modified. But maybe I'm mistaken and the interface's declaration is just badly placed (would apply to the mmuext interface then, too)?I should point out that the
MMU_FLAG_RANGE_UPDATEhypercall didn't end up being needed for improving the SAP test, but does get invoked by other usesof
Quoted 1 line
mprotect()and feel that it is useful. As it stands I've only implementedx86
Quoted 3 lines
version of this, other architecture owners would need to do the same. Perhaps as far as what is presented in
public/xen.h, we should just have a more generic description (not pointing out the format) and leave it up toeach
Quoted 1 line
architecture as to what specific format for the val member makes sense.
Quoted 7 lines
No other architecture uses
do_mmu_update(). The public definitions are simply in the wrong header file.Is
MMU_FLAG_RANGE_UPDATEreally useful if you haveMMU_ATOMIC_PT_UPDATE? If it is it's presumably a performance issue, and the question should be: why is a sequenceMMU_ATOMIC_PT_UPDATEslower thanMMU_FLAG_RANGE_UPDATE, and can we make it fast enough thatMMU_FLAG_RANGE_UPDATEis redundant?
I did find a quite measurable performance difference between using the two methods, but I consider it useful mainly because of its compactness in terms of being able to update all ptes in a page table without needing a large array to initialize from. That said, I am not beholden to this flag range hypercall, since the other one is more generic and we should be able to make it fast enough. If you prefer, we can just go with the MMU_ATOMIC_PT_UPDATE hypercall only.
Quoted 4 lines
Also, the documentation for
MMU_ATOMIC_PT_UDATEinxen.his possibly incorrect. It doesn't make sense to me -- are your meanings for ptr[2:] and val actually correct?
Oh,you are correct. I had originally coded MMU_ATOMIC_PT_UDATE as it is described, since doing it that way was giving me about 14% better performance than doing the batching one mmu_update_t structure at a time, but I again opted for backing off to the more general case and forgot to update that description for that hypercall. I'll get that fixed.
Quoted 2 lines
-- Keir
I don't see how. If you are seeing a problem, please elaborate.
- Bruce
Quoted 1 line
Quoted 1 line
Quoted 1 line
On 1/7/2008 at 8:18 AM, in message <[email protected]>, Andi Kleen
Quoted 15 lines · <[email protected]> wrote:
xen/arch/x86/mm.c static void free_l4_table(struct page_in · line 1363
Quoted 3 lines · Bruce Rogers
xen/arch/x86/mm.c +3
-0+ +/* Allowed to change in Accessed/Dirty flags only. */ +BUG_ON ((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));Are you sure that
BUG_ONcan't be triggered from the hypercall? It should error out in this case I think.-Andi
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel