All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] virtio_blk: Add support for lifetime feature
@ 2021-04-16 19:47 Enrico Granata
  2021-04-17  2:42 ` kernel test robot
  2021-04-20  7:01   ` Christoph Hellwig
  0 siblings, 2 replies; 11+ messages in thread
From: Enrico Granata @ 2021-04-16 19:47 UTC (permalink / raw)
  To: mst, jasowang, pbonzini, stefanha, axboe, virtualization,
	linux-block, linux-kernel
  Cc: egranata

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.

Signed-off-by: Enrico Granata <egranata@google.com>
---
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(-)

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index b9fa3ef5b57c..31969f680a9f 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -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 = {
diff --git a/include/uapi/linux/virtio_blk.h b/include/uapi/linux/virtio_blk.h
index d888f013d9ff..bbd3978b9d08 100644
--- a/include/uapi/linux/virtio_blk.h
+++ b/include/uapi/linux/virtio_blk.h
@@ -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
-- 
2.31.1.368.gbe11c130af-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread
* Re: [PATCH v2] virtio_blk: Add support for lifetime feature
  2021-04-16 19:47 [PATCH v2] virtio_blk: Add support for lifetime feature Enrico Granata
@ 2021-04-29 10:04 ` Dan Carpenter
  2021-04-20  7:01   ` Christoph Hellwig
  1 sibling, 0 replies; 11+ messages in thread
From: kernel test robot @ 2021-04-21 15:59 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 8595 bytes --]

CC: kbuild-all(a)lists.01.org
In-Reply-To: <20210416194709.155497-1-egranata@google.com>
References: <20210416194709.155497-1-egranata@google.com>
TO: Enrico Granata <egranata@google.com>

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-rc8]
[cannot apply to vhost/linux-next next-20210421]
[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
:::::: branch date: 5 days ago
:::::: commit date: 5 days ago
config: i386-randconfig-m021-20210421 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

New smatch warnings:
drivers/block/virtio_blk.c:256 virtio_queue_rq() error: uninitialized symbol 'type'.

Old smatch warnings:
drivers/block/virtio_blk.c:258 virtio_queue_rq() error: uninitialized symbol 'type'.

vim +/type +256 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

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 33215 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-04-29 10:04 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-16 19:47 [PATCH v2] virtio_blk: Add support for lifetime feature Enrico Granata
2021-04-17  2:42 ` kernel test robot
2021-04-20  7:01 ` Christoph Hellwig
2021-04-20  7:01   ` Christoph Hellwig
2021-04-20 10:08   ` Michael S. Tsirkin
2021-04-20 10:08     ` Michael S. Tsirkin
2021-04-20 16:28     ` Enrico Granata
2021-04-20 16:41     ` Cornelia Huck
2021-04-20 16:41       ` Cornelia Huck
2021-04-21 15:59 kernel test robot
2021-04-29 10:04 ` Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.