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