From: Xi Wang <xi.wang@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Jens Axboe <axboe@kernel.dk>,
Dan Carpenter <dan.carpenter@oracle.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH RFC] slab: introduce knalloc/kxnalloc
Date: Thu, 09 Feb 2012 07:41:41 -0500 [thread overview]
Message-ID: <4F33BF05.208@gmail.com> (raw)
In-Reply-To: <20120208142513.4db2493a.akpm@linux-foundation.org>
This patch introduces knalloc/kxnalloc wrappers that perform integer
overflow checks without zeroing the memory.
knalloc(n, size, flags) is the non-zeroing version of kcalloc(),
which allocates n * size bytes.
kxnalloc(xsize, n, size, flags) allocates xsize + n * size bytes.
It is useful to allocate a structure ending with a zero-length array,
which is a commonly used pattern. For example, in posix_acl_alloc()
to allocate a posix_acl object one could call
kxnalloc(sizeof(struct posix_acl),
count, sizeof(struct posix_acl_entry), flags);
to avoid overflowing the allocation size.
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
I coined the name knalloc() for Andrew's wtf_do_i_call_this() in the
previous email. A better name is welcome.
I additionally added kxnalloc() since it's a common idiom.
---
include/linux/slab.h | 31 +++++++++++++++++++++++++++----
1 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index 573c809..e3a1e81 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -190,7 +190,8 @@ size_t ksize(const void *);
#endif
/**
- * kcalloc - allocate memory for an array. The memory is set to zero.
+ * kxnalloc - allocate memory for an array with an extra header.
+ * @xsize: extra header size.
* @n: number of elements.
* @size: element size.
* @flags: the type of memory to allocate.
@@ -240,11 +241,33 @@ size_t ksize(const void *);
* for general use, and so are not documented here. For a full list of
* potential flags, always refer to linux/gfp.h.
*/
-static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
+static inline void *kxnalloc(size_t xsize, size_t n, size_t size, gfp_t flags)
{
- if (size != 0 && n > ULONG_MAX / size)
+ if (size != 0 && n > (ULONG_MAX - xsize) / size)
return NULL;
- return __kmalloc(n * size, flags | __GFP_ZERO);
+ return __kmalloc(xsize + n * size, flags);
+}
+
+/**
+ * knalloc - allocate memory for an array.
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate (see kmalloc).
+ */
+static inline void *knalloc(size_t n, size_t size, gfp_t flags)
+{
+ return kxnalloc(0, n, size, flags);
+}
+
+/**
+ * kcalloc - allocate memory for an array. The memory is set to zero.
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate (see kmalloc).
+ */
+static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
+{
+ return knalloc(n, size, flags | __GFP_ZERO);
}
#if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
--
1.7.5.4
next prev parent reply other threads:[~2012-02-09 12:41 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-07 14:11 integer overflows in kernel/relay.c Dan Carpenter
2012-02-08 8:34 ` Jens Axboe
2012-02-08 22:25 ` Andrew Morton
2012-02-09 12:41 ` Jens Axboe
2012-02-09 17:39 ` Andrew Morton
2012-02-09 12:41 ` Xi Wang [this message]
2012-02-09 13:05 ` [PATCH RFC] slab: introduce knalloc/kxnalloc Pekka Enberg
2012-02-09 13:19 ` Jens Axboe
2012-02-09 13:26 ` Xi Wang
2012-02-09 13:48 ` [PATCH RFC v2] slab: introduce kmalloc_array Xi Wang
2012-02-09 22:42 ` David Rientjes
2012-02-09 23:08 ` Andrew Morton
2012-02-09 22:47 ` Jesper Juhl
2012-02-09 23:06 ` Andrew Morton
2012-02-09 23:43 ` Joe Perches
2012-02-13 15:08 ` Xi Wang
2012-02-13 16:01 ` Christoph Lameter
2012-02-13 19:44 ` Dan Carpenter
2012-02-13 20:27 ` Christoph Lameter
2012-02-14 7:20 ` Dan Carpenter
2012-02-14 7:35 ` Pekka Enberg
2012-02-14 11:12 ` Xi Wang
2012-02-14 15:02 ` Christoph Lameter
2012-02-14 16:30 ` Xi Wang
2012-02-14 16:34 ` Christoph Lameter
2012-02-14 16:43 ` Xi Wang
2012-02-14 19:33 ` Uninline kcalloc Christoph Lameter
2012-02-14 19:37 ` Christoph Lameter
2012-02-14 20:46 ` Andrew Morton
2012-02-14 20:50 ` Nick Bowler
2012-02-14 21:24 ` Christoph Lameter
2012-02-15 20:17 ` Nick Bowler
2012-02-15 20:24 ` Nick Bowler
2012-02-14 20:45 ` Andrew Morton
2012-02-14 20:58 ` Pekka Enberg
2012-02-14 21:09 ` Christoph Lameter
2012-02-14 21:31 ` Pekka Enberg
2012-02-14 21:38 ` Christoph Lameter
2012-02-14 21:46 ` Xi Wang
2012-02-14 22:08 ` Christoph Lameter
2012-02-15 19:14 ` Xi Wang
2012-02-15 19:34 ` Christoph Lameter
2012-02-16 3:10 ` Xi Wang
2012-02-16 14:51 ` Christoph Lameter
2012-02-16 18:32 ` Xi Wang
2012-02-16 20:47 ` Christoph Lameter
2012-02-10 13:09 ` [PATCH RFC v2] slab: introduce kmalloc_array Alexey Dobriyan
2012-02-10 13:11 ` Alexey Dobriyan
2012-02-10 13:55 ` Xi Wang
2012-02-10 13:58 ` Alexey Dobriyan
2012-02-10 14:09 ` Xi Wang
2012-02-11 12:19 ` Alexey Dobriyan
2012-02-12 5:46 ` Xi Wang
2012-02-09 12:56 ` integer overflows in kernel/relay.c Pekka Enberg
2012-02-09 10:44 ` [patch] relay: prevent integer overflow in relay_open() Dan Carpenter
2012-02-09 11:55 ` walter harms
2012-02-09 12:36 ` Dan Carpenter
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=4F33BF05.208@gmail.com \
--to=xi.wang@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=dan.carpenter@oracle.com \
--cc=linux-kernel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).