Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

alloc_tag: introduce shutdown_mem_profiling helper function

Implement a helper function to disable memory allocation profiling and use
it when creation of /proc/allocinfo fails. Ensure /proc/allocinfo does
not get created when memory allocation profiling is disabled.

Link: https://lkml.kernel.org/r/20241023170759.999909-3-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: David Rientjes <rientjes@google.com>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Liam R. Howlett <Liam.Howlett@Oracle.com>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
Cc: Minchan Kim <minchan@google.com>
Cc: Paul E. McKenney <paulmck@kernel.org>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Sourav Panda <souravpanda@google.com>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Huth <thuth@redhat.com>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Xiongwei Song <xiongwei.song@windriver.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Suren Baghdasaryan and committed by
Andrew Morton
3e09c500 7c8c76e4

+26 -7
+26 -7
lib/alloc_tag.c
··· 8 8 #include <linux/seq_buf.h> 9 9 #include <linux/seq_file.h> 10 10 11 + #define ALLOCINFO_FILE_NAME "allocinfo" 12 + 13 + #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT 14 + static bool mem_profiling_support __meminitdata = true; 15 + #else 16 + static bool mem_profiling_support __meminitdata; 17 + #endif 18 + 11 19 static struct codetag_type *alloc_tag_cttype; 12 20 13 21 DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); ··· 152 144 return nr; 153 145 } 154 146 147 + static void __init shutdown_mem_profiling(void) 148 + { 149 + if (mem_alloc_profiling_enabled()) 150 + static_branch_disable(&mem_alloc_profiling_key); 151 + 152 + if (!mem_profiling_support) 153 + return; 154 + 155 + mem_profiling_support = false; 156 + } 157 + 155 158 static void __init procfs_init(void) 156 159 { 157 - proc_create_seq("allocinfo", 0400, NULL, &allocinfo_seq_op); 160 + if (!mem_profiling_support) 161 + return; 162 + 163 + if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) { 164 + pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME); 165 + shutdown_mem_profiling(); 166 + } 158 167 } 159 168 160 169 static bool alloc_tag_module_unload(struct codetag_type *cttype, ··· 198 173 199 174 return module_unused; 200 175 } 201 - 202 - #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT 203 - static bool mem_profiling_support __meminitdata = true; 204 - #else 205 - static bool mem_profiling_support __meminitdata; 206 - #endif 207 176 208 177 static int __init setup_early_mem_profiling(char *str) 209 178 {