Skip to contentSkip to conversation

Minor fixes for memory allocation profiling

PATCH Superseded Suren Baghdasaryan 3 participants 15 messages Sep 9, 2025 – Sep 12, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Suren Baghdasaryan

author reviewer
#0 lore raw

cover Minor fixes for memory allocation profiling

Over the last couple months I gathered a few reports of minor issues in memory allocation profiling which are addressed in this patchset.

Patchset is based on mm-new.

Suren Baghdasaryan (3):
alloc_tag: use release_pages() in the cleanup path
alloc_tag: prevent enabling memory profiling if it was shut down
alloc_tag: avoid warnings when freeing non-compound "tail" pages

 lib/alloc_tag.c | 17 ++++++++++++++---
 mm/page_alloc.c |  9 ++++++++-
 2 files changed, 22 insertions(+), 4 deletions(-)

base-commit: f4e8f46973fe0c0f579944a37e96ba9efbe00cca

Signature · 1 line
2.51.0.384.g4c02a37b29-goog
author reviewer
#1 lore raw

PATCH 1/3 alloc_tag: use release_pages() in the cleanup path

When bulk-freeing an array of pages use release_pages() instead of freeing them page-by-page:

---
 lib/alloc_tag.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/alloc_tag.c +3 -2
index e9b33848700a..95688c4cba7a 100644
@@ -438,9 +438,10 @@ static int vm_module_tags_populate(void)
 		if (nr < more_pages ||
 		    vmap_pages_range(phys_end, phys_end + (nr << PAGE_SHIFT), PAGE_KERNEL,
 				     next_page, PAGE_SHIFT) < 0) {
+			release_pages_arg arg = { .pages = next_page };
+
 			/* Clean up and error out */
-			for (int i = 0; i < nr; i++)
-				__free_page(next_page[i]);
+			release_pages(arg, nr);
 			return -ENOMEM;
 		}
 
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Acked-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] alloc_tag: use release_pages() in the cleanup path

In reply to Suren Baghdasaryan
Quoted 9 lines · Suren Baghdasaryan

When bulk-freeing an array of pages use release_pages() instead of freeing them page-by-page:

---
 lib/alloc_tag.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 7 lines
lib/alloc_tag.c +0 -0
index e9b33848700a..95688c4cba7a 100644
@@ -438,9 +438,10 @@ static int vm_module_tags_populate(void)
 		if (nr < more_pages ||
 		    vmap_pages_range(phys_end, phys_end + (nr << PAGE_SHIFT), PAGE_KERNEL,

lib/alloc_tag.c static int vm_module_tags_populate(void) · line 447

Quoted 10 lines
lib/alloc_tag.c +3 -2
 				     next_page, PAGE_SHIFT) < 0) {
+			release_pages_arg arg = { .pages = next_page };
+
 			/* Clean up and error out */
-			for (int i = 0; i < nr; i++)
-				__free_page(next_page[i]);
+			release_pages(arg, nr);
 			return -ENOMEM;
 		}
 

Maybe this can be done in free_mod_tags_mem as well?

author reviewer
#4 lore raw

Re: [PATCH 1/3] alloc_tag: use release_pages() in the cleanup path

Quoted 27 lines · Usama Arif
Quoted 9 lines · Suren Baghdasaryan

When bulk-freeing an array of pages use release_pages() instead of freeing them page-by-page:

---
 lib/alloc_tag.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 11 lines
lib/alloc_tag.c +2 -0
index e9b33848700a..95688c4cba7a 100644
@@ -438,9 +438,10 @@ static int vm_module_tags_populate(void)
              if (nr < more_pages ||
                  vmap_pages_range(phys_end, phys_end + (nr << PAGE_SHIFT), PAGE_KERNEL,
                                   next_page, PAGE_SHIFT) < 0) {
+                     release_pages_arg arg = { .pages = next_page };
+
                      /* Clean up and error out */
Quoted 8 lines

lib/alloc_tag.c static int vm_module_tags_populate(void) · line 447

Quoted 6 lines
lib/alloc_tag.c +1 -2
-                     for (int i = 0; i < nr; i++)
-                             __free_page(next_page[i]);
+                     release_pages(arg, nr);
                      return -ENOMEM;
              }
 

Maybe this can be done in free_mod_tags_mem as well?

Good point. I think I will respin this patchset with your suggestions
and will include one more fix for an issue that Shakeel just reported
(see https://lore.kernel.org/all/CAJuCfpGUjaZcs1r9ADKck_Ni7f41kHaiejR01Z0bE8pG0K1uXA@mail.gmail.com/).
Thanks!
Suren.

author reviewer
#5 lore raw

PATCH 2/3 alloc_tag: prevent enabling memory profiling if it was shut down

In reply to Suren Baghdasaryan

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.

---
 lib/alloc_tag.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
lib/alloc_tag.c +11 -1
index 95688c4cba7a..79891528e7b6 100644
@@ -767,6 +767,16 @@ struct page_ext_operations page_alloc_tagging_ops = {
 EXPORT_SYMBOL(page_alloc_tagging_ops);
 
 #ifdef CONFIG_SYSCTL
+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;
+
+	return proc_do_static_key(table, write, buffer, lenp, ppos);
+}
+
+
 static struct ctl_table memory_allocation_profiling_sysctls[] = {
 	{
 		.procname	= "mem_profiling",
@@ -776,7 +786,7 @@ static struct ctl_table memory_allocation_profiling_sysctls[] = {
 #else
 		.mode		= 0644,
 #endif
-		.proc_handler	= proc_do_static_key,
+		.proc_handler	= proc_mem_profiling_handler,
 	},
 };
 
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Acked-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] alloc_tag: prevent enabling memory profiling if it was shut down

Quoted 7 lines · Suren Baghdasaryan

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.

Acked-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] alloc_tag: prevent enabling memory profiling if it was shut down

In reply to Suren Baghdasaryan
Quoted 11 lines · Suren Baghdasaryan

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.

---
 lib/alloc_tag.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

lib/alloc_tag.c struct page_ext_operations page_alloc_tagging_ops = { · line 769

Quoted 8 lines
lib/alloc_tag.c +0 -0
index 95688c4cba7a..79891528e7b6 100644
@@ -767,6 +767,16 @@ struct page_ext_operations page_alloc_tagging_ops = {
 EXPORT_SYMBOL(page_alloc_tagging_ops);
 
 #ifdef CONFIG_SYSCTL

nit: might be good to add a comment here that we are not using default as we dont want to enable it back if it fails once. But not worth a respin as someone can always check git log if curious.

Quoted 22 lines
lib/alloc_tag.c +11 -1
+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;
+
+	return proc_do_static_key(table, write, buffer, lenp, ppos);
+}
+
+
 static struct ctl_table memory_allocation_profiling_sysctls[] = {
 	{
 		.procname	= "mem_profiling",
@@ -776,7 +786,7 @@ static struct ctl_table memory_allocation_profiling_sysctls[] = {
 #else
 		.mode		= 0644,
 #endif
-		.proc_handler	= proc_do_static_key,
+		.proc_handler	= proc_mem_profiling_handler,
 	},
 };
 
author reviewer
#8 lore raw

Re: [PATCH 2/3] alloc_tag: prevent enabling memory profiling if it was shut down

Quoted 27 lines · Usama Arif
Quoted 11 lines · Suren Baghdasaryan

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.

---
 lib/alloc_tag.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
Quoted 7 lines
lib/alloc_tag.c +0 -0
index 95688c4cba7a..79891528e7b6 100644
@@ -767,6 +767,16 @@ struct page_ext_operations page_alloc_tagging_ops = {
 EXPORT_SYMBOL(page_alloc_tagging_ops);
 
Quoted 4 lines

lib/alloc_tag.c struct page_ext_operations page_alloc_tagging_ops = { · line 769

Quoted 1 line
lib/alloc_tag.c +0 -0
 #ifdef CONFIG_SYSCTL

nit: might be good to add a comment here that we are not using default as we dont want to enable it back if it fails once. But not worth a respin as someone can always check git log if curious.

Ok, if I respin the series will add it. Thanks!

Quoted 24 lines
Quoted 23 lines
lib/alloc_tag.c +11 -1
+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;
+
+     return proc_do_static_key(table, write, buffer, lenp, ppos);
+}
+
+
 static struct ctl_table memory_allocation_profiling_sysctls[] = {
      {
              .procname       = "mem_profiling",
@@ -776,7 +786,7 @@ static struct ctl_table memory_allocation_profiling_sysctls[] = {
 #else
              .mode           = 0644,
 #endif
-             .proc_handler   = proc_do_static_key,
+             .proc_handler   = proc_mem_profiling_handler,
      },
 };
 
author reviewer
#9 lore raw

PATCH 3/3 alloc_tag: avoid warnings when freeing non-compound "tail" pages

In reply to Suren Baghdasaryan

When freeing "tail" pages of a non-compount high-order page, we properly subtract the allocation tag counters, however later when these pages are released, alloc_tag_sub() will issue warnings because tags for these pages are NULL.
This issue was originally anticipated by Vlastimil in his review [1] and then recently reported by David.
Prevent warnings by marking the tags empty.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/page_alloc.c +8 -1
index 1760346bbd24..d21a411e807e 100644
@@ -5240,9 +5240,16 @@ static void ___free_pages(struct page *page, unsigned int order,
 		__free_frozen_pages(page, order, fpi_flags);
 	else if (!head) {
 		pgalloc_tag_sub_pages(tag, (1 << order) - 1);
-		while (order-- > 0)
+		while (order-- > 0) {
+			/*
+			 * The "tail" pages of this non-compound high-order
+			 * page will have no code tags, so to avoid warnings
+			 * mark them as empty.
+			 */
+			clear_page_tag_ref(page + (1 << order));
 			__free_frozen_pages(page + (1 << order), order,
 					    fpi_flags);
+		}
 	}
 }
 
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
#10 lore raw

Re: [PATCH 3/3] alloc_tag: avoid warnings when freeing non-compound "tail" pages

Quoted 22 lines · Suren Baghdasaryan

When freeing "tail" pages of a non-compount high-order page, we properly subtract the allocation tag counters, however later when these pages are released, alloc_tag_sub() will issue warnings because tags for these pages are NULL.
This issue was originally anticipated by Vlastimil in his review [1] and then recently reported by David.
Prevent warnings by marking the tags empty.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index 1760346bbd24..d21a411e807e 100644
@@ -5240,9 +5240,16 @@ static void ___free_pages(struct page *page, unsigned int order,
 		__free_frozen_pages(page, order, fpi_flags);

mm/page_alloc.c static void ___free_pages(struct page *page, unsigned int order, · line 5249

Quoted 10 lines
mm/page_alloc.c +7 -1
 	else if (!head) {
 		pgalloc_tag_sub_pages(tag, (1 << order) - 1);
-		while (order-- > 0)
+		while (order-- > 0) {
+			/*
+			 * The "tail" pages of this non-compound high-order
+			 * page will have no code tags, so to avoid warnings
+			 * mark them as empty.
+			 */
+			clear_page_tag_ref(page + (1 << order));

Do we need something similar for the release_pages() code path or is it happening already?

Quoted 9 lines
mm/page_alloc.c +1 -0
 			__free_frozen_pages(page + (1 << order), order,
 					    fpi_flags);
+		}
 	}
 }
 
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
author reviewer
#11 lore raw

Re: [PATCH 3/3] alloc_tag: avoid warnings when freeing non-compound "tail" pages

Quoted 29 lines · Shakeel Butt
Quoted 27 lines · Suren Baghdasaryan

When freeing "tail" pages of a non-compount high-order page, we properly subtract the allocation tag counters, however later when these pages are released, alloc_tag_sub() will issue warnings because tags for these pages are NULL.
This issue was originally anticipated by Vlastimil in his review [1] and then recently reported by David.
Prevent warnings by marking the tags empty.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/page_alloc.c +2 -1
index 1760346bbd24..d21a411e807e 100644
@@ -5240,9 +5240,16 @@ static void ___free_pages(struct page *page, unsigned int order,
              __free_frozen_pages(page, order, fpi_flags);
      else if (!head) {
              pgalloc_tag_sub_pages(tag, (1 << order) - 1);
-             while (order-- > 0)
+             while (order-- > 0) {
+                     /*
Quoted 8 lines

mm/page_alloc.c static void ___free_pages(struct page *page, unsigned int order, · line 5249

Quoted 5 lines
mm/page_alloc.c +5 -0
+                      * The "tail" pages of this non-compound high-order
+                      * page will have no code tags, so to avoid warnings
+                      * mark them as empty.
+                      */
+                     clear_page_tag_ref(page + (1 << order));

Do we need something similar for the release_pages() code path or is it happening already?

Pages released with release_pages() should have valid tags, so I expect no warnings.

Quoted 11 lines
Quoted 10 lines
mm/page_alloc.c +1 -1
                      __free_frozen_pages(page + (1 << order), order,
                                          fpi_flags);
+             }
      }
 }
 
--

2.51.0.384.g4c02a37b29-goog

#12 lore raw

Re: [PATCH 3/3] alloc_tag: avoid warnings when freeing non-compound "tail" pages

Quoted 39 lines · Suren Baghdasaryan
Quoted 29 lines · Shakeel Butt
Quoted 27 lines · Suren Baghdasaryan

When freeing "tail" pages of a non-compount high-order page, we properly subtract the allocation tag counters, however later when these pages are released, alloc_tag_sub() will issue warnings because tags for these pages are NULL.
This issue was originally anticipated by Vlastimil in his review [1] and then recently reported by David.
Prevent warnings by marking the tags empty.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/page_alloc.c +2 -1
index 1760346bbd24..d21a411e807e 100644
@@ -5240,9 +5240,16 @@ static void ___free_pages(struct page *page, unsigned int order,
              __free_frozen_pages(page, order, fpi_flags);
      else if (!head) {
              pgalloc_tag_sub_pages(tag, (1 << order) - 1);
-             while (order-- > 0)
+             while (order-- > 0) {
+                     /*
Quoted 8 lines

mm/page_alloc.c static void ___free_pages(struct page *page, unsigned int order, · line 5249

Quoted 5 lines
mm/page_alloc.c +5 -0
+                      * The "tail" pages of this non-compound high-order
+                      * page will have no code tags, so to avoid warnings
+                      * mark them as empty.
+                      */
+                     clear_page_tag_ref(page + (1 << order));

Do we need something similar for the release_pages() code path or is it happening already?

Quoted 3 lines

Pages released with release_pages() should have valid tags, so I expect no warnings.

Oh so this is orthogonal to your first patch which is replacing __free_page with release_pages(). This non-compound high-order page is not an issue in the codepath vm_module_tags_populate(), correct?

author reviewer
#13 lore raw

Re: [PATCH 3/3] alloc_tag: avoid warnings when freeing non-compound "tail" pages

Quoted 41 lines · Shakeel Butt
Quoted 39 lines · Suren Baghdasaryan
Quoted 37 lines · Shakeel Butt
Quoted 22 lines · Suren Baghdasaryan

When freeing "tail" pages of a non-compount high-order page, we properly subtract the allocation tag counters, however later when these pages are released, alloc_tag_sub() will issue warnings because tags for these pages are NULL.
This issue was originally anticipated by Vlastimil in his review [1] and then recently reported by David.
Prevent warnings by marking the tags empty.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index 1760346bbd24..d21a411e807e 100644
@@ -5240,9 +5240,16 @@ static void ___free_pages(struct page *page, unsigned int order,
              __free_frozen_pages(page, order, fpi_flags);

mm/page_alloc.c static void ___free_pages(struct page *page, unsigned int order, · line 5249

Quoted 10 lines
mm/page_alloc.c +7 -1
      else if (!head) {
              pgalloc_tag_sub_pages(tag, (1 << order) - 1);
-             while (order-- > 0)
+             while (order-- > 0) {
+                     /*
+                      * The "tail" pages of this non-compound high-order
+                      * page will have no code tags, so to avoid warnings
+                      * mark them as empty.
+                      */
+                     clear_page_tag_ref(page + (1 << order));

Do we need something similar for the release_pages() code path or is it happening already?

Quoted 7 lines
Quoted 3 lines

Pages released with release_pages() should have valid tags, so I expect no warnings.

Oh so this is orthogonal to your first patch which is replacing __free_page with release_pages(). This non-compound high-order page is not an issue in the codepath vm_module_tags_populate(), correct?

Yes, these are independent fixes.

Acked-by on v1 3/3
#14 lore raw

Re: [PATCH 3/3] alloc_tag: avoid warnings when freeing non-compound "tail" pages

In reply to Suren Baghdasaryan
Quoted 8 lines · Suren Baghdasaryan

When freeing "tail" pages of a non-compount high-order page, we properly subtract the allocation tag counters, however later when these pages are released, alloc_tag_sub() will issue warnings because tags for these pages are NULL.
This issue was originally anticipated by Vlastimil in his review [1] and then recently reported by David.
Prevent warnings by marking the tags empty.

Quoted 4 lines

[1] https://lore.kernel.org/all/[email protected]/