slub_kunit.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <kunit/test.h>
  3. #include <kunit/test-bug.h>
  4. #include <linux/mm.h>
  5. #include <linux/slab.h>
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/rcupdate.h>
  9. #include "../mm/slab.h"
  10. static struct kunit_resource resource;
  11. static int slab_errors;
  12. /*
  13. * Wrapper function for kmem_cache_create(), which reduces 2 parameters:
  14. * 'align' and 'ctor', and sets SLAB_SKIP_KFENCE flag to avoid getting an
  15. * object from kfence pool, where the operation could be caught by both
  16. * our test and kfence sanity check.
  17. */
  18. static struct kmem_cache *test_kmem_cache_create(const char *name,
  19. unsigned int size, slab_flags_t flags)
  20. {
  21. struct kmem_cache *s = kmem_cache_create(name, size, 0,
  22. (flags | SLAB_NO_USER_FLAGS), NULL);
  23. s->flags |= SLAB_SKIP_KFENCE;
  24. return s;
  25. }
  26. static void test_clobber_zone(struct kunit *test)
  27. {
  28. struct kmem_cache *s = test_kmem_cache_create("TestSlub_RZ_alloc", 64,
  29. SLAB_RED_ZONE);
  30. u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
  31. kasan_disable_current();
  32. p[64] = 0x12;
  33. validate_slab_cache(s);
  34. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  35. kasan_enable_current();
  36. kmem_cache_free(s, p);
  37. kmem_cache_destroy(s);
  38. }
  39. #ifndef CONFIG_KASAN
  40. static void test_next_pointer(struct kunit *test)
  41. {
  42. struct kmem_cache *s = test_kmem_cache_create("TestSlub_next_ptr_free",
  43. 64, SLAB_POISON);
  44. u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
  45. unsigned long tmp;
  46. unsigned long *ptr_addr;
  47. kmem_cache_free(s, p);
  48. ptr_addr = (unsigned long *)(p + s->offset);
  49. tmp = *ptr_addr;
  50. p[s->offset] = ~p[s->offset];
  51. /*
  52. * Expecting three errors.
  53. * One for the corrupted freechain and the other one for the wrong
  54. * count of objects in use. The third error is fixing broken cache.
  55. */
  56. validate_slab_cache(s);
  57. KUNIT_EXPECT_EQ(test, 3, slab_errors);
  58. /*
  59. * Try to repair corrupted freepointer.
  60. * Still expecting two errors. The first for the wrong count
  61. * of objects in use.
  62. * The second error is for fixing broken cache.
  63. */
  64. *ptr_addr = tmp;
  65. slab_errors = 0;
  66. validate_slab_cache(s);
  67. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  68. /*
  69. * Previous validation repaired the count of objects in use.
  70. * Now expecting no error.
  71. */
  72. slab_errors = 0;
  73. validate_slab_cache(s);
  74. KUNIT_EXPECT_EQ(test, 0, slab_errors);
  75. kmem_cache_destroy(s);
  76. }
  77. static void test_first_word(struct kunit *test)
  78. {
  79. struct kmem_cache *s = test_kmem_cache_create("TestSlub_1th_word_free",
  80. 64, SLAB_POISON);
  81. u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
  82. kmem_cache_free(s, p);
  83. *p = 0x78;
  84. validate_slab_cache(s);
  85. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  86. kmem_cache_destroy(s);
  87. }
  88. static void test_clobber_50th_byte(struct kunit *test)
  89. {
  90. struct kmem_cache *s = test_kmem_cache_create("TestSlub_50th_word_free",
  91. 64, SLAB_POISON);
  92. u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
  93. kmem_cache_free(s, p);
  94. p[50] = 0x9a;
  95. validate_slab_cache(s);
  96. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  97. kmem_cache_destroy(s);
  98. }
  99. #endif
  100. static void test_clobber_redzone_free(struct kunit *test)
  101. {
  102. struct kmem_cache *s = test_kmem_cache_create("TestSlub_RZ_free", 64,
  103. SLAB_RED_ZONE);
  104. u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
  105. kasan_disable_current();
  106. kmem_cache_free(s, p);
  107. p[64] = 0xab;
  108. validate_slab_cache(s);
  109. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  110. kasan_enable_current();
  111. kmem_cache_destroy(s);
  112. }
  113. static void test_kmalloc_redzone_access(struct kunit *test)
  114. {
  115. struct kmem_cache *s = test_kmem_cache_create("TestSlub_RZ_kmalloc", 32,
  116. SLAB_KMALLOC|SLAB_STORE_USER|SLAB_RED_ZONE);
  117. u8 *p = alloc_hooks(__kmalloc_cache_noprof(s, GFP_KERNEL, 18));
  118. kasan_disable_current();
  119. /* Suppress the -Warray-bounds warning */
  120. OPTIMIZER_HIDE_VAR(p);
  121. p[18] = 0xab;
  122. p[19] = 0xab;
  123. validate_slab_cache(s);
  124. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  125. kasan_enable_current();
  126. kmem_cache_free(s, p);
  127. kmem_cache_destroy(s);
  128. }
  129. struct test_kfree_rcu_struct {
  130. struct rcu_head rcu;
  131. };
  132. static void test_kfree_rcu(struct kunit *test)
  133. {
  134. struct kmem_cache *s;
  135. struct test_kfree_rcu_struct *p;
  136. if (IS_BUILTIN(CONFIG_SLUB_KUNIT_TEST))
  137. kunit_skip(test, "can't do kfree_rcu() when test is built-in");
  138. s = test_kmem_cache_create("TestSlub_kfree_rcu",
  139. sizeof(struct test_kfree_rcu_struct),
  140. SLAB_NO_MERGE);
  141. p = kmem_cache_alloc(s, GFP_KERNEL);
  142. kfree_rcu(p, rcu);
  143. kmem_cache_destroy(s);
  144. KUNIT_EXPECT_EQ(test, 0, slab_errors);
  145. }
  146. static void test_leak_destroy(struct kunit *test)
  147. {
  148. struct kmem_cache *s = test_kmem_cache_create("TestSlub_leak_destroy",
  149. 64, SLAB_NO_MERGE);
  150. kmem_cache_alloc(s, GFP_KERNEL);
  151. kmem_cache_destroy(s);
  152. KUNIT_EXPECT_EQ(test, 2, slab_errors);
  153. }
  154. static int test_init(struct kunit *test)
  155. {
  156. slab_errors = 0;
  157. kunit_add_named_resource(test, NULL, NULL, &resource,
  158. "slab_errors", &slab_errors);
  159. return 0;
  160. }
  161. static struct kunit_case test_cases[] = {
  162. KUNIT_CASE(test_clobber_zone),
  163. #ifndef CONFIG_KASAN
  164. KUNIT_CASE(test_next_pointer),
  165. KUNIT_CASE(test_first_word),
  166. KUNIT_CASE(test_clobber_50th_byte),
  167. #endif
  168. KUNIT_CASE(test_clobber_redzone_free),
  169. KUNIT_CASE(test_kmalloc_redzone_access),
  170. KUNIT_CASE(test_kfree_rcu),
  171. KUNIT_CASE(test_leak_destroy),
  172. {}
  173. };
  174. static struct kunit_suite test_suite = {
  175. .name = "slub_test",
  176. .init = test_init,
  177. .test_cases = test_cases,
  178. };
  179. kunit_test_suite(test_suite);
  180. MODULE_LICENSE("GPL");