* [PATCH 0/3] xen/arm: p2m: free 1GB static shared memory on domain destroy
@ 2026-11-30 16:43 Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 1/3] xen/arm: p2m: add __p2m_free_entry() with a put argument Gabriel Quintáns Souto
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Gabriel Quintáns Souto @ 2026-11-30 16:43 UTC (permalink / raw)
To: xen-devel
Cc: Luca Fancellu, Gabriel Quintáns Souto, Stefano Stabellini,
Julien Grall, Bertrand Marquis, Michal Orzel, Volodymyr Babchuk
Static shared memory can map a 1GB region into the borrower with a
single level 1 foreign entry, but level 1 puts were not implemented in
p2m_put_page(). When the borrower is destroyed, the extra reference it
holds on every page of the region is left behind, so the pages are never
freed and destroying the owner afterwards never completes.
Patch 2 is the main fix: relinquish_p2m_mapping() now splits the 1GB
mapping into 2MB blocks, so the puts go through the level 2 path and
can be preempted. Patch 3 adds level 1 puts for the case where the
split can't be done. Patch 1 fixes a separate issue in the split error
path, where references were put on the copied entries while the
superpage was still mapped, and the other two depend on it.
To reproduce it, I used a dom0less setup on QEMU virt (cortex-a72,
GICv3, -m 4G) with CONFIG_STATIC_SHM, where domU1 owns a 1GB region
allocated from the heap and domU2 borrows it, both at a 1GB aligned IPA:
domU1 {
...
shm-1g {
compatible = "xen,domain-shared-memory-v1";
role = "owner";
xen,shm-id = "shm-1g";
xen,shared-mem = <0x00 0x80000000 0x00 0x40000000>;
};
};
domU2 {
...
shm-1g {
compatible = "xen,domain-shared-memory-v1";
role = "borrower";
xen,shm-id = "shm-1g";
xen,shared-mem = <0x00 0x80000000 0x00 0x40000000>;
};
};
The 'q' debug key reports "1G mappings: 1" for both domains. Then, from
dom0, I destroyed domU2 followed by domU1:
- Without the series, free_memory only went from 1982 to 2497 MB, and
xl destroy of domU1 never returned. 'q' showed it stuck with
dying=1 and nr_pages=262144.
- With the series, both destroys completed and free_memory went from
1982 to 3525 MB.
The -ENOMEM fallback was tested through a forced failure of
p2m_alloc_page() during relinquish.
An alternative would be to limit the foreign mappings of static shared
memory to 2MB, which avoids the issue without touching the level 1
path. I went with the split instead, since the TODO in p2m_put_page()
suggests level 1 superpages were meant to be supported.
Gabriel Quintáns Souto (3):
xen/arm: p2m: add __p2m_free_entry() with a put argument
xen/arm: p2m: split level 1 foreign superpages for preemption
xen/arm: p2m: support level 1 puts if the superpage cannot be split
xen/arch/arm/mmu/p2m.c | 68 +++++++++++++++++++++++++++++-------------
1 file changed, 48 insertions(+), 20 deletions(-)
--
2.56.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/3] xen/arm: p2m: add __p2m_free_entry() with a put argument
2026-11-30 16:43 [PATCH 0/3] xen/arm: p2m: free 1GB static shared memory on domain destroy Gabriel Quintáns Souto
@ 2026-11-30 16:43 ` Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 3/3] xen/arm: p2m: support level 1 puts if the superpage cannot be split Gabriel Quintáns Souto
2 siblings, 0 replies; 6+ messages in thread
From: Gabriel Quintáns Souto @ 2026-11-30 16:43 UTC (permalink / raw)
To: xen-devel
Cc: Luca Fancellu, Gabriel Quintáns Souto, Stefano Stabellini,
Julien Grall, Bertrand Marquis, Michal Orzel, Volodymyr Babchuk
If an error occurs while shattering a superpage, we only need
to free the sub-tree allocated for the split. The superpage is still
in place, so p2m_put_page() will be called when it is removed, and
calling it before would drop the references twice, which can free
pages that are still mapped.
Fixes: 3e2121ce479b ("xen/p2m: put reference for level 2 superpage")
Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
---
xen/arch/arm/mmu/p2m.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
index 53d2893..bc6d196 100644
--- a/xen/arch/arm/mmu/p2m.c
+++ b/xen/arch/arm/mmu/p2m.c
@@ -810,9 +810,13 @@ static void p2m_put_page(const lpae_t pte, unsigned int level)
return p2m_put_l3_page(mfn, pte.p2m.type);
}
-/* Free lpae sub-tree behind an entry */
-static void p2m_free_entry(struct p2m_domain *p2m,
- lpae_t entry, unsigned int level)
+/*
+ * Free lpae sub-tree behind an entry. If put is false, the references
+ * on the leaf entries are kept, e.g. because they are copies of a
+ * superpage that is still in place.
+ */
+static void __p2m_free_entry(struct p2m_domain *p2m, lpae_t entry,
+ unsigned int level, bool put)
{
unsigned int i;
lpae_t *table;
@@ -839,14 +843,15 @@ static void p2m_free_entry(struct p2m_domain *p2m,
p2m->stats.mappings[level]--;
- p2m_put_page(entry, level);
+ if ( put )
+ p2m_put_page(entry, level);
return;
}
table = map_domain_page(lpae_get_mfn(entry));
for ( i = 0; i < XEN_PT_LPAE_ENTRIES; i++ )
- p2m_free_entry(p2m, *(table + i), level + 1);
+ __p2m_free_entry(p2m, *(table + i), level + 1, put);
unmap_domain_page(table);
@@ -867,6 +872,12 @@ static void p2m_free_entry(struct p2m_domain *p2m,
p2m_free_page(p2m->domain, pg);
}
+static void p2m_free_entry(struct p2m_domain *p2m,
+ lpae_t entry, unsigned int level)
+{
+ __p2m_free_entry(p2m, entry, level, true);
+}
+
static bool p2m_split_superpage(struct p2m_domain *p2m, lpae_t *entry,
unsigned int level, unsigned int target,
const unsigned int *offsets)
@@ -1025,7 +1036,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
p2m->stats.mappings[level]++;
/* Free the allocated sub-tree */
- p2m_free_entry(p2m, split_pte, level);
+ __p2m_free_entry(p2m, split_pte, level, false);
rc = -ENOMEM;
goto out;
--
2.56.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption
2026-11-30 16:43 [PATCH 0/3] xen/arm: p2m: free 1GB static shared memory on domain destroy Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 1/3] xen/arm: p2m: add __p2m_free_entry() with a put argument Gabriel Quintáns Souto
@ 2026-11-30 16:43 ` Gabriel Quintáns Souto
2026-12-02 10:06 ` Grall, Julien
2026-11-30 16:43 ` [PATCH 3/3] xen/arm: p2m: support level 1 puts if the superpage cannot be split Gabriel Quintáns Souto
2 siblings, 1 reply; 6+ messages in thread
From: Gabriel Quintáns Souto @ 2026-11-30 16:43 UTC (permalink / raw)
To: xen-devel
Cc: Luca Fancellu, Gabriel Quintáns Souto, Stefano Stabellini,
Julien Grall, Bertrand Marquis, Michal Orzel, Volodymyr Babchuk
Static shared memory can create 1GB foreign mappings, as
p2m_set_entry() picks the mapping size based only on alignment.
p2m_put_page() doesn't have a put for level 1, so the references of
such a mapping are never dropped, the pages are never freed and
destroying the owner never completes.
Putting the references of a 1GB block at once would take too long
without being able to preempt, since relinquish_p2m_mapping() can only
be preempted between entries. By changing the order to 2MB,
__p2m_set_entry() can split the superpage and therefore put 2MB
blocks, allowing the preemption check in relinquish_p2m_mapping() to
work properly. For that, the check also needs ">=" instead of ">".
Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
---
xen/arch/arm/mmu/p2m.c | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
index bc6d196..e7a27db 100644
--- a/xen/arch/arm/mmu/p2m.c
+++ b/xen/arch/arm/mmu/p2m.c
@@ -1595,6 +1595,13 @@ int relinquish_p2m_mapping(struct domain *d)
start = gfn_next_boundary(start, order) )
{
mfn_t mfn = p2m_get_entry(p2m, start, &t, NULL, &order, NULL);
+ bool split = false;
+
+ if ( p2m_is_foreign(t) && order == XEN_PT_LEVEL_ORDER(1) )
+ {
+ order = XEN_PT_LEVEL_ORDER(2);
+ split = true;
+ }
count++;
/*
@@ -1602,7 +1609,7 @@ int relinquish_p2m_mapping(struct domain *d)
* foreign mapping.
*/
if ( (!(count % 512) ||
- (p2m_is_foreign(t) && (order > XEN_PT_LEVEL_ORDER(2)))) &&
+ (p2m_is_foreign(t) && (order >= XEN_PT_LEVEL_ORDER(2)))) &&
hypercall_preempt_check() )
{
rc = -ERESTART;
@@ -1621,6 +1628,16 @@ int relinquish_p2m_mapping(struct domain *d)
*/
rc = __p2m_set_entry(p2m, start, order, INVALID_MFN,
p2m_invalid, p2m_access_rwx);
+ if ( unlikely(rc == -ENOMEM && split) )
+ {
+ /*
+ * Shattering the foreign superpage needs a page from the P2M
+ * pool. If none is left, remove the whole superpage at once.
+ */
+ order = XEN_PT_LEVEL_ORDER(1);
+ rc = __p2m_set_entry(p2m, start, order, INVALID_MFN,
+ p2m_invalid, p2m_access_rwx);
+ }
if ( unlikely(rc) )
{
printk(XENLOG_G_ERR "Unable to remove mapping gfn=%#"PRI_gfn" order=%u from the p2m of domain %d\n", gfn_x(start), order, d->domain_id);
--
2.56.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/3] xen/arm: p2m: support level 1 puts if the superpage cannot be split
2026-11-30 16:43 [PATCH 0/3] xen/arm: p2m: free 1GB static shared memory on domain destroy Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 1/3] xen/arm: p2m: add __p2m_free_entry() with a put argument Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption Gabriel Quintáns Souto
@ 2026-11-30 16:43 ` Gabriel Quintáns Souto
2 siblings, 0 replies; 6+ messages in thread
From: Gabriel Quintáns Souto @ 2026-11-30 16:43 UTC (permalink / raw)
To: xen-devel
Cc: Luca Fancellu, Gabriel Quintáns Souto, Stefano Stabellini,
Julien Grall, Bertrand Marquis, Michal Orzel, Volodymyr Babchuk
If the P2M pool is exhausted while trying to split a level 1
superpage, p2m_put_page() does not perform put for that level. This
causes the references not to be dropped and the pages to never be
freed, meaning destroying the owner never completes in that case.
Add support for level 1 puts by reusing the original
p2m_put_l2_superpage(), making it generic for superpage mappings.
Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
---
xen/arch/arm/mmu/p2m.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
index e7a27db..4da5c58 100644
--- a/xen/arch/arm/mmu/p2m.c
+++ b/xen/arch/arm/mmu/p2m.c
@@ -768,11 +768,16 @@ static void p2m_put_l3_page(mfn_t mfn, p2m_type_t type)
page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN);
}
-/* Put any references on the superpage referenced by mfn. */
-static void p2m_put_l2_superpage(mfn_t mfn, p2m_type_t type)
+/*
+ * Put any references on the superpage referenced by mfn. For a level 1
+ * superpage this can't be preempted, so relinquish_p2m_mapping() shatters
+ * it first when it can.
+ */
+static void p2m_put_superpage(mfn_t mfn, p2m_type_t type,
+ unsigned int level)
{
struct page_info *pg;
- unsigned int i;
+ unsigned long i;
/*
* TODO: Handle other p2m types, but be aware that any changes to handle
@@ -786,7 +791,7 @@ static void p2m_put_l2_superpage(mfn_t mfn, p2m_type_t type)
pg = mfn_to_page(mfn);
- for ( i = 0; i < XEN_PT_LPAE_ENTRIES; i++, pg++ )
+ for ( i = 0; i < 1UL << XEN_PT_LEVEL_ORDER(level); i++, pg++ )
p2m_put_foreign_page(pg);
}
@@ -797,15 +802,8 @@ static void p2m_put_page(const lpae_t pte, unsigned int level)
ASSERT(p2m_is_valid(pte));
- /*
- * TODO: Currently we don't handle level 1 super-page, Xen is not
- * preemptible and therefore some work is needed to handle such
- * superpages, for which at some point Xen might end up freeing memory
- * and therefore for such a big mapping it could end up in a very long
- * operation.
- */
- if ( level == 2 )
- return p2m_put_l2_superpage(mfn, pte.p2m.type);
+ if ( level == 1 || level == 2 )
+ return p2m_put_superpage(mfn, pte.p2m.type, level);
else if ( level == 3 )
return p2m_put_l3_page(mfn, pte.p2m.type);
}
@@ -1633,6 +1631,8 @@ int relinquish_p2m_mapping(struct domain *d)
/*
* Shattering the foreign superpage needs a page from the P2M
* pool. If none is left, remove the whole superpage at once.
+ * This can't be preempted, but should only happen when the
+ * pool is exhausted.
*/
order = XEN_PT_LEVEL_ORDER(1);
rc = __p2m_set_entry(p2m, start, order, INVALID_MFN,
--
2.56.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption
2026-11-30 16:43 ` [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption Gabriel Quintáns Souto
@ 2026-12-02 10:06 ` Grall, Julien
2026-12-02 11:08 ` Gabriel Quintáns Souto
0 siblings, 1 reply; 6+ messages in thread
From: Grall, Julien @ 2026-12-02 10:06 UTC (permalink / raw)
To: Gabriel Quintáns Souto, xen-devel
Cc: Luca Fancellu, Stefano Stabellini, Bertrand Marquis, Michal Orzel,
Volodymyr Babchuk
Hi Gabriel,
Thank you for the series.
On 30/11/2026 16:43, Gabriel Quintáns Souto wrote:
> Static shared memory can create 1GB foreign mappings, as
> p2m_set_entry() picks the mapping size based only on alignment.
> p2m_put_page() doesn't have a put for level 1, so the references of
> such a mapping are never dropped, the pages are never freed and
> destroying the owner never completes.
>
> Putting the references of a 1GB block at once would take too long
> without being able to preempt, since relinquish_p2m_mapping() can only
> be preempted between entries. By changing the order to 2MB,
> __p2m_set_entry() can split the superpage and therefore put 2MB
> blocks, allowing the preemption check in relinquish_p2m_mapping() to
> work properly. For that, the check also needs ">=" instead of ">".
>
> Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
> ---
> xen/arch/arm/mmu/p2m.c | 19 ++++++++++++++++++-
> 1 file changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
> index bc6d196..e7a27db 100644
> --- a/xen/arch/arm/mmu/p2m.c
> +++ b/xen/arch/arm/mmu/p2m.c
> @@ -1595,6 +1595,13 @@ int relinquish_p2m_mapping(struct domain *d)
> start = gfn_next_boundary(start, order) )
> {
> mfn_t mfn = p2m_get_entry(p2m, start, &t, NULL, &order, NULL);
> + bool split = false;
> +
> + if ( p2m_is_foreign(t) && order == XEN_PT_LEVEL_ORDER(1) )
> + {
> + order = XEN_PT_LEVEL_ORDER(2);
> + split = true;
> + }
>
> count++;
> /*
> @@ -1602,7 +1609,7 @@ int relinquish_p2m_mapping(struct domain *d)
> * foreign mapping.
> */
> if ( (!(count % 512) ||
> - (p2m_is_foreign(t) && (order > XEN_PT_LEVEL_ORDER(2)))) &&
> + (p2m_is_foreign(t) && (order >= XEN_PT_LEVEL_ORDER(2)))) &&
> hypercall_preempt_check() )
> {
> rc = -ERESTART;
> @@ -1621,6 +1628,16 @@ int relinquish_p2m_mapping(struct domain *d)
> */
> rc = __p2m_set_entry(p2m, start, order, INVALID_MFN,
> p2m_invalid, p2m_access_rwx);
> + if ( unlikely(rc == -ENOMEM && split) )
> + {
> + /*
> + * Shattering the foreign superpage needs a page from the P2M
> + * pool. If none is left, remove the whole superpage at once.
> + */
Wouldn't this re-introduce the long running operation you were trying to
prevent?
But I think it is not great to require memory when tearing down the
domain. I would prefer if we consider dropping support for 1GiB foreign
and always force mapping with 2MiB.
Cheers,
--
Julien Grall
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption
2026-12-02 10:06 ` Grall, Julien
@ 2026-12-02 11:08 ` Gabriel Quintáns Souto
0 siblings, 0 replies; 6+ messages in thread
From: Gabriel Quintáns Souto @ 2026-12-02 11:08 UTC (permalink / raw)
To: Grall, Julien, Gabriel Quintáns Souto, xen-devel
Cc: Luca Fancellu, Stefano Stabellini, Bertrand Marquis, Michal Orzel,
Volodymyr Babchuk
On Wed Dec 2, 2026 at 11:06 AM CET, Julien Grall wrote:
> Hi Gabriel,
>
> Thank you for the series.
>
> On 30/11/2026 16:43, Gabriel Quintáns Souto wrote:
>> Static shared memory can create 1GB foreign mappings, as
>> p2m_set_entry() picks the mapping size based only on alignment.
>> p2m_put_page() doesn't have a put for level 1, so the references of
>> such a mapping are never dropped, the pages are never freed and
>> destroying the owner never completes.
>>
>> Putting the references of a 1GB block at once would take too long
>> without being able to preempt, since relinquish_p2m_mapping() can only
>> be preempted between entries. By changing the order to 2MB,
>> __p2m_set_entry() can split the superpage and therefore put 2MB
>> blocks, allowing the preemption check in relinquish_p2m_mapping() to
>> work properly. For that, the check also needs ">=" instead of ">".
>>
>> Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
>> ---
>> xen/arch/arm/mmu/p2m.c | 19 ++++++++++++++++++-
>> 1 file changed, 18 insertions(+), 1 deletion(-)
>>
>> diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
>> index bc6d196..e7a27db 100644
>> --- a/xen/arch/arm/mmu/p2m.c
>> +++ b/xen/arch/arm/mmu/p2m.c
>> @@ -1595,6 +1595,13 @@ int relinquish_p2m_mapping(struct domain *d)
>> start = gfn_next_boundary(start, order) )
>> {
>> mfn_t mfn = p2m_get_entry(p2m, start, &t, NULL, &order, NULL);
>> + bool split = false;
>> +
>> + if ( p2m_is_foreign(t) && order == XEN_PT_LEVEL_ORDER(1) )
>> + {
>> + order = XEN_PT_LEVEL_ORDER(2);
>> + split = true;
>> + }
>>
>> count++;
>> /*
>> @@ -1602,7 +1609,7 @@ int relinquish_p2m_mapping(struct domain *d)
>> * foreign mapping.
>> */
>> if ( (!(count % 512) ||
>> - (p2m_is_foreign(t) && (order > XEN_PT_LEVEL_ORDER(2)))) &&
>> + (p2m_is_foreign(t) && (order >= XEN_PT_LEVEL_ORDER(2)))) &&
>> hypercall_preempt_check() )
>> {
>> rc = -ERESTART;
>> @@ -1621,6 +1628,16 @@ int relinquish_p2m_mapping(struct domain *d)
>> */
>> rc = __p2m_set_entry(p2m, start, order, INVALID_MFN,
>> p2m_invalid, p2m_access_rwx);
>> + if ( unlikely(rc == -ENOMEM && split) )
>> + {
>> + /*
>> + * Shattering the foreign superpage needs a page from the P2M
>> + * pool. If none is left, remove the whole superpage at once.
>> + */
>
> Wouldn't this re-introduce the long running operation you were trying to
> prevent?
>
With the whole series applied, relinquish_p2m_mapping() shatters the
1GB mapping into 2MB mappings, and then calls __p2m_set_entry() on
each of them with preemption. It only falls back to the level 1 put
handler if the P2M pool is exhausted, which should be rare.
> But I think it is not great to require memory when tearing down the
> domain. I would prefer if we consider dropping support for 1GiB foreign
> and always force mapping with 2MiB.
>
> Cheers,
IMO it's better to keep 1GB foreign mappings, since forcing 2MB
changes how any 1GB aligned static shm region is mapped and loses
the benefit of 1GB blocks. The memory needed at teardown is one page
from the P2M pool per 1GB foreign mapping, and if it isn't available
the fallback still puts the pages.
That said, if you still prefer forcing 2MB, I'll do that in v2.
Best regards,
Gabriel
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 11:08 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:43 [PATCH 0/3] xen/arm: p2m: free 1GB static shared memory on domain destroy Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 1/3] xen/arm: p2m: add __p2m_free_entry() with a put argument Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 2/3] xen/arm: p2m: split level 1 foreign superpages for preemption Gabriel Quintáns Souto
2026-12-02 10:06 ` Grall, Julien
2026-12-02 11:08 ` Gabriel Quintáns Souto
2026-11-30 16:43 ` [PATCH 3/3] xen/arm: p2m: support level 1 puts if the superpage cannot be split Gabriel Quintáns Souto
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.