binder_alloc_selftest.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* binder_alloc_selftest.c
  2. *
  3. * Android IPC Subsystem
  4. *
  5. * Copyright (C) 2017 Google, Inc.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/mm_types.h>
  19. #include <linux/err.h>
  20. #include "binder_alloc.h"
  21. #define BUFFER_NUM 5
  22. #define BUFFER_MIN_SIZE (PAGE_SIZE / 8)
  23. static bool binder_selftest_run = true;
  24. static int binder_selftest_failures;
  25. static DEFINE_MUTEX(binder_selftest_lock);
  26. /**
  27. * enum buf_end_align_type - Page alignment of a buffer
  28. * end with regard to the end of the previous buffer.
  29. *
  30. * In the pictures below, buf2 refers to the buffer we
  31. * are aligning. buf1 refers to previous buffer by addr.
  32. * Symbol [ means the start of a buffer, ] means the end
  33. * of a buffer, and | means page boundaries.
  34. */
  35. enum buf_end_align_type {
  36. /**
  37. * @SAME_PAGE_UNALIGNED: The end of this buffer is on
  38. * the same page as the end of the previous buffer and
  39. * is not page aligned. Examples:
  40. * buf1 ][ buf2 ][ ...
  41. * buf1 ]|[ buf2 ][ ...
  42. */
  43. SAME_PAGE_UNALIGNED = 0,
  44. /**
  45. * @SAME_PAGE_ALIGNED: When the end of the previous buffer
  46. * is not page aligned, the end of this buffer is on the
  47. * same page as the end of the previous buffer and is page
  48. * aligned. When the previous buffer is page aligned, the
  49. * end of this buffer is aligned to the next page boundary.
  50. * Examples:
  51. * buf1 ][ buf2 ]| ...
  52. * buf1 ]|[ buf2 ]| ...
  53. */
  54. SAME_PAGE_ALIGNED,
  55. /**
  56. * @NEXT_PAGE_UNALIGNED: The end of this buffer is on
  57. * the page next to the end of the previous buffer and
  58. * is not page aligned. Examples:
  59. * buf1 ][ buf2 | buf2 ][ ...
  60. * buf1 ]|[ buf2 | buf2 ][ ...
  61. */
  62. NEXT_PAGE_UNALIGNED,
  63. /**
  64. * @NEXT_PAGE_ALIGNED: The end of this buffer is on
  65. * the page next to the end of the previous buffer and
  66. * is page aligned. Examples:
  67. * buf1 ][ buf2 | buf2 ]| ...
  68. * buf1 ]|[ buf2 | buf2 ]| ...
  69. */
  70. NEXT_PAGE_ALIGNED,
  71. /**
  72. * @NEXT_NEXT_UNALIGNED: The end of this buffer is on
  73. * the page that follows the page after the end of the
  74. * previous buffer and is not page aligned. Examples:
  75. * buf1 ][ buf2 | buf2 | buf2 ][ ...
  76. * buf1 ]|[ buf2 | buf2 | buf2 ][ ...
  77. */
  78. NEXT_NEXT_UNALIGNED,
  79. LOOP_END,
  80. };
  81. static void pr_err_size_seq(size_t *sizes, int *seq)
  82. {
  83. int i;
  84. pr_err("alloc sizes: ");
  85. for (i = 0; i < BUFFER_NUM; i++)
  86. pr_cont("[%zu]", sizes[i]);
  87. pr_cont("\n");
  88. pr_err("free seq: ");
  89. for (i = 0; i < BUFFER_NUM; i++)
  90. pr_cont("[%d]", seq[i]);
  91. pr_cont("\n");
  92. }
  93. static bool check_buffer_pages_allocated(struct binder_alloc *alloc,
  94. struct binder_buffer *buffer,
  95. size_t size)
  96. {
  97. void *page_addr, *end;
  98. int page_index;
  99. end = (void *)PAGE_ALIGN((uintptr_t)buffer->data + size);
  100. page_addr = buffer->data;
  101. for (; page_addr < end; page_addr += PAGE_SIZE) {
  102. page_index = (page_addr - alloc->buffer) / PAGE_SIZE;
  103. if (!alloc->pages[page_index].page_ptr ||
  104. !list_empty(&alloc->pages[page_index].lru)) {
  105. pr_err("expect alloc but is %s at page index %d\n",
  106. alloc->pages[page_index].page_ptr ?
  107. "lru" : "free", page_index);
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. static void binder_selftest_alloc_buf(struct binder_alloc *alloc,
  114. struct binder_buffer *buffers[],
  115. size_t *sizes, int *seq)
  116. {
  117. int i;
  118. for (i = 0; i < BUFFER_NUM; i++) {
  119. buffers[i] = binder_alloc_new_buf(alloc, sizes[i], 0, 0, 0);
  120. if (IS_ERR(buffers[i]) ||
  121. !check_buffer_pages_allocated(alloc, buffers[i],
  122. sizes[i])) {
  123. pr_err_size_seq(sizes, seq);
  124. binder_selftest_failures++;
  125. }
  126. }
  127. }
  128. static void binder_selftest_free_buf(struct binder_alloc *alloc,
  129. struct binder_buffer *buffers[],
  130. size_t *sizes, int *seq, size_t end)
  131. {
  132. int i;
  133. for (i = 0; i < BUFFER_NUM; i++)
  134. binder_alloc_free_buf(alloc, buffers[seq[i]]);
  135. for (i = 0; i < end / PAGE_SIZE; i++) {
  136. /**
  137. * Error message on a free page can be false positive
  138. * if binder shrinker ran during binder_alloc_free_buf
  139. * calls above.
  140. */
  141. if (list_empty(&alloc->pages[i].lru)) {
  142. pr_err_size_seq(sizes, seq);
  143. pr_err("expect lru but is %s at page index %d\n",
  144. alloc->pages[i].page_ptr ? "alloc" : "free", i);
  145. binder_selftest_failures++;
  146. }
  147. }
  148. }
  149. static void binder_selftest_free_page(struct binder_alloc *alloc)
  150. {
  151. int i;
  152. unsigned long count;
  153. while ((count = list_lru_count(&binder_alloc_lru))) {
  154. list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
  155. NULL, count);
  156. }
  157. for (i = 0; i < (alloc->buffer_size / PAGE_SIZE); i++) {
  158. if (alloc->pages[i].page_ptr) {
  159. pr_err("expect free but is %s at page index %d\n",
  160. list_empty(&alloc->pages[i].lru) ?
  161. "alloc" : "lru", i);
  162. binder_selftest_failures++;
  163. }
  164. }
  165. }
  166. static void binder_selftest_alloc_free(struct binder_alloc *alloc,
  167. size_t *sizes, int *seq, size_t end)
  168. {
  169. struct binder_buffer *buffers[BUFFER_NUM];
  170. binder_selftest_alloc_buf(alloc, buffers, sizes, seq);
  171. binder_selftest_free_buf(alloc, buffers, sizes, seq, end);
  172. /* Allocate from lru. */
  173. binder_selftest_alloc_buf(alloc, buffers, sizes, seq);
  174. if (list_lru_count(&binder_alloc_lru))
  175. pr_err("lru list should be empty but is not\n");
  176. binder_selftest_free_buf(alloc, buffers, sizes, seq, end);
  177. binder_selftest_free_page(alloc);
  178. }
  179. static bool is_dup(int *seq, int index, int val)
  180. {
  181. int i;
  182. for (i = 0; i < index; i++) {
  183. if (seq[i] == val)
  184. return true;
  185. }
  186. return false;
  187. }
  188. /* Generate BUFFER_NUM factorial free orders. */
  189. static void binder_selftest_free_seq(struct binder_alloc *alloc,
  190. size_t *sizes, int *seq,
  191. int index, size_t end)
  192. {
  193. int i;
  194. if (index == BUFFER_NUM) {
  195. binder_selftest_alloc_free(alloc, sizes, seq, end);
  196. return;
  197. }
  198. for (i = 0; i < BUFFER_NUM; i++) {
  199. if (is_dup(seq, index, i))
  200. continue;
  201. seq[index] = i;
  202. binder_selftest_free_seq(alloc, sizes, seq, index + 1, end);
  203. }
  204. }
  205. static void binder_selftest_alloc_size(struct binder_alloc *alloc,
  206. size_t *end_offset)
  207. {
  208. int i;
  209. int seq[BUFFER_NUM] = {0};
  210. size_t front_sizes[BUFFER_NUM];
  211. size_t back_sizes[BUFFER_NUM];
  212. size_t last_offset, offset = 0;
  213. for (i = 0; i < BUFFER_NUM; i++) {
  214. last_offset = offset;
  215. offset = end_offset[i];
  216. front_sizes[i] = offset - last_offset;
  217. back_sizes[BUFFER_NUM - i - 1] = front_sizes[i];
  218. }
  219. /*
  220. * Buffers share the first or last few pages.
  221. * Only BUFFER_NUM - 1 buffer sizes are adjustable since
  222. * we need one giant buffer before getting to the last page.
  223. */
  224. back_sizes[0] += alloc->buffer_size - end_offset[BUFFER_NUM - 1];
  225. binder_selftest_free_seq(alloc, front_sizes, seq, 0,
  226. end_offset[BUFFER_NUM - 1]);
  227. binder_selftest_free_seq(alloc, back_sizes, seq, 0, alloc->buffer_size);
  228. }
  229. static void binder_selftest_alloc_offset(struct binder_alloc *alloc,
  230. size_t *end_offset, int index)
  231. {
  232. int align;
  233. size_t end, prev;
  234. if (index == BUFFER_NUM) {
  235. binder_selftest_alloc_size(alloc, end_offset);
  236. return;
  237. }
  238. prev = index == 0 ? 0 : end_offset[index - 1];
  239. end = prev;
  240. BUILD_BUG_ON(BUFFER_MIN_SIZE * BUFFER_NUM >= PAGE_SIZE);
  241. for (align = SAME_PAGE_UNALIGNED; align < LOOP_END; align++) {
  242. if (align % 2)
  243. end = ALIGN(end, PAGE_SIZE);
  244. else
  245. end += BUFFER_MIN_SIZE;
  246. end_offset[index] = end;
  247. binder_selftest_alloc_offset(alloc, end_offset, index + 1);
  248. }
  249. }
  250. /**
  251. * binder_selftest_alloc() - Test alloc and free of buffer pages.
  252. * @alloc: Pointer to alloc struct.
  253. *
  254. * Allocate BUFFER_NUM buffers to cover all page alignment cases,
  255. * then free them in all orders possible. Check that pages are
  256. * correctly allocated, put onto lru when buffers are freed, and
  257. * are freed when binder_alloc_free_page is called.
  258. */
  259. void binder_selftest_alloc(struct binder_alloc *alloc)
  260. {
  261. size_t end_offset[BUFFER_NUM];
  262. if (!binder_selftest_run)
  263. return;
  264. mutex_lock(&binder_selftest_lock);
  265. if (!binder_selftest_run || !alloc->vma)
  266. goto done;
  267. pr_info("STARTED\n");
  268. binder_selftest_alloc_offset(alloc, end_offset, 0);
  269. binder_selftest_run = false;
  270. if (binder_selftest_failures > 0)
  271. pr_info("%d tests FAILED\n", binder_selftest_failures);
  272. else
  273. pr_info("PASSED\n");
  274. done:
  275. mutex_unlock(&binder_selftest_lock);
  276. }