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