Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

binder: Convert binder_alloc selftests to KUnit

Convert the existing binder_alloc_selftest tests into KUnit tests. These
tests allocate and free an exhaustive combination of buffers with
various sizes and alignments. This change allows them to be run without
blocking or otherwise interfering with other processes in binder.

This test is refactored into more meaningful cases in the subsequent
patch.

Signed-off-by: Tiffany Yang <ynaffit@google.com>
Acked-by: Carlos Llamas <cmllamas@google.com>
Link: https://lore.kernel.org/r/20250714185321.2417234-6-ynaffit@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Tiffany Yang and committed by
Greg Kroah-Hartman
f6544dcd 5e024582

+282 -366
-10
drivers/android/Kconfig
··· 37 37 created. Each binder device has its own context manager, and is 38 38 therefore logically separated from the other devices. 39 39 40 - config ANDROID_BINDER_IPC_SELFTEST 41 - bool "Android Binder IPC Driver Selftest" 42 - depends on ANDROID_BINDER_IPC 43 - help 44 - This feature allows binder selftest to run. 45 - 46 - Binder selftest checks the allocation and free of binder buffers 47 - exhaustively with combinations of various buffer sizes and 48 - alignments. 49 - 50 40 config ANDROID_BINDER_ALLOC_KUNIT_TEST 51 41 tristate "KUnit Tests for Android Binder Alloc" if !KUNIT_ALL_TESTS 52 42 depends on ANDROID_BINDER_IPC && KUNIT
-1
drivers/android/Makefile
··· 3 3 4 4 obj-$(CONFIG_ANDROID_BINDERFS) += binderfs.o 5 5 obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o 6 - obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o 7 6 obj-$(CONFIG_ANDROID_BINDER_ALLOC_KUNIT_TEST) += tests/
-5
drivers/android/binder.c
··· 5709 5709 struct binder_thread *thread; 5710 5710 void __user *ubuf = (void __user *)arg; 5711 5711 5712 - /*pr_info("binder_ioctl: %d:%d %x %lx\n", 5713 - proc->pid, current->pid, cmd, arg);*/ 5714 - 5715 - binder_selftest_alloc(&proc->alloc); 5716 - 5717 5712 trace_binder_ioctl(cmd, arg); 5718 5713 5719 5714 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
+3
drivers/android/binder_alloc.c
··· 697 697 out: 698 698 return buffer; 699 699 } 700 + EXPORT_SYMBOL_IF_KUNIT(binder_alloc_new_buf); 700 701 701 702 static unsigned long buffer_start_page(struct binder_buffer *buffer) 702 703 { ··· 876 875 binder_free_buf_locked(alloc, buffer); 877 876 mutex_unlock(&alloc->mutex); 878 877 } 878 + EXPORT_SYMBOL_IF_KUNIT(binder_alloc_free_buf); 879 879 880 880 /** 881 881 * binder_alloc_mmap_handler() - map virtual address space for proc ··· 1213 1211 err_mmget: 1214 1212 return LRU_SKIP; 1215 1213 } 1214 + EXPORT_SYMBOL_IF_KUNIT(binder_alloc_free_page); 1216 1215 1217 1216 static unsigned long 1218 1217 binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
-5
drivers/android/binder_alloc.h
··· 121 121 bool oneway_spam_detected; 122 122 }; 123 123 124 - #ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST 125 - void binder_selftest_alloc(struct binder_alloc *alloc); 126 - #else 127 - static inline void binder_selftest_alloc(struct binder_alloc *alloc) {} 128 - #endif 129 124 enum lru_status binder_alloc_free_page(struct list_head *item, 130 125 struct list_lru_one *lru, 131 126 void *cb_arg);
-345
drivers/android/binder_alloc_selftest.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* binder_alloc_selftest.c 3 - * 4 - * Android IPC Subsystem 5 - * 6 - * Copyright (C) 2017 Google, Inc. 7 - */ 8 - 9 - #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 - 11 - #include <linux/err.h> 12 - #include <linux/list_lru.h> 13 - #include <linux/mm_types.h> 14 - #include "binder_alloc.h" 15 - 16 - #define BUFFER_NUM 5 17 - #define BUFFER_MIN_SIZE (PAGE_SIZE / 8) 18 - 19 - static bool binder_selftest_run = true; 20 - static int binder_selftest_failures; 21 - static DEFINE_MUTEX(binder_selftest_lock); 22 - static struct list_lru binder_selftest_freelist; 23 - 24 - /** 25 - * enum buf_end_align_type - Page alignment of a buffer 26 - * end with regard to the end of the previous buffer. 27 - * 28 - * In the pictures below, buf2 refers to the buffer we 29 - * are aligning. buf1 refers to previous buffer by addr. 30 - * Symbol [ means the start of a buffer, ] means the end 31 - * of a buffer, and | means page boundaries. 32 - */ 33 - enum buf_end_align_type { 34 - /** 35 - * @SAME_PAGE_UNALIGNED: The end of this buffer is on 36 - * the same page as the end of the previous buffer and 37 - * is not page aligned. Examples: 38 - * buf1 ][ buf2 ][ ... 39 - * buf1 ]|[ buf2 ][ ... 40 - */ 41 - SAME_PAGE_UNALIGNED = 0, 42 - /** 43 - * @SAME_PAGE_ALIGNED: When the end of the previous buffer 44 - * is not page aligned, the end of this buffer is on the 45 - * same page as the end of the previous buffer and is page 46 - * aligned. When the previous buffer is page aligned, the 47 - * end of this buffer is aligned to the next page boundary. 48 - * Examples: 49 - * buf1 ][ buf2 ]| ... 50 - * buf1 ]|[ buf2 ]| ... 51 - */ 52 - SAME_PAGE_ALIGNED, 53 - /** 54 - * @NEXT_PAGE_UNALIGNED: The end of this buffer is on 55 - * the page next to the end of the previous buffer and 56 - * is not page aligned. Examples: 57 - * buf1 ][ buf2 | buf2 ][ ... 58 - * buf1 ]|[ buf2 | buf2 ][ ... 59 - */ 60 - NEXT_PAGE_UNALIGNED, 61 - /** 62 - * @NEXT_PAGE_ALIGNED: The end of this buffer is on 63 - * the page next to the end of the previous buffer and 64 - * is page aligned. Examples: 65 - * buf1 ][ buf2 | buf2 ]| ... 66 - * buf1 ]|[ buf2 | buf2 ]| ... 67 - */ 68 - NEXT_PAGE_ALIGNED, 69 - /** 70 - * @NEXT_NEXT_UNALIGNED: The end of this buffer is on 71 - * the page that follows the page after the end of the 72 - * previous buffer and is not page aligned. Examples: 73 - * buf1 ][ buf2 | buf2 | buf2 ][ ... 74 - * buf1 ]|[ buf2 | buf2 | buf2 ][ ... 75 - */ 76 - NEXT_NEXT_UNALIGNED, 77 - /** 78 - * @LOOP_END: The number of enum values in &buf_end_align_type. 79 - * It is used for controlling loop termination. 80 - */ 81 - LOOP_END, 82 - }; 83 - 84 - static void pr_err_size_seq(size_t *sizes, int *seq) 85 - { 86 - int i; 87 - 88 - pr_err("alloc sizes: "); 89 - for (i = 0; i < BUFFER_NUM; i++) 90 - pr_cont("[%zu]", sizes[i]); 91 - pr_cont("\n"); 92 - pr_err("free seq: "); 93 - for (i = 0; i < BUFFER_NUM; i++) 94 - pr_cont("[%d]", seq[i]); 95 - pr_cont("\n"); 96 - } 97 - 98 - static bool check_buffer_pages_allocated(struct binder_alloc *alloc, 99 - struct binder_buffer *buffer, 100 - size_t size) 101 - { 102 - unsigned long page_addr; 103 - unsigned long end; 104 - int page_index; 105 - 106 - end = PAGE_ALIGN(buffer->user_data + size); 107 - page_addr = buffer->user_data; 108 - for (; page_addr < end; page_addr += PAGE_SIZE) { 109 - page_index = (page_addr - alloc->vm_start) / PAGE_SIZE; 110 - if (!alloc->pages[page_index] || 111 - !list_empty(page_to_lru(alloc->pages[page_index]))) { 112 - pr_err("expect alloc but is %s at page index %d\n", 113 - alloc->pages[page_index] ? 114 - "lru" : "free", page_index); 115 - return false; 116 - } 117 - } 118 - return true; 119 - } 120 - 121 - static void binder_selftest_alloc_buf(struct binder_alloc *alloc, 122 - struct binder_buffer *buffers[], 123 - size_t *sizes, int *seq) 124 - { 125 - int i; 126 - 127 - for (i = 0; i < BUFFER_NUM; i++) { 128 - buffers[i] = binder_alloc_new_buf(alloc, sizes[i], 0, 0, 0); 129 - if (IS_ERR(buffers[i]) || 130 - !check_buffer_pages_allocated(alloc, buffers[i], 131 - sizes[i])) { 132 - pr_err_size_seq(sizes, seq); 133 - binder_selftest_failures++; 134 - } 135 - } 136 - } 137 - 138 - static void binder_selftest_free_buf(struct binder_alloc *alloc, 139 - struct binder_buffer *buffers[], 140 - size_t *sizes, int *seq, size_t end) 141 - { 142 - int i; 143 - 144 - for (i = 0; i < BUFFER_NUM; i++) 145 - binder_alloc_free_buf(alloc, buffers[seq[i]]); 146 - 147 - for (i = 0; i <= (end - 1) / PAGE_SIZE; i++) { 148 - if (list_empty(page_to_lru(alloc->pages[i]))) { 149 - pr_err_size_seq(sizes, seq); 150 - pr_err("expect lru but is %s at page index %d\n", 151 - alloc->pages[i] ? "alloc" : "free", i); 152 - binder_selftest_failures++; 153 - } 154 - } 155 - } 156 - 157 - static void binder_selftest_free_page(struct binder_alloc *alloc) 158 - { 159 - int i; 160 - unsigned long count; 161 - 162 - while ((count = list_lru_count(&binder_selftest_freelist))) { 163 - list_lru_walk(&binder_selftest_freelist, binder_alloc_free_page, 164 - NULL, count); 165 - } 166 - 167 - for (i = 0; i < (alloc->buffer_size / PAGE_SIZE); i++) { 168 - if (alloc->pages[i]) { 169 - pr_err("expect free but is %s at page index %d\n", 170 - list_empty(page_to_lru(alloc->pages[i])) ? 171 - "alloc" : "lru", i); 172 - binder_selftest_failures++; 173 - } 174 - } 175 - } 176 - 177 - static void binder_selftest_alloc_free(struct binder_alloc *alloc, 178 - size_t *sizes, int *seq, size_t end) 179 - { 180 - struct binder_buffer *buffers[BUFFER_NUM]; 181 - 182 - binder_selftest_alloc_buf(alloc, buffers, sizes, seq); 183 - binder_selftest_free_buf(alloc, buffers, sizes, seq, end); 184 - 185 - /* Allocate from lru. */ 186 - binder_selftest_alloc_buf(alloc, buffers, sizes, seq); 187 - if (list_lru_count(&binder_selftest_freelist)) 188 - pr_err("lru list should be empty but is not\n"); 189 - 190 - binder_selftest_free_buf(alloc, buffers, sizes, seq, end); 191 - binder_selftest_free_page(alloc); 192 - } 193 - 194 - static bool is_dup(int *seq, int index, int val) 195 - { 196 - int i; 197 - 198 - for (i = 0; i < index; i++) { 199 - if (seq[i] == val) 200 - return true; 201 - } 202 - return false; 203 - } 204 - 205 - /* Generate BUFFER_NUM factorial free orders. */ 206 - static void binder_selftest_free_seq(struct binder_alloc *alloc, 207 - size_t *sizes, int *seq, 208 - int index, size_t end) 209 - { 210 - int i; 211 - 212 - if (index == BUFFER_NUM) { 213 - binder_selftest_alloc_free(alloc, sizes, seq, end); 214 - return; 215 - } 216 - for (i = 0; i < BUFFER_NUM; i++) { 217 - if (is_dup(seq, index, i)) 218 - continue; 219 - seq[index] = i; 220 - binder_selftest_free_seq(alloc, sizes, seq, index + 1, end); 221 - } 222 - } 223 - 224 - static void binder_selftest_alloc_size(struct binder_alloc *alloc, 225 - size_t *end_offset) 226 - { 227 - int i; 228 - int seq[BUFFER_NUM] = {0}; 229 - size_t front_sizes[BUFFER_NUM]; 230 - size_t back_sizes[BUFFER_NUM]; 231 - size_t last_offset, offset = 0; 232 - 233 - for (i = 0; i < BUFFER_NUM; i++) { 234 - last_offset = offset; 235 - offset = end_offset[i]; 236 - front_sizes[i] = offset - last_offset; 237 - back_sizes[BUFFER_NUM - i - 1] = front_sizes[i]; 238 - } 239 - /* 240 - * Buffers share the first or last few pages. 241 - * Only BUFFER_NUM - 1 buffer sizes are adjustable since 242 - * we need one giant buffer before getting to the last page. 243 - */ 244 - back_sizes[0] += alloc->buffer_size - end_offset[BUFFER_NUM - 1]; 245 - binder_selftest_free_seq(alloc, front_sizes, seq, 0, 246 - end_offset[BUFFER_NUM - 1]); 247 - binder_selftest_free_seq(alloc, back_sizes, seq, 0, alloc->buffer_size); 248 - } 249 - 250 - static void binder_selftest_alloc_offset(struct binder_alloc *alloc, 251 - size_t *end_offset, int index) 252 - { 253 - int align; 254 - size_t end, prev; 255 - 256 - if (index == BUFFER_NUM) { 257 - binder_selftest_alloc_size(alloc, end_offset); 258 - return; 259 - } 260 - prev = index == 0 ? 0 : end_offset[index - 1]; 261 - end = prev; 262 - 263 - BUILD_BUG_ON(BUFFER_MIN_SIZE * BUFFER_NUM >= PAGE_SIZE); 264 - 265 - for (align = SAME_PAGE_UNALIGNED; align < LOOP_END; align++) { 266 - if (align % 2) 267 - end = ALIGN(end, PAGE_SIZE); 268 - else 269 - end += BUFFER_MIN_SIZE; 270 - end_offset[index] = end; 271 - binder_selftest_alloc_offset(alloc, end_offset, index + 1); 272 - } 273 - } 274 - 275 - int binder_selftest_alloc_get_page_count(struct binder_alloc *alloc) 276 - { 277 - struct page *page; 278 - int allocated = 0; 279 - int i; 280 - 281 - for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { 282 - page = alloc->pages[i]; 283 - if (page) 284 - allocated++; 285 - } 286 - return allocated; 287 - } 288 - 289 - /** 290 - * binder_selftest_alloc() - Test alloc and free of buffer pages. 291 - * @alloc: Pointer to alloc struct. 292 - * 293 - * Allocate BUFFER_NUM buffers to cover all page alignment cases, 294 - * then free them in all orders possible. Check that pages are 295 - * correctly allocated, put onto lru when buffers are freed, and 296 - * are freed when binder_alloc_free_page is called. 297 - */ 298 - void binder_selftest_alloc(struct binder_alloc *alloc) 299 - { 300 - struct list_lru *prev_freelist; 301 - size_t end_offset[BUFFER_NUM]; 302 - 303 - if (!binder_selftest_run) 304 - return; 305 - mutex_lock(&binder_selftest_lock); 306 - if (!binder_selftest_run || !alloc->mapped) 307 - goto done; 308 - 309 - prev_freelist = alloc->freelist; 310 - 311 - /* 312 - * It is not safe to modify this process's alloc->freelist if it has any 313 - * pages on a freelist. Since the test runs before any binder ioctls can 314 - * be dealt with, none of its pages should be allocated yet. 315 - */ 316 - if (binder_selftest_alloc_get_page_count(alloc)) { 317 - pr_err("process has existing alloc state\n"); 318 - goto cleanup; 319 - } 320 - 321 - if (list_lru_init(&binder_selftest_freelist)) { 322 - pr_err("failed to init test freelist\n"); 323 - goto cleanup; 324 - } 325 - 326 - alloc->freelist = &binder_selftest_freelist; 327 - 328 - pr_info("STARTED\n"); 329 - binder_selftest_alloc_offset(alloc, end_offset, 0); 330 - if (binder_selftest_failures > 0) 331 - pr_info("%d tests FAILED\n", binder_selftest_failures); 332 - else 333 - pr_info("PASSED\n"); 334 - 335 - if (list_lru_count(&binder_selftest_freelist)) 336 - pr_err("expect test freelist to be empty\n"); 337 - 338 - cleanup: 339 - /* Even if we didn't run the test, it's no longer thread-safe. */ 340 - binder_selftest_run = false; 341 - alloc->freelist = prev_freelist; 342 - list_lru_destroy(&binder_selftest_freelist); 343 - done: 344 - mutex_unlock(&binder_selftest_lock); 345 - }
+279
drivers/android/tests/binder_alloc_kunit.c
··· 21 21 22 22 #define BINDER_MMAP_SIZE SZ_128K 23 23 24 + #define BUFFER_NUM 5 25 + #define BUFFER_MIN_SIZE (PAGE_SIZE / 8) 26 + 27 + static int binder_alloc_test_failures; 28 + 29 + /** 30 + * enum buf_end_align_type - Page alignment of a buffer 31 + * end with regard to the end of the previous buffer. 32 + * 33 + * In the pictures below, buf2 refers to the buffer we 34 + * are aligning. buf1 refers to previous buffer by addr. 35 + * Symbol [ means the start of a buffer, ] means the end 36 + * of a buffer, and | means page boundaries. 37 + */ 38 + enum buf_end_align_type { 39 + /** 40 + * @SAME_PAGE_UNALIGNED: The end of this buffer is on 41 + * the same page as the end of the previous buffer and 42 + * is not page aligned. Examples: 43 + * buf1 ][ buf2 ][ ... 44 + * buf1 ]|[ buf2 ][ ... 45 + */ 46 + SAME_PAGE_UNALIGNED = 0, 47 + /** 48 + * @SAME_PAGE_ALIGNED: When the end of the previous buffer 49 + * is not page aligned, the end of this buffer is on the 50 + * same page as the end of the previous buffer and is page 51 + * aligned. When the previous buffer is page aligned, the 52 + * end of this buffer is aligned to the next page boundary. 53 + * Examples: 54 + * buf1 ][ buf2 ]| ... 55 + * buf1 ]|[ buf2 ]| ... 56 + */ 57 + SAME_PAGE_ALIGNED, 58 + /** 59 + * @NEXT_PAGE_UNALIGNED: The end of this buffer is on 60 + * the page next to the end of the previous buffer and 61 + * is not page aligned. Examples: 62 + * buf1 ][ buf2 | buf2 ][ ... 63 + * buf1 ]|[ buf2 | buf2 ][ ... 64 + */ 65 + NEXT_PAGE_UNALIGNED, 66 + /** 67 + * @NEXT_PAGE_ALIGNED: The end of this buffer is on 68 + * the page next to the end of the previous buffer and 69 + * is page aligned. Examples: 70 + * buf1 ][ buf2 | buf2 ]| ... 71 + * buf1 ]|[ buf2 | buf2 ]| ... 72 + */ 73 + NEXT_PAGE_ALIGNED, 74 + /** 75 + * @NEXT_NEXT_UNALIGNED: The end of this buffer is on 76 + * the page that follows the page after the end of the 77 + * previous buffer and is not page aligned. Examples: 78 + * buf1 ][ buf2 | buf2 | buf2 ][ ... 79 + * buf1 ]|[ buf2 | buf2 | buf2 ][ ... 80 + */ 81 + NEXT_NEXT_UNALIGNED, 82 + /** 83 + * @LOOP_END: The number of enum values in &buf_end_align_type. 84 + * It is used for controlling loop termination. 85 + */ 86 + LOOP_END, 87 + }; 88 + 89 + static void pr_err_size_seq(struct kunit *test, size_t *sizes, int *seq) 90 + { 91 + int i; 92 + 93 + kunit_err(test, "alloc sizes: "); 94 + for (i = 0; i < BUFFER_NUM; i++) 95 + pr_cont("[%zu]", sizes[i]); 96 + pr_cont("\n"); 97 + kunit_err(test, "free seq: "); 98 + for (i = 0; i < BUFFER_NUM; i++) 99 + pr_cont("[%d]", seq[i]); 100 + pr_cont("\n"); 101 + } 102 + 103 + static bool check_buffer_pages_allocated(struct kunit *test, 104 + struct binder_alloc *alloc, 105 + struct binder_buffer *buffer, 106 + size_t size) 107 + { 108 + unsigned long page_addr; 109 + unsigned long end; 110 + int page_index; 111 + 112 + end = PAGE_ALIGN(buffer->user_data + size); 113 + page_addr = buffer->user_data; 114 + for (; page_addr < end; page_addr += PAGE_SIZE) { 115 + page_index = (page_addr - alloc->vm_start) / PAGE_SIZE; 116 + if (!alloc->pages[page_index] || 117 + !list_empty(page_to_lru(alloc->pages[page_index]))) { 118 + kunit_err(test, "expect alloc but is %s at page index %d\n", 119 + alloc->pages[page_index] ? 120 + "lru" : "free", page_index); 121 + return false; 122 + } 123 + } 124 + return true; 125 + } 126 + 127 + static void binder_alloc_test_alloc_buf(struct kunit *test, 128 + struct binder_alloc *alloc, 129 + struct binder_buffer *buffers[], 130 + size_t *sizes, int *seq) 131 + { 132 + int i; 133 + 134 + for (i = 0; i < BUFFER_NUM; i++) { 135 + buffers[i] = binder_alloc_new_buf(alloc, sizes[i], 0, 0, 0); 136 + if (IS_ERR(buffers[i]) || 137 + !check_buffer_pages_allocated(test, alloc, buffers[i], sizes[i])) { 138 + pr_err_size_seq(test, sizes, seq); 139 + binder_alloc_test_failures++; 140 + } 141 + } 142 + } 143 + 144 + static void binder_alloc_test_free_buf(struct kunit *test, 145 + struct binder_alloc *alloc, 146 + struct binder_buffer *buffers[], 147 + size_t *sizes, int *seq, size_t end) 148 + { 149 + int i; 150 + 151 + for (i = 0; i < BUFFER_NUM; i++) 152 + binder_alloc_free_buf(alloc, buffers[seq[i]]); 153 + 154 + for (i = 0; i <= (end - 1) / PAGE_SIZE; i++) { 155 + if (list_empty(page_to_lru(alloc->pages[i]))) { 156 + pr_err_size_seq(test, sizes, seq); 157 + kunit_err(test, "expect lru but is %s at page index %d\n", 158 + alloc->pages[i] ? "alloc" : "free", i); 159 + binder_alloc_test_failures++; 160 + } 161 + } 162 + } 163 + 164 + static void binder_alloc_test_free_page(struct kunit *test, 165 + struct binder_alloc *alloc) 166 + { 167 + unsigned long count; 168 + int i; 169 + 170 + while ((count = list_lru_count(alloc->freelist))) { 171 + list_lru_walk(alloc->freelist, binder_alloc_free_page, 172 + NULL, count); 173 + } 174 + 175 + for (i = 0; i < (alloc->buffer_size / PAGE_SIZE); i++) { 176 + if (alloc->pages[i]) { 177 + kunit_err(test, "expect free but is %s at page index %d\n", 178 + list_empty(page_to_lru(alloc->pages[i])) ? 179 + "alloc" : "lru", i); 180 + binder_alloc_test_failures++; 181 + } 182 + } 183 + } 184 + 185 + static void binder_alloc_test_alloc_free(struct kunit *test, 186 + struct binder_alloc *alloc, 187 + size_t *sizes, int *seq, size_t end) 188 + { 189 + struct binder_buffer *buffers[BUFFER_NUM]; 190 + 191 + binder_alloc_test_alloc_buf(test, alloc, buffers, sizes, seq); 192 + binder_alloc_test_free_buf(test, alloc, buffers, sizes, seq, end); 193 + 194 + /* Allocate from lru. */ 195 + binder_alloc_test_alloc_buf(test, alloc, buffers, sizes, seq); 196 + if (list_lru_count(alloc->freelist)) 197 + kunit_err(test, "lru list should be empty but is not\n"); 198 + 199 + binder_alloc_test_free_buf(test, alloc, buffers, sizes, seq, end); 200 + binder_alloc_test_free_page(test, alloc); 201 + } 202 + 203 + static bool is_dup(int *seq, int index, int val) 204 + { 205 + int i; 206 + 207 + for (i = 0; i < index; i++) { 208 + if (seq[i] == val) 209 + return true; 210 + } 211 + return false; 212 + } 213 + 214 + /* Generate BUFFER_NUM factorial free orders. */ 215 + static void permute_frees(struct kunit *test, struct binder_alloc *alloc, 216 + size_t *sizes, int *seq, int index, size_t end) 217 + { 218 + int i; 219 + 220 + if (index == BUFFER_NUM) { 221 + binder_alloc_test_alloc_free(test, alloc, sizes, seq, end); 222 + return; 223 + } 224 + for (i = 0; i < BUFFER_NUM; i++) { 225 + if (is_dup(seq, index, i)) 226 + continue; 227 + seq[index] = i; 228 + permute_frees(test, alloc, sizes, seq, index + 1, end); 229 + } 230 + } 231 + 232 + static void gen_buf_sizes(struct kunit *test, struct binder_alloc *alloc, 233 + size_t *end_offset) 234 + { 235 + size_t last_offset, offset = 0; 236 + size_t front_sizes[BUFFER_NUM]; 237 + size_t back_sizes[BUFFER_NUM]; 238 + int seq[BUFFER_NUM] = {0}; 239 + int i; 240 + 241 + for (i = 0; i < BUFFER_NUM; i++) { 242 + last_offset = offset; 243 + offset = end_offset[i]; 244 + front_sizes[i] = offset - last_offset; 245 + back_sizes[BUFFER_NUM - i - 1] = front_sizes[i]; 246 + } 247 + /* 248 + * Buffers share the first or last few pages. 249 + * Only BUFFER_NUM - 1 buffer sizes are adjustable since 250 + * we need one giant buffer before getting to the last page. 251 + */ 252 + back_sizes[0] += alloc->buffer_size - end_offset[BUFFER_NUM - 1]; 253 + permute_frees(test, alloc, front_sizes, seq, 0, 254 + end_offset[BUFFER_NUM - 1]); 255 + permute_frees(test, alloc, back_sizes, seq, 0, alloc->buffer_size); 256 + } 257 + 258 + static void gen_buf_offsets(struct kunit *test, struct binder_alloc *alloc, 259 + size_t *end_offset, int index) 260 + { 261 + size_t end, prev; 262 + int align; 263 + 264 + if (index == BUFFER_NUM) { 265 + gen_buf_sizes(test, alloc, end_offset); 266 + return; 267 + } 268 + prev = index == 0 ? 0 : end_offset[index - 1]; 269 + end = prev; 270 + 271 + BUILD_BUG_ON(BUFFER_MIN_SIZE * BUFFER_NUM >= PAGE_SIZE); 272 + 273 + for (align = SAME_PAGE_UNALIGNED; align < LOOP_END; align++) { 274 + if (align % 2) 275 + end = ALIGN(end, PAGE_SIZE); 276 + else 277 + end += BUFFER_MIN_SIZE; 278 + end_offset[index] = end; 279 + gen_buf_offsets(test, alloc, end_offset, index + 1); 280 + } 281 + } 282 + 24 283 struct binder_alloc_test { 25 284 struct binder_alloc alloc; 26 285 struct list_lru binder_test_freelist; ··· 313 54 KUNIT_EXPECT_EQ(test, binder_alloc_buffer_size(alloc, buf), 314 55 BINDER_MMAP_SIZE); 315 56 KUNIT_EXPECT_TRUE(test, list_is_last(&buf->entry, &alloc->buffers)); 57 + } 58 + 59 + /** 60 + * binder_alloc_exhaustive_test() - Exhaustively test alloc and free of buffer pages. 61 + * @test: The test context object. 62 + * 63 + * Allocate BUFFER_NUM buffers to cover all page alignment cases, 64 + * then free them in all orders possible. Check that pages are 65 + * correctly allocated, put onto lru when buffers are freed, and 66 + * are freed when binder_alloc_free_page() is called. 67 + */ 68 + static void binder_alloc_exhaustive_test(struct kunit *test) 69 + { 70 + struct binder_alloc_test *priv = test->priv; 71 + size_t end_offset[BUFFER_NUM]; 72 + 73 + gen_buf_offsets(test, &priv->alloc, end_offset, 0); 74 + 75 + KUNIT_EXPECT_EQ(test, binder_alloc_test_failures, 0); 316 76 } 317 77 318 78 /* ===== End test cases ===== */ ··· 427 149 static struct kunit_case binder_alloc_test_cases[] = { 428 150 KUNIT_CASE(binder_alloc_test_init_freelist), 429 151 KUNIT_CASE(binder_alloc_test_mmap), 152 + KUNIT_CASE(binder_alloc_exhaustive_test), 430 153 {} 431 154 }; 432 155