Skip to contentSkip to conversation

virtio_blk: add fua write support

RFC In review Alberto Faria 3 participants 7 messages May 8, 2025 – May 14

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover virtio_blk: add fua write support

There is a proposal to introduce virtio-blk fua write support to the virtio spec [1]. This series implements that by adding VIRTIO_BLK_{F,T}_OUT_FUA uapi definitions and corresponding virtio-blk support. It also enables vdpa_sim_blk to handle fua writes.

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

Alberto Faria (2):
virtio_blk: add fua write support
vdpa_sim_blk: add support for fua write commands

 drivers/block/virtio_blk.c           | 10 +++++++---
 drivers/vdpa/vdpa_sim/vdpa_sim_blk.c |  6 ++++--
 include/uapi/linux/virtio_blk.h      |  4 ++++
 3 files changed, 15 insertions(+), 5 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

RFC 1/2 virtio_blk: add fua write support

Add the proposed VIRTIO_BLK_{F,T}_OUT_FUA uapi definitions and corresponding virtio-blk support.

---
 drivers/block/virtio_blk.c      | 10 +++++++---
 include/uapi/linux/virtio_blk.h |  4 ++++
 2 files changed, 11 insertions(+), 3 deletions(-)
drivers/block/virtio_blk.c +7 -3
index 7cffea01d868c..021f05c469c50 100644
@@ -256,7 +256,8 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
 		sector = blk_rq_pos(req);
 		break;
 	case REQ_OP_WRITE:
-		type = VIRTIO_BLK_T_OUT;
+		type = req->cmd_flags & REQ_FUA ?
+			VIRTIO_BLK_T_OUT_FUA : VIRTIO_BLK_T_OUT;
 		sector = blk_rq_pos(req);
 		break;
 	case REQ_OP_FLUSH:
@@ -1500,6 +1501,9 @@ static int virtblk_probe(struct virtio_device *vdev)
 	if (virtblk_get_cache_mode(vdev))
 		lim.features |= BLK_FEAT_WRITE_CACHE;
 
+	if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_OUT_FUA))
+		lim.features |= BLK_FEAT_FUA;
+
 	vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, &lim, vblk);
 	if (IS_ERR(vblk->disk)) {
 		err = PTR_ERR(vblk->disk);
@@ -1650,7 +1654,7 @@ static unsigned int features_legacy[] = {
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
 	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
-	VIRTIO_BLK_F_SECURE_ERASE,
+	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_OUT_FUA,
 }
 ;
 static unsigned int features[] = {
@@ -1658,7 +1662,7 @@ static unsigned int features[] = {
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
 	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
-	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED,
+	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED, VIRTIO_BLK_F_OUT_FUA,
 };
 
 static struct virtio_driver virtio_blk = {
include/uapi/linux/virtio_blk.h +4 -0
index 3744e4da1b2a7..52be7943f6b8d 100644
@@ -42,6 +42,7 @@
 #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_OUT_FUA		18	/* FUA write is supported */
 
 /* Legacy feature bits */
 #ifndef VIRTIO_BLK_NO_LEGACY
@@ -206,6 +207,9 @@ struct virtio_blk_config {
 /* Reset All zones command */
 #define VIRTIO_BLK_T_ZONE_RESET_ALL 26
 
+/* FUA write command */
+#define VIRTIO_BLK_T_OUT_FUA        27
+
 #ifndef VIRTIO_BLK_NO_LEGACY
 /* Barrier before this op. */
 #define VIRTIO_BLK_T_BARRIER	0x80000000
Signature · 1 line
2.49.0
#2 lore raw
Quoted 34 lines · Alberto Faria

Add the proposed VIRTIO_BLK_{F,T}_OUT_FUA uapi definitions and corresponding virtio-blk support.

---
 drivers/block/virtio_blk.c      | 10 +++++++---
 include/uapi/linux/virtio_blk.h |  4 ++++
 2 files changed, 11 insertions(+), 3 deletions(-)
drivers/block/virtio_blk.c +5 -1
index 7cffea01d868c..021f05c469c50 100644
@@ -256,7 +256,8 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
 		sector = blk_rq_pos(req);
 		break;
 	case REQ_OP_WRITE:
-		type = VIRTIO_BLK_T_OUT;
+		type = req->cmd_flags & REQ_FUA ?
+			VIRTIO_BLK_T_OUT_FUA : VIRTIO_BLK_T_OUT;
 		sector = blk_rq_pos(req);
 		break;
 	case REQ_OP_FLUSH:
@@ -1500,6 +1501,9 @@ static int virtblk_probe(struct virtio_device *vdev)
 	if (virtblk_get_cache_mode(vdev))
 		lim.features |= BLK_FEAT_WRITE_CACHE;
 
+	if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_OUT_FUA))
+		lim.features |= BLK_FEAT_FUA;
+
 	vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, &lim, vblk);
 	if (IS_ERR(vblk->disk)) {
 		err = PTR_ERR(vblk->disk);

drivers/block/virtio_blk.c static unsigned int features_legacy[] = { · line 1657

Quoted 5 lines
drivers/block/virtio_blk.c +1 -1
@@ -1650,7 +1654,7 @@ static unsigned int features_legacy[] = {
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
 	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
-	VIRTIO_BLK_F_SECURE_ERASE,
+	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_OUT_FUA,

This is a new feature. Legacy VIRTIO devices don't support it. VIRTIO_BLK_F_ZONED was not added to features_legacy[] when it was introduced either.

Quoted 37 lines
drivers/block/virtio_blk.c +1 -1
 }
 ;
 static unsigned int features[] = {
@@ -1658,7 +1662,7 @@ static unsigned int features[] = {
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
 	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
-	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED,
+	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED, VIRTIO_BLK_F_OUT_FUA,
 };
 
 static struct virtio_driver virtio_blk = {
include/uapi/linux/virtio_blk.h +4 -0
index 3744e4da1b2a7..52be7943f6b8d 100644
@@ -42,6 +42,7 @@
 #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_OUT_FUA		18	/* FUA write is supported */
 
 /* Legacy feature bits */
 #ifndef VIRTIO_BLK_NO_LEGACY
@@ -206,6 +207,9 @@ struct virtio_blk_config {
 /* Reset All zones command */
 #define VIRTIO_BLK_T_ZONE_RESET_ALL 26
 
+/* FUA write command */
+#define VIRTIO_BLK_T_OUT_FUA        27
+
 #ifndef VIRTIO_BLK_NO_LEGACY
 /* Barrier before this op. */
 #define VIRTIO_BLK_T_BARRIER	0x80000000
Signature · 1 line
2.49.0
#3 lore raw

RFC PATCH v2 1/1 virtio_blk: add fua write support

Continuing the work of Alberto Faria <[email protected]> this patch addresses the remaining feedback regarding removing VIRTIO_BLK_F_OUT_FUA from features_legacy[]

---
 drivers/block/virtio_blk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/virtio_blk.c +1 -1
index 5a6aa3dfb18a..aee2d503d6ef 100644
@@ -1665,7 +1665,7 @@ static unsigned int features_legacy[] = {
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
 	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
-	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_OUT_FUA,
+	VIRTIO_BLK_F_SECURE_ERASE,
 }
 ;
 static unsigned int features[] = {
Signature · 1 line
2.54.0
#4 lore raw
Quoted 10 lines · Sana Sharma

Continuing the work of Alberto Faria <[email protected]> this patch addresses the remaining feedback regarding removing VIRTIO_BLK_F_OUT_FUA from features_legacy[]

Quoted 3 lines
---
 drivers/block/virtio_blk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, Sana!

Please squash your change into Alberto's commit, rebase against the latest linux.git/master, test against a patched QEMU, and resend the entire series once the VIRTIO spec change has been merged (the vote is currently ongoing).

Quoted 17 lines
drivers/block/virtio_blk.c +1 -1
index 5a6aa3dfb18a..aee2d503d6ef 100644
@@ -1665,7 +1665,7 @@ static unsigned int features_legacy[] = {
 	VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 	VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
 	VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
-	VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_OUT_FUA,
+	VIRTIO_BLK_F_SECURE_ERASE,
 }
 ;
 static unsigned int features[] = {
Signature · 1 line
2.54.0
#5 lore raw

RFC 2/2 vdpa_sim_blk: add support for fua write commands

In reply to Alberto Faria

Expose VIRTIO_BLK_F_OUT_FUA to drivers. The simulator is a ramdisk, so it handles VIRTIO_BLK_T_OUT and VIRTIO_BLK_T_OUT_FUA in the same way.

---
 drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +4 -2
index b137f36793439..64f293e2931d1 100644
@@ -31,7 +31,8 @@
 				 (1ULL << VIRTIO_BLK_F_TOPOLOGY) | \
 				 (1ULL << VIRTIO_BLK_F_MQ)       | \
 				 (1ULL << VIRTIO_BLK_F_DISCARD)  | \
-				 (1ULL << VIRTIO_BLK_F_WRITE_ZEROES))
+				 (1ULL << VIRTIO_BLK_F_WRITE_ZEROES)  | \
+				 (1ULL << VIRTIO_BLK_F_OUT_FUA))
 
 #define VDPASIM_BLK_CAPACITY	0x40000
 #define VDPASIM_BLK_SIZE_MAX	0x1000
@@ -158,7 +159,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
 	status = VIRTIO_BLK_S_OK;
 
 	if (type != VIRTIO_BLK_T_IN && type != VIRTIO_BLK_T_OUT &&
-	    sector != 0) {
+	    type != VIRTIO_BLK_T_OUT_FUA && sector != 0) {
 		dev_dbg(&vdpasim->vdpa.dev,
 			"sector must be 0 for %u request - sector: 0x%llx\n",
 			type, sector);
@@ -191,6 +192,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
 		break;
 
 	case VIRTIO_BLK_T_OUT:
+	case VIRTIO_BLK_T_OUT_FUA:
 		if (!vdpasim_blk_check_range(vdpasim, sector,
 					     to_pull >> SECTOR_SHIFT,
 					     VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)) {
Signature · 1 line
2.49.0
reviewer Reviewed-by on v1 2/2
#6 lore raw

Re: [RFC 2/2] vdpa_sim_blk: add support for fua write commands

Quoted 7 lines · Alberto Faria

Expose VIRTIO_BLK_F_OUT_FUA to drivers. The simulator is a ramdisk, so it handles VIRTIO_BLK_T_OUT and VIRTIO_BLK_T_OUT_FUA in the same way.

---
 drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)