Skip to contentSkip to conversation

block: ignore flush requests when storage is clean

PATCH Superseded Denis V. Lunev 5 participants 13 messages Jul 4, 2016 – Jul 11, 2016 Block layer

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover block: ignore flush requests when storage is clean

Changes from v4:
- Moved to write generation scheme instead of dirty flag
- Added retry setup to IDE PIO and FLUSH requests

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 (4):
ide: refactor retry_unit set and clear into separate function
ide: set retry_unit for PIO and FLUSH requests
tests: in IDE and AHCI tests perform DMA write before flushing
block: ignore flush requests when storage is clean

 block.c                   |  3 +++
 block/io.c                | 18 ++++++++++++++++++
 hw/ide/core.c             | 24 ++++++++++++++++++------
 include/block/block_int.h |  5 +++++
 tests/ahci-test.c         | 34 ++++++++++++++++++++++++++++++++--
 tests/ide-test.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 119 insertions(+), 8 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH v5 1/4 ide: refactor retry_unit set and clear into separate function

From: Evgeny Yakovlev <[email protected]>

Code to set and clear state associated with retry in moved into ide_set_retry and ide_clear_retry to make adding retry setups easier.

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 | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
hw/ide/core.c +16 -6
index 029f6b9..b72346e 100644
@@ -466,6 +466,20 @@ void ide_abort_command(IDEState *s)
     s->error = ABRT_ERR;
 }
 
+static void ide_set_retry(IDEState *s)
+{
+    s->bus->retry_unit = s->unit;
+    s->bus->retry_sector_num = ide_get_sector(s);
+    s->bus->retry_nsector = s->nsector;
+}
+
+static void ide_clear_retry(IDEState *s)
+{
+    s->bus->retry_unit = -1;
+    s->bus->retry_sector_num = 0;
+    s->bus->retry_nsector = 0;
+}
+
 /* prepare data transfer and tell what to do after */
 void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
                         EndTransferFunc *end_transfer_func)
@@ -756,9 +770,7 @@ void dma_buf_commit(IDEState *s, uint32_t tx_bytes)
 void ide_set_inactive(IDEState *s, bool more)
 {
     s->bus->dma->aiocb = NULL;
-    s->bus->retry_unit = -1;
-    s->bus->retry_sector_num = 0;
-    s->bus->retry_nsector = 0;
+    ide_clear_retry(s);
     if (s->bus->dma->ops->set_inactive) {
         s->bus->dma->ops->set_inactive(s->bus->dma, more);
     }
@@ -914,9 +926,7 @@ static void ide_sector_start_dma(IDEState *s, enum ide_dma_cmd dma_cmd)
 void ide_start_dma(IDEState *s, BlockCompletionFunc *cb)
 {
     s->io_buffer_index = 0;
-    s->bus->retry_unit = s->unit;
-    s->bus->retry_sector_num = ide_get_sector(s);
-    s->bus->retry_nsector = s->nsector;
+    ide_set_retry(s);
     if (s->bus->dma->ops->start_dma) {
         s->bus->dma->ops->start_dma(s->bus->dma, s, cb);
     }
Signature · 1 line
2.1.4
#2 lore raw

PATCH v5 2/4 ide: set retry_unit for PIO and FLUSH requests

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 | 2 ++
 1 file changed, 2 insertions(+)
hw/ide/core.c +2 -0
index b72346e..14f03d2 100644
@@ -487,6 +487,7 @@ void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
     s->end_transfer_func = end_transfer_func;
     s->data_ptr = buf;
     s->data_end = buf + size;
+    ide_set_retry(s);
     if (!(s->status & ERR_STAT)) {
         s->status |= DRQ_STAT;
     }
@@ -1056,6 +1057,7 @@ static void ide_flush_cache(IDEState *s)
     }
 
     s->status |= BUSY_STAT;
+    ide_set_retry(s);
     block_acct_start(blk_get_stats(s->blk), &s->acct, 0, BLOCK_ACCT_FLUSH);
     s->pio_aiocb = blk_aio_flush(s->blk, ide_flush_cb, s);
 }
Signature · 1 line
2.1.4
#3 lore raw

PATCH v5 3/4 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
#4 lore raw

PATCH v5 4/4 block: ignore flush requests when storage is clean

In reply to 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 write generation scheme in BlockDriverState. Current write generation is checked against last flushed generation to avoid unnessesary flushes.

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                   |  3 +++
 block/io.c                | 18 ++++++++++++++++++
 include/block/block_int.h |  5 +++++
 3 files changed, 26 insertions(+)
block.c +3 -0
index f4648e9..366fad6 100644
@@ -234,6 +234,8 @@ BlockDriverState *bdrv_new(void)
     bs->refcnt = 1;
     bs->aio_context = qemu_get_aio_context();
 
+    qemu_co_queue_init(&bs->flush_queue);
+
     QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
 
     return bs;
@@ -2582,6 +2584,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->write_gen;
     }
     return ret;
 }
block/io.c +18 -0
index 7cf3645..a5451b6 100644
@@ -1294,6 +1294,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
     }
     bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
 
+    ++bs->write_gen;
     bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
 
     if (bs->wr_highest_offset < offset + bytes) {
@@ -2211,6 +2212,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 {
     int ret;
     BdrvTrackedRequest req;
+    int current_gen = bs->write_gen;
 
     if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
         bdrv_is_sg(bs)) {
@@ -2219,6 +2221,12 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 
     tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
 
+    /* Wait until any previous flushes are completed */
+    while (bs->flush_started_gen != bs->flushed_gen) {
+        qemu_co_queue_wait(&bs->flush_queue);
+    }
+    bs->flush_started_gen = current_gen;
+
     /* Write back all layers by calling one driver function */
     if (bs->drv->bdrv_co_flush) {
         ret = bs->drv->bdrv_co_flush(bs);
@@ -2239,6 +2247,11 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
         goto flush_parent;
     }
 
+    /* Check if we really need to flush anything */
+    if (bs->flushed_gen == current_gen) {
+        goto flush_parent;
+    }
+
     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);
@@ -2279,6 +2292,10 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 flush_parent:
     ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
 out:
+    /* Notify any pending flushes that we have completed */
+    bs->flushed_gen = current_gen;
+    qemu_co_queue_restart_all(&bs->flush_queue);
+
     tracked_request_end(&req);
     return ret;
 }
@@ -2402,6 +2419,7 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
     }
     ret = 0;
 out:
+    ++bs->write_gen;
     bdrv_set_dirty(bs, req.offset >> BDRV_SECTOR_BITS,
                    req.bytes >> BDRV_SECTOR_BITS);
     tracked_request_end(&req);
include/block/block_int.h +5 -0
index 2057156..8543daf 100644
@@ -420,6 +420,11 @@ struct BlockDriverState {
                          note this is a reference count */
     bool probed;
 
+    CoQueue flush_queue;            /* Serializing flush queue */
+    unsigned int write_gen;         /* Current data generation */
+    unsigned int flush_started_gen; /* Generation for which flush has started */
+    unsigned int flushed_gen;       /* Flushed write generation */
+
     BlockDriver *drv; /* NULL means no media */
     void *opaque;
 
Signature · 1 line
2.1.4
#5 lore raw
Quoted 18 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 write generation scheme in BlockDriverState. Current write generation is checked against last flushed generation to avoid unnessesary flushes.

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.

+++ b/block/io.c

block/io.c static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs, · line 1297

Quoted 4 lines
block/io.c +1 -0
@@ -1294,6 +1294,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
     }
     bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
 
+    ++bs->write_gen;

Why pre-increment? Most code uses post-increment when done as a statement in isolation.

Quoted 12 lines
block/io.c +1 -0
     bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
 
     if (bs->wr_highest_offset < offset + bytes) {
@@ -2211,6 +2212,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 {
     int ret;
     BdrvTrackedRequest req;
+    int current_gen = bs->write_gen;
 
     if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
         bdrv_is_sg(bs)) {

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

Quoted 5 lines
block/io.c +2 -0
@@ -2219,6 +2221,12 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 
     tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
 
+    /* Wait until any previous flushes are completed */
+    while (bs->flush_started_gen != bs->flushed_gen) {

Should this be an inequality, as in s/!=/</, in case several flushes can be started in parallel and where the later flush ends up finishing before the earlier flush?

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

Quoted 13 lines
block/io.c +6 -0
+        qemu_co_queue_wait(&bs->flush_queue);
+    }
+    bs->flush_started_gen = current_gen;
+
     /* Write back all layers by calling one driver function */
     if (bs->drv->bdrv_co_flush) {
         ret = bs->drv->bdrv_co_flush(bs);
@@ -2239,6 +2247,11 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
         goto flush_parent;
     }
 
+    /* Check if we really need to flush anything */
+    if (bs->flushed_gen == current_gen) {

Likewise, if you are tracking generations, should this be s/==/<=/ (am I getting the direction correct)?

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

Quoted 9 lines
include/block/block_int.h +5 -0
+++ b/include/block/block_int.h
@@ -420,6 +420,11 @@ struct BlockDriverState {
                          note this is a reference count */
     bool probed;
 
+    CoQueue flush_queue;            /* Serializing flush queue */
+    unsigned int write_gen;         /* Current data generation */
+    unsigned int flush_started_gen; /* Generation for which flush has started */
+    unsigned int flushed_gen;       /* Flushed write generation */

Should these be 64-bit integers to avoid risk of overflow after just 2^32 flush attempts?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw
Quoted 16 lines · Eric Blake
Quoted 15 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 write generation scheme in BlockDriverState. Current write generation is checked against last flushed generation to avoid unnessesary flushes.

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.

Quoted 8 lines

block/io.c static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs, · line 1297

Quoted 6 lines

+++ b/block/io.c

block/io.c +1 -0
@@ -1294,6 +1294,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
      }
      bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
 
+    ++bs->write_gen;

Why pre-increment? Most code uses post-increment when done as a statement in isolation.

Just a habit of mine, from C++ days, where you can never be sure if someone overloaded post-increment operator and it will then generate a temporary object because post-increment is defined to return previous value. Now it's just a muscle memory :)

Quoted 14 lines
Quoted 14 lines
block/io.c +1 -0
      bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
 
      if (bs->wr_highest_offset < offset + bytes) {
@@ -2211,6 +2212,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
  {
      int ret;
      BdrvTrackedRequest req;
+    int current_gen = bs->write_gen;
 
      if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
          bdrv_is_sg(bs)) {
@@ -2219,6 +2221,12 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 
Quoted 7 lines

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

Quoted 4 lines
block/io.c +2 -0
      tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
 
+    /* Wait until any previous flushes are completed */
+    while (bs->flush_started_gen != bs->flushed_gen) {

Should this be an inequality, as in s/!=/</, in case several flushes can be started in parallel and where the later flush ends up finishing before the earlier flush?

flush_started_gen is always ahead of flushed_gen or is equal to it no matter how many requests we have in flight. using != allows us to ignore checking for unsigned overflow (which you also mention in this email).

Quoted 12 lines
Quoted 12 lines
block/io.c +4 -0
+        qemu_co_queue_wait(&bs->flush_queue);
+    }
+    bs->flush_started_gen = current_gen;
+
      /* Write back all layers by calling one driver function */
      if (bs->drv->bdrv_co_flush) {
          ret = bs->drv->bdrv_co_flush(bs);
@@ -2239,6 +2247,11 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
          goto flush_parent;
      }
 
Quoted 4 lines

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

Quoted 2 lines
block/io.c +2 -0
+    /* Check if we really need to flush anything */
+    if (bs->flushed_gen == current_gen) {

Likewise, if you are tracking generations, should this be s/==/<=/ (am I getting the direction correct)?

Same here - '==' is so that we don't have to worry about unsigned overflow.

Quoted 6 lines
Quoted 6 lines
include/block/block_int.h +1 -0
+++ b/include/block/block_int.h
@@ -420,6 +420,11 @@ struct BlockDriverState {
                           note this is a reference count */
      bool probed;
 
Quoted 7 lines

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

Quoted 4 lines
include/block/block_int.h +4 -0
+    CoQueue flush_queue;            /* Serializing flush queue */
+    unsigned int write_gen;         /* Current data generation */
+    unsigned int flush_started_gen; /* Generation for which flush has started */
+    unsigned int flushed_gen;       /* Flushed write generation */

Should these be 64-bit integers to avoid risk of overflow after just 2^32 flush attempts?

We don't have to care about unsigned overflow. It has a well defined behavior and we only check if generations are equal or not.

#7 lore raw
In reply to Denis V. Lunev
Quoted 120 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 write generation scheme in BlockDriverState. Current write generation is checked against last flushed generation to avoid unnessesary flushes.

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                   |  3 +++
 block/io.c                | 18 ++++++++++++++++++
 include/block/block_int.h |  5 +++++
 3 files changed, 26 insertions(+)
block.c +3 -0
index f4648e9..366fad6 100644
@@ -234,6 +234,8 @@ BlockDriverState *bdrv_new(void)
     bs->refcnt = 1;
     bs->aio_context = qemu_get_aio_context();
 
+    qemu_co_queue_init(&bs->flush_queue);
+
     QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
 
     return bs;
@@ -2582,6 +2584,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->write_gen;
     }
     return ret;
 }
block/io.c +18 -0
index 7cf3645..a5451b6 100644
@@ -1294,6 +1294,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
     }
     bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
 
+    ++bs->write_gen;
     bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
 
     if (bs->wr_highest_offset < offset + bytes) {
@@ -2211,6 +2212,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 {
     int ret;
     BdrvTrackedRequest req;
+    int current_gen = bs->write_gen;
 
     if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
         bdrv_is_sg(bs)) {
@@ -2219,6 +2221,12 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 
     tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
 
+    /* Wait until any previous flushes are completed */
+    while (bs->flush_started_gen != bs->flushed_gen) {
+        qemu_co_queue_wait(&bs->flush_queue);
+    }
+    bs->flush_started_gen = current_gen;
+
     /* Write back all layers by calling one driver function */
     if (bs->drv->bdrv_co_flush) {
         ret = bs->drv->bdrv_co_flush(bs);
@@ -2239,6 +2247,11 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
         goto flush_parent;
     }
 
+    /* Check if we really need to flush anything */
+    if (bs->flushed_gen == current_gen) {
+        goto flush_parent;
+    }
+
     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);
@@ -2279,6 +2292,10 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 flush_parent:
     ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
 out:
+    /* Notify any pending flushes that we have completed */
+    bs->flushed_gen = current_gen;
+    qemu_co_queue_restart_all(&bs->flush_queue);
+
     tracked_request_end(&req);
     return ret;
 }
@@ -2402,6 +2419,7 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
     }
     ret = 0;
 out:
+    ++bs->write_gen;
     bdrv_set_dirty(bs, req.offset >> BDRV_SECTOR_BITS,
                    req.bytes >> BDRV_SECTOR_BITS);
     tracked_request_end(&req);
include/block/block_int.h +0 -0
index 2057156..8543daf 100644
@@ -420,6 +420,11 @@ struct BlockDriverState {
                          note this is a reference count */

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

Quoted 10 lines
include/block/block_int.h +5 -0
     bool probed;
 
+    CoQueue flush_queue;            /* Serializing flush queue */
+    unsigned int write_gen;         /* Current data generation */
+    unsigned int flush_started_gen; /* Generation for which flush has started */
+    unsigned int flushed_gen;       /* Flushed write generation */
+
     BlockDriver *drv; /* NULL means no media */
     void *opaque;
 

Breaks qcow2 iotests 026 089 141 144

--js

#8 lore raw
Quoted 123 lines · John Snow
Quoted 121 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 write generation scheme in BlockDriverState. Current write generation is checked against last flushed generation to avoid unnessesary flushes.

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                   |  3 +++
  block/io.c                | 18 ++++++++++++++++++
  include/block/block_int.h |  5 +++++
  3 files changed, 26 insertions(+)
block.c +3 -0
index f4648e9..366fad6 100644
@@ -234,6 +234,8 @@ BlockDriverState *bdrv_new(void)
      bs->refcnt = 1;
      bs->aio_context = qemu_get_aio_context();
 
+    qemu_co_queue_init(&bs->flush_queue);
+
      QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
 
      return bs;
@@ -2582,6 +2584,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->write_gen;
      }
      return ret;
  }
block/io.c +18 -0
index 7cf3645..a5451b6 100644
@@ -1294,6 +1294,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
      }
      bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
 
+    ++bs->write_gen;
      bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
 
      if (bs->wr_highest_offset < offset + bytes) {
@@ -2211,6 +2212,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
  {
      int ret;
      BdrvTrackedRequest req;
+    int current_gen = bs->write_gen;
 
      if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
          bdrv_is_sg(bs)) {
@@ -2219,6 +2221,12 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
 
      tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
 
+    /* Wait until any previous flushes are completed */
+    while (bs->flush_started_gen != bs->flushed_gen) {
+        qemu_co_queue_wait(&bs->flush_queue);
+    }
+    bs->flush_started_gen = current_gen;
+
      /* Write back all layers by calling one driver function */
      if (bs->drv->bdrv_co_flush) {
          ret = bs->drv->bdrv_co_flush(bs);
@@ -2239,6 +2247,11 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
          goto flush_parent;
      }
 
+    /* Check if we really need to flush anything */
+    if (bs->flushed_gen == current_gen) {
+        goto flush_parent;
+    }
+
      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);
@@ -2279,6 +2292,10 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
  flush_parent:
      ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
  out:
+    /* Notify any pending flushes that we have completed */
+    bs->flushed_gen = current_gen;
+    qemu_co_queue_restart_all(&bs->flush_queue);
+
      tracked_request_end(&req);
      return ret;
  }
@@ -2402,6 +2419,7 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
      }
      ret = 0;
  out:
+    ++bs->write_gen;
      bdrv_set_dirty(bs, req.offset >> BDRV_SECTOR_BITS,
                     req.bytes >> BDRV_SECTOR_BITS);
      tracked_request_end(&req);
include/block/block_int.h +0 -0
index 2057156..8543daf 100644
@@ -420,6 +420,11 @@ struct BlockDriverState {
                           note this is a reference count */
      bool probed;
 
Quoted 10 lines

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

Quoted 9 lines
include/block/block_int.h +5 -0
+    CoQueue flush_queue;            /* Serializing flush queue */
+    unsigned int write_gen;         /* Current data generation */
+    unsigned int flush_started_gen; /* Generation for which flush has started */
+    unsigned int flushed_gen;       /* Flushed write generation */
+
      BlockDriver *drv; /* NULL means no media */
      void *opaque;
 

Breaks qcow2 iotests 026 089 141 144

Sorry, didn't knew those tests existed, only ran make check previously.
Looking at 026, looks like it is the same problem as in IDE and AHCI. Test case injects blkdebug write errors which should be triggered by flushes and expects to see them in output. However those flushes are now skipped and no events are generated. Otherwise resulting image looks consistent, all data was flushed. Expect the same problem to be with other tests, but maybe test case is incorrect now?

Quoted 2 lines

--js

#9 lore raw
Quoted 135 lines · Evgeny Yakovlev
Quoted 123 lines · John Snow
Quoted 121 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 write generation scheme in BlockDriverState. Current write generation is checked against last flushed generation to avoid unnessesary flushes.

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                   |  3 +++
  block/io.c                | 18 ++++++++++++++++++
  include/block/block_int.h |  5 +++++
  3 files changed, 26 insertions(+)
block.c +2 -0
index f4648e9..366fad6 100644
@@ -234,6 +234,8 @@ BlockDriverState *bdrv_new(void)
      bs->refcnt = 1;
      bs->aio_context = qemu_get_aio_context();
  +    qemu_co_queue_init(&bs->flush_queue);
+
      QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
        return bs;
@@ -2582,6 +2584,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->write_gen;
      }
      return ret;
  }
block/io.c +14 -0
index 7cf3645..a5451b6 100644
@@ -1294,6 +1294,7 @@ static int coroutine_fn
 bdrv_aligned_pwritev(BlockDriverState *bs,
      }
      bdrv_debug_event(bs, BLKDBG_PWRITEV_DONE);
  +    ++bs->write_gen;
      bdrv_set_dirty(bs, start_sector, end_sector - start_sector);
        if (bs->wr_highest_offset < offset + bytes) {
@@ -2211,6 +2212,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState
 *bs)
  {
      int ret;
      BdrvTrackedRequest req;
+    int current_gen = bs->write_gen;
        if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
          bdrv_is_sg(bs)) {
@@ -2219,6 +2221,12 @@ int coroutine_fn
 bdrv_co_flush(BlockDriverState *bs)
        tracked_request_begin(&req, bs, 0, 0, BDRV_TRACKED_FLUSH);
  +    /* Wait until any previous flushes are completed */
+    while (bs->flush_started_gen != bs->flushed_gen) {
+        qemu_co_queue_wait(&bs->flush_queue);
+    }
+    bs->flush_started_gen = current_gen;
+
      /* Write back all layers by calling one driver function */
      if (bs->drv->bdrv_co_flush) {
          ret = bs->drv->bdrv_co_flush(bs);
@@ -2239,6 +2247,11 @@ int coroutine_fn
 bdrv_co_flush(BlockDriverState *bs)
          goto flush_parent;
      }
  +    /* Check if we really need to flush anything */
+    if (bs->flushed_gen == current_gen) {
+        goto flush_parent;
+    }
+
      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);
@@ -2279,6 +2292,10 @@ int coroutine_fn
 bdrv_co_flush(BlockDriverState *bs)
  flush_parent:
      ret = bs->file ? bdrv_co_flush(bs->file->bs) : 0;
  out:
+    /* Notify any pending flushes that we have completed */
+    bs->flushed_gen = current_gen;
+    qemu_co_queue_restart_all(&bs->flush_queue);
+
      tracked_request_end(&req);
      return ret;
  }
block/io.c +1 -0
@@ -2402,6 +2419,7 @@ int coroutine_fn
 bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
      }
      ret = 0;
  out:
+    ++bs->write_gen;
      bdrv_set_dirty(bs, req.offset >> BDRV_SECTOR_BITS,
                     req.bytes >> BDRV_SECTOR_BITS);
      tracked_request_end(&req);
include/block/block_int.h +1 -0
index 2057156..8543daf 100644
@@ -420,6 +420,11 @@ struct BlockDriverState {
                           note this is a reference count */
      bool probed;
  +    CoQueue flush_queue;            /* Serializing flush queue */
+    unsigned int write_gen;         /* Current data generation */
Quoted 8 lines

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

Quoted 7 lines
include/block/block_int.h +1 -0
+    unsigned int flush_started_gen; /* Generation for which flush

has started */

include/block/block_int.h +2 -0
+    unsigned int flushed_gen;       /* Flushed write generation */
+
      BlockDriver *drv; /* NULL means no media */
      void *opaque;

Breaks qcow2 iotests 026 089 141 144

Quoted 8 lines

Sorry, didn't knew those tests existed, only ran make check previously. Looking at 026, looks like it is the same problem as in IDE and AHCI. Test case injects blkdebug write errors which should be triggered by flushes and expects to see them in output. However those flushes are now skipped and no events are generated. Otherwise resulting image looks consistent, all data was flushed. Expect the same problem to be with other tests, but maybe test case is incorrect now?

No problem -- these tests don't *always* get run before merge but I like to enforce it for my tree.

Yes, it just looks like most of the test output has to be updated.

In the case of 026, it looks like most test cases that are testing for failures have other error text to confirm that the error did indeed happen, and that the flush messages are just "extraneous" failure messages -- i.e. not the primary effect being tested for, so it should be safe to just update the reference output.

I suspect the other tests are similar.

Thank you,
--John

Reviewed-by on the v5 series
#10 lore raw
In reply to Denis V. Lunev
Quoted 32 lines · Denis V. Lunev

Changes from v4:
- Moved to write generation scheme instead of dirty flag
- Added retry setup to IDE PIO and FLUSH requests

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 (4):
ide: refactor retry_unit set and clear into separate function
ide: set retry_unit for PIO and FLUSH requests
tests: in IDE and AHCI tests perform DMA write before flushing
block: ignore flush requests when storage is clean

Quoted 8 lines
 block.c                   |  3 +++
 block/io.c                | 18 ++++++++++++++++++
 hw/ide/core.c             | 24 ++++++++++++++++++------
 include/block/block_int.h |  5 +++++
 tests/ahci-test.c         | 34 ++++++++++++++++++++++++++++++++--
 tests/ide-test.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 119 insertions(+), 8 deletions(-)

Thanks!

Paolo

#11 lore raw
Quoted 41 lines · Paolo Bonzini
Quoted 32 lines · Denis V. Lunev

Changes from v4:
- Moved to write generation scheme instead of dirty flag
- Added retry setup to IDE PIO and FLUSH requests

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 (4):
ide: refactor retry_unit set and clear into separate function
ide: set retry_unit for PIO and FLUSH requests
tests: in IDE and AHCI tests perform DMA write before flushing
block: ignore flush requests when storage is clean

Quoted 8 lines
  block.c                   |  3 +++
  block/io.c                | 18 ++++++++++++++++++
  hw/ide/core.c             | 24 ++++++++++++++++++------
  include/block/block_int.h |  5 +++++
  tests/ahci-test.c         | 34 ++++++++++++++++++++++++++++++++--
  tests/ide-test.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
  6 files changed, 119 insertions(+), 8 deletions(-)
Quoted 3 lines

Thanks!

You're welcome!

Quoted 2 lines

Paolo

#12 lore raw
In reply to Paolo Bonzini
Quoted 42 lines · Paolo Bonzini
Quoted 32 lines · Denis V. Lunev

Changes from v4:
- Moved to write generation scheme instead of dirty flag
- Added retry setup to IDE PIO and FLUSH requests

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 (4):
ide: refactor retry_unit set and clear into separate function
ide: set retry_unit for PIO and FLUSH requests
tests: in IDE and AHCI tests perform DMA write before flushing
block: ignore flush requests when storage is clean

Quoted 8 lines
 block.c                   |  3 +++
 block/io.c                | 18 ++++++++++++++++++
 hw/ide/core.c             | 24 ++++++++++++++++++------
 include/block/block_int.h |  5 +++++
 tests/ahci-test.c         | 34 ++++++++++++++++++++++++++++++++--
 tests/ide-test.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 119 insertions(+), 8 deletions(-)
Quoted 6 lines

Thanks!

Paolo

I'll stage and send tomorrow, thank you.

--js