From patchwork Tue Feb 3 06:29:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nicholas A. Bellinger" X-Patchwork-Id: 5766541 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E8412BF440 for ; Tue, 3 Feb 2015 07:04:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 030D220A6A for ; Tue, 3 Feb 2015 07:04:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0B2D920A68 for ; Tue, 3 Feb 2015 07:04:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753520AbbBCHE0 (ORCPT ); Tue, 3 Feb 2015 02:04:26 -0500 Received: from mail-ob0-f179.google.com ([209.85.214.179]:42568 "EHLO mail-ob0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933951AbbBCHDI (ORCPT ); Tue, 3 Feb 2015 02:03:08 -0500 Received: by mail-ob0-f179.google.com with SMTP id wp4so6681820obc.10 for ; Mon, 02 Feb 2015 23:03:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=67K39quznE7/tNvFyxyi42x3Jqd6tM/y5rfeoJtKe4A=; b=WKvGO/F0iEH+m3a2EB5nspagEN7tR4H9P+v85xrOnaE8wNINlt0VPIwAoU6RL3Mr0x bejygiwFFcKZG4s7h/l5PQk+TGkyb54HbXfNho3/6gEpN7SEChIdEq6DCTHnOGR7UlRL 7INB6VJxVgmAceAQXNf/IYjYhD9Y2LnKix90kmaRzU0wh6WsO9E1i3MCURT3UIHDEU86 vEkb85dCQCrzmtmyywXtMidnw8hxb1WJlBZzrupS4I7hL6ioHI5FiY2EqaW9Af204PUp Gt+24BgF0EjKHvekxfCNjgJt6wCfbpWqv4uV2eUVGFw9O2cS+2bOadkAShtOlPBMoOyV vo4Q== X-Gm-Message-State: ALoCoQmogjmQs8TM1v+yuIKIxeX7f6scFZ92zvazzJ13fGQXboJzqsudQ3MJALjAyTywJsI67nJ6 X-Received: by 10.202.111.131 with SMTP id v3mr13566736oik.133.1422946987524; Mon, 02 Feb 2015 23:03:07 -0800 (PST) Received: from localhost.localdomain (mail.linux-iscsi.org. [67.23.28.174]) by mx.google.com with ESMTPSA id t8sm10657870obv.22.2015.02.02.23.03.07 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 02 Feb 2015 23:03:07 -0800 (PST) From: "Nicholas A. Bellinger" To: target-devel Cc: linux-scsi , kvm-devel , Paolo Bonzini , "Michael S. Tsirkin" , Al Viro , Christoph Hellwig , Nicholas Bellinger Subject: [PATCH-v3 4/9] vhost/scsi: Add ANY_LAYOUT iov -> sgl mapping prerequisites Date: Tue, 3 Feb 2015 06:29:58 +0000 Message-Id: <1422945003-24538-5-git-send-email-nab@daterainc.com> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1422945003-24538-1-git-send-email-nab@daterainc.com> References: <1422945003-24538-1-git-send-email-nab@daterainc.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Nicholas Bellinger This patch adds ANY_LAYOUT prerequisites logic for accepting a set of protection + data payloads via iov_iter. Also includes helpers for calcuating SGLs + invoking vhost_scsi_map_to_sgl() with a known number of iovecs. Required by ANY_LAYOUT processing when struct iovec may be offset into the first outgoing virtio-scsi request header. v2 changes: - Clear ->tvc_sgl_count for vhost_scsi_mapal failure - Make vhost_scsi_mapal + vhost_scsi_calc_sgls accept max_niov - Minor cleanups v3 changes: - Update vhost_scsi_mapal + friends to use iov_iter - Move iov_iter sanity checks into vhost_scsi_calc_sgls - Convert vhost_scsi_calc_sgls() to iov_iter_npages() Cc: Michael S. Tsirkin Cc: Paolo Bonzini Signed-off-by: Nicholas Bellinger --- drivers/vhost/scsi.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index c3b12b3..7dfff15 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -914,6 +914,99 @@ vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd *cmd, return 0; } +static int +vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls) +{ + int sgl_count = 0; + + if (!iter || !iter->iov) { + pr_err("%s: iter->iov is NULL, but expected bytes: %zu" + " present\n", __func__, bytes); + return -EINVAL; + } + + sgl_count = iov_iter_npages(iter, 0xffff); + if (sgl_count > max_sgls) { + pr_err("%s: requested sgl_count: %d exceeds pre-allocated" + " max_sgls: %d\n", __func__, sgl_count, max_sgls); + return -EINVAL; + } + return sgl_count; +} + +static int +vhost_scsi_iov_to_sgl(struct tcm_vhost_cmd *cmd, bool write, + struct iov_iter *iter, struct scatterlist *sg, + int sg_count) +{ + size_t off = iter->iov_offset; + int i, ret; + + for (i = 0; i < iter->nr_segs; i++) { + void __user *base = iter->iov[i].iov_base + off; + size_t len = iter->iov[i].iov_len - off; + + ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write); + if (ret < 0) { + for (i = 0; i < sg_count; i++) { + struct page *page = sg_page(&sg[i]); + if (page) + put_page(page); + } + return ret; + } + sg += ret; + off = 0; + } + return 0; +} + +static int +vhost_scsi_mapal(struct tcm_vhost_cmd *cmd, + size_t prot_bytes, struct iov_iter *prot_iter, + size_t data_bytes, struct iov_iter *data_iter) +{ + int sgl_count, ret; + bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE); + + if (prot_bytes) { + sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes, + TCM_VHOST_PREALLOC_PROT_SGLS); + if (sgl_count < 0) + return sgl_count; + + sg_init_table(cmd->tvc_prot_sgl, sgl_count); + cmd->tvc_prot_sgl_count = sgl_count; + pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__, + cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count); + + ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter, + cmd->tvc_prot_sgl, + cmd->tvc_prot_sgl_count); + if (ret < 0) { + cmd->tvc_prot_sgl_count = 0; + return ret; + } + } + sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes, + TCM_VHOST_PREALLOC_SGLS); + if (sgl_count < 0) + return sgl_count; + + sg_init_table(cmd->tvc_sgl, sgl_count); + cmd->tvc_sgl_count = sgl_count; + pr_debug("%s data_sg %p data_sgl_count %u\n", __func__, + cmd->tvc_sgl, cmd->tvc_sgl_count); + + ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter, + cmd->tvc_sgl, cmd->tvc_sgl_count); + if (ret < 0) { + cmd->tvc_sgl_count = 0; + return ret; + } + return 0; +} + static void tcm_vhost_submission_work(struct work_struct *work) { struct tcm_vhost_cmd *cmd =