Re: [PATCH] kmemleak: survive in a low-memory situation

From: Catalin Marinas
Date: Wed Jan 02 2019 - 11:59:39 EST


Hi Qian,

On Wed, Jan 02, 2019 at 11:08:49AM -0500, Qian Cai wrote:
> Kmemleak could quickly fail to allocate an object structure and then
> disable itself in a low-memory situation. For example, running a mmap()
> workload triggering swapping and OOM [1].
>
> First, it unnecessarily attempt to allocate even though the tracking
> object is NULL in kmem_cache_alloc(). For example,
>
> alloc_io
> bio_alloc_bioset
> mempool_alloc
> mempool_alloc_slab
> kmem_cache_alloc
> slab_alloc_node
> __slab_alloc <-- could return NULL
> slab_post_alloc_hook
> kmemleak_alloc_recursive

kmemleak_alloc() only continues with the kmemleak_object allocation if
the given pointer is not NULL.

> diff --git a/mm/slab.h b/mm/slab.h
> index 4190c24ef0e9..51a9a942cc56 100644
> --- a/mm/slab.h
> +++ b/mm/slab.h
> @@ -435,15 +435,16 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
> {
> size_t i;
>
> - flags &= gfp_allowed_mask;
> - for (i = 0; i < size; i++) {
> - void *object = p[i];
> -
> - kmemleak_alloc_recursive(object, s->object_size, 1,
> - s->flags, flags);
> - p[i] = kasan_slab_alloc(s, object, flags);
> + if (*p) {
> + flags &= gfp_allowed_mask;
> + for (i = 0; i < size; i++) {
> + void *object = p[i];
> +
> + kmemleak_alloc_recursive(object, s->object_size, 1,
> + s->flags, flags);
> + p[i] = kasan_slab_alloc(s, object, flags);
> + }
> }

This is not necessary for kmemleak.

--
Catalin