[PATCH v3 0/3] mm/slub: some debug enhancements

Feng Tang
2022-07-27T07:10:54Z

kmalloc's API family is critical for mm, and one of its nature is that
it will round up the request size to a fixed one (mostly power of 2).
When user requests memory for '2^n + 1' bytes, actually 2^(n+1) bytes
could be allocated, so in worst case, there is around 50% memory space
waste.

The wastage is not a big issue for requests that get allocated/freed
quickly, but may cause problems with objects that have longer life time,
and there were some OOM cases in some extrem cases.

This patchset tries to :
* Add a debug method to track each kmalloced object's wastage info,
and show the call stack of original allocation
* Extend the redzone sanity check to the extra kmalloced buffer than
requested, to better detect un-legitimate access to it.

Please help to review, thanks!

- Feng

---
Changelogs:

since v2:
* rebased against slab tree's 'for-next' branch
* fix pointer handling (Kefeng Wang)
* move kzalloc zeroing handling change to a separate patch (Vlastimil Babka)
* make 'orig_size' only depend on KMALLOC & STORE_USER flag
bits (Vlastimil Babka)

since v1:
* limit the 'orig_size' to kmalloc objects only, and save
it after track in metadata (Vlastimil Babka)
* fix a offset calculation problem in print_trailer

since RFC:
* fix problems in kmem_cache_alloc_bulk() and records sorting,
improve the print format (Hyeonggon Yoo)
* fix a compiling issue found by 0Day bot
* update the commit log based info from iova developers

Feng Tang (3):
mm/slub: enable debugging memory wasting of kmalloc
mm/slub: only zero the requested size of buffer for kzalloc
mm/slub: extend redzone check to cover extra allocated kmalloc space
than requested

include/linux/slab.h | 2 +
mm/slab.c | 8 +--
mm/slab.h | 9 ++-
mm/slub.c | 157 ++++++++++++++++++++++++++++++++++++-------
4 files changed, 146 insertions(+), 30 deletions(-)

--
2.27.0