Provide support for multiple frame buffers in Xen.
PATCH 1/1 Provide support for multiple frame buffers in Xen.
From: Robert Phillips <[email protected]>
Support is provided for both shadow and hardware assisted paging (HAP) modes. This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon request, returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged.
--- xen/arch/x86/hvm/Makefile | 3 +- xen/arch/x86/hvm/dirty_vram.c | 878 ++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/hvm.c | 4 +- xen/arch/x86/mm/hap/hap.c | 140 +----- xen/arch/x86/mm/paging.c | 232 ++++----- xen/arch/x86/mm/shadow/common.c | 335 +++++++------ xen/arch/x86/mm/shadow/multi.c | 169 +++---- xen/arch/x86/mm/shadow/multi.h | 7 +- xen/arch/x86/mm/shadow/types.h | 1 + xen/include/asm-x86/hap.h | 4 - xen/include/asm-x86/hvm/dirty_vram.h | 157 ++++++ xen/include/asm-x86/hvm/domain.h | 2 +- xen/include/asm-x86/paging.h | 22 +- xen/include/asm-x86/shadow.h | 6 - 14 files changed, 1403 insertions(+), 557 deletions(-) create mode 100644 xen/arch/x86/hvm/dirty_vram.c create mode 100644 xen/include/asm-x86/hvm/dirty_vram.h
xen/arch/x86/hvm/Makefile +2 -1
@@ -2,6 +2,7 @@ subdir-y += svm subdir-y += vmx obj-y += asid.o +obj-y += dirty_vram.o obj-y += emulate.o obj-y += hpet.o obj-y += hvm.o
@@ -22,4 +23,4 @@ obj-y += vlapic.o obj-y += vmsi.o obj-y += vpic.o obj-y += vpt.o -obj-y += vpmu.o +obj-y += vpmu.o
xen/arch/x86/hvm/dirty_vram.c new file +878 -0
878 lines not on this page · raw message
xen/arch/x86/hvm/hvm.c +2 -2
@@ -57,6 +57,7 @@#include <asm/hvm/cacheattr.h> #include <asm/hvm/trace.h> #include <asm/hvm/nestedhvm.h> +#include <asm/hvm/dirty_vram.h> #include <asm/mtrr.h> #include <asm/apic.h> #include <public/sched.h>
@@ -1433,8 +1434,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa,*/ if ( access_w ) { -paging_mark_dirty (v->domain,mfn_x (mfn)); -p2m_change_type (v->domain, gfn, p2m_ram_logdirty, p2m_ram_rw); +paging_mark_dirty_hap (v->domain, gfn,mfn_x (mfn)); } rc =1 ; goto out_put_gfn;
xen/arch/x86/mm/hap/hap.c +3 -137
162 lines not on this page · raw message
xen/arch/x86/mm/paging.c +87 -145
283 lines not on this page · raw message
xen/arch/x86/mm/shadow/common.c +182 -153
379 lines not on this page · raw message
xen/arch/x86/mm/shadow/multi.c +75 -94
250 lines not on this page · raw message
xen/arch/x86/mm/shadow/multi.h +6 -1
@@ -66,7 +66,12 @@ SHADOW_INTERNAL_NAME(sh_rm_write_access_from_l1, GUEST_LEVELS)extern int SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1 ,GUEST_LEVELS ) (struct vcpu *v,mfn_t sl1mfn,mfn_t target_mfn); - +extern int +SHADOW_INTERNAL_NAME (sh_find_vram_mappings_in_l1 ,GUEST_LEVELS ) + (struct vcpu *v,mfn_t sl1mfn, +unsigned long begin_pfn, +unsigned long end_pfn, +int *removed);extern void SHADOW_INTERNAL_NAME (sh_clear_shadow_entry ,GUEST_LEVELS ) (struct vcpu *v,void *ep,mfn_t smfn);
xen/arch/x86/mm/shadow/types.h +1 -0
@@ -229,6 +229,7 @@ static inline shadow_l4e_t shadow_l4e_from_mfn(mfn_t mfn, u32 flags)#define sh_update_cr3 INTERNAL_NAME(sh_update_cr3)#define sh_rm_write_access_from_l1 INTERNAL_NAME(sh_rm_write_access_from_l1)#define sh_rm_mappings_from_l1 INTERNAL_NAME(sh_rm_mappings_from_l1) +#define sh_find_vram_mappings_in_l1 INTERNAL_NAME(sh_find_vram_mappings_in_l1)#define sh_remove_l1_shadow INTERNAL_NAME(sh_remove_l1_shadow)#define sh_remove_l2_shadow INTERNAL_NAME(sh_remove_l2_shadow)#define sh_remove_l3_shadow INTERNAL_NAME(sh_remove_l3_shadow)
xen/include/asm-x86/hap.h +0 -4
@@ -57,10 +57,6 @@ void hap_final_teardown(struct domain *d);void hap_teardown (struct domain *d);void hap_vcpu_init (struct vcpu *v);void hap_logdirty_init (struct domain *d); -int hap_track_dirty_vram (struct domain *d, -unsigned long begin_pfn, -unsigned long nr, -XEN_GUEST_HANDLE_64 (uint8) dirty_bitmap);extern const struct paging_mode *hap_paging_get_mode (struct vcpu *);
xen/include/asm-x86/hvm/dirty_vram.h new file +157 -0
157 lines not on this page · raw message
xen/include/asm-x86/hvm/domain.h +1 -1
@@ -74,7 +74,7 @@ struct hvm_domain {struct list_head pinned_cacheattr_ranges;/* VRAM dirty support. */ -struct sh_dirty_vram *dirty_vram; +struct dv_dirty_vram * dirty_vram;/* If one of vcpus of this domain is in no_fill_mode or * mtrr/pat between vcpus is not the same, set is_in_uc_mode
xen/include/asm-x86/paging.h +9 -13
@@ -137,10 +137,10 @@ struct paging_mode {void paging_free_log_dirty_bitmap (struct domain *d);/* get the dirty bitmap for a specific range of pfns */ -int paging_log_dirty_range (struct domain *d, -unsigned long begin_pfn, -unsigned long nr, -XEN_GUEST_HANDLE_64 (uint8) dirty_bitmap); +void paging_log_dirty_range (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +uint8_t *dirty_bitmap);/* enable log dirty */ int paging_log_dirty_enable (struct domain *d);
@@ -161,6 +161,11 @@ void paging_mark_dirty(struct domain *d, unsigned long guest_mfn);* This is called from inside paging code, with the paging lock held. */ int paging_mfn_is_dirty (struct domain *d,mfn_t gmfn); +/* mark a page as dirty, from hap page fault handler */ +void paging_mark_dirty_hap (struct domain *d, +unsigned long pfn, +unsigned long guest_mfn); +/* * Log-dirty radix tree indexing: * All tree nodes are PAGE_SIZE bytes, mapped on-demand.
@@ -183,15 +188,6 @@ int paging_mfn_is_dirty(struct domain *d, mfn_t gmfn);#define L4_LOGDIRTY_IDX (pfn) 0#endif -/* VRAM dirty tracking support */ -struct sh_dirty_vram { -unsigned long begin_pfn; -unsigned long end_pfn; -paddr_t *sl1ma; -uint8_t *dirty_bitmap; -s_time_t last_dirty; -}; -/***************************************************************************** * Entry points into the paging-assistance code */
xen/include/asm-x86/shadow.h +0 -6
@@ -62,12 +62,6 @@ void shadow_vcpu_init(struct vcpu *v);/* Enable an arbitrary shadow mode. Call once at domain creation. */ int shadow_enable (struct domain *d,u32 mode); -/* Enable VRAM dirty bit tracking. */ -int shadow_track_dirty_vram (struct domain *d, -unsigned long first_pfn, -unsigned long nr, -XEN_GUEST_HANDLE_64 (uint8) dirty_bitmap); -/* Handler for shadow control ops: operations from user-space to enable * and disable ephemeral shadow modes (test mode and log-dirty mode) and * manipulate the log-dirty bitmap. */
Signature · 1 line
1.7.9.5
Here is a document describing this patch.
Robert Phillips
Principal Software Engineer, XenClient-Enterprise - Westford [email protected]
-----Original Message-----
From: Robert Phillips [mailto:[email protected]]
Sent: Tuesday, October 16, 2012 2:15 PM
To: [email protected]
Cc: Robert Phillips; Robert Phillips
Subject: [PATCH] Provide support for multiple frame buffers in Xen.
From: Robert Phillips <[email protected]>
Support is provided for both shadow and hardware assisted paging (HAP) modes. This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon request, returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged.
--- xen/arch/x86/hvm/Makefile | 3 +- xen/arch/x86/hvm/dirty_vram.c | 878 ++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/hvm.c | 4 +- xen/arch/x86/mm/hap/hap.c | 140 +----- xen/arch/x86/mm/paging.c | 232 ++++----- xen/arch/x86/mm/shadow/common.c | 335 +++++++------ xen/arch/x86/mm/shadow/multi.c | 169 +++---- xen/arch/x86/mm/shadow/multi.h | 7 +- xen/arch/x86/mm/shadow/types.h | 1 + xen/include/asm-x86/hap.h | 4 - xen/include/asm-x86/hvm/dirty_vram.h | 157 ++++++ xen/include/asm-x86/hvm/domain.h | 2 +- xen/include/asm-x86/paging.h | 22 +- xen/include/asm-x86/shadow.h | 6 - 14 files changed, 1403 insertions(+), 557 deletions(-) create mode 100644 xen/arch/x86/hvm/dirty_vram.c create mode 100644 xen/include/asm-x86/hvm/dirty_vram.h
xen/arch/x86/hvm/Makefile +2 -1
@@ -2,6 +2,7 @@ subdir-y += svm subdir-y += vmx obj-y += asid.o +obj-y += dirty_vram.o obj-y += emulate.o obj-y += hpet.o obj-y += hvm.o
@@ -22,4 +23,4 @@ obj-y += vlapic.o obj-y += vmsi.o obj-y += vpic.o obj-y += vpt.o -obj-y += vpmu.o +obj-y += vpmu.o
xen/arch/x86/hvm/dirty_vram.c new file +878 -0
878 lines not on this page · raw message
xen/arch/x86/hvm/hvm.c +2 -2
@@ -57,6 +57,7 @@#include <asm/hvm/cacheattr.h> #include <asm/hvm/trace.h> #include <asm/hvm/nestedhvm.h> +#include <asm/hvm/dirty_vram.h> #include <asm/mtrr.h> #include <asm/apic.h> #include <public/sched.h>
@@ -1433,8 +1434,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa,*/ if ( access_w ) { -paging_mark_dirty (v->domain,mfn_x (mfn)); -p2m_change_type (v->domain, gfn, p2m_ram_logdirty, p2m_ram_rw); +paging_mark_dirty_hap (v->domain, gfn,mfn_x (mfn)); } rc =1 ; goto out_put_gfn;
xen/arch/x86/mm/hap/hap.c +3 -137
162 lines not on this page · raw message
xen/arch/x86/mm/paging.c +87 -145
283 lines not on this page · raw message
xen/arch/x86/mm/shadow/common.c +182 -153
379 lines not on this page · raw message
xen/arch/x86/mm/shadow/multi.c +75 -94
250 lines not on this page · raw message
xen/arch/x86/mm/shadow/multi.h +6 -1
@@ -66,7 +66,12 @@ SHADOW_INTERNAL_NAME(sh_rm_write_access_from_l1, GUEST_LEVELS)extern int SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1 ,GUEST_LEVELS ) (struct vcpu *v,mfn_t sl1mfn,mfn_t target_mfn); - +extern int +SHADOW_INTERNAL_NAME (sh_find_vram_mappings_in_l1 ,GUEST_LEVELS ) + (struct vcpu *v,mfn_t sl1mfn, +unsigned long begin_pfn, +unsigned long end_pfn, +int *removed);extern void SHADOW_INTERNAL_NAME (sh_clear_shadow_entry ,GUEST_LEVELS ) (struct vcpu *v,void *ep,mfn_t smfn);
xen/arch/x86/mm/shadow/types.h +1 -0
@@ -229,6 +229,7 @@ static inline shadow_l4e_t shadow_l4e_from_mfn(mfn_t mfn, u32 flags)#define sh_update_cr3 INTERNAL_NAME(sh_update_cr3)#define sh_rm_write_access_from_l1 INTERNAL_NAME(sh_rm_write_access_from_l1)#define sh_rm_mappings_from_l1 INTERNAL_NAME(sh_rm_mappings_from_l1) +#define sh_find_vram_mappings_in_l1 INTERNAL_NAME(sh_find_vram_mappings_in_l1)#define sh_remove_l1_shadow INTERNAL_NAME(sh_remove_l1_shadow)#define sh_remove_l2_shadow INTERNAL_NAME(sh_remove_l2_shadow)#define sh_remove_l3_shadow INTERNAL_NAME(sh_remove_l3_shadow)
xen/include/asm-x86/hap.h +0 -4
@@ -57,10 +57,6 @@ void hap_final_teardown(struct domain *d);void hap_teardown (struct domain *d);void hap_vcpu_init (struct vcpu *v);void hap_logdirty_init (struct domain *d); -int hap_track_dirty_vram (struct domain *d, -unsigned long begin_pfn, -unsigned long nr, -XEN_GUEST_HANDLE_64 (uint8) dirty_bitmap);extern const struct paging_mode *hap_paging_get_mode (struct vcpu *);
xen/include/asm-x86/hvm/dirty_vram.h new file +157 -0
157 lines not on this page · raw message
xen/include/asm-x86/hvm/domain.h +1 -1
@@ -74,7 +74,7 @@ struct hvm_domain {struct list_head pinned_cacheattr_ranges;/* VRAM dirty support. */ -struct sh_dirty_vram *dirty_vram; +struct dv_dirty_vram * dirty_vram;/* If one of vcpus of this domain is in no_fill_mode or * mtrr/pat between vcpus is not the same, set is_in_uc_mode
xen/include/asm-x86/paging.h +9 -13
@@ -137,10 +137,10 @@ struct paging_mode {void paging_free_log_dirty_bitmap (struct domain *d);/* get the dirty bitmap for a specific range of pfns */ -int paging_log_dirty_range (struct domain *d, -unsigned long begin_pfn, -unsigned long nr, -XEN_GUEST_HANDLE_64 (uint8) dirty_bitmap); +void paging_log_dirty_range (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +uint8_t *dirty_bitmap);/* enable log dirty */ int paging_log_dirty_enable (struct domain *d);
@@ -161,6 +161,11 @@ void paging_mark_dirty(struct domain *d, unsigned long guest_mfn);* This is called from inside paging code, with the paging lock held. */ int paging_mfn_is_dirty (struct domain *d,mfn_t gmfn); +/* mark a page as dirty, from hap page fault handler */ +void paging_mark_dirty_hap (struct domain *d, +unsigned long pfn, +unsigned long guest_mfn); +/* * Log-dirty radix tree indexing: * All tree nodes are PAGE_SIZE bytes, mapped on-demand.
@@ -183,15 +188,6 @@ int paging_mfn_is_dirty(struct domain *d, mfn_t gmfn);#define L4_LOGDIRTY_IDX (pfn) 0#endif -/* VRAM dirty tracking support */ -struct sh_dirty_vram { -unsigned long begin_pfn; -unsigned long end_pfn; -paddr_t *sl1ma; -uint8_t *dirty_bitmap; -s_time_t last_dirty; -}; -/***************************************************************************** * Entry points into the paging-assistance code */
xen/include/asm-x86/shadow.h +0 -6
@@ -62,12 +62,6 @@ void shadow_vcpu_init(struct vcpu *v);/* Enable an arbitrary shadow mode. Call once at domain creation. */ int shadow_enable (struct domain *d,u32 mode); -/* Enable VRAM dirty bit tracking. */ -int shadow_track_dirty_vram (struct domain *d, -unsigned long first_pfn, -unsigned long nr, -XEN_GUEST_HANDLE_64 (uint8) dirty_bitmap); -/* Handler for shadow control ops: operations from user-space to enable * and disable ephemeral shadow modes (test mode and log-dirty mode) and * manipulate the log-dirty bitmap. */
--
1.7.9.5
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 12 lines · Robert Phillips
From: Robert Phillips <[email protected]>
Support is provided for both shadow and hardware assisted paging (HAP) modes. This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon request, returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged.
Signed-off-by Robert Phillips <[email protected]>
Quoted 16 lines
--- xen/arch/x86/hvm/Makefile | 3 +- xen/arch/x86/hvm/dirty_vram.c | 878 ++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/hvm.c | 4 +- xen/arch/x86/mm/hap/hap.c | 140 +----- xen/arch/x86/mm/paging.c | 232 ++++----- xen/arch/x86/mm/shadow/common.c | 335 +++++++------ xen/arch/x86/mm/shadow/multi.c | 169 +++---- xen/arch/x86/mm/shadow/multi.h | 7 +- xen/arch/x86/mm/shadow/types.h | 1 + xen/include/asm-x86/hap.h | 4 - xen/include/asm-x86/hvm/dirty_vram.h | 157 ++++++ xen/include/asm-x86/hvm/domain.h | 2 +- xen/include/asm-x86/paging.h | 22 +- xen/include/asm-x86/shadow.h | 6 - 14 files changed, 1403 insertions(+), 557 deletions(-)
Wow. That's a bunch of code! :) Thanks for sending it, and for the document too.
You don't say why it's useful to have multiple framebuffers -- I take it this is useful for laptop environments? Also, I'm a bit surprised not to see some hypercall API changes to go with it.
Reading the PDF you sent, it looks like you've implemented log-dirty in two new ways: by scanning the shadow PTEs and by scanning EPT entries. But you also leave the old ways (trap-on-access and populate a bitmap) despite saying "The eliminated code was complex, buggy and unnecessary". If the bitmap-tracking code is really buggy we need to know about it, as that's what we use for live migration! What's the problem with it?
I've had a brief skim of the code, and it looks rather over-complex for what it does. I'm not sure what the purpose of all these linked lists of ranges is -- couldn't this just be done with a struct rangeset describing which areas are being tracked? Then since we already maintain a sparse bitmap to hold the dirty log we don't need any other metadata, or other ways of checking dirtiness.
Style nits: you're not consistently following the Xen coding style, in particular the spaces around parentheses in 'if's and 'for's. Also you've got some code in new files that's clearly at least derived from old code but just got your own name and copyright at the head of the file. Please carry the copyright over from old code when you move it.
Cheers,
Tim.
Tim,
Thank you for taking the time (and I expect it was a considerable time) to review this patch.
And thanks for pointing out the coding style nits. I'll re-submit the patch when the dust settles regarding its contents.
re " You don't say why it's useful to have multiple framebuffers ..."
Yes, in a laptop environment there can be multiple monitors, each with its own frame buffer, and the monitors can be plugged/unplugged dynamically. So the number of monitors and the location and size of their framebuffers is dynamic.
re: The " complex, buggy and unnecessary " code eliminated by this patch. I was refering to the function paging_log_dirty_range().
I believe the bug was toward the end of the function where it used to call clear_page(l1)
The function copies bits from l1 into a temporary bitmap, then copies them from there to the user-provided dirty_bitmap. When it's done, it clears the page at l1.
But two framebuffers might cohabit that page, not overlapping but at distinction areas within it.
Reading the dirtiness for one frame buffer and then clearing the whole page wipes out information "owned" by the other frame buffer. This bug would not show up if there is only one frame buffer so your live migration code is ok.
The old code wasn't really necessary because there is an easier way of telling if some page is dirty.
Yes, one can look at the dirty bit as the old code did.
Or one can check if the page's type was switched to p2m_ram_rw, which is what the new function hap_clean_vram_tracking_range() does.
In terms of complexity, the new hap_clean_vram_tracking_range() is much more simple than the old paging_log_dirty_range().
re: " implemented log-dirty in two new ways"
The HAP code path scans EPT entries. Obviously the shadow code does not.
The shadow code does substantial bookkeeping, which I'll get to in a minute, which the HAP path does not. Although HAP does bookkeep the set of ranges, all the linked-list stuff isn't used or allocated.
In shadow mode the new code makes a clean separation between determining the interesting set of PTEs and examining those PTEs looking for dirty bits.
When a new range is detected, the code *does* scan all PTEs looking for ones that map into the new range but (unlike the old code) that is a one-time event.
Quoted 1 line
From then on the range's pages are kept by "dead reckoning", i.e. hooking all changes to PTEs to see if they pertain to the range.
Unfortunately when a process terminates it doesn't tear down its page tables; it just abandons them.
So a range can get left pointing to a PTE that is no longer in a page table page.
But, in due time, the shadow code recognizes this and removes them. The new code hooks that too, and updates its ranges accordingly. When a no-longer-in-use range's pages have all been recycled, the range deletes itself.
So the overhead of bookkeeping a range's set of interesting PTEs is low.
And when it's time to look for dirty bits, we know precisely which PTEs to look at.
The old code used to scan all page tables periodically and we would see a performance hit with precisely that periodicity.
One unfortunate bit of complexity relates to the fact that several PTEs can map to the same guest physical page. We have to bookkeep them all, so each PTEs that maps to a guest physical page must be represented by its own dv_paddr_link, and, for the set that relate to the same guest page, they are all linked together.
The head of the linked list is the entry in the range's pl_tab array that corresponds to that guest physical page.
re: " since we already maintain a sparse bitmap to hold the dirty log"
I don't believe the dirty log is maintained except when page_mode is set for PG_log_dirty.
That mode exists for live migrate and the discipline for entering/leaving it is quite different than the finer granularity needed for dirty vram.
Thanks
-- rsp
Robert Phillips
Principal Software Engineer, XenClient - Citrix - Westford
-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Monday, October 22, 2012 12:10 PM
To: Robert Phillips
Cc: [email protected]; Robert Phillips
Subject: Re: [Xen-devel] [PATCH] Provide support for multiple frame buffers in Xen.
Quoted 12 lines · Robert Phillips
From: Robert Phillips <[email protected]>
Support is provided for both shadow and hardware assisted paging (HAP) modes. This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon request, returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged.
Signed-off-by Robert Phillips <[email protected]>
Quoted 16 lines
--- xen/arch/x86/hvm/Makefile | 3 +- xen/arch/x86/hvm/dirty_vram.c | 878 ++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/hvm.c | 4 +- xen/arch/x86/mm/hap/hap.c | 140 +----- xen/arch/x86/mm/paging.c | 232 ++++----- xen/arch/x86/mm/shadow/common.c | 335 +++++++------ xen/arch/x86/mm/shadow/multi.c | 169 +++---- xen/arch/x86/mm/shadow/multi.h | 7 +- xen/arch/x86/mm/shadow/types.h | 1 + xen/include/asm-x86/hap.h | 4 - xen/include/asm-x86/hvm/dirty_vram.h | 157 ++++++ xen/include/asm-x86/hvm/domain.h | 2 +- xen/include/asm-x86/paging.h | 22 +- xen/include/asm-x86/shadow.h | 6 - 14 files changed, 1403 insertions(+), 557 deletions(-)
Wow. That's a bunch of code! :) Thanks for sending it, and for the document too.
You don't say why it's useful to have multiple framebuffers -- I take it this is useful for laptop environments? Also, I'm a bit surprised not to see some hypercall API changes to go with it.
Reading the PDF you sent, it looks like you've implemented log-dirty in two new ways: by scanning the shadow PTEs and by scanning EPT entries. But you also leave the old ways (trap-on-access and populate a bitmap) despite saying "The eliminated code was complex, buggy and unnecessary". If the bitmap-tracking code is really buggy we need to know about it, as that's what we use for live migration! What's the problem with it?
I've had a brief skim of the code, and it looks rather over-complex for what it does. I'm not sure what the purpose of all these linked lists of ranges is -- couldn't this just be done with a struct rangeset describing which areas are being tracked? Then since we already maintain a sparse bitmap to hold the dirty log we don't need any other metadata, or other ways of checking dirtiness.
Style nits: you're not consistently following the Xen coding style, in particular the spaces around parentheses in 'if's and 'for's. Also you've got some code in new files that's clearly at least derived from old code but just got your own name and copyright at the head of the file. Please carry the copyright over from old code when you move it.
Cheers,
Tim.
Hi Robert,
I've spent a bit more time digging around and I think I have a better idea of why you've done things the way you did. The 'simple' version I was thinking of doesn't work as well as I thought. :|
Quoted 9 lines · Robert Phillips
I believe the bug was toward the end of the function where it used to call
clear_page(l1)The function copies bits from l1 into a temporary bitmap, then copies them from there to the user-provideddirty_bitmap. When it's done, it clears the page at l1. But two framebuffers might cohabit that page, not overlapping but at distinction areas within it. Reading the dirtiness for one frame buffer and then clearing the whole page wipes out information "owned" by the other frame buffer. This bug would not show up if there is only one frame buffer so your live migration code is ok.
Yep, understood.
Quoted 4 lines
And when it's time to look for dirty bits, we know precisely which PTEs to look at. The old code used to scan all page tables periodically and we would see a performance hit with precisely that periodicity.
Was this caused by the 2-second timeout where it would try to unmap the vram if it hadn't been dirtied in that time? Were you finding that it would unmap and then immediately try to map it again?
Quoted 3 lines
One unfortunate bit of complexity relates to the fact that several PTEs can map to the same guest physical page. We have to bookkeep them all
The old code basically relied on this not happening (assuming that framebuffers would be mapped only once). Is that assumption just wrong? Is it broken by things like DirectX?
Quoted 5 lines
so each PTEs that maps to a guest physical page must be represented by its own
dv_paddr_link, and, for the set that relate to the same guest page, they are all linked together. The head of the linked list is the entry in the range'spl_tabarray that corresponds to that guest physical page.
Right; you've built a complete reverse mapping from pfn to ptes.
Quoted 3 lines
re: " since we already maintain a sparse bitmap to hold the dirty log"
I don't believe the dirty log is maintained except whenpage_modeis set forPG_log_dirty.
That mode exists for live migrate and the discipline for entering/leaving it is quite different than the finer granularity needed for dirty vram.
Sorry, I had got confused there. I was thinking that we could move over more to PG_log_dirty-style operation, where we'd trap on writes and update the bitmap (since we now keep that bitmap as a trie the sparseness would be OK). But on closer inspection the cost of clearing all the mappings when the bitmap is cleared would be either too much overhead (throw away _all_ shadows every time) or about as complex as the mechanism needed to scan the _PAGE_DIRTY bits.
I think I have a better idea of the intention of the patch now; I'll go over the code in detail today.
Cheers,
Tim.
Thanks, Tim, for your review.
I still owe xen-devel a revised version of the patch that adheres better to the Xen coding style conventions. (Excuse my ignorance but) are those documented somewhere?
-- rsp
-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Thursday, November 01, 2012 7:03 AM
To: Robert Phillips
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH] Provide support for multiple frame buffers in Xen.
Hi Robert,
I've spent a bit more time digging around and I think I have a better idea of why you've done things the way you did. The 'simple' version I was thinking of doesn't work as well as I thought. :|
Quoted 9 lines · Robert Phillips
I believe the bug was toward the end of the function where it used to call
clear_page(l1)The function copies bits from l1 into a temporary bitmap, then copies them from there to the user-provideddirty_bitmap. When it's done, it clears the page at l1. But two framebuffers might cohabit that page, not overlapping but at distinction areas within it. Reading the dirtiness for one frame buffer and then clearing the whole page wipes out information "owned" by the other frame buffer. This bug would not show up if there is only one frame buffer so your live migration code is ok.
Yep, understood.
Quoted 4 lines
And when it's time to look for dirty bits, we know precisely which PTEs to look at. The old code used to scan all page tables periodically and we would see a performance hit with precisely that periodicity.
Was this caused by the 2-second timeout where it would try to unmap the vram if it hadn't been dirtied in that time? Were you finding that it would unmap and then immediately try to map it again?
Quoted 3 lines
One unfortunate bit of complexity relates to the fact that several PTEs can map to the same guest physical page. We have to bookkeep them all
The old code basically relied on this not happening (assuming that framebuffers would be mapped only once). Is that assumption just wrong? Is it broken by things like DirectX?
Quoted 5 lines
so each PTEs that maps to a guest physical page must be represented by its own
dv_paddr_link, and, for the set that relate to the same guest page, they are all linked together. The head of the linked list is the entry in the range'spl_tabarray that corresponds to that guest physical page.
Right; you've built a complete reverse mapping from pfn to ptes.
Quoted 3 lines
re: " since we already maintain a sparse bitmap to hold the dirty log"
I don't believe the dirty log is maintained except whenpage_modeis set forPG_log_dirty.
That mode exists for live migrate and the discipline for entering/leaving it is quite different than the finer granularity needed for dirty vram.
Sorry, I had got confused there. I was thinking that we could move over more to PG_log_dirty-style operation, where we'd trap on writes and update the bitmap (since we now keep that bitmap as a trie the sparseness would be OK). But on closer inspection the cost of clearing all the mappings when the bitmap is cleared would be either too much overhead (throw away _all_ shadows every time) or about as complex as the mechanism needed to scan the _PAGE_DIRTY bits.
I think I have a better idea of the intention of the patch now; I'll go over the code in detail today.
Cheers,
Tim.
Please can you avoid top posting, it makes conversations harder to follow.
Quoted 4 lines · Robert Phillips
Thanks, Tim, for your review.
I still owe xen-devel a revised version of the patch that adheres better to the Xen coding style conventions. (Excuse my ignorance but) are those documented somewhere?
It's in the file "CODING_STYLE" at the top-level of the source tree.
Ian.
Hi,
Quoted 10 lines · Robert Phillips
From: Robert Phillips <[email protected]>
Support is provided for both shadow and hardware assisted paging (HAP) modes. This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon request, returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged.
Having read through this in detail, it's looking very plausible. :) A few style nits:
- please use the Xen spacings around 'if ( foo )' and 'for ( x; y; z )';
- there's a bit of trailing whitespace in the new file, and a few places where indentation seems to have gone a bit wrong;
- please make sure the whole thing is linewrapped to <80 characters; and
- there's no need for braces around single-line blocks.
More substantive comments:
- I think the dirty_vram.c and dirty_vram.h files belong under mm/ rather than under hvm/. The ``#include "../mm/mm-locks.h"'' is an indicator that this is really MM code.
- Please use xzalloc() rather than xmalloc() + memset(0). It avoids the sizes of alloc and memset getting out of sync.
- The i386 build is dead, so you can drop some #ifdef __i386__ sections.
- There really ought to be some limit on how many PTEs you're willing to track. Otherwise a large guest can consume lots and lots of Xen's memory by making lots of PTEs that point to framebuffers. That might also lead to performance problems, e.g. in the unshadow function that walks over all those liked lists.
Also, I think that the memory for the paddr_links ought to come from the shadow pool (i.e. using domain->arch.paging.alloc_page()) rather than soaking up otherwise free memory.
A few other detailed comments below...
Quoted 4 lines
xen/arch/x86/hvm/dirty_vram.c new file +4
-0+/* Free a paddr_link struct, given address of its predecessor in linked list */ +dv_paddr_link_t * +free_paddr_link(struct domain *d, + dv_paddr_link_t **ppl,
xen/arch/x86/hvm/dirty_vram.c line 367
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ dv_paddr_link_t *pl) +{ +dv_dirty_vram_t *dirty_vram = d->arch.hvm_domain.dirty_vram; +dv_paddr_link_t *npl;/* next pl */ + +ASSERT (paging_locked_by_me (d) ); +/* extension mapping? */ +if (ppl)/* yes. free it */ + { + pl = (*ppl);
This assignment seems like it should always be a noop. Would it be correct to replace it with ASSERT(pl == *ppl)?
Quoted 7 lines
xen/arch/x86/hvm/dirty_vram.c new file +7
-0+ (*ppl) = npl = pl->pl_next; + } +else /* main table */ + { +/* move 2nd mapping to main table. +* and free 2nd mapping */ +dv_paddr_link_t * spl;
xen/arch/x86/hvm/dirty_vram.c line 384
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ spl = pl->pl_next; +if (spl ==NULL ) + { + pl->sl1ma =INVALID_PADDR ; +return pl; + } + pl->sl1ma = spl->sl1ma; + pl->pl_next = spl->pl_next; + npl = pl;/* reprocess main table entry again */ + pl = spl;
OK, that took a lot of staring at to be sure it's right. :) I'd be inclined to just put all paddr_links in the linked list (and have an array of pointers rather than an array of paddr_link_ts). Is it worth having the extra complexity here, and at the callers, to avoid a single memory read?
Quoted 18 lines
xen/arch/x86/hvm/dirty_vram.c new file +18
-0+ } + pl->sl1ma =INVALID_PADDR ; + pl->pl_next = dirty_vram->pl_free; + dirty_vram->pl_free = pl; +return npl; +} + + +/* dirty_vram_range_update() +* This is called whenever a level 1 page table entry is modified. +* If the L1PTE is being cleared, the function removes any paddr_links +* that refer to it. +* If the L1PTE is being set to a frame buffer page, a paddr_link is +* created for that page's entry in pl_tab. +* Returns 1 iff entry found and set or cleared. +*/ +int dirty_vram_range_update(struct domain *d, + unsigned long gfn,
xen/arch/x86/hvm/dirty_vram.c line 412
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+paddr_t sl1ma, + int set) +{ + int effective = 0; +dv_range_t *range; + +ASSERT (paging_locked_by_me (d)); + range =dirty_vram_range_find_gfn (d, gfn); +if ( range ) + {
I think this would be more readable as 'if ( !range ) return 0' here rather than indenting most of the function.
Quoted 16 lines
xen/arch/x86/hvm/dirty_vram.c new file +16
-0+unsigned long i = gfn - range->begin_pfn; +dv_paddr_link_t *pl = &range->pl_tab[ i ]; +dv_paddr_link_t **ppl =NULL ; +int len =0 ; + +/* find matching entry (pl), if any, and its predecessor +* in linked list (ppl) */ +while (pl !=NULL ) + { +if (pl->sl1ma == sl1ma || pl->sl1ma ==INVALID_PADDR ) +break ; + ppl = &pl->pl_next; + pl = *ppl; + len++; + } +
xen/arch/x86/hvm/dirty_vram.c line 438
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+if (set) + { +/* Did we find sl1ma in either the main table or the linked list? */ +if (pl ==NULL )/* no, so we'll need to alloc a link */ + { +ASSERT (ppl !=NULL ); +/* alloc link and append it to list */ + (*ppl) = pl =alloc_paddr_link (d); +if (pl ==NULL ) + goto out;
This needs to signal some sort of error. Otherwise, if we can't add this sl1e to the list we'll just silently fail to track it.
xen/arch/x86/hvm/dirty_vram.c line 441
Quoted 3 lines
xen/arch/x86/hvm/dirty_vram.c new file +3
-0+ } +if ( pl->sl1ma != sl1ma ) + {
ASSERT(pl->sl1ma == INVALID_PADDR) ?
Quoted 21 lines
xen/arch/x86/hvm/dirty_vram.c new file +21
-0+ pl->sl1ma = sl1ma; + range->nr_mappings++; + } + effective =1 ; +if (len > range->mappings_hwm) + { + range->mappings_hwm = len; +#if DEBUG_update_vram_mapping +gdprintk (XENLOG_DEBUG , +"[%lx] set sl1ma:%lx hwm:%d mappings:%d freepages:%d\n" , + gfn, sl1ma, + range->mappings_hwm, + range->nr_mappings, + d->arch.paging.shadow.free_pages); +#endif + } + } +else /* clear */ + { +if (pl && pl->sl1ma == sl1ma ) + {
xen/arch/x86/hvm/dirty_vram.c line 472
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+#if DEBUG_update_vram_mapping +gdprintk (XENLOG_DEBUG , +"[%lx] clear sl1ma:%lx mappings:%d\n" , + gfn, sl1ma, + range->nr_mappings-1 ); +#endif +free_paddr_link (d, ppl, pl); +if ( --range->nr_mappings ==0 ) + { +dirty_vram_range_free (d, range);
What's this for? If the guest unmaps the framebuffer and remaps it (or if the shadow PTs of the mappings are temporarily discarded) this will stop us from tracking the new mappings until the toolstack asks for the bitmap (and then it will be expensive to go and find the mappings).
Quoted 21 lines
xen/arch/x86/hvm/dirty_vram.c new file +20
-0+ } + effective =1 ; + } + } + } + out: +return effective; +} +/* shadow_track_dirty_vram() +* This is the API called by the guest to determine which pages in the range +* from [begin_pfn:begin_pfn+nr) have been dirtied since the last call. +* It creates the domain's dv_dirty_vram on demand. +* It creates ranges on demand when some [begin_pfn:nr) is first encountered. +* To collect the dirty bitmask it calls shadow_scan_dirty_flags(). +* It copies the dirty bitmask into guest storage. +*/ +int shadow_track_dirty_vram (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +XEN_GUEST_HANDLE_64 (uint8) guest_dirty_bitmap)
xen/arch/x86/hvm/dirty_vram.c line 576
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+{ +int rc =0 ; +unsigned long end_pfn = begin_pfn + nr; +int flush_tlb =0 ; +dv_range_t *range; +struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (end_pfn < begin_pfn + || begin_pfn > p2m->max_mapped_pfn + || end_pfn >= p2m->max_mapped_pfn)
I know you just copied this from the old definition but the limits seem wrong here -- I think it should be:
if ( end_pfn < begin_pfn || end_pfn > p2m->max_mapped_pfn + 1 )
Quoted 18 lines
xen/arch/x86/hvm/dirty_vram.c new file +18
-0+/* hap_clean_vram_tracking_range() +* For all the pages in the range specified by [begin_pfn,nr), +* note in the dirty bitmap any page that has been marked as read-write, +* which signifies that the page has been dirtied, and reset the page +* to ram_logdirty. +*/ +void hap_clean_vram_tracking_range (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +uint8_t *dirty_bitmap) +{ +int i; +unsigned long pfn; +dv_dirty_vram_t *dirty_vram = d->arch.hvm_domain.dirty_vram; +dv_range_t *range; + +ASSERT (p2m_locked_by_me (p2m_get_hostp2m (d))); +ASSERT (paging_locked_by_me (d));
xen/arch/x86/hvm/dirty_vram.c line 750
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ +if ( !dirty_vram ) + { +gdprintk (XENLOG_DEBUG ,"Should only be called while tracking dirty vram.\n" ); +return ; + } + + range =dirty_vram_range_find (d, begin_pfn, nr); +if (!range) +return ;
Oughtn't we to return all 1s in the bitmap here? If the range isn't currently being tracked we should conservatively assume it's all dirty, right?
Quoted 33 lines
xen/arch/x86/hvm/dirty_vram.c new file +33
-0+ +/* set l1e entries of P2M table to be read-only. */ +/* On first write, it page faults, its entry is changed to read-write, +* its bit in the dirty bitmap is set, and on retry the write succeeds. */ +for (i =0 , pfn = range->begin_pfn; pfn < range->end_pfn; i++, pfn++) + { +p2m_type_t pt; + pt =p2m_change_type (d, pfn, p2m_ram_rw, p2m_ram_logdirty); +if (pt == p2m_ram_rw) + dirty_bitmap[i >>3 ] |= (1 << (i &7 )); + } +flush_tlb_mask (d->domain_dirty_cpumask); +} + +static void hap_vram_tracking_init (struct domain *d) +{ +paging_log_dirty_init (d, hap_enable_vram_tracking, + hap_disable_vram_tracking, +NULL ); +} + +/* hap_track_dirty_vram() +* Create the domain's dv_dirty_vram struct on demand. +* Create a dirty vram range on demand when some [begin_pfn:begin_pfn+nr] is first encountered. +* Collect the guest_dirty bitmask, a bit mask of the dirties vram pages, by +* calling paging_log_dirty_range(). +*/ +int hap_track_dirty_vram (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +XEN_GUEST_HANDLE_64 (uint8) guest_dirty_bitmap) +{ +long rc =0 ;
xen/arch/x86/hvm/dirty_vram.c line 793
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+dv_dirty_vram_t *dirty_vram; +int restart_log_dirty =0 ; + +paging_lock (d); + dirty_vram = d->arch.hvm_domain.dirty_vram; +if ( nr ) + { +dv_range_t *range =NULL ; +int size = (nr +BITS_PER_LONG -1 ) /BITS_PER_LONG ; +unsigned long dirty_bitmap[size];
All the users of this array cast to (uint8_t *) -- just declare it as uint8_t * instead?
Quoted 31 lines
xen/arch/x86/hvm/dirty_vram.c new file +31
-0+ +/* Already tracking dirty vram? */ +if (paging_mode_log_dirty (d) && dirty_vram )/* yes */ + { +/* Handle the addition of another range */ + range =dirty_vram_range_find (d, begin_pfn, nr); +if ( !range ) + { + rc = -ENOMEM ; +if ( !(range =dirty_vram_range_alloc (d, begin_pfn, nr)) ) + goto param_fail; + restart_log_dirty =1 ; + } + } +/* Just starting to track dirty vram? */ +else if ( !paging_mode_log_dirty (d) && !dirty_vram )/* yes */ + { + rc = -ENOMEM ; +if ( !(dirty_vram =dirty_vram_alloc (d)) ) + goto param_fail; + +if ( !(range =dirty_vram_range_find_or_alloc (d, begin_pfn, nr)) ) + goto param_fail; + + restart_log_dirty =1 ; +/* Initialize callbacks for vram tracking */ +hap_vram_tracking_init (d); + } +else + { +/* Test for invalid combination */
xen/arch/x86/hvm/dirty_vram.c line 834
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+if ( !paging_mode_log_dirty (d) && dirty_vram ) + rc = -EINVAL ; +else /* logging dirty of all memory, not tracking dirty vram */ + rc = -ENODATA ; + goto param_fail; + } + +if (restart_log_dirty) + { +/* disable then enable log dirty */
Why disable and re-enable? The call to paging_log_dirty_range() below will reset the p2m entries of the range you care about, so I think all you need to do is enable it in the 'just starting' case above.
And, since you know you're in HAP mode, not in log-dirty mode, and already have the paging lock, you can just set d->arch.paging.mode |= PG_log_dirty there rather than jumping through the paging_log_dirty_enable() path and messing with locks.
Quoted 20 lines
xen/arch/x86/hvm/dirty_vram.c new file +20
-0+paging_unlock (d); +if (paging_mode_log_dirty (d)) +paging_log_dirty_disable (d); + + rc =paging_log_dirty_enable (d); +paging_lock (d); +if (rc !=0 ) + goto param_fail; + } + +paging_unlock (d); +memset (dirty_bitmap,0x00 , size *BYTES_PER_LONG ); +paging_log_dirty_range (d, begin_pfn, nr, (uint8_t *)dirty_bitmap); + rc = -EFAULT ; +if (copy_to_guest (guest_dirty_bitmap, + (uint8_t *)dirty_bitmap, + size *BYTES_PER_LONG ) ==0 ) + { + rc =0 ; + }
xen/arch/x86/hvm/dirty_vram.c line 864
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ } +else + { +/* If zero pages specified while already tracking dirty vram +* then stop tracking */ +if (paging_mode_log_dirty (d) && dirty_vram ) { +paging_unlock (d); + rc =paging_log_dirty_disable (d); +paging_lock (d); +dirty_vram_free (d);
This is different from the shadow case -- there, IIUC, you just ignore requests where nr == 0; here, you tear down all vram tracking. Can you choose one of those, and document it in the public header?
Quoted 5 lines
xen/arch/x86/hvm/dirty_vram.c new file +5
-0+ }else /* benign no-op */ + { + rc =0 ; + } +paging_unlock (d);
xen/arch/x86/hvm/dirty_vram.c
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +8
-0+ } + +return rc; +/* paging_mark_dirty_hap() +* Make a hap page writeable and mark it as dirty. +* This done atomically under the p2m and paging locks to avoid leaving +* a window where the page might be modified without being marked as dirty. +*/
I'm perplexed by this -- AFAICT it's either not necessary (because all log-dirty read/clean ops are done with the domain paused) or not sufficient (because although the bitmap and the PTE are updated under the p2m lock, the actual dirtying of the page happens at some other time). Can you spell out for me exactly what this is protecting against?
Quoted 9 lines
xen/arch/x86/mm/shadow/common.c +9
-0+typedef int (*hash_pfn_callback_t )(struct vcpu *v, +mfn_t smfn, +unsigned long begin_pfn, +unsigned long end_pfn, +int *removed); + +static int hash_pfn_foreach(struct vcpu *v, + unsigned int callback_mask, + hash_pfn_callback_t callbacks[],
xen/arch/x86/mm/shadow/common.c void shadow_clean_dirty_bitmap(struct domain *d) · line 3583
Quoted 10 lines
xen/arch/x86/mm/shadow/common.c +10
-0+unsigned long begin_pfn, + unsigned long end_pfn) +/* Walk the hash table looking at the types of the entries and +* calling the appropriate callback function for each entry. +* The mask determines which shadow types we call back for, and the array +* of callbacks tells us which function to call. +* Any callback may return non-zero to let us skip the rest of the scan. +* +* WARNING: Callbacks MUST NOT add or remove hash entries unless they +* then return non-zero to terminate the scan. */
This code duplication is a bit much. I think you should recast the existing hash_foreach() function to take a pointer as its fourth argument instead of an MFN, and then make the existing callers just cast
their MFN argument as a pointer. The you can use the same function,
passing a pointer to a struct { begin, end, removed }.
Please make the changes to hash_foreach() in a separate patch from the dirty_vram stuff.
xen/arch/x86/mm/shadow/multi.c static int shadow_set_l1e(struct vcpu *v, · line 1173
Quoted 12 lines
xen/arch/x86/mm/shadow/multi.c +2
-1@@ -1211,12 +1164,14 @@ static int shadow_set_l1e(struct vcpu *v,shadow_l1e_remove_flags (new_sl1e, _PAGE_RW);/* fall through */ case 0 : -shadow_vram_get_l1e (new_sl1e, sl1e, sl1mfn, d); +shadow_vram_fix_l1e (old_sl1e, new_sl1e, sl1e, sl1mfn, d);break ; } } } +shadow_vram_fix_l1e (old_sl1e, new_sl1e, sl1e, sl1mfn, d);
Why is this being called twice here?
Cheers,
Tim.
Hi Tim,
Thank you for your in-depth review.
Concurrent with this email I will submit a revised patch (version 2) containing the changes I mention below.
-- rsp
Quoted 21 lines
-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Thursday, November 01, 2012 10:25 AM
To: Robert Phillips
Cc: [email protected]; Robert Phillips
Subject: Re: [Xen-devel] [PATCH] Provide support for multiple frame buffers in Xen.Hi,
Quoted 3 lines · Robert Phillips
From: Robert Phillips <[email protected]>
Support is provided for both shadow and hardware assisted paging (HAP)
modes.
Quoted 2 lines
This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon
request,
Quoted 3 lines
returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor
Quoted 8 lines
(or monitors) that have changed.
Quoted 1 line
Each monitor has a frame buffer of some size at some position in guest
physical memory.
Quoted 1 line
The set of frame buffers being tracked can change over time as monitors
are plugged and unplugged.
Having read through this in detail, it's looking very plausible. :) A few style nits:
[ Done
Quoted 5 lines
- please use the Xen spacings around 'if ( foo )' and 'for ( x; y; z )';
- there's a bit of trailing whitespace in the new file, and a few places where indentation seems to have gone a bit wrong;
- please make sure the whole thing is linewrapped to <80 characters; and
- there's no need for braces around single-line blocks.
] Done
Quoted 5 lines
More substantive comments:
- I think thedirty_vram.canddirty_vram.hfiles belong undermm/rather than under hvm/. The ``#include "../mm/mm-locks.h"'' is an indicator that this is really MM code.
I have moved dirty_vram.c under mm/
I have moved dirty_vram.h under include/asm-x86 . In that location it is available to modules like hvm.c
Quoted 2 lines
- Please use
xzalloc()rather thanxmalloc()+ memset(0). It avoids the sizes of alloc and memset getting out of sync.
Done
Quoted 1 line
- The i386 build is dead, so you can drop some #ifdef
__i386__sections.
Done
Quoted 5 lines
- There really ought to be some limit on how many PTEs you're willing to track. Otherwise a large guest can consume lots and lots of Xen's memory by making lots of PTEs that point to framebuffers. That might also lead to performance problems, e.g. in the unshadow function that walks over all those liked lists.
Done but see note below in the comment starting "This needs to signal some sort of error".
Quoted 3 lines
Also, I think that the memory for the
paddr_linksought to come from the shadow pool (i.e. usingdomain->arch.paging.alloc_page()) rather than soaking up otherwise free memory.
Done
Quoted 5 lines
A few other detailed comments below...
Quoted 1 line
+/* Free a
paddr_linkstruct, given address of its predecessor in linked list*/
Quoted 16 lines
xen/arch/x86/hvm/dirty_vram.c line 367
Quoted 13 lines
xen/arch/x86/hvm/dirty_vram.c new file +13
-0+dv_paddr_link_t * +free_paddr_link (struct domain *d, +dv_paddr_link_t **ppl, +dv_paddr_link_t *pl) +{ +dv_dirty_vram_t *dirty_vram = d->arch.hvm_domain.dirty_vram; +dv_paddr_link_t *npl;/* next pl */ + +ASSERT (paging_locked_by_me (d) ); +/* extension mapping? */ +if (ppl)/* yes. free it */ + { + pl = (*ppl);This assignment seems like it should always be a noop. Would it be correct to replace it with
ASSERT(pl == *ppl)?
Yes, Done.
Quoted 19 lines
Quoted 8 lines
xen/arch/x86/hvm/dirty_vram.c new file +7
-0+ (*ppl) = npl = pl->pl_next; + } +else /* main table */ + { +/* move 2nd mapping to main table. +* and free 2nd mapping */ +dv_paddr_link_t * spl;xen/arch/x86/hvm/dirty_vram.c line 384
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ spl = pl->pl_next; +if (spl ==NULL ) + { + pl->sl1ma =INVALID_PADDR ; +return pl; + } + pl->sl1ma = spl->sl1ma; + pl->pl_next = spl->pl_next; + npl = pl;/* reprocess main table entry again */ + pl = spl;
Quoted 1 line
OK, that took a lot of staring at to be sure it's right. :)
I added some comments. Reading code shouldn't be so taxing.
Quoted 5 lines
I'd be
inclined to just put allpaddr_linksin the linked list (and have an array of pointers rather than an array ofpaddr_link_ts). Is it worth having the extra complexity here, and at the callers, to avoid a single memory read?
Almost all frame buffer pages have a single mapping so the algorithm's common case is met by constructing pl_tab as an array of paddr_links. That is, the table's paddr_links will rarely point to a chain of extension paddr_links.
The code encapsulates the complexity in a single function. Sorry it required so much staring.
Even if all paddr_links were stored in a linked list, the caller would have to walk that linked list, so would be just as complex.
Quoted 24 lines
Quoted 24 lines
xen/arch/x86/hvm/dirty_vram.c new file +23
-0+ } + pl->sl1ma =INVALID_PADDR ; + pl->pl_next = dirty_vram->pl_free; + dirty_vram->pl_free = pl; +return npl; +} + + +/* dirty_vram_range_update() +* This is called whenever a level 1 page table entry is modified. +* If the L1PTE is being cleared, the function removes any paddr_links +* that refer to it. +* If the L1PTE is being set to a frame buffer page, a paddr_link is +* created for that page's entry in pl_tab. +* Returns 1 iff entry found and set or cleared. +*/ +int dirty_vram_range_update (struct domain *d, +unsigned long gfn, +paddr_t sl1ma, +int set) +{ +int effective =0 ; +dv_range_t *range;
Quoted 8 lines
xen/arch/x86/hvm/dirty_vram.c line 412
Quoted 5 lines
xen/arch/x86/hvm/dirty_vram.c new file +5
-0+ +ASSERT (paging_locked_by_me (d)); + range =dirty_vram_range_find_gfn (d, gfn); +if ( range ) + {I think this would be more readable as 'if ( !range ) return 0' here rather than indenting most of the function.
Done.
Quoted 22 lines
Quoted 22 lines
xen/arch/x86/hvm/dirty_vram.c new file +21
-0+unsigned long i = gfn - range->begin_pfn; +dv_paddr_link_t *pl = &range->pl_tab[ i ]; +dv_paddr_link_t **ppl =NULL ; +int len =0 ; + +/* find matching entry (pl), if any, and its predecessor +* in linked list (ppl) */ +while (pl !=NULL ) + { +if (pl->sl1ma == sl1ma || pl->sl1ma ==INVALID_PADDR ) +break ; + ppl = &pl->pl_next; + pl = *ppl; + len++; + } + +if (set) + { +/* Did we find sl1ma in either the main table or the linked list? */ +if (pl ==NULL )/* no, so we'll need to alloc a link */ + {
Quoted 8 lines
xen/arch/x86/hvm/dirty_vram.c line 438
Quoted 5 lines
xen/arch/x86/hvm/dirty_vram.c new file +5
-0+ASSERT (ppl !=NULL ); +/* alloc link and append it to list */ + (*ppl) = pl =alloc_paddr_link (d); +if (pl ==NULL ) + goto out;This needs to signal some sort of error. Otherwise, if we can't add this sl1e to the list we'll just silently fail to track it.
I don't know what sort of error we can signal.
The immediate symptom would be that areas of the monitor would not be refreshed. But, since we're running out of memory, that might be the least of the quests's woes.
The updated patch actually makes the symptom more likely (though still very unlikely) by putting an arbitrary bound on the length of paddr_link chains.
It handles a rogue process, one that has an arbitrarily large number of mappings for frame buffer pages, by simply not recording the excessive mappings.
If that results in unrefreshed blocks on the monitor, so be it.
Quoted 6 lines
xen/arch/x86/hvm/dirty_vram.c line 441
Quoted 4 lines
xen/arch/x86/hvm/dirty_vram.c new file +3
-0+ } +if ( pl->sl1ma != sl1ma ) + {
ASSERT(pl->sl1ma == INVALID_PADDR)?
Yes, Done.
Quoted 30 lines
xen/arch/x86/hvm/dirty_vram.c
Quoted 11 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ pl->sl1ma = sl1ma; + range->nr_mappings++; + } + effective =1 ; +if (len > range->mappings_hwm) + { + range->mappings_hwm = len; +#if DEBUG_update_vram_mapping + gdprintk(XENLOG_DEBUG, + "[%lx] set sl1ma:%lx hwm:%d mappings:%dfreepages:%d\n",
Quoted 18 lines
xen/arch/x86/hvm/dirty_vram.c new file +18
-0+ gfn, sl1ma, + range->mappings_hwm, + range->nr_mappings, + d->arch.paging.shadow.free_pages); +#endif + } + } +else /* clear */ + { +if (pl && pl->sl1ma == sl1ma ) + { +#if DEBUG_update_vram_mapping +gdprintk (XENLOG_DEBUG , +"[%lx] clear sl1ma:%lx mappings:%d\n" , + gfn, sl1ma, + range->nr_mappings-1 ); +#endif +free_paddr_link (d, ppl, pl);
Quoted 9 lines
xen/arch/x86/hvm/dirty_vram.c line 472
Quoted 3 lines
xen/arch/x86/hvm/dirty_vram.c new file +3
-0+if ( --range->nr_mappings ==0 ) + { +dirty_vram_range_free (d, range);What's this for? If the guest unmaps the framebuffer and remaps it (or if the shadow PTs of the mappings are temporarily discarded) this will stop us from tracking the new mappings until the toolstack asks for the bitmap (and then it will be expensive to go and find the mappings).
I don't see this happening. If the guest unmaps the framebuffer, the shadow code lazily recovers the shadow pages, so tracking will continue until it decides a page is no longer a shadow page. That is when this code is invoked.
It tears down the mappings to that page and if some range ends up with no mappings then the range is useless.
Vram ranges are generated willy-nilly as needed. This is the only mechanism for cleaning them up.
Quoted 37 lines
xen/arch/x86/hvm/dirty_vram.c
Quoted 11 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+ } + effective =1 ; + } + } + } + out: +return effective; +} +/* shadow_track_dirty_vram() +* This is the API called by the guest to determine which pages in the range
Quoted 3 lines
+ * from [begin_pfn:begin_pfn+nr) have been dirtied since the last call.
+ * It creates the domain'sdv_dirty_vramon demand.
+ * It creates ranges on demand when some [begin_pfn:nr) is firstencountered.
Quoted 7 lines
+ * To collect the dirty bitmask it calls
shadow_scan_dirty_flags().
+ * It copies the dirty bitmask into guest storage.
+ */xen/arch/x86/hvm/dirty_vram.c new file +4
-0+int shadow_track_dirty_vram (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +XEN_GUEST_HANDLE_64 (uint8) guest_dirty_bitmap)xen/arch/x86/hvm/dirty_vram.c line 576
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+{ +int rc =0 ; +unsigned long end_pfn = begin_pfn + nr; +int flush_tlb =0 ; +dv_range_t *range; +struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (end_pfn < begin_pfn + || begin_pfn > p2m->max_mapped_pfn + || end_pfn >= p2m->max_mapped_pfn)I know you just copied this from the old definition but the limits seem wrong here -- I think it should be:
Quoted 1 line
if ( end_pfn < begin_pfn || end_pfn > p2m->max_mapped_pfn + 1 )
You're right. Done. And commented because it's pretty obscure.
Quoted 32 lines
Quoted 14 lines
xen/arch/x86/hvm/dirty_vram.c new file +12
-0+/* hap_clean_vram_tracking_range() +* For all the pages in the range specified by [begin_pfn,nr), +* note in the dirty bitmap any page that has been marked as read-write, +* which signifies that the page has been dirtied, and reset the page +* to ram_logdirty. +*/ +void hap_clean_vram_tracking_range (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +uint8_t *dirty_bitmap) +{ +int i;xen/arch/x86/hvm/dirty_vram.c
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+unsigned long pfn; +dv_dirty_vram_t *dirty_vram = d->arch.hvm_domain.dirty_vram; +dv_range_t *range; + +ASSERT (p2m_locked_by_me (p2m_get_hostp2m (d))); +ASSERT (paging_locked_by_me (d)); + +if ( !dirty_vram ) + { + gdprintk(XENLOG_DEBUG, "Should only be called while tracking dirtyvram.\n");
xen/arch/x86/hvm/dirty_vram.c line 750
Quoted 6 lines
xen/arch/x86/hvm/dirty_vram.c new file +6
-0+return ; + } + + range =dirty_vram_range_find (d, begin_pfn, nr); +if (!range) +return ;
Quoted 3 lines
Oughtn't we to return all 1s in the bitmap here? If the range isn't currently being tracked we should conservatively assume it's all dirty, right?
The callers will should ensure that the range exists. This is just a conservative test.
Anyway, it begs the question of whether a new range should be considered all dirty or all clean. Intuitively "all dirty" seems the right answer but in practice it works fine as written since the guest is busy updating the
whole frame buffer.
Quoted 40 lines
Quoted 15 lines
xen/arch/x86/hvm/dirty_vram.c new file +14
-0+ +/* set l1e entries of P2M table to be read-only. */ +/* On first write, it page faults, its entry is changed to read-write, +* its bit in the dirty bitmap is set, and on retry the write succeeds. */ +for (i =0 , pfn = range->begin_pfn; pfn < range->end_pfn; i++, pfn++) + { +p2m_type_t pt; + pt =p2m_change_type (d, pfn, p2m_ram_rw, p2m_ram_logdirty); +if (pt == p2m_ram_rw) + dirty_bitmap[i >>3 ] |= (1 << (i &7 )); + } +flush_tlb_mask (d->domain_dirty_cpumask); +} +xen/arch/x86/hvm/dirty_vram.c
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+static void hap_vram_tracking_init (struct domain *d) +{ +paging_log_dirty_init (d, hap_enable_vram_tracking, + hap_disable_vram_tracking, +NULL ); +} + +/* hap_track_dirty_vram() +* Create the domain's dv_dirty_vram struct on demand. +* Create a dirty vram range on demand when some [begin_pfn:begin_pfn+nr] is first encountered.
Quoted 14 lines
+ * Collect the
guest_dirtybitmask, a bit mask of the dirties vram pages, by
+ * callingpaging_log_dirty_range().
+ */xen/arch/x86/hvm/dirty_vram.c new file +11
-0+int hap_track_dirty_vram (struct domain *d, +unsigned long begin_pfn, +unsigned long nr, +XEN_GUEST_HANDLE_64 (uint8) guest_dirty_bitmap) +{ +long rc =0 ; +dv_dirty_vram_t *dirty_vram; +int restart_log_dirty =0 ; + +paging_lock (d); + dirty_vram = d->arch.hvm_domain.dirty_vram;
Quoted 8 lines
xen/arch/x86/hvm/dirty_vram.c line 793
Quoted 5 lines
xen/arch/x86/hvm/dirty_vram.c new file +5
-0+if ( nr ) + { +dv_range_t *range =NULL ; +int size = (nr +BITS_PER_LONG -1 ) /BITS_PER_LONG ; +unsigned long dirty_bitmap[size];All the users of this array cast to (
uint8_t*) -- just declare it asuint8_t* instead?
Yes, done. The original code seemed overly fond of using LONGs ...
Quoted 38 lines
Quoted 38 lines
xen/arch/x86/hvm/dirty_vram.c new file +37
-0+ +/* Already tracking dirty vram? */ +if (paging_mode_log_dirty (d) && dirty_vram )/* yes */ + { +/* Handle the addition of another range */ + range =dirty_vram_range_find (d, begin_pfn, nr); +if ( !range ) + { + rc = -ENOMEM ; +if ( !(range =dirty_vram_range_alloc (d, begin_pfn, nr)) ) + goto param_fail; + restart_log_dirty =1 ; + } + } +/* Just starting to track dirty vram? */ +else if ( !paging_mode_log_dirty (d) && !dirty_vram )/* yes */ + { + rc = -ENOMEM ; +if ( !(dirty_vram =dirty_vram_alloc (d)) ) + goto param_fail; + +if ( !(range =dirty_vram_range_find_or_alloc (d, begin_pfn, nr)) ) + goto param_fail; + + restart_log_dirty =1 ; +/* Initialize callbacks for vram tracking */ +hap_vram_tracking_init (d); + } +else + { +/* Test for invalid combination */ +if ( !paging_mode_log_dirty (d) && dirty_vram ) + rc = -EINVAL ; +else /* logging dirty of all memory, not tracking dirty vram */ + rc = -ENODATA ; + goto param_fail; + }
Quoted 8 lines
xen/arch/x86/hvm/dirty_vram.c line 834
Quoted 4 lines
xen/arch/x86/hvm/dirty_vram.c new file +4
-0+ +if (restart_log_dirty) + { +/* disable then enable log dirty */ Why disable and re-enable? The call to
paging_log_dirty_range()below will reset the p2m entries of the range you care about, so I think all you need to do is enable it in the 'just starting' case above.
Done.
Quoted 5 lines
And, since you know you're in HAP mode, not in log-dirty mode, and already have the paging lock, you can just set
d->arch.paging.mode|=PG_log_dirtythere rather than jumping through thepaging_log_dirty_enable()path and messing with locks.
No, paging_log_dirty_enable() goes through several layers of functions and ends up calling hap_enable_vram_tracking(), which does quite a bit of stuff.
Quoted 27 lines
Quoted 27 lines
xen/arch/x86/hvm/dirty_vram.c new file +26
-0+paging_unlock (d); +if (paging_mode_log_dirty (d)) +paging_log_dirty_disable (d); + + rc =paging_log_dirty_enable (d); +paging_lock (d); +if (rc !=0 ) + goto param_fail; + } + +paging_unlock (d); +memset (dirty_bitmap,0x00 , size *BYTES_PER_LONG ); +paging_log_dirty_range (d, begin_pfn, nr, (uint8_t *)dirty_bitmap); + rc = -EFAULT ; +if (copy_to_guest (guest_dirty_bitmap, + (uint8_t *)dirty_bitmap, + size *BYTES_PER_LONG ) ==0 ) + { + rc =0 ; + } + } +else + { +/* If zero pages specified while already tracking dirty vram +* then stop tracking */ +if (paging_mode_log_dirty (d) && dirty_vram ) {
Quoted 8 lines
xen/arch/x86/hvm/dirty_vram.c line 864
Quoted 4 lines
xen/arch/x86/hvm/dirty_vram.c new file +4
-0+paging_unlock (d); + rc =paging_log_dirty_disable (d); +paging_lock (d); +dirty_vram_free (d);This is different from the shadow case -- there, IIUC, you just ignore requests where nr == 0; here, you tear down all vram tracking. Can you choose one of those, and document it in the public header?
Done. I changed the shadow code to tear down if nr == 0. (The HAP code seemed to expect that behavior and the shadow code didn't seem to care.)
And updated the public header.
Quoted 15 lines
Quoted 5 lines
xen/arch/x86/hvm/dirty_vram.c new file +4
-0+ }else /* benign no-op */ + { + rc =0 ; + }xen/arch/x86/hvm/dirty_vram.c
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +8
-0+paging_unlock (d); + } + +return rc; +/* paging_mark_dirty_hap() +* Make a hap page writeable and mark it as dirty. +* This done atomically under the p2m and paging locks to avoid leaving +* a window where the page might be modified without being marked as
Quoted 8 lines
dirty.
Quoted 1 line
+ */
I'm perplexed by this -- AFAICT it's either not necessary (because all log-dirty read/clean ops are done with the domain paused) or not sufficient (because although the bitmap and the PTE are updated under the p2m lock, the actual dirtying of the page happens at some other time). Can you spell out for me exactly what this is protecting against?
The comment over-stated the problem so I've toned it down from "without being marked" to "without being counted".
paging_mark_dirty_hap() is in the page fault path and has two steps:
(1) It calls p2m_change_type() to re-mark some pfn as writeable (i.e. p2m_ram_rw), which is a no-op if the pfn is already writeable.
This must be done under the p2m_lock.
(2) If the pfn was previously read-only (i.e. p2m_ram_logdirty) then it bumps two dirty counts. And it marks the page as dirty.
This must be done under the paging lock.
As an invariant, the dirty counts should be precisely the number of pages made writeable.
With this patch, step (2) is also done under the p2m_lock. This avoids having a window between steps (1) and (2), in which hap_track_dirty_vram() might get in and would break the invariant by clearing the range's dirty count.
It may be that this invariant is not particularly useful, that things will just work out. But I do wonder if, without it, we'll have situations where the dirty counts will indicate dirty pages when there are none, or no dirty pages when there are some.
Quoted 23 lines
Quoted 5 lines
xen/arch/x86/mm/shadow/common.c +4
-0+typedef int (*hash_pfn_callback_t)(struct vcpu *v, + mfn_t smfn, + unsigned long begin_pfn, + unsigned long end_pfn,xen/arch/x86/mm/shadow/common.c void shadow_clean_dirty_bitmap(struct domain *d)
Quoted 10 lines
xen/arch/x86/mm/shadow/common.c +10
-0+ int *removed); + +static int hash_pfn_foreach (struct vcpu *v, +unsigned int callback_mask, +hash_pfn_callback_t callbacks[], +unsigned long begin_pfn, +unsigned long end_pfn) +/* Walk the hash table looking at the types of the entries and +* calling the appropriate callback function for each entry. +* The mask determines which shadow types we call back for, and the array
Quoted 4 lines
+ * of callbacks tells us which function to call.
+ * Any callback may return non-zero to let us skip the rest of the scan.
+ *
+ * WARNING: Callbacks MUST NOT add or remove hash entries unlessthey
Quoted 1 line
+ * then return non-zero to terminate the scan. */
Quoted 8 lines
This code duplication is a bit much. I think you should recast the existing
hash_foreach()function to take a pointer as its fourth argument instead of an MFN, and then make the existing callers just cast
their MFN argument as a pointer. The you can use the same function,
passing a pointer to a struct { begin, end, removed }.Please make the changes to
hash_foreach()in a separate patch from thedirty_vramstuff.
Yes, I'd be happy to submit a patch as you suggest after we're done with this one.
Quoted 12 lines
Quoted 12 lines
xen/arch/x86/mm/shadow/multi.c +1
-1@@ -1211,12 +1164,14 @@ static int shadow_set_l1e(struct vcpu *v,shadow_l1e_remove_flags (new_sl1e, _PAGE_RW);/* fall through */ case 0 : -shadow_vram_get_l1e (new_sl1e, sl1e, sl1mfn, d); +shadow_vram_fix_l1e (old_sl1e, new_sl1e, sl1e, sl1mfn, d);break ; } } }
Quoted 3 lines
Review static int shadow_set_l1e(struct vcpu *v, · line 1173
Quoted 1 line
(quoted diff) +1
-0+ shadow_vram_fix_l1e(old_sl1e, new_sl1e, sl1e, sl1mfn, d);Why is this being called twice here?
Stupidity. I've eliminated the first of the two.
Quoted 4 lines
Cheers,
Tim.
Thanks again.
-- rsp
Hi,
Thanks for the update patch.
Quoted 8 lines · Robert Phillips
Quoted 4 lines
More substantive comments:
- I think thedirty_vram.canddirty_vram.hfiles belong undermm/rather than under hvm/. The ``#include "../mm/mm-locks.h"'' is an indicator that this is really MM code.I have moved
dirty_vram.cundermm/
I have moveddirty_vram.hunderinclude/asm-x86. In that location it is available to modules likehvm.c
Sure, that seems sgood.
Quoted 6 lines
Quoted 5 lines
I'd be
inclined to just put allpaddr_linksin the linked list (and have an array of pointers rather than an array ofpaddr_link_ts). Is it worth having the extra complexity here, and at the callers, to avoid a single memory read?
Quoted 7 lines
Almost all frame buffer pages have a single mapping so the algorithm's common case is met by constructing
pl_tabas an array ofpaddr_links. That is, the table'spaddr_linkswill rarely point to a chain of extensionpaddr_links. The code encapsulates the complexity in a single function. Sorry it required so much staring. Even if allpaddr_linkswere stored in a linked list, the caller would have to walk that linked list, so would be just as complex.
OK.
Quoted 16 lines
Quoted 11 lines
xen/arch/x86/hvm/dirty_vram.c line 438
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+if (set) + { +/* Did we find sl1ma in either the main table or the linked list? */ +if (pl ==NULL )/* no, so we'll need to alloc a link */ + { +ASSERT (ppl !=NULL ); +/* alloc link and append it to list */ + (*ppl) = pl =alloc_paddr_link (d); +if (pl ==NULL ) + goto out;Quoted 2 lines
This needs to signal some sort of error. Otherwise, if we can't add this sl1e to the list we'll just silently fail to track it.
I don't know what sort of error we can signal.
Quoted 9 lines
The immediate symptom would be that areas of the monitor would not be refreshed. But, since we're running out of memory, that might be the least of the quests's woes. The updated patch actually makes the symptom more likely (though still very unlikely) by putting an arbitrary bound on the length of
paddr_linkchains. It handles a rogue process, one that has an arbitrarily large number of mappings for frame buffer pages, by simply not recording the excessive mappings. If that results in unrefreshed blocks on the monitor, so be it.
I think the correct behaviour would be to report these pages as dirty. That way anything that relies on seeing all changes will behave correctly, though less efficiently.
Quoted 12 lines
Quoted 11 lines
xen/arch/x86/hvm/dirty_vram.c line 472
Quoted 5 lines
xen/arch/x86/hvm/dirty_vram.c new file +5
-0+#endif +free_paddr_link (d, ppl, pl); +if ( --range->nr_mappings ==0 ) + { +dirty_vram_range_free (d, range);What's this for? If the guest unmaps the framebuffer and remaps it (or if the shadow PTs of the mappings are temporarily discarded) this will stop us from tracking the new mappings until the toolstack asks for the bitmap (and then it will be expensive to go and find the mappings).
Quoted 4 lines
I don't see this happening. If the guest unmaps the framebuffer, the shadow code lazily recovers the shadow pages, so tracking will continue until it decides a page is no longer a shadow page. That is when this code is invoked.
If the guest unmaps the buffer by clearing the PTEs then when the PTE changes are propagated into the shadows this will cause nr_mappings to fall to zero here. If we _don't_ free the range, then when the FB is mapped again we'll spot the new PTEs as they're shadowed and DTRT. If we _do_ free the range we end up walking all shadows looking for the mappings.
Quoted 3 lines
It tears down the mappings to that page and if some range ends up with no mappings then the range is useless. Vram ranges are generated willy-nilly as needed. This is the only mechanism for cleaning them up.
Shouldn't there be some way for the tools to indicate that they're done with a range? I guess they can tear down the whole lot and then start again with whatever ranges are still in use.
Quoted 6 lines
Quoted 3 lines
Oughtn't we to return all 1s in the bitmap here? If the range isn't currently being tracked we should conservatively assume it's all dirty, right?
The callers will should ensure that the range exists. This is just a conservative test.
OK.
Quoted 4 lines
Anyway, it begs the question of whether a new range should be considered all dirty or all clean. Intuitively "all dirty" seems the right answer but in practice it works fine as written since the guest is busy updating the whole frame buffer.
What if the guest's not writing to the framebuffer at the moment, or only to a part of it? Shouldn't the tools see the existing content?
Quoted 7 lines
Quoted 4 lines
And, since you know you're in HAP mode, not in log-dirty mode, and already have the paging lock, you can just set
d->arch.paging.mode|=PG_log_dirtythere rather than jumping through thepaging_log_dirty_enable()path and messing with locks.No,
paging_log_dirty_enable()goes through several layers of functions and ends up callinghap_enable_vram_tracking(), which does quite a bit of stuff.
OK.
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
+/*
paging_mark_dirty_hap()
+ * Make a hap page writeable and mark it as dirty.
+ * This done atomically under the p2m and paging locks to avoid leaving
+ * a window where the page might be modified without being marked asQuoted 8 lines
dirty.
Quoted 1 line
+ */
I'm perplexed by this -- AFAICT it's either not necessary (because all log-dirty read/clean ops are done with the domain paused) or not sufficient (because although the bitmap and the PTE are updated under the p2m lock, the actual dirtying of the page happens at some other time). Can you spell out for me exactly what this is protecting against?
The comment over-stated the problem so I've toned it down from "without being marked" to "without being counted".
paging_mark_dirty_hap()is in the page fault path and has two steps:(1) It calls
p2m_change_type()to re-mark some pfn as writeable (i.e.p2m_ram_rw), which is a no-op if the pfn is already writeable. This must be done under thep2m_lock.
Quoted 6 lines
(2) If the pfn was previously read-only (i.e.
p2m_ram_logdirty) then it bumps two dirty counts. And it marks the page as dirty. This must be done under the paging lock.As an invariant, the dirty counts should be precisely the number of pages made writeable.
That invariant is already doomed -- any other agent (including the guest itself) can change the p2m mappings for one of the pfns, or mark the pfn dirty, without going through the NPF fault handler. AFAICS the counts could just as well be implemented as flags, to say 'something in this range was dirtied'; the only interesting case is when they're 0.
Quoted 4 lines
It may be that this invariant is not particularly useful, that things will just work out. But I do wonder if, without it, we'll have situations where the dirty counts will indicate dirty pages when there are none, or no dirty pages when there are some.
The second of those cases would be the bad one; I think that can be avoided by just switching the order of paging_mark_dirty() and p2m_change_type() in hvm_hap_nested_page_fault(), and maybe putting a wmb() between them.
Cheers,
Tim.
Hi,
I'm sending version 3 of the patch shortly. I think it addresses all your concerns.
Quoted 66 lines
-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Thursday, November 08, 2012 8:25 AM
To: Robert Phillips
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH] Provide support for multiple frame buffers in Xen.Hi,
Thanks for the update patch.
Quoted 8 lines · Robert Phillips
Quoted 4 lines
More substantive comments:
- I think thedirty_vram.canddirty_vram.hfiles belong undermm/rather than under hvm/. The ``#include "../mm/mm-locks.h"'' is an indicator that this is really MM code.I have moved
dirty_vram.cundermm/
I have moveddirty_vram.hunderinclude/asm-x86. In that location it is available to modules likehvm.cSure, that seems sgood.
Quoted 6 lines
Quoted 5 lines
I'd be
inclined to just put allpaddr_linksin the linked list (and have an array of pointers rather than an array ofpaddr_link_ts). Is it worth having the extra complexity here, and at the callers, to avoid a single memory read?Quoted 7 lines
Almost all frame buffer pages have a single mapping so the algorithm's common case is met by constructing
pl_tabas an array ofpaddr_links. That is, the table'spaddr_linkswill rarely point to a chain of extensionpaddr_links. The code encapsulates the complexity in a single function. Sorry it required so much staring. Even if allpaddr_linkswere stored in a linked list, the caller would have to walk that linked list, so would be just as complex.OK.
Quoted 16 lines
Quoted 11 lines
xen/arch/x86/hvm/dirty_vram.c line 438
Quoted 10 lines
xen/arch/x86/hvm/dirty_vram.c new file +10
-0+if (set) + { +/* Did we find sl1ma in either the main table or the linked list? */ +if (pl ==NULL )/* no, so we'll need to alloc a link */ + { +ASSERT (ppl !=NULL ); +/* alloc link and append it to list */ + (*ppl) = pl =alloc_paddr_link (d); +if (pl ==NULL ) + goto out;Quoted 2 lines
This needs to signal some sort of error. Otherwise, if we can't add this sl1e to the list we'll just silently fail to track it.
I don't know what sort of error we can signal.
Quoted 9 lines
The immediate symptom would be that areas of the monitor would not be refreshed. But, since we're running out of memory, that might be the least of the quests's woes. The updated patch actually makes the symptom more likely (though still very unlikely) by putting an arbitrary bound on the length of
paddr_linkchains. It handles a rogue process, one that has an arbitrarily large number of mappings for frame buffer pages, by simply not recording the excessive mappings. If that results in unrefreshed blocks on the monitor, so be it.
Quoted 3 lines
I think the correct behaviour would be to report these pages as dirty. That way anything that relies on seeing all changes will behave correctly, though less efficiently.
Ok, I've associated a boolean called "stuck_dirty" with each frame buffer page. If for any reason we're unable to generated a complete set of mappings, the bit gets set and that frame buffer page is considered dirty forever more, or until the range gets torn down.
I was unable to make a failure happen (not surprisingly) so I added some fault injection code for testing. It's currently disabled/compiled-out.
Quoted 18 lines
Quoted 13 lines
Quoted 7 lines
xen/arch/x86/hvm/dirty_vram.c line 472
Quoted 6 lines
xen/arch/x86/hvm/dirty_vram.c new file +5
-0+#endif +free_paddr_link (d, ppl, pl); +if ( --range->nr_mappings ==0 ) + { +dirty_vram_range_free (d, range);Quoted 5 lines
What's this for? If the guest unmaps the framebuffer and remaps it (or if the shadow PTs of the mappings are temporarily discarded) this will stop us from tracking the new mappings until the toolstack asks for the bitmap (and then it will be expensive to go and find the mappings).
Quoted 4 lines
I don't see this happening. If the guest unmaps the framebuffer, the shadow code lazily recovers the shadow pages, so tracking will continue until it decides a page is no longer a shadow page. That is when this code is invoked.
Quoted 7 lines
If the guest unmaps the buffer by clearing the PTEs then when the PTE changes are propagated into the shadows this will cause
nr_mappingsto fall to zero here. If we _don't_ free the range, then when the FB is mapped again we'll spot the new PTEs as they're shadowed and DTRT. If we _do_ free the range we end up walking all shadows looking for the mappings.
Ok, I've removed the teardown code. Ranges are no longer torn down just because they have no mappings. But they can still be torn down if another mapping is created, one that overlaps, or if dirty vram bookkeeping is stopped altogether, which happens as a side-effect of requesting a range with zero pages.
Quoted 7 lines
Quoted 3 lines
It tears down the mappings to that page and if some range ends up with no mappings then the range is useless. Vram ranges are generated willy-nilly as needed. This is the only mechanism for cleaning them up.
Shouldn't there be some way for the tools to indicate that they're done with a range? I guess they can tear down the whole lot and then start again with whatever ranges are still in use.
Right. Like little children, they do not clean up after themselves.
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Oughtn't we to return all 1s in the bitmap here? If the range isn't currently being tracked we should conservatively assume it's all dirty, right?
The callers will should ensure that the range exists. This is just a conservative test.
OK.
Quoted 7 lines
Quoted 4 lines
Anyway, it begs the question of whether a new range should be considered all dirty or all clean. Intuitively "all dirty" seems the right answer but in practice it works fine as written since the guest is busy updating the whole frame buffer.
What if the guest's not writing to the framebuffer at the moment, or only to a part of it? Shouldn't the tools see the existing content?
Ok, in HAP a new range is considered to be "all dirty".
Quoted 56 lines
Quoted 8 lines
Quoted 5 lines
And, since you know you're in HAP mode, not in log-dirty mode, and already have the paging lock, you can just set
d->arch.paging.mode|=PG_log_dirtythere rather than jumping through thepaging_log_dirty_enable()path and messing with locks.No,
paging_log_dirty_enable()goes through several layers of functions and ends up callinghap_enable_vram_tracking(), which does quite a bit ofstuff.
OK.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+/*
paging_mark_dirty_hap()
+ * Make a hap page writeable and mark it as dirty.
+ * This done atomically under the p2m and paging locks to avoidleaving
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ * a window where the page might be modified without being marked
as
Quoted 5 lines
Quoted 5 lines
dirty.
Quoted 1 line
+ */
I'm perplexed by this -- AFAICT it's either not necessary (because all log-dirty read/clean ops are done with the domain paused) or not
sufficient
Quoted 1 line
Quoted 1 line
(because although the bitmap and the PTE are updated under the p2m
lock,
Quoted 12 lines
Quoted 2 lines
the actual dirtying of the page happens at some other time). Can you spell out for me exactly what this is protecting against?
The comment over-stated the problem so I've toned it down from "without being marked" to "without being counted".
paging_mark_dirty_hap()is in the page fault path and has two steps:(1) It calls
p2m_change_type()to re-mark some pfn as writeable (i.e.p2m_ram_rw), which is a no-op if the pfn is already writeable. This must be done under thep2m_lock.Quoted 6 lines
(2) If the pfn was previously read-only (i.e.
p2m_ram_logdirty) then it bumps two dirty counts. And it marks the page as dirty. This must be done under the paging lock.As an invariant, the dirty counts should be precisely the number of pages made writeable.
That invariant is already doomed -- any other agent (including the guest itself) can change the p2m mappings for one of the pfns, or mark the pfn dirty, without going through the NPF fault handler. AFAICS the counts could just as well be implemented as flags, to say 'something in this range was dirtied'; the only interesting case is when they're 0.
Quoted 4 lines
It may be that this invariant is not particularly useful, that things will just work out. But I do wonder if, without it, we'll have situations where the dirty counts will indicate dirty pages when there are none, or no dirty pages when there are some.
Quoted 4 lines
The second of those cases would be the bad one; I think that can be avoided by just switching the order of
paging_mark_dirty()andp2m_change_type()inhvm_hap_nested_page_fault(), and maybe putting awmb()between them.
Ok, I've done as you suggested. I don't think the wmb() is needed but I could be convinced.
Quoted 4 lines
Cheers,
Tim.