Skip to contentSkip to conversation

x86/mm: Mark pages as dirty after (rather than before) writing to them

PATCH In review Andrew Cooper 2 participants 2 messages May 11, 2017 – May 12, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mm: Mark pages as dirty after (rather than before) writing to them

Otherwise a logdirty client can race with observing the page becoming dirty, and copy the frame before the write is complete and end up with a stale version.

---
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>

---
 xen/arch/x86/mm.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
xen/arch/x86/mm.c +4 -6
index 77b0af1..97c3cb8 100644
@@ -3556,11 +3556,10 @@ long do_mmuext_op(
                 break;
             }
 
-            /* A page is dirtied when it's being cleared. */
-            paging_mark_dirty(pg_owner, _mfn(page_to_mfn(page)));
-
             clear_domain_page(_mfn(page_to_mfn(page)));
 
+            paging_mark_dirty(pg_owner, _mfn(page_to_mfn(page)));
+
             put_page_and_type(page);
             break;
         }
@@ -3594,12 +3593,11 @@ long do_mmuext_op(
                 break;
             }
 
-            /* A page is dirtied when it's being copied to. */
-            paging_mark_dirty(pg_owner, _mfn(page_to_mfn(dst_page)));
-
             copy_domain_page(_mfn(page_to_mfn(dst_page)),
                              _mfn(page_to_mfn(src_page)));
 
+            paging_mark_dirty(pg_owner, _mfn(page_to_mfn(dst_page)));
+
             put_page_and_type(dst_page);
             put_page(src_page);
             break;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 11.05.17 at 19:08, <[email protected]> wrote:

Otherwise a logdirty client can race with observing the page becoming dirty, and copy the frame before the write is complete and end up with a stale version.

So I must have looked at the wrong examples back when writing this code. There are a couple of oddities, but in particular I wonder why you wouldn't want to also move the use in do_update_descriptor(). Similarly I'd expect hvm_unmap_guest_frame() would want to mark the page dirty in the !permanent case instead of _hvm_map_guest_frame() (I'm afraid it's again me who is guilty of having got this one wrong).

As to oddities:

Why does alloc_page_type() need to mark a page dirty? Wouldn't doing so in free_page_type() suffice?

Why does free_page_type() have an extra paging_mode_enabled() check which no other callers have?

Why are pages being marked dirty when they are being pinned? As per above, doing this when unpinning ought to suffice, and both seem redundant with what alloc_page_type() / free_page_type() do respectively.

Why does MMU_MACHPHYS_UPDATE require dirtying the MFN? Same for p2m_pod_demand_populate() and __mem_sharing_unshare_page() doing this next to a set_gpfn_from_mfn() call. (There I additionally wonder whether it shouldn't honor global log-dirty mode instead of unconditionally setting p2m_ram_rw.)

Why does hvm_add_ioreq_gmfn() mark the page dirty, but hvm_remove_ioreq_gmfn() doesn't (despite calling clear_page())? Isn't the page part of the guest no matter that it's not part of its physmap?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel