Commit 0386bf38 authored by Greg Thelen's avatar Greg Thelen Committed by Linus Torvalds

kasan: add memcg kmem_cache test

Make a kasan test which uses a SLAB_ACCOUNT slab cache.  If the test is
run within a non default memcg, then it uncovers the bug fixed by
"kasan: drain quarantine of memcg slab objects"[1].

If run without fix [1] it shows "Slab cache still has objects", and the
kmem_cache structure is leaked.
Here's an unpatched kernel test:

 $ dmesg -c > /dev/null
 $ mkdir /sys/fs/cgroup/memory/test
 $ echo $$ > /sys/fs/cgroup/memory/test/tasks
 $ modprobe test_kasan 2> /dev/null
 $ dmesg | grep -B1 still
 [ 123.456789] kasan test: memcg_accounted_kmem_cache allocate memcg accounted object
 [ 124.456789] kmem_cache_destroy test_cache: Slab cache still has objects

Kernels with fix [1] don't have the "Slab cache still has objects"
warning or the underlying leak.

The new test runs and passes in the default (root) memcg, though in the
root memcg it won't uncover the problem fixed by [1].

Link: default avatarGreg Thelen <>
Reviewed-by: default avatarVladimir Davydov <>
Cc: Andrey Ryabinin <>
Cc: Alexander Potapenko <>
Cc: Dmitry Vyukov <>
Cc: Christoph Lameter <>
Cc: Pekka Enberg <>
Cc: David Rientjes <>
Cc: Joonsoo Kim <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent f9fa1d91
......@@ -11,6 +11,7 @@
#define pr_fmt(fmt) "kasan test: %s " fmt, __func__
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/mman.h>
#include <linux/mm.h>
......@@ -331,6 +332,38 @@ static noinline void __init kmem_cache_oob(void)
static noinline void __init memcg_accounted_kmem_cache(void)
int i;
char *p;
size_t size = 200;
struct kmem_cache *cache;
cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
if (!cache) {
pr_err("Cache allocation failed\n");
pr_info("allocate memcg accounted object\n");
* Several allocations with a delay to allow for lazy per memcg kmem
* cache creation.
for (i = 0; i < 5; i++) {
p = kmem_cache_alloc(cache, GFP_KERNEL);
if (!p) {
pr_err("Allocation failed\n");
goto free_cache;
kmem_cache_free(cache, p);
static char global_array[10];
static noinline void __init kasan_global_oob(void)
......@@ -460,6 +493,7 @@ static int __init kmalloc_tests_init(void)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment