xen/arm: Harden the P2M code in p2m_remove_mapping()
PATCH v7 1/2 xen/arm: Harden the P2M code in p2m_remove_mapping()
From: Oleksandr Tyshchenko <[email protected]>
Borrow the x86's check from p2m_remove_page() which was added by the following commit: c65ea16dbcafbe4fe21693b18f8c2a3c5d14600e "x86/p2m: don't assert that the passed in MFN matches for a remove" and adjust it to the Arm code base.
Basically, this check will be strictly needed for the xenheap pages after applying a subsequent commit which will introduce xenheap based M2P approach on Arm. But, it will be a good opportunity to harden the P2M code for *every* RAM pages since it is possible to remove any GFN - MFN mapping currently on Arm (even with the wrong helpers).
---
You can find the corresponding discussion at:
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
Changes V6 -> V7:
- make this commit to be the first
- update commit description and add a comment in code
--- xen/arch/arm/p2m.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
xen/arch/arm/p2m.c +28 -0
@@ -1308,11 +1308,39 @@ static inline int p2m_remove_mapping(struct domain *d, mfn_t mfn) {struct p2m_domain *p2m =p2m_get_hostp2m (d); +unsigned long i;int rc;p2m_write_lock (p2m); +/* +* Before removing the GFN - MFN mapping for any RAM pages make sure +* that there is no difference between what is already mapped and what +* is requested to be unmapped. +* If they don't match bail out early. For instance, this could happen +* if two CPUs are requesting to unmap the same P2M concurrently. +*/ +for ( i =0 ; i < nr; ) + { +unsigned int cur_order; +p2m_type_t t; +mfn_t mfn_return =p2m_get_entry (p2m,gfn_add (start_gfn, i), &t,NULL , + &cur_order,NULL ); + +if (p2m_is_any_ram (t) && + (!mfn_valid (mfn) || !mfn_eq (mfn_add (mfn, i), mfn_return)) ) + { + rc = -EILSEQ ; + goto out; + } + + i += (1UL << cur_order) - + ((gfn_x (start_gfn) + i) & ((1UL << cur_order) -1 )); + } + rc =p2m_set_entry (p2m, start_gfn, nr,INVALID_MFN , p2m_invalid, p2m_access_rwx); + +out:p2m_write_unlock (p2m);return rc;
Signature · 1 line
2.25.1
PATCH v7 2/2 xen/gnttab: Store frame GFN in struct page_info on Arm
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct page_info directly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct page_info borrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm). Please note, to minimize code changes and avoid introducing an extra #ifdef-s to the header, we keep the same amount of bits on both subarches, although the count portion on Arm64 could be wider, so we waste some bits here.
Introduce corresponding PGT_* constructs and access macro page_get(set)_xenheap_gfn. Please note, all accesses to the GFN portion of type_info field should always be protected by the P2M lock. In case when it is not feasible to satisfy that requirement (risk of deadlock, lock inversion, etc) it is important to make sure that all non-protected updates to this field are atomic.
As several non-protected read accesses still exist within current code (most calls to page_get_xenheap_gfn() are not protected by the P2M lock) the subsequent patch will introduce hardening code for p2m_remove_mapping() to be called with P2M lock held in order to check any difference between what is already mapped and what is requested to be ummapped.
Update existing gnttab macros to deal with GFN value according to new location. Also update the use of count portion of type_info field on Arm in share_xen_page_with_guest().
While at it, extend this simplified M2P-like approach for any xenheap pages which are proccessed in xenmem_add_to_physmap_one() except foreign ones. Update the code to set GFN portion after establishing new mapping for the xenheap page in said function and to clean GFN portion when putting a reference on that page in p2m_put_l3_page().
And for everything to work correctly introduce arch-specific initialization pattern PGT_TYPE_INFO_INITIALIZER to be applied to type_info field during initialization at alloc_heap_pages() and acquire_staticmem_pages(). The pattern's purpose on Arm is to clear the GFN portion before use, on x86 it is just a stub.
This patch is intended to fix the potential issue on Arm which might happen when remapping grant-table frame. A guest (or the toolstack) will unmap the grant-table frame using XENMEM_remove_physmap. This is a generic hypercall, so on x86, we are relying on the fact the M2P entry will be cleared on removal. For architecture without the M2P, the GFN would still be present in the grant frame/status array. So on the next call to map the page, we will end up to request the P2M to remove whatever mapping was the given GFN. This could well be another mapping.
Please note, this patch also changes the behavior how the shared_info page (which is xenheap RAM page) is mapped in xenmem_add_to_physmap_one(). Now, we only allow to map the shared_info at once. The subsequent attempts to map it will result in -EBUSY. Doing that we mandate the caller to first unmap the page before mapping it again. This is to prevent Xen creating an unwanted hole in the P2M. For instance, this could happen if the firmware stole a RAM address for mapping the shared_info page into but forgot to unmap it afterwards.
Besides that, this patch simplifies arch code on Arm by removing arrays and corresponding management code and as the result gnttab_init_arch/gnttab_destroy_arch helpers and struct grant_table_arch become useless and can be dropped globally.
---
Dear @RISC-V maintainers, please note in current patch I drop arch specific helpers gnttab_init(destroy)_arch helpers as unneeded for both Arm and x86. Please let me know if you are going to reuse them in the nearest future and I will retain them.
You can find the related discussions at:
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
Changes RFC1 -> RFC2:
- update patch description
- add/update comments in code
- clarify check in p2m_put_l3_page()
- introduce arch_alloc_xenheap_page() and arch_free_xenheap_page() and drop page_arch_init()
- add ASSERT to gnttab_shared_page() and gnttab_status_page()
- rework changes to Arm's struct page_info: do not split type_info, allocate GFN portion by reducing count portion, create corresponding PGT_* construct, etc
- update page_get_frame_gfn() and page_set_frame_gfn()
- update the use of count portion on Arm
- drop the leading underscore in the macro parameter names
Changes RFC2 -> RFC3:
- update patch description
- drop PGT_count_base and MASK_INSR() in share_xen_page_with_guest()
- update alloc_xenheap_page() and free_xenheap_page() for SEPARATE_XENHEAP case (Arm32)
- provide an extra bit for GFN portion, to get PGT_INVALID_FRAME_GFN one bit more than the maximum number of physical address bits on Arm32
Changes RFC3 -> V4:
- rebase on Jan's "gnttab: remove guest_physmap_remove_page() call from gnttab_map_frame()"
- finally resolve locking question by recent Julien's suggestion, so drop the RFC tag
- update comments in Arm's mm.h/p2m.c to not mention grant table
- convert page_set(get)_frame_gfn to static inline func and rename them to page_set(get)_xenheap_gfn()
- rename PGT_INVALID_FRAME_GFN to PGT_INVALID_XENHEAP_GFN
- add ASSERT(is_xen_heap_page(...)) in page_set(get)_frame_gfn
- remove BUG_ON() in arch_free_xenheap_page
- remove local type_info in share_xen_page_with_guest()
- remove an extra argument p2m in p2m_put_l3_page()
- remove #ifdef CONFIG_GRANT_TABLE in p2m_put_l3_page()
- also cover real-only pages by using p2m_is_ram instead of a check against p2m_ram_rw in p2m_put_l3_page() and use "else if" construct
- call arch_free_xenheap_page() before clearing the PGC_xen_heap in free_xenheap_pages()
- remove ASSERT() in gnttab_shared(status)_page and use simpler virt_to_page
- remove local pg_ in gnttab_shared(status)_gfn
- update patch description to reflect recent changes
Changes V4 -> V5:
- rebase on latest staging
- update patch description
- drop arch_alloc(free)_xenheap_page macro and use arch-specific initialization pattern to clear GFN portion before use
- add const to struct page_info *p in page_get_xenheap_gfn
- fix a breakage on Arm32
Changes V5 -> V6:
- update patch description
- add/update comments in code
- s/PGT_TYPE_INFO_INIT_PATTERN/PGT_TYPE_INFO_INITIALIZER
- define PGT_TYPE_INFO_INITIALIZER in page_alloc.c if arch doesn't define it
- modify page_get_xenheap_gfn() to use ACCESS_ONCE() when reading type_info field
- modify page_set_xenheap_gfn() to use cmpxchg() when changing type_info field
- apply PGT_TYPE_INFO_INITIALIZER in alloc_heap_pages() and acquire_staticmem_pages() rather than altering both flavors of alloc_xenheap_pages() to make an extra assignment
- simplify gnttab_shared(status)_page and gnttab_(shared)status_gfn macro
- update a check in Arm's gnttab_set_frame_gfn()
Changes V6 -> V7:
- make this commit to be the last
- update commit description and add/update comments in code
--- xen/arch/arm/include/asm/grant_table.h | 53 ++++++-------------------- xen/arch/arm/include/asm/mm.h | 47 +++++++++++++++++++++-- xen/arch/arm/mm.c | 51 ++++++++++++++++++++++--- xen/arch/arm/p2m.c | 7 +++- xen/arch/x86/include/asm/grant_table.h | 5 --- xen/common/grant_table.c | 9 ----- xen/common/page_alloc.c | 8 +++- 7 files changed, 112 insertions(+), 68 deletions(-)
xen/arch/arm/include/asm/grant_table.h +11 -42
@@ -11,11 +11,6 @@#define INITIAL_NR_GRANT_FRAMES 1U#define GNTTAB_MAX_VERSION 1 -struct grant_table_arch { -gfn_t *shared_gfn; -gfn_t *status_gfn; -}; -static inline void gnttab_clear_flags (struct domain *d,unsigned int mask,uint16_t *addr) {
@@ -56,53 +51,27 @@ int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,#define gnttab_dom0_frames () \ min_t(unsigned int, opt_max_grant_frames, PFN_DOWN(_etext - _stext)) -#define gnttab_init_arch (gt) \ -({ \ - unsigned int ngf_ = (gt)->max_grant_frames; \ - unsigned int nsf_ = grant_to_status_frames(ngf_); \ - \ - (gt)->arch.shared_gfn = xmalloc_array(gfn_t, ngf_); \ - (gt)->arch.status_gfn = xmalloc_array(gfn_t, nsf_); \ - if ( (gt)->arch.shared_gfn && (gt)->arch.status_gfn ) \ - { \ - while ( ngf_-- ) \ - (gt)->arch.shared_gfn[ngf_] = INVALID_GFN; \ - while ( nsf_-- ) \ - (gt)->arch.status_gfn[nsf_] = INVALID_GFN; \ - } \ - else \ - gnttab_destroy_arch(gt); \ - (gt)->arch.shared_gfn ? 0 : -ENOMEM; \ -}) - -#define gnttab_destroy_arch (gt) \ - do { \ - XFREE((gt)->arch.shared_gfn); \ - XFREE((gt)->arch.status_gfn); \ - } while ( 0 ) -#define gnttab_set_frame_gfn (gt, st, idx, gfn, mfn) \ - ({ \ - int rc_ = 0; \ - gfn_t ogfn = gnttab_get_frame_gfn(gt, st, idx); \ - if ( gfn_eq(ogfn, INVALID_GFN) || gfn_eq(ogfn, gfn) || \ - (rc_ = guest_physmap_remove_page((gt)->domain, ogfn, mfn, \ - 0)) == 0 ) \ - ((st) ? (gt)->arch.status_gfn \ - : (gt)->arch.shared_gfn)[idx] = (gfn); \ - rc_; \ - }) + (gfn_eq(gfn, INVALID_GFN) \ + ? guest_physmap_remove_page((gt)->domain, \ + gnttab_get_frame_gfn(gt, st, idx), \ + mfn, 0) \ + : 0)#define gnttab_get_frame_gfn (gt, st, idx) ({ \ (st) ? gnttab_status_gfn(NULL, gt, idx) \ : gnttab_shared_gfn(NULL, gt, idx); \ }) +#define gnttab_shared_page (t, i) virt_to_page((t)->shared_raw[i]) + +#define gnttab_status_page (t, i) virt_to_page((t)->status[i]) +#define gnttab_shared_gfn (d, t, i) \ - (((i) >= nr_grant_frames(t)) ? INVALID_GFN : (t)->arch.shared_gfn[i]) + page_get_xenheap_gfn(gnttab_shared_page(t, i))#define gnttab_status_gfn (d, t, i) \ - (((i) >= nr_status_frames(t)) ? INVALID_GFN : (t)->arch.status_gfn[i]) + page_get_xenheap_gfn(gnttab_status_page(t, i))#define gnttab_need_iommu_mapping (d) \ (is_domain_direct_mapped(d) && is_iommu_enabled(d))
xen/arch/arm/include/asm/mm.h +44 -3
59 lines not on this page · raw message
xen/arch/arm/mm.c +46 -5
63 lines not on this page · raw message
xen/arch/arm/p2m.c +5 -2
@@ -716,6 +716,8 @@ static int p2m_mem_access_radix_set(struct p2m_domain *p2m, gfn_t gfn,*/ static void p2m_put_l3_page (const lpae_t pte) { +mfn_t mfn =lpae_get_mfn (pte); +ASSERT (p2m_is_valid (pte));/*
@@ -727,11 +729,12 @@ static void p2m_put_l3_page(const lpae_t pte)*/ if (p2m_is_foreign (pte.p2m.type) ) { -mfn_t mfn =lpae_get_mfn (pte); -ASSERT (mfn_valid (mfn));put_page (mfn_to_page (mfn)); } +/* Detect the xenheap page and mark the stored GFN as invalid. */ +else if (p2m_is_ram (pte.p2m.type) &&is_xen_heap_mfn (mfn) ) +page_set_xenheap_gfn (mfn_to_page (mfn),INVALID_GFN ); }/* Free lpae sub-tree behind an entry */
xen/arch/x86/include/asm/grant_table.h +0 -5
@@ -14,9 +14,6 @@#define INITIAL_NR_GRANT_FRAMES 1U -struct grant_table_arch { -}; -static inline int create_grant_host_mapping (uint64_t addr,mfn_t frame,unsigned int flags,unsigned int cache_flags)
@@ -35,8 +32,6 @@ static inline int replace_grant_host_mapping(uint64_t addr, mfn_t frame,return replace_grant_pv_mapping (addr, frame, new_addr, flags); } -#define gnttab_init_arch (gt) 0 -#define gnttab_destroy_arch (gt) do {} while ( 0 )#define gnttab_set_frame_gfn (gt, st, idx, gfn, mfn) \ (gfn_eq(gfn, INVALID_GFN) \ ? guest_physmap_remove_page((gt)->domain, \
xen/common/grant_table.c +0 -9
@@ -99,8 +99,6 @@ struct grant_table {/* Domain to which this struct grant_table belongs. */ struct domain *domain; - -struct grant_table_arch arch; };unsigned int __read_mostly opt_max_grant_frames =64 ;
@@ -2018,14 +2016,9 @@ int grant_table_init(struct domain *d, int max_grant_frames,grant_write_lock (gt); - ret =gnttab_init_arch (gt); -if ( ret ) - goto unlock; -/* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */ ret =gnttab_grow_table (d,0 ); - unlock:grant_write_unlock (gt); out:
@@ -3939,8 +3932,6 @@ grant_table_destroy(if ( t ==NULL )return ; -gnttab_destroy_arch (t); -for ( i =0 ; i <nr_grant_frames (t); i++ )free_xenheap_page (t->shared_raw[i]);xfree (t->shared_raw);
xen/common/page_alloc.c +6 -2
@@ -155,6 +155,10 @@#define PGC_reserved 0#endif +#ifndef PGT_TYPE_INFO_INITIALIZER +#define PGT_TYPE_INFO_INITIALIZER 0 +#endif +/* * Comma-separated list of hexadecimal page numbers containing bad bytes. * e.g. 'badpage=0x3f45,0x8a321'.
@@ -1024,7 +1028,7 @@ static struct page_info *alloc_heap_pages( &tlbflush_timestamp);/* Initialise fields which have other uses for free pages. */ - pg[i].u.inuse.type_info =0 ; + pg[i].u.inuse.type_info =PGT_TYPE_INFO_INITIALIZER ;page_set_owner (&pg[i],NULL ); }
@@ -2702,7 +2706,7 @@ static struct page_info * __init acquire_staticmem_pages(mfn_t smfn,*/ pg[i].count_info = PGC_reserved | PGC_state_inuse;/* Initialise fields which have other uses for free pages. */ - pg[i].u.inuse.type_info =0 ; + pg[i].u.inuse.type_info =PGT_TYPE_INFO_INITIALIZER ;page_set_owner (&pg[i],NULL ); }
Signature · 1 line
2.25.1
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Hi Oleksandr,
Quoted 2 lines · Oleksandr Tyshchenko
Suggested-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>
Quoted 1 line
xen/arch/x86/include/asm/grant_table.h | 5 ---
This changes will need an ack from the x86 maintainers.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Quoted 1 line · Julien Grall
Hi Oleksandr,
Hello Julien
Quoted 6 lines · Oleksandr Tyshchenko
Quoted 2 lines
Suggested-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>Reviewed-by Julien Grall <[email protected]>
Thank you!
Quoted 4 lines
Quoted 2 lines
xen/arch/x86/include/asm/grant_table.h| 5 ---This changes will need an ack from the x86 maintainers.
Yes.
Jan has reviewed that patch many times.
Quoted 3 lines
Cheers,
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
In reply to Oleksandr TyshchenkoQuoted 69 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm). Please note, to minimize code changes and avoid introducing an extra #ifdef-s to the header, we keep the same amount of bits on both subarches, although the count portion on Arm64 could be wider, so we waste some bits here.Introduce corresponding PGT_* constructs and access macro page_get(set)_xenheap_gfn. Please note, all accesses to the GFN portion of
type_infofield should always be protected by the P2M lock. In case when it is not feasible to satisfy that requirement (risk of deadlock, lock inversion, etc) it is important to make sure that all non-protected updates to this field are atomic.
As several non-protected read accesses still exist within current code (most calls topage_get_xenheap_gfn()are not protected by the P2M lock) the subsequent patch will introduce hardening code forp2m_remove_mapping()to be called with P2M lock held in order to check any difference between what is already mapped and what is requested to be ummapped.Update existing gnttab macros to deal with GFN value according to new location. Also update the use of count portion of
type_infofield on Arm inshare_xen_page_with_guest().While at it, extend this simplified M2P-like approach for any xenheap pages which are proccessed in
xenmem_add_to_physmap_one()except foreign ones. Update the code to set GFN portion after establishing new mapping for the xenheap page in said function and to clean GFN portion when putting a reference on that page inp2m_put_l3_page().And for everything to work correctly introduce arch-specific initialization pattern
PGT_TYPE_INFO_INITIALIZERto be applied totype_infofield during initialization atalloc_heap_pages()andacquire_staticmem_pages(). The pattern's purpose on Arm is to clear the GFN portion before use, on x86 it is just a stub.This patch is intended to fix the potential issue on Arm which might happen when remapping grant-table frame. A guest (or the toolstack) will unmap the grant-table frame using
XENMEM_remove_physmap. This is a generic hypercall, so on x86, we are relying on the fact the M2P entry will be cleared on removal. For architecture without the M2P, the GFN would still be present in the grant frame/status array. So on the next call to map the page, we will end up to request the P2M to remove whatever mapping was the given GFN. This could well be another mapping.Please note, this patch also changes the behavior how the
shared_infopage (which is xenheap RAM page) is mapped inxenmem_add_to_physmap_one(). Now, we only allow to map theshared_infoat once. The subsequent attempts to map it will result in-EBUSY. Doing that we mandate the caller to first unmap the page before mapping it again. This is to prevent Xen creating an unwanted hole in the P2M. For instance, this could happen if the firmware stole a RAM address for mapping theshared_infopage into but forgot to unmap it afterwards.
Quoted 8 lines
Besides that, this patch simplifies arch code on Arm by removing arrays and corresponding management code and as the result gnttab_init_arch/gnttab_destroy_arch helpers and struct
grant_table_archbecome useless and can be dropped globally.Suggested-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
In reply to Oleksandr TyshchenkoQuoted 7 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.
Quoted 5 lines
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).
I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to VCPUOP_register_vcpu_info, yet map_vcpu_info() tries to obtain a reference for every vCPU. With my adding of GFN (really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.
I guess this went unnoticed because Linux presumably uses different GFNs for every vCPU, so the issue doesn't surface. But I'm afraid this is a regression (unless I'm overlooking something, perhaps a mitigating factor) which wants fixing for 4.17.
Jan
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Hi Jan,
Quoted 14 lines · Jan Beulich
Quoted 7 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.Quoted 5 lines
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).
Quoted 3 lines
I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to
VCPUOP_register_vcpu_info, yetmap_vcpu_info()tries to obtain a reference for every vCPU.
AFAIU, this would be a reference of the **count_info** not **type_info** (which BTW will never be incremented on Arm because we have no type support).
The commit message is only referring to the 'type_info's count'. So...
Quoted 3 lines
With my adding of GFN
(really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.
... I am not sure which problem you are referring too.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Quoted 17 lines · Julien Grall
Hi Jan,
Quoted 14 lines · Jan Beulich
Quoted 12 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).
Quoted 7 lines
Quoted 3 lines
I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to
VCPUOP_register_vcpu_info, yetmap_vcpu_info()tries to obtain a reference for every vCPU.AFAIU, this would be a reference of the **count_info** not **type_info** (which BTW will never be incremented on Arm because we have no type support).
I should have said "obtain a writable type reference".
Quoted 7 lines
The commit message is only referring to the 'type_info's count'. So...
Quoted 3 lines
With my adding of GFN
(really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.... I am not sure which problem you are referring too.
Wow - a mere stub (but not inline) function to make the build happy. Then why is the description talking about one bit that's needed on Arm?
Jan
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Hi Jan,
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Julien Grall
Hi Jan,
Quoted 17 lines · Jan Beulich
Quoted 7 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.Quoted 5 lines
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to
VCPUOP_register_vcpu_info, yetmap_vcpu_info()tries to obtain a reference for every vCPU.
Quoted 5 lines
Quoted 3 lines
AFAIU, this would be a reference of the **count_info** not **type_info** (which BTW will never be incremented on Arm because we have no type support).
I should have said "obtain a writable type reference".
Thanks for the clarification.
Quoted 7 lines
Quoted 7 lines
The commit message is only referring to the 'type_info's count'. So...
Quoted 3 lines
With my adding of GFN
(really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.
Quoted 5 lines
Quoted 1 line
... I am not sure which problem you are referring too.
Wow - a mere stub (but not inline) function to make the build happy. Then why is the description talking about one bit that's needed on Arm?
Because share_xen_page_with_guest() will always set the type info's count to 1.
TBH I don't exactly know why we set it. I always assumed this was a requirement for the common code but never checked.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Quoted 46 lines · Julien Grall
Hi Jan,
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Julien Grall
Hi Jan,
Quoted 17 lines · Jan Beulich
Quoted 7 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.Quoted 5 lines
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to
VCPUOP_register_vcpu_info, yetmap_vcpu_info()tries to obtain a reference for every vCPU.Quoted 5 lines
Quoted 3 lines
AFAIU, this would be a reference of the **count_info** not **type_info** (which BTW will never be incremented on Arm because we have no type support).
I should have said "obtain a writable type reference".
Thanks for the clarification.
Quoted 7 lines
Quoted 7 lines
The commit message is only referring to the 'type_info's count'. So...
Quoted 3 lines
With my adding of GFN
(really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.Quoted 5 lines
Quoted 1 line
... I am not sure which problem you are referring too.
Wow - a mere stub (but not inline) function to make the build happy. Then why is the description talking about one bit that's needed on Arm?
Quoted 5 lines
Because
share_xen_page_with_guest()will always set the type info's count to 1.TBH I don't exactly know why we set it. I always assumed this was a requirement for the common code but never checked.
I don't think there is any such requirement. In fact there are only very few uses of type_info in common code. By also setting PGT_count_mask to zero you could actually have the compiler eliminate some dead code ...
Jan
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
In reply to Julien GrallQuoted 42 lines · Julien Grall
Quoted 20 lines · Jan Beulich
Quoted 19 lines · Julien Grall
Quoted 17 lines · Jan Beulich
Quoted 7 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.Quoted 5 lines
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to
VCPUOP_register_vcpu_info, yetmap_vcpu_info()tries to obtain a reference for every vCPU.Quoted 5 lines
Quoted 3 lines
AFAIU, this would be a reference of the **count_info** not **type_info** (which BTW will never be incremented on Arm because we have no type support).
I should have said "obtain a writable type reference".
Thanks for the clarification.
Quoted 7 lines
Quoted 7 lines
The commit message is only referring to the 'type_info's count'. So...
Quoted 3 lines
With my adding of GFN
(really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.Quoted 5 lines
Quoted 1 line
... I am not sure which problem you are referring too.
Wow - a mere stub (but not inline) function to make the build happy. Then why is the description talking about one bit that's needed on Arm?
Quoted 5 lines
Because
share_xen_page_with_guest()will always set the type info's count to 1.TBH I don't exactly know why we set it. I always assumed this was a requirement for the common code but never checked.
So my first thought was that this type-ref handling all being no-ops would be an issue with gnttab v2, but besides that not being security supported on Arm the code also passes SHARE_rw (for a reason that escapes me) when sharing the status pages.
It does however mean that Dom0 can map the trace buffers r/w (unless there's some special code in Arm preventing that), despite them being shared with SHARE_ro. Not a big problem considering all the power Dom0 has, but still against the intentions.
Jan
Re: [PATCH V7 2/2] xen/gnttab: Store frame GFN in struct page_info on Arm
Hi Jan,
Quoted 46 lines · Jan Beulich
Quoted 45 lines · Julien Grall
Quoted 20 lines · Jan Beulich
Quoted 19 lines · Julien Grall
Quoted 17 lines · Jan Beulich
Quoted 7 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Rework Arm implementation to store grant table frame GFN in struct
page_infodirectly instead of keeping it in standalone status/shared arrays. This patch is based on the assumption that a grant table page is a xenheap page.Quoted 5 lines
To cover 64-bit/40-bit IPA on Arm64/Arm32 we need the space to hold 52-bit/28-bit + extra bit value respectively. In order to not grow the size of struct
page_infoborrow the required amount of bits from type_info's count portion which current context won't suffer (currently only 1 bit is used on Arm).I'm afraid this isn't true: There's no requirement for a guest to pass all different GFNs to
VCPUOP_register_vcpu_info, yetmap_vcpu_info()tries to obtain a reference for every vCPU.Quoted 5 lines
Quoted 3 lines
AFAIU, this would be a reference of the **count_info** not **type_info** (which BTW will never be incremented on Arm because we have no type support).
I should have said "obtain a writable type reference".
Thanks for the clarification.
Quoted 7 lines
Quoted 7 lines
The commit message is only referring to the 'type_info's count'. So...
Quoted 3 lines
With my adding of GFN
(really gaddr) based registration of the runstate area (already looking towards 4.18) the maximum possible count is to further grow.Quoted 5 lines
Quoted 1 line
... I am not sure which problem you are referring too.
Wow - a mere stub (but not inline) function to make the build happy. Then why is the description talking about one bit that's needed on Arm?
Because
share_xen_page_with_guest()will always set the type info's count to 1.
Quoted 7 lines
Quoted 2 lines
TBH I don't exactly know why we set it. I always assumed this was a requirement for the common code but never checked.
So my first thought was that this type-ref handling all being no-ops would be an issue with gnttab v2, but besides that not being security supported on Arm the code also passes
SHARE_rw(for a reason that escapes me) when sharing the status pages.
Probably because grant-table v2 was never tested on Arm.
Quoted 5 lines
It does however mean that Dom0 can map the trace buffers r/w (unless there's some special code in Arm preventing that), despite them being shared with
SHARE_ro. Not a big problem considering all the power Dom0 has, but still against the intentions.
We don't use the refcounting but still use the flag PGT_writable_page to indicate whether the mapping is writeable or read-only.
The code to map the trace buffers will look at the flag and decide the attribute in the P2M.
Cheers,
Signature · 1 line
Julien Grall
Hi Oleksandr,
In the future, please provide a cover letter (even if it is short) when you bundle multiple patches. This is useful to make generic comments and help threading in e-mail client (each patch would be a subthread of 0 rather than 1).
Quoted 35 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
Borrow the x86's check from
p2m_remove_page()which was added by the following commit:c65ea16dbcafbe4fe21693b18f8c2a3c5d14600e"x86/p2m: don't assert that the passed in MFN matches for a remove" and adjust it to the Arm code base.Basically, this check will be strictly needed for the xenheap pages after applying a subsequent commit which will introduce xenheap based M2P approach on Arm. But, it will be a good opportunity to harden the P2M code for *every* RAM pages since it is possible to remove any GFN - MFN mapping currently on Arm (even with the wrong helpers).
Suggested-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
You can find the corresponding discussion at:
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/Changes V6 -> V7:
- make this commit to be the first
- update commit description and add a comment in code--- xen/arch/arm/p2m.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)xen/arch/arm/p2m.c +0
-0@@ -1308,11 +1308,39 @@ static inline int p2m_remove_mapping(struct domain *d, mfn_t mfn) {struct p2m_domain *p2m =p2m_get_hostp2m (d);
xen/arch/arm/p2m.c static inline int p2m_remove_mapping(struct domain *d, · line 1320
Quoted 10 lines
xen/arch/arm/p2m.c +7
-0+unsigned long i;int rc;p2m_write_lock (p2m); +/* +* Before removing the GFN - MFN mapping for any RAM pages make sure +* that there is no difference between what is already mapped and what +* is requested to be unmapped. +* If they don't match bail out early. For instance, this could happen +* if two CPUs are requesting to unmap the same P2M concurrently.
Missing word: P2M *entry*
Other than that:
Cheers,
Signature · 1 line
Julien Grall
Quoted 1 line · Julien Grall
Hi Oleksandr,
Hello Julien
Quoted 5 lines
In the future, please provide a cover letter (even if it is short) when you bundle multiple patches. This is useful to make generic comments and help threading in e-mail client (each patch would be a subthread of 0 rather than 1).
Yes, will do.
Quoted 46 lines · Oleksandr Tyshchenko
Quoted 44 lines
From: Oleksandr Tyshchenko <[email protected]>
Borrow the x86's check from
p2m_remove_page()which was added by the following commit:c65ea16dbcafbe4fe21693b18f8c2a3c5d14600e"x86/p2m: don't assert that the passed in MFN matches for a remove" and adjust it to the Arm code base.Basically, this check will be strictly needed for the xenheap pages after applying a subsequent commit which will introduce xenheap based M2P approach on Arm. But, it will be a good opportunity to harden the P2M code for *every* RAM pages since it is possible to remove any GFN - MFN mapping currently on Arm (even with the wrong helpers).
Suggested-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
You can find the corresponding discussion at:
https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!3a2u-XL4NvAzSMfz72LARrdWVFvq2In5ZpUdxP2cSt7bM8PgV7P_ZclZG2R-rE9PcosUHyqsKRNfVG2TiM9Tlg$
[lore[.]kernel[.]org]
https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!3a2u-XL4NvAzSMfz72LARrdWVFvq2In5ZpUdxP2cSt7bM8PgV7P_ZclZG2R-rE9PcosUHyqsKRNfVG0kg7IZSA$
[lore[.]kernel[.]org]Changes V6 -> V7:
- make this commit to be the first
- update commit description and add a comment in code
---
xen/arch/arm/p2m.c| 28 ++++++++++++++++++++++++++++1 file changed, 28 insertions(+)xen/arch/arm/p2m.c +3
-0@@ -1308,11 +1308,39 @@ static inline int p2m_remove_mapping(structdomain *d, mfn_t mfn) { struct p2m_domain *p2m = p2m_get_hostp2m(d); + unsigned long i; int rc; p2m_write_lock(p2m); + /* +* Before removing the GFN - MFN mapping for any RAM pages make sure
Quoted 8 lines
Quoted 6 lines
+ * that there is no difference between what is already mapped and what
+ * is requested to be unmapped.
+ * If they don't match bail out early. For instance, this could happen
+ * if two CPUs are requesting to unmap the same P2M concurrently.Missing word: P2M *entry*
Yes. May I please ask, could this be done on the commit if this appears to be the last version?
Quoted 4 lines
Other than that:
Reviewed-by Julien Grall <[email protected]>
Thank you!
Quoted 4 lines
Cheers,
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Hi Oleksandr,
Quoted 54 lines · Oleksandr Tyshchenko
Quoted 45 lines · Oleksandr Tyshchenko
Quoted 44 lines
From: Oleksandr Tyshchenko <[email protected]>
Borrow the x86's check from
p2m_remove_page()which was added by the following commit:c65ea16dbcafbe4fe21693b18f8c2a3c5d14600e"x86/p2m: don't assert that the passed in MFN matches for a remove" and adjust it to the Arm code base.Basically, this check will be strictly needed for the xenheap pages after applying a subsequent commit which will introduce xenheap based M2P approach on Arm. But, it will be a good opportunity to harden the P2M code for *every* RAM pages since it is possible to remove any GFN - MFN mapping currently on Arm (even with the wrong helpers).
Suggested-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
You can find the corresponding discussion at:
https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!3a2u-XL4NvAzSMfz72LARrdWVFvq2In5ZpUdxP2cSt7bM8PgV7P_ZclZG2R-rE9PcosUHyqsKRNfVG2TiM9Tlg$
[lore[.]kernel[.]org]
https://urldefense.com/v3/__https://lore.kernel.org/xen-devel/[email protected]/__;!!GF_29dbcQIUBPA!3a2u-XL4NvAzSMfz72LARrdWVFvq2In5ZpUdxP2cSt7bM8PgV7P_ZclZG2R-rE9PcosUHyqsKRNfVG0kg7IZSA$
[lore[.]kernel[.]org]Changes V6 -> V7:
- make this commit to be the first
- update commit description and add a comment in code--- xen/arch/arm/p2m.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)xen/arch/arm/p2m.c +3
-0@@ -1308,11 +1308,39 @@ static inline int p2m_remove_mapping(structdomain *d, mfn_t mfn) { struct p2m_domain *p2m = p2m_get_hostp2m(d); + unsigned long i; int rc; p2m_write_lock(p2m); + /* +* Before removing the GFN - MFN mapping for any RAM pages make sure
Quoted 8 lines
Quoted 6 lines
+ * that there is no difference between what is already mapped and what
+ * is requested to be unmapped.
+ * If they don't match bail out early. For instance, this could happen
+ * if two CPUs are requesting to unmap the same P2M concurrently.Missing word: P2M *entry*
Quoted 2 lines
Yes. May I please ask, could this be done on the commit if this appears to be the last version?
I have committed the series with the same typo.
Cheers,
Signature · 1 line
Julien Grall