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