alloc_tag: two fixes and one cleanup
Subsystems (5)
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]>
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.
--- 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
@@ -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
@@ -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
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.
--- include/linux/mempool.h | 4 +++- mm/mempool.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-)
include/linux/mempool.h +3 -1
@@ -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
@@ -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
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
@@ -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
@@ -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
Vlastimil Babka (SUSE)
maintainerQuoted 9 lines · David Hildenbrand (Arm)
Hi,
two fixes (one for
alloc_tagas 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
Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Chris Mason (1):
mempool: fix missing alloc tagging inmempool_resize()David Hildenbrand (Arm) (2):
alloc_tag: handle a NULL tag inalloc_tag_set_inaccurate()
mempool: account the element array to the callerinclude/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-57326f1fcc85Best regards,
Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
Quoted 6 lines · Vlastimil Babka (SUSE)
Quoted 5 lines · David Hildenbrand (Arm)
Hi,
two fixes (one for
alloc_tagas 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.cis also for slab, so we'll take it :)
Thanks! :)
Signature · 3 lines
Cheers, David
Vlastimil Babka (SUSE)
maintainerRe: [PATCH 3/3] mempool: account the element array to the caller
In reply to David Hildenbrand (Arm)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 bymempool_init_node()andmempool_resize()ends up being accounted tomm/mempool.cin/proc/allocinfo.Let's consistently account all mempool allocations to the caller, like
ptr_ring_init()andptr_ring_resize()do, by using the_noprofvariants.
Quoted 8 lines
mempool_init_node()is only called frommempool_init_noprof()andmempool_create_node_noprof(), which already set the allocation tag.While at it, make
mempool_init_node()static and rename it tomempool_init_node_noprof(): it is not used outside ofmm/mempool.c, and calling it without an allocation tag would be wrong.Signed-off-by David Hildenbrand (Arm) <[email protected]>
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@@ -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@@ -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 ;
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 bymempool_init_node()andmempool_resize()ends up being accounted tomm/mempool.cin/proc/allocinfo.Let's consistently account all mempool allocations to the caller, like
ptr_ring_init()andptr_ring_resize()do, by using the_noprofvariants.
mempool_init_node()is only called frommempool_init_noprof()andmempool_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 tomempool_init_node_noprof(): it is not used outside ofmm/mempool.c, and calling it without an allocation tag would be wrong.Signed-off-by David Hildenbrand (Arm) <[email protected]>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
Vlastimil Babka (SUSE)
maintainerQuoted 9 lines · David Hildenbrand (Arm)
Quoted 8 lines · Vlastimil Babka (SUSE)
Quoted 7 lines · David Hildenbrand (Arm)
Hi,
two fixes (one for
alloc_tagas 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.cis 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!
Re: [PATCH 1/3] alloc_tag: handle a NULL tag in alloc_tag_set_inaccurate()
In reply to David Hildenbrand (Arm)Quoted 15 lines · David Hildenbrand (Arm)
With memory allocation profiling enabled after boot, a slab allocation can dereference a NULL pointer if
current->alloc_tagis not set and allocating the slab object extensions fails:__alloc_tagging_slab_alloc_hook()passescurrent->alloc_tagtoalloc_tag_set_inaccurate(), which does not handle a NULL tag currently.One way to trigger it is through rhashtable:
rhashtable_init()recordscurrent->alloc_taginht->alloc_tag, which is NULL while profiling is disabled. Once profiling gets enabled,alloc_hooks_tag()setscurrent->alloc_tagto that NULL tag when allocating a bucket table.Fix it by handling NULL tags in
alloc_tag_set_inaccurate(), just likealloc_tag_add()andalloc_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")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Looks good to me,
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH 2/3] mempool: fix missing alloc tagging in mempool_resize()
In reply to David Hildenbrand (Arm)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_noprofallocators and rely on the caller to have setcurrent->alloc_tag.Unlike
mempool_init()ormempool_alloc(),mempool_resize()is not analloc_hooks()wrapper, though.Therefore, the newly allocated elements are missing from
/proc/allocinfo, and withCONFIG_MEM_ALLOC_PROFILING_DEBUG=ywe can trigger "current->alloc_tagnot set" and "alloc_tagwas not set" warnings.Fix it by turning
mempool_resize()into analloc_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")Signed-off-by Chris Mason <[email protected]>[ david: adjust description, remove kmalloc_objs() change ]Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Looks good to me,
Signature · 2 lines
Cheers, Harry / Hyeonggon
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 bymempool_init_node()andmempool_resize()ends up being accounted tomm/mempool.cin/proc/allocinfo.Let's consistently account all mempool allocations to the caller, like
ptr_ring_init()andptr_ring_resize()do, by using the_noprofvariants.
mempool_init_node()is only called frommempool_init_noprof()andmempool_create_node_noprof(), which already set the allocation tag.
Quoted 6 lines
While at it, make
mempool_init_node()static and rename it tomempool_init_node_noprof(): it is not used outside ofmm/mempool.c, and calling it without an allocation tag would be wrong.Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Looks good to me,
Signature · 2 lines
Cheers, Harry / Hyeonggon