From patchwork Mon Feb 2 04:06:28 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: 5759151 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 1F6ECBF440 for ; Mon, 2 Feb 2015 04:40:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 21C17202A1 for ; Mon, 2 Feb 2015 04:40:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6EDBE20295 for ; Mon, 2 Feb 2015 04:40:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932376AbbBBEjp (ORCPT ); Sun, 1 Feb 2015 23:39:45 -0500 Received: from mail-oi0-f47.google.com ([209.85.218.47]:33936 "EHLO mail-oi0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755165AbbBBEjj (ORCPT ); Sun, 1 Feb 2015 23:39:39 -0500 Received: by mail-oi0-f47.google.com with SMTP id a141so42687150oig.6 for ; Sun, 01 Feb 2015 20:39:38 -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=vINK+uUJ6Ms1bDYYufeoyAfE8WllZrmC2PdQ4tXJ8dE=; b=hq10uLzSoZTw967alMK3eL6Es3VzfeffsY41Xl2E8cL/zqAAsFJw56vxi2+3P24qXU XJqgjQJKc2iwJGoPFU7W/EbgxqMDryfNsochDFZYooJm566xwxV6rDOfKM2s0aplfwEP s5KsTASRwyda4gmv+fZjUVqrn53FksrxvZTClzdSFOEroI/It7f+l8Lj7gELR96aE1qo 6sB6rQi8a5kHCG/rnFtzr33Zslgdil2xKoArWaiNZWv1biFf2zu/ZbJ9Mr6v8RaUXBNV O7IfyUTEqEWwOyMGqP+32K4bOQY3X1FNuuD3A3yQvYYVq9jlS7YPROmWkwR1QMcHtEBq 8Y5g== X-Gm-Message-State: ALoCoQlNTd2PYaQIOSkbn7oV6z6mOeu9VPwegRgL1Gaix1c+058aNSLuFOt7KiqvZ3eq2j859IS9 X-Received: by 10.182.76.69 with SMTP id i5mr10922230obw.19.1422851978924; Sun, 01 Feb 2015 20:39:38 -0800 (PST) Received: from localhost.localdomain (mail.linux-iscsi.org. [67.23.28.174]) by mx.google.com with ESMTPSA id y2sm8950007oei.1.2015.02.01.20.39.38 (version=TLSv1 cipher=RC4-SHA bits=128/128); Sun, 01 Feb 2015 20:39:38 -0800 (PST) From: "Nicholas A. Bellinger" To: target-devel Cc: linux-scsi , kvm-devel , Paolo Bonzini , "Michael S. Tsirkin" , Nicholas Bellinger Subject: [PATCH-v2 05/11] vhost/scsi: Add ANY_LAYOUT iov -> sgl mapping prerequisites Date: Mon, 2 Feb 2015 04:06:28 +0000 Message-Id: <1422849994-3419-6-git-send-email-nab@daterainc.com> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1422849994-3419-1-git-send-email-nab@daterainc.com> References: <1422849994-3419-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 iovec + offset. 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 Cc: Michael S. Tsirkin Cc: Paolo Bonzini Signed-off-by: Nicholas Bellinger --- drivers/vhost/scsi.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 049e603..ecbd567 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -909,6 +909,121 @@ vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd *cmd, return 0; } +static int +vhost_scsi_calc_sgls(struct iovec *iov, size_t off, size_t bytes, + int *niov, int max_niov, int max_sgls) +{ + size_t tmp = 0; + int sgl_count = 0; + + *niov = 0; + + while (tmp < bytes) { + void __user *base = iov[*niov].iov_base + off; + size_t len = iov[(*niov)++].iov_len - off; + + if (*niov > max_niov) { + pr_err("%s: current *niov %d exceeds max_niov: %d\n", + __func__, *niov, max_niov); + return -EINVAL; + } + sgl_count += iov_num_pages(base, len); + tmp += min(len, bytes); + off = 0; + } + 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 -ENOBUFS; + } + return sgl_count; +} + +static int +vhost_scsi_iov_to_sgl(struct tcm_vhost_cmd *cmd, bool write, + struct iovec *iov, size_t iov_off, int niov, + struct scatterlist *sg, int sg_count) +{ + int i, ret; + + for (i = 0; i < niov; i++) { + void __user *base = iov[i].iov_base + iov_off; + size_t len = iov[i].iov_len - iov_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; + iov_off = 0; + } + return 0; +} + +static int +vhost_scsi_mapal(struct tcm_vhost_cmd *cmd, int max_niov, + size_t prot_bytes, struct iovec *prot_iov, size_t prot_off, + size_t data_bytes, struct iovec *data_iov, size_t data_off) +{ + int sgl_count = 0, niov, ret; + bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE); + + if (prot_bytes) { + if (!prot_iov) { + pr_err("%s: prot_iov is NULL, but prot_bytes: %zu" + "present\n", __func__, prot_bytes); + return -EINVAL; + } + sgl_count = vhost_scsi_calc_sgls(prot_iov, prot_off, prot_bytes, + &niov, max_niov, + 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_iov, prot_off, + niov, cmd->tvc_prot_sgl, + cmd->tvc_prot_sgl_count); + if (ret < 0) { + cmd->tvc_prot_sgl_count = 0; + return ret; + } + max_niov -= niov; + } + if (!data_iov) { + pr_err("%s: data_iov is NULL, but data_bytes: %zu present\n", + __func__, data_bytes); + return -EINVAL; + } + sgl_count = vhost_scsi_calc_sgls(data_iov, data_off, data_bytes, + &niov, max_niov, + 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_iov, data_off, niov, + 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 =