From nobody Sun May 19 10:01:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562171326; cv=none; d=zoho.com; s=zohoarc; b=jyD//0NEZUIsNC+WsOz76c71Ns5eMNJE5+9/CTu9xyUde0/TRBPvQ9DSTKf0WY74Ty2L0PQAHz+WIcQV5DxB3AIxQSjvHltVQa4l/tvnp/j26bwsG5rwDcZOmlCwSnSxCaOe8r24qIcVbaUuCN8RttTRt3p4v6YJRmdoISwhlww= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562171326; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=PAkz32RGbQskzSDZa/j/G6a7k3lVxZwCbZ3juuu7LsA=; b=ZvHCHOfJiWhF5HpfY4Hl+EKSVPYcox0NjjRlrW3XtBV3bqML/u1vZ6qvlKgIsFGRU30roHI+DWnWydT9JPiJXVVIq6wNkgRzgBCigQqmBwgx1z3i04LIE7w4xir/tAgYEmm28KX+yQXBqS4qt0RIWth49Tt+sORqdv5DHe9WKiY= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562171326865565.0369910324598; Wed, 3 Jul 2019 09:28:46 -0700 (PDT) Received: from localhost ([::1]:37402 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hii7f-0004YW-U6 for importer@patchew.org; Wed, 03 Jul 2019 12:28:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57223) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hihnh-0004vB-6y for qemu-devel@nongnu.org; Wed, 03 Jul 2019 12:08:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hihng-0000vA-3L for qemu-devel@nongnu.org; Wed, 03 Jul 2019 12:08:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59870) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hihnc-0000tn-QJ; Wed, 03 Jul 2019 12:08:00 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CE83083F42; Wed, 3 Jul 2019 16:07:57 +0000 (UTC) Received: from maximlenovopc.usersys.redhat.com (dhcp-4-67.tlv.redhat.com [10.35.4.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id DD323891A4; Wed, 3 Jul 2019 16:07:55 +0000 (UTC) From: Maxim Levitsky To: qemu-devel@nongnu.org Date: Wed, 3 Jul 2019 19:07:54 +0300 Message-Id: <20190703160754.12361-1-mlevitsk@redhat.com> In-Reply-To: <20190703155944.9637-7-mlevitsk@redhat.com> References: <20190703155944.9637-7-mlevitsk@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 03 Jul 2019 16:07:57 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4] block/nvme: add support for discard X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Max Reitz , Paolo Bonzini , Maxim Levitsky , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Maxim Levitsky --- block/nvme.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++ block/trace-events | 2 ++ 2 files changed, 83 insertions(+) diff --git a/block/nvme.c b/block/nvme.c index 02e0846643..96a715dcc1 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -111,6 +111,7 @@ typedef struct { bool plugged; =20 bool supports_write_zeros; + bool supports_discard; =20 CoMutex dma_map_lock; CoQueue dma_flush_queue; @@ -460,6 +461,7 @@ static void nvme_identify(BlockDriverState *bs, int nam= espace, Error **errp) s->page_size / sizeof(uint64_t) * s->page_size); =20 s->supports_write_zeros =3D (idctrl->oncs & NVME_ONCS_WRITE_ZEROS) != =3D 0; + s->supports_discard =3D (idctrl->oncs & NVME_ONCS_DSM) !=3D 0; =20 memset(resp, 0, 4096); =20 @@ -1149,6 +1151,84 @@ static coroutine_fn int nvme_co_pwrite_zeroes(BlockD= riverState *bs, } =20 =20 +static int coroutine_fn nvme_co_pdiscard(BlockDriverState *bs, + int64_t offset, + int bytes) +{ + BDRVNVMeState *s =3D bs->opaque; + NVMeQueuePair *ioq =3D s->queues[1]; + NVMeRequest *req; + NvmeDsmRange *buf; + QEMUIOVector local_qiov; + int r; + + NvmeCmd cmd =3D { + .opcode =3D NVME_CMD_DSM, + .nsid =3D cpu_to_le32(s->nsid), + .cdw10 =3D 0, /*number of ranges - 0 based*/ + .cdw11 =3D cpu_to_le32(1 << 2), /*deallocate bit*/ + }; + + NVMeCoData data =3D { + .ctx =3D bdrv_get_aio_context(bs), + .ret =3D -EINPROGRESS, + }; + + if (!s->supports_discard) { + return -ENOTSUP; + } + + assert(s->nr_queues > 1); + + buf =3D qemu_try_blockalign0(bs, 4096); + if (!buf) { + return -ENOMEM; + } + + buf->nlb =3D cpu_to_le32(bytes >> s->blkshift); + buf->slba =3D cpu_to_le64(offset >> s->blkshift); + buf->cattr =3D 0; + + qemu_iovec_init(&local_qiov, 1); + qemu_iovec_add(&local_qiov, buf, 4096); + + req =3D nvme_get_free_req(ioq); + assert(req); + + qemu_co_mutex_lock(&s->dma_map_lock); + r =3D nvme_cmd_map_qiov(bs, &cmd, req, &local_qiov); + qemu_co_mutex_unlock(&s->dma_map_lock); + + if (r) { + req->busy =3D false; + return r; + } + + trace_nvme_dsm(s, offset, bytes); + + nvme_submit_command(s, ioq, req, &cmd, nvme_rw_cb, &data); + + data.co =3D qemu_coroutine_self(); + while (data.ret =3D=3D -EINPROGRESS) { + qemu_coroutine_yield(); + } + + qemu_co_mutex_lock(&s->dma_map_lock); + r =3D nvme_cmd_unmap_qiov(bs, &local_qiov); + qemu_co_mutex_unlock(&s->dma_map_lock); + if (r) { + return r; + } + + trace_nvme_dsm_done(s, offset, bytes, data.ret); + + qemu_iovec_destroy(&local_qiov); + qemu_vfree(buf); + return data.ret; + +} + + static int nvme_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, Error **errp) { @@ -1363,6 +1443,7 @@ static BlockDriver bdrv_nvme =3D { .bdrv_co_pwritev =3D nvme_co_pwritev, =20 .bdrv_co_pwrite_zeroes =3D nvme_co_pwrite_zeroes, + .bdrv_co_pdiscard =3D nvme_co_pdiscard, =20 .bdrv_co_flush_to_disk =3D nvme_co_flush, .bdrv_reopen_prepare =3D nvme_reopen_prepare, diff --git a/block/trace-events b/block/trace-events index 12f363bb44..f763f79d99 100644 --- a/block/trace-events +++ b/block/trace-events @@ -152,6 +152,8 @@ nvme_write_zeros(void *s, uint64_t offset, uint64_t byt= es, int flags) "s %p offs nvme_qiov_unaligned(const void *qiov, int n, void *base, size_t size, int = align) "qiov %p n %d base %p size 0x%zx align 0x%x" nvme_prw_buffered(void *s, uint64_t offset, uint64_t bytes, int niov, int = is_write) "s %p offset %"PRId64" bytes %"PRId64" niov %d is_write %d" nvme_rw_done(void *s, int is_write, uint64_t offset, uint64_t bytes, int r= et) "s %p is_write %d offset %"PRId64" bytes %"PRId64" ret %d" +nvme_dsm(void *s, uint64_t offset, uint64_t bytes) "s %p offset %"PRId64" = bytes %"PRId64"" +nvme_dsm_done(void *s, uint64_t offset, uint64_t bytes, int ret) "s %p off= set %"PRId64" bytes %"PRId64" ret %d" nvme_dma_map_flush(void *s) "s %p" nvme_free_req_queue_wait(void *q) "q %p" nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) = "s %p cmd %p req %p qiov %p entries %d" --=20 2.17.2