Skip to contentSkip to conversation

alloc_tag: check mem_profiling_support in alloc_tag_init

PATCH Applied Casey Chen 2 participants 2 messages May 13, 2025

Subsystems (2)
lore
Share & tools
lore

Casey Chen

author
#0 lore raw

PATCH v5 1/1 alloc_tag: check mem_profiling_support in alloc_tag_init

If mem_profiling_support is false, for example by sysctl.vm.mem_profiling=never, alloc_tag_init should skip module tags allocation, codetag type registration and procfs init.

---
 lib/alloc_tag.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
lib/alloc_tag.c +19 -15
index 25ecc1334b67..3baf19879c8e 100644
@@ -244,17 +244,6 @@ static void shutdown_mem_profiling(bool remove_file)
 	mem_profiling_support = false;
 }
 
-static void __init procfs_init(void)
-{
-	if (!mem_profiling_support)
-		return;
-
-	if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) {
-		pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
-		shutdown_mem_profiling(false);
-	}
-}
-
 void __init alloc_tag_sec_init(void)
 {
 	struct alloc_tag *last_codetag;
@@ -762,19 +751,34 @@ static int __init alloc_tag_init(void)
 	};
 	int res;
 
+	sysctl_init();
+
+	if (!mem_profiling_support) {
+		pr_info("Memory allocation profiling is not supported!\n");
+		return 0;
+	}
+
+	if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) {
+		pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
+		shutdown_mem_profiling(false);
+		return -ENOMEM;
+	}
+
 	res = alloc_mod_tags_mem();
-	if (res)
+	if (res) {
+		pr_err("Failed to reserve address space for module tags, errno = %d\n", res);
+		shutdown_mem_profiling(true);
 		return res;
+	}
 
 	alloc_tag_cttype = codetag_register_type(&desc);
 	if (IS_ERR(alloc_tag_cttype)) {
+		pr_err("Allocation tags registration failed, errno = %ld\n", PTR_ERR(alloc_tag_cttype));
 		free_mod_tags_mem();
+		shutdown_mem_profiling(true);
 		return PTR_ERR(alloc_tag_cttype);
 	}
 
-	sysctl_init();
-	procfs_init();
-
 	return 0;
 }
 module_init(alloc_tag_init);
Signature · 1 line
2.49.0
#1 lore raw
Quoted 9 lines · Casey Chen

If mem_profiling_support is false, for example by sysctl.vm.mem_profiling=never, alloc_tag_init should skip module tags allocation, codetag type registration and procfs init.

Sending to Andrew as I just noticed the patch was not sent to him initially.

I don't think we need to CC stable as IMO this is more of an optimization than a bug fix.

Quoted 68 lines
---
 lib/alloc_tag.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
lib/alloc_tag.c +19 -16
index 25ecc1334b67..3baf19879c8e 100644
@@ -244,17 +244,6 @@ static void shutdown_mem_profiling(bool remove_file)
        mem_profiling_support = false;
 }
 
-static void __init procfs_init(void)
-{
-       if (!mem_profiling_support)
-               return;
-
-       if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) {
-               pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
-               shutdown_mem_profiling(false);
-       }
-}
-
 void __init alloc_tag_sec_init(void)
 {
        struct alloc_tag *last_codetag;
@@ -762,19 +751,34 @@ static int __init alloc_tag_init(void)
        };
        int res;
 
+       sysctl_init();
+
+       if (!mem_profiling_support) {
+               pr_info("Memory allocation profiling is not supported!\n");
+               return 0;
+       }
+
+       if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) {
+               pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
+               shutdown_mem_profiling(false);
+               return -ENOMEM;
+       }
+
        res = alloc_mod_tags_mem();
-       if (res)
+       if (res) {
+               pr_err("Failed to reserve address space for module tags, errno = %d\n", res);
+               shutdown_mem_profiling(true);
                return res;
+       }
 
        alloc_tag_cttype = codetag_register_type(&desc);
        if (IS_ERR(alloc_tag_cttype)) {
+               pr_err("Allocation tags registration failed, errno = %ld\n", PTR_ERR(alloc_tag_cttype));
                free_mod_tags_mem();
+               shutdown_mem_profiling(true);
                return PTR_ERR(alloc_tag_cttype);
        }
 
-       sysctl_init();
-       procfs_init();
-
        return 0;
 }
 module_init(alloc_tag_init);
--

2.49.0