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