From d6a225656f0cf16f1292c3e0fb70403292758e85 Mon Sep 17 00:00:00 2001 From: Kathiravan Thirumoorthy Date: Wed, 13 Sep 2023 21:05:53 +0530 Subject: [PATCH] ipq: mem: Track memory usage selectively Track memory usage selectively for networking stack and wifi / eth device drivers. Change-Id: I8fdaf672ea6f207091ebb04cdbbf0121fe86a103 Signed-off-by: Kathiravan Thirumoorthy --- include/linux/debugobjects.h | 15 +++++++++++++++ include/linux/dmapool.h | 11 +++++++++++ include/linux/mm.h | 4 ++++ init/main.c | 1 + lib/Kconfig.debug | 4 ++++ lib/Makefile | 1 + mm/Makefile | 1 + mm/dmapool.c | 11 ----------- scripts/Makefile.build | 33 +++++++++++++++++++++++++++++++++ 9 files changed, 70 insertions(+), 11 deletions(-) diff --git a/include/linux/debugobjects.h b/include/linux/debugobjects.h index 738f366dffeb..fb9abddbdb7e 100644 --- a/include/linux/debugobjects.h +++ b/include/linux/debugobjects.h @@ -106,6 +106,21 @@ static inline void debug_objects_early_init(void) { } static inline void debug_objects_mem_init(void) { } #endif +#ifdef CONFIG_DEBUG_MEM_USAGE +struct debug_obj_trace { + struct hlist_node node; + void *addr; + void *stack[9]; + int size; +}; + +extern void debug_object_trace_init(void *addr, void **stack, size_t size); +extern void debug_object_trace_free(void *addr); +extern void debug_mem_usage_init(void); +#else +static inline void debug_mem_usage_init(void) { } +#endif + #ifdef CONFIG_DEBUG_OBJECTS_FREE extern void debug_check_no_obj_freed(const void *address, unsigned long size); #else diff --git a/include/linux/dmapool.h b/include/linux/dmapool.h index f632ecfb4238..7cd9ed9f78a1 100644 --- a/include/linux/dmapool.h +++ b/include/linux/dmapool.h @@ -16,6 +16,17 @@ struct device; +struct dma_pool { /* the pool */ + struct list_head page_list; + spinlock_t lock; + size_t size; + struct device *dev; + size_t allocation; + size_t boundary; + char name[32]; + struct list_head pools; +}; + #ifdef CONFIG_HAS_DMA struct dma_pool *dma_pool_create(const char *name, struct device *dev, diff --git a/include/linux/mm.h b/include/linux/mm.h index e5e8acf8eb89..335e7efdc2e7 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3494,4 +3494,8 @@ madvise_set_anon_name(struct mm_struct *mm, unsigned long start, } #endif +#ifdef CONFIG_DEBUG_MEM_USAGE +extern int debug_mem_usage_enabled; +#endif + #endif /* _LINUX_MM_H */ diff --git a/init/main.c b/init/main.c index 88dd07e5c7d7..5154ee77f3bc 100644 --- a/init/main.c +++ b/init/main.c @@ -874,6 +874,7 @@ static void __init mm_init(void) kmemleak_init(); pgtable_init(); debug_objects_mem_init(); + debug_mem_usage_init(); vmalloc_init(); /* Should be run after vmap initialization */ if (early_page_ext_enabled()) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 8a3d0b3c91e7..203ac5283bcf 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -987,6 +987,10 @@ config DEBUG_STACKOVERFLOW If in doubt, say "N". +config DEBUG_MEM_USAGE + default n + bool "Track memory usage selectively" + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" diff --git a/lib/Makefile b/lib/Makefile index 3b00ad1f06b0..e80726633723 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -169,6 +169,7 @@ obj-$(CONFIG_ASSOCIATIVE_ARRAY) += assoc_array.o obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o obj-$(CONFIG_DEBUG_LIST) += list_debug.o obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o +obj-$(CONFIG_DEBUG_MEM_USAGE) += debug_mem_usage.o obj-$(CONFIG_BITREVERSE) += bitrev.o obj-$(CONFIG_LINEAR_RANGES) += linear_ranges.o diff --git a/mm/Makefile b/mm/Makefile index 8e105e5b3e29..a6aec06066ad 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -124,6 +124,7 @@ obj-$(CONFIG_SECRETMEM) += secretmem.o obj-$(CONFIG_CMA_SYSFS) += cma_sysfs.o obj-$(CONFIG_USERFAULTFD) += userfaultfd.o obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o +obj-$(CONFIG_DEBUG_MEM_USAGE) += wrap_mem_api.o obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o obj-$(CONFIG_DAMON) += damon/ obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o diff --git a/mm/dmapool.c b/mm/dmapool.c index a7eb5d0eb2da..3e0d2c7cdabc 100644 --- a/mm/dmapool.c +++ b/mm/dmapool.c @@ -40,17 +40,6 @@ #define DMAPOOL_DEBUG 1 #endif -struct dma_pool { /* the pool */ - struct list_head page_list; - spinlock_t lock; - size_t size; - struct device *dev; - size_t allocation; - size_t boundary; - char name[32]; - struct list_head pools; -}; - struct dma_page { /* cacheable header for 'allocation' bytes */ struct list_head page_list; void *vaddr; diff --git a/scripts/Makefile.build b/scripts/Makefile.build index ec533c08f37b..60f625c9eab9 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -157,10 +157,43 @@ ifdef CONFIG_LTO_CLANG cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@) endif +ifdef CONFIG_DEBUG_MEM_USAGE +CFLAGS_WRAP := -include include/linux/debug_mem_usage.h +NEW_CFLAGS_WRAP := $(CFLAGS_WRAP) + +cmd_export_new_cflags = \ + $(if $(findstring kernel/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring arch/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring init/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring mm/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring block/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring kernel/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring block/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring usr/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring security/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring sound/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring crypto/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring firmware/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring samples/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring tools/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring lib/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring fs/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring ipc/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring drivers/, $@), $(filter-out $(NEW_CFLAGS_WRAP), $(CFLAGS_WRAP))) \ + $(if $(findstring drivers/net/, $@), $(CFLAGS_WRAP)) \ + $(if $(findstring net/, $@), $(CFLAGS_WRAP)) \ + $(if $(findstring /linux-ipq, $@), $(CFLAGS_WRAP)) + +quiet_cmd_cc_o_c = CC $(quiet_modtag) $@ + cmd_cc_o_c = $(CC) $(c_flags) $(cmd_export_new_cflags) -c -o $@ $< \ + $(cmd_ld_single_m) \ + $(cmd_objtool) +else quiet_cmd_cc_o_c = CC $(quiet_modtag) $@ cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \ $(cmd_ld_single_m) \ $(cmd_objtool) +endif ifdef CONFIG_MODVERSIONS # When module versioning is enabled the following steps are executed: -- 2.34.1