Skip to contentSkip to conversation

virtio-blk: Add support for FUA write requests

PATCH Applied Alberto Faria 4 participants 8 messages Oct 21 – Nov 2

lore
Share & tools
lore
#0 lore raw

cover virtio-blk: Add support for FUA write requests

virtio-blk FUA write support has been introduced to the virtio spec [1], and a kernel patch is under review [2]. This patch enables FUA on the virtio-blk device, so guests can submit FUA writes instead of the slower alternative of a regular write followed by a flush.

A simple randwrite fio workload on a RAM disk (bs=4k, iodepth=1, direct=1, sync=1) shows a throughput improvement of 17.1k IOPS to 27.7k IOPS (+62%).

[1] https://lore.kernel.org/virtio-comment/[email protected]/
[2] https://lore.kernel.org/linux-block/[email protected]/

Alberto Faria (2):
include: Add virtio-blk request flag FUA definitions
virtio-blk: Add support for FUA write requests

 hw/block/virtio-blk.c                       | 27 +++++++++++++++++++++
 hw/core/machine.c                           |  1 +
 hw/virtio/virtio-qmp.c                      |  4 +++
 include/hw/virtio/virtio-blk.h              |  1 +
 include/standard-headers/linux/virtio_blk.h | 13 ++++++++--
 5 files changed, 44 insertions(+), 2 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/2 include: Add virtio-blk request flag FUA definitions

---
 include/standard-headers/linux/virtio_blk.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
include/standard-headers/linux/virtio_blk.h +11 -2
index d7be3cf5e4..c6c8ce1eec 100644
@@ -42,6 +42,8 @@
 #define VIRTIO_BLK_F_WRITE_ZEROES	14	/* WRITE ZEROES is supported */
 #define VIRTIO_BLK_F_SECURE_ERASE	16 /* Secure Erase is supported */
 #define VIRTIO_BLK_F_ZONED		17	/* Zoned block device */
+#define VIRTIO_BLK_F_REQ_FLAGS		18	/* Request flags supported */
+#define VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA	19	/* FUA flag supported */
 
 /* Legacy feature bits */
 #ifndef VIRTIO_BLK_NO_LEGACY
@@ -209,6 +211,9 @@ struct virtio_blk_config {
 #define VIRTIO_BLK_T_BARRIER	0x80000000
 #endif /* !VIRTIO_BLK_NO_LEGACY */
 
+/* Request flag bits */
+#define VIRTIO_BLK_REQ_FLAG_OUT_FUA	0
+
 /*
  * This comes first in the read scatter-gather list.
  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
@@ -217,8 +222,12 @@ struct virtio_blk_config {
 struct virtio_blk_outhdr {
 	/* VIRTIO_BLK_T* */
 	__virtio32 type;
-	/* io priority. */
-	__virtio32 ioprio;
+	union {
+		/* io priority. */
+		__virtio32 ioprio;
+		/* Request flags. */
+		__virtio32 flags;
+	};
 	/* Sector (ie. 512 byte offset) */
 	__virtio64 sector;
 };
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/2] include: Add virtio-blk request flag FUA definitions

Quoted 5 lines · Alberto Faria
---
 include/standard-headers/linux/virtio_blk.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Maybe explicitly mark as *placeholder*, so it doesn't get merged accidentally?

#3 lore raw

Re: [PATCH 1/2] include: Add virtio-blk request flag FUA definitions

Am 21.10.2026 um 09:59 hat Cornelia Huck geschrieben:

Quoted 9 lines · Alberto Faria
Quoted 5 lines
---
 include/standard-headers/linux/virtio_blk.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Maybe explicitly mark as *placeholder*, so it doesn't get merged accidentally?

What is the proper (or at least common) way to do this? Do we really have to wait for Linux to merge the guest driver side of virtio device changes before we can add the emulation in QEMU?

Kevin

#4 lore raw

Re: [PATCH 1/2] include: Add virtio-blk request flag FUA definitions

Quoted 8 lines · Kevin Wolf

Am 21.10.2026 um 09:59 hat Cornelia Huck geschrieben:

Quoted 7 lines · Alberto Faria
Quoted 5 lines
---
 include/standard-headers/linux/virtio_blk.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
Quoted 6 lines
Quoted 2 lines

Maybe explicitly mark as *placeholder*, so it doesn't get merged accidentally?

What is the proper (or at least common) way to do this? Do we really have to wait for Linux to merge the guest driver side of virtio device changes before we can add the emulation in QEMU?

If we have some changes in the synced-from-Linux headers, we really need to do a proper header sync to get them (or the next sync might remove the changes again...) So yes, this unfortunately needs to wait for a kernel with the changes (can be -rc)...

***

The other question (not really related to this patch set, maybe warranting a separate discussion): Should we move the host <-> guest interface definition to a third place that both Linux and QEMU (and any other piece of software) can sync from? Source-of-truth is the virtio spec, so in a perfect world QEMU should not have to wait for a Linux kernel with the definitions to be released once it has been enshrined there. Grabbing everything from Linux is something that did get the job done and avoided duplicating code, but maybe it's time to revisit this. (We obviously should keep syncing headers for actual kernel <-> userspace interfaces.)

#5 lore raw

PATCH 2/2 virtio-blk: Add support for FUA write requests

In reply to Alberto Faria
---
 hw/block/virtio-blk.c          | 27 +++++++++++++++++++++++++++
 hw/core/machine.c              |  1 +
 hw/virtio/virtio-qmp.c         |  4 ++++
 include/hw/virtio/virtio-blk.h |  1 +
 4 files changed, 33 insertions(+)
hw/block/virtio-blk.c +27 -0
index 61c341ce2c..3e01e043bd 100644
@@ -219,6 +219,7 @@ static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb,
     QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
     int64_t sector_num = mrb->reqs[start]->sector_num;
     bool is_write = mrb->is_write;
+    bool is_fua = mrb->is_fua;
     BdrvRequestFlags flags = 0;
 
     if (num_reqs > 1) {
@@ -255,6 +256,9 @@ static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb,
     }
 
     if (is_write) {
+        if (is_fua) {
+            flags |= BDRV_REQ_FUA;
+        }
         blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov,
                         flags, virtio_blk_rw_complete,
                         mrb->reqs[start]);
@@ -829,6 +833,7 @@ out:
 static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 {
     uint32_t type;
+    uint32_t flags;
     struct iovec *in_iov = req->elem.in_sg;
     struct iovec *out_iov = req->elem.out_sg;
     unsigned in_num = req->elem.in_num;
@@ -865,6 +870,7 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
                               &req->inhdr_undo);
 
     type = virtio_ldl_p(vdev, &req->out.type);
+    flags = virtio_ldl_p(vdev, &req->out.flags);
 
     /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
      * is an optional flag. Although a guest should not send this flag if
@@ -873,6 +879,9 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
     case VIRTIO_BLK_T_IN:
     {
         bool is_write = type & VIRTIO_BLK_T_OUT;
+        bool is_fua = is_write &&
+            virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA) &&
+            (flags & (1U << VIRTIO_BLK_REQ_FLAG_OUT_FUA));
         req->sector_num = virtio_ldq_p(vdev, &req->out.sector);
 
         if (is_write) {
@@ -900,6 +909,7 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
          * changes */
         if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
                                   is_write != mrb->is_write ||
+                                  is_fua != mrb->is_fua ||
                                   !s->conf.request_merging)) {
             virtio_blk_submit_multireq(s, mrb);
         }
@@ -907,6 +917,7 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
         assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
         mrb->reqs[mrb->num_reqs++] = req;
         mrb->is_write = is_write;
+        mrb->is_fua = is_fua;
         break;
     }
     case VIRTIO_BLK_T_FLUSH:
@@ -1283,6 +1294,10 @@ static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
     /* Firstly sync all virtio-blk possible supported features */
     features |= s->host_features;
 
+    if (virtio_has_feature(features, VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA)) {
+        virtio_add_feature(&features, VIRTIO_BLK_F_REQ_FLAGS);
+    }
+
     virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
     virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
     virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
@@ -1308,6 +1323,15 @@ static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
     return features;
 }
 
+static int virtio_blk_validate_features(VirtIODevice *vdev)
+{
+    if (virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA) &&
+        !virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_REQ_FLAGS)) {
+        return -EINVAL;
+    }
+    return 0;
+}
+
 static int virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
 {
     VirtIOBlock *s = VIRTIO_BLK(vdev);
@@ -1925,6 +1949,8 @@ static const Property virtio_blk_properties[] = {
                        conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS),
     DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock,
                        conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS),
+    DEFINE_PROP_BIT64("fua", VirtIOBlock, host_features,
+                      VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA, true),
     DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock,
                      conf.x_enable_wce_if_config_wce, true),
 };
@@ -1942,6 +1968,7 @@ static void virtio_blk_class_init(ObjectClass *klass, const void *data)
     vdc->get_config = virtio_blk_update_config;
     vdc->set_config = virtio_blk_set_config;
     vdc->get_features = virtio_blk_get_features;
+    vdc->validate_features = virtio_blk_validate_features;
     vdc->set_status = virtio_blk_set_status;
     vdc->reset = virtio_blk_reset;
     vdc->save = virtio_blk_save_device;
hw/core/machine.c +1 -0
index d9ac4b419a..7fc1853dab 100644
@@ -43,6 +43,7 @@
 GlobalProperty hw_compat_11_1[] = {
     { "sysbus-ehci-usb", "x-migrate-fetch-addr-64bit", "off" },
     { "pci-ehci-usb", "x-migrate-fetch-addr-64bit", "off" },
+    { "virtio-blk-device", "fua", "off" },
 };
 const size_t hw_compat_11_1_len = G_N_ELEMENTS(hw_compat_11_1);
 
hw/virtio/virtio-qmp.c +4 -0
index 5aaeebdd96..4aaab650c6 100644
@@ -171,6 +171,10 @@ static const qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
             "VIRTIO_BLK_F_SECURE_ERASE: Secure erase supported"),
     FEATURE_ENTRY(VIRTIO_BLK_F_ZONED, \
             "VIRTIO_BLK_F_ZONED: Zoned block devices"),
+    FEATURE_ENTRY(VIRTIO_BLK_F_REQ_FLAGS, \
+            "VIRTIO_BLK_F_REQ_FLAGS: Request flags supported"),
+    FEATURE_ENTRY(VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA, \
+            "VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA: FUA flag for OUT requests"),
     FEATURE_ENTRY(VIRTIO_BLK_F_BARRIER, \
             "VIRTIO_BLK_F_BARRIER: Request barriers supported"),
     FEATURE_ENTRY(VIRTIO_BLK_F_SCSI, \
include/hw/virtio/virtio-blk.h +1 -0
index 3d8dee7ec1..916df26b6f 100644
@@ -98,6 +98,7 @@ typedef struct MultiReqBuffer {
     VirtIOBlockReq *reqs[VIRTIO_BLK_MAX_MERGE_REQS];
     unsigned int num_reqs;
     bool is_write;
+    bool is_fua;
 } MultiReqBuffer;
 
 typedef struct VirtIOBlkClass {
Signature · 1 line
2.56.0
Applied
#7 lore raw
In reply to Alberto Faria

Am 21.10.2026 um 08:22 hat Alberto Faria geschrieben:

Quoted 8 lines

virtio-blk FUA write support has been introduced to the virtio spec [1], and a kernel patch is under review [2]. This patch enables FUA on the virtio-blk device, so guests can submit FUA writes instead of the slower alternative of a regular write followed by a flush.

A simple randwrite fio workload on a RAM disk (bs=4k, iodepth=1, direct=1, sync=1) shows a throughput improvement of 17.1k IOPS to 27.7k IOPS (+62%).

Quoted 6 lines

[1] https://lore.kernel.org/virtio-comment/[email protected]/
[2] https://lore.kernel.org/linux-block/[email protected]/

Alberto Faria (2):
include: Add virtio-blk request flag FUA definitions
virtio-blk: Add support for FUA write requests

As patch 1 can't be applied, I squashed in the below temporary workaround that should be reverted after the Linux headers provide the necessary definitions.

Thanks, applied to the block branch.

Kevin

hw/block/virtio-blk.c +11 -1
index 345c9e07567..9b7c84fda6c 100644
@@ -38,6 +38,16 @@
 #include "hw/virtio/virtio-blk-common.h"
 #include "qemu/coroutine.h"
 
+/* TODO Remove once the Linux headers have caught up */
+#ifndef VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA
+#define VIRTIO_BLK_F_REQ_FLAGS         18      /* Request flags supported */
+#define VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA 19      /* FUA flag supported */
+
+#define VIRTIO_BLK_REQ_FLAG_OUT_FUA    0
+
+#define VIRTIO_BLK_OUTDHR_FLAGS(out) out.ioprio
+#endif
+
 /* Internal buffer size limit for zone report */
 #define VIRTIO_BLK_MAX_ZONES_PER_BATCH 4096
 
@@ -936,7 +946,7 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
                               &req->inhdr_undo);
 
     type = virtio_ldl_p(vdev, &req->out.type);
-    flags = virtio_ldl_p(vdev, &req->out.flags);
+    flags = virtio_ldl_p(vdev, &VIRTIO_BLK_OUTDHR_FLAGS(req->out));
 
     /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
      * is an optional flag. Although a guest should not send this flag if
hw/virtio/virtio-qmp.c +6 -0
index a15ec3685a3..af2f4b73dce 100644
@@ -33,6 +33,12 @@
 #include "standard-headers/linux/virtio_vsock.h"
 #include "standard-headers/linux/virtio_gpio.h"
 
+/* TODO Remove once the Linux headers have caught up */
+#ifndef VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA
+#define VIRTIO_BLK_F_REQ_FLAGS         18      /* Request flags supported */
+#define VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA 19      /* FUA flag supported */
+#endif
+
 #define FEATURE_ENTRY(name, desc) (qmp_virtio_feature_map_t) \
     { .virtio_bit = name, .feature_desc = desc }