| 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 | |
| 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 |
| 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 |
| 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 | |
| 52 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 53 | |
| 54 | #include <asm/cacheflush.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/file.h> |
| 57 | #include <linux/freezer.h> |
| 58 | #include <linux/fs.h> |
| 59 | #include <linux/list.h> |
| 60 | #include <linux/miscdevice.h> |
| 61 | #include <linux/module.h> |
| 62 | #include <linux/mutex.h> |
| 63 | #include <linux/nsproxy.h> |
| 64 | #include <linux/poll.h> |
| 65 | #include <linux/debugfs.h> |
| 66 | #include <linux/rbtree.h> |
| 67 | #include <linux/sched/signal.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/seq_file.h> |
| 70 | #include <linux/uaccess.h> |
| 71 | #include <linux/pid_namespace.h> |
| 72 | #include <linux/security.h> |
| 73 | #include <linux/spinlock.h> |
| 74 | |
| 75 | #include <uapi/linux/android/binder.h> |
| 76 | #include <uapi/linux/sched/types.h> |
| 77 | #include "binder_alloc.h" |
| 78 | #include "binder_trace.h" |
| 79 | |
| 80 | static HLIST_HEAD(binder_deferred_list); |
| 81 | static DEFINE_MUTEX(binder_deferred_lock); |
| 82 | |
| 83 | static HLIST_HEAD(binder_devices); |
| 84 | static HLIST_HEAD(binder_procs); |
| 85 | static DEFINE_MUTEX(binder_procs_lock); |
| 86 | |
| 87 | static HLIST_HEAD(binder_dead_nodes); |
| 88 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
| 89 | |
| 90 | static struct dentry *binder_debugfs_dir_entry_root; |
| 91 | static struct dentry *binder_debugfs_dir_entry_proc; |
| 92 | static atomic_t binder_last_id; |
| 93 | |
| 94 | #define BINDER_DEBUG_ENTRY(name) \ |
| 95 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 96 | { \ |
| 97 | return single_open(file, binder_##name##_show, inode->i_private); \ |
| 98 | } \ |
| 99 | \ |
| 100 | static const struct file_operations binder_##name##_fops = { \ |
| 101 | .owner = THIS_MODULE, \ |
| 102 | .open = binder_##name##_open, \ |
| 103 | .read = seq_read, \ |
| 104 | .llseek = seq_lseek, \ |
| 105 | .release = single_release, \ |
| 106 | } |
| 107 | |
| 108 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 109 | BINDER_DEBUG_ENTRY(proc); |
| 110 | |
| 111 | /* This is only defined in include/asm-arm/sizes.h */ |
| 112 | #ifndef SZ_1K |
| 113 | #define SZ_1K 0x400 |
| 114 | #endif |
| 115 | |
| 116 | #ifndef SZ_4M |
| 117 | #define SZ_4M 0x400000 |
| 118 | #endif |
| 119 | |
| 120 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 121 | |
| 122 | enum { |
| 123 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 124 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 125 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 126 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 127 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 128 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 129 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 130 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 131 | BINDER_DEBUG_THREADS = 1U << 8, |
| 132 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 133 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 134 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 135 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
| 136 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
| 137 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
| 138 | }; |
| 139 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 140 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
| 141 | module_param_named(debug_mask, binder_debug_mask, uint, 0644); |
| 142 | |
| 143 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 144 | module_param_named(devices, binder_devices_param, charp, 0444); |
| 145 | |
| 146 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 147 | static int binder_stop_on_user_error; |
| 148 | |
| 149 | static int binder_set_stop_on_user_error(const char *val, |
| 150 | const struct kernel_param *kp) |
| 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | ret = param_set_int(val, kp); |
| 155 | if (binder_stop_on_user_error < 2) |
| 156 | wake_up(&binder_user_error_wait); |
| 157 | return ret; |
| 158 | } |
| 159 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
| 160 | param_get_int, &binder_stop_on_user_error, 0644); |
| 161 | |
| 162 | #define binder_debug(mask, x...) \ |
| 163 | do { \ |
| 164 | if (binder_debug_mask & mask) \ |
| 165 | pr_info(x); \ |
| 166 | } while (0) |
| 167 | |
| 168 | #define binder_user_error(x...) \ |
| 169 | do { \ |
| 170 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
| 171 | pr_info(x); \ |
| 172 | if (binder_stop_on_user_error) \ |
| 173 | binder_stop_on_user_error = 2; \ |
| 174 | } while (0) |
| 175 | |
| 176 | #define to_flat_binder_object(hdr) \ |
| 177 | container_of(hdr, struct flat_binder_object, hdr) |
| 178 | |
| 179 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 180 | |
| 181 | #define to_binder_buffer_object(hdr) \ |
| 182 | container_of(hdr, struct binder_buffer_object, hdr) |
| 183 | |
| 184 | #define to_binder_fd_array_object(hdr) \ |
| 185 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 186 | |
| 187 | enum binder_stat_types { |
| 188 | BINDER_STAT_PROC, |
| 189 | BINDER_STAT_THREAD, |
| 190 | BINDER_STAT_NODE, |
| 191 | BINDER_STAT_REF, |
| 192 | BINDER_STAT_DEATH, |
| 193 | BINDER_STAT_TRANSACTION, |
| 194 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 195 | BINDER_STAT_COUNT |
| 196 | }; |
| 197 | |
| 198 | struct binder_stats { |
| 199 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 200 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 201 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 202 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
| 203 | }; |
| 204 | |
| 205 | static struct binder_stats binder_stats; |
| 206 | |
| 207 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 208 | { |
| 209 | atomic_inc(&binder_stats.obj_deleted[type]); |
| 210 | } |
| 211 | |
| 212 | static inline void binder_stats_created(enum binder_stat_types type) |
| 213 | { |
| 214 | atomic_inc(&binder_stats.obj_created[type]); |
| 215 | } |
| 216 | |
| 217 | struct binder_transaction_log_entry { |
| 218 | int debug_id; |
| 219 | int debug_id_done; |
| 220 | int call_type; |
| 221 | int from_proc; |
| 222 | int from_thread; |
| 223 | int target_handle; |
| 224 | int to_proc; |
| 225 | int to_thread; |
| 226 | int to_node; |
| 227 | int data_size; |
| 228 | int offsets_size; |
| 229 | int return_error_line; |
| 230 | uint32_t return_error; |
| 231 | uint32_t return_error_param; |
| 232 | const char *context_name; |
| 233 | }; |
| 234 | struct binder_transaction_log { |
| 235 | atomic_t cur; |
| 236 | bool full; |
| 237 | struct binder_transaction_log_entry entry[32]; |
| 238 | }; |
| 239 | static struct binder_transaction_log binder_transaction_log; |
| 240 | static struct binder_transaction_log binder_transaction_log_failed; |
| 241 | |
| 242 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 243 | struct binder_transaction_log *log) |
| 244 | { |
| 245 | struct binder_transaction_log_entry *e; |
| 246 | unsigned int cur = atomic_inc_return(&log->cur); |
| 247 | |
| 248 | if (cur >= ARRAY_SIZE(log->entry)) |
| 249 | log->full = true; |
| 250 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 251 | WRITE_ONCE(e->debug_id_done, 0); |
| 252 | /* |
| 253 | * write-barrier to synchronize access to e->debug_id_done. |
| 254 | * We make sure the initialized 0 value is seen before |
| 255 | * memset() other fields are zeroed by memset. |
| 256 | */ |
| 257 | smp_wmb(); |
| 258 | memset(e, 0, sizeof(*e)); |
| 259 | return e; |
| 260 | } |
| 261 | |
| 262 | struct binder_context { |
| 263 | struct binder_node *binder_context_mgr_node; |
| 264 | struct mutex context_mgr_node_lock; |
| 265 | |
| 266 | kuid_t binder_context_mgr_uid; |
| 267 | const char *name; |
| 268 | }; |
| 269 | |
| 270 | struct binder_device { |
| 271 | struct hlist_node hlist; |
| 272 | struct miscdevice miscdev; |
| 273 | struct binder_context context; |
| 274 | }; |
| 275 | |
| 276 | /** |
| 277 | * struct binder_work - work enqueued on a worklist |
| 278 | * @entry: node enqueued on list |
| 279 | * @type: type of work to be performed |
| 280 | * |
| 281 | * There are separate work lists for proc, thread, and node (async). |
| 282 | */ |
| 283 | struct binder_work { |
| 284 | struct list_head entry; |
| 285 | |
| 286 | enum binder_work_type { |
| 287 | BINDER_WORK_TRANSACTION = 1, |
| 288 | BINDER_WORK_TRANSACTION_COMPLETE, |
| 289 | BINDER_WORK_RETURN_ERROR, |
| 290 | BINDER_WORK_NODE, |
| 291 | BINDER_WORK_DEAD_BINDER, |
| 292 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 293 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 294 | } type; |
| 295 | }; |
| 296 | |
| 297 | struct binder_error { |
| 298 | struct binder_work work; |
| 299 | uint32_t cmd; |
| 300 | }; |
| 301 | |
| 302 | /** |
| 303 | * struct binder_node - binder node bookkeeping |
| 304 | * @debug_id: unique ID for debugging |
| 305 | * (invariant after initialized) |
| 306 | * @lock: lock for node fields |
| 307 | * @work: worklist element for node work |
| 308 | * (protected by @proc->inner_lock) |
| 309 | * @rb_node: element for proc->nodes tree |
| 310 | * (protected by @proc->inner_lock) |
| 311 | * @dead_node: element for binder_dead_nodes list |
| 312 | * (protected by binder_dead_nodes_lock) |
| 313 | * @proc: binder_proc that owns this node |
| 314 | * (invariant after initialized) |
| 315 | * @refs: list of references on this node |
| 316 | * (protected by @lock) |
| 317 | * @internal_strong_refs: used to take strong references when |
| 318 | * initiating a transaction |
| 319 | * (protected by @proc->inner_lock if @proc |
| 320 | * and by @lock) |
| 321 | * @local_weak_refs: weak user refs from local process |
| 322 | * (protected by @proc->inner_lock if @proc |
| 323 | * and by @lock) |
| 324 | * @local_strong_refs: strong user refs from local process |
| 325 | * (protected by @proc->inner_lock if @proc |
| 326 | * and by @lock) |
| 327 | * @tmp_refs: temporary kernel refs |
| 328 | * (protected by @proc->inner_lock while @proc |
| 329 | * is valid, and by binder_dead_nodes_lock |
| 330 | * if @proc is NULL. During inc/dec and node release |
| 331 | * it is also protected by @lock to provide safety |
| 332 | * as the node dies and @proc becomes NULL) |
| 333 | * @ptr: userspace pointer for node |
| 334 | * (invariant, no lock needed) |
| 335 | * @cookie: userspace cookie for node |
| 336 | * (invariant, no lock needed) |
| 337 | * @has_strong_ref: userspace notified of strong ref |
| 338 | * (protected by @proc->inner_lock if @proc |
| 339 | * and by @lock) |
| 340 | * @pending_strong_ref: userspace has acked notification of strong ref |
| 341 | * (protected by @proc->inner_lock if @proc |
| 342 | * and by @lock) |
| 343 | * @has_weak_ref: userspace notified of weak ref |
| 344 | * (protected by @proc->inner_lock if @proc |
| 345 | * and by @lock) |
| 346 | * @pending_weak_ref: userspace has acked notification of weak ref |
| 347 | * (protected by @proc->inner_lock if @proc |
| 348 | * and by @lock) |
| 349 | * @has_async_transaction: async transaction to node in progress |
| 350 | * (protected by @lock) |
| 351 | * @sched_policy: minimum scheduling policy for node |
| 352 | * (invariant after initialized) |
| 353 | * @accept_fds: file descriptor operations supported for node |
| 354 | * (invariant after initialized) |
| 355 | * @min_priority: minimum scheduling priority |
| 356 | * (invariant after initialized) |
| 357 | * @inherit_rt: inherit RT scheduling policy from caller |
| 358 | * @txn_security_ctx: require sender's security context |
| 359 | * (invariant after initialized) |
| 360 | * @async_todo: list of async work items |
| 361 | * (protected by @proc->inner_lock) |
| 362 | * |
| 363 | * Bookkeeping structure for binder nodes. |
| 364 | */ |
| 365 | struct binder_node { |
| 366 | int debug_id; |
| 367 | spinlock_t lock; |
| 368 | struct binder_work work; |
| 369 | union { |
| 370 | struct rb_node rb_node; |
| 371 | struct hlist_node dead_node; |
| 372 | }; |
| 373 | struct binder_proc *proc; |
| 374 | struct hlist_head refs; |
| 375 | int internal_strong_refs; |
| 376 | int local_weak_refs; |
| 377 | int local_strong_refs; |
| 378 | int tmp_refs; |
| 379 | binder_uintptr_t ptr; |
| 380 | binder_uintptr_t cookie; |
| 381 | struct { |
| 382 | /* |
| 383 | * bitfield elements protected by |
| 384 | * proc inner_lock |
| 385 | */ |
| 386 | u8 has_strong_ref:1; |
| 387 | u8 pending_strong_ref:1; |
| 388 | u8 has_weak_ref:1; |
| 389 | u8 pending_weak_ref:1; |
| 390 | }; |
| 391 | struct { |
| 392 | /* |
| 393 | * invariant after initialization |
| 394 | */ |
| 395 | u8 sched_policy:2; |
| 396 | u8 inherit_rt:1; |
| 397 | u8 accept_fds:1; |
| 398 | u8 txn_security_ctx:1; |
| 399 | u8 min_priority; |
| 400 | }; |
| 401 | bool has_async_transaction; |
| 402 | struct list_head async_todo; |
| 403 | }; |
| 404 | |
| 405 | struct binder_ref_death { |
| 406 | /** |
| 407 | * @work: worklist element for death notifications |
| 408 | * (protected by inner_lock of the proc that |
| 409 | * this ref belongs to) |
| 410 | */ |
| 411 | struct binder_work work; |
| 412 | binder_uintptr_t cookie; |
| 413 | }; |
| 414 | |
| 415 | /** |
| 416 | * struct binder_ref_data - binder_ref counts and id |
| 417 | * @debug_id: unique ID for the ref |
| 418 | * @desc: unique userspace handle for ref |
| 419 | * @strong: strong ref count (debugging only if not locked) |
| 420 | * @weak: weak ref count (debugging only if not locked) |
| 421 | * |
| 422 | * Structure to hold ref count and ref id information. Since |
| 423 | * the actual ref can only be accessed with a lock, this structure |
| 424 | * is used to return information about the ref to callers of |
| 425 | * ref inc/dec functions. |
| 426 | */ |
| 427 | struct binder_ref_data { |
| 428 | int debug_id; |
| 429 | uint32_t desc; |
| 430 | int strong; |
| 431 | int weak; |
| 432 | }; |
| 433 | |
| 434 | /** |
| 435 | * struct binder_ref - struct to track references on nodes |
| 436 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 437 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 438 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 439 | * @node_entry: list entry for node->refs list in target node |
| 440 | * (protected by @node->lock) |
| 441 | * @proc: binder_proc containing ref |
| 442 | * @node: binder_node of target node. When cleaning up a |
| 443 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 444 | * @node indicates the node must be freed |
| 445 | * @death: pointer to death notification (ref_death) if requested |
| 446 | * (protected by @node->lock) |
| 447 | * |
| 448 | * Structure to track references from procA to target node (on procB). This |
| 449 | * structure is unsafe to access without holding @proc->outer_lock. |
| 450 | */ |
| 451 | struct binder_ref { |
| 452 | /* Lookups needed: */ |
| 453 | /* node + proc => ref (transaction) */ |
| 454 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 455 | /* node => refs + procs (proc exit) */ |
| 456 | struct binder_ref_data data; |
| 457 | struct rb_node rb_node_desc; |
| 458 | struct rb_node rb_node_node; |
| 459 | struct hlist_node node_entry; |
| 460 | struct binder_proc *proc; |
| 461 | struct binder_node *node; |
| 462 | struct binder_ref_death *death; |
| 463 | }; |
| 464 | |
| 465 | enum binder_deferred_state { |
| 466 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 467 | BINDER_DEFERRED_FLUSH = 0x02, |
| 468 | BINDER_DEFERRED_RELEASE = 0x04, |
| 469 | }; |
| 470 | |
| 471 | /** |
| 472 | * struct binder_priority - scheduler policy and priority |
| 473 | * @sched_policy scheduler policy |
| 474 | * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT |
| 475 | * |
| 476 | * The binder driver supports inheriting the following scheduler policies: |
| 477 | * SCHED_NORMAL |
| 478 | * SCHED_BATCH |
| 479 | * SCHED_FIFO |
| 480 | * SCHED_RR |
| 481 | */ |
| 482 | struct binder_priority { |
| 483 | unsigned int sched_policy; |
| 484 | int prio; |
| 485 | }; |
| 486 | |
| 487 | /** |
| 488 | * struct binder_proc - binder process bookkeeping |
| 489 | * @proc_node: element for binder_procs list |
| 490 | * @threads: rbtree of binder_threads in this proc |
| 491 | * (protected by @inner_lock) |
| 492 | * @nodes: rbtree of binder nodes associated with |
| 493 | * this proc ordered by node->ptr |
| 494 | * (protected by @inner_lock) |
| 495 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
| 496 | * (protected by @outer_lock) |
| 497 | * @refs_by_node: rbtree of refs ordered by ref->node |
| 498 | * (protected by @outer_lock) |
| 499 | * @waiting_threads: threads currently waiting for proc work |
| 500 | * (protected by @inner_lock) |
| 501 | * @pid PID of group_leader of process |
| 502 | * (invariant after initialized) |
| 503 | * @tsk task_struct for group_leader of process |
| 504 | * (invariant after initialized) |
| 505 | * @files files_struct for process |
| 506 | * (protected by @files_lock) |
| 507 | * @files_lock mutex to protect @files |
| 508 | * @deferred_work_node: element for binder_deferred_list |
| 509 | * (protected by binder_deferred_lock) |
| 510 | * @deferred_work: bitmap of deferred work to perform |
| 511 | * (protected by binder_deferred_lock) |
| 512 | * @is_dead: process is dead and awaiting free |
| 513 | * when outstanding transactions are cleaned up |
| 514 | * (protected by @inner_lock) |
| 515 | * @todo: list of work for this process |
| 516 | * (protected by @inner_lock) |
| 517 | * @stats: per-process binder statistics |
| 518 | * (atomics, no lock needed) |
| 519 | * @delivered_death: list of delivered death notification |
| 520 | * (protected by @inner_lock) |
| 521 | * @max_threads: cap on number of binder threads |
| 522 | * (protected by @inner_lock) |
| 523 | * @requested_threads: number of binder threads requested but not |
| 524 | * yet started. In current implementation, can |
| 525 | * only be 0 or 1. |
| 526 | * (protected by @inner_lock) |
| 527 | * @requested_threads_started: number binder threads started |
| 528 | * (protected by @inner_lock) |
| 529 | * @tmp_ref: temporary reference to indicate proc is in use |
| 530 | * (protected by @inner_lock) |
| 531 | * @default_priority: default scheduler priority |
| 532 | * (invariant after initialized) |
| 533 | * @debugfs_entry: debugfs node |
| 534 | * @alloc: binder allocator bookkeeping |
| 535 | * @context: binder_context for this proc |
| 536 | * (invariant after initialized) |
| 537 | * @inner_lock: can nest under outer_lock and/or node lock |
| 538 | * @outer_lock: no nesting under innor or node lock |
| 539 | * Lock order: 1) outer, 2) node, 3) inner |
| 540 | * |
| 541 | * Bookkeeping structure for binder processes |
| 542 | */ |
| 543 | struct binder_proc { |
| 544 | struct hlist_node proc_node; |
| 545 | struct rb_root threads; |
| 546 | struct rb_root nodes; |
| 547 | struct rb_root refs_by_desc; |
| 548 | struct rb_root refs_by_node; |
| 549 | struct list_head waiting_threads; |
| 550 | int pid; |
| 551 | struct task_struct *tsk; |
| 552 | struct files_struct *files; |
| 553 | struct mutex files_lock; |
| 554 | struct hlist_node deferred_work_node; |
| 555 | int deferred_work; |
| 556 | bool is_dead; |
| 557 | |
| 558 | struct list_head todo; |
| 559 | struct binder_stats stats; |
| 560 | struct list_head delivered_death; |
| 561 | int max_threads; |
| 562 | int requested_threads; |
| 563 | int requested_threads_started; |
| 564 | int tmp_ref; |
| 565 | struct binder_priority default_priority; |
| 566 | struct dentry *debugfs_entry; |
| 567 | struct binder_alloc alloc; |
| 568 | struct binder_context *context; |
| 569 | spinlock_t inner_lock; |
| 570 | spinlock_t outer_lock; |
| 571 | }; |
| 572 | |
| 573 | enum { |
| 574 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 575 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 576 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 577 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 578 | BINDER_LOOPER_STATE_WAITING = 0x10, |
| 579 | BINDER_LOOPER_STATE_POLL = 0x20, |
| 580 | }; |
| 581 | |
| 582 | /** |
| 583 | * struct binder_thread - binder thread bookkeeping |
| 584 | * @proc: binder process for this thread |
| 585 | * (invariant after initialization) |
| 586 | * @rb_node: element for proc->threads rbtree |
| 587 | * (protected by @proc->inner_lock) |
| 588 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 589 | * (protected by @proc->inner_lock) |
| 590 | * @pid: PID for this thread |
| 591 | * (invariant after initialization) |
| 592 | * @looper: bitmap of looping state |
| 593 | * (only accessed by this thread) |
| 594 | * @looper_needs_return: looping thread needs to exit driver |
| 595 | * (no lock needed) |
| 596 | * @transaction_stack: stack of in-progress transactions for this thread |
| 597 | * (protected by @proc->inner_lock) |
| 598 | * @todo: list of work to do for this thread |
| 599 | * (protected by @proc->inner_lock) |
| 600 | * @process_todo: whether work in @todo should be processed |
| 601 | * (protected by @proc->inner_lock) |
| 602 | * @return_error: transaction errors reported by this thread |
| 603 | * (only accessed by this thread) |
| 604 | * @reply_error: transaction errors reported by target thread |
| 605 | * (protected by @proc->inner_lock) |
| 606 | * @wait: wait queue for thread work |
| 607 | * @stats: per-thread statistics |
| 608 | * (atomics, no lock needed) |
| 609 | * @tmp_ref: temporary reference to indicate thread is in use |
| 610 | * (atomic since @proc->inner_lock cannot |
| 611 | * always be acquired) |
| 612 | * @is_dead: thread is dead and awaiting free |
| 613 | * when outstanding transactions are cleaned up |
| 614 | * (protected by @proc->inner_lock) |
| 615 | * @task: struct task_struct for this thread |
| 616 | * |
| 617 | * Bookkeeping structure for binder threads. |
| 618 | */ |
| 619 | struct binder_thread { |
| 620 | struct binder_proc *proc; |
| 621 | struct rb_node rb_node; |
| 622 | struct list_head waiting_thread_node; |
| 623 | int pid; |
| 624 | int looper; /* only modified by this thread */ |
| 625 | bool looper_need_return; /* can be written by other thread */ |
| 626 | struct binder_transaction *transaction_stack; |
| 627 | struct list_head todo; |
| 628 | bool process_todo; |
| 629 | struct binder_error return_error; |
| 630 | struct binder_error reply_error; |
| 631 | wait_queue_head_t wait; |
| 632 | struct binder_stats stats; |
| 633 | atomic_t tmp_ref; |
| 634 | bool is_dead; |
| 635 | struct task_struct *task; |
| 636 | }; |
| 637 | |
| 638 | struct binder_transaction { |
| 639 | int debug_id; |
| 640 | struct binder_work work; |
| 641 | struct binder_thread *from; |
| 642 | struct binder_transaction *from_parent; |
| 643 | struct binder_proc *to_proc; |
| 644 | struct binder_thread *to_thread; |
| 645 | struct binder_transaction *to_parent; |
| 646 | unsigned need_reply:1; |
| 647 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 648 | |
| 649 | struct binder_buffer *buffer; |
| 650 | unsigned int code; |
| 651 | unsigned int flags; |
| 652 | struct binder_priority priority; |
| 653 | struct binder_priority saved_priority; |
| 654 | bool set_priority_called; |
| 655 | kuid_t sender_euid; |
| 656 | binder_uintptr_t security_ctx; |
| 657 | /** |
| 658 | * @lock: protects @from, @to_proc, and @to_thread |
| 659 | * |
| 660 | * @from, @to_proc, and @to_thread can be set to NULL |
| 661 | * during thread teardown |
| 662 | */ |
| 663 | spinlock_t lock; |
| 664 | }; |
| 665 | |
| 666 | /** |
| 667 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 668 | * @proc: struct binder_proc to acquire |
| 669 | * |
| 670 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 671 | * structures associated with the given proc. |
| 672 | */ |
| 673 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 674 | static void |
| 675 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 676 | { |
| 677 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 678 | "%s: line=%d\n", __func__, line); |
| 679 | spin_lock(&proc->outer_lock); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 684 | * @proc: struct binder_proc to acquire |
| 685 | * |
| 686 | * Release lock acquired via binder_proc_lock() |
| 687 | */ |
| 688 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 689 | static void |
| 690 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 691 | { |
| 692 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 693 | "%s: line=%d\n", __func__, line); |
| 694 | spin_unlock(&proc->outer_lock); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 699 | * @proc: struct binder_proc to acquire |
| 700 | * |
| 701 | * Acquires proc->inner_lock. Used to protect todo lists |
| 702 | */ |
| 703 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 704 | static void |
| 705 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 706 | { |
| 707 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 708 | "%s: line=%d\n", __func__, line); |
| 709 | spin_lock(&proc->inner_lock); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 714 | * @proc: struct binder_proc to acquire |
| 715 | * |
| 716 | * Release lock acquired via binder_inner_proc_lock() |
| 717 | */ |
| 718 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 719 | static void |
| 720 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 721 | { |
| 722 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 723 | "%s: line=%d\n", __func__, line); |
| 724 | spin_unlock(&proc->inner_lock); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 729 | * @node: struct binder_node to acquire |
| 730 | * |
| 731 | * Acquires node->lock. Used to protect binder_node fields |
| 732 | */ |
| 733 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 734 | static void |
| 735 | _binder_node_lock(struct binder_node *node, int line) |
| 736 | { |
| 737 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 738 | "%s: line=%d\n", __func__, line); |
| 739 | spin_lock(&node->lock); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 744 | * @node: struct binder_node to acquire |
| 745 | * |
| 746 | * Release lock acquired via binder_node_lock() |
| 747 | */ |
| 748 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 749 | static void |
| 750 | _binder_node_unlock(struct binder_node *node, int line) |
| 751 | { |
| 752 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 753 | "%s: line=%d\n", __func__, line); |
| 754 | spin_unlock(&node->lock); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * binder_node_inner_lock() - Acquire node and inner locks |
| 759 | * @node: struct binder_node to acquire |
| 760 | * |
| 761 | * Acquires node->lock. If node->proc also acquires |
| 762 | * proc->inner_lock. Used to protect binder_node fields |
| 763 | */ |
| 764 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 765 | static void |
| 766 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 767 | { |
| 768 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 769 | "%s: line=%d\n", __func__, line); |
| 770 | spin_lock(&node->lock); |
| 771 | if (node->proc) |
| 772 | binder_inner_proc_lock(node->proc); |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * binder_node_unlock() - Release node and inner locks |
| 777 | * @node: struct binder_node to acquire |
| 778 | * |
| 779 | * Release lock acquired via binder_node_lock() |
| 780 | */ |
| 781 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 782 | static void |
| 783 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 784 | { |
| 785 | struct binder_proc *proc = node->proc; |
| 786 | |
| 787 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 788 | "%s: line=%d\n", __func__, line); |
| 789 | if (proc) |
| 790 | binder_inner_proc_unlock(proc); |
| 791 | spin_unlock(&node->lock); |
| 792 | } |
| 793 | |
| 794 | #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER |
| 795 | /* |
| 796 | * Binder Debug Snapshot |
| 797 | */ |
| 798 | static void init_binder_transaction_base(int type, struct trace_binder_transaction_base *base, |
| 799 | struct binder_transaction *t, struct binder_thread *from, |
| 800 | struct binder_thread *to) |
| 801 | { |
| 802 | struct binder_thread *t_from; |
| 803 | struct binder_thread *t_to; |
| 804 | |
| 805 | if (base == NULL) |
| 806 | return; |
| 807 | |
| 808 | t_from = t->from ? t->from : (from ? from : NULL); |
| 809 | t_to = t->to_thread ? t->to_thread : (to ? to : NULL); |
| 810 | base->trace_type = type; |
| 811 | base->transaction_id = t->debug_id; |
| 812 | base->from_pid = t_from ? t_from->proc->pid : 0; |
| 813 | base->from_tid = t_from ? t_from->pid : 0; |
| 814 | base->to_pid = t->to_proc ? t->to_proc->pid : 0; |
| 815 | base->to_tid = t_to ? t_to->pid : 0; |
| 816 | if (t_from) { |
| 817 | strncpy(base->from_pid_comm, t_from->proc->tsk->comm, TASK_COMM_LEN); |
| 818 | strncpy(base->from_tid_comm, t_from->task->comm, TASK_COMM_LEN); |
| 819 | } else { |
| 820 | base->from_pid_comm[0] = '\0'; |
| 821 | base->from_tid_comm[0] = '\0'; |
| 822 | } |
| 823 | if (t->to_proc) |
| 824 | strncpy(base->to_pid_comm, t->to_proc->tsk->comm, TASK_COMM_LEN); |
| 825 | else |
| 826 | base->to_pid_comm[0] = '\0'; |
| 827 | if (t_to) |
| 828 | strncpy(base->to_tid_comm, t_to->task->comm, TASK_COMM_LEN); |
| 829 | else |
| 830 | base->to_tid_comm[0] = '\0'; |
| 831 | } |
| 832 | |
| 833 | static void dss_binder_transaction(int reply, struct binder_transaction *t, struct binder_thread *from, int to_node_id) |
| 834 | { |
| 835 | struct trace_binder_transaction_base base; |
| 836 | struct trace_binder_transaction transaction; |
| 837 | |
| 838 | init_binder_transaction_base(TRANSACTION, &base, t, from, NULL); |
| 839 | transaction.to_node_id = to_node_id; |
| 840 | transaction.reply = reply; |
| 841 | transaction.flags = t->flags; |
| 842 | transaction.code = t->code; |
| 843 | |
| 844 | dbg_snapshot_binder(&base, &transaction, NULL); |
| 845 | } |
| 846 | |
| 847 | static void dss_binder_transaction_received(struct binder_transaction *t, struct binder_thread *to) |
| 848 | { |
| 849 | struct trace_binder_transaction_base base; |
| 850 | |
| 851 | init_binder_transaction_base(TRANSACTION_DONE, &base, t, NULL, to); |
| 852 | |
| 853 | dbg_snapshot_binder(&base, NULL, NULL); |
| 854 | } |
| 855 | |
| 856 | static void dss_binder_transaction_failed(int reply, struct binder_transaction_log_entry *e, |
| 857 | char *from_pid_comm, char *from_tid_comm, |
| 858 | unsigned int flags, unsigned int code) |
| 859 | { |
| 860 | struct trace_binder_transaction_base base; |
| 861 | struct trace_binder_transaction transaction; |
| 862 | struct trace_binder_transaction_error error; |
| 863 | |
| 864 | base.trace_type = TRANSACTION_ERROR; |
| 865 | base.transaction_id = e->debug_id; |
| 866 | base.from_pid = e->from_proc; |
| 867 | base.from_tid = e->from_thread; |
| 868 | base.to_pid = e->to_proc; |
| 869 | base.to_tid = e->to_thread; |
| 870 | strncpy(base.from_pid_comm, from_pid_comm, TASK_COMM_LEN); |
| 871 | strncpy(base.from_tid_comm, from_tid_comm, TASK_COMM_LEN); |
| 872 | base.to_pid_comm[0] = '\0'; |
| 873 | base.to_tid_comm[0] = '\0'; |
| 874 | transaction.to_node_id = e->to_node; |
| 875 | transaction.reply = reply; |
| 876 | transaction.flags = flags; |
| 877 | transaction.code = code; |
| 878 | error.return_error = e->return_error; |
| 879 | error.return_error_param = e->return_error_param; |
| 880 | error.return_error_line = e->return_error_line; |
| 881 | |
| 882 | dbg_snapshot_binder(&base, &transaction, &error); |
| 883 | } |
| 884 | #endif /* CONFIG_DEBUG_SNAPSHOT_BINDER */ |
| 885 | |
| 886 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 887 | { |
| 888 | return list_empty(list); |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * binder_worklist_empty() - Check if no items on the work list |
| 893 | * @proc: binder_proc associated with list |
| 894 | * @list: list to check |
| 895 | * |
| 896 | * Return: true if there are no items on list, else false |
| 897 | */ |
| 898 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 899 | struct list_head *list) |
| 900 | { |
| 901 | bool ret; |
| 902 | |
| 903 | binder_inner_proc_lock(proc); |
| 904 | ret = binder_worklist_empty_ilocked(list); |
| 905 | binder_inner_proc_unlock(proc); |
| 906 | return ret; |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 911 | * @work: struct binder_work to add to list |
| 912 | * @target_list: list to add work to |
| 913 | * |
| 914 | * Adds the work to the specified list. Asserts that work |
| 915 | * is not already on a list. |
| 916 | * |
| 917 | * Requires the proc->inner_lock to be held. |
| 918 | */ |
| 919 | static void |
| 920 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 921 | struct list_head *target_list) |
| 922 | { |
| 923 | BUG_ON(target_list == NULL); |
| 924 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 925 | list_add_tail(&work->entry, target_list); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
| 930 | * @thread: thread to queue work to |
| 931 | * @work: struct binder_work to add to list |
| 932 | * |
| 933 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 934 | * flag, which means that (if it wasn't already set) the thread will go to |
| 935 | * sleep without handling this work when it calls read. |
| 936 | * |
| 937 | * Requires the proc->inner_lock to be held. |
| 938 | */ |
| 939 | static void |
| 940 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 941 | struct binder_work *work) |
| 942 | { |
| 943 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 948 | * @thread: thread to queue work to |
| 949 | * @work: struct binder_work to add to list |
| 950 | * |
| 951 | * Adds the work to the todo list of the thread, and enables processing |
| 952 | * of the todo queue. |
| 953 | * |
| 954 | * Requires the proc->inner_lock to be held. |
| 955 | */ |
| 956 | static void |
| 957 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 958 | struct binder_work *work) |
| 959 | { |
| 960 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 961 | thread->process_todo = true; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 966 | * @thread: thread to queue work to |
| 967 | * @work: struct binder_work to add to list |
| 968 | * |
| 969 | * Adds the work to the todo list of the thread, and enables processing |
| 970 | * of the todo queue. |
| 971 | */ |
| 972 | static void |
| 973 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 974 | struct binder_work *work) |
| 975 | { |
| 976 | binder_inner_proc_lock(thread->proc); |
| 977 | binder_enqueue_thread_work_ilocked(thread, work); |
| 978 | binder_inner_proc_unlock(thread->proc); |
| 979 | } |
| 980 | |
| 981 | static void |
| 982 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 983 | { |
| 984 | list_del_init(&work->entry); |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * binder_dequeue_work() - Removes an item from the work list |
| 989 | * @proc: binder_proc associated with list |
| 990 | * @work: struct binder_work to remove from list |
| 991 | * |
| 992 | * Removes the specified work item from whatever list it is on. |
| 993 | * Can safely be called if work is not on any list. |
| 994 | */ |
| 995 | static void |
| 996 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 997 | { |
| 998 | binder_inner_proc_lock(proc); |
| 999 | binder_dequeue_work_ilocked(work); |
| 1000 | binder_inner_proc_unlock(proc); |
| 1001 | } |
| 1002 | |
| 1003 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 1004 | struct list_head *list) |
| 1005 | { |
| 1006 | struct binder_work *w; |
| 1007 | |
| 1008 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 1009 | if (w) |
| 1010 | list_del_init(&w->entry); |
| 1011 | return w; |
| 1012 | } |
| 1013 | |
| 1014 | static void |
| 1015 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
| 1016 | static void binder_free_thread(struct binder_thread *thread); |
| 1017 | static void binder_free_proc(struct binder_proc *proc); |
| 1018 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
| 1019 | |
| 1020 | static int task_get_unused_fd_flags(struct binder_proc *proc, int flags) |
| 1021 | { |
| 1022 | unsigned long rlim_cur; |
| 1023 | unsigned long irqs; |
| 1024 | int ret; |
| 1025 | |
| 1026 | mutex_lock(&proc->files_lock); |
| 1027 | if (proc->files == NULL) { |
| 1028 | ret = -ESRCH; |
| 1029 | goto err; |
| 1030 | } |
| 1031 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 1032 | ret = -EMFILE; |
| 1033 | goto err; |
| 1034 | } |
| 1035 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 1036 | unlock_task_sighand(proc->tsk, &irqs); |
| 1037 | |
| 1038 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 1039 | err: |
| 1040 | mutex_unlock(&proc->files_lock); |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * copied from fd_install |
| 1046 | */ |
| 1047 | static void task_fd_install( |
| 1048 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 1049 | { |
| 1050 | mutex_lock(&proc->files_lock); |
| 1051 | if (proc->files) |
| 1052 | __fd_install(proc->files, fd, file); |
| 1053 | mutex_unlock(&proc->files_lock); |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * copied from sys_close |
| 1058 | */ |
| 1059 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 1060 | { |
| 1061 | int retval; |
| 1062 | |
| 1063 | mutex_lock(&proc->files_lock); |
| 1064 | if (proc->files == NULL) { |
| 1065 | retval = -ESRCH; |
| 1066 | goto err; |
| 1067 | } |
| 1068 | retval = __close_fd(proc->files, fd); |
| 1069 | /* can't restart close syscall because file table entry was cleared */ |
| 1070 | if (unlikely(retval == -ERESTARTSYS || |
| 1071 | retval == -ERESTARTNOINTR || |
| 1072 | retval == -ERESTARTNOHAND || |
| 1073 | retval == -ERESTART_RESTARTBLOCK)) |
| 1074 | retval = -EINTR; |
| 1075 | err: |
| 1076 | mutex_unlock(&proc->files_lock); |
| 1077 | return retval; |
| 1078 | } |
| 1079 | |
| 1080 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 1081 | bool do_proc_work) |
| 1082 | { |
| 1083 | return thread->process_todo || |
| 1084 | thread->looper_need_return || |
| 1085 | (do_proc_work && |
| 1086 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 1087 | } |
| 1088 | |
| 1089 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 1090 | { |
| 1091 | bool has_work; |
| 1092 | |
| 1093 | binder_inner_proc_lock(thread->proc); |
| 1094 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 1095 | binder_inner_proc_unlock(thread->proc); |
| 1096 | |
| 1097 | return has_work; |
| 1098 | } |
| 1099 | |
| 1100 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 1101 | { |
| 1102 | return !thread->transaction_stack && |
| 1103 | binder_worklist_empty_ilocked(&thread->todo) && |
| 1104 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 1105 | BINDER_LOOPER_STATE_REGISTERED)); |
| 1106 | } |
| 1107 | |
| 1108 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 1109 | bool sync) |
| 1110 | { |
| 1111 | struct rb_node *n; |
| 1112 | struct binder_thread *thread; |
| 1113 | |
| 1114 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 1115 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 1116 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 1117 | binder_available_for_proc_work_ilocked(thread)) { |
| 1118 | if (sync) |
| 1119 | wake_up_interruptible_sync(&thread->wait); |
| 1120 | else |
| 1121 | wake_up_interruptible(&thread->wait); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1128 | * @proc: process to select a thread from |
| 1129 | * |
| 1130 | * Note that calling this function moves the thread off the waiting_threads |
| 1131 | * list, so it can only be woken up by the caller of this function, or a |
| 1132 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1133 | * returns. |
| 1134 | * |
| 1135 | * Return: If there's a thread currently waiting for process work, |
| 1136 | * returns that thread. Otherwise returns NULL. |
| 1137 | */ |
| 1138 | static struct binder_thread * |
| 1139 | binder_select_thread_ilocked(struct binder_proc *proc) |
| 1140 | { |
| 1141 | struct binder_thread *thread; |
| 1142 | |
| 1143 | assert_spin_locked(&proc->inner_lock); |
| 1144 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1145 | struct binder_thread, |
| 1146 | waiting_thread_node); |
| 1147 | |
| 1148 | if (thread) |
| 1149 | list_del_init(&thread->waiting_thread_node); |
| 1150 | |
| 1151 | return thread; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1156 | * @proc: process to wake up a thread in |
| 1157 | * @thread: specific thread to wake-up (may be NULL) |
| 1158 | * @sync: whether to do a synchronous wake-up |
| 1159 | * |
| 1160 | * This function wakes up a thread in the @proc process. |
| 1161 | * The caller may provide a specific thread to wake-up in |
| 1162 | * the @thread parameter. If @thread is NULL, this function |
| 1163 | * will wake up threads that have called poll(). |
| 1164 | * |
| 1165 | * Note that for this function to work as expected, callers |
| 1166 | * should first call binder_select_thread() to find a thread |
| 1167 | * to handle the work (if they don't have a thread already), |
| 1168 | * and pass the result into the @thread parameter. |
| 1169 | */ |
| 1170 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1171 | struct binder_thread *thread, |
| 1172 | bool sync) |
| 1173 | { |
| 1174 | assert_spin_locked(&proc->inner_lock); |
| 1175 | |
| 1176 | if (thread) { |
| 1177 | if (sync) |
| 1178 | wake_up_interruptible_sync(&thread->wait); |
| 1179 | else |
| 1180 | wake_up_interruptible(&thread->wait); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | /* Didn't find a thread waiting for proc work; this can happen |
| 1185 | * in two scenarios: |
| 1186 | * 1. All threads are busy handling transactions |
| 1187 | * In that case, one of those threads should call back into |
| 1188 | * the kernel driver soon and pick up this work. |
| 1189 | * 2. Threads are using the (e)poll interface, in which case |
| 1190 | * they may be blocked on the waitqueue without having been |
| 1191 | * added to waiting_threads. For this case, we just iterate |
| 1192 | * over all threads not handling transaction work, and |
| 1193 | * wake them all up. We wake all because we don't know whether |
| 1194 | * a thread that called into (e)poll is handling non-binder |
| 1195 | * work currently. |
| 1196 | */ |
| 1197 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1198 | } |
| 1199 | |
| 1200 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1201 | { |
| 1202 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1203 | |
| 1204 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1205 | } |
| 1206 | |
| 1207 | static bool is_rt_policy(int policy) |
| 1208 | { |
| 1209 | return policy == SCHED_FIFO || policy == SCHED_RR; |
| 1210 | } |
| 1211 | |
| 1212 | static bool is_fair_policy(int policy) |
| 1213 | { |
| 1214 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 1215 | } |
| 1216 | |
| 1217 | static bool binder_supported_policy(int policy) |
| 1218 | { |
| 1219 | return is_fair_policy(policy) || is_rt_policy(policy); |
| 1220 | } |
| 1221 | |
| 1222 | static int to_userspace_prio(int policy, int kernel_priority) |
| 1223 | { |
| 1224 | if (is_fair_policy(policy)) |
| 1225 | return PRIO_TO_NICE(kernel_priority); |
| 1226 | else |
| 1227 | return MAX_USER_RT_PRIO - 1 - kernel_priority; |
| 1228 | } |
| 1229 | |
| 1230 | static int to_kernel_prio(int policy, int user_priority) |
| 1231 | { |
| 1232 | if (is_fair_policy(policy)) |
| 1233 | return NICE_TO_PRIO(user_priority); |
| 1234 | else |
| 1235 | return MAX_USER_RT_PRIO - 1 - user_priority; |
| 1236 | } |
| 1237 | |
| 1238 | static void binder_do_set_priority(struct task_struct *task, |
| 1239 | struct binder_priority desired, |
| 1240 | bool verify) |
| 1241 | { |
| 1242 | int priority; /* user-space prio value */ |
| 1243 | bool has_cap_nice; |
| 1244 | unsigned int policy = desired.sched_policy; |
| 1245 | |
| 1246 | if (task->policy == policy && task->normal_prio == desired.prio) |
| 1247 | return; |
| 1248 | |
| 1249 | has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE); |
| 1250 | |
| 1251 | priority = to_userspace_prio(policy, desired.prio); |
| 1252 | |
| 1253 | if (verify && is_rt_policy(policy) && !has_cap_nice) { |
| 1254 | long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO); |
| 1255 | |
| 1256 | if (max_rtprio == 0) { |
| 1257 | policy = SCHED_NORMAL; |
| 1258 | priority = MIN_NICE; |
| 1259 | } else if (priority > max_rtprio) { |
| 1260 | priority = max_rtprio; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | if (verify && is_fair_policy(policy) && !has_cap_nice) { |
| 1265 | long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE)); |
| 1266 | |
| 1267 | if (min_nice > MAX_NICE) { |
| 1268 | binder_user_error("%d(%s) RLIMIT_NICE not set\n", |
| 1269 | task->pid, task->comm); |
| 1270 | return; |
| 1271 | } else if (priority < min_nice) { |
| 1272 | priority = min_nice; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | if (policy != desired.sched_policy || |
| 1277 | to_kernel_prio(policy, priority) != desired.prio) |
| 1278 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
| 1279 | "%d(%s): priority %d not allowed, using %d instead\n", |
| 1280 | task->pid, task->comm, desired.prio, |
| 1281 | to_kernel_prio(policy, priority)); |
| 1282 | |
| 1283 | trace_binder_set_priority(task->tgid, task->pid, task->normal_prio, |
| 1284 | to_kernel_prio(policy, priority), |
| 1285 | desired.prio); |
| 1286 | |
| 1287 | /* Set the actual priority */ |
| 1288 | if (task->policy != policy || is_rt_policy(policy)) { |
| 1289 | struct sched_param params; |
| 1290 | |
| 1291 | params.sched_priority = is_rt_policy(policy) ? priority : 0; |
| 1292 | |
| 1293 | sched_setscheduler_nocheck(task, |
| 1294 | policy | SCHED_RESET_ON_FORK, |
| 1295 | ¶ms); |
| 1296 | } |
| 1297 | if (is_fair_policy(policy)) |
| 1298 | set_user_nice(task, priority); |
| 1299 | } |
| 1300 | |
| 1301 | static void binder_set_priority(struct task_struct *task, |
| 1302 | struct binder_priority desired) |
| 1303 | { |
| 1304 | binder_do_set_priority(task, desired, /* verify = */ true); |
| 1305 | } |
| 1306 | |
| 1307 | static void binder_restore_priority(struct task_struct *task, |
| 1308 | struct binder_priority desired) |
| 1309 | { |
| 1310 | binder_do_set_priority(task, desired, /* verify = */ false); |
| 1311 | } |
| 1312 | |
| 1313 | static void binder_transaction_priority(struct task_struct *task, |
| 1314 | struct binder_transaction *t, |
| 1315 | struct binder_priority node_prio, |
| 1316 | bool inherit_rt) |
| 1317 | { |
| 1318 | struct binder_priority desired_prio = t->priority; |
| 1319 | |
| 1320 | if (t->set_priority_called) |
| 1321 | return; |
| 1322 | |
| 1323 | t->set_priority_called = true; |
| 1324 | t->saved_priority.sched_policy = task->policy; |
| 1325 | t->saved_priority.prio = task->normal_prio; |
| 1326 | |
| 1327 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { |
| 1328 | desired_prio.prio = NICE_TO_PRIO(0); |
| 1329 | desired_prio.sched_policy = SCHED_NORMAL; |
| 1330 | } |
| 1331 | |
| 1332 | if (node_prio.prio < t->priority.prio || |
| 1333 | (node_prio.prio == t->priority.prio && |
| 1334 | node_prio.sched_policy == SCHED_FIFO)) { |
| 1335 | /* |
| 1336 | * In case the minimum priority on the node is |
| 1337 | * higher (lower value), use that priority. If |
| 1338 | * the priority is the same, but the node uses |
| 1339 | * SCHED_FIFO, prefer SCHED_FIFO, since it can |
| 1340 | * run unbounded, unlike SCHED_RR. |
| 1341 | */ |
| 1342 | desired_prio = node_prio; |
| 1343 | } |
| 1344 | |
| 1345 | binder_set_priority(task, desired_prio); |
| 1346 | } |
| 1347 | |
| 1348 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1349 | binder_uintptr_t ptr) |
| 1350 | { |
| 1351 | struct rb_node *n = proc->nodes.rb_node; |
| 1352 | struct binder_node *node; |
| 1353 | |
| 1354 | assert_spin_locked(&proc->inner_lock); |
| 1355 | |
| 1356 | while (n) { |
| 1357 | node = rb_entry(n, struct binder_node, rb_node); |
| 1358 | |
| 1359 | if (ptr < node->ptr) |
| 1360 | n = n->rb_left; |
| 1361 | else if (ptr > node->ptr) |
| 1362 | n = n->rb_right; |
| 1363 | else { |
| 1364 | /* |
| 1365 | * take an implicit weak reference |
| 1366 | * to ensure node stays alive until |
| 1367 | * call to binder_put_node() |
| 1368 | */ |
| 1369 | binder_inc_node_tmpref_ilocked(node); |
| 1370 | return node; |
| 1371 | } |
| 1372 | } |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | |
| 1376 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1377 | binder_uintptr_t ptr) |
| 1378 | { |
| 1379 | struct binder_node *node; |
| 1380 | |
| 1381 | binder_inner_proc_lock(proc); |
| 1382 | node = binder_get_node_ilocked(proc, ptr); |
| 1383 | binder_inner_proc_unlock(proc); |
| 1384 | return node; |
| 1385 | } |
| 1386 | |
| 1387 | static struct binder_node *binder_init_node_ilocked( |
| 1388 | struct binder_proc *proc, |
| 1389 | struct binder_node *new_node, |
| 1390 | struct flat_binder_object *fp) |
| 1391 | { |
| 1392 | struct rb_node **p = &proc->nodes.rb_node; |
| 1393 | struct rb_node *parent = NULL; |
| 1394 | struct binder_node *node; |
| 1395 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1396 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1397 | __u32 flags = fp ? fp->flags : 0; |
| 1398 | s8 priority; |
| 1399 | |
| 1400 | assert_spin_locked(&proc->inner_lock); |
| 1401 | |
| 1402 | while (*p) { |
| 1403 | |
| 1404 | parent = *p; |
| 1405 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1406 | |
| 1407 | if (ptr < node->ptr) |
| 1408 | p = &(*p)->rb_left; |
| 1409 | else if (ptr > node->ptr) |
| 1410 | p = &(*p)->rb_right; |
| 1411 | else { |
| 1412 | /* |
| 1413 | * A matching node is already in |
| 1414 | * the rb tree. Abandon the init |
| 1415 | * and return it. |
| 1416 | */ |
| 1417 | binder_inc_node_tmpref_ilocked(node); |
| 1418 | return node; |
| 1419 | } |
| 1420 | } |
| 1421 | node = new_node; |
| 1422 | binder_stats_created(BINDER_STAT_NODE); |
| 1423 | node->tmp_refs++; |
| 1424 | rb_link_node(&node->rb_node, parent, p); |
| 1425 | rb_insert_color(&node->rb_node, &proc->nodes); |
| 1426 | node->debug_id = atomic_inc_return(&binder_last_id); |
| 1427 | node->proc = proc; |
| 1428 | node->ptr = ptr; |
| 1429 | node->cookie = cookie; |
| 1430 | node->work.type = BINDER_WORK_NODE; |
| 1431 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1432 | node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >> |
| 1433 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; |
| 1434 | node->min_priority = to_kernel_prio(node->sched_policy, priority); |
| 1435 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
| 1436 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); |
| 1437 | node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX); |
| 1438 | spin_lock_init(&node->lock); |
| 1439 | INIT_LIST_HEAD(&node->work.entry); |
| 1440 | INIT_LIST_HEAD(&node->async_todo); |
| 1441 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1442 | "%d:%d(%s:%s) node %d u%016llx c%016llx created\n", |
| 1443 | proc->pid, current->pid, proc->tsk->comm, current->comm, node->debug_id, |
| 1444 | (u64)node->ptr, (u64)node->cookie); |
| 1445 | |
| 1446 | return node; |
| 1447 | } |
| 1448 | |
| 1449 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1450 | struct flat_binder_object *fp) |
| 1451 | { |
| 1452 | struct binder_node *node; |
| 1453 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1454 | |
| 1455 | if (!new_node) |
| 1456 | return NULL; |
| 1457 | binder_inner_proc_lock(proc); |
| 1458 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1459 | binder_inner_proc_unlock(proc); |
| 1460 | if (node != new_node) |
| 1461 | /* |
| 1462 | * The node was already added by another thread |
| 1463 | */ |
| 1464 | kfree(new_node); |
| 1465 | |
| 1466 | return node; |
| 1467 | } |
| 1468 | |
| 1469 | static void binder_free_node(struct binder_node *node) |
| 1470 | { |
| 1471 | kfree(node); |
| 1472 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1473 | } |
| 1474 | |
| 1475 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1476 | int internal, |
| 1477 | struct list_head *target_list) |
| 1478 | { |
| 1479 | struct binder_proc *proc = node->proc; |
| 1480 | |
| 1481 | assert_spin_locked(&node->lock); |
| 1482 | if (proc) |
| 1483 | assert_spin_locked(&proc->inner_lock); |
| 1484 | if (strong) { |
| 1485 | if (internal) { |
| 1486 | if (target_list == NULL && |
| 1487 | node->internal_strong_refs == 0 && |
| 1488 | !(node->proc && |
| 1489 | node == node->proc->context->binder_context_mgr_node && |
| 1490 | node->has_strong_ref)) { |
| 1491 | pr_err("invalid inc strong node for %d\n", |
| 1492 | node->debug_id); |
| 1493 | return -EINVAL; |
| 1494 | } |
| 1495 | node->internal_strong_refs++; |
| 1496 | } else |
| 1497 | node->local_strong_refs++; |
| 1498 | if (!node->has_strong_ref && target_list) { |
| 1499 | binder_dequeue_work_ilocked(&node->work); |
| 1500 | /* |
| 1501 | * Note: this function is the only place where we queue |
| 1502 | * directly to a thread->todo without using the |
| 1503 | * corresponding binder_enqueue_thread_work() helper |
| 1504 | * functions; in this case it's ok to not set the |
| 1505 | * process_todo flag, since we know this node work will |
| 1506 | * always be followed by other work that starts queue |
| 1507 | * processing: in case of synchronous transactions, a |
| 1508 | * BR_REPLY or BR_ERROR; in case of oneway |
| 1509 | * transactions, a BR_TRANSACTION_COMPLETE. |
| 1510 | */ |
| 1511 | binder_enqueue_work_ilocked(&node->work, target_list); |
| 1512 | } |
| 1513 | } else { |
| 1514 | if (!internal) |
| 1515 | node->local_weak_refs++; |
| 1516 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1517 | if (target_list == NULL) { |
| 1518 | pr_err("invalid inc weak node for %d\n", |
| 1519 | node->debug_id); |
| 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | /* |
| 1523 | * See comment above |
| 1524 | */ |
| 1525 | binder_enqueue_work_ilocked(&node->work, target_list); |
| 1526 | } |
| 1527 | } |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1532 | struct list_head *target_list) |
| 1533 | { |
| 1534 | int ret; |
| 1535 | |
| 1536 | binder_node_inner_lock(node); |
| 1537 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1538 | binder_node_inner_unlock(node); |
| 1539 | |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1544 | int strong, int internal) |
| 1545 | { |
| 1546 | struct binder_proc *proc = node->proc; |
| 1547 | |
| 1548 | assert_spin_locked(&node->lock); |
| 1549 | if (proc) |
| 1550 | assert_spin_locked(&proc->inner_lock); |
| 1551 | if (strong) { |
| 1552 | if (internal) |
| 1553 | node->internal_strong_refs--; |
| 1554 | else |
| 1555 | node->local_strong_refs--; |
| 1556 | if (node->local_strong_refs || node->internal_strong_refs) |
| 1557 | return false; |
| 1558 | } else { |
| 1559 | if (!internal) |
| 1560 | node->local_weak_refs--; |
| 1561 | if (node->local_weak_refs || node->tmp_refs || |
| 1562 | !hlist_empty(&node->refs)) |
| 1563 | return false; |
| 1564 | } |
| 1565 | |
| 1566 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
| 1567 | if (list_empty(&node->work.entry)) { |
| 1568 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
| 1569 | binder_wakeup_proc_ilocked(proc); |
| 1570 | } |
| 1571 | } else { |
| 1572 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
| 1573 | !node->local_weak_refs && !node->tmp_refs) { |
| 1574 | if (proc) { |
| 1575 | binder_dequeue_work_ilocked(&node->work); |
| 1576 | rb_erase(&node->rb_node, &proc->nodes); |
| 1577 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1578 | "refless node %d deleted\n", |
| 1579 | node->debug_id); |
| 1580 | } else { |
| 1581 | BUG_ON(!list_empty(&node->work.entry)); |
| 1582 | spin_lock(&binder_dead_nodes_lock); |
| 1583 | /* |
| 1584 | * tmp_refs could have changed so |
| 1585 | * check it again |
| 1586 | */ |
| 1587 | if (node->tmp_refs) { |
| 1588 | spin_unlock(&binder_dead_nodes_lock); |
| 1589 | return false; |
| 1590 | } |
| 1591 | hlist_del(&node->dead_node); |
| 1592 | spin_unlock(&binder_dead_nodes_lock); |
| 1593 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1594 | "dead node %d deleted\n", |
| 1595 | node->debug_id); |
| 1596 | } |
| 1597 | return true; |
| 1598 | } |
| 1599 | } |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
| 1603 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1604 | { |
| 1605 | bool free_node; |
| 1606 | |
| 1607 | binder_node_inner_lock(node); |
| 1608 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1609 | binder_node_inner_unlock(node); |
| 1610 | if (free_node) |
| 1611 | binder_free_node(node); |
| 1612 | } |
| 1613 | |
| 1614 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
| 1615 | { |
| 1616 | /* |
| 1617 | * No call to binder_inc_node() is needed since we |
| 1618 | * don't need to inform userspace of any changes to |
| 1619 | * tmp_refs |
| 1620 | */ |
| 1621 | node->tmp_refs++; |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1626 | * @node: node to reference |
| 1627 | * |
| 1628 | * Take reference on node to prevent the node from being freed |
| 1629 | * while referenced only by a local variable. The inner lock is |
| 1630 | * needed to serialize with the node work on the queue (which |
| 1631 | * isn't needed after the node is dead). If the node is dead |
| 1632 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1633 | * node->tmp_refs against dead-node-only cases where the node |
| 1634 | * lock cannot be acquired (eg traversing the dead node list to |
| 1635 | * print nodes) |
| 1636 | */ |
| 1637 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1638 | { |
| 1639 | binder_node_lock(node); |
| 1640 | if (node->proc) |
| 1641 | binder_inner_proc_lock(node->proc); |
| 1642 | else |
| 1643 | spin_lock(&binder_dead_nodes_lock); |
| 1644 | binder_inc_node_tmpref_ilocked(node); |
| 1645 | if (node->proc) |
| 1646 | binder_inner_proc_unlock(node->proc); |
| 1647 | else |
| 1648 | spin_unlock(&binder_dead_nodes_lock); |
| 1649 | binder_node_unlock(node); |
| 1650 | } |
| 1651 | |
| 1652 | /** |
| 1653 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1654 | * @node: node to reference |
| 1655 | * |
| 1656 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1657 | */ |
| 1658 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1659 | { |
| 1660 | bool free_node; |
| 1661 | |
| 1662 | binder_node_inner_lock(node); |
| 1663 | if (!node->proc) |
| 1664 | spin_lock(&binder_dead_nodes_lock); |
| 1665 | node->tmp_refs--; |
| 1666 | BUG_ON(node->tmp_refs < 0); |
| 1667 | if (!node->proc) |
| 1668 | spin_unlock(&binder_dead_nodes_lock); |
| 1669 | /* |
| 1670 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1671 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1672 | * causes no actual reference to be released in binder_dec_node(). |
| 1673 | * If that changes, a change is needed here too. |
| 1674 | */ |
| 1675 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1676 | binder_node_inner_unlock(node); |
| 1677 | if (free_node) |
| 1678 | binder_free_node(node); |
| 1679 | } |
| 1680 | |
| 1681 | static void binder_put_node(struct binder_node *node) |
| 1682 | { |
| 1683 | binder_dec_node_tmpref(node); |
| 1684 | } |
| 1685 | |
| 1686 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1687 | u32 desc, bool need_strong_ref) |
| 1688 | { |
| 1689 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1690 | struct binder_ref *ref; |
| 1691 | |
| 1692 | while (n) { |
| 1693 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1694 | |
| 1695 | if (desc < ref->data.desc) { |
| 1696 | n = n->rb_left; |
| 1697 | } else if (desc > ref->data.desc) { |
| 1698 | n = n->rb_right; |
| 1699 | } else if (need_strong_ref && !ref->data.strong) { |
| 1700 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1701 | return NULL; |
| 1702 | } else { |
| 1703 | return ref; |
| 1704 | } |
| 1705 | } |
| 1706 | return NULL; |
| 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * binder_get_ref_for_node_olocked() - get the ref associated with given node |
| 1711 | * @proc: binder_proc that owns the ref |
| 1712 | * @node: binder_node of target |
| 1713 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1714 | * |
| 1715 | * Look up the ref for the given node and return it if it exists |
| 1716 | * |
| 1717 | * If it doesn't exist and the caller provides a newly allocated |
| 1718 | * ref, initialize the fields of the newly allocated ref and insert |
| 1719 | * into the given proc rb_trees and node refs list. |
| 1720 | * |
| 1721 | * Return: the ref for node. It is possible that another thread |
| 1722 | * allocated/initialized the ref first in which case the |
| 1723 | * returned ref would be different than the passed-in |
| 1724 | * new_ref. new_ref must be kfree'd by the caller in |
| 1725 | * this case. |
| 1726 | */ |
| 1727 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1728 | struct binder_proc *proc, |
| 1729 | struct binder_node *node, |
| 1730 | struct binder_ref *new_ref) |
| 1731 | { |
| 1732 | struct binder_context *context = proc->context; |
| 1733 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1734 | struct rb_node *parent = NULL; |
| 1735 | struct binder_ref *ref; |
| 1736 | struct rb_node *n; |
| 1737 | |
| 1738 | while (*p) { |
| 1739 | parent = *p; |
| 1740 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1741 | |
| 1742 | if (node < ref->node) |
| 1743 | p = &(*p)->rb_left; |
| 1744 | else if (node > ref->node) |
| 1745 | p = &(*p)->rb_right; |
| 1746 | else |
| 1747 | return ref; |
| 1748 | } |
| 1749 | if (!new_ref) |
| 1750 | return NULL; |
| 1751 | |
| 1752 | binder_stats_created(BINDER_STAT_REF); |
| 1753 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
| 1754 | new_ref->proc = proc; |
| 1755 | new_ref->node = node; |
| 1756 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1757 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1758 | |
| 1759 | new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1; |
| 1760 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1761 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1762 | if (ref->data.desc > new_ref->data.desc) |
| 1763 | break; |
| 1764 | new_ref->data.desc = ref->data.desc + 1; |
| 1765 | } |
| 1766 | |
| 1767 | p = &proc->refs_by_desc.rb_node; |
| 1768 | while (*p) { |
| 1769 | parent = *p; |
| 1770 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1771 | |
| 1772 | if (new_ref->data.desc < ref->data.desc) |
| 1773 | p = &(*p)->rb_left; |
| 1774 | else if (new_ref->data.desc > ref->data.desc) |
| 1775 | p = &(*p)->rb_right; |
| 1776 | else |
| 1777 | BUG(); |
| 1778 | } |
| 1779 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1780 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
| 1781 | |
| 1782 | binder_node_lock(node); |
| 1783 | hlist_add_head(&new_ref->node_entry, &node->refs); |
| 1784 | |
| 1785 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1786 | "%d(%s) new ref %d desc %d for node %d\n", |
| 1787 | proc->pid, proc->tsk->comm, new_ref->data.debug_id, new_ref->data.desc, |
| 1788 | node->debug_id); |
| 1789 | binder_node_unlock(node); |
| 1790 | return new_ref; |
| 1791 | } |
| 1792 | |
| 1793 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
| 1794 | { |
| 1795 | bool delete_node = false; |
| 1796 | |
| 1797 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1798 | "%d(%s) delete ref %d desc %d for node %d\n", |
| 1799 | ref->proc->pid, ref->proc->tsk->comm, ref->data.debug_id, ref->data.desc, |
| 1800 | ref->node->debug_id); |
| 1801 | |
| 1802 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1803 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
| 1804 | |
| 1805 | binder_node_inner_lock(ref->node); |
| 1806 | if (ref->data.strong) |
| 1807 | binder_dec_node_nilocked(ref->node, 1, 1); |
| 1808 | |
| 1809 | hlist_del(&ref->node_entry); |
| 1810 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1811 | binder_node_inner_unlock(ref->node); |
| 1812 | /* |
| 1813 | * Clear ref->node unless we want the caller to free the node |
| 1814 | */ |
| 1815 | if (!delete_node) { |
| 1816 | /* |
| 1817 | * The caller uses ref->node to determine |
| 1818 | * whether the node needs to be freed. Clear |
| 1819 | * it since the node is still alive. |
| 1820 | */ |
| 1821 | ref->node = NULL; |
| 1822 | } |
| 1823 | |
| 1824 | if (ref->death) { |
| 1825 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1826 | "%d(%s) delete ref %d desc %d has death notification\n", |
| 1827 | ref->proc->pid, ref->proc->tsk->comm, ref->data.debug_id, |
| 1828 | ref->data.desc); |
| 1829 | binder_dequeue_work(ref->proc, &ref->death->work); |
| 1830 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1831 | } |
| 1832 | binder_stats_deleted(BINDER_STAT_REF); |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * binder_inc_ref_olocked() - increment the ref for given handle |
| 1837 | * @ref: ref to be incremented |
| 1838 | * @strong: if true, strong increment, else weak |
| 1839 | * @target_list: list to queue node work on |
| 1840 | * |
| 1841 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
| 1842 | * |
| 1843 | * Return: 0, if successful, else errno |
| 1844 | */ |
| 1845 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1846 | struct list_head *target_list) |
| 1847 | { |
| 1848 | int ret; |
| 1849 | |
| 1850 | if (strong) { |
| 1851 | if (ref->data.strong == 0) { |
| 1852 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1853 | if (ret) |
| 1854 | return ret; |
| 1855 | } |
| 1856 | ref->data.strong++; |
| 1857 | } else { |
| 1858 | if (ref->data.weak == 0) { |
| 1859 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1860 | if (ret) |
| 1861 | return ret; |
| 1862 | } |
| 1863 | ref->data.weak++; |
| 1864 | } |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * binder_dec_ref() - dec the ref for given handle |
| 1870 | * @ref: ref to be decremented |
| 1871 | * @strong: if true, strong decrement, else weak |
| 1872 | * |
| 1873 | * Decrement the ref. |
| 1874 | * |
| 1875 | * Return: true if ref is cleaned up and ready to be freed |
| 1876 | */ |
| 1877 | static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong) |
| 1878 | { |
| 1879 | if (strong) { |
| 1880 | if (ref->data.strong == 0) { |
| 1881 | binder_user_error("%d(%s) invalid dec strong, ref %d desc %d s %d w %d\n", |
| 1882 | ref->proc->pid, ref->proc->tsk->comm, ref->data.debug_id, |
| 1883 | ref->data.desc, ref->data.strong, |
| 1884 | ref->data.weak); |
| 1885 | return false; |
| 1886 | } |
| 1887 | ref->data.strong--; |
| 1888 | if (ref->data.strong == 0) |
| 1889 | binder_dec_node(ref->node, strong, 1); |
| 1890 | } else { |
| 1891 | if (ref->data.weak == 0) { |
| 1892 | binder_user_error("%d(%s) invalid dec weak, ref %d desc %d s %d w %d\n", |
| 1893 | ref->proc->pid, ref->proc->tsk->comm, ref->data.debug_id, |
| 1894 | ref->data.desc, ref->data.strong, |
| 1895 | ref->data.weak); |
| 1896 | return false; |
| 1897 | } |
| 1898 | ref->data.weak--; |
| 1899 | } |
| 1900 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
| 1901 | binder_cleanup_ref_olocked(ref); |
| 1902 | return true; |
| 1903 | } |
| 1904 | return false; |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1909 | * @proc: proc containing the ref |
| 1910 | * @desc: the handle associated with the ref |
| 1911 | * @need_strong_ref: if true, only return node if ref is strong |
| 1912 | * @rdata: the id/refcount data for the ref |
| 1913 | * |
| 1914 | * Given a proc and ref handle, return the associated binder_node |
| 1915 | * |
| 1916 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1917 | */ |
| 1918 | static struct binder_node *binder_get_node_from_ref( |
| 1919 | struct binder_proc *proc, |
| 1920 | u32 desc, bool need_strong_ref, |
| 1921 | struct binder_ref_data *rdata) |
| 1922 | { |
| 1923 | struct binder_node *node; |
| 1924 | struct binder_ref *ref; |
| 1925 | |
| 1926 | binder_proc_lock(proc); |
| 1927 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
| 1928 | if (!ref) |
| 1929 | goto err_no_ref; |
| 1930 | node = ref->node; |
| 1931 | /* |
| 1932 | * Take an implicit reference on the node to ensure |
| 1933 | * it stays alive until the call to binder_put_node() |
| 1934 | */ |
| 1935 | binder_inc_node_tmpref(node); |
| 1936 | if (rdata) |
| 1937 | *rdata = ref->data; |
| 1938 | binder_proc_unlock(proc); |
| 1939 | |
| 1940 | return node; |
| 1941 | |
| 1942 | err_no_ref: |
| 1943 | binder_proc_unlock(proc); |
| 1944 | return NULL; |
| 1945 | } |
| 1946 | |
| 1947 | /** |
| 1948 | * binder_free_ref() - free the binder_ref |
| 1949 | * @ref: ref to free |
| 1950 | * |
| 1951 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1952 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
| 1953 | */ |
| 1954 | static void binder_free_ref(struct binder_ref *ref) |
| 1955 | { |
| 1956 | if (ref->node) |
| 1957 | binder_free_node(ref->node); |
| 1958 | kfree(ref->death); |
| 1959 | kfree(ref); |
| 1960 | } |
| 1961 | |
| 1962 | /** |
| 1963 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1964 | * @proc: proc containing the ref |
| 1965 | * @desc: the handle associated with the ref |
| 1966 | * @increment: true=inc reference, false=dec reference |
| 1967 | * @strong: true=strong reference, false=weak reference |
| 1968 | * @rdata: the id/refcount data for the ref |
| 1969 | * |
| 1970 | * Given a proc and ref handle, increment or decrement the ref |
| 1971 | * according to "increment" arg. |
| 1972 | * |
| 1973 | * Return: 0 if successful, else errno |
| 1974 | */ |
| 1975 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1976 | uint32_t desc, bool increment, bool strong, |
| 1977 | struct binder_ref_data *rdata) |
| 1978 | { |
| 1979 | int ret = 0; |
| 1980 | struct binder_ref *ref; |
| 1981 | bool delete_ref = false; |
| 1982 | |
| 1983 | binder_proc_lock(proc); |
| 1984 | ref = binder_get_ref_olocked(proc, desc, strong); |
| 1985 | if (!ref) { |
| 1986 | ret = -EINVAL; |
| 1987 | goto err_no_ref; |
| 1988 | } |
| 1989 | if (increment) |
| 1990 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
| 1991 | else |
| 1992 | delete_ref = binder_dec_ref_olocked(ref, strong); |
| 1993 | |
| 1994 | if (rdata) |
| 1995 | *rdata = ref->data; |
| 1996 | binder_proc_unlock(proc); |
| 1997 | |
| 1998 | if (delete_ref) |
| 1999 | binder_free_ref(ref); |
| 2000 | return ret; |
| 2001 | |
| 2002 | err_no_ref: |
| 2003 | binder_proc_unlock(proc); |
| 2004 | return ret; |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 2009 | * @proc: proc containing the ref |
| 2010 | * @desc: the handle associated with the ref |
| 2011 | * @strong: true=strong reference, false=weak reference |
| 2012 | * @rdata: the id/refcount data for the ref |
| 2013 | * |
| 2014 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 2015 | * |
| 2016 | * Return: 0 if successful, else errno |
| 2017 | */ |
| 2018 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 2019 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 2020 | { |
| 2021 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | /** |
| 2026 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 2027 | * @proc: proc containing the ref |
| 2028 | * @node: target node |
| 2029 | * @strong: true=strong reference, false=weak reference |
| 2030 | * @target_list: worklist to use if node is incremented |
| 2031 | * @rdata: the id/refcount data for the ref |
| 2032 | * |
| 2033 | * Given a proc and node, increment the ref. Create the ref if it |
| 2034 | * doesn't already exist |
| 2035 | * |
| 2036 | * Return: 0 if successful, else errno |
| 2037 | */ |
| 2038 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 2039 | struct binder_node *node, |
| 2040 | bool strong, |
| 2041 | struct list_head *target_list, |
| 2042 | struct binder_ref_data *rdata) |
| 2043 | { |
| 2044 | struct binder_ref *ref; |
| 2045 | struct binder_ref *new_ref = NULL; |
| 2046 | int ret = 0; |
| 2047 | |
| 2048 | binder_proc_lock(proc); |
| 2049 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
| 2050 | if (!ref) { |
| 2051 | binder_proc_unlock(proc); |
| 2052 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 2053 | if (!new_ref) |
| 2054 | return -ENOMEM; |
| 2055 | binder_proc_lock(proc); |
| 2056 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
| 2057 | } |
| 2058 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
| 2059 | *rdata = ref->data; |
| 2060 | binder_proc_unlock(proc); |
| 2061 | if (new_ref && ref != new_ref) |
| 2062 | /* |
| 2063 | * Another thread created the ref first so |
| 2064 | * free the one we allocated |
| 2065 | */ |
| 2066 | kfree(new_ref); |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
| 2070 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 2071 | struct binder_transaction *t) |
| 2072 | { |
| 2073 | BUG_ON(!target_thread); |
| 2074 | assert_spin_locked(&target_thread->proc->inner_lock); |
| 2075 | BUG_ON(target_thread->transaction_stack != t); |
| 2076 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 2077 | target_thread->transaction_stack = |
| 2078 | target_thread->transaction_stack->from_parent; |
| 2079 | t->from = NULL; |
| 2080 | } |
| 2081 | |
| 2082 | /** |
| 2083 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 2084 | * @thread: thread to decrement |
| 2085 | * |
| 2086 | * A thread needs to be kept alive while being used to create or |
| 2087 | * handle a transaction. binder_get_txn_from() is used to safely |
| 2088 | * extract t->from from a binder_transaction and keep the thread |
| 2089 | * indicated by t->from from being freed. When done with that |
| 2090 | * binder_thread, this function is called to decrement the |
| 2091 | * tmp_ref and free if appropriate (thread has been released |
| 2092 | * and no transaction being processed by the driver) |
| 2093 | */ |
| 2094 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 2095 | { |
| 2096 | /* |
| 2097 | * atomic is used to protect the counter value while |
| 2098 | * it cannot reach zero or thread->is_dead is false |
| 2099 | */ |
| 2100 | binder_inner_proc_lock(thread->proc); |
| 2101 | atomic_dec(&thread->tmp_ref); |
| 2102 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
| 2103 | binder_inner_proc_unlock(thread->proc); |
| 2104 | binder_free_thread(thread); |
| 2105 | return; |
| 2106 | } |
| 2107 | binder_inner_proc_unlock(thread->proc); |
| 2108 | } |
| 2109 | |
| 2110 | /** |
| 2111 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 2112 | * @proc: proc to decrement |
| 2113 | * |
| 2114 | * A binder_proc needs to be kept alive while being used to create or |
| 2115 | * handle a transaction. proc->tmp_ref is incremented when |
| 2116 | * creating a new transaction or the binder_proc is currently in-use |
| 2117 | * by threads that are being released. When done with the binder_proc, |
| 2118 | * this function is called to decrement the counter and free the |
| 2119 | * proc if appropriate (proc has been released, all threads have |
| 2120 | * been released and not currenly in-use to process a transaction). |
| 2121 | */ |
| 2122 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 2123 | { |
| 2124 | binder_inner_proc_lock(proc); |
| 2125 | proc->tmp_ref--; |
| 2126 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 2127 | !proc->tmp_ref) { |
| 2128 | binder_inner_proc_unlock(proc); |
| 2129 | binder_free_proc(proc); |
| 2130 | return; |
| 2131 | } |
| 2132 | binder_inner_proc_unlock(proc); |
| 2133 | } |
| 2134 | |
| 2135 | /** |
| 2136 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 2137 | * @t: binder transaction for t->from |
| 2138 | * |
| 2139 | * Atomically return the "from" thread and increment the tmp_ref |
| 2140 | * count for the thread to ensure it stays alive until |
| 2141 | * binder_thread_dec_tmpref() is called. |
| 2142 | * |
| 2143 | * Return: the value of t->from |
| 2144 | */ |
| 2145 | static struct binder_thread *binder_get_txn_from( |
| 2146 | struct binder_transaction *t) |
| 2147 | { |
| 2148 | struct binder_thread *from; |
| 2149 | |
| 2150 | spin_lock(&t->lock); |
| 2151 | from = t->from; |
| 2152 | if (from) |
| 2153 | atomic_inc(&from->tmp_ref); |
| 2154 | spin_unlock(&t->lock); |
| 2155 | return from; |
| 2156 | } |
| 2157 | |
| 2158 | /** |
| 2159 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 2160 | * @t: binder transaction for t->from |
| 2161 | * |
| 2162 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 2163 | * to guarantee that the thread cannot be released while operating on it. |
| 2164 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 2165 | * as well as call binder_dec_thread_txn() to release the reference. |
| 2166 | * |
| 2167 | * Return: the value of t->from |
| 2168 | */ |
| 2169 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 2170 | struct binder_transaction *t) |
| 2171 | { |
| 2172 | struct binder_thread *from; |
| 2173 | |
| 2174 | from = binder_get_txn_from(t); |
| 2175 | if (!from) |
| 2176 | return NULL; |
| 2177 | binder_inner_proc_lock(from->proc); |
| 2178 | if (t->from) { |
| 2179 | BUG_ON(from != t->from); |
| 2180 | return from; |
| 2181 | } |
| 2182 | binder_inner_proc_unlock(from->proc); |
| 2183 | binder_thread_dec_tmpref(from); |
| 2184 | return NULL; |
| 2185 | } |
| 2186 | |
| 2187 | static void binder_free_transaction(struct binder_transaction *t) |
| 2188 | { |
| 2189 | struct binder_proc *target_proc = t->to_proc; |
| 2190 | |
| 2191 | if (target_proc) { |
| 2192 | binder_inner_proc_lock(target_proc); |
| 2193 | if (t->buffer) |
| 2194 | t->buffer->transaction = NULL; |
| 2195 | binder_inner_proc_unlock(target_proc); |
| 2196 | } |
| 2197 | /* |
| 2198 | * If the transaction has no target_proc, then |
| 2199 | * t->buffer->transaction has already been cleared. |
| 2200 | */ |
| 2201 | kfree(t); |
| 2202 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2203 | } |
| 2204 | |
| 2205 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 2206 | uint32_t error_code) |
| 2207 | { |
| 2208 | struct binder_thread *target_thread; |
| 2209 | struct binder_transaction *next; |
| 2210 | |
| 2211 | BUG_ON(t->flags & TF_ONE_WAY); |
| 2212 | while (1) { |
| 2213 | target_thread = binder_get_txn_from_and_acq_inner(t); |
| 2214 | if (target_thread) { |
| 2215 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2216 | "send failed reply for transaction %d to %d:%d(%s:%s)\n", |
| 2217 | t->debug_id, |
| 2218 | target_thread->proc->pid, |
| 2219 | target_thread->pid, |
| 2220 | target_thread->proc->tsk->comm, |
| 2221 | target_thread->task->comm); |
| 2222 | |
| 2223 | binder_pop_transaction_ilocked(target_thread, t); |
| 2224 | if (target_thread->reply_error.cmd == BR_OK) { |
| 2225 | target_thread->reply_error.cmd = error_code; |
| 2226 | binder_enqueue_thread_work_ilocked( |
| 2227 | target_thread, |
| 2228 | &target_thread->reply_error.work); |
| 2229 | wake_up_interruptible(&target_thread->wait); |
| 2230 | } else { |
| 2231 | /* |
| 2232 | * Cannot get here for normal operation, but |
| 2233 | * we can if multiple synchronous transactions |
| 2234 | * are sent without blocking for responses. |
| 2235 | * Just ignore the 2nd error in this case. |
| 2236 | */ |
| 2237 | pr_warn("Unexpected reply error: %u\n", |
| 2238 | target_thread->reply_error.cmd); |
| 2239 | } |
| 2240 | binder_inner_proc_unlock(target_thread->proc); |
| 2241 | binder_thread_dec_tmpref(target_thread); |
| 2242 | binder_free_transaction(t); |
| 2243 | return; |
| 2244 | } |
| 2245 | next = t->from_parent; |
| 2246 | |
| 2247 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2248 | "send failed reply for transaction %d, target dead\n", |
| 2249 | t->debug_id); |
| 2250 | |
| 2251 | binder_free_transaction(t); |
| 2252 | if (next == NULL) { |
| 2253 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2254 | "reply failed, no target thread at root\n"); |
| 2255 | return; |
| 2256 | } |
| 2257 | t = next; |
| 2258 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2259 | "reply failed, no target thread -- retry %d\n", |
| 2260 | t->debug_id); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | /** |
| 2265 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2266 | * @t: transaction that needs to be cleaned up |
| 2267 | * @reason: reason the transaction wasn't delivered |
| 2268 | * @error_code: error to return to caller (if synchronous call) |
| 2269 | */ |
| 2270 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2271 | const char *reason, |
| 2272 | uint32_t error_code) |
| 2273 | { |
| 2274 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2275 | binder_send_failed_reply(t, error_code); |
| 2276 | } else { |
| 2277 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2278 | "undelivered transaction %d, %s\n", |
| 2279 | t->debug_id, reason); |
| 2280 | binder_free_transaction(t); |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | /** |
| 2285 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 2286 | * @buffer: binder_buffer that we're parsing. |
| 2287 | * @offset: offset in the buffer at which to validate an object. |
| 2288 | * |
| 2289 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 2290 | * size of that object. Otherwise, it returns zero. |
| 2291 | */ |
| 2292 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 2293 | { |
| 2294 | /* Check if we can read a header first */ |
| 2295 | struct binder_object_header *hdr; |
| 2296 | size_t object_size = 0; |
| 2297 | |
| 2298 | if (buffer->data_size < sizeof(*hdr) || |
| 2299 | offset > buffer->data_size - sizeof(*hdr) || |
| 2300 | !IS_ALIGNED(offset, sizeof(u32))) |
| 2301 | return 0; |
| 2302 | |
| 2303 | /* Ok, now see if we can read a complete object. */ |
| 2304 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 2305 | switch (hdr->type) { |
| 2306 | case BINDER_TYPE_BINDER: |
| 2307 | case BINDER_TYPE_WEAK_BINDER: |
| 2308 | case BINDER_TYPE_HANDLE: |
| 2309 | case BINDER_TYPE_WEAK_HANDLE: |
| 2310 | object_size = sizeof(struct flat_binder_object); |
| 2311 | break; |
| 2312 | case BINDER_TYPE_FD: |
| 2313 | object_size = sizeof(struct binder_fd_object); |
| 2314 | break; |
| 2315 | case BINDER_TYPE_PTR: |
| 2316 | object_size = sizeof(struct binder_buffer_object); |
| 2317 | break; |
| 2318 | case BINDER_TYPE_FDA: |
| 2319 | object_size = sizeof(struct binder_fd_array_object); |
| 2320 | break; |
| 2321 | default: |
| 2322 | return 0; |
| 2323 | } |
| 2324 | if (offset <= buffer->data_size - object_size && |
| 2325 | buffer->data_size >= object_size) |
| 2326 | return object_size; |
| 2327 | else |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
| 2331 | /** |
| 2332 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 2333 | * @b: binder_buffer containing the object |
| 2334 | * @index: index in offset array at which the binder_buffer_object is |
| 2335 | * located |
| 2336 | * @start: points to the start of the offset array |
| 2337 | * @num_valid: the number of valid offsets in the offset array |
| 2338 | * |
| 2339 | * Return: If @index is within the valid range of the offset array |
| 2340 | * described by @start and @num_valid, and if there's a valid |
| 2341 | * binder_buffer_object at the offset found in index @index |
| 2342 | * of the offset array, that object is returned. Otherwise, |
| 2343 | * %NULL is returned. |
| 2344 | * Note that the offset found in index @index itself is not |
| 2345 | * verified; this function assumes that @num_valid elements |
| 2346 | * from @start were previously verified to have valid offsets. |
| 2347 | */ |
| 2348 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 2349 | binder_size_t index, |
| 2350 | binder_size_t *start, |
| 2351 | binder_size_t num_valid) |
| 2352 | { |
| 2353 | struct binder_buffer_object *buffer_obj; |
| 2354 | binder_size_t *offp; |
| 2355 | |
| 2356 | if (index >= num_valid) |
| 2357 | return NULL; |
| 2358 | |
| 2359 | offp = start + index; |
| 2360 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 2361 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 2362 | return NULL; |
| 2363 | |
| 2364 | return buffer_obj; |
| 2365 | } |
| 2366 | |
| 2367 | /** |
| 2368 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 2369 | * @b: transaction buffer |
| 2370 | * @objects_start start of objects buffer |
| 2371 | * @buffer: binder_buffer_object in which to fix up |
| 2372 | * @offset: start offset in @buffer to fix up |
| 2373 | * @last_obj: last binder_buffer_object that we fixed up in |
| 2374 | * @last_min_offset: minimum fixup offset in @last_obj |
| 2375 | * |
| 2376 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2377 | * allowed. |
| 2378 | * |
| 2379 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2380 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2381 | * buffer object that was verified, or one of its parents. |
| 2382 | * |
| 2383 | * Example of what is allowed: |
| 2384 | * |
| 2385 | * A |
| 2386 | * B (parent = A, offset = 0) |
| 2387 | * C (parent = A, offset = 16) |
| 2388 | * D (parent = C, offset = 0) |
| 2389 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2390 | * |
| 2391 | * Examples of what is not allowed: |
| 2392 | * |
| 2393 | * Decreasing offsets within the same parent: |
| 2394 | * A |
| 2395 | * C (parent = A, offset = 16) |
| 2396 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2397 | * |
| 2398 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2399 | * A |
| 2400 | * B (parent = A, offset = 0) |
| 2401 | * C (parent = A, offset = 0) |
| 2402 | * C (parent = A, offset = 16) |
| 2403 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2404 | */ |
| 2405 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 2406 | binder_size_t *objects_start, |
| 2407 | struct binder_buffer_object *buffer, |
| 2408 | binder_size_t fixup_offset, |
| 2409 | struct binder_buffer_object *last_obj, |
| 2410 | binder_size_t last_min_offset) |
| 2411 | { |
| 2412 | if (!last_obj) { |
| 2413 | /* Nothing to fix up in */ |
| 2414 | return false; |
| 2415 | } |
| 2416 | |
| 2417 | while (last_obj != buffer) { |
| 2418 | /* |
| 2419 | * Safe to retrieve the parent of last_obj, since it |
| 2420 | * was already previously verified by the driver. |
| 2421 | */ |
| 2422 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 2423 | return false; |
| 2424 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 2425 | last_obj = (struct binder_buffer_object *) |
| 2426 | (b->data + *(objects_start + last_obj->parent)); |
| 2427 | } |
| 2428 | return (fixup_offset >= last_min_offset); |
| 2429 | } |
| 2430 | |
| 2431 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2432 | struct binder_buffer *buffer, |
| 2433 | binder_size_t *failed_at) |
| 2434 | { |
| 2435 | binder_size_t *offp, *off_start, *off_end; |
| 2436 | int debug_id = buffer->debug_id; |
| 2437 | |
| 2438 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2439 | "%d(%s) buffer release %d, size %zd-%zd, failed at %pK\n", |
| 2440 | proc->pid, proc->tsk->comm, buffer->debug_id, |
| 2441 | buffer->data_size, buffer->offsets_size, failed_at); |
| 2442 | |
| 2443 | if (buffer->target_node) |
| 2444 | binder_dec_node(buffer->target_node, 1, 0); |
| 2445 | |
| 2446 | off_start = (binder_size_t *)(buffer->data + |
| 2447 | ALIGN(buffer->data_size, sizeof(void *))); |
| 2448 | if (failed_at) |
| 2449 | off_end = failed_at; |
| 2450 | else |
| 2451 | off_end = (void *)off_start + buffer->offsets_size; |
| 2452 | for (offp = off_start; offp < off_end; offp++) { |
| 2453 | struct binder_object_header *hdr; |
| 2454 | size_t object_size = binder_validate_object(buffer, *offp); |
| 2455 | |
| 2456 | if (object_size == 0) { |
| 2457 | pr_err("transaction release %d bad object at offset %lld, size %zd\n", |
| 2458 | debug_id, (u64)*offp, buffer->data_size); |
| 2459 | continue; |
| 2460 | } |
| 2461 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 2462 | switch (hdr->type) { |
| 2463 | case BINDER_TYPE_BINDER: |
| 2464 | case BINDER_TYPE_WEAK_BINDER: { |
| 2465 | struct flat_binder_object *fp; |
| 2466 | struct binder_node *node; |
| 2467 | |
| 2468 | fp = to_flat_binder_object(hdr); |
| 2469 | node = binder_get_node(proc, fp->binder); |
| 2470 | if (node == NULL) { |
| 2471 | pr_err("transaction release %d bad node %016llx\n", |
| 2472 | debug_id, (u64)fp->binder); |
| 2473 | break; |
| 2474 | } |
| 2475 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2476 | " node %d u%016llx\n", |
| 2477 | node->debug_id, (u64)node->ptr); |
| 2478 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2479 | 0); |
| 2480 | binder_put_node(node); |
| 2481 | } break; |
| 2482 | case BINDER_TYPE_HANDLE: |
| 2483 | case BINDER_TYPE_WEAK_HANDLE: { |
| 2484 | struct flat_binder_object *fp; |
| 2485 | struct binder_ref_data rdata; |
| 2486 | int ret; |
| 2487 | |
| 2488 | fp = to_flat_binder_object(hdr); |
| 2489 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2490 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2491 | |
| 2492 | if (ret) { |
| 2493 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2494 | debug_id, fp->handle, ret); |
| 2495 | break; |
| 2496 | } |
| 2497 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2498 | " ref %d desc %d\n", |
| 2499 | rdata.debug_id, rdata.desc); |
| 2500 | } break; |
| 2501 | |
| 2502 | case BINDER_TYPE_FD: { |
| 2503 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2504 | |
| 2505 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2506 | " fd %d\n", fp->fd); |
| 2507 | if (failed_at) |
| 2508 | task_close_fd(proc, fp->fd); |
| 2509 | } break; |
| 2510 | case BINDER_TYPE_PTR: |
| 2511 | /* |
| 2512 | * Nothing to do here, this will get cleaned up when the |
| 2513 | * transaction buffer gets freed |
| 2514 | */ |
| 2515 | break; |
| 2516 | case BINDER_TYPE_FDA: { |
| 2517 | struct binder_fd_array_object *fda; |
| 2518 | struct binder_buffer_object *parent; |
| 2519 | uintptr_t parent_buffer; |
| 2520 | u32 *fd_array; |
| 2521 | size_t fd_index; |
| 2522 | binder_size_t fd_buf_size; |
| 2523 | |
| 2524 | fda = to_binder_fd_array_object(hdr); |
| 2525 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2526 | off_start, |
| 2527 | offp - off_start); |
| 2528 | if (!parent) { |
| 2529 | pr_err("transaction release %d bad parent offset", |
| 2530 | debug_id); |
| 2531 | continue; |
| 2532 | } |
| 2533 | /* |
| 2534 | * Since the parent was already fixed up, convert it |
| 2535 | * back to kernel address space to access it |
| 2536 | */ |
| 2537 | parent_buffer = parent->buffer - |
| 2538 | binder_alloc_get_user_buffer_offset( |
| 2539 | &proc->alloc); |
| 2540 | |
| 2541 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2542 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2543 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2544 | debug_id, (u64)fda->num_fds); |
| 2545 | continue; |
| 2546 | } |
| 2547 | if (fd_buf_size > parent->length || |
| 2548 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2549 | /* No space for all file descriptors here. */ |
| 2550 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2551 | debug_id, (u64)fda->num_fds); |
| 2552 | continue; |
| 2553 | } |
| 2554 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
| 2555 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
| 2556 | task_close_fd(proc, fd_array[fd_index]); |
| 2557 | } break; |
| 2558 | default: |
| 2559 | pr_err("transaction release %d bad object type %x\n", |
| 2560 | debug_id, hdr->type); |
| 2561 | break; |
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2567 | struct binder_transaction *t, |
| 2568 | struct binder_thread *thread) |
| 2569 | { |
| 2570 | struct binder_node *node; |
| 2571 | struct binder_proc *proc = thread->proc; |
| 2572 | struct binder_proc *target_proc = t->to_proc; |
| 2573 | struct binder_ref_data rdata; |
| 2574 | int ret = 0; |
| 2575 | |
| 2576 | node = binder_get_node(proc, fp->binder); |
| 2577 | if (!node) { |
| 2578 | node = binder_new_node(proc, fp); |
| 2579 | if (!node) |
| 2580 | return -ENOMEM; |
| 2581 | } |
| 2582 | if (fp->cookie != node->cookie) { |
| 2583 | binder_user_error("%d:%d(%s:%s) sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2584 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)fp->binder, |
| 2585 | node->debug_id, (u64)fp->cookie, |
| 2586 | (u64)node->cookie); |
| 2587 | ret = -EINVAL; |
| 2588 | goto done; |
| 2589 | } |
| 2590 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2591 | ret = -EPERM; |
| 2592 | goto done; |
| 2593 | } |
| 2594 | |
| 2595 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2596 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2597 | &thread->todo, &rdata); |
| 2598 | if (ret) |
| 2599 | goto done; |
| 2600 | |
| 2601 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2602 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2603 | else |
| 2604 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2605 | fp->binder = 0; |
| 2606 | fp->handle = rdata.desc; |
| 2607 | fp->cookie = 0; |
| 2608 | |
| 2609 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
| 2610 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2611 | " node %d u%016llx -> ref %d desc %d\n", |
| 2612 | node->debug_id, (u64)node->ptr, |
| 2613 | rdata.debug_id, rdata.desc); |
| 2614 | done: |
| 2615 | binder_put_node(node); |
| 2616 | return ret; |
| 2617 | } |
| 2618 | |
| 2619 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2620 | struct binder_transaction *t, |
| 2621 | struct binder_thread *thread) |
| 2622 | { |
| 2623 | struct binder_proc *proc = thread->proc; |
| 2624 | struct binder_proc *target_proc = t->to_proc; |
| 2625 | struct binder_node *node; |
| 2626 | struct binder_ref_data src_rdata; |
| 2627 | int ret = 0; |
| 2628 | |
| 2629 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2630 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2631 | if (!node) { |
| 2632 | binder_user_error("%d:%d(%s:%s) got transaction with invalid handle, %d\n", |
| 2633 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, fp->handle); |
| 2634 | return -EINVAL; |
| 2635 | } |
| 2636 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2637 | ret = -EPERM; |
| 2638 | goto done; |
| 2639 | } |
| 2640 | |
| 2641 | binder_node_lock(node); |
| 2642 | if (node->proc == target_proc) { |
| 2643 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2644 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2645 | else |
| 2646 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
| 2647 | fp->binder = node->ptr; |
| 2648 | fp->cookie = node->cookie; |
| 2649 | if (node->proc) |
| 2650 | binder_inner_proc_lock(node->proc); |
| 2651 | binder_inc_node_nilocked(node, |
| 2652 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2653 | 0, NULL); |
| 2654 | if (node->proc) |
| 2655 | binder_inner_proc_unlock(node->proc); |
| 2656 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
| 2657 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2658 | " ref %d desc %d -> node %d u%016llx\n", |
| 2659 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2660 | (u64)node->ptr); |
| 2661 | binder_node_unlock(node); |
| 2662 | } else { |
| 2663 | struct binder_ref_data dest_rdata; |
| 2664 | |
| 2665 | binder_node_unlock(node); |
| 2666 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2667 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2668 | NULL, &dest_rdata); |
| 2669 | if (ret) |
| 2670 | goto done; |
| 2671 | |
| 2672 | fp->binder = 0; |
| 2673 | fp->handle = dest_rdata.desc; |
| 2674 | fp->cookie = 0; |
| 2675 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2676 | &dest_rdata); |
| 2677 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2678 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
| 2679 | src_rdata.debug_id, src_rdata.desc, |
| 2680 | dest_rdata.debug_id, dest_rdata.desc, |
| 2681 | node->debug_id); |
| 2682 | } |
| 2683 | done: |
| 2684 | binder_put_node(node); |
| 2685 | return ret; |
| 2686 | } |
| 2687 | |
| 2688 | static int binder_translate_fd(int fd, |
| 2689 | struct binder_transaction *t, |
| 2690 | struct binder_thread *thread, |
| 2691 | struct binder_transaction *in_reply_to) |
| 2692 | { |
| 2693 | struct binder_proc *proc = thread->proc; |
| 2694 | struct binder_proc *target_proc = t->to_proc; |
| 2695 | int target_fd; |
| 2696 | struct file *file; |
| 2697 | int ret; |
| 2698 | bool target_allows_fd; |
| 2699 | |
| 2700 | if (in_reply_to) |
| 2701 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2702 | else |
| 2703 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2704 | if (!target_allows_fd) { |
| 2705 | binder_user_error("%d:%d(%s:%s) got %s with fd, %d, but target does not allow fds\n", |
| 2706 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 2707 | in_reply_to ? "reply" : "transaction", |
| 2708 | fd); |
| 2709 | ret = -EPERM; |
| 2710 | goto err_fd_not_accepted; |
| 2711 | } |
| 2712 | |
| 2713 | file = fget(fd); |
| 2714 | if (!file) { |
| 2715 | binder_user_error("%d:%d(%s:%s) got transaction with invalid fd, %d\n", |
| 2716 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, fd); |
| 2717 | ret = -EBADF; |
| 2718 | goto err_fget; |
| 2719 | } |
| 2720 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2721 | if (ret < 0) { |
| 2722 | ret = -EPERM; |
| 2723 | goto err_security; |
| 2724 | } |
| 2725 | |
| 2726 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2727 | if (target_fd < 0) { |
| 2728 | ret = -ENOMEM; |
| 2729 | goto err_get_unused_fd; |
| 2730 | } |
| 2731 | task_fd_install(target_proc, target_fd, file); |
| 2732 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2733 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2734 | fd, target_fd); |
| 2735 | |
| 2736 | return target_fd; |
| 2737 | |
| 2738 | err_get_unused_fd: |
| 2739 | err_security: |
| 2740 | fput(file); |
| 2741 | err_fget: |
| 2742 | err_fd_not_accepted: |
| 2743 | return ret; |
| 2744 | } |
| 2745 | |
| 2746 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2747 | struct binder_buffer_object *parent, |
| 2748 | struct binder_transaction *t, |
| 2749 | struct binder_thread *thread, |
| 2750 | struct binder_transaction *in_reply_to) |
| 2751 | { |
| 2752 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
| 2753 | int target_fd; |
| 2754 | uintptr_t parent_buffer; |
| 2755 | u32 *fd_array; |
| 2756 | struct binder_proc *proc = thread->proc; |
| 2757 | struct binder_proc *target_proc = t->to_proc; |
| 2758 | |
| 2759 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2760 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2761 | binder_user_error("%d:%d(%s:%s) got transaction with invalid number of fds (%lld)\n", |
| 2762 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)fda->num_fds); |
| 2763 | return -EINVAL; |
| 2764 | } |
| 2765 | if (fd_buf_size > parent->length || |
| 2766 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2767 | /* No space for all file descriptors here. */ |
| 2768 | binder_user_error("%d:%d(%s:%s) not enough space to store %lld fds in buffer\n", |
| 2769 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)fda->num_fds); |
| 2770 | return -EINVAL; |
| 2771 | } |
| 2772 | /* |
| 2773 | * Since the parent was already fixed up, convert it |
| 2774 | * back to the kernel address space to access it |
| 2775 | */ |
| 2776 | parent_buffer = parent->buffer - |
| 2777 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
| 2778 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
| 2779 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2780 | binder_user_error("%d:%d(%s:%s) parent offset not aligned correctly.\n", |
| 2781 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 2782 | return -EINVAL; |
| 2783 | } |
| 2784 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
| 2785 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, |
| 2786 | in_reply_to); |
| 2787 | if (target_fd < 0) |
| 2788 | goto err_translate_fd_failed; |
| 2789 | fd_array[fdi] = target_fd; |
| 2790 | } |
| 2791 | return 0; |
| 2792 | |
| 2793 | err_translate_fd_failed: |
| 2794 | /* |
| 2795 | * Failed to allocate fd or security error, free fds |
| 2796 | * installed so far. |
| 2797 | */ |
| 2798 | num_installed_fds = fdi; |
| 2799 | for (fdi = 0; fdi < num_installed_fds; fdi++) |
| 2800 | task_close_fd(target_proc, fd_array[fdi]); |
| 2801 | return target_fd; |
| 2802 | } |
| 2803 | |
| 2804 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2805 | struct binder_thread *thread, |
| 2806 | struct binder_buffer_object *bp, |
| 2807 | binder_size_t *off_start, |
| 2808 | binder_size_t num_valid, |
| 2809 | struct binder_buffer_object *last_fixup_obj, |
| 2810 | binder_size_t last_fixup_min_off) |
| 2811 | { |
| 2812 | struct binder_buffer_object *parent; |
| 2813 | u8 *parent_buffer; |
| 2814 | struct binder_buffer *b = t->buffer; |
| 2815 | struct binder_proc *proc = thread->proc; |
| 2816 | struct binder_proc *target_proc = t->to_proc; |
| 2817 | |
| 2818 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2819 | return 0; |
| 2820 | |
| 2821 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2822 | if (!parent) { |
| 2823 | binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n", |
| 2824 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 2825 | return -EINVAL; |
| 2826 | } |
| 2827 | |
| 2828 | if (!binder_validate_fixup(b, off_start, |
| 2829 | parent, bp->parent_offset, |
| 2830 | last_fixup_obj, |
| 2831 | last_fixup_min_off)) { |
| 2832 | binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n", |
| 2833 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 2834 | return -EINVAL; |
| 2835 | } |
| 2836 | |
| 2837 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2838 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2839 | /* No space for a pointer here! */ |
| 2840 | binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset\n", |
| 2841 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 2842 | return -EINVAL; |
| 2843 | } |
| 2844 | parent_buffer = (u8 *)((uintptr_t)parent->buffer - |
| 2845 | binder_alloc_get_user_buffer_offset( |
| 2846 | &target_proc->alloc)); |
| 2847 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2848 | |
| 2849 | return 0; |
| 2850 | } |
| 2851 | |
| 2852 | /** |
| 2853 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2854 | * @t: transaction to send |
| 2855 | * @proc: process to send the transaction to |
| 2856 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2857 | * |
| 2858 | * This function queues a transaction to the specified process. It will try |
| 2859 | * to find a thread in the target process to handle the transaction and |
| 2860 | * wake it up. If no thread is found, the work is queued to the proc |
| 2861 | * waitqueue. |
| 2862 | * |
| 2863 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2864 | * to the waitlist of that specific thread. |
| 2865 | * |
| 2866 | * Return: true if the transactions was successfully queued |
| 2867 | * false if the target process or thread is dead |
| 2868 | */ |
| 2869 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2870 | struct binder_proc *proc, |
| 2871 | struct binder_thread *thread) |
| 2872 | { |
| 2873 | struct binder_node *node = t->buffer->target_node; |
| 2874 | struct binder_priority node_prio; |
| 2875 | bool oneway = !!(t->flags & TF_ONE_WAY); |
| 2876 | bool pending_async = false; |
| 2877 | |
| 2878 | BUG_ON(!node); |
| 2879 | binder_node_lock(node); |
| 2880 | node_prio.prio = node->min_priority; |
| 2881 | node_prio.sched_policy = node->sched_policy; |
| 2882 | |
| 2883 | if (oneway) { |
| 2884 | BUG_ON(thread); |
| 2885 | if (node->has_async_transaction) { |
| 2886 | pending_async = true; |
| 2887 | } else { |
| 2888 | node->has_async_transaction = true; |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | binder_inner_proc_lock(proc); |
| 2893 | |
| 2894 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2895 | binder_inner_proc_unlock(proc); |
| 2896 | binder_node_unlock(node); |
| 2897 | return false; |
| 2898 | } |
| 2899 | |
| 2900 | if (!thread && !pending_async) |
| 2901 | thread = binder_select_thread_ilocked(proc); |
| 2902 | |
| 2903 | if (thread) { |
| 2904 | binder_transaction_priority(thread->task, t, node_prio, |
| 2905 | node->inherit_rt); |
| 2906 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2907 | } else if (!pending_async) { |
| 2908 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
| 2909 | } else { |
| 2910 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
| 2911 | } |
| 2912 | |
| 2913 | if (!pending_async) |
| 2914 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2915 | |
| 2916 | binder_inner_proc_unlock(proc); |
| 2917 | binder_node_unlock(node); |
| 2918 | |
| 2919 | return true; |
| 2920 | } |
| 2921 | |
| 2922 | /** |
| 2923 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2924 | * @node: struct binder_node for which to get refs |
| 2925 | * @proc: returns @node->proc if valid |
| 2926 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2927 | * |
| 2928 | * User-space normally keeps the node alive when creating a transaction |
| 2929 | * since it has a reference to the target. The local strong ref keeps it |
| 2930 | * alive if the sending process dies before the target process processes |
| 2931 | * the transaction. If the source process is malicious or has a reference |
| 2932 | * counting bug, relying on the local strong ref can fail. |
| 2933 | * |
| 2934 | * Since user-space can cause the local strong ref to go away, we also take |
| 2935 | * a tmpref on the node to ensure it survives while we are constructing |
| 2936 | * the transaction. We also need a tmpref on the proc while we are |
| 2937 | * constructing the transaction, so we take that here as well. |
| 2938 | * |
| 2939 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2940 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2941 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2942 | */ |
| 2943 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2944 | struct binder_node *node, |
| 2945 | struct binder_proc **procp, |
| 2946 | uint32_t *error) |
| 2947 | { |
| 2948 | struct binder_node *target_node = NULL; |
| 2949 | |
| 2950 | binder_node_inner_lock(node); |
| 2951 | if (node->proc) { |
| 2952 | target_node = node; |
| 2953 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2954 | binder_inc_node_tmpref_ilocked(node); |
| 2955 | node->proc->tmp_ref++; |
| 2956 | *procp = node->proc; |
| 2957 | } else |
| 2958 | *error = BR_DEAD_REPLY; |
| 2959 | binder_node_inner_unlock(node); |
| 2960 | |
| 2961 | return target_node; |
| 2962 | } |
| 2963 | |
| 2964 | static void binder_transaction(struct binder_proc *proc, |
| 2965 | struct binder_thread *thread, |
| 2966 | struct binder_transaction_data *tr, int reply, |
| 2967 | binder_size_t extra_buffers_size) |
| 2968 | { |
| 2969 | int ret; |
| 2970 | struct binder_transaction *t; |
| 2971 | struct binder_work *tcomplete; |
| 2972 | binder_size_t *offp, *off_end, *off_start; |
| 2973 | binder_size_t off_min; |
| 2974 | u8 *sg_bufp, *sg_buf_end; |
| 2975 | struct binder_proc *target_proc = NULL; |
| 2976 | struct binder_thread *target_thread = NULL; |
| 2977 | struct binder_node *target_node = NULL; |
| 2978 | struct binder_transaction *in_reply_to = NULL; |
| 2979 | struct binder_transaction_log_entry *e; |
| 2980 | uint32_t return_error = 0; |
| 2981 | uint32_t return_error_param = 0; |
| 2982 | uint32_t return_error_line = 0; |
| 2983 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2984 | binder_size_t last_fixup_min_off = 0; |
| 2985 | struct binder_context *context = proc->context; |
| 2986 | int t_debug_id = atomic_inc_return(&binder_last_id); |
| 2987 | char *secctx = NULL; |
| 2988 | u32 secctx_sz = 0; |
| 2989 | |
| 2990 | e = binder_transaction_log_add(&binder_transaction_log); |
| 2991 | e->debug_id = t_debug_id; |
| 2992 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2993 | e->from_proc = proc->pid; |
| 2994 | e->from_thread = thread->pid; |
| 2995 | e->target_handle = tr->target.handle; |
| 2996 | e->data_size = tr->data_size; |
| 2997 | e->offsets_size = tr->offsets_size; |
| 2998 | e->context_name = proc->context->name; |
| 2999 | |
| 3000 | if (reply) { |
| 3001 | binder_inner_proc_lock(proc); |
| 3002 | in_reply_to = thread->transaction_stack; |
| 3003 | if (in_reply_to == NULL) { |
| 3004 | binder_inner_proc_unlock(proc); |
| 3005 | binder_user_error("%d:%d(%s:%s) got reply transaction with no transaction stack\n", |
| 3006 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3007 | return_error = BR_FAILED_REPLY; |
| 3008 | return_error_param = -EPROTO; |
| 3009 | return_error_line = __LINE__; |
| 3010 | goto err_empty_call_stack; |
| 3011 | } |
| 3012 | if (in_reply_to->to_thread != thread) { |
| 3013 | spin_lock(&in_reply_to->lock); |
| 3014 | binder_user_error("%d:%d(%s:%s) got reply transaction with bad transaction stack, "\ |
| 3015 | "transaction %d has target %d:%d(%s:%s)\n", |
| 3016 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, in_reply_to->debug_id, |
| 3017 | in_reply_to->to_proc ? |
| 3018 | in_reply_to->to_proc->pid : 0, |
| 3019 | in_reply_to->to_thread ? |
| 3020 | in_reply_to->to_thread->pid : 0, |
| 3021 | in_reply_to->to_proc ? |
| 3022 | in_reply_to->to_proc->tsk->comm : "", |
| 3023 | in_reply_to->to_thread ? |
| 3024 | in_reply_to->to_thread->task->comm : ""); |
| 3025 | spin_unlock(&in_reply_to->lock); |
| 3026 | binder_inner_proc_unlock(proc); |
| 3027 | return_error = BR_FAILED_REPLY; |
| 3028 | return_error_param = -EPROTO; |
| 3029 | return_error_line = __LINE__; |
| 3030 | in_reply_to = NULL; |
| 3031 | goto err_bad_call_stack; |
| 3032 | } |
| 3033 | thread->transaction_stack = in_reply_to->to_parent; |
| 3034 | binder_inner_proc_unlock(proc); |
| 3035 | target_thread = binder_get_txn_from_and_acq_inner(in_reply_to); |
| 3036 | if (target_thread == NULL) { |
| 3037 | return_error = BR_DEAD_REPLY; |
| 3038 | return_error_line = __LINE__; |
| 3039 | goto err_dead_binder; |
| 3040 | } |
| 3041 | if (target_thread->transaction_stack != in_reply_to) { |
| 3042 | binder_user_error("%d:%d(%s:%s) got reply transaction with bad target transaction "\ |
| 3043 | "stack %d, expected %d\n", |
| 3044 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3045 | target_thread->transaction_stack ? |
| 3046 | target_thread->transaction_stack->debug_id : 0, |
| 3047 | in_reply_to->debug_id); |
| 3048 | binder_inner_proc_unlock(target_thread->proc); |
| 3049 | return_error = BR_FAILED_REPLY; |
| 3050 | return_error_param = -EPROTO; |
| 3051 | return_error_line = __LINE__; |
| 3052 | in_reply_to = NULL; |
| 3053 | target_thread = NULL; |
| 3054 | goto err_dead_binder; |
| 3055 | } |
| 3056 | target_proc = target_thread->proc; |
| 3057 | target_proc->tmp_ref++; |
| 3058 | binder_inner_proc_unlock(target_thread->proc); |
| 3059 | } else { |
| 3060 | if (tr->target.handle) { |
| 3061 | struct binder_ref *ref; |
| 3062 | |
| 3063 | /* |
| 3064 | * There must already be a strong ref |
| 3065 | * on this node. If so, do a strong |
| 3066 | * increment on the node to ensure it |
| 3067 | * stays alive until the transaction is |
| 3068 | * done. |
| 3069 | */ |
| 3070 | binder_proc_lock(proc); |
| 3071 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 3072 | true); |
| 3073 | if (ref) { |
| 3074 | target_node = binder_get_node_refs_for_txn( |
| 3075 | ref->node, &target_proc, |
| 3076 | &return_error); |
| 3077 | } else { |
| 3078 | binder_user_error("%d:%d(%s:%s) got transaction to invalid handle\n", |
| 3079 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3080 | return_error = BR_FAILED_REPLY; |
| 3081 | } |
| 3082 | binder_proc_unlock(proc); |
| 3083 | } else { |
| 3084 | mutex_lock(&context->context_mgr_node_lock); |
| 3085 | target_node = context->binder_context_mgr_node; |
| 3086 | if (target_node) |
| 3087 | target_node = binder_get_node_refs_for_txn( |
| 3088 | target_node, &target_proc, |
| 3089 | &return_error); |
| 3090 | else |
| 3091 | return_error = BR_DEAD_REPLY; |
| 3092 | mutex_unlock(&context->context_mgr_node_lock); |
| 3093 | if (target_node && target_proc == proc) { |
| 3094 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 3095 | proc->pid, thread->pid); |
| 3096 | return_error = BR_FAILED_REPLY; |
| 3097 | return_error_param = -EINVAL; |
| 3098 | return_error_line = __LINE__; |
| 3099 | goto err_invalid_target_handle; |
| 3100 | } |
| 3101 | } |
| 3102 | if (!target_node) { |
| 3103 | /* |
| 3104 | * return_error is set above |
| 3105 | */ |
| 3106 | return_error_param = -EINVAL; |
| 3107 | return_error_line = __LINE__; |
| 3108 | goto err_dead_binder; |
| 3109 | } |
| 3110 | e->to_node = target_node->debug_id; |
| 3111 | if (security_binder_transaction(proc->tsk, |
| 3112 | target_proc->tsk) < 0) { |
| 3113 | return_error = BR_FAILED_REPLY; |
| 3114 | return_error_param = -EPERM; |
| 3115 | return_error_line = __LINE__; |
| 3116 | goto err_invalid_target_handle; |
| 3117 | } |
| 3118 | binder_inner_proc_lock(proc); |
| 3119 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 3120 | struct binder_transaction *tmp; |
| 3121 | |
| 3122 | tmp = thread->transaction_stack; |
| 3123 | if (tmp->to_thread != thread) { |
| 3124 | spin_lock(&tmp->lock); |
| 3125 | binder_user_error("%d:%d(%s:%s) got new transaction with bad transaction stack, "\ |
| 3126 | "transaction %d has target %d:%d(%s:%s)\n", |
| 3127 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, tmp->debug_id, |
| 3128 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 3129 | tmp->to_thread ? |
| 3130 | tmp->to_thread->pid : 0, |
| 3131 | tmp->to_proc ? tmp->to_proc->tsk->comm : "", |
| 3132 | tmp->to_thread ? tmp->to_thread->task->comm : ""); |
| 3133 | spin_unlock(&tmp->lock); |
| 3134 | binder_inner_proc_unlock(proc); |
| 3135 | return_error = BR_FAILED_REPLY; |
| 3136 | return_error_param = -EPROTO; |
| 3137 | return_error_line = __LINE__; |
| 3138 | goto err_bad_call_stack; |
| 3139 | } |
| 3140 | while (tmp) { |
| 3141 | struct binder_thread *from; |
| 3142 | |
| 3143 | spin_lock(&tmp->lock); |
| 3144 | from = tmp->from; |
| 3145 | if (from && from->proc == target_proc) { |
| 3146 | atomic_inc(&from->tmp_ref); |
| 3147 | target_thread = from; |
| 3148 | spin_unlock(&tmp->lock); |
| 3149 | break; |
| 3150 | } |
| 3151 | spin_unlock(&tmp->lock); |
| 3152 | tmp = tmp->from_parent; |
| 3153 | } |
| 3154 | } |
| 3155 | binder_inner_proc_unlock(proc); |
| 3156 | } |
| 3157 | if (target_thread) { |
| 3158 | e->to_thread = target_thread->pid; |
| 3159 | } |
| 3160 | e->to_proc = target_proc->pid; |
| 3161 | |
| 3162 | /* TODO: reuse incoming transaction for reply */ |
| 3163 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 3164 | if (t == NULL) { |
| 3165 | return_error = BR_FAILED_REPLY; |
| 3166 | return_error_param = -ENOMEM; |
| 3167 | return_error_line = __LINE__; |
| 3168 | goto err_alloc_t_failed; |
| 3169 | } |
| 3170 | binder_stats_created(BINDER_STAT_TRANSACTION); |
| 3171 | spin_lock_init(&t->lock); |
| 3172 | |
| 3173 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 3174 | if (tcomplete == NULL) { |
| 3175 | return_error = BR_FAILED_REPLY; |
| 3176 | return_error_param = -ENOMEM; |
| 3177 | return_error_line = __LINE__; |
| 3178 | goto err_alloc_tcomplete_failed; |
| 3179 | } |
| 3180 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3181 | |
| 3182 | t->debug_id = t_debug_id; |
| 3183 | |
| 3184 | if (reply) |
| 3185 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 3186 | "%d:%d(%s:%s) BC_REPLY %d -> %d:%d (%s:%s), data %016llx-%016llx size %lld-%lld-%lld\n", |
| 3187 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, t->debug_id, |
| 3188 | target_proc->pid, target_thread->pid, target_proc->tsk->comm, target_thread->task->comm, |
| 3189 | (u64)tr->data.ptr.buffer, |
| 3190 | (u64)tr->data.ptr.offsets, |
| 3191 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3192 | (u64)extra_buffers_size); |
| 3193 | else |
| 3194 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 3195 | "%d:%d(%s:%s) BC_TRANSACTION %d -> %d (%s) - node %d, data %016llx-%016llx size %lld-%lld-%lld\n", |
| 3196 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, t->debug_id, |
| 3197 | target_proc->pid, target_proc->tsk->comm, target_node->debug_id, |
| 3198 | (u64)tr->data.ptr.buffer, |
| 3199 | (u64)tr->data.ptr.offsets, |
| 3200 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3201 | (u64)extra_buffers_size); |
| 3202 | |
| 3203 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 3204 | t->from = thread; |
| 3205 | else |
| 3206 | t->from = NULL; |
| 3207 | t->sender_euid = task_euid(proc->tsk); |
| 3208 | t->to_proc = target_proc; |
| 3209 | t->to_thread = target_thread; |
| 3210 | t->code = tr->code; |
| 3211 | t->flags = tr->flags; |
| 3212 | if (!(t->flags & TF_ONE_WAY) && |
| 3213 | binder_supported_policy(current->policy)) { |
| 3214 | /* Inherit supported policies for synchronous transactions */ |
| 3215 | t->priority.sched_policy = current->policy; |
| 3216 | t->priority.prio = current->normal_prio; |
| 3217 | } else { |
| 3218 | /* Otherwise, fall back to the default priority */ |
| 3219 | t->priority = target_proc->default_priority; |
| 3220 | } |
| 3221 | |
| 3222 | if (target_node && target_node->txn_security_ctx) { |
| 3223 | u32 secid; |
| 3224 | size_t added_size; |
| 3225 | |
| 3226 | security_task_getsecid(proc->tsk, &secid); |
| 3227 | ret = security_secid_to_secctx(secid, &secctx, &secctx_sz); |
| 3228 | if (ret) { |
| 3229 | return_error = BR_FAILED_REPLY; |
| 3230 | return_error_param = ret; |
| 3231 | return_error_line = __LINE__; |
| 3232 | goto err_get_secctx_failed; |
| 3233 | } |
| 3234 | added_size = ALIGN(secctx_sz, sizeof(u64)); |
| 3235 | extra_buffers_size += added_size; |
| 3236 | if (extra_buffers_size < added_size) { |
| 3237 | /* integer overflow of extra_buffers_size */ |
| 3238 | return_error = BR_FAILED_REPLY; |
| 3239 | return_error_param = EINVAL; |
| 3240 | return_error_line = __LINE__; |
| 3241 | goto err_bad_extra_size; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER |
| 3246 | dss_binder_transaction(reply, t, t->from ? t->from : thread, target_node ? target_node->debug_id : 0); |
| 3247 | #endif |
| 3248 | trace_binder_transaction(reply, t, target_node); |
| 3249 | |
| 3250 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
| 3251 | tr->offsets_size, extra_buffers_size, |
| 3252 | !reply && (t->flags & TF_ONE_WAY)); |
| 3253 | if (IS_ERR(t->buffer)) { |
| 3254 | /* |
| 3255 | * -ESRCH indicates VMA cleared. The target is dying. |
| 3256 | */ |
| 3257 | return_error_param = PTR_ERR(t->buffer); |
| 3258 | return_error = return_error_param == -ESRCH ? |
| 3259 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 3260 | return_error_line = __LINE__; |
| 3261 | t->buffer = NULL; |
| 3262 | goto err_binder_alloc_buf_failed; |
| 3263 | } |
| 3264 | if (secctx) { |
| 3265 | size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) + |
| 3266 | ALIGN(tr->offsets_size, sizeof(void *)) + |
| 3267 | ALIGN(extra_buffers_size, sizeof(void *)) - |
| 3268 | ALIGN(secctx_sz, sizeof(u64)); |
| 3269 | char *kptr = t->buffer->data + buf_offset; |
| 3270 | |
| 3271 | t->security_ctx = (uintptr_t)kptr + |
| 3272 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
| 3273 | memcpy(kptr, secctx, secctx_sz); |
| 3274 | security_release_secctx(secctx, secctx_sz); |
| 3275 | secctx = NULL; |
| 3276 | } |
| 3277 | t->buffer->debug_id = t->debug_id; |
| 3278 | t->buffer->transaction = t; |
| 3279 | t->buffer->target_node = target_node; |
| 3280 | trace_binder_transaction_alloc_buf(t->buffer); |
| 3281 | off_start = (binder_size_t *)(t->buffer->data + |
| 3282 | ALIGN(tr->data_size, sizeof(void *))); |
| 3283 | offp = off_start; |
| 3284 | |
| 3285 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 3286 | tr->data.ptr.buffer, tr->data_size)) { |
| 3287 | binder_user_error("%d:%d(%s:%s) got transaction with invalid data ptr\n", |
| 3288 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3289 | return_error = BR_FAILED_REPLY; |
| 3290 | return_error_param = -EFAULT; |
| 3291 | return_error_line = __LINE__; |
| 3292 | goto err_copy_data_failed; |
| 3293 | } |
| 3294 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 3295 | tr->data.ptr.offsets, tr->offsets_size)) { |
| 3296 | binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n", |
| 3297 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3298 | return_error = BR_FAILED_REPLY; |
| 3299 | return_error_param = -EFAULT; |
| 3300 | return_error_line = __LINE__; |
| 3301 | goto err_copy_data_failed; |
| 3302 | } |
| 3303 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3304 | binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets size, %lld\n", |
| 3305 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)tr->offsets_size); |
| 3306 | return_error = BR_FAILED_REPLY; |
| 3307 | return_error_param = -EINVAL; |
| 3308 | return_error_line = __LINE__; |
| 3309 | goto err_bad_offset; |
| 3310 | } |
| 3311 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3312 | binder_user_error("%d:%d(%s:%s) got transaction with unaligned buffers size, %lld\n", |
| 3313 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3314 | (u64)extra_buffers_size); |
| 3315 | return_error = BR_FAILED_REPLY; |
| 3316 | return_error_param = -EINVAL; |
| 3317 | return_error_line = __LINE__; |
| 3318 | goto err_bad_offset; |
| 3319 | } |
| 3320 | off_end = (void *)off_start + tr->offsets_size; |
| 3321 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 3322 | sg_buf_end = sg_bufp + extra_buffers_size - |
| 3323 | ALIGN(secctx_sz, sizeof(u64)); |
| 3324 | off_min = 0; |
| 3325 | for (; offp < off_end; offp++) { |
| 3326 | struct binder_object_header *hdr; |
| 3327 | size_t object_size = binder_validate_object(t->buffer, *offp); |
| 3328 | |
| 3329 | if (object_size == 0 || *offp < off_min) { |
| 3330 | binder_user_error("%d:%d(%s:%s) got transaction with invalid offset (%lld, min %lld max %lld)"\ |
| 3331 | " or object.\n", |
| 3332 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3333 | (u64)*offp, |
| 3334 | (u64)off_min, |
| 3335 | (u64)t->buffer->data_size); |
| 3336 | return_error = BR_FAILED_REPLY; |
| 3337 | return_error_param = -EINVAL; |
| 3338 | return_error_line = __LINE__; |
| 3339 | goto err_bad_offset; |
| 3340 | } |
| 3341 | |
| 3342 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 3343 | off_min = *offp + object_size; |
| 3344 | switch (hdr->type) { |
| 3345 | case BINDER_TYPE_BINDER: |
| 3346 | case BINDER_TYPE_WEAK_BINDER: { |
| 3347 | struct flat_binder_object *fp; |
| 3348 | |
| 3349 | fp = to_flat_binder_object(hdr); |
| 3350 | ret = binder_translate_binder(fp, t, thread); |
| 3351 | if (ret < 0) { |
| 3352 | return_error = BR_FAILED_REPLY; |
| 3353 | return_error_param = ret; |
| 3354 | return_error_line = __LINE__; |
| 3355 | goto err_translate_failed; |
| 3356 | } |
| 3357 | } break; |
| 3358 | case BINDER_TYPE_HANDLE: |
| 3359 | case BINDER_TYPE_WEAK_HANDLE: { |
| 3360 | struct flat_binder_object *fp; |
| 3361 | |
| 3362 | fp = to_flat_binder_object(hdr); |
| 3363 | ret = binder_translate_handle(fp, t, thread); |
| 3364 | if (ret < 0) { |
| 3365 | return_error = BR_FAILED_REPLY; |
| 3366 | return_error_param = ret; |
| 3367 | return_error_line = __LINE__; |
| 3368 | goto err_translate_failed; |
| 3369 | } |
| 3370 | } break; |
| 3371 | |
| 3372 | case BINDER_TYPE_FD: { |
| 3373 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 3374 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3375 | in_reply_to); |
| 3376 | |
| 3377 | if (target_fd < 0) { |
| 3378 | return_error = BR_FAILED_REPLY; |
| 3379 | return_error_param = target_fd; |
| 3380 | return_error_line = __LINE__; |
| 3381 | goto err_translate_failed; |
| 3382 | } |
| 3383 | fp->pad_binder = 0; |
| 3384 | fp->fd = target_fd; |
| 3385 | } break; |
| 3386 | case BINDER_TYPE_FDA: { |
| 3387 | struct binder_fd_array_object *fda = |
| 3388 | to_binder_fd_array_object(hdr); |
| 3389 | struct binder_buffer_object *parent = |
| 3390 | binder_validate_ptr(t->buffer, fda->parent, |
| 3391 | off_start, |
| 3392 | offp - off_start); |
| 3393 | if (!parent) { |
| 3394 | binder_user_error("%d:%d(%s:%s) got transaction with invalid parent offset or type\n", |
| 3395 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3396 | return_error = BR_FAILED_REPLY; |
| 3397 | return_error_param = -EINVAL; |
| 3398 | return_error_line = __LINE__; |
| 3399 | goto err_bad_parent; |
| 3400 | } |
| 3401 | if (!binder_validate_fixup(t->buffer, off_start, |
| 3402 | parent, fda->parent_offset, |
| 3403 | last_fixup_obj, |
| 3404 | last_fixup_min_off)) { |
| 3405 | binder_user_error("%d:%d(%s:%s) got transaction with out-of-order buffer fixup\n", |
| 3406 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3407 | return_error = BR_FAILED_REPLY; |
| 3408 | return_error_param = -EINVAL; |
| 3409 | return_error_line = __LINE__; |
| 3410 | goto err_bad_parent; |
| 3411 | } |
| 3412 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3413 | in_reply_to); |
| 3414 | if (ret < 0) { |
| 3415 | return_error = BR_FAILED_REPLY; |
| 3416 | return_error_param = ret; |
| 3417 | return_error_line = __LINE__; |
| 3418 | goto err_translate_failed; |
| 3419 | } |
| 3420 | last_fixup_obj = parent; |
| 3421 | last_fixup_min_off = |
| 3422 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3423 | } break; |
| 3424 | case BINDER_TYPE_PTR: { |
| 3425 | struct binder_buffer_object *bp = |
| 3426 | to_binder_buffer_object(hdr); |
| 3427 | size_t buf_left = sg_buf_end - sg_bufp; |
| 3428 | |
| 3429 | if (bp->length > buf_left) { |
| 3430 | binder_user_error("%d:%d(%s:%s) got transaction with too large buffer\n", |
| 3431 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3432 | return_error = BR_FAILED_REPLY; |
| 3433 | return_error_param = -EINVAL; |
| 3434 | return_error_line = __LINE__; |
| 3435 | goto err_bad_offset; |
| 3436 | } |
| 3437 | if (copy_from_user(sg_bufp, |
| 3438 | (const void __user *)(uintptr_t) |
| 3439 | bp->buffer, bp->length)) { |
| 3440 | binder_user_error("%d:%d(%s:%s) got transaction with invalid offsets ptr\n", |
| 3441 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3442 | return_error_param = -EFAULT; |
| 3443 | return_error = BR_FAILED_REPLY; |
| 3444 | return_error_line = __LINE__; |
| 3445 | goto err_copy_data_failed; |
| 3446 | } |
| 3447 | /* Fixup buffer pointer to target proc address space */ |
| 3448 | bp->buffer = (uintptr_t)sg_bufp + |
| 3449 | binder_alloc_get_user_buffer_offset( |
| 3450 | &target_proc->alloc); |
| 3451 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 3452 | |
| 3453 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 3454 | offp - off_start, |
| 3455 | last_fixup_obj, |
| 3456 | last_fixup_min_off); |
| 3457 | if (ret < 0) { |
| 3458 | return_error = BR_FAILED_REPLY; |
| 3459 | return_error_param = ret; |
| 3460 | return_error_line = __LINE__; |
| 3461 | goto err_translate_failed; |
| 3462 | } |
| 3463 | last_fixup_obj = bp; |
| 3464 | last_fixup_min_off = 0; |
| 3465 | } break; |
| 3466 | default: |
| 3467 | binder_user_error("%d:%d(%s:%s) got transaction with invalid object type, %x\n", |
| 3468 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, hdr->type); |
| 3469 | return_error = BR_FAILED_REPLY; |
| 3470 | return_error_param = -EINVAL; |
| 3471 | return_error_line = __LINE__; |
| 3472 | goto err_bad_object_type; |
| 3473 | } |
| 3474 | } |
| 3475 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
| 3476 | t->work.type = BINDER_WORK_TRANSACTION; |
| 3477 | |
| 3478 | if (reply) { |
| 3479 | binder_enqueue_thread_work(thread, tcomplete); |
| 3480 | binder_inner_proc_lock(target_proc); |
| 3481 | if (target_thread->is_dead) { |
| 3482 | binder_inner_proc_unlock(target_proc); |
| 3483 | goto err_dead_proc_or_thread; |
| 3484 | } |
| 3485 | BUG_ON(t->buffer->async_transaction != 0); |
| 3486 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
| 3487 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
| 3488 | binder_inner_proc_unlock(target_proc); |
| 3489 | wake_up_interruptible_sync(&target_thread->wait); |
| 3490 | binder_restore_priority(current, in_reply_to->saved_priority); |
| 3491 | binder_free_transaction(in_reply_to); |
| 3492 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3493 | BUG_ON(t->buffer->async_transaction != 0); |
| 3494 | binder_inner_proc_lock(proc); |
| 3495 | /* |
| 3496 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3497 | * userspace immediately; this allows the target process to |
| 3498 | * immediately start processing this transaction, reducing |
| 3499 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3500 | * the target replies (or there is an error). |
| 3501 | */ |
| 3502 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
| 3503 | t->need_reply = 1; |
| 3504 | t->from_parent = thread->transaction_stack; |
| 3505 | thread->transaction_stack = t; |
| 3506 | binder_inner_proc_unlock(proc); |
| 3507 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
| 3508 | binder_inner_proc_lock(proc); |
| 3509 | binder_pop_transaction_ilocked(thread, t); |
| 3510 | binder_inner_proc_unlock(proc); |
| 3511 | goto err_dead_proc_or_thread; |
| 3512 | } |
| 3513 | } else { |
| 3514 | BUG_ON(target_node == NULL); |
| 3515 | BUG_ON(t->buffer->async_transaction != 1); |
| 3516 | binder_enqueue_thread_work(thread, tcomplete); |
| 3517 | if (!binder_proc_transaction(t, target_proc, NULL)) |
| 3518 | goto err_dead_proc_or_thread; |
| 3519 | } |
| 3520 | if (target_thread) |
| 3521 | binder_thread_dec_tmpref(target_thread); |
| 3522 | binder_proc_dec_tmpref(target_proc); |
| 3523 | if (target_node) |
| 3524 | binder_dec_node_tmpref(target_node); |
| 3525 | /* |
| 3526 | * write barrier to synchronize with initialization |
| 3527 | * of log entry |
| 3528 | */ |
| 3529 | smp_wmb(); |
| 3530 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3531 | return; |
| 3532 | |
| 3533 | err_dead_proc_or_thread: |
| 3534 | return_error = BR_DEAD_REPLY; |
| 3535 | return_error_line = __LINE__; |
| 3536 | binder_dequeue_work(proc, tcomplete); |
| 3537 | err_translate_failed: |
| 3538 | err_bad_object_type: |
| 3539 | err_bad_offset: |
| 3540 | err_bad_parent: |
| 3541 | err_copy_data_failed: |
| 3542 | trace_binder_transaction_failed_buffer_release(t->buffer); |
| 3543 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
| 3544 | if (target_node) |
| 3545 | binder_dec_node_tmpref(target_node); |
| 3546 | target_node = NULL; |
| 3547 | t->buffer->transaction = NULL; |
| 3548 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
| 3549 | err_binder_alloc_buf_failed: |
| 3550 | err_bad_extra_size: |
| 3551 | if (secctx) |
| 3552 | security_release_secctx(secctx, secctx_sz); |
| 3553 | err_get_secctx_failed: |
| 3554 | kfree(tcomplete); |
| 3555 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3556 | err_alloc_tcomplete_failed: |
| 3557 | kfree(t); |
| 3558 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3559 | err_alloc_t_failed: |
| 3560 | err_bad_call_stack: |
| 3561 | err_empty_call_stack: |
| 3562 | err_dead_binder: |
| 3563 | err_invalid_target_handle: |
| 3564 | if (target_thread) |
| 3565 | binder_thread_dec_tmpref(target_thread); |
| 3566 | if (target_proc) |
| 3567 | binder_proc_dec_tmpref(target_proc); |
| 3568 | if (target_node) { |
| 3569 | binder_dec_node(target_node, 1, 0); |
| 3570 | binder_dec_node_tmpref(target_node); |
| 3571 | } |
| 3572 | |
| 3573 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 3574 | "%d:%d(%s:%s) transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3575 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3576 | return_error, return_error_param, |
| 3577 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3578 | return_error_line); |
| 3579 | |
| 3580 | { |
| 3581 | struct binder_transaction_log_entry *fe; |
| 3582 | |
| 3583 | e->return_error = return_error; |
| 3584 | e->return_error_param = return_error_param; |
| 3585 | e->return_error_line = return_error_line; |
| 3586 | #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER |
| 3587 | dss_binder_transaction_failed(reply, e, proc->tsk->comm, thread->task->comm, tr->flags, tr->code); |
| 3588 | #endif |
| 3589 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3590 | *fe = *e; |
| 3591 | /* |
| 3592 | * write barrier to synchronize with initialization |
| 3593 | * of log entry |
| 3594 | */ |
| 3595 | smp_wmb(); |
| 3596 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3597 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
| 3598 | } |
| 3599 | |
| 3600 | BUG_ON(thread->return_error.cmd != BR_OK); |
| 3601 | if (in_reply_to) { |
| 3602 | binder_restore_priority(current, in_reply_to->saved_priority); |
| 3603 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
| 3604 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
| 3605 | binder_send_failed_reply(in_reply_to, return_error); |
| 3606 | } else { |
| 3607 | thread->return_error.cmd = return_error; |
| 3608 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | static int binder_thread_write(struct binder_proc *proc, |
| 3613 | struct binder_thread *thread, |
| 3614 | binder_uintptr_t binder_buffer, size_t size, |
| 3615 | binder_size_t *consumed) |
| 3616 | { |
| 3617 | uint32_t cmd; |
| 3618 | struct binder_context *context = proc->context; |
| 3619 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
| 3620 | void __user *ptr = buffer + *consumed; |
| 3621 | void __user *end = buffer + size; |
| 3622 | |
| 3623 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
| 3624 | int ret; |
| 3625 | |
| 3626 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3627 | return -EFAULT; |
| 3628 | ptr += sizeof(uint32_t); |
| 3629 | trace_binder_command(cmd); |
| 3630 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
| 3631 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3632 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3633 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
| 3634 | } |
| 3635 | switch (cmd) { |
| 3636 | case BC_INCREFS: |
| 3637 | case BC_ACQUIRE: |
| 3638 | case BC_RELEASE: |
| 3639 | case BC_DECREFS: { |
| 3640 | uint32_t target; |
| 3641 | const char *debug_string; |
| 3642 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3643 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3644 | struct binder_ref_data rdata; |
| 3645 | |
| 3646 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3647 | return -EFAULT; |
| 3648 | |
| 3649 | ptr += sizeof(uint32_t); |
| 3650 | ret = -1; |
| 3651 | if (increment && !target) { |
| 3652 | struct binder_node *ctx_mgr_node; |
| 3653 | mutex_lock(&context->context_mgr_node_lock); |
| 3654 | ctx_mgr_node = context->binder_context_mgr_node; |
| 3655 | if (ctx_mgr_node) |
| 3656 | ret = binder_inc_ref_for_node( |
| 3657 | proc, ctx_mgr_node, |
| 3658 | strong, NULL, &rdata); |
| 3659 | mutex_unlock(&context->context_mgr_node_lock); |
| 3660 | } |
| 3661 | if (ret) |
| 3662 | ret = binder_update_ref_for_handle( |
| 3663 | proc, target, increment, strong, |
| 3664 | &rdata); |
| 3665 | if (!ret && rdata.desc != target) { |
| 3666 | binder_user_error("%d:%d(%s:%s) tried to acquire reference to"\ |
| 3667 | " desc %d, got %d instead\n", |
| 3668 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3669 | target, rdata.desc); |
| 3670 | } |
| 3671 | switch (cmd) { |
| 3672 | case BC_INCREFS: |
| 3673 | debug_string = "IncRefs"; |
| 3674 | break; |
| 3675 | case BC_ACQUIRE: |
| 3676 | debug_string = "Acquire"; |
| 3677 | break; |
| 3678 | case BC_RELEASE: |
| 3679 | debug_string = "Release"; |
| 3680 | break; |
| 3681 | case BC_DECREFS: |
| 3682 | default: |
| 3683 | debug_string = "DecRefs"; |
| 3684 | break; |
| 3685 | } |
| 3686 | if (ret) { |
| 3687 | binder_user_error("%d:%d(%s:%s) %s %d refcount change on invalid ref %d ret %d\n", |
| 3688 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3689 | debug_string, strong, target, ret); |
| 3690 | break; |
| 3691 | } |
| 3692 | binder_debug(BINDER_DEBUG_USER_REFS, |
| 3693 | "%d:%d(%s:%s) %s ref %d desc %d s %d w %d\n", |
| 3694 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3695 | debug_string, rdata.debug_id, rdata.desc, rdata.strong, |
| 3696 | rdata.weak); |
| 3697 | break; |
| 3698 | } |
| 3699 | case BC_INCREFS_DONE: |
| 3700 | case BC_ACQUIRE_DONE: { |
| 3701 | binder_uintptr_t node_ptr; |
| 3702 | binder_uintptr_t cookie; |
| 3703 | struct binder_node *node; |
| 3704 | bool free_node; |
| 3705 | |
| 3706 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3707 | return -EFAULT; |
| 3708 | ptr += sizeof(binder_uintptr_t); |
| 3709 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
| 3710 | return -EFAULT; |
| 3711 | ptr += sizeof(binder_uintptr_t); |
| 3712 | node = binder_get_node(proc, node_ptr); |
| 3713 | if (node == NULL) { |
| 3714 | binder_user_error("%d:%d(%s:%s) %s u%016llx no match\n", |
| 3715 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3716 | cmd == BC_INCREFS_DONE ? |
| 3717 | "BC_INCREFS_DONE" : |
| 3718 | "BC_ACQUIRE_DONE", |
| 3719 | (u64)node_ptr); |
| 3720 | break; |
| 3721 | } |
| 3722 | if (cookie != node->cookie) { |
| 3723 | binder_user_error("%d:%d(%s:%s) %s u%016llx node %d cookie mismatch"\ |
| 3724 | " %016llx != %016llx\n", |
| 3725 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3726 | cmd == BC_INCREFS_DONE ? |
| 3727 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
| 3728 | (u64)node_ptr, node->debug_id, |
| 3729 | (u64)cookie, (u64)node->cookie); |
| 3730 | binder_put_node(node); |
| 3731 | break; |
| 3732 | } |
| 3733 | binder_node_inner_lock(node); |
| 3734 | if (cmd == BC_ACQUIRE_DONE) { |
| 3735 | if (node->pending_strong_ref == 0) { |
| 3736 | binder_user_error("%d:%d(%s:%s) BC_ACQUIRE_DONE node %d"\ |
| 3737 | " has no pending acquire request\n", |
| 3738 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3739 | node->debug_id); |
| 3740 | binder_node_inner_unlock(node); |
| 3741 | binder_put_node(node); |
| 3742 | break; |
| 3743 | } |
| 3744 | node->pending_strong_ref = 0; |
| 3745 | } else { |
| 3746 | if (node->pending_weak_ref == 0) { |
| 3747 | binder_user_error("%d:%d(%s:%s) BC_INCREFS_DONE node %d"\ |
| 3748 | " has no pending increfs request\n", |
| 3749 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3750 | node->debug_id); |
| 3751 | binder_node_inner_unlock(node); |
| 3752 | binder_put_node(node); |
| 3753 | break; |
| 3754 | } |
| 3755 | node->pending_weak_ref = 0; |
| 3756 | } |
| 3757 | free_node = binder_dec_node_nilocked(node, |
| 3758 | cmd == BC_ACQUIRE_DONE, 0); |
| 3759 | WARN_ON(free_node); |
| 3760 | binder_debug(BINDER_DEBUG_USER_REFS, |
| 3761 | "%d:%d(%s:%s) %s node %d ls %d lw %d tr %d\n", |
| 3762 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3763 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
| 3764 | node->debug_id, node->local_strong_refs, |
| 3765 | node->local_weak_refs, node->tmp_refs); |
| 3766 | binder_node_inner_unlock(node); |
| 3767 | binder_put_node(node); |
| 3768 | break; |
| 3769 | } |
| 3770 | case BC_ATTEMPT_ACQUIRE: |
| 3771 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
| 3772 | return -EINVAL; |
| 3773 | case BC_ACQUIRE_RESULT: |
| 3774 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
| 3775 | return -EINVAL; |
| 3776 | |
| 3777 | case BC_FREE_BUFFER: { |
| 3778 | binder_uintptr_t data_ptr; |
| 3779 | struct binder_buffer *buffer; |
| 3780 | |
| 3781 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
| 3782 | return -EFAULT; |
| 3783 | ptr += sizeof(binder_uintptr_t); |
| 3784 | |
| 3785 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3786 | data_ptr); |
| 3787 | if (IS_ERR_OR_NULL(buffer)) { |
| 3788 | if (PTR_ERR(buffer) == -EPERM) { |
| 3789 | binder_user_error( |
| 3790 | "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n", |
| 3791 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3792 | (u64)data_ptr); |
| 3793 | } else { |
| 3794 | binder_user_error( |
| 3795 | "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx no match\n", |
| 3796 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3797 | (u64)data_ptr); |
| 3798 | } |
| 3799 | break; |
| 3800 | } |
| 3801 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
| 3802 | "%d:%d(%s:%s) BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3803 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)data_ptr, |
| 3804 | buffer->debug_id, |
| 3805 | buffer->transaction ? "active" : "finished"); |
| 3806 | |
| 3807 | binder_inner_proc_lock(proc); |
| 3808 | if (buffer->transaction) { |
| 3809 | buffer->transaction->buffer = NULL; |
| 3810 | buffer->transaction = NULL; |
| 3811 | } |
| 3812 | binder_inner_proc_unlock(proc); |
| 3813 | if (buffer->async_transaction && buffer->target_node) { |
| 3814 | struct binder_node *buf_node; |
| 3815 | struct binder_work *w; |
| 3816 | |
| 3817 | buf_node = buffer->target_node; |
| 3818 | binder_node_inner_lock(buf_node); |
| 3819 | BUG_ON(!buf_node->has_async_transaction); |
| 3820 | BUG_ON(buf_node->proc != proc); |
| 3821 | w = binder_dequeue_work_head_ilocked( |
| 3822 | &buf_node->async_todo); |
| 3823 | if (!w) { |
| 3824 | buf_node->has_async_transaction = false; |
| 3825 | } else { |
| 3826 | binder_enqueue_work_ilocked( |
| 3827 | w, &proc->todo); |
| 3828 | binder_wakeup_proc_ilocked(proc); |
| 3829 | } |
| 3830 | binder_node_inner_unlock(buf_node); |
| 3831 | } |
| 3832 | trace_binder_transaction_buffer_release(buffer); |
| 3833 | binder_transaction_buffer_release(proc, buffer, NULL); |
| 3834 | binder_alloc_free_buf(&proc->alloc, buffer); |
| 3835 | break; |
| 3836 | } |
| 3837 | |
| 3838 | case BC_TRANSACTION_SG: |
| 3839 | case BC_REPLY_SG: { |
| 3840 | struct binder_transaction_data_sg tr; |
| 3841 | |
| 3842 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3843 | return -EFAULT; |
| 3844 | ptr += sizeof(tr); |
| 3845 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3846 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3847 | break; |
| 3848 | } |
| 3849 | case BC_TRANSACTION: |
| 3850 | case BC_REPLY: { |
| 3851 | struct binder_transaction_data tr; |
| 3852 | |
| 3853 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3854 | return -EFAULT; |
| 3855 | ptr += sizeof(tr); |
| 3856 | binder_transaction(proc, thread, &tr, |
| 3857 | cmd == BC_REPLY, 0); |
| 3858 | break; |
| 3859 | } |
| 3860 | |
| 3861 | case BC_REGISTER_LOOPER: |
| 3862 | binder_debug(BINDER_DEBUG_THREADS, |
| 3863 | "%d:%d(%s:%s) BC_REGISTER_LOOPER\n", |
| 3864 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3865 | binder_inner_proc_lock(proc); |
| 3866 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3867 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
| 3868 | binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER"\ |
| 3869 | " called after BC_ENTER_LOOPER\n", |
| 3870 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3871 | } else if (proc->requested_threads == 0) { |
| 3872 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
| 3873 | binder_user_error("%d:%d(%s:%s) ERROR: BC_REGISTER_LOOPER called without request\n", |
| 3874 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3875 | } else { |
| 3876 | proc->requested_threads--; |
| 3877 | proc->requested_threads_started++; |
| 3878 | } |
| 3879 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
| 3880 | binder_inner_proc_unlock(proc); |
| 3881 | break; |
| 3882 | case BC_ENTER_LOOPER: |
| 3883 | binder_debug(BINDER_DEBUG_THREADS, |
| 3884 | "%d:%d(%s:%s) BC_ENTER_LOOPER\n", |
| 3885 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3886 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3887 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
| 3888 | binder_user_error("%d:%d(%s:%s) ERROR: BC_ENTER_LOOPER"\ |
| 3889 | " called after BC_REGISTER_LOOPER\n", |
| 3890 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3891 | } |
| 3892 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3893 | break; |
| 3894 | case BC_EXIT_LOOPER: |
| 3895 | binder_debug(BINDER_DEBUG_THREADS, |
| 3896 | "%d:%d(%s:%s) BC_EXIT_LOOPER\n", |
| 3897 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3898 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3899 | break; |
| 3900 | |
| 3901 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3902 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3903 | uint32_t target; |
| 3904 | binder_uintptr_t cookie; |
| 3905 | struct binder_ref *ref; |
| 3906 | struct binder_ref_death *death = NULL; |
| 3907 | |
| 3908 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3909 | return -EFAULT; |
| 3910 | ptr += sizeof(uint32_t); |
| 3911 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
| 3912 | return -EFAULT; |
| 3913 | ptr += sizeof(binder_uintptr_t); |
| 3914 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3915 | /* |
| 3916 | * Allocate memory for death notification |
| 3917 | * before taking lock |
| 3918 | */ |
| 3919 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3920 | if (death == NULL) { |
| 3921 | WARN_ON(thread->return_error.cmd != |
| 3922 | BR_OK); |
| 3923 | thread->return_error.cmd = BR_ERROR; |
| 3924 | binder_enqueue_thread_work( |
| 3925 | thread, |
| 3926 | &thread->return_error.work); |
| 3927 | binder_debug( |
| 3928 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3929 | "%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3930 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3931 | break; |
| 3932 | } |
| 3933 | } |
| 3934 | binder_proc_lock(proc); |
| 3935 | ref = binder_get_ref_olocked(proc, target, false); |
| 3936 | if (ref == NULL) { |
| 3937 | binder_user_error("%d:%d(%s:%s) %s invalid ref %d\n", |
| 3938 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3939 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3940 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3941 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3942 | target); |
| 3943 | binder_proc_unlock(proc); |
| 3944 | kfree(death); |
| 3945 | break; |
| 3946 | } |
| 3947 | |
| 3948 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
| 3949 | "%d:%d(%s:%s) %s %016llx ref %d desc %d s %d w %d for node %d\n", |
| 3950 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3951 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3952 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3953 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3954 | (u64)cookie, ref->data.debug_id, |
| 3955 | ref->data.desc, ref->data.strong, |
| 3956 | ref->data.weak, ref->node->debug_id); |
| 3957 | |
| 3958 | binder_node_lock(ref->node); |
| 3959 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3960 | if (ref->death) { |
| 3961 | binder_user_error("%d:%d(%s:%s) BC_REQUEST_DEATH_NOTIFICATION"\ |
| 3962 | " death notification already set\n", |
| 3963 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3964 | binder_node_unlock(ref->node); |
| 3965 | binder_proc_unlock(proc); |
| 3966 | kfree(death); |
| 3967 | break; |
| 3968 | } |
| 3969 | binder_stats_created(BINDER_STAT_DEATH); |
| 3970 | INIT_LIST_HEAD(&death->work.entry); |
| 3971 | death->cookie = cookie; |
| 3972 | ref->death = death; |
| 3973 | if (ref->node->proc == NULL) { |
| 3974 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 3975 | |
| 3976 | binder_inner_proc_lock(proc); |
| 3977 | binder_enqueue_work_ilocked( |
| 3978 | &ref->death->work, &proc->todo); |
| 3979 | binder_wakeup_proc_ilocked(proc); |
| 3980 | binder_inner_proc_unlock(proc); |
| 3981 | } |
| 3982 | } else { |
| 3983 | if (ref->death == NULL) { |
| 3984 | binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\ |
| 3985 | " death notification not active\n", |
| 3986 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 3987 | binder_node_unlock(ref->node); |
| 3988 | binder_proc_unlock(proc); |
| 3989 | break; |
| 3990 | } |
| 3991 | death = ref->death; |
| 3992 | if (death->cookie != cookie) { |
| 3993 | binder_user_error("%d:%d(%s:%s) BC_CLEAR_DEATH_NOTIFICATION"\ |
| 3994 | " death notification cookie mismatch %016llx != %016llx\n", |
| 3995 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 3996 | (u64)death->cookie, |
| 3997 | (u64)cookie); |
| 3998 | binder_node_unlock(ref->node); |
| 3999 | binder_proc_unlock(proc); |
| 4000 | break; |
| 4001 | } |
| 4002 | ref->death = NULL; |
| 4003 | binder_inner_proc_lock(proc); |
| 4004 | if (list_empty(&death->work.entry)) { |
| 4005 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
| 4006 | if (thread->looper & |
| 4007 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4008 | BINDER_LOOPER_STATE_ENTERED)) |
| 4009 | binder_enqueue_thread_work_ilocked( |
| 4010 | thread, |
| 4011 | &death->work); |
| 4012 | else { |
| 4013 | binder_enqueue_work_ilocked( |
| 4014 | &death->work, |
| 4015 | &proc->todo); |
| 4016 | binder_wakeup_proc_ilocked( |
| 4017 | proc); |
| 4018 | } |
| 4019 | } else { |
| 4020 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 4021 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 4022 | } |
| 4023 | binder_inner_proc_unlock(proc); |
| 4024 | } |
| 4025 | binder_node_unlock(ref->node); |
| 4026 | binder_proc_unlock(proc); |
| 4027 | } break; |
| 4028 | case BC_DEAD_BINDER_DONE: { |
| 4029 | struct binder_work *w; |
| 4030 | binder_uintptr_t cookie; |
| 4031 | struct binder_ref_death *death = NULL; |
| 4032 | |
| 4033 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
| 4034 | return -EFAULT; |
| 4035 | |
| 4036 | ptr += sizeof(cookie); |
| 4037 | binder_inner_proc_lock(proc); |
| 4038 | list_for_each_entry(w, &proc->delivered_death, |
| 4039 | entry) { |
| 4040 | struct binder_ref_death *tmp_death = |
| 4041 | container_of(w, |
| 4042 | struct binder_ref_death, |
| 4043 | work); |
| 4044 | |
| 4045 | if (tmp_death->cookie == cookie) { |
| 4046 | death = tmp_death; |
| 4047 | break; |
| 4048 | } |
| 4049 | } |
| 4050 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 4051 | "%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx found %pK\n", |
| 4052 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)cookie, |
| 4053 | death); |
| 4054 | if (death == NULL) { |
| 4055 | binder_user_error("%d:%d(%s:%s) BC_DEAD_BINDER_DONE %016llx not found\n", |
| 4056 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, (u64)cookie); |
| 4057 | binder_inner_proc_unlock(proc); |
| 4058 | break; |
| 4059 | } |
| 4060 | binder_dequeue_work_ilocked(&death->work); |
| 4061 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 4062 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
| 4063 | if (thread->looper & |
| 4064 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4065 | BINDER_LOOPER_STATE_ENTERED)) |
| 4066 | binder_enqueue_thread_work_ilocked( |
| 4067 | thread, &death->work); |
| 4068 | else { |
| 4069 | binder_enqueue_work_ilocked( |
| 4070 | &death->work, |
| 4071 | &proc->todo); |
| 4072 | binder_wakeup_proc_ilocked(proc); |
| 4073 | } |
| 4074 | } |
| 4075 | binder_inner_proc_unlock(proc); |
| 4076 | } break; |
| 4077 | |
| 4078 | default: |
| 4079 | pr_err("%d:%d(%s:%s) unknown command %d\n", |
| 4080 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, cmd); |
| 4081 | return -EINVAL; |
| 4082 | } |
| 4083 | *consumed = ptr - buffer; |
| 4084 | } |
| 4085 | return 0; |
| 4086 | } |
| 4087 | |
| 4088 | static void binder_stat_br(struct binder_proc *proc, |
| 4089 | struct binder_thread *thread, uint32_t cmd) |
| 4090 | { |
| 4091 | trace_binder_return(cmd); |
| 4092 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
| 4093 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 4094 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 4095 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
| 4096 | } |
| 4097 | } |
| 4098 | |
| 4099 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 4100 | struct binder_thread *thread, |
| 4101 | void __user **ptrp, |
| 4102 | binder_uintptr_t node_ptr, |
| 4103 | binder_uintptr_t node_cookie, |
| 4104 | int node_debug_id, |
| 4105 | uint32_t cmd, const char *cmd_name) |
| 4106 | { |
| 4107 | void __user *ptr = *ptrp; |
| 4108 | |
| 4109 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4110 | return -EFAULT; |
| 4111 | ptr += sizeof(uint32_t); |
| 4112 | |
| 4113 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 4114 | return -EFAULT; |
| 4115 | ptr += sizeof(binder_uintptr_t); |
| 4116 | |
| 4117 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 4118 | return -EFAULT; |
| 4119 | ptr += sizeof(binder_uintptr_t); |
| 4120 | |
| 4121 | binder_stat_br(proc, thread, cmd); |
| 4122 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d(%s:%s) %s %d u%016llx c%016llx\n", |
| 4123 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4124 | cmd_name, node_debug_id, |
| 4125 | (u64)node_ptr, (u64)node_cookie); |
| 4126 | |
| 4127 | *ptrp = ptr; |
| 4128 | return 0; |
| 4129 | } |
| 4130 | |
| 4131 | static int binder_wait_for_work(struct binder_thread *thread, |
| 4132 | bool do_proc_work) |
| 4133 | { |
| 4134 | DEFINE_WAIT(wait); |
| 4135 | struct binder_proc *proc = thread->proc; |
| 4136 | int ret = 0; |
| 4137 | |
| 4138 | freezer_do_not_count(); |
| 4139 | binder_inner_proc_lock(proc); |
| 4140 | for (;;) { |
| 4141 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 4142 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 4143 | break; |
| 4144 | if (do_proc_work) |
| 4145 | list_add(&thread->waiting_thread_node, |
| 4146 | &proc->waiting_threads); |
| 4147 | binder_inner_proc_unlock(proc); |
| 4148 | schedule(); |
| 4149 | binder_inner_proc_lock(proc); |
| 4150 | list_del_init(&thread->waiting_thread_node); |
| 4151 | if (signal_pending(current)) { |
| 4152 | ret = -ERESTARTSYS; |
| 4153 | break; |
| 4154 | } |
| 4155 | } |
| 4156 | finish_wait(&thread->wait, &wait); |
| 4157 | binder_inner_proc_unlock(proc); |
| 4158 | freezer_count(); |
| 4159 | |
| 4160 | return ret; |
| 4161 | } |
| 4162 | |
| 4163 | static int binder_thread_read(struct binder_proc *proc, |
| 4164 | struct binder_thread *thread, |
| 4165 | binder_uintptr_t binder_buffer, size_t size, |
| 4166 | binder_size_t *consumed, int non_block) |
| 4167 | { |
| 4168 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
| 4169 | void __user *ptr = buffer + *consumed; |
| 4170 | void __user *end = buffer + size; |
| 4171 | |
| 4172 | int ret = 0; |
| 4173 | int wait_for_proc_work; |
| 4174 | |
| 4175 | if (*consumed == 0) { |
| 4176 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 4177 | return -EFAULT; |
| 4178 | ptr += sizeof(uint32_t); |
| 4179 | } |
| 4180 | |
| 4181 | retry: |
| 4182 | binder_inner_proc_lock(proc); |
| 4183 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4184 | binder_inner_proc_unlock(proc); |
| 4185 | |
| 4186 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
| 4187 | |
| 4188 | trace_binder_wait_for_work(wait_for_proc_work, |
| 4189 | !!thread->transaction_stack, |
| 4190 | !binder_worklist_empty(proc, &thread->todo)); |
| 4191 | if (wait_for_proc_work) { |
| 4192 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4193 | BINDER_LOOPER_STATE_ENTERED))) { |
| 4194 | binder_user_error("%d:%d(%s:%s) ERROR: Thread waiting for process work"\ |
| 4195 | " before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n", |
| 4196 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, thread->looper); |
| 4197 | wait_event_interruptible(binder_user_error_wait, |
| 4198 | binder_stop_on_user_error < 2); |
| 4199 | } |
| 4200 | binder_restore_priority(current, proc->default_priority); |
| 4201 | } |
| 4202 | |
| 4203 | if (non_block) { |
| 4204 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 4205 | ret = -EAGAIN; |
| 4206 | } else { |
| 4207 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 4208 | } |
| 4209 | |
| 4210 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 4211 | |
| 4212 | if (ret) |
| 4213 | return ret; |
| 4214 | |
| 4215 | while (1) { |
| 4216 | uint32_t cmd; |
| 4217 | struct binder_transaction_data_secctx tr; |
| 4218 | struct binder_transaction_data *trd = &tr.transaction_data; |
| 4219 | struct binder_work *w = NULL; |
| 4220 | struct list_head *list = NULL; |
| 4221 | struct binder_transaction *t = NULL; |
| 4222 | struct binder_thread *t_from; |
| 4223 | size_t trsize = sizeof(*trd); |
| 4224 | |
| 4225 | binder_inner_proc_lock(proc); |
| 4226 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 4227 | list = &thread->todo; |
| 4228 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 4229 | wait_for_proc_work) |
| 4230 | list = &proc->todo; |
| 4231 | else { |
| 4232 | binder_inner_proc_unlock(proc); |
| 4233 | |
| 4234 | /* no data added */ |
| 4235 | if (ptr - buffer == 4 && !thread->looper_need_return) |
| 4236 | goto retry; |
| 4237 | break; |
| 4238 | } |
| 4239 | |
| 4240 | if (end - ptr < sizeof(tr) + 4) { |
| 4241 | binder_inner_proc_unlock(proc); |
| 4242 | break; |
| 4243 | } |
| 4244 | w = binder_dequeue_work_head_ilocked(list); |
| 4245 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 4246 | thread->process_todo = false; |
| 4247 | |
| 4248 | switch (w->type) { |
| 4249 | case BINDER_WORK_TRANSACTION: { |
| 4250 | binder_inner_proc_unlock(proc); |
| 4251 | t = container_of(w, struct binder_transaction, work); |
| 4252 | } break; |
| 4253 | case BINDER_WORK_RETURN_ERROR: { |
| 4254 | struct binder_error *e = container_of( |
| 4255 | w, struct binder_error, work); |
| 4256 | |
| 4257 | WARN_ON(e->cmd == BR_OK); |
| 4258 | binder_inner_proc_unlock(proc); |
| 4259 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 4260 | return -EFAULT; |
| 4261 | cmd = e->cmd; |
| 4262 | e->cmd = BR_OK; |
| 4263 | ptr += sizeof(uint32_t); |
| 4264 | |
| 4265 | binder_stat_br(proc, thread, e->cmd); |
| 4266 | } break; |
| 4267 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
| 4268 | binder_inner_proc_unlock(proc); |
| 4269 | cmd = BR_TRANSACTION_COMPLETE; |
| 4270 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4271 | return -EFAULT; |
| 4272 | ptr += sizeof(uint32_t); |
| 4273 | |
| 4274 | binder_stat_br(proc, thread, cmd); |
| 4275 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
| 4276 | "%d:%d(%s:%s) BR_TRANSACTION_COMPLETE\n", |
| 4277 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 4278 | kfree(w); |
| 4279 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4280 | } break; |
| 4281 | case BINDER_WORK_NODE: { |
| 4282 | struct binder_node *node = container_of(w, struct binder_node, work); |
| 4283 | int strong, weak; |
| 4284 | binder_uintptr_t node_ptr = node->ptr; |
| 4285 | binder_uintptr_t node_cookie = node->cookie; |
| 4286 | int node_debug_id = node->debug_id; |
| 4287 | int has_weak_ref; |
| 4288 | int has_strong_ref; |
| 4289 | void __user *orig_ptr = ptr; |
| 4290 | |
| 4291 | BUG_ON(proc != node->proc); |
| 4292 | strong = node->internal_strong_refs || |
| 4293 | node->local_strong_refs; |
| 4294 | weak = !hlist_empty(&node->refs) || |
| 4295 | node->local_weak_refs || |
| 4296 | node->tmp_refs || strong; |
| 4297 | has_strong_ref = node->has_strong_ref; |
| 4298 | has_weak_ref = node->has_weak_ref; |
| 4299 | |
| 4300 | if (weak && !has_weak_ref) { |
| 4301 | node->has_weak_ref = 1; |
| 4302 | node->pending_weak_ref = 1; |
| 4303 | node->local_weak_refs++; |
| 4304 | } |
| 4305 | if (strong && !has_strong_ref) { |
| 4306 | node->has_strong_ref = 1; |
| 4307 | node->pending_strong_ref = 1; |
| 4308 | node->local_strong_refs++; |
| 4309 | } |
| 4310 | if (!strong && has_strong_ref) |
| 4311 | node->has_strong_ref = 0; |
| 4312 | if (!weak && has_weak_ref) |
| 4313 | node->has_weak_ref = 0; |
| 4314 | if (!weak && !strong) { |
| 4315 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4316 | "%d:%d(%s:%s) node %d u%016llx c%016llx deleted\n", |
| 4317 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4318 | node_debug_id, |
| 4319 | (u64)node_ptr, |
| 4320 | (u64)node_cookie); |
| 4321 | rb_erase(&node->rb_node, &proc->nodes); |
| 4322 | binder_inner_proc_unlock(proc); |
| 4323 | binder_node_lock(node); |
| 4324 | /* |
| 4325 | * Acquire the node lock before freeing the |
| 4326 | * node to serialize with other threads that |
| 4327 | * may have been holding the node lock while |
| 4328 | * decrementing this node (avoids race where |
| 4329 | * this thread frees while the other thread |
| 4330 | * is unlocking the node after the final |
| 4331 | * decrement) |
| 4332 | */ |
| 4333 | binder_node_unlock(node); |
| 4334 | binder_free_node(node); |
| 4335 | } else |
| 4336 | binder_inner_proc_unlock(proc); |
| 4337 | |
| 4338 | if (weak && !has_weak_ref) |
| 4339 | ret = binder_put_node_cmd( |
| 4340 | proc, thread, &ptr, node_ptr, |
| 4341 | node_cookie, node_debug_id, |
| 4342 | BR_INCREFS, "BR_INCREFS"); |
| 4343 | if (!ret && strong && !has_strong_ref) |
| 4344 | ret = binder_put_node_cmd( |
| 4345 | proc, thread, &ptr, node_ptr, |
| 4346 | node_cookie, node_debug_id, |
| 4347 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4348 | if (!ret && !strong && has_strong_ref) |
| 4349 | ret = binder_put_node_cmd( |
| 4350 | proc, thread, &ptr, node_ptr, |
| 4351 | node_cookie, node_debug_id, |
| 4352 | BR_RELEASE, "BR_RELEASE"); |
| 4353 | if (!ret && !weak && has_weak_ref) |
| 4354 | ret = binder_put_node_cmd( |
| 4355 | proc, thread, &ptr, node_ptr, |
| 4356 | node_cookie, node_debug_id, |
| 4357 | BR_DECREFS, "BR_DECREFS"); |
| 4358 | if (orig_ptr == ptr) |
| 4359 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4360 | "%d:%d(%s:%s) node %d u%016llx c%016llx state unchanged\n", |
| 4361 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4362 | node_debug_id, |
| 4363 | (u64)node_ptr, |
| 4364 | (u64)node_cookie); |
| 4365 | if (ret) |
| 4366 | return ret; |
| 4367 | } break; |
| 4368 | case BINDER_WORK_DEAD_BINDER: |
| 4369 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4370 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4371 | struct binder_ref_death *death; |
| 4372 | uint32_t cmd; |
| 4373 | binder_uintptr_t cookie; |
| 4374 | |
| 4375 | death = container_of(w, struct binder_ref_death, work); |
| 4376 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4377 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4378 | else |
| 4379 | cmd = BR_DEAD_BINDER; |
| 4380 | cookie = death->cookie; |
| 4381 | |
| 4382 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
| 4383 | "%d:%d(%s:%s) %s %016llx\n", |
| 4384 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4385 | cmd == BR_DEAD_BINDER ? |
| 4386 | "BR_DEAD_BINDER" : |
| 4387 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 4388 | (u64)cookie); |
| 4389 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
| 4390 | binder_inner_proc_unlock(proc); |
| 4391 | kfree(death); |
| 4392 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4393 | } else { |
| 4394 | binder_enqueue_work_ilocked( |
| 4395 | w, &proc->delivered_death); |
| 4396 | binder_inner_proc_unlock(proc); |
| 4397 | } |
| 4398 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4399 | return -EFAULT; |
| 4400 | ptr += sizeof(uint32_t); |
| 4401 | if (put_user(cookie, |
| 4402 | (binder_uintptr_t __user *)ptr)) |
| 4403 | return -EFAULT; |
| 4404 | ptr += sizeof(binder_uintptr_t); |
| 4405 | binder_stat_br(proc, thread, cmd); |
| 4406 | if (cmd == BR_DEAD_BINDER) |
| 4407 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4408 | } break; |
| 4409 | } |
| 4410 | |
| 4411 | if (!t) |
| 4412 | continue; |
| 4413 | |
| 4414 | BUG_ON(t->buffer == NULL); |
| 4415 | if (t->buffer->target_node) { |
| 4416 | struct binder_node *target_node = t->buffer->target_node; |
| 4417 | struct binder_priority node_prio; |
| 4418 | |
| 4419 | trd->target.ptr = target_node->ptr; |
| 4420 | trd->cookie = target_node->cookie; |
| 4421 | node_prio.sched_policy = target_node->sched_policy; |
| 4422 | node_prio.prio = target_node->min_priority; |
| 4423 | binder_transaction_priority(current, t, node_prio, |
| 4424 | target_node->inherit_rt); |
| 4425 | cmd = BR_TRANSACTION; |
| 4426 | } else { |
| 4427 | trd->target.ptr = 0; |
| 4428 | trd->cookie = 0; |
| 4429 | cmd = BR_REPLY; |
| 4430 | } |
| 4431 | trd->code = t->code; |
| 4432 | trd->flags = t->flags; |
| 4433 | trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
| 4434 | |
| 4435 | t_from = binder_get_txn_from(t); |
| 4436 | if (t_from) { |
| 4437 | struct task_struct *sender = t_from->proc->tsk; |
| 4438 | |
| 4439 | trd->sender_pid = |
| 4440 | task_tgid_nr_ns(sender, |
| 4441 | task_active_pid_ns(current)); |
| 4442 | } else { |
| 4443 | trd->sender_pid = 0; |
| 4444 | } |
| 4445 | |
| 4446 | trd->data_size = t->buffer->data_size; |
| 4447 | trd->offsets_size = t->buffer->offsets_size; |
| 4448 | trd->data.ptr.buffer = (binder_uintptr_t) |
| 4449 | ((uintptr_t)t->buffer->data + |
| 4450 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
| 4451 | trd->data.ptr.offsets = trd->data.ptr.buffer + |
| 4452 | ALIGN(t->buffer->data_size, |
| 4453 | sizeof(void *)); |
| 4454 | |
| 4455 | tr.secctx = t->security_ctx; |
| 4456 | if (t->security_ctx) { |
| 4457 | cmd = BR_TRANSACTION_SEC_CTX; |
| 4458 | trsize = sizeof(tr); |
| 4459 | } |
| 4460 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4461 | if (t_from) |
| 4462 | binder_thread_dec_tmpref(t_from); |
| 4463 | |
| 4464 | binder_cleanup_transaction(t, "put_user failed", |
| 4465 | BR_FAILED_REPLY); |
| 4466 | |
| 4467 | return -EFAULT; |
| 4468 | } |
| 4469 | ptr += sizeof(uint32_t); |
| 4470 | if (copy_to_user(ptr, &tr, trsize)) { |
| 4471 | if (t_from) |
| 4472 | binder_thread_dec_tmpref(t_from); |
| 4473 | |
| 4474 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4475 | BR_FAILED_REPLY); |
| 4476 | |
| 4477 | return -EFAULT; |
| 4478 | } |
| 4479 | ptr += trsize; |
| 4480 | |
| 4481 | #ifdef CONFIG_DEBUG_SNAPSHOT_BINDER |
| 4482 | dss_binder_transaction_received(t, thread); |
| 4483 | #endif |
| 4484 | trace_binder_transaction_received(t); |
| 4485 | binder_stat_br(proc, thread, cmd); |
| 4486 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 4487 | "%d:%d(%s:%s) %s %d %d:%d(%s:%s), cmd %d size %zd-%zd ptr %016llx-%016llx\n", |
| 4488 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4489 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 4490 | (cmd == BR_TRANSACTION_SEC_CTX) ? |
| 4491 | "BR_TRANSACTION_SEC_CTX" : "BR_REPLY", |
| 4492 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4493 | t_from ? t_from->pid : 0, |
| 4494 | t_from ? t_from->proc->tsk->comm : "", |
| 4495 | t_from ? t_from->task->comm : "", |
| 4496 | cmd, |
| 4497 | t->buffer->data_size, t->buffer->offsets_size, |
| 4498 | (u64)trd->data.ptr.buffer, |
| 4499 | (u64)trd->data.ptr.offsets); |
| 4500 | |
| 4501 | if (t_from) |
| 4502 | binder_thread_dec_tmpref(t_from); |
| 4503 | t->buffer->allow_user_free = 1; |
| 4504 | if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) { |
| 4505 | binder_inner_proc_lock(thread->proc); |
| 4506 | t->to_parent = thread->transaction_stack; |
| 4507 | t->to_thread = thread; |
| 4508 | thread->transaction_stack = t; |
| 4509 | binder_inner_proc_unlock(thread->proc); |
| 4510 | } else { |
| 4511 | binder_free_transaction(t); |
| 4512 | } |
| 4513 | break; |
| 4514 | } |
| 4515 | |
| 4516 | done: |
| 4517 | |
| 4518 | *consumed = ptr - buffer; |
| 4519 | binder_inner_proc_lock(proc); |
| 4520 | if (proc->requested_threads == 0 && |
| 4521 | list_empty(&thread->proc->waiting_threads) && |
| 4522 | proc->requested_threads_started < proc->max_threads && |
| 4523 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4524 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4525 | /*spawn a new thread if we leave this out */) { |
| 4526 | proc->requested_threads++; |
| 4527 | binder_inner_proc_unlock(proc); |
| 4528 | binder_debug(BINDER_DEBUG_THREADS, |
| 4529 | "%d:%d(%s:%s) BR_SPAWN_LOOPER\n", |
| 4530 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 4531 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4532 | return -EFAULT; |
| 4533 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
| 4534 | } else |
| 4535 | binder_inner_proc_unlock(proc); |
| 4536 | return 0; |
| 4537 | } |
| 4538 | |
| 4539 | static void binder_release_work(struct binder_proc *proc, |
| 4540 | struct list_head *list) |
| 4541 | { |
| 4542 | struct binder_work *w; |
| 4543 | enum binder_work_type wtype; |
| 4544 | |
| 4545 | while (1) { |
| 4546 | binder_inner_proc_lock(proc); |
| 4547 | w = binder_dequeue_work_head_ilocked(list); |
| 4548 | wtype = w ? w->type : 0; |
| 4549 | binder_inner_proc_unlock(proc); |
| 4550 | if (!w) |
| 4551 | return; |
| 4552 | |
| 4553 | switch (wtype) { |
| 4554 | case BINDER_WORK_TRANSACTION: { |
| 4555 | struct binder_transaction *t; |
| 4556 | |
| 4557 | t = container_of(w, struct binder_transaction, work); |
| 4558 | |
| 4559 | binder_cleanup_transaction(t, "process died.", |
| 4560 | BR_DEAD_REPLY); |
| 4561 | } break; |
| 4562 | case BINDER_WORK_RETURN_ERROR: { |
| 4563 | struct binder_error *e = container_of( |
| 4564 | w, struct binder_error, work); |
| 4565 | |
| 4566 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4567 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4568 | e->cmd); |
| 4569 | } break; |
| 4570 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
| 4571 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4572 | "undelivered TRANSACTION_COMPLETE\n"); |
| 4573 | kfree(w); |
| 4574 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4575 | } break; |
| 4576 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4577 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4578 | struct binder_ref_death *death; |
| 4579 | |
| 4580 | death = container_of(w, struct binder_ref_death, work); |
| 4581 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4582 | "undelivered death notification, %016llx\n", |
| 4583 | (u64)death->cookie); |
| 4584 | kfree(death); |
| 4585 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4586 | } break; |
| 4587 | case BINDER_WORK_NODE: |
| 4588 | break; |
| 4589 | default: |
| 4590 | pr_err("unexpected work type, %d, not freed\n", |
| 4591 | wtype); |
| 4592 | break; |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | } |
| 4597 | |
| 4598 | static struct binder_thread *binder_get_thread_ilocked( |
| 4599 | struct binder_proc *proc, struct binder_thread *new_thread) |
| 4600 | { |
| 4601 | struct binder_thread *thread = NULL; |
| 4602 | struct rb_node *parent = NULL; |
| 4603 | struct rb_node **p = &proc->threads.rb_node; |
| 4604 | |
| 4605 | while (*p) { |
| 4606 | parent = *p; |
| 4607 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4608 | |
| 4609 | if (current->pid < thread->pid) |
| 4610 | p = &(*p)->rb_left; |
| 4611 | else if (current->pid > thread->pid) |
| 4612 | p = &(*p)->rb_right; |
| 4613 | else |
| 4614 | return thread; |
| 4615 | } |
| 4616 | if (!new_thread) |
| 4617 | return NULL; |
| 4618 | thread = new_thread; |
| 4619 | binder_stats_created(BINDER_STAT_THREAD); |
| 4620 | thread->proc = proc; |
| 4621 | thread->pid = current->pid; |
| 4622 | get_task_struct(current); |
| 4623 | thread->task = current; |
| 4624 | atomic_set(&thread->tmp_ref, 0); |
| 4625 | init_waitqueue_head(&thread->wait); |
| 4626 | INIT_LIST_HEAD(&thread->todo); |
| 4627 | rb_link_node(&thread->rb_node, parent, p); |
| 4628 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4629 | thread->looper_need_return = true; |
| 4630 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4631 | thread->return_error.cmd = BR_OK; |
| 4632 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4633 | thread->reply_error.cmd = BR_OK; |
| 4634 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
| 4635 | return thread; |
| 4636 | } |
| 4637 | |
| 4638 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4639 | { |
| 4640 | struct binder_thread *thread; |
| 4641 | struct binder_thread *new_thread; |
| 4642 | |
| 4643 | binder_inner_proc_lock(proc); |
| 4644 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4645 | binder_inner_proc_unlock(proc); |
| 4646 | if (!thread) { |
| 4647 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4648 | if (new_thread == NULL) |
| 4649 | return NULL; |
| 4650 | binder_inner_proc_lock(proc); |
| 4651 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4652 | binder_inner_proc_unlock(proc); |
| 4653 | if (thread != new_thread) |
| 4654 | kfree(new_thread); |
| 4655 | } |
| 4656 | return thread; |
| 4657 | } |
| 4658 | |
| 4659 | static void binder_free_proc(struct binder_proc *proc) |
| 4660 | { |
| 4661 | BUG_ON(!list_empty(&proc->todo)); |
| 4662 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4663 | binder_alloc_deferred_release(&proc->alloc); |
| 4664 | put_task_struct(proc->tsk); |
| 4665 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4666 | kfree(proc); |
| 4667 | } |
| 4668 | |
| 4669 | static void binder_free_thread(struct binder_thread *thread) |
| 4670 | { |
| 4671 | BUG_ON(!list_empty(&thread->todo)); |
| 4672 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4673 | binder_proc_dec_tmpref(thread->proc); |
| 4674 | put_task_struct(thread->task); |
| 4675 | kfree(thread); |
| 4676 | } |
| 4677 | |
| 4678 | static int binder_thread_release(struct binder_proc *proc, |
| 4679 | struct binder_thread *thread) |
| 4680 | { |
| 4681 | struct binder_transaction *t; |
| 4682 | struct binder_transaction *send_reply = NULL; |
| 4683 | int active_transactions = 0; |
| 4684 | struct binder_transaction *last_t = NULL; |
| 4685 | |
| 4686 | binder_inner_proc_lock(thread->proc); |
| 4687 | /* |
| 4688 | * take a ref on the proc so it survives |
| 4689 | * after we remove this thread from proc->threads. |
| 4690 | * The corresponding dec is when we actually |
| 4691 | * free the thread in binder_free_thread() |
| 4692 | */ |
| 4693 | proc->tmp_ref++; |
| 4694 | /* |
| 4695 | * take a ref on this thread to ensure it |
| 4696 | * survives while we are releasing it |
| 4697 | */ |
| 4698 | atomic_inc(&thread->tmp_ref); |
| 4699 | rb_erase(&thread->rb_node, &proc->threads); |
| 4700 | t = thread->transaction_stack; |
| 4701 | if (t) { |
| 4702 | spin_lock(&t->lock); |
| 4703 | if (t->to_thread == thread) |
| 4704 | send_reply = t; |
| 4705 | } |
| 4706 | thread->is_dead = true; |
| 4707 | |
| 4708 | while (t) { |
| 4709 | last_t = t; |
| 4710 | active_transactions++; |
| 4711 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4712 | "release %d:%d(%s:%s) transaction %d %s, still active\n", |
| 4713 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4714 | t->debug_id, |
| 4715 | (t->to_thread == thread) ? "in" : "out"); |
| 4716 | |
| 4717 | if (t->to_thread == thread) { |
| 4718 | t->to_proc = NULL; |
| 4719 | t->to_thread = NULL; |
| 4720 | if (t->buffer) { |
| 4721 | t->buffer->transaction = NULL; |
| 4722 | t->buffer = NULL; |
| 4723 | } |
| 4724 | t = t->to_parent; |
| 4725 | } else if (t->from == thread) { |
| 4726 | t->from = NULL; |
| 4727 | t = t->from_parent; |
| 4728 | } else |
| 4729 | BUG(); |
| 4730 | spin_unlock(&last_t->lock); |
| 4731 | if (t) |
| 4732 | spin_lock(&t->lock); |
| 4733 | } |
| 4734 | |
| 4735 | /* |
| 4736 | * If this thread used poll, make sure we remove the waitqueue |
| 4737 | * from any epoll data structures holding it with POLLFREE. |
| 4738 | * waitqueue_active() is safe to use here because we're holding |
| 4739 | * the inner lock. |
| 4740 | */ |
| 4741 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && |
| 4742 | waitqueue_active(&thread->wait)) { |
| 4743 | wake_up_poll(&thread->wait, POLLHUP | POLLFREE); |
| 4744 | } |
| 4745 | |
| 4746 | binder_inner_proc_unlock(thread->proc); |
| 4747 | |
| 4748 | /* |
| 4749 | * This is needed to avoid races between wake_up_poll() above and |
| 4750 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file |
| 4751 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read |
| 4752 | * lock, so we can be sure it's done after calling synchronize_rcu(). |
| 4753 | */ |
| 4754 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4755 | synchronize_rcu(); |
| 4756 | |
| 4757 | if (send_reply) |
| 4758 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
| 4759 | binder_release_work(proc, &thread->todo); |
| 4760 | binder_thread_dec_tmpref(thread); |
| 4761 | return active_transactions; |
| 4762 | } |
| 4763 | |
| 4764 | static unsigned int binder_poll(struct file *filp, |
| 4765 | struct poll_table_struct *wait) |
| 4766 | { |
| 4767 | struct binder_proc *proc = filp->private_data; |
| 4768 | struct binder_thread *thread = NULL; |
| 4769 | bool wait_for_proc_work; |
| 4770 | |
| 4771 | thread = binder_get_thread(proc); |
| 4772 | if (!thread) |
| 4773 | return POLLERR; |
| 4774 | |
| 4775 | binder_inner_proc_lock(thread->proc); |
| 4776 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4777 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4778 | |
| 4779 | binder_inner_proc_unlock(thread->proc); |
| 4780 | |
| 4781 | poll_wait(filp, &thread->wait, wait); |
| 4782 | |
| 4783 | if (binder_has_work(thread, wait_for_proc_work)) |
| 4784 | return POLLIN; |
| 4785 | |
| 4786 | return 0; |
| 4787 | } |
| 4788 | |
| 4789 | static int binder_ioctl_write_read(struct file *filp, |
| 4790 | unsigned int cmd, unsigned long arg, |
| 4791 | struct binder_thread *thread) |
| 4792 | { |
| 4793 | int ret = 0; |
| 4794 | struct binder_proc *proc = filp->private_data; |
| 4795 | unsigned int size = _IOC_SIZE(cmd); |
| 4796 | void __user *ubuf = (void __user *)arg; |
| 4797 | struct binder_write_read bwr; |
| 4798 | |
| 4799 | if (size != sizeof(struct binder_write_read)) { |
| 4800 | ret = -EINVAL; |
| 4801 | goto out; |
| 4802 | } |
| 4803 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4804 | ret = -EFAULT; |
| 4805 | goto out; |
| 4806 | } |
| 4807 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4808 | "%d:%d(%s:%s) write %lld at %016llx, read %lld at %016llx\n", |
| 4809 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4810 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4811 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4812 | |
| 4813 | if (bwr.write_size > 0) { |
| 4814 | ret = binder_thread_write(proc, thread, |
| 4815 | bwr.write_buffer, |
| 4816 | bwr.write_size, |
| 4817 | &bwr.write_consumed); |
| 4818 | trace_binder_write_done(ret); |
| 4819 | if (ret < 0) { |
| 4820 | bwr.read_consumed = 0; |
| 4821 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4822 | ret = -EFAULT; |
| 4823 | goto out; |
| 4824 | } |
| 4825 | } |
| 4826 | if (bwr.read_size > 0) { |
| 4827 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4828 | bwr.read_size, |
| 4829 | &bwr.read_consumed, |
| 4830 | filp->f_flags & O_NONBLOCK); |
| 4831 | trace_binder_read_done(ret); |
| 4832 | binder_inner_proc_lock(proc); |
| 4833 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
| 4834 | binder_wakeup_proc_ilocked(proc); |
| 4835 | binder_inner_proc_unlock(proc); |
| 4836 | if (ret < 0) { |
| 4837 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4838 | ret = -EFAULT; |
| 4839 | goto out; |
| 4840 | } |
| 4841 | } |
| 4842 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4843 | "%d:%d(%s:%s) wrote %lld of %lld, read return %lld of %lld\n", |
| 4844 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm, |
| 4845 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4846 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4847 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4848 | ret = -EFAULT; |
| 4849 | goto out; |
| 4850 | } |
| 4851 | out: |
| 4852 | return ret; |
| 4853 | } |
| 4854 | |
| 4855 | static int binder_ioctl_set_ctx_mgr(struct file *filp, |
| 4856 | struct flat_binder_object *fbo) |
| 4857 | { |
| 4858 | int ret = 0; |
| 4859 | struct binder_proc *proc = filp->private_data; |
| 4860 | struct binder_context *context = proc->context; |
| 4861 | struct binder_node *new_node; |
| 4862 | kuid_t curr_euid = current_euid(); |
| 4863 | |
| 4864 | mutex_lock(&context->context_mgr_node_lock); |
| 4865 | if (context->binder_context_mgr_node) { |
| 4866 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4867 | ret = -EBUSY; |
| 4868 | goto out; |
| 4869 | } |
| 4870 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4871 | if (ret < 0) |
| 4872 | goto out; |
| 4873 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4874 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
| 4875 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4876 | from_kuid(&init_user_ns, curr_euid), |
| 4877 | from_kuid(&init_user_ns, |
| 4878 | context->binder_context_mgr_uid)); |
| 4879 | ret = -EPERM; |
| 4880 | goto out; |
| 4881 | } |
| 4882 | } else { |
| 4883 | context->binder_context_mgr_uid = curr_euid; |
| 4884 | } |
| 4885 | new_node = binder_new_node(proc, fbo); |
| 4886 | if (!new_node) { |
| 4887 | ret = -ENOMEM; |
| 4888 | goto out; |
| 4889 | } |
| 4890 | binder_node_lock(new_node); |
| 4891 | new_node->local_weak_refs++; |
| 4892 | new_node->local_strong_refs++; |
| 4893 | new_node->has_strong_ref = 1; |
| 4894 | new_node->has_weak_ref = 1; |
| 4895 | context->binder_context_mgr_node = new_node; |
| 4896 | binder_node_unlock(new_node); |
| 4897 | binder_put_node(new_node); |
| 4898 | out: |
| 4899 | mutex_unlock(&context->context_mgr_node_lock); |
| 4900 | return ret; |
| 4901 | } |
| 4902 | |
| 4903 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4904 | struct binder_node_debug_info *info) |
| 4905 | { |
| 4906 | struct rb_node *n; |
| 4907 | binder_uintptr_t ptr = info->ptr; |
| 4908 | |
| 4909 | memset(info, 0, sizeof(*info)); |
| 4910 | |
| 4911 | binder_inner_proc_lock(proc); |
| 4912 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4913 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4914 | rb_node); |
| 4915 | if (node->ptr > ptr) { |
| 4916 | info->ptr = node->ptr; |
| 4917 | info->cookie = node->cookie; |
| 4918 | info->has_strong_ref = node->has_strong_ref; |
| 4919 | info->has_weak_ref = node->has_weak_ref; |
| 4920 | break; |
| 4921 | } |
| 4922 | } |
| 4923 | binder_inner_proc_unlock(proc); |
| 4924 | |
| 4925 | return 0; |
| 4926 | } |
| 4927 | |
| 4928 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4929 | { |
| 4930 | int ret; |
| 4931 | struct binder_proc *proc = filp->private_data; |
| 4932 | struct binder_thread *thread; |
| 4933 | unsigned int size = _IOC_SIZE(cmd); |
| 4934 | void __user *ubuf = (void __user *)arg; |
| 4935 | |
| 4936 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4937 | proc->pid, current->pid, cmd, arg);*/ |
| 4938 | |
| 4939 | binder_selftest_alloc(&proc->alloc); |
| 4940 | |
| 4941 | trace_binder_ioctl(cmd, arg); |
| 4942 | |
| 4943 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4944 | if (ret) |
| 4945 | goto err_unlocked; |
| 4946 | |
| 4947 | thread = binder_get_thread(proc); |
| 4948 | if (thread == NULL) { |
| 4949 | ret = -ENOMEM; |
| 4950 | goto err; |
| 4951 | } |
| 4952 | |
| 4953 | switch (cmd) { |
| 4954 | case BINDER_WRITE_READ: |
| 4955 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4956 | if (ret) |
| 4957 | goto err; |
| 4958 | break; |
| 4959 | case BINDER_SET_MAX_THREADS: { |
| 4960 | int max_threads; |
| 4961 | |
| 4962 | if (copy_from_user(&max_threads, ubuf, |
| 4963 | sizeof(max_threads))) { |
| 4964 | ret = -EINVAL; |
| 4965 | goto err; |
| 4966 | } |
| 4967 | binder_inner_proc_lock(proc); |
| 4968 | proc->max_threads = max_threads; |
| 4969 | binder_inner_proc_unlock(proc); |
| 4970 | break; |
| 4971 | } |
| 4972 | case BINDER_SET_CONTEXT_MGR_EXT: { |
| 4973 | struct flat_binder_object fbo; |
| 4974 | |
| 4975 | if (copy_from_user(&fbo, ubuf, sizeof(fbo))) { |
| 4976 | ret = -EINVAL; |
| 4977 | goto err; |
| 4978 | } |
| 4979 | ret = binder_ioctl_set_ctx_mgr(filp, &fbo); |
| 4980 | if (ret) |
| 4981 | goto err; |
| 4982 | break; |
| 4983 | } |
| 4984 | case BINDER_SET_CONTEXT_MGR: |
| 4985 | ret = binder_ioctl_set_ctx_mgr(filp, NULL); |
| 4986 | if (ret) |
| 4987 | goto err; |
| 4988 | break; |
| 4989 | case BINDER_THREAD_EXIT: |
| 4990 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d(%s:%s) exit\n", |
| 4991 | proc->pid, thread->pid, proc->tsk->comm, thread->task->comm); |
| 4992 | binder_thread_release(proc, thread); |
| 4993 | thread = NULL; |
| 4994 | break; |
| 4995 | case BINDER_VERSION: { |
| 4996 | struct binder_version __user *ver = ubuf; |
| 4997 | |
| 4998 | if (size != sizeof(struct binder_version)) { |
| 4999 | ret = -EINVAL; |
| 5000 | goto err; |
| 5001 | } |
| 5002 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 5003 | &ver->protocol_version)) { |
| 5004 | ret = -EINVAL; |
| 5005 | goto err; |
| 5006 | } |
| 5007 | break; |
| 5008 | } |
| 5009 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 5010 | struct binder_node_debug_info info; |
| 5011 | |
| 5012 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5013 | ret = -EFAULT; |
| 5014 | goto err; |
| 5015 | } |
| 5016 | |
| 5017 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 5018 | if (ret < 0) |
| 5019 | goto err; |
| 5020 | |
| 5021 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5022 | ret = -EFAULT; |
| 5023 | goto err; |
| 5024 | } |
| 5025 | break; |
| 5026 | } |
| 5027 | default: |
| 5028 | ret = -EINVAL; |
| 5029 | goto err; |
| 5030 | } |
| 5031 | ret = 0; |
| 5032 | err: |
| 5033 | if (thread) |
| 5034 | thread->looper_need_return = false; |
| 5035 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 5036 | if (ret && ret != -ERESTARTSYS) |
| 5037 | pr_info("%d:%d(%s:%s) ioctl %x %lx returned %d\n", proc->pid, current->pid, |
| 5038 | proc->tsk->comm, current->comm, cmd, arg, ret); |
| 5039 | err_unlocked: |
| 5040 | trace_binder_ioctl_done(ret); |
| 5041 | return ret; |
| 5042 | } |
| 5043 | |
| 5044 | static void binder_vma_open(struct vm_area_struct *vma) |
| 5045 | { |
| 5046 | struct binder_proc *proc = vma->vm_private_data; |
| 5047 | |
| 5048 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5049 | "%d(%s) open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 5050 | proc->pid, proc->tsk->comm, vma->vm_start, vma->vm_end, |
| 5051 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5052 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 5053 | } |
| 5054 | |
| 5055 | static void binder_vma_close(struct vm_area_struct *vma) |
| 5056 | { |
| 5057 | struct binder_proc *proc = vma->vm_private_data; |
| 5058 | |
| 5059 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5060 | "%d(%s) close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 5061 | proc->pid, proc->tsk->comm, vma->vm_start, vma->vm_end, |
| 5062 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5063 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 5064 | binder_alloc_vma_close(&proc->alloc); |
| 5065 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 5066 | } |
| 5067 | |
| 5068 | static int binder_vm_fault(struct vm_fault *vmf) |
| 5069 | { |
| 5070 | return VM_FAULT_SIGBUS; |
| 5071 | } |
| 5072 | |
| 5073 | static const struct vm_operations_struct binder_vm_ops = { |
| 5074 | .open = binder_vma_open, |
| 5075 | .close = binder_vma_close, |
| 5076 | .fault = binder_vm_fault, |
| 5077 | }; |
| 5078 | |
| 5079 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 5080 | { |
| 5081 | int ret; |
| 5082 | struct binder_proc *proc = filp->private_data; |
| 5083 | const char *failure_string; |
| 5084 | |
| 5085 | if (proc->tsk != current->group_leader) |
| 5086 | return -EINVAL; |
| 5087 | |
| 5088 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 5089 | vma->vm_end = vma->vm_start + SZ_4M; |
| 5090 | |
| 5091 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5092 | "%s: %d(%s) %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 5093 | __func__, proc->pid, proc->tsk->comm, vma->vm_start, vma->vm_end, |
| 5094 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5095 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 5096 | |
| 5097 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 5098 | ret = -EPERM; |
| 5099 | failure_string = "bad vm_flags"; |
| 5100 | goto err_bad_arg; |
| 5101 | } |
| 5102 | vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP; |
| 5103 | vma->vm_flags &= ~VM_MAYWRITE; |
| 5104 | |
| 5105 | vma->vm_ops = &binder_vm_ops; |
| 5106 | vma->vm_private_data = proc; |
| 5107 | |
| 5108 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 5109 | if (ret) { |
| 5110 | failure_string = ""; |
| 5111 | goto err_bad_arg; |
| 5112 | } |
| 5113 | mutex_lock(&proc->files_lock); |
| 5114 | proc->files = get_files_struct(current); |
| 5115 | mutex_unlock(&proc->files_lock); |
| 5116 | return 0; |
| 5117 | |
| 5118 | err_bad_arg: |
| 5119 | pr_err("%s: %d(%s) %lx-%lx %s failed %d\n", __func__, |
| 5120 | proc->pid, proc->tsk->comm, vma->vm_start, vma->vm_end, failure_string, ret); |
| 5121 | return ret; |
| 5122 | } |
| 5123 | |
| 5124 | static int binder_open(struct inode *nodp, struct file *filp) |
| 5125 | { |
| 5126 | struct binder_proc *proc; |
| 5127 | struct binder_device *binder_dev; |
| 5128 | |
| 5129 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d (%s:%s)\n", __func__, |
| 5130 | current->group_leader->pid, current->pid, |
| 5131 | current->group_leader->comm, current->comm); |
| 5132 | |
| 5133 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 5134 | if (proc == NULL) |
| 5135 | return -ENOMEM; |
| 5136 | spin_lock_init(&proc->inner_lock); |
| 5137 | spin_lock_init(&proc->outer_lock); |
| 5138 | get_task_struct(current->group_leader); |
| 5139 | proc->tsk = current->group_leader; |
| 5140 | mutex_init(&proc->files_lock); |
| 5141 | INIT_LIST_HEAD(&proc->todo); |
| 5142 | if (binder_supported_policy(current->policy)) { |
| 5143 | proc->default_priority.sched_policy = current->policy; |
| 5144 | proc->default_priority.prio = current->normal_prio; |
| 5145 | } else { |
| 5146 | proc->default_priority.sched_policy = SCHED_NORMAL; |
| 5147 | proc->default_priority.prio = NICE_TO_PRIO(0); |
| 5148 | } |
| 5149 | |
| 5150 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 5151 | miscdev); |
| 5152 | proc->context = &binder_dev->context; |
| 5153 | binder_alloc_init(&proc->alloc); |
| 5154 | |
| 5155 | binder_stats_created(BINDER_STAT_PROC); |
| 5156 | proc->pid = current->group_leader->pid; |
| 5157 | INIT_LIST_HEAD(&proc->delivered_death); |
| 5158 | INIT_LIST_HEAD(&proc->waiting_threads); |
| 5159 | filp->private_data = proc; |
| 5160 | |
| 5161 | mutex_lock(&binder_procs_lock); |
| 5162 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 5163 | mutex_unlock(&binder_procs_lock); |
| 5164 | |
| 5165 | if (binder_debugfs_dir_entry_proc) { |
| 5166 | char strbuf[11]; |
| 5167 | |
| 5168 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
| 5169 | /* |
| 5170 | * proc debug entries are shared between contexts, so |
| 5171 | * this will fail if the process tries to open the driver |
| 5172 | * again with a different context. The priting code will |
| 5173 | * anyway print all contexts that a given PID has, so this |
| 5174 | * is not a problem. |
| 5175 | */ |
| 5176 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
| 5177 | binder_debugfs_dir_entry_proc, |
| 5178 | (void *)(unsigned long)proc->pid, |
| 5179 | &binder_proc_fops); |
| 5180 | } |
| 5181 | |
| 5182 | return 0; |
| 5183 | } |
| 5184 | |
| 5185 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 5186 | { |
| 5187 | struct binder_proc *proc = filp->private_data; |
| 5188 | |
| 5189 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 5190 | |
| 5191 | return 0; |
| 5192 | } |
| 5193 | |
| 5194 | static void binder_deferred_flush(struct binder_proc *proc) |
| 5195 | { |
| 5196 | struct rb_node *n; |
| 5197 | int wake_count = 0; |
| 5198 | |
| 5199 | binder_inner_proc_lock(proc); |
| 5200 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 5201 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
| 5202 | |
| 5203 | thread->looper_need_return = true; |
| 5204 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 5205 | wake_up_interruptible(&thread->wait); |
| 5206 | wake_count++; |
| 5207 | } |
| 5208 | } |
| 5209 | binder_inner_proc_unlock(proc); |
| 5210 | |
| 5211 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5212 | "binder_flush: %d(%s) woke %d threads\n", proc->pid, proc->tsk->comm, |
| 5213 | wake_count); |
| 5214 | } |
| 5215 | |
| 5216 | static int binder_release(struct inode *nodp, struct file *filp) |
| 5217 | { |
| 5218 | struct binder_proc *proc = filp->private_data; |
| 5219 | |
| 5220 | debugfs_remove(proc->debugfs_entry); |
| 5221 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 5222 | |
| 5223 | return 0; |
| 5224 | } |
| 5225 | |
| 5226 | static int binder_node_release(struct binder_node *node, int refs) |
| 5227 | { |
| 5228 | struct binder_ref *ref; |
| 5229 | int death = 0; |
| 5230 | struct binder_proc *proc = node->proc; |
| 5231 | |
| 5232 | binder_release_work(proc, &node->async_todo); |
| 5233 | |
| 5234 | binder_node_lock(node); |
| 5235 | binder_inner_proc_lock(proc); |
| 5236 | binder_dequeue_work_ilocked(&node->work); |
| 5237 | /* |
| 5238 | * The caller must have taken a temporary ref on the node, |
| 5239 | */ |
| 5240 | BUG_ON(!node->tmp_refs); |
| 5241 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
| 5242 | binder_inner_proc_unlock(proc); |
| 5243 | binder_node_unlock(node); |
| 5244 | binder_free_node(node); |
| 5245 | |
| 5246 | return refs; |
| 5247 | } |
| 5248 | |
| 5249 | node->proc = NULL; |
| 5250 | node->local_strong_refs = 0; |
| 5251 | node->local_weak_refs = 0; |
| 5252 | binder_inner_proc_unlock(proc); |
| 5253 | |
| 5254 | spin_lock(&binder_dead_nodes_lock); |
| 5255 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
| 5256 | spin_unlock(&binder_dead_nodes_lock); |
| 5257 | |
| 5258 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 5259 | refs++; |
| 5260 | /* |
| 5261 | * Need the node lock to synchronize |
| 5262 | * with new notification requests and the |
| 5263 | * inner lock to synchronize with queued |
| 5264 | * death notifications. |
| 5265 | */ |
| 5266 | binder_inner_proc_lock(ref->proc); |
| 5267 | if (!ref->death) { |
| 5268 | binder_inner_proc_unlock(ref->proc); |
| 5269 | continue; |
| 5270 | } |
| 5271 | |
| 5272 | death++; |
| 5273 | |
| 5274 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 5275 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 5276 | binder_enqueue_work_ilocked(&ref->death->work, |
| 5277 | &ref->proc->todo); |
| 5278 | binder_wakeup_proc_ilocked(ref->proc); |
| 5279 | binder_inner_proc_unlock(ref->proc); |
| 5280 | } |
| 5281 | |
| 5282 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 5283 | "node %d now dead, refs %d, death %d\n", |
| 5284 | node->debug_id, refs, death); |
| 5285 | binder_node_unlock(node); |
| 5286 | binder_put_node(node); |
| 5287 | |
| 5288 | return refs; |
| 5289 | } |
| 5290 | |
| 5291 | static void binder_deferred_release(struct binder_proc *proc) |
| 5292 | { |
| 5293 | struct binder_context *context = proc->context; |
| 5294 | struct rb_node *n; |
| 5295 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
| 5296 | |
| 5297 | BUG_ON(proc->files); |
| 5298 | |
| 5299 | mutex_lock(&binder_procs_lock); |
| 5300 | hlist_del(&proc->proc_node); |
| 5301 | mutex_unlock(&binder_procs_lock); |
| 5302 | |
| 5303 | mutex_lock(&context->context_mgr_node_lock); |
| 5304 | if (context->binder_context_mgr_node && |
| 5305 | context->binder_context_mgr_node->proc == proc) { |
| 5306 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 5307 | "%s: %d(%s) context_mgr_node gone\n", |
| 5308 | __func__, proc->pid, proc->tsk->comm); |
| 5309 | context->binder_context_mgr_node = NULL; |
| 5310 | } |
| 5311 | mutex_unlock(&context->context_mgr_node_lock); |
| 5312 | binder_inner_proc_lock(proc); |
| 5313 | /* |
| 5314 | * Make sure proc stays alive after we |
| 5315 | * remove all the threads |
| 5316 | */ |
| 5317 | proc->tmp_ref++; |
| 5318 | |
| 5319 | proc->is_dead = true; |
| 5320 | threads = 0; |
| 5321 | active_transactions = 0; |
| 5322 | while ((n = rb_first(&proc->threads))) { |
| 5323 | struct binder_thread *thread; |
| 5324 | |
| 5325 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 5326 | binder_inner_proc_unlock(proc); |
| 5327 | threads++; |
| 5328 | active_transactions += binder_thread_release(proc, thread); |
| 5329 | binder_inner_proc_lock(proc); |
| 5330 | } |
| 5331 | |
| 5332 | nodes = 0; |
| 5333 | incoming_refs = 0; |
| 5334 | while ((n = rb_first(&proc->nodes))) { |
| 5335 | struct binder_node *node; |
| 5336 | |
| 5337 | node = rb_entry(n, struct binder_node, rb_node); |
| 5338 | nodes++; |
| 5339 | /* |
| 5340 | * take a temporary ref on the node before |
| 5341 | * calling binder_node_release() which will either |
| 5342 | * kfree() the node or call binder_put_node() |
| 5343 | */ |
| 5344 | binder_inc_node_tmpref_ilocked(node); |
| 5345 | rb_erase(&node->rb_node, &proc->nodes); |
| 5346 | binder_inner_proc_unlock(proc); |
| 5347 | incoming_refs = binder_node_release(node, incoming_refs); |
| 5348 | binder_inner_proc_lock(proc); |
| 5349 | } |
| 5350 | binder_inner_proc_unlock(proc); |
| 5351 | |
| 5352 | outgoing_refs = 0; |
| 5353 | binder_proc_lock(proc); |
| 5354 | while ((n = rb_first(&proc->refs_by_desc))) { |
| 5355 | struct binder_ref *ref; |
| 5356 | |
| 5357 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 5358 | outgoing_refs++; |
| 5359 | binder_cleanup_ref_olocked(ref); |
| 5360 | binder_proc_unlock(proc); |
| 5361 | binder_free_ref(ref); |
| 5362 | binder_proc_lock(proc); |
| 5363 | } |
| 5364 | binder_proc_unlock(proc); |
| 5365 | |
| 5366 | binder_release_work(proc, &proc->todo); |
| 5367 | binder_release_work(proc, &proc->delivered_death); |
| 5368 | |
| 5369 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5370 | "%s: %d(%s) threads %d, nodes %d (ref %d), refs %d, active transactions %d\n", |
| 5371 | __func__, proc->pid, proc->tsk->comm, threads, nodes, incoming_refs, |
| 5372 | outgoing_refs, active_transactions); |
| 5373 | |
| 5374 | binder_proc_dec_tmpref(proc); |
| 5375 | } |
| 5376 | |
| 5377 | static void binder_deferred_func(struct work_struct *work) |
| 5378 | { |
| 5379 | struct binder_proc *proc; |
| 5380 | struct files_struct *files; |
| 5381 | |
| 5382 | int defer; |
| 5383 | |
| 5384 | do { |
| 5385 | mutex_lock(&binder_deferred_lock); |
| 5386 | if (!hlist_empty(&binder_deferred_list)) { |
| 5387 | proc = hlist_entry(binder_deferred_list.first, |
| 5388 | struct binder_proc, deferred_work_node); |
| 5389 | hlist_del_init(&proc->deferred_work_node); |
| 5390 | defer = proc->deferred_work; |
| 5391 | proc->deferred_work = 0; |
| 5392 | } else { |
| 5393 | proc = NULL; |
| 5394 | defer = 0; |
| 5395 | } |
| 5396 | mutex_unlock(&binder_deferred_lock); |
| 5397 | |
| 5398 | files = NULL; |
| 5399 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
| 5400 | mutex_lock(&proc->files_lock); |
| 5401 | files = proc->files; |
| 5402 | if (files) |
| 5403 | proc->files = NULL; |
| 5404 | mutex_unlock(&proc->files_lock); |
| 5405 | } |
| 5406 | |
| 5407 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5408 | binder_deferred_flush(proc); |
| 5409 | |
| 5410 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5411 | binder_deferred_release(proc); /* frees proc */ |
| 5412 | |
| 5413 | if (files) |
| 5414 | put_files_struct(files); |
| 5415 | } while (proc); |
| 5416 | } |
| 5417 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5418 | |
| 5419 | static void |
| 5420 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5421 | { |
| 5422 | mutex_lock(&binder_deferred_lock); |
| 5423 | proc->deferred_work |= defer; |
| 5424 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5425 | hlist_add_head(&proc->deferred_work_node, |
| 5426 | &binder_deferred_list); |
| 5427 | schedule_work(&binder_deferred_work); |
| 5428 | } |
| 5429 | mutex_unlock(&binder_deferred_lock); |
| 5430 | } |
| 5431 | |
| 5432 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5433 | struct binder_proc *proc, |
| 5434 | const char *prefix, |
| 5435 | struct binder_transaction *t) |
| 5436 | { |
| 5437 | struct binder_proc *to_proc; |
| 5438 | struct binder_buffer *buffer = t->buffer; |
| 5439 | |
| 5440 | spin_lock(&t->lock); |
| 5441 | to_proc = t->to_proc; |
| 5442 | seq_printf(m, |
| 5443 | "%s %d: %pK from %d:%d(%s:%s) to %d:%d(%s:%s) code %x flags %x pri %d:%d r%d", |
| 5444 | prefix, t->debug_id, t, |
| 5445 | t->from ? t->from->proc->pid : 0, |
| 5446 | t->from ? t->from->pid : 0, |
| 5447 | t->from ? t->from->proc->tsk->comm : "", |
| 5448 | t->from ? t->from->task->comm : "", |
| 5449 | to_proc ? to_proc->pid : 0, |
| 5450 | t->to_thread ? t->to_thread->pid : 0, |
| 5451 | to_proc ? to_proc->tsk->comm : "", |
| 5452 | t->to_thread ? t->to_thread->task->comm : "", |
| 5453 | t->code, t->flags, t->priority.sched_policy, |
| 5454 | t->priority.prio, t->need_reply); |
| 5455 | spin_unlock(&t->lock); |
| 5456 | |
| 5457 | if (proc != to_proc) { |
| 5458 | /* |
| 5459 | * Can only safely deref buffer if we are holding the |
| 5460 | * correct proc inner lock for this node |
| 5461 | */ |
| 5462 | seq_puts(m, "\n"); |
| 5463 | return; |
| 5464 | } |
| 5465 | |
| 5466 | if (buffer == NULL) { |
| 5467 | seq_puts(m, " buffer free\n"); |
| 5468 | return; |
| 5469 | } |
| 5470 | if (buffer->target_node) |
| 5471 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
| 5472 | seq_printf(m, " size %zd:%zd data %pK\n", |
| 5473 | buffer->data_size, buffer->offsets_size, |
| 5474 | buffer->data); |
| 5475 | } |
| 5476 | |
| 5477 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5478 | struct binder_proc *proc, |
| 5479 | const char *prefix, |
| 5480 | const char *transaction_prefix, |
| 5481 | struct binder_work *w) |
| 5482 | { |
| 5483 | struct binder_node *node; |
| 5484 | struct binder_transaction *t; |
| 5485 | |
| 5486 | switch (w->type) { |
| 5487 | case BINDER_WORK_TRANSACTION: |
| 5488 | t = container_of(w, struct binder_transaction, work); |
| 5489 | print_binder_transaction_ilocked( |
| 5490 | m, proc, transaction_prefix, t); |
| 5491 | break; |
| 5492 | case BINDER_WORK_RETURN_ERROR: { |
| 5493 | struct binder_error *e = container_of( |
| 5494 | w, struct binder_error, work); |
| 5495 | |
| 5496 | seq_printf(m, "%stransaction error: %u\n", |
| 5497 | prefix, e->cmd); |
| 5498 | } break; |
| 5499 | case BINDER_WORK_TRANSACTION_COMPLETE: |
| 5500 | seq_printf(m, "%stransaction complete\n", prefix); |
| 5501 | break; |
| 5502 | case BINDER_WORK_NODE: |
| 5503 | node = container_of(w, struct binder_node, work); |
| 5504 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5505 | prefix, node->debug_id, |
| 5506 | (u64)node->ptr, (u64)node->cookie); |
| 5507 | break; |
| 5508 | case BINDER_WORK_DEAD_BINDER: |
| 5509 | seq_printf(m, "%shas dead binder\n", prefix); |
| 5510 | break; |
| 5511 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 5512 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
| 5513 | break; |
| 5514 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
| 5515 | seq_printf(m, "%shas cleared death notification\n", prefix); |
| 5516 | break; |
| 5517 | default: |
| 5518 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
| 5519 | break; |
| 5520 | } |
| 5521 | } |
| 5522 | |
| 5523 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5524 | struct binder_thread *thread, |
| 5525 | int print_always) |
| 5526 | { |
| 5527 | struct binder_transaction *t; |
| 5528 | struct binder_work *w; |
| 5529 | size_t start_pos = m->count; |
| 5530 | size_t header_pos; |
| 5531 | |
| 5532 | seq_printf(m, " thread %d %s: l %02x need_return %d tr %d\n", |
| 5533 | thread->pid, thread->task->comm, thread->looper, |
| 5534 | thread->looper_need_return, |
| 5535 | atomic_read(&thread->tmp_ref)); |
| 5536 | header_pos = m->count; |
| 5537 | t = thread->transaction_stack; |
| 5538 | while (t) { |
| 5539 | if (t->from == thread) { |
| 5540 | print_binder_transaction_ilocked(m, thread->proc, |
| 5541 | " outgoing transaction", t); |
| 5542 | t = t->from_parent; |
| 5543 | } else if (t->to_thread == thread) { |
| 5544 | print_binder_transaction_ilocked(m, thread->proc, |
| 5545 | " incoming transaction", t); |
| 5546 | t = t->to_parent; |
| 5547 | } else { |
| 5548 | print_binder_transaction_ilocked(m, thread->proc, |
| 5549 | " bad transaction", t); |
| 5550 | t = NULL; |
| 5551 | } |
| 5552 | } |
| 5553 | list_for_each_entry(w, &thread->todo, entry) { |
| 5554 | print_binder_work_ilocked(m, thread->proc, " ", |
| 5555 | " pending transaction", w); |
| 5556 | } |
| 5557 | if (!print_always && m->count == header_pos) |
| 5558 | m->count = start_pos; |
| 5559 | } |
| 5560 | |
| 5561 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5562 | struct binder_node *node) |
| 5563 | { |
| 5564 | struct binder_ref *ref; |
| 5565 | struct binder_work *w; |
| 5566 | int count; |
| 5567 | |
| 5568 | count = 0; |
| 5569 | hlist_for_each_entry(ref, &node->refs, node_entry) |
| 5570 | count++; |
| 5571 | |
| 5572 | 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", |
| 5573 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
| 5574 | node->sched_policy, node->min_priority, |
| 5575 | node->has_strong_ref, node->has_weak_ref, |
| 5576 | node->local_strong_refs, node->local_weak_refs, |
| 5577 | node->internal_strong_refs, count, node->tmp_refs); |
| 5578 | if (count) { |
| 5579 | seq_puts(m, " proc"); |
| 5580 | hlist_for_each_entry(ref, &node->refs, node_entry) |
| 5581 | seq_printf(m, " %d %s", ref->proc->pid, ref->proc->tsk->comm); |
| 5582 | } |
| 5583 | seq_puts(m, "\n"); |
| 5584 | if (node->proc) { |
| 5585 | list_for_each_entry(w, &node->async_todo, entry) |
| 5586 | print_binder_work_ilocked(m, node->proc, " ", |
| 5587 | " pending async transaction", w); |
| 5588 | } |
| 5589 | } |
| 5590 | |
| 5591 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5592 | struct binder_ref *ref) |
| 5593 | { |
| 5594 | binder_node_lock(ref->node); |
| 5595 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5596 | ref->data.debug_id, ref->data.desc, |
| 5597 | ref->node->proc ? "" : "dead ", |
| 5598 | ref->node->debug_id, ref->data.strong, |
| 5599 | ref->data.weak, ref->death); |
| 5600 | binder_node_unlock(ref->node); |
| 5601 | } |
| 5602 | |
| 5603 | static void print_binder_proc(struct seq_file *m, |
| 5604 | struct binder_proc *proc, int print_all) |
| 5605 | { |
| 5606 | struct binder_work *w; |
| 5607 | struct rb_node *n; |
| 5608 | size_t start_pos = m->count; |
| 5609 | size_t header_pos; |
| 5610 | struct binder_node *last_node = NULL; |
| 5611 | |
| 5612 | seq_printf(m, "proc %d %s\n", proc->pid, proc->tsk->comm); |
| 5613 | seq_printf(m, "context %s\n", proc->context->name); |
| 5614 | header_pos = m->count; |
| 5615 | |
| 5616 | binder_inner_proc_lock(proc); |
| 5617 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5618 | print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread, |
| 5619 | rb_node), print_all); |
| 5620 | |
| 5621 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 5622 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5623 | rb_node); |
| 5624 | /* |
| 5625 | * take a temporary reference on the node so it |
| 5626 | * survives and isn't removed from the tree |
| 5627 | * while we print it. |
| 5628 | */ |
| 5629 | binder_inc_node_tmpref_ilocked(node); |
| 5630 | /* Need to drop inner lock to take node lock */ |
| 5631 | binder_inner_proc_unlock(proc); |
| 5632 | if (last_node) |
| 5633 | binder_put_node(last_node); |
| 5634 | binder_node_inner_lock(node); |
| 5635 | print_binder_node_nilocked(m, node); |
| 5636 | binder_node_inner_unlock(node); |
| 5637 | last_node = node; |
| 5638 | binder_inner_proc_lock(proc); |
| 5639 | } |
| 5640 | binder_inner_proc_unlock(proc); |
| 5641 | if (last_node) |
| 5642 | binder_put_node(last_node); |
| 5643 | |
| 5644 | if (print_all) { |
| 5645 | binder_proc_lock(proc); |
| 5646 | for (n = rb_first(&proc->refs_by_desc); |
| 5647 | n != NULL; |
| 5648 | n = rb_next(n)) |
| 5649 | print_binder_ref_olocked(m, rb_entry(n, |
| 5650 | struct binder_ref, |
| 5651 | rb_node_desc)); |
| 5652 | binder_proc_unlock(proc); |
| 5653 | } |
| 5654 | binder_alloc_print_allocated(m, &proc->alloc); |
| 5655 | binder_inner_proc_lock(proc); |
| 5656 | list_for_each_entry(w, &proc->todo, entry) |
| 5657 | print_binder_work_ilocked(m, proc, " ", |
| 5658 | " pending transaction", w); |
| 5659 | list_for_each_entry(w, &proc->delivered_death, entry) { |
| 5660 | seq_puts(m, " has delivered dead binder\n"); |
| 5661 | break; |
| 5662 | } |
| 5663 | binder_inner_proc_unlock(proc); |
| 5664 | if (!print_all && m->count == header_pos) |
| 5665 | m->count = start_pos; |
| 5666 | } |
| 5667 | |
| 5668 | static const char * const binder_return_strings[] = { |
| 5669 | "BR_ERROR", |
| 5670 | "BR_OK", |
| 5671 | "BR_TRANSACTION", |
| 5672 | "BR_REPLY", |
| 5673 | "BR_ACQUIRE_RESULT", |
| 5674 | "BR_DEAD_REPLY", |
| 5675 | "BR_TRANSACTION_COMPLETE", |
| 5676 | "BR_INCREFS", |
| 5677 | "BR_ACQUIRE", |
| 5678 | "BR_RELEASE", |
| 5679 | "BR_DECREFS", |
| 5680 | "BR_ATTEMPT_ACQUIRE", |
| 5681 | "BR_NOOP", |
| 5682 | "BR_SPAWN_LOOPER", |
| 5683 | "BR_FINISHED", |
| 5684 | "BR_DEAD_BINDER", |
| 5685 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5686 | "BR_FAILED_REPLY" |
| 5687 | }; |
| 5688 | |
| 5689 | static const char * const binder_command_strings[] = { |
| 5690 | "BC_TRANSACTION", |
| 5691 | "BC_REPLY", |
| 5692 | "BC_ACQUIRE_RESULT", |
| 5693 | "BC_FREE_BUFFER", |
| 5694 | "BC_INCREFS", |
| 5695 | "BC_ACQUIRE", |
| 5696 | "BC_RELEASE", |
| 5697 | "BC_DECREFS", |
| 5698 | "BC_INCREFS_DONE", |
| 5699 | "BC_ACQUIRE_DONE", |
| 5700 | "BC_ATTEMPT_ACQUIRE", |
| 5701 | "BC_REGISTER_LOOPER", |
| 5702 | "BC_ENTER_LOOPER", |
| 5703 | "BC_EXIT_LOOPER", |
| 5704 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5705 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 5706 | "BC_DEAD_BINDER_DONE", |
| 5707 | "BC_TRANSACTION_SG", |
| 5708 | "BC_REPLY_SG", |
| 5709 | }; |
| 5710 | |
| 5711 | static const char * const binder_objstat_strings[] = { |
| 5712 | "proc", |
| 5713 | "thread", |
| 5714 | "node", |
| 5715 | "ref", |
| 5716 | "death", |
| 5717 | "transaction", |
| 5718 | "transaction_complete" |
| 5719 | }; |
| 5720 | |
| 5721 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5722 | struct binder_stats *stats) |
| 5723 | { |
| 5724 | int i; |
| 5725 | |
| 5726 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
| 5727 | ARRAY_SIZE(binder_command_strings)); |
| 5728 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
| 5729 | int temp = atomic_read(&stats->bc[i]); |
| 5730 | |
| 5731 | if (temp) |
| 5732 | seq_printf(m, "%s%s: %d\n", prefix, |
| 5733 | binder_command_strings[i], temp); |
| 5734 | } |
| 5735 | |
| 5736 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
| 5737 | ARRAY_SIZE(binder_return_strings)); |
| 5738 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
| 5739 | int temp = atomic_read(&stats->br[i]); |
| 5740 | |
| 5741 | if (temp) |
| 5742 | seq_printf(m, "%s%s: %d\n", prefix, |
| 5743 | binder_return_strings[i], temp); |
| 5744 | } |
| 5745 | |
| 5746 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
| 5747 | ARRAY_SIZE(binder_objstat_strings)); |
| 5748 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
| 5749 | ARRAY_SIZE(stats->obj_deleted)); |
| 5750 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
| 5751 | int created = atomic_read(&stats->obj_created[i]); |
| 5752 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5753 | |
| 5754 | if (created || deleted) |
| 5755 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5756 | prefix, |
| 5757 | binder_objstat_strings[i], |
| 5758 | created - deleted, |
| 5759 | created); |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | static void print_binder_proc_stats(struct seq_file *m, |
| 5764 | struct binder_proc *proc) |
| 5765 | { |
| 5766 | struct binder_work *w; |
| 5767 | struct binder_thread *thread; |
| 5768 | struct rb_node *n; |
| 5769 | int count, strong, weak, ready_threads; |
| 5770 | size_t free_async_space = |
| 5771 | binder_alloc_get_free_async_space(&proc->alloc); |
| 5772 | |
| 5773 | seq_printf(m, "proc %d %s\n", proc->pid, proc->tsk->comm); |
| 5774 | seq_printf(m, "context %s\n", proc->context->name); |
| 5775 | count = 0; |
| 5776 | ready_threads = 0; |
| 5777 | binder_inner_proc_lock(proc); |
| 5778 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5779 | count++; |
| 5780 | |
| 5781 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5782 | ready_threads++; |
| 5783 | |
| 5784 | seq_printf(m, " threads: %d\n", count); |
| 5785 | seq_printf(m, " requested threads: %d+%d/%d\n" |
| 5786 | " ready threads %d\n" |
| 5787 | " free async space %zd\n", proc->requested_threads, |
| 5788 | proc->requested_threads_started, proc->max_threads, |
| 5789 | ready_threads, |
| 5790 | free_async_space); |
| 5791 | count = 0; |
| 5792 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5793 | count++; |
| 5794 | binder_inner_proc_unlock(proc); |
| 5795 | seq_printf(m, " nodes: %d\n", count); |
| 5796 | count = 0; |
| 5797 | strong = 0; |
| 5798 | weak = 0; |
| 5799 | binder_proc_lock(proc); |
| 5800 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5801 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5802 | rb_node_desc); |
| 5803 | count++; |
| 5804 | strong += ref->data.strong; |
| 5805 | weak += ref->data.weak; |
| 5806 | } |
| 5807 | binder_proc_unlock(proc); |
| 5808 | seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak); |
| 5809 | |
| 5810 | count = binder_alloc_get_allocated_count(&proc->alloc); |
| 5811 | seq_printf(m, " buffers: %d\n", count); |
| 5812 | |
| 5813 | binder_alloc_print_pages(m, &proc->alloc); |
| 5814 | |
| 5815 | count = 0; |
| 5816 | binder_inner_proc_lock(proc); |
| 5817 | list_for_each_entry(w, &proc->todo, entry) { |
| 5818 | if (w->type == BINDER_WORK_TRANSACTION) |
| 5819 | count++; |
| 5820 | } |
| 5821 | binder_inner_proc_unlock(proc); |
| 5822 | seq_printf(m, " pending transactions: %d\n", count); |
| 5823 | |
| 5824 | print_binder_stats(m, " ", &proc->stats); |
| 5825 | } |
| 5826 | |
| 5827 | |
| 5828 | static int binder_state_show(struct seq_file *m, void *unused) |
| 5829 | { |
| 5830 | struct binder_proc *proc; |
| 5831 | struct binder_node *node; |
| 5832 | struct binder_node *last_node = NULL; |
| 5833 | |
| 5834 | seq_puts(m, "binder state:\n"); |
| 5835 | |
| 5836 | spin_lock(&binder_dead_nodes_lock); |
| 5837 | if (!hlist_empty(&binder_dead_nodes)) |
| 5838 | seq_puts(m, "dead nodes:\n"); |
| 5839 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5840 | /* |
| 5841 | * take a temporary reference on the node so it |
| 5842 | * survives and isn't removed from the list |
| 5843 | * while we print it. |
| 5844 | */ |
| 5845 | node->tmp_refs++; |
| 5846 | spin_unlock(&binder_dead_nodes_lock); |
| 5847 | if (last_node) |
| 5848 | binder_put_node(last_node); |
| 5849 | binder_node_lock(node); |
| 5850 | print_binder_node_nilocked(m, node); |
| 5851 | binder_node_unlock(node); |
| 5852 | last_node = node; |
| 5853 | spin_lock(&binder_dead_nodes_lock); |
| 5854 | } |
| 5855 | spin_unlock(&binder_dead_nodes_lock); |
| 5856 | if (last_node) |
| 5857 | binder_put_node(last_node); |
| 5858 | |
| 5859 | mutex_lock(&binder_procs_lock); |
| 5860 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
| 5861 | print_binder_proc(m, proc, 1); |
| 5862 | mutex_unlock(&binder_procs_lock); |
| 5863 | |
| 5864 | return 0; |
| 5865 | } |
| 5866 | |
| 5867 | static int binder_stats_show(struct seq_file *m, void *unused) |
| 5868 | { |
| 5869 | struct binder_proc *proc; |
| 5870 | |
| 5871 | seq_puts(m, "binder stats:\n"); |
| 5872 | |
| 5873 | print_binder_stats(m, "", &binder_stats); |
| 5874 | |
| 5875 | mutex_lock(&binder_procs_lock); |
| 5876 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
| 5877 | print_binder_proc_stats(m, proc); |
| 5878 | mutex_unlock(&binder_procs_lock); |
| 5879 | |
| 5880 | return 0; |
| 5881 | } |
| 5882 | |
| 5883 | static int binder_transactions_show(struct seq_file *m, void *unused) |
| 5884 | { |
| 5885 | struct binder_proc *proc; |
| 5886 | |
| 5887 | seq_puts(m, "binder transactions:\n"); |
| 5888 | mutex_lock(&binder_procs_lock); |
| 5889 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
| 5890 | print_binder_proc(m, proc, 0); |
| 5891 | mutex_unlock(&binder_procs_lock); |
| 5892 | |
| 5893 | return 0; |
| 5894 | } |
| 5895 | |
| 5896 | static int binder_proc_show(struct seq_file *m, void *unused) |
| 5897 | { |
| 5898 | struct binder_proc *itr; |
| 5899 | int pid = (unsigned long)m->private; |
| 5900 | |
| 5901 | mutex_lock(&binder_procs_lock); |
| 5902 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
| 5903 | if (itr->pid == pid) { |
| 5904 | seq_puts(m, "binder proc state:\n"); |
| 5905 | print_binder_proc(m, itr, 1); |
| 5906 | } |
| 5907 | } |
| 5908 | mutex_unlock(&binder_procs_lock); |
| 5909 | |
| 5910 | return 0; |
| 5911 | } |
| 5912 | |
| 5913 | static void print_binder_transaction_log_entry(struct seq_file *m, |
| 5914 | struct binder_transaction_log_entry *e) |
| 5915 | { |
| 5916 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5917 | /* |
| 5918 | * read barrier to guarantee debug_id_done read before |
| 5919 | * we print the log values |
| 5920 | */ |
| 5921 | smp_rmb(); |
| 5922 | seq_printf(m, |
| 5923 | "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d", |
| 5924 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5925 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
| 5926 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
| 5927 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5928 | e->return_error, e->return_error_param, |
| 5929 | e->return_error_line); |
| 5930 | /* |
| 5931 | * read-barrier to guarantee read of debug_id_done after |
| 5932 | * done printing the fields of the entry |
| 5933 | */ |
| 5934 | smp_rmb(); |
| 5935 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5936 | "\n" : " (incomplete)\n"); |
| 5937 | } |
| 5938 | |
| 5939 | static int binder_transaction_log_show(struct seq_file *m, void *unused) |
| 5940 | { |
| 5941 | struct binder_transaction_log *log = m->private; |
| 5942 | unsigned int log_cur = atomic_read(&log->cur); |
| 5943 | unsigned int count; |
| 5944 | unsigned int cur; |
| 5945 | int i; |
| 5946 | |
| 5947 | count = log_cur + 1; |
| 5948 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5949 | 0 : count % ARRAY_SIZE(log->entry); |
| 5950 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5951 | count = ARRAY_SIZE(log->entry); |
| 5952 | for (i = 0; i < count; i++) { |
| 5953 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5954 | |
| 5955 | print_binder_transaction_log_entry(m, &log->entry[index]); |
| 5956 | } |
| 5957 | return 0; |
| 5958 | } |
| 5959 | |
| 5960 | static const struct file_operations binder_fops = { |
| 5961 | .owner = THIS_MODULE, |
| 5962 | .poll = binder_poll, |
| 5963 | .unlocked_ioctl = binder_ioctl, |
| 5964 | .compat_ioctl = binder_ioctl, |
| 5965 | .mmap = binder_mmap, |
| 5966 | .open = binder_open, |
| 5967 | .flush = binder_flush, |
| 5968 | .release = binder_release, |
| 5969 | }; |
| 5970 | |
| 5971 | BINDER_DEBUG_ENTRY(state); |
| 5972 | BINDER_DEBUG_ENTRY(stats); |
| 5973 | BINDER_DEBUG_ENTRY(transactions); |
| 5974 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5975 | |
| 5976 | static int __init init_binder_device(const char *name) |
| 5977 | { |
| 5978 | int ret; |
| 5979 | struct binder_device *binder_device; |
| 5980 | |
| 5981 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5982 | if (!binder_device) |
| 5983 | return -ENOMEM; |
| 5984 | |
| 5985 | binder_device->miscdev.fops = &binder_fops; |
| 5986 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5987 | binder_device->miscdev.name = name; |
| 5988 | |
| 5989 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5990 | binder_device->context.name = name; |
| 5991 | mutex_init(&binder_device->context.context_mgr_node_lock); |
| 5992 | |
| 5993 | ret = misc_register(&binder_device->miscdev); |
| 5994 | if (ret < 0) { |
| 5995 | kfree(binder_device); |
| 5996 | return ret; |
| 5997 | } |
| 5998 | |
| 5999 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 6000 | |
| 6001 | return ret; |
| 6002 | } |
| 6003 | |
| 6004 | static int __init binder_init(void) |
| 6005 | { |
| 6006 | int ret; |
| 6007 | char *device_name, *device_names, *device_tmp; |
| 6008 | struct binder_device *device; |
| 6009 | struct hlist_node *tmp; |
| 6010 | |
| 6011 | ret = binder_alloc_shrinker_init(); |
| 6012 | if (ret) |
| 6013 | return ret; |
| 6014 | |
| 6015 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 6016 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 6017 | |
| 6018 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 6019 | if (binder_debugfs_dir_entry_root) |
| 6020 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 6021 | binder_debugfs_dir_entry_root); |
| 6022 | |
| 6023 | if (binder_debugfs_dir_entry_root) { |
| 6024 | debugfs_create_file("state", |
| 6025 | 0444, |
| 6026 | binder_debugfs_dir_entry_root, |
| 6027 | NULL, |
| 6028 | &binder_state_fops); |
| 6029 | debugfs_create_file("stats", |
| 6030 | 0444, |
| 6031 | binder_debugfs_dir_entry_root, |
| 6032 | NULL, |
| 6033 | &binder_stats_fops); |
| 6034 | debugfs_create_file("transactions", |
| 6035 | 0444, |
| 6036 | binder_debugfs_dir_entry_root, |
| 6037 | NULL, |
| 6038 | &binder_transactions_fops); |
| 6039 | debugfs_create_file("transaction_log", |
| 6040 | 0444, |
| 6041 | binder_debugfs_dir_entry_root, |
| 6042 | &binder_transaction_log, |
| 6043 | &binder_transaction_log_fops); |
| 6044 | debugfs_create_file("failed_transaction_log", |
| 6045 | 0444, |
| 6046 | binder_debugfs_dir_entry_root, |
| 6047 | &binder_transaction_log_failed, |
| 6048 | &binder_transaction_log_fops); |
| 6049 | } |
| 6050 | |
| 6051 | /* |
| 6052 | * Copy the module_parameter string, because we don't want to |
| 6053 | * tokenize it in-place. |
| 6054 | */ |
| 6055 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 6056 | if (!device_names) { |
| 6057 | ret = -ENOMEM; |
| 6058 | goto err_alloc_device_names_failed; |
| 6059 | } |
| 6060 | strcpy(device_names, binder_devices_param); |
| 6061 | |
| 6062 | device_tmp = device_names; |
| 6063 | while ((device_name = strsep(&device_tmp, ","))) { |
| 6064 | ret = init_binder_device(device_name); |
| 6065 | if (ret) |
| 6066 | goto err_init_binder_device_failed; |
| 6067 | } |
| 6068 | |
| 6069 | return ret; |
| 6070 | |
| 6071 | err_init_binder_device_failed: |
| 6072 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 6073 | misc_deregister(&device->miscdev); |
| 6074 | hlist_del(&device->hlist); |
| 6075 | kfree(device); |
| 6076 | } |
| 6077 | |
| 6078 | kfree(device_names); |
| 6079 | |
| 6080 | err_alloc_device_names_failed: |
| 6081 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 6082 | |
| 6083 | return ret; |
| 6084 | } |
| 6085 | |
| 6086 | device_initcall(binder_init); |
| 6087 | |
| 6088 | #define CREATE_TRACE_POINTS |
| 6089 | #include "binder_trace.h" |
| 6090 | |
| 6091 | MODULE_LICENSE("GPL v2"); |