IOMMU: superpage support when not sharing pagetables
v7 cover IOMMU: superpage support when not sharing pagetables
For a long time we've been rather inefficient with IOMMU page table management when not sharing page tables, i.e. in particular for PV (and further specifically also for PV Dom0) and AMD (where nowadays we never share page tables). While up to about 3.5 years ago AMD code had logic to un-shatter page mappings, that logic was ripped out for being buggy (XSA-275 plus follow-on).
This series enables use of large pages in AMD and Intel (VT-d) code; Arm is presently not in need of any enabling as pagetables are always shared there. It also augments PV Dom0 creation with suitable explicit IOMMU mapping calls to facilitate use of large pages there. Depending on the amount of memory handed to Dom0 this improves booting time (latency until Dom0 actually starts) quite a bit; subsequent shattering of some of the large pages may of course consume some of the saved time.
Known fallout has been spelled out here:
https://lists.xen.org/archives/html/xen-devel/2021-08/msg00781.html
See individual patches for details on the v7 changes.
01: iommu: add preemption support to iommu_{un,}map()
02: IOMMU/x86: perform PV Dom0 mappings in batches
03: IOMMU/x86: support freeing of pagetables
02: IOMMU/x86: new command line option to suppress use of superpage mappings 03: AMD/IOMMU: allow use of superpage mappings
04: VT-d: allow use of superpage mappings
05: x86: introduce helper for recording degree of contiguity in page tables 06: IOMMU/x86: prefill newly allocate page tables
07: AMD/IOMMU: free all-empty page tables
08: VT-d: free all-empty page tables
09: AMD/IOMMU: replace all-contiguous page tables by superpage mappings 10: VT-d: replace all-contiguous page tables by superpage mappings 11: IOMMU/x86: add perf counters for page table splitting / coalescing 12: VT-d: fold dma_pte_clear_one() into its only caller
While not directly related (except that making this mode work properly here was a fair part of the overall work), at this occasion I'd also like to renew my proposal to make "iommu=dom0-strict" the default going forward. It already is not only the default, but the only possible mode for PVH Dom0.
Jan
PATCH v7 1/14 iommu: add preemption support to iommu_{un,}map()
From: Roger Pau Monné <[email protected]>
The loop in iommu_{,un}map() can be arbitrary large, and as such it needs to handle preemption. Introduce a new flag that signals whether the function should do preemption checks, returning the number of pages that have been processed in case a need for preemption was actually found.
Note that the cleanup done in iommu_map() can now be incomplete if preemption has happened, and hence callers would need to take care of unmapping the whole range (ie: ranges already mapped by previously preempted calls). So far none of the callers care about having those ranges unmapped, so error handling in arch_iommu_hwdom_init() can be kept as-is.
Note that iommu_legacy_{un,}map() are left without preemption handling:
callers of those interfaces aren't going to modified to pass bigger chunks, and hence the functions won't be modified as they are legacy and uses should be replaced with iommu_{un,}map() instead if preemption is required.
---
v7: Integrate into series, with quite a few adjustments (beyond mere re-
basing).
xen/drivers/passthrough/iommu.c +27 -11
@@ -308,13 +308,13 @@ static unsigned int mapping_order(constreturn order; } -int iommu_map (struct domain *d,dfn_t dfn0,mfn_t mfn0, -unsigned long page_count,unsigned int flags, -unsigned int *flush_flags) +long iommu_map (struct domain *d,dfn_t dfn0,mfn_t mfn0, +unsigned long page_count,unsigned int flags, +unsigned int *flush_flags) {const struct domain_iommu *hd =dom_iommu (d);unsigned long i; -unsigned int order; +unsigned int order, j =0 ;int rc =0 ;if ( !is_iommu_enabled (d) )
@@ -329,6 +329,11 @@ int iommu_map(struct domain *d, dfn_t df order =mapping_order (hd, dfn, mfn, page_count - i); +if ( (flags & IOMMUF_preempt) && + ((!(++j &0xfff ) &&general_preempt_check ()) || + i >LONG_MAX - (1UL << order)) ) +return i; + rc =iommu_call (hd->platform_ops, map_page, d, dfn, mfn, flags |IOMMUF_order (order), flush_flags);
@@ -341,7 +346,7 @@ int iommu_map(struct domain *d, dfn_t df d->domain_id, dfn_x(dfn), mfn_x(mfn), rc);/* while statement to satisfy __must_check */ -while (iommu_unmap (d, dfn0, i, flush_flags) ) +while (iommu_unmap (d, dfn0, i,0 , flush_flags) )break ;if ( !is_hardware_domain (d) )
@@ -365,7 +370,10 @@ int iommu_legacy_map(struct domain *d, d unsigned long page_count, unsigned int flags) {unsigned int flush_flags =0 ; -int rc =iommu_map (d, dfn, mfn, page_count, flags, &flush_flags); +int rc; + +ASSERT (!(flags & IOMMUF_preempt)); + rc =iommu_map (d, dfn, mfn, page_count, flags, &flush_flags);if ( !this_cpu (iommu_dont_flush_iotlb) && !rc ) rc =iommu_iotlb_flush (d, dfn, page_count, flush_flags);
@@ -373,25 +381,33 @@ int iommu_legacy_map(struct domain *d, dreturn rc; } -int iommu_unmap (struct domain *d,dfn_t dfn0,unsigned long page_count, -unsigned int *flush_flags) +long iommu_unmap (struct domain *d,dfn_t dfn0,unsigned long page_count, +unsigned int flags,unsigned int *flush_flags) {const struct domain_iommu *hd =dom_iommu (d);unsigned long i; -unsigned int order; +unsigned int order, j =0 ;int rc =0 ;if ( !is_iommu_enabled (d) )return 0 ; +ASSERT (!(flags & ~IOMMUF_preempt)); +for ( i =0 ; i < page_count; i +=1UL << order ) {dfn_t dfn =dfn_add (dfn0, i);int err; order =mapping_order (hd, dfn,_mfn (0 ), page_count - i); + +if ( (flags & IOMMUF_preempt) && + ((!(++j &0xfff ) &&general_preempt_check ()) || + i >LONG_MAX - (1UL << order)) ) +return i; + err =iommu_call (hd->platform_ops, unmap_page, d, dfn, - order, flush_flags); + flags |IOMMUF_order (order), flush_flags);if (likely (!err) )continue ;
@@ -425,7 +441,7 @@ int iommu_unmap(struct domain *d, dfn_tint iommu_legacy_unmap (struct domain *d,dfn_t dfn,unsigned long page_count) {unsigned int flush_flags =0 ; -int rc =iommu_unmap (d, dfn, page_count, &flush_flags); +int rc =iommu_unmap (d, dfn, page_count,0 , &flush_flags);if ( !this_cpu (iommu_dont_flush_iotlb) && !rc ) rc =iommu_iotlb_flush (d, dfn, page_count, flush_flags);
xen/include/xen/iommu.h +15 -8
@@ -124,14 +124,15 @@ void arch_iommu_check_autotranslated_hwdvoid arch_iommu_hwdom_init (struct domain *d);/* -* The following flags are passed to map operations and passed by lookup -* operations. +* The following flags are passed to map (applicable ones also to unmap) +* operations, while some are passed back by lookup operations. */ #define IOMMUF_order (n) ((n) & 0x3f)#define _IOMMUF_readable 6#define IOMMUF_readable (1u<<_IOMMUF_readable)#define _IOMMUF_writable 7#define IOMMUF_writable (1u<<_IOMMUF_writable) +#define IOMMUF_preempt (1u << 8)/* * flush_flags:
@@ -153,12 +154,18 @@ enum#define IOMMU_FLUSHF_modified (1u << _IOMMU_FLUSHF_modified)#define IOMMU_FLUSHF_all (1u << _IOMMU_FLUSHF_all) -int __must_check iommu_map (struct domain *d,dfn_t dfn,mfn_t mfn, -unsigned long page_count,unsigned int flags, -unsigned int *flush_flags); -int __must_check iommu_unmap (struct domain *d,dfn_t dfn, -unsigned long page_count, -unsigned int *flush_flags); +/* +* For both of these: Negative return values are error indicators. Zero +* indicates full successful completion of the request, while positive +* values indicate partial completion, which is possible only with +* IOMMUF_preempt passed in. +*/ +long __must_check iommu_map (struct domain *d,dfn_t dfn,mfn_t mfn, +unsigned long page_count,unsigned int flags, +unsigned int *flush_flags); +long __must_check iommu_unmap (struct domain *d,dfn_t dfn, +unsigned long page_count,unsigned int flags, +unsigned int *flush_flags); int __must_check iommu_legacy_map(struct domain *d, dfn_t dfn, mfn_t mfn, unsigned long page_count,
Re: [PATCH v7 01/14] iommu: add preemption support to iommu_{un,}map()
Quoted 15 lines · Jan Beulich
From: Roger Pau Monné <[email protected]>
The loop in iommu_{,un}map() can be arbitrary large, and as such it needs to handle preemption. Introduce a new flag that signals whether the function should do preemption checks, returning the number of pages that have been processed in case a need for preemption was actually found.
Note that the cleanup done in
iommu_map()can now be incomplete if preemption has happened, and hence callers would need to take care of unmapping the whole range (ie: ranges already mapped by previously preempted calls). So far none of the callers care about having those ranges unmapped, so error handling inarch_iommu_hwdom_init()can be kept as-is.
Quoted 8 lines
Note that iommu_legacy_{un,}map() are left without preemption handling:
callers of those interfaces aren't going to modified to pass bigger chunks, and hence the functions won't be modified as they are legacy and uses should be replaced with iommu_{un,}map() instead if preemption is required.Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Jan Beulich <[email protected]>
PATCH v7 2/14 IOMMU/x86: perform PV Dom0 mappings in batches
In reply to Jan BeulichFor large page mappings to be easily usable (i.e. in particular without un-shattering of smaller page mappings) and for mapping operations to then also be more efficient, pass batches of Dom0 memory to iommu_map(). In dom0_construct_pv() and its helpers (covering strict mode) this additionally requires establishing the type of those pages (albeit with zero type references).
The earlier establishing of PGT_writable_page | PGT_validated requires the existing places where this gets done (through get_page_and_type()) to be updated: For pages which actually have a mapping, the type refcount needs to be 1.
There is actually a related bug that gets fixed here as a side effect:
Typically the last L1 table would get marked as such only after get_page_and_type(..., PGT_writable_page). While this is fine as far as refcounting goes, the page did remain mapped in the IOMMU in this case (when "iommu=dom0-strict").
---
Subsequently p2m_add_identity_entry() may want to also gain an order parameter, for arch_iommu_hwdom_init() to use. While this only affects non-RAM regions, systems typically have 2-16Mb of reserved space immediately below 4Gb, which hence could be mapped more efficiently.
Eventually we may want to overhaul this logic to use a rangeset based approach instead, punching holes into originally uniformly large-page- mapped regions. Doing so right here would first and foremost be yet more of a change.
The installing of zero-ref writable types has in fact shown (observed while putting together the change) that despite the intention by the XSA-288 changes (affecting DomU-s only) for Dom0 a number of sufficiently ordinary pages (at the very least initrd and P2M ones as well as pages that are part of the initial allocation but not part of the initial mapping) still have been starting out as PGT_none, meaning that they would have gained IOMMU mappings only the first time these pages would get mapped writably. Consequently an open question is whether iommu_memory_setup() should set the pages to PGT_writable_page independent of need_iommu_pt_sync().
I didn't think I need to address the bug mentioned in the description in a separate (prereq) patch, but if others disagree I could certainly break out that part (needing to first use iommu_legacy_unmap() then).
Note that 4k P2M pages don't get (pre-)mapped in setup_pv_physmap():
They'll end up mapped via the later get_page_and_type().
As to the way these refs get installed: I've chosen to avoid the more expensive {get,put}_page_and_type(), favoring to put in place the intended type directly. I guess I could be convinced to avoid this bypassing of the actual logic; I merely think it's unnecessarily expensive.
Note also that strictly speaking the iommu_iotlb_flush_all() here (as well as the pre-existing one in arch_iommu_hwdom_init()) shouldn't be
needed: Actual hooking up (AMD) or enabling of translation (VT-d) occurs only afterwards anyway, so nothing can have made it into TLBs just yet.
---
v7: Restore after the prior verson having been reverted. Use preemption
when calling iommu_map(). Correct log message in arch_iommu_hwdom_init().
v3: Fold iommu_map() into (the now renamed) iommu_memory_setup(). Move
iommu_unmap() into mark_pv_pt_pages_rdonly(). Adjust (split) log message in arch_iommu_hwdom_init().
xen/arch/x86/pv/dom0_build.c +90 -7
@@ -46,7 +46,8 @@ void __init dom0_update_physmap(bool comstatic __init void mark_pv_pt_pages_rdonly (struct domain *d,l4_pgentry_t *l4start,unsigned long vpt_start, -unsigned long nr_pt_pages) +unsigned long nr_pt_pages, +unsigned int *flush_flags) {unsigned long count;struct page_info *page;
@@ -71,6 +72,14 @@ static __init void mark_pv_pt_pages_rdonASSERT ((page->u.inuse.type_info & PGT_type_mask) <= PGT_root_page_table);ASSERT (!(page->u.inuse.type_info & ~(PGT_type_mask | PGT_pae_xen_l2))); +/* +* Page table pages need to be removed from the IOMMU again in case +* iommu_memory_setup() ended up mapping them. +*/ +if (need_iommu_pt_sync (d) && +iommu_unmap (d,_dfn (mfn_x (page_to_mfn (page))),1 ,0 , flush_flags) ) +BUG (); +/* Read-only mapping + PGC_allocated + page-table page. */ page->count_info = PGC_allocated |3 ; page->u.inuse.type_info |= PGT_validated |1 ;
@@ -107,11 +116,56 @@ static __init void mark_pv_pt_pages_rdonunmap_domain_page (pl3e); } +static void __init iommu_memory_setup (struct domain *d,const char *what, +struct page_info *page,unsigned long nr, +unsigned int *flush_flags) +{ +long rc; +mfn_t mfn =page_to_mfn (page); + +if ( !need_iommu_pt_sync (d) ) +return ; + +while ( (rc =iommu_map (d,_dfn (mfn_x (mfn)), mfn, nr, + IOMMUF_readable | IOMMUF_writable | IOMMUF_preempt, + flush_flags)) >0 ) + { +mfn_add (mfn, rc); + nr -= rc; +/* See comment below. */ +for ( ; rc--; ++page ) + { +ASSERT (!page->u.inuse.type_info); + page->u.inuse.type_info = PGT_writable_page | PGT_validated; + } +process_pending_softirqs (); + } +if ( rc ) + { +printk (XENLOG_ERR +"pre-mapping %s MFN [%lx,%lx) into IOMMU failed: %ld\n" , + what,mfn_x (mfn),mfn_x (mfn) + nr, rc); +return ; + } + +/* +* For successfully established IOMMU mappings the type of the page(s) +* needs to match (for _get_page_type() to unmap upon type change). Set +* the page(s) to writable with no type ref. +*/ +for ( ; nr--; ++page ) + { +ASSERT (!page->u.inuse.type_info); + page->u.inuse.type_info = PGT_writable_page | PGT_validated; + } +} +static __init void setup_pv_physmap (struct domain *d,unsigned long pgtbl_pfn,unsigned long v_start,unsigned long v_end,unsigned long vphysmap_start,unsigned long vphysmap_end, -unsigned long nr_pages) +unsigned long nr_pages, +unsigned int *flush_flags) {struct page_info *page =NULL ;l4_pgentry_t *pl4e, *l4start =map_domain_page (_mfn (pgtbl_pfn));
@@ -177,6 +231,10 @@ static __init void setup_pv_physmap(stru L3_PAGETABLE_SHIFT - PAGE_SHIFT, MEMF_no_scrub)) != NULL ) { +iommu_memory_setup (d,"P2M 1G" , page, +SUPERPAGE_PAGES *SUPERPAGE_PAGES , + flush_flags); + *pl3e =l3e_from_page (page,L1_PROT |_PAGE_DIRTY|_PAGE_PSE); vphysmap_start +=1UL <<L3_PAGETABLE_SHIFT ;continue ;
@@ -203,6 +261,9 @@ static __init void setup_pv_physmap(stru L2_PAGETABLE_SHIFT - PAGE_SHIFT, MEMF_no_scrub)) != NULL ) { +iommu_memory_setup (d,"P2M 2M" , page,SUPERPAGE_PAGES , + flush_flags); + *pl2e =l2e_from_page (page,L1_PROT |_PAGE_DIRTY|_PAGE_PSE); vphysmap_start +=1UL <<L2_PAGETABLE_SHIFT ;continue ;
@@ -311,6 +372,7 @@ int __init dom0_construct_pv(struct domaunsigned long initrd_pfn =-1 , initrd_mfn =0 ;unsigned long count;struct page_info *page =NULL ; +unsigned int flush_flags =0 ;start_info_t *si;struct vcpu *v = d->vcpu[0 ];void *image_base =bootstrap_map (image);
@@ -573,6 +635,9 @@ int __init dom0_construct_pv(struct domaBUG (); } initrd->mod_end =0 ; + +iommu_memory_setup (d,"initrd" ,mfn_to_page (_mfn (initrd_mfn)), +PFN_UP (initrd_len), &flush_flags); } printk("PHYSICAL MEMORY ARRANGEMENT:\n"
@@ -606,6 +671,13 @@ int __init dom0_construct_pv(struct domaprocess_pending_softirqs (); +/* +* Map the full range here and then punch holes for page tables +* alongside marking them as such in mark_pv_pt_pages_rdonly(). +*/ +iommu_memory_setup (d,"init-alloc" ,mfn_to_page (_mfn (alloc_spfn)), + alloc_epfn - alloc_spfn, &flush_flags); + mpt_alloc = (vpt_start - v_start) +pfn_to_paddr (alloc_spfn);if ( vinitrd_start ) mpt_alloc -=PAGE_ALIGN (initrd_len);
@@ -690,7 +762,8 @@ int __init dom0_construct_pv(struct doma l1tab++; page =mfn_to_page (_mfn (mfn)); -if ( !page->u.inuse.type_info && +if ( (!page->u.inuse.type_info || + page->u.inuse.type_info == (PGT_writable_page | PGT_validated)) && !get_page_and_type (page, d, PGT_writable_page) )BUG (); }
@@ -719,7 +792,7 @@ int __init dom0_construct_pv(struct doma }/* Pages that are part of page tables must be read only. */ -mark_pv_pt_pages_rdonly (d, l4start, vpt_start, nr_pt_pages); +mark_pv_pt_pages_rdonly (d, l4start, vpt_start, nr_pt_pages, &flush_flags);/* Mask all upcalls... */ for ( i =0 ; i <XEN_LEGACY_MAX_VCPUS ; i++ )
@@ -794,7 +867,7 @@ int __init dom0_construct_pv(struct doma { pfn =pagetable_get_pfn (v->arch.guest_table);setup_pv_physmap (d, pfn, v_start, v_end, vphysmap_start, vphysmap_end, - nr_pages); + nr_pages, &flush_flags); }/* Write the phys->machine and machine->phys table entries. */
@@ -825,7 +898,9 @@ int __init dom0_construct_pv(struct domaif (get_gpfn_from_mfn (mfn) >= count ) {BUG_ON (compat); -if ( !page->u.inuse.type_info && +if ( (!page->u.inuse.type_info || + page->u.inuse.type_info == (PGT_writable_page | + PGT_validated)) && !get_page_and_type (page, d, PGT_writable_page) )BUG ();
@@ -841,8 +916,12 @@ int __init dom0_construct_pv(struct doma#endif while ( pfn < nr_pages ) { -if ( (page =alloc_chunk (d, nr_pages -domain_tot_pages (d))) ==NULL ) + count =domain_tot_pages (d); +if ( (page =alloc_chunk (d, nr_pages - count)) ==NULL )panic ("Not enough RAM for DOM0 reservation\n" ); + +iommu_memory_setup (d,"chunk" , page,domain_tot_pages (d) - count, + &flush_flags);while ( pfn <domain_tot_pages (d) ) { mfn =mfn_x (page_to_mfn (page));
@@ -857,6 +936,10 @@ int __init dom0_construct_pv(struct doma } } +/* Use while() to avoid compiler warning. */ +while (iommu_iotlb_flush_all (d, flush_flags) ) +break ; +if ( initrd_len !=0 ) { si->mod_start = vinitrd_start ?: initrd_pfn;
xen/drivers/passthrough/x86/iommu.c +39 -13
78 lines not on this page · raw message
Re: [PATCH v7 02/14] IOMMU/x86: perform PV Dom0 mappings in batches
Quoted 12 lines · Jan Beulich
For large page mappings to be easily usable (i.e. in particular without un-shattering of smaller page mappings) and for mapping operations to then also be more efficient, pass batches of Dom0 memory to
iommu_map(). Indom0_construct_pv()and its helpers (covering strict mode) this additionally requires establishing the type of those pages (albeit with zero type references).The earlier establishing of
PGT_writable_page|PGT_validatedrequires the existing places where this gets done (throughget_page_and_type()) to be updated: For pages which actually have a mapping, the type refcount needs to be 1.
Quoted 7 lines
There is actually a related bug that gets fixed here as a side effect:
Typically the last L1 table would get marked as such only afterget_page_and_type(..., PGT_writable_page). While this is fine as far as refcounting goes, the page did remain mapped in the IOMMU in this case (when "iommu=dom0-strict").Signed-off-by Jan Beulich <[email protected]>
PATCH v7 3/14 IOMMU/x86: support freeing of pagetables
In reply to Jan BeulichFor vendor specific code to support superpages we need to be able to deal with a superpage mapping replacing an intermediate page table (or hierarchy thereof). Consequently an iommu_alloc_pgtable() counterpart is needed to free individual page tables while a domain is still alive. Since the freeing needs to be deferred until after a suitable IOTLB flush was performed, released page tables get queued for processing by a tasklet.
---
I was considering whether to use a softirq-tasklet instead. This would have the benefit of avoiding extra scheduling operations, but come with the risk of the freeing happening prematurely because of a process_pending_softirqs() somewhere.
---
v7: Add cf_check to free_queued_pgtables(). Add ASSERT().
v6: Extend comment on the use of process_pending_softirqs().
v5: Fix CPU_UP_PREPARE for BIGMEM. Schedule tasklet in CPU_DOWN_FAILED
when list is not empty. Skip all processing in CPU_DEAD when list is empty.
v4: Change type of iommu_queue_free_pgtable()'s 1st parameter. Re-base.
v3: Call process_pending_softirqs() from free_queued_pgtables().
xen/arch/x86/include/asm/iommu.h +1 -0
@@ -147,6 +147,7 @@ void iommu_free_domid(domid_t domid, unsint __must_check iommu_free_pgtables (struct domain *d);struct domain_iommu ;struct page_info *__must_check iommu_alloc_pgtable (struct domain_iommu *hd); +void iommu_queue_free_pgtable (struct domain_iommu *hd,struct page_info *pg);#endif /* !__ARCH_X86_IOMMU_H__ */ /*
xen/drivers/passthrough/x86/iommu.c +100 -0
112 lines not on this page · raw message
Re: [PATCH v7 03/14] IOMMU/x86: support freeing of pagetables
Quoted 10 lines · Jan Beulich
For vendor specific code to support superpages we need to be able to deal with a superpage mapping replacing an intermediate page table (or hierarchy thereof). Consequently an
iommu_alloc_pgtable()counterpart is needed to free individual page tables while a domain is still alive. Since the freeing needs to be deferred until after a suitable IOTLB flush was performed, released page tables get queued for processing by a tasklet.Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 4/14 IOMMU/x86: new command line option to suppress use of superpage mappings
In reply to Jan BeulichBefore actually enabling their use, provide a means to suppress it in case of problems. Note that using the option can also affect the sharing of page tables in the VT-d / EPT combination: If EPT would use large page mappings but the option is in effect, page table sharing would be suppressed (to properly fulfill the admin request).
---
v7: Fix condition in vtd_ept_page_compatible().
v6: New.
docs/misc/xen-command-line.pandoc +7 -1
@@ -1405,7 +1405,7 @@ detection of systems known to misbehave ### iommu = List of [ <bool>, verbose, debug, force, required, quarantine[=scratch-page], - sharept, intremap, intpost, crash-disable, + sharept, superpages, intremap, intpost, crash-disable, snoop, qinval, igfx, amd-iommu-perdev-intremap, dom0-{passthrough,strict} ]
@@ -1481,6 +1481,12 @@ boolean (e.g. `iommu=no`) can override t This option is ignored on ARM, and the pagetables are always shared. +* The `superpages` boolean controls whether superpage mappings may be used + in IOMMU page tables. If using this option is necessary to fix an issue, + please report a bug. + + This option is only valid on x86. + * The `intremap` boolean controls the Interrupt Remapping sub-feature, and is active by default on compatible hardware. On x86 systems, the first generation of IOMMUs only supported DMA remapping, and Interrupt Remapping
xen/arch/x86/include/asm/iommu.h +1 -1
@@ -132,7 +132,7 @@ extern bool untrusted_msi;int pi_update_irte (const struct pi_desc *pi_desc,const struct pirq *pirq,const uint8_t gvec); -extern bool iommu_non_coherent; +extern bool iommu_non_coherent, iommu_superpages;static inline void iommu_sync_cache (const void *addr,unsigned int size) {
xen/drivers/passthrough/iommu.c +2 -0
@@ -88,6 +88,8 @@ static int __init cf_check parse_iommu_p iommu_igfx = val; else if ( (val = parse_boolean("qinval", s, ss)) >= 0 ) iommu_qinval = val; + else if ( (val = parse_boolean("superpages", s, ss)) >= 0 ) + iommu_superpages = val;#endif else if ( (val = parse_boolean("verbose", s, ss)) >= 0 ) iommu_verbose = val;
xen/drivers/passthrough/vtd/iommu.c +4 -2
@@ -2213,8 +2213,10 @@ static bool __init vtd_ept_page_compatibif (rdmsr_safe (MSR_IA32_VMX_EPT_VPID_CAP , ept_cap) !=0 )return false; -return (ept_has_2mb (ept_cap) && opt_hap_2mb) <=cap_sps_2mb (vtd_cap) && - (ept_has_1gb (ept_cap) && opt_hap_1gb) <=cap_sps_1gb (vtd_cap); +return (ept_has_2mb (ept_cap) && opt_hap_2mb) <= + (cap_sps_2mb (vtd_cap) && iommu_superpages) && + (ept_has_1gb (ept_cap) && opt_hap_1gb) <= + (cap_sps_1gb (vtd_cap) && iommu_superpages); } static int cf_check intel_iommu_add_device(u8 devfn, struct pci_dev *pdev)
xen/drivers/passthrough/x86/iommu.c +6 -0
@@ -31,6 +31,7 @@const struct iommu_init_ops *__initdata iommu_init_ops;struct iommu_ops __ro_after_init iommu_ops;bool __read_mostly iommu_non_coherent; +bool __initdata iommu_superpages = true;enum iommu_intremap __read_mostly iommu_intremap = iommu_intremap_full;
@@ -104,8 +105,13 @@ int __init iommu_hardware_setup(void)mask_IO_APIC_setup (ioapic_entries); } +if ( !iommu_superpages ) + iommu_ops.page_sizes &=PAGE_SIZE_4K ; + rc = iommu_init_ops->setup (); +ASSERT (iommu_superpages || iommu_ops.page_sizes ==PAGE_SIZE_4K ); +if ( ioapic_entries ) {restore_IO_APIC_setup (ioapic_entries, rc);
RE: [PATCH v7 04/14] IOMMU/x86: new command line option to suppress use of superpage mappings
Quoted 11 lines
From: Jan Beulich <[email protected]>
Sent: Tuesday, July 5, 2022 8:45 PMBefore actually enabling their use, provide a means to suppress it in case of problems. Note that using the option can also affect the sharing of page tables in the VT-d / EPT combination: If EPT would use large page mappings but the option is in effect, page table sharing would be suppressed (to properly fulfill the admin request).
Requested-by Roger Pau Monné <[email protected]>Signed-off-by Jan Beulich <[email protected]>
Re: [PATCH v7 04/14] IOMMU/x86: new command line option to suppress use of superpage mappings
In reply to Jan BeulichQuoted 8 lines · Jan Beulich
Before actually enabling their use, provide a means to suppress it in case of problems. Note that using the option can also affect the sharing of page tables in the VT-d / EPT combination: If EPT would use large page mappings but the option is in effect, page table sharing would be suppressed (to properly fulfill the admin request).
Requested-by Roger Pau Monné <[email protected]>Signed-off-by Jan Beulich <[email protected]>
PATCH v7 5/14 AMD/IOMMU: allow use of superpage mappings
In reply to Jan BeulichNo separate feature flags exist which would control availability of these; the only restriction is HATS (establishing the maximum number of page table levels in general), and even that has a lower bound of 4. Thus we can unconditionally announce 2M and 1G mappings. (Via non- default page sizes the implementation in principle permits arbitrary size mappings, but these require multiple identical leaf PTEs to be written, which isn't all that different from having to write multiple consecutive PTEs with increasing frame numbers. IMO that's therefore beneficial only on hardware where suitable TLBs exist; I'm unaware of such hardware.)
Note that in principle 512G and 256T mappings could also be supported right away, but the freeing of page tables (to be introduced in subsequent patches) when replacing a sufficiently populated tree with a single huge page would need suitable preemption, which will require extra work.
---
v5: Drop PAGE_SIZE_512G. In amd_iommu_{,un}map_page() assert page order
is supported.
v4: Change type of queue_free_pt()'s 1st parameter. Re-base.
v3: Rename queue_free_pt()'s last parameter. Replace "level > 1" checks
where possible.
xen/drivers/passthrough/amd/iommu_map.c +49 -7
@@ -32,12 +32,13 @@ static unsigned int pfn_to_pde_idx(unsig }static union amd_iommu_pte clear_iommu_pte_present (unsigned long l1_mfn, -unsigned long dfn) +unsigned long dfn, +unsigned int level) {union amd_iommu_pte *table, *pte, old; table =map_domain_page (_mfn (l1_mfn)); - pte = &table[pfn_to_pde_idx (dfn,1 )]; + pte = &table[pfn_to_pde_idx (dfn, level)]; old = *pte;write_atomic (&pte->raw,0 );
@@ -351,15 +352,39 @@ static int iommu_pde_from_dfn(struct domreturn 0 ; } +static void queue_free_pt (struct domain_iommu *hd,mfn_t mfn,unsigned int level) +{ +if ( level >1 ) + { +union amd_iommu_pte *pt =map_domain_page (mfn); +unsigned int i; + +for ( i =0 ; i <PTE_PER_TABLE_SIZE ; ++i ) +if ( pt[i].pr && pt[i].next_level ) + { +ASSERT (pt[i].next_level < level); +queue_free_pt (hd,_mfn (pt[i].mfn), pt[i].next_level); + } + +unmap_domain_page (pt); + } + +iommu_queue_free_pgtable (hd,mfn_to_page (mfn)); +} + int cf_check amd_iommu_map_page(struct domain *d,dfn_t dfn,mfn_t mfn,unsigned int flags,unsigned int *flush_flags) {struct domain_iommu *hd =dom_iommu (d); +unsigned int level = (IOMMUF_order (flags) /PTE_PER_TABLE_SHIFT ) +1 ;int rc;unsigned long pt_mfn =0 ;union amd_iommu_pte old; +ASSERT ((hd->platform_ops->page_sizes >>IOMMUF_order (flags)) & +PAGE_SIZE_4K ); +spin_lock (&hd->arch.mapping_lock);/*
@@ -384,7 +409,7 @@ int cf_check amd_iommu_map_page(return rc; } -if (iommu_pde_from_dfn (d,dfn_x (dfn),1 , &pt_mfn, flush_flags, true) || +if (iommu_pde_from_dfn (d,dfn_x (dfn), level, &pt_mfn, flush_flags, true) || !pt_mfn ) {spin_unlock (&hd->arch.mapping_lock);
@@ -394,8 +419,8 @@ int cf_check amd_iommu_map_page(return -EFAULT ; } -/* Install 4k mapping */ - old =set_iommu_pte_present (pt_mfn,dfn_x (dfn),mfn_x (mfn),1 , +/* Install mapping */ + old =set_iommu_pte_present (pt_mfn,dfn_x (dfn),mfn_x (mfn), level, (flags & IOMMUF_writable), (flags & IOMMUF_readable));
@@ -403,8 +428,13 @@ int cf_check amd_iommu_map_page( *flush_flags |= IOMMU_FLUSHF_added;if ( old.pr ) + { *flush_flags |= IOMMU_FLUSHF_modified; +if (IOMMUF_order (flags) && old.next_level ) +queue_free_pt (hd,_mfn (old.mfn), old.next_level); + } +return 0 ; }
@@ -413,8 +443,15 @@ int cf_check amd_iommu_unmap_page( {unsigned long pt_mfn =0 ;struct domain_iommu *hd =dom_iommu (d); +unsigned int level = (order /PTE_PER_TABLE_SHIFT ) +1 ;union amd_iommu_pte old = {}; +/* +* While really we could unmap at any granularity, for now we assume unmaps +* are issued by common code only at the same granularity as maps. +*/ +ASSERT ((hd->platform_ops->page_sizes >> order) &PAGE_SIZE_4K ); +spin_lock (&hd->arch.mapping_lock);if ( !hd->arch.amd.root_table )
@@ -423,7 +460,7 @@ int cf_check amd_iommu_unmap_page(return 0 ; } -if (iommu_pde_from_dfn (d,dfn_x (dfn),1 , &pt_mfn, flush_flags, false) ) +if (iommu_pde_from_dfn (d,dfn_x (dfn), level, &pt_mfn, flush_flags, false) ) {spin_unlock (&hd->arch.mapping_lock); AMD_IOMMU_ERROR("invalid IO pagetable entry dfn = %"PRI_dfn"\n",
@@ -435,14 +472,19 @@ int cf_check amd_iommu_unmap_page(if ( pt_mfn ) {/* Mark PTE as 'page not present'. */ - old =clear_iommu_pte_present (pt_mfn,dfn_x (dfn)); + old =clear_iommu_pte_present (pt_mfn,dfn_x (dfn), level); }spin_unlock (&hd->arch.mapping_lock);if ( old.pr ) + { *flush_flags |= IOMMU_FLUSHF_modified; +if ( order && old.next_level ) +queue_free_pt (hd,_mfn (old.mfn), old.next_level); + } +return 0 ; }
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
@@ -747,7 +747,7 @@ static void cf_check amd_dump_page_table } static const struct iommu_ops __initconst_cf_clobber _iommu_ops = { - .page_sizes =PAGE_SIZE_4K , + .page_sizes =PAGE_SIZE_4K |PAGE_SIZE_2M |PAGE_SIZE_1G , .init = amd_iommu_domain_init, .hwdom_init = amd_iommu_hwdom_init, .quarantine_init = amd_iommu_quarantine_init,
xen/include/xen/page-defs.h +10 -0
@@ -21,4 +21,14 @@#define PAGE_MASK_64K PAGE_MASK_GRAN(64K)#define PAGE_ALIGN_64K (addr) PAGE_ALIGN_GRAN(64K, addr) +#define PAGE_SHIFT_2M 21 +#define PAGE_SIZE_2M PAGE_SIZE_GRAN(2M) +#define PAGE_MASK_2M PAGE_MASK_GRAN(2M) +#define PAGE_ALIGN_2M (addr) PAGE_ALIGN_GRAN(2M, addr) + +#define PAGE_SHIFT_1G 30 +#define PAGE_SIZE_1G PAGE_SIZE_GRAN(1G) +#define PAGE_MASK_1G PAGE_MASK_GRAN(1G) +#define PAGE_ALIGN_1G (addr) PAGE_ALIGN_GRAN(1G, addr) +#endif /* __XEN_PAGE_DEFS_H__ */
Re: [PATCH v7 05/14] AMD/IOMMU: allow use of superpage mappings
Quoted 11 lines · Jan Beulich
No separate feature flags exist which would control availability of these; the only restriction is HATS (establishing the maximum number of page table levels in general), and even that has a lower bound of 4. Thus we can unconditionally announce 2M and 1G mappings. (Via non- default page sizes the implementation in principle permits arbitrary size mappings, but these require multiple identical leaf PTEs to be written, which isn't all that different from having to write multiple consecutive PTEs with increasing frame numbers. IMO that's therefore beneficial only on hardware where suitable TLBs exist; I'm unaware of such hardware.)
Quoted 8 lines
Note that in principle 512G and 256T mappings could also be supported right away, but the freeing of page tables (to be introduced in subsequent patches) when replacing a sufficiently populated tree with a single huge page would need suitable preemption, which will require extra work.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 6/14 VT-d: allow use of superpage mappings
In reply to Jan Beulich... depending on feature availability (and absence of quirks).
Also make the page table dumping function aware of superpages.
---
v6: Re-base over addition of "iommu=no-superpages" command line option.
v5: In intel_iommu_{,un}map_page() assert page order is supported.
v4: Change type of queue_free_pt()'s 1st parameter. Re-base.
v3: Rename queue_free_pt()'s last parameter. Replace "level > 1" checks
where possible. Tighten assertion.
xen/drivers/passthrough/vtd/iommu.c +60 -8
@@ -779,18 +779,37 @@ static int __must_check cf_check iommu_freturn ret; } +static void queue_free_pt (struct domain_iommu *hd,mfn_t mfn,unsigned int level) +{ +if ( level >1 ) + { +struct dma_pte *pt =map_domain_page (mfn); +unsigned int i; + +for ( i =0 ; i <PTE_NUM ; ++i ) +if (dma_pte_present (pt[i]) && !dma_pte_superpage (pt[i]) ) +queue_free_pt (hd,maddr_to_mfn (dma_pte_addr (pt[i])), + level -1 ); + +unmap_domain_page (pt); + } + +iommu_queue_free_pgtable (hd,mfn_to_page (mfn)); +} +/* clear one page's page table */ static int dma_pte_clear_one (struct domain *domain,daddr_t addr,unsigned int order,unsigned int *flush_flags) {struct domain_iommu *hd =dom_iommu (domain); -struct dma_pte *page =NULL , *pte =NULL ; +struct dma_pte *page =NULL , *pte =NULL , old;u64 pg_maddr; +unsigned int level = (order /LEVEL_STRIDE ) +1 ;spin_lock (&hd->arch.mapping_lock); -/* get last level pte */ - pg_maddr =addr_to_dma_page_maddr (domain, addr,1 , flush_flags, false); +/* get target level pte */ + pg_maddr =addr_to_dma_page_maddr (domain, addr, level, flush_flags, false);if ( pg_maddr <PAGE_SIZE ) {spin_unlock (&hd->arch.mapping_lock);
@@ -798,7 +817,7 @@ static int dma_pte_clear_one(struct doma } page = (struct dma_pte *)map_vtd_domain_page (pg_maddr); - pte = page +address_level_offset (addr,1 ); + pte = &page[address_level_offset (addr, level)];if ( !dma_pte_present (*pte) ) {
@@ -807,14 +826,20 @@ static int dma_pte_clear_one(struct domareturn 0 ; } + old = *pte;dma_clear_pte (*pte); - *flush_flags |= IOMMU_FLUSHF_modified;spin_unlock (&hd->arch.mapping_lock);iommu_sync_cache (pte,sizeof (struct dma_pte ));unmap_vtd_domain_page (page); + *flush_flags |= IOMMU_FLUSHF_modified; + +if ( order && !dma_pte_superpage (old) ) +queue_free_pt (hd,maddr_to_mfn (dma_pte_addr (old)), + order /LEVEL_STRIDE ); +return 0 ; }
@@ -2092,8 +2117,12 @@ static int __must_check cf_check intel_istruct domain_iommu *hd =dom_iommu (d);struct dma_pte *page, *pte, old, new = {};u64 pg_maddr; +unsigned int level = (IOMMUF_order (flags) /LEVEL_STRIDE ) +1 ;int rc =0 ; +ASSERT ((hd->platform_ops->page_sizes >>IOMMUF_order (flags)) & +PAGE_SIZE_4K ); +/* Do nothing if VT-d shares EPT page table */ if (iommu_use_hap_pt (d) )return 0 ;
@@ -2116,7 +2145,7 @@ static int __must_check cf_check intel_ireturn 0 ; } - pg_maddr =addr_to_dma_page_maddr (d,dfn_to_daddr (dfn),1 , flush_flags, + pg_maddr =addr_to_dma_page_maddr (d,dfn_to_daddr (dfn), level, flush_flags, true);if ( pg_maddr <PAGE_SIZE ) {
@@ -2125,13 +2154,15 @@ static int __must_check cf_check intel_i } page = (struct dma_pte *)map_vtd_domain_page (pg_maddr); - pte = &page[dfn_x (dfn) &LEVEL_MASK ]; + pte = &page[address_level_offset (dfn_to_daddr (dfn), level)]; old = *pte;dma_set_pte_addr (new,mfn_to_maddr (mfn));dma_set_pte_prot (new, ((flags & IOMMUF_readable) ?DMA_PTE_READ :0 ) | ((flags & IOMMUF_writable) ?DMA_PTE_WRITE :0 )); +if (IOMMUF_order (flags) ) +dma_set_pte_superpage (new);/* Set the SNP on leaf page table if Snoop Control available */ if ( iommu_snoop )
@@ -2152,14 +2183,26 @@ static int __must_check cf_check intel_i *flush_flags |= IOMMU_FLUSHF_added;if (dma_pte_present (old) ) + { *flush_flags |= IOMMU_FLUSHF_modified; +if (IOMMUF_order (flags) && !dma_pte_superpage (old) ) +queue_free_pt (hd,maddr_to_mfn (dma_pte_addr (old)), +IOMMUF_order (flags) /LEVEL_STRIDE ); + } +return rc; } static int __must_check cf_check intel_iommu_unmap_page(struct domain *d,dfn_t dfn,unsigned int order,unsigned int *flush_flags) { +/* +* While really we could unmap at any granularity, for now we assume unmaps +* are issued by common code only at the same granularity as maps. +*/ +ASSERT ((dom_iommu (d)->platform_ops->page_sizes >> order) &PAGE_SIZE_4K ); +/* Do nothing if VT-d shares EPT page table */ if (iommu_use_hap_pt (d) )return 0 ;
@@ -2516,6 +2559,7 @@ static int __init cf_check vtd_setup(voi {struct acpi_drhd_unit *drhd;struct vtd_iommu *iommu; +unsigned int large_sizes = iommu_superpages ?PAGE_SIZE_2M |PAGE_SIZE_1G :0 ;int ret;bool reg_inval_supported = true;
@@ -2558,6 +2602,11 @@ static int __init cf_check vtd_setup(voicap_sps_2mb (iommu->cap) ?", 2MB" :"" , cap_sps_1gb(iommu->cap) ? ", 1GB" : ""); +if ( !cap_sps_2mb (iommu->cap) ) + large_sizes &= ~PAGE_SIZE_2M ; +if ( !cap_sps_1gb (iommu->cap) ) + large_sizes &= ~PAGE_SIZE_1G ; +#ifndef iommu_snoopif ( iommu_snoop && !ecap_snp_ctl (iommu->ecap) ) iommu_snoop = false;
@@ -2629,6 +2678,9 @@ static int __init cf_check vtd_setup(voiif ( ret ) goto error; +ASSERT (iommu_ops.page_sizes ==PAGE_SIZE_4K ); + iommu_ops.page_sizes |= large_sizes; +register_keyhandler ('V' , vtd_dump_iommu_info,"dump iommu info" ,1 );return 0 ;
@@ -2961,7 +3013,7 @@ static void vtd_dump_page_table_level(pacontinue ; address = gpa +offset_level_address (i, level); -if ( next_level >=1 ) +if ( next_level && !dma_pte_superpage (*pte) )vtd_dump_page_table_level (dma_pte_addr (*pte), next_level, address, indent +1 );else
Re: [PATCH v7 06/14] VT-d: allow use of superpage mappings
Quoted 7 lines · Jan Beulich
... depending on feature availability (and absence of quirks).
Also make the page table dumping function aware of superpages.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Kevin Tian <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 7/14 x86: introduce helper for recording degree of contiguity in page tables
In reply to Jan BeulichThis is a re-usable helper (kind of a template) which gets introduced without users so that the individual subsequent patches introducing such users can get committed independently of one another.
See the comment at the top of the new file. To demonstrate the effect, if a page table had just 16 entries, this would be the set of markers for a page table with fully contiguous mappings:
index 0 1 2 3 4 5 6 7 8 9 A B C D E F
marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0
"Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, but also a respective number of all non-present entries.
---
v5: Bail early from step 1 if possible. Arrange for consumers who are
just after CONTIG_{LEVEL_SHIFT,NR}. Extend comment.
v3: Rename function and header. Introduce IS_CONTIG().
v2: New.
xen/arch/x86/include/asm/pt-contig-markers.h new file +110 -0
@@ -0,0 +1,110 @@ +#ifndef __ASM_X86_PT_CONTIG_MARKERS_H +#define __ASM_X86_PT_CONTIG_MARKERS_H + +/* +* Short of having function templates in C, the function defined below is +* intended to be used by multiple parties interested in recording the +* degree of contiguity in mappings by a single page table. +* +* Scheme: Every entry records the order of contiguous successive entries, +* up to the maximum order covered by that entry (which is the number of +* clear low bits in its index, with entry 0 being the exception using +* the base-2 logarithm of the number of entries in a single page table). +* While a few entries need touching upon update, knowing whether the +* table is fully contiguous (and can hence be replaced by a higher level +* leaf entry) is then possible by simply looking at entry 0's marker. +* +* Prereqs: +* - CONTIG_MASK needs to be #define-d, to a value having at least 4 +* contiguous bits (ignored by hardware), before including this file (or +* else only CONTIG_LEVEL_SHIFT and CONTIG_NR will become available), +* - page tables to be passed to the helper need to be initialized with +* correct markers, +* - not-present entries need to be entirely clear except for the marker. +*/ + +/* This is the same for all anticipated users, so doesn't need passing in. */ +#define CONTIG_LEVEL_SHIFT 9 +#define CONTIG_NR (1 << CONTIG_LEVEL_SHIFT) + +#ifdef CONTIG_MASK + +#include <xen/bitops.h> +#include <xen/lib.h> +#include <xen/page-size.h> + +#define GET_MARKER (e) MASK_EXTR(e, CONTIG_MASK) +#define SET_MARKER (e, m) \ + ((void)((e) = ((e) & ~CONTIG_MASK) | MASK_INSR(m, CONTIG_MASK))) + +#define IS_CONTIG (kind, pt, i, idx, shift, b) \ + ((kind) == PTE_kind_leaf \ + ? (((pt)[i] ^ (pt)[idx]) & ~CONTIG_MASK) == (1ULL << ((b) + (shift))) \ + : !((pt)[i] & ~CONTIG_MASK)) + +enum PTE_kind { + PTE_kind_null, + PTE_kind_leaf, + PTE_kind_table, +}; + +static bool pt_update_contig_markers (uint64_t *pt,unsigned int idx, +unsigned int level,enum PTE_kind kind) +{ +unsigned int b, i = idx; +unsigned int shift = (level -1 ) *CONTIG_LEVEL_SHIFT +PAGE_SHIFT ; + +ASSERT (idx <CONTIG_NR ); +ASSERT (!(pt[idx] &CONTIG_MASK )); + +/* Step 1: Reduce markers in lower numbered entries. */ +while ( i ) + { + b =find_first_set_bit (i); + i &= ~(1U << b); +if (GET_MARKER (pt[i]) <= b ) +break ; +SET_MARKER (pt[i], b); + } + +/* An intermediate table is never contiguous with anything. */ +if ( kind == PTE_kind_table ) +return false; + +/* +* Present entries need in-sync index and address to be a candidate +* for being contiguous: What we're after is whether ultimately the +* intermediate table can be replaced by a superpage. +*/ +if ( kind != PTE_kind_null && + idx != ((pt[idx] >> shift) & (CONTIG_NR -1 )) ) +return false; + +/* Step 2: Check higher numbered entries for contiguity. */ +for ( b =0 ; b <CONTIG_LEVEL_SHIFT && !(idx & (1U << b)); ++b ) + { + i = idx | (1U << b); +if ( !IS_CONTIG (kind, pt, i, idx, shift, b) ||GET_MARKER (pt[i]) != b ) +break ; + } + +/* Step 3: Update markers in this and lower numbered entries. */ +for ( ;SET_MARKER (pt[idx], b), b <CONTIG_LEVEL_SHIFT ; ++b ) + { + i = idx ^ (1U << b); +if ( !IS_CONTIG (kind, pt, i, idx, shift, b) ||GET_MARKER (pt[i]) != b ) +break ; + idx &= ~(1U << b); + } + +return b ==CONTIG_LEVEL_SHIFT ; +} + +#undef IS_CONTIG +#undef SET_MARKER +#undef GET_MARKER +#undef CONTIG_MASK + +#endif /* CONTIG_MASK */ + +#endif /* __ASM_X86_PT_CONTIG_MARKERS_H */
Re: [PATCH v7 07/14] x86: introduce helper for recording degree of contiguity in page tables
Quoted 11 lines · Jan Beulich
This is a re-usable helper (kind of a template) which gets introduced without users so that the individual subsequent patches introducing such users can get committed independently of one another.
See the comment at the top of the new file. To demonstrate the effect, if a page table had just 16 entries, this would be the set of markers for a page table with fully contiguous mappings:
index 0 1 2 3 4 5 6 7 8 9 A B C D E F
marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0
Quoted 6 lines
"Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, but also a respective number of all non-present entries.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 8/14 IOMMU/x86: prefill newly allocate page tables
In reply to Jan BeulichPage tables are used for two purposes after allocation: They either start out all empty, or they are filled to replace a superpage. Subsequently, to replace all empty or fully contiguous page tables, contiguous sub-regions will be recorded within individual page tables. Install the initial set of markers immediately after allocation. Make sure to retain these markers when further populating a page table in preparation for it to replace a superpage.
The markers are simply 4-bit fields holding the order value of contiguous entries. To demonstrate this, if a page table had just 16 entries, this would be the initial (fully contiguous) set of markers:
index 0 1 2 3 4 5 6 7 8 9 A B C D E F
marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0
"Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, and identical attributes, but also a respective number of all non-present (zero except for the markers) entries.
---
An alternative to the ASSERT()s added to set_iommu_ptes_present() would be to make the function less general-purpose; it's used in a single place only after all (i.e. it might as well be folded into its only caller).
While in VT-d's comment ahead of struct dma_pte I'm adjusting the description of the high bits, I'd like to note that the description of some of the lower bits isn't correct either. Yet I don't think adjusting that belongs here.
---
v6: Use sizeof().
v5: Assert next_mfn is suitably aligned in set_iommu_ptes_present(). Use
CONTIG_LEVEL_SHIFT in favor of PAGE_SHIFT-3.
v4: Add another comment referring to pt-contig-markers.h. Re-base.
v3: Add comments. Re-base.
v2: New.
xen/arch/x86/include/asm/iommu.h +2 -1
@@ -146,7 +146,8 @@ void iommu_free_domid(domid_t domid, unsint __must_check iommu_free_pgtables (struct domain *d);struct domain_iommu ; -struct page_info *__must_check iommu_alloc_pgtable (struct domain_iommu *hd); +struct page_info *__must_check iommu_alloc_pgtable (struct domain_iommu *hd, +uint64_t contig_mask);void iommu_queue_free_pgtable (struct domain_iommu *hd,struct page_info *pg);#endif /* !__ARCH_X86_IOMMU_H__ */
xen/drivers/passthrough/amd/iommu-defs.h +3 -1
@@ -446,11 +446,13 @@ union amd_iommu_x2apic_control {#define IOMMU_PAGE_TABLE_U32_PER_ENTRY (IOMMU_PAGE_TABLE_ENTRY_SIZE / 4)#define IOMMU_PAGE_TABLE_ALIGNMENT 4096 +#define IOMMU_PTE_CONTIG_MASK 0x1e/* The ign0 field below. */ +union amd_iommu_pte {uint64_t raw;struct {bool pr:1 ; -unsigned int ign0:4 ; +unsigned int ign0:4 ;/* Covered by IOMMU_PTE_CONTIG_MASK. */ bool a:1 ;bool d:1 ;unsigned int ign1:2 ;
xen/drivers/passthrough/amd/iommu_map.c +21 -5
@@ -21,6 +21,8 @@#include "iommu.h" +#include <asm/pt-contig-markers.h> +/* Given pfn and page table level, return pde index */ static unsigned int pfn_to_pde_idx (unsigned long pfn,unsigned int level) {
@@ -113,9 +115,23 @@ static void set_iommu_ptes_present(unsigreturn ; } +ASSERT (!(next_mfn & (page_sz -1 ))); +while ( nr_ptes-- ) { -set_iommu_pde_present (pde, next_mfn,0 , iw, ir); +ASSERT (!pde->next_level); +ASSERT (!pde->u); + +if ( pde > table ) +ASSERT (pde->ign0 ==find_first_set_bit (pde - table)); +else +ASSERT (pde->ign0 ==CONTIG_LEVEL_SHIFT ); + + pde->iw = iw; + pde->ir = ir; + pde->fc = true;/* See set_iommu_pde_present(). */ + pde->mfn = next_mfn; + pde->pr = true; ++pde; next_mfn += page_sz;
@@ -295,7 +311,7 @@ static int iommu_pde_from_dfn(struct dom mfn = next_table_mfn;/* allocate lower level page table */ - table =iommu_alloc_pgtable (hd); + table =iommu_alloc_pgtable (hd,IOMMU_PTE_CONTIG_MASK );if ( table ==NULL ) {AMD_IOMMU_ERROR ("cannot allocate I/O page table\n" );
@@ -325,7 +341,7 @@ static int iommu_pde_from_dfn(struct domif ( next_table_mfn ==0 ) { - table =iommu_alloc_pgtable (hd); + table =iommu_alloc_pgtable (hd,IOMMU_PTE_CONTIG_MASK );if ( table ==NULL ) {AMD_IOMMU_ERROR ("cannot allocate I/O page table\n" );
@@ -726,7 +742,7 @@ static int fill_qpt(union amd_iommu_pte* page table pages, and the resulting allocations are always * zeroed. */ - pgs[level] =iommu_alloc_pgtable (hd); + pgs[level] =iommu_alloc_pgtable (hd,0 );if ( !pgs[level] ) { rc = -ENOMEM ;
@@ -784,7 +800,7 @@ int cf_check amd_iommu_quarantine_init(sreturn 0 ; } - pdev->arch.amd.root_table =iommu_alloc_pgtable (hd); + pdev->arch.amd.root_table =iommu_alloc_pgtable (hd,0 );if ( !pdev->arch.amd.root_table )return -ENOMEM ;
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
@@ -342,7 +342,7 @@ int amd_iommu_alloc_root(struct domain *if (unlikely (!hd->arch.amd.root_table) && d != dom_io ) { - hd->arch.amd.root_table =iommu_alloc_pgtable (hd); + hd->arch.amd.root_table =iommu_alloc_pgtable (hd,0 );if ( !hd->arch.amd.root_table )return -ENOMEM ; }
xen/drivers/passthrough/vtd/iommu.c +8 -7
@@ -334,7 +334,7 @@ static uint64_t addr_to_dma_page_maddr(s goto out; pte_maddr = level; -if ( !(pg =iommu_alloc_pgtable (hd)) ) +if ( !(pg =iommu_alloc_pgtable (hd,0 )) ) goto out; hd->arch.vtd.pgd_maddr =page_to_maddr (pg);
@@ -376,7 +376,7 @@ static uint64_t addr_to_dma_page_maddr(s } pte_maddr = level -1 ; - pg =iommu_alloc_pgtable (hd); + pg =iommu_alloc_pgtable (hd,DMA_PTE_CONTIG_MASK );if ( !pg )break ;
@@ -388,12 +388,13 @@ static uint64_t addr_to_dma_page_maddr(sstruct dma_pte *split =map_vtd_domain_page (pte_maddr);unsigned long inc =1UL <<level_to_offset_bits (level -1 ); - split[0 ].val = pte->val; + split[0 ].val |= pte->val & ~DMA_PTE_CONTIG_MASK ;if ( inc ==PAGE_SIZE ) split[0 ].val &= ~DMA_PTE_SP ;for ( offset =1 ; offset <PTE_NUM ; ++offset ) - split[offset].val = split[offset -1 ].val + inc; + split[offset].val |= + (split[offset -1 ].val & ~DMA_PTE_CONTIG_MASK ) + inc;iommu_sync_cache (split,PAGE_SIZE );unmap_vtd_domain_page (split);
@@ -2168,7 +2169,7 @@ static int __must_check cf_check intel_iif ( iommu_snoop )dma_set_pte_snp (new); -if ( old.val == new.val ) +if ( !((old.val ^ new.val) & ~DMA_PTE_CONTIG_MASK ) ) {spin_unlock (&hd->arch.mapping_lock);unmap_vtd_domain_page (page);
@@ -3058,7 +3059,7 @@ static int fill_qpt(struct dma_pte *this* page table pages, and the resulting allocations are always * zeroed. */ - pgs[level] =iommu_alloc_pgtable (hd); + pgs[level] =iommu_alloc_pgtable (hd,0 );if ( !pgs[level] ) { rc = -ENOMEM ;
@@ -3115,7 +3116,7 @@ static int cf_check intel_iommu_quarantiif ( !drhd )return -ENODEV ; - pg =iommu_alloc_pgtable (hd); + pg =iommu_alloc_pgtable (hd,0 );if ( !pg )return -ENOMEM ;
xen/drivers/passthrough/vtd/iommu.h +6 -2
@@ -253,7 +253,10 @@ struct context_entry {* 2-6: reserved * 7: super page * 8-11: available -* 12-63: Host physcial address +* 12-51: Host physcial address +* 52-61: available (52-55 used for DMA_PTE_CONTIG_MASK) +* 62: reserved +* 63: available */ struct dma_pte {u64 val;
@@ -263,6 +266,7 @@ struct dma_pte {#define DMA_PTE_PROT (DMA_PTE_READ | DMA_PTE_WRITE)#define DMA_PTE_SP (1 << 7)#define DMA_PTE_SNP (1 << 11) +#define DMA_PTE_CONTIG_MASK (0xfull << PADDR_BITS)#define dma_clear_pte (p) do {(p).val = 0;} while(0)#define dma_set_pte_readable (p) do {(p).val |= DMA_PTE_READ;} while(0)#define dma_set_pte_writable (p) do {(p).val |= DMA_PTE_WRITE;} while(0)
@@ -276,7 +280,7 @@ struct dma_pte {#define dma_pte_write (p) (dma_pte_prot(p) & DMA_PTE_WRITE)#define dma_pte_addr (p) ((p).val & PADDR_MASK & PAGE_MASK_4K)#define dma_set_pte_addr (p, addr) do {\ - (p).val |= ((addr) & PAGE_MASK_4K); } while (0) + (p).val |= ((addr) & PADDR_MASK & PAGE_MASK_4K); } while (0)#define dma_pte_present (p) (((p).val & DMA_PTE_PROT) != 0)#define dma_pte_superpage (p) (((p).val & DMA_PTE_SP) != 0)
xen/drivers/passthrough/x86/iommu.c +27 -3
@@ -26,6 +26,7 @@#include <asm/hvm/io.h> #include <asm/io_apic.h> #include <asm/mem_paging.h> +#include <asm/pt-contig-markers.h> #include <asm/setup.h> const struct iommu_init_ops *__initdata iommu_init_ops;
@@ -534,11 +535,12 @@ int iommu_free_pgtables(struct domain *dreturn 0 ; } -struct page_info *iommu_alloc_pgtable (struct domain_iommu *hd) +struct page_info *iommu_alloc_pgtable (struct domain_iommu *hd, +uint64_t contig_mask) {unsigned int memflags =0 ;struct page_info *pg; -void *p; +uint64_t *p;#ifdef CONFIG_NUMA if ( hd->node !=NUMA_NO_NODE )
@@ -550,7 +552,29 @@ struct page_info *iommu_alloc_pgtable(streturn NULL ; p =__map_domain_page (pg); -clear_page (p); + +if ( contig_mask ) + { +/* See pt-contig-markers.h for a description of the marker scheme. */ +unsigned int i, shift =find_first_set_bit (contig_mask); + +ASSERT ((CONTIG_LEVEL_SHIFT & (contig_mask >> shift)) ==CONTIG_LEVEL_SHIFT ); + + p[0 ] = (CONTIG_LEVEL_SHIFT +0ull ) << shift; + p[1 ] =0 ; + p[2 ] =1ull << shift; + p[3 ] =0 ; + +for ( i =4 ; i <PAGE_SIZE /sizeof (*p); i +=4 ) + { + p[i +0 ] = (find_first_set_bit (i) +0ull ) << shift; + p[i +1 ] =0 ; + p[i +2 ] =1ull << shift; + p[i +3 ] =0 ; + } + } +else +clear_page (p);iommu_sync_cache (p,PAGE_SIZE );
Re: [PATCH v7 08/14] IOMMU/x86: prefill newly allocate page tables
Quoted 15 lines · Jan Beulich
Page tables are used for two purposes after allocation: They either start out all empty, or they are filled to replace a superpage. Subsequently, to replace all empty or fully contiguous page tables, contiguous sub-regions will be recorded within individual page tables. Install the initial set of markers immediately after allocation. Make sure to retain these markers when further populating a page table in preparation for it to replace a superpage.
The markers are simply 4-bit fields holding the order value of contiguous entries. To demonstrate this, if a page table had just 16 entries, this would be the initial (fully contiguous) set of markers:
index 0 1 2 3 4 5 6 7 8 9 A B C D E F
marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0
Quoted 8 lines
"Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, and identical attributes, but also a respective number of all non-present (zero except for the markers) entries.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Kevin Tian <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 9/14 AMD/IOMMU: free all-empty page tables
In reply to Jan BeulichWhen a page table ends up with no present entries left, it can be replaced by a non-present entry at the next higher level. The page table itself can then be scheduled for freeing.
Note that while its output isn't used there yet, pt_update_contig_markers() right away needs to be called in all places where entries get updated, not just the one where entries get cleared.
---
v5: Re-base over changes earlier in the series.
v4: Re-base over changes earlier in the series.
v3: Re-base over changes earlier in the series.
v2: New.
xen/drivers/passthrough/amd/iommu_map.c +34 -3
@@ -21,6 +21,7 @@#include "iommu.h" +#define CONTIG_MASK IOMMU_PTE_CONTIG_MASK#include <asm/pt-contig-markers.h> /* Given pfn and page table level, return pde index */
@@ -35,16 +36,20 @@ static unsigned int pfn_to_pde_idx(unsigstatic union amd_iommu_pte clear_iommu_pte_present (unsigned long l1_mfn,unsigned long dfn, -unsigned int level) +unsigned int level, +bool *free) {union amd_iommu_pte *table, *pte, old; +unsigned int idx =pfn_to_pde_idx (dfn, level); table =map_domain_page (_mfn (l1_mfn)); - pte = &table[pfn_to_pde_idx (dfn, level)]; + pte = &table[idx]; old = *pte;write_atomic (&pte->raw,0 ); + *free =pt_update_contig_markers (&table->raw, idx, level, PTE_kind_null); +unmap_domain_page (table);return old;
@@ -87,7 +92,11 @@ static union amd_iommu_pte set_iommu_pteif ( !old.pr || old.next_level || old.mfn != next_mfn || old.iw != iw || old.ir != ir ) + {set_iommu_pde_present (pde, next_mfn,0 , iw, ir); +pt_update_contig_markers (&table->raw,pfn_to_pde_idx (dfn, level), + level, PTE_kind_leaf); + }else old.pr = false;/* signal "no change" to the caller */
@@ -326,6 +335,9 @@ static int iommu_pde_from_dfn(struct domsmp_wmb ();set_iommu_pde_present (pde, next_table_mfn, next_level, true, true); +pt_update_contig_markers (&next_table_vaddr->raw, +pfn_to_pde_idx (dfn, level), + level, PTE_kind_table); *flush_flags |= IOMMU_FLUSHF_modified; }
@@ -351,6 +363,9 @@ static int iommu_pde_from_dfn(struct dom next_table_mfn =mfn_x (page_to_mfn (table));set_iommu_pde_present (pde, next_table_mfn, next_level, true, true); +pt_update_contig_markers (&next_table_vaddr->raw, +pfn_to_pde_idx (dfn, level), + level, PTE_kind_table); }else /* should never reach here */ {
@@ -487,8 +502,24 @@ int cf_check amd_iommu_unmap_page(if ( pt_mfn ) { +bool free; +/* Mark PTE as 'page not present'. */ - old =clear_iommu_pte_present (pt_mfn,dfn_x (dfn), level); + old =clear_iommu_pte_present (pt_mfn,dfn_x (dfn), level, &free); + +while (unlikely (free) && ++level < hd->arch.amd.paging_mode ) + { +struct page_info *pg =mfn_to_page (_mfn (pt_mfn)); + +if (iommu_pde_from_dfn (d,dfn_x (dfn), level, &pt_mfn, + flush_flags, false) ) +BUG (); +BUG_ON (!pt_mfn); + +clear_iommu_pte_present (pt_mfn,dfn_x (dfn), level, &free); + *flush_flags |= IOMMU_FLUSHF_all; +iommu_queue_free_pgtable (hd, pg); + } }spin_unlock (&hd->arch.mapping_lock);
Re: [PATCH v7 09/14] AMD/IOMMU: free all-empty page tables
Quoted 10 lines · Jan Beulich
When a page table ends up with no present entries left, it can be replaced by a non-present entry at the next higher level. The page table itself can then be scheduled for freeing.
Note that while its output isn't used there yet,
pt_update_contig_markers()right away needs to be called in all places where entries get updated, not just the one where entries get cleared.Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 10/14 VT-d: free all-empty page tables
In reply to Jan BeulichWhen a page table ends up with no present entries left, it can be replaced by a non-present entry at the next higher level. The page table itself can then be scheduled for freeing.
Note that while its output isn't used there yet, pt_update_contig_markers() right away needs to be called in all places where entries get updated, not just the one where entries get cleared.
Note further that while pt_update_contig_markers() updates perhaps several PTEs within the table, since these are changes to "avail" bits only I do not think that cache flushing would be needed afterwards. Such cache flushing (of entire pages, unless adding yet more logic to me more selective) would be quite noticable performance-wise (very prominent during Dom0 boot).
Also note that cache sync-ing is likely more strict than necessary. This is both to be on the safe side as well as to maintain the pattern of all updates of (potentially) live tables being accompanied by a flush (if so needed).
---
v4: Re-base over changes earlier in the series.
v3: Properly bound loop. Re-base over changes earlier in the series.
v2: New.
---
The hang during boot on my Latitude E6410 (see the respective code comment) was pretty close after iommu_enable_translation(). No errors, no watchdog would kick in, just sometimes the first few pixel lines of the next log message's (XEN) prefix would have made it out to the screen (and there's no serial there). It's been a lot of experimenting until I figured the workaround (which I consider ugly, but halfway acceptable). I've been trying hard to make sure the workaround wouldn't be masking a real issue, yet I'm still wary of it possibly doing so ... My best guess at this point is that on these old IOMMUs the ignored bits 52...61 aren't really ignored for present entries, but also aren't "reserved" enough to trigger faults. This guess is from having tried to set other bits in this range (unconditionally, and with the workaround here in place), which yielded the same behavior.
xen/drivers/passthrough/vtd/iommu.c +43 -2
@@ -43,6 +43,9 @@#include "vtd.h" #include "../ats.h" +#define CONTIG_MASK DMA_PTE_CONTIG_MASK +#include <asm/pt-contig-markers.h> +/* dom_io is used as a sentinel for quarantined devices */ #define QUARANTINE_SKIP (d, pgd_maddr) ((d) == dom_io && !(pgd_maddr))#define DEVICE_DOMID (d, pdev) ((d) != dom_io ? (d)->domain_id \
@@ -405,6 +408,9 @@ static uint64_t addr_to_dma_page_maddr(swrite_atomic (&pte->val, new_pte.val);iommu_sync_cache (pte,sizeof (struct dma_pte )); +pt_update_contig_markers (&parent->val, +address_level_offset (addr, level), + level, PTE_kind_table); }if ( --level == target )
@@ -829,9 +835,31 @@ static int dma_pte_clear_one(struct doma old = *pte;dma_clear_pte (*pte); +iommu_sync_cache (pte,sizeof (*pte)); + +while (pt_update_contig_markers (&page->val, +address_level_offset (addr, level), + level, PTE_kind_null) && + ++level < min_pt_levels ) + { +struct page_info *pg =maddr_to_page (pg_maddr); + +unmap_vtd_domain_page (page); + + pg_maddr =addr_to_dma_page_maddr (domain, addr, level, flush_flags, + false); +BUG_ON (pg_maddr <PAGE_SIZE ); + + page =map_vtd_domain_page (pg_maddr); + pte = &page[address_level_offset (addr, level)]; +dma_clear_pte (*pte); +iommu_sync_cache (pte,sizeof (*pte)); + + *flush_flags |= IOMMU_FLUSHF_all; +iommu_queue_free_pgtable (hd, pg); + }spin_unlock (&hd->arch.mapping_lock); -iommu_sync_cache (pte,sizeof (struct dma_pte ));unmap_vtd_domain_page (page);
@@ -2177,8 +2205,21 @@ static int __must_check cf_check intel_i } *pte = new; -iommu_sync_cache (pte,sizeof (struct dma_pte )); + +/* +* While the (ab)use of PTE_kind_table here allows to save some work in +* the function, the main motivation for it is that it avoids a so far +* unexplained hang during boot (while preparing Dom0) on a Westmere +* based laptop. +*/ +pt_update_contig_markers (&page->val, +address_level_offset (dfn_to_daddr (dfn), level), + level, + (hd->platform_ops->page_sizes & + (1UL <<level_to_offset_bits (level +1 )) + ? PTE_kind_leaf : PTE_kind_table)); +spin_unlock (&hd->arch.mapping_lock);unmap_vtd_domain_page (page);
Re: [PATCH v7 10/14] VT-d: free all-empty page tables
Quoted 15 lines · Jan Beulich
When a page table ends up with no present entries left, it can be replaced by a non-present entry at the next higher level. The page table itself can then be scheduled for freeing.
Note that while its output isn't used there yet,
pt_update_contig_markers()right away needs to be called in all places where entries get updated, not just the one where entries get cleared.Note further that while
pt_update_contig_markers()updates perhaps several PTEs within the table, since these are changes to "avail" bits only I do not think that cache flushing would be needed afterwards. Such cache flushing (of entire pages, unless adding yet more logic to me more selective) would be quite noticable performance-wise (very prominent during Dom0 boot).
Quoted 8 lines
Also note that cache sync-ing is likely more strict than necessary. This is both to be on the safe side as well as to maintain the pattern of all updates of (potentially) live tables being accompanied by a flush (if so needed).
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Kevin Tian <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 11/14 AMD/IOMMU: replace all-contiguous page tables by superpage mappings
In reply to Jan BeulichWhen a page table ends up with all contiguous entries (including all identical attributes), it can be replaced by a superpage entry at the next higher level. The page table itself can then be scheduled for freeing.
---
Unlike the freeing of all-empty page tables, this causes quite a bit of back and forth for PV domains, due to their mapping/unmapping of pages when they get converted to/from being page tables. It may therefore be worth considering to delay re-coalescing a little, to avoid doing so when the superpage would otherwise get split again pretty soon. But I think this would better be the subject of a separate change anyway.
Of course this could also be helped by more "aware" kernel side
behavior: They could avoid immediately mapping freed page tables writable again, in anticipation of re-using that same page for another page table elsewhere.
---
v4: Re-base over changes earlier in the series.
v3: New.
xen/drivers/passthrough/amd/iommu_map.c +29 -5
@@ -81,7 +81,8 @@ static union amd_iommu_pte set_iommu_pteunsigned long dfn, unsigned long next_mfn, unsigned int level, - bool iw, bool ir) + bool iw, bool ir, + bool *contig) { union amd_iommu_pte *table, *pde, old;
@@ -94,11 +95,15 @@ static union amd_iommu_pte set_iommu_pte old.iw != iw || old.ir != ir ) {set_iommu_pde_present (pde, next_mfn,0 , iw, ir); -pt_update_contig_markers (&table->raw,pfn_to_pde_idx (dfn, level), - level, PTE_kind_leaf); + *contig =pt_update_contig_markers (&table->raw, +pfn_to_pde_idx (dfn, level), + level, PTE_kind_leaf); } else + { old.pr = false;/* signal "no change" to the caller */ + *contig = false; + }unmap_domain_page (table);
@@ -409,6 +414,7 @@ int cf_check amd_iommu_map_page( {struct domain_iommu *hd =dom_iommu (d);unsigned int level = (IOMMUF_order (flags) /PTE_PER_TABLE_SHIFT ) +1 ; +bool contig;int rc;unsigned long pt_mfn =0 ;union amd_iommu_pte old;
@@ -452,8 +458,26 @@ int cf_check amd_iommu_map_page(/* Install mapping */ old =set_iommu_pte_present (pt_mfn,dfn_x (dfn),mfn_x (mfn), level, - (flags & IOMMUF_writable), - (flags & IOMMUF_readable)); + flags & IOMMUF_writable, + flags & IOMMUF_readable, &contig); + +while (unlikely (contig) && ++level < hd->arch.amd.paging_mode ) + { +struct page_info *pg =mfn_to_page (_mfn (pt_mfn)); +unsigned long next_mfn; + +if (iommu_pde_from_dfn (d,dfn_x (dfn), level, &pt_mfn, flush_flags, + false) ) +BUG (); +BUG_ON (!pt_mfn); + + next_mfn =mfn_x (mfn) & (~0UL << (PTE_PER_TABLE_SHIFT * (level -1 ))); +set_iommu_pte_present (pt_mfn,dfn_x (dfn), next_mfn, level, + flags & IOMMUF_writable, + flags & IOMMUF_readable, &contig); + *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all; +iommu_queue_free_pgtable (hd, pg); + }spin_unlock (&hd->arch.mapping_lock);
Re: [PATCH v7 11/14] AMD/IOMMU: replace all-contiguous page tables by superpage mappings
Quoted 7 lines · Jan Beulich
When a page table ends up with all contiguous entries (including all identical attributes), it can be replaced by a superpage entry at the next higher level. The page table itself can then be scheduled for freeing.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 12/14 VT-d: replace all-contiguous page tables by superpage mappings
In reply to Jan BeulichWhen a page table ends up with all contiguous entries (including all identical attributes), it can be replaced by a superpage entry at the next higher level. The page table itself can then be scheduled for freeing.
The adjustment to LEVEL_MASK is merely to avoid leaving a latent trap for whenever we (and obviously hardware) start supporting 512G mappings.
Note that cache sync-ing is likely more strict than necessary. This is both to be on the safe side as well as to maintain the pattern of all updates of (potentially) live tables being accompanied by a flush (if so needed).
---
Unlike the freeing of all-empty page tables, this causes quite a bit of back and forth for PV domains, due to their mapping/unmapping of pages when they get converted to/from being page tables. It may therefore be worth considering to delay re-coalescing a little, to avoid doing so when the superpage would otherwise get split again pretty soon. But I think this would better be the subject of a separate change anyway.
Of course this could also be helped by more "aware" kernel side
behavior: They could avoid immediately mapping freed page tables writable again, in anticipation of re-using that same page for another page table elsewhere.
---
v4: Re-base over changes earlier in the series.
v3: New.
xen/drivers/passthrough/vtd/iommu.c +28 -7
@@ -2211,14 +2211,35 @@ static int __must_check cf_check intel_i* While the (ab)use of PTE_kind_table here allows to save some work in * the function, the main motivation for it is that it avoids a so far * unexplained hang during boot (while preparing Dom0) on a Westmere -* based laptop. +* based laptop. This also has the intended effect of terminating the +* loop when super pages aren't supported anymore at the next level. */ -pt_update_contig_markers (&page->val, -address_level_offset (dfn_to_daddr (dfn), level), - level, - (hd->platform_ops->page_sizes & - (1UL <<level_to_offset_bits (level +1 )) - ? PTE_kind_leaf : PTE_kind_table)); +while (pt_update_contig_markers (&page->val, +address_level_offset (dfn_to_daddr (dfn), level), + level, + (hd->platform_ops->page_sizes & + (1UL <<level_to_offset_bits (level +1 )) + ? PTE_kind_leaf : PTE_kind_table)) ) + { +struct page_info *pg =maddr_to_page (pg_maddr); + +unmap_vtd_domain_page (page); + + new.val &= ~(LEVEL_MASK <<level_to_offset_bits (level)); +dma_set_pte_superpage (new); + + pg_maddr =addr_to_dma_page_maddr (d,dfn_to_daddr (dfn), ++level, + flush_flags, false); +BUG_ON (pg_maddr <PAGE_SIZE ); + + page =map_vtd_domain_page (pg_maddr); + pte = &page[address_level_offset (dfn_to_daddr (dfn), level)]; + *pte = new; +iommu_sync_cache (pte,sizeof (*pte)); + + *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all; +iommu_queue_free_pgtable (hd, pg); + }spin_unlock (&hd->arch.mapping_lock);unmap_vtd_domain_page (page);
xen/drivers/passthrough/vtd/iommu.h +1 -1
@@ -232,7 +232,7 @@ struct context_entry {/* page table handling */ #define LEVEL_STRIDE (9) -#define LEVEL_MASK ((1 << LEVEL_STRIDE) - 1) +#define LEVEL_MASK (PTE_NUM - 1UL)#define PTE_NUM (1 << LEVEL_STRIDE)#define level_to_agaw (val) ((val) - 2)#define agaw_to_level (val) ((val) + 2)
Re: [PATCH v7 12/14] VT-d: replace all-contiguous page tables by superpage mappings
Quoted 8 lines · Jan Beulich
When a page table ends up with all contiguous entries (including all identical attributes), it can be replaced by a superpage entry at the next higher level. The page table itself can then be scheduled for freeing.
The adjustment to
LEVEL_MASKis merely to avoid leaving a latent trap for whenever we (and obviously hardware) start supporting 512G mappings.
Quoted 8 lines
Note that cache sync-ing is likely more strict than necessary. This is both to be on the safe side as well as to maintain the pattern of all updates of (potentially) live tables being accompanied by a flush (if so needed).
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Kevin Tian <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
PATCH v7 13/14 IOMMU/x86: add perf counters for page table splitting / coalescing
In reply to Jan Beulich---
v3: New.
xen/arch/x86/include/asm/perfc_defn.h +3 -0
@@ -125,4 +125,7 @@ PERFCOUNTER(realmode_exits, "vmexit PERFCOUNTER(pauseloop_exits, "vmexits from Pause-Loop Detection") +PERFCOUNTER(iommu_pt_shatters, "IOMMU page table shatters") +PERFCOUNTER (iommu_pt_coalesces,"IOMMU page table coalesces" ) +/*#endif*/ /* __XEN_PERFC_DEFN_H__ */
xen/drivers/passthrough/amd/iommu_map.c +4 -0
@@ -345,6 +345,8 @@ static int iommu_pde_from_dfn(struct dom level, PTE_kind_table); *flush_flags |= IOMMU_FLUSHF_modified; + +perfc_incr (iommu_pt_shatters); }/* Install lower level page table for non-present entries */
@@ -477,6 +479,7 @@ int cf_check amd_iommu_map_page( flags & IOMMUF_readable, &contig); *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all;iommu_queue_free_pgtable (hd, pg); +perfc_incr (iommu_pt_coalesces); }spin_unlock (&hd->arch.mapping_lock);
@@ -543,6 +546,7 @@ int cf_check amd_iommu_unmap_page(clear_iommu_pte_present (pt_mfn,dfn_x (dfn), level, &free); *flush_flags |= IOMMU_FLUSHF_all;iommu_queue_free_pgtable (hd, pg); +perfc_incr (iommu_pt_coalesces); } }
xen/drivers/passthrough/vtd/iommu.c +4 -0
@@ -404,6 +404,8 @@ static uint64_t addr_to_dma_page_maddr(sif ( flush_flags ) *flush_flags |= IOMMU_FLUSHF_modified; + +perfc_incr (iommu_pt_shatters); }write_atomic (&pte->val, new_pte.val);
@@ -857,6 +859,7 @@ static int dma_pte_clear_one(struct doma *flush_flags |= IOMMU_FLUSHF_all;iommu_queue_free_pgtable (hd, pg); +perfc_incr (iommu_pt_coalesces); }spin_unlock (&hd->arch.mapping_lock);
@@ -2239,6 +2242,7 @@ static int __must_check cf_check intel_i *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all;iommu_queue_free_pgtable (hd, pg); +perfc_incr (iommu_pt_coalesces); }spin_unlock (&hd->arch.mapping_lock);
Re: [PATCH v7 13/14] IOMMU/x86: add perf counters for page table splitting / coalescing
Quoted 3 lines · Jan Beulich
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Kevin tian <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
Quoted 20 lines · Jan Beulich
For a long time we've been rather inefficient with IOMMU page table management when not sharing page tables, i.e. in particular for PV (and further specifically also for PV Dom0) and AMD (where nowadays we never share page tables). While up to about 3.5 years ago AMD code had logic to un-shatter page mappings, that logic was ripped out for being buggy (XSA-275 plus follow-on).
This series enables use of large pages in AMD and Intel (VT-d) code; Arm is presently not in need of any enabling as pagetables are always shared there. It also augments PV Dom0 creation with suitable explicit IOMMU mapping calls to facilitate use of large pages there. Depending on the amount of memory handed to Dom0 this improves booting time (latency until Dom0 actually starts) quite a bit; subsequent shattering of some of the large pages may of course consume some of the saved time.
Known fallout has been spelled out here:
https://lists.xen.org/archives/html/xen-devel/2021-08/msg00781.htmlSee individual patches for details on the v7 changes.
Quoted 14 lines
01: iommu: add preemption support to iommu_{un,}map()
02: IOMMU/x86: perform PV Dom0 mappings in batches
03: IOMMU/x86: support freeing of pagetables
02: IOMMU/x86: new command line option to suppress use of superpage mappings 03: AMD/IOMMU: allow use of superpage mappings
04: VT-d: allow use of superpage mappings
05: x86: introduce helper for recording degree of contiguity in page tables 06: IOMMU/x86: prefill newly allocate page tables
07: AMD/IOMMU: free all-empty page tables
08: VT-d: free all-empty page tables
09: AMD/IOMMU: replace all-contiguous page tables by superpage mappings 10: VT-d: replace all-contiguous page tables by superpage mappings 11: IOMMU/x86: add perf counters for page table splitting / coalescing 12: VT-d: folddma_pte_clear_one()into its only caller
And I realize I've successfully screwed up numbering here. The order of patches if correct, though - it's just that from the 4th patch onwards all numbers are off by 2.
Jan
PATCH v7 14/14 VT-d: fold dma_pte_clear_one() into its only caller
In reply to Jan BeulichThis way intel_iommu_unmap_page() ends up quite a bit more similar to intel_iommu_map_page().
No functional change intended.
---
v5: Re-base of changes earlier in the series.
v4: New.
xen/drivers/passthrough/vtd/iommu.c +63 -71
@@ -806,75 +806,6 @@ static void queue_free_pt(struct domain_iommu_queue_free_pgtable (hd,mfn_to_page (mfn)); } -/* clear one page's page table */ -static int dma_pte_clear_one (struct domain *domain,daddr_t addr, -unsigned int order, -unsigned int *flush_flags) -{ -struct domain_iommu *hd =dom_iommu (domain); -struct dma_pte *page =NULL , *pte =NULL , old; -u64 pg_maddr; -unsigned int level = (order /LEVEL_STRIDE ) +1 ; - -spin_lock (&hd->arch.mapping_lock); -/* get target level pte */ - pg_maddr =addr_to_dma_page_maddr (domain, addr, level, flush_flags, false); -if ( pg_maddr <PAGE_SIZE ) - { -spin_unlock (&hd->arch.mapping_lock); -return pg_maddr ? -ENOMEM :0 ; - } - - page = (struct dma_pte *)map_vtd_domain_page (pg_maddr); - pte = &page[address_level_offset (addr, level)]; - -if ( !dma_pte_present (*pte) ) - { -spin_unlock (&hd->arch.mapping_lock); -unmap_vtd_domain_page (page); -return 0 ; - } - - old = *pte; -dma_clear_pte (*pte); -iommu_sync_cache (pte,sizeof (*pte)); - -while (pt_update_contig_markers (&page->val, -address_level_offset (addr, level), - level, PTE_kind_null) && - ++level < min_pt_levels ) - { -struct page_info *pg =maddr_to_page (pg_maddr); - -unmap_vtd_domain_page (page); - - pg_maddr =addr_to_dma_page_maddr (domain, addr, level, flush_flags, - false); -BUG_ON (pg_maddr <PAGE_SIZE ); - - page =map_vtd_domain_page (pg_maddr); - pte = &page[address_level_offset (addr, level)]; -dma_clear_pte (*pte); -iommu_sync_cache (pte,sizeof (*pte)); - - *flush_flags |= IOMMU_FLUSHF_all; -iommu_queue_free_pgtable (hd, pg); -perfc_incr (iommu_pt_coalesces); - } - -spin_unlock (&hd->arch.mapping_lock); - -unmap_vtd_domain_page (page); - - *flush_flags |= IOMMU_FLUSHF_modified; - -if ( order && !dma_pte_superpage (old) ) -queue_free_pt (hd,maddr_to_mfn (dma_pte_addr (old)), - order /LEVEL_STRIDE ); - -return 0 ; -} -static int iommu_set_root_entry (struct vtd_iommu *iommu) {u32 sts;
@@ -2264,11 +2195,17 @@ static int __must_check cf_check intel_i static int __must_check cf_check intel_iommu_unmap_page(struct domain *d,dfn_t dfn,unsigned int order,unsigned int *flush_flags) { +struct domain_iommu *hd =dom_iommu (d); +daddr_t addr =dfn_to_daddr (dfn); +struct dma_pte *page =NULL , *pte =NULL , old; +uint64_t pg_maddr; +unsigned int level = (order /LEVEL_STRIDE ) +1 ; +/* * While really we could unmap at any granularity, for now we assume unmaps * are issued by common code only at the same granularity as maps. */ -ASSERT ((dom_iommu (d)->platform_ops->page_sizes >> order) &PAGE_SIZE_4K ); +ASSERT ((hd->platform_ops->page_sizes >> order) &PAGE_SIZE_4K );/* Do nothing if VT-d shares EPT page table */ if (iommu_use_hap_pt (d) )
@@ -2278,7 +2215,62 @@ static int __must_check cf_check intel_iif ( iommu_hwdom_passthrough &&is_hardware_domain (d) )return 0 ; -return dma_pte_clear_one (d,dfn_to_daddr (dfn), order, flush_flags); +spin_lock (&hd->arch.mapping_lock); +/* get target level pte */ + pg_maddr =addr_to_dma_page_maddr (d, addr, level, flush_flags, false); +if ( pg_maddr <PAGE_SIZE ) + { +spin_unlock (&hd->arch.mapping_lock); +return pg_maddr ? -ENOMEM :0 ; + } + + page =map_vtd_domain_page (pg_maddr); + pte = &page[address_level_offset (addr, level)]; + +if ( !dma_pte_present (*pte) ) + { +spin_unlock (&hd->arch.mapping_lock); +unmap_vtd_domain_page (page); +return 0 ; + } + + old = *pte; +dma_clear_pte (*pte); +iommu_sync_cache (pte,sizeof (*pte)); + +while (pt_update_contig_markers (&page->val, +address_level_offset (addr, level), + level, PTE_kind_null) && + ++level < min_pt_levels ) + { +struct page_info *pg =maddr_to_page (pg_maddr); + +unmap_vtd_domain_page (page); + + pg_maddr =addr_to_dma_page_maddr (d, addr, level, flush_flags, false); +BUG_ON (pg_maddr <PAGE_SIZE ); + + page =map_vtd_domain_page (pg_maddr); + pte = &page[address_level_offset (addr, level)]; +dma_clear_pte (*pte); +iommu_sync_cache (pte,sizeof (*pte)); + + *flush_flags |= IOMMU_FLUSHF_all; +iommu_queue_free_pgtable (hd, pg); +perfc_incr (iommu_pt_coalesces); + } + +spin_unlock (&hd->arch.mapping_lock); + +unmap_vtd_domain_page (page); + + *flush_flags |= IOMMU_FLUSHF_modified; + +if ( order && !dma_pte_superpage (old) ) +queue_free_pt (hd,maddr_to_mfn (dma_pte_addr (old)), + order /LEVEL_STRIDE ); + +return 0 ; } static int cf_check intel_iommu_lookup_page(
Re: [PATCH v7 14/14] VT-d: fold dma_pte_clear_one() into its only caller
Quoted 8 lines · Jan Beulich
This way
intel_iommu_unmap_page()ends up quite a bit more similar tointel_iommu_map_page().No functional change intended.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Kevin Tian <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
Quoted 15 lines · Jan Beulich
For a long time we've been rather inefficient with IOMMU page table management when not sharing page tables, i.e. in particular for PV (and further specifically also for PV Dom0) and AMD (where nowadays we never share page tables). While up to about 3.5 years ago AMD code had logic to un-shatter page mappings, that logic was ripped out for being buggy (XSA-275 plus follow-on).
This series enables use of large pages in AMD and Intel (VT-d) code; Arm is presently not in need of any enabling as pagetables are always shared there. It also augments PV Dom0 creation with suitable explicit IOMMU mapping calls to facilitate use of large pages there. Depending on the amount of memory handed to Dom0 this improves booting time (latency until Dom0 actually starts) quite a bit; subsequent shattering of some of the large pages may of course consume some of the saved time.
Quoted 7 lines
Known fallout has been spelled out here:
https://lists.xen.org/archives/html/xen-devel/2021-08/msg00781.htmlSee individual patches for details on the v7 changes.
01: iommu: add preemption support to iommu_{un,}map() 02: IOMMU/x86: perform PV Dom0 mappings in batches
Paul,
without meaning this to be a ping, may I ask whether - with Roger away for the next two months - you could find time to review these first two patches? I think this would then allow the entire series to go in.
Thanks, Jan
Quoted 21 lines
03: IOMMU/x86: support freeing of pagetables
02: IOMMU/x86: new command line option to suppress use of superpage mappings 03: AMD/IOMMU: allow use of superpage mappings
04: VT-d: allow use of superpage mappings
05: x86: introduce helper for recording degree of contiguity in page tables 06: IOMMU/x86: prefill newly allocate page tables
07: AMD/IOMMU: free all-empty page tables
08: VT-d: free all-empty page tables
09: AMD/IOMMU: replace all-contiguous page tables by superpage mappings 10: VT-d: replace all-contiguous page tables by superpage mappings 11: IOMMU/x86: add perf counters for page table splitting / coalescing 12: VT-d: folddma_pte_clear_one()into its only callerWhile not directly related (except that making this mode work properly here was a fair part of the overall work), at this occasion I'd also like to renew my proposal to make "iommu=dom0-strict" the default going forward. It already is not only the default, but the only possible mode for PVH Dom0.
Jan