From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A892C43381 for ; Mon, 25 Mar 2019 10:07:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6F96620811 for ; Mon, 25 Mar 2019 10:07:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730463AbfCYKHb (ORCPT ); Mon, 25 Mar 2019 06:07:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53488 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729935AbfCYKHb (ORCPT ); Mon, 25 Mar 2019 06:07:31 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 10504356F2; Mon, 25 Mar 2019 10:07:29 +0000 (UTC) Received: from localhost (ovpn-8-18.pek2.redhat.com [10.72.8.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id CB7DC600CD; Mon, 25 Mar 2019 10:07:22 +0000 (UTC) From: Ming Lei To: linux-nvme@lists.infradead.org, Christoph Hellwig Cc: Ming Lei , Yi Zhang , Sagi Grimberg , Chaitanya Kulkarni , stable@vger.kernel.org Subject: [PATCH 1/2] nvme: target: fix nvmet_file_init_bvec() Date: Mon, 25 Mar 2019 18:07:07 +0800 Message-Id: <20190325100708.24172-2-ming.lei@redhat.com> In-Reply-To: <20190325100708.24172-1-ming.lei@redhat.com> References: <20190325100708.24172-1-ming.lei@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 25 Mar 2019 10:07:29 +0000 (UTC) Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org There isn't sg iterator helper for building bvec, so invent one and fix the issue in nvmet_file_init_bvec(). The issue is that one sg may include multipge continuous pages, and only the 1st .bv_offset isn't zero, also the length for the last bvec has to consider the remained length. Reported-by: Yi Zhang Fixes: d5eff33ee6f8("nvmet: add simple file backed ns support") Cc: Yi Zhang Cc: Sagi Grimberg Cc: Chaitanya Kulkarni Cc: Signed-off-by: Ming Lei --- drivers/nvme/target/io-cmd-file.c | 57 ++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c index 3e43212d3c1c..1c76e9e2a474 100644 --- a/drivers/nvme/target/io-cmd-file.c +++ b/drivers/nvme/target/io-cmd-file.c @@ -75,13 +75,6 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns) return ret; } -static void nvmet_file_init_bvec(struct bio_vec *bv, struct sg_page_iter *iter) -{ - bv->bv_page = sg_page_iter_page(iter); - bv->bv_offset = iter->sg->offset; - bv->bv_len = PAGE_SIZE - iter->sg->offset; -} - static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos, unsigned long nr_segs, size_t count, int ki_flags) { @@ -129,13 +122,14 @@ static void nvmet_file_io_done(struct kiocb *iocb, long ret, long ret2) static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags) { ssize_t nr_bvec = DIV_ROUND_UP(req->data_len, PAGE_SIZE); - struct sg_page_iter sg_pg_iter; + struct scatterlist *sg; + struct bio_vec *bv; unsigned long bv_cnt = 0; bool is_sync = false; size_t len = 0, total_len = 0; ssize_t ret = 0; loff_t pos; - + int i, j, sg_done; if (req->f.mpool_alloc && nr_bvec > NVMET_MAX_MPOOL_BVEC) is_sync = true; @@ -147,26 +141,33 @@ static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags) } memset(&req->f.iocb, 0, sizeof(struct kiocb)); - for_each_sg_page(req->sg, &sg_pg_iter, req->sg_cnt, 0) { - nvmet_file_init_bvec(&req->f.bvec[bv_cnt], &sg_pg_iter); - len += req->f.bvec[bv_cnt].bv_len; - total_len += req->f.bvec[bv_cnt].bv_len; - bv_cnt++; - - WARN_ON_ONCE((nr_bvec - 1) < 0); - - if (unlikely(is_sync) && - (nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) { - ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0); - if (ret < 0) - goto complete; - - pos += len; - bv_cnt = 0; - len = 0; + for_each_sg(req->sg, sg, req->sg_cnt, i) + for (j = 0, sg_done = 0; + (bv = &req->f.bvec[bv_cnt]) && sg_done < sg->length; + j++, sg_done += bv->bv_len) { + bv->bv_offset = j ? 0 : sg->offset; + bv->bv_len = min_t(unsigned, PAGE_SIZE - bv->bv_offset, + sg->length - sg_done); + bv->bv_page = nth_page(sg_page(sg), j); + + len += bv->bv_len; + total_len += bv->bv_len; + bv_cnt++; + + WARN_ON_ONCE((nr_bvec - 1) < 0); + + if (unlikely(is_sync) && + (nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) { + ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0); + if (ret < 0) + goto complete; + + pos += len; + bv_cnt = 0; + len = 0; + } + nr_bvec--; } - nr_bvec--; - } if (WARN_ON_ONCE(total_len != req->data_len)) { ret = -EIO; -- 2.9.5