x86/p2m: fix xenmem_add_to_physmap_one double page removal
PATCH v2 1/1 x86/p2m: fix xenmem_add_to_physmap_one double page removal
If the new gfn matches the previous one (ie: gpfn == old_gpfn) xenmem_add_to_physmap_one will issue a duplicated call to guest_physmap_remove_page with the same guest frame number, because the get_gpfn_from_mfn call has been moved by commit f8582da041 to be performed before the original page is removed. This leads to the second guest_physmap_remove_page failing, which was not the case before commit f8582da041.
Add a shortcut to skip modifying the p2m if the mapping is already as requested.
---
Changes since v1:
- Short circuit and skip modifying the p2m.
--- xen/arch/x86/mm/p2m.c | 7 +++++++ 1 file changed, 7 insertions(+)
xen/arch/x86/mm/p2m.c +7 -0
@@ -2799,6 +2799,13 @@ int xenmem_add_to_physmap_one( goto put_all; } +if (gfn_eq (_gfn (old_gpfn), gpfn) ) + { +/* Nothing to do, mapping is already as requested. */ +ASSERT (mfn_eq (prev_mfn, mfn)); + goto put_all; + } +/* Remove previously mapped page if it was present. */ if ( p2mt == p2m_mmio_direct ) rc = -EPERM ;
Signature · 1 line
2.33.0
Quoted 10 lines · Roger Pau Monne
If the new gfn matches the previous one (ie: gpfn ==
old_gpfn)xenmem_add_to_physmap_onewill issue a duplicated call toguest_physmap_remove_pagewith the same guest frame number, because theget_gpfn_from_mfncall has been moved by commitf8582da041to be performed before the original page is removed. This leads to the secondguest_physmap_remove_pagefailing, which was not the case before commitf8582da041.Add a shortcut to skip modifying the p2m if the mapping is already as requested.
I've meanwhile had further thoughts here - not sure in how far they affect what to do (including whether to go back to v1, with the description's 1st paragraph adjusted as per above):
xen/arch/x86/mm/p2m.c int xenmem_add_to_physmap_one( · line 2807
Quoted 12 lines
xen/arch/x86/mm/p2m.c +6
-0@@ -2799,6 +2799,13 @@ int xenmem_add_to_physmap_one( goto put_all; } +if (gfn_eq (_gfn (old_gpfn), gpfn) ) + { +/* Nothing to do, mapping is already as requested. */ +ASSERT (mfn_eq (prev_mfn, mfn)); + goto put_all; + }
The mapping may not be "already as requested" because of p2m type or p2m access. Thoughts? (At the very least the new check would likely want to move after the p2m_mmio_direct one.)
I've also meanwhile realized that it was a different form of short circuiting that I had been thinking about before: XENMAPSPACE_gmfn's idx == gpfn.
Jan
Quoted 22 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monne
If the new gfn matches the previous one (ie: gpfn ==
old_gpfn)xenmem_add_to_physmap_onewill issue a duplicated call toguest_physmap_remove_pagewith the same guest frame number, because theget_gpfn_from_mfncall has been moved by commitf8582da041to be performed before the original page is removed. This leads to the secondguest_physmap_remove_pagefailing, which was not the case before commitf8582da041.Add a shortcut to skip modifying the p2m if the mapping is already as requested.
I've meanwhile had further thoughts here - not sure in how far they affect what to do (including whether to go back to v1, with the description's 1st paragraph adjusted as per above):
Quoted 6 lines
xen/arch/x86/mm/p2m.c +0
-0@@ -2799,6 +2799,13 @@ int xenmem_add_to_physmap_one( goto put_all; }
Quoted 10 lines
xen/arch/x86/mm/p2m.c int xenmem_add_to_physmap_one( · line 2807
Quoted 6 lines
xen/arch/x86/mm/p2m.c +6
-0+if (gfn_eq (_gfn (old_gpfn), gpfn) ) + { +/* Nothing to do, mapping is already as requested. */ +ASSERT (mfn_eq (prev_mfn, mfn)); + goto put_all; + }The mapping may not be "already as requested" because of p2m type or p2m access. Thoughts? (At the very least the new check would likely want to move after the
p2m_mmio_directone.)
Is the type really relevant for the caller? If the mapping is already setup as requested, I don't think it makes sense to return -EPERM if the type is mmio. I'm also unsure whether we can get into that state in the first place.
I'm unsure regarding the access, I would say changing the access type should be done by other means rather that replying on xenmem_add_to_physmap.
v1 was indeed more similar to the previous behavior IMO, so it's likely a safer approach.
Thanks, Roger.
Quoted 34 lines · Roger Pau Monné
Quoted 22 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monne
If the new gfn matches the previous one (ie: gpfn ==
old_gpfn)xenmem_add_to_physmap_onewill issue a duplicated call toguest_physmap_remove_pagewith the same guest frame number, because theget_gpfn_from_mfncall has been moved by commitf8582da041to be performed before the original page is removed. This leads to the secondguest_physmap_remove_pagefailing, which was not the case before commitf8582da041.Add a shortcut to skip modifying the p2m if the mapping is already as requested.
I've meanwhile had further thoughts here - not sure in how far they affect what to do (including whether to go back to v1, with the description's 1st paragraph adjusted as per above):
Quoted 6 lines
xen/arch/x86/mm/p2m.c +0
-0@@ -2799,6 +2799,13 @@ int xenmem_add_to_physmap_one( goto put_all; }Quoted 10 lines
xen/arch/x86/mm/p2m.c int xenmem_add_to_physmap_one( · line 2807
Quoted 6 lines
xen/arch/x86/mm/p2m.c +6
-0+if (gfn_eq (_gfn (old_gpfn), gpfn) ) + { +/* Nothing to do, mapping is already as requested. */ +ASSERT (mfn_eq (prev_mfn, mfn)); + goto put_all; + }The mapping may not be "already as requested" because of p2m type or p2m access. Thoughts? (At the very least the new check would likely want to move after the
p2m_mmio_directone.)
Quoted 4 lines
Is the type really relevant for the caller? If the mapping is already setup as requested, I don't think it makes sense to return
-EPERMif the type is mmio. I'm also unsure whether we can get into that state in the first place.
mmio perhaps indeed can't occur (because of the earlier get_page_from_mfn()), but in general the type might very well be
relevant: The seemingly benign change might e.g. change logdirty to ram_rw. Whether that's for good or bad is a different matter.
Quoted 3 lines
I'm unsure regarding the access, I would say changing the access type should be done by other means rather that replying on
xenmem_add_to_physmap.
It _should_, yes, but there might be callers relying on it changing as a side effect here. (There might also be callers abusing the call for this purpose.)
Quoted 2 lines
v1 was indeed more similar to the previous behavior IMO, so it's likely a safer approach.
Okay, I'll commit v1 with the slightly adjusted description then.
Jan