19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2777783e0SGreg Kroah-Hartman /* binder.c
3777783e0SGreg Kroah-Hartman *
4777783e0SGreg Kroah-Hartman * Android IPC Subsystem
5777783e0SGreg Kroah-Hartman *
6777783e0SGreg Kroah-Hartman * Copyright (C) 2007-2008 Google, Inc.
7777783e0SGreg Kroah-Hartman */
8777783e0SGreg Kroah-Hartman
99630fe88STodd Kjos /*
109630fe88STodd Kjos * Locking overview
119630fe88STodd Kjos *
129630fe88STodd Kjos * There are 3 main spinlocks which must be acquired in the
139630fe88STodd Kjos * order shown:
149630fe88STodd Kjos *
159630fe88STodd Kjos * 1) proc->outer_lock : protects binder_ref
169630fe88STodd Kjos * binder_proc_lock() and binder_proc_unlock() are
179630fe88STodd Kjos * used to acq/rel.
189630fe88STodd Kjos * 2) node->lock : protects most fields of binder_node.
199630fe88STodd Kjos * binder_node_lock() and binder_node_unlock() are
209630fe88STodd Kjos * used to acq/rel
219630fe88STodd Kjos * 3) proc->inner_lock : protects the thread and node lists
221b77e9dcSMartijn Coenen * (proc->threads, proc->waiting_threads, proc->nodes)
231b77e9dcSMartijn Coenen * and all todo lists associated with the binder_proc
241b77e9dcSMartijn Coenen * (proc->todo, thread->todo, proc->delivered_death and
251b77e9dcSMartijn Coenen * node->async_todo), as well as thread->transaction_stack
269630fe88STodd Kjos * binder_inner_proc_lock() and binder_inner_proc_unlock()
279630fe88STodd Kjos * are used to acq/rel
289630fe88STodd Kjos *
299630fe88STodd Kjos * Any lock under procA must never be nested under any lock at the same
309630fe88STodd Kjos * level or below on procB.
319630fe88STodd Kjos *
329630fe88STodd Kjos * Functions that require a lock held on entry indicate which lock
339630fe88STodd Kjos * in the suffix of the function name:
349630fe88STodd Kjos *
359630fe88STodd Kjos * foo_olocked() : requires node->outer_lock
369630fe88STodd Kjos * foo_nlocked() : requires node->lock
379630fe88STodd Kjos * foo_ilocked() : requires proc->inner_lock
389630fe88STodd Kjos * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
399630fe88STodd Kjos * foo_nilocked(): requires node->lock and proc->inner_lock
409630fe88STodd Kjos * ...
419630fe88STodd Kjos */
429630fe88STodd Kjos
43777783e0SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44777783e0SGreg Kroah-Hartman
45777783e0SGreg Kroah-Hartman #include <linux/fdtable.h>
46777783e0SGreg Kroah-Hartman #include <linux/file.h>
47777783e0SGreg Kroah-Hartman #include <linux/freezer.h>
48777783e0SGreg Kroah-Hartman #include <linux/fs.h>
49777783e0SGreg Kroah-Hartman #include <linux/list.h>
50777783e0SGreg Kroah-Hartman #include <linux/miscdevice.h>
51777783e0SGreg Kroah-Hartman #include <linux/module.h>
52777783e0SGreg Kroah-Hartman #include <linux/mutex.h>
53777783e0SGreg Kroah-Hartman #include <linux/nsproxy.h>
54777783e0SGreg Kroah-Hartman #include <linux/poll.h>
55777783e0SGreg Kroah-Hartman #include <linux/debugfs.h>
56777783e0SGreg Kroah-Hartman #include <linux/rbtree.h>
573f07c014SIngo Molnar #include <linux/sched/signal.h>
586e84f315SIngo Molnar #include <linux/sched/mm.h>
59777783e0SGreg Kroah-Hartman #include <linux/seq_file.h>
6051d8a7ecSChristian Brauner #include <linux/string.h>
61777783e0SGreg Kroah-Hartman #include <linux/uaccess.h>
62777783e0SGreg Kroah-Hartman #include <linux/pid_namespace.h>
6379af7307SStephen Smalley #include <linux/security.h>
649630fe88STodd Kjos #include <linux/spinlock.h>
65128f3804SSherry Yang #include <linux/ratelimit.h>
6644d8047fSTodd Kjos #include <linux/syscalls.h>
6780cd7956STodd Kjos #include <linux/task_work.h>
68990be747SJann Horn #include <linux/sizes.h>
6980093619SChuang Zhang #include <linux/ktime.h>
70777783e0SGreg Kroah-Hartman
719246a4a9SGreg Kroah-Hartman #include <uapi/linux/android/binder.h>
72f371a7c1SGuenter Roeck
73e80ca2e9SAjith P V #include <linux/cacheflush.h>
74f371a7c1SGuenter Roeck
753ad20fe3SChristian Brauner #include "binder_internal.h"
76777783e0SGreg Kroah-Hartman #include "binder_trace.h"
77777783e0SGreg Kroah-Hartman
78c44b1231STodd Kjos static HLIST_HEAD(binder_deferred_list);
79777783e0SGreg Kroah-Hartman static DEFINE_MUTEX(binder_deferred_lock);
80777783e0SGreg Kroah-Hartman
81ac4812c5SMartijn Coenen static HLIST_HEAD(binder_devices);
82777783e0SGreg Kroah-Hartman static HLIST_HEAD(binder_procs);
83c44b1231STodd Kjos static DEFINE_MUTEX(binder_procs_lock);
84c44b1231STodd Kjos
85777783e0SGreg Kroah-Hartman static HLIST_HEAD(binder_dead_nodes);
86c44b1231STodd Kjos static DEFINE_SPINLOCK(binder_dead_nodes_lock);
87777783e0SGreg Kroah-Hartman
88777783e0SGreg Kroah-Hartman static struct dentry *binder_debugfs_dir_entry_root;
89777783e0SGreg Kroah-Hartman static struct dentry *binder_debugfs_dir_entry_proc;
90656a800aSTodd Kjos static atomic_t binder_last_id;
91777783e0SGreg Kroah-Hartman
92c13e0a52SYangtao Li static int proc_show(struct seq_file *m, void *unused);
93c13e0a52SYangtao Li DEFINE_SHOW_ATTRIBUTE(proc);
94777783e0SGreg Kroah-Hartman
95777783e0SGreg Kroah-Hartman #define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
96777783e0SGreg Kroah-Hartman
97777783e0SGreg Kroah-Hartman enum {
98777783e0SGreg Kroah-Hartman BINDER_DEBUG_USER_ERROR = 1U << 0,
99777783e0SGreg Kroah-Hartman BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
100777783e0SGreg Kroah-Hartman BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
101777783e0SGreg Kroah-Hartman BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
102777783e0SGreg Kroah-Hartman BINDER_DEBUG_DEAD_BINDER = 1U << 4,
103777783e0SGreg Kroah-Hartman BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
104777783e0SGreg Kroah-Hartman BINDER_DEBUG_READ_WRITE = 1U << 6,
105777783e0SGreg Kroah-Hartman BINDER_DEBUG_USER_REFS = 1U << 7,
106777783e0SGreg Kroah-Hartman BINDER_DEBUG_THREADS = 1U << 8,
107777783e0SGreg Kroah-Hartman BINDER_DEBUG_TRANSACTION = 1U << 9,
108777783e0SGreg Kroah-Hartman BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
109777783e0SGreg Kroah-Hartman BINDER_DEBUG_FREE_BUFFER = 1U << 11,
110777783e0SGreg Kroah-Hartman BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
11119c98724STodd Kjos BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
1129630fe88STodd Kjos BINDER_DEBUG_SPINLOCKS = 1U << 14,
113777783e0SGreg Kroah-Hartman };
114777783e0SGreg Kroah-Hartman static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
115777783e0SGreg Kroah-Hartman BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
11621d02ddfSHarsh Shandilya module_param_named(debug_mask, binder_debug_mask, uint, 0644);
117777783e0SGreg Kroah-Hartman
118ca2864c6SHridya Valsaraju char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
119ac4812c5SMartijn Coenen module_param_named(devices, binder_devices_param, charp, 0444);
120ac4812c5SMartijn Coenen
121777783e0SGreg Kroah-Hartman static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
122777783e0SGreg Kroah-Hartman static int binder_stop_on_user_error;
123777783e0SGreg Kroah-Hartman
binder_set_stop_on_user_error(const char * val,const struct kernel_param * kp)124777783e0SGreg Kroah-Hartman static int binder_set_stop_on_user_error(const char *val,
125e4dca7b7SKees Cook const struct kernel_param *kp)
126777783e0SGreg Kroah-Hartman {
127777783e0SGreg Kroah-Hartman int ret;
128777783e0SGreg Kroah-Hartman
129777783e0SGreg Kroah-Hartman ret = param_set_int(val, kp);
130777783e0SGreg Kroah-Hartman if (binder_stop_on_user_error < 2)
131777783e0SGreg Kroah-Hartman wake_up(&binder_user_error_wait);
132777783e0SGreg Kroah-Hartman return ret;
133777783e0SGreg Kroah-Hartman }
134777783e0SGreg Kroah-Hartman module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
13521d02ddfSHarsh Shandilya param_get_int, &binder_stop_on_user_error, 0644);
136777783e0SGreg Kroah-Hartman
binder_debug(int mask,const char * format,...)13706a3494eSCarlos Llamas static __printf(2, 3) void binder_debug(int mask, const char *format, ...)
13806a3494eSCarlos Llamas {
13906a3494eSCarlos Llamas struct va_format vaf;
14006a3494eSCarlos Llamas va_list args;
141777783e0SGreg Kroah-Hartman
14206a3494eSCarlos Llamas if (binder_debug_mask & mask) {
14306a3494eSCarlos Llamas va_start(args, format);
14406a3494eSCarlos Llamas vaf.va = &args;
14506a3494eSCarlos Llamas vaf.fmt = format;
14606a3494eSCarlos Llamas pr_info_ratelimited("%pV", &vaf);
14706a3494eSCarlos Llamas va_end(args);
14806a3494eSCarlos Llamas }
14906a3494eSCarlos Llamas }
15006a3494eSCarlos Llamas
151a15dac8bSCarlos Llamas #define binder_txn_error(x...) \
152a15dac8bSCarlos Llamas binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, x)
153a15dac8bSCarlos Llamas
binder_user_error(const char * format,...)15406a3494eSCarlos Llamas static __printf(1, 2) void binder_user_error(const char *format, ...)
15506a3494eSCarlos Llamas {
15606a3494eSCarlos Llamas struct va_format vaf;
15706a3494eSCarlos Llamas va_list args;
15806a3494eSCarlos Llamas
15906a3494eSCarlos Llamas if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) {
16006a3494eSCarlos Llamas va_start(args, format);
16106a3494eSCarlos Llamas vaf.va = &args;
16206a3494eSCarlos Llamas vaf.fmt = format;
16306a3494eSCarlos Llamas pr_info_ratelimited("%pV", &vaf);
16406a3494eSCarlos Llamas va_end(args);
16506a3494eSCarlos Llamas }
16606a3494eSCarlos Llamas
16706a3494eSCarlos Llamas if (binder_stop_on_user_error)
16806a3494eSCarlos Llamas binder_stop_on_user_error = 2;
16906a3494eSCarlos Llamas }
170777783e0SGreg Kroah-Hartman
171bd32889eSCarlos Llamas #define binder_set_extended_error(ee, _id, _command, _param) \
172bd32889eSCarlos Llamas do { \
173bd32889eSCarlos Llamas (ee)->id = _id; \
174bd32889eSCarlos Llamas (ee)->command = _command; \
175bd32889eSCarlos Llamas (ee)->param = _param; \
176bd32889eSCarlos Llamas } while (0)
177bd32889eSCarlos Llamas
178feba3900SMartijn Coenen #define to_flat_binder_object(hdr) \
179feba3900SMartijn Coenen container_of(hdr, struct flat_binder_object, hdr)
180feba3900SMartijn Coenen
181feba3900SMartijn Coenen #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
182feba3900SMartijn Coenen
1837980240bSMartijn Coenen #define to_binder_buffer_object(hdr) \
1847980240bSMartijn Coenen container_of(hdr, struct binder_buffer_object, hdr)
1857980240bSMartijn Coenen
186def95c73SMartijn Coenen #define to_binder_fd_array_object(hdr) \
187def95c73SMartijn Coenen container_of(hdr, struct binder_fd_array_object, hdr)
188def95c73SMartijn Coenen
189777783e0SGreg Kroah-Hartman static struct binder_stats binder_stats;
190777783e0SGreg Kroah-Hartman
binder_stats_deleted(enum binder_stat_types type)191777783e0SGreg Kroah-Hartman static inline void binder_stats_deleted(enum binder_stat_types type)
192777783e0SGreg Kroah-Hartman {
1930953c797SBadhri Jagan Sridharan atomic_inc(&binder_stats.obj_deleted[type]);
194777783e0SGreg Kroah-Hartman }
195777783e0SGreg Kroah-Hartman
binder_stats_created(enum binder_stat_types type)196777783e0SGreg Kroah-Hartman static inline void binder_stats_created(enum binder_stat_types type)
197777783e0SGreg Kroah-Hartman {
1980953c797SBadhri Jagan Sridharan atomic_inc(&binder_stats.obj_created[type]);
199777783e0SGreg Kroah-Hartman }
200777783e0SGreg Kroah-Hartman
201b7e241bbSCarlos Llamas struct binder_transaction_log_entry {
202b7e241bbSCarlos Llamas int debug_id;
203b7e241bbSCarlos Llamas int debug_id_done;
204b7e241bbSCarlos Llamas int call_type;
205b7e241bbSCarlos Llamas int from_proc;
206b7e241bbSCarlos Llamas int from_thread;
207b7e241bbSCarlos Llamas int target_handle;
208b7e241bbSCarlos Llamas int to_proc;
209b7e241bbSCarlos Llamas int to_thread;
210b7e241bbSCarlos Llamas int to_node;
211b7e241bbSCarlos Llamas int data_size;
212b7e241bbSCarlos Llamas int offsets_size;
213b7e241bbSCarlos Llamas int return_error_line;
214b7e241bbSCarlos Llamas uint32_t return_error;
215b7e241bbSCarlos Llamas uint32_t return_error_param;
216b7e241bbSCarlos Llamas char context_name[BINDERFS_MAX_NAME + 1];
217b7e241bbSCarlos Llamas };
218b7e241bbSCarlos Llamas
219b7e241bbSCarlos Llamas struct binder_transaction_log {
220b7e241bbSCarlos Llamas atomic_t cur;
221b7e241bbSCarlos Llamas bool full;
222b7e241bbSCarlos Llamas struct binder_transaction_log_entry entry[32];
223b7e241bbSCarlos Llamas };
224b7e241bbSCarlos Llamas
225b7e241bbSCarlos Llamas static struct binder_transaction_log binder_transaction_log;
226b7e241bbSCarlos Llamas static struct binder_transaction_log binder_transaction_log_failed;
227777783e0SGreg Kroah-Hartman
binder_transaction_log_add(struct binder_transaction_log * log)228777783e0SGreg Kroah-Hartman static struct binder_transaction_log_entry *binder_transaction_log_add(
229777783e0SGreg Kroah-Hartman struct binder_transaction_log *log)
230777783e0SGreg Kroah-Hartman {
231777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *e;
232d99c7333STodd Kjos unsigned int cur = atomic_inc_return(&log->cur);
233777783e0SGreg Kroah-Hartman
234d99c7333STodd Kjos if (cur >= ARRAY_SIZE(log->entry))
235197410adSGustavo A. R. Silva log->full = true;
236d99c7333STodd Kjos e = &log->entry[cur % ARRAY_SIZE(log->entry)];
237d99c7333STodd Kjos WRITE_ONCE(e->debug_id_done, 0);
238d99c7333STodd Kjos /*
239d99c7333STodd Kjos * write-barrier to synchronize access to e->debug_id_done.
240d99c7333STodd Kjos * We make sure the initialized 0 value is seen before
241d99c7333STodd Kjos * memset() other fields are zeroed by memset.
242d99c7333STodd Kjos */
243d99c7333STodd Kjos smp_wmb();
244d99c7333STodd Kjos memset(e, 0, sizeof(*e));
245777783e0SGreg Kroah-Hartman return e;
246777783e0SGreg Kroah-Hartman }
247777783e0SGreg Kroah-Hartman
248777783e0SGreg Kroah-Hartman enum binder_deferred_state {
24944d8047fSTodd Kjos BINDER_DEFERRED_FLUSH = 0x01,
25044d8047fSTodd Kjos BINDER_DEFERRED_RELEASE = 0x02,
251777783e0SGreg Kroah-Hartman };
252777783e0SGreg Kroah-Hartman
253777783e0SGreg Kroah-Hartman enum {
254777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_REGISTERED = 0x01,
255777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_ENTERED = 0x02,
256777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_EXITED = 0x04,
257777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_INVALID = 0x08,
258777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_WAITING = 0x10,
2591b77e9dcSMartijn Coenen BINDER_LOOPER_STATE_POLL = 0x20,
260777783e0SGreg Kroah-Hartman };
261777783e0SGreg Kroah-Hartman
2629630fe88STodd Kjos /**
2639630fe88STodd Kjos * binder_proc_lock() - Acquire outer lock for given binder_proc
2649630fe88STodd Kjos * @proc: struct binder_proc to acquire
2659630fe88STodd Kjos *
2669630fe88STodd Kjos * Acquires proc->outer_lock. Used to protect binder_ref
2679630fe88STodd Kjos * structures associated with the given proc.
2689630fe88STodd Kjos */
2699630fe88STodd Kjos #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
2709630fe88STodd Kjos static void
_binder_proc_lock(struct binder_proc * proc,int line)2719630fe88STodd Kjos _binder_proc_lock(struct binder_proc *proc, int line)
272324fa64cSTodd Kjos __acquires(&proc->outer_lock)
2739630fe88STodd Kjos {
2749630fe88STodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
2759630fe88STodd Kjos "%s: line=%d\n", __func__, line);
2769630fe88STodd Kjos spin_lock(&proc->outer_lock);
2779630fe88STodd Kjos }
2789630fe88STodd Kjos
2799630fe88STodd Kjos /**
2809630fe88STodd Kjos * binder_proc_unlock() - Release spinlock for given binder_proc
2819630fe88STodd Kjos * @proc: struct binder_proc to acquire
2829630fe88STodd Kjos *
2839630fe88STodd Kjos * Release lock acquired via binder_proc_lock()
2849630fe88STodd Kjos */
285ad228a34SRandy Dunlap #define binder_proc_unlock(proc) _binder_proc_unlock(proc, __LINE__)
2869630fe88STodd Kjos static void
_binder_proc_unlock(struct binder_proc * proc,int line)2879630fe88STodd Kjos _binder_proc_unlock(struct binder_proc *proc, int line)
288324fa64cSTodd Kjos __releases(&proc->outer_lock)
2899630fe88STodd Kjos {
2909630fe88STodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
2919630fe88STodd Kjos "%s: line=%d\n", __func__, line);
2929630fe88STodd Kjos spin_unlock(&proc->outer_lock);
2939630fe88STodd Kjos }
2949630fe88STodd Kjos
2959630fe88STodd Kjos /**
2969630fe88STodd Kjos * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
2979630fe88STodd Kjos * @proc: struct binder_proc to acquire
2989630fe88STodd Kjos *
2999630fe88STodd Kjos * Acquires proc->inner_lock. Used to protect todo lists
3009630fe88STodd Kjos */
3019630fe88STodd Kjos #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
3029630fe88STodd Kjos static void
_binder_inner_proc_lock(struct binder_proc * proc,int line)3039630fe88STodd Kjos _binder_inner_proc_lock(struct binder_proc *proc, int line)
304324fa64cSTodd Kjos __acquires(&proc->inner_lock)
3059630fe88STodd Kjos {
3069630fe88STodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
3079630fe88STodd Kjos "%s: line=%d\n", __func__, line);
3089630fe88STodd Kjos spin_lock(&proc->inner_lock);
3099630fe88STodd Kjos }
3109630fe88STodd Kjos
3119630fe88STodd Kjos /**
3129630fe88STodd Kjos * binder_inner_proc_unlock() - Release inner lock for given binder_proc
3139630fe88STodd Kjos * @proc: struct binder_proc to acquire
3149630fe88STodd Kjos *
3159630fe88STodd Kjos * Release lock acquired via binder_inner_proc_lock()
3169630fe88STodd Kjos */
3179630fe88STodd Kjos #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
3189630fe88STodd Kjos static void
_binder_inner_proc_unlock(struct binder_proc * proc,int line)3199630fe88STodd Kjos _binder_inner_proc_unlock(struct binder_proc *proc, int line)
320324fa64cSTodd Kjos __releases(&proc->inner_lock)
3219630fe88STodd Kjos {
3229630fe88STodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
3239630fe88STodd Kjos "%s: line=%d\n", __func__, line);
3249630fe88STodd Kjos spin_unlock(&proc->inner_lock);
3259630fe88STodd Kjos }
3269630fe88STodd Kjos
3279630fe88STodd Kjos /**
3289630fe88STodd Kjos * binder_node_lock() - Acquire spinlock for given binder_node
3299630fe88STodd Kjos * @node: struct binder_node to acquire
3309630fe88STodd Kjos *
3319630fe88STodd Kjos * Acquires node->lock. Used to protect binder_node fields
3329630fe88STodd Kjos */
3339630fe88STodd Kjos #define binder_node_lock(node) _binder_node_lock(node, __LINE__)
3349630fe88STodd Kjos static void
_binder_node_lock(struct binder_node * node,int line)3359630fe88STodd Kjos _binder_node_lock(struct binder_node *node, int line)
336324fa64cSTodd Kjos __acquires(&node->lock)
3379630fe88STodd Kjos {
3389630fe88STodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
3399630fe88STodd Kjos "%s: line=%d\n", __func__, line);
3409630fe88STodd Kjos spin_lock(&node->lock);
3419630fe88STodd Kjos }
3429630fe88STodd Kjos
3439630fe88STodd Kjos /**
3449630fe88STodd Kjos * binder_node_unlock() - Release spinlock for given binder_proc
3459630fe88STodd Kjos * @node: struct binder_node to acquire
3469630fe88STodd Kjos *
3479630fe88STodd Kjos * Release lock acquired via binder_node_lock()
3489630fe88STodd Kjos */
3499630fe88STodd Kjos #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
3509630fe88STodd Kjos static void
_binder_node_unlock(struct binder_node * node,int line)3519630fe88STodd Kjos _binder_node_unlock(struct binder_node *node, int line)
352324fa64cSTodd Kjos __releases(&node->lock)
3539630fe88STodd Kjos {
3549630fe88STodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
3559630fe88STodd Kjos "%s: line=%d\n", __func__, line);
3569630fe88STodd Kjos spin_unlock(&node->lock);
3579630fe88STodd Kjos }
3589630fe88STodd Kjos
359673068eeSTodd Kjos /**
360673068eeSTodd Kjos * binder_node_inner_lock() - Acquire node and inner locks
361673068eeSTodd Kjos * @node: struct binder_node to acquire
362673068eeSTodd Kjos *
363673068eeSTodd Kjos * Acquires node->lock. If node->proc also acquires
364673068eeSTodd Kjos * proc->inner_lock. Used to protect binder_node fields
365673068eeSTodd Kjos */
366673068eeSTodd Kjos #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
367673068eeSTodd Kjos static void
_binder_node_inner_lock(struct binder_node * node,int line)368673068eeSTodd Kjos _binder_node_inner_lock(struct binder_node *node, int line)
369324fa64cSTodd Kjos __acquires(&node->lock) __acquires(&node->proc->inner_lock)
370673068eeSTodd Kjos {
371673068eeSTodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
372673068eeSTodd Kjos "%s: line=%d\n", __func__, line);
373673068eeSTodd Kjos spin_lock(&node->lock);
374673068eeSTodd Kjos if (node->proc)
375673068eeSTodd Kjos binder_inner_proc_lock(node->proc);
376324fa64cSTodd Kjos else
377324fa64cSTodd Kjos /* annotation for sparse */
378324fa64cSTodd Kjos __acquire(&node->proc->inner_lock);
379673068eeSTodd Kjos }
380673068eeSTodd Kjos
381673068eeSTodd Kjos /**
382ad228a34SRandy Dunlap * binder_node_inner_unlock() - Release node and inner locks
383673068eeSTodd Kjos * @node: struct binder_node to acquire
384673068eeSTodd Kjos *
385673068eeSTodd Kjos * Release lock acquired via binder_node_lock()
386673068eeSTodd Kjos */
387673068eeSTodd Kjos #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
388673068eeSTodd Kjos static void
_binder_node_inner_unlock(struct binder_node * node,int line)389673068eeSTodd Kjos _binder_node_inner_unlock(struct binder_node *node, int line)
390324fa64cSTodd Kjos __releases(&node->lock) __releases(&node->proc->inner_lock)
391673068eeSTodd Kjos {
392673068eeSTodd Kjos struct binder_proc *proc = node->proc;
393673068eeSTodd Kjos
394673068eeSTodd Kjos binder_debug(BINDER_DEBUG_SPINLOCKS,
395673068eeSTodd Kjos "%s: line=%d\n", __func__, line);
396673068eeSTodd Kjos if (proc)
397673068eeSTodd Kjos binder_inner_proc_unlock(proc);
398324fa64cSTodd Kjos else
399324fa64cSTodd Kjos /* annotation for sparse */
400324fa64cSTodd Kjos __release(&node->proc->inner_lock);
401673068eeSTodd Kjos spin_unlock(&node->lock);
402673068eeSTodd Kjos }
403673068eeSTodd Kjos
binder_worklist_empty_ilocked(struct list_head * list)40472196393STodd Kjos static bool binder_worklist_empty_ilocked(struct list_head *list)
40572196393STodd Kjos {
40672196393STodd Kjos return list_empty(list);
40772196393STodd Kjos }
40872196393STodd Kjos
40972196393STodd Kjos /**
41072196393STodd Kjos * binder_worklist_empty() - Check if no items on the work list
41172196393STodd Kjos * @proc: binder_proc associated with list
41272196393STodd Kjos * @list: list to check
41372196393STodd Kjos *
41472196393STodd Kjos * Return: true if there are no items on list, else false
41572196393STodd Kjos */
binder_worklist_empty(struct binder_proc * proc,struct list_head * list)41672196393STodd Kjos static bool binder_worklist_empty(struct binder_proc *proc,
41772196393STodd Kjos struct list_head *list)
41872196393STodd Kjos {
41972196393STodd Kjos bool ret;
42072196393STodd Kjos
42172196393STodd Kjos binder_inner_proc_lock(proc);
42272196393STodd Kjos ret = binder_worklist_empty_ilocked(list);
42372196393STodd Kjos binder_inner_proc_unlock(proc);
42472196393STodd Kjos return ret;
42572196393STodd Kjos }
42672196393STodd Kjos
427148ade2cSMartijn Coenen /**
428148ade2cSMartijn Coenen * binder_enqueue_work_ilocked() - Add an item to the work list
429148ade2cSMartijn Coenen * @work: struct binder_work to add to list
430148ade2cSMartijn Coenen * @target_list: list to add work to
431148ade2cSMartijn Coenen *
432148ade2cSMartijn Coenen * Adds the work to the specified list. Asserts that work
433148ade2cSMartijn Coenen * is not already on a list.
434148ade2cSMartijn Coenen *
435148ade2cSMartijn Coenen * Requires the proc->inner_lock to be held.
436148ade2cSMartijn Coenen */
43772196393STodd Kjos static void
binder_enqueue_work_ilocked(struct binder_work * work,struct list_head * target_list)43872196393STodd Kjos binder_enqueue_work_ilocked(struct binder_work *work,
43972196393STodd Kjos struct list_head *target_list)
44072196393STodd Kjos {
44172196393STodd Kjos BUG_ON(target_list == NULL);
44272196393STodd Kjos BUG_ON(work->entry.next && !list_empty(&work->entry));
44372196393STodd Kjos list_add_tail(&work->entry, target_list);
44472196393STodd Kjos }
44572196393STodd Kjos
44672196393STodd Kjos /**
447148ade2cSMartijn Coenen * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
448148ade2cSMartijn Coenen * @thread: thread to queue work to
44972196393STodd Kjos * @work: struct binder_work to add to list
45072196393STodd Kjos *
451148ade2cSMartijn Coenen * Adds the work to the todo list of the thread. Doesn't set the process_todo
452148ade2cSMartijn Coenen * flag, which means that (if it wasn't already set) the thread will go to
453148ade2cSMartijn Coenen * sleep without handling this work when it calls read.
454148ade2cSMartijn Coenen *
455148ade2cSMartijn Coenen * Requires the proc->inner_lock to be held.
45672196393STodd Kjos */
45772196393STodd Kjos static void
binder_enqueue_deferred_thread_work_ilocked(struct binder_thread * thread,struct binder_work * work)458148ade2cSMartijn Coenen binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
459148ade2cSMartijn Coenen struct binder_work *work)
46072196393STodd Kjos {
46144b73962SSherry Yang WARN_ON(!list_empty(&thread->waiting_thread_node));
462148ade2cSMartijn Coenen binder_enqueue_work_ilocked(work, &thread->todo);
463148ade2cSMartijn Coenen }
464148ade2cSMartijn Coenen
465148ade2cSMartijn Coenen /**
466148ade2cSMartijn Coenen * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
467148ade2cSMartijn Coenen * @thread: thread to queue work to
468148ade2cSMartijn Coenen * @work: struct binder_work to add to list
469148ade2cSMartijn Coenen *
470148ade2cSMartijn Coenen * Adds the work to the todo list of the thread, and enables processing
471148ade2cSMartijn Coenen * of the todo queue.
472148ade2cSMartijn Coenen *
473148ade2cSMartijn Coenen * Requires the proc->inner_lock to be held.
474148ade2cSMartijn Coenen */
475148ade2cSMartijn Coenen static void
binder_enqueue_thread_work_ilocked(struct binder_thread * thread,struct binder_work * work)476148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
477148ade2cSMartijn Coenen struct binder_work *work)
478148ade2cSMartijn Coenen {
47944b73962SSherry Yang WARN_ON(!list_empty(&thread->waiting_thread_node));
480148ade2cSMartijn Coenen binder_enqueue_work_ilocked(work, &thread->todo);
481a7ae586fSCarlos Llamas
482a7ae586fSCarlos Llamas /* (e)poll-based threads require an explicit wakeup signal when
483a7ae586fSCarlos Llamas * queuing their own work; they rely on these events to consume
484a7ae586fSCarlos Llamas * messages without I/O block. Without it, threads risk waiting
485a7ae586fSCarlos Llamas * indefinitely without handling the work.
486a7ae586fSCarlos Llamas */
487a7ae586fSCarlos Llamas if (thread->looper & BINDER_LOOPER_STATE_POLL &&
488a7ae586fSCarlos Llamas thread->pid == current->pid && !thread->process_todo)
489a7ae586fSCarlos Llamas wake_up_interruptible_sync(&thread->wait);
490a7ae586fSCarlos Llamas
491148ade2cSMartijn Coenen thread->process_todo = true;
492148ade2cSMartijn Coenen }
493148ade2cSMartijn Coenen
494148ade2cSMartijn Coenen /**
495148ade2cSMartijn Coenen * binder_enqueue_thread_work() - Add an item to the thread work list
496148ade2cSMartijn Coenen * @thread: thread to queue work to
497148ade2cSMartijn Coenen * @work: struct binder_work to add to list
498148ade2cSMartijn Coenen *
499148ade2cSMartijn Coenen * Adds the work to the todo list of the thread, and enables processing
500148ade2cSMartijn Coenen * of the todo queue.
501148ade2cSMartijn Coenen */
502148ade2cSMartijn Coenen static void
binder_enqueue_thread_work(struct binder_thread * thread,struct binder_work * work)503148ade2cSMartijn Coenen binder_enqueue_thread_work(struct binder_thread *thread,
504148ade2cSMartijn Coenen struct binder_work *work)
505148ade2cSMartijn Coenen {
506148ade2cSMartijn Coenen binder_inner_proc_lock(thread->proc);
507148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(thread, work);
508148ade2cSMartijn Coenen binder_inner_proc_unlock(thread->proc);
50972196393STodd Kjos }
51072196393STodd Kjos
51172196393STodd Kjos static void
binder_dequeue_work_ilocked(struct binder_work * work)51272196393STodd Kjos binder_dequeue_work_ilocked(struct binder_work *work)
51372196393STodd Kjos {
51472196393STodd Kjos list_del_init(&work->entry);
51572196393STodd Kjos }
51672196393STodd Kjos
51772196393STodd Kjos /**
51872196393STodd Kjos * binder_dequeue_work() - Removes an item from the work list
51972196393STodd Kjos * @proc: binder_proc associated with list
52072196393STodd Kjos * @work: struct binder_work to remove from list
52172196393STodd Kjos *
52272196393STodd Kjos * Removes the specified work item from whatever list it is on.
52372196393STodd Kjos * Can safely be called if work is not on any list.
52472196393STodd Kjos */
52572196393STodd Kjos static void
binder_dequeue_work(struct binder_proc * proc,struct binder_work * work)52672196393STodd Kjos binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
52772196393STodd Kjos {
52872196393STodd Kjos binder_inner_proc_lock(proc);
52972196393STodd Kjos binder_dequeue_work_ilocked(work);
53072196393STodd Kjos binder_inner_proc_unlock(proc);
53172196393STodd Kjos }
53272196393STodd Kjos
binder_dequeue_work_head_ilocked(struct list_head * list)53372196393STodd Kjos static struct binder_work *binder_dequeue_work_head_ilocked(
53472196393STodd Kjos struct list_head *list)
53572196393STodd Kjos {
53672196393STodd Kjos struct binder_work *w;
53772196393STodd Kjos
53872196393STodd Kjos w = list_first_entry_or_null(list, struct binder_work, entry);
53972196393STodd Kjos if (w)
54072196393STodd Kjos list_del_init(&w->entry);
54172196393STodd Kjos return w;
54272196393STodd Kjos }
54372196393STodd Kjos
544777783e0SGreg Kroah-Hartman static void
545777783e0SGreg Kroah-Hartman binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
5467a4408c6STodd Kjos static void binder_free_thread(struct binder_thread *thread);
5477a4408c6STodd Kjos static void binder_free_proc(struct binder_proc *proc);
548da0fa9e4STodd Kjos static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
549777783e0SGreg Kroah-Hartman
binder_has_work_ilocked(struct binder_thread * thread,bool do_proc_work)5501b77e9dcSMartijn Coenen static bool binder_has_work_ilocked(struct binder_thread *thread,
5511b77e9dcSMartijn Coenen bool do_proc_work)
5521b77e9dcSMartijn Coenen {
553148ade2cSMartijn Coenen return thread->process_todo ||
5541b77e9dcSMartijn Coenen thread->looper_need_return ||
5551b77e9dcSMartijn Coenen (do_proc_work &&
5561b77e9dcSMartijn Coenen !binder_worklist_empty_ilocked(&thread->proc->todo));
5571b77e9dcSMartijn Coenen }
5581b77e9dcSMartijn Coenen
binder_has_work(struct binder_thread * thread,bool do_proc_work)5591b77e9dcSMartijn Coenen static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
5601b77e9dcSMartijn Coenen {
5611b77e9dcSMartijn Coenen bool has_work;
5621b77e9dcSMartijn Coenen
5631b77e9dcSMartijn Coenen binder_inner_proc_lock(thread->proc);
5641b77e9dcSMartijn Coenen has_work = binder_has_work_ilocked(thread, do_proc_work);
5651b77e9dcSMartijn Coenen binder_inner_proc_unlock(thread->proc);
5661b77e9dcSMartijn Coenen
5671b77e9dcSMartijn Coenen return has_work;
5681b77e9dcSMartijn Coenen }
5691b77e9dcSMartijn Coenen
binder_available_for_proc_work_ilocked(struct binder_thread * thread)5701b77e9dcSMartijn Coenen static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
5711b77e9dcSMartijn Coenen {
5721b77e9dcSMartijn Coenen return !thread->transaction_stack &&
573a1d85fc6SCarlos Llamas binder_worklist_empty_ilocked(&thread->todo);
5741b77e9dcSMartijn Coenen }
5751b77e9dcSMartijn Coenen
binder_wakeup_poll_threads_ilocked(struct binder_proc * proc,bool sync)5761b77e9dcSMartijn Coenen static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
5771b77e9dcSMartijn Coenen bool sync)
5781b77e9dcSMartijn Coenen {
5791b77e9dcSMartijn Coenen struct rb_node *n;
5801b77e9dcSMartijn Coenen struct binder_thread *thread;
5811b77e9dcSMartijn Coenen
5821b77e9dcSMartijn Coenen for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5831b77e9dcSMartijn Coenen thread = rb_entry(n, struct binder_thread, rb_node);
5841b77e9dcSMartijn Coenen if (thread->looper & BINDER_LOOPER_STATE_POLL &&
5851b77e9dcSMartijn Coenen binder_available_for_proc_work_ilocked(thread)) {
5861b77e9dcSMartijn Coenen if (sync)
5871b77e9dcSMartijn Coenen wake_up_interruptible_sync(&thread->wait);
5881b77e9dcSMartijn Coenen else
5891b77e9dcSMartijn Coenen wake_up_interruptible(&thread->wait);
5901b77e9dcSMartijn Coenen }
5911b77e9dcSMartijn Coenen }
5921b77e9dcSMartijn Coenen }
5931b77e9dcSMartijn Coenen
594408c68b1SMartijn Coenen /**
595408c68b1SMartijn Coenen * binder_select_thread_ilocked() - selects a thread for doing proc work.
596408c68b1SMartijn Coenen * @proc: process to select a thread from
597408c68b1SMartijn Coenen *
598408c68b1SMartijn Coenen * Note that calling this function moves the thread off the waiting_threads
599408c68b1SMartijn Coenen * list, so it can only be woken up by the caller of this function, or a
600408c68b1SMartijn Coenen * signal. Therefore, callers *should* always wake up the thread this function
601408c68b1SMartijn Coenen * returns.
602408c68b1SMartijn Coenen *
603408c68b1SMartijn Coenen * Return: If there's a thread currently waiting for process work,
604408c68b1SMartijn Coenen * returns that thread. Otherwise returns NULL.
605408c68b1SMartijn Coenen */
606408c68b1SMartijn Coenen static struct binder_thread *
binder_select_thread_ilocked(struct binder_proc * proc)607408c68b1SMartijn Coenen binder_select_thread_ilocked(struct binder_proc *proc)
6081b77e9dcSMartijn Coenen {
6091b77e9dcSMartijn Coenen struct binder_thread *thread;
6101b77e9dcSMartijn Coenen
611858b2719SMartijn Coenen assert_spin_locked(&proc->inner_lock);
6121b77e9dcSMartijn Coenen thread = list_first_entry_or_null(&proc->waiting_threads,
6131b77e9dcSMartijn Coenen struct binder_thread,
6141b77e9dcSMartijn Coenen waiting_thread_node);
6151b77e9dcSMartijn Coenen
616408c68b1SMartijn Coenen if (thread)
6171b77e9dcSMartijn Coenen list_del_init(&thread->waiting_thread_node);
618408c68b1SMartijn Coenen
619408c68b1SMartijn Coenen return thread;
620408c68b1SMartijn Coenen }
621408c68b1SMartijn Coenen
622408c68b1SMartijn Coenen /**
623408c68b1SMartijn Coenen * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
624408c68b1SMartijn Coenen * @proc: process to wake up a thread in
625408c68b1SMartijn Coenen * @thread: specific thread to wake-up (may be NULL)
626408c68b1SMartijn Coenen * @sync: whether to do a synchronous wake-up
627408c68b1SMartijn Coenen *
628408c68b1SMartijn Coenen * This function wakes up a thread in the @proc process.
629408c68b1SMartijn Coenen * The caller may provide a specific thread to wake-up in
630408c68b1SMartijn Coenen * the @thread parameter. If @thread is NULL, this function
631408c68b1SMartijn Coenen * will wake up threads that have called poll().
632408c68b1SMartijn Coenen *
633408c68b1SMartijn Coenen * Note that for this function to work as expected, callers
634408c68b1SMartijn Coenen * should first call binder_select_thread() to find a thread
635408c68b1SMartijn Coenen * to handle the work (if they don't have a thread already),
636408c68b1SMartijn Coenen * and pass the result into the @thread parameter.
637408c68b1SMartijn Coenen */
binder_wakeup_thread_ilocked(struct binder_proc * proc,struct binder_thread * thread,bool sync)638408c68b1SMartijn Coenen static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
639408c68b1SMartijn Coenen struct binder_thread *thread,
640408c68b1SMartijn Coenen bool sync)
641408c68b1SMartijn Coenen {
642858b2719SMartijn Coenen assert_spin_locked(&proc->inner_lock);
643408c68b1SMartijn Coenen
644408c68b1SMartijn Coenen if (thread) {
6451b77e9dcSMartijn Coenen if (sync)
6461b77e9dcSMartijn Coenen wake_up_interruptible_sync(&thread->wait);
6471b77e9dcSMartijn Coenen else
6481b77e9dcSMartijn Coenen wake_up_interruptible(&thread->wait);
6491b77e9dcSMartijn Coenen return;
6501b77e9dcSMartijn Coenen }
6511b77e9dcSMartijn Coenen
6521b77e9dcSMartijn Coenen /* Didn't find a thread waiting for proc work; this can happen
6531b77e9dcSMartijn Coenen * in two scenarios:
6541b77e9dcSMartijn Coenen * 1. All threads are busy handling transactions
6551b77e9dcSMartijn Coenen * In that case, one of those threads should call back into
6561b77e9dcSMartijn Coenen * the kernel driver soon and pick up this work.
6571b77e9dcSMartijn Coenen * 2. Threads are using the (e)poll interface, in which case
6581b77e9dcSMartijn Coenen * they may be blocked on the waitqueue without having been
6591b77e9dcSMartijn Coenen * added to waiting_threads. For this case, we just iterate
6601b77e9dcSMartijn Coenen * over all threads not handling transaction work, and
6611b77e9dcSMartijn Coenen * wake them all up. We wake all because we don't know whether
6621b77e9dcSMartijn Coenen * a thread that called into (e)poll is handling non-binder
6631b77e9dcSMartijn Coenen * work currently.
6641b77e9dcSMartijn Coenen */
6651b77e9dcSMartijn Coenen binder_wakeup_poll_threads_ilocked(proc, sync);
6661b77e9dcSMartijn Coenen }
6671b77e9dcSMartijn Coenen
binder_wakeup_proc_ilocked(struct binder_proc * proc)668408c68b1SMartijn Coenen static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
669408c68b1SMartijn Coenen {
670408c68b1SMartijn Coenen struct binder_thread *thread = binder_select_thread_ilocked(proc);
671408c68b1SMartijn Coenen
672408c68b1SMartijn Coenen binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
673408c68b1SMartijn Coenen }
674408c68b1SMartijn Coenen
binder_set_nice(long nice)675777783e0SGreg Kroah-Hartman static void binder_set_nice(long nice)
676777783e0SGreg Kroah-Hartman {
677777783e0SGreg Kroah-Hartman long min_nice;
678777783e0SGreg Kroah-Hartman
679777783e0SGreg Kroah-Hartman if (can_nice(current, nice)) {
680777783e0SGreg Kroah-Hartman set_user_nice(current, nice);
681777783e0SGreg Kroah-Hartman return;
682777783e0SGreg Kroah-Hartman }
683c3643b69SKrzysztof Opasiak min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE));
684777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_PRIORITY_CAP,
685777783e0SGreg Kroah-Hartman "%d: nice value %ld not allowed use %ld instead\n",
686777783e0SGreg Kroah-Hartman current->pid, nice, min_nice);
687777783e0SGreg Kroah-Hartman set_user_nice(current, min_nice);
688777783e0SGreg Kroah-Hartman if (min_nice <= MAX_NICE)
689777783e0SGreg Kroah-Hartman return;
690777783e0SGreg Kroah-Hartman binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
691777783e0SGreg Kroah-Hartman }
692777783e0SGreg Kroah-Hartman
binder_get_node_ilocked(struct binder_proc * proc,binder_uintptr_t ptr)693da0fa9e4STodd Kjos static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
694777783e0SGreg Kroah-Hartman binder_uintptr_t ptr)
695777783e0SGreg Kroah-Hartman {
696777783e0SGreg Kroah-Hartman struct rb_node *n = proc->nodes.rb_node;
697777783e0SGreg Kroah-Hartman struct binder_node *node;
698777783e0SGreg Kroah-Hartman
699858b2719SMartijn Coenen assert_spin_locked(&proc->inner_lock);
700da0fa9e4STodd Kjos
701777783e0SGreg Kroah-Hartman while (n) {
702777783e0SGreg Kroah-Hartman node = rb_entry(n, struct binder_node, rb_node);
703777783e0SGreg Kroah-Hartman
704777783e0SGreg Kroah-Hartman if (ptr < node->ptr)
705777783e0SGreg Kroah-Hartman n = n->rb_left;
706777783e0SGreg Kroah-Hartman else if (ptr > node->ptr)
707777783e0SGreg Kroah-Hartman n = n->rb_right;
708adc18842STodd Kjos else {
709adc18842STodd Kjos /*
710adc18842STodd Kjos * take an implicit weak reference
711adc18842STodd Kjos * to ensure node stays alive until
712adc18842STodd Kjos * call to binder_put_node()
713adc18842STodd Kjos */
714da0fa9e4STodd Kjos binder_inc_node_tmpref_ilocked(node);
715777783e0SGreg Kroah-Hartman return node;
716777783e0SGreg Kroah-Hartman }
717adc18842STodd Kjos }
718777783e0SGreg Kroah-Hartman return NULL;
719777783e0SGreg Kroah-Hartman }
720777783e0SGreg Kroah-Hartman
binder_get_node(struct binder_proc * proc,binder_uintptr_t ptr)721da0fa9e4STodd Kjos static struct binder_node *binder_get_node(struct binder_proc *proc,
722da0fa9e4STodd Kjos binder_uintptr_t ptr)
723da0fa9e4STodd Kjos {
724da0fa9e4STodd Kjos struct binder_node *node;
725da0fa9e4STodd Kjos
726da0fa9e4STodd Kjos binder_inner_proc_lock(proc);
727da0fa9e4STodd Kjos node = binder_get_node_ilocked(proc, ptr);
728da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
729da0fa9e4STodd Kjos return node;
730da0fa9e4STodd Kjos }
731da0fa9e4STodd Kjos
binder_init_node_ilocked(struct binder_proc * proc,struct binder_node * new_node,struct flat_binder_object * fp)732da0fa9e4STodd Kjos static struct binder_node *binder_init_node_ilocked(
733da0fa9e4STodd Kjos struct binder_proc *proc,
734da0fa9e4STodd Kjos struct binder_node *new_node,
735673068eeSTodd Kjos struct flat_binder_object *fp)
736777783e0SGreg Kroah-Hartman {
737777783e0SGreg Kroah-Hartman struct rb_node **p = &proc->nodes.rb_node;
738777783e0SGreg Kroah-Hartman struct rb_node *parent = NULL;
739777783e0SGreg Kroah-Hartman struct binder_node *node;
740673068eeSTodd Kjos binder_uintptr_t ptr = fp ? fp->binder : 0;
741673068eeSTodd Kjos binder_uintptr_t cookie = fp ? fp->cookie : 0;
742673068eeSTodd Kjos __u32 flags = fp ? fp->flags : 0;
743777783e0SGreg Kroah-Hartman
744858b2719SMartijn Coenen assert_spin_locked(&proc->inner_lock);
745858b2719SMartijn Coenen
746777783e0SGreg Kroah-Hartman while (*p) {
747da0fa9e4STodd Kjos
748777783e0SGreg Kroah-Hartman parent = *p;
749777783e0SGreg Kroah-Hartman node = rb_entry(parent, struct binder_node, rb_node);
750777783e0SGreg Kroah-Hartman
751777783e0SGreg Kroah-Hartman if (ptr < node->ptr)
752777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
753777783e0SGreg Kroah-Hartman else if (ptr > node->ptr)
754777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
755da0fa9e4STodd Kjos else {
756da0fa9e4STodd Kjos /*
757da0fa9e4STodd Kjos * A matching node is already in
758da0fa9e4STodd Kjos * the rb tree. Abandon the init
759da0fa9e4STodd Kjos * and return it.
760da0fa9e4STodd Kjos */
761da0fa9e4STodd Kjos binder_inc_node_tmpref_ilocked(node);
762da0fa9e4STodd Kjos return node;
763777783e0SGreg Kroah-Hartman }
764da0fa9e4STodd Kjos }
765da0fa9e4STodd Kjos node = new_node;
766777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_NODE);
767adc18842STodd Kjos node->tmp_refs++;
768777783e0SGreg Kroah-Hartman rb_link_node(&node->rb_node, parent, p);
769777783e0SGreg Kroah-Hartman rb_insert_color(&node->rb_node, &proc->nodes);
770656a800aSTodd Kjos node->debug_id = atomic_inc_return(&binder_last_id);
771777783e0SGreg Kroah-Hartman node->proc = proc;
772777783e0SGreg Kroah-Hartman node->ptr = ptr;
773777783e0SGreg Kroah-Hartman node->cookie = cookie;
774777783e0SGreg Kroah-Hartman node->work.type = BINDER_WORK_NODE;
775673068eeSTodd Kjos node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
776673068eeSTodd Kjos node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
777ec74136dSTodd Kjos node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
7789630fe88STodd Kjos spin_lock_init(&node->lock);
779777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&node->work.entry);
780777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&node->async_todo);
781777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
782777783e0SGreg Kroah-Hartman "%d:%d node %d u%016llx c%016llx created\n",
783777783e0SGreg Kroah-Hartman proc->pid, current->pid, node->debug_id,
784777783e0SGreg Kroah-Hartman (u64)node->ptr, (u64)node->cookie);
785da0fa9e4STodd Kjos
786da0fa9e4STodd Kjos return node;
787da0fa9e4STodd Kjos }
788da0fa9e4STodd Kjos
binder_new_node(struct binder_proc * proc,struct flat_binder_object * fp)789da0fa9e4STodd Kjos static struct binder_node *binder_new_node(struct binder_proc *proc,
790da0fa9e4STodd Kjos struct flat_binder_object *fp)
791da0fa9e4STodd Kjos {
792da0fa9e4STodd Kjos struct binder_node *node;
793da0fa9e4STodd Kjos struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
794da0fa9e4STodd Kjos
795da0fa9e4STodd Kjos if (!new_node)
796da0fa9e4STodd Kjos return NULL;
797da0fa9e4STodd Kjos binder_inner_proc_lock(proc);
798da0fa9e4STodd Kjos node = binder_init_node_ilocked(proc, new_node, fp);
799da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
800da0fa9e4STodd Kjos if (node != new_node)
801da0fa9e4STodd Kjos /*
802da0fa9e4STodd Kjos * The node was already added by another thread
803da0fa9e4STodd Kjos */
804da0fa9e4STodd Kjos kfree(new_node);
805da0fa9e4STodd Kjos
806777783e0SGreg Kroah-Hartman return node;
807777783e0SGreg Kroah-Hartman }
808777783e0SGreg Kroah-Hartman
binder_free_node(struct binder_node * node)809ed29721eSTodd Kjos static void binder_free_node(struct binder_node *node)
810ed29721eSTodd Kjos {
811ed29721eSTodd Kjos kfree(node);
812ed29721eSTodd Kjos binder_stats_deleted(BINDER_STAT_NODE);
813ed29721eSTodd Kjos }
814ed29721eSTodd Kjos
binder_inc_node_nilocked(struct binder_node * node,int strong,int internal,struct list_head * target_list)815673068eeSTodd Kjos static int binder_inc_node_nilocked(struct binder_node *node, int strong,
816ed29721eSTodd Kjos int internal,
817777783e0SGreg Kroah-Hartman struct list_head *target_list)
818777783e0SGreg Kroah-Hartman {
819673068eeSTodd Kjos struct binder_proc *proc = node->proc;
820673068eeSTodd Kjos
821858b2719SMartijn Coenen assert_spin_locked(&node->lock);
822673068eeSTodd Kjos if (proc)
823858b2719SMartijn Coenen assert_spin_locked(&proc->inner_lock);
824777783e0SGreg Kroah-Hartman if (strong) {
825777783e0SGreg Kroah-Hartman if (internal) {
826777783e0SGreg Kroah-Hartman if (target_list == NULL &&
827777783e0SGreg Kroah-Hartman node->internal_strong_refs == 0 &&
828342e5c90SMartijn Coenen !(node->proc &&
829342e5c90SMartijn Coenen node == node->proc->context->binder_context_mgr_node &&
830777783e0SGreg Kroah-Hartman node->has_strong_ref)) {
831777783e0SGreg Kroah-Hartman pr_err("invalid inc strong node for %d\n",
832777783e0SGreg Kroah-Hartman node->debug_id);
833777783e0SGreg Kroah-Hartman return -EINVAL;
834777783e0SGreg Kroah-Hartman }
835777783e0SGreg Kroah-Hartman node->internal_strong_refs++;
836777783e0SGreg Kroah-Hartman } else
837777783e0SGreg Kroah-Hartman node->local_strong_refs++;
838777783e0SGreg Kroah-Hartman if (!node->has_strong_ref && target_list) {
83944b73962SSherry Yang struct binder_thread *thread = container_of(target_list,
84044b73962SSherry Yang struct binder_thread, todo);
84172196393STodd Kjos binder_dequeue_work_ilocked(&node->work);
84244b73962SSherry Yang BUG_ON(&thread->todo != target_list);
84344b73962SSherry Yang binder_enqueue_deferred_thread_work_ilocked(thread,
84444b73962SSherry Yang &node->work);
845777783e0SGreg Kroah-Hartman }
846777783e0SGreg Kroah-Hartman } else {
847777783e0SGreg Kroah-Hartman if (!internal)
848777783e0SGreg Kroah-Hartman node->local_weak_refs++;
849777783e0SGreg Kroah-Hartman if (!node->has_weak_ref && list_empty(&node->work.entry)) {
850777783e0SGreg Kroah-Hartman if (target_list == NULL) {
851777783e0SGreg Kroah-Hartman pr_err("invalid inc weak node for %d\n",
852777783e0SGreg Kroah-Hartman node->debug_id);
853777783e0SGreg Kroah-Hartman return -EINVAL;
854777783e0SGreg Kroah-Hartman }
855148ade2cSMartijn Coenen /*
856148ade2cSMartijn Coenen * See comment above
857148ade2cSMartijn Coenen */
85872196393STodd Kjos binder_enqueue_work_ilocked(&node->work, target_list);
859777783e0SGreg Kroah-Hartman }
860777783e0SGreg Kroah-Hartman }
861777783e0SGreg Kroah-Hartman return 0;
862777783e0SGreg Kroah-Hartman }
863777783e0SGreg Kroah-Hartman
binder_inc_node(struct binder_node * node,int strong,int internal,struct list_head * target_list)864ed29721eSTodd Kjos static int binder_inc_node(struct binder_node *node, int strong, int internal,
865ed29721eSTodd Kjos struct list_head *target_list)
866777783e0SGreg Kroah-Hartman {
867ed29721eSTodd Kjos int ret;
868ed29721eSTodd Kjos
869673068eeSTodd Kjos binder_node_inner_lock(node);
870673068eeSTodd Kjos ret = binder_inc_node_nilocked(node, strong, internal, target_list);
871673068eeSTodd Kjos binder_node_inner_unlock(node);
872ed29721eSTodd Kjos
873ed29721eSTodd Kjos return ret;
874ed29721eSTodd Kjos }
875ed29721eSTodd Kjos
binder_dec_node_nilocked(struct binder_node * node,int strong,int internal)876673068eeSTodd Kjos static bool binder_dec_node_nilocked(struct binder_node *node,
877ed29721eSTodd Kjos int strong, int internal)
878ed29721eSTodd Kjos {
879ed29721eSTodd Kjos struct binder_proc *proc = node->proc;
880ed29721eSTodd Kjos
881858b2719SMartijn Coenen assert_spin_locked(&node->lock);
882ed29721eSTodd Kjos if (proc)
883858b2719SMartijn Coenen assert_spin_locked(&proc->inner_lock);
884777783e0SGreg Kroah-Hartman if (strong) {
885777783e0SGreg Kroah-Hartman if (internal)
886777783e0SGreg Kroah-Hartman node->internal_strong_refs--;
887777783e0SGreg Kroah-Hartman else
888777783e0SGreg Kroah-Hartman node->local_strong_refs--;
889777783e0SGreg Kroah-Hartman if (node->local_strong_refs || node->internal_strong_refs)
890ed29721eSTodd Kjos return false;
891777783e0SGreg Kroah-Hartman } else {
892777783e0SGreg Kroah-Hartman if (!internal)
893777783e0SGreg Kroah-Hartman node->local_weak_refs--;
894adc18842STodd Kjos if (node->local_weak_refs || node->tmp_refs ||
895adc18842STodd Kjos !hlist_empty(&node->refs))
896ed29721eSTodd Kjos return false;
897777783e0SGreg Kroah-Hartman }
898ed29721eSTodd Kjos
899ed29721eSTodd Kjos if (proc && (node->has_strong_ref || node->has_weak_ref)) {
900777783e0SGreg Kroah-Hartman if (list_empty(&node->work.entry)) {
90172196393STodd Kjos binder_enqueue_work_ilocked(&node->work, &proc->todo);
902408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(proc);
903777783e0SGreg Kroah-Hartman }
904777783e0SGreg Kroah-Hartman } else {
905777783e0SGreg Kroah-Hartman if (hlist_empty(&node->refs) && !node->local_strong_refs &&
906adc18842STodd Kjos !node->local_weak_refs && !node->tmp_refs) {
907ed29721eSTodd Kjos if (proc) {
90872196393STodd Kjos binder_dequeue_work_ilocked(&node->work);
90972196393STodd Kjos rb_erase(&node->rb_node, &proc->nodes);
910777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
911777783e0SGreg Kroah-Hartman "refless node %d deleted\n",
912777783e0SGreg Kroah-Hartman node->debug_id);
913777783e0SGreg Kroah-Hartman } else {
91472196393STodd Kjos BUG_ON(!list_empty(&node->work.entry));
915c44b1231STodd Kjos spin_lock(&binder_dead_nodes_lock);
916ed29721eSTodd Kjos /*
917ed29721eSTodd Kjos * tmp_refs could have changed so
918ed29721eSTodd Kjos * check it again
919ed29721eSTodd Kjos */
920ed29721eSTodd Kjos if (node->tmp_refs) {
921ed29721eSTodd Kjos spin_unlock(&binder_dead_nodes_lock);
922ed29721eSTodd Kjos return false;
923ed29721eSTodd Kjos }
924777783e0SGreg Kroah-Hartman hlist_del(&node->dead_node);
925c44b1231STodd Kjos spin_unlock(&binder_dead_nodes_lock);
926777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
927777783e0SGreg Kroah-Hartman "dead node %d deleted\n",
928777783e0SGreg Kroah-Hartman node->debug_id);
929777783e0SGreg Kroah-Hartman }
930ed29721eSTodd Kjos return true;
931777783e0SGreg Kroah-Hartman }
932777783e0SGreg Kroah-Hartman }
933ed29721eSTodd Kjos return false;
934ed29721eSTodd Kjos }
935777783e0SGreg Kroah-Hartman
binder_dec_node(struct binder_node * node,int strong,int internal)936ed29721eSTodd Kjos static void binder_dec_node(struct binder_node *node, int strong, int internal)
937ed29721eSTodd Kjos {
938ed29721eSTodd Kjos bool free_node;
939ed29721eSTodd Kjos
940673068eeSTodd Kjos binder_node_inner_lock(node);
941673068eeSTodd Kjos free_node = binder_dec_node_nilocked(node, strong, internal);
942673068eeSTodd Kjos binder_node_inner_unlock(node);
943ed29721eSTodd Kjos if (free_node)
944ed29721eSTodd Kjos binder_free_node(node);
945777783e0SGreg Kroah-Hartman }
946777783e0SGreg Kroah-Hartman
binder_inc_node_tmpref_ilocked(struct binder_node * node)947ed29721eSTodd Kjos static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
948adc18842STodd Kjos {
949adc18842STodd Kjos /*
950adc18842STodd Kjos * No call to binder_inc_node() is needed since we
951adc18842STodd Kjos * don't need to inform userspace of any changes to
952adc18842STodd Kjos * tmp_refs
953adc18842STodd Kjos */
954adc18842STodd Kjos node->tmp_refs++;
955adc18842STodd Kjos }
956adc18842STodd Kjos
957adc18842STodd Kjos /**
958ed29721eSTodd Kjos * binder_inc_node_tmpref() - take a temporary reference on node
959ed29721eSTodd Kjos * @node: node to reference
960ed29721eSTodd Kjos *
961ed29721eSTodd Kjos * Take reference on node to prevent the node from being freed
962ed29721eSTodd Kjos * while referenced only by a local variable. The inner lock is
963ed29721eSTodd Kjos * needed to serialize with the node work on the queue (which
964ed29721eSTodd Kjos * isn't needed after the node is dead). If the node is dead
965ed29721eSTodd Kjos * (node->proc is NULL), use binder_dead_nodes_lock to protect
966ed29721eSTodd Kjos * node->tmp_refs against dead-node-only cases where the node
967ed29721eSTodd Kjos * lock cannot be acquired (eg traversing the dead node list to
968ed29721eSTodd Kjos * print nodes)
969ed29721eSTodd Kjos */
binder_inc_node_tmpref(struct binder_node * node)970ed29721eSTodd Kjos static void binder_inc_node_tmpref(struct binder_node *node)
971ed29721eSTodd Kjos {
972673068eeSTodd Kjos binder_node_lock(node);
973ed29721eSTodd Kjos if (node->proc)
974ed29721eSTodd Kjos binder_inner_proc_lock(node->proc);
975ed29721eSTodd Kjos else
976ed29721eSTodd Kjos spin_lock(&binder_dead_nodes_lock);
977ed29721eSTodd Kjos binder_inc_node_tmpref_ilocked(node);
978ed29721eSTodd Kjos if (node->proc)
979ed29721eSTodd Kjos binder_inner_proc_unlock(node->proc);
980ed29721eSTodd Kjos else
981ed29721eSTodd Kjos spin_unlock(&binder_dead_nodes_lock);
982673068eeSTodd Kjos binder_node_unlock(node);
983ed29721eSTodd Kjos }
984ed29721eSTodd Kjos
985ed29721eSTodd Kjos /**
986adc18842STodd Kjos * binder_dec_node_tmpref() - remove a temporary reference on node
987adc18842STodd Kjos * @node: node to reference
988adc18842STodd Kjos *
989adc18842STodd Kjos * Release temporary reference on node taken via binder_inc_node_tmpref()
990adc18842STodd Kjos */
binder_dec_node_tmpref(struct binder_node * node)991adc18842STodd Kjos static void binder_dec_node_tmpref(struct binder_node *node)
992adc18842STodd Kjos {
993ed29721eSTodd Kjos bool free_node;
994ed29721eSTodd Kjos
995673068eeSTodd Kjos binder_node_inner_lock(node);
996673068eeSTodd Kjos if (!node->proc)
997ed29721eSTodd Kjos spin_lock(&binder_dead_nodes_lock);
998324fa64cSTodd Kjos else
999324fa64cSTodd Kjos __acquire(&binder_dead_nodes_lock);
1000adc18842STodd Kjos node->tmp_refs--;
1001adc18842STodd Kjos BUG_ON(node->tmp_refs < 0);
1002ed29721eSTodd Kjos if (!node->proc)
1003ed29721eSTodd Kjos spin_unlock(&binder_dead_nodes_lock);
1004324fa64cSTodd Kjos else
1005324fa64cSTodd Kjos __release(&binder_dead_nodes_lock);
1006adc18842STodd Kjos /*
1007adc18842STodd Kjos * Call binder_dec_node() to check if all refcounts are 0
1008adc18842STodd Kjos * and cleanup is needed. Calling with strong=0 and internal=1
1009adc18842STodd Kjos * causes no actual reference to be released in binder_dec_node().
1010adc18842STodd Kjos * If that changes, a change is needed here too.
1011adc18842STodd Kjos */
1012673068eeSTodd Kjos free_node = binder_dec_node_nilocked(node, 0, 1);
1013673068eeSTodd Kjos binder_node_inner_unlock(node);
1014ed29721eSTodd Kjos if (free_node)
1015ed29721eSTodd Kjos binder_free_node(node);
1016adc18842STodd Kjos }
1017adc18842STodd Kjos
binder_put_node(struct binder_node * node)1018adc18842STodd Kjos static void binder_put_node(struct binder_node *node)
1019adc18842STodd Kjos {
1020adc18842STodd Kjos binder_dec_node_tmpref(node);
1021adc18842STodd Kjos }
1022777783e0SGreg Kroah-Hartman
binder_get_ref_olocked(struct binder_proc * proc,u32 desc,bool need_strong_ref)10232c1838dcSTodd Kjos static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
10240a3ffab9SArve Hjønnevåg u32 desc, bool need_strong_ref)
1025777783e0SGreg Kroah-Hartman {
1026777783e0SGreg Kroah-Hartman struct rb_node *n = proc->refs_by_desc.rb_node;
1027777783e0SGreg Kroah-Hartman struct binder_ref *ref;
1028777783e0SGreg Kroah-Hartman
1029777783e0SGreg Kroah-Hartman while (n) {
1030777783e0SGreg Kroah-Hartman ref = rb_entry(n, struct binder_ref, rb_node_desc);
1031777783e0SGreg Kroah-Hartman
1032372e3147STodd Kjos if (desc < ref->data.desc) {
1033777783e0SGreg Kroah-Hartman n = n->rb_left;
1034372e3147STodd Kjos } else if (desc > ref->data.desc) {
1035777783e0SGreg Kroah-Hartman n = n->rb_right;
1036372e3147STodd Kjos } else if (need_strong_ref && !ref->data.strong) {
10370a3ffab9SArve Hjønnevåg binder_user_error("tried to use weak ref as strong ref\n");
10380a3ffab9SArve Hjønnevåg return NULL;
10390a3ffab9SArve Hjønnevåg } else {
1040777783e0SGreg Kroah-Hartman return ref;
1041777783e0SGreg Kroah-Hartman }
10420a3ffab9SArve Hjønnevåg }
1043777783e0SGreg Kroah-Hartman return NULL;
1044777783e0SGreg Kroah-Hartman }
1045777783e0SGreg Kroah-Hartman
1046372e3147STodd Kjos /**
10472c1838dcSTodd Kjos * binder_get_ref_for_node_olocked() - get the ref associated with given node
1048372e3147STodd Kjos * @proc: binder_proc that owns the ref
1049372e3147STodd Kjos * @node: binder_node of target
1050372e3147STodd Kjos * @new_ref: newly allocated binder_ref to be initialized or %NULL
1051372e3147STodd Kjos *
1052372e3147STodd Kjos * Look up the ref for the given node and return it if it exists
1053372e3147STodd Kjos *
1054372e3147STodd Kjos * If it doesn't exist and the caller provides a newly allocated
1055372e3147STodd Kjos * ref, initialize the fields of the newly allocated ref and insert
1056372e3147STodd Kjos * into the given proc rb_trees and node refs list.
1057372e3147STodd Kjos *
1058372e3147STodd Kjos * Return: the ref for node. It is possible that another thread
1059372e3147STodd Kjos * allocated/initialized the ref first in which case the
1060372e3147STodd Kjos * returned ref would be different than the passed-in
1061372e3147STodd Kjos * new_ref. new_ref must be kfree'd by the caller in
1062372e3147STodd Kjos * this case.
1063372e3147STodd Kjos */
binder_get_ref_for_node_olocked(struct binder_proc * proc,struct binder_node * node,struct binder_ref * new_ref)10642c1838dcSTodd Kjos static struct binder_ref *binder_get_ref_for_node_olocked(
10652c1838dcSTodd Kjos struct binder_proc *proc,
1066372e3147STodd Kjos struct binder_node *node,
1067372e3147STodd Kjos struct binder_ref *new_ref)
1068777783e0SGreg Kroah-Hartman {
1069372e3147STodd Kjos struct binder_context *context = proc->context;
1070777783e0SGreg Kroah-Hartman struct rb_node **p = &proc->refs_by_node.rb_node;
1071777783e0SGreg Kroah-Hartman struct rb_node *parent = NULL;
1072372e3147STodd Kjos struct binder_ref *ref;
1073372e3147STodd Kjos struct rb_node *n;
1074777783e0SGreg Kroah-Hartman
1075777783e0SGreg Kroah-Hartman while (*p) {
1076777783e0SGreg Kroah-Hartman parent = *p;
1077777783e0SGreg Kroah-Hartman ref = rb_entry(parent, struct binder_ref, rb_node_node);
1078777783e0SGreg Kroah-Hartman
1079777783e0SGreg Kroah-Hartman if (node < ref->node)
1080777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
1081777783e0SGreg Kroah-Hartman else if (node > ref->node)
1082777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
1083777783e0SGreg Kroah-Hartman else
1084777783e0SGreg Kroah-Hartman return ref;
1085777783e0SGreg Kroah-Hartman }
1086372e3147STodd Kjos if (!new_ref)
1087777783e0SGreg Kroah-Hartman return NULL;
1088372e3147STodd Kjos
1089777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_REF);
1090372e3147STodd Kjos new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1091777783e0SGreg Kroah-Hartman new_ref->proc = proc;
1092777783e0SGreg Kroah-Hartman new_ref->node = node;
1093777783e0SGreg Kroah-Hartman rb_link_node(&new_ref->rb_node_node, parent, p);
1094777783e0SGreg Kroah-Hartman rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1095777783e0SGreg Kroah-Hartman
1096372e3147STodd Kjos new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
1097777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1098777783e0SGreg Kroah-Hartman ref = rb_entry(n, struct binder_ref, rb_node_desc);
1099372e3147STodd Kjos if (ref->data.desc > new_ref->data.desc)
1100777783e0SGreg Kroah-Hartman break;
1101372e3147STodd Kjos new_ref->data.desc = ref->data.desc + 1;
1102777783e0SGreg Kroah-Hartman }
1103777783e0SGreg Kroah-Hartman
1104777783e0SGreg Kroah-Hartman p = &proc->refs_by_desc.rb_node;
1105777783e0SGreg Kroah-Hartman while (*p) {
1106777783e0SGreg Kroah-Hartman parent = *p;
1107777783e0SGreg Kroah-Hartman ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1108777783e0SGreg Kroah-Hartman
1109372e3147STodd Kjos if (new_ref->data.desc < ref->data.desc)
1110777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
1111372e3147STodd Kjos else if (new_ref->data.desc > ref->data.desc)
1112777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
1113777783e0SGreg Kroah-Hartman else
1114777783e0SGreg Kroah-Hartman BUG();
1115777783e0SGreg Kroah-Hartman }
1116777783e0SGreg Kroah-Hartman rb_link_node(&new_ref->rb_node_desc, parent, p);
1117777783e0SGreg Kroah-Hartman rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1118673068eeSTodd Kjos
1119673068eeSTodd Kjos binder_node_lock(node);
1120777783e0SGreg Kroah-Hartman hlist_add_head(&new_ref->node_entry, &node->refs);
1121777783e0SGreg Kroah-Hartman
1122777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1123777783e0SGreg Kroah-Hartman "%d new ref %d desc %d for node %d\n",
1124372e3147STodd Kjos proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1125777783e0SGreg Kroah-Hartman node->debug_id);
1126673068eeSTodd Kjos binder_node_unlock(node);
1127777783e0SGreg Kroah-Hartman return new_ref;
1128777783e0SGreg Kroah-Hartman }
1129777783e0SGreg Kroah-Hartman
binder_cleanup_ref_olocked(struct binder_ref * ref)11302c1838dcSTodd Kjos static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1131777783e0SGreg Kroah-Hartman {
1132ed29721eSTodd Kjos bool delete_node = false;
1133ed29721eSTodd Kjos
1134777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1135777783e0SGreg Kroah-Hartman "%d delete ref %d desc %d for node %d\n",
1136372e3147STodd Kjos ref->proc->pid, ref->data.debug_id, ref->data.desc,
1137777783e0SGreg Kroah-Hartman ref->node->debug_id);
1138777783e0SGreg Kroah-Hartman
1139777783e0SGreg Kroah-Hartman rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1140777783e0SGreg Kroah-Hartman rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1141372e3147STodd Kjos
1142673068eeSTodd Kjos binder_node_inner_lock(ref->node);
1143372e3147STodd Kjos if (ref->data.strong)
1144673068eeSTodd Kjos binder_dec_node_nilocked(ref->node, 1, 1);
1145372e3147STodd Kjos
1146777783e0SGreg Kroah-Hartman hlist_del(&ref->node_entry);
1147673068eeSTodd Kjos delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1148673068eeSTodd Kjos binder_node_inner_unlock(ref->node);
1149ed29721eSTodd Kjos /*
1150ed29721eSTodd Kjos * Clear ref->node unless we want the caller to free the node
1151ed29721eSTodd Kjos */
1152ed29721eSTodd Kjos if (!delete_node) {
1153ed29721eSTodd Kjos /*
1154ed29721eSTodd Kjos * The caller uses ref->node to determine
1155ed29721eSTodd Kjos * whether the node needs to be freed. Clear
1156ed29721eSTodd Kjos * it since the node is still alive.
1157ed29721eSTodd Kjos */
1158ed29721eSTodd Kjos ref->node = NULL;
1159ed29721eSTodd Kjos }
1160372e3147STodd Kjos
1161777783e0SGreg Kroah-Hartman if (ref->death) {
1162777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
1163777783e0SGreg Kroah-Hartman "%d delete ref %d desc %d has death notification\n",
1164372e3147STodd Kjos ref->proc->pid, ref->data.debug_id,
1165372e3147STodd Kjos ref->data.desc);
116672196393STodd Kjos binder_dequeue_work(ref->proc, &ref->death->work);
1167777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_DEATH);
1168777783e0SGreg Kroah-Hartman }
1169777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_REF);
1170777783e0SGreg Kroah-Hartman }
1171777783e0SGreg Kroah-Hartman
1172372e3147STodd Kjos /**
11732c1838dcSTodd Kjos * binder_inc_ref_olocked() - increment the ref for given handle
1174372e3147STodd Kjos * @ref: ref to be incremented
1175372e3147STodd Kjos * @strong: if true, strong increment, else weak
1176372e3147STodd Kjos * @target_list: list to queue node work on
1177372e3147STodd Kjos *
11782c1838dcSTodd Kjos * Increment the ref. @ref->proc->outer_lock must be held on entry
1179372e3147STodd Kjos *
1180372e3147STodd Kjos * Return: 0, if successful, else errno
1181372e3147STodd Kjos */
binder_inc_ref_olocked(struct binder_ref * ref,int strong,struct list_head * target_list)11822c1838dcSTodd Kjos static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1183777783e0SGreg Kroah-Hartman struct list_head *target_list)
1184777783e0SGreg Kroah-Hartman {
1185777783e0SGreg Kroah-Hartman int ret;
1186777783e0SGreg Kroah-Hartman
1187777783e0SGreg Kroah-Hartman if (strong) {
1188372e3147STodd Kjos if (ref->data.strong == 0) {
1189777783e0SGreg Kroah-Hartman ret = binder_inc_node(ref->node, 1, 1, target_list);
1190777783e0SGreg Kroah-Hartman if (ret)
1191777783e0SGreg Kroah-Hartman return ret;
1192777783e0SGreg Kroah-Hartman }
1193372e3147STodd Kjos ref->data.strong++;
1194777783e0SGreg Kroah-Hartman } else {
1195372e3147STodd Kjos if (ref->data.weak == 0) {
1196777783e0SGreg Kroah-Hartman ret = binder_inc_node(ref->node, 0, 1, target_list);
1197777783e0SGreg Kroah-Hartman if (ret)
1198777783e0SGreg Kroah-Hartman return ret;
1199777783e0SGreg Kroah-Hartman }
1200372e3147STodd Kjos ref->data.weak++;
1201777783e0SGreg Kroah-Hartman }
1202777783e0SGreg Kroah-Hartman return 0;
1203777783e0SGreg Kroah-Hartman }
1204777783e0SGreg Kroah-Hartman
1205372e3147STodd Kjos /**
1206ad228a34SRandy Dunlap * binder_dec_ref_olocked() - dec the ref for given handle
1207372e3147STodd Kjos * @ref: ref to be decremented
1208372e3147STodd Kjos * @strong: if true, strong decrement, else weak
1209372e3147STodd Kjos *
1210372e3147STodd Kjos * Decrement the ref.
1211372e3147STodd Kjos *
1212ad228a34SRandy Dunlap * Return: %true if ref is cleaned up and ready to be freed.
1213372e3147STodd Kjos */
binder_dec_ref_olocked(struct binder_ref * ref,int strong)12142c1838dcSTodd Kjos static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1215777783e0SGreg Kroah-Hartman {
1216777783e0SGreg Kroah-Hartman if (strong) {
1217372e3147STodd Kjos if (ref->data.strong == 0) {
1218777783e0SGreg Kroah-Hartman binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1219372e3147STodd Kjos ref->proc->pid, ref->data.debug_id,
1220372e3147STodd Kjos ref->data.desc, ref->data.strong,
1221372e3147STodd Kjos ref->data.weak);
1222372e3147STodd Kjos return false;
1223777783e0SGreg Kroah-Hartman }
1224372e3147STodd Kjos ref->data.strong--;
1225ed29721eSTodd Kjos if (ref->data.strong == 0)
1226ed29721eSTodd Kjos binder_dec_node(ref->node, strong, 1);
1227777783e0SGreg Kroah-Hartman } else {
1228372e3147STodd Kjos if (ref->data.weak == 0) {
1229777783e0SGreg Kroah-Hartman binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1230372e3147STodd Kjos ref->proc->pid, ref->data.debug_id,
1231372e3147STodd Kjos ref->data.desc, ref->data.strong,
1232372e3147STodd Kjos ref->data.weak);
1233372e3147STodd Kjos return false;
1234777783e0SGreg Kroah-Hartman }
1235372e3147STodd Kjos ref->data.weak--;
1236777783e0SGreg Kroah-Hartman }
1237372e3147STodd Kjos if (ref->data.strong == 0 && ref->data.weak == 0) {
12382c1838dcSTodd Kjos binder_cleanup_ref_olocked(ref);
1239372e3147STodd Kjos return true;
1240372e3147STodd Kjos }
1241372e3147STodd Kjos return false;
1242372e3147STodd Kjos }
1243372e3147STodd Kjos
1244372e3147STodd Kjos /**
1245372e3147STodd Kjos * binder_get_node_from_ref() - get the node from the given proc/desc
1246372e3147STodd Kjos * @proc: proc containing the ref
1247372e3147STodd Kjos * @desc: the handle associated with the ref
1248372e3147STodd Kjos * @need_strong_ref: if true, only return node if ref is strong
1249372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1250372e3147STodd Kjos *
1251372e3147STodd Kjos * Given a proc and ref handle, return the associated binder_node
1252372e3147STodd Kjos *
1253372e3147STodd Kjos * Return: a binder_node or NULL if not found or not strong when strong required
1254372e3147STodd Kjos */
binder_get_node_from_ref(struct binder_proc * proc,u32 desc,bool need_strong_ref,struct binder_ref_data * rdata)1255372e3147STodd Kjos static struct binder_node *binder_get_node_from_ref(
1256372e3147STodd Kjos struct binder_proc *proc,
1257372e3147STodd Kjos u32 desc, bool need_strong_ref,
1258372e3147STodd Kjos struct binder_ref_data *rdata)
1259372e3147STodd Kjos {
1260372e3147STodd Kjos struct binder_node *node;
1261372e3147STodd Kjos struct binder_ref *ref;
1262372e3147STodd Kjos
12632c1838dcSTodd Kjos binder_proc_lock(proc);
12642c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1265372e3147STodd Kjos if (!ref)
1266372e3147STodd Kjos goto err_no_ref;
1267372e3147STodd Kjos node = ref->node;
1268adc18842STodd Kjos /*
1269adc18842STodd Kjos * Take an implicit reference on the node to ensure
1270adc18842STodd Kjos * it stays alive until the call to binder_put_node()
1271adc18842STodd Kjos */
1272adc18842STodd Kjos binder_inc_node_tmpref(node);
1273372e3147STodd Kjos if (rdata)
1274372e3147STodd Kjos *rdata = ref->data;
12752c1838dcSTodd Kjos binder_proc_unlock(proc);
1276372e3147STodd Kjos
1277372e3147STodd Kjos return node;
1278372e3147STodd Kjos
1279372e3147STodd Kjos err_no_ref:
12802c1838dcSTodd Kjos binder_proc_unlock(proc);
1281372e3147STodd Kjos return NULL;
1282372e3147STodd Kjos }
1283372e3147STodd Kjos
1284372e3147STodd Kjos /**
1285372e3147STodd Kjos * binder_free_ref() - free the binder_ref
1286372e3147STodd Kjos * @ref: ref to free
1287372e3147STodd Kjos *
1288ed29721eSTodd Kjos * Free the binder_ref. Free the binder_node indicated by ref->node
1289ed29721eSTodd Kjos * (if non-NULL) and the binder_ref_death indicated by ref->death.
1290372e3147STodd Kjos */
binder_free_ref(struct binder_ref * ref)1291372e3147STodd Kjos static void binder_free_ref(struct binder_ref *ref)
1292372e3147STodd Kjos {
1293ed29721eSTodd Kjos if (ref->node)
1294ed29721eSTodd Kjos binder_free_node(ref->node);
1295372e3147STodd Kjos kfree(ref->death);
1296372e3147STodd Kjos kfree(ref);
1297372e3147STodd Kjos }
1298372e3147STodd Kjos
1299372e3147STodd Kjos /**
1300372e3147STodd Kjos * binder_update_ref_for_handle() - inc/dec the ref for given handle
1301372e3147STodd Kjos * @proc: proc containing the ref
1302372e3147STodd Kjos * @desc: the handle associated with the ref
1303372e3147STodd Kjos * @increment: true=inc reference, false=dec reference
1304372e3147STodd Kjos * @strong: true=strong reference, false=weak reference
1305372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1306372e3147STodd Kjos *
1307372e3147STodd Kjos * Given a proc and ref handle, increment or decrement the ref
1308372e3147STodd Kjos * according to "increment" arg.
1309372e3147STodd Kjos *
1310372e3147STodd Kjos * Return: 0 if successful, else errno
1311372e3147STodd Kjos */
binder_update_ref_for_handle(struct binder_proc * proc,uint32_t desc,bool increment,bool strong,struct binder_ref_data * rdata)1312372e3147STodd Kjos static int binder_update_ref_for_handle(struct binder_proc *proc,
1313372e3147STodd Kjos uint32_t desc, bool increment, bool strong,
1314372e3147STodd Kjos struct binder_ref_data *rdata)
1315372e3147STodd Kjos {
1316372e3147STodd Kjos int ret = 0;
1317372e3147STodd Kjos struct binder_ref *ref;
1318372e3147STodd Kjos bool delete_ref = false;
1319372e3147STodd Kjos
13202c1838dcSTodd Kjos binder_proc_lock(proc);
13212c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, desc, strong);
1322372e3147STodd Kjos if (!ref) {
1323372e3147STodd Kjos ret = -EINVAL;
1324372e3147STodd Kjos goto err_no_ref;
1325372e3147STodd Kjos }
1326372e3147STodd Kjos if (increment)
13272c1838dcSTodd Kjos ret = binder_inc_ref_olocked(ref, strong, NULL);
1328372e3147STodd Kjos else
13292c1838dcSTodd Kjos delete_ref = binder_dec_ref_olocked(ref, strong);
1330372e3147STodd Kjos
1331372e3147STodd Kjos if (rdata)
1332372e3147STodd Kjos *rdata = ref->data;
13332c1838dcSTodd Kjos binder_proc_unlock(proc);
1334372e3147STodd Kjos
1335372e3147STodd Kjos if (delete_ref)
1336372e3147STodd Kjos binder_free_ref(ref);
1337372e3147STodd Kjos return ret;
1338372e3147STodd Kjos
1339372e3147STodd Kjos err_no_ref:
13402c1838dcSTodd Kjos binder_proc_unlock(proc);
1341372e3147STodd Kjos return ret;
1342372e3147STodd Kjos }
1343372e3147STodd Kjos
1344372e3147STodd Kjos /**
1345372e3147STodd Kjos * binder_dec_ref_for_handle() - dec the ref for given handle
1346372e3147STodd Kjos * @proc: proc containing the ref
1347372e3147STodd Kjos * @desc: the handle associated with the ref
1348372e3147STodd Kjos * @strong: true=strong reference, false=weak reference
1349372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1350372e3147STodd Kjos *
1351372e3147STodd Kjos * Just calls binder_update_ref_for_handle() to decrement the ref.
1352372e3147STodd Kjos *
1353372e3147STodd Kjos * Return: 0 if successful, else errno
1354372e3147STodd Kjos */
binder_dec_ref_for_handle(struct binder_proc * proc,uint32_t desc,bool strong,struct binder_ref_data * rdata)1355372e3147STodd Kjos static int binder_dec_ref_for_handle(struct binder_proc *proc,
1356372e3147STodd Kjos uint32_t desc, bool strong, struct binder_ref_data *rdata)
1357372e3147STodd Kjos {
1358372e3147STodd Kjos return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1359372e3147STodd Kjos }
1360372e3147STodd Kjos
1361372e3147STodd Kjos
1362372e3147STodd Kjos /**
1363372e3147STodd Kjos * binder_inc_ref_for_node() - increment the ref for given proc/node
1364372e3147STodd Kjos * @proc: proc containing the ref
1365372e3147STodd Kjos * @node: target node
1366372e3147STodd Kjos * @strong: true=strong reference, false=weak reference
1367372e3147STodd Kjos * @target_list: worklist to use if node is incremented
1368372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1369372e3147STodd Kjos *
1370372e3147STodd Kjos * Given a proc and node, increment the ref. Create the ref if it
1371372e3147STodd Kjos * doesn't already exist
1372372e3147STodd Kjos *
1373372e3147STodd Kjos * Return: 0 if successful, else errno
1374372e3147STodd Kjos */
binder_inc_ref_for_node(struct binder_proc * proc,struct binder_node * node,bool strong,struct list_head * target_list,struct binder_ref_data * rdata)1375372e3147STodd Kjos static int binder_inc_ref_for_node(struct binder_proc *proc,
1376372e3147STodd Kjos struct binder_node *node,
1377372e3147STodd Kjos bool strong,
1378372e3147STodd Kjos struct list_head *target_list,
1379372e3147STodd Kjos struct binder_ref_data *rdata)
1380372e3147STodd Kjos {
1381372e3147STodd Kjos struct binder_ref *ref;
1382372e3147STodd Kjos struct binder_ref *new_ref = NULL;
1383372e3147STodd Kjos int ret = 0;
1384372e3147STodd Kjos
13852c1838dcSTodd Kjos binder_proc_lock(proc);
13862c1838dcSTodd Kjos ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1387372e3147STodd Kjos if (!ref) {
13882c1838dcSTodd Kjos binder_proc_unlock(proc);
1389372e3147STodd Kjos new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1390372e3147STodd Kjos if (!new_ref)
1391372e3147STodd Kjos return -ENOMEM;
13922c1838dcSTodd Kjos binder_proc_lock(proc);
13932c1838dcSTodd Kjos ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1394372e3147STodd Kjos }
13952c1838dcSTodd Kjos ret = binder_inc_ref_olocked(ref, strong, target_list);
1396372e3147STodd Kjos *rdata = ref->data;
1397a0e44c64SCarlos Llamas if (ret && ref == new_ref) {
1398a0e44c64SCarlos Llamas /*
1399a0e44c64SCarlos Llamas * Cleanup the failed reference here as the target
1400a0e44c64SCarlos Llamas * could now be dead and have already released its
1401a0e44c64SCarlos Llamas * references by now. Calling on the new reference
1402a0e44c64SCarlos Llamas * with strong=0 and a tmp_refs will not decrement
1403a0e44c64SCarlos Llamas * the node. The new_ref gets kfree'd below.
1404a0e44c64SCarlos Llamas */
1405a0e44c64SCarlos Llamas binder_cleanup_ref_olocked(new_ref);
1406a0e44c64SCarlos Llamas ref = NULL;
1407a0e44c64SCarlos Llamas }
1408a0e44c64SCarlos Llamas
14092c1838dcSTodd Kjos binder_proc_unlock(proc);
1410372e3147STodd Kjos if (new_ref && ref != new_ref)
1411372e3147STodd Kjos /*
1412372e3147STodd Kjos * Another thread created the ref first so
1413372e3147STodd Kjos * free the one we allocated
1414372e3147STodd Kjos */
1415372e3147STodd Kjos kfree(new_ref);
1416372e3147STodd Kjos return ret;
1417777783e0SGreg Kroah-Hartman }
1418777783e0SGreg Kroah-Hartman
binder_pop_transaction_ilocked(struct binder_thread * target_thread,struct binder_transaction * t)14190b89d69aSMartijn Coenen static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1420777783e0SGreg Kroah-Hartman struct binder_transaction *t)
1421777783e0SGreg Kroah-Hartman {
1422b6d282ceSTodd Kjos BUG_ON(!target_thread);
1423858b2719SMartijn Coenen assert_spin_locked(&target_thread->proc->inner_lock);
1424777783e0SGreg Kroah-Hartman BUG_ON(target_thread->transaction_stack != t);
1425777783e0SGreg Kroah-Hartman BUG_ON(target_thread->transaction_stack->from != target_thread);
1426777783e0SGreg Kroah-Hartman target_thread->transaction_stack =
1427777783e0SGreg Kroah-Hartman target_thread->transaction_stack->from_parent;
1428777783e0SGreg Kroah-Hartman t->from = NULL;
1429777783e0SGreg Kroah-Hartman }
1430b6d282ceSTodd Kjos
14317a4408c6STodd Kjos /**
14327a4408c6STodd Kjos * binder_thread_dec_tmpref() - decrement thread->tmp_ref
14337a4408c6STodd Kjos * @thread: thread to decrement
14347a4408c6STodd Kjos *
14357a4408c6STodd Kjos * A thread needs to be kept alive while being used to create or
14367a4408c6STodd Kjos * handle a transaction. binder_get_txn_from() is used to safely
14377a4408c6STodd Kjos * extract t->from from a binder_transaction and keep the thread
14387a4408c6STodd Kjos * indicated by t->from from being freed. When done with that
14397a4408c6STodd Kjos * binder_thread, this function is called to decrement the
14407a4408c6STodd Kjos * tmp_ref and free if appropriate (thread has been released
14417a4408c6STodd Kjos * and no transaction being processed by the driver)
14427a4408c6STodd Kjos */
binder_thread_dec_tmpref(struct binder_thread * thread)14437a4408c6STodd Kjos static void binder_thread_dec_tmpref(struct binder_thread *thread)
14447a4408c6STodd Kjos {
14457a4408c6STodd Kjos /*
14467a4408c6STodd Kjos * atomic is used to protect the counter value while
14477a4408c6STodd Kjos * it cannot reach zero or thread->is_dead is false
14487a4408c6STodd Kjos */
14497bd7b0e6STodd Kjos binder_inner_proc_lock(thread->proc);
14507a4408c6STodd Kjos atomic_dec(&thread->tmp_ref);
14517a4408c6STodd Kjos if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
14527bd7b0e6STodd Kjos binder_inner_proc_unlock(thread->proc);
14537a4408c6STodd Kjos binder_free_thread(thread);
14547a4408c6STodd Kjos return;
14557a4408c6STodd Kjos }
14567bd7b0e6STodd Kjos binder_inner_proc_unlock(thread->proc);
14577a4408c6STodd Kjos }
14587a4408c6STodd Kjos
14597a4408c6STodd Kjos /**
14607a4408c6STodd Kjos * binder_proc_dec_tmpref() - decrement proc->tmp_ref
14617a4408c6STodd Kjos * @proc: proc to decrement
14627a4408c6STodd Kjos *
14637a4408c6STodd Kjos * A binder_proc needs to be kept alive while being used to create or
14647a4408c6STodd Kjos * handle a transaction. proc->tmp_ref is incremented when
14657a4408c6STodd Kjos * creating a new transaction or the binder_proc is currently in-use
14667a4408c6STodd Kjos * by threads that are being released. When done with the binder_proc,
14677a4408c6STodd Kjos * this function is called to decrement the counter and free the
14687a4408c6STodd Kjos * proc if appropriate (proc has been released, all threads have
14697a4408c6STodd Kjos * been released and not currenly in-use to process a transaction).
14707a4408c6STodd Kjos */
binder_proc_dec_tmpref(struct binder_proc * proc)14717a4408c6STodd Kjos static void binder_proc_dec_tmpref(struct binder_proc *proc)
14727a4408c6STodd Kjos {
14737bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
14747a4408c6STodd Kjos proc->tmp_ref--;
14757a4408c6STodd Kjos if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
14767a4408c6STodd Kjos !proc->tmp_ref) {
14777bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
14787a4408c6STodd Kjos binder_free_proc(proc);
14797a4408c6STodd Kjos return;
14807a4408c6STodd Kjos }
14817bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
14827a4408c6STodd Kjos }
14837a4408c6STodd Kjos
14847a4408c6STodd Kjos /**
14857a4408c6STodd Kjos * binder_get_txn_from() - safely extract the "from" thread in transaction
14867a4408c6STodd Kjos * @t: binder transaction for t->from
14877a4408c6STodd Kjos *
14887a4408c6STodd Kjos * Atomically return the "from" thread and increment the tmp_ref
14897a4408c6STodd Kjos * count for the thread to ensure it stays alive until
14907a4408c6STodd Kjos * binder_thread_dec_tmpref() is called.
14917a4408c6STodd Kjos *
14927a4408c6STodd Kjos * Return: the value of t->from
14937a4408c6STodd Kjos */
binder_get_txn_from(struct binder_transaction * t)14947a4408c6STodd Kjos static struct binder_thread *binder_get_txn_from(
14957a4408c6STodd Kjos struct binder_transaction *t)
14967a4408c6STodd Kjos {
14977a4408c6STodd Kjos struct binder_thread *from;
14987a4408c6STodd Kjos
14997a4408c6STodd Kjos spin_lock(&t->lock);
15007a4408c6STodd Kjos from = t->from;
15017a4408c6STodd Kjos if (from)
15027a4408c6STodd Kjos atomic_inc(&from->tmp_ref);
15037a4408c6STodd Kjos spin_unlock(&t->lock);
15047a4408c6STodd Kjos return from;
15057a4408c6STodd Kjos }
15067a4408c6STodd Kjos
15070b89d69aSMartijn Coenen /**
15080b89d69aSMartijn Coenen * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
15090b89d69aSMartijn Coenen * @t: binder transaction for t->from
15100b89d69aSMartijn Coenen *
15110b89d69aSMartijn Coenen * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
15120b89d69aSMartijn Coenen * to guarantee that the thread cannot be released while operating on it.
15130b89d69aSMartijn Coenen * The caller must call binder_inner_proc_unlock() to release the inner lock
15140b89d69aSMartijn Coenen * as well as call binder_dec_thread_txn() to release the reference.
15150b89d69aSMartijn Coenen *
15160b89d69aSMartijn Coenen * Return: the value of t->from
15170b89d69aSMartijn Coenen */
binder_get_txn_from_and_acq_inner(struct binder_transaction * t)15180b89d69aSMartijn Coenen static struct binder_thread *binder_get_txn_from_and_acq_inner(
15190b89d69aSMartijn Coenen struct binder_transaction *t)
1520324fa64cSTodd Kjos __acquires(&t->from->proc->inner_lock)
15210b89d69aSMartijn Coenen {
15220b89d69aSMartijn Coenen struct binder_thread *from;
15230b89d69aSMartijn Coenen
15240b89d69aSMartijn Coenen from = binder_get_txn_from(t);
1525324fa64cSTodd Kjos if (!from) {
1526324fa64cSTodd Kjos __acquire(&from->proc->inner_lock);
15270b89d69aSMartijn Coenen return NULL;
1528324fa64cSTodd Kjos }
15290b89d69aSMartijn Coenen binder_inner_proc_lock(from->proc);
15300b89d69aSMartijn Coenen if (t->from) {
15310b89d69aSMartijn Coenen BUG_ON(from != t->from);
15320b89d69aSMartijn Coenen return from;
15330b89d69aSMartijn Coenen }
15340b89d69aSMartijn Coenen binder_inner_proc_unlock(from->proc);
1535324fa64cSTodd Kjos __acquire(&from->proc->inner_lock);
15360b89d69aSMartijn Coenen binder_thread_dec_tmpref(from);
15370b89d69aSMartijn Coenen return NULL;
15380b89d69aSMartijn Coenen }
15390b89d69aSMartijn Coenen
154044d8047fSTodd Kjos /**
154144d8047fSTodd Kjos * binder_free_txn_fixups() - free unprocessed fd fixups
154244d8047fSTodd Kjos * @t: binder transaction for t->from
154344d8047fSTodd Kjos *
154444d8047fSTodd Kjos * If the transaction is being torn down prior to being
154544d8047fSTodd Kjos * processed by the target process, free all of the
154644d8047fSTodd Kjos * fd fixups and fput the file structs. It is safe to
154744d8047fSTodd Kjos * call this function after the fixups have been
154844d8047fSTodd Kjos * processed -- in that case, the list will be empty.
154944d8047fSTodd Kjos */
binder_free_txn_fixups(struct binder_transaction * t)155044d8047fSTodd Kjos static void binder_free_txn_fixups(struct binder_transaction *t)
155144d8047fSTodd Kjos {
155244d8047fSTodd Kjos struct binder_txn_fd_fixup *fixup, *tmp;
155344d8047fSTodd Kjos
155444d8047fSTodd Kjos list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
155544d8047fSTodd Kjos fput(fixup->file);
1556a8a570c6SCarlos Llamas if (fixup->target_fd >= 0)
1557a8a570c6SCarlos Llamas put_unused_fd(fixup->target_fd);
155844d8047fSTodd Kjos list_del(&fixup->fixup_entry);
155944d8047fSTodd Kjos kfree(fixup);
156044d8047fSTodd Kjos }
156144d8047fSTodd Kjos }
156244d8047fSTodd Kjos
binder_txn_latency_free(struct binder_transaction * t)15631987f112SFrankie.Chang static void binder_txn_latency_free(struct binder_transaction *t)
15641987f112SFrankie.Chang {
15651987f112SFrankie.Chang int from_proc, from_thread, to_proc, to_thread;
15661987f112SFrankie.Chang
15671987f112SFrankie.Chang spin_lock(&t->lock);
15681987f112SFrankie.Chang from_proc = t->from ? t->from->proc->pid : 0;
15691987f112SFrankie.Chang from_thread = t->from ? t->from->pid : 0;
15701987f112SFrankie.Chang to_proc = t->to_proc ? t->to_proc->pid : 0;
15711987f112SFrankie.Chang to_thread = t->to_thread ? t->to_thread->pid : 0;
15721987f112SFrankie.Chang spin_unlock(&t->lock);
15731987f112SFrankie.Chang
15741987f112SFrankie.Chang trace_binder_txn_latency_free(t, from_proc, from_thread, to_proc, to_thread);
15751987f112SFrankie.Chang }
15761987f112SFrankie.Chang
binder_free_transaction(struct binder_transaction * t)1577b6d282ceSTodd Kjos static void binder_free_transaction(struct binder_transaction *t)
1578b6d282ceSTodd Kjos {
1579a370003cSTodd Kjos struct binder_proc *target_proc = t->to_proc;
1580a370003cSTodd Kjos
1581a370003cSTodd Kjos if (target_proc) {
1582a370003cSTodd Kjos binder_inner_proc_lock(target_proc);
1583432ff1e9SMarco Ballesio target_proc->outstanding_txns--;
1584432ff1e9SMarco Ballesio if (target_proc->outstanding_txns < 0)
1585432ff1e9SMarco Ballesio pr_warn("%s: Unexpected outstanding_txns %d\n",
1586432ff1e9SMarco Ballesio __func__, target_proc->outstanding_txns);
1587432ff1e9SMarco Ballesio if (!target_proc->outstanding_txns && target_proc->is_frozen)
1588432ff1e9SMarco Ballesio wake_up_interruptible_all(&target_proc->freeze_wait);
1589777783e0SGreg Kroah-Hartman if (t->buffer)
1590777783e0SGreg Kroah-Hartman t->buffer->transaction = NULL;
1591a370003cSTodd Kjos binder_inner_proc_unlock(target_proc);
1592a370003cSTodd Kjos }
15931987f112SFrankie.Chang if (trace_binder_txn_latency_free_enabled())
15941987f112SFrankie.Chang binder_txn_latency_free(t);
1595a370003cSTodd Kjos /*
1596a370003cSTodd Kjos * If the transaction has no target_proc, then
1597a370003cSTodd Kjos * t->buffer->transaction has already been cleared.
1598a370003cSTodd Kjos */
159944d8047fSTodd Kjos binder_free_txn_fixups(t);
1600777783e0SGreg Kroah-Hartman kfree(t);
1601777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION);
1602777783e0SGreg Kroah-Hartman }
1603777783e0SGreg Kroah-Hartman
binder_send_failed_reply(struct binder_transaction * t,uint32_t error_code)1604777783e0SGreg Kroah-Hartman static void binder_send_failed_reply(struct binder_transaction *t,
1605777783e0SGreg Kroah-Hartman uint32_t error_code)
1606777783e0SGreg Kroah-Hartman {
1607777783e0SGreg Kroah-Hartman struct binder_thread *target_thread;
1608777783e0SGreg Kroah-Hartman struct binder_transaction *next;
1609777783e0SGreg Kroah-Hartman
1610777783e0SGreg Kroah-Hartman BUG_ON(t->flags & TF_ONE_WAY);
1611777783e0SGreg Kroah-Hartman while (1) {
16120b89d69aSMartijn Coenen target_thread = binder_get_txn_from_and_acq_inner(t);
1613777783e0SGreg Kroah-Hartman if (target_thread) {
1614777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1615777783e0SGreg Kroah-Hartman "send failed reply for transaction %d to %d:%d\n",
1616777783e0SGreg Kroah-Hartman t->debug_id,
1617777783e0SGreg Kroah-Hartman target_thread->proc->pid,
1618777783e0SGreg Kroah-Hartman target_thread->pid);
1619777783e0SGreg Kroah-Hartman
16200b89d69aSMartijn Coenen binder_pop_transaction_ilocked(target_thread, t);
162126549d17STodd Kjos if (target_thread->reply_error.cmd == BR_OK) {
162226549d17STodd Kjos target_thread->reply_error.cmd = error_code;
1623148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(
1624148ade2cSMartijn Coenen target_thread,
1625148ade2cSMartijn Coenen &target_thread->reply_error.work);
1626777783e0SGreg Kroah-Hartman wake_up_interruptible(&target_thread->wait);
1627777783e0SGreg Kroah-Hartman } else {
1628e46a3b3bSTodd Kjos /*
1629e46a3b3bSTodd Kjos * Cannot get here for normal operation, but
1630e46a3b3bSTodd Kjos * we can if multiple synchronous transactions
1631e46a3b3bSTodd Kjos * are sent without blocking for responses.
1632e46a3b3bSTodd Kjos * Just ignore the 2nd error in this case.
1633e46a3b3bSTodd Kjos */
1634e46a3b3bSTodd Kjos pr_warn("Unexpected reply error: %u\n",
163526549d17STodd Kjos target_thread->reply_error.cmd);
1636777783e0SGreg Kroah-Hartman }
16370b89d69aSMartijn Coenen binder_inner_proc_unlock(target_thread->proc);
16387a4408c6STodd Kjos binder_thread_dec_tmpref(target_thread);
163926549d17STodd Kjos binder_free_transaction(t);
1640777783e0SGreg Kroah-Hartman return;
1641777783e0SGreg Kroah-Hartman }
164272b93c79SMrinal Pandey __release(&target_thread->proc->inner_lock);
1643777783e0SGreg Kroah-Hartman next = t->from_parent;
1644777783e0SGreg Kroah-Hartman
1645777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1646777783e0SGreg Kroah-Hartman "send failed reply for transaction %d, target dead\n",
1647777783e0SGreg Kroah-Hartman t->debug_id);
1648777783e0SGreg Kroah-Hartman
1649b6d282ceSTodd Kjos binder_free_transaction(t);
1650777783e0SGreg Kroah-Hartman if (next == NULL) {
1651777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
1652777783e0SGreg Kroah-Hartman "reply failed, no target thread at root\n");
1653777783e0SGreg Kroah-Hartman return;
1654777783e0SGreg Kroah-Hartman }
1655777783e0SGreg Kroah-Hartman t = next;
1656777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
1657777783e0SGreg Kroah-Hartman "reply failed, no target thread -- retry %d\n",
1658777783e0SGreg Kroah-Hartman t->debug_id);
1659777783e0SGreg Kroah-Hartman }
1660777783e0SGreg Kroah-Hartman }
1661777783e0SGreg Kroah-Hartman
1662feba3900SMartijn Coenen /**
1663fb2c4452SMartijn Coenen * binder_cleanup_transaction() - cleans up undelivered transaction
1664fb2c4452SMartijn Coenen * @t: transaction that needs to be cleaned up
1665fb2c4452SMartijn Coenen * @reason: reason the transaction wasn't delivered
1666fb2c4452SMartijn Coenen * @error_code: error to return to caller (if synchronous call)
1667fb2c4452SMartijn Coenen */
binder_cleanup_transaction(struct binder_transaction * t,const char * reason,uint32_t error_code)1668fb2c4452SMartijn Coenen static void binder_cleanup_transaction(struct binder_transaction *t,
1669fb2c4452SMartijn Coenen const char *reason,
1670fb2c4452SMartijn Coenen uint32_t error_code)
1671fb2c4452SMartijn Coenen {
1672fb2c4452SMartijn Coenen if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
1673fb2c4452SMartijn Coenen binder_send_failed_reply(t, error_code);
1674fb2c4452SMartijn Coenen } else {
1675fb2c4452SMartijn Coenen binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
1676fb2c4452SMartijn Coenen "undelivered transaction %d, %s\n",
1677fb2c4452SMartijn Coenen t->debug_id, reason);
1678fb2c4452SMartijn Coenen binder_free_transaction(t);
1679fb2c4452SMartijn Coenen }
1680fb2c4452SMartijn Coenen }
1681fb2c4452SMartijn Coenen
1682fb2c4452SMartijn Coenen /**
16837a67a393STodd Kjos * binder_get_object() - gets object and checks for valid metadata
16847a67a393STodd Kjos * @proc: binder_proc owning the buffer
16856d98eb95STodd Kjos * @u: sender's user pointer to base of buffer
1686feba3900SMartijn Coenen * @buffer: binder_buffer that we're parsing.
16877a67a393STodd Kjos * @offset: offset in the @buffer at which to validate an object.
16887a67a393STodd Kjos * @object: struct binder_object to read into
1689feba3900SMartijn Coenen *
16906d98eb95STodd Kjos * Copy the binder object at the given offset into @object. If @u is
16916d98eb95STodd Kjos * provided then the copy is from the sender's buffer. If not, then
16926d98eb95STodd Kjos * it is copied from the target's @buffer.
16936d98eb95STodd Kjos *
16946d98eb95STodd Kjos * Return: If there's a valid metadata object at @offset, the
16957a67a393STodd Kjos * size of that object. Otherwise, it returns zero. The object
16967a67a393STodd Kjos * is read into the struct binder_object pointed to by @object.
1697feba3900SMartijn Coenen */
binder_get_object(struct binder_proc * proc,const void __user * u,struct binder_buffer * buffer,unsigned long offset,struct binder_object * object)16987a67a393STodd Kjos static size_t binder_get_object(struct binder_proc *proc,
16996d98eb95STodd Kjos const void __user *u,
17007a67a393STodd Kjos struct binder_buffer *buffer,
17017a67a393STodd Kjos unsigned long offset,
17027a67a393STodd Kjos struct binder_object *object)
1703feba3900SMartijn Coenen {
17047a67a393STodd Kjos size_t read_size;
1705feba3900SMartijn Coenen struct binder_object_header *hdr;
1706feba3900SMartijn Coenen size_t object_size = 0;
1707feba3900SMartijn Coenen
17087a67a393STodd Kjos read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
17091d7f1049SCarlos Llamas if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
17101d7f1049SCarlos Llamas !IS_ALIGNED(offset, sizeof(u32)))
17116d98eb95STodd Kjos return 0;
17121d7f1049SCarlos Llamas
17136d98eb95STodd Kjos if (u) {
17146d98eb95STodd Kjos if (copy_from_user(object, u + offset, read_size))
17156d98eb95STodd Kjos return 0;
17166d98eb95STodd Kjos } else {
17176d98eb95STodd Kjos if (binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
1718bb4a2e48STodd Kjos offset, read_size))
1719bb4a2e48STodd Kjos return 0;
17206d98eb95STodd Kjos }
1721feba3900SMartijn Coenen
17227a67a393STodd Kjos /* Ok, now see if we read a complete object. */
17237a67a393STodd Kjos hdr = &object->hdr;
1724feba3900SMartijn Coenen switch (hdr->type) {
1725feba3900SMartijn Coenen case BINDER_TYPE_BINDER:
1726feba3900SMartijn Coenen case BINDER_TYPE_WEAK_BINDER:
1727feba3900SMartijn Coenen case BINDER_TYPE_HANDLE:
1728feba3900SMartijn Coenen case BINDER_TYPE_WEAK_HANDLE:
1729feba3900SMartijn Coenen object_size = sizeof(struct flat_binder_object);
1730feba3900SMartijn Coenen break;
1731feba3900SMartijn Coenen case BINDER_TYPE_FD:
1732feba3900SMartijn Coenen object_size = sizeof(struct binder_fd_object);
1733feba3900SMartijn Coenen break;
17347980240bSMartijn Coenen case BINDER_TYPE_PTR:
17357980240bSMartijn Coenen object_size = sizeof(struct binder_buffer_object);
17367980240bSMartijn Coenen break;
1737def95c73SMartijn Coenen case BINDER_TYPE_FDA:
1738def95c73SMartijn Coenen object_size = sizeof(struct binder_fd_array_object);
1739def95c73SMartijn Coenen break;
1740feba3900SMartijn Coenen default:
1741feba3900SMartijn Coenen return 0;
1742feba3900SMartijn Coenen }
1743feba3900SMartijn Coenen if (offset <= buffer->data_size - object_size &&
1744feba3900SMartijn Coenen buffer->data_size >= object_size)
1745feba3900SMartijn Coenen return object_size;
1746feba3900SMartijn Coenen else
1747feba3900SMartijn Coenen return 0;
1748feba3900SMartijn Coenen }
1749feba3900SMartijn Coenen
17507980240bSMartijn Coenen /**
17517980240bSMartijn Coenen * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1752db6b0b81STodd Kjos * @proc: binder_proc owning the buffer
17537980240bSMartijn Coenen * @b: binder_buffer containing the object
1754db6b0b81STodd Kjos * @object: struct binder_object to read into
17557980240bSMartijn Coenen * @index: index in offset array at which the binder_buffer_object is
17567980240bSMartijn Coenen * located
1757db6b0b81STodd Kjos * @start_offset: points to the start of the offset array
1758db6b0b81STodd Kjos * @object_offsetp: offset of @object read from @b
17597980240bSMartijn Coenen * @num_valid: the number of valid offsets in the offset array
17607980240bSMartijn Coenen *
17617980240bSMartijn Coenen * Return: If @index is within the valid range of the offset array
17627980240bSMartijn Coenen * described by @start and @num_valid, and if there's a valid
17637980240bSMartijn Coenen * binder_buffer_object at the offset found in index @index
17647980240bSMartijn Coenen * of the offset array, that object is returned. Otherwise,
17657980240bSMartijn Coenen * %NULL is returned.
17667980240bSMartijn Coenen * Note that the offset found in index @index itself is not
17677980240bSMartijn Coenen * verified; this function assumes that @num_valid elements
17687980240bSMartijn Coenen * from @start were previously verified to have valid offsets.
1769db6b0b81STodd Kjos * If @object_offsetp is non-NULL, then the offset within
1770db6b0b81STodd Kjos * @b is written to it.
17717980240bSMartijn Coenen */
binder_validate_ptr(struct binder_proc * proc,struct binder_buffer * b,struct binder_object * object,binder_size_t index,binder_size_t start_offset,binder_size_t * object_offsetp,binder_size_t num_valid)1772db6b0b81STodd Kjos static struct binder_buffer_object *binder_validate_ptr(
1773db6b0b81STodd Kjos struct binder_proc *proc,
1774db6b0b81STodd Kjos struct binder_buffer *b,
1775db6b0b81STodd Kjos struct binder_object *object,
17767980240bSMartijn Coenen binder_size_t index,
1777db6b0b81STodd Kjos binder_size_t start_offset,
1778db6b0b81STodd Kjos binder_size_t *object_offsetp,
17797980240bSMartijn Coenen binder_size_t num_valid)
17807980240bSMartijn Coenen {
1781db6b0b81STodd Kjos size_t object_size;
1782db6b0b81STodd Kjos binder_size_t object_offset;
1783db6b0b81STodd Kjos unsigned long buffer_offset;
17847980240bSMartijn Coenen
17857980240bSMartijn Coenen if (index >= num_valid)
17867980240bSMartijn Coenen return NULL;
17877980240bSMartijn Coenen
1788db6b0b81STodd Kjos buffer_offset = start_offset + sizeof(binder_size_t) * index;
1789bb4a2e48STodd Kjos if (binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
1790bb4a2e48STodd Kjos b, buffer_offset,
1791bb4a2e48STodd Kjos sizeof(object_offset)))
1792bb4a2e48STodd Kjos return NULL;
17936d98eb95STodd Kjos object_size = binder_get_object(proc, NULL, b, object_offset, object);
1794db6b0b81STodd Kjos if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
17957980240bSMartijn Coenen return NULL;
1796db6b0b81STodd Kjos if (object_offsetp)
1797db6b0b81STodd Kjos *object_offsetp = object_offset;
17987980240bSMartijn Coenen
1799db6b0b81STodd Kjos return &object->bbo;
18007980240bSMartijn Coenen }
18017980240bSMartijn Coenen
18027980240bSMartijn Coenen /**
18037980240bSMartijn Coenen * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1804db6b0b81STodd Kjos * @proc: binder_proc owning the buffer
18057980240bSMartijn Coenen * @b: transaction buffer
1806db6b0b81STodd Kjos * @objects_start_offset: offset to start of objects buffer
1807db6b0b81STodd Kjos * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
1808db6b0b81STodd Kjos * @fixup_offset: start offset in @buffer to fix up
1809db6b0b81STodd Kjos * @last_obj_offset: offset to last binder_buffer_object that we fixed
1810db6b0b81STodd Kjos * @last_min_offset: minimum fixup offset in object at @last_obj_offset
18117980240bSMartijn Coenen *
18127980240bSMartijn Coenen * Return: %true if a fixup in buffer @buffer at offset @offset is
18137980240bSMartijn Coenen * allowed.
18147980240bSMartijn Coenen *
18157980240bSMartijn Coenen * For safety reasons, we only allow fixups inside a buffer to happen
18167980240bSMartijn Coenen * at increasing offsets; additionally, we only allow fixup on the last
18177980240bSMartijn Coenen * buffer object that was verified, or one of its parents.
18187980240bSMartijn Coenen *
18197980240bSMartijn Coenen * Example of what is allowed:
18207980240bSMartijn Coenen *
18217980240bSMartijn Coenen * A
18227980240bSMartijn Coenen * B (parent = A, offset = 0)
18237980240bSMartijn Coenen * C (parent = A, offset = 16)
18247980240bSMartijn Coenen * D (parent = C, offset = 0)
18257980240bSMartijn Coenen * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
18267980240bSMartijn Coenen *
18277980240bSMartijn Coenen * Examples of what is not allowed:
18287980240bSMartijn Coenen *
18297980240bSMartijn Coenen * Decreasing offsets within the same parent:
18307980240bSMartijn Coenen * A
18317980240bSMartijn Coenen * C (parent = A, offset = 16)
18327980240bSMartijn Coenen * B (parent = A, offset = 0) // decreasing offset within A
18337980240bSMartijn Coenen *
18347980240bSMartijn Coenen * Referring to a parent that wasn't the last object or any of its parents:
18357980240bSMartijn Coenen * A
18367980240bSMartijn Coenen * B (parent = A, offset = 0)
18377980240bSMartijn Coenen * C (parent = A, offset = 0)
18387980240bSMartijn Coenen * C (parent = A, offset = 16)
18397980240bSMartijn Coenen * D (parent = B, offset = 0) // B is not A or any of A's parents
18407980240bSMartijn Coenen */
binder_validate_fixup(struct binder_proc * proc,struct binder_buffer * b,binder_size_t objects_start_offset,binder_size_t buffer_obj_offset,binder_size_t fixup_offset,binder_size_t last_obj_offset,binder_size_t last_min_offset)1841db6b0b81STodd Kjos static bool binder_validate_fixup(struct binder_proc *proc,
1842db6b0b81STodd Kjos struct binder_buffer *b,
1843db6b0b81STodd Kjos binder_size_t objects_start_offset,
1844db6b0b81STodd Kjos binder_size_t buffer_obj_offset,
18457980240bSMartijn Coenen binder_size_t fixup_offset,
1846db6b0b81STodd Kjos binder_size_t last_obj_offset,
18477980240bSMartijn Coenen binder_size_t last_min_offset)
18487980240bSMartijn Coenen {
1849db6b0b81STodd Kjos if (!last_obj_offset) {
18507980240bSMartijn Coenen /* Nothing to fix up in */
18517980240bSMartijn Coenen return false;
18527980240bSMartijn Coenen }
18537980240bSMartijn Coenen
1854db6b0b81STodd Kjos while (last_obj_offset != buffer_obj_offset) {
1855db6b0b81STodd Kjos unsigned long buffer_offset;
1856db6b0b81STodd Kjos struct binder_object last_object;
1857db6b0b81STodd Kjos struct binder_buffer_object *last_bbo;
18586d98eb95STodd Kjos size_t object_size = binder_get_object(proc, NULL, b,
18596d98eb95STodd Kjos last_obj_offset,
1860db6b0b81STodd Kjos &last_object);
1861db6b0b81STodd Kjos if (object_size != sizeof(*last_bbo))
1862db6b0b81STodd Kjos return false;
1863db6b0b81STodd Kjos
1864db6b0b81STodd Kjos last_bbo = &last_object.bbo;
18657980240bSMartijn Coenen /*
18667980240bSMartijn Coenen * Safe to retrieve the parent of last_obj, since it
18677980240bSMartijn Coenen * was already previously verified by the driver.
18687980240bSMartijn Coenen */
1869db6b0b81STodd Kjos if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
18707980240bSMartijn Coenen return false;
1871db6b0b81STodd Kjos last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
1872db6b0b81STodd Kjos buffer_offset = objects_start_offset +
1873bb4a2e48STodd Kjos sizeof(binder_size_t) * last_bbo->parent;
1874bb4a2e48STodd Kjos if (binder_alloc_copy_from_buffer(&proc->alloc,
1875bb4a2e48STodd Kjos &last_obj_offset,
1876db6b0b81STodd Kjos b, buffer_offset,
1877bb4a2e48STodd Kjos sizeof(last_obj_offset)))
1878bb4a2e48STodd Kjos return false;
18797980240bSMartijn Coenen }
18807980240bSMartijn Coenen return (fixup_offset >= last_min_offset);
18817980240bSMartijn Coenen }
18827980240bSMartijn Coenen
188380cd7956STodd Kjos /**
188480cd7956STodd Kjos * struct binder_task_work_cb - for deferred close
188580cd7956STodd Kjos *
188680cd7956STodd Kjos * @twork: callback_head for task work
188780cd7956STodd Kjos * @fd: fd to close
188880cd7956STodd Kjos *
188980cd7956STodd Kjos * Structure to pass task work to be handled after
189080cd7956STodd Kjos * returning from binder_ioctl() via task_work_add().
189180cd7956STodd Kjos */
189280cd7956STodd Kjos struct binder_task_work_cb {
189380cd7956STodd Kjos struct callback_head twork;
189480cd7956STodd Kjos struct file *file;
189580cd7956STodd Kjos };
189680cd7956STodd Kjos
189780cd7956STodd Kjos /**
189880cd7956STodd Kjos * binder_do_fd_close() - close list of file descriptors
189980cd7956STodd Kjos * @twork: callback head for task work
190080cd7956STodd Kjos *
190180cd7956STodd Kjos * It is not safe to call ksys_close() during the binder_ioctl()
190280cd7956STodd Kjos * function if there is a chance that binder's own file descriptor
190380cd7956STodd Kjos * might be closed. This is to meet the requirements for using
190480cd7956STodd Kjos * fdget() (see comments for __fget_light()). Therefore use
190580cd7956STodd Kjos * task_work_add() to schedule the close operation once we have
190680cd7956STodd Kjos * returned from binder_ioctl(). This function is a callback
190780cd7956STodd Kjos * for that mechanism and does the actual ksys_close() on the
190880cd7956STodd Kjos * given file descriptor.
190980cd7956STodd Kjos */
binder_do_fd_close(struct callback_head * twork)191080cd7956STodd Kjos static void binder_do_fd_close(struct callback_head *twork)
191180cd7956STodd Kjos {
191280cd7956STodd Kjos struct binder_task_work_cb *twcb = container_of(twork,
191380cd7956STodd Kjos struct binder_task_work_cb, twork);
191480cd7956STodd Kjos
191580cd7956STodd Kjos fput(twcb->file);
191680cd7956STodd Kjos kfree(twcb);
191780cd7956STodd Kjos }
191880cd7956STodd Kjos
191980cd7956STodd Kjos /**
192080cd7956STodd Kjos * binder_deferred_fd_close() - schedule a close for the given file-descriptor
192180cd7956STodd Kjos * @fd: file-descriptor to close
192280cd7956STodd Kjos *
192380cd7956STodd Kjos * See comments in binder_do_fd_close(). This function is used to schedule
192480cd7956STodd Kjos * a file-descriptor to be closed after returning from binder_ioctl().
192580cd7956STodd Kjos */
binder_deferred_fd_close(int fd)192680cd7956STodd Kjos static void binder_deferred_fd_close(int fd)
192780cd7956STodd Kjos {
192880cd7956STodd Kjos struct binder_task_work_cb *twcb;
192980cd7956STodd Kjos
193080cd7956STodd Kjos twcb = kzalloc(sizeof(*twcb), GFP_KERNEL);
193180cd7956STodd Kjos if (!twcb)
193280cd7956STodd Kjos return;
193380cd7956STodd Kjos init_task_work(&twcb->twork, binder_do_fd_close);
19346319194eSAl Viro twcb->file = close_fd_get_file(fd);
19356e802a4bSJens Axboe if (twcb->file) {
193640a19260SAl Viro // pin it until binder_do_fd_close(); see comments there
193740a19260SAl Viro get_file(twcb->file);
19386e802a4bSJens Axboe filp_close(twcb->file, current->files);
193991989c70SJens Axboe task_work_add(current, &twcb->twork, TWA_RESUME);
19406e802a4bSJens Axboe } else {
194180cd7956STodd Kjos kfree(twcb);
194280cd7956STodd Kjos }
19436e802a4bSJens Axboe }
194480cd7956STodd Kjos
binder_transaction_buffer_release(struct binder_proc * proc,struct binder_thread * thread,struct binder_buffer * buffer,binder_size_t off_end_offset,bool is_failure)1945777783e0SGreg Kroah-Hartman static void binder_transaction_buffer_release(struct binder_proc *proc,
19465fdb55c1STodd Kjos struct binder_thread *thread,
1947777783e0SGreg Kroah-Hartman struct binder_buffer *buffer,
1948bdc1c5faSCarlos Llamas binder_size_t off_end_offset,
1949bde4a19fSTodd Kjos bool is_failure)
1950777783e0SGreg Kroah-Hartman {
1951777783e0SGreg Kroah-Hartman int debug_id = buffer->debug_id;
1952bdc1c5faSCarlos Llamas binder_size_t off_start_offset, buffer_offset;
1953777783e0SGreg Kroah-Hartman
1954777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
1955bde4a19fSTodd Kjos "%d buffer release %d, size %zd-%zd, failed at %llx\n",
1956777783e0SGreg Kroah-Hartman proc->pid, buffer->debug_id,
1957bde4a19fSTodd Kjos buffer->data_size, buffer->offsets_size,
1958bdc1c5faSCarlos Llamas (unsigned long long)off_end_offset);
1959777783e0SGreg Kroah-Hartman
1960777783e0SGreg Kroah-Hartman if (buffer->target_node)
1961777783e0SGreg Kroah-Hartman binder_dec_node(buffer->target_node, 1, 0);
1962777783e0SGreg Kroah-Hartman
1963db6b0b81STodd Kjos off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
1964bdc1c5faSCarlos Llamas
1965bde4a19fSTodd Kjos for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
1966bde4a19fSTodd Kjos buffer_offset += sizeof(binder_size_t)) {
1967feba3900SMartijn Coenen struct binder_object_header *hdr;
1968bb4a2e48STodd Kjos size_t object_size = 0;
19697a67a393STodd Kjos struct binder_object object;
19708ced0c62STodd Kjos binder_size_t object_offset;
1971777783e0SGreg Kroah-Hartman
1972bb4a2e48STodd Kjos if (!binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
19738ced0c62STodd Kjos buffer, buffer_offset,
1974bb4a2e48STodd Kjos sizeof(object_offset)))
19756d98eb95STodd Kjos object_size = binder_get_object(proc, NULL, buffer,
19767a67a393STodd Kjos object_offset, &object);
1977feba3900SMartijn Coenen if (object_size == 0) {
1978feba3900SMartijn Coenen pr_err("transaction release %d bad object at offset %lld, size %zd\n",
19798ced0c62STodd Kjos debug_id, (u64)object_offset, buffer->data_size);
1980777783e0SGreg Kroah-Hartman continue;
1981777783e0SGreg Kroah-Hartman }
19827a67a393STodd Kjos hdr = &object.hdr;
1983feba3900SMartijn Coenen switch (hdr->type) {
1984777783e0SGreg Kroah-Hartman case BINDER_TYPE_BINDER:
1985777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_BINDER: {
1986feba3900SMartijn Coenen struct flat_binder_object *fp;
1987feba3900SMartijn Coenen struct binder_node *node;
1988777783e0SGreg Kroah-Hartman
1989feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
1990feba3900SMartijn Coenen node = binder_get_node(proc, fp->binder);
1991777783e0SGreg Kroah-Hartman if (node == NULL) {
1992777783e0SGreg Kroah-Hartman pr_err("transaction release %d bad node %016llx\n",
1993777783e0SGreg Kroah-Hartman debug_id, (u64)fp->binder);
1994777783e0SGreg Kroah-Hartman break;
1995777783e0SGreg Kroah-Hartman }
1996777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
1997777783e0SGreg Kroah-Hartman " node %d u%016llx\n",
1998777783e0SGreg Kroah-Hartman node->debug_id, (u64)node->ptr);
1999feba3900SMartijn Coenen binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2000feba3900SMartijn Coenen 0);
2001adc18842STodd Kjos binder_put_node(node);
2002777783e0SGreg Kroah-Hartman } break;
2003777783e0SGreg Kroah-Hartman case BINDER_TYPE_HANDLE:
2004777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_HANDLE: {
2005feba3900SMartijn Coenen struct flat_binder_object *fp;
2006372e3147STodd Kjos struct binder_ref_data rdata;
2007372e3147STodd Kjos int ret;
20080a3ffab9SArve Hjønnevåg
2009feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
2010372e3147STodd Kjos ret = binder_dec_ref_for_handle(proc, fp->handle,
2011372e3147STodd Kjos hdr->type == BINDER_TYPE_HANDLE, &rdata);
2012372e3147STodd Kjos
2013372e3147STodd Kjos if (ret) {
2014372e3147STodd Kjos pr_err("transaction release %d bad handle %d, ret = %d\n",
2015372e3147STodd Kjos debug_id, fp->handle, ret);
2016777783e0SGreg Kroah-Hartman break;
2017777783e0SGreg Kroah-Hartman }
2018777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
2019372e3147STodd Kjos " ref %d desc %d\n",
2020372e3147STodd Kjos rdata.debug_id, rdata.desc);
2021777783e0SGreg Kroah-Hartman } break;
2022777783e0SGreg Kroah-Hartman
2023feba3900SMartijn Coenen case BINDER_TYPE_FD: {
202444d8047fSTodd Kjos /*
202544d8047fSTodd Kjos * No need to close the file here since user-space
2026690cfa20SAjith P V * closes it for successfully delivered
202744d8047fSTodd Kjos * transactions. For transactions that weren't
202844d8047fSTodd Kjos * delivered, the new fd was never allocated so
202944d8047fSTodd Kjos * there is no need to close and the fput on the
203044d8047fSTodd Kjos * file is done when the transaction is torn
203144d8047fSTodd Kjos * down.
203244d8047fSTodd Kjos */
2033feba3900SMartijn Coenen } break;
20347980240bSMartijn Coenen case BINDER_TYPE_PTR:
20357980240bSMartijn Coenen /*
20367980240bSMartijn Coenen * Nothing to do here, this will get cleaned up when the
20377980240bSMartijn Coenen * transaction buffer gets freed
20387980240bSMartijn Coenen */
20397980240bSMartijn Coenen break;
2040def95c73SMartijn Coenen case BINDER_TYPE_FDA: {
2041def95c73SMartijn Coenen struct binder_fd_array_object *fda;
2042def95c73SMartijn Coenen struct binder_buffer_object *parent;
2043db6b0b81STodd Kjos struct binder_object ptr_object;
2044bde4a19fSTodd Kjos binder_size_t fda_offset;
2045def95c73SMartijn Coenen size_t fd_index;
2046def95c73SMartijn Coenen binder_size_t fd_buf_size;
2047bde4a19fSTodd Kjos binder_size_t num_valid;
2048def95c73SMartijn Coenen
204932e9f56aSTodd Kjos if (is_failure) {
205044d8047fSTodd Kjos /*
205144d8047fSTodd Kjos * The fd fixups have not been applied so no
205244d8047fSTodd Kjos * fds need to be closed.
205344d8047fSTodd Kjos */
205444d8047fSTodd Kjos continue;
205544d8047fSTodd Kjos }
205644d8047fSTodd Kjos
2057bde4a19fSTodd Kjos num_valid = (buffer_offset - off_start_offset) /
2058bde4a19fSTodd Kjos sizeof(binder_size_t);
2059def95c73SMartijn Coenen fda = to_binder_fd_array_object(hdr);
2060db6b0b81STodd Kjos parent = binder_validate_ptr(proc, buffer, &ptr_object,
2061db6b0b81STodd Kjos fda->parent,
2062db6b0b81STodd Kjos off_start_offset,
2063db6b0b81STodd Kjos NULL,
2064bde4a19fSTodd Kjos num_valid);
2065def95c73SMartijn Coenen if (!parent) {
2066f7f84fdeSArvind Yadav pr_err("transaction release %d bad parent offset\n",
2067def95c73SMartijn Coenen debug_id);
2068def95c73SMartijn Coenen continue;
2069def95c73SMartijn Coenen }
2070def95c73SMartijn Coenen fd_buf_size = sizeof(u32) * fda->num_fds;
2071def95c73SMartijn Coenen if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2072def95c73SMartijn Coenen pr_err("transaction release %d invalid number of fds (%lld)\n",
2073def95c73SMartijn Coenen debug_id, (u64)fda->num_fds);
2074def95c73SMartijn Coenen continue;
2075def95c73SMartijn Coenen }
2076def95c73SMartijn Coenen if (fd_buf_size > parent->length ||
2077def95c73SMartijn Coenen fda->parent_offset > parent->length - fd_buf_size) {
2078def95c73SMartijn Coenen /* No space for all file descriptors here. */
2079def95c73SMartijn Coenen pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2080def95c73SMartijn Coenen debug_id, (u64)fda->num_fds);
2081def95c73SMartijn Coenen continue;
2082def95c73SMartijn Coenen }
2083bde4a19fSTodd Kjos /*
2084bde4a19fSTodd Kjos * the source data for binder_buffer_object is visible
2085bde4a19fSTodd Kjos * to user-space and the @buffer element is the user
2086bde4a19fSTodd Kjos * pointer to the buffer_object containing the fd_array.
2087bde4a19fSTodd Kjos * Convert the address to an offset relative to
2088bde4a19fSTodd Kjos * the base of the transaction buffer.
2089bde4a19fSTodd Kjos */
2090bde4a19fSTodd Kjos fda_offset =
2091bde4a19fSTodd Kjos (parent->buffer - (uintptr_t)buffer->user_data) +
2092bde4a19fSTodd Kjos fda->parent_offset;
20938ced0c62STodd Kjos for (fd_index = 0; fd_index < fda->num_fds;
20948ced0c62STodd Kjos fd_index++) {
20958ced0c62STodd Kjos u32 fd;
2096bb4a2e48STodd Kjos int err;
2097bde4a19fSTodd Kjos binder_size_t offset = fda_offset +
2098bde4a19fSTodd Kjos fd_index * sizeof(fd);
20998ced0c62STodd Kjos
2100bb4a2e48STodd Kjos err = binder_alloc_copy_from_buffer(
2101bb4a2e48STodd Kjos &proc->alloc, &fd, buffer,
2102bb4a2e48STodd Kjos offset, sizeof(fd));
2103bb4a2e48STodd Kjos WARN_ON(err);
21045fdb55c1STodd Kjos if (!err) {
21058ced0c62STodd Kjos binder_deferred_fd_close(fd);
21065fdb55c1STodd Kjos /*
21075fdb55c1STodd Kjos * Need to make sure the thread goes
21085fdb55c1STodd Kjos * back to userspace to complete the
21095fdb55c1STodd Kjos * deferred close
21105fdb55c1STodd Kjos */
21115fdb55c1STodd Kjos if (thread)
21125fdb55c1STodd Kjos thread->looper_need_return = true;
21135fdb55c1STodd Kjos }
21148ced0c62STodd Kjos }
2115def95c73SMartijn Coenen } break;
2116777783e0SGreg Kroah-Hartman default:
2117777783e0SGreg Kroah-Hartman pr_err("transaction release %d bad object type %x\n",
2118feba3900SMartijn Coenen debug_id, hdr->type);
2119777783e0SGreg Kroah-Hartman break;
2120777783e0SGreg Kroah-Hartman }
2121777783e0SGreg Kroah-Hartman }
2122777783e0SGreg Kroah-Hartman }
2123777783e0SGreg Kroah-Hartman
2124bdc1c5faSCarlos Llamas /* Clean up all the objects in the buffer */
binder_release_entire_buffer(struct binder_proc * proc,struct binder_thread * thread,struct binder_buffer * buffer,bool is_failure)2125bdc1c5faSCarlos Llamas static inline void binder_release_entire_buffer(struct binder_proc *proc,
2126bdc1c5faSCarlos Llamas struct binder_thread *thread,
2127bdc1c5faSCarlos Llamas struct binder_buffer *buffer,
2128bdc1c5faSCarlos Llamas bool is_failure)
2129bdc1c5faSCarlos Llamas {
2130bdc1c5faSCarlos Llamas binder_size_t off_end_offset;
2131bdc1c5faSCarlos Llamas
2132bdc1c5faSCarlos Llamas off_end_offset = ALIGN(buffer->data_size, sizeof(void *));
2133bdc1c5faSCarlos Llamas off_end_offset += buffer->offsets_size;
2134bdc1c5faSCarlos Llamas
2135bdc1c5faSCarlos Llamas binder_transaction_buffer_release(proc, thread, buffer,
2136bdc1c5faSCarlos Llamas off_end_offset, is_failure);
2137bdc1c5faSCarlos Llamas }
2138bdc1c5faSCarlos Llamas
binder_translate_binder(struct flat_binder_object * fp,struct binder_transaction * t,struct binder_thread * thread)2139a056af42SMartijn Coenen static int binder_translate_binder(struct flat_binder_object *fp,
2140a056af42SMartijn Coenen struct binder_transaction *t,
2141a056af42SMartijn Coenen struct binder_thread *thread)
2142a056af42SMartijn Coenen {
2143a056af42SMartijn Coenen struct binder_node *node;
2144a056af42SMartijn Coenen struct binder_proc *proc = thread->proc;
2145a056af42SMartijn Coenen struct binder_proc *target_proc = t->to_proc;
2146372e3147STodd Kjos struct binder_ref_data rdata;
2147adc18842STodd Kjos int ret = 0;
2148a056af42SMartijn Coenen
2149a056af42SMartijn Coenen node = binder_get_node(proc, fp->binder);
2150a056af42SMartijn Coenen if (!node) {
2151673068eeSTodd Kjos node = binder_new_node(proc, fp);
2152a056af42SMartijn Coenen if (!node)
2153a056af42SMartijn Coenen return -ENOMEM;
2154a056af42SMartijn Coenen }
2155a056af42SMartijn Coenen if (fp->cookie != node->cookie) {
2156a056af42SMartijn Coenen binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2157a056af42SMartijn Coenen proc->pid, thread->pid, (u64)fp->binder,
2158a056af42SMartijn Coenen node->debug_id, (u64)fp->cookie,
2159a056af42SMartijn Coenen (u64)node->cookie);
2160adc18842STodd Kjos ret = -EINVAL;
2161adc18842STodd Kjos goto done;
2162a056af42SMartijn Coenen }
216352f88693STodd Kjos if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2164adc18842STodd Kjos ret = -EPERM;
2165adc18842STodd Kjos goto done;
2166adc18842STodd Kjos }
2167a056af42SMartijn Coenen
2168372e3147STodd Kjos ret = binder_inc_ref_for_node(target_proc, node,
2169372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_BINDER,
2170372e3147STodd Kjos &thread->todo, &rdata);
2171372e3147STodd Kjos if (ret)
2172adc18842STodd Kjos goto done;
2173a056af42SMartijn Coenen
2174a056af42SMartijn Coenen if (fp->hdr.type == BINDER_TYPE_BINDER)
2175a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_HANDLE;
2176a056af42SMartijn Coenen else
2177a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2178a056af42SMartijn Coenen fp->binder = 0;
2179372e3147STodd Kjos fp->handle = rdata.desc;
2180a056af42SMartijn Coenen fp->cookie = 0;
2181a056af42SMartijn Coenen
2182372e3147STodd Kjos trace_binder_transaction_node_to_ref(t, node, &rdata);
2183a056af42SMartijn Coenen binder_debug(BINDER_DEBUG_TRANSACTION,
2184a056af42SMartijn Coenen " node %d u%016llx -> ref %d desc %d\n",
2185a056af42SMartijn Coenen node->debug_id, (u64)node->ptr,
2186372e3147STodd Kjos rdata.debug_id, rdata.desc);
2187adc18842STodd Kjos done:
2188adc18842STodd Kjos binder_put_node(node);
2189adc18842STodd Kjos return ret;
2190a056af42SMartijn Coenen }
2191a056af42SMartijn Coenen
binder_translate_handle(struct flat_binder_object * fp,struct binder_transaction * t,struct binder_thread * thread)2192a056af42SMartijn Coenen static int binder_translate_handle(struct flat_binder_object *fp,
2193a056af42SMartijn Coenen struct binder_transaction *t,
2194a056af42SMartijn Coenen struct binder_thread *thread)
2195a056af42SMartijn Coenen {
2196a056af42SMartijn Coenen struct binder_proc *proc = thread->proc;
2197a056af42SMartijn Coenen struct binder_proc *target_proc = t->to_proc;
2198372e3147STodd Kjos struct binder_node *node;
2199372e3147STodd Kjos struct binder_ref_data src_rdata;
2200adc18842STodd Kjos int ret = 0;
2201a056af42SMartijn Coenen
2202372e3147STodd Kjos node = binder_get_node_from_ref(proc, fp->handle,
2203372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2204372e3147STodd Kjos if (!node) {
2205a056af42SMartijn Coenen binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2206a056af42SMartijn Coenen proc->pid, thread->pid, fp->handle);
2207a056af42SMartijn Coenen return -EINVAL;
2208a056af42SMartijn Coenen }
220952f88693STodd Kjos if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2210adc18842STodd Kjos ret = -EPERM;
2211adc18842STodd Kjos goto done;
2212adc18842STodd Kjos }
2213a056af42SMartijn Coenen
2214673068eeSTodd Kjos binder_node_lock(node);
2215372e3147STodd Kjos if (node->proc == target_proc) {
2216a056af42SMartijn Coenen if (fp->hdr.type == BINDER_TYPE_HANDLE)
2217a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_BINDER;
2218a056af42SMartijn Coenen else
2219a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2220372e3147STodd Kjos fp->binder = node->ptr;
2221372e3147STodd Kjos fp->cookie = node->cookie;
2222673068eeSTodd Kjos if (node->proc)
2223673068eeSTodd Kjos binder_inner_proc_lock(node->proc);
2224324fa64cSTodd Kjos else
2225324fa64cSTodd Kjos __acquire(&node->proc->inner_lock);
2226673068eeSTodd Kjos binder_inc_node_nilocked(node,
2227372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_BINDER,
2228a056af42SMartijn Coenen 0, NULL);
2229673068eeSTodd Kjos if (node->proc)
2230673068eeSTodd Kjos binder_inner_proc_unlock(node->proc);
2231324fa64cSTodd Kjos else
2232324fa64cSTodd Kjos __release(&node->proc->inner_lock);
2233372e3147STodd Kjos trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2234a056af42SMartijn Coenen binder_debug(BINDER_DEBUG_TRANSACTION,
2235a056af42SMartijn Coenen " ref %d desc %d -> node %d u%016llx\n",
2236372e3147STodd Kjos src_rdata.debug_id, src_rdata.desc, node->debug_id,
2237372e3147STodd Kjos (u64)node->ptr);
2238673068eeSTodd Kjos binder_node_unlock(node);
2239a056af42SMartijn Coenen } else {
2240372e3147STodd Kjos struct binder_ref_data dest_rdata;
2241a056af42SMartijn Coenen
2242673068eeSTodd Kjos binder_node_unlock(node);
2243372e3147STodd Kjos ret = binder_inc_ref_for_node(target_proc, node,
2244372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_HANDLE,
2245372e3147STodd Kjos NULL, &dest_rdata);
2246372e3147STodd Kjos if (ret)
2247adc18842STodd Kjos goto done;
2248a056af42SMartijn Coenen
2249a056af42SMartijn Coenen fp->binder = 0;
2250372e3147STodd Kjos fp->handle = dest_rdata.desc;
2251a056af42SMartijn Coenen fp->cookie = 0;
2252372e3147STodd Kjos trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2253372e3147STodd Kjos &dest_rdata);
2254a056af42SMartijn Coenen binder_debug(BINDER_DEBUG_TRANSACTION,
2255a056af42SMartijn Coenen " ref %d desc %d -> ref %d desc %d (node %d)\n",
2256372e3147STodd Kjos src_rdata.debug_id, src_rdata.desc,
2257372e3147STodd Kjos dest_rdata.debug_id, dest_rdata.desc,
2258372e3147STodd Kjos node->debug_id);
2259a056af42SMartijn Coenen }
2260adc18842STodd Kjos done:
2261adc18842STodd Kjos binder_put_node(node);
2262adc18842STodd Kjos return ret;
2263a056af42SMartijn Coenen }
2264a056af42SMartijn Coenen
binder_translate_fd(u32 fd,binder_size_t fd_offset,struct binder_transaction * t,struct binder_thread * thread,struct binder_transaction * in_reply_to)22658ced0c62STodd Kjos static int binder_translate_fd(u32 fd, binder_size_t fd_offset,
2266a056af42SMartijn Coenen struct binder_transaction *t,
2267a056af42SMartijn Coenen struct binder_thread *thread,
2268a056af42SMartijn Coenen struct binder_transaction *in_reply_to)
2269a056af42SMartijn Coenen {
2270a056af42SMartijn Coenen struct binder_proc *proc = thread->proc;
2271a056af42SMartijn Coenen struct binder_proc *target_proc = t->to_proc;
227244d8047fSTodd Kjos struct binder_txn_fd_fixup *fixup;
2273a056af42SMartijn Coenen struct file *file;
227444d8047fSTodd Kjos int ret = 0;
2275a056af42SMartijn Coenen bool target_allows_fd;
2276a056af42SMartijn Coenen
2277a056af42SMartijn Coenen if (in_reply_to)
2278a056af42SMartijn Coenen target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2279a056af42SMartijn Coenen else
2280a056af42SMartijn Coenen target_allows_fd = t->buffer->target_node->accept_fds;
2281a056af42SMartijn Coenen if (!target_allows_fd) {
2282a056af42SMartijn Coenen binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2283a056af42SMartijn Coenen proc->pid, thread->pid,
2284a056af42SMartijn Coenen in_reply_to ? "reply" : "transaction",
2285a056af42SMartijn Coenen fd);
2286a056af42SMartijn Coenen ret = -EPERM;
2287a056af42SMartijn Coenen goto err_fd_not_accepted;
2288a056af42SMartijn Coenen }
2289a056af42SMartijn Coenen
2290a056af42SMartijn Coenen file = fget(fd);
2291a056af42SMartijn Coenen if (!file) {
2292a056af42SMartijn Coenen binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2293a056af42SMartijn Coenen proc->pid, thread->pid, fd);
2294a056af42SMartijn Coenen ret = -EBADF;
2295a056af42SMartijn Coenen goto err_fget;
2296a056af42SMartijn Coenen }
229752f88693STodd Kjos ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
2298a056af42SMartijn Coenen if (ret < 0) {
2299a056af42SMartijn Coenen ret = -EPERM;
2300a056af42SMartijn Coenen goto err_security;
2301a056af42SMartijn Coenen }
2302a056af42SMartijn Coenen
230344d8047fSTodd Kjos /*
230444d8047fSTodd Kjos * Add fixup record for this transaction. The allocation
230544d8047fSTodd Kjos * of the fd in the target needs to be done from a
230644d8047fSTodd Kjos * target thread.
230744d8047fSTodd Kjos */
230844d8047fSTodd Kjos fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
230944d8047fSTodd Kjos if (!fixup) {
2310a056af42SMartijn Coenen ret = -ENOMEM;
231144d8047fSTodd Kjos goto err_alloc;
2312a056af42SMartijn Coenen }
231344d8047fSTodd Kjos fixup->file = file;
23148ced0c62STodd Kjos fixup->offset = fd_offset;
2315a8a570c6SCarlos Llamas fixup->target_fd = -1;
231644d8047fSTodd Kjos trace_binder_transaction_fd_send(t, fd, fixup->offset);
231744d8047fSTodd Kjos list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
2318a056af42SMartijn Coenen
231944d8047fSTodd Kjos return ret;
2320a056af42SMartijn Coenen
232144d8047fSTodd Kjos err_alloc:
2322a056af42SMartijn Coenen err_security:
2323a056af42SMartijn Coenen fput(file);
2324a056af42SMartijn Coenen err_fget:
2325a056af42SMartijn Coenen err_fd_not_accepted:
2326a056af42SMartijn Coenen return ret;
2327a056af42SMartijn Coenen }
2328a056af42SMartijn Coenen
232909184ae9STodd Kjos /**
233009184ae9STodd Kjos * struct binder_ptr_fixup - data to be fixed-up in target buffer
233109184ae9STodd Kjos * @offset offset in target buffer to fixup
233209184ae9STodd Kjos * @skip_size bytes to skip in copy (fixup will be written later)
233309184ae9STodd Kjos * @fixup_data data to write at fixup offset
233409184ae9STodd Kjos * @node list node
233509184ae9STodd Kjos *
233609184ae9STodd Kjos * This is used for the pointer fixup list (pf) which is created and consumed
233709184ae9STodd Kjos * during binder_transaction() and is only accessed locally. No
233809184ae9STodd Kjos * locking is necessary.
233909184ae9STodd Kjos *
234009184ae9STodd Kjos * The list is ordered by @offset.
234109184ae9STodd Kjos */
234209184ae9STodd Kjos struct binder_ptr_fixup {
234309184ae9STodd Kjos binder_size_t offset;
234409184ae9STodd Kjos size_t skip_size;
234509184ae9STodd Kjos binder_uintptr_t fixup_data;
234609184ae9STodd Kjos struct list_head node;
234709184ae9STodd Kjos };
234809184ae9STodd Kjos
234909184ae9STodd Kjos /**
235009184ae9STodd Kjos * struct binder_sg_copy - scatter-gather data to be copied
235109184ae9STodd Kjos * @offset offset in target buffer
235209184ae9STodd Kjos * @sender_uaddr user address in source buffer
235309184ae9STodd Kjos * @length bytes to copy
235409184ae9STodd Kjos * @node list node
235509184ae9STodd Kjos *
235609184ae9STodd Kjos * This is used for the sg copy list (sgc) which is created and consumed
235709184ae9STodd Kjos * during binder_transaction() and is only accessed locally. No
235809184ae9STodd Kjos * locking is necessary.
235909184ae9STodd Kjos *
236009184ae9STodd Kjos * The list is ordered by @offset.
236109184ae9STodd Kjos */
236209184ae9STodd Kjos struct binder_sg_copy {
236309184ae9STodd Kjos binder_size_t offset;
236409184ae9STodd Kjos const void __user *sender_uaddr;
236509184ae9STodd Kjos size_t length;
236609184ae9STodd Kjos struct list_head node;
236709184ae9STodd Kjos };
236809184ae9STodd Kjos
236909184ae9STodd Kjos /**
237009184ae9STodd Kjos * binder_do_deferred_txn_copies() - copy and fixup scatter-gather data
237109184ae9STodd Kjos * @alloc: binder_alloc associated with @buffer
237209184ae9STodd Kjos * @buffer: binder buffer in target process
237309184ae9STodd Kjos * @sgc_head: list_head of scatter-gather copy list
237409184ae9STodd Kjos * @pf_head: list_head of pointer fixup list
237509184ae9STodd Kjos *
237609184ae9STodd Kjos * Processes all elements of @sgc_head, applying fixups from @pf_head
237709184ae9STodd Kjos * and copying the scatter-gather data from the source process' user
237809184ae9STodd Kjos * buffer to the target's buffer. It is expected that the list creation
237909184ae9STodd Kjos * and processing all occurs during binder_transaction() so these lists
238009184ae9STodd Kjos * are only accessed in local context.
238109184ae9STodd Kjos *
238209184ae9STodd Kjos * Return: 0=success, else -errno
238309184ae9STodd Kjos */
binder_do_deferred_txn_copies(struct binder_alloc * alloc,struct binder_buffer * buffer,struct list_head * sgc_head,struct list_head * pf_head)238409184ae9STodd Kjos static int binder_do_deferred_txn_copies(struct binder_alloc *alloc,
238509184ae9STodd Kjos struct binder_buffer *buffer,
238609184ae9STodd Kjos struct list_head *sgc_head,
238709184ae9STodd Kjos struct list_head *pf_head)
238809184ae9STodd Kjos {
238909184ae9STodd Kjos int ret = 0;
239009184ae9STodd Kjos struct binder_sg_copy *sgc, *tmpsgc;
23912d1746e3SAlessandro Astone struct binder_ptr_fixup *tmppf;
239209184ae9STodd Kjos struct binder_ptr_fixup *pf =
239309184ae9STodd Kjos list_first_entry_or_null(pf_head, struct binder_ptr_fixup,
239409184ae9STodd Kjos node);
239509184ae9STodd Kjos
239609184ae9STodd Kjos list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
239709184ae9STodd Kjos size_t bytes_copied = 0;
239809184ae9STodd Kjos
239909184ae9STodd Kjos while (bytes_copied < sgc->length) {
240009184ae9STodd Kjos size_t copy_size;
240109184ae9STodd Kjos size_t bytes_left = sgc->length - bytes_copied;
240209184ae9STodd Kjos size_t offset = sgc->offset + bytes_copied;
240309184ae9STodd Kjos
240409184ae9STodd Kjos /*
240509184ae9STodd Kjos * We copy up to the fixup (pointed to by pf)
240609184ae9STodd Kjos */
240709184ae9STodd Kjos copy_size = pf ? min(bytes_left, (size_t)pf->offset - offset)
240809184ae9STodd Kjos : bytes_left;
240909184ae9STodd Kjos if (!ret && copy_size)
241009184ae9STodd Kjos ret = binder_alloc_copy_user_to_buffer(
241109184ae9STodd Kjos alloc, buffer,
241209184ae9STodd Kjos offset,
241309184ae9STodd Kjos sgc->sender_uaddr + bytes_copied,
241409184ae9STodd Kjos copy_size);
241509184ae9STodd Kjos bytes_copied += copy_size;
241609184ae9STodd Kjos if (copy_size != bytes_left) {
241709184ae9STodd Kjos BUG_ON(!pf);
241809184ae9STodd Kjos /* we stopped at a fixup offset */
241909184ae9STodd Kjos if (pf->skip_size) {
242009184ae9STodd Kjos /*
242109184ae9STodd Kjos * we are just skipping. This is for
242209184ae9STodd Kjos * BINDER_TYPE_FDA where the translated
242309184ae9STodd Kjos * fds will be fixed up when we get
242409184ae9STodd Kjos * to target context.
242509184ae9STodd Kjos */
242609184ae9STodd Kjos bytes_copied += pf->skip_size;
242709184ae9STodd Kjos } else {
242809184ae9STodd Kjos /* apply the fixup indicated by pf */
242909184ae9STodd Kjos if (!ret)
243009184ae9STodd Kjos ret = binder_alloc_copy_to_buffer(
243109184ae9STodd Kjos alloc, buffer,
243209184ae9STodd Kjos pf->offset,
243309184ae9STodd Kjos &pf->fixup_data,
243409184ae9STodd Kjos sizeof(pf->fixup_data));
243509184ae9STodd Kjos bytes_copied += sizeof(pf->fixup_data);
243609184ae9STodd Kjos }
243709184ae9STodd Kjos list_del(&pf->node);
243809184ae9STodd Kjos kfree(pf);
243909184ae9STodd Kjos pf = list_first_entry_or_null(pf_head,
244009184ae9STodd Kjos struct binder_ptr_fixup, node);
244109184ae9STodd Kjos }
244209184ae9STodd Kjos }
244309184ae9STodd Kjos list_del(&sgc->node);
244409184ae9STodd Kjos kfree(sgc);
244509184ae9STodd Kjos }
24462d1746e3SAlessandro Astone list_for_each_entry_safe(pf, tmppf, pf_head, node) {
24472d1746e3SAlessandro Astone BUG_ON(pf->skip_size == 0);
24482d1746e3SAlessandro Astone list_del(&pf->node);
24492d1746e3SAlessandro Astone kfree(pf);
24502d1746e3SAlessandro Astone }
245109184ae9STodd Kjos BUG_ON(!list_empty(sgc_head));
245209184ae9STodd Kjos
245309184ae9STodd Kjos return ret > 0 ? -EINVAL : ret;
245409184ae9STodd Kjos }
245509184ae9STodd Kjos
245609184ae9STodd Kjos /**
245709184ae9STodd Kjos * binder_cleanup_deferred_txn_lists() - free specified lists
245809184ae9STodd Kjos * @sgc_head: list_head of scatter-gather copy list
245909184ae9STodd Kjos * @pf_head: list_head of pointer fixup list
246009184ae9STodd Kjos *
246109184ae9STodd Kjos * Called to clean up @sgc_head and @pf_head if there is an
246209184ae9STodd Kjos * error.
246309184ae9STodd Kjos */
binder_cleanup_deferred_txn_lists(struct list_head * sgc_head,struct list_head * pf_head)246409184ae9STodd Kjos static void binder_cleanup_deferred_txn_lists(struct list_head *sgc_head,
246509184ae9STodd Kjos struct list_head *pf_head)
246609184ae9STodd Kjos {
246709184ae9STodd Kjos struct binder_sg_copy *sgc, *tmpsgc;
246809184ae9STodd Kjos struct binder_ptr_fixup *pf, *tmppf;
246909184ae9STodd Kjos
247009184ae9STodd Kjos list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
247109184ae9STodd Kjos list_del(&sgc->node);
247209184ae9STodd Kjos kfree(sgc);
247309184ae9STodd Kjos }
247409184ae9STodd Kjos list_for_each_entry_safe(pf, tmppf, pf_head, node) {
247509184ae9STodd Kjos list_del(&pf->node);
247609184ae9STodd Kjos kfree(pf);
247709184ae9STodd Kjos }
247809184ae9STodd Kjos }
247909184ae9STodd Kjos
248009184ae9STodd Kjos /**
248109184ae9STodd Kjos * binder_defer_copy() - queue a scatter-gather buffer for copy
248209184ae9STodd Kjos * @sgc_head: list_head of scatter-gather copy list
248309184ae9STodd Kjos * @offset: binder buffer offset in target process
248409184ae9STodd Kjos * @sender_uaddr: user address in source process
248509184ae9STodd Kjos * @length: bytes to copy
248609184ae9STodd Kjos *
248709184ae9STodd Kjos * Specify a scatter-gather block to be copied. The actual copy must
248809184ae9STodd Kjos * be deferred until all the needed fixups are identified and queued.
248909184ae9STodd Kjos * Then the copy and fixups are done together so un-translated values
249009184ae9STodd Kjos * from the source are never visible in the target buffer.
249109184ae9STodd Kjos *
249209184ae9STodd Kjos * We are guaranteed that repeated calls to this function will have
249309184ae9STodd Kjos * monotonically increasing @offset values so the list will naturally
249409184ae9STodd Kjos * be ordered.
249509184ae9STodd Kjos *
249609184ae9STodd Kjos * Return: 0=success, else -errno
249709184ae9STodd Kjos */
binder_defer_copy(struct list_head * sgc_head,binder_size_t offset,const void __user * sender_uaddr,size_t length)249809184ae9STodd Kjos static int binder_defer_copy(struct list_head *sgc_head, binder_size_t offset,
249909184ae9STodd Kjos const void __user *sender_uaddr, size_t length)
250009184ae9STodd Kjos {
250109184ae9STodd Kjos struct binder_sg_copy *bc = kzalloc(sizeof(*bc), GFP_KERNEL);
250209184ae9STodd Kjos
250309184ae9STodd Kjos if (!bc)
250409184ae9STodd Kjos return -ENOMEM;
250509184ae9STodd Kjos
250609184ae9STodd Kjos bc->offset = offset;
250709184ae9STodd Kjos bc->sender_uaddr = sender_uaddr;
250809184ae9STodd Kjos bc->length = length;
250909184ae9STodd Kjos INIT_LIST_HEAD(&bc->node);
251009184ae9STodd Kjos
251109184ae9STodd Kjos /*
251209184ae9STodd Kjos * We are guaranteed that the deferred copies are in-order
251309184ae9STodd Kjos * so just add to the tail.
251409184ae9STodd Kjos */
251509184ae9STodd Kjos list_add_tail(&bc->node, sgc_head);
251609184ae9STodd Kjos
251709184ae9STodd Kjos return 0;
251809184ae9STodd Kjos }
251909184ae9STodd Kjos
252009184ae9STodd Kjos /**
252109184ae9STodd Kjos * binder_add_fixup() - queue a fixup to be applied to sg copy
252209184ae9STodd Kjos * @pf_head: list_head of binder ptr fixup list
252309184ae9STodd Kjos * @offset: binder buffer offset in target process
252409184ae9STodd Kjos * @fixup: bytes to be copied for fixup
252509184ae9STodd Kjos * @skip_size: bytes to skip when copying (fixup will be applied later)
252609184ae9STodd Kjos *
252709184ae9STodd Kjos * Add the specified fixup to a list ordered by @offset. When copying
252809184ae9STodd Kjos * the scatter-gather buffers, the fixup will be copied instead of
252909184ae9STodd Kjos * data from the source buffer. For BINDER_TYPE_FDA fixups, the fixup
253009184ae9STodd Kjos * will be applied later (in target process context), so we just skip
253109184ae9STodd Kjos * the bytes specified by @skip_size. If @skip_size is 0, we copy the
253209184ae9STodd Kjos * value in @fixup.
253309184ae9STodd Kjos *
253409184ae9STodd Kjos * This function is called *mostly* in @offset order, but there are
253509184ae9STodd Kjos * exceptions. Since out-of-order inserts are relatively uncommon,
253609184ae9STodd Kjos * we insert the new element by searching backward from the tail of
253709184ae9STodd Kjos * the list.
253809184ae9STodd Kjos *
253909184ae9STodd Kjos * Return: 0=success, else -errno
254009184ae9STodd Kjos */
binder_add_fixup(struct list_head * pf_head,binder_size_t offset,binder_uintptr_t fixup,size_t skip_size)254109184ae9STodd Kjos static int binder_add_fixup(struct list_head *pf_head, binder_size_t offset,
254209184ae9STodd Kjos binder_uintptr_t fixup, size_t skip_size)
254309184ae9STodd Kjos {
254409184ae9STodd Kjos struct binder_ptr_fixup *pf = kzalloc(sizeof(*pf), GFP_KERNEL);
254509184ae9STodd Kjos struct binder_ptr_fixup *tmppf;
254609184ae9STodd Kjos
254709184ae9STodd Kjos if (!pf)
254809184ae9STodd Kjos return -ENOMEM;
254909184ae9STodd Kjos
255009184ae9STodd Kjos pf->offset = offset;
255109184ae9STodd Kjos pf->fixup_data = fixup;
255209184ae9STodd Kjos pf->skip_size = skip_size;
255309184ae9STodd Kjos INIT_LIST_HEAD(&pf->node);
255409184ae9STodd Kjos
255509184ae9STodd Kjos /* Fixups are *mostly* added in-order, but there are some
255609184ae9STodd Kjos * exceptions. Look backwards through list for insertion point.
255709184ae9STodd Kjos */
255809184ae9STodd Kjos list_for_each_entry_reverse(tmppf, pf_head, node) {
255909184ae9STodd Kjos if (tmppf->offset < pf->offset) {
256009184ae9STodd Kjos list_add(&pf->node, &tmppf->node);
256109184ae9STodd Kjos return 0;
256209184ae9STodd Kjos }
256309184ae9STodd Kjos }
256409184ae9STodd Kjos /*
256509184ae9STodd Kjos * if we get here, then the new offset is the lowest so
256609184ae9STodd Kjos * insert at the head
256709184ae9STodd Kjos */
256809184ae9STodd Kjos list_add(&pf->node, pf_head);
256909184ae9STodd Kjos return 0;
257009184ae9STodd Kjos }
257109184ae9STodd Kjos
binder_translate_fd_array(struct list_head * pf_head,struct binder_fd_array_object * fda,const void __user * sender_ubuffer,struct binder_buffer_object * parent,struct binder_buffer_object * sender_uparent,struct binder_transaction * t,struct binder_thread * thread,struct binder_transaction * in_reply_to)257209184ae9STodd Kjos static int binder_translate_fd_array(struct list_head *pf_head,
257309184ae9STodd Kjos struct binder_fd_array_object *fda,
2574656e01f3STodd Kjos const void __user *sender_ubuffer,
2575def95c73SMartijn Coenen struct binder_buffer_object *parent,
2576656e01f3STodd Kjos struct binder_buffer_object *sender_uparent,
2577def95c73SMartijn Coenen struct binder_transaction *t,
2578def95c73SMartijn Coenen struct binder_thread *thread,
2579def95c73SMartijn Coenen struct binder_transaction *in_reply_to)
2580def95c73SMartijn Coenen {
258144d8047fSTodd Kjos binder_size_t fdi, fd_buf_size;
2582bde4a19fSTodd Kjos binder_size_t fda_offset;
2583656e01f3STodd Kjos const void __user *sender_ufda_base;
2584def95c73SMartijn Coenen struct binder_proc *proc = thread->proc;
258509184ae9STodd Kjos int ret;
2586def95c73SMartijn Coenen
2587ef38de92SAlessandro Astone if (fda->num_fds == 0)
2588ef38de92SAlessandro Astone return 0;
2589ef38de92SAlessandro Astone
2590def95c73SMartijn Coenen fd_buf_size = sizeof(u32) * fda->num_fds;
2591def95c73SMartijn Coenen if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2592def95c73SMartijn Coenen binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2593def95c73SMartijn Coenen proc->pid, thread->pid, (u64)fda->num_fds);
2594def95c73SMartijn Coenen return -EINVAL;
2595def95c73SMartijn Coenen }
2596def95c73SMartijn Coenen if (fd_buf_size > parent->length ||
2597def95c73SMartijn Coenen fda->parent_offset > parent->length - fd_buf_size) {
2598def95c73SMartijn Coenen /* No space for all file descriptors here. */
2599def95c73SMartijn Coenen binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2600def95c73SMartijn Coenen proc->pid, thread->pid, (u64)fda->num_fds);
2601def95c73SMartijn Coenen return -EINVAL;
2602def95c73SMartijn Coenen }
2603bde4a19fSTodd Kjos /*
2604bde4a19fSTodd Kjos * the source data for binder_buffer_object is visible
2605bde4a19fSTodd Kjos * to user-space and the @buffer element is the user
2606bde4a19fSTodd Kjos * pointer to the buffer_object containing the fd_array.
2607bde4a19fSTodd Kjos * Convert the address to an offset relative to
2608bde4a19fSTodd Kjos * the base of the transaction buffer.
2609bde4a19fSTodd Kjos */
2610bde4a19fSTodd Kjos fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2611bde4a19fSTodd Kjos fda->parent_offset;
26129a0a930fSArnd Bergmann sender_ufda_base = (void __user *)(uintptr_t)sender_uparent->buffer +
26139a0a930fSArnd Bergmann fda->parent_offset;
2614656e01f3STodd Kjos
2615656e01f3STodd Kjos if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32)) ||
2616656e01f3STodd Kjos !IS_ALIGNED((unsigned long)sender_ufda_base, sizeof(u32))) {
2617def95c73SMartijn Coenen binder_user_error("%d:%d parent offset not aligned correctly.\n",
2618def95c73SMartijn Coenen proc->pid, thread->pid);
2619def95c73SMartijn Coenen return -EINVAL;
2620def95c73SMartijn Coenen }
262109184ae9STodd Kjos ret = binder_add_fixup(pf_head, fda_offset, 0, fda->num_fds * sizeof(u32));
262209184ae9STodd Kjos if (ret)
262309184ae9STodd Kjos return ret;
262409184ae9STodd Kjos
2625def95c73SMartijn Coenen for (fdi = 0; fdi < fda->num_fds; fdi++) {
26268ced0c62STodd Kjos u32 fd;
2627bde4a19fSTodd Kjos binder_size_t offset = fda_offset + fdi * sizeof(fd);
2628656e01f3STodd Kjos binder_size_t sender_uoffset = fdi * sizeof(fd);
26298ced0c62STodd Kjos
2630656e01f3STodd Kjos ret = copy_from_user(&fd, sender_ufda_base + sender_uoffset, sizeof(fd));
2631bb4a2e48STodd Kjos if (!ret)
26328ced0c62STodd Kjos ret = binder_translate_fd(fd, offset, t, thread,
2633def95c73SMartijn Coenen in_reply_to);
2634fe6b1869STodd Kjos if (ret)
2635fe6b1869STodd Kjos return ret > 0 ? -EINVAL : ret;
2636def95c73SMartijn Coenen }
2637def95c73SMartijn Coenen return 0;
2638def95c73SMartijn Coenen }
2639def95c73SMartijn Coenen
binder_fixup_parent(struct list_head * pf_head,struct binder_transaction * t,struct binder_thread * thread,struct binder_buffer_object * bp,binder_size_t off_start_offset,binder_size_t num_valid,binder_size_t last_fixup_obj_off,binder_size_t last_fixup_min_off)264009184ae9STodd Kjos static int binder_fixup_parent(struct list_head *pf_head,
264109184ae9STodd Kjos struct binder_transaction *t,
26427980240bSMartijn Coenen struct binder_thread *thread,
26437980240bSMartijn Coenen struct binder_buffer_object *bp,
2644db6b0b81STodd Kjos binder_size_t off_start_offset,
26457980240bSMartijn Coenen binder_size_t num_valid,
2646db6b0b81STodd Kjos binder_size_t last_fixup_obj_off,
26477980240bSMartijn Coenen binder_size_t last_fixup_min_off)
26487980240bSMartijn Coenen {
26497980240bSMartijn Coenen struct binder_buffer_object *parent;
26507980240bSMartijn Coenen struct binder_buffer *b = t->buffer;
26517980240bSMartijn Coenen struct binder_proc *proc = thread->proc;
26527980240bSMartijn Coenen struct binder_proc *target_proc = t->to_proc;
2653db6b0b81STodd Kjos struct binder_object object;
2654db6b0b81STodd Kjos binder_size_t buffer_offset;
2655db6b0b81STodd Kjos binder_size_t parent_offset;
26567980240bSMartijn Coenen
26577980240bSMartijn Coenen if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
26587980240bSMartijn Coenen return 0;
26597980240bSMartijn Coenen
2660db6b0b81STodd Kjos parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2661db6b0b81STodd Kjos off_start_offset, &parent_offset,
2662db6b0b81STodd Kjos num_valid);
26637980240bSMartijn Coenen if (!parent) {
26647980240bSMartijn Coenen binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
26657980240bSMartijn Coenen proc->pid, thread->pid);
26667980240bSMartijn Coenen return -EINVAL;
26677980240bSMartijn Coenen }
26687980240bSMartijn Coenen
2669db6b0b81STodd Kjos if (!binder_validate_fixup(target_proc, b, off_start_offset,
2670db6b0b81STodd Kjos parent_offset, bp->parent_offset,
2671db6b0b81STodd Kjos last_fixup_obj_off,
26727980240bSMartijn Coenen last_fixup_min_off)) {
26737980240bSMartijn Coenen binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
26747980240bSMartijn Coenen proc->pid, thread->pid);
26757980240bSMartijn Coenen return -EINVAL;
26767980240bSMartijn Coenen }
26777980240bSMartijn Coenen
26787980240bSMartijn Coenen if (parent->length < sizeof(binder_uintptr_t) ||
26797980240bSMartijn Coenen bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
26807980240bSMartijn Coenen /* No space for a pointer here! */
26817980240bSMartijn Coenen binder_user_error("%d:%d got transaction with invalid parent offset\n",
26827980240bSMartijn Coenen proc->pid, thread->pid);
26837980240bSMartijn Coenen return -EINVAL;
26847980240bSMartijn Coenen }
2685db6b0b81STodd Kjos buffer_offset = bp->parent_offset +
2686bde4a19fSTodd Kjos (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
268709184ae9STodd Kjos return binder_add_fixup(pf_head, buffer_offset, bp->buffer, 0);
26887980240bSMartijn Coenen }
26897980240bSMartijn Coenen
2690408c68b1SMartijn Coenen /**
26919864bb48SLi Li * binder_can_update_transaction() - Can a txn be superseded by an updated one?
26929864bb48SLi Li * @t1: the pending async txn in the frozen process
26939864bb48SLi Li * @t2: the new async txn to supersede the outdated pending one
26949864bb48SLi Li *
26959864bb48SLi Li * Return: true if t2 can supersede t1
26969864bb48SLi Li * false if t2 can not supersede t1
26979864bb48SLi Li */
binder_can_update_transaction(struct binder_transaction * t1,struct binder_transaction * t2)26989864bb48SLi Li static bool binder_can_update_transaction(struct binder_transaction *t1,
26999864bb48SLi Li struct binder_transaction *t2)
27009864bb48SLi Li {
27019864bb48SLi Li if ((t1->flags & t2->flags & (TF_ONE_WAY | TF_UPDATE_TXN)) !=
27029864bb48SLi Li (TF_ONE_WAY | TF_UPDATE_TXN) || !t1->to_proc || !t2->to_proc)
27039864bb48SLi Li return false;
27049864bb48SLi Li if (t1->to_proc->tsk == t2->to_proc->tsk && t1->code == t2->code &&
27059864bb48SLi Li t1->flags == t2->flags && t1->buffer->pid == t2->buffer->pid &&
27069864bb48SLi Li t1->buffer->target_node->ptr == t2->buffer->target_node->ptr &&
27079864bb48SLi Li t1->buffer->target_node->cookie == t2->buffer->target_node->cookie)
27089864bb48SLi Li return true;
27099864bb48SLi Li return false;
27109864bb48SLi Li }
27119864bb48SLi Li
27129864bb48SLi Li /**
27139864bb48SLi Li * binder_find_outdated_transaction_ilocked() - Find the outdated transaction
27149864bb48SLi Li * @t: new async transaction
27159864bb48SLi Li * @target_list: list to find outdated transaction
27169864bb48SLi Li *
27179864bb48SLi Li * Return: the outdated transaction if found
27189864bb48SLi Li * NULL if no outdated transacton can be found
27199864bb48SLi Li *
27209864bb48SLi Li * Requires the proc->inner_lock to be held.
27219864bb48SLi Li */
27229864bb48SLi Li static struct binder_transaction *
binder_find_outdated_transaction_ilocked(struct binder_transaction * t,struct list_head * target_list)27239864bb48SLi Li binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
27249864bb48SLi Li struct list_head *target_list)
27259864bb48SLi Li {
27269864bb48SLi Li struct binder_work *w;
27279864bb48SLi Li
27289864bb48SLi Li list_for_each_entry(w, target_list, entry) {
27299864bb48SLi Li struct binder_transaction *t_queued;
27309864bb48SLi Li
27319864bb48SLi Li if (w->type != BINDER_WORK_TRANSACTION)
27329864bb48SLi Li continue;
27339864bb48SLi Li t_queued = container_of(w, struct binder_transaction, work);
27349864bb48SLi Li if (binder_can_update_transaction(t_queued, t))
27359864bb48SLi Li return t_queued;
27369864bb48SLi Li }
27379864bb48SLi Li return NULL;
27389864bb48SLi Li }
27399864bb48SLi Li
27409864bb48SLi Li /**
2741408c68b1SMartijn Coenen * binder_proc_transaction() - sends a transaction to a process and wakes it up
2742408c68b1SMartijn Coenen * @t: transaction to send
2743408c68b1SMartijn Coenen * @proc: process to send the transaction to
2744408c68b1SMartijn Coenen * @thread: thread in @proc to send the transaction to (may be NULL)
2745408c68b1SMartijn Coenen *
2746408c68b1SMartijn Coenen * This function queues a transaction to the specified process. It will try
2747408c68b1SMartijn Coenen * to find a thread in the target process to handle the transaction and
2748408c68b1SMartijn Coenen * wake it up. If no thread is found, the work is queued to the proc
2749408c68b1SMartijn Coenen * waitqueue.
2750408c68b1SMartijn Coenen *
2751408c68b1SMartijn Coenen * If the @thread parameter is not NULL, the transaction is always queued
2752408c68b1SMartijn Coenen * to the waitlist of that specific thread.
2753408c68b1SMartijn Coenen *
2754432ff1e9SMarco Ballesio * Return: 0 if the transaction was successfully queued
2755432ff1e9SMarco Ballesio * BR_DEAD_REPLY if the target process or thread is dead
27560567461aSLi Li * BR_FROZEN_REPLY if the target process or thread is frozen and
27570567461aSLi Li * the sync transaction was rejected
27580567461aSLi Li * BR_TRANSACTION_PENDING_FROZEN if the target process is frozen
27590567461aSLi Li * and the async transaction was successfully queued
2760408c68b1SMartijn Coenen */
binder_proc_transaction(struct binder_transaction * t,struct binder_proc * proc,struct binder_thread * thread)2761432ff1e9SMarco Ballesio static int binder_proc_transaction(struct binder_transaction *t,
2762408c68b1SMartijn Coenen struct binder_proc *proc,
2763408c68b1SMartijn Coenen struct binder_thread *thread)
2764408c68b1SMartijn Coenen {
2765408c68b1SMartijn Coenen struct binder_node *node = t->buffer->target_node;
2766408c68b1SMartijn Coenen bool oneway = !!(t->flags & TF_ONE_WAY);
2767148ade2cSMartijn Coenen bool pending_async = false;
27689864bb48SLi Li struct binder_transaction *t_outdated = NULL;
27690567461aSLi Li bool frozen = false;
2770408c68b1SMartijn Coenen
2771408c68b1SMartijn Coenen BUG_ON(!node);
2772408c68b1SMartijn Coenen binder_node_lock(node);
2773408c68b1SMartijn Coenen if (oneway) {
2774408c68b1SMartijn Coenen BUG_ON(thread);
27758df5b949SMrinal Pandey if (node->has_async_transaction)
2776148ade2cSMartijn Coenen pending_async = true;
27778df5b949SMrinal Pandey else
2778197410adSGustavo A. R. Silva node->has_async_transaction = true;
2779408c68b1SMartijn Coenen }
2780408c68b1SMartijn Coenen
2781408c68b1SMartijn Coenen binder_inner_proc_lock(proc);
2782ae28c1beSMarco Ballesio if (proc->is_frozen) {
27830567461aSLi Li frozen = true;
2784ae28c1beSMarco Ballesio proc->sync_recv |= !oneway;
2785ae28c1beSMarco Ballesio proc->async_recv |= oneway;
2786ae28c1beSMarco Ballesio }
2787408c68b1SMartijn Coenen
27880567461aSLi Li if ((frozen && !oneway) || proc->is_dead ||
2789432ff1e9SMarco Ballesio (thread && thread->is_dead)) {
2790408c68b1SMartijn Coenen binder_inner_proc_unlock(proc);
2791408c68b1SMartijn Coenen binder_node_unlock(node);
27920567461aSLi Li return frozen ? BR_FROZEN_REPLY : BR_DEAD_REPLY;
2793408c68b1SMartijn Coenen }
2794408c68b1SMartijn Coenen
2795148ade2cSMartijn Coenen if (!thread && !pending_async)
2796408c68b1SMartijn Coenen thread = binder_select_thread_ilocked(proc);
2797408c68b1SMartijn Coenen
27989864bb48SLi Li if (thread) {
2799148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(thread, &t->work);
28009864bb48SLi Li } else if (!pending_async) {
2801148ade2cSMartijn Coenen binder_enqueue_work_ilocked(&t->work, &proc->todo);
28029864bb48SLi Li } else {
28030567461aSLi Li if ((t->flags & TF_UPDATE_TXN) && frozen) {
28049864bb48SLi Li t_outdated = binder_find_outdated_transaction_ilocked(t,
28059864bb48SLi Li &node->async_todo);
28069864bb48SLi Li if (t_outdated) {
28079864bb48SLi Li binder_debug(BINDER_DEBUG_TRANSACTION,
28089864bb48SLi Li "txn %d supersedes %d\n",
28099864bb48SLi Li t->debug_id, t_outdated->debug_id);
28109864bb48SLi Li list_del_init(&t_outdated->work.entry);
28119864bb48SLi Li proc->outstanding_txns--;
28129864bb48SLi Li }
28139864bb48SLi Li }
2814148ade2cSMartijn Coenen binder_enqueue_work_ilocked(&t->work, &node->async_todo);
28159864bb48SLi Li }
2816408c68b1SMartijn Coenen
2817148ade2cSMartijn Coenen if (!pending_async)
2818408c68b1SMartijn Coenen binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2819408c68b1SMartijn Coenen
2820432ff1e9SMarco Ballesio proc->outstanding_txns++;
2821408c68b1SMartijn Coenen binder_inner_proc_unlock(proc);
2822408c68b1SMartijn Coenen binder_node_unlock(node);
2823408c68b1SMartijn Coenen
28249864bb48SLi Li /*
28259864bb48SLi Li * To reduce potential contention, free the outdated transaction and
28269864bb48SLi Li * buffer after releasing the locks.
28279864bb48SLi Li */
28289864bb48SLi Li if (t_outdated) {
28299864bb48SLi Li struct binder_buffer *buffer = t_outdated->buffer;
28309864bb48SLi Li
28319864bb48SLi Li t_outdated->buffer = NULL;
28329864bb48SLi Li buffer->transaction = NULL;
28339864bb48SLi Li trace_binder_transaction_update_buffer_release(buffer);
2834bdc1c5faSCarlos Llamas binder_release_entire_buffer(proc, NULL, buffer, false);
28359864bb48SLi Li binder_alloc_free_buf(&proc->alloc, buffer);
28369864bb48SLi Li kfree(t_outdated);
28379864bb48SLi Li binder_stats_deleted(BINDER_STAT_TRANSACTION);
28389864bb48SLi Li }
28399864bb48SLi Li
28400567461aSLi Li if (oneway && frozen)
28410567461aSLi Li return BR_TRANSACTION_PENDING_FROZEN;
28420567461aSLi Li
2843432ff1e9SMarco Ballesio return 0;
2844408c68b1SMartijn Coenen }
2845408c68b1SMartijn Coenen
2846512cf465STodd Kjos /**
2847512cf465STodd Kjos * binder_get_node_refs_for_txn() - Get required refs on node for txn
2848512cf465STodd Kjos * @node: struct binder_node for which to get refs
2849ad228a34SRandy Dunlap * @procp: returns @node->proc if valid
2850ad228a34SRandy Dunlap * @error: if no @procp then returns BR_DEAD_REPLY
2851512cf465STodd Kjos *
2852512cf465STodd Kjos * User-space normally keeps the node alive when creating a transaction
2853512cf465STodd Kjos * since it has a reference to the target. The local strong ref keeps it
2854512cf465STodd Kjos * alive if the sending process dies before the target process processes
2855512cf465STodd Kjos * the transaction. If the source process is malicious or has a reference
2856512cf465STodd Kjos * counting bug, relying on the local strong ref can fail.
2857512cf465STodd Kjos *
2858512cf465STodd Kjos * Since user-space can cause the local strong ref to go away, we also take
2859512cf465STodd Kjos * a tmpref on the node to ensure it survives while we are constructing
2860512cf465STodd Kjos * the transaction. We also need a tmpref on the proc while we are
2861512cf465STodd Kjos * constructing the transaction, so we take that here as well.
2862512cf465STodd Kjos *
2863512cf465STodd Kjos * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2864ad228a34SRandy Dunlap * Also sets @procp if valid. If the @node->proc is NULL indicating that the
2865ad228a34SRandy Dunlap * target proc has died, @error is set to BR_DEAD_REPLY.
2866512cf465STodd Kjos */
binder_get_node_refs_for_txn(struct binder_node * node,struct binder_proc ** procp,uint32_t * error)2867512cf465STodd Kjos static struct binder_node *binder_get_node_refs_for_txn(
2868512cf465STodd Kjos struct binder_node *node,
2869512cf465STodd Kjos struct binder_proc **procp,
2870512cf465STodd Kjos uint32_t *error)
2871512cf465STodd Kjos {
2872512cf465STodd Kjos struct binder_node *target_node = NULL;
2873512cf465STodd Kjos
2874512cf465STodd Kjos binder_node_inner_lock(node);
2875512cf465STodd Kjos if (node->proc) {
2876512cf465STodd Kjos target_node = node;
2877512cf465STodd Kjos binder_inc_node_nilocked(node, 1, 0, NULL);
2878512cf465STodd Kjos binder_inc_node_tmpref_ilocked(node);
2879512cf465STodd Kjos node->proc->tmp_ref++;
2880512cf465STodd Kjos *procp = node->proc;
2881512cf465STodd Kjos } else
2882512cf465STodd Kjos *error = BR_DEAD_REPLY;
2883512cf465STodd Kjos binder_node_inner_unlock(node);
2884512cf465STodd Kjos
2885512cf465STodd Kjos return target_node;
2886512cf465STodd Kjos }
2887512cf465STodd Kjos
binder_set_txn_from_error(struct binder_transaction * t,int id,uint32_t command,int32_t param)2888bd32889eSCarlos Llamas static void binder_set_txn_from_error(struct binder_transaction *t, int id,
2889bd32889eSCarlos Llamas uint32_t command, int32_t param)
2890bd32889eSCarlos Llamas {
2891bd32889eSCarlos Llamas struct binder_thread *from = binder_get_txn_from_and_acq_inner(t);
2892bd32889eSCarlos Llamas
2893bd32889eSCarlos Llamas if (!from) {
2894bd32889eSCarlos Llamas /* annotation for sparse */
2895bd32889eSCarlos Llamas __release(&from->proc->inner_lock);
2896bd32889eSCarlos Llamas return;
2897bd32889eSCarlos Llamas }
2898bd32889eSCarlos Llamas
2899bd32889eSCarlos Llamas /* don't override existing errors */
2900bd32889eSCarlos Llamas if (from->ee.command == BR_OK)
2901bd32889eSCarlos Llamas binder_set_extended_error(&from->ee, id, command, param);
2902bd32889eSCarlos Llamas binder_inner_proc_unlock(from->proc);
2903bd32889eSCarlos Llamas binder_thread_dec_tmpref(from);
2904bd32889eSCarlos Llamas }
2905bd32889eSCarlos Llamas
binder_transaction(struct binder_proc * proc,struct binder_thread * thread,struct binder_transaction_data * tr,int reply,binder_size_t extra_buffers_size)2906777783e0SGreg Kroah-Hartman static void binder_transaction(struct binder_proc *proc,
2907777783e0SGreg Kroah-Hartman struct binder_thread *thread,
29084bfac80aSMartijn Coenen struct binder_transaction_data *tr, int reply,
29094bfac80aSMartijn Coenen binder_size_t extra_buffers_size)
2910777783e0SGreg Kroah-Hartman {
2911a056af42SMartijn Coenen int ret;
2912777783e0SGreg Kroah-Hartman struct binder_transaction *t;
291344b73962SSherry Yang struct binder_work *w;
2914777783e0SGreg Kroah-Hartman struct binder_work *tcomplete;
2915bde4a19fSTodd Kjos binder_size_t buffer_offset = 0;
2916bde4a19fSTodd Kjos binder_size_t off_start_offset, off_end_offset;
2917212265e5SArve Hjønnevåg binder_size_t off_min;
2918bde4a19fSTodd Kjos binder_size_t sg_buf_offset, sg_buf_end_offset;
29196d98eb95STodd Kjos binder_size_t user_offset = 0;
29207a4408c6STodd Kjos struct binder_proc *target_proc = NULL;
2921777783e0SGreg Kroah-Hartman struct binder_thread *target_thread = NULL;
2922777783e0SGreg Kroah-Hartman struct binder_node *target_node = NULL;
2923777783e0SGreg Kroah-Hartman struct binder_transaction *in_reply_to = NULL;
2924777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *e;
292557ada2fbSTodd Kjos uint32_t return_error = 0;
292657ada2fbSTodd Kjos uint32_t return_error_param = 0;
292757ada2fbSTodd Kjos uint32_t return_error_line = 0;
2928db6b0b81STodd Kjos binder_size_t last_fixup_obj_off = 0;
29297980240bSMartijn Coenen binder_size_t last_fixup_min_off = 0;
2930342e5c90SMartijn Coenen struct binder_context *context = proc->context;
2931d99c7333STodd Kjos int t_debug_id = atomic_inc_return(&binder_last_id);
293280093619SChuang Zhang ktime_t t_start_time = ktime_get();
2933ec74136dSTodd Kjos char *secctx = NULL;
2934ec74136dSTodd Kjos u32 secctx_sz = 0;
293509184ae9STodd Kjos struct list_head sgc_head;
293609184ae9STodd Kjos struct list_head pf_head;
29376d98eb95STodd Kjos const void __user *user_buffer = (const void __user *)
29386d98eb95STodd Kjos (uintptr_t)tr->data.ptr.buffer;
293909184ae9STodd Kjos INIT_LIST_HEAD(&sgc_head);
294009184ae9STodd Kjos INIT_LIST_HEAD(&pf_head);
2941777783e0SGreg Kroah-Hartman
2942777783e0SGreg Kroah-Hartman e = binder_transaction_log_add(&binder_transaction_log);
2943d99c7333STodd Kjos e->debug_id = t_debug_id;
2944777783e0SGreg Kroah-Hartman e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2945777783e0SGreg Kroah-Hartman e->from_proc = proc->pid;
2946777783e0SGreg Kroah-Hartman e->from_thread = thread->pid;
2947777783e0SGreg Kroah-Hartman e->target_handle = tr->target.handle;
2948777783e0SGreg Kroah-Hartman e->data_size = tr->data_size;
2949777783e0SGreg Kroah-Hartman e->offsets_size = tr->offsets_size;
295051d8a7ecSChristian Brauner strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME);
2951777783e0SGreg Kroah-Hartman
2952bd32889eSCarlos Llamas binder_inner_proc_lock(proc);
2953bd32889eSCarlos Llamas binder_set_extended_error(&thread->ee, t_debug_id, BR_OK, 0);
2954bd32889eSCarlos Llamas binder_inner_proc_unlock(proc);
2955bd32889eSCarlos Llamas
2956777783e0SGreg Kroah-Hartman if (reply) {
29570b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
2958777783e0SGreg Kroah-Hartman in_reply_to = thread->transaction_stack;
2959777783e0SGreg Kroah-Hartman if (in_reply_to == NULL) {
29600b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
2961777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got reply transaction with no transaction stack\n",
2962777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
2963777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
296457ada2fbSTodd Kjos return_error_param = -EPROTO;
296557ada2fbSTodd Kjos return_error_line = __LINE__;
2966777783e0SGreg Kroah-Hartman goto err_empty_call_stack;
2967777783e0SGreg Kroah-Hartman }
2968777783e0SGreg Kroah-Hartman if (in_reply_to->to_thread != thread) {
29697a4408c6STodd Kjos spin_lock(&in_reply_to->lock);
2970777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
2971777783e0SGreg Kroah-Hartman proc->pid, thread->pid, in_reply_to->debug_id,
2972777783e0SGreg Kroah-Hartman in_reply_to->to_proc ?
2973777783e0SGreg Kroah-Hartman in_reply_to->to_proc->pid : 0,
2974777783e0SGreg Kroah-Hartman in_reply_to->to_thread ?
2975777783e0SGreg Kroah-Hartman in_reply_to->to_thread->pid : 0);
29767a4408c6STodd Kjos spin_unlock(&in_reply_to->lock);
29770b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
2978777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
297957ada2fbSTodd Kjos return_error_param = -EPROTO;
298057ada2fbSTodd Kjos return_error_line = __LINE__;
2981777783e0SGreg Kroah-Hartman in_reply_to = NULL;
2982777783e0SGreg Kroah-Hartman goto err_bad_call_stack;
2983777783e0SGreg Kroah-Hartman }
2984777783e0SGreg Kroah-Hartman thread->transaction_stack = in_reply_to->to_parent;
29850b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
29860b89d69aSMartijn Coenen binder_set_nice(in_reply_to->saved_priority);
29870b89d69aSMartijn Coenen target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
2988777783e0SGreg Kroah-Hartman if (target_thread == NULL) {
2989324fa64cSTodd Kjos /* annotation for sparse */
2990324fa64cSTodd Kjos __release(&target_thread->proc->inner_lock);
2991a15dac8bSCarlos Llamas binder_txn_error("%d:%d reply target not found\n",
2992a15dac8bSCarlos Llamas thread->pid, proc->pid);
2993777783e0SGreg Kroah-Hartman return_error = BR_DEAD_REPLY;
299457ada2fbSTodd Kjos return_error_line = __LINE__;
2995777783e0SGreg Kroah-Hartman goto err_dead_binder;
2996777783e0SGreg Kroah-Hartman }
2997777783e0SGreg Kroah-Hartman if (target_thread->transaction_stack != in_reply_to) {
2998777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
2999777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3000777783e0SGreg Kroah-Hartman target_thread->transaction_stack ?
3001777783e0SGreg Kroah-Hartman target_thread->transaction_stack->debug_id : 0,
3002777783e0SGreg Kroah-Hartman in_reply_to->debug_id);
30030b89d69aSMartijn Coenen binder_inner_proc_unlock(target_thread->proc);
3004777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
300557ada2fbSTodd Kjos return_error_param = -EPROTO;
300657ada2fbSTodd Kjos return_error_line = __LINE__;
3007777783e0SGreg Kroah-Hartman in_reply_to = NULL;
3008777783e0SGreg Kroah-Hartman target_thread = NULL;
3009777783e0SGreg Kroah-Hartman goto err_dead_binder;
3010777783e0SGreg Kroah-Hartman }
3011777783e0SGreg Kroah-Hartman target_proc = target_thread->proc;
30127a4408c6STodd Kjos target_proc->tmp_ref++;
30130b89d69aSMartijn Coenen binder_inner_proc_unlock(target_thread->proc);
3014777783e0SGreg Kroah-Hartman } else {
3015777783e0SGreg Kroah-Hartman if (tr->target.handle) {
3016777783e0SGreg Kroah-Hartman struct binder_ref *ref;
3017777783e0SGreg Kroah-Hartman
3018eb34983bSTodd Kjos /*
3019eb34983bSTodd Kjos * There must already be a strong ref
3020eb34983bSTodd Kjos * on this node. If so, do a strong
3021eb34983bSTodd Kjos * increment on the node to ensure it
3022eb34983bSTodd Kjos * stays alive until the transaction is
3023eb34983bSTodd Kjos * done.
3024eb34983bSTodd Kjos */
30252c1838dcSTodd Kjos binder_proc_lock(proc);
30262c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, tr->target.handle,
30272c1838dcSTodd Kjos true);
3028eb34983bSTodd Kjos if (ref) {
3029512cf465STodd Kjos target_node = binder_get_node_refs_for_txn(
3030512cf465STodd Kjos ref->node, &target_proc,
3031512cf465STodd Kjos &return_error);
3032512cf465STodd Kjos } else {
30331ae14df5SRamji Jiyani binder_user_error("%d:%d got transaction to invalid handle, %u\n",
30341ae14df5SRamji Jiyani proc->pid, thread->pid, tr->target.handle);
3035777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
3036777783e0SGreg Kroah-Hartman }
3037512cf465STodd Kjos binder_proc_unlock(proc);
3038777783e0SGreg Kroah-Hartman } else {
3039c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
3040342e5c90SMartijn Coenen target_node = context->binder_context_mgr_node;
3041512cf465STodd Kjos if (target_node)
3042512cf465STodd Kjos target_node = binder_get_node_refs_for_txn(
3043512cf465STodd Kjos target_node, &target_proc,
3044512cf465STodd Kjos &return_error);
3045512cf465STodd Kjos else
3046777783e0SGreg Kroah-Hartman return_error = BR_DEAD_REPLY;
3047c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
304849ed9694SHridya Valsaraju if (target_node && target_proc->pid == proc->pid) {
30497aa135fcSMartijn Coenen binder_user_error("%d:%d got transaction to context manager from process owning it\n",
30507aa135fcSMartijn Coenen proc->pid, thread->pid);
30517aa135fcSMartijn Coenen return_error = BR_FAILED_REPLY;
30527aa135fcSMartijn Coenen return_error_param = -EINVAL;
30537aa135fcSMartijn Coenen return_error_line = __LINE__;
30547aa135fcSMartijn Coenen goto err_invalid_target_handle;
30557aa135fcSMartijn Coenen }
3056777783e0SGreg Kroah-Hartman }
3057512cf465STodd Kjos if (!target_node) {
3058a15dac8bSCarlos Llamas binder_txn_error("%d:%d cannot find target node\n",
3059a15dac8bSCarlos Llamas thread->pid, proc->pid);
3060512cf465STodd Kjos /*
3061512cf465STodd Kjos * return_error is set above
3062512cf465STodd Kjos */
3063512cf465STodd Kjos return_error_param = -EINVAL;
306457ada2fbSTodd Kjos return_error_line = __LINE__;
3065777783e0SGreg Kroah-Hartman goto err_dead_binder;
3066777783e0SGreg Kroah-Hartman }
3067512cf465STodd Kjos e->to_node = target_node->debug_id;
30684b836a14SJann Horn if (WARN_ON(proc == target_proc)) {
3069a15dac8bSCarlos Llamas binder_txn_error("%d:%d self transactions not allowed\n",
3070a15dac8bSCarlos Llamas thread->pid, proc->pid);
30714b836a14SJann Horn return_error = BR_FAILED_REPLY;
30724b836a14SJann Horn return_error_param = -EINVAL;
30734b836a14SJann Horn return_error_line = __LINE__;
30744b836a14SJann Horn goto err_invalid_target_handle;
30754b836a14SJann Horn }
307652f88693STodd Kjos if (security_binder_transaction(proc->cred,
307752f88693STodd Kjos target_proc->cred) < 0) {
3078a15dac8bSCarlos Llamas binder_txn_error("%d:%d transaction credentials failed\n",
3079a15dac8bSCarlos Llamas thread->pid, proc->pid);
308079af7307SStephen Smalley return_error = BR_FAILED_REPLY;
308157ada2fbSTodd Kjos return_error_param = -EPERM;
308257ada2fbSTodd Kjos return_error_line = __LINE__;
308379af7307SStephen Smalley goto err_invalid_target_handle;
308479af7307SStephen Smalley }
30850b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
308644b73962SSherry Yang
308744b73962SSherry Yang w = list_first_entry_or_null(&thread->todo,
308844b73962SSherry Yang struct binder_work, entry);
308944b73962SSherry Yang if (!(tr->flags & TF_ONE_WAY) && w &&
309044b73962SSherry Yang w->type == BINDER_WORK_TRANSACTION) {
309144b73962SSherry Yang /*
309244b73962SSherry Yang * Do not allow new outgoing transaction from a
309344b73962SSherry Yang * thread that has a transaction at the head of
309444b73962SSherry Yang * its todo list. Only need to check the head
309544b73962SSherry Yang * because binder_select_thread_ilocked picks a
309644b73962SSherry Yang * thread from proc->waiting_threads to enqueue
309744b73962SSherry Yang * the transaction, and nothing is queued to the
309844b73962SSherry Yang * todo list while the thread is on waiting_threads.
309944b73962SSherry Yang */
310044b73962SSherry Yang binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
310144b73962SSherry Yang proc->pid, thread->pid);
310244b73962SSherry Yang binder_inner_proc_unlock(proc);
310344b73962SSherry Yang return_error = BR_FAILED_REPLY;
310444b73962SSherry Yang return_error_param = -EPROTO;
310544b73962SSherry Yang return_error_line = __LINE__;
310644b73962SSherry Yang goto err_bad_todo_list;
310744b73962SSherry Yang }
310844b73962SSherry Yang
3109777783e0SGreg Kroah-Hartman if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3110777783e0SGreg Kroah-Hartman struct binder_transaction *tmp;
3111777783e0SGreg Kroah-Hartman
3112777783e0SGreg Kroah-Hartman tmp = thread->transaction_stack;
3113777783e0SGreg Kroah-Hartman if (tmp->to_thread != thread) {
31147a4408c6STodd Kjos spin_lock(&tmp->lock);
3115777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3116777783e0SGreg Kroah-Hartman proc->pid, thread->pid, tmp->debug_id,
3117777783e0SGreg Kroah-Hartman tmp->to_proc ? tmp->to_proc->pid : 0,
3118777783e0SGreg Kroah-Hartman tmp->to_thread ?
3119777783e0SGreg Kroah-Hartman tmp->to_thread->pid : 0);
31207a4408c6STodd Kjos spin_unlock(&tmp->lock);
31210b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3122777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
312357ada2fbSTodd Kjos return_error_param = -EPROTO;
312457ada2fbSTodd Kjos return_error_line = __LINE__;
3125777783e0SGreg Kroah-Hartman goto err_bad_call_stack;
3126777783e0SGreg Kroah-Hartman }
3127777783e0SGreg Kroah-Hartman while (tmp) {
31287a4408c6STodd Kjos struct binder_thread *from;
31297a4408c6STodd Kjos
31307a4408c6STodd Kjos spin_lock(&tmp->lock);
31317a4408c6STodd Kjos from = tmp->from;
31327a4408c6STodd Kjos if (from && from->proc == target_proc) {
31337a4408c6STodd Kjos atomic_inc(&from->tmp_ref);
31347a4408c6STodd Kjos target_thread = from;
31357a4408c6STodd Kjos spin_unlock(&tmp->lock);
31367a4408c6STodd Kjos break;
31377a4408c6STodd Kjos }
31387a4408c6STodd Kjos spin_unlock(&tmp->lock);
3139777783e0SGreg Kroah-Hartman tmp = tmp->from_parent;
3140777783e0SGreg Kroah-Hartman }
3141777783e0SGreg Kroah-Hartman }
31420b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3143777783e0SGreg Kroah-Hartman }
3144408c68b1SMartijn Coenen if (target_thread)
3145777783e0SGreg Kroah-Hartman e->to_thread = target_thread->pid;
3146777783e0SGreg Kroah-Hartman e->to_proc = target_proc->pid;
3147777783e0SGreg Kroah-Hartman
3148777783e0SGreg Kroah-Hartman /* TODO: reuse incoming transaction for reply */
3149777783e0SGreg Kroah-Hartman t = kzalloc(sizeof(*t), GFP_KERNEL);
3150777783e0SGreg Kroah-Hartman if (t == NULL) {
3151a15dac8bSCarlos Llamas binder_txn_error("%d:%d cannot allocate transaction\n",
3152a15dac8bSCarlos Llamas thread->pid, proc->pid);
3153777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
315457ada2fbSTodd Kjos return_error_param = -ENOMEM;
315557ada2fbSTodd Kjos return_error_line = __LINE__;
3156777783e0SGreg Kroah-Hartman goto err_alloc_t_failed;
3157777783e0SGreg Kroah-Hartman }
315844d8047fSTodd Kjos INIT_LIST_HEAD(&t->fd_fixups);
3159777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_TRANSACTION);
31607a4408c6STodd Kjos spin_lock_init(&t->lock);
3161777783e0SGreg Kroah-Hartman
3162777783e0SGreg Kroah-Hartman tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3163777783e0SGreg Kroah-Hartman if (tcomplete == NULL) {
3164a15dac8bSCarlos Llamas binder_txn_error("%d:%d cannot allocate work for transaction\n",
3165a15dac8bSCarlos Llamas thread->pid, proc->pid);
3166777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
316757ada2fbSTodd Kjos return_error_param = -ENOMEM;
316857ada2fbSTodd Kjos return_error_line = __LINE__;
3169777783e0SGreg Kroah-Hartman goto err_alloc_tcomplete_failed;
3170777783e0SGreg Kroah-Hartman }
3171777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3172777783e0SGreg Kroah-Hartman
3173d99c7333STodd Kjos t->debug_id = t_debug_id;
317480093619SChuang Zhang t->start_time = t_start_time;
3175777783e0SGreg Kroah-Hartman
3176777783e0SGreg Kroah-Hartman if (reply)
3177777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
31784bfac80aSMartijn Coenen "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3179777783e0SGreg Kroah-Hartman proc->pid, thread->pid, t->debug_id,
3180777783e0SGreg Kroah-Hartman target_proc->pid, target_thread->pid,
3181777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.buffer,
3182777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.offsets,
31834bfac80aSMartijn Coenen (u64)tr->data_size, (u64)tr->offsets_size,
31844bfac80aSMartijn Coenen (u64)extra_buffers_size);
3185777783e0SGreg Kroah-Hartman else
3186777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
31874bfac80aSMartijn Coenen "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3188777783e0SGreg Kroah-Hartman proc->pid, thread->pid, t->debug_id,
3189777783e0SGreg Kroah-Hartman target_proc->pid, target_node->debug_id,
3190777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.buffer,
3191777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.offsets,
31924bfac80aSMartijn Coenen (u64)tr->data_size, (u64)tr->offsets_size,
31934bfac80aSMartijn Coenen (u64)extra_buffers_size);
3194777783e0SGreg Kroah-Hartman
3195777783e0SGreg Kroah-Hartman if (!reply && !(tr->flags & TF_ONE_WAY))
3196777783e0SGreg Kroah-Hartman t->from = thread;
3197777783e0SGreg Kroah-Hartman else
3198777783e0SGreg Kroah-Hartman t->from = NULL;
3199c21c0f9aSChuang Zhang t->from_pid = proc->pid;
3200c21c0f9aSChuang Zhang t->from_tid = thread->pid;
3201c21a80caSTodd Kjos t->sender_euid = task_euid(proc->tsk);
3202777783e0SGreg Kroah-Hartman t->to_proc = target_proc;
3203777783e0SGreg Kroah-Hartman t->to_thread = target_thread;
3204777783e0SGreg Kroah-Hartman t->code = tr->code;
3205777783e0SGreg Kroah-Hartman t->flags = tr->flags;
3206777783e0SGreg Kroah-Hartman t->priority = task_nice(current);
3207777783e0SGreg Kroah-Hartman
3208ec74136dSTodd Kjos if (target_node && target_node->txn_security_ctx) {
3209ec74136dSTodd Kjos u32 secid;
32100b050950STodd Kjos size_t added_size;
3211ec74136dSTodd Kjos
32124d5b5539STodd Kjos security_cred_getsecid(proc->cred, &secid);
3213ec74136dSTodd Kjos ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
3214ec74136dSTodd Kjos if (ret) {
3215a15dac8bSCarlos Llamas binder_txn_error("%d:%d failed to get security context\n",
3216a15dac8bSCarlos Llamas thread->pid, proc->pid);
3217ec74136dSTodd Kjos return_error = BR_FAILED_REPLY;
3218ec74136dSTodd Kjos return_error_param = ret;
3219ec74136dSTodd Kjos return_error_line = __LINE__;
3220ec74136dSTodd Kjos goto err_get_secctx_failed;
3221ec74136dSTodd Kjos }
32220b050950STodd Kjos added_size = ALIGN(secctx_sz, sizeof(u64));
32230b050950STodd Kjos extra_buffers_size += added_size;
32240b050950STodd Kjos if (extra_buffers_size < added_size) {
3225a15dac8bSCarlos Llamas binder_txn_error("%d:%d integer overflow of extra_buffers_size\n",
3226a15dac8bSCarlos Llamas thread->pid, proc->pid);
32270b050950STodd Kjos return_error = BR_FAILED_REPLY;
322888f6c779SZhang Qilong return_error_param = -EINVAL;
32290b050950STodd Kjos return_error_line = __LINE__;
32300b050950STodd Kjos goto err_bad_extra_size;
32310b050950STodd Kjos }
3232ec74136dSTodd Kjos }
3233ec74136dSTodd Kjos
3234777783e0SGreg Kroah-Hartman trace_binder_transaction(reply, t, target_node);
3235777783e0SGreg Kroah-Hartman
323619c98724STodd Kjos t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
32374bfac80aSMartijn Coenen tr->offsets_size, extra_buffers_size,
3238261e7818SMartijn Coenen !reply && (t->flags & TF_ONE_WAY), current->tgid);
323957ada2fbSTodd Kjos if (IS_ERR(t->buffer)) {
3240a15dac8bSCarlos Llamas char *s;
3241a15dac8bSCarlos Llamas
3242a15dac8bSCarlos Llamas ret = PTR_ERR(t->buffer);
3243a15dac8bSCarlos Llamas s = (ret == -ESRCH) ? ": vma cleared, target dead or dying"
3244a15dac8bSCarlos Llamas : (ret == -ENOSPC) ? ": no space left"
3245a15dac8bSCarlos Llamas : (ret == -ENOMEM) ? ": memory allocation failed"
3246a15dac8bSCarlos Llamas : "";
3247a15dac8bSCarlos Llamas binder_txn_error("cannot allocate buffer%s", s);
3248a15dac8bSCarlos Llamas
324957ada2fbSTodd Kjos return_error_param = PTR_ERR(t->buffer);
325057ada2fbSTodd Kjos return_error = return_error_param == -ESRCH ?
325157ada2fbSTodd Kjos BR_DEAD_REPLY : BR_FAILED_REPLY;
325257ada2fbSTodd Kjos return_error_line = __LINE__;
325357ada2fbSTodd Kjos t->buffer = NULL;
3254777783e0SGreg Kroah-Hartman goto err_binder_alloc_buf_failed;
3255777783e0SGreg Kroah-Hartman }
3256ec74136dSTodd Kjos if (secctx) {
3257bb4a2e48STodd Kjos int err;
3258ec74136dSTodd Kjos size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3259ec74136dSTodd Kjos ALIGN(tr->offsets_size, sizeof(void *)) +
3260ec74136dSTodd Kjos ALIGN(extra_buffers_size, sizeof(void *)) -
3261ec74136dSTodd Kjos ALIGN(secctx_sz, sizeof(u64));
3262ec74136dSTodd Kjos
3263bde4a19fSTodd Kjos t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
3264bb4a2e48STodd Kjos err = binder_alloc_copy_to_buffer(&target_proc->alloc,
32658ced0c62STodd Kjos t->buffer, buf_offset,
32668ced0c62STodd Kjos secctx, secctx_sz);
3267bb4a2e48STodd Kjos if (err) {
3268bb4a2e48STodd Kjos t->security_ctx = 0;
3269bb4a2e48STodd Kjos WARN_ON(1);
3270bb4a2e48STodd Kjos }
3271ec74136dSTodd Kjos security_release_secctx(secctx, secctx_sz);
3272ec74136dSTodd Kjos secctx = NULL;
3273ec74136dSTodd Kjos }
3274777783e0SGreg Kroah-Hartman t->buffer->debug_id = t->debug_id;
3275777783e0SGreg Kroah-Hartman t->buffer->transaction = t;
3276777783e0SGreg Kroah-Hartman t->buffer->target_node = target_node;
32770f966cbaSTodd Kjos t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF);
3278777783e0SGreg Kroah-Hartman trace_binder_transaction_alloc_buf(t->buffer);
3279777783e0SGreg Kroah-Hartman
32801a7c3d9bSTodd Kjos if (binder_alloc_copy_user_to_buffer(
32811a7c3d9bSTodd Kjos &target_proc->alloc,
32821a7c3d9bSTodd Kjos t->buffer,
32831a7c3d9bSTodd Kjos ALIGN(tr->data_size, sizeof(void *)),
32841a7c3d9bSTodd Kjos (const void __user *)
32851a7c3d9bSTodd Kjos (uintptr_t)tr->data.ptr.offsets,
32861a7c3d9bSTodd Kjos tr->offsets_size)) {
3287777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3288777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
3289777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
329057ada2fbSTodd Kjos return_error_param = -EFAULT;
329157ada2fbSTodd Kjos return_error_line = __LINE__;
3292777783e0SGreg Kroah-Hartman goto err_copy_data_failed;
3293777783e0SGreg Kroah-Hartman }
3294777783e0SGreg Kroah-Hartman if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3295777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3296777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)tr->offsets_size);
3297777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
329857ada2fbSTodd Kjos return_error_param = -EINVAL;
329957ada2fbSTodd Kjos return_error_line = __LINE__;
3300777783e0SGreg Kroah-Hartman goto err_bad_offset;
3301777783e0SGreg Kroah-Hartman }
33027980240bSMartijn Coenen if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
33037980240bSMartijn Coenen binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
33047980240bSMartijn Coenen proc->pid, thread->pid,
33057980240bSMartijn Coenen (u64)extra_buffers_size);
33067980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
330757ada2fbSTodd Kjos return_error_param = -EINVAL;
330857ada2fbSTodd Kjos return_error_line = __LINE__;
33097980240bSMartijn Coenen goto err_bad_offset;
33107980240bSMartijn Coenen }
3311bde4a19fSTodd Kjos off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3312bde4a19fSTodd Kjos buffer_offset = off_start_offset;
3313bde4a19fSTodd Kjos off_end_offset = off_start_offset + tr->offsets_size;
3314bde4a19fSTodd Kjos sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3315a5658706SMartijn Coenen sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3316a5658706SMartijn Coenen ALIGN(secctx_sz, sizeof(u64));
3317212265e5SArve Hjønnevåg off_min = 0;
3318bde4a19fSTodd Kjos for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3319bde4a19fSTodd Kjos buffer_offset += sizeof(binder_size_t)) {
3320feba3900SMartijn Coenen struct binder_object_header *hdr;
33218ced0c62STodd Kjos size_t object_size;
33227a67a393STodd Kjos struct binder_object object;
33238ced0c62STodd Kjos binder_size_t object_offset;
33246d98eb95STodd Kjos binder_size_t copy_size;
3325777783e0SGreg Kroah-Hartman
3326bb4a2e48STodd Kjos if (binder_alloc_copy_from_buffer(&target_proc->alloc,
33278ced0c62STodd Kjos &object_offset,
33288ced0c62STodd Kjos t->buffer,
33298ced0c62STodd Kjos buffer_offset,
3330bb4a2e48STodd Kjos sizeof(object_offset))) {
3331a15dac8bSCarlos Llamas binder_txn_error("%d:%d copy offset from buffer failed\n",
3332a15dac8bSCarlos Llamas thread->pid, proc->pid);
3333bb4a2e48STodd Kjos return_error = BR_FAILED_REPLY;
3334bb4a2e48STodd Kjos return_error_param = -EINVAL;
3335bb4a2e48STodd Kjos return_error_line = __LINE__;
3336bb4a2e48STodd Kjos goto err_bad_offset;
3337bb4a2e48STodd Kjos }
33386d98eb95STodd Kjos
33396d98eb95STodd Kjos /*
33406d98eb95STodd Kjos * Copy the source user buffer up to the next object
33416d98eb95STodd Kjos * that will be processed.
33426d98eb95STodd Kjos */
33436d98eb95STodd Kjos copy_size = object_offset - user_offset;
33446d98eb95STodd Kjos if (copy_size && (user_offset > object_offset ||
3345*1f33d9f1SCarlos Llamas object_offset > tr->data_size ||
33466d98eb95STodd Kjos binder_alloc_copy_user_to_buffer(
33476d98eb95STodd Kjos &target_proc->alloc,
33486d98eb95STodd Kjos t->buffer, user_offset,
33496d98eb95STodd Kjos user_buffer + user_offset,
33506d98eb95STodd Kjos copy_size))) {
33516d98eb95STodd Kjos binder_user_error("%d:%d got transaction with invalid data ptr\n",
33526d98eb95STodd Kjos proc->pid, thread->pid);
33536d98eb95STodd Kjos return_error = BR_FAILED_REPLY;
33546d98eb95STodd Kjos return_error_param = -EFAULT;
33556d98eb95STodd Kjos return_error_line = __LINE__;
33566d98eb95STodd Kjos goto err_copy_data_failed;
33576d98eb95STodd Kjos }
33586d98eb95STodd Kjos object_size = binder_get_object(target_proc, user_buffer,
33596d98eb95STodd Kjos t->buffer, object_offset, &object);
33608ced0c62STodd Kjos if (object_size == 0 || object_offset < off_min) {
3361feba3900SMartijn Coenen binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
33628ced0c62STodd Kjos proc->pid, thread->pid,
33638ced0c62STodd Kjos (u64)object_offset,
3364212265e5SArve Hjønnevåg (u64)off_min,
3365feba3900SMartijn Coenen (u64)t->buffer->data_size);
3366777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
336757ada2fbSTodd Kjos return_error_param = -EINVAL;
336857ada2fbSTodd Kjos return_error_line = __LINE__;
3369777783e0SGreg Kroah-Hartman goto err_bad_offset;
3370777783e0SGreg Kroah-Hartman }
33716d98eb95STodd Kjos /*
33726d98eb95STodd Kjos * Set offset to the next buffer fragment to be
33736d98eb95STodd Kjos * copied
33746d98eb95STodd Kjos */
33756d98eb95STodd Kjos user_offset = object_offset + object_size;
3376feba3900SMartijn Coenen
33777a67a393STodd Kjos hdr = &object.hdr;
33788ced0c62STodd Kjos off_min = object_offset + object_size;
3379feba3900SMartijn Coenen switch (hdr->type) {
3380777783e0SGreg Kroah-Hartman case BINDER_TYPE_BINDER:
3381777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_BINDER: {
3382feba3900SMartijn Coenen struct flat_binder_object *fp;
3383777783e0SGreg Kroah-Hartman
3384feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
3385a056af42SMartijn Coenen ret = binder_translate_binder(fp, t, thread);
3386bb4a2e48STodd Kjos
3387bb4a2e48STodd Kjos if (ret < 0 ||
3388bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3389bb4a2e48STodd Kjos t->buffer,
3390bb4a2e48STodd Kjos object_offset,
3391bb4a2e48STodd Kjos fp, sizeof(*fp))) {
3392a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate binder failed\n",
3393a15dac8bSCarlos Llamas thread->pid, proc->pid);
3394777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
339557ada2fbSTodd Kjos return_error_param = ret;
339657ada2fbSTodd Kjos return_error_line = __LINE__;
3397a056af42SMartijn Coenen goto err_translate_failed;
3398777783e0SGreg Kroah-Hartman }
3399777783e0SGreg Kroah-Hartman } break;
3400777783e0SGreg Kroah-Hartman case BINDER_TYPE_HANDLE:
3401777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_HANDLE: {
3402feba3900SMartijn Coenen struct flat_binder_object *fp;
34030a3ffab9SArve Hjønnevåg
3404feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
3405a056af42SMartijn Coenen ret = binder_translate_handle(fp, t, thread);
3406bb4a2e48STodd Kjos if (ret < 0 ||
3407bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3408bb4a2e48STodd Kjos t->buffer,
3409bb4a2e48STodd Kjos object_offset,
3410bb4a2e48STodd Kjos fp, sizeof(*fp))) {
3411a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate handle failed\n",
3412a15dac8bSCarlos Llamas thread->pid, proc->pid);
3413777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
341457ada2fbSTodd Kjos return_error_param = ret;
341557ada2fbSTodd Kjos return_error_line = __LINE__;
3416a056af42SMartijn Coenen goto err_translate_failed;
3417777783e0SGreg Kroah-Hartman }
3418777783e0SGreg Kroah-Hartman } break;
3419777783e0SGreg Kroah-Hartman
3420777783e0SGreg Kroah-Hartman case BINDER_TYPE_FD: {
3421feba3900SMartijn Coenen struct binder_fd_object *fp = to_binder_fd_object(hdr);
34228ced0c62STodd Kjos binder_size_t fd_offset = object_offset +
34238ced0c62STodd Kjos (uintptr_t)&fp->fd - (uintptr_t)fp;
34248ced0c62STodd Kjos int ret = binder_translate_fd(fp->fd, fd_offset, t,
34258ced0c62STodd Kjos thread, in_reply_to);
3426777783e0SGreg Kroah-Hartman
3427bb4a2e48STodd Kjos fp->pad_binder = 0;
3428bb4a2e48STodd Kjos if (ret < 0 ||
3429bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3430bb4a2e48STodd Kjos t->buffer,
3431bb4a2e48STodd Kjos object_offset,
3432bb4a2e48STodd Kjos fp, sizeof(*fp))) {
3433a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate fd failed\n",
3434a15dac8bSCarlos Llamas thread->pid, proc->pid);
3435777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
343644d8047fSTodd Kjos return_error_param = ret;
343757ada2fbSTodd Kjos return_error_line = __LINE__;
3438a056af42SMartijn Coenen goto err_translate_failed;
3439777783e0SGreg Kroah-Hartman }
3440777783e0SGreg Kroah-Hartman } break;
3441def95c73SMartijn Coenen case BINDER_TYPE_FDA: {
3442db6b0b81STodd Kjos struct binder_object ptr_object;
3443db6b0b81STodd Kjos binder_size_t parent_offset;
3444656e01f3STodd Kjos struct binder_object user_object;
3445656e01f3STodd Kjos size_t user_parent_size;
3446def95c73SMartijn Coenen struct binder_fd_array_object *fda =
3447def95c73SMartijn Coenen to_binder_fd_array_object(hdr);
344816981742STodd Kjos size_t num_valid = (buffer_offset - off_start_offset) /
3449bde4a19fSTodd Kjos sizeof(binder_size_t);
3450def95c73SMartijn Coenen struct binder_buffer_object *parent =
3451db6b0b81STodd Kjos binder_validate_ptr(target_proc, t->buffer,
3452db6b0b81STodd Kjos &ptr_object, fda->parent,
3453db6b0b81STodd Kjos off_start_offset,
3454db6b0b81STodd Kjos &parent_offset,
3455bde4a19fSTodd Kjos num_valid);
3456def95c73SMartijn Coenen if (!parent) {
3457def95c73SMartijn Coenen binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3458def95c73SMartijn Coenen proc->pid, thread->pid);
3459def95c73SMartijn Coenen return_error = BR_FAILED_REPLY;
346057ada2fbSTodd Kjos return_error_param = -EINVAL;
346157ada2fbSTodd Kjos return_error_line = __LINE__;
3462def95c73SMartijn Coenen goto err_bad_parent;
3463def95c73SMartijn Coenen }
3464db6b0b81STodd Kjos if (!binder_validate_fixup(target_proc, t->buffer,
3465db6b0b81STodd Kjos off_start_offset,
3466db6b0b81STodd Kjos parent_offset,
3467db6b0b81STodd Kjos fda->parent_offset,
3468db6b0b81STodd Kjos last_fixup_obj_off,
3469def95c73SMartijn Coenen last_fixup_min_off)) {
3470def95c73SMartijn Coenen binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3471def95c73SMartijn Coenen proc->pid, thread->pid);
3472def95c73SMartijn Coenen return_error = BR_FAILED_REPLY;
347357ada2fbSTodd Kjos return_error_param = -EINVAL;
347457ada2fbSTodd Kjos return_error_line = __LINE__;
3475def95c73SMartijn Coenen goto err_bad_parent;
3476def95c73SMartijn Coenen }
3477656e01f3STodd Kjos /*
3478656e01f3STodd Kjos * We need to read the user version of the parent
3479656e01f3STodd Kjos * object to get the original user offset
3480656e01f3STodd Kjos */
3481656e01f3STodd Kjos user_parent_size =
3482656e01f3STodd Kjos binder_get_object(proc, user_buffer, t->buffer,
3483656e01f3STodd Kjos parent_offset, &user_object);
3484656e01f3STodd Kjos if (user_parent_size != sizeof(user_object.bbo)) {
3485656e01f3STodd Kjos binder_user_error("%d:%d invalid ptr object size: %zd vs %zd\n",
3486656e01f3STodd Kjos proc->pid, thread->pid,
3487656e01f3STodd Kjos user_parent_size,
3488656e01f3STodd Kjos sizeof(user_object.bbo));
3489656e01f3STodd Kjos return_error = BR_FAILED_REPLY;
3490656e01f3STodd Kjos return_error_param = -EINVAL;
3491656e01f3STodd Kjos return_error_line = __LINE__;
3492656e01f3STodd Kjos goto err_bad_parent;
3493656e01f3STodd Kjos }
349409184ae9STodd Kjos ret = binder_translate_fd_array(&pf_head, fda,
349509184ae9STodd Kjos user_buffer, parent,
3496656e01f3STodd Kjos &user_object.bbo, t,
3497656e01f3STodd Kjos thread, in_reply_to);
34986d98eb95STodd Kjos if (!ret)
34996d98eb95STodd Kjos ret = binder_alloc_copy_to_buffer(&target_proc->alloc,
35006d98eb95STodd Kjos t->buffer,
35016d98eb95STodd Kjos object_offset,
35026d98eb95STodd Kjos fda, sizeof(*fda));
35036d98eb95STodd Kjos if (ret) {
3504a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate fd array failed\n",
3505a15dac8bSCarlos Llamas thread->pid, proc->pid);
3506def95c73SMartijn Coenen return_error = BR_FAILED_REPLY;
35076d98eb95STodd Kjos return_error_param = ret > 0 ? -EINVAL : ret;
350857ada2fbSTodd Kjos return_error_line = __LINE__;
3509def95c73SMartijn Coenen goto err_translate_failed;
3510def95c73SMartijn Coenen }
3511db6b0b81STodd Kjos last_fixup_obj_off = parent_offset;
3512def95c73SMartijn Coenen last_fixup_min_off =
3513def95c73SMartijn Coenen fda->parent_offset + sizeof(u32) * fda->num_fds;
3514def95c73SMartijn Coenen } break;
35157980240bSMartijn Coenen case BINDER_TYPE_PTR: {
35167980240bSMartijn Coenen struct binder_buffer_object *bp =
35177980240bSMartijn Coenen to_binder_buffer_object(hdr);
3518bde4a19fSTodd Kjos size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3519bde4a19fSTodd Kjos size_t num_valid;
3520777783e0SGreg Kroah-Hartman
35217980240bSMartijn Coenen if (bp->length > buf_left) {
35227980240bSMartijn Coenen binder_user_error("%d:%d got transaction with too large buffer\n",
35237980240bSMartijn Coenen proc->pid, thread->pid);
35247980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
352557ada2fbSTodd Kjos return_error_param = -EINVAL;
352657ada2fbSTodd Kjos return_error_line = __LINE__;
35277980240bSMartijn Coenen goto err_bad_offset;
35287980240bSMartijn Coenen }
352909184ae9STodd Kjos ret = binder_defer_copy(&sgc_head, sg_buf_offset,
353009184ae9STodd Kjos (const void __user *)(uintptr_t)bp->buffer,
353109184ae9STodd Kjos bp->length);
353209184ae9STodd Kjos if (ret) {
3533a15dac8bSCarlos Llamas binder_txn_error("%d:%d deferred copy failed\n",
3534a15dac8bSCarlos Llamas thread->pid, proc->pid);
35357980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
353609184ae9STodd Kjos return_error_param = ret;
353757ada2fbSTodd Kjos return_error_line = __LINE__;
353809184ae9STodd Kjos goto err_translate_failed;
35397980240bSMartijn Coenen }
35407980240bSMartijn Coenen /* Fixup buffer pointer to target proc address space */
3541bde4a19fSTodd Kjos bp->buffer = (uintptr_t)
3542bde4a19fSTodd Kjos t->buffer->user_data + sg_buf_offset;
3543bde4a19fSTodd Kjos sg_buf_offset += ALIGN(bp->length, sizeof(u64));
35447980240bSMartijn Coenen
354516981742STodd Kjos num_valid = (buffer_offset - off_start_offset) /
3546bde4a19fSTodd Kjos sizeof(binder_size_t);
354709184ae9STodd Kjos ret = binder_fixup_parent(&pf_head, t,
354809184ae9STodd Kjos thread, bp,
3549db6b0b81STodd Kjos off_start_offset,
3550bde4a19fSTodd Kjos num_valid,
3551db6b0b81STodd Kjos last_fixup_obj_off,
35527980240bSMartijn Coenen last_fixup_min_off);
3553bb4a2e48STodd Kjos if (ret < 0 ||
3554bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3555bb4a2e48STodd Kjos t->buffer,
3556bb4a2e48STodd Kjos object_offset,
3557bb4a2e48STodd Kjos bp, sizeof(*bp))) {
3558a15dac8bSCarlos Llamas binder_txn_error("%d:%d failed to fixup parent\n",
3559a15dac8bSCarlos Llamas thread->pid, proc->pid);
35607980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
356157ada2fbSTodd Kjos return_error_param = ret;
356257ada2fbSTodd Kjos return_error_line = __LINE__;
35637980240bSMartijn Coenen goto err_translate_failed;
35647980240bSMartijn Coenen }
3565db6b0b81STodd Kjos last_fixup_obj_off = object_offset;
35667980240bSMartijn Coenen last_fixup_min_off = 0;
35677980240bSMartijn Coenen } break;
3568777783e0SGreg Kroah-Hartman default:
3569777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3570feba3900SMartijn Coenen proc->pid, thread->pid, hdr->type);
3571777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
357257ada2fbSTodd Kjos return_error_param = -EINVAL;
357357ada2fbSTodd Kjos return_error_line = __LINE__;
3574777783e0SGreg Kroah-Hartman goto err_bad_object_type;
3575777783e0SGreg Kroah-Hartman }
3576777783e0SGreg Kroah-Hartman }
35776d98eb95STodd Kjos /* Done processing objects, copy the rest of the buffer */
35786d98eb95STodd Kjos if (binder_alloc_copy_user_to_buffer(
35796d98eb95STodd Kjos &target_proc->alloc,
35806d98eb95STodd Kjos t->buffer, user_offset,
35816d98eb95STodd Kjos user_buffer + user_offset,
35826d98eb95STodd Kjos tr->data_size - user_offset)) {
35836d98eb95STodd Kjos binder_user_error("%d:%d got transaction with invalid data ptr\n",
35846d98eb95STodd Kjos proc->pid, thread->pid);
35856d98eb95STodd Kjos return_error = BR_FAILED_REPLY;
35866d98eb95STodd Kjos return_error_param = -EFAULT;
35876d98eb95STodd Kjos return_error_line = __LINE__;
35886d98eb95STodd Kjos goto err_copy_data_failed;
35896d98eb95STodd Kjos }
359009184ae9STodd Kjos
359109184ae9STodd Kjos ret = binder_do_deferred_txn_copies(&target_proc->alloc, t->buffer,
359209184ae9STodd Kjos &sgc_head, &pf_head);
359309184ae9STodd Kjos if (ret) {
359409184ae9STodd Kjos binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
359509184ae9STodd Kjos proc->pid, thread->pid);
359609184ae9STodd Kjos return_error = BR_FAILED_REPLY;
359709184ae9STodd Kjos return_error_param = ret;
359809184ae9STodd Kjos return_error_line = __LINE__;
359909184ae9STodd Kjos goto err_copy_data_failed;
360009184ae9STodd Kjos }
3601a7dc1e6fSHang Lu if (t->buffer->oneway_spam_suspect)
3602a7dc1e6fSHang Lu tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT;
3603a7dc1e6fSHang Lu else
3604ccae6f67STodd Kjos tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3605673068eeSTodd Kjos t->work.type = BINDER_WORK_TRANSACTION;
3606ccae6f67STodd Kjos
3607777783e0SGreg Kroah-Hartman if (reply) {
3608148ade2cSMartijn Coenen binder_enqueue_thread_work(thread, tcomplete);
36090b89d69aSMartijn Coenen binder_inner_proc_lock(target_proc);
3610b564171aSLi Li if (target_thread->is_dead) {
3611b564171aSLi Li return_error = BR_DEAD_REPLY;
36120b89d69aSMartijn Coenen binder_inner_proc_unlock(target_proc);
36137a4408c6STodd Kjos goto err_dead_proc_or_thread;
36140b89d69aSMartijn Coenen }
3615777783e0SGreg Kroah-Hartman BUG_ON(t->buffer->async_transaction != 0);
36160b89d69aSMartijn Coenen binder_pop_transaction_ilocked(target_thread, in_reply_to);
3617148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3618432ff1e9SMarco Ballesio target_proc->outstanding_txns++;
36190b89d69aSMartijn Coenen binder_inner_proc_unlock(target_proc);
3620408c68b1SMartijn Coenen wake_up_interruptible_sync(&target_thread->wait);
3621b6d282ceSTodd Kjos binder_free_transaction(in_reply_to);
3622777783e0SGreg Kroah-Hartman } else if (!(t->flags & TF_ONE_WAY)) {
3623777783e0SGreg Kroah-Hartman BUG_ON(t->buffer->async_transaction != 0);
36240b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
3625148ade2cSMartijn Coenen /*
3626148ade2cSMartijn Coenen * Defer the TRANSACTION_COMPLETE, so we don't return to
3627148ade2cSMartijn Coenen * userspace immediately; this allows the target process to
3628148ade2cSMartijn Coenen * immediately start processing this transaction, reducing
3629148ade2cSMartijn Coenen * latency. We will then return the TRANSACTION_COMPLETE when
3630148ade2cSMartijn Coenen * the target replies (or there is an error).
3631148ade2cSMartijn Coenen */
3632148ade2cSMartijn Coenen binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3633777783e0SGreg Kroah-Hartman t->need_reply = 1;
3634777783e0SGreg Kroah-Hartman t->from_parent = thread->transaction_stack;
3635777783e0SGreg Kroah-Hartman thread->transaction_stack = t;
36360b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3637432ff1e9SMarco Ballesio return_error = binder_proc_transaction(t,
3638432ff1e9SMarco Ballesio target_proc, target_thread);
3639432ff1e9SMarco Ballesio if (return_error) {
36400b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
36410b89d69aSMartijn Coenen binder_pop_transaction_ilocked(thread, t);
36420b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
36437a4408c6STodd Kjos goto err_dead_proc_or_thread;
36447a4408c6STodd Kjos }
3645777783e0SGreg Kroah-Hartman } else {
3646777783e0SGreg Kroah-Hartman BUG_ON(target_node == NULL);
3647777783e0SGreg Kroah-Hartman BUG_ON(t->buffer->async_transaction != 1);
3648432ff1e9SMarco Ballesio return_error = binder_proc_transaction(t, target_proc, NULL);
36490567461aSLi Li /*
36500567461aSLi Li * Let the caller know when async transaction reaches a frozen
36510567461aSLi Li * process and is put in a pending queue, waiting for the target
36520567461aSLi Li * process to be unfrozen.
36530567461aSLi Li */
36540567461aSLi Li if (return_error == BR_TRANSACTION_PENDING_FROZEN)
36550567461aSLi Li tcomplete->type = BINDER_WORK_TRANSACTION_PENDING;
36560567461aSLi Li binder_enqueue_thread_work(thread, tcomplete);
36570567461aSLi Li if (return_error &&
36580567461aSLi Li return_error != BR_TRANSACTION_PENDING_FROZEN)
36597a4408c6STodd Kjos goto err_dead_proc_or_thread;
3660777783e0SGreg Kroah-Hartman }
36617a4408c6STodd Kjos if (target_thread)
36627a4408c6STodd Kjos binder_thread_dec_tmpref(target_thread);
36637a4408c6STodd Kjos binder_proc_dec_tmpref(target_proc);
3664512cf465STodd Kjos if (target_node)
3665512cf465STodd Kjos binder_dec_node_tmpref(target_node);
3666d99c7333STodd Kjos /*
3667d99c7333STodd Kjos * write barrier to synchronize with initialization
3668d99c7333STodd Kjos * of log entry
3669d99c7333STodd Kjos */
3670d99c7333STodd Kjos smp_wmb();
3671d99c7333STodd Kjos WRITE_ONCE(e->debug_id_done, t_debug_id);
3672777783e0SGreg Kroah-Hartman return;
3673777783e0SGreg Kroah-Hartman
36747a4408c6STodd Kjos err_dead_proc_or_thread:
3675a15dac8bSCarlos Llamas binder_txn_error("%d:%d dead process or thread\n",
3676a15dac8bSCarlos Llamas thread->pid, proc->pid);
36777a4408c6STodd Kjos return_error_line = __LINE__;
3678d53bebdfSXu YiPing binder_dequeue_work(proc, tcomplete);
3679a056af42SMartijn Coenen err_translate_failed:
3680777783e0SGreg Kroah-Hartman err_bad_object_type:
3681777783e0SGreg Kroah-Hartman err_bad_offset:
3682def95c73SMartijn Coenen err_bad_parent:
3683777783e0SGreg Kroah-Hartman err_copy_data_failed:
368409184ae9STodd Kjos binder_cleanup_deferred_txn_lists(&sgc_head, &pf_head);
368544d8047fSTodd Kjos binder_free_txn_fixups(t);
3686777783e0SGreg Kroah-Hartman trace_binder_transaction_failed_buffer_release(t->buffer);
36875fdb55c1STodd Kjos binder_transaction_buffer_release(target_proc, NULL, t->buffer,
3688bde4a19fSTodd Kjos buffer_offset, true);
3689512cf465STodd Kjos if (target_node)
3690512cf465STodd Kjos binder_dec_node_tmpref(target_node);
3691eb34983bSTodd Kjos target_node = NULL;
3692777783e0SGreg Kroah-Hartman t->buffer->transaction = NULL;
369319c98724STodd Kjos binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3694777783e0SGreg Kroah-Hartman err_binder_alloc_buf_failed:
36950b050950STodd Kjos err_bad_extra_size:
3696ec74136dSTodd Kjos if (secctx)
3697ec74136dSTodd Kjos security_release_secctx(secctx, secctx_sz);
3698ec74136dSTodd Kjos err_get_secctx_failed:
3699777783e0SGreg Kroah-Hartman kfree(tcomplete);
3700777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3701777783e0SGreg Kroah-Hartman err_alloc_tcomplete_failed:
37021987f112SFrankie.Chang if (trace_binder_txn_latency_free_enabled())
37031987f112SFrankie.Chang binder_txn_latency_free(t);
3704777783e0SGreg Kroah-Hartman kfree(t);
3705777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION);
3706777783e0SGreg Kroah-Hartman err_alloc_t_failed:
370744b73962SSherry Yang err_bad_todo_list:
3708777783e0SGreg Kroah-Hartman err_bad_call_stack:
3709777783e0SGreg Kroah-Hartman err_empty_call_stack:
3710777783e0SGreg Kroah-Hartman err_dead_binder:
3711777783e0SGreg Kroah-Hartman err_invalid_target_handle:
3712512cf465STodd Kjos if (target_node) {
3713eb34983bSTodd Kjos binder_dec_node(target_node, 1, 0);
3714512cf465STodd Kjos binder_dec_node_tmpref(target_node);
3715512cf465STodd Kjos }
3716eb34983bSTodd Kjos
3717777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
37189474be34SCarlos Llamas "%d:%d transaction %s to %d:%d failed %d/%d/%d, size %lld-%lld line %d\n",
37199474be34SCarlos Llamas proc->pid, thread->pid, reply ? "reply" :
37209474be34SCarlos Llamas (tr->flags & TF_ONE_WAY ? "async" : "call"),
37219474be34SCarlos Llamas target_proc ? target_proc->pid : 0,
37229474be34SCarlos Llamas target_thread ? target_thread->pid : 0,
37239474be34SCarlos Llamas t_debug_id, return_error, return_error_param,
372457ada2fbSTodd Kjos (u64)tr->data_size, (u64)tr->offsets_size,
372557ada2fbSTodd Kjos return_error_line);
3726777783e0SGreg Kroah-Hartman
3727dafa5e9aSCarlos Llamas if (target_thread)
3728dafa5e9aSCarlos Llamas binder_thread_dec_tmpref(target_thread);
3729dafa5e9aSCarlos Llamas if (target_proc)
3730dafa5e9aSCarlos Llamas binder_proc_dec_tmpref(target_proc);
3731dafa5e9aSCarlos Llamas
3732777783e0SGreg Kroah-Hartman {
3733777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *fe;
3734777783e0SGreg Kroah-Hartman
373557ada2fbSTodd Kjos e->return_error = return_error;
373657ada2fbSTodd Kjos e->return_error_param = return_error_param;
373757ada2fbSTodd Kjos e->return_error_line = return_error_line;
3738777783e0SGreg Kroah-Hartman fe = binder_transaction_log_add(&binder_transaction_log_failed);
3739777783e0SGreg Kroah-Hartman *fe = *e;
3740d99c7333STodd Kjos /*
3741d99c7333STodd Kjos * write barrier to synchronize with initialization
3742d99c7333STodd Kjos * of log entry
3743d99c7333STodd Kjos */
3744d99c7333STodd Kjos smp_wmb();
3745d99c7333STodd Kjos WRITE_ONCE(e->debug_id_done, t_debug_id);
3746d99c7333STodd Kjos WRITE_ONCE(fe->debug_id_done, t_debug_id);
3747777783e0SGreg Kroah-Hartman }
3748777783e0SGreg Kroah-Hartman
374926549d17STodd Kjos BUG_ON(thread->return_error.cmd != BR_OK);
3750777783e0SGreg Kroah-Hartman if (in_reply_to) {
3751bd32889eSCarlos Llamas binder_set_txn_from_error(in_reply_to, t_debug_id,
3752bd32889eSCarlos Llamas return_error, return_error_param);
375326549d17STodd Kjos thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3754148ade2cSMartijn Coenen binder_enqueue_thread_work(thread, &thread->return_error.work);
3755777783e0SGreg Kroah-Hartman binder_send_failed_reply(in_reply_to, return_error);
375626549d17STodd Kjos } else {
3757bd32889eSCarlos Llamas binder_inner_proc_lock(proc);
3758bd32889eSCarlos Llamas binder_set_extended_error(&thread->ee, t_debug_id,
3759bd32889eSCarlos Llamas return_error, return_error_param);
3760bd32889eSCarlos Llamas binder_inner_proc_unlock(proc);
376126549d17STodd Kjos thread->return_error.cmd = return_error;
3762148ade2cSMartijn Coenen binder_enqueue_thread_work(thread, &thread->return_error.work);
376326549d17STodd Kjos }
3764777783e0SGreg Kroah-Hartman }
3765777783e0SGreg Kroah-Hartman
376644d8047fSTodd Kjos /**
376744d8047fSTodd Kjos * binder_free_buf() - free the specified buffer
376844d8047fSTodd Kjos * @proc: binder proc that owns buffer
376944d8047fSTodd Kjos * @buffer: buffer to be freed
377032e9f56aSTodd Kjos * @is_failure: failed to send transaction
377144d8047fSTodd Kjos *
377244d8047fSTodd Kjos * If buffer for an async transaction, enqueue the next async
377344d8047fSTodd Kjos * transaction from the node.
377444d8047fSTodd Kjos *
377544d8047fSTodd Kjos * Cleanup buffer and free it.
377644d8047fSTodd Kjos */
3777f4608ce9SWei Yongjun static void
binder_free_buf(struct binder_proc * proc,struct binder_thread * thread,struct binder_buffer * buffer,bool is_failure)37785fdb55c1STodd Kjos binder_free_buf(struct binder_proc *proc,
37795fdb55c1STodd Kjos struct binder_thread *thread,
378032e9f56aSTodd Kjos struct binder_buffer *buffer, bool is_failure)
378144d8047fSTodd Kjos {
3782a370003cSTodd Kjos binder_inner_proc_lock(proc);
378344d8047fSTodd Kjos if (buffer->transaction) {
378444d8047fSTodd Kjos buffer->transaction->buffer = NULL;
378544d8047fSTodd Kjos buffer->transaction = NULL;
378644d8047fSTodd Kjos }
3787a370003cSTodd Kjos binder_inner_proc_unlock(proc);
378844d8047fSTodd Kjos if (buffer->async_transaction && buffer->target_node) {
378944d8047fSTodd Kjos struct binder_node *buf_node;
379044d8047fSTodd Kjos struct binder_work *w;
379144d8047fSTodd Kjos
379244d8047fSTodd Kjos buf_node = buffer->target_node;
379344d8047fSTodd Kjos binder_node_inner_lock(buf_node);
379444d8047fSTodd Kjos BUG_ON(!buf_node->has_async_transaction);
379544d8047fSTodd Kjos BUG_ON(buf_node->proc != proc);
379644d8047fSTodd Kjos w = binder_dequeue_work_head_ilocked(
379744d8047fSTodd Kjos &buf_node->async_todo);
379844d8047fSTodd Kjos if (!w) {
379944d8047fSTodd Kjos buf_node->has_async_transaction = false;
380044d8047fSTodd Kjos } else {
380144d8047fSTodd Kjos binder_enqueue_work_ilocked(
380244d8047fSTodd Kjos w, &proc->todo);
380344d8047fSTodd Kjos binder_wakeup_proc_ilocked(proc);
380444d8047fSTodd Kjos }
380544d8047fSTodd Kjos binder_node_inner_unlock(buf_node);
380644d8047fSTodd Kjos }
380744d8047fSTodd Kjos trace_binder_transaction_buffer_release(buffer);
3808bdc1c5faSCarlos Llamas binder_release_entire_buffer(proc, thread, buffer, is_failure);
380944d8047fSTodd Kjos binder_alloc_free_buf(&proc->alloc, buffer);
381044d8047fSTodd Kjos }
381144d8047fSTodd Kjos
binder_thread_write(struct binder_proc * proc,struct binder_thread * thread,binder_uintptr_t binder_buffer,size_t size,binder_size_t * consumed)3812777783e0SGreg Kroah-Hartman static int binder_thread_write(struct binder_proc *proc,
3813777783e0SGreg Kroah-Hartman struct binder_thread *thread,
3814777783e0SGreg Kroah-Hartman binder_uintptr_t binder_buffer, size_t size,
3815777783e0SGreg Kroah-Hartman binder_size_t *consumed)
3816777783e0SGreg Kroah-Hartman {
3817777783e0SGreg Kroah-Hartman uint32_t cmd;
3818342e5c90SMartijn Coenen struct binder_context *context = proc->context;
3819777783e0SGreg Kroah-Hartman void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
3820777783e0SGreg Kroah-Hartman void __user *ptr = buffer + *consumed;
3821777783e0SGreg Kroah-Hartman void __user *end = buffer + size;
3822777783e0SGreg Kroah-Hartman
382326549d17STodd Kjos while (ptr < end && thread->return_error.cmd == BR_OK) {
3824372e3147STodd Kjos int ret;
3825372e3147STodd Kjos
3826777783e0SGreg Kroah-Hartman if (get_user(cmd, (uint32_t __user *)ptr))
3827777783e0SGreg Kroah-Hartman return -EFAULT;
3828777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
3829777783e0SGreg Kroah-Hartman trace_binder_command(cmd);
3830777783e0SGreg Kroah-Hartman if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
38310953c797SBadhri Jagan Sridharan atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
38320953c797SBadhri Jagan Sridharan atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
38330953c797SBadhri Jagan Sridharan atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
3834777783e0SGreg Kroah-Hartman }
3835777783e0SGreg Kroah-Hartman switch (cmd) {
3836777783e0SGreg Kroah-Hartman case BC_INCREFS:
3837777783e0SGreg Kroah-Hartman case BC_ACQUIRE:
3838777783e0SGreg Kroah-Hartman case BC_RELEASE:
3839777783e0SGreg Kroah-Hartman case BC_DECREFS: {
3840777783e0SGreg Kroah-Hartman uint32_t target;
3841777783e0SGreg Kroah-Hartman const char *debug_string;
3842372e3147STodd Kjos bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3843372e3147STodd Kjos bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3844372e3147STodd Kjos struct binder_ref_data rdata;
3845777783e0SGreg Kroah-Hartman
3846777783e0SGreg Kroah-Hartman if (get_user(target, (uint32_t __user *)ptr))
3847777783e0SGreg Kroah-Hartman return -EFAULT;
3848c44b1231STodd Kjos
3849777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
3850372e3147STodd Kjos ret = -1;
3851372e3147STodd Kjos if (increment && !target) {
3852c44b1231STodd Kjos struct binder_node *ctx_mgr_node;
38536c20032cSAndrew Bridges
3854c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
3855c44b1231STodd Kjos ctx_mgr_node = context->binder_context_mgr_node;
38564b836a14SJann Horn if (ctx_mgr_node) {
38574b836a14SJann Horn if (ctx_mgr_node->proc == proc) {
38584b836a14SJann Horn binder_user_error("%d:%d context manager tried to acquire desc 0\n",
38594b836a14SJann Horn proc->pid, thread->pid);
38604b836a14SJann Horn mutex_unlock(&context->context_mgr_node_lock);
38614b836a14SJann Horn return -EINVAL;
38624b836a14SJann Horn }
3863372e3147STodd Kjos ret = binder_inc_ref_for_node(
3864372e3147STodd Kjos proc, ctx_mgr_node,
3865372e3147STodd Kjos strong, NULL, &rdata);
38664b836a14SJann Horn }
3867c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
3868c44b1231STodd Kjos }
3869372e3147STodd Kjos if (ret)
3870372e3147STodd Kjos ret = binder_update_ref_for_handle(
3871372e3147STodd Kjos proc, target, increment, strong,
3872372e3147STodd Kjos &rdata);
3873372e3147STodd Kjos if (!ret && rdata.desc != target) {
3874372e3147STodd Kjos binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3875372e3147STodd Kjos proc->pid, thread->pid,
3876372e3147STodd Kjos target, rdata.desc);
3877777783e0SGreg Kroah-Hartman }
3878777783e0SGreg Kroah-Hartman switch (cmd) {
3879777783e0SGreg Kroah-Hartman case BC_INCREFS:
3880777783e0SGreg Kroah-Hartman debug_string = "IncRefs";
3881777783e0SGreg Kroah-Hartman break;
3882777783e0SGreg Kroah-Hartman case BC_ACQUIRE:
3883777783e0SGreg Kroah-Hartman debug_string = "Acquire";
3884777783e0SGreg Kroah-Hartman break;
3885777783e0SGreg Kroah-Hartman case BC_RELEASE:
3886777783e0SGreg Kroah-Hartman debug_string = "Release";
3887777783e0SGreg Kroah-Hartman break;
3888777783e0SGreg Kroah-Hartman case BC_DECREFS:
3889777783e0SGreg Kroah-Hartman default:
3890777783e0SGreg Kroah-Hartman debug_string = "DecRefs";
3891372e3147STodd Kjos break;
3892372e3147STodd Kjos }
3893372e3147STodd Kjos if (ret) {
3894372e3147STodd Kjos binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3895372e3147STodd Kjos proc->pid, thread->pid, debug_string,
3896372e3147STodd Kjos strong, target, ret);
3897777783e0SGreg Kroah-Hartman break;
3898777783e0SGreg Kroah-Hartman }
3899777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_USER_REFS,
3900372e3147STodd Kjos "%d:%d %s ref %d desc %d s %d w %d\n",
3901372e3147STodd Kjos proc->pid, thread->pid, debug_string,
3902372e3147STodd Kjos rdata.debug_id, rdata.desc, rdata.strong,
3903372e3147STodd Kjos rdata.weak);
3904777783e0SGreg Kroah-Hartman break;
3905777783e0SGreg Kroah-Hartman }
3906777783e0SGreg Kroah-Hartman case BC_INCREFS_DONE:
3907777783e0SGreg Kroah-Hartman case BC_ACQUIRE_DONE: {
3908777783e0SGreg Kroah-Hartman binder_uintptr_t node_ptr;
3909777783e0SGreg Kroah-Hartman binder_uintptr_t cookie;
3910777783e0SGreg Kroah-Hartman struct binder_node *node;
3911673068eeSTodd Kjos bool free_node;
3912777783e0SGreg Kroah-Hartman
3913777783e0SGreg Kroah-Hartman if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
3914777783e0SGreg Kroah-Hartman return -EFAULT;
3915777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
3916777783e0SGreg Kroah-Hartman if (get_user(cookie, (binder_uintptr_t __user *)ptr))
3917777783e0SGreg Kroah-Hartman return -EFAULT;
3918777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
3919777783e0SGreg Kroah-Hartman node = binder_get_node(proc, node_ptr);
3920777783e0SGreg Kroah-Hartman if (node == NULL) {
3921777783e0SGreg Kroah-Hartman binder_user_error("%d:%d %s u%016llx no match\n",
3922777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3923777783e0SGreg Kroah-Hartman cmd == BC_INCREFS_DONE ?
3924777783e0SGreg Kroah-Hartman "BC_INCREFS_DONE" :
3925777783e0SGreg Kroah-Hartman "BC_ACQUIRE_DONE",
3926777783e0SGreg Kroah-Hartman (u64)node_ptr);
3927777783e0SGreg Kroah-Hartman break;
3928777783e0SGreg Kroah-Hartman }
3929777783e0SGreg Kroah-Hartman if (cookie != node->cookie) {
3930777783e0SGreg Kroah-Hartman binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
3931777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3932777783e0SGreg Kroah-Hartman cmd == BC_INCREFS_DONE ?
3933777783e0SGreg Kroah-Hartman "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3934777783e0SGreg Kroah-Hartman (u64)node_ptr, node->debug_id,
3935777783e0SGreg Kroah-Hartman (u64)cookie, (u64)node->cookie);
3936adc18842STodd Kjos binder_put_node(node);
3937777783e0SGreg Kroah-Hartman break;
3938777783e0SGreg Kroah-Hartman }
3939673068eeSTodd Kjos binder_node_inner_lock(node);
3940777783e0SGreg Kroah-Hartman if (cmd == BC_ACQUIRE_DONE) {
3941777783e0SGreg Kroah-Hartman if (node->pending_strong_ref == 0) {
3942777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
3943777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3944777783e0SGreg Kroah-Hartman node->debug_id);
3945673068eeSTodd Kjos binder_node_inner_unlock(node);
3946adc18842STodd Kjos binder_put_node(node);
3947777783e0SGreg Kroah-Hartman break;
3948777783e0SGreg Kroah-Hartman }
3949777783e0SGreg Kroah-Hartman node->pending_strong_ref = 0;
3950777783e0SGreg Kroah-Hartman } else {
3951777783e0SGreg Kroah-Hartman if (node->pending_weak_ref == 0) {
3952777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
3953777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3954777783e0SGreg Kroah-Hartman node->debug_id);
3955673068eeSTodd Kjos binder_node_inner_unlock(node);
3956adc18842STodd Kjos binder_put_node(node);
3957777783e0SGreg Kroah-Hartman break;
3958777783e0SGreg Kroah-Hartman }
3959777783e0SGreg Kroah-Hartman node->pending_weak_ref = 0;
3960777783e0SGreg Kroah-Hartman }
3961673068eeSTodd Kjos free_node = binder_dec_node_nilocked(node,
3962673068eeSTodd Kjos cmd == BC_ACQUIRE_DONE, 0);
3963673068eeSTodd Kjos WARN_ON(free_node);
3964777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_USER_REFS,
3965adc18842STodd Kjos "%d:%d %s node %d ls %d lw %d tr %d\n",
3966777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3967777783e0SGreg Kroah-Hartman cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
3968adc18842STodd Kjos node->debug_id, node->local_strong_refs,
3969adc18842STodd Kjos node->local_weak_refs, node->tmp_refs);
3970673068eeSTodd Kjos binder_node_inner_unlock(node);
3971adc18842STodd Kjos binder_put_node(node);
3972777783e0SGreg Kroah-Hartman break;
3973777783e0SGreg Kroah-Hartman }
3974777783e0SGreg Kroah-Hartman case BC_ATTEMPT_ACQUIRE:
3975777783e0SGreg Kroah-Hartman pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
3976777783e0SGreg Kroah-Hartman return -EINVAL;
3977777783e0SGreg Kroah-Hartman case BC_ACQUIRE_RESULT:
3978777783e0SGreg Kroah-Hartman pr_err("BC_ACQUIRE_RESULT not supported\n");
3979777783e0SGreg Kroah-Hartman return -EINVAL;
3980777783e0SGreg Kroah-Hartman
3981777783e0SGreg Kroah-Hartman case BC_FREE_BUFFER: {
3982777783e0SGreg Kroah-Hartman binder_uintptr_t data_ptr;
3983777783e0SGreg Kroah-Hartman struct binder_buffer *buffer;
3984777783e0SGreg Kroah-Hartman
3985777783e0SGreg Kroah-Hartman if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
3986777783e0SGreg Kroah-Hartman return -EFAULT;
3987777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
3988777783e0SGreg Kroah-Hartman
398953d311cfSTodd Kjos buffer = binder_alloc_prepare_to_free(&proc->alloc,
399019c98724STodd Kjos data_ptr);
39917bada55aSTodd Kjos if (IS_ERR_OR_NULL(buffer)) {
39927bada55aSTodd Kjos if (PTR_ERR(buffer) == -EPERM) {
39937bada55aSTodd Kjos binder_user_error(
39947bada55aSTodd Kjos "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
39957bada55aSTodd Kjos proc->pid, thread->pid,
39967bada55aSTodd Kjos (u64)data_ptr);
39977bada55aSTodd Kjos } else {
39987bada55aSTodd Kjos binder_user_error(
39997bada55aSTodd Kjos "%d:%d BC_FREE_BUFFER u%016llx no match\n",
40007bada55aSTodd Kjos proc->pid, thread->pid,
40017bada55aSTodd Kjos (u64)data_ptr);
4002777783e0SGreg Kroah-Hartman }
4003777783e0SGreg Kroah-Hartman break;
4004777783e0SGreg Kroah-Hartman }
4005777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FREE_BUFFER,
4006777783e0SGreg Kroah-Hartman "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
4007777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)data_ptr,
4008777783e0SGreg Kroah-Hartman buffer->debug_id,
4009777783e0SGreg Kroah-Hartman buffer->transaction ? "active" : "finished");
401032e9f56aSTodd Kjos binder_free_buf(proc, thread, buffer, false);
4011777783e0SGreg Kroah-Hartman break;
4012777783e0SGreg Kroah-Hartman }
4013777783e0SGreg Kroah-Hartman
40147980240bSMartijn Coenen case BC_TRANSACTION_SG:
40157980240bSMartijn Coenen case BC_REPLY_SG: {
40167980240bSMartijn Coenen struct binder_transaction_data_sg tr;
40177980240bSMartijn Coenen
40187980240bSMartijn Coenen if (copy_from_user(&tr, ptr, sizeof(tr)))
40197980240bSMartijn Coenen return -EFAULT;
40207980240bSMartijn Coenen ptr += sizeof(tr);
40217980240bSMartijn Coenen binder_transaction(proc, thread, &tr.transaction_data,
40227980240bSMartijn Coenen cmd == BC_REPLY_SG, tr.buffers_size);
40237980240bSMartijn Coenen break;
40247980240bSMartijn Coenen }
4025777783e0SGreg Kroah-Hartman case BC_TRANSACTION:
4026777783e0SGreg Kroah-Hartman case BC_REPLY: {
4027777783e0SGreg Kroah-Hartman struct binder_transaction_data tr;
4028777783e0SGreg Kroah-Hartman
4029777783e0SGreg Kroah-Hartman if (copy_from_user(&tr, ptr, sizeof(tr)))
4030777783e0SGreg Kroah-Hartman return -EFAULT;
4031777783e0SGreg Kroah-Hartman ptr += sizeof(tr);
40324bfac80aSMartijn Coenen binder_transaction(proc, thread, &tr,
40334bfac80aSMartijn Coenen cmd == BC_REPLY, 0);
4034777783e0SGreg Kroah-Hartman break;
4035777783e0SGreg Kroah-Hartman }
4036777783e0SGreg Kroah-Hartman
4037777783e0SGreg Kroah-Hartman case BC_REGISTER_LOOPER:
4038777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4039777783e0SGreg Kroah-Hartman "%d:%d BC_REGISTER_LOOPER\n",
4040777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4041b3e68612STodd Kjos binder_inner_proc_lock(proc);
4042777783e0SGreg Kroah-Hartman if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
4043777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_INVALID;
4044777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
4045777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4046777783e0SGreg Kroah-Hartman } else if (proc->requested_threads == 0) {
4047777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_INVALID;
4048777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
4049777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4050777783e0SGreg Kroah-Hartman } else {
4051777783e0SGreg Kroah-Hartman proc->requested_threads--;
4052777783e0SGreg Kroah-Hartman proc->requested_threads_started++;
4053777783e0SGreg Kroah-Hartman }
4054777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
4055b3e68612STodd Kjos binder_inner_proc_unlock(proc);
4056777783e0SGreg Kroah-Hartman break;
4057777783e0SGreg Kroah-Hartman case BC_ENTER_LOOPER:
4058777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4059777783e0SGreg Kroah-Hartman "%d:%d BC_ENTER_LOOPER\n",
4060777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4061777783e0SGreg Kroah-Hartman if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
4062777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_INVALID;
4063777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
4064777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4065777783e0SGreg Kroah-Hartman }
4066777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_ENTERED;
4067777783e0SGreg Kroah-Hartman break;
4068777783e0SGreg Kroah-Hartman case BC_EXIT_LOOPER:
4069777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4070777783e0SGreg Kroah-Hartman "%d:%d BC_EXIT_LOOPER\n",
4071777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4072777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_EXITED;
4073777783e0SGreg Kroah-Hartman break;
4074777783e0SGreg Kroah-Hartman
4075777783e0SGreg Kroah-Hartman case BC_REQUEST_DEATH_NOTIFICATION:
4076777783e0SGreg Kroah-Hartman case BC_CLEAR_DEATH_NOTIFICATION: {
4077777783e0SGreg Kroah-Hartman uint32_t target;
4078777783e0SGreg Kroah-Hartman binder_uintptr_t cookie;
4079777783e0SGreg Kroah-Hartman struct binder_ref *ref;
40802c1838dcSTodd Kjos struct binder_ref_death *death = NULL;
4081777783e0SGreg Kroah-Hartman
4082777783e0SGreg Kroah-Hartman if (get_user(target, (uint32_t __user *)ptr))
4083777783e0SGreg Kroah-Hartman return -EFAULT;
4084777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4085777783e0SGreg Kroah-Hartman if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4086777783e0SGreg Kroah-Hartman return -EFAULT;
4087777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
40882c1838dcSTodd Kjos if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
40892c1838dcSTodd Kjos /*
40902c1838dcSTodd Kjos * Allocate memory for death notification
40912c1838dcSTodd Kjos * before taking lock
40922c1838dcSTodd Kjos */
40932c1838dcSTodd Kjos death = kzalloc(sizeof(*death), GFP_KERNEL);
40942c1838dcSTodd Kjos if (death == NULL) {
40952c1838dcSTodd Kjos WARN_ON(thread->return_error.cmd !=
40962c1838dcSTodd Kjos BR_OK);
40972c1838dcSTodd Kjos thread->return_error.cmd = BR_ERROR;
4098148ade2cSMartijn Coenen binder_enqueue_thread_work(
4099148ade2cSMartijn Coenen thread,
4100148ade2cSMartijn Coenen &thread->return_error.work);
41012c1838dcSTodd Kjos binder_debug(
41022c1838dcSTodd Kjos BINDER_DEBUG_FAILED_TRANSACTION,
41032c1838dcSTodd Kjos "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
41042c1838dcSTodd Kjos proc->pid, thread->pid);
41052c1838dcSTodd Kjos break;
41062c1838dcSTodd Kjos }
41072c1838dcSTodd Kjos }
41082c1838dcSTodd Kjos binder_proc_lock(proc);
41092c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, target, false);
4110777783e0SGreg Kroah-Hartman if (ref == NULL) {
4111777783e0SGreg Kroah-Hartman binder_user_error("%d:%d %s invalid ref %d\n",
4112777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4113777783e0SGreg Kroah-Hartman cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4114777783e0SGreg Kroah-Hartman "BC_REQUEST_DEATH_NOTIFICATION" :
4115777783e0SGreg Kroah-Hartman "BC_CLEAR_DEATH_NOTIFICATION",
4116777783e0SGreg Kroah-Hartman target);
41172c1838dcSTodd Kjos binder_proc_unlock(proc);
41182c1838dcSTodd Kjos kfree(death);
4119777783e0SGreg Kroah-Hartman break;
4120777783e0SGreg Kroah-Hartman }
4121777783e0SGreg Kroah-Hartman
4122777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4123777783e0SGreg Kroah-Hartman "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
4124777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4125777783e0SGreg Kroah-Hartman cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4126777783e0SGreg Kroah-Hartman "BC_REQUEST_DEATH_NOTIFICATION" :
4127777783e0SGreg Kroah-Hartman "BC_CLEAR_DEATH_NOTIFICATION",
4128372e3147STodd Kjos (u64)cookie, ref->data.debug_id,
4129372e3147STodd Kjos ref->data.desc, ref->data.strong,
4130372e3147STodd Kjos ref->data.weak, ref->node->debug_id);
4131777783e0SGreg Kroah-Hartman
4132ab51ec6bSMartijn Coenen binder_node_lock(ref->node);
4133777783e0SGreg Kroah-Hartman if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
4134777783e0SGreg Kroah-Hartman if (ref->death) {
4135777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
4136777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4137ab51ec6bSMartijn Coenen binder_node_unlock(ref->node);
41382c1838dcSTodd Kjos binder_proc_unlock(proc);
41392c1838dcSTodd Kjos kfree(death);
4140777783e0SGreg Kroah-Hartman break;
4141777783e0SGreg Kroah-Hartman }
4142777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_DEATH);
4143777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&death->work.entry);
4144777783e0SGreg Kroah-Hartman death->cookie = cookie;
4145777783e0SGreg Kroah-Hartman ref->death = death;
4146777783e0SGreg Kroah-Hartman if (ref->node->proc == NULL) {
4147777783e0SGreg Kroah-Hartman ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4148bb74562aSMartijn Coenen
41491b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
41501b77e9dcSMartijn Coenen binder_enqueue_work_ilocked(
4151bb74562aSMartijn Coenen &ref->death->work, &proc->todo);
4152bb74562aSMartijn Coenen binder_wakeup_proc_ilocked(proc);
41531b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
4154777783e0SGreg Kroah-Hartman }
4155777783e0SGreg Kroah-Hartman } else {
4156777783e0SGreg Kroah-Hartman if (ref->death == NULL) {
4157777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
4158777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4159673068eeSTodd Kjos binder_node_unlock(ref->node);
41602c1838dcSTodd Kjos binder_proc_unlock(proc);
4161777783e0SGreg Kroah-Hartman break;
4162777783e0SGreg Kroah-Hartman }
4163777783e0SGreg Kroah-Hartman death = ref->death;
4164777783e0SGreg Kroah-Hartman if (death->cookie != cookie) {
4165777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
4166777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4167777783e0SGreg Kroah-Hartman (u64)death->cookie,
4168777783e0SGreg Kroah-Hartman (u64)cookie);
4169673068eeSTodd Kjos binder_node_unlock(ref->node);
41702c1838dcSTodd Kjos binder_proc_unlock(proc);
4171777783e0SGreg Kroah-Hartman break;
4172777783e0SGreg Kroah-Hartman }
4173777783e0SGreg Kroah-Hartman ref->death = NULL;
417472196393STodd Kjos binder_inner_proc_lock(proc);
4175777783e0SGreg Kroah-Hartman if (list_empty(&death->work.entry)) {
4176777783e0SGreg Kroah-Hartman death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
417772196393STodd Kjos if (thread->looper &
417872196393STodd Kjos (BINDER_LOOPER_STATE_REGISTERED |
417972196393STodd Kjos BINDER_LOOPER_STATE_ENTERED))
4180148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(
4181148ade2cSMartijn Coenen thread,
4182148ade2cSMartijn Coenen &death->work);
418372196393STodd Kjos else {
418472196393STodd Kjos binder_enqueue_work_ilocked(
418572196393STodd Kjos &death->work,
418672196393STodd Kjos &proc->todo);
41871b77e9dcSMartijn Coenen binder_wakeup_proc_ilocked(
4188408c68b1SMartijn Coenen proc);
4189777783e0SGreg Kroah-Hartman }
4190777783e0SGreg Kroah-Hartman } else {
4191777783e0SGreg Kroah-Hartman BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4192777783e0SGreg Kroah-Hartman death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4193777783e0SGreg Kroah-Hartman }
419472196393STodd Kjos binder_inner_proc_unlock(proc);
4195777783e0SGreg Kroah-Hartman }
4196ab51ec6bSMartijn Coenen binder_node_unlock(ref->node);
41972c1838dcSTodd Kjos binder_proc_unlock(proc);
4198777783e0SGreg Kroah-Hartman } break;
4199777783e0SGreg Kroah-Hartman case BC_DEAD_BINDER_DONE: {
4200777783e0SGreg Kroah-Hartman struct binder_work *w;
4201777783e0SGreg Kroah-Hartman binder_uintptr_t cookie;
4202777783e0SGreg Kroah-Hartman struct binder_ref_death *death = NULL;
4203777783e0SGreg Kroah-Hartman
4204777783e0SGreg Kroah-Hartman if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4205777783e0SGreg Kroah-Hartman return -EFAULT;
4206777783e0SGreg Kroah-Hartman
42077a64cd88SLisa Du ptr += sizeof(cookie);
420872196393STodd Kjos binder_inner_proc_lock(proc);
420972196393STodd Kjos list_for_each_entry(w, &proc->delivered_death,
421072196393STodd Kjos entry) {
421172196393STodd Kjos struct binder_ref_death *tmp_death =
421272196393STodd Kjos container_of(w,
421372196393STodd Kjos struct binder_ref_death,
421472196393STodd Kjos work);
4215777783e0SGreg Kroah-Hartman
4216777783e0SGreg Kroah-Hartman if (tmp_death->cookie == cookie) {
4217777783e0SGreg Kroah-Hartman death = tmp_death;
4218777783e0SGreg Kroah-Hartman break;
4219777783e0SGreg Kroah-Hartman }
4220777783e0SGreg Kroah-Hartman }
4221777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
42228ca86f16STodd Kjos "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
4223777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)cookie,
4224777783e0SGreg Kroah-Hartman death);
4225777783e0SGreg Kroah-Hartman if (death == NULL) {
4226777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4227777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)cookie);
422872196393STodd Kjos binder_inner_proc_unlock(proc);
4229777783e0SGreg Kroah-Hartman break;
4230777783e0SGreg Kroah-Hartman }
423172196393STodd Kjos binder_dequeue_work_ilocked(&death->work);
4232777783e0SGreg Kroah-Hartman if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4233777783e0SGreg Kroah-Hartman death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
423472196393STodd Kjos if (thread->looper &
423572196393STodd Kjos (BINDER_LOOPER_STATE_REGISTERED |
423672196393STodd Kjos BINDER_LOOPER_STATE_ENTERED))
4237148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(
4238148ade2cSMartijn Coenen thread, &death->work);
423972196393STodd Kjos else {
424072196393STodd Kjos binder_enqueue_work_ilocked(
424172196393STodd Kjos &death->work,
424272196393STodd Kjos &proc->todo);
4243408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(proc);
4244777783e0SGreg Kroah-Hartman }
4245777783e0SGreg Kroah-Hartman }
424672196393STodd Kjos binder_inner_proc_unlock(proc);
4247777783e0SGreg Kroah-Hartman } break;
4248777783e0SGreg Kroah-Hartman
4249777783e0SGreg Kroah-Hartman default:
4250da486496SCarlos Llamas pr_err("%d:%d unknown command %u\n",
4251777783e0SGreg Kroah-Hartman proc->pid, thread->pid, cmd);
4252777783e0SGreg Kroah-Hartman return -EINVAL;
4253777783e0SGreg Kroah-Hartman }
4254777783e0SGreg Kroah-Hartman *consumed = ptr - buffer;
4255777783e0SGreg Kroah-Hartman }
4256777783e0SGreg Kroah-Hartman return 0;
4257777783e0SGreg Kroah-Hartman }
4258777783e0SGreg Kroah-Hartman
binder_stat_br(struct binder_proc * proc,struct binder_thread * thread,uint32_t cmd)4259777783e0SGreg Kroah-Hartman static void binder_stat_br(struct binder_proc *proc,
4260777783e0SGreg Kroah-Hartman struct binder_thread *thread, uint32_t cmd)
4261777783e0SGreg Kroah-Hartman {
4262777783e0SGreg Kroah-Hartman trace_binder_return(cmd);
4263777783e0SGreg Kroah-Hartman if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
42640953c797SBadhri Jagan Sridharan atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
42650953c797SBadhri Jagan Sridharan atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
42660953c797SBadhri Jagan Sridharan atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
4267777783e0SGreg Kroah-Hartman }
4268777783e0SGreg Kroah-Hartman }
4269777783e0SGreg Kroah-Hartman
binder_put_node_cmd(struct binder_proc * proc,struct binder_thread * thread,void __user ** ptrp,binder_uintptr_t node_ptr,binder_uintptr_t node_cookie,int node_debug_id,uint32_t cmd,const char * cmd_name)427026b47d8aSTodd Kjos static int binder_put_node_cmd(struct binder_proc *proc,
427126b47d8aSTodd Kjos struct binder_thread *thread,
427226b47d8aSTodd Kjos void __user **ptrp,
427326b47d8aSTodd Kjos binder_uintptr_t node_ptr,
427426b47d8aSTodd Kjos binder_uintptr_t node_cookie,
427526b47d8aSTodd Kjos int node_debug_id,
427626b47d8aSTodd Kjos uint32_t cmd, const char *cmd_name)
427726b47d8aSTodd Kjos {
427826b47d8aSTodd Kjos void __user *ptr = *ptrp;
427926b47d8aSTodd Kjos
428026b47d8aSTodd Kjos if (put_user(cmd, (uint32_t __user *)ptr))
428126b47d8aSTodd Kjos return -EFAULT;
428226b47d8aSTodd Kjos ptr += sizeof(uint32_t);
428326b47d8aSTodd Kjos
428426b47d8aSTodd Kjos if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
428526b47d8aSTodd Kjos return -EFAULT;
428626b47d8aSTodd Kjos ptr += sizeof(binder_uintptr_t);
428726b47d8aSTodd Kjos
428826b47d8aSTodd Kjos if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
428926b47d8aSTodd Kjos return -EFAULT;
429026b47d8aSTodd Kjos ptr += sizeof(binder_uintptr_t);
429126b47d8aSTodd Kjos
429226b47d8aSTodd Kjos binder_stat_br(proc, thread, cmd);
429326b47d8aSTodd Kjos binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
429426b47d8aSTodd Kjos proc->pid, thread->pid, cmd_name, node_debug_id,
429526b47d8aSTodd Kjos (u64)node_ptr, (u64)node_cookie);
429626b47d8aSTodd Kjos
429726b47d8aSTodd Kjos *ptrp = ptr;
429826b47d8aSTodd Kjos return 0;
429926b47d8aSTodd Kjos }
430026b47d8aSTodd Kjos
binder_wait_for_work(struct binder_thread * thread,bool do_proc_work)43011b77e9dcSMartijn Coenen static int binder_wait_for_work(struct binder_thread *thread,
43021b77e9dcSMartijn Coenen bool do_proc_work)
43031b77e9dcSMartijn Coenen {
43041b77e9dcSMartijn Coenen DEFINE_WAIT(wait);
43051b77e9dcSMartijn Coenen struct binder_proc *proc = thread->proc;
43061b77e9dcSMartijn Coenen int ret = 0;
43071b77e9dcSMartijn Coenen
43081b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
43091b77e9dcSMartijn Coenen for (;;) {
4310f5d39b02SPeter Zijlstra prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE);
43111b77e9dcSMartijn Coenen if (binder_has_work_ilocked(thread, do_proc_work))
43121b77e9dcSMartijn Coenen break;
43131b77e9dcSMartijn Coenen if (do_proc_work)
43141b77e9dcSMartijn Coenen list_add(&thread->waiting_thread_node,
43151b77e9dcSMartijn Coenen &proc->waiting_threads);
43161b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
43171b77e9dcSMartijn Coenen schedule();
43181b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
43191b77e9dcSMartijn Coenen list_del_init(&thread->waiting_thread_node);
43201b77e9dcSMartijn Coenen if (signal_pending(current)) {
432195c16f9dSMarco Ballesio ret = -EINTR;
43221b77e9dcSMartijn Coenen break;
43231b77e9dcSMartijn Coenen }
43241b77e9dcSMartijn Coenen }
43251b77e9dcSMartijn Coenen finish_wait(&thread->wait, &wait);
43261b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
43271b77e9dcSMartijn Coenen
43281b77e9dcSMartijn Coenen return ret;
43291b77e9dcSMartijn Coenen }
43301b77e9dcSMartijn Coenen
433144d8047fSTodd Kjos /**
433244d8047fSTodd Kjos * binder_apply_fd_fixups() - finish fd translation
43338ced0c62STodd Kjos * @proc: binder_proc associated @t->buffer
433444d8047fSTodd Kjos * @t: binder transaction with list of fd fixups
433544d8047fSTodd Kjos *
433644d8047fSTodd Kjos * Now that we are in the context of the transaction target
433744d8047fSTodd Kjos * process, we can allocate and install fds. Process the
433844d8047fSTodd Kjos * list of fds to translate and fixup the buffer with the
4339a8a570c6SCarlos Llamas * new fds first and only then install the files.
434044d8047fSTodd Kjos *
4341a8a570c6SCarlos Llamas * If we fail to allocate an fd, skip the install and release
434244d8047fSTodd Kjos * any fds that have already been allocated.
434344d8047fSTodd Kjos */
binder_apply_fd_fixups(struct binder_proc * proc,struct binder_transaction * t)43448ced0c62STodd Kjos static int binder_apply_fd_fixups(struct binder_proc *proc,
43458ced0c62STodd Kjos struct binder_transaction *t)
434644d8047fSTodd Kjos {
434744d8047fSTodd Kjos struct binder_txn_fd_fixup *fixup, *tmp;
434844d8047fSTodd Kjos int ret = 0;
434944d8047fSTodd Kjos
435044d8047fSTodd Kjos list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
435144d8047fSTodd Kjos int fd = get_unused_fd_flags(O_CLOEXEC);
435244d8047fSTodd Kjos
435344d8047fSTodd Kjos if (fd < 0) {
435444d8047fSTodd Kjos binder_debug(BINDER_DEBUG_TRANSACTION,
435544d8047fSTodd Kjos "failed fd fixup txn %d fd %d\n",
435644d8047fSTodd Kjos t->debug_id, fd);
435744d8047fSTodd Kjos ret = -ENOMEM;
4358a8a570c6SCarlos Llamas goto err;
435944d8047fSTodd Kjos }
436044d8047fSTodd Kjos binder_debug(BINDER_DEBUG_TRANSACTION,
436144d8047fSTodd Kjos "fd fixup txn %d fd %d\n",
436244d8047fSTodd Kjos t->debug_id, fd);
436344d8047fSTodd Kjos trace_binder_transaction_fd_recv(t, fd, fixup->offset);
4364a8a570c6SCarlos Llamas fixup->target_fd = fd;
4365bb4a2e48STodd Kjos if (binder_alloc_copy_to_buffer(&proc->alloc, t->buffer,
43668ced0c62STodd Kjos fixup->offset, &fd,
4367bb4a2e48STodd Kjos sizeof(u32))) {
4368bb4a2e48STodd Kjos ret = -EINVAL;
4369a8a570c6SCarlos Llamas goto err;
4370bb4a2e48STodd Kjos }
437144d8047fSTodd Kjos }
437244d8047fSTodd Kjos list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
4373a8a570c6SCarlos Llamas fd_install(fixup->target_fd, fixup->file);
437444d8047fSTodd Kjos list_del(&fixup->fixup_entry);
437544d8047fSTodd Kjos kfree(fixup);
437644d8047fSTodd Kjos }
437744d8047fSTodd Kjos
437844d8047fSTodd Kjos return ret;
4379a8a570c6SCarlos Llamas
4380a8a570c6SCarlos Llamas err:
4381a8a570c6SCarlos Llamas binder_free_txn_fixups(t);
4382a8a570c6SCarlos Llamas return ret;
438344d8047fSTodd Kjos }
438444d8047fSTodd Kjos
binder_thread_read(struct binder_proc * proc,struct binder_thread * thread,binder_uintptr_t binder_buffer,size_t size,binder_size_t * consumed,int non_block)4385777783e0SGreg Kroah-Hartman static int binder_thread_read(struct binder_proc *proc,
4386777783e0SGreg Kroah-Hartman struct binder_thread *thread,
4387777783e0SGreg Kroah-Hartman binder_uintptr_t binder_buffer, size_t size,
4388777783e0SGreg Kroah-Hartman binder_size_t *consumed, int non_block)
4389777783e0SGreg Kroah-Hartman {
4390777783e0SGreg Kroah-Hartman void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4391777783e0SGreg Kroah-Hartman void __user *ptr = buffer + *consumed;
4392777783e0SGreg Kroah-Hartman void __user *end = buffer + size;
4393777783e0SGreg Kroah-Hartman
4394777783e0SGreg Kroah-Hartman int ret = 0;
4395777783e0SGreg Kroah-Hartman int wait_for_proc_work;
4396777783e0SGreg Kroah-Hartman
4397777783e0SGreg Kroah-Hartman if (*consumed == 0) {
4398777783e0SGreg Kroah-Hartman if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4399777783e0SGreg Kroah-Hartman return -EFAULT;
4400777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4401777783e0SGreg Kroah-Hartman }
4402777783e0SGreg Kroah-Hartman
4403777783e0SGreg Kroah-Hartman retry:
44040b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
44051b77e9dcSMartijn Coenen wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
44060b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
4407777783e0SGreg Kroah-Hartman
4408777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_WAITING;
4409777783e0SGreg Kroah-Hartman
4410777783e0SGreg Kroah-Hartman trace_binder_wait_for_work(wait_for_proc_work,
4411777783e0SGreg Kroah-Hartman !!thread->transaction_stack,
441272196393STodd Kjos !binder_worklist_empty(proc, &thread->todo));
4413777783e0SGreg Kroah-Hartman if (wait_for_proc_work) {
4414777783e0SGreg Kroah-Hartman if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4415777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_ENTERED))) {
4416777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
4417777783e0SGreg Kroah-Hartman proc->pid, thread->pid, thread->looper);
4418777783e0SGreg Kroah-Hartman wait_event_interruptible(binder_user_error_wait,
4419777783e0SGreg Kroah-Hartman binder_stop_on_user_error < 2);
4420777783e0SGreg Kroah-Hartman }
4421777783e0SGreg Kroah-Hartman binder_set_nice(proc->default_priority);
4422777783e0SGreg Kroah-Hartman }
4423777783e0SGreg Kroah-Hartman
44241b77e9dcSMartijn Coenen if (non_block) {
44251b77e9dcSMartijn Coenen if (!binder_has_work(thread, wait_for_proc_work))
44261b77e9dcSMartijn Coenen ret = -EAGAIN;
44271b77e9dcSMartijn Coenen } else {
44281b77e9dcSMartijn Coenen ret = binder_wait_for_work(thread, wait_for_proc_work);
44291b77e9dcSMartijn Coenen }
44301b77e9dcSMartijn Coenen
4431777783e0SGreg Kroah-Hartman thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4432777783e0SGreg Kroah-Hartman
4433777783e0SGreg Kroah-Hartman if (ret)
4434777783e0SGreg Kroah-Hartman return ret;
4435777783e0SGreg Kroah-Hartman
4436777783e0SGreg Kroah-Hartman while (1) {
4437777783e0SGreg Kroah-Hartman uint32_t cmd;
4438ec74136dSTodd Kjos struct binder_transaction_data_secctx tr;
4439ec74136dSTodd Kjos struct binder_transaction_data *trd = &tr.transaction_data;
444072196393STodd Kjos struct binder_work *w = NULL;
444172196393STodd Kjos struct list_head *list = NULL;
4442777783e0SGreg Kroah-Hartman struct binder_transaction *t = NULL;
44437a4408c6STodd Kjos struct binder_thread *t_from;
4444ec74136dSTodd Kjos size_t trsize = sizeof(*trd);
4445777783e0SGreg Kroah-Hartman
4446ed29721eSTodd Kjos binder_inner_proc_lock(proc);
444772196393STodd Kjos if (!binder_worklist_empty_ilocked(&thread->todo))
444872196393STodd Kjos list = &thread->todo;
444972196393STodd Kjos else if (!binder_worklist_empty_ilocked(&proc->todo) &&
445072196393STodd Kjos wait_for_proc_work)
445172196393STodd Kjos list = &proc->todo;
445272196393STodd Kjos else {
445372196393STodd Kjos binder_inner_proc_unlock(proc);
445472196393STodd Kjos
4455777783e0SGreg Kroah-Hartman /* no data added */
445608dabceeSTodd Kjos if (ptr - buffer == 4 && !thread->looper_need_return)
4457777783e0SGreg Kroah-Hartman goto retry;
4458777783e0SGreg Kroah-Hartman break;
4459777783e0SGreg Kroah-Hartman }
4460777783e0SGreg Kroah-Hartman
4461ed29721eSTodd Kjos if (end - ptr < sizeof(tr) + 4) {
4462ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4463777783e0SGreg Kroah-Hartman break;
4464ed29721eSTodd Kjos }
446572196393STodd Kjos w = binder_dequeue_work_head_ilocked(list);
4466148ade2cSMartijn Coenen if (binder_worklist_empty_ilocked(&thread->todo))
4467148ade2cSMartijn Coenen thread->process_todo = false;
4468777783e0SGreg Kroah-Hartman
4469777783e0SGreg Kroah-Hartman switch (w->type) {
4470777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION: {
4471ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4472777783e0SGreg Kroah-Hartman t = container_of(w, struct binder_transaction, work);
4473777783e0SGreg Kroah-Hartman } break;
447426549d17STodd Kjos case BINDER_WORK_RETURN_ERROR: {
447526549d17STodd Kjos struct binder_error *e = container_of(
447626549d17STodd Kjos w, struct binder_error, work);
447726549d17STodd Kjos
447826549d17STodd Kjos WARN_ON(e->cmd == BR_OK);
4479ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
448026549d17STodd Kjos if (put_user(e->cmd, (uint32_t __user *)ptr))
448126549d17STodd Kjos return -EFAULT;
4482838d5565S宋金时 cmd = e->cmd;
448326549d17STodd Kjos e->cmd = BR_OK;
448426549d17STodd Kjos ptr += sizeof(uint32_t);
448526549d17STodd Kjos
4486838d5565S宋金时 binder_stat_br(proc, thread, cmd);
448726549d17STodd Kjos } break;
4488a7dc1e6fSHang Lu case BINDER_WORK_TRANSACTION_COMPLETE:
44890567461aSLi Li case BINDER_WORK_TRANSACTION_PENDING:
4490a7dc1e6fSHang Lu case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
4491a7dc1e6fSHang Lu if (proc->oneway_spam_detection_enabled &&
4492a7dc1e6fSHang Lu w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
4493a7dc1e6fSHang Lu cmd = BR_ONEWAY_SPAM_SUSPECT;
44940567461aSLi Li else if (w->type == BINDER_WORK_TRANSACTION_PENDING)
44950567461aSLi Li cmd = BR_TRANSACTION_PENDING_FROZEN;
4496a7dc1e6fSHang Lu else
4497777783e0SGreg Kroah-Hartman cmd = BR_TRANSACTION_COMPLETE;
4498a7dc1e6fSHang Lu binder_inner_proc_unlock(proc);
44991909a671STodd Kjos kfree(w);
45001909a671STodd Kjos binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4501777783e0SGreg Kroah-Hartman if (put_user(cmd, (uint32_t __user *)ptr))
4502777783e0SGreg Kroah-Hartman return -EFAULT;
4503777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4504777783e0SGreg Kroah-Hartman
4505777783e0SGreg Kroah-Hartman binder_stat_br(proc, thread, cmd);
4506777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4507777783e0SGreg Kroah-Hartman "%d:%d BR_TRANSACTION_COMPLETE\n",
4508777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4509777783e0SGreg Kroah-Hartman } break;
4510777783e0SGreg Kroah-Hartman case BINDER_WORK_NODE: {
4511777783e0SGreg Kroah-Hartman struct binder_node *node = container_of(w, struct binder_node, work);
451226b47d8aSTodd Kjos int strong, weak;
451326b47d8aSTodd Kjos binder_uintptr_t node_ptr = node->ptr;
451426b47d8aSTodd Kjos binder_uintptr_t node_cookie = node->cookie;
451526b47d8aSTodd Kjos int node_debug_id = node->debug_id;
451626b47d8aSTodd Kjos int has_weak_ref;
451726b47d8aSTodd Kjos int has_strong_ref;
451826b47d8aSTodd Kjos void __user *orig_ptr = ptr;
4519777783e0SGreg Kroah-Hartman
452026b47d8aSTodd Kjos BUG_ON(proc != node->proc);
452126b47d8aSTodd Kjos strong = node->internal_strong_refs ||
452226b47d8aSTodd Kjos node->local_strong_refs;
452326b47d8aSTodd Kjos weak = !hlist_empty(&node->refs) ||
4524adc18842STodd Kjos node->local_weak_refs ||
4525adc18842STodd Kjos node->tmp_refs || strong;
452626b47d8aSTodd Kjos has_strong_ref = node->has_strong_ref;
452726b47d8aSTodd Kjos has_weak_ref = node->has_weak_ref;
452826b47d8aSTodd Kjos
452926b47d8aSTodd Kjos if (weak && !has_weak_ref) {
4530777783e0SGreg Kroah-Hartman node->has_weak_ref = 1;
4531777783e0SGreg Kroah-Hartman node->pending_weak_ref = 1;
4532777783e0SGreg Kroah-Hartman node->local_weak_refs++;
453326b47d8aSTodd Kjos }
453426b47d8aSTodd Kjos if (strong && !has_strong_ref) {
4535777783e0SGreg Kroah-Hartman node->has_strong_ref = 1;
4536777783e0SGreg Kroah-Hartman node->pending_strong_ref = 1;
4537777783e0SGreg Kroah-Hartman node->local_strong_refs++;
4538777783e0SGreg Kroah-Hartman }
453926b47d8aSTodd Kjos if (!strong && has_strong_ref)
454026b47d8aSTodd Kjos node->has_strong_ref = 0;
454126b47d8aSTodd Kjos if (!weak && has_weak_ref)
454226b47d8aSTodd Kjos node->has_weak_ref = 0;
4543777783e0SGreg Kroah-Hartman if (!weak && !strong) {
4544777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4545777783e0SGreg Kroah-Hartman "%d:%d node %d u%016llx c%016llx deleted\n",
4546777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
454726b47d8aSTodd Kjos node_debug_id,
454826b47d8aSTodd Kjos (u64)node_ptr,
454926b47d8aSTodd Kjos (u64)node_cookie);
4550777783e0SGreg Kroah-Hartman rb_erase(&node->rb_node, &proc->nodes);
4551ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4552673068eeSTodd Kjos binder_node_lock(node);
4553673068eeSTodd Kjos /*
4554673068eeSTodd Kjos * Acquire the node lock before freeing the
4555673068eeSTodd Kjos * node to serialize with other threads that
4556673068eeSTodd Kjos * may have been holding the node lock while
4557673068eeSTodd Kjos * decrementing this node (avoids race where
4558673068eeSTodd Kjos * this thread frees while the other thread
4559673068eeSTodd Kjos * is unlocking the node after the final
4560673068eeSTodd Kjos * decrement)
4561673068eeSTodd Kjos */
4562673068eeSTodd Kjos binder_node_unlock(node);
4563ed29721eSTodd Kjos binder_free_node(node);
4564ed29721eSTodd Kjos } else
4565ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4566ed29721eSTodd Kjos
456726b47d8aSTodd Kjos if (weak && !has_weak_ref)
456826b47d8aSTodd Kjos ret = binder_put_node_cmd(
456926b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
457026b47d8aSTodd Kjos node_cookie, node_debug_id,
457126b47d8aSTodd Kjos BR_INCREFS, "BR_INCREFS");
457226b47d8aSTodd Kjos if (!ret && strong && !has_strong_ref)
457326b47d8aSTodd Kjos ret = binder_put_node_cmd(
457426b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
457526b47d8aSTodd Kjos node_cookie, node_debug_id,
457626b47d8aSTodd Kjos BR_ACQUIRE, "BR_ACQUIRE");
457726b47d8aSTodd Kjos if (!ret && !strong && has_strong_ref)
457826b47d8aSTodd Kjos ret = binder_put_node_cmd(
457926b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
458026b47d8aSTodd Kjos node_cookie, node_debug_id,
458126b47d8aSTodd Kjos BR_RELEASE, "BR_RELEASE");
458226b47d8aSTodd Kjos if (!ret && !weak && has_weak_ref)
458326b47d8aSTodd Kjos ret = binder_put_node_cmd(
458426b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
458526b47d8aSTodd Kjos node_cookie, node_debug_id,
458626b47d8aSTodd Kjos BR_DECREFS, "BR_DECREFS");
458726b47d8aSTodd Kjos if (orig_ptr == ptr)
4588777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4589777783e0SGreg Kroah-Hartman "%d:%d node %d u%016llx c%016llx state unchanged\n",
4590777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
459126b47d8aSTodd Kjos node_debug_id,
459226b47d8aSTodd Kjos (u64)node_ptr,
459326b47d8aSTodd Kjos (u64)node_cookie);
459426b47d8aSTodd Kjos if (ret)
459526b47d8aSTodd Kjos return ret;
4596777783e0SGreg Kroah-Hartman } break;
4597777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER:
4598777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4599777783e0SGreg Kroah-Hartman case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4600777783e0SGreg Kroah-Hartman struct binder_ref_death *death;
4601777783e0SGreg Kroah-Hartman uint32_t cmd;
4602ab51ec6bSMartijn Coenen binder_uintptr_t cookie;
4603777783e0SGreg Kroah-Hartman
4604777783e0SGreg Kroah-Hartman death = container_of(w, struct binder_ref_death, work);
4605777783e0SGreg Kroah-Hartman if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4606777783e0SGreg Kroah-Hartman cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4607777783e0SGreg Kroah-Hartman else
4608777783e0SGreg Kroah-Hartman cmd = BR_DEAD_BINDER;
4609ab51ec6bSMartijn Coenen cookie = death->cookie;
4610ab51ec6bSMartijn Coenen
4611777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4612777783e0SGreg Kroah-Hartman "%d:%d %s %016llx\n",
4613777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4614777783e0SGreg Kroah-Hartman cmd == BR_DEAD_BINDER ?
4615777783e0SGreg Kroah-Hartman "BR_DEAD_BINDER" :
4616777783e0SGreg Kroah-Hartman "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4617ab51ec6bSMartijn Coenen (u64)cookie);
4618777783e0SGreg Kroah-Hartman if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4619ab51ec6bSMartijn Coenen binder_inner_proc_unlock(proc);
4620777783e0SGreg Kroah-Hartman kfree(death);
4621777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_DEATH);
4622ed29721eSTodd Kjos } else {
462372196393STodd Kjos binder_enqueue_work_ilocked(
462472196393STodd Kjos w, &proc->delivered_death);
4625ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4626ed29721eSTodd Kjos }
4627ab51ec6bSMartijn Coenen if (put_user(cmd, (uint32_t __user *)ptr))
4628ab51ec6bSMartijn Coenen return -EFAULT;
4629ab51ec6bSMartijn Coenen ptr += sizeof(uint32_t);
4630ab51ec6bSMartijn Coenen if (put_user(cookie,
4631ab51ec6bSMartijn Coenen (binder_uintptr_t __user *)ptr))
4632ab51ec6bSMartijn Coenen return -EFAULT;
4633ab51ec6bSMartijn Coenen ptr += sizeof(binder_uintptr_t);
4634ab51ec6bSMartijn Coenen binder_stat_br(proc, thread, cmd);
4635777783e0SGreg Kroah-Hartman if (cmd == BR_DEAD_BINDER)
4636777783e0SGreg Kroah-Hartman goto done; /* DEAD_BINDER notifications can cause transactions */
4637777783e0SGreg Kroah-Hartman } break;
4638324fa64cSTodd Kjos default:
4639324fa64cSTodd Kjos binder_inner_proc_unlock(proc);
4640324fa64cSTodd Kjos pr_err("%d:%d: bad work type %d\n",
4641324fa64cSTodd Kjos proc->pid, thread->pid, w->type);
4642324fa64cSTodd Kjos break;
4643777783e0SGreg Kroah-Hartman }
4644777783e0SGreg Kroah-Hartman
4645777783e0SGreg Kroah-Hartman if (!t)
4646777783e0SGreg Kroah-Hartman continue;
4647777783e0SGreg Kroah-Hartman
4648777783e0SGreg Kroah-Hartman BUG_ON(t->buffer == NULL);
4649777783e0SGreg Kroah-Hartman if (t->buffer->target_node) {
4650777783e0SGreg Kroah-Hartman struct binder_node *target_node = t->buffer->target_node;
4651777783e0SGreg Kroah-Hartman
4652ec74136dSTodd Kjos trd->target.ptr = target_node->ptr;
4653ec74136dSTodd Kjos trd->cookie = target_node->cookie;
4654777783e0SGreg Kroah-Hartman t->saved_priority = task_nice(current);
4655777783e0SGreg Kroah-Hartman if (t->priority < target_node->min_priority &&
4656777783e0SGreg Kroah-Hartman !(t->flags & TF_ONE_WAY))
4657777783e0SGreg Kroah-Hartman binder_set_nice(t->priority);
4658777783e0SGreg Kroah-Hartman else if (!(t->flags & TF_ONE_WAY) ||
4659777783e0SGreg Kroah-Hartman t->saved_priority > target_node->min_priority)
4660777783e0SGreg Kroah-Hartman binder_set_nice(target_node->min_priority);
4661777783e0SGreg Kroah-Hartman cmd = BR_TRANSACTION;
4662777783e0SGreg Kroah-Hartman } else {
4663ec74136dSTodd Kjos trd->target.ptr = 0;
4664ec74136dSTodd Kjos trd->cookie = 0;
4665777783e0SGreg Kroah-Hartman cmd = BR_REPLY;
4666777783e0SGreg Kroah-Hartman }
4667ec74136dSTodd Kjos trd->code = t->code;
4668ec74136dSTodd Kjos trd->flags = t->flags;
4669ec74136dSTodd Kjos trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4670777783e0SGreg Kroah-Hartman
46717a4408c6STodd Kjos t_from = binder_get_txn_from(t);
46727a4408c6STodd Kjos if (t_from) {
46737a4408c6STodd Kjos struct task_struct *sender = t_from->proc->tsk;
4674777783e0SGreg Kroah-Hartman
4675ec74136dSTodd Kjos trd->sender_pid =
4676ec74136dSTodd Kjos task_tgid_nr_ns(sender,
4677777783e0SGreg Kroah-Hartman task_active_pid_ns(current));
4678777783e0SGreg Kroah-Hartman } else {
4679ec74136dSTodd Kjos trd->sender_pid = 0;
4680777783e0SGreg Kroah-Hartman }
4681777783e0SGreg Kroah-Hartman
46828ced0c62STodd Kjos ret = binder_apply_fd_fixups(proc, t);
468344d8047fSTodd Kjos if (ret) {
468444d8047fSTodd Kjos struct binder_buffer *buffer = t->buffer;
468544d8047fSTodd Kjos bool oneway = !!(t->flags & TF_ONE_WAY);
468644d8047fSTodd Kjos int tid = t->debug_id;
468744d8047fSTodd Kjos
468844d8047fSTodd Kjos if (t_from)
468944d8047fSTodd Kjos binder_thread_dec_tmpref(t_from);
469044d8047fSTodd Kjos buffer->transaction = NULL;
469144d8047fSTodd Kjos binder_cleanup_transaction(t, "fd fixups failed",
469244d8047fSTodd Kjos BR_FAILED_REPLY);
469332e9f56aSTodd Kjos binder_free_buf(proc, thread, buffer, true);
469444d8047fSTodd Kjos binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
469544d8047fSTodd Kjos "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n",
469644d8047fSTodd Kjos proc->pid, thread->pid,
469744d8047fSTodd Kjos oneway ? "async " :
469844d8047fSTodd Kjos (cmd == BR_REPLY ? "reply " : ""),
469944d8047fSTodd Kjos tid, BR_FAILED_REPLY, ret, __LINE__);
470044d8047fSTodd Kjos if (cmd == BR_REPLY) {
470144d8047fSTodd Kjos cmd = BR_FAILED_REPLY;
470244d8047fSTodd Kjos if (put_user(cmd, (uint32_t __user *)ptr))
470344d8047fSTodd Kjos return -EFAULT;
470444d8047fSTodd Kjos ptr += sizeof(uint32_t);
470544d8047fSTodd Kjos binder_stat_br(proc, thread, cmd);
470644d8047fSTodd Kjos break;
470744d8047fSTodd Kjos }
470844d8047fSTodd Kjos continue;
470944d8047fSTodd Kjos }
4710ec74136dSTodd Kjos trd->data_size = t->buffer->data_size;
4711ec74136dSTodd Kjos trd->offsets_size = t->buffer->offsets_size;
4712bde4a19fSTodd Kjos trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
4713ec74136dSTodd Kjos trd->data.ptr.offsets = trd->data.ptr.buffer +
4714777783e0SGreg Kroah-Hartman ALIGN(t->buffer->data_size,
4715777783e0SGreg Kroah-Hartman sizeof(void *));
4716777783e0SGreg Kroah-Hartman
4717ec74136dSTodd Kjos tr.secctx = t->security_ctx;
4718ec74136dSTodd Kjos if (t->security_ctx) {
4719ec74136dSTodd Kjos cmd = BR_TRANSACTION_SEC_CTX;
4720ec74136dSTodd Kjos trsize = sizeof(tr);
4721ec74136dSTodd Kjos }
47227a4408c6STodd Kjos if (put_user(cmd, (uint32_t __user *)ptr)) {
47237a4408c6STodd Kjos if (t_from)
47247a4408c6STodd Kjos binder_thread_dec_tmpref(t_from);
4725fb2c4452SMartijn Coenen
4726fb2c4452SMartijn Coenen binder_cleanup_transaction(t, "put_user failed",
4727fb2c4452SMartijn Coenen BR_FAILED_REPLY);
4728fb2c4452SMartijn Coenen
4729777783e0SGreg Kroah-Hartman return -EFAULT;
47307a4408c6STodd Kjos }
4731777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4732ec74136dSTodd Kjos if (copy_to_user(ptr, &tr, trsize)) {
47337a4408c6STodd Kjos if (t_from)
47347a4408c6STodd Kjos binder_thread_dec_tmpref(t_from);
4735fb2c4452SMartijn Coenen
4736fb2c4452SMartijn Coenen binder_cleanup_transaction(t, "copy_to_user failed",
4737fb2c4452SMartijn Coenen BR_FAILED_REPLY);
4738fb2c4452SMartijn Coenen
4739777783e0SGreg Kroah-Hartman return -EFAULT;
47407a4408c6STodd Kjos }
4741ec74136dSTodd Kjos ptr += trsize;
4742777783e0SGreg Kroah-Hartman
4743777783e0SGreg Kroah-Hartman trace_binder_transaction_received(t);
4744777783e0SGreg Kroah-Hartman binder_stat_br(proc, thread, cmd);
4745777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
4746da486496SCarlos Llamas "%d:%d %s %d %d:%d, cmd %u size %zd-%zd ptr %016llx-%016llx\n",
4747777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4748777783e0SGreg Kroah-Hartman (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4749ec74136dSTodd Kjos (cmd == BR_TRANSACTION_SEC_CTX) ?
4750ec74136dSTodd Kjos "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
47517a4408c6STodd Kjos t->debug_id, t_from ? t_from->proc->pid : 0,
47527a4408c6STodd Kjos t_from ? t_from->pid : 0, cmd,
4753777783e0SGreg Kroah-Hartman t->buffer->data_size, t->buffer->offsets_size,
4754ec74136dSTodd Kjos (u64)trd->data.ptr.buffer,
4755ec74136dSTodd Kjos (u64)trd->data.ptr.offsets);
4756777783e0SGreg Kroah-Hartman
47577a4408c6STodd Kjos if (t_from)
47587a4408c6STodd Kjos binder_thread_dec_tmpref(t_from);
4759777783e0SGreg Kroah-Hartman t->buffer->allow_user_free = 1;
4760ec74136dSTodd Kjos if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
47610b89d69aSMartijn Coenen binder_inner_proc_lock(thread->proc);
4762777783e0SGreg Kroah-Hartman t->to_parent = thread->transaction_stack;
4763777783e0SGreg Kroah-Hartman t->to_thread = thread;
4764777783e0SGreg Kroah-Hartman thread->transaction_stack = t;
47650b89d69aSMartijn Coenen binder_inner_proc_unlock(thread->proc);
4766777783e0SGreg Kroah-Hartman } else {
4767b6d282ceSTodd Kjos binder_free_transaction(t);
4768777783e0SGreg Kroah-Hartman }
4769777783e0SGreg Kroah-Hartman break;
4770777783e0SGreg Kroah-Hartman }
4771777783e0SGreg Kroah-Hartman
4772777783e0SGreg Kroah-Hartman done:
4773777783e0SGreg Kroah-Hartman
4774777783e0SGreg Kroah-Hartman *consumed = ptr - buffer;
4775b3e68612STodd Kjos binder_inner_proc_lock(proc);
47761b77e9dcSMartijn Coenen if (proc->requested_threads == 0 &&
47771b77e9dcSMartijn Coenen list_empty(&thread->proc->waiting_threads) &&
4778777783e0SGreg Kroah-Hartman proc->requested_threads_started < proc->max_threads &&
4779777783e0SGreg Kroah-Hartman (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4780777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4781777783e0SGreg Kroah-Hartman /*spawn a new thread if we leave this out */) {
4782777783e0SGreg Kroah-Hartman proc->requested_threads++;
4783b3e68612STodd Kjos binder_inner_proc_unlock(proc);
4784777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4785777783e0SGreg Kroah-Hartman "%d:%d BR_SPAWN_LOOPER\n",
4786777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4787777783e0SGreg Kroah-Hartman if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4788777783e0SGreg Kroah-Hartman return -EFAULT;
4789777783e0SGreg Kroah-Hartman binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
4790b3e68612STodd Kjos } else
4791b3e68612STodd Kjos binder_inner_proc_unlock(proc);
4792777783e0SGreg Kroah-Hartman return 0;
4793777783e0SGreg Kroah-Hartman }
4794777783e0SGreg Kroah-Hartman
binder_release_work(struct binder_proc * proc,struct list_head * list)479572196393STodd Kjos static void binder_release_work(struct binder_proc *proc,
479672196393STodd Kjos struct list_head *list)
4797777783e0SGreg Kroah-Hartman {
4798777783e0SGreg Kroah-Hartman struct binder_work *w;
4799f3277cbfSTodd Kjos enum binder_work_type wtype;
4800777783e0SGreg Kroah-Hartman
480172196393STodd Kjos while (1) {
4802f3277cbfSTodd Kjos binder_inner_proc_lock(proc);
4803f3277cbfSTodd Kjos w = binder_dequeue_work_head_ilocked(list);
4804f3277cbfSTodd Kjos wtype = w ? w->type : 0;
4805f3277cbfSTodd Kjos binder_inner_proc_unlock(proc);
480672196393STodd Kjos if (!w)
480772196393STodd Kjos return;
480872196393STodd Kjos
4809f3277cbfSTodd Kjos switch (wtype) {
4810777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION: {
4811777783e0SGreg Kroah-Hartman struct binder_transaction *t;
4812777783e0SGreg Kroah-Hartman
4813777783e0SGreg Kroah-Hartman t = container_of(w, struct binder_transaction, work);
4814fb2c4452SMartijn Coenen
4815fb2c4452SMartijn Coenen binder_cleanup_transaction(t, "process died.",
4816fb2c4452SMartijn Coenen BR_DEAD_REPLY);
4817777783e0SGreg Kroah-Hartman } break;
481826549d17STodd Kjos case BINDER_WORK_RETURN_ERROR: {
481926549d17STodd Kjos struct binder_error *e = container_of(
482026549d17STodd Kjos w, struct binder_error, work);
482126549d17STodd Kjos
482226549d17STodd Kjos binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
482326549d17STodd Kjos "undelivered TRANSACTION_ERROR: %u\n",
482426549d17STodd Kjos e->cmd);
482526549d17STodd Kjos } break;
48261aa3aaf8SCarlos Llamas case BINDER_WORK_TRANSACTION_PENDING:
48271aa3aaf8SCarlos Llamas case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT:
4828777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION_COMPLETE: {
4829777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4830777783e0SGreg Kroah-Hartman "undelivered TRANSACTION_COMPLETE\n");
4831777783e0SGreg Kroah-Hartman kfree(w);
4832777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4833777783e0SGreg Kroah-Hartman } break;
4834777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4835777783e0SGreg Kroah-Hartman case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4836777783e0SGreg Kroah-Hartman struct binder_ref_death *death;
4837777783e0SGreg Kroah-Hartman
4838777783e0SGreg Kroah-Hartman death = container_of(w, struct binder_ref_death, work);
4839777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4840777783e0SGreg Kroah-Hartman "undelivered death notification, %016llx\n",
4841777783e0SGreg Kroah-Hartman (u64)death->cookie);
4842777783e0SGreg Kroah-Hartman kfree(death);
4843777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_DEATH);
4844777783e0SGreg Kroah-Hartman } break;
4845f3277cbfSTodd Kjos case BINDER_WORK_NODE:
4846f3277cbfSTodd Kjos break;
4847777783e0SGreg Kroah-Hartman default:
4848777783e0SGreg Kroah-Hartman pr_err("unexpected work type, %d, not freed\n",
4849f3277cbfSTodd Kjos wtype);
4850777783e0SGreg Kroah-Hartman break;
4851777783e0SGreg Kroah-Hartman }
4852777783e0SGreg Kroah-Hartman }
4853777783e0SGreg Kroah-Hartman
4854777783e0SGreg Kroah-Hartman }
4855777783e0SGreg Kroah-Hartman
binder_get_thread_ilocked(struct binder_proc * proc,struct binder_thread * new_thread)48567bd7b0e6STodd Kjos static struct binder_thread *binder_get_thread_ilocked(
48577bd7b0e6STodd Kjos struct binder_proc *proc, struct binder_thread *new_thread)
4858777783e0SGreg Kroah-Hartman {
4859777783e0SGreg Kroah-Hartman struct binder_thread *thread = NULL;
4860777783e0SGreg Kroah-Hartman struct rb_node *parent = NULL;
4861777783e0SGreg Kroah-Hartman struct rb_node **p = &proc->threads.rb_node;
4862777783e0SGreg Kroah-Hartman
4863777783e0SGreg Kroah-Hartman while (*p) {
4864777783e0SGreg Kroah-Hartman parent = *p;
4865777783e0SGreg Kroah-Hartman thread = rb_entry(parent, struct binder_thread, rb_node);
4866777783e0SGreg Kroah-Hartman
4867777783e0SGreg Kroah-Hartman if (current->pid < thread->pid)
4868777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
4869777783e0SGreg Kroah-Hartman else if (current->pid > thread->pid)
4870777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
4871777783e0SGreg Kroah-Hartman else
48727bd7b0e6STodd Kjos return thread;
4873777783e0SGreg Kroah-Hartman }
48747bd7b0e6STodd Kjos if (!new_thread)
4875777783e0SGreg Kroah-Hartman return NULL;
48767bd7b0e6STodd Kjos thread = new_thread;
4877777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_THREAD);
4878777783e0SGreg Kroah-Hartman thread->proc = proc;
4879777783e0SGreg Kroah-Hartman thread->pid = current->pid;
48807a4408c6STodd Kjos atomic_set(&thread->tmp_ref, 0);
4881777783e0SGreg Kroah-Hartman init_waitqueue_head(&thread->wait);
4882777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&thread->todo);
4883777783e0SGreg Kroah-Hartman rb_link_node(&thread->rb_node, parent, p);
4884777783e0SGreg Kroah-Hartman rb_insert_color(&thread->rb_node, &proc->threads);
488508dabceeSTodd Kjos thread->looper_need_return = true;
488626549d17STodd Kjos thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
488726549d17STodd Kjos thread->return_error.cmd = BR_OK;
488826549d17STodd Kjos thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
488926549d17STodd Kjos thread->reply_error.cmd = BR_OK;
4890bd32889eSCarlos Llamas thread->ee.command = BR_OK;
48911b77e9dcSMartijn Coenen INIT_LIST_HEAD(&new_thread->waiting_thread_node);
48927bd7b0e6STodd Kjos return thread;
48937bd7b0e6STodd Kjos }
48947bd7b0e6STodd Kjos
binder_get_thread(struct binder_proc * proc)48957bd7b0e6STodd Kjos static struct binder_thread *binder_get_thread(struct binder_proc *proc)
48967bd7b0e6STodd Kjos {
48977bd7b0e6STodd Kjos struct binder_thread *thread;
48987bd7b0e6STodd Kjos struct binder_thread *new_thread;
48997bd7b0e6STodd Kjos
49007bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
49017bd7b0e6STodd Kjos thread = binder_get_thread_ilocked(proc, NULL);
49027bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
49037bd7b0e6STodd Kjos if (!thread) {
49047bd7b0e6STodd Kjos new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
49057bd7b0e6STodd Kjos if (new_thread == NULL)
49067bd7b0e6STodd Kjos return NULL;
49077bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
49087bd7b0e6STodd Kjos thread = binder_get_thread_ilocked(proc, new_thread);
49097bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
49107bd7b0e6STodd Kjos if (thread != new_thread)
49117bd7b0e6STodd Kjos kfree(new_thread);
4912777783e0SGreg Kroah-Hartman }
4913777783e0SGreg Kroah-Hartman return thread;
4914777783e0SGreg Kroah-Hartman }
4915777783e0SGreg Kroah-Hartman
binder_free_proc(struct binder_proc * proc)49167a4408c6STodd Kjos static void binder_free_proc(struct binder_proc *proc)
49177a4408c6STodd Kjos {
4918d35d3660STodd Kjos struct binder_device *device;
4919d35d3660STodd Kjos
49207a4408c6STodd Kjos BUG_ON(!list_empty(&proc->todo));
49217a4408c6STodd Kjos BUG_ON(!list_empty(&proc->delivered_death));
4922432ff1e9SMarco Ballesio if (proc->outstanding_txns)
4923432ff1e9SMarco Ballesio pr_warn("%s: Unexpected outstanding_txns %d\n",
4924432ff1e9SMarco Ballesio __func__, proc->outstanding_txns);
4925d35d3660STodd Kjos device = container_of(proc->context, struct binder_device, context);
4926d35d3660STodd Kjos if (refcount_dec_and_test(&device->ref)) {
4927d35d3660STodd Kjos kfree(proc->context->name);
4928d35d3660STodd Kjos kfree(device);
4929d35d3660STodd Kjos }
49307a4408c6STodd Kjos binder_alloc_deferred_release(&proc->alloc);
49317a4408c6STodd Kjos put_task_struct(proc->tsk);
493229bc22acSTodd Kjos put_cred(proc->cred);
49337a4408c6STodd Kjos binder_stats_deleted(BINDER_STAT_PROC);
49347a4408c6STodd Kjos kfree(proc);
49357a4408c6STodd Kjos }
49367a4408c6STodd Kjos
binder_free_thread(struct binder_thread * thread)49377a4408c6STodd Kjos static void binder_free_thread(struct binder_thread *thread)
49387a4408c6STodd Kjos {
49397a4408c6STodd Kjos BUG_ON(!list_empty(&thread->todo));
49407a4408c6STodd Kjos binder_stats_deleted(BINDER_STAT_THREAD);
49417a4408c6STodd Kjos binder_proc_dec_tmpref(thread->proc);
49427a4408c6STodd Kjos kfree(thread);
49437a4408c6STodd Kjos }
49447a4408c6STodd Kjos
binder_thread_release(struct binder_proc * proc,struct binder_thread * thread)49457a4408c6STodd Kjos static int binder_thread_release(struct binder_proc *proc,
4946777783e0SGreg Kroah-Hartman struct binder_thread *thread)
4947777783e0SGreg Kroah-Hartman {
4948777783e0SGreg Kroah-Hartman struct binder_transaction *t;
4949777783e0SGreg Kroah-Hartman struct binder_transaction *send_reply = NULL;
4950777783e0SGreg Kroah-Hartman int active_transactions = 0;
49517a4408c6STodd Kjos struct binder_transaction *last_t = NULL;
4952777783e0SGreg Kroah-Hartman
49537bd7b0e6STodd Kjos binder_inner_proc_lock(thread->proc);
49547a4408c6STodd Kjos /*
49557a4408c6STodd Kjos * take a ref on the proc so it survives
49567a4408c6STodd Kjos * after we remove this thread from proc->threads.
49577a4408c6STodd Kjos * The corresponding dec is when we actually
49587a4408c6STodd Kjos * free the thread in binder_free_thread()
49597a4408c6STodd Kjos */
49607a4408c6STodd Kjos proc->tmp_ref++;
49617a4408c6STodd Kjos /*
49627a4408c6STodd Kjos * take a ref on this thread to ensure it
49637a4408c6STodd Kjos * survives while we are releasing it
49647a4408c6STodd Kjos */
49657a4408c6STodd Kjos atomic_inc(&thread->tmp_ref);
4966777783e0SGreg Kroah-Hartman rb_erase(&thread->rb_node, &proc->threads);
4967777783e0SGreg Kroah-Hartman t = thread->transaction_stack;
49687a4408c6STodd Kjos if (t) {
49697a4408c6STodd Kjos spin_lock(&t->lock);
49707a4408c6STodd Kjos if (t->to_thread == thread)
4971777783e0SGreg Kroah-Hartman send_reply = t;
4972324fa64cSTodd Kjos } else {
4973324fa64cSTodd Kjos __acquire(&t->lock);
49747a4408c6STodd Kjos }
49757a4408c6STodd Kjos thread->is_dead = true;
49767a4408c6STodd Kjos
4977777783e0SGreg Kroah-Hartman while (t) {
49787a4408c6STodd Kjos last_t = t;
4979777783e0SGreg Kroah-Hartman active_transactions++;
4980777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4981777783e0SGreg Kroah-Hartman "release %d:%d transaction %d %s, still active\n",
4982777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4983777783e0SGreg Kroah-Hartman t->debug_id,
4984777783e0SGreg Kroah-Hartman (t->to_thread == thread) ? "in" : "out");
4985777783e0SGreg Kroah-Hartman
4986777783e0SGreg Kroah-Hartman if (t->to_thread == thread) {
4987432ff1e9SMarco Ballesio thread->proc->outstanding_txns--;
4988777783e0SGreg Kroah-Hartman t->to_proc = NULL;
4989777783e0SGreg Kroah-Hartman t->to_thread = NULL;
4990777783e0SGreg Kroah-Hartman if (t->buffer) {
4991777783e0SGreg Kroah-Hartman t->buffer->transaction = NULL;
4992777783e0SGreg Kroah-Hartman t->buffer = NULL;
4993777783e0SGreg Kroah-Hartman }
4994777783e0SGreg Kroah-Hartman t = t->to_parent;
4995777783e0SGreg Kroah-Hartman } else if (t->from == thread) {
4996777783e0SGreg Kroah-Hartman t->from = NULL;
4997777783e0SGreg Kroah-Hartman t = t->from_parent;
4998777783e0SGreg Kroah-Hartman } else
4999777783e0SGreg Kroah-Hartman BUG();
50007a4408c6STodd Kjos spin_unlock(&last_t->lock);
50017a4408c6STodd Kjos if (t)
50027a4408c6STodd Kjos spin_lock(&t->lock);
5003324fa64cSTodd Kjos else
5004324fa64cSTodd Kjos __acquire(&t->lock);
5005777783e0SGreg Kroah-Hartman }
5006324fa64cSTodd Kjos /* annotation for sparse, lock not acquired in last iteration above */
5007324fa64cSTodd Kjos __release(&t->lock);
5008f5cb779bSMartijn Coenen
5009f5cb779bSMartijn Coenen /*
5010a880b28aSEric Biggers * If this thread used poll, make sure we remove the waitqueue from any
5011a880b28aSEric Biggers * poll data structures holding it.
5012f5cb779bSMartijn Coenen */
5013a880b28aSEric Biggers if (thread->looper & BINDER_LOOPER_STATE_POLL)
5014a880b28aSEric Biggers wake_up_pollfree(&thread->wait);
5015f5cb779bSMartijn Coenen
50167bd7b0e6STodd Kjos binder_inner_proc_unlock(thread->proc);
50177a4408c6STodd Kjos
50185eeb2ca0SMartijn Coenen /*
5019a880b28aSEric Biggers * This is needed to avoid races between wake_up_pollfree() above and
5020a880b28aSEric Biggers * someone else removing the last entry from the queue for other reasons
5021a880b28aSEric Biggers * (e.g. ep_remove_wait_queue() being called due to an epoll file
5022a880b28aSEric Biggers * descriptor being closed). Such other users hold an RCU read lock, so
5023a880b28aSEric Biggers * we can be sure they're done after we call synchronize_rcu().
50245eeb2ca0SMartijn Coenen */
50255eeb2ca0SMartijn Coenen if (thread->looper & BINDER_LOOPER_STATE_POLL)
50265eeb2ca0SMartijn Coenen synchronize_rcu();
50275eeb2ca0SMartijn Coenen
5028777783e0SGreg Kroah-Hartman if (send_reply)
5029777783e0SGreg Kroah-Hartman binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
503072196393STodd Kjos binder_release_work(proc, &thread->todo);
50317a4408c6STodd Kjos binder_thread_dec_tmpref(thread);
5032777783e0SGreg Kroah-Hartman return active_transactions;
5033777783e0SGreg Kroah-Hartman }
5034777783e0SGreg Kroah-Hartman
binder_poll(struct file * filp,struct poll_table_struct * wait)5035afc9a42bSAl Viro static __poll_t binder_poll(struct file *filp,
5036777783e0SGreg Kroah-Hartman struct poll_table_struct *wait)
5037777783e0SGreg Kroah-Hartman {
5038777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5039777783e0SGreg Kroah-Hartman struct binder_thread *thread = NULL;
50401b77e9dcSMartijn Coenen bool wait_for_proc_work;
5041777783e0SGreg Kroah-Hartman
5042777783e0SGreg Kroah-Hartman thread = binder_get_thread(proc);
5043f8898267SEric Biggers if (!thread)
5044802c6131SCarlos Llamas return EPOLLERR;
5045777783e0SGreg Kroah-Hartman
50460b89d69aSMartijn Coenen binder_inner_proc_lock(thread->proc);
50471b77e9dcSMartijn Coenen thread->looper |= BINDER_LOOPER_STATE_POLL;
50481b77e9dcSMartijn Coenen wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
50491b77e9dcSMartijn Coenen
50500b89d69aSMartijn Coenen binder_inner_proc_unlock(thread->proc);
5051777783e0SGreg Kroah-Hartman
5052777783e0SGreg Kroah-Hartman poll_wait(filp, &thread->wait, wait);
50531b77e9dcSMartijn Coenen
505466b83a4cSMartijn Coenen if (binder_has_work(thread, wait_for_proc_work))
5055a9a08845SLinus Torvalds return EPOLLIN;
50561b77e9dcSMartijn Coenen
5057777783e0SGreg Kroah-Hartman return 0;
5058777783e0SGreg Kroah-Hartman }
5059777783e0SGreg Kroah-Hartman
binder_ioctl_write_read(struct file * filp,unsigned long arg,struct binder_thread * thread)50607feb35bcSJiazi.Li static int binder_ioctl_write_read(struct file *filp, unsigned long arg,
5061777783e0SGreg Kroah-Hartman struct binder_thread *thread)
5062777783e0SGreg Kroah-Hartman {
5063777783e0SGreg Kroah-Hartman int ret = 0;
5064777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5065777783e0SGreg Kroah-Hartman void __user *ubuf = (void __user *)arg;
5066777783e0SGreg Kroah-Hartman struct binder_write_read bwr;
5067777783e0SGreg Kroah-Hartman
5068777783e0SGreg Kroah-Hartman if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
5069777783e0SGreg Kroah-Hartman ret = -EFAULT;
5070777783e0SGreg Kroah-Hartman goto out;
5071777783e0SGreg Kroah-Hartman }
5072777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_READ_WRITE,
5073777783e0SGreg Kroah-Hartman "%d:%d write %lld at %016llx, read %lld at %016llx\n",
5074777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
5075777783e0SGreg Kroah-Hartman (u64)bwr.write_size, (u64)bwr.write_buffer,
5076777783e0SGreg Kroah-Hartman (u64)bwr.read_size, (u64)bwr.read_buffer);
5077777783e0SGreg Kroah-Hartman
5078777783e0SGreg Kroah-Hartman if (bwr.write_size > 0) {
5079777783e0SGreg Kroah-Hartman ret = binder_thread_write(proc, thread,
5080777783e0SGreg Kroah-Hartman bwr.write_buffer,
5081777783e0SGreg Kroah-Hartman bwr.write_size,
5082777783e0SGreg Kroah-Hartman &bwr.write_consumed);
5083777783e0SGreg Kroah-Hartman trace_binder_write_done(ret);
5084777783e0SGreg Kroah-Hartman if (ret < 0) {
5085777783e0SGreg Kroah-Hartman bwr.read_consumed = 0;
5086777783e0SGreg Kroah-Hartman if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
5087777783e0SGreg Kroah-Hartman ret = -EFAULT;
5088777783e0SGreg Kroah-Hartman goto out;
5089777783e0SGreg Kroah-Hartman }
5090777783e0SGreg Kroah-Hartman }
5091777783e0SGreg Kroah-Hartman if (bwr.read_size > 0) {
5092777783e0SGreg Kroah-Hartman ret = binder_thread_read(proc, thread, bwr.read_buffer,
5093777783e0SGreg Kroah-Hartman bwr.read_size,
5094777783e0SGreg Kroah-Hartman &bwr.read_consumed,
5095777783e0SGreg Kroah-Hartman filp->f_flags & O_NONBLOCK);
5096777783e0SGreg Kroah-Hartman trace_binder_read_done(ret);
50971b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
50981b77e9dcSMartijn Coenen if (!binder_worklist_empty_ilocked(&proc->todo))
5099408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(proc);
51001b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
5101777783e0SGreg Kroah-Hartman if (ret < 0) {
5102777783e0SGreg Kroah-Hartman if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
5103777783e0SGreg Kroah-Hartman ret = -EFAULT;
5104777783e0SGreg Kroah-Hartman goto out;
5105777783e0SGreg Kroah-Hartman }
5106777783e0SGreg Kroah-Hartman }
5107777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_READ_WRITE,
5108777783e0SGreg Kroah-Hartman "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
5109777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
5110777783e0SGreg Kroah-Hartman (u64)bwr.write_consumed, (u64)bwr.write_size,
5111777783e0SGreg Kroah-Hartman (u64)bwr.read_consumed, (u64)bwr.read_size);
5112777783e0SGreg Kroah-Hartman if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
5113777783e0SGreg Kroah-Hartman ret = -EFAULT;
5114777783e0SGreg Kroah-Hartman goto out;
5115777783e0SGreg Kroah-Hartman }
5116777783e0SGreg Kroah-Hartman out:
5117777783e0SGreg Kroah-Hartman return ret;
5118777783e0SGreg Kroah-Hartman }
5119777783e0SGreg Kroah-Hartman
binder_ioctl_set_ctx_mgr(struct file * filp,struct flat_binder_object * fbo)5120ec74136dSTodd Kjos static int binder_ioctl_set_ctx_mgr(struct file *filp,
5121ec74136dSTodd Kjos struct flat_binder_object *fbo)
5122777783e0SGreg Kroah-Hartman {
5123777783e0SGreg Kroah-Hartman int ret = 0;
5124777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5125342e5c90SMartijn Coenen struct binder_context *context = proc->context;
5126c44b1231STodd Kjos struct binder_node *new_node;
5127777783e0SGreg Kroah-Hartman kuid_t curr_euid = current_euid();
5128777783e0SGreg Kroah-Hartman
5129c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
5130342e5c90SMartijn Coenen if (context->binder_context_mgr_node) {
5131777783e0SGreg Kroah-Hartman pr_err("BINDER_SET_CONTEXT_MGR already set\n");
5132777783e0SGreg Kroah-Hartman ret = -EBUSY;
5133777783e0SGreg Kroah-Hartman goto out;
5134777783e0SGreg Kroah-Hartman }
513552f88693STodd Kjos ret = security_binder_set_context_mgr(proc->cred);
513679af7307SStephen Smalley if (ret < 0)
513779af7307SStephen Smalley goto out;
5138342e5c90SMartijn Coenen if (uid_valid(context->binder_context_mgr_uid)) {
5139342e5c90SMartijn Coenen if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
5140777783e0SGreg Kroah-Hartman pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
5141777783e0SGreg Kroah-Hartman from_kuid(&init_user_ns, curr_euid),
5142777783e0SGreg Kroah-Hartman from_kuid(&init_user_ns,
5143342e5c90SMartijn Coenen context->binder_context_mgr_uid));
5144777783e0SGreg Kroah-Hartman ret = -EPERM;
5145777783e0SGreg Kroah-Hartman goto out;
5146777783e0SGreg Kroah-Hartman }
5147777783e0SGreg Kroah-Hartman } else {
5148342e5c90SMartijn Coenen context->binder_context_mgr_uid = curr_euid;
5149777783e0SGreg Kroah-Hartman }
5150ec74136dSTodd Kjos new_node = binder_new_node(proc, fbo);
5151c44b1231STodd Kjos if (!new_node) {
5152777783e0SGreg Kroah-Hartman ret = -ENOMEM;
5153777783e0SGreg Kroah-Hartman goto out;
5154777783e0SGreg Kroah-Hartman }
5155673068eeSTodd Kjos binder_node_lock(new_node);
5156c44b1231STodd Kjos new_node->local_weak_refs++;
5157c44b1231STodd Kjos new_node->local_strong_refs++;
5158c44b1231STodd Kjos new_node->has_strong_ref = 1;
5159c44b1231STodd Kjos new_node->has_weak_ref = 1;
5160c44b1231STodd Kjos context->binder_context_mgr_node = new_node;
5161673068eeSTodd Kjos binder_node_unlock(new_node);
5162adc18842STodd Kjos binder_put_node(new_node);
5163777783e0SGreg Kroah-Hartman out:
5164c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
5165777783e0SGreg Kroah-Hartman return ret;
5166777783e0SGreg Kroah-Hartman }
5167777783e0SGreg Kroah-Hartman
binder_ioctl_get_node_info_for_ref(struct binder_proc * proc,struct binder_node_info_for_ref * info)5168b7e6a896SMartijn Coenen static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
5169b7e6a896SMartijn Coenen struct binder_node_info_for_ref *info)
5170b7e6a896SMartijn Coenen {
5171b7e6a896SMartijn Coenen struct binder_node *node;
5172b7e6a896SMartijn Coenen struct binder_context *context = proc->context;
5173b7e6a896SMartijn Coenen __u32 handle = info->handle;
5174b7e6a896SMartijn Coenen
5175b7e6a896SMartijn Coenen if (info->strong_count || info->weak_count || info->reserved1 ||
5176b7e6a896SMartijn Coenen info->reserved2 || info->reserved3) {
5177b7e6a896SMartijn Coenen binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
5178b7e6a896SMartijn Coenen proc->pid);
5179b7e6a896SMartijn Coenen return -EINVAL;
5180b7e6a896SMartijn Coenen }
5181b7e6a896SMartijn Coenen
5182b7e6a896SMartijn Coenen /* This ioctl may only be used by the context manager */
5183b7e6a896SMartijn Coenen mutex_lock(&context->context_mgr_node_lock);
5184b7e6a896SMartijn Coenen if (!context->binder_context_mgr_node ||
5185b7e6a896SMartijn Coenen context->binder_context_mgr_node->proc != proc) {
5186b7e6a896SMartijn Coenen mutex_unlock(&context->context_mgr_node_lock);
5187b7e6a896SMartijn Coenen return -EPERM;
5188b7e6a896SMartijn Coenen }
5189b7e6a896SMartijn Coenen mutex_unlock(&context->context_mgr_node_lock);
5190b7e6a896SMartijn Coenen
5191b7e6a896SMartijn Coenen node = binder_get_node_from_ref(proc, handle, true, NULL);
5192b7e6a896SMartijn Coenen if (!node)
5193b7e6a896SMartijn Coenen return -EINVAL;
5194b7e6a896SMartijn Coenen
5195b7e6a896SMartijn Coenen info->strong_count = node->local_strong_refs +
5196b7e6a896SMartijn Coenen node->internal_strong_refs;
5197b7e6a896SMartijn Coenen info->weak_count = node->local_weak_refs;
5198b7e6a896SMartijn Coenen
5199b7e6a896SMartijn Coenen binder_put_node(node);
5200b7e6a896SMartijn Coenen
5201b7e6a896SMartijn Coenen return 0;
5202b7e6a896SMartijn Coenen }
5203b7e6a896SMartijn Coenen
binder_ioctl_get_node_debug_info(struct binder_proc * proc,struct binder_node_debug_info * info)5204abcc6153SColin Cross static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
5205abcc6153SColin Cross struct binder_node_debug_info *info)
5206abcc6153SColin Cross {
5207abcc6153SColin Cross struct rb_node *n;
5208abcc6153SColin Cross binder_uintptr_t ptr = info->ptr;
5209abcc6153SColin Cross
5210abcc6153SColin Cross memset(info, 0, sizeof(*info));
5211abcc6153SColin Cross
5212abcc6153SColin Cross binder_inner_proc_lock(proc);
5213abcc6153SColin Cross for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5214abcc6153SColin Cross struct binder_node *node = rb_entry(n, struct binder_node,
5215abcc6153SColin Cross rb_node);
5216abcc6153SColin Cross if (node->ptr > ptr) {
5217abcc6153SColin Cross info->ptr = node->ptr;
5218abcc6153SColin Cross info->cookie = node->cookie;
5219abcc6153SColin Cross info->has_strong_ref = node->has_strong_ref;
5220abcc6153SColin Cross info->has_weak_ref = node->has_weak_ref;
5221abcc6153SColin Cross break;
5222abcc6153SColin Cross }
5223abcc6153SColin Cross }
5224abcc6153SColin Cross binder_inner_proc_unlock(proc);
5225abcc6153SColin Cross
5226abcc6153SColin Cross return 0;
5227abcc6153SColin Cross }
5228abcc6153SColin Cross
binder_txns_pending_ilocked(struct binder_proc * proc)5229b564171aSLi Li static bool binder_txns_pending_ilocked(struct binder_proc *proc)
5230b564171aSLi Li {
5231b564171aSLi Li struct rb_node *n;
5232b564171aSLi Li struct binder_thread *thread;
5233b564171aSLi Li
5234b564171aSLi Li if (proc->outstanding_txns > 0)
5235b564171aSLi Li return true;
5236b564171aSLi Li
5237b564171aSLi Li for (n = rb_first(&proc->threads); n; n = rb_next(n)) {
5238b564171aSLi Li thread = rb_entry(n, struct binder_thread, rb_node);
5239b564171aSLi Li if (thread->transaction_stack)
5240b564171aSLi Li return true;
5241b564171aSLi Li }
5242b564171aSLi Li return false;
5243b564171aSLi Li }
5244b564171aSLi Li
binder_ioctl_freeze(struct binder_freeze_info * info,struct binder_proc * target_proc)5245432ff1e9SMarco Ballesio static int binder_ioctl_freeze(struct binder_freeze_info *info,
5246432ff1e9SMarco Ballesio struct binder_proc *target_proc)
5247432ff1e9SMarco Ballesio {
5248432ff1e9SMarco Ballesio int ret = 0;
5249432ff1e9SMarco Ballesio
5250432ff1e9SMarco Ballesio if (!info->enable) {
5251432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5252ae28c1beSMarco Ballesio target_proc->sync_recv = false;
5253ae28c1beSMarco Ballesio target_proc->async_recv = false;
5254432ff1e9SMarco Ballesio target_proc->is_frozen = false;
5255432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5256432ff1e9SMarco Ballesio return 0;
5257432ff1e9SMarco Ballesio }
5258432ff1e9SMarco Ballesio
5259432ff1e9SMarco Ballesio /*
5260432ff1e9SMarco Ballesio * Freezing the target. Prevent new transactions by
5261432ff1e9SMarco Ballesio * setting frozen state. If timeout specified, wait
5262432ff1e9SMarco Ballesio * for transactions to drain.
5263432ff1e9SMarco Ballesio */
5264432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5265ae28c1beSMarco Ballesio target_proc->sync_recv = false;
5266ae28c1beSMarco Ballesio target_proc->async_recv = false;
5267432ff1e9SMarco Ballesio target_proc->is_frozen = true;
5268432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5269432ff1e9SMarco Ballesio
5270432ff1e9SMarco Ballesio if (info->timeout_ms > 0)
5271432ff1e9SMarco Ballesio ret = wait_event_interruptible_timeout(
5272432ff1e9SMarco Ballesio target_proc->freeze_wait,
5273432ff1e9SMarco Ballesio (!target_proc->outstanding_txns),
5274432ff1e9SMarco Ballesio msecs_to_jiffies(info->timeout_ms));
5275432ff1e9SMarco Ballesio
5276b564171aSLi Li /* Check pending transactions that wait for reply */
5277b564171aSLi Li if (ret >= 0) {
5278b564171aSLi Li binder_inner_proc_lock(target_proc);
5279b564171aSLi Li if (binder_txns_pending_ilocked(target_proc))
5280432ff1e9SMarco Ballesio ret = -EAGAIN;
5281b564171aSLi Li binder_inner_proc_unlock(target_proc);
5282b564171aSLi Li }
5283432ff1e9SMarco Ballesio
5284432ff1e9SMarco Ballesio if (ret < 0) {
5285432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5286432ff1e9SMarco Ballesio target_proc->is_frozen = false;
5287432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5288432ff1e9SMarco Ballesio }
5289432ff1e9SMarco Ballesio
5290432ff1e9SMarco Ballesio return ret;
5291432ff1e9SMarco Ballesio }
5292432ff1e9SMarco Ballesio
binder_ioctl_get_freezer_info(struct binder_frozen_status_info * info)5293ae28c1beSMarco Ballesio static int binder_ioctl_get_freezer_info(
5294ae28c1beSMarco Ballesio struct binder_frozen_status_info *info)
5295ae28c1beSMarco Ballesio {
5296ae28c1beSMarco Ballesio struct binder_proc *target_proc;
5297ae28c1beSMarco Ballesio bool found = false;
5298b564171aSLi Li __u32 txns_pending;
5299ae28c1beSMarco Ballesio
5300ae28c1beSMarco Ballesio info->sync_recv = 0;
5301ae28c1beSMarco Ballesio info->async_recv = 0;
5302ae28c1beSMarco Ballesio
5303ae28c1beSMarco Ballesio mutex_lock(&binder_procs_lock);
5304ae28c1beSMarco Ballesio hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5305ae28c1beSMarco Ballesio if (target_proc->pid == info->pid) {
5306ae28c1beSMarco Ballesio found = true;
5307ae28c1beSMarco Ballesio binder_inner_proc_lock(target_proc);
5308b564171aSLi Li txns_pending = binder_txns_pending_ilocked(target_proc);
5309b564171aSLi Li info->sync_recv |= target_proc->sync_recv |
5310b564171aSLi Li (txns_pending << 1);
5311ae28c1beSMarco Ballesio info->async_recv |= target_proc->async_recv;
5312ae28c1beSMarco Ballesio binder_inner_proc_unlock(target_proc);
5313ae28c1beSMarco Ballesio }
5314ae28c1beSMarco Ballesio }
5315ae28c1beSMarco Ballesio mutex_unlock(&binder_procs_lock);
5316ae28c1beSMarco Ballesio
5317ae28c1beSMarco Ballesio if (!found)
5318ae28c1beSMarco Ballesio return -EINVAL;
5319ae28c1beSMarco Ballesio
5320ae28c1beSMarco Ballesio return 0;
5321ae28c1beSMarco Ballesio }
5322ae28c1beSMarco Ballesio
binder_ioctl_get_extended_error(struct binder_thread * thread,void __user * ubuf)5323bd32889eSCarlos Llamas static int binder_ioctl_get_extended_error(struct binder_thread *thread,
5324bd32889eSCarlos Llamas void __user *ubuf)
5325bd32889eSCarlos Llamas {
5326aed86f8aSSchspa Shi struct binder_extended_error ee;
5327bd32889eSCarlos Llamas
5328bd32889eSCarlos Llamas binder_inner_proc_lock(thread->proc);
5329aed86f8aSSchspa Shi ee = thread->ee;
5330aed86f8aSSchspa Shi binder_set_extended_error(&thread->ee, 0, BR_OK, 0);
5331bd32889eSCarlos Llamas binder_inner_proc_unlock(thread->proc);
5332bd32889eSCarlos Llamas
5333aed86f8aSSchspa Shi if (copy_to_user(ubuf, &ee, sizeof(ee)))
5334aed86f8aSSchspa Shi return -EFAULT;
5335bd32889eSCarlos Llamas
5336bd32889eSCarlos Llamas return 0;
5337bd32889eSCarlos Llamas }
5338bd32889eSCarlos Llamas
binder_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)5339777783e0SGreg Kroah-Hartman static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5340777783e0SGreg Kroah-Hartman {
5341777783e0SGreg Kroah-Hartman int ret;
5342777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5343777783e0SGreg Kroah-Hartman struct binder_thread *thread;
5344777783e0SGreg Kroah-Hartman void __user *ubuf = (void __user *)arg;
5345777783e0SGreg Kroah-Hartman
5346777783e0SGreg Kroah-Hartman /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5347777783e0SGreg Kroah-Hartman proc->pid, current->pid, cmd, arg);*/
5348777783e0SGreg Kroah-Hartman
53494175e2b4SSherry Yang binder_selftest_alloc(&proc->alloc);
53504175e2b4SSherry Yang
5351777783e0SGreg Kroah-Hartman trace_binder_ioctl(cmd, arg);
5352777783e0SGreg Kroah-Hartman
5353777783e0SGreg Kroah-Hartman ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5354777783e0SGreg Kroah-Hartman if (ret)
5355777783e0SGreg Kroah-Hartman goto err_unlocked;
5356777783e0SGreg Kroah-Hartman
5357777783e0SGreg Kroah-Hartman thread = binder_get_thread(proc);
5358777783e0SGreg Kroah-Hartman if (thread == NULL) {
5359777783e0SGreg Kroah-Hartman ret = -ENOMEM;
5360777783e0SGreg Kroah-Hartman goto err;
5361777783e0SGreg Kroah-Hartman }
5362777783e0SGreg Kroah-Hartman
5363777783e0SGreg Kroah-Hartman switch (cmd) {
5364777783e0SGreg Kroah-Hartman case BINDER_WRITE_READ:
53657feb35bcSJiazi.Li ret = binder_ioctl_write_read(filp, arg, thread);
5366777783e0SGreg Kroah-Hartman if (ret)
5367777783e0SGreg Kroah-Hartman goto err;
5368777783e0SGreg Kroah-Hartman break;
5369b3e68612STodd Kjos case BINDER_SET_MAX_THREADS: {
537071df2cb2SCarlos Llamas u32 max_threads;
5371b3e68612STodd Kjos
5372b3e68612STodd Kjos if (copy_from_user(&max_threads, ubuf,
5373b3e68612STodd Kjos sizeof(max_threads))) {
5374777783e0SGreg Kroah-Hartman ret = -EINVAL;
5375777783e0SGreg Kroah-Hartman goto err;
5376777783e0SGreg Kroah-Hartman }
5377b3e68612STodd Kjos binder_inner_proc_lock(proc);
5378b3e68612STodd Kjos proc->max_threads = max_threads;
5379b3e68612STodd Kjos binder_inner_proc_unlock(proc);
5380777783e0SGreg Kroah-Hartman break;
5381b3e68612STodd Kjos }
5382ec74136dSTodd Kjos case BINDER_SET_CONTEXT_MGR_EXT: {
5383ec74136dSTodd Kjos struct flat_binder_object fbo;
5384ec74136dSTodd Kjos
5385ec74136dSTodd Kjos if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5386ec74136dSTodd Kjos ret = -EINVAL;
5387ec74136dSTodd Kjos goto err;
5388ec74136dSTodd Kjos }
5389ec74136dSTodd Kjos ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5390ec74136dSTodd Kjos if (ret)
5391ec74136dSTodd Kjos goto err;
5392ec74136dSTodd Kjos break;
5393ec74136dSTodd Kjos }
5394777783e0SGreg Kroah-Hartman case BINDER_SET_CONTEXT_MGR:
5395ec74136dSTodd Kjos ret = binder_ioctl_set_ctx_mgr(filp, NULL);
5396777783e0SGreg Kroah-Hartman if (ret)
5397777783e0SGreg Kroah-Hartman goto err;
5398777783e0SGreg Kroah-Hartman break;
5399777783e0SGreg Kroah-Hartman case BINDER_THREAD_EXIT:
5400777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
5401777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
54027a4408c6STodd Kjos binder_thread_release(proc, thread);
5403777783e0SGreg Kroah-Hartman thread = NULL;
5404777783e0SGreg Kroah-Hartman break;
5405777783e0SGreg Kroah-Hartman case BINDER_VERSION: {
5406777783e0SGreg Kroah-Hartman struct binder_version __user *ver = ubuf;
5407777783e0SGreg Kroah-Hartman
5408777783e0SGreg Kroah-Hartman if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5409777783e0SGreg Kroah-Hartman &ver->protocol_version)) {
5410777783e0SGreg Kroah-Hartman ret = -EINVAL;
5411777783e0SGreg Kroah-Hartman goto err;
5412777783e0SGreg Kroah-Hartman }
5413777783e0SGreg Kroah-Hartman break;
5414777783e0SGreg Kroah-Hartman }
5415b7e6a896SMartijn Coenen case BINDER_GET_NODE_INFO_FOR_REF: {
5416b7e6a896SMartijn Coenen struct binder_node_info_for_ref info;
5417b7e6a896SMartijn Coenen
5418b7e6a896SMartijn Coenen if (copy_from_user(&info, ubuf, sizeof(info))) {
5419b7e6a896SMartijn Coenen ret = -EFAULT;
5420b7e6a896SMartijn Coenen goto err;
5421b7e6a896SMartijn Coenen }
5422b7e6a896SMartijn Coenen
5423b7e6a896SMartijn Coenen ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5424b7e6a896SMartijn Coenen if (ret < 0)
5425b7e6a896SMartijn Coenen goto err;
5426b7e6a896SMartijn Coenen
5427b7e6a896SMartijn Coenen if (copy_to_user(ubuf, &info, sizeof(info))) {
5428b7e6a896SMartijn Coenen ret = -EFAULT;
5429b7e6a896SMartijn Coenen goto err;
5430b7e6a896SMartijn Coenen }
5431b7e6a896SMartijn Coenen
5432b7e6a896SMartijn Coenen break;
5433b7e6a896SMartijn Coenen }
5434abcc6153SColin Cross case BINDER_GET_NODE_DEBUG_INFO: {
5435abcc6153SColin Cross struct binder_node_debug_info info;
5436abcc6153SColin Cross
5437abcc6153SColin Cross if (copy_from_user(&info, ubuf, sizeof(info))) {
5438abcc6153SColin Cross ret = -EFAULT;
5439abcc6153SColin Cross goto err;
5440abcc6153SColin Cross }
5441abcc6153SColin Cross
5442abcc6153SColin Cross ret = binder_ioctl_get_node_debug_info(proc, &info);
5443abcc6153SColin Cross if (ret < 0)
5444abcc6153SColin Cross goto err;
5445abcc6153SColin Cross
5446abcc6153SColin Cross if (copy_to_user(ubuf, &info, sizeof(info))) {
5447abcc6153SColin Cross ret = -EFAULT;
5448abcc6153SColin Cross goto err;
5449abcc6153SColin Cross }
5450abcc6153SColin Cross break;
5451abcc6153SColin Cross }
5452432ff1e9SMarco Ballesio case BINDER_FREEZE: {
5453432ff1e9SMarco Ballesio struct binder_freeze_info info;
5454432ff1e9SMarco Ballesio struct binder_proc **target_procs = NULL, *target_proc;
5455432ff1e9SMarco Ballesio int target_procs_count = 0, i = 0;
5456432ff1e9SMarco Ballesio
5457432ff1e9SMarco Ballesio ret = 0;
5458432ff1e9SMarco Ballesio
5459432ff1e9SMarco Ballesio if (copy_from_user(&info, ubuf, sizeof(info))) {
5460432ff1e9SMarco Ballesio ret = -EFAULT;
5461432ff1e9SMarco Ballesio goto err;
5462432ff1e9SMarco Ballesio }
5463432ff1e9SMarco Ballesio
5464432ff1e9SMarco Ballesio mutex_lock(&binder_procs_lock);
5465432ff1e9SMarco Ballesio hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5466432ff1e9SMarco Ballesio if (target_proc->pid == info.pid)
5467432ff1e9SMarco Ballesio target_procs_count++;
5468432ff1e9SMarco Ballesio }
5469432ff1e9SMarco Ballesio
5470432ff1e9SMarco Ballesio if (target_procs_count == 0) {
5471432ff1e9SMarco Ballesio mutex_unlock(&binder_procs_lock);
5472432ff1e9SMarco Ballesio ret = -EINVAL;
5473432ff1e9SMarco Ballesio goto err;
5474432ff1e9SMarco Ballesio }
5475432ff1e9SMarco Ballesio
5476432ff1e9SMarco Ballesio target_procs = kcalloc(target_procs_count,
5477432ff1e9SMarco Ballesio sizeof(struct binder_proc *),
5478432ff1e9SMarco Ballesio GFP_KERNEL);
5479432ff1e9SMarco Ballesio
5480432ff1e9SMarco Ballesio if (!target_procs) {
5481432ff1e9SMarco Ballesio mutex_unlock(&binder_procs_lock);
5482432ff1e9SMarco Ballesio ret = -ENOMEM;
5483432ff1e9SMarco Ballesio goto err;
5484432ff1e9SMarco Ballesio }
5485432ff1e9SMarco Ballesio
5486432ff1e9SMarco Ballesio hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5487432ff1e9SMarco Ballesio if (target_proc->pid != info.pid)
5488432ff1e9SMarco Ballesio continue;
5489432ff1e9SMarco Ballesio
5490432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5491432ff1e9SMarco Ballesio target_proc->tmp_ref++;
5492432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5493432ff1e9SMarco Ballesio
5494432ff1e9SMarco Ballesio target_procs[i++] = target_proc;
5495432ff1e9SMarco Ballesio }
5496432ff1e9SMarco Ballesio mutex_unlock(&binder_procs_lock);
5497432ff1e9SMarco Ballesio
5498432ff1e9SMarco Ballesio for (i = 0; i < target_procs_count; i++) {
5499432ff1e9SMarco Ballesio if (ret >= 0)
5500432ff1e9SMarco Ballesio ret = binder_ioctl_freeze(&info,
5501432ff1e9SMarco Ballesio target_procs[i]);
5502432ff1e9SMarco Ballesio
5503432ff1e9SMarco Ballesio binder_proc_dec_tmpref(target_procs[i]);
5504432ff1e9SMarco Ballesio }
5505432ff1e9SMarco Ballesio
5506432ff1e9SMarco Ballesio kfree(target_procs);
5507432ff1e9SMarco Ballesio
5508432ff1e9SMarco Ballesio if (ret < 0)
5509432ff1e9SMarco Ballesio goto err;
5510432ff1e9SMarco Ballesio break;
5511432ff1e9SMarco Ballesio }
5512ae28c1beSMarco Ballesio case BINDER_GET_FROZEN_INFO: {
5513ae28c1beSMarco Ballesio struct binder_frozen_status_info info;
5514ae28c1beSMarco Ballesio
5515ae28c1beSMarco Ballesio if (copy_from_user(&info, ubuf, sizeof(info))) {
5516ae28c1beSMarco Ballesio ret = -EFAULT;
5517ae28c1beSMarco Ballesio goto err;
5518ae28c1beSMarco Ballesio }
5519ae28c1beSMarco Ballesio
5520ae28c1beSMarco Ballesio ret = binder_ioctl_get_freezer_info(&info);
5521ae28c1beSMarco Ballesio if (ret < 0)
5522ae28c1beSMarco Ballesio goto err;
5523ae28c1beSMarco Ballesio
5524ae28c1beSMarco Ballesio if (copy_to_user(ubuf, &info, sizeof(info))) {
5525ae28c1beSMarco Ballesio ret = -EFAULT;
5526ae28c1beSMarco Ballesio goto err;
5527ae28c1beSMarco Ballesio }
5528ae28c1beSMarco Ballesio break;
5529ae28c1beSMarco Ballesio }
5530a7dc1e6fSHang Lu case BINDER_ENABLE_ONEWAY_SPAM_DETECTION: {
5531a7dc1e6fSHang Lu uint32_t enable;
5532a7dc1e6fSHang Lu
5533a7dc1e6fSHang Lu if (copy_from_user(&enable, ubuf, sizeof(enable))) {
5534ced081a4SLuca Stefani ret = -EFAULT;
5535a7dc1e6fSHang Lu goto err;
5536a7dc1e6fSHang Lu }
5537a7dc1e6fSHang Lu binder_inner_proc_lock(proc);
5538a7dc1e6fSHang Lu proc->oneway_spam_detection_enabled = (bool)enable;
5539a7dc1e6fSHang Lu binder_inner_proc_unlock(proc);
5540a7dc1e6fSHang Lu break;
5541a7dc1e6fSHang Lu }
5542bd32889eSCarlos Llamas case BINDER_GET_EXTENDED_ERROR:
5543bd32889eSCarlos Llamas ret = binder_ioctl_get_extended_error(thread, ubuf);
5544bd32889eSCarlos Llamas if (ret < 0)
5545bd32889eSCarlos Llamas goto err;
5546bd32889eSCarlos Llamas break;
5547777783e0SGreg Kroah-Hartman default:
5548777783e0SGreg Kroah-Hartman ret = -EINVAL;
5549777783e0SGreg Kroah-Hartman goto err;
5550777783e0SGreg Kroah-Hartman }
5551777783e0SGreg Kroah-Hartman ret = 0;
5552777783e0SGreg Kroah-Hartman err:
5553777783e0SGreg Kroah-Hartman if (thread)
555408dabceeSTodd Kjos thread->looper_need_return = false;
5555777783e0SGreg Kroah-Hartman wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
555695c16f9dSMarco Ballesio if (ret && ret != -EINTR)
5557777783e0SGreg Kroah-Hartman pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
5558777783e0SGreg Kroah-Hartman err_unlocked:
5559777783e0SGreg Kroah-Hartman trace_binder_ioctl_done(ret);
5560777783e0SGreg Kroah-Hartman return ret;
5561777783e0SGreg Kroah-Hartman }
5562777783e0SGreg Kroah-Hartman
binder_vma_open(struct vm_area_struct * vma)5563777783e0SGreg Kroah-Hartman static void binder_vma_open(struct vm_area_struct *vma)
5564777783e0SGreg Kroah-Hartman {
5565777783e0SGreg Kroah-Hartman struct binder_proc *proc = vma->vm_private_data;
5566777783e0SGreg Kroah-Hartman
5567777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5568777783e0SGreg Kroah-Hartman "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5569777783e0SGreg Kroah-Hartman proc->pid, vma->vm_start, vma->vm_end,
5570777783e0SGreg Kroah-Hartman (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5571777783e0SGreg Kroah-Hartman (unsigned long)pgprot_val(vma->vm_page_prot));
5572777783e0SGreg Kroah-Hartman }
5573777783e0SGreg Kroah-Hartman
binder_vma_close(struct vm_area_struct * vma)5574777783e0SGreg Kroah-Hartman static void binder_vma_close(struct vm_area_struct *vma)
5575777783e0SGreg Kroah-Hartman {
5576777783e0SGreg Kroah-Hartman struct binder_proc *proc = vma->vm_private_data;
5577777783e0SGreg Kroah-Hartman
5578777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5579777783e0SGreg Kroah-Hartman "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5580777783e0SGreg Kroah-Hartman proc->pid, vma->vm_start, vma->vm_end,
5581777783e0SGreg Kroah-Hartman (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5582777783e0SGreg Kroah-Hartman (unsigned long)pgprot_val(vma->vm_page_prot));
558319c98724STodd Kjos binder_alloc_vma_close(&proc->alloc);
5584777783e0SGreg Kroah-Hartman }
5585777783e0SGreg Kroah-Hartman
binder_vm_fault(struct vm_fault * vmf)5586e19f70aaSSouptick Joarder static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
5587777783e0SGreg Kroah-Hartman {
5588777783e0SGreg Kroah-Hartman return VM_FAULT_SIGBUS;
5589777783e0SGreg Kroah-Hartman }
5590777783e0SGreg Kroah-Hartman
55917cbea8dcSKirill A. Shutemov static const struct vm_operations_struct binder_vm_ops = {
5592777783e0SGreg Kroah-Hartman .open = binder_vma_open,
5593777783e0SGreg Kroah-Hartman .close = binder_vma_close,
5594777783e0SGreg Kroah-Hartman .fault = binder_vm_fault,
5595777783e0SGreg Kroah-Hartman };
5596777783e0SGreg Kroah-Hartman
binder_mmap(struct file * filp,struct vm_area_struct * vma)559719c98724STodd Kjos static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
559819c98724STodd Kjos {
559919c98724STodd Kjos struct binder_proc *proc = filp->private_data;
560019c98724STodd Kjos
560119c98724STodd Kjos if (proc->tsk != current->group_leader)
560219c98724STodd Kjos return -EINVAL;
560319c98724STodd Kjos
560419c98724STodd Kjos binder_debug(BINDER_DEBUG_OPEN_CLOSE,
560519c98724STodd Kjos "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
560619c98724STodd Kjos __func__, proc->pid, vma->vm_start, vma->vm_end,
560719c98724STodd Kjos (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
560819c98724STodd Kjos (unsigned long)pgprot_val(vma->vm_page_prot));
560919c98724STodd Kjos
561019c98724STodd Kjos if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
56112a3809daSLiu Shixin pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
56122a3809daSLiu Shixin proc->pid, vma->vm_start, vma->vm_end, "bad vm_flags", -EPERM);
56132a3809daSLiu Shixin return -EPERM;
561419c98724STodd Kjos }
56151c71222eSSuren Baghdasaryan vm_flags_mod(vma, VM_DONTCOPY | VM_MIXEDMAP, VM_MAYWRITE);
5616720c2419SMinchan Kim
561719c98724STodd Kjos vma->vm_ops = &binder_vm_ops;
561819c98724STodd Kjos vma->vm_private_data = proc;
561919c98724STodd Kjos
56202a3809daSLiu Shixin return binder_alloc_mmap_handler(&proc->alloc, vma);
5621777783e0SGreg Kroah-Hartman }
5622777783e0SGreg Kroah-Hartman
binder_open(struct inode * nodp,struct file * filp)5623777783e0SGreg Kroah-Hartman static int binder_open(struct inode *nodp, struct file *filp)
5624777783e0SGreg Kroah-Hartman {
5625eb143f87SMartin Fuzzey struct binder_proc *proc, *itr;
5626ac4812c5SMartijn Coenen struct binder_device *binder_dev;
56274feb80faSHridya Valsaraju struct binderfs_info *info;
56284feb80faSHridya Valsaraju struct dentry *binder_binderfs_dir_entry_proc = NULL;
5629eb143f87SMartin Fuzzey bool existing_pid = false;
5630777783e0SGreg Kroah-Hartman
563100c41cddSElad Wexler binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
5632777783e0SGreg Kroah-Hartman current->group_leader->pid, current->pid);
5633777783e0SGreg Kroah-Hartman
5634777783e0SGreg Kroah-Hartman proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5635777783e0SGreg Kroah-Hartman if (proc == NULL)
5636777783e0SGreg Kroah-Hartman return -ENOMEM;
56379630fe88STodd Kjos spin_lock_init(&proc->inner_lock);
56389630fe88STodd Kjos spin_lock_init(&proc->outer_lock);
5639c4ea41baSTodd Kjos get_task_struct(current->group_leader);
5640c4ea41baSTodd Kjos proc->tsk = current->group_leader;
564129bc22acSTodd Kjos proc->cred = get_cred(filp->f_cred);
5642777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&proc->todo);
5643432ff1e9SMarco Ballesio init_waitqueue_head(&proc->freeze_wait);
5644777783e0SGreg Kroah-Hartman proc->default_priority = task_nice(current);
56453ad20fe3SChristian Brauner /* binderfs stashes devices in i_private */
56464feb80faSHridya Valsaraju if (is_binderfs_device(nodp)) {
5647f0fe2c0fSChristian Brauner binder_dev = nodp->i_private;
56484feb80faSHridya Valsaraju info = nodp->i_sb->s_fs_info;
56494feb80faSHridya Valsaraju binder_binderfs_dir_entry_proc = info->proc_log_dir;
56504feb80faSHridya Valsaraju } else {
56513ad20fe3SChristian Brauner binder_dev = container_of(filp->private_data,
56523ad20fe3SChristian Brauner struct binder_device, miscdev);
56534feb80faSHridya Valsaraju }
5654f0fe2c0fSChristian Brauner refcount_inc(&binder_dev->ref);
5655ac4812c5SMartijn Coenen proc->context = &binder_dev->context;
565619c98724STodd Kjos binder_alloc_init(&proc->alloc);
5657777783e0SGreg Kroah-Hartman
5658777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_PROC);
5659777783e0SGreg Kroah-Hartman proc->pid = current->group_leader->pid;
5660777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&proc->delivered_death);
56611b77e9dcSMartijn Coenen INIT_LIST_HEAD(&proc->waiting_threads);
5662777783e0SGreg Kroah-Hartman filp->private_data = proc;
5663777783e0SGreg Kroah-Hartman
5664c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
5665eb143f87SMartin Fuzzey hlist_for_each_entry(itr, &binder_procs, proc_node) {
5666eb143f87SMartin Fuzzey if (itr->pid == proc->pid) {
5667eb143f87SMartin Fuzzey existing_pid = true;
5668eb143f87SMartin Fuzzey break;
5669eb143f87SMartin Fuzzey }
5670eb143f87SMartin Fuzzey }
5671c44b1231STodd Kjos hlist_add_head(&proc->proc_node, &binder_procs);
5672c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
5673c44b1231STodd Kjos
5674eb143f87SMartin Fuzzey if (binder_debugfs_dir_entry_proc && !existing_pid) {
5675777783e0SGreg Kroah-Hartman char strbuf[11];
5676777783e0SGreg Kroah-Hartman
5677777783e0SGreg Kroah-Hartman snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
567814db3181SMartijn Coenen /*
5679eb143f87SMartin Fuzzey * proc debug entries are shared between contexts.
5680eb143f87SMartin Fuzzey * Only create for the first PID to avoid debugfs log spamming
5681eb143f87SMartin Fuzzey * The printing code will anyway print all contexts for a given
5682eb143f87SMartin Fuzzey * PID so this is not a problem.
568314db3181SMartijn Coenen */
568421d02ddfSHarsh Shandilya proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
568514db3181SMartijn Coenen binder_debugfs_dir_entry_proc,
568614db3181SMartijn Coenen (void *)(unsigned long)proc->pid,
5687c13e0a52SYangtao Li &proc_fops);
5688777783e0SGreg Kroah-Hartman }
5689777783e0SGreg Kroah-Hartman
5690eb143f87SMartin Fuzzey if (binder_binderfs_dir_entry_proc && !existing_pid) {
56914feb80faSHridya Valsaraju char strbuf[11];
56924feb80faSHridya Valsaraju struct dentry *binderfs_entry;
56934feb80faSHridya Valsaraju
56944feb80faSHridya Valsaraju snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
56954feb80faSHridya Valsaraju /*
56964feb80faSHridya Valsaraju * Similar to debugfs, the process specific log file is shared
5697eb143f87SMartin Fuzzey * between contexts. Only create for the first PID.
5698eb143f87SMartin Fuzzey * This is ok since same as debugfs, the log file will contain
5699eb143f87SMartin Fuzzey * information on all contexts of a given PID.
57004feb80faSHridya Valsaraju */
57014feb80faSHridya Valsaraju binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
57024feb80faSHridya Valsaraju strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
57034feb80faSHridya Valsaraju if (!IS_ERR(binderfs_entry)) {
57044feb80faSHridya Valsaraju proc->binderfs_entry = binderfs_entry;
57054feb80faSHridya Valsaraju } else {
57064feb80faSHridya Valsaraju int error;
57074feb80faSHridya Valsaraju
57084feb80faSHridya Valsaraju error = PTR_ERR(binderfs_entry);
57094feb80faSHridya Valsaraju pr_warn("Unable to create file %s in binderfs (error %d)\n",
57104feb80faSHridya Valsaraju strbuf, error);
57114feb80faSHridya Valsaraju }
57124feb80faSHridya Valsaraju }
57134feb80faSHridya Valsaraju
5714777783e0SGreg Kroah-Hartman return 0;
5715777783e0SGreg Kroah-Hartman }
5716777783e0SGreg Kroah-Hartman
binder_flush(struct file * filp,fl_owner_t id)5717777783e0SGreg Kroah-Hartman static int binder_flush(struct file *filp, fl_owner_t id)
5718777783e0SGreg Kroah-Hartman {
5719777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5720777783e0SGreg Kroah-Hartman
5721777783e0SGreg Kroah-Hartman binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5722777783e0SGreg Kroah-Hartman
5723777783e0SGreg Kroah-Hartman return 0;
5724777783e0SGreg Kroah-Hartman }
5725777783e0SGreg Kroah-Hartman
binder_deferred_flush(struct binder_proc * proc)5726777783e0SGreg Kroah-Hartman static void binder_deferred_flush(struct binder_proc *proc)
5727777783e0SGreg Kroah-Hartman {
5728777783e0SGreg Kroah-Hartman struct rb_node *n;
5729777783e0SGreg Kroah-Hartman int wake_count = 0;
5730777783e0SGreg Kroah-Hartman
57317bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
5732777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5733777783e0SGreg Kroah-Hartman struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
5734777783e0SGreg Kroah-Hartman
573508dabceeSTodd Kjos thread->looper_need_return = true;
5736777783e0SGreg Kroah-Hartman if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5737777783e0SGreg Kroah-Hartman wake_up_interruptible(&thread->wait);
5738777783e0SGreg Kroah-Hartman wake_count++;
5739777783e0SGreg Kroah-Hartman }
5740777783e0SGreg Kroah-Hartman }
57417bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
5742777783e0SGreg Kroah-Hartman
5743777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5744777783e0SGreg Kroah-Hartman "binder_flush: %d woke %d threads\n", proc->pid,
5745777783e0SGreg Kroah-Hartman wake_count);
5746777783e0SGreg Kroah-Hartman }
5747777783e0SGreg Kroah-Hartman
binder_release(struct inode * nodp,struct file * filp)5748777783e0SGreg Kroah-Hartman static int binder_release(struct inode *nodp, struct file *filp)
5749777783e0SGreg Kroah-Hartman {
5750777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5751777783e0SGreg Kroah-Hartman
5752777783e0SGreg Kroah-Hartman debugfs_remove(proc->debugfs_entry);
57534feb80faSHridya Valsaraju
57544feb80faSHridya Valsaraju if (proc->binderfs_entry) {
57554feb80faSHridya Valsaraju binderfs_remove_file(proc->binderfs_entry);
57564feb80faSHridya Valsaraju proc->binderfs_entry = NULL;
57574feb80faSHridya Valsaraju }
57584feb80faSHridya Valsaraju
5759777783e0SGreg Kroah-Hartman binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5760777783e0SGreg Kroah-Hartman
5761777783e0SGreg Kroah-Hartman return 0;
5762777783e0SGreg Kroah-Hartman }
5763777783e0SGreg Kroah-Hartman
binder_node_release(struct binder_node * node,int refs)5764777783e0SGreg Kroah-Hartman static int binder_node_release(struct binder_node *node, int refs)
5765777783e0SGreg Kroah-Hartman {
5766777783e0SGreg Kroah-Hartman struct binder_ref *ref;
5767777783e0SGreg Kroah-Hartman int death = 0;
5768ed29721eSTodd Kjos struct binder_proc *proc = node->proc;
5769777783e0SGreg Kroah-Hartman
577072196393STodd Kjos binder_release_work(proc, &node->async_todo);
5771ed29721eSTodd Kjos
5772673068eeSTodd Kjos binder_node_lock(node);
5773ed29721eSTodd Kjos binder_inner_proc_lock(proc);
577472196393STodd Kjos binder_dequeue_work_ilocked(&node->work);
5775adc18842STodd Kjos /*
5776adc18842STodd Kjos * The caller must have taken a temporary ref on the node,
5777adc18842STodd Kjos */
5778adc18842STodd Kjos BUG_ON(!node->tmp_refs);
5779adc18842STodd Kjos if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
5780ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
5781673068eeSTodd Kjos binder_node_unlock(node);
5782ed29721eSTodd Kjos binder_free_node(node);
5783777783e0SGreg Kroah-Hartman
5784777783e0SGreg Kroah-Hartman return refs;
5785777783e0SGreg Kroah-Hartman }
5786777783e0SGreg Kroah-Hartman
5787777783e0SGreg Kroah-Hartman node->proc = NULL;
5788777783e0SGreg Kroah-Hartman node->local_strong_refs = 0;
5789777783e0SGreg Kroah-Hartman node->local_weak_refs = 0;
5790ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
5791c44b1231STodd Kjos
5792c44b1231STodd Kjos spin_lock(&binder_dead_nodes_lock);
5793777783e0SGreg Kroah-Hartman hlist_add_head(&node->dead_node, &binder_dead_nodes);
5794c44b1231STodd Kjos spin_unlock(&binder_dead_nodes_lock);
5795777783e0SGreg Kroah-Hartman
5796777783e0SGreg Kroah-Hartman hlist_for_each_entry(ref, &node->refs, node_entry) {
5797777783e0SGreg Kroah-Hartman refs++;
5798ab51ec6bSMartijn Coenen /*
5799ab51ec6bSMartijn Coenen * Need the node lock to synchronize
5800ab51ec6bSMartijn Coenen * with new notification requests and the
5801ab51ec6bSMartijn Coenen * inner lock to synchronize with queued
5802ab51ec6bSMartijn Coenen * death notifications.
5803ab51ec6bSMartijn Coenen */
5804ab51ec6bSMartijn Coenen binder_inner_proc_lock(ref->proc);
5805ab51ec6bSMartijn Coenen if (!ref->death) {
5806ab51ec6bSMartijn Coenen binder_inner_proc_unlock(ref->proc);
5807777783e0SGreg Kroah-Hartman continue;
5808ab51ec6bSMartijn Coenen }
5809777783e0SGreg Kroah-Hartman
5810777783e0SGreg Kroah-Hartman death++;
5811777783e0SGreg Kroah-Hartman
5812ab51ec6bSMartijn Coenen BUG_ON(!list_empty(&ref->death->work.entry));
5813777783e0SGreg Kroah-Hartman ref->death->work.type = BINDER_WORK_DEAD_BINDER;
581472196393STodd Kjos binder_enqueue_work_ilocked(&ref->death->work,
5815777783e0SGreg Kroah-Hartman &ref->proc->todo);
5816408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(ref->proc);
581772196393STodd Kjos binder_inner_proc_unlock(ref->proc);
5818777783e0SGreg Kroah-Hartman }
5819777783e0SGreg Kroah-Hartman
5820777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
5821777783e0SGreg Kroah-Hartman "node %d now dead, refs %d, death %d\n",
5822777783e0SGreg Kroah-Hartman node->debug_id, refs, death);
5823673068eeSTodd Kjos binder_node_unlock(node);
5824adc18842STodd Kjos binder_put_node(node);
5825777783e0SGreg Kroah-Hartman
5826777783e0SGreg Kroah-Hartman return refs;
5827777783e0SGreg Kroah-Hartman }
5828777783e0SGreg Kroah-Hartman
binder_deferred_release(struct binder_proc * proc)5829777783e0SGreg Kroah-Hartman static void binder_deferred_release(struct binder_proc *proc)
5830777783e0SGreg Kroah-Hartman {
5831342e5c90SMartijn Coenen struct binder_context *context = proc->context;
5832777783e0SGreg Kroah-Hartman struct rb_node *n;
583319c98724STodd Kjos int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
5834777783e0SGreg Kroah-Hartman
5835c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
5836777783e0SGreg Kroah-Hartman hlist_del(&proc->proc_node);
5837c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
5838777783e0SGreg Kroah-Hartman
5839c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
5840342e5c90SMartijn Coenen if (context->binder_context_mgr_node &&
5841342e5c90SMartijn Coenen context->binder_context_mgr_node->proc == proc) {
5842777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
5843777783e0SGreg Kroah-Hartman "%s: %d context_mgr_node gone\n",
5844777783e0SGreg Kroah-Hartman __func__, proc->pid);
5845342e5c90SMartijn Coenen context->binder_context_mgr_node = NULL;
5846777783e0SGreg Kroah-Hartman }
5847c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
58487bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
58497a4408c6STodd Kjos /*
58507a4408c6STodd Kjos * Make sure proc stays alive after we
58517a4408c6STodd Kjos * remove all the threads
58527a4408c6STodd Kjos */
58537a4408c6STodd Kjos proc->tmp_ref++;
5854777783e0SGreg Kroah-Hartman
58557a4408c6STodd Kjos proc->is_dead = true;
5856432ff1e9SMarco Ballesio proc->is_frozen = false;
5857ae28c1beSMarco Ballesio proc->sync_recv = false;
5858ae28c1beSMarco Ballesio proc->async_recv = false;
5859777783e0SGreg Kroah-Hartman threads = 0;
5860777783e0SGreg Kroah-Hartman active_transactions = 0;
5861777783e0SGreg Kroah-Hartman while ((n = rb_first(&proc->threads))) {
5862777783e0SGreg Kroah-Hartman struct binder_thread *thread;
5863777783e0SGreg Kroah-Hartman
5864777783e0SGreg Kroah-Hartman thread = rb_entry(n, struct binder_thread, rb_node);
58657bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
5866777783e0SGreg Kroah-Hartman threads++;
58677a4408c6STodd Kjos active_transactions += binder_thread_release(proc, thread);
58687bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
5869777783e0SGreg Kroah-Hartman }
5870777783e0SGreg Kroah-Hartman
5871777783e0SGreg Kroah-Hartman nodes = 0;
5872777783e0SGreg Kroah-Hartman incoming_refs = 0;
5873777783e0SGreg Kroah-Hartman while ((n = rb_first(&proc->nodes))) {
5874777783e0SGreg Kroah-Hartman struct binder_node *node;
5875777783e0SGreg Kroah-Hartman
5876777783e0SGreg Kroah-Hartman node = rb_entry(n, struct binder_node, rb_node);
5877777783e0SGreg Kroah-Hartman nodes++;
5878adc18842STodd Kjos /*
5879adc18842STodd Kjos * take a temporary ref on the node before
5880adc18842STodd Kjos * calling binder_node_release() which will either
5881adc18842STodd Kjos * kfree() the node or call binder_put_node()
5882adc18842STodd Kjos */
5883da0fa9e4STodd Kjos binder_inc_node_tmpref_ilocked(node);
5884777783e0SGreg Kroah-Hartman rb_erase(&node->rb_node, &proc->nodes);
5885da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
5886777783e0SGreg Kroah-Hartman incoming_refs = binder_node_release(node, incoming_refs);
5887da0fa9e4STodd Kjos binder_inner_proc_lock(proc);
5888777783e0SGreg Kroah-Hartman }
5889da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
5890777783e0SGreg Kroah-Hartman
5891777783e0SGreg Kroah-Hartman outgoing_refs = 0;
58922c1838dcSTodd Kjos binder_proc_lock(proc);
5893777783e0SGreg Kroah-Hartman while ((n = rb_first(&proc->refs_by_desc))) {
5894777783e0SGreg Kroah-Hartman struct binder_ref *ref;
5895777783e0SGreg Kroah-Hartman
5896777783e0SGreg Kroah-Hartman ref = rb_entry(n, struct binder_ref, rb_node_desc);
5897777783e0SGreg Kroah-Hartman outgoing_refs++;
58982c1838dcSTodd Kjos binder_cleanup_ref_olocked(ref);
58992c1838dcSTodd Kjos binder_proc_unlock(proc);
5900372e3147STodd Kjos binder_free_ref(ref);
59012c1838dcSTodd Kjos binder_proc_lock(proc);
5902777783e0SGreg Kroah-Hartman }
59032c1838dcSTodd Kjos binder_proc_unlock(proc);
5904777783e0SGreg Kroah-Hartman
590572196393STodd Kjos binder_release_work(proc, &proc->todo);
590672196393STodd Kjos binder_release_work(proc, &proc->delivered_death);
5907777783e0SGreg Kroah-Hartman
5908777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
590919c98724STodd Kjos "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
5910777783e0SGreg Kroah-Hartman __func__, proc->pid, threads, nodes, incoming_refs,
591119c98724STodd Kjos outgoing_refs, active_transactions);
5912777783e0SGreg Kroah-Hartman
59137a4408c6STodd Kjos binder_proc_dec_tmpref(proc);
5914777783e0SGreg Kroah-Hartman }
5915777783e0SGreg Kroah-Hartman
binder_deferred_func(struct work_struct * work)5916777783e0SGreg Kroah-Hartman static void binder_deferred_func(struct work_struct *work)
5917777783e0SGreg Kroah-Hartman {
5918777783e0SGreg Kroah-Hartman struct binder_proc *proc;
5919777783e0SGreg Kroah-Hartman
5920777783e0SGreg Kroah-Hartman int defer;
5921777783e0SGreg Kroah-Hartman
5922777783e0SGreg Kroah-Hartman do {
5923777783e0SGreg Kroah-Hartman mutex_lock(&binder_deferred_lock);
5924777783e0SGreg Kroah-Hartman if (!hlist_empty(&binder_deferred_list)) {
5925777783e0SGreg Kroah-Hartman proc = hlist_entry(binder_deferred_list.first,
5926777783e0SGreg Kroah-Hartman struct binder_proc, deferred_work_node);
5927777783e0SGreg Kroah-Hartman hlist_del_init(&proc->deferred_work_node);
5928777783e0SGreg Kroah-Hartman defer = proc->deferred_work;
5929777783e0SGreg Kroah-Hartman proc->deferred_work = 0;
5930777783e0SGreg Kroah-Hartman } else {
5931777783e0SGreg Kroah-Hartman proc = NULL;
5932777783e0SGreg Kroah-Hartman defer = 0;
5933777783e0SGreg Kroah-Hartman }
5934777783e0SGreg Kroah-Hartman mutex_unlock(&binder_deferred_lock);
5935777783e0SGreg Kroah-Hartman
5936777783e0SGreg Kroah-Hartman if (defer & BINDER_DEFERRED_FLUSH)
5937777783e0SGreg Kroah-Hartman binder_deferred_flush(proc);
5938777783e0SGreg Kroah-Hartman
5939777783e0SGreg Kroah-Hartman if (defer & BINDER_DEFERRED_RELEASE)
5940777783e0SGreg Kroah-Hartman binder_deferred_release(proc); /* frees proc */
5941777783e0SGreg Kroah-Hartman } while (proc);
5942777783e0SGreg Kroah-Hartman }
5943777783e0SGreg Kroah-Hartman static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5944777783e0SGreg Kroah-Hartman
5945777783e0SGreg Kroah-Hartman static void
binder_defer_work(struct binder_proc * proc,enum binder_deferred_state defer)5946777783e0SGreg Kroah-Hartman binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5947777783e0SGreg Kroah-Hartman {
5948777783e0SGreg Kroah-Hartman mutex_lock(&binder_deferred_lock);
5949777783e0SGreg Kroah-Hartman proc->deferred_work |= defer;
5950777783e0SGreg Kroah-Hartman if (hlist_unhashed(&proc->deferred_work_node)) {
5951777783e0SGreg Kroah-Hartman hlist_add_head(&proc->deferred_work_node,
5952777783e0SGreg Kroah-Hartman &binder_deferred_list);
59531beba52dSBhaktipriya Shridhar schedule_work(&binder_deferred_work);
5954777783e0SGreg Kroah-Hartman }
5955777783e0SGreg Kroah-Hartman mutex_unlock(&binder_deferred_lock);
5956777783e0SGreg Kroah-Hartman }
5957777783e0SGreg Kroah-Hartman
print_binder_transaction_ilocked(struct seq_file * m,struct binder_proc * proc,const char * prefix,struct binder_transaction * t)59585f2f6369STodd Kjos static void print_binder_transaction_ilocked(struct seq_file *m,
59595f2f6369STodd Kjos struct binder_proc *proc,
59605f2f6369STodd Kjos const char *prefix,
5961777783e0SGreg Kroah-Hartman struct binder_transaction *t)
5962777783e0SGreg Kroah-Hartman {
59635f2f6369STodd Kjos struct binder_proc *to_proc;
59645f2f6369STodd Kjos struct binder_buffer *buffer = t->buffer;
596580093619SChuang Zhang ktime_t current_time = ktime_get();
59665f2f6369STodd Kjos
59677a4408c6STodd Kjos spin_lock(&t->lock);
59685f2f6369STodd Kjos to_proc = t->to_proc;
5969777783e0SGreg Kroah-Hartman seq_printf(m,
597080093619SChuang Zhang "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d elapsed %lldms",
5971777783e0SGreg Kroah-Hartman prefix, t->debug_id, t,
5972c21c0f9aSChuang Zhang t->from_pid,
5973c21c0f9aSChuang Zhang t->from_tid,
59745f2f6369STodd Kjos to_proc ? to_proc->pid : 0,
5975777783e0SGreg Kroah-Hartman t->to_thread ? t->to_thread->pid : 0,
597680093619SChuang Zhang t->code, t->flags, t->priority, t->need_reply,
597780093619SChuang Zhang ktime_ms_delta(current_time, t->start_time));
59787a4408c6STodd Kjos spin_unlock(&t->lock);
59797a4408c6STodd Kjos
59805f2f6369STodd Kjos if (proc != to_proc) {
59815f2f6369STodd Kjos /*
59825f2f6369STodd Kjos * Can only safely deref buffer if we are holding the
59835f2f6369STodd Kjos * correct proc inner lock for this node
59845f2f6369STodd Kjos */
59855f2f6369STodd Kjos seq_puts(m, "\n");
59865f2f6369STodd Kjos return;
59875f2f6369STodd Kjos }
59885f2f6369STodd Kjos
59895f2f6369STodd Kjos if (buffer == NULL) {
5990777783e0SGreg Kroah-Hartman seq_puts(m, " buffer free\n");
5991777783e0SGreg Kroah-Hartman return;
5992777783e0SGreg Kroah-Hartman }
59935f2f6369STodd Kjos if (buffer->target_node)
59945f2f6369STodd Kjos seq_printf(m, " node %d", buffer->target_node->debug_id);
59958ca86f16STodd Kjos seq_printf(m, " size %zd:%zd data %pK\n",
59965f2f6369STodd Kjos buffer->data_size, buffer->offsets_size,
5997bde4a19fSTodd Kjos buffer->user_data);
5998777783e0SGreg Kroah-Hartman }
5999777783e0SGreg Kroah-Hartman
print_binder_work_ilocked(struct seq_file * m,struct binder_proc * proc,const char * prefix,const char * transaction_prefix,struct binder_work * w)60005f2f6369STodd Kjos static void print_binder_work_ilocked(struct seq_file *m,
60015f2f6369STodd Kjos struct binder_proc *proc,
60025f2f6369STodd Kjos const char *prefix,
6003777783e0SGreg Kroah-Hartman const char *transaction_prefix,
6004777783e0SGreg Kroah-Hartman struct binder_work *w)
6005777783e0SGreg Kroah-Hartman {
6006777783e0SGreg Kroah-Hartman struct binder_node *node;
6007777783e0SGreg Kroah-Hartman struct binder_transaction *t;
6008777783e0SGreg Kroah-Hartman
6009777783e0SGreg Kroah-Hartman switch (w->type) {
6010777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION:
6011777783e0SGreg Kroah-Hartman t = container_of(w, struct binder_transaction, work);
60125f2f6369STodd Kjos print_binder_transaction_ilocked(
60135f2f6369STodd Kjos m, proc, transaction_prefix, t);
6014777783e0SGreg Kroah-Hartman break;
601526549d17STodd Kjos case BINDER_WORK_RETURN_ERROR: {
601626549d17STodd Kjos struct binder_error *e = container_of(
601726549d17STodd Kjos w, struct binder_error, work);
601826549d17STodd Kjos
601926549d17STodd Kjos seq_printf(m, "%stransaction error: %u\n",
602026549d17STodd Kjos prefix, e->cmd);
602126549d17STodd Kjos } break;
6022777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION_COMPLETE:
6023777783e0SGreg Kroah-Hartman seq_printf(m, "%stransaction complete\n", prefix);
6024777783e0SGreg Kroah-Hartman break;
6025777783e0SGreg Kroah-Hartman case BINDER_WORK_NODE:
6026777783e0SGreg Kroah-Hartman node = container_of(w, struct binder_node, work);
6027777783e0SGreg Kroah-Hartman seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
6028777783e0SGreg Kroah-Hartman prefix, node->debug_id,
6029777783e0SGreg Kroah-Hartman (u64)node->ptr, (u64)node->cookie);
6030777783e0SGreg Kroah-Hartman break;
6031777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER:
6032777783e0SGreg Kroah-Hartman seq_printf(m, "%shas dead binder\n", prefix);
6033777783e0SGreg Kroah-Hartman break;
6034777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
6035777783e0SGreg Kroah-Hartman seq_printf(m, "%shas cleared dead binder\n", prefix);
6036777783e0SGreg Kroah-Hartman break;
6037777783e0SGreg Kroah-Hartman case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
6038777783e0SGreg Kroah-Hartman seq_printf(m, "%shas cleared death notification\n", prefix);
6039777783e0SGreg Kroah-Hartman break;
6040777783e0SGreg Kroah-Hartman default:
6041777783e0SGreg Kroah-Hartman seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
6042777783e0SGreg Kroah-Hartman break;
6043777783e0SGreg Kroah-Hartman }
6044777783e0SGreg Kroah-Hartman }
6045777783e0SGreg Kroah-Hartman
print_binder_thread_ilocked(struct seq_file * m,struct binder_thread * thread,int print_always)604672196393STodd Kjos static void print_binder_thread_ilocked(struct seq_file *m,
6047777783e0SGreg Kroah-Hartman struct binder_thread *thread,
6048777783e0SGreg Kroah-Hartman int print_always)
6049777783e0SGreg Kroah-Hartman {
6050777783e0SGreg Kroah-Hartman struct binder_transaction *t;
6051777783e0SGreg Kroah-Hartman struct binder_work *w;
6052777783e0SGreg Kroah-Hartman size_t start_pos = m->count;
6053777783e0SGreg Kroah-Hartman size_t header_pos;
6054777783e0SGreg Kroah-Hartman
60557a4408c6STodd Kjos seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
605608dabceeSTodd Kjos thread->pid, thread->looper,
60577a4408c6STodd Kjos thread->looper_need_return,
60587a4408c6STodd Kjos atomic_read(&thread->tmp_ref));
6059777783e0SGreg Kroah-Hartman header_pos = m->count;
6060777783e0SGreg Kroah-Hartman t = thread->transaction_stack;
6061777783e0SGreg Kroah-Hartman while (t) {
6062777783e0SGreg Kroah-Hartman if (t->from == thread) {
60635f2f6369STodd Kjos print_binder_transaction_ilocked(m, thread->proc,
6064777783e0SGreg Kroah-Hartman " outgoing transaction", t);
6065777783e0SGreg Kroah-Hartman t = t->from_parent;
6066777783e0SGreg Kroah-Hartman } else if (t->to_thread == thread) {
60675f2f6369STodd Kjos print_binder_transaction_ilocked(m, thread->proc,
6068777783e0SGreg Kroah-Hartman " incoming transaction", t);
6069777783e0SGreg Kroah-Hartman t = t->to_parent;
6070777783e0SGreg Kroah-Hartman } else {
60715f2f6369STodd Kjos print_binder_transaction_ilocked(m, thread->proc,
60725f2f6369STodd Kjos " bad transaction", t);
6073777783e0SGreg Kroah-Hartman t = NULL;
6074777783e0SGreg Kroah-Hartman }
6075777783e0SGreg Kroah-Hartman }
6076777783e0SGreg Kroah-Hartman list_for_each_entry(w, &thread->todo, entry) {
60775f2f6369STodd Kjos print_binder_work_ilocked(m, thread->proc, " ",
607872196393STodd Kjos " pending transaction", w);
6079777783e0SGreg Kroah-Hartman }
6080777783e0SGreg Kroah-Hartman if (!print_always && m->count == header_pos)
6081777783e0SGreg Kroah-Hartman m->count = start_pos;
6082777783e0SGreg Kroah-Hartman }
6083777783e0SGreg Kroah-Hartman
print_binder_node_nilocked(struct seq_file * m,struct binder_node * node)6084da0fa9e4STodd Kjos static void print_binder_node_nilocked(struct seq_file *m,
6085673068eeSTodd Kjos struct binder_node *node)
6086777783e0SGreg Kroah-Hartman {
6087777783e0SGreg Kroah-Hartman struct binder_ref *ref;
6088777783e0SGreg Kroah-Hartman struct binder_work *w;
6089777783e0SGreg Kroah-Hartman int count;
6090777783e0SGreg Kroah-Hartman
6091777783e0SGreg Kroah-Hartman count = 0;
6092777783e0SGreg Kroah-Hartman hlist_for_each_entry(ref, &node->refs, node_entry)
6093777783e0SGreg Kroah-Hartman count++;
6094777783e0SGreg Kroah-Hartman
6095adc18842STodd Kjos seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
6096777783e0SGreg Kroah-Hartman node->debug_id, (u64)node->ptr, (u64)node->cookie,
6097777783e0SGreg Kroah-Hartman node->has_strong_ref, node->has_weak_ref,
6098777783e0SGreg Kroah-Hartman node->local_strong_refs, node->local_weak_refs,
6099adc18842STodd Kjos node->internal_strong_refs, count, node->tmp_refs);
6100777783e0SGreg Kroah-Hartman if (count) {
6101777783e0SGreg Kroah-Hartman seq_puts(m, " proc");
6102777783e0SGreg Kroah-Hartman hlist_for_each_entry(ref, &node->refs, node_entry)
6103777783e0SGreg Kroah-Hartman seq_printf(m, " %d", ref->proc->pid);
6104777783e0SGreg Kroah-Hartman }
6105777783e0SGreg Kroah-Hartman seq_puts(m, "\n");
610672196393STodd Kjos if (node->proc) {
6107777783e0SGreg Kroah-Hartman list_for_each_entry(w, &node->async_todo, entry)
61085f2f6369STodd Kjos print_binder_work_ilocked(m, node->proc, " ",
6109777783e0SGreg Kroah-Hartman " pending async transaction", w);
611072196393STodd Kjos }
6111777783e0SGreg Kroah-Hartman }
6112777783e0SGreg Kroah-Hartman
print_binder_ref_olocked(struct seq_file * m,struct binder_ref * ref)61132c1838dcSTodd Kjos static void print_binder_ref_olocked(struct seq_file *m,
61142c1838dcSTodd Kjos struct binder_ref *ref)
6115777783e0SGreg Kroah-Hartman {
6116673068eeSTodd Kjos binder_node_lock(ref->node);
6117372e3147STodd Kjos seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
6118372e3147STodd Kjos ref->data.debug_id, ref->data.desc,
6119372e3147STodd Kjos ref->node->proc ? "" : "dead ",
6120372e3147STodd Kjos ref->node->debug_id, ref->data.strong,
6121372e3147STodd Kjos ref->data.weak, ref->death);
6122673068eeSTodd Kjos binder_node_unlock(ref->node);
6123777783e0SGreg Kroah-Hartman }
6124777783e0SGreg Kroah-Hartman
print_binder_proc(struct seq_file * m,struct binder_proc * proc,int print_all)6125777783e0SGreg Kroah-Hartman static void print_binder_proc(struct seq_file *m,
6126777783e0SGreg Kroah-Hartman struct binder_proc *proc, int print_all)
6127777783e0SGreg Kroah-Hartman {
6128777783e0SGreg Kroah-Hartman struct binder_work *w;
6129777783e0SGreg Kroah-Hartman struct rb_node *n;
6130777783e0SGreg Kroah-Hartman size_t start_pos = m->count;
6131777783e0SGreg Kroah-Hartman size_t header_pos;
6132da0fa9e4STodd Kjos struct binder_node *last_node = NULL;
6133777783e0SGreg Kroah-Hartman
6134777783e0SGreg Kroah-Hartman seq_printf(m, "proc %d\n", proc->pid);
613514db3181SMartijn Coenen seq_printf(m, "context %s\n", proc->context->name);
6136777783e0SGreg Kroah-Hartman header_pos = m->count;
6137777783e0SGreg Kroah-Hartman
613872196393STodd Kjos binder_inner_proc_lock(proc);
6139777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
614072196393STodd Kjos print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
6141777783e0SGreg Kroah-Hartman rb_node), print_all);
6142da0fa9e4STodd Kjos
6143777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
6144777783e0SGreg Kroah-Hartman struct binder_node *node = rb_entry(n, struct binder_node,
6145777783e0SGreg Kroah-Hartman rb_node);
6146ecd589d8STodd Kjos if (!print_all && !node->has_async_transaction)
6147ecd589d8STodd Kjos continue;
6148ecd589d8STodd Kjos
6149da0fa9e4STodd Kjos /*
6150da0fa9e4STodd Kjos * take a temporary reference on the node so it
6151da0fa9e4STodd Kjos * survives and isn't removed from the tree
6152da0fa9e4STodd Kjos * while we print it.
6153da0fa9e4STodd Kjos */
6154da0fa9e4STodd Kjos binder_inc_node_tmpref_ilocked(node);
6155da0fa9e4STodd Kjos /* Need to drop inner lock to take node lock */
6156da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6157da0fa9e4STodd Kjos if (last_node)
6158da0fa9e4STodd Kjos binder_put_node(last_node);
6159da0fa9e4STodd Kjos binder_node_inner_lock(node);
6160da0fa9e4STodd Kjos print_binder_node_nilocked(m, node);
6161da0fa9e4STodd Kjos binder_node_inner_unlock(node);
6162da0fa9e4STodd Kjos last_node = node;
6163da0fa9e4STodd Kjos binder_inner_proc_lock(proc);
6164777783e0SGreg Kroah-Hartman }
6165da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6166da0fa9e4STodd Kjos if (last_node)
6167da0fa9e4STodd Kjos binder_put_node(last_node);
6168da0fa9e4STodd Kjos
6169777783e0SGreg Kroah-Hartman if (print_all) {
61702c1838dcSTodd Kjos binder_proc_lock(proc);
6171777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->refs_by_desc);
6172777783e0SGreg Kroah-Hartman n != NULL;
6173777783e0SGreg Kroah-Hartman n = rb_next(n))
61742c1838dcSTodd Kjos print_binder_ref_olocked(m, rb_entry(n,
61752c1838dcSTodd Kjos struct binder_ref,
6176777783e0SGreg Kroah-Hartman rb_node_desc));
61772c1838dcSTodd Kjos binder_proc_unlock(proc);
6178777783e0SGreg Kroah-Hartman }
617919c98724STodd Kjos binder_alloc_print_allocated(m, &proc->alloc);
618072196393STodd Kjos binder_inner_proc_lock(proc);
6181777783e0SGreg Kroah-Hartman list_for_each_entry(w, &proc->todo, entry)
61825f2f6369STodd Kjos print_binder_work_ilocked(m, proc, " ",
61835f2f6369STodd Kjos " pending transaction", w);
6184777783e0SGreg Kroah-Hartman list_for_each_entry(w, &proc->delivered_death, entry) {
6185777783e0SGreg Kroah-Hartman seq_puts(m, " has delivered dead binder\n");
6186777783e0SGreg Kroah-Hartman break;
6187777783e0SGreg Kroah-Hartman }
618872196393STodd Kjos binder_inner_proc_unlock(proc);
6189777783e0SGreg Kroah-Hartman if (!print_all && m->count == header_pos)
6190777783e0SGreg Kroah-Hartman m->count = start_pos;
6191777783e0SGreg Kroah-Hartman }
6192777783e0SGreg Kroah-Hartman
6193777783e0SGreg Kroah-Hartman static const char * const binder_return_strings[] = {
6194777783e0SGreg Kroah-Hartman "BR_ERROR",
6195777783e0SGreg Kroah-Hartman "BR_OK",
6196777783e0SGreg Kroah-Hartman "BR_TRANSACTION",
6197777783e0SGreg Kroah-Hartman "BR_REPLY",
6198777783e0SGreg Kroah-Hartman "BR_ACQUIRE_RESULT",
6199777783e0SGreg Kroah-Hartman "BR_DEAD_REPLY",
6200777783e0SGreg Kroah-Hartman "BR_TRANSACTION_COMPLETE",
6201777783e0SGreg Kroah-Hartman "BR_INCREFS",
6202777783e0SGreg Kroah-Hartman "BR_ACQUIRE",
6203777783e0SGreg Kroah-Hartman "BR_RELEASE",
6204777783e0SGreg Kroah-Hartman "BR_DECREFS",
6205777783e0SGreg Kroah-Hartman "BR_ATTEMPT_ACQUIRE",
6206777783e0SGreg Kroah-Hartman "BR_NOOP",
6207777783e0SGreg Kroah-Hartman "BR_SPAWN_LOOPER",
6208777783e0SGreg Kroah-Hartman "BR_FINISHED",
6209777783e0SGreg Kroah-Hartman "BR_DEAD_BINDER",
6210777783e0SGreg Kroah-Hartman "BR_CLEAR_DEATH_NOTIFICATION_DONE",
621100516915SHang Lu "BR_FAILED_REPLY",
621200516915SHang Lu "BR_FROZEN_REPLY",
6213a7dc1e6fSHang Lu "BR_ONEWAY_SPAM_SUSPECT",
62140567461aSLi Li "BR_TRANSACTION_PENDING_FROZEN"
6215777783e0SGreg Kroah-Hartman };
6216777783e0SGreg Kroah-Hartman
6217777783e0SGreg Kroah-Hartman static const char * const binder_command_strings[] = {
6218777783e0SGreg Kroah-Hartman "BC_TRANSACTION",
6219777783e0SGreg Kroah-Hartman "BC_REPLY",
6220777783e0SGreg Kroah-Hartman "BC_ACQUIRE_RESULT",
6221777783e0SGreg Kroah-Hartman "BC_FREE_BUFFER",
6222777783e0SGreg Kroah-Hartman "BC_INCREFS",
6223777783e0SGreg Kroah-Hartman "BC_ACQUIRE",
6224777783e0SGreg Kroah-Hartman "BC_RELEASE",
6225777783e0SGreg Kroah-Hartman "BC_DECREFS",
6226777783e0SGreg Kroah-Hartman "BC_INCREFS_DONE",
6227777783e0SGreg Kroah-Hartman "BC_ACQUIRE_DONE",
6228777783e0SGreg Kroah-Hartman "BC_ATTEMPT_ACQUIRE",
6229777783e0SGreg Kroah-Hartman "BC_REGISTER_LOOPER",
6230777783e0SGreg Kroah-Hartman "BC_ENTER_LOOPER",
6231777783e0SGreg Kroah-Hartman "BC_EXIT_LOOPER",
6232777783e0SGreg Kroah-Hartman "BC_REQUEST_DEATH_NOTIFICATION",
6233777783e0SGreg Kroah-Hartman "BC_CLEAR_DEATH_NOTIFICATION",
62347980240bSMartijn Coenen "BC_DEAD_BINDER_DONE",
62357980240bSMartijn Coenen "BC_TRANSACTION_SG",
62367980240bSMartijn Coenen "BC_REPLY_SG",
6237777783e0SGreg Kroah-Hartman };
6238777783e0SGreg Kroah-Hartman
6239777783e0SGreg Kroah-Hartman static const char * const binder_objstat_strings[] = {
6240777783e0SGreg Kroah-Hartman "proc",
6241777783e0SGreg Kroah-Hartman "thread",
6242777783e0SGreg Kroah-Hartman "node",
6243777783e0SGreg Kroah-Hartman "ref",
6244777783e0SGreg Kroah-Hartman "death",
6245777783e0SGreg Kroah-Hartman "transaction",
6246777783e0SGreg Kroah-Hartman "transaction_complete"
6247777783e0SGreg Kroah-Hartman };
6248777783e0SGreg Kroah-Hartman
print_binder_stats(struct seq_file * m,const char * prefix,struct binder_stats * stats)6249777783e0SGreg Kroah-Hartman static void print_binder_stats(struct seq_file *m, const char *prefix,
6250777783e0SGreg Kroah-Hartman struct binder_stats *stats)
6251777783e0SGreg Kroah-Hartman {
6252777783e0SGreg Kroah-Hartman int i;
6253777783e0SGreg Kroah-Hartman
6254777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
6255777783e0SGreg Kroah-Hartman ARRAY_SIZE(binder_command_strings));
6256777783e0SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
62570953c797SBadhri Jagan Sridharan int temp = atomic_read(&stats->bc[i]);
62580953c797SBadhri Jagan Sridharan
62590953c797SBadhri Jagan Sridharan if (temp)
6260777783e0SGreg Kroah-Hartman seq_printf(m, "%s%s: %d\n", prefix,
62610953c797SBadhri Jagan Sridharan binder_command_strings[i], temp);
6262777783e0SGreg Kroah-Hartman }
6263777783e0SGreg Kroah-Hartman
6264777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
6265777783e0SGreg Kroah-Hartman ARRAY_SIZE(binder_return_strings));
6266777783e0SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
62670953c797SBadhri Jagan Sridharan int temp = atomic_read(&stats->br[i]);
62680953c797SBadhri Jagan Sridharan
62690953c797SBadhri Jagan Sridharan if (temp)
6270777783e0SGreg Kroah-Hartman seq_printf(m, "%s%s: %d\n", prefix,
62710953c797SBadhri Jagan Sridharan binder_return_strings[i], temp);
6272777783e0SGreg Kroah-Hartman }
6273777783e0SGreg Kroah-Hartman
6274777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
6275777783e0SGreg Kroah-Hartman ARRAY_SIZE(binder_objstat_strings));
6276777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
6277777783e0SGreg Kroah-Hartman ARRAY_SIZE(stats->obj_deleted));
6278777783e0SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
62790953c797SBadhri Jagan Sridharan int created = atomic_read(&stats->obj_created[i]);
62800953c797SBadhri Jagan Sridharan int deleted = atomic_read(&stats->obj_deleted[i]);
62810953c797SBadhri Jagan Sridharan
62820953c797SBadhri Jagan Sridharan if (created || deleted)
62830953c797SBadhri Jagan Sridharan seq_printf(m, "%s%s: active %d total %d\n",
62840953c797SBadhri Jagan Sridharan prefix,
6285777783e0SGreg Kroah-Hartman binder_objstat_strings[i],
62860953c797SBadhri Jagan Sridharan created - deleted,
62870953c797SBadhri Jagan Sridharan created);
6288777783e0SGreg Kroah-Hartman }
6289777783e0SGreg Kroah-Hartman }
6290777783e0SGreg Kroah-Hartman
print_binder_proc_stats(struct seq_file * m,struct binder_proc * proc)6291777783e0SGreg Kroah-Hartman static void print_binder_proc_stats(struct seq_file *m,
6292777783e0SGreg Kroah-Hartman struct binder_proc *proc)
6293777783e0SGreg Kroah-Hartman {
6294777783e0SGreg Kroah-Hartman struct binder_work *w;
62951b77e9dcSMartijn Coenen struct binder_thread *thread;
6296777783e0SGreg Kroah-Hartman struct rb_node *n;
62971b77e9dcSMartijn Coenen int count, strong, weak, ready_threads;
62987bd7b0e6STodd Kjos size_t free_async_space =
62997bd7b0e6STodd Kjos binder_alloc_get_free_async_space(&proc->alloc);
6300777783e0SGreg Kroah-Hartman
6301777783e0SGreg Kroah-Hartman seq_printf(m, "proc %d\n", proc->pid);
630214db3181SMartijn Coenen seq_printf(m, "context %s\n", proc->context->name);
6303777783e0SGreg Kroah-Hartman count = 0;
63041b77e9dcSMartijn Coenen ready_threads = 0;
63057bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
6306777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
6307777783e0SGreg Kroah-Hartman count++;
63081b77e9dcSMartijn Coenen
63091b77e9dcSMartijn Coenen list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
63101b77e9dcSMartijn Coenen ready_threads++;
63111b77e9dcSMartijn Coenen
6312777783e0SGreg Kroah-Hartman seq_printf(m, " threads: %d\n", count);
6313777783e0SGreg Kroah-Hartman seq_printf(m, " requested threads: %d+%d/%d\n"
6314777783e0SGreg Kroah-Hartman " ready threads %d\n"
6315777783e0SGreg Kroah-Hartman " free async space %zd\n", proc->requested_threads,
6316777783e0SGreg Kroah-Hartman proc->requested_threads_started, proc->max_threads,
63171b77e9dcSMartijn Coenen ready_threads,
63187bd7b0e6STodd Kjos free_async_space);
6319777783e0SGreg Kroah-Hartman count = 0;
6320777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
6321777783e0SGreg Kroah-Hartman count++;
6322da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6323777783e0SGreg Kroah-Hartman seq_printf(m, " nodes: %d\n", count);
6324777783e0SGreg Kroah-Hartman count = 0;
6325777783e0SGreg Kroah-Hartman strong = 0;
6326777783e0SGreg Kroah-Hartman weak = 0;
63272c1838dcSTodd Kjos binder_proc_lock(proc);
6328777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
6329777783e0SGreg Kroah-Hartman struct binder_ref *ref = rb_entry(n, struct binder_ref,
6330777783e0SGreg Kroah-Hartman rb_node_desc);
6331777783e0SGreg Kroah-Hartman count++;
6332372e3147STodd Kjos strong += ref->data.strong;
6333372e3147STodd Kjos weak += ref->data.weak;
6334777783e0SGreg Kroah-Hartman }
63352c1838dcSTodd Kjos binder_proc_unlock(proc);
6336777783e0SGreg Kroah-Hartman seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
6337777783e0SGreg Kroah-Hartman
633819c98724STodd Kjos count = binder_alloc_get_allocated_count(&proc->alloc);
6339777783e0SGreg Kroah-Hartman seq_printf(m, " buffers: %d\n", count);
6340777783e0SGreg Kroah-Hartman
63418ef4665aSSherry Yang binder_alloc_print_pages(m, &proc->alloc);
63428ef4665aSSherry Yang
6343777783e0SGreg Kroah-Hartman count = 0;
634472196393STodd Kjos binder_inner_proc_lock(proc);
6345777783e0SGreg Kroah-Hartman list_for_each_entry(w, &proc->todo, entry) {
634672196393STodd Kjos if (w->type == BINDER_WORK_TRANSACTION)
6347777783e0SGreg Kroah-Hartman count++;
6348777783e0SGreg Kroah-Hartman }
634972196393STodd Kjos binder_inner_proc_unlock(proc);
6350777783e0SGreg Kroah-Hartman seq_printf(m, " pending transactions: %d\n", count);
6351777783e0SGreg Kroah-Hartman
6352777783e0SGreg Kroah-Hartman print_binder_stats(m, " ", &proc->stats);
6353777783e0SGreg Kroah-Hartman }
6354777783e0SGreg Kroah-Hartman
state_show(struct seq_file * m,void * unused)6355b7e241bbSCarlos Llamas static int state_show(struct seq_file *m, void *unused)
6356777783e0SGreg Kroah-Hartman {
6357777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6358777783e0SGreg Kroah-Hartman struct binder_node *node;
6359673068eeSTodd Kjos struct binder_node *last_node = NULL;
6360777783e0SGreg Kroah-Hartman
6361777783e0SGreg Kroah-Hartman seq_puts(m, "binder state:\n");
6362777783e0SGreg Kroah-Hartman
6363c44b1231STodd Kjos spin_lock(&binder_dead_nodes_lock);
6364777783e0SGreg Kroah-Hartman if (!hlist_empty(&binder_dead_nodes))
6365777783e0SGreg Kroah-Hartman seq_puts(m, "dead nodes:\n");
6366673068eeSTodd Kjos hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
6367673068eeSTodd Kjos /*
6368673068eeSTodd Kjos * take a temporary reference on the node so it
6369673068eeSTodd Kjos * survives and isn't removed from the list
6370673068eeSTodd Kjos * while we print it.
6371673068eeSTodd Kjos */
6372673068eeSTodd Kjos node->tmp_refs++;
6373c44b1231STodd Kjos spin_unlock(&binder_dead_nodes_lock);
6374673068eeSTodd Kjos if (last_node)
6375673068eeSTodd Kjos binder_put_node(last_node);
6376673068eeSTodd Kjos binder_node_lock(node);
6377da0fa9e4STodd Kjos print_binder_node_nilocked(m, node);
6378673068eeSTodd Kjos binder_node_unlock(node);
6379673068eeSTodd Kjos last_node = node;
6380673068eeSTodd Kjos spin_lock(&binder_dead_nodes_lock);
6381673068eeSTodd Kjos }
6382673068eeSTodd Kjos spin_unlock(&binder_dead_nodes_lock);
6383673068eeSTodd Kjos if (last_node)
6384673068eeSTodd Kjos binder_put_node(last_node);
6385777783e0SGreg Kroah-Hartman
6386c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6387777783e0SGreg Kroah-Hartman hlist_for_each_entry(proc, &binder_procs, proc_node)
6388777783e0SGreg Kroah-Hartman print_binder_proc(m, proc, 1);
6389c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6390a60b890fSTodd Kjos
6391777783e0SGreg Kroah-Hartman return 0;
6392777783e0SGreg Kroah-Hartman }
6393777783e0SGreg Kroah-Hartman
stats_show(struct seq_file * m,void * unused)6394b7e241bbSCarlos Llamas static int stats_show(struct seq_file *m, void *unused)
6395777783e0SGreg Kroah-Hartman {
6396777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6397777783e0SGreg Kroah-Hartman
6398777783e0SGreg Kroah-Hartman seq_puts(m, "binder stats:\n");
6399777783e0SGreg Kroah-Hartman
6400777783e0SGreg Kroah-Hartman print_binder_stats(m, "", &binder_stats);
6401777783e0SGreg Kroah-Hartman
6402c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6403777783e0SGreg Kroah-Hartman hlist_for_each_entry(proc, &binder_procs, proc_node)
6404777783e0SGreg Kroah-Hartman print_binder_proc_stats(m, proc);
6405c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6406a60b890fSTodd Kjos
6407777783e0SGreg Kroah-Hartman return 0;
6408777783e0SGreg Kroah-Hartman }
6409777783e0SGreg Kroah-Hartman
transactions_show(struct seq_file * m,void * unused)6410b7e241bbSCarlos Llamas static int transactions_show(struct seq_file *m, void *unused)
6411777783e0SGreg Kroah-Hartman {
6412777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6413777783e0SGreg Kroah-Hartman
6414777783e0SGreg Kroah-Hartman seq_puts(m, "binder transactions:\n");
6415c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6416777783e0SGreg Kroah-Hartman hlist_for_each_entry(proc, &binder_procs, proc_node)
6417777783e0SGreg Kroah-Hartman print_binder_proc(m, proc, 0);
6418c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6419a60b890fSTodd Kjos
6420777783e0SGreg Kroah-Hartman return 0;
6421777783e0SGreg Kroah-Hartman }
6422777783e0SGreg Kroah-Hartman
proc_show(struct seq_file * m,void * unused)6423c13e0a52SYangtao Li static int proc_show(struct seq_file *m, void *unused)
6424777783e0SGreg Kroah-Hartman {
642583050a4eSRiley Andrews struct binder_proc *itr;
642614db3181SMartijn Coenen int pid = (unsigned long)m->private;
6427777783e0SGreg Kroah-Hartman
6428c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
642983050a4eSRiley Andrews hlist_for_each_entry(itr, &binder_procs, proc_node) {
643014db3181SMartijn Coenen if (itr->pid == pid) {
6431777783e0SGreg Kroah-Hartman seq_puts(m, "binder proc state:\n");
643214db3181SMartijn Coenen print_binder_proc(m, itr, 1);
643314db3181SMartijn Coenen }
643483050a4eSRiley Andrews }
6435c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6436c44b1231STodd Kjos
6437777783e0SGreg Kroah-Hartman return 0;
6438777783e0SGreg Kroah-Hartman }
6439777783e0SGreg Kroah-Hartman
print_binder_transaction_log_entry(struct seq_file * m,struct binder_transaction_log_entry * e)6440777783e0SGreg Kroah-Hartman static void print_binder_transaction_log_entry(struct seq_file *m,
6441777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *e)
6442777783e0SGreg Kroah-Hartman {
6443d99c7333STodd Kjos int debug_id = READ_ONCE(e->debug_id_done);
6444d99c7333STodd Kjos /*
6445d99c7333STodd Kjos * read barrier to guarantee debug_id_done read before
6446d99c7333STodd Kjos * we print the log values
6447d99c7333STodd Kjos */
6448d99c7333STodd Kjos smp_rmb();
6449777783e0SGreg Kroah-Hartman seq_printf(m,
6450d99c7333STodd Kjos "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
6451777783e0SGreg Kroah-Hartman e->debug_id, (e->call_type == 2) ? "reply" :
6452777783e0SGreg Kroah-Hartman ((e->call_type == 1) ? "async" : "call "), e->from_proc,
645314db3181SMartijn Coenen e->from_thread, e->to_proc, e->to_thread, e->context_name,
645457ada2fbSTodd Kjos e->to_node, e->target_handle, e->data_size, e->offsets_size,
645557ada2fbSTodd Kjos e->return_error, e->return_error_param,
645657ada2fbSTodd Kjos e->return_error_line);
6457d99c7333STodd Kjos /*
6458d99c7333STodd Kjos * read-barrier to guarantee read of debug_id_done after
6459d99c7333STodd Kjos * done printing the fields of the entry
6460d99c7333STodd Kjos */
6461d99c7333STodd Kjos smp_rmb();
6462d99c7333STodd Kjos seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6463d99c7333STodd Kjos "\n" : " (incomplete)\n");
6464777783e0SGreg Kroah-Hartman }
6465777783e0SGreg Kroah-Hartman
transaction_log_show(struct seq_file * m,void * unused)6466b7e241bbSCarlos Llamas static int transaction_log_show(struct seq_file *m, void *unused)
6467777783e0SGreg Kroah-Hartman {
6468777783e0SGreg Kroah-Hartman struct binder_transaction_log *log = m->private;
6469d99c7333STodd Kjos unsigned int log_cur = atomic_read(&log->cur);
6470d99c7333STodd Kjos unsigned int count;
6471d99c7333STodd Kjos unsigned int cur;
6472777783e0SGreg Kroah-Hartman int i;
6473777783e0SGreg Kroah-Hartman
6474d99c7333STodd Kjos count = log_cur + 1;
6475d99c7333STodd Kjos cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6476d99c7333STodd Kjos 0 : count % ARRAY_SIZE(log->entry);
6477d99c7333STodd Kjos if (count > ARRAY_SIZE(log->entry) || log->full)
6478d99c7333STodd Kjos count = ARRAY_SIZE(log->entry);
6479d99c7333STodd Kjos for (i = 0; i < count; i++) {
6480d99c7333STodd Kjos unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6481d99c7333STodd Kjos
6482d99c7333STodd Kjos print_binder_transaction_log_entry(m, &log->entry[index]);
6483777783e0SGreg Kroah-Hartman }
6484777783e0SGreg Kroah-Hartman return 0;
6485777783e0SGreg Kroah-Hartman }
6486777783e0SGreg Kroah-Hartman
64873ad20fe3SChristian Brauner const struct file_operations binder_fops = {
6488777783e0SGreg Kroah-Hartman .owner = THIS_MODULE,
6489777783e0SGreg Kroah-Hartman .poll = binder_poll,
6490777783e0SGreg Kroah-Hartman .unlocked_ioctl = binder_ioctl,
64911832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl,
6492777783e0SGreg Kroah-Hartman .mmap = binder_mmap,
6493777783e0SGreg Kroah-Hartman .open = binder_open,
6494777783e0SGreg Kroah-Hartman .flush = binder_flush,
6495777783e0SGreg Kroah-Hartman .release = binder_release,
6496777783e0SGreg Kroah-Hartman };
6497777783e0SGreg Kroah-Hartman
6498b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(state);
6499b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(stats);
6500b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(transactions);
6501b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(transaction_log);
6502b7e241bbSCarlos Llamas
6503b7e241bbSCarlos Llamas const struct binder_debugfs_entry binder_debugfs_entries[] = {
6504b7e241bbSCarlos Llamas {
6505b7e241bbSCarlos Llamas .name = "state",
6506b7e241bbSCarlos Llamas .mode = 0444,
6507b7e241bbSCarlos Llamas .fops = &state_fops,
6508b7e241bbSCarlos Llamas .data = NULL,
6509b7e241bbSCarlos Llamas },
6510b7e241bbSCarlos Llamas {
6511b7e241bbSCarlos Llamas .name = "stats",
6512b7e241bbSCarlos Llamas .mode = 0444,
6513b7e241bbSCarlos Llamas .fops = &stats_fops,
6514b7e241bbSCarlos Llamas .data = NULL,
6515b7e241bbSCarlos Llamas },
6516b7e241bbSCarlos Llamas {
6517b7e241bbSCarlos Llamas .name = "transactions",
6518b7e241bbSCarlos Llamas .mode = 0444,
6519b7e241bbSCarlos Llamas .fops = &transactions_fops,
6520b7e241bbSCarlos Llamas .data = NULL,
6521b7e241bbSCarlos Llamas },
6522b7e241bbSCarlos Llamas {
6523b7e241bbSCarlos Llamas .name = "transaction_log",
6524b7e241bbSCarlos Llamas .mode = 0444,
6525b7e241bbSCarlos Llamas .fops = &transaction_log_fops,
6526b7e241bbSCarlos Llamas .data = &binder_transaction_log,
6527b7e241bbSCarlos Llamas },
6528b7e241bbSCarlos Llamas {
6529b7e241bbSCarlos Llamas .name = "failed_transaction_log",
6530b7e241bbSCarlos Llamas .mode = 0444,
6531b7e241bbSCarlos Llamas .fops = &transaction_log_fops,
6532b7e241bbSCarlos Llamas .data = &binder_transaction_log_failed,
6533b7e241bbSCarlos Llamas },
6534b7e241bbSCarlos Llamas {} /* terminator */
6535b7e241bbSCarlos Llamas };
6536b7e241bbSCarlos Llamas
init_binder_device(const char * name)6537ac4812c5SMartijn Coenen static int __init init_binder_device(const char *name)
6538ac4812c5SMartijn Coenen {
6539ac4812c5SMartijn Coenen int ret;
6540ac4812c5SMartijn Coenen struct binder_device *binder_device;
6541ac4812c5SMartijn Coenen
6542ac4812c5SMartijn Coenen binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6543ac4812c5SMartijn Coenen if (!binder_device)
6544ac4812c5SMartijn Coenen return -ENOMEM;
6545ac4812c5SMartijn Coenen
6546ac4812c5SMartijn Coenen binder_device->miscdev.fops = &binder_fops;
6547ac4812c5SMartijn Coenen binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6548ac4812c5SMartijn Coenen binder_device->miscdev.name = name;
6549ac4812c5SMartijn Coenen
6550f0fe2c0fSChristian Brauner refcount_set(&binder_device->ref, 1);
6551ac4812c5SMartijn Coenen binder_device->context.binder_context_mgr_uid = INVALID_UID;
6552ac4812c5SMartijn Coenen binder_device->context.name = name;
6553c44b1231STodd Kjos mutex_init(&binder_device->context.context_mgr_node_lock);
6554ac4812c5SMartijn Coenen
6555ac4812c5SMartijn Coenen ret = misc_register(&binder_device->miscdev);
6556ac4812c5SMartijn Coenen if (ret < 0) {
6557ac4812c5SMartijn Coenen kfree(binder_device);
6558ac4812c5SMartijn Coenen return ret;
6559ac4812c5SMartijn Coenen }
6560ac4812c5SMartijn Coenen
6561ac4812c5SMartijn Coenen hlist_add_head(&binder_device->hlist, &binder_devices);
6562ac4812c5SMartijn Coenen
6563ac4812c5SMartijn Coenen return ret;
6564ac4812c5SMartijn Coenen }
6565ac4812c5SMartijn Coenen
binder_init(void)6566777783e0SGreg Kroah-Hartman static int __init binder_init(void)
6567777783e0SGreg Kroah-Hartman {
6568777783e0SGreg Kroah-Hartman int ret;
65695b9633afSChristian Brauner char *device_name, *device_tmp;
6570ac4812c5SMartijn Coenen struct binder_device *device;
6571ac4812c5SMartijn Coenen struct hlist_node *tmp;
65725b9633afSChristian Brauner char *device_names = NULL;
657351b080a4SWang Ming const struct binder_debugfs_entry *db_entry;
6574777783e0SGreg Kroah-Hartman
6575533dfb25STetsuo Handa ret = binder_alloc_shrinker_init();
6576533dfb25STetsuo Handa if (ret)
6577533dfb25STetsuo Handa return ret;
6578f2517eb7SSherry Yang
6579d99c7333STodd Kjos atomic_set(&binder_transaction_log.cur, ~0U);
6580d99c7333STodd Kjos atomic_set(&binder_transaction_log_failed.cur, ~0U);
6581d99c7333STodd Kjos
6582777783e0SGreg Kroah-Hartman binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6583b7e241bbSCarlos Llamas
6584b7e241bbSCarlos Llamas binder_for_each_debugfs_entry(db_entry)
6585b7e241bbSCarlos Llamas debugfs_create_file(db_entry->name,
6586b7e241bbSCarlos Llamas db_entry->mode,
6587b7e241bbSCarlos Llamas binder_debugfs_dir_entry_root,
6588b7e241bbSCarlos Llamas db_entry->data,
6589b7e241bbSCarlos Llamas db_entry->fops);
6590b7e241bbSCarlos Llamas
6591777783e0SGreg Kroah-Hartman binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6592777783e0SGreg Kroah-Hartman binder_debugfs_dir_entry_root);
6593ac4812c5SMartijn Coenen
6594ca2864c6SHridya Valsaraju if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
6595ca2864c6SHridya Valsaraju strcmp(binder_devices_param, "") != 0) {
6596ac4812c5SMartijn Coenen /*
6597ac4812c5SMartijn Coenen * Copy the module_parameter string, because we don't want to
6598ac4812c5SMartijn Coenen * tokenize it in-place.
6599ac4812c5SMartijn Coenen */
66006b6642daSRasmus Villemoes device_names = kstrdup(binder_devices_param, GFP_KERNEL);
6601ac4812c5SMartijn Coenen if (!device_names) {
6602ac4812c5SMartijn Coenen ret = -ENOMEM;
6603ac4812c5SMartijn Coenen goto err_alloc_device_names_failed;
6604ac4812c5SMartijn Coenen }
6605ac4812c5SMartijn Coenen
660622eb9476SChristian Brauner device_tmp = device_names;
660722eb9476SChristian Brauner while ((device_name = strsep(&device_tmp, ","))) {
6608ac4812c5SMartijn Coenen ret = init_binder_device(device_name);
6609ac4812c5SMartijn Coenen if (ret)
6610ac4812c5SMartijn Coenen goto err_init_binder_device_failed;
6611ac4812c5SMartijn Coenen }
6612793c8232SChristian Brauner }
6613ac4812c5SMartijn Coenen
66145b9633afSChristian Brauner ret = init_binderfs();
66155b9633afSChristian Brauner if (ret)
66165b9633afSChristian Brauner goto err_init_binder_device_failed;
6617ac4812c5SMartijn Coenen
6618ac4812c5SMartijn Coenen return ret;
6619ac4812c5SMartijn Coenen
6620ac4812c5SMartijn Coenen err_init_binder_device_failed:
6621ac4812c5SMartijn Coenen hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6622ac4812c5SMartijn Coenen misc_deregister(&device->miscdev);
6623ac4812c5SMartijn Coenen hlist_del(&device->hlist);
6624ac4812c5SMartijn Coenen kfree(device);
6625ac4812c5SMartijn Coenen }
662622eb9476SChristian Brauner
662722eb9476SChristian Brauner kfree(device_names);
662822eb9476SChristian Brauner
6629ac4812c5SMartijn Coenen err_alloc_device_names_failed:
6630ac4812c5SMartijn Coenen debugfs_remove_recursive(binder_debugfs_dir_entry_root);
6631adb9743dSQi Zheng binder_alloc_shrinker_exit();
6632ac4812c5SMartijn Coenen
6633777783e0SGreg Kroah-Hartman return ret;
6634777783e0SGreg Kroah-Hartman }
6635777783e0SGreg Kroah-Hartman
6636777783e0SGreg Kroah-Hartman device_initcall(binder_init);
6637777783e0SGreg Kroah-Hartman
6638777783e0SGreg Kroah-Hartman #define CREATE_TRACE_POINTS
6639777783e0SGreg Kroah-Hartman #include "binder_trace.h"
6640777783e0SGreg Kroah-Hartman
6641777783e0SGreg Kroah-Hartman MODULE_LICENSE("GPL v2");
6642