improve the kmem_cache_alloc_bulk API
Subsystems (14)
Hi all,
kmem_cache_alloc_bulk has a very unintuitive and undocumented return value convention. Fix that and add documentation.
Note that the few comments explaining it mention that the gfp flags must allow "spinning". That's not really a term used in the memory allocator, is this supposed to mean "block" or "sleep"?
Diffstat:
drivers/gpu/drm/msm/msm_iommu.c | 6 +-- drivers/gpu/drm/panthor/panthor_mmu.c | 12 ++----- include/linux/slab.h | 6 ++- io_uring/io_uring.c | 23 +++++-------- lib/test_meminit.c | 19 +++++------ mm/kasan/kasan_test_c.c | 5 +- mm/kfence/kfence_test.c | 9 ++--- mm/slub.c | 58 ++++++++++++++++++---------------- net/bpf/test_run.c | 7 +--- net/core/skbuff.c | 23 +++++++------ tools/include/linux/slab.h | 2 - tools/testing/shared/linux.c | 19 ++++------- 12 files changed, 92 insertions(+), 97 deletions(-)
PATCH 1/1 mm/slab: improve kmem_cache_alloc_bulk
The kmem_cache_alloc_bulk return value is weird. It returns the number of allocated objects, but that must always be 0 or the requested number based on the implementations and the handling in the callers, but that assumption is not actually documented anywhere, which confuses automated review tools.
Fix this by returning a bool if the allocation succeeded and adding a kerneldoc comment explaining the API.
--- drivers/gpu/drm/msm/msm_iommu.c | 6 +-- drivers/gpu/drm/panthor/panthor_mmu.c | 12 +++--- include/linux/slab.h | 6 ++- io_uring/io_uring.c | 23 +++++------ lib/test_meminit.c | 19 +++++---- mm/kasan/kasan_test_c.c | 5 +-- mm/kfence/kfence_test.c | 9 +++-- mm/slub.c | 58 +++++++++++++++------------ net/bpf/test_run.c | 7 ++-- net/core/skbuff.c | 24 ++++++----- tools/include/linux/slab.h | 2 +- tools/testing/shared/linux.c | 19 ++++----- 12 files changed, 93 insertions(+), 97 deletions(-)
drivers/gpu/drm/msm/msm_iommu.c +2 -4
@@ -330,17 +330,15 @@ static int msm_iommu_pagetable_prealloc_allocate(struct msm_mmu *mmu, struct msm_mmu_prealloc *p) { struct kmem_cache *pt_cache = get_pt_cache(mmu); -int ret; p->pages =kvmalloc_objs (*p->pages, p->count);if (!p->pages)return -ENOMEM ; - ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , p->count, p->pages); -if (ret != p->count) { +if (!kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , p->count, p->pages)) {kfree (p->pages); p->pages =NULL ; - p->count = ret; + p->count =0 ;return -ENOMEM ; }
drivers/gpu/drm/panthor/panthor_mmu.c +5 -7
@@ -1274,10 +1274,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, goto err_cleanup; } - ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, - op_ctx->rsvd_page_tables.pages); - op_ctx->rsvd_page_tables.count = ret; -if (ret != pt_count) { +if (!kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, + op_ctx->rsvd_page_tables.pages)) { + op_ctx->rsvd_page_tables.count =0 ; ret = -ENOMEM ; goto err_cleanup; }
@@ -1328,9 +1327,8 @@ static int panthor_vm_prepare_unmap_op_ctx(struct panthor_vm_op_ctx *op_ctx, goto err_cleanup; } - ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, - op_ctx->rsvd_page_tables.pages); -if (ret != pt_count) { +if (!kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, + op_ctx->rsvd_page_tables.pages)) { ret = -ENOMEM ; goto err_cleanup; }
include/linux/slab.h +4 -2
@@ -815,8 +815,10 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,*/ void kmem_cache_free_bulk (struct kmem_cache *s,size_t size,void **p); -int kmem_cache_alloc_bulk_noprof (struct kmem_cache *s,gfp_t flags,size_t size,void **p); -#define kmem_cache_alloc_bulk (...) alloc_hooks(kmem_cache_alloc_bulk_noprof(__VA_ARGS__)) +bool kmem_cache_alloc_bulk_noprof (struct kmem_cache *s,gfp_t flags, +size_t size,void **p); +#define kmem_cache_alloc_bulk (...) \ + alloc_hooks(kmem_cache_alloc_bulk_noprof(__VA_ARGS__)) static __always_inline void kfree_bulk(size_t size, void **p) {
io_uring/io_uring.c +9 -14
@@ -978,29 +978,24 @@ __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) {gfp_t gfp =GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO;void *reqs[IO_REQ_ALLOC_BATCH ]; -int ret; - - ret =kmem_cache_alloc_bulk (req_cachep, gfp,ARRAY_SIZE (reqs), reqs); +int nr_reqs =ARRAY_SIZE (reqs);/* -* Bulk alloc is all-or-nothing. If we fail to get a batch, -* retry single alloc to be on the safe side. +* Bulk alloc is all-or-nothing. If we fail to get a batch, retry a +* single allocation to be on the safe side. */ -if (unlikely (ret <=0 )) { +if (!kmem_cache_alloc_bulk (req_cachep, gfp, nr_reqs, reqs)) { reqs[0 ] =kmem_cache_alloc (req_cachep, gfp);if (!reqs[0 ])return false; - ret =1 ; + nr_reqs =1 ; } -percpu_ref_get_many (&ctx->refs, ret); - ctx->nr_req_allocated += ret; - -while (ret--) { -struct io_kiocb *req = reqs[ret]; +percpu_ref_get_many (&ctx->refs, nr_reqs); + ctx->nr_req_allocated += nr_reqs; -io_req_add_to_cache (req, ctx); - } +while (nr_reqs--) +io_req_add_to_cache (reqs[nr_reqs], ctx);return true; }
lib/test_meminit.c +9 -10
@@ -229,14 +229,12 @@ static int __init do_kmem_cache_size(size_t size, bool want_ctor,for (iter =0 ; iter <10 ; iter++) {/* Do a test of bulk allocations */ if (!want_rcu && !want_ctor) { -int ret; - - ret =kmem_cache_alloc_bulk (c, alloc_mask,BULK_SIZE , bulk_array); -if (!ret) { +if (!kmem_cache_alloc_bulk (c, alloc_mask,BULK_SIZE , + bulk_array)) { fail = true; }else {int i; -for (i =0 ; i < ret; i++) +for (i =0 ; i <BULK_SIZE ; i++) fail |=check_buf (bulk_array[i], size, want_ctor, want_rcu, want_zero);kmem_cache_free_bulk (c, ret, bulk_array); }
@@ -354,17 +352,18 @@ static int __init do_kmem_cache_size_bulk(int size, int *total_failures) c =kmem_cache_create ("test_cache" , size, size,0 ,NULL );for (iter =0 ; (iter < maxiter) && !fail; iter++) { - num =kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), - objects); -for (i =0 ; i < num; i++) { +if (!kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), + objects)) +continue ; + +for (i =0 ; i <ARRAY_SIZE (objects); i++) { bytes =count_nonzero_bytes (objects[i], size);if (bytes) fail = true;fill_with_garbage (objects[i], size); } -if (num) -kmem_cache_free_bulk (c, num, objects); +kmem_cache_free_bulk (c, num, objects); }kmem_cache_destroy (c); *total_failures += fail;
mm/kasan/kasan_test_c.c +2 -3
@@ -1225,14 +1225,13 @@ static void kmem_cache_bulk(struct kunit *test)struct kmem_cache *cache;size_t size =200 ;char *p[10 ]; -bool ret;int i; cache =kmem_cache_create ("test_cache" , size,0 ,0 ,NULL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, cache); - ret =kmem_cache_alloc_bulk (cache,GFP_KERNEL ,ARRAY_SIZE (p), (void **)&p); -if (!ret) { +if (!kmem_cache_alloc_bulk (cache,GFP_KERNEL ,ARRAY_SIZE (p), + (void **)&p)) {kunit_err (test,"Allocation failed: %s\n" , __func__);kmem_cache_destroy (cache);return ;
mm/kfence/kfence_test.c +5 -4
@@ -761,9 +761,10 @@ static void test_memcache_alloc_bulk(struct kunit *test) timeout = jiffies +msecs_to_jiffies (100 * kfence_sample_interval); do { void *objects[100]; - int i, num = kmem_cache_alloc_bulk(test_cache, GFP_ATOMIC, ARRAY_SIZE(objects), - objects); - if (!num) + int i; + + if (!kmem_cache_alloc_bulk(test_cache, GFP_ATOMIC, + ARRAY_SIZE(objects), objects)) continue; for (i = 0; i < ARRAY_SIZE(objects); i++) { if (is_kfence_address(objects[i])) {
@@ -771,7 +772,7 @@ static void test_memcache_alloc_bulk(struct kunit *test)break ; } } -kmem_cache_free_bulk (test_cache, num, objects); +kmem_cache_free_bulk (test_cache,ARRAY_SIZE (objects), objects);/* * kmem_cache_alloc_bulk() disables interrupts, and calling it * in a tight loop may not give KFENCE a chance to switch the
mm/slub.c +32 -26
118 lines not on this page · raw message
net/bpf/test_run.c +3 -4
@@ -243,12 +243,11 @@ static int xdp_recv_frames(struct xdp_frame **frames, int nframes, struct net_device *dev) {gfp_t gfp = __GFP_ZERO |GFP_ATOMIC ; -int i, n; +int i;LIST_HEAD (list); - n =kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, gfp, nframes, - (void **)skbs); -if (unlikely (n ==0 )) { +if (!kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, gfp, nframes, + (void **)skbs)) {for (i =0 ; i < nframes; i++)xdp_return_frame (frames[i]);return -ENOMEM ;
net/core/skbuff.c +13 -11
@@ -288,11 +288,11 @@ static inline struct sk_buff *napi_skb_cache_get(bool alloc)local_lock_nested_bh (&napi_alloc_cache.bh_lock);if (unlikely (!nc->skb_count)) { -if (alloc) - nc->skb_count =kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, -GFP_ATOMIC | __GFP_NOWARN, -NAPI_SKB_CACHE_BULK , - nc->skb_cache); +if (alloc &&kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, +GFP_ATOMIC | __GFP_NOWARN, +NAPI_SKB_CACHE_BULK , + nc->skb_cache)) + nc->skb_count =NAPI_SKB_CACHE_BULK ;if (unlikely (!nc->skb_count)) {local_unlock_nested_bh (&napi_alloc_cache.bh_lock);return NULL ;
@@ -353,16 +353,18 @@ u32 napi_skb_cache_get_bulk(void **skbs, u32 n)/* No enough cached skbs. Try refilling the cache first */ bulk =min (NAPI_SKB_CACHE_SIZE - nc->skb_count,NAPI_SKB_CACHE_BULK ); - nc->skb_count +=kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, -GFP_ATOMIC | __GFP_NOWARN, bulk, - &nc->skb_cache[nc->skb_count]); +if (kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, +GFP_ATOMIC | __GFP_NOWARN, bulk, + &nc->skb_cache[nc->skb_count])) + nc->skb_count += bulk;if (likely (nc->skb_count >= n)) goto get;/* Still not enough. Bulk-allocate the missing part directly, zeroed */ - n -=kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, -GFP_ATOMIC | __GFP_ZERO | __GFP_NOWARN, - n - nc->skb_count, &skbs[nc->skb_count]); +if (kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, +GFP_ATOMIC | __GFP_ZERO | __GFP_NOWARN, + n - nc->skb_count, &skbs[nc->skb_count])) + n = nc->skb_count;if (likely (nc->skb_count >= n)) goto get;
tools/include/linux/slab.h +1 -1
@@ -183,7 +183,7 @@ __kmem_cache_create(const char *name, unsigned int size, unsigned int align, default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__) void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list); -int kmem_cache_alloc_bulk (struct kmem_cache *cachep,gfp_t gfp,size_t size, +bool kmem_cache_alloc_bulk (struct kmem_cache *cachep,gfp_t gfp,size_t size,void **list);struct slab_sheaf *kmem_cache_prefill_sheaf (struct kmem_cache *s,gfp_t gfp,unsigned int size);
tools/testing/shared/linux.c +8 -11
@@ -154,7 +154,7 @@ void kmem_cache_shrink(struct kmem_cache *cachep) { } -int kmem_cache_alloc_bulk (struct kmem_cache *cachep,gfp_t gfp,size_t size, +bool kmem_cache_alloc_bulk (struct kmem_cache *cachep,gfp_t gfp,size_t size,void **p) {size_t i;
@@ -213,7 +213,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,pthread_mutex_unlock (&cachep->lock);if (cachep->callback) cachep->exec_callback = true; -return 0 ; +return false; }for (i =0 ; i < size; i++) {
@@ -224,7 +224,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,printf ("Allocating %p from slab\n" , p[i]); } -return size; +return true; } struct kmem_cache *
@@ -271,8 +271,8 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size) sheaf->cache = s; sheaf->capacity = capacity; - sheaf->size =kmem_cache_alloc_bulk (s, gfp, size, sheaf->objects); -if (!sheaf->size) { + sheaf->size = size; +if (!kmem_cache_alloc_bulk (s, gfp, size, sheaf->objects)) {free (sheaf);return NULL ; }
@@ -284,7 +284,6 @@ int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp_t gfp, struct slab_sheaf **sheafp, unsigned int size) {struct slab_sheaf *sheaf = *sheafp; -int refill;if (sheaf->size >= size)return 0 ;
@@ -299,12 +298,10 @@ int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp_t gfp,return 0 ; } - refill =kmem_cache_alloc_bulk (s, gfp, size - sheaf->size, - &sheaf->objects[sheaf->size]); -if (!refill) +if (!kmem_cache_alloc_bulk (s, gfp, size - sheaf->size, + &sheaf->objects[sheaf->size]))return -ENOMEM ; - - sheaf->size += refill; + sheaf->size += (size - sheaf->size);return 0 ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] The patch fails to set the reserved page tables count upon a successful bulk allocation in…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The patch fails to set the reserved page tables count upon a successful bulk allocation in panthor_vm_prepare_map_op_ctx(), leaving it at 0.
- [High] kmem_cache_alloc_bulk_noprof incorrectly returns false (failure) when requested to allocate 0 objects, causing false-positive allocation failures.
- [Medium] The patch removes the assignment to num in do_kmem_cache_size_bulk(), causing an uninitialized variable to be passed to kmem_cache_free_bulk().
- [Low] The patch leaves an undeclared identifier ret in do_kmem_cache_size(), causing a build failure.
--
commit d2aac103779e2c77c7fc949b832a9d8ab2aaa90f
Author: Christoph Hellwig <[email protected]>
mm/slab: improve kmem_cache_alloc_bulk
This patch changes the return type of kmem_cache_alloc_bulk from an int to a bool to clearly indicate whether the allocation succeeded or failed. It updates the slab implementation, tests, and callers to match the new behavior.
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0@@ -1274,10 +1274,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, goto err_cleanup; }
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, · line 1282
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-4- ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, - op_ctx->rsvd_page_tables.pages); - op_ctx->rsvd_page_tables.count = ret; -if (ret != pt_count) { +if (!kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, + op_ctx->rsvd_page_tables.pages)) { + op_ctx->rsvd_page_tables.count =0 ; ret = -ENOMEM ; goto err_cleanup; }
[Severity: High]
Does this omit setting op_ctx->rsvd_page_tables.count to pt_count on success? Since op_ctx is initialized via memset previously, the count will remain 0, which could lead to memory leaks when these page tables are consumed or cleaned up later.
[ ... ]
Quoted 11 lines
lib/test_meminit.c +0
-3@@ -229,14 +229,12 @@ static int __init do_kmem_cache_size(size_t size, bool want_ctor,for (iter =0 ; iter <10 ; iter++) {/* Do a test of bulk allocations */ if (!want_rcu && !want_ctor) { -int ret; - - ret =kmem_cache_alloc_bulk (c, alloc_mask,BULK_SIZE , bulk_array);
lib/test_meminit.c static int __init do_kmem_cache_size(size_t size, bool want_ctor, · line 239
Quoted 10 lines
lib/test_meminit.c +3
-2-if (!ret) { +if (!kmem_cache_alloc_bulk (c, alloc_mask,BULK_SIZE , + bulk_array)) { fail = true; }else {int i; -for (i =0 ; i < ret; i++) +for (i =0 ; i <BULK_SIZE ; i++) fail |=check_buf (bulk_array[i], size, want_ctor, want_rcu, want_zero);kmem_cache_free_bulk (c, ret, bulk_array);
[Severity: Low]
Will this cause a compilation error? The local variable ret is removed from this scope, but it is still used in the kmem_cache_free_bulk() call above.
Quoted 12 lines
lib/test_meminit.c +4
-3}@@ -354,17 +352,18 @@ static int __init do_kmem_cache_size_bulk(int size, int *total_failures) c =kmem_cache_create ("test_cache" , size, size,0 ,NULL );for (iter =0 ; (iter < maxiter) && !fail; iter++) { - num =kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), - objects); -for (i =0 ; i < num; i++) { +if (!kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), + objects)) +continue ; +
lib/test_meminit.c static int __init do_kmem_cache_size_bulk(int size, int *total_failures) · line 366
Quoted 10 lines
lib/test_meminit.c +2
-2+for (i =0 ; i <ARRAY_SIZE (objects); i++) { bytes =count_nonzero_bytes (objects[i], size);if (bytes) fail = true;fill_with_garbage (objects[i], size); } -if (num) -kmem_cache_free_bulk (c, num, objects); +kmem_cache_free_bulk (c, num, objects);
[Severity: Medium]
Is num uninitialized here? It's no longer assigned the result of the allocation call, so passing it to kmem_cache_free_bulk() might use indeterminate stack data and lead to memory corruption.
Quoted 3 lines
lib/test_meminit.c +0
-0}kmem_cache_destroy (c); *total_failures += fail;
[ ... ]
Quoted 6 lines
mm/slub.c +0
-1@@ -7382,12 +7392,8 @@ int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size, -int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size,
mm/slub.c int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size, · line 7399
Quoted 10 lines
mm/slub.c +3
-2- void **p) +bool kmem_cache_alloc_bulk_noprof (struct kmem_cache *s,gfp_t flags, +size_t size,void **p) {unsigned int i =0 ;void *kfence_obj;if (!size) -return 0 ; +return false;
[Severity: High]
Does this incorrectly report an allocation failure when size is 0? Previously, callers interpreting the return value against size (ret != size) would treat 0 objects allocated for a request of 0 as a success. Returning false here contradicts the typical kernel behavior where allocating 0 items is a successful no-op, and might cause callers to incorrectly abort with an error.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
Quoted 4 lines · On Wed, May 27, 2026 at 07:27:17AM +0000, [email protected] wrote:
Does this omit setting
op_ctx->rsvd_page_tables.counttopt_counton success? Sinceop_ctxis initialized via memset previously, the count will remain 0, which could lead to memory leaks when these page tables are consumed or cleaned up later.
Yes, fixed.
Quoted 10 lines
lib/test_meminit.c static int __init do_kmem_cache_size(size_t size, bool want_ctor, · line 239
Quoted 6 lines
lib/test_meminit.c +1
-1}else {int i; -for (i =0 ; i < ret; i++) +for (i =0 ; i <BULK_SIZE ; i++) fail |=check_buf (bulk_array[i], size, want_ctor, want_rcu, want_zero);kmem_cache_free_bulk (c, ret, bulk_array);[Severity: Low]
Will this cause a compilation error? The local variable ret is removed from this scope, but it is still used in thekmem_cache_free_bulk()call above.
Yes, no idea why this didn't get included in my build after enabling it earlier.
Quoted 20 lines
Quoted 20 lines
lib/test_meminit.c +5
-3}@@ -354,17 +352,18 @@ static int __init do_kmem_cache_size_bulk(int size, int *total_failures) c =kmem_cache_create ("test_cache" , size, size,0 ,NULL );for (iter =0 ; (iter < maxiter) && !fail; iter++) { - num =kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), - objects); -for (i =0 ; i < num; i++) { +if (!kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), + objects)) +continue ; + +for (i =0 ; i <ARRAY_SIZE (objects); i++) { bytes =count_nonzero_bytes (objects[i], size);if (bytes) fail = true;fill_with_garbage (objects[i], size); }
Quoted 8 lines
lib/test_meminit.c static int __init do_kmem_cache_size_bulk(int size, int *total_failures) · line 366
Quoted 3 lines
lib/test_meminit.c +1
-2-if (num) -kmem_cache_free_bulk (c, num, objects); +kmem_cache_free_bulk (c, num, objects);[Severity: Medium]
Is num uninitialized here? It's no longer assigned the result of the allocation call, so passing it tokmem_cache_free_bulk()might use indeterminate stack data and lead to memory corruption.
Yes, this should use ARRAY_SIZE directly as well.
Quoted 6 lines
Quoted 6 lines
mm/slub.c +2
-0+bool kmem_cache_alloc_bulk_noprof (struct kmem_cache *s,gfp_t flags, +size_t size,void **p) {unsigned int i =0 ;void *kfence_obj;
Quoted 6 lines
mm/slub.c int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, · line 7344
Quoted 3 lines
mm/slub.c +1
-1if (!size) -return 0 ; +return false;[Severity: High]
Does this incorrectly report an allocation failure when size is 0?
It correctly reports one as that's how all callers treat a 0 return.
Update version in git here, not reposting until more human comments arrive:
https://git.infradead.org/?p=users/hch/misc.git;a=shortlog;h=refs/heads/fix-kmem_cache_alloc_bulk
[email protected]
AI reviewRe: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
In reply to Christoph HellwigDoes this omit setting op_ctx->rsvd_page_tables.count to pt_count on success?
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0@@ -1274,10 +1274,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, goto err_cleanup; }
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, · line 1282
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-4- ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, - op_ctx->rsvd_page_tables.pages); - op_ctx->rsvd_page_tables.count = ret; -if (ret != pt_count) { +if (!kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, + op_ctx->rsvd_page_tables.pages)) { + op_ctx->rsvd_page_tables.count =0 ; ret = -ENOMEM ; goto err_cleanup; }
Does this omit setting op_ctx->rsvd_page_tables.count to pt_count on success?
Since op_ctx is initialized via memset previously, the count will remain 0 when the allocation succeeds. This could lead to memory leaks when these page tables are consumed or cleaned up later, since the cleanup code won't know about the allocated page tables.
The original code set the count to ret on both success and failure. The updated code only sets count = 0 in the failure path, but doesn't set count = pt_count in the success path.
[ ... ]
Quoted 12 lines
lib/test_meminit.c +0
-4@@ -229,14 +229,12 @@ static int __init do_kmem_cache_size(size_t size, bool want_ctor,for (iter =0 ; iter <10 ; iter++) {/* Do a test of bulk allocations */ if (!want_rcu && !want_ctor) { -int ret; - - ret =kmem_cache_alloc_bulk (c, alloc_mask,BULK_SIZE , bulk_array); -if (!ret) {
lib/test_meminit.c static int __init do_kmem_cache_size(size_t size, bool want_ctor, · line 240
Quoted 10 lines
lib/test_meminit.c +3
-1+if (!kmem_cache_alloc_bulk (c, alloc_mask,BULK_SIZE , + bulk_array)) { fail = true; }else {int i; -for (i =0 ; i < ret; i++) +for (i =0 ; i <BULK_SIZE ; i++) fail |=check_buf (bulk_array[i], size, want_ctor, want_rcu, want_zero);kmem_cache_free_bulk (c, ret, bulk_array); }
Will this cause a compilation error?
The local variable ret is removed from this scope, but it's still used in the kmem_cache_free_bulk() call. This should fail to compile because ret is undeclared.
The fix should probably replace ret with BULK_SIZE in the free call:
kmem_cache_free_bulk(c, BULK_SIZE, bulk_array);
Quoted 14 lines
lib/test_meminit.c +5
-3@@ -354,17 +352,18 @@ static int __init do_kmem_cache_size_bulk(int size, int *total_failures) c =kmem_cache_create ("test_cache" , size, size,0 ,NULL );for (iter =0 ; (iter < maxiter) && !fail; iter++) { - num =kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), - objects); -for (i =0 ; i < num; i++) { +if (!kmem_cache_alloc_bulk (c,GFP_KERNEL ,ARRAY_SIZE (objects), + objects)) +continue ; + +for (i =0 ; i <ARRAY_SIZE (objects); i++) { bytes =count_nonzero_bytes (objects[i], size);if (bytes)
lib/test_meminit.c static int __init do_kmem_cache_size_bulk(int size, int *total_failures) · line 369
Quoted 10 lines
lib/test_meminit.c +1
-2fail = true;fill_with_garbage (objects[i], size); } -if (num) -kmem_cache_free_bulk (c, num, objects); +kmem_cache_free_bulk (c, num, objects); }kmem_cache_destroy (c); *total_failures += fail;
Is num uninitialized here?
It's no longer assigned the result of the allocation call. Passing it to kmem_cache_free_bulk() will use indeterminate stack data, which could lead to memory corruption. The fix should probably be:
kmem_cache_free_bulk(c, ARRAY_SIZE(objects), objects);
[ ... ]
Quoted 4 lines
mm/slub.c +0
-0
[ ... ]
mm/slub.c int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size, · line 7367
Quoted 8 lines
mm/slub.c +1
-1@@ -7352,18 +7362,18 @@ int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size,*/ unsigned int i =0 ;void *kfence_obj;if (!size) -return 0 ; +return false;
Does this incorrectly report an allocation failure when size is 0?
Previously, callers interpreting the return value against size (ret != size) would treat 0 objects allocated for a request of 0 as a success. Returning false here contradicts the typical kernel behavior where allocating 0 items is a successful no-op.
This might cause callers to incorrectly abort with an error when they request 0 objects, which should be a valid no-op case.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26496962101
Re: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
In reply to Christoph HellwigQuoted 6 lines · Christoph Hellwig
The
kmem_cache_alloc_bulkreturn value is weird. It returns the number of allocated objects, but that must always be 0 or the requested number based on the implementations and the handling in the callers, but that assumption is not actually documented anywhere, which confuses automated review tools.
I remember, this API behavior was requested by AKPM when I developed kmem_cache_alloc_bulk. I trusted AKPM's decision, but I cannot explain why this choice was made.
I kept the netdev code usage below. The current napi_skb_cache_get_bulk have a retry logic that assumes that a partial bulk number can be returned (which it cannot as Hellwig explains). Cc Alex/Olek please review the changes below as you added this retry logic.
Quoted 19 lines
Fix this by returning a bool if the allocation succeeded and adding a kerneldoc comment explaining the API.
Signed-off-by Christoph Hellwig <[email protected]>--- drivers/gpu/drm/msm/msm_iommu.c | 6 +-- drivers/gpu/drm/panthor/panthor_mmu.c | 12 +++--- include/linux/slab.h | 6 ++- io_uring/io_uring.c | 23 +++++------ lib/test_meminit.c | 19 +++++---- mm/kasan/kasan_test_c.c | 5 +-- mm/kfence/kfence_test.c | 9 +++-- mm/slub.c | 58 +++++++++++++++------------ net/bpf/test_run.c | 7 ++-- net/core/skbuff.c | 24 ++++++----- tools/include/linux/slab.h | 2 +- tools/testing/shared/linux.c | 19 ++++----- 12 files changed, 93 insertions(+), 97 deletions(-)
[...]
Quoted 46 lines
net/core/skbuff.c +13
-11@@ -288,11 +288,11 @@ static inline struct sk_buff *napi_skb_cache_get(bool alloc)local_lock_nested_bh (&napi_alloc_cache.bh_lock);if (unlikely (!nc->skb_count)) { -if (alloc) - nc->skb_count =kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, -GFP_ATOMIC | __GFP_NOWARN, -NAPI_SKB_CACHE_BULK , - nc->skb_cache); +if (alloc &&kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, +GFP_ATOMIC | __GFP_NOWARN, +NAPI_SKB_CACHE_BULK , + nc->skb_cache)) + nc->skb_count =NAPI_SKB_CACHE_BULK ;if (unlikely (!nc->skb_count)) {local_unlock_nested_bh (&napi_alloc_cache.bh_lock);return NULL ;@@ -353,16 +353,18 @@ u32 napi_skb_cache_get_bulk(void **skbs, u32 n)/* No enough cached skbs. Try refilling the cache first */ bulk =min (NAPI_SKB_CACHE_SIZE - nc->skb_count,NAPI_SKB_CACHE_BULK ); - nc->skb_count +=kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, -GFP_ATOMIC | __GFP_NOWARN, bulk, - &nc->skb_cache[nc->skb_count]); +if (kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, +GFP_ATOMIC | __GFP_NOWARN, bulk, + &nc->skb_cache[nc->skb_count])) + nc->skb_count += bulk;if (likely (nc->skb_count >= n)) goto get;/* Still not enough. Bulk-allocate the missing part directly, zeroed */ - n -=kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, -GFP_ATOMIC | __GFP_ZERO | __GFP_NOWARN, - n - nc->skb_count, &skbs[nc->skb_count]); +if (kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, +GFP_ATOMIC | __GFP_ZERO | __GFP_NOWARN, + n - nc->skb_count, &skbs[nc->skb_count])) + n = nc->skb_count;if (likely (nc->skb_count >= n)) goto get;
Alexander Lobakin
Reviewed-byRe: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
From: Jesper Dangaard Brouer <[email protected]>
Date: Wed, 27 May 2026 10:51:42 +0200
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines
The
kmem_cache_alloc_bulkreturn value is weird. It returns the number of allocated objects, but that must always be 0 or the requested number based on the implementations and the handling in the callers, but that assumption is not actually documented anywhere, which confuses automated review tools.
Quoted 3 lines
I remember, this API behavior was requested by AKPM when I developed kmem_cache_alloc_bulk. I trusted AKPM's decision, but I cannot explain why this choice was made.
I sorta remember that when I was reading this function, I also noticed that it always returns only 2 possible values (0 or the requested number), but didn't pay enough attention or it was already after I introduced napi_skb_cache_get_bulk().
Quoted 5 lines
I kept the netdev code usage below. The current
napi_skb_cache_get_bulkhave a retry logic that assumes that a partial bulk number can be returned (which it cannot as Hellwig explains). Cc Alex/Olek please review the changes below as you added this retry logic.
As far as I can see, the diff below doesn't introduce any functional changes (but allows for a bit better compiler optimization). The logic is still the same:
1) try to allocate non-zeroed skbs into the cache
2) if not enough, try to allocate zeroed skbs directly
3) if still not enough, return less than requested
The logic is still valid even if kmem_cache_alloc_bulk() return bool -- we might have some skbs in the cache (but less than requested) and then the first allocation try may fail, but the second one succeed (as it allocates from a different (the zeroed) zone).
Quoted 60 lines
Quoted 21 lines
Fix this by returning a bool if the allocation succeeded and adding a kerneldoc comment explaining the API.
Signed-off-by Christoph Hellwig <[email protected]>---
drivers/gpu/drm/msm/msm_iommu.c | 6 +--drivers/gpu/drm/panthor/panthor_mmu.c| 12 +++--- include/linux/slab.h | 6 ++- io_uring/io_uring.c | 23 +++++------ lib/test_meminit.c | 19 +++++---- mm/kasan/kasan_test_c.c | 5 +-- mm/kfence/kfence_test.c | 9 +++--
mm/slub.c | 58 +++++++++++++++------------ net/bpf/test_run.c | 7 ++-- net/core/skbuff.c | 24 ++++++----- tools/include/linux/slab.h | 2 +- tools/testing/shared/linux.c | 19 ++++-----12 files changed, 93 insertions(+), 97 deletions(-)[...]
Quoted 36 lines
net/core/skbuff.c +5
-5@@ -288,11 +288,11 @@ static inline struct sk_buff *napi_skb_cache_get(bool alloc) local_lock_nested_bh(&napi_alloc_cache.bh_lock); if (unlikely(!nc->skb_count)) { - if (alloc) - nc->skb_count =kmem_cache_alloc_bulk (net_hotdata.skbuff_cache, - GFP_ATOMIC | __GFP_NOWARN, - NAPI_SKB_CACHE_BULK, - nc->skb_cache); + if (alloc && kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, + GFP_ATOMIC | __GFP_NOWARN, + NAPI_SKB_CACHE_BULK, + nc->skb_cache)) + nc->skb_count = NAPI_SKB_CACHE_BULK; if (unlikely(!nc->skb_count)) { local_unlock_nested_bh(&napi_alloc_cache.bh_lock);return NULL;
net/core/skbuff.c +4
-3@@ -353,16 +353,18 @@ u32 napi_skb_cache_get_bulk(void **skbs, u32 n) /* No enough cached skbs. Try refilling the cache first */ bulk = min(NAPI_SKB_CACHE_SIZE - nc->skb_count,NAPI_SKB_CACHE_BULK ); - nc->skb_count += kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, - GFP_ATOMIC | __GFP_NOWARN, bulk, - &nc->skb_cache[nc->skb_count]); + if (kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, + GFP_ATOMIC | __GFP_NOWARN, bulk, + &nc->skb_cache[nc->skb_count])) + nc->skb_count += bulk; if (likely(nc->skb_count >= n)) goto get; /* Still not enough. Bulk-allocate the missing part directly,
Quoted 8 lines
Quoted 8 lines
zeroed */
- n -= kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, -GFP_ATOMIC|__GFP_ZERO|__GFP_NOWARN, - n -nc->skb_count, &skbs[nc->skb_count]); + if (kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, +GFP_ATOMIC|__GFP_ZERO|__GFP_NOWARN, + n -nc->skb_count, &skbs[nc->skb_count])) + n =nc->skb_count;
kmem_cache_alloc_bulk() allocates n - nc->skb_count, but here you assign nc->skb_count to n.
Ah wait,
n -= n - nc->skb_count
n = n - (n - nc->skb_count)
n = n - n + nc->skb_count
n = nc->skb_count
Correct :D
Quoted 2 lines
Quoted 2 lines
if (
likely(nc->skb_count >= n))
goto get;
Thanks,
Olek
Re: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
Quoted 12 lines · Alexander Lobakin
Quoted 7 lines
Quoted 7 lines
- n -= kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, -
GFP_ATOMIC|__GFP_ZERO|__GFP_NOWARN, - n -nc->skb_count, &skbs[nc->skb_count]); + if (kmem_cache_alloc_bulk(net_hotdata.skbuff_cache, +GFP_ATOMIC|__GFP_ZERO|__GFP_NOWARN, + n -nc->skb_count, &skbs[nc->skb_count])) + n =nc->skb_count;
kmem_cache_alloc_bulk()allocatesn - nc->skb_count, but here you assignnc->skb_countto n.
Ah wait,
Quoted 6 lines
n -= n -
nc->skb_count
n = n - (n -nc->skb_count)
n = n - n +nc->skb_count
n =nc->skb_countCorrect :D
Exactly the steps I went through when writing this patch :)
Vlastimil Babka (SUSE)
maintainerRe: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
In reply to Christoph HellwigQuoted 10 lines · Christoph Hellwig
The
kmem_cache_alloc_bulkreturn value is weird. It returns the number of allocated objects, but that must always be 0 or the requested number based on the implementations and the handling in the callers, but that assumption is not actually documented anywhere, which confuses automated review tools.Fix this by returning a bool if the allocation succeeded and adding a kerneldoc comment explaining the API.
Signed-off-by Christoph Hellwig <[email protected]>
Would 0 / -ENOMEM be more like what people would expect? I guess both that and bool are better than the current API.
Re: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
Quoted 7 lines · Vlastimil Babka (SUSE)
Quoted 6 lines · Christoph Hellwig
The
kmem_cache_alloc_bulkreturn value is weird. It returns the number of allocated objects, but that must always be 0 or the requested number based on the implementations and the handling in the callers, but that assumption is not actually documented anywhere, which confuses automated review tools.
Quoted 7 lines
Quoted 4 lines
Fix this by returning a bool if the allocation succeeded and adding a kerneldoc comment explaining the API.
Signed-off-by Christoph Hellwig <[email protected]>Would 0 /
-ENOMEMbe more like what people would expect? I guess both that and bool are better than the current API.
I find an errno return where the API could not return anything but the specific error code a bit odd. But even that would be a lot better than the current version.
kernel test robot
build botRe: [PATCH] mm/slab: improve kmem_cache_alloc_bulk
In reply to Christoph Hellwigkernel test robot noticed the following build errors:
Hi Christoph,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Christoph-Hellwig/mm-slab-improve-kmem_cache_alloc_bulk/20260527-150421 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20260527070239.2252948-2-hch%40lst.de patch subject: [PATCH] mm/slab: improve kmem_cache_alloc_bulk config: parisc-randconfig-r071-20260528 (https://download.01.org/0day-ci/archive/20260528/[email protected]/config) compiler: hppa-linux-gcc (GCC) 8.5.0 smatch: v0.5.0-9185-gbcc58b9c reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260528/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
lib/test_meminit.c: In function 'do_kmem_cache_size':
>> lib/test_meminit.c:239:29: error: 'ret' undeclared (first use in this function); did you mean 'net'?
kmem_cache_free_bulk(c, ret, bulk_array);
^~~
net lib/test_meminit.c:239:29: note: each undeclared identifier is reported only once for each function it appears in
vim +239 lib/test_meminit.c
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 209
5015a300a522c8f Alexander Potapenko 2019-07-16 210 /*
5015a300a522c8f Alexander Potapenko 2019-07-16 211 * Test kmem_cache with given parameters:
5015a300a522c8f Alexander Potapenko 2019-07-16 212 * want_ctor - use a constructor;
5015a300a522c8f Alexander Potapenko 2019-07-16 213 * want_rcu - use SLAB_TYPESAFE_BY_RCU;
5015a300a522c8f Alexander Potapenko 2019-07-16 214 * want_zero - use __GFP_ZERO.
5015a300a522c8f Alexander Potapenko 2019-07-16 215 */
5015a300a522c8f Alexander Potapenko 2019-07-16 216 static int __init do_kmem_cache_size(size_t size, bool want_ctor,
5015a300a522c8f Alexander Potapenko 2019-07-16 217 bool want_rcu, bool want_zero,
5015a300a522c8f Alexander Potapenko 2019-07-16 218 int *total_failures)
5015a300a522c8f Alexander Potapenko 2019-07-16 219 {
5015a300a522c8f Alexander Potapenko 2019-07-16 220 struct kmem_cache *c;
5015a300a522c8f Alexander Potapenko 2019-07-16 221 int iter;
5015a300a522c8f Alexander Potapenko 2019-07-16 222 bool fail = false;
5015a300a522c8f Alexander Potapenko 2019-07-16 223 gfp_t alloc_mask = GFP_KERNEL | (want_zero ? __GFP_ZERO : 0);
5015a300a522c8f Alexander Potapenko 2019-07-16 224 void *buf, *buf_copy;
5015a300a522c8f Alexander Potapenko 2019-07-16 225
5015a300a522c8f Alexander Potapenko 2019-07-16 226 c = kmem_cache_create("test_cache", size, 1,
5015a300a522c8f Alexander Potapenko 2019-07-16 227 want_rcu ? SLAB_TYPESAFE_BY_RCU : 0,
5015a300a522c8f Alexander Potapenko 2019-07-16 228 want_ctor ? test_ctor : NULL);
5015a300a522c8f Alexander Potapenko 2019-07-16 229 for (iter = 0; iter < 10; iter++) {
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 230 /* Do a test of bulk allocations */
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 231 if (!want_rcu && !want_ctor) {
863d4ee245fb4d6 Christoph Hellwig 2026-05-27 232 if (!kmem_cache_alloc_bulk(c, alloc_mask, BULK_SIZE,
863d4ee245fb4d6 Christoph Hellwig 2026-05-27 233 bulk_array)) {
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 234 fail = true;
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 235 } else {
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 236 int i;
863d4ee245fb4d6 Christoph Hellwig 2026-05-27 237 for (i = 0; i < BULK_SIZE; i++)
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 238 fail |= check_buf(bulk_array[i], size, want_ctor, want_rcu, want_zero);
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 @239 kmem_cache_free_bulk(c, ret, bulk_array);
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 240 }
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 241 }
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 242
5015a300a522c8f Alexander Potapenko 2019-07-16 243 buf = kmem_cache_alloc(c, alloc_mask);
5015a300a522c8f Alexander Potapenko 2019-07-16 244 /* Check that buf is zeroed, if it must be. */
dc5c5ad79f0cc2d Laura Abbott 2019-12-04 245 fail |= check_buf(buf, size, want_ctor, want_rcu, want_zero);
5015a300a522c8f Alexander Potapenko 2019-07-16 246 fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);
d3a811617ae629d Arnd Bergmann 2019-07-16 247
d3a811617ae629d Arnd Bergmann 2019-07-16 248 if (!want_rcu) {
d3a811617ae629d Arnd Bergmann 2019-07-16 249 kmem_cache_free(c, buf);
d3a811617ae629d Arnd Bergmann 2019-07-16 250 continue;
d3a811617ae629d Arnd Bergmann 2019-07-16 251 }
d3a811617ae629d Arnd Bergmann 2019-07-16 252
5015a300a522c8f Alexander Potapenko 2019-07-16 253 /*
5015a300a522c8f Alexander Potapenko 2019-07-16 254 * If this is an RCU cache, use a critical section to ensure we
5015a300a522c8f Alexander Potapenko 2019-07-16 255 * can touch objects after they're freed.
5015a300a522c8f Alexander Potapenko 2019-07-16 256 */
5015a300a522c8f Alexander Potapenko 2019-07-16 257 rcu_read_lock();
5015a300a522c8f Alexander Potapenko 2019-07-16 258 /*
5015a300a522c8f Alexander Potapenko 2019-07-16 259 * Copy the buffer to check that it's not wiped on
5015a300a522c8f Alexander Potapenko 2019-07-16 260 * free().
5015a300a522c8f Alexander Potapenko 2019-07-16 261 */
733d1d1a7745113 Alexander Potapenko 2019-08-02 262 buf_copy = kmalloc(size, GFP_ATOMIC);
5015a300a522c8f Alexander Potapenko 2019-07-16 263 if (buf_copy)
5015a300a522c8f Alexander Potapenko 2019-07-16 264 memcpy(buf_copy, buf, size);
d3a811617ae629d Arnd Bergmann 2019-07-16 265
4ab7ace465466d2 Alexander Potapenko 2019-07-16 266 kmem_cache_free(c, buf);
5015a300a522c8f Alexander Potapenko 2019-07-16 267 /*
5015a300a522c8f Alexander Potapenko 2019-07-16 268 * Check that |buf| is intact after kmem_cache_free().
5015a300a522c8f Alexander Potapenko 2019-07-16 269 * |want_zero| is false, because we wrote garbage to
5015a300a522c8f Alexander Potapenko 2019-07-16 270 * the buffer already.
5015a300a522c8f Alexander Potapenko 2019-07-16 271 */
5015a300a522c8f Alexander Potapenko 2019-07-16 272 fail |= check_buf(buf, size, want_ctor, want_rcu,
5015a300a522c8f Alexander Potapenko 2019-07-16 273 false);
5015a300a522c8f Alexander Potapenko 2019-07-16 274 if (buf_copy) {
5015a300a522c8f Alexander Potapenko 2019-07-16 275 fail |= (bool)memcmp(buf, buf_copy, size);
5015a300a522c8f Alexander Potapenko 2019-07-16 276 kfree(buf_copy);
5015a300a522c8f Alexander Potapenko 2019-07-16 277 }
5015a300a522c8f Alexander Potapenko 2019-07-16 278 rcu_read_unlock();
5015a300a522c8f Alexander Potapenko 2019-07-16 279 }
5015a300a522c8f Alexander Potapenko 2019-07-16 280 kmem_cache_destroy(c);
5015a300a522c8f Alexander Potapenko 2019-07-16 281
5015a300a522c8f Alexander Potapenko 2019-07-16 282 *total_failures += fail;
5015a300a522c8f Alexander Potapenko 2019-07-16 283 return 1;
5015a300a522c8f Alexander Potapenko 2019-07-16 284 }
5015a300a522c8f Alexander Potapenko 2019-07-16 285 --
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Vlastimil Babka (SUSE)
maintainerQuoted 8 lines · Christoph Hellwig
Hi all,
kmem_cache_alloc_bulkhas a very unintuitive and undocumented return value convention. Fix that and add documentation.Note that the few comments explaining it mention that the gfp flags must allow "spinning". That's not really a term used in the memory allocator, is this supposed to mean "block" or "sleep"?
Page allocator now has alloc_pages_nolock() for when no spinning is possible, and it uses ALLOC_TRYLOCK internally.
Slab has kmalloc_nolock() relying on that when it needs new pages.
In terms of gfp flags, such context is currently indicated by lack of __GFP_KSWAPD_RECLAIM, where lack of __GFP_DIRECT_RECLAIM only means "no sleeping" - see gfpflags_allow_spinning(). Slab uses it internally as there's no ALLOC_TRYLOCK, but also there are callers from memcg and stackdepot.
Like the rest of gfp flags it's far from ideal, maybe we'll figure out a better design eventually.
Quoted 14 lines
Diffstat:
drivers/gpu/drm/msm/msm_iommu.c | 6 +-- drivers/gpu/drm/panthor/panthor_mmu.c | 12 ++----- include/linux/slab.h | 6 ++- io_uring/io_uring.c | 23 +++++-------- lib/test_meminit.c | 19 +++++------ mm/kasan/kasan_test_c.c | 5 +- mm/kfence/kfence_test.c | 9 ++--- mm/slub.c | 58 ++++++++++++++++++---------------- net/bpf/test_run.c | 7 +--- net/core/skbuff.c | 23 +++++++------ tools/include/linux/slab.h | 2 - tools/testing/shared/linux.c | 19 ++++------- 12 files changed, 92 insertions(+), 97 deletions(-)
Quoted 10 lines · Vlastimil Babka (SUSE)
Quoted 5 lines
value convention. Fix that and add documentation.
Note that the few comments explaining it mention that the gfp flags must allow "spinning". That's not really a term used in the memory allocator, is this supposed to mean "block" or "sleep"?
Page allocator now has
alloc_pages_nolock()for when no spinning is possible, and it usesALLOC_TRYLOCKinternally.Slab has
kmalloc_nolock()relying on that when it needs new pages.
The comment long predates that, and it isn't expressed using gfp flags, but by requiring separate functions so I somehow doubt that was meant. But I could also not see why it would not support GFP_ATOMIC / GFP_NOWAIT allocation, so I might just be confused.
Vlastimil Babka (SUSE)
maintainerQuoted 7 lines · Christoph Hellwig
Quoted 6 lines · Vlastimil Babka (SUSE)
Quoted 5 lines
value convention. Fix that and add documentation.
Note that the few comments explaining it mention that the gfp flags must allow "spinning". That's not really a term used in the memory allocator, is this supposed to mean "block" or "sleep"?
Quoted 6 lines
Quoted 4 lines
Page allocator now has
alloc_pages_nolock()for when no spinning is possible, and it usesALLOC_TRYLOCKinternally.Slab has
kmalloc_nolock()relying on that when it needs new pages.The comment long predates that, and it isn't expressed using gfp flags,
Do we both mean this comment?
-/* Note that interrupts must be enabled when calling this function. */ +/*
+ * Note that interrupts must be enabled when calling this function and gfp
+ * flags must allow spinning.
+ */
int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size,
void **p)
commit 46dea1744498 ("slab: refill sheaves from all nodes") from this January. Previously it was just interrupts enabled.
Quoted 1 line
but by requiring separate functions so I somehow doubt that was meant.
Yeah, it's expressed by the _nolock variants. But slab propagates it internally by the gfp flags, and since 46dea1744498 it affects kmem_cache_alloc_bulk().
Quoted 2 lines
But I could also not see why it would not support
GFP_ATOMIC/GFP_NOWAITallocation, so I might just be confused.
Yeah those are supported because they can spin, just not sleep.
Quoted 7 lines · Vlastimil Babka (SUSE)
commit
46dea1744498("slab: refill sheaves from all nodes") from this January. Previously it was just interrupts enabled.Quoted 1 line
but by requiring separate functions so I somehow doubt that was meant.
Yeah, it's expressed by the
_nolockvariants. But slab propagates it internally by the gfp flags, and since46dea1744498it affectskmem_cache_alloc_bulk().
So what are the GFP flags that affect spinning? I can't find anything related to that in either Documentation/core-api/memory-allocation.rst or include/linux/gfp_types.h.
Vlastimil Babka (SUSE)
maintainerQuoted 4 lines · Christoph Hellwig
Quoted 3 lines · Vlastimil Babka (SUSE)
commit
46dea1744498("slab: refill sheaves from all nodes") from this January. Previously it was just interrupts enabled.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
but by requiring separate functions so I somehow doubt that was meant.
Yeah, it's expressed by the
_nolockvariants. But slab propagates it internally by the gfp flags, and since46dea1744498it affectskmem_cache_alloc_bulk().So what are the GFP flags that affect spinning? I can't find anything related to that in either
Documentation/core-api/memory-allocation.rstorinclude/linux/gfp_types.h.
See gfpflags_allow_spinning(). The usage by slab and page allocator users was meant to be via _nolock() variants, not by removing the flags when calling the normal functions, hence it's not documented. But the abstraction has now leaked in the kmem_cache_alloc_bulk() case, hm. I think instead of documenting that, we should adjust the implementation in slab, if possible. I'll try to look into that.