Skip to contentSkip to conversation

Minor fixes for memory allocation profiling

PATCH Applied Suren Baghdasaryan 2 participants 5 messages Sep 15, 2025 – Sep 16, 2025

Subsystems (3)
lore
Share & tools
lore

Suren Baghdasaryan

author reviewer
#0 lore raw

v2 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 after reverting previous version.

Changes since v1 [1]:
- Use release_pages() in free_mod_tags_mem() in [1/3], per Usama Arif
- Add comment for proc_mem_profiling_handler() in [2/3], per Usama Arif
- Add Acked-by, per Shakeel Butt and Usama Arif
- Fixed a typo in [1/3] changelog

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

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 | 26 ++++++++++++++++++++------
 mm/page_alloc.c |  9 ++++++++-
 2 files changed, 28 insertions(+), 7 deletions(-)
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
author reviewer
#1 lore raw

PATCH v2 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 | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/alloc_tag.c +5 -5
index e9b33848700a..715315f5d9ba 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;
 		}
 
@@ -682,11 +683,10 @@ static int __init alloc_mod_tags_mem(void)
 
 static void __init free_mod_tags_mem(void)
 {
-	int i;
+	release_pages_arg arg = { .pages = vm_module_tags->pages };
 
 	module_tags.start_addr = 0;
-	for (i = 0; i < vm_module_tags->nr_pages; i++)
-		__free_page(vm_module_tags->pages[i]);
+	release_pages(arg, vm_module_tags->nr_pages);
 	kfree(vm_module_tags->pages);
 	free_vm_area(vm_module_tags);
 }
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
author reviewer
#2 lore raw

PATCH v2 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 | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
lib/alloc_tag.c +15 -1
index 715315f5d9ba..f79217427d81 100644
@@ -766,6 +766,20 @@ struct page_ext_operations page_alloc_tagging_ops = {
 EXPORT_SYMBOL(page_alloc_tagging_ops);
 
 #ifdef CONFIG_SYSCTL
+/*
+ * Not using proc_do_static_key() directly to prevent enabling profiling
+ * after it was shut down.
+ */
+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",
@@ -775,7 +789,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
author reviewer
#3 lore raw

PATCH v2 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 f54c5ee1e318..2bfab96c207f 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
Acked-by on v2 3/3
#4 lore raw

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

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 9 lines

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

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)