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 | ||
18 | #include <asm/cacheflush.h> | |
19 | #include <linux/fdtable.h> | |
20 | #include <linux/file.h> | |
21 | #include <linux/fs.h> | |
22 | #include <linux/list.h> | |
23 | #include <linux/miscdevice.h> | |
24 | #include <linux/mm.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/mutex.h> | |
27 | #include <linux/nsproxy.h> | |
28 | #include <linux/poll.h> | |
16b66554 | 29 | #include <linux/debugfs.h> |
355b0502 GKH |
30 | #include <linux/rbtree.h> |
31 | #include <linux/sched.h> | |
5249f488 | 32 | #include <linux/seq_file.h> |
355b0502 GKH |
33 | #include <linux/uaccess.h> |
34 | #include <linux/vmalloc.h> | |
c11a166c | 35 | #include <linux/slab.h> |
355b0502 GKH |
36 | |
37 | #include "binder.h" | |
975a1ac9 | 38 | #include "binder_trace.h" |
355b0502 | 39 | |
975a1ac9 | 40 | static DEFINE_MUTEX(binder_main_lock); |
355b0502 | 41 | static DEFINE_MUTEX(binder_deferred_lock); |
bd1eff97 | 42 | static DEFINE_MUTEX(binder_mmap_lock); |
355b0502 GKH |
43 | |
44 | static HLIST_HEAD(binder_procs); | |
45 | static HLIST_HEAD(binder_deferred_list); | |
46 | static HLIST_HEAD(binder_dead_nodes); | |
47 | ||
16b66554 AH |
48 | static struct dentry *binder_debugfs_dir_entry_root; |
49 | static struct dentry *binder_debugfs_dir_entry_proc; | |
355b0502 | 50 | static struct binder_node *binder_context_mgr_node; |
4a2ebb93 | 51 | static kuid_t binder_context_mgr_uid = INVALID_UID; |
355b0502 | 52 | static int binder_last_id; |
3c762a49 | 53 | static struct workqueue_struct *binder_deferred_workqueue; |
355b0502 | 54 | |
5249f488 AH |
55 | #define BINDER_DEBUG_ENTRY(name) \ |
56 | static int binder_##name##_open(struct inode *inode, struct file *file) \ | |
57 | { \ | |
16b66554 | 58 | return single_open(file, binder_##name##_show, inode->i_private); \ |
5249f488 AH |
59 | } \ |
60 | \ | |
61 | static const struct file_operations binder_##name##_fops = { \ | |
62 | .owner = THIS_MODULE, \ | |
63 | .open = binder_##name##_open, \ | |
64 | .read = seq_read, \ | |
65 | .llseek = seq_lseek, \ | |
66 | .release = single_release, \ | |
67 | } | |
68 | ||
69 | static int binder_proc_show(struct seq_file *m, void *unused); | |
70 | BINDER_DEBUG_ENTRY(proc); | |
355b0502 GKH |
71 | |
72 | /* This is only defined in include/asm-arm/sizes.h */ | |
73 | #ifndef SZ_1K | |
74 | #define SZ_1K 0x400 | |
75 | #endif | |
76 | ||
77 | #ifndef SZ_4M | |
78 | #define SZ_4M 0x400000 | |
79 | #endif | |
80 | ||
81 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) | |
82 | ||
83 | #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64) | |
84 | ||
85 | enum { | |
86 | BINDER_DEBUG_USER_ERROR = 1U << 0, | |
87 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, | |
88 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, | |
89 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, | |
90 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, | |
91 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, | |
92 | BINDER_DEBUG_READ_WRITE = 1U << 6, | |
93 | BINDER_DEBUG_USER_REFS = 1U << 7, | |
94 | BINDER_DEBUG_THREADS = 1U << 8, | |
95 | BINDER_DEBUG_TRANSACTION = 1U << 9, | |
96 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, | |
97 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, | |
98 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, | |
99 | BINDER_DEBUG_BUFFER_ALLOC = 1U << 13, | |
100 | BINDER_DEBUG_PRIORITY_CAP = 1U << 14, | |
101 | BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15, | |
102 | }; | |
103 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | | |
104 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; | |
105 | module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO); | |
106 | ||
2c52325e | 107 | static bool binder_debug_no_lock; |
355b0502 GKH |
108 | module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO); |
109 | ||
110 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); | |
111 | static int binder_stop_on_user_error; | |
112 | ||
113 | static int binder_set_stop_on_user_error(const char *val, | |
114 | struct kernel_param *kp) | |
115 | { | |
116 | int ret; | |
117 | ret = param_set_int(val, kp); | |
118 | if (binder_stop_on_user_error < 2) | |
119 | wake_up(&binder_user_error_wait); | |
120 | return ret; | |
121 | } | |
122 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, | |
123 | param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO); | |
124 | ||
125 | #define binder_debug(mask, x...) \ | |
126 | do { \ | |
127 | if (binder_debug_mask & mask) \ | |
258767fe | 128 | pr_info(x); \ |
355b0502 GKH |
129 | } while (0) |
130 | ||
131 | #define binder_user_error(x...) \ | |
132 | do { \ | |
133 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ | |
258767fe | 134 | pr_info(x); \ |
355b0502 GKH |
135 | if (binder_stop_on_user_error) \ |
136 | binder_stop_on_user_error = 2; \ | |
137 | } while (0) | |
138 | ||
139 | enum binder_stat_types { | |
140 | BINDER_STAT_PROC, | |
141 | BINDER_STAT_THREAD, | |
142 | BINDER_STAT_NODE, | |
143 | BINDER_STAT_REF, | |
144 | BINDER_STAT_DEATH, | |
145 | BINDER_STAT_TRANSACTION, | |
146 | BINDER_STAT_TRANSACTION_COMPLETE, | |
147 | BINDER_STAT_COUNT | |
148 | }; | |
149 | ||
150 | struct binder_stats { | |
151 | int br[_IOC_NR(BR_FAILED_REPLY) + 1]; | |
152 | int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1]; | |
153 | int obj_created[BINDER_STAT_COUNT]; | |
154 | int obj_deleted[BINDER_STAT_COUNT]; | |
155 | }; | |
156 | ||
157 | static struct binder_stats binder_stats; | |
158 | ||
159 | static inline void binder_stats_deleted(enum binder_stat_types type) | |
160 | { | |
161 | binder_stats.obj_deleted[type]++; | |
162 | } | |
163 | ||
164 | static inline void binder_stats_created(enum binder_stat_types type) | |
165 | { | |
166 | binder_stats.obj_created[type]++; | |
167 | } | |
168 | ||
169 | struct binder_transaction_log_entry { | |
170 | int debug_id; | |
171 | int call_type; | |
172 | int from_proc; | |
173 | int from_thread; | |
174 | int target_handle; | |
175 | int to_proc; | |
176 | int to_thread; | |
177 | int to_node; | |
178 | int data_size; | |
179 | int offsets_size; | |
180 | }; | |
181 | struct binder_transaction_log { | |
182 | int next; | |
183 | int full; | |
184 | struct binder_transaction_log_entry entry[32]; | |
185 | }; | |
186 | static struct binder_transaction_log binder_transaction_log; | |
187 | static struct binder_transaction_log binder_transaction_log_failed; | |
188 | ||
189 | static struct binder_transaction_log_entry *binder_transaction_log_add( | |
190 | struct binder_transaction_log *log) | |
191 | { | |
192 | struct binder_transaction_log_entry *e; | |
193 | e = &log->entry[log->next]; | |
194 | memset(e, 0, sizeof(*e)); | |
195 | log->next++; | |
196 | if (log->next == ARRAY_SIZE(log->entry)) { | |
197 | log->next = 0; | |
198 | log->full = 1; | |
199 | } | |
200 | return e; | |
201 | } | |
202 | ||
203 | struct binder_work { | |
204 | struct list_head entry; | |
205 | enum { | |
206 | BINDER_WORK_TRANSACTION = 1, | |
207 | BINDER_WORK_TRANSACTION_COMPLETE, | |
208 | BINDER_WORK_NODE, | |
209 | BINDER_WORK_DEAD_BINDER, | |
210 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, | |
211 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, | |
212 | } type; | |
213 | }; | |
214 | ||
215 | struct binder_node { | |
216 | int debug_id; | |
217 | struct binder_work work; | |
218 | union { | |
219 | struct rb_node rb_node; | |
220 | struct hlist_node dead_node; | |
221 | }; | |
222 | struct binder_proc *proc; | |
223 | struct hlist_head refs; | |
224 | int internal_strong_refs; | |
225 | int local_weak_refs; | |
226 | int local_strong_refs; | |
227 | void __user *ptr; | |
228 | void __user *cookie; | |
229 | unsigned has_strong_ref:1; | |
230 | unsigned pending_strong_ref:1; | |
231 | unsigned has_weak_ref:1; | |
232 | unsigned pending_weak_ref:1; | |
233 | unsigned has_async_transaction:1; | |
234 | unsigned accept_fds:1; | |
235 | unsigned min_priority:8; | |
236 | struct list_head async_todo; | |
237 | }; | |
238 | ||
239 | struct binder_ref_death { | |
240 | struct binder_work work; | |
241 | void __user *cookie; | |
242 | }; | |
243 | ||
244 | struct binder_ref { | |
245 | /* Lookups needed: */ | |
246 | /* node + proc => ref (transaction) */ | |
247 | /* desc + proc => ref (transaction, inc/dec ref) */ | |
248 | /* node => refs + procs (proc exit) */ | |
249 | int debug_id; | |
250 | struct rb_node rb_node_desc; | |
251 | struct rb_node rb_node_node; | |
252 | struct hlist_node node_entry; | |
253 | struct binder_proc *proc; | |
254 | struct binder_node *node; | |
255 | uint32_t desc; | |
256 | int strong; | |
257 | int weak; | |
258 | struct binder_ref_death *death; | |
259 | }; | |
260 | ||
261 | struct binder_buffer { | |
217218f0 | 262 | struct list_head entry; /* free and allocated entries by address */ |
355b0502 GKH |
263 | struct rb_node rb_node; /* free entry by size or allocated entry */ |
264 | /* by address */ | |
265 | unsigned free:1; | |
266 | unsigned allow_user_free:1; | |
267 | unsigned async_transaction:1; | |
268 | unsigned debug_id:29; | |
269 | ||
270 | struct binder_transaction *transaction; | |
271 | ||
272 | struct binder_node *target_node; | |
273 | size_t data_size; | |
274 | size_t offsets_size; | |
275 | uint8_t data[0]; | |
276 | }; | |
277 | ||
278 | enum binder_deferred_state { | |
279 | BINDER_DEFERRED_PUT_FILES = 0x01, | |
280 | BINDER_DEFERRED_FLUSH = 0x02, | |
281 | BINDER_DEFERRED_RELEASE = 0x04, | |
282 | }; | |
283 | ||
284 | struct binder_proc { | |
285 | struct hlist_node proc_node; | |
286 | struct rb_root threads; | |
287 | struct rb_root nodes; | |
288 | struct rb_root refs_by_desc; | |
289 | struct rb_root refs_by_node; | |
290 | int pid; | |
291 | struct vm_area_struct *vma; | |
2a90957f | 292 | struct mm_struct *vma_vm_mm; |
355b0502 GKH |
293 | struct task_struct *tsk; |
294 | struct files_struct *files; | |
295 | struct hlist_node deferred_work_node; | |
296 | int deferred_work; | |
297 | void *buffer; | |
298 | ptrdiff_t user_buffer_offset; | |
299 | ||
300 | struct list_head buffers; | |
301 | struct rb_root free_buffers; | |
302 | struct rb_root allocated_buffers; | |
303 | size_t free_async_space; | |
304 | ||
305 | struct page **pages; | |
306 | size_t buffer_size; | |
307 | uint32_t buffer_free; | |
308 | struct list_head todo; | |
309 | wait_queue_head_t wait; | |
310 | struct binder_stats stats; | |
311 | struct list_head delivered_death; | |
312 | int max_threads; | |
313 | int requested_threads; | |
314 | int requested_threads_started; | |
315 | int ready_threads; | |
316 | long default_priority; | |
16b66554 | 317 | struct dentry *debugfs_entry; |
355b0502 GKH |
318 | }; |
319 | ||
320 | enum { | |
321 | BINDER_LOOPER_STATE_REGISTERED = 0x01, | |
322 | BINDER_LOOPER_STATE_ENTERED = 0x02, | |
323 | BINDER_LOOPER_STATE_EXITED = 0x04, | |
324 | BINDER_LOOPER_STATE_INVALID = 0x08, | |
325 | BINDER_LOOPER_STATE_WAITING = 0x10, | |
326 | BINDER_LOOPER_STATE_NEED_RETURN = 0x20 | |
327 | }; | |
328 | ||
329 | struct binder_thread { | |
330 | struct binder_proc *proc; | |
331 | struct rb_node rb_node; | |
332 | int pid; | |
333 | int looper; | |
334 | struct binder_transaction *transaction_stack; | |
335 | struct list_head todo; | |
336 | uint32_t return_error; /* Write failed, return error code in read buf */ | |
337 | uint32_t return_error2; /* Write failed, return error code in read */ | |
338 | /* buffer. Used when sending a reply to a dead process that */ | |
339 | /* we are also waiting on */ | |
340 | wait_queue_head_t wait; | |
341 | struct binder_stats stats; | |
342 | }; | |
343 | ||
344 | struct binder_transaction { | |
345 | int debug_id; | |
346 | struct binder_work work; | |
347 | struct binder_thread *from; | |
348 | struct binder_transaction *from_parent; | |
349 | struct binder_proc *to_proc; | |
350 | struct binder_thread *to_thread; | |
351 | struct binder_transaction *to_parent; | |
352 | unsigned need_reply:1; | |
353 | /* unsigned is_dead:1; */ /* not used at the moment */ | |
354 | ||
355 | struct binder_buffer *buffer; | |
356 | unsigned int code; | |
357 | unsigned int flags; | |
358 | long priority; | |
359 | long saved_priority; | |
4a2ebb93 | 360 | kuid_t sender_euid; |
355b0502 GKH |
361 | }; |
362 | ||
363 | static void | |
364 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); | |
365 | ||
efde99cd | 366 | static int task_get_unused_fd_flags(struct binder_proc *proc, int flags) |
355b0502 GKH |
367 | { |
368 | struct files_struct *files = proc->files; | |
355b0502 GKH |
369 | unsigned long rlim_cur; |
370 | unsigned long irqs; | |
371 | ||
372 | if (files == NULL) | |
373 | return -ESRCH; | |
374 | ||
dcfadfa4 AV |
375 | if (!lock_task_sighand(proc->tsk, &irqs)) |
376 | return -EMFILE; | |
bf202361 | 377 | |
dcfadfa4 AV |
378 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
379 | unlock_task_sighand(proc->tsk, &irqs); | |
355b0502 | 380 | |
dcfadfa4 | 381 | return __alloc_fd(files, 0, rlim_cur, flags); |
355b0502 GKH |
382 | } |
383 | ||
384 | /* | |
385 | * copied from fd_install | |
386 | */ | |
387 | static void task_fd_install( | |
388 | struct binder_proc *proc, unsigned int fd, struct file *file) | |
389 | { | |
f869e8a7 AV |
390 | if (proc->files) |
391 | __fd_install(proc->files, fd, file); | |
355b0502 GKH |
392 | } |
393 | ||
394 | /* | |
395 | * copied from sys_close | |
396 | */ | |
397 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) | |
398 | { | |
355b0502 GKH |
399 | int retval; |
400 | ||
483ce1d4 | 401 | if (proc->files == NULL) |
355b0502 GKH |
402 | return -ESRCH; |
403 | ||
483ce1d4 | 404 | retval = __close_fd(proc->files, fd); |
355b0502 GKH |
405 | /* can't restart close syscall because file table entry was cleared */ |
406 | if (unlikely(retval == -ERESTARTSYS || | |
407 | retval == -ERESTARTNOINTR || | |
408 | retval == -ERESTARTNOHAND || | |
409 | retval == -ERESTART_RESTARTBLOCK)) | |
410 | retval = -EINTR; | |
411 | ||
412 | return retval; | |
355b0502 GKH |
413 | } |
414 | ||
975a1ac9 AH |
415 | static inline void binder_lock(const char *tag) |
416 | { | |
417 | trace_binder_lock(tag); | |
418 | mutex_lock(&binder_main_lock); | |
419 | trace_binder_locked(tag); | |
420 | } | |
421 | ||
422 | static inline void binder_unlock(const char *tag) | |
423 | { | |
424 | trace_binder_unlock(tag); | |
425 | mutex_unlock(&binder_main_lock); | |
426 | } | |
427 | ||
355b0502 GKH |
428 | static void binder_set_nice(long nice) |
429 | { | |
430 | long min_nice; | |
431 | if (can_nice(current, nice)) { | |
432 | set_user_nice(current, nice); | |
433 | return; | |
434 | } | |
435 | min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur; | |
436 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, | |
437 | "binder: %d: nice value %ld not allowed use " | |
438 | "%ld instead\n", current->pid, nice, min_nice); | |
439 | set_user_nice(current, min_nice); | |
440 | if (min_nice < 20) | |
441 | return; | |
442 | binder_user_error("binder: %d RLIMIT_NICE not set\n", current->pid); | |
443 | } | |
444 | ||
445 | static size_t binder_buffer_size(struct binder_proc *proc, | |
446 | struct binder_buffer *buffer) | |
447 | { | |
448 | if (list_is_last(&buffer->entry, &proc->buffers)) | |
449 | return proc->buffer + proc->buffer_size - (void *)buffer->data; | |
450 | else | |
451 | return (size_t)list_entry(buffer->entry.next, | |
452 | struct binder_buffer, entry) - (size_t)buffer->data; | |
453 | } | |
454 | ||
455 | static void binder_insert_free_buffer(struct binder_proc *proc, | |
456 | struct binder_buffer *new_buffer) | |
457 | { | |
458 | struct rb_node **p = &proc->free_buffers.rb_node; | |
459 | struct rb_node *parent = NULL; | |
460 | struct binder_buffer *buffer; | |
461 | size_t buffer_size; | |
462 | size_t new_buffer_size; | |
463 | ||
464 | BUG_ON(!new_buffer->free); | |
465 | ||
466 | new_buffer_size = binder_buffer_size(proc, new_buffer); | |
467 | ||
468 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
469 | "binder: %d: add free buffer, size %zd, " | |
470 | "at %p\n", proc->pid, new_buffer_size, new_buffer); | |
471 | ||
472 | while (*p) { | |
473 | parent = *p; | |
474 | buffer = rb_entry(parent, struct binder_buffer, rb_node); | |
475 | BUG_ON(!buffer->free); | |
476 | ||
477 | buffer_size = binder_buffer_size(proc, buffer); | |
478 | ||
479 | if (new_buffer_size < buffer_size) | |
480 | p = &parent->rb_left; | |
481 | else | |
482 | p = &parent->rb_right; | |
483 | } | |
484 | rb_link_node(&new_buffer->rb_node, parent, p); | |
485 | rb_insert_color(&new_buffer->rb_node, &proc->free_buffers); | |
486 | } | |
487 | ||
488 | static void binder_insert_allocated_buffer(struct binder_proc *proc, | |
489 | struct binder_buffer *new_buffer) | |
490 | { | |
491 | struct rb_node **p = &proc->allocated_buffers.rb_node; | |
492 | struct rb_node *parent = NULL; | |
493 | struct binder_buffer *buffer; | |
494 | ||
495 | BUG_ON(new_buffer->free); | |
496 | ||
497 | while (*p) { | |
498 | parent = *p; | |
499 | buffer = rb_entry(parent, struct binder_buffer, rb_node); | |
500 | BUG_ON(buffer->free); | |
501 | ||
502 | if (new_buffer < buffer) | |
503 | p = &parent->rb_left; | |
504 | else if (new_buffer > buffer) | |
505 | p = &parent->rb_right; | |
506 | else | |
507 | BUG(); | |
508 | } | |
509 | rb_link_node(&new_buffer->rb_node, parent, p); | |
510 | rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers); | |
511 | } | |
512 | ||
513 | static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc, | |
514 | void __user *user_ptr) | |
515 | { | |
516 | struct rb_node *n = proc->allocated_buffers.rb_node; | |
517 | struct binder_buffer *buffer; | |
518 | struct binder_buffer *kern_ptr; | |
519 | ||
520 | kern_ptr = user_ptr - proc->user_buffer_offset | |
521 | - offsetof(struct binder_buffer, data); | |
522 | ||
523 | while (n) { | |
524 | buffer = rb_entry(n, struct binder_buffer, rb_node); | |
525 | BUG_ON(buffer->free); | |
526 | ||
527 | if (kern_ptr < buffer) | |
528 | n = n->rb_left; | |
529 | else if (kern_ptr > buffer) | |
530 | n = n->rb_right; | |
531 | else | |
532 | return buffer; | |
533 | } | |
534 | return NULL; | |
535 | } | |
536 | ||
537 | static int binder_update_page_range(struct binder_proc *proc, int allocate, | |
538 | void *start, void *end, | |
539 | struct vm_area_struct *vma) | |
540 | { | |
541 | void *page_addr; | |
542 | unsigned long user_page_addr; | |
543 | struct vm_struct tmp_area; | |
544 | struct page **page; | |
545 | struct mm_struct *mm; | |
546 | ||
547 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
548 | "binder: %d: %s pages %p-%p\n", proc->pid, | |
549 | allocate ? "allocate" : "free", start, end); | |
550 | ||
551 | if (end <= start) | |
552 | return 0; | |
553 | ||
975a1ac9 AH |
554 | trace_binder_update_page_range(proc, allocate, start, end); |
555 | ||
355b0502 GKH |
556 | if (vma) |
557 | mm = NULL; | |
558 | else | |
559 | mm = get_task_mm(proc->tsk); | |
560 | ||
561 | if (mm) { | |
562 | down_write(&mm->mmap_sem); | |
563 | vma = proc->vma; | |
2a90957f | 564 | if (vma && mm != proc->vma_vm_mm) { |
bd1eff97 AH |
565 | pr_err("binder: %d: vma mm and task mm mismatch\n", |
566 | proc->pid); | |
567 | vma = NULL; | |
568 | } | |
355b0502 GKH |
569 | } |
570 | ||
571 | if (allocate == 0) | |
572 | goto free_range; | |
573 | ||
574 | if (vma == NULL) { | |
258767fe | 575 | pr_err("binder: %d: binder_alloc_buf failed to " |
355b0502 GKH |
576 | "map pages in userspace, no vma\n", proc->pid); |
577 | goto err_no_vma; | |
578 | } | |
579 | ||
580 | for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) { | |
581 | int ret; | |
582 | struct page **page_array_ptr; | |
583 | page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE]; | |
584 | ||
585 | BUG_ON(*page); | |
585650dc | 586 | *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); |
355b0502 | 587 | if (*page == NULL) { |
258767fe | 588 | pr_err("binder: %d: binder_alloc_buf failed " |
355b0502 GKH |
589 | "for page at %p\n", proc->pid, page_addr); |
590 | goto err_alloc_page_failed; | |
591 | } | |
592 | tmp_area.addr = page_addr; | |
593 | tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */; | |
594 | page_array_ptr = page; | |
595 | ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr); | |
596 | if (ret) { | |
258767fe | 597 | pr_err("binder: %d: binder_alloc_buf failed " |
355b0502 GKH |
598 | "to map page at %p in kernel\n", |
599 | proc->pid, page_addr); | |
600 | goto err_map_kernel_failed; | |
601 | } | |
602 | user_page_addr = | |
603 | (uintptr_t)page_addr + proc->user_buffer_offset; | |
604 | ret = vm_insert_page(vma, user_page_addr, page[0]); | |
605 | if (ret) { | |
258767fe | 606 | pr_err("binder: %d: binder_alloc_buf failed " |
355b0502 GKH |
607 | "to map page at %lx in userspace\n", |
608 | proc->pid, user_page_addr); | |
609 | goto err_vm_insert_page_failed; | |
610 | } | |
611 | /* vm_insert_page does not seem to increment the refcount */ | |
612 | } | |
613 | if (mm) { | |
614 | up_write(&mm->mmap_sem); | |
615 | mmput(mm); | |
616 | } | |
617 | return 0; | |
618 | ||
619 | free_range: | |
620 | for (page_addr = end - PAGE_SIZE; page_addr >= start; | |
621 | page_addr -= PAGE_SIZE) { | |
622 | page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE]; | |
623 | if (vma) | |
624 | zap_page_range(vma, (uintptr_t)page_addr + | |
625 | proc->user_buffer_offset, PAGE_SIZE, NULL); | |
626 | err_vm_insert_page_failed: | |
627 | unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE); | |
628 | err_map_kernel_failed: | |
629 | __free_page(*page); | |
630 | *page = NULL; | |
631 | err_alloc_page_failed: | |
632 | ; | |
633 | } | |
634 | err_no_vma: | |
635 | if (mm) { | |
636 | up_write(&mm->mmap_sem); | |
637 | mmput(mm); | |
638 | } | |
639 | return -ENOMEM; | |
640 | } | |
641 | ||
642 | static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc, | |
643 | size_t data_size, | |
644 | size_t offsets_size, int is_async) | |
645 | { | |
646 | struct rb_node *n = proc->free_buffers.rb_node; | |
647 | struct binder_buffer *buffer; | |
648 | size_t buffer_size; | |
649 | struct rb_node *best_fit = NULL; | |
650 | void *has_page_addr; | |
651 | void *end_page_addr; | |
652 | size_t size; | |
653 | ||
654 | if (proc->vma == NULL) { | |
258767fe | 655 | pr_err("binder: %d: binder_alloc_buf, no vma\n", |
355b0502 GKH |
656 | proc->pid); |
657 | return NULL; | |
658 | } | |
659 | ||
660 | size = ALIGN(data_size, sizeof(void *)) + | |
661 | ALIGN(offsets_size, sizeof(void *)); | |
662 | ||
663 | if (size < data_size || size < offsets_size) { | |
664 | binder_user_error("binder: %d: got transaction with invalid " | |
665 | "size %zd-%zd\n", proc->pid, data_size, offsets_size); | |
666 | return NULL; | |
667 | } | |
668 | ||
669 | if (is_async && | |
670 | proc->free_async_space < size + sizeof(struct binder_buffer)) { | |
671 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
672 | "binder: %d: binder_alloc_buf size %zd" | |
673 | "failed, no async space left\n", proc->pid, size); | |
674 | return NULL; | |
675 | } | |
676 | ||
677 | while (n) { | |
678 | buffer = rb_entry(n, struct binder_buffer, rb_node); | |
679 | BUG_ON(!buffer->free); | |
680 | buffer_size = binder_buffer_size(proc, buffer); | |
681 | ||
682 | if (size < buffer_size) { | |
683 | best_fit = n; | |
684 | n = n->rb_left; | |
685 | } else if (size > buffer_size) | |
686 | n = n->rb_right; | |
687 | else { | |
688 | best_fit = n; | |
689 | break; | |
690 | } | |
691 | } | |
692 | if (best_fit == NULL) { | |
258767fe | 693 | pr_err("binder: %d: binder_alloc_buf size %zd failed, " |
355b0502 GKH |
694 | "no address space\n", proc->pid, size); |
695 | return NULL; | |
696 | } | |
697 | if (n == NULL) { | |
698 | buffer = rb_entry(best_fit, struct binder_buffer, rb_node); | |
699 | buffer_size = binder_buffer_size(proc, buffer); | |
700 | } | |
701 | ||
702 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
703 | "binder: %d: binder_alloc_buf size %zd got buff" | |
704 | "er %p size %zd\n", proc->pid, size, buffer, buffer_size); | |
705 | ||
706 | has_page_addr = | |
707 | (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK); | |
708 | if (n == NULL) { | |
709 | if (size + sizeof(struct binder_buffer) + 4 >= buffer_size) | |
710 | buffer_size = size; /* no room for other buffers */ | |
711 | else | |
712 | buffer_size = size + sizeof(struct binder_buffer); | |
713 | } | |
714 | end_page_addr = | |
715 | (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size); | |
716 | if (end_page_addr > has_page_addr) | |
717 | end_page_addr = has_page_addr; | |
718 | if (binder_update_page_range(proc, 1, | |
719 | (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL)) | |
720 | return NULL; | |
721 | ||
722 | rb_erase(best_fit, &proc->free_buffers); | |
723 | buffer->free = 0; | |
724 | binder_insert_allocated_buffer(proc, buffer); | |
725 | if (buffer_size != size) { | |
726 | struct binder_buffer *new_buffer = (void *)buffer->data + size; | |
727 | list_add(&new_buffer->entry, &buffer->entry); | |
728 | new_buffer->free = 1; | |
729 | binder_insert_free_buffer(proc, new_buffer); | |
730 | } | |
731 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
732 | "binder: %d: binder_alloc_buf size %zd got " | |
733 | "%p\n", proc->pid, size, buffer); | |
734 | buffer->data_size = data_size; | |
735 | buffer->offsets_size = offsets_size; | |
736 | buffer->async_transaction = is_async; | |
737 | if (is_async) { | |
738 | proc->free_async_space -= size + sizeof(struct binder_buffer); | |
739 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC, | |
740 | "binder: %d: binder_alloc_buf size %zd " | |
741 | "async free %zd\n", proc->pid, size, | |
742 | proc->free_async_space); | |
743 | } | |
744 | ||
745 | return buffer; | |
746 | } | |
747 | ||
748 | static void *buffer_start_page(struct binder_buffer *buffer) | |
749 | { | |
750 | return (void *)((uintptr_t)buffer & PAGE_MASK); | |
751 | } | |
752 | ||
753 | static void *buffer_end_page(struct binder_buffer *buffer) | |
754 | { | |
755 | return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK); | |
756 | } | |
757 | ||
758 | static void binder_delete_free_buffer(struct binder_proc *proc, | |
759 | struct binder_buffer *buffer) | |
760 | { | |
761 | struct binder_buffer *prev, *next = NULL; | |
762 | int free_page_end = 1; | |
763 | int free_page_start = 1; | |
764 | ||
765 | BUG_ON(proc->buffers.next == &buffer->entry); | |
766 | prev = list_entry(buffer->entry.prev, struct binder_buffer, entry); | |
767 | BUG_ON(!prev->free); | |
768 | if (buffer_end_page(prev) == buffer_start_page(buffer)) { | |
769 | free_page_start = 0; | |
770 | if (buffer_end_page(prev) == buffer_end_page(buffer)) | |
771 | free_page_end = 0; | |
772 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
773 | "binder: %d: merge free, buffer %p " | |
774 | "share page with %p\n", proc->pid, buffer, prev); | |
775 | } | |
776 | ||
777 | if (!list_is_last(&buffer->entry, &proc->buffers)) { | |
778 | next = list_entry(buffer->entry.next, | |
779 | struct binder_buffer, entry); | |
780 | if (buffer_start_page(next) == buffer_end_page(buffer)) { | |
781 | free_page_end = 0; | |
782 | if (buffer_start_page(next) == | |
783 | buffer_start_page(buffer)) | |
784 | free_page_start = 0; | |
785 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
786 | "binder: %d: merge free, buffer" | |
787 | " %p share page with %p\n", proc->pid, | |
788 | buffer, prev); | |
789 | } | |
790 | } | |
791 | list_del(&buffer->entry); | |
792 | if (free_page_start || free_page_end) { | |
793 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
794 | "binder: %d: merge free, buffer %p do " | |
795 | "not share page%s%s with with %p or %p\n", | |
796 | proc->pid, buffer, free_page_start ? "" : " end", | |
797 | free_page_end ? "" : " start", prev, next); | |
798 | binder_update_page_range(proc, 0, free_page_start ? | |
799 | buffer_start_page(buffer) : buffer_end_page(buffer), | |
800 | (free_page_end ? buffer_end_page(buffer) : | |
801 | buffer_start_page(buffer)) + PAGE_SIZE, NULL); | |
802 | } | |
803 | } | |
804 | ||
805 | static void binder_free_buf(struct binder_proc *proc, | |
806 | struct binder_buffer *buffer) | |
807 | { | |
808 | size_t size, buffer_size; | |
809 | ||
810 | buffer_size = binder_buffer_size(proc, buffer); | |
811 | ||
812 | size = ALIGN(buffer->data_size, sizeof(void *)) + | |
813 | ALIGN(buffer->offsets_size, sizeof(void *)); | |
814 | ||
815 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, | |
816 | "binder: %d: binder_free_buf %p size %zd buffer" | |
817 | "_size %zd\n", proc->pid, buffer, size, buffer_size); | |
818 | ||
819 | BUG_ON(buffer->free); | |
820 | BUG_ON(size > buffer_size); | |
821 | BUG_ON(buffer->transaction != NULL); | |
822 | BUG_ON((void *)buffer < proc->buffer); | |
823 | BUG_ON((void *)buffer > proc->buffer + proc->buffer_size); | |
824 | ||
825 | if (buffer->async_transaction) { | |
826 | proc->free_async_space += size + sizeof(struct binder_buffer); | |
827 | ||
828 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC, | |
829 | "binder: %d: binder_free_buf size %zd " | |
830 | "async free %zd\n", proc->pid, size, | |
831 | proc->free_async_space); | |
832 | } | |
833 | ||
834 | binder_update_page_range(proc, 0, | |
835 | (void *)PAGE_ALIGN((uintptr_t)buffer->data), | |
836 | (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK), | |
837 | NULL); | |
838 | rb_erase(&buffer->rb_node, &proc->allocated_buffers); | |
839 | buffer->free = 1; | |
840 | if (!list_is_last(&buffer->entry, &proc->buffers)) { | |
841 | struct binder_buffer *next = list_entry(buffer->entry.next, | |
842 | struct binder_buffer, entry); | |
843 | if (next->free) { | |
844 | rb_erase(&next->rb_node, &proc->free_buffers); | |
845 | binder_delete_free_buffer(proc, next); | |
846 | } | |
847 | } | |
848 | if (proc->buffers.next != &buffer->entry) { | |
849 | struct binder_buffer *prev = list_entry(buffer->entry.prev, | |
850 | struct binder_buffer, entry); | |
851 | if (prev->free) { | |
852 | binder_delete_free_buffer(proc, buffer); | |
853 | rb_erase(&prev->rb_node, &proc->free_buffers); | |
854 | buffer = prev; | |
855 | } | |
856 | } | |
857 | binder_insert_free_buffer(proc, buffer); | |
858 | } | |
859 | ||
860 | static struct binder_node *binder_get_node(struct binder_proc *proc, | |
861 | void __user *ptr) | |
862 | { | |
863 | struct rb_node *n = proc->nodes.rb_node; | |
864 | struct binder_node *node; | |
865 | ||
866 | while (n) { | |
867 | node = rb_entry(n, struct binder_node, rb_node); | |
868 | ||
869 | if (ptr < node->ptr) | |
870 | n = n->rb_left; | |
871 | else if (ptr > node->ptr) | |
872 | n = n->rb_right; | |
873 | else | |
874 | return node; | |
875 | } | |
876 | return NULL; | |
877 | } | |
878 | ||
879 | static struct binder_node *binder_new_node(struct binder_proc *proc, | |
880 | void __user *ptr, | |
881 | void __user *cookie) | |
882 | { | |
883 | struct rb_node **p = &proc->nodes.rb_node; | |
884 | struct rb_node *parent = NULL; | |
885 | struct binder_node *node; | |
886 | ||
887 | while (*p) { | |
888 | parent = *p; | |
889 | node = rb_entry(parent, struct binder_node, rb_node); | |
890 | ||
891 | if (ptr < node->ptr) | |
892 | p = &(*p)->rb_left; | |
893 | else if (ptr > node->ptr) | |
894 | p = &(*p)->rb_right; | |
895 | else | |
896 | return NULL; | |
897 | } | |
898 | ||
899 | node = kzalloc(sizeof(*node), GFP_KERNEL); | |
900 | if (node == NULL) | |
901 | return NULL; | |
902 | binder_stats_created(BINDER_STAT_NODE); | |
903 | rb_link_node(&node->rb_node, parent, p); | |
904 | rb_insert_color(&node->rb_node, &proc->nodes); | |
905 | node->debug_id = ++binder_last_id; | |
906 | node->proc = proc; | |
907 | node->ptr = ptr; | |
908 | node->cookie = cookie; | |
909 | node->work.type = BINDER_WORK_NODE; | |
910 | INIT_LIST_HEAD(&node->work.entry); | |
911 | INIT_LIST_HEAD(&node->async_todo); | |
912 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
913 | "binder: %d:%d node %d u%p c%p created\n", | |
914 | proc->pid, current->pid, node->debug_id, | |
915 | node->ptr, node->cookie); | |
916 | return node; | |
917 | } | |
918 | ||
919 | static int binder_inc_node(struct binder_node *node, int strong, int internal, | |
920 | struct list_head *target_list) | |
921 | { | |
922 | if (strong) { | |
923 | if (internal) { | |
924 | if (target_list == NULL && | |
925 | node->internal_strong_refs == 0 && | |
926 | !(node == binder_context_mgr_node && | |
927 | node->has_strong_ref)) { | |
258767fe | 928 | pr_err("binder: invalid inc strong " |
355b0502 GKH |
929 | "node for %d\n", node->debug_id); |
930 | return -EINVAL; | |
931 | } | |
932 | node->internal_strong_refs++; | |
933 | } else | |
934 | node->local_strong_refs++; | |
935 | if (!node->has_strong_ref && target_list) { | |
936 | list_del_init(&node->work.entry); | |
937 | list_add_tail(&node->work.entry, target_list); | |
938 | } | |
939 | } else { | |
940 | if (!internal) | |
941 | node->local_weak_refs++; | |
942 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { | |
943 | if (target_list == NULL) { | |
258767fe | 944 | pr_err("binder: invalid inc weak node " |
355b0502 GKH |
945 | "for %d\n", node->debug_id); |
946 | return -EINVAL; | |
947 | } | |
948 | list_add_tail(&node->work.entry, target_list); | |
949 | } | |
950 | } | |
951 | return 0; | |
952 | } | |
953 | ||
954 | static int binder_dec_node(struct binder_node *node, int strong, int internal) | |
955 | { | |
956 | if (strong) { | |
957 | if (internal) | |
958 | node->internal_strong_refs--; | |
959 | else | |
960 | node->local_strong_refs--; | |
961 | if (node->local_strong_refs || node->internal_strong_refs) | |
962 | return 0; | |
963 | } else { | |
964 | if (!internal) | |
965 | node->local_weak_refs--; | |
966 | if (node->local_weak_refs || !hlist_empty(&node->refs)) | |
967 | return 0; | |
968 | } | |
969 | if (node->proc && (node->has_strong_ref || node->has_weak_ref)) { | |
970 | if (list_empty(&node->work.entry)) { | |
971 | list_add_tail(&node->work.entry, &node->proc->todo); | |
972 | wake_up_interruptible(&node->proc->wait); | |
973 | } | |
974 | } else { | |
975 | if (hlist_empty(&node->refs) && !node->local_strong_refs && | |
976 | !node->local_weak_refs) { | |
977 | list_del_init(&node->work.entry); | |
978 | if (node->proc) { | |
979 | rb_erase(&node->rb_node, &node->proc->nodes); | |
980 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
981 | "binder: refless node %d deleted\n", | |
982 | node->debug_id); | |
983 | } else { | |
984 | hlist_del(&node->dead_node); | |
985 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
986 | "binder: dead node %d deleted\n", | |
987 | node->debug_id); | |
988 | } | |
989 | kfree(node); | |
990 | binder_stats_deleted(BINDER_STAT_NODE); | |
991 | } | |
992 | } | |
993 | ||
994 | return 0; | |
995 | } | |
996 | ||
997 | ||
998 | static struct binder_ref *binder_get_ref(struct binder_proc *proc, | |
999 | uint32_t desc) | |
1000 | { | |
1001 | struct rb_node *n = proc->refs_by_desc.rb_node; | |
1002 | struct binder_ref *ref; | |
1003 | ||
1004 | while (n) { | |
1005 | ref = rb_entry(n, struct binder_ref, rb_node_desc); | |
1006 | ||
1007 | if (desc < ref->desc) | |
1008 | n = n->rb_left; | |
1009 | else if (desc > ref->desc) | |
1010 | n = n->rb_right; | |
1011 | else | |
1012 | return ref; | |
1013 | } | |
1014 | return NULL; | |
1015 | } | |
1016 | ||
1017 | static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc, | |
1018 | struct binder_node *node) | |
1019 | { | |
1020 | struct rb_node *n; | |
1021 | struct rb_node **p = &proc->refs_by_node.rb_node; | |
1022 | struct rb_node *parent = NULL; | |
1023 | struct binder_ref *ref, *new_ref; | |
1024 | ||
1025 | while (*p) { | |
1026 | parent = *p; | |
1027 | ref = rb_entry(parent, struct binder_ref, rb_node_node); | |
1028 | ||
1029 | if (node < ref->node) | |
1030 | p = &(*p)->rb_left; | |
1031 | else if (node > ref->node) | |
1032 | p = &(*p)->rb_right; | |
1033 | else | |
1034 | return ref; | |
1035 | } | |
1036 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); | |
1037 | if (new_ref == NULL) | |
1038 | return NULL; | |
1039 | binder_stats_created(BINDER_STAT_REF); | |
1040 | new_ref->debug_id = ++binder_last_id; | |
1041 | new_ref->proc = proc; | |
1042 | new_ref->node = node; | |
1043 | rb_link_node(&new_ref->rb_node_node, parent, p); | |
1044 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); | |
1045 | ||
1046 | new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1; | |
1047 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { | |
1048 | ref = rb_entry(n, struct binder_ref, rb_node_desc); | |
1049 | if (ref->desc > new_ref->desc) | |
1050 | break; | |
1051 | new_ref->desc = ref->desc + 1; | |
1052 | } | |
1053 | ||
1054 | p = &proc->refs_by_desc.rb_node; | |
1055 | while (*p) { | |
1056 | parent = *p; | |
1057 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); | |
1058 | ||
1059 | if (new_ref->desc < ref->desc) | |
1060 | p = &(*p)->rb_left; | |
1061 | else if (new_ref->desc > ref->desc) | |
1062 | p = &(*p)->rb_right; | |
1063 | else | |
1064 | BUG(); | |
1065 | } | |
1066 | rb_link_node(&new_ref->rb_node_desc, parent, p); | |
1067 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); | |
1068 | if (node) { | |
1069 | hlist_add_head(&new_ref->node_entry, &node->refs); | |
1070 | ||
1071 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
1072 | "binder: %d new ref %d desc %d for " | |
1073 | "node %d\n", proc->pid, new_ref->debug_id, | |
1074 | new_ref->desc, node->debug_id); | |
1075 | } else { | |
1076 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
1077 | "binder: %d new ref %d desc %d for " | |
1078 | "dead node\n", proc->pid, new_ref->debug_id, | |
1079 | new_ref->desc); | |
1080 | } | |
1081 | return new_ref; | |
1082 | } | |
1083 | ||
1084 | static void binder_delete_ref(struct binder_ref *ref) | |
1085 | { | |
1086 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
1087 | "binder: %d delete ref %d desc %d for " | |
1088 | "node %d\n", ref->proc->pid, ref->debug_id, | |
1089 | ref->desc, ref->node->debug_id); | |
1090 | ||
1091 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); | |
1092 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); | |
1093 | if (ref->strong) | |
1094 | binder_dec_node(ref->node, 1, 1); | |
1095 | hlist_del(&ref->node_entry); | |
1096 | binder_dec_node(ref->node, 0, 1); | |
1097 | if (ref->death) { | |
1098 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | |
1099 | "binder: %d delete ref %d desc %d " | |
1100 | "has death notification\n", ref->proc->pid, | |
1101 | ref->debug_id, ref->desc); | |
1102 | list_del(&ref->death->work.entry); | |
1103 | kfree(ref->death); | |
1104 | binder_stats_deleted(BINDER_STAT_DEATH); | |
1105 | } | |
1106 | kfree(ref); | |
1107 | binder_stats_deleted(BINDER_STAT_REF); | |
1108 | } | |
1109 | ||
1110 | static int binder_inc_ref(struct binder_ref *ref, int strong, | |
1111 | struct list_head *target_list) | |
1112 | { | |
1113 | int ret; | |
1114 | if (strong) { | |
1115 | if (ref->strong == 0) { | |
1116 | ret = binder_inc_node(ref->node, 1, 1, target_list); | |
1117 | if (ret) | |
1118 | return ret; | |
1119 | } | |
1120 | ref->strong++; | |
1121 | } else { | |
1122 | if (ref->weak == 0) { | |
1123 | ret = binder_inc_node(ref->node, 0, 1, target_list); | |
1124 | if (ret) | |
1125 | return ret; | |
1126 | } | |
1127 | ref->weak++; | |
1128 | } | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | ||
1133 | static int binder_dec_ref(struct binder_ref *ref, int strong) | |
1134 | { | |
1135 | if (strong) { | |
1136 | if (ref->strong == 0) { | |
1137 | binder_user_error("binder: %d invalid dec strong, " | |
1138 | "ref %d desc %d s %d w %d\n", | |
1139 | ref->proc->pid, ref->debug_id, | |
1140 | ref->desc, ref->strong, ref->weak); | |
1141 | return -EINVAL; | |
1142 | } | |
1143 | ref->strong--; | |
1144 | if (ref->strong == 0) { | |
1145 | int ret; | |
1146 | ret = binder_dec_node(ref->node, strong, 1); | |
1147 | if (ret) | |
1148 | return ret; | |
1149 | } | |
1150 | } else { | |
1151 | if (ref->weak == 0) { | |
1152 | binder_user_error("binder: %d invalid dec weak, " | |
1153 | "ref %d desc %d s %d w %d\n", | |
1154 | ref->proc->pid, ref->debug_id, | |
1155 | ref->desc, ref->strong, ref->weak); | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | ref->weak--; | |
1159 | } | |
1160 | if (ref->strong == 0 && ref->weak == 0) | |
1161 | binder_delete_ref(ref); | |
1162 | return 0; | |
1163 | } | |
1164 | ||
1165 | static void binder_pop_transaction(struct binder_thread *target_thread, | |
1166 | struct binder_transaction *t) | |
1167 | { | |
1168 | if (target_thread) { | |
1169 | BUG_ON(target_thread->transaction_stack != t); | |
1170 | BUG_ON(target_thread->transaction_stack->from != target_thread); | |
1171 | target_thread->transaction_stack = | |
1172 | target_thread->transaction_stack->from_parent; | |
1173 | t->from = NULL; | |
1174 | } | |
1175 | t->need_reply = 0; | |
1176 | if (t->buffer) | |
1177 | t->buffer->transaction = NULL; | |
1178 | kfree(t); | |
1179 | binder_stats_deleted(BINDER_STAT_TRANSACTION); | |
1180 | } | |
1181 | ||
1182 | static void binder_send_failed_reply(struct binder_transaction *t, | |
1183 | uint32_t error_code) | |
1184 | { | |
1185 | struct binder_thread *target_thread; | |
1186 | BUG_ON(t->flags & TF_ONE_WAY); | |
1187 | while (1) { | |
1188 | target_thread = t->from; | |
1189 | if (target_thread) { | |
1190 | if (target_thread->return_error != BR_OK && | |
1191 | target_thread->return_error2 == BR_OK) { | |
1192 | target_thread->return_error2 = | |
1193 | target_thread->return_error; | |
1194 | target_thread->return_error = BR_OK; | |
1195 | } | |
1196 | if (target_thread->return_error == BR_OK) { | |
1197 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | |
1198 | "binder: send failed reply for " | |
1199 | "transaction %d to %d:%d\n", | |
1200 | t->debug_id, target_thread->proc->pid, | |
1201 | target_thread->pid); | |
1202 | ||
1203 | binder_pop_transaction(target_thread, t); | |
1204 | target_thread->return_error = error_code; | |
1205 | wake_up_interruptible(&target_thread->wait); | |
1206 | } else { | |
258767fe | 1207 | pr_err("binder: reply failed, target " |
355b0502 GKH |
1208 | "thread, %d:%d, has error code %d " |
1209 | "already\n", target_thread->proc->pid, | |
1210 | target_thread->pid, | |
1211 | target_thread->return_error); | |
1212 | } | |
1213 | return; | |
1214 | } else { | |
1215 | struct binder_transaction *next = t->from_parent; | |
1216 | ||
1217 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | |
1218 | "binder: send failed reply " | |
1219 | "for transaction %d, target dead\n", | |
1220 | t->debug_id); | |
1221 | ||
1222 | binder_pop_transaction(target_thread, t); | |
1223 | if (next == NULL) { | |
1224 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | |
1225 | "binder: reply failed," | |
1226 | " no target thread at root\n"); | |
1227 | return; | |
1228 | } | |
1229 | t = next; | |
1230 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | |
1231 | "binder: reply failed, no target " | |
1232 | "thread -- retry %d\n", t->debug_id); | |
1233 | } | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | static void binder_transaction_buffer_release(struct binder_proc *proc, | |
1238 | struct binder_buffer *buffer, | |
1239 | size_t *failed_at) | |
1240 | { | |
1241 | size_t *offp, *off_end; | |
1242 | int debug_id = buffer->debug_id; | |
1243 | ||
1244 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
1245 | "binder: %d buffer release %d, size %zd-%zd, failed at %p\n", | |
1246 | proc->pid, buffer->debug_id, | |
1247 | buffer->data_size, buffer->offsets_size, failed_at); | |
1248 | ||
1249 | if (buffer->target_node) | |
1250 | binder_dec_node(buffer->target_node, 1, 0); | |
1251 | ||
1252 | offp = (size_t *)(buffer->data + ALIGN(buffer->data_size, sizeof(void *))); | |
1253 | if (failed_at) | |
1254 | off_end = failed_at; | |
1255 | else | |
1256 | off_end = (void *)offp + buffer->offsets_size; | |
1257 | for (; offp < off_end; offp++) { | |
1258 | struct flat_binder_object *fp; | |
1259 | if (*offp > buffer->data_size - sizeof(*fp) || | |
1260 | buffer->data_size < sizeof(*fp) || | |
1261 | !IS_ALIGNED(*offp, sizeof(void *))) { | |
258767fe | 1262 | pr_err("binder: transaction release %d bad" |
355b0502 GKH |
1263 | "offset %zd, size %zd\n", debug_id, |
1264 | *offp, buffer->data_size); | |
1265 | continue; | |
1266 | } | |
1267 | fp = (struct flat_binder_object *)(buffer->data + *offp); | |
1268 | switch (fp->type) { | |
1269 | case BINDER_TYPE_BINDER: | |
1270 | case BINDER_TYPE_WEAK_BINDER: { | |
1271 | struct binder_node *node = binder_get_node(proc, fp->binder); | |
1272 | if (node == NULL) { | |
258767fe | 1273 | pr_err("binder: transaction release %d" |
355b0502 GKH |
1274 | " bad node %p\n", debug_id, fp->binder); |
1275 | break; | |
1276 | } | |
1277 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
1278 | " node %d u%p\n", | |
1279 | node->debug_id, node->ptr); | |
1280 | binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0); | |
1281 | } break; | |
1282 | case BINDER_TYPE_HANDLE: | |
1283 | case BINDER_TYPE_WEAK_HANDLE: { | |
1284 | struct binder_ref *ref = binder_get_ref(proc, fp->handle); | |
1285 | if (ref == NULL) { | |
258767fe | 1286 | pr_err("binder: transaction release %d" |
355b0502 GKH |
1287 | " bad handle %ld\n", debug_id, |
1288 | fp->handle); | |
1289 | break; | |
1290 | } | |
1291 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
1292 | " ref %d desc %d (node %d)\n", | |
1293 | ref->debug_id, ref->desc, ref->node->debug_id); | |
1294 | binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE); | |
1295 | } break; | |
1296 | ||
1297 | case BINDER_TYPE_FD: | |
1298 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
1299 | " fd %ld\n", fp->handle); | |
1300 | if (failed_at) | |
1301 | task_close_fd(proc, fp->handle); | |
1302 | break; | |
1303 | ||
1304 | default: | |
258767fe | 1305 | pr_err("binder: transaction release %d bad " |
355b0502 GKH |
1306 | "object type %lx\n", debug_id, fp->type); |
1307 | break; | |
1308 | } | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | static void binder_transaction(struct binder_proc *proc, | |
1313 | struct binder_thread *thread, | |
1314 | struct binder_transaction_data *tr, int reply) | |
1315 | { | |
1316 | struct binder_transaction *t; | |
1317 | struct binder_work *tcomplete; | |
1318 | size_t *offp, *off_end; | |
1319 | struct binder_proc *target_proc; | |
1320 | struct binder_thread *target_thread = NULL; | |
1321 | struct binder_node *target_node = NULL; | |
1322 | struct list_head *target_list; | |
1323 | wait_queue_head_t *target_wait; | |
1324 | struct binder_transaction *in_reply_to = NULL; | |
1325 | struct binder_transaction_log_entry *e; | |
1326 | uint32_t return_error; | |
1327 | ||
1328 | e = binder_transaction_log_add(&binder_transaction_log); | |
1329 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); | |
1330 | e->from_proc = proc->pid; | |
1331 | e->from_thread = thread->pid; | |
1332 | e->target_handle = tr->target.handle; | |
1333 | e->data_size = tr->data_size; | |
1334 | e->offsets_size = tr->offsets_size; | |
1335 | ||
1336 | if (reply) { | |
1337 | in_reply_to = thread->transaction_stack; | |
1338 | if (in_reply_to == NULL) { | |
1339 | binder_user_error("binder: %d:%d got reply transaction " | |
1340 | "with no transaction stack\n", | |
1341 | proc->pid, thread->pid); | |
1342 | return_error = BR_FAILED_REPLY; | |
1343 | goto err_empty_call_stack; | |
1344 | } | |
1345 | binder_set_nice(in_reply_to->saved_priority); | |
1346 | if (in_reply_to->to_thread != thread) { | |
1347 | binder_user_error("binder: %d:%d got reply transaction " | |
1348 | "with bad transaction stack," | |
1349 | " transaction %d has target %d:%d\n", | |
1350 | proc->pid, thread->pid, in_reply_to->debug_id, | |
1351 | in_reply_to->to_proc ? | |
1352 | in_reply_to->to_proc->pid : 0, | |
1353 | in_reply_to->to_thread ? | |
1354 | in_reply_to->to_thread->pid : 0); | |
1355 | return_error = BR_FAILED_REPLY; | |
1356 | in_reply_to = NULL; | |
1357 | goto err_bad_call_stack; | |
1358 | } | |
1359 | thread->transaction_stack = in_reply_to->to_parent; | |
1360 | target_thread = in_reply_to->from; | |
1361 | if (target_thread == NULL) { | |
1362 | return_error = BR_DEAD_REPLY; | |
1363 | goto err_dead_binder; | |
1364 | } | |
1365 | if (target_thread->transaction_stack != in_reply_to) { | |
1366 | binder_user_error("binder: %d:%d got reply transaction " | |
1367 | "with bad target transaction stack %d, " | |
1368 | "expected %d\n", | |
1369 | proc->pid, thread->pid, | |
1370 | target_thread->transaction_stack ? | |
1371 | target_thread->transaction_stack->debug_id : 0, | |
1372 | in_reply_to->debug_id); | |
1373 | return_error = BR_FAILED_REPLY; | |
1374 | in_reply_to = NULL; | |
1375 | target_thread = NULL; | |
1376 | goto err_dead_binder; | |
1377 | } | |
1378 | target_proc = target_thread->proc; | |
1379 | } else { | |
1380 | if (tr->target.handle) { | |
1381 | struct binder_ref *ref; | |
1382 | ref = binder_get_ref(proc, tr->target.handle); | |
1383 | if (ref == NULL) { | |
1384 | binder_user_error("binder: %d:%d got " | |
1385 | "transaction to invalid handle\n", | |
1386 | proc->pid, thread->pid); | |
1387 | return_error = BR_FAILED_REPLY; | |
1388 | goto err_invalid_target_handle; | |
1389 | } | |
1390 | target_node = ref->node; | |
1391 | } else { | |
1392 | target_node = binder_context_mgr_node; | |
1393 | if (target_node == NULL) { | |
1394 | return_error = BR_DEAD_REPLY; | |
1395 | goto err_no_context_mgr_node; | |
1396 | } | |
1397 | } | |
1398 | e->to_node = target_node->debug_id; | |
1399 | target_proc = target_node->proc; | |
1400 | if (target_proc == NULL) { | |
1401 | return_error = BR_DEAD_REPLY; | |
1402 | goto err_dead_binder; | |
1403 | } | |
1404 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { | |
1405 | struct binder_transaction *tmp; | |
1406 | tmp = thread->transaction_stack; | |
1407 | if (tmp->to_thread != thread) { | |
1408 | binder_user_error("binder: %d:%d got new " | |
1409 | "transaction with bad transaction stack" | |
1410 | ", transaction %d has target %d:%d\n", | |
1411 | proc->pid, thread->pid, tmp->debug_id, | |
1412 | tmp->to_proc ? tmp->to_proc->pid : 0, | |
1413 | tmp->to_thread ? | |
1414 | tmp->to_thread->pid : 0); | |
1415 | return_error = BR_FAILED_REPLY; | |
1416 | goto err_bad_call_stack; | |
1417 | } | |
1418 | while (tmp) { | |
1419 | if (tmp->from && tmp->from->proc == target_proc) | |
1420 | target_thread = tmp->from; | |
1421 | tmp = tmp->from_parent; | |
1422 | } | |
1423 | } | |
1424 | } | |
1425 | if (target_thread) { | |
1426 | e->to_thread = target_thread->pid; | |
1427 | target_list = &target_thread->todo; | |
1428 | target_wait = &target_thread->wait; | |
1429 | } else { | |
1430 | target_list = &target_proc->todo; | |
1431 | target_wait = &target_proc->wait; | |
1432 | } | |
1433 | e->to_proc = target_proc->pid; | |
1434 | ||
1435 | /* TODO: reuse incoming transaction for reply */ | |
1436 | t = kzalloc(sizeof(*t), GFP_KERNEL); | |
1437 | if (t == NULL) { | |
1438 | return_error = BR_FAILED_REPLY; | |
1439 | goto err_alloc_t_failed; | |
1440 | } | |
1441 | binder_stats_created(BINDER_STAT_TRANSACTION); | |
1442 | ||
1443 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); | |
1444 | if (tcomplete == NULL) { | |
1445 | return_error = BR_FAILED_REPLY; | |
1446 | goto err_alloc_tcomplete_failed; | |
1447 | } | |
1448 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); | |
1449 | ||
1450 | t->debug_id = ++binder_last_id; | |
1451 | e->debug_id = t->debug_id; | |
1452 | ||
1453 | if (reply) | |
1454 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
1455 | "binder: %d:%d BC_REPLY %d -> %d:%d, " | |
1456 | "data %p-%p size %zd-%zd\n", | |
1457 | proc->pid, thread->pid, t->debug_id, | |
1458 | target_proc->pid, target_thread->pid, | |
1459 | tr->data.ptr.buffer, tr->data.ptr.offsets, | |
1460 | tr->data_size, tr->offsets_size); | |
1461 | else | |
1462 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
1463 | "binder: %d:%d BC_TRANSACTION %d -> " | |
1464 | "%d - node %d, data %p-%p size %zd-%zd\n", | |
1465 | proc->pid, thread->pid, t->debug_id, | |
1466 | target_proc->pid, target_node->debug_id, | |
1467 | tr->data.ptr.buffer, tr->data.ptr.offsets, | |
1468 | tr->data_size, tr->offsets_size); | |
1469 | ||
1470 | if (!reply && !(tr->flags & TF_ONE_WAY)) | |
1471 | t->from = thread; | |
1472 | else | |
1473 | t->from = NULL; | |
1474 | t->sender_euid = proc->tsk->cred->euid; | |
1475 | t->to_proc = target_proc; | |
1476 | t->to_thread = target_thread; | |
1477 | t->code = tr->code; | |
1478 | t->flags = tr->flags; | |
1479 | t->priority = task_nice(current); | |
975a1ac9 AH |
1480 | |
1481 | trace_binder_transaction(reply, t, target_node); | |
1482 | ||
355b0502 GKH |
1483 | t->buffer = binder_alloc_buf(target_proc, tr->data_size, |
1484 | tr->offsets_size, !reply && (t->flags & TF_ONE_WAY)); | |
1485 | if (t->buffer == NULL) { | |
1486 | return_error = BR_FAILED_REPLY; | |
1487 | goto err_binder_alloc_buf_failed; | |
1488 | } | |
1489 | t->buffer->allow_user_free = 0; | |
1490 | t->buffer->debug_id = t->debug_id; | |
1491 | t->buffer->transaction = t; | |
1492 | t->buffer->target_node = target_node; | |
975a1ac9 | 1493 | trace_binder_transaction_alloc_buf(t->buffer); |
355b0502 GKH |
1494 | if (target_node) |
1495 | binder_inc_node(target_node, 1, 0, NULL); | |
1496 | ||
1497 | offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *))); | |
1498 | ||
1499 | if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) { | |
1500 | binder_user_error("binder: %d:%d got transaction with invalid " | |
1501 | "data ptr\n", proc->pid, thread->pid); | |
1502 | return_error = BR_FAILED_REPLY; | |
1503 | goto err_copy_data_failed; | |
1504 | } | |
1505 | if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) { | |
1506 | binder_user_error("binder: %d:%d got transaction with invalid " | |
1507 | "offsets ptr\n", proc->pid, thread->pid); | |
1508 | return_error = BR_FAILED_REPLY; | |
1509 | goto err_copy_data_failed; | |
1510 | } | |
1511 | if (!IS_ALIGNED(tr->offsets_size, sizeof(size_t))) { | |
1512 | binder_user_error("binder: %d:%d got transaction with " | |
1513 | "invalid offsets size, %zd\n", | |
1514 | proc->pid, thread->pid, tr->offsets_size); | |
1515 | return_error = BR_FAILED_REPLY; | |
1516 | goto err_bad_offset; | |
1517 | } | |
1518 | off_end = (void *)offp + tr->offsets_size; | |
1519 | for (; offp < off_end; offp++) { | |
1520 | struct flat_binder_object *fp; | |
1521 | if (*offp > t->buffer->data_size - sizeof(*fp) || | |
1522 | t->buffer->data_size < sizeof(*fp) || | |
1523 | !IS_ALIGNED(*offp, sizeof(void *))) { | |
1524 | binder_user_error("binder: %d:%d got transaction with " | |
1525 | "invalid offset, %zd\n", | |
1526 | proc->pid, thread->pid, *offp); | |
1527 | return_error = BR_FAILED_REPLY; | |
1528 | goto err_bad_offset; | |
1529 | } | |
1530 | fp = (struct flat_binder_object *)(t->buffer->data + *offp); | |
1531 | switch (fp->type) { | |
1532 | case BINDER_TYPE_BINDER: | |
1533 | case BINDER_TYPE_WEAK_BINDER: { | |
1534 | struct binder_ref *ref; | |
1535 | struct binder_node *node = binder_get_node(proc, fp->binder); | |
1536 | if (node == NULL) { | |
1537 | node = binder_new_node(proc, fp->binder, fp->cookie); | |
1538 | if (node == NULL) { | |
1539 | return_error = BR_FAILED_REPLY; | |
1540 | goto err_binder_new_node_failed; | |
1541 | } | |
1542 | node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK; | |
1543 | node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); | |
1544 | } | |
1545 | if (fp->cookie != node->cookie) { | |
1546 | binder_user_error("binder: %d:%d sending u%p " | |
1547 | "node %d, cookie mismatch %p != %p\n", | |
1548 | proc->pid, thread->pid, | |
1549 | fp->binder, node->debug_id, | |
1550 | fp->cookie, node->cookie); | |
1551 | goto err_binder_get_ref_for_node_failed; | |
1552 | } | |
1553 | ref = binder_get_ref_for_node(target_proc, node); | |
1554 | if (ref == NULL) { | |
1555 | return_error = BR_FAILED_REPLY; | |
1556 | goto err_binder_get_ref_for_node_failed; | |
1557 | } | |
1558 | if (fp->type == BINDER_TYPE_BINDER) | |
1559 | fp->type = BINDER_TYPE_HANDLE; | |
1560 | else | |
1561 | fp->type = BINDER_TYPE_WEAK_HANDLE; | |
1562 | fp->handle = ref->desc; | |
1563 | binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, | |
1564 | &thread->todo); | |
1565 | ||
975a1ac9 | 1566 | trace_binder_transaction_node_to_ref(t, node, ref); |
355b0502 GKH |
1567 | binder_debug(BINDER_DEBUG_TRANSACTION, |
1568 | " node %d u%p -> ref %d desc %d\n", | |
1569 | node->debug_id, node->ptr, ref->debug_id, | |
1570 | ref->desc); | |
1571 | } break; | |
1572 | case BINDER_TYPE_HANDLE: | |
1573 | case BINDER_TYPE_WEAK_HANDLE: { | |
1574 | struct binder_ref *ref = binder_get_ref(proc, fp->handle); | |
1575 | if (ref == NULL) { | |
1576 | binder_user_error("binder: %d:%d got " | |
1577 | "transaction with invalid " | |
1578 | "handle, %ld\n", proc->pid, | |
1579 | thread->pid, fp->handle); | |
1580 | return_error = BR_FAILED_REPLY; | |
1581 | goto err_binder_get_ref_failed; | |
1582 | } | |
1583 | if (ref->node->proc == target_proc) { | |
1584 | if (fp->type == BINDER_TYPE_HANDLE) | |
1585 | fp->type = BINDER_TYPE_BINDER; | |
1586 | else | |
1587 | fp->type = BINDER_TYPE_WEAK_BINDER; | |
1588 | fp->binder = ref->node->ptr; | |
1589 | fp->cookie = ref->node->cookie; | |
1590 | binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL); | |
975a1ac9 | 1591 | trace_binder_transaction_ref_to_node(t, ref); |
355b0502 GKH |
1592 | binder_debug(BINDER_DEBUG_TRANSACTION, |
1593 | " ref %d desc %d -> node %d u%p\n", | |
1594 | ref->debug_id, ref->desc, ref->node->debug_id, | |
1595 | ref->node->ptr); | |
1596 | } else { | |
1597 | struct binder_ref *new_ref; | |
1598 | new_ref = binder_get_ref_for_node(target_proc, ref->node); | |
1599 | if (new_ref == NULL) { | |
1600 | return_error = BR_FAILED_REPLY; | |
1601 | goto err_binder_get_ref_for_node_failed; | |
1602 | } | |
1603 | fp->handle = new_ref->desc; | |
1604 | binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL); | |
975a1ac9 AH |
1605 | trace_binder_transaction_ref_to_ref(t, ref, |
1606 | new_ref); | |
355b0502 GKH |
1607 | binder_debug(BINDER_DEBUG_TRANSACTION, |
1608 | " ref %d desc %d -> ref %d desc %d (node %d)\n", | |
1609 | ref->debug_id, ref->desc, new_ref->debug_id, | |
1610 | new_ref->desc, ref->node->debug_id); | |
1611 | } | |
1612 | } break; | |
1613 | ||
1614 | case BINDER_TYPE_FD: { | |
1615 | int target_fd; | |
1616 | struct file *file; | |
1617 | ||
1618 | if (reply) { | |
1619 | if (!(in_reply_to->flags & TF_ACCEPT_FDS)) { | |
1620 | binder_user_error("binder: %d:%d got reply with fd, %ld, but target does not allow fds\n", | |
1621 | proc->pid, thread->pid, fp->handle); | |
1622 | return_error = BR_FAILED_REPLY; | |
1623 | goto err_fd_not_allowed; | |
1624 | } | |
1625 | } else if (!target_node->accept_fds) { | |
1626 | binder_user_error("binder: %d:%d got transaction with fd, %ld, but target does not allow fds\n", | |
1627 | proc->pid, thread->pid, fp->handle); | |
1628 | return_error = BR_FAILED_REPLY; | |
1629 | goto err_fd_not_allowed; | |
1630 | } | |
1631 | ||
1632 | file = fget(fp->handle); | |
1633 | if (file == NULL) { | |
1634 | binder_user_error("binder: %d:%d got transaction with invalid fd, %ld\n", | |
1635 | proc->pid, thread->pid, fp->handle); | |
1636 | return_error = BR_FAILED_REPLY; | |
1637 | goto err_fget_failed; | |
1638 | } | |
1639 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); | |
1640 | if (target_fd < 0) { | |
1641 | fput(file); | |
1642 | return_error = BR_FAILED_REPLY; | |
1643 | goto err_get_unused_fd_failed; | |
1644 | } | |
1645 | task_fd_install(target_proc, target_fd, file); | |
975a1ac9 | 1646 | trace_binder_transaction_fd(t, fp->handle, target_fd); |
355b0502 GKH |
1647 | binder_debug(BINDER_DEBUG_TRANSACTION, |
1648 | " fd %ld -> %d\n", fp->handle, target_fd); | |
1649 | /* TODO: fput? */ | |
1650 | fp->handle = target_fd; | |
1651 | } break; | |
1652 | ||
1653 | default: | |
1654 | binder_user_error("binder: %d:%d got transactio" | |
1655 | "n with invalid object type, %lx\n", | |
1656 | proc->pid, thread->pid, fp->type); | |
1657 | return_error = BR_FAILED_REPLY; | |
1658 | goto err_bad_object_type; | |
1659 | } | |
1660 | } | |
1661 | if (reply) { | |
1662 | BUG_ON(t->buffer->async_transaction != 0); | |
1663 | binder_pop_transaction(target_thread, in_reply_to); | |
1664 | } else if (!(t->flags & TF_ONE_WAY)) { | |
1665 | BUG_ON(t->buffer->async_transaction != 0); | |
1666 | t->need_reply = 1; | |
1667 | t->from_parent = thread->transaction_stack; | |
1668 | thread->transaction_stack = t; | |
1669 | } else { | |
1670 | BUG_ON(target_node == NULL); | |
1671 | BUG_ON(t->buffer->async_transaction != 1); | |
1672 | if (target_node->has_async_transaction) { | |
1673 | target_list = &target_node->async_todo; | |
1674 | target_wait = NULL; | |
1675 | } else | |
1676 | target_node->has_async_transaction = 1; | |
1677 | } | |
1678 | t->work.type = BINDER_WORK_TRANSACTION; | |
1679 | list_add_tail(&t->work.entry, target_list); | |
1680 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; | |
1681 | list_add_tail(&tcomplete->entry, &thread->todo); | |
1682 | if (target_wait) | |
1683 | wake_up_interruptible(target_wait); | |
1684 | return; | |
1685 | ||
1686 | err_get_unused_fd_failed: | |
1687 | err_fget_failed: | |
1688 | err_fd_not_allowed: | |
1689 | err_binder_get_ref_for_node_failed: | |
1690 | err_binder_get_ref_failed: | |
1691 | err_binder_new_node_failed: | |
1692 | err_bad_object_type: | |
1693 | err_bad_offset: | |
1694 | err_copy_data_failed: | |
975a1ac9 | 1695 | trace_binder_transaction_failed_buffer_release(t->buffer); |
355b0502 GKH |
1696 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
1697 | t->buffer->transaction = NULL; | |
1698 | binder_free_buf(target_proc, t->buffer); | |
1699 | err_binder_alloc_buf_failed: | |
1700 | kfree(tcomplete); | |
1701 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); | |
1702 | err_alloc_tcomplete_failed: | |
1703 | kfree(t); | |
1704 | binder_stats_deleted(BINDER_STAT_TRANSACTION); | |
1705 | err_alloc_t_failed: | |
1706 | err_bad_call_stack: | |
1707 | err_empty_call_stack: | |
1708 | err_dead_binder: | |
1709 | err_invalid_target_handle: | |
1710 | err_no_context_mgr_node: | |
1711 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | |
1712 | "binder: %d:%d transaction failed %d, size %zd-%zd\n", | |
1713 | proc->pid, thread->pid, return_error, | |
1714 | tr->data_size, tr->offsets_size); | |
1715 | ||
1716 | { | |
1717 | struct binder_transaction_log_entry *fe; | |
1718 | fe = binder_transaction_log_add(&binder_transaction_log_failed); | |
1719 | *fe = *e; | |
1720 | } | |
1721 | ||
1722 | BUG_ON(thread->return_error != BR_OK); | |
1723 | if (in_reply_to) { | |
1724 | thread->return_error = BR_TRANSACTION_COMPLETE; | |
1725 | binder_send_failed_reply(in_reply_to, return_error); | |
1726 | } else | |
1727 | thread->return_error = return_error; | |
1728 | } | |
1729 | ||
1730 | int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread, | |
1731 | void __user *buffer, int size, signed long *consumed) | |
1732 | { | |
1733 | uint32_t cmd; | |
1734 | void __user *ptr = buffer + *consumed; | |
1735 | void __user *end = buffer + size; | |
1736 | ||
1737 | while (ptr < end && thread->return_error == BR_OK) { | |
1738 | if (get_user(cmd, (uint32_t __user *)ptr)) | |
1739 | return -EFAULT; | |
1740 | ptr += sizeof(uint32_t); | |
975a1ac9 | 1741 | trace_binder_command(cmd); |
355b0502 GKH |
1742 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
1743 | binder_stats.bc[_IOC_NR(cmd)]++; | |
1744 | proc->stats.bc[_IOC_NR(cmd)]++; | |
1745 | thread->stats.bc[_IOC_NR(cmd)]++; | |
1746 | } | |
1747 | switch (cmd) { | |
1748 | case BC_INCREFS: | |
1749 | case BC_ACQUIRE: | |
1750 | case BC_RELEASE: | |
1751 | case BC_DECREFS: { | |
1752 | uint32_t target; | |
1753 | struct binder_ref *ref; | |
1754 | const char *debug_string; | |
1755 | ||
1756 | if (get_user(target, (uint32_t __user *)ptr)) | |
1757 | return -EFAULT; | |
1758 | ptr += sizeof(uint32_t); | |
1759 | if (target == 0 && binder_context_mgr_node && | |
1760 | (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) { | |
1761 | ref = binder_get_ref_for_node(proc, | |
1762 | binder_context_mgr_node); | |
1763 | if (ref->desc != target) { | |
1764 | binder_user_error("binder: %d:" | |
1765 | "%d tried to acquire " | |
1766 | "reference to desc 0, " | |
1767 | "got %d instead\n", | |
1768 | proc->pid, thread->pid, | |
1769 | ref->desc); | |
1770 | } | |
1771 | } else | |
1772 | ref = binder_get_ref(proc, target); | |
1773 | if (ref == NULL) { | |
1774 | binder_user_error("binder: %d:%d refcou" | |
1775 | "nt change on invalid ref %d\n", | |
1776 | proc->pid, thread->pid, target); | |
1777 | break; | |
1778 | } | |
1779 | switch (cmd) { | |
1780 | case BC_INCREFS: | |
1781 | debug_string = "IncRefs"; | |
1782 | binder_inc_ref(ref, 0, NULL); | |
1783 | break; | |
1784 | case BC_ACQUIRE: | |
1785 | debug_string = "Acquire"; | |
1786 | binder_inc_ref(ref, 1, NULL); | |
1787 | break; | |
1788 | case BC_RELEASE: | |
1789 | debug_string = "Release"; | |
1790 | binder_dec_ref(ref, 1); | |
1791 | break; | |
1792 | case BC_DECREFS: | |
1793 | default: | |
1794 | debug_string = "DecRefs"; | |
1795 | binder_dec_ref(ref, 0); | |
1796 | break; | |
1797 | } | |
1798 | binder_debug(BINDER_DEBUG_USER_REFS, | |
1799 | "binder: %d:%d %s ref %d desc %d s %d w %d for node %d\n", | |
1800 | proc->pid, thread->pid, debug_string, ref->debug_id, | |
1801 | ref->desc, ref->strong, ref->weak, ref->node->debug_id); | |
1802 | break; | |
1803 | } | |
1804 | case BC_INCREFS_DONE: | |
1805 | case BC_ACQUIRE_DONE: { | |
1806 | void __user *node_ptr; | |
1807 | void *cookie; | |
1808 | struct binder_node *node; | |
1809 | ||
1810 | if (get_user(node_ptr, (void * __user *)ptr)) | |
1811 | return -EFAULT; | |
1812 | ptr += sizeof(void *); | |
1813 | if (get_user(cookie, (void * __user *)ptr)) | |
1814 | return -EFAULT; | |
1815 | ptr += sizeof(void *); | |
1816 | node = binder_get_node(proc, node_ptr); | |
1817 | if (node == NULL) { | |
1818 | binder_user_error("binder: %d:%d " | |
1819 | "%s u%p no match\n", | |
1820 | proc->pid, thread->pid, | |
1821 | cmd == BC_INCREFS_DONE ? | |
1822 | "BC_INCREFS_DONE" : | |
1823 | "BC_ACQUIRE_DONE", | |
1824 | node_ptr); | |
1825 | break; | |
1826 | } | |
1827 | if (cookie != node->cookie) { | |
1828 | binder_user_error("binder: %d:%d %s u%p node %d" | |
1829 | " cookie mismatch %p != %p\n", | |
1830 | proc->pid, thread->pid, | |
1831 | cmd == BC_INCREFS_DONE ? | |
1832 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", | |
1833 | node_ptr, node->debug_id, | |
1834 | cookie, node->cookie); | |
1835 | break; | |
1836 | } | |
1837 | if (cmd == BC_ACQUIRE_DONE) { | |
1838 | if (node->pending_strong_ref == 0) { | |
1839 | binder_user_error("binder: %d:%d " | |
1840 | "BC_ACQUIRE_DONE node %d has " | |
1841 | "no pending acquire request\n", | |
1842 | proc->pid, thread->pid, | |
1843 | node->debug_id); | |
1844 | break; | |
1845 | } | |
1846 | node->pending_strong_ref = 0; | |
1847 | } else { | |
1848 | if (node->pending_weak_ref == 0) { | |
1849 | binder_user_error("binder: %d:%d " | |
1850 | "BC_INCREFS_DONE node %d has " | |
1851 | "no pending increfs request\n", | |
1852 | proc->pid, thread->pid, | |
1853 | node->debug_id); | |
1854 | break; | |
1855 | } | |
1856 | node->pending_weak_ref = 0; | |
1857 | } | |
1858 | binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0); | |
1859 | binder_debug(BINDER_DEBUG_USER_REFS, | |
1860 | "binder: %d:%d %s node %d ls %d lw %d\n", | |
1861 | proc->pid, thread->pid, | |
1862 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", | |
1863 | node->debug_id, node->local_strong_refs, node->local_weak_refs); | |
1864 | break; | |
1865 | } | |
1866 | case BC_ATTEMPT_ACQUIRE: | |
258767fe | 1867 | pr_err("binder: BC_ATTEMPT_ACQUIRE not supported\n"); |
355b0502 GKH |
1868 | return -EINVAL; |
1869 | case BC_ACQUIRE_RESULT: | |
258767fe | 1870 | pr_err("binder: BC_ACQUIRE_RESULT not supported\n"); |
355b0502 GKH |
1871 | return -EINVAL; |
1872 | ||
1873 | case BC_FREE_BUFFER: { | |
1874 | void __user *data_ptr; | |
1875 | struct binder_buffer *buffer; | |
1876 | ||
1877 | if (get_user(data_ptr, (void * __user *)ptr)) | |
1878 | return -EFAULT; | |
1879 | ptr += sizeof(void *); | |
1880 | ||
1881 | buffer = binder_buffer_lookup(proc, data_ptr); | |
1882 | if (buffer == NULL) { | |
1883 | binder_user_error("binder: %d:%d " | |
1884 | "BC_FREE_BUFFER u%p no match\n", | |
1885 | proc->pid, thread->pid, data_ptr); | |
1886 | break; | |
1887 | } | |
1888 | if (!buffer->allow_user_free) { | |
1889 | binder_user_error("binder: %d:%d " | |
1890 | "BC_FREE_BUFFER u%p matched " | |
1891 | "unreturned buffer\n", | |
1892 | proc->pid, thread->pid, data_ptr); | |
1893 | break; | |
1894 | } | |
1895 | binder_debug(BINDER_DEBUG_FREE_BUFFER, | |
1896 | "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n", | |
1897 | proc->pid, thread->pid, data_ptr, buffer->debug_id, | |
1898 | buffer->transaction ? "active" : "finished"); | |
1899 | ||
1900 | if (buffer->transaction) { | |
1901 | buffer->transaction->buffer = NULL; | |
1902 | buffer->transaction = NULL; | |
1903 | } | |
1904 | if (buffer->async_transaction && buffer->target_node) { | |
1905 | BUG_ON(!buffer->target_node->has_async_transaction); | |
1906 | if (list_empty(&buffer->target_node->async_todo)) | |
1907 | buffer->target_node->has_async_transaction = 0; | |
1908 | else | |
1909 | list_move_tail(buffer->target_node->async_todo.next, &thread->todo); | |
1910 | } | |
975a1ac9 | 1911 | trace_binder_transaction_buffer_release(buffer); |
355b0502 GKH |
1912 | binder_transaction_buffer_release(proc, buffer, NULL); |
1913 | binder_free_buf(proc, buffer); | |
1914 | break; | |
1915 | } | |
1916 | ||
1917 | case BC_TRANSACTION: | |
1918 | case BC_REPLY: { | |
1919 | struct binder_transaction_data tr; | |
1920 | ||
1921 | if (copy_from_user(&tr, ptr, sizeof(tr))) | |
1922 | return -EFAULT; | |
1923 | ptr += sizeof(tr); | |
1924 | binder_transaction(proc, thread, &tr, cmd == BC_REPLY); | |
1925 | break; | |
1926 | } | |
1927 | ||
1928 | case BC_REGISTER_LOOPER: | |
1929 | binder_debug(BINDER_DEBUG_THREADS, | |
1930 | "binder: %d:%d BC_REGISTER_LOOPER\n", | |
1931 | proc->pid, thread->pid); | |
1932 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { | |
1933 | thread->looper |= BINDER_LOOPER_STATE_INVALID; | |
1934 | binder_user_error("binder: %d:%d ERROR:" | |
1935 | " BC_REGISTER_LOOPER called " | |
1936 | "after BC_ENTER_LOOPER\n", | |
1937 | proc->pid, thread->pid); | |
1938 | } else if (proc->requested_threads == 0) { | |
1939 | thread->looper |= BINDER_LOOPER_STATE_INVALID; | |
1940 | binder_user_error("binder: %d:%d ERROR:" | |
1941 | " BC_REGISTER_LOOPER called " | |
1942 | "without request\n", | |
1943 | proc->pid, thread->pid); | |
1944 | } else { | |
1945 | proc->requested_threads--; | |
1946 | proc->requested_threads_started++; | |
1947 | } | |
1948 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; | |
1949 | break; | |
1950 | case BC_ENTER_LOOPER: | |
1951 | binder_debug(BINDER_DEBUG_THREADS, | |
1952 | "binder: %d:%d BC_ENTER_LOOPER\n", | |
1953 | proc->pid, thread->pid); | |
1954 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { | |
1955 | thread->looper |= BINDER_LOOPER_STATE_INVALID; | |
1956 | binder_user_error("binder: %d:%d ERROR:" | |
1957 | " BC_ENTER_LOOPER called after " | |
1958 | "BC_REGISTER_LOOPER\n", | |
1959 | proc->pid, thread->pid); | |
1960 | } | |
1961 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; | |
1962 | break; | |
1963 | case BC_EXIT_LOOPER: | |
1964 | binder_debug(BINDER_DEBUG_THREADS, | |
1965 | "binder: %d:%d BC_EXIT_LOOPER\n", | |
1966 | proc->pid, thread->pid); | |
1967 | thread->looper |= BINDER_LOOPER_STATE_EXITED; | |
1968 | break; | |
1969 | ||
1970 | case BC_REQUEST_DEATH_NOTIFICATION: | |
1971 | case BC_CLEAR_DEATH_NOTIFICATION: { | |
1972 | uint32_t target; | |
1973 | void __user *cookie; | |
1974 | struct binder_ref *ref; | |
1975 | struct binder_ref_death *death; | |
1976 | ||
1977 | if (get_user(target, (uint32_t __user *)ptr)) | |
1978 | return -EFAULT; | |
1979 | ptr += sizeof(uint32_t); | |
1980 | if (get_user(cookie, (void __user * __user *)ptr)) | |
1981 | return -EFAULT; | |
1982 | ptr += sizeof(void *); | |
1983 | ref = binder_get_ref(proc, target); | |
1984 | if (ref == NULL) { | |
1985 | binder_user_error("binder: %d:%d %s " | |
1986 | "invalid ref %d\n", | |
1987 | proc->pid, thread->pid, | |
1988 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? | |
1989 | "BC_REQUEST_DEATH_NOTIFICATION" : | |
1990 | "BC_CLEAR_DEATH_NOTIFICATION", | |
1991 | target); | |
1992 | break; | |
1993 | } | |
1994 | ||
1995 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, | |
1996 | "binder: %d:%d %s %p ref %d desc %d s %d w %d for node %d\n", | |
1997 | proc->pid, thread->pid, | |
1998 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? | |
1999 | "BC_REQUEST_DEATH_NOTIFICATION" : | |
2000 | "BC_CLEAR_DEATH_NOTIFICATION", | |
2001 | cookie, ref->debug_id, ref->desc, | |
2002 | ref->strong, ref->weak, ref->node->debug_id); | |
2003 | ||
2004 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { | |
2005 | if (ref->death) { | |
2006 | binder_user_error("binder: %d:%" | |
2007 | "d BC_REQUEST_DEATH_NOTI" | |
2008 | "FICATION death notific" | |
2009 | "ation already set\n", | |
2010 | proc->pid, thread->pid); | |
2011 | break; | |
2012 | } | |
2013 | death = kzalloc(sizeof(*death), GFP_KERNEL); | |
2014 | if (death == NULL) { | |
2015 | thread->return_error = BR_ERROR; | |
2016 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, | |
2017 | "binder: %d:%d " | |
2018 | "BC_REQUEST_DEATH_NOTIFICATION failed\n", | |
2019 | proc->pid, thread->pid); | |
2020 | break; | |
2021 | } | |
2022 | binder_stats_created(BINDER_STAT_DEATH); | |
2023 | INIT_LIST_HEAD(&death->work.entry); | |
2024 | death->cookie = cookie; | |
2025 | ref->death = death; | |
2026 | if (ref->node->proc == NULL) { | |
2027 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; | |
2028 | if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) { | |
2029 | list_add_tail(&ref->death->work.entry, &thread->todo); | |
2030 | } else { | |
2031 | list_add_tail(&ref->death->work.entry, &proc->todo); | |
2032 | wake_up_interruptible(&proc->wait); | |
2033 | } | |
2034 | } | |
2035 | } else { | |
2036 | if (ref->death == NULL) { | |
2037 | binder_user_error("binder: %d:%" | |
2038 | "d BC_CLEAR_DEATH_NOTIFI" | |
2039 | "CATION death notificat" | |
2040 | "ion not active\n", | |
2041 | proc->pid, thread->pid); | |
2042 | break; | |
2043 | } | |
2044 | death = ref->death; | |
2045 | if (death->cookie != cookie) { | |
2046 | binder_user_error("binder: %d:%" | |
2047 | "d BC_CLEAR_DEATH_NOTIFI" | |
2048 | "CATION death notificat" | |
2049 | "ion cookie mismatch " | |
2050 | "%p != %p\n", | |
2051 | proc->pid, thread->pid, | |
2052 | death->cookie, cookie); | |
2053 | break; | |
2054 | } | |
2055 | ref->death = NULL; | |
2056 | if (list_empty(&death->work.entry)) { | |
2057 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; | |
2058 | if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) { | |
2059 | list_add_tail(&death->work.entry, &thread->todo); | |
2060 | } else { | |
2061 | list_add_tail(&death->work.entry, &proc->todo); | |
2062 | wake_up_interruptible(&proc->wait); | |
2063 | } | |
2064 | } else { | |
2065 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); | |
2066 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; | |
2067 | } | |
2068 | } | |
2069 | } break; | |
2070 | case BC_DEAD_BINDER_DONE: { | |
2071 | struct binder_work *w; | |
2072 | void __user *cookie; | |
2073 | struct binder_ref_death *death = NULL; | |
2074 | if (get_user(cookie, (void __user * __user *)ptr)) | |
2075 | return -EFAULT; | |
2076 | ||
2077 | ptr += sizeof(void *); | |
2078 | list_for_each_entry(w, &proc->delivered_death, entry) { | |
2079 | struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work); | |
2080 | if (tmp_death->cookie == cookie) { | |
2081 | death = tmp_death; | |
2082 | break; | |
2083 | } | |
2084 | } | |
2085 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | |
2086 | "binder: %d:%d BC_DEAD_BINDER_DONE %p found %p\n", | |
2087 | proc->pid, thread->pid, cookie, death); | |
2088 | if (death == NULL) { | |
2089 | binder_user_error("binder: %d:%d BC_DEAD" | |
2090 | "_BINDER_DONE %p not found\n", | |
2091 | proc->pid, thread->pid, cookie); | |
2092 | break; | |
2093 | } | |
2094 | ||
2095 | list_del_init(&death->work.entry); | |
2096 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { | |
2097 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; | |
2098 | if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) { | |
2099 | list_add_tail(&death->work.entry, &thread->todo); | |
2100 | } else { | |
2101 | list_add_tail(&death->work.entry, &proc->todo); | |
2102 | wake_up_interruptible(&proc->wait); | |
2103 | } | |
2104 | } | |
2105 | } break; | |
2106 | ||
2107 | default: | |
258767fe | 2108 | pr_err("binder: %d:%d unknown command %d\n", |
355b0502 GKH |
2109 | proc->pid, thread->pid, cmd); |
2110 | return -EINVAL; | |
2111 | } | |
2112 | *consumed = ptr - buffer; | |
2113 | } | |
2114 | return 0; | |
2115 | } | |
2116 | ||
2117 | void binder_stat_br(struct binder_proc *proc, struct binder_thread *thread, | |
2118 | uint32_t cmd) | |
2119 | { | |
975a1ac9 | 2120 | trace_binder_return(cmd); |
355b0502 GKH |
2121 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
2122 | binder_stats.br[_IOC_NR(cmd)]++; | |
2123 | proc->stats.br[_IOC_NR(cmd)]++; | |
2124 | thread->stats.br[_IOC_NR(cmd)]++; | |
2125 | } | |
2126 | } | |
2127 | ||
2128 | static int binder_has_proc_work(struct binder_proc *proc, | |
2129 | struct binder_thread *thread) | |
2130 | { | |
2131 | return !list_empty(&proc->todo) || | |
2132 | (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN); | |
2133 | } | |
2134 | ||
2135 | static int binder_has_thread_work(struct binder_thread *thread) | |
2136 | { | |
2137 | return !list_empty(&thread->todo) || thread->return_error != BR_OK || | |
2138 | (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN); | |
2139 | } | |
2140 | ||
2141 | static int binder_thread_read(struct binder_proc *proc, | |
2142 | struct binder_thread *thread, | |
2143 | void __user *buffer, int size, | |
2144 | signed long *consumed, int non_block) | |
2145 | { | |
2146 | void __user *ptr = buffer + *consumed; | |
2147 | void __user *end = buffer + size; | |
2148 | ||
2149 | int ret = 0; | |
2150 | int wait_for_proc_work; | |
2151 | ||
2152 | if (*consumed == 0) { | |
2153 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) | |
2154 | return -EFAULT; | |
2155 | ptr += sizeof(uint32_t); | |
2156 | } | |
2157 | ||
2158 | retry: | |
2159 | wait_for_proc_work = thread->transaction_stack == NULL && | |
2160 | list_empty(&thread->todo); | |
2161 | ||
2162 | if (thread->return_error != BR_OK && ptr < end) { | |
2163 | if (thread->return_error2 != BR_OK) { | |
2164 | if (put_user(thread->return_error2, (uint32_t __user *)ptr)) | |
2165 | return -EFAULT; | |
2166 | ptr += sizeof(uint32_t); | |
89334ab4 | 2167 | binder_stat_br(proc, thread, thread->return_error2); |
355b0502 GKH |
2168 | if (ptr == end) |
2169 | goto done; | |
2170 | thread->return_error2 = BR_OK; | |
2171 | } | |
2172 | if (put_user(thread->return_error, (uint32_t __user *)ptr)) | |
2173 | return -EFAULT; | |
2174 | ptr += sizeof(uint32_t); | |
89334ab4 | 2175 | binder_stat_br(proc, thread, thread->return_error); |
355b0502 GKH |
2176 | thread->return_error = BR_OK; |
2177 | goto done; | |
2178 | } | |
2179 | ||
2180 | ||
2181 | thread->looper |= BINDER_LOOPER_STATE_WAITING; | |
2182 | if (wait_for_proc_work) | |
2183 | proc->ready_threads++; | |
975a1ac9 AH |
2184 | |
2185 | binder_unlock(__func__); | |
2186 | ||
2187 | trace_binder_wait_for_work(wait_for_proc_work, | |
2188 | !!thread->transaction_stack, | |
2189 | !list_empty(&thread->todo)); | |
355b0502 GKH |
2190 | if (wait_for_proc_work) { |
2191 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | | |
2192 | BINDER_LOOPER_STATE_ENTERED))) { | |
2193 | binder_user_error("binder: %d:%d ERROR: Thread waiting " | |
2194 | "for process work before calling BC_REGISTER_" | |
2195 | "LOOPER or BC_ENTER_LOOPER (state %x)\n", | |
2196 | proc->pid, thread->pid, thread->looper); | |
2197 | wait_event_interruptible(binder_user_error_wait, | |
2198 | binder_stop_on_user_error < 2); | |
2199 | } | |
2200 | binder_set_nice(proc->default_priority); | |
2201 | if (non_block) { | |
2202 | if (!binder_has_proc_work(proc, thread)) | |
2203 | ret = -EAGAIN; | |
2204 | } else | |
2205 | ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread)); | |
2206 | } else { | |
2207 | if (non_block) { | |
2208 | if (!binder_has_thread_work(thread)) | |
2209 | ret = -EAGAIN; | |
2210 | } else | |
2211 | ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread)); | |
2212 | } | |
975a1ac9 AH |
2213 | |
2214 | binder_lock(__func__); | |
2215 | ||
355b0502 GKH |
2216 | if (wait_for_proc_work) |
2217 | proc->ready_threads--; | |
2218 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; | |
2219 | ||
2220 | if (ret) | |
2221 | return ret; | |
2222 | ||
2223 | while (1) { | |
2224 | uint32_t cmd; | |
2225 | struct binder_transaction_data tr; | |
2226 | struct binder_work *w; | |
2227 | struct binder_transaction *t = NULL; | |
2228 | ||
2229 | if (!list_empty(&thread->todo)) | |
2230 | w = list_first_entry(&thread->todo, struct binder_work, entry); | |
2231 | else if (!list_empty(&proc->todo) && wait_for_proc_work) | |
2232 | w = list_first_entry(&proc->todo, struct binder_work, entry); | |
2233 | else { | |
2234 | if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */ | |
2235 | goto retry; | |
2236 | break; | |
2237 | } | |
2238 | ||
2239 | if (end - ptr < sizeof(tr) + 4) | |
2240 | break; | |
2241 | ||
2242 | switch (w->type) { | |
2243 | case BINDER_WORK_TRANSACTION: { | |
2244 | t = container_of(w, struct binder_transaction, work); | |
2245 | } break; | |
2246 | case BINDER_WORK_TRANSACTION_COMPLETE: { | |
2247 | cmd = BR_TRANSACTION_COMPLETE; | |
2248 | if (put_user(cmd, (uint32_t __user *)ptr)) | |
2249 | return -EFAULT; | |
2250 | ptr += sizeof(uint32_t); | |
2251 | ||
2252 | binder_stat_br(proc, thread, cmd); | |
2253 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, | |
2254 | "binder: %d:%d BR_TRANSACTION_COMPLETE\n", | |
2255 | proc->pid, thread->pid); | |
2256 | ||
2257 | list_del(&w->entry); | |
2258 | kfree(w); | |
2259 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); | |
2260 | } break; | |
2261 | case BINDER_WORK_NODE: { | |
2262 | struct binder_node *node = container_of(w, struct binder_node, work); | |
2263 | uint32_t cmd = BR_NOOP; | |
2264 | const char *cmd_name; | |
2265 | int strong = node->internal_strong_refs || node->local_strong_refs; | |
2266 | int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong; | |
2267 | if (weak && !node->has_weak_ref) { | |
2268 | cmd = BR_INCREFS; | |
2269 | cmd_name = "BR_INCREFS"; | |
2270 | node->has_weak_ref = 1; | |
2271 | node->pending_weak_ref = 1; | |
2272 | node->local_weak_refs++; | |
2273 | } else if (strong && !node->has_strong_ref) { | |
2274 | cmd = BR_ACQUIRE; | |
2275 | cmd_name = "BR_ACQUIRE"; | |
2276 | node->has_strong_ref = 1; | |
2277 | node->pending_strong_ref = 1; | |
2278 | node->local_strong_refs++; | |
2279 | } else if (!strong && node->has_strong_ref) { | |
2280 | cmd = BR_RELEASE; | |
2281 | cmd_name = "BR_RELEASE"; | |
2282 | node->has_strong_ref = 0; | |
2283 | } else if (!weak && node->has_weak_ref) { | |
2284 | cmd = BR_DECREFS; | |
2285 | cmd_name = "BR_DECREFS"; | |
2286 | node->has_weak_ref = 0; | |
2287 | } | |
2288 | if (cmd != BR_NOOP) { | |
2289 | if (put_user(cmd, (uint32_t __user *)ptr)) | |
2290 | return -EFAULT; | |
2291 | ptr += sizeof(uint32_t); | |
2292 | if (put_user(node->ptr, (void * __user *)ptr)) | |
2293 | return -EFAULT; | |
2294 | ptr += sizeof(void *); | |
2295 | if (put_user(node->cookie, (void * __user *)ptr)) | |
2296 | return -EFAULT; | |
2297 | ptr += sizeof(void *); | |
2298 | ||
2299 | binder_stat_br(proc, thread, cmd); | |
2300 | binder_debug(BINDER_DEBUG_USER_REFS, | |
2301 | "binder: %d:%d %s %d u%p c%p\n", | |
2302 | proc->pid, thread->pid, cmd_name, node->debug_id, node->ptr, node->cookie); | |
2303 | } else { | |
2304 | list_del_init(&w->entry); | |
2305 | if (!weak && !strong) { | |
2306 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
2307 | "binder: %d:%d node %d u%p c%p deleted\n", | |
2308 | proc->pid, thread->pid, node->debug_id, | |
2309 | node->ptr, node->cookie); | |
2310 | rb_erase(&node->rb_node, &proc->nodes); | |
2311 | kfree(node); | |
2312 | binder_stats_deleted(BINDER_STAT_NODE); | |
2313 | } else { | |
2314 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, | |
2315 | "binder: %d:%d node %d u%p c%p state unchanged\n", | |
2316 | proc->pid, thread->pid, node->debug_id, node->ptr, | |
2317 | node->cookie); | |
2318 | } | |
2319 | } | |
2320 | } break; | |
2321 | case BINDER_WORK_DEAD_BINDER: | |
2322 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: | |
2323 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { | |
2324 | struct binder_ref_death *death; | |
2325 | uint32_t cmd; | |
2326 | ||
2327 | death = container_of(w, struct binder_ref_death, work); | |
2328 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) | |
2329 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; | |
2330 | else | |
2331 | cmd = BR_DEAD_BINDER; | |
2332 | if (put_user(cmd, (uint32_t __user *)ptr)) | |
2333 | return -EFAULT; | |
2334 | ptr += sizeof(uint32_t); | |
2335 | if (put_user(death->cookie, (void * __user *)ptr)) | |
2336 | return -EFAULT; | |
2337 | ptr += sizeof(void *); | |
89334ab4 | 2338 | binder_stat_br(proc, thread, cmd); |
355b0502 GKH |
2339 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
2340 | "binder: %d:%d %s %p\n", | |
2341 | proc->pid, thread->pid, | |
2342 | cmd == BR_DEAD_BINDER ? | |
2343 | "BR_DEAD_BINDER" : | |
2344 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", | |
2345 | death->cookie); | |
2346 | ||
2347 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { | |
2348 | list_del(&w->entry); | |
2349 | kfree(death); | |
2350 | binder_stats_deleted(BINDER_STAT_DEATH); | |
2351 | } else | |
2352 | list_move(&w->entry, &proc->delivered_death); | |
2353 | if (cmd == BR_DEAD_BINDER) | |
2354 | goto done; /* DEAD_BINDER notifications can cause transactions */ | |
2355 | } break; | |
2356 | } | |
2357 | ||
2358 | if (!t) | |
2359 | continue; | |
2360 | ||
2361 | BUG_ON(t->buffer == NULL); | |
2362 | if (t->buffer->target_node) { | |
2363 | struct binder_node *target_node = t->buffer->target_node; | |
2364 | tr.target.ptr = target_node->ptr; | |
2365 | tr.cookie = target_node->cookie; | |
2366 | t->saved_priority = task_nice(current); | |
2367 | if (t->priority < target_node->min_priority && | |
2368 | !(t->flags & TF_ONE_WAY)) | |
2369 | binder_set_nice(t->priority); | |
2370 | else if (!(t->flags & TF_ONE_WAY) || | |
2371 | t->saved_priority > target_node->min_priority) | |
2372 | binder_set_nice(target_node->min_priority); | |
2373 | cmd = BR_TRANSACTION; | |
2374 | } else { | |
2375 | tr.target.ptr = NULL; | |
2376 | tr.cookie = NULL; | |
2377 | cmd = BR_REPLY; | |
2378 | } | |
2379 | tr.code = t->code; | |
2380 | tr.flags = t->flags; | |
4a2ebb93 | 2381 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
355b0502 GKH |
2382 | |
2383 | if (t->from) { | |
2384 | struct task_struct *sender = t->from->proc->tsk; | |
2385 | tr.sender_pid = task_tgid_nr_ns(sender, | |
2386 | current->nsproxy->pid_ns); | |
2387 | } else { | |
2388 | tr.sender_pid = 0; | |
2389 | } | |
2390 | ||
2391 | tr.data_size = t->buffer->data_size; | |
2392 | tr.offsets_size = t->buffer->offsets_size; | |
2393 | tr.data.ptr.buffer = (void *)t->buffer->data + | |
2394 | proc->user_buffer_offset; | |
2395 | tr.data.ptr.offsets = tr.data.ptr.buffer + | |
2396 | ALIGN(t->buffer->data_size, | |
2397 | sizeof(void *)); | |
2398 | ||
2399 | if (put_user(cmd, (uint32_t __user *)ptr)) | |
2400 | return -EFAULT; | |
2401 | ptr += sizeof(uint32_t); | |
2402 | if (copy_to_user(ptr, &tr, sizeof(tr))) | |
2403 | return -EFAULT; | |
2404 | ptr += sizeof(tr); | |
2405 | ||
975a1ac9 | 2406 | trace_binder_transaction_received(t); |
355b0502 GKH |
2407 | binder_stat_br(proc, thread, cmd); |
2408 | binder_debug(BINDER_DEBUG_TRANSACTION, | |
2409 | "binder: %d:%d %s %d %d:%d, cmd %d" | |
2410 | "size %zd-%zd ptr %p-%p\n", | |
2411 | proc->pid, thread->pid, | |
2412 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : | |
2413 | "BR_REPLY", | |
2414 | t->debug_id, t->from ? t->from->proc->pid : 0, | |
2415 | t->from ? t->from->pid : 0, cmd, | |
2416 | t->buffer->data_size, t->buffer->offsets_size, | |
2417 | tr.data.ptr.buffer, tr.data.ptr.offsets); | |
2418 | ||
2419 | list_del(&t->work.entry); | |
2420 | t->buffer->allow_user_free = 1; | |
2421 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { | |
2422 | t->to_parent = thread->transaction_stack; | |
2423 | t->to_thread = thread; | |
2424 | thread->transaction_stack = t; | |
2425 | } else { | |
2426 | t->buffer->transaction = NULL; | |
2427 | kfree(t); | |
2428 | binder_stats_deleted(BINDER_STAT_TRANSACTION); | |
2429 | } | |
2430 | break; | |
2431 | } | |
2432 | ||
2433 | done: | |
2434 | ||
2435 | *consumed = ptr - buffer; | |
2436 | if (proc->requested_threads + proc->ready_threads == 0 && | |
2437 | proc->requested_threads_started < proc->max_threads && | |
2438 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | | |
2439 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ | |
2440 | /*spawn a new thread if we leave this out */) { | |
2441 | proc->requested_threads++; | |
2442 | binder_debug(BINDER_DEBUG_THREADS, | |
2443 | "binder: %d:%d BR_SPAWN_LOOPER\n", | |
2444 | proc->pid, thread->pid); | |
2445 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) | |
2446 | return -EFAULT; | |
89334ab4 | 2447 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
355b0502 GKH |
2448 | } |
2449 | return 0; | |
2450 | } | |
2451 | ||
2452 | static void binder_release_work(struct list_head *list) | |
2453 | { | |
2454 | struct binder_work *w; | |
2455 | while (!list_empty(list)) { | |
2456 | w = list_first_entry(list, struct binder_work, entry); | |
2457 | list_del_init(&w->entry); | |
2458 | switch (w->type) { | |
2459 | case BINDER_WORK_TRANSACTION: { | |
2460 | struct binder_transaction *t; | |
2461 | ||
2462 | t = container_of(w, struct binder_transaction, work); | |
675d66b0 AH |
2463 | if (t->buffer->target_node && |
2464 | !(t->flags & TF_ONE_WAY)) { | |
355b0502 | 2465 | binder_send_failed_reply(t, BR_DEAD_REPLY); |
675d66b0 AH |
2466 | } else { |
2467 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | |
2468 | "binder: undelivered transaction %d\n", | |
2469 | t->debug_id); | |
2470 | t->buffer->transaction = NULL; | |
2471 | kfree(t); | |
2472 | binder_stats_deleted(BINDER_STAT_TRANSACTION); | |
2473 | } | |
355b0502 GKH |
2474 | } break; |
2475 | case BINDER_WORK_TRANSACTION_COMPLETE: { | |
675d66b0 AH |
2476 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
2477 | "binder: undelivered TRANSACTION_COMPLETE\n"); | |
355b0502 GKH |
2478 | kfree(w); |
2479 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); | |
2480 | } break; | |
675d66b0 AH |
2481 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
2482 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { | |
2483 | struct binder_ref_death *death; | |
2484 | ||
2485 | death = container_of(w, struct binder_ref_death, work); | |
2486 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | |
2487 | "binder: undelivered death notification, %p\n", | |
2488 | death->cookie); | |
2489 | kfree(death); | |
2490 | binder_stats_deleted(BINDER_STAT_DEATH); | |
2491 | } break; | |
355b0502 | 2492 | default: |
675d66b0 AH |
2493 | pr_err("binder: unexpected work type, %d, not freed\n", |
2494 | w->type); | |
355b0502 GKH |
2495 | break; |
2496 | } | |
2497 | } | |
2498 | ||
2499 | } | |
2500 | ||
2501 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) | |
2502 | { | |
2503 | struct binder_thread *thread = NULL; | |
2504 | struct rb_node *parent = NULL; | |
2505 | struct rb_node **p = &proc->threads.rb_node; | |
2506 | ||
2507 | while (*p) { | |
2508 | parent = *p; | |
2509 | thread = rb_entry(parent, struct binder_thread, rb_node); | |
2510 | ||
2511 | if (current->pid < thread->pid) | |
2512 | p = &(*p)->rb_left; | |
2513 | else if (current->pid > thread->pid) | |
2514 | p = &(*p)->rb_right; | |
2515 | else | |
2516 | break; | |
2517 | } | |
2518 | if (*p == NULL) { | |
2519 | thread = kzalloc(sizeof(*thread), GFP_KERNEL); | |
2520 | if (thread == NULL) | |
2521 | return NULL; | |
2522 | binder_stats_created(BINDER_STAT_THREAD); | |
2523 | thread->proc = proc; | |
2524 | thread->pid = current->pid; | |
2525 | init_waitqueue_head(&thread->wait); | |
2526 | INIT_LIST_HEAD(&thread->todo); | |
2527 | rb_link_node(&thread->rb_node, parent, p); | |
2528 | rb_insert_color(&thread->rb_node, &proc->threads); | |
2529 | thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN; | |
2530 | thread->return_error = BR_OK; | |
2531 | thread->return_error2 = BR_OK; | |
2532 | } | |
2533 | return thread; | |
2534 | } | |
2535 | ||
2536 | static int binder_free_thread(struct binder_proc *proc, | |
2537 | struct binder_thread *thread) | |
2538 | { | |
2539 | struct binder_transaction *t; | |
2540 | struct binder_transaction *send_reply = NULL; | |
2541 | int active_transactions = 0; | |
2542 | ||
2543 | rb_erase(&thread->rb_node, &proc->threads); | |
2544 | t = thread->transaction_stack; | |
2545 | if (t && t->to_thread == thread) | |
2546 | send_reply = t; | |
2547 | while (t) { | |
2548 | active_transactions++; | |
2549 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, | |
2550 | "binder: release %d:%d transaction %d " | |
2551 | "%s, still active\n", proc->pid, thread->pid, | |
2552 | t->debug_id, | |
2553 | (t->to_thread == thread) ? "in" : "out"); | |
2554 | ||
2555 | if (t->to_thread == thread) { | |
2556 | t->to_proc = NULL; | |
2557 | t->to_thread = NULL; | |
2558 | if (t->buffer) { | |
2559 | t->buffer->transaction = NULL; | |
2560 | t->buffer = NULL; | |
2561 | } | |
2562 | t = t->to_parent; | |
2563 | } else if (t->from == thread) { | |
2564 | t->from = NULL; | |
2565 | t = t->from_parent; | |
2566 | } else | |
2567 | BUG(); | |
2568 | } | |
2569 | if (send_reply) | |
2570 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); | |
2571 | binder_release_work(&thread->todo); | |
2572 | kfree(thread); | |
2573 | binder_stats_deleted(BINDER_STAT_THREAD); | |
2574 | return active_transactions; | |
2575 | } | |
2576 | ||
2577 | static unsigned int binder_poll(struct file *filp, | |
2578 | struct poll_table_struct *wait) | |
2579 | { | |
2580 | struct binder_proc *proc = filp->private_data; | |
2581 | struct binder_thread *thread = NULL; | |
2582 | int wait_for_proc_work; | |
2583 | ||
975a1ac9 AH |
2584 | binder_lock(__func__); |
2585 | ||
355b0502 GKH |
2586 | thread = binder_get_thread(proc); |
2587 | ||
2588 | wait_for_proc_work = thread->transaction_stack == NULL && | |
2589 | list_empty(&thread->todo) && thread->return_error == BR_OK; | |
975a1ac9 AH |
2590 | |
2591 | binder_unlock(__func__); | |
355b0502 GKH |
2592 | |
2593 | if (wait_for_proc_work) { | |
2594 | if (binder_has_proc_work(proc, thread)) | |
2595 | return POLLIN; | |
2596 | poll_wait(filp, &proc->wait, wait); | |
2597 | if (binder_has_proc_work(proc, thread)) | |
2598 | return POLLIN; | |
2599 | } else { | |
2600 | if (binder_has_thread_work(thread)) | |
2601 | return POLLIN; | |
2602 | poll_wait(filp, &thread->wait, wait); | |
2603 | if (binder_has_thread_work(thread)) | |
2604 | return POLLIN; | |
2605 | } | |
2606 | return 0; | |
2607 | } | |
2608 | ||
2609 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
2610 | { | |
2611 | int ret; | |
2612 | struct binder_proc *proc = filp->private_data; | |
2613 | struct binder_thread *thread; | |
2614 | unsigned int size = _IOC_SIZE(cmd); | |
2615 | void __user *ubuf = (void __user *)arg; | |
2616 | ||
258767fe | 2617 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/ |
355b0502 | 2618 | |
975a1ac9 AH |
2619 | trace_binder_ioctl(cmd, arg); |
2620 | ||
355b0502 GKH |
2621 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
2622 | if (ret) | |
975a1ac9 | 2623 | goto err_unlocked; |
355b0502 | 2624 | |
975a1ac9 | 2625 | binder_lock(__func__); |
355b0502 GKH |
2626 | thread = binder_get_thread(proc); |
2627 | if (thread == NULL) { | |
2628 | ret = -ENOMEM; | |
2629 | goto err; | |
2630 | } | |
2631 | ||
2632 | switch (cmd) { | |
2633 | case BINDER_WRITE_READ: { | |
2634 | struct binder_write_read bwr; | |
2635 | if (size != sizeof(struct binder_write_read)) { | |
2636 | ret = -EINVAL; | |
2637 | goto err; | |
2638 | } | |
2639 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { | |
2640 | ret = -EFAULT; | |
2641 | goto err; | |
2642 | } | |
2643 | binder_debug(BINDER_DEBUG_READ_WRITE, | |
2644 | "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n", | |
2645 | proc->pid, thread->pid, bwr.write_size, bwr.write_buffer, | |
2646 | bwr.read_size, bwr.read_buffer); | |
2647 | ||
2648 | if (bwr.write_size > 0) { | |
2649 | ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed); | |
975a1ac9 | 2650 | trace_binder_write_done(ret); |
355b0502 GKH |
2651 | if (ret < 0) { |
2652 | bwr.read_consumed = 0; | |
2653 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) | |
2654 | ret = -EFAULT; | |
2655 | goto err; | |
2656 | } | |
2657 | } | |
2658 | if (bwr.read_size > 0) { | |
2659 | ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK); | |
975a1ac9 | 2660 | trace_binder_read_done(ret); |
355b0502 GKH |
2661 | if (!list_empty(&proc->todo)) |
2662 | wake_up_interruptible(&proc->wait); | |
2663 | if (ret < 0) { | |
2664 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) | |
2665 | ret = -EFAULT; | |
2666 | goto err; | |
2667 | } | |
2668 | } | |
2669 | binder_debug(BINDER_DEBUG_READ_WRITE, | |
2670 | "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n", | |
2671 | proc->pid, thread->pid, bwr.write_consumed, bwr.write_size, | |
2672 | bwr.read_consumed, bwr.read_size); | |
2673 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { | |
2674 | ret = -EFAULT; | |
2675 | goto err; | |
2676 | } | |
2677 | break; | |
2678 | } | |
2679 | case BINDER_SET_MAX_THREADS: | |
2680 | if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) { | |
2681 | ret = -EINVAL; | |
2682 | goto err; | |
2683 | } | |
2684 | break; | |
2685 | case BINDER_SET_CONTEXT_MGR: | |
2686 | if (binder_context_mgr_node != NULL) { | |
258767fe | 2687 | pr_err("binder: BINDER_SET_CONTEXT_MGR already set\n"); |
355b0502 GKH |
2688 | ret = -EBUSY; |
2689 | goto err; | |
2690 | } | |
4a2ebb93 EB |
2691 | if (uid_valid(binder_context_mgr_uid)) { |
2692 | if (!uid_eq(binder_context_mgr_uid, current->cred->euid)) { | |
258767fe | 2693 | pr_err("binder: BINDER_SET_" |
355b0502 | 2694 | "CONTEXT_MGR bad uid %d != %d\n", |
4a2ebb93 EB |
2695 | from_kuid(&init_user_ns, current->cred->euid), |
2696 | from_kuid(&init_user_ns, binder_context_mgr_uid)); | |
355b0502 GKH |
2697 | ret = -EPERM; |
2698 | goto err; | |
2699 | } | |
2700 | } else | |
2701 | binder_context_mgr_uid = current->cred->euid; | |
2702 | binder_context_mgr_node = binder_new_node(proc, NULL, NULL); | |
2703 | if (binder_context_mgr_node == NULL) { | |
2704 | ret = -ENOMEM; | |
2705 | goto err; | |
2706 | } | |
2707 | binder_context_mgr_node->local_weak_refs++; | |
2708 | binder_context_mgr_node->local_strong_refs++; | |
2709 | binder_context_mgr_node->has_strong_ref = 1; | |
2710 | binder_context_mgr_node->has_weak_ref = 1; | |
2711 | break; | |
2712 | case BINDER_THREAD_EXIT: | |
2713 | binder_debug(BINDER_DEBUG_THREADS, "binder: %d:%d exit\n", | |
2714 | proc->pid, thread->pid); | |
2715 | binder_free_thread(proc, thread); | |
2716 | thread = NULL; | |
2717 | break; | |
2718 | case BINDER_VERSION: | |
2719 | if (size != sizeof(struct binder_version)) { | |
2720 | ret = -EINVAL; | |
2721 | goto err; | |
2722 | } | |
2723 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) { | |
2724 | ret = -EINVAL; | |
2725 | goto err; | |
2726 | } | |
2727 | break; | |
2728 | default: | |
2729 | ret = -EINVAL; | |
2730 | goto err; | |
2731 | } | |
2732 | ret = 0; | |
2733 | err: | |
2734 | if (thread) | |
2735 | thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN; | |
975a1ac9 | 2736 | binder_unlock(__func__); |
355b0502 GKH |
2737 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
2738 | if (ret && ret != -ERESTARTSYS) | |
258767fe | 2739 | pr_info("binder: %d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret); |
975a1ac9 AH |
2740 | err_unlocked: |
2741 | trace_binder_ioctl_done(ret); | |
355b0502 GKH |
2742 | return ret; |
2743 | } | |
2744 | ||
2745 | static void binder_vma_open(struct vm_area_struct *vma) | |
2746 | { | |
2747 | struct binder_proc *proc = vma->vm_private_data; | |
2748 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | |
2749 | "binder: %d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", | |
2750 | proc->pid, vma->vm_start, vma->vm_end, | |
2751 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, | |
2752 | (unsigned long)pgprot_val(vma->vm_page_prot)); | |
355b0502 GKH |
2753 | } |
2754 | ||
2755 | static void binder_vma_close(struct vm_area_struct *vma) | |
2756 | { | |
2757 | struct binder_proc *proc = vma->vm_private_data; | |
2758 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | |
2759 | "binder: %d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", | |
2760 | proc->pid, vma->vm_start, vma->vm_end, | |
2761 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, | |
2762 | (unsigned long)pgprot_val(vma->vm_page_prot)); | |
2763 | proc->vma = NULL; | |
2a90957f | 2764 | proc->vma_vm_mm = NULL; |
355b0502 GKH |
2765 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
2766 | } | |
2767 | ||
2768 | static struct vm_operations_struct binder_vm_ops = { | |
2769 | .open = binder_vma_open, | |
2770 | .close = binder_vma_close, | |
2771 | }; | |
2772 | ||
2773 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) | |
2774 | { | |
2775 | int ret; | |
2776 | struct vm_struct *area; | |
2777 | struct binder_proc *proc = filp->private_data; | |
2778 | const char *failure_string; | |
2779 | struct binder_buffer *buffer; | |
2780 | ||
a79f41ed AV |
2781 | if (proc->tsk != current) |
2782 | return -EINVAL; | |
2783 | ||
355b0502 GKH |
2784 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
2785 | vma->vm_end = vma->vm_start + SZ_4M; | |
2786 | ||
2787 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | |
2788 | "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", | |
2789 | proc->pid, vma->vm_start, vma->vm_end, | |
2790 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, | |
2791 | (unsigned long)pgprot_val(vma->vm_page_prot)); | |
2792 | ||
2793 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { | |
2794 | ret = -EPERM; | |
2795 | failure_string = "bad vm_flags"; | |
2796 | goto err_bad_arg; | |
2797 | } | |
2798 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; | |
2799 | ||
bd1eff97 | 2800 | mutex_lock(&binder_mmap_lock); |
355b0502 GKH |
2801 | if (proc->buffer) { |
2802 | ret = -EBUSY; | |
2803 | failure_string = "already mapped"; | |
2804 | goto err_already_mapped; | |
2805 | } | |
2806 | ||
2807 | area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP); | |
2808 | if (area == NULL) { | |
2809 | ret = -ENOMEM; | |
2810 | failure_string = "get_vm_area"; | |
2811 | goto err_get_vm_area_failed; | |
2812 | } | |
2813 | proc->buffer = area->addr; | |
2814 | proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer; | |
bd1eff97 | 2815 | mutex_unlock(&binder_mmap_lock); |
355b0502 GKH |
2816 | |
2817 | #ifdef CONFIG_CPU_CACHE_VIPT | |
2818 | if (cache_is_vipt_aliasing()) { | |
2819 | while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) { | |
258767fe | 2820 | pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer); |
355b0502 GKH |
2821 | vma->vm_start += PAGE_SIZE; |
2822 | } | |
2823 | } | |
2824 | #endif | |
2825 | proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL); | |
2826 | if (proc->pages == NULL) { | |
2827 | ret = -ENOMEM; | |
2828 | failure_string = "alloc page array"; | |
2829 | goto err_alloc_pages_failed; | |
2830 | } | |
2831 | proc->buffer_size = vma->vm_end - vma->vm_start; | |
2832 | ||
2833 | vma->vm_ops = &binder_vm_ops; | |
2834 | vma->vm_private_data = proc; | |
2835 | ||
2836 | if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) { | |
2837 | ret = -ENOMEM; | |
2838 | failure_string = "alloc small buf"; | |
2839 | goto err_alloc_small_buf_failed; | |
2840 | } | |
2841 | buffer = proc->buffer; | |
2842 | INIT_LIST_HEAD(&proc->buffers); | |
2843 | list_add(&buffer->entry, &proc->buffers); | |
2844 | buffer->free = 1; | |
2845 | binder_insert_free_buffer(proc, buffer); | |
2846 | proc->free_async_space = proc->buffer_size / 2; | |
2847 | barrier(); | |
a79f41ed | 2848 | proc->files = get_files_struct(current); |
355b0502 | 2849 | proc->vma = vma; |
2a90957f | 2850 | proc->vma_vm_mm = vma->vm_mm; |
355b0502 | 2851 | |
258767fe | 2852 | /*pr_info("binder_mmap: %d %lx-%lx maps %p\n", |
355b0502 GKH |
2853 | proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/ |
2854 | return 0; | |
2855 | ||
2856 | err_alloc_small_buf_failed: | |
2857 | kfree(proc->pages); | |
2858 | proc->pages = NULL; | |
2859 | err_alloc_pages_failed: | |
bd1eff97 | 2860 | mutex_lock(&binder_mmap_lock); |
355b0502 GKH |
2861 | vfree(proc->buffer); |
2862 | proc->buffer = NULL; | |
2863 | err_get_vm_area_failed: | |
2864 | err_already_mapped: | |
bd1eff97 | 2865 | mutex_unlock(&binder_mmap_lock); |
355b0502 | 2866 | err_bad_arg: |
258767fe | 2867 | pr_err("binder_mmap: %d %lx-%lx %s failed %d\n", |
355b0502 GKH |
2868 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
2869 | return ret; | |
2870 | } | |
2871 | ||
2872 | static int binder_open(struct inode *nodp, struct file *filp) | |
2873 | { | |
2874 | struct binder_proc *proc; | |
2875 | ||
2876 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n", | |
2877 | current->group_leader->pid, current->pid); | |
2878 | ||
2879 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); | |
2880 | if (proc == NULL) | |
2881 | return -ENOMEM; | |
2882 | get_task_struct(current); | |
2883 | proc->tsk = current; | |
2884 | INIT_LIST_HEAD(&proc->todo); | |
2885 | init_waitqueue_head(&proc->wait); | |
2886 | proc->default_priority = task_nice(current); | |
975a1ac9 AH |
2887 | |
2888 | binder_lock(__func__); | |
2889 | ||
355b0502 GKH |
2890 | binder_stats_created(BINDER_STAT_PROC); |
2891 | hlist_add_head(&proc->proc_node, &binder_procs); | |
2892 | proc->pid = current->group_leader->pid; | |
2893 | INIT_LIST_HEAD(&proc->delivered_death); | |
2894 | filp->private_data = proc; | |
975a1ac9 AH |
2895 | |
2896 | binder_unlock(__func__); | |
355b0502 | 2897 | |
16b66554 | 2898 | if (binder_debugfs_dir_entry_proc) { |
355b0502 GKH |
2899 | char strbuf[11]; |
2900 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); | |
16b66554 AH |
2901 | proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO, |
2902 | binder_debugfs_dir_entry_proc, proc, &binder_proc_fops); | |
355b0502 GKH |
2903 | } |
2904 | ||
2905 | return 0; | |
2906 | } | |
2907 | ||
2908 | static int binder_flush(struct file *filp, fl_owner_t id) | |
2909 | { | |
2910 | struct binder_proc *proc = filp->private_data; | |
2911 | ||
2912 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); | |
2913 | ||
2914 | return 0; | |
2915 | } | |
2916 | ||
2917 | static void binder_deferred_flush(struct binder_proc *proc) | |
2918 | { | |
2919 | struct rb_node *n; | |
2920 | int wake_count = 0; | |
2921 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { | |
2922 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); | |
2923 | thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN; | |
2924 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { | |
2925 | wake_up_interruptible(&thread->wait); | |
2926 | wake_count++; | |
2927 | } | |
2928 | } | |
2929 | wake_up_interruptible_all(&proc->wait); | |
2930 | ||
2931 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | |
2932 | "binder_flush: %d woke %d threads\n", proc->pid, | |
2933 | wake_count); | |
2934 | } | |
2935 | ||
2936 | static int binder_release(struct inode *nodp, struct file *filp) | |
2937 | { | |
2938 | struct binder_proc *proc = filp->private_data; | |
16b66554 | 2939 | debugfs_remove(proc->debugfs_entry); |
355b0502 GKH |
2940 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
2941 | ||
2942 | return 0; | |
2943 | } | |
2944 | ||
2945 | static void binder_deferred_release(struct binder_proc *proc) | |
2946 | { | |
2947 | struct hlist_node *pos; | |
2948 | struct binder_transaction *t; | |
2949 | struct rb_node *n; | |
2950 | int threads, nodes, incoming_refs, outgoing_refs, buffers, active_transactions, page_count; | |
2951 | ||
2952 | BUG_ON(proc->vma); | |
2953 | BUG_ON(proc->files); | |
2954 | ||
2955 | hlist_del(&proc->proc_node); | |
2956 | if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) { | |
2957 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | |
2958 | "binder_release: %d context_mgr_node gone\n", | |
2959 | proc->pid); | |
2960 | binder_context_mgr_node = NULL; | |
2961 | } | |
2962 | ||
2963 | threads = 0; | |
2964 | active_transactions = 0; | |
2965 | while ((n = rb_first(&proc->threads))) { | |
2966 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); | |
2967 | threads++; | |
2968 | active_transactions += binder_free_thread(proc, thread); | |
2969 | } | |
2970 | nodes = 0; | |
2971 | incoming_refs = 0; | |
2972 | while ((n = rb_first(&proc->nodes))) { | |
2973 | struct binder_node *node = rb_entry(n, struct binder_node, rb_node); | |
2974 | ||
2975 | nodes++; | |
2976 | rb_erase(&node->rb_node, &proc->nodes); | |
2977 | list_del_init(&node->work.entry); | |
675d66b0 | 2978 | binder_release_work(&node->async_todo); |
355b0502 GKH |
2979 | if (hlist_empty(&node->refs)) { |
2980 | kfree(node); | |
2981 | binder_stats_deleted(BINDER_STAT_NODE); | |
2982 | } else { | |
2983 | struct binder_ref *ref; | |
2984 | int death = 0; | |
2985 | ||
2986 | node->proc = NULL; | |
2987 | node->local_strong_refs = 0; | |
2988 | node->local_weak_refs = 0; | |
2989 | hlist_add_head(&node->dead_node, &binder_dead_nodes); | |
2990 | ||
2991 | hlist_for_each_entry(ref, pos, &node->refs, node_entry) { | |
2992 | incoming_refs++; | |
2993 | if (ref->death) { | |
2994 | death++; | |
2995 | if (list_empty(&ref->death->work.entry)) { | |
2996 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; | |
2997 | list_add_tail(&ref->death->work.entry, &ref->proc->todo); | |
2998 | wake_up_interruptible(&ref->proc->wait); | |
2999 | } else | |
3000 | BUG(); | |
3001 | } | |
3002 | } | |
3003 | binder_debug(BINDER_DEBUG_DEAD_BINDER, | |
3004 | "binder: node %d now dead, " | |
3005 | "refs %d, death %d\n", node->debug_id, | |
3006 | incoming_refs, death); | |
3007 | } | |
3008 | } | |
3009 | outgoing_refs = 0; | |
3010 | while ((n = rb_first(&proc->refs_by_desc))) { | |
3011 | struct binder_ref *ref = rb_entry(n, struct binder_ref, | |
3012 | rb_node_desc); | |
3013 | outgoing_refs++; | |
3014 | binder_delete_ref(ref); | |
3015 | } | |
3016 | binder_release_work(&proc->todo); | |
675d66b0 | 3017 | binder_release_work(&proc->delivered_death); |
355b0502 GKH |
3018 | buffers = 0; |
3019 | ||
3020 | while ((n = rb_first(&proc->allocated_buffers))) { | |
3021 | struct binder_buffer *buffer = rb_entry(n, struct binder_buffer, | |
3022 | rb_node); | |
3023 | t = buffer->transaction; | |
3024 | if (t) { | |
3025 | t->buffer = NULL; | |
3026 | buffer->transaction = NULL; | |
258767fe | 3027 | pr_err("binder: release proc %d, " |
355b0502 GKH |
3028 | "transaction %d, not freed\n", |
3029 | proc->pid, t->debug_id); | |
3030 | /*BUG();*/ | |
3031 | } | |
3032 | binder_free_buf(proc, buffer); | |
3033 | buffers++; | |
3034 | } | |
3035 | ||
3036 | binder_stats_deleted(BINDER_STAT_PROC); | |
3037 | ||
3038 | page_count = 0; | |
3039 | if (proc->pages) { | |
3040 | int i; | |
3041 | for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) { | |
3042 | if (proc->pages[i]) { | |
58526090 | 3043 | void *page_addr = proc->buffer + i * PAGE_SIZE; |
355b0502 GKH |
3044 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
3045 | "binder_release: %d: " | |
3046 | "page %d at %p not freed\n", | |
3047 | proc->pid, i, | |
58526090 CL |
3048 | page_addr); |
3049 | unmap_kernel_range((unsigned long)page_addr, | |
3050 | PAGE_SIZE); | |
355b0502 GKH |
3051 | __free_page(proc->pages[i]); |
3052 | page_count++; | |
3053 | } | |
3054 | } | |
3055 | kfree(proc->pages); | |
3056 | vfree(proc->buffer); | |
3057 | } | |
3058 | ||
3059 | put_task_struct(proc->tsk); | |
3060 | ||
3061 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, | |
3062 | "binder_release: %d threads %d, nodes %d (ref %d), " | |
3063 | "refs %d, active transactions %d, buffers %d, " | |
3064 | "pages %d\n", | |
3065 | proc->pid, threads, nodes, incoming_refs, outgoing_refs, | |
3066 | active_transactions, buffers, page_count); | |
3067 | ||
3068 | kfree(proc); | |
3069 | } | |
3070 | ||
3071 | static void binder_deferred_func(struct work_struct *work) | |
3072 | { | |
3073 | struct binder_proc *proc; | |
3074 | struct files_struct *files; | |
3075 | ||
3076 | int defer; | |
3077 | do { | |
975a1ac9 | 3078 | binder_lock(__func__); |
355b0502 GKH |
3079 | mutex_lock(&binder_deferred_lock); |
3080 | if (!hlist_empty(&binder_deferred_list)) { | |
3081 | proc = hlist_entry(binder_deferred_list.first, | |
3082 | struct binder_proc, deferred_work_node); | |
3083 | hlist_del_init(&proc->deferred_work_node); | |
3084 | defer = proc->deferred_work; | |
3085 | proc->deferred_work = 0; | |
3086 | } else { | |
3087 | proc = NULL; | |
3088 | defer = 0; | |
3089 | } | |
3090 | mutex_unlock(&binder_deferred_lock); | |
3091 | ||
3092 | files = NULL; | |
3093 | if (defer & BINDER_DEFERRED_PUT_FILES) { | |
3094 | files = proc->files; | |
3095 | if (files) | |
3096 | proc->files = NULL; | |
3097 | } | |
3098 | ||
3099 | if (defer & BINDER_DEFERRED_FLUSH) | |
3100 | binder_deferred_flush(proc); | |
3101 | ||
3102 | if (defer & BINDER_DEFERRED_RELEASE) | |
3103 | binder_deferred_release(proc); /* frees proc */ | |
3104 | ||
975a1ac9 | 3105 | binder_unlock(__func__); |
355b0502 GKH |
3106 | if (files) |
3107 | put_files_struct(files); | |
3108 | } while (proc); | |
3109 | } | |
3110 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); | |
3111 | ||
3112 | static void | |
3113 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) | |
3114 | { | |
3115 | mutex_lock(&binder_deferred_lock); | |
3116 | proc->deferred_work |= defer; | |
3117 | if (hlist_unhashed(&proc->deferred_work_node)) { | |
3118 | hlist_add_head(&proc->deferred_work_node, | |
3119 | &binder_deferred_list); | |
3c762a49 | 3120 | queue_work(binder_deferred_workqueue, &binder_deferred_work); |
355b0502 GKH |
3121 | } |
3122 | mutex_unlock(&binder_deferred_lock); | |
3123 | } | |
3124 | ||
5249f488 AH |
3125 | static void print_binder_transaction(struct seq_file *m, const char *prefix, |
3126 | struct binder_transaction *t) | |
3127 | { | |
3128 | seq_printf(m, | |
3129 | "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d", | |
3130 | prefix, t->debug_id, t, | |
3131 | t->from ? t->from->proc->pid : 0, | |
3132 | t->from ? t->from->pid : 0, | |
3133 | t->to_proc ? t->to_proc->pid : 0, | |
3134 | t->to_thread ? t->to_thread->pid : 0, | |
3135 | t->code, t->flags, t->priority, t->need_reply); | |
355b0502 | 3136 | if (t->buffer == NULL) { |
5249f488 AH |
3137 | seq_puts(m, " buffer free\n"); |
3138 | return; | |
355b0502 | 3139 | } |
5249f488 AH |
3140 | if (t->buffer->target_node) |
3141 | seq_printf(m, " node %d", | |
3142 | t->buffer->target_node->debug_id); | |
3143 | seq_printf(m, " size %zd:%zd data %p\n", | |
3144 | t->buffer->data_size, t->buffer->offsets_size, | |
3145 | t->buffer->data); | |
355b0502 GKH |
3146 | } |
3147 | ||
5249f488 AH |
3148 | static void print_binder_buffer(struct seq_file *m, const char *prefix, |
3149 | struct binder_buffer *buffer) | |
355b0502 | 3150 | { |
5249f488 AH |
3151 | seq_printf(m, "%s %d: %p size %zd:%zd %s\n", |
3152 | prefix, buffer->debug_id, buffer->data, | |
3153 | buffer->data_size, buffer->offsets_size, | |
3154 | buffer->transaction ? "active" : "delivered"); | |
355b0502 GKH |
3155 | } |
3156 | ||
5249f488 AH |
3157 | static void print_binder_work(struct seq_file *m, const char *prefix, |
3158 | const char *transaction_prefix, | |
3159 | struct binder_work *w) | |
355b0502 GKH |
3160 | { |
3161 | struct binder_node *node; | |
3162 | struct binder_transaction *t; | |
3163 | ||
3164 | switch (w->type) { | |
3165 | case BINDER_WORK_TRANSACTION: | |
3166 | t = container_of(w, struct binder_transaction, work); | |
5249f488 | 3167 | print_binder_transaction(m, transaction_prefix, t); |
355b0502 GKH |
3168 | break; |
3169 | case BINDER_WORK_TRANSACTION_COMPLETE: | |
5249f488 | 3170 | seq_printf(m, "%stransaction complete\n", prefix); |
355b0502 GKH |
3171 | break; |
3172 | case BINDER_WORK_NODE: | |
3173 | node = container_of(w, struct binder_node, work); | |
5249f488 AH |
3174 | seq_printf(m, "%snode work %d: u%p c%p\n", |
3175 | prefix, node->debug_id, node->ptr, node->cookie); | |
355b0502 GKH |
3176 | break; |
3177 | case BINDER_WORK_DEAD_BINDER: | |
5249f488 | 3178 | seq_printf(m, "%shas dead binder\n", prefix); |
355b0502 GKH |
3179 | break; |
3180 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: | |
5249f488 | 3181 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
355b0502 GKH |
3182 | break; |
3183 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: | |
5249f488 | 3184 | seq_printf(m, "%shas cleared death notification\n", prefix); |
355b0502 GKH |
3185 | break; |
3186 | default: | |
5249f488 | 3187 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
355b0502 GKH |
3188 | break; |
3189 | } | |
355b0502 GKH |
3190 | } |
3191 | ||
5249f488 AH |
3192 | static void print_binder_thread(struct seq_file *m, |
3193 | struct binder_thread *thread, | |
3194 | int print_always) | |
355b0502 GKH |
3195 | { |
3196 | struct binder_transaction *t; | |
3197 | struct binder_work *w; | |
5249f488 AH |
3198 | size_t start_pos = m->count; |
3199 | size_t header_pos; | |
355b0502 | 3200 | |
5249f488 AH |
3201 | seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper); |
3202 | header_pos = m->count; | |
355b0502 GKH |
3203 | t = thread->transaction_stack; |
3204 | while (t) { | |
355b0502 | 3205 | if (t->from == thread) { |
5249f488 AH |
3206 | print_binder_transaction(m, |
3207 | " outgoing transaction", t); | |
355b0502 GKH |
3208 | t = t->from_parent; |
3209 | } else if (t->to_thread == thread) { | |
5249f488 AH |
3210 | print_binder_transaction(m, |
3211 | " incoming transaction", t); | |
355b0502 GKH |
3212 | t = t->to_parent; |
3213 | } else { | |
5249f488 | 3214 | print_binder_transaction(m, " bad transaction", t); |
355b0502 GKH |
3215 | t = NULL; |
3216 | } | |
3217 | } | |
3218 | list_for_each_entry(w, &thread->todo, entry) { | |
5249f488 | 3219 | print_binder_work(m, " ", " pending transaction", w); |
355b0502 | 3220 | } |
5249f488 AH |
3221 | if (!print_always && m->count == header_pos) |
3222 | m->count = start_pos; | |
355b0502 GKH |
3223 | } |
3224 | ||
5249f488 | 3225 | static void print_binder_node(struct seq_file *m, struct binder_node *node) |
355b0502 GKH |
3226 | { |
3227 | struct binder_ref *ref; | |
3228 | struct hlist_node *pos; | |
3229 | struct binder_work *w; | |
3230 | int count; | |
3231 | ||
3232 | count = 0; | |
3233 | hlist_for_each_entry(ref, pos, &node->refs, node_entry) | |
3234 | count++; | |
3235 | ||
5249f488 AH |
3236 | seq_printf(m, " node %d: u%p c%p hs %d hw %d ls %d lw %d is %d iw %d", |
3237 | node->debug_id, node->ptr, node->cookie, | |
3238 | node->has_strong_ref, node->has_weak_ref, | |
3239 | node->local_strong_refs, node->local_weak_refs, | |
3240 | node->internal_strong_refs, count); | |
355b0502 | 3241 | if (count) { |
5249f488 AH |
3242 | seq_puts(m, " proc"); |
3243 | hlist_for_each_entry(ref, pos, &node->refs, node_entry) | |
3244 | seq_printf(m, " %d", ref->proc->pid); | |
355b0502 | 3245 | } |
5249f488 AH |
3246 | seq_puts(m, "\n"); |
3247 | list_for_each_entry(w, &node->async_todo, entry) | |
3248 | print_binder_work(m, " ", | |
3249 | " pending async transaction", w); | |
355b0502 GKH |
3250 | } |
3251 | ||
5249f488 | 3252 | static void print_binder_ref(struct seq_file *m, struct binder_ref *ref) |
355b0502 | 3253 | { |
5249f488 AH |
3254 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n", |
3255 | ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ", | |
3256 | ref->node->debug_id, ref->strong, ref->weak, ref->death); | |
355b0502 GKH |
3257 | } |
3258 | ||
5249f488 AH |
3259 | static void print_binder_proc(struct seq_file *m, |
3260 | struct binder_proc *proc, int print_all) | |
355b0502 GKH |
3261 | { |
3262 | struct binder_work *w; | |
3263 | struct rb_node *n; | |
5249f488 AH |
3264 | size_t start_pos = m->count; |
3265 | size_t header_pos; | |
3266 | ||
3267 | seq_printf(m, "proc %d\n", proc->pid); | |
3268 | header_pos = m->count; | |
3269 | ||
3270 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) | |
3271 | print_binder_thread(m, rb_entry(n, struct binder_thread, | |
3272 | rb_node), print_all); | |
3273 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { | |
355b0502 GKH |
3274 | struct binder_node *node = rb_entry(n, struct binder_node, |
3275 | rb_node); | |
3276 | if (print_all || node->has_async_transaction) | |
5249f488 | 3277 | print_binder_node(m, node); |
355b0502 GKH |
3278 | } |
3279 | if (print_all) { | |
3280 | for (n = rb_first(&proc->refs_by_desc); | |
5249f488 | 3281 | n != NULL; |
355b0502 | 3282 | n = rb_next(n)) |
5249f488 AH |
3283 | print_binder_ref(m, rb_entry(n, struct binder_ref, |
3284 | rb_node_desc)); | |
355b0502 | 3285 | } |
5249f488 AH |
3286 | for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n)) |
3287 | print_binder_buffer(m, " buffer", | |
3288 | rb_entry(n, struct binder_buffer, rb_node)); | |
3289 | list_for_each_entry(w, &proc->todo, entry) | |
3290 | print_binder_work(m, " ", " pending transaction", w); | |
355b0502 | 3291 | list_for_each_entry(w, &proc->delivered_death, entry) { |
5249f488 | 3292 | seq_puts(m, " has delivered dead binder\n"); |
355b0502 GKH |
3293 | break; |
3294 | } | |
5249f488 AH |
3295 | if (!print_all && m->count == header_pos) |
3296 | m->count = start_pos; | |
355b0502 GKH |
3297 | } |
3298 | ||
3299 | static const char *binder_return_strings[] = { | |
3300 | "BR_ERROR", | |
3301 | "BR_OK", | |
3302 | "BR_TRANSACTION", | |
3303 | "BR_REPLY", | |
3304 | "BR_ACQUIRE_RESULT", | |
3305 | "BR_DEAD_REPLY", | |
3306 | "BR_TRANSACTION_COMPLETE", | |
3307 | "BR_INCREFS", | |
3308 | "BR_ACQUIRE", | |
3309 | "BR_RELEASE", | |
3310 | "BR_DECREFS", | |
3311 | "BR_ATTEMPT_ACQUIRE", | |
3312 | "BR_NOOP", | |
3313 | "BR_SPAWN_LOOPER", | |
3314 | "BR_FINISHED", | |
3315 | "BR_DEAD_BINDER", | |
3316 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", | |
3317 | "BR_FAILED_REPLY" | |
3318 | }; | |
3319 | ||
3320 | static const char *binder_command_strings[] = { | |
3321 | "BC_TRANSACTION", | |
3322 | "BC_REPLY", | |
3323 | "BC_ACQUIRE_RESULT", | |
3324 | "BC_FREE_BUFFER", | |
3325 | "BC_INCREFS", | |
3326 | "BC_ACQUIRE", | |
3327 | "BC_RELEASE", | |
3328 | "BC_DECREFS", | |
3329 | "BC_INCREFS_DONE", | |
3330 | "BC_ACQUIRE_DONE", | |
3331 | "BC_ATTEMPT_ACQUIRE", | |
3332 | "BC_REGISTER_LOOPER", | |
3333 | "BC_ENTER_LOOPER", | |
3334 | "BC_EXIT_LOOPER", | |
3335 | "BC_REQUEST_DEATH_NOTIFICATION", | |
3336 | "BC_CLEAR_DEATH_NOTIFICATION", | |
3337 | "BC_DEAD_BINDER_DONE" | |
3338 | }; | |
3339 | ||
3340 | static const char *binder_objstat_strings[] = { | |
3341 | "proc", | |
3342 | "thread", | |
3343 | "node", | |
3344 | "ref", | |
3345 | "death", | |
3346 | "transaction", | |
3347 | "transaction_complete" | |
3348 | }; | |
3349 | ||
5249f488 AH |
3350 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
3351 | struct binder_stats *stats) | |
355b0502 GKH |
3352 | { |
3353 | int i; | |
3354 | ||
3355 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != | |
5249f488 | 3356 | ARRAY_SIZE(binder_command_strings)); |
355b0502 GKH |
3357 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
3358 | if (stats->bc[i]) | |
5249f488 AH |
3359 | seq_printf(m, "%s%s: %d\n", prefix, |
3360 | binder_command_strings[i], stats->bc[i]); | |
355b0502 GKH |
3361 | } |
3362 | ||
3363 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != | |
5249f488 | 3364 | ARRAY_SIZE(binder_return_strings)); |
355b0502 GKH |
3365 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
3366 | if (stats->br[i]) | |
5249f488 AH |
3367 | seq_printf(m, "%s%s: %d\n", prefix, |
3368 | binder_return_strings[i], stats->br[i]); | |
355b0502 GKH |
3369 | } |
3370 | ||
3371 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != | |
5249f488 | 3372 | ARRAY_SIZE(binder_objstat_strings)); |
355b0502 | 3373 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
5249f488 | 3374 | ARRAY_SIZE(stats->obj_deleted)); |
355b0502 GKH |
3375 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
3376 | if (stats->obj_created[i] || stats->obj_deleted[i]) | |
5249f488 AH |
3377 | seq_printf(m, "%s%s: active %d total %d\n", prefix, |
3378 | binder_objstat_strings[i], | |
3379 | stats->obj_created[i] - stats->obj_deleted[i], | |
3380 | stats->obj_created[i]); | |
355b0502 | 3381 | } |
355b0502 GKH |
3382 | } |
3383 | ||
5249f488 AH |
3384 | static void print_binder_proc_stats(struct seq_file *m, |
3385 | struct binder_proc *proc) | |
355b0502 GKH |
3386 | { |
3387 | struct binder_work *w; | |
3388 | struct rb_node *n; | |
3389 | int count, strong, weak; | |
3390 | ||
5249f488 | 3391 | seq_printf(m, "proc %d\n", proc->pid); |
355b0502 GKH |
3392 | count = 0; |
3393 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) | |
3394 | count++; | |
5249f488 AH |
3395 | seq_printf(m, " threads: %d\n", count); |
3396 | seq_printf(m, " requested threads: %d+%d/%d\n" | |
355b0502 GKH |
3397 | " ready threads %d\n" |
3398 | " free async space %zd\n", proc->requested_threads, | |
3399 | proc->requested_threads_started, proc->max_threads, | |
3400 | proc->ready_threads, proc->free_async_space); | |
355b0502 GKH |
3401 | count = 0; |
3402 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) | |
3403 | count++; | |
5249f488 | 3404 | seq_printf(m, " nodes: %d\n", count); |
355b0502 GKH |
3405 | count = 0; |
3406 | strong = 0; | |
3407 | weak = 0; | |
3408 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { | |
3409 | struct binder_ref *ref = rb_entry(n, struct binder_ref, | |
3410 | rb_node_desc); | |
3411 | count++; | |
3412 | strong += ref->strong; | |
3413 | weak += ref->weak; | |
3414 | } | |
5249f488 | 3415 | seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak); |
355b0502 GKH |
3416 | |
3417 | count = 0; | |
3418 | for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n)) | |
3419 | count++; | |
5249f488 | 3420 | seq_printf(m, " buffers: %d\n", count); |
355b0502 GKH |
3421 | |
3422 | count = 0; | |
3423 | list_for_each_entry(w, &proc->todo, entry) { | |
3424 | switch (w->type) { | |
3425 | case BINDER_WORK_TRANSACTION: | |
3426 | count++; | |
3427 | break; | |
3428 | default: | |
3429 | break; | |
3430 | } | |
3431 | } | |
5249f488 | 3432 | seq_printf(m, " pending transactions: %d\n", count); |
355b0502 | 3433 | |
5249f488 | 3434 | print_binder_stats(m, " ", &proc->stats); |
355b0502 GKH |
3435 | } |
3436 | ||
3437 | ||
5249f488 | 3438 | static int binder_state_show(struct seq_file *m, void *unused) |
355b0502 GKH |
3439 | { |
3440 | struct binder_proc *proc; | |
3441 | struct hlist_node *pos; | |
3442 | struct binder_node *node; | |
355b0502 GKH |
3443 | int do_lock = !binder_debug_no_lock; |
3444 | ||
355b0502 | 3445 | if (do_lock) |
975a1ac9 | 3446 | binder_lock(__func__); |
355b0502 | 3447 | |
5249f488 | 3448 | seq_puts(m, "binder state:\n"); |
355b0502 GKH |
3449 | |
3450 | if (!hlist_empty(&binder_dead_nodes)) | |
5249f488 AH |
3451 | seq_puts(m, "dead nodes:\n"); |
3452 | hlist_for_each_entry(node, pos, &binder_dead_nodes, dead_node) | |
3453 | print_binder_node(m, node); | |
355b0502 | 3454 | |
5249f488 AH |
3455 | hlist_for_each_entry(proc, pos, &binder_procs, proc_node) |
3456 | print_binder_proc(m, proc, 1); | |
355b0502 | 3457 | if (do_lock) |
975a1ac9 | 3458 | binder_unlock(__func__); |
5249f488 | 3459 | return 0; |
355b0502 GKH |
3460 | } |
3461 | ||
5249f488 | 3462 | static int binder_stats_show(struct seq_file *m, void *unused) |
355b0502 GKH |
3463 | { |
3464 | struct binder_proc *proc; | |
3465 | struct hlist_node *pos; | |
355b0502 GKH |
3466 | int do_lock = !binder_debug_no_lock; |
3467 | ||
355b0502 | 3468 | if (do_lock) |
975a1ac9 | 3469 | binder_lock(__func__); |
355b0502 | 3470 | |
5249f488 | 3471 | seq_puts(m, "binder stats:\n"); |
355b0502 | 3472 | |
5249f488 | 3473 | print_binder_stats(m, "", &binder_stats); |
355b0502 | 3474 | |
5249f488 AH |
3475 | hlist_for_each_entry(proc, pos, &binder_procs, proc_node) |
3476 | print_binder_proc_stats(m, proc); | |
355b0502 | 3477 | if (do_lock) |
975a1ac9 | 3478 | binder_unlock(__func__); |
5249f488 | 3479 | return 0; |
355b0502 GKH |
3480 | } |
3481 | ||
5249f488 | 3482 | static int binder_transactions_show(struct seq_file *m, void *unused) |
355b0502 GKH |
3483 | { |
3484 | struct binder_proc *proc; | |
3485 | struct hlist_node *pos; | |
355b0502 GKH |
3486 | int do_lock = !binder_debug_no_lock; |
3487 | ||
355b0502 | 3488 | if (do_lock) |
975a1ac9 | 3489 | binder_lock(__func__); |
355b0502 | 3490 | |
5249f488 AH |
3491 | seq_puts(m, "binder transactions:\n"); |
3492 | hlist_for_each_entry(proc, pos, &binder_procs, proc_node) | |
3493 | print_binder_proc(m, proc, 0); | |
355b0502 | 3494 | if (do_lock) |
975a1ac9 | 3495 | binder_unlock(__func__); |
5249f488 | 3496 | return 0; |
355b0502 GKH |
3497 | } |
3498 | ||
5249f488 | 3499 | static int binder_proc_show(struct seq_file *m, void *unused) |
355b0502 | 3500 | { |
5249f488 | 3501 | struct binder_proc *proc = m->private; |
355b0502 GKH |
3502 | int do_lock = !binder_debug_no_lock; |
3503 | ||
355b0502 | 3504 | if (do_lock) |
975a1ac9 | 3505 | binder_lock(__func__); |
5249f488 AH |
3506 | seq_puts(m, "binder proc state:\n"); |
3507 | print_binder_proc(m, proc, 1); | |
355b0502 | 3508 | if (do_lock) |
975a1ac9 | 3509 | binder_unlock(__func__); |
5249f488 | 3510 | return 0; |
355b0502 GKH |
3511 | } |
3512 | ||
5249f488 | 3513 | static void print_binder_transaction_log_entry(struct seq_file *m, |
355b0502 GKH |
3514 | struct binder_transaction_log_entry *e) |
3515 | { | |
5249f488 AH |
3516 | seq_printf(m, |
3517 | "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n", | |
3518 | e->debug_id, (e->call_type == 2) ? "reply" : | |
3519 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, | |
3520 | e->from_thread, e->to_proc, e->to_thread, e->to_node, | |
3521 | e->target_handle, e->data_size, e->offsets_size); | |
355b0502 GKH |
3522 | } |
3523 | ||
5249f488 | 3524 | static int binder_transaction_log_show(struct seq_file *m, void *unused) |
355b0502 | 3525 | { |
5249f488 | 3526 | struct binder_transaction_log *log = m->private; |
355b0502 | 3527 | int i; |
355b0502 GKH |
3528 | |
3529 | if (log->full) { | |
5249f488 AH |
3530 | for (i = log->next; i < ARRAY_SIZE(log->entry); i++) |
3531 | print_binder_transaction_log_entry(m, &log->entry[i]); | |
355b0502 | 3532 | } |
5249f488 AH |
3533 | for (i = 0; i < log->next; i++) |
3534 | print_binder_transaction_log_entry(m, &log->entry[i]); | |
3535 | return 0; | |
355b0502 GKH |
3536 | } |
3537 | ||
3538 | static const struct file_operations binder_fops = { | |
3539 | .owner = THIS_MODULE, | |
3540 | .poll = binder_poll, | |
3541 | .unlocked_ioctl = binder_ioctl, | |
3542 | .mmap = binder_mmap, | |
3543 | .open = binder_open, | |
3544 | .flush = binder_flush, | |
3545 | .release = binder_release, | |
3546 | }; | |
3547 | ||
3548 | static struct miscdevice binder_miscdev = { | |
3549 | .minor = MISC_DYNAMIC_MINOR, | |
3550 | .name = "binder", | |
3551 | .fops = &binder_fops | |
3552 | }; | |
3553 | ||
5249f488 AH |
3554 | BINDER_DEBUG_ENTRY(state); |
3555 | BINDER_DEBUG_ENTRY(stats); | |
3556 | BINDER_DEBUG_ENTRY(transactions); | |
3557 | BINDER_DEBUG_ENTRY(transaction_log); | |
3558 | ||
355b0502 GKH |
3559 | static int __init binder_init(void) |
3560 | { | |
3561 | int ret; | |
3562 | ||
3c762a49 AH |
3563 | binder_deferred_workqueue = create_singlethread_workqueue("binder"); |
3564 | if (!binder_deferred_workqueue) | |
3565 | return -ENOMEM; | |
3566 | ||
16b66554 AH |
3567 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
3568 | if (binder_debugfs_dir_entry_root) | |
3569 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", | |
3570 | binder_debugfs_dir_entry_root); | |
355b0502 | 3571 | ret = misc_register(&binder_miscdev); |
16b66554 AH |
3572 | if (binder_debugfs_dir_entry_root) { |
3573 | debugfs_create_file("state", | |
3574 | S_IRUGO, | |
3575 | binder_debugfs_dir_entry_root, | |
3576 | NULL, | |
3577 | &binder_state_fops); | |
3578 | debugfs_create_file("stats", | |
3579 | S_IRUGO, | |
3580 | binder_debugfs_dir_entry_root, | |
3581 | NULL, | |
3582 | &binder_stats_fops); | |
3583 | debugfs_create_file("transactions", | |
3584 | S_IRUGO, | |
3585 | binder_debugfs_dir_entry_root, | |
3586 | NULL, | |
3587 | &binder_transactions_fops); | |
3588 | debugfs_create_file("transaction_log", | |
3589 | S_IRUGO, | |
3590 | binder_debugfs_dir_entry_root, | |
3591 | &binder_transaction_log, | |
3592 | &binder_transaction_log_fops); | |
3593 | debugfs_create_file("failed_transaction_log", | |
3594 | S_IRUGO, | |
3595 | binder_debugfs_dir_entry_root, | |
3596 | &binder_transaction_log_failed, | |
3597 | &binder_transaction_log_fops); | |
355b0502 GKH |
3598 | } |
3599 | return ret; | |
3600 | } | |
3601 | ||
3602 | device_initcall(binder_init); | |
3603 | ||
975a1ac9 AH |
3604 | #define CREATE_TRACE_POINTS |
3605 | #include "binder_trace.h" | |
3606 | ||
355b0502 | 3607 | MODULE_LICENSE("GPL v2"); |