Skip to contentSkip to conversation

alloc_tag: check mem_profiling_support in alloc_tag_init

PATCH Superseded Casey Chen 2 participants 4 messages May 12, 2025 – May 13, 2025

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Casey Chen

author
#0 lore raw

PATCH v3 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..7c798778669c 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 -EAGAIN;
+	}
+
 	res = alloc_mod_tags_mem();
-	if (res)
+	if (res) {
+		pr_err("Failed to allocate module tags\n");
+		shutdown_mem_profiling(true);
 		return res;
+	}
 
 	alloc_tag_cttype = codetag_register_type(&desc);
 	if (IS_ERR(alloc_tag_cttype)) {
+		pr_err("Failed to register codetag type with desc %s\n", desc.section);
 		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 36 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.

---
 lib/alloc_tag.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
lib/alloc_tag.c +0 -11
index 25ecc1334b67..7c798778669c 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)
        };

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 761

Quoted 10 lines
lib/alloc_tag.c +8 -0
        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)) {

I suggest keeping file creation as the last step so that we don't have to remove it if later steps fail.

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 764

Quoted 3 lines
lib/alloc_tag.c +3 -0
+               pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
+               shutdown_mem_profiling(false);
+               return -EAGAIN;

Why EAGAIN? I would just let it fall through returning 0 (after moving this to the end as I suggested in my previous comment).

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 769

Quoted 6 lines
lib/alloc_tag.c +4 -1
+       }
+
        res = alloc_mod_tags_mem();
-       if (res)
+       if (res) {
+               pr_err("Failed to allocate module tags\n");

The error message is a bit misleading here since we are not allocating module tags but reserving virtual space for them. Maybe:
"Failed to reserve address space for module tags"

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 776

Quoted 7 lines
lib/alloc_tag.c +3 -0
+               shutdown_mem_profiling(true);
                return res;
+       }
 
        alloc_tag_cttype = codetag_register_type(&desc);
        if (IS_ERR(alloc_tag_cttype)) {
+               pr_err("Failed to register codetag type with desc %s\n", desc.section);

pr_err("Allocation tags registration failed, errno = %d\n", ret);

Quoted 14 lines
lib/alloc_tag.c +1 -4
                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

author
#2 lore raw
Quoted 40 lines · Suren Baghdasaryan
Quoted 38 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.

---
 lib/alloc_tag.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
lib/alloc_tag.c +0 -11
index 25ecc1334b67..7c798778669c 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;
 
Quoted 12 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 761

Quoted 8 lines
lib/alloc_tag.c +8 -0
+       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)) {

I suggest keeping file creation as the last step so that we don't have to remove it if later steps fail.

If the file creation is the last step, how could it clean up codetag type registration ? As we miss codetag unregistration function, I think codetag type registration should be the last step.

Quoted 7 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 764

Quoted 3 lines
lib/alloc_tag.c +3 -0
+               pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
+               shutdown_mem_profiling(false);
+               return -EAGAIN;

Why EAGAIN? I would just let it fall through returning 0 (after moving this to the end as I suggested in my previous comment).

return 0 is fine to me if this fails. EAGAIN sounds fine too.

Quoted 11 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 769

Quoted 6 lines
lib/alloc_tag.c +4 -1
+       }
+
        res = alloc_mod_tags_mem();
-       if (res)
+       if (res) {
+               pr_err("Failed to allocate module tags\n");

The error message is a bit misleading here since we are not allocating module tags but reserving virtual space for them. Maybe:
"Failed to reserve address space for module tags"

Sounds good!

Quoted 10 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 776

Quoted 7 lines
lib/alloc_tag.c +3 -0
+               shutdown_mem_profiling(true);
                return res;
+       }
 
        alloc_tag_cttype = codetag_register_type(&desc);
        if (IS_ERR(alloc_tag_cttype)) {
+               pr_err("Failed to register codetag type with desc %s\n", desc.section);

pr_err("Allocation tags registration failed, errno = %d\n", ret);

Sounds good too.

Quoted 14 lines
Quoted 14 lines
lib/alloc_tag.c +1 -4
                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

#3 lore raw
Quoted 55 lines · Casey Chen
Quoted 40 lines · Suren Baghdasaryan
Quoted 38 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.

---
 lib/alloc_tag.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
lib/alloc_tag.c +0 -11
index 25ecc1334b67..7c798778669c 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;
 
Quoted 12 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 761

Quoted 8 lines
lib/alloc_tag.c +8 -0
+       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)) {

I suggest keeping file creation as the last step so that we don't have to remove it if later steps fail.

Quoted 3 lines

If the file creation is the last step, how could it clean up codetag type registration ? As we miss codetag unregistration function, I think codetag type registration should be the last step.

I see. Ok, keeping it here sounds reasonable. Implementing codetag_unregister_type() just for this failure case would not make sense. Please go ahead and address the other nits and I think it will be good to go.

Quoted 52 lines
Quoted 8 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 764

Quoted 4 lines
lib/alloc_tag.c +3 -0
+               pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
+               shutdown_mem_profiling(false);
+               return -EAGAIN;

Why EAGAIN? I would just let it fall through returning 0 (after moving this to the end as I suggested in my previous comment).

return 0 is fine to me if this fails. EAGAIN sounds fine too.

Quoted 11 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 769

Quoted 6 lines
lib/alloc_tag.c +4 -1
+       }
+
        res = alloc_mod_tags_mem();
-       if (res)
+       if (res) {
+               pr_err("Failed to allocate module tags\n");

The error message is a bit misleading here since we are not allocating module tags but reserving virtual space for them. Maybe:
"Failed to reserve address space for module tags"

Sounds good!

Quoted 10 lines

lib/alloc_tag.c static int __init alloc_tag_init(void) · line 776

Quoted 7 lines
lib/alloc_tag.c +3 -0
+               shutdown_mem_profiling(true);
                return res;
+       }
 
        alloc_tag_cttype = codetag_register_type(&desc);
        if (IS_ERR(alloc_tag_cttype)) {
+               pr_err("Failed to register codetag type with desc %s\n", desc.section);

pr_err("Allocation tags registration failed, errno = %d\n", ret);

Sounds good too.

Quoted 14 lines
Quoted 14 lines
lib/alloc_tag.c +1 -4
                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