Skip to contentSkip to conversation

final patches for cache=writeback

PATCH Applied Paolo Bonzini 2 participants 5 messages Aug 9, 2012

lore
Share & tools
lore
#0 lore raw

cover final patches for cache=writeback

Kevin,

here is a resend of the virtio-blk patches plus the one-liner for flipping the default cache mode from writethrough to writeback.

Paolo Bonzini (3):
virtio-blk: support VIRTIO_BLK_F_CONFIG_WCE
virtio-blk: disable write cache if not negotiated
blockdev: flip default cache mode from writethrough to writeback

 blockdev.c      |  1 +
 hw/virtio-blk.c | 30 ++++++++++++++++++++++++++++--
 hw/virtio-blk.h |  4 +++-

3 file modificati, 32 inserzioni(+), 3 rimozioni(-)

Signature · 1 line
1.7.11.2
#1 lore raw

PATCH 1/3 virtio-blk: support VIRTIO_BLK_F_CONFIG_WCE

Also rename VIRTIO_BLK_F_WCACHE to VIRTIO_BLK_F_WCE for consistency with the spec.

---
 hw/virtio-blk.c | 16 ++++++++++++++--
 hw/virtio-blk.h |  4 +++-

2 file modificati, 17 inserzioni(+), 3 rimozioni(-)

hw/virtio-blk.c +14 -2
index f21757e..9c12b61 100644
@@ -509,9 +509,19 @@ static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
     blkcfg.size_max = 0;
     blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
     blkcfg.alignment_offset = 0;
+    blkcfg.wce = bdrv_enable_write_cache(s->bs);
     memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
 }
 
+static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
+{
+    VirtIOBlock *s = to_virtio_blk(vdev);
+    struct virtio_blk_config blkcfg;
+
+    memcpy(&blkcfg, config, sizeof(blkcfg));
+    bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
+}
+
 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
 {
     VirtIOBlock *s = to_virtio_blk(vdev);
@@ -522,9 +532,10 @@ static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
     features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
     features |= (1 << VIRTIO_BLK_F_SCSI);
 
+    features |= (1 << VIRTIO_BLK_F_CONFIG_WCE);
     if (bdrv_enable_write_cache(s->bs))
-        features |= (1 << VIRTIO_BLK_F_WCACHE);
-    
+        features |= (1 << VIRTIO_BLK_F_WCE);
+
     if (bdrv_is_read_only(s->bs))
         features |= 1 << VIRTIO_BLK_F_RO;
 
@@ -609,6 +620,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
                                           sizeof(VirtIOBlock));
 
     s->vdev.get_config = virtio_blk_update_config;
+    s->vdev.set_config = virtio_blk_set_config;
     s->vdev.get_features = virtio_blk_get_features;
     s->vdev.reset = virtio_blk_reset;
     s->bs = blk->conf.bs;
hw/virtio-blk.h +3 -1
index 79ebccc..35834cf 100644
@@ -31,8 +31,9 @@
 #define VIRTIO_BLK_F_BLK_SIZE   6       /* Block size of disk is available*/
 #define VIRTIO_BLK_F_SCSI       7       /* Supports scsi command passthru */
 /* #define VIRTIO_BLK_F_IDENTIFY   8       ATA IDENTIFY supported, DEPRECATED */
-#define VIRTIO_BLK_F_WCACHE     9       /* write cache enabled */
+#define VIRTIO_BLK_F_WCE        9       /* write cache enabled */
 #define VIRTIO_BLK_F_TOPOLOGY   10      /* Topology information is available */
+#define VIRTIO_BLK_F_CONFIG_WCE 11      /* write cache configurable */
 
 #define VIRTIO_BLK_ID_BYTES     20      /* ID string length */
 
@@ -49,6 +50,7 @@ struct virtio_blk_config
     uint8_t alignment_offset;
     uint16_t min_io_size;
     uint32_t opt_io_size;
+    uint8_t wce;
 } QEMU_PACKED;
 
 /* These two define direction. */
Signature · 1 line
1.7.11.2
#2 lore raw

PATCH 2/3 virtio-blk: disable write cache if not negotiated

In reply to Paolo Bonzini

If the guest does not support flushes, we should run in writethrough mode. The setting is temporary until the next reset, so that for example the BIOS will run in writethrough mode while Linux will run with a writeback cache.

---
 hw/virtio-blk.c | 14 ++++++++++++++

1 file modificato, 14 inserzioni(+)

hw/virtio-blk.c +14 -0
index 9c12b61..36bfb12 100644
@@ -542,6 +542,19 @@ static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
     return features;
 }
 
+static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
+{
+    VirtIOBlock *s = to_virtio_blk(vdev);
+    uint32_t features;
+
+    if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
+        return;
+    }
+
+    features = vdev->guest_features;
+    bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
+}
+
 static void virtio_blk_save(QEMUFile *f, void *opaque)
 {
     VirtIOBlock *s = opaque;
@@ -622,6 +635,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
     s->vdev.get_config = virtio_blk_update_config;
     s->vdev.set_config = virtio_blk_set_config;
     s->vdev.get_features = virtio_blk_get_features;
+    s->vdev.set_status = virtio_blk_set_status;
     s->vdev.reset = virtio_blk_reset;
     s->bs = blk->conf.bs;
     s->conf = &blk->conf;
Signature · 1 line
1.7.11.2
#3 lore raw

PATCH 3/3 blockdev: flip default cache mode from writethrough to writeback

In reply to Paolo Bonzini

Now all major device models (IDE, SCSI, virtio) can choose between writethrough and writeback at run-time, and virtio will even revert to writethrough if the guest is not capable of sending flushes. So we can change the default to writeback at last.

Tested, for lack of a better idea, with a breakpoint on bdrv_open and all cache choices one by one.

---
 blockdev.c | 1 +

1 file modificato, 1 inserzione(+)

blockdev.c +1 -0
index 8669142..7c83baa 100644
@@ -377,6 +377,7 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
 	}
     }
 
+    bdrv_flags |= BDRV_O_CACHE_WB;
     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
         if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
             error_report("invalid cache option");
Signature · 1 line
1.7.11.2
Applied
#4 lore raw
In reply to Paolo Bonzini

Am 09.08.2012 16:07, schrieb Paolo Bonzini:

Quoted 10 lines

Kevin,

here is a resend of the virtio-blk patches plus the one-liner for flipping the default cache mode from writethrough to writeback.

Paolo Bonzini (3):
virtio-blk: support VIRTIO_BLK_F_CONFIG_WCE
virtio-blk: disable write cache if not negotiated
blockdev: flip default cache mode from writethrough to writeback

Quoted 4 lines
 blockdev.c      |  1 +
 hw/virtio-blk.c | 30 ++++++++++++++++++++++++++++--
 hw/virtio-blk.h |  4 +++-

3 file modificati, 32 inserzioni(+), 3 rimozioni(-)

Thanks, applied all to the block branch.

Kevin