From: Li Zefan <lizf@cn.fujitsu.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>,
LKML <linux-kernel@vger.kernel.org>,
"linux-mm@kvack.org" <linux-mm@kvack.org>
Subject: [PATCH -v2] memdup_user(): introduce
Date: Fri, 06 Mar 2009 17:49:45 +0800 [thread overview]
Message-ID: <49B0F1B9.1080903@cn.fujitsu.com> (raw)
In-Reply-To: <20090306011548.ffdf9cbc.akpm@linux-foundation.org>
I notice there are many places doing copy_from_user() which follows
kmalloc():
dst = kmalloc(len, GFP_KERNEL);
if (!dst)
return -ENOMEM;
if (copy_from_user(dst, src, len)) {
kfree(dst);
return -EFAULT
}
memdup_user() is a wrapper of the above code. With this new function,
we don't have to write 'len' twice, which can lead to typos/mistakes.
It also produces smaller code and kernel text.
A quick grep shows 250+ places where memdup_user() *may* be used. I'll
prepare a patchset to do this conversion.
v1 -> v2: change the name from kmemdup_from_user to memdup_user.
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
---
Can this get into 2.6.29, so I can prepare patches based on linux-next?
And this won't cause regression, since no one uses it yet. :)
---
include/linux/string.h | 1 +
mm/util.c | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index 76ec218..79f30f3 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -12,6 +12,7 @@
#include <linux/stddef.h> /* for NULL */
extern char *strndup_user(const char __user *, long);
+extern void *memdup_user(const void __user *, size_t, gfp_t);
/*
* Include machine specific inline routines
diff --git a/mm/util.c b/mm/util.c
index 37eaccd..3d21c21 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -70,6 +70,32 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp)
EXPORT_SYMBOL(kmemdup);
/**
+ * memdup_user - duplicate memory region from user space
+ *
+ * @src: source address in user space
+ * @len: number of bytes to copy
+ * @gfp: GFP mask to use
+ *
+ * Returns an ERR_PTR() on failure.
+ */
+void *memdup_user(const void __user *src, size_t len, gfp_t gfp)
+{
+ void *p;
+
+ p = kmalloc_track_caller(len, gfp);
+ if (!p)
+ return ERR_PTR(-ENOMEM);
+
+ if (copy_from_user(p, src, len)) {
+ kfree(p);
+ return ERR_PTR(-EFAULT);
+ }
+
+ return p;
+}
+EXPORT_SYMBOL(memdup_user);
+
+/**
* __krealloc - like krealloc() but don't free @p.
* @p: object to reallocate memory for.
* @new_size: how many bytes of memory are required.
--
1.5.4.rc3
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2009-03-06 9:49 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-06 7:04 [RFC][PATCH] kmemdup_from_user(): introduce Li Zefan
2009-03-06 7:23 ` Américo Wang
2009-03-06 7:37 ` KOSAKI Motohiro
2009-03-06 8:03 ` KOSAKI Motohiro
2009-03-06 7:39 ` Li Zefan
2009-03-06 8:20 ` Alexey Dobriyan
2009-03-06 8:27 ` Li Zefan
2009-03-06 8:39 ` Andrew Morton
2009-03-06 8:57 ` Alexey Dobriyan
2009-03-06 9:09 ` KOSAKI Motohiro
2009-03-06 9:01 ` Li Zefan
2009-03-06 9:15 ` Andrew Morton
2009-03-06 9:49 ` Li Zefan [this message]
2009-03-06 23:03 ` [PATCH -v2] memdup_user(): introduce Andrew Morton
2009-03-07 16:48 ` Arjan van de Ven
2009-03-07 16:54 ` Roland Dreier
2009-03-07 18:27 ` Andrew Morton
2009-03-09 2:22 ` Li Zefan
2009-03-09 3:00 ` Andrew Morton
2009-03-09 3:30 ` Li Zefan
2009-03-09 3:45 ` Andrew Morton
2009-03-06 9:03 ` [RFC][PATCH] kmemdup_from_user(): introduce Alexey Dobriyan
2009-03-06 9:02 ` Li Zefan
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=49B0F1B9.1080903@cn.fujitsu.com \
--to=lizf@cn.fujitsu.com \
--cc=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/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