test_meminit.c 10 KB

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