Skip to contentSkip to conversation

virtio_blk: Add support for lifetime feature

PATCH In review Enrico Granata 5 participants 6 messages Apr 16, 2021 – Apr 20, 2021

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

PATCH v2 1/1 virtio_blk: Add support for lifetime feature

The VirtIO TC has adopted a new feature in virtio-blk enabling discovery of lifetime information.

This commit adds support for the VIRTIO_BLK_T_LIFETIME command to the virtio_blk driver, and adds two new attributes to the sysfs entry for virtio_blk:
* pre_eol_info
* life_time

which are defined in the same manner as the files of the same name for the eMMC driver, in line with the VirtIO specification.

---
Changes in v2:
- Removed redudnant buffer size checks
- Renamed variables for consistency
- Cleaned up endianness

 drivers/block/virtio_blk.c      | 75 +++++++++++++++++++++++++++++++--
 include/uapi/linux/virtio_blk.h | 11 +++++
 2 files changed, 83 insertions(+), 3 deletions(-)
drivers/block/virtio_blk.c +72 -3
index b9fa3ef5b57c..31969f680a9f 100644
@@ -246,14 +246,15 @@ static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
 		unmap = !(req->cmd_flags & REQ_NOUNMAP);
 		break;
 	case REQ_OP_DRV_IN:
-		type = VIRTIO_BLK_T_GET_ID;
-		break;
+		break; /* type already set for custom requests */
 	default:
 		WARN_ON_ONCE(1);
 		return BLK_STS_IOERR;
 	}
 
-	vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, type);
+	if (req_op(req) != REQ_OP_DRV_IN)
+		vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, type);
+
 	vbr->out_hdr.sector = type ?
 		0 : cpu_to_virtio64(vblk->vdev, blk_rq_pos(req));
 	vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
@@ -310,11 +311,14 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
 	struct virtio_blk *vblk = disk->private_data;
 	struct request_queue *q = vblk->disk->queue;
 	struct request *req;
+	struct virtblk_req *vbr;
 	int err;
 
 	req = blk_get_request(q, REQ_OP_DRV_IN, 0);
 	if (IS_ERR(req))
 		return PTR_ERR(req);
+	vbr = blk_mq_rq_to_pdu(req);
+	vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_ID);
 
 	err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
 	if (err)
@@ -327,6 +331,34 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
 	return err;
 }
 
+static int virtblk_get_lifetime(struct gendisk *disk, struct virtio_blk_lifetime *lifetime)
+{
+	struct virtio_blk *vblk = disk->private_data;
+	struct request_queue *q = vblk->disk->queue;
+	struct request *req;
+	struct virtblk_req *vbr;
+	int err;
+
+	if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_LIFETIME))
+		return -EOPNOTSUPP;
+
+	req = blk_get_request(q, REQ_OP_DRV_IN, 0);
+	if (IS_ERR(req))
+		return PTR_ERR(req);
+	vbr = blk_mq_rq_to_pdu(req);
+	vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_LIFETIME);
+
+	err = blk_rq_map_kern(q, req, lifetime, sizeof(*lifetime), GFP_KERNEL);
+	if (err)
+		goto out;
+
+	blk_execute_rq(vblk->disk, req, false);
+	err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
+out:
+	blk_put_request(req);
+	return err;
+}
+
 static void virtblk_get(struct virtio_blk *vblk)
 {
 	refcount_inc(&vblk->refs);
@@ -435,6 +467,40 @@ static ssize_t serial_show(struct device *dev,
 
 static DEVICE_ATTR_RO(serial);
 
+static ssize_t pre_eol_info_show(struct device *dev,
+			   struct device_attribute *attr, char *buf)
+{
+	struct gendisk *disk = dev_to_disk(dev);
+	struct virtio_blk_lifetime lft;
+	int err;
+
+	err = virtblk_get_lifetime(disk, &lft);
+	if (err)
+		return 0;
+
+	return sprintf(buf, "0x%02x\n", le16_to_cpu(lft.pre_eol_info));
+}
+
+static DEVICE_ATTR_RO(pre_eol_info);
+
+static ssize_t life_time_show(struct device *dev,
+			   struct device_attribute *attr, char *buf)
+{
+	struct gendisk *disk = dev_to_disk(dev);
+	struct virtio_blk_lifetime lft;
+	int err;
+
+	err = virtblk_get_lifetime(disk, &lft);
+	if (err)
+		return 0;
+
+	return sprintf(buf, "0x%02x 0x%02x\n",
+			le16_to_cpu(lft.device_life_time_est_typ_a),
+			le16_to_cpu(lft.device_life_time_est_typ_b));
+}
+
+static DEVICE_ATTR_RO(life_time);
+
 /* The queue's logical block size must be set before calling this */
 static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
 {
@@ -638,6 +704,8 @@ static DEVICE_ATTR_RW(cache_type);
 
 static struct attribute *virtblk_attrs[] = {
 	&dev_attr_serial.attr,
+	&dev_attr_pre_eol_info.attr,
+	&dev_attr_life_time.attr,
 	&dev_attr_cache_type.attr,
 	NULL,
 };
@@ -984,6 +1052,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_LIFETIME,
 };
 
 static struct virtio_driver virtio_blk = {
include/uapi/linux/virtio_blk.h +11 -0
index d888f013d9ff..bbd3978b9d08 100644
@@ -40,6 +40,7 @@
 #define VIRTIO_BLK_F_MQ		12	/* support more than one vq */
 #define VIRTIO_BLK_F_DISCARD	13	/* DISCARD is supported */
 #define VIRTIO_BLK_F_WRITE_ZEROES	14	/* WRITE ZEROES is supported */
+#define VIRTIO_BLK_F_LIFETIME	15 /* LIFETIME is supported */
 
 /* Legacy feature bits */
 #ifndef VIRTIO_BLK_NO_LEGACY
@@ -149,6 +150,9 @@ struct virtio_blk_config {
 /* Get device ID command */
 #define VIRTIO_BLK_T_GET_ID    8
 
+/* Get device lifetime command */
+#define VIRTIO_BLK_T_GET_LIFETIME 10
+
 /* Discard command */
 #define VIRTIO_BLK_T_DISCARD	11
 
@@ -196,6 +200,13 @@ struct virtio_scsi_inhdr {
 };
 #endif /* !VIRTIO_BLK_NO_LEGACY */
 
+/* Lifetime information for virtio_blk device */
+struct virtio_blk_lifetime {
+	__le16 pre_eol_info;
+	__le16 device_life_time_est_typ_a;
+	__le16 device_life_time_est_typ_b;
+};
+
 /* And this is the final byte of the write scatter-gather list. */
 #define VIRTIO_BLK_S_OK		0
 #define VIRTIO_BLK_S_IOERR	1
Signature · 1 line
2.31.1.368.gbe11c130af-goog
#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Enrico,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on block/for-next]
[also build test WARNING on linus/master v5.12-rc7]
[cannot apply to vhost/linux-next next-20210416]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Enrico-Granata/virtio_blk-Add-support-for-lifetime-feature/20210417-034754
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: x86_64-randconfig-a004-20210416 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 6a18cc23efad410db48a3ccfc233d215de7d4cb9)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/fc90f60f9bc3b5165dc34acaabc80559e1fbcb5e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Enrico-Granata/virtio_blk-Add-support-for-lifetime-feature/20210417-034754 git checkout fc90f60f9bc3b5165dc34acaabc80559e1fbcb5e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/block/virtio_blk.c:248:7: warning: variable 'type' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]
           case REQ_OP_DRV_IN:
                ^~~~~~~~~~~~~
   drivers/block/virtio_blk.c:258:24: note: uninitialized use occurs here
           vbr->out_hdr.sector = type ?
                                 ^~~~
   drivers/block/virtio_blk.c:229:10: note: initialize the variable 'type' to silence this warning
           u32 type;
                   ^
                    = 0

1 warning generated.

vim +/type +248 drivers/block/virtio_blk.c

944e7c87967c82 Jens Axboe         2018-11-26  216  
fc17b6534eb839 Christoph Hellwig  2017-06-03  217  static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
74c450521dd8d2 Jens Axboe         2014-10-29  218  			   const struct blk_mq_queue_data *bd)
e467cde238184d Rusty Russell      2007-10-22  219  {
1cf7e9c68fe842 Jens Axboe         2013-11-01  220  	struct virtio_blk *vblk = hctx->queue->queuedata;
74c450521dd8d2 Jens Axboe         2014-10-29  221  	struct request *req = bd->rq;
9d74e25737d73e Christoph Hellwig  2014-04-14  222  	struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
1cf7e9c68fe842 Jens Axboe         2013-11-01  223  	unsigned long flags;
20af3cfd20145f Paolo Bonzini      2013-03-20  224  	unsigned int num;
6a27b656fc0210 Ming Lei           2014-06-26  225  	int qid = hctx->queue_num;
5261b85e586afe Rusty Russell      2014-03-13  226  	int err;
e8edca6f7f9223 Ming Lei           2014-05-30  227  	bool notify = false;
1f23816b8eb8fd Changpeng Liu      2018-11-01  228  	bool unmap = false;
aebf526b53aea1 Christoph Hellwig  2017-01-31  229  	u32 type;
e467cde238184d Rusty Russell      2007-10-22  230  
1cf7e9c68fe842 Jens Axboe         2013-11-01  231  	BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
e467cde238184d Rusty Russell      2007-10-22  232  
aebf526b53aea1 Christoph Hellwig  2017-01-31  233  	switch (req_op(req)) {
aebf526b53aea1 Christoph Hellwig  2017-01-31  234  	case REQ_OP_READ:
aebf526b53aea1 Christoph Hellwig  2017-01-31  235  	case REQ_OP_WRITE:
aebf526b53aea1 Christoph Hellwig  2017-01-31  236  		type = 0;
f1b0ef06260271 Christoph Hellwig  2009-09-17  237  		break;
aebf526b53aea1 Christoph Hellwig  2017-01-31  238  	case REQ_OP_FLUSH:
aebf526b53aea1 Christoph Hellwig  2017-01-31  239  		type = VIRTIO_BLK_T_FLUSH;
f1b0ef06260271 Christoph Hellwig  2009-09-17  240  		break;
1f23816b8eb8fd Changpeng Liu      2018-11-01  241  	case REQ_OP_DISCARD:
1f23816b8eb8fd Changpeng Liu      2018-11-01  242  		type = VIRTIO_BLK_T_DISCARD;
1f23816b8eb8fd Changpeng Liu      2018-11-01  243  		break;
1f23816b8eb8fd Changpeng Liu      2018-11-01  244  	case REQ_OP_WRITE_ZEROES:
1f23816b8eb8fd Changpeng Liu      2018-11-01  245  		type = VIRTIO_BLK_T_WRITE_ZEROES;
1f23816b8eb8fd Changpeng Liu      2018-11-01  246  		unmap = !(req->cmd_flags & REQ_NOUNMAP);
1f23816b8eb8fd Changpeng Liu      2018-11-01  247  		break;
aebf526b53aea1 Christoph Hellwig  2017-01-31 @248  	case REQ_OP_DRV_IN:
fc90f60f9bc3b5 Enrico Granata     2021-04-16  249  		break; /* type already set for custom requests */
f1b0ef06260271 Christoph Hellwig  2009-09-17  250  	default:
aebf526b53aea1 Christoph Hellwig  2017-01-31  251  		WARN_ON_ONCE(1);
fc17b6534eb839 Christoph Hellwig  2017-06-03  252  		return BLK_STS_IOERR;
dd40e456a40ebb FUJITA Tomonori    2010-07-03  253  	}
e467cde238184d Rusty Russell      2007-10-22  254  
fc90f60f9bc3b5 Enrico Granata     2021-04-16  255  	if (req_op(req) != REQ_OP_DRV_IN)
aebf526b53aea1 Christoph Hellwig  2017-01-31  256  		vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, type);
fc90f60f9bc3b5 Enrico Granata     2021-04-16  257  
aebf526b53aea1 Christoph Hellwig  2017-01-31  258  	vbr->out_hdr.sector = type ?
aebf526b53aea1 Christoph Hellwig  2017-01-31  259  		0 : cpu_to_virtio64(vblk->vdev, blk_rq_pos(req));
aebf526b53aea1 Christoph Hellwig  2017-01-31  260  	vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
aebf526b53aea1 Christoph Hellwig  2017-01-31  261  
e2490073cd7c3d Christoph Hellwig  2014-09-13  262  	blk_mq_start_request(req);
e2490073cd7c3d Christoph Hellwig  2014-09-13  263  
1f23816b8eb8fd Changpeng Liu      2018-11-01  264  	if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
1f23816b8eb8fd Changpeng Liu      2018-11-01  265  		err = virtblk_setup_discard_write_zeroes(req, unmap);
1f23816b8eb8fd Changpeng Liu      2018-11-01  266  		if (err)
1f23816b8eb8fd Changpeng Liu      2018-11-01  267  			return BLK_STS_RESOURCE;
1f23816b8eb8fd Changpeng Liu      2018-11-01  268  	}
1f23816b8eb8fd Changpeng Liu      2018-11-01  269  
85dada09eeb31c Christoph Hellwig  2017-01-28  270  	num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
1cde26f928863d Hannes Reinecke    2009-05-18  271  	if (num) {
85dada09eeb31c Christoph Hellwig  2017-01-28  272  		if (rq_data_dir(req) == WRITE)
19c1c5a64c3b8e Michael S. Tsirkin 2014-10-07  273  			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
20af3cfd20145f Paolo Bonzini      2013-03-20  274  		else
19c1c5a64c3b8e Michael S. Tsirkin 2014-10-07  275  			vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_IN);
e467cde238184d Rusty Russell      2007-10-22  276  	}
e467cde238184d Rusty Russell      2007-10-22  277  
6a27b656fc0210 Ming Lei           2014-06-26  278  	spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
97b50a654d5de5 Christoph Hellwig  2017-01-28  279  	err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg, num);
5261b85e586afe Rusty Russell      2014-03-13  280  	if (err) {
6a27b656fc0210 Ming Lei           2014-06-26  281  		virtqueue_kick(vblk->vqs[qid].vq);
f5f6b95c72f7f8 Halil Pasic        2020-02-13  282  		/* Don't stop the queue if -ENOMEM: we may have failed to
f5f6b95c72f7f8 Halil Pasic        2020-02-13  283  		 * bounce the buffer due to global resource outage.
f5f6b95c72f7f8 Halil Pasic        2020-02-13  284  		 */
f5f6b95c72f7f8 Halil Pasic        2020-02-13  285  		if (err == -ENOSPC)
1cf7e9c68fe842 Jens Axboe         2013-11-01  286  			blk_mq_stop_hw_queue(hctx);
6a27b656fc0210 Ming Lei           2014-06-26  287  		spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
3d973b2e9a6259 Halil Pasic        2020-02-13  288  		switch (err) {
3d973b2e9a6259 Halil Pasic        2020-02-13  289  		case -ENOSPC:
86ff7c2a80cd35 Ming Lei           2018-01-30  290  			return BLK_STS_DEV_RESOURCE;
3d973b2e9a6259 Halil Pasic        2020-02-13  291  		case -ENOMEM:
3d973b2e9a6259 Halil Pasic        2020-02-13  292  			return BLK_STS_RESOURCE;
3d973b2e9a6259 Halil Pasic        2020-02-13  293  		default:
fc17b6534eb839 Christoph Hellwig  2017-06-03  294  			return BLK_STS_IOERR;
e467cde238184d Rusty Russell      2007-10-22  295  		}
3d973b2e9a6259 Halil Pasic        2020-02-13  296  	}
e467cde238184d Rusty Russell      2007-10-22  297  
74c450521dd8d2 Jens Axboe         2014-10-29  298  	if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
e8edca6f7f9223 Ming Lei           2014-05-30  299  		notify = true;
6a27b656fc0210 Ming Lei           2014-06-26  300  	spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
e8edca6f7f9223 Ming Lei           2014-05-30  301  
e8edca6f7f9223 Ming Lei           2014-05-30  302  	if (notify)
6a27b656fc0210 Ming Lei           2014-06-26  303  		virtqueue_notify(vblk->vqs[qid].vq);
fc17b6534eb839 Christoph Hellwig  2017-06-03  304  	return BLK_STS_OK;
a98755c559e0e9 Asias He           2012-08-08  305  }
a98755c559e0e9 Asias He           2012-08-08  306  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#2 lore raw
In reply to Enrico Granata

Just to despit my 2 cents again: I think the way this is specified in the virtio spec is actively harmful and we should not suport it in Linux.

If others override me we at least need to require a detailed documentation of these fields as the virto spec does not provide it.

Please also do not add pointless over 80 character lines, and follow the one value per sysfs file rule.

#3 lore raw
Quoted 9 lines · Christoph Hellwig

Just to despit my 2 cents again: I think the way this is specified in the virtio spec is actively harmful and we should not suport it in Linux.

If others override me we at least need to require a detailed documentation of these fields as the virto spec does not provide it.

Please also do not add pointless over 80 character lines, and follow the one value per sysfs file rule.

Enrico would you like to raise the issues with the virtio TC for resolution?

Signature · 1 line
MST
#4 lore raw

I prepared a proposed patch to address these concerns:
https://lists.oasis-open.org/archives/virtio-dev/202104/msg00007.html

Feedback will be much appreciated

Thanks,
- Enrico

Quoted 18 lines · Michael S. Tsirkin
Quoted 9 lines · Christoph Hellwig

Just to despit my 2 cents again: I think the way this is specified in the virtio spec is actively harmful and we should not suport it in Linux.

If others override me we at least need to require a detailed documentation of these fields as the virto spec does not provide it.

Please also do not add pointless over 80 character lines, and follow the one value per sysfs file rule.

Enrico would you like to raise the issues with the virtio TC for resolution?

--
MST

#5 lore raw
In reply to Michael S. Tsirkin
Quoted 5 lines · On Tue, 20 Apr 2021 06:08:29 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 4 lines · Christoph Hellwig

Just to despit my 2 cents again: I think the way this is specified in the virtio spec is actively harmful and we should not suport it in Linux.

Quoted 9 lines
Quoted 5 lines

If others override me we at least need to require a detailed documentation of these fields as the virto spec does not provide it.

Please also do not add pointless over 80 character lines, and follow the one value per sysfs file rule.

Enrico would you like to raise the issues with the virtio TC for resolution?

FWIW, I've opened https://github.com/oasis-tcs/virtio-spec/issues/106
to track this.