open-coded page list manipulation in shadow code
Tim,
in the course of adding >16Tb support to the hypervisor, I ran into issues with you having added several open-coded page list accesses while breaking up non-order-0 allocations there. I looked at that code for quite a while, but wasn't really able to figure how to properly convert these. In particular I'm struggling with the list terminations (would using NULL for PAGE_LIST_NULL be correct when using ordinary struct list_head-s for struct page_info's list member?) and the implications of this comment
/* Page lists don't have pointers back to the head structure, so
* it's safe to use a head structure on the stack to link the pages
* together. */
in shadow_alloc(), but then again I also didn't look very closely at the other places that also fail to compile.
To help myself test the new code, I invented a shadow stub which provides just enough functionality to build and not crash with all the other shadow code compiled out. I wonder whether such a build mode might actually be useful for other purposes, and hence whether it might be worthwhile extracting that from the patch into a standalone one.
Thanks, Jan
Quoted 10 lines · Jan Beulich
Tim,
in the course of adding >16Tb support to the hypervisor, I ran into issues with you having added several open-coded page list accesses while breaking up non-order-0 allocations there. I looked at that code for quite a while, but wasn't really able to figure how to properly convert these. In particular I'm struggling with the list terminations (would using NULL for
PAGE_LIST_NULLbe correct when using ordinary struct list_head-s for struct page_info's list member?)
Yes, I think so. The fragmentary lists would terminate in NULL in both directions.
Quoted 5 lines
and the implications of this comment
/* Page lists don't have pointers back to the head structure, so
* it's safe to use a head structure on the stack to link the pages
* together. */
Yep; we'd have to make sure that we explicitly NULL'ed out the first page's prev and the last page's next. This all seems a bit unsatisfactory though.
Maybe it would be better to piggy-back these partial lists into the hash-table's linked list (i.e. the 'next_shadow' field) instead of the pinned list. That list is much more active, though.
Let me have a think about it on Thursday (assuming I don't have too many patch series to review by then!)
Quoted 9 lines
in
shadow_alloc(), but then again I also didn't look very closely at the other places that also fail to compile.To help myself test the new code, I invented a shadow stub which provides just enough functionality to build and not crash with all the other shadow code compiled out. I wonder whether such a build mode might actually be useful for other purposes, and hence whether it might be worthwhile extracting that from the patch into a standalone one.
If it's not too much work, yes please. There are already use cases where people don't need shadow pagetable support, and once PVH is stable that will be more common. Having the option to compile out 7kloc of twisty MMU code seems good. :)
Tim.
Hi,
Quoted 2 lines · Tim Deegan
Let me have a think about it on Thursday (assuming I don't have too many patch series to review by then!)
OK, here's what I have. It keeps the mechanism the same, threading on the main page list entry, but tidies it up to use the page_list operations rather than open-coding. I've tested it (lightly - basically jsut boot testing) with 32bit, 32pae and 64bit Windows VMs (all SMP), with bigmem=y and bigmem=n.
It was developed on top of your bigmem series, but it should apply OK before patch 3/4, removing the need to nobble shadow-paging in that patch.
Cheers,
Tim.
Quoted 1 line
From
57d211e47ef42012791252b4897231b392b7aff7Mon Sep 17 00:00:00 2001
From: Tim Deegan <[email protected]>
Date: Thu, 29 Jan 2015 17:09:58 +0000
Subject: [PATCH] x86/shadow: Tidy up fragmentary page lists in multi-page
shadows.
Multi-page shadows are linked together using the 'list' field. When those shadows are in the pinned list, the list fragments are spliced into the pinned list; otherwise they have no associated list head.
Rework the code that handles these fragments to use the page_list interface rather than manipulating the fields directly. This makes the code cleaner, and allows the 'list' field to be either the compact pdx form or a normal list_entry.
--- xen/arch/x86/mm/shadow/common.c | 42 +++-------- xen/arch/x86/mm/shadow/multi.c | 14 ++-- xen/arch/x86/mm/shadow/private.h | 151 ++++++++++++++++++++++----------------- xen/include/xen/mm.h | 9 +++ 4 files changed, 114 insertions(+), 102 deletions(-)
xen/arch/x86/mm/shadow/common.c +11 -31
@@ -1220,33 +1220,6 @@ static unsigned int shadow_min_acceptable_pages(struct domain *d)return (vcpu_count *128 ); } -/* Figure out the size (in pages) of a given shadow type */ -static inline u32 -shadow_size (unsigned int shadow_type) -{ -static const u32 type_to_size[SH_type_unused] = { -1 ,/* SH_type_none */ -2 ,/* SH_type_l1_32_shadow */ -2 ,/* SH_type_fl1_32_shadow */ -4 ,/* SH_type_l2_32_shadow */ -1 ,/* SH_type_l1_pae_shadow */ -1 ,/* SH_type_fl1_pae_shadow */ -1 ,/* SH_type_l2_pae_shadow */ -1 ,/* SH_type_l2h_pae_shadow */ -1 ,/* SH_type_l1_64_shadow */ -1 ,/* SH_type_fl1_64_shadow */ -1 ,/* SH_type_l2_64_shadow */ -1 ,/* SH_type_l2h_64_shadow */ -1 ,/* SH_type_l3_64_shadow */ -1 ,/* SH_type_l4_64_shadow */ -1 ,/* SH_type_p2m_table */ -1 ,/* SH_type_monitor_table */ -1 /* SH_type_oos_snapshot */ - }; -ASSERT (shadow_type < SH_type_unused); -return type_to_size[shadow_type]; -} -/* Dispatcher function: call the per-mode function that will unhook the * non-Xen mappings in this top-level shadow mfn. With user_only == 1, * unhooks only the user-mode mappings. */
@@ -1489,9 +1462,6 @@ mfn_t shadow_alloc(struct domain *d,break ; } -/* Page lists don't have pointers back to the head structure, so -* it's safe to use a head structure on the stack to link the pages -* together. */ INIT_PAGE_LIST_HEAD (&tmp_list);/* Init page info fields and clear the pages */
@@ -1525,6 +1495,14 @@ mfn_t shadow_alloc(struct domain *d,if ( shadow_type >= SH_type_min_shadow && shadow_type <= SH_type_max_shadow ) sp->u.sh.head =1 ; + +#ifndef PAGE_LIST_NULL +/* The temporary list-head is on our stack. Blank out the +* pointers to it in the shadows, just to get a clean failure if +* we accidentally follow them. */ + tmp_list.next->prev = tmp_list.prev->next =NULL ; +#endif +return page_to_mfn (sp); }
@@ -1533,6 +1511,7 @@ mfn_t shadow_alloc(struct domain *d,void shadow_free (struct domain *d,mfn_t smfn) {struct page_info *next =NULL , *sp =mfn_to_page (smfn); +struct page_list_head *pin_list;unsigned int pages;u32 shadow_type;int i;
@@ -1544,6 +1523,7 @@ void shadow_free(struct domain *d, mfn_t smfn)ASSERT (shadow_type != SH_type_none);ASSERT (sp->u.sh.head || (shadow_type > SH_type_max_shadow)); pages =shadow_size (shadow_type); + pin_list = &d->arch.paging.shadow.pinned_shadows;for ( i =0 ; i < pages; i++ ) {
@@ -1564,7 +1544,7 @@ void shadow_free(struct domain *d, mfn_t smfn)#endif /* Get the next page before we overwrite the list header */ if ( i < pages -1 ) - next =pdx_to_page (sp->list.next); + next =page_list_next (sp, pin_list);/* Strip out the type: this is now a free shadow page */ sp->u.sh.type = sp->u.sh.head =0 ;/* Remember the TLB timestamp so we will know whether to flush
xen/arch/x86/mm/shadow/multi.c +7 -7
@@ -428,20 +428,20 @@ sh_guest_get_eff_l1e(struct vcpu *v, unsigned long addr, void *eff_l1e)/* From one page of a multi-page shadow, find the next one */ static inline mfn_t sh_next_page (mfn_t smfn) { -mfn_t next; -struct page_info *pg =mfn_to_page (smfn); +struct page_info *pg =mfn_to_page (smfn), *next; +struct page_list_head h =PAGE_LIST_HEAD_INIT (h);ASSERT (pg->u.sh.type == SH_type_l1_32_shadow || pg->u.sh.type == SH_type_fl1_32_shadow || pg->u.sh.type == SH_type_l2_32_shadow);ASSERT (pg->u.sh.type == SH_type_l2_32_shadow || pg->u.sh.head); -ASSERT (pg->list.next !=PAGE_LIST_NULL ); - next =_mfn (pdx_to_pfn (pg->list.next)); + next =page_list_next (pg, &h); -ASSERT (mfn_to_page (next)->u.sh.type == pg->u.sh.type); -ASSERT (!mfn_to_page (next)->u.sh.head); -return next; +ASSERT (next); +ASSERT (next->u.sh.type == pg->u.sh.type); +ASSERT (!next->u.sh.head); +return page_to_mfn (next); } static inline u32
xen/arch/x86/mm/shadow/private.h +87 -64
210 lines not on this page · raw message
xen/include/xen/mm.h +9 -0
@@ -172,6 +172,11 @@ page_list_first(const struct page_list_head *head)return head->next; }static inline struct page_info * +page_list_last (const struct page_list_head *head) +{ +return head->tail; +} +static inline struct page_info *page_list_next (const struct page_info *page,const struct page_list_head *head) {
@@ -331,8 +336,12 @@ page_list_splice(struct page_list_head *list, struct page_list_head *head)# define page_list_empty list_empty# define page_list_first (hd) list_entry((hd)->next, \ struct page_info, list) +# define page_list_last (hd) list_entry((hd)->prev, \ + struct page_info, list)# define page_list_next (pg, hd) list_entry((pg)->list.next, \ struct page_info, list) +# define page_list_prev (pg, hd) list_entry((pg)->list.prev, \ + struct page_info, list)# define page_list_add (pg, hd) list_add(&(pg)->list, hd)# define page_list_add_tail (pg, hd) list_add_tail(&(pg)->list, hd)# define page_list_del (pg, hd) list_del(&(pg)->list)
Signature · 1 line
2.1.4
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 29.01.15 at 18:30, <[email protected]> wrote:
OK, here's what I have. It keeps the mechanism the same, threading on the main page list entry, but tidies it up to use the
page_listoperations rather than open-coding. I've tested it (lightly - basically jsut boot testing) with 32bit, 32pae and 64bit Windows VMs (all SMP), with bigmem=y and bigmem=n.It was developed on top of your bigmem series, but it should apply OK before patch 3/4, removing the need to nobble shadow-paging in that patch.
Thanks, looks quite okay, just a couple of remarks.
xen/arch/x86/mm/shadow/common.c mfn_t shadow_alloc(struct domain *d, · line 1504
Quoted 11 lines
xen/arch/x86/mm/shadow/common.c +7
-0@@ -1525,6 +1495,14 @@ mfn_t shadow_alloc(struct domain *d,if ( shadow_type >= SH_type_min_shadow && shadow_type <= SH_type_max_shadow ) sp->u.sh.head =1 ; + +#ifndef PAGE_LIST_NULL +/* The temporary list-head is on our stack. Blank out the +* pointers to it in the shadows, just to get a clean failure if +* we accidentally follow them. */ + tmp_list.next->prev = tmp_list.prev->next =NULL ; +#endif
Perhaps better to use LIST_POISON{1,2} here, so we don't point into PV VA space?
Quoted 22 lines
xen/arch/x86/mm/shadow/private.h +16
-0@@ -318,6 +318,33 @@ static inline int mfn_oos_may_write(mfn_t gmfn) }#endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */ +/* Figure out the size (in pages) of a given shadow type */ +static inline u32 +shadow_size (unsigned int shadow_type) +{ +static const u32 type_to_size[SH_type_unused] = { +1 ,/* SH_type_none */ +2 ,/* SH_type_l1_32_shadow */ +2 ,/* SH_type_fl1_32_shadow */ +4 ,/* SH_type_l2_32_shadow */ +1 ,/* SH_type_l1_pae_shadow */ +1 ,/* SH_type_fl1_pae_shadow */ +1 ,/* SH_type_l2_pae_shadow */ +1 ,/* SH_type_l2h_pae_shadow */ +1 ,/* SH_type_l1_64_shadow */ +1 ,/* SH_type_fl1_64_shadow */ +1 ,/* SH_type_l2_64_shadow */
xen/arch/x86/mm/shadow/private.h static inline int mfn_oos_may_write(mfn_t gmfn) · line 346
Quoted 10 lines
xen/arch/x86/mm/shadow/private.h +10
-0+1 ,/* SH_type_l2h_64_shadow */ +1 ,/* SH_type_l3_64_shadow */ +1 ,/* SH_type_l4_64_shadow */ +1 ,/* SH_type_p2m_table */ +1 ,/* SH_type_monitor_table */ + 1 /* SH_type_oos_snapshot */ + }; +ASSERT (shadow_type < SH_type_unused); +return type_to_size[shadow_type]; +}
Isn't this going to lead to multiple instances of that static array?
Quoted 56 lines
xen/arch/x86/mm/shadow/private.h +15
-27@@ -668,46 +697,40 @@ static inline int sh_pin(struct vcpu *v, mfn_t smfn)* of pinned shadows, and release the extra ref. */ static inline void sh_unpin (struct vcpu *v,mfn_t smfn) { -struct page_list_head h, *pin_list; -struct page_info *sp; - +struct page_list_head tmp_list, *pin_list; +struct page_info *sp, *next; +unsigned int i, head_type; +ASSERT (mfn_valid (smfn)); sp =mfn_to_page (smfn); + head_type = sp->u.sh.type;ASSERT (sh_type_is_pinnable (v, sp->u.sh.type));ASSERT (sp->u.sh.head); -/* Treat the up-to-four pages of the shadow as a unit in the list ops */ - h.next = h.tail = sp; -if ( sp->u.sh.type == SH_type_l2_32_shadow ) - { - h.tail =pdx_to_page (h.tail->list.next); - h.tail =pdx_to_page (h.tail->list.next); - h.tail =pdx_to_page (h.tail->list.next); -ASSERT (h.tail->u.sh.type == SH_type_l2_32_shadow); - } - pin_list = &v->domain->arch.paging.shadow.pinned_shadows; -if ( !sp->u.sh.pinned )return ; - sp->u.sh.pinned =0 ; -/* Cut the sub-list out of the list of pinned shadows */ -if ( pin_list->next == h.next && pin_list->tail == h.tail ) - pin_list->next = pin_list->tail =NULL ; -else +/* Cut the sub-list out of the list of pinned shadows, +* stitching it back into a list fragment of its own. */ + pin_list = &v->domain->arch.paging.shadow.pinned_shadows; +INIT_PAGE_LIST_HEAD (&tmp_list); +for ( i =0 ; i <shadow_size (head_type); i++ ) { -if ( pin_list->next == h.next ) - pin_list->next =page_list_next (h.tail, pin_list); -else -page_list_prev (h.next, pin_list)->list.next = h.tail->list.next; -if ( pin_list->tail == h.tail ) - pin_list->tail =page_list_prev (h.next, pin_list); -else -page_list_next (h.tail, pin_list)->list.prev = h.next->list.prev; +ASSERT (sp->u.sh.type == head_type); +ASSERT (!i || !sp->u.sh.head); + next =page_list_next (sp, pin_list); +page_list_del (sp, pin_list); +page_list_add_tail (sp, &tmp_list);
xen/arch/x86/mm/shadow/private.h static inline int sh_pin(struct vcpu *v, mfn_t smfn) · line 732
Quoted 10 lines
xen/arch/x86/mm/shadow/private.h +7
-2+ sp = next; } - h.tail->list.next = h.next->list.prev =PAGE_LIST_NULL ; - +#ifndef PAGE_LIST_NULL +/* The temporary list-head is on our stack. Blank out the +* pointers to it in the shadows, just to get a clean failure if +* we accidentally follow them. */ + tmp_list.next->prev = tmp_list.prev->next =NULL ; +#endif
Same here. Perhaps worth putting into another inline helper?
Jan
Quoted 13 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 29.01.15 at 18:30, <[email protected]> wrote:
OK, here's what I have. It keeps the mechanism the same, threading on the main page list entry, but tidies it up to use the
page_listoperations rather than open-coding. I've tested it (lightly - basically jsut boot testing) with 32bit, 32pae and 64bit Windows VMs (all SMP), with bigmem=y and bigmem=n.It was developed on top of your bigmem series, but it should apply OK before patch 3/4, removing the need to nobble shadow-paging in that patch.
Thanks, looks quite okay, just a couple of remarks.
Quoted 14 lines
xen/arch/x86/mm/shadow/common.c mfn_t shadow_alloc(struct domain *d, · line 1504
Quoted 11 lines
xen/arch/x86/mm/shadow/common.c +7
-0@@ -1525,6 +1495,14 @@ mfn_t shadow_alloc(struct domain *d,if ( shadow_type >= SH_type_min_shadow && shadow_type <= SH_type_max_shadow ) sp->u.sh.head =1 ; + +#ifndef PAGE_LIST_NULL +/* The temporary list-head is on our stack. Blank out the +* pointers to it in the shadows, just to get a clean failure if +* we accidentally follow them. */ + tmp_list.next->prev = tmp_list.prev->next =NULL ; +#endif Perhaps better to use LIST_POISON{1,2} here, so we don't point into PV VA space?
Yep, will do.
Quoted 26 lines
Quoted 26 lines
xen/arch/x86/mm/shadow/private.h +20
-0@@ -318,6 +318,33 @@ static inline int mfn_oos_may_write(mfn_t gmfn) }#endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */ +/* Figure out the size (in pages) of a given shadow type */ +static inline u32 +shadow_size (unsigned int shadow_type) +{ +static const u32 type_to_size[SH_type_unused] = { +1 ,/* SH_type_none */ +2 ,/* SH_type_l1_32_shadow */ +2 ,/* SH_type_fl1_32_shadow */ +4 ,/* SH_type_l2_32_shadow */ +1 ,/* SH_type_l1_pae_shadow */ +1 ,/* SH_type_fl1_pae_shadow */ +1 ,/* SH_type_l2_pae_shadow */ +1 ,/* SH_type_l2h_pae_shadow */ +1 ,/* SH_type_l1_64_shadow */ +1 ,/* SH_type_fl1_64_shadow */ +1 ,/* SH_type_l2_64_shadow */ +1 ,/* SH_type_l2h_64_shadow */ +1 ,/* SH_type_l3_64_shadow */ +1 ,/* SH_type_l4_64_shadow */ +1 ,/* SH_type_p2m_table */
Quoted 8 lines
xen/arch/x86/mm/shadow/private.h static inline int mfn_oos_may_write(mfn_t gmfn) · line 346
Quoted 6 lines
xen/arch/x86/mm/shadow/private.h +6
-0+1 ,/* SH_type_monitor_table */ + 1 /* SH_type_oos_snapshot */ + }; +ASSERT (shadow_type < SH_type_unused); +return type_to_size[shadow_type]; +}Isn't this going to lead to multiple instances of that static array?
Urgh, maybe. I'd have thought this would end up as a common symbol (if that's the term I mean - one where the linker will merge identical copies) but I didn't check what actually happens. I'll move it back into a .c and just have the lookup function in the header.
Quoted 8 lines
xen/arch/x86/mm/shadow/private.h static inline int mfn_oos_may_write(mfn_t gmfn)
Quoted 6 lines
xen/arch/x86/mm/shadow/private.h +6
-0+#ifndef PAGE_LIST_NULL +/* The temporary list-head is on our stack. Blank out the +* pointers to it in the shadows, just to get a clean failure if +* we accidentally follow them. */ + tmp_list.next->prev = tmp_list.prev->next =NULL ; +#endif Same here. Perhaps worth putting into another inline helper?
Yep, will do so for v2.
Cheers,
Tim.
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 30.01.15 at 11:20, <[email protected]> wrote:
Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 29.01.15 at 18:30, <[email protected]> wrote:
xen/arch/x86/mm/shadow/private.h +20
-0@@ -318,6 +318,33 @@ static inline int mfn_oos_may_write(mfn_t gmfn) }#endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */ +/* Figure out the size (in pages) of a given shadow type */ +static inline u32 +shadow_size (unsigned int shadow_type) +{ +static const u32 type_to_size[SH_type_unused] = { +1 ,/* SH_type_none */ +2 ,/* SH_type_l1_32_shadow */ +2 ,/* SH_type_fl1_32_shadow */ +4 ,/* SH_type_l2_32_shadow */ +1 ,/* SH_type_l1_pae_shadow */ +1 ,/* SH_type_fl1_pae_shadow */ +1 ,/* SH_type_l2_pae_shadow */ +1 ,/* SH_type_l2h_pae_shadow */ +1 ,/* SH_type_l1_64_shadow */ +1 ,/* SH_type_fl1_64_shadow */ +1 ,/* SH_type_l2_64_shadow */ +1 ,/* SH_type_l2h_64_shadow */ +1 ,/* SH_type_l3_64_shadow */ +1 ,/* SH_type_l4_64_shadow */ +1 ,/* SH_type_p2m_table */ Quoted 8 lines
xen/arch/x86/mm/shadow/private.h static inline int mfn_oos_may_write(mfn_t gmfn) · line 346
Quoted 6 lines
xen/arch/x86/mm/shadow/private.h +6
-0+1 ,/* SH_type_monitor_table */ + 1 /* SH_type_oos_snapshot */ + }; +ASSERT (shadow_type < SH_type_unused); +return type_to_size[shadow_type]; +}Isn't this going to lead to multiple instances of that static array?
Quoted 3 lines
Urgh, maybe. I'd have thought this would end up as a common symbol (if that's the term I mean - one where the linker will merge identical copies) but I didn't check what actually happens.
That's C++ behavior you have in mind.
Jan