binder_alloc_selftest.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 + size);
  100. for (page_addr = buffer; page_addr < end; page_addr += PAGE_SIZE) {
  101. page_index = (page_addr - alloc->buffer) / PAGE_SIZE;
  102. if (!alloc->pages[page_index]) {
  103. pr_err("incorrect alloc state at page index %d\n",
  104. page_index);
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. static void binder_selftest_alloc_buf(struct binder_alloc *alloc,
  111. struct binder_buffer *buffers[],
  112. size_t *sizes, int *seq)
  113. {
  114. int i;
  115. for (i = 0; i < BUFFER_NUM; i++) {
  116. buffers[i] = binder_alloc_new_buf(alloc, sizes[i], 0, 0, 0);
  117. if (IS_ERR(buffers[i]) ||
  118. !check_buffer_pages_allocated(alloc, buffers[i],
  119. sizes[i])) {
  120. pr_err_size_seq(sizes, seq);
  121. binder_selftest_failures++;
  122. }
  123. }
  124. }
  125. static void binder_selftest_free_buf(struct binder_alloc *alloc,
  126. struct binder_buffer *buffers[],
  127. size_t *sizes, int *seq)
  128. {
  129. int i;
  130. for (i = 0; i < BUFFER_NUM; i++)
  131. binder_alloc_free_buf(alloc, buffers[seq[i]]);
  132. for (i = 0; i < (alloc->buffer_size / PAGE_SIZE); i++) {
  133. if ((!alloc->pages[i]) == (i == 0)) {
  134. pr_err("incorrect free state at page index %d\n", i);
  135. binder_selftest_failures++;
  136. }
  137. }
  138. }
  139. static void binder_selftest_alloc_free(struct binder_alloc *alloc,
  140. size_t *sizes, int *seq)
  141. {
  142. struct binder_buffer *buffers[BUFFER_NUM];
  143. binder_selftest_alloc_buf(alloc, buffers, sizes, seq);
  144. binder_selftest_free_buf(alloc, buffers, sizes, seq);
  145. }
  146. static bool is_dup(int *seq, int index, int val)
  147. {
  148. int i;
  149. for (i = 0; i < index; i++) {
  150. if (seq[i] == val)
  151. return true;
  152. }
  153. return false;
  154. }
  155. /* Generate BUFFER_NUM factorial free orders. */
  156. static void binder_selftest_free_seq(struct binder_alloc *alloc,
  157. size_t *sizes, int *seq, int index)
  158. {
  159. int i;
  160. if (index == BUFFER_NUM) {
  161. binder_selftest_alloc_free(alloc, sizes, seq);
  162. return;
  163. }
  164. for (i = 0; i < BUFFER_NUM; i++) {
  165. if (is_dup(seq, index, i))
  166. continue;
  167. seq[index] = i;
  168. binder_selftest_free_seq(alloc, sizes, seq, index + 1);
  169. }
  170. }
  171. static void binder_selftest_alloc_size(struct binder_alloc *alloc,
  172. size_t *end_offset)
  173. {
  174. int i;
  175. int seq[BUFFER_NUM] = {0};
  176. size_t front_sizes[BUFFER_NUM];
  177. size_t back_sizes[BUFFER_NUM];
  178. size_t last_offset, offset = 0;
  179. for (i = 0; i < BUFFER_NUM; i++) {
  180. last_offset = offset;
  181. offset = end_offset[i];
  182. front_sizes[i] = offset - last_offset;
  183. back_sizes[BUFFER_NUM - i - 1] = front_sizes[i];
  184. }
  185. /*
  186. * Buffers share the first or last few pages.
  187. * Only BUFFER_NUM - 1 buffer sizes are adjustable since
  188. * we need one giant buffer before getting to the last page.
  189. */
  190. back_sizes[0] += alloc->buffer_size - end_offset[BUFFER_NUM - 1]
  191. - sizeof(struct binder_buffer) * BUFFER_NUM;
  192. binder_selftest_free_seq(alloc, front_sizes, seq, 0);
  193. binder_selftest_free_seq(alloc, back_sizes, seq, 0);
  194. }
  195. static void binder_selftest_alloc_offset(struct binder_alloc *alloc,
  196. size_t *end_offset, int index)
  197. {
  198. int align;
  199. size_t end, prev;
  200. if (index == BUFFER_NUM) {
  201. binder_selftest_alloc_size(alloc, end_offset);
  202. return;
  203. }
  204. prev = index == 0 ? 0 : end_offset[index - 1];
  205. end = prev;
  206. BUILD_BUG_ON((BUFFER_MIN_SIZE + sizeof(struct binder_buffer))
  207. * BUFFER_NUM >= PAGE_SIZE);
  208. for (align = SAME_PAGE_UNALIGNED; align < LOOP_END; align++) {
  209. if (align % 2)
  210. end = ALIGN(end, PAGE_SIZE);
  211. else
  212. end += BUFFER_MIN_SIZE;
  213. end_offset[index] = end;
  214. binder_selftest_alloc_offset(alloc, end_offset, index + 1);
  215. }
  216. }
  217. /**
  218. * binder_selftest_alloc() - Test alloc and free of buffer pages.
  219. * @alloc: Pointer to alloc struct.
  220. *
  221. * Allocate BUFFER_NUM buffers to cover all page alignment cases,
  222. * then free them in all orders possible. Check that pages are
  223. * allocated after buffer alloc and freed after freeing buffer.
  224. */
  225. void binder_selftest_alloc(struct binder_alloc *alloc)
  226. {
  227. size_t end_offset[BUFFER_NUM];
  228. if (!binder_selftest_run)
  229. return;
  230. mutex_lock(&binder_selftest_lock);
  231. if (!binder_selftest_run || !alloc->vma)
  232. goto done;
  233. pr_info("STARTED\n");
  234. binder_selftest_alloc_offset(alloc, end_offset, 0);
  235. binder_selftest_run = false;
  236. if (binder_selftest_failures > 0)
  237. pr_info("%d tests FAILED\n", binder_selftest_failures);
  238. else
  239. pr_info("PASSED\n");
  240. done:
  241. mutex_unlock(&binder_selftest_lock);
  242. }