From patchwork Mon Dec 5 16:20:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alvaro Karsz X-Patchwork-Id: 13067150 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B0A07C352A1 for ; Wed, 7 Dec 2022 13:38:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670420282; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=7KdLGTrNCIH4Eolwc0NFNRFYzLIiDzcqzTsPfF0E/P8=; b=CoRZbA/zvJVXViBxupvljOJYTx1LAdQFOzI8NKztqLFV5Ba7gRdbfdl4tkxvn2S7nQEHKH 5J0KHHCK67ginvA7Zyj3UkETAbvAxA+PsAR8d0tUvWkPEBzn465rpqsdknfyBWjoGaJHqj NVMMyXY24/ycTOXp1ATDK7CN/WMzOIg= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-93-mNwurCFxMlCHqXM7VFOiew-1; Wed, 07 Dec 2022 08:37:25 -0500 X-MC-Unique: mNwurCFxMlCHqXM7VFOiew-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B8DB71C06929; Wed, 7 Dec 2022 13:37:20 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id A6DCD4A9254; Wed, 7 Dec 2022 13:37:20 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 98D2919465B2; Wed, 7 Dec 2022 13:37:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 0DFE31946589 for ; Mon, 5 Dec 2022 16:20:42 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id EDBFA2024CBB; Mon, 5 Dec 2022 16:20:41 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E428D2028CE4 for ; Mon, 5 Dec 2022 16:20:41 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BF79A101A528 for ; Mon, 5 Dec 2022 16:20:41 +0000 (UTC) Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-659-66NJcUCtNEuL7VSXpXQWJQ-1; Mon, 05 Dec 2022 11:20:40 -0500 X-MC-Unique: 66NJcUCtNEuL7VSXpXQWJQ-1 Received: by mail-wr1-f41.google.com with SMTP id y16so19411926wrm.2 for ; Mon, 05 Dec 2022 08:20:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ficp9YnxtYUVosXmAWDNlYqnWO69Vk0MG7GW8V0nNo4=; b=Nbn/QsAatr6GMcqspAnEBPqFBNWOQEs9HiJkcxYNrjmAi+CC3/uyDhd4LfUJV2safS ref2J419CGlcye+vqchHNMFrDw43NVajBJtpzEKZ1Xf/ek2apauEN5u9K7ktbxXRdFBz xS6KdrLF/f+OqhoKA3wvobjDFLOzDQ7F09/OZ4HCJDizdBHw6+YT1McUfjHmMVQ+0y5q dFREt/W0wITx5ai5aN9XM9xqJ5AbAfIVixQFifSdQVnBAIqwsgMCwZ01ASSh3Sow2nKr h6JAGww9Z6wQIjVeO2d1vJ2Pr+Mw0AzTUI/sMhiuM4qO3LDvtoq8+QPgseldUJM3uGdK xZPA== X-Gm-Message-State: ANoB5plt4LuJR7MgZgA1lFfFhRH5XzJ9HZY2YpGhD2YFdiH4g55zLyTL /GY5tOLLMfUd6BFMRRPlU5r9xQ== X-Google-Smtp-Source: AA0mqf49u7SktyNNs0t8+g6HVZk+8T/Ivq5svMIZrXvPl798MUnRShoJMB1JcN8K1mqX6Fote2xpBQ== X-Received: by 2002:a5d:5957:0:b0:22e:3e9:ba15 with SMTP id e23-20020a5d5957000000b0022e03e9ba15mr48747236wri.180.1670257238839; Mon, 05 Dec 2022 08:20:38 -0800 (PST) Received: from localhost.localdomain (bzq-84-110-153-254.static-ip.bezeqint.net. [84.110.153.254]) by smtp.gmail.com with ESMTPSA id bg9-20020a05600c3c8900b003cf483ee8e0sm23775783wmb.24.2022.12.05.08.20.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 08:20:38 -0800 (PST) From: Alvaro Karsz To: virtualization@lists.linux-foundation.org Date: Mon, 5 Dec 2022 18:20:34 +0200 Message-Id: <20221205162035.2261037-1-alvaro.karsz@solid-run.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mailman-Approved-At: Wed, 07 Dec 2022 13:37:16 +0000 Subject: [dm-devel] [PATCH v3] virtio_blk: add VIRTIO_BLK_F_LIFETIME feature support X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jens Axboe , linux-scsi@vger.kernel.org, "Michael S. Tsirkin" , Alvaro Karsz , Jason Wang , linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, dm-devel@redhat.com, Stefan Hajnoczi , Paolo Bonzini Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Implement the VIRTIO_BLK_F_LIFETIME feature for VirtIO block devices. This commit introduces a new ioctl command, VBLK_LIFETIME. VBLK_LIFETIME ioctl asks for the block device to provide lifetime information by sending a VIRTIO_BLK_T_GET_LIFETIME command to the device. lifetime information fields: - pre_eol_info: specifies the percentage of reserved blocks that are consumed. optional values following virtio spec: *) 0 - undefined. *) 1 - normal, < 80% of reserved blocks are consumed. *) 2 - warning, 80% of reserved blocks are consumed. *) 3 - urgent, 90% of reserved blocks are consumed. - device_lifetime_est_typ_a: this field refers to wear of SLC cells and is provided in increments of 10used, and so on, thru to 11 meaning estimated lifetime exceeded. All values above 11 are reserved. - device_lifetime_est_typ_b: this field refers to wear of MLC cells and is provided with the same semantics as device_lifetime_est_typ_a. The data received from the device will be sent as is to the user. No data check/decode is done by virtblk. Signed-off-by: Alvaro Karsz --- v2: - Remove (void *) casting. - Fix comments format in virtio_blk.h. - Set ioprio value for legacy devices for REQ_OP_DRV_IN operations. v3: - Initialize struct virtio_blk_lifetime to 0 instead of memset. - Rename ioctl from VBLK_LIFETIME to VBLK_GET_LIFETIME. - Return EOPNOTSUPP insted of ENOTTY if ioctl is not supported. - Check if process is CAP_SYS_ADMIN capable in lifetime ioctl. - Check if vdev is not NULL before accessing it in lifetime ioctl. -- --- drivers/block/virtio_blk.c | 106 ++++++++++++++++++++++++++++++-- include/uapi/linux/virtio_blk.h | 32 ++++++++++ 2 files changed, 133 insertions(+), 5 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index 19da5defd73..392d57fd6b7 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -101,6 +101,18 @@ static inline blk_status_t virtblk_result(struct virtblk_req *vbr) } } +static inline int virtblk_ioctl_result(struct virtblk_req *vbr) +{ + switch (vbr->status) { + case VIRTIO_BLK_S_OK: + return 0; + case VIRTIO_BLK_S_UNSUPP: + return -EOPNOTSUPP; + default: + return -EIO; + } +} + static inline struct virtio_blk_vq *get_virtio_blk_vq(struct blk_mq_hw_ctx *hctx) { struct virtio_blk *vblk = hctx->queue->queuedata; @@ -218,6 +230,7 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev, u32 type; vbr->out_hdr.sector = 0; + vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req)); switch (req_op(req)) { case REQ_OP_READ: @@ -244,15 +257,14 @@ static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev, type = VIRTIO_BLK_T_SECURE_ERASE; break; case REQ_OP_DRV_IN: - type = VIRTIO_BLK_T_GET_ID; - break; + /* type is set in virtblk_get_id/virtblk_ioctl_lifetime */ + return 0; default: WARN_ON_ONCE(1); return BLK_STS_IOERR; } vbr->out_hdr.type = cpu_to_virtio32(vdev, type); - vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req)); if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES || type == VIRTIO_BLK_T_SECURE_ERASE) { @@ -459,12 +471,16 @@ 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_mq_alloc_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) goto out; @@ -508,6 +524,85 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) return ret; } +/* Get lifetime information from device */ +static int virtblk_ioctl_lifetime(struct virtio_blk *vblk, unsigned long arg) +{ + struct request_queue *q = vblk->disk->queue; + struct request *req = NULL; + struct virtblk_req *vbr; + struct virtio_blk_lifetime lifetime = {}; + int ret; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + /* The virtio_blk_lifetime struct fields follow virtio spec. + * There is no check/decode on values received from the device. + * The data is sent as is to the user. + */ + + /* This ioctl is allowed only if VIRTIO_BLK_F_LIFETIME + * feature is negotiated. + */ + if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_LIFETIME)) + return -EOPNOTSUPP; + + req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0); + if (IS_ERR(req)) + return PTR_ERR(req); + + /* Write the correct type */ + vbr = blk_mq_rq_to_pdu(req); + vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_LIFETIME); + + ret = blk_rq_map_kern(q, req, &lifetime, sizeof(lifetime), GFP_KERNEL); + if (ret) + goto out; + + blk_execute_rq(req, false); + + ret = virtblk_ioctl_result(blk_mq_rq_to_pdu(req)); + if (ret) + goto out; + + /* Pass the data to the user */ + if (copy_to_user((void __user *)arg, &lifetime, sizeof(lifetime))) { + ret = -EFAULT; + goto out; + } + +out: + blk_mq_free_request(req); + return ret; +} + +static int virtblk_ioctl(struct block_device *bd, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + struct virtio_blk *vblk = bd->bd_disk->private_data; + int ret; + + mutex_lock(&vblk->vdev_mutex); + + if (!vblk->vdev) { + ret = -ENXIO; + goto exit; + } + + switch (cmd) { + case VBLK_GET_LIFETIME: + ret = virtblk_ioctl_lifetime(vblk, arg); + break; + default: + ret = -EOPNOTSUPP; + break; + } + +exit: + mutex_unlock(&vblk->vdev_mutex); + return ret; +} + static void virtblk_free_disk(struct gendisk *disk) { struct virtio_blk *vblk = disk->private_data; @@ -520,6 +615,7 @@ static void virtblk_free_disk(struct gendisk *disk) static const struct block_device_operations virtblk_fops = { .owner = THIS_MODULE, .getgeo = virtblk_getgeo, + .ioctl = virtblk_ioctl, .free_disk = virtblk_free_disk, }; @@ -1239,7 +1335,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_LIFETIME, } ; static unsigned int features[] = { @@ -1247,7 +1343,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_SECURE_ERASE, 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 58e70b24b50..e755d62d2ea 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 /* Storage lifetime information is supported */ #define VIRTIO_BLK_F_SECURE_ERASE 16 /* Secure Erase is supported */ /* Legacy feature bits */ @@ -165,6 +166,9 @@ struct virtio_blk_config { /* Get device ID command */ #define VIRTIO_BLK_T_GET_ID 8 +/* Get lifetime information command */ +#define VIRTIO_BLK_T_GET_LIFETIME 10 + /* Discard command */ #define VIRTIO_BLK_T_DISCARD 11 @@ -206,6 +210,30 @@ struct virtio_blk_discard_write_zeroes { __le32 flags; }; +/* Get lifetime information struct for each request */ +struct virtio_blk_lifetime { + /* + * specifies the percentage of reserved blocks that are consumed. + * optional values following virtio spec: + * 0 - undefined + * 1 - normal, < 80% of reserved blocks are consumed + * 2 - warning, 80% of reserved blocks are consumed + * 3 - urgent, 90% of reserved blocks are consumed + */ + __le16 pre_eol_info; + /* + * this field refers to wear of SLC cells and is provided in increments of 10used, + * and so on, thru to 11 meaning estimated lifetime exceeded. All values above 11 + * are reserved + */ + __le16 device_lifetime_est_typ_a; + /* + * this field refers to wear of MLC cells and is provided with the same semantics as + * device_lifetime_est_typ_a + */ + __le16 device_lifetime_est_typ_b; +}; + #ifndef VIRTIO_BLK_NO_LEGACY struct virtio_scsi_inhdr { __virtio32 errors; @@ -219,4 +247,8 @@ struct virtio_scsi_inhdr { #define VIRTIO_BLK_S_OK 0 #define VIRTIO_BLK_S_IOERR 1 #define VIRTIO_BLK_S_UNSUPP 2 + +/* Virtblk ioctl commands */ +#define VBLK_GET_LIFETIME _IOR('r', 0, struct virtio_blk_lifetime) + #endif /* _LINUX_VIRTIO_BLK_H */