From patchwork Mon Jan 21 08:05:27 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 2009551 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 096963FD1A for ; Mon, 21 Jan 2013 08:03:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752228Ab3AUID4 (ORCPT ); Mon, 21 Jan 2013 03:03:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37330 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752001Ab3AUIDz (ORCPT ); Mon, 21 Jan 2013 03:03:55 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r0L83rkF026356 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 21 Jan 2013 03:03:53 -0500 Received: from hj.localdomain.com ([10.66.6.0]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r0L83ZcN012334; Mon, 21 Jan 2013 03:03:48 -0500 From: Asias He To: Nicholas Bellinger Cc: "Michael S. Tsirkin" , Rusty Russell , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, target-devel@vger.kernel.org Subject: [PATCH 2/3] tcm_vhost: Optimize gup in vhost_scsi_map_to_sgl Date: Mon, 21 Jan 2013 16:05:27 +0800 Message-Id: <1358755528-31421-3-git-send-email-asias@redhat.com> In-Reply-To: <1358755528-31421-1-git-send-email-asias@redhat.com> References: <1358755528-31421-1-git-send-email-asias@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org We can get all the pages in one time instead of calling gup N times. Signed-off-by: Asias He --- drivers/vhost/tcm_vhost.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/drivers/vhost/tcm_vhost.c b/drivers/vhost/tcm_vhost.c index ca35c16..59be442 100644 --- a/drivers/vhost/tcm_vhost.c +++ b/drivers/vhost/tcm_vhost.c @@ -430,37 +430,45 @@ static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd( * Returns the number of scatterlist entries used or -errno on error. */ static int vhost_scsi_map_to_sgl(struct scatterlist *sgl, - unsigned int sgl_count, void __user *ptr, size_t len, int write) + unsigned int sgl_count, struct iovec *iov, int write) { struct scatterlist *sg = sgl; unsigned int npages = 0; + void __user *ptr = iov->iov_base; + size_t len = iov->iov_len; int ret; + unsigned int pages_nr, offset, nbytes; + struct page **pages; + + pages_nr = iov_num_pages(iov); + pages = kmalloc(pages_nr * sizeof(struct page *), GFP_ATOMIC); + if (!pages) + return -ENOMEM; + + ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages); + if (ret != pages_nr) + goto err; while (len > 0) { - struct page *page; - unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK; - unsigned int nbytes = min_t(unsigned int, - PAGE_SIZE - offset, len); + offset = (uintptr_t)ptr & ~PAGE_MASK; + nbytes = min_t(unsigned int, PAGE_SIZE - offset, len); if (npages == sgl_count) { ret = -ENOBUFS; goto err; } - ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page); - BUG_ON(ret == 0); /* we should either get our page or fail */ - if (ret < 0) - goto err; - - sg_set_page(sg, page, nbytes, offset); + sg_set_page(sg, pages[npages], nbytes, offset); ptr += nbytes; len -= nbytes; sg++; npages++; } + kfree(pages); return npages; err: + kfree(pages); /* Put pages that we hold */ for (sg = sgl; sg != &sgl[npages]; sg++) put_page(sg_page(sg)); @@ -498,8 +506,7 @@ static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd, pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count); for (i = 0; i < niov; i++) { - ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base, - iov[i].iov_len, write); + ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write); if (ret < 0) { for (i = 0; i < tv_cmd->tvc_sgl_count; i++) put_page(sg_page(&tv_cmd->tvc_sgl[i]));