Re: [PATCH 9/9] mm: kmemleak: fix undetected leaks for page aligned objects

Matthew Wilcox
2023-01-24T16:16:38Z

On Mon, Jan 23, 2023 at 11:04:19AM -0600, George Prekas wrote:
> If kmalloc returns a page aligned object, then the object has 2
> references: the pointer returned by kmalloc and page->s_mem of the first
> page of the object. Account for this extra reference, so that kmemleak
> can correctly detect leaks for page aligned objects.

Is this true for multi-page slabs? Imagine if you allocate objects of
size 4kB. Slab/Slub will allocate slabs of size 32kB and try to
allocate 8 objects from each slab. Only the first one will have an
s_mem pointing into it. No?

> - kmemleak_alloc_recursive(p[i], s->object_size, 1,
> + min_count = 1;
> + /* If p[i] is page aligned, then a page->s_mem refers to it. */

Also this comment should read 'slab->s_mem'. Individual pages within
the slab do not have s_mem pointers, only the slab.

> + if (((uintptr_t)p[i] & ~PAGE_MASK) == 0)
> + min_count++;
> + kmemleak_alloc_recursive(p[i], s->object_size, min_count,
> s->flags, flags);