Remove mfn_to_gfn() on ARM
cover Remove mfn_to_gfn() on ARM
On ARM, mfn_to_gfn() is defined as an identity macro that unconditionally returns the MFN as the GFN. This is incorrect for any non-direct-mapped domain.
It only has two call sites: getdomaininfo() and memory_exchange(). It causes getdomaininfo() to report the wrong shared_info_frame on ARM, breaking any toolstack or test that relies on it. While as steal_page() is not supported on arm, the error does not propagate to memory_exchange().
This series fixes the problem in the following steps:
- Generalize the existing per-page GFN storage in type_info so it is usable beyond xenheap pages.
- Introduce a shared_info_to_gfn() macro so getdomaininfo() switches to use page_get_xenheap_gfn() on ARM and still mfn_to_gfn() on x86
- Fix memory_exchange() to use page_set_gfn()/page_get_gfn(), which generalize the existing page_set_xenheap_gfn()/page_get_xenheap_gfn() from xenheap pages, instead of mfn_to_gfn() on ARM for stolen pages.
With all the above modification, we could remove the now-unused mfn_to_gfn() macro on ARM.
Penny Zheng (3):
xen/arm: generalize per-page GFN storage beyond xenheap pages
xen: introduce shared_info_to_gfn()
xen/arm: fix mfn_to_gfn() usage in memory_exchange()
xen/arch/arm/include/asm/mm.h | 40 +++++++++++++++++++++++----------- xen/arch/x86/include/asm/p2m.h | 3 +++ xen/common/domctl.c | 3 +-- xen/common/memory.c | 11 ++++++++++ 4 files changed, 42 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 xen/arm: generalize per-page GFN storage beyond xenheap pages
As preparation for fixing mfn_to_gfn() on ARM, we extend the existing GFN field in page_info's type_info to be usable for not only xenheap ones. Another usage will be introduced later for stolen pages in memory exchaging.
Introduce general-purpose page_get_gfn() and page_set_gfn() helpers that read and write the GFN stored in type_info. The old page_get_xenheap_gfn() and page_set_xenheap_gfn() are retained as thin wrappers with their xenheap ASSERTs, so all current callers remain unchanged.
Also introduce PGT_INVALID_GFN as the general sentinel, with PGT_INVALID_XENHEAP_GFN aliased to it for backward compatibility.
--- xen/arch/arm/include/asm/mm.h | 37 +++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-)
xen/arch/arm/include/asm/mm.h +25 -12
@@ -113,18 +113,21 @@ struct page_info#define PGT_count_mask PG_mask(3, 3)/* -* Stored in bits [28:0] (arm32) or [60:0] (arm64) GFN if page is xenheap page. +* Stored in bits [28:0] (arm32) or [60:0] (arm64) GFN if page is xenheap page, +* or stolen ones in memory exchanging. */ #define PGT_gfn_width PG_shift(3)#define PGT_gfn_mask ((1UL<<PGT_gfn_width)-1) -#define PGT_INVALID_XENHEAP_GFN _gfn(PGT_gfn_mask) +#define PGT_INVALID_GFN _gfn(PGT_gfn_mask) +#define PGT_INVALID_XENHEAP_GFN PGT_INVALID_GFN/* * An arch-specific initialization pattern is needed for the type_info field -* as it's GFN portion can contain the valid GFN if page is xenheap page. +* as it's GFN portion can contain the valid GFN if page is xenheap page, or +* stolen in memory exchanging. */ -#define PGT_TYPE_INFO_INITIALIZER gfn_x(PGT_INVALID_XENHEAP_GFN) +#define PGT_TYPE_INFO_INITIALIZER gfn_x(PGT_INVALID_GFN)/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)
@@ -338,28 +341,38 @@ unsigned int arch_get_dma_bitsize(void);* that requirement (risk of deadlock, lock inversion, etc) it is important * to make sure that all non-protected updates to this field are atomic. */ -static inline gfn_t page_get_xenheap_gfn (const struct page_info *p) +static inline gfn_t page_get_gfn (const struct page_info *p) {gfn_t gfn_ =_gfn (ACCESS_ONCE (p->u.inuse.type_info) & PGT_gfn_mask); -ASSERT (is_xen_heap_page (p)); - -return gfn_eq (gfn_,PGT_INVALID_XENHEAP_GFN ) ?INVALID_GFN : gfn_; +return gfn_eq (gfn_,PGT_INVALID_GFN ) ?INVALID_GFN : gfn_; } -static inline void page_set_xenheap_gfn (struct page_info *p,gfn_t gfn) +static inline void page_set_gfn (struct page_info *p,gfn_t gfn) { -gfn_t gfn_ =gfn_eq (gfn,INVALID_GFN ) ?PGT_INVALID_XENHEAP_GFN : gfn; +gfn_t gfn_ =gfn_eq (gfn,INVALID_GFN ) ?PGT_INVALID_GFN : gfn;unsigned long x, nx, y = p->u.inuse.type_info; -ASSERT (is_xen_heap_page (p)); -do { x = y; nx = (x & ~PGT_gfn_mask) |gfn_x (gfn_); }while ( (y =cmpxchg (&p->u.inuse.type_info, x, nx)) != x ); } +static inline gfn_t page_get_xenheap_gfn (const struct page_info *p) +{ +ASSERT (is_xen_heap_page (p)); + +return page_get_gfn (p); +} + +static inline void page_set_xenheap_gfn (struct page_info *p,gfn_t gfn) +{ +ASSERT (is_xen_heap_page (p)); + +page_set_gfn (p, gfn); +} +#endif /* __ARCH_ARM_MM__ */ /* * Local variables:
Signature · 1 line
2.43.0
Re: [PATCH v1 1/3] xen/arm: generalize per-page GFN storage beyond xenheap pages
Quoted 8 lines · Penny Zheng
As preparation for fixing
mfn_to_gfn()on ARM, we extend the existing GFN field in page_info'stype_infoto be usable for not only xenheap ones. Another usage will be introduced later for stolen pages in memory exchaging.Introduce general-purpose
page_get_gfn()andpage_set_gfn()helpers that read and write the GFN stored intype_info. The oldpage_get_xenheap_gfn()andpage_set_xenheap_gfn()are retained as thin wrappers with their xenheap ASSERTs, so all current callers remain unchanged.
Why was this GFN setting limited to Xenheap pages back at the time? Depending on the reasons, retaining the old accessors may or may not be a good idea.
Quoted 2 lines
Also introduce
PGT_INVALID_GFNas the general sentinel, withPGT_INVALID_XENHEAP_GFNaliased to it for backward compatibility.
This I view as unnecessary, if not confusing.
xen/arch/arm/include/asm/mm.h struct page_info · line 118
Quoted 10 lines
xen/arch/arm/include/asm/mm.h +2
-1@@ -113,18 +113,21 @@ struct page_info#define PGT_count_mask PG_mask(3, 3)/* -* Stored in bits [28:0] (arm32) or [60:0] (arm64) GFN if page is xenheap page. +* Stored in bits [28:0] (arm32) or [60:0] (arm64) GFN if page is xenheap page, +* or stolen ones in memory exchanging. */
Does the purpose really need limiting like this? If the field covered by PGT_gfn_* is uniformly available (see the question above), I don't see why a new constraint would need spelling out. If it's not uniformly available, then likely the description needs expanding as to when the new accessors are okay to use. If uniformly available, what may want spelling out is under what conditions one can expect the field to be properly set (until such time where it's set correctly on all guest-owned pages).
Jan
RE: [PATCH v1 1/3] xen/arm: generalize per-page GFN storage beyond xenheap pages
[Public]
Hi,
Quoted 17 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Monday, March 30, 2026 9:49 PM
To: Penny, Zheng <[email protected]>
Cc: Huang, Ray <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Bertrand Marquis <[email protected]>; Orzel, Michal <[email protected]>; Volodymyr Babchuk <[email protected]>; [email protected]; Garcia Vallejo, Alejandro <[email protected]>
Subject: Re: [PATCH v1 1/3] xen/arm: generalize per-page GFN storage beyond xenheap pagesQuoted 4 lines · Penny Zheng
As preparation for fixing
mfn_to_gfn()on ARM, we extend the existing GFN field in page_info'stype_infoto be usable for not only xenheap ones. Another usage will be introduced later for stolen pages in memory exchaging.
Quoted 8 lines
Quoted 4 lines
Introduce general-purpose
page_get_gfn()andpage_set_gfn()helpers that read and write the GFN stored intype_info. The oldpage_get_xenheap_gfn()andpage_set_xenheap_gfn()are retained as thin wrappers with their xenheap ASSERTs, so all current callers remain unchanged.Why was this GFN setting limited to Xenheap pages back at the time? Depending on the reasons, retaining the old accessors may or may not be a good idea.
The only call site is to set shared_info gfn. I assumed GFN setting could be uniformly available. FWIT, the only limitation is that it shall not be applied to shared pages. In arm, I think it's static shared memory.
Quoted 25 lines
Quoted 2 lines
Also introduce
PGT_INVALID_GFNas the general sentinel, withPGT_INVALID_XENHEAP_GFNaliased to it for backward compatibility.This I view as unnecessary, if not confusing.
xen/arch/arm/include/asm/mm.h struct page_info · line 119
Quoted 11 lines
xen/arch/arm/include/asm/mm.h +3
-1@@ -113,18 +113,21 @@ struct page_info#define PGT_count_mask PG_mask(3, 3)/* -* Stored in bits [28:0] (arm32) or [60:0] (arm64) GFN if page is xenheap page. +* Stored in bits [28:0] (arm32) or [60:0] (arm64) GFN if page is +xenheap page, +* or stolen ones in memory exchanging. */ Does the purpose really need limiting like this? If the field covered by PGT_gfn_* is uniformly available (see the question above), I don't see why a new constraint would need spelling out. If it's not uniformly available, then likely the description needs expanding as to when the new accessors are okay to use. If uniformly available, what may want spelling out is under what conditions one can expect the field to be properly set (until such time where it's set correctly on all guest-owned pages).
Jan
PATCH 2/3 xen: introduce shared_info_to_gfn()
In reply to Penny ZhengOn ARM, mfn_to_gfn() is a simple identity macro that actually does not return the correct GFN for domains other than direct-map ones, so getdomaininfo() is returning the wrong shared_info_frame on ARM.
Introduce a common shared_info_to_gfn(d) macro to output correct GFN for both ARM and x86 in getdomaininfo():
- ARM: uses page_get_xenheap_gfn() to read the stored GFN
- x86: simply wraps the existing mfn_to_gfn() solution which consults the M2P table
--- xen/arch/arm/include/asm/mm.h | 2 ++ xen/arch/x86/include/asm/p2m.h | 3 +++ xen/common/domctl.c | 3 +-- 3 files changed, 6 insertions(+), 2 deletions(-)
xen/arch/arm/include/asm/mm.h +2 -0
@@ -308,6 +308,8 @@ struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va,/* Xen always owns P2M on ARM */ #define set_gpfn_from_mfn (mfn, pfn) do { (void) (mfn), (void)(pfn); } while (0)#define mfn_to_gfn (d, mfn) ((void)(d), _gfn(mfn_x(mfn))) +#define shared_info_to_gfn (d) \ + page_get_xenheap_gfn(virt_to_page((d)->shared_info))/* Arch-specific portion of memory_op hypercall. */ long arch_memory_op (int op,XEN_GUEST_HANDLE_PARAM (void ) arg);
xen/arch/x86/include/asm/p2m.h +3 -0
@@ -582,6 +582,9 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)return _gfn (mfn_x (mfn)); } +#define shared_info_to_gfn (d) \ + mfn_to_gfn((d), _mfn(virt_to_mfn((d)->shared_info))) +#ifdef CONFIG_ALTP2M #define AP2MGET_prepopulate true#define AP2MGET_query false
xen/common/domctl.c +1 -2
@@ -104,8 +104,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)#ifdef CONFIG_MEM_PAGING info->paged_pages =atomic_read (&d->paged_pages);#endif - info->shared_info_frame = -gfn_x (mfn_to_gfn (d,_mfn (virt_to_mfn (d->shared_info)))); + info->shared_info_frame =gfn_x (shared_info_to_gfn (d));BUG_ON (SHARED_M2P (info->shared_info_frame)); info->cpupool =cpupool_get_id (d);
Signature · 1 line
2.43.0
Re: [PATCH v1 2/3] xen: introduce shared_info_to_gfn()
Quoted 4 lines · Penny Zheng
On ARM,
mfn_to_gfn()is a simple identity macro that actually does not return the correct GFN for domains other than direct-map ones, sogetdomaininfo()is returning the wrongshared_info_frameon ARM.
Quoted 8 lines
Introduce a common
shared_info_to_gfn(d)macro to output correct GFN for both ARM and x86 ingetdomaininfo():
- ARM: usespage_get_xenheap_gfn()to read the stored GFN
- x86: simply wraps the existingmfn_to_gfn()solution which consults the M2P tableSuggested-by Alejandro Vallejo <[email protected]>Signed-off-by Penny Zheng <[email protected]>
preferably with ...
xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 586
Quoted 8 lines
xen/arch/x86/include/asm/p2m.h +2
-0@@ -582,6 +582,9 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)return _gfn (mfn_x (mfn)); } +#define shared_info_to_gfn (d) \ + mfn_to_gfn((d), _mfn(virt_to_mfn((d)->shared_info)))
... the excess parentheses removed from the first argument. (Can be adjusted while committing if no other need for a v2 arises.)
It also looks as if this change was independent of patch 1. As indicated before, such information would be useful to supply right away, as it allows this patch to go in ahead of patch 1 (if acks arrive here but not there).
Btw, shouldn't there be a Fixes: tag? If so, and if this then was to be backported, it being (in)dependent of patch 1 would become yet more relevant.
Jan
Re: [PATCH v1 2/3] xen: introduce shared_info_to_gfn()
In reply to Penny ZhengQuoted 10 lines · Penny Zheng
On ARM,
mfn_to_gfn()is a simple identity macro that actually does not return the correct GFN for domains other than direct-map ones, sogetdomaininfo()is returning the wrongshared_info_frameon ARM.Introduce a common
shared_info_to_gfn(d)macro to output correct GFN for both ARM and x86 ingetdomaininfo():
- ARM: usespage_get_xenheap_gfn()to read the stored GFN
- x86: simply wraps the existingmfn_to_gfn()solution which consults the M2P table
Quoted 6 lines
Suggested-by Alejandro Vallejo <[email protected]>Signed-off-by Penny Zheng <[email protected]>--- xen/arch/arm/include/asm/mm.h | 2 ++ xen/arch/x86/include/asm/p2m.h | 3 +++ xen/common/domctl.c | 3 +--
Seeing you only added this macro for Arm and x86, the RISCV and PPC builds will likely fail to build.
Other than that, I agree with Jan that this requires a Fixes tag and the changes look good.
~Michal
PATCH 3/3 xen/arm: fix mfn_to_gfn() usage in memory_exchange()
In reply to Penny ZhengOn ARM, the only callsite of mfn_to_gfn() is in memory_exchange(). Although memory_exchange() is actually not reachable on ARM because steal_page() always returns -EOPNOTSUPP. We still need to fix it here to ensure correctness once steal_page() is implemented for ARM in the future, and avoid propagating broken usage.
Fixing it with the following steps:
- Recording the GFN into the page's type_info via page_set_gfn() at stolen time
- Retrieving it via page_get_gfn() at consume time instead of mfn_to_gfn().
With the only call site removed in ARM, mfn_to_gfn() macro is no longer referenced and could be removed in ARM.
--- xen/arch/arm/include/asm/mm.h | 1 - xen/common/memory.c | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/mm.h +0 -1
@@ -307,7 +307,6 @@ struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va,/* Xen always owns P2M on ARM */ #define set_gpfn_from_mfn (mfn, pfn) do { (void) (mfn), (void)(pfn); } while (0) -#define mfn_to_gfn (d, mfn) ((void)(d), _gfn(mfn_x(mfn)))#define shared_info_to_gfn (d) \ page_get_xenheap_gfn(virt_to_page((d)->shared_info))
xen/common/memory.c +11 -0
@@ -814,6 +814,13 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg) goto fail; } +#ifndef CONFIG_X86 +/* +* Record the GFN in the page's type_info, +* so that we can retrieve it later when consuming the page. +*/ +page_set_gfn (page,_gfn (gmfn + k)); +#endif page_list_add (page, &in_chunk_list);#ifdef CONFIG_X86 put_gfn (d, gmfn + k);
@@ -849,7 +856,11 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)gfn_t gfn; mfn =page_to_mfn (page); +#ifdef CONFIG_X86 gfn =mfn_to_gfn (d, mfn); +#else + gfn =page_get_gfn (page); +#endif /* Pages were unshared above */ BUG_ON (SHARED_M2P (gfn_x (gfn)));if (guest_physmap_remove_page (d, gfn, mfn,0 ) )
Signature · 1 line
2.43.0
Re: [PATCH v1 3/3] xen/arm: fix mfn_to_gfn() usage in memory_exchange()
Quoted 5 lines · Penny Zheng
On ARM, the only callsite of
mfn_to_gfn()is inmemory_exchange(). Althoughmemory_exchange()is actually not reachable on ARM becausesteal_page()always returns-EOPNOTSUPP. We still need to fix it here to ensure correctness oncesteal_page()is implemented for ARM in the future, and avoid propagating broken usage.
steal_page() may need making functional on Arm without the need to permit memory_exchange(). That would allow a different approach to the problem.
Quoted 18 lines
xen/common/memory.c +7
-0@@ -814,6 +814,13 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg) goto fail; } +#ifndef CONFIG_X86 +/* +* Record the GFN in the page's type_info, +* so that we can retrieve it later when consuming the page. +*/ +page_set_gfn (page,_gfn (gmfn + k)); +#endif page_list_add (page, &in_chunk_list);#ifdef CONFIG_X86 put_gfn (d, gmfn + k);@@ -849,7 +856,11 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)gfn_t gfn;
xen/common/memory.c static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg) · line 866
Quoted 10 lines
xen/common/memory.c +4
-0mfn =page_to_mfn (page); +#ifdef CONFIG_X86 gfn =mfn_to_gfn (d, mfn); +#else + gfn =page_get_gfn (page); +#endif /* Pages were unshared above */ BUG_ON (SHARED_M2P (gfn_x (gfn)));if (guest_physmap_remove_page (d, gfn, mfn,0 ) )
I pretty strongly object to further CONFIG_<arch> uses in common code. We should reduce their amount, not increase it. Why can't page_set_gfn() be a no-op on x86, while page_get_gfn() would resolve to mfn_to_gfn()?
The #ifdef-ary here will, btw, likely need reducing if you want to make steal_page() and memory_exchange() work on Arm: Like x86, a GFN reference would then presumably need holding across the steal_page() invocation.
After you have fetched the GFN, I think you want to invalidate the field again. Otherwise, after the subsequent guest_physmap_add_page(), the stored value will likely be stale.
Jan