Re: [PATCH] slab: introduce kmem_cache_zalloc allocator

From: Balbir Singh
Date: Mon Mar 20 2006 - 11:03:15 EST


<snip>

> /**
> + * kmem_cache_alloc - Allocate an object. The memory is set to zero.
> + * @cache: The cache to allocate from.
> + * @flags: See kmalloc().
> + *
> + * Allocate an object from this cache and set the allocated memory to zero.
> + * The flags are only relevant if the cache has no available objects.
> + */
> +void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
> +{
> + void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
> + if (ret)
> + memset(ret, 0, obj_size(cache));
> + return ret;
> +}
> +EXPORT_SYMBOL(kmem_cache_zalloc);
> +
> +/**
> * kmem_ptr_validate - check if an untrusted pointer might
> * be a slab entry.
> * @cachep: the cache we're checking against
> diff --git a/mm/slob.c b/mm/slob.c
> index a1f42bd..9bcc7e2 100644
> --- a/mm/slob.c
> +++ b/mm/slob.c
> @@ -294,6 +294,16 @@ void *kmem_cache_alloc(struct kmem_cache
> }
> EXPORT_SYMBOL(kmem_cache_alloc);
>
> +void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
> +{
> + void *ret = kmem_cache_alloc(c, flags);
> + if (ret)
> + memset(ret, 0, c->size);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL(kmem_cache_zalloc);
> +
> void kmem_cache_free(struct kmem_cache *c, void *b)
> {
> if (c->dtor)

Could we please create a more generic variation of this patch -- may be a
function called kmem_cache_alloc_set(). The function would not only
memset the data to 0, but instead to any specified pattern passed as
an argument.

This could be used to poison allocated memory. Passing 0 would make
this equivalent to kmem_cache_zalloc(). Basically, instead of doing

mem = __cache_alloc(...)
memset(mem, 0, size)

I would prefer if we could have

mem = __cache_alloc(...)
memset(mem, X, size)

Balbir
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/