forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/android/binder.c
....@@ -1,18 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* binder.c
23 *
34 * Android IPC Subsystem
45 *
56 * 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
- *
167 */
178
189 /*
....@@ -66,20 +57,25 @@
6657 #include <linux/sched/signal.h>
6758 #include <linux/sched/mm.h>
6859 #include <linux/seq_file.h>
60
+#include <linux/string.h>
6961 #include <linux/uaccess.h>
7062 #include <linux/pid_namespace.h>
7163 #include <linux/security.h>
7264 #include <linux/spinlock.h>
7365 #include <linux/ratelimit.h>
66
+#include <linux/syscalls.h>
67
+#include <linux/task_work.h>
68
+#include <linux/sizes.h>
69
+#include <linux/android_vendor.h>
7470
75
-#include <uapi/linux/android/binder.h>
7671 #include <uapi/linux/sched/types.h>
72
+#include <uapi/linux/android/binder.h>
7773
7874 #include <asm/cacheflush.h>
7975
80
-#include "binder_alloc.h"
8176 #include "binder_internal.h"
8277 #include "binder_trace.h"
78
+#include <trace/hooks/binder.h>
8379
8480 static HLIST_HEAD(binder_deferred_list);
8581 static DEFINE_MUTEX(binder_deferred_lock);
....@@ -97,15 +93,6 @@
9793
9894 static int proc_show(struct seq_file *m, void *unused);
9995 DEFINE_SHOW_ATTRIBUTE(proc);
100
-
101
-/* This is only defined in include/asm-arm/sizes.h */
102
-#ifndef SZ_1K
103
-#define SZ_1K 0x400
104
-#endif
105
-
106
-#ifndef SZ_4M
107
-#define SZ_4M 0x400000
108
-#endif
10996
11097 #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
11198
....@@ -174,24 +161,6 @@
174161 #define to_binder_fd_array_object(hdr) \
175162 container_of(hdr, struct binder_fd_array_object, hdr)
176163
177
-enum binder_stat_types {
178
- BINDER_STAT_PROC,
179
- BINDER_STAT_THREAD,
180
- BINDER_STAT_NODE,
181
- BINDER_STAT_REF,
182
- BINDER_STAT_DEATH,
183
- BINDER_STAT_TRANSACTION,
184
- BINDER_STAT_TRANSACTION_COMPLETE,
185
- BINDER_STAT_COUNT
186
-};
187
-
188
-struct binder_stats {
189
- atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
190
- atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
191
- atomic_t obj_created[BINDER_STAT_COUNT];
192
- atomic_t obj_deleted[BINDER_STAT_COUNT];
193
-};
194
-
195164 static struct binder_stats binder_stats;
196165
197166 static inline void binder_stats_deleted(enum binder_stat_types type)
....@@ -204,8 +173,32 @@
204173 atomic_inc(&binder_stats.obj_created[type]);
205174 }
206175
207
-struct binder_transaction_log binder_transaction_log;
208
-struct binder_transaction_log binder_transaction_log_failed;
176
+struct binder_transaction_log_entry {
177
+ int debug_id;
178
+ int debug_id_done;
179
+ int call_type;
180
+ int from_proc;
181
+ int from_thread;
182
+ int target_handle;
183
+ int to_proc;
184
+ int to_thread;
185
+ int to_node;
186
+ int data_size;
187
+ int offsets_size;
188
+ int return_error_line;
189
+ uint32_t return_error;
190
+ uint32_t return_error_param;
191
+ char context_name[BINDERFS_MAX_NAME + 1];
192
+};
193
+
194
+struct binder_transaction_log {
195
+ atomic_t cur;
196
+ bool full;
197
+ struct binder_transaction_log_entry entry[32];
198
+};
199
+
200
+static struct binder_transaction_log binder_transaction_log;
201
+static struct binder_transaction_log binder_transaction_log_failed;
209202
210203 static struct binder_transaction_log_entry *binder_transaction_log_add(
211204 struct binder_transaction_log *log)
....@@ -227,307 +220,9 @@
227220 return e;
228221 }
229222
230
-/**
231
- * struct binder_work - work enqueued on a worklist
232
- * @entry: node enqueued on list
233
- * @type: type of work to be performed
234
- *
235
- * There are separate work lists for proc, thread, and node (async).
236
- */
237
-struct binder_work {
238
- struct list_head entry;
239
-
240
- enum binder_work_type {
241
- BINDER_WORK_TRANSACTION = 1,
242
- BINDER_WORK_TRANSACTION_COMPLETE,
243
- BINDER_WORK_RETURN_ERROR,
244
- BINDER_WORK_NODE,
245
- BINDER_WORK_DEAD_BINDER,
246
- BINDER_WORK_DEAD_BINDER_AND_CLEAR,
247
- BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
248
- } type;
249
-};
250
-
251
-struct binder_error {
252
- struct binder_work work;
253
- uint32_t cmd;
254
-};
255
-
256
-/**
257
- * struct binder_node - binder node bookkeeping
258
- * @debug_id: unique ID for debugging
259
- * (invariant after initialized)
260
- * @lock: lock for node fields
261
- * @work: worklist element for node work
262
- * (protected by @proc->inner_lock)
263
- * @rb_node: element for proc->nodes tree
264
- * (protected by @proc->inner_lock)
265
- * @dead_node: element for binder_dead_nodes list
266
- * (protected by binder_dead_nodes_lock)
267
- * @proc: binder_proc that owns this node
268
- * (invariant after initialized)
269
- * @refs: list of references on this node
270
- * (protected by @lock)
271
- * @internal_strong_refs: used to take strong references when
272
- * initiating a transaction
273
- * (protected by @proc->inner_lock if @proc
274
- * and by @lock)
275
- * @local_weak_refs: weak user refs from local process
276
- * (protected by @proc->inner_lock if @proc
277
- * and by @lock)
278
- * @local_strong_refs: strong user refs from local process
279
- * (protected by @proc->inner_lock if @proc
280
- * and by @lock)
281
- * @tmp_refs: temporary kernel refs
282
- * (protected by @proc->inner_lock while @proc
283
- * is valid, and by binder_dead_nodes_lock
284
- * if @proc is NULL. During inc/dec and node release
285
- * it is also protected by @lock to provide safety
286
- * as the node dies and @proc becomes NULL)
287
- * @ptr: userspace pointer for node
288
- * (invariant, no lock needed)
289
- * @cookie: userspace cookie for node
290
- * (invariant, no lock needed)
291
- * @has_strong_ref: userspace notified of strong ref
292
- * (protected by @proc->inner_lock if @proc
293
- * and by @lock)
294
- * @pending_strong_ref: userspace has acked notification of strong ref
295
- * (protected by @proc->inner_lock if @proc
296
- * and by @lock)
297
- * @has_weak_ref: userspace notified of weak ref
298
- * (protected by @proc->inner_lock if @proc
299
- * and by @lock)
300
- * @pending_weak_ref: userspace has acked notification of weak ref
301
- * (protected by @proc->inner_lock if @proc
302
- * and by @lock)
303
- * @has_async_transaction: async transaction to node in progress
304
- * (protected by @lock)
305
- * @sched_policy: minimum scheduling policy for node
306
- * (invariant after initialized)
307
- * @accept_fds: file descriptor operations supported for node
308
- * (invariant after initialized)
309
- * @min_priority: minimum scheduling priority
310
- * (invariant after initialized)
311
- * @inherit_rt: inherit RT scheduling policy from caller
312
- * @txn_security_ctx: require sender's security context
313
- * (invariant after initialized)
314
- * @async_todo: list of async work items
315
- * (protected by @proc->inner_lock)
316
- *
317
- * Bookkeeping structure for binder nodes.
318
- */
319
-struct binder_node {
320
- int debug_id;
321
- spinlock_t lock;
322
- struct binder_work work;
323
- union {
324
- struct rb_node rb_node;
325
- struct hlist_node dead_node;
326
- };
327
- struct binder_proc *proc;
328
- struct hlist_head refs;
329
- int internal_strong_refs;
330
- int local_weak_refs;
331
- int local_strong_refs;
332
- int tmp_refs;
333
- binder_uintptr_t ptr;
334
- binder_uintptr_t cookie;
335
- struct {
336
- /*
337
- * bitfield elements protected by
338
- * proc inner_lock
339
- */
340
- u8 has_strong_ref:1;
341
- u8 pending_strong_ref:1;
342
- u8 has_weak_ref:1;
343
- u8 pending_weak_ref:1;
344
- };
345
- struct {
346
- /*
347
- * invariant after initialization
348
- */
349
- u8 sched_policy:2;
350
- u8 inherit_rt:1;
351
- u8 accept_fds:1;
352
- u8 txn_security_ctx:1;
353
- u8 min_priority;
354
- };
355
- bool has_async_transaction;
356
- struct list_head async_todo;
357
-};
358
-
359
-struct binder_ref_death {
360
- /**
361
- * @work: worklist element for death notifications
362
- * (protected by inner_lock of the proc that
363
- * this ref belongs to)
364
- */
365
- struct binder_work work;
366
- binder_uintptr_t cookie;
367
-};
368
-
369
-/**
370
- * struct binder_ref_data - binder_ref counts and id
371
- * @debug_id: unique ID for the ref
372
- * @desc: unique userspace handle for ref
373
- * @strong: strong ref count (debugging only if not locked)
374
- * @weak: weak ref count (debugging only if not locked)
375
- *
376
- * Structure to hold ref count and ref id information. Since
377
- * the actual ref can only be accessed with a lock, this structure
378
- * is used to return information about the ref to callers of
379
- * ref inc/dec functions.
380
- */
381
-struct binder_ref_data {
382
- int debug_id;
383
- uint32_t desc;
384
- int strong;
385
- int weak;
386
-};
387
-
388
-/**
389
- * struct binder_ref - struct to track references on nodes
390
- * @data: binder_ref_data containing id, handle, and current refcounts
391
- * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
392
- * @rb_node_node: node for lookup by @node in proc's rb_tree
393
- * @node_entry: list entry for node->refs list in target node
394
- * (protected by @node->lock)
395
- * @proc: binder_proc containing ref
396
- * @node: binder_node of target node. When cleaning up a
397
- * ref for deletion in binder_cleanup_ref, a non-NULL
398
- * @node indicates the node must be freed
399
- * @death: pointer to death notification (ref_death) if requested
400
- * (protected by @node->lock)
401
- *
402
- * Structure to track references from procA to target node (on procB). This
403
- * structure is unsafe to access without holding @proc->outer_lock.
404
- */
405
-struct binder_ref {
406
- /* Lookups needed: */
407
- /* node + proc => ref (transaction) */
408
- /* desc + proc => ref (transaction, inc/dec ref) */
409
- /* node => refs + procs (proc exit) */
410
- struct binder_ref_data data;
411
- struct rb_node rb_node_desc;
412
- struct rb_node rb_node_node;
413
- struct hlist_node node_entry;
414
- struct binder_proc *proc;
415
- struct binder_node *node;
416
- struct binder_ref_death *death;
417
-};
418
-
419223 enum binder_deferred_state {
420
- BINDER_DEFERRED_PUT_FILES = 0x01,
421
- BINDER_DEFERRED_FLUSH = 0x02,
422
- BINDER_DEFERRED_RELEASE = 0x04,
423
-};
424
-
425
-/**
426
- * struct binder_priority - scheduler policy and priority
427
- * @sched_policy scheduler policy
428
- * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
429
- *
430
- * The binder driver supports inheriting the following scheduler policies:
431
- * SCHED_NORMAL
432
- * SCHED_BATCH
433
- * SCHED_FIFO
434
- * SCHED_RR
435
- */
436
-struct binder_priority {
437
- unsigned int sched_policy;
438
- int prio;
439
-};
440
-
441
-/**
442
- * struct binder_proc - binder process bookkeeping
443
- * @proc_node: element for binder_procs list
444
- * @threads: rbtree of binder_threads in this proc
445
- * (protected by @inner_lock)
446
- * @nodes: rbtree of binder nodes associated with
447
- * this proc ordered by node->ptr
448
- * (protected by @inner_lock)
449
- * @refs_by_desc: rbtree of refs ordered by ref->desc
450
- * (protected by @outer_lock)
451
- * @refs_by_node: rbtree of refs ordered by ref->node
452
- * (protected by @outer_lock)
453
- * @waiting_threads: threads currently waiting for proc work
454
- * (protected by @inner_lock)
455
- * @pid PID of group_leader of process
456
- * (invariant after initialized)
457
- * @tsk task_struct for group_leader of process
458
- * (invariant after initialized)
459
- * @files files_struct for process
460
- * (protected by @files_lock)
461
- * @files_lock mutex to protect @files
462
- * @cred struct cred associated with the `struct file`
463
- * in binder_open()
464
- * (invariant after initialized)
465
- * @deferred_work_node: element for binder_deferred_list
466
- * (protected by binder_deferred_lock)
467
- * @deferred_work: bitmap of deferred work to perform
468
- * (protected by binder_deferred_lock)
469
- * @is_dead: process is dead and awaiting free
470
- * when outstanding transactions are cleaned up
471
- * (protected by @inner_lock)
472
- * @todo: list of work for this process
473
- * (protected by @inner_lock)
474
- * @stats: per-process binder statistics
475
- * (atomics, no lock needed)
476
- * @delivered_death: list of delivered death notification
477
- * (protected by @inner_lock)
478
- * @max_threads: cap on number of binder threads
479
- * (protected by @inner_lock)
480
- * @requested_threads: number of binder threads requested but not
481
- * yet started. In current implementation, can
482
- * only be 0 or 1.
483
- * (protected by @inner_lock)
484
- * @requested_threads_started: number binder threads started
485
- * (protected by @inner_lock)
486
- * @tmp_ref: temporary reference to indicate proc is in use
487
- * (protected by @inner_lock)
488
- * @default_priority: default scheduler priority
489
- * (invariant after initialized)
490
- * @debugfs_entry: debugfs node
491
- * @alloc: binder allocator bookkeeping
492
- * @context: binder_context for this proc
493
- * (invariant after initialized)
494
- * @inner_lock: can nest under outer_lock and/or node lock
495
- * @outer_lock: no nesting under innor or node lock
496
- * Lock order: 1) outer, 2) node, 3) inner
497
- * @binderfs_entry: process-specific binderfs log file
498
- *
499
- * Bookkeeping structure for binder processes
500
- */
501
-struct binder_proc {
502
- struct hlist_node proc_node;
503
- struct rb_root threads;
504
- struct rb_root nodes;
505
- struct rb_root refs_by_desc;
506
- struct rb_root refs_by_node;
507
- struct list_head waiting_threads;
508
- int pid;
509
- struct task_struct *tsk;
510
- struct files_struct *files;
511
- struct mutex files_lock;
512
- const struct cred *cred;
513
- struct hlist_node deferred_work_node;
514
- int deferred_work;
515
- bool is_dead;
516
-
517
- struct list_head todo;
518
- struct binder_stats stats;
519
- struct list_head delivered_death;
520
- int max_threads;
521
- int requested_threads;
522
- int requested_threads_started;
523
- int tmp_ref;
524
- struct binder_priority default_priority;
525
- struct dentry *debugfs_entry;
526
- struct binder_alloc alloc;
527
- struct binder_context *context;
528
- spinlock_t inner_lock;
529
- spinlock_t outer_lock;
530
- struct dentry *binderfs_entry;
224
+ BINDER_DEFERRED_FLUSH = 0x01,
225
+ BINDER_DEFERRED_RELEASE = 0x02,
531226 };
532227
533228 enum {
....@@ -540,110 +235,6 @@
540235 };
541236
542237 /**
543
- * struct binder_thread - binder thread bookkeeping
544
- * @proc: binder process for this thread
545
- * (invariant after initialization)
546
- * @rb_node: element for proc->threads rbtree
547
- * (protected by @proc->inner_lock)
548
- * @waiting_thread_node: element for @proc->waiting_threads list
549
- * (protected by @proc->inner_lock)
550
- * @pid: PID for this thread
551
- * (invariant after initialization)
552
- * @looper: bitmap of looping state
553
- * (only accessed by this thread)
554
- * @looper_needs_return: looping thread needs to exit driver
555
- * (no lock needed)
556
- * @transaction_stack: stack of in-progress transactions for this thread
557
- * (protected by @proc->inner_lock)
558
- * @todo: list of work to do for this thread
559
- * (protected by @proc->inner_lock)
560
- * @process_todo: whether work in @todo should be processed
561
- * (protected by @proc->inner_lock)
562
- * @return_error: transaction errors reported by this thread
563
- * (only accessed by this thread)
564
- * @reply_error: transaction errors reported by target thread
565
- * (protected by @proc->inner_lock)
566
- * @wait: wait queue for thread work
567
- * @stats: per-thread statistics
568
- * (atomics, no lock needed)
569
- * @tmp_ref: temporary reference to indicate thread is in use
570
- * (atomic since @proc->inner_lock cannot
571
- * always be acquired)
572
- * @is_dead: thread is dead and awaiting free
573
- * when outstanding transactions are cleaned up
574
- * (protected by @proc->inner_lock)
575
- * @task: struct task_struct for this thread
576
- *
577
- * Bookkeeping structure for binder threads.
578
- */
579
-struct binder_thread {
580
- struct binder_proc *proc;
581
- struct rb_node rb_node;
582
- struct list_head waiting_thread_node;
583
- int pid;
584
- int looper; /* only modified by this thread */
585
- bool looper_need_return; /* can be written by other thread */
586
- struct binder_transaction *transaction_stack;
587
- struct list_head todo;
588
- bool process_todo;
589
- struct binder_error return_error;
590
- struct binder_error reply_error;
591
- wait_queue_head_t wait;
592
- struct binder_stats stats;
593
- atomic_t tmp_ref;
594
- bool is_dead;
595
- struct task_struct *task;
596
-};
597
-
598
-struct binder_transaction {
599
- int debug_id;
600
- struct binder_work work;
601
- struct binder_thread *from;
602
- struct binder_transaction *from_parent;
603
- struct binder_proc *to_proc;
604
- struct binder_thread *to_thread;
605
- struct binder_transaction *to_parent;
606
- unsigned need_reply:1;
607
- /* unsigned is_dead:1; */ /* not used at the moment */
608
-
609
- struct binder_buffer *buffer;
610
- unsigned int code;
611
- unsigned int flags;
612
- struct binder_priority priority;
613
- struct binder_priority saved_priority;
614
- bool set_priority_called;
615
- kuid_t sender_euid;
616
- binder_uintptr_t security_ctx;
617
- /**
618
- * @lock: protects @from, @to_proc, and @to_thread
619
- *
620
- * @from, @to_proc, and @to_thread can be set to NULL
621
- * during thread teardown
622
- */
623
- spinlock_t lock;
624
-};
625
-
626
-/**
627
- * struct binder_object - union of flat binder object types
628
- * @hdr: generic object header
629
- * @fbo: binder object (nodes and refs)
630
- * @fdo: file descriptor object
631
- * @bbo: binder buffer pointer
632
- * @fdao: file descriptor array
633
- *
634
- * Used for type-independent object copies
635
- */
636
-struct binder_object {
637
- union {
638
- struct binder_object_header hdr;
639
- struct flat_binder_object fbo;
640
- struct binder_fd_object fdo;
641
- struct binder_buffer_object bbo;
642
- struct binder_fd_array_object fdao;
643
- };
644
-};
645
-
646
-/**
647238 * binder_proc_lock() - Acquire outer lock for given binder_proc
648239 * @proc: struct binder_proc to acquire
649240 *
....@@ -653,6 +244,7 @@
653244 #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
654245 static void
655246 _binder_proc_lock(struct binder_proc *proc, int line)
247
+ __acquires(&proc->outer_lock)
656248 {
657249 binder_debug(BINDER_DEBUG_SPINLOCKS,
658250 "%s: line=%d\n", __func__, line);
....@@ -668,6 +260,7 @@
668260 #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
669261 static void
670262 _binder_proc_unlock(struct binder_proc *proc, int line)
263
+ __releases(&proc->outer_lock)
671264 {
672265 binder_debug(BINDER_DEBUG_SPINLOCKS,
673266 "%s: line=%d\n", __func__, line);
....@@ -683,6 +276,7 @@
683276 #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
684277 static void
685278 _binder_inner_proc_lock(struct binder_proc *proc, int line)
279
+ __acquires(&proc->inner_lock)
686280 {
687281 binder_debug(BINDER_DEBUG_SPINLOCKS,
688282 "%s: line=%d\n", __func__, line);
....@@ -698,6 +292,7 @@
698292 #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
699293 static void
700294 _binder_inner_proc_unlock(struct binder_proc *proc, int line)
295
+ __releases(&proc->inner_lock)
701296 {
702297 binder_debug(BINDER_DEBUG_SPINLOCKS,
703298 "%s: line=%d\n", __func__, line);
....@@ -713,6 +308,7 @@
713308 #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
714309 static void
715310 _binder_node_lock(struct binder_node *node, int line)
311
+ __acquires(&node->lock)
716312 {
717313 binder_debug(BINDER_DEBUG_SPINLOCKS,
718314 "%s: line=%d\n", __func__, line);
....@@ -728,6 +324,7 @@
728324 #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
729325 static void
730326 _binder_node_unlock(struct binder_node *node, int line)
327
+ __releases(&node->lock)
731328 {
732329 binder_debug(BINDER_DEBUG_SPINLOCKS,
733330 "%s: line=%d\n", __func__, line);
....@@ -744,12 +341,16 @@
744341 #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
745342 static void
746343 _binder_node_inner_lock(struct binder_node *node, int line)
344
+ __acquires(&node->lock) __acquires(&node->proc->inner_lock)
747345 {
748346 binder_debug(BINDER_DEBUG_SPINLOCKS,
749347 "%s: line=%d\n", __func__, line);
750348 spin_lock(&node->lock);
751349 if (node->proc)
752350 binder_inner_proc_lock(node->proc);
351
+ else
352
+ /* annotation for sparse */
353
+ __acquire(&node->proc->inner_lock);
753354 }
754355
755356 /**
....@@ -761,6 +362,7 @@
761362 #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
762363 static void
763364 _binder_node_inner_unlock(struct binder_node *node, int line)
365
+ __releases(&node->lock) __releases(&node->proc->inner_lock)
764366 {
765367 struct binder_proc *proc = node->proc;
766368
....@@ -768,6 +370,9 @@
768370 "%s: line=%d\n", __func__, line);
769371 if (proc)
770372 binder_inner_proc_unlock(proc);
373
+ else
374
+ /* annotation for sparse */
375
+ __release(&node->proc->inner_lock);
771376 spin_unlock(&node->lock);
772377 }
773378
....@@ -907,69 +512,14 @@
907512 static void binder_free_proc(struct binder_proc *proc);
908513 static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
909514
910
-static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
911
-{
912
- unsigned long rlim_cur;
913
- unsigned long irqs;
914
- int ret;
915
-
916
- mutex_lock(&proc->files_lock);
917
- if (proc->files == NULL) {
918
- ret = -ESRCH;
919
- goto err;
920
- }
921
- if (!lock_task_sighand(proc->tsk, &irqs)) {
922
- ret = -EMFILE;
923
- goto err;
924
- }
925
- rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
926
- unlock_task_sighand(proc->tsk, &irqs);
927
-
928
- ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
929
-err:
930
- mutex_unlock(&proc->files_lock);
931
- return ret;
932
-}
933
-
934
-/*
935
- * copied from fd_install
936
- */
937
-static void task_fd_install(
938
- struct binder_proc *proc, unsigned int fd, struct file *file)
939
-{
940
- mutex_lock(&proc->files_lock);
941
- if (proc->files)
942
- __fd_install(proc->files, fd, file);
943
- mutex_unlock(&proc->files_lock);
944
-}
945
-
946
-/*
947
- * copied from sys_close
948
- */
949
-static long task_close_fd(struct binder_proc *proc, unsigned int fd)
950
-{
951
- int retval;
952
-
953
- mutex_lock(&proc->files_lock);
954
- if (proc->files == NULL) {
955
- retval = -ESRCH;
956
- goto err;
957
- }
958
- retval = __close_fd(proc->files, fd);
959
- /* can't restart close syscall because file table entry was cleared */
960
- if (unlikely(retval == -ERESTARTSYS ||
961
- retval == -ERESTARTNOINTR ||
962
- retval == -ERESTARTNOHAND ||
963
- retval == -ERESTART_RESTARTBLOCK))
964
- retval = -EINTR;
965
-err:
966
- mutex_unlock(&proc->files_lock);
967
- return retval;
968
-}
969
-
970515 static bool binder_has_work_ilocked(struct binder_thread *thread,
971516 bool do_proc_work)
972517 {
518
+ int ret = 0;
519
+
520
+ trace_android_vh_binder_has_work_ilocked(thread, do_proc_work, &ret);
521
+ if (ret)
522
+ return true;
973523 return thread->process_todo ||
974524 thread->looper_need_return ||
975525 (do_proc_work &&
....@@ -1005,6 +555,7 @@
1005555 thread = rb_entry(n, struct binder_thread, rb_node);
1006556 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1007557 binder_available_for_proc_work_ilocked(thread)) {
558
+ trace_android_vh_binder_wakeup_ilocked(thread->task, sync, proc);
1008559 if (sync)
1009560 wake_up_interruptible_sync(&thread->wait);
1010561 else
....@@ -1064,6 +615,7 @@
1064615 assert_spin_locked(&proc->inner_lock);
1065616
1066617 if (thread) {
618
+ trace_android_vh_binder_wakeup_ilocked(thread->task, sync, proc);
1067619 if (sync)
1068620 wake_up_interruptible_sync(&thread->wait);
1069621 else
....@@ -1206,6 +758,7 @@
1206758 bool inherit_rt)
1207759 {
1208760 struct binder_priority desired_prio = t->priority;
761
+ bool skip = false;
1209762
1210763 if (t->set_priority_called)
1211764 return;
....@@ -1213,6 +766,10 @@
1213766 t->set_priority_called = true;
1214767 t->saved_priority.sched_policy = task->policy;
1215768 t->saved_priority.prio = task->normal_prio;
769
+
770
+ trace_android_vh_binder_priority_skip(task, &skip);
771
+ if (skip)
772
+ return;
1216773
1217774 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1218775 desired_prio.prio = NICE_TO_PRIO(0);
....@@ -1233,6 +790,7 @@
1233790 }
1234791
1235792 binder_set_priority(task, desired_prio);
793
+ trace_android_vh_binder_set_priority(t, task);
1236794 }
1237795
1238796 static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
....@@ -1545,10 +1103,14 @@
15451103 binder_node_inner_lock(node);
15461104 if (!node->proc)
15471105 spin_lock(&binder_dead_nodes_lock);
1106
+ else
1107
+ __acquire(&binder_dead_nodes_lock);
15481108 node->tmp_refs--;
15491109 BUG_ON(node->tmp_refs < 0);
15501110 if (!node->proc)
15511111 spin_unlock(&binder_dead_nodes_lock);
1112
+ else
1113
+ __release(&binder_dead_nodes_lock);
15521114 /*
15531115 * Call binder_dec_node() to check if all refcounts are 0
15541116 * and cleanup is needed. Calling with strong=0 and internal=1
....@@ -1669,6 +1231,7 @@
16691231 "%d new ref %d desc %d for node %d\n",
16701232 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
16711233 node->debug_id);
1234
+ trace_android_vh_binder_new_ref(proc->tsk, new_ref->data.desc, new_ref->node->debug_id);
16721235 binder_node_unlock(node);
16731236 return new_ref;
16741237 }
....@@ -1836,6 +1399,7 @@
18361399 */
18371400 static void binder_free_ref(struct binder_ref *ref)
18381401 {
1402
+ trace_android_vh_binder_del_ref(ref->proc ? ref->proc->tsk : 0, ref->data.desc);
18391403 if (ref->node)
18401404 binder_free_node(ref->node);
18411405 kfree(ref->death);
....@@ -1940,6 +1504,18 @@
19401504 }
19411505 ret = binder_inc_ref_olocked(ref, strong, target_list);
19421506 *rdata = ref->data;
1507
+ if (ret && ref == new_ref) {
1508
+ /*
1509
+ * Cleanup the failed reference here as the target
1510
+ * could now be dead and have already released its
1511
+ * references by now. Calling on the new reference
1512
+ * with strong=0 and a tmp_refs will not decrement
1513
+ * the node. The new_ref gets kfree'd below.
1514
+ */
1515
+ binder_cleanup_ref_olocked(new_ref);
1516
+ ref = NULL;
1517
+ }
1518
+
19431519 binder_proc_unlock(proc);
19441520 if (new_ref && ref != new_ref)
19451521 /*
....@@ -2051,20 +1627,45 @@
20511627 */
20521628 static struct binder_thread *binder_get_txn_from_and_acq_inner(
20531629 struct binder_transaction *t)
1630
+ __acquires(&t->from->proc->inner_lock)
20541631 {
20551632 struct binder_thread *from;
20561633
20571634 from = binder_get_txn_from(t);
2058
- if (!from)
1635
+ if (!from) {
1636
+ __acquire(&from->proc->inner_lock);
20591637 return NULL;
1638
+ }
20601639 binder_inner_proc_lock(from->proc);
20611640 if (t->from) {
20621641 BUG_ON(from != t->from);
20631642 return from;
20641643 }
20651644 binder_inner_proc_unlock(from->proc);
1645
+ __acquire(&from->proc->inner_lock);
20661646 binder_thread_dec_tmpref(from);
20671647 return NULL;
1648
+}
1649
+
1650
+/**
1651
+ * binder_free_txn_fixups() - free unprocessed fd fixups
1652
+ * @t: binder transaction for t->from
1653
+ *
1654
+ * If the transaction is being torn down prior to being
1655
+ * processed by the target process, free all of the
1656
+ * fd fixups and fput the file structs. It is safe to
1657
+ * call this function after the fixups have been
1658
+ * processed -- in that case, the list will be empty.
1659
+ */
1660
+static void binder_free_txn_fixups(struct binder_transaction *t)
1661
+{
1662
+ struct binder_txn_fd_fixup *fixup, *tmp;
1663
+
1664
+ list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
1665
+ fput(fixup->file);
1666
+ list_del(&fixup->fixup_entry);
1667
+ kfree(fixup);
1668
+ }
20681669 }
20691670
20701671 static void binder_free_transaction(struct binder_transaction *t)
....@@ -2073,6 +1674,12 @@
20731674
20741675 if (target_proc) {
20751676 binder_inner_proc_lock(target_proc);
1677
+ target_proc->outstanding_txns--;
1678
+ if (target_proc->outstanding_txns < 0)
1679
+ pr_warn("%s: Unexpected outstanding_txns %d\n",
1680
+ __func__, target_proc->outstanding_txns);
1681
+ if (!target_proc->outstanding_txns && target_proc->is_frozen)
1682
+ wake_up_interruptible_all(&target_proc->freeze_wait);
20761683 if (t->buffer)
20771684 t->buffer->transaction = NULL;
20781685 binder_inner_proc_unlock(target_proc);
....@@ -2081,6 +1688,7 @@
20811688 * If the transaction has no target_proc, then
20821689 * t->buffer->transaction has already been cleared.
20831690 */
1691
+ binder_free_txn_fixups(t);
20841692 kfree(t);
20851693 binder_stats_deleted(BINDER_STAT_TRANSACTION);
20861694 }
....@@ -2123,6 +1731,7 @@
21231731 binder_free_transaction(t);
21241732 return;
21251733 }
1734
+ __release(&target_thread->proc->inner_lock);
21261735 next = t->from_parent;
21271736
21281737 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
....@@ -2165,15 +1774,21 @@
21651774 /**
21661775 * binder_get_object() - gets object and checks for valid metadata
21671776 * @proc: binder_proc owning the buffer
1777
+ * @u: sender's user pointer to base of buffer
21681778 * @buffer: binder_buffer that we're parsing.
21691779 * @offset: offset in the @buffer at which to validate an object.
21701780 * @object: struct binder_object to read into
21711781 *
2172
- * Return: If there's a valid metadata object at @offset in @buffer, the
1782
+ * Copy the binder object at the given offset into @object. If @u is
1783
+ * provided then the copy is from the sender's buffer. If not, then
1784
+ * it is copied from the target's @buffer.
1785
+ *
1786
+ * Return: If there's a valid metadata object at @offset, the
21731787 * size of that object. Otherwise, it returns zero. The object
21741788 * is read into the struct binder_object pointed to by @object.
21751789 */
21761790 static size_t binder_get_object(struct binder_proc *proc,
1791
+ const void __user *u,
21771792 struct binder_buffer *buffer,
21781793 unsigned long offset,
21791794 struct binder_object *object)
....@@ -2183,11 +1798,16 @@
21831798 size_t object_size = 0;
21841799
21851800 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
2186
- if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
2187
- !IS_ALIGNED(offset, sizeof(u32)))
1801
+ if (offset > buffer->data_size || read_size < sizeof(*hdr))
21881802 return 0;
2189
- binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2190
- offset, read_size);
1803
+ if (u) {
1804
+ if (copy_from_user(object, u + offset, read_size))
1805
+ return 0;
1806
+ } else {
1807
+ if (binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
1808
+ offset, read_size))
1809
+ return 0;
1810
+ }
21911811
21921812 /* Ok, now see if we read a complete object. */
21931813 hdr = &object->hdr;
....@@ -2256,9 +1876,11 @@
22561876 return NULL;
22571877
22581878 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2259
- binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2260
- b, buffer_offset, sizeof(object_offset));
2261
- object_size = binder_get_object(proc, b, object_offset, object);
1879
+ if (binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
1880
+ b, buffer_offset,
1881
+ sizeof(object_offset)))
1882
+ return NULL;
1883
+ object_size = binder_get_object(proc, NULL, b, object_offset, object);
22621884 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
22631885 return NULL;
22641886 if (object_offsetp)
....@@ -2323,7 +1945,8 @@
23231945 unsigned long buffer_offset;
23241946 struct binder_object last_object;
23251947 struct binder_buffer_object *last_bbo;
2326
- size_t object_size = binder_get_object(proc, b, last_obj_offset,
1948
+ size_t object_size = binder_get_object(proc, NULL, b,
1949
+ last_obj_offset,
23271950 &last_object);
23281951 if (object_size != sizeof(*last_bbo))
23291952 return false;
....@@ -2337,15 +1960,78 @@
23371960 return false;
23381961 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
23391962 buffer_offset = objects_start_offset +
2340
- sizeof(binder_size_t) * last_bbo->parent,
2341
- binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2342
- b, buffer_offset,
2343
- sizeof(last_obj_offset));
1963
+ sizeof(binder_size_t) * last_bbo->parent;
1964
+ if (binder_alloc_copy_from_buffer(&proc->alloc,
1965
+ &last_obj_offset,
1966
+ b, buffer_offset,
1967
+ sizeof(last_obj_offset)))
1968
+ return false;
23441969 }
23451970 return (fixup_offset >= last_min_offset);
23461971 }
23471972
1973
+/**
1974
+ * struct binder_task_work_cb - for deferred close
1975
+ *
1976
+ * @twork: callback_head for task work
1977
+ * @fd: fd to close
1978
+ *
1979
+ * Structure to pass task work to be handled after
1980
+ * returning from binder_ioctl() via task_work_add().
1981
+ */
1982
+struct binder_task_work_cb {
1983
+ struct callback_head twork;
1984
+ struct file *file;
1985
+};
1986
+
1987
+/**
1988
+ * binder_do_fd_close() - close list of file descriptors
1989
+ * @twork: callback head for task work
1990
+ *
1991
+ * It is not safe to call ksys_close() during the binder_ioctl()
1992
+ * function if there is a chance that binder's own file descriptor
1993
+ * might be closed. This is to meet the requirements for using
1994
+ * fdget() (see comments for __fget_light()). Therefore use
1995
+ * task_work_add() to schedule the close operation once we have
1996
+ * returned from binder_ioctl(). This function is a callback
1997
+ * for that mechanism and does the actual ksys_close() on the
1998
+ * given file descriptor.
1999
+ */
2000
+static void binder_do_fd_close(struct callback_head *twork)
2001
+{
2002
+ struct binder_task_work_cb *twcb = container_of(twork,
2003
+ struct binder_task_work_cb, twork);
2004
+
2005
+ fput(twcb->file);
2006
+ kfree(twcb);
2007
+}
2008
+
2009
+/**
2010
+ * binder_deferred_fd_close() - schedule a close for the given file-descriptor
2011
+ * @fd: file-descriptor to close
2012
+ *
2013
+ * See comments in binder_do_fd_close(). This function is used to schedule
2014
+ * a file-descriptor to be closed after returning from binder_ioctl().
2015
+ */
2016
+static void binder_deferred_fd_close(int fd)
2017
+{
2018
+ struct binder_task_work_cb *twcb;
2019
+
2020
+ twcb = kzalloc(sizeof(*twcb), GFP_KERNEL);
2021
+ if (!twcb)
2022
+ return;
2023
+ init_task_work(&twcb->twork, binder_do_fd_close);
2024
+ close_fd_get_file(fd, &twcb->file);
2025
+ if (twcb->file) {
2026
+ filp_close(twcb->file, current->files);
2027
+ task_work_add(current, &twcb->twork, TWA_RESUME);
2028
+ } else {
2029
+ kfree(twcb);
2030
+ }
2031
+}
2032
+
23482033 static void binder_transaction_buffer_release(struct binder_proc *proc,
2034
+ struct binder_thread *thread,
23492035 struct binder_buffer *buffer,
23502036 binder_size_t failed_at,
23512037 bool is_failure)
....@@ -2363,20 +2049,20 @@
23632049 binder_dec_node(buffer->target_node, 1, 0);
23642050
23652051 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
2366
- off_end_offset = is_failure ? failed_at :
2052
+ off_end_offset = is_failure && failed_at ? failed_at :
23672053 off_start_offset + buffer->offsets_size;
23682054 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
23692055 buffer_offset += sizeof(binder_size_t)) {
23702056 struct binder_object_header *hdr;
2371
- size_t object_size;
2057
+ size_t object_size = 0;
23722058 struct binder_object object;
23732059 binder_size_t object_offset;
23742060
2375
- binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2376
- buffer, buffer_offset,
2377
- sizeof(object_offset));
2378
- object_size = binder_get_object(proc, buffer,
2379
- object_offset, &object);
2061
+ if (!binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2062
+ buffer, buffer_offset,
2063
+ sizeof(object_offset)))
2064
+ object_size = binder_get_object(proc, NULL, buffer,
2065
+ object_offset, &object);
23802066 if (object_size == 0) {
23812067 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
23822068 debug_id, (u64)object_offset, buffer->data_size);
....@@ -2424,12 +2110,15 @@
24242110 } break;
24252111
24262112 case BINDER_TYPE_FD: {
2427
- struct binder_fd_object *fp = to_binder_fd_object(hdr);
2428
-
2429
- binder_debug(BINDER_DEBUG_TRANSACTION,
2430
- " fd %d\n", fp->fd);
2431
- if (failed_at)
2432
- task_close_fd(proc, fp->fd);
2113
+ /*
2114
+ * No need to close the file here since user-space
2115
+ * closes it for for successfully delivered
2116
+ * transactions. For transactions that weren't
2117
+ * delivered, the new fd was never allocated so
2118
+ * there is no need to close and the fput on the
2119
+ * file is done when the transaction is torn
2120
+ * down.
2121
+ */
24332122 } break;
24342123 case BINDER_TYPE_PTR:
24352124 /*
....@@ -2445,6 +2134,14 @@
24452134 size_t fd_index;
24462135 binder_size_t fd_buf_size;
24472136 binder_size_t num_valid;
2137
+
2138
+ if (is_failure) {
2139
+ /*
2140
+ * The fd fixups have not been applied so no
2141
+ * fds need to be closed.
2142
+ */
2143
+ continue;
2144
+ }
24482145
24492146 num_valid = (buffer_offset - off_start_offset) /
24502147 sizeof(binder_size_t);
....@@ -2485,15 +2182,24 @@
24852182 for (fd_index = 0; fd_index < fda->num_fds;
24862183 fd_index++) {
24872184 u32 fd;
2185
+ int err;
24882186 binder_size_t offset = fda_offset +
24892187 fd_index * sizeof(fd);
24902188
2491
- binder_alloc_copy_from_buffer(&proc->alloc,
2492
- &fd,
2493
- buffer,
2494
- offset,
2495
- sizeof(fd));
2496
- task_close_fd(proc, fd);
2189
+ err = binder_alloc_copy_from_buffer(
2190
+ &proc->alloc, &fd, buffer,
2191
+ offset, sizeof(fd));
2192
+ WARN_ON(err);
2193
+ if (!err) {
2194
+ binder_deferred_fd_close(fd);
2195
+ /*
2196
+ * Need to make sure the thread goes
2197
+ * back to userspace to complete the
2198
+ * deferred close
2199
+ */
2200
+ if (thread)
2201
+ thread->looper_need_return = true;
2202
+ }
24972203 }
24982204 } break;
24992205 default:
....@@ -2528,7 +2234,8 @@
25282234 ret = -EINVAL;
25292235 goto done;
25302236 }
2531
- if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2237
+ if (security_binder_transfer_binder(binder_get_cred(proc),
2238
+ binder_get_cred(target_proc))) {
25322239 ret = -EPERM;
25332240 goto done;
25342241 }
....@@ -2574,7 +2281,8 @@
25742281 proc->pid, thread->pid, fp->handle);
25752282 return -EINVAL;
25762283 }
2577
- if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2284
+ if (security_binder_transfer_binder(binder_get_cred(proc),
2285
+ binder_get_cred(target_proc))) {
25782286 ret = -EPERM;
25792287 goto done;
25802288 }
....@@ -2589,11 +2297,15 @@
25892297 fp->cookie = node->cookie;
25902298 if (node->proc)
25912299 binder_inner_proc_lock(node->proc);
2300
+ else
2301
+ __acquire(&node->proc->inner_lock);
25922302 binder_inc_node_nilocked(node,
25932303 fp->hdr.type == BINDER_TYPE_BINDER,
25942304 0, NULL);
25952305 if (node->proc)
25962306 binder_inner_proc_unlock(node->proc);
2307
+ else
2308
+ __release(&node->proc->inner_lock);
25972309 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
25982310 binder_debug(BINDER_DEBUG_TRANSACTION,
25992311 " ref %d desc %d -> node %d u%016llx\n",
....@@ -2626,16 +2338,16 @@
26262338 return ret;
26272339 }
26282340
2629
-static int binder_translate_fd(int fd,
2341
+static int binder_translate_fd(u32 fd, binder_size_t fd_offset,
26302342 struct binder_transaction *t,
26312343 struct binder_thread *thread,
26322344 struct binder_transaction *in_reply_to)
26332345 {
26342346 struct binder_proc *proc = thread->proc;
26352347 struct binder_proc *target_proc = t->to_proc;
2636
- int target_fd;
2348
+ struct binder_txn_fd_fixup *fixup;
26372349 struct file *file;
2638
- int ret;
2350
+ int ret = 0;
26392351 bool target_allows_fd;
26402352
26412353 if (in_reply_to)
....@@ -2658,25 +2370,31 @@
26582370 ret = -EBADF;
26592371 goto err_fget;
26602372 }
2661
- ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
2373
+ ret = security_binder_transfer_file(binder_get_cred(proc),
2374
+ binder_get_cred(target_proc), file);
26622375 if (ret < 0) {
26632376 ret = -EPERM;
26642377 goto err_security;
26652378 }
26662379
2667
- target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2668
- if (target_fd < 0) {
2380
+ /*
2381
+ * Add fixup record for this transaction. The allocation
2382
+ * of the fd in the target needs to be done from a
2383
+ * target thread.
2384
+ */
2385
+ fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
2386
+ if (!fixup) {
26692387 ret = -ENOMEM;
2670
- goto err_get_unused_fd;
2388
+ goto err_alloc;
26712389 }
2672
- task_fd_install(target_proc, target_fd, file);
2673
- trace_binder_transaction_fd(t, fd, target_fd);
2674
- binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2675
- fd, target_fd);
2390
+ fixup->file = file;
2391
+ fixup->offset = fd_offset;
2392
+ trace_binder_transaction_fd_send(t, fd, fixup->offset);
2393
+ list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
26762394
2677
- return target_fd;
2395
+ return ret;
26782396
2679
-err_get_unused_fd:
2397
+err_alloc:
26802398 err_security:
26812399 fput(file);
26822400 err_fget:
....@@ -2684,17 +2402,266 @@
26842402 return ret;
26852403 }
26862404
2687
-static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2405
+/**
2406
+ * struct binder_ptr_fixup - data to be fixed-up in target buffer
2407
+ * @offset offset in target buffer to fixup
2408
+ * @skip_size bytes to skip in copy (fixup will be written later)
2409
+ * @fixup_data data to write at fixup offset
2410
+ * @node list node
2411
+ *
2412
+ * This is used for the pointer fixup list (pf) which is created and consumed
2413
+ * during binder_transaction() and is only accessed locally. No
2414
+ * locking is necessary.
2415
+ *
2416
+ * The list is ordered by @offset.
2417
+ */
2418
+struct binder_ptr_fixup {
2419
+ binder_size_t offset;
2420
+ size_t skip_size;
2421
+ binder_uintptr_t fixup_data;
2422
+ struct list_head node;
2423
+};
2424
+
2425
+/**
2426
+ * struct binder_sg_copy - scatter-gather data to be copied
2427
+ * @offset offset in target buffer
2428
+ * @sender_uaddr user address in source buffer
2429
+ * @length bytes to copy
2430
+ * @node list node
2431
+ *
2432
+ * This is used for the sg copy list (sgc) which is created and consumed
2433
+ * during binder_transaction() and is only accessed locally. No
2434
+ * locking is necessary.
2435
+ *
2436
+ * The list is ordered by @offset.
2437
+ */
2438
+struct binder_sg_copy {
2439
+ binder_size_t offset;
2440
+ const void __user *sender_uaddr;
2441
+ size_t length;
2442
+ struct list_head node;
2443
+};
2444
+
2445
+/**
2446
+ * binder_do_deferred_txn_copies() - copy and fixup scatter-gather data
2447
+ * @alloc: binder_alloc associated with @buffer
2448
+ * @buffer: binder buffer in target process
2449
+ * @sgc_head: list_head of scatter-gather copy list
2450
+ * @pf_head: list_head of pointer fixup list
2451
+ *
2452
+ * Processes all elements of @sgc_head, applying fixups from @pf_head
2453
+ * and copying the scatter-gather data from the source process' user
2454
+ * buffer to the target's buffer. It is expected that the list creation
2455
+ * and processing all occurs during binder_transaction() so these lists
2456
+ * are only accessed in local context.
2457
+ *
2458
+ * Return: 0=success, else -errno
2459
+ */
2460
+static int binder_do_deferred_txn_copies(struct binder_alloc *alloc,
2461
+ struct binder_buffer *buffer,
2462
+ struct list_head *sgc_head,
2463
+ struct list_head *pf_head)
2464
+{
2465
+ int ret = 0;
2466
+ struct binder_sg_copy *sgc, *tmpsgc;
2467
+ struct binder_ptr_fixup *tmppf;
2468
+ struct binder_ptr_fixup *pf =
2469
+ list_first_entry_or_null(pf_head, struct binder_ptr_fixup,
2470
+ node);
2471
+
2472
+ list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
2473
+ size_t bytes_copied = 0;
2474
+
2475
+ while (bytes_copied < sgc->length) {
2476
+ size_t copy_size;
2477
+ size_t bytes_left = sgc->length - bytes_copied;
2478
+ size_t offset = sgc->offset + bytes_copied;
2479
+
2480
+ /*
2481
+ * We copy up to the fixup (pointed to by pf)
2482
+ */
2483
+ copy_size = pf ? min(bytes_left, (size_t)pf->offset - offset)
2484
+ : bytes_left;
2485
+ if (!ret && copy_size)
2486
+ ret = binder_alloc_copy_user_to_buffer(
2487
+ alloc, buffer,
2488
+ offset,
2489
+ sgc->sender_uaddr + bytes_copied,
2490
+ copy_size);
2491
+ bytes_copied += copy_size;
2492
+ if (copy_size != bytes_left) {
2493
+ BUG_ON(!pf);
2494
+ /* we stopped at a fixup offset */
2495
+ if (pf->skip_size) {
2496
+ /*
2497
+ * we are just skipping. This is for
2498
+ * BINDER_TYPE_FDA where the translated
2499
+ * fds will be fixed up when we get
2500
+ * to target context.
2501
+ */
2502
+ bytes_copied += pf->skip_size;
2503
+ } else {
2504
+ /* apply the fixup indicated by pf */
2505
+ if (!ret)
2506
+ ret = binder_alloc_copy_to_buffer(
2507
+ alloc, buffer,
2508
+ pf->offset,
2509
+ &pf->fixup_data,
2510
+ sizeof(pf->fixup_data));
2511
+ bytes_copied += sizeof(pf->fixup_data);
2512
+ }
2513
+ list_del(&pf->node);
2514
+ kfree(pf);
2515
+ pf = list_first_entry_or_null(pf_head,
2516
+ struct binder_ptr_fixup, node);
2517
+ }
2518
+ }
2519
+ list_del(&sgc->node);
2520
+ kfree(sgc);
2521
+ }
2522
+ list_for_each_entry_safe(pf, tmppf, pf_head, node) {
2523
+ BUG_ON(pf->skip_size == 0);
2524
+ list_del(&pf->node);
2525
+ kfree(pf);
2526
+ }
2527
+ BUG_ON(!list_empty(sgc_head));
2528
+
2529
+ return ret > 0 ? -EINVAL : ret;
2530
+}
2531
+
2532
+/**
2533
+ * binder_cleanup_deferred_txn_lists() - free specified lists
2534
+ * @sgc_head: list_head of scatter-gather copy list
2535
+ * @pf_head: list_head of pointer fixup list
2536
+ *
2537
+ * Called to clean up @sgc_head and @pf_head if there is an
2538
+ * error.
2539
+ */
2540
+static void binder_cleanup_deferred_txn_lists(struct list_head *sgc_head,
2541
+ struct list_head *pf_head)
2542
+{
2543
+ struct binder_sg_copy *sgc, *tmpsgc;
2544
+ struct binder_ptr_fixup *pf, *tmppf;
2545
+
2546
+ list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
2547
+ list_del(&sgc->node);
2548
+ kfree(sgc);
2549
+ }
2550
+ list_for_each_entry_safe(pf, tmppf, pf_head, node) {
2551
+ list_del(&pf->node);
2552
+ kfree(pf);
2553
+ }
2554
+}
2555
+
2556
+/**
2557
+ * binder_defer_copy() - queue a scatter-gather buffer for copy
2558
+ * @sgc_head: list_head of scatter-gather copy list
2559
+ * @offset: binder buffer offset in target process
2560
+ * @sender_uaddr: user address in source process
2561
+ * @length: bytes to copy
2562
+ *
2563
+ * Specify a scatter-gather block to be copied. The actual copy must
2564
+ * be deferred until all the needed fixups are identified and queued.
2565
+ * Then the copy and fixups are done together so un-translated values
2566
+ * from the source are never visible in the target buffer.
2567
+ *
2568
+ * We are guaranteed that repeated calls to this function will have
2569
+ * monotonically increasing @offset values so the list will naturally
2570
+ * be ordered.
2571
+ *
2572
+ * Return: 0=success, else -errno
2573
+ */
2574
+static int binder_defer_copy(struct list_head *sgc_head, binder_size_t offset,
2575
+ const void __user *sender_uaddr, size_t length)
2576
+{
2577
+ struct binder_sg_copy *bc = kzalloc(sizeof(*bc), GFP_KERNEL);
2578
+
2579
+ if (!bc)
2580
+ return -ENOMEM;
2581
+
2582
+ bc->offset = offset;
2583
+ bc->sender_uaddr = sender_uaddr;
2584
+ bc->length = length;
2585
+ INIT_LIST_HEAD(&bc->node);
2586
+
2587
+ /*
2588
+ * We are guaranteed that the deferred copies are in-order
2589
+ * so just add to the tail.
2590
+ */
2591
+ list_add_tail(&bc->node, sgc_head);
2592
+
2593
+ return 0;
2594
+}
2595
+
2596
+/**
2597
+ * binder_add_fixup() - queue a fixup to be applied to sg copy
2598
+ * @pf_head: list_head of binder ptr fixup list
2599
+ * @offset: binder buffer offset in target process
2600
+ * @fixup: bytes to be copied for fixup
2601
+ * @skip_size: bytes to skip when copying (fixup will be applied later)
2602
+ *
2603
+ * Add the specified fixup to a list ordered by @offset. When copying
2604
+ * the scatter-gather buffers, the fixup will be copied instead of
2605
+ * data from the source buffer. For BINDER_TYPE_FDA fixups, the fixup
2606
+ * will be applied later (in target process context), so we just skip
2607
+ * the bytes specified by @skip_size. If @skip_size is 0, we copy the
2608
+ * value in @fixup.
2609
+ *
2610
+ * This function is called *mostly* in @offset order, but there are
2611
+ * exceptions. Since out-of-order inserts are relatively uncommon,
2612
+ * we insert the new element by searching backward from the tail of
2613
+ * the list.
2614
+ *
2615
+ * Return: 0=success, else -errno
2616
+ */
2617
+static int binder_add_fixup(struct list_head *pf_head, binder_size_t offset,
2618
+ binder_uintptr_t fixup, size_t skip_size)
2619
+{
2620
+ struct binder_ptr_fixup *pf = kzalloc(sizeof(*pf), GFP_KERNEL);
2621
+ struct binder_ptr_fixup *tmppf;
2622
+
2623
+ if (!pf)
2624
+ return -ENOMEM;
2625
+
2626
+ pf->offset = offset;
2627
+ pf->fixup_data = fixup;
2628
+ pf->skip_size = skip_size;
2629
+ INIT_LIST_HEAD(&pf->node);
2630
+
2631
+ /* Fixups are *mostly* added in-order, but there are some
2632
+ * exceptions. Look backwards through list for insertion point.
2633
+ */
2634
+ list_for_each_entry_reverse(tmppf, pf_head, node) {
2635
+ if (tmppf->offset < pf->offset) {
2636
+ list_add(&pf->node, &tmppf->node);
2637
+ return 0;
2638
+ }
2639
+ }
2640
+ /*
2641
+ * if we get here, then the new offset is the lowest so
2642
+ * insert at the head
2643
+ */
2644
+ list_add(&pf->node, pf_head);
2645
+ return 0;
2646
+}
2647
+
2648
+static int binder_translate_fd_array(struct list_head *pf_head,
2649
+ struct binder_fd_array_object *fda,
2650
+ const void __user *sender_ubuffer,
26882651 struct binder_buffer_object *parent,
2652
+ struct binder_buffer_object *sender_uparent,
26892653 struct binder_transaction *t,
26902654 struct binder_thread *thread,
26912655 struct binder_transaction *in_reply_to)
26922656 {
2693
- binder_size_t fdi, fd_buf_size, num_installed_fds;
2657
+ binder_size_t fdi, fd_buf_size;
26942658 binder_size_t fda_offset;
2695
- int target_fd;
2659
+ const void __user *sender_ufda_base;
26962660 struct binder_proc *proc = thread->proc;
2697
- struct binder_proc *target_proc = t->to_proc;
2661
+ int ret;
2662
+
2663
+ if (fda->num_fds == 0)
2664
+ return 0;
26982665
26992666 fd_buf_size = sizeof(u32) * fda->num_fds;
27002667 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
....@@ -2718,46 +2685,36 @@
27182685 */
27192686 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
27202687 fda->parent_offset;
2721
- if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
2688
+ sender_ufda_base = (void __user *)(uintptr_t)sender_uparent->buffer +
2689
+ fda->parent_offset;
2690
+
2691
+ if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32)) ||
2692
+ !IS_ALIGNED((unsigned long)sender_ufda_base, sizeof(u32))) {
27222693 binder_user_error("%d:%d parent offset not aligned correctly.\n",
27232694 proc->pid, thread->pid);
27242695 return -EINVAL;
27252696 }
2697
+ ret = binder_add_fixup(pf_head, fda_offset, 0, fda->num_fds * sizeof(u32));
2698
+ if (ret)
2699
+ return ret;
2700
+
27262701 for (fdi = 0; fdi < fda->num_fds; fdi++) {
27272702 u32 fd;
2728
-
27292703 binder_size_t offset = fda_offset + fdi * sizeof(fd);
2704
+ binder_size_t sender_uoffset = fdi * sizeof(fd);
27302705
2731
- binder_alloc_copy_from_buffer(&target_proc->alloc,
2732
- &fd, t->buffer,
2733
- offset, sizeof(fd));
2734
- target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
2735
- if (target_fd < 0)
2736
- goto err_translate_fd_failed;
2737
- binder_alloc_copy_to_buffer(&target_proc->alloc,
2738
- t->buffer, offset,
2739
- &target_fd, sizeof(fd));
2706
+ ret = copy_from_user(&fd, sender_ufda_base + sender_uoffset, sizeof(fd));
2707
+ if (!ret)
2708
+ ret = binder_translate_fd(fd, offset, t, thread,
2709
+ in_reply_to);
2710
+ if (ret)
2711
+ return ret > 0 ? -EINVAL : ret;
27402712 }
27412713 return 0;
2742
-
2743
-err_translate_fd_failed:
2744
- /*
2745
- * Failed to allocate fd or security error, free fds
2746
- * installed so far.
2747
- */
2748
- num_installed_fds = fdi;
2749
- for (fdi = 0; fdi < num_installed_fds; fdi++) {
2750
- u32 fd;
2751
- binder_size_t offset = fda_offset + fdi * sizeof(fd);
2752
- binder_alloc_copy_from_buffer(&target_proc->alloc,
2753
- &fd, t->buffer,
2754
- offset, sizeof(fd));
2755
- task_close_fd(target_proc, fd);
2756
- }
2757
- return target_fd;
27582714 }
27592715
2760
-static int binder_fixup_parent(struct binder_transaction *t,
2716
+static int binder_fixup_parent(struct list_head *pf_head,
2717
+ struct binder_transaction *t,
27612718 struct binder_thread *thread,
27622719 struct binder_buffer_object *bp,
27632720 binder_size_t off_start_offset,
....@@ -2803,10 +2760,57 @@
28032760 }
28042761 buffer_offset = bp->parent_offset +
28052762 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
2806
- binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2807
- &bp->buffer, sizeof(bp->buffer));
2763
+ return binder_add_fixup(pf_head, buffer_offset, bp->buffer, 0);
2764
+}
28082765
2809
- return 0;
2766
+/**
2767
+ * binder_can_update_transaction() - Can a txn be superseded by an updated one?
2768
+ * @t1: the pending async txn in the frozen process
2769
+ * @t2: the new async txn to supersede the outdated pending one
2770
+ *
2771
+ * Return: true if t2 can supersede t1
2772
+ * false if t2 can not supersede t1
2773
+ */
2774
+static bool binder_can_update_transaction(struct binder_transaction *t1,
2775
+ struct binder_transaction *t2)
2776
+{
2777
+ if ((t1->flags & t2->flags & (TF_ONE_WAY | TF_UPDATE_TXN)) !=
2778
+ (TF_ONE_WAY | TF_UPDATE_TXN) || !t1->to_proc || !t2->to_proc)
2779
+ return false;
2780
+ if (t1->to_proc->tsk == t2->to_proc->tsk && t1->code == t2->code &&
2781
+ t1->flags == t2->flags && t1->buffer->pid == t2->buffer->pid &&
2782
+ t1->buffer->target_node->ptr == t2->buffer->target_node->ptr &&
2783
+ t1->buffer->target_node->cookie == t2->buffer->target_node->cookie)
2784
+ return true;
2785
+ return false;
2786
+}
2787
+
2788
+/**
2789
+ * binder_find_outdated_transaction_ilocked() - Find the outdated transaction
2790
+ * @t: new async transaction
2791
+ * @target_list: list to find outdated transaction
2792
+ *
2793
+ * Return: the outdated transaction if found
2794
+ * NULL if no outdated transacton can be found
2795
+ *
2796
+ * Requires the proc->inner_lock to be held.
2797
+ */
2798
+static struct binder_transaction *
2799
+binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
2800
+ struct list_head *target_list)
2801
+{
2802
+ struct binder_work *w;
2803
+
2804
+ list_for_each_entry(w, target_list, entry) {
2805
+ struct binder_transaction *t_queued;
2806
+
2807
+ if (w->type != BINDER_WORK_TRANSACTION)
2808
+ continue;
2809
+ t_queued = container_of(w, struct binder_transaction, work);
2810
+ if (binder_can_update_transaction(t_queued, t))
2811
+ return t_queued;
2812
+ }
2813
+ return NULL;
28102814 }
28112815
28122816 /**
....@@ -2823,10 +2827,11 @@
28232827 * If the @thread parameter is not NULL, the transaction is always queued
28242828 * to the waitlist of that specific thread.
28252829 *
2826
- * Return: true if the transactions was successfully queued
2827
- * false if the target process or thread is dead
2830
+ * Return: 0 if the transaction was successfully queued
2831
+ * BR_DEAD_REPLY if the target process or thread is dead
2832
+ * BR_FROZEN_REPLY if the target process or thread is frozen
28282833 */
2829
-static bool binder_proc_transaction(struct binder_transaction *t,
2834
+static int binder_proc_transaction(struct binder_transaction *t,
28302835 struct binder_proc *proc,
28312836 struct binder_thread *thread)
28322837 {
....@@ -2834,6 +2839,7 @@
28342839 struct binder_priority node_prio;
28352840 bool oneway = !!(t->flags & TF_ONE_WAY);
28362841 bool pending_async = false;
2842
+ struct binder_transaction *t_outdated = NULL;
28372843
28382844 BUG_ON(!node);
28392845 binder_node_lock(node);
....@@ -2842,23 +2848,30 @@
28422848
28432849 if (oneway) {
28442850 BUG_ON(thread);
2845
- if (node->has_async_transaction) {
2851
+ if (node->has_async_transaction)
28462852 pending_async = true;
2847
- } else {
2853
+ else
28482854 node->has_async_transaction = true;
2849
- }
28502855 }
28512856
28522857 binder_inner_proc_lock(proc);
2858
+ if (proc->is_frozen) {
2859
+ proc->sync_recv |= !oneway;
2860
+ proc->async_recv |= oneway;
2861
+ }
28532862
2854
- if (proc->is_dead || (thread && thread->is_dead)) {
2863
+ if ((proc->is_frozen && !oneway) || proc->is_dead ||
2864
+ (thread && thread->is_dead)) {
28552865 binder_inner_proc_unlock(proc);
28562866 binder_node_unlock(node);
2857
- return false;
2867
+ return proc->is_frozen ? BR_FROZEN_REPLY : BR_DEAD_REPLY;
28582868 }
28592869
28602870 if (!thread && !pending_async)
28612871 thread = binder_select_thread_ilocked(proc);
2872
+
2873
+ trace_android_vh_binder_proc_transaction(current, proc->tsk,
2874
+ thread ? thread->task : 0, node->debug_id, t->code, pending_async);
28622875
28632876 if (thread) {
28642877 binder_transaction_priority(thread->task, t, node_prio,
....@@ -2867,16 +2880,47 @@
28672880 } else if (!pending_async) {
28682881 binder_enqueue_work_ilocked(&t->work, &proc->todo);
28692882 } else {
2883
+ if ((t->flags & TF_UPDATE_TXN) && proc->is_frozen) {
2884
+ t_outdated = binder_find_outdated_transaction_ilocked(t,
2885
+ &node->async_todo);
2886
+ if (t_outdated) {
2887
+ binder_debug(BINDER_DEBUG_TRANSACTION,
2888
+ "txn %d supersedes %d\n",
2889
+ t->debug_id, t_outdated->debug_id);
2890
+ list_del_init(&t_outdated->work.entry);
2891
+ proc->outstanding_txns--;
2892
+ }
2893
+ }
28702894 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
28712895 }
2896
+
2897
+ trace_android_vh_binder_proc_transaction_end(current, proc->tsk,
2898
+ thread ? thread->task : NULL, t->code, pending_async, !oneway);
28722899
28732900 if (!pending_async)
28742901 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
28752902
2903
+ proc->outstanding_txns++;
28762904 binder_inner_proc_unlock(proc);
28772905 binder_node_unlock(node);
28782906
2879
- return true;
2907
+ /*
2908
+ * To reduce potential contention, free the outdated transaction and
2909
+ * buffer after releasing the locks.
2910
+ */
2911
+ if (t_outdated) {
2912
+ struct binder_buffer *buffer = t_outdated->buffer;
2913
+
2914
+ t_outdated->buffer = NULL;
2915
+ buffer->transaction = NULL;
2916
+ trace_binder_transaction_update_buffer_release(buffer);
2917
+ binder_transaction_buffer_release(proc, NULL, buffer, 0, 0);
2918
+ binder_alloc_free_buf(&proc->alloc, buffer);
2919
+ kfree(t_outdated);
2920
+ binder_stats_deleted(BINDER_STAT_TRANSACTION);
2921
+ }
2922
+
2923
+ return 0;
28802924 }
28812925
28822926 /**
....@@ -2934,6 +2978,7 @@
29342978 binder_size_t off_start_offset, off_end_offset;
29352979 binder_size_t off_min;
29362980 binder_size_t sg_buf_offset, sg_buf_end_offset;
2981
+ binder_size_t user_offset = 0;
29372982 struct binder_proc *target_proc = NULL;
29382983 struct binder_thread *target_thread = NULL;
29392984 struct binder_node *target_node = NULL;
....@@ -2948,6 +2993,12 @@
29482993 int t_debug_id = atomic_inc_return(&binder_last_id);
29492994 char *secctx = NULL;
29502995 u32 secctx_sz = 0;
2996
+ struct list_head sgc_head;
2997
+ struct list_head pf_head;
2998
+ const void __user *user_buffer = (const void __user *)
2999
+ (uintptr_t)tr->data.ptr.buffer;
3000
+ INIT_LIST_HEAD(&sgc_head);
3001
+ INIT_LIST_HEAD(&pf_head);
29513002
29523003 e = binder_transaction_log_add(&binder_transaction_log);
29533004 e->debug_id = t_debug_id;
....@@ -2957,7 +3008,7 @@
29573008 e->target_handle = tr->target.handle;
29583009 e->data_size = tr->data_size;
29593010 e->offsets_size = tr->offsets_size;
2960
- e->context_name = proc->context->name;
3011
+ strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME);
29613012
29623013 if (reply) {
29633014 binder_inner_proc_lock(proc);
....@@ -2991,6 +3042,8 @@
29913042 binder_inner_proc_unlock(proc);
29923043 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
29933044 if (target_thread == NULL) {
3045
+ /* annotation for sparse */
3046
+ __release(&target_thread->proc->inner_lock);
29943047 return_error = BR_DEAD_REPLY;
29953048 return_error_line = __LINE__;
29963049 goto err_dead_binder;
....@@ -3012,6 +3065,7 @@
30123065 target_proc = target_thread->proc;
30133066 target_proc->tmp_ref++;
30143067 binder_inner_proc_unlock(target_thread->proc);
3068
+ trace_android_vh_binder_reply(target_proc, proc, thread, tr);
30153069 } else {
30163070 if (tr->target.handle) {
30173071 struct binder_ref *ref;
....@@ -3031,8 +3085,8 @@
30313085 ref->node, &target_proc,
30323086 &return_error);
30333087 } else {
3034
- binder_user_error("%d:%d got transaction to invalid handle\n",
3035
- proc->pid, thread->pid);
3088
+ binder_user_error("%d:%d got transaction to invalid handle, %u\n",
3089
+ proc->pid, thread->pid, tr->target.handle);
30363090 return_error = BR_FAILED_REPLY;
30373091 }
30383092 binder_proc_unlock(proc);
....@@ -3064,8 +3118,9 @@
30643118 goto err_dead_binder;
30653119 }
30663120 e->to_node = target_node->debug_id;
3067
- if (security_binder_transaction(proc->cred,
3068
- target_proc->cred) < 0) {
3121
+ trace_android_vh_binder_trans(target_proc, proc, thread, tr);
3122
+ if (security_binder_transaction(binder_get_cred(proc),
3123
+ binder_get_cred(target_proc)) < 0) {
30693124 return_error = BR_FAILED_REPLY;
30703125 return_error_param = -EPERM;
30713126 return_error_line = __LINE__;
....@@ -3133,6 +3188,7 @@
31333188 if (target_thread)
31343189 e->to_thread = target_thread->pid;
31353190 e->to_proc = target_proc->pid;
3191
+ trace_android_rvh_binder_transaction(target_proc, proc, thread, tr);
31363192
31373193 /* TODO: reuse incoming transaction for reply */
31383194 t = kzalloc(sizeof(*t), GFP_KERNEL);
....@@ -3142,8 +3198,10 @@
31423198 return_error_line = __LINE__;
31433199 goto err_alloc_t_failed;
31443200 }
3201
+ INIT_LIST_HEAD(&t->fd_fixups);
31453202 binder_stats_created(BINDER_STAT_TRANSACTION);
31463203 spin_lock_init(&t->lock);
3204
+ trace_android_vh_binder_transaction_init(t);
31473205
31483206 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
31493207 if (tcomplete == NULL) {
....@@ -3197,9 +3255,28 @@
31973255 if (target_node && target_node->txn_security_ctx) {
31983256 u32 secid;
31993257 size_t added_size;
3258
+ int max_retries = 100;
32003259
3201
- security_cred_getsecid(proc->cred, &secid);
3260
+ security_cred_getsecid(binder_get_cred(proc), &secid);
3261
+ retry_alloc:
32023262 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3263
+ if (ret == -ENOMEM && max_retries-- > 0) {
3264
+ struct page *dummy_page;
3265
+
3266
+ /*
3267
+ * security_secid_to_secctx() can fail because of a
3268
+ * GFP_ATOMIC allocation in which case -ENOMEM is
3269
+ * returned. This needs to be retried, but there is
3270
+ * currently no way to tell userspace to retry so we
3271
+ * do it here. We make sure there is still available
3272
+ * memory first and then retry.
3273
+ */
3274
+ dummy_page = alloc_page(GFP_KERNEL);
3275
+ if (dummy_page) {
3276
+ __free_page(dummy_page);
3277
+ goto retry_alloc;
3278
+ }
3279
+ }
32033280 if (ret) {
32043281 return_error = BR_FAILED_REPLY;
32053282 return_error_param = ret;
....@@ -3234,36 +3311,29 @@
32343311 goto err_binder_alloc_buf_failed;
32353312 }
32363313 if (secctx) {
3314
+ int err;
32373315 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
32383316 ALIGN(tr->offsets_size, sizeof(void *)) +
32393317 ALIGN(extra_buffers_size, sizeof(void *)) -
32403318 ALIGN(secctx_sz, sizeof(u64));
32413319
32423320 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
3243
- binder_alloc_copy_to_buffer(&target_proc->alloc,
3244
- t->buffer, buf_offset,
3245
- secctx, secctx_sz);
3321
+ err = binder_alloc_copy_to_buffer(&target_proc->alloc,
3322
+ t->buffer, buf_offset,
3323
+ secctx, secctx_sz);
3324
+ if (err) {
3325
+ t->security_ctx = 0;
3326
+ WARN_ON(1);
3327
+ }
32463328 security_release_secctx(secctx, secctx_sz);
32473329 secctx = NULL;
32483330 }
32493331 t->buffer->debug_id = t->debug_id;
32503332 t->buffer->transaction = t;
32513333 t->buffer->target_node = target_node;
3334
+ t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF);
32523335 trace_binder_transaction_alloc_buf(t->buffer);
32533336
3254
- if (binder_alloc_copy_user_to_buffer(
3255
- &target_proc->alloc,
3256
- t->buffer, 0,
3257
- (const void __user *)
3258
- (uintptr_t)tr->data.ptr.buffer,
3259
- tr->data_size)) {
3260
- binder_user_error("%d:%d got transaction with invalid data ptr\n",
3261
- proc->pid, thread->pid);
3262
- return_error = BR_FAILED_REPLY;
3263
- return_error_param = -EFAULT;
3264
- return_error_line = __LINE__;
3265
- goto err_copy_data_failed;
3266
- }
32673337 if (binder_alloc_copy_user_to_buffer(
32683338 &target_proc->alloc,
32693339 t->buffer,
....@@ -3308,14 +3378,39 @@
33083378 size_t object_size;
33093379 struct binder_object object;
33103380 binder_size_t object_offset;
3381
+ binder_size_t copy_size;
33113382
3312
- binder_alloc_copy_from_buffer(&target_proc->alloc,
3313
- &object_offset,
3314
- t->buffer,
3315
- buffer_offset,
3316
- sizeof(object_offset));
3317
- object_size = binder_get_object(target_proc, t->buffer,
3318
- object_offset, &object);
3383
+ if (binder_alloc_copy_from_buffer(&target_proc->alloc,
3384
+ &object_offset,
3385
+ t->buffer,
3386
+ buffer_offset,
3387
+ sizeof(object_offset))) {
3388
+ return_error = BR_FAILED_REPLY;
3389
+ return_error_param = -EINVAL;
3390
+ return_error_line = __LINE__;
3391
+ goto err_bad_offset;
3392
+ }
3393
+
3394
+ /*
3395
+ * Copy the source user buffer up to the next object
3396
+ * that will be processed.
3397
+ */
3398
+ copy_size = object_offset - user_offset;
3399
+ if (copy_size && (user_offset > object_offset ||
3400
+ binder_alloc_copy_user_to_buffer(
3401
+ &target_proc->alloc,
3402
+ t->buffer, user_offset,
3403
+ user_buffer + user_offset,
3404
+ copy_size))) {
3405
+ binder_user_error("%d:%d got transaction with invalid data ptr\n",
3406
+ proc->pid, thread->pid);
3407
+ return_error = BR_FAILED_REPLY;
3408
+ return_error_param = -EFAULT;
3409
+ return_error_line = __LINE__;
3410
+ goto err_copy_data_failed;
3411
+ }
3412
+ object_size = binder_get_object(target_proc, user_buffer,
3413
+ t->buffer, object_offset, &object);
33193414 if (object_size == 0 || object_offset < off_min) {
33203415 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
33213416 proc->pid, thread->pid,
....@@ -3327,6 +3422,11 @@
33273422 return_error_line = __LINE__;
33283423 goto err_bad_offset;
33293424 }
3425
+ /*
3426
+ * Set offset to the next buffer fragment to be
3427
+ * copied
3428
+ */
3429
+ user_offset = object_offset + object_size;
33303430
33313431 hdr = &object.hdr;
33323432 off_min = object_offset + object_size;
....@@ -3337,15 +3437,17 @@
33373437
33383438 fp = to_flat_binder_object(hdr);
33393439 ret = binder_translate_binder(fp, t, thread);
3340
- if (ret < 0) {
3440
+
3441
+ if (ret < 0 ||
3442
+ binder_alloc_copy_to_buffer(&target_proc->alloc,
3443
+ t->buffer,
3444
+ object_offset,
3445
+ fp, sizeof(*fp))) {
33413446 return_error = BR_FAILED_REPLY;
33423447 return_error_param = ret;
33433448 return_error_line = __LINE__;
33443449 goto err_translate_failed;
33453450 }
3346
- binder_alloc_copy_to_buffer(&target_proc->alloc,
3347
- t->buffer, object_offset,
3348
- fp, sizeof(*fp));
33493451 } break;
33503452 case BINDER_TYPE_HANDLE:
33513453 case BINDER_TYPE_WEAK_HANDLE: {
....@@ -3353,37 +3455,42 @@
33533455
33543456 fp = to_flat_binder_object(hdr);
33553457 ret = binder_translate_handle(fp, t, thread);
3356
- if (ret < 0) {
3458
+ if (ret < 0 ||
3459
+ binder_alloc_copy_to_buffer(&target_proc->alloc,
3460
+ t->buffer,
3461
+ object_offset,
3462
+ fp, sizeof(*fp))) {
33573463 return_error = BR_FAILED_REPLY;
33583464 return_error_param = ret;
33593465 return_error_line = __LINE__;
33603466 goto err_translate_failed;
33613467 }
3362
- binder_alloc_copy_to_buffer(&target_proc->alloc,
3363
- t->buffer, object_offset,
3364
- fp, sizeof(*fp));
33653468 } break;
33663469
33673470 case BINDER_TYPE_FD: {
33683471 struct binder_fd_object *fp = to_binder_fd_object(hdr);
3369
- int target_fd = binder_translate_fd(fp->fd, t, thread,
3370
- in_reply_to);
3472
+ binder_size_t fd_offset = object_offset +
3473
+ (uintptr_t)&fp->fd - (uintptr_t)fp;
3474
+ int ret = binder_translate_fd(fp->fd, fd_offset, t,
3475
+ thread, in_reply_to);
33713476
3372
- if (target_fd < 0) {
3477
+ fp->pad_binder = 0;
3478
+ if (ret < 0 ||
3479
+ binder_alloc_copy_to_buffer(&target_proc->alloc,
3480
+ t->buffer,
3481
+ object_offset,
3482
+ fp, sizeof(*fp))) {
33733483 return_error = BR_FAILED_REPLY;
3374
- return_error_param = target_fd;
3484
+ return_error_param = ret;
33753485 return_error_line = __LINE__;
33763486 goto err_translate_failed;
33773487 }
3378
- fp->pad_binder = 0;
3379
- fp->fd = target_fd;
3380
- binder_alloc_copy_to_buffer(&target_proc->alloc,
3381
- t->buffer, object_offset,
3382
- fp, sizeof(*fp));
33833488 } break;
33843489 case BINDER_TYPE_FDA: {
33853490 struct binder_object ptr_object;
33863491 binder_size_t parent_offset;
3492
+ struct binder_object user_object;
3493
+ size_t user_parent_size;
33873494 struct binder_fd_array_object *fda =
33883495 to_binder_fd_array_object(hdr);
33893496 size_t num_valid = (buffer_offset - off_start_offset) /
....@@ -3415,11 +3522,35 @@
34153522 return_error_line = __LINE__;
34163523 goto err_bad_parent;
34173524 }
3418
- ret = binder_translate_fd_array(fda, parent, t, thread,
3419
- in_reply_to);
3420
- if (ret < 0) {
3525
+ /*
3526
+ * We need to read the user version of the parent
3527
+ * object to get the original user offset
3528
+ */
3529
+ user_parent_size =
3530
+ binder_get_object(proc, user_buffer, t->buffer,
3531
+ parent_offset, &user_object);
3532
+ if (user_parent_size != sizeof(user_object.bbo)) {
3533
+ binder_user_error("%d:%d invalid ptr object size: %zd vs %zd\n",
3534
+ proc->pid, thread->pid,
3535
+ user_parent_size,
3536
+ sizeof(user_object.bbo));
34213537 return_error = BR_FAILED_REPLY;
3422
- return_error_param = ret;
3538
+ return_error_param = -EINVAL;
3539
+ return_error_line = __LINE__;
3540
+ goto err_bad_parent;
3541
+ }
3542
+ ret = binder_translate_fd_array(&pf_head, fda,
3543
+ user_buffer, parent,
3544
+ &user_object.bbo, t,
3545
+ thread, in_reply_to);
3546
+ if (!ret)
3547
+ ret = binder_alloc_copy_to_buffer(&target_proc->alloc,
3548
+ t->buffer,
3549
+ object_offset,
3550
+ fda, sizeof(*fda));
3551
+ if (ret) {
3552
+ return_error = BR_FAILED_REPLY;
3553
+ return_error_param = ret > 0 ? -EINVAL : ret;
34233554 return_error_line = __LINE__;
34243555 goto err_translate_failed;
34253556 }
....@@ -3441,19 +3572,14 @@
34413572 return_error_line = __LINE__;
34423573 goto err_bad_offset;
34433574 }
3444
- if (binder_alloc_copy_user_to_buffer(
3445
- &target_proc->alloc,
3446
- t->buffer,
3447
- sg_buf_offset,
3448
- (const void __user *)
3449
- (uintptr_t)bp->buffer,
3450
- bp->length)) {
3451
- binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3452
- proc->pid, thread->pid);
3453
- return_error_param = -EFAULT;
3575
+ ret = binder_defer_copy(&sgc_head, sg_buf_offset,
3576
+ (const void __user *)(uintptr_t)bp->buffer,
3577
+ bp->length);
3578
+ if (ret) {
34543579 return_error = BR_FAILED_REPLY;
3580
+ return_error_param = ret;
34553581 return_error_line = __LINE__;
3456
- goto err_copy_data_failed;
3582
+ goto err_translate_failed;
34573583 }
34583584 /* Fixup buffer pointer to target proc address space */
34593585 bp->buffer = (uintptr_t)
....@@ -3462,20 +3588,22 @@
34623588
34633589 num_valid = (buffer_offset - off_start_offset) /
34643590 sizeof(binder_size_t);
3465
- ret = binder_fixup_parent(t, thread, bp,
3591
+ ret = binder_fixup_parent(&pf_head, t,
3592
+ thread, bp,
34663593 off_start_offset,
34673594 num_valid,
34683595 last_fixup_obj_off,
34693596 last_fixup_min_off);
3470
- if (ret < 0) {
3597
+ if (ret < 0 ||
3598
+ binder_alloc_copy_to_buffer(&target_proc->alloc,
3599
+ t->buffer,
3600
+ object_offset,
3601
+ bp, sizeof(*bp))) {
34713602 return_error = BR_FAILED_REPLY;
34723603 return_error_param = ret;
34733604 return_error_line = __LINE__;
34743605 goto err_translate_failed;
34753606 }
3476
- binder_alloc_copy_to_buffer(&target_proc->alloc,
3477
- t->buffer, object_offset,
3478
- bp, sizeof(*bp));
34793607 last_fixup_obj_off = object_offset;
34803608 last_fixup_min_off = 0;
34813609 } break;
....@@ -3488,21 +3616,51 @@
34883616 goto err_bad_object_type;
34893617 }
34903618 }
3491
- tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3619
+ /* Done processing objects, copy the rest of the buffer */
3620
+ if (binder_alloc_copy_user_to_buffer(
3621
+ &target_proc->alloc,
3622
+ t->buffer, user_offset,
3623
+ user_buffer + user_offset,
3624
+ tr->data_size - user_offset)) {
3625
+ binder_user_error("%d:%d got transaction with invalid data ptr\n",
3626
+ proc->pid, thread->pid);
3627
+ return_error = BR_FAILED_REPLY;
3628
+ return_error_param = -EFAULT;
3629
+ return_error_line = __LINE__;
3630
+ goto err_copy_data_failed;
3631
+ }
3632
+
3633
+ ret = binder_do_deferred_txn_copies(&target_proc->alloc, t->buffer,
3634
+ &sgc_head, &pf_head);
3635
+ if (ret) {
3636
+ binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3637
+ proc->pid, thread->pid);
3638
+ return_error = BR_FAILED_REPLY;
3639
+ return_error_param = ret;
3640
+ return_error_line = __LINE__;
3641
+ goto err_copy_data_failed;
3642
+ }
3643
+ if (t->buffer->oneway_spam_suspect)
3644
+ tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT;
3645
+ else
3646
+ tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
34923647 t->work.type = BINDER_WORK_TRANSACTION;
34933648
34943649 if (reply) {
34953650 binder_enqueue_thread_work(thread, tcomplete);
34963651 binder_inner_proc_lock(target_proc);
34973652 if (target_thread->is_dead) {
3653
+ return_error = BR_DEAD_REPLY;
34983654 binder_inner_proc_unlock(target_proc);
34993655 goto err_dead_proc_or_thread;
35003656 }
35013657 BUG_ON(t->buffer->async_transaction != 0);
35023658 binder_pop_transaction_ilocked(target_thread, in_reply_to);
35033659 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3660
+ target_proc->outstanding_txns++;
35043661 binder_inner_proc_unlock(target_proc);
35053662 wake_up_interruptible_sync(&target_thread->wait);
3663
+ trace_android_vh_binder_restore_priority(in_reply_to, current);
35063664 binder_restore_priority(current, in_reply_to->saved_priority);
35073665 binder_free_transaction(in_reply_to);
35083666 } else if (!(t->flags & TF_ONE_WAY)) {
....@@ -3520,7 +3678,9 @@
35203678 t->from_parent = thread->transaction_stack;
35213679 thread->transaction_stack = t;
35223680 binder_inner_proc_unlock(proc);
3523
- if (!binder_proc_transaction(t, target_proc, target_thread)) {
3681
+ return_error = binder_proc_transaction(t,
3682
+ target_proc, target_thread);
3683
+ if (return_error) {
35243684 binder_inner_proc_lock(proc);
35253685 binder_pop_transaction_ilocked(thread, t);
35263686 binder_inner_proc_unlock(proc);
....@@ -3530,7 +3690,8 @@
35303690 BUG_ON(target_node == NULL);
35313691 BUG_ON(t->buffer->async_transaction != 1);
35323692 binder_enqueue_thread_work(thread, tcomplete);
3533
- if (!binder_proc_transaction(t, target_proc, NULL))
3693
+ return_error = binder_proc_transaction(t, target_proc, NULL);
3694
+ if (return_error)
35343695 goto err_dead_proc_or_thread;
35353696 }
35363697 if (target_thread)
....@@ -3547,7 +3708,6 @@
35473708 return;
35483709
35493710 err_dead_proc_or_thread:
3550
- return_error = BR_DEAD_REPLY;
35513711 return_error_line = __LINE__;
35523712 binder_dequeue_work(proc, tcomplete);
35533713 err_translate_failed:
....@@ -3555,8 +3715,10 @@
35553715 err_bad_offset:
35563716 err_bad_parent:
35573717 err_copy_data_failed:
3718
+ binder_cleanup_deferred_txn_lists(&sgc_head, &pf_head);
3719
+ binder_free_txn_fixups(t);
35583720 trace_binder_transaction_failed_buffer_release(t->buffer);
3559
- binder_transaction_buffer_release(target_proc, t->buffer,
3721
+ binder_transaction_buffer_release(target_proc, NULL, t->buffer,
35603722 buffer_offset, true);
35613723 if (target_node)
35623724 binder_dec_node_tmpref(target_node);
....@@ -3613,6 +3775,7 @@
36133775
36143776 BUG_ON(thread->return_error.cmd != BR_OK);
36153777 if (in_reply_to) {
3778
+ trace_android_vh_binder_restore_priority(in_reply_to, current);
36163779 binder_restore_priority(current, in_reply_to->saved_priority);
36173780 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
36183781 binder_enqueue_thread_work(thread, &thread->return_error.work);
....@@ -3621,6 +3784,52 @@
36213784 thread->return_error.cmd = return_error;
36223785 binder_enqueue_thread_work(thread, &thread->return_error.work);
36233786 }
3787
+}
3788
+
3789
+/**
3790
+ * binder_free_buf() - free the specified buffer
3791
+ * @proc: binder proc that owns buffer
3792
+ * @buffer: buffer to be freed
3793
+ * @is_failure: failed to send transaction
3794
+ *
3795
+ * If buffer for an async transaction, enqueue the next async
3796
+ * transaction from the node.
3797
+ *
3798
+ * Cleanup buffer and free it.
3799
+ */
3800
+static void
3801
+binder_free_buf(struct binder_proc *proc,
3802
+ struct binder_thread *thread,
3803
+ struct binder_buffer *buffer, bool is_failure)
3804
+{
3805
+ binder_inner_proc_lock(proc);
3806
+ if (buffer->transaction) {
3807
+ buffer->transaction->buffer = NULL;
3808
+ buffer->transaction = NULL;
3809
+ }
3810
+ binder_inner_proc_unlock(proc);
3811
+ if (buffer->async_transaction && buffer->target_node) {
3812
+ struct binder_node *buf_node;
3813
+ struct binder_work *w;
3814
+
3815
+ buf_node = buffer->target_node;
3816
+ binder_node_inner_lock(buf_node);
3817
+ BUG_ON(!buf_node->has_async_transaction);
3818
+ BUG_ON(buf_node->proc != proc);
3819
+ w = binder_dequeue_work_head_ilocked(
3820
+ &buf_node->async_todo);
3821
+ if (!w) {
3822
+ buf_node->has_async_transaction = false;
3823
+ } else {
3824
+ binder_enqueue_work_ilocked(
3825
+ w, &proc->todo);
3826
+ binder_wakeup_proc_ilocked(proc);
3827
+ }
3828
+ binder_node_inner_unlock(buf_node);
3829
+ }
3830
+ trace_binder_transaction_buffer_release(buffer);
3831
+ binder_transaction_buffer_release(proc, thread, buffer, 0, is_failure);
3832
+ binder_alloc_free_buf(&proc->alloc, buffer);
36243833 }
36253834
36263835 static int binder_thread_write(struct binder_proc *proc,
....@@ -3813,35 +4022,7 @@
38134022 proc->pid, thread->pid, (u64)data_ptr,
38144023 buffer->debug_id,
38154024 buffer->transaction ? "active" : "finished");
3816
-
3817
- binder_inner_proc_lock(proc);
3818
- if (buffer->transaction) {
3819
- buffer->transaction->buffer = NULL;
3820
- buffer->transaction = NULL;
3821
- }
3822
- binder_inner_proc_unlock(proc);
3823
- if (buffer->async_transaction && buffer->target_node) {
3824
- struct binder_node *buf_node;
3825
- struct binder_work *w;
3826
-
3827
- buf_node = buffer->target_node;
3828
- binder_node_inner_lock(buf_node);
3829
- BUG_ON(!buf_node->has_async_transaction);
3830
- BUG_ON(buf_node->proc != proc);
3831
- w = binder_dequeue_work_head_ilocked(
3832
- &buf_node->async_todo);
3833
- if (!w) {
3834
- buf_node->has_async_transaction = false;
3835
- } else {
3836
- binder_enqueue_work_ilocked(
3837
- w, &proc->todo);
3838
- binder_wakeup_proc_ilocked(proc);
3839
- }
3840
- binder_node_inner_unlock(buf_node);
3841
- }
3842
- trace_binder_transaction_buffer_release(buffer);
3843
- binder_transaction_buffer_release(proc, buffer, 0, false);
3844
- binder_alloc_free_buf(&proc->alloc, buffer);
4025
+ binder_free_buf(proc, thread, buffer, false);
38454026 break;
38464027 }
38474028
....@@ -3887,6 +4068,7 @@
38874068 }
38884069 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
38894070 binder_inner_proc_unlock(proc);
4071
+ trace_android_vh_binder_looper_state_registered(thread, proc);
38904072 break;
38914073 case BC_ENTER_LOOPER:
38924074 binder_debug(BINDER_DEBUG_THREADS,
....@@ -4148,18 +4330,84 @@
41484330 if (do_proc_work)
41494331 list_add(&thread->waiting_thread_node,
41504332 &proc->waiting_threads);
4333
+ trace_android_vh_binder_wait_for_work(do_proc_work, thread, proc);
41514334 binder_inner_proc_unlock(proc);
41524335 schedule();
41534336 binder_inner_proc_lock(proc);
41544337 list_del_init(&thread->waiting_thread_node);
41554338 if (signal_pending(current)) {
4156
- ret = -ERESTARTSYS;
4339
+ ret = -EINTR;
41574340 break;
41584341 }
41594342 }
41604343 finish_wait(&thread->wait, &wait);
41614344 binder_inner_proc_unlock(proc);
41624345 freezer_count();
4346
+
4347
+ return ret;
4348
+}
4349
+
4350
+/**
4351
+ * binder_apply_fd_fixups() - finish fd translation
4352
+ * @proc: binder_proc associated @t->buffer
4353
+ * @t: binder transaction with list of fd fixups
4354
+ *
4355
+ * Now that we are in the context of the transaction target
4356
+ * process, we can allocate and install fds. Process the
4357
+ * list of fds to translate and fixup the buffer with the
4358
+ * new fds.
4359
+ *
4360
+ * If we fail to allocate an fd, then free the resources by
4361
+ * fput'ing files that have not been processed and ksys_close'ing
4362
+ * any fds that have already been allocated.
4363
+ */
4364
+static int binder_apply_fd_fixups(struct binder_proc *proc,
4365
+ struct binder_transaction *t)
4366
+{
4367
+ struct binder_txn_fd_fixup *fixup, *tmp;
4368
+ int ret = 0;
4369
+
4370
+ list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
4371
+ int fd = get_unused_fd_flags(O_CLOEXEC);
4372
+
4373
+ if (fd < 0) {
4374
+ binder_debug(BINDER_DEBUG_TRANSACTION,
4375
+ "failed fd fixup txn %d fd %d\n",
4376
+ t->debug_id, fd);
4377
+ ret = -ENOMEM;
4378
+ break;
4379
+ }
4380
+ binder_debug(BINDER_DEBUG_TRANSACTION,
4381
+ "fd fixup txn %d fd %d\n",
4382
+ t->debug_id, fd);
4383
+ trace_binder_transaction_fd_recv(t, fd, fixup->offset);
4384
+ fd_install(fd, fixup->file);
4385
+ fixup->file = NULL;
4386
+ if (binder_alloc_copy_to_buffer(&proc->alloc, t->buffer,
4387
+ fixup->offset, &fd,
4388
+ sizeof(u32))) {
4389
+ ret = -EINVAL;
4390
+ break;
4391
+ }
4392
+ }
4393
+ list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
4394
+ if (fixup->file) {
4395
+ fput(fixup->file);
4396
+ } else if (ret) {
4397
+ u32 fd;
4398
+ int err;
4399
+
4400
+ err = binder_alloc_copy_from_buffer(&proc->alloc, &fd,
4401
+ t->buffer,
4402
+ fixup->offset,
4403
+ sizeof(fd));
4404
+ WARN_ON(err);
4405
+ if (!err)
4406
+ binder_deferred_fd_close(fd);
4407
+ }
4408
+ list_del(&fixup->fixup_entry);
4409
+ kfree(fixup);
4410
+ }
41634411
41644412 return ret;
41654413 }
....@@ -4200,6 +4448,7 @@
42004448 wait_event_interruptible(binder_user_error_wait,
42014449 binder_stop_on_user_error < 2);
42024450 }
4451
+ trace_android_vh_binder_restore_priority(NULL, current);
42034452 binder_restore_priority(current, proc->default_priority);
42044453 }
42054454
....@@ -4244,6 +4493,7 @@
42444493 binder_inner_proc_unlock(proc);
42454494 break;
42464495 }
4496
+ trace_android_vh_binder_thread_read(&list, proc, thread);
42474497 w = binder_dequeue_work_head_ilocked(list);
42484498 if (binder_worklist_empty_ilocked(&thread->todo))
42494499 thread->process_todo = false;
....@@ -4267,9 +4517,14 @@
42674517
42684518 binder_stat_br(proc, thread, cmd);
42694519 } break;
4270
- case BINDER_WORK_TRANSACTION_COMPLETE: {
4520
+ case BINDER_WORK_TRANSACTION_COMPLETE:
4521
+ case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
4522
+ if (proc->oneway_spam_detection_enabled &&
4523
+ w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
4524
+ cmd = BR_ONEWAY_SPAM_SUSPECT;
4525
+ else
4526
+ cmd = BR_TRANSACTION_COMPLETE;
42714527 binder_inner_proc_unlock(proc);
4272
- cmd = BR_TRANSACTION_COMPLETE;
42734528 kfree(w);
42744529 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
42754530 if (put_user(cmd, (uint32_t __user *)ptr))
....@@ -4409,6 +4664,11 @@
44094664 if (cmd == BR_DEAD_BINDER)
44104665 goto done; /* DEAD_BINDER notifications can cause transactions */
44114666 } break;
4667
+ default:
4668
+ binder_inner_proc_unlock(proc);
4669
+ pr_err("%d:%d: bad work type %d\n",
4670
+ proc->pid, thread->pid, w->type);
4671
+ break;
44124672 }
44134673
44144674 if (!t)
....@@ -4442,10 +4702,39 @@
44424702 trd->sender_pid =
44434703 task_tgid_nr_ns(sender,
44444704 task_active_pid_ns(current));
4705
+ trace_android_vh_sync_txn_recvd(thread->task, t_from->task);
44454706 } else {
44464707 trd->sender_pid = 0;
44474708 }
44484709
4710
+ ret = binder_apply_fd_fixups(proc, t);
4711
+ if (ret) {
4712
+ struct binder_buffer *buffer = t->buffer;
4713
+ bool oneway = !!(t->flags & TF_ONE_WAY);
4714
+ int tid = t->debug_id;
4715
+
4716
+ if (t_from)
4717
+ binder_thread_dec_tmpref(t_from);
4718
+ buffer->transaction = NULL;
4719
+ binder_cleanup_transaction(t, "fd fixups failed",
4720
+ BR_FAILED_REPLY);
4721
+ binder_free_buf(proc, thread, buffer, true);
4722
+ binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
4723
+ "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n",
4724
+ proc->pid, thread->pid,
4725
+ oneway ? "async " :
4726
+ (cmd == BR_REPLY ? "reply " : ""),
4727
+ tid, BR_FAILED_REPLY, ret, __LINE__);
4728
+ if (cmd == BR_REPLY) {
4729
+ cmd = BR_FAILED_REPLY;
4730
+ if (put_user(cmd, (uint32_t __user *)ptr))
4731
+ return -EFAULT;
4732
+ ptr += sizeof(uint32_t);
4733
+ binder_stat_br(proc, thread, cmd);
4734
+ break;
4735
+ }
4736
+ continue;
4737
+ }
44494738 trd->data_size = t->buffer->data_size;
44504739 trd->offsets_size = t->buffer->offsets_size;
44514740 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
....@@ -4654,9 +4943,14 @@
46544943 static void binder_free_proc(struct binder_proc *proc)
46554944 {
46564945 struct binder_device *device;
4946
+ struct binder_proc_ext *eproc =
4947
+ container_of(proc, struct binder_proc_ext, proc);
46574948
46584949 BUG_ON(!list_empty(&proc->todo));
46594950 BUG_ON(!list_empty(&proc->delivered_death));
4951
+ if (proc->outstanding_txns)
4952
+ pr_warn("%s: Unexpected outstanding_txns %d\n",
4953
+ __func__, proc->outstanding_txns);
46604954 device = container_of(proc->context, struct binder_device, context);
46614955 if (refcount_dec_and_test(&device->ref)) {
46624956 kfree(proc->context->name);
....@@ -4664,9 +4958,10 @@
46644958 }
46654959 binder_alloc_deferred_release(&proc->alloc);
46664960 put_task_struct(proc->tsk);
4667
- put_cred(proc->cred);
4961
+ put_cred(eproc->cred);
46684962 binder_stats_deleted(BINDER_STAT_PROC);
4669
- kfree(proc);
4963
+ trace_android_vh_binder_free_proc(proc);
4964
+ kfree(eproc);
46704965 }
46714966
46724967 static void binder_free_thread(struct binder_thread *thread)
....@@ -4705,6 +5000,8 @@
47055000 spin_lock(&t->lock);
47065001 if (t->to_thread == thread)
47075002 send_reply = t;
5003
+ } else {
5004
+ __acquire(&t->lock);
47085005 }
47095006 thread->is_dead = true;
47105007
....@@ -4718,6 +5015,7 @@
47185015 (t->to_thread == thread) ? "in" : "out");
47195016
47205017 if (t->to_thread == thread) {
5018
+ thread->proc->outstanding_txns--;
47215019 t->to_proc = NULL;
47225020 t->to_thread = NULL;
47235021 if (t->buffer) {
....@@ -4733,7 +5031,11 @@
47335031 spin_unlock(&last_t->lock);
47345032 if (t)
47355033 spin_lock(&t->lock);
5034
+ else
5035
+ __acquire(&t->lock);
47365036 }
5037
+ /* annotation for sparse, lock not acquired in last iteration above */
5038
+ __release(&t->lock);
47375039
47385040 /*
47395041 * If this thread used poll, make sure we remove the waitqueue from any
....@@ -4757,6 +5059,7 @@
47575059 if (send_reply)
47585060 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
47595061 binder_release_work(proc, &thread->todo);
5062
+ trace_android_vh_binder_thread_release(proc, thread);
47605063 binder_thread_dec_tmpref(thread);
47615064 return active_transactions;
47625065 }
....@@ -4833,6 +5136,7 @@
48335136 if (!binder_worklist_empty_ilocked(&proc->todo))
48345137 binder_wakeup_proc_ilocked(proc);
48355138 binder_inner_proc_unlock(proc);
5139
+ trace_android_vh_binder_read_done(proc, thread);
48365140 if (ret < 0) {
48375141 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
48385142 ret = -EFAULT;
....@@ -4867,7 +5171,7 @@
48675171 ret = -EBUSY;
48685172 goto out;
48695173 }
4870
- ret = security_binder_set_context_mgr(proc->cred);
5174
+ ret = security_binder_set_context_mgr(binder_get_cred(proc));
48715175 if (ret < 0)
48725176 goto out;
48735177 if (uid_valid(context->binder_context_mgr_uid)) {
....@@ -4957,6 +5261,100 @@
49575261 }
49585262 }
49595263 binder_inner_proc_unlock(proc);
5264
+
5265
+ return 0;
5266
+}
5267
+
5268
+static bool binder_txns_pending_ilocked(struct binder_proc *proc)
5269
+{
5270
+ struct rb_node *n;
5271
+ struct binder_thread *thread;
5272
+
5273
+ if (proc->outstanding_txns > 0)
5274
+ return true;
5275
+
5276
+ for (n = rb_first(&proc->threads); n; n = rb_next(n)) {
5277
+ thread = rb_entry(n, struct binder_thread, rb_node);
5278
+ if (thread->transaction_stack)
5279
+ return true;
5280
+ }
5281
+ return false;
5282
+}
5283
+
5284
+static int binder_ioctl_freeze(struct binder_freeze_info *info,
5285
+ struct binder_proc *target_proc)
5286
+{
5287
+ int ret = 0;
5288
+
5289
+ if (!info->enable) {
5290
+ binder_inner_proc_lock(target_proc);
5291
+ target_proc->sync_recv = false;
5292
+ target_proc->async_recv = false;
5293
+ target_proc->is_frozen = false;
5294
+ binder_inner_proc_unlock(target_proc);
5295
+ return 0;
5296
+ }
5297
+
5298
+ /*
5299
+ * Freezing the target. Prevent new transactions by
5300
+ * setting frozen state. If timeout specified, wait
5301
+ * for transactions to drain.
5302
+ */
5303
+ binder_inner_proc_lock(target_proc);
5304
+ target_proc->sync_recv = false;
5305
+ target_proc->async_recv = false;
5306
+ target_proc->is_frozen = true;
5307
+ binder_inner_proc_unlock(target_proc);
5308
+
5309
+ if (info->timeout_ms > 0)
5310
+ ret = wait_event_interruptible_timeout(
5311
+ target_proc->freeze_wait,
5312
+ (!target_proc->outstanding_txns),
5313
+ msecs_to_jiffies(info->timeout_ms));
5314
+
5315
+ /* Check pending transactions that wait for reply */
5316
+ if (ret >= 0) {
5317
+ binder_inner_proc_lock(target_proc);
5318
+ if (binder_txns_pending_ilocked(target_proc))
5319
+ ret = -EAGAIN;
5320
+ binder_inner_proc_unlock(target_proc);
5321
+ }
5322
+
5323
+ if (ret < 0) {
5324
+ binder_inner_proc_lock(target_proc);
5325
+ target_proc->is_frozen = false;
5326
+ binder_inner_proc_unlock(target_proc);
5327
+ }
5328
+
5329
+ return ret;
5330
+}
5331
+
5332
+static int binder_ioctl_get_freezer_info(
5333
+ struct binder_frozen_status_info *info)
5334
+{
5335
+ struct binder_proc *target_proc;
5336
+ bool found = false;
5337
+ __u32 txns_pending;
5338
+
5339
+ info->sync_recv = 0;
5340
+ info->async_recv = 0;
5341
+
5342
+ mutex_lock(&binder_procs_lock);
5343
+ hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5344
+ if (target_proc->pid == info->pid) {
5345
+ found = true;
5346
+ binder_inner_proc_lock(target_proc);
5347
+ txns_pending = binder_txns_pending_ilocked(target_proc);
5348
+ info->sync_recv |= target_proc->sync_recv |
5349
+ (txns_pending << 1);
5350
+ info->async_recv |= target_proc->async_recv;
5351
+ binder_inner_proc_unlock(target_proc);
5352
+ }
5353
+ }
5354
+ mutex_unlock(&binder_procs_lock);
5355
+
5356
+ if (!found)
5357
+ return -EINVAL;
49605358
49615359 return 0;
49625360 }
....@@ -5079,6 +5477,96 @@
50795477 }
50805478 break;
50815479 }
5480
+ case BINDER_FREEZE: {
5481
+ struct binder_freeze_info info;
5482
+ struct binder_proc **target_procs = NULL, *target_proc;
5483
+ int target_procs_count = 0, i = 0;
5484
+
5485
+ ret = 0;
5486
+
5487
+ if (copy_from_user(&info, ubuf, sizeof(info))) {
5488
+ ret = -EFAULT;
5489
+ goto err;
5490
+ }
5491
+
5492
+ mutex_lock(&binder_procs_lock);
5493
+ hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5494
+ if (target_proc->pid == info.pid)
5495
+ target_procs_count++;
5496
+ }
5497
+
5498
+ if (target_procs_count == 0) {
5499
+ mutex_unlock(&binder_procs_lock);
5500
+ ret = -EINVAL;
5501
+ goto err;
5502
+ }
5503
+
5504
+ target_procs = kcalloc(target_procs_count,
5505
+ sizeof(struct binder_proc *),
5506
+ GFP_KERNEL);
5507
+
5508
+ if (!target_procs) {
5509
+ mutex_unlock(&binder_procs_lock);
5510
+ ret = -ENOMEM;
5511
+ goto err;
5512
+ }
5513
+
5514
+ hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5515
+ if (target_proc->pid != info.pid)
5516
+ continue;
5517
+
5518
+ binder_inner_proc_lock(target_proc);
5519
+ target_proc->tmp_ref++;
5520
+ binder_inner_proc_unlock(target_proc);
5521
+
5522
+ target_procs[i++] = target_proc;
5523
+ }
5524
+ mutex_unlock(&binder_procs_lock);
5525
+
5526
+ for (i = 0; i < target_procs_count; i++) {
5527
+ if (ret >= 0)
5528
+ ret = binder_ioctl_freeze(&info,
5529
+ target_procs[i]);
5530
+
5531
+ binder_proc_dec_tmpref(target_procs[i]);
5532
+ }
5533
+
5534
+ kfree(target_procs);
5535
+
5536
+ if (ret < 0)
5537
+ goto err;
5538
+ break;
5539
+ }
5540
+ case BINDER_GET_FROZEN_INFO: {
5541
+ struct binder_frozen_status_info info;
5542
+
5543
+ if (copy_from_user(&info, ubuf, sizeof(info))) {
5544
+ ret = -EFAULT;
5545
+ goto err;
5546
+ }
5547
+
5548
+ ret = binder_ioctl_get_freezer_info(&info);
5549
+ if (ret < 0)
5550
+ goto err;
5551
+
5552
+ if (copy_to_user(ubuf, &info, sizeof(info))) {
5553
+ ret = -EFAULT;
5554
+ goto err;
5555
+ }
5556
+ break;
5557
+ }
5558
+ case BINDER_ENABLE_ONEWAY_SPAM_DETECTION: {
5559
+ uint32_t enable;
5560
+
5561
+ if (copy_from_user(&enable, ubuf, sizeof(enable))) {
5562
+ ret = -EFAULT;
5563
+ goto err;
5564
+ }
5565
+ binder_inner_proc_lock(proc);
5566
+ proc->oneway_spam_detection_enabled = (bool)enable;
5567
+ binder_inner_proc_unlock(proc);
5568
+ break;
5569
+ }
50825570 default:
50835571 ret = -EINVAL;
50845572 goto err;
....@@ -5088,7 +5576,7 @@
50885576 if (thread)
50895577 thread->looper_need_return = false;
50905578 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5091
- if (ret && ret != -ERESTARTSYS)
5579
+ if (ret && ret != -EINTR)
50925580 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
50935581 err_unlocked:
50945582 trace_binder_ioctl_done(ret);
....@@ -5116,7 +5604,6 @@
51165604 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
51175605 (unsigned long)pgprot_val(vma->vm_page_prot));
51185606 binder_alloc_vma_close(&proc->alloc);
5119
- binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
51205607 }
51215608
51225609 static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
....@@ -5132,15 +5619,10 @@
51325619
51335620 static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
51345621 {
5135
- int ret;
51365622 struct binder_proc *proc = filp->private_data;
5137
- const char *failure_string;
51385623
51395624 if (proc->tsk != current->group_leader)
51405625 return -EINVAL;
5141
-
5142
- if ((vma->vm_end - vma->vm_start) > SZ_4M)
5143
- vma->vm_end = vma->vm_start + SZ_4M;
51445626
51455627 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
51465628 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
....@@ -5149,9 +5631,9 @@
51495631 (unsigned long)pgprot_val(vma->vm_page_prot));
51505632
51515633 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5152
- ret = -EPERM;
5153
- failure_string = "bad vm_flags";
5154
- goto err_bad_arg;
5634
+ pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
5635
+ proc->pid, vma->vm_start, vma->vm_end, "bad vm_flags", -EPERM);
5636
+ return -EPERM;
51555637 }
51565638 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
51575639 vma->vm_flags &= ~VM_MAYWRITE;
....@@ -5159,40 +5641,32 @@
51595641 vma->vm_ops = &binder_vm_ops;
51605642 vma->vm_private_data = proc;
51615643
5162
- ret = binder_alloc_mmap_handler(&proc->alloc, vma);
5163
- if (ret)
5164
- return ret;
5165
- mutex_lock(&proc->files_lock);
5166
- proc->files = get_files_struct(current);
5167
- mutex_unlock(&proc->files_lock);
5168
- return 0;
5169
-
5170
-err_bad_arg:
5171
- pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
5172
- proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5173
- return ret;
5644
+ return binder_alloc_mmap_handler(&proc->alloc, vma);
51745645 }
51755646
51765647 static int binder_open(struct inode *nodp, struct file *filp)
51775648 {
5178
- struct binder_proc *proc;
5649
+ struct binder_proc *proc, *itr;
5650
+ struct binder_proc_ext *eproc;
51795651 struct binder_device *binder_dev;
51805652 struct binderfs_info *info;
51815653 struct dentry *binder_binderfs_dir_entry_proc = NULL;
5654
+ bool existing_pid = false;
51825655
51835656 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
51845657 current->group_leader->pid, current->pid);
51855658
5186
- proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5659
+ eproc = kzalloc(sizeof(*eproc), GFP_KERNEL);
5660
+ proc = &eproc->proc;
51875661 if (proc == NULL)
51885662 return -ENOMEM;
51895663 spin_lock_init(&proc->inner_lock);
51905664 spin_lock_init(&proc->outer_lock);
51915665 get_task_struct(current->group_leader);
51925666 proc->tsk = current->group_leader;
5193
- mutex_init(&proc->files_lock);
5194
- proc->cred = get_cred(filp->f_cred);
5667
+ eproc->cred = get_cred(filp->f_cred);
51955668 INIT_LIST_HEAD(&proc->todo);
5669
+ init_waitqueue_head(&proc->freeze_wait);
51965670 if (binder_supported_policy(current->policy)) {
51975671 proc->default_priority.sched_policy = current->policy;
51985672 proc->default_priority.prio = current->normal_prio;
....@@ -5221,19 +5695,24 @@
52215695 filp->private_data = proc;
52225696
52235697 mutex_lock(&binder_procs_lock);
5698
+ hlist_for_each_entry(itr, &binder_procs, proc_node) {
5699
+ if (itr->pid == proc->pid) {
5700
+ existing_pid = true;
5701
+ break;
5702
+ }
5703
+ }
52245704 hlist_add_head(&proc->proc_node, &binder_procs);
52255705 mutex_unlock(&binder_procs_lock);
5226
-
5227
- if (binder_debugfs_dir_entry_proc) {
5706
+ trace_android_vh_binder_preset(&binder_procs, &binder_procs_lock);
5707
+ if (binder_debugfs_dir_entry_proc && !existing_pid) {
52285708 char strbuf[11];
52295709
52305710 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
52315711 /*
5232
- * proc debug entries are shared between contexts, so
5233
- * this will fail if the process tries to open the driver
5234
- * again with a different context. The priting code will
5235
- * anyway print all contexts that a given PID has, so this
5236
- * is not a problem.
5712
+ * proc debug entries are shared between contexts.
5713
+ * Only create for the first PID to avoid debugfs log spamming
5714
+ * The printing code will anyway print all contexts for a given
5715
+ * PID so this is not a problem.
52375716 */
52385717 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
52395718 binder_debugfs_dir_entry_proc,
....@@ -5241,19 +5720,16 @@
52415720 &proc_fops);
52425721 }
52435722
5244
- if (binder_binderfs_dir_entry_proc) {
5723
+ if (binder_binderfs_dir_entry_proc && !existing_pid) {
52455724 char strbuf[11];
52465725 struct dentry *binderfs_entry;
52475726
52485727 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
52495728 /*
52505729 * Similar to debugfs, the process specific log file is shared
5251
- * between contexts. If the file has already been created for a
5252
- * process, the following binderfs_create_file() call will
5253
- * fail with error code EEXIST if another context of the same
5254
- * process invoked binder_open(). This is ok since same as
5255
- * debugfs, the log file will contain information on all
5256
- * contexts of a given PID.
5730
+ * between contexts. Only create for the first PID.
5731
+ * This is ok since same as debugfs, the log file will contain
5732
+ * information on all contexts of a given PID.
52575733 */
52585734 binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
52595735 strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
....@@ -5263,10 +5739,8 @@
52635739 int error;
52645740
52655741 error = PTR_ERR(binderfs_entry);
5266
- if (error != -EEXIST) {
5267
- pr_warn("Unable to create file %s in binderfs (error %d)\n",
5268
- strbuf, error);
5269
- }
5742
+ pr_warn("Unable to create file %s in binderfs (error %d)\n",
5743
+ strbuf, error);
52705744 }
52715745 }
52725746
....@@ -5391,8 +5865,6 @@
53915865 struct rb_node *n;
53925866 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
53935867
5394
- BUG_ON(proc->files);
5395
-
53965868 mutex_lock(&binder_procs_lock);
53975869 hlist_del(&proc->proc_node);
53985870 mutex_unlock(&binder_procs_lock);
....@@ -5414,6 +5886,9 @@
54145886 proc->tmp_ref++;
54155887
54165888 proc->is_dead = true;
5889
+ proc->is_frozen = false;
5890
+ proc->sync_recv = false;
5891
+ proc->async_recv = false;
54175892 threads = 0;
54185893 active_transactions = 0;
54195894 while ((n = rb_first(&proc->threads))) {
....@@ -5474,7 +5949,6 @@
54745949 static void binder_deferred_func(struct work_struct *work)
54755950 {
54765951 struct binder_proc *proc;
5477
- struct files_struct *files;
54785952
54795953 int defer;
54805954
....@@ -5492,23 +5966,11 @@
54925966 }
54935967 mutex_unlock(&binder_deferred_lock);
54945968
5495
- files = NULL;
5496
- if (defer & BINDER_DEFERRED_PUT_FILES) {
5497
- mutex_lock(&proc->files_lock);
5498
- files = proc->files;
5499
- if (files)
5500
- proc->files = NULL;
5501
- mutex_unlock(&proc->files_lock);
5502
- }
5503
-
55045969 if (defer & BINDER_DEFERRED_FLUSH)
55055970 binder_deferred_flush(proc);
55065971
55075972 if (defer & BINDER_DEFERRED_RELEASE)
55085973 binder_deferred_release(proc); /* frees proc */
5509
-
5510
- if (files)
5511
- put_files_struct(files);
55125974 } while (proc);
55135975 }
55145976 static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
....@@ -5535,6 +5997,7 @@
55355997 struct binder_buffer *buffer = t->buffer;
55365998
55375999 spin_lock(&t->lock);
6000
+ trace_android_vh_binder_print_transaction_info(m, proc, prefix, t);
55386001 to_proc = t->to_proc;
55396002 seq_printf(m,
55406003 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
....@@ -5779,7 +6242,9 @@
57796242 "BR_FINISHED",
57806243 "BR_DEAD_BINDER",
57816244 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5782
- "BR_FAILED_REPLY"
6245
+ "BR_FAILED_REPLY",
6246
+ "BR_FROZEN_REPLY",
6247
+ "BR_ONEWAY_SPAM_SUSPECT",
57836248 };
57846249
57856250 static const char * const binder_command_strings[] = {
....@@ -5920,8 +6385,7 @@
59206385 print_binder_stats(m, " ", &proc->stats);
59216386 }
59226387
5923
-
5924
-int binder_state_show(struct seq_file *m, void *unused)
6388
+static int state_show(struct seq_file *m, void *unused)
59256389 {
59266390 struct binder_proc *proc;
59276391 struct binder_node *node;
....@@ -5960,7 +6424,7 @@
59606424 return 0;
59616425 }
59626426
5963
-int binder_stats_show(struct seq_file *m, void *unused)
6427
+static int stats_show(struct seq_file *m, void *unused)
59646428 {
59656429 struct binder_proc *proc;
59666430
....@@ -5976,7 +6440,7 @@
59766440 return 0;
59776441 }
59786442
5979
-int binder_transactions_show(struct seq_file *m, void *unused)
6443
+static int transactions_show(struct seq_file *m, void *unused)
59806444 {
59816445 struct binder_proc *proc;
59826446
....@@ -6032,7 +6496,7 @@
60326496 "\n" : " (incomplete)\n");
60336497 }
60346498
6035
-int binder_transaction_log_show(struct seq_file *m, void *unused)
6499
+static int transaction_log_show(struct seq_file *m, void *unused)
60366500 {
60376501 struct binder_transaction_log *log = m->private;
60386502 unsigned int log_cur = atomic_read(&log->cur);
....@@ -6057,11 +6521,50 @@
60576521 .owner = THIS_MODULE,
60586522 .poll = binder_poll,
60596523 .unlocked_ioctl = binder_ioctl,
6060
- .compat_ioctl = binder_ioctl,
6524
+ .compat_ioctl = compat_ptr_ioctl,
60616525 .mmap = binder_mmap,
60626526 .open = binder_open,
60636527 .flush = binder_flush,
60646528 .release = binder_release,
6529
+};
6530
+
6531
+DEFINE_SHOW_ATTRIBUTE(state);
6532
+DEFINE_SHOW_ATTRIBUTE(stats);
6533
+DEFINE_SHOW_ATTRIBUTE(transactions);
6534
+DEFINE_SHOW_ATTRIBUTE(transaction_log);
6535
+
6536
+const struct binder_debugfs_entry binder_debugfs_entries[] = {
6537
+ {
6538
+ .name = "state",
6539
+ .mode = 0444,
6540
+ .fops = &state_fops,
6541
+ .data = NULL,
6542
+ },
6543
+ {
6544
+ .name = "stats",
6545
+ .mode = 0444,
6546
+ .fops = &stats_fops,
6547
+ .data = NULL,
6548
+ },
6549
+ {
6550
+ .name = "transactions",
6551
+ .mode = 0444,
6552
+ .fops = &transactions_fops,
6553
+ .data = NULL,
6554
+ },
6555
+ {
6556
+ .name = "transaction_log",
6557
+ .mode = 0444,
6558
+ .fops = &transaction_log_fops,
6559
+ .data = &binder_transaction_log,
6560
+ },
6561
+ {
6562
+ .name = "failed_transaction_log",
6563
+ .mode = 0444,
6564
+ .fops = &transaction_log_fops,
6565
+ .data = &binder_transaction_log_failed,
6566
+ },
6567
+ {} /* terminator */
60656568 };
60666569
60676570 static int __init init_binder_device(const char *name)
....@@ -6109,36 +6612,18 @@
61096612 atomic_set(&binder_transaction_log_failed.cur, ~0U);
61106613
61116614 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6112
- if (binder_debugfs_dir_entry_root)
6615
+ if (binder_debugfs_dir_entry_root) {
6616
+ const struct binder_debugfs_entry *db_entry;
6617
+
6618
+ binder_for_each_debugfs_entry(db_entry)
6619
+ debugfs_create_file(db_entry->name,
6620
+ db_entry->mode,
6621
+ binder_debugfs_dir_entry_root,
6622
+ db_entry->data,
6623
+ db_entry->fops);
6624
+
61136625 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
61146626 binder_debugfs_dir_entry_root);
6115
-
6116
- if (binder_debugfs_dir_entry_root) {
6117
- debugfs_create_file("state",
6118
- 0444,
6119
- binder_debugfs_dir_entry_root,
6120
- NULL,
6121
- &binder_state_fops);
6122
- debugfs_create_file("stats",
6123
- 0444,
6124
- binder_debugfs_dir_entry_root,
6125
- NULL,
6126
- &binder_stats_fops);
6127
- debugfs_create_file("transactions",
6128
- 0444,
6129
- binder_debugfs_dir_entry_root,
6130
- NULL,
6131
- &binder_transactions_fops);
6132
- debugfs_create_file("transaction_log",
6133
- 0444,
6134
- binder_debugfs_dir_entry_root,
6135
- &binder_transaction_log,
6136
- &binder_transaction_log_fops);
6137
- debugfs_create_file("failed_transaction_log",
6138
- 0444,
6139
- binder_debugfs_dir_entry_root,
6140
- &binder_transaction_log_failed,
6141
- &binder_transaction_log_fops);
61426627 }
61436628
61446629 if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
....@@ -6186,5 +6671,6 @@
61866671
61876672 #define CREATE_TRACE_POINTS
61886673 #include "binder_trace.h"
6674
+EXPORT_TRACEPOINT_SYMBOL_GPL(binder_transaction_received);
61896675
61906676 MODULE_LICENSE("GPL v2");