Skip to contentSkip to conversation

migration: Fix a potential guest memory corruption

PATCH In review Zhenzhong Duan 2 participants 3 messages Sep 19, 2022 – Oct 17, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: Fix a potential guest memory corruption

Imagine a rare case, after a dirty page is sent to compression threads's ring, dirty bitmap sync trigger right away and mark the same page dirty again and sent. Then the new page may be overwriten by stale page in compression threads's ring in the destination.

So we need to ensure there is only one copy of the same dirty page either by flushing the ring after every bitmap sync or avoiding processing same dirty page continuously.

I choose the 2nd which avoids the time consuming flush operation.

---
 migration/ram.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
migration/ram.c +4 -4
index dc1de9ddbc68..67b2035586bd 100644
@@ -1551,7 +1551,7 @@ static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
     pss->postcopy_requested = false;
     pss->postcopy_target_channel = RAM_CHANNEL_PRECOPY;
 
-    pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
+    pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page + 1);
     if (pss->complete_round && pss->block == rs->last_seen_block &&
         pss->page >= rs->last_page) {
         /*
@@ -1564,7 +1564,7 @@ static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
     if (!offset_in_ramblock(pss->block,
                             ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) {
         /* Didn't find anything in this RAM Block */
-        pss->page = 0;
+        pss->page = -1;
         pss->block = QLIST_NEXT_RCU(pss->block, next);
         if (!pss->block) {
             /*
@@ -2694,7 +2694,7 @@ static void ram_state_reset(RAMState *rs)
 {
     rs->last_seen_block = NULL;
     rs->last_sent_block = NULL;
-    rs->last_page = 0;
+    rs->last_page = -1;
     rs->last_version = ram_list.version;
     rs->xbzrle_enabled = false;
     postcopy_preempt_reset(rs);
@@ -2889,7 +2889,7 @@ void ram_postcopy_send_discard_bitmap(MigrationState *ms)
     /* Easiest way to make sure we don't resume in the middle of a host-page */
     rs->last_seen_block = NULL;
     rs->last_sent_block = NULL;
-    rs->last_page = 0;
+    rs->last_page = -1;
 
     postcopy_each_ram_send_discard(ms);
 
Signature · 1 line
2.25.1
#1 lore raw

* Zhenzhong Duan ([email protected]) wrote:

Hi,

Quoted 4 lines

Imagine a rare case, after a dirty page is sent to compression threads's ring, dirty bitmap sync trigger right away and mark the same page dirty again and sent. Then the new page may be overwriten by stale page in compression threads's ring in the destination.

Yes, I think we had a similar problem in multifd.

Quoted 5 lines

So we need to ensure there is only one copy of the same dirty page either by flushing the ring after every bitmap sync or avoiding processing same dirty page continuously.

I choose the 2nd which avoids the time consuming flush operation.

I'm not sure this guarantees it; it makes it much less likely but if only a few pages are dirtied and you have lots of threads, I think the same thing could still happy.

I think you're going to need to flush the ring after each sync.

Dave

Quoted 48 lines
---
 migration/ram.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
migration/ram.c +4 -4
index dc1de9ddbc68..67b2035586bd 100644
@@ -1551,7 +1551,7 @@ static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
     pss->postcopy_requested = false;
     pss->postcopy_target_channel = RAM_CHANNEL_PRECOPY;
 
-    pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
+    pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page + 1);
     if (pss->complete_round && pss->block == rs->last_seen_block &&
         pss->page >= rs->last_page) {
         /*
@@ -1564,7 +1564,7 @@ static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
     if (!offset_in_ramblock(pss->block,
                             ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) {
         /* Didn't find anything in this RAM Block */
-        pss->page = 0;
+        pss->page = -1;
         pss->block = QLIST_NEXT_RCU(pss->block, next);
         if (!pss->block) {
             /*
@@ -2694,7 +2694,7 @@ static void ram_state_reset(RAMState *rs)
 {
     rs->last_seen_block = NULL;
     rs->last_sent_block = NULL;
-    rs->last_page = 0;
+    rs->last_page = -1;
     rs->last_version = ram_list.version;
     rs->xbzrle_enabled = false;
     postcopy_preempt_reset(rs);
@@ -2889,7 +2889,7 @@ void ram_postcopy_send_discard_bitmap(MigrationState *ms)
     /* Easiest way to make sure we don't resume in the middle of a host-page */
     rs->last_seen_block = NULL;
     rs->last_sent_block = NULL;
-    rs->last_page = 0;
+    rs->last_page = -1;
 
     postcopy_each_ram_send_discard(ms);
 
Signature · 1 line
2.25.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#2 lore raw
Quoted 9 lines

-----Original Message-----
From: Dr. David Alan Gilbert <[email protected]>
Sent: Tuesday, October 11, 2022 7:06 PM
To: Duan, Zhenzhong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] migration: Fix a potential guest memory corruption

Quoted 3 lines

* Zhenzhong Duan ([email protected]) wrote:

Hi,

Hi,
Sorry for late response. Just back from vacation.

Quoted 7 lines
Quoted 5 lines

Imagine a rare case, after a dirty page is sent to compression threads's ring, dirty bitmap sync trigger right away and mark the same page dirty again and sent. Then the new page may be overwriten by stale page in compression threads's ring in the destination.

Yes, I think we had a similar problem in multifd.

Multifd flush operation multifd_send_sync_main() is called in each memory iteration which is more aggressive than in compression. I think not an issue in multifd?

Quoted 10 lines
Quoted 6 lines

So we need to ensure there is only one copy of the same dirty page either by flushing the ring after every bitmap sync or avoiding processing same dirty page continuously.

I choose the 2nd which avoids the time consuming flush operation.

I'm not sure this guarantees it; it makes it much less likely but if only a few pages are dirtied and you have lots of threads, I think the same thing could still happy.

I didn't get it, imagine there are dirty page "A B C D E F G" in current RAMBLOCK.
1. Page "A B C D" are sent to compression thread.
2. dirty page sync triggers, update dirty map to "A B D E F G"
3. Page D is checked and sent to compression thread again, so there may be two copy of page D in compression thread, corruption!
4. Page "E F G" are sent to compression thread.
5. flush operation triggered at end of current RAMBLOCK.
6. In next iteration, page "A B" are sent.

After patch:
1. Page "A B C D" are sent to compression thread.
2. dirty page sync triggers, update dirty map to " A B D E F G"
3. Page after page D are checked and sent to compression thread which are Page "E F G".
5. flush operation triggered at end of current RAMBLOCK, ensures page D flushed.
6. In next iteration, page "A B D" are sent.

Thanks
Zhenzhong

Quoted 59 lines

I think you're going to need to flush the ring after each sync.

Dave

migration/ram.c static bool find_dirty_block(RAMState *rs,

Quoted 10 lines
---
 migration/ram.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
migration/ram.c index renamed from migration/ram.c +0 -0

dc1de9ddbc68..67b2035586bd 100644

migration/ram.c +0 -0
@@ -1551,7 +1551,7 @@ static bool find_dirty_block(RAMState *rs,

PageSearchStatus *pss, bool *again)

migration/ram.c static bool find_dirty_block(RAMState *rs,

Quoted 10 lines
migration/ram.c +2 -1
     pss->postcopy_requested = false;
     pss->postcopy_target_channel = RAM_CHANNEL_PRECOPY;
 
-    pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
+    pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page
+ + 1);
     if (pss->complete_round && pss->block == rs->last_seen_block &&
         pss->page >= rs->last_page) {
         /*
@@ -1564,7 +1564,7 @@ static bool find_dirty_block(RAMState *rs,

PageSearchStatus *pss, bool *again)

migration/ram.c void

Quoted 17 lines
migration/ram.c +2 -2
     if (!offset_in_ramblock(pss->block,
                             ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) {
         /* Didn't find anything in this RAM Block */
-        pss->page = 0;
+        pss->page = -1;
         pss->block = QLIST_NEXT_RCU(pss->block, next);
         if (!pss->block) {
             /*
@@ -2694,7 +2694,7 @@ static void ram_state_reset(RAMState *rs)  {
     rs->last_seen_block = NULL;
     rs->last_sent_block = NULL;
-    rs->last_page = 0;
+    rs->last_page = -1;
     rs->last_version = ram_list.version;
     rs->xbzrle_enabled = false;
     postcopy_preempt_reset(rs);
@@ -2889,7 +2889,7 @@ void

ram_postcopy_send_discard_bitmap(MigrationState *ms)

migration/ram.c void · line 2889

Quoted 1 line
migration/ram.c +0 -0
     /* Easiest way to make sure we don't resume in the middle of a host-page

*/

Quoted 10 lines
migration/ram.c +1 -2
     rs->last_seen_block = NULL;
     rs->last_sent_block = NULL;
-    rs->last_page = 0;
+    rs->last_page = -1;
 
     postcopy_each_ram_send_discard(ms);
 
--

2.25.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK