Skip to contentSkip to conversation

alloc_tag: two fixes and one cleanup

PATCH In review David Hildenbrand (Arm) 3 participants 12 messages Nov 19 – Nov 23

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover alloc_tag: two fixes and one cleanup

Hi,

two fixes (one for alloc_tag as triggered by slab, one for mempool) and one related cleanup to make things consistent.

Quickly reproduced and tested the warnings in a x86-64 VM.

This should go through Andrew's tree. No need to rush, it can wait until the next merge window.

---
Chris Mason (1):
mempool: fix missing alloc tagging in mempool_resize()

David Hildenbrand (Arm) (2):
alloc_tag: handle a NULL tag in alloc_tag_set_inaccurate()
mempool: account the element array to the caller

 include/linux/alloc_tag.h |  3 ++-
 include/linux/mempool.h   |  7 +++----
 mm/mempool.c              | 23 ++++++++++++-----------
 mm/page_alloc.c           |  3 +--
 4 files changed, 18 insertions(+), 18 deletions(-)

---
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
change-id: 20261119-alloc_tag-57326f1fcc85

Best regards,

Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
#1 lore raw

PATCH 1/3 alloc_tag: handle a NULL tag in alloc_tag_set_inaccurate()

With memory allocation profiling enabled after boot, a slab allocation can dereference a NULL pointer if current->alloc_tag is not set and allocating the slab object extensions fails:
__alloc_tagging_slab_alloc_hook() passes current->alloc_tag to alloc_tag_set_inaccurate(), which does not handle a NULL tag currently.

One way to trigger it is through rhashtable: rhashtable_init() records current->alloc_tag in ht->alloc_tag, which is NULL while profiling is disabled. Once profiling gets enabled, alloc_hooks_tag() sets current->alloc_tag to that NULL tag when allocating a bucket table.

Fix it by handling NULL tags in alloc_tag_set_inaccurate(), just like alloc_tag_add() and alloc_tag_sub() already do, and drop the now unnecessary check in __pgalloc_tag_add().

This has low urgency.

Fixes b9e2f58ffb84 ("alloc_tag: mark inaccurate allocation counters in /proc/allocinfo output")
---
 include/linux/alloc_tag.h | 3 ++-
 mm/page_alloc.c           | 3 +--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/alloc_tag.h +2 -1
index 7f2d80a597924..0f9909c6701a7 100644
@@ -227,7 +227,8 @@ static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes)
 
 static inline void alloc_tag_set_inaccurate(struct alloc_tag *tag)
 {
-	tag->ct.flags |= CODETAG_FLAG_INACCURATE;
+	if (tag)
+		tag->ct.flags |= CODETAG_FLAG_INACCURATE;
 }
 
 static inline bool alloc_tag_is_inaccurate(struct alloc_tag *tag)
mm/page_alloc.c +1 -2
index 2fef4092c913c..0b76923145456 100644
@@ -1262,8 +1262,7 @@ void __pgalloc_tag_add(struct page *page, struct task_struct *task,
 		 * clear the tag ref later when page_ext is initialized.
 		 */
 		alloc_tag_add_early_pfn(page_to_pfn(page), alloc_flags);
-		if (task->alloc_tag)
-			alloc_tag_set_inaccurate(task->alloc_tag);
+		alloc_tag_set_inaccurate(task->alloc_tag);
 	}
 }
 
Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] alloc_tag: handle a NULL tag in alloc_tag_set_inaccurate()

Quoted 15 lines · David Hildenbrand (Arm)

With memory allocation profiling enabled after boot, a slab allocation can dereference a NULL pointer if current->alloc_tag is not set and allocating the slab object extensions fails:
__alloc_tagging_slab_alloc_hook() passes current->alloc_tag to alloc_tag_set_inaccurate(), which does not handle a NULL tag currently.

One way to trigger it is through rhashtable: rhashtable_init() records current->alloc_tag in ht->alloc_tag, which is NULL while profiling is disabled. Once profiling gets enabled, alloc_hooks_tag() sets current->alloc_tag to that NULL tag when allocating a bucket table.

Fix it by handling NULL tags in alloc_tag_set_inaccurate(), just like alloc_tag_add() and alloc_tag_sub() already do, and drop the now unnecessary check in __pgalloc_tag_add().

Quoted 7 lines

This has low urgency.

Fixes b9e2f58ffb84 ("alloc_tag: mark inaccurate allocation counters in /proc/allocinfo output")

---

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#3 lore raw

PATCH 2/3 mempool: fix missing alloc tagging in mempool_resize()

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

With memory allocation profiling enabled, mempool_resize() allocates new elements without an allocation tag: mempool_alloc_slab() and friends use the _noprof allocators and rely on the caller to have set current->alloc_tag.

Unlike mempool_init() or mempool_alloc(), mempool_resize() is not an alloc_hooks() wrapper, though.

Therefore, the newly allocated elements are missing from /proc/allocinfo, and with CONFIG_MEM_ALLOC_PROFILING_DEBUG=y we can trigger "current->alloc_tag not set" and "alloc_tag was not set" warnings.

Fix it by turning mempool_resize() into an alloc_hooks() wrapper, just like the other mempool functions that allocate elements.

This has low urgency.

Fixes e26d8769da6d ("mempool: hook up to memory allocation profiling")
[ david: adjust description, remove kmalloc_objs() change ]
---
 include/linux/mempool.h | 4 +++-
 mm/mempool.c            | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/linux/mempool.h +3 -1
index 6da502aef2f71..5444dcb63efa8 100644
@@ -59,7 +59,9 @@ struct mempool *mempool_create_node_noprof(int min_nr,
 	mempool_create_node(_min_nr, _alloc_fn, _free_fn, _pool_data,	\
 			    GFP_KERNEL, NUMA_NO_NODE)
 
-int mempool_resize(struct mempool *pool, int new_min_nr);
+int mempool_resize_noprof(struct mempool *pool, int new_min_nr);
+#define mempool_resize(...)						\
+	alloc_hooks(mempool_resize_noprof(__VA_ARGS__))
 void mempool_destroy(struct mempool *pool);
 
 void *mempool_alloc_noprof(struct mempool *pool, gfp_t gfp_mask) __malloc;
mm/mempool.c +2 -2
index cb74e718b2c65..5e0ba516efdbf 100644
@@ -361,7 +361,7 @@ EXPORT_SYMBOL(mempool_create_node_noprof);
  *
  * Return: %0 on success, negative error code otherwise.
  */
-int mempool_resize(struct mempool *pool, int new_min_nr)
+int mempool_resize_noprof(struct mempool *pool, int new_min_nr)
 {
 	void *element;
 	void **new_elements;
@@ -420,7 +420,7 @@ int mempool_resize(struct mempool *pool, int new_min_nr)
 out:
 	return 0;
 }
-EXPORT_SYMBOL(mempool_resize);
+EXPORT_SYMBOL(mempool_resize_noprof);
 
 static unsigned int mempool_alloc_from_pool(struct mempool *pool, void **elems,
 		unsigned int count, unsigned int allocated,
Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] mempool: fix missing alloc tagging in mempool_resize()

Quoted 17 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

With memory allocation profiling enabled, mempool_resize() allocates new elements without an allocation tag: mempool_alloc_slab() and friends use the _noprof allocators and rely on the caller to have set current->alloc_tag.

Unlike mempool_init() or mempool_alloc(), mempool_resize() is not an alloc_hooks() wrapper, though.

Therefore, the newly allocated elements are missing from /proc/allocinfo, and with CONFIG_MEM_ALLOC_PROFILING_DEBUG=y we can trigger "current->alloc_tag not set" and "alloc_tag was not set" warnings.

Fix it by turning mempool_resize() into an alloc_hooks() wrapper, just like the other mempool functions that allocate elements.

Quoted 7 lines

This has low urgency.

Fixes e26d8769da6d ("mempool: hook up to memory allocation profiling")
[ david: adjust description, remove kmalloc_objs() change ]

---

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#5 lore raw

PATCH 3/3 mempool: account the element array to the caller

In reply to David Hildenbrand (Arm)

mempool_create_node() accounts the struct mempool and all elements to the caller, but the array of element pointers allocated by mempool_init_node() and mempool_resize() ends up being accounted to mm/mempool.c in /proc/allocinfo.

Let's consistently account all mempool allocations to the caller, like ptr_ring_init() and ptr_ring_resize() do, by using the _noprof variants.

mempool_init_node() is only called from mempool_init_noprof() and mempool_create_node_noprof(), which already set the allocation tag.

While at it, make mempool_init_node() static and rename it to mempool_init_node_noprof(): it is not used outside of mm/mempool.c, and calling it without an allocation tag would be wrong.

---
 include/linux/mempool.h |  3 ---
 mm/mempool.c            | 19 ++++++++++---------
 2 files changed, 10 insertions(+), 12 deletions(-)
include/linux/mempool.h +0 -3
index 5444dcb63efa8..7f8e257017c01 100644
@@ -38,9 +38,6 @@ static inline bool mempool_is_saturated(struct mempool *pool)
 }
 
 void mempool_exit(struct mempool *pool);
-int mempool_init_node(struct mempool *pool, int min_nr,
-		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
-		void *pool_data, gfp_t gfp_mask, int node_id);
 int mempool_init_noprof(struct mempool *pool, int min_nr,
 		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
 		void *pool_data);
mm/mempool.c +10 -9
index 5e0ba516efdbf..30cbe44d464b3 100644
@@ -243,7 +243,7 @@ void mempool_destroy(struct mempool *pool)
 }
 EXPORT_SYMBOL(mempool_destroy);
 
-int mempool_init_node(struct mempool *pool, int min_nr,
+static int mempool_init_node_noprof(struct mempool *pool, int min_nr,
 		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
 		void *pool_data, gfp_t gfp_mask, int node_id)
 {
@@ -257,8 +257,9 @@ int mempool_init_node(struct mempool *pool, int min_nr,
 	 * max() used here to ensure storage for at least 1 element to support
 	 * zero minimum pool
 	 */
-	pool->elements = kmalloc_array_node(max(1, min_nr), sizeof(void *),
-					    gfp_mask, node_id);
+	pool->elements = kmalloc_array_node_noprof(max(1, min_nr),
+						   sizeof(void *), gfp_mask,
+						   node_id);
 	if (!pool->elements)
 		return -ENOMEM;
 
@@ -279,7 +280,6 @@ int mempool_init_node(struct mempool *pool, int min_nr,
 
 	return 0;
 }
-EXPORT_SYMBOL(mempool_init_node);
 
 /**
  * mempool_init - initialize a memory pool
@@ -299,8 +299,8 @@ int mempool_init_noprof(struct mempool *pool, int min_nr,
 		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
 		void *pool_data)
 {
-	return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
-				 pool_data, GFP_KERNEL, NUMA_NO_NODE);
+	return mempool_init_node_noprof(pool, min_nr, alloc_fn, free_fn,
+					pool_data, GFP_KERNEL, NUMA_NO_NODE);
 
 }
 EXPORT_SYMBOL(mempool_init_noprof);
@@ -333,8 +333,8 @@ struct mempool *mempool_create_node_noprof(int min_nr,
 	if (!pool)
 		return NULL;
 
-	if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
-			      gfp_mask, node_id)) {
+	if (mempool_init_node_noprof(pool, min_nr, alloc_fn, free_fn,
+				     pool_data, gfp_mask, node_id)) {
 		kfree(pool);
 		return NULL;
 	}
@@ -384,7 +384,8 @@ int mempool_resize_noprof(struct mempool *pool, int new_min_nr)
 	spin_unlock_irqrestore(&pool->lock, flags);
 
 	/* Grow the pool */
-	new_elements = kmalloc_objs(*new_elements, new_min_nr);
+	new_elements = kmalloc_array_noprof(new_min_nr, sizeof(*new_elements),
+					    GFP_KERNEL);
 	if (!new_elements)
 		return -ENOMEM;
 
Signature · 1 line
2.56.1
#6 lore raw

Re: [PATCH 3/3] mempool: account the element array to the caller

Quoted 8 lines · David Hildenbrand (Arm)

mempool_create_node() accounts the struct mempool and all elements to the caller, but the array of element pointers allocated by mempool_init_node() and mempool_resize() ends up being accounted to mm/mempool.c in /proc/allocinfo.

Let's consistently account all mempool allocations to the caller, like ptr_ring_init() and ptr_ring_resize() do, by using the _noprof variants.

Quoted 8 lines

mempool_init_node() is only called from mempool_init_noprof() and mempool_create_node_noprof(), which already set the allocation tag.

While at it, make mempool_init_node() static and rename it to mempool_init_node_noprof(): it is not used outside of mm/mempool.c, and calling it without an allocation tag would be wrong.

LGTM, but if this is due to an AI report, they should teach it better about what's urgent, to the point of not reporting such things at all :)

Quoted 85 lines
---
 include/linux/mempool.h |  3 ---
 mm/mempool.c            | 19 ++++++++++---------
 2 files changed, 10 insertions(+), 12 deletions(-)
include/linux/mempool.h +0 -3
index 5444dcb63efa8..7f8e257017c01 100644
@@ -38,9 +38,6 @@ static inline bool mempool_is_saturated(struct mempool *pool)
 }
 
 void mempool_exit(struct mempool *pool);
-int mempool_init_node(struct mempool *pool, int min_nr,
-		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
-		void *pool_data, gfp_t gfp_mask, int node_id);
 int mempool_init_noprof(struct mempool *pool, int min_nr,
 		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
 		void *pool_data);
mm/mempool.c +10 -9
index 5e0ba516efdbf..30cbe44d464b3 100644
@@ -243,7 +243,7 @@ void mempool_destroy(struct mempool *pool)
 }
 EXPORT_SYMBOL(mempool_destroy);
 
-int mempool_init_node(struct mempool *pool, int min_nr,
+static int mempool_init_node_noprof(struct mempool *pool, int min_nr,
 		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
 		void *pool_data, gfp_t gfp_mask, int node_id)
 {
@@ -257,8 +257,9 @@ int mempool_init_node(struct mempool *pool, int min_nr,
 	 * max() used here to ensure storage for at least 1 element to support
 	 * zero minimum pool
 	 */
-	pool->elements = kmalloc_array_node(max(1, min_nr), sizeof(void *),
-					    gfp_mask, node_id);
+	pool->elements = kmalloc_array_node_noprof(max(1, min_nr),
+						   sizeof(void *), gfp_mask,
+						   node_id);
 	if (!pool->elements)
 		return -ENOMEM;
 
@@ -279,7 +280,6 @@ int mempool_init_node(struct mempool *pool, int min_nr,
 
 	return 0;
 }
-EXPORT_SYMBOL(mempool_init_node);
 
 /**
  * mempool_init - initialize a memory pool
@@ -299,8 +299,8 @@ int mempool_init_noprof(struct mempool *pool, int min_nr,
 		mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
 		void *pool_data)
 {
-	return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
-				 pool_data, GFP_KERNEL, NUMA_NO_NODE);
+	return mempool_init_node_noprof(pool, min_nr, alloc_fn, free_fn,
+					pool_data, GFP_KERNEL, NUMA_NO_NODE);
 
 }
 EXPORT_SYMBOL(mempool_init_noprof);
@@ -333,8 +333,8 @@ struct mempool *mempool_create_node_noprof(int min_nr,
 	if (!pool)
 		return NULL;
 
-	if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
-			      gfp_mask, node_id)) {
+	if (mempool_init_node_noprof(pool, min_nr, alloc_fn, free_fn,
+				     pool_data, gfp_mask, node_id)) {
 		kfree(pool);
 		return NULL;
 	}
@@ -384,7 +384,8 @@ int mempool_resize_noprof(struct mempool *pool, int new_min_nr)
 	spin_unlock_irqrestore(&pool->lock, flags);
 
 	/* Grow the pool */
-	new_elements = kmalloc_objs(*new_elements, new_min_nr);
+	new_elements = kmalloc_array_noprof(new_min_nr, sizeof(*new_elements),
+					    GFP_KERNEL);
 	if (!new_elements)
 		return -ENOMEM;
 
#7 lore raw

Re: [PATCH 3/3] mempool: account the element array to the caller

Quoted 12 lines · Vlastimil Babka (SUSE)
Quoted 11 lines · David Hildenbrand (Arm)

mempool_create_node() accounts the struct mempool and all elements to the caller, but the array of element pointers allocated by mempool_init_node() and mempool_resize() ends up being accounted to mm/mempool.c in /proc/allocinfo.

Let's consistently account all mempool allocations to the caller, like ptr_ring_init() and ptr_ring_resize() do, by using the _noprof variants.

mempool_init_node() is only called from mempool_init_noprof() and mempool_create_node_noprof(), which already set the allocation tag.

Quoted 8 lines
Quoted 5 lines

While at it, make mempool_init_node() static and rename it to mempool_init_node_noprof(): it is not used outside of mm/mempool.c, and calling it without an allocation tag would be wrong.

LGTM, but if this is due to an AI report, they should teach it better about what's urgent, to the point of not reporting such things at all :)

That's just a cleanup from me while reworking #2, where AI tried to do some of this here inconsistently in patch #2.

Feel free to drop this one here.

Signature · 3 lines
Cheers,

David
maintainer Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] mempool: account the element array to the caller

In reply to David Hildenbrand (Arm)
Quoted 11 lines · David Hildenbrand (Arm)

mempool_create_node() accounts the struct mempool and all elements to the caller, but the array of element pointers allocated by mempool_init_node() and mempool_resize() ends up being accounted to mm/mempool.c in /proc/allocinfo.

Let's consistently account all mempool allocations to the caller, like ptr_ring_init() and ptr_ring_resize() do, by using the _noprof variants.

mempool_init_node() is only called from mempool_init_noprof() and mempool_create_node_noprof(), which already set the allocation tag.

Quoted 6 lines

While at it, make mempool_init_node() static and rename it to mempool_init_node_noprof(): it is not used outside of mm/mempool.c, and calling it without an allocation tag would be wrong.

---

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#9 lore raw
In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)

Hi,

two fixes (one for alloc_tag as triggered by slab, one for mempool) and one related cleanup to make things consistent.

Quickly reproduced and tested the warnings in a x86-64 VM.

This should go through Andrew's tree. No need to rush, it can wait until the next merge window.

mempool is part of slab section and the first fix while in page_alloc.c is also for slab, so we'll take it :)

Quoted 23 lines

---
Chris Mason (1):
mempool: fix missing alloc tagging in mempool_resize()

David Hildenbrand (Arm) (2):
alloc_tag: handle a NULL tag in alloc_tag_set_inaccurate()
mempool: account the element array to the caller

 include/linux/alloc_tag.h |  3 ++-
 include/linux/mempool.h   |  7 +++----
 mm/mempool.c              | 23 ++++++++++++-----------
 mm/page_alloc.c           |  3 +--
 4 files changed, 18 insertions(+), 18 deletions(-)

---
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
change-id: 20261119-alloc_tag-57326f1fcc85

Best regards,

Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
#10 lore raw
Quoted 6 lines · Vlastimil Babka (SUSE)
Quoted 5 lines · David Hildenbrand (Arm)

Hi,

two fixes (one for alloc_tag as triggered by slab, one for mempool) and one related cleanup to make things consistent.

Quoted 7 lines
Quoted 4 lines

Quickly reproduced and tested the warnings in a x86-64 VM.

This should go through Andrew's tree. No need to rush, it can wait until the next merge window.

mempool is part of slab section and the first fix while in page_alloc.c is also for slab, so we'll take it :)

Thanks! :)

Signature · 3 lines
Cheers,

David
#11 lore raw
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 8 lines · Vlastimil Babka (SUSE)
Quoted 7 lines · David Hildenbrand (Arm)

Hi,

two fixes (one for alloc_tag as triggered by slab, one for mempool) and one related cleanup to make things consistent.

Quickly reproduced and tested the warnings in a x86-64 VM.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This should go through Andrew's tree. No need to rush, it can wait until the next merge window.

mempool is part of slab section and the first fix while in page_alloc.c is also for slab, so we'll take it :)

Thanks! :)

Actually, let's separate to minimize conflict potential

patch 1 merged to: page_alloc/for-7.5/misc
patches 2+3 merged to: slab/for-7.5/misc

Thanks!