3 * Android IPC Subsystem
5 * Copyright (C) 2007-2008 Google, Inc.
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.
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.
21 * There are 3 main spinlocks which must be acquired in the
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
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()
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
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
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <linux/fdtable.h>
55 #include <linux/file.h>
56 #include <linux/freezer.h>
58 #include <linux/list.h>
59 #include <linux/miscdevice.h>
60 #include <linux/module.h>
61 #include <linux/mutex.h>
62 #include <linux/nsproxy.h>
63 #include <linux/poll.h>
64 #include <linux/debugfs.h>
65 #include <linux/rbtree.h>
66 #include <linux/sched/signal.h>
67 #include <linux/sched/mm.h>
68 #include <linux/seq_file.h>
69 #include <linux/uaccess.h>
70 #include <linux/pid_namespace.h>
71 #include <linux/security.h>
72 #include <linux/spinlock.h>
73 #include <linux/ratelimit.h>
75 #include <uapi/linux/android/binder.h>
77 #include <asm/cacheflush.h>
79 #include "binder_alloc.h"
80 #include "binder_trace.h"
82 static HLIST_HEAD(binder_deferred_list
);
83 static DEFINE_MUTEX(binder_deferred_lock
);
85 static HLIST_HEAD(binder_devices
);
86 static HLIST_HEAD(binder_procs
);
87 static DEFINE_MUTEX(binder_procs_lock
);
89 static HLIST_HEAD(binder_dead_nodes
);
90 static DEFINE_SPINLOCK(binder_dead_nodes_lock
);
92 static struct dentry
*binder_debugfs_dir_entry_root
;
93 static struct dentry
*binder_debugfs_dir_entry_proc
;
94 static atomic_t binder_last_id
;
96 #define BINDER_DEBUG_ENTRY(name) \
97 static int binder_##name##_open(struct inode *inode, struct file *file) \
99 return single_open(file, binder_##name##_show, inode->i_private); \
102 static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
110 static int binder_proc_show(struct seq_file
*m
, void *unused
);
111 BINDER_DEBUG_ENTRY(proc
);
113 /* This is only defined in include/asm-arm/sizes.h */
119 #define SZ_4M 0x400000
122 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
125 BINDER_DEBUG_USER_ERROR
= 1U << 0,
126 BINDER_DEBUG_FAILED_TRANSACTION
= 1U << 1,
127 BINDER_DEBUG_DEAD_TRANSACTION
= 1U << 2,
128 BINDER_DEBUG_OPEN_CLOSE
= 1U << 3,
129 BINDER_DEBUG_DEAD_BINDER
= 1U << 4,
130 BINDER_DEBUG_DEATH_NOTIFICATION
= 1U << 5,
131 BINDER_DEBUG_READ_WRITE
= 1U << 6,
132 BINDER_DEBUG_USER_REFS
= 1U << 7,
133 BINDER_DEBUG_THREADS
= 1U << 8,
134 BINDER_DEBUG_TRANSACTION
= 1U << 9,
135 BINDER_DEBUG_TRANSACTION_COMPLETE
= 1U << 10,
136 BINDER_DEBUG_FREE_BUFFER
= 1U << 11,
137 BINDER_DEBUG_INTERNAL_REFS
= 1U << 12,
138 BINDER_DEBUG_PRIORITY_CAP
= 1U << 13,
139 BINDER_DEBUG_SPINLOCKS
= 1U << 14,
141 static uint32_t binder_debug_mask
= BINDER_DEBUG_USER_ERROR
|
142 BINDER_DEBUG_FAILED_TRANSACTION
| BINDER_DEBUG_DEAD_TRANSACTION
;
143 module_param_named(debug_mask
, binder_debug_mask
, uint
, 0644);
145 static char *binder_devices_param
= CONFIG_ANDROID_BINDER_DEVICES
;
146 module_param_named(devices
, binder_devices_param
, charp
, 0444);
148 static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait
);
149 static int binder_stop_on_user_error
;
151 static int binder_set_stop_on_user_error(const char *val
,
152 const struct kernel_param
*kp
)
156 ret
= param_set_int(val
, kp
);
157 if (binder_stop_on_user_error
< 2)
158 wake_up(&binder_user_error_wait
);
161 module_param_call(stop_on_user_error
, binder_set_stop_on_user_error
,
162 param_get_int
, &binder_stop_on_user_error
, 0644);
164 #define binder_debug(mask, x...) \
166 if (binder_debug_mask & mask) \
167 pr_info_ratelimited(x); \
170 #define binder_user_error(x...) \
172 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
173 pr_info_ratelimited(x); \
174 if (binder_stop_on_user_error) \
175 binder_stop_on_user_error = 2; \
178 #define to_flat_binder_object(hdr) \
179 container_of(hdr, struct flat_binder_object, hdr)
181 #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
183 #define to_binder_buffer_object(hdr) \
184 container_of(hdr, struct binder_buffer_object, hdr)
186 #define to_binder_fd_array_object(hdr) \
187 container_of(hdr, struct binder_fd_array_object, hdr)
189 enum binder_stat_types
{
195 BINDER_STAT_TRANSACTION
,
196 BINDER_STAT_TRANSACTION_COMPLETE
,
200 struct binder_stats
{
201 atomic_t br
[_IOC_NR(BR_FAILED_REPLY
) + 1];
202 atomic_t bc
[_IOC_NR(BC_REPLY_SG
) + 1];
203 atomic_t obj_created
[BINDER_STAT_COUNT
];
204 atomic_t obj_deleted
[BINDER_STAT_COUNT
];
207 static struct binder_stats binder_stats
;
209 static inline void binder_stats_deleted(enum binder_stat_types type
)
211 atomic_inc(&binder_stats
.obj_deleted
[type
]);
214 static inline void binder_stats_created(enum binder_stat_types type
)
216 atomic_inc(&binder_stats
.obj_created
[type
]);
219 struct binder_transaction_log_entry
{
231 int return_error_line
;
232 uint32_t return_error
;
233 uint32_t return_error_param
;
234 const char *context_name
;
236 struct binder_transaction_log
{
239 struct binder_transaction_log_entry entry
[32];
241 static struct binder_transaction_log binder_transaction_log
;
242 static struct binder_transaction_log binder_transaction_log_failed
;
244 static struct binder_transaction_log_entry
*binder_transaction_log_add(
245 struct binder_transaction_log
*log
)
247 struct binder_transaction_log_entry
*e
;
248 unsigned int cur
= atomic_inc_return(&log
->cur
);
250 if (cur
>= ARRAY_SIZE(log
->entry
))
252 e
= &log
->entry
[cur
% ARRAY_SIZE(log
->entry
)];
253 WRITE_ONCE(e
->debug_id_done
, 0);
255 * write-barrier to synchronize access to e->debug_id_done.
256 * We make sure the initialized 0 value is seen before
257 * memset() other fields are zeroed by memset.
260 memset(e
, 0, sizeof(*e
));
264 struct binder_context
{
265 struct binder_node
*binder_context_mgr_node
;
266 struct mutex context_mgr_node_lock
;
268 kuid_t binder_context_mgr_uid
;
272 struct binder_device
{
273 struct hlist_node hlist
;
274 struct miscdevice miscdev
;
275 struct binder_context context
;
279 * struct binder_work - work enqueued on a worklist
280 * @entry: node enqueued on list
281 * @type: type of work to be performed
283 * There are separate work lists for proc, thread, and node (async).
286 struct list_head entry
;
289 BINDER_WORK_TRANSACTION
= 1,
290 BINDER_WORK_TRANSACTION_COMPLETE
,
291 BINDER_WORK_RETURN_ERROR
,
293 BINDER_WORK_DEAD_BINDER
,
294 BINDER_WORK_DEAD_BINDER_AND_CLEAR
,
295 BINDER_WORK_CLEAR_DEATH_NOTIFICATION
,
299 struct binder_error
{
300 struct binder_work work
;
305 * struct binder_node - binder node bookkeeping
306 * @debug_id: unique ID for debugging
307 * (invariant after initialized)
308 * @lock: lock for node fields
309 * @work: worklist element for node work
310 * (protected by @proc->inner_lock)
311 * @rb_node: element for proc->nodes tree
312 * (protected by @proc->inner_lock)
313 * @dead_node: element for binder_dead_nodes list
314 * (protected by binder_dead_nodes_lock)
315 * @proc: binder_proc that owns this node
316 * (invariant after initialized)
317 * @refs: list of references on this node
318 * (protected by @lock)
319 * @internal_strong_refs: used to take strong references when
320 * initiating a transaction
321 * (protected by @proc->inner_lock if @proc
323 * @local_weak_refs: weak user refs from local process
324 * (protected by @proc->inner_lock if @proc
326 * @local_strong_refs: strong user refs from local process
327 * (protected by @proc->inner_lock if @proc
329 * @tmp_refs: temporary kernel refs
330 * (protected by @proc->inner_lock while @proc
331 * is valid, and by binder_dead_nodes_lock
332 * if @proc is NULL. During inc/dec and node release
333 * it is also protected by @lock to provide safety
334 * as the node dies and @proc becomes NULL)
335 * @ptr: userspace pointer for node
336 * (invariant, no lock needed)
337 * @cookie: userspace cookie for node
338 * (invariant, no lock needed)
339 * @has_strong_ref: userspace notified of strong ref
340 * (protected by @proc->inner_lock if @proc
342 * @pending_strong_ref: userspace has acked notification of strong ref
343 * (protected by @proc->inner_lock if @proc
345 * @has_weak_ref: userspace notified of weak ref
346 * (protected by @proc->inner_lock if @proc
348 * @pending_weak_ref: userspace has acked notification of weak ref
349 * (protected by @proc->inner_lock if @proc
351 * @has_async_transaction: async transaction to node in progress
352 * (protected by @lock)
353 * @accept_fds: file descriptor operations supported for node
354 * (invariant after initialized)
355 * @min_priority: minimum scheduling priority
356 * (invariant after initialized)
357 * @async_todo: list of async work items
358 * (protected by @proc->inner_lock)
360 * Bookkeeping structure for binder nodes.
365 struct binder_work work
;
367 struct rb_node rb_node
;
368 struct hlist_node dead_node
;
370 struct binder_proc
*proc
;
371 struct hlist_head refs
;
372 int internal_strong_refs
;
374 int local_strong_refs
;
376 binder_uintptr_t ptr
;
377 binder_uintptr_t cookie
;
380 * bitfield elements protected by
384 u8 pending_strong_ref
:1;
386 u8 pending_weak_ref
:1;
390 * invariant after initialization
395 bool has_async_transaction
;
396 struct list_head async_todo
;
399 struct binder_ref_death
{
401 * @work: worklist element for death notifications
402 * (protected by inner_lock of the proc that
403 * this ref belongs to)
405 struct binder_work work
;
406 binder_uintptr_t cookie
;
410 * struct binder_ref_data - binder_ref counts and id
411 * @debug_id: unique ID for the ref
412 * @desc: unique userspace handle for ref
413 * @strong: strong ref count (debugging only if not locked)
414 * @weak: weak ref count (debugging only if not locked)
416 * Structure to hold ref count and ref id information. Since
417 * the actual ref can only be accessed with a lock, this structure
418 * is used to return information about the ref to callers of
419 * ref inc/dec functions.
421 struct binder_ref_data
{
429 * struct binder_ref - struct to track references on nodes
430 * @data: binder_ref_data containing id, handle, and current refcounts
431 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
432 * @rb_node_node: node for lookup by @node in proc's rb_tree
433 * @node_entry: list entry for node->refs list in target node
434 * (protected by @node->lock)
435 * @proc: binder_proc containing ref
436 * @node: binder_node of target node. When cleaning up a
437 * ref for deletion in binder_cleanup_ref, a non-NULL
438 * @node indicates the node must be freed
439 * @death: pointer to death notification (ref_death) if requested
440 * (protected by @node->lock)
442 * Structure to track references from procA to target node (on procB). This
443 * structure is unsafe to access without holding @proc->outer_lock.
446 /* Lookups needed: */
447 /* node + proc => ref (transaction) */
448 /* desc + proc => ref (transaction, inc/dec ref) */
449 /* node => refs + procs (proc exit) */
450 struct binder_ref_data data
;
451 struct rb_node rb_node_desc
;
452 struct rb_node rb_node_node
;
453 struct hlist_node node_entry
;
454 struct binder_proc
*proc
;
455 struct binder_node
*node
;
456 struct binder_ref_death
*death
;
459 enum binder_deferred_state
{
460 BINDER_DEFERRED_PUT_FILES
= 0x01,
461 BINDER_DEFERRED_FLUSH
= 0x02,
462 BINDER_DEFERRED_RELEASE
= 0x04,
466 * struct binder_proc - binder process bookkeeping
467 * @proc_node: element for binder_procs list
468 * @threads: rbtree of binder_threads in this proc
469 * (protected by @inner_lock)
470 * @nodes: rbtree of binder nodes associated with
471 * this proc ordered by node->ptr
472 * (protected by @inner_lock)
473 * @refs_by_desc: rbtree of refs ordered by ref->desc
474 * (protected by @outer_lock)
475 * @refs_by_node: rbtree of refs ordered by ref->node
476 * (protected by @outer_lock)
477 * @waiting_threads: threads currently waiting for proc work
478 * (protected by @inner_lock)
479 * @pid PID of group_leader of process
480 * (invariant after initialized)
481 * @tsk task_struct for group_leader of process
482 * (invariant after initialized)
483 * @files files_struct for process
484 * (protected by @files_lock)
485 * @files_lock mutex to protect @files
486 * @deferred_work_node: element for binder_deferred_list
487 * (protected by binder_deferred_lock)
488 * @deferred_work: bitmap of deferred work to perform
489 * (protected by binder_deferred_lock)
490 * @is_dead: process is dead and awaiting free
491 * when outstanding transactions are cleaned up
492 * (protected by @inner_lock)
493 * @todo: list of work for this process
494 * (protected by @inner_lock)
495 * @stats: per-process binder statistics
496 * (atomics, no lock needed)
497 * @delivered_death: list of delivered death notification
498 * (protected by @inner_lock)
499 * @max_threads: cap on number of binder threads
500 * (protected by @inner_lock)
501 * @requested_threads: number of binder threads requested but not
502 * yet started. In current implementation, can
504 * (protected by @inner_lock)
505 * @requested_threads_started: number binder threads started
506 * (protected by @inner_lock)
507 * @tmp_ref: temporary reference to indicate proc is in use
508 * (protected by @inner_lock)
509 * @default_priority: default scheduler priority
510 * (invariant after initialized)
511 * @debugfs_entry: debugfs node
512 * @alloc: binder allocator bookkeeping
513 * @context: binder_context for this proc
514 * (invariant after initialized)
515 * @inner_lock: can nest under outer_lock and/or node lock
516 * @outer_lock: no nesting under innor or node lock
517 * Lock order: 1) outer, 2) node, 3) inner
519 * Bookkeeping structure for binder processes
522 struct hlist_node proc_node
;
523 struct rb_root threads
;
524 struct rb_root nodes
;
525 struct rb_root refs_by_desc
;
526 struct rb_root refs_by_node
;
527 struct list_head waiting_threads
;
529 struct task_struct
*tsk
;
530 struct files_struct
*files
;
531 struct mutex files_lock
;
532 struct hlist_node deferred_work_node
;
536 struct list_head todo
;
537 struct binder_stats stats
;
538 struct list_head delivered_death
;
540 int requested_threads
;
541 int requested_threads_started
;
543 long default_priority
;
544 struct dentry
*debugfs_entry
;
545 struct binder_alloc alloc
;
546 struct binder_context
*context
;
547 spinlock_t inner_lock
;
548 spinlock_t outer_lock
;
552 BINDER_LOOPER_STATE_REGISTERED
= 0x01,
553 BINDER_LOOPER_STATE_ENTERED
= 0x02,
554 BINDER_LOOPER_STATE_EXITED
= 0x04,
555 BINDER_LOOPER_STATE_INVALID
= 0x08,
556 BINDER_LOOPER_STATE_WAITING
= 0x10,
557 BINDER_LOOPER_STATE_POLL
= 0x20,
561 * struct binder_thread - binder thread bookkeeping
562 * @proc: binder process for this thread
563 * (invariant after initialization)
564 * @rb_node: element for proc->threads rbtree
565 * (protected by @proc->inner_lock)
566 * @waiting_thread_node: element for @proc->waiting_threads list
567 * (protected by @proc->inner_lock)
568 * @pid: PID for this thread
569 * (invariant after initialization)
570 * @looper: bitmap of looping state
571 * (only accessed by this thread)
572 * @looper_needs_return: looping thread needs to exit driver
574 * @transaction_stack: stack of in-progress transactions for this thread
575 * (protected by @proc->inner_lock)
576 * @todo: list of work to do for this thread
577 * (protected by @proc->inner_lock)
578 * @process_todo: whether work in @todo should be processed
579 * (protected by @proc->inner_lock)
580 * @return_error: transaction errors reported by this thread
581 * (only accessed by this thread)
582 * @reply_error: transaction errors reported by target thread
583 * (protected by @proc->inner_lock)
584 * @wait: wait queue for thread work
585 * @stats: per-thread statistics
586 * (atomics, no lock needed)
587 * @tmp_ref: temporary reference to indicate thread is in use
588 * (atomic since @proc->inner_lock cannot
589 * always be acquired)
590 * @is_dead: thread is dead and awaiting free
591 * when outstanding transactions are cleaned up
592 * (protected by @proc->inner_lock)
594 * Bookkeeping structure for binder threads.
596 struct binder_thread
{
597 struct binder_proc
*proc
;
598 struct rb_node rb_node
;
599 struct list_head waiting_thread_node
;
601 int looper
; /* only modified by this thread */
602 bool looper_need_return
; /* can be written by other thread */
603 struct binder_transaction
*transaction_stack
;
604 struct list_head todo
;
606 struct binder_error return_error
;
607 struct binder_error reply_error
;
608 wait_queue_head_t wait
;
609 struct binder_stats stats
;
614 struct binder_transaction
{
616 struct binder_work work
;
617 struct binder_thread
*from
;
618 struct binder_transaction
*from_parent
;
619 struct binder_proc
*to_proc
;
620 struct binder_thread
*to_thread
;
621 struct binder_transaction
*to_parent
;
622 unsigned need_reply
:1;
623 /* unsigned is_dead:1; */ /* not used at the moment */
625 struct binder_buffer
*buffer
;
632 * @lock: protects @from, @to_proc, and @to_thread
634 * @from, @to_proc, and @to_thread can be set to NULL
635 * during thread teardown
641 * binder_proc_lock() - Acquire outer lock for given binder_proc
642 * @proc: struct binder_proc to acquire
644 * Acquires proc->outer_lock. Used to protect binder_ref
645 * structures associated with the given proc.
647 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
649 _binder_proc_lock(struct binder_proc
*proc
, int line
)
651 binder_debug(BINDER_DEBUG_SPINLOCKS
,
652 "%s: line=%d\n", __func__
, line
);
653 spin_lock(&proc
->outer_lock
);
657 * binder_proc_unlock() - Release spinlock for given binder_proc
658 * @proc: struct binder_proc to acquire
660 * Release lock acquired via binder_proc_lock()
662 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
664 _binder_proc_unlock(struct binder_proc
*proc
, int line
)
666 binder_debug(BINDER_DEBUG_SPINLOCKS
,
667 "%s: line=%d\n", __func__
, line
);
668 spin_unlock(&proc
->outer_lock
);
672 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
673 * @proc: struct binder_proc to acquire
675 * Acquires proc->inner_lock. Used to protect todo lists
677 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
679 _binder_inner_proc_lock(struct binder_proc
*proc
, int line
)
681 binder_debug(BINDER_DEBUG_SPINLOCKS
,
682 "%s: line=%d\n", __func__
, line
);
683 spin_lock(&proc
->inner_lock
);
687 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
688 * @proc: struct binder_proc to acquire
690 * Release lock acquired via binder_inner_proc_lock()
692 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
694 _binder_inner_proc_unlock(struct binder_proc
*proc
, int line
)
696 binder_debug(BINDER_DEBUG_SPINLOCKS
,
697 "%s: line=%d\n", __func__
, line
);
698 spin_unlock(&proc
->inner_lock
);
702 * binder_node_lock() - Acquire spinlock for given binder_node
703 * @node: struct binder_node to acquire
705 * Acquires node->lock. Used to protect binder_node fields
707 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
709 _binder_node_lock(struct binder_node
*node
, int line
)
711 binder_debug(BINDER_DEBUG_SPINLOCKS
,
712 "%s: line=%d\n", __func__
, line
);
713 spin_lock(&node
->lock
);
717 * binder_node_unlock() - Release spinlock for given binder_proc
718 * @node: struct binder_node to acquire
720 * Release lock acquired via binder_node_lock()
722 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
724 _binder_node_unlock(struct binder_node
*node
, int line
)
726 binder_debug(BINDER_DEBUG_SPINLOCKS
,
727 "%s: line=%d\n", __func__
, line
);
728 spin_unlock(&node
->lock
);
732 * binder_node_inner_lock() - Acquire node and inner locks
733 * @node: struct binder_node to acquire
735 * Acquires node->lock. If node->proc also acquires
736 * proc->inner_lock. Used to protect binder_node fields
738 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
740 _binder_node_inner_lock(struct binder_node
*node
, int line
)
742 binder_debug(BINDER_DEBUG_SPINLOCKS
,
743 "%s: line=%d\n", __func__
, line
);
744 spin_lock(&node
->lock
);
746 binder_inner_proc_lock(node
->proc
);
750 * binder_node_unlock() - Release node and inner locks
751 * @node: struct binder_node to acquire
753 * Release lock acquired via binder_node_lock()
755 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
757 _binder_node_inner_unlock(struct binder_node
*node
, int line
)
759 struct binder_proc
*proc
= node
->proc
;
761 binder_debug(BINDER_DEBUG_SPINLOCKS
,
762 "%s: line=%d\n", __func__
, line
);
764 binder_inner_proc_unlock(proc
);
765 spin_unlock(&node
->lock
);
768 static bool binder_worklist_empty_ilocked(struct list_head
*list
)
770 return list_empty(list
);
774 * binder_worklist_empty() - Check if no items on the work list
775 * @proc: binder_proc associated with list
776 * @list: list to check
778 * Return: true if there are no items on list, else false
780 static bool binder_worklist_empty(struct binder_proc
*proc
,
781 struct list_head
*list
)
785 binder_inner_proc_lock(proc
);
786 ret
= binder_worklist_empty_ilocked(list
);
787 binder_inner_proc_unlock(proc
);
792 * binder_enqueue_work_ilocked() - Add an item to the work list
793 * @work: struct binder_work to add to list
794 * @target_list: list to add work to
796 * Adds the work to the specified list. Asserts that work
797 * is not already on a list.
799 * Requires the proc->inner_lock to be held.
802 binder_enqueue_work_ilocked(struct binder_work
*work
,
803 struct list_head
*target_list
)
805 BUG_ON(target_list
== NULL
);
806 BUG_ON(work
->entry
.next
&& !list_empty(&work
->entry
));
807 list_add_tail(&work
->entry
, target_list
);
811 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
812 * @thread: thread to queue work to
813 * @work: struct binder_work to add to list
815 * Adds the work to the todo list of the thread. Doesn't set the process_todo
816 * flag, which means that (if it wasn't already set) the thread will go to
817 * sleep without handling this work when it calls read.
819 * Requires the proc->inner_lock to be held.
822 binder_enqueue_deferred_thread_work_ilocked(struct binder_thread
*thread
,
823 struct binder_work
*work
)
825 WARN_ON(!list_empty(&thread
->waiting_thread_node
));
826 binder_enqueue_work_ilocked(work
, &thread
->todo
);
830 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
831 * @thread: thread to queue work to
832 * @work: struct binder_work to add to list
834 * Adds the work to the todo list of the thread, and enables processing
837 * Requires the proc->inner_lock to be held.
840 binder_enqueue_thread_work_ilocked(struct binder_thread
*thread
,
841 struct binder_work
*work
)
843 WARN_ON(!list_empty(&thread
->waiting_thread_node
));
844 binder_enqueue_work_ilocked(work
, &thread
->todo
);
845 thread
->process_todo
= true;
849 * binder_enqueue_thread_work() - Add an item to the thread work list
850 * @thread: thread to queue work to
851 * @work: struct binder_work to add to list
853 * Adds the work to the todo list of the thread, and enables processing
857 binder_enqueue_thread_work(struct binder_thread
*thread
,
858 struct binder_work
*work
)
860 binder_inner_proc_lock(thread
->proc
);
861 binder_enqueue_thread_work_ilocked(thread
, work
);
862 binder_inner_proc_unlock(thread
->proc
);
866 binder_dequeue_work_ilocked(struct binder_work
*work
)
868 list_del_init(&work
->entry
);
872 * binder_dequeue_work() - Removes an item from the work list
873 * @proc: binder_proc associated with list
874 * @work: struct binder_work to remove from list
876 * Removes the specified work item from whatever list it is on.
877 * Can safely be called if work is not on any list.
880 binder_dequeue_work(struct binder_proc
*proc
, struct binder_work
*work
)
882 binder_inner_proc_lock(proc
);
883 binder_dequeue_work_ilocked(work
);
884 binder_inner_proc_unlock(proc
);
887 static struct binder_work
*binder_dequeue_work_head_ilocked(
888 struct list_head
*list
)
890 struct binder_work
*w
;
892 w
= list_first_entry_or_null(list
, struct binder_work
, entry
);
894 list_del_init(&w
->entry
);
899 * binder_dequeue_work_head() - Dequeues the item at head of list
900 * @proc: binder_proc associated with list
901 * @list: list to dequeue head
903 * Removes the head of the list if there are items on the list
905 * Return: pointer dequeued binder_work, NULL if list was empty
907 static struct binder_work
*binder_dequeue_work_head(
908 struct binder_proc
*proc
,
909 struct list_head
*list
)
911 struct binder_work
*w
;
913 binder_inner_proc_lock(proc
);
914 w
= binder_dequeue_work_head_ilocked(list
);
915 binder_inner_proc_unlock(proc
);
920 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
);
921 static void binder_free_thread(struct binder_thread
*thread
);
922 static void binder_free_proc(struct binder_proc
*proc
);
923 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
);
925 static int task_get_unused_fd_flags(struct binder_proc
*proc
, int flags
)
927 unsigned long rlim_cur
;
931 mutex_lock(&proc
->files_lock
);
932 if (proc
->files
== NULL
) {
936 if (!lock_task_sighand(proc
->tsk
, &irqs
)) {
940 rlim_cur
= task_rlimit(proc
->tsk
, RLIMIT_NOFILE
);
941 unlock_task_sighand(proc
->tsk
, &irqs
);
943 ret
= __alloc_fd(proc
->files
, 0, rlim_cur
, flags
);
945 mutex_unlock(&proc
->files_lock
);
950 * copied from fd_install
952 static void task_fd_install(
953 struct binder_proc
*proc
, unsigned int fd
, struct file
*file
)
955 mutex_lock(&proc
->files_lock
);
957 __fd_install(proc
->files
, fd
, file
);
958 mutex_unlock(&proc
->files_lock
);
962 * copied from sys_close
964 static long task_close_fd(struct binder_proc
*proc
, unsigned int fd
)
968 mutex_lock(&proc
->files_lock
);
969 if (proc
->files
== NULL
) {
973 retval
= __close_fd(proc
->files
, fd
);
974 /* can't restart close syscall because file table entry was cleared */
975 if (unlikely(retval
== -ERESTARTSYS
||
976 retval
== -ERESTARTNOINTR
||
977 retval
== -ERESTARTNOHAND
||
978 retval
== -ERESTART_RESTARTBLOCK
))
981 mutex_unlock(&proc
->files_lock
);
985 static bool binder_has_work_ilocked(struct binder_thread
*thread
,
988 return thread
->process_todo
||
989 thread
->looper_need_return
||
991 !binder_worklist_empty_ilocked(&thread
->proc
->todo
));
994 static bool binder_has_work(struct binder_thread
*thread
, bool do_proc_work
)
998 binder_inner_proc_lock(thread
->proc
);
999 has_work
= binder_has_work_ilocked(thread
, do_proc_work
);
1000 binder_inner_proc_unlock(thread
->proc
);
1005 static bool binder_available_for_proc_work_ilocked(struct binder_thread
*thread
)
1007 return !thread
->transaction_stack
&&
1008 binder_worklist_empty_ilocked(&thread
->todo
) &&
1009 (thread
->looper
& (BINDER_LOOPER_STATE_ENTERED
|
1010 BINDER_LOOPER_STATE_REGISTERED
));
1013 static void binder_wakeup_poll_threads_ilocked(struct binder_proc
*proc
,
1017 struct binder_thread
*thread
;
1019 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
1020 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
1021 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
&&
1022 binder_available_for_proc_work_ilocked(thread
)) {
1024 wake_up_interruptible_sync(&thread
->wait
);
1026 wake_up_interruptible(&thread
->wait
);
1032 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1033 * @proc: process to select a thread from
1035 * Note that calling this function moves the thread off the waiting_threads
1036 * list, so it can only be woken up by the caller of this function, or a
1037 * signal. Therefore, callers *should* always wake up the thread this function
1040 * Return: If there's a thread currently waiting for process work,
1041 * returns that thread. Otherwise returns NULL.
1043 static struct binder_thread
*
1044 binder_select_thread_ilocked(struct binder_proc
*proc
)
1046 struct binder_thread
*thread
;
1048 assert_spin_locked(&proc
->inner_lock
);
1049 thread
= list_first_entry_or_null(&proc
->waiting_threads
,
1050 struct binder_thread
,
1051 waiting_thread_node
);
1054 list_del_init(&thread
->waiting_thread_node
);
1060 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1061 * @proc: process to wake up a thread in
1062 * @thread: specific thread to wake-up (may be NULL)
1063 * @sync: whether to do a synchronous wake-up
1065 * This function wakes up a thread in the @proc process.
1066 * The caller may provide a specific thread to wake-up in
1067 * the @thread parameter. If @thread is NULL, this function
1068 * will wake up threads that have called poll().
1070 * Note that for this function to work as expected, callers
1071 * should first call binder_select_thread() to find a thread
1072 * to handle the work (if they don't have a thread already),
1073 * and pass the result into the @thread parameter.
1075 static void binder_wakeup_thread_ilocked(struct binder_proc
*proc
,
1076 struct binder_thread
*thread
,
1079 assert_spin_locked(&proc
->inner_lock
);
1083 wake_up_interruptible_sync(&thread
->wait
);
1085 wake_up_interruptible(&thread
->wait
);
1089 /* Didn't find a thread waiting for proc work; this can happen
1091 * 1. All threads are busy handling transactions
1092 * In that case, one of those threads should call back into
1093 * the kernel driver soon and pick up this work.
1094 * 2. Threads are using the (e)poll interface, in which case
1095 * they may be blocked on the waitqueue without having been
1096 * added to waiting_threads. For this case, we just iterate
1097 * over all threads not handling transaction work, and
1098 * wake them all up. We wake all because we don't know whether
1099 * a thread that called into (e)poll is handling non-binder
1102 binder_wakeup_poll_threads_ilocked(proc
, sync
);
1105 static void binder_wakeup_proc_ilocked(struct binder_proc
*proc
)
1107 struct binder_thread
*thread
= binder_select_thread_ilocked(proc
);
1109 binder_wakeup_thread_ilocked(proc
, thread
, /* sync = */false);
1112 static void binder_set_nice(long nice
)
1116 if (can_nice(current
, nice
)) {
1117 set_user_nice(current
, nice
);
1120 min_nice
= rlimit_to_nice(rlimit(RLIMIT_NICE
));
1121 binder_debug(BINDER_DEBUG_PRIORITY_CAP
,
1122 "%d: nice value %ld not allowed use %ld instead\n",
1123 current
->pid
, nice
, min_nice
);
1124 set_user_nice(current
, min_nice
);
1125 if (min_nice
<= MAX_NICE
)
1127 binder_user_error("%d RLIMIT_NICE not set\n", current
->pid
);
1130 static struct binder_node
*binder_get_node_ilocked(struct binder_proc
*proc
,
1131 binder_uintptr_t ptr
)
1133 struct rb_node
*n
= proc
->nodes
.rb_node
;
1134 struct binder_node
*node
;
1136 assert_spin_locked(&proc
->inner_lock
);
1139 node
= rb_entry(n
, struct binder_node
, rb_node
);
1141 if (ptr
< node
->ptr
)
1143 else if (ptr
> node
->ptr
)
1147 * take an implicit weak reference
1148 * to ensure node stays alive until
1149 * call to binder_put_node()
1151 binder_inc_node_tmpref_ilocked(node
);
1158 static struct binder_node
*binder_get_node(struct binder_proc
*proc
,
1159 binder_uintptr_t ptr
)
1161 struct binder_node
*node
;
1163 binder_inner_proc_lock(proc
);
1164 node
= binder_get_node_ilocked(proc
, ptr
);
1165 binder_inner_proc_unlock(proc
);
1169 static struct binder_node
*binder_init_node_ilocked(
1170 struct binder_proc
*proc
,
1171 struct binder_node
*new_node
,
1172 struct flat_binder_object
*fp
)
1174 struct rb_node
**p
= &proc
->nodes
.rb_node
;
1175 struct rb_node
*parent
= NULL
;
1176 struct binder_node
*node
;
1177 binder_uintptr_t ptr
= fp
? fp
->binder
: 0;
1178 binder_uintptr_t cookie
= fp
? fp
->cookie
: 0;
1179 __u32 flags
= fp
? fp
->flags
: 0;
1181 assert_spin_locked(&proc
->inner_lock
);
1186 node
= rb_entry(parent
, struct binder_node
, rb_node
);
1188 if (ptr
< node
->ptr
)
1190 else if (ptr
> node
->ptr
)
1191 p
= &(*p
)->rb_right
;
1194 * A matching node is already in
1195 * the rb tree. Abandon the init
1198 binder_inc_node_tmpref_ilocked(node
);
1203 binder_stats_created(BINDER_STAT_NODE
);
1205 rb_link_node(&node
->rb_node
, parent
, p
);
1206 rb_insert_color(&node
->rb_node
, &proc
->nodes
);
1207 node
->debug_id
= atomic_inc_return(&binder_last_id
);
1210 node
->cookie
= cookie
;
1211 node
->work
.type
= BINDER_WORK_NODE
;
1212 node
->min_priority
= flags
& FLAT_BINDER_FLAG_PRIORITY_MASK
;
1213 node
->accept_fds
= !!(flags
& FLAT_BINDER_FLAG_ACCEPTS_FDS
);
1214 spin_lock_init(&node
->lock
);
1215 INIT_LIST_HEAD(&node
->work
.entry
);
1216 INIT_LIST_HEAD(&node
->async_todo
);
1217 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1218 "%d:%d node %d u%016llx c%016llx created\n",
1219 proc
->pid
, current
->pid
, node
->debug_id
,
1220 (u64
)node
->ptr
, (u64
)node
->cookie
);
1225 static struct binder_node
*binder_new_node(struct binder_proc
*proc
,
1226 struct flat_binder_object
*fp
)
1228 struct binder_node
*node
;
1229 struct binder_node
*new_node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1233 binder_inner_proc_lock(proc
);
1234 node
= binder_init_node_ilocked(proc
, new_node
, fp
);
1235 binder_inner_proc_unlock(proc
);
1236 if (node
!= new_node
)
1238 * The node was already added by another thread
1245 static void binder_free_node(struct binder_node
*node
)
1248 binder_stats_deleted(BINDER_STAT_NODE
);
1251 static int binder_inc_node_nilocked(struct binder_node
*node
, int strong
,
1253 struct list_head
*target_list
)
1255 struct binder_proc
*proc
= node
->proc
;
1257 assert_spin_locked(&node
->lock
);
1259 assert_spin_locked(&proc
->inner_lock
);
1262 if (target_list
== NULL
&&
1263 node
->internal_strong_refs
== 0 &&
1265 node
== node
->proc
->context
->binder_context_mgr_node
&&
1266 node
->has_strong_ref
)) {
1267 pr_err("invalid inc strong node for %d\n",
1271 node
->internal_strong_refs
++;
1273 node
->local_strong_refs
++;
1274 if (!node
->has_strong_ref
&& target_list
) {
1275 struct binder_thread
*thread
= container_of(target_list
,
1276 struct binder_thread
, todo
);
1277 binder_dequeue_work_ilocked(&node
->work
);
1278 BUG_ON(&thread
->todo
!= target_list
);
1279 binder_enqueue_deferred_thread_work_ilocked(thread
,
1284 node
->local_weak_refs
++;
1285 if (!node
->has_weak_ref
&& list_empty(&node
->work
.entry
)) {
1286 if (target_list
== NULL
) {
1287 pr_err("invalid inc weak node for %d\n",
1294 binder_enqueue_work_ilocked(&node
->work
, target_list
);
1300 static int binder_inc_node(struct binder_node
*node
, int strong
, int internal
,
1301 struct list_head
*target_list
)
1305 binder_node_inner_lock(node
);
1306 ret
= binder_inc_node_nilocked(node
, strong
, internal
, target_list
);
1307 binder_node_inner_unlock(node
);
1312 static bool binder_dec_node_nilocked(struct binder_node
*node
,
1313 int strong
, int internal
)
1315 struct binder_proc
*proc
= node
->proc
;
1317 assert_spin_locked(&node
->lock
);
1319 assert_spin_locked(&proc
->inner_lock
);
1322 node
->internal_strong_refs
--;
1324 node
->local_strong_refs
--;
1325 if (node
->local_strong_refs
|| node
->internal_strong_refs
)
1329 node
->local_weak_refs
--;
1330 if (node
->local_weak_refs
|| node
->tmp_refs
||
1331 !hlist_empty(&node
->refs
))
1335 if (proc
&& (node
->has_strong_ref
|| node
->has_weak_ref
)) {
1336 if (list_empty(&node
->work
.entry
)) {
1337 binder_enqueue_work_ilocked(&node
->work
, &proc
->todo
);
1338 binder_wakeup_proc_ilocked(proc
);
1341 if (hlist_empty(&node
->refs
) && !node
->local_strong_refs
&&
1342 !node
->local_weak_refs
&& !node
->tmp_refs
) {
1344 binder_dequeue_work_ilocked(&node
->work
);
1345 rb_erase(&node
->rb_node
, &proc
->nodes
);
1346 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1347 "refless node %d deleted\n",
1350 BUG_ON(!list_empty(&node
->work
.entry
));
1351 spin_lock(&binder_dead_nodes_lock
);
1353 * tmp_refs could have changed so
1356 if (node
->tmp_refs
) {
1357 spin_unlock(&binder_dead_nodes_lock
);
1360 hlist_del(&node
->dead_node
);
1361 spin_unlock(&binder_dead_nodes_lock
);
1362 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1363 "dead node %d deleted\n",
1372 static void binder_dec_node(struct binder_node
*node
, int strong
, int internal
)
1376 binder_node_inner_lock(node
);
1377 free_node
= binder_dec_node_nilocked(node
, strong
, internal
);
1378 binder_node_inner_unlock(node
);
1380 binder_free_node(node
);
1383 static void binder_inc_node_tmpref_ilocked(struct binder_node
*node
)
1386 * No call to binder_inc_node() is needed since we
1387 * don't need to inform userspace of any changes to
1394 * binder_inc_node_tmpref() - take a temporary reference on node
1395 * @node: node to reference
1397 * Take reference on node to prevent the node from being freed
1398 * while referenced only by a local variable. The inner lock is
1399 * needed to serialize with the node work on the queue (which
1400 * isn't needed after the node is dead). If the node is dead
1401 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1402 * node->tmp_refs against dead-node-only cases where the node
1403 * lock cannot be acquired (eg traversing the dead node list to
1406 static void binder_inc_node_tmpref(struct binder_node
*node
)
1408 binder_node_lock(node
);
1410 binder_inner_proc_lock(node
->proc
);
1412 spin_lock(&binder_dead_nodes_lock
);
1413 binder_inc_node_tmpref_ilocked(node
);
1415 binder_inner_proc_unlock(node
->proc
);
1417 spin_unlock(&binder_dead_nodes_lock
);
1418 binder_node_unlock(node
);
1422 * binder_dec_node_tmpref() - remove a temporary reference on node
1423 * @node: node to reference
1425 * Release temporary reference on node taken via binder_inc_node_tmpref()
1427 static void binder_dec_node_tmpref(struct binder_node
*node
)
1431 binder_node_inner_lock(node
);
1433 spin_lock(&binder_dead_nodes_lock
);
1435 BUG_ON(node
->tmp_refs
< 0);
1437 spin_unlock(&binder_dead_nodes_lock
);
1439 * Call binder_dec_node() to check if all refcounts are 0
1440 * and cleanup is needed. Calling with strong=0 and internal=1
1441 * causes no actual reference to be released in binder_dec_node().
1442 * If that changes, a change is needed here too.
1444 free_node
= binder_dec_node_nilocked(node
, 0, 1);
1445 binder_node_inner_unlock(node
);
1447 binder_free_node(node
);
1450 static void binder_put_node(struct binder_node
*node
)
1452 binder_dec_node_tmpref(node
);
1455 static struct binder_ref
*binder_get_ref_olocked(struct binder_proc
*proc
,
1456 u32 desc
, bool need_strong_ref
)
1458 struct rb_node
*n
= proc
->refs_by_desc
.rb_node
;
1459 struct binder_ref
*ref
;
1462 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1464 if (desc
< ref
->data
.desc
) {
1466 } else if (desc
> ref
->data
.desc
) {
1468 } else if (need_strong_ref
&& !ref
->data
.strong
) {
1469 binder_user_error("tried to use weak ref as strong ref\n");
1479 * binder_get_ref_for_node_olocked() - get the ref associated with given node
1480 * @proc: binder_proc that owns the ref
1481 * @node: binder_node of target
1482 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1484 * Look up the ref for the given node and return it if it exists
1486 * If it doesn't exist and the caller provides a newly allocated
1487 * ref, initialize the fields of the newly allocated ref and insert
1488 * into the given proc rb_trees and node refs list.
1490 * Return: the ref for node. It is possible that another thread
1491 * allocated/initialized the ref first in which case the
1492 * returned ref would be different than the passed-in
1493 * new_ref. new_ref must be kfree'd by the caller in
1496 static struct binder_ref
*binder_get_ref_for_node_olocked(
1497 struct binder_proc
*proc
,
1498 struct binder_node
*node
,
1499 struct binder_ref
*new_ref
)
1501 struct binder_context
*context
= proc
->context
;
1502 struct rb_node
**p
= &proc
->refs_by_node
.rb_node
;
1503 struct rb_node
*parent
= NULL
;
1504 struct binder_ref
*ref
;
1509 ref
= rb_entry(parent
, struct binder_ref
, rb_node_node
);
1511 if (node
< ref
->node
)
1513 else if (node
> ref
->node
)
1514 p
= &(*p
)->rb_right
;
1521 binder_stats_created(BINDER_STAT_REF
);
1522 new_ref
->data
.debug_id
= atomic_inc_return(&binder_last_id
);
1523 new_ref
->proc
= proc
;
1524 new_ref
->node
= node
;
1525 rb_link_node(&new_ref
->rb_node_node
, parent
, p
);
1526 rb_insert_color(&new_ref
->rb_node_node
, &proc
->refs_by_node
);
1528 new_ref
->data
.desc
= (node
== context
->binder_context_mgr_node
) ? 0 : 1;
1529 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
1530 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
1531 if (ref
->data
.desc
> new_ref
->data
.desc
)
1533 new_ref
->data
.desc
= ref
->data
.desc
+ 1;
1536 p
= &proc
->refs_by_desc
.rb_node
;
1539 ref
= rb_entry(parent
, struct binder_ref
, rb_node_desc
);
1541 if (new_ref
->data
.desc
< ref
->data
.desc
)
1543 else if (new_ref
->data
.desc
> ref
->data
.desc
)
1544 p
= &(*p
)->rb_right
;
1548 rb_link_node(&new_ref
->rb_node_desc
, parent
, p
);
1549 rb_insert_color(&new_ref
->rb_node_desc
, &proc
->refs_by_desc
);
1551 binder_node_lock(node
);
1552 hlist_add_head(&new_ref
->node_entry
, &node
->refs
);
1554 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1555 "%d new ref %d desc %d for node %d\n",
1556 proc
->pid
, new_ref
->data
.debug_id
, new_ref
->data
.desc
,
1558 binder_node_unlock(node
);
1562 static void binder_cleanup_ref_olocked(struct binder_ref
*ref
)
1564 bool delete_node
= false;
1566 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
1567 "%d delete ref %d desc %d for node %d\n",
1568 ref
->proc
->pid
, ref
->data
.debug_id
, ref
->data
.desc
,
1569 ref
->node
->debug_id
);
1571 rb_erase(&ref
->rb_node_desc
, &ref
->proc
->refs_by_desc
);
1572 rb_erase(&ref
->rb_node_node
, &ref
->proc
->refs_by_node
);
1574 binder_node_inner_lock(ref
->node
);
1575 if (ref
->data
.strong
)
1576 binder_dec_node_nilocked(ref
->node
, 1, 1);
1578 hlist_del(&ref
->node_entry
);
1579 delete_node
= binder_dec_node_nilocked(ref
->node
, 0, 1);
1580 binder_node_inner_unlock(ref
->node
);
1582 * Clear ref->node unless we want the caller to free the node
1586 * The caller uses ref->node to determine
1587 * whether the node needs to be freed. Clear
1588 * it since the node is still alive.
1594 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
1595 "%d delete ref %d desc %d has death notification\n",
1596 ref
->proc
->pid
, ref
->data
.debug_id
,
1598 binder_dequeue_work(ref
->proc
, &ref
->death
->work
);
1599 binder_stats_deleted(BINDER_STAT_DEATH
);
1601 binder_stats_deleted(BINDER_STAT_REF
);
1605 * binder_inc_ref_olocked() - increment the ref for given handle
1606 * @ref: ref to be incremented
1607 * @strong: if true, strong increment, else weak
1608 * @target_list: list to queue node work on
1610 * Increment the ref. @ref->proc->outer_lock must be held on entry
1612 * Return: 0, if successful, else errno
1614 static int binder_inc_ref_olocked(struct binder_ref
*ref
, int strong
,
1615 struct list_head
*target_list
)
1620 if (ref
->data
.strong
== 0) {
1621 ret
= binder_inc_node(ref
->node
, 1, 1, target_list
);
1627 if (ref
->data
.weak
== 0) {
1628 ret
= binder_inc_node(ref
->node
, 0, 1, target_list
);
1638 * binder_dec_ref() - dec the ref for given handle
1639 * @ref: ref to be decremented
1640 * @strong: if true, strong decrement, else weak
1642 * Decrement the ref.
1644 * Return: true if ref is cleaned up and ready to be freed
1646 static bool binder_dec_ref_olocked(struct binder_ref
*ref
, int strong
)
1649 if (ref
->data
.strong
== 0) {
1650 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1651 ref
->proc
->pid
, ref
->data
.debug_id
,
1652 ref
->data
.desc
, ref
->data
.strong
,
1657 if (ref
->data
.strong
== 0)
1658 binder_dec_node(ref
->node
, strong
, 1);
1660 if (ref
->data
.weak
== 0) {
1661 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1662 ref
->proc
->pid
, ref
->data
.debug_id
,
1663 ref
->data
.desc
, ref
->data
.strong
,
1669 if (ref
->data
.strong
== 0 && ref
->data
.weak
== 0) {
1670 binder_cleanup_ref_olocked(ref
);
1677 * binder_get_node_from_ref() - get the node from the given proc/desc
1678 * @proc: proc containing the ref
1679 * @desc: the handle associated with the ref
1680 * @need_strong_ref: if true, only return node if ref is strong
1681 * @rdata: the id/refcount data for the ref
1683 * Given a proc and ref handle, return the associated binder_node
1685 * Return: a binder_node or NULL if not found or not strong when strong required
1687 static struct binder_node
*binder_get_node_from_ref(
1688 struct binder_proc
*proc
,
1689 u32 desc
, bool need_strong_ref
,
1690 struct binder_ref_data
*rdata
)
1692 struct binder_node
*node
;
1693 struct binder_ref
*ref
;
1695 binder_proc_lock(proc
);
1696 ref
= binder_get_ref_olocked(proc
, desc
, need_strong_ref
);
1701 * Take an implicit reference on the node to ensure
1702 * it stays alive until the call to binder_put_node()
1704 binder_inc_node_tmpref(node
);
1707 binder_proc_unlock(proc
);
1712 binder_proc_unlock(proc
);
1717 * binder_free_ref() - free the binder_ref
1720 * Free the binder_ref. Free the binder_node indicated by ref->node
1721 * (if non-NULL) and the binder_ref_death indicated by ref->death.
1723 static void binder_free_ref(struct binder_ref
*ref
)
1726 binder_free_node(ref
->node
);
1732 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1733 * @proc: proc containing the ref
1734 * @desc: the handle associated with the ref
1735 * @increment: true=inc reference, false=dec reference
1736 * @strong: true=strong reference, false=weak reference
1737 * @rdata: the id/refcount data for the ref
1739 * Given a proc and ref handle, increment or decrement the ref
1740 * according to "increment" arg.
1742 * Return: 0 if successful, else errno
1744 static int binder_update_ref_for_handle(struct binder_proc
*proc
,
1745 uint32_t desc
, bool increment
, bool strong
,
1746 struct binder_ref_data
*rdata
)
1749 struct binder_ref
*ref
;
1750 bool delete_ref
= false;
1752 binder_proc_lock(proc
);
1753 ref
= binder_get_ref_olocked(proc
, desc
, strong
);
1759 ret
= binder_inc_ref_olocked(ref
, strong
, NULL
);
1761 delete_ref
= binder_dec_ref_olocked(ref
, strong
);
1765 binder_proc_unlock(proc
);
1768 binder_free_ref(ref
);
1772 binder_proc_unlock(proc
);
1777 * binder_dec_ref_for_handle() - dec the ref for given handle
1778 * @proc: proc containing the ref
1779 * @desc: the handle associated with the ref
1780 * @strong: true=strong reference, false=weak reference
1781 * @rdata: the id/refcount data for the ref
1783 * Just calls binder_update_ref_for_handle() to decrement the ref.
1785 * Return: 0 if successful, else errno
1787 static int binder_dec_ref_for_handle(struct binder_proc
*proc
,
1788 uint32_t desc
, bool strong
, struct binder_ref_data
*rdata
)
1790 return binder_update_ref_for_handle(proc
, desc
, false, strong
, rdata
);
1795 * binder_inc_ref_for_node() - increment the ref for given proc/node
1796 * @proc: proc containing the ref
1797 * @node: target node
1798 * @strong: true=strong reference, false=weak reference
1799 * @target_list: worklist to use if node is incremented
1800 * @rdata: the id/refcount data for the ref
1802 * Given a proc and node, increment the ref. Create the ref if it
1803 * doesn't already exist
1805 * Return: 0 if successful, else errno
1807 static int binder_inc_ref_for_node(struct binder_proc
*proc
,
1808 struct binder_node
*node
,
1810 struct list_head
*target_list
,
1811 struct binder_ref_data
*rdata
)
1813 struct binder_ref
*ref
;
1814 struct binder_ref
*new_ref
= NULL
;
1817 binder_proc_lock(proc
);
1818 ref
= binder_get_ref_for_node_olocked(proc
, node
, NULL
);
1820 binder_proc_unlock(proc
);
1821 new_ref
= kzalloc(sizeof(*ref
), GFP_KERNEL
);
1824 binder_proc_lock(proc
);
1825 ref
= binder_get_ref_for_node_olocked(proc
, node
, new_ref
);
1827 ret
= binder_inc_ref_olocked(ref
, strong
, target_list
);
1829 binder_proc_unlock(proc
);
1830 if (new_ref
&& ref
!= new_ref
)
1832 * Another thread created the ref first so
1833 * free the one we allocated
1839 static void binder_pop_transaction_ilocked(struct binder_thread
*target_thread
,
1840 struct binder_transaction
*t
)
1842 BUG_ON(!target_thread
);
1843 assert_spin_locked(&target_thread
->proc
->inner_lock
);
1844 BUG_ON(target_thread
->transaction_stack
!= t
);
1845 BUG_ON(target_thread
->transaction_stack
->from
!= target_thread
);
1846 target_thread
->transaction_stack
=
1847 target_thread
->transaction_stack
->from_parent
;
1852 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1853 * @thread: thread to decrement
1855 * A thread needs to be kept alive while being used to create or
1856 * handle a transaction. binder_get_txn_from() is used to safely
1857 * extract t->from from a binder_transaction and keep the thread
1858 * indicated by t->from from being freed. When done with that
1859 * binder_thread, this function is called to decrement the
1860 * tmp_ref and free if appropriate (thread has been released
1861 * and no transaction being processed by the driver)
1863 static void binder_thread_dec_tmpref(struct binder_thread
*thread
)
1866 * atomic is used to protect the counter value while
1867 * it cannot reach zero or thread->is_dead is false
1869 binder_inner_proc_lock(thread
->proc
);
1870 atomic_dec(&thread
->tmp_ref
);
1871 if (thread
->is_dead
&& !atomic_read(&thread
->tmp_ref
)) {
1872 binder_inner_proc_unlock(thread
->proc
);
1873 binder_free_thread(thread
);
1876 binder_inner_proc_unlock(thread
->proc
);
1880 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1881 * @proc: proc to decrement
1883 * A binder_proc needs to be kept alive while being used to create or
1884 * handle a transaction. proc->tmp_ref is incremented when
1885 * creating a new transaction or the binder_proc is currently in-use
1886 * by threads that are being released. When done with the binder_proc,
1887 * this function is called to decrement the counter and free the
1888 * proc if appropriate (proc has been released, all threads have
1889 * been released and not currenly in-use to process a transaction).
1891 static void binder_proc_dec_tmpref(struct binder_proc
*proc
)
1893 binder_inner_proc_lock(proc
);
1895 if (proc
->is_dead
&& RB_EMPTY_ROOT(&proc
->threads
) &&
1897 binder_inner_proc_unlock(proc
);
1898 binder_free_proc(proc
);
1901 binder_inner_proc_unlock(proc
);
1905 * binder_get_txn_from() - safely extract the "from" thread in transaction
1906 * @t: binder transaction for t->from
1908 * Atomically return the "from" thread and increment the tmp_ref
1909 * count for the thread to ensure it stays alive until
1910 * binder_thread_dec_tmpref() is called.
1912 * Return: the value of t->from
1914 static struct binder_thread
*binder_get_txn_from(
1915 struct binder_transaction
*t
)
1917 struct binder_thread
*from
;
1919 spin_lock(&t
->lock
);
1922 atomic_inc(&from
->tmp_ref
);
1923 spin_unlock(&t
->lock
);
1928 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1929 * @t: binder transaction for t->from
1931 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1932 * to guarantee that the thread cannot be released while operating on it.
1933 * The caller must call binder_inner_proc_unlock() to release the inner lock
1934 * as well as call binder_dec_thread_txn() to release the reference.
1936 * Return: the value of t->from
1938 static struct binder_thread
*binder_get_txn_from_and_acq_inner(
1939 struct binder_transaction
*t
)
1941 struct binder_thread
*from
;
1943 from
= binder_get_txn_from(t
);
1946 binder_inner_proc_lock(from
->proc
);
1948 BUG_ON(from
!= t
->from
);
1951 binder_inner_proc_unlock(from
->proc
);
1952 binder_thread_dec_tmpref(from
);
1956 static void binder_free_transaction(struct binder_transaction
*t
)
1958 struct binder_proc
*target_proc
= t
->to_proc
;
1961 binder_inner_proc_lock(target_proc
);
1963 t
->buffer
->transaction
= NULL
;
1964 binder_inner_proc_unlock(target_proc
);
1967 * If the transaction has no target_proc, then
1968 * t->buffer->transaction has already been cleared.
1971 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
1974 static void binder_send_failed_reply(struct binder_transaction
*t
,
1975 uint32_t error_code
)
1977 struct binder_thread
*target_thread
;
1978 struct binder_transaction
*next
;
1980 BUG_ON(t
->flags
& TF_ONE_WAY
);
1982 target_thread
= binder_get_txn_from_and_acq_inner(t
);
1983 if (target_thread
) {
1984 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
1985 "send failed reply for transaction %d to %d:%d\n",
1987 target_thread
->proc
->pid
,
1988 target_thread
->pid
);
1990 binder_pop_transaction_ilocked(target_thread
, t
);
1991 if (target_thread
->reply_error
.cmd
== BR_OK
) {
1992 target_thread
->reply_error
.cmd
= error_code
;
1993 binder_enqueue_thread_work_ilocked(
1995 &target_thread
->reply_error
.work
);
1996 wake_up_interruptible(&target_thread
->wait
);
1999 * Cannot get here for normal operation, but
2000 * we can if multiple synchronous transactions
2001 * are sent without blocking for responses.
2002 * Just ignore the 2nd error in this case.
2004 pr_warn("Unexpected reply error: %u\n",
2005 target_thread
->reply_error
.cmd
);
2007 binder_inner_proc_unlock(target_thread
->proc
);
2008 binder_thread_dec_tmpref(target_thread
);
2009 binder_free_transaction(t
);
2012 next
= t
->from_parent
;
2014 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
2015 "send failed reply for transaction %d, target dead\n",
2018 binder_free_transaction(t
);
2020 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2021 "reply failed, no target thread at root\n");
2025 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
2026 "reply failed, no target thread -- retry %d\n",
2032 * binder_cleanup_transaction() - cleans up undelivered transaction
2033 * @t: transaction that needs to be cleaned up
2034 * @reason: reason the transaction wasn't delivered
2035 * @error_code: error to return to caller (if synchronous call)
2037 static void binder_cleanup_transaction(struct binder_transaction
*t
,
2039 uint32_t error_code
)
2041 if (t
->buffer
->target_node
&& !(t
->flags
& TF_ONE_WAY
)) {
2042 binder_send_failed_reply(t
, error_code
);
2044 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
2045 "undelivered transaction %d, %s\n",
2046 t
->debug_id
, reason
);
2047 binder_free_transaction(t
);
2052 * binder_validate_object() - checks for a valid metadata object in a buffer.
2053 * @buffer: binder_buffer that we're parsing.
2054 * @offset: offset in the buffer at which to validate an object.
2056 * Return: If there's a valid metadata object at @offset in @buffer, the
2057 * size of that object. Otherwise, it returns zero.
2059 static size_t binder_validate_object(struct binder_buffer
*buffer
, u64 offset
)
2061 /* Check if we can read a header first */
2062 struct binder_object_header
*hdr
;
2063 size_t object_size
= 0;
2065 if (buffer
->data_size
< sizeof(*hdr
) ||
2066 offset
> buffer
->data_size
- sizeof(*hdr
) ||
2067 !IS_ALIGNED(offset
, sizeof(u32
)))
2070 /* Ok, now see if we can read a complete object. */
2071 hdr
= (struct binder_object_header
*)(buffer
->data
+ offset
);
2072 switch (hdr
->type
) {
2073 case BINDER_TYPE_BINDER
:
2074 case BINDER_TYPE_WEAK_BINDER
:
2075 case BINDER_TYPE_HANDLE
:
2076 case BINDER_TYPE_WEAK_HANDLE
:
2077 object_size
= sizeof(struct flat_binder_object
);
2079 case BINDER_TYPE_FD
:
2080 object_size
= sizeof(struct binder_fd_object
);
2082 case BINDER_TYPE_PTR
:
2083 object_size
= sizeof(struct binder_buffer_object
);
2085 case BINDER_TYPE_FDA
:
2086 object_size
= sizeof(struct binder_fd_array_object
);
2091 if (offset
<= buffer
->data_size
- object_size
&&
2092 buffer
->data_size
>= object_size
)
2099 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2100 * @b: binder_buffer containing the object
2101 * @index: index in offset array at which the binder_buffer_object is
2103 * @start: points to the start of the offset array
2104 * @num_valid: the number of valid offsets in the offset array
2106 * Return: If @index is within the valid range of the offset array
2107 * described by @start and @num_valid, and if there's a valid
2108 * binder_buffer_object at the offset found in index @index
2109 * of the offset array, that object is returned. Otherwise,
2110 * %NULL is returned.
2111 * Note that the offset found in index @index itself is not
2112 * verified; this function assumes that @num_valid elements
2113 * from @start were previously verified to have valid offsets.
2115 static struct binder_buffer_object
*binder_validate_ptr(struct binder_buffer
*b
,
2116 binder_size_t index
,
2117 binder_size_t
*start
,
2118 binder_size_t num_valid
)
2120 struct binder_buffer_object
*buffer_obj
;
2121 binder_size_t
*offp
;
2123 if (index
>= num_valid
)
2126 offp
= start
+ index
;
2127 buffer_obj
= (struct binder_buffer_object
*)(b
->data
+ *offp
);
2128 if (buffer_obj
->hdr
.type
!= BINDER_TYPE_PTR
)
2135 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2136 * @b: transaction buffer
2137 * @objects_start start of objects buffer
2138 * @buffer: binder_buffer_object in which to fix up
2139 * @offset: start offset in @buffer to fix up
2140 * @last_obj: last binder_buffer_object that we fixed up in
2141 * @last_min_offset: minimum fixup offset in @last_obj
2143 * Return: %true if a fixup in buffer @buffer at offset @offset is
2146 * For safety reasons, we only allow fixups inside a buffer to happen
2147 * at increasing offsets; additionally, we only allow fixup on the last
2148 * buffer object that was verified, or one of its parents.
2150 * Example of what is allowed:
2153 * B (parent = A, offset = 0)
2154 * C (parent = A, offset = 16)
2155 * D (parent = C, offset = 0)
2156 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2158 * Examples of what is not allowed:
2160 * Decreasing offsets within the same parent:
2162 * C (parent = A, offset = 16)
2163 * B (parent = A, offset = 0) // decreasing offset within A
2165 * Referring to a parent that wasn't the last object or any of its parents:
2167 * B (parent = A, offset = 0)
2168 * C (parent = A, offset = 0)
2169 * C (parent = A, offset = 16)
2170 * D (parent = B, offset = 0) // B is not A or any of A's parents
2172 static bool binder_validate_fixup(struct binder_buffer
*b
,
2173 binder_size_t
*objects_start
,
2174 struct binder_buffer_object
*buffer
,
2175 binder_size_t fixup_offset
,
2176 struct binder_buffer_object
*last_obj
,
2177 binder_size_t last_min_offset
)
2180 /* Nothing to fix up in */
2184 while (last_obj
!= buffer
) {
2186 * Safe to retrieve the parent of last_obj, since it
2187 * was already previously verified by the driver.
2189 if ((last_obj
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
) == 0)
2191 last_min_offset
= last_obj
->parent_offset
+ sizeof(uintptr_t);
2192 last_obj
= (struct binder_buffer_object
*)
2193 (b
->data
+ *(objects_start
+ last_obj
->parent
));
2195 return (fixup_offset
>= last_min_offset
);
2198 static void binder_transaction_buffer_release(struct binder_proc
*proc
,
2199 struct binder_buffer
*buffer
,
2200 binder_size_t
*failed_at
)
2202 binder_size_t
*offp
, *off_start
, *off_end
;
2203 int debug_id
= buffer
->debug_id
;
2205 binder_debug(BINDER_DEBUG_TRANSACTION
,
2206 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
2207 proc
->pid
, buffer
->debug_id
,
2208 buffer
->data_size
, buffer
->offsets_size
, failed_at
);
2210 if (buffer
->target_node
)
2211 binder_dec_node(buffer
->target_node
, 1, 0);
2213 off_start
= (binder_size_t
*)(buffer
->data
+
2214 ALIGN(buffer
->data_size
, sizeof(void *)));
2216 off_end
= failed_at
;
2218 off_end
= (void *)off_start
+ buffer
->offsets_size
;
2219 for (offp
= off_start
; offp
< off_end
; offp
++) {
2220 struct binder_object_header
*hdr
;
2221 size_t object_size
= binder_validate_object(buffer
, *offp
);
2223 if (object_size
== 0) {
2224 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
2225 debug_id
, (u64
)*offp
, buffer
->data_size
);
2228 hdr
= (struct binder_object_header
*)(buffer
->data
+ *offp
);
2229 switch (hdr
->type
) {
2230 case BINDER_TYPE_BINDER
:
2231 case BINDER_TYPE_WEAK_BINDER
: {
2232 struct flat_binder_object
*fp
;
2233 struct binder_node
*node
;
2235 fp
= to_flat_binder_object(hdr
);
2236 node
= binder_get_node(proc
, fp
->binder
);
2238 pr_err("transaction release %d bad node %016llx\n",
2239 debug_id
, (u64
)fp
->binder
);
2242 binder_debug(BINDER_DEBUG_TRANSACTION
,
2243 " node %d u%016llx\n",
2244 node
->debug_id
, (u64
)node
->ptr
);
2245 binder_dec_node(node
, hdr
->type
== BINDER_TYPE_BINDER
,
2247 binder_put_node(node
);
2249 case BINDER_TYPE_HANDLE
:
2250 case BINDER_TYPE_WEAK_HANDLE
: {
2251 struct flat_binder_object
*fp
;
2252 struct binder_ref_data rdata
;
2255 fp
= to_flat_binder_object(hdr
);
2256 ret
= binder_dec_ref_for_handle(proc
, fp
->handle
,
2257 hdr
->type
== BINDER_TYPE_HANDLE
, &rdata
);
2260 pr_err("transaction release %d bad handle %d, ret = %d\n",
2261 debug_id
, fp
->handle
, ret
);
2264 binder_debug(BINDER_DEBUG_TRANSACTION
,
2265 " ref %d desc %d\n",
2266 rdata
.debug_id
, rdata
.desc
);
2269 case BINDER_TYPE_FD
: {
2270 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
2272 binder_debug(BINDER_DEBUG_TRANSACTION
,
2273 " fd %d\n", fp
->fd
);
2275 task_close_fd(proc
, fp
->fd
);
2277 case BINDER_TYPE_PTR
:
2279 * Nothing to do here, this will get cleaned up when the
2280 * transaction buffer gets freed
2283 case BINDER_TYPE_FDA
: {
2284 struct binder_fd_array_object
*fda
;
2285 struct binder_buffer_object
*parent
;
2286 uintptr_t parent_buffer
;
2289 binder_size_t fd_buf_size
;
2291 fda
= to_binder_fd_array_object(hdr
);
2292 parent
= binder_validate_ptr(buffer
, fda
->parent
,
2296 pr_err("transaction release %d bad parent offset\n",
2301 * Since the parent was already fixed up, convert it
2302 * back to kernel address space to access it
2304 parent_buffer
= parent
->buffer
-
2305 binder_alloc_get_user_buffer_offset(
2308 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2309 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2310 pr_err("transaction release %d invalid number of fds (%lld)\n",
2311 debug_id
, (u64
)fda
->num_fds
);
2314 if (fd_buf_size
> parent
->length
||
2315 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2316 /* No space for all file descriptors here. */
2317 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2318 debug_id
, (u64
)fda
->num_fds
);
2321 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2322 for (fd_index
= 0; fd_index
< fda
->num_fds
; fd_index
++)
2323 task_close_fd(proc
, fd_array
[fd_index
]);
2326 pr_err("transaction release %d bad object type %x\n",
2327 debug_id
, hdr
->type
);
2333 static int binder_translate_binder(struct flat_binder_object
*fp
,
2334 struct binder_transaction
*t
,
2335 struct binder_thread
*thread
)
2337 struct binder_node
*node
;
2338 struct binder_proc
*proc
= thread
->proc
;
2339 struct binder_proc
*target_proc
= t
->to_proc
;
2340 struct binder_ref_data rdata
;
2343 node
= binder_get_node(proc
, fp
->binder
);
2345 node
= binder_new_node(proc
, fp
);
2349 if (fp
->cookie
!= node
->cookie
) {
2350 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2351 proc
->pid
, thread
->pid
, (u64
)fp
->binder
,
2352 node
->debug_id
, (u64
)fp
->cookie
,
2357 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2362 ret
= binder_inc_ref_for_node(target_proc
, node
,
2363 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2364 &thread
->todo
, &rdata
);
2368 if (fp
->hdr
.type
== BINDER_TYPE_BINDER
)
2369 fp
->hdr
.type
= BINDER_TYPE_HANDLE
;
2371 fp
->hdr
.type
= BINDER_TYPE_WEAK_HANDLE
;
2373 fp
->handle
= rdata
.desc
;
2376 trace_binder_transaction_node_to_ref(t
, node
, &rdata
);
2377 binder_debug(BINDER_DEBUG_TRANSACTION
,
2378 " node %d u%016llx -> ref %d desc %d\n",
2379 node
->debug_id
, (u64
)node
->ptr
,
2380 rdata
.debug_id
, rdata
.desc
);
2382 binder_put_node(node
);
2386 static int binder_translate_handle(struct flat_binder_object
*fp
,
2387 struct binder_transaction
*t
,
2388 struct binder_thread
*thread
)
2390 struct binder_proc
*proc
= thread
->proc
;
2391 struct binder_proc
*target_proc
= t
->to_proc
;
2392 struct binder_node
*node
;
2393 struct binder_ref_data src_rdata
;
2396 node
= binder_get_node_from_ref(proc
, fp
->handle
,
2397 fp
->hdr
.type
== BINDER_TYPE_HANDLE
, &src_rdata
);
2399 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2400 proc
->pid
, thread
->pid
, fp
->handle
);
2403 if (security_binder_transfer_binder(proc
->tsk
, target_proc
->tsk
)) {
2408 binder_node_lock(node
);
2409 if (node
->proc
== target_proc
) {
2410 if (fp
->hdr
.type
== BINDER_TYPE_HANDLE
)
2411 fp
->hdr
.type
= BINDER_TYPE_BINDER
;
2413 fp
->hdr
.type
= BINDER_TYPE_WEAK_BINDER
;
2414 fp
->binder
= node
->ptr
;
2415 fp
->cookie
= node
->cookie
;
2417 binder_inner_proc_lock(node
->proc
);
2418 binder_inc_node_nilocked(node
,
2419 fp
->hdr
.type
== BINDER_TYPE_BINDER
,
2422 binder_inner_proc_unlock(node
->proc
);
2423 trace_binder_transaction_ref_to_node(t
, node
, &src_rdata
);
2424 binder_debug(BINDER_DEBUG_TRANSACTION
,
2425 " ref %d desc %d -> node %d u%016llx\n",
2426 src_rdata
.debug_id
, src_rdata
.desc
, node
->debug_id
,
2428 binder_node_unlock(node
);
2430 struct binder_ref_data dest_rdata
;
2432 binder_node_unlock(node
);
2433 ret
= binder_inc_ref_for_node(target_proc
, node
,
2434 fp
->hdr
.type
== BINDER_TYPE_HANDLE
,
2440 fp
->handle
= dest_rdata
.desc
;
2442 trace_binder_transaction_ref_to_ref(t
, node
, &src_rdata
,
2444 binder_debug(BINDER_DEBUG_TRANSACTION
,
2445 " ref %d desc %d -> ref %d desc %d (node %d)\n",
2446 src_rdata
.debug_id
, src_rdata
.desc
,
2447 dest_rdata
.debug_id
, dest_rdata
.desc
,
2451 binder_put_node(node
);
2455 static int binder_translate_fd(int fd
,
2456 struct binder_transaction
*t
,
2457 struct binder_thread
*thread
,
2458 struct binder_transaction
*in_reply_to
)
2460 struct binder_proc
*proc
= thread
->proc
;
2461 struct binder_proc
*target_proc
= t
->to_proc
;
2465 bool target_allows_fd
;
2468 target_allows_fd
= !!(in_reply_to
->flags
& TF_ACCEPT_FDS
);
2470 target_allows_fd
= t
->buffer
->target_node
->accept_fds
;
2471 if (!target_allows_fd
) {
2472 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2473 proc
->pid
, thread
->pid
,
2474 in_reply_to
? "reply" : "transaction",
2477 goto err_fd_not_accepted
;
2482 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2483 proc
->pid
, thread
->pid
, fd
);
2487 ret
= security_binder_transfer_file(proc
->tsk
, target_proc
->tsk
, file
);
2493 target_fd
= task_get_unused_fd_flags(target_proc
, O_CLOEXEC
);
2494 if (target_fd
< 0) {
2496 goto err_get_unused_fd
;
2498 task_fd_install(target_proc
, target_fd
, file
);
2499 trace_binder_transaction_fd(t
, fd
, target_fd
);
2500 binder_debug(BINDER_DEBUG_TRANSACTION
, " fd %d -> %d\n",
2509 err_fd_not_accepted
:
2513 static int binder_translate_fd_array(struct binder_fd_array_object
*fda
,
2514 struct binder_buffer_object
*parent
,
2515 struct binder_transaction
*t
,
2516 struct binder_thread
*thread
,
2517 struct binder_transaction
*in_reply_to
)
2519 binder_size_t fdi
, fd_buf_size
, num_installed_fds
;
2521 uintptr_t parent_buffer
;
2523 struct binder_proc
*proc
= thread
->proc
;
2524 struct binder_proc
*target_proc
= t
->to_proc
;
2526 fd_buf_size
= sizeof(u32
) * fda
->num_fds
;
2527 if (fda
->num_fds
>= SIZE_MAX
/ sizeof(u32
)) {
2528 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2529 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2532 if (fd_buf_size
> parent
->length
||
2533 fda
->parent_offset
> parent
->length
- fd_buf_size
) {
2534 /* No space for all file descriptors here. */
2535 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2536 proc
->pid
, thread
->pid
, (u64
)fda
->num_fds
);
2540 * Since the parent was already fixed up, convert it
2541 * back to the kernel address space to access it
2543 parent_buffer
= parent
->buffer
-
2544 binder_alloc_get_user_buffer_offset(&target_proc
->alloc
);
2545 fd_array
= (u32
*)(parent_buffer
+ (uintptr_t)fda
->parent_offset
);
2546 if (!IS_ALIGNED((unsigned long)fd_array
, sizeof(u32
))) {
2547 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2548 proc
->pid
, thread
->pid
);
2551 for (fdi
= 0; fdi
< fda
->num_fds
; fdi
++) {
2552 target_fd
= binder_translate_fd(fd_array
[fdi
], t
, thread
,
2555 goto err_translate_fd_failed
;
2556 fd_array
[fdi
] = target_fd
;
2560 err_translate_fd_failed
:
2562 * Failed to allocate fd or security error, free fds
2565 num_installed_fds
= fdi
;
2566 for (fdi
= 0; fdi
< num_installed_fds
; fdi
++)
2567 task_close_fd(target_proc
, fd_array
[fdi
]);
2571 static int binder_fixup_parent(struct binder_transaction
*t
,
2572 struct binder_thread
*thread
,
2573 struct binder_buffer_object
*bp
,
2574 binder_size_t
*off_start
,
2575 binder_size_t num_valid
,
2576 struct binder_buffer_object
*last_fixup_obj
,
2577 binder_size_t last_fixup_min_off
)
2579 struct binder_buffer_object
*parent
;
2581 struct binder_buffer
*b
= t
->buffer
;
2582 struct binder_proc
*proc
= thread
->proc
;
2583 struct binder_proc
*target_proc
= t
->to_proc
;
2585 if (!(bp
->flags
& BINDER_BUFFER_FLAG_HAS_PARENT
))
2588 parent
= binder_validate_ptr(b
, bp
->parent
, off_start
, num_valid
);
2590 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2591 proc
->pid
, thread
->pid
);
2595 if (!binder_validate_fixup(b
, off_start
,
2596 parent
, bp
->parent_offset
,
2598 last_fixup_min_off
)) {
2599 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2600 proc
->pid
, thread
->pid
);
2604 if (parent
->length
< sizeof(binder_uintptr_t
) ||
2605 bp
->parent_offset
> parent
->length
- sizeof(binder_uintptr_t
)) {
2606 /* No space for a pointer here! */
2607 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2608 proc
->pid
, thread
->pid
);
2611 parent_buffer
= (u8
*)((uintptr_t)parent
->buffer
-
2612 binder_alloc_get_user_buffer_offset(
2613 &target_proc
->alloc
));
2614 *(binder_uintptr_t
*)(parent_buffer
+ bp
->parent_offset
) = bp
->buffer
;
2620 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2621 * @t: transaction to send
2622 * @proc: process to send the transaction to
2623 * @thread: thread in @proc to send the transaction to (may be NULL)
2625 * This function queues a transaction to the specified process. It will try
2626 * to find a thread in the target process to handle the transaction and
2627 * wake it up. If no thread is found, the work is queued to the proc
2630 * If the @thread parameter is not NULL, the transaction is always queued
2631 * to the waitlist of that specific thread.
2633 * Return: true if the transactions was successfully queued
2634 * false if the target process or thread is dead
2636 static bool binder_proc_transaction(struct binder_transaction
*t
,
2637 struct binder_proc
*proc
,
2638 struct binder_thread
*thread
)
2640 struct binder_node
*node
= t
->buffer
->target_node
;
2641 bool oneway
= !!(t
->flags
& TF_ONE_WAY
);
2642 bool pending_async
= false;
2645 binder_node_lock(node
);
2648 if (node
->has_async_transaction
) {
2649 pending_async
= true;
2651 node
->has_async_transaction
= true;
2655 binder_inner_proc_lock(proc
);
2657 if (proc
->is_dead
|| (thread
&& thread
->is_dead
)) {
2658 binder_inner_proc_unlock(proc
);
2659 binder_node_unlock(node
);
2663 if (!thread
&& !pending_async
)
2664 thread
= binder_select_thread_ilocked(proc
);
2667 binder_enqueue_thread_work_ilocked(thread
, &t
->work
);
2668 else if (!pending_async
)
2669 binder_enqueue_work_ilocked(&t
->work
, &proc
->todo
);
2671 binder_enqueue_work_ilocked(&t
->work
, &node
->async_todo
);
2674 binder_wakeup_thread_ilocked(proc
, thread
, !oneway
/* sync */);
2676 binder_inner_proc_unlock(proc
);
2677 binder_node_unlock(node
);
2683 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2684 * @node: struct binder_node for which to get refs
2685 * @proc: returns @node->proc if valid
2686 * @error: if no @proc then returns BR_DEAD_REPLY
2688 * User-space normally keeps the node alive when creating a transaction
2689 * since it has a reference to the target. The local strong ref keeps it
2690 * alive if the sending process dies before the target process processes
2691 * the transaction. If the source process is malicious or has a reference
2692 * counting bug, relying on the local strong ref can fail.
2694 * Since user-space can cause the local strong ref to go away, we also take
2695 * a tmpref on the node to ensure it survives while we are constructing
2696 * the transaction. We also need a tmpref on the proc while we are
2697 * constructing the transaction, so we take that here as well.
2699 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2700 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2701 * target proc has died, @error is set to BR_DEAD_REPLY
2703 static struct binder_node
*binder_get_node_refs_for_txn(
2704 struct binder_node
*node
,
2705 struct binder_proc
**procp
,
2708 struct binder_node
*target_node
= NULL
;
2710 binder_node_inner_lock(node
);
2713 binder_inc_node_nilocked(node
, 1, 0, NULL
);
2714 binder_inc_node_tmpref_ilocked(node
);
2715 node
->proc
->tmp_ref
++;
2716 *procp
= node
->proc
;
2718 *error
= BR_DEAD_REPLY
;
2719 binder_node_inner_unlock(node
);
2724 static void binder_transaction(struct binder_proc
*proc
,
2725 struct binder_thread
*thread
,
2726 struct binder_transaction_data
*tr
, int reply
,
2727 binder_size_t extra_buffers_size
)
2730 struct binder_transaction
*t
;
2731 struct binder_work
*w
;
2732 struct binder_work
*tcomplete
;
2733 binder_size_t
*offp
, *off_end
, *off_start
;
2734 binder_size_t off_min
;
2735 u8
*sg_bufp
, *sg_buf_end
;
2736 struct binder_proc
*target_proc
= NULL
;
2737 struct binder_thread
*target_thread
= NULL
;
2738 struct binder_node
*target_node
= NULL
;
2739 struct binder_transaction
*in_reply_to
= NULL
;
2740 struct binder_transaction_log_entry
*e
;
2741 uint32_t return_error
= 0;
2742 uint32_t return_error_param
= 0;
2743 uint32_t return_error_line
= 0;
2744 struct binder_buffer_object
*last_fixup_obj
= NULL
;
2745 binder_size_t last_fixup_min_off
= 0;
2746 struct binder_context
*context
= proc
->context
;
2747 int t_debug_id
= atomic_inc_return(&binder_last_id
);
2749 e
= binder_transaction_log_add(&binder_transaction_log
);
2750 e
->debug_id
= t_debug_id
;
2751 e
->call_type
= reply
? 2 : !!(tr
->flags
& TF_ONE_WAY
);
2752 e
->from_proc
= proc
->pid
;
2753 e
->from_thread
= thread
->pid
;
2754 e
->target_handle
= tr
->target
.handle
;
2755 e
->data_size
= tr
->data_size
;
2756 e
->offsets_size
= tr
->offsets_size
;
2757 e
->context_name
= proc
->context
->name
;
2760 binder_inner_proc_lock(proc
);
2761 in_reply_to
= thread
->transaction_stack
;
2762 if (in_reply_to
== NULL
) {
2763 binder_inner_proc_unlock(proc
);
2764 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2765 proc
->pid
, thread
->pid
);
2766 return_error
= BR_FAILED_REPLY
;
2767 return_error_param
= -EPROTO
;
2768 return_error_line
= __LINE__
;
2769 goto err_empty_call_stack
;
2771 if (in_reply_to
->to_thread
!= thread
) {
2772 spin_lock(&in_reply_to
->lock
);
2773 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2774 proc
->pid
, thread
->pid
, in_reply_to
->debug_id
,
2775 in_reply_to
->to_proc
?
2776 in_reply_to
->to_proc
->pid
: 0,
2777 in_reply_to
->to_thread
?
2778 in_reply_to
->to_thread
->pid
: 0);
2779 spin_unlock(&in_reply_to
->lock
);
2780 binder_inner_proc_unlock(proc
);
2781 return_error
= BR_FAILED_REPLY
;
2782 return_error_param
= -EPROTO
;
2783 return_error_line
= __LINE__
;
2785 goto err_bad_call_stack
;
2787 thread
->transaction_stack
= in_reply_to
->to_parent
;
2788 binder_inner_proc_unlock(proc
);
2789 binder_set_nice(in_reply_to
->saved_priority
);
2790 target_thread
= binder_get_txn_from_and_acq_inner(in_reply_to
);
2791 if (target_thread
== NULL
) {
2792 return_error
= BR_DEAD_REPLY
;
2793 return_error_line
= __LINE__
;
2794 goto err_dead_binder
;
2796 if (target_thread
->transaction_stack
!= in_reply_to
) {
2797 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2798 proc
->pid
, thread
->pid
,
2799 target_thread
->transaction_stack
?
2800 target_thread
->transaction_stack
->debug_id
: 0,
2801 in_reply_to
->debug_id
);
2802 binder_inner_proc_unlock(target_thread
->proc
);
2803 return_error
= BR_FAILED_REPLY
;
2804 return_error_param
= -EPROTO
;
2805 return_error_line
= __LINE__
;
2807 target_thread
= NULL
;
2808 goto err_dead_binder
;
2810 target_proc
= target_thread
->proc
;
2811 target_proc
->tmp_ref
++;
2812 binder_inner_proc_unlock(target_thread
->proc
);
2814 if (tr
->target
.handle
) {
2815 struct binder_ref
*ref
;
2818 * There must already be a strong ref
2819 * on this node. If so, do a strong
2820 * increment on the node to ensure it
2821 * stays alive until the transaction is
2824 binder_proc_lock(proc
);
2825 ref
= binder_get_ref_olocked(proc
, tr
->target
.handle
,
2828 target_node
= binder_get_node_refs_for_txn(
2829 ref
->node
, &target_proc
,
2832 binder_user_error("%d:%d got transaction to invalid handle\n",
2833 proc
->pid
, thread
->pid
);
2834 return_error
= BR_FAILED_REPLY
;
2836 binder_proc_unlock(proc
);
2838 mutex_lock(&context
->context_mgr_node_lock
);
2839 target_node
= context
->binder_context_mgr_node
;
2841 target_node
= binder_get_node_refs_for_txn(
2842 target_node
, &target_proc
,
2845 return_error
= BR_DEAD_REPLY
;
2846 mutex_unlock(&context
->context_mgr_node_lock
);
2847 if (target_node
&& target_proc
->pid
== proc
->pid
) {
2848 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
2849 proc
->pid
, thread
->pid
);
2850 return_error
= BR_FAILED_REPLY
;
2851 return_error_param
= -EINVAL
;
2852 return_error_line
= __LINE__
;
2853 goto err_invalid_target_handle
;
2858 * return_error is set above
2860 return_error_param
= -EINVAL
;
2861 return_error_line
= __LINE__
;
2862 goto err_dead_binder
;
2864 e
->to_node
= target_node
->debug_id
;
2865 if (security_binder_transaction(proc
->tsk
,
2866 target_proc
->tsk
) < 0) {
2867 return_error
= BR_FAILED_REPLY
;
2868 return_error_param
= -EPERM
;
2869 return_error_line
= __LINE__
;
2870 goto err_invalid_target_handle
;
2872 binder_inner_proc_lock(proc
);
2874 w
= list_first_entry_or_null(&thread
->todo
,
2875 struct binder_work
, entry
);
2876 if (!(tr
->flags
& TF_ONE_WAY
) && w
&&
2877 w
->type
== BINDER_WORK_TRANSACTION
) {
2879 * Do not allow new outgoing transaction from a
2880 * thread that has a transaction at the head of
2881 * its todo list. Only need to check the head
2882 * because binder_select_thread_ilocked picks a
2883 * thread from proc->waiting_threads to enqueue
2884 * the transaction, and nothing is queued to the
2885 * todo list while the thread is on waiting_threads.
2887 binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
2888 proc
->pid
, thread
->pid
);
2889 binder_inner_proc_unlock(proc
);
2890 return_error
= BR_FAILED_REPLY
;
2891 return_error_param
= -EPROTO
;
2892 return_error_line
= __LINE__
;
2893 goto err_bad_todo_list
;
2896 if (!(tr
->flags
& TF_ONE_WAY
) && thread
->transaction_stack
) {
2897 struct binder_transaction
*tmp
;
2899 tmp
= thread
->transaction_stack
;
2900 if (tmp
->to_thread
!= thread
) {
2901 spin_lock(&tmp
->lock
);
2902 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
2903 proc
->pid
, thread
->pid
, tmp
->debug_id
,
2904 tmp
->to_proc
? tmp
->to_proc
->pid
: 0,
2906 tmp
->to_thread
->pid
: 0);
2907 spin_unlock(&tmp
->lock
);
2908 binder_inner_proc_unlock(proc
);
2909 return_error
= BR_FAILED_REPLY
;
2910 return_error_param
= -EPROTO
;
2911 return_error_line
= __LINE__
;
2912 goto err_bad_call_stack
;
2915 struct binder_thread
*from
;
2917 spin_lock(&tmp
->lock
);
2919 if (from
&& from
->proc
== target_proc
) {
2920 atomic_inc(&from
->tmp_ref
);
2921 target_thread
= from
;
2922 spin_unlock(&tmp
->lock
);
2925 spin_unlock(&tmp
->lock
);
2926 tmp
= tmp
->from_parent
;
2929 binder_inner_proc_unlock(proc
);
2932 e
->to_thread
= target_thread
->pid
;
2933 e
->to_proc
= target_proc
->pid
;
2935 /* TODO: reuse incoming transaction for reply */
2936 t
= kzalloc(sizeof(*t
), GFP_KERNEL
);
2938 return_error
= BR_FAILED_REPLY
;
2939 return_error_param
= -ENOMEM
;
2940 return_error_line
= __LINE__
;
2941 goto err_alloc_t_failed
;
2943 binder_stats_created(BINDER_STAT_TRANSACTION
);
2944 spin_lock_init(&t
->lock
);
2946 tcomplete
= kzalloc(sizeof(*tcomplete
), GFP_KERNEL
);
2947 if (tcomplete
== NULL
) {
2948 return_error
= BR_FAILED_REPLY
;
2949 return_error_param
= -ENOMEM
;
2950 return_error_line
= __LINE__
;
2951 goto err_alloc_tcomplete_failed
;
2953 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE
);
2955 t
->debug_id
= t_debug_id
;
2958 binder_debug(BINDER_DEBUG_TRANSACTION
,
2959 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
2960 proc
->pid
, thread
->pid
, t
->debug_id
,
2961 target_proc
->pid
, target_thread
->pid
,
2962 (u64
)tr
->data
.ptr
.buffer
,
2963 (u64
)tr
->data
.ptr
.offsets
,
2964 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
2965 (u64
)extra_buffers_size
);
2967 binder_debug(BINDER_DEBUG_TRANSACTION
,
2968 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
2969 proc
->pid
, thread
->pid
, t
->debug_id
,
2970 target_proc
->pid
, target_node
->debug_id
,
2971 (u64
)tr
->data
.ptr
.buffer
,
2972 (u64
)tr
->data
.ptr
.offsets
,
2973 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
2974 (u64
)extra_buffers_size
);
2976 if (!reply
&& !(tr
->flags
& TF_ONE_WAY
))
2980 t
->sender_euid
= task_euid(proc
->tsk
);
2981 t
->to_proc
= target_proc
;
2982 t
->to_thread
= target_thread
;
2984 t
->flags
= tr
->flags
;
2985 t
->priority
= task_nice(current
);
2987 trace_binder_transaction(reply
, t
, target_node
);
2989 t
->buffer
= binder_alloc_new_buf(&target_proc
->alloc
, tr
->data_size
,
2990 tr
->offsets_size
, extra_buffers_size
,
2991 !reply
&& (t
->flags
& TF_ONE_WAY
));
2992 if (IS_ERR(t
->buffer
)) {
2994 * -ESRCH indicates VMA cleared. The target is dying.
2996 return_error_param
= PTR_ERR(t
->buffer
);
2997 return_error
= return_error_param
== -ESRCH
?
2998 BR_DEAD_REPLY
: BR_FAILED_REPLY
;
2999 return_error_line
= __LINE__
;
3001 goto err_binder_alloc_buf_failed
;
3003 t
->buffer
->debug_id
= t
->debug_id
;
3004 t
->buffer
->transaction
= t
;
3005 t
->buffer
->target_node
= target_node
;
3006 trace_binder_transaction_alloc_buf(t
->buffer
);
3007 off_start
= (binder_size_t
*)(t
->buffer
->data
+
3008 ALIGN(tr
->data_size
, sizeof(void *)));
3011 if (copy_from_user(t
->buffer
->data
, (const void __user
*)(uintptr_t)
3012 tr
->data
.ptr
.buffer
, tr
->data_size
)) {
3013 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3014 proc
->pid
, thread
->pid
);
3015 return_error
= BR_FAILED_REPLY
;
3016 return_error_param
= -EFAULT
;
3017 return_error_line
= __LINE__
;
3018 goto err_copy_data_failed
;
3020 if (copy_from_user(offp
, (const void __user
*)(uintptr_t)
3021 tr
->data
.ptr
.offsets
, tr
->offsets_size
)) {
3022 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3023 proc
->pid
, thread
->pid
);
3024 return_error
= BR_FAILED_REPLY
;
3025 return_error_param
= -EFAULT
;
3026 return_error_line
= __LINE__
;
3027 goto err_copy_data_failed
;
3029 if (!IS_ALIGNED(tr
->offsets_size
, sizeof(binder_size_t
))) {
3030 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3031 proc
->pid
, thread
->pid
, (u64
)tr
->offsets_size
);
3032 return_error
= BR_FAILED_REPLY
;
3033 return_error_param
= -EINVAL
;
3034 return_error_line
= __LINE__
;
3035 goto err_bad_offset
;
3037 if (!IS_ALIGNED(extra_buffers_size
, sizeof(u64
))) {
3038 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3039 proc
->pid
, thread
->pid
,
3040 (u64
)extra_buffers_size
);
3041 return_error
= BR_FAILED_REPLY
;
3042 return_error_param
= -EINVAL
;
3043 return_error_line
= __LINE__
;
3044 goto err_bad_offset
;
3046 off_end
= (void *)off_start
+ tr
->offsets_size
;
3047 sg_bufp
= (u8
*)(PTR_ALIGN(off_end
, sizeof(void *)));
3048 sg_buf_end
= sg_bufp
+ extra_buffers_size
;
3050 for (; offp
< off_end
; offp
++) {
3051 struct binder_object_header
*hdr
;
3052 size_t object_size
= binder_validate_object(t
->buffer
, *offp
);
3054 if (object_size
== 0 || *offp
< off_min
) {
3055 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
3056 proc
->pid
, thread
->pid
, (u64
)*offp
,
3058 (u64
)t
->buffer
->data_size
);
3059 return_error
= BR_FAILED_REPLY
;
3060 return_error_param
= -EINVAL
;
3061 return_error_line
= __LINE__
;
3062 goto err_bad_offset
;
3065 hdr
= (struct binder_object_header
*)(t
->buffer
->data
+ *offp
);
3066 off_min
= *offp
+ object_size
;
3067 switch (hdr
->type
) {
3068 case BINDER_TYPE_BINDER
:
3069 case BINDER_TYPE_WEAK_BINDER
: {
3070 struct flat_binder_object
*fp
;
3072 fp
= to_flat_binder_object(hdr
);
3073 ret
= binder_translate_binder(fp
, t
, thread
);
3075 return_error
= BR_FAILED_REPLY
;
3076 return_error_param
= ret
;
3077 return_error_line
= __LINE__
;
3078 goto err_translate_failed
;
3081 case BINDER_TYPE_HANDLE
:
3082 case BINDER_TYPE_WEAK_HANDLE
: {
3083 struct flat_binder_object
*fp
;
3085 fp
= to_flat_binder_object(hdr
);
3086 ret
= binder_translate_handle(fp
, t
, thread
);
3088 return_error
= BR_FAILED_REPLY
;
3089 return_error_param
= ret
;
3090 return_error_line
= __LINE__
;
3091 goto err_translate_failed
;
3095 case BINDER_TYPE_FD
: {
3096 struct binder_fd_object
*fp
= to_binder_fd_object(hdr
);
3097 int target_fd
= binder_translate_fd(fp
->fd
, t
, thread
,
3100 if (target_fd
< 0) {
3101 return_error
= BR_FAILED_REPLY
;
3102 return_error_param
= target_fd
;
3103 return_error_line
= __LINE__
;
3104 goto err_translate_failed
;
3109 case BINDER_TYPE_FDA
: {
3110 struct binder_fd_array_object
*fda
=
3111 to_binder_fd_array_object(hdr
);
3112 struct binder_buffer_object
*parent
=
3113 binder_validate_ptr(t
->buffer
, fda
->parent
,
3117 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3118 proc
->pid
, thread
->pid
);
3119 return_error
= BR_FAILED_REPLY
;
3120 return_error_param
= -EINVAL
;
3121 return_error_line
= __LINE__
;
3122 goto err_bad_parent
;
3124 if (!binder_validate_fixup(t
->buffer
, off_start
,
3125 parent
, fda
->parent_offset
,
3127 last_fixup_min_off
)) {
3128 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3129 proc
->pid
, thread
->pid
);
3130 return_error
= BR_FAILED_REPLY
;
3131 return_error_param
= -EINVAL
;
3132 return_error_line
= __LINE__
;
3133 goto err_bad_parent
;
3135 ret
= binder_translate_fd_array(fda
, parent
, t
, thread
,
3138 return_error
= BR_FAILED_REPLY
;
3139 return_error_param
= ret
;
3140 return_error_line
= __LINE__
;
3141 goto err_translate_failed
;
3143 last_fixup_obj
= parent
;
3144 last_fixup_min_off
=
3145 fda
->parent_offset
+ sizeof(u32
) * fda
->num_fds
;
3147 case BINDER_TYPE_PTR
: {
3148 struct binder_buffer_object
*bp
=
3149 to_binder_buffer_object(hdr
);
3150 size_t buf_left
= sg_buf_end
- sg_bufp
;
3152 if (bp
->length
> buf_left
) {
3153 binder_user_error("%d:%d got transaction with too large buffer\n",
3154 proc
->pid
, thread
->pid
);
3155 return_error
= BR_FAILED_REPLY
;
3156 return_error_param
= -EINVAL
;
3157 return_error_line
= __LINE__
;
3158 goto err_bad_offset
;
3160 if (copy_from_user(sg_bufp
,
3161 (const void __user
*)(uintptr_t)
3162 bp
->buffer
, bp
->length
)) {
3163 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3164 proc
->pid
, thread
->pid
);
3165 return_error_param
= -EFAULT
;
3166 return_error
= BR_FAILED_REPLY
;
3167 return_error_line
= __LINE__
;
3168 goto err_copy_data_failed
;
3170 /* Fixup buffer pointer to target proc address space */
3171 bp
->buffer
= (uintptr_t)sg_bufp
+
3172 binder_alloc_get_user_buffer_offset(
3173 &target_proc
->alloc
);
3174 sg_bufp
+= ALIGN(bp
->length
, sizeof(u64
));
3176 ret
= binder_fixup_parent(t
, thread
, bp
, off_start
,
3179 last_fixup_min_off
);
3181 return_error
= BR_FAILED_REPLY
;
3182 return_error_param
= ret
;
3183 return_error_line
= __LINE__
;
3184 goto err_translate_failed
;
3186 last_fixup_obj
= bp
;
3187 last_fixup_min_off
= 0;
3190 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3191 proc
->pid
, thread
->pid
, hdr
->type
);
3192 return_error
= BR_FAILED_REPLY
;
3193 return_error_param
= -EINVAL
;
3194 return_error_line
= __LINE__
;
3195 goto err_bad_object_type
;
3198 tcomplete
->type
= BINDER_WORK_TRANSACTION_COMPLETE
;
3199 t
->work
.type
= BINDER_WORK_TRANSACTION
;
3202 binder_enqueue_thread_work(thread
, tcomplete
);
3203 binder_inner_proc_lock(target_proc
);
3204 if (target_thread
->is_dead
) {
3205 binder_inner_proc_unlock(target_proc
);
3206 goto err_dead_proc_or_thread
;
3208 BUG_ON(t
->buffer
->async_transaction
!= 0);
3209 binder_pop_transaction_ilocked(target_thread
, in_reply_to
);
3210 binder_enqueue_thread_work_ilocked(target_thread
, &t
->work
);
3211 binder_inner_proc_unlock(target_proc
);
3212 wake_up_interruptible_sync(&target_thread
->wait
);
3213 binder_free_transaction(in_reply_to
);
3214 } else if (!(t
->flags
& TF_ONE_WAY
)) {
3215 BUG_ON(t
->buffer
->async_transaction
!= 0);
3216 binder_inner_proc_lock(proc
);
3218 * Defer the TRANSACTION_COMPLETE, so we don't return to
3219 * userspace immediately; this allows the target process to
3220 * immediately start processing this transaction, reducing
3221 * latency. We will then return the TRANSACTION_COMPLETE when
3222 * the target replies (or there is an error).
3224 binder_enqueue_deferred_thread_work_ilocked(thread
, tcomplete
);
3226 t
->from_parent
= thread
->transaction_stack
;
3227 thread
->transaction_stack
= t
;
3228 binder_inner_proc_unlock(proc
);
3229 if (!binder_proc_transaction(t
, target_proc
, target_thread
)) {
3230 binder_inner_proc_lock(proc
);
3231 binder_pop_transaction_ilocked(thread
, t
);
3232 binder_inner_proc_unlock(proc
);
3233 goto err_dead_proc_or_thread
;
3236 BUG_ON(target_node
== NULL
);
3237 BUG_ON(t
->buffer
->async_transaction
!= 1);
3238 binder_enqueue_thread_work(thread
, tcomplete
);
3239 if (!binder_proc_transaction(t
, target_proc
, NULL
))
3240 goto err_dead_proc_or_thread
;
3243 binder_thread_dec_tmpref(target_thread
);
3244 binder_proc_dec_tmpref(target_proc
);
3246 binder_dec_node_tmpref(target_node
);
3248 * write barrier to synchronize with initialization
3252 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3255 err_dead_proc_or_thread
:
3256 return_error
= BR_DEAD_REPLY
;
3257 return_error_line
= __LINE__
;
3258 binder_dequeue_work(proc
, tcomplete
);
3259 err_translate_failed
:
3260 err_bad_object_type
:
3263 err_copy_data_failed
:
3264 trace_binder_transaction_failed_buffer_release(t
->buffer
);
3265 binder_transaction_buffer_release(target_proc
, t
->buffer
, offp
);
3267 binder_dec_node_tmpref(target_node
);
3269 t
->buffer
->transaction
= NULL
;
3270 binder_alloc_free_buf(&target_proc
->alloc
, t
->buffer
);
3271 err_binder_alloc_buf_failed
:
3273 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3274 err_alloc_tcomplete_failed
:
3276 binder_stats_deleted(BINDER_STAT_TRANSACTION
);
3280 err_empty_call_stack
:
3282 err_invalid_target_handle
:
3284 binder_thread_dec_tmpref(target_thread
);
3286 binder_proc_dec_tmpref(target_proc
);
3288 binder_dec_node(target_node
, 1, 0);
3289 binder_dec_node_tmpref(target_node
);
3292 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION
,
3293 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3294 proc
->pid
, thread
->pid
, return_error
, return_error_param
,
3295 (u64
)tr
->data_size
, (u64
)tr
->offsets_size
,
3299 struct binder_transaction_log_entry
*fe
;
3301 e
->return_error
= return_error
;
3302 e
->return_error_param
= return_error_param
;
3303 e
->return_error_line
= return_error_line
;
3304 fe
= binder_transaction_log_add(&binder_transaction_log_failed
);
3307 * write barrier to synchronize with initialization
3311 WRITE_ONCE(e
->debug_id_done
, t_debug_id
);
3312 WRITE_ONCE(fe
->debug_id_done
, t_debug_id
);
3315 BUG_ON(thread
->return_error
.cmd
!= BR_OK
);
3317 thread
->return_error
.cmd
= BR_TRANSACTION_COMPLETE
;
3318 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3319 binder_send_failed_reply(in_reply_to
, return_error
);
3321 thread
->return_error
.cmd
= return_error
;
3322 binder_enqueue_thread_work(thread
, &thread
->return_error
.work
);
3326 static int binder_thread_write(struct binder_proc
*proc
,
3327 struct binder_thread
*thread
,
3328 binder_uintptr_t binder_buffer
, size_t size
,
3329 binder_size_t
*consumed
)
3332 struct binder_context
*context
= proc
->context
;
3333 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3334 void __user
*ptr
= buffer
+ *consumed
;
3335 void __user
*end
= buffer
+ size
;
3337 while (ptr
< end
&& thread
->return_error
.cmd
== BR_OK
) {
3340 if (get_user(cmd
, (uint32_t __user
*)ptr
))
3342 ptr
+= sizeof(uint32_t);
3343 trace_binder_command(cmd
);
3344 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.bc
)) {
3345 atomic_inc(&binder_stats
.bc
[_IOC_NR(cmd
)]);
3346 atomic_inc(&proc
->stats
.bc
[_IOC_NR(cmd
)]);
3347 atomic_inc(&thread
->stats
.bc
[_IOC_NR(cmd
)]);
3355 const char *debug_string
;
3356 bool strong
= cmd
== BC_ACQUIRE
|| cmd
== BC_RELEASE
;
3357 bool increment
= cmd
== BC_INCREFS
|| cmd
== BC_ACQUIRE
;
3358 struct binder_ref_data rdata
;
3360 if (get_user(target
, (uint32_t __user
*)ptr
))
3363 ptr
+= sizeof(uint32_t);
3365 if (increment
&& !target
) {
3366 struct binder_node
*ctx_mgr_node
;
3367 mutex_lock(&context
->context_mgr_node_lock
);
3368 ctx_mgr_node
= context
->binder_context_mgr_node
;
3370 ret
= binder_inc_ref_for_node(
3372 strong
, NULL
, &rdata
);
3373 mutex_unlock(&context
->context_mgr_node_lock
);
3376 ret
= binder_update_ref_for_handle(
3377 proc
, target
, increment
, strong
,
3379 if (!ret
&& rdata
.desc
!= target
) {
3380 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3381 proc
->pid
, thread
->pid
,
3382 target
, rdata
.desc
);
3386 debug_string
= "IncRefs";
3389 debug_string
= "Acquire";
3392 debug_string
= "Release";
3396 debug_string
= "DecRefs";
3400 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3401 proc
->pid
, thread
->pid
, debug_string
,
3402 strong
, target
, ret
);
3405 binder_debug(BINDER_DEBUG_USER_REFS
,
3406 "%d:%d %s ref %d desc %d s %d w %d\n",
3407 proc
->pid
, thread
->pid
, debug_string
,
3408 rdata
.debug_id
, rdata
.desc
, rdata
.strong
,
3412 case BC_INCREFS_DONE
:
3413 case BC_ACQUIRE_DONE
: {
3414 binder_uintptr_t node_ptr
;
3415 binder_uintptr_t cookie
;
3416 struct binder_node
*node
;
3419 if (get_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3421 ptr
+= sizeof(binder_uintptr_t
);
3422 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3424 ptr
+= sizeof(binder_uintptr_t
);
3425 node
= binder_get_node(proc
, node_ptr
);
3427 binder_user_error("%d:%d %s u%016llx no match\n",
3428 proc
->pid
, thread
->pid
,
3429 cmd
== BC_INCREFS_DONE
?
3435 if (cookie
!= node
->cookie
) {
3436 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3437 proc
->pid
, thread
->pid
,
3438 cmd
== BC_INCREFS_DONE
?
3439 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3440 (u64
)node_ptr
, node
->debug_id
,
3441 (u64
)cookie
, (u64
)node
->cookie
);
3442 binder_put_node(node
);
3445 binder_node_inner_lock(node
);
3446 if (cmd
== BC_ACQUIRE_DONE
) {
3447 if (node
->pending_strong_ref
== 0) {
3448 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3449 proc
->pid
, thread
->pid
,
3451 binder_node_inner_unlock(node
);
3452 binder_put_node(node
);
3455 node
->pending_strong_ref
= 0;
3457 if (node
->pending_weak_ref
== 0) {
3458 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3459 proc
->pid
, thread
->pid
,
3461 binder_node_inner_unlock(node
);
3462 binder_put_node(node
);
3465 node
->pending_weak_ref
= 0;
3467 free_node
= binder_dec_node_nilocked(node
,
3468 cmd
== BC_ACQUIRE_DONE
, 0);
3470 binder_debug(BINDER_DEBUG_USER_REFS
,
3471 "%d:%d %s node %d ls %d lw %d tr %d\n",
3472 proc
->pid
, thread
->pid
,
3473 cmd
== BC_INCREFS_DONE
? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3474 node
->debug_id
, node
->local_strong_refs
,
3475 node
->local_weak_refs
, node
->tmp_refs
);
3476 binder_node_inner_unlock(node
);
3477 binder_put_node(node
);
3480 case BC_ATTEMPT_ACQUIRE
:
3481 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3483 case BC_ACQUIRE_RESULT
:
3484 pr_err("BC_ACQUIRE_RESULT not supported\n");
3487 case BC_FREE_BUFFER
: {
3488 binder_uintptr_t data_ptr
;
3489 struct binder_buffer
*buffer
;
3491 if (get_user(data_ptr
, (binder_uintptr_t __user
*)ptr
))
3493 ptr
+= sizeof(binder_uintptr_t
);
3495 buffer
= binder_alloc_prepare_to_free(&proc
->alloc
,
3497 if (IS_ERR_OR_NULL(buffer
)) {
3498 if (PTR_ERR(buffer
) == -EPERM
) {
3500 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3501 proc
->pid
, thread
->pid
,
3505 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
3506 proc
->pid
, thread
->pid
,
3511 binder_debug(BINDER_DEBUG_FREE_BUFFER
,
3512 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3513 proc
->pid
, thread
->pid
, (u64
)data_ptr
,
3515 buffer
->transaction
? "active" : "finished");
3517 binder_inner_proc_lock(proc
);
3518 if (buffer
->transaction
) {
3519 buffer
->transaction
->buffer
= NULL
;
3520 buffer
->transaction
= NULL
;
3522 binder_inner_proc_unlock(proc
);
3523 if (buffer
->async_transaction
&& buffer
->target_node
) {
3524 struct binder_node
*buf_node
;
3525 struct binder_work
*w
;
3527 buf_node
= buffer
->target_node
;
3528 binder_node_inner_lock(buf_node
);
3529 BUG_ON(!buf_node
->has_async_transaction
);
3530 BUG_ON(buf_node
->proc
!= proc
);
3531 w
= binder_dequeue_work_head_ilocked(
3532 &buf_node
->async_todo
);
3534 buf_node
->has_async_transaction
= false;
3536 binder_enqueue_work_ilocked(
3538 binder_wakeup_proc_ilocked(proc
);
3540 binder_node_inner_unlock(buf_node
);
3542 trace_binder_transaction_buffer_release(buffer
);
3543 binder_transaction_buffer_release(proc
, buffer
, NULL
);
3544 binder_alloc_free_buf(&proc
->alloc
, buffer
);
3548 case BC_TRANSACTION_SG
:
3550 struct binder_transaction_data_sg tr
;
3552 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3555 binder_transaction(proc
, thread
, &tr
.transaction_data
,
3556 cmd
== BC_REPLY_SG
, tr
.buffers_size
);
3559 case BC_TRANSACTION
:
3561 struct binder_transaction_data tr
;
3563 if (copy_from_user(&tr
, ptr
, sizeof(tr
)))
3566 binder_transaction(proc
, thread
, &tr
,
3567 cmd
== BC_REPLY
, 0);
3571 case BC_REGISTER_LOOPER
:
3572 binder_debug(BINDER_DEBUG_THREADS
,
3573 "%d:%d BC_REGISTER_LOOPER\n",
3574 proc
->pid
, thread
->pid
);
3575 binder_inner_proc_lock(proc
);
3576 if (thread
->looper
& BINDER_LOOPER_STATE_ENTERED
) {
3577 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3578 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
3579 proc
->pid
, thread
->pid
);
3580 } else if (proc
->requested_threads
== 0) {
3581 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3582 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
3583 proc
->pid
, thread
->pid
);
3585 proc
->requested_threads
--;
3586 proc
->requested_threads_started
++;
3588 thread
->looper
|= BINDER_LOOPER_STATE_REGISTERED
;
3589 binder_inner_proc_unlock(proc
);
3591 case BC_ENTER_LOOPER
:
3592 binder_debug(BINDER_DEBUG_THREADS
,
3593 "%d:%d BC_ENTER_LOOPER\n",
3594 proc
->pid
, thread
->pid
);
3595 if (thread
->looper
& BINDER_LOOPER_STATE_REGISTERED
) {
3596 thread
->looper
|= BINDER_LOOPER_STATE_INVALID
;
3597 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
3598 proc
->pid
, thread
->pid
);
3600 thread
->looper
|= BINDER_LOOPER_STATE_ENTERED
;
3602 case BC_EXIT_LOOPER
:
3603 binder_debug(BINDER_DEBUG_THREADS
,
3604 "%d:%d BC_EXIT_LOOPER\n",
3605 proc
->pid
, thread
->pid
);
3606 thread
->looper
|= BINDER_LOOPER_STATE_EXITED
;
3609 case BC_REQUEST_DEATH_NOTIFICATION
:
3610 case BC_CLEAR_DEATH_NOTIFICATION
: {
3612 binder_uintptr_t cookie
;
3613 struct binder_ref
*ref
;
3614 struct binder_ref_death
*death
= NULL
;
3616 if (get_user(target
, (uint32_t __user
*)ptr
))
3618 ptr
+= sizeof(uint32_t);
3619 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3621 ptr
+= sizeof(binder_uintptr_t
);
3622 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3624 * Allocate memory for death notification
3625 * before taking lock
3627 death
= kzalloc(sizeof(*death
), GFP_KERNEL
);
3628 if (death
== NULL
) {
3629 WARN_ON(thread
->return_error
.cmd
!=
3631 thread
->return_error
.cmd
= BR_ERROR
;
3632 binder_enqueue_thread_work(
3634 &thread
->return_error
.work
);
3636 BINDER_DEBUG_FAILED_TRANSACTION
,
3637 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3638 proc
->pid
, thread
->pid
);
3642 binder_proc_lock(proc
);
3643 ref
= binder_get_ref_olocked(proc
, target
, false);
3645 binder_user_error("%d:%d %s invalid ref %d\n",
3646 proc
->pid
, thread
->pid
,
3647 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3648 "BC_REQUEST_DEATH_NOTIFICATION" :
3649 "BC_CLEAR_DEATH_NOTIFICATION",
3651 binder_proc_unlock(proc
);
3656 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
3657 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
3658 proc
->pid
, thread
->pid
,
3659 cmd
== BC_REQUEST_DEATH_NOTIFICATION
?
3660 "BC_REQUEST_DEATH_NOTIFICATION" :
3661 "BC_CLEAR_DEATH_NOTIFICATION",
3662 (u64
)cookie
, ref
->data
.debug_id
,
3663 ref
->data
.desc
, ref
->data
.strong
,
3664 ref
->data
.weak
, ref
->node
->debug_id
);
3666 binder_node_lock(ref
->node
);
3667 if (cmd
== BC_REQUEST_DEATH_NOTIFICATION
) {
3669 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
3670 proc
->pid
, thread
->pid
);
3671 binder_node_unlock(ref
->node
);
3672 binder_proc_unlock(proc
);
3676 binder_stats_created(BINDER_STAT_DEATH
);
3677 INIT_LIST_HEAD(&death
->work
.entry
);
3678 death
->cookie
= cookie
;
3680 if (ref
->node
->proc
== NULL
) {
3681 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
3683 binder_inner_proc_lock(proc
);
3684 binder_enqueue_work_ilocked(
3685 &ref
->death
->work
, &proc
->todo
);
3686 binder_wakeup_proc_ilocked(proc
);
3687 binder_inner_proc_unlock(proc
);
3690 if (ref
->death
== NULL
) {
3691 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
3692 proc
->pid
, thread
->pid
);
3693 binder_node_unlock(ref
->node
);
3694 binder_proc_unlock(proc
);
3698 if (death
->cookie
!= cookie
) {
3699 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
3700 proc
->pid
, thread
->pid
,
3703 binder_node_unlock(ref
->node
);
3704 binder_proc_unlock(proc
);
3708 binder_inner_proc_lock(proc
);
3709 if (list_empty(&death
->work
.entry
)) {
3710 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3711 if (thread
->looper
&
3712 (BINDER_LOOPER_STATE_REGISTERED
|
3713 BINDER_LOOPER_STATE_ENTERED
))
3714 binder_enqueue_thread_work_ilocked(
3718 binder_enqueue_work_ilocked(
3721 binder_wakeup_proc_ilocked(
3725 BUG_ON(death
->work
.type
!= BINDER_WORK_DEAD_BINDER
);
3726 death
->work
.type
= BINDER_WORK_DEAD_BINDER_AND_CLEAR
;
3728 binder_inner_proc_unlock(proc
);
3730 binder_node_unlock(ref
->node
);
3731 binder_proc_unlock(proc
);
3733 case BC_DEAD_BINDER_DONE
: {
3734 struct binder_work
*w
;
3735 binder_uintptr_t cookie
;
3736 struct binder_ref_death
*death
= NULL
;
3738 if (get_user(cookie
, (binder_uintptr_t __user
*)ptr
))
3741 ptr
+= sizeof(cookie
);
3742 binder_inner_proc_lock(proc
);
3743 list_for_each_entry(w
, &proc
->delivered_death
,
3745 struct binder_ref_death
*tmp_death
=
3747 struct binder_ref_death
,
3750 if (tmp_death
->cookie
== cookie
) {
3755 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
3756 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
3757 proc
->pid
, thread
->pid
, (u64
)cookie
,
3759 if (death
== NULL
) {
3760 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3761 proc
->pid
, thread
->pid
, (u64
)cookie
);
3762 binder_inner_proc_unlock(proc
);
3765 binder_dequeue_work_ilocked(&death
->work
);
3766 if (death
->work
.type
== BINDER_WORK_DEAD_BINDER_AND_CLEAR
) {
3767 death
->work
.type
= BINDER_WORK_CLEAR_DEATH_NOTIFICATION
;
3768 if (thread
->looper
&
3769 (BINDER_LOOPER_STATE_REGISTERED
|
3770 BINDER_LOOPER_STATE_ENTERED
))
3771 binder_enqueue_thread_work_ilocked(
3772 thread
, &death
->work
);
3774 binder_enqueue_work_ilocked(
3777 binder_wakeup_proc_ilocked(proc
);
3780 binder_inner_proc_unlock(proc
);
3784 pr_err("%d:%d unknown command %d\n",
3785 proc
->pid
, thread
->pid
, cmd
);
3788 *consumed
= ptr
- buffer
;
3793 static void binder_stat_br(struct binder_proc
*proc
,
3794 struct binder_thread
*thread
, uint32_t cmd
)
3796 trace_binder_return(cmd
);
3797 if (_IOC_NR(cmd
) < ARRAY_SIZE(binder_stats
.br
)) {
3798 atomic_inc(&binder_stats
.br
[_IOC_NR(cmd
)]);
3799 atomic_inc(&proc
->stats
.br
[_IOC_NR(cmd
)]);
3800 atomic_inc(&thread
->stats
.br
[_IOC_NR(cmd
)]);
3804 static int binder_put_node_cmd(struct binder_proc
*proc
,
3805 struct binder_thread
*thread
,
3807 binder_uintptr_t node_ptr
,
3808 binder_uintptr_t node_cookie
,
3810 uint32_t cmd
, const char *cmd_name
)
3812 void __user
*ptr
= *ptrp
;
3814 if (put_user(cmd
, (uint32_t __user
*)ptr
))
3816 ptr
+= sizeof(uint32_t);
3818 if (put_user(node_ptr
, (binder_uintptr_t __user
*)ptr
))
3820 ptr
+= sizeof(binder_uintptr_t
);
3822 if (put_user(node_cookie
, (binder_uintptr_t __user
*)ptr
))
3824 ptr
+= sizeof(binder_uintptr_t
);
3826 binder_stat_br(proc
, thread
, cmd
);
3827 binder_debug(BINDER_DEBUG_USER_REFS
, "%d:%d %s %d u%016llx c%016llx\n",
3828 proc
->pid
, thread
->pid
, cmd_name
, node_debug_id
,
3829 (u64
)node_ptr
, (u64
)node_cookie
);
3835 static int binder_wait_for_work(struct binder_thread
*thread
,
3839 struct binder_proc
*proc
= thread
->proc
;
3842 freezer_do_not_count();
3843 binder_inner_proc_lock(proc
);
3845 prepare_to_wait(&thread
->wait
, &wait
, TASK_INTERRUPTIBLE
);
3846 if (binder_has_work_ilocked(thread
, do_proc_work
))
3849 list_add(&thread
->waiting_thread_node
,
3850 &proc
->waiting_threads
);
3851 binder_inner_proc_unlock(proc
);
3853 binder_inner_proc_lock(proc
);
3854 list_del_init(&thread
->waiting_thread_node
);
3855 if (signal_pending(current
)) {
3860 finish_wait(&thread
->wait
, &wait
);
3861 binder_inner_proc_unlock(proc
);
3867 static int binder_thread_read(struct binder_proc
*proc
,
3868 struct binder_thread
*thread
,
3869 binder_uintptr_t binder_buffer
, size_t size
,
3870 binder_size_t
*consumed
, int non_block
)
3872 void __user
*buffer
= (void __user
*)(uintptr_t)binder_buffer
;
3873 void __user
*ptr
= buffer
+ *consumed
;
3874 void __user
*end
= buffer
+ size
;
3877 int wait_for_proc_work
;
3879 if (*consumed
== 0) {
3880 if (put_user(BR_NOOP
, (uint32_t __user
*)ptr
))
3882 ptr
+= sizeof(uint32_t);
3886 binder_inner_proc_lock(proc
);
3887 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
3888 binder_inner_proc_unlock(proc
);
3890 thread
->looper
|= BINDER_LOOPER_STATE_WAITING
;
3892 trace_binder_wait_for_work(wait_for_proc_work
,
3893 !!thread
->transaction_stack
,
3894 !binder_worklist_empty(proc
, &thread
->todo
));
3895 if (wait_for_proc_work
) {
3896 if (!(thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
3897 BINDER_LOOPER_STATE_ENTERED
))) {
3898 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
3899 proc
->pid
, thread
->pid
, thread
->looper
);
3900 wait_event_interruptible(binder_user_error_wait
,
3901 binder_stop_on_user_error
< 2);
3903 binder_set_nice(proc
->default_priority
);
3907 if (!binder_has_work(thread
, wait_for_proc_work
))
3910 ret
= binder_wait_for_work(thread
, wait_for_proc_work
);
3913 thread
->looper
&= ~BINDER_LOOPER_STATE_WAITING
;
3920 struct binder_transaction_data tr
;
3921 struct binder_work
*w
= NULL
;
3922 struct list_head
*list
= NULL
;
3923 struct binder_transaction
*t
= NULL
;
3924 struct binder_thread
*t_from
;
3926 binder_inner_proc_lock(proc
);
3927 if (!binder_worklist_empty_ilocked(&thread
->todo
))
3928 list
= &thread
->todo
;
3929 else if (!binder_worklist_empty_ilocked(&proc
->todo
) &&
3933 binder_inner_proc_unlock(proc
);
3936 if (ptr
- buffer
== 4 && !thread
->looper_need_return
)
3941 if (end
- ptr
< sizeof(tr
) + 4) {
3942 binder_inner_proc_unlock(proc
);
3945 w
= binder_dequeue_work_head_ilocked(list
);
3946 if (binder_worklist_empty_ilocked(&thread
->todo
))
3947 thread
->process_todo
= false;
3950 case BINDER_WORK_TRANSACTION
: {
3951 binder_inner_proc_unlock(proc
);
3952 t
= container_of(w
, struct binder_transaction
, work
);
3954 case BINDER_WORK_RETURN_ERROR
: {
3955 struct binder_error
*e
= container_of(
3956 w
, struct binder_error
, work
);
3958 WARN_ON(e
->cmd
== BR_OK
);
3959 binder_inner_proc_unlock(proc
);
3960 if (put_user(e
->cmd
, (uint32_t __user
*)ptr
))
3964 ptr
+= sizeof(uint32_t);
3966 binder_stat_br(proc
, thread
, cmd
);
3968 case BINDER_WORK_TRANSACTION_COMPLETE
: {
3969 binder_inner_proc_unlock(proc
);
3970 cmd
= BR_TRANSACTION_COMPLETE
;
3972 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
3973 if (put_user(cmd
, (uint32_t __user
*)ptr
))
3975 ptr
+= sizeof(uint32_t);
3977 binder_stat_br(proc
, thread
, cmd
);
3978 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE
,
3979 "%d:%d BR_TRANSACTION_COMPLETE\n",
3980 proc
->pid
, thread
->pid
);
3982 case BINDER_WORK_NODE
: {
3983 struct binder_node
*node
= container_of(w
, struct binder_node
, work
);
3985 binder_uintptr_t node_ptr
= node
->ptr
;
3986 binder_uintptr_t node_cookie
= node
->cookie
;
3987 int node_debug_id
= node
->debug_id
;
3990 void __user
*orig_ptr
= ptr
;
3992 BUG_ON(proc
!= node
->proc
);
3993 strong
= node
->internal_strong_refs
||
3994 node
->local_strong_refs
;
3995 weak
= !hlist_empty(&node
->refs
) ||
3996 node
->local_weak_refs
||
3997 node
->tmp_refs
|| strong
;
3998 has_strong_ref
= node
->has_strong_ref
;
3999 has_weak_ref
= node
->has_weak_ref
;
4001 if (weak
&& !has_weak_ref
) {
4002 node
->has_weak_ref
= 1;
4003 node
->pending_weak_ref
= 1;
4004 node
->local_weak_refs
++;
4006 if (strong
&& !has_strong_ref
) {
4007 node
->has_strong_ref
= 1;
4008 node
->pending_strong_ref
= 1;
4009 node
->local_strong_refs
++;
4011 if (!strong
&& has_strong_ref
)
4012 node
->has_strong_ref
= 0;
4013 if (!weak
&& has_weak_ref
)
4014 node
->has_weak_ref
= 0;
4015 if (!weak
&& !strong
) {
4016 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4017 "%d:%d node %d u%016llx c%016llx deleted\n",
4018 proc
->pid
, thread
->pid
,
4022 rb_erase(&node
->rb_node
, &proc
->nodes
);
4023 binder_inner_proc_unlock(proc
);
4024 binder_node_lock(node
);
4026 * Acquire the node lock before freeing the
4027 * node to serialize with other threads that
4028 * may have been holding the node lock while
4029 * decrementing this node (avoids race where
4030 * this thread frees while the other thread
4031 * is unlocking the node after the final
4034 binder_node_unlock(node
);
4035 binder_free_node(node
);
4037 binder_inner_proc_unlock(proc
);
4039 if (weak
&& !has_weak_ref
)
4040 ret
= binder_put_node_cmd(
4041 proc
, thread
, &ptr
, node_ptr
,
4042 node_cookie
, node_debug_id
,
4043 BR_INCREFS
, "BR_INCREFS");
4044 if (!ret
&& strong
&& !has_strong_ref
)
4045 ret
= binder_put_node_cmd(
4046 proc
, thread
, &ptr
, node_ptr
,
4047 node_cookie
, node_debug_id
,
4048 BR_ACQUIRE
, "BR_ACQUIRE");
4049 if (!ret
&& !strong
&& has_strong_ref
)
4050 ret
= binder_put_node_cmd(
4051 proc
, thread
, &ptr
, node_ptr
,
4052 node_cookie
, node_debug_id
,
4053 BR_RELEASE
, "BR_RELEASE");
4054 if (!ret
&& !weak
&& has_weak_ref
)
4055 ret
= binder_put_node_cmd(
4056 proc
, thread
, &ptr
, node_ptr
,
4057 node_cookie
, node_debug_id
,
4058 BR_DECREFS
, "BR_DECREFS");
4059 if (orig_ptr
== ptr
)
4060 binder_debug(BINDER_DEBUG_INTERNAL_REFS
,
4061 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4062 proc
->pid
, thread
->pid
,
4069 case BINDER_WORK_DEAD_BINDER
:
4070 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4071 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4072 struct binder_ref_death
*death
;
4074 binder_uintptr_t cookie
;
4076 death
= container_of(w
, struct binder_ref_death
, work
);
4077 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
)
4078 cmd
= BR_CLEAR_DEATH_NOTIFICATION_DONE
;
4080 cmd
= BR_DEAD_BINDER
;
4081 cookie
= death
->cookie
;
4083 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION
,
4084 "%d:%d %s %016llx\n",
4085 proc
->pid
, thread
->pid
,
4086 cmd
== BR_DEAD_BINDER
?
4088 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4090 if (w
->type
== BINDER_WORK_CLEAR_DEATH_NOTIFICATION
) {
4091 binder_inner_proc_unlock(proc
);
4093 binder_stats_deleted(BINDER_STAT_DEATH
);
4095 binder_enqueue_work_ilocked(
4096 w
, &proc
->delivered_death
);
4097 binder_inner_proc_unlock(proc
);
4099 if (put_user(cmd
, (uint32_t __user
*)ptr
))
4101 ptr
+= sizeof(uint32_t);
4102 if (put_user(cookie
,
4103 (binder_uintptr_t __user
*)ptr
))
4105 ptr
+= sizeof(binder_uintptr_t
);
4106 binder_stat_br(proc
, thread
, cmd
);
4107 if (cmd
== BR_DEAD_BINDER
)
4108 goto done
; /* DEAD_BINDER notifications can cause transactions */
4115 BUG_ON(t
->buffer
== NULL
);
4116 if (t
->buffer
->target_node
) {
4117 struct binder_node
*target_node
= t
->buffer
->target_node
;
4119 tr
.target
.ptr
= target_node
->ptr
;
4120 tr
.cookie
= target_node
->cookie
;
4121 t
->saved_priority
= task_nice(current
);
4122 if (t
->priority
< target_node
->min_priority
&&
4123 !(t
->flags
& TF_ONE_WAY
))
4124 binder_set_nice(t
->priority
);
4125 else if (!(t
->flags
& TF_ONE_WAY
) ||
4126 t
->saved_priority
> target_node
->min_priority
)
4127 binder_set_nice(target_node
->min_priority
);
4128 cmd
= BR_TRANSACTION
;
4135 tr
.flags
= t
->flags
;
4136 tr
.sender_euid
= from_kuid(current_user_ns(), t
->sender_euid
);
4138 t_from
= binder_get_txn_from(t
);
4140 struct task_struct
*sender
= t_from
->proc
->tsk
;
4142 tr
.sender_pid
= task_tgid_nr_ns(sender
,
4143 task_active_pid_ns(current
));
4148 tr
.data_size
= t
->buffer
->data_size
;
4149 tr
.offsets_size
= t
->buffer
->offsets_size
;
4150 tr
.data
.ptr
.buffer
= (binder_uintptr_t
)
4151 ((uintptr_t)t
->buffer
->data
+
4152 binder_alloc_get_user_buffer_offset(&proc
->alloc
));
4153 tr
.data
.ptr
.offsets
= tr
.data
.ptr
.buffer
+
4154 ALIGN(t
->buffer
->data_size
,
4157 if (put_user(cmd
, (uint32_t __user
*)ptr
)) {
4159 binder_thread_dec_tmpref(t_from
);
4161 binder_cleanup_transaction(t
, "put_user failed",
4166 ptr
+= sizeof(uint32_t);
4167 if (copy_to_user(ptr
, &tr
, sizeof(tr
))) {
4169 binder_thread_dec_tmpref(t_from
);
4171 binder_cleanup_transaction(t
, "copy_to_user failed",
4178 trace_binder_transaction_received(t
);
4179 binder_stat_br(proc
, thread
, cmd
);
4180 binder_debug(BINDER_DEBUG_TRANSACTION
,
4181 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
4182 proc
->pid
, thread
->pid
,
4183 (cmd
== BR_TRANSACTION
) ? "BR_TRANSACTION" :
4185 t
->debug_id
, t_from
? t_from
->proc
->pid
: 0,
4186 t_from
? t_from
->pid
: 0, cmd
,
4187 t
->buffer
->data_size
, t
->buffer
->offsets_size
,
4188 (u64
)tr
.data
.ptr
.buffer
, (u64
)tr
.data
.ptr
.offsets
);
4191 binder_thread_dec_tmpref(t_from
);
4192 t
->buffer
->allow_user_free
= 1;
4193 if (cmd
== BR_TRANSACTION
&& !(t
->flags
& TF_ONE_WAY
)) {
4194 binder_inner_proc_lock(thread
->proc
);
4195 t
->to_parent
= thread
->transaction_stack
;
4196 t
->to_thread
= thread
;
4197 thread
->transaction_stack
= t
;
4198 binder_inner_proc_unlock(thread
->proc
);
4200 binder_free_transaction(t
);
4207 *consumed
= ptr
- buffer
;
4208 binder_inner_proc_lock(proc
);
4209 if (proc
->requested_threads
== 0 &&
4210 list_empty(&thread
->proc
->waiting_threads
) &&
4211 proc
->requested_threads_started
< proc
->max_threads
&&
4212 (thread
->looper
& (BINDER_LOOPER_STATE_REGISTERED
|
4213 BINDER_LOOPER_STATE_ENTERED
)) /* the user-space code fails to */
4214 /*spawn a new thread if we leave this out */) {
4215 proc
->requested_threads
++;
4216 binder_inner_proc_unlock(proc
);
4217 binder_debug(BINDER_DEBUG_THREADS
,
4218 "%d:%d BR_SPAWN_LOOPER\n",
4219 proc
->pid
, thread
->pid
);
4220 if (put_user(BR_SPAWN_LOOPER
, (uint32_t __user
*)buffer
))
4222 binder_stat_br(proc
, thread
, BR_SPAWN_LOOPER
);
4224 binder_inner_proc_unlock(proc
);
4228 static void binder_release_work(struct binder_proc
*proc
,
4229 struct list_head
*list
)
4231 struct binder_work
*w
;
4234 w
= binder_dequeue_work_head(proc
, list
);
4239 case BINDER_WORK_TRANSACTION
: {
4240 struct binder_transaction
*t
;
4242 t
= container_of(w
, struct binder_transaction
, work
);
4244 binder_cleanup_transaction(t
, "process died.",
4247 case BINDER_WORK_RETURN_ERROR
: {
4248 struct binder_error
*e
= container_of(
4249 w
, struct binder_error
, work
);
4251 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4252 "undelivered TRANSACTION_ERROR: %u\n",
4255 case BINDER_WORK_TRANSACTION_COMPLETE
: {
4256 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4257 "undelivered TRANSACTION_COMPLETE\n");
4259 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE
);
4261 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
4262 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
: {
4263 struct binder_ref_death
*death
;
4265 death
= container_of(w
, struct binder_ref_death
, work
);
4266 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4267 "undelivered death notification, %016llx\n",
4268 (u64
)death
->cookie
);
4270 binder_stats_deleted(BINDER_STAT_DEATH
);
4273 pr_err("unexpected work type, %d, not freed\n",
4281 static struct binder_thread
*binder_get_thread_ilocked(
4282 struct binder_proc
*proc
, struct binder_thread
*new_thread
)
4284 struct binder_thread
*thread
= NULL
;
4285 struct rb_node
*parent
= NULL
;
4286 struct rb_node
**p
= &proc
->threads
.rb_node
;
4290 thread
= rb_entry(parent
, struct binder_thread
, rb_node
);
4292 if (current
->pid
< thread
->pid
)
4294 else if (current
->pid
> thread
->pid
)
4295 p
= &(*p
)->rb_right
;
4301 thread
= new_thread
;
4302 binder_stats_created(BINDER_STAT_THREAD
);
4303 thread
->proc
= proc
;
4304 thread
->pid
= current
->pid
;
4305 atomic_set(&thread
->tmp_ref
, 0);
4306 init_waitqueue_head(&thread
->wait
);
4307 INIT_LIST_HEAD(&thread
->todo
);
4308 rb_link_node(&thread
->rb_node
, parent
, p
);
4309 rb_insert_color(&thread
->rb_node
, &proc
->threads
);
4310 thread
->looper_need_return
= true;
4311 thread
->return_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4312 thread
->return_error
.cmd
= BR_OK
;
4313 thread
->reply_error
.work
.type
= BINDER_WORK_RETURN_ERROR
;
4314 thread
->reply_error
.cmd
= BR_OK
;
4315 INIT_LIST_HEAD(&new_thread
->waiting_thread_node
);
4319 static struct binder_thread
*binder_get_thread(struct binder_proc
*proc
)
4321 struct binder_thread
*thread
;
4322 struct binder_thread
*new_thread
;
4324 binder_inner_proc_lock(proc
);
4325 thread
= binder_get_thread_ilocked(proc
, NULL
);
4326 binder_inner_proc_unlock(proc
);
4328 new_thread
= kzalloc(sizeof(*thread
), GFP_KERNEL
);
4329 if (new_thread
== NULL
)
4331 binder_inner_proc_lock(proc
);
4332 thread
= binder_get_thread_ilocked(proc
, new_thread
);
4333 binder_inner_proc_unlock(proc
);
4334 if (thread
!= new_thread
)
4340 static void binder_free_proc(struct binder_proc
*proc
)
4342 BUG_ON(!list_empty(&proc
->todo
));
4343 BUG_ON(!list_empty(&proc
->delivered_death
));
4344 binder_alloc_deferred_release(&proc
->alloc
);
4345 put_task_struct(proc
->tsk
);
4346 binder_stats_deleted(BINDER_STAT_PROC
);
4350 static void binder_free_thread(struct binder_thread
*thread
)
4352 BUG_ON(!list_empty(&thread
->todo
));
4353 binder_stats_deleted(BINDER_STAT_THREAD
);
4354 binder_proc_dec_tmpref(thread
->proc
);
4358 static int binder_thread_release(struct binder_proc
*proc
,
4359 struct binder_thread
*thread
)
4361 struct binder_transaction
*t
;
4362 struct binder_transaction
*send_reply
= NULL
;
4363 int active_transactions
= 0;
4364 struct binder_transaction
*last_t
= NULL
;
4366 binder_inner_proc_lock(thread
->proc
);
4368 * take a ref on the proc so it survives
4369 * after we remove this thread from proc->threads.
4370 * The corresponding dec is when we actually
4371 * free the thread in binder_free_thread()
4375 * take a ref on this thread to ensure it
4376 * survives while we are releasing it
4378 atomic_inc(&thread
->tmp_ref
);
4379 rb_erase(&thread
->rb_node
, &proc
->threads
);
4380 t
= thread
->transaction_stack
;
4382 spin_lock(&t
->lock
);
4383 if (t
->to_thread
== thread
)
4386 thread
->is_dead
= true;
4390 active_transactions
++;
4391 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION
,
4392 "release %d:%d transaction %d %s, still active\n",
4393 proc
->pid
, thread
->pid
,
4395 (t
->to_thread
== thread
) ? "in" : "out");
4397 if (t
->to_thread
== thread
) {
4399 t
->to_thread
= NULL
;
4401 t
->buffer
->transaction
= NULL
;
4405 } else if (t
->from
== thread
) {
4410 spin_unlock(&last_t
->lock
);
4412 spin_lock(&t
->lock
);
4416 * If this thread used poll, make sure we remove the waitqueue
4417 * from any epoll data structures holding it with POLLFREE.
4418 * waitqueue_active() is safe to use here because we're holding
4421 if ((thread
->looper
& BINDER_LOOPER_STATE_POLL
) &&
4422 waitqueue_active(&thread
->wait
)) {
4423 wake_up_poll(&thread
->wait
, EPOLLHUP
| POLLFREE
);
4426 binder_inner_proc_unlock(thread
->proc
);
4429 * This is needed to avoid races between wake_up_poll() above and
4430 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4431 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4432 * lock, so we can be sure it's done after calling synchronize_rcu().
4434 if (thread
->looper
& BINDER_LOOPER_STATE_POLL
)
4438 binder_send_failed_reply(send_reply
, BR_DEAD_REPLY
);
4439 binder_release_work(proc
, &thread
->todo
);
4440 binder_thread_dec_tmpref(thread
);
4441 return active_transactions
;
4444 static __poll_t
binder_poll(struct file
*filp
,
4445 struct poll_table_struct
*wait
)
4447 struct binder_proc
*proc
= filp
->private_data
;
4448 struct binder_thread
*thread
= NULL
;
4449 bool wait_for_proc_work
;
4451 thread
= binder_get_thread(proc
);
4455 binder_inner_proc_lock(thread
->proc
);
4456 thread
->looper
|= BINDER_LOOPER_STATE_POLL
;
4457 wait_for_proc_work
= binder_available_for_proc_work_ilocked(thread
);
4459 binder_inner_proc_unlock(thread
->proc
);
4461 poll_wait(filp
, &thread
->wait
, wait
);
4463 if (binder_has_work(thread
, wait_for_proc_work
))
4469 static int binder_ioctl_write_read(struct file
*filp
,
4470 unsigned int cmd
, unsigned long arg
,
4471 struct binder_thread
*thread
)
4474 struct binder_proc
*proc
= filp
->private_data
;
4475 unsigned int size
= _IOC_SIZE(cmd
);
4476 void __user
*ubuf
= (void __user
*)arg
;
4477 struct binder_write_read bwr
;
4479 if (size
!= sizeof(struct binder_write_read
)) {
4483 if (copy_from_user(&bwr
, ubuf
, sizeof(bwr
))) {
4487 binder_debug(BINDER_DEBUG_READ_WRITE
,
4488 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4489 proc
->pid
, thread
->pid
,
4490 (u64
)bwr
.write_size
, (u64
)bwr
.write_buffer
,
4491 (u64
)bwr
.read_size
, (u64
)bwr
.read_buffer
);
4493 if (bwr
.write_size
> 0) {
4494 ret
= binder_thread_write(proc
, thread
,
4497 &bwr
.write_consumed
);
4498 trace_binder_write_done(ret
);
4500 bwr
.read_consumed
= 0;
4501 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4506 if (bwr
.read_size
> 0) {
4507 ret
= binder_thread_read(proc
, thread
, bwr
.read_buffer
,
4510 filp
->f_flags
& O_NONBLOCK
);
4511 trace_binder_read_done(ret
);
4512 binder_inner_proc_lock(proc
);
4513 if (!binder_worklist_empty_ilocked(&proc
->todo
))
4514 binder_wakeup_proc_ilocked(proc
);
4515 binder_inner_proc_unlock(proc
);
4517 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
)))
4522 binder_debug(BINDER_DEBUG_READ_WRITE
,
4523 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4524 proc
->pid
, thread
->pid
,
4525 (u64
)bwr
.write_consumed
, (u64
)bwr
.write_size
,
4526 (u64
)bwr
.read_consumed
, (u64
)bwr
.read_size
);
4527 if (copy_to_user(ubuf
, &bwr
, sizeof(bwr
))) {
4535 static int binder_ioctl_set_ctx_mgr(struct file
*filp
)
4538 struct binder_proc
*proc
= filp
->private_data
;
4539 struct binder_context
*context
= proc
->context
;
4540 struct binder_node
*new_node
;
4541 kuid_t curr_euid
= current_euid();
4543 mutex_lock(&context
->context_mgr_node_lock
);
4544 if (context
->binder_context_mgr_node
) {
4545 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4549 ret
= security_binder_set_context_mgr(proc
->tsk
);
4552 if (uid_valid(context
->binder_context_mgr_uid
)) {
4553 if (!uid_eq(context
->binder_context_mgr_uid
, curr_euid
)) {
4554 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4555 from_kuid(&init_user_ns
, curr_euid
),
4556 from_kuid(&init_user_ns
,
4557 context
->binder_context_mgr_uid
));
4562 context
->binder_context_mgr_uid
= curr_euid
;
4564 new_node
= binder_new_node(proc
, NULL
);
4569 binder_node_lock(new_node
);
4570 new_node
->local_weak_refs
++;
4571 new_node
->local_strong_refs
++;
4572 new_node
->has_strong_ref
= 1;
4573 new_node
->has_weak_ref
= 1;
4574 context
->binder_context_mgr_node
= new_node
;
4575 binder_node_unlock(new_node
);
4576 binder_put_node(new_node
);
4578 mutex_unlock(&context
->context_mgr_node_lock
);
4582 static int binder_ioctl_get_node_debug_info(struct binder_proc
*proc
,
4583 struct binder_node_debug_info
*info
)
4586 binder_uintptr_t ptr
= info
->ptr
;
4588 memset(info
, 0, sizeof(*info
));
4590 binder_inner_proc_lock(proc
);
4591 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
4592 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
4594 if (node
->ptr
> ptr
) {
4595 info
->ptr
= node
->ptr
;
4596 info
->cookie
= node
->cookie
;
4597 info
->has_strong_ref
= node
->has_strong_ref
;
4598 info
->has_weak_ref
= node
->has_weak_ref
;
4602 binder_inner_proc_unlock(proc
);
4607 static long binder_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
4610 struct binder_proc
*proc
= filp
->private_data
;
4611 struct binder_thread
*thread
;
4612 unsigned int size
= _IOC_SIZE(cmd
);
4613 void __user
*ubuf
= (void __user
*)arg
;
4615 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4616 proc->pid, current->pid, cmd, arg);*/
4618 binder_selftest_alloc(&proc
->alloc
);
4620 trace_binder_ioctl(cmd
, arg
);
4622 ret
= wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4626 thread
= binder_get_thread(proc
);
4627 if (thread
== NULL
) {
4633 case BINDER_WRITE_READ
:
4634 ret
= binder_ioctl_write_read(filp
, cmd
, arg
, thread
);
4638 case BINDER_SET_MAX_THREADS
: {
4641 if (copy_from_user(&max_threads
, ubuf
,
4642 sizeof(max_threads
))) {
4646 binder_inner_proc_lock(proc
);
4647 proc
->max_threads
= max_threads
;
4648 binder_inner_proc_unlock(proc
);
4651 case BINDER_SET_CONTEXT_MGR
:
4652 ret
= binder_ioctl_set_ctx_mgr(filp
);
4656 case BINDER_THREAD_EXIT
:
4657 binder_debug(BINDER_DEBUG_THREADS
, "%d:%d exit\n",
4658 proc
->pid
, thread
->pid
);
4659 binder_thread_release(proc
, thread
);
4662 case BINDER_VERSION
: {
4663 struct binder_version __user
*ver
= ubuf
;
4665 if (size
!= sizeof(struct binder_version
)) {
4669 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION
,
4670 &ver
->protocol_version
)) {
4676 case BINDER_GET_NODE_DEBUG_INFO
: {
4677 struct binder_node_debug_info info
;
4679 if (copy_from_user(&info
, ubuf
, sizeof(info
))) {
4684 ret
= binder_ioctl_get_node_debug_info(proc
, &info
);
4688 if (copy_to_user(ubuf
, &info
, sizeof(info
))) {
4701 thread
->looper_need_return
= false;
4702 wait_event_interruptible(binder_user_error_wait
, binder_stop_on_user_error
< 2);
4703 if (ret
&& ret
!= -ERESTARTSYS
)
4704 pr_info("%d:%d ioctl %x %lx returned %d\n", proc
->pid
, current
->pid
, cmd
, arg
, ret
);
4706 trace_binder_ioctl_done(ret
);
4710 static void binder_vma_open(struct vm_area_struct
*vma
)
4712 struct binder_proc
*proc
= vma
->vm_private_data
;
4714 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4715 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4716 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4717 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4718 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4721 static void binder_vma_close(struct vm_area_struct
*vma
)
4723 struct binder_proc
*proc
= vma
->vm_private_data
;
4725 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4726 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
4727 proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4728 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4729 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4730 binder_alloc_vma_close(&proc
->alloc
);
4731 binder_defer_work(proc
, BINDER_DEFERRED_PUT_FILES
);
4734 static vm_fault_t
binder_vm_fault(struct vm_fault
*vmf
)
4736 return VM_FAULT_SIGBUS
;
4739 static const struct vm_operations_struct binder_vm_ops
= {
4740 .open
= binder_vma_open
,
4741 .close
= binder_vma_close
,
4742 .fault
= binder_vm_fault
,
4745 static int binder_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
4748 struct binder_proc
*proc
= filp
->private_data
;
4749 const char *failure_string
;
4751 if (proc
->tsk
!= current
->group_leader
)
4754 if ((vma
->vm_end
- vma
->vm_start
) > SZ_4M
)
4755 vma
->vm_end
= vma
->vm_start
+ SZ_4M
;
4757 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4758 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4759 __func__
, proc
->pid
, vma
->vm_start
, vma
->vm_end
,
4760 (vma
->vm_end
- vma
->vm_start
) / SZ_1K
, vma
->vm_flags
,
4761 (unsigned long)pgprot_val(vma
->vm_page_prot
));
4763 if (vma
->vm_flags
& FORBIDDEN_MMAP_FLAGS
) {
4765 failure_string
= "bad vm_flags";
4768 vma
->vm_flags
|= VM_DONTCOPY
| VM_MIXEDMAP
;
4769 vma
->vm_flags
&= ~VM_MAYWRITE
;
4771 vma
->vm_ops
= &binder_vm_ops
;
4772 vma
->vm_private_data
= proc
;
4774 ret
= binder_alloc_mmap_handler(&proc
->alloc
, vma
);
4777 mutex_lock(&proc
->files_lock
);
4778 proc
->files
= get_files_struct(current
);
4779 mutex_unlock(&proc
->files_lock
);
4783 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__
,
4784 proc
->pid
, vma
->vm_start
, vma
->vm_end
, failure_string
, ret
);
4788 static int binder_open(struct inode
*nodp
, struct file
*filp
)
4790 struct binder_proc
*proc
;
4791 struct binder_device
*binder_dev
;
4793 binder_debug(BINDER_DEBUG_OPEN_CLOSE
, "%s: %d:%d\n", __func__
,
4794 current
->group_leader
->pid
, current
->pid
);
4796 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
4799 spin_lock_init(&proc
->inner_lock
);
4800 spin_lock_init(&proc
->outer_lock
);
4801 get_task_struct(current
->group_leader
);
4802 proc
->tsk
= current
->group_leader
;
4803 mutex_init(&proc
->files_lock
);
4804 INIT_LIST_HEAD(&proc
->todo
);
4805 proc
->default_priority
= task_nice(current
);
4806 binder_dev
= container_of(filp
->private_data
, struct binder_device
,
4808 proc
->context
= &binder_dev
->context
;
4809 binder_alloc_init(&proc
->alloc
);
4811 binder_stats_created(BINDER_STAT_PROC
);
4812 proc
->pid
= current
->group_leader
->pid
;
4813 INIT_LIST_HEAD(&proc
->delivered_death
);
4814 INIT_LIST_HEAD(&proc
->waiting_threads
);
4815 filp
->private_data
= proc
;
4817 mutex_lock(&binder_procs_lock
);
4818 hlist_add_head(&proc
->proc_node
, &binder_procs
);
4819 mutex_unlock(&binder_procs_lock
);
4821 if (binder_debugfs_dir_entry_proc
) {
4824 snprintf(strbuf
, sizeof(strbuf
), "%u", proc
->pid
);
4826 * proc debug entries are shared between contexts, so
4827 * this will fail if the process tries to open the driver
4828 * again with a different context. The priting code will
4829 * anyway print all contexts that a given PID has, so this
4832 proc
->debugfs_entry
= debugfs_create_file(strbuf
, 0444,
4833 binder_debugfs_dir_entry_proc
,
4834 (void *)(unsigned long)proc
->pid
,
4841 static int binder_flush(struct file
*filp
, fl_owner_t id
)
4843 struct binder_proc
*proc
= filp
->private_data
;
4845 binder_defer_work(proc
, BINDER_DEFERRED_FLUSH
);
4850 static void binder_deferred_flush(struct binder_proc
*proc
)
4855 binder_inner_proc_lock(proc
);
4856 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
)) {
4857 struct binder_thread
*thread
= rb_entry(n
, struct binder_thread
, rb_node
);
4859 thread
->looper_need_return
= true;
4860 if (thread
->looper
& BINDER_LOOPER_STATE_WAITING
) {
4861 wake_up_interruptible(&thread
->wait
);
4865 binder_inner_proc_unlock(proc
);
4867 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
4868 "binder_flush: %d woke %d threads\n", proc
->pid
,
4872 static int binder_release(struct inode
*nodp
, struct file
*filp
)
4874 struct binder_proc
*proc
= filp
->private_data
;
4876 debugfs_remove(proc
->debugfs_entry
);
4877 binder_defer_work(proc
, BINDER_DEFERRED_RELEASE
);
4882 static int binder_node_release(struct binder_node
*node
, int refs
)
4884 struct binder_ref
*ref
;
4886 struct binder_proc
*proc
= node
->proc
;
4888 binder_release_work(proc
, &node
->async_todo
);
4890 binder_node_lock(node
);
4891 binder_inner_proc_lock(proc
);
4892 binder_dequeue_work_ilocked(&node
->work
);
4894 * The caller must have taken a temporary ref on the node,
4896 BUG_ON(!node
->tmp_refs
);
4897 if (hlist_empty(&node
->refs
) && node
->tmp_refs
== 1) {
4898 binder_inner_proc_unlock(proc
);
4899 binder_node_unlock(node
);
4900 binder_free_node(node
);
4906 node
->local_strong_refs
= 0;
4907 node
->local_weak_refs
= 0;
4908 binder_inner_proc_unlock(proc
);
4910 spin_lock(&binder_dead_nodes_lock
);
4911 hlist_add_head(&node
->dead_node
, &binder_dead_nodes
);
4912 spin_unlock(&binder_dead_nodes_lock
);
4914 hlist_for_each_entry(ref
, &node
->refs
, node_entry
) {
4917 * Need the node lock to synchronize
4918 * with new notification requests and the
4919 * inner lock to synchronize with queued
4920 * death notifications.
4922 binder_inner_proc_lock(ref
->proc
);
4924 binder_inner_proc_unlock(ref
->proc
);
4930 BUG_ON(!list_empty(&ref
->death
->work
.entry
));
4931 ref
->death
->work
.type
= BINDER_WORK_DEAD_BINDER
;
4932 binder_enqueue_work_ilocked(&ref
->death
->work
,
4934 binder_wakeup_proc_ilocked(ref
->proc
);
4935 binder_inner_proc_unlock(ref
->proc
);
4938 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
4939 "node %d now dead, refs %d, death %d\n",
4940 node
->debug_id
, refs
, death
);
4941 binder_node_unlock(node
);
4942 binder_put_node(node
);
4947 static void binder_deferred_release(struct binder_proc
*proc
)
4949 struct binder_context
*context
= proc
->context
;
4951 int threads
, nodes
, incoming_refs
, outgoing_refs
, active_transactions
;
4953 BUG_ON(proc
->files
);
4955 mutex_lock(&binder_procs_lock
);
4956 hlist_del(&proc
->proc_node
);
4957 mutex_unlock(&binder_procs_lock
);
4959 mutex_lock(&context
->context_mgr_node_lock
);
4960 if (context
->binder_context_mgr_node
&&
4961 context
->binder_context_mgr_node
->proc
== proc
) {
4962 binder_debug(BINDER_DEBUG_DEAD_BINDER
,
4963 "%s: %d context_mgr_node gone\n",
4964 __func__
, proc
->pid
);
4965 context
->binder_context_mgr_node
= NULL
;
4967 mutex_unlock(&context
->context_mgr_node_lock
);
4968 binder_inner_proc_lock(proc
);
4970 * Make sure proc stays alive after we
4971 * remove all the threads
4975 proc
->is_dead
= true;
4977 active_transactions
= 0;
4978 while ((n
= rb_first(&proc
->threads
))) {
4979 struct binder_thread
*thread
;
4981 thread
= rb_entry(n
, struct binder_thread
, rb_node
);
4982 binder_inner_proc_unlock(proc
);
4984 active_transactions
+= binder_thread_release(proc
, thread
);
4985 binder_inner_proc_lock(proc
);
4990 while ((n
= rb_first(&proc
->nodes
))) {
4991 struct binder_node
*node
;
4993 node
= rb_entry(n
, struct binder_node
, rb_node
);
4996 * take a temporary ref on the node before
4997 * calling binder_node_release() which will either
4998 * kfree() the node or call binder_put_node()
5000 binder_inc_node_tmpref_ilocked(node
);
5001 rb_erase(&node
->rb_node
, &proc
->nodes
);
5002 binder_inner_proc_unlock(proc
);
5003 incoming_refs
= binder_node_release(node
, incoming_refs
);
5004 binder_inner_proc_lock(proc
);
5006 binder_inner_proc_unlock(proc
);
5009 binder_proc_lock(proc
);
5010 while ((n
= rb_first(&proc
->refs_by_desc
))) {
5011 struct binder_ref
*ref
;
5013 ref
= rb_entry(n
, struct binder_ref
, rb_node_desc
);
5015 binder_cleanup_ref_olocked(ref
);
5016 binder_proc_unlock(proc
);
5017 binder_free_ref(ref
);
5018 binder_proc_lock(proc
);
5020 binder_proc_unlock(proc
);
5022 binder_release_work(proc
, &proc
->todo
);
5023 binder_release_work(proc
, &proc
->delivered_death
);
5025 binder_debug(BINDER_DEBUG_OPEN_CLOSE
,
5026 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5027 __func__
, proc
->pid
, threads
, nodes
, incoming_refs
,
5028 outgoing_refs
, active_transactions
);
5030 binder_proc_dec_tmpref(proc
);
5033 static void binder_deferred_func(struct work_struct
*work
)
5035 struct binder_proc
*proc
;
5036 struct files_struct
*files
;
5041 mutex_lock(&binder_deferred_lock
);
5042 if (!hlist_empty(&binder_deferred_list
)) {
5043 proc
= hlist_entry(binder_deferred_list
.first
,
5044 struct binder_proc
, deferred_work_node
);
5045 hlist_del_init(&proc
->deferred_work_node
);
5046 defer
= proc
->deferred_work
;
5047 proc
->deferred_work
= 0;
5052 mutex_unlock(&binder_deferred_lock
);
5055 if (defer
& BINDER_DEFERRED_PUT_FILES
) {
5056 mutex_lock(&proc
->files_lock
);
5057 files
= proc
->files
;
5060 mutex_unlock(&proc
->files_lock
);
5063 if (defer
& BINDER_DEFERRED_FLUSH
)
5064 binder_deferred_flush(proc
);
5066 if (defer
& BINDER_DEFERRED_RELEASE
)
5067 binder_deferred_release(proc
); /* frees proc */
5070 put_files_struct(files
);
5073 static DECLARE_WORK(binder_deferred_work
, binder_deferred_func
);
5076 binder_defer_work(struct binder_proc
*proc
, enum binder_deferred_state defer
)
5078 mutex_lock(&binder_deferred_lock
);
5079 proc
->deferred_work
|= defer
;
5080 if (hlist_unhashed(&proc
->deferred_work_node
)) {
5081 hlist_add_head(&proc
->deferred_work_node
,
5082 &binder_deferred_list
);
5083 schedule_work(&binder_deferred_work
);
5085 mutex_unlock(&binder_deferred_lock
);
5088 static void print_binder_transaction_ilocked(struct seq_file
*m
,
5089 struct binder_proc
*proc
,
5091 struct binder_transaction
*t
)
5093 struct binder_proc
*to_proc
;
5094 struct binder_buffer
*buffer
= t
->buffer
;
5096 spin_lock(&t
->lock
);
5097 to_proc
= t
->to_proc
;
5099 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
5100 prefix
, t
->debug_id
, t
,
5101 t
->from
? t
->from
->proc
->pid
: 0,
5102 t
->from
? t
->from
->pid
: 0,
5103 to_proc
? to_proc
->pid
: 0,
5104 t
->to_thread
? t
->to_thread
->pid
: 0,
5105 t
->code
, t
->flags
, t
->priority
, t
->need_reply
);
5106 spin_unlock(&t
->lock
);
5108 if (proc
!= to_proc
) {
5110 * Can only safely deref buffer if we are holding the
5111 * correct proc inner lock for this node
5117 if (buffer
== NULL
) {
5118 seq_puts(m
, " buffer free\n");
5121 if (buffer
->target_node
)
5122 seq_printf(m
, " node %d", buffer
->target_node
->debug_id
);
5123 seq_printf(m
, " size %zd:%zd data %pK\n",
5124 buffer
->data_size
, buffer
->offsets_size
,
5128 static void print_binder_work_ilocked(struct seq_file
*m
,
5129 struct binder_proc
*proc
,
5131 const char *transaction_prefix
,
5132 struct binder_work
*w
)
5134 struct binder_node
*node
;
5135 struct binder_transaction
*t
;
5138 case BINDER_WORK_TRANSACTION
:
5139 t
= container_of(w
, struct binder_transaction
, work
);
5140 print_binder_transaction_ilocked(
5141 m
, proc
, transaction_prefix
, t
);
5143 case BINDER_WORK_RETURN_ERROR
: {
5144 struct binder_error
*e
= container_of(
5145 w
, struct binder_error
, work
);
5147 seq_printf(m
, "%stransaction error: %u\n",
5150 case BINDER_WORK_TRANSACTION_COMPLETE
:
5151 seq_printf(m
, "%stransaction complete\n", prefix
);
5153 case BINDER_WORK_NODE
:
5154 node
= container_of(w
, struct binder_node
, work
);
5155 seq_printf(m
, "%snode work %d: u%016llx c%016llx\n",
5156 prefix
, node
->debug_id
,
5157 (u64
)node
->ptr
, (u64
)node
->cookie
);
5159 case BINDER_WORK_DEAD_BINDER
:
5160 seq_printf(m
, "%shas dead binder\n", prefix
);
5162 case BINDER_WORK_DEAD_BINDER_AND_CLEAR
:
5163 seq_printf(m
, "%shas cleared dead binder\n", prefix
);
5165 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION
:
5166 seq_printf(m
, "%shas cleared death notification\n", prefix
);
5169 seq_printf(m
, "%sunknown work: type %d\n", prefix
, w
->type
);
5174 static void print_binder_thread_ilocked(struct seq_file
*m
,
5175 struct binder_thread
*thread
,
5178 struct binder_transaction
*t
;
5179 struct binder_work
*w
;
5180 size_t start_pos
= m
->count
;
5183 seq_printf(m
, " thread %d: l %02x need_return %d tr %d\n",
5184 thread
->pid
, thread
->looper
,
5185 thread
->looper_need_return
,
5186 atomic_read(&thread
->tmp_ref
));
5187 header_pos
= m
->count
;
5188 t
= thread
->transaction_stack
;
5190 if (t
->from
== thread
) {
5191 print_binder_transaction_ilocked(m
, thread
->proc
,
5192 " outgoing transaction", t
);
5194 } else if (t
->to_thread
== thread
) {
5195 print_binder_transaction_ilocked(m
, thread
->proc
,
5196 " incoming transaction", t
);
5199 print_binder_transaction_ilocked(m
, thread
->proc
,
5200 " bad transaction", t
);
5204 list_for_each_entry(w
, &thread
->todo
, entry
) {
5205 print_binder_work_ilocked(m
, thread
->proc
, " ",
5206 " pending transaction", w
);
5208 if (!print_always
&& m
->count
== header_pos
)
5209 m
->count
= start_pos
;
5212 static void print_binder_node_nilocked(struct seq_file
*m
,
5213 struct binder_node
*node
)
5215 struct binder_ref
*ref
;
5216 struct binder_work
*w
;
5220 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5223 seq_printf(m
, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
5224 node
->debug_id
, (u64
)node
->ptr
, (u64
)node
->cookie
,
5225 node
->has_strong_ref
, node
->has_weak_ref
,
5226 node
->local_strong_refs
, node
->local_weak_refs
,
5227 node
->internal_strong_refs
, count
, node
->tmp_refs
);
5229 seq_puts(m
, " proc");
5230 hlist_for_each_entry(ref
, &node
->refs
, node_entry
)
5231 seq_printf(m
, " %d", ref
->proc
->pid
);
5235 list_for_each_entry(w
, &node
->async_todo
, entry
)
5236 print_binder_work_ilocked(m
, node
->proc
, " ",
5237 " pending async transaction", w
);
5241 static void print_binder_ref_olocked(struct seq_file
*m
,
5242 struct binder_ref
*ref
)
5244 binder_node_lock(ref
->node
);
5245 seq_printf(m
, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5246 ref
->data
.debug_id
, ref
->data
.desc
,
5247 ref
->node
->proc
? "" : "dead ",
5248 ref
->node
->debug_id
, ref
->data
.strong
,
5249 ref
->data
.weak
, ref
->death
);
5250 binder_node_unlock(ref
->node
);
5253 static void print_binder_proc(struct seq_file
*m
,
5254 struct binder_proc
*proc
, int print_all
)
5256 struct binder_work
*w
;
5258 size_t start_pos
= m
->count
;
5260 struct binder_node
*last_node
= NULL
;
5262 seq_printf(m
, "proc %d\n", proc
->pid
);
5263 seq_printf(m
, "context %s\n", proc
->context
->name
);
5264 header_pos
= m
->count
;
5266 binder_inner_proc_lock(proc
);
5267 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5268 print_binder_thread_ilocked(m
, rb_entry(n
, struct binder_thread
,
5269 rb_node
), print_all
);
5271 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
)) {
5272 struct binder_node
*node
= rb_entry(n
, struct binder_node
,
5275 * take a temporary reference on the node so it
5276 * survives and isn't removed from the tree
5277 * while we print it.
5279 binder_inc_node_tmpref_ilocked(node
);
5280 /* Need to drop inner lock to take node lock */
5281 binder_inner_proc_unlock(proc
);
5283 binder_put_node(last_node
);
5284 binder_node_inner_lock(node
);
5285 print_binder_node_nilocked(m
, node
);
5286 binder_node_inner_unlock(node
);
5288 binder_inner_proc_lock(proc
);
5290 binder_inner_proc_unlock(proc
);
5292 binder_put_node(last_node
);
5295 binder_proc_lock(proc
);
5296 for (n
= rb_first(&proc
->refs_by_desc
);
5299 print_binder_ref_olocked(m
, rb_entry(n
,
5302 binder_proc_unlock(proc
);
5304 binder_alloc_print_allocated(m
, &proc
->alloc
);
5305 binder_inner_proc_lock(proc
);
5306 list_for_each_entry(w
, &proc
->todo
, entry
)
5307 print_binder_work_ilocked(m
, proc
, " ",
5308 " pending transaction", w
);
5309 list_for_each_entry(w
, &proc
->delivered_death
, entry
) {
5310 seq_puts(m
, " has delivered dead binder\n");
5313 binder_inner_proc_unlock(proc
);
5314 if (!print_all
&& m
->count
== header_pos
)
5315 m
->count
= start_pos
;
5318 static const char * const binder_return_strings
[] = {
5323 "BR_ACQUIRE_RESULT",
5325 "BR_TRANSACTION_COMPLETE",
5330 "BR_ATTEMPT_ACQUIRE",
5335 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5339 static const char * const binder_command_strings
[] = {
5342 "BC_ACQUIRE_RESULT",
5350 "BC_ATTEMPT_ACQUIRE",
5351 "BC_REGISTER_LOOPER",
5354 "BC_REQUEST_DEATH_NOTIFICATION",
5355 "BC_CLEAR_DEATH_NOTIFICATION",
5356 "BC_DEAD_BINDER_DONE",
5357 "BC_TRANSACTION_SG",
5361 static const char * const binder_objstat_strings
[] = {
5368 "transaction_complete"
5371 static void print_binder_stats(struct seq_file
*m
, const char *prefix
,
5372 struct binder_stats
*stats
)
5376 BUILD_BUG_ON(ARRAY_SIZE(stats
->bc
) !=
5377 ARRAY_SIZE(binder_command_strings
));
5378 for (i
= 0; i
< ARRAY_SIZE(stats
->bc
); i
++) {
5379 int temp
= atomic_read(&stats
->bc
[i
]);
5382 seq_printf(m
, "%s%s: %d\n", prefix
,
5383 binder_command_strings
[i
], temp
);
5386 BUILD_BUG_ON(ARRAY_SIZE(stats
->br
) !=
5387 ARRAY_SIZE(binder_return_strings
));
5388 for (i
= 0; i
< ARRAY_SIZE(stats
->br
); i
++) {
5389 int temp
= atomic_read(&stats
->br
[i
]);
5392 seq_printf(m
, "%s%s: %d\n", prefix
,
5393 binder_return_strings
[i
], temp
);
5396 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5397 ARRAY_SIZE(binder_objstat_strings
));
5398 BUILD_BUG_ON(ARRAY_SIZE(stats
->obj_created
) !=
5399 ARRAY_SIZE(stats
->obj_deleted
));
5400 for (i
= 0; i
< ARRAY_SIZE(stats
->obj_created
); i
++) {
5401 int created
= atomic_read(&stats
->obj_created
[i
]);
5402 int deleted
= atomic_read(&stats
->obj_deleted
[i
]);
5404 if (created
|| deleted
)
5405 seq_printf(m
, "%s%s: active %d total %d\n",
5407 binder_objstat_strings
[i
],
5413 static void print_binder_proc_stats(struct seq_file
*m
,
5414 struct binder_proc
*proc
)
5416 struct binder_work
*w
;
5417 struct binder_thread
*thread
;
5419 int count
, strong
, weak
, ready_threads
;
5420 size_t free_async_space
=
5421 binder_alloc_get_free_async_space(&proc
->alloc
);
5423 seq_printf(m
, "proc %d\n", proc
->pid
);
5424 seq_printf(m
, "context %s\n", proc
->context
->name
);
5427 binder_inner_proc_lock(proc
);
5428 for (n
= rb_first(&proc
->threads
); n
!= NULL
; n
= rb_next(n
))
5431 list_for_each_entry(thread
, &proc
->waiting_threads
, waiting_thread_node
)
5434 seq_printf(m
, " threads: %d\n", count
);
5435 seq_printf(m
, " requested threads: %d+%d/%d\n"
5436 " ready threads %d\n"
5437 " free async space %zd\n", proc
->requested_threads
,
5438 proc
->requested_threads_started
, proc
->max_threads
,
5442 for (n
= rb_first(&proc
->nodes
); n
!= NULL
; n
= rb_next(n
))
5444 binder_inner_proc_unlock(proc
);
5445 seq_printf(m
, " nodes: %d\n", count
);
5449 binder_proc_lock(proc
);
5450 for (n
= rb_first(&proc
->refs_by_desc
); n
!= NULL
; n
= rb_next(n
)) {
5451 struct binder_ref
*ref
= rb_entry(n
, struct binder_ref
,
5454 strong
+= ref
->data
.strong
;
5455 weak
+= ref
->data
.weak
;
5457 binder_proc_unlock(proc
);
5458 seq_printf(m
, " refs: %d s %d w %d\n", count
, strong
, weak
);
5460 count
= binder_alloc_get_allocated_count(&proc
->alloc
);
5461 seq_printf(m
, " buffers: %d\n", count
);
5463 binder_alloc_print_pages(m
, &proc
->alloc
);
5466 binder_inner_proc_lock(proc
);
5467 list_for_each_entry(w
, &proc
->todo
, entry
) {
5468 if (w
->type
== BINDER_WORK_TRANSACTION
)
5471 binder_inner_proc_unlock(proc
);
5472 seq_printf(m
, " pending transactions: %d\n", count
);
5474 print_binder_stats(m
, " ", &proc
->stats
);
5478 static int binder_state_show(struct seq_file
*m
, void *unused
)
5480 struct binder_proc
*proc
;
5481 struct binder_node
*node
;
5482 struct binder_node
*last_node
= NULL
;
5484 seq_puts(m
, "binder state:\n");
5486 spin_lock(&binder_dead_nodes_lock
);
5487 if (!hlist_empty(&binder_dead_nodes
))
5488 seq_puts(m
, "dead nodes:\n");
5489 hlist_for_each_entry(node
, &binder_dead_nodes
, dead_node
) {
5491 * take a temporary reference on the node so it
5492 * survives and isn't removed from the list
5493 * while we print it.
5496 spin_unlock(&binder_dead_nodes_lock
);
5498 binder_put_node(last_node
);
5499 binder_node_lock(node
);
5500 print_binder_node_nilocked(m
, node
);
5501 binder_node_unlock(node
);
5503 spin_lock(&binder_dead_nodes_lock
);
5505 spin_unlock(&binder_dead_nodes_lock
);
5507 binder_put_node(last_node
);
5509 mutex_lock(&binder_procs_lock
);
5510 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5511 print_binder_proc(m
, proc
, 1);
5512 mutex_unlock(&binder_procs_lock
);
5517 static int binder_stats_show(struct seq_file
*m
, void *unused
)
5519 struct binder_proc
*proc
;
5521 seq_puts(m
, "binder stats:\n");
5523 print_binder_stats(m
, "", &binder_stats
);
5525 mutex_lock(&binder_procs_lock
);
5526 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5527 print_binder_proc_stats(m
, proc
);
5528 mutex_unlock(&binder_procs_lock
);
5533 static int binder_transactions_show(struct seq_file
*m
, void *unused
)
5535 struct binder_proc
*proc
;
5537 seq_puts(m
, "binder transactions:\n");
5538 mutex_lock(&binder_procs_lock
);
5539 hlist_for_each_entry(proc
, &binder_procs
, proc_node
)
5540 print_binder_proc(m
, proc
, 0);
5541 mutex_unlock(&binder_procs_lock
);
5546 static int binder_proc_show(struct seq_file
*m
, void *unused
)
5548 struct binder_proc
*itr
;
5549 int pid
= (unsigned long)m
->private;
5551 mutex_lock(&binder_procs_lock
);
5552 hlist_for_each_entry(itr
, &binder_procs
, proc_node
) {
5553 if (itr
->pid
== pid
) {
5554 seq_puts(m
, "binder proc state:\n");
5555 print_binder_proc(m
, itr
, 1);
5558 mutex_unlock(&binder_procs_lock
);
5563 static void print_binder_transaction_log_entry(struct seq_file
*m
,
5564 struct binder_transaction_log_entry
*e
)
5566 int debug_id
= READ_ONCE(e
->debug_id_done
);
5568 * read barrier to guarantee debug_id_done read before
5569 * we print the log values
5573 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
5574 e
->debug_id
, (e
->call_type
== 2) ? "reply" :
5575 ((e
->call_type
== 1) ? "async" : "call "), e
->from_proc
,
5576 e
->from_thread
, e
->to_proc
, e
->to_thread
, e
->context_name
,
5577 e
->to_node
, e
->target_handle
, e
->data_size
, e
->offsets_size
,
5578 e
->return_error
, e
->return_error_param
,
5579 e
->return_error_line
);
5581 * read-barrier to guarantee read of debug_id_done after
5582 * done printing the fields of the entry
5585 seq_printf(m
, debug_id
&& debug_id
== READ_ONCE(e
->debug_id_done
) ?
5586 "\n" : " (incomplete)\n");
5589 static int binder_transaction_log_show(struct seq_file
*m
, void *unused
)
5591 struct binder_transaction_log
*log
= m
->private;
5592 unsigned int log_cur
= atomic_read(&log
->cur
);
5597 count
= log_cur
+ 1;
5598 cur
= count
< ARRAY_SIZE(log
->entry
) && !log
->full
?
5599 0 : count
% ARRAY_SIZE(log
->entry
);
5600 if (count
> ARRAY_SIZE(log
->entry
) || log
->full
)
5601 count
= ARRAY_SIZE(log
->entry
);
5602 for (i
= 0; i
< count
; i
++) {
5603 unsigned int index
= cur
++ % ARRAY_SIZE(log
->entry
);
5605 print_binder_transaction_log_entry(m
, &log
->entry
[index
]);
5610 static const struct file_operations binder_fops
= {
5611 .owner
= THIS_MODULE
,
5612 .poll
= binder_poll
,
5613 .unlocked_ioctl
= binder_ioctl
,
5614 .compat_ioctl
= binder_ioctl
,
5615 .mmap
= binder_mmap
,
5616 .open
= binder_open
,
5617 .flush
= binder_flush
,
5618 .release
= binder_release
,
5621 BINDER_DEBUG_ENTRY(state
);
5622 BINDER_DEBUG_ENTRY(stats
);
5623 BINDER_DEBUG_ENTRY(transactions
);
5624 BINDER_DEBUG_ENTRY(transaction_log
);
5626 static int __init
init_binder_device(const char *name
)
5629 struct binder_device
*binder_device
;
5631 binder_device
= kzalloc(sizeof(*binder_device
), GFP_KERNEL
);
5635 binder_device
->miscdev
.fops
= &binder_fops
;
5636 binder_device
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
5637 binder_device
->miscdev
.name
= name
;
5639 binder_device
->context
.binder_context_mgr_uid
= INVALID_UID
;
5640 binder_device
->context
.name
= name
;
5641 mutex_init(&binder_device
->context
.context_mgr_node_lock
);
5643 ret
= misc_register(&binder_device
->miscdev
);
5645 kfree(binder_device
);
5649 hlist_add_head(&binder_device
->hlist
, &binder_devices
);
5654 static int __init
binder_init(void)
5657 char *device_name
, *device_names
, *device_tmp
;
5658 struct binder_device
*device
;
5659 struct hlist_node
*tmp
;
5661 ret
= binder_alloc_shrinker_init();
5665 atomic_set(&binder_transaction_log
.cur
, ~0U);
5666 atomic_set(&binder_transaction_log_failed
.cur
, ~0U);
5668 binder_debugfs_dir_entry_root
= debugfs_create_dir("binder", NULL
);
5669 if (binder_debugfs_dir_entry_root
)
5670 binder_debugfs_dir_entry_proc
= debugfs_create_dir("proc",
5671 binder_debugfs_dir_entry_root
);
5673 if (binder_debugfs_dir_entry_root
) {
5674 debugfs_create_file("state",
5676 binder_debugfs_dir_entry_root
,
5678 &binder_state_fops
);
5679 debugfs_create_file("stats",
5681 binder_debugfs_dir_entry_root
,
5683 &binder_stats_fops
);
5684 debugfs_create_file("transactions",
5686 binder_debugfs_dir_entry_root
,
5688 &binder_transactions_fops
);
5689 debugfs_create_file("transaction_log",
5691 binder_debugfs_dir_entry_root
,
5692 &binder_transaction_log
,
5693 &binder_transaction_log_fops
);
5694 debugfs_create_file("failed_transaction_log",
5696 binder_debugfs_dir_entry_root
,
5697 &binder_transaction_log_failed
,
5698 &binder_transaction_log_fops
);
5702 * Copy the module_parameter string, because we don't want to
5703 * tokenize it in-place.
5705 device_names
= kzalloc(strlen(binder_devices_param
) + 1, GFP_KERNEL
);
5706 if (!device_names
) {
5708 goto err_alloc_device_names_failed
;
5710 strcpy(device_names
, binder_devices_param
);
5712 device_tmp
= device_names
;
5713 while ((device_name
= strsep(&device_tmp
, ","))) {
5714 ret
= init_binder_device(device_name
);
5716 goto err_init_binder_device_failed
;
5721 err_init_binder_device_failed
:
5722 hlist_for_each_entry_safe(device
, tmp
, &binder_devices
, hlist
) {
5723 misc_deregister(&device
->miscdev
);
5724 hlist_del(&device
->hlist
);
5728 kfree(device_names
);
5730 err_alloc_device_names_failed
:
5731 debugfs_remove_recursive(binder_debugfs_dir_entry_root
);
5736 device_initcall(binder_init
);
5738 #define CREATE_TRACE_POINTS
5739 #include "binder_trace.h"
5741 MODULE_LICENSE("GPL v2");