linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Miloslav Trmač" <mitr@redhat.com>
To: Herbert Xu <herbert@gondor.hengli.com.au>
Cc: linux-crypto@vger.kernel.org,
	"Nikos Mavrogiannopoulos" <n.mavrogiannopoulos@gmail.com>,
	"Neil Horman" <nhorman@redhat.com>,
	linux-kernel@vger.kernel.org, "Miloslav Trmač" <mitr@redhat.com>
Subject: [PATCH 16/19] Add helpers for zero-copy userspace access
Date: Fri, 20 Aug 2010 10:46:00 +0200	[thread overview]
Message-ID: <1282293963-27807-18-git-send-email-mitr@redhat.com> (raw)
In-Reply-To: <1282293963-27807-1-git-send-email-mitr@redhat.com>

---
 crypto/userspace/cryptodev_main.c |   87 +++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/crypto/userspace/cryptodev_main.c b/crypto/userspace/cryptodev_main.c
index c6419f4..a6712db 100644
--- a/crypto/userspace/cryptodev_main.c
+++ b/crypto/userspace/cryptodev_main.c
@@ -1,3 +1,46 @@
+/*
+ * Driver for /dev/crypto device (aka CryptoDev)
+ *
+ * Copyright (c) 2004 Michal Ludvig <mludvig@logix.net.nz>, SuSE Labs
+ * Copyright (c) 2009,2010 Nikos Mavrogiannopoulos <nmav@gnutls.org>
+ *
+ * This file is part of linux cryptodev.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+/*
+ * Device /dev/crypto provides an interface for 
+ * accessing kernel CryptoAPI algorithms (ciphers,
+ * hashes) from userspace programs.
+ *
+ * /dev/crypto interface was originally introduced in
+ * OpenBSD and this module attempts to keep the API.
+ *
+ */
+
+#include <linux/audit.h>
+#include <linux/crypto.h>
+#include <linux/mm.h>
+#include <linux/highmem.h>
+#include <linux/ioctl.h>
+#include <linux/random.h>
+#include <linux/syscalls.h>
+#include <linux/pagemap.h>
+#include <linux/uaccess.h>
+#include <linux/scatterlist.h>
 #include "cryptodev_int.h"
 #include "ncr-int.h"
 #include <linux/version.h>
@@ -11,3 +54,47 @@ MODULE_LICENSE("GPL");
 int cryptodev_verbosity = 0;
 module_param(cryptodev_verbosity, int, 0644);
 MODULE_PARM_DESC(cryptodev_verbosity, "0: normal, 1: verbose, 2: debug");
+
+/* ====== CryptoAPI ====== */
+
+void release_user_pages(struct page **pg, int pagecount)
+{
+	while (pagecount--) {
+		if (!PageReserved(pg[pagecount]))
+			SetPageDirty(pg[pagecount]);
+		page_cache_release(pg[pagecount]);
+	}
+}
+
+/* offset of buf in it's first page */
+#define PAGEOFFSET(buf) ((unsigned long)buf & ~PAGE_MASK)
+
+/* fetch the pages addr resides in into pg and initialise sg with them */
+int __get_userbuf(uint8_t __user *addr, uint32_t len, int write,
+		int pgcount, struct page **pg, struct scatterlist *sg)
+{
+	int ret, pglen, i = 0;
+	struct scatterlist *sgp;
+
+	down_write(&current->mm->mmap_sem);
+	ret = get_user_pages(current, current->mm,
+			(unsigned long)addr, pgcount, write, 0, pg, NULL);
+	up_write(&current->mm->mmap_sem);
+	if (ret != pgcount)
+		return -EINVAL;
+
+	sg_init_table(sg, pgcount);
+
+	pglen = min((ptrdiff_t)(PAGE_SIZE - PAGEOFFSET(addr)), (ptrdiff_t)len);
+	sg_set_page(sg, pg[i++], pglen, PAGEOFFSET(addr));
+
+	len -= pglen;
+	for (sgp = sg_next(sg); len; sgp = sg_next(sgp)) {
+		pglen = min((uint32_t)PAGE_SIZE, len);
+		sg_set_page(sgp, pg[i++], pglen, 0);
+		len -= pglen;
+	}
+	sg_mark_end(sg_last(sg, pgcount));
+	return 0;
+}
+
-- 
1.7.2.1


  parent reply	other threads:[~2010-08-20  8:48 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-20  8:45 [PATCH 00/19] RFC, v2: "New" /dev/crypto user-space interface Miloslav Trmač
2010-08-20  8:45 ` [PATCH 01/19] User-space API definition Miloslav Trmač
2010-08-20 12:48   ` Stefan Richter
2010-08-21  7:35     ` Nikos Mavrogiannopoulos
2010-08-21  9:11     ` Miloslav Trmac
2010-08-20 17:12   ` Randy Dunlap
2010-08-21 13:09   ` Kyle Moffett
2010-08-21 14:54     ` Nikos Mavrogiannopoulos
2010-08-22 10:22     ` David Howells
2010-09-03  9:18   ` Herbert Xu
2010-09-03  9:34     ` Nikos Mavrogiannopoulos
2010-09-03 15:20     ` Nikos Mavrogiannopoulos
2010-08-20  8:45 ` [PATCH 02/19] Add CRYPTO_USERSPACE config option Miloslav Trmač
2010-08-20  8:45 ` [PATCH 03/19] Add libtommath headers Miloslav Trmač
2010-08-20  8:45 ` [PATCH 04/19] Add libtomcrypt headers Miloslav Trmač
2010-08-20  8:45 ` [PATCH 05/19] Add internal /dev/crypto implementation headers Miloslav Trmač
2010-08-20  8:45 ` [PATCH 06/19] Add ioctl() argument and attribute handling utils Miloslav Trmač
2010-08-20 12:59   ` Stefan Richter
2010-08-21  2:15     ` Miloslav Trmac
2010-08-21  7:15       ` Stefan Richter
2010-08-20  8:45 ` [PATCH 07/19] Add crypto API utilities Miloslav Trmač
2010-08-20  8:45 ` [PATCH 08/19] Add per-process and per-user limits Miloslav Trmač
2010-08-20  8:45 ` [PATCH 09/19] Add libtommath implementation Miloslav Trmač
2010-08-20  8:45 ` [PATCH 10/19] Add libtomcrypt implementation Miloslav Trmač
2010-08-20  8:45 ` [PATCH 10/19] Add libtommath implementation Miloslav Trmač
2010-08-20  8:45 ` [PATCH 11/19] Add algorithm properties table Miloslav Trmač
2010-08-20  8:45 ` [PATCH 12/19] Add DH implementation and pubkey abstraction layer Miloslav Trmač
2010-08-20  8:45 ` [PATCH 13/19] Add /dev/crypto auditing infrastructure Miloslav Trmač
2010-08-20  8:45 ` [PATCH 14/19] Add most operations on key objects Miloslav Trmač
2010-08-20  8:45 ` [PATCH 15/19] Add key wrapping operations Miloslav Trmač
2010-08-20  8:46 ` Miloslav Trmač [this message]
2010-08-20  8:46 ` [PATCH 17/19] Add session operations Miloslav Trmač
2010-08-20  8:46 ` [PATCH 18/19] Add ioctl handlers Miloslav Trmač
2010-08-20  8:46 ` [PATCH 19/19] Finally, add the /dev/crypto device Miloslav Trmač
2010-08-20 13:56 ` [PATCH 00/19] RFC, v2: "New" /dev/crypto user-space interface Ted Ts'o
2010-08-20 17:03   ` Nikos Mavrogiannopoulos
2010-08-20 23:48     ` Ted Ts'o
2010-08-23  6:39       ` Tomas Mraz
2010-08-21 17:08 ` Arnd Bergmann
2010-08-22  7:52   ` Nikos Mavrogiannopoulos
2010-08-23  8:09     ` Arnd Bergmann
2010-08-23  9:34       ` Nikos Mavrogiannopoulos
2010-08-25  6:20 ` Pavel Machek
2010-08-25  6:44   ` Tomas Mraz
2010-08-25 15:28   ` Miloslav Trmac

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=1282293963-27807-18-git-send-email-mitr@redhat.com \
    --to=mitr@redhat.com \
    --cc=herbert@gondor.hengli.com.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=n.mavrogiannopoulos@gmail.com \
    --cc=nhorman@redhat.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).