virtio_blk: add FUA write support
Subsystems (3)
v3 cover virtio_blk: add FUA write support
virtio-blk FUA write support has been added to the virtio spec [1]. This series implements that by adding the new request flag 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]/
v2:
- Check for VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA when handling requests with REQ_FUA.
- Don't advertise FUA support on zoned devices.
- Restore BLK_FEAT_FUA in cache_type_store().
- vdpa_sim: Reject VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA without VIRTIO_BLK_F_REQ_FLAGS.
v3:
- (sashiko) virtio_blK: Check for both VIRTIO_BLK_F_REQ_FLAGS and VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA when handling REQ_FUA.
- (sashiko) vdpa_sim: Fix possible ERR_PTR(0) return.
Alberto Faria (2):
virtio_blk: add fua write support
vdpa_sim_blk: add support for FUA writes
drivers/block/virtio_blk.c | 29 ++++++++++++++++++++++++++-- drivers/vdpa/vdpa_sim/vdpa_sim.c | 15 +++++++++++++- drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 + drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 14 +++++++++++++- include/uapi/linux/virtio_blk.h | 13 +++++++++++-- 5 files changed, 66 insertions(+), 6 deletions(-)
Signature · 1 line
2.56.1
PATCH v3 1/2 virtio_blk: add fua write support
Add the new virtio-blk request FUA flag uapi definitions and corresponding virtio-blk support.
--- drivers/block/virtio_blk.c | 29 +++++++++++++++++++++++++++-- include/uapi/linux/virtio_blk.h | 13 +++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-)
drivers/block/virtio_blk.c +27 -2
@@ -248,7 +248,10 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,return BLK_STS_NOTSUPP ;/* Set fields for all request types */ - vbr->out_hdr.ioprio =cpu_to_virtio32 (vdev,req_get_ioprio (req)); +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS )) + vbr->out_hdr.flags =0 ; +else + vbr->out_hdr.ioprio =cpu_to_virtio32 (vdev,req_get_ioprio (req));switch (req_op (req)) {case REQ_OP_READ :
@@ -258,6 +261,11 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,case REQ_OP_WRITE : type =VIRTIO_BLK_T_OUT ; sector =blk_rq_pos (req); +if ((req->cmd_flags &REQ_FUA ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA )) + vbr->out_hdr.flags |=cpu_to_virtio32 (vdev, +BIT (VIRTIO_BLK_REQ_FLAG_OUT_FUA ));break ;case REQ_OP_FLUSH : type =VIRTIO_BLK_T_FLUSH ;
@@ -1151,10 +1159,17 @@ cache_type_store(struct device *dev, struct device_attribute *attr,virtio_cwrite8 (vdev, offsetof(struct virtio_blk_config , wce), i); lim =queue_limits_start_update (disk->queue); +if (virtblk_get_cache_mode (vdev)) lim.features |=BLK_FEAT_WRITE_CACHE ;else lim.features &= ~BLK_FEAT_WRITE_CACHE ; + +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + lim.features |=BLK_FEAT_FUA ; + i =queue_limits_commit_update_frozen (disk->queue, &lim);if (i)return i;
@@ -1575,6 +1590,15 @@ static int virtblk_probe(struct virtio_device *vdev)if (virtblk_get_cache_mode (vdev)) lim.features |=BLK_FEAT_WRITE_CACHE ; +/* +* Do not advertise FUA when the device is zoned, since we +* would not be able to handle it on REQ_OP_ZONE_APPEND. +*/ +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + 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);
@@ -1740,7 +1764,8 @@ 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_REQ_FLAGS , + VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA, }; static struct virtio_driver virtio_blk = {
include/uapi/linux/virtio_blk.h +11 -2
@@ -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
@@ -211,6 +213,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,
@@ -219,8 +224,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.1
Re: [PATCH v3 1/2] virtio_blk: add fua write support
drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1601
Quoted 9 lines · Alberto Faria
drivers/block/virtio_blk.c +9
-0+/* +* Do not advertise FUA when the device is zoned, since we +* would not be able to handle it on REQ_OP_ZONE_APPEND. +*/ +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + lim.features |=BLK_FEAT_FUA ; +
Can you elaborate why?
Re: [PATCH v3 1/2] virtio_blk: add fua write support
Quoted 11 lines · Johannes Thumshirn
drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1601
Quoted 9 lines · Alberto Faria
drivers/block/virtio_blk.c +9
-0+/* +* Do not advertise FUA when the device is zoned, since we +* would not be able to handle it on REQ_OP_ZONE_APPEND. +*/ +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + lim.features |=BLK_FEAT_FUA ; +
Quoted 1 line
Can you elaborate why?
The virtio spec currently only defines FUA for OUT requests, so there would be no way to pass on the REQ_FUA to the device on a zone append.
Alberto
Re: [PATCH v3 1/2] virtio_blk: add fua write support
Quoted 13 lines · Alberto Faria
Quoted 11 lines · Johannes Thumshirn
Quoted 9 lines · Alberto Faria
drivers/block/virtio_blk.c +9
-0+/* +* Do not advertise FUA when the device is zoned, since we +* would not be able to handle it on REQ_OP_ZONE_APPEND. +*/ +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + lim.features |=BLK_FEAT_FUA ; +
Quoted 4 lines
Quoted 1 line
Can you elaborate why?
The virtio spec currently only defines FUA for OUT requests, so there would be no way to pass on the
REQ_FUAto the device on a zone append.
Still very confused. What does OUT requests mean ? Is it the data transfer direction (device to host)? If yes and an OUT request indicates a "read", then why single out zone append? Why not all write commands?
If on the other hand this flags indicates that the command cdb is one way only and cannot return data (e.g. the written location that zone append returns), then OK, I understand. But the comment should clarify that.
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v3 1/2] virtio_blk: add fua write support
In reply to Alberto FariaQuoted 38 lines · Alberto Faria
Add the new virtio-blk request FUA flag uapi definitions and corresponding virtio-blk support.
Signed-off-by Alberto Faria <[email protected]>--- drivers/block/virtio_blk.c | 29 +++++++++++++++++++++++++++-- include/uapi/linux/virtio_blk.h | 13 +++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-)drivers/block/virtio_blk.c +9
-1@@ -248,7 +248,10 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,return BLK_STS_NOTSUPP ;/* Set fields for all request types */ - vbr->out_hdr.ioprio =cpu_to_virtio32 (vdev,req_get_ioprio (req)); +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS )) + vbr->out_hdr.flags =0 ; +else + vbr->out_hdr.ioprio =cpu_to_virtio32 (vdev,req_get_ioprio (req));switch (req_op (req)) {case REQ_OP_READ :@@ -258,6 +261,11 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,case REQ_OP_WRITE : type =VIRTIO_BLK_T_OUT ; sector =blk_rq_pos (req); +if ((req->cmd_flags &REQ_FUA ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA )) + vbr->out_hdr.flags |=cpu_to_virtio32 (vdev, +BIT (VIRTIO_BLK_REQ_FLAG_OUT_FUA ));break ;case REQ_OP_FLUSH : type =VIRTIO_BLK_T_FLUSH ;
drivers/block/virtio_blk.c cache_type_store(struct device *dev, struct device_attribute *attr, · line 1162
Quoted 4 lines
drivers/block/virtio_blk.c +1
-0@@ -1151,10 +1159,17 @@ cache_type_store(struct device *dev, struct device_attribute *attr,virtio_cwrite8 (vdev, offsetof(struct virtio_blk_config , wce), i); lim =queue_limits_start_update (disk->queue); +
White line change.
Quoted 14 lines
drivers/block/virtio_blk.c +6
-0if (virtblk_get_cache_mode (vdev)) lim.features |=BLK_FEAT_WRITE_CACHE ;else lim.features &= ~BLK_FEAT_WRITE_CACHE ; + +if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + lim.features |=BLK_FEAT_FUA ; + i =queue_limits_commit_update_frozen (disk->queue, &lim);if (i)return i;
drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 1596
Quoted 7 lines
drivers/block/virtio_blk.c +4
-0@@ -1575,6 +1590,15 @@ static int virtblk_probe(struct virtio_device *vdev)if (virtblk_get_cache_mode (vdev)) lim.features |=BLK_FEAT_WRITE_CACHE ; +/* +* Do not advertise FUA when the device is zoned, since we +* would not be able to handle it on REQ_OP_ZONE_APPEND. +*/
Why is that? I do not see any reason for this.
Quoted 56 lines
drivers/block/virtio_blk.c +7
-1+if (virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS ) && +virtio_has_feature (vdev,VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA ) && + !(lim.features &BLK_FEAT_ZONED )) + 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);@@ -1740,7 +1764,8 @@ 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_REQ_FLAGS , + VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA, }; static struct virtio_driver virtio_blk = {include/uapi/linux/virtio_blk.h +11
-2@@ -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 @@ -211,6 +213,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, @@ -219,8 +224,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 · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v3 1/2] virtio_blk: add fua write support
In reply to Alberto FariaQuoted 8 lines · Alberto Faria
Add the new virtio-blk request FUA flag uapi definitions and corresponding virtio-blk support.
Signed-off-by Alberto Faria <[email protected]>--- drivers/block/virtio_blk.c | 29 +++++++++++++++++++++++++++-- include/uapi/linux/virtio_blk.h | 13 +++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-)
PATCH v3 2/2 vdpa_sim_blk: add support for FUA writes
In reply to Alberto FariaExpose VIRTIO_BLK_F_REQ_FLAGS and VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA to drivers. The simulator is a ramdisk, so it handles FUA and non-FUA writes in the same way.
--- drivers/vdpa/vdpa_sim/vdpa_sim.c | 15 ++++++++++++++- drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 + drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 14 +++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +14 -1
@@ -198,7 +198,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,struct vdpa_device *vdpa;struct vdpasim *vdpasim;struct device *dev; -int i, ret = -ENOMEM ; +int i, ret = -ENOMEM , ret2;if (!dev_attr->alloc_size)return ERR_PTR (-EINVAL );
@@ -209,6 +209,12 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,if (config->device_features & ~dev_attr->supported_features)return ERR_PTR (-EINVAL ); +if (dev_attr->validate_features) { + ret2 = dev_attr->validate_features ( + config->device_features); +if (ret2) +return ERR_PTR (ret2); + } dev_attr->supported_features = config->device_features; }
@@ -442,11 +448,18 @@ static u64 vdpasim_get_backend_features(const struct vdpa_device *vdpa)static int vdpasim_set_driver_features (struct vdpa_device *vdpa,u64 features) {struct vdpasim *vdpasim =vdpa_to_sim (vdpa); +int ret;/* DMA mapping must be done by driver */ if (!(features & (1ULL <<VIRTIO_F_ACCESS_PLATFORM )))return -EINVAL ; +if (vdpasim->dev_attr.validate_features) { + ret = vdpasim->dev_attr.validate_features (features); +if (ret) +return ret; + } + vdpasim->features = features & vdpasim->dev_attr.supported_features;return 0 ;
drivers/vdpa/vdpa_sim/vdpa_sim.h +1 -0
@@ -45,6 +45,7 @@ struct vdpasim_dev_attr {u32 nas;void (*work_fn)(struct vdpasim *vdpasim); +int (*validate_features)(u64 features);void (*get_config)(struct vdpasim *vdpasim,void *config);void (*set_config)(struct vdpasim *vdpasim,const void *config); int (*get_stats)(struct vdpasim *vdpasim, u16 idx,
drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +13 -1
@@ -32,7 +32,9 @@ (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_REQ_FLAGS) | \ + (1ULL << VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA))#define VDPASIM_BLK_CAPACITY 0x40000#define VDPASIM_BLK_SIZE_MAX 0x1000
@@ -371,6 +373,15 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim)vdpasim_schedule_work (vdpasim); } +static int vdpasim_blk_validate_features (u64 features) +{ +if ((features &BIT_ULL (VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA )) && + !(features &BIT_ULL (VIRTIO_BLK_F_REQ_FLAGS ))) +return -EINVAL ; + +return 0 ; +} +static void vdpasim_blk_get_config (struct vdpasim *vdpasim,void *config) {struct virtio_blk_config *blk_config = config;
@@ -424,6 +435,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, dev_attr.nas =VDPASIM_BLK_AS_NUM ; dev_attr.alloc_size =sizeof (struct vdpasim_blk ); dev_attr.config_size =sizeof (struct virtio_blk_config ); + dev_attr.validate_features = vdpasim_blk_validate_features; dev_attr.get_config = vdpasim_blk_get_config; dev_attr.work_fn = vdpasim_blk_work; dev_attr.free = vdpasim_blk_free;
Signature · 1 line
2.56.1
Re: [PATCH v3 2/2] vdpa_sim_blk: add support for FUA writes
drivers/vdpa/vdpa_sim/vdpa_sim.h struct vdpasim_dev_attr { · line 48
Quoted 9 lines · Alberto Faria
drivers/vdpa/vdpa_sim/vdpa_sim.h +1
-0@@ -45,6 +45,7 @@ struct vdpasim_dev_attr {u32 nas;void (*work_fn)(struct vdpasim *vdpasim); +int (*validate_features)(u64 features);
This is okay for now. Possible future changes:
- It may be necessary to split validate_features() into validate_device_features() and validate_driver_features(). This is because the device can advertise all possible features even though drivers may not be allowed to enable all of them simultaneously.
- A struct vdpasim *vdpasim argument will probably be needed for checks that depend on the device itself.
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]