tools/libs/guest: re-read dominfo after changing a PV guest's width on restore
PATCH v2 1/2 tools/libs/guest: re-read dominfo after changing a PV guest's width on restore
The x86 PV restore code reads the domain's information once, when the stream starts, and keeps it in ctx->dominfo. When the X86_PV_INFO record gives a width other than the domain's, handle_x86_pv_info() changes it with XEN_DOMCTL_set_address_size, and then refreshes only the guest's width and page-table levels.
Since 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G"), making a domain 32-bit may replace a shared_info page (if the originally allocated page was above 4GiB). The shared_info_frame read when the stream started will refer to a freed page. handle_shared_info() maps that frame to write the SHARED_INFO record, and process_start_info() puts it in the guest's start_info. Xen holds no reference to the replaced page by then, so the mapping fails, and with it the restore of any 32-bit PV guest whose shared_info page was moved.
Re-read the domain's information after changing its width.
---
Not tested end to end: no 32-bit PV guest here can suspend. Restore's steps replayed from dom0 on staging, on an Intel box (create a PV domain, read its info, make it 32-bit, populate 1GiB, then map the frame read first): that map fails with -EBUSY every time, while the frame read after the switch maps. Build-tested.
--- tools/libs/guest/xg_sr_restore_x86_pv.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/libs/guest/xg_sr_restore_x86_pv.c +6 -0
@@ -646,6 +646,12 @@ static int handle_x86_pv_info(struct xc_sr_context *ctx, }/* Domain's information changed, better to refresh. */ +if (xc_domain_getinfo_single (xch, ctx->domid, &ctx->dominfo) <0 ) + { +PERROR ("Failed to refresh dominfo for d%d" , ctx->domid); +return -1 ; + } + rc =x86_pv_domain_info (ctx);if ( rc !=0 ) {
Signature · 1 line
2.56.0
PATCH v2 2/2 x86/pv: free the shared_info page switch_compat() replaces
For a 32-bit PV guest, switch_compat() replaces a shared_info page above 4GiB with one below, and drops the old page with a bare put_page().
Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call free_xenheap_page() on them for them to return to their pool.
d->shared_info here only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is the PGC_allocated bit. Dropping the reference without having been the one to clear the PGC_allocated bit puts the page in an invalid state.
If there are no outstanding references to d->shared_info when switch_compat() happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever calls free_xenheap_page() on it. So whenever this path is triggered, a page is leaked.
Theoretically, if there *is* an outstanding reference to d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction, relinquish_memory() walks the list, sees the PGC_allocated bit, and calls put_page() again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page() only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping of shared_info that it holds across that call.
Release the page the way domain destruction releases d->shared_info:
drop the allocation reference with put_page_alloc_ref(), as relinquish_memory() does, which clears PGC_allocated and takes the page off the xenheap list once nothing else refers to it, and free it with free_xenheap_page() once an RCU grace period has passed.
The free waits for the grace period because do_settime() writes every domain's wallclock holding only wc_lock and the domain list's RCU read lock, neither of which excludes switch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head, d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user, domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference, domain_destroy() queues d->rcu again, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.
Freeing the page relies on "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the saved shared_info into.
Also allocate the new page into a local variable rather than straight into d->shared_info: if the allocation failed, a concurrent do_settime() could dereference the NULL pointer before the old value was restored.
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".
Tested on an Intel box (debug build, PV dom0), with restore's steps replayed from dom0 in the order libxenguest takes them without "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore" (no 32-bit PV guest here can suspend). Once the domain's memory is populated, the replaced page comes back as its pfn 0 every time, and maps. With a mapping of the old page held across the switch, the page is leaked with the new message, and the unmap is clean.
--- xen/arch/x86/include/asm/domain.h | 3 +++ xen/arch/x86/pv/domain.c | 43 ++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-)
xen/arch/x86/include/asm/domain.h +3 -0
@@ -302,6 +302,9 @@ struct pv_domain/* Maximum physical-address bitwidth supported by this guest. */ unsigned int physaddr_bitsize; + +/* shared_info page switch_compat() replaced, until it is freed. */ +void *old_shared_info;#endif /* map_domain_page() mapping cache. */
xen/arch/x86/pv/domain.c +37 -6
66 lines not on this page · raw message
Signature · 1 line
2.56.0
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
Quoted 72 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.
Quoted 4 lines
Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
In reply to George DunlapQuoted 76 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>
Quoted 6 lines
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".
Actually, is there a dependency here as in "must be applied in this order"? It looks to me as if the 2nd patch going in first wouldn't make the situation worse.
Jan
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
Quoted 85 lines · Jan Beulich
Quoted 76 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>Quoted 6 lines
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".
Quoted 3 lines
Actually, is there a dependency here as in "must be applied in this order"? It looks to me as if the 2nd patch going in first wouldn't make the situation worse.
Before patch 2, the stale frame will always have a refcount of 0, so the map will always fail, causing the restore to fail. (And in the case of a live migrate, causing the attempted sending domain to resume.)
After patch 2, the stale frame may be in one of three states:
a) Free, refcount of zero. libxc's map will fail the same way, failing the restore.
b) Allocated to a different domain. This will also fail, because it will have the wrong domain, failing the restore.
c) Allocated to the same domain. In this case, the map will succeed:
libxc will restore the contents into the wrong page, and then write a truncated frame into the start_info that it passes to the guest. This is a lot worse than the failed restore.
In Claude's experiments, on an idle system, the page gets re-allocated to the same domain 10/10 times.
-George
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
Quoted 103 lines · George Dunlap
Quoted 85 lines · Jan Beulich
Quoted 76 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>Quoted 6 lines
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".Quoted 3 lines
Actually, is there a dependency here as in "must be applied in this order"? It looks to me as if the 2nd patch going in first wouldn't make the situation worse.
Before patch 2, the stale frame will always have a refcount of 0, so the map will always fail, causing the restore to fail. (And in the case of a live migrate, causing the attempted sending domain to resume.)
After patch 2, the stale frame may be in one of three states:
a) Free, refcount of zero. libxc's map will fail the same way, failing the restore.
b) Allocated to a different domain. This will also fail, because it will have the wrong domain, failing the restore.
Quoted 7 lines
c) Allocated to the same domain. In this case, the map will succeed:
libxc will restore the contents into the wrong page, and then write a truncated frame into thestart_infothat it passes to the guest. This is a lot worse than the failed restore.In Claude's experiments, on an idle system, the page gets re-allocated to the same domain 10/10 times.
Hmm, all of this to me reads like an analysis of what happens if only patch 1 went in. But the question was whether putting in patch 2 without patch 1 would cause things to be worse than before.
Jan
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
Quoted 110 lines · Jan Beulich
Quoted 108 lines · George Dunlap
Quoted 85 lines · Jan Beulich
Quoted 76 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>Quoted 6 lines
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".Quoted 3 lines
Actually, is there a dependency here as in "must be applied in this order"? It looks to me as if the 2nd patch going in first wouldn't make the situation worse.
Before patch 2, the stale frame will always have a refcount of 0, so the map will always fail, causing the restore to fail. (And in the case of a live migrate, causing the attempted sending domain to resume.)
After patch 2, the stale frame may be in one of three states:
a) Free, refcount of zero. libxc's map will fail the same way, failing the restore.
b) Allocated to a different domain. This will also fail, because it will have the wrong domain, failing the restore.
c) Allocated to the same domain. In this case, the map will succeed:
libxc will restore the contents into the wrong page, and then write a truncated frame into thestart_infothat it passes to the guest. This is a lot worse than the failed restore.
Quoted 6 lines
Quoted 2 lines
In Claude's experiments, on an idle system, the page gets re-allocated to the same domain 10/10 times.
Hmm, all of this to me reads like an analysis of what happens if only patch 1 went in. But the question was whether putting in patch 2 without patch 1 would cause things to be worse than before.
I'm a bit confused. Are we talking about the same patch 1 and 2? If we put in the current patch we're discussing, "[PATCH v2 2/2] x86/pv:
free the shared_info page switch_compat() replaces", without 1/2, then we'll have the issue described above until patch 1 is applied. 1/2 fixes the stale-shared_info-on-restore problem before 2/2 enables the old shared_info to actually be freed and reused.
I don't see how putting patch 1/2 in without patch 2/2 would cause the issues described.
-George
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
Quoted 114 lines · George Dunlap
Quoted 113 lines · Jan Beulich
Quoted 110 lines · George Dunlap
Quoted 85 lines · Jan Beulich
Quoted 76 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>Quoted 6 lines
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".Quoted 3 lines
Actually, is there a dependency here as in "must be applied in this order"? It looks to me as if the 2nd patch going in first wouldn't make the situation worse.
Before patch 2, the stale frame will always have a refcount of 0, so the map will always fail, causing the restore to fail. (And in the case of a live migrate, causing the attempted sending domain to resume.)
After patch 2, the stale frame may be in one of three states:
a) Free, refcount of zero. libxc's map will fail the same way, failing the restore.
b) Allocated to a different domain. This will also fail, because it will have the wrong domain, failing the restore.
c) Allocated to the same domain. In this case, the map will succeed:
libxc will restore the contents into the wrong page, and then write a truncated frame into thestart_infothat it passes to the guest. This is a lot worse than the failed restore.In Claude's experiments, on an idle system, the page gets re-allocated to the same domain 10/10 times.
Quoted 8 lines
Quoted 3 lines
Hmm, all of this to me reads like an analysis of what happens if only patch 1 went in. But the question was whether putting in patch 2 without patch 1 would cause things to be worse than before.
I'm a bit confused. Are we talking about the same patch 1 and 2? If we put in the current patch we're discussing, "[PATCH v2 2/2] x86/pv:
free theshared_infopageswitch_compat()replaces", without 1/2, then we'll have the issue described above until patch 1 is applied.
Right, but is this any worse than the situation without either patch applied?
Quoted 6 lines
1/2
fixes the stale-shared_info-on-restore problem before 2/2 enables the oldshared_infoto actually be freed and reused.I don't see how putting patch 1/2 in without patch 2/2 would cause the issues described.
No question that applying in-order will yield a good result. The question is whether I need to wait with putting in patch 2 until patch 1 has been acked.
Jan
Re: [PATCH v2 2/2] x86/pv: free the shared_info page switch_compat() replaces
Quoted 120 lines · Jan Beulich
Quoted 118 lines · George Dunlap
Quoted 116 lines · Jan Beulich
Quoted 103 lines · George Dunlap
Quoted 85 lines · Jan Beulich
Quoted 76 lines · George Dunlap
For a 32-bit PV guest,
switch_compat()replaces ashared_infopage above 4GiB with one below, and drops the old page with a bareput_page().Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call
free_xenheap_page()on them for them to return to their pool.
d->shared_infohere only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is thePGC_allocatedbit. Dropping the reference without having been the one to clear thePGC_allocatedbit puts the page in an invalid state.If there are no outstanding references to
d->shared_infowhenswitch_compat()happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever callsfree_xenheap_page()on it. So whenever this path is triggered, a page is leaked.Theoretically, if there *is* an outstanding reference to
d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction,relinquish_memory()walks the list, sees thePGC_allocatedbit, and callsput_page()again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page()only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping ofshared_infothat it holds across that call.Release the page the way domain destruction releases
d->shared_info:
drop the allocation reference withput_page_alloc_ref(), asrelinquish_memory()does, which clearsPGC_allocatedand takes the page off the xenheap list once nothing else refers to it, and free it withfree_xenheap_page()once an RCU grace period has passed.The free waits for the grace period because
do_settime()writes every domain's wallclock holding onlywc_lockand the domain list's RCU read lock, neither of which excludesswitch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head,d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user,domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference,domain_destroy()queuesd->rcuagain, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. If something holds one all the same (a toolstack that mapped the page before the switch, for instance), leave the page alone and leak it, rather than free a page that is still in use.Freeing the page relies on "
tools/libs/guest: re-read dominfo after changing a PV guest's width on restore", without which restoring a 32-bit PV guest maps the replaced page by the frame number it read before the switch. Once the page is freed, that frame can come back as one of the restored domain's own pages, which restore would then write the savedshared_infointo.Also allocate the new page into a local variable rather than straight into
d->shared_info: if the allocation failed, a concurrentdo_settime()could dereference the NULL pointer before the old value was restored.Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Suggested-by Jan Beulich <[email protected]>Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>Quoted 6 lines
---
Changes in v2:
- If the page is still referenced once its allocation reference has been dropped, leak it rather than free it (Jan).
- Depends on a new patch, "tools/libs/guest: re-read dominfo after changing a PV guest's width on restore".Quoted 3 lines
Actually, is there a dependency here as in "must be applied in this order"? It looks to me as if the 2nd patch going in first wouldn't make the situation worse.
Before patch 2, the stale frame will always have a refcount of 0, so the map will always fail, causing the restore to fail. (And in the case of a live migrate, causing the attempted sending domain to resume.)
After patch 2, the stale frame may be in one of three states:
a) Free, refcount of zero. libxc's map will fail the same way, failing the restore.
b) Allocated to a different domain. This will also fail, because it will have the wrong domain, failing the restore.
Quoted 7 lines
c) Allocated to the same domain. In this case, the map will succeed:
libxc will restore the contents into the wrong page, and then write a truncated frame into thestart_infothat it passes to the guest. This is a lot worse than the failed restore.In Claude's experiments, on an idle system, the page gets re-allocated to the same domain 10/10 times.
Hmm, all of this to me reads like an analysis of what happens if only patch 1 went in. But the question was whether putting in patch 2 without patch 1 would cause things to be worse than before.
Quoted 7 lines
Quoted 4 lines
I'm a bit confused. Are we talking about the same patch 1 and 2? If we put in the current patch we're discussing, "[PATCH v2 2/2] x86/pv:
free theshared_infopageswitch_compat()replaces", without 1/2, then we'll have the issue described above until patch 1 is applied.Right, but is this any worse than the situation without either patch applied?
Yes? To repeat myself: before patch 2, the bug in patch 1 always gives you a clean failure. After patch 2, the bug in patch 1 usually results in non-clean failure, and may result in silent corruption instead.
-George
Quoted 38 lines · George Dunlap
The x86 PV restore code reads the domain's information once, when the stream starts, and keeps it in
ctx->dominfo. When theX86_PV_INFOrecord gives a width other than the domain's,handle_x86_pv_info()changes it withXEN_DOMCTL_set_address_size, and then refreshes only the guest's width and page-table levels.Since
8a9255405ca6("x86/domain: adjust limitation onshared_infoallocation below 4G"), making a domain 32-bit may replace ashared_infopage (if the originally allocated page was above 4GiB). Theshared_info_frameread when the stream started will refer to a freed page.handle_shared_info()maps that frame to write theSHARED_INFOrecord, andprocess_start_info()puts it in the guest'sstart_info. Xen holds no reference to the replaced page by then, so the mapping fails, and with it the restore of any 32-bit PV guest whoseshared_infopage was moved.Re-read the domain's information after changing its width.
Fixes 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocation below 4G")Assisted-by Claude Code:claude-opus-5-5Signed-off-by George Dunlap <[email protected]>---
Not tested end to end: no 32-bit PV guest here can suspend. Restore's steps replayed from dom0 on staging, on an Intel box (create a PV domain, read its info, make it 32-bit, populate 1GiB, then map the frame read first): that map fails with-EBUSYevery time, while the frame read after the switch maps. Build-tested.--- tools/libs/guest/xg_sr_restore_x86_pv.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/libs/guest/xg_sr_restore_x86_pv.c +0
-0@@ -646,6 +646,12 @@ static int handle_x86_pv_info(struct xc_sr_context *ctx, }
tools/libs/guest/xg_sr_restore_x86_pv.c static int handle_x86_pv_info(struct xc_sr_context *ctx, · line 657
Quoted 10 lines
tools/libs/guest/xg_sr_restore_x86_pv.c +6
-0/* Domain's information changed, better to refresh. */ +if (xc_domain_getinfo_single (xch, ctx->domid, &ctx->dominfo) <0 ) + { +PERROR ("Failed to refresh dominfo for d%d" , ctx->domid); +return -1 ; + } + rc =x86_pv_domain_info (ctx);if ( rc !=0 ) {
Oh lovely.
I think I'm going to start by putting together a real migration test including PV32. We *need* to stop errors this simple from slipping in, and there have been quite a few recently.
~Andrew