Skip to contentSkip to conversation

block: ignore flush requests when storage is clean

PATCH Superseded Denis V. Lunev 5 participants 14 messages Jun 27, 2016 – Jun 29, 2016 Block layer

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover block: ignore flush requests when storage is clean

Changes from v3:
- Fixed a typo in commit message
- Rebased on Kevin'n origin/block

Changes from v2:
- Better comments
- Rebased on latest master

Changes from v1:
- Flush requests that should be skipped will now wait for completion of any previous requests already in flight
- Fixed IDE and AHCI tests to dirty media for new flush behaviour
- Fixed a problem in IDE CMD_FLUSH_CACHE failure handling

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>

Evgeny Yakovlev (3):
block: ignore flush requests when storage is clean
ide: ignore retry_unit check for non-retry operation
tests: in IDE and AHCI tests perform DMA write before flushing

 block.c                   |  1 +
 block/dirty-bitmap.c      |  3 +++
 block/io.c                | 19 +++++++++++++++++++
 hw/ide/core.c             |  3 ++-
 include/block/block_int.h |  1 +
 tests/ahci-test.c         | 34 ++++++++++++++++++++++++++++++++--
 tests/ide-test.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 101 insertions(+), 3 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH v4 1/3 block: ignore flush requests when storage is clean

From: Evgeny Yakovlev <[email protected]>

Some guests (win2008 server for example) do a lot of unnecessary flushing when underlying media has not changed. This adds additional overhead on host when calling fsync/fdatasync.

This change introduces a dirty flag in BlockDriverState which is set in bdrv_set_dirty and is checked in bdrv_co_flush. This allows us to avoid unnecessary flushing when storage is clean.

The problem with excessive flushing was found by a performance test which does parallel directory tree creation (from 2 processes). Results improved from 0.424 loops/sec to 0.432 loops/sec. Each loop creates 10^3 directories with 10 files in each.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
 block.c                   |  1 +
 block/dirty-bitmap.c      |  3 +++
 block/io.c                | 19 +++++++++++++++++++
 include/block/block_int.h |  1 +
 4 files changed, 24 insertions(+)
block.c +1 -0
index 947df29..68ae3a0 100644
@@ -2581,6 +2581,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
         bdrv_dirty_bitmap_truncate(bs);
         bdrv_parent_cb_resize(bs);
+        bs->dirty = true; /* file node sync is needed after truncate */
     }
     return ret;
 }
block/dirty-bitmap.c +3 -0
index 4902ca5..54e0413 100644
@@ -370,6 +370,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
         }
         hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
     }
+
+    /* Set global block driver dirty flag even if bitmap is disabled */
+    bs->dirty = true;
 }
 
 /**
block/io.c +19 -0
index b9e53e3..152f5a9 100644
@@ -2247,6 +2247,25 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
         goto flush_parent;
     }
 
+    /* Check if storage is actually dirty before flushing to disk */
+    if (!bs->dirty) {
+        /* Flush requests are appended to tracked request list in order so that
+         * most recent request is at the head of the list. Following code uses
+         * this ordering to wait for the most recent flush request to complete
+         * to ensure that requests return in order */
+        BdrvTrackedRequest *prev_req;
+        QLIST_FOREACH(prev_req, &bs->tracked_requests, list) {
+            if (prev_req == &req || prev_req->type != BDRV_TRACKED_FLUSH) {
+                continue;
+            }
+
+            qemu_co_queue_wait(&prev_req->wait_queue);
+            break;
+        }
+        goto flush_parent;
+    }
+    bs->dirty = false;
+
     BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
     if (bs->drv->bdrv_co_flush_to_disk) {
         ret = bs->drv->bdrv_co_flush_to_disk(bs);
include/block/block_int.h +1 -0
index 0432ba5..59a7def 100644
@@ -435,6 +435,7 @@ struct BlockDriverState {
     bool valid_key; /* if true, a valid encryption key has been set */
     bool sg;        /* if true, the device is a /dev/sg* */
     bool probed;    /* if true, format was probed rather than specified */
+    bool dirty;     /* if true, media is dirty and should be flushed */
 
     int copy_on_read; /* if nonzero, copy read backing sectors into image.
                          note this is a reference count */
Signature · 1 line
2.1.4
#2 lore raw
Quoted 69 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Some guests (win2008 server for example) do a lot of unnecessary flushing when underlying media has not changed. This adds additional overhead on host when calling fsync/fdatasync.

This change introduces a dirty flag in BlockDriverState which is set in bdrv_set_dirty and is checked in bdrv_co_flush. This allows us to avoid unnecessary flushing when storage is clean.

The problem with excessive flushing was found by a performance test which does parallel directory tree creation (from 2 processes). Results improved from 0.424 loops/sec to 0.432 loops/sec. Each loop creates 10^3 directories with 10 files in each.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
 block.c                   |  1 +
 block/dirty-bitmap.c      |  3 +++
 block/io.c                | 19 +++++++++++++++++++
 include/block/block_int.h |  1 +
 4 files changed, 24 insertions(+)
block.c +1 -0
index 947df29..68ae3a0 100644
@@ -2581,6 +2581,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
         bdrv_dirty_bitmap_truncate(bs);
         bdrv_parent_cb_resize(bs);
+        bs->dirty = true; /* file node sync is needed after truncate */
     }
     return ret;
 }
block/dirty-bitmap.c +3 -0
index 4902ca5..54e0413 100644
@@ -370,6 +370,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
         }
         hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
     }
+
+    /* Set global block driver dirty flag even if bitmap is disabled */
+    bs->dirty = true;
 }
 
 /**
block/io.c +6 -0
index b9e53e3..152f5a9 100644
@@ -2247,6 +2247,25 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
         goto flush_parent;
     }
 
+    /* Check if storage is actually dirty before flushing to disk */
+    if (!bs->dirty) {
+        /* Flush requests are appended to tracked request list in order so that
+         * most recent request is at the head of the list. Following code uses
+         * this ordering to wait for the most recent flush request to complete
+         * to ensure that requests return in order */

block/io.c int coroutine_fn bdrv_co_flush(BlockDriverState *bs) · line 2265

Quoted 10 lines
block/io.c +10 -0
+        BdrvTrackedRequest *prev_req;
+        QLIST_FOREACH(prev_req, &bs->tracked_requests, list) {
+            if (prev_req == &req || prev_req->type != BDRV_TRACKED_FLUSH) {
+                continue;
+            }
+
+            qemu_co_queue_wait(&prev_req->wait_queue);
+            break;
+        }
+        goto flush_parent;

Should we check bs->dirty again after qemu_co_queue_wait()? I think another write request could sneak in while this coroutine yields.

Quoted 11 lines
block/io.c +3 -0
+    }
+    bs->dirty = false;
+
     BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
     if (bs->drv->bdrv_co_flush_to_disk) {
         ret = bs->drv->bdrv_co_flush_to_disk(bs);
include/block/block_int.h +0 -0
index 0432ba5..59a7def 100644

include/block/block_int.h struct BlockDriverState { · line 438

Quoted 4 lines
include/block/block_int.h +1 -0
@@ -435,6 +435,7 @@ struct BlockDriverState {
     bool valid_key; /* if true, a valid encryption key has been set */
     bool sg;        /* if true, the device is a /dev/sg* */
     bool probed;    /* if true, format was probed rather than specified */
+    bool dirty;     /* if true, media is dirty and should be flushed */

How about renaming this to "need_flush"? The one "dirty" we had is set by bdrv_set_dirty, and cleared by bdrv_reset_dirty_bitmap. I'd avoid the confusion between the two concepts.

Fam

Quoted 6 lines
include/block/block_int.h +0 -0
     int copy_on_read; /* if nonzero, copy read backing sectors into image.
                          note this is a reference count */
Signature · 1 line
2.1.4
#3 lore raw
Quoted 74 lines · Fam Zheng
Quoted 73 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Some guests (win2008 server for example) do a lot of unnecessary flushing when underlying media has not changed. This adds additional overhead on host when calling fsync/fdatasync.

This change introduces a dirty flag in BlockDriverState which is set in bdrv_set_dirty and is checked in bdrv_co_flush. This allows us to avoid unnecessary flushing when storage is clean.

The problem with excessive flushing was found by a performance test which does parallel directory tree creation (from 2 processes). Results improved from 0.424 loops/sec to 0.432 loops/sec. Each loop creates 10^3 directories with 10 files in each.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  block.c                   |  1 +
  block/dirty-bitmap.c      |  3 +++
  block/io.c                | 19 +++++++++++++++++++
  include/block/block_int.h |  1 +
  4 files changed, 24 insertions(+)
block.c +1 -0
index 947df29..68ae3a0 100644
@@ -2581,6 +2581,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
          ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
          bdrv_dirty_bitmap_truncate(bs);
          bdrv_parent_cb_resize(bs);
+        bs->dirty = true; /* file node sync is needed after truncate */
      }
      return ret;
  }
block/dirty-bitmap.c +3 -0
index 4902ca5..54e0413 100644
@@ -370,6 +370,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
          }
          hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
      }
+
+    /* Set global block driver dirty flag even if bitmap is disabled */
+    bs->dirty = true;
  }
 
  /**
block/io.c +10 -0
index b9e53e3..152f5a9 100644
@@ -2247,6 +2247,25 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
          goto flush_parent;
      }
 
+    /* Check if storage is actually dirty before flushing to disk */
+    if (!bs->dirty) {
+        /* Flush requests are appended to tracked request list in order so that
+         * most recent request is at the head of the list. Following code uses
+         * this ordering to wait for the most recent flush request to complete
+         * to ensure that requests return in order */
+        BdrvTrackedRequest *prev_req;
+        QLIST_FOREACH(prev_req, &bs->tracked_requests, list) {
+            if (prev_req == &req || prev_req->type != BDRV_TRACKED_FLUSH) {
+                continue;
Quoted 8 lines

block/io.c int coroutine_fn bdrv_co_flush(BlockDriverState *bs) · line 2265

Quoted 6 lines
block/io.c +6 -0
+            }
+
+            qemu_co_queue_wait(&prev_req->wait_queue);
+            break;
+        }
+        goto flush_parent;

Should we check bs->dirty again after qemu_co_queue_wait()? I think another write request could sneak in while this coroutine yields.

no, we do not care. Any subsequent to FLUSH write does not guaranteed to be flushed. We have the warranty only that all write requests completed prior to this flush are really flushed.

Quoted 19 lines
Quoted 11 lines
block/io.c +3 -0
+    }
+    bs->dirty = false;
+
      BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
      if (bs->drv->bdrv_co_flush_to_disk) {
          ret = bs->drv->bdrv_co_flush_to_disk(bs);
include/block/block_int.h +0 -0
index 0432ba5..59a7def 100644

include/block/block_int.h struct BlockDriverState { · line 438

Quoted 4 lines
include/block/block_int.h +1 -0
@@ -435,6 +435,7 @@ struct BlockDriverState {
      bool valid_key; /* if true, a valid encryption key has been set */
      bool sg;        /* if true, the device is a /dev/sg* */
      bool probed;    /* if true, format was probed rather than specified */
+    bool dirty;     /* if true, media is dirty and should be flushed */

How about renaming this to "need_flush"? The one "dirty" we had is set by bdrv_set_dirty, and cleared by bdrv_reset_dirty_bitmap. I'd avoid the confusion between the two concepts.

Quoted 1 line

Fam

can be

Quoted 6 lines
Quoted 6 lines
include/block/block_int.h +0 -0
      int copy_on_read; /* if nonzero, copy read backing sectors into image.
                           note this is a reference count */
Signature · 1 line
2.1.4
#4 lore raw
Quoted 81 lines · Denis V. Lunev
Quoted 72 lines · Fam Zheng
Quoted 71 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Some guests (win2008 server for example) do a lot of unnecessary flushing when underlying media has not changed. This adds additional overhead on host when calling fsync/fdatasync.

This change introduces a dirty flag in BlockDriverState which is set in bdrv_set_dirty and is checked in bdrv_co_flush. This allows us to avoid unnecessary flushing when storage is clean.

The problem with excessive flushing was found by a performance test which does parallel directory tree creation (from 2 processes). Results improved from 0.424 loops/sec to 0.432 loops/sec. Each loop creates 10^3 directories with 10 files in each.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  block.c                   |  1 +
  block/dirty-bitmap.c      |  3 +++
  block/io.c                | 19 +++++++++++++++++++
  include/block/block_int.h |  1 +
  4 files changed, 24 insertions(+)
block.c +1 -0
index 947df29..68ae3a0 100644
@@ -2581,6 +2581,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
          ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
          bdrv_dirty_bitmap_truncate(bs);
          bdrv_parent_cb_resize(bs);
+        bs->dirty = true; /* file node sync is needed after truncate */
      }
      return ret;
  }
block/dirty-bitmap.c +3 -0
index 4902ca5..54e0413 100644
@@ -370,6 +370,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
          }
          hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
      }
+
+    /* Set global block driver dirty flag even if bitmap is disabled */
+    bs->dirty = true;
  }
  /**
block/io.c +10 -0
index b9e53e3..152f5a9 100644
@@ -2247,6 +2247,25 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
          goto flush_parent;
      }
+    /* Check if storage is actually dirty before flushing to disk */
+    if (!bs->dirty) {
+        /* Flush requests are appended to tracked request list in order so that
+         * most recent request is at the head of the list. Following code uses
+         * this ordering to wait for the most recent flush request to complete
+         * to ensure that requests return in order */
+        BdrvTrackedRequest *prev_req;
+        QLIST_FOREACH(prev_req, &bs->tracked_requests, list) {
+            if (prev_req == &req || prev_req->type != BDRV_TRACKED_FLUSH) {
+                continue;
Quoted 8 lines

block/io.c int coroutine_fn bdrv_co_flush(BlockDriverState *bs) · line 2265

Quoted 6 lines
block/io.c +6 -0
+            }
+
+            qemu_co_queue_wait(&prev_req->wait_queue);
+            break;
+        }
+        goto flush_parent;

Should we check bs->dirty again after qemu_co_queue_wait()? I think another write request could sneak in while this coroutine yields.

Quoted 3 lines

no, we do not care. Any subsequent to FLUSH write does not guaranteed to be flushed. We have the warranty only that all write requests completed prior to this flush are really flushed.

I'm not worried about subsequent requests.

A prior request can be already in progress or be waiting when we check bs->dirty, though it would be false there, but it will become true soon -- bdrv_set_dirty is only called when a request is completing.

Fam

Quoted 33 lines
Quoted 22 lines
Quoted 14 lines
block/io.c +3 -0
+    }
+    bs->dirty = false;
+
      BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
      if (bs->drv->bdrv_co_flush_to_disk) {
          ret = bs->drv->bdrv_co_flush_to_disk(bs);
include/block/block_int.h +0 -0
index 0432ba5..59a7def 100644

include/block/block_int.h struct BlockDriverState { · line 438

Quoted 4 lines
include/block/block_int.h +1 -0
@@ -435,6 +435,7 @@ struct BlockDriverState {
      bool valid_key; /* if true, a valid encryption key has been set */
      bool sg;        /* if true, the device is a /dev/sg* */
      bool probed;    /* if true, format was probed rather than specified */
+    bool dirty;     /* if true, media is dirty and should be flushed */

How about renaming this to "need_flush"? The one "dirty" we had is set by bdrv_set_dirty, and cleared by bdrv_reset_dirty_bitmap. I'd avoid the confusion between the two concepts.

Quoted 1 line

Fam

can be

Quoted 5 lines
Quoted 5 lines
include/block/block_int.h +0 -0
      int copy_on_read; /* if nonzero, copy read backing sectors into image.
                           note this is a reference count */
Signature · 1 line
2.1.4
#5 lore raw
Quoted 85 lines · Fam Zheng
Quoted 81 lines · Denis V. Lunev
Quoted 72 lines · Fam Zheng
Quoted 71 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Some guests (win2008 server for example) do a lot of unnecessary flushing when underlying media has not changed. This adds additional overhead on host when calling fsync/fdatasync.

This change introduces a dirty flag in BlockDriverState which is set in bdrv_set_dirty and is checked in bdrv_co_flush. This allows us to avoid unnecessary flushing when storage is clean.

The problem with excessive flushing was found by a performance test which does parallel directory tree creation (from 2 processes). Results improved from 0.424 loops/sec to 0.432 loops/sec. Each loop creates 10^3 directories with 10 files in each.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
   block.c                   |  1 +
   block/dirty-bitmap.c      |  3 +++
   block/io.c                | 19 +++++++++++++++++++
   include/block/block_int.h |  1 +
   4 files changed, 24 insertions(+)
block.c +1 -0
index 947df29..68ae3a0 100644
@@ -2581,6 +2581,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
           ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
           bdrv_dirty_bitmap_truncate(bs);
           bdrv_parent_cb_resize(bs);
+        bs->dirty = true; /* file node sync is needed after truncate */
       }
       return ret;
   }
block/dirty-bitmap.c +3 -0
index 4902ca5..54e0413 100644
@@ -370,6 +370,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
           }
           hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
       }
+
+    /* Set global block driver dirty flag even if bitmap is disabled */
+    bs->dirty = true;
   }
   /**
block/io.c +10 -0
index b9e53e3..152f5a9 100644
@@ -2247,6 +2247,25 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
           goto flush_parent;
       }
+    /* Check if storage is actually dirty before flushing to disk */
+    if (!bs->dirty) {
+        /* Flush requests are appended to tracked request list in order so that
+         * most recent request is at the head of the list. Following code uses
+         * this ordering to wait for the most recent flush request to complete
+         * to ensure that requests return in order */
+        BdrvTrackedRequest *prev_req;
+        QLIST_FOREACH(prev_req, &bs->tracked_requests, list) {
+            if (prev_req == &req || prev_req->type != BDRV_TRACKED_FLUSH) {
+                continue;
Quoted 8 lines

block/io.c int coroutine_fn bdrv_co_flush(BlockDriverState *bs) · line 2265

Quoted 6 lines
block/io.c +6 -0
+            }
+
+            qemu_co_queue_wait(&prev_req->wait_queue);
+            break;
+        }
+        goto flush_parent;

Should we check bs->dirty again after qemu_co_queue_wait()? I think another write request could sneak in while this coroutine yields.

Quoted 3 lines

no, we do not care. Any subsequent to FLUSH write does not guaranteed to be flushed. We have the warranty only that all write requests completed prior to this flush are really flushed.

Quoted 7 lines

I'm not worried about subsequent requests.

A prior request can be already in progress or be waiting when we check bs->dirty, though it would be false there, but it will become true soon -- bdrv_set_dirty is only called when a request is completing.

Fam

I have written specifically about this situation. FLUSH in the controller does not guarantee that requests which are in the progress at the moment when the flush is initiated will be flushed.

It guarantees that requests, which are completed, i.e. which status 'COMPLETED' was returned to the guest, will be flushed.

Den

#6 lore raw
In reply to Fam Zheng
Quoted 86 lines · Fam Zheng
Quoted 81 lines · Denis V. Lunev
Quoted 72 lines · Fam Zheng
Quoted 71 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Some guests (win2008 server for example) do a lot of unnecessary flushing when underlying media has not changed. This adds additional overhead on host when calling fsync/fdatasync.

This change introduces a dirty flag in BlockDriverState which is set in bdrv_set_dirty and is checked in bdrv_co_flush. This allows us to avoid unnecessary flushing when storage is clean.

The problem with excessive flushing was found by a performance test which does parallel directory tree creation (from 2 processes). Results improved from 0.424 loops/sec to 0.432 loops/sec. Each loop creates 10^3 directories with 10 files in each.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  block.c                   |  1 +
  block/dirty-bitmap.c      |  3 +++
  block/io.c                | 19 +++++++++++++++++++
  include/block/block_int.h |  1 +
  4 files changed, 24 insertions(+)
block.c +1 -0
index 947df29..68ae3a0 100644
@@ -2581,6 +2581,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
          ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
          bdrv_dirty_bitmap_truncate(bs);
          bdrv_parent_cb_resize(bs);
+        bs->dirty = true; /* file node sync is needed after truncate */
      }
      return ret;
  }
block/dirty-bitmap.c +3 -0
index 4902ca5..54e0413 100644
@@ -370,6 +370,9 @@ void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
          }
          hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
      }
+
+    /* Set global block driver dirty flag even if bitmap is disabled */
+    bs->dirty = true;
  }
  /**
block/io.c +10 -0
index b9e53e3..152f5a9 100644
@@ -2247,6 +2247,25 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
          goto flush_parent;
      }
+    /* Check if storage is actually dirty before flushing to disk */
+    if (!bs->dirty) {
+        /* Flush requests are appended to tracked request list in order so that
+         * most recent request is at the head of the list. Following code uses
+         * this ordering to wait for the most recent flush request to complete
+         * to ensure that requests return in order */
+        BdrvTrackedRequest *prev_req;
+        QLIST_FOREACH(prev_req, &bs->tracked_requests, list) {
+            if (prev_req == &req || prev_req->type != BDRV_TRACKED_FLUSH) {
+                continue;
Quoted 8 lines

block/io.c int coroutine_fn bdrv_co_flush(BlockDriverState *bs) · line 2265

Quoted 6 lines
block/io.c +6 -0
+            }
+
+            qemu_co_queue_wait(&prev_req->wait_queue);
+            break;
+        }
+        goto flush_parent;

Should we check bs->dirty again after qemu_co_queue_wait()? I think another write request could sneak in while this coroutine yields.

Quoted 3 lines

no, we do not care. Any subsequent to FLUSH write does not guaranteed to be flushed. We have the warranty only that all write requests completed prior to this flush are really flushed.

Quoted 5 lines

I'm not worried about subsequent requests.

A prior request can be already in progress or be waiting when we check bs->dirty, though it would be false there, but it will become true soon -- bdrv_set_dirty is only called when a request is completing.

Flush only guarantees that already completed writes are persistent. It is not a barrier operation. It does not wait for in-flight writes and makes no guarantee regarding them.

Stefan

#7 lore raw

PATCH v4 2/3 ide: ignore retry_unit check for non-retry operation

In reply to Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

The following sequence of tests discovered a problem in IDE emulation:
1. Send DMA write to IDE device 0
2. Send CMD_FLUSH_CACHE to same IDE device which will be failed by block layer using blkdebug script in tests/ide-test:test_retry_flush

When doing DMA request ide/core.c will set s->retry_unit to s->unit in ide_start_dma. When dma completes ide_set_inactive sets retry_unit to -1. After that ide_flush_cache runs and fails thanks to blkdebug. ide_flush_cb calls ide_handle_rw_error which asserts that s->retry_unit == s->unit. But s->retry_unit is still -1 after previous DMA completion and flush does not use anything related to retry.

This patch restricts retry unit assertion only to ops that actually use retry logic.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
 hw/ide/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/ide/core.c +2 -1
index 029f6b9..17f884b 100644
@@ -779,7 +779,8 @@ int ide_handle_rw_error(IDEState *s, int error, int op)
     BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
 
     if (action == BLOCK_ERROR_ACTION_STOP) {
-        assert(s->bus->retry_unit == s->unit);
+        assert(!(IS_IDE_RETRY_DMA(op) || IS_IDE_RETRY_PIO(op))
+                || s->bus->retry_unit == s->unit);
         s->bus->error_status = op;
     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
         block_acct_failed(blk_get_stats(s->blk), &s->acct);
Signature · 1 line
2.1.4
#8 lore raw

PATCH v4 3/3 tests: in IDE and AHCI tests perform DMA write before flushing

In reply to Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Due to changes in flush behaviour clean disks stopped generating flush_to_disk events and IDE and AHCI tests that test flush commands started to fail.

This change adds additional DMA writes to affected tests before sending flush commands so that bdrv_flush actually generates flush_to_disk event.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
 tests/ahci-test.c | 34 ++++++++++++++++++++++++++++++++--
 tests/ide-test.c  | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)
tests/ahci-test.c +32 -2
index 57dc44c..d707714 100644
@@ -1063,11 +1063,34 @@ static void test_dma_fragmented(void)
     g_free(tx);
 }
 
+/*
+ * Write sector 0 with random data to make AHCI storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(AHCIQState* ahci, uint8_t port)
+{
+    uint64_t ptr;
+    unsigned bufsize = 512;
+
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);
+
+    ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 0);
+    ahci_free(ahci, ptr);
+}
+
 static void test_flush(void)
 {
     AHCIQState *ahci;
+    uint8_t port;
 
     ahci = ahci_boot_and_enable(NULL);
+
+    port = ahci_port_select(ahci);
+    ahci_port_clear(ahci, port);
+
+    make_dirty(ahci, port);
+
     ahci_test_flush(ahci);
     ahci_shutdown(ahci);
 }
@@ -1087,10 +1110,13 @@ static void test_flush_retry(void)
                                 debug_path,
                                 tmp_path, imgfmt);
 
-    /* Issue Flush Command and wait for error */
     port = ahci_port_select(ahci);
     ahci_port_clear(ahci, port);
 
+    /* Issue write so that flush actually goes to disk */
+    make_dirty(ahci, port);
+
+    /* Issue Flush Command and wait for error */
     cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
     ahci_guest_io_resume(ahci, cmd);
 
@@ -1343,9 +1369,13 @@ static void test_flush_migrate(void)
 
     set_context(src->parent);
 
-    /* Issue Flush Command */
     px = ahci_port_select(src);
     ahci_port_clear(src, px);
+
+    /* Dirty device so that flush reaches disk */
+    make_dirty(src, px);
+
+    /* Issue Flush Command */
     cmd = ahci_command_create(CMD_FLUSH_CACHE);
     ahci_command_commit(src, cmd, px);
     ahci_command_issue_async(src, cmd);
tests/ide-test.c +43 -0

61 lines not on this page · raw message

Signature · 1 line
2.1.4
Reviewed-by on v4 3/3
#9 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] tests: in IDE and AHCI tests perform DMA write before flushing

Quoted 149 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Due to changes in flush behaviour clean disks stopped generating flush_to_disk events and IDE and AHCI tests that test flush commands started to fail.

This change adds additional DMA writes to affected tests before sending flush commands so that bdrv_flush actually generates flush_to_disk event.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
 tests/ahci-test.c | 34 ++++++++++++++++++++++++++++++++--
 tests/ide-test.c  | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)
tests/ahci-test.c +32 -2
index 57dc44c..d707714 100644
@@ -1063,11 +1063,34 @@ static void test_dma_fragmented(void)
     g_free(tx);
 }
 
+/*
+ * Write sector 0 with random data to make AHCI storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(AHCIQState* ahci, uint8_t port)
+{
+    uint64_t ptr;
+    unsigned bufsize = 512;
+
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);
+
+    ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 0);
+    ahci_free(ahci, ptr);
+}
+
 static void test_flush(void)
 {
     AHCIQState *ahci;
+    uint8_t port;
 
     ahci = ahci_boot_and_enable(NULL);
+
+    port = ahci_port_select(ahci);
+    ahci_port_clear(ahci, port);
+
+    make_dirty(ahci, port);
+
     ahci_test_flush(ahci);
     ahci_shutdown(ahci);
 }
@@ -1087,10 +1110,13 @@ static void test_flush_retry(void)
                                 debug_path,
                                 tmp_path, imgfmt);
 
-    /* Issue Flush Command and wait for error */
     port = ahci_port_select(ahci);
     ahci_port_clear(ahci, port);
 
+    /* Issue write so that flush actually goes to disk */
+    make_dirty(ahci, port);
+
+    /* Issue Flush Command and wait for error */
     cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
     ahci_guest_io_resume(ahci, cmd);
 
@@ -1343,9 +1369,13 @@ static void test_flush_migrate(void)
 
     set_context(src->parent);
 
-    /* Issue Flush Command */
     px = ahci_port_select(src);
     ahci_port_clear(src, px);
+
+    /* Dirty device so that flush reaches disk */
+    make_dirty(src, px);
+
+    /* Issue Flush Command */
     cmd = ahci_command_create(CMD_FLUSH_CACHE);
     ahci_command_commit(src, cmd, px);
     ahci_command_issue_async(src, cmd);
tests/ide-test.c +38 -0
index fed1b2e..8466d0f 100644
@@ -499,6 +499,39 @@ static void test_identify(void)
     ide_test_quit();
 }
 
+/*
+ * Write sector 0 with random data to make IDE storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(uint8_t device)
+{
+    uint8_t status;
+    size_t len = 512;
+    uintptr_t guest_buf;
+    void* buf;
+
+    guest_buf = guest_alloc(guest_malloc, len);
+    buf = g_malloc(len);
+    g_assert(guest_buf);
+    g_assert(buf);
+
+    memwrite(guest_buf, buf, len);
+
+    PrdtEntry prdt[] = {
+        {
+            .addr = cpu_to_le32(guest_buf),
+            .size = cpu_to_le32(len | PRDT_EOT),
+        },
+    };
+
+    status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
+                              ARRAY_SIZE(prdt), NULL);
+    g_assert_cmphex(status, ==, BM_STS_INTR);
+    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
+
+    g_free(buf);
+}
+
 static void test_flush(void)
 {
     uint8_t data;
@@ -507,6 +540,11 @@ static void test_flush(void)
         "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
         tmp_path);
 
+    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
     /* Delay the completion of the flush request until we explicitly do it */
     g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
 
@@ -549,6 +587,11 @@ static void test_retry_flush(const char *machine)
         "rerror=stop,werror=stop",

tests/ide-test.c static void test_retry_flush(const char *machine) · line 597

Quoted 10 lines
tests/ide-test.c +5 -0
         debug_path, tmp_path);
 
+    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
     /* FLUSH CACHE command on device 0*/
     outb(IDE_BASE + reg_device, 0);
     outb(IDE_BASE + reg_command, CMD_FLUSH_CACHE);

However, the patch-set needs to be re-ordered as 3-1-2. Intermediate patch commits cannot have failing tests.

#10 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] tests: in IDE and AHCI tests perform DMA write before flushing

Quoted 163 lines · John Snow
Quoted 149 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Due to changes in flush behaviour clean disks stopped generating flush_to_disk events and IDE and AHCI tests that test flush commands started to fail.

This change adds additional DMA writes to affected tests before sending flush commands so that bdrv_flush actually generates flush_to_disk event.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  tests/ahci-test.c | 34 ++++++++++++++++++++++++++++++++--
  tests/ide-test.c  | 43 +++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 75 insertions(+), 2 deletions(-)
tests/ahci-test.c +32 -2
index 57dc44c..d707714 100644
@@ -1063,11 +1063,34 @@ static void test_dma_fragmented(void)
      g_free(tx);
  }
 
+/*
+ * Write sector 0 with random data to make AHCI storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(AHCIQState* ahci, uint8_t port)
+{
+    uint64_t ptr;
+    unsigned bufsize = 512;
+
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);
+
+    ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 0);
+    ahci_free(ahci, ptr);
+}
+
  static void test_flush(void)
  {
      AHCIQState *ahci;
+    uint8_t port;
 
      ahci = ahci_boot_and_enable(NULL);
+
+    port = ahci_port_select(ahci);
+    ahci_port_clear(ahci, port);
+
+    make_dirty(ahci, port);
+
      ahci_test_flush(ahci);
      ahci_shutdown(ahci);
  }
@@ -1087,10 +1110,13 @@ static void test_flush_retry(void)
                                  debug_path,
                                  tmp_path, imgfmt);
 
-    /* Issue Flush Command and wait for error */
      port = ahci_port_select(ahci);
      ahci_port_clear(ahci, port);
 
+    /* Issue write so that flush actually goes to disk */
+    make_dirty(ahci, port);
+
+    /* Issue Flush Command and wait for error */
      cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
      ahci_guest_io_resume(ahci, cmd);
 
@@ -1343,9 +1369,13 @@ static void test_flush_migrate(void)
 
      set_context(src->parent);
 
-    /* Issue Flush Command */
      px = ahci_port_select(src);
      ahci_port_clear(src, px);
+
+    /* Dirty device so that flush reaches disk */
+    make_dirty(src, px);
+
+    /* Issue Flush Command */
      cmd = ahci_command_create(CMD_FLUSH_CACHE);
      ahci_command_commit(src, cmd, px);
      ahci_command_issue_async(src, cmd);
tests/ide-test.c +38 -0
index fed1b2e..8466d0f 100644
@@ -499,6 +499,39 @@ static void test_identify(void)
      ide_test_quit();
  }
 
+/*
+ * Write sector 0 with random data to make IDE storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(uint8_t device)
+{
+    uint8_t status;
+    size_t len = 512;
+    uintptr_t guest_buf;
+    void* buf;
+
+    guest_buf = guest_alloc(guest_malloc, len);
+    buf = g_malloc(len);
+    g_assert(guest_buf);
+    g_assert(buf);
+
+    memwrite(guest_buf, buf, len);
+
+    PrdtEntry prdt[] = {
+        {
+            .addr = cpu_to_le32(guest_buf),
+            .size = cpu_to_le32(len | PRDT_EOT),
+        },
+    };
+
+    status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
+                              ARRAY_SIZE(prdt), NULL);
+    g_assert_cmphex(status, ==, BM_STS_INTR);
+    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
+
+    g_free(buf);
+}
+
  static void test_flush(void)
  {
      uint8_t data;
@@ -507,6 +540,11 @@ static void test_flush(void)
          "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
          tmp_path);
 
+    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* Delay the completion of the flush request until we explicitly do it */
      g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
 
@@ -549,6 +587,11 @@ static void test_retry_flush(const char *machine)
          "rerror=stop,werror=stop",

tests/ide-test.c static void test_retry_flush(const char *machine) · line 597

Quoted 10 lines
tests/ide-test.c +5 -0
          debug_path, tmp_path);
 
+    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* FLUSH CACHE command on device 0*/
      outb(IDE_BASE + reg_device, 0);
      outb(IDE_BASE + reg_command, CMD_FLUSH_CACHE);
Quoted 2 lines

However, the patch-set needs to be re-ordered as 3-1-2. Intermediate patch commits cannot have failing tests.

ok

#11 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] tests: in IDE and AHCI tests perform DMA write before flushing

In reply to John Snow
Quoted 163 lines · John Snow
Quoted 149 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Due to changes in flush behaviour clean disks stopped generating flush_to_disk events and IDE and AHCI tests that test flush commands started to fail.

This change adds additional DMA writes to affected tests before sending flush commands so that bdrv_flush actually generates flush_to_disk event.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  tests/ahci-test.c | 34 ++++++++++++++++++++++++++++++++--
  tests/ide-test.c  | 43 +++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 75 insertions(+), 2 deletions(-)
tests/ahci-test.c +32 -2
index 57dc44c..d707714 100644
@@ -1063,11 +1063,34 @@ static void test_dma_fragmented(void)
      g_free(tx);
  }
 
+/*
+ * Write sector 0 with random data to make AHCI storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(AHCIQState* ahci, uint8_t port)
+{
+    uint64_t ptr;
+    unsigned bufsize = 512;
+
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);
+
+    ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 0);
+    ahci_free(ahci, ptr);
+}
+
  static void test_flush(void)
  {
      AHCIQState *ahci;
+    uint8_t port;
 
      ahci = ahci_boot_and_enable(NULL);
+
+    port = ahci_port_select(ahci);
+    ahci_port_clear(ahci, port);
+
+    make_dirty(ahci, port);
+
      ahci_test_flush(ahci);
      ahci_shutdown(ahci);
  }
@@ -1087,10 +1110,13 @@ static void test_flush_retry(void)
                                  debug_path,
                                  tmp_path, imgfmt);
 
-    /* Issue Flush Command and wait for error */
      port = ahci_port_select(ahci);
      ahci_port_clear(ahci, port);
 
+    /* Issue write so that flush actually goes to disk */
+    make_dirty(ahci, port);
+
+    /* Issue Flush Command and wait for error */
      cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
      ahci_guest_io_resume(ahci, cmd);
 
@@ -1343,9 +1369,13 @@ static void test_flush_migrate(void)
 
      set_context(src->parent);
 
-    /* Issue Flush Command */
      px = ahci_port_select(src);
      ahci_port_clear(src, px);
+
+    /* Dirty device so that flush reaches disk */
+    make_dirty(src, px);
+
+    /* Issue Flush Command */
      cmd = ahci_command_create(CMD_FLUSH_CACHE);
      ahci_command_commit(src, cmd, px);
      ahci_command_issue_async(src, cmd);
tests/ide-test.c +38 -0
index fed1b2e..8466d0f 100644
@@ -499,6 +499,39 @@ static void test_identify(void)
      ide_test_quit();
  }
 
+/*
+ * Write sector 0 with random data to make IDE storage dirty
+ * Needed for flush tests so that flushes actually go though the block layer
+ */
+static void make_dirty(uint8_t device)
+{
+    uint8_t status;
+    size_t len = 512;
+    uintptr_t guest_buf;
+    void* buf;
+
+    guest_buf = guest_alloc(guest_malloc, len);
+    buf = g_malloc(len);
+    g_assert(guest_buf);
+    g_assert(buf);
+
+    memwrite(guest_buf, buf, len);
+
+    PrdtEntry prdt[] = {
+        {
+            .addr = cpu_to_le32(guest_buf),
+            .size = cpu_to_le32(len | PRDT_EOT),
+        },
+    };
+
+    status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
+                              ARRAY_SIZE(prdt), NULL);
+    g_assert_cmphex(status, ==, BM_STS_INTR);
+    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
+
+    g_free(buf);
+}
+
  static void test_flush(void)
  {
      uint8_t data;
@@ -507,6 +540,11 @@ static void test_flush(void)
          "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
          tmp_path);
 
+    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* Delay the completion of the flush request until we explicitly do it */
      g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
 
@@ -549,6 +587,11 @@ static void test_retry_flush(const char *machine)
          "rerror=stop,werror=stop",

tests/ide-test.c static void test_retry_flush(const char *machine) · line 597

Quoted 10 lines
tests/ide-test.c +5 -0
          debug_path, tmp_path);
 
+    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* FLUSH CACHE command on device 0*/
      outb(IDE_BASE + reg_device, 0);
      outb(IDE_BASE + reg_command, CMD_FLUSH_CACHE);
Quoted 2 lines

However, the patch-set needs to be re-ordered as 3-1-2. Intermediate patch commits cannot have failing tests.

Sure, although 2-3-1 would probably be better, since 2 fixes an assert in IDE that is triggered by new tests in 3?

#12 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] tests: in IDE and AHCI tests perform DMA write before flushing

Quoted 164 lines · Evgeny Yakovlev
Quoted 35 lines · John Snow
Quoted 33 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Due to changes in flush behaviour clean disks stopped generating flush_to_disk events and IDE and AHCI tests that test flush commands started to fail.

This change adds additional DMA writes to affected tests before sending flush commands so that bdrv_flush actually generates flush_to_disk event.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  tests/ahci-test.c | 34 ++++++++++++++++++++++++++++++++--
  tests/ide-test.c  | 43 +++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 75 insertions(+), 2 deletions(-)
tests/ahci-test.c +2 -0
index 57dc44c..d707714 100644
@@ -1063,11 +1063,34 @@ static void test_dma_fragmented(void)
      g_free(tx);
  }
  +/*
+ * Write sector 0 with random data to make AHCI storage dirty
+ * Needed for flush tests so that flushes actually go though the

block layer

Quoted 8 lines

tests/ahci-test.c static void test_dma_fragmented(void) · line 1076

Quoted 8 lines

+ */

tests/ahci-test.c +7 -0
+static void make_dirty(AHCIQState* ahci, uint8_t port)
+{
+    uint64_t ptr;
+    unsigned bufsize = 512;
+
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);

With no link to allow us to view the keynote from another room or our desks, most of us had not choice but to abandon our attempt to actually listen Jim talk about the future of Red Hat.

Quoted 55 lines
Quoted 55 lines
tests/ahci-test.c +15 -0
+
+    ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 0);
+    ahci_free(ahci, ptr);
+}
+
  static void test_flush(void)
  {
      AHCIQState *ahci;
+    uint8_t port;
        ahci = ahci_boot_and_enable(NULL);
+
+    port = ahci_port_select(ahci);
+    ahci_port_clear(ahci, port);
+
+    make_dirty(ahci, port);
+
      ahci_test_flush(ahci);
      ahci_shutdown(ahci);
  }
@@ -1087,10 +1110,13 @@ static void test_flush_retry(void)
                                  debug_path,
                                  tmp_path, imgfmt);
  -    /* Issue Flush Command and wait for error */
      port = ahci_port_select(ahci);
      ahci_port_clear(ahci, port);
  +    /* Issue write so that flush actually goes to disk */
+    make_dirty(ahci, port);
+
+    /* Issue Flush Command and wait for error */
      cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
      ahci_guest_io_resume(ahci, cmd);
  @@ -1343,9 +1369,13 @@ static void test_flush_migrate(void)
        set_context(src->parent);
  -    /* Issue Flush Command */
      px = ahci_port_select(src);
      ahci_port_clear(src, px);
tests/ahci-test.c +5 -0
+
+    /* Dirty device so that flush reaches disk */
+    make_dirty(src, px);
+
+    /* Issue Flush Command */
      cmd = ahci_command_create(CMD_FLUSH_CACHE);
      ahci_command_commit(src, cmd, px);
      ahci_command_issue_async(src, cmd);
tests/ide-test.c +2 -0
index fed1b2e..8466d0f 100644
@@ -499,6 +499,39 @@ static void test_identify(void)
      ide_test_quit();
  }
  +/*
+ * Write sector 0 with random data to make IDE storage dirty
+ * Needed for flush tests so that flushes actually go though the

block layer

Quoted 8 lines

tests/ide-test.c static void test_identify(void) · line 512

Quoted 8 lines

+ */

tests/ide-test.c +7 -0
+static void make_dirty(uint8_t device)
+{
+    uint8_t status;
+    size_t len = 512;
+    uintptr_t guest_buf;
+    void* buf;
+

With no link to allow us to view the keynote from another room or our desks, most of us had not choice but to abandon our attempt to actually listen Jim talk about the future of Red Hat.

Quoted 50 lines
Quoted 41 lines
tests/ide-test.c +26 -0
+    guest_buf = guest_alloc(guest_malloc, len);
+    buf = g_malloc(len);
+    g_assert(guest_buf);
+    g_assert(buf);
+
+    memwrite(guest_buf, buf, len);
+
+    PrdtEntry prdt[] = {
+        {
+            .addr = cpu_to_le32(guest_buf),
+            .size = cpu_to_le32(len | PRDT_EOT),
+        },
+    };
+
+    status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
+                              ARRAY_SIZE(prdt), NULL);
+    g_assert_cmphex(status, ==, BM_STS_INTR);
+    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
+
+    g_free(buf);
+}
+
  static void test_flush(void)
  {
      uint8_t data;
@@ -507,6 +540,11 @@ static void test_flush(void)
          "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
          tmp_path);
  +    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* Delay the completion of the flush request until we

explicitly do it */

      g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
  @@ -549,6 +587,11 @@ static void test_retry_flush(const char *machine)
          "rerror=stop,werror=stop",
          debug_path, tmp_path);

+ qtest_irq_intercept_in(global_qtest, "ioapic");

tests/ide-test.c static void test_retry_flush(const char *machine) · line 597

Quoted 7 lines
tests/ide-test.c +4 -0
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* FLUSH CACHE command on device 0*/
      outb(IDE_BASE + reg_device, 0);
      outb(IDE_BASE + reg_command, CMD_FLUSH_CACHE);
Quoted 6 lines
Quoted 2 lines

However, the patch-set needs to be re-ordered as 3-1-2. Intermediate patch commits cannot have failing tests.

Sure, although 2-3-1 would probably be better, since 2 fixes an assert in IDE that is triggered by new tests in 3?

:)

Yes, thanks!

--js

#13 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] tests: in IDE and AHCI tests perform DMA write before flushing

Quoted 43 lines · John Snow
Quoted 40 lines · Evgeny Yakovlev
Quoted 38 lines · John Snow
Quoted 36 lines · Denis V. Lunev

From: Evgeny Yakovlev <[email protected]>

Due to changes in flush behaviour clean disks stopped generating flush_to_disk events and IDE and AHCI tests that test flush commands started to fail.

This change adds additional DMA writes to affected tests before sending flush commands so that bdrv_flush actually generates flush_to_disk event.

CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC John Snow <[email protected]>
---
  tests/ahci-test.c | 34 ++++++++++++++++++++++++++++++++--
  tests/ide-test.c  | 43 +++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 75 insertions(+), 2 deletions(-)
tests/ahci-test.c +2 -0
index 57dc44c..d707714 100644
@@ -1063,11 +1063,34 @@ static void test_dma_fragmented(void)
      g_free(tx);
  }
  +/*
+ * Write sector 0 with random data to make AHCI storage dirty
+ * Needed for flush tests so that flushes actually go though the

block layer
+ */

tests/ahci-test.c +2 -0
+static void make_dirty(AHCIQState* ahci, uint8_t port)
+{
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

tests/ahci-test.c static void test_dma_fragmented(void) · line 1076

Quoted 5 lines
tests/ahci-test.c +5 -0
+    uint64_t ptr;
+    unsigned bufsize = 512;
+
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);

With no link to allow us to view the keynote from another room or our desks, most of us had not choice but to abandon our attempt to actually listen Jim talk about the future of Red Hat.

Apologies, random clipboard detritus escaped into my emails again. (Red Hat summit is ongoing right now and some people had concerns about access to the live feeds. Apologies.)

Please pardon the noise.

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
tests/ahci-test.c +15 -0
+
+    ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 0);
+    ahci_free(ahci, ptr);
+}
+
  static void test_flush(void)
  {
      AHCIQState *ahci;
+    uint8_t port;
        ahci = ahci_boot_and_enable(NULL);
+
+    port = ahci_port_select(ahci);
+    ahci_port_clear(ahci, port);
+
+    make_dirty(ahci, port);
+
      ahci_test_flush(ahci);
      ahci_shutdown(ahci);
  }
@@ -1087,10 +1110,13 @@ static void test_flush_retry(void)
                                  debug_path,
                                  tmp_path, imgfmt);
  -    /* Issue Flush Command and wait for error */
      port = ahci_port_select(ahci);
      ahci_port_clear(ahci, port);
  +    /* Issue write so that flush actually goes to disk */
+    make_dirty(ahci, port);
+
+    /* Issue Flush Command and wait for error */
      cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
      ahci_guest_io_resume(ahci, cmd);
  @@ -1343,9 +1369,13 @@ static void test_flush_migrate(void)
        set_context(src->parent);
  -    /* Issue Flush Command */
      px = ahci_port_select(src);
      ahci_port_clear(src, px);
tests/ahci-test.c +5 -0
+
+    /* Dirty device so that flush reaches disk */
+    make_dirty(src, px);
+
+    /* Issue Flush Command */
      cmd = ahci_command_create(CMD_FLUSH_CACHE);
      ahci_command_commit(src, cmd, px);
      ahci_command_issue_async(src, cmd);
tests/ide-test.c +2 -0
index fed1b2e..8466d0f 100644
@@ -499,6 +499,39 @@ static void test_identify(void)
      ide_test_quit();
  }
  +/*
+ * Write sector 0 with random data to make IDE storage dirty
+ * Needed for flush tests so that flushes actually go though the

block layer
+ */

tests/ide-test.c +2 -0
+static void make_dirty(uint8_t device)
+{
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

tests/ide-test.c static void test_identify(void) · line 512

Quoted 5 lines
tests/ide-test.c +5 -0
+    uint8_t status;
+    size_t len = 512;
+    uintptr_t guest_buf;
+    void* buf;
+

With no link to allow us to view the keynote from another room or our desks, most of us had not choice but to abandon our attempt to actually listen Jim talk about the future of Red Hat.

My clipboard must have been really angry.

Quoted 63 lines
Quoted 50 lines
Quoted 50 lines
Quoted 41 lines
tests/ide-test.c +26 -0
+    guest_buf = guest_alloc(guest_malloc, len);
+    buf = g_malloc(len);
+    g_assert(guest_buf);
+    g_assert(buf);
+
+    memwrite(guest_buf, buf, len);
+
+    PrdtEntry prdt[] = {
+        {
+            .addr = cpu_to_le32(guest_buf),
+            .size = cpu_to_le32(len | PRDT_EOT),
+        },
+    };
+
+    status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
+                              ARRAY_SIZE(prdt), NULL);
+    g_assert_cmphex(status, ==, BM_STS_INTR);
+    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
+
+    g_free(buf);
+}
+
  static void test_flush(void)
  {
      uint8_t data;
@@ -507,6 +540,11 @@ static void test_flush(void)
          "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
          tmp_path);
  +    qtest_irq_intercept_in(global_qtest, "ioapic");
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* Delay the completion of the flush request until we

explicitly do it */

      g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
  @@ -549,6 +587,11 @@ static void test_retry_flush(const char *machine)
          "rerror=stop,werror=stop",
          debug_path, tmp_path);

+ qtest_irq_intercept_in(global_qtest, "ioapic");

tests/ide-test.c static void test_retry_flush(const char *machine) · line 597

Quoted 7 lines
tests/ide-test.c +4 -0
+
+    /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
+    make_dirty(0);
+
      /* FLUSH CACHE command on device 0*/
      outb(IDE_BASE + reg_device, 0);
      outb(IDE_BASE + reg_command, CMD_FLUSH_CACHE);
Quoted 6 lines
Quoted 2 lines

However, the patch-set needs to be re-ordered as 3-1-2. Intermediate patch commits cannot have failing tests.

Sure, although 2-3-1 would probably be better, since 2 fixes an assert in IDE that is triggered by new tests in 3?

:)

Yes, thanks!

--js