qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
To: kvm@vger.kernel.org, linux-s390@vger.kernel.org, qemu-devel@nongnu.org
Cc: bjsdjshi@linux.vnet.ibm.com, renxiaof@linux.vnet.ibm.com,
	cornelia.huck@de.ibm.com, borntraeger@de.ibm.com, agraf@suse.com,
	alex.williamson@redhat.com
Subject: [Qemu-devel] [PATCH RFC 6/8] vfio: ccw: introduce page array interfaces
Date: Fri, 29 Apr 2016 14:11:53 +0200	[thread overview]
Message-ID: <1461931915-22397-7-git-send-email-bjsdjshi@linux.vnet.ibm.com> (raw)
In-Reply-To: <1461931915-22397-1-git-send-email-bjsdjshi@linux.vnet.ibm.com>

CCW translation requires to pin/unpin sets of mem pages frequently.
Currently we have a lack of support to do this in an efficient way.
So we introduce page_array data structure and helper functions to
handle pin/unpin operations here.

Signed-off-by: Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
---
 drivers/vfio/ccw/Makefile   |   2 +-
 drivers/vfio/ccw/ccwchain.c | 128 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 drivers/vfio/ccw/ccwchain.c

diff --git a/drivers/vfio/ccw/Makefile b/drivers/vfio/ccw/Makefile
index ea14ca9..ac62330 100644
--- a/drivers/vfio/ccw/Makefile
+++ b/drivers/vfio/ccw/Makefile
@@ -1,2 +1,2 @@
-vfio-ccw-y := vfio_ccw.o
+vfio-ccw-y := vfio_ccw.o ccwchain.o
 obj-$(CONFIG_VFIO_CCW) += vfio-ccw.o
diff --git a/drivers/vfio/ccw/ccwchain.c b/drivers/vfio/ccw/ccwchain.c
new file mode 100644
index 0000000..03b4e82
--- /dev/null
+++ b/drivers/vfio/ccw/ccwchain.c
@@ -0,0 +1,128 @@
+/*
+ * ccwchain interfaces
+ *
+ * Copyright IBM Corp. 2016
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License (version 2 only)
+ * as published by the Free Software Foundation.
+ *
+ * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
+ *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
+ */
+
+#include <linux/mm.h>
+#include <linux/slab.h>
+
+struct page_array {
+	u64			hva;
+	int			nr;
+	struct page		**items;
+};
+
+struct page_arrays {
+	struct page_array	*parray;
+	int			nr;
+};
+
+/*
+ * Helpers to operate page_array.
+ */
+/*
+ * page_array_pin() - pin user pages in memory
+ * @p: page_array on which to perform the operation
+ *
+ * Attempt to pin user pages in memory.
+ *
+ * Usage of page_array:
+ * @p->hva      starting user address. Assigned by caller.
+ * @p->nr       number of pages from @p->hva to pin. Assigned by caller.
+ *              number of pages pinned. Assigned by callee.
+ * @p->items    array that receives pointers to the pages pinned. Allocated by
+ *              caller.
+ *
+ * Returns:
+ *   Number of pages pinned on success. If @p->nr is 0 or negative, returns 0.
+ *   If no pages were pinned, returns -errno.
+ */
+static int page_array_pin(struct page_array *p)
+{
+	int i, nr;
+
+	nr = get_user_pages_fast(p->hva, p->nr, 1, p->items);
+	if (nr <= 0) {
+		p->nr = 0;
+		return nr;
+	} else if (nr != p->nr) {
+		for (i = 0; i < nr; i++)
+			put_page(p->items[i]);
+		p->nr = 0;
+		return -ENOMEM;
+	}
+
+	return nr;
+}
+
+/* Unpin the items before releasing the memory. */
+static void page_array_items_unpin_free(struct page_array *p)
+{
+	int i;
+
+	for (i = 0; i < p->nr; i++)
+		put_page(p->items[i]);
+
+	p->nr = 0;
+	kfree(p->items);
+}
+
+/* Alloc memory for items, then pin pages with them. */
+static int page_array_items_alloc_pin(u64 hva,
+				      unsigned int len,
+				      struct page_array *p)
+{
+	int ret;
+
+	if (!len || p->nr)
+		return -EINVAL;
+
+	p->hva = hva;
+
+	p->nr = ((hva & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
+	if (!p->nr)
+		return -EINVAL;
+
+	p->items = kcalloc(p->nr, sizeof(*p->items), GFP_KERNEL);
+	if (!p->items)
+		return -ENOMEM;
+
+	ret = page_array_pin(p);
+	if (ret <= 0)
+		kfree(p->items);
+
+	return ret;
+}
+
+static int page_arrays_init(struct page_arrays *ps, int nr)
+{
+	ps->parray = kcalloc(nr, sizeof(*ps->parray), GFP_KERNEL);
+	if (!ps->parray) {
+		ps->nr = 0;
+		return -ENOMEM;
+	}
+
+	ps->nr = nr;
+	return 0;
+}
+
+static void page_arrays_unpin_free(struct page_arrays *ps)
+{
+	int i;
+
+	for (i = 0; i < ps->nr; i++)
+		page_array_items_unpin_free(ps->parray + i);
+
+	kfree(ps->parray);
+
+	ps->parray = NULL;
+	ps->nr = 0;
+}
-- 
2.6.6

  parent reply	other threads:[~2016-04-29 12:13 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-29 12:11 [Qemu-devel] [PATCH RFC 0/8] basic vfio-ccw infrastructure Dong Jia Shi
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 1/8] iommu: s390: enable iommu api for s390 ccw devices Dong Jia Shi
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 2/8] s390: move orb.h from drivers/s390/ to arch/s390/ Dong Jia Shi
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 3/8] vfio: ccw: basic implementation for vfio_ccw driver Dong Jia Shi
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 4/8] vfio: ccw: realize VFIO_DEVICE_GET_INFO ioctl Dong Jia Shi
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 5/8] vfio: ccw: realize VFIO_DEVICE_CCW_HOT_RESET ioctl Dong Jia Shi
2016-04-29 12:11 ` Dong Jia Shi [this message]
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 7/8] vfio: ccw: introduce ccw chain interfaces Dong Jia Shi
2016-04-29 12:11 ` [Qemu-devel] [PATCH RFC 8/8] vfio: ccw: realize VFIO_DEVICE_CCW_CMD_REQUEST ioctl Dong Jia Shi
2016-04-29 17:17 ` [Qemu-devel] [PATCH RFC 0/8] basic vfio-ccw infrastructure Alex Williamson
2016-05-04  9:26   ` Dong Jia
2016-05-04 19:26     ` Alex Williamson
2016-05-05 10:29       ` Dong Jia
2016-05-05 19:19         ` Alex Williamson
2016-05-05 20:23           ` Neo Jia
2016-05-09  9:59             ` Dong Jia
2016-05-09  9:55           ` Dong Jia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1461931915-22397-7-git-send-email-bjsdjshi@linux.vnet.ibm.com \
    --to=bjsdjshi@linux.vnet.ibm.com \
    --cc=agraf@suse.com \
    --cc=alex.williamson@redhat.com \
    --cc=borntraeger@de.ibm.com \
    --cc=cornelia.huck@de.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=qemu-devel@nongnu.org \
    --cc=renxiaof@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).