pvh/dom0/shadow/amd fixes
v3 cover pvh/dom0/shadow/amd fixes
Hello,
The following series contains fixes that should be considered for 4.12.
I'm not sure whether patches 5, 6, 7 and 8 should be aimed at 4.12, they contain changes to the p2m code that could affect HVM guests. Note that without those changes a PVH dom0 running on AMD hardware will be unable to create guests. Overall the patches are a nice cleanup to the handling of p2m_ioreq_server and p2m_map_foreign types IMO.
The series can also be found at:
git://xenbits.xen.org/people/royger/xen.git fixes-4.12-v3
Roger Pau Monne (8):
dom0/pvh: align allocation and mapping order to start address x86/pvh: reorder PVH dom0 iommu initialization amd/npt/shadow: replace assert that prevents creating 2M/1G MMIO
entries
pvh/dom0: warn when dom0_mem is not set
x86/mm: split p2m ioreq server pages special handling into helper
p2m: change write_p2m_entry to return an error code x86/mm: handle foreign mappings in p2m_entry_modify npt/shadow: allow getting foreign page table entries
xen/arch/x86/dom0_build.c | 10 +++ xen/arch/x86/hvm/dom0_build.c | 37 ++++++---- xen/arch/x86/mm/hap/hap.c | 15 +++- xen/arch/x86/mm/hap/nested_hap.c | 4 +- xen/arch/x86/mm/p2m-ept.c | 107 ++++++---------------------- xen/arch/x86/mm/p2m-pt.c | 88 ++++++++++------------- xen/arch/x86/mm/paging.c | 12 ++-- xen/arch/x86/mm/shadow/common.c | 8 ++- xen/arch/x86/mm/shadow/none.c | 7 +- xen/arch/x86/mm/shadow/private.h | 6 +- xen/drivers/passthrough/x86/iommu.c | 7 +- xen/include/asm-x86/p2m.h | 54 +++++++++++++- xen/include/asm-x86/paging.h | 8 +-- 13 files changed, 191 insertions(+), 172 deletions(-)
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 1/8 dom0/pvh: align allocation and mapping order to start address
The p2m and iommu mapping code always had the requirement that addresses and orders must be aligned when populating the p2m or the iommu page tables.
PVH dom0 builder didn't take this requirement into account, and can call into the p2m/iommu mapping helpers with addresses and orders that are not aligned.
Fix this by making sure the orders passed to the physmap population helpers are always aligned to the guest address to be populated.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
---
Without this patch trying to create a PVH dom0 will trigger an assert on certain hardware depending on the memory map.
---
Changes since v1:
- Reword commit message.
--- xen/arch/x86/hvm/dom0_build.c | 2 ++ 1 file changed, 2 insertions(+)
xen/arch/x86/hvm/dom0_build.c +2 -0
@@ -152,6 +152,8 @@ static int __init pvh_populate_memory_range(struct domain *d, order =get_order_from_pages (end - start +1 ); order =min (order ? order -1 :0 , max_order); +/* The order allocated and populated must be aligned to the address. */ + order =min (order, start ?find_first_set_bit (start) :MAX_ORDER ); page =alloc_domheap_pages (d, order, dom0_memflags | MEMF_no_scrub);if ( page ==NULL ) {
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 1/8] dom0/pvh: align allocation and mapping order to start address
Quoted 8 lines · Roger Pau Monne
The p2m and iommu mapping code always had the requirement that addresses and orders must be aligned when populating the p2m or the iommu page tables.
PVH dom0 builder didn't take this requirement into account, and can call into the p2m/iommu mapping helpers with addresses and orders that are not aligned.
Quoted 6 lines
Fix this by making sure the orders passed to the physmap population helpers are always aligned to the guest address to be populated.
Signed-off-by Roger Pau Monné <[email protected]>Reviewed-by Wei Liu <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 2/8 x86/pvh: reorder PVH dom0 iommu initialization
In reply to Roger Pau MonneSo that the iommu is initialized before populating the p2m, and entries added get the corresponding iommu page table entries if required. This requires splitting the current pvh_setup_p2m into two different functions. One that crafts dom0 physmap and sets the paging allocation, and another one that actually populates the p2m with RAM regions.
Note that this allows to remove the special casing done for the low 1MB in hwdom_iommu_map.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v1:
- New in this version.
---
The previous flow, where the iommu was enabled after having populated the p2m was used to workaround an issue on an old pre-Haswell Intel box. I no longer have that box, and when I tested PVH dom0 on it was before the iommu map-reserved fixes.
If hardware needing the iommu page tables to contain RAM regions is found I'm happy to work on other solutions, like performing the setup of the iommu at the start of dom0_construct_pvh but only enabling it when dom0 p2m is fully populated.
--- xen/arch/x86/hvm/dom0_build.c | 35 +++++++++++++++++++---------- xen/drivers/passthrough/x86/iommu.c | 7 +----- 2 files changed, 24 insertions(+), 18 deletions(-)
xen/arch/x86/hvm/dom0_build.c +23 -12
@@ -409,14 +409,10 @@ static __init void pvh_setup_e820(struct domain *d, unsigned long nr_pages)ASSERT (cur_pages == nr_pages); } -static int __init pvh_setup_p2m (struct domain *d) +static void __init pvh_init_p2m (struct domain *d) { -struct vcpu *v = d->vcpu[0 ];unsigned long nr_pages =dom0_compute_nr_pages (d,NULL ,0 ); -unsigned int i; -int rc;bool preempted; -#define MB1_PAGES PFN_DOWN(MB(1))pvh_setup_e820 (d, nr_pages); do {
@@ -425,6 +421,14 @@ static int __init pvh_setup_p2m(struct domain *d) &preempted);process_pending_softirqs (); }while ( preempted ); +} + +static int __init pvh_populate_p2m (struct domain *d) +{ +struct vcpu *v = d->vcpu[0 ]; +unsigned int i; +int rc; +#define MB1_PAGES PFN_DOWN(MB(1))/* * Memory below 1MB is identity mapped initially. RAM regions are
@@ -1134,13 +1138,6 @@ int __init dom0_construct_pvh(struct domain *d, const module_t *image,printk (XENLOG_INFO "*** Building a PVH Dom%d ***\n" , d->domain_id); - rc =pvh_setup_p2m (d); -if ( rc ) - { -printk ("Failed to setup Dom0 physical memory map\n" ); -return rc; - } -/* * NB: MMCFG initialization needs to be performed before iommu * initialization so the iommu code can fetch the MMCFG regions used by the
@@ -1148,8 +1145,22 @@ int __init dom0_construct_pvh(struct domain *d, const module_t *image,*/ pvh_setup_mmcfg (d); +/* +* Craft dom0 physical memory map and set the paging allocation. This must +* be done before the iommu initializion, since iommu initialization code +* will likely add mappings required by devices to the p2m (ie: RMRRs). +*/ +pvh_init_p2m (d); +iommu_hwdom_init (d); + rc =pvh_populate_p2m (d); +if ( rc ) + { +printk ("Failed to setup Dom0 physical memory map\n" ); +return rc; + } + rc =pvh_load_kernel (d, image, image_headroom, initrd,bootstrap_map (image), cmdline, &entry, &start_info);if ( rc )
xen/drivers/passthrough/x86/iommu.c +1 -6
@@ -151,12 +151,7 @@ static bool __hwdom_init hwdom_iommu_map(const struct domain *d,* inclusive mapping additionally maps in every pfn up to 4GB except those * that fall in unusable ranges for PV Dom0. */ -if ( (pfn > max_pfn && !mfn_valid (mfn)) ||xen_in_range (pfn) || -/* -* Ignore any address below 1MB, that's already identity mapped by the -* Dom0 builder for HVM. -*/ - (!d->domain_id &&is_hvm_domain (d) && pfn <PFN_DOWN (MB (1 ))) ) +if ( (pfn > max_pfn && !mfn_valid (mfn)) ||xen_in_range (pfn) )return false; switch ( type = page_get_ram_type(mfn) )
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 2/8] x86/pvh: reorder PVH dom0 iommu initialization
Quoted 7 lines · Roger Pau Monne
So that the iommu is initialized before populating the p2m, and entries added get the corresponding iommu page table entries if required. This requires splitting the current
pvh_setup_p2minto two different functions. One that crafts dom0 physmap and sets the paging allocation, and another one that actually populates the p2m with RAM regions.
Quoted 5 lines
Note that this allows to remove the special casing done for the low 1MB in
hwdom_iommu_map.Signed-off-by Roger Pau Monné <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 3/8 amd/npt/shadow: replace assert that prevents creating 2M/1G MMIO entries
In reply to Roger Pau MonneThe assert was originally added to make sure that higher order regions (> PAGE_ORDER_4K) could not be used to bypass the mmio_ro_ranges check performed by p2m_type_to_flags.
This however is already checked in set_mmio_p2m_entry, which makes sure that higher order mappings don't overlap with mmio_ro_ranges, thus allowing the creation of high order MMIO mappings safely.
Replace the assert to allow 2M/1G entries to be created for MMIO regions and add some extra asserts as a replacement to make sure there's no overlapping with MMIO read-only ranges.
Note that 1G MMIO entries will not be created unless mmio_order is changed to allow it.
---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Juergen Gross <[email protected]>
---
Without this patch trying to create a PVH dom0 will trigger an assert on certain hardware depending on the memory map.
---
Changes since v2:
- Unify checks into a helper function.
Changes since v1:
- Fix subject.
- Replace the assert with a suitable one.
--- xen/arch/x86/mm/p2m-pt.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
xen/arch/x86/mm/p2m-pt.c +19 -4
@@ -479,6 +479,23 @@ int p2m_pt_handle_deferred_changes(uint64_t gpa)return rc; } +/* Checks only applicable to entries with order > PAGE_ORDER_4K */ +static void check_entry (mfn_t mfn,p2m_type_t new,p2m_type_t old, +unsigned int order) +{ +ASSERT (order >PAGE_ORDER_4K ); +ASSERT (old != p2m_ioreq_server); +if ( new == p2m_mmio_direct ) +ASSERT (!mfn_eq (mfn,INVALID_MFN ) && + !rangeset_overlaps_range (mmio_ro_ranges,mfn_x (mfn), +mfn_x (mfn) + (1ul << order))); +else if (p2m_allows_invalid_mfn (new) || new == p2m_invalid || + new == p2m_mmio_dm ) +ASSERT (mfn_valid (mfn) ||mfn_eq (mfn,INVALID_MFN )); +else +ASSERT (mfn_valid (mfn)); +} +/* Returns: 0 for success, -errno for failure */ static int p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,
@@ -575,8 +592,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, } } -ASSERT (p2m_flags_to_type (flags) != p2m_ioreq_server); -ASSERT (!mfn_valid (mfn) || p2mt != p2m_mmio_direct); +check_entry (mfn, p2mt,p2m_flags_to_type (flags), page_order); l3e_content =mfn_valid (mfn) ||p2m_allows_invalid_mfn (p2mt) ? p2m_l3e_from_pfn(mfn_x(mfn), p2m_type_to_flags(p2m, p2mt, mfn, 2))
@@ -667,8 +683,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, } } -ASSERT (p2m_flags_to_type (flags) != p2m_ioreq_server); -ASSERT (!mfn_valid (mfn) || p2mt != p2m_mmio_direct); +check_entry (mfn, p2mt,p2m_flags_to_type (flags), page_order); l2e_content =mfn_valid (mfn) ||p2m_allows_invalid_mfn (p2mt) ? p2m_l2e_from_pfn(mfn_x(mfn), p2m_type_to_flags(p2m, p2mt, mfn, 1))
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 3/8] amd/npt/shadow: replace assert that prevents creating 2M/1G MMIO entries
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 15.02.19 at 15:18, <[email protected]> wrote:
The assert was originally added to make sure that higher order regions (>
PAGE_ORDER_4K) could not be used to bypass themmio_ro_rangescheck performed byp2m_type_to_flags.This however is already checked in
set_mmio_p2m_entry, which makes sure that higher order mappings don't overlap withmmio_ro_ranges, thus allowing the creation of high order MMIO mappings safely.
Quoted 8 lines
Replace the assert to allow 2M/1G entries to be created for MMIO regions and add some extra asserts as a replacement to make sure there's no overlapping with MMIO read-only ranges.
Note that 1G MMIO entries will not be created unless
mmio_orderis changed to allow it.Signed-off-by Roger Pau Monné <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 3/8] amd/npt/shadow: replace assert that prevents creating 2M/1G MMIO entries
In reply to Roger Pau MonneQuoted 12 lines · Roger Pau Monne
The assert was originally added to make sure that higher order regions (>
PAGE_ORDER_4K) could not be used to bypass themmio_ro_rangescheck performed byp2m_type_to_flags.This however is already checked in
set_mmio_p2m_entry, which makes sure that higher order mappings don't overlap withmmio_ro_ranges, thus allowing the creation of high order MMIO mappings safely.
Quoted 8 lines
Replace the assert to allow 2M/1G entries to be created for MMIO regions and add some extra asserts as a replacement to make sure there's no overlapping with MMIO read-only ranges.
Note that 1G MMIO entries will not be created unless
mmio_orderis changed to allow it.Signed-off-by Roger Pau Monné <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 3/8] amd/npt/shadow: replace assert that prevents creating 2M/1G MMIO entries
In reply to Roger Pau MonneQuoted 8 lines · Roger Pau Monne
The assert was originally added to make sure that higher order regions (>
PAGE_ORDER_4K) could not be used to bypass themmio_ro_rangescheck performed byp2m_type_to_flags.This however is already checked in
set_mmio_p2m_entry, which makes sure that higher order mappings don't overlap withmmio_ro_ranges, thus allowing the creation of high order MMIO mappings safely.
Quoted 8 lines
Replace the assert to allow 2M/1G entries to be created for MMIO regions and add some extra asserts as a replacement to make sure there's no overlapping with MMIO read-only ranges.
Note that 1G MMIO entries will not be created unless
mmio_orderis changed to allow it.Signed-off-by Roger Pau Monné <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 4/8 pvh/dom0: warn when dom0_mem is not set
In reply to Roger Pau MonneThere have been several reports of the dom0 builder running out of memory when building a PVH dom0 without having specified a dom0_mem value. Print a warning message if dom0_mem is not set when booting in PVH mode.
This is a temporary workaround until accounting for internal memory required by Xen (ie: paging structures) is improved.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Juergen Gross <[email protected]>
---
Without this patch creating a PVH dom0 without a dom0_mem parameter can result in the dom0 builder running out of memory thus leading to a Xen crash. The added message gives a hit to the user about a possible fix.
---
Changes since v2:
- Print message only once.
Changes since v1:
- Rewrite the warning message.
- Check nr_pages in order to figure out if the amount of dom0 memory has been set.
--- xen/arch/x86/dom0_build.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
xen/arch/x86/dom0_build.c +10 -0
@@ -378,8 +378,18 @@ unsigned long __init dom0_compute_nr_pages(* maximum of 128MB. */ if ( !nr_pages ) + { nr_pages = avail - (pv_shim ?pv_shim_mem (avail) :min (avail /16 ,128UL << (20 -PAGE_SHIFT ))); +if (is_hvm_domain (d) && !need_paging ) +/* +* Temporary workaround message until internal (paging) memory +* accounting required to build a pvh dom0 is improved. +*/ +printk ("WARNING: PVH dom0 without dom0_mem set is still unstable. " +"If you get crashes during boot, try adding a dom0_mem parameter\n" ); + } +/* Clamp according to min/max limits and available memory. */ nr_pages =max (nr_pages, min_pages);
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 4/8] pvh/dom0: warn when dom0_mem is not set
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.02.19 at 15:18, <[email protected]> wrote:
There have been several reports of the dom0 builder running out of memory when building a PVH dom0 without having specified a
dom0_memvalue. Print a warning message ifdom0_memis not set when booting in PVH mode.This is a temporary workaround until accounting for internal memory required by Xen (ie: paging structures) is improved.
Signed-off-by Roger Pau Monné <[email protected]>
Btw, it looks as if you did Cc Jürgen here and on patch 3, but not on the first two.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 4/8] pvh/dom0: warn when dom0_mem is not set
In reply to Roger Pau MonneQuoted 9 lines · Roger Pau Monne
There have been several reports of the dom0 builder running out of memory when building a PVH dom0 without having specified a
dom0_memvalue. Print a warning message ifdom0_memis not set when booting in PVH mode.This is a temporary workaround until accounting for internal memory required by Xen (ie: paging structures) is improved.
Signed-off-by Roger Pau Monné <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH for-4.12 v3 4/8] pvh/dom0: warn when dom0_mem is not set
In reply to Roger Pau MonneQuoted 9 lines · Roger Pau Monne
There have been several reports of the dom0 builder running out of memory when building a PVH dom0 without having specified a
dom0_memvalue. Print a warning message ifdom0_memis not set when booting in PVH mode.This is a temporary workaround until accounting for internal memory required by Xen (ie: paging structures) is improved.
Signed-off-by Roger Pau Monné <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 5/8 x86/mm: split p2m ioreq server pages special handling into helper
In reply to Roger Pau MonneSo that it can be shared by both ept, npt and shadow code, instead of duplicating it.
No change in functionality intended.
---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v1:
- Remove unused p2mt_old from p2m_pt_set_entry.
--- xen/arch/x86/mm/hap/hap.c | 3 ++ xen/arch/x86/mm/p2m-ept.c | 55 +++++++++++---------------------- xen/arch/x86/mm/p2m-pt.c | 24 -------------- xen/arch/x86/mm/shadow/common.c | 3 ++ xen/include/asm-x86/p2m.h | 32 +++++++++++++++++++ 5 files changed, 56 insertions(+), 61 deletions(-)
xen/arch/x86/mm/hap/hap.c +3 -0
@@ -734,6 +734,9 @@ hap_write_p2m_entry(struct domain *d, unsigned long gfn, l1_pgentry_t *p, && perms_strictly_increased(old_flags, l1e_get_flags(new)) ); } +p2m_entry_modify (p2m_get_hostp2m (d),p2m_flags_to_type (l1e_get_flags (new)), +p2m_flags_to_type (old_flags), level); +safe_write_pte (p, new);if ( old_flags & _PAGE_PRESENT )flush_tlb_mask (d->dirty_cpumask);
xen/arch/x86/mm/p2m-ept.c +18 -37
146 lines not on this page · raw message
xen/arch/x86/mm/p2m-pt.c +0 -24
@@ -436,13 +436,6 @@ static int do_recalc(struct p2m_domain *p2m, unsigned long gfn) flags |= _PAGE_PSE; } -if ( ot == p2m_ioreq_server ) - { -ASSERT (p2m->ioreq.entry_count >0 ); -ASSERT (level ==0 ); - p2m->ioreq.entry_count--; - } - e =l1e_from_pfn (mfn, flags);p2m_add_iommu_flags (&e, level, (nt == p2m_ram_rw)
@@ -616,8 +609,6 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,if ( page_order ==PAGE_ORDER_4K ) { -p2m_type_t p2mt_old; - rc =p2m_next_level (p2m, &table, &gfn_remainder, gfn,L2_PAGETABLE_SHIFT -PAGE_SHIFT ,L2_PAGETABLE_ENTRIES ,1 ,1 );
@@ -641,21 +632,6 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,if ( entry_content.l1 !=0 )p2m_add_iommu_flags (&entry_content,0 , iommu_pte_flags); - p2mt_old =p2m_flags_to_type (l1e_get_flags (*p2m_entry)); - -/* -* p2m_ioreq_server is only used for 4K pages, so -* the count is only done for level 1 entries. -*/ -if ( p2mt == p2m_ioreq_server ) - p2m->ioreq.entry_count++; - -if ( p2mt_old == p2m_ioreq_server ) - { -ASSERT (p2m->ioreq.entry_count >0 ); - p2m->ioreq.entry_count--; - } -/* level 1 entry */ p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,1 );/* NB: paging_write_p2m_entry() handles tlb flushes properly */
xen/arch/x86/mm/shadow/common.c +3 -0
@@ -3188,6 +3188,9 @@ shadow_write_p2m_entry(struct domain *d, unsigned long gfn,if (likely (d->arch.paging.shadow.total_pages !=0 ) )sh_unshadow_for_p2m_change (d, gfn, p, new, level); +p2m_entry_modify (p2m_get_hostp2m (d),p2m_flags_to_type (l1e_get_flags (new)), +p2m_flags_to_type (l1e_get_flags (*p)), level); +/* Update the entry with new content */ safe_write_pte (p, new);
xen/include/asm-x86/p2m.h +32 -0
@@ -932,6 +932,38 @@ int p2m_set_ioreq_server(struct domain *d, unsigned int flags,struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d,unsigned int *flags); +static inline void p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, +p2m_type_t ot,unsigned int level) +{ +if ( level !=1 || nt == ot ) +return ; + +switch ( nt ) + { +case p2m_ioreq_server: +/* +* p2m_ioreq_server is only used for 4K pages, so +* the count is only done for level 1 entries. +*/ + p2m->ioreq.entry_count++; +break ; + +default : +break ; + } + +switch ( ot ) + { +case p2m_ioreq_server: +ASSERT (p2m->ioreq.entry_count >0 ); + p2m->ioreq.entry_count--; +break ; + +default : +break ; + } +} +#endif /* _XEN_ASM_X86_P2M_H */ /*
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 5/8] x86/mm: split p2m ioreq server pages special handling into helper
Quoted 7 lines · Roger Pau Monne
So that it can be shared by both ept, npt and shadow code, instead of duplicating it.
No change in functionality intended.
Signed-off-by Roger Pau Monné <[email protected]>Reviewed-by Paul Durrant <[email protected]>
There's part of me that feels like this patch wants some ASSERTs or something; but as they're added in later on, and everything else looks good:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 6/8 p2m: change write_p2m_entry to return an error code
In reply to Roger Pau MonneThis is in preparation for also changing p2m_entry_modify to return an error code.
No functional change intended.
---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Tim Deegan <[email protected]>
---
Changes since v2:
- New in this version.
--- xen/arch/x86/mm/hap/hap.c | 4 +++- xen/arch/x86/mm/hap/nested_hap.c | 4 +++- xen/arch/x86/mm/p2m-pt.c | 30 ++++++++++++++++++------------ xen/arch/x86/mm/paging.c | 12 ++++++++---- xen/arch/x86/mm/shadow/common.c | 4 +++- xen/arch/x86/mm/shadow/none.c | 7 ++++--- xen/arch/x86/mm/shadow/private.h | 6 +++--- xen/include/asm-x86/p2m.h | 4 ++-- xen/include/asm-x86/paging.h | 8 ++++---- 9 files changed, 48 insertions(+), 31 deletions(-)
xen/arch/x86/mm/hap/hap.c +3 -1
@@ -708,7 +708,7 @@ static void hap_update_paging_modes(struct vcpu *v)put_gfn (d, cr3_gfn); } -static void +static int hap_write_p2m_entry (struct domain *d,unsigned long gfn,l1_pgentry_t *p,l1_pgentry_t new,unsigned int level) {
@@ -745,6 +745,8 @@ hap_write_p2m_entry(struct domain *d, unsigned long gfn, l1_pgentry_t *p,if ( flush_nestedp2m )p2m_flush_nestedp2m (d); + +return 0 ; } static unsigned long hap_gva_to_gfn_real_mode(
xen/arch/x86/mm/hap/nested_hap.c +3 -1
@@ -71,7 +71,7 @@/* NESTED VIRT P2M FUNCTIONS */ /********************************************/ -void +int nestedp2m_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn,l1_pgentry_t *p,l1_pgentry_t new,unsigned int level) {
@@ -87,6 +87,8 @@ nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn,flush_tlb_mask (p2m->dirty_cpumask);paging_unlock (d); + +return 0 ; }/********************************************/
xen/arch/x86/mm/p2m-pt.c +18 -12
@@ -202,7 +202,7 @@ p2m_next_level(struct p2m_domain *p2m, void **table, new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 )); }else if ( flags & _PAGE_PSE ) {
@@ -250,14 +250,14 @@ p2m_next_level(struct p2m_domain *p2m, void **table, { new_entry =l1e_from_pfn (pfn | (i << ((level -1 ) *PAGETABLE_ORDER )), flags); - p2m->write_p2m_entry (p2m, gfn, l1_entry + i, new_entry, level); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, l1_entry + i, new_entry, level)); }unmap_domain_page (l1_entry); new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 )); }else ASSERT (flags & _PAGE_PRESENT);
@@ -321,7 +321,7 @@ static int p2m_pt_set_recalc_range(struct p2m_domain *p2m,if ( (l1e_get_flags (e) & _PAGE_PRESENT) && !needs_recalc (l1, e) ) {set_recalc (l1, e); - p2m->write_p2m_entry (p2m, first_gfn, pent, e, level); +BUG_ON (p2m->write_p2m_entry (p2m, first_gfn, pent, e, level)); } first_gfn +=1UL << (i *PAGETABLE_ORDER ); }
@@ -392,14 +392,14 @@ static int do_recalc(struct p2m_domain *p2m, unsigned long gfn) !needs_recalc(l1, ent) ) {set_recalc (l1, ent); - p2m->write_p2m_entry (p2m, gfn - remainder, &ptab[i], - ent, level); +BUG_ON (p2m->write_p2m_entry (p2m, gfn - remainder, &ptab[i], + ent, level)); } remainder -=1UL << ((level -1 ) *PAGETABLE_ORDER ); }smp_wmb ();clear_recalc (l1, e); - p2m->write_p2m_entry (p2m, gfn, pent, e, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, pent, e, level +1 )); }unmap_domain_page ((void *)((unsigned long )pent &PAGE_MASK )); }
@@ -444,7 +444,7 @@ static int do_recalc(struct p2m_domain *p2m, unsigned long gfn) }else clear_recalc (l1, e); - p2m->write_p2m_entry (p2m, gfn, pent, e, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, pent, e, level +1 )); } out:
@@ -595,8 +595,10 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,if ( entry_content.l1 !=0 )p2m_add_iommu_flags (&entry_content,0 , iommu_pte_flags); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,3 ); + rc = p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,3 );/* NB: paging_write_p2m_entry() handles tlb flushes properly */ +if ( rc ) + goto out; }else {
@@ -633,8 +635,10 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,p2m_add_iommu_flags (&entry_content,0 , iommu_pte_flags);/* level 1 entry */ - p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,1 ); + rc = p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,1 );/* NB: paging_write_p2m_entry() handles tlb flushes properly */ +if ( rc ) + goto out; }else if ( page_order ==PAGE_ORDER_2M ) {
@@ -669,8 +673,10 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,if ( entry_content.l1 !=0 )p2m_add_iommu_flags (&entry_content,0 , iommu_pte_flags); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,2 ); + rc = p2m->write_p2m_entry (p2m, gfn, p2m_entry, entry_content,2 );/* NB: paging_write_p2m_entry() handles tlb flushes properly */ +if ( rc ) + goto out; }/* Track the highest gfn for which we have ever had a valid mapping */
@@ -895,7 +901,7 @@ static void p2m_pt_change_entry_type_global(struct p2m_domain *p2m, !needs_recalc(l1, e) ) {set_recalc (l1, e); - p2m->write_p2m_entry (p2m, gfn, &tab[i], e,4 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, &tab[i], e,4 )); ++changed; } gfn +=1UL << (L4_PAGETABLE_SHIFT -PAGE_SHIFT );
xen/arch/x86/mm/paging.c +8 -4
@@ -932,18 +932,22 @@ void paging_update_nestedmode(struct vcpu *v) }#endif -void paging_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn, -l1_pgentry_t *p,l1_pgentry_t new, -unsigned int level) +int paging_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn, +l1_pgentry_t *p,l1_pgentry_t new, +unsigned int level) {struct domain *d = p2m->domain;struct vcpu *v = current; +int rc =0 ; +if ( v->domain != d ) v = d->vcpu ? d->vcpu[0 ] :NULL ;if (likely (v &&paging_mode_enabled (d) &&paging_get_hostmode (v) !=NULL ) ) -paging_get_hostmode (v)->write_p2m_entry (d, gfn, p, new, level); + rc =paging_get_hostmode (v)->write_p2m_entry (d, gfn, p, new, level);else safe_write_pte (p, new); + +return rc; }int paging_set_allocation (struct domain *d,unsigned int pages,bool *preempted)
xen/arch/x86/mm/shadow/common.c +3 -1
@@ -3176,7 +3176,7 @@ static void sh_unshadow_for_p2m_change(struct domain *d, unsigned long gfn, } } -void +int shadow_write_p2m_entry (struct domain *d,unsigned long gfn,l1_pgentry_t *p,l1_pgentry_t new,unsigned int level)
@@ -3209,6 +3209,8 @@ shadow_write_p2m_entry(struct domain *d, unsigned long gfn,#endif paging_unlock (d); + +return 0 ; }/**************************************************************************/
xen/arch/x86/mm/shadow/none.c +4 -3
@@ -60,11 +60,12 @@ static void _update_paging_modes(struct vcpu *v)ASSERT_UNREACHABLE (); } -static void _write_p2m_entry (struct domain *d,unsigned long gfn, -l1_pgentry_t *p,l1_pgentry_t new, -unsigned int level) +static int _write_p2m_entry (struct domain *d,unsigned long gfn, +l1_pgentry_t *p,l1_pgentry_t new, +unsigned int level) {ASSERT_UNREACHABLE (); +return -ENOSYS ; }static const struct paging_mode sh_paging_none = {
xen/arch/x86/mm/shadow/private.h +3 -3
@@ -372,9 +372,9 @@ extern int sh_remove_write_access(struct domain *d, mfn_t readonly_mfn, unsigned long fault_addr);/* Functions that atomically write PT/P2M entries and update state */ -void shadow_write_p2m_entry (struct domain *d,unsigned long gfn, -l1_pgentry_t *p,l1_pgentry_t new, -unsigned int level); +int shadow_write_p2m_entry (struct domain *d,unsigned long gfn, +l1_pgentry_t *p,l1_pgentry_t new, +unsigned int level);/* Update all the things that are derived from the guest's CR0/CR3/CR4. * Called to initialize paging structures if the paging mode
xen/include/asm-x86/p2m.h +2 -2
@@ -265,7 +265,7 @@ struct p2m_domain { unsigned long last_gfn);void (*memory_type_changed)(struct p2m_domain *p2m); -void (*write_p2m_entry)(struct p2m_domain *p2m, +int (*write_p2m_entry)(struct p2m_domain *p2m,unsigned long gfn,l1_pgentry_t *p,l1_pgentry_t new,unsigned int level);long (*audit_p2m)(struct p2m_domain *p2m);
@@ -837,7 +837,7 @@ void p2m_flush_nestedp2m(struct domain *d);/* Flushes the np2m specified by np2m_base (if it exists) */ void np2m_flush_base (struct vcpu *v,unsigned long np2m_base); -void nestedp2m_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn, +int nestedp2m_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn,l1_pgentry_t *p,l1_pgentry_t new,unsigned int level);/*
xen/include/asm-x86/paging.h +4 -4
@@ -124,7 +124,7 @@ struct paging_mode {void (*update_cr3 )(struct vcpu *v,int do_locking,bool noflush);void (*update_paging_modes )(struct vcpu *v); -void (*write_p2m_entry )(struct domain *d,unsigned long gfn, +int (*write_p2m_entry )(struct domain *d,unsigned long gfn,l1_pgentry_t *p,l1_pgentry_t new,unsigned int level);
@@ -339,9 +339,9 @@ static inline void safe_write_pte(l1_pgentry_t *p, l1_pgentry_t new)* we are writing. */ struct p2m_domain ; -void paging_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn, -l1_pgentry_t *p,l1_pgentry_t new, -unsigned int level); +int paging_write_p2m_entry (struct p2m_domain *p2m,unsigned long gfn, +l1_pgentry_t *p,l1_pgentry_t new, +unsigned int level);/* * Called from the guest to indicate that the a process is being
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 6/8] p2m: change write_p2m_entry to return an error code
Quoted 7 lines · Roger Pau Monne
This is in preparation for also changing
p2m_entry_modifyto return an error code.No functional change intended.
I think you need to explain wheny/why you’re using BUG_ON() rather than ASSERT() or passing the caller up the stack.
Just in general:
* Passing things up the stack should be used when the caller is already expecting to handle errors, and the state when the error was discovered isn’t broken, or too hard to fix.
* BUG_ON() should be used when you can’t pass things up the stack, and continuing would certainly cause a vulnerability.
* ASSERT() should be used when continuing might work, or might have an effect later whose badness is equal or less than that of a host crash; OR whose truth can be clearly observed from the code directly surrounding it.
For instance...
xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 205
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +1
-1@@ -202,7 +202,7 @@ p2m_next_level(struct p2m_domain *p2m, void **table, new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ));
In this case, a few lines above we have return -ENOMEM; so:
1. The caller is expecting to handle error values, and
2. There’s no unusual state to try to clean up.
It seems like the rc = … / if ( rc ) return rc pattern would be better here.
xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 254
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +1
-1}else if ( flags & _PAGE_PSE ) {@@ -250,14 +250,14 @@ p2m_next_level(struct p2m_domain *p2m, void **table, { new_entry =l1e_from_pfn (pfn | (i << ((level -1 ) *PAGETABLE_ORDER )), flags); - p2m->write_p2m_entry (p2m, gfn, l1_entry + i, new_entry, level); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, l1_entry + i, new_entry, level)); }
Here, we can’t return an error value, because we’re halfway through an operation and don’t want to have to bother to clean up. Ignoring the error and leaving it half-done would certainly be worse than a host crash.
On the other hand… we know that the previous write_entry succeeded; is it really possible for this one to fail?
p2m_entry_modify() doesn’t do anything for entries > 4k, and (by the end of this series) will BUG_ON() if its "
I’m tempted to say that what we should do is use ASSERT() here (and many other places) put a comment in p2m_entry_modify() to say that when changing it, we need to revisit all the direct callers to make sure that ASSERT() is still suitable.
xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 260
Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c +1
-1unmap_domain_page (l1_entry); new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ));
In this case, we can see from the context that what’s been written is a mid-level entry that has just been allocated; there’s no code change that should be able to cause this to fail. I’m inclined to say this should only be an ASSERT().
xen/arch/x86/mm/p2m-pt.c static int p2m_pt_set_recalc_range(struct p2m_domain *p2m, · line 325
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +1
-1}else ASSERT (flags & _PAGE_PRESENT);@@ -321,7 +321,7 @@ static int p2m_pt_set_recalc_range(struct p2m_domain *p2m,if ( (l1e_get_flags (e) & _PAGE_PRESENT) && !needs_recalc (l1, e) ) {set_recalc (l1, e); - p2m->write_p2m_entry (p2m, first_gfn, pent, e, level); +BUG_ON (p2m->write_p2m_entry (p2m, first_gfn, pent, e, level)); }
Again here; theoretically, the only change has been that RECALC_FLAGS have been added.
And so on.
Thoughts? (Looking for input from Jan here as well.)
If not, it seems like we should also be modifying the places in p2m-ept.c to do BUG_ON() rather than ASSERT()’ing that the page write succeeded.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 6/8] p2m: change write_p2m_entry to return an error code
Quoted 28 lines · George Dunlap
Quoted 7 lines · Roger Pau Monne
This is in preparation for also changing
p2m_entry_modifyto return an error code.No functional change intended.
I think you need to explain wheny/why you’re using
BUG_ON()rather thanASSERT()or passing the caller up the stack.Just in general:
* Passing things up the stack should be used when the caller is already expecting to handle errors, and the state when the error was discovered isn’t broken, or too hard to fix.
*
BUG_ON()should be used when you can’t pass things up the stack, and continuing would certainly cause a vulnerability.*
ASSERT()should be used when continuing might work, or might have an effect later whose badness is equal or less than that of a host crash; OR whose truth can be clearly observed from the code directly surrounding it.For instance...
Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c +0
-0@@ -202,7 +202,7 @@ p2m_next_level(struct p2m_domain *p2m, void **table, new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);
Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 205
Quoted 3 lines
xen/arch/x86/mm/p2m-pt.c +1
-1p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ));In this case, a few lines above we have
return -ENOMEM; so:
1. The caller is expecting to handle error values, and
2. There’s no unusual state to try to clean up.
I'm not that familiar with the p2m code, but there's a call to p2m_alloc_ptp just further up, and while I think failing here would only imply that a page has been added to p2m->pages but has not actually been used in the p2m page tables because the addition of the entry failed.
I think adding an entry that expands the page table structure should never fail, and hence I think the following:
rc = p2m->write_p2m_entry(p2m, gfn, p2m_entry, new_entry, level + 1); if ( rc )
{
ASSERT_UNREACHABLE();
return rc;
}Would be the best way to handle the return code from write_p2m_entry in p2m_next_level.
Quoted 44 lines
It seems like the
rc = … / if ( rc ) return rcpattern would be better here.xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 254
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +1
-1}else if ( flags & _PAGE_PSE ) {@@ -250,14 +250,14 @@ p2m_next_level(struct p2m_domain *p2m, void **table, { new_entry =l1e_from_pfn (pfn | (i << ((level -1 ) *PAGETABLE_ORDER )), flags); - p2m->write_p2m_entry (p2m, gfn, l1_entry + i, new_entry, level); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, l1_entry + i, new_entry, level)); }Here, we can’t return an error value, because we’re halfway through an operation and don’t want to have to bother to clean up. Ignoring the error and leaving it half-done would certainly be worse than a host crash.
On the other hand… we know that the previous
write_entrysucceeded; is it really possible for this one to fail?
p2m_entry_modify()doesn’t do anything for entries > 4k, and (by the end of this series) willBUG_ON()if its "I’m tempted to say that what we should do is use
ASSERT()here (and many other places) put a comment inp2m_entry_modify()to say that when changing it, we need to revisit all the direct callers to make sure thatASSERT()is still suitable.xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 260
Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c +1
-1unmap_domain_page (l1_entry); new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ));In this case, we can see from the context that what’s been written is a mid-level entry that has just been allocated; there’s no code change that should be able to cause this to fail. I’m inclined to say this should only be an
ASSERT().xen/arch/x86/mm/p2m-pt.c static int p2m_pt_set_recalc_range(struct p2m_domain *p2m, · line 325
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +1
-1}else ASSERT (flags & _PAGE_PRESENT);@@ -321,7 +321,7 @@ static int p2m_pt_set_recalc_range(struct p2m_domain *p2m,if ( (l1e_get_flags (e) & _PAGE_PRESENT) && !needs_recalc (l1, e) ) {set_recalc (l1, e); - p2m->write_p2m_entry (p2m, first_gfn, pent, e, level); +BUG_ON (p2m->write_p2m_entry (p2m, first_gfn, pent, e, level)); }Again here; theoretically, the only change has been that
RECALC_FLAGShave been added.
Quoted 3 lines
And so on.
Thoughts? (Looking for input from Jan here as well.)
As you say here and above, the only caller that's expected to fail is p2m_pt_set_entry, the rest just do recalc or add intermediate entries to the p2m, which must always succeed.
Quoted 1 line
If not, it seems like we should also be modifying the places in
p2m-ept.cto doBUG_ON()rather than ASSERT()’ing that the page write succeeded.
I can apply the same treatment that's done in p2m-ept.c, so that we have consistency between both implementations.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 6/8] p2m: change write_p2m_entry to return an error code
Quoted 48 lines · Roger Pau Monné
Quoted 28 lines · George Dunlap
Quoted 7 lines · Roger Pau Monne
This is in preparation for also changing
p2m_entry_modifyto return an error code.No functional change intended.
I think you need to explain wheny/why you’re using
BUG_ON()rather thanASSERT()or passing the caller up the stack.Just in general:
* Passing things up the stack should be used when the caller is already expecting to handle errors, and the state when the error was discovered isn’t broken, or too hard to fix.
*
BUG_ON()should be used when you can’t pass things up the stack, and continuing would certainly cause a vulnerability.*
ASSERT()should be used when continuing might work, or might have an effect later whose badness is equal or less than that of a host crash; OR whose truth can be clearly observed from the code directly surrounding it.For instance...
Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c +0
-0@@ -202,7 +202,7 @@ p2m_next_level(struct p2m_domain *p2m, void **table, new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 205
Quoted 3 lines
xen/arch/x86/mm/p2m-pt.c +1
-1p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); - p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ); +BUG_ON (p2m->write_p2m_entry (p2m, gfn, p2m_entry, new_entry, level +1 ));In this case, a few lines above we have
return -ENOMEM; so:
1. The caller is expecting to handle error values, and
2. There’s no unusual state to try to clean up.I'm not that familiar with the p2m code, but there's a call to
p2m_alloc_ptpjust further up, and while I think failing here would only imply that a page has been added top2m->pagesbut has not actually been used in the p2m page tables because the addition of the entry failed.I think adding an entry that expands the page table structure should never fail, and hence I think the following:
rc =
p2m->write_p2m_entry(p2m, gfn, p2m_entry, new_entry, level + 1); if ( rc )
Quoted 7 lines
{ ASSERT_UNREACHABLE(); return rc; }Would be the best way to handle the return code from
write_p2m_entryinp2m_next_level.
That works for me.
Quoted 17 lines
Quoted 13 lines
xen/arch/x86/mm/p2m-pt.c static int p2m_pt_set_recalc_range(struct p2m_domain *p2m, · line 325
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +1
-1}else ASSERT (flags & _PAGE_PRESENT);@@ -321,7 +321,7 @@ static int p2m_pt_set_recalc_range(struct p2m_domain *p2m,if ( (l1e_get_flags (e) & _PAGE_PRESENT) && !needs_recalc (l1, e) ) {set_recalc (l1, e); - p2m->write_p2m_entry (p2m, first_gfn, pent, e, level); +BUG_ON (p2m->write_p2m_entry (p2m, first_gfn, pent, e, level)); }Again here; theoretically, the only change has been that
RECALC_FLAGShave been added.Quoted 3 lines
And so on.
Thoughts? (Looking for input from Jan here as well.)
Quoted 8 lines
As you say here and above, the only caller that's expected to fail is
p2m_pt_set_entry, the rest just do recalc or add intermediate entries to the p2m, which must always succeed.Quoted 1 line
If not, it seems like we should also be modifying the places in
p2m-ept.cto doBUG_ON()rather than ASSERT()’ing that the page write succeeded.I can apply the same treatment that's done in
p2m-ept.c, so that we have consistency between both implementations.
Sounds good.
Thanks!
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 6/8] p2m: change write_p2m_entry to return an error code
In reply to George DunlapQuoted 13 lines
xen/arch/x86/mm/p2m-pt.c static int p2m_pt_set_recalc_range(struct p2m_domain *p2m, · line 325
Quoted 9 lines
Quoted 1 line · On 15.02.19 at 18:48, <[email protected]> wrote:
On Feb 15, 2019, at 2:18 PM, Roger Pau Monne <[email protected]> wrote:
xen/arch/x86/mm/p2m-pt.c +1
-1@@ -321,7 +321,7 @@ static int p2m_pt_set_recalc_range(struct p2m_domain *p2m,if ( (l1e_get_flags (e) & _PAGE_PRESENT) && !needs_recalc (l1, e) ) {set_recalc (l1, e); - p2m->write_p2m_entry (p2m, first_gfn, pent, e, level); +BUG_ON (p2m->write_p2m_entry (p2m, first_gfn, pent, e, level)); }Again here; theoretically, the only change has been that
RECALC_FLAGShave been added.
Quoted 3 lines
And so on.
Thoughts? (Looking for input from Jan here as well.)
I'd indeed prefer a more fine grained (justified per instance) approach like you suggest, but I wasn't really opposed to how it was done so far as it didn't seem like any of the BUG_ON()-s should actually be able to trigger, considering that there was no failure possible there till now.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 7/8 x86/mm: handle foreign mappings in p2m_entry_modify
In reply to Roger Pau MonneSo that the specific handling can be removed from atomic_write_ept_entry and be shared with npt and shadow code.
This commit also removes the check that prevent non-ept PVH dom0 from mapping foreign pages.
---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Tim Deegan <[email protected]>
---
Changes since v2:
- Return an error code from p2m_entry_modify and propagate it to the callers.
Changes since v1:
- Simply code since mfn_to_page cannot return NULL.
- Check if the mfn is valid before getting/dropping the page reference.
- Use BUG_ON instead of ASSERTs, since getting the reference counting wrong is more dangerous than a DoS.
--- xen/arch/x86/mm/hap/hap.c | 12 +++++-- xen/arch/x86/mm/p2m-ept.c | 56 +++------------------------------ xen/arch/x86/mm/p2m-pt.c | 7 ----- xen/arch/x86/mm/shadow/common.c | 3 +- xen/include/asm-x86/p2m.h | 26 ++++++++++++--- 5 files changed, 39 insertions(+), 65 deletions(-)
xen/arch/x86/mm/hap/hap.c +10 -2
@@ -714,6 +714,7 @@ hap_write_p2m_entry(struct domain *d, unsigned long gfn, l1_pgentry_t *p, {uint32_t old_flags;bool_t flush_nestedp2m =0 ; +int rc;/* We know always use the host p2m here, regardless if the vcpu * is in host or guest mode. The vcpu can be in guest mode by
@@ -734,8 +735,15 @@ hap_write_p2m_entry(struct domain *d, unsigned long gfn, l1_pgentry_t *p, && perms_strictly_increased(old_flags, l1e_get_flags(new)) ); } -p2m_entry_modify (p2m_get_hostp2m (d),p2m_flags_to_type (l1e_get_flags (new)), -p2m_flags_to_type (old_flags), level); + rc =p2m_entry_modify (p2m_get_hostp2m (d), +p2m_flags_to_type (l1e_get_flags (new)), +p2m_flags_to_type (old_flags),l1e_get_mfn (new), +l1e_get_mfn (*p), level); +if ( rc ) + { +paging_unlock (d); +return rc; + }safe_write_pte (p, new);if ( old_flags & _PAGE_PRESENT )
xen/arch/x86/mm/p2m-ept.c +5 -51
70 lines not on this page · raw message
xen/arch/x86/mm/p2m-pt.c +0 -7
@@ -540,13 +540,6 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,__trace_var (TRC_MEM_SET_P2M_ENTRY ,0 ,sizeof (t), &t); } -if (unlikely (p2m_is_foreign (p2mt)) ) - { -/* hvm fixme: foreign types are only supported on ept at present */ -gdprintk (XENLOG_WARNING ,"Unimplemented foreign p2m type.\n" ); -return -EINVAL ; - } -/* Carry out any eventually pending earlier changes first. */ rc =do_recalc (p2m, gfn);if ( rc <0 )
xen/arch/x86/mm/shadow/common.c +2 -1
@@ -3189,7 +3189,8 @@ shadow_write_p2m_entry(struct domain *d, unsigned long gfn,sh_unshadow_for_p2m_change (d, gfn, p, new, level);p2m_entry_modify (p2m_get_hostp2m (d),p2m_flags_to_type (l1e_get_flags (new)), -p2m_flags_to_type (l1e_get_flags (*p)), level); +p2m_flags_to_type (l1e_get_flags (*p)),l1e_get_mfn (new), +l1e_get_mfn (*p), level);/* Update the entry with new content */ safe_write_pte (p, new);
xen/include/asm-x86/p2m.h +22 -4
@@ -932,11 +932,14 @@ int p2m_set_ioreq_server(struct domain *d, unsigned int flags,struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d,unsigned int *flags); -static inline void p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, -p2m_type_t ot,unsigned int level) +static inline int p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, +p2m_type_t ot,mfn_t nfn,mfn_t ofn, +unsigned int level) { -if ( level !=1 || nt == ot ) -return ; +BUG_ON (level >1 && (nt == p2m_ioreq_server || nt == p2m_map_foreign)); + +if ( level !=1 || (nt == ot &&mfn_eq (nfn, ofn)) ) +return 0 ;switch ( nt ) {
@@ -948,6 +951,14 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count++;break ; +case p2m_map_foreign: +BUG_ON (!mfn_valid (nfn)); + +if ( !page_get_owner_and_reference (mfn_to_page (nfn)) ) +return -EBUSY ; + +break ; +default :break ; }
@@ -959,9 +970,16 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count--;break ; +case p2m_map_foreign: +BUG_ON (!mfn_valid (ofn)); +put_page (mfn_to_page (ofn)); +break ; +default : break; } + +return 0 ; }#endif /* _XEN_ASM_X86_P2M_H */
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 7/8] x86/mm: handle foreign mappings in p2m_entry_modify
Quoted 11 lines · Roger Pau Monne
So that the specific handling can be removed from
atomic_write_ept_entryand be shared with npt and shadow code.This commit also removes the check that prevent non-ept PVH dom0 from mapping foreign pages.
Signed-off-by Roger Pau Monné <[email protected]>
Mostly looks good; just a few comments...
Quoted 25 lines
xen/include/asm-x86/p2m.h +7
-4@@ -932,11 +932,14 @@ int p2m_set_ioreq_server(struct domain *d, unsigned int flags,struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d,unsigned int *flags); -static inline void p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, -p2m_type_t ot,unsigned int level) +static inline int p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, +p2m_type_t ot,mfn_t nfn,mfn_t ofn, +unsigned int level) { -if ( level !=1 || nt == ot ) -return ; +BUG_ON (level >1 && (nt == p2m_ioreq_server || nt == p2m_map_foreign)); + +if ( level !=1 || (nt == ot &&mfn_eq (nfn, ofn)) ) +return 0 ;switch ( nt ) {
xen/include/asm-x86/p2m.h static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, · line 955
Quoted 5 lines
xen/include/asm-x86/p2m.h +2
-0@@ -948,6 +951,14 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count++;break ; +case p2m_map_foreign: +BUG_ON (!mfn_valid (nfn));
Since we’re going to be returning errors anyway, why not retain the original functionality of returning -EINVAL in this case, rather than BUG_ON?
Quoted 10 lines
xen/include/asm-x86/p2m.h +6
-0+ +if ( !page_get_owner_and_reference (mfn_to_page (nfn)) ) +return -EBUSY ; + +break ; +default :break ; }
xen/include/asm-x86/p2m.h static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, · line 974
Quoted 5 lines
xen/include/asm-x86/p2m.h +2
-0@@ -959,9 +970,16 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count--;break ; +case p2m_map_foreign: +BUG_ON (!mfn_valid (ofn));
If somehow this happened, then the bug isn’t here but somewhere else; continuing on won’t make things any worse than they would be if this page weren’t removed. I think this should probably be an ASSERT() (to help narrow down where a bug may have come from), followed by a simple return. Likely the worst that would happen here is an un-killable domain; no need to crash production servers in this case.
Thanks,
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 7/8] x86/mm: handle foreign mappings in p2m_entry_modify
Quoted 41 lines · George Dunlap
Quoted 11 lines · Roger Pau Monne
So that the specific handling can be removed from
atomic_write_ept_entryand be shared with npt and shadow code.This commit also removes the check that prevent non-ept PVH dom0 from mapping foreign pages.
Signed-off-by Roger Pau Monné <[email protected]>Mostly looks good; just a few comments...
Quoted 28 lines
xen/include/asm-x86/p2m.h +7
-4@@ -932,11 +932,14 @@ int p2m_set_ioreq_server(struct domain *d, unsigned int flags,struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d,unsigned int *flags); -static inline void p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, -p2m_type_t ot,unsigned int level) +static inline int p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, +p2m_type_t ot,mfn_t nfn,mfn_t ofn, +unsigned int level) { -if ( level !=1 || nt == ot ) -return ; +BUG_ON (level >1 && (nt == p2m_ioreq_server || nt == p2m_map_foreign)); + +if ( level !=1 || (nt == ot &&mfn_eq (nfn, ofn)) ) +return 0 ;switch ( nt ) {@@ -948,6 +951,14 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count++;break ;
Quoted 4 lines
xen/include/asm-x86/p2m.h static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, · line 955
Quoted 2 lines
xen/include/asm-x86/p2m.h +2
-0+case p2m_map_foreign: +BUG_ON (!mfn_valid (nfn));Since we’re going to be returning errors anyway, why not retain the original functionality of returning
-EINVALin this case, rather thanBUG_ON?
Ack. I added the BUG_ON because trying to add a foreign entry with an invalid mfn means something else went wrong in the caller, since it should not be possible to perform such action. As you pointed out however there's no reason to panic, since an error can be returned to the caller.
Would you be fine with also adding an ASSERT_UNREACHABLE before returning the error?
Quoted 13 lines
Quoted 13 lines
xen/include/asm-x86/p2m.h +6
-0+ +if ( !page_get_owner_and_reference (mfn_to_page (nfn)) ) +return -EBUSY ; + +break ; +default :break ; }@@ -959,9 +970,16 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count--;break ;
Quoted 4 lines
xen/include/asm-x86/p2m.h static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, · line 974
Quoted 2 lines
xen/include/asm-x86/p2m.h +2
-0+case p2m_map_foreign: +BUG_ON (!mfn_valid (ofn));If somehow this happened, then the bug isn’t here but somewhere else; continuing on won’t make things any worse than they would be if this page weren’t removed. I think this should probably be an
ASSERT()(to help narrow down where a bug may have come from), followed by a simple return. Likely the worst that would happen here is an un-killable domain; no need to crash production servers in this case.
Ack, I think what I suggested above should also be used here:
if ( !mfn_valid(ofn) )
{
ASSERT_UNREACHABLE();
return -EINVAL;
}Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 7/8] x86/mm: handle foreign mappings in p2m_entry_modify
Quoted 74 lines · Roger Pau Monné
Quoted 41 lines · George Dunlap
Quoted 11 lines · Roger Pau Monne
So that the specific handling can be removed from
atomic_write_ept_entryand be shared with npt and shadow code.This commit also removes the check that prevent non-ept PVH dom0 from mapping foreign pages.
Signed-off-by Roger Pau Monné <[email protected]>Mostly looks good; just a few comments...
Quoted 28 lines
xen/include/asm-x86/p2m.h +7
-4@@ -932,11 +932,14 @@ int p2m_set_ioreq_server(struct domain *d, unsigned int flags,struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d,unsigned int *flags); -static inline void p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, -p2m_type_t ot,unsigned int level) +static inline int p2m_entry_modify (struct p2m_domain *p2m,p2m_type_t nt, +p2m_type_t ot,mfn_t nfn,mfn_t ofn, +unsigned int level) { -if ( level !=1 || nt == ot ) -return ; +BUG_ON (level >1 && (nt == p2m_ioreq_server || nt == p2m_map_foreign)); + +if ( level !=1 || (nt == ot &&mfn_eq (nfn, ofn)) ) +return 0 ;switch ( nt ) {@@ -948,6 +951,14 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count++;break ;Quoted 4 lines
xen/include/asm-x86/p2m.h static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, · line 955
Quoted 2 lines
xen/include/asm-x86/p2m.h +2
-0+case p2m_map_foreign: +BUG_ON (!mfn_valid (nfn));Since we’re going to be returning errors anyway, why not retain the original functionality of returning
-EINVALin this case, rather thanBUG_ON?Ack. I added the
BUG_ONbecause trying to add a foreign entry with an invalid mfn means something else went wrong in the caller, since it should not be possible to perform such action. As you pointed out however there's no reason to panic, since an error can be returned to the caller.Would you be fine with also adding an
ASSERT_UNREACHABLEbefore returning the error?Quoted 13 lines
Quoted 13 lines
xen/include/asm-x86/p2m.h +6
-0+ +if ( !page_get_owner_and_reference (mfn_to_page (nfn)) ) +return -EBUSY ; + +break ; +default :break ; }@@ -959,9 +970,16 @@ static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m->ioreq.entry_count--;break ;Quoted 4 lines
xen/include/asm-x86/p2m.h static inline void p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, · line 974
Quoted 2 lines
xen/include/asm-x86/p2m.h +2
-0+case p2m_map_foreign: +BUG_ON (!mfn_valid (ofn));If somehow this happened, then the bug isn’t here but somewhere else; continuing on won’t make things any worse than they would be if this page weren’t removed. I think this should probably be an
ASSERT()(to help narrow down where a bug may have come from), followed by a simple return. Likely the worst that would happen here is an un-killable domain; no need to crash production servers in this case.
Quoted 7 lines
Ack, I think what I suggested above should also be used here:
if ( !mfn_valid(ofn) )
{ ASSERT_UNREACHABLE(); return -EINVAL; }
Yes to both ASSERTs. If something has broken one of our assumptions, the sooner we find it out (during development) the better.
Thanks,
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 8/8 npt/shadow: allow getting foreign page table entries
In reply to Roger Pau MonneCurrent npt and shadow code to get an entry will always return INVALID_MFN for foreign entries. Allow to return the entry mfn for foreign entries, like it's done for grant table entries.
---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v2:
- Use p2m_is_any_ram.
--- xen/arch/x86/mm/p2m-pt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/mm/p2m-pt.c +2 -2
@@ -869,8 +869,8 @@ pod_retry_l1: *t =p2m_recalc_type (recalc ||_needs_recalc (flags), l1t, p2m, gfn);unmap_domain_page (l1e); -ASSERT (mfn_valid (mfn) || !p2m_is_ram (*t) ||p2m_is_paging (*t)); -return (p2m_is_valid (*t) ||p2m_is_grant (*t)) ? mfn :INVALID_MFN ; +ASSERT (mfn_valid (mfn) || !p2m_is_any_ram (*t) ||p2m_is_paging (*t)); +return (p2m_is_valid (*t) ||p2m_is_any_ram (*t)) ? mfn :INVALID_MFN ; } static void p2m_pt_change_entry_type_global(struct p2m_domain *p2m,
Signature · 7 lines
2.17.2 (Apple Git-113) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel