From patchwork Fri Feb 6 05:06:37 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: 5788561 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id DD3479F30C for ; Fri, 6 Feb 2015 05:40:05 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F180220155 for ; Fri, 6 Feb 2015 05:40:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0D1DE2015E for ; Fri, 6 Feb 2015 05:40:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752484AbbBFFjx (ORCPT ); Fri, 6 Feb 2015 00:39:53 -0500 Received: from mail-oi0-f43.google.com ([209.85.218.43]:62072 "EHLO mail-oi0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751555AbbBFFjv (ORCPT ); Fri, 6 Feb 2015 00:39:51 -0500 Received: by mail-oi0-f43.google.com with SMTP id z81so10295064oif.2 for ; Thu, 05 Feb 2015 21:39:50 -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=G4ZFJcL1HS/0I1ad1LJjbxoplenwm0SLNNzzHZnxs+o=; b=jIpOHAUjNVhI7H7iUaSkrHE4H9xyXY/w+ZKVDYQSE1BDj0mfak/7blfSM8MQl1Cr5m lsQxdrgyCo/7xHEC8qiS1WC5dQiyljajg2DO7TBo/Sw8KcQ0wMhA0MdFNiEmPzs8HONs z4uG6A9NhMShyVVTzUDfwBqqticHjChQA51AABjAdDKBwxR5BCrgmRgoSNff6+pvgSUN 8hO8LmtIeBWancc/CowQEIuTf+FZayMLhnyd/iFeGKQRZ7NUmNv1hUf682acMGf6grJq tydNxIssqAEQ0Jdf4KfgoXFgdjM/zRlGShvd1nFrvwcxu/T0x3EqyJVhdhE6siMSYHS8 lBOg== X-Gm-Message-State: ALoCoQnydch4B4OKiktaIriBiJTo7COim0zsjNftFgPG3bocBZxet8eBhiIoGAJ8Bcd2OPiOSxVw X-Received: by 10.182.186.33 with SMTP id fh1mr1299081obc.50.1423201190398; Thu, 05 Feb 2015 21:39:50 -0800 (PST) Received: from localhost.localdomain (mail.linux-iscsi.org. [67.23.28.174]) by mx.google.com with ESMTPSA id x8sm800393obg.18.2015.02.05.21.39.49 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 05 Feb 2015 21:39:50 -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 , Al Viro Subject: [PATCH-v4 4/8] vhost/scsi: Add ANY_LAYOUT iov -> sgl mapping prerequisites Date: Fri, 6 Feb 2015 05:06:37 +0000 Message-Id: <1423199201-14659-5-git-send-email-nab@daterainc.com> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1423199201-14659-1-git-send-email-nab@daterainc.com> References: <1423199201-14659-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. Cc: Michael S. Tsirkin Cc: Paolo Bonzini Cc: Al Viro 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 b9800c7..5396b8a 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 =