Skip to contentSkip to conversation

kmem_cache_zalloc()

PATCH In review Eric Sandeen 3 participants 3 messages Apr 3, 2002

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kmem_cache_zalloc()

Welcome back Linus -

There was a brief thread on this patch while you were gone, please see
http://www.uwsg.iu.edu/hypermail/linux/kernel/0203.3/0601.html

In short, we're using a kmem_cache_zalloc() function in XFS 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 you, we can roll this functionality back under fs/xfs to reduce our changes in the mainline kernel.

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.
#1 lore raw

Uttered "Eric Sandeen" <[email protected]>, spoke thus:

Quoted 7 lines

In short, we're using a kmem_cache_zalloc() function in XFS 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 you, we can roll this functionality back under fs/xfs to reduce our changes in the mainline kernel.

Why not use the constructor function interface to kmem_cache_create that is _already_ in the kernel API?

#2 lore raw
In reply to Eric Sandeen
Quoted 6 lines · Tommy Reynolds

Uttered "Eric Sandeen" <[email protected]>, spoke thus:

Quoted 3 lines

In short, we're using a kmem_cache_zalloc() function in XFS which just does kmem_cache_alloc + memset.

Quoted 7 lines
Quoted 4 lines

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 you, we can roll this functionality back under fs/xfs to reduce our changes in the mainline kernel.

Why not use the constructor function interface to kmem_cache_create that is _already_ in the kernel API?

Constructors are only called once when the slab is allocated. It is expected that objects are returned to the slab in the same state.

I think a better idea would be to add a flag to the cache that tells kmem_cache_alloc() to zero out the object it allocates instead of creating another interface.

--

Brian Gerst