Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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