Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Google, Inc. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #ifndef _LINUX_BINDER_ALLOC_H |
| 16 | #define _LINUX_BINDER_ALLOC_H |
| 17 | |
| 18 | #include <linux/rbtree.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/rtmutex.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/slab.h> |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 24 | #include <linux/list_lru.h> |
wya | 893c9f0 | 2019-07-08 13:16:21 +0800 | [diff] [blame] | 25 | |
| 26 | #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT |
| 27 | #define BINDER_IPC_32BIT 1 |
| 28 | #endif |
| 29 | |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 30 | #include <uapi/linux/android/binder.h> |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 31 | |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 32 | extern struct list_lru binder_alloc_lru; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 33 | struct binder_transaction; |
| 34 | |
| 35 | /** |
| 36 | * struct binder_buffer - buffer used for binder transactions |
| 37 | * @entry: entry alloc->buffers |
| 38 | * @rb_node: node for allocated_buffers/free_buffers rb trees |
Todd Kjos | e60bc94 | 2018-12-05 15:19:25 -0800 | [diff] [blame] | 39 | * @free: %true if buffer is free |
Todd Kjos | 97c54a1 | 2020-11-20 15:37:43 -0800 | [diff] [blame] | 40 | * @clear_on_free: %true if buffer must be zeroed after use |
Todd Kjos | e60bc94 | 2018-12-05 15:19:25 -0800 | [diff] [blame] | 41 | * @allow_user_free: %true if user is allowed to free buffer |
| 42 | * @async_transaction: %true if buffer is in use for an async txn |
| 43 | * @debug_id: unique ID for debugging |
| 44 | * @transaction: pointer to associated struct binder_transaction |
| 45 | * @target_node: struct binder_node associated with this buffer |
| 46 | * @data_size: size of @transaction data |
| 47 | * @offsets_size: size of array of offsets |
| 48 | * @extra_buffers_size: size of space for other objects (like sg lists) |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 49 | * @user_data: user pointer to base of buffer space |
Martijn Coenen | 6e126c4 | 2020-08-21 14:25:44 +0200 | [diff] [blame] | 50 | * @pid: pid to attribute the buffer to (caller) |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 51 | * |
| 52 | * Bookkeeping structure for binder transaction buffers |
| 53 | */ |
| 54 | struct binder_buffer { |
| 55 | struct list_head entry; /* free and allocated entries by address */ |
| 56 | struct rb_node rb_node; /* free entry by size or allocated entry */ |
| 57 | /* by address */ |
| 58 | unsigned free:1; |
Todd Kjos | 97c54a1 | 2020-11-20 15:37:43 -0800 | [diff] [blame] | 59 | unsigned clear_on_free:1; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 60 | unsigned allow_user_free:1; |
| 61 | unsigned async_transaction:1; |
Todd Kjos | 97c54a1 | 2020-11-20 15:37:43 -0800 | [diff] [blame] | 62 | unsigned debug_id:28; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 63 | |
| 64 | struct binder_transaction *transaction; |
| 65 | |
| 66 | struct binder_node *target_node; |
| 67 | size_t data_size; |
| 68 | size_t offsets_size; |
| 69 | size_t extra_buffers_size; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 70 | void __user *user_data; |
Martijn Coenen | 6e126c4 | 2020-08-21 14:25:44 +0200 | [diff] [blame] | 71 | int pid; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 75 | * struct binder_lru_page - page object used for binder shrinker |
| 76 | * @page_ptr: pointer to physical page in mmap'd space |
| 77 | * @lru: entry in binder_alloc_lru |
| 78 | * @alloc: binder_alloc for a proc |
| 79 | */ |
| 80 | struct binder_lru_page { |
| 81 | struct list_head lru; |
| 82 | struct page *page_ptr; |
| 83 | struct binder_alloc *alloc; |
| 84 | }; |
| 85 | |
| 86 | /** |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 87 | * struct binder_alloc - per-binder proc state for binder allocator |
| 88 | * @vma: vm_area_struct passed to mmap_handler |
| 89 | * (invarient after mmap) |
| 90 | * @tsk: tid for task that called init for this proc |
| 91 | * (invariant after init) |
| 92 | * @vma_vm_mm: copy of vma->vm_mm (invarient after mmap) |
| 93 | * @buffer: base of per-proc address space mapped via mmap |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 94 | * @buffers: list of all buffers for this proc |
| 95 | * @free_buffers: rb tree of buffers available for allocation |
| 96 | * sorted by size |
| 97 | * @allocated_buffers: rb tree of allocated buffers sorted by address |
| 98 | * @free_async_space: VA space available for async buffers. This is |
| 99 | * initialized at mmap time to 1/2 the full VA space |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 100 | * @pages: array of binder_lru_page |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 101 | * @buffer_size: size of address space specified via mmap |
| 102 | * @pid: pid for associated binder_proc (invariant after init) |
Martijn Coenen | c05ec29 | 2017-10-24 16:37:39 +0200 | [diff] [blame] | 103 | * @pages_high: high watermark of offset in @pages |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 104 | * |
| 105 | * Bookkeeping structure for per-proc address space management for binder |
| 106 | * buffers. It is normally initialized during binder_init() and binder_mmap() |
| 107 | * calls. The address space is used for both user-visible buffers and for |
| 108 | * struct binder_buffer objects used to track the user buffers |
| 109 | */ |
| 110 | struct binder_alloc { |
| 111 | struct mutex mutex; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 112 | struct vm_area_struct *vma; |
| 113 | struct mm_struct *vma_vm_mm; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 114 | void __user *buffer; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 115 | struct list_head buffers; |
| 116 | struct rb_root free_buffers; |
| 117 | struct rb_root allocated_buffers; |
| 118 | size_t free_async_space; |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 119 | struct binder_lru_page *pages; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 120 | size_t buffer_size; |
| 121 | uint32_t buffer_free; |
| 122 | int pid; |
Martijn Coenen | c05ec29 | 2017-10-24 16:37:39 +0200 | [diff] [blame] | 123 | size_t pages_high; |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Sherry Yang | 435416b | 2017-06-22 14:37:45 -0700 | [diff] [blame] | 126 | #ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST |
| 127 | void binder_selftest_alloc(struct binder_alloc *alloc); |
| 128 | #else |
| 129 | static inline void binder_selftest_alloc(struct binder_alloc *alloc) {} |
| 130 | #endif |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 131 | enum lru_status binder_alloc_free_page(struct list_head *item, |
| 132 | struct list_lru_one *lru, |
| 133 | spinlock_t *lock, void *cb_arg); |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 134 | extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc, |
| 135 | size_t data_size, |
| 136 | size_t offsets_size, |
| 137 | size_t extra_buffers_size, |
Martijn Coenen | 6e126c4 | 2020-08-21 14:25:44 +0200 | [diff] [blame] | 138 | int is_async, |
| 139 | int pid); |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 140 | extern void binder_alloc_init(struct binder_alloc *alloc); |
Tetsuo Handa | f8cb822 | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 141 | extern int binder_alloc_shrinker_init(void); |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 142 | extern void binder_alloc_vma_close(struct binder_alloc *alloc); |
| 143 | extern struct binder_buffer * |
| 144 | binder_alloc_prepare_to_free(struct binder_alloc *alloc, |
| 145 | uintptr_t user_ptr); |
| 146 | extern void binder_alloc_free_buf(struct binder_alloc *alloc, |
| 147 | struct binder_buffer *buffer); |
| 148 | extern int binder_alloc_mmap_handler(struct binder_alloc *alloc, |
| 149 | struct vm_area_struct *vma); |
| 150 | extern void binder_alloc_deferred_release(struct binder_alloc *alloc); |
| 151 | extern int binder_alloc_get_allocated_count(struct binder_alloc *alloc); |
| 152 | extern void binder_alloc_print_allocated(struct seq_file *m, |
| 153 | struct binder_alloc *alloc); |
Sherry Yang | 9100442 | 2017-08-22 17:26:57 -0700 | [diff] [blame] | 154 | void binder_alloc_print_pages(struct seq_file *m, |
| 155 | struct binder_alloc *alloc); |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * binder_alloc_get_free_async_space() - get free space available for async |
| 159 | * @alloc: binder_alloc for this proc |
| 160 | * |
| 161 | * Return: the bytes remaining in the address-space for async transactions |
| 162 | */ |
| 163 | static inline size_t |
| 164 | binder_alloc_get_free_async_space(struct binder_alloc *alloc) |
| 165 | { |
| 166 | size_t free_async_space; |
| 167 | |
| 168 | mutex_lock(&alloc->mutex); |
| 169 | free_async_space = alloc->free_async_space; |
| 170 | mutex_unlock(&alloc->mutex); |
| 171 | return free_async_space; |
| 172 | } |
| 173 | |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 174 | unsigned long |
| 175 | binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc, |
| 176 | struct binder_buffer *buffer, |
| 177 | binder_size_t buffer_offset, |
| 178 | const void __user *from, |
| 179 | size_t bytes); |
| 180 | |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 181 | void binder_alloc_copy_to_buffer(struct binder_alloc *alloc, |
| 182 | struct binder_buffer *buffer, |
| 183 | binder_size_t buffer_offset, |
| 184 | void *src, |
| 185 | size_t bytes); |
| 186 | |
| 187 | void binder_alloc_copy_from_buffer(struct binder_alloc *alloc, |
| 188 | void *dest, |
| 189 | struct binder_buffer *buffer, |
| 190 | binder_size_t buffer_offset, |
| 191 | size_t bytes); |
| 192 | |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 193 | #endif /* _LINUX_BINDER_ALLOC_H */ |
| 194 | |