]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
alloc_tag: fix rw permission issue when handling boot parameter
authorRan Xiaokai <ran.xiaokai@zte.com.cn>
Thu, 15 Jan 2026 03:15:36 +0000 (03:15 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 31 Jan 2026 22:22:37 +0000 (14:22 -0800)
Boot parameters prefixed with "sysctl." are processed during the final
stage of system initialization via kernel_init()-> do_sysctl_args().  When
CONFIG_MEM_ALLOC_PROFILING_DEBUG is enabled, the sysctl.vm.mem_profiling
entry is not writable and will cause a warning.

Before run_init_process(), system initialization executes in kernel thread
context.  Use current->mm to distinguish sysctl writes during
do_sysctl_args() from user-space triggered ones.

And when the proc_handler is from do_sysctl_args(), always return success
because the same value was already set by setup_early_mem_profiling() and
this eliminates a permission denied warning.

Link: https://lkml.kernel.org/r/20260115031536.164254-1-ranxiaokai627@163.com
Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
Suggested-by: Suren Baghdasaryan <surenb@google.com>
Acked-by: Suren Baghdasaryan <surenb@google.com>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
lib/alloc_tag.c

index 846a5b5b44a4e5f530605c97a200e03260e73d77..00ae4673a271732ee72efe8434df37645dfc3375 100644 (file)
@@ -776,8 +776,22 @@ EXPORT_SYMBOL(page_alloc_tagging_ops);
 static int proc_mem_profiling_handler(const struct ctl_table *table, int write,
                                      void *buffer, size_t *lenp, loff_t *ppos)
 {
-       if (!mem_profiling_support && write)
-               return -EINVAL;
+       if (write) {
+               /*
+                * Call from do_sysctl_args() which is a no-op since the same
+                * value was already set by setup_early_mem_profiling.
+                * Return success to avoid warnings from do_sysctl_args().
+                */
+               if (!current->mm)
+                       return 0;
+
+#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
+               /* User can't toggle profiling while debugging */
+               return -EACCES;
+#endif
+               if (!mem_profiling_support)
+                       return -EINVAL;
+       }
 
        return proc_do_static_key(table, write, buffer, lenp, ppos);
 }
@@ -787,11 +801,7 @@ static const struct ctl_table memory_allocation_profiling_sysctls[] = {
        {
                .procname       = "mem_profiling",
                .data           = &mem_alloc_profiling_key,
-#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
-               .mode           = 0444,
-#else
                .mode           = 0644,
-#endif
                .proc_handler   = proc_mem_profiling_handler,
        },
 };