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

alloc_tag: prevent enabling memory profiling if it was shut down

Memory profiling can be shut down due to reasons like a failure during
initialization. When this happens, the user should not be able to
re-enable it. Current sysctrl interface does not handle this properly and
will allow re-enabling memory profiling. Fix this by checking for this
condition during sysctrl write operation.

Link: https://lkml.kernel.org/r/20250915212756.3998938-3-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Usama Arif <usamaarif642@gmail.com>
Cc: David Wang <00107082@163.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Sourav Panda <souravpanda@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Suren Baghdasaryan and committed by
Andrew Morton
9e8a0bbb 123bcf28

+15 -1
+15 -1
lib/alloc_tag.c
··· 766 766 EXPORT_SYMBOL(page_alloc_tagging_ops); 767 767 768 768 #ifdef CONFIG_SYSCTL 769 + /* 770 + * Not using proc_do_static_key() directly to prevent enabling profiling 771 + * after it was shut down. 772 + */ 773 + static int proc_mem_profiling_handler(const struct ctl_table *table, int write, 774 + void *buffer, size_t *lenp, loff_t *ppos) 775 + { 776 + if (!mem_profiling_support && write) 777 + return -EINVAL; 778 + 779 + return proc_do_static_key(table, write, buffer, lenp, ppos); 780 + } 781 + 782 + 769 783 static struct ctl_table memory_allocation_profiling_sysctls[] = { 770 784 { 771 785 .procname = "mem_profiling", ··· 789 775 #else 790 776 .mode = 0644, 791 777 #endif 792 - .proc_handler = proc_do_static_key, 778 + .proc_handler = proc_mem_profiling_handler, 793 779 }, 794 780 }; 795 781