binder_alloc_selftest.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 __user *page_addr;
  98. void __user *end;
  99. int page_index;
  100. end = (void __user *)PAGE_ALIGN((uintptr_t)buffer->user_data + size);
  101. page_addr = buffer->user_data;
  102. for (; page_addr < end; page_addr += PAGE_SIZE) {
  103. page_index = (page_addr - alloc->buffer) / PAGE_SIZE;
  104. if (!alloc->pages[page_index].page_ptr ||
  105. !list_empty(&alloc->pages[page_index].lru)) {
  106. pr_err("expect alloc but is %s at page index %d\n",
  107. alloc->pages[page_index].page_ptr ?
  108. "lru" : "free", page_index);
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. static void binder_selftest_alloc_buf(struct binder_alloc *alloc,
  115. struct binder_buffer *buffers[],
  116. size_t *sizes, int *seq)
  117. {
  118. int i;
  119. for (i = 0; i < BUFFER_NUM; i++) {
  120. buffers[i] = binder_alloc_new_buf(alloc, sizes[i], 0, 0, 0);
  121. if (IS_ERR(buffers[i]) ||
  122. !check_buffer_pages_allocated(alloc, buffers[i],
  123. sizes[i])) {
  124. pr_err_size_seq(sizes, seq);
  125. binder_selftest_failures++;
  126. }
  127. }
  128. }
  129. static void binder_selftest_free_buf(struct binder_alloc *alloc,
  130. struct binder_buffer *buffers[],
  131. size_t *sizes, int *seq, size_t end)
  132. {
  133. int i;
  134. for (i = 0; i < BUFFER_NUM; i++)
  135. binder_alloc_free_buf(alloc, buffers[seq[i]]);
  136. for (i = 0; i < end / PAGE_SIZE; i++) {
  137. /**
  138. * Error message on a free page can be false positive
  139. * if binder shrinker ran during binder_alloc_free_buf
  140. * calls above.
  141. */
  142. if (list_empty(&alloc->pages[i].lru)) {
  143. pr_err_size_seq(sizes, seq);
  144. pr_err("expect lru but is %s at page index %d\n",
  145. alloc->pages[i].page_ptr ? "alloc" : "free", i);
  146. binder_selftest_failures++;
  147. }
  148. }
  149. }
  150. static void binder_selftest_free_page(struct binder_alloc *alloc)
  151. {
  152. int i;
  153. unsigned long count;
  154. while ((count = list_lru_count(&binder_alloc_lru))) {
  155. list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
  156. NULL, count);
  157. }
  158. for (i = 0; i < (alloc->buffer_size / PAGE_SIZE); i++) {
  159. if (alloc->pages[i].page_ptr) {
  160. pr_err("expect free but is %s at page index %d\n",
  161. list_empty(&alloc->pages[i].lru) ?
  162. "alloc" : "lru", i);
  163. binder_selftest_failures++;
  164. }
  165. }
  166. }
  167. static void binder_selftest_alloc_free(struct binder_alloc *alloc,
  168. size_t *sizes, int *seq, size_t end)
  169. {
  170. struct binder_buffer *buffers[BUFFER_NUM];
  171. binder_selftest_alloc_buf(alloc, buffers, sizes, seq);
  172. binder_selftest_free_buf(alloc, buffers, sizes, seq, end);
  173. /* Allocate from lru. */
  174. binder_selftest_alloc_buf(alloc, buffers, sizes, seq);
  175. if (list_lru_count(&binder_alloc_lru))
  176. pr_err("lru list should be empty but is not\n");
  177. binder_selftest_free_buf(alloc, buffers, sizes, seq, end);
  178. binder_selftest_free_page(alloc);
  179. }
  180. static bool is_dup(int *seq, int index, int val)
  181. {
  182. int i;
  183. for (i = 0; i < index; i++) {
  184. if (seq[i] == val)
  185. return true;
  186. }
  187. return false;
  188. }
  189. /* Generate BUFFER_NUM factorial free orders. */
  190. static void binder_selftest_free_seq(struct binder_alloc *alloc,
  191. size_t *sizes, int *seq,
  192. int index, size_t end)
  193. {
  194. int i;
  195. if (index == BUFFER_NUM) {
  196. binder_selftest_alloc_free(alloc, sizes, seq, end);
  197. return;
  198. }
  199. for (i = 0; i < BUFFER_NUM; i++) {
  200. if (is_dup(seq, index, i))
  201. continue;
  202. seq[index] = i;
  203. binder_selftest_free_seq(alloc, sizes, seq, index + 1, end);
  204. }
  205. }
  206. static void binder_selftest_alloc_size(struct binder_alloc *alloc,
  207. size_t *end_offset)
  208. {
  209. int i;
  210. int seq[BUFFER_NUM] = {0};
  211. size_t front_sizes[BUFFER_NUM];
  212. size_t back_sizes[BUFFER_NUM];
  213. size_t last_offset, offset = 0;
  214. for (i = 0; i < BUFFER_NUM; i++) {
  215. last_offset = offset;
  216. offset = end_offset[i];
  217. front_sizes[i] = offset - last_offset;
  218. back_sizes[BUFFER_NUM - i - 1] = front_sizes[i];
  219. }
  220. /*
  221. * Buffers share the first or last few pages.
  222. * Only BUFFER_NUM - 1 buffer sizes are adjustable since
  223. * we need one giant buffer before getting to the last page.
  224. */
  225. back_sizes[0] += alloc->buffer_size - end_offset[BUFFER_NUM - 1];
  226. binder_selftest_free_seq(alloc, front_sizes, seq, 0,
  227. end_offset[BUFFER_NUM - 1]);
  228. binder_selftest_free_seq(alloc, back_sizes, seq, 0, alloc->buffer_size);
  229. }
  230. static void binder_selftest_alloc_offset(struct binder_alloc *alloc,
  231. size_t *end_offset, int index)
  232. {
  233. int align;
  234. size_t end, prev;
  235. if (index == BUFFER_NUM) {
  236. binder_selftest_alloc_size(alloc, end_offset);
  237. return;
  238. }
  239. prev = index == 0 ? 0 : end_offset[index - 1];
  240. end = prev;
  241. BUILD_BUG_ON(BUFFER_MIN_SIZE * BUFFER_NUM >= PAGE_SIZE);
  242. for (align = SAME_PAGE_UNALIGNED; align < LOOP_END; align++) {
  243. if (align % 2)
  244. end = ALIGN(end, PAGE_SIZE);
  245. else
  246. end += BUFFER_MIN_SIZE;
  247. end_offset[index] = end;
  248. binder_selftest_alloc_offset(alloc, end_offset, index + 1);
  249. }
  250. }
  251. /**
  252. * binder_selftest_alloc() - Test alloc and free of buffer pages.
  253. * @alloc: Pointer to alloc struct.
  254. *
  255. * Allocate BUFFER_NUM buffers to cover all page alignment cases,
  256. * then free them in all orders possible. Check that pages are
  257. * correctly allocated, put onto lru when buffers are freed, and
  258. * are freed when binder_alloc_free_page is called.
  259. */
  260. void binder_selftest_alloc(struct binder_alloc *alloc)
  261. {
  262. size_t end_offset[BUFFER_NUM];
  263. if (!binder_selftest_run)
  264. return;
  265. mutex_lock(&binder_selftest_lock);
  266. if (!binder_selftest_run || !alloc->vma)
  267. goto done;
  268. pr_info("STARTED\n");
  269. binder_selftest_alloc_offset(alloc, end_offset, 0);
  270. binder_selftest_run = false;
  271. if (binder_selftest_failures > 0)
  272. pr_info("%d tests FAILED\n", binder_selftest_failures);
  273. else
  274. pr_info("PASSED\n");
  275. done:
  276. mutex_unlock(&binder_selftest_lock);
  277. }