| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1 | /* binder.c | 
|  | 2 | * | 
|  | 3 | * Android IPC Subsystem | 
|  | 4 | * | 
|  | 5 | * Copyright (C) 2007-2008 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 |  | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 18 | /* | 
|  | 19 | * Locking overview | 
|  | 20 | * | 
|  | 21 | * There are 3 main spinlocks which must be acquired in the | 
|  | 22 | * order shown: | 
|  | 23 | * | 
|  | 24 | * 1) proc->outer_lock : protects binder_ref | 
|  | 25 | *    binder_proc_lock() and binder_proc_unlock() are | 
|  | 26 | *    used to acq/rel. | 
|  | 27 | * 2) node->lock : protects most fields of binder_node. | 
|  | 28 | *    binder_node_lock() and binder_node_unlock() are | 
|  | 29 | *    used to acq/rel | 
|  | 30 | * 3) proc->inner_lock : protects the thread and node lists | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 31 | *    (proc->threads, proc->waiting_threads, proc->nodes) | 
|  | 32 | *    and all todo lists associated with the binder_proc | 
|  | 33 | *    (proc->todo, thread->todo, proc->delivered_death and | 
|  | 34 | *    node->async_todo), as well as thread->transaction_stack | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 35 | *    binder_inner_proc_lock() and binder_inner_proc_unlock() | 
|  | 36 | *    are used to acq/rel | 
|  | 37 | * | 
|  | 38 | * Any lock under procA must never be nested under any lock at the same | 
|  | 39 | * level or below on procB. | 
|  | 40 | * | 
|  | 41 | * Functions that require a lock held on entry indicate which lock | 
|  | 42 | * in the suffix of the function name: | 
|  | 43 | * | 
|  | 44 | * foo_olocked() : requires node->outer_lock | 
|  | 45 | * foo_nlocked() : requires node->lock | 
|  | 46 | * foo_ilocked() : requires proc->inner_lock | 
|  | 47 | * foo_oilocked(): requires proc->outer_lock and proc->inner_lock | 
|  | 48 | * foo_nilocked(): requires node->lock and proc->inner_lock | 
|  | 49 | * ... | 
|  | 50 | */ | 
|  | 51 |  | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 52 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
|  | 53 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 54 | #include <asm/cacheflush.h> | 
|  | 55 | #include <linux/fdtable.h> | 
|  | 56 | #include <linux/file.h> | 
| Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 57 | #include <linux/freezer.h> | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 58 | #include <linux/fs.h> | 
|  | 59 | #include <linux/list.h> | 
|  | 60 | #include <linux/miscdevice.h> | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 61 | #include <linux/module.h> | 
|  | 62 | #include <linux/mutex.h> | 
|  | 63 | #include <linux/nsproxy.h> | 
|  | 64 | #include <linux/poll.h> | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 65 | #include <linux/debugfs.h> | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 66 | #include <linux/rbtree.h> | 
|  | 67 | #include <linux/sched.h> | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 68 | #include <linux/seq_file.h> | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 69 | #include <linux/uaccess.h> | 
| Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 70 | #include <linux/pid_namespace.h> | 
| Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 71 | #include <linux/security.h> | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 72 | #include <linux/spinlock.h> | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 73 |  | 
| Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT | 
|  | 75 | #define BINDER_IPC_32BIT 1 | 
|  | 76 | #endif | 
|  | 77 |  | 
|  | 78 | #include <uapi/linux/android/binder.h> | 
| Todd Kjos | b934102 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 79 | #include "binder_alloc.h" | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 80 | #include "binder_trace.h" | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 81 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 82 | static HLIST_HEAD(binder_deferred_list); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 83 | static DEFINE_MUTEX(binder_deferred_lock); | 
|  | 84 |  | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 85 | static HLIST_HEAD(binder_devices); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 86 | static HLIST_HEAD(binder_procs); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 87 | static DEFINE_MUTEX(binder_procs_lock); | 
|  | 88 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 89 | static HLIST_HEAD(binder_dead_nodes); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 90 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 91 |  | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 92 | static struct dentry *binder_debugfs_dir_entry_root; | 
|  | 93 | static struct dentry *binder_debugfs_dir_entry_proc; | 
| Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 94 | static atomic_t binder_last_id; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 95 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 96 | #define BINDER_DEBUG_ENTRY(name) \ | 
|  | 97 | static int binder_##name##_open(struct inode *inode, struct file *file) \ | 
|  | 98 | { \ | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 99 | return single_open(file, binder_##name##_show, inode->i_private); \ | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 100 | } \ | 
|  | 101 | \ | 
|  | 102 | static const struct file_operations binder_##name##_fops = { \ | 
|  | 103 | .owner = THIS_MODULE, \ | 
|  | 104 | .open = binder_##name##_open, \ | 
|  | 105 | .read = seq_read, \ | 
|  | 106 | .llseek = seq_lseek, \ | 
|  | 107 | .release = single_release, \ | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | static int binder_proc_show(struct seq_file *m, void *unused); | 
|  | 111 | BINDER_DEBUG_ENTRY(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 112 |  | 
|  | 113 | /* This is only defined in include/asm-arm/sizes.h */ | 
|  | 114 | #ifndef SZ_1K | 
|  | 115 | #define SZ_1K                               0x400 | 
|  | 116 | #endif | 
|  | 117 |  | 
|  | 118 | #ifndef SZ_4M | 
|  | 119 | #define SZ_4M                               0x400000 | 
|  | 120 | #endif | 
|  | 121 |  | 
|  | 122 | #define FORBIDDEN_MMAP_FLAGS                (VM_WRITE) | 
|  | 123 |  | 
|  | 124 | #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64) | 
|  | 125 |  | 
|  | 126 | enum { | 
|  | 127 | BINDER_DEBUG_USER_ERROR             = 1U << 0, | 
|  | 128 | BINDER_DEBUG_FAILED_TRANSACTION     = 1U << 1, | 
|  | 129 | BINDER_DEBUG_DEAD_TRANSACTION       = 1U << 2, | 
|  | 130 | BINDER_DEBUG_OPEN_CLOSE             = 1U << 3, | 
|  | 131 | BINDER_DEBUG_DEAD_BINDER            = 1U << 4, | 
|  | 132 | BINDER_DEBUG_DEATH_NOTIFICATION     = 1U << 5, | 
|  | 133 | BINDER_DEBUG_READ_WRITE             = 1U << 6, | 
|  | 134 | BINDER_DEBUG_USER_REFS              = 1U << 7, | 
|  | 135 | BINDER_DEBUG_THREADS                = 1U << 8, | 
|  | 136 | BINDER_DEBUG_TRANSACTION            = 1U << 9, | 
|  | 137 | BINDER_DEBUG_TRANSACTION_COMPLETE   = 1U << 10, | 
|  | 138 | BINDER_DEBUG_FREE_BUFFER            = 1U << 11, | 
|  | 139 | BINDER_DEBUG_INTERNAL_REFS          = 1U << 12, | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 140 | BINDER_DEBUG_PRIORITY_CAP           = 1U << 13, | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 141 | BINDER_DEBUG_SPINLOCKS              = 1U << 14, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 142 | }; | 
|  | 143 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | | 
|  | 144 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; | 
|  | 145 | module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO); | 
|  | 146 |  | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 147 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; | 
|  | 148 | module_param_named(devices, binder_devices_param, charp, S_IRUGO); | 
|  | 149 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 150 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); | 
|  | 151 | static int binder_stop_on_user_error; | 
|  | 152 |  | 
|  | 153 | static int binder_set_stop_on_user_error(const char *val, | 
|  | 154 | struct kernel_param *kp) | 
|  | 155 | { | 
|  | 156 | int ret; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 157 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 158 | ret = param_set_int(val, kp); | 
|  | 159 | if (binder_stop_on_user_error < 2) | 
|  | 160 | wake_up(&binder_user_error_wait); | 
|  | 161 | return ret; | 
|  | 162 | } | 
|  | 163 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, | 
|  | 164 | param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO); | 
|  | 165 |  | 
|  | 166 | #define binder_debug(mask, x...) \ | 
|  | 167 | do { \ | 
|  | 168 | if (binder_debug_mask & mask) \ | 
| Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 169 | pr_info(x); \ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 170 | } while (0) | 
|  | 171 |  | 
|  | 172 | #define binder_user_error(x...) \ | 
|  | 173 | do { \ | 
|  | 174 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ | 
| Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 175 | pr_info(x); \ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 176 | if (binder_stop_on_user_error) \ | 
|  | 177 | binder_stop_on_user_error = 2; \ | 
|  | 178 | } while (0) | 
|  | 179 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 180 | #define to_flat_binder_object(hdr) \ | 
|  | 181 | container_of(hdr, struct flat_binder_object, hdr) | 
|  | 182 |  | 
|  | 183 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) | 
|  | 184 |  | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 185 | #define to_binder_buffer_object(hdr) \ | 
|  | 186 | container_of(hdr, struct binder_buffer_object, hdr) | 
|  | 187 |  | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 188 | #define to_binder_fd_array_object(hdr) \ | 
|  | 189 | container_of(hdr, struct binder_fd_array_object, hdr) | 
|  | 190 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 191 | enum binder_stat_types { | 
|  | 192 | BINDER_STAT_PROC, | 
|  | 193 | BINDER_STAT_THREAD, | 
|  | 194 | BINDER_STAT_NODE, | 
|  | 195 | BINDER_STAT_REF, | 
|  | 196 | BINDER_STAT_DEATH, | 
|  | 197 | BINDER_STAT_TRANSACTION, | 
|  | 198 | BINDER_STAT_TRANSACTION_COMPLETE, | 
|  | 199 | BINDER_STAT_COUNT | 
|  | 200 | }; | 
|  | 201 |  | 
|  | 202 | struct binder_stats { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 203 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; | 
|  | 204 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; | 
|  | 205 | atomic_t obj_created[BINDER_STAT_COUNT]; | 
|  | 206 | atomic_t obj_deleted[BINDER_STAT_COUNT]; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 207 | }; | 
|  | 208 |  | 
|  | 209 | static struct binder_stats binder_stats; | 
|  | 210 |  | 
|  | 211 | static inline void binder_stats_deleted(enum binder_stat_types type) | 
|  | 212 | { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 213 | atomic_inc(&binder_stats.obj_deleted[type]); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 214 | } | 
|  | 215 |  | 
|  | 216 | static inline void binder_stats_created(enum binder_stat_types type) | 
|  | 217 | { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 218 | atomic_inc(&binder_stats.obj_created[type]); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | struct binder_transaction_log_entry { | 
|  | 222 | int debug_id; | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 223 | int debug_id_done; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 224 | int call_type; | 
|  | 225 | int from_proc; | 
|  | 226 | int from_thread; | 
|  | 227 | int target_handle; | 
|  | 228 | int to_proc; | 
|  | 229 | int to_thread; | 
|  | 230 | int to_node; | 
|  | 231 | int data_size; | 
|  | 232 | int offsets_size; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 233 | int return_error_line; | 
|  | 234 | uint32_t return_error; | 
|  | 235 | uint32_t return_error_param; | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 236 | const char *context_name; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 237 | }; | 
|  | 238 | struct binder_transaction_log { | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 239 | atomic_t cur; | 
|  | 240 | bool full; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 241 | struct binder_transaction_log_entry entry[32]; | 
|  | 242 | }; | 
|  | 243 | static struct binder_transaction_log binder_transaction_log; | 
|  | 244 | static struct binder_transaction_log binder_transaction_log_failed; | 
|  | 245 |  | 
|  | 246 | static struct binder_transaction_log_entry *binder_transaction_log_add( | 
|  | 247 | struct binder_transaction_log *log) | 
|  | 248 | { | 
|  | 249 | struct binder_transaction_log_entry *e; | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 250 | unsigned int cur = atomic_inc_return(&log->cur); | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 251 |  | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 252 | if (cur >= ARRAY_SIZE(log->entry)) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 253 | log->full = 1; | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 254 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; | 
|  | 255 | WRITE_ONCE(e->debug_id_done, 0); | 
|  | 256 | /* | 
|  | 257 | * write-barrier to synchronize access to e->debug_id_done. | 
|  | 258 | * We make sure the initialized 0 value is seen before | 
|  | 259 | * memset() other fields are zeroed by memset. | 
|  | 260 | */ | 
|  | 261 | smp_wmb(); | 
|  | 262 | memset(e, 0, sizeof(*e)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 263 | return e; | 
|  | 264 | } | 
|  | 265 |  | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 266 | struct binder_context { | 
|  | 267 | struct binder_node *binder_context_mgr_node; | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 268 | struct mutex context_mgr_node_lock; | 
|  | 269 |  | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 270 | kuid_t binder_context_mgr_uid; | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 271 | const char *name; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 272 | }; | 
|  | 273 |  | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 274 | struct binder_device { | 
|  | 275 | struct hlist_node hlist; | 
|  | 276 | struct miscdevice miscdev; | 
|  | 277 | struct binder_context context; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 278 | }; | 
|  | 279 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 280 | /** | 
|  | 281 | * struct binder_work - work enqueued on a worklist | 
|  | 282 | * @entry:             node enqueued on list | 
|  | 283 | * @type:              type of work to be performed | 
|  | 284 | * | 
|  | 285 | * There are separate work lists for proc, thread, and node (async). | 
|  | 286 | */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 287 | struct binder_work { | 
|  | 288 | struct list_head entry; | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 289 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 290 | enum { | 
|  | 291 | BINDER_WORK_TRANSACTION = 1, | 
|  | 292 | BINDER_WORK_TRANSACTION_COMPLETE, | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 293 | BINDER_WORK_RETURN_ERROR, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 294 | BINDER_WORK_NODE, | 
|  | 295 | BINDER_WORK_DEAD_BINDER, | 
|  | 296 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, | 
|  | 297 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, | 
|  | 298 | } type; | 
|  | 299 | }; | 
|  | 300 |  | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 301 | struct binder_error { | 
|  | 302 | struct binder_work work; | 
|  | 303 | uint32_t cmd; | 
|  | 304 | }; | 
|  | 305 |  | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 306 | /** | 
|  | 307 | * struct binder_node - binder node bookkeeping | 
|  | 308 | * @debug_id:             unique ID for debugging | 
|  | 309 | *                        (invariant after initialized) | 
|  | 310 | * @lock:                 lock for node fields | 
|  | 311 | * @work:                 worklist element for node work | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 312 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 313 | * @rb_node:              element for proc->nodes tree | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 314 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 315 | * @dead_node:            element for binder_dead_nodes list | 
|  | 316 | *                        (protected by binder_dead_nodes_lock) | 
|  | 317 | * @proc:                 binder_proc that owns this node | 
|  | 318 | *                        (invariant after initialized) | 
|  | 319 | * @refs:                 list of references on this node | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 320 | *                        (protected by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 321 | * @internal_strong_refs: used to take strong references when | 
|  | 322 | *                        initiating a transaction | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 323 | *                        (protected by @proc->inner_lock if @proc | 
|  | 324 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 325 | * @local_weak_refs:      weak user refs from local process | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 326 | *                        (protected by @proc->inner_lock if @proc | 
|  | 327 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 328 | * @local_strong_refs:    strong user refs from local process | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 329 | *                        (protected by @proc->inner_lock if @proc | 
|  | 330 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 331 | * @tmp_refs:             temporary kernel refs | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 332 | *                        (protected by @proc->inner_lock while @proc | 
|  | 333 | *                        is valid, and by binder_dead_nodes_lock | 
|  | 334 | *                        if @proc is NULL. During inc/dec and node release | 
|  | 335 | *                        it is also protected by @lock to provide safety | 
|  | 336 | *                        as the node dies and @proc becomes NULL) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 337 | * @ptr:                  userspace pointer for node | 
|  | 338 | *                        (invariant, no lock needed) | 
|  | 339 | * @cookie:               userspace cookie for node | 
|  | 340 | *                        (invariant, no lock needed) | 
|  | 341 | * @has_strong_ref:       userspace notified of strong ref | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 342 | *                        (protected by @proc->inner_lock if @proc | 
|  | 343 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 344 | * @pending_strong_ref:   userspace has acked notification of strong ref | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 345 | *                        (protected by @proc->inner_lock if @proc | 
|  | 346 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 347 | * @has_weak_ref:         userspace notified of weak ref | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 348 | *                        (protected by @proc->inner_lock if @proc | 
|  | 349 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 350 | * @pending_weak_ref:     userspace has acked notification of weak ref | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 351 | *                        (protected by @proc->inner_lock if @proc | 
|  | 352 | *                        and by @lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 353 | * @has_async_transaction: async transaction to node in progress | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 354 | *                        (protected by @lock) | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 355 | * @sched_policy:         minimum scheduling policy for node | 
|  | 356 | *                        (invariant after initialized) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 357 | * @accept_fds:           file descriptor operations supported for node | 
|  | 358 | *                        (invariant after initialized) | 
|  | 359 | * @min_priority:         minimum scheduling priority | 
|  | 360 | *                        (invariant after initialized) | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 361 | * @inherit_rt:           inherit RT scheduling policy from caller | 
|  | 362 | *                        (invariant after initialized) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 363 | * @async_todo:           list of async work items | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 364 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 365 | * | 
|  | 366 | * Bookkeeping structure for binder nodes. | 
|  | 367 | */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 368 | struct binder_node { | 
|  | 369 | int debug_id; | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 370 | spinlock_t lock; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 371 | struct binder_work work; | 
|  | 372 | union { | 
|  | 373 | struct rb_node rb_node; | 
|  | 374 | struct hlist_node dead_node; | 
|  | 375 | }; | 
|  | 376 | struct binder_proc *proc; | 
|  | 377 | struct hlist_head refs; | 
|  | 378 | int internal_strong_refs; | 
|  | 379 | int local_weak_refs; | 
|  | 380 | int local_strong_refs; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 381 | int tmp_refs; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 382 | binder_uintptr_t ptr; | 
|  | 383 | binder_uintptr_t cookie; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 384 | struct { | 
|  | 385 | /* | 
|  | 386 | * bitfield elements protected by | 
|  | 387 | * proc inner_lock | 
|  | 388 | */ | 
|  | 389 | u8 has_strong_ref:1; | 
|  | 390 | u8 pending_strong_ref:1; | 
|  | 391 | u8 has_weak_ref:1; | 
|  | 392 | u8 pending_weak_ref:1; | 
|  | 393 | }; | 
|  | 394 | struct { | 
|  | 395 | /* | 
|  | 396 | * invariant after initialization | 
|  | 397 | */ | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 398 | u8 sched_policy:2; | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 399 | u8 inherit_rt:1; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 400 | u8 accept_fds:1; | 
|  | 401 | u8 min_priority; | 
|  | 402 | }; | 
|  | 403 | bool has_async_transaction; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 404 | struct list_head async_todo; | 
|  | 405 | }; | 
|  | 406 |  | 
|  | 407 | struct binder_ref_death { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 408 | /** | 
|  | 409 | * @work: worklist element for death notifications | 
|  | 410 | *        (protected by inner_lock of the proc that | 
|  | 411 | *        this ref belongs to) | 
|  | 412 | */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 413 | struct binder_work work; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 414 | binder_uintptr_t cookie; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 415 | }; | 
|  | 416 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 417 | /** | 
|  | 418 | * struct binder_ref_data - binder_ref counts and id | 
|  | 419 | * @debug_id:        unique ID for the ref | 
|  | 420 | * @desc:            unique userspace handle for ref | 
|  | 421 | * @strong:          strong ref count (debugging only if not locked) | 
|  | 422 | * @weak:            weak ref count (debugging only if not locked) | 
|  | 423 | * | 
|  | 424 | * Structure to hold ref count and ref id information. Since | 
|  | 425 | * the actual ref can only be accessed with a lock, this structure | 
|  | 426 | * is used to return information about the ref to callers of | 
|  | 427 | * ref inc/dec functions. | 
|  | 428 | */ | 
|  | 429 | struct binder_ref_data { | 
|  | 430 | int debug_id; | 
|  | 431 | uint32_t desc; | 
|  | 432 | int strong; | 
|  | 433 | int weak; | 
|  | 434 | }; | 
|  | 435 |  | 
|  | 436 | /** | 
|  | 437 | * struct binder_ref - struct to track references on nodes | 
|  | 438 | * @data:        binder_ref_data containing id, handle, and current refcounts | 
|  | 439 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree | 
|  | 440 | * @rb_node_node: node for lookup by @node in proc's rb_tree | 
|  | 441 | * @node_entry:  list entry for node->refs list in target node | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 442 | *               (protected by @node->lock) | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 443 | * @proc:        binder_proc containing ref | 
|  | 444 | * @node:        binder_node of target node. When cleaning up a | 
|  | 445 | *               ref for deletion in binder_cleanup_ref, a non-NULL | 
|  | 446 | *               @node indicates the node must be freed | 
|  | 447 | * @death:       pointer to death notification (ref_death) if requested | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 448 | *               (protected by @node->lock) | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 449 | * | 
|  | 450 | * Structure to track references from procA to target node (on procB). This | 
|  | 451 | * structure is unsafe to access without holding @proc->outer_lock. | 
|  | 452 | */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 453 | struct binder_ref { | 
|  | 454 | /* Lookups needed: */ | 
|  | 455 | /*   node + proc => ref (transaction) */ | 
|  | 456 | /*   desc + proc => ref (transaction, inc/dec ref) */ | 
|  | 457 | /*   node => refs + procs (proc exit) */ | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 458 | struct binder_ref_data data; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 459 | struct rb_node rb_node_desc; | 
|  | 460 | struct rb_node rb_node_node; | 
|  | 461 | struct hlist_node node_entry; | 
|  | 462 | struct binder_proc *proc; | 
|  | 463 | struct binder_node *node; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 464 | struct binder_ref_death *death; | 
|  | 465 | }; | 
|  | 466 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 467 | enum binder_deferred_state { | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 468 | BINDER_DEFERRED_FLUSH        = 0x01, | 
|  | 469 | BINDER_DEFERRED_RELEASE      = 0x02, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 470 | }; | 
|  | 471 |  | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 472 | /** | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 473 | * struct binder_priority - scheduler policy and priority | 
|  | 474 | * @sched_policy            scheduler policy | 
|  | 475 | * @prio                    [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT | 
|  | 476 | * | 
|  | 477 | * The binder driver supports inheriting the following scheduler policies: | 
|  | 478 | * SCHED_NORMAL | 
|  | 479 | * SCHED_BATCH | 
|  | 480 | * SCHED_FIFO | 
|  | 481 | * SCHED_RR | 
|  | 482 | */ | 
|  | 483 | struct binder_priority { | 
|  | 484 | unsigned int sched_policy; | 
|  | 485 | int prio; | 
|  | 486 | }; | 
|  | 487 |  | 
|  | 488 | /** | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 489 | * struct binder_proc - binder process bookkeeping | 
|  | 490 | * @proc_node:            element for binder_procs list | 
|  | 491 | * @threads:              rbtree of binder_threads in this proc | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 492 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 493 | * @nodes:                rbtree of binder nodes associated with | 
|  | 494 | *                        this proc ordered by node->ptr | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 495 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 496 | * @refs_by_desc:         rbtree of refs ordered by ref->desc | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 497 | *                        (protected by @outer_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 498 | * @refs_by_node:         rbtree of refs ordered by ref->node | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 499 | *                        (protected by @outer_lock) | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 500 | * @waiting_threads:      threads currently waiting for proc work | 
|  | 501 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 502 | * @pid                   PID of group_leader of process | 
|  | 503 | *                        (invariant after initialized) | 
|  | 504 | * @tsk                   task_struct for group_leader of process | 
|  | 505 | *                        (invariant after initialized) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 506 | * @deferred_work_node:   element for binder_deferred_list | 
|  | 507 | *                        (protected by binder_deferred_lock) | 
|  | 508 | * @deferred_work:        bitmap of deferred work to perform | 
|  | 509 | *                        (protected by binder_deferred_lock) | 
|  | 510 | * @is_dead:              process is dead and awaiting free | 
|  | 511 | *                        when outstanding transactions are cleaned up | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 512 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 513 | * @todo:                 list of work for this process | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 514 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 515 | * @stats:                per-process binder statistics | 
|  | 516 | *                        (atomics, no lock needed) | 
|  | 517 | * @delivered_death:      list of delivered death notification | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 518 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 519 | * @max_threads:          cap on number of binder threads | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 520 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 521 | * @requested_threads:    number of binder threads requested but not | 
|  | 522 | *                        yet started. In current implementation, can | 
|  | 523 | *                        only be 0 or 1. | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 524 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 525 | * @requested_threads_started: number binder threads started | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 526 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 527 | * @tmp_ref:              temporary reference to indicate proc is in use | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 528 | *                        (protected by @inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 529 | * @default_priority:     default scheduler priority | 
|  | 530 | *                        (invariant after initialized) | 
|  | 531 | * @debugfs_entry:        debugfs node | 
|  | 532 | * @alloc:                binder allocator bookkeeping | 
|  | 533 | * @context:              binder_context for this proc | 
|  | 534 | *                        (invariant after initialized) | 
|  | 535 | * @inner_lock:           can nest under outer_lock and/or node lock | 
|  | 536 | * @outer_lock:           no nesting under innor or node lock | 
|  | 537 | *                        Lock order: 1) outer, 2) node, 3) inner | 
|  | 538 | * | 
|  | 539 | * Bookkeeping structure for binder processes | 
|  | 540 | */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 541 | struct binder_proc { | 
|  | 542 | struct hlist_node proc_node; | 
|  | 543 | struct rb_root threads; | 
|  | 544 | struct rb_root nodes; | 
|  | 545 | struct rb_root refs_by_desc; | 
|  | 546 | struct rb_root refs_by_node; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 547 | struct list_head waiting_threads; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 548 | int pid; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 549 | struct task_struct *tsk; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 550 | struct hlist_node deferred_work_node; | 
|  | 551 | int deferred_work; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 552 | bool is_dead; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 553 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 554 | struct list_head todo; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 555 | struct binder_stats stats; | 
|  | 556 | struct list_head delivered_death; | 
|  | 557 | int max_threads; | 
|  | 558 | int requested_threads; | 
|  | 559 | int requested_threads_started; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 560 | int tmp_ref; | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 561 | struct binder_priority default_priority; | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 562 | struct dentry *debugfs_entry; | 
| Todd Kjos | f85d229 | 2016-10-10 10:39:59 -0700 | [diff] [blame] | 563 | struct binder_alloc alloc; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 564 | struct binder_context *context; | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 565 | spinlock_t inner_lock; | 
|  | 566 | spinlock_t outer_lock; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 567 | }; | 
|  | 568 |  | 
|  | 569 | enum { | 
|  | 570 | BINDER_LOOPER_STATE_REGISTERED  = 0x01, | 
|  | 571 | BINDER_LOOPER_STATE_ENTERED     = 0x02, | 
|  | 572 | BINDER_LOOPER_STATE_EXITED      = 0x04, | 
|  | 573 | BINDER_LOOPER_STATE_INVALID     = 0x08, | 
|  | 574 | BINDER_LOOPER_STATE_WAITING     = 0x10, | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 575 | BINDER_LOOPER_STATE_POLL        = 0x20, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 576 | }; | 
|  | 577 |  | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 578 | /** | 
|  | 579 | * struct binder_thread - binder thread bookkeeping | 
|  | 580 | * @proc:                 binder process for this thread | 
|  | 581 | *                        (invariant after initialization) | 
|  | 582 | * @rb_node:              element for proc->threads rbtree | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 583 | *                        (protected by @proc->inner_lock) | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 584 | * @waiting_thread_node:  element for @proc->waiting_threads list | 
|  | 585 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 586 | * @pid:                  PID for this thread | 
|  | 587 | *                        (invariant after initialization) | 
|  | 588 | * @looper:               bitmap of looping state | 
|  | 589 | *                        (only accessed by this thread) | 
|  | 590 | * @looper_needs_return:  looping thread needs to exit driver | 
|  | 591 | *                        (no lock needed) | 
|  | 592 | * @transaction_stack:    stack of in-progress transactions for this thread | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 593 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 594 | * @todo:                 list of work to do for this thread | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 595 | *                        (protected by @proc->inner_lock) | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 596 | * @process_todo:         whether work in @todo should be processed | 
|  | 597 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 598 | * @return_error:         transaction errors reported by this thread | 
|  | 599 | *                        (only accessed by this thread) | 
|  | 600 | * @reply_error:          transaction errors reported by target thread | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 601 | *                        (protected by @proc->inner_lock) | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 602 | * @wait:                 wait queue for thread work | 
|  | 603 | * @stats:                per-thread statistics | 
|  | 604 | *                        (atomics, no lock needed) | 
|  | 605 | * @tmp_ref:              temporary reference to indicate thread is in use | 
|  | 606 | *                        (atomic since @proc->inner_lock cannot | 
|  | 607 | *                        always be acquired) | 
|  | 608 | * @is_dead:              thread is dead and awaiting free | 
|  | 609 | *                        when outstanding transactions are cleaned up | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 610 | *                        (protected by @proc->inner_lock) | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 611 | * @task:                 struct task_struct for this thread | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 612 | * | 
|  | 613 | * Bookkeeping structure for binder threads. | 
|  | 614 | */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 615 | struct binder_thread { | 
|  | 616 | struct binder_proc *proc; | 
|  | 617 | struct rb_node rb_node; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 618 | struct list_head waiting_thread_node; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 619 | int pid; | 
| Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 620 | int looper;              /* only modified by this thread */ | 
|  | 621 | bool looper_need_return; /* can be written by other thread */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 622 | struct binder_transaction *transaction_stack; | 
|  | 623 | struct list_head todo; | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 624 | bool process_todo; | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 625 | struct binder_error return_error; | 
|  | 626 | struct binder_error reply_error; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 627 | wait_queue_head_t wait; | 
|  | 628 | struct binder_stats stats; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 629 | atomic_t tmp_ref; | 
|  | 630 | bool is_dead; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 631 | struct task_struct *task; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 632 | }; | 
|  | 633 |  | 
|  | 634 | struct binder_transaction { | 
|  | 635 | int debug_id; | 
|  | 636 | struct binder_work work; | 
|  | 637 | struct binder_thread *from; | 
|  | 638 | struct binder_transaction *from_parent; | 
|  | 639 | struct binder_proc *to_proc; | 
|  | 640 | struct binder_thread *to_thread; | 
|  | 641 | struct binder_transaction *to_parent; | 
|  | 642 | unsigned need_reply:1; | 
|  | 643 | /* unsigned is_dead:1; */	/* not used at the moment */ | 
|  | 644 |  | 
|  | 645 | struct binder_buffer *buffer; | 
|  | 646 | unsigned int	code; | 
|  | 647 | unsigned int	flags; | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 648 | struct binder_priority	priority; | 
|  | 649 | struct binder_priority	saved_priority; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 650 | bool    set_priority_called; | 
| Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 651 | kuid_t	sender_euid; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 652 | /** | 
|  | 653 | * @lock:  protects @from, @to_proc, and @to_thread | 
|  | 654 | * | 
|  | 655 | * @from, @to_proc, and @to_thread can be set to NULL | 
|  | 656 | * during thread teardown | 
|  | 657 | */ | 
|  | 658 | spinlock_t lock; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 659 | }; | 
|  | 660 |  | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 661 | /** | 
|  | 662 | * binder_proc_lock() - Acquire outer lock for given binder_proc | 
|  | 663 | * @proc:         struct binder_proc to acquire | 
|  | 664 | * | 
|  | 665 | * Acquires proc->outer_lock. Used to protect binder_ref | 
|  | 666 | * structures associated with the given proc. | 
|  | 667 | */ | 
|  | 668 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) | 
|  | 669 | static void | 
|  | 670 | _binder_proc_lock(struct binder_proc *proc, int line) | 
|  | 671 | { | 
|  | 672 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 673 | "%s: line=%d\n", __func__, line); | 
|  | 674 | spin_lock(&proc->outer_lock); | 
|  | 675 | } | 
|  | 676 |  | 
|  | 677 | /** | 
|  | 678 | * binder_proc_unlock() - Release spinlock for given binder_proc | 
|  | 679 | * @proc:         struct binder_proc to acquire | 
|  | 680 | * | 
|  | 681 | * Release lock acquired via binder_proc_lock() | 
|  | 682 | */ | 
|  | 683 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) | 
|  | 684 | static void | 
|  | 685 | _binder_proc_unlock(struct binder_proc *proc, int line) | 
|  | 686 | { | 
|  | 687 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 688 | "%s: line=%d\n", __func__, line); | 
|  | 689 | spin_unlock(&proc->outer_lock); | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | /** | 
|  | 693 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc | 
|  | 694 | * @proc:         struct binder_proc to acquire | 
|  | 695 | * | 
|  | 696 | * Acquires proc->inner_lock. Used to protect todo lists | 
|  | 697 | */ | 
|  | 698 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) | 
|  | 699 | static void | 
|  | 700 | _binder_inner_proc_lock(struct binder_proc *proc, int line) | 
|  | 701 | { | 
|  | 702 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 703 | "%s: line=%d\n", __func__, line); | 
|  | 704 | spin_lock(&proc->inner_lock); | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | /** | 
|  | 708 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc | 
|  | 709 | * @proc:         struct binder_proc to acquire | 
|  | 710 | * | 
|  | 711 | * Release lock acquired via binder_inner_proc_lock() | 
|  | 712 | */ | 
|  | 713 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) | 
|  | 714 | static void | 
|  | 715 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) | 
|  | 716 | { | 
|  | 717 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 718 | "%s: line=%d\n", __func__, line); | 
|  | 719 | spin_unlock(&proc->inner_lock); | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | /** | 
|  | 723 | * binder_node_lock() - Acquire spinlock for given binder_node | 
|  | 724 | * @node:         struct binder_node to acquire | 
|  | 725 | * | 
|  | 726 | * Acquires node->lock. Used to protect binder_node fields | 
|  | 727 | */ | 
|  | 728 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) | 
|  | 729 | static void | 
|  | 730 | _binder_node_lock(struct binder_node *node, int line) | 
|  | 731 | { | 
|  | 732 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 733 | "%s: line=%d\n", __func__, line); | 
|  | 734 | spin_lock(&node->lock); | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | /** | 
|  | 738 | * binder_node_unlock() - Release spinlock for given binder_proc | 
|  | 739 | * @node:         struct binder_node to acquire | 
|  | 740 | * | 
|  | 741 | * Release lock acquired via binder_node_lock() | 
|  | 742 | */ | 
|  | 743 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) | 
|  | 744 | static void | 
|  | 745 | _binder_node_unlock(struct binder_node *node, int line) | 
|  | 746 | { | 
|  | 747 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 748 | "%s: line=%d\n", __func__, line); | 
|  | 749 | spin_unlock(&node->lock); | 
|  | 750 | } | 
|  | 751 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 752 | /** | 
|  | 753 | * binder_node_inner_lock() - Acquire node and inner locks | 
|  | 754 | * @node:         struct binder_node to acquire | 
|  | 755 | * | 
|  | 756 | * Acquires node->lock. If node->proc also acquires | 
|  | 757 | * proc->inner_lock. Used to protect binder_node fields | 
|  | 758 | */ | 
|  | 759 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) | 
|  | 760 | static void | 
|  | 761 | _binder_node_inner_lock(struct binder_node *node, int line) | 
|  | 762 | { | 
|  | 763 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 764 | "%s: line=%d\n", __func__, line); | 
|  | 765 | spin_lock(&node->lock); | 
|  | 766 | if (node->proc) | 
|  | 767 | binder_inner_proc_lock(node->proc); | 
|  | 768 | } | 
|  | 769 |  | 
|  | 770 | /** | 
|  | 771 | * binder_node_unlock() - Release node and inner locks | 
|  | 772 | * @node:         struct binder_node to acquire | 
|  | 773 | * | 
|  | 774 | * Release lock acquired via binder_node_lock() | 
|  | 775 | */ | 
|  | 776 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) | 
|  | 777 | static void | 
|  | 778 | _binder_node_inner_unlock(struct binder_node *node, int line) | 
|  | 779 | { | 
|  | 780 | struct binder_proc *proc = node->proc; | 
|  | 781 |  | 
|  | 782 | binder_debug(BINDER_DEBUG_SPINLOCKS, | 
|  | 783 | "%s: line=%d\n", __func__, line); | 
|  | 784 | if (proc) | 
|  | 785 | binder_inner_proc_unlock(proc); | 
|  | 786 | spin_unlock(&node->lock); | 
|  | 787 | } | 
|  | 788 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 789 | static bool binder_worklist_empty_ilocked(struct list_head *list) | 
|  | 790 | { | 
|  | 791 | return list_empty(list); | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | /** | 
|  | 795 | * binder_worklist_empty() - Check if no items on the work list | 
|  | 796 | * @proc:       binder_proc associated with list | 
|  | 797 | * @list:	list to check | 
|  | 798 | * | 
|  | 799 | * Return: true if there are no items on list, else false | 
|  | 800 | */ | 
|  | 801 | static bool binder_worklist_empty(struct binder_proc *proc, | 
|  | 802 | struct list_head *list) | 
|  | 803 | { | 
|  | 804 | bool ret; | 
|  | 805 |  | 
|  | 806 | binder_inner_proc_lock(proc); | 
|  | 807 | ret = binder_worklist_empty_ilocked(list); | 
|  | 808 | binder_inner_proc_unlock(proc); | 
|  | 809 | return ret; | 
|  | 810 | } | 
|  | 811 |  | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 812 | /** | 
|  | 813 | * binder_enqueue_work_ilocked() - Add an item to the work list | 
|  | 814 | * @work:         struct binder_work to add to list | 
|  | 815 | * @target_list:  list to add work to | 
|  | 816 | * | 
|  | 817 | * Adds the work to the specified list. Asserts that work | 
|  | 818 | * is not already on a list. | 
|  | 819 | * | 
|  | 820 | * Requires the proc->inner_lock to be held. | 
|  | 821 | */ | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 822 | static void | 
|  | 823 | binder_enqueue_work_ilocked(struct binder_work *work, | 
|  | 824 | struct list_head *target_list) | 
|  | 825 | { | 
|  | 826 | BUG_ON(target_list == NULL); | 
|  | 827 | BUG_ON(work->entry.next && !list_empty(&work->entry)); | 
|  | 828 | list_add_tail(&work->entry, target_list); | 
|  | 829 | } | 
|  | 830 |  | 
|  | 831 | /** | 
| Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 832 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 833 | * @thread:       thread to queue work to | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 834 | * @work:         struct binder_work to add to list | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 835 | * | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 836 | * Adds the work to the todo list of the thread. Doesn't set the process_todo | 
|  | 837 | * flag, which means that (if it wasn't already set) the thread will go to | 
|  | 838 | * sleep without handling this work when it calls read. | 
|  | 839 | * | 
|  | 840 | * Requires the proc->inner_lock to be held. | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 841 | */ | 
|  | 842 | static void | 
| Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 843 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, | 
|  | 844 | struct binder_work *work) | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 845 | { | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 846 | binder_enqueue_work_ilocked(work, &thread->todo); | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | /** | 
|  | 850 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list | 
|  | 851 | * @thread:       thread to queue work to | 
|  | 852 | * @work:         struct binder_work to add to list | 
|  | 853 | * | 
|  | 854 | * Adds the work to the todo list of the thread, and enables processing | 
|  | 855 | * of the todo queue. | 
|  | 856 | * | 
|  | 857 | * Requires the proc->inner_lock to be held. | 
|  | 858 | */ | 
|  | 859 | static void | 
|  | 860 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, | 
|  | 861 | struct binder_work *work) | 
|  | 862 | { | 
|  | 863 | binder_enqueue_work_ilocked(work, &thread->todo); | 
|  | 864 | thread->process_todo = true; | 
|  | 865 | } | 
|  | 866 |  | 
|  | 867 | /** | 
|  | 868 | * binder_enqueue_thread_work() - Add an item to the thread work list | 
|  | 869 | * @thread:       thread to queue work to | 
|  | 870 | * @work:         struct binder_work to add to list | 
|  | 871 | * | 
|  | 872 | * Adds the work to the todo list of the thread, and enables processing | 
|  | 873 | * of the todo queue. | 
|  | 874 | */ | 
|  | 875 | static void | 
|  | 876 | binder_enqueue_thread_work(struct binder_thread *thread, | 
|  | 877 | struct binder_work *work) | 
|  | 878 | { | 
|  | 879 | binder_inner_proc_lock(thread->proc); | 
|  | 880 | binder_enqueue_thread_work_ilocked(thread, work); | 
|  | 881 | binder_inner_proc_unlock(thread->proc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 882 | } | 
|  | 883 |  | 
|  | 884 | static void | 
|  | 885 | binder_dequeue_work_ilocked(struct binder_work *work) | 
|  | 886 | { | 
|  | 887 | list_del_init(&work->entry); | 
|  | 888 | } | 
|  | 889 |  | 
|  | 890 | /** | 
|  | 891 | * binder_dequeue_work() - Removes an item from the work list | 
|  | 892 | * @proc:         binder_proc associated with list | 
|  | 893 | * @work:         struct binder_work to remove from list | 
|  | 894 | * | 
|  | 895 | * Removes the specified work item from whatever list it is on. | 
|  | 896 | * Can safely be called if work is not on any list. | 
|  | 897 | */ | 
|  | 898 | static void | 
|  | 899 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) | 
|  | 900 | { | 
|  | 901 | binder_inner_proc_lock(proc); | 
|  | 902 | binder_dequeue_work_ilocked(work); | 
|  | 903 | binder_inner_proc_unlock(proc); | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | static struct binder_work *binder_dequeue_work_head_ilocked( | 
|  | 907 | struct list_head *list) | 
|  | 908 | { | 
|  | 909 | struct binder_work *w; | 
|  | 910 |  | 
|  | 911 | w = list_first_entry_or_null(list, struct binder_work, entry); | 
|  | 912 | if (w) | 
|  | 913 | list_del_init(&w->entry); | 
|  | 914 | return w; | 
|  | 915 | } | 
|  | 916 |  | 
|  | 917 | /** | 
|  | 918 | * binder_dequeue_work_head() - Dequeues the item at head of list | 
|  | 919 | * @proc:         binder_proc associated with list | 
|  | 920 | * @list:         list to dequeue head | 
|  | 921 | * | 
|  | 922 | * Removes the head of the list if there are items on the list | 
|  | 923 | * | 
|  | 924 | * Return: pointer dequeued binder_work, NULL if list was empty | 
|  | 925 | */ | 
|  | 926 | static struct binder_work *binder_dequeue_work_head( | 
|  | 927 | struct binder_proc *proc, | 
|  | 928 | struct list_head *list) | 
|  | 929 | { | 
|  | 930 | struct binder_work *w; | 
|  | 931 |  | 
|  | 932 | binder_inner_proc_lock(proc); | 
|  | 933 | w = binder_dequeue_work_head_ilocked(list); | 
|  | 934 | binder_inner_proc_unlock(proc); | 
|  | 935 | return w; | 
|  | 936 | } | 
|  | 937 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 938 | static void | 
|  | 939 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 940 | static void binder_free_thread(struct binder_thread *thread); | 
|  | 941 | static void binder_free_proc(struct binder_proc *proc); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 942 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 943 |  | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 944 | struct files_struct *binder_get_files_struct(struct binder_proc *proc) | 
|  | 945 | { | 
|  | 946 | return get_files_struct(proc->tsk); | 
|  | 947 | } | 
|  | 948 |  | 
| Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 949 | static int task_get_unused_fd_flags(struct binder_proc *proc, int flags) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 950 | { | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 951 | struct files_struct *files; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 952 | unsigned long rlim_cur; | 
|  | 953 | unsigned long irqs; | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 954 | int ret; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 955 |  | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 956 | files = binder_get_files_struct(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 957 | if (files == NULL) | 
|  | 958 | return -ESRCH; | 
|  | 959 |  | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 960 | if (!lock_task_sighand(proc->tsk, &irqs)) { | 
|  | 961 | ret = -EMFILE; | 
|  | 962 | goto err; | 
|  | 963 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 964 |  | 
| Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 965 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); | 
|  | 966 | unlock_task_sighand(proc->tsk, &irqs); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 967 |  | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 968 | ret = __alloc_fd(files, 0, rlim_cur, flags); | 
|  | 969 | err: | 
|  | 970 | put_files_struct(files); | 
|  | 971 | return ret; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 972 | } | 
|  | 973 |  | 
|  | 974 | /* | 
|  | 975 | * copied from fd_install | 
|  | 976 | */ | 
|  | 977 | static void task_fd_install( | 
|  | 978 | struct binder_proc *proc, unsigned int fd, struct file *file) | 
|  | 979 | { | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 980 | struct files_struct *files = binder_get_files_struct(proc); | 
|  | 981 |  | 
|  | 982 | if (files) { | 
|  | 983 | __fd_install(files, fd, file); | 
|  | 984 | put_files_struct(files); | 
|  | 985 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 986 | } | 
|  | 987 |  | 
|  | 988 | /* | 
|  | 989 | * copied from sys_close | 
|  | 990 | */ | 
|  | 991 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) | 
|  | 992 | { | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 993 | struct files_struct *files = binder_get_files_struct(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 994 | int retval; | 
|  | 995 |  | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 996 | if (files == NULL) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 997 | return -ESRCH; | 
|  | 998 |  | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 999 | retval = __close_fd(files, fd); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1000 | /* can't restart close syscall because file table entry was cleared */ | 
|  | 1001 | if (unlikely(retval == -ERESTARTSYS || | 
|  | 1002 | retval == -ERESTARTNOINTR || | 
|  | 1003 | retval == -ERESTARTNOHAND || | 
|  | 1004 | retval == -ERESTART_RESTARTBLOCK)) | 
|  | 1005 | retval = -EINTR; | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 1006 | put_files_struct(files); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1007 |  | 
|  | 1008 | return retval; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1009 | } | 
|  | 1010 |  | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1011 | static bool binder_has_work_ilocked(struct binder_thread *thread, | 
|  | 1012 | bool do_proc_work) | 
|  | 1013 | { | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1014 | return thread->process_todo || | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1015 | thread->looper_need_return || | 
|  | 1016 | (do_proc_work && | 
|  | 1017 | !binder_worklist_empty_ilocked(&thread->proc->todo)); | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) | 
|  | 1021 | { | 
|  | 1022 | bool has_work; | 
|  | 1023 |  | 
|  | 1024 | binder_inner_proc_lock(thread->proc); | 
|  | 1025 | has_work = binder_has_work_ilocked(thread, do_proc_work); | 
|  | 1026 | binder_inner_proc_unlock(thread->proc); | 
|  | 1027 |  | 
|  | 1028 | return has_work; | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) | 
|  | 1032 | { | 
|  | 1033 | return !thread->transaction_stack && | 
|  | 1034 | binder_worklist_empty_ilocked(&thread->todo) && | 
|  | 1035 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | | 
|  | 1036 | BINDER_LOOPER_STATE_REGISTERED)); | 
|  | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, | 
|  | 1040 | bool sync) | 
|  | 1041 | { | 
|  | 1042 | struct rb_node *n; | 
|  | 1043 | struct binder_thread *thread; | 
|  | 1044 |  | 
|  | 1045 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { | 
|  | 1046 | thread = rb_entry(n, struct binder_thread, rb_node); | 
|  | 1047 | if (thread->looper & BINDER_LOOPER_STATE_POLL && | 
|  | 1048 | binder_available_for_proc_work_ilocked(thread)) { | 
|  | 1049 | if (sync) | 
|  | 1050 | wake_up_interruptible_sync(&thread->wait); | 
|  | 1051 | else | 
|  | 1052 | wake_up_interruptible(&thread->wait); | 
|  | 1053 | } | 
|  | 1054 | } | 
|  | 1055 | } | 
|  | 1056 |  | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1057 | /** | 
|  | 1058 | * binder_select_thread_ilocked() - selects a thread for doing proc work. | 
|  | 1059 | * @proc:	process to select a thread from | 
|  | 1060 | * | 
|  | 1061 | * Note that calling this function moves the thread off the waiting_threads | 
|  | 1062 | * list, so it can only be woken up by the caller of this function, or a | 
|  | 1063 | * signal. Therefore, callers *should* always wake up the thread this function | 
|  | 1064 | * returns. | 
|  | 1065 | * | 
|  | 1066 | * Return:	If there's a thread currently waiting for process work, | 
|  | 1067 | *		returns that thread. Otherwise returns NULL. | 
|  | 1068 | */ | 
|  | 1069 | static struct binder_thread * | 
|  | 1070 | binder_select_thread_ilocked(struct binder_proc *proc) | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1071 | { | 
|  | 1072 | struct binder_thread *thread; | 
|  | 1073 |  | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1074 | assert_spin_locked(&proc->inner_lock); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1075 | thread = list_first_entry_or_null(&proc->waiting_threads, | 
|  | 1076 | struct binder_thread, | 
|  | 1077 | waiting_thread_node); | 
|  | 1078 |  | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1079 | if (thread) | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1080 | list_del_init(&thread->waiting_thread_node); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1081 |  | 
|  | 1082 | return thread; | 
|  | 1083 | } | 
|  | 1084 |  | 
|  | 1085 | /** | 
|  | 1086 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. | 
|  | 1087 | * @proc:	process to wake up a thread in | 
|  | 1088 | * @thread:	specific thread to wake-up (may be NULL) | 
|  | 1089 | * @sync:	whether to do a synchronous wake-up | 
|  | 1090 | * | 
|  | 1091 | * This function wakes up a thread in the @proc process. | 
|  | 1092 | * The caller may provide a specific thread to wake-up in | 
|  | 1093 | * the @thread parameter. If @thread is NULL, this function | 
|  | 1094 | * will wake up threads that have called poll(). | 
|  | 1095 | * | 
|  | 1096 | * Note that for this function to work as expected, callers | 
|  | 1097 | * should first call binder_select_thread() to find a thread | 
|  | 1098 | * to handle the work (if they don't have a thread already), | 
|  | 1099 | * and pass the result into the @thread parameter. | 
|  | 1100 | */ | 
|  | 1101 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, | 
|  | 1102 | struct binder_thread *thread, | 
|  | 1103 | bool sync) | 
|  | 1104 | { | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1105 | assert_spin_locked(&proc->inner_lock); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1106 |  | 
|  | 1107 | if (thread) { | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1108 | if (sync) | 
|  | 1109 | wake_up_interruptible_sync(&thread->wait); | 
|  | 1110 | else | 
|  | 1111 | wake_up_interruptible(&thread->wait); | 
|  | 1112 | return; | 
|  | 1113 | } | 
|  | 1114 |  | 
|  | 1115 | /* Didn't find a thread waiting for proc work; this can happen | 
|  | 1116 | * in two scenarios: | 
|  | 1117 | * 1. All threads are busy handling transactions | 
|  | 1118 | *    In that case, one of those threads should call back into | 
|  | 1119 | *    the kernel driver soon and pick up this work. | 
|  | 1120 | * 2. Threads are using the (e)poll interface, in which case | 
|  | 1121 | *    they may be blocked on the waitqueue without having been | 
|  | 1122 | *    added to waiting_threads. For this case, we just iterate | 
|  | 1123 | *    over all threads not handling transaction work, and | 
|  | 1124 | *    wake them all up. We wake all because we don't know whether | 
|  | 1125 | *    a thread that called into (e)poll is handling non-binder | 
|  | 1126 | *    work currently. | 
|  | 1127 | */ | 
|  | 1128 | binder_wakeup_poll_threads_ilocked(proc, sync); | 
|  | 1129 | } | 
|  | 1130 |  | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1131 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) | 
|  | 1132 | { | 
|  | 1133 | struct binder_thread *thread = binder_select_thread_ilocked(proc); | 
|  | 1134 |  | 
|  | 1135 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); | 
|  | 1136 | } | 
|  | 1137 |  | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1138 | static bool is_rt_policy(int policy) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1139 | { | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1140 | return policy == SCHED_FIFO || policy == SCHED_RR; | 
|  | 1141 | } | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1142 |  | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1143 | static bool is_fair_policy(int policy) | 
|  | 1144 | { | 
|  | 1145 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; | 
|  | 1146 | } | 
|  | 1147 |  | 
|  | 1148 | static bool binder_supported_policy(int policy) | 
|  | 1149 | { | 
|  | 1150 | return is_fair_policy(policy) || is_rt_policy(policy); | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | static int to_userspace_prio(int policy, int kernel_priority) | 
|  | 1154 | { | 
|  | 1155 | if (is_fair_policy(policy)) | 
|  | 1156 | return PRIO_TO_NICE(kernel_priority); | 
|  | 1157 | else | 
|  | 1158 | return MAX_USER_RT_PRIO - 1 - kernel_priority; | 
|  | 1159 | } | 
|  | 1160 |  | 
|  | 1161 | static int to_kernel_prio(int policy, int user_priority) | 
|  | 1162 | { | 
|  | 1163 | if (is_fair_policy(policy)) | 
|  | 1164 | return NICE_TO_PRIO(user_priority); | 
|  | 1165 | else | 
|  | 1166 | return MAX_USER_RT_PRIO - 1 - user_priority; | 
|  | 1167 | } | 
|  | 1168 |  | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1169 | static void binder_do_set_priority(struct task_struct *task, | 
|  | 1170 | struct binder_priority desired, | 
|  | 1171 | bool verify) | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1172 | { | 
|  | 1173 | int priority; /* user-space prio value */ | 
|  | 1174 | bool has_cap_nice; | 
|  | 1175 | unsigned int policy = desired.sched_policy; | 
|  | 1176 |  | 
|  | 1177 | if (task->policy == policy && task->normal_prio == desired.prio) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1178 | return; | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1179 |  | 
|  | 1180 | has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE); | 
|  | 1181 |  | 
|  | 1182 | priority = to_userspace_prio(policy, desired.prio); | 
|  | 1183 |  | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1184 | if (verify && is_rt_policy(policy) && !has_cap_nice) { | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1185 | long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO); | 
|  | 1186 |  | 
|  | 1187 | if (max_rtprio == 0) { | 
|  | 1188 | policy = SCHED_NORMAL; | 
|  | 1189 | priority = MIN_NICE; | 
|  | 1190 | } else if (priority > max_rtprio) { | 
|  | 1191 | priority = max_rtprio; | 
|  | 1192 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1193 | } | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1194 |  | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1195 | if (verify && is_fair_policy(policy) && !has_cap_nice) { | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1196 | long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE)); | 
|  | 1197 |  | 
|  | 1198 | if (min_nice > MAX_NICE) { | 
|  | 1199 | binder_user_error("%d RLIMIT_NICE not set\n", | 
|  | 1200 | task->pid); | 
|  | 1201 | return; | 
|  | 1202 | } else if (priority < min_nice) { | 
|  | 1203 | priority = min_nice; | 
|  | 1204 | } | 
|  | 1205 | } | 
|  | 1206 |  | 
|  | 1207 | if (policy != desired.sched_policy || | 
|  | 1208 | to_kernel_prio(policy, priority) != desired.prio) | 
|  | 1209 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, | 
|  | 1210 | "%d: priority %d not allowed, using %d instead\n", | 
|  | 1211 | task->pid, desired.prio, | 
|  | 1212 | to_kernel_prio(policy, priority)); | 
|  | 1213 |  | 
| Martijn Coenen | 81402ea | 2017-05-08 09:33:22 -0700 | [diff] [blame] | 1214 | trace_binder_set_priority(task->tgid, task->pid, task->normal_prio, | 
|  | 1215 | to_kernel_prio(policy, priority), | 
|  | 1216 | desired.prio); | 
|  | 1217 |  | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1218 | /* Set the actual priority */ | 
|  | 1219 | if (task->policy != policy || is_rt_policy(policy)) { | 
|  | 1220 | struct sched_param params; | 
|  | 1221 |  | 
|  | 1222 | params.sched_priority = is_rt_policy(policy) ? priority : 0; | 
|  | 1223 |  | 
|  | 1224 | sched_setscheduler_nocheck(task, | 
|  | 1225 | policy | SCHED_RESET_ON_FORK, | 
|  | 1226 | ¶ms); | 
|  | 1227 | } | 
|  | 1228 | if (is_fair_policy(policy)) | 
|  | 1229 | set_user_nice(task, priority); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1230 | } | 
|  | 1231 |  | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1232 | static void binder_set_priority(struct task_struct *task, | 
|  | 1233 | struct binder_priority desired) | 
|  | 1234 | { | 
|  | 1235 | binder_do_set_priority(task, desired, /* verify = */ true); | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | static void binder_restore_priority(struct task_struct *task, | 
|  | 1239 | struct binder_priority desired) | 
|  | 1240 | { | 
|  | 1241 | binder_do_set_priority(task, desired, /* verify = */ false); | 
|  | 1242 | } | 
|  | 1243 |  | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1244 | static void binder_transaction_priority(struct task_struct *task, | 
|  | 1245 | struct binder_transaction *t, | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1246 | struct binder_priority node_prio, | 
|  | 1247 | bool inherit_rt) | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1248 | { | 
| Ganesh Mahendran | 9add7c4 | 2017-09-27 15:12:25 +0800 | [diff] [blame] | 1249 | struct binder_priority desired_prio = t->priority; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1250 |  | 
|  | 1251 | if (t->set_priority_called) | 
|  | 1252 | return; | 
|  | 1253 |  | 
|  | 1254 | t->set_priority_called = true; | 
|  | 1255 | t->saved_priority.sched_policy = task->policy; | 
|  | 1256 | t->saved_priority.prio = task->normal_prio; | 
|  | 1257 |  | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1258 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { | 
|  | 1259 | desired_prio.prio = NICE_TO_PRIO(0); | 
|  | 1260 | desired_prio.sched_policy = SCHED_NORMAL; | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1261 | } | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1262 |  | 
|  | 1263 | if (node_prio.prio < t->priority.prio || | 
|  | 1264 | (node_prio.prio == t->priority.prio && | 
|  | 1265 | node_prio.sched_policy == SCHED_FIFO)) { | 
|  | 1266 | /* | 
|  | 1267 | * In case the minimum priority on the node is | 
|  | 1268 | * higher (lower value), use that priority. If | 
|  | 1269 | * the priority is the same, but the node uses | 
|  | 1270 | * SCHED_FIFO, prefer SCHED_FIFO, since it can | 
|  | 1271 | * run unbounded, unlike SCHED_RR. | 
|  | 1272 | */ | 
|  | 1273 | desired_prio = node_prio; | 
|  | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | binder_set_priority(task, desired_prio); | 
|  | 1277 | } | 
|  | 1278 |  | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1279 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, | 
|  | 1280 | binder_uintptr_t ptr) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1281 | { | 
|  | 1282 | struct rb_node *n = proc->nodes.rb_node; | 
|  | 1283 | struct binder_node *node; | 
|  | 1284 |  | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1285 | assert_spin_locked(&proc->inner_lock); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1286 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1287 | while (n) { | 
|  | 1288 | node = rb_entry(n, struct binder_node, rb_node); | 
|  | 1289 |  | 
|  | 1290 | if (ptr < node->ptr) | 
|  | 1291 | n = n->rb_left; | 
|  | 1292 | else if (ptr > node->ptr) | 
|  | 1293 | n = n->rb_right; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1294 | else { | 
|  | 1295 | /* | 
|  | 1296 | * take an implicit weak reference | 
|  | 1297 | * to ensure node stays alive until | 
|  | 1298 | * call to binder_put_node() | 
|  | 1299 | */ | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1300 | binder_inc_node_tmpref_ilocked(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1301 | return node; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1302 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1303 | } | 
|  | 1304 | return NULL; | 
|  | 1305 | } | 
|  | 1306 |  | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1307 | static struct binder_node *binder_get_node(struct binder_proc *proc, | 
|  | 1308 | binder_uintptr_t ptr) | 
|  | 1309 | { | 
|  | 1310 | struct binder_node *node; | 
|  | 1311 |  | 
|  | 1312 | binder_inner_proc_lock(proc); | 
|  | 1313 | node = binder_get_node_ilocked(proc, ptr); | 
|  | 1314 | binder_inner_proc_unlock(proc); | 
|  | 1315 | return node; | 
|  | 1316 | } | 
|  | 1317 |  | 
|  | 1318 | static struct binder_node *binder_init_node_ilocked( | 
|  | 1319 | struct binder_proc *proc, | 
|  | 1320 | struct binder_node *new_node, | 
|  | 1321 | struct flat_binder_object *fp) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1322 | { | 
|  | 1323 | struct rb_node **p = &proc->nodes.rb_node; | 
|  | 1324 | struct rb_node *parent = NULL; | 
|  | 1325 | struct binder_node *node; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1326 | binder_uintptr_t ptr = fp ? fp->binder : 0; | 
|  | 1327 | binder_uintptr_t cookie = fp ? fp->cookie : 0; | 
|  | 1328 | __u32 flags = fp ? fp->flags : 0; | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1329 | s8 priority; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1330 |  | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1331 | assert_spin_locked(&proc->inner_lock); | 
|  | 1332 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1333 | while (*p) { | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1334 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1335 | parent = *p; | 
|  | 1336 | node = rb_entry(parent, struct binder_node, rb_node); | 
|  | 1337 |  | 
|  | 1338 | if (ptr < node->ptr) | 
|  | 1339 | p = &(*p)->rb_left; | 
|  | 1340 | else if (ptr > node->ptr) | 
|  | 1341 | p = &(*p)->rb_right; | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1342 | else { | 
|  | 1343 | /* | 
|  | 1344 | * A matching node is already in | 
|  | 1345 | * the rb tree. Abandon the init | 
|  | 1346 | * and return it. | 
|  | 1347 | */ | 
|  | 1348 | binder_inc_node_tmpref_ilocked(node); | 
|  | 1349 | return node; | 
|  | 1350 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1351 | } | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1352 | node = new_node; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1353 | binder_stats_created(BINDER_STAT_NODE); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1354 | node->tmp_refs++; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1355 | rb_link_node(&node->rb_node, parent, p); | 
|  | 1356 | rb_insert_color(&node->rb_node, &proc->nodes); | 
| Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 1357 | node->debug_id = atomic_inc_return(&binder_last_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1358 | node->proc = proc; | 
|  | 1359 | node->ptr = ptr; | 
|  | 1360 | node->cookie = cookie; | 
|  | 1361 | node->work.type = BINDER_WORK_NODE; | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1362 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; | 
| Ganesh Mahendran | 6cd2631 | 2017-09-26 17:56:25 +0800 | [diff] [blame] | 1363 | node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >> | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1364 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; | 
|  | 1365 | node->min_priority = to_kernel_prio(node->sched_policy, priority); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1366 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1367 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 1368 | spin_lock_init(&node->lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1369 | INIT_LIST_HEAD(&node->work.entry); | 
|  | 1370 | INIT_LIST_HEAD(&node->async_todo); | 
|  | 1371 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1372 | "%d:%d node %d u%016llx c%016llx created\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1373 | proc->pid, current->pid, node->debug_id, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1374 | (u64)node->ptr, (u64)node->cookie); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1375 |  | 
|  | 1376 | return node; | 
|  | 1377 | } | 
|  | 1378 |  | 
|  | 1379 | static struct binder_node *binder_new_node(struct binder_proc *proc, | 
|  | 1380 | struct flat_binder_object *fp) | 
|  | 1381 | { | 
|  | 1382 | struct binder_node *node; | 
|  | 1383 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); | 
|  | 1384 |  | 
|  | 1385 | if (!new_node) | 
|  | 1386 | return NULL; | 
|  | 1387 | binder_inner_proc_lock(proc); | 
|  | 1388 | node = binder_init_node_ilocked(proc, new_node, fp); | 
|  | 1389 | binder_inner_proc_unlock(proc); | 
|  | 1390 | if (node != new_node) | 
|  | 1391 | /* | 
|  | 1392 | * The node was already added by another thread | 
|  | 1393 | */ | 
|  | 1394 | kfree(new_node); | 
|  | 1395 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1396 | return node; | 
|  | 1397 | } | 
|  | 1398 |  | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1399 | static void binder_free_node(struct binder_node *node) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1400 | { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1401 | kfree(node); | 
|  | 1402 | binder_stats_deleted(BINDER_STAT_NODE); | 
|  | 1403 | } | 
|  | 1404 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1405 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, | 
|  | 1406 | int internal, | 
|  | 1407 | struct list_head *target_list) | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1408 | { | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1409 | struct binder_proc *proc = node->proc; | 
|  | 1410 |  | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1411 | assert_spin_locked(&node->lock); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1412 | if (proc) | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1413 | assert_spin_locked(&proc->inner_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1414 | if (strong) { | 
|  | 1415 | if (internal) { | 
|  | 1416 | if (target_list == NULL && | 
|  | 1417 | node->internal_strong_refs == 0 && | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 1418 | !(node->proc && | 
|  | 1419 | node == node->proc->context-> | 
|  | 1420 | binder_context_mgr_node && | 
|  | 1421 | node->has_strong_ref)) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1422 | pr_err("invalid inc strong node for %d\n", | 
|  | 1423 | node->debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1424 | return -EINVAL; | 
|  | 1425 | } | 
|  | 1426 | node->internal_strong_refs++; | 
|  | 1427 | } else | 
|  | 1428 | node->local_strong_refs++; | 
|  | 1429 | if (!node->has_strong_ref && target_list) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1430 | binder_dequeue_work_ilocked(&node->work); | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1431 | /* | 
|  | 1432 | * Note: this function is the only place where we queue | 
|  | 1433 | * directly to a thread->todo without using the | 
|  | 1434 | * corresponding binder_enqueue_thread_work() helper | 
|  | 1435 | * functions; in this case it's ok to not set the | 
|  | 1436 | * process_todo flag, since we know this node work will | 
|  | 1437 | * always be followed by other work that starts queue | 
|  | 1438 | * processing: in case of synchronous transactions, a | 
|  | 1439 | * BR_REPLY or BR_ERROR; in case of oneway | 
|  | 1440 | * transactions, a BR_TRANSACTION_COMPLETE. | 
|  | 1441 | */ | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1442 | binder_enqueue_work_ilocked(&node->work, target_list); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1443 | } | 
|  | 1444 | } else { | 
|  | 1445 | if (!internal) | 
|  | 1446 | node->local_weak_refs++; | 
|  | 1447 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { | 
|  | 1448 | if (target_list == NULL) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1449 | pr_err("invalid inc weak node for %d\n", | 
|  | 1450 | node->debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1451 | return -EINVAL; | 
|  | 1452 | } | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1453 | /* | 
|  | 1454 | * See comment above | 
|  | 1455 | */ | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1456 | binder_enqueue_work_ilocked(&node->work, target_list); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1457 | } | 
|  | 1458 | } | 
|  | 1459 | return 0; | 
|  | 1460 | } | 
|  | 1461 |  | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1462 | static int binder_inc_node(struct binder_node *node, int strong, int internal, | 
|  | 1463 | struct list_head *target_list) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1464 | { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1465 | int ret; | 
|  | 1466 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1467 | binder_node_inner_lock(node); | 
|  | 1468 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); | 
|  | 1469 | binder_node_inner_unlock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1470 |  | 
|  | 1471 | return ret; | 
|  | 1472 | } | 
|  | 1473 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1474 | static bool binder_dec_node_nilocked(struct binder_node *node, | 
|  | 1475 | int strong, int internal) | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1476 | { | 
|  | 1477 | struct binder_proc *proc = node->proc; | 
|  | 1478 |  | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1479 | assert_spin_locked(&node->lock); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1480 | if (proc) | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1481 | assert_spin_locked(&proc->inner_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1482 | if (strong) { | 
|  | 1483 | if (internal) | 
|  | 1484 | node->internal_strong_refs--; | 
|  | 1485 | else | 
|  | 1486 | node->local_strong_refs--; | 
|  | 1487 | if (node->local_strong_refs || node->internal_strong_refs) | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1488 | return false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1489 | } else { | 
|  | 1490 | if (!internal) | 
|  | 1491 | node->local_weak_refs--; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1492 | if (node->local_weak_refs || node->tmp_refs || | 
|  | 1493 | !hlist_empty(&node->refs)) | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1494 | return false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1495 | } | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1496 |  | 
|  | 1497 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1498 | if (list_empty(&node->work.entry)) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1499 | binder_enqueue_work_ilocked(&node->work, &proc->todo); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1500 | binder_wakeup_proc_ilocked(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1501 | } | 
|  | 1502 | } else { | 
|  | 1503 | if (hlist_empty(&node->refs) && !node->local_strong_refs && | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1504 | !node->local_weak_refs && !node->tmp_refs) { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1505 | if (proc) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1506 | binder_dequeue_work_ilocked(&node->work); | 
|  | 1507 | rb_erase(&node->rb_node, &proc->nodes); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1508 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1509 | "refless node %d deleted\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1510 | node->debug_id); | 
|  | 1511 | } else { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1512 | BUG_ON(!list_empty(&node->work.entry)); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1513 | spin_lock(&binder_dead_nodes_lock); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1514 | /* | 
|  | 1515 | * tmp_refs could have changed so | 
|  | 1516 | * check it again | 
|  | 1517 | */ | 
|  | 1518 | if (node->tmp_refs) { | 
|  | 1519 | spin_unlock(&binder_dead_nodes_lock); | 
|  | 1520 | return false; | 
|  | 1521 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1522 | hlist_del(&node->dead_node); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1523 | spin_unlock(&binder_dead_nodes_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1524 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1525 | "dead node %d deleted\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1526 | node->debug_id); | 
|  | 1527 | } | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1528 | return true; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1529 | } | 
|  | 1530 | } | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1531 | return false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1532 | } | 
|  | 1533 |  | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1534 | static void binder_dec_node(struct binder_node *node, int strong, int internal) | 
|  | 1535 | { | 
|  | 1536 | bool free_node; | 
|  | 1537 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1538 | binder_node_inner_lock(node); | 
|  | 1539 | free_node = binder_dec_node_nilocked(node, strong, internal); | 
|  | 1540 | binder_node_inner_unlock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1541 | if (free_node) | 
|  | 1542 | binder_free_node(node); | 
|  | 1543 | } | 
|  | 1544 |  | 
|  | 1545 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1546 | { | 
|  | 1547 | /* | 
|  | 1548 | * No call to binder_inc_node() is needed since we | 
|  | 1549 | * don't need to inform userspace of any changes to | 
|  | 1550 | * tmp_refs | 
|  | 1551 | */ | 
|  | 1552 | node->tmp_refs++; | 
|  | 1553 | } | 
|  | 1554 |  | 
|  | 1555 | /** | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1556 | * binder_inc_node_tmpref() - take a temporary reference on node | 
|  | 1557 | * @node:	node to reference | 
|  | 1558 | * | 
|  | 1559 | * Take reference on node to prevent the node from being freed | 
|  | 1560 | * while referenced only by a local variable. The inner lock is | 
|  | 1561 | * needed to serialize with the node work on the queue (which | 
|  | 1562 | * isn't needed after the node is dead). If the node is dead | 
|  | 1563 | * (node->proc is NULL), use binder_dead_nodes_lock to protect | 
|  | 1564 | * node->tmp_refs against dead-node-only cases where the node | 
|  | 1565 | * lock cannot be acquired (eg traversing the dead node list to | 
|  | 1566 | * print nodes) | 
|  | 1567 | */ | 
|  | 1568 | static void binder_inc_node_tmpref(struct binder_node *node) | 
|  | 1569 | { | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1570 | binder_node_lock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1571 | if (node->proc) | 
|  | 1572 | binder_inner_proc_lock(node->proc); | 
|  | 1573 | else | 
|  | 1574 | spin_lock(&binder_dead_nodes_lock); | 
|  | 1575 | binder_inc_node_tmpref_ilocked(node); | 
|  | 1576 | if (node->proc) | 
|  | 1577 | binder_inner_proc_unlock(node->proc); | 
|  | 1578 | else | 
|  | 1579 | spin_unlock(&binder_dead_nodes_lock); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1580 | binder_node_unlock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1581 | } | 
|  | 1582 |  | 
|  | 1583 | /** | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1584 | * binder_dec_node_tmpref() - remove a temporary reference on node | 
|  | 1585 | * @node:	node to reference | 
|  | 1586 | * | 
|  | 1587 | * Release temporary reference on node taken via binder_inc_node_tmpref() | 
|  | 1588 | */ | 
|  | 1589 | static void binder_dec_node_tmpref(struct binder_node *node) | 
|  | 1590 | { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1591 | bool free_node; | 
|  | 1592 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1593 | binder_node_inner_lock(node); | 
|  | 1594 | if (!node->proc) | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1595 | spin_lock(&binder_dead_nodes_lock); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1596 | node->tmp_refs--; | 
|  | 1597 | BUG_ON(node->tmp_refs < 0); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1598 | if (!node->proc) | 
|  | 1599 | spin_unlock(&binder_dead_nodes_lock); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1600 | /* | 
|  | 1601 | * Call binder_dec_node() to check if all refcounts are 0 | 
|  | 1602 | * and cleanup is needed. Calling with strong=0 and internal=1 | 
|  | 1603 | * causes no actual reference to be released in binder_dec_node(). | 
|  | 1604 | * If that changes, a change is needed here too. | 
|  | 1605 | */ | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1606 | free_node = binder_dec_node_nilocked(node, 0, 1); | 
|  | 1607 | binder_node_inner_unlock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1608 | if (free_node) | 
|  | 1609 | binder_free_node(node); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1610 | } | 
|  | 1611 |  | 
|  | 1612 | static void binder_put_node(struct binder_node *node) | 
|  | 1613 | { | 
|  | 1614 | binder_dec_node_tmpref(node); | 
|  | 1615 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1616 |  | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1617 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, | 
|  | 1618 | u32 desc, bool need_strong_ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1619 | { | 
|  | 1620 | struct rb_node *n = proc->refs_by_desc.rb_node; | 
|  | 1621 | struct binder_ref *ref; | 
|  | 1622 |  | 
|  | 1623 | while (n) { | 
|  | 1624 | ref = rb_entry(n, struct binder_ref, rb_node_desc); | 
|  | 1625 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1626 | if (desc < ref->data.desc) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1627 | n = n->rb_left; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1628 | } else if (desc > ref->data.desc) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1629 | n = n->rb_right; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1630 | } else if (need_strong_ref && !ref->data.strong) { | 
| Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1631 | binder_user_error("tried to use weak ref as strong ref\n"); | 
|  | 1632 | return NULL; | 
|  | 1633 | } else { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1634 | return ref; | 
| Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1635 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1636 | } | 
|  | 1637 | return NULL; | 
|  | 1638 | } | 
|  | 1639 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1640 | /** | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1641 | * binder_get_ref_for_node_olocked() - get the ref associated with given node | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1642 | * @proc:	binder_proc that owns the ref | 
|  | 1643 | * @node:	binder_node of target | 
|  | 1644 | * @new_ref:	newly allocated binder_ref to be initialized or %NULL | 
|  | 1645 | * | 
|  | 1646 | * Look up the ref for the given node and return it if it exists | 
|  | 1647 | * | 
|  | 1648 | * If it doesn't exist and the caller provides a newly allocated | 
|  | 1649 | * ref, initialize the fields of the newly allocated ref and insert | 
|  | 1650 | * into the given proc rb_trees and node refs list. | 
|  | 1651 | * | 
|  | 1652 | * Return:	the ref for node. It is possible that another thread | 
|  | 1653 | *		allocated/initialized the ref first in which case the | 
|  | 1654 | *		returned ref would be different than the passed-in | 
|  | 1655 | *		new_ref. new_ref must be kfree'd by the caller in | 
|  | 1656 | *		this case. | 
|  | 1657 | */ | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1658 | static struct binder_ref *binder_get_ref_for_node_olocked( | 
|  | 1659 | struct binder_proc *proc, | 
|  | 1660 | struct binder_node *node, | 
|  | 1661 | struct binder_ref *new_ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1662 | { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1663 | struct binder_context *context = proc->context; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1664 | struct rb_node **p = &proc->refs_by_node.rb_node; | 
|  | 1665 | struct rb_node *parent = NULL; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1666 | struct binder_ref *ref; | 
|  | 1667 | struct rb_node *n; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1668 |  | 
|  | 1669 | while (*p) { | 
|  | 1670 | parent = *p; | 
|  | 1671 | ref = rb_entry(parent, struct binder_ref, rb_node_node); | 
|  | 1672 |  | 
|  | 1673 | if (node < ref->node) | 
|  | 1674 | p = &(*p)->rb_left; | 
|  | 1675 | else if (node > ref->node) | 
|  | 1676 | p = &(*p)->rb_right; | 
|  | 1677 | else | 
|  | 1678 | return ref; | 
|  | 1679 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1680 | if (!new_ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1681 | return NULL; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1682 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1683 | binder_stats_created(BINDER_STAT_REF); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1684 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1685 | new_ref->proc = proc; | 
|  | 1686 | new_ref->node = node; | 
|  | 1687 | rb_link_node(&new_ref->rb_node_node, parent, p); | 
|  | 1688 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); | 
|  | 1689 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1690 | new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1691 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { | 
|  | 1692 | ref = rb_entry(n, struct binder_ref, rb_node_desc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1693 | if (ref->data.desc > new_ref->data.desc) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1694 | break; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1695 | new_ref->data.desc = ref->data.desc + 1; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1696 | } | 
|  | 1697 |  | 
|  | 1698 | p = &proc->refs_by_desc.rb_node; | 
|  | 1699 | while (*p) { | 
|  | 1700 | parent = *p; | 
|  | 1701 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); | 
|  | 1702 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1703 | if (new_ref->data.desc < ref->data.desc) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1704 | p = &(*p)->rb_left; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1705 | else if (new_ref->data.desc > ref->data.desc) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1706 | p = &(*p)->rb_right; | 
|  | 1707 | else | 
|  | 1708 | BUG(); | 
|  | 1709 | } | 
|  | 1710 | rb_link_node(&new_ref->rb_node_desc, parent, p); | 
|  | 1711 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1712 |  | 
|  | 1713 | binder_node_lock(node); | 
| Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1714 | hlist_add_head(&new_ref->node_entry, &node->refs); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1715 |  | 
| Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1716 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
|  | 1717 | "%d new ref %d desc %d for node %d\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1718 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, | 
| Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1719 | node->debug_id); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1720 | binder_node_unlock(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1721 | return new_ref; | 
|  | 1722 | } | 
|  | 1723 |  | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1724 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1725 | { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1726 | bool delete_node = false; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1727 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1728 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1729 | "%d delete ref %d desc %d for node %d\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1730 | ref->proc->pid, ref->data.debug_id, ref->data.desc, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1731 | ref->node->debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1732 |  | 
|  | 1733 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); | 
|  | 1734 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1735 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1736 | binder_node_inner_lock(ref->node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1737 | if (ref->data.strong) | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1738 | binder_dec_node_nilocked(ref->node, 1, 1); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1739 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1740 | hlist_del(&ref->node_entry); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1741 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); | 
|  | 1742 | binder_node_inner_unlock(ref->node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1743 | /* | 
|  | 1744 | * Clear ref->node unless we want the caller to free the node | 
|  | 1745 | */ | 
|  | 1746 | if (!delete_node) { | 
|  | 1747 | /* | 
|  | 1748 | * The caller uses ref->node to determine | 
|  | 1749 | * whether the node needs to be freed. Clear | 
|  | 1750 | * it since the node is still alive. | 
|  | 1751 | */ | 
|  | 1752 | ref->node = NULL; | 
|  | 1753 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1754 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1755 | if (ref->death) { | 
|  | 1756 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1757 | "%d delete ref %d desc %d has death notification\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1758 | ref->proc->pid, ref->data.debug_id, | 
|  | 1759 | ref->data.desc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1760 | binder_dequeue_work(ref->proc, &ref->death->work); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1761 | binder_stats_deleted(BINDER_STAT_DEATH); | 
|  | 1762 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1763 | binder_stats_deleted(BINDER_STAT_REF); | 
|  | 1764 | } | 
|  | 1765 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1766 | /** | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1767 | * binder_inc_ref_olocked() - increment the ref for given handle | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1768 | * @ref:         ref to be incremented | 
|  | 1769 | * @strong:      if true, strong increment, else weak | 
|  | 1770 | * @target_list: list to queue node work on | 
|  | 1771 | * | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1772 | * Increment the ref. @ref->proc->outer_lock must be held on entry | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1773 | * | 
|  | 1774 | * Return: 0, if successful, else errno | 
|  | 1775 | */ | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1776 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, | 
|  | 1777 | struct list_head *target_list) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1778 | { | 
|  | 1779 | int ret; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1780 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1781 | if (strong) { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1782 | if (ref->data.strong == 0) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1783 | ret = binder_inc_node(ref->node, 1, 1, target_list); | 
|  | 1784 | if (ret) | 
|  | 1785 | return ret; | 
|  | 1786 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1787 | ref->data.strong++; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1788 | } else { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1789 | if (ref->data.weak == 0) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1790 | ret = binder_inc_node(ref->node, 0, 1, target_list); | 
|  | 1791 | if (ret) | 
|  | 1792 | return ret; | 
|  | 1793 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1794 | ref->data.weak++; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1795 | } | 
|  | 1796 | return 0; | 
|  | 1797 | } | 
|  | 1798 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1799 | /** | 
|  | 1800 | * binder_dec_ref() - dec the ref for given handle | 
|  | 1801 | * @ref:	ref to be decremented | 
|  | 1802 | * @strong:	if true, strong decrement, else weak | 
|  | 1803 | * | 
|  | 1804 | * Decrement the ref. | 
|  | 1805 | * | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1806 | * Return: true if ref is cleaned up and ready to be freed | 
|  | 1807 | */ | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1808 | static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1809 | { | 
|  | 1810 | if (strong) { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1811 | if (ref->data.strong == 0) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1812 | binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1813 | ref->proc->pid, ref->data.debug_id, | 
|  | 1814 | ref->data.desc, ref->data.strong, | 
|  | 1815 | ref->data.weak); | 
|  | 1816 | return false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1817 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1818 | ref->data.strong--; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1819 | if (ref->data.strong == 0) | 
|  | 1820 | binder_dec_node(ref->node, strong, 1); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1821 | } else { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1822 | if (ref->data.weak == 0) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1823 | binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1824 | ref->proc->pid, ref->data.debug_id, | 
|  | 1825 | ref->data.desc, ref->data.strong, | 
|  | 1826 | ref->data.weak); | 
|  | 1827 | return false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1828 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1829 | ref->data.weak--; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1830 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1831 | if (ref->data.strong == 0 && ref->data.weak == 0) { | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1832 | binder_cleanup_ref_olocked(ref); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1833 | return true; | 
|  | 1834 | } | 
|  | 1835 | return false; | 
|  | 1836 | } | 
|  | 1837 |  | 
|  | 1838 | /** | 
|  | 1839 | * binder_get_node_from_ref() - get the node from the given proc/desc | 
|  | 1840 | * @proc:	proc containing the ref | 
|  | 1841 | * @desc:	the handle associated with the ref | 
|  | 1842 | * @need_strong_ref: if true, only return node if ref is strong | 
|  | 1843 | * @rdata:	the id/refcount data for the ref | 
|  | 1844 | * | 
|  | 1845 | * Given a proc and ref handle, return the associated binder_node | 
|  | 1846 | * | 
|  | 1847 | * Return: a binder_node or NULL if not found or not strong when strong required | 
|  | 1848 | */ | 
|  | 1849 | static struct binder_node *binder_get_node_from_ref( | 
|  | 1850 | struct binder_proc *proc, | 
|  | 1851 | u32 desc, bool need_strong_ref, | 
|  | 1852 | struct binder_ref_data *rdata) | 
|  | 1853 | { | 
|  | 1854 | struct binder_node *node; | 
|  | 1855 | struct binder_ref *ref; | 
|  | 1856 |  | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1857 | binder_proc_lock(proc); | 
|  | 1858 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1859 | if (!ref) | 
|  | 1860 | goto err_no_ref; | 
|  | 1861 | node = ref->node; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1862 | /* | 
|  | 1863 | * Take an implicit reference on the node to ensure | 
|  | 1864 | * it stays alive until the call to binder_put_node() | 
|  | 1865 | */ | 
|  | 1866 | binder_inc_node_tmpref(node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1867 | if (rdata) | 
|  | 1868 | *rdata = ref->data; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1869 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1870 |  | 
|  | 1871 | return node; | 
|  | 1872 |  | 
|  | 1873 | err_no_ref: | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1874 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1875 | return NULL; | 
|  | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | /** | 
|  | 1879 | * binder_free_ref() - free the binder_ref | 
|  | 1880 | * @ref:	ref to free | 
|  | 1881 | * | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1882 | * Free the binder_ref. Free the binder_node indicated by ref->node | 
|  | 1883 | * (if non-NULL) and the binder_ref_death indicated by ref->death. | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1884 | */ | 
|  | 1885 | static void binder_free_ref(struct binder_ref *ref) | 
|  | 1886 | { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1887 | if (ref->node) | 
|  | 1888 | binder_free_node(ref->node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1889 | kfree(ref->death); | 
|  | 1890 | kfree(ref); | 
|  | 1891 | } | 
|  | 1892 |  | 
|  | 1893 | /** | 
|  | 1894 | * binder_update_ref_for_handle() - inc/dec the ref for given handle | 
|  | 1895 | * @proc:	proc containing the ref | 
|  | 1896 | * @desc:	the handle associated with the ref | 
|  | 1897 | * @increment:	true=inc reference, false=dec reference | 
|  | 1898 | * @strong:	true=strong reference, false=weak reference | 
|  | 1899 | * @rdata:	the id/refcount data for the ref | 
|  | 1900 | * | 
|  | 1901 | * Given a proc and ref handle, increment or decrement the ref | 
|  | 1902 | * according to "increment" arg. | 
|  | 1903 | * | 
|  | 1904 | * Return: 0 if successful, else errno | 
|  | 1905 | */ | 
|  | 1906 | static int binder_update_ref_for_handle(struct binder_proc *proc, | 
|  | 1907 | uint32_t desc, bool increment, bool strong, | 
|  | 1908 | struct binder_ref_data *rdata) | 
|  | 1909 | { | 
|  | 1910 | int ret = 0; | 
|  | 1911 | struct binder_ref *ref; | 
|  | 1912 | bool delete_ref = false; | 
|  | 1913 |  | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1914 | binder_proc_lock(proc); | 
|  | 1915 | ref = binder_get_ref_olocked(proc, desc, strong); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1916 | if (!ref) { | 
|  | 1917 | ret = -EINVAL; | 
|  | 1918 | goto err_no_ref; | 
|  | 1919 | } | 
|  | 1920 | if (increment) | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1921 | ret = binder_inc_ref_olocked(ref, strong, NULL); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1922 | else | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1923 | delete_ref = binder_dec_ref_olocked(ref, strong); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1924 |  | 
|  | 1925 | if (rdata) | 
|  | 1926 | *rdata = ref->data; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1927 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1928 |  | 
|  | 1929 | if (delete_ref) | 
|  | 1930 | binder_free_ref(ref); | 
|  | 1931 | return ret; | 
|  | 1932 |  | 
|  | 1933 | err_no_ref: | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1934 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1935 | return ret; | 
|  | 1936 | } | 
|  | 1937 |  | 
|  | 1938 | /** | 
|  | 1939 | * binder_dec_ref_for_handle() - dec the ref for given handle | 
|  | 1940 | * @proc:	proc containing the ref | 
|  | 1941 | * @desc:	the handle associated with the ref | 
|  | 1942 | * @strong:	true=strong reference, false=weak reference | 
|  | 1943 | * @rdata:	the id/refcount data for the ref | 
|  | 1944 | * | 
|  | 1945 | * Just calls binder_update_ref_for_handle() to decrement the ref. | 
|  | 1946 | * | 
|  | 1947 | * Return: 0 if successful, else errno | 
|  | 1948 | */ | 
|  | 1949 | static int binder_dec_ref_for_handle(struct binder_proc *proc, | 
|  | 1950 | uint32_t desc, bool strong, struct binder_ref_data *rdata) | 
|  | 1951 | { | 
|  | 1952 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); | 
|  | 1953 | } | 
|  | 1954 |  | 
|  | 1955 |  | 
|  | 1956 | /** | 
|  | 1957 | * binder_inc_ref_for_node() - increment the ref for given proc/node | 
|  | 1958 | * @proc:	 proc containing the ref | 
|  | 1959 | * @node:	 target node | 
|  | 1960 | * @strong:	 true=strong reference, false=weak reference | 
|  | 1961 | * @target_list: worklist to use if node is incremented | 
|  | 1962 | * @rdata:	 the id/refcount data for the ref | 
|  | 1963 | * | 
|  | 1964 | * Given a proc and node, increment the ref. Create the ref if it | 
|  | 1965 | * doesn't already exist | 
|  | 1966 | * | 
|  | 1967 | * Return: 0 if successful, else errno | 
|  | 1968 | */ | 
|  | 1969 | static int binder_inc_ref_for_node(struct binder_proc *proc, | 
|  | 1970 | struct binder_node *node, | 
|  | 1971 | bool strong, | 
|  | 1972 | struct list_head *target_list, | 
|  | 1973 | struct binder_ref_data *rdata) | 
|  | 1974 | { | 
|  | 1975 | struct binder_ref *ref; | 
|  | 1976 | struct binder_ref *new_ref = NULL; | 
|  | 1977 | int ret = 0; | 
|  | 1978 |  | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1979 | binder_proc_lock(proc); | 
|  | 1980 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1981 | if (!ref) { | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1982 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1983 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); | 
|  | 1984 | if (!new_ref) | 
|  | 1985 | return -ENOMEM; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1986 | binder_proc_lock(proc); | 
|  | 1987 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1988 | } | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1989 | ret = binder_inc_ref_olocked(ref, strong, target_list); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1990 | *rdata = ref->data; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1991 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1992 | if (new_ref && ref != new_ref) | 
|  | 1993 | /* | 
|  | 1994 | * Another thread created the ref first so | 
|  | 1995 | * free the one we allocated | 
|  | 1996 | */ | 
|  | 1997 | kfree(new_ref); | 
|  | 1998 | return ret; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1999 | } | 
|  | 2000 |  | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2001 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, | 
|  | 2002 | struct binder_transaction *t) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2003 | { | 
| Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2004 | BUG_ON(!target_thread); | 
| Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 2005 | assert_spin_locked(&target_thread->proc->inner_lock); | 
| Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2006 | BUG_ON(target_thread->transaction_stack != t); | 
|  | 2007 | BUG_ON(target_thread->transaction_stack->from != target_thread); | 
|  | 2008 | target_thread->transaction_stack = | 
|  | 2009 | target_thread->transaction_stack->from_parent; | 
|  | 2010 | t->from = NULL; | 
|  | 2011 | } | 
|  | 2012 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2013 | /** | 
|  | 2014 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref | 
|  | 2015 | * @thread:	thread to decrement | 
|  | 2016 | * | 
|  | 2017 | * A thread needs to be kept alive while being used to create or | 
|  | 2018 | * handle a transaction. binder_get_txn_from() is used to safely | 
|  | 2019 | * extract t->from from a binder_transaction and keep the thread | 
|  | 2020 | * indicated by t->from from being freed. When done with that | 
|  | 2021 | * binder_thread, this function is called to decrement the | 
|  | 2022 | * tmp_ref and free if appropriate (thread has been released | 
|  | 2023 | * and no transaction being processed by the driver) | 
|  | 2024 | */ | 
|  | 2025 | static void binder_thread_dec_tmpref(struct binder_thread *thread) | 
|  | 2026 | { | 
|  | 2027 | /* | 
|  | 2028 | * atomic is used to protect the counter value while | 
|  | 2029 | * it cannot reach zero or thread->is_dead is false | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2030 | */ | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2031 | binder_inner_proc_lock(thread->proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2032 | atomic_dec(&thread->tmp_ref); | 
|  | 2033 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2034 | binder_inner_proc_unlock(thread->proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2035 | binder_free_thread(thread); | 
|  | 2036 | return; | 
|  | 2037 | } | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2038 | binder_inner_proc_unlock(thread->proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2039 | } | 
|  | 2040 |  | 
|  | 2041 | /** | 
|  | 2042 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref | 
|  | 2043 | * @proc:	proc to decrement | 
|  | 2044 | * | 
|  | 2045 | * A binder_proc needs to be kept alive while being used to create or | 
|  | 2046 | * handle a transaction. proc->tmp_ref is incremented when | 
|  | 2047 | * creating a new transaction or the binder_proc is currently in-use | 
|  | 2048 | * by threads that are being released. When done with the binder_proc, | 
|  | 2049 | * this function is called to decrement the counter and free the | 
|  | 2050 | * proc if appropriate (proc has been released, all threads have | 
|  | 2051 | * been released and not currenly in-use to process a transaction). | 
|  | 2052 | */ | 
|  | 2053 | static void binder_proc_dec_tmpref(struct binder_proc *proc) | 
|  | 2054 | { | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2055 | binder_inner_proc_lock(proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2056 | proc->tmp_ref--; | 
|  | 2057 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && | 
|  | 2058 | !proc->tmp_ref) { | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2059 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2060 | binder_free_proc(proc); | 
|  | 2061 | return; | 
|  | 2062 | } | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2063 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2064 | } | 
|  | 2065 |  | 
|  | 2066 | /** | 
|  | 2067 | * binder_get_txn_from() - safely extract the "from" thread in transaction | 
|  | 2068 | * @t:	binder transaction for t->from | 
|  | 2069 | * | 
|  | 2070 | * Atomically return the "from" thread and increment the tmp_ref | 
|  | 2071 | * count for the thread to ensure it stays alive until | 
|  | 2072 | * binder_thread_dec_tmpref() is called. | 
|  | 2073 | * | 
|  | 2074 | * Return: the value of t->from | 
|  | 2075 | */ | 
|  | 2076 | static struct binder_thread *binder_get_txn_from( | 
|  | 2077 | struct binder_transaction *t) | 
|  | 2078 | { | 
|  | 2079 | struct binder_thread *from; | 
|  | 2080 |  | 
|  | 2081 | spin_lock(&t->lock); | 
|  | 2082 | from = t->from; | 
|  | 2083 | if (from) | 
|  | 2084 | atomic_inc(&from->tmp_ref); | 
|  | 2085 | spin_unlock(&t->lock); | 
|  | 2086 | return from; | 
|  | 2087 | } | 
|  | 2088 |  | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2089 | /** | 
|  | 2090 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock | 
|  | 2091 | * @t:	binder transaction for t->from | 
|  | 2092 | * | 
|  | 2093 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock | 
|  | 2094 | * to guarantee that the thread cannot be released while operating on it. | 
|  | 2095 | * The caller must call binder_inner_proc_unlock() to release the inner lock | 
|  | 2096 | * as well as call binder_dec_thread_txn() to release the reference. | 
|  | 2097 | * | 
|  | 2098 | * Return: the value of t->from | 
|  | 2099 | */ | 
|  | 2100 | static struct binder_thread *binder_get_txn_from_and_acq_inner( | 
|  | 2101 | struct binder_transaction *t) | 
|  | 2102 | { | 
|  | 2103 | struct binder_thread *from; | 
|  | 2104 |  | 
|  | 2105 | from = binder_get_txn_from(t); | 
|  | 2106 | if (!from) | 
|  | 2107 | return NULL; | 
|  | 2108 | binder_inner_proc_lock(from->proc); | 
|  | 2109 | if (t->from) { | 
|  | 2110 | BUG_ON(from != t->from); | 
|  | 2111 | return from; | 
|  | 2112 | } | 
|  | 2113 | binder_inner_proc_unlock(from->proc); | 
|  | 2114 | binder_thread_dec_tmpref(from); | 
|  | 2115 | return NULL; | 
|  | 2116 | } | 
|  | 2117 |  | 
| Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2118 | static void binder_free_transaction(struct binder_transaction *t) | 
|  | 2119 | { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2120 | if (t->buffer) | 
|  | 2121 | t->buffer->transaction = NULL; | 
|  | 2122 | kfree(t); | 
|  | 2123 | binder_stats_deleted(BINDER_STAT_TRANSACTION); | 
|  | 2124 | } | 
|  | 2125 |  | 
|  | 2126 | static void binder_send_failed_reply(struct binder_transaction *t, | 
|  | 2127 | uint32_t error_code) | 
|  | 2128 | { | 
|  | 2129 | struct binder_thread *target_thread; | 
| Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2130 | struct binder_transaction *next; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2131 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2132 | BUG_ON(t->flags & TF_ONE_WAY); | 
|  | 2133 | while (1) { | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2134 | target_thread = binder_get_txn_from_and_acq_inner(t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2135 | if (target_thread) { | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2136 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | 
|  | 2137 | "send failed reply for transaction %d to %d:%d\n", | 
|  | 2138 | t->debug_id, | 
|  | 2139 | target_thread->proc->pid, | 
|  | 2140 | target_thread->pid); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2141 |  | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2142 | binder_pop_transaction_ilocked(target_thread, t); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2143 | if (target_thread->reply_error.cmd == BR_OK) { | 
|  | 2144 | target_thread->reply_error.cmd = error_code; | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2145 | binder_enqueue_thread_work_ilocked( | 
|  | 2146 | target_thread, | 
|  | 2147 | &target_thread->reply_error.work); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2148 | wake_up_interruptible(&target_thread->wait); | 
|  | 2149 | } else { | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2150 | WARN(1, "Unexpected reply error: %u\n", | 
|  | 2151 | target_thread->reply_error.cmd); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2152 | } | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2153 | binder_inner_proc_unlock(target_thread->proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2154 | binder_thread_dec_tmpref(target_thread); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2155 | binder_free_transaction(t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2156 | return; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2157 | } | 
| Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2158 | next = t->from_parent; | 
|  | 2159 |  | 
|  | 2160 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | 
|  | 2161 | "send failed reply for transaction %d, target dead\n", | 
|  | 2162 | t->debug_id); | 
|  | 2163 |  | 
| Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2164 | binder_free_transaction(t); | 
| Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2165 | if (next == NULL) { | 
|  | 2166 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | 
|  | 2167 | "reply failed, no target thread at root\n"); | 
|  | 2168 | return; | 
|  | 2169 | } | 
|  | 2170 | t = next; | 
|  | 2171 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | 
|  | 2172 | "reply failed, no target thread -- retry %d\n", | 
|  | 2173 | t->debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2174 | } | 
|  | 2175 | } | 
|  | 2176 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2177 | /** | 
| Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 2178 | * binder_cleanup_transaction() - cleans up undelivered transaction | 
|  | 2179 | * @t:		transaction that needs to be cleaned up | 
|  | 2180 | * @reason:	reason the transaction wasn't delivered | 
|  | 2181 | * @error_code:	error to return to caller (if synchronous call) | 
|  | 2182 | */ | 
|  | 2183 | static void binder_cleanup_transaction(struct binder_transaction *t, | 
|  | 2184 | const char *reason, | 
|  | 2185 | uint32_t error_code) | 
|  | 2186 | { | 
|  | 2187 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { | 
|  | 2188 | binder_send_failed_reply(t, error_code); | 
|  | 2189 | } else { | 
|  | 2190 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | 
|  | 2191 | "undelivered transaction %d, %s\n", | 
|  | 2192 | t->debug_id, reason); | 
|  | 2193 | binder_free_transaction(t); | 
|  | 2194 | } | 
|  | 2195 | } | 
|  | 2196 |  | 
|  | 2197 | /** | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2198 | * binder_validate_object() - checks for a valid metadata object in a buffer. | 
|  | 2199 | * @buffer:	binder_buffer that we're parsing. | 
|  | 2200 | * @offset:	offset in the buffer at which to validate an object. | 
|  | 2201 | * | 
|  | 2202 | * Return:	If there's a valid metadata object at @offset in @buffer, the | 
|  | 2203 | *		size of that object. Otherwise, it returns zero. | 
|  | 2204 | */ | 
|  | 2205 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) | 
|  | 2206 | { | 
|  | 2207 | /* Check if we can read a header first */ | 
|  | 2208 | struct binder_object_header *hdr; | 
|  | 2209 | size_t object_size = 0; | 
|  | 2210 |  | 
|  | 2211 | if (offset > buffer->data_size - sizeof(*hdr) || | 
|  | 2212 | buffer->data_size < sizeof(*hdr) || | 
|  | 2213 | !IS_ALIGNED(offset, sizeof(u32))) | 
|  | 2214 | return 0; | 
|  | 2215 |  | 
|  | 2216 | /* Ok, now see if we can read a complete object. */ | 
|  | 2217 | hdr = (struct binder_object_header *)(buffer->data + offset); | 
|  | 2218 | switch (hdr->type) { | 
|  | 2219 | case BINDER_TYPE_BINDER: | 
|  | 2220 | case BINDER_TYPE_WEAK_BINDER: | 
|  | 2221 | case BINDER_TYPE_HANDLE: | 
|  | 2222 | case BINDER_TYPE_WEAK_HANDLE: | 
|  | 2223 | object_size = sizeof(struct flat_binder_object); | 
|  | 2224 | break; | 
|  | 2225 | case BINDER_TYPE_FD: | 
|  | 2226 | object_size = sizeof(struct binder_fd_object); | 
|  | 2227 | break; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2228 | case BINDER_TYPE_PTR: | 
|  | 2229 | object_size = sizeof(struct binder_buffer_object); | 
|  | 2230 | break; | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2231 | case BINDER_TYPE_FDA: | 
|  | 2232 | object_size = sizeof(struct binder_fd_array_object); | 
|  | 2233 | break; | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2234 | default: | 
|  | 2235 | return 0; | 
|  | 2236 | } | 
|  | 2237 | if (offset <= buffer->data_size - object_size && | 
|  | 2238 | buffer->data_size >= object_size) | 
|  | 2239 | return object_size; | 
|  | 2240 | else | 
|  | 2241 | return 0; | 
|  | 2242 | } | 
|  | 2243 |  | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2244 | /** | 
|  | 2245 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. | 
|  | 2246 | * @b:		binder_buffer containing the object | 
|  | 2247 | * @index:	index in offset array at which the binder_buffer_object is | 
|  | 2248 | *		located | 
|  | 2249 | * @start:	points to the start of the offset array | 
|  | 2250 | * @num_valid:	the number of valid offsets in the offset array | 
|  | 2251 | * | 
|  | 2252 | * Return:	If @index is within the valid range of the offset array | 
|  | 2253 | *		described by @start and @num_valid, and if there's a valid | 
|  | 2254 | *		binder_buffer_object at the offset found in index @index | 
|  | 2255 | *		of the offset array, that object is returned. Otherwise, | 
|  | 2256 | *		%NULL is returned. | 
|  | 2257 | *		Note that the offset found in index @index itself is not | 
|  | 2258 | *		verified; this function assumes that @num_valid elements | 
|  | 2259 | *		from @start were previously verified to have valid offsets. | 
|  | 2260 | */ | 
|  | 2261 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, | 
|  | 2262 | binder_size_t index, | 
|  | 2263 | binder_size_t *start, | 
|  | 2264 | binder_size_t num_valid) | 
|  | 2265 | { | 
|  | 2266 | struct binder_buffer_object *buffer_obj; | 
|  | 2267 | binder_size_t *offp; | 
|  | 2268 |  | 
|  | 2269 | if (index >= num_valid) | 
|  | 2270 | return NULL; | 
|  | 2271 |  | 
|  | 2272 | offp = start + index; | 
|  | 2273 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); | 
|  | 2274 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) | 
|  | 2275 | return NULL; | 
|  | 2276 |  | 
|  | 2277 | return buffer_obj; | 
|  | 2278 | } | 
|  | 2279 |  | 
|  | 2280 | /** | 
|  | 2281 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. | 
|  | 2282 | * @b:			transaction buffer | 
|  | 2283 | * @objects_start	start of objects buffer | 
|  | 2284 | * @buffer:		binder_buffer_object in which to fix up | 
|  | 2285 | * @offset:		start offset in @buffer to fix up | 
|  | 2286 | * @last_obj:		last binder_buffer_object that we fixed up in | 
|  | 2287 | * @last_min_offset:	minimum fixup offset in @last_obj | 
|  | 2288 | * | 
|  | 2289 | * Return:		%true if a fixup in buffer @buffer at offset @offset is | 
|  | 2290 | *			allowed. | 
|  | 2291 | * | 
|  | 2292 | * For safety reasons, we only allow fixups inside a buffer to happen | 
|  | 2293 | * at increasing offsets; additionally, we only allow fixup on the last | 
|  | 2294 | * buffer object that was verified, or one of its parents. | 
|  | 2295 | * | 
|  | 2296 | * Example of what is allowed: | 
|  | 2297 | * | 
|  | 2298 | * A | 
|  | 2299 | *   B (parent = A, offset = 0) | 
|  | 2300 | *   C (parent = A, offset = 16) | 
|  | 2301 | *     D (parent = C, offset = 0) | 
|  | 2302 | *   E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) | 
|  | 2303 | * | 
|  | 2304 | * Examples of what is not allowed: | 
|  | 2305 | * | 
|  | 2306 | * Decreasing offsets within the same parent: | 
|  | 2307 | * A | 
|  | 2308 | *   C (parent = A, offset = 16) | 
|  | 2309 | *   B (parent = A, offset = 0) // decreasing offset within A | 
|  | 2310 | * | 
|  | 2311 | * Referring to a parent that wasn't the last object or any of its parents: | 
|  | 2312 | * A | 
|  | 2313 | *   B (parent = A, offset = 0) | 
|  | 2314 | *   C (parent = A, offset = 0) | 
|  | 2315 | *   C (parent = A, offset = 16) | 
|  | 2316 | *     D (parent = B, offset = 0) // B is not A or any of A's parents | 
|  | 2317 | */ | 
|  | 2318 | static bool binder_validate_fixup(struct binder_buffer *b, | 
|  | 2319 | binder_size_t *objects_start, | 
|  | 2320 | struct binder_buffer_object *buffer, | 
|  | 2321 | binder_size_t fixup_offset, | 
|  | 2322 | struct binder_buffer_object *last_obj, | 
|  | 2323 | binder_size_t last_min_offset) | 
|  | 2324 | { | 
|  | 2325 | if (!last_obj) { | 
|  | 2326 | /* Nothing to fix up in */ | 
|  | 2327 | return false; | 
|  | 2328 | } | 
|  | 2329 |  | 
|  | 2330 | while (last_obj != buffer) { | 
|  | 2331 | /* | 
|  | 2332 | * Safe to retrieve the parent of last_obj, since it | 
|  | 2333 | * was already previously verified by the driver. | 
|  | 2334 | */ | 
|  | 2335 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) | 
|  | 2336 | return false; | 
|  | 2337 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); | 
|  | 2338 | last_obj = (struct binder_buffer_object *) | 
|  | 2339 | (b->data + *(objects_start + last_obj->parent)); | 
|  | 2340 | } | 
|  | 2341 | return (fixup_offset >= last_min_offset); | 
|  | 2342 | } | 
|  | 2343 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2344 | static void binder_transaction_buffer_release(struct binder_proc *proc, | 
|  | 2345 | struct binder_buffer *buffer, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2346 | binder_size_t *failed_at) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2347 | { | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2348 | binder_size_t *offp, *off_start, *off_end; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2349 | int debug_id = buffer->debug_id; | 
|  | 2350 |  | 
|  | 2351 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2352 | "%d buffer release %d, size %zd-%zd, failed at %p\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2353 | proc->pid, buffer->debug_id, | 
|  | 2354 | buffer->data_size, buffer->offsets_size, failed_at); | 
|  | 2355 |  | 
|  | 2356 | if (buffer->target_node) | 
|  | 2357 | binder_dec_node(buffer->target_node, 1, 0); | 
|  | 2358 |  | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2359 | off_start = (binder_size_t *)(buffer->data + | 
|  | 2360 | ALIGN(buffer->data_size, sizeof(void *))); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2361 | if (failed_at) | 
|  | 2362 | off_end = failed_at; | 
|  | 2363 | else | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2364 | off_end = (void *)off_start + buffer->offsets_size; | 
|  | 2365 | for (offp = off_start; offp < off_end; offp++) { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2366 | struct binder_object_header *hdr; | 
|  | 2367 | size_t object_size = binder_validate_object(buffer, *offp); | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2368 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2369 | if (object_size == 0) { | 
|  | 2370 | pr_err("transaction release %d bad object at offset %lld, size %zd\n", | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2371 | debug_id, (u64)*offp, buffer->data_size); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2372 | continue; | 
|  | 2373 | } | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2374 | hdr = (struct binder_object_header *)(buffer->data + *offp); | 
|  | 2375 | switch (hdr->type) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2376 | case BINDER_TYPE_BINDER: | 
|  | 2377 | case BINDER_TYPE_WEAK_BINDER: { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2378 | struct flat_binder_object *fp; | 
|  | 2379 | struct binder_node *node; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2380 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2381 | fp = to_flat_binder_object(hdr); | 
|  | 2382 | node = binder_get_node(proc, fp->binder); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2383 | if (node == NULL) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2384 | pr_err("transaction release %d bad node %016llx\n", | 
|  | 2385 | debug_id, (u64)fp->binder); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2386 | break; | 
|  | 2387 | } | 
|  | 2388 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2389 | "        node %d u%016llx\n", | 
|  | 2390 | node->debug_id, (u64)node->ptr); | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2391 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, | 
|  | 2392 | 0); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2393 | binder_put_node(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2394 | } break; | 
|  | 2395 | case BINDER_TYPE_HANDLE: | 
|  | 2396 | case BINDER_TYPE_WEAK_HANDLE: { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2397 | struct flat_binder_object *fp; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2398 | struct binder_ref_data rdata; | 
|  | 2399 | int ret; | 
| Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2400 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2401 | fp = to_flat_binder_object(hdr); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2402 | ret = binder_dec_ref_for_handle(proc, fp->handle, | 
|  | 2403 | hdr->type == BINDER_TYPE_HANDLE, &rdata); | 
|  | 2404 |  | 
|  | 2405 | if (ret) { | 
|  | 2406 | pr_err("transaction release %d bad handle %d, ret = %d\n", | 
|  | 2407 | debug_id, fp->handle, ret); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2408 | break; | 
|  | 2409 | } | 
|  | 2410 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2411 | "        ref %d desc %d\n", | 
|  | 2412 | rdata.debug_id, rdata.desc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2413 | } break; | 
|  | 2414 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2415 | case BINDER_TYPE_FD: { | 
|  | 2416 | struct binder_fd_object *fp = to_binder_fd_object(hdr); | 
|  | 2417 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2418 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2419 | "        fd %d\n", fp->fd); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2420 | if (failed_at) | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2421 | task_close_fd(proc, fp->fd); | 
|  | 2422 | } break; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2423 | case BINDER_TYPE_PTR: | 
|  | 2424 | /* | 
|  | 2425 | * Nothing to do here, this will get cleaned up when the | 
|  | 2426 | * transaction buffer gets freed | 
|  | 2427 | */ | 
|  | 2428 | break; | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2429 | case BINDER_TYPE_FDA: { | 
|  | 2430 | struct binder_fd_array_object *fda; | 
|  | 2431 | struct binder_buffer_object *parent; | 
|  | 2432 | uintptr_t parent_buffer; | 
|  | 2433 | u32 *fd_array; | 
|  | 2434 | size_t fd_index; | 
|  | 2435 | binder_size_t fd_buf_size; | 
|  | 2436 |  | 
|  | 2437 | fda = to_binder_fd_array_object(hdr); | 
|  | 2438 | parent = binder_validate_ptr(buffer, fda->parent, | 
|  | 2439 | off_start, | 
|  | 2440 | offp - off_start); | 
|  | 2441 | if (!parent) { | 
|  | 2442 | pr_err("transaction release %d bad parent offset", | 
|  | 2443 | debug_id); | 
|  | 2444 | continue; | 
|  | 2445 | } | 
|  | 2446 | /* | 
|  | 2447 | * Since the parent was already fixed up, convert it | 
|  | 2448 | * back to kernel address space to access it | 
|  | 2449 | */ | 
|  | 2450 | parent_buffer = parent->buffer - | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2451 | binder_alloc_get_user_buffer_offset( | 
|  | 2452 | &proc->alloc); | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2453 |  | 
|  | 2454 | fd_buf_size = sizeof(u32) * fda->num_fds; | 
|  | 2455 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { | 
|  | 2456 | pr_err("transaction release %d invalid number of fds (%lld)\n", | 
|  | 2457 | debug_id, (u64)fda->num_fds); | 
|  | 2458 | continue; | 
|  | 2459 | } | 
|  | 2460 | if (fd_buf_size > parent->length || | 
|  | 2461 | fda->parent_offset > parent->length - fd_buf_size) { | 
|  | 2462 | /* No space for all file descriptors here. */ | 
|  | 2463 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", | 
|  | 2464 | debug_id, (u64)fda->num_fds); | 
|  | 2465 | continue; | 
|  | 2466 | } | 
| Arnd Bergmann | e312c3f | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2467 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2468 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) | 
|  | 2469 | task_close_fd(proc, fd_array[fd_index]); | 
|  | 2470 | } break; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2471 | default: | 
| Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2472 | pr_err("transaction release %d bad object type %x\n", | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2473 | debug_id, hdr->type); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2474 | break; | 
|  | 2475 | } | 
|  | 2476 | } | 
|  | 2477 | } | 
|  | 2478 |  | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2479 | static int binder_translate_binder(struct flat_binder_object *fp, | 
|  | 2480 | struct binder_transaction *t, | 
|  | 2481 | struct binder_thread *thread) | 
|  | 2482 | { | 
|  | 2483 | struct binder_node *node; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2484 | struct binder_proc *proc = thread->proc; | 
|  | 2485 | struct binder_proc *target_proc = t->to_proc; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2486 | struct binder_ref_data rdata; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2487 | int ret = 0; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2488 |  | 
|  | 2489 | node = binder_get_node(proc, fp->binder); | 
|  | 2490 | if (!node) { | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2491 | node = binder_new_node(proc, fp); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2492 | if (!node) | 
|  | 2493 | return -ENOMEM; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2494 | } | 
|  | 2495 | if (fp->cookie != node->cookie) { | 
|  | 2496 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", | 
|  | 2497 | proc->pid, thread->pid, (u64)fp->binder, | 
|  | 2498 | node->debug_id, (u64)fp->cookie, | 
|  | 2499 | (u64)node->cookie); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2500 | ret = -EINVAL; | 
|  | 2501 | goto done; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2502 | } | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2503 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { | 
|  | 2504 | ret = -EPERM; | 
|  | 2505 | goto done; | 
|  | 2506 | } | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2507 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2508 | ret = binder_inc_ref_for_node(target_proc, node, | 
|  | 2509 | fp->hdr.type == BINDER_TYPE_BINDER, | 
|  | 2510 | &thread->todo, &rdata); | 
|  | 2511 | if (ret) | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2512 | goto done; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2513 |  | 
|  | 2514 | if (fp->hdr.type == BINDER_TYPE_BINDER) | 
|  | 2515 | fp->hdr.type = BINDER_TYPE_HANDLE; | 
|  | 2516 | else | 
|  | 2517 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; | 
|  | 2518 | fp->binder = 0; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2519 | fp->handle = rdata.desc; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2520 | fp->cookie = 0; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2521 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2522 | trace_binder_transaction_node_to_ref(t, node, &rdata); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2523 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
|  | 2524 | "        node %d u%016llx -> ref %d desc %d\n", | 
|  | 2525 | node->debug_id, (u64)node->ptr, | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2526 | rdata.debug_id, rdata.desc); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2527 | done: | 
|  | 2528 | binder_put_node(node); | 
|  | 2529 | return ret; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2530 | } | 
|  | 2531 |  | 
|  | 2532 | static int binder_translate_handle(struct flat_binder_object *fp, | 
|  | 2533 | struct binder_transaction *t, | 
|  | 2534 | struct binder_thread *thread) | 
|  | 2535 | { | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2536 | struct binder_proc *proc = thread->proc; | 
|  | 2537 | struct binder_proc *target_proc = t->to_proc; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2538 | struct binder_node *node; | 
|  | 2539 | struct binder_ref_data src_rdata; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2540 | int ret = 0; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2541 |  | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2542 | node = binder_get_node_from_ref(proc, fp->handle, | 
|  | 2543 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); | 
|  | 2544 | if (!node) { | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2545 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", | 
|  | 2546 | proc->pid, thread->pid, fp->handle); | 
|  | 2547 | return -EINVAL; | 
|  | 2548 | } | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2549 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { | 
|  | 2550 | ret = -EPERM; | 
|  | 2551 | goto done; | 
|  | 2552 | } | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2553 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2554 | binder_node_lock(node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2555 | if (node->proc == target_proc) { | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2556 | if (fp->hdr.type == BINDER_TYPE_HANDLE) | 
|  | 2557 | fp->hdr.type = BINDER_TYPE_BINDER; | 
|  | 2558 | else | 
|  | 2559 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2560 | fp->binder = node->ptr; | 
|  | 2561 | fp->cookie = node->cookie; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2562 | if (node->proc) | 
|  | 2563 | binder_inner_proc_lock(node->proc); | 
|  | 2564 | binder_inc_node_nilocked(node, | 
|  | 2565 | fp->hdr.type == BINDER_TYPE_BINDER, | 
|  | 2566 | 0, NULL); | 
|  | 2567 | if (node->proc) | 
|  | 2568 | binder_inner_proc_unlock(node->proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2569 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2570 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
|  | 2571 | "        ref %d desc %d -> node %d u%016llx\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2572 | src_rdata.debug_id, src_rdata.desc, node->debug_id, | 
|  | 2573 | (u64)node->ptr); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2574 | binder_node_unlock(node); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2575 | } else { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2576 | struct binder_ref_data dest_rdata; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2577 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2578 | binder_node_unlock(node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2579 | ret = binder_inc_ref_for_node(target_proc, node, | 
|  | 2580 | fp->hdr.type == BINDER_TYPE_HANDLE, | 
|  | 2581 | NULL, &dest_rdata); | 
|  | 2582 | if (ret) | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2583 | goto done; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2584 |  | 
|  | 2585 | fp->binder = 0; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2586 | fp->handle = dest_rdata.desc; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2587 | fp->cookie = 0; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2588 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, | 
|  | 2589 | &dest_rdata); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2590 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
|  | 2591 | "        ref %d desc %d -> ref %d desc %d (node %d)\n", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2592 | src_rdata.debug_id, src_rdata.desc, | 
|  | 2593 | dest_rdata.debug_id, dest_rdata.desc, | 
|  | 2594 | node->debug_id); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2595 | } | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2596 | done: | 
|  | 2597 | binder_put_node(node); | 
|  | 2598 | return ret; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2599 | } | 
|  | 2600 |  | 
|  | 2601 | static int binder_translate_fd(int fd, | 
|  | 2602 | struct binder_transaction *t, | 
|  | 2603 | struct binder_thread *thread, | 
|  | 2604 | struct binder_transaction *in_reply_to) | 
|  | 2605 | { | 
|  | 2606 | struct binder_proc *proc = thread->proc; | 
|  | 2607 | struct binder_proc *target_proc = t->to_proc; | 
|  | 2608 | int target_fd; | 
|  | 2609 | struct file *file; | 
|  | 2610 | int ret; | 
|  | 2611 | bool target_allows_fd; | 
|  | 2612 |  | 
|  | 2613 | if (in_reply_to) | 
|  | 2614 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); | 
|  | 2615 | else | 
|  | 2616 | target_allows_fd = t->buffer->target_node->accept_fds; | 
|  | 2617 | if (!target_allows_fd) { | 
|  | 2618 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", | 
|  | 2619 | proc->pid, thread->pid, | 
|  | 2620 | in_reply_to ? "reply" : "transaction", | 
|  | 2621 | fd); | 
|  | 2622 | ret = -EPERM; | 
|  | 2623 | goto err_fd_not_accepted; | 
|  | 2624 | } | 
|  | 2625 |  | 
|  | 2626 | file = fget(fd); | 
|  | 2627 | if (!file) { | 
|  | 2628 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", | 
|  | 2629 | proc->pid, thread->pid, fd); | 
|  | 2630 | ret = -EBADF; | 
|  | 2631 | goto err_fget; | 
|  | 2632 | } | 
|  | 2633 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); | 
|  | 2634 | if (ret < 0) { | 
|  | 2635 | ret = -EPERM; | 
|  | 2636 | goto err_security; | 
|  | 2637 | } | 
|  | 2638 |  | 
|  | 2639 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); | 
|  | 2640 | if (target_fd < 0) { | 
|  | 2641 | ret = -ENOMEM; | 
|  | 2642 | goto err_get_unused_fd; | 
|  | 2643 | } | 
|  | 2644 | task_fd_install(target_proc, target_fd, file); | 
|  | 2645 | trace_binder_transaction_fd(t, fd, target_fd); | 
|  | 2646 | binder_debug(BINDER_DEBUG_TRANSACTION, "        fd %d -> %d\n", | 
|  | 2647 | fd, target_fd); | 
|  | 2648 |  | 
|  | 2649 | return target_fd; | 
|  | 2650 |  | 
|  | 2651 | err_get_unused_fd: | 
|  | 2652 | err_security: | 
|  | 2653 | fput(file); | 
|  | 2654 | err_fget: | 
|  | 2655 | err_fd_not_accepted: | 
|  | 2656 | return ret; | 
|  | 2657 | } | 
|  | 2658 |  | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2659 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, | 
|  | 2660 | struct binder_buffer_object *parent, | 
|  | 2661 | struct binder_transaction *t, | 
|  | 2662 | struct binder_thread *thread, | 
|  | 2663 | struct binder_transaction *in_reply_to) | 
|  | 2664 | { | 
|  | 2665 | binder_size_t fdi, fd_buf_size, num_installed_fds; | 
|  | 2666 | int target_fd; | 
|  | 2667 | uintptr_t parent_buffer; | 
|  | 2668 | u32 *fd_array; | 
|  | 2669 | struct binder_proc *proc = thread->proc; | 
|  | 2670 | struct binder_proc *target_proc = t->to_proc; | 
|  | 2671 |  | 
|  | 2672 | fd_buf_size = sizeof(u32) * fda->num_fds; | 
|  | 2673 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { | 
|  | 2674 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", | 
|  | 2675 | proc->pid, thread->pid, (u64)fda->num_fds); | 
|  | 2676 | return -EINVAL; | 
|  | 2677 | } | 
|  | 2678 | if (fd_buf_size > parent->length || | 
|  | 2679 | fda->parent_offset > parent->length - fd_buf_size) { | 
|  | 2680 | /* No space for all file descriptors here. */ | 
|  | 2681 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", | 
|  | 2682 | proc->pid, thread->pid, (u64)fda->num_fds); | 
|  | 2683 | return -EINVAL; | 
|  | 2684 | } | 
|  | 2685 | /* | 
|  | 2686 | * Since the parent was already fixed up, convert it | 
|  | 2687 | * back to the kernel address space to access it | 
|  | 2688 | */ | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2689 | parent_buffer = parent->buffer - | 
|  | 2690 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); | 
| Arnd Bergmann | e312c3f | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2691 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2692 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { | 
|  | 2693 | binder_user_error("%d:%d parent offset not aligned correctly.\n", | 
|  | 2694 | proc->pid, thread->pid); | 
|  | 2695 | return -EINVAL; | 
|  | 2696 | } | 
|  | 2697 | for (fdi = 0; fdi < fda->num_fds; fdi++) { | 
|  | 2698 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, | 
|  | 2699 | in_reply_to); | 
|  | 2700 | if (target_fd < 0) | 
|  | 2701 | goto err_translate_fd_failed; | 
|  | 2702 | fd_array[fdi] = target_fd; | 
|  | 2703 | } | 
|  | 2704 | return 0; | 
|  | 2705 |  | 
|  | 2706 | err_translate_fd_failed: | 
|  | 2707 | /* | 
|  | 2708 | * Failed to allocate fd or security error, free fds | 
|  | 2709 | * installed so far. | 
|  | 2710 | */ | 
|  | 2711 | num_installed_fds = fdi; | 
|  | 2712 | for (fdi = 0; fdi < num_installed_fds; fdi++) | 
|  | 2713 | task_close_fd(target_proc, fd_array[fdi]); | 
|  | 2714 | return target_fd; | 
|  | 2715 | } | 
|  | 2716 |  | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2717 | static int binder_fixup_parent(struct binder_transaction *t, | 
|  | 2718 | struct binder_thread *thread, | 
|  | 2719 | struct binder_buffer_object *bp, | 
|  | 2720 | binder_size_t *off_start, | 
|  | 2721 | binder_size_t num_valid, | 
|  | 2722 | struct binder_buffer_object *last_fixup_obj, | 
|  | 2723 | binder_size_t last_fixup_min_off) | 
|  | 2724 | { | 
|  | 2725 | struct binder_buffer_object *parent; | 
|  | 2726 | u8 *parent_buffer; | 
|  | 2727 | struct binder_buffer *b = t->buffer; | 
|  | 2728 | struct binder_proc *proc = thread->proc; | 
|  | 2729 | struct binder_proc *target_proc = t->to_proc; | 
|  | 2730 |  | 
|  | 2731 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) | 
|  | 2732 | return 0; | 
|  | 2733 |  | 
|  | 2734 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); | 
|  | 2735 | if (!parent) { | 
|  | 2736 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", | 
|  | 2737 | proc->pid, thread->pid); | 
|  | 2738 | return -EINVAL; | 
|  | 2739 | } | 
|  | 2740 |  | 
|  | 2741 | if (!binder_validate_fixup(b, off_start, | 
|  | 2742 | parent, bp->parent_offset, | 
|  | 2743 | last_fixup_obj, | 
|  | 2744 | last_fixup_min_off)) { | 
|  | 2745 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", | 
|  | 2746 | proc->pid, thread->pid); | 
|  | 2747 | return -EINVAL; | 
|  | 2748 | } | 
|  | 2749 |  | 
|  | 2750 | if (parent->length < sizeof(binder_uintptr_t) || | 
|  | 2751 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { | 
|  | 2752 | /* No space for a pointer here! */ | 
|  | 2753 | binder_user_error("%d:%d got transaction with invalid parent offset\n", | 
|  | 2754 | proc->pid, thread->pid); | 
|  | 2755 | return -EINVAL; | 
|  | 2756 | } | 
| Arnd Bergmann | e312c3f | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2757 | parent_buffer = (u8 *)((uintptr_t)parent->buffer - | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2758 | binder_alloc_get_user_buffer_offset( | 
|  | 2759 | &target_proc->alloc)); | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2760 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; | 
|  | 2761 |  | 
|  | 2762 | return 0; | 
|  | 2763 | } | 
|  | 2764 |  | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2765 | /** | 
|  | 2766 | * binder_proc_transaction() - sends a transaction to a process and wakes it up | 
|  | 2767 | * @t:		transaction to send | 
|  | 2768 | * @proc:	process to send the transaction to | 
|  | 2769 | * @thread:	thread in @proc to send the transaction to (may be NULL) | 
|  | 2770 | * | 
|  | 2771 | * This function queues a transaction to the specified process. It will try | 
|  | 2772 | * to find a thread in the target process to handle the transaction and | 
|  | 2773 | * wake it up. If no thread is found, the work is queued to the proc | 
|  | 2774 | * waitqueue. | 
|  | 2775 | * | 
|  | 2776 | * If the @thread parameter is not NULL, the transaction is always queued | 
|  | 2777 | * to the waitlist of that specific thread. | 
|  | 2778 | * | 
|  | 2779 | * Return:	true if the transactions was successfully queued | 
|  | 2780 | *		false if the target process or thread is dead | 
|  | 2781 | */ | 
|  | 2782 | static bool binder_proc_transaction(struct binder_transaction *t, | 
|  | 2783 | struct binder_proc *proc, | 
|  | 2784 | struct binder_thread *thread) | 
|  | 2785 | { | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2786 | struct binder_node *node = t->buffer->target_node; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2787 | struct binder_priority node_prio; | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2788 | bool oneway = !!(t->flags & TF_ONE_WAY); | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2789 | bool pending_async = false; | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2790 |  | 
|  | 2791 | BUG_ON(!node); | 
|  | 2792 | binder_node_lock(node); | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2793 | node_prio.prio = node->min_priority; | 
|  | 2794 | node_prio.sched_policy = node->sched_policy; | 
|  | 2795 |  | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2796 | if (oneway) { | 
|  | 2797 | BUG_ON(thread); | 
|  | 2798 | if (node->has_async_transaction) { | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2799 | pending_async = true; | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2800 | } else { | 
|  | 2801 | node->has_async_transaction = 1; | 
|  | 2802 | } | 
|  | 2803 | } | 
|  | 2804 |  | 
|  | 2805 | binder_inner_proc_lock(proc); | 
|  | 2806 |  | 
|  | 2807 | if (proc->is_dead || (thread && thread->is_dead)) { | 
|  | 2808 | binder_inner_proc_unlock(proc); | 
|  | 2809 | binder_node_unlock(node); | 
|  | 2810 | return false; | 
|  | 2811 | } | 
|  | 2812 |  | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2813 | if (!thread && !pending_async) | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2814 | thread = binder_select_thread_ilocked(proc); | 
|  | 2815 |  | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2816 | if (thread) { | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 2817 | binder_transaction_priority(thread->task, t, node_prio, | 
|  | 2818 | node->inherit_rt); | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2819 | binder_enqueue_thread_work_ilocked(thread, &t->work); | 
|  | 2820 | } else if (!pending_async) { | 
|  | 2821 | binder_enqueue_work_ilocked(&t->work, &proc->todo); | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2822 | } else { | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2823 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2824 | } | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2825 |  | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2826 | if (!pending_async) | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2827 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); | 
|  | 2828 |  | 
|  | 2829 | binder_inner_proc_unlock(proc); | 
|  | 2830 | binder_node_unlock(node); | 
|  | 2831 |  | 
|  | 2832 | return true; | 
|  | 2833 | } | 
|  | 2834 |  | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2835 | /** | 
|  | 2836 | * binder_get_node_refs_for_txn() - Get required refs on node for txn | 
|  | 2837 | * @node:         struct binder_node for which to get refs | 
|  | 2838 | * @proc:         returns @node->proc if valid | 
|  | 2839 | * @error:        if no @proc then returns BR_DEAD_REPLY | 
|  | 2840 | * | 
|  | 2841 | * User-space normally keeps the node alive when creating a transaction | 
|  | 2842 | * since it has a reference to the target. The local strong ref keeps it | 
|  | 2843 | * alive if the sending process dies before the target process processes | 
|  | 2844 | * the transaction. If the source process is malicious or has a reference | 
|  | 2845 | * counting bug, relying on the local strong ref can fail. | 
|  | 2846 | * | 
|  | 2847 | * Since user-space can cause the local strong ref to go away, we also take | 
|  | 2848 | * a tmpref on the node to ensure it survives while we are constructing | 
|  | 2849 | * the transaction. We also need a tmpref on the proc while we are | 
|  | 2850 | * constructing the transaction, so we take that here as well. | 
|  | 2851 | * | 
|  | 2852 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. | 
|  | 2853 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the | 
|  | 2854 | * target proc has died, @error is set to BR_DEAD_REPLY | 
|  | 2855 | */ | 
|  | 2856 | static struct binder_node *binder_get_node_refs_for_txn( | 
|  | 2857 | struct binder_node *node, | 
|  | 2858 | struct binder_proc **procp, | 
|  | 2859 | uint32_t *error) | 
|  | 2860 | { | 
|  | 2861 | struct binder_node *target_node = NULL; | 
|  | 2862 |  | 
|  | 2863 | binder_node_inner_lock(node); | 
|  | 2864 | if (node->proc) { | 
|  | 2865 | target_node = node; | 
|  | 2866 | binder_inc_node_nilocked(node, 1, 0, NULL); | 
|  | 2867 | binder_inc_node_tmpref_ilocked(node); | 
|  | 2868 | node->proc->tmp_ref++; | 
|  | 2869 | *procp = node->proc; | 
|  | 2870 | } else | 
|  | 2871 | *error = BR_DEAD_REPLY; | 
|  | 2872 | binder_node_inner_unlock(node); | 
|  | 2873 |  | 
|  | 2874 | return target_node; | 
|  | 2875 | } | 
|  | 2876 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2877 | static void binder_transaction(struct binder_proc *proc, | 
|  | 2878 | struct binder_thread *thread, | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2879 | struct binder_transaction_data *tr, int reply, | 
|  | 2880 | binder_size_t extra_buffers_size) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2881 | { | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2882 | int ret; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2883 | struct binder_transaction *t; | 
|  | 2884 | struct binder_work *tcomplete; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2885 | binder_size_t *offp, *off_end, *off_start; | 
| Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2886 | binder_size_t off_min; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2887 | u8 *sg_bufp, *sg_buf_end; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2888 | struct binder_proc *target_proc = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2889 | struct binder_thread *target_thread = NULL; | 
|  | 2890 | struct binder_node *target_node = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2891 | struct binder_transaction *in_reply_to = NULL; | 
|  | 2892 | struct binder_transaction_log_entry *e; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2893 | uint32_t return_error = 0; | 
|  | 2894 | uint32_t return_error_param = 0; | 
|  | 2895 | uint32_t return_error_line = 0; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2896 | struct binder_buffer_object *last_fixup_obj = NULL; | 
|  | 2897 | binder_size_t last_fixup_min_off = 0; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 2898 | struct binder_context *context = proc->context; | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2899 | int t_debug_id = atomic_inc_return(&binder_last_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2900 |  | 
|  | 2901 | e = binder_transaction_log_add(&binder_transaction_log); | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2902 | e->debug_id = t_debug_id; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2903 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); | 
|  | 2904 | e->from_proc = proc->pid; | 
|  | 2905 | e->from_thread = thread->pid; | 
|  | 2906 | e->target_handle = tr->target.handle; | 
|  | 2907 | e->data_size = tr->data_size; | 
|  | 2908 | e->offsets_size = tr->offsets_size; | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 2909 | e->context_name = proc->context->name; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2910 |  | 
|  | 2911 | if (reply) { | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2912 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2913 | in_reply_to = thread->transaction_stack; | 
|  | 2914 | if (in_reply_to == NULL) { | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2915 | binder_inner_proc_unlock(proc); | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2916 | binder_user_error("%d:%d got reply transaction with no transaction stack\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2917 | proc->pid, thread->pid); | 
|  | 2918 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2919 | return_error_param = -EPROTO; | 
|  | 2920 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2921 | goto err_empty_call_stack; | 
|  | 2922 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2923 | if (in_reply_to->to_thread != thread) { | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2924 | spin_lock(&in_reply_to->lock); | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2925 | binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2926 | proc->pid, thread->pid, in_reply_to->debug_id, | 
|  | 2927 | in_reply_to->to_proc ? | 
|  | 2928 | in_reply_to->to_proc->pid : 0, | 
|  | 2929 | in_reply_to->to_thread ? | 
|  | 2930 | in_reply_to->to_thread->pid : 0); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2931 | spin_unlock(&in_reply_to->lock); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2932 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2933 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2934 | return_error_param = -EPROTO; | 
|  | 2935 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2936 | in_reply_to = NULL; | 
|  | 2937 | goto err_bad_call_stack; | 
|  | 2938 | } | 
|  | 2939 | thread->transaction_stack = in_reply_to->to_parent; | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2940 | binder_inner_proc_unlock(proc); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2941 | target_thread = binder_get_txn_from_and_acq_inner(in_reply_to); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2942 | if (target_thread == NULL) { | 
|  | 2943 | return_error = BR_DEAD_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2944 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2945 | goto err_dead_binder; | 
|  | 2946 | } | 
|  | 2947 | if (target_thread->transaction_stack != in_reply_to) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2948 | binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2949 | proc->pid, thread->pid, | 
|  | 2950 | target_thread->transaction_stack ? | 
|  | 2951 | target_thread->transaction_stack->debug_id : 0, | 
|  | 2952 | in_reply_to->debug_id); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2953 | binder_inner_proc_unlock(target_thread->proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2954 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2955 | return_error_param = -EPROTO; | 
|  | 2956 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2957 | in_reply_to = NULL; | 
|  | 2958 | target_thread = NULL; | 
|  | 2959 | goto err_dead_binder; | 
|  | 2960 | } | 
|  | 2961 | target_proc = target_thread->proc; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2962 | target_proc->tmp_ref++; | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2963 | binder_inner_proc_unlock(target_thread->proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2964 | } else { | 
|  | 2965 | if (tr->target.handle) { | 
|  | 2966 | struct binder_ref *ref; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2967 |  | 
| Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2968 | /* | 
|  | 2969 | * There must already be a strong ref | 
|  | 2970 | * on this node. If so, do a strong | 
|  | 2971 | * increment on the node to ensure it | 
|  | 2972 | * stays alive until the transaction is | 
|  | 2973 | * done. | 
|  | 2974 | */ | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2975 | binder_proc_lock(proc); | 
|  | 2976 | ref = binder_get_ref_olocked(proc, tr->target.handle, | 
|  | 2977 | true); | 
| Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2978 | if (ref) { | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2979 | target_node = binder_get_node_refs_for_txn( | 
|  | 2980 | ref->node, &target_proc, | 
|  | 2981 | &return_error); | 
|  | 2982 | } else { | 
|  | 2983 | binder_user_error("%d:%d got transaction to invalid handle\n", | 
|  | 2984 | proc->pid, thread->pid); | 
|  | 2985 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2986 | } | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2987 | binder_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2988 | } else { | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2989 | mutex_lock(&context->context_mgr_node_lock); | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 2990 | target_node = context->binder_context_mgr_node; | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2991 | if (target_node) | 
|  | 2992 | target_node = binder_get_node_refs_for_txn( | 
|  | 2993 | target_node, &target_proc, | 
|  | 2994 | &return_error); | 
|  | 2995 | else | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2996 | return_error = BR_DEAD_REPLY; | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2997 | mutex_unlock(&context->context_mgr_node_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2998 | } | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2999 | if (!target_node) { | 
|  | 3000 | /* | 
|  | 3001 | * return_error is set above | 
|  | 3002 | */ | 
|  | 3003 | return_error_param = -EINVAL; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3004 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3005 | goto err_dead_binder; | 
|  | 3006 | } | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3007 | e->to_node = target_node->debug_id; | 
| Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3008 | if (security_binder_transaction(proc->tsk, | 
|  | 3009 | target_proc->tsk) < 0) { | 
|  | 3010 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3011 | return_error_param = -EPERM; | 
|  | 3012 | return_error_line = __LINE__; | 
| Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3013 | goto err_invalid_target_handle; | 
|  | 3014 | } | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3015 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3016 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { | 
|  | 3017 | struct binder_transaction *tmp; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3018 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3019 | tmp = thread->transaction_stack; | 
|  | 3020 | if (tmp->to_thread != thread) { | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3021 | spin_lock(&tmp->lock); | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3022 | binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3023 | proc->pid, thread->pid, tmp->debug_id, | 
|  | 3024 | tmp->to_proc ? tmp->to_proc->pid : 0, | 
|  | 3025 | tmp->to_thread ? | 
|  | 3026 | tmp->to_thread->pid : 0); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3027 | spin_unlock(&tmp->lock); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3028 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3029 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3030 | return_error_param = -EPROTO; | 
|  | 3031 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3032 | goto err_bad_call_stack; | 
|  | 3033 | } | 
|  | 3034 | while (tmp) { | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3035 | struct binder_thread *from; | 
|  | 3036 |  | 
|  | 3037 | spin_lock(&tmp->lock); | 
|  | 3038 | from = tmp->from; | 
|  | 3039 | if (from && from->proc == target_proc) { | 
|  | 3040 | atomic_inc(&from->tmp_ref); | 
|  | 3041 | target_thread = from; | 
|  | 3042 | spin_unlock(&tmp->lock); | 
|  | 3043 | break; | 
|  | 3044 | } | 
|  | 3045 | spin_unlock(&tmp->lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3046 | tmp = tmp->from_parent; | 
|  | 3047 | } | 
|  | 3048 | } | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3049 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3050 | } | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3051 | if (target_thread) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3052 | e->to_thread = target_thread->pid; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3053 | e->to_proc = target_proc->pid; | 
|  | 3054 |  | 
|  | 3055 | /* TODO: reuse incoming transaction for reply */ | 
|  | 3056 | t = kzalloc(sizeof(*t), GFP_KERNEL); | 
|  | 3057 | if (t == NULL) { | 
|  | 3058 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3059 | return_error_param = -ENOMEM; | 
|  | 3060 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3061 | goto err_alloc_t_failed; | 
|  | 3062 | } | 
|  | 3063 | binder_stats_created(BINDER_STAT_TRANSACTION); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3064 | spin_lock_init(&t->lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3065 |  | 
|  | 3066 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); | 
|  | 3067 | if (tcomplete == NULL) { | 
|  | 3068 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3069 | return_error_param = -ENOMEM; | 
|  | 3070 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3071 | goto err_alloc_tcomplete_failed; | 
|  | 3072 | } | 
|  | 3073 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); | 
|  | 3074 |  | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3075 | t->debug_id = t_debug_id; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3076 |  | 
|  | 3077 | if (reply) | 
|  | 3078 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3079 | "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3080 | proc->pid, thread->pid, t->debug_id, | 
|  | 3081 | target_proc->pid, target_thread->pid, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3082 | (u64)tr->data.ptr.buffer, | 
|  | 3083 | (u64)tr->data.ptr.offsets, | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3084 | (u64)tr->data_size, (u64)tr->offsets_size, | 
|  | 3085 | (u64)extra_buffers_size); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3086 | else | 
|  | 3087 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3088 | "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3089 | proc->pid, thread->pid, t->debug_id, | 
|  | 3090 | target_proc->pid, target_node->debug_id, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3091 | (u64)tr->data.ptr.buffer, | 
|  | 3092 | (u64)tr->data.ptr.offsets, | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3093 | (u64)tr->data_size, (u64)tr->offsets_size, | 
|  | 3094 | (u64)extra_buffers_size); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3095 |  | 
|  | 3096 | if (!reply && !(tr->flags & TF_ONE_WAY)) | 
|  | 3097 | t->from = thread; | 
|  | 3098 | else | 
|  | 3099 | t->from = NULL; | 
| Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 3100 | t->sender_euid = task_euid(proc->tsk); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3101 | t->to_proc = target_proc; | 
|  | 3102 | t->to_thread = target_thread; | 
|  | 3103 | t->code = tr->code; | 
|  | 3104 | t->flags = tr->flags; | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 3105 | if (!(t->flags & TF_ONE_WAY) && | 
|  | 3106 | binder_supported_policy(current->policy)) { | 
|  | 3107 | /* Inherit supported policies for synchronous transactions */ | 
|  | 3108 | t->priority.sched_policy = current->policy; | 
|  | 3109 | t->priority.prio = current->normal_prio; | 
|  | 3110 | } else { | 
|  | 3111 | /* Otherwise, fall back to the default priority */ | 
|  | 3112 | t->priority = target_proc->default_priority; | 
|  | 3113 | } | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3114 |  | 
|  | 3115 | trace_binder_transaction(reply, t, target_node); | 
|  | 3116 |  | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3117 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3118 | tr->offsets_size, extra_buffers_size, | 
|  | 3119 | !reply && (t->flags & TF_ONE_WAY)); | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3120 | if (IS_ERR(t->buffer)) { | 
|  | 3121 | /* | 
|  | 3122 | * -ESRCH indicates VMA cleared. The target is dying. | 
|  | 3123 | */ | 
|  | 3124 | return_error_param = PTR_ERR(t->buffer); | 
|  | 3125 | return_error = return_error_param == -ESRCH ? | 
|  | 3126 | BR_DEAD_REPLY : BR_FAILED_REPLY; | 
|  | 3127 | return_error_line = __LINE__; | 
|  | 3128 | t->buffer = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3129 | goto err_binder_alloc_buf_failed; | 
|  | 3130 | } | 
|  | 3131 | t->buffer->allow_user_free = 0; | 
|  | 3132 | t->buffer->debug_id = t->debug_id; | 
|  | 3133 | t->buffer->transaction = t; | 
|  | 3134 | t->buffer->target_node = target_node; | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3135 | trace_binder_transaction_alloc_buf(t->buffer); | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3136 | off_start = (binder_size_t *)(t->buffer->data + | 
|  | 3137 | ALIGN(tr->data_size, sizeof(void *))); | 
|  | 3138 | offp = off_start; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3139 |  | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3140 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) | 
|  | 3141 | tr->data.ptr.buffer, tr->data_size)) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3142 | binder_user_error("%d:%d got transaction with invalid data ptr\n", | 
|  | 3143 | proc->pid, thread->pid); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3144 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3145 | return_error_param = -EFAULT; | 
|  | 3146 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3147 | goto err_copy_data_failed; | 
|  | 3148 | } | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3149 | if (copy_from_user(offp, (const void __user *)(uintptr_t) | 
|  | 3150 | tr->data.ptr.offsets, tr->offsets_size)) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3151 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", | 
|  | 3152 | proc->pid, thread->pid); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3153 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3154 | return_error_param = -EFAULT; | 
|  | 3155 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3156 | goto err_copy_data_failed; | 
|  | 3157 | } | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3158 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { | 
|  | 3159 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", | 
|  | 3160 | proc->pid, thread->pid, (u64)tr->offsets_size); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3161 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3162 | return_error_param = -EINVAL; | 
|  | 3163 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3164 | goto err_bad_offset; | 
|  | 3165 | } | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3166 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { | 
|  | 3167 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", | 
|  | 3168 | proc->pid, thread->pid, | 
| Amit Pundir | 44cbb18 | 2017-02-01 12:53:45 +0530 | [diff] [blame] | 3169 | (u64)extra_buffers_size); | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3170 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3171 | return_error_param = -EINVAL; | 
|  | 3172 | return_error_line = __LINE__; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3173 | goto err_bad_offset; | 
|  | 3174 | } | 
|  | 3175 | off_end = (void *)off_start + tr->offsets_size; | 
|  | 3176 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); | 
|  | 3177 | sg_buf_end = sg_bufp + extra_buffers_size; | 
| Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3178 | off_min = 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3179 | for (; offp < off_end; offp++) { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3180 | struct binder_object_header *hdr; | 
|  | 3181 | size_t object_size = binder_validate_object(t->buffer, *offp); | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3182 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3183 | if (object_size == 0 || *offp < off_min) { | 
|  | 3184 | binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n", | 
| Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3185 | proc->pid, thread->pid, (u64)*offp, | 
|  | 3186 | (u64)off_min, | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3187 | (u64)t->buffer->data_size); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3188 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3189 | return_error_param = -EINVAL; | 
|  | 3190 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3191 | goto err_bad_offset; | 
|  | 3192 | } | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3193 |  | 
|  | 3194 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); | 
|  | 3195 | off_min = *offp + object_size; | 
|  | 3196 | switch (hdr->type) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3197 | case BINDER_TYPE_BINDER: | 
|  | 3198 | case BINDER_TYPE_WEAK_BINDER: { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3199 | struct flat_binder_object *fp; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3200 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3201 | fp = to_flat_binder_object(hdr); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3202 | ret = binder_translate_binder(fp, t, thread); | 
|  | 3203 | if (ret < 0) { | 
| Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3204 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3205 | return_error_param = ret; | 
|  | 3206 | return_error_line = __LINE__; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3207 | goto err_translate_failed; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3208 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3209 | } break; | 
|  | 3210 | case BINDER_TYPE_HANDLE: | 
|  | 3211 | case BINDER_TYPE_WEAK_HANDLE: { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3212 | struct flat_binder_object *fp; | 
| Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3213 |  | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3214 | fp = to_flat_binder_object(hdr); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3215 | ret = binder_translate_handle(fp, t, thread); | 
|  | 3216 | if (ret < 0) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3217 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3218 | return_error_param = ret; | 
|  | 3219 | return_error_line = __LINE__; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3220 | goto err_translate_failed; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3221 | } | 
|  | 3222 | } break; | 
|  | 3223 |  | 
|  | 3224 | case BINDER_TYPE_FD: { | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3225 | struct binder_fd_object *fp = to_binder_fd_object(hdr); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3226 | int target_fd = binder_translate_fd(fp->fd, t, thread, | 
|  | 3227 | in_reply_to); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3228 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3229 | if (target_fd < 0) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3230 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3231 | return_error_param = target_fd; | 
|  | 3232 | return_error_line = __LINE__; | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3233 | goto err_translate_failed; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3234 | } | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3235 | fp->pad_binder = 0; | 
|  | 3236 | fp->fd = target_fd; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3237 | } break; | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3238 | case BINDER_TYPE_FDA: { | 
|  | 3239 | struct binder_fd_array_object *fda = | 
|  | 3240 | to_binder_fd_array_object(hdr); | 
|  | 3241 | struct binder_buffer_object *parent = | 
|  | 3242 | binder_validate_ptr(t->buffer, fda->parent, | 
|  | 3243 | off_start, | 
|  | 3244 | offp - off_start); | 
|  | 3245 | if (!parent) { | 
|  | 3246 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", | 
|  | 3247 | proc->pid, thread->pid); | 
|  | 3248 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3249 | return_error_param = -EINVAL; | 
|  | 3250 | return_error_line = __LINE__; | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3251 | goto err_bad_parent; | 
|  | 3252 | } | 
|  | 3253 | if (!binder_validate_fixup(t->buffer, off_start, | 
|  | 3254 | parent, fda->parent_offset, | 
|  | 3255 | last_fixup_obj, | 
|  | 3256 | last_fixup_min_off)) { | 
|  | 3257 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", | 
|  | 3258 | proc->pid, thread->pid); | 
|  | 3259 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3260 | return_error_param = -EINVAL; | 
|  | 3261 | return_error_line = __LINE__; | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3262 | goto err_bad_parent; | 
|  | 3263 | } | 
|  | 3264 | ret = binder_translate_fd_array(fda, parent, t, thread, | 
|  | 3265 | in_reply_to); | 
|  | 3266 | if (ret < 0) { | 
|  | 3267 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3268 | return_error_param = ret; | 
|  | 3269 | return_error_line = __LINE__; | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3270 | goto err_translate_failed; | 
|  | 3271 | } | 
|  | 3272 | last_fixup_obj = parent; | 
|  | 3273 | last_fixup_min_off = | 
|  | 3274 | fda->parent_offset + sizeof(u32) * fda->num_fds; | 
|  | 3275 | } break; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3276 | case BINDER_TYPE_PTR: { | 
|  | 3277 | struct binder_buffer_object *bp = | 
|  | 3278 | to_binder_buffer_object(hdr); | 
|  | 3279 | size_t buf_left = sg_buf_end - sg_bufp; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3280 |  | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3281 | if (bp->length > buf_left) { | 
|  | 3282 | binder_user_error("%d:%d got transaction with too large buffer\n", | 
|  | 3283 | proc->pid, thread->pid); | 
|  | 3284 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3285 | return_error_param = -EINVAL; | 
|  | 3286 | return_error_line = __LINE__; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3287 | goto err_bad_offset; | 
|  | 3288 | } | 
|  | 3289 | if (copy_from_user(sg_bufp, | 
|  | 3290 | (const void __user *)(uintptr_t) | 
|  | 3291 | bp->buffer, bp->length)) { | 
|  | 3292 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", | 
|  | 3293 | proc->pid, thread->pid); | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3294 | return_error_param = -EFAULT; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3295 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3296 | return_error_line = __LINE__; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3297 | goto err_copy_data_failed; | 
|  | 3298 | } | 
|  | 3299 | /* Fixup buffer pointer to target proc address space */ | 
|  | 3300 | bp->buffer = (uintptr_t)sg_bufp + | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3301 | binder_alloc_get_user_buffer_offset( | 
|  | 3302 | &target_proc->alloc); | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3303 | sg_bufp += ALIGN(bp->length, sizeof(u64)); | 
|  | 3304 |  | 
|  | 3305 | ret = binder_fixup_parent(t, thread, bp, off_start, | 
|  | 3306 | offp - off_start, | 
|  | 3307 | last_fixup_obj, | 
|  | 3308 | last_fixup_min_off); | 
|  | 3309 | if (ret < 0) { | 
|  | 3310 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3311 | return_error_param = ret; | 
|  | 3312 | return_error_line = __LINE__; | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3313 | goto err_translate_failed; | 
|  | 3314 | } | 
|  | 3315 | last_fixup_obj = bp; | 
|  | 3316 | last_fixup_min_off = 0; | 
|  | 3317 | } break; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3318 | default: | 
| Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3319 | binder_user_error("%d:%d got transaction with invalid object type, %x\n", | 
| Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3320 | proc->pid, thread->pid, hdr->type); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3321 | return_error = BR_FAILED_REPLY; | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3322 | return_error_param = -EINVAL; | 
|  | 3323 | return_error_line = __LINE__; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3324 | goto err_bad_object_type; | 
|  | 3325 | } | 
|  | 3326 | } | 
| Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3327 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3328 | t->work.type = BINDER_WORK_TRANSACTION; | 
| Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3329 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3330 | if (reply) { | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3331 | binder_enqueue_thread_work(thread, tcomplete); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3332 | binder_inner_proc_lock(target_proc); | 
|  | 3333 | if (target_thread->is_dead) { | 
|  | 3334 | binder_inner_proc_unlock(target_proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3335 | goto err_dead_proc_or_thread; | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3336 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3337 | BUG_ON(t->buffer->async_transaction != 0); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3338 | binder_pop_transaction_ilocked(target_thread, in_reply_to); | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3339 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3340 | binder_inner_proc_unlock(target_proc); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3341 | wake_up_interruptible_sync(&target_thread->wait); | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3342 | binder_restore_priority(current, in_reply_to->saved_priority); | 
| Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 3343 | binder_free_transaction(in_reply_to); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3344 | } else if (!(t->flags & TF_ONE_WAY)) { | 
|  | 3345 | BUG_ON(t->buffer->async_transaction != 0); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3346 | binder_inner_proc_lock(proc); | 
| Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 3347 | /* | 
|  | 3348 | * Defer the TRANSACTION_COMPLETE, so we don't return to | 
|  | 3349 | * userspace immediately; this allows the target process to | 
|  | 3350 | * immediately start processing this transaction, reducing | 
|  | 3351 | * latency. We will then return the TRANSACTION_COMPLETE when | 
|  | 3352 | * the target replies (or there is an error). | 
|  | 3353 | */ | 
|  | 3354 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3355 | t->need_reply = 1; | 
|  | 3356 | t->from_parent = thread->transaction_stack; | 
|  | 3357 | thread->transaction_stack = t; | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3358 | binder_inner_proc_unlock(proc); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3359 | if (!binder_proc_transaction(t, target_proc, target_thread)) { | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3360 | binder_inner_proc_lock(proc); | 
|  | 3361 | binder_pop_transaction_ilocked(thread, t); | 
|  | 3362 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3363 | goto err_dead_proc_or_thread; | 
|  | 3364 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3365 | } else { | 
|  | 3366 | BUG_ON(target_node == NULL); | 
|  | 3367 | BUG_ON(t->buffer->async_transaction != 1); | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3368 | binder_enqueue_thread_work(thread, tcomplete); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3369 | if (!binder_proc_transaction(t, target_proc, NULL)) | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3370 | goto err_dead_proc_or_thread; | 
| Riley Andrews | b596881 | 2015-09-01 12:42:07 -0700 | [diff] [blame] | 3371 | } | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3372 | if (target_thread) | 
|  | 3373 | binder_thread_dec_tmpref(target_thread); | 
|  | 3374 | binder_proc_dec_tmpref(target_proc); | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3375 | if (target_node) | 
|  | 3376 | binder_dec_node_tmpref(target_node); | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3377 | /* | 
|  | 3378 | * write barrier to synchronize with initialization | 
|  | 3379 | * of log entry | 
|  | 3380 | */ | 
|  | 3381 | smp_wmb(); | 
|  | 3382 | WRITE_ONCE(e->debug_id_done, t_debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3383 | return; | 
|  | 3384 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3385 | err_dead_proc_or_thread: | 
|  | 3386 | return_error = BR_DEAD_REPLY; | 
|  | 3387 | return_error_line = __LINE__; | 
| Xu YiPing | 86578a0 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3388 | binder_dequeue_work(proc, tcomplete); | 
| Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3389 | err_translate_failed: | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3390 | err_bad_object_type: | 
|  | 3391 | err_bad_offset: | 
| Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3392 | err_bad_parent: | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3393 | err_copy_data_failed: | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3394 | trace_binder_transaction_failed_buffer_release(t->buffer); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3395 | binder_transaction_buffer_release(target_proc, t->buffer, offp); | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3396 | if (target_node) | 
|  | 3397 | binder_dec_node_tmpref(target_node); | 
| Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3398 | target_node = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3399 | t->buffer->transaction = NULL; | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3400 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3401 | err_binder_alloc_buf_failed: | 
|  | 3402 | kfree(tcomplete); | 
|  | 3403 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); | 
|  | 3404 | err_alloc_tcomplete_failed: | 
|  | 3405 | kfree(t); | 
|  | 3406 | binder_stats_deleted(BINDER_STAT_TRANSACTION); | 
|  | 3407 | err_alloc_t_failed: | 
|  | 3408 | err_bad_call_stack: | 
|  | 3409 | err_empty_call_stack: | 
|  | 3410 | err_dead_binder: | 
|  | 3411 | err_invalid_target_handle: | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3412 | if (target_thread) | 
|  | 3413 | binder_thread_dec_tmpref(target_thread); | 
|  | 3414 | if (target_proc) | 
|  | 3415 | binder_proc_dec_tmpref(target_proc); | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3416 | if (target_node) { | 
| Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3417 | binder_dec_node(target_node, 1, 0); | 
| Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3418 | binder_dec_node_tmpref(target_node); | 
|  | 3419 | } | 
| Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3420 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3421 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3422 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", | 
|  | 3423 | proc->pid, thread->pid, return_error, return_error_param, | 
|  | 3424 | (u64)tr->data_size, (u64)tr->offsets_size, | 
|  | 3425 | return_error_line); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3426 |  | 
|  | 3427 | { | 
|  | 3428 | struct binder_transaction_log_entry *fe; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3429 |  | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3430 | e->return_error = return_error; | 
|  | 3431 | e->return_error_param = return_error_param; | 
|  | 3432 | e->return_error_line = return_error_line; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3433 | fe = binder_transaction_log_add(&binder_transaction_log_failed); | 
|  | 3434 | *fe = *e; | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3435 | /* | 
|  | 3436 | * write barrier to synchronize with initialization | 
|  | 3437 | * of log entry | 
|  | 3438 | */ | 
|  | 3439 | smp_wmb(); | 
|  | 3440 | WRITE_ONCE(e->debug_id_done, t_debug_id); | 
|  | 3441 | WRITE_ONCE(fe->debug_id_done, t_debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3442 | } | 
|  | 3443 |  | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3444 | BUG_ON(thread->return_error.cmd != BR_OK); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3445 | if (in_reply_to) { | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3446 | binder_restore_priority(current, in_reply_to->saved_priority); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3447 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3448 | binder_enqueue_thread_work(thread, &thread->return_error.work); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3449 | binder_send_failed_reply(in_reply_to, return_error); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3450 | } else { | 
|  | 3451 | thread->return_error.cmd = return_error; | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3452 | binder_enqueue_thread_work(thread, &thread->return_error.work); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3453 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3454 | } | 
|  | 3455 |  | 
| Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3456 | static int binder_thread_write(struct binder_proc *proc, | 
|  | 3457 | struct binder_thread *thread, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3458 | binder_uintptr_t binder_buffer, size_t size, | 
|  | 3459 | binder_size_t *consumed) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3460 | { | 
|  | 3461 | uint32_t cmd; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3462 | struct binder_context *context = proc->context; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3463 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3464 | void __user *ptr = buffer + *consumed; | 
|  | 3465 | void __user *end = buffer + size; | 
|  | 3466 |  | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3467 | while (ptr < end && thread->return_error.cmd == BR_OK) { | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3468 | int ret; | 
|  | 3469 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3470 | if (get_user(cmd, (uint32_t __user *)ptr)) | 
|  | 3471 | return -EFAULT; | 
|  | 3472 | ptr += sizeof(uint32_t); | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3473 | trace_binder_command(cmd); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3474 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3475 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); | 
|  | 3476 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); | 
|  | 3477 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3478 | } | 
|  | 3479 | switch (cmd) { | 
|  | 3480 | case BC_INCREFS: | 
|  | 3481 | case BC_ACQUIRE: | 
|  | 3482 | case BC_RELEASE: | 
|  | 3483 | case BC_DECREFS: { | 
|  | 3484 | uint32_t target; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3485 | const char *debug_string; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3486 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; | 
|  | 3487 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; | 
|  | 3488 | struct binder_ref_data rdata; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3489 |  | 
|  | 3490 | if (get_user(target, (uint32_t __user *)ptr)) | 
|  | 3491 | return -EFAULT; | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3492 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3493 | ptr += sizeof(uint32_t); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3494 | ret = -1; | 
|  | 3495 | if (increment && !target) { | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3496 | struct binder_node *ctx_mgr_node; | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3497 | mutex_lock(&context->context_mgr_node_lock); | 
|  | 3498 | ctx_mgr_node = context->binder_context_mgr_node; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3499 | if (ctx_mgr_node) | 
|  | 3500 | ret = binder_inc_ref_for_node( | 
|  | 3501 | proc, ctx_mgr_node, | 
|  | 3502 | strong, NULL, &rdata); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3503 | mutex_unlock(&context->context_mgr_node_lock); | 
|  | 3504 | } | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3505 | if (ret) | 
|  | 3506 | ret = binder_update_ref_for_handle( | 
|  | 3507 | proc, target, increment, strong, | 
|  | 3508 | &rdata); | 
|  | 3509 | if (!ret && rdata.desc != target) { | 
|  | 3510 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", | 
|  | 3511 | proc->pid, thread->pid, | 
|  | 3512 | target, rdata.desc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3513 | } | 
|  | 3514 | switch (cmd) { | 
|  | 3515 | case BC_INCREFS: | 
|  | 3516 | debug_string = "IncRefs"; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3517 | break; | 
|  | 3518 | case BC_ACQUIRE: | 
|  | 3519 | debug_string = "Acquire"; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3520 | break; | 
|  | 3521 | case BC_RELEASE: | 
|  | 3522 | debug_string = "Release"; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3523 | break; | 
|  | 3524 | case BC_DECREFS: | 
|  | 3525 | default: | 
|  | 3526 | debug_string = "DecRefs"; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3527 | break; | 
|  | 3528 | } | 
|  | 3529 | if (ret) { | 
|  | 3530 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", | 
|  | 3531 | proc->pid, thread->pid, debug_string, | 
|  | 3532 | strong, target, ret); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3533 | break; | 
|  | 3534 | } | 
|  | 3535 | binder_debug(BINDER_DEBUG_USER_REFS, | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3536 | "%d:%d %s ref %d desc %d s %d w %d\n", | 
|  | 3537 | proc->pid, thread->pid, debug_string, | 
|  | 3538 | rdata.debug_id, rdata.desc, rdata.strong, | 
|  | 3539 | rdata.weak); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3540 | break; | 
|  | 3541 | } | 
|  | 3542 | case BC_INCREFS_DONE: | 
|  | 3543 | case BC_ACQUIRE_DONE: { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3544 | binder_uintptr_t node_ptr; | 
|  | 3545 | binder_uintptr_t cookie; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3546 | struct binder_node *node; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3547 | bool free_node; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3548 |  | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3549 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3550 | return -EFAULT; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3551 | ptr += sizeof(binder_uintptr_t); | 
|  | 3552 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3553 | return -EFAULT; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3554 | ptr += sizeof(binder_uintptr_t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3555 | node = binder_get_node(proc, node_ptr); | 
|  | 3556 | if (node == NULL) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3557 | binder_user_error("%d:%d %s u%016llx no match\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3558 | proc->pid, thread->pid, | 
|  | 3559 | cmd == BC_INCREFS_DONE ? | 
|  | 3560 | "BC_INCREFS_DONE" : | 
|  | 3561 | "BC_ACQUIRE_DONE", | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3562 | (u64)node_ptr); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3563 | break; | 
|  | 3564 | } | 
|  | 3565 | if (cookie != node->cookie) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3566 | binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3567 | proc->pid, thread->pid, | 
|  | 3568 | cmd == BC_INCREFS_DONE ? | 
|  | 3569 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3570 | (u64)node_ptr, node->debug_id, | 
|  | 3571 | (u64)cookie, (u64)node->cookie); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3572 | binder_put_node(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3573 | break; | 
|  | 3574 | } | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3575 | binder_node_inner_lock(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3576 | if (cmd == BC_ACQUIRE_DONE) { | 
|  | 3577 | if (node->pending_strong_ref == 0) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3578 | binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3579 | proc->pid, thread->pid, | 
|  | 3580 | node->debug_id); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3581 | binder_node_inner_unlock(node); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3582 | binder_put_node(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3583 | break; | 
|  | 3584 | } | 
|  | 3585 | node->pending_strong_ref = 0; | 
|  | 3586 | } else { | 
|  | 3587 | if (node->pending_weak_ref == 0) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3588 | binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3589 | proc->pid, thread->pid, | 
|  | 3590 | node->debug_id); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3591 | binder_node_inner_unlock(node); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3592 | binder_put_node(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3593 | break; | 
|  | 3594 | } | 
|  | 3595 | node->pending_weak_ref = 0; | 
|  | 3596 | } | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3597 | free_node = binder_dec_node_nilocked(node, | 
|  | 3598 | cmd == BC_ACQUIRE_DONE, 0); | 
|  | 3599 | WARN_ON(free_node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3600 | binder_debug(BINDER_DEBUG_USER_REFS, | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3601 | "%d:%d %s node %d ls %d lw %d tr %d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3602 | proc->pid, thread->pid, | 
|  | 3603 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3604 | node->debug_id, node->local_strong_refs, | 
|  | 3605 | node->local_weak_refs, node->tmp_refs); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3606 | binder_node_inner_unlock(node); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3607 | binder_put_node(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3608 | break; | 
|  | 3609 | } | 
|  | 3610 | case BC_ATTEMPT_ACQUIRE: | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3611 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3612 | return -EINVAL; | 
|  | 3613 | case BC_ACQUIRE_RESULT: | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3614 | pr_err("BC_ACQUIRE_RESULT not supported\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3615 | return -EINVAL; | 
|  | 3616 |  | 
|  | 3617 | case BC_FREE_BUFFER: { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3618 | binder_uintptr_t data_ptr; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3619 | struct binder_buffer *buffer; | 
|  | 3620 |  | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3621 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3622 | return -EFAULT; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3623 | ptr += sizeof(binder_uintptr_t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3624 |  | 
| Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 3625 | buffer = binder_alloc_prepare_to_free(&proc->alloc, | 
|  | 3626 | data_ptr); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3627 | if (buffer == NULL) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3628 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", | 
|  | 3629 | proc->pid, thread->pid, (u64)data_ptr); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3630 | break; | 
|  | 3631 | } | 
|  | 3632 | if (!buffer->allow_user_free) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3633 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", | 
|  | 3634 | proc->pid, thread->pid, (u64)data_ptr); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3635 | break; | 
|  | 3636 | } | 
|  | 3637 | binder_debug(BINDER_DEBUG_FREE_BUFFER, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3638 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", | 
|  | 3639 | proc->pid, thread->pid, (u64)data_ptr, | 
|  | 3640 | buffer->debug_id, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3641 | buffer->transaction ? "active" : "finished"); | 
|  | 3642 |  | 
|  | 3643 | if (buffer->transaction) { | 
|  | 3644 | buffer->transaction->buffer = NULL; | 
|  | 3645 | buffer->transaction = NULL; | 
|  | 3646 | } | 
|  | 3647 | if (buffer->async_transaction && buffer->target_node) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3648 | struct binder_node *buf_node; | 
|  | 3649 | struct binder_work *w; | 
|  | 3650 |  | 
|  | 3651 | buf_node = buffer->target_node; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3652 | binder_node_inner_lock(buf_node); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3653 | BUG_ON(!buf_node->has_async_transaction); | 
|  | 3654 | BUG_ON(buf_node->proc != proc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3655 | w = binder_dequeue_work_head_ilocked( | 
|  | 3656 | &buf_node->async_todo); | 
| Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3657 | if (!w) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3658 | buf_node->has_async_transaction = 0; | 
| Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3659 | } else { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3660 | binder_enqueue_work_ilocked( | 
| Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3661 | w, &proc->todo); | 
|  | 3662 | binder_wakeup_proc_ilocked(proc); | 
|  | 3663 | } | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3664 | binder_node_inner_unlock(buf_node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3665 | } | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3666 | trace_binder_transaction_buffer_release(buffer); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3667 | binder_transaction_buffer_release(proc, buffer, NULL); | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3668 | binder_alloc_free_buf(&proc->alloc, buffer); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3669 | break; | 
|  | 3670 | } | 
|  | 3671 |  | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3672 | case BC_TRANSACTION_SG: | 
|  | 3673 | case BC_REPLY_SG: { | 
|  | 3674 | struct binder_transaction_data_sg tr; | 
|  | 3675 |  | 
|  | 3676 | if (copy_from_user(&tr, ptr, sizeof(tr))) | 
|  | 3677 | return -EFAULT; | 
|  | 3678 | ptr += sizeof(tr); | 
|  | 3679 | binder_transaction(proc, thread, &tr.transaction_data, | 
|  | 3680 | cmd == BC_REPLY_SG, tr.buffers_size); | 
|  | 3681 | break; | 
|  | 3682 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3683 | case BC_TRANSACTION: | 
|  | 3684 | case BC_REPLY: { | 
|  | 3685 | struct binder_transaction_data tr; | 
|  | 3686 |  | 
|  | 3687 | if (copy_from_user(&tr, ptr, sizeof(tr))) | 
|  | 3688 | return -EFAULT; | 
|  | 3689 | ptr += sizeof(tr); | 
| Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3690 | binder_transaction(proc, thread, &tr, | 
|  | 3691 | cmd == BC_REPLY, 0); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3692 | break; | 
|  | 3693 | } | 
|  | 3694 |  | 
|  | 3695 | case BC_REGISTER_LOOPER: | 
|  | 3696 | binder_debug(BINDER_DEBUG_THREADS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3697 | "%d:%d BC_REGISTER_LOOPER\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3698 | proc->pid, thread->pid); | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3699 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3700 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { | 
|  | 3701 | thread->looper |= BINDER_LOOPER_STATE_INVALID; | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3702 | binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3703 | proc->pid, thread->pid); | 
|  | 3704 | } else if (proc->requested_threads == 0) { | 
|  | 3705 | thread->looper |= BINDER_LOOPER_STATE_INVALID; | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3706 | binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3707 | proc->pid, thread->pid); | 
|  | 3708 | } else { | 
|  | 3709 | proc->requested_threads--; | 
|  | 3710 | proc->requested_threads_started++; | 
|  | 3711 | } | 
|  | 3712 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3713 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3714 | break; | 
|  | 3715 | case BC_ENTER_LOOPER: | 
|  | 3716 | binder_debug(BINDER_DEBUG_THREADS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3717 | "%d:%d BC_ENTER_LOOPER\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3718 | proc->pid, thread->pid); | 
|  | 3719 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { | 
|  | 3720 | thread->looper |= BINDER_LOOPER_STATE_INVALID; | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3721 | binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3722 | proc->pid, thread->pid); | 
|  | 3723 | } | 
|  | 3724 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; | 
|  | 3725 | break; | 
|  | 3726 | case BC_EXIT_LOOPER: | 
|  | 3727 | binder_debug(BINDER_DEBUG_THREADS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3728 | "%d:%d BC_EXIT_LOOPER\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3729 | proc->pid, thread->pid); | 
|  | 3730 | thread->looper |= BINDER_LOOPER_STATE_EXITED; | 
|  | 3731 | break; | 
|  | 3732 |  | 
|  | 3733 | case BC_REQUEST_DEATH_NOTIFICATION: | 
|  | 3734 | case BC_CLEAR_DEATH_NOTIFICATION: { | 
|  | 3735 | uint32_t target; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3736 | binder_uintptr_t cookie; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3737 | struct binder_ref *ref; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3738 | struct binder_ref_death *death = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3739 |  | 
|  | 3740 | if (get_user(target, (uint32_t __user *)ptr)) | 
|  | 3741 | return -EFAULT; | 
|  | 3742 | ptr += sizeof(uint32_t); | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3743 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3744 | return -EFAULT; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3745 | ptr += sizeof(binder_uintptr_t); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3746 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { | 
|  | 3747 | /* | 
|  | 3748 | * Allocate memory for death notification | 
|  | 3749 | * before taking lock | 
|  | 3750 | */ | 
|  | 3751 | death = kzalloc(sizeof(*death), GFP_KERNEL); | 
|  | 3752 | if (death == NULL) { | 
|  | 3753 | WARN_ON(thread->return_error.cmd != | 
|  | 3754 | BR_OK); | 
|  | 3755 | thread->return_error.cmd = BR_ERROR; | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3756 | binder_enqueue_thread_work( | 
|  | 3757 | thread, | 
|  | 3758 | &thread->return_error.work); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3759 | binder_debug( | 
|  | 3760 | BINDER_DEBUG_FAILED_TRANSACTION, | 
|  | 3761 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", | 
|  | 3762 | proc->pid, thread->pid); | 
|  | 3763 | break; | 
|  | 3764 | } | 
|  | 3765 | } | 
|  | 3766 | binder_proc_lock(proc); | 
|  | 3767 | ref = binder_get_ref_olocked(proc, target, false); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3768 | if (ref == NULL) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3769 | binder_user_error("%d:%d %s invalid ref %d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3770 | proc->pid, thread->pid, | 
|  | 3771 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? | 
|  | 3772 | "BC_REQUEST_DEATH_NOTIFICATION" : | 
|  | 3773 | "BC_CLEAR_DEATH_NOTIFICATION", | 
|  | 3774 | target); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3775 | binder_proc_unlock(proc); | 
|  | 3776 | kfree(death); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3777 | break; | 
|  | 3778 | } | 
|  | 3779 |  | 
|  | 3780 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3781 | "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3782 | proc->pid, thread->pid, | 
|  | 3783 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? | 
|  | 3784 | "BC_REQUEST_DEATH_NOTIFICATION" : | 
|  | 3785 | "BC_CLEAR_DEATH_NOTIFICATION", | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3786 | (u64)cookie, ref->data.debug_id, | 
|  | 3787 | ref->data.desc, ref->data.strong, | 
|  | 3788 | ref->data.weak, ref->node->debug_id); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3789 |  | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3790 | binder_node_lock(ref->node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3791 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { | 
|  | 3792 | if (ref->death) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3793 | binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3794 | proc->pid, thread->pid); | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3795 | binder_node_unlock(ref->node); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3796 | binder_proc_unlock(proc); | 
|  | 3797 | kfree(death); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3798 | break; | 
|  | 3799 | } | 
|  | 3800 | binder_stats_created(BINDER_STAT_DEATH); | 
|  | 3801 | INIT_LIST_HEAD(&death->work.entry); | 
|  | 3802 | death->cookie = cookie; | 
|  | 3803 | ref->death = death; | 
|  | 3804 | if (ref->node->proc == NULL) { | 
|  | 3805 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; | 
| Martijn Coenen | 3bdbe4c | 2017-08-10 13:50:52 +0200 | [diff] [blame] | 3806 |  | 
|  | 3807 | binder_inner_proc_lock(proc); | 
|  | 3808 | binder_enqueue_work_ilocked( | 
|  | 3809 | &ref->death->work, &proc->todo); | 
|  | 3810 | binder_wakeup_proc_ilocked(proc); | 
|  | 3811 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3812 | } | 
|  | 3813 | } else { | 
|  | 3814 | if (ref->death == NULL) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3815 | binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3816 | proc->pid, thread->pid); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3817 | binder_node_unlock(ref->node); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3818 | binder_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3819 | break; | 
|  | 3820 | } | 
|  | 3821 | death = ref->death; | 
|  | 3822 | if (death->cookie != cookie) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3823 | binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3824 | proc->pid, thread->pid, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3825 | (u64)death->cookie, | 
|  | 3826 | (u64)cookie); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3827 | binder_node_unlock(ref->node); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3828 | binder_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3829 | break; | 
|  | 3830 | } | 
|  | 3831 | ref->death = NULL; | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3832 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3833 | if (list_empty(&death->work.entry)) { | 
|  | 3834 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3835 | if (thread->looper & | 
|  | 3836 | (BINDER_LOOPER_STATE_REGISTERED | | 
|  | 3837 | BINDER_LOOPER_STATE_ENTERED)) | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3838 | binder_enqueue_thread_work_ilocked( | 
|  | 3839 | thread, | 
|  | 3840 | &death->work); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3841 | else { | 
|  | 3842 | binder_enqueue_work_ilocked( | 
|  | 3843 | &death->work, | 
|  | 3844 | &proc->todo); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3845 | binder_wakeup_proc_ilocked( | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3846 | proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3847 | } | 
|  | 3848 | } else { | 
|  | 3849 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); | 
|  | 3850 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; | 
|  | 3851 | } | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3852 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3853 | } | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3854 | binder_node_unlock(ref->node); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3855 | binder_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3856 | } break; | 
|  | 3857 | case BC_DEAD_BINDER_DONE: { | 
|  | 3858 | struct binder_work *w; | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3859 | binder_uintptr_t cookie; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3860 | struct binder_ref_death *death = NULL; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3861 |  | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3862 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3863 | return -EFAULT; | 
|  | 3864 |  | 
| Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3865 | ptr += sizeof(cookie); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3866 | binder_inner_proc_lock(proc); | 
|  | 3867 | list_for_each_entry(w, &proc->delivered_death, | 
|  | 3868 | entry) { | 
|  | 3869 | struct binder_ref_death *tmp_death = | 
|  | 3870 | container_of(w, | 
|  | 3871 | struct binder_ref_death, | 
|  | 3872 | work); | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3873 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3874 | if (tmp_death->cookie == cookie) { | 
|  | 3875 | death = tmp_death; | 
|  | 3876 | break; | 
|  | 3877 | } | 
|  | 3878 | } | 
|  | 3879 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3880 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n", | 
|  | 3881 | proc->pid, thread->pid, (u64)cookie, | 
|  | 3882 | death); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3883 | if (death == NULL) { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3884 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", | 
|  | 3885 | proc->pid, thread->pid, (u64)cookie); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3886 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3887 | break; | 
|  | 3888 | } | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3889 | binder_dequeue_work_ilocked(&death->work); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3890 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { | 
|  | 3891 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3892 | if (thread->looper & | 
|  | 3893 | (BINDER_LOOPER_STATE_REGISTERED | | 
|  | 3894 | BINDER_LOOPER_STATE_ENTERED)) | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3895 | binder_enqueue_thread_work_ilocked( | 
|  | 3896 | thread, &death->work); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3897 | else { | 
|  | 3898 | binder_enqueue_work_ilocked( | 
|  | 3899 | &death->work, | 
|  | 3900 | &proc->todo); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3901 | binder_wakeup_proc_ilocked(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3902 | } | 
|  | 3903 | } | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3904 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3905 | } break; | 
|  | 3906 |  | 
|  | 3907 | default: | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3908 | pr_err("%d:%d unknown command %d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3909 | proc->pid, thread->pid, cmd); | 
|  | 3910 | return -EINVAL; | 
|  | 3911 | } | 
|  | 3912 | *consumed = ptr - buffer; | 
|  | 3913 | } | 
|  | 3914 | return 0; | 
|  | 3915 | } | 
|  | 3916 |  | 
| Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3917 | static void binder_stat_br(struct binder_proc *proc, | 
|  | 3918 | struct binder_thread *thread, uint32_t cmd) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3919 | { | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3920 | trace_binder_return(cmd); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3921 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3922 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); | 
|  | 3923 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); | 
|  | 3924 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3925 | } | 
|  | 3926 | } | 
|  | 3927 |  | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 3928 | static int binder_put_node_cmd(struct binder_proc *proc, | 
|  | 3929 | struct binder_thread *thread, | 
|  | 3930 | void __user **ptrp, | 
|  | 3931 | binder_uintptr_t node_ptr, | 
|  | 3932 | binder_uintptr_t node_cookie, | 
|  | 3933 | int node_debug_id, | 
|  | 3934 | uint32_t cmd, const char *cmd_name) | 
|  | 3935 | { | 
|  | 3936 | void __user *ptr = *ptrp; | 
|  | 3937 |  | 
|  | 3938 | if (put_user(cmd, (uint32_t __user *)ptr)) | 
|  | 3939 | return -EFAULT; | 
|  | 3940 | ptr += sizeof(uint32_t); | 
|  | 3941 |  | 
|  | 3942 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) | 
|  | 3943 | return -EFAULT; | 
|  | 3944 | ptr += sizeof(binder_uintptr_t); | 
|  | 3945 |  | 
|  | 3946 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) | 
|  | 3947 | return -EFAULT; | 
|  | 3948 | ptr += sizeof(binder_uintptr_t); | 
|  | 3949 |  | 
|  | 3950 | binder_stat_br(proc, thread, cmd); | 
|  | 3951 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", | 
|  | 3952 | proc->pid, thread->pid, cmd_name, node_debug_id, | 
|  | 3953 | (u64)node_ptr, (u64)node_cookie); | 
|  | 3954 |  | 
|  | 3955 | *ptrp = ptr; | 
|  | 3956 | return 0; | 
|  | 3957 | } | 
|  | 3958 |  | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3959 | static int binder_wait_for_work(struct binder_thread *thread, | 
|  | 3960 | bool do_proc_work) | 
|  | 3961 | { | 
|  | 3962 | DEFINE_WAIT(wait); | 
|  | 3963 | struct binder_proc *proc = thread->proc; | 
|  | 3964 | int ret = 0; | 
|  | 3965 |  | 
|  | 3966 | freezer_do_not_count(); | 
|  | 3967 | binder_inner_proc_lock(proc); | 
|  | 3968 | for (;;) { | 
|  | 3969 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); | 
|  | 3970 | if (binder_has_work_ilocked(thread, do_proc_work)) | 
|  | 3971 | break; | 
|  | 3972 | if (do_proc_work) | 
|  | 3973 | list_add(&thread->waiting_thread_node, | 
|  | 3974 | &proc->waiting_threads); | 
|  | 3975 | binder_inner_proc_unlock(proc); | 
|  | 3976 | schedule(); | 
|  | 3977 | binder_inner_proc_lock(proc); | 
|  | 3978 | list_del_init(&thread->waiting_thread_node); | 
|  | 3979 | if (signal_pending(current)) { | 
|  | 3980 | ret = -ERESTARTSYS; | 
|  | 3981 | break; | 
|  | 3982 | } | 
|  | 3983 | } | 
|  | 3984 | finish_wait(&thread->wait, &wait); | 
|  | 3985 | binder_inner_proc_unlock(proc); | 
|  | 3986 | freezer_count(); | 
|  | 3987 |  | 
|  | 3988 | return ret; | 
|  | 3989 | } | 
|  | 3990 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3991 | static int binder_thread_read(struct binder_proc *proc, | 
|  | 3992 | struct binder_thread *thread, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3993 | binder_uintptr_t binder_buffer, size_t size, | 
|  | 3994 | binder_size_t *consumed, int non_block) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3995 | { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3996 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3997 | void __user *ptr = buffer + *consumed; | 
|  | 3998 | void __user *end = buffer + size; | 
|  | 3999 |  | 
|  | 4000 | int ret = 0; | 
|  | 4001 | int wait_for_proc_work; | 
|  | 4002 |  | 
|  | 4003 | if (*consumed == 0) { | 
|  | 4004 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) | 
|  | 4005 | return -EFAULT; | 
|  | 4006 | ptr += sizeof(uint32_t); | 
|  | 4007 | } | 
|  | 4008 |  | 
|  | 4009 | retry: | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4010 | binder_inner_proc_lock(proc); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4011 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4012 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4013 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4014 | thread->looper |= BINDER_LOOPER_STATE_WAITING; | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4015 |  | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4016 | trace_binder_wait_for_work(wait_for_proc_work, | 
|  | 4017 | !!thread->transaction_stack, | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4018 | !binder_worklist_empty(proc, &thread->todo)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4019 | if (wait_for_proc_work) { | 
|  | 4020 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | | 
|  | 4021 | BINDER_LOOPER_STATE_ENTERED))) { | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4022 | binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4023 | proc->pid, thread->pid, thread->looper); | 
|  | 4024 | wait_event_interruptible(binder_user_error_wait, | 
|  | 4025 | binder_stop_on_user_error < 2); | 
|  | 4026 | } | 
| Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 4027 | binder_restore_priority(current, proc->default_priority); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4028 | } | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4029 |  | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4030 | if (non_block) { | 
|  | 4031 | if (!binder_has_work(thread, wait_for_proc_work)) | 
|  | 4032 | ret = -EAGAIN; | 
|  | 4033 | } else { | 
|  | 4034 | ret = binder_wait_for_work(thread, wait_for_proc_work); | 
|  | 4035 | } | 
|  | 4036 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4037 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; | 
|  | 4038 |  | 
|  | 4039 | if (ret) | 
|  | 4040 | return ret; | 
|  | 4041 |  | 
|  | 4042 | while (1) { | 
|  | 4043 | uint32_t cmd; | 
|  | 4044 | struct binder_transaction_data tr; | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4045 | struct binder_work *w = NULL; | 
|  | 4046 | struct list_head *list = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4047 | struct binder_transaction *t = NULL; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4048 | struct binder_thread *t_from; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4049 |  | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4050 | binder_inner_proc_lock(proc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4051 | if (!binder_worklist_empty_ilocked(&thread->todo)) | 
|  | 4052 | list = &thread->todo; | 
|  | 4053 | else if (!binder_worklist_empty_ilocked(&proc->todo) && | 
|  | 4054 | wait_for_proc_work) | 
|  | 4055 | list = &proc->todo; | 
|  | 4056 | else { | 
|  | 4057 | binder_inner_proc_unlock(proc); | 
|  | 4058 |  | 
| Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 4059 | /* no data added */ | 
| Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4060 | if (ptr - buffer == 4 && !thread->looper_need_return) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4061 | goto retry; | 
|  | 4062 | break; | 
|  | 4063 | } | 
|  | 4064 |  | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4065 | if (end - ptr < sizeof(tr) + 4) { | 
|  | 4066 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4067 | break; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4068 | } | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4069 | w = binder_dequeue_work_head_ilocked(list); | 
| Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4070 | if (binder_worklist_empty_ilocked(&thread->todo)) | 
|  | 4071 | thread->process_todo = false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4072 |  | 
|  | 4073 | switch (w->type) { | 
|  | 4074 | case BINDER_WORK_TRANSACTION: { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4075 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4076 | t = container_of(w, struct binder_transaction, work); | 
|  | 4077 | } break; | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4078 | case BINDER_WORK_RETURN_ERROR: { | 
|  | 4079 | struct binder_error *e = container_of( | 
|  | 4080 | w, struct binder_error, work); | 
|  | 4081 |  | 
|  | 4082 | WARN_ON(e->cmd == BR_OK); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4083 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4084 | if (put_user(e->cmd, (uint32_t __user *)ptr)) | 
|  | 4085 | return -EFAULT; | 
|  | 4086 | e->cmd = BR_OK; | 
|  | 4087 | ptr += sizeof(uint32_t); | 
|  | 4088 |  | 
|  | 4089 | binder_stat_br(proc, thread, cmd); | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4090 | } break; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4091 | case BINDER_WORK_TRANSACTION_COMPLETE: { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4092 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4093 | cmd = BR_TRANSACTION_COMPLETE; | 
|  | 4094 | if (put_user(cmd, (uint32_t __user *)ptr)) | 
|  | 4095 | return -EFAULT; | 
|  | 4096 | ptr += sizeof(uint32_t); | 
|  | 4097 |  | 
|  | 4098 | binder_stat_br(proc, thread, cmd); | 
|  | 4099 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4100 | "%d:%d BR_TRANSACTION_COMPLETE\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4101 | proc->pid, thread->pid); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4102 | kfree(w); | 
|  | 4103 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); | 
|  | 4104 | } break; | 
|  | 4105 | case BINDER_WORK_NODE: { | 
|  | 4106 | struct binder_node *node = container_of(w, struct binder_node, work); | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4107 | int strong, weak; | 
|  | 4108 | binder_uintptr_t node_ptr = node->ptr; | 
|  | 4109 | binder_uintptr_t node_cookie = node->cookie; | 
|  | 4110 | int node_debug_id = node->debug_id; | 
|  | 4111 | int has_weak_ref; | 
|  | 4112 | int has_strong_ref; | 
|  | 4113 | void __user *orig_ptr = ptr; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4114 |  | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4115 | BUG_ON(proc != node->proc); | 
|  | 4116 | strong = node->internal_strong_refs || | 
|  | 4117 | node->local_strong_refs; | 
|  | 4118 | weak = !hlist_empty(&node->refs) || | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4119 | node->local_weak_refs || | 
|  | 4120 | node->tmp_refs || strong; | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4121 | has_strong_ref = node->has_strong_ref; | 
|  | 4122 | has_weak_ref = node->has_weak_ref; | 
|  | 4123 |  | 
|  | 4124 | if (weak && !has_weak_ref) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4125 | node->has_weak_ref = 1; | 
|  | 4126 | node->pending_weak_ref = 1; | 
|  | 4127 | node->local_weak_refs++; | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4128 | } | 
|  | 4129 | if (strong && !has_strong_ref) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4130 | node->has_strong_ref = 1; | 
|  | 4131 | node->pending_strong_ref = 1; | 
|  | 4132 | node->local_strong_refs++; | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4133 | } | 
|  | 4134 | if (!strong && has_strong_ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4135 | node->has_strong_ref = 0; | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4136 | if (!weak && has_weak_ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4137 | node->has_weak_ref = 0; | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4138 | if (!weak && !strong) { | 
|  | 4139 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
|  | 4140 | "%d:%d node %d u%016llx c%016llx deleted\n", | 
|  | 4141 | proc->pid, thread->pid, | 
|  | 4142 | node_debug_id, | 
|  | 4143 | (u64)node_ptr, | 
|  | 4144 | (u64)node_cookie); | 
|  | 4145 | rb_erase(&node->rb_node, &proc->nodes); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4146 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4147 | binder_node_lock(node); | 
|  | 4148 | /* | 
|  | 4149 | * Acquire the node lock before freeing the | 
|  | 4150 | * node to serialize with other threads that | 
|  | 4151 | * may have been holding the node lock while | 
|  | 4152 | * decrementing this node (avoids race where | 
|  | 4153 | * this thread frees while the other thread | 
|  | 4154 | * is unlocking the node after the final | 
|  | 4155 | * decrement) | 
|  | 4156 | */ | 
|  | 4157 | binder_node_unlock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4158 | binder_free_node(node); | 
|  | 4159 | } else | 
|  | 4160 | binder_inner_proc_unlock(proc); | 
|  | 4161 |  | 
| Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4162 | if (weak && !has_weak_ref) | 
|  | 4163 | ret = binder_put_node_cmd( | 
|  | 4164 | proc, thread, &ptr, node_ptr, | 
|  | 4165 | node_cookie, node_debug_id, | 
|  | 4166 | BR_INCREFS, "BR_INCREFS"); | 
|  | 4167 | if (!ret && strong && !has_strong_ref) | 
|  | 4168 | ret = binder_put_node_cmd( | 
|  | 4169 | proc, thread, &ptr, node_ptr, | 
|  | 4170 | node_cookie, node_debug_id, | 
|  | 4171 | BR_ACQUIRE, "BR_ACQUIRE"); | 
|  | 4172 | if (!ret && !strong && has_strong_ref) | 
|  | 4173 | ret = binder_put_node_cmd( | 
|  | 4174 | proc, thread, &ptr, node_ptr, | 
|  | 4175 | node_cookie, node_debug_id, | 
|  | 4176 | BR_RELEASE, "BR_RELEASE"); | 
|  | 4177 | if (!ret && !weak && has_weak_ref) | 
|  | 4178 | ret = binder_put_node_cmd( | 
|  | 4179 | proc, thread, &ptr, node_ptr, | 
|  | 4180 | node_cookie, node_debug_id, | 
|  | 4181 | BR_DECREFS, "BR_DECREFS"); | 
|  | 4182 | if (orig_ptr == ptr) | 
|  | 4183 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | 
|  | 4184 | "%d:%d node %d u%016llx c%016llx state unchanged\n", | 
|  | 4185 | proc->pid, thread->pid, | 
|  | 4186 | node_debug_id, | 
|  | 4187 | (u64)node_ptr, | 
|  | 4188 | (u64)node_cookie); | 
|  | 4189 | if (ret) | 
|  | 4190 | return ret; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4191 | } break; | 
|  | 4192 | case BINDER_WORK_DEAD_BINDER: | 
|  | 4193 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: | 
|  | 4194 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { | 
|  | 4195 | struct binder_ref_death *death; | 
|  | 4196 | uint32_t cmd; | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4197 | binder_uintptr_t cookie; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4198 |  | 
|  | 4199 | death = container_of(w, struct binder_ref_death, work); | 
|  | 4200 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) | 
|  | 4201 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; | 
|  | 4202 | else | 
|  | 4203 | cmd = BR_DEAD_BINDER; | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4204 | cookie = death->cookie; | 
|  | 4205 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4206 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4207 | "%d:%d %s %016llx\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4208 | proc->pid, thread->pid, | 
|  | 4209 | cmd == BR_DEAD_BINDER ? | 
|  | 4210 | "BR_DEAD_BINDER" : | 
|  | 4211 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4212 | (u64)cookie); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4213 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4214 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4215 | kfree(death); | 
|  | 4216 | binder_stats_deleted(BINDER_STAT_DEATH); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4217 | } else { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4218 | binder_enqueue_work_ilocked( | 
|  | 4219 | w, &proc->delivered_death); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4220 | binder_inner_proc_unlock(proc); | 
|  | 4221 | } | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4222 | if (put_user(cmd, (uint32_t __user *)ptr)) | 
|  | 4223 | return -EFAULT; | 
|  | 4224 | ptr += sizeof(uint32_t); | 
|  | 4225 | if (put_user(cookie, | 
|  | 4226 | (binder_uintptr_t __user *)ptr)) | 
|  | 4227 | return -EFAULT; | 
|  | 4228 | ptr += sizeof(binder_uintptr_t); | 
|  | 4229 | binder_stat_br(proc, thread, cmd); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4230 | if (cmd == BR_DEAD_BINDER) | 
|  | 4231 | goto done; /* DEAD_BINDER notifications can cause transactions */ | 
|  | 4232 | } break; | 
|  | 4233 | } | 
|  | 4234 |  | 
|  | 4235 | if (!t) | 
|  | 4236 | continue; | 
|  | 4237 |  | 
|  | 4238 | BUG_ON(t->buffer == NULL); | 
|  | 4239 | if (t->buffer->target_node) { | 
|  | 4240 | struct binder_node *target_node = t->buffer->target_node; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4241 | struct binder_priority node_prio; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4242 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4243 | tr.target.ptr = target_node->ptr; | 
|  | 4244 | tr.cookie =  target_node->cookie; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4245 | node_prio.sched_policy = target_node->sched_policy; | 
|  | 4246 | node_prio.prio = target_node->min_priority; | 
| Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 4247 | binder_transaction_priority(current, t, node_prio, | 
|  | 4248 | target_node->inherit_rt); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4249 | cmd = BR_TRANSACTION; | 
|  | 4250 | } else { | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4251 | tr.target.ptr = 0; | 
|  | 4252 | tr.cookie = 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4253 | cmd = BR_REPLY; | 
|  | 4254 | } | 
|  | 4255 | tr.code = t->code; | 
|  | 4256 | tr.flags = t->flags; | 
| Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 4257 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4258 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4259 | t_from = binder_get_txn_from(t); | 
|  | 4260 | if (t_from) { | 
|  | 4261 | struct task_struct *sender = t_from->proc->tsk; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4262 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4263 | tr.sender_pid = task_tgid_nr_ns(sender, | 
| Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 4264 | task_active_pid_ns(current)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4265 | } else { | 
|  | 4266 | tr.sender_pid = 0; | 
|  | 4267 | } | 
|  | 4268 |  | 
|  | 4269 | tr.data_size = t->buffer->data_size; | 
|  | 4270 | tr.offsets_size = t->buffer->offsets_size; | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4271 | tr.data.ptr.buffer = (binder_uintptr_t) | 
|  | 4272 | ((uintptr_t)t->buffer->data + | 
|  | 4273 | binder_alloc_get_user_buffer_offset(&proc->alloc)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4274 | tr.data.ptr.offsets = tr.data.ptr.buffer + | 
|  | 4275 | ALIGN(t->buffer->data_size, | 
|  | 4276 | sizeof(void *)); | 
|  | 4277 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4278 | if (put_user(cmd, (uint32_t __user *)ptr)) { | 
|  | 4279 | if (t_from) | 
|  | 4280 | binder_thread_dec_tmpref(t_from); | 
| Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4281 |  | 
|  | 4282 | binder_cleanup_transaction(t, "put_user failed", | 
|  | 4283 | BR_FAILED_REPLY); | 
|  | 4284 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4285 | return -EFAULT; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4286 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4287 | ptr += sizeof(uint32_t); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4288 | if (copy_to_user(ptr, &tr, sizeof(tr))) { | 
|  | 4289 | if (t_from) | 
|  | 4290 | binder_thread_dec_tmpref(t_from); | 
| Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4291 |  | 
|  | 4292 | binder_cleanup_transaction(t, "copy_to_user failed", | 
|  | 4293 | BR_FAILED_REPLY); | 
|  | 4294 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4295 | return -EFAULT; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4296 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4297 | ptr += sizeof(tr); | 
|  | 4298 |  | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4299 | trace_binder_transaction_received(t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4300 | binder_stat_br(proc, thread, cmd); | 
|  | 4301 | binder_debug(BINDER_DEBUG_TRANSACTION, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4302 | "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4303 | proc->pid, thread->pid, | 
|  | 4304 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : | 
|  | 4305 | "BR_REPLY", | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4306 | t->debug_id, t_from ? t_from->proc->pid : 0, | 
|  | 4307 | t_from ? t_from->pid : 0, cmd, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4308 | t->buffer->data_size, t->buffer->offsets_size, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4309 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4310 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4311 | if (t_from) | 
|  | 4312 | binder_thread_dec_tmpref(t_from); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4313 | t->buffer->allow_user_free = 1; | 
|  | 4314 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4315 | binder_inner_proc_lock(thread->proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4316 | t->to_parent = thread->transaction_stack; | 
|  | 4317 | t->to_thread = thread; | 
|  | 4318 | thread->transaction_stack = t; | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4319 | binder_inner_proc_unlock(thread->proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4320 | } else { | 
| Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 4321 | binder_free_transaction(t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4322 | } | 
|  | 4323 | break; | 
|  | 4324 | } | 
|  | 4325 |  | 
|  | 4326 | done: | 
|  | 4327 |  | 
|  | 4328 | *consumed = ptr - buffer; | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4329 | binder_inner_proc_lock(proc); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4330 | if (proc->requested_threads == 0 && | 
|  | 4331 | list_empty(&thread->proc->waiting_threads) && | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4332 | proc->requested_threads_started < proc->max_threads && | 
|  | 4333 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | | 
|  | 4334 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ | 
|  | 4335 | /*spawn a new thread if we leave this out */) { | 
|  | 4336 | proc->requested_threads++; | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4337 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4338 | binder_debug(BINDER_DEBUG_THREADS, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4339 | "%d:%d BR_SPAWN_LOOPER\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4340 | proc->pid, thread->pid); | 
|  | 4341 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) | 
|  | 4342 | return -EFAULT; | 
| Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4343 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4344 | } else | 
|  | 4345 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4346 | return 0; | 
|  | 4347 | } | 
|  | 4348 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4349 | static void binder_release_work(struct binder_proc *proc, | 
|  | 4350 | struct list_head *list) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4351 | { | 
|  | 4352 | struct binder_work *w; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4353 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4354 | while (1) { | 
|  | 4355 | w = binder_dequeue_work_head(proc, list); | 
|  | 4356 | if (!w) | 
|  | 4357 | return; | 
|  | 4358 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4359 | switch (w->type) { | 
|  | 4360 | case BINDER_WORK_TRANSACTION: { | 
|  | 4361 | struct binder_transaction *t; | 
|  | 4362 |  | 
|  | 4363 | t = container_of(w, struct binder_transaction, work); | 
| Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4364 |  | 
|  | 4365 | binder_cleanup_transaction(t, "process died.", | 
|  | 4366 | BR_DEAD_REPLY); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4367 | } break; | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4368 | case BINDER_WORK_RETURN_ERROR: { | 
|  | 4369 | struct binder_error *e = container_of( | 
|  | 4370 | w, struct binder_error, work); | 
|  | 4371 |  | 
|  | 4372 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | 
|  | 4373 | "undelivered TRANSACTION_ERROR: %u\n", | 
|  | 4374 | e->cmd); | 
|  | 4375 | } break; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4376 | case BINDER_WORK_TRANSACTION_COMPLETE: { | 
| Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4377 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4378 | "undelivered TRANSACTION_COMPLETE\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4379 | kfree(w); | 
|  | 4380 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); | 
|  | 4381 | } break; | 
| Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4382 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: | 
|  | 4383 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { | 
|  | 4384 | struct binder_ref_death *death; | 
|  | 4385 |  | 
|  | 4386 | death = container_of(w, struct binder_ref_death, work); | 
|  | 4387 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4388 | "undelivered death notification, %016llx\n", | 
|  | 4389 | (u64)death->cookie); | 
| Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4390 | kfree(death); | 
|  | 4391 | binder_stats_deleted(BINDER_STAT_DEATH); | 
|  | 4392 | } break; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4393 | default: | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4394 | pr_err("unexpected work type, %d, not freed\n", | 
| Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4395 | w->type); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4396 | break; | 
|  | 4397 | } | 
|  | 4398 | } | 
|  | 4399 |  | 
|  | 4400 | } | 
|  | 4401 |  | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4402 | static struct binder_thread *binder_get_thread_ilocked( | 
|  | 4403 | struct binder_proc *proc, struct binder_thread *new_thread) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4404 | { | 
|  | 4405 | struct binder_thread *thread = NULL; | 
|  | 4406 | struct rb_node *parent = NULL; | 
|  | 4407 | struct rb_node **p = &proc->threads.rb_node; | 
|  | 4408 |  | 
|  | 4409 | while (*p) { | 
|  | 4410 | parent = *p; | 
|  | 4411 | thread = rb_entry(parent, struct binder_thread, rb_node); | 
|  | 4412 |  | 
|  | 4413 | if (current->pid < thread->pid) | 
|  | 4414 | p = &(*p)->rb_left; | 
|  | 4415 | else if (current->pid > thread->pid) | 
|  | 4416 | p = &(*p)->rb_right; | 
|  | 4417 | else | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4418 | return thread; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4419 | } | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4420 | if (!new_thread) | 
|  | 4421 | return NULL; | 
|  | 4422 | thread = new_thread; | 
|  | 4423 | binder_stats_created(BINDER_STAT_THREAD); | 
|  | 4424 | thread->proc = proc; | 
|  | 4425 | thread->pid = current->pid; | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4426 | get_task_struct(current); | 
|  | 4427 | thread->task = current; | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4428 | atomic_set(&thread->tmp_ref, 0); | 
|  | 4429 | init_waitqueue_head(&thread->wait); | 
|  | 4430 | INIT_LIST_HEAD(&thread->todo); | 
|  | 4431 | rb_link_node(&thread->rb_node, parent, p); | 
|  | 4432 | rb_insert_color(&thread->rb_node, &proc->threads); | 
|  | 4433 | thread->looper_need_return = true; | 
|  | 4434 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; | 
|  | 4435 | thread->return_error.cmd = BR_OK; | 
|  | 4436 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; | 
|  | 4437 | thread->reply_error.cmd = BR_OK; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4438 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4439 | return thread; | 
|  | 4440 | } | 
|  | 4441 |  | 
|  | 4442 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) | 
|  | 4443 | { | 
|  | 4444 | struct binder_thread *thread; | 
|  | 4445 | struct binder_thread *new_thread; | 
|  | 4446 |  | 
|  | 4447 | binder_inner_proc_lock(proc); | 
|  | 4448 | thread = binder_get_thread_ilocked(proc, NULL); | 
|  | 4449 | binder_inner_proc_unlock(proc); | 
|  | 4450 | if (!thread) { | 
|  | 4451 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); | 
|  | 4452 | if (new_thread == NULL) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4453 | return NULL; | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4454 | binder_inner_proc_lock(proc); | 
|  | 4455 | thread = binder_get_thread_ilocked(proc, new_thread); | 
|  | 4456 | binder_inner_proc_unlock(proc); | 
|  | 4457 | if (thread != new_thread) | 
|  | 4458 | kfree(new_thread); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4459 | } | 
|  | 4460 | return thread; | 
|  | 4461 | } | 
|  | 4462 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4463 | static void binder_free_proc(struct binder_proc *proc) | 
|  | 4464 | { | 
|  | 4465 | BUG_ON(!list_empty(&proc->todo)); | 
|  | 4466 | BUG_ON(!list_empty(&proc->delivered_death)); | 
|  | 4467 | binder_alloc_deferred_release(&proc->alloc); | 
|  | 4468 | put_task_struct(proc->tsk); | 
|  | 4469 | binder_stats_deleted(BINDER_STAT_PROC); | 
|  | 4470 | kfree(proc); | 
|  | 4471 | } | 
|  | 4472 |  | 
|  | 4473 | static void binder_free_thread(struct binder_thread *thread) | 
|  | 4474 | { | 
|  | 4475 | BUG_ON(!list_empty(&thread->todo)); | 
|  | 4476 | binder_stats_deleted(BINDER_STAT_THREAD); | 
|  | 4477 | binder_proc_dec_tmpref(thread->proc); | 
| Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4478 | put_task_struct(thread->task); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4479 | kfree(thread); | 
|  | 4480 | } | 
|  | 4481 |  | 
|  | 4482 | static int binder_thread_release(struct binder_proc *proc, | 
|  | 4483 | struct binder_thread *thread) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4484 | { | 
|  | 4485 | struct binder_transaction *t; | 
|  | 4486 | struct binder_transaction *send_reply = NULL; | 
|  | 4487 | int active_transactions = 0; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4488 | struct binder_transaction *last_t = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4489 |  | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4490 | binder_inner_proc_lock(thread->proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4491 | /* | 
|  | 4492 | * take a ref on the proc so it survives | 
|  | 4493 | * after we remove this thread from proc->threads. | 
|  | 4494 | * The corresponding dec is when we actually | 
|  | 4495 | * free the thread in binder_free_thread() | 
|  | 4496 | */ | 
|  | 4497 | proc->tmp_ref++; | 
|  | 4498 | /* | 
|  | 4499 | * take a ref on this thread to ensure it | 
|  | 4500 | * survives while we are releasing it | 
|  | 4501 | */ | 
|  | 4502 | atomic_inc(&thread->tmp_ref); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4503 | rb_erase(&thread->rb_node, &proc->threads); | 
|  | 4504 | t = thread->transaction_stack; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4505 | if (t) { | 
|  | 4506 | spin_lock(&t->lock); | 
|  | 4507 | if (t->to_thread == thread) | 
|  | 4508 | send_reply = t; | 
|  | 4509 | } | 
|  | 4510 | thread->is_dead = true; | 
|  | 4511 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4512 | while (t) { | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4513 | last_t = t; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4514 | active_transactions++; | 
|  | 4515 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4516 | "release %d:%d transaction %d %s, still active\n", | 
|  | 4517 | proc->pid, thread->pid, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4518 | t->debug_id, | 
|  | 4519 | (t->to_thread == thread) ? "in" : "out"); | 
|  | 4520 |  | 
|  | 4521 | if (t->to_thread == thread) { | 
|  | 4522 | t->to_proc = NULL; | 
|  | 4523 | t->to_thread = NULL; | 
|  | 4524 | if (t->buffer) { | 
|  | 4525 | t->buffer->transaction = NULL; | 
|  | 4526 | t->buffer = NULL; | 
|  | 4527 | } | 
|  | 4528 | t = t->to_parent; | 
|  | 4529 | } else if (t->from == thread) { | 
|  | 4530 | t->from = NULL; | 
|  | 4531 | t = t->from_parent; | 
|  | 4532 | } else | 
|  | 4533 | BUG(); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4534 | spin_unlock(&last_t->lock); | 
|  | 4535 | if (t) | 
|  | 4536 | spin_lock(&t->lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4537 | } | 
| Martijn Coenen | 550c01d | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4538 |  | 
|  | 4539 | /* | 
|  | 4540 | * If this thread used poll, make sure we remove the waitqueue | 
|  | 4541 | * from any epoll data structures holding it with POLLFREE. | 
|  | 4542 | * waitqueue_active() is safe to use here because we're holding | 
|  | 4543 | * the inner lock. | 
|  | 4544 | */ | 
|  | 4545 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && | 
|  | 4546 | waitqueue_active(&thread->wait)) { | 
|  | 4547 | wake_up_poll(&thread->wait, POLLHUP | POLLFREE); | 
|  | 4548 | } | 
|  | 4549 |  | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4550 | binder_inner_proc_unlock(thread->proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4551 |  | 
| Martijn Coenen | 72766d7 | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4552 | /* | 
|  | 4553 | * This is needed to avoid races between wake_up_poll() above and | 
|  | 4554 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file | 
|  | 4555 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read | 
|  | 4556 | * lock, so we can be sure it's done after calling synchronize_rcu(). | 
|  | 4557 | */ | 
|  | 4558 | if (thread->looper & BINDER_LOOPER_STATE_POLL) | 
|  | 4559 | synchronize_rcu(); | 
|  | 4560 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4561 | if (send_reply) | 
|  | 4562 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4563 | binder_release_work(proc, &thread->todo); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4564 | binder_thread_dec_tmpref(thread); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4565 | return active_transactions; | 
|  | 4566 | } | 
|  | 4567 |  | 
|  | 4568 | static unsigned int binder_poll(struct file *filp, | 
|  | 4569 | struct poll_table_struct *wait) | 
|  | 4570 | { | 
|  | 4571 | struct binder_proc *proc = filp->private_data; | 
|  | 4572 | struct binder_thread *thread = NULL; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4573 | bool wait_for_proc_work; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4574 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4575 | thread = binder_get_thread(proc); | 
| Eric Biggers | 4be5a28 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4576 | if (!thread) | 
|  | 4577 | return POLLERR; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4578 |  | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4579 | binder_inner_proc_lock(thread->proc); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4580 | thread->looper |= BINDER_LOOPER_STATE_POLL; | 
|  | 4581 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); | 
|  | 4582 |  | 
| Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4583 | binder_inner_proc_unlock(thread->proc); | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4584 |  | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4585 | poll_wait(filp, &thread->wait, wait); | 
|  | 4586 |  | 
| Martijn Coenen | 4781093 | 2017-08-10 12:32:00 +0200 | [diff] [blame] | 4587 | if (binder_has_work(thread, wait_for_proc_work)) | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4588 | return POLLIN; | 
|  | 4589 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4590 | return 0; | 
|  | 4591 | } | 
|  | 4592 |  | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4593 | static int binder_ioctl_write_read(struct file *filp, | 
|  | 4594 | unsigned int cmd, unsigned long arg, | 
|  | 4595 | struct binder_thread *thread) | 
|  | 4596 | { | 
|  | 4597 | int ret = 0; | 
|  | 4598 | struct binder_proc *proc = filp->private_data; | 
|  | 4599 | unsigned int size = _IOC_SIZE(cmd); | 
|  | 4600 | void __user *ubuf = (void __user *)arg; | 
|  | 4601 | struct binder_write_read bwr; | 
|  | 4602 |  | 
|  | 4603 | if (size != sizeof(struct binder_write_read)) { | 
|  | 4604 | ret = -EINVAL; | 
|  | 4605 | goto out; | 
|  | 4606 | } | 
|  | 4607 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { | 
|  | 4608 | ret = -EFAULT; | 
|  | 4609 | goto out; | 
|  | 4610 | } | 
|  | 4611 | binder_debug(BINDER_DEBUG_READ_WRITE, | 
|  | 4612 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", | 
|  | 4613 | proc->pid, thread->pid, | 
|  | 4614 | (u64)bwr.write_size, (u64)bwr.write_buffer, | 
|  | 4615 | (u64)bwr.read_size, (u64)bwr.read_buffer); | 
|  | 4616 |  | 
|  | 4617 | if (bwr.write_size > 0) { | 
|  | 4618 | ret = binder_thread_write(proc, thread, | 
|  | 4619 | bwr.write_buffer, | 
|  | 4620 | bwr.write_size, | 
|  | 4621 | &bwr.write_consumed); | 
|  | 4622 | trace_binder_write_done(ret); | 
|  | 4623 | if (ret < 0) { | 
|  | 4624 | bwr.read_consumed = 0; | 
|  | 4625 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) | 
|  | 4626 | ret = -EFAULT; | 
|  | 4627 | goto out; | 
|  | 4628 | } | 
|  | 4629 | } | 
|  | 4630 | if (bwr.read_size > 0) { | 
|  | 4631 | ret = binder_thread_read(proc, thread, bwr.read_buffer, | 
|  | 4632 | bwr.read_size, | 
|  | 4633 | &bwr.read_consumed, | 
|  | 4634 | filp->f_flags & O_NONBLOCK); | 
|  | 4635 | trace_binder_read_done(ret); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4636 | binder_inner_proc_lock(proc); | 
|  | 4637 | if (!binder_worklist_empty_ilocked(&proc->todo)) | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4638 | binder_wakeup_proc_ilocked(proc); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4639 | binder_inner_proc_unlock(proc); | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4640 | if (ret < 0) { | 
|  | 4641 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) | 
|  | 4642 | ret = -EFAULT; | 
|  | 4643 | goto out; | 
|  | 4644 | } | 
|  | 4645 | } | 
|  | 4646 | binder_debug(BINDER_DEBUG_READ_WRITE, | 
|  | 4647 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", | 
|  | 4648 | proc->pid, thread->pid, | 
|  | 4649 | (u64)bwr.write_consumed, (u64)bwr.write_size, | 
|  | 4650 | (u64)bwr.read_consumed, (u64)bwr.read_size); | 
|  | 4651 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { | 
|  | 4652 | ret = -EFAULT; | 
|  | 4653 | goto out; | 
|  | 4654 | } | 
|  | 4655 | out: | 
|  | 4656 | return ret; | 
|  | 4657 | } | 
|  | 4658 |  | 
|  | 4659 | static int binder_ioctl_set_ctx_mgr(struct file *filp) | 
|  | 4660 | { | 
|  | 4661 | int ret = 0; | 
|  | 4662 | struct binder_proc *proc = filp->private_data; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4663 | struct binder_context *context = proc->context; | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4664 | struct binder_node *new_node; | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4665 | kuid_t curr_euid = current_euid(); | 
|  | 4666 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4667 | mutex_lock(&context->context_mgr_node_lock); | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4668 | if (context->binder_context_mgr_node) { | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4669 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); | 
|  | 4670 | ret = -EBUSY; | 
|  | 4671 | goto out; | 
|  | 4672 | } | 
| Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4673 | ret = security_binder_set_context_mgr(proc->tsk); | 
|  | 4674 | if (ret < 0) | 
|  | 4675 | goto out; | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4676 | if (uid_valid(context->binder_context_mgr_uid)) { | 
|  | 4677 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4678 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", | 
|  | 4679 | from_kuid(&init_user_ns, curr_euid), | 
|  | 4680 | from_kuid(&init_user_ns, | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4681 | context->binder_context_mgr_uid)); | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4682 | ret = -EPERM; | 
|  | 4683 | goto out; | 
|  | 4684 | } | 
|  | 4685 | } else { | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4686 | context->binder_context_mgr_uid = curr_euid; | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4687 | } | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4688 | new_node = binder_new_node(proc, NULL); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4689 | if (!new_node) { | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4690 | ret = -ENOMEM; | 
|  | 4691 | goto out; | 
|  | 4692 | } | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4693 | binder_node_lock(new_node); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4694 | new_node->local_weak_refs++; | 
|  | 4695 | new_node->local_strong_refs++; | 
|  | 4696 | new_node->has_strong_ref = 1; | 
|  | 4697 | new_node->has_weak_ref = 1; | 
|  | 4698 | context->binder_context_mgr_node = new_node; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4699 | binder_node_unlock(new_node); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4700 | binder_put_node(new_node); | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4701 | out: | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4702 | mutex_unlock(&context->context_mgr_node_lock); | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4703 | return ret; | 
|  | 4704 | } | 
|  | 4705 |  | 
| Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4706 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, | 
|  | 4707 | struct binder_node_debug_info *info) { | 
|  | 4708 | struct rb_node *n; | 
|  | 4709 | binder_uintptr_t ptr = info->ptr; | 
|  | 4710 |  | 
|  | 4711 | memset(info, 0, sizeof(*info)); | 
|  | 4712 |  | 
|  | 4713 | binder_inner_proc_lock(proc); | 
|  | 4714 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { | 
|  | 4715 | struct binder_node *node = rb_entry(n, struct binder_node, | 
|  | 4716 | rb_node); | 
|  | 4717 | if (node->ptr > ptr) { | 
|  | 4718 | info->ptr = node->ptr; | 
|  | 4719 | info->cookie = node->cookie; | 
|  | 4720 | info->has_strong_ref = node->has_strong_ref; | 
|  | 4721 | info->has_weak_ref = node->has_weak_ref; | 
|  | 4722 | break; | 
|  | 4723 | } | 
|  | 4724 | } | 
|  | 4725 | binder_inner_proc_unlock(proc); | 
|  | 4726 |  | 
|  | 4727 | return 0; | 
|  | 4728 | } | 
|  | 4729 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4730 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | 
|  | 4731 | { | 
|  | 4732 | int ret; | 
|  | 4733 | struct binder_proc *proc = filp->private_data; | 
|  | 4734 | struct binder_thread *thread; | 
|  | 4735 | unsigned int size = _IOC_SIZE(cmd); | 
|  | 4736 | void __user *ubuf = (void __user *)arg; | 
|  | 4737 |  | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4738 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", | 
|  | 4739 | proc->pid, current->pid, cmd, arg);*/ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4740 |  | 
| Sherry Yang | 435416b | 2017-06-22 14:37:45 -0700 | [diff] [blame] | 4741 | binder_selftest_alloc(&proc->alloc); | 
|  | 4742 |  | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4743 | trace_binder_ioctl(cmd, arg); | 
|  | 4744 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4745 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); | 
|  | 4746 | if (ret) | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4747 | goto err_unlocked; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4748 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4749 | thread = binder_get_thread(proc); | 
|  | 4750 | if (thread == NULL) { | 
|  | 4751 | ret = -ENOMEM; | 
|  | 4752 | goto err; | 
|  | 4753 | } | 
|  | 4754 |  | 
|  | 4755 | switch (cmd) { | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4756 | case BINDER_WRITE_READ: | 
|  | 4757 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); | 
|  | 4758 | if (ret) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4759 | goto err; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4760 | break; | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4761 | case BINDER_SET_MAX_THREADS: { | 
|  | 4762 | int max_threads; | 
|  | 4763 |  | 
|  | 4764 | if (copy_from_user(&max_threads, ubuf, | 
|  | 4765 | sizeof(max_threads))) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4766 | ret = -EINVAL; | 
|  | 4767 | goto err; | 
|  | 4768 | } | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4769 | binder_inner_proc_lock(proc); | 
|  | 4770 | proc->max_threads = max_threads; | 
|  | 4771 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4772 | break; | 
| Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4773 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4774 | case BINDER_SET_CONTEXT_MGR: | 
| Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4775 | ret = binder_ioctl_set_ctx_mgr(filp); | 
|  | 4776 | if (ret) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4777 | goto err; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4778 | break; | 
|  | 4779 | case BINDER_THREAD_EXIT: | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4780 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4781 | proc->pid, thread->pid); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4782 | binder_thread_release(proc, thread); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4783 | thread = NULL; | 
|  | 4784 | break; | 
| Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4785 | case BINDER_VERSION: { | 
|  | 4786 | struct binder_version __user *ver = ubuf; | 
|  | 4787 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4788 | if (size != sizeof(struct binder_version)) { | 
|  | 4789 | ret = -EINVAL; | 
|  | 4790 | goto err; | 
|  | 4791 | } | 
| Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4792 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, | 
|  | 4793 | &ver->protocol_version)) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4794 | ret = -EINVAL; | 
|  | 4795 | goto err; | 
|  | 4796 | } | 
|  | 4797 | break; | 
| Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4798 | } | 
| Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4799 | case BINDER_GET_NODE_DEBUG_INFO: { | 
|  | 4800 | struct binder_node_debug_info info; | 
|  | 4801 |  | 
|  | 4802 | if (copy_from_user(&info, ubuf, sizeof(info))) { | 
|  | 4803 | ret = -EFAULT; | 
|  | 4804 | goto err; | 
|  | 4805 | } | 
|  | 4806 |  | 
|  | 4807 | ret = binder_ioctl_get_node_debug_info(proc, &info); | 
|  | 4808 | if (ret < 0) | 
|  | 4809 | goto err; | 
|  | 4810 |  | 
|  | 4811 | if (copy_to_user(ubuf, &info, sizeof(info))) { | 
|  | 4812 | ret = -EFAULT; | 
|  | 4813 | goto err; | 
|  | 4814 | } | 
|  | 4815 | break; | 
|  | 4816 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4817 | default: | 
|  | 4818 | ret = -EINVAL; | 
|  | 4819 | goto err; | 
|  | 4820 | } | 
|  | 4821 | ret = 0; | 
|  | 4822 | err: | 
|  | 4823 | if (thread) | 
| Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4824 | thread->looper_need_return = false; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4825 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); | 
|  | 4826 | if (ret && ret != -ERESTARTSYS) | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4827 | pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret); | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4828 | err_unlocked: | 
|  | 4829 | trace_binder_ioctl_done(ret); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4830 | return ret; | 
|  | 4831 | } | 
|  | 4832 |  | 
|  | 4833 | static void binder_vma_open(struct vm_area_struct *vma) | 
|  | 4834 | { | 
|  | 4835 | struct binder_proc *proc = vma->vm_private_data; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4836 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4837 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4838 | "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4839 | proc->pid, vma->vm_start, vma->vm_end, | 
|  | 4840 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, | 
|  | 4841 | (unsigned long)pgprot_val(vma->vm_page_prot)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4842 | } | 
|  | 4843 |  | 
|  | 4844 | static void binder_vma_close(struct vm_area_struct *vma) | 
|  | 4845 | { | 
|  | 4846 | struct binder_proc *proc = vma->vm_private_data; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4847 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4848 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | 
| Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4849 | "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4850 | proc->pid, vma->vm_start, vma->vm_end, | 
|  | 4851 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, | 
|  | 4852 | (unsigned long)pgprot_val(vma->vm_page_prot)); | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4853 | binder_alloc_vma_close(&proc->alloc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4854 | } | 
|  | 4855 |  | 
| Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4856 | static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | 
|  | 4857 | { | 
|  | 4858 | return VM_FAULT_SIGBUS; | 
|  | 4859 | } | 
|  | 4860 |  | 
| Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4861 | static const struct vm_operations_struct binder_vm_ops = { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4862 | .open = binder_vma_open, | 
|  | 4863 | .close = binder_vma_close, | 
| Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4864 | .fault = binder_vm_fault, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4865 | }; | 
|  | 4866 |  | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4867 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) | 
|  | 4868 | { | 
|  | 4869 | int ret; | 
|  | 4870 | struct binder_proc *proc = filp->private_data; | 
|  | 4871 | const char *failure_string; | 
|  | 4872 |  | 
|  | 4873 | if (proc->tsk != current->group_leader) | 
|  | 4874 | return -EINVAL; | 
|  | 4875 |  | 
|  | 4876 | if ((vma->vm_end - vma->vm_start) > SZ_4M) | 
|  | 4877 | vma->vm_end = vma->vm_start + SZ_4M; | 
|  | 4878 |  | 
|  | 4879 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | 
|  | 4880 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", | 
|  | 4881 | __func__, proc->pid, vma->vm_start, vma->vm_end, | 
|  | 4882 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, | 
|  | 4883 | (unsigned long)pgprot_val(vma->vm_page_prot)); | 
|  | 4884 |  | 
|  | 4885 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { | 
|  | 4886 | ret = -EPERM; | 
|  | 4887 | failure_string = "bad vm_flags"; | 
|  | 4888 | goto err_bad_arg; | 
|  | 4889 | } | 
|  | 4890 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; | 
|  | 4891 | vma->vm_ops = &binder_vm_ops; | 
|  | 4892 | vma->vm_private_data = proc; | 
|  | 4893 |  | 
|  | 4894 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); | 
| Todd Kjos | f09daf1 | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 4895 |  | 
|  | 4896 | return ret; | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4897 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4898 | err_bad_arg: | 
| Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 4899 | pr_err("binder_mmap: %d %lx-%lx %s failed %d\n", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4900 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); | 
|  | 4901 | return ret; | 
|  | 4902 | } | 
|  | 4903 |  | 
|  | 4904 | static int binder_open(struct inode *nodp, struct file *filp) | 
|  | 4905 | { | 
|  | 4906 | struct binder_proc *proc; | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 4907 | struct binder_device *binder_dev; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4908 |  | 
|  | 4909 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n", | 
|  | 4910 | current->group_leader->pid, current->pid); | 
|  | 4911 |  | 
|  | 4912 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); | 
|  | 4913 | if (proc == NULL) | 
|  | 4914 | return -ENOMEM; | 
| Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 4915 | spin_lock_init(&proc->inner_lock); | 
|  | 4916 | spin_lock_init(&proc->outer_lock); | 
| Martijn Coenen | 872c26e | 2017-03-07 15:51:18 +0100 | [diff] [blame] | 4917 | get_task_struct(current->group_leader); | 
|  | 4918 | proc->tsk = current->group_leader; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4919 | INIT_LIST_HEAD(&proc->todo); | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 4920 | if (binder_supported_policy(current->policy)) { | 
|  | 4921 | proc->default_priority.sched_policy = current->policy; | 
|  | 4922 | proc->default_priority.prio = current->normal_prio; | 
|  | 4923 | } else { | 
|  | 4924 | proc->default_priority.sched_policy = SCHED_NORMAL; | 
|  | 4925 | proc->default_priority.prio = NICE_TO_PRIO(0); | 
|  | 4926 | } | 
|  | 4927 |  | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 4928 | binder_dev = container_of(filp->private_data, struct binder_device, | 
|  | 4929 | miscdev); | 
|  | 4930 | proc->context = &binder_dev->context; | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4931 | binder_alloc_init(&proc->alloc); | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4932 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4933 | binder_stats_created(BINDER_STAT_PROC); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4934 | proc->pid = current->group_leader->pid; | 
|  | 4935 | INIT_LIST_HEAD(&proc->delivered_death); | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4936 | INIT_LIST_HEAD(&proc->waiting_threads); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4937 | filp->private_data = proc; | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4938 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4939 | mutex_lock(&binder_procs_lock); | 
|  | 4940 | hlist_add_head(&proc->proc_node, &binder_procs); | 
|  | 4941 | mutex_unlock(&binder_procs_lock); | 
|  | 4942 |  | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4943 | if (binder_debugfs_dir_entry_proc) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4944 | char strbuf[11]; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4945 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4946 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 4947 | /* | 
|  | 4948 | * proc debug entries are shared between contexts, so | 
|  | 4949 | * this will fail if the process tries to open the driver | 
|  | 4950 | * again with a different context. The priting code will | 
|  | 4951 | * anyway print all contexts that a given PID has, so this | 
|  | 4952 | * is not a problem. | 
|  | 4953 | */ | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4954 | proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO, | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 4955 | binder_debugfs_dir_entry_proc, | 
|  | 4956 | (void *)(unsigned long)proc->pid, | 
|  | 4957 | &binder_proc_fops); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4958 | } | 
|  | 4959 |  | 
|  | 4960 | return 0; | 
|  | 4961 | } | 
|  | 4962 |  | 
|  | 4963 | static int binder_flush(struct file *filp, fl_owner_t id) | 
|  | 4964 | { | 
|  | 4965 | struct binder_proc *proc = filp->private_data; | 
|  | 4966 |  | 
|  | 4967 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); | 
|  | 4968 |  | 
|  | 4969 | return 0; | 
|  | 4970 | } | 
|  | 4971 |  | 
|  | 4972 | static void binder_deferred_flush(struct binder_proc *proc) | 
|  | 4973 | { | 
|  | 4974 | struct rb_node *n; | 
|  | 4975 | int wake_count = 0; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4976 |  | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4977 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4978 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { | 
|  | 4979 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4980 |  | 
| Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4981 | thread->looper_need_return = true; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4982 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { | 
|  | 4983 | wake_up_interruptible(&thread->wait); | 
|  | 4984 | wake_count++; | 
|  | 4985 | } | 
|  | 4986 | } | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4987 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4988 |  | 
|  | 4989 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | 
|  | 4990 | "binder_flush: %d woke %d threads\n", proc->pid, | 
|  | 4991 | wake_count); | 
|  | 4992 | } | 
|  | 4993 |  | 
|  | 4994 | static int binder_release(struct inode *nodp, struct file *filp) | 
|  | 4995 | { | 
|  | 4996 | struct binder_proc *proc = filp->private_data; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4997 |  | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4998 | debugfs_remove(proc->debugfs_entry); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4999 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); | 
|  | 5000 |  | 
|  | 5001 | return 0; | 
|  | 5002 | } | 
|  | 5003 |  | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5004 | static int binder_node_release(struct binder_node *node, int refs) | 
|  | 5005 | { | 
|  | 5006 | struct binder_ref *ref; | 
|  | 5007 | int death = 0; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5008 | struct binder_proc *proc = node->proc; | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5009 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5010 | binder_release_work(proc, &node->async_todo); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5011 |  | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5012 | binder_node_lock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5013 | binder_inner_proc_lock(proc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5014 | binder_dequeue_work_ilocked(&node->work); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5015 | /* | 
|  | 5016 | * The caller must have taken a temporary ref on the node, | 
|  | 5017 | */ | 
|  | 5018 | BUG_ON(!node->tmp_refs); | 
|  | 5019 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5020 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5021 | binder_node_unlock(node); | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5022 | binder_free_node(node); | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5023 |  | 
|  | 5024 | return refs; | 
|  | 5025 | } | 
|  | 5026 |  | 
|  | 5027 | node->proc = NULL; | 
|  | 5028 | node->local_strong_refs = 0; | 
|  | 5029 | node->local_weak_refs = 0; | 
| Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5030 | binder_inner_proc_unlock(proc); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5031 |  | 
|  | 5032 | spin_lock(&binder_dead_nodes_lock); | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5033 | hlist_add_head(&node->dead_node, &binder_dead_nodes); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5034 | spin_unlock(&binder_dead_nodes_lock); | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5035 |  | 
|  | 5036 | hlist_for_each_entry(ref, &node->refs, node_entry) { | 
|  | 5037 | refs++; | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5038 | /* | 
|  | 5039 | * Need the node lock to synchronize | 
|  | 5040 | * with new notification requests and the | 
|  | 5041 | * inner lock to synchronize with queued | 
|  | 5042 | * death notifications. | 
|  | 5043 | */ | 
|  | 5044 | binder_inner_proc_lock(ref->proc); | 
|  | 5045 | if (!ref->death) { | 
|  | 5046 | binder_inner_proc_unlock(ref->proc); | 
| Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 5047 | continue; | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5048 | } | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5049 |  | 
|  | 5050 | death++; | 
|  | 5051 |  | 
| Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5052 | BUG_ON(!list_empty(&ref->death->work.entry)); | 
|  | 5053 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; | 
|  | 5054 | binder_enqueue_work_ilocked(&ref->death->work, | 
|  | 5055 | &ref->proc->todo); | 
| Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 5056 | binder_wakeup_proc_ilocked(ref->proc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5057 | binder_inner_proc_unlock(ref->proc); | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5058 | } | 
|  | 5059 |  | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5060 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | 
|  | 5061 | "node %d now dead, refs %d, death %d\n", | 
|  | 5062 | node->debug_id, refs, death); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5063 | binder_node_unlock(node); | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5064 | binder_put_node(node); | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5065 |  | 
|  | 5066 | return refs; | 
|  | 5067 | } | 
|  | 5068 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5069 | static void binder_deferred_release(struct binder_proc *proc) | 
|  | 5070 | { | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5071 | struct binder_context *context = proc->context; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5072 | struct rb_node *n; | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5073 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5074 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5075 | mutex_lock(&binder_procs_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5076 | hlist_del(&proc->proc_node); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5077 | mutex_unlock(&binder_procs_lock); | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5078 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5079 | mutex_lock(&context->context_mgr_node_lock); | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5080 | if (context->binder_context_mgr_node && | 
|  | 5081 | context->binder_context_mgr_node->proc == proc) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5082 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | 
| Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 5083 | "%s: %d context_mgr_node gone\n", | 
|  | 5084 | __func__, proc->pid); | 
| Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5085 | context->binder_context_mgr_node = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5086 | } | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5087 | mutex_unlock(&context->context_mgr_node_lock); | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5088 | binder_inner_proc_lock(proc); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5089 | /* | 
|  | 5090 | * Make sure proc stays alive after we | 
|  | 5091 | * remove all the threads | 
|  | 5092 | */ | 
|  | 5093 | proc->tmp_ref++; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5094 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5095 | proc->is_dead = true; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5096 | threads = 0; | 
|  | 5097 | active_transactions = 0; | 
|  | 5098 | while ((n = rb_first(&proc->threads))) { | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5099 | struct binder_thread *thread; | 
|  | 5100 |  | 
|  | 5101 | thread = rb_entry(n, struct binder_thread, rb_node); | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5102 | binder_inner_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5103 | threads++; | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5104 | active_transactions += binder_thread_release(proc, thread); | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5105 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5106 | } | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5107 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5108 | nodes = 0; | 
|  | 5109 | incoming_refs = 0; | 
|  | 5110 | while ((n = rb_first(&proc->nodes))) { | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5111 | struct binder_node *node; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5112 |  | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5113 | node = rb_entry(n, struct binder_node, rb_node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5114 | nodes++; | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5115 | /* | 
|  | 5116 | * take a temporary ref on the node before | 
|  | 5117 | * calling binder_node_release() which will either | 
|  | 5118 | * kfree() the node or call binder_put_node() | 
|  | 5119 | */ | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5120 | binder_inc_node_tmpref_ilocked(node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5121 | rb_erase(&node->rb_node, &proc->nodes); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5122 | binder_inner_proc_unlock(proc); | 
| Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5123 | incoming_refs = binder_node_release(node, incoming_refs); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5124 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5125 | } | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5126 | binder_inner_proc_unlock(proc); | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5127 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5128 | outgoing_refs = 0; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5129 | binder_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5130 | while ((n = rb_first(&proc->refs_by_desc))) { | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5131 | struct binder_ref *ref; | 
|  | 5132 |  | 
|  | 5133 | ref = rb_entry(n, struct binder_ref, rb_node_desc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5134 | outgoing_refs++; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5135 | binder_cleanup_ref_olocked(ref); | 
|  | 5136 | binder_proc_unlock(proc); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5137 | binder_free_ref(ref); | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5138 | binder_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5139 | } | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5140 | binder_proc_unlock(proc); | 
| Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5141 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5142 | binder_release_work(proc, &proc->todo); | 
|  | 5143 | binder_release_work(proc, &proc->delivered_death); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5144 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5145 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5146 | "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n", | 
| Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 5147 | __func__, proc->pid, threads, nodes, incoming_refs, | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5148 | outgoing_refs, active_transactions); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5149 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5150 | binder_proc_dec_tmpref(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5151 | } | 
|  | 5152 |  | 
|  | 5153 | static void binder_deferred_func(struct work_struct *work) | 
|  | 5154 | { | 
|  | 5155 | struct binder_proc *proc; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5156 | int defer; | 
| Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5157 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5158 | do { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5159 | mutex_lock(&binder_deferred_lock); | 
|  | 5160 | if (!hlist_empty(&binder_deferred_list)) { | 
|  | 5161 | proc = hlist_entry(binder_deferred_list.first, | 
|  | 5162 | struct binder_proc, deferred_work_node); | 
|  | 5163 | hlist_del_init(&proc->deferred_work_node); | 
|  | 5164 | defer = proc->deferred_work; | 
|  | 5165 | proc->deferred_work = 0; | 
|  | 5166 | } else { | 
|  | 5167 | proc = NULL; | 
|  | 5168 | defer = 0; | 
|  | 5169 | } | 
|  | 5170 | mutex_unlock(&binder_deferred_lock); | 
|  | 5171 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5172 | if (defer & BINDER_DEFERRED_FLUSH) | 
|  | 5173 | binder_deferred_flush(proc); | 
|  | 5174 |  | 
|  | 5175 | if (defer & BINDER_DEFERRED_RELEASE) | 
|  | 5176 | binder_deferred_release(proc); /* frees proc */ | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5177 | } while (proc); | 
|  | 5178 | } | 
|  | 5179 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); | 
|  | 5180 |  | 
|  | 5181 | static void | 
|  | 5182 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) | 
|  | 5183 | { | 
|  | 5184 | mutex_lock(&binder_deferred_lock); | 
|  | 5185 | proc->deferred_work |= defer; | 
|  | 5186 | if (hlist_unhashed(&proc->deferred_work_node)) { | 
|  | 5187 | hlist_add_head(&proc->deferred_work_node, | 
|  | 5188 | &binder_deferred_list); | 
| Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5189 | schedule_work(&binder_deferred_work); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5190 | } | 
|  | 5191 | mutex_unlock(&binder_deferred_lock); | 
|  | 5192 | } | 
|  | 5193 |  | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5194 | static void print_binder_transaction_ilocked(struct seq_file *m, | 
|  | 5195 | struct binder_proc *proc, | 
|  | 5196 | const char *prefix, | 
|  | 5197 | struct binder_transaction *t) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5198 | { | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5199 | struct binder_proc *to_proc; | 
|  | 5200 | struct binder_buffer *buffer = t->buffer; | 
|  | 5201 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5202 | spin_lock(&t->lock); | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5203 | to_proc = t->to_proc; | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5204 | seq_printf(m, | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5205 | "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d", | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5206 | prefix, t->debug_id, t, | 
|  | 5207 | t->from ? t->from->proc->pid : 0, | 
|  | 5208 | t->from ? t->from->pid : 0, | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5209 | to_proc ? to_proc->pid : 0, | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5210 | t->to_thread ? t->to_thread->pid : 0, | 
| Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5211 | t->code, t->flags, t->priority.sched_policy, | 
|  | 5212 | t->priority.prio, t->need_reply); | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5213 | spin_unlock(&t->lock); | 
|  | 5214 |  | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5215 | if (proc != to_proc) { | 
|  | 5216 | /* | 
|  | 5217 | * Can only safely deref buffer if we are holding the | 
|  | 5218 | * correct proc inner lock for this node | 
|  | 5219 | */ | 
|  | 5220 | seq_puts(m, "\n"); | 
|  | 5221 | return; | 
|  | 5222 | } | 
|  | 5223 |  | 
|  | 5224 | if (buffer == NULL) { | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5225 | seq_puts(m, " buffer free\n"); | 
|  | 5226 | return; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5227 | } | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5228 | if (buffer->target_node) | 
|  | 5229 | seq_printf(m, " node %d", buffer->target_node->debug_id); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5230 | seq_printf(m, " size %zd:%zd data %p\n", | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5231 | buffer->data_size, buffer->offsets_size, | 
|  | 5232 | buffer->data); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5233 | } | 
|  | 5234 |  | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5235 | static void print_binder_work_ilocked(struct seq_file *m, | 
|  | 5236 | struct binder_proc *proc, | 
|  | 5237 | const char *prefix, | 
|  | 5238 | const char *transaction_prefix, | 
|  | 5239 | struct binder_work *w) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5240 | { | 
|  | 5241 | struct binder_node *node; | 
|  | 5242 | struct binder_transaction *t; | 
|  | 5243 |  | 
|  | 5244 | switch (w->type) { | 
|  | 5245 | case BINDER_WORK_TRANSACTION: | 
|  | 5246 | t = container_of(w, struct binder_transaction, work); | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5247 | print_binder_transaction_ilocked( | 
|  | 5248 | m, proc, transaction_prefix, t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5249 | break; | 
| Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 5250 | case BINDER_WORK_RETURN_ERROR: { | 
|  | 5251 | struct binder_error *e = container_of( | 
|  | 5252 | w, struct binder_error, work); | 
|  | 5253 |  | 
|  | 5254 | seq_printf(m, "%stransaction error: %u\n", | 
|  | 5255 | prefix, e->cmd); | 
|  | 5256 | } break; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5257 | case BINDER_WORK_TRANSACTION_COMPLETE: | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5258 | seq_printf(m, "%stransaction complete\n", prefix); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5259 | break; | 
|  | 5260 | case BINDER_WORK_NODE: | 
|  | 5261 | node = container_of(w, struct binder_node, work); | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5262 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", | 
|  | 5263 | prefix, node->debug_id, | 
|  | 5264 | (u64)node->ptr, (u64)node->cookie); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5265 | break; | 
|  | 5266 | case BINDER_WORK_DEAD_BINDER: | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5267 | seq_printf(m, "%shas dead binder\n", prefix); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5268 | break; | 
|  | 5269 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5270 | seq_printf(m, "%shas cleared dead binder\n", prefix); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5271 | break; | 
|  | 5272 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5273 | seq_printf(m, "%shas cleared death notification\n", prefix); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5274 | break; | 
|  | 5275 | default: | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5276 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5277 | break; | 
|  | 5278 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5279 | } | 
|  | 5280 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5281 | static void print_binder_thread_ilocked(struct seq_file *m, | 
|  | 5282 | struct binder_thread *thread, | 
|  | 5283 | int print_always) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5284 | { | 
|  | 5285 | struct binder_transaction *t; | 
|  | 5286 | struct binder_work *w; | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5287 | size_t start_pos = m->count; | 
|  | 5288 | size_t header_pos; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5289 |  | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5290 | seq_printf(m, "  thread %d: l %02x need_return %d tr %d\n", | 
| Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 5291 | thread->pid, thread->looper, | 
| Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5292 | thread->looper_need_return, | 
|  | 5293 | atomic_read(&thread->tmp_ref)); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5294 | header_pos = m->count; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5295 | t = thread->transaction_stack; | 
|  | 5296 | while (t) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5297 | if (t->from == thread) { | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5298 | print_binder_transaction_ilocked(m, thread->proc, | 
|  | 5299 | "    outgoing transaction", t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5300 | t = t->from_parent; | 
|  | 5301 | } else if (t->to_thread == thread) { | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5302 | print_binder_transaction_ilocked(m, thread->proc, | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5303 | "    incoming transaction", t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5304 | t = t->to_parent; | 
|  | 5305 | } else { | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5306 | print_binder_transaction_ilocked(m, thread->proc, | 
|  | 5307 | "    bad transaction", t); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5308 | t = NULL; | 
|  | 5309 | } | 
|  | 5310 | } | 
|  | 5311 | list_for_each_entry(w, &thread->todo, entry) { | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5312 | print_binder_work_ilocked(m, thread->proc, "    ", | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5313 | "    pending transaction", w); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5314 | } | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5315 | if (!print_always && m->count == header_pos) | 
|  | 5316 | m->count = start_pos; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5317 | } | 
|  | 5318 |  | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5319 | static void print_binder_node_nilocked(struct seq_file *m, | 
|  | 5320 | struct binder_node *node) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5321 | { | 
|  | 5322 | struct binder_ref *ref; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5323 | struct binder_work *w; | 
|  | 5324 | int count; | 
|  | 5325 |  | 
|  | 5326 | count = 0; | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5327 | hlist_for_each_entry(ref, &node->refs, node_entry) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5328 | count++; | 
|  | 5329 |  | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5330 | seq_printf(m, "  node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d", | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5331 | node->debug_id, (u64)node->ptr, (u64)node->cookie, | 
| Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5332 | node->sched_policy, node->min_priority, | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5333 | node->has_strong_ref, node->has_weak_ref, | 
|  | 5334 | node->local_strong_refs, node->local_weak_refs, | 
| Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5335 | node->internal_strong_refs, count, node->tmp_refs); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5336 | if (count) { | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5337 | seq_puts(m, " proc"); | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5338 | hlist_for_each_entry(ref, &node->refs, node_entry) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5339 | seq_printf(m, " %d", ref->proc->pid); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5340 | } | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5341 | seq_puts(m, "\n"); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5342 | if (node->proc) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5343 | list_for_each_entry(w, &node->async_todo, entry) | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5344 | print_binder_work_ilocked(m, node->proc, "    ", | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5345 | "    pending async transaction", w); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5346 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5347 | } | 
|  | 5348 |  | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5349 | static void print_binder_ref_olocked(struct seq_file *m, | 
|  | 5350 | struct binder_ref *ref) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5351 | { | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5352 | binder_node_lock(ref->node); | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5353 | seq_printf(m, "  ref %d: desc %d %snode %d s %d w %d d %pK\n", | 
|  | 5354 | ref->data.debug_id, ref->data.desc, | 
|  | 5355 | ref->node->proc ? "" : "dead ", | 
|  | 5356 | ref->node->debug_id, ref->data.strong, | 
|  | 5357 | ref->data.weak, ref->death); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5358 | binder_node_unlock(ref->node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5359 | } | 
|  | 5360 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5361 | static void print_binder_proc(struct seq_file *m, | 
|  | 5362 | struct binder_proc *proc, int print_all) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5363 | { | 
|  | 5364 | struct binder_work *w; | 
|  | 5365 | struct rb_node *n; | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5366 | size_t start_pos = m->count; | 
|  | 5367 | size_t header_pos; | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5368 | struct binder_node *last_node = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5369 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5370 | seq_printf(m, "proc %d\n", proc->pid); | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5371 | seq_printf(m, "context %s\n", proc->context->name); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5372 | header_pos = m->count; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5373 |  | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5374 | binder_inner_proc_lock(proc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5375 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5376 | print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread, | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5377 | rb_node), print_all); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5378 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5379 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5380 | struct binder_node *node = rb_entry(n, struct binder_node, | 
|  | 5381 | rb_node); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5382 | /* | 
|  | 5383 | * take a temporary reference on the node so it | 
|  | 5384 | * survives and isn't removed from the tree | 
|  | 5385 | * while we print it. | 
|  | 5386 | */ | 
|  | 5387 | binder_inc_node_tmpref_ilocked(node); | 
|  | 5388 | /* Need to drop inner lock to take node lock */ | 
|  | 5389 | binder_inner_proc_unlock(proc); | 
|  | 5390 | if (last_node) | 
|  | 5391 | binder_put_node(last_node); | 
|  | 5392 | binder_node_inner_lock(node); | 
|  | 5393 | print_binder_node_nilocked(m, node); | 
|  | 5394 | binder_node_inner_unlock(node); | 
|  | 5395 | last_node = node; | 
|  | 5396 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5397 | } | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5398 | binder_inner_proc_unlock(proc); | 
|  | 5399 | if (last_node) | 
|  | 5400 | binder_put_node(last_node); | 
|  | 5401 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5402 | if (print_all) { | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5403 | binder_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5404 | for (n = rb_first(&proc->refs_by_desc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5405 | n != NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5406 | n = rb_next(n)) | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5407 | print_binder_ref_olocked(m, rb_entry(n, | 
|  | 5408 | struct binder_ref, | 
|  | 5409 | rb_node_desc)); | 
|  | 5410 | binder_proc_unlock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5411 | } | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5412 | binder_alloc_print_allocated(m, &proc->alloc); | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5413 | binder_inner_proc_lock(proc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5414 | list_for_each_entry(w, &proc->todo, entry) | 
| Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5415 | print_binder_work_ilocked(m, proc, "  ", | 
|  | 5416 | "  pending transaction", w); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5417 | list_for_each_entry(w, &proc->delivered_death, entry) { | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5418 | seq_puts(m, "  has delivered dead binder\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5419 | break; | 
|  | 5420 | } | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5421 | binder_inner_proc_unlock(proc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5422 | if (!print_all && m->count == header_pos) | 
|  | 5423 | m->count = start_pos; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5424 | } | 
|  | 5425 |  | 
| Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5426 | static const char * const binder_return_strings[] = { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5427 | "BR_ERROR", | 
|  | 5428 | "BR_OK", | 
|  | 5429 | "BR_TRANSACTION", | 
|  | 5430 | "BR_REPLY", | 
|  | 5431 | "BR_ACQUIRE_RESULT", | 
|  | 5432 | "BR_DEAD_REPLY", | 
|  | 5433 | "BR_TRANSACTION_COMPLETE", | 
|  | 5434 | "BR_INCREFS", | 
|  | 5435 | "BR_ACQUIRE", | 
|  | 5436 | "BR_RELEASE", | 
|  | 5437 | "BR_DECREFS", | 
|  | 5438 | "BR_ATTEMPT_ACQUIRE", | 
|  | 5439 | "BR_NOOP", | 
|  | 5440 | "BR_SPAWN_LOOPER", | 
|  | 5441 | "BR_FINISHED", | 
|  | 5442 | "BR_DEAD_BINDER", | 
|  | 5443 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", | 
|  | 5444 | "BR_FAILED_REPLY" | 
|  | 5445 | }; | 
|  | 5446 |  | 
| Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5447 | static const char * const binder_command_strings[] = { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5448 | "BC_TRANSACTION", | 
|  | 5449 | "BC_REPLY", | 
|  | 5450 | "BC_ACQUIRE_RESULT", | 
|  | 5451 | "BC_FREE_BUFFER", | 
|  | 5452 | "BC_INCREFS", | 
|  | 5453 | "BC_ACQUIRE", | 
|  | 5454 | "BC_RELEASE", | 
|  | 5455 | "BC_DECREFS", | 
|  | 5456 | "BC_INCREFS_DONE", | 
|  | 5457 | "BC_ACQUIRE_DONE", | 
|  | 5458 | "BC_ATTEMPT_ACQUIRE", | 
|  | 5459 | "BC_REGISTER_LOOPER", | 
|  | 5460 | "BC_ENTER_LOOPER", | 
|  | 5461 | "BC_EXIT_LOOPER", | 
|  | 5462 | "BC_REQUEST_DEATH_NOTIFICATION", | 
|  | 5463 | "BC_CLEAR_DEATH_NOTIFICATION", | 
| Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 5464 | "BC_DEAD_BINDER_DONE", | 
|  | 5465 | "BC_TRANSACTION_SG", | 
|  | 5466 | "BC_REPLY_SG", | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5467 | }; | 
|  | 5468 |  | 
| Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5469 | static const char * const binder_objstat_strings[] = { | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5470 | "proc", | 
|  | 5471 | "thread", | 
|  | 5472 | "node", | 
|  | 5473 | "ref", | 
|  | 5474 | "death", | 
|  | 5475 | "transaction", | 
|  | 5476 | "transaction_complete" | 
|  | 5477 | }; | 
|  | 5478 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5479 | static void print_binder_stats(struct seq_file *m, const char *prefix, | 
|  | 5480 | struct binder_stats *stats) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5481 | { | 
|  | 5482 | int i; | 
|  | 5483 |  | 
|  | 5484 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5485 | ARRAY_SIZE(binder_command_strings)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5486 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5487 | int temp = atomic_read(&stats->bc[i]); | 
|  | 5488 |  | 
|  | 5489 | if (temp) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5490 | seq_printf(m, "%s%s: %d\n", prefix, | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5491 | binder_command_strings[i], temp); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5492 | } | 
|  | 5493 |  | 
|  | 5494 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5495 | ARRAY_SIZE(binder_return_strings)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5496 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5497 | int temp = atomic_read(&stats->br[i]); | 
|  | 5498 |  | 
|  | 5499 | if (temp) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5500 | seq_printf(m, "%s%s: %d\n", prefix, | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5501 | binder_return_strings[i], temp); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5502 | } | 
|  | 5503 |  | 
|  | 5504 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5505 | ARRAY_SIZE(binder_objstat_strings)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5506 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5507 | ARRAY_SIZE(stats->obj_deleted)); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5508 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5509 | int created = atomic_read(&stats->obj_created[i]); | 
|  | 5510 | int deleted = atomic_read(&stats->obj_deleted[i]); | 
|  | 5511 |  | 
|  | 5512 | if (created || deleted) | 
|  | 5513 | seq_printf(m, "%s%s: active %d total %d\n", | 
|  | 5514 | prefix, | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5515 | binder_objstat_strings[i], | 
| Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5516 | created - deleted, | 
|  | 5517 | created); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5518 | } | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5519 | } | 
|  | 5520 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5521 | static void print_binder_proc_stats(struct seq_file *m, | 
|  | 5522 | struct binder_proc *proc) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5523 | { | 
|  | 5524 | struct binder_work *w; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5525 | struct binder_thread *thread; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5526 | struct rb_node *n; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5527 | int count, strong, weak, ready_threads; | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5528 | size_t free_async_space = | 
|  | 5529 | binder_alloc_get_free_async_space(&proc->alloc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5530 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5531 | seq_printf(m, "proc %d\n", proc->pid); | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5532 | seq_printf(m, "context %s\n", proc->context->name); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5533 | count = 0; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5534 | ready_threads = 0; | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5535 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5536 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) | 
|  | 5537 | count++; | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5538 |  | 
|  | 5539 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) | 
|  | 5540 | ready_threads++; | 
|  | 5541 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5542 | seq_printf(m, "  threads: %d\n", count); | 
|  | 5543 | seq_printf(m, "  requested threads: %d+%d/%d\n" | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5544 | "  ready threads %d\n" | 
|  | 5545 | "  free async space %zd\n", proc->requested_threads, | 
|  | 5546 | proc->requested_threads_started, proc->max_threads, | 
| Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5547 | ready_threads, | 
| Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5548 | free_async_space); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5549 | count = 0; | 
|  | 5550 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) | 
|  | 5551 | count++; | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5552 | binder_inner_proc_unlock(proc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5553 | seq_printf(m, "  nodes: %d\n", count); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5554 | count = 0; | 
|  | 5555 | strong = 0; | 
|  | 5556 | weak = 0; | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5557 | binder_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5558 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { | 
|  | 5559 | struct binder_ref *ref = rb_entry(n, struct binder_ref, | 
|  | 5560 | rb_node_desc); | 
|  | 5561 | count++; | 
| Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5562 | strong += ref->data.strong; | 
|  | 5563 | weak += ref->data.weak; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5564 | } | 
| Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5565 | binder_proc_unlock(proc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5566 | seq_printf(m, "  refs: %d s %d w %d\n", count, strong, weak); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5567 |  | 
| Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5568 | count = binder_alloc_get_allocated_count(&proc->alloc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5569 | seq_printf(m, "  buffers: %d\n", count); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5570 |  | 
| Sherry Yang | 9100442 | 2017-08-22 17:26:57 -0700 | [diff] [blame] | 5571 | binder_alloc_print_pages(m, &proc->alloc); | 
|  | 5572 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5573 | count = 0; | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5574 | binder_inner_proc_lock(proc); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5575 | list_for_each_entry(w, &proc->todo, entry) { | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5576 | if (w->type == BINDER_WORK_TRANSACTION) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5577 | count++; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5578 | } | 
| Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5579 | binder_inner_proc_unlock(proc); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5580 | seq_printf(m, "  pending transactions: %d\n", count); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5581 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5582 | print_binder_stats(m, "  ", &proc->stats); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5583 | } | 
|  | 5584 |  | 
|  | 5585 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5586 | static int binder_state_show(struct seq_file *m, void *unused) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5587 | { | 
|  | 5588 | struct binder_proc *proc; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5589 | struct binder_node *node; | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5590 | struct binder_node *last_node = NULL; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5591 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5592 | seq_puts(m, "binder state:\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5593 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5594 | spin_lock(&binder_dead_nodes_lock); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5595 | if (!hlist_empty(&binder_dead_nodes)) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5596 | seq_puts(m, "dead nodes:\n"); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5597 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { | 
|  | 5598 | /* | 
|  | 5599 | * take a temporary reference on the node so it | 
|  | 5600 | * survives and isn't removed from the list | 
|  | 5601 | * while we print it. | 
|  | 5602 | */ | 
|  | 5603 | node->tmp_refs++; | 
|  | 5604 | spin_unlock(&binder_dead_nodes_lock); | 
|  | 5605 | if (last_node) | 
|  | 5606 | binder_put_node(last_node); | 
|  | 5607 | binder_node_lock(node); | 
| Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5608 | print_binder_node_nilocked(m, node); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5609 | binder_node_unlock(node); | 
|  | 5610 | last_node = node; | 
|  | 5611 | spin_lock(&binder_dead_nodes_lock); | 
|  | 5612 | } | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5613 | spin_unlock(&binder_dead_nodes_lock); | 
| Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5614 | if (last_node) | 
|  | 5615 | binder_put_node(last_node); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5616 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5617 | mutex_lock(&binder_procs_lock); | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5618 | hlist_for_each_entry(proc, &binder_procs, proc_node) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5619 | print_binder_proc(m, proc, 1); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5620 | mutex_unlock(&binder_procs_lock); | 
| Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5621 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5622 | return 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5623 | } | 
|  | 5624 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5625 | static int binder_stats_show(struct seq_file *m, void *unused) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5626 | { | 
|  | 5627 | struct binder_proc *proc; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5628 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5629 | seq_puts(m, "binder stats:\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5630 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5631 | print_binder_stats(m, "", &binder_stats); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5632 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5633 | mutex_lock(&binder_procs_lock); | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5634 | hlist_for_each_entry(proc, &binder_procs, proc_node) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5635 | print_binder_proc_stats(m, proc); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5636 | mutex_unlock(&binder_procs_lock); | 
| Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5637 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5638 | return 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5639 | } | 
|  | 5640 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5641 | static int binder_transactions_show(struct seq_file *m, void *unused) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5642 | { | 
|  | 5643 | struct binder_proc *proc; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5644 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5645 | seq_puts(m, "binder transactions:\n"); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5646 | mutex_lock(&binder_procs_lock); | 
| Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5647 | hlist_for_each_entry(proc, &binder_procs, proc_node) | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5648 | print_binder_proc(m, proc, 0); | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5649 | mutex_unlock(&binder_procs_lock); | 
| Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5650 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5651 | return 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5652 | } | 
|  | 5653 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5654 | static int binder_proc_show(struct seq_file *m, void *unused) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5655 | { | 
| Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5656 | struct binder_proc *itr; | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5657 | int pid = (unsigned long)m->private; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5658 |  | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5659 | mutex_lock(&binder_procs_lock); | 
| Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5660 | hlist_for_each_entry(itr, &binder_procs, proc_node) { | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5661 | if (itr->pid == pid) { | 
|  | 5662 | seq_puts(m, "binder proc state:\n"); | 
|  | 5663 | print_binder_proc(m, itr, 1); | 
| Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5664 | } | 
|  | 5665 | } | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5666 | mutex_unlock(&binder_procs_lock); | 
|  | 5667 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5668 | return 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5669 | } | 
|  | 5670 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5671 | static void print_binder_transaction_log_entry(struct seq_file *m, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5672 | struct binder_transaction_log_entry *e) | 
|  | 5673 | { | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5674 | int debug_id = READ_ONCE(e->debug_id_done); | 
|  | 5675 | /* | 
|  | 5676 | * read barrier to guarantee debug_id_done read before | 
|  | 5677 | * we print the log values | 
|  | 5678 | */ | 
|  | 5679 | smp_rmb(); | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5680 | seq_printf(m, | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5681 | "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d", | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5682 | e->debug_id, (e->call_type == 2) ? "reply" : | 
|  | 5683 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, | 
| Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5684 | e->from_thread, e->to_proc, e->to_thread, e->context_name, | 
| Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 5685 | e->to_node, e->target_handle, e->data_size, e->offsets_size, | 
|  | 5686 | e->return_error, e->return_error_param, | 
|  | 5687 | e->return_error_line); | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5688 | /* | 
|  | 5689 | * read-barrier to guarantee read of debug_id_done after | 
|  | 5690 | * done printing the fields of the entry | 
|  | 5691 | */ | 
|  | 5692 | smp_rmb(); | 
|  | 5693 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? | 
|  | 5694 | "\n" : " (incomplete)\n"); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5695 | } | 
|  | 5696 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5697 | static int binder_transaction_log_show(struct seq_file *m, void *unused) | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5698 | { | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5699 | struct binder_transaction_log *log = m->private; | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5700 | unsigned int log_cur = atomic_read(&log->cur); | 
|  | 5701 | unsigned int count; | 
|  | 5702 | unsigned int cur; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5703 | int i; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5704 |  | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5705 | count = log_cur + 1; | 
|  | 5706 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? | 
|  | 5707 | 0 : count % ARRAY_SIZE(log->entry); | 
|  | 5708 | if (count > ARRAY_SIZE(log->entry) || log->full) | 
|  | 5709 | count = ARRAY_SIZE(log->entry); | 
|  | 5710 | for (i = 0; i < count; i++) { | 
|  | 5711 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); | 
|  | 5712 |  | 
|  | 5713 | print_binder_transaction_log_entry(m, &log->entry[index]); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5714 | } | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5715 | return 0; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5716 | } | 
|  | 5717 |  | 
|  | 5718 | static const struct file_operations binder_fops = { | 
|  | 5719 | .owner = THIS_MODULE, | 
|  | 5720 | .poll = binder_poll, | 
|  | 5721 | .unlocked_ioctl = binder_ioctl, | 
| Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5722 | .compat_ioctl = binder_ioctl, | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5723 | .mmap = binder_mmap, | 
|  | 5724 | .open = binder_open, | 
|  | 5725 | .flush = binder_flush, | 
|  | 5726 | .release = binder_release, | 
|  | 5727 | }; | 
|  | 5728 |  | 
| Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5729 | BINDER_DEBUG_ENTRY(state); | 
|  | 5730 | BINDER_DEBUG_ENTRY(stats); | 
|  | 5731 | BINDER_DEBUG_ENTRY(transactions); | 
|  | 5732 | BINDER_DEBUG_ENTRY(transaction_log); | 
|  | 5733 |  | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5734 | static int __init init_binder_device(const char *name) | 
|  | 5735 | { | 
|  | 5736 | int ret; | 
|  | 5737 | struct binder_device *binder_device; | 
|  | 5738 |  | 
|  | 5739 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); | 
|  | 5740 | if (!binder_device) | 
|  | 5741 | return -ENOMEM; | 
|  | 5742 |  | 
|  | 5743 | binder_device->miscdev.fops = &binder_fops; | 
|  | 5744 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; | 
|  | 5745 | binder_device->miscdev.name = name; | 
|  | 5746 |  | 
|  | 5747 | binder_device->context.binder_context_mgr_uid = INVALID_UID; | 
|  | 5748 | binder_device->context.name = name; | 
| Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5749 | mutex_init(&binder_device->context.context_mgr_node_lock); | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5750 |  | 
|  | 5751 | ret = misc_register(&binder_device->miscdev); | 
|  | 5752 | if (ret < 0) { | 
|  | 5753 | kfree(binder_device); | 
|  | 5754 | return ret; | 
|  | 5755 | } | 
|  | 5756 |  | 
|  | 5757 | hlist_add_head(&binder_device->hlist, &binder_devices); | 
|  | 5758 |  | 
|  | 5759 | return ret; | 
|  | 5760 | } | 
|  | 5761 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5762 | static int __init binder_init(void) | 
|  | 5763 | { | 
|  | 5764 | int ret; | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5765 | char *device_name, *device_names; | 
|  | 5766 | struct binder_device *device; | 
|  | 5767 | struct hlist_node *tmp; | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5768 |  | 
| Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 5769 | binder_alloc_shrinker_init(); | 
|  | 5770 |  | 
| Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5771 | atomic_set(&binder_transaction_log.cur, ~0U); | 
|  | 5772 | atomic_set(&binder_transaction_log_failed.cur, ~0U); | 
|  | 5773 |  | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5774 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); | 
|  | 5775 | if (binder_debugfs_dir_entry_root) | 
|  | 5776 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", | 
|  | 5777 | binder_debugfs_dir_entry_root); | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5778 |  | 
| Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5779 | if (binder_debugfs_dir_entry_root) { | 
|  | 5780 | debugfs_create_file("state", | 
|  | 5781 | S_IRUGO, | 
|  | 5782 | binder_debugfs_dir_entry_root, | 
|  | 5783 | NULL, | 
|  | 5784 | &binder_state_fops); | 
|  | 5785 | debugfs_create_file("stats", | 
|  | 5786 | S_IRUGO, | 
|  | 5787 | binder_debugfs_dir_entry_root, | 
|  | 5788 | NULL, | 
|  | 5789 | &binder_stats_fops); | 
|  | 5790 | debugfs_create_file("transactions", | 
|  | 5791 | S_IRUGO, | 
|  | 5792 | binder_debugfs_dir_entry_root, | 
|  | 5793 | NULL, | 
|  | 5794 | &binder_transactions_fops); | 
|  | 5795 | debugfs_create_file("transaction_log", | 
|  | 5796 | S_IRUGO, | 
|  | 5797 | binder_debugfs_dir_entry_root, | 
|  | 5798 | &binder_transaction_log, | 
|  | 5799 | &binder_transaction_log_fops); | 
|  | 5800 | debugfs_create_file("failed_transaction_log", | 
|  | 5801 | S_IRUGO, | 
|  | 5802 | binder_debugfs_dir_entry_root, | 
|  | 5803 | &binder_transaction_log_failed, | 
|  | 5804 | &binder_transaction_log_fops); | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5805 | } | 
| Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5806 |  | 
|  | 5807 | /* | 
|  | 5808 | * Copy the module_parameter string, because we don't want to | 
|  | 5809 | * tokenize it in-place. | 
|  | 5810 | */ | 
|  | 5811 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); | 
|  | 5812 | if (!device_names) { | 
|  | 5813 | ret = -ENOMEM; | 
|  | 5814 | goto err_alloc_device_names_failed; | 
|  | 5815 | } | 
|  | 5816 | strcpy(device_names, binder_devices_param); | 
|  | 5817 |  | 
|  | 5818 | while ((device_name = strsep(&device_names, ","))) { | 
|  | 5819 | ret = init_binder_device(device_name); | 
|  | 5820 | if (ret) | 
|  | 5821 | goto err_init_binder_device_failed; | 
|  | 5822 | } | 
|  | 5823 |  | 
|  | 5824 | return ret; | 
|  | 5825 |  | 
|  | 5826 | err_init_binder_device_failed: | 
|  | 5827 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { | 
|  | 5828 | misc_deregister(&device->miscdev); | 
|  | 5829 | hlist_del(&device->hlist); | 
|  | 5830 | kfree(device); | 
|  | 5831 | } | 
|  | 5832 | err_alloc_device_names_failed: | 
|  | 5833 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); | 
|  | 5834 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5835 | return ret; | 
|  | 5836 | } | 
|  | 5837 |  | 
|  | 5838 | device_initcall(binder_init); | 
|  | 5839 |  | 
| Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5840 | #define CREATE_TRACE_POINTS | 
|  | 5841 | #include "binder_trace.h" | 
|  | 5842 |  | 
| Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5843 | MODULE_LICENSE("GPL v2"); |