-
Notifications
You must be signed in to change notification settings - Fork 20
/
binder-android-kernel-version-0.3.patch
3533 lines (3463 loc) · 89 KB
/
binder-android-kernel-version-0.3.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From a56f2649bc1af31a9703bb124555b85047b8d170 Mon Sep 17 00:00:00 2001
From: Rong Shen <[email protected]>
Date: Mon, 27 Feb 2012 23:31:40 +1100
Subject: [PATCH 1/4] Makefile change to use the new binder driver.
---
drivers/staging/android/Makefile | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/drivers/staging/android/Makefile b/drivers/staging/android/Makefile
index 8e057e6..12fe3f7 100644
--- a/drivers/staging/android/Makefile
+++ b/drivers/staging/android/Makefile
@@ -1,4 +1,4 @@
-obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o
+obj-$(CONFIG_ANDROID_BINDER_IPC) += msg_queue.o binder_new.o
obj-$(CONFIG_ANDROID_LOGGER) += logger.o
obj-$(CONFIG_ANDROID_RAM_CONSOLE) += ram_console.o
obj-$(CONFIG_ANDROID_TIMED_OUTPUT) += timed_output.o
--
1.7.1
From ab3d910849aafa5289488b792455b4c9134d69db Mon Sep 17 00:00:00 2001
From: Rong Shen <[email protected]>
Date: Mon, 27 Feb 2012 23:39:22 +1100
Subject: [PATCH 2/4] added new binder driver
---
drivers/staging/android/binder_new.c | 2484 ++++++++++++++++++++++++++++++++++
drivers/staging/android/fast_slob.h | 166 +++
drivers/staging/android/msg_queue.c | 253 ++++
drivers/staging/android/msg_queue.h | 133 ++
4 files changed, 3036 insertions(+), 0 deletions(-)
create mode 100644 drivers/staging/android/binder_new.c
create mode 100644 drivers/staging/android/fast_slob.h
create mode 100644 drivers/staging/android/msg_queue.c
create mode 100644 drivers/staging/android/msg_queue.h
diff --git a/drivers/staging/android/binder_new.c b/drivers/staging/android/binder_new.c
new file mode 100644
index 0000000..3a3bd7a
--- /dev/null
+++ b/drivers/staging/android/binder_new.c
@@ -0,0 +1,2484 @@
+/*
+ * Android Binder IPC driver
+ * Copyright (c) 2012 Rong Shen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/compiler.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/fs.h>
+#include <linux/mm.h>
+#include <linux/file.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/rbtree.h>
+#include <linux/poll.h>
+#include <linux/spinlock.h>
+#include <linux/uaccess.h>
+#include <linux/miscdevice.h>
+#include <linux/seq_file.h>
+#include <linux/debugfs.h>
+
+#include <asm/atomic.h>
+
+#include "msg_queue.h"
+#include "fast_slob.h"
+#include "binder.h"
+
+
+#define OBJ_HASH_BUCKET_SIZE 128
+#define MAX_TRACE_DEPTH 4
+#define MSG_BUF_ALIGN(n) (((n) & (sizeof(void *) - 1)) ? ALIGN((n), sizeof(void *)) : (n))
+#define OBJ_IS_BINDER(o) ((o)->owner_queue)
+#define OBJ_IS_HANDLE(o) (!OBJ_IS_BINDER(o))
+#define DUMP_MSG(pid, tid, wrt, msg) //_dump_msg(pid, tid, wrt, msg)
+
+
+enum { // compat: review looper idea
+ BINDER_LOOPER_STATE_INVALID = 0x00,
+ BINDER_LOOPER_STATE_REGISTERED = 0x01,
+ BINDER_LOOPER_STATE_ENTERED = 0x02,
+ BINDER_LOOPER_STATE_READY = 0x03 // compat
+};
+
+typedef enum {
+ BINDER_EVT_OBJ_DEAD = 1,
+} binder_event_t;
+
+
+struct binder_proc {
+ spinlock_t lock;
+ struct rb_root thread_tree;
+
+ spinlock_t obj_lock;
+ struct rb_root obj_tree;
+ struct hlist_head obj_hash[OBJ_HASH_BUCKET_SIZE];
+ unsigned long obj_seq;
+
+ struct msg_queue *queue;
+
+ struct fast_slob *slob;
+ int slob_uses;
+ unsigned long ustart;
+
+ pid_t pid;
+
+ atomic_t proc_loopers, requested_loopers, registered_loopers;
+ int max_threads;
+
+ struct dentry *proc_dir, *thread_dir, *obj_dir;
+ struct list_head garbage_list; // garbage collected when proc is released
+};
+
+struct binder_thread {
+ pid_t pid;
+
+ struct rb_node rb_node;
+ struct msg_queue *queue;
+
+ int state;
+ int non_block;
+ unsigned int last_error;
+
+ int pending_replies;
+ struct list_head incoming_transactions;
+
+ struct binder_proc *proc;
+ struct dentry *info_node;
+};
+
+struct binder_notifier {
+ struct list_head list;
+ int event;
+ void *cookie;
+ msg_queue_id to_notify;
+};
+
+struct binder_obj {
+ msg_queue_id owner;
+ struct msg_queue *owner_queue;
+
+ void *binder;
+ void *cookie;
+
+ struct rb_node rb_node;
+
+ unsigned long ref;
+ struct hlist_node hash_node;
+
+ atomic_t refs;
+
+ spinlock_t lock; // used for notifiers only
+ struct list_head notifiers;
+
+ struct dentry *info_node;
+};
+
+#define bcmd_transaction_data binder_transaction_data
+
+struct bcmd_notifier_data {
+ long handle;
+ void *cookie;
+};
+
+struct bcmd_ref_return {
+ uint32_t cmd;
+ void *binder;
+ void *cookie;
+};
+
+struct bcmd_msg_buf {
+ uint8_t *data;
+ uint8_t *offsets;
+
+ size_t data_size;
+ size_t offsets_size;
+ size_t buf_size;
+
+ msg_queue_id owners[0]; // owners of objects in this buffer
+};
+
+struct bcmd_msg_trace {
+ msg_queue_id caller_proc;
+ msg_queue_id caller_thread;
+};
+
+struct bcmd_msg {
+ struct list_head list;
+
+ void *binder;
+ void *cookie;
+
+ unsigned int type; // compat: review all data types in/out of the ioctl
+ unsigned int code;
+ unsigned int flags;
+
+ struct bcmd_msg_buf *buf;
+
+ pid_t sender_pid;
+ uid_t sender_euid;
+
+ msg_queue_id reply_to;
+
+ int trace_depth;
+ struct bcmd_msg_trace traces[MAX_TRACE_DEPTH];
+};
+
+struct slob_buf {
+ unsigned long uaddr_data;
+ unsigned long uaddr_offsets;
+ size_t data_size;
+ size_t offsets_size;
+ char data[0];
+};
+
+struct debugfs_priv {
+ msg_queue_id owner;
+ struct binder_proc *proc;
+ unsigned long data;
+ struct list_head list;
+};
+
+
+static struct binder_obj *context_mgr_obj; // compat: is context mgr necessary?
+static uid_t context_mgr_uid = -1;
+
+static struct dentry *debugfs_root;
+
+
+static struct debugfs_priv *debugfs_new_proc(struct binder_proc *proc);
+static struct debugfs_priv *debugfs_new_thread(struct binder_proc *proc, struct binder_thread *thread);
+static struct debugfs_priv *debugfs_new_obj(struct binder_proc *proc, struct binder_obj *obj);
+
+
+// TODO: to be deleted
+void _hexdump(const void *buf, unsigned long size)
+{
+ int col = 0, off = 0, n = 0;
+ unsigned char *p = (unsigned char *)buf;
+ char cbuf[64];
+
+ if (size > 256)
+ size = 256;
+ while (size--) {
+ if (!col)
+ printk("\t%08x:", off);
+
+ printk(" %02x", *p);
+ cbuf[n++] = (*p >= 0x20 && *p < 0x7f) ? (char)*p : ' ';
+ cbuf[n++] = ' ';
+
+ p++;
+ off++;
+ col++;
+
+ if (!(col % 16)) {
+ cbuf[n] = '\0';
+ printk(" %s\n", cbuf);
+ n = 0;
+ col = 0;
+ } else if (!(col % 4))
+ printk(" ");
+ }
+
+ if (col % 16) {
+ while (col++ < 16)
+ printk(" ");
+ cbuf[n] = '\0';
+ printk(" %s\n", cbuf);
+ } else
+ printk("\n");
+}
+
+void _dump_msg(int pid, int tid, int write, struct bcmd_msg *msg)
+{
+ printk("proc %d (tid %d) %s %s message:\n", pid, tid, write ? "write" : "read", (msg->type == BC_REPLY) ? "reply" : "transaction");
+ printk("\tbinder %p, cookie %p, type %u, code %u, flags %u, uid %d, pid %d, queue %ld\n",
+ msg->binder, msg->cookie, msg->type, msg->code, msg->flags, msg->sender_pid, msg->sender_euid, msg->reply_to);
+
+ if (msg->buf) {
+ struct bcmd_msg_buf *mbuf = msg->buf;
+
+ if (mbuf->data_size > 0) {
+ printk("\t data size %u\n", mbuf->data_size);
+ _hexdump(mbuf->data, mbuf->data_size);
+
+ if (mbuf->offsets_size > 0) {
+ printk("\t offsets size %u\n", mbuf->offsets_size);
+ _hexdump(mbuf->offsets, mbuf->offsets_size);
+ }
+ }
+ }
+}
+
+static inline int binder_cmp(msg_queue_id owner0, void *binder0, msg_queue_id owner1, void *binder1)
+{
+ ssize_t sign;
+
+ if ((sign = owner0 - owner1))
+ return (sign > 0) ? 1 : -1;
+
+ if ((sign = binder0 - binder1))
+ return (sign > 0) ? 1 : -1;
+
+ return 0;
+}
+
+static struct binder_obj *binder_find_obj(struct binder_proc *proc, msg_queue_id owner, void *binder)
+{
+ struct rb_node **p = &proc->obj_tree.rb_node;
+ struct rb_node *parent = NULL;
+ struct binder_obj *obj;
+ int r;
+
+ spin_lock(&proc->obj_lock);
+ while (*p) {
+ parent = *p;
+ obj = rb_entry(parent, struct binder_obj, rb_node);
+
+ r = binder_cmp(owner, binder, obj->owner, obj->binder);
+ if (r < 0)
+ p = &(*p)->rb_left;
+ else if (r > 0)
+ p = &(*p)->rb_right;
+ else {
+ spin_unlock(&proc->obj_lock);
+ return obj;
+ }
+ }
+ spin_unlock(&proc->obj_lock);
+
+ return NULL;
+}
+
+static inline struct binder_obj *binder_find_my_obj(struct binder_proc *proc, void *binder)
+{
+ return binder_find_obj(proc, msg_queue_id(proc->queue), binder);
+}
+
+static struct binder_obj *binder_find_obj_by_ref(struct binder_proc *proc, unsigned long ref)
+{
+ struct binder_obj *obj;
+ struct hlist_head *head;
+ struct hlist_node *node;
+
+ spin_lock(&proc->obj_lock);
+
+ head = &proc->obj_hash[ref % OBJ_HASH_BUCKET_SIZE];
+ hlist_for_each_entry(obj, node, head, hash_node) {
+ if (obj->ref == ref) {
+ spin_unlock(&proc->obj_lock);
+ return obj;
+ }
+ }
+
+ spin_unlock(&proc->obj_lock);
+ return NULL;
+}
+
+static struct binder_obj *_binder_new_obj(struct binder_proc *proc, msg_queue_id owner, struct msg_queue *owner_queue, void *binder, void *cookie)
+{
+ struct rb_node **p = &proc->obj_tree.rb_node;
+ struct rb_node *parent = NULL;
+ struct binder_obj *obj, *new_obj;
+ int r;
+ struct debugfs_priv *priv;
+
+ new_obj = kmalloc(sizeof(*obj), GFP_KERNEL);
+ if (!new_obj)
+ return NULL;
+
+ new_obj->owner = owner;
+ new_obj->owner_queue = owner_queue;
+ new_obj->binder = binder;
+ new_obj->cookie = cookie;
+ spin_lock_init(&new_obj->lock);
+ INIT_LIST_HEAD(&new_obj->notifiers);
+
+ atomic_set(&new_obj->refs, 0);
+
+ spin_lock(&proc->obj_lock);
+ while (*p) {
+ parent = *p;
+ obj = rb_entry(parent, struct binder_obj, rb_node);
+
+ r = binder_cmp(owner, binder, obj->owner, obj->binder);
+ if (r < 0)
+ p = &(*p)->rb_left;
+ else if (r > 0)
+ p = &(*p)->rb_right;
+ else { // other thread has created an object before we do
+ spin_unlock(&proc->obj_lock);
+ kfree(new_obj);
+ return obj;
+ }
+ }
+
+ rb_link_node(&new_obj->rb_node, parent, p);
+ rb_insert_color(&new_obj->rb_node, &proc->obj_tree);
+
+ new_obj->ref = proc->obj_seq++;
+ hlist_add_head(&new_obj->hash_node, &proc->obj_hash[new_obj->ref % OBJ_HASH_BUCKET_SIZE]);
+
+ spin_unlock(&proc->obj_lock);
+
+ if (!(priv = debugfs_new_obj(proc, new_obj))) {
+ spin_lock(&proc->obj_lock);
+ rb_erase(&new_obj->rb_node, &proc->obj_tree);
+ hlist_del(&new_obj->hash_node);
+ spin_unlock(&proc->obj_lock);
+ kfree(new_obj);
+ return NULL;
+ }
+ spin_lock(&proc->lock);
+ list_add(&priv->list, &proc->garbage_list);
+ spin_unlock(&proc->lock);
+
+ return new_obj;
+}
+
+static inline struct binder_obj *binder_new_obj(struct binder_proc *proc, void *binder, void *cookie)
+{
+ return _binder_new_obj(proc, msg_queue_id(proc->queue), proc->queue, binder, cookie);
+}
+
+// used by the queue owner
+inline int _bcmd_read_msg(struct msg_queue *q, struct bcmd_msg **pmsg)
+{
+ struct list_head *list = &(*pmsg)->list;
+ int r;
+
+ r = read_msg_queue(q, &list);
+ if (!r)
+ *pmsg = container_of(list, struct bcmd_msg, list);
+ return r;
+}
+
+// used by any process
+inline int bcmd_read_msg(msg_queue_id id, struct bcmd_msg **pmsg)
+{
+ struct msg_queue *q;
+ int r;
+
+ if (!(q = get_msg_queue(id)))
+ return -ENODEV;
+
+ r = _bcmd_read_msg(q, pmsg);
+
+ put_msg_queue(q);
+ return r;
+}
+
+// used by the queue owner
+inline int _bcmd_write_msg(struct msg_queue *q, struct bcmd_msg *msg)
+{
+ return write_msg_queue(q, &msg->list);
+}
+
+// used by any process
+inline int bcmd_write_msg(msg_queue_id id, struct bcmd_msg *msg)
+{
+ struct msg_queue *q;
+ int r;
+
+ if (!(q = get_msg_queue(id)))
+ return -ENODEV;
+
+ r = _bcmd_write_msg(q, msg);
+
+ put_msg_queue(q);
+ return r;
+}
+
+// used by the queue owner
+inline int _bcmd_write_msg_head(struct msg_queue *q, struct bcmd_msg *msg)
+{
+ return write_msg_queue_head(q, &msg->list);
+}
+
+// used by any process
+inline int bcmd_write_msg_head(msg_queue_id id, struct bcmd_msg *msg)
+{
+ struct msg_queue *q;
+ int r;
+
+ if (!(q = get_msg_queue(id)))
+ return -ENODEV;
+
+ r = _bcmd_write_msg_head(q, msg);
+
+ put_msg_queue(q);
+ return r;
+}
+
+static struct bcmd_msg *binder_alloc_msg(size_t data_size, size_t offsets_size)
+{
+ size_t num_objs, msg_size, msg_buf_size, buf_size;
+ struct bcmd_msg *msg;
+ struct bcmd_msg_buf *mbuf;
+
+ num_objs = offsets_size / sizeof(size_t);
+ msg_buf_size = sizeof(*mbuf) + num_objs * sizeof(msg_queue_id);
+ msg_size = sizeof(*msg) + msg_buf_size;
+ buf_size = msg_size + MSG_BUF_ALIGN(data_size) + MSG_BUF_ALIGN(offsets_size);
+
+ msg = kmalloc(buf_size, GFP_KERNEL);
+ if (!msg)
+ return NULL;
+
+ mbuf = (struct bcmd_msg_buf *)((char *)msg + sizeof(*msg));
+ mbuf->data = (char *)msg + msg_size;
+ mbuf->offsets = (char *)mbuf->data + MSG_BUF_ALIGN(data_size);
+
+ mbuf->data_size = data_size;
+ mbuf->offsets_size = offsets_size;
+ mbuf->buf_size = buf_size;
+
+ msg->buf = mbuf;
+ msg->trace_depth = 0;
+ return msg;
+}
+
+static struct bcmd_msg *binder_realloc_msg(struct bcmd_msg *msg, size_t data_size, size_t offsets_size)
+{
+ size_t num_objs, msg_size, msg_buf_size, buf_size;
+ struct bcmd_msg_buf *mbuf = msg->buf;
+
+ num_objs = offsets_size / sizeof(size_t);
+ msg_buf_size = sizeof(*mbuf) + num_objs * sizeof(msg_queue_id);
+ msg_size = sizeof(*msg) + msg_buf_size;
+ buf_size = msg_size + MSG_BUF_ALIGN(data_size) + MSG_BUF_ALIGN(offsets_size);
+
+ if (mbuf->buf_size >= buf_size) {
+ mbuf->data = (char *)msg + msg_size;
+ mbuf->offsets = (char *)mbuf->data + MSG_BUF_ALIGN(data_size);
+
+ mbuf->data_size = data_size;
+ mbuf->offsets_size = offsets_size;
+
+ msg->trace_depth = 0;
+ return msg;
+ }
+
+ kfree(msg);
+ return binder_alloc_msg(data_size, offsets_size);
+}
+
+static inline int binder_write_cmd(msg_queue_id q, void *binder, void *cookie, unsigned int cmd)
+{
+ struct bcmd_msg *msg;
+ int r;
+
+ msg = binder_alloc_msg(0, 0);
+ if (!msg)
+ return -ENOMEM;
+
+ msg->type = cmd;
+ msg->binder = binder;
+ msg->cookie = cookie;
+
+ r = bcmd_write_msg(q, msg);
+ if (r < 0) {
+ kfree(msg);
+ return r;
+ }
+
+ return 0;
+}
+
+static inline int binder_inform_owner(struct binder_obj *obj, unsigned int cmd)
+{
+ return binder_write_cmd(obj->owner, obj->binder, obj->cookie, cmd);
+}
+
+static int _binder_free_obj(struct binder_proc *proc, struct binder_obj *obj)
+{
+ debugfs_remove(obj->info_node);
+
+ if (OBJ_IS_BINDER(obj)) {
+ struct binder_notifier *notifier, *next;
+ struct bcmd_msg *msg = NULL;
+
+ list_for_each_entry_safe(notifier, next, &obj->notifiers, list) {
+ list_del(¬ifier->list);
+
+ if (!msg) {
+ msg = binder_alloc_msg(0, 0);
+ if (!msg) {
+ kfree(obj);
+ return -ENOMEM;
+ }
+ }
+
+ msg->type = BR_DEAD_BINDER;
+ msg->binder = obj->binder;
+ msg->cookie = notifier->cookie;
+ msg->reply_to = obj->owner; // identify the owner
+ if (!bcmd_write_msg(notifier->to_notify, msg))
+ msg = NULL;
+
+ kfree(notifier);
+ }
+ if (msg)
+ kfree(msg);
+ } else {
+ // reference - tell the owner we are no longer referencing the object
+ if (atomic_read(&obj->refs) > 0)
+ binder_inform_owner(obj, BC_RELEASE);
+ }
+
+ kfree(obj);
+ return 0;
+}
+
+static int binder_free_obj(struct binder_proc *proc, struct binder_obj *obj, int force)
+{
+ spin_lock(&proc->obj_lock);
+ rb_erase(&obj->rb_node, &proc->obj_tree);
+ hlist_del(&obj->hash_node);
+ spin_unlock(&proc->obj_lock);
+
+ if (!force)
+ return _binder_free_obj(proc, obj);
+
+ debugfs_remove(obj->info_node);
+ kfree(obj);
+ return 0;
+}
+
+static int drain_msg_buf(struct binder_proc *proc, struct bcmd_msg *msg)
+{
+ struct bcmd_msg_buf *mbuf = msg->buf;
+
+ if (mbuf->offsets_size > 0) {
+ struct flat_binder_object *bp;
+ size_t *p, *ep, off;
+ int n;
+
+ p = (size_t *)mbuf->offsets;
+ ep = (size_t *)((char *)mbuf->offsets + mbuf->offsets_size);
+ n = 0;
+
+ while (p < ep) {
+ off = *p++;
+ if (off + sizeof(*bp) > mbuf->data_size)
+ return -EINVAL;
+
+ bp = (struct flat_binder_object *)(mbuf->data + off);
+ switch (bp->type) {
+ case BINDER_TYPE_BINDER:
+ case BINDER_TYPE_HANDLE:
+ if (mbuf->owners[n] != msg_queue_id(proc->queue))
+ binder_write_cmd(mbuf->owners[n], bp->binder, bp->cookie, BC_RELEASE);
+ break;
+
+ case BINDER_TYPE_FD:
+ if (bp->binder)
+ fput(bp->binder);
+ break;
+
+ default:
+ break;
+ }
+
+ n++;
+ }
+ }
+
+ return 0;
+}
+
+static void drain_msg_queue(struct binder_proc *proc, struct msg_queue *q)
+{
+ struct list_head *entry;
+ struct bcmd_msg *msg;
+
+ while ((entry = msg_queue_pop(q))) {
+ msg = container_of(entry, struct bcmd_msg, list);
+
+ if (msg->type == BC_TRANSACTION) {
+ drain_msg_buf(proc, msg);
+
+ if (!(msg->flags & TF_ONE_WAY)) {
+ msg->type = BR_DEAD_REPLY;
+ if (!bcmd_write_msg(msg->reply_to, msg))
+ continue;
+ }
+ } else if (msg->type == BC_REPLY) {
+ drain_msg_buf(proc, msg);
+ } else if (msg->type == BC_CLEAR_DEATH_NOTIFICATION) {
+ struct binder_obj *obj;
+
+ obj = binder_find_my_obj(proc, msg->binder);
+ if (obj) {
+ struct binder_notifier *notifier, *next;
+
+ spin_lock(&obj->lock);
+ list_for_each_entry_safe(notifier, next, &obj->notifiers, list) {
+ if (notifier->event == BINDER_EVT_OBJ_DEAD &&
+ notifier->cookie == msg->cookie) {
+ list_del(¬ifier->list);
+ kfree(notifier);
+ break;
+ }
+ }
+ spin_unlock(&obj->lock);
+ }
+ }
+
+ kfree(msg);
+ }
+}
+
+static void thread_queue_release(struct msg_queue *q, void *data)
+{
+ struct binder_thread *thread = data;
+ struct binder_proc *proc = thread->proc;
+ struct bcmd_msg *msg, *next;
+
+ drain_msg_queue(proc, q);
+
+ list_for_each_entry_safe(msg, next, &thread->incoming_transactions, list) {
+ list_del(&msg->list);
+
+ msg->type = BR_DEAD_REPLY;
+ if (bcmd_write_msg(msg->reply_to, msg) < 0)
+ kfree(msg);
+ }
+
+ spin_lock(&proc->lock);
+ rb_erase(&thread->rb_node, &proc->thread_tree);
+ spin_unlock(&proc->lock);
+
+ kfree(thread);
+
+ // this has to be the last step as the last proc queue user will destroy the proc
+ put_msg_queue(proc->queue);
+}
+
+static void proc_queue_release(struct msg_queue *q, void *data)
+{
+ struct binder_proc *proc = data;
+ struct rb_node *n;
+ struct binder_obj *obj;
+ struct debugfs_priv *priv, *next;
+
+ drain_msg_queue(proc, q);
+
+ // safe to free objs and send BR_DEAD_BINDER
+ while ((n = rb_first(&proc->obj_tree))) {
+ obj = rb_entry(n, struct binder_obj, rb_node);
+
+ rb_erase(n, &proc->obj_tree);
+ hlist_del(&obj->hash_node);
+
+ _binder_free_obj(proc, obj);
+ }
+
+ // safe to do garbage collection now
+ list_for_each_entry_safe(priv, next, &proc->garbage_list, list) {
+ list_del(&priv->list);
+ kfree(priv);
+ }
+
+ if (proc->slob)
+ fast_slob_destroy(proc->slob);
+
+ kfree(proc);
+}
+
+static struct binder_proc *binder_new_proc(struct file *filp)
+{
+ struct binder_proc *proc;
+ struct debugfs_priv *priv;
+ int i;
+
+ proc = kmalloc(sizeof(*proc), GFP_KERNEL);
+ if (!proc)
+ return NULL;
+
+ proc->queue = create_msg_queue(0, proc_queue_release, proc);
+ if (!proc->queue) {
+ kfree(proc);
+ return NULL;
+ }
+
+ proc->slob = NULL;
+ proc->slob_uses = 0;
+ proc->ustart = 0;
+ proc->pid = task_tgid_vnr(current);
+ proc->max_threads = 0;
+
+ atomic_set(&proc->proc_loopers, 0);
+ atomic_set(&proc->requested_loopers, 0);
+ atomic_set(&proc->registered_loopers, 0);
+
+ spin_lock_init(&proc->lock);
+ proc->thread_tree.rb_node = NULL;
+
+ for (i = 0; i < sizeof(proc->obj_hash) / sizeof(proc->obj_hash[0]); i++)
+ INIT_HLIST_HEAD(&proc->obj_hash[i]);
+ proc->obj_seq = 1;
+
+ spin_lock_init(&proc->obj_lock);
+ proc->obj_tree.rb_node = NULL;
+
+ INIT_LIST_HEAD(&proc->garbage_list);
+
+ if (!(priv = debugfs_new_proc(proc))) {
+ // the queue release handler will free proc struct
+ free_msg_queue(proc->queue);
+ return NULL;
+ }
+ list_add(&priv->list, &proc->garbage_list);
+
+ return proc;
+}
+
+static struct binder_thread *binder_new_thread(struct binder_proc *proc, struct file *filp, pid_t pid)
+{
+ struct binder_thread *new_thread, *thread;
+ struct rb_node **p = &proc->thread_tree.rb_node;
+ struct rb_node *parent = NULL;
+ struct debugfs_priv *priv;
+
+ new_thread = kmalloc(sizeof(*new_thread), GFP_KERNEL);
+ if (!new_thread)
+ return NULL;
+
+ new_thread->queue = create_msg_queue(0, thread_queue_release, new_thread);
+ if (!new_thread->queue || !get_msg_queue(msg_queue_id(proc->queue))) {
+ kfree(new_thread);
+ return NULL;
+ }
+
+ new_thread->pid = pid;
+ new_thread->state = 0;
+ new_thread->last_error = 0;
+ new_thread->non_block = (filp->f_flags & O_NONBLOCK) ? 1 : 0; // compat
+ new_thread->pending_replies = 0;
+ INIT_LIST_HEAD(&new_thread->incoming_transactions);
+ new_thread->proc = proc;
+
+ if (!(priv = debugfs_new_thread(proc, new_thread))) {
+ free_msg_queue(new_thread->queue);
+ kfree(new_thread);
+ return NULL;
+ }
+
+ spin_lock(&proc->lock);
+ while (*p) {
+ parent = *p;
+ thread = rb_entry(parent, struct binder_thread, rb_node);
+
+ if (pid < thread->pid)
+ p = &(*p)->rb_left;
+ else if (pid > thread->pid)
+ p = &(*p)->rb_right;
+ else {
+ spin_unlock(&proc->lock);
+ BUG();
+ free_msg_queue(new_thread->queue);
+ kfree(new_thread);
+ return thread;
+ }
+ }
+
+ rb_link_node(&new_thread->rb_node, parent, p);
+ rb_insert_color(&new_thread->rb_node, &proc->thread_tree);
+
+ list_add(&priv->list, &proc->garbage_list);
+ spin_unlock(&proc->lock);
+
+ return new_thread;
+}
+
+static struct binder_thread *binder_get_thread(struct binder_proc *proc, struct file *filp)
+{
+ struct rb_node **p = &proc->thread_tree.rb_node;
+ struct rb_node *parent = NULL;
+ struct binder_thread *thread;
+ pid_t pid = task_pid_vnr(current);
+
+ spin_lock(&proc->lock);
+ while (*p) {
+ parent = *p;
+ thread = rb_entry(parent, struct binder_thread, rb_node);
+
+ if (pid < thread->pid)
+ p = &(*p)->rb_left;
+ else if (pid > thread->pid)
+ p = &(*p)->rb_right;
+ else {
+ spin_unlock(&proc->lock);
+ return thread;
+ }
+ }
+ spin_unlock(&proc->lock);
+
+ return binder_new_thread(proc, filp, pid);
+}
+
+static int binder_free_thread(struct binder_proc *proc, struct binder_thread *thread)
+{
+ free_msg_queue(thread->queue);
+ debugfs_remove(thread->info_node);
+ return 0;
+}
+
+static int binder_free_proc(struct binder_proc *proc)
+{
+ struct rb_node *n;
+ struct binder_thread *thread;
+
+ free_msg_queue(proc->queue);
+
+ while ((n = rb_first(&proc->thread_tree))) {
+ thread = rb_entry(n, struct binder_thread, rb_node);
+ binder_free_thread(proc, thread);
+ }
+
+ debugfs_remove_recursive(proc->proc_dir);
+ return 0;
+}
+
+static inline int binder_acquire_obj(struct binder_proc *proc, struct binder_thread *thread, struct binder_obj *obj)
+{
+ int refs = atomic_inc_return(&obj->refs);
+
+ if (refs == 1) {
+ struct bcmd_msg *msg;
+ int r;
+
+ msg = binder_alloc_msg(0, 0);
+ if (!msg)
+ return -ENOMEM;
+
+ msg->binder = obj->binder;
+ msg->cookie = obj->cookie;
+
+ if (OBJ_IS_BINDER(obj)) {
+ msg->type = BR_ACQUIRE;
+ r = _bcmd_write_msg(thread->queue, msg); // owner thread should receive BR_ACQUIRE
+ } else {
+ msg->type = BC_ACQUIRE;
+ r = bcmd_write_msg(obj->owner, msg); // tell the owner we are referencing it
+ }
+
+ if (r < 0) {
+ kfree(msg);
+ return r;
+ }
+ }
+
+ return refs;
+}
+
+static inline int binder_release_obj(struct binder_proc *proc, struct binder_thread *thread, struct binder_obj *obj)
+{
+ int refs = atomic_dec_return(&obj->refs);
+
+ if (refs == 0) {
+ struct bcmd_msg *msg;
+ int r;
+
+ msg = binder_alloc_msg(0, 0);
+ if (!msg)
+ return -ENOMEM;
+
+ msg->binder = obj->binder;
+ msg->cookie = obj->cookie;
+
+ if (OBJ_IS_BINDER(obj)) {
+ msg->type = BR_RELEASE;
+ r = _bcmd_write_msg(thread->queue, msg);
+ } else {
+ msg->type = BC_RELEASE;
+ r = bcmd_write_msg(obj->owner, msg); // tell the owner we are no longer referencing it
+ }
+
+ if (r < 0) {
+ kfree(msg);
+ return r;