Skip to content

Commit

Permalink
lib/test_meminit.c: add bulk alloc/free tests
Browse files Browse the repository at this point in the history
kmem_cache_alloc_bulk/kmem_cache_free_bulk are used to make multiple
allocations of the same size to avoid the overhead of multiple
kmalloc/kfree calls.  Extend the kmem_cache tests to make some calls to
these APIs.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Laura Abbott <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Tested-by: Alexander Potapenko <[email protected]>
Cc: Laura Abbott <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Kostya Serebryany <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Sandeep Patil <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: Marco Elver <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
labbott authored and torvalds committed Dec 5, 2019
1 parent 8b7569a commit dc5c5ad
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/test_meminit.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ static bool __init check_buf(void *buf, int size, bool want_ctor,
return fail;
}

#define BULK_SIZE 100
static void *bulk_array[BULK_SIZE];

/*
* Test kmem_cache with given parameters:
* want_ctor - use a constructor;
Expand All @@ -203,9 +206,24 @@ static int __init do_kmem_cache_size(size_t size, bool want_ctor,
want_rcu ? SLAB_TYPESAFE_BY_RCU : 0,
want_ctor ? test_ctor : NULL);
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) {
fail = true;
} else {
int i;
for (i = 0; i < ret; i++)
fail |= check_buf(bulk_array[i], size, want_ctor, want_rcu, want_zero);
kmem_cache_free_bulk(c, ret, bulk_array);
}
}

buf = kmem_cache_alloc(c, alloc_mask);
/* Check that buf is zeroed, if it must be. */
fail = check_buf(buf, size, want_ctor, want_rcu, want_zero);
fail |= check_buf(buf, size, want_ctor, want_rcu, want_zero);
fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);

if (!want_rcu) {
Expand Down

0 comments on commit dc5c5ad

Please sign in to comment.