test_meminit.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test cases for SL[AOU]B/page initialization at alloc/free time.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/mm.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. #include <linux/vmalloc.h>
  13. #define GARBAGE_INT (0x09A7BA9E)
  14. #define GARBAGE_BYTE (0x9E)
  15. #define REPORT_FAILURES_IN_FN() \
  16. do { \
  17. if (failures) \
  18. pr_info("%s failed %d out of %d times\n", \
  19. __func__, failures, num_tests); \
  20. else \
  21. pr_info("all %d tests in %s passed\n", \
  22. num_tests, __func__); \
  23. } while (0)
  24. /* Calculate the number of uninitialized bytes in the buffer. */
  25. static int __init count_nonzero_bytes(void *ptr, size_t size)
  26. {
  27. int i, ret = 0;
  28. unsigned char *p = (unsigned char *)ptr;
  29. for (i = 0; i < size; i++)
  30. if (p[i])
  31. ret++;
  32. return ret;
  33. }
  34. /* Fill a buffer with garbage, skipping |skip| first bytes. */
  35. static void __init fill_with_garbage_skip(void *ptr, int size, size_t skip)
  36. {
  37. unsigned int *p = (unsigned int *)((char *)ptr + skip);
  38. int i = 0;
  39. WARN_ON(skip > size);
  40. size -= skip;
  41. while (size >= sizeof(*p)) {
  42. p[i] = GARBAGE_INT;
  43. i++;
  44. size -= sizeof(*p);
  45. }
  46. if (size)
  47. memset(&p[i], GARBAGE_BYTE, size);
  48. }
  49. static void __init fill_with_garbage(void *ptr, size_t size)
  50. {
  51. fill_with_garbage_skip(ptr, size, 0);
  52. }
  53. static int __init do_alloc_pages_order(int order, int *total_failures)
  54. {
  55. struct page *page;
  56. void *buf;
  57. size_t size = PAGE_SIZE << order;
  58. page = alloc_pages(GFP_KERNEL, order);
  59. buf = page_address(page);
  60. fill_with_garbage(buf, size);
  61. __free_pages(page, order);
  62. page = alloc_pages(GFP_KERNEL, order);
  63. buf = page_address(page);
  64. if (count_nonzero_bytes(buf, size))
  65. (*total_failures)++;
  66. fill_with_garbage(buf, size);
  67. __free_pages(page, order);
  68. return 1;
  69. }
  70. /* Test the page allocator by calling alloc_pages with different orders. */
  71. static int __init test_pages(int *total_failures)
  72. {
  73. int failures = 0, num_tests = 0;
  74. int i;
  75. for (i = 0; i < 10; i++)
  76. num_tests += do_alloc_pages_order(i, &failures);
  77. REPORT_FAILURES_IN_FN();
  78. *total_failures += failures;
  79. return num_tests;
  80. }
  81. /* Test kmalloc() with given parameters. */
  82. static int __init do_kmalloc_size(size_t size, int *total_failures)
  83. {
  84. void *buf;
  85. buf = kmalloc(size, GFP_KERNEL);
  86. fill_with_garbage(buf, size);
  87. kfree(buf);
  88. buf = kmalloc(size, GFP_KERNEL);
  89. if (count_nonzero_bytes(buf, size))
  90. (*total_failures)++;
  91. fill_with_garbage(buf, size);
  92. kfree(buf);
  93. return 1;
  94. }
  95. /* Test vmalloc() with given parameters. */
  96. static int __init do_vmalloc_size(size_t size, int *total_failures)
  97. {
  98. void *buf;
  99. buf = vmalloc(size);
  100. fill_with_garbage(buf, size);
  101. vfree(buf);
  102. buf = vmalloc(size);
  103. if (count_nonzero_bytes(buf, size))
  104. (*total_failures)++;
  105. fill_with_garbage(buf, size);
  106. vfree(buf);
  107. return 1;
  108. }
  109. /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
  110. static int __init test_kvmalloc(int *total_failures)
  111. {
  112. int failures = 0, num_tests = 0;
  113. int i, size;
  114. for (i = 0; i < 20; i++) {
  115. size = 1 << i;
  116. num_tests += do_kmalloc_size(size, &failures);
  117. num_tests += do_vmalloc_size(size, &failures);
  118. }
  119. REPORT_FAILURES_IN_FN();
  120. *total_failures += failures;
  121. return num_tests;
  122. }
  123. #define CTOR_BYTES (sizeof(unsigned int))
  124. #define CTOR_PATTERN (0x41414141)
  125. /* Initialize the first 4 bytes of the object. */
  126. static void test_ctor(void *obj)
  127. {
  128. *(unsigned int *)obj = CTOR_PATTERN;
  129. }
  130. /*
  131. * Check the invariants for the buffer allocated from a slab cache.
  132. * If the cache has a test constructor, the first 4 bytes of the object must
  133. * always remain equal to CTOR_PATTERN.
  134. * If the cache isn't an RCU-typesafe one, or if the allocation is done with
  135. * __GFP_ZERO, then the object contents must be zeroed after allocation.
  136. * If the cache is an RCU-typesafe one, the object contents must never be
  137. * zeroed after the first use. This is checked by memcmp() in
  138. * do_kmem_cache_size().
  139. */
  140. static bool __init check_buf(void *buf, int size, bool want_ctor,
  141. bool want_rcu, bool want_zero)
  142. {
  143. int bytes;
  144. bool fail = false;
  145. bytes = count_nonzero_bytes(buf, size);
  146. WARN_ON(want_ctor && want_zero);
  147. if (want_zero)
  148. return bytes;
  149. if (want_ctor) {
  150. if (*(unsigned int *)buf != CTOR_PATTERN)
  151. fail = 1;
  152. } else {
  153. if (bytes)
  154. fail = !want_rcu;
  155. }
  156. return fail;
  157. }
  158. #define BULK_SIZE 100
  159. static void *bulk_array[BULK_SIZE];
  160. /*
  161. * Test kmem_cache with given parameters:
  162. * want_ctor - use a constructor;
  163. * want_rcu - use SLAB_TYPESAFE_BY_RCU;
  164. * want_zero - use __GFP_ZERO.
  165. */
  166. static int __init do_kmem_cache_size(size_t size, bool want_ctor,
  167. bool want_rcu, bool want_zero,
  168. int *total_failures)
  169. {
  170. struct kmem_cache *c;
  171. int iter;
  172. bool fail = false;
  173. gfp_t alloc_mask = GFP_KERNEL | (want_zero ? __GFP_ZERO : 0);
  174. void *buf, *buf_copy;
  175. c = kmem_cache_create("test_cache", size, 1,
  176. want_rcu ? SLAB_TYPESAFE_BY_RCU : 0,
  177. want_ctor ? test_ctor : NULL);
  178. for (iter = 0; iter < 10; iter++) {
  179. /* Do a test of bulk allocations */
  180. if (!want_rcu && !want_ctor) {
  181. int ret;
  182. ret = kmem_cache_alloc_bulk(c, alloc_mask, BULK_SIZE, bulk_array);
  183. if (!ret) {
  184. fail = true;
  185. } else {
  186. int i;
  187. for (i = 0; i < ret; i++)
  188. fail |= check_buf(bulk_array[i], size, want_ctor, want_rcu, want_zero);
  189. kmem_cache_free_bulk(c, ret, bulk_array);
  190. }
  191. }
  192. buf = kmem_cache_alloc(c, alloc_mask);
  193. /* Check that buf is zeroed, if it must be. */
  194. fail |= check_buf(buf, size, want_ctor, want_rcu, want_zero);
  195. fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);
  196. if (!want_rcu) {
  197. kmem_cache_free(c, buf);
  198. continue;
  199. }
  200. /*
  201. * If this is an RCU cache, use a critical section to ensure we
  202. * can touch objects after they're freed.
  203. */
  204. rcu_read_lock();
  205. /*
  206. * Copy the buffer to check that it's not wiped on
  207. * free().
  208. */
  209. buf_copy = kmalloc(size, GFP_ATOMIC);
  210. if (buf_copy)
  211. memcpy(buf_copy, buf, size);
  212. kmem_cache_free(c, buf);
  213. /*
  214. * Check that |buf| is intact after kmem_cache_free().
  215. * |want_zero| is false, because we wrote garbage to
  216. * the buffer already.
  217. */
  218. fail |= check_buf(buf, size, want_ctor, want_rcu,
  219. false);
  220. if (buf_copy) {
  221. fail |= (bool)memcmp(buf, buf_copy, size);
  222. kfree(buf_copy);
  223. }
  224. rcu_read_unlock();
  225. }
  226. kmem_cache_destroy(c);
  227. *total_failures += fail;
  228. return 1;
  229. }
  230. /*
  231. * Check that the data written to an RCU-allocated object survives
  232. * reallocation.
  233. */
  234. static int __init do_kmem_cache_rcu_persistent(int size, int *total_failures)
  235. {
  236. struct kmem_cache *c;
  237. void *buf, *buf_contents, *saved_ptr;
  238. void **used_objects;
  239. int i, iter, maxiter = 1024;
  240. bool fail = false;
  241. c = kmem_cache_create("test_cache", size, size, SLAB_TYPESAFE_BY_RCU,
  242. NULL);
  243. buf = kmem_cache_alloc(c, GFP_KERNEL);
  244. saved_ptr = buf;
  245. fill_with_garbage(buf, size);
  246. buf_contents = kmalloc(size, GFP_KERNEL);
  247. if (!buf_contents)
  248. goto out;
  249. used_objects = kmalloc_array(maxiter, sizeof(void *), GFP_KERNEL);
  250. if (!used_objects) {
  251. kfree(buf_contents);
  252. goto out;
  253. }
  254. memcpy(buf_contents, buf, size);
  255. kmem_cache_free(c, buf);
  256. /*
  257. * Run for a fixed number of iterations. If we never hit saved_ptr,
  258. * assume the test passes.
  259. */
  260. for (iter = 0; iter < maxiter; iter++) {
  261. buf = kmem_cache_alloc(c, GFP_KERNEL);
  262. used_objects[iter] = buf;
  263. if (buf == saved_ptr) {
  264. fail = memcmp(buf_contents, buf, size);
  265. for (i = 0; i <= iter; i++)
  266. kmem_cache_free(c, used_objects[i]);
  267. goto free_out;
  268. }
  269. }
  270. free_out:
  271. kmem_cache_destroy(c);
  272. kfree(buf_contents);
  273. kfree(used_objects);
  274. out:
  275. *total_failures += fail;
  276. return 1;
  277. }
  278. static int __init do_kmem_cache_size_bulk(int size, int *total_failures)
  279. {
  280. struct kmem_cache *c;
  281. int i, iter, maxiter = 1024;
  282. int num, bytes;
  283. bool fail = false;
  284. void *objects[10];
  285. c = kmem_cache_create("test_cache", size, size, 0, NULL);
  286. for (iter = 0; (iter < maxiter) && !fail; iter++) {
  287. num = kmem_cache_alloc_bulk(c, GFP_KERNEL, ARRAY_SIZE(objects),
  288. objects);
  289. for (i = 0; i < num; i++) {
  290. bytes = count_nonzero_bytes(objects[i], size);
  291. if (bytes)
  292. fail = true;
  293. fill_with_garbage(objects[i], size);
  294. }
  295. if (num)
  296. kmem_cache_free_bulk(c, num, objects);
  297. }
  298. kmem_cache_destroy(c);
  299. *total_failures += fail;
  300. return 1;
  301. }
  302. /*
  303. * Test kmem_cache allocation by creating caches of different sizes, with and
  304. * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
  305. */
  306. static int __init test_kmemcache(int *total_failures)
  307. {
  308. int failures = 0, num_tests = 0;
  309. int i, flags, size;
  310. bool ctor, rcu, zero;
  311. for (i = 0; i < 10; i++) {
  312. size = 8 << i;
  313. for (flags = 0; flags < 8; flags++) {
  314. ctor = flags & 1;
  315. rcu = flags & 2;
  316. zero = flags & 4;
  317. if (ctor & zero)
  318. continue;
  319. num_tests += do_kmem_cache_size(size, ctor, rcu, zero,
  320. &failures);
  321. }
  322. num_tests += do_kmem_cache_size_bulk(size, &failures);
  323. }
  324. REPORT_FAILURES_IN_FN();
  325. *total_failures += failures;
  326. return num_tests;
  327. }
  328. /* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */
  329. static int __init test_rcu_persistent(int *total_failures)
  330. {
  331. int failures = 0, num_tests = 0;
  332. int i, size;
  333. for (i = 0; i < 10; i++) {
  334. size = 8 << i;
  335. num_tests += do_kmem_cache_rcu_persistent(size, &failures);
  336. }
  337. REPORT_FAILURES_IN_FN();
  338. *total_failures += failures;
  339. return num_tests;
  340. }
  341. /*
  342. * Run the tests. Each test function returns the number of executed tests and
  343. * updates |failures| with the number of failed tests.
  344. */
  345. static int __init test_meminit_init(void)
  346. {
  347. int failures = 0, num_tests = 0;
  348. num_tests += test_pages(&failures);
  349. num_tests += test_kvmalloc(&failures);
  350. num_tests += test_kmemcache(&failures);
  351. num_tests += test_rcu_persistent(&failures);
  352. if (failures == 0)
  353. pr_info("all %d tests passed!\n", num_tests);
  354. else
  355. pr_info("failures: %d out of %d\n", failures, num_tests);
  356. return failures ? -EINVAL : 0;
  357. }
  358. module_init(test_meminit_init);
  359. MODULE_LICENSE("GPL");