(repost) kmem_cache_zalloc
Subsystems (2)
PATCH 1/1 (repost) kmem_cache_zalloc
(reposting)
There was a brief thread on this patch a while ago, please see
http://www.uwsg.iu.edu/hypermail/linux/kernel/0203.3/0601.html
In short, XFS is using a kmem_cache_zalloc() function which just does kmem_cache_alloc + memset.
We'd like to incorporate this into the kernel proper, and several others chimed in that it would be useful, so here's the patch. If it's a no-go with Linus, we can roll this functionality back under fs/xfs to reduce our changes in the mainline kernel.
Brian Gerst suggested adding a flag to the cache to tell kmem_cache_zalloc() to zero the object, and this also sounds like a reasonable way to go, but there was no discussion after that.
Thanks,
-Eric
include/linux/slab.h +1 -0
@@ -56,6 +56,7 @@extern int kmem_cache_destroy (kmem_cache_t *);extern int kmem_cache_shrink (kmem_cache_t *);extern void *kmem_cache_alloc (kmem_cache_t *,int ); +extern void *kmem_cache_zalloc (kmem_cache_t *,int );extern void kmem_cache_free (kmem_cache_t *,void *);extern void *kmalloc (size_t ,int );
mm/slab.c +17 -0
@@ -1611,6 +1611,23 @@local_irq_restore (flags); } +void * +kmem_cache_zalloc (kmem_cache_t *cachep,int flags) +{ +void *ptr; + ptr =__kmem_cache_alloc (cachep, flags); + if (ptr) +#if DEBUG +memset (ptr,0 , cachep->objsize - + (cachep->flags &SLAB_RED_ZONE ?2 *BYTES_PER_WORD :0 )); +#else +memset (ptr,0 , cachep->objsize); +#endif + +return ptr; +} + +/** * kfree - free previously allocated memory * @objp: pointer returned by kmalloc.
kernel/ksyms.c +1 -0
@@ -102,6 +115,7 @@EXPORT_SYMBOL (kmem_cache_destroy);EXPORT_SYMBOL (kmem_cache_shrink);EXPORT_SYMBOL (kmem_cache_alloc); +EXPORT_SYMBOL (kmem_cache_zalloc);EXPORT_SYMBOL (kmem_cache_free);EXPORT_SYMBOL (kmalloc);EXPORT_SYMBOL (kfree);
Signature · 2 lines
Eric Sandeen XFS for Linux http://oss.sgi.com/projects/xfs [email protected] SGI, Inc.
Quoted 4 lines · In article <[email protected]> you wrote:
We'd like to incorporate this into the kernel proper, and several others chimed in that it would be useful, so here's the patch. If it's a no-go with Linus, we can roll this functionality back under fs/xfs to reduce our changes in the mainline kernel.
personally I liked the kcalloc suggesition more; that would fix a lot of multiplication exploitable bugs at the same time
Quoted 7 lines · Eric Sandeen
(reposting)
There was a brief thread on this patch a while ago, please see
http://www.uwsg.iu.edu/hypermail/linux/kernel/0203.3/0601.htmlIn short, XFS is using a
kmem_cache_zalloc()function which just doeskmem_cache_alloc+ memset.
Hi Eric,
I don't think kmem_cache_zalloc is a good idea. The idea behind the slab cache is to allow object reuse by storing constructed objects in the caches, and a memset directly after the alloc destroys the object state. A kmen_zalloc/kzalloc might make more sense.
Hi Christoph -
Quoted 4 lines · Christoph Hellwig
I don't think
kmem_cache_zallocis a good idea. The idea behind the slab cache is to allow object reuse by storing constructed objects in the caches, and a memset directly after the alloc destroys the object state. A kmen_zalloc/kzalloc might make more sense.
The constructor is one part of it, but what about more efficient memory use? If we let things fall into the default power-of-two caches, we'd waste quite a lot of memory.
See http://oss.sgi.com/~sandeen/slabinfo.html for example.
On this machine we'd use 30% more memory by using kmem_alloc vs kmem_cache_alloc.
-Eric
Signature · 2 lines
Eric Sandeen XFS for Linux http://oss.sgi.com/projects/xfs [email protected] SGI, Inc.
Quoted 2 lines
caches, and a memset directly after the alloc destroys the object state. A kmen_zalloc/kzalloc might make more sense.
s/kzalloc/kcalloc/
Then our api continues to remind people of the C one so is easier to remember (and we need calloc stuff anyway in multiple places)