]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - lib/test_kasan.c
ext4: xattr-in-inode support
[karo-tx-linux.git] / lib / test_kasan.c
index fbdf87920093b9b1dbd94e4b370a24e43d6bb8f7..a25c9763fce19f17c723b9db3645ae93ba47dcb6 100644 (file)
@@ -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>
@@ -19,6 +20,7 @@
 #include <linux/string.h>
 #include <linux/uaccess.h>
 #include <linux/module.h>
+#include <linux/kasan.h>
 
 /*
  * Note: test functions are marked noinline so that their names appear in
@@ -331,6 +333,38 @@ static noinline void __init kmem_cache_oob(void)
        kmem_cache_destroy(cache);
 }
 
+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");
+               return;
+       }
+
+       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);
+               msleep(100);
+       }
+
+free_cache:
+       kmem_cache_destroy(cache);
+}
+
 static char global_array[10];
 
 static noinline void __init kasan_global_oob(void)
@@ -441,6 +475,12 @@ static noinline void __init use_after_scope_test(void)
 
 static int __init kmalloc_tests_init(void)
 {
+       /*
+        * Temporarily enable multi-shot mode. Otherwise, we'd only get a
+        * report for the first case.
+        */
+       bool multishot = kasan_save_enable_multi_shot();
+
        kmalloc_oob_right();
        kmalloc_oob_left();
        kmalloc_node_oob_right();
@@ -460,11 +500,15 @@ static int __init kmalloc_tests_init(void)
        kmalloc_uaf_memset();
        kmalloc_uaf2();
        kmem_cache_oob();
+       memcg_accounted_kmem_cache();
        kasan_stack_oob();
        kasan_global_oob();
        ksize_unpoisons_memory();
        copy_user_test();
        use_after_scope_test();
+
+       kasan_restore_multi_shot(multishot);
+
        return -EAGAIN;
 }