virtio_blk: add FUA write support
Subsystems (3)
v2 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.
Alberto Faria (2):
virtio_blk: add fua write support
vdpa_sim_blk: add support for FUA writes
drivers/block/virtio_blk.c | 28 ++++++++++++++++++++++++++-- drivers/vdpa/vdpa_sim/vdpa_sim.c | 13 +++++++++++++ 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, 64 insertions(+), 5 deletions(-)
Signature · 1 line
2.56.1
PATCH v2 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 | 28 ++++++++++++++++++++++++++-- include/uapi/linux/virtio_blk.h | 13 +++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-)
drivers/block/virtio_blk.c +26 -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,10 @@ 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_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 +1158,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 +1589,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 +1763,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
[email protected]
AI reviewRe: [PATCH v2 1/2] virtio_blk: add fua write support
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] virtio_blk: missing VIRTIO_BLK_F_REQ_FLAGS check in virtblk_setup_cmd() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio_blk: missing VIRTIO_BLK_F_REQ_FLAGS check in virtblk_setup_cmd()
--
commit f7bfe54853bedaa1afa630f98325bb99ee3bb7cb
Author: Alberto Faria <[email protected]>
virtio_blk: add fua write support
This commit adds UAPI definitions for virtio-blk request flags and VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA, and teaches virtio_blk to advertise and handle REQ_FUA writes.
Quoted 17 lines
drivers/block/virtio_blk.c +4
-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 :
drivers/block/virtio_blk.c static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev, · line 268
Quoted 8 lines
drivers/block/virtio_blk.c +4
-0@@ -258,6 +261,10 @@ 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_OUT_FUA )) + vbr->out_hdr.flags |=cpu_to_virtio32 (vdev, +BIT (VIRTIO_BLK_REQ_FLAG_OUT_FUA ));break ;
[Severity: Medium]
Should REQ_OP_WRITE also check VIRTIO_BLK_F_REQ_FLAGS before touching vbr->out_hdr.flags, or should virtio_blk clear VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA in a .validate callback when VIRTIO_BLK_F_REQ_FLAGS is not offered?
Unlike virtblk_probe() and cache_type_store(), which check both features, virtblk_setup_cmd() only checks VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA here. If VIRTIO_BLK_F_REQ_FLAGS is not negotiated (for example, when filtered out via debugfs_filter_features), virtio_blk accepts VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA without its prerequisite feature and can fail feature negotiation with spec-compliant devices, or overwrite vbr->out_hdr.ioprio in the union if a REQ_FUA write reaches the queue.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 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 | 13 +++++++++++++ drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 + drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 14 +++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +13 -0
@@ -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) { + ret = dev_attr->validate_features ( + config->device_features); +if (ret) +return ERR_PTR (ret); + } 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
[email protected]
AI reviewRe: [PATCH v2 2/2] vdpa_sim_blk: add support for FUA writes
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] vdpa_sim: vdpasim_create() clobbers ret with 0, returning NULL on error --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] vdpa_sim: vdpasim_create() clobbers ret with 0, returning NULL on error
--
commit 44caf7459577594a98fabfacf52e1c5c4e5f2cf1
Author: Alberto Faria <[email protected]>
vdpa_sim_blk: add support for FUA writes
This commit adds a validate_features callback to vdpasim_dev_attr and exposes VIRTIO_BLK_F_REQ_FLAGS and VIRTIO_BLK_F_REQ_FLAGS_OUT_FUA in vdpa_sim_blk.
Quoted 7 lines
drivers/vdpa/vdpa_sim/vdpa_sim.c +0
-0@@ -209,6 +209,12 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,if (config->device_features & ~dev_attr->supported_features)
drivers/vdpa/vdpa_sim/vdpa_sim.c struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, · line 220
Quoted 10 lines
drivers/vdpa/vdpa_sim/vdpa_sim.c +6
-0return ERR_PTR (-EINVAL ); +if (dev_attr->validate_features) { + ret = dev_attr->validate_features ( + config->device_features); +if (ret) +return ERR_PTR (ret); + } dev_attr->supported_features = config->device_features; }
[Severity: Medium]
Can assigning the return value of dev_attr->validate_features() to ret cause vdpasim_create() to return NULL instead of an error pointer on later failures?
ret is initialized to -ENOMEM at the start of vdpasim_create() for the allocation and dma_set_mask_and_coherent() failure paths that jump to err_iommu without setting ret. When validate_features() succeeds, it overwrites ret with 0, so a later allocation failure frees dev and returns ERR_PTR(0) (NULL), causing callers such as vdpasim_blk_dev_add() that only check IS_ERR(simdev) to dereference NULL.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2