From aa71a02cf3629795f761e3234e8bb252c76d3e78 Mon Sep 17 00:00:00 2001 From: Fangzheng Zhang Date: Fri, 24 Nov 2023 11:05:14 +0800 Subject: [PATCH] ANDROID: vendor_hooks: mm: add hook to count the number pages allocated for each slab Add the tracing interface on the kmalloc_large allocation path, which can detect the number of pages allocated by the slab, and if exceeds a threshold, trigger a panic or other actions. Bug: 312897430 Change-Id: I5575d0e4f91dab1c6e074f3e907fee8ea9327fd7 Signed-off-by: Fangzheng Zhang --- drivers/android/vendor_hooks.c | 1 + include/trace/hooks/mm.h | 3 +++ mm/slab_common.c | 2 ++ 3 files changed, 6 insertions(+) diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index 71a349221f3f..8425e8709b41 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -363,3 +363,4 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_filemap_get_folio); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mmc_blk_mq_rw_recovery); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_sd_update_bus_speed_mode); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_slab_folio_alloced); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_kmalloc_large_alloced); diff --git a/include/trace/hooks/mm.h b/include/trace/hooks/mm.h index 6b9353695660..0bd0c34e17b9 100644 --- a/include/trace/hooks/mm.h +++ b/include/trace/hooks/mm.h @@ -199,6 +199,9 @@ DECLARE_HOOK(android_vh_adjust_kvmalloc_flags, DECLARE_HOOK(android_vh_slab_folio_alloced, TP_PROTO(unsigned int order, gfp_t flags), TP_ARGS(order, flags)); +DECLARE_HOOK(android_vh_kmalloc_large_alloced, + TP_PROTO(struct page *page, unsigned int order, gfp_t flags), + TP_ARGS(page, order, flags)); #endif /* _TRACE_HOOK_MM_H */ /* This part must be outside protection */ diff --git a/mm/slab_common.c b/mm/slab_common.c index cfbfb2c580ea..f64ebceb848c 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1102,6 +1102,8 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node) PAGE_SIZE << order); } + trace_android_vh_kmalloc_large_alloced(page, order, flags); + ptr = kasan_kmalloc_large(ptr, size, flags); /* As ptr might get tagged, call kmemleak hook after KASAN. */ kmemleak_alloc(ptr, size, 1, flags);