#define REPORT_FAILURES_IN_FN() \ do { \ if (failures) \
pr_info("%s failed %d out of %d times\n", \
__func__, failures, num_tests); \ else \
pr_info("all %d tests in %s passed\n", \
num_tests, __func__); \
} while (0)
/* Calculate the number of uninitialized bytes in the buffer. */ staticint __init count_nonzero_bytes(void *ptr, size_t size)
{ int i, ret = 0; unsignedchar *p = (unsignedchar *)ptr;
for (i = 0; i < size; i++) if (p[i])
ret++; return ret;
}
/* Fill a buffer with garbage, skipping |skip| first bytes. */ staticvoid __init fill_with_garbage_skip(void *ptr, int size, size_t skip)
{ unsignedint *p = (unsignedint *)((char *)ptr + skip); int i = 0;
WARN_ON(skip > size);
size -= skip;
while (size >= sizeof(*p)) {
p[i] = GARBAGE_INT;
i++;
size -= sizeof(*p);
} if (size)
memset(&p[i], GARBAGE_BYTE, size);
}
/* Test the page allocator by calling alloc_pages with different orders. */ staticint __init test_pages(int *total_failures)
{ int failures = 0, num_tests = 0; int i;
for (i = 0; i < NR_PAGE_ORDERS; i++)
num_tests += do_alloc_pages_order(i, &failures);
/* Test kmalloc()/vmalloc() by allocating objects of different sizes. */ staticint __init test_kvmalloc(int *total_failures)
{ int failures = 0, num_tests = 0; int i, size;
for (i = 0; i < 20; i++) {
size = 1 << i;
num_tests += do_kmalloc_size(size, &failures);
num_tests += do_vmalloc_size(size, &failures);
}
#define CTOR_BYTES (sizeof(unsignedint)) #define CTOR_PATTERN (0x41414141) /* Initialize the first 4 bytes of the object. */ staticvoid test_ctor(void *obj)
{
*(unsignedint *)obj = CTOR_PATTERN;
}
/* * Check the invariants for the buffer allocated from a slab cache. * If the cache has a test constructor, the first 4 bytes of the object must * always remain equal to CTOR_PATTERN. * If the cache isn't an RCU-typesafe one, or if the allocation is done with * __GFP_ZERO, then the object contents must be zeroed after allocation. * If the cache is an RCU-typesafe one, the object contents must never be * zeroed after the first use. This is checked by memcmp() in * do_kmem_cache_size().
*/ staticbool __init check_buf(void *buf, int size, bool want_ctor, bool want_rcu, bool want_zero)
{ int bytes; bool fail = false;
bytes = count_nonzero_bytes(buf, size);
WARN_ON(want_ctor && want_zero); if (want_zero) return bytes; if (want_ctor) { if (*(unsignedint *)buf != CTOR_PATTERN)
fail = 1;
} else { if (bytes)
fail = !want_rcu;
} return fail;
}
/* * Test kmem_cache with given parameters: * want_ctor - use a constructor; * want_rcu - use SLAB_TYPESAFE_BY_RCU; * want_zero - use __GFP_ZERO.
*/ staticint __init do_kmem_cache_size(size_t size, bool want_ctor, bool want_rcu, bool want_zero, int *total_failures)
{ struct kmem_cache *c; int iter; bool fail = false;
gfp_t alloc_mask = GFP_KERNEL | (want_zero ? __GFP_ZERO : 0); void *buf, *buf_copy;
c = kmem_cache_create("test_cache", size, 1,
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);
fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);
if (!want_rcu) {
kmem_cache_free(c, buf); continue;
}
/* * If this is an RCU cache, use a critical section to ensure we * can touch objects after they're freed.
*/
rcu_read_lock(); /* * Copy the buffer to check that it's not wiped on * free().
*/
buf_copy = kmalloc(size, GFP_ATOMIC); if (buf_copy)
memcpy(buf_copy, buf, size);
kmem_cache_free(c, buf); /* * Check that |buf| is intact after kmem_cache_free(). * |want_zero| is false, because we wrote garbage to * the buffer already.
*/
fail |= check_buf(buf, size, want_ctor, want_rcu, false); if (buf_copy) {
fail |= (bool)memcmp(buf, buf_copy, size);
kfree(buf_copy);
}
rcu_read_unlock();
}
kmem_cache_destroy(c);
*total_failures += fail; return 1;
}
/* * Check that the data written to an RCU-allocated object survives * reallocation.
*/ staticint __init do_kmem_cache_rcu_persistent(int size, int *total_failures)
{ struct kmem_cache *c; void *buf, *buf_contents, *saved_ptr; void **used_objects; int i, iter, maxiter = 1024; bool fail = false;
c = kmem_cache_create("test_cache", size, size, SLAB_TYPESAFE_BY_RCU,
NULL);
buf = kmem_cache_alloc(c, GFP_KERNEL); if (!buf) goto out;
saved_ptr = buf;
fill_with_garbage(buf, size);
buf_contents = kmalloc(size, GFP_KERNEL); if (!buf_contents) {
kmem_cache_free(c, buf); goto out;
}
used_objects = kmalloc_array(maxiter, sizeof(void *), GFP_KERNEL); if (!used_objects) {
kmem_cache_free(c, buf);
kfree(buf_contents); goto out;
}
memcpy(buf_contents, buf, size);
kmem_cache_free(c, buf); /* * Run for a fixed number of iterations. If we never hit saved_ptr, * assume the test passes.
*/ for (iter = 0; iter < maxiter; iter++) {
buf = kmem_cache_alloc(c, GFP_KERNEL);
used_objects[iter] = buf; if (buf == saved_ptr) {
fail = memcmp(buf_contents, buf, size); for (i = 0; i <= iter; i++)
kmem_cache_free(c, used_objects[i]); goto free_out;
}
}
for (iter = 0; iter < maxiter; iter++)
kmem_cache_free(c, used_objects[iter]);
/* * Test kmem_cache allocation by creating caches of different sizes, with and * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
*/ staticint __init test_kmemcache(int *total_failures)
{ int failures = 0, num_tests = 0; int i, flags, size; bool ctor, rcu, zero;
/* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */ staticint __init test_rcu_persistent(int *total_failures)
{ int failures = 0, num_tests = 0; int i, size;
for (i = 0; i < 10; i++) {
size = 8 << i;
num_tests += do_kmem_cache_rcu_persistent(size, &failures);
}
REPORT_FAILURES_IN_FN();
*total_failures += failures; return num_tests;
}
/* * Run the tests. Each test function returns the number of executed tests and * updates |failures| with the number of failed tests.
*/ staticint __init test_meminit_init(void)
{ int failures = 0, num_tests = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.