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 /**
280e9e46ed2SBa Jing * binder_proc_unlock() - Release outer lock 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);
48197830f3cSCarlos Llamas
48297830f3cSCarlos Llamas /* (e)poll-based threads require an explicit wakeup signal when
48397830f3cSCarlos Llamas * queuing their own work; they rely on these events to consume
48497830f3cSCarlos Llamas * messages without I/O block. Without it, threads risk waiting
48597830f3cSCarlos Llamas * indefinitely without handling the work.
48697830f3cSCarlos Llamas */
48797830f3cSCarlos Llamas if (thread->looper & BINDER_LOOPER_STATE_POLL &&
48897830f3cSCarlos Llamas thread->pid == current->pid && !thread->process_todo)
48997830f3cSCarlos Llamas wake_up_interruptible_sync(&thread->wait);
49097830f3cSCarlos 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 &&
57331643d84SCarlos 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
104615d9da3fSCarlos Llamas /* Find the smallest unused descriptor the "slow way" */
slow_desc_lookup_olocked(struct binder_proc * proc,u32 offset)104711512c19SCarlos Llamas static u32 slow_desc_lookup_olocked(struct binder_proc *proc, u32 offset)
104815d9da3fSCarlos Llamas {
104915d9da3fSCarlos Llamas struct binder_ref *ref;
105015d9da3fSCarlos Llamas struct rb_node *n;
105115d9da3fSCarlos Llamas u32 desc;
105215d9da3fSCarlos Llamas
105311512c19SCarlos Llamas desc = offset;
105415d9da3fSCarlos Llamas for (n = rb_first(&proc->refs_by_desc); n; n = rb_next(n)) {
105515d9da3fSCarlos Llamas ref = rb_entry(n, struct binder_ref, rb_node_desc);
105615d9da3fSCarlos Llamas if (ref->data.desc > desc)
105715d9da3fSCarlos Llamas break;
105815d9da3fSCarlos Llamas desc = ref->data.desc + 1;
105915d9da3fSCarlos Llamas }
106015d9da3fSCarlos Llamas
106115d9da3fSCarlos Llamas return desc;
106215d9da3fSCarlos Llamas }
106315d9da3fSCarlos Llamas
106415d9da3fSCarlos Llamas /*
106515d9da3fSCarlos Llamas * Find an available reference descriptor ID. The proc->outer_lock might
106615d9da3fSCarlos Llamas * be released in the process, in which case -EAGAIN is returned and the
106715d9da3fSCarlos Llamas * @desc should be considered invalid.
106815d9da3fSCarlos Llamas */
get_ref_desc_olocked(struct binder_proc * proc,struct binder_node * node,u32 * desc)106915d9da3fSCarlos Llamas static int get_ref_desc_olocked(struct binder_proc *proc,
107015d9da3fSCarlos Llamas struct binder_node *node,
107115d9da3fSCarlos Llamas u32 *desc)
107215d9da3fSCarlos Llamas {
107315d9da3fSCarlos Llamas struct dbitmap *dmap = &proc->dmap;
107411512c19SCarlos Llamas unsigned int nbits, offset;
107515d9da3fSCarlos Llamas unsigned long *new, bit;
107615d9da3fSCarlos Llamas
107715d9da3fSCarlos Llamas /* 0 is reserved for the context manager */
107811512c19SCarlos Llamas offset = (node == proc->context->binder_context_mgr_node) ? 0 : 1;
107915d9da3fSCarlos Llamas
108015d9da3fSCarlos Llamas if (!dbitmap_enabled(dmap)) {
108111512c19SCarlos Llamas *desc = slow_desc_lookup_olocked(proc, offset);
108215d9da3fSCarlos Llamas return 0;
108315d9da3fSCarlos Llamas }
108415d9da3fSCarlos Llamas
108511512c19SCarlos Llamas if (dbitmap_acquire_next_zero_bit(dmap, offset, &bit) == 0) {
108615d9da3fSCarlos Llamas *desc = bit;
108715d9da3fSCarlos Llamas return 0;
108815d9da3fSCarlos Llamas }
108915d9da3fSCarlos Llamas
109015d9da3fSCarlos Llamas /*
109115d9da3fSCarlos Llamas * The dbitmap is full and needs to grow. The proc->outer_lock
109215d9da3fSCarlos Llamas * is briefly released to allocate the new bitmap safely.
109315d9da3fSCarlos Llamas */
109415d9da3fSCarlos Llamas nbits = dbitmap_grow_nbits(dmap);
109515d9da3fSCarlos Llamas binder_proc_unlock(proc);
109615d9da3fSCarlos Llamas new = bitmap_zalloc(nbits, GFP_KERNEL);
109715d9da3fSCarlos Llamas binder_proc_lock(proc);
109815d9da3fSCarlos Llamas dbitmap_grow(dmap, new, nbits);
109915d9da3fSCarlos Llamas
110015d9da3fSCarlos Llamas return -EAGAIN;
110115d9da3fSCarlos Llamas }
110215d9da3fSCarlos Llamas
1103372e3147STodd Kjos /**
11042c1838dcSTodd Kjos * binder_get_ref_for_node_olocked() - get the ref associated with given node
1105372e3147STodd Kjos * @proc: binder_proc that owns the ref
1106372e3147STodd Kjos * @node: binder_node of target
1107372e3147STodd Kjos * @new_ref: newly allocated binder_ref to be initialized or %NULL
1108372e3147STodd Kjos *
1109372e3147STodd Kjos * Look up the ref for the given node and return it if it exists
1110372e3147STodd Kjos *
1111372e3147STodd Kjos * If it doesn't exist and the caller provides a newly allocated
1112372e3147STodd Kjos * ref, initialize the fields of the newly allocated ref and insert
1113372e3147STodd Kjos * into the given proc rb_trees and node refs list.
1114372e3147STodd Kjos *
1115372e3147STodd Kjos * Return: the ref for node. It is possible that another thread
1116372e3147STodd Kjos * allocated/initialized the ref first in which case the
1117372e3147STodd Kjos * returned ref would be different than the passed-in
1118372e3147STodd Kjos * new_ref. new_ref must be kfree'd by the caller in
1119372e3147STodd Kjos * this case.
1120372e3147STodd Kjos */
binder_get_ref_for_node_olocked(struct binder_proc * proc,struct binder_node * node,struct binder_ref * new_ref)11212c1838dcSTodd Kjos static struct binder_ref *binder_get_ref_for_node_olocked(
11222c1838dcSTodd Kjos struct binder_proc *proc,
1123372e3147STodd Kjos struct binder_node *node,
1124372e3147STodd Kjos struct binder_ref *new_ref)
1125777783e0SGreg Kroah-Hartman {
1126372e3147STodd Kjos struct binder_ref *ref;
112715d9da3fSCarlos Llamas struct rb_node *parent;
112815d9da3fSCarlos Llamas struct rb_node **p;
112915d9da3fSCarlos Llamas u32 desc;
1130777783e0SGreg Kroah-Hartman
113115d9da3fSCarlos Llamas retry:
113215d9da3fSCarlos Llamas p = &proc->refs_by_node.rb_node;
113315d9da3fSCarlos Llamas parent = NULL;
1134777783e0SGreg Kroah-Hartman while (*p) {
1135777783e0SGreg Kroah-Hartman parent = *p;
1136777783e0SGreg Kroah-Hartman ref = rb_entry(parent, struct binder_ref, rb_node_node);
1137777783e0SGreg Kroah-Hartman
1138777783e0SGreg Kroah-Hartman if (node < ref->node)
1139777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
1140777783e0SGreg Kroah-Hartman else if (node > ref->node)
1141777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
1142777783e0SGreg Kroah-Hartman else
1143777783e0SGreg Kroah-Hartman return ref;
1144777783e0SGreg Kroah-Hartman }
1145372e3147STodd Kjos if (!new_ref)
1146777783e0SGreg Kroah-Hartman return NULL;
1147372e3147STodd Kjos
114815d9da3fSCarlos Llamas /* might release the proc->outer_lock */
114915d9da3fSCarlos Llamas if (get_ref_desc_olocked(proc, node, &desc) == -EAGAIN)
115015d9da3fSCarlos Llamas goto retry;
115115d9da3fSCarlos Llamas
1152777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_REF);
1153372e3147STodd Kjos new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
1154777783e0SGreg Kroah-Hartman new_ref->proc = proc;
1155777783e0SGreg Kroah-Hartman new_ref->node = node;
1156777783e0SGreg Kroah-Hartman rb_link_node(&new_ref->rb_node_node, parent, p);
1157777783e0SGreg Kroah-Hartman rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1158777783e0SGreg Kroah-Hartman
115915d9da3fSCarlos Llamas new_ref->data.desc = desc;
1160777783e0SGreg Kroah-Hartman p = &proc->refs_by_desc.rb_node;
1161777783e0SGreg Kroah-Hartman while (*p) {
1162777783e0SGreg Kroah-Hartman parent = *p;
1163777783e0SGreg Kroah-Hartman ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1164777783e0SGreg Kroah-Hartman
1165372e3147STodd Kjos if (new_ref->data.desc < ref->data.desc)
1166777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
1167372e3147STodd Kjos else if (new_ref->data.desc > ref->data.desc)
1168777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
1169777783e0SGreg Kroah-Hartman else
1170777783e0SGreg Kroah-Hartman BUG();
1171777783e0SGreg Kroah-Hartman }
1172777783e0SGreg Kroah-Hartman rb_link_node(&new_ref->rb_node_desc, parent, p);
1173777783e0SGreg Kroah-Hartman rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1174673068eeSTodd Kjos
1175673068eeSTodd Kjos binder_node_lock(node);
1176777783e0SGreg Kroah-Hartman hlist_add_head(&new_ref->node_entry, &node->refs);
1177777783e0SGreg Kroah-Hartman
1178777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1179777783e0SGreg Kroah-Hartman "%d new ref %d desc %d for node %d\n",
1180372e3147STodd Kjos proc->pid, new_ref->data.debug_id, new_ref->data.desc,
1181777783e0SGreg Kroah-Hartman node->debug_id);
1182673068eeSTodd Kjos binder_node_unlock(node);
1183777783e0SGreg Kroah-Hartman return new_ref;
1184777783e0SGreg Kroah-Hartman }
1185777783e0SGreg Kroah-Hartman
binder_cleanup_ref_olocked(struct binder_ref * ref)11862c1838dcSTodd Kjos static void binder_cleanup_ref_olocked(struct binder_ref *ref)
1187777783e0SGreg Kroah-Hartman {
118815d9da3fSCarlos Llamas struct dbitmap *dmap = &ref->proc->dmap;
1189ed29721eSTodd Kjos bool delete_node = false;
1190ed29721eSTodd Kjos
1191777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1192777783e0SGreg Kroah-Hartman "%d delete ref %d desc %d for node %d\n",
1193372e3147STodd Kjos ref->proc->pid, ref->data.debug_id, ref->data.desc,
1194777783e0SGreg Kroah-Hartman ref->node->debug_id);
1195777783e0SGreg Kroah-Hartman
119615d9da3fSCarlos Llamas if (dbitmap_enabled(dmap))
119715d9da3fSCarlos Llamas dbitmap_clear_bit(dmap, ref->data.desc);
1198777783e0SGreg Kroah-Hartman rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1199777783e0SGreg Kroah-Hartman rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1200372e3147STodd Kjos
1201673068eeSTodd Kjos binder_node_inner_lock(ref->node);
1202372e3147STodd Kjos if (ref->data.strong)
1203673068eeSTodd Kjos binder_dec_node_nilocked(ref->node, 1, 1);
1204372e3147STodd Kjos
1205777783e0SGreg Kroah-Hartman hlist_del(&ref->node_entry);
1206673068eeSTodd Kjos delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1207673068eeSTodd Kjos binder_node_inner_unlock(ref->node);
1208ed29721eSTodd Kjos /*
1209ed29721eSTodd Kjos * Clear ref->node unless we want the caller to free the node
1210ed29721eSTodd Kjos */
1211ed29721eSTodd Kjos if (!delete_node) {
1212ed29721eSTodd Kjos /*
1213ed29721eSTodd Kjos * The caller uses ref->node to determine
1214ed29721eSTodd Kjos * whether the node needs to be freed. Clear
1215ed29721eSTodd Kjos * it since the node is still alive.
1216ed29721eSTodd Kjos */
1217ed29721eSTodd Kjos ref->node = NULL;
1218ed29721eSTodd Kjos }
1219372e3147STodd Kjos
1220777783e0SGreg Kroah-Hartman if (ref->death) {
1221777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
1222777783e0SGreg Kroah-Hartman "%d delete ref %d desc %d has death notification\n",
1223372e3147STodd Kjos ref->proc->pid, ref->data.debug_id,
1224372e3147STodd Kjos ref->data.desc);
122572196393STodd Kjos binder_dequeue_work(ref->proc, &ref->death->work);
1226777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_DEATH);
1227777783e0SGreg Kroah-Hartman }
12287e20434cSCarlos Llamas
12297e20434cSCarlos Llamas if (ref->freeze) {
12307e20434cSCarlos Llamas binder_dequeue_work(ref->proc, &ref->freeze->work);
12317e20434cSCarlos Llamas binder_stats_deleted(BINDER_STAT_FREEZE);
12327e20434cSCarlos Llamas }
12337e20434cSCarlos Llamas
1234777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_REF);
1235777783e0SGreg Kroah-Hartman }
1236777783e0SGreg Kroah-Hartman
1237372e3147STodd Kjos /**
12382c1838dcSTodd Kjos * binder_inc_ref_olocked() - increment the ref for given handle
1239372e3147STodd Kjos * @ref: ref to be incremented
1240372e3147STodd Kjos * @strong: if true, strong increment, else weak
1241372e3147STodd Kjos * @target_list: list to queue node work on
1242372e3147STodd Kjos *
12432c1838dcSTodd Kjos * Increment the ref. @ref->proc->outer_lock must be held on entry
1244372e3147STodd Kjos *
1245372e3147STodd Kjos * Return: 0, if successful, else errno
1246372e3147STodd Kjos */
binder_inc_ref_olocked(struct binder_ref * ref,int strong,struct list_head * target_list)12472c1838dcSTodd Kjos static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1248777783e0SGreg Kroah-Hartman struct list_head *target_list)
1249777783e0SGreg Kroah-Hartman {
1250777783e0SGreg Kroah-Hartman int ret;
1251777783e0SGreg Kroah-Hartman
1252777783e0SGreg Kroah-Hartman if (strong) {
1253372e3147STodd Kjos if (ref->data.strong == 0) {
1254777783e0SGreg Kroah-Hartman ret = binder_inc_node(ref->node, 1, 1, target_list);
1255777783e0SGreg Kroah-Hartman if (ret)
1256777783e0SGreg Kroah-Hartman return ret;
1257777783e0SGreg Kroah-Hartman }
1258372e3147STodd Kjos ref->data.strong++;
1259777783e0SGreg Kroah-Hartman } else {
1260372e3147STodd Kjos if (ref->data.weak == 0) {
1261777783e0SGreg Kroah-Hartman ret = binder_inc_node(ref->node, 0, 1, target_list);
1262777783e0SGreg Kroah-Hartman if (ret)
1263777783e0SGreg Kroah-Hartman return ret;
1264777783e0SGreg Kroah-Hartman }
1265372e3147STodd Kjos ref->data.weak++;
1266777783e0SGreg Kroah-Hartman }
1267777783e0SGreg Kroah-Hartman return 0;
1268777783e0SGreg Kroah-Hartman }
1269777783e0SGreg Kroah-Hartman
1270372e3147STodd Kjos /**
1271ad228a34SRandy Dunlap * binder_dec_ref_olocked() - dec the ref for given handle
1272372e3147STodd Kjos * @ref: ref to be decremented
1273372e3147STodd Kjos * @strong: if true, strong decrement, else weak
1274372e3147STodd Kjos *
1275372e3147STodd Kjos * Decrement the ref.
1276372e3147STodd Kjos *
1277ad228a34SRandy Dunlap * Return: %true if ref is cleaned up and ready to be freed.
1278372e3147STodd Kjos */
binder_dec_ref_olocked(struct binder_ref * ref,int strong)12792c1838dcSTodd Kjos static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
1280777783e0SGreg Kroah-Hartman {
1281777783e0SGreg Kroah-Hartman if (strong) {
1282372e3147STodd Kjos if (ref->data.strong == 0) {
1283777783e0SGreg Kroah-Hartman binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
1284372e3147STodd Kjos ref->proc->pid, ref->data.debug_id,
1285372e3147STodd Kjos ref->data.desc, ref->data.strong,
1286372e3147STodd Kjos ref->data.weak);
1287372e3147STodd Kjos return false;
1288777783e0SGreg Kroah-Hartman }
1289372e3147STodd Kjos ref->data.strong--;
1290ed29721eSTodd Kjos if (ref->data.strong == 0)
1291ed29721eSTodd Kjos binder_dec_node(ref->node, strong, 1);
1292777783e0SGreg Kroah-Hartman } else {
1293372e3147STodd Kjos if (ref->data.weak == 0) {
1294777783e0SGreg Kroah-Hartman binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
1295372e3147STodd Kjos ref->proc->pid, ref->data.debug_id,
1296372e3147STodd Kjos ref->data.desc, ref->data.strong,
1297372e3147STodd Kjos ref->data.weak);
1298372e3147STodd Kjos return false;
1299777783e0SGreg Kroah-Hartman }
1300372e3147STodd Kjos ref->data.weak--;
1301777783e0SGreg Kroah-Hartman }
1302372e3147STodd Kjos if (ref->data.strong == 0 && ref->data.weak == 0) {
13032c1838dcSTodd Kjos binder_cleanup_ref_olocked(ref);
1304372e3147STodd Kjos return true;
1305372e3147STodd Kjos }
1306372e3147STodd Kjos return false;
1307372e3147STodd Kjos }
1308372e3147STodd Kjos
1309372e3147STodd Kjos /**
1310372e3147STodd Kjos * binder_get_node_from_ref() - get the node from the given proc/desc
1311372e3147STodd Kjos * @proc: proc containing the ref
1312372e3147STodd Kjos * @desc: the handle associated with the ref
1313372e3147STodd Kjos * @need_strong_ref: if true, only return node if ref is strong
1314372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1315372e3147STodd Kjos *
1316372e3147STodd Kjos * Given a proc and ref handle, return the associated binder_node
1317372e3147STodd Kjos *
1318372e3147STodd Kjos * Return: a binder_node or NULL if not found or not strong when strong required
1319372e3147STodd Kjos */
binder_get_node_from_ref(struct binder_proc * proc,u32 desc,bool need_strong_ref,struct binder_ref_data * rdata)1320372e3147STodd Kjos static struct binder_node *binder_get_node_from_ref(
1321372e3147STodd Kjos struct binder_proc *proc,
1322372e3147STodd Kjos u32 desc, bool need_strong_ref,
1323372e3147STodd Kjos struct binder_ref_data *rdata)
1324372e3147STodd Kjos {
1325372e3147STodd Kjos struct binder_node *node;
1326372e3147STodd Kjos struct binder_ref *ref;
1327372e3147STodd Kjos
13282c1838dcSTodd Kjos binder_proc_lock(proc);
13292c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
1330372e3147STodd Kjos if (!ref)
1331372e3147STodd Kjos goto err_no_ref;
1332372e3147STodd Kjos node = ref->node;
1333adc18842STodd Kjos /*
1334adc18842STodd Kjos * Take an implicit reference on the node to ensure
1335adc18842STodd Kjos * it stays alive until the call to binder_put_node()
1336adc18842STodd Kjos */
1337adc18842STodd Kjos binder_inc_node_tmpref(node);
1338372e3147STodd Kjos if (rdata)
1339372e3147STodd Kjos *rdata = ref->data;
13402c1838dcSTodd Kjos binder_proc_unlock(proc);
1341372e3147STodd Kjos
1342372e3147STodd Kjos return node;
1343372e3147STodd Kjos
1344372e3147STodd Kjos err_no_ref:
13452c1838dcSTodd Kjos binder_proc_unlock(proc);
1346372e3147STodd Kjos return NULL;
1347372e3147STodd Kjos }
1348372e3147STodd Kjos
1349372e3147STodd Kjos /**
1350372e3147STodd Kjos * binder_free_ref() - free the binder_ref
1351372e3147STodd Kjos * @ref: ref to free
1352372e3147STodd Kjos *
1353ed29721eSTodd Kjos * Free the binder_ref. Free the binder_node indicated by ref->node
1354ed29721eSTodd Kjos * (if non-NULL) and the binder_ref_death indicated by ref->death.
1355372e3147STodd Kjos */
binder_free_ref(struct binder_ref * ref)1356372e3147STodd Kjos static void binder_free_ref(struct binder_ref *ref)
1357372e3147STodd Kjos {
1358ed29721eSTodd Kjos if (ref->node)
1359ed29721eSTodd Kjos binder_free_node(ref->node);
1360372e3147STodd Kjos kfree(ref->death);
1361d579b04aSYu-Ting Tseng kfree(ref->freeze);
1362372e3147STodd Kjos kfree(ref);
1363372e3147STodd Kjos }
1364372e3147STodd Kjos
136515d9da3fSCarlos Llamas /* shrink descriptor bitmap if needed */
try_shrink_dmap(struct binder_proc * proc)136615d9da3fSCarlos Llamas static void try_shrink_dmap(struct binder_proc *proc)
136715d9da3fSCarlos Llamas {
136815d9da3fSCarlos Llamas unsigned long *new;
136915d9da3fSCarlos Llamas int nbits;
137015d9da3fSCarlos Llamas
137115d9da3fSCarlos Llamas binder_proc_lock(proc);
137215d9da3fSCarlos Llamas nbits = dbitmap_shrink_nbits(&proc->dmap);
137315d9da3fSCarlos Llamas binder_proc_unlock(proc);
137415d9da3fSCarlos Llamas
137515d9da3fSCarlos Llamas if (!nbits)
137615d9da3fSCarlos Llamas return;
137715d9da3fSCarlos Llamas
137815d9da3fSCarlos Llamas new = bitmap_zalloc(nbits, GFP_KERNEL);
137915d9da3fSCarlos Llamas binder_proc_lock(proc);
138015d9da3fSCarlos Llamas dbitmap_shrink(&proc->dmap, new, nbits);
138115d9da3fSCarlos Llamas binder_proc_unlock(proc);
138215d9da3fSCarlos Llamas }
138315d9da3fSCarlos Llamas
1384372e3147STodd Kjos /**
1385372e3147STodd Kjos * binder_update_ref_for_handle() - inc/dec the ref for given handle
1386372e3147STodd Kjos * @proc: proc containing the ref
1387372e3147STodd Kjos * @desc: the handle associated with the ref
1388372e3147STodd Kjos * @increment: true=inc reference, false=dec reference
1389372e3147STodd Kjos * @strong: true=strong reference, false=weak reference
1390372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1391372e3147STodd Kjos *
1392372e3147STodd Kjos * Given a proc and ref handle, increment or decrement the ref
1393372e3147STodd Kjos * according to "increment" arg.
1394372e3147STodd Kjos *
1395372e3147STodd Kjos * Return: 0 if successful, else errno
1396372e3147STodd Kjos */
binder_update_ref_for_handle(struct binder_proc * proc,uint32_t desc,bool increment,bool strong,struct binder_ref_data * rdata)1397372e3147STodd Kjos static int binder_update_ref_for_handle(struct binder_proc *proc,
1398372e3147STodd Kjos uint32_t desc, bool increment, bool strong,
1399372e3147STodd Kjos struct binder_ref_data *rdata)
1400372e3147STodd Kjos {
1401372e3147STodd Kjos int ret = 0;
1402372e3147STodd Kjos struct binder_ref *ref;
1403372e3147STodd Kjos bool delete_ref = false;
1404372e3147STodd Kjos
14052c1838dcSTodd Kjos binder_proc_lock(proc);
14062c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, desc, strong);
1407372e3147STodd Kjos if (!ref) {
1408372e3147STodd Kjos ret = -EINVAL;
1409372e3147STodd Kjos goto err_no_ref;
1410372e3147STodd Kjos }
1411372e3147STodd Kjos if (increment)
14122c1838dcSTodd Kjos ret = binder_inc_ref_olocked(ref, strong, NULL);
1413372e3147STodd Kjos else
14142c1838dcSTodd Kjos delete_ref = binder_dec_ref_olocked(ref, strong);
1415372e3147STodd Kjos
1416372e3147STodd Kjos if (rdata)
1417372e3147STodd Kjos *rdata = ref->data;
14182c1838dcSTodd Kjos binder_proc_unlock(proc);
1419372e3147STodd Kjos
142015d9da3fSCarlos Llamas if (delete_ref) {
1421372e3147STodd Kjos binder_free_ref(ref);
142215d9da3fSCarlos Llamas try_shrink_dmap(proc);
142315d9da3fSCarlos Llamas }
1424372e3147STodd Kjos return ret;
1425372e3147STodd Kjos
1426372e3147STodd Kjos err_no_ref:
14272c1838dcSTodd Kjos binder_proc_unlock(proc);
1428372e3147STodd Kjos return ret;
1429372e3147STodd Kjos }
1430372e3147STodd Kjos
1431372e3147STodd Kjos /**
1432372e3147STodd Kjos * binder_dec_ref_for_handle() - dec the ref for given handle
1433372e3147STodd Kjos * @proc: proc containing the ref
1434372e3147STodd Kjos * @desc: the handle associated with the ref
1435372e3147STodd Kjos * @strong: true=strong reference, false=weak reference
1436372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1437372e3147STodd Kjos *
1438372e3147STodd Kjos * Just calls binder_update_ref_for_handle() to decrement the ref.
1439372e3147STodd Kjos *
1440372e3147STodd Kjos * Return: 0 if successful, else errno
1441372e3147STodd Kjos */
binder_dec_ref_for_handle(struct binder_proc * proc,uint32_t desc,bool strong,struct binder_ref_data * rdata)1442372e3147STodd Kjos static int binder_dec_ref_for_handle(struct binder_proc *proc,
1443372e3147STodd Kjos uint32_t desc, bool strong, struct binder_ref_data *rdata)
1444372e3147STodd Kjos {
1445372e3147STodd Kjos return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1446372e3147STodd Kjos }
1447372e3147STodd Kjos
1448372e3147STodd Kjos
1449372e3147STodd Kjos /**
1450372e3147STodd Kjos * binder_inc_ref_for_node() - increment the ref for given proc/node
1451372e3147STodd Kjos * @proc: proc containing the ref
1452372e3147STodd Kjos * @node: target node
1453372e3147STodd Kjos * @strong: true=strong reference, false=weak reference
1454372e3147STodd Kjos * @target_list: worklist to use if node is incremented
1455372e3147STodd Kjos * @rdata: the id/refcount data for the ref
1456372e3147STodd Kjos *
1457372e3147STodd Kjos * Given a proc and node, increment the ref. Create the ref if it
1458372e3147STodd Kjos * doesn't already exist
1459372e3147STodd Kjos *
1460372e3147STodd Kjos * Return: 0 if successful, else errno
1461372e3147STodd 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)1462372e3147STodd Kjos static int binder_inc_ref_for_node(struct binder_proc *proc,
1463372e3147STodd Kjos struct binder_node *node,
1464372e3147STodd Kjos bool strong,
1465372e3147STodd Kjos struct list_head *target_list,
1466372e3147STodd Kjos struct binder_ref_data *rdata)
1467372e3147STodd Kjos {
1468372e3147STodd Kjos struct binder_ref *ref;
1469372e3147STodd Kjos struct binder_ref *new_ref = NULL;
1470372e3147STodd Kjos int ret = 0;
1471372e3147STodd Kjos
14722c1838dcSTodd Kjos binder_proc_lock(proc);
14732c1838dcSTodd Kjos ref = binder_get_ref_for_node_olocked(proc, node, NULL);
1474372e3147STodd Kjos if (!ref) {
14752c1838dcSTodd Kjos binder_proc_unlock(proc);
1476372e3147STodd Kjos new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1477372e3147STodd Kjos if (!new_ref)
1478372e3147STodd Kjos return -ENOMEM;
14792c1838dcSTodd Kjos binder_proc_lock(proc);
14802c1838dcSTodd Kjos ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
1481372e3147STodd Kjos }
14822c1838dcSTodd Kjos ret = binder_inc_ref_olocked(ref, strong, target_list);
1483372e3147STodd Kjos *rdata = ref->data;
1484a0e44c64SCarlos Llamas if (ret && ref == new_ref) {
1485a0e44c64SCarlos Llamas /*
1486a0e44c64SCarlos Llamas * Cleanup the failed reference here as the target
1487a0e44c64SCarlos Llamas * could now be dead and have already released its
1488a0e44c64SCarlos Llamas * references by now. Calling on the new reference
1489a0e44c64SCarlos Llamas * with strong=0 and a tmp_refs will not decrement
1490a0e44c64SCarlos Llamas * the node. The new_ref gets kfree'd below.
1491a0e44c64SCarlos Llamas */
1492a0e44c64SCarlos Llamas binder_cleanup_ref_olocked(new_ref);
1493a0e44c64SCarlos Llamas ref = NULL;
1494a0e44c64SCarlos Llamas }
1495a0e44c64SCarlos Llamas
14962c1838dcSTodd Kjos binder_proc_unlock(proc);
1497372e3147STodd Kjos if (new_ref && ref != new_ref)
1498372e3147STodd Kjos /*
1499372e3147STodd Kjos * Another thread created the ref first so
1500372e3147STodd Kjos * free the one we allocated
1501372e3147STodd Kjos */
1502372e3147STodd Kjos kfree(new_ref);
1503372e3147STodd Kjos return ret;
1504777783e0SGreg Kroah-Hartman }
1505777783e0SGreg Kroah-Hartman
binder_pop_transaction_ilocked(struct binder_thread * target_thread,struct binder_transaction * t)15060b89d69aSMartijn Coenen static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1507777783e0SGreg Kroah-Hartman struct binder_transaction *t)
1508777783e0SGreg Kroah-Hartman {
1509b6d282ceSTodd Kjos BUG_ON(!target_thread);
1510858b2719SMartijn Coenen assert_spin_locked(&target_thread->proc->inner_lock);
1511777783e0SGreg Kroah-Hartman BUG_ON(target_thread->transaction_stack != t);
1512777783e0SGreg Kroah-Hartman BUG_ON(target_thread->transaction_stack->from != target_thread);
1513777783e0SGreg Kroah-Hartman target_thread->transaction_stack =
1514777783e0SGreg Kroah-Hartman target_thread->transaction_stack->from_parent;
1515777783e0SGreg Kroah-Hartman t->from = NULL;
1516777783e0SGreg Kroah-Hartman }
1517b6d282ceSTodd Kjos
15187a4408c6STodd Kjos /**
15197a4408c6STodd Kjos * binder_thread_dec_tmpref() - decrement thread->tmp_ref
15207a4408c6STodd Kjos * @thread: thread to decrement
15217a4408c6STodd Kjos *
15227a4408c6STodd Kjos * A thread needs to be kept alive while being used to create or
15237a4408c6STodd Kjos * handle a transaction. binder_get_txn_from() is used to safely
15247a4408c6STodd Kjos * extract t->from from a binder_transaction and keep the thread
15257a4408c6STodd Kjos * indicated by t->from from being freed. When done with that
15267a4408c6STodd Kjos * binder_thread, this function is called to decrement the
15277a4408c6STodd Kjos * tmp_ref and free if appropriate (thread has been released
15287a4408c6STodd Kjos * and no transaction being processed by the driver)
15297a4408c6STodd Kjos */
binder_thread_dec_tmpref(struct binder_thread * thread)15307a4408c6STodd Kjos static void binder_thread_dec_tmpref(struct binder_thread *thread)
15317a4408c6STodd Kjos {
15327a4408c6STodd Kjos /*
15337a4408c6STodd Kjos * atomic is used to protect the counter value while
15347a4408c6STodd Kjos * it cannot reach zero or thread->is_dead is false
15357a4408c6STodd Kjos */
15367bd7b0e6STodd Kjos binder_inner_proc_lock(thread->proc);
15377a4408c6STodd Kjos atomic_dec(&thread->tmp_ref);
15387a4408c6STodd Kjos if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
15397bd7b0e6STodd Kjos binder_inner_proc_unlock(thread->proc);
15407a4408c6STodd Kjos binder_free_thread(thread);
15417a4408c6STodd Kjos return;
15427a4408c6STodd Kjos }
15437bd7b0e6STodd Kjos binder_inner_proc_unlock(thread->proc);
15447a4408c6STodd Kjos }
15457a4408c6STodd Kjos
15467a4408c6STodd Kjos /**
15477a4408c6STodd Kjos * binder_proc_dec_tmpref() - decrement proc->tmp_ref
15487a4408c6STodd Kjos * @proc: proc to decrement
15497a4408c6STodd Kjos *
15507a4408c6STodd Kjos * A binder_proc needs to be kept alive while being used to create or
15517a4408c6STodd Kjos * handle a transaction. proc->tmp_ref is incremented when
15527a4408c6STodd Kjos * creating a new transaction or the binder_proc is currently in-use
15537a4408c6STodd Kjos * by threads that are being released. When done with the binder_proc,
15547a4408c6STodd Kjos * this function is called to decrement the counter and free the
15557a4408c6STodd Kjos * proc if appropriate (proc has been released, all threads have
155659d617dcSRuffalo Lavoisier * been released and not currently in-use to process a transaction).
15577a4408c6STodd Kjos */
binder_proc_dec_tmpref(struct binder_proc * proc)15587a4408c6STodd Kjos static void binder_proc_dec_tmpref(struct binder_proc *proc)
15597a4408c6STodd Kjos {
15607bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
15617a4408c6STodd Kjos proc->tmp_ref--;
15627a4408c6STodd Kjos if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
15637a4408c6STodd Kjos !proc->tmp_ref) {
15647bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
15657a4408c6STodd Kjos binder_free_proc(proc);
15667a4408c6STodd Kjos return;
15677a4408c6STodd Kjos }
15687bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
15697a4408c6STodd Kjos }
15707a4408c6STodd Kjos
15717a4408c6STodd Kjos /**
15727a4408c6STodd Kjos * binder_get_txn_from() - safely extract the "from" thread in transaction
15737a4408c6STodd Kjos * @t: binder transaction for t->from
15747a4408c6STodd Kjos *
15757a4408c6STodd Kjos * Atomically return the "from" thread and increment the tmp_ref
15767a4408c6STodd Kjos * count for the thread to ensure it stays alive until
15777a4408c6STodd Kjos * binder_thread_dec_tmpref() is called.
15787a4408c6STodd Kjos *
15797a4408c6STodd Kjos * Return: the value of t->from
15807a4408c6STodd Kjos */
binder_get_txn_from(struct binder_transaction * t)15817a4408c6STodd Kjos static struct binder_thread *binder_get_txn_from(
15827a4408c6STodd Kjos struct binder_transaction *t)
15837a4408c6STodd Kjos {
15847a4408c6STodd Kjos struct binder_thread *from;
15857a4408c6STodd Kjos
15867a4408c6STodd Kjos spin_lock(&t->lock);
15877a4408c6STodd Kjos from = t->from;
15887a4408c6STodd Kjos if (from)
15897a4408c6STodd Kjos atomic_inc(&from->tmp_ref);
15907a4408c6STodd Kjos spin_unlock(&t->lock);
15917a4408c6STodd Kjos return from;
15927a4408c6STodd Kjos }
15937a4408c6STodd Kjos
15940b89d69aSMartijn Coenen /**
15950b89d69aSMartijn Coenen * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
15960b89d69aSMartijn Coenen * @t: binder transaction for t->from
15970b89d69aSMartijn Coenen *
15980b89d69aSMartijn Coenen * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
15990b89d69aSMartijn Coenen * to guarantee that the thread cannot be released while operating on it.
16000b89d69aSMartijn Coenen * The caller must call binder_inner_proc_unlock() to release the inner lock
16010b89d69aSMartijn Coenen * as well as call binder_dec_thread_txn() to release the reference.
16020b89d69aSMartijn Coenen *
16030b89d69aSMartijn Coenen * Return: the value of t->from
16040b89d69aSMartijn Coenen */
binder_get_txn_from_and_acq_inner(struct binder_transaction * t)16050b89d69aSMartijn Coenen static struct binder_thread *binder_get_txn_from_and_acq_inner(
16060b89d69aSMartijn Coenen struct binder_transaction *t)
1607324fa64cSTodd Kjos __acquires(&t->from->proc->inner_lock)
16080b89d69aSMartijn Coenen {
16090b89d69aSMartijn Coenen struct binder_thread *from;
16100b89d69aSMartijn Coenen
16110b89d69aSMartijn Coenen from = binder_get_txn_from(t);
1612324fa64cSTodd Kjos if (!from) {
1613324fa64cSTodd Kjos __acquire(&from->proc->inner_lock);
16140b89d69aSMartijn Coenen return NULL;
1615324fa64cSTodd Kjos }
16160b89d69aSMartijn Coenen binder_inner_proc_lock(from->proc);
16170b89d69aSMartijn Coenen if (t->from) {
16180b89d69aSMartijn Coenen BUG_ON(from != t->from);
16190b89d69aSMartijn Coenen return from;
16200b89d69aSMartijn Coenen }
16210b89d69aSMartijn Coenen binder_inner_proc_unlock(from->proc);
1622324fa64cSTodd Kjos __acquire(&from->proc->inner_lock);
16230b89d69aSMartijn Coenen binder_thread_dec_tmpref(from);
16240b89d69aSMartijn Coenen return NULL;
16250b89d69aSMartijn Coenen }
16260b89d69aSMartijn Coenen
162744d8047fSTodd Kjos /**
162844d8047fSTodd Kjos * binder_free_txn_fixups() - free unprocessed fd fixups
162944d8047fSTodd Kjos * @t: binder transaction for t->from
163044d8047fSTodd Kjos *
163144d8047fSTodd Kjos * If the transaction is being torn down prior to being
163244d8047fSTodd Kjos * processed by the target process, free all of the
163344d8047fSTodd Kjos * fd fixups and fput the file structs. It is safe to
163444d8047fSTodd Kjos * call this function after the fixups have been
163544d8047fSTodd Kjos * processed -- in that case, the list will be empty.
163644d8047fSTodd Kjos */
binder_free_txn_fixups(struct binder_transaction * t)163744d8047fSTodd Kjos static void binder_free_txn_fixups(struct binder_transaction *t)
163844d8047fSTodd Kjos {
163944d8047fSTodd Kjos struct binder_txn_fd_fixup *fixup, *tmp;
164044d8047fSTodd Kjos
164144d8047fSTodd Kjos list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
164244d8047fSTodd Kjos fput(fixup->file);
1643a8a570c6SCarlos Llamas if (fixup->target_fd >= 0)
1644a8a570c6SCarlos Llamas put_unused_fd(fixup->target_fd);
164544d8047fSTodd Kjos list_del(&fixup->fixup_entry);
164644d8047fSTodd Kjos kfree(fixup);
164744d8047fSTodd Kjos }
164844d8047fSTodd Kjos }
164944d8047fSTodd Kjos
binder_txn_latency_free(struct binder_transaction * t)16501987f112SFrankie.Chang static void binder_txn_latency_free(struct binder_transaction *t)
16511987f112SFrankie.Chang {
16521987f112SFrankie.Chang int from_proc, from_thread, to_proc, to_thread;
16531987f112SFrankie.Chang
16541987f112SFrankie.Chang spin_lock(&t->lock);
16551987f112SFrankie.Chang from_proc = t->from ? t->from->proc->pid : 0;
16561987f112SFrankie.Chang from_thread = t->from ? t->from->pid : 0;
16571987f112SFrankie.Chang to_proc = t->to_proc ? t->to_proc->pid : 0;
16581987f112SFrankie.Chang to_thread = t->to_thread ? t->to_thread->pid : 0;
16591987f112SFrankie.Chang spin_unlock(&t->lock);
16601987f112SFrankie.Chang
16611987f112SFrankie.Chang trace_binder_txn_latency_free(t, from_proc, from_thread, to_proc, to_thread);
16621987f112SFrankie.Chang }
16631987f112SFrankie.Chang
binder_free_transaction(struct binder_transaction * t)1664b6d282ceSTodd Kjos static void binder_free_transaction(struct binder_transaction *t)
1665b6d282ceSTodd Kjos {
1666a370003cSTodd Kjos struct binder_proc *target_proc = t->to_proc;
1667a370003cSTodd Kjos
1668a370003cSTodd Kjos if (target_proc) {
1669a370003cSTodd Kjos binder_inner_proc_lock(target_proc);
1670432ff1e9SMarco Ballesio target_proc->outstanding_txns--;
1671432ff1e9SMarco Ballesio if (target_proc->outstanding_txns < 0)
1672432ff1e9SMarco Ballesio pr_warn("%s: Unexpected outstanding_txns %d\n",
1673432ff1e9SMarco Ballesio __func__, target_proc->outstanding_txns);
1674432ff1e9SMarco Ballesio if (!target_proc->outstanding_txns && target_proc->is_frozen)
1675432ff1e9SMarco Ballesio wake_up_interruptible_all(&target_proc->freeze_wait);
1676777783e0SGreg Kroah-Hartman if (t->buffer)
1677777783e0SGreg Kroah-Hartman t->buffer->transaction = NULL;
1678a370003cSTodd Kjos binder_inner_proc_unlock(target_proc);
1679a370003cSTodd Kjos }
16801987f112SFrankie.Chang if (trace_binder_txn_latency_free_enabled())
16811987f112SFrankie.Chang binder_txn_latency_free(t);
1682a370003cSTodd Kjos /*
1683a370003cSTodd Kjos * If the transaction has no target_proc, then
1684a370003cSTodd Kjos * t->buffer->transaction has already been cleared.
1685a370003cSTodd Kjos */
168644d8047fSTodd Kjos binder_free_txn_fixups(t);
1687777783e0SGreg Kroah-Hartman kfree(t);
1688777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION);
1689777783e0SGreg Kroah-Hartman }
1690777783e0SGreg Kroah-Hartman
binder_send_failed_reply(struct binder_transaction * t,uint32_t error_code)1691777783e0SGreg Kroah-Hartman static void binder_send_failed_reply(struct binder_transaction *t,
1692777783e0SGreg Kroah-Hartman uint32_t error_code)
1693777783e0SGreg Kroah-Hartman {
1694777783e0SGreg Kroah-Hartman struct binder_thread *target_thread;
1695777783e0SGreg Kroah-Hartman struct binder_transaction *next;
1696777783e0SGreg Kroah-Hartman
1697777783e0SGreg Kroah-Hartman BUG_ON(t->flags & TF_ONE_WAY);
1698777783e0SGreg Kroah-Hartman while (1) {
16990b89d69aSMartijn Coenen target_thread = binder_get_txn_from_and_acq_inner(t);
1700777783e0SGreg Kroah-Hartman if (target_thread) {
1701777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1702777783e0SGreg Kroah-Hartman "send failed reply for transaction %d to %d:%d\n",
1703777783e0SGreg Kroah-Hartman t->debug_id,
1704777783e0SGreg Kroah-Hartman target_thread->proc->pid,
1705777783e0SGreg Kroah-Hartman target_thread->pid);
1706777783e0SGreg Kroah-Hartman
17070b89d69aSMartijn Coenen binder_pop_transaction_ilocked(target_thread, t);
170826549d17STodd Kjos if (target_thread->reply_error.cmd == BR_OK) {
170926549d17STodd Kjos target_thread->reply_error.cmd = error_code;
1710148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(
1711148ade2cSMartijn Coenen target_thread,
1712148ade2cSMartijn Coenen &target_thread->reply_error.work);
1713777783e0SGreg Kroah-Hartman wake_up_interruptible(&target_thread->wait);
1714777783e0SGreg Kroah-Hartman } else {
1715e46a3b3bSTodd Kjos /*
1716e46a3b3bSTodd Kjos * Cannot get here for normal operation, but
1717e46a3b3bSTodd Kjos * we can if multiple synchronous transactions
1718e46a3b3bSTodd Kjos * are sent without blocking for responses.
1719e46a3b3bSTodd Kjos * Just ignore the 2nd error in this case.
1720e46a3b3bSTodd Kjos */
1721e46a3b3bSTodd Kjos pr_warn("Unexpected reply error: %u\n",
172226549d17STodd Kjos target_thread->reply_error.cmd);
1723777783e0SGreg Kroah-Hartman }
17240b89d69aSMartijn Coenen binder_inner_proc_unlock(target_thread->proc);
17257a4408c6STodd Kjos binder_thread_dec_tmpref(target_thread);
172626549d17STodd Kjos binder_free_transaction(t);
1727777783e0SGreg Kroah-Hartman return;
1728777783e0SGreg Kroah-Hartman }
172972b93c79SMrinal Pandey __release(&target_thread->proc->inner_lock);
1730777783e0SGreg Kroah-Hartman next = t->from_parent;
1731777783e0SGreg Kroah-Hartman
1732777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1733777783e0SGreg Kroah-Hartman "send failed reply for transaction %d, target dead\n",
1734777783e0SGreg Kroah-Hartman t->debug_id);
1735777783e0SGreg Kroah-Hartman
1736b6d282ceSTodd Kjos binder_free_transaction(t);
1737777783e0SGreg Kroah-Hartman if (next == NULL) {
1738777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
1739777783e0SGreg Kroah-Hartman "reply failed, no target thread at root\n");
1740777783e0SGreg Kroah-Hartman return;
1741777783e0SGreg Kroah-Hartman }
1742777783e0SGreg Kroah-Hartman t = next;
1743777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
1744777783e0SGreg Kroah-Hartman "reply failed, no target thread -- retry %d\n",
1745777783e0SGreg Kroah-Hartman t->debug_id);
1746777783e0SGreg Kroah-Hartman }
1747777783e0SGreg Kroah-Hartman }
1748777783e0SGreg Kroah-Hartman
1749feba3900SMartijn Coenen /**
1750fb2c4452SMartijn Coenen * binder_cleanup_transaction() - cleans up undelivered transaction
1751fb2c4452SMartijn Coenen * @t: transaction that needs to be cleaned up
1752fb2c4452SMartijn Coenen * @reason: reason the transaction wasn't delivered
1753fb2c4452SMartijn Coenen * @error_code: error to return to caller (if synchronous call)
1754fb2c4452SMartijn Coenen */
binder_cleanup_transaction(struct binder_transaction * t,const char * reason,uint32_t error_code)1755fb2c4452SMartijn Coenen static void binder_cleanup_transaction(struct binder_transaction *t,
1756fb2c4452SMartijn Coenen const char *reason,
1757fb2c4452SMartijn Coenen uint32_t error_code)
1758fb2c4452SMartijn Coenen {
1759fb2c4452SMartijn Coenen if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
1760fb2c4452SMartijn Coenen binder_send_failed_reply(t, error_code);
1761fb2c4452SMartijn Coenen } else {
1762fb2c4452SMartijn Coenen binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
1763fb2c4452SMartijn Coenen "undelivered transaction %d, %s\n",
1764fb2c4452SMartijn Coenen t->debug_id, reason);
1765fb2c4452SMartijn Coenen binder_free_transaction(t);
1766fb2c4452SMartijn Coenen }
1767fb2c4452SMartijn Coenen }
1768fb2c4452SMartijn Coenen
1769fb2c4452SMartijn Coenen /**
17707a67a393STodd Kjos * binder_get_object() - gets object and checks for valid metadata
17717a67a393STodd Kjos * @proc: binder_proc owning the buffer
17726d98eb95STodd Kjos * @u: sender's user pointer to base of buffer
1773feba3900SMartijn Coenen * @buffer: binder_buffer that we're parsing.
17747a67a393STodd Kjos * @offset: offset in the @buffer at which to validate an object.
17757a67a393STodd Kjos * @object: struct binder_object to read into
1776feba3900SMartijn Coenen *
17776d98eb95STodd Kjos * Copy the binder object at the given offset into @object. If @u is
17786d98eb95STodd Kjos * provided then the copy is from the sender's buffer. If not, then
17796d98eb95STodd Kjos * it is copied from the target's @buffer.
17806d98eb95STodd Kjos *
17816d98eb95STodd Kjos * Return: If there's a valid metadata object at @offset, the
17827a67a393STodd Kjos * size of that object. Otherwise, it returns zero. The object
17837a67a393STodd Kjos * is read into the struct binder_object pointed to by @object.
1784feba3900SMartijn Coenen */
binder_get_object(struct binder_proc * proc,const void __user * u,struct binder_buffer * buffer,unsigned long offset,struct binder_object * object)17857a67a393STodd Kjos static size_t binder_get_object(struct binder_proc *proc,
17866d98eb95STodd Kjos const void __user *u,
17877a67a393STodd Kjos struct binder_buffer *buffer,
17887a67a393STodd Kjos unsigned long offset,
17897a67a393STodd Kjos struct binder_object *object)
1790feba3900SMartijn Coenen {
17917a67a393STodd Kjos size_t read_size;
1792feba3900SMartijn Coenen struct binder_object_header *hdr;
1793feba3900SMartijn Coenen size_t object_size = 0;
1794feba3900SMartijn Coenen
17957a67a393STodd Kjos read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
1796aaef7382SCarlos Llamas if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
1797aaef7382SCarlos Llamas !IS_ALIGNED(offset, sizeof(u32)))
17986d98eb95STodd Kjos return 0;
1799aaef7382SCarlos Llamas
18006d98eb95STodd Kjos if (u) {
18016d98eb95STodd Kjos if (copy_from_user(object, u + offset, read_size))
18026d98eb95STodd Kjos return 0;
18036d98eb95STodd Kjos } else {
18046d98eb95STodd Kjos if (binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
1805bb4a2e48STodd Kjos offset, read_size))
1806bb4a2e48STodd Kjos return 0;
18076d98eb95STodd Kjos }
1808feba3900SMartijn Coenen
18097a67a393STodd Kjos /* Ok, now see if we read a complete object. */
18107a67a393STodd Kjos hdr = &object->hdr;
1811feba3900SMartijn Coenen switch (hdr->type) {
1812feba3900SMartijn Coenen case BINDER_TYPE_BINDER:
1813feba3900SMartijn Coenen case BINDER_TYPE_WEAK_BINDER:
1814feba3900SMartijn Coenen case BINDER_TYPE_HANDLE:
1815feba3900SMartijn Coenen case BINDER_TYPE_WEAK_HANDLE:
1816feba3900SMartijn Coenen object_size = sizeof(struct flat_binder_object);
1817feba3900SMartijn Coenen break;
1818feba3900SMartijn Coenen case BINDER_TYPE_FD:
1819feba3900SMartijn Coenen object_size = sizeof(struct binder_fd_object);
1820feba3900SMartijn Coenen break;
18217980240bSMartijn Coenen case BINDER_TYPE_PTR:
18227980240bSMartijn Coenen object_size = sizeof(struct binder_buffer_object);
18237980240bSMartijn Coenen break;
1824def95c73SMartijn Coenen case BINDER_TYPE_FDA:
1825def95c73SMartijn Coenen object_size = sizeof(struct binder_fd_array_object);
1826def95c73SMartijn Coenen break;
1827feba3900SMartijn Coenen default:
1828feba3900SMartijn Coenen return 0;
1829feba3900SMartijn Coenen }
1830feba3900SMartijn Coenen if (offset <= buffer->data_size - object_size &&
1831feba3900SMartijn Coenen buffer->data_size >= object_size)
1832feba3900SMartijn Coenen return object_size;
1833feba3900SMartijn Coenen else
1834feba3900SMartijn Coenen return 0;
1835feba3900SMartijn Coenen }
1836feba3900SMartijn Coenen
18377980240bSMartijn Coenen /**
18387980240bSMartijn Coenen * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1839db6b0b81STodd Kjos * @proc: binder_proc owning the buffer
18407980240bSMartijn Coenen * @b: binder_buffer containing the object
1841db6b0b81STodd Kjos * @object: struct binder_object to read into
18427980240bSMartijn Coenen * @index: index in offset array at which the binder_buffer_object is
18437980240bSMartijn Coenen * located
1844db6b0b81STodd Kjos * @start_offset: points to the start of the offset array
1845db6b0b81STodd Kjos * @object_offsetp: offset of @object read from @b
18467980240bSMartijn Coenen * @num_valid: the number of valid offsets in the offset array
18477980240bSMartijn Coenen *
18487980240bSMartijn Coenen * Return: If @index is within the valid range of the offset array
18497980240bSMartijn Coenen * described by @start and @num_valid, and if there's a valid
18507980240bSMartijn Coenen * binder_buffer_object at the offset found in index @index
18517980240bSMartijn Coenen * of the offset array, that object is returned. Otherwise,
18527980240bSMartijn Coenen * %NULL is returned.
18537980240bSMartijn Coenen * Note that the offset found in index @index itself is not
18547980240bSMartijn Coenen * verified; this function assumes that @num_valid elements
18557980240bSMartijn Coenen * from @start were previously verified to have valid offsets.
1856db6b0b81STodd Kjos * If @object_offsetp is non-NULL, then the offset within
1857db6b0b81STodd Kjos * @b is written to it.
18587980240bSMartijn 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)1859db6b0b81STodd Kjos static struct binder_buffer_object *binder_validate_ptr(
1860db6b0b81STodd Kjos struct binder_proc *proc,
1861db6b0b81STodd Kjos struct binder_buffer *b,
1862db6b0b81STodd Kjos struct binder_object *object,
18637980240bSMartijn Coenen binder_size_t index,
1864db6b0b81STodd Kjos binder_size_t start_offset,
1865db6b0b81STodd Kjos binder_size_t *object_offsetp,
18667980240bSMartijn Coenen binder_size_t num_valid)
18677980240bSMartijn Coenen {
1868db6b0b81STodd Kjos size_t object_size;
1869db6b0b81STodd Kjos binder_size_t object_offset;
1870db6b0b81STodd Kjos unsigned long buffer_offset;
18717980240bSMartijn Coenen
18727980240bSMartijn Coenen if (index >= num_valid)
18737980240bSMartijn Coenen return NULL;
18747980240bSMartijn Coenen
1875db6b0b81STodd Kjos buffer_offset = start_offset + sizeof(binder_size_t) * index;
1876bb4a2e48STodd Kjos if (binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
1877bb4a2e48STodd Kjos b, buffer_offset,
1878bb4a2e48STodd Kjos sizeof(object_offset)))
1879bb4a2e48STodd Kjos return NULL;
18806d98eb95STodd Kjos object_size = binder_get_object(proc, NULL, b, object_offset, object);
1881db6b0b81STodd Kjos if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
18827980240bSMartijn Coenen return NULL;
1883db6b0b81STodd Kjos if (object_offsetp)
1884db6b0b81STodd Kjos *object_offsetp = object_offset;
18857980240bSMartijn Coenen
1886db6b0b81STodd Kjos return &object->bbo;
18877980240bSMartijn Coenen }
18887980240bSMartijn Coenen
18897980240bSMartijn Coenen /**
18907980240bSMartijn Coenen * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1891db6b0b81STodd Kjos * @proc: binder_proc owning the buffer
18927980240bSMartijn Coenen * @b: transaction buffer
1893db6b0b81STodd Kjos * @objects_start_offset: offset to start of objects buffer
1894db6b0b81STodd Kjos * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
1895db6b0b81STodd Kjos * @fixup_offset: start offset in @buffer to fix up
1896db6b0b81STodd Kjos * @last_obj_offset: offset to last binder_buffer_object that we fixed
1897db6b0b81STodd Kjos * @last_min_offset: minimum fixup offset in object at @last_obj_offset
18987980240bSMartijn Coenen *
18997980240bSMartijn Coenen * Return: %true if a fixup in buffer @buffer at offset @offset is
19007980240bSMartijn Coenen * allowed.
19017980240bSMartijn Coenen *
19027980240bSMartijn Coenen * For safety reasons, we only allow fixups inside a buffer to happen
19037980240bSMartijn Coenen * at increasing offsets; additionally, we only allow fixup on the last
19047980240bSMartijn Coenen * buffer object that was verified, or one of its parents.
19057980240bSMartijn Coenen *
19067980240bSMartijn Coenen * Example of what is allowed:
19077980240bSMartijn Coenen *
19087980240bSMartijn Coenen * A
19097980240bSMartijn Coenen * B (parent = A, offset = 0)
19107980240bSMartijn Coenen * C (parent = A, offset = 16)
19117980240bSMartijn Coenen * D (parent = C, offset = 0)
19127980240bSMartijn Coenen * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
19137980240bSMartijn Coenen *
19147980240bSMartijn Coenen * Examples of what is not allowed:
19157980240bSMartijn Coenen *
19167980240bSMartijn Coenen * Decreasing offsets within the same parent:
19177980240bSMartijn Coenen * A
19187980240bSMartijn Coenen * C (parent = A, offset = 16)
19197980240bSMartijn Coenen * B (parent = A, offset = 0) // decreasing offset within A
19207980240bSMartijn Coenen *
19217980240bSMartijn Coenen * Referring to a parent that wasn't the last object or any of its parents:
19227980240bSMartijn Coenen * A
19237980240bSMartijn Coenen * B (parent = A, offset = 0)
19247980240bSMartijn Coenen * C (parent = A, offset = 0)
19257980240bSMartijn Coenen * C (parent = A, offset = 16)
19267980240bSMartijn Coenen * D (parent = B, offset = 0) // B is not A or any of A's parents
19277980240bSMartijn 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)1928db6b0b81STodd Kjos static bool binder_validate_fixup(struct binder_proc *proc,
1929db6b0b81STodd Kjos struct binder_buffer *b,
1930db6b0b81STodd Kjos binder_size_t objects_start_offset,
1931db6b0b81STodd Kjos binder_size_t buffer_obj_offset,
19327980240bSMartijn Coenen binder_size_t fixup_offset,
1933db6b0b81STodd Kjos binder_size_t last_obj_offset,
19347980240bSMartijn Coenen binder_size_t last_min_offset)
19357980240bSMartijn Coenen {
1936db6b0b81STodd Kjos if (!last_obj_offset) {
19377980240bSMartijn Coenen /* Nothing to fix up in */
19387980240bSMartijn Coenen return false;
19397980240bSMartijn Coenen }
19407980240bSMartijn Coenen
1941db6b0b81STodd Kjos while (last_obj_offset != buffer_obj_offset) {
1942db6b0b81STodd Kjos unsigned long buffer_offset;
1943db6b0b81STodd Kjos struct binder_object last_object;
1944db6b0b81STodd Kjos struct binder_buffer_object *last_bbo;
19456d98eb95STodd Kjos size_t object_size = binder_get_object(proc, NULL, b,
19466d98eb95STodd Kjos last_obj_offset,
1947db6b0b81STodd Kjos &last_object);
1948db6b0b81STodd Kjos if (object_size != sizeof(*last_bbo))
1949db6b0b81STodd Kjos return false;
1950db6b0b81STodd Kjos
1951db6b0b81STodd Kjos last_bbo = &last_object.bbo;
19527980240bSMartijn Coenen /*
19537980240bSMartijn Coenen * Safe to retrieve the parent of last_obj, since it
19547980240bSMartijn Coenen * was already previously verified by the driver.
19557980240bSMartijn Coenen */
1956db6b0b81STodd Kjos if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
19577980240bSMartijn Coenen return false;
1958db6b0b81STodd Kjos last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
1959db6b0b81STodd Kjos buffer_offset = objects_start_offset +
1960bb4a2e48STodd Kjos sizeof(binder_size_t) * last_bbo->parent;
1961bb4a2e48STodd Kjos if (binder_alloc_copy_from_buffer(&proc->alloc,
1962bb4a2e48STodd Kjos &last_obj_offset,
1963db6b0b81STodd Kjos b, buffer_offset,
1964bb4a2e48STodd Kjos sizeof(last_obj_offset)))
1965bb4a2e48STodd Kjos return false;
19667980240bSMartijn Coenen }
19677980240bSMartijn Coenen return (fixup_offset >= last_min_offset);
19687980240bSMartijn Coenen }
19697980240bSMartijn Coenen
197080cd7956STodd Kjos /**
197180cd7956STodd Kjos * struct binder_task_work_cb - for deferred close
197280cd7956STodd Kjos *
197380cd7956STodd Kjos * @twork: callback_head for task work
19742a8f84b5SCarlos Llamas * @file: file to close
197580cd7956STodd Kjos *
197680cd7956STodd Kjos * Structure to pass task work to be handled after
197780cd7956STodd Kjos * returning from binder_ioctl() via task_work_add().
197880cd7956STodd Kjos */
197980cd7956STodd Kjos struct binder_task_work_cb {
198080cd7956STodd Kjos struct callback_head twork;
198180cd7956STodd Kjos struct file *file;
198280cd7956STodd Kjos };
198380cd7956STodd Kjos
198480cd7956STodd Kjos /**
198580cd7956STodd Kjos * binder_do_fd_close() - close list of file descriptors
198680cd7956STodd Kjos * @twork: callback head for task work
198780cd7956STodd Kjos *
198880cd7956STodd Kjos * It is not safe to call ksys_close() during the binder_ioctl()
198980cd7956STodd Kjos * function if there is a chance that binder's own file descriptor
199080cd7956STodd Kjos * might be closed. This is to meet the requirements for using
199180cd7956STodd Kjos * fdget() (see comments for __fget_light()). Therefore use
199280cd7956STodd Kjos * task_work_add() to schedule the close operation once we have
199380cd7956STodd Kjos * returned from binder_ioctl(). This function is a callback
199480cd7956STodd Kjos * for that mechanism and does the actual ksys_close() on the
199580cd7956STodd Kjos * given file descriptor.
199680cd7956STodd Kjos */
binder_do_fd_close(struct callback_head * twork)199780cd7956STodd Kjos static void binder_do_fd_close(struct callback_head *twork)
199880cd7956STodd Kjos {
199980cd7956STodd Kjos struct binder_task_work_cb *twcb = container_of(twork,
200080cd7956STodd Kjos struct binder_task_work_cb, twork);
200180cd7956STodd Kjos
200280cd7956STodd Kjos fput(twcb->file);
200380cd7956STodd Kjos kfree(twcb);
200480cd7956STodd Kjos }
200580cd7956STodd Kjos
200680cd7956STodd Kjos /**
200780cd7956STodd Kjos * binder_deferred_fd_close() - schedule a close for the given file-descriptor
200880cd7956STodd Kjos * @fd: file-descriptor to close
200980cd7956STodd Kjos *
201080cd7956STodd Kjos * See comments in binder_do_fd_close(). This function is used to schedule
201180cd7956STodd Kjos * a file-descriptor to be closed after returning from binder_ioctl().
201280cd7956STodd Kjos */
binder_deferred_fd_close(int fd)201380cd7956STodd Kjos static void binder_deferred_fd_close(int fd)
201480cd7956STodd Kjos {
201580cd7956STodd Kjos struct binder_task_work_cb *twcb;
201680cd7956STodd Kjos
201780cd7956STodd Kjos twcb = kzalloc(sizeof(*twcb), GFP_KERNEL);
201880cd7956STodd Kjos if (!twcb)
201980cd7956STodd Kjos return;
202080cd7956STodd Kjos init_task_work(&twcb->twork, binder_do_fd_close);
2021a88c955fSChristian Brauner twcb->file = file_close_fd(fd);
20226e802a4bSJens Axboe if (twcb->file) {
202340a19260SAl Viro // pin it until binder_do_fd_close(); see comments there
202440a19260SAl Viro get_file(twcb->file);
20256e802a4bSJens Axboe filp_close(twcb->file, current->files);
202691989c70SJens Axboe task_work_add(current, &twcb->twork, TWA_RESUME);
20276e802a4bSJens Axboe } else {
202880cd7956STodd Kjos kfree(twcb);
202980cd7956STodd Kjos }
20306e802a4bSJens Axboe }
203180cd7956STodd 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)2032777783e0SGreg Kroah-Hartman static void binder_transaction_buffer_release(struct binder_proc *proc,
20335fdb55c1STodd Kjos struct binder_thread *thread,
2034777783e0SGreg Kroah-Hartman struct binder_buffer *buffer,
2035bdc1c5faSCarlos Llamas binder_size_t off_end_offset,
2036bde4a19fSTodd Kjos bool is_failure)
2037777783e0SGreg Kroah-Hartman {
2038777783e0SGreg Kroah-Hartman int debug_id = buffer->debug_id;
2039bdc1c5faSCarlos Llamas binder_size_t off_start_offset, buffer_offset;
2040777783e0SGreg Kroah-Hartman
2041777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
2042bde4a19fSTodd Kjos "%d buffer release %d, size %zd-%zd, failed at %llx\n",
2043777783e0SGreg Kroah-Hartman proc->pid, buffer->debug_id,
2044bde4a19fSTodd Kjos buffer->data_size, buffer->offsets_size,
2045bdc1c5faSCarlos Llamas (unsigned long long)off_end_offset);
2046777783e0SGreg Kroah-Hartman
2047777783e0SGreg Kroah-Hartman if (buffer->target_node)
2048777783e0SGreg Kroah-Hartman binder_dec_node(buffer->target_node, 1, 0);
2049777783e0SGreg Kroah-Hartman
2050db6b0b81STodd Kjos off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
2051bdc1c5faSCarlos Llamas
2052bde4a19fSTodd Kjos for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2053bde4a19fSTodd Kjos buffer_offset += sizeof(binder_size_t)) {
2054feba3900SMartijn Coenen struct binder_object_header *hdr;
2055bb4a2e48STodd Kjos size_t object_size = 0;
20567a67a393STodd Kjos struct binder_object object;
20578ced0c62STodd Kjos binder_size_t object_offset;
2058777783e0SGreg Kroah-Hartman
2059bb4a2e48STodd Kjos if (!binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
20608ced0c62STodd Kjos buffer, buffer_offset,
2061bb4a2e48STodd Kjos sizeof(object_offset)))
20626d98eb95STodd Kjos object_size = binder_get_object(proc, NULL, buffer,
20637a67a393STodd Kjos object_offset, &object);
2064feba3900SMartijn Coenen if (object_size == 0) {
2065feba3900SMartijn Coenen pr_err("transaction release %d bad object at offset %lld, size %zd\n",
20668ced0c62STodd Kjos debug_id, (u64)object_offset, buffer->data_size);
2067777783e0SGreg Kroah-Hartman continue;
2068777783e0SGreg Kroah-Hartman }
20697a67a393STodd Kjos hdr = &object.hdr;
2070feba3900SMartijn Coenen switch (hdr->type) {
2071777783e0SGreg Kroah-Hartman case BINDER_TYPE_BINDER:
2072777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_BINDER: {
2073feba3900SMartijn Coenen struct flat_binder_object *fp;
2074feba3900SMartijn Coenen struct binder_node *node;
2075777783e0SGreg Kroah-Hartman
2076feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
2077feba3900SMartijn Coenen node = binder_get_node(proc, fp->binder);
2078777783e0SGreg Kroah-Hartman if (node == NULL) {
2079777783e0SGreg Kroah-Hartman pr_err("transaction release %d bad node %016llx\n",
2080777783e0SGreg Kroah-Hartman debug_id, (u64)fp->binder);
2081777783e0SGreg Kroah-Hartman break;
2082777783e0SGreg Kroah-Hartman }
2083777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
2084777783e0SGreg Kroah-Hartman " node %d u%016llx\n",
2085777783e0SGreg Kroah-Hartman node->debug_id, (u64)node->ptr);
2086feba3900SMartijn Coenen binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2087feba3900SMartijn Coenen 0);
2088adc18842STodd Kjos binder_put_node(node);
2089777783e0SGreg Kroah-Hartman } break;
2090777783e0SGreg Kroah-Hartman case BINDER_TYPE_HANDLE:
2091777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_HANDLE: {
2092feba3900SMartijn Coenen struct flat_binder_object *fp;
2093372e3147STodd Kjos struct binder_ref_data rdata;
2094372e3147STodd Kjos int ret;
20950a3ffab9SArve Hjønnevåg
2096feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
2097372e3147STodd Kjos ret = binder_dec_ref_for_handle(proc, fp->handle,
2098372e3147STodd Kjos hdr->type == BINDER_TYPE_HANDLE, &rdata);
2099372e3147STodd Kjos
2100372e3147STodd Kjos if (ret) {
2101372e3147STodd Kjos pr_err("transaction release %d bad handle %d, ret = %d\n",
2102372e3147STodd Kjos debug_id, fp->handle, ret);
2103777783e0SGreg Kroah-Hartman break;
2104777783e0SGreg Kroah-Hartman }
2105777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
2106372e3147STodd Kjos " ref %d desc %d\n",
2107372e3147STodd Kjos rdata.debug_id, rdata.desc);
2108777783e0SGreg Kroah-Hartman } break;
2109777783e0SGreg Kroah-Hartman
2110feba3900SMartijn Coenen case BINDER_TYPE_FD: {
211144d8047fSTodd Kjos /*
211244d8047fSTodd Kjos * No need to close the file here since user-space
2113690cfa20SAjith P V * closes it for successfully delivered
211444d8047fSTodd Kjos * transactions. For transactions that weren't
211544d8047fSTodd Kjos * delivered, the new fd was never allocated so
211644d8047fSTodd Kjos * there is no need to close and the fput on the
211744d8047fSTodd Kjos * file is done when the transaction is torn
211844d8047fSTodd Kjos * down.
211944d8047fSTodd Kjos */
2120feba3900SMartijn Coenen } break;
21217980240bSMartijn Coenen case BINDER_TYPE_PTR:
21227980240bSMartijn Coenen /*
21237980240bSMartijn Coenen * Nothing to do here, this will get cleaned up when the
21247980240bSMartijn Coenen * transaction buffer gets freed
21257980240bSMartijn Coenen */
21267980240bSMartijn Coenen break;
2127def95c73SMartijn Coenen case BINDER_TYPE_FDA: {
2128def95c73SMartijn Coenen struct binder_fd_array_object *fda;
2129def95c73SMartijn Coenen struct binder_buffer_object *parent;
2130db6b0b81STodd Kjos struct binder_object ptr_object;
2131bde4a19fSTodd Kjos binder_size_t fda_offset;
2132def95c73SMartijn Coenen size_t fd_index;
2133def95c73SMartijn Coenen binder_size_t fd_buf_size;
2134bde4a19fSTodd Kjos binder_size_t num_valid;
2135def95c73SMartijn Coenen
213632e9f56aSTodd Kjos if (is_failure) {
213744d8047fSTodd Kjos /*
213844d8047fSTodd Kjos * The fd fixups have not been applied so no
213944d8047fSTodd Kjos * fds need to be closed.
214044d8047fSTodd Kjos */
214144d8047fSTodd Kjos continue;
214244d8047fSTodd Kjos }
214344d8047fSTodd Kjos
2144bde4a19fSTodd Kjos num_valid = (buffer_offset - off_start_offset) /
2145bde4a19fSTodd Kjos sizeof(binder_size_t);
2146def95c73SMartijn Coenen fda = to_binder_fd_array_object(hdr);
2147db6b0b81STodd Kjos parent = binder_validate_ptr(proc, buffer, &ptr_object,
2148db6b0b81STodd Kjos fda->parent,
2149db6b0b81STodd Kjos off_start_offset,
2150db6b0b81STodd Kjos NULL,
2151bde4a19fSTodd Kjos num_valid);
2152def95c73SMartijn Coenen if (!parent) {
2153f7f84fdeSArvind Yadav pr_err("transaction release %d bad parent offset\n",
2154def95c73SMartijn Coenen debug_id);
2155def95c73SMartijn Coenen continue;
2156def95c73SMartijn Coenen }
2157def95c73SMartijn Coenen fd_buf_size = sizeof(u32) * fda->num_fds;
2158def95c73SMartijn Coenen if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2159def95c73SMartijn Coenen pr_err("transaction release %d invalid number of fds (%lld)\n",
2160def95c73SMartijn Coenen debug_id, (u64)fda->num_fds);
2161def95c73SMartijn Coenen continue;
2162def95c73SMartijn Coenen }
2163def95c73SMartijn Coenen if (fd_buf_size > parent->length ||
2164def95c73SMartijn Coenen fda->parent_offset > parent->length - fd_buf_size) {
2165def95c73SMartijn Coenen /* No space for all file descriptors here. */
2166def95c73SMartijn Coenen pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2167def95c73SMartijn Coenen debug_id, (u64)fda->num_fds);
2168def95c73SMartijn Coenen continue;
2169def95c73SMartijn Coenen }
2170bde4a19fSTodd Kjos /*
2171bde4a19fSTodd Kjos * the source data for binder_buffer_object is visible
2172bde4a19fSTodd Kjos * to user-space and the @buffer element is the user
2173bde4a19fSTodd Kjos * pointer to the buffer_object containing the fd_array.
2174bde4a19fSTodd Kjos * Convert the address to an offset relative to
2175bde4a19fSTodd Kjos * the base of the transaction buffer.
2176bde4a19fSTodd Kjos */
2177df9aabeaSCarlos Llamas fda_offset = parent->buffer - buffer->user_data +
2178bde4a19fSTodd Kjos fda->parent_offset;
21798ced0c62STodd Kjos for (fd_index = 0; fd_index < fda->num_fds;
21808ced0c62STodd Kjos fd_index++) {
21818ced0c62STodd Kjos u32 fd;
2182bb4a2e48STodd Kjos int err;
2183bde4a19fSTodd Kjos binder_size_t offset = fda_offset +
2184bde4a19fSTodd Kjos fd_index * sizeof(fd);
21858ced0c62STodd Kjos
2186bb4a2e48STodd Kjos err = binder_alloc_copy_from_buffer(
2187bb4a2e48STodd Kjos &proc->alloc, &fd, buffer,
2188bb4a2e48STodd Kjos offset, sizeof(fd));
2189bb4a2e48STodd Kjos WARN_ON(err);
21905fdb55c1STodd Kjos if (!err) {
21918ced0c62STodd Kjos binder_deferred_fd_close(fd);
21925fdb55c1STodd Kjos /*
21935fdb55c1STodd Kjos * Need to make sure the thread goes
21945fdb55c1STodd Kjos * back to userspace to complete the
21955fdb55c1STodd Kjos * deferred close
21965fdb55c1STodd Kjos */
21975fdb55c1STodd Kjos if (thread)
21985fdb55c1STodd Kjos thread->looper_need_return = true;
21995fdb55c1STodd Kjos }
22008ced0c62STodd Kjos }
2201def95c73SMartijn Coenen } break;
2202777783e0SGreg Kroah-Hartman default:
2203777783e0SGreg Kroah-Hartman pr_err("transaction release %d bad object type %x\n",
2204feba3900SMartijn Coenen debug_id, hdr->type);
2205777783e0SGreg Kroah-Hartman break;
2206777783e0SGreg Kroah-Hartman }
2207777783e0SGreg Kroah-Hartman }
2208777783e0SGreg Kroah-Hartman }
2209777783e0SGreg Kroah-Hartman
2210bdc1c5faSCarlos 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)2211bdc1c5faSCarlos Llamas static inline void binder_release_entire_buffer(struct binder_proc *proc,
2212bdc1c5faSCarlos Llamas struct binder_thread *thread,
2213bdc1c5faSCarlos Llamas struct binder_buffer *buffer,
2214bdc1c5faSCarlos Llamas bool is_failure)
2215bdc1c5faSCarlos Llamas {
2216bdc1c5faSCarlos Llamas binder_size_t off_end_offset;
2217bdc1c5faSCarlos Llamas
2218bdc1c5faSCarlos Llamas off_end_offset = ALIGN(buffer->data_size, sizeof(void *));
2219bdc1c5faSCarlos Llamas off_end_offset += buffer->offsets_size;
2220bdc1c5faSCarlos Llamas
2221bdc1c5faSCarlos Llamas binder_transaction_buffer_release(proc, thread, buffer,
2222bdc1c5faSCarlos Llamas off_end_offset, is_failure);
2223bdc1c5faSCarlos Llamas }
2224bdc1c5faSCarlos Llamas
binder_translate_binder(struct flat_binder_object * fp,struct binder_transaction * t,struct binder_thread * thread)2225a056af42SMartijn Coenen static int binder_translate_binder(struct flat_binder_object *fp,
2226a056af42SMartijn Coenen struct binder_transaction *t,
2227a056af42SMartijn Coenen struct binder_thread *thread)
2228a056af42SMartijn Coenen {
2229a056af42SMartijn Coenen struct binder_node *node;
2230a056af42SMartijn Coenen struct binder_proc *proc = thread->proc;
2231a056af42SMartijn Coenen struct binder_proc *target_proc = t->to_proc;
2232372e3147STodd Kjos struct binder_ref_data rdata;
2233adc18842STodd Kjos int ret = 0;
2234a056af42SMartijn Coenen
2235a056af42SMartijn Coenen node = binder_get_node(proc, fp->binder);
2236a056af42SMartijn Coenen if (!node) {
2237673068eeSTodd Kjos node = binder_new_node(proc, fp);
2238a056af42SMartijn Coenen if (!node)
2239a056af42SMartijn Coenen return -ENOMEM;
2240a056af42SMartijn Coenen }
2241a056af42SMartijn Coenen if (fp->cookie != node->cookie) {
2242a056af42SMartijn Coenen binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2243a056af42SMartijn Coenen proc->pid, thread->pid, (u64)fp->binder,
2244a056af42SMartijn Coenen node->debug_id, (u64)fp->cookie,
2245a056af42SMartijn Coenen (u64)node->cookie);
2246adc18842STodd Kjos ret = -EINVAL;
2247adc18842STodd Kjos goto done;
2248a056af42SMartijn Coenen }
224952f88693STodd Kjos if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2250adc18842STodd Kjos ret = -EPERM;
2251adc18842STodd Kjos goto done;
2252adc18842STodd Kjos }
2253a056af42SMartijn Coenen
2254372e3147STodd Kjos ret = binder_inc_ref_for_node(target_proc, node,
2255372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_BINDER,
2256372e3147STodd Kjos &thread->todo, &rdata);
2257372e3147STodd Kjos if (ret)
2258adc18842STodd Kjos goto done;
2259a056af42SMartijn Coenen
2260a056af42SMartijn Coenen if (fp->hdr.type == BINDER_TYPE_BINDER)
2261a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_HANDLE;
2262a056af42SMartijn Coenen else
2263a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2264a056af42SMartijn Coenen fp->binder = 0;
2265372e3147STodd Kjos fp->handle = rdata.desc;
2266a056af42SMartijn Coenen fp->cookie = 0;
2267a056af42SMartijn Coenen
2268372e3147STodd Kjos trace_binder_transaction_node_to_ref(t, node, &rdata);
2269a056af42SMartijn Coenen binder_debug(BINDER_DEBUG_TRANSACTION,
2270a056af42SMartijn Coenen " node %d u%016llx -> ref %d desc %d\n",
2271a056af42SMartijn Coenen node->debug_id, (u64)node->ptr,
2272372e3147STodd Kjos rdata.debug_id, rdata.desc);
2273adc18842STodd Kjos done:
2274adc18842STodd Kjos binder_put_node(node);
2275adc18842STodd Kjos return ret;
2276a056af42SMartijn Coenen }
2277a056af42SMartijn Coenen
binder_translate_handle(struct flat_binder_object * fp,struct binder_transaction * t,struct binder_thread * thread)2278a056af42SMartijn Coenen static int binder_translate_handle(struct flat_binder_object *fp,
2279a056af42SMartijn Coenen struct binder_transaction *t,
2280a056af42SMartijn Coenen struct binder_thread *thread)
2281a056af42SMartijn Coenen {
2282a056af42SMartijn Coenen struct binder_proc *proc = thread->proc;
2283a056af42SMartijn Coenen struct binder_proc *target_proc = t->to_proc;
2284372e3147STodd Kjos struct binder_node *node;
2285372e3147STodd Kjos struct binder_ref_data src_rdata;
2286adc18842STodd Kjos int ret = 0;
2287a056af42SMartijn Coenen
2288372e3147STodd Kjos node = binder_get_node_from_ref(proc, fp->handle,
2289372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2290372e3147STodd Kjos if (!node) {
2291a056af42SMartijn Coenen binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2292a056af42SMartijn Coenen proc->pid, thread->pid, fp->handle);
2293a056af42SMartijn Coenen return -EINVAL;
2294a056af42SMartijn Coenen }
229552f88693STodd Kjos if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
2296adc18842STodd Kjos ret = -EPERM;
2297adc18842STodd Kjos goto done;
2298adc18842STodd Kjos }
2299a056af42SMartijn Coenen
2300673068eeSTodd Kjos binder_node_lock(node);
2301372e3147STodd Kjos if (node->proc == target_proc) {
2302a056af42SMartijn Coenen if (fp->hdr.type == BINDER_TYPE_HANDLE)
2303a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_BINDER;
2304a056af42SMartijn Coenen else
2305a056af42SMartijn Coenen fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
2306372e3147STodd Kjos fp->binder = node->ptr;
2307372e3147STodd Kjos fp->cookie = node->cookie;
2308673068eeSTodd Kjos if (node->proc)
2309673068eeSTodd Kjos binder_inner_proc_lock(node->proc);
2310324fa64cSTodd Kjos else
2311324fa64cSTodd Kjos __acquire(&node->proc->inner_lock);
2312673068eeSTodd Kjos binder_inc_node_nilocked(node,
2313372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_BINDER,
2314a056af42SMartijn Coenen 0, NULL);
2315673068eeSTodd Kjos if (node->proc)
2316673068eeSTodd Kjos binder_inner_proc_unlock(node->proc);
2317324fa64cSTodd Kjos else
2318324fa64cSTodd Kjos __release(&node->proc->inner_lock);
2319372e3147STodd Kjos trace_binder_transaction_ref_to_node(t, node, &src_rdata);
2320a056af42SMartijn Coenen binder_debug(BINDER_DEBUG_TRANSACTION,
2321a056af42SMartijn Coenen " ref %d desc %d -> node %d u%016llx\n",
2322372e3147STodd Kjos src_rdata.debug_id, src_rdata.desc, node->debug_id,
2323372e3147STodd Kjos (u64)node->ptr);
2324673068eeSTodd Kjos binder_node_unlock(node);
2325a056af42SMartijn Coenen } else {
2326372e3147STodd Kjos struct binder_ref_data dest_rdata;
2327a056af42SMartijn Coenen
2328673068eeSTodd Kjos binder_node_unlock(node);
2329372e3147STodd Kjos ret = binder_inc_ref_for_node(target_proc, node,
2330372e3147STodd Kjos fp->hdr.type == BINDER_TYPE_HANDLE,
2331372e3147STodd Kjos NULL, &dest_rdata);
2332372e3147STodd Kjos if (ret)
2333adc18842STodd Kjos goto done;
2334a056af42SMartijn Coenen
2335a056af42SMartijn Coenen fp->binder = 0;
2336372e3147STodd Kjos fp->handle = dest_rdata.desc;
2337a056af42SMartijn Coenen fp->cookie = 0;
2338372e3147STodd Kjos trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2339372e3147STodd Kjos &dest_rdata);
2340a056af42SMartijn Coenen binder_debug(BINDER_DEBUG_TRANSACTION,
2341a056af42SMartijn Coenen " ref %d desc %d -> ref %d desc %d (node %d)\n",
2342372e3147STodd Kjos src_rdata.debug_id, src_rdata.desc,
2343372e3147STodd Kjos dest_rdata.debug_id, dest_rdata.desc,
2344372e3147STodd Kjos node->debug_id);
2345a056af42SMartijn Coenen }
2346adc18842STodd Kjos done:
2347adc18842STodd Kjos binder_put_node(node);
2348adc18842STodd Kjos return ret;
2349a056af42SMartijn Coenen }
2350a056af42SMartijn 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)23518ced0c62STodd Kjos static int binder_translate_fd(u32 fd, binder_size_t fd_offset,
2352a056af42SMartijn Coenen struct binder_transaction *t,
2353a056af42SMartijn Coenen struct binder_thread *thread,
2354a056af42SMartijn Coenen struct binder_transaction *in_reply_to)
2355a056af42SMartijn Coenen {
2356a056af42SMartijn Coenen struct binder_proc *proc = thread->proc;
2357a056af42SMartijn Coenen struct binder_proc *target_proc = t->to_proc;
235844d8047fSTodd Kjos struct binder_txn_fd_fixup *fixup;
2359a056af42SMartijn Coenen struct file *file;
236044d8047fSTodd Kjos int ret = 0;
2361a056af42SMartijn Coenen bool target_allows_fd;
2362a056af42SMartijn Coenen
2363a056af42SMartijn Coenen if (in_reply_to)
2364a056af42SMartijn Coenen target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2365a056af42SMartijn Coenen else
2366a056af42SMartijn Coenen target_allows_fd = t->buffer->target_node->accept_fds;
2367a056af42SMartijn Coenen if (!target_allows_fd) {
2368a056af42SMartijn Coenen binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2369a056af42SMartijn Coenen proc->pid, thread->pid,
2370a056af42SMartijn Coenen in_reply_to ? "reply" : "transaction",
2371a056af42SMartijn Coenen fd);
2372a056af42SMartijn Coenen ret = -EPERM;
2373a056af42SMartijn Coenen goto err_fd_not_accepted;
2374a056af42SMartijn Coenen }
2375a056af42SMartijn Coenen
2376a056af42SMartijn Coenen file = fget(fd);
2377a056af42SMartijn Coenen if (!file) {
2378a056af42SMartijn Coenen binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2379a056af42SMartijn Coenen proc->pid, thread->pid, fd);
2380a056af42SMartijn Coenen ret = -EBADF;
2381a056af42SMartijn Coenen goto err_fget;
2382a056af42SMartijn Coenen }
238352f88693STodd Kjos ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
2384a056af42SMartijn Coenen if (ret < 0) {
2385a056af42SMartijn Coenen ret = -EPERM;
2386a056af42SMartijn Coenen goto err_security;
2387a056af42SMartijn Coenen }
2388a056af42SMartijn Coenen
238944d8047fSTodd Kjos /*
239044d8047fSTodd Kjos * Add fixup record for this transaction. The allocation
239144d8047fSTodd Kjos * of the fd in the target needs to be done from a
239244d8047fSTodd Kjos * target thread.
239344d8047fSTodd Kjos */
239444d8047fSTodd Kjos fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
239544d8047fSTodd Kjos if (!fixup) {
2396a056af42SMartijn Coenen ret = -ENOMEM;
239744d8047fSTodd Kjos goto err_alloc;
2398a056af42SMartijn Coenen }
239944d8047fSTodd Kjos fixup->file = file;
24008ced0c62STodd Kjos fixup->offset = fd_offset;
2401a8a570c6SCarlos Llamas fixup->target_fd = -1;
240244d8047fSTodd Kjos trace_binder_transaction_fd_send(t, fd, fixup->offset);
240344d8047fSTodd Kjos list_add_tail(&fixup->fixup_entry, &t->fd_fixups);
2404a056af42SMartijn Coenen
240544d8047fSTodd Kjos return ret;
2406a056af42SMartijn Coenen
240744d8047fSTodd Kjos err_alloc:
2408a056af42SMartijn Coenen err_security:
2409a056af42SMartijn Coenen fput(file);
2410a056af42SMartijn Coenen err_fget:
2411a056af42SMartijn Coenen err_fd_not_accepted:
2412a056af42SMartijn Coenen return ret;
2413a056af42SMartijn Coenen }
2414a056af42SMartijn Coenen
241509184ae9STodd Kjos /**
241609184ae9STodd Kjos * struct binder_ptr_fixup - data to be fixed-up in target buffer
241709184ae9STodd Kjos * @offset offset in target buffer to fixup
241809184ae9STodd Kjos * @skip_size bytes to skip in copy (fixup will be written later)
241909184ae9STodd Kjos * @fixup_data data to write at fixup offset
242009184ae9STodd Kjos * @node list node
242109184ae9STodd Kjos *
242209184ae9STodd Kjos * This is used for the pointer fixup list (pf) which is created and consumed
242309184ae9STodd Kjos * during binder_transaction() and is only accessed locally. No
242409184ae9STodd Kjos * locking is necessary.
242509184ae9STodd Kjos *
242609184ae9STodd Kjos * The list is ordered by @offset.
242709184ae9STodd Kjos */
242809184ae9STodd Kjos struct binder_ptr_fixup {
242909184ae9STodd Kjos binder_size_t offset;
243009184ae9STodd Kjos size_t skip_size;
243109184ae9STodd Kjos binder_uintptr_t fixup_data;
243209184ae9STodd Kjos struct list_head node;
243309184ae9STodd Kjos };
243409184ae9STodd Kjos
243509184ae9STodd Kjos /**
243609184ae9STodd Kjos * struct binder_sg_copy - scatter-gather data to be copied
243709184ae9STodd Kjos * @offset offset in target buffer
243809184ae9STodd Kjos * @sender_uaddr user address in source buffer
243909184ae9STodd Kjos * @length bytes to copy
244009184ae9STodd Kjos * @node list node
244109184ae9STodd Kjos *
244209184ae9STodd Kjos * This is used for the sg copy list (sgc) which is created and consumed
244309184ae9STodd Kjos * during binder_transaction() and is only accessed locally. No
244409184ae9STodd Kjos * locking is necessary.
244509184ae9STodd Kjos *
244609184ae9STodd Kjos * The list is ordered by @offset.
244709184ae9STodd Kjos */
244809184ae9STodd Kjos struct binder_sg_copy {
244909184ae9STodd Kjos binder_size_t offset;
245009184ae9STodd Kjos const void __user *sender_uaddr;
245109184ae9STodd Kjos size_t length;
245209184ae9STodd Kjos struct list_head node;
245309184ae9STodd Kjos };
245409184ae9STodd Kjos
245509184ae9STodd Kjos /**
245609184ae9STodd Kjos * binder_do_deferred_txn_copies() - copy and fixup scatter-gather data
245709184ae9STodd Kjos * @alloc: binder_alloc associated with @buffer
245809184ae9STodd Kjos * @buffer: binder buffer in target process
245909184ae9STodd Kjos * @sgc_head: list_head of scatter-gather copy list
246009184ae9STodd Kjos * @pf_head: list_head of pointer fixup list
246109184ae9STodd Kjos *
246209184ae9STodd Kjos * Processes all elements of @sgc_head, applying fixups from @pf_head
246309184ae9STodd Kjos * and copying the scatter-gather data from the source process' user
246409184ae9STodd Kjos * buffer to the target's buffer. It is expected that the list creation
246509184ae9STodd Kjos * and processing all occurs during binder_transaction() so these lists
246609184ae9STodd Kjos * are only accessed in local context.
246709184ae9STodd Kjos *
246809184ae9STodd Kjos * Return: 0=success, else -errno
246909184ae9STodd Kjos */
binder_do_deferred_txn_copies(struct binder_alloc * alloc,struct binder_buffer * buffer,struct list_head * sgc_head,struct list_head * pf_head)247009184ae9STodd Kjos static int binder_do_deferred_txn_copies(struct binder_alloc *alloc,
247109184ae9STodd Kjos struct binder_buffer *buffer,
247209184ae9STodd Kjos struct list_head *sgc_head,
247309184ae9STodd Kjos struct list_head *pf_head)
247409184ae9STodd Kjos {
247509184ae9STodd Kjos int ret = 0;
247609184ae9STodd Kjos struct binder_sg_copy *sgc, *tmpsgc;
24772d1746e3SAlessandro Astone struct binder_ptr_fixup *tmppf;
247809184ae9STodd Kjos struct binder_ptr_fixup *pf =
247909184ae9STodd Kjos list_first_entry_or_null(pf_head, struct binder_ptr_fixup,
248009184ae9STodd Kjos node);
248109184ae9STodd Kjos
248209184ae9STodd Kjos list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
248309184ae9STodd Kjos size_t bytes_copied = 0;
248409184ae9STodd Kjos
248509184ae9STodd Kjos while (bytes_copied < sgc->length) {
248609184ae9STodd Kjos size_t copy_size;
248709184ae9STodd Kjos size_t bytes_left = sgc->length - bytes_copied;
248809184ae9STodd Kjos size_t offset = sgc->offset + bytes_copied;
248909184ae9STodd Kjos
249009184ae9STodd Kjos /*
249109184ae9STodd Kjos * We copy up to the fixup (pointed to by pf)
249209184ae9STodd Kjos */
249309184ae9STodd Kjos copy_size = pf ? min(bytes_left, (size_t)pf->offset - offset)
249409184ae9STodd Kjos : bytes_left;
249509184ae9STodd Kjos if (!ret && copy_size)
249609184ae9STodd Kjos ret = binder_alloc_copy_user_to_buffer(
249709184ae9STodd Kjos alloc, buffer,
249809184ae9STodd Kjos offset,
249909184ae9STodd Kjos sgc->sender_uaddr + bytes_copied,
250009184ae9STodd Kjos copy_size);
250109184ae9STodd Kjos bytes_copied += copy_size;
250209184ae9STodd Kjos if (copy_size != bytes_left) {
250309184ae9STodd Kjos BUG_ON(!pf);
250409184ae9STodd Kjos /* we stopped at a fixup offset */
250509184ae9STodd Kjos if (pf->skip_size) {
250609184ae9STodd Kjos /*
250709184ae9STodd Kjos * we are just skipping. This is for
250809184ae9STodd Kjos * BINDER_TYPE_FDA where the translated
250909184ae9STodd Kjos * fds will be fixed up when we get
251009184ae9STodd Kjos * to target context.
251109184ae9STodd Kjos */
251209184ae9STodd Kjos bytes_copied += pf->skip_size;
251309184ae9STodd Kjos } else {
251409184ae9STodd Kjos /* apply the fixup indicated by pf */
251509184ae9STodd Kjos if (!ret)
251609184ae9STodd Kjos ret = binder_alloc_copy_to_buffer(
251709184ae9STodd Kjos alloc, buffer,
251809184ae9STodd Kjos pf->offset,
251909184ae9STodd Kjos &pf->fixup_data,
252009184ae9STodd Kjos sizeof(pf->fixup_data));
252109184ae9STodd Kjos bytes_copied += sizeof(pf->fixup_data);
252209184ae9STodd Kjos }
252309184ae9STodd Kjos list_del(&pf->node);
252409184ae9STodd Kjos kfree(pf);
252509184ae9STodd Kjos pf = list_first_entry_or_null(pf_head,
252609184ae9STodd Kjos struct binder_ptr_fixup, node);
252709184ae9STodd Kjos }
252809184ae9STodd Kjos }
252909184ae9STodd Kjos list_del(&sgc->node);
253009184ae9STodd Kjos kfree(sgc);
253109184ae9STodd Kjos }
25322d1746e3SAlessandro Astone list_for_each_entry_safe(pf, tmppf, pf_head, node) {
25332d1746e3SAlessandro Astone BUG_ON(pf->skip_size == 0);
25342d1746e3SAlessandro Astone list_del(&pf->node);
25352d1746e3SAlessandro Astone kfree(pf);
25362d1746e3SAlessandro Astone }
253709184ae9STodd Kjos BUG_ON(!list_empty(sgc_head));
253809184ae9STodd Kjos
253909184ae9STodd Kjos return ret > 0 ? -EINVAL : ret;
254009184ae9STodd Kjos }
254109184ae9STodd Kjos
254209184ae9STodd Kjos /**
254309184ae9STodd Kjos * binder_cleanup_deferred_txn_lists() - free specified lists
254409184ae9STodd Kjos * @sgc_head: list_head of scatter-gather copy list
254509184ae9STodd Kjos * @pf_head: list_head of pointer fixup list
254609184ae9STodd Kjos *
254709184ae9STodd Kjos * Called to clean up @sgc_head and @pf_head if there is an
254809184ae9STodd Kjos * error.
254909184ae9STodd Kjos */
binder_cleanup_deferred_txn_lists(struct list_head * sgc_head,struct list_head * pf_head)255009184ae9STodd Kjos static void binder_cleanup_deferred_txn_lists(struct list_head *sgc_head,
255109184ae9STodd Kjos struct list_head *pf_head)
255209184ae9STodd Kjos {
255309184ae9STodd Kjos struct binder_sg_copy *sgc, *tmpsgc;
255409184ae9STodd Kjos struct binder_ptr_fixup *pf, *tmppf;
255509184ae9STodd Kjos
255609184ae9STodd Kjos list_for_each_entry_safe(sgc, tmpsgc, sgc_head, node) {
255709184ae9STodd Kjos list_del(&sgc->node);
255809184ae9STodd Kjos kfree(sgc);
255909184ae9STodd Kjos }
256009184ae9STodd Kjos list_for_each_entry_safe(pf, tmppf, pf_head, node) {
256109184ae9STodd Kjos list_del(&pf->node);
256209184ae9STodd Kjos kfree(pf);
256309184ae9STodd Kjos }
256409184ae9STodd Kjos }
256509184ae9STodd Kjos
256609184ae9STodd Kjos /**
256709184ae9STodd Kjos * binder_defer_copy() - queue a scatter-gather buffer for copy
256809184ae9STodd Kjos * @sgc_head: list_head of scatter-gather copy list
256909184ae9STodd Kjos * @offset: binder buffer offset in target process
257009184ae9STodd Kjos * @sender_uaddr: user address in source process
257109184ae9STodd Kjos * @length: bytes to copy
257209184ae9STodd Kjos *
257309184ae9STodd Kjos * Specify a scatter-gather block to be copied. The actual copy must
257409184ae9STodd Kjos * be deferred until all the needed fixups are identified and queued.
257509184ae9STodd Kjos * Then the copy and fixups are done together so un-translated values
257609184ae9STodd Kjos * from the source are never visible in the target buffer.
257709184ae9STodd Kjos *
257809184ae9STodd Kjos * We are guaranteed that repeated calls to this function will have
257909184ae9STodd Kjos * monotonically increasing @offset values so the list will naturally
258009184ae9STodd Kjos * be ordered.
258109184ae9STodd Kjos *
258209184ae9STodd Kjos * Return: 0=success, else -errno
258309184ae9STodd Kjos */
binder_defer_copy(struct list_head * sgc_head,binder_size_t offset,const void __user * sender_uaddr,size_t length)258409184ae9STodd Kjos static int binder_defer_copy(struct list_head *sgc_head, binder_size_t offset,
258509184ae9STodd Kjos const void __user *sender_uaddr, size_t length)
258609184ae9STodd Kjos {
258709184ae9STodd Kjos struct binder_sg_copy *bc = kzalloc(sizeof(*bc), GFP_KERNEL);
258809184ae9STodd Kjos
258909184ae9STodd Kjos if (!bc)
259009184ae9STodd Kjos return -ENOMEM;
259109184ae9STodd Kjos
259209184ae9STodd Kjos bc->offset = offset;
259309184ae9STodd Kjos bc->sender_uaddr = sender_uaddr;
259409184ae9STodd Kjos bc->length = length;
259509184ae9STodd Kjos INIT_LIST_HEAD(&bc->node);
259609184ae9STodd Kjos
259709184ae9STodd Kjos /*
259809184ae9STodd Kjos * We are guaranteed that the deferred copies are in-order
259909184ae9STodd Kjos * so just add to the tail.
260009184ae9STodd Kjos */
260109184ae9STodd Kjos list_add_tail(&bc->node, sgc_head);
260209184ae9STodd Kjos
260309184ae9STodd Kjos return 0;
260409184ae9STodd Kjos }
260509184ae9STodd Kjos
260609184ae9STodd Kjos /**
260709184ae9STodd Kjos * binder_add_fixup() - queue a fixup to be applied to sg copy
260809184ae9STodd Kjos * @pf_head: list_head of binder ptr fixup list
260909184ae9STodd Kjos * @offset: binder buffer offset in target process
261009184ae9STodd Kjos * @fixup: bytes to be copied for fixup
261109184ae9STodd Kjos * @skip_size: bytes to skip when copying (fixup will be applied later)
261209184ae9STodd Kjos *
261309184ae9STodd Kjos * Add the specified fixup to a list ordered by @offset. When copying
261409184ae9STodd Kjos * the scatter-gather buffers, the fixup will be copied instead of
261509184ae9STodd Kjos * data from the source buffer. For BINDER_TYPE_FDA fixups, the fixup
261609184ae9STodd Kjos * will be applied later (in target process context), so we just skip
261709184ae9STodd Kjos * the bytes specified by @skip_size. If @skip_size is 0, we copy the
261809184ae9STodd Kjos * value in @fixup.
261909184ae9STodd Kjos *
262009184ae9STodd Kjos * This function is called *mostly* in @offset order, but there are
262109184ae9STodd Kjos * exceptions. Since out-of-order inserts are relatively uncommon,
262209184ae9STodd Kjos * we insert the new element by searching backward from the tail of
262309184ae9STodd Kjos * the list.
262409184ae9STodd Kjos *
262509184ae9STodd Kjos * Return: 0=success, else -errno
262609184ae9STodd Kjos */
binder_add_fixup(struct list_head * pf_head,binder_size_t offset,binder_uintptr_t fixup,size_t skip_size)262709184ae9STodd Kjos static int binder_add_fixup(struct list_head *pf_head, binder_size_t offset,
262809184ae9STodd Kjos binder_uintptr_t fixup, size_t skip_size)
262909184ae9STodd Kjos {
263009184ae9STodd Kjos struct binder_ptr_fixup *pf = kzalloc(sizeof(*pf), GFP_KERNEL);
263109184ae9STodd Kjos struct binder_ptr_fixup *tmppf;
263209184ae9STodd Kjos
263309184ae9STodd Kjos if (!pf)
263409184ae9STodd Kjos return -ENOMEM;
263509184ae9STodd Kjos
263609184ae9STodd Kjos pf->offset = offset;
263709184ae9STodd Kjos pf->fixup_data = fixup;
263809184ae9STodd Kjos pf->skip_size = skip_size;
263909184ae9STodd Kjos INIT_LIST_HEAD(&pf->node);
264009184ae9STodd Kjos
264109184ae9STodd Kjos /* Fixups are *mostly* added in-order, but there are some
264209184ae9STodd Kjos * exceptions. Look backwards through list for insertion point.
264309184ae9STodd Kjos */
264409184ae9STodd Kjos list_for_each_entry_reverse(tmppf, pf_head, node) {
264509184ae9STodd Kjos if (tmppf->offset < pf->offset) {
264609184ae9STodd Kjos list_add(&pf->node, &tmppf->node);
264709184ae9STodd Kjos return 0;
264809184ae9STodd Kjos }
264909184ae9STodd Kjos }
265009184ae9STodd Kjos /*
265109184ae9STodd Kjos * if we get here, then the new offset is the lowest so
265209184ae9STodd Kjos * insert at the head
265309184ae9STodd Kjos */
265409184ae9STodd Kjos list_add(&pf->node, pf_head);
265509184ae9STodd Kjos return 0;
265609184ae9STodd Kjos }
265709184ae9STodd 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)265809184ae9STodd Kjos static int binder_translate_fd_array(struct list_head *pf_head,
265909184ae9STodd Kjos struct binder_fd_array_object *fda,
2660656e01f3STodd Kjos const void __user *sender_ubuffer,
2661def95c73SMartijn Coenen struct binder_buffer_object *parent,
2662656e01f3STodd Kjos struct binder_buffer_object *sender_uparent,
2663def95c73SMartijn Coenen struct binder_transaction *t,
2664def95c73SMartijn Coenen struct binder_thread *thread,
2665def95c73SMartijn Coenen struct binder_transaction *in_reply_to)
2666def95c73SMartijn Coenen {
266744d8047fSTodd Kjos binder_size_t fdi, fd_buf_size;
2668bde4a19fSTodd Kjos binder_size_t fda_offset;
2669656e01f3STodd Kjos const void __user *sender_ufda_base;
2670def95c73SMartijn Coenen struct binder_proc *proc = thread->proc;
267109184ae9STodd Kjos int ret;
2672def95c73SMartijn Coenen
2673ef38de92SAlessandro Astone if (fda->num_fds == 0)
2674ef38de92SAlessandro Astone return 0;
2675ef38de92SAlessandro Astone
2676def95c73SMartijn Coenen fd_buf_size = sizeof(u32) * fda->num_fds;
2677def95c73SMartijn Coenen if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2678def95c73SMartijn Coenen binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2679def95c73SMartijn Coenen proc->pid, thread->pid, (u64)fda->num_fds);
2680def95c73SMartijn Coenen return -EINVAL;
2681def95c73SMartijn Coenen }
2682def95c73SMartijn Coenen if (fd_buf_size > parent->length ||
2683def95c73SMartijn Coenen fda->parent_offset > parent->length - fd_buf_size) {
2684def95c73SMartijn Coenen /* No space for all file descriptors here. */
2685def95c73SMartijn Coenen binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2686def95c73SMartijn Coenen proc->pid, thread->pid, (u64)fda->num_fds);
2687def95c73SMartijn Coenen return -EINVAL;
2688def95c73SMartijn Coenen }
2689bde4a19fSTodd Kjos /*
2690bde4a19fSTodd Kjos * the source data for binder_buffer_object is visible
2691bde4a19fSTodd Kjos * to user-space and the @buffer element is the user
2692bde4a19fSTodd Kjos * pointer to the buffer_object containing the fd_array.
2693bde4a19fSTodd Kjos * Convert the address to an offset relative to
2694bde4a19fSTodd Kjos * the base of the transaction buffer.
2695bde4a19fSTodd Kjos */
2696df9aabeaSCarlos Llamas fda_offset = parent->buffer - t->buffer->user_data +
2697bde4a19fSTodd Kjos fda->parent_offset;
26989a0a930fSArnd Bergmann sender_ufda_base = (void __user *)(uintptr_t)sender_uparent->buffer +
26999a0a930fSArnd Bergmann fda->parent_offset;
2700656e01f3STodd Kjos
2701656e01f3STodd Kjos if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32)) ||
2702656e01f3STodd Kjos !IS_ALIGNED((unsigned long)sender_ufda_base, sizeof(u32))) {
2703def95c73SMartijn Coenen binder_user_error("%d:%d parent offset not aligned correctly.\n",
2704def95c73SMartijn Coenen proc->pid, thread->pid);
2705def95c73SMartijn Coenen return -EINVAL;
2706def95c73SMartijn Coenen }
270709184ae9STodd Kjos ret = binder_add_fixup(pf_head, fda_offset, 0, fda->num_fds * sizeof(u32));
270809184ae9STodd Kjos if (ret)
270909184ae9STodd Kjos return ret;
271009184ae9STodd Kjos
2711def95c73SMartijn Coenen for (fdi = 0; fdi < fda->num_fds; fdi++) {
27128ced0c62STodd Kjos u32 fd;
2713bde4a19fSTodd Kjos binder_size_t offset = fda_offset + fdi * sizeof(fd);
2714656e01f3STodd Kjos binder_size_t sender_uoffset = fdi * sizeof(fd);
27158ced0c62STodd Kjos
2716656e01f3STodd Kjos ret = copy_from_user(&fd, sender_ufda_base + sender_uoffset, sizeof(fd));
2717bb4a2e48STodd Kjos if (!ret)
27188ced0c62STodd Kjos ret = binder_translate_fd(fd, offset, t, thread,
2719def95c73SMartijn Coenen in_reply_to);
2720fe6b1869STodd Kjos if (ret)
2721fe6b1869STodd Kjos return ret > 0 ? -EINVAL : ret;
2722def95c73SMartijn Coenen }
2723def95c73SMartijn Coenen return 0;
2724def95c73SMartijn Coenen }
2725def95c73SMartijn 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)272609184ae9STodd Kjos static int binder_fixup_parent(struct list_head *pf_head,
272709184ae9STodd Kjos struct binder_transaction *t,
27287980240bSMartijn Coenen struct binder_thread *thread,
27297980240bSMartijn Coenen struct binder_buffer_object *bp,
2730db6b0b81STodd Kjos binder_size_t off_start_offset,
27317980240bSMartijn Coenen binder_size_t num_valid,
2732db6b0b81STodd Kjos binder_size_t last_fixup_obj_off,
27337980240bSMartijn Coenen binder_size_t last_fixup_min_off)
27347980240bSMartijn Coenen {
27357980240bSMartijn Coenen struct binder_buffer_object *parent;
27367980240bSMartijn Coenen struct binder_buffer *b = t->buffer;
27377980240bSMartijn Coenen struct binder_proc *proc = thread->proc;
27387980240bSMartijn Coenen struct binder_proc *target_proc = t->to_proc;
2739db6b0b81STodd Kjos struct binder_object object;
2740db6b0b81STodd Kjos binder_size_t buffer_offset;
2741db6b0b81STodd Kjos binder_size_t parent_offset;
27427980240bSMartijn Coenen
27437980240bSMartijn Coenen if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
27447980240bSMartijn Coenen return 0;
27457980240bSMartijn Coenen
2746db6b0b81STodd Kjos parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2747db6b0b81STodd Kjos off_start_offset, &parent_offset,
2748db6b0b81STodd Kjos num_valid);
27497980240bSMartijn Coenen if (!parent) {
27507980240bSMartijn Coenen binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
27517980240bSMartijn Coenen proc->pid, thread->pid);
27527980240bSMartijn Coenen return -EINVAL;
27537980240bSMartijn Coenen }
27547980240bSMartijn Coenen
2755db6b0b81STodd Kjos if (!binder_validate_fixup(target_proc, b, off_start_offset,
2756db6b0b81STodd Kjos parent_offset, bp->parent_offset,
2757db6b0b81STodd Kjos last_fixup_obj_off,
27587980240bSMartijn Coenen last_fixup_min_off)) {
27597980240bSMartijn Coenen binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
27607980240bSMartijn Coenen proc->pid, thread->pid);
27617980240bSMartijn Coenen return -EINVAL;
27627980240bSMartijn Coenen }
27637980240bSMartijn Coenen
27647980240bSMartijn Coenen if (parent->length < sizeof(binder_uintptr_t) ||
27657980240bSMartijn Coenen bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
27667980240bSMartijn Coenen /* No space for a pointer here! */
27677980240bSMartijn Coenen binder_user_error("%d:%d got transaction with invalid parent offset\n",
27687980240bSMartijn Coenen proc->pid, thread->pid);
27697980240bSMartijn Coenen return -EINVAL;
27707980240bSMartijn Coenen }
2771df9aabeaSCarlos Llamas
2772df9aabeaSCarlos Llamas buffer_offset = bp->parent_offset + parent->buffer - b->user_data;
2773df9aabeaSCarlos Llamas
277409184ae9STodd Kjos return binder_add_fixup(pf_head, buffer_offset, bp->buffer, 0);
27757980240bSMartijn Coenen }
27767980240bSMartijn Coenen
2777408c68b1SMartijn Coenen /**
27789864bb48SLi Li * binder_can_update_transaction() - Can a txn be superseded by an updated one?
27799864bb48SLi Li * @t1: the pending async txn in the frozen process
27809864bb48SLi Li * @t2: the new async txn to supersede the outdated pending one
27819864bb48SLi Li *
27829864bb48SLi Li * Return: true if t2 can supersede t1
27839864bb48SLi Li * false if t2 can not supersede t1
27849864bb48SLi Li */
binder_can_update_transaction(struct binder_transaction * t1,struct binder_transaction * t2)27859864bb48SLi Li static bool binder_can_update_transaction(struct binder_transaction *t1,
27869864bb48SLi Li struct binder_transaction *t2)
27879864bb48SLi Li {
27889864bb48SLi Li if ((t1->flags & t2->flags & (TF_ONE_WAY | TF_UPDATE_TXN)) !=
27899864bb48SLi Li (TF_ONE_WAY | TF_UPDATE_TXN) || !t1->to_proc || !t2->to_proc)
27909864bb48SLi Li return false;
27919864bb48SLi Li if (t1->to_proc->tsk == t2->to_proc->tsk && t1->code == t2->code &&
27929864bb48SLi Li t1->flags == t2->flags && t1->buffer->pid == t2->buffer->pid &&
27939864bb48SLi Li t1->buffer->target_node->ptr == t2->buffer->target_node->ptr &&
27949864bb48SLi Li t1->buffer->target_node->cookie == t2->buffer->target_node->cookie)
27959864bb48SLi Li return true;
27969864bb48SLi Li return false;
27979864bb48SLi Li }
27989864bb48SLi Li
27999864bb48SLi Li /**
28009864bb48SLi Li * binder_find_outdated_transaction_ilocked() - Find the outdated transaction
28019864bb48SLi Li * @t: new async transaction
28029864bb48SLi Li * @target_list: list to find outdated transaction
28039864bb48SLi Li *
28049864bb48SLi Li * Return: the outdated transaction if found
28059864bb48SLi Li * NULL if no outdated transacton can be found
28069864bb48SLi Li *
28079864bb48SLi Li * Requires the proc->inner_lock to be held.
28089864bb48SLi Li */
28099864bb48SLi Li static struct binder_transaction *
binder_find_outdated_transaction_ilocked(struct binder_transaction * t,struct list_head * target_list)28109864bb48SLi Li binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
28119864bb48SLi Li struct list_head *target_list)
28129864bb48SLi Li {
28139864bb48SLi Li struct binder_work *w;
28149864bb48SLi Li
28159864bb48SLi Li list_for_each_entry(w, target_list, entry) {
28169864bb48SLi Li struct binder_transaction *t_queued;
28179864bb48SLi Li
28189864bb48SLi Li if (w->type != BINDER_WORK_TRANSACTION)
28199864bb48SLi Li continue;
28209864bb48SLi Li t_queued = container_of(w, struct binder_transaction, work);
28219864bb48SLi Li if (binder_can_update_transaction(t_queued, t))
28229864bb48SLi Li return t_queued;
28239864bb48SLi Li }
28249864bb48SLi Li return NULL;
28259864bb48SLi Li }
28269864bb48SLi Li
28279864bb48SLi Li /**
2828408c68b1SMartijn Coenen * binder_proc_transaction() - sends a transaction to a process and wakes it up
2829408c68b1SMartijn Coenen * @t: transaction to send
2830408c68b1SMartijn Coenen * @proc: process to send the transaction to
2831408c68b1SMartijn Coenen * @thread: thread in @proc to send the transaction to (may be NULL)
2832408c68b1SMartijn Coenen *
2833408c68b1SMartijn Coenen * This function queues a transaction to the specified process. It will try
2834408c68b1SMartijn Coenen * to find a thread in the target process to handle the transaction and
2835408c68b1SMartijn Coenen * wake it up. If no thread is found, the work is queued to the proc
2836408c68b1SMartijn Coenen * waitqueue.
2837408c68b1SMartijn Coenen *
2838408c68b1SMartijn Coenen * If the @thread parameter is not NULL, the transaction is always queued
2839408c68b1SMartijn Coenen * to the waitlist of that specific thread.
2840408c68b1SMartijn Coenen *
2841432ff1e9SMarco Ballesio * Return: 0 if the transaction was successfully queued
2842432ff1e9SMarco Ballesio * BR_DEAD_REPLY if the target process or thread is dead
28430567461aSLi Li * BR_FROZEN_REPLY if the target process or thread is frozen and
28440567461aSLi Li * the sync transaction was rejected
28450567461aSLi Li * BR_TRANSACTION_PENDING_FROZEN if the target process is frozen
28460567461aSLi Li * and the async transaction was successfully queued
2847408c68b1SMartijn Coenen */
binder_proc_transaction(struct binder_transaction * t,struct binder_proc * proc,struct binder_thread * thread)2848432ff1e9SMarco Ballesio static int binder_proc_transaction(struct binder_transaction *t,
2849408c68b1SMartijn Coenen struct binder_proc *proc,
2850408c68b1SMartijn Coenen struct binder_thread *thread)
2851408c68b1SMartijn Coenen {
2852408c68b1SMartijn Coenen struct binder_node *node = t->buffer->target_node;
2853408c68b1SMartijn Coenen bool oneway = !!(t->flags & TF_ONE_WAY);
2854148ade2cSMartijn Coenen bool pending_async = false;
28559864bb48SLi Li struct binder_transaction *t_outdated = NULL;
28560567461aSLi Li bool frozen = false;
2857408c68b1SMartijn Coenen
2858408c68b1SMartijn Coenen BUG_ON(!node);
2859408c68b1SMartijn Coenen binder_node_lock(node);
2860408c68b1SMartijn Coenen if (oneway) {
2861408c68b1SMartijn Coenen BUG_ON(thread);
28628df5b949SMrinal Pandey if (node->has_async_transaction)
2863148ade2cSMartijn Coenen pending_async = true;
28648df5b949SMrinal Pandey else
2865197410adSGustavo A. R. Silva node->has_async_transaction = true;
2866408c68b1SMartijn Coenen }
2867408c68b1SMartijn Coenen
2868408c68b1SMartijn Coenen binder_inner_proc_lock(proc);
2869ae28c1beSMarco Ballesio if (proc->is_frozen) {
28700567461aSLi Li frozen = true;
2871ae28c1beSMarco Ballesio proc->sync_recv |= !oneway;
2872ae28c1beSMarco Ballesio proc->async_recv |= oneway;
2873ae28c1beSMarco Ballesio }
2874408c68b1SMartijn Coenen
28750567461aSLi Li if ((frozen && !oneway) || proc->is_dead ||
2876432ff1e9SMarco Ballesio (thread && thread->is_dead)) {
2877408c68b1SMartijn Coenen binder_inner_proc_unlock(proc);
2878408c68b1SMartijn Coenen binder_node_unlock(node);
28790567461aSLi Li return frozen ? BR_FROZEN_REPLY : BR_DEAD_REPLY;
2880408c68b1SMartijn Coenen }
2881408c68b1SMartijn Coenen
2882148ade2cSMartijn Coenen if (!thread && !pending_async)
2883408c68b1SMartijn Coenen thread = binder_select_thread_ilocked(proc);
2884408c68b1SMartijn Coenen
28859864bb48SLi Li if (thread) {
2886148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(thread, &t->work);
28879864bb48SLi Li } else if (!pending_async) {
2888148ade2cSMartijn Coenen binder_enqueue_work_ilocked(&t->work, &proc->todo);
28899864bb48SLi Li } else {
28900567461aSLi Li if ((t->flags & TF_UPDATE_TXN) && frozen) {
28919864bb48SLi Li t_outdated = binder_find_outdated_transaction_ilocked(t,
28929864bb48SLi Li &node->async_todo);
28939864bb48SLi Li if (t_outdated) {
28949864bb48SLi Li binder_debug(BINDER_DEBUG_TRANSACTION,
28959864bb48SLi Li "txn %d supersedes %d\n",
28969864bb48SLi Li t->debug_id, t_outdated->debug_id);
28979864bb48SLi Li list_del_init(&t_outdated->work.entry);
28989864bb48SLi Li proc->outstanding_txns--;
28999864bb48SLi Li }
29009864bb48SLi Li }
2901148ade2cSMartijn Coenen binder_enqueue_work_ilocked(&t->work, &node->async_todo);
29029864bb48SLi Li }
2903408c68b1SMartijn Coenen
2904148ade2cSMartijn Coenen if (!pending_async)
2905408c68b1SMartijn Coenen binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2906408c68b1SMartijn Coenen
2907432ff1e9SMarco Ballesio proc->outstanding_txns++;
2908408c68b1SMartijn Coenen binder_inner_proc_unlock(proc);
2909408c68b1SMartijn Coenen binder_node_unlock(node);
2910408c68b1SMartijn Coenen
29119864bb48SLi Li /*
29129864bb48SLi Li * To reduce potential contention, free the outdated transaction and
29139864bb48SLi Li * buffer after releasing the locks.
29149864bb48SLi Li */
29159864bb48SLi Li if (t_outdated) {
29169864bb48SLi Li struct binder_buffer *buffer = t_outdated->buffer;
29179864bb48SLi Li
29189864bb48SLi Li t_outdated->buffer = NULL;
29199864bb48SLi Li buffer->transaction = NULL;
29209864bb48SLi Li trace_binder_transaction_update_buffer_release(buffer);
2921bdc1c5faSCarlos Llamas binder_release_entire_buffer(proc, NULL, buffer, false);
29229864bb48SLi Li binder_alloc_free_buf(&proc->alloc, buffer);
29239864bb48SLi Li kfree(t_outdated);
29249864bb48SLi Li binder_stats_deleted(BINDER_STAT_TRANSACTION);
29259864bb48SLi Li }
29269864bb48SLi Li
29270567461aSLi Li if (oneway && frozen)
29280567461aSLi Li return BR_TRANSACTION_PENDING_FROZEN;
29290567461aSLi Li
2930432ff1e9SMarco Ballesio return 0;
2931408c68b1SMartijn Coenen }
2932408c68b1SMartijn Coenen
2933512cf465STodd Kjos /**
2934512cf465STodd Kjos * binder_get_node_refs_for_txn() - Get required refs on node for txn
2935512cf465STodd Kjos * @node: struct binder_node for which to get refs
2936ad228a34SRandy Dunlap * @procp: returns @node->proc if valid
2937ad228a34SRandy Dunlap * @error: if no @procp then returns BR_DEAD_REPLY
2938512cf465STodd Kjos *
2939512cf465STodd Kjos * User-space normally keeps the node alive when creating a transaction
2940512cf465STodd Kjos * since it has a reference to the target. The local strong ref keeps it
2941512cf465STodd Kjos * alive if the sending process dies before the target process processes
2942512cf465STodd Kjos * the transaction. If the source process is malicious or has a reference
2943512cf465STodd Kjos * counting bug, relying on the local strong ref can fail.
2944512cf465STodd Kjos *
2945512cf465STodd Kjos * Since user-space can cause the local strong ref to go away, we also take
2946512cf465STodd Kjos * a tmpref on the node to ensure it survives while we are constructing
2947512cf465STodd Kjos * the transaction. We also need a tmpref on the proc while we are
2948512cf465STodd Kjos * constructing the transaction, so we take that here as well.
2949512cf465STodd Kjos *
2950512cf465STodd Kjos * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2951ad228a34SRandy Dunlap * Also sets @procp if valid. If the @node->proc is NULL indicating that the
2952ad228a34SRandy Dunlap * target proc has died, @error is set to BR_DEAD_REPLY.
2953512cf465STodd Kjos */
binder_get_node_refs_for_txn(struct binder_node * node,struct binder_proc ** procp,uint32_t * error)2954512cf465STodd Kjos static struct binder_node *binder_get_node_refs_for_txn(
2955512cf465STodd Kjos struct binder_node *node,
2956512cf465STodd Kjos struct binder_proc **procp,
2957512cf465STodd Kjos uint32_t *error)
2958512cf465STodd Kjos {
2959512cf465STodd Kjos struct binder_node *target_node = NULL;
2960512cf465STodd Kjos
2961512cf465STodd Kjos binder_node_inner_lock(node);
2962512cf465STodd Kjos if (node->proc) {
2963512cf465STodd Kjos target_node = node;
2964512cf465STodd Kjos binder_inc_node_nilocked(node, 1, 0, NULL);
2965512cf465STodd Kjos binder_inc_node_tmpref_ilocked(node);
2966512cf465STodd Kjos node->proc->tmp_ref++;
2967512cf465STodd Kjos *procp = node->proc;
2968512cf465STodd Kjos } else
2969512cf465STodd Kjos *error = BR_DEAD_REPLY;
2970512cf465STodd Kjos binder_node_inner_unlock(node);
2971512cf465STodd Kjos
2972512cf465STodd Kjos return target_node;
2973512cf465STodd Kjos }
2974512cf465STodd Kjos
binder_set_txn_from_error(struct binder_transaction * t,int id,uint32_t command,int32_t param)2975bd32889eSCarlos Llamas static void binder_set_txn_from_error(struct binder_transaction *t, int id,
2976bd32889eSCarlos Llamas uint32_t command, int32_t param)
2977bd32889eSCarlos Llamas {
2978bd32889eSCarlos Llamas struct binder_thread *from = binder_get_txn_from_and_acq_inner(t);
2979bd32889eSCarlos Llamas
2980bd32889eSCarlos Llamas if (!from) {
2981bd32889eSCarlos Llamas /* annotation for sparse */
2982bd32889eSCarlos Llamas __release(&from->proc->inner_lock);
2983bd32889eSCarlos Llamas return;
2984bd32889eSCarlos Llamas }
2985bd32889eSCarlos Llamas
2986bd32889eSCarlos Llamas /* don't override existing errors */
2987bd32889eSCarlos Llamas if (from->ee.command == BR_OK)
2988bd32889eSCarlos Llamas binder_set_extended_error(&from->ee, id, command, param);
2989bd32889eSCarlos Llamas binder_inner_proc_unlock(from->proc);
2990bd32889eSCarlos Llamas binder_thread_dec_tmpref(from);
2991bd32889eSCarlos Llamas }
2992bd32889eSCarlos Llamas
binder_transaction(struct binder_proc * proc,struct binder_thread * thread,struct binder_transaction_data * tr,int reply,binder_size_t extra_buffers_size)2993777783e0SGreg Kroah-Hartman static void binder_transaction(struct binder_proc *proc,
2994777783e0SGreg Kroah-Hartman struct binder_thread *thread,
29954bfac80aSMartijn Coenen struct binder_transaction_data *tr, int reply,
29964bfac80aSMartijn Coenen binder_size_t extra_buffers_size)
2997777783e0SGreg Kroah-Hartman {
2998a056af42SMartijn Coenen int ret;
2999777783e0SGreg Kroah-Hartman struct binder_transaction *t;
300044b73962SSherry Yang struct binder_work *w;
3001777783e0SGreg Kroah-Hartman struct binder_work *tcomplete;
3002bde4a19fSTodd Kjos binder_size_t buffer_offset = 0;
3003bde4a19fSTodd Kjos binder_size_t off_start_offset, off_end_offset;
3004212265e5SArve Hjønnevåg binder_size_t off_min;
3005bde4a19fSTodd Kjos binder_size_t sg_buf_offset, sg_buf_end_offset;
30066d98eb95STodd Kjos binder_size_t user_offset = 0;
30077a4408c6STodd Kjos struct binder_proc *target_proc = NULL;
3008777783e0SGreg Kroah-Hartman struct binder_thread *target_thread = NULL;
3009777783e0SGreg Kroah-Hartman struct binder_node *target_node = NULL;
3010777783e0SGreg Kroah-Hartman struct binder_transaction *in_reply_to = NULL;
3011777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *e;
301257ada2fbSTodd Kjos uint32_t return_error = 0;
301357ada2fbSTodd Kjos uint32_t return_error_param = 0;
301457ada2fbSTodd Kjos uint32_t return_error_line = 0;
3015db6b0b81STodd Kjos binder_size_t last_fixup_obj_off = 0;
30167980240bSMartijn Coenen binder_size_t last_fixup_min_off = 0;
3017342e5c90SMartijn Coenen struct binder_context *context = proc->context;
3018d99c7333STodd Kjos int t_debug_id = atomic_inc_return(&binder_last_id);
301980093619SChuang Zhang ktime_t t_start_time = ktime_get();
302001292013SCasey Schaufler struct lsm_context lsmctx = { };
302109184ae9STodd Kjos struct list_head sgc_head;
302209184ae9STodd Kjos struct list_head pf_head;
30236d98eb95STodd Kjos const void __user *user_buffer = (const void __user *)
30246d98eb95STodd Kjos (uintptr_t)tr->data.ptr.buffer;
302509184ae9STodd Kjos INIT_LIST_HEAD(&sgc_head);
302609184ae9STodd Kjos INIT_LIST_HEAD(&pf_head);
3027777783e0SGreg Kroah-Hartman
3028777783e0SGreg Kroah-Hartman e = binder_transaction_log_add(&binder_transaction_log);
3029d99c7333STodd Kjos e->debug_id = t_debug_id;
3030777783e0SGreg Kroah-Hartman e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3031777783e0SGreg Kroah-Hartman e->from_proc = proc->pid;
3032777783e0SGreg Kroah-Hartman e->from_thread = thread->pid;
3033777783e0SGreg Kroah-Hartman e->target_handle = tr->target.handle;
3034777783e0SGreg Kroah-Hartman e->data_size = tr->data_size;
3035777783e0SGreg Kroah-Hartman e->offsets_size = tr->offsets_size;
303651d8a7ecSChristian Brauner strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME);
3037777783e0SGreg Kroah-Hartman
3038bd32889eSCarlos Llamas binder_inner_proc_lock(proc);
3039bd32889eSCarlos Llamas binder_set_extended_error(&thread->ee, t_debug_id, BR_OK, 0);
3040bd32889eSCarlos Llamas binder_inner_proc_unlock(proc);
3041bd32889eSCarlos Llamas
3042777783e0SGreg Kroah-Hartman if (reply) {
30430b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
3044777783e0SGreg Kroah-Hartman in_reply_to = thread->transaction_stack;
3045777783e0SGreg Kroah-Hartman if (in_reply_to == NULL) {
30460b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3047777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got reply transaction with no transaction stack\n",
3048777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
3049777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
305057ada2fbSTodd Kjos return_error_param = -EPROTO;
305157ada2fbSTodd Kjos return_error_line = __LINE__;
3052777783e0SGreg Kroah-Hartman goto err_empty_call_stack;
3053777783e0SGreg Kroah-Hartman }
3054777783e0SGreg Kroah-Hartman if (in_reply_to->to_thread != thread) {
30557a4408c6STodd Kjos spin_lock(&in_reply_to->lock);
3056777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
3057777783e0SGreg Kroah-Hartman proc->pid, thread->pid, in_reply_to->debug_id,
3058777783e0SGreg Kroah-Hartman in_reply_to->to_proc ?
3059777783e0SGreg Kroah-Hartman in_reply_to->to_proc->pid : 0,
3060777783e0SGreg Kroah-Hartman in_reply_to->to_thread ?
3061777783e0SGreg Kroah-Hartman in_reply_to->to_thread->pid : 0);
30627a4408c6STodd Kjos spin_unlock(&in_reply_to->lock);
30630b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3064777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
306557ada2fbSTodd Kjos return_error_param = -EPROTO;
306657ada2fbSTodd Kjos return_error_line = __LINE__;
3067777783e0SGreg Kroah-Hartman in_reply_to = NULL;
3068777783e0SGreg Kroah-Hartman goto err_bad_call_stack;
3069777783e0SGreg Kroah-Hartman }
3070777783e0SGreg Kroah-Hartman thread->transaction_stack = in_reply_to->to_parent;
30710b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
30720b89d69aSMartijn Coenen binder_set_nice(in_reply_to->saved_priority);
30730b89d69aSMartijn Coenen target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
3074777783e0SGreg Kroah-Hartman if (target_thread == NULL) {
3075324fa64cSTodd Kjos /* annotation for sparse */
3076324fa64cSTodd Kjos __release(&target_thread->proc->inner_lock);
3077a15dac8bSCarlos Llamas binder_txn_error("%d:%d reply target not found\n",
3078a15dac8bSCarlos Llamas thread->pid, proc->pid);
3079777783e0SGreg Kroah-Hartman return_error = BR_DEAD_REPLY;
308057ada2fbSTodd Kjos return_error_line = __LINE__;
3081777783e0SGreg Kroah-Hartman goto err_dead_binder;
3082777783e0SGreg Kroah-Hartman }
3083777783e0SGreg Kroah-Hartman if (target_thread->transaction_stack != in_reply_to) {
3084777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
3085777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
3086777783e0SGreg Kroah-Hartman target_thread->transaction_stack ?
3087777783e0SGreg Kroah-Hartman target_thread->transaction_stack->debug_id : 0,
3088777783e0SGreg Kroah-Hartman in_reply_to->debug_id);
30890b89d69aSMartijn Coenen binder_inner_proc_unlock(target_thread->proc);
3090777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
309157ada2fbSTodd Kjos return_error_param = -EPROTO;
309257ada2fbSTodd Kjos return_error_line = __LINE__;
3093777783e0SGreg Kroah-Hartman in_reply_to = NULL;
3094777783e0SGreg Kroah-Hartman target_thread = NULL;
3095777783e0SGreg Kroah-Hartman goto err_dead_binder;
3096777783e0SGreg Kroah-Hartman }
3097777783e0SGreg Kroah-Hartman target_proc = target_thread->proc;
30987a4408c6STodd Kjos target_proc->tmp_ref++;
30990b89d69aSMartijn Coenen binder_inner_proc_unlock(target_thread->proc);
3100777783e0SGreg Kroah-Hartman } else {
3101777783e0SGreg Kroah-Hartman if (tr->target.handle) {
3102777783e0SGreg Kroah-Hartman struct binder_ref *ref;
3103777783e0SGreg Kroah-Hartman
3104eb34983bSTodd Kjos /*
3105eb34983bSTodd Kjos * There must already be a strong ref
3106eb34983bSTodd Kjos * on this node. If so, do a strong
3107eb34983bSTodd Kjos * increment on the node to ensure it
3108eb34983bSTodd Kjos * stays alive until the transaction is
3109eb34983bSTodd Kjos * done.
3110eb34983bSTodd Kjos */
31112c1838dcSTodd Kjos binder_proc_lock(proc);
31122c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, tr->target.handle,
31132c1838dcSTodd Kjos true);
3114eb34983bSTodd Kjos if (ref) {
3115512cf465STodd Kjos target_node = binder_get_node_refs_for_txn(
3116512cf465STodd Kjos ref->node, &target_proc,
3117512cf465STodd Kjos &return_error);
3118512cf465STodd Kjos } else {
31191ae14df5SRamji Jiyani binder_user_error("%d:%d got transaction to invalid handle, %u\n",
31201ae14df5SRamji Jiyani proc->pid, thread->pid, tr->target.handle);
3121777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
3122777783e0SGreg Kroah-Hartman }
3123512cf465STodd Kjos binder_proc_unlock(proc);
3124777783e0SGreg Kroah-Hartman } else {
3125c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
3126342e5c90SMartijn Coenen target_node = context->binder_context_mgr_node;
3127512cf465STodd Kjos if (target_node)
3128512cf465STodd Kjos target_node = binder_get_node_refs_for_txn(
3129512cf465STodd Kjos target_node, &target_proc,
3130512cf465STodd Kjos &return_error);
3131512cf465STodd Kjos else
3132777783e0SGreg Kroah-Hartman return_error = BR_DEAD_REPLY;
3133c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
313449ed9694SHridya Valsaraju if (target_node && target_proc->pid == proc->pid) {
31357aa135fcSMartijn Coenen binder_user_error("%d:%d got transaction to context manager from process owning it\n",
31367aa135fcSMartijn Coenen proc->pid, thread->pid);
31377aa135fcSMartijn Coenen return_error = BR_FAILED_REPLY;
31387aa135fcSMartijn Coenen return_error_param = -EINVAL;
31397aa135fcSMartijn Coenen return_error_line = __LINE__;
31407aa135fcSMartijn Coenen goto err_invalid_target_handle;
31417aa135fcSMartijn Coenen }
3142777783e0SGreg Kroah-Hartman }
3143512cf465STodd Kjos if (!target_node) {
3144a15dac8bSCarlos Llamas binder_txn_error("%d:%d cannot find target node\n",
3145a15dac8bSCarlos Llamas thread->pid, proc->pid);
3146512cf465STodd Kjos /*
3147512cf465STodd Kjos * return_error is set above
3148512cf465STodd Kjos */
3149512cf465STodd Kjos return_error_param = -EINVAL;
315057ada2fbSTodd Kjos return_error_line = __LINE__;
3151777783e0SGreg Kroah-Hartman goto err_dead_binder;
3152777783e0SGreg Kroah-Hartman }
3153512cf465STodd Kjos e->to_node = target_node->debug_id;
31544b836a14SJann Horn if (WARN_ON(proc == target_proc)) {
3155a15dac8bSCarlos Llamas binder_txn_error("%d:%d self transactions not allowed\n",
3156a15dac8bSCarlos Llamas thread->pid, proc->pid);
31574b836a14SJann Horn return_error = BR_FAILED_REPLY;
31584b836a14SJann Horn return_error_param = -EINVAL;
31594b836a14SJann Horn return_error_line = __LINE__;
31604b836a14SJann Horn goto err_invalid_target_handle;
31614b836a14SJann Horn }
316252f88693STodd Kjos if (security_binder_transaction(proc->cred,
316352f88693STodd Kjos target_proc->cred) < 0) {
3164a15dac8bSCarlos Llamas binder_txn_error("%d:%d transaction credentials failed\n",
3165a15dac8bSCarlos Llamas thread->pid, proc->pid);
316679af7307SStephen Smalley return_error = BR_FAILED_REPLY;
316757ada2fbSTodd Kjos return_error_param = -EPERM;
316857ada2fbSTodd Kjos return_error_line = __LINE__;
316979af7307SStephen Smalley goto err_invalid_target_handle;
317079af7307SStephen Smalley }
31710b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
317244b73962SSherry Yang
317344b73962SSherry Yang w = list_first_entry_or_null(&thread->todo,
317444b73962SSherry Yang struct binder_work, entry);
317544b73962SSherry Yang if (!(tr->flags & TF_ONE_WAY) && w &&
317644b73962SSherry Yang w->type == BINDER_WORK_TRANSACTION) {
317744b73962SSherry Yang /*
317844b73962SSherry Yang * Do not allow new outgoing transaction from a
317944b73962SSherry Yang * thread that has a transaction at the head of
318044b73962SSherry Yang * its todo list. Only need to check the head
318144b73962SSherry Yang * because binder_select_thread_ilocked picks a
318244b73962SSherry Yang * thread from proc->waiting_threads to enqueue
318344b73962SSherry Yang * the transaction, and nothing is queued to the
318444b73962SSherry Yang * todo list while the thread is on waiting_threads.
318544b73962SSherry Yang */
318644b73962SSherry Yang binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n",
318744b73962SSherry Yang proc->pid, thread->pid);
318844b73962SSherry Yang binder_inner_proc_unlock(proc);
318944b73962SSherry Yang return_error = BR_FAILED_REPLY;
319044b73962SSherry Yang return_error_param = -EPROTO;
319144b73962SSherry Yang return_error_line = __LINE__;
319244b73962SSherry Yang goto err_bad_todo_list;
319344b73962SSherry Yang }
319444b73962SSherry Yang
3195777783e0SGreg Kroah-Hartman if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3196777783e0SGreg Kroah-Hartman struct binder_transaction *tmp;
3197777783e0SGreg Kroah-Hartman
3198777783e0SGreg Kroah-Hartman tmp = thread->transaction_stack;
3199777783e0SGreg Kroah-Hartman if (tmp->to_thread != thread) {
32007a4408c6STodd Kjos spin_lock(&tmp->lock);
3201777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
3202777783e0SGreg Kroah-Hartman proc->pid, thread->pid, tmp->debug_id,
3203777783e0SGreg Kroah-Hartman tmp->to_proc ? tmp->to_proc->pid : 0,
3204777783e0SGreg Kroah-Hartman tmp->to_thread ?
3205777783e0SGreg Kroah-Hartman tmp->to_thread->pid : 0);
32067a4408c6STodd Kjos spin_unlock(&tmp->lock);
32070b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3208777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
320957ada2fbSTodd Kjos return_error_param = -EPROTO;
321057ada2fbSTodd Kjos return_error_line = __LINE__;
3211777783e0SGreg Kroah-Hartman goto err_bad_call_stack;
3212777783e0SGreg Kroah-Hartman }
3213777783e0SGreg Kroah-Hartman while (tmp) {
32147a4408c6STodd Kjos struct binder_thread *from;
32157a4408c6STodd Kjos
32167a4408c6STodd Kjos spin_lock(&tmp->lock);
32177a4408c6STodd Kjos from = tmp->from;
32187a4408c6STodd Kjos if (from && from->proc == target_proc) {
32197a4408c6STodd Kjos atomic_inc(&from->tmp_ref);
32207a4408c6STodd Kjos target_thread = from;
32217a4408c6STodd Kjos spin_unlock(&tmp->lock);
32227a4408c6STodd Kjos break;
32237a4408c6STodd Kjos }
32247a4408c6STodd Kjos spin_unlock(&tmp->lock);
3225777783e0SGreg Kroah-Hartman tmp = tmp->from_parent;
3226777783e0SGreg Kroah-Hartman }
3227777783e0SGreg Kroah-Hartman }
32280b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3229777783e0SGreg Kroah-Hartman }
3230408c68b1SMartijn Coenen if (target_thread)
3231777783e0SGreg Kroah-Hartman e->to_thread = target_thread->pid;
3232777783e0SGreg Kroah-Hartman e->to_proc = target_proc->pid;
3233777783e0SGreg Kroah-Hartman
3234777783e0SGreg Kroah-Hartman /* TODO: reuse incoming transaction for reply */
3235777783e0SGreg Kroah-Hartman t = kzalloc(sizeof(*t), GFP_KERNEL);
3236777783e0SGreg Kroah-Hartman if (t == NULL) {
3237a15dac8bSCarlos Llamas binder_txn_error("%d:%d cannot allocate transaction\n",
3238a15dac8bSCarlos Llamas thread->pid, proc->pid);
3239777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
324057ada2fbSTodd Kjos return_error_param = -ENOMEM;
324157ada2fbSTodd Kjos return_error_line = __LINE__;
3242777783e0SGreg Kroah-Hartman goto err_alloc_t_failed;
3243777783e0SGreg Kroah-Hartman }
324444d8047fSTodd Kjos INIT_LIST_HEAD(&t->fd_fixups);
3245777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_TRANSACTION);
32467a4408c6STodd Kjos spin_lock_init(&t->lock);
3247777783e0SGreg Kroah-Hartman
3248777783e0SGreg Kroah-Hartman tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3249777783e0SGreg Kroah-Hartman if (tcomplete == NULL) {
3250a15dac8bSCarlos Llamas binder_txn_error("%d:%d cannot allocate work for transaction\n",
3251a15dac8bSCarlos Llamas thread->pid, proc->pid);
3252777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
325357ada2fbSTodd Kjos return_error_param = -ENOMEM;
325457ada2fbSTodd Kjos return_error_line = __LINE__;
3255777783e0SGreg Kroah-Hartman goto err_alloc_tcomplete_failed;
3256777783e0SGreg Kroah-Hartman }
3257777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3258777783e0SGreg Kroah-Hartman
3259d99c7333STodd Kjos t->debug_id = t_debug_id;
326080093619SChuang Zhang t->start_time = t_start_time;
3261777783e0SGreg Kroah-Hartman
3262777783e0SGreg Kroah-Hartman if (reply)
3263777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
32644bfac80aSMartijn Coenen "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
3265777783e0SGreg Kroah-Hartman proc->pid, thread->pid, t->debug_id,
3266777783e0SGreg Kroah-Hartman target_proc->pid, target_thread->pid,
3267777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.buffer,
3268777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.offsets,
32694bfac80aSMartijn Coenen (u64)tr->data_size, (u64)tr->offsets_size,
32704bfac80aSMartijn Coenen (u64)extra_buffers_size);
3271777783e0SGreg Kroah-Hartman else
3272777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
32734bfac80aSMartijn Coenen "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
3274777783e0SGreg Kroah-Hartman proc->pid, thread->pid, t->debug_id,
3275777783e0SGreg Kroah-Hartman target_proc->pid, target_node->debug_id,
3276777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.buffer,
3277777783e0SGreg Kroah-Hartman (u64)tr->data.ptr.offsets,
32784bfac80aSMartijn Coenen (u64)tr->data_size, (u64)tr->offsets_size,
32794bfac80aSMartijn Coenen (u64)extra_buffers_size);
3280777783e0SGreg Kroah-Hartman
3281777783e0SGreg Kroah-Hartman if (!reply && !(tr->flags & TF_ONE_WAY))
3282777783e0SGreg Kroah-Hartman t->from = thread;
3283777783e0SGreg Kroah-Hartman else
3284777783e0SGreg Kroah-Hartman t->from = NULL;
3285c21c0f9aSChuang Zhang t->from_pid = proc->pid;
3286c21c0f9aSChuang Zhang t->from_tid = thread->pid;
3287c21a80caSTodd Kjos t->sender_euid = task_euid(proc->tsk);
3288777783e0SGreg Kroah-Hartman t->to_proc = target_proc;
3289777783e0SGreg Kroah-Hartman t->to_thread = target_thread;
3290777783e0SGreg Kroah-Hartman t->code = tr->code;
3291777783e0SGreg Kroah-Hartman t->flags = tr->flags;
3292777783e0SGreg Kroah-Hartman t->priority = task_nice(current);
3293777783e0SGreg Kroah-Hartman
3294ec74136dSTodd Kjos if (target_node && target_node->txn_security_ctx) {
3295ec74136dSTodd Kjos u32 secid;
32960b050950STodd Kjos size_t added_size;
3297ec74136dSTodd Kjos
32984d5b5539STodd Kjos security_cred_getsecid(proc->cred, &secid);
32992d470c77SCasey Schaufler ret = security_secid_to_secctx(secid, &lsmctx);
33002d470c77SCasey Schaufler if (ret < 0) {
3301a15dac8bSCarlos Llamas binder_txn_error("%d:%d failed to get security context\n",
3302a15dac8bSCarlos Llamas thread->pid, proc->pid);
3303ec74136dSTodd Kjos return_error = BR_FAILED_REPLY;
3304ec74136dSTodd Kjos return_error_param = ret;
3305ec74136dSTodd Kjos return_error_line = __LINE__;
3306ec74136dSTodd Kjos goto err_get_secctx_failed;
3307ec74136dSTodd Kjos }
33086fba8981SCasey Schaufler added_size = ALIGN(lsmctx.len, sizeof(u64));
33090b050950STodd Kjos extra_buffers_size += added_size;
33100b050950STodd Kjos if (extra_buffers_size < added_size) {
3311a15dac8bSCarlos Llamas binder_txn_error("%d:%d integer overflow of extra_buffers_size\n",
3312a15dac8bSCarlos Llamas thread->pid, proc->pid);
33130b050950STodd Kjos return_error = BR_FAILED_REPLY;
331488f6c779SZhang Qilong return_error_param = -EINVAL;
33150b050950STodd Kjos return_error_line = __LINE__;
33160b050950STodd Kjos goto err_bad_extra_size;
33170b050950STodd Kjos }
3318ec74136dSTodd Kjos }
3319ec74136dSTodd Kjos
3320777783e0SGreg Kroah-Hartman trace_binder_transaction(reply, t, target_node);
3321777783e0SGreg Kroah-Hartman
332219c98724STodd Kjos t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
33234bfac80aSMartijn Coenen tr->offsets_size, extra_buffers_size,
332489f71743SCarlos Llamas !reply && (t->flags & TF_ONE_WAY));
332557ada2fbSTodd Kjos if (IS_ERR(t->buffer)) {
3326a15dac8bSCarlos Llamas char *s;
3327a15dac8bSCarlos Llamas
3328a15dac8bSCarlos Llamas ret = PTR_ERR(t->buffer);
3329a15dac8bSCarlos Llamas s = (ret == -ESRCH) ? ": vma cleared, target dead or dying"
3330a15dac8bSCarlos Llamas : (ret == -ENOSPC) ? ": no space left"
3331a15dac8bSCarlos Llamas : (ret == -ENOMEM) ? ": memory allocation failed"
3332a15dac8bSCarlos Llamas : "";
3333a15dac8bSCarlos Llamas binder_txn_error("cannot allocate buffer%s", s);
3334a15dac8bSCarlos Llamas
333557ada2fbSTodd Kjos return_error_param = PTR_ERR(t->buffer);
333657ada2fbSTodd Kjos return_error = return_error_param == -ESRCH ?
333757ada2fbSTodd Kjos BR_DEAD_REPLY : BR_FAILED_REPLY;
333857ada2fbSTodd Kjos return_error_line = __LINE__;
333957ada2fbSTodd Kjos t->buffer = NULL;
3340777783e0SGreg Kroah-Hartman goto err_binder_alloc_buf_failed;
3341777783e0SGreg Kroah-Hartman }
33426fba8981SCasey Schaufler if (lsmctx.context) {
3343bb4a2e48STodd Kjos int err;
3344ec74136dSTodd Kjos size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3345ec74136dSTodd Kjos ALIGN(tr->offsets_size, sizeof(void *)) +
3346ec74136dSTodd Kjos ALIGN(extra_buffers_size, sizeof(void *)) -
33476fba8981SCasey Schaufler ALIGN(lsmctx.len, sizeof(u64));
3348ec74136dSTodd Kjos
3349df9aabeaSCarlos Llamas t->security_ctx = t->buffer->user_data + buf_offset;
3350bb4a2e48STodd Kjos err = binder_alloc_copy_to_buffer(&target_proc->alloc,
33518ced0c62STodd Kjos t->buffer, buf_offset,
33526fba8981SCasey Schaufler lsmctx.context, lsmctx.len);
3353bb4a2e48STodd Kjos if (err) {
3354bb4a2e48STodd Kjos t->security_ctx = 0;
3355bb4a2e48STodd Kjos WARN_ON(1);
3356bb4a2e48STodd Kjos }
33576fba8981SCasey Schaufler security_release_secctx(&lsmctx);
33586fba8981SCasey Schaufler lsmctx.context = NULL;
3359ec74136dSTodd Kjos }
3360777783e0SGreg Kroah-Hartman t->buffer->debug_id = t->debug_id;
3361777783e0SGreg Kroah-Hartman t->buffer->transaction = t;
3362777783e0SGreg Kroah-Hartman t->buffer->target_node = target_node;
33630f966cbaSTodd Kjos t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF);
3364777783e0SGreg Kroah-Hartman trace_binder_transaction_alloc_buf(t->buffer);
3365777783e0SGreg Kroah-Hartman
33661a7c3d9bSTodd Kjos if (binder_alloc_copy_user_to_buffer(
33671a7c3d9bSTodd Kjos &target_proc->alloc,
33681a7c3d9bSTodd Kjos t->buffer,
33691a7c3d9bSTodd Kjos ALIGN(tr->data_size, sizeof(void *)),
33701a7c3d9bSTodd Kjos (const void __user *)
33711a7c3d9bSTodd Kjos (uintptr_t)tr->data.ptr.offsets,
33721a7c3d9bSTodd Kjos tr->offsets_size)) {
3373777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3374777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
3375777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
337657ada2fbSTodd Kjos return_error_param = -EFAULT;
337757ada2fbSTodd Kjos return_error_line = __LINE__;
3378777783e0SGreg Kroah-Hartman goto err_copy_data_failed;
3379777783e0SGreg Kroah-Hartman }
3380777783e0SGreg Kroah-Hartman if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3381777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3382777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)tr->offsets_size);
3383777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
338457ada2fbSTodd Kjos return_error_param = -EINVAL;
338557ada2fbSTodd Kjos return_error_line = __LINE__;
3386777783e0SGreg Kroah-Hartman goto err_bad_offset;
3387777783e0SGreg Kroah-Hartman }
33887980240bSMartijn Coenen if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
33897980240bSMartijn Coenen binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
33907980240bSMartijn Coenen proc->pid, thread->pid,
33917980240bSMartijn Coenen (u64)extra_buffers_size);
33927980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
339357ada2fbSTodd Kjos return_error_param = -EINVAL;
339457ada2fbSTodd Kjos return_error_line = __LINE__;
33957980240bSMartijn Coenen goto err_bad_offset;
33967980240bSMartijn Coenen }
3397bde4a19fSTodd Kjos off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3398bde4a19fSTodd Kjos buffer_offset = off_start_offset;
3399bde4a19fSTodd Kjos off_end_offset = off_start_offset + tr->offsets_size;
3400bde4a19fSTodd Kjos sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
3401a5658706SMartijn Coenen sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
34026fba8981SCasey Schaufler ALIGN(lsmctx.len, sizeof(u64));
3403212265e5SArve Hjønnevåg off_min = 0;
3404bde4a19fSTodd Kjos for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3405bde4a19fSTodd Kjos buffer_offset += sizeof(binder_size_t)) {
3406feba3900SMartijn Coenen struct binder_object_header *hdr;
34078ced0c62STodd Kjos size_t object_size;
34087a67a393STodd Kjos struct binder_object object;
34098ced0c62STodd Kjos binder_size_t object_offset;
34106d98eb95STodd Kjos binder_size_t copy_size;
3411777783e0SGreg Kroah-Hartman
3412bb4a2e48STodd Kjos if (binder_alloc_copy_from_buffer(&target_proc->alloc,
34138ced0c62STodd Kjos &object_offset,
34148ced0c62STodd Kjos t->buffer,
34158ced0c62STodd Kjos buffer_offset,
3416bb4a2e48STodd Kjos sizeof(object_offset))) {
3417a15dac8bSCarlos Llamas binder_txn_error("%d:%d copy offset from buffer failed\n",
3418a15dac8bSCarlos Llamas thread->pid, proc->pid);
3419bb4a2e48STodd Kjos return_error = BR_FAILED_REPLY;
3420bb4a2e48STodd Kjos return_error_param = -EINVAL;
3421bb4a2e48STodd Kjos return_error_line = __LINE__;
3422bb4a2e48STodd Kjos goto err_bad_offset;
3423bb4a2e48STodd Kjos }
34246d98eb95STodd Kjos
34256d98eb95STodd Kjos /*
34266d98eb95STodd Kjos * Copy the source user buffer up to the next object
34276d98eb95STodd Kjos * that will be processed.
34286d98eb95STodd Kjos */
34296d98eb95STodd Kjos copy_size = object_offset - user_offset;
34306d98eb95STodd Kjos if (copy_size && (user_offset > object_offset ||
34314df15365SCarlos Llamas object_offset > tr->data_size ||
34326d98eb95STodd Kjos binder_alloc_copy_user_to_buffer(
34336d98eb95STodd Kjos &target_proc->alloc,
34346d98eb95STodd Kjos t->buffer, user_offset,
34356d98eb95STodd Kjos user_buffer + user_offset,
34366d98eb95STodd Kjos copy_size))) {
34376d98eb95STodd Kjos binder_user_error("%d:%d got transaction with invalid data ptr\n",
34386d98eb95STodd Kjos proc->pid, thread->pid);
34396d98eb95STodd Kjos return_error = BR_FAILED_REPLY;
34406d98eb95STodd Kjos return_error_param = -EFAULT;
34416d98eb95STodd Kjos return_error_line = __LINE__;
34426d98eb95STodd Kjos goto err_copy_data_failed;
34436d98eb95STodd Kjos }
34446d98eb95STodd Kjos object_size = binder_get_object(target_proc, user_buffer,
34456d98eb95STodd Kjos t->buffer, object_offset, &object);
34468ced0c62STodd Kjos if (object_size == 0 || object_offset < off_min) {
3447feba3900SMartijn Coenen binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
34488ced0c62STodd Kjos proc->pid, thread->pid,
34498ced0c62STodd Kjos (u64)object_offset,
3450212265e5SArve Hjønnevåg (u64)off_min,
3451feba3900SMartijn Coenen (u64)t->buffer->data_size);
3452777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
345357ada2fbSTodd Kjos return_error_param = -EINVAL;
345457ada2fbSTodd Kjos return_error_line = __LINE__;
3455777783e0SGreg Kroah-Hartman goto err_bad_offset;
3456777783e0SGreg Kroah-Hartman }
34576d98eb95STodd Kjos /*
34586d98eb95STodd Kjos * Set offset to the next buffer fragment to be
34596d98eb95STodd Kjos * copied
34606d98eb95STodd Kjos */
34616d98eb95STodd Kjos user_offset = object_offset + object_size;
3462feba3900SMartijn Coenen
34637a67a393STodd Kjos hdr = &object.hdr;
34648ced0c62STodd Kjos off_min = object_offset + object_size;
3465feba3900SMartijn Coenen switch (hdr->type) {
3466777783e0SGreg Kroah-Hartman case BINDER_TYPE_BINDER:
3467777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_BINDER: {
3468feba3900SMartijn Coenen struct flat_binder_object *fp;
3469777783e0SGreg Kroah-Hartman
3470feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
3471a056af42SMartijn Coenen ret = binder_translate_binder(fp, t, thread);
3472bb4a2e48STodd Kjos
3473bb4a2e48STodd Kjos if (ret < 0 ||
3474bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3475bb4a2e48STodd Kjos t->buffer,
3476bb4a2e48STodd Kjos object_offset,
3477bb4a2e48STodd Kjos fp, sizeof(*fp))) {
3478a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate binder failed\n",
3479a15dac8bSCarlos Llamas thread->pid, proc->pid);
3480777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
348157ada2fbSTodd Kjos return_error_param = ret;
348257ada2fbSTodd Kjos return_error_line = __LINE__;
3483a056af42SMartijn Coenen goto err_translate_failed;
3484777783e0SGreg Kroah-Hartman }
3485777783e0SGreg Kroah-Hartman } break;
3486777783e0SGreg Kroah-Hartman case BINDER_TYPE_HANDLE:
3487777783e0SGreg Kroah-Hartman case BINDER_TYPE_WEAK_HANDLE: {
3488feba3900SMartijn Coenen struct flat_binder_object *fp;
34890a3ffab9SArve Hjønnevåg
3490feba3900SMartijn Coenen fp = to_flat_binder_object(hdr);
3491a056af42SMartijn Coenen ret = binder_translate_handle(fp, t, thread);
3492bb4a2e48STodd Kjos if (ret < 0 ||
3493bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3494bb4a2e48STodd Kjos t->buffer,
3495bb4a2e48STodd Kjos object_offset,
3496bb4a2e48STodd Kjos fp, sizeof(*fp))) {
3497a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate handle failed\n",
3498a15dac8bSCarlos Llamas thread->pid, proc->pid);
3499777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
350057ada2fbSTodd Kjos return_error_param = ret;
350157ada2fbSTodd Kjos return_error_line = __LINE__;
3502a056af42SMartijn Coenen goto err_translate_failed;
3503777783e0SGreg Kroah-Hartman }
3504777783e0SGreg Kroah-Hartman } break;
3505777783e0SGreg Kroah-Hartman
3506777783e0SGreg Kroah-Hartman case BINDER_TYPE_FD: {
3507feba3900SMartijn Coenen struct binder_fd_object *fp = to_binder_fd_object(hdr);
35088ced0c62STodd Kjos binder_size_t fd_offset = object_offset +
35098ced0c62STodd Kjos (uintptr_t)&fp->fd - (uintptr_t)fp;
35108ced0c62STodd Kjos int ret = binder_translate_fd(fp->fd, fd_offset, t,
35118ced0c62STodd Kjos thread, in_reply_to);
3512777783e0SGreg Kroah-Hartman
3513bb4a2e48STodd Kjos fp->pad_binder = 0;
3514bb4a2e48STodd Kjos if (ret < 0 ||
3515bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3516bb4a2e48STodd Kjos t->buffer,
3517bb4a2e48STodd Kjos object_offset,
3518bb4a2e48STodd Kjos fp, sizeof(*fp))) {
3519a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate fd failed\n",
3520a15dac8bSCarlos Llamas thread->pid, proc->pid);
3521777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
352244d8047fSTodd Kjos return_error_param = ret;
352357ada2fbSTodd Kjos return_error_line = __LINE__;
3524a056af42SMartijn Coenen goto err_translate_failed;
3525777783e0SGreg Kroah-Hartman }
3526777783e0SGreg Kroah-Hartman } break;
3527def95c73SMartijn Coenen case BINDER_TYPE_FDA: {
3528db6b0b81STodd Kjos struct binder_object ptr_object;
3529db6b0b81STodd Kjos binder_size_t parent_offset;
3530656e01f3STodd Kjos struct binder_object user_object;
3531656e01f3STodd Kjos size_t user_parent_size;
3532def95c73SMartijn Coenen struct binder_fd_array_object *fda =
3533def95c73SMartijn Coenen to_binder_fd_array_object(hdr);
353416981742STodd Kjos size_t num_valid = (buffer_offset - off_start_offset) /
3535bde4a19fSTodd Kjos sizeof(binder_size_t);
3536def95c73SMartijn Coenen struct binder_buffer_object *parent =
3537db6b0b81STodd Kjos binder_validate_ptr(target_proc, t->buffer,
3538db6b0b81STodd Kjos &ptr_object, fda->parent,
3539db6b0b81STodd Kjos off_start_offset,
3540db6b0b81STodd Kjos &parent_offset,
3541bde4a19fSTodd Kjos num_valid);
3542def95c73SMartijn Coenen if (!parent) {
3543def95c73SMartijn Coenen binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3544def95c73SMartijn Coenen proc->pid, thread->pid);
3545def95c73SMartijn Coenen return_error = BR_FAILED_REPLY;
354657ada2fbSTodd Kjos return_error_param = -EINVAL;
354757ada2fbSTodd Kjos return_error_line = __LINE__;
3548def95c73SMartijn Coenen goto err_bad_parent;
3549def95c73SMartijn Coenen }
3550db6b0b81STodd Kjos if (!binder_validate_fixup(target_proc, t->buffer,
3551db6b0b81STodd Kjos off_start_offset,
3552db6b0b81STodd Kjos parent_offset,
3553db6b0b81STodd Kjos fda->parent_offset,
3554db6b0b81STodd Kjos last_fixup_obj_off,
3555def95c73SMartijn Coenen last_fixup_min_off)) {
3556def95c73SMartijn Coenen binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3557def95c73SMartijn Coenen proc->pid, thread->pid);
3558def95c73SMartijn Coenen return_error = BR_FAILED_REPLY;
355957ada2fbSTodd Kjos return_error_param = -EINVAL;
356057ada2fbSTodd Kjos return_error_line = __LINE__;
3561def95c73SMartijn Coenen goto err_bad_parent;
3562def95c73SMartijn Coenen }
3563656e01f3STodd Kjos /*
3564656e01f3STodd Kjos * We need to read the user version of the parent
3565656e01f3STodd Kjos * object to get the original user offset
3566656e01f3STodd Kjos */
3567656e01f3STodd Kjos user_parent_size =
3568656e01f3STodd Kjos binder_get_object(proc, user_buffer, t->buffer,
3569656e01f3STodd Kjos parent_offset, &user_object);
3570656e01f3STodd Kjos if (user_parent_size != sizeof(user_object.bbo)) {
3571656e01f3STodd Kjos binder_user_error("%d:%d invalid ptr object size: %zd vs %zd\n",
3572656e01f3STodd Kjos proc->pid, thread->pid,
3573656e01f3STodd Kjos user_parent_size,
3574656e01f3STodd Kjos sizeof(user_object.bbo));
3575656e01f3STodd Kjos return_error = BR_FAILED_REPLY;
3576656e01f3STodd Kjos return_error_param = -EINVAL;
3577656e01f3STodd Kjos return_error_line = __LINE__;
3578656e01f3STodd Kjos goto err_bad_parent;
3579656e01f3STodd Kjos }
358009184ae9STodd Kjos ret = binder_translate_fd_array(&pf_head, fda,
358109184ae9STodd Kjos user_buffer, parent,
3582656e01f3STodd Kjos &user_object.bbo, t,
3583656e01f3STodd Kjos thread, in_reply_to);
35846d98eb95STodd Kjos if (!ret)
35856d98eb95STodd Kjos ret = binder_alloc_copy_to_buffer(&target_proc->alloc,
35866d98eb95STodd Kjos t->buffer,
35876d98eb95STodd Kjos object_offset,
35886d98eb95STodd Kjos fda, sizeof(*fda));
35896d98eb95STodd Kjos if (ret) {
3590a15dac8bSCarlos Llamas binder_txn_error("%d:%d translate fd array failed\n",
3591a15dac8bSCarlos Llamas thread->pid, proc->pid);
3592def95c73SMartijn Coenen return_error = BR_FAILED_REPLY;
35936d98eb95STodd Kjos return_error_param = ret > 0 ? -EINVAL : ret;
359457ada2fbSTodd Kjos return_error_line = __LINE__;
3595def95c73SMartijn Coenen goto err_translate_failed;
3596def95c73SMartijn Coenen }
3597db6b0b81STodd Kjos last_fixup_obj_off = parent_offset;
3598def95c73SMartijn Coenen last_fixup_min_off =
3599def95c73SMartijn Coenen fda->parent_offset + sizeof(u32) * fda->num_fds;
3600def95c73SMartijn Coenen } break;
36017980240bSMartijn Coenen case BINDER_TYPE_PTR: {
36027980240bSMartijn Coenen struct binder_buffer_object *bp =
36037980240bSMartijn Coenen to_binder_buffer_object(hdr);
3604bde4a19fSTodd Kjos size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3605bde4a19fSTodd Kjos size_t num_valid;
3606777783e0SGreg Kroah-Hartman
36077980240bSMartijn Coenen if (bp->length > buf_left) {
36087980240bSMartijn Coenen binder_user_error("%d:%d got transaction with too large buffer\n",
36097980240bSMartijn Coenen proc->pid, thread->pid);
36107980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
361157ada2fbSTodd Kjos return_error_param = -EINVAL;
361257ada2fbSTodd Kjos return_error_line = __LINE__;
36137980240bSMartijn Coenen goto err_bad_offset;
36147980240bSMartijn Coenen }
361509184ae9STodd Kjos ret = binder_defer_copy(&sgc_head, sg_buf_offset,
361609184ae9STodd Kjos (const void __user *)(uintptr_t)bp->buffer,
361709184ae9STodd Kjos bp->length);
361809184ae9STodd Kjos if (ret) {
3619a15dac8bSCarlos Llamas binder_txn_error("%d:%d deferred copy failed\n",
3620a15dac8bSCarlos Llamas thread->pid, proc->pid);
36217980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
362209184ae9STodd Kjos return_error_param = ret;
362357ada2fbSTodd Kjos return_error_line = __LINE__;
362409184ae9STodd Kjos goto err_translate_failed;
36257980240bSMartijn Coenen }
36267980240bSMartijn Coenen /* Fixup buffer pointer to target proc address space */
3627df9aabeaSCarlos Llamas bp->buffer = t->buffer->user_data + sg_buf_offset;
3628bde4a19fSTodd Kjos sg_buf_offset += ALIGN(bp->length, sizeof(u64));
36297980240bSMartijn Coenen
363016981742STodd Kjos num_valid = (buffer_offset - off_start_offset) /
3631bde4a19fSTodd Kjos sizeof(binder_size_t);
363209184ae9STodd Kjos ret = binder_fixup_parent(&pf_head, t,
363309184ae9STodd Kjos thread, bp,
3634db6b0b81STodd Kjos off_start_offset,
3635bde4a19fSTodd Kjos num_valid,
3636db6b0b81STodd Kjos last_fixup_obj_off,
36377980240bSMartijn Coenen last_fixup_min_off);
3638bb4a2e48STodd Kjos if (ret < 0 ||
3639bb4a2e48STodd Kjos binder_alloc_copy_to_buffer(&target_proc->alloc,
3640bb4a2e48STodd Kjos t->buffer,
3641bb4a2e48STodd Kjos object_offset,
3642bb4a2e48STodd Kjos bp, sizeof(*bp))) {
3643a15dac8bSCarlos Llamas binder_txn_error("%d:%d failed to fixup parent\n",
3644a15dac8bSCarlos Llamas thread->pid, proc->pid);
36457980240bSMartijn Coenen return_error = BR_FAILED_REPLY;
364657ada2fbSTodd Kjos return_error_param = ret;
364757ada2fbSTodd Kjos return_error_line = __LINE__;
36487980240bSMartijn Coenen goto err_translate_failed;
36497980240bSMartijn Coenen }
3650db6b0b81STodd Kjos last_fixup_obj_off = object_offset;
36517980240bSMartijn Coenen last_fixup_min_off = 0;
36527980240bSMartijn Coenen } break;
3653777783e0SGreg Kroah-Hartman default:
3654777783e0SGreg Kroah-Hartman binder_user_error("%d:%d got transaction with invalid object type, %x\n",
3655feba3900SMartijn Coenen proc->pid, thread->pid, hdr->type);
3656777783e0SGreg Kroah-Hartman return_error = BR_FAILED_REPLY;
365757ada2fbSTodd Kjos return_error_param = -EINVAL;
365857ada2fbSTodd Kjos return_error_line = __LINE__;
3659777783e0SGreg Kroah-Hartman goto err_bad_object_type;
3660777783e0SGreg Kroah-Hartman }
3661777783e0SGreg Kroah-Hartman }
36626d98eb95STodd Kjos /* Done processing objects, copy the rest of the buffer */
36636d98eb95STodd Kjos if (binder_alloc_copy_user_to_buffer(
36646d98eb95STodd Kjos &target_proc->alloc,
36656d98eb95STodd Kjos t->buffer, user_offset,
36666d98eb95STodd Kjos user_buffer + user_offset,
36676d98eb95STodd Kjos tr->data_size - user_offset)) {
36686d98eb95STodd Kjos binder_user_error("%d:%d got transaction with invalid data ptr\n",
36696d98eb95STodd Kjos proc->pid, thread->pid);
36706d98eb95STodd Kjos return_error = BR_FAILED_REPLY;
36716d98eb95STodd Kjos return_error_param = -EFAULT;
36726d98eb95STodd Kjos return_error_line = __LINE__;
36736d98eb95STodd Kjos goto err_copy_data_failed;
36746d98eb95STodd Kjos }
367509184ae9STodd Kjos
367609184ae9STodd Kjos ret = binder_do_deferred_txn_copies(&target_proc->alloc, t->buffer,
367709184ae9STodd Kjos &sgc_head, &pf_head);
367809184ae9STodd Kjos if (ret) {
367909184ae9STodd Kjos binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
368009184ae9STodd Kjos proc->pid, thread->pid);
368109184ae9STodd Kjos return_error = BR_FAILED_REPLY;
368209184ae9STodd Kjos return_error_param = ret;
368309184ae9STodd Kjos return_error_line = __LINE__;
368409184ae9STodd Kjos goto err_copy_data_failed;
368509184ae9STodd Kjos }
3686a7dc1e6fSHang Lu if (t->buffer->oneway_spam_suspect)
3687a7dc1e6fSHang Lu tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT;
3688a7dc1e6fSHang Lu else
3689ccae6f67STodd Kjos tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
3690673068eeSTodd Kjos t->work.type = BINDER_WORK_TRANSACTION;
3691ccae6f67STodd Kjos
3692777783e0SGreg Kroah-Hartman if (reply) {
3693148ade2cSMartijn Coenen binder_enqueue_thread_work(thread, tcomplete);
36940b89d69aSMartijn Coenen binder_inner_proc_lock(target_proc);
3695b564171aSLi Li if (target_thread->is_dead) {
3696b564171aSLi Li return_error = BR_DEAD_REPLY;
36970b89d69aSMartijn Coenen binder_inner_proc_unlock(target_proc);
36987a4408c6STodd Kjos goto err_dead_proc_or_thread;
36990b89d69aSMartijn Coenen }
3700777783e0SGreg Kroah-Hartman BUG_ON(t->buffer->async_transaction != 0);
37010b89d69aSMartijn Coenen binder_pop_transaction_ilocked(target_thread, in_reply_to);
3702148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(target_thread, &t->work);
3703432ff1e9SMarco Ballesio target_proc->outstanding_txns++;
37040b89d69aSMartijn Coenen binder_inner_proc_unlock(target_proc);
3705408c68b1SMartijn Coenen wake_up_interruptible_sync(&target_thread->wait);
3706b6d282ceSTodd Kjos binder_free_transaction(in_reply_to);
3707777783e0SGreg Kroah-Hartman } else if (!(t->flags & TF_ONE_WAY)) {
3708777783e0SGreg Kroah-Hartman BUG_ON(t->buffer->async_transaction != 0);
37090b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
3710148ade2cSMartijn Coenen /*
3711148ade2cSMartijn Coenen * Defer the TRANSACTION_COMPLETE, so we don't return to
3712148ade2cSMartijn Coenen * userspace immediately; this allows the target process to
3713148ade2cSMartijn Coenen * immediately start processing this transaction, reducing
3714148ade2cSMartijn Coenen * latency. We will then return the TRANSACTION_COMPLETE when
3715148ade2cSMartijn Coenen * the target replies (or there is an error).
3716148ade2cSMartijn Coenen */
3717148ade2cSMartijn Coenen binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
3718777783e0SGreg Kroah-Hartman t->need_reply = 1;
3719777783e0SGreg Kroah-Hartman t->from_parent = thread->transaction_stack;
3720777783e0SGreg Kroah-Hartman thread->transaction_stack = t;
37210b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
3722432ff1e9SMarco Ballesio return_error = binder_proc_transaction(t,
3723432ff1e9SMarco Ballesio target_proc, target_thread);
3724432ff1e9SMarco Ballesio if (return_error) {
37250b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
37260b89d69aSMartijn Coenen binder_pop_transaction_ilocked(thread, t);
37270b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
37287a4408c6STodd Kjos goto err_dead_proc_or_thread;
37297a4408c6STodd Kjos }
3730777783e0SGreg Kroah-Hartman } else {
3731777783e0SGreg Kroah-Hartman BUG_ON(target_node == NULL);
3732777783e0SGreg Kroah-Hartman BUG_ON(t->buffer->async_transaction != 1);
3733432ff1e9SMarco Ballesio return_error = binder_proc_transaction(t, target_proc, NULL);
37340567461aSLi Li /*
37350567461aSLi Li * Let the caller know when async transaction reaches a frozen
37360567461aSLi Li * process and is put in a pending queue, waiting for the target
37370567461aSLi Li * process to be unfrozen.
37380567461aSLi Li */
37390567461aSLi Li if (return_error == BR_TRANSACTION_PENDING_FROZEN)
37400567461aSLi Li tcomplete->type = BINDER_WORK_TRANSACTION_PENDING;
37410567461aSLi Li binder_enqueue_thread_work(thread, tcomplete);
37420567461aSLi Li if (return_error &&
37430567461aSLi Li return_error != BR_TRANSACTION_PENDING_FROZEN)
37447a4408c6STodd Kjos goto err_dead_proc_or_thread;
3745777783e0SGreg Kroah-Hartman }
37467a4408c6STodd Kjos if (target_thread)
37477a4408c6STodd Kjos binder_thread_dec_tmpref(target_thread);
37487a4408c6STodd Kjos binder_proc_dec_tmpref(target_proc);
3749512cf465STodd Kjos if (target_node)
3750512cf465STodd Kjos binder_dec_node_tmpref(target_node);
3751d99c7333STodd Kjos /*
3752d99c7333STodd Kjos * write barrier to synchronize with initialization
3753d99c7333STodd Kjos * of log entry
3754d99c7333STodd Kjos */
3755d99c7333STodd Kjos smp_wmb();
3756d99c7333STodd Kjos WRITE_ONCE(e->debug_id_done, t_debug_id);
3757777783e0SGreg Kroah-Hartman return;
3758777783e0SGreg Kroah-Hartman
37597a4408c6STodd Kjos err_dead_proc_or_thread:
3760a15dac8bSCarlos Llamas binder_txn_error("%d:%d dead process or thread\n",
3761a15dac8bSCarlos Llamas thread->pid, proc->pid);
37627a4408c6STodd Kjos return_error_line = __LINE__;
3763d53bebdfSXu YiPing binder_dequeue_work(proc, tcomplete);
3764a056af42SMartijn Coenen err_translate_failed:
3765777783e0SGreg Kroah-Hartman err_bad_object_type:
3766777783e0SGreg Kroah-Hartman err_bad_offset:
3767def95c73SMartijn Coenen err_bad_parent:
3768777783e0SGreg Kroah-Hartman err_copy_data_failed:
376909184ae9STodd Kjos binder_cleanup_deferred_txn_lists(&sgc_head, &pf_head);
377044d8047fSTodd Kjos binder_free_txn_fixups(t);
3771777783e0SGreg Kroah-Hartman trace_binder_transaction_failed_buffer_release(t->buffer);
37725fdb55c1STodd Kjos binder_transaction_buffer_release(target_proc, NULL, t->buffer,
3773bde4a19fSTodd Kjos buffer_offset, true);
3774512cf465STodd Kjos if (target_node)
3775512cf465STodd Kjos binder_dec_node_tmpref(target_node);
3776eb34983bSTodd Kjos target_node = NULL;
3777777783e0SGreg Kroah-Hartman t->buffer->transaction = NULL;
377819c98724STodd Kjos binder_alloc_free_buf(&target_proc->alloc, t->buffer);
3779777783e0SGreg Kroah-Hartman err_binder_alloc_buf_failed:
37800b050950STodd Kjos err_bad_extra_size:
37816fba8981SCasey Schaufler if (lsmctx.context)
37826fba8981SCasey Schaufler security_release_secctx(&lsmctx);
3783ec74136dSTodd Kjos err_get_secctx_failed:
3784777783e0SGreg Kroah-Hartman kfree(tcomplete);
3785777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3786777783e0SGreg Kroah-Hartman err_alloc_tcomplete_failed:
37871987f112SFrankie.Chang if (trace_binder_txn_latency_free_enabled())
37881987f112SFrankie.Chang binder_txn_latency_free(t);
3789777783e0SGreg Kroah-Hartman kfree(t);
3790777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION);
3791777783e0SGreg Kroah-Hartman err_alloc_t_failed:
379244b73962SSherry Yang err_bad_todo_list:
3793777783e0SGreg Kroah-Hartman err_bad_call_stack:
3794777783e0SGreg Kroah-Hartman err_empty_call_stack:
3795777783e0SGreg Kroah-Hartman err_dead_binder:
3796777783e0SGreg Kroah-Hartman err_invalid_target_handle:
3797512cf465STodd Kjos if (target_node) {
3798eb34983bSTodd Kjos binder_dec_node(target_node, 1, 0);
3799512cf465STodd Kjos binder_dec_node_tmpref(target_node);
3800512cf465STodd Kjos }
3801eb34983bSTodd Kjos
3802777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
380348dc1c36SCarlos Llamas "%d:%d transaction %s to %d:%d failed %d/%d/%d, code %u size %lld-%lld line %d\n",
38049474be34SCarlos Llamas proc->pid, thread->pid, reply ? "reply" :
38059474be34SCarlos Llamas (tr->flags & TF_ONE_WAY ? "async" : "call"),
38069474be34SCarlos Llamas target_proc ? target_proc->pid : 0,
38079474be34SCarlos Llamas target_thread ? target_thread->pid : 0,
38089474be34SCarlos Llamas t_debug_id, return_error, return_error_param,
380948dc1c36SCarlos Llamas tr->code, (u64)tr->data_size, (u64)tr->offsets_size,
381057ada2fbSTodd Kjos return_error_line);
3811777783e0SGreg Kroah-Hartman
3812dafa5e9aSCarlos Llamas if (target_thread)
3813dafa5e9aSCarlos Llamas binder_thread_dec_tmpref(target_thread);
3814dafa5e9aSCarlos Llamas if (target_proc)
3815dafa5e9aSCarlos Llamas binder_proc_dec_tmpref(target_proc);
3816dafa5e9aSCarlos Llamas
3817777783e0SGreg Kroah-Hartman {
3818777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *fe;
3819777783e0SGreg Kroah-Hartman
382057ada2fbSTodd Kjos e->return_error = return_error;
382157ada2fbSTodd Kjos e->return_error_param = return_error_param;
382257ada2fbSTodd Kjos e->return_error_line = return_error_line;
3823777783e0SGreg Kroah-Hartman fe = binder_transaction_log_add(&binder_transaction_log_failed);
3824777783e0SGreg Kroah-Hartman *fe = *e;
3825d99c7333STodd Kjos /*
3826d99c7333STodd Kjos * write barrier to synchronize with initialization
3827d99c7333STodd Kjos * of log entry
3828d99c7333STodd Kjos */
3829d99c7333STodd Kjos smp_wmb();
3830d99c7333STodd Kjos WRITE_ONCE(e->debug_id_done, t_debug_id);
3831d99c7333STodd Kjos WRITE_ONCE(fe->debug_id_done, t_debug_id);
3832777783e0SGreg Kroah-Hartman }
3833777783e0SGreg Kroah-Hartman
383426549d17STodd Kjos BUG_ON(thread->return_error.cmd != BR_OK);
3835777783e0SGreg Kroah-Hartman if (in_reply_to) {
3836bd32889eSCarlos Llamas binder_set_txn_from_error(in_reply_to, t_debug_id,
3837bd32889eSCarlos Llamas return_error, return_error_param);
383826549d17STodd Kjos thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
3839148ade2cSMartijn Coenen binder_enqueue_thread_work(thread, &thread->return_error.work);
3840777783e0SGreg Kroah-Hartman binder_send_failed_reply(in_reply_to, return_error);
384126549d17STodd Kjos } else {
3842bd32889eSCarlos Llamas binder_inner_proc_lock(proc);
3843bd32889eSCarlos Llamas binder_set_extended_error(&thread->ee, t_debug_id,
3844bd32889eSCarlos Llamas return_error, return_error_param);
3845bd32889eSCarlos Llamas binder_inner_proc_unlock(proc);
384626549d17STodd Kjos thread->return_error.cmd = return_error;
3847148ade2cSMartijn Coenen binder_enqueue_thread_work(thread, &thread->return_error.work);
384826549d17STodd Kjos }
3849777783e0SGreg Kroah-Hartman }
3850777783e0SGreg Kroah-Hartman
3851d579b04aSYu-Ting Tseng static int
binder_request_freeze_notification(struct binder_proc * proc,struct binder_thread * thread,struct binder_handle_cookie * handle_cookie)3852d579b04aSYu-Ting Tseng binder_request_freeze_notification(struct binder_proc *proc,
3853d579b04aSYu-Ting Tseng struct binder_thread *thread,
3854d579b04aSYu-Ting Tseng struct binder_handle_cookie *handle_cookie)
3855d579b04aSYu-Ting Tseng {
3856d579b04aSYu-Ting Tseng struct binder_ref_freeze *freeze;
3857d579b04aSYu-Ting Tseng struct binder_ref *ref;
3858d579b04aSYu-Ting Tseng
3859d579b04aSYu-Ting Tseng freeze = kzalloc(sizeof(*freeze), GFP_KERNEL);
3860d579b04aSYu-Ting Tseng if (!freeze)
3861d579b04aSYu-Ting Tseng return -ENOMEM;
3862d579b04aSYu-Ting Tseng binder_proc_lock(proc);
3863d579b04aSYu-Ting Tseng ref = binder_get_ref_olocked(proc, handle_cookie->handle, false);
3864d579b04aSYu-Ting Tseng if (!ref) {
3865d579b04aSYu-Ting Tseng binder_user_error("%d:%d BC_REQUEST_FREEZE_NOTIFICATION invalid ref %d\n",
3866d579b04aSYu-Ting Tseng proc->pid, thread->pid, handle_cookie->handle);
3867d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3868d579b04aSYu-Ting Tseng kfree(freeze);
3869d579b04aSYu-Ting Tseng return -EINVAL;
3870d579b04aSYu-Ting Tseng }
3871d579b04aSYu-Ting Tseng
3872d579b04aSYu-Ting Tseng binder_node_lock(ref->node);
3873ca63c669SCarlos Llamas if (ref->freeze) {
3874ca63c669SCarlos Llamas binder_user_error("%d:%d BC_REQUEST_FREEZE_NOTIFICATION already set\n",
3875ca63c669SCarlos Llamas proc->pid, thread->pid);
3876d579b04aSYu-Ting Tseng binder_node_unlock(ref->node);
3877d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3878d579b04aSYu-Ting Tseng kfree(freeze);
3879d579b04aSYu-Ting Tseng return -EINVAL;
3880d579b04aSYu-Ting Tseng }
3881d579b04aSYu-Ting Tseng
3882d579b04aSYu-Ting Tseng binder_stats_created(BINDER_STAT_FREEZE);
3883d579b04aSYu-Ting Tseng INIT_LIST_HEAD(&freeze->work.entry);
3884d579b04aSYu-Ting Tseng freeze->cookie = handle_cookie->cookie;
3885d579b04aSYu-Ting Tseng freeze->work.type = BINDER_WORK_FROZEN_BINDER;
3886d579b04aSYu-Ting Tseng ref->freeze = freeze;
3887d579b04aSYu-Ting Tseng
3888ca63c669SCarlos Llamas if (ref->node->proc) {
3889ca63c669SCarlos Llamas binder_inner_proc_lock(ref->node->proc);
3890ca63c669SCarlos Llamas freeze->is_frozen = ref->node->proc->is_frozen;
3891ca63c669SCarlos Llamas binder_inner_proc_unlock(ref->node->proc);
3892ca63c669SCarlos Llamas
3893d579b04aSYu-Ting Tseng binder_inner_proc_lock(proc);
3894ca63c669SCarlos Llamas binder_enqueue_work_ilocked(&freeze->work, &proc->todo);
3895d579b04aSYu-Ting Tseng binder_wakeup_proc_ilocked(proc);
3896d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
3897ca63c669SCarlos Llamas }
3898d579b04aSYu-Ting Tseng
3899d579b04aSYu-Ting Tseng binder_node_unlock(ref->node);
3900d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3901d579b04aSYu-Ting Tseng return 0;
3902d579b04aSYu-Ting Tseng }
3903d579b04aSYu-Ting Tseng
3904d579b04aSYu-Ting Tseng static int
binder_clear_freeze_notification(struct binder_proc * proc,struct binder_thread * thread,struct binder_handle_cookie * handle_cookie)3905d579b04aSYu-Ting Tseng binder_clear_freeze_notification(struct binder_proc *proc,
3906d579b04aSYu-Ting Tseng struct binder_thread *thread,
3907d579b04aSYu-Ting Tseng struct binder_handle_cookie *handle_cookie)
3908d579b04aSYu-Ting Tseng {
3909d579b04aSYu-Ting Tseng struct binder_ref_freeze *freeze;
3910d579b04aSYu-Ting Tseng struct binder_ref *ref;
3911d579b04aSYu-Ting Tseng
3912d579b04aSYu-Ting Tseng binder_proc_lock(proc);
3913d579b04aSYu-Ting Tseng ref = binder_get_ref_olocked(proc, handle_cookie->handle, false);
3914d579b04aSYu-Ting Tseng if (!ref) {
3915d579b04aSYu-Ting Tseng binder_user_error("%d:%d BC_CLEAR_FREEZE_NOTIFICATION invalid ref %d\n",
3916d579b04aSYu-Ting Tseng proc->pid, thread->pid, handle_cookie->handle);
3917d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3918d579b04aSYu-Ting Tseng return -EINVAL;
3919d579b04aSYu-Ting Tseng }
3920d579b04aSYu-Ting Tseng
3921d579b04aSYu-Ting Tseng binder_node_lock(ref->node);
3922d579b04aSYu-Ting Tseng
3923d579b04aSYu-Ting Tseng if (!ref->freeze) {
3924d579b04aSYu-Ting Tseng binder_user_error("%d:%d BC_CLEAR_FREEZE_NOTIFICATION freeze notification not active\n",
3925d579b04aSYu-Ting Tseng proc->pid, thread->pid);
3926d579b04aSYu-Ting Tseng binder_node_unlock(ref->node);
3927d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3928d579b04aSYu-Ting Tseng return -EINVAL;
3929d579b04aSYu-Ting Tseng }
3930d579b04aSYu-Ting Tseng freeze = ref->freeze;
3931d579b04aSYu-Ting Tseng binder_inner_proc_lock(proc);
3932d579b04aSYu-Ting Tseng if (freeze->cookie != handle_cookie->cookie) {
3933d579b04aSYu-Ting Tseng binder_user_error("%d:%d BC_CLEAR_FREEZE_NOTIFICATION freeze notification cookie mismatch %016llx != %016llx\n",
3934d579b04aSYu-Ting Tseng proc->pid, thread->pid, (u64)freeze->cookie,
3935d579b04aSYu-Ting Tseng (u64)handle_cookie->cookie);
3936d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
3937d579b04aSYu-Ting Tseng binder_node_unlock(ref->node);
3938d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3939d579b04aSYu-Ting Tseng return -EINVAL;
3940d579b04aSYu-Ting Tseng }
3941d579b04aSYu-Ting Tseng ref->freeze = NULL;
3942d579b04aSYu-Ting Tseng /*
3943d579b04aSYu-Ting Tseng * Take the existing freeze object and overwrite its work type. There are three cases here:
3944d579b04aSYu-Ting Tseng * 1. No pending notification. In this case just add the work to the queue.
3945d579b04aSYu-Ting Tseng * 2. A notification was sent and is pending an ack from userspace. Once an ack arrives, we
3946d579b04aSYu-Ting Tseng * should resend with the new work type.
3947d579b04aSYu-Ting Tseng * 3. A notification is pending to be sent. Since the work is already in the queue, nothing
3948d579b04aSYu-Ting Tseng * needs to be done here.
3949d579b04aSYu-Ting Tseng */
3950d579b04aSYu-Ting Tseng freeze->work.type = BINDER_WORK_CLEAR_FREEZE_NOTIFICATION;
3951d579b04aSYu-Ting Tseng if (list_empty(&freeze->work.entry)) {
3952d579b04aSYu-Ting Tseng binder_enqueue_work_ilocked(&freeze->work, &proc->todo);
3953d579b04aSYu-Ting Tseng binder_wakeup_proc_ilocked(proc);
3954d579b04aSYu-Ting Tseng } else if (freeze->sent) {
3955d579b04aSYu-Ting Tseng freeze->resend = true;
3956d579b04aSYu-Ting Tseng }
3957d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
3958d579b04aSYu-Ting Tseng binder_node_unlock(ref->node);
3959d579b04aSYu-Ting Tseng binder_proc_unlock(proc);
3960d579b04aSYu-Ting Tseng return 0;
3961d579b04aSYu-Ting Tseng }
3962d579b04aSYu-Ting Tseng
3963d579b04aSYu-Ting Tseng static int
binder_freeze_notification_done(struct binder_proc * proc,struct binder_thread * thread,binder_uintptr_t cookie)3964d579b04aSYu-Ting Tseng binder_freeze_notification_done(struct binder_proc *proc,
3965d579b04aSYu-Ting Tseng struct binder_thread *thread,
3966d579b04aSYu-Ting Tseng binder_uintptr_t cookie)
3967d579b04aSYu-Ting Tseng {
3968d579b04aSYu-Ting Tseng struct binder_ref_freeze *freeze = NULL;
3969d579b04aSYu-Ting Tseng struct binder_work *w;
3970d579b04aSYu-Ting Tseng
3971d579b04aSYu-Ting Tseng binder_inner_proc_lock(proc);
3972d579b04aSYu-Ting Tseng list_for_each_entry(w, &proc->delivered_freeze, entry) {
3973d579b04aSYu-Ting Tseng struct binder_ref_freeze *tmp_freeze =
3974d579b04aSYu-Ting Tseng container_of(w, struct binder_ref_freeze, work);
3975d579b04aSYu-Ting Tseng
3976d579b04aSYu-Ting Tseng if (tmp_freeze->cookie == cookie) {
3977d579b04aSYu-Ting Tseng freeze = tmp_freeze;
3978d579b04aSYu-Ting Tseng break;
3979d579b04aSYu-Ting Tseng }
3980d579b04aSYu-Ting Tseng }
3981d579b04aSYu-Ting Tseng if (!freeze) {
3982d579b04aSYu-Ting Tseng binder_user_error("%d:%d BC_FREEZE_NOTIFICATION_DONE %016llx not found\n",
3983d579b04aSYu-Ting Tseng proc->pid, thread->pid, (u64)cookie);
3984d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
3985d579b04aSYu-Ting Tseng return -EINVAL;
3986d579b04aSYu-Ting Tseng }
3987d579b04aSYu-Ting Tseng binder_dequeue_work_ilocked(&freeze->work);
3988d579b04aSYu-Ting Tseng freeze->sent = false;
3989d579b04aSYu-Ting Tseng if (freeze->resend) {
3990d579b04aSYu-Ting Tseng freeze->resend = false;
3991d579b04aSYu-Ting Tseng binder_enqueue_work_ilocked(&freeze->work, &proc->todo);
3992d579b04aSYu-Ting Tseng binder_wakeup_proc_ilocked(proc);
3993d579b04aSYu-Ting Tseng }
3994d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
3995d579b04aSYu-Ting Tseng return 0;
3996d579b04aSYu-Ting Tseng }
3997d579b04aSYu-Ting Tseng
399844d8047fSTodd Kjos /**
399944d8047fSTodd Kjos * binder_free_buf() - free the specified buffer
400044d8047fSTodd Kjos * @proc: binder proc that owns buffer
400144d8047fSTodd Kjos * @buffer: buffer to be freed
400232e9f56aSTodd Kjos * @is_failure: failed to send transaction
400344d8047fSTodd Kjos *
400444d8047fSTodd Kjos * If buffer for an async transaction, enqueue the next async
400544d8047fSTodd Kjos * transaction from the node.
400644d8047fSTodd Kjos *
400744d8047fSTodd Kjos * Cleanup buffer and free it.
400844d8047fSTodd Kjos */
4009f4608ce9SWei Yongjun static void
binder_free_buf(struct binder_proc * proc,struct binder_thread * thread,struct binder_buffer * buffer,bool is_failure)40105fdb55c1STodd Kjos binder_free_buf(struct binder_proc *proc,
40115fdb55c1STodd Kjos struct binder_thread *thread,
401232e9f56aSTodd Kjos struct binder_buffer *buffer, bool is_failure)
401344d8047fSTodd Kjos {
4014a370003cSTodd Kjos binder_inner_proc_lock(proc);
401544d8047fSTodd Kjos if (buffer->transaction) {
401644d8047fSTodd Kjos buffer->transaction->buffer = NULL;
401744d8047fSTodd Kjos buffer->transaction = NULL;
401844d8047fSTodd Kjos }
4019a370003cSTodd Kjos binder_inner_proc_unlock(proc);
402044d8047fSTodd Kjos if (buffer->async_transaction && buffer->target_node) {
402144d8047fSTodd Kjos struct binder_node *buf_node;
402244d8047fSTodd Kjos struct binder_work *w;
402344d8047fSTodd Kjos
402444d8047fSTodd Kjos buf_node = buffer->target_node;
402544d8047fSTodd Kjos binder_node_inner_lock(buf_node);
402644d8047fSTodd Kjos BUG_ON(!buf_node->has_async_transaction);
402744d8047fSTodd Kjos BUG_ON(buf_node->proc != proc);
402844d8047fSTodd Kjos w = binder_dequeue_work_head_ilocked(
402944d8047fSTodd Kjos &buf_node->async_todo);
403044d8047fSTodd Kjos if (!w) {
403144d8047fSTodd Kjos buf_node->has_async_transaction = false;
403244d8047fSTodd Kjos } else {
403344d8047fSTodd Kjos binder_enqueue_work_ilocked(
403444d8047fSTodd Kjos w, &proc->todo);
403544d8047fSTodd Kjos binder_wakeup_proc_ilocked(proc);
403644d8047fSTodd Kjos }
403744d8047fSTodd Kjos binder_node_inner_unlock(buf_node);
403844d8047fSTodd Kjos }
403944d8047fSTodd Kjos trace_binder_transaction_buffer_release(buffer);
4040bdc1c5faSCarlos Llamas binder_release_entire_buffer(proc, thread, buffer, is_failure);
404144d8047fSTodd Kjos binder_alloc_free_buf(&proc->alloc, buffer);
404244d8047fSTodd Kjos }
404344d8047fSTodd Kjos
binder_thread_write(struct binder_proc * proc,struct binder_thread * thread,binder_uintptr_t binder_buffer,size_t size,binder_size_t * consumed)4044777783e0SGreg Kroah-Hartman static int binder_thread_write(struct binder_proc *proc,
4045777783e0SGreg Kroah-Hartman struct binder_thread *thread,
4046777783e0SGreg Kroah-Hartman binder_uintptr_t binder_buffer, size_t size,
4047777783e0SGreg Kroah-Hartman binder_size_t *consumed)
4048777783e0SGreg Kroah-Hartman {
4049777783e0SGreg Kroah-Hartman uint32_t cmd;
4050342e5c90SMartijn Coenen struct binder_context *context = proc->context;
4051777783e0SGreg Kroah-Hartman void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4052777783e0SGreg Kroah-Hartman void __user *ptr = buffer + *consumed;
4053777783e0SGreg Kroah-Hartman void __user *end = buffer + size;
4054777783e0SGreg Kroah-Hartman
405526549d17STodd Kjos while (ptr < end && thread->return_error.cmd == BR_OK) {
4056372e3147STodd Kjos int ret;
4057372e3147STodd Kjos
4058777783e0SGreg Kroah-Hartman if (get_user(cmd, (uint32_t __user *)ptr))
4059777783e0SGreg Kroah-Hartman return -EFAULT;
4060777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4061777783e0SGreg Kroah-Hartman trace_binder_command(cmd);
4062777783e0SGreg Kroah-Hartman if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
40630953c797SBadhri Jagan Sridharan atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
40640953c797SBadhri Jagan Sridharan atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
40650953c797SBadhri Jagan Sridharan atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
4066777783e0SGreg Kroah-Hartman }
4067777783e0SGreg Kroah-Hartman switch (cmd) {
4068777783e0SGreg Kroah-Hartman case BC_INCREFS:
4069777783e0SGreg Kroah-Hartman case BC_ACQUIRE:
4070777783e0SGreg Kroah-Hartman case BC_RELEASE:
4071777783e0SGreg Kroah-Hartman case BC_DECREFS: {
4072777783e0SGreg Kroah-Hartman uint32_t target;
4073777783e0SGreg Kroah-Hartman const char *debug_string;
4074372e3147STodd Kjos bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
4075372e3147STodd Kjos bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
4076372e3147STodd Kjos struct binder_ref_data rdata;
4077777783e0SGreg Kroah-Hartman
4078777783e0SGreg Kroah-Hartman if (get_user(target, (uint32_t __user *)ptr))
4079777783e0SGreg Kroah-Hartman return -EFAULT;
4080c44b1231STodd Kjos
4081777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4082372e3147STodd Kjos ret = -1;
4083372e3147STodd Kjos if (increment && !target) {
4084c44b1231STodd Kjos struct binder_node *ctx_mgr_node;
40856c20032cSAndrew Bridges
4086c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
4087c44b1231STodd Kjos ctx_mgr_node = context->binder_context_mgr_node;
40884b836a14SJann Horn if (ctx_mgr_node) {
40894b836a14SJann Horn if (ctx_mgr_node->proc == proc) {
40904b836a14SJann Horn binder_user_error("%d:%d context manager tried to acquire desc 0\n",
40914b836a14SJann Horn proc->pid, thread->pid);
40924b836a14SJann Horn mutex_unlock(&context->context_mgr_node_lock);
40934b836a14SJann Horn return -EINVAL;
40944b836a14SJann Horn }
4095372e3147STodd Kjos ret = binder_inc_ref_for_node(
4096372e3147STodd Kjos proc, ctx_mgr_node,
4097372e3147STodd Kjos strong, NULL, &rdata);
40984b836a14SJann Horn }
4099c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
4100c44b1231STodd Kjos }
4101372e3147STodd Kjos if (ret)
4102372e3147STodd Kjos ret = binder_update_ref_for_handle(
4103372e3147STodd Kjos proc, target, increment, strong,
4104372e3147STodd Kjos &rdata);
4105372e3147STodd Kjos if (!ret && rdata.desc != target) {
4106372e3147STodd Kjos binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
4107372e3147STodd Kjos proc->pid, thread->pid,
4108372e3147STodd Kjos target, rdata.desc);
4109777783e0SGreg Kroah-Hartman }
4110777783e0SGreg Kroah-Hartman switch (cmd) {
4111777783e0SGreg Kroah-Hartman case BC_INCREFS:
4112777783e0SGreg Kroah-Hartman debug_string = "IncRefs";
4113777783e0SGreg Kroah-Hartman break;
4114777783e0SGreg Kroah-Hartman case BC_ACQUIRE:
4115777783e0SGreg Kroah-Hartman debug_string = "Acquire";
4116777783e0SGreg Kroah-Hartman break;
4117777783e0SGreg Kroah-Hartman case BC_RELEASE:
4118777783e0SGreg Kroah-Hartman debug_string = "Release";
4119777783e0SGreg Kroah-Hartman break;
4120777783e0SGreg Kroah-Hartman case BC_DECREFS:
4121777783e0SGreg Kroah-Hartman default:
4122777783e0SGreg Kroah-Hartman debug_string = "DecRefs";
4123372e3147STodd Kjos break;
4124372e3147STodd Kjos }
4125372e3147STodd Kjos if (ret) {
4126372e3147STodd Kjos binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
4127372e3147STodd Kjos proc->pid, thread->pid, debug_string,
4128372e3147STodd Kjos strong, target, ret);
4129777783e0SGreg Kroah-Hartman break;
4130777783e0SGreg Kroah-Hartman }
4131777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_USER_REFS,
4132372e3147STodd Kjos "%d:%d %s ref %d desc %d s %d w %d\n",
4133372e3147STodd Kjos proc->pid, thread->pid, debug_string,
4134372e3147STodd Kjos rdata.debug_id, rdata.desc, rdata.strong,
4135372e3147STodd Kjos rdata.weak);
4136777783e0SGreg Kroah-Hartman break;
4137777783e0SGreg Kroah-Hartman }
4138777783e0SGreg Kroah-Hartman case BC_INCREFS_DONE:
4139777783e0SGreg Kroah-Hartman case BC_ACQUIRE_DONE: {
4140777783e0SGreg Kroah-Hartman binder_uintptr_t node_ptr;
4141777783e0SGreg Kroah-Hartman binder_uintptr_t cookie;
4142777783e0SGreg Kroah-Hartman struct binder_node *node;
4143673068eeSTodd Kjos bool free_node;
4144777783e0SGreg Kroah-Hartman
4145777783e0SGreg Kroah-Hartman if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
4146777783e0SGreg Kroah-Hartman return -EFAULT;
4147777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
4148777783e0SGreg Kroah-Hartman if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4149777783e0SGreg Kroah-Hartman return -EFAULT;
4150777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
4151777783e0SGreg Kroah-Hartman node = binder_get_node(proc, node_ptr);
4152777783e0SGreg Kroah-Hartman if (node == NULL) {
4153777783e0SGreg Kroah-Hartman binder_user_error("%d:%d %s u%016llx no match\n",
4154777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4155777783e0SGreg Kroah-Hartman cmd == BC_INCREFS_DONE ?
4156777783e0SGreg Kroah-Hartman "BC_INCREFS_DONE" :
4157777783e0SGreg Kroah-Hartman "BC_ACQUIRE_DONE",
4158777783e0SGreg Kroah-Hartman (u64)node_ptr);
4159777783e0SGreg Kroah-Hartman break;
4160777783e0SGreg Kroah-Hartman }
4161777783e0SGreg Kroah-Hartman if (cookie != node->cookie) {
4162777783e0SGreg Kroah-Hartman binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
4163777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4164777783e0SGreg Kroah-Hartman cmd == BC_INCREFS_DONE ?
4165777783e0SGreg Kroah-Hartman "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
4166777783e0SGreg Kroah-Hartman (u64)node_ptr, node->debug_id,
4167777783e0SGreg Kroah-Hartman (u64)cookie, (u64)node->cookie);
4168adc18842STodd Kjos binder_put_node(node);
4169777783e0SGreg Kroah-Hartman break;
4170777783e0SGreg Kroah-Hartman }
4171673068eeSTodd Kjos binder_node_inner_lock(node);
4172777783e0SGreg Kroah-Hartman if (cmd == BC_ACQUIRE_DONE) {
4173777783e0SGreg Kroah-Hartman if (node->pending_strong_ref == 0) {
4174777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
4175777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4176777783e0SGreg Kroah-Hartman node->debug_id);
4177673068eeSTodd Kjos binder_node_inner_unlock(node);
4178adc18842STodd Kjos binder_put_node(node);
4179777783e0SGreg Kroah-Hartman break;
4180777783e0SGreg Kroah-Hartman }
4181777783e0SGreg Kroah-Hartman node->pending_strong_ref = 0;
4182777783e0SGreg Kroah-Hartman } else {
4183777783e0SGreg Kroah-Hartman if (node->pending_weak_ref == 0) {
4184777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
4185777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4186777783e0SGreg Kroah-Hartman node->debug_id);
4187673068eeSTodd Kjos binder_node_inner_unlock(node);
4188adc18842STodd Kjos binder_put_node(node);
4189777783e0SGreg Kroah-Hartman break;
4190777783e0SGreg Kroah-Hartman }
4191777783e0SGreg Kroah-Hartman node->pending_weak_ref = 0;
4192777783e0SGreg Kroah-Hartman }
4193673068eeSTodd Kjos free_node = binder_dec_node_nilocked(node,
4194673068eeSTodd Kjos cmd == BC_ACQUIRE_DONE, 0);
4195673068eeSTodd Kjos WARN_ON(free_node);
4196777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_USER_REFS,
4197adc18842STodd Kjos "%d:%d %s node %d ls %d lw %d tr %d\n",
4198777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4199777783e0SGreg Kroah-Hartman cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
4200adc18842STodd Kjos node->debug_id, node->local_strong_refs,
4201adc18842STodd Kjos node->local_weak_refs, node->tmp_refs);
4202673068eeSTodd Kjos binder_node_inner_unlock(node);
4203adc18842STodd Kjos binder_put_node(node);
4204777783e0SGreg Kroah-Hartman break;
4205777783e0SGreg Kroah-Hartman }
4206777783e0SGreg Kroah-Hartman case BC_ATTEMPT_ACQUIRE:
4207777783e0SGreg Kroah-Hartman pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
4208777783e0SGreg Kroah-Hartman return -EINVAL;
4209777783e0SGreg Kroah-Hartman case BC_ACQUIRE_RESULT:
4210777783e0SGreg Kroah-Hartman pr_err("BC_ACQUIRE_RESULT not supported\n");
4211777783e0SGreg Kroah-Hartman return -EINVAL;
4212777783e0SGreg Kroah-Hartman
4213777783e0SGreg Kroah-Hartman case BC_FREE_BUFFER: {
4214777783e0SGreg Kroah-Hartman binder_uintptr_t data_ptr;
4215777783e0SGreg Kroah-Hartman struct binder_buffer *buffer;
4216777783e0SGreg Kroah-Hartman
4217777783e0SGreg Kroah-Hartman if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
4218777783e0SGreg Kroah-Hartman return -EFAULT;
4219777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
4220777783e0SGreg Kroah-Hartman
422153d311cfSTodd Kjos buffer = binder_alloc_prepare_to_free(&proc->alloc,
422219c98724STodd Kjos data_ptr);
42237bada55aSTodd Kjos if (IS_ERR_OR_NULL(buffer)) {
42247bada55aSTodd Kjos if (PTR_ERR(buffer) == -EPERM) {
42257bada55aSTodd Kjos binder_user_error(
42267bada55aSTodd Kjos "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
42277bada55aSTodd Kjos proc->pid, thread->pid,
42287bada55aSTodd Kjos (u64)data_ptr);
42297bada55aSTodd Kjos } else {
42307bada55aSTodd Kjos binder_user_error(
42317bada55aSTodd Kjos "%d:%d BC_FREE_BUFFER u%016llx no match\n",
42327bada55aSTodd Kjos proc->pid, thread->pid,
42337bada55aSTodd Kjos (u64)data_ptr);
4234777783e0SGreg Kroah-Hartman }
4235777783e0SGreg Kroah-Hartman break;
4236777783e0SGreg Kroah-Hartman }
4237777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_FREE_BUFFER,
4238777783e0SGreg Kroah-Hartman "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
4239777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)data_ptr,
4240777783e0SGreg Kroah-Hartman buffer->debug_id,
4241777783e0SGreg Kroah-Hartman buffer->transaction ? "active" : "finished");
424232e9f56aSTodd Kjos binder_free_buf(proc, thread, buffer, false);
4243777783e0SGreg Kroah-Hartman break;
4244777783e0SGreg Kroah-Hartman }
4245777783e0SGreg Kroah-Hartman
42467980240bSMartijn Coenen case BC_TRANSACTION_SG:
42477980240bSMartijn Coenen case BC_REPLY_SG: {
42487980240bSMartijn Coenen struct binder_transaction_data_sg tr;
42497980240bSMartijn Coenen
42507980240bSMartijn Coenen if (copy_from_user(&tr, ptr, sizeof(tr)))
42517980240bSMartijn Coenen return -EFAULT;
42527980240bSMartijn Coenen ptr += sizeof(tr);
42537980240bSMartijn Coenen binder_transaction(proc, thread, &tr.transaction_data,
42547980240bSMartijn Coenen cmd == BC_REPLY_SG, tr.buffers_size);
42557980240bSMartijn Coenen break;
42567980240bSMartijn Coenen }
4257777783e0SGreg Kroah-Hartman case BC_TRANSACTION:
4258777783e0SGreg Kroah-Hartman case BC_REPLY: {
4259777783e0SGreg Kroah-Hartman struct binder_transaction_data tr;
4260777783e0SGreg Kroah-Hartman
4261777783e0SGreg Kroah-Hartman if (copy_from_user(&tr, ptr, sizeof(tr)))
4262777783e0SGreg Kroah-Hartman return -EFAULT;
4263777783e0SGreg Kroah-Hartman ptr += sizeof(tr);
42644bfac80aSMartijn Coenen binder_transaction(proc, thread, &tr,
42654bfac80aSMartijn Coenen cmd == BC_REPLY, 0);
4266777783e0SGreg Kroah-Hartman break;
4267777783e0SGreg Kroah-Hartman }
4268777783e0SGreg Kroah-Hartman
4269777783e0SGreg Kroah-Hartman case BC_REGISTER_LOOPER:
4270777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4271777783e0SGreg Kroah-Hartman "%d:%d BC_REGISTER_LOOPER\n",
4272777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4273b3e68612STodd Kjos binder_inner_proc_lock(proc);
4274777783e0SGreg Kroah-Hartman if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
4275777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_INVALID;
4276777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
4277777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4278777783e0SGreg Kroah-Hartman } else if (proc->requested_threads == 0) {
4279777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_INVALID;
4280777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
4281777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4282777783e0SGreg Kroah-Hartman } else {
4283777783e0SGreg Kroah-Hartman proc->requested_threads--;
4284777783e0SGreg Kroah-Hartman proc->requested_threads_started++;
4285777783e0SGreg Kroah-Hartman }
4286777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
4287b3e68612STodd Kjos binder_inner_proc_unlock(proc);
4288777783e0SGreg Kroah-Hartman break;
4289777783e0SGreg Kroah-Hartman case BC_ENTER_LOOPER:
4290777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4291777783e0SGreg Kroah-Hartman "%d:%d BC_ENTER_LOOPER\n",
4292777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4293777783e0SGreg Kroah-Hartman if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
4294777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_INVALID;
4295777783e0SGreg Kroah-Hartman binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
4296777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4297777783e0SGreg Kroah-Hartman }
4298777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_ENTERED;
4299777783e0SGreg Kroah-Hartman break;
4300777783e0SGreg Kroah-Hartman case BC_EXIT_LOOPER:
4301777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
4302777783e0SGreg Kroah-Hartman "%d:%d BC_EXIT_LOOPER\n",
4303777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4304777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_EXITED;
4305777783e0SGreg Kroah-Hartman break;
4306777783e0SGreg Kroah-Hartman
4307777783e0SGreg Kroah-Hartman case BC_REQUEST_DEATH_NOTIFICATION:
4308777783e0SGreg Kroah-Hartman case BC_CLEAR_DEATH_NOTIFICATION: {
4309777783e0SGreg Kroah-Hartman uint32_t target;
4310777783e0SGreg Kroah-Hartman binder_uintptr_t cookie;
4311777783e0SGreg Kroah-Hartman struct binder_ref *ref;
43122c1838dcSTodd Kjos struct binder_ref_death *death = NULL;
4313777783e0SGreg Kroah-Hartman
4314777783e0SGreg Kroah-Hartman if (get_user(target, (uint32_t __user *)ptr))
4315777783e0SGreg Kroah-Hartman return -EFAULT;
4316777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4317777783e0SGreg Kroah-Hartman if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4318777783e0SGreg Kroah-Hartman return -EFAULT;
4319777783e0SGreg Kroah-Hartman ptr += sizeof(binder_uintptr_t);
43202c1838dcSTodd Kjos if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
43212c1838dcSTodd Kjos /*
43222c1838dcSTodd Kjos * Allocate memory for death notification
43232c1838dcSTodd Kjos * before taking lock
43242c1838dcSTodd Kjos */
43252c1838dcSTodd Kjos death = kzalloc(sizeof(*death), GFP_KERNEL);
43262c1838dcSTodd Kjos if (death == NULL) {
43272c1838dcSTodd Kjos WARN_ON(thread->return_error.cmd !=
43282c1838dcSTodd Kjos BR_OK);
43292c1838dcSTodd Kjos thread->return_error.cmd = BR_ERROR;
4330148ade2cSMartijn Coenen binder_enqueue_thread_work(
4331148ade2cSMartijn Coenen thread,
4332148ade2cSMartijn Coenen &thread->return_error.work);
43332c1838dcSTodd Kjos binder_debug(
43342c1838dcSTodd Kjos BINDER_DEBUG_FAILED_TRANSACTION,
43352c1838dcSTodd Kjos "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
43362c1838dcSTodd Kjos proc->pid, thread->pid);
43372c1838dcSTodd Kjos break;
43382c1838dcSTodd Kjos }
43392c1838dcSTodd Kjos }
43402c1838dcSTodd Kjos binder_proc_lock(proc);
43412c1838dcSTodd Kjos ref = binder_get_ref_olocked(proc, target, false);
4342777783e0SGreg Kroah-Hartman if (ref == NULL) {
4343777783e0SGreg Kroah-Hartman binder_user_error("%d:%d %s invalid ref %d\n",
4344777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4345777783e0SGreg Kroah-Hartman cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4346777783e0SGreg Kroah-Hartman "BC_REQUEST_DEATH_NOTIFICATION" :
4347777783e0SGreg Kroah-Hartman "BC_CLEAR_DEATH_NOTIFICATION",
4348777783e0SGreg Kroah-Hartman target);
43492c1838dcSTodd Kjos binder_proc_unlock(proc);
43502c1838dcSTodd Kjos kfree(death);
4351777783e0SGreg Kroah-Hartman break;
4352777783e0SGreg Kroah-Hartman }
4353777783e0SGreg Kroah-Hartman
4354777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4355777783e0SGreg Kroah-Hartman "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
4356777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4357777783e0SGreg Kroah-Hartman cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4358777783e0SGreg Kroah-Hartman "BC_REQUEST_DEATH_NOTIFICATION" :
4359777783e0SGreg Kroah-Hartman "BC_CLEAR_DEATH_NOTIFICATION",
4360372e3147STodd Kjos (u64)cookie, ref->data.debug_id,
4361372e3147STodd Kjos ref->data.desc, ref->data.strong,
4362372e3147STodd Kjos ref->data.weak, ref->node->debug_id);
4363777783e0SGreg Kroah-Hartman
4364ab51ec6bSMartijn Coenen binder_node_lock(ref->node);
4365777783e0SGreg Kroah-Hartman if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
4366777783e0SGreg Kroah-Hartman if (ref->death) {
4367777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
4368777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4369ab51ec6bSMartijn Coenen binder_node_unlock(ref->node);
43702c1838dcSTodd Kjos binder_proc_unlock(proc);
43712c1838dcSTodd Kjos kfree(death);
4372777783e0SGreg Kroah-Hartman break;
4373777783e0SGreg Kroah-Hartman }
4374777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_DEATH);
4375777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&death->work.entry);
4376777783e0SGreg Kroah-Hartman death->cookie = cookie;
4377777783e0SGreg Kroah-Hartman ref->death = death;
4378777783e0SGreg Kroah-Hartman if (ref->node->proc == NULL) {
4379777783e0SGreg Kroah-Hartman ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4380bb74562aSMartijn Coenen
43811b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
43821b77e9dcSMartijn Coenen binder_enqueue_work_ilocked(
4383bb74562aSMartijn Coenen &ref->death->work, &proc->todo);
4384bb74562aSMartijn Coenen binder_wakeup_proc_ilocked(proc);
43851b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
4386777783e0SGreg Kroah-Hartman }
4387777783e0SGreg Kroah-Hartman } else {
4388777783e0SGreg Kroah-Hartman if (ref->death == NULL) {
4389777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
4390777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4391673068eeSTodd Kjos binder_node_unlock(ref->node);
43922c1838dcSTodd Kjos binder_proc_unlock(proc);
4393777783e0SGreg Kroah-Hartman break;
4394777783e0SGreg Kroah-Hartman }
4395777783e0SGreg Kroah-Hartman death = ref->death;
4396777783e0SGreg Kroah-Hartman if (death->cookie != cookie) {
4397777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
4398777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4399777783e0SGreg Kroah-Hartman (u64)death->cookie,
4400777783e0SGreg Kroah-Hartman (u64)cookie);
4401673068eeSTodd Kjos binder_node_unlock(ref->node);
44022c1838dcSTodd Kjos binder_proc_unlock(proc);
4403777783e0SGreg Kroah-Hartman break;
4404777783e0SGreg Kroah-Hartman }
4405777783e0SGreg Kroah-Hartman ref->death = NULL;
440672196393STodd Kjos binder_inner_proc_lock(proc);
4407777783e0SGreg Kroah-Hartman if (list_empty(&death->work.entry)) {
4408777783e0SGreg Kroah-Hartman death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
440972196393STodd Kjos if (thread->looper &
441072196393STodd Kjos (BINDER_LOOPER_STATE_REGISTERED |
441172196393STodd Kjos BINDER_LOOPER_STATE_ENTERED))
4412148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(
4413148ade2cSMartijn Coenen thread,
4414148ade2cSMartijn Coenen &death->work);
441572196393STodd Kjos else {
441672196393STodd Kjos binder_enqueue_work_ilocked(
441772196393STodd Kjos &death->work,
441872196393STodd Kjos &proc->todo);
44191b77e9dcSMartijn Coenen binder_wakeup_proc_ilocked(
4420408c68b1SMartijn Coenen proc);
4421777783e0SGreg Kroah-Hartman }
4422777783e0SGreg Kroah-Hartman } else {
4423777783e0SGreg Kroah-Hartman BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4424777783e0SGreg Kroah-Hartman death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4425777783e0SGreg Kroah-Hartman }
442672196393STodd Kjos binder_inner_proc_unlock(proc);
4427777783e0SGreg Kroah-Hartman }
4428ab51ec6bSMartijn Coenen binder_node_unlock(ref->node);
44292c1838dcSTodd Kjos binder_proc_unlock(proc);
4430777783e0SGreg Kroah-Hartman } break;
4431777783e0SGreg Kroah-Hartman case BC_DEAD_BINDER_DONE: {
4432777783e0SGreg Kroah-Hartman struct binder_work *w;
4433777783e0SGreg Kroah-Hartman binder_uintptr_t cookie;
4434777783e0SGreg Kroah-Hartman struct binder_ref_death *death = NULL;
4435777783e0SGreg Kroah-Hartman
4436777783e0SGreg Kroah-Hartman if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4437777783e0SGreg Kroah-Hartman return -EFAULT;
4438777783e0SGreg Kroah-Hartman
44397a64cd88SLisa Du ptr += sizeof(cookie);
444072196393STodd Kjos binder_inner_proc_lock(proc);
444172196393STodd Kjos list_for_each_entry(w, &proc->delivered_death,
444272196393STodd Kjos entry) {
444372196393STodd Kjos struct binder_ref_death *tmp_death =
444472196393STodd Kjos container_of(w,
444572196393STodd Kjos struct binder_ref_death,
444672196393STodd Kjos work);
4447777783e0SGreg Kroah-Hartman
4448777783e0SGreg Kroah-Hartman if (tmp_death->cookie == cookie) {
4449777783e0SGreg Kroah-Hartman death = tmp_death;
4450777783e0SGreg Kroah-Hartman break;
4451777783e0SGreg Kroah-Hartman }
4452777783e0SGreg Kroah-Hartman }
4453777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
44548ca86f16STodd Kjos "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
4455777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)cookie,
4456777783e0SGreg Kroah-Hartman death);
4457777783e0SGreg Kroah-Hartman if (death == NULL) {
4458777783e0SGreg Kroah-Hartman binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4459777783e0SGreg Kroah-Hartman proc->pid, thread->pid, (u64)cookie);
446072196393STodd Kjos binder_inner_proc_unlock(proc);
4461777783e0SGreg Kroah-Hartman break;
4462777783e0SGreg Kroah-Hartman }
446372196393STodd Kjos binder_dequeue_work_ilocked(&death->work);
4464777783e0SGreg Kroah-Hartman if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4465777783e0SGreg Kroah-Hartman death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
446672196393STodd Kjos if (thread->looper &
446772196393STodd Kjos (BINDER_LOOPER_STATE_REGISTERED |
446872196393STodd Kjos BINDER_LOOPER_STATE_ENTERED))
4469148ade2cSMartijn Coenen binder_enqueue_thread_work_ilocked(
4470148ade2cSMartijn Coenen thread, &death->work);
447172196393STodd Kjos else {
447272196393STodd Kjos binder_enqueue_work_ilocked(
447372196393STodd Kjos &death->work,
447472196393STodd Kjos &proc->todo);
4475408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(proc);
4476777783e0SGreg Kroah-Hartman }
4477777783e0SGreg Kroah-Hartman }
447872196393STodd Kjos binder_inner_proc_unlock(proc);
4479777783e0SGreg Kroah-Hartman } break;
4480777783e0SGreg Kroah-Hartman
4481d579b04aSYu-Ting Tseng case BC_REQUEST_FREEZE_NOTIFICATION: {
4482d579b04aSYu-Ting Tseng struct binder_handle_cookie handle_cookie;
4483d579b04aSYu-Ting Tseng int error;
4484d579b04aSYu-Ting Tseng
4485d579b04aSYu-Ting Tseng if (copy_from_user(&handle_cookie, ptr, sizeof(handle_cookie)))
4486d579b04aSYu-Ting Tseng return -EFAULT;
4487d579b04aSYu-Ting Tseng ptr += sizeof(handle_cookie);
4488d579b04aSYu-Ting Tseng error = binder_request_freeze_notification(proc, thread,
4489d579b04aSYu-Ting Tseng &handle_cookie);
4490d579b04aSYu-Ting Tseng if (error)
4491d579b04aSYu-Ting Tseng return error;
4492d579b04aSYu-Ting Tseng } break;
4493d579b04aSYu-Ting Tseng
4494d579b04aSYu-Ting Tseng case BC_CLEAR_FREEZE_NOTIFICATION: {
4495d579b04aSYu-Ting Tseng struct binder_handle_cookie handle_cookie;
4496d579b04aSYu-Ting Tseng int error;
4497d579b04aSYu-Ting Tseng
4498d579b04aSYu-Ting Tseng if (copy_from_user(&handle_cookie, ptr, sizeof(handle_cookie)))
4499d579b04aSYu-Ting Tseng return -EFAULT;
4500d579b04aSYu-Ting Tseng ptr += sizeof(handle_cookie);
4501d579b04aSYu-Ting Tseng error = binder_clear_freeze_notification(proc, thread, &handle_cookie);
4502d579b04aSYu-Ting Tseng if (error)
4503d579b04aSYu-Ting Tseng return error;
4504d579b04aSYu-Ting Tseng } break;
4505d579b04aSYu-Ting Tseng
4506d579b04aSYu-Ting Tseng case BC_FREEZE_NOTIFICATION_DONE: {
4507d579b04aSYu-Ting Tseng binder_uintptr_t cookie;
4508d579b04aSYu-Ting Tseng int error;
4509d579b04aSYu-Ting Tseng
4510d579b04aSYu-Ting Tseng if (get_user(cookie, (binder_uintptr_t __user *)ptr))
4511d579b04aSYu-Ting Tseng return -EFAULT;
4512d579b04aSYu-Ting Tseng
4513d579b04aSYu-Ting Tseng ptr += sizeof(cookie);
4514d579b04aSYu-Ting Tseng error = binder_freeze_notification_done(proc, thread, cookie);
4515d579b04aSYu-Ting Tseng if (error)
4516d579b04aSYu-Ting Tseng return error;
4517d579b04aSYu-Ting Tseng } break;
4518d579b04aSYu-Ting Tseng
4519777783e0SGreg Kroah-Hartman default:
4520da486496SCarlos Llamas pr_err("%d:%d unknown command %u\n",
4521777783e0SGreg Kroah-Hartman proc->pid, thread->pid, cmd);
4522777783e0SGreg Kroah-Hartman return -EINVAL;
4523777783e0SGreg Kroah-Hartman }
4524777783e0SGreg Kroah-Hartman *consumed = ptr - buffer;
4525777783e0SGreg Kroah-Hartman }
4526777783e0SGreg Kroah-Hartman return 0;
4527777783e0SGreg Kroah-Hartman }
4528777783e0SGreg Kroah-Hartman
binder_stat_br(struct binder_proc * proc,struct binder_thread * thread,uint32_t cmd)4529777783e0SGreg Kroah-Hartman static void binder_stat_br(struct binder_proc *proc,
4530777783e0SGreg Kroah-Hartman struct binder_thread *thread, uint32_t cmd)
4531777783e0SGreg Kroah-Hartman {
4532777783e0SGreg Kroah-Hartman trace_binder_return(cmd);
4533777783e0SGreg Kroah-Hartman if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
45340953c797SBadhri Jagan Sridharan atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
45350953c797SBadhri Jagan Sridharan atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
45360953c797SBadhri Jagan Sridharan atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
4537777783e0SGreg Kroah-Hartman }
4538777783e0SGreg Kroah-Hartman }
4539777783e0SGreg 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)454026b47d8aSTodd Kjos static int binder_put_node_cmd(struct binder_proc *proc,
454126b47d8aSTodd Kjos struct binder_thread *thread,
454226b47d8aSTodd Kjos void __user **ptrp,
454326b47d8aSTodd Kjos binder_uintptr_t node_ptr,
454426b47d8aSTodd Kjos binder_uintptr_t node_cookie,
454526b47d8aSTodd Kjos int node_debug_id,
454626b47d8aSTodd Kjos uint32_t cmd, const char *cmd_name)
454726b47d8aSTodd Kjos {
454826b47d8aSTodd Kjos void __user *ptr = *ptrp;
454926b47d8aSTodd Kjos
455026b47d8aSTodd Kjos if (put_user(cmd, (uint32_t __user *)ptr))
455126b47d8aSTodd Kjos return -EFAULT;
455226b47d8aSTodd Kjos ptr += sizeof(uint32_t);
455326b47d8aSTodd Kjos
455426b47d8aSTodd Kjos if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
455526b47d8aSTodd Kjos return -EFAULT;
455626b47d8aSTodd Kjos ptr += sizeof(binder_uintptr_t);
455726b47d8aSTodd Kjos
455826b47d8aSTodd Kjos if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
455926b47d8aSTodd Kjos return -EFAULT;
456026b47d8aSTodd Kjos ptr += sizeof(binder_uintptr_t);
456126b47d8aSTodd Kjos
456226b47d8aSTodd Kjos binder_stat_br(proc, thread, cmd);
456326b47d8aSTodd Kjos binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
456426b47d8aSTodd Kjos proc->pid, thread->pid, cmd_name, node_debug_id,
456526b47d8aSTodd Kjos (u64)node_ptr, (u64)node_cookie);
456626b47d8aSTodd Kjos
456726b47d8aSTodd Kjos *ptrp = ptr;
456826b47d8aSTodd Kjos return 0;
456926b47d8aSTodd Kjos }
457026b47d8aSTodd Kjos
binder_wait_for_work(struct binder_thread * thread,bool do_proc_work)45711b77e9dcSMartijn Coenen static int binder_wait_for_work(struct binder_thread *thread,
45721b77e9dcSMartijn Coenen bool do_proc_work)
45731b77e9dcSMartijn Coenen {
45741b77e9dcSMartijn Coenen DEFINE_WAIT(wait);
45751b77e9dcSMartijn Coenen struct binder_proc *proc = thread->proc;
45761b77e9dcSMartijn Coenen int ret = 0;
45771b77e9dcSMartijn Coenen
45781b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
45791b77e9dcSMartijn Coenen for (;;) {
4580f5d39b02SPeter Zijlstra prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE);
45811b77e9dcSMartijn Coenen if (binder_has_work_ilocked(thread, do_proc_work))
45821b77e9dcSMartijn Coenen break;
45831b77e9dcSMartijn Coenen if (do_proc_work)
45841b77e9dcSMartijn Coenen list_add(&thread->waiting_thread_node,
45851b77e9dcSMartijn Coenen &proc->waiting_threads);
45861b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
45871b77e9dcSMartijn Coenen schedule();
45881b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
45891b77e9dcSMartijn Coenen list_del_init(&thread->waiting_thread_node);
45901b77e9dcSMartijn Coenen if (signal_pending(current)) {
459195c16f9dSMarco Ballesio ret = -EINTR;
45921b77e9dcSMartijn Coenen break;
45931b77e9dcSMartijn Coenen }
45941b77e9dcSMartijn Coenen }
45951b77e9dcSMartijn Coenen finish_wait(&thread->wait, &wait);
45961b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
45971b77e9dcSMartijn Coenen
45981b77e9dcSMartijn Coenen return ret;
45991b77e9dcSMartijn Coenen }
46001b77e9dcSMartijn Coenen
460144d8047fSTodd Kjos /**
460244d8047fSTodd Kjos * binder_apply_fd_fixups() - finish fd translation
46038ced0c62STodd Kjos * @proc: binder_proc associated @t->buffer
460444d8047fSTodd Kjos * @t: binder transaction with list of fd fixups
460544d8047fSTodd Kjos *
460644d8047fSTodd Kjos * Now that we are in the context of the transaction target
460744d8047fSTodd Kjos * process, we can allocate and install fds. Process the
460844d8047fSTodd Kjos * list of fds to translate and fixup the buffer with the
4609a8a570c6SCarlos Llamas * new fds first and only then install the files.
461044d8047fSTodd Kjos *
4611a8a570c6SCarlos Llamas * If we fail to allocate an fd, skip the install and release
461244d8047fSTodd Kjos * any fds that have already been allocated.
461344d8047fSTodd Kjos */
binder_apply_fd_fixups(struct binder_proc * proc,struct binder_transaction * t)46148ced0c62STodd Kjos static int binder_apply_fd_fixups(struct binder_proc *proc,
46158ced0c62STodd Kjos struct binder_transaction *t)
461644d8047fSTodd Kjos {
461744d8047fSTodd Kjos struct binder_txn_fd_fixup *fixup, *tmp;
461844d8047fSTodd Kjos int ret = 0;
461944d8047fSTodd Kjos
462044d8047fSTodd Kjos list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
462144d8047fSTodd Kjos int fd = get_unused_fd_flags(O_CLOEXEC);
462244d8047fSTodd Kjos
462344d8047fSTodd Kjos if (fd < 0) {
462444d8047fSTodd Kjos binder_debug(BINDER_DEBUG_TRANSACTION,
462544d8047fSTodd Kjos "failed fd fixup txn %d fd %d\n",
462644d8047fSTodd Kjos t->debug_id, fd);
462744d8047fSTodd Kjos ret = -ENOMEM;
4628a8a570c6SCarlos Llamas goto err;
462944d8047fSTodd Kjos }
463044d8047fSTodd Kjos binder_debug(BINDER_DEBUG_TRANSACTION,
463144d8047fSTodd Kjos "fd fixup txn %d fd %d\n",
463244d8047fSTodd Kjos t->debug_id, fd);
463344d8047fSTodd Kjos trace_binder_transaction_fd_recv(t, fd, fixup->offset);
4634a8a570c6SCarlos Llamas fixup->target_fd = fd;
4635bb4a2e48STodd Kjos if (binder_alloc_copy_to_buffer(&proc->alloc, t->buffer,
46368ced0c62STodd Kjos fixup->offset, &fd,
4637bb4a2e48STodd Kjos sizeof(u32))) {
4638bb4a2e48STodd Kjos ret = -EINVAL;
4639a8a570c6SCarlos Llamas goto err;
4640bb4a2e48STodd Kjos }
464144d8047fSTodd Kjos }
464244d8047fSTodd Kjos list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
4643a8a570c6SCarlos Llamas fd_install(fixup->target_fd, fixup->file);
464444d8047fSTodd Kjos list_del(&fixup->fixup_entry);
464544d8047fSTodd Kjos kfree(fixup);
464644d8047fSTodd Kjos }
464744d8047fSTodd Kjos
464844d8047fSTodd Kjos return ret;
4649a8a570c6SCarlos Llamas
4650a8a570c6SCarlos Llamas err:
4651a8a570c6SCarlos Llamas binder_free_txn_fixups(t);
4652a8a570c6SCarlos Llamas return ret;
465344d8047fSTodd Kjos }
465444d8047fSTodd 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)4655777783e0SGreg Kroah-Hartman static int binder_thread_read(struct binder_proc *proc,
4656777783e0SGreg Kroah-Hartman struct binder_thread *thread,
4657777783e0SGreg Kroah-Hartman binder_uintptr_t binder_buffer, size_t size,
4658777783e0SGreg Kroah-Hartman binder_size_t *consumed, int non_block)
4659777783e0SGreg Kroah-Hartman {
4660777783e0SGreg Kroah-Hartman void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
4661777783e0SGreg Kroah-Hartman void __user *ptr = buffer + *consumed;
4662777783e0SGreg Kroah-Hartman void __user *end = buffer + size;
4663777783e0SGreg Kroah-Hartman
4664777783e0SGreg Kroah-Hartman int ret = 0;
4665777783e0SGreg Kroah-Hartman int wait_for_proc_work;
4666777783e0SGreg Kroah-Hartman
4667777783e0SGreg Kroah-Hartman if (*consumed == 0) {
4668777783e0SGreg Kroah-Hartman if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4669777783e0SGreg Kroah-Hartman return -EFAULT;
4670777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4671777783e0SGreg Kroah-Hartman }
4672777783e0SGreg Kroah-Hartman
4673777783e0SGreg Kroah-Hartman retry:
46740b89d69aSMartijn Coenen binder_inner_proc_lock(proc);
46751b77e9dcSMartijn Coenen wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
46760b89d69aSMartijn Coenen binder_inner_proc_unlock(proc);
4677777783e0SGreg Kroah-Hartman
4678777783e0SGreg Kroah-Hartman thread->looper |= BINDER_LOOPER_STATE_WAITING;
4679777783e0SGreg Kroah-Hartman
4680777783e0SGreg Kroah-Hartman trace_binder_wait_for_work(wait_for_proc_work,
4681777783e0SGreg Kroah-Hartman !!thread->transaction_stack,
468272196393STodd Kjos !binder_worklist_empty(proc, &thread->todo));
4683777783e0SGreg Kroah-Hartman if (wait_for_proc_work) {
4684777783e0SGreg Kroah-Hartman if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4685777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_ENTERED))) {
4686777783e0SGreg 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",
4687777783e0SGreg Kroah-Hartman proc->pid, thread->pid, thread->looper);
4688777783e0SGreg Kroah-Hartman wait_event_interruptible(binder_user_error_wait,
4689777783e0SGreg Kroah-Hartman binder_stop_on_user_error < 2);
4690777783e0SGreg Kroah-Hartman }
4691777783e0SGreg Kroah-Hartman binder_set_nice(proc->default_priority);
4692777783e0SGreg Kroah-Hartman }
4693777783e0SGreg Kroah-Hartman
46941b77e9dcSMartijn Coenen if (non_block) {
46951b77e9dcSMartijn Coenen if (!binder_has_work(thread, wait_for_proc_work))
46961b77e9dcSMartijn Coenen ret = -EAGAIN;
46971b77e9dcSMartijn Coenen } else {
46981b77e9dcSMartijn Coenen ret = binder_wait_for_work(thread, wait_for_proc_work);
46991b77e9dcSMartijn Coenen }
47001b77e9dcSMartijn Coenen
4701777783e0SGreg Kroah-Hartman thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4702777783e0SGreg Kroah-Hartman
4703777783e0SGreg Kroah-Hartman if (ret)
4704777783e0SGreg Kroah-Hartman return ret;
4705777783e0SGreg Kroah-Hartman
4706777783e0SGreg Kroah-Hartman while (1) {
4707777783e0SGreg Kroah-Hartman uint32_t cmd;
4708ec74136dSTodd Kjos struct binder_transaction_data_secctx tr;
4709ec74136dSTodd Kjos struct binder_transaction_data *trd = &tr.transaction_data;
471072196393STodd Kjos struct binder_work *w = NULL;
471172196393STodd Kjos struct list_head *list = NULL;
4712777783e0SGreg Kroah-Hartman struct binder_transaction *t = NULL;
47137a4408c6STodd Kjos struct binder_thread *t_from;
4714ec74136dSTodd Kjos size_t trsize = sizeof(*trd);
4715777783e0SGreg Kroah-Hartman
4716ed29721eSTodd Kjos binder_inner_proc_lock(proc);
471772196393STodd Kjos if (!binder_worklist_empty_ilocked(&thread->todo))
471872196393STodd Kjos list = &thread->todo;
471972196393STodd Kjos else if (!binder_worklist_empty_ilocked(&proc->todo) &&
472072196393STodd Kjos wait_for_proc_work)
472172196393STodd Kjos list = &proc->todo;
472272196393STodd Kjos else {
472372196393STodd Kjos binder_inner_proc_unlock(proc);
472472196393STodd Kjos
4725777783e0SGreg Kroah-Hartman /* no data added */
472608dabceeSTodd Kjos if (ptr - buffer == 4 && !thread->looper_need_return)
4727777783e0SGreg Kroah-Hartman goto retry;
4728777783e0SGreg Kroah-Hartman break;
4729777783e0SGreg Kroah-Hartman }
4730777783e0SGreg Kroah-Hartman
4731ed29721eSTodd Kjos if (end - ptr < sizeof(tr) + 4) {
4732ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4733777783e0SGreg Kroah-Hartman break;
4734ed29721eSTodd Kjos }
473572196393STodd Kjos w = binder_dequeue_work_head_ilocked(list);
4736148ade2cSMartijn Coenen if (binder_worklist_empty_ilocked(&thread->todo))
4737148ade2cSMartijn Coenen thread->process_todo = false;
4738777783e0SGreg Kroah-Hartman
4739777783e0SGreg Kroah-Hartman switch (w->type) {
4740777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION: {
4741ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4742777783e0SGreg Kroah-Hartman t = container_of(w, struct binder_transaction, work);
4743777783e0SGreg Kroah-Hartman } break;
474426549d17STodd Kjos case BINDER_WORK_RETURN_ERROR: {
474526549d17STodd Kjos struct binder_error *e = container_of(
474626549d17STodd Kjos w, struct binder_error, work);
474726549d17STodd Kjos
474826549d17STodd Kjos WARN_ON(e->cmd == BR_OK);
4749ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
475026549d17STodd Kjos if (put_user(e->cmd, (uint32_t __user *)ptr))
475126549d17STodd Kjos return -EFAULT;
4752838d5565S宋金时 cmd = e->cmd;
475326549d17STodd Kjos e->cmd = BR_OK;
475426549d17STodd Kjos ptr += sizeof(uint32_t);
475526549d17STodd Kjos
4756838d5565S宋金时 binder_stat_br(proc, thread, cmd);
475726549d17STodd Kjos } break;
4758a7dc1e6fSHang Lu case BINDER_WORK_TRANSACTION_COMPLETE:
47590567461aSLi Li case BINDER_WORK_TRANSACTION_PENDING:
4760a7dc1e6fSHang Lu case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
4761a7dc1e6fSHang Lu if (proc->oneway_spam_detection_enabled &&
4762a7dc1e6fSHang Lu w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
4763a7dc1e6fSHang Lu cmd = BR_ONEWAY_SPAM_SUSPECT;
47640567461aSLi Li else if (w->type == BINDER_WORK_TRANSACTION_PENDING)
47650567461aSLi Li cmd = BR_TRANSACTION_PENDING_FROZEN;
4766a7dc1e6fSHang Lu else
4767777783e0SGreg Kroah-Hartman cmd = BR_TRANSACTION_COMPLETE;
4768a7dc1e6fSHang Lu binder_inner_proc_unlock(proc);
47691909a671STodd Kjos kfree(w);
47701909a671STodd Kjos binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4771777783e0SGreg Kroah-Hartman if (put_user(cmd, (uint32_t __user *)ptr))
4772777783e0SGreg Kroah-Hartman return -EFAULT;
4773777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
4774777783e0SGreg Kroah-Hartman
4775777783e0SGreg Kroah-Hartman binder_stat_br(proc, thread, cmd);
4776777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
4777777783e0SGreg Kroah-Hartman "%d:%d BR_TRANSACTION_COMPLETE\n",
4778777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
4779777783e0SGreg Kroah-Hartman } break;
4780777783e0SGreg Kroah-Hartman case BINDER_WORK_NODE: {
4781777783e0SGreg Kroah-Hartman struct binder_node *node = container_of(w, struct binder_node, work);
478226b47d8aSTodd Kjos int strong, weak;
478326b47d8aSTodd Kjos binder_uintptr_t node_ptr = node->ptr;
478426b47d8aSTodd Kjos binder_uintptr_t node_cookie = node->cookie;
478526b47d8aSTodd Kjos int node_debug_id = node->debug_id;
478626b47d8aSTodd Kjos int has_weak_ref;
478726b47d8aSTodd Kjos int has_strong_ref;
478826b47d8aSTodd Kjos void __user *orig_ptr = ptr;
4789777783e0SGreg Kroah-Hartman
479026b47d8aSTodd Kjos BUG_ON(proc != node->proc);
479126b47d8aSTodd Kjos strong = node->internal_strong_refs ||
479226b47d8aSTodd Kjos node->local_strong_refs;
479326b47d8aSTodd Kjos weak = !hlist_empty(&node->refs) ||
4794adc18842STodd Kjos node->local_weak_refs ||
4795adc18842STodd Kjos node->tmp_refs || strong;
479626b47d8aSTodd Kjos has_strong_ref = node->has_strong_ref;
479726b47d8aSTodd Kjos has_weak_ref = node->has_weak_ref;
479826b47d8aSTodd Kjos
479926b47d8aSTodd Kjos if (weak && !has_weak_ref) {
4800777783e0SGreg Kroah-Hartman node->has_weak_ref = 1;
4801777783e0SGreg Kroah-Hartman node->pending_weak_ref = 1;
4802777783e0SGreg Kroah-Hartman node->local_weak_refs++;
480326b47d8aSTodd Kjos }
480426b47d8aSTodd Kjos if (strong && !has_strong_ref) {
4805777783e0SGreg Kroah-Hartman node->has_strong_ref = 1;
4806777783e0SGreg Kroah-Hartman node->pending_strong_ref = 1;
4807777783e0SGreg Kroah-Hartman node->local_strong_refs++;
4808777783e0SGreg Kroah-Hartman }
480926b47d8aSTodd Kjos if (!strong && has_strong_ref)
481026b47d8aSTodd Kjos node->has_strong_ref = 0;
481126b47d8aSTodd Kjos if (!weak && has_weak_ref)
481226b47d8aSTodd Kjos node->has_weak_ref = 0;
4813777783e0SGreg Kroah-Hartman if (!weak && !strong) {
4814777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4815777783e0SGreg Kroah-Hartman "%d:%d node %d u%016llx c%016llx deleted\n",
4816777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
481726b47d8aSTodd Kjos node_debug_id,
481826b47d8aSTodd Kjos (u64)node_ptr,
481926b47d8aSTodd Kjos (u64)node_cookie);
4820777783e0SGreg Kroah-Hartman rb_erase(&node->rb_node, &proc->nodes);
4821ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4822673068eeSTodd Kjos binder_node_lock(node);
4823673068eeSTodd Kjos /*
4824673068eeSTodd Kjos * Acquire the node lock before freeing the
4825673068eeSTodd Kjos * node to serialize with other threads that
4826673068eeSTodd Kjos * may have been holding the node lock while
4827673068eeSTodd Kjos * decrementing this node (avoids race where
4828673068eeSTodd Kjos * this thread frees while the other thread
4829673068eeSTodd Kjos * is unlocking the node after the final
4830673068eeSTodd Kjos * decrement)
4831673068eeSTodd Kjos */
4832673068eeSTodd Kjos binder_node_unlock(node);
4833ed29721eSTodd Kjos binder_free_node(node);
4834ed29721eSTodd Kjos } else
4835ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4836ed29721eSTodd Kjos
483726b47d8aSTodd Kjos if (weak && !has_weak_ref)
483826b47d8aSTodd Kjos ret = binder_put_node_cmd(
483926b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
484026b47d8aSTodd Kjos node_cookie, node_debug_id,
484126b47d8aSTodd Kjos BR_INCREFS, "BR_INCREFS");
484226b47d8aSTodd Kjos if (!ret && strong && !has_strong_ref)
484326b47d8aSTodd Kjos ret = binder_put_node_cmd(
484426b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
484526b47d8aSTodd Kjos node_cookie, node_debug_id,
484626b47d8aSTodd Kjos BR_ACQUIRE, "BR_ACQUIRE");
484726b47d8aSTodd Kjos if (!ret && !strong && has_strong_ref)
484826b47d8aSTodd Kjos ret = binder_put_node_cmd(
484926b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
485026b47d8aSTodd Kjos node_cookie, node_debug_id,
485126b47d8aSTodd Kjos BR_RELEASE, "BR_RELEASE");
485226b47d8aSTodd Kjos if (!ret && !weak && has_weak_ref)
485326b47d8aSTodd Kjos ret = binder_put_node_cmd(
485426b47d8aSTodd Kjos proc, thread, &ptr, node_ptr,
485526b47d8aSTodd Kjos node_cookie, node_debug_id,
485626b47d8aSTodd Kjos BR_DECREFS, "BR_DECREFS");
485726b47d8aSTodd Kjos if (orig_ptr == ptr)
4858777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4859777783e0SGreg Kroah-Hartman "%d:%d node %d u%016llx c%016llx state unchanged\n",
4860777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
486126b47d8aSTodd Kjos node_debug_id,
486226b47d8aSTodd Kjos (u64)node_ptr,
486326b47d8aSTodd Kjos (u64)node_cookie);
486426b47d8aSTodd Kjos if (ret)
486526b47d8aSTodd Kjos return ret;
4866777783e0SGreg Kroah-Hartman } break;
4867777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER:
4868777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4869777783e0SGreg Kroah-Hartman case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4870777783e0SGreg Kroah-Hartman struct binder_ref_death *death;
4871777783e0SGreg Kroah-Hartman uint32_t cmd;
4872ab51ec6bSMartijn Coenen binder_uintptr_t cookie;
4873777783e0SGreg Kroah-Hartman
4874777783e0SGreg Kroah-Hartman death = container_of(w, struct binder_ref_death, work);
4875777783e0SGreg Kroah-Hartman if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4876777783e0SGreg Kroah-Hartman cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4877777783e0SGreg Kroah-Hartman else
4878777783e0SGreg Kroah-Hartman cmd = BR_DEAD_BINDER;
4879ab51ec6bSMartijn Coenen cookie = death->cookie;
4880ab51ec6bSMartijn Coenen
4881777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
4882777783e0SGreg Kroah-Hartman "%d:%d %s %016llx\n",
4883777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
4884777783e0SGreg Kroah-Hartman cmd == BR_DEAD_BINDER ?
4885777783e0SGreg Kroah-Hartman "BR_DEAD_BINDER" :
4886777783e0SGreg Kroah-Hartman "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4887ab51ec6bSMartijn Coenen (u64)cookie);
4888777783e0SGreg Kroah-Hartman if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
4889ab51ec6bSMartijn Coenen binder_inner_proc_unlock(proc);
4890777783e0SGreg Kroah-Hartman kfree(death);
4891777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_DEATH);
4892ed29721eSTodd Kjos } else {
489372196393STodd Kjos binder_enqueue_work_ilocked(
489472196393STodd Kjos w, &proc->delivered_death);
4895ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
4896ed29721eSTodd Kjos }
4897ab51ec6bSMartijn Coenen if (put_user(cmd, (uint32_t __user *)ptr))
4898ab51ec6bSMartijn Coenen return -EFAULT;
4899ab51ec6bSMartijn Coenen ptr += sizeof(uint32_t);
4900ab51ec6bSMartijn Coenen if (put_user(cookie,
4901ab51ec6bSMartijn Coenen (binder_uintptr_t __user *)ptr))
4902ab51ec6bSMartijn Coenen return -EFAULT;
4903ab51ec6bSMartijn Coenen ptr += sizeof(binder_uintptr_t);
4904ab51ec6bSMartijn Coenen binder_stat_br(proc, thread, cmd);
4905777783e0SGreg Kroah-Hartman if (cmd == BR_DEAD_BINDER)
4906777783e0SGreg Kroah-Hartman goto done; /* DEAD_BINDER notifications can cause transactions */
4907777783e0SGreg Kroah-Hartman } break;
4908d579b04aSYu-Ting Tseng
4909d579b04aSYu-Ting Tseng case BINDER_WORK_FROZEN_BINDER: {
4910d579b04aSYu-Ting Tseng struct binder_ref_freeze *freeze;
4911d579b04aSYu-Ting Tseng struct binder_frozen_state_info info;
4912d579b04aSYu-Ting Tseng
4913d579b04aSYu-Ting Tseng memset(&info, 0, sizeof(info));
4914d579b04aSYu-Ting Tseng freeze = container_of(w, struct binder_ref_freeze, work);
4915d579b04aSYu-Ting Tseng info.is_frozen = freeze->is_frozen;
4916d579b04aSYu-Ting Tseng info.cookie = freeze->cookie;
4917d579b04aSYu-Ting Tseng freeze->sent = true;
4918d579b04aSYu-Ting Tseng binder_enqueue_work_ilocked(w, &proc->delivered_freeze);
4919d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
4920d579b04aSYu-Ting Tseng
4921d579b04aSYu-Ting Tseng if (put_user(BR_FROZEN_BINDER, (uint32_t __user *)ptr))
4922d579b04aSYu-Ting Tseng return -EFAULT;
4923d579b04aSYu-Ting Tseng ptr += sizeof(uint32_t);
4924d579b04aSYu-Ting Tseng if (copy_to_user(ptr, &info, sizeof(info)))
4925d579b04aSYu-Ting Tseng return -EFAULT;
4926d579b04aSYu-Ting Tseng ptr += sizeof(info);
4927d579b04aSYu-Ting Tseng binder_stat_br(proc, thread, BR_FROZEN_BINDER);
4928d579b04aSYu-Ting Tseng goto done; /* BR_FROZEN_BINDER notifications can cause transactions */
4929d579b04aSYu-Ting Tseng } break;
4930d579b04aSYu-Ting Tseng
4931d579b04aSYu-Ting Tseng case BINDER_WORK_CLEAR_FREEZE_NOTIFICATION: {
4932d579b04aSYu-Ting Tseng struct binder_ref_freeze *freeze =
4933d579b04aSYu-Ting Tseng container_of(w, struct binder_ref_freeze, work);
4934d579b04aSYu-Ting Tseng binder_uintptr_t cookie = freeze->cookie;
4935d579b04aSYu-Ting Tseng
4936d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
4937d579b04aSYu-Ting Tseng kfree(freeze);
4938d579b04aSYu-Ting Tseng binder_stats_deleted(BINDER_STAT_FREEZE);
4939d579b04aSYu-Ting Tseng if (put_user(BR_CLEAR_FREEZE_NOTIFICATION_DONE, (uint32_t __user *)ptr))
4940d579b04aSYu-Ting Tseng return -EFAULT;
4941d579b04aSYu-Ting Tseng ptr += sizeof(uint32_t);
4942d579b04aSYu-Ting Tseng if (put_user(cookie, (binder_uintptr_t __user *)ptr))
4943d579b04aSYu-Ting Tseng return -EFAULT;
4944d579b04aSYu-Ting Tseng ptr += sizeof(binder_uintptr_t);
4945d579b04aSYu-Ting Tseng binder_stat_br(proc, thread, BR_CLEAR_FREEZE_NOTIFICATION_DONE);
4946d579b04aSYu-Ting Tseng } break;
4947d579b04aSYu-Ting Tseng
4948324fa64cSTodd Kjos default:
4949324fa64cSTodd Kjos binder_inner_proc_unlock(proc);
4950324fa64cSTodd Kjos pr_err("%d:%d: bad work type %d\n",
4951324fa64cSTodd Kjos proc->pid, thread->pid, w->type);
4952324fa64cSTodd Kjos break;
4953777783e0SGreg Kroah-Hartman }
4954777783e0SGreg Kroah-Hartman
4955777783e0SGreg Kroah-Hartman if (!t)
4956777783e0SGreg Kroah-Hartman continue;
4957777783e0SGreg Kroah-Hartman
4958777783e0SGreg Kroah-Hartman BUG_ON(t->buffer == NULL);
4959777783e0SGreg Kroah-Hartman if (t->buffer->target_node) {
4960777783e0SGreg Kroah-Hartman struct binder_node *target_node = t->buffer->target_node;
4961777783e0SGreg Kroah-Hartman
4962ec74136dSTodd Kjos trd->target.ptr = target_node->ptr;
4963ec74136dSTodd Kjos trd->cookie = target_node->cookie;
4964777783e0SGreg Kroah-Hartman t->saved_priority = task_nice(current);
4965777783e0SGreg Kroah-Hartman if (t->priority < target_node->min_priority &&
4966777783e0SGreg Kroah-Hartman !(t->flags & TF_ONE_WAY))
4967777783e0SGreg Kroah-Hartman binder_set_nice(t->priority);
4968777783e0SGreg Kroah-Hartman else if (!(t->flags & TF_ONE_WAY) ||
4969777783e0SGreg Kroah-Hartman t->saved_priority > target_node->min_priority)
4970777783e0SGreg Kroah-Hartman binder_set_nice(target_node->min_priority);
4971777783e0SGreg Kroah-Hartman cmd = BR_TRANSACTION;
4972777783e0SGreg Kroah-Hartman } else {
4973ec74136dSTodd Kjos trd->target.ptr = 0;
4974ec74136dSTodd Kjos trd->cookie = 0;
4975777783e0SGreg Kroah-Hartman cmd = BR_REPLY;
4976777783e0SGreg Kroah-Hartman }
4977ec74136dSTodd Kjos trd->code = t->code;
4978ec74136dSTodd Kjos trd->flags = t->flags;
4979ec74136dSTodd Kjos trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
4980777783e0SGreg Kroah-Hartman
49817a4408c6STodd Kjos t_from = binder_get_txn_from(t);
49827a4408c6STodd Kjos if (t_from) {
49837a4408c6STodd Kjos struct task_struct *sender = t_from->proc->tsk;
4984777783e0SGreg Kroah-Hartman
4985ec74136dSTodd Kjos trd->sender_pid =
4986ec74136dSTodd Kjos task_tgid_nr_ns(sender,
4987777783e0SGreg Kroah-Hartman task_active_pid_ns(current));
4988777783e0SGreg Kroah-Hartman } else {
4989ec74136dSTodd Kjos trd->sender_pid = 0;
4990777783e0SGreg Kroah-Hartman }
4991777783e0SGreg Kroah-Hartman
49928ced0c62STodd Kjos ret = binder_apply_fd_fixups(proc, t);
499344d8047fSTodd Kjos if (ret) {
499444d8047fSTodd Kjos struct binder_buffer *buffer = t->buffer;
499544d8047fSTodd Kjos bool oneway = !!(t->flags & TF_ONE_WAY);
499644d8047fSTodd Kjos int tid = t->debug_id;
499744d8047fSTodd Kjos
499844d8047fSTodd Kjos if (t_from)
499944d8047fSTodd Kjos binder_thread_dec_tmpref(t_from);
500044d8047fSTodd Kjos buffer->transaction = NULL;
500144d8047fSTodd Kjos binder_cleanup_transaction(t, "fd fixups failed",
500244d8047fSTodd Kjos BR_FAILED_REPLY);
500332e9f56aSTodd Kjos binder_free_buf(proc, thread, buffer, true);
500444d8047fSTodd Kjos binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
500544d8047fSTodd Kjos "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n",
500644d8047fSTodd Kjos proc->pid, thread->pid,
500744d8047fSTodd Kjos oneway ? "async " :
500844d8047fSTodd Kjos (cmd == BR_REPLY ? "reply " : ""),
500944d8047fSTodd Kjos tid, BR_FAILED_REPLY, ret, __LINE__);
501044d8047fSTodd Kjos if (cmd == BR_REPLY) {
501144d8047fSTodd Kjos cmd = BR_FAILED_REPLY;
501244d8047fSTodd Kjos if (put_user(cmd, (uint32_t __user *)ptr))
501344d8047fSTodd Kjos return -EFAULT;
501444d8047fSTodd Kjos ptr += sizeof(uint32_t);
501544d8047fSTodd Kjos binder_stat_br(proc, thread, cmd);
501644d8047fSTodd Kjos break;
501744d8047fSTodd Kjos }
501844d8047fSTodd Kjos continue;
501944d8047fSTodd Kjos }
5020ec74136dSTodd Kjos trd->data_size = t->buffer->data_size;
5021ec74136dSTodd Kjos trd->offsets_size = t->buffer->offsets_size;
5022df9aabeaSCarlos Llamas trd->data.ptr.buffer = t->buffer->user_data;
5023ec74136dSTodd Kjos trd->data.ptr.offsets = trd->data.ptr.buffer +
5024777783e0SGreg Kroah-Hartman ALIGN(t->buffer->data_size,
5025777783e0SGreg Kroah-Hartman sizeof(void *));
5026777783e0SGreg Kroah-Hartman
5027ec74136dSTodd Kjos tr.secctx = t->security_ctx;
5028ec74136dSTodd Kjos if (t->security_ctx) {
5029ec74136dSTodd Kjos cmd = BR_TRANSACTION_SEC_CTX;
5030ec74136dSTodd Kjos trsize = sizeof(tr);
5031ec74136dSTodd Kjos }
50327a4408c6STodd Kjos if (put_user(cmd, (uint32_t __user *)ptr)) {
50337a4408c6STodd Kjos if (t_from)
50347a4408c6STodd Kjos binder_thread_dec_tmpref(t_from);
5035fb2c4452SMartijn Coenen
5036fb2c4452SMartijn Coenen binder_cleanup_transaction(t, "put_user failed",
5037fb2c4452SMartijn Coenen BR_FAILED_REPLY);
5038fb2c4452SMartijn Coenen
5039777783e0SGreg Kroah-Hartman return -EFAULT;
50407a4408c6STodd Kjos }
5041777783e0SGreg Kroah-Hartman ptr += sizeof(uint32_t);
5042ec74136dSTodd Kjos if (copy_to_user(ptr, &tr, trsize)) {
50437a4408c6STodd Kjos if (t_from)
50447a4408c6STodd Kjos binder_thread_dec_tmpref(t_from);
5045fb2c4452SMartijn Coenen
5046fb2c4452SMartijn Coenen binder_cleanup_transaction(t, "copy_to_user failed",
5047fb2c4452SMartijn Coenen BR_FAILED_REPLY);
5048fb2c4452SMartijn Coenen
5049777783e0SGreg Kroah-Hartman return -EFAULT;
50507a4408c6STodd Kjos }
5051ec74136dSTodd Kjos ptr += trsize;
5052777783e0SGreg Kroah-Hartman
5053777783e0SGreg Kroah-Hartman trace_binder_transaction_received(t);
5054777783e0SGreg Kroah-Hartman binder_stat_br(proc, thread, cmd);
5055777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_TRANSACTION,
5056da486496SCarlos Llamas "%d:%d %s %d %d:%d, cmd %u size %zd-%zd ptr %016llx-%016llx\n",
5057777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
5058777783e0SGreg Kroah-Hartman (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
5059ec74136dSTodd Kjos (cmd == BR_TRANSACTION_SEC_CTX) ?
5060ec74136dSTodd Kjos "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
50617a4408c6STodd Kjos t->debug_id, t_from ? t_from->proc->pid : 0,
50627a4408c6STodd Kjos t_from ? t_from->pid : 0, cmd,
5063777783e0SGreg Kroah-Hartman t->buffer->data_size, t->buffer->offsets_size,
5064ec74136dSTodd Kjos (u64)trd->data.ptr.buffer,
5065ec74136dSTodd Kjos (u64)trd->data.ptr.offsets);
5066777783e0SGreg Kroah-Hartman
50677a4408c6STodd Kjos if (t_from)
50687a4408c6STodd Kjos binder_thread_dec_tmpref(t_from);
5069777783e0SGreg Kroah-Hartman t->buffer->allow_user_free = 1;
5070ec74136dSTodd Kjos if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
50710b89d69aSMartijn Coenen binder_inner_proc_lock(thread->proc);
5072777783e0SGreg Kroah-Hartman t->to_parent = thread->transaction_stack;
5073777783e0SGreg Kroah-Hartman t->to_thread = thread;
5074777783e0SGreg Kroah-Hartman thread->transaction_stack = t;
50750b89d69aSMartijn Coenen binder_inner_proc_unlock(thread->proc);
5076777783e0SGreg Kroah-Hartman } else {
5077b6d282ceSTodd Kjos binder_free_transaction(t);
5078777783e0SGreg Kroah-Hartman }
5079777783e0SGreg Kroah-Hartman break;
5080777783e0SGreg Kroah-Hartman }
5081777783e0SGreg Kroah-Hartman
5082777783e0SGreg Kroah-Hartman done:
5083777783e0SGreg Kroah-Hartman
5084777783e0SGreg Kroah-Hartman *consumed = ptr - buffer;
5085b3e68612STodd Kjos binder_inner_proc_lock(proc);
50861b77e9dcSMartijn Coenen if (proc->requested_threads == 0 &&
50871b77e9dcSMartijn Coenen list_empty(&thread->proc->waiting_threads) &&
5088777783e0SGreg Kroah-Hartman proc->requested_threads_started < proc->max_threads &&
5089777783e0SGreg Kroah-Hartman (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
5090777783e0SGreg Kroah-Hartman BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
5091777783e0SGreg Kroah-Hartman /*spawn a new thread if we leave this out */) {
5092777783e0SGreg Kroah-Hartman proc->requested_threads++;
5093b3e68612STodd Kjos binder_inner_proc_unlock(proc);
5094777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS,
5095777783e0SGreg Kroah-Hartman "%d:%d BR_SPAWN_LOOPER\n",
5096777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
5097777783e0SGreg Kroah-Hartman if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
5098777783e0SGreg Kroah-Hartman return -EFAULT;
5099777783e0SGreg Kroah-Hartman binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
5100b3e68612STodd Kjos } else
5101b3e68612STodd Kjos binder_inner_proc_unlock(proc);
5102777783e0SGreg Kroah-Hartman return 0;
5103777783e0SGreg Kroah-Hartman }
5104777783e0SGreg Kroah-Hartman
binder_release_work(struct binder_proc * proc,struct list_head * list)510572196393STodd Kjos static void binder_release_work(struct binder_proc *proc,
510672196393STodd Kjos struct list_head *list)
5107777783e0SGreg Kroah-Hartman {
5108777783e0SGreg Kroah-Hartman struct binder_work *w;
5109f3277cbfSTodd Kjos enum binder_work_type wtype;
5110777783e0SGreg Kroah-Hartman
511172196393STodd Kjos while (1) {
5112f3277cbfSTodd Kjos binder_inner_proc_lock(proc);
5113f3277cbfSTodd Kjos w = binder_dequeue_work_head_ilocked(list);
5114f3277cbfSTodd Kjos wtype = w ? w->type : 0;
5115f3277cbfSTodd Kjos binder_inner_proc_unlock(proc);
511672196393STodd Kjos if (!w)
511772196393STodd Kjos return;
511872196393STodd Kjos
5119f3277cbfSTodd Kjos switch (wtype) {
5120777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION: {
5121777783e0SGreg Kroah-Hartman struct binder_transaction *t;
5122777783e0SGreg Kroah-Hartman
5123777783e0SGreg Kroah-Hartman t = container_of(w, struct binder_transaction, work);
5124fb2c4452SMartijn Coenen
5125fb2c4452SMartijn Coenen binder_cleanup_transaction(t, "process died.",
5126fb2c4452SMartijn Coenen BR_DEAD_REPLY);
5127777783e0SGreg Kroah-Hartman } break;
512826549d17STodd Kjos case BINDER_WORK_RETURN_ERROR: {
512926549d17STodd Kjos struct binder_error *e = container_of(
513026549d17STodd Kjos w, struct binder_error, work);
513126549d17STodd Kjos
513226549d17STodd Kjos binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
513326549d17STodd Kjos "undelivered TRANSACTION_ERROR: %u\n",
513426549d17STodd Kjos e->cmd);
513526549d17STodd Kjos } break;
51361aa3aaf8SCarlos Llamas case BINDER_WORK_TRANSACTION_PENDING:
51371aa3aaf8SCarlos Llamas case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT:
5138777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION_COMPLETE: {
5139777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
5140777783e0SGreg Kroah-Hartman "undelivered TRANSACTION_COMPLETE\n");
5141777783e0SGreg Kroah-Hartman kfree(w);
5142777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
5143777783e0SGreg Kroah-Hartman } break;
5144777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
5145777783e0SGreg Kroah-Hartman case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
5146777783e0SGreg Kroah-Hartman struct binder_ref_death *death;
5147777783e0SGreg Kroah-Hartman
5148777783e0SGreg Kroah-Hartman death = container_of(w, struct binder_ref_death, work);
5149777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
5150777783e0SGreg Kroah-Hartman "undelivered death notification, %016llx\n",
5151777783e0SGreg Kroah-Hartman (u64)death->cookie);
5152777783e0SGreg Kroah-Hartman kfree(death);
5153777783e0SGreg Kroah-Hartman binder_stats_deleted(BINDER_STAT_DEATH);
5154777783e0SGreg Kroah-Hartman } break;
5155f3277cbfSTodd Kjos case BINDER_WORK_NODE:
5156f3277cbfSTodd Kjos break;
51571db76ec2SCarlos Llamas case BINDER_WORK_CLEAR_FREEZE_NOTIFICATION: {
51581db76ec2SCarlos Llamas struct binder_ref_freeze *freeze;
51591db76ec2SCarlos Llamas
51601db76ec2SCarlos Llamas freeze = container_of(w, struct binder_ref_freeze, work);
51611db76ec2SCarlos Llamas binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
51621db76ec2SCarlos Llamas "undelivered freeze notification, %016llx\n",
51631db76ec2SCarlos Llamas (u64)freeze->cookie);
51641db76ec2SCarlos Llamas kfree(freeze);
51651db76ec2SCarlos Llamas binder_stats_deleted(BINDER_STAT_FREEZE);
51661db76ec2SCarlos Llamas } break;
5167777783e0SGreg Kroah-Hartman default:
5168777783e0SGreg Kroah-Hartman pr_err("unexpected work type, %d, not freed\n",
5169f3277cbfSTodd Kjos wtype);
5170777783e0SGreg Kroah-Hartman break;
5171777783e0SGreg Kroah-Hartman }
5172777783e0SGreg Kroah-Hartman }
5173777783e0SGreg Kroah-Hartman
5174777783e0SGreg Kroah-Hartman }
5175777783e0SGreg Kroah-Hartman
binder_get_thread_ilocked(struct binder_proc * proc,struct binder_thread * new_thread)51767bd7b0e6STodd Kjos static struct binder_thread *binder_get_thread_ilocked(
51777bd7b0e6STodd Kjos struct binder_proc *proc, struct binder_thread *new_thread)
5178777783e0SGreg Kroah-Hartman {
5179777783e0SGreg Kroah-Hartman struct binder_thread *thread = NULL;
5180777783e0SGreg Kroah-Hartman struct rb_node *parent = NULL;
5181777783e0SGreg Kroah-Hartman struct rb_node **p = &proc->threads.rb_node;
5182777783e0SGreg Kroah-Hartman
5183777783e0SGreg Kroah-Hartman while (*p) {
5184777783e0SGreg Kroah-Hartman parent = *p;
5185777783e0SGreg Kroah-Hartman thread = rb_entry(parent, struct binder_thread, rb_node);
5186777783e0SGreg Kroah-Hartman
5187777783e0SGreg Kroah-Hartman if (current->pid < thread->pid)
5188777783e0SGreg Kroah-Hartman p = &(*p)->rb_left;
5189777783e0SGreg Kroah-Hartman else if (current->pid > thread->pid)
5190777783e0SGreg Kroah-Hartman p = &(*p)->rb_right;
5191777783e0SGreg Kroah-Hartman else
51927bd7b0e6STodd Kjos return thread;
5193777783e0SGreg Kroah-Hartman }
51947bd7b0e6STodd Kjos if (!new_thread)
5195777783e0SGreg Kroah-Hartman return NULL;
51967bd7b0e6STodd Kjos thread = new_thread;
5197777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_THREAD);
5198777783e0SGreg Kroah-Hartman thread->proc = proc;
5199777783e0SGreg Kroah-Hartman thread->pid = current->pid;
52007a4408c6STodd Kjos atomic_set(&thread->tmp_ref, 0);
5201777783e0SGreg Kroah-Hartman init_waitqueue_head(&thread->wait);
5202777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&thread->todo);
5203777783e0SGreg Kroah-Hartman rb_link_node(&thread->rb_node, parent, p);
5204777783e0SGreg Kroah-Hartman rb_insert_color(&thread->rb_node, &proc->threads);
520508dabceeSTodd Kjos thread->looper_need_return = true;
520626549d17STodd Kjos thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
520726549d17STodd Kjos thread->return_error.cmd = BR_OK;
520826549d17STodd Kjos thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
520926549d17STodd Kjos thread->reply_error.cmd = BR_OK;
5210bd32889eSCarlos Llamas thread->ee.command = BR_OK;
52111b77e9dcSMartijn Coenen INIT_LIST_HEAD(&new_thread->waiting_thread_node);
52127bd7b0e6STodd Kjos return thread;
52137bd7b0e6STodd Kjos }
52147bd7b0e6STodd Kjos
binder_get_thread(struct binder_proc * proc)52157bd7b0e6STodd Kjos static struct binder_thread *binder_get_thread(struct binder_proc *proc)
52167bd7b0e6STodd Kjos {
52177bd7b0e6STodd Kjos struct binder_thread *thread;
52187bd7b0e6STodd Kjos struct binder_thread *new_thread;
52197bd7b0e6STodd Kjos
52207bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
52217bd7b0e6STodd Kjos thread = binder_get_thread_ilocked(proc, NULL);
52227bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
52237bd7b0e6STodd Kjos if (!thread) {
52247bd7b0e6STodd Kjos new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
52257bd7b0e6STodd Kjos if (new_thread == NULL)
52267bd7b0e6STodd Kjos return NULL;
52277bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
52287bd7b0e6STodd Kjos thread = binder_get_thread_ilocked(proc, new_thread);
52297bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
52307bd7b0e6STodd Kjos if (thread != new_thread)
52317bd7b0e6STodd Kjos kfree(new_thread);
5232777783e0SGreg Kroah-Hartman }
5233777783e0SGreg Kroah-Hartman return thread;
5234777783e0SGreg Kroah-Hartman }
5235777783e0SGreg Kroah-Hartman
binder_free_proc(struct binder_proc * proc)52367a4408c6STodd Kjos static void binder_free_proc(struct binder_proc *proc)
52377a4408c6STodd Kjos {
5238d35d3660STodd Kjos struct binder_device *device;
5239d35d3660STodd Kjos
52407a4408c6STodd Kjos BUG_ON(!list_empty(&proc->todo));
52417a4408c6STodd Kjos BUG_ON(!list_empty(&proc->delivered_death));
5242432ff1e9SMarco Ballesio if (proc->outstanding_txns)
5243432ff1e9SMarco Ballesio pr_warn("%s: Unexpected outstanding_txns %d\n",
5244432ff1e9SMarco Ballesio __func__, proc->outstanding_txns);
5245d35d3660STodd Kjos device = container_of(proc->context, struct binder_device, context);
5246d35d3660STodd Kjos if (refcount_dec_and_test(&device->ref)) {
5247d35d3660STodd Kjos kfree(proc->context->name);
5248d35d3660STodd Kjos kfree(device);
5249d35d3660STodd Kjos }
52507a4408c6STodd Kjos binder_alloc_deferred_release(&proc->alloc);
52517a4408c6STodd Kjos put_task_struct(proc->tsk);
525229bc22acSTodd Kjos put_cred(proc->cred);
52537a4408c6STodd Kjos binder_stats_deleted(BINDER_STAT_PROC);
525415d9da3fSCarlos Llamas dbitmap_free(&proc->dmap);
52557a4408c6STodd Kjos kfree(proc);
52567a4408c6STodd Kjos }
52577a4408c6STodd Kjos
binder_free_thread(struct binder_thread * thread)52587a4408c6STodd Kjos static void binder_free_thread(struct binder_thread *thread)
52597a4408c6STodd Kjos {
52607a4408c6STodd Kjos BUG_ON(!list_empty(&thread->todo));
52617a4408c6STodd Kjos binder_stats_deleted(BINDER_STAT_THREAD);
52627a4408c6STodd Kjos binder_proc_dec_tmpref(thread->proc);
52637a4408c6STodd Kjos kfree(thread);
52647a4408c6STodd Kjos }
52657a4408c6STodd Kjos
binder_thread_release(struct binder_proc * proc,struct binder_thread * thread)52667a4408c6STodd Kjos static int binder_thread_release(struct binder_proc *proc,
5267777783e0SGreg Kroah-Hartman struct binder_thread *thread)
5268777783e0SGreg Kroah-Hartman {
5269777783e0SGreg Kroah-Hartman struct binder_transaction *t;
5270777783e0SGreg Kroah-Hartman struct binder_transaction *send_reply = NULL;
5271777783e0SGreg Kroah-Hartman int active_transactions = 0;
52727a4408c6STodd Kjos struct binder_transaction *last_t = NULL;
5273777783e0SGreg Kroah-Hartman
52747bd7b0e6STodd Kjos binder_inner_proc_lock(thread->proc);
52757a4408c6STodd Kjos /*
52767a4408c6STodd Kjos * take a ref on the proc so it survives
52777a4408c6STodd Kjos * after we remove this thread from proc->threads.
52787a4408c6STodd Kjos * The corresponding dec is when we actually
52797a4408c6STodd Kjos * free the thread in binder_free_thread()
52807a4408c6STodd Kjos */
52817a4408c6STodd Kjos proc->tmp_ref++;
52827a4408c6STodd Kjos /*
52837a4408c6STodd Kjos * take a ref on this thread to ensure it
52847a4408c6STodd Kjos * survives while we are releasing it
52857a4408c6STodd Kjos */
52867a4408c6STodd Kjos atomic_inc(&thread->tmp_ref);
5287777783e0SGreg Kroah-Hartman rb_erase(&thread->rb_node, &proc->threads);
5288777783e0SGreg Kroah-Hartman t = thread->transaction_stack;
52897a4408c6STodd Kjos if (t) {
52907a4408c6STodd Kjos spin_lock(&t->lock);
52917a4408c6STodd Kjos if (t->to_thread == thread)
5292777783e0SGreg Kroah-Hartman send_reply = t;
5293324fa64cSTodd Kjos } else {
5294324fa64cSTodd Kjos __acquire(&t->lock);
52957a4408c6STodd Kjos }
52967a4408c6STodd Kjos thread->is_dead = true;
52977a4408c6STodd Kjos
5298777783e0SGreg Kroah-Hartman while (t) {
52997a4408c6STodd Kjos last_t = t;
5300777783e0SGreg Kroah-Hartman active_transactions++;
5301777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
5302777783e0SGreg Kroah-Hartman "release %d:%d transaction %d %s, still active\n",
5303777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
5304777783e0SGreg Kroah-Hartman t->debug_id,
5305777783e0SGreg Kroah-Hartman (t->to_thread == thread) ? "in" : "out");
5306777783e0SGreg Kroah-Hartman
5307777783e0SGreg Kroah-Hartman if (t->to_thread == thread) {
5308432ff1e9SMarco Ballesio thread->proc->outstanding_txns--;
5309777783e0SGreg Kroah-Hartman t->to_proc = NULL;
5310777783e0SGreg Kroah-Hartman t->to_thread = NULL;
5311777783e0SGreg Kroah-Hartman if (t->buffer) {
5312777783e0SGreg Kroah-Hartman t->buffer->transaction = NULL;
5313777783e0SGreg Kroah-Hartman t->buffer = NULL;
5314777783e0SGreg Kroah-Hartman }
5315777783e0SGreg Kroah-Hartman t = t->to_parent;
5316777783e0SGreg Kroah-Hartman } else if (t->from == thread) {
5317777783e0SGreg Kroah-Hartman t->from = NULL;
5318777783e0SGreg Kroah-Hartman t = t->from_parent;
5319777783e0SGreg Kroah-Hartman } else
5320777783e0SGreg Kroah-Hartman BUG();
53217a4408c6STodd Kjos spin_unlock(&last_t->lock);
53227a4408c6STodd Kjos if (t)
53237a4408c6STodd Kjos spin_lock(&t->lock);
5324324fa64cSTodd Kjos else
5325324fa64cSTodd Kjos __acquire(&t->lock);
5326777783e0SGreg Kroah-Hartman }
5327324fa64cSTodd Kjos /* annotation for sparse, lock not acquired in last iteration above */
5328324fa64cSTodd Kjos __release(&t->lock);
5329f5cb779bSMartijn Coenen
5330f5cb779bSMartijn Coenen /*
5331a880b28aSEric Biggers * If this thread used poll, make sure we remove the waitqueue from any
5332a880b28aSEric Biggers * poll data structures holding it.
5333f5cb779bSMartijn Coenen */
5334a880b28aSEric Biggers if (thread->looper & BINDER_LOOPER_STATE_POLL)
5335a880b28aSEric Biggers wake_up_pollfree(&thread->wait);
5336f5cb779bSMartijn Coenen
53377bd7b0e6STodd Kjos binder_inner_proc_unlock(thread->proc);
53387a4408c6STodd Kjos
53395eeb2ca0SMartijn Coenen /*
5340a880b28aSEric Biggers * This is needed to avoid races between wake_up_pollfree() above and
5341a880b28aSEric Biggers * someone else removing the last entry from the queue for other reasons
5342a880b28aSEric Biggers * (e.g. ep_remove_wait_queue() being called due to an epoll file
5343a880b28aSEric Biggers * descriptor being closed). Such other users hold an RCU read lock, so
5344a880b28aSEric Biggers * we can be sure they're done after we call synchronize_rcu().
53455eeb2ca0SMartijn Coenen */
53465eeb2ca0SMartijn Coenen if (thread->looper & BINDER_LOOPER_STATE_POLL)
53475eeb2ca0SMartijn Coenen synchronize_rcu();
53485eeb2ca0SMartijn Coenen
5349777783e0SGreg Kroah-Hartman if (send_reply)
5350777783e0SGreg Kroah-Hartman binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
535172196393STodd Kjos binder_release_work(proc, &thread->todo);
53527a4408c6STodd Kjos binder_thread_dec_tmpref(thread);
5353777783e0SGreg Kroah-Hartman return active_transactions;
5354777783e0SGreg Kroah-Hartman }
5355777783e0SGreg Kroah-Hartman
binder_poll(struct file * filp,struct poll_table_struct * wait)5356afc9a42bSAl Viro static __poll_t binder_poll(struct file *filp,
5357777783e0SGreg Kroah-Hartman struct poll_table_struct *wait)
5358777783e0SGreg Kroah-Hartman {
5359777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5360777783e0SGreg Kroah-Hartman struct binder_thread *thread = NULL;
53611b77e9dcSMartijn Coenen bool wait_for_proc_work;
5362777783e0SGreg Kroah-Hartman
5363777783e0SGreg Kroah-Hartman thread = binder_get_thread(proc);
5364f8898267SEric Biggers if (!thread)
53656ac061dbSCarlos Llamas return EPOLLERR;
5366777783e0SGreg Kroah-Hartman
53670b89d69aSMartijn Coenen binder_inner_proc_lock(thread->proc);
53681b77e9dcSMartijn Coenen thread->looper |= BINDER_LOOPER_STATE_POLL;
53691b77e9dcSMartijn Coenen wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
53701b77e9dcSMartijn Coenen
53710b89d69aSMartijn Coenen binder_inner_proc_unlock(thread->proc);
5372777783e0SGreg Kroah-Hartman
5373777783e0SGreg Kroah-Hartman poll_wait(filp, &thread->wait, wait);
53741b77e9dcSMartijn Coenen
537566b83a4cSMartijn Coenen if (binder_has_work(thread, wait_for_proc_work))
5376a9a08845SLinus Torvalds return EPOLLIN;
53771b77e9dcSMartijn Coenen
5378777783e0SGreg Kroah-Hartman return 0;
5379777783e0SGreg Kroah-Hartman }
5380777783e0SGreg Kroah-Hartman
binder_ioctl_write_read(struct file * filp,unsigned long arg,struct binder_thread * thread)53817feb35bcSJiazi.Li static int binder_ioctl_write_read(struct file *filp, unsigned long arg,
5382777783e0SGreg Kroah-Hartman struct binder_thread *thread)
5383777783e0SGreg Kroah-Hartman {
5384777783e0SGreg Kroah-Hartman int ret = 0;
5385777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5386777783e0SGreg Kroah-Hartman void __user *ubuf = (void __user *)arg;
5387777783e0SGreg Kroah-Hartman struct binder_write_read bwr;
5388777783e0SGreg Kroah-Hartman
5389777783e0SGreg Kroah-Hartman if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
5390777783e0SGreg Kroah-Hartman ret = -EFAULT;
5391777783e0SGreg Kroah-Hartman goto out;
5392777783e0SGreg Kroah-Hartman }
5393777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_READ_WRITE,
5394777783e0SGreg Kroah-Hartman "%d:%d write %lld at %016llx, read %lld at %016llx\n",
5395777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
5396777783e0SGreg Kroah-Hartman (u64)bwr.write_size, (u64)bwr.write_buffer,
5397777783e0SGreg Kroah-Hartman (u64)bwr.read_size, (u64)bwr.read_buffer);
5398777783e0SGreg Kroah-Hartman
5399777783e0SGreg Kroah-Hartman if (bwr.write_size > 0) {
5400777783e0SGreg Kroah-Hartman ret = binder_thread_write(proc, thread,
5401777783e0SGreg Kroah-Hartman bwr.write_buffer,
5402777783e0SGreg Kroah-Hartman bwr.write_size,
5403777783e0SGreg Kroah-Hartman &bwr.write_consumed);
5404777783e0SGreg Kroah-Hartman trace_binder_write_done(ret);
5405777783e0SGreg Kroah-Hartman if (ret < 0) {
5406777783e0SGreg Kroah-Hartman bwr.read_consumed = 0;
5407777783e0SGreg Kroah-Hartman if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
5408777783e0SGreg Kroah-Hartman ret = -EFAULT;
5409777783e0SGreg Kroah-Hartman goto out;
5410777783e0SGreg Kroah-Hartman }
5411777783e0SGreg Kroah-Hartman }
5412777783e0SGreg Kroah-Hartman if (bwr.read_size > 0) {
5413777783e0SGreg Kroah-Hartman ret = binder_thread_read(proc, thread, bwr.read_buffer,
5414777783e0SGreg Kroah-Hartman bwr.read_size,
5415777783e0SGreg Kroah-Hartman &bwr.read_consumed,
5416777783e0SGreg Kroah-Hartman filp->f_flags & O_NONBLOCK);
5417777783e0SGreg Kroah-Hartman trace_binder_read_done(ret);
54181b77e9dcSMartijn Coenen binder_inner_proc_lock(proc);
54191b77e9dcSMartijn Coenen if (!binder_worklist_empty_ilocked(&proc->todo))
5420408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(proc);
54211b77e9dcSMartijn Coenen binder_inner_proc_unlock(proc);
5422777783e0SGreg Kroah-Hartman if (ret < 0) {
5423777783e0SGreg Kroah-Hartman if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
5424777783e0SGreg Kroah-Hartman ret = -EFAULT;
5425777783e0SGreg Kroah-Hartman goto out;
5426777783e0SGreg Kroah-Hartman }
5427777783e0SGreg Kroah-Hartman }
5428777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_READ_WRITE,
5429777783e0SGreg Kroah-Hartman "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
5430777783e0SGreg Kroah-Hartman proc->pid, thread->pid,
5431777783e0SGreg Kroah-Hartman (u64)bwr.write_consumed, (u64)bwr.write_size,
5432777783e0SGreg Kroah-Hartman (u64)bwr.read_consumed, (u64)bwr.read_size);
5433777783e0SGreg Kroah-Hartman if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
5434777783e0SGreg Kroah-Hartman ret = -EFAULT;
5435777783e0SGreg Kroah-Hartman goto out;
5436777783e0SGreg Kroah-Hartman }
5437777783e0SGreg Kroah-Hartman out:
5438777783e0SGreg Kroah-Hartman return ret;
5439777783e0SGreg Kroah-Hartman }
5440777783e0SGreg Kroah-Hartman
binder_ioctl_set_ctx_mgr(struct file * filp,struct flat_binder_object * fbo)5441ec74136dSTodd Kjos static int binder_ioctl_set_ctx_mgr(struct file *filp,
5442ec74136dSTodd Kjos struct flat_binder_object *fbo)
5443777783e0SGreg Kroah-Hartman {
5444777783e0SGreg Kroah-Hartman int ret = 0;
5445777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5446342e5c90SMartijn Coenen struct binder_context *context = proc->context;
5447c44b1231STodd Kjos struct binder_node *new_node;
5448777783e0SGreg Kroah-Hartman kuid_t curr_euid = current_euid();
5449777783e0SGreg Kroah-Hartman
5450c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
5451342e5c90SMartijn Coenen if (context->binder_context_mgr_node) {
5452777783e0SGreg Kroah-Hartman pr_err("BINDER_SET_CONTEXT_MGR already set\n");
5453777783e0SGreg Kroah-Hartman ret = -EBUSY;
5454777783e0SGreg Kroah-Hartman goto out;
5455777783e0SGreg Kroah-Hartman }
545652f88693STodd Kjos ret = security_binder_set_context_mgr(proc->cred);
545779af7307SStephen Smalley if (ret < 0)
545879af7307SStephen Smalley goto out;
5459342e5c90SMartijn Coenen if (uid_valid(context->binder_context_mgr_uid)) {
5460342e5c90SMartijn Coenen if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
5461777783e0SGreg Kroah-Hartman pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
5462777783e0SGreg Kroah-Hartman from_kuid(&init_user_ns, curr_euid),
5463777783e0SGreg Kroah-Hartman from_kuid(&init_user_ns,
5464342e5c90SMartijn Coenen context->binder_context_mgr_uid));
5465777783e0SGreg Kroah-Hartman ret = -EPERM;
5466777783e0SGreg Kroah-Hartman goto out;
5467777783e0SGreg Kroah-Hartman }
5468777783e0SGreg Kroah-Hartman } else {
5469342e5c90SMartijn Coenen context->binder_context_mgr_uid = curr_euid;
5470777783e0SGreg Kroah-Hartman }
5471ec74136dSTodd Kjos new_node = binder_new_node(proc, fbo);
5472c44b1231STodd Kjos if (!new_node) {
5473777783e0SGreg Kroah-Hartman ret = -ENOMEM;
5474777783e0SGreg Kroah-Hartman goto out;
5475777783e0SGreg Kroah-Hartman }
5476673068eeSTodd Kjos binder_node_lock(new_node);
5477c44b1231STodd Kjos new_node->local_weak_refs++;
5478c44b1231STodd Kjos new_node->local_strong_refs++;
5479c44b1231STodd Kjos new_node->has_strong_ref = 1;
5480c44b1231STodd Kjos new_node->has_weak_ref = 1;
5481c44b1231STodd Kjos context->binder_context_mgr_node = new_node;
5482673068eeSTodd Kjos binder_node_unlock(new_node);
5483adc18842STodd Kjos binder_put_node(new_node);
5484777783e0SGreg Kroah-Hartman out:
5485c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
5486777783e0SGreg Kroah-Hartman return ret;
5487777783e0SGreg Kroah-Hartman }
5488777783e0SGreg Kroah-Hartman
binder_ioctl_get_node_info_for_ref(struct binder_proc * proc,struct binder_node_info_for_ref * info)5489b7e6a896SMartijn Coenen static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
5490b7e6a896SMartijn Coenen struct binder_node_info_for_ref *info)
5491b7e6a896SMartijn Coenen {
5492b7e6a896SMartijn Coenen struct binder_node *node;
5493b7e6a896SMartijn Coenen struct binder_context *context = proc->context;
5494b7e6a896SMartijn Coenen __u32 handle = info->handle;
5495b7e6a896SMartijn Coenen
5496b7e6a896SMartijn Coenen if (info->strong_count || info->weak_count || info->reserved1 ||
5497b7e6a896SMartijn Coenen info->reserved2 || info->reserved3) {
5498b7e6a896SMartijn Coenen binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
5499b7e6a896SMartijn Coenen proc->pid);
5500b7e6a896SMartijn Coenen return -EINVAL;
5501b7e6a896SMartijn Coenen }
5502b7e6a896SMartijn Coenen
5503b7e6a896SMartijn Coenen /* This ioctl may only be used by the context manager */
5504b7e6a896SMartijn Coenen mutex_lock(&context->context_mgr_node_lock);
5505b7e6a896SMartijn Coenen if (!context->binder_context_mgr_node ||
5506b7e6a896SMartijn Coenen context->binder_context_mgr_node->proc != proc) {
5507b7e6a896SMartijn Coenen mutex_unlock(&context->context_mgr_node_lock);
5508b7e6a896SMartijn Coenen return -EPERM;
5509b7e6a896SMartijn Coenen }
5510b7e6a896SMartijn Coenen mutex_unlock(&context->context_mgr_node_lock);
5511b7e6a896SMartijn Coenen
5512b7e6a896SMartijn Coenen node = binder_get_node_from_ref(proc, handle, true, NULL);
5513b7e6a896SMartijn Coenen if (!node)
5514b7e6a896SMartijn Coenen return -EINVAL;
5515b7e6a896SMartijn Coenen
5516b7e6a896SMartijn Coenen info->strong_count = node->local_strong_refs +
5517b7e6a896SMartijn Coenen node->internal_strong_refs;
5518b7e6a896SMartijn Coenen info->weak_count = node->local_weak_refs;
5519b7e6a896SMartijn Coenen
5520b7e6a896SMartijn Coenen binder_put_node(node);
5521b7e6a896SMartijn Coenen
5522b7e6a896SMartijn Coenen return 0;
5523b7e6a896SMartijn Coenen }
5524b7e6a896SMartijn Coenen
binder_ioctl_get_node_debug_info(struct binder_proc * proc,struct binder_node_debug_info * info)5525abcc6153SColin Cross static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
5526abcc6153SColin Cross struct binder_node_debug_info *info)
5527abcc6153SColin Cross {
5528abcc6153SColin Cross struct rb_node *n;
5529abcc6153SColin Cross binder_uintptr_t ptr = info->ptr;
5530abcc6153SColin Cross
5531abcc6153SColin Cross memset(info, 0, sizeof(*info));
5532abcc6153SColin Cross
5533abcc6153SColin Cross binder_inner_proc_lock(proc);
5534abcc6153SColin Cross for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5535abcc6153SColin Cross struct binder_node *node = rb_entry(n, struct binder_node,
5536abcc6153SColin Cross rb_node);
5537abcc6153SColin Cross if (node->ptr > ptr) {
5538abcc6153SColin Cross info->ptr = node->ptr;
5539abcc6153SColin Cross info->cookie = node->cookie;
5540abcc6153SColin Cross info->has_strong_ref = node->has_strong_ref;
5541abcc6153SColin Cross info->has_weak_ref = node->has_weak_ref;
5542abcc6153SColin Cross break;
5543abcc6153SColin Cross }
5544abcc6153SColin Cross }
5545abcc6153SColin Cross binder_inner_proc_unlock(proc);
5546abcc6153SColin Cross
5547abcc6153SColin Cross return 0;
5548abcc6153SColin Cross }
5549abcc6153SColin Cross
binder_txns_pending_ilocked(struct binder_proc * proc)5550b564171aSLi Li static bool binder_txns_pending_ilocked(struct binder_proc *proc)
5551b564171aSLi Li {
5552b564171aSLi Li struct rb_node *n;
5553b564171aSLi Li struct binder_thread *thread;
5554b564171aSLi Li
5555b564171aSLi Li if (proc->outstanding_txns > 0)
5556b564171aSLi Li return true;
5557b564171aSLi Li
5558b564171aSLi Li for (n = rb_first(&proc->threads); n; n = rb_next(n)) {
5559b564171aSLi Li thread = rb_entry(n, struct binder_thread, rb_node);
5560b564171aSLi Li if (thread->transaction_stack)
5561b564171aSLi Li return true;
5562b564171aSLi Li }
5563b564171aSLi Li return false;
5564b564171aSLi Li }
5565b564171aSLi Li
binder_add_freeze_work(struct binder_proc * proc,bool is_frozen)5566d579b04aSYu-Ting Tseng static void binder_add_freeze_work(struct binder_proc *proc, bool is_frozen)
5567d579b04aSYu-Ting Tseng {
5568dc8aea47SCarlos Llamas struct binder_node *prev = NULL;
5569d579b04aSYu-Ting Tseng struct rb_node *n;
5570d579b04aSYu-Ting Tseng struct binder_ref *ref;
5571d579b04aSYu-Ting Tseng
5572d579b04aSYu-Ting Tseng binder_inner_proc_lock(proc);
5573d579b04aSYu-Ting Tseng for (n = rb_first(&proc->nodes); n; n = rb_next(n)) {
5574d579b04aSYu-Ting Tseng struct binder_node *node;
5575d579b04aSYu-Ting Tseng
5576d579b04aSYu-Ting Tseng node = rb_entry(n, struct binder_node, rb_node);
5577dc8aea47SCarlos Llamas binder_inc_node_tmpref_ilocked(node);
5578d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
5579dc8aea47SCarlos Llamas if (prev)
5580dc8aea47SCarlos Llamas binder_put_node(prev);
5581d579b04aSYu-Ting Tseng binder_node_lock(node);
5582d579b04aSYu-Ting Tseng hlist_for_each_entry(ref, &node->refs, node_entry) {
5583d579b04aSYu-Ting Tseng /*
5584d579b04aSYu-Ting Tseng * Need the node lock to synchronize
5585d579b04aSYu-Ting Tseng * with new notification requests and the
5586d579b04aSYu-Ting Tseng * inner lock to synchronize with queued
5587d579b04aSYu-Ting Tseng * freeze notifications.
5588d579b04aSYu-Ting Tseng */
5589d579b04aSYu-Ting Tseng binder_inner_proc_lock(ref->proc);
5590d579b04aSYu-Ting Tseng if (!ref->freeze) {
5591d579b04aSYu-Ting Tseng binder_inner_proc_unlock(ref->proc);
5592d579b04aSYu-Ting Tseng continue;
5593d579b04aSYu-Ting Tseng }
5594d579b04aSYu-Ting Tseng ref->freeze->work.type = BINDER_WORK_FROZEN_BINDER;
5595d579b04aSYu-Ting Tseng if (list_empty(&ref->freeze->work.entry)) {
5596d579b04aSYu-Ting Tseng ref->freeze->is_frozen = is_frozen;
5597d579b04aSYu-Ting Tseng binder_enqueue_work_ilocked(&ref->freeze->work, &ref->proc->todo);
5598d579b04aSYu-Ting Tseng binder_wakeup_proc_ilocked(ref->proc);
5599d579b04aSYu-Ting Tseng } else {
5600d579b04aSYu-Ting Tseng if (ref->freeze->sent && ref->freeze->is_frozen != is_frozen)
5601d579b04aSYu-Ting Tseng ref->freeze->resend = true;
5602d579b04aSYu-Ting Tseng ref->freeze->is_frozen = is_frozen;
5603d579b04aSYu-Ting Tseng }
5604d579b04aSYu-Ting Tseng binder_inner_proc_unlock(ref->proc);
5605d579b04aSYu-Ting Tseng }
5606dc8aea47SCarlos Llamas prev = node;
5607d579b04aSYu-Ting Tseng binder_node_unlock(node);
5608d579b04aSYu-Ting Tseng binder_inner_proc_lock(proc);
5609011e69a1SCarlos Llamas if (proc->is_dead)
5610011e69a1SCarlos Llamas break;
5611d579b04aSYu-Ting Tseng }
5612d579b04aSYu-Ting Tseng binder_inner_proc_unlock(proc);
5613dc8aea47SCarlos Llamas if (prev)
5614dc8aea47SCarlos Llamas binder_put_node(prev);
5615d579b04aSYu-Ting Tseng }
5616d579b04aSYu-Ting Tseng
binder_ioctl_freeze(struct binder_freeze_info * info,struct binder_proc * target_proc)5617432ff1e9SMarco Ballesio static int binder_ioctl_freeze(struct binder_freeze_info *info,
5618432ff1e9SMarco Ballesio struct binder_proc *target_proc)
5619432ff1e9SMarco Ballesio {
5620432ff1e9SMarco Ballesio int ret = 0;
5621432ff1e9SMarco Ballesio
5622432ff1e9SMarco Ballesio if (!info->enable) {
5623432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5624ae28c1beSMarco Ballesio target_proc->sync_recv = false;
5625ae28c1beSMarco Ballesio target_proc->async_recv = false;
5626432ff1e9SMarco Ballesio target_proc->is_frozen = false;
5627432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5628d579b04aSYu-Ting Tseng binder_add_freeze_work(target_proc, false);
5629432ff1e9SMarco Ballesio return 0;
5630432ff1e9SMarco Ballesio }
5631432ff1e9SMarco Ballesio
5632432ff1e9SMarco Ballesio /*
5633432ff1e9SMarco Ballesio * Freezing the target. Prevent new transactions by
5634432ff1e9SMarco Ballesio * setting frozen state. If timeout specified, wait
5635432ff1e9SMarco Ballesio * for transactions to drain.
5636432ff1e9SMarco Ballesio */
5637432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5638ae28c1beSMarco Ballesio target_proc->sync_recv = false;
5639ae28c1beSMarco Ballesio target_proc->async_recv = false;
5640432ff1e9SMarco Ballesio target_proc->is_frozen = true;
5641432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5642432ff1e9SMarco Ballesio
5643432ff1e9SMarco Ballesio if (info->timeout_ms > 0)
5644432ff1e9SMarco Ballesio ret = wait_event_interruptible_timeout(
5645432ff1e9SMarco Ballesio target_proc->freeze_wait,
5646432ff1e9SMarco Ballesio (!target_proc->outstanding_txns),
5647432ff1e9SMarco Ballesio msecs_to_jiffies(info->timeout_ms));
5648432ff1e9SMarco Ballesio
5649b564171aSLi Li /* Check pending transactions that wait for reply */
5650b564171aSLi Li if (ret >= 0) {
5651b564171aSLi Li binder_inner_proc_lock(target_proc);
5652b564171aSLi Li if (binder_txns_pending_ilocked(target_proc))
5653432ff1e9SMarco Ballesio ret = -EAGAIN;
5654b564171aSLi Li binder_inner_proc_unlock(target_proc);
5655b564171aSLi Li }
5656432ff1e9SMarco Ballesio
5657432ff1e9SMarco Ballesio if (ret < 0) {
5658432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5659432ff1e9SMarco Ballesio target_proc->is_frozen = false;
5660432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5661d579b04aSYu-Ting Tseng } else {
5662d579b04aSYu-Ting Tseng binder_add_freeze_work(target_proc, true);
5663432ff1e9SMarco Ballesio }
5664432ff1e9SMarco Ballesio
5665432ff1e9SMarco Ballesio return ret;
5666432ff1e9SMarco Ballesio }
5667432ff1e9SMarco Ballesio
binder_ioctl_get_freezer_info(struct binder_frozen_status_info * info)5668ae28c1beSMarco Ballesio static int binder_ioctl_get_freezer_info(
5669ae28c1beSMarco Ballesio struct binder_frozen_status_info *info)
5670ae28c1beSMarco Ballesio {
5671ae28c1beSMarco Ballesio struct binder_proc *target_proc;
5672ae28c1beSMarco Ballesio bool found = false;
5673b564171aSLi Li __u32 txns_pending;
5674ae28c1beSMarco Ballesio
5675ae28c1beSMarco Ballesio info->sync_recv = 0;
5676ae28c1beSMarco Ballesio info->async_recv = 0;
5677ae28c1beSMarco Ballesio
5678ae28c1beSMarco Ballesio mutex_lock(&binder_procs_lock);
5679ae28c1beSMarco Ballesio hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5680ae28c1beSMarco Ballesio if (target_proc->pid == info->pid) {
5681ae28c1beSMarco Ballesio found = true;
5682ae28c1beSMarco Ballesio binder_inner_proc_lock(target_proc);
5683b564171aSLi Li txns_pending = binder_txns_pending_ilocked(target_proc);
5684b564171aSLi Li info->sync_recv |= target_proc->sync_recv |
5685b564171aSLi Li (txns_pending << 1);
5686ae28c1beSMarco Ballesio info->async_recv |= target_proc->async_recv;
5687ae28c1beSMarco Ballesio binder_inner_proc_unlock(target_proc);
5688ae28c1beSMarco Ballesio }
5689ae28c1beSMarco Ballesio }
5690ae28c1beSMarco Ballesio mutex_unlock(&binder_procs_lock);
5691ae28c1beSMarco Ballesio
5692ae28c1beSMarco Ballesio if (!found)
5693ae28c1beSMarco Ballesio return -EINVAL;
5694ae28c1beSMarco Ballesio
5695ae28c1beSMarco Ballesio return 0;
5696ae28c1beSMarco Ballesio }
5697ae28c1beSMarco Ballesio
binder_ioctl_get_extended_error(struct binder_thread * thread,void __user * ubuf)5698bd32889eSCarlos Llamas static int binder_ioctl_get_extended_error(struct binder_thread *thread,
5699bd32889eSCarlos Llamas void __user *ubuf)
5700bd32889eSCarlos Llamas {
5701aed86f8aSSchspa Shi struct binder_extended_error ee;
5702bd32889eSCarlos Llamas
5703bd32889eSCarlos Llamas binder_inner_proc_lock(thread->proc);
5704aed86f8aSSchspa Shi ee = thread->ee;
5705aed86f8aSSchspa Shi binder_set_extended_error(&thread->ee, 0, BR_OK, 0);
5706bd32889eSCarlos Llamas binder_inner_proc_unlock(thread->proc);
5707bd32889eSCarlos Llamas
5708aed86f8aSSchspa Shi if (copy_to_user(ubuf, &ee, sizeof(ee)))
5709aed86f8aSSchspa Shi return -EFAULT;
5710bd32889eSCarlos Llamas
5711bd32889eSCarlos Llamas return 0;
5712bd32889eSCarlos Llamas }
5713bd32889eSCarlos Llamas
binder_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)5714777783e0SGreg Kroah-Hartman static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5715777783e0SGreg Kroah-Hartman {
5716777783e0SGreg Kroah-Hartman int ret;
5717777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
5718777783e0SGreg Kroah-Hartman struct binder_thread *thread;
5719777783e0SGreg Kroah-Hartman void __user *ubuf = (void __user *)arg;
5720777783e0SGreg Kroah-Hartman
5721777783e0SGreg Kroah-Hartman /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5722777783e0SGreg Kroah-Hartman proc->pid, current->pid, cmd, arg);*/
5723777783e0SGreg Kroah-Hartman
57244175e2b4SSherry Yang binder_selftest_alloc(&proc->alloc);
57254175e2b4SSherry Yang
5726777783e0SGreg Kroah-Hartman trace_binder_ioctl(cmd, arg);
5727777783e0SGreg Kroah-Hartman
5728777783e0SGreg Kroah-Hartman ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5729777783e0SGreg Kroah-Hartman if (ret)
5730777783e0SGreg Kroah-Hartman goto err_unlocked;
5731777783e0SGreg Kroah-Hartman
5732777783e0SGreg Kroah-Hartman thread = binder_get_thread(proc);
5733777783e0SGreg Kroah-Hartman if (thread == NULL) {
5734777783e0SGreg Kroah-Hartman ret = -ENOMEM;
5735777783e0SGreg Kroah-Hartman goto err;
5736777783e0SGreg Kroah-Hartman }
5737777783e0SGreg Kroah-Hartman
5738777783e0SGreg Kroah-Hartman switch (cmd) {
5739777783e0SGreg Kroah-Hartman case BINDER_WRITE_READ:
57407feb35bcSJiazi.Li ret = binder_ioctl_write_read(filp, arg, thread);
5741777783e0SGreg Kroah-Hartman if (ret)
5742777783e0SGreg Kroah-Hartman goto err;
5743777783e0SGreg Kroah-Hartman break;
5744b3e68612STodd Kjos case BINDER_SET_MAX_THREADS: {
574542316941SCarlos Llamas u32 max_threads;
5746b3e68612STodd Kjos
5747b3e68612STodd Kjos if (copy_from_user(&max_threads, ubuf,
5748b3e68612STodd Kjos sizeof(max_threads))) {
5749777783e0SGreg Kroah-Hartman ret = -EINVAL;
5750777783e0SGreg Kroah-Hartman goto err;
5751777783e0SGreg Kroah-Hartman }
5752b3e68612STodd Kjos binder_inner_proc_lock(proc);
5753b3e68612STodd Kjos proc->max_threads = max_threads;
5754b3e68612STodd Kjos binder_inner_proc_unlock(proc);
5755777783e0SGreg Kroah-Hartman break;
5756b3e68612STodd Kjos }
5757ec74136dSTodd Kjos case BINDER_SET_CONTEXT_MGR_EXT: {
5758ec74136dSTodd Kjos struct flat_binder_object fbo;
5759ec74136dSTodd Kjos
5760ec74136dSTodd Kjos if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5761ec74136dSTodd Kjos ret = -EINVAL;
5762ec74136dSTodd Kjos goto err;
5763ec74136dSTodd Kjos }
5764ec74136dSTodd Kjos ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5765ec74136dSTodd Kjos if (ret)
5766ec74136dSTodd Kjos goto err;
5767ec74136dSTodd Kjos break;
5768ec74136dSTodd Kjos }
5769777783e0SGreg Kroah-Hartman case BINDER_SET_CONTEXT_MGR:
5770ec74136dSTodd Kjos ret = binder_ioctl_set_ctx_mgr(filp, NULL);
5771777783e0SGreg Kroah-Hartman if (ret)
5772777783e0SGreg Kroah-Hartman goto err;
5773777783e0SGreg Kroah-Hartman break;
5774777783e0SGreg Kroah-Hartman case BINDER_THREAD_EXIT:
5775777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
5776777783e0SGreg Kroah-Hartman proc->pid, thread->pid);
57777a4408c6STodd Kjos binder_thread_release(proc, thread);
5778777783e0SGreg Kroah-Hartman thread = NULL;
5779777783e0SGreg Kroah-Hartman break;
5780777783e0SGreg Kroah-Hartman case BINDER_VERSION: {
5781777783e0SGreg Kroah-Hartman struct binder_version __user *ver = ubuf;
5782777783e0SGreg Kroah-Hartman
5783777783e0SGreg Kroah-Hartman if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5784777783e0SGreg Kroah-Hartman &ver->protocol_version)) {
5785777783e0SGreg Kroah-Hartman ret = -EINVAL;
5786777783e0SGreg Kroah-Hartman goto err;
5787777783e0SGreg Kroah-Hartman }
5788777783e0SGreg Kroah-Hartman break;
5789777783e0SGreg Kroah-Hartman }
5790b7e6a896SMartijn Coenen case BINDER_GET_NODE_INFO_FOR_REF: {
5791b7e6a896SMartijn Coenen struct binder_node_info_for_ref info;
5792b7e6a896SMartijn Coenen
5793b7e6a896SMartijn Coenen if (copy_from_user(&info, ubuf, sizeof(info))) {
5794b7e6a896SMartijn Coenen ret = -EFAULT;
5795b7e6a896SMartijn Coenen goto err;
5796b7e6a896SMartijn Coenen }
5797b7e6a896SMartijn Coenen
5798b7e6a896SMartijn Coenen ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5799b7e6a896SMartijn Coenen if (ret < 0)
5800b7e6a896SMartijn Coenen goto err;
5801b7e6a896SMartijn Coenen
5802b7e6a896SMartijn Coenen if (copy_to_user(ubuf, &info, sizeof(info))) {
5803b7e6a896SMartijn Coenen ret = -EFAULT;
5804b7e6a896SMartijn Coenen goto err;
5805b7e6a896SMartijn Coenen }
5806b7e6a896SMartijn Coenen
5807b7e6a896SMartijn Coenen break;
5808b7e6a896SMartijn Coenen }
5809abcc6153SColin Cross case BINDER_GET_NODE_DEBUG_INFO: {
5810abcc6153SColin Cross struct binder_node_debug_info info;
5811abcc6153SColin Cross
5812abcc6153SColin Cross if (copy_from_user(&info, ubuf, sizeof(info))) {
5813abcc6153SColin Cross ret = -EFAULT;
5814abcc6153SColin Cross goto err;
5815abcc6153SColin Cross }
5816abcc6153SColin Cross
5817abcc6153SColin Cross ret = binder_ioctl_get_node_debug_info(proc, &info);
5818abcc6153SColin Cross if (ret < 0)
5819abcc6153SColin Cross goto err;
5820abcc6153SColin Cross
5821abcc6153SColin Cross if (copy_to_user(ubuf, &info, sizeof(info))) {
5822abcc6153SColin Cross ret = -EFAULT;
5823abcc6153SColin Cross goto err;
5824abcc6153SColin Cross }
5825abcc6153SColin Cross break;
5826abcc6153SColin Cross }
5827432ff1e9SMarco Ballesio case BINDER_FREEZE: {
5828432ff1e9SMarco Ballesio struct binder_freeze_info info;
5829432ff1e9SMarco Ballesio struct binder_proc **target_procs = NULL, *target_proc;
5830432ff1e9SMarco Ballesio int target_procs_count = 0, i = 0;
5831432ff1e9SMarco Ballesio
5832432ff1e9SMarco Ballesio ret = 0;
5833432ff1e9SMarco Ballesio
5834432ff1e9SMarco Ballesio if (copy_from_user(&info, ubuf, sizeof(info))) {
5835432ff1e9SMarco Ballesio ret = -EFAULT;
5836432ff1e9SMarco Ballesio goto err;
5837432ff1e9SMarco Ballesio }
5838432ff1e9SMarco Ballesio
5839432ff1e9SMarco Ballesio mutex_lock(&binder_procs_lock);
5840432ff1e9SMarco Ballesio hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5841432ff1e9SMarco Ballesio if (target_proc->pid == info.pid)
5842432ff1e9SMarco Ballesio target_procs_count++;
5843432ff1e9SMarco Ballesio }
5844432ff1e9SMarco Ballesio
5845432ff1e9SMarco Ballesio if (target_procs_count == 0) {
5846432ff1e9SMarco Ballesio mutex_unlock(&binder_procs_lock);
5847432ff1e9SMarco Ballesio ret = -EINVAL;
5848432ff1e9SMarco Ballesio goto err;
5849432ff1e9SMarco Ballesio }
5850432ff1e9SMarco Ballesio
5851432ff1e9SMarco Ballesio target_procs = kcalloc(target_procs_count,
5852432ff1e9SMarco Ballesio sizeof(struct binder_proc *),
5853432ff1e9SMarco Ballesio GFP_KERNEL);
5854432ff1e9SMarco Ballesio
5855432ff1e9SMarco Ballesio if (!target_procs) {
5856432ff1e9SMarco Ballesio mutex_unlock(&binder_procs_lock);
5857432ff1e9SMarco Ballesio ret = -ENOMEM;
5858432ff1e9SMarco Ballesio goto err;
5859432ff1e9SMarco Ballesio }
5860432ff1e9SMarco Ballesio
5861432ff1e9SMarco Ballesio hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5862432ff1e9SMarco Ballesio if (target_proc->pid != info.pid)
5863432ff1e9SMarco Ballesio continue;
5864432ff1e9SMarco Ballesio
5865432ff1e9SMarco Ballesio binder_inner_proc_lock(target_proc);
5866432ff1e9SMarco Ballesio target_proc->tmp_ref++;
5867432ff1e9SMarco Ballesio binder_inner_proc_unlock(target_proc);
5868432ff1e9SMarco Ballesio
5869432ff1e9SMarco Ballesio target_procs[i++] = target_proc;
5870432ff1e9SMarco Ballesio }
5871432ff1e9SMarco Ballesio mutex_unlock(&binder_procs_lock);
5872432ff1e9SMarco Ballesio
5873432ff1e9SMarco Ballesio for (i = 0; i < target_procs_count; i++) {
5874432ff1e9SMarco Ballesio if (ret >= 0)
5875432ff1e9SMarco Ballesio ret = binder_ioctl_freeze(&info,
5876432ff1e9SMarco Ballesio target_procs[i]);
5877432ff1e9SMarco Ballesio
5878432ff1e9SMarco Ballesio binder_proc_dec_tmpref(target_procs[i]);
5879432ff1e9SMarco Ballesio }
5880432ff1e9SMarco Ballesio
5881432ff1e9SMarco Ballesio kfree(target_procs);
5882432ff1e9SMarco Ballesio
5883432ff1e9SMarco Ballesio if (ret < 0)
5884432ff1e9SMarco Ballesio goto err;
5885432ff1e9SMarco Ballesio break;
5886432ff1e9SMarco Ballesio }
5887ae28c1beSMarco Ballesio case BINDER_GET_FROZEN_INFO: {
5888ae28c1beSMarco Ballesio struct binder_frozen_status_info info;
5889ae28c1beSMarco Ballesio
5890ae28c1beSMarco Ballesio if (copy_from_user(&info, ubuf, sizeof(info))) {
5891ae28c1beSMarco Ballesio ret = -EFAULT;
5892ae28c1beSMarco Ballesio goto err;
5893ae28c1beSMarco Ballesio }
5894ae28c1beSMarco Ballesio
5895ae28c1beSMarco Ballesio ret = binder_ioctl_get_freezer_info(&info);
5896ae28c1beSMarco Ballesio if (ret < 0)
5897ae28c1beSMarco Ballesio goto err;
5898ae28c1beSMarco Ballesio
5899ae28c1beSMarco Ballesio if (copy_to_user(ubuf, &info, sizeof(info))) {
5900ae28c1beSMarco Ballesio ret = -EFAULT;
5901ae28c1beSMarco Ballesio goto err;
5902ae28c1beSMarco Ballesio }
5903ae28c1beSMarco Ballesio break;
5904ae28c1beSMarco Ballesio }
5905a7dc1e6fSHang Lu case BINDER_ENABLE_ONEWAY_SPAM_DETECTION: {
5906a7dc1e6fSHang Lu uint32_t enable;
5907a7dc1e6fSHang Lu
5908a7dc1e6fSHang Lu if (copy_from_user(&enable, ubuf, sizeof(enable))) {
5909ced081a4SLuca Stefani ret = -EFAULT;
5910a7dc1e6fSHang Lu goto err;
5911a7dc1e6fSHang Lu }
5912a7dc1e6fSHang Lu binder_inner_proc_lock(proc);
5913a7dc1e6fSHang Lu proc->oneway_spam_detection_enabled = (bool)enable;
5914a7dc1e6fSHang Lu binder_inner_proc_unlock(proc);
5915a7dc1e6fSHang Lu break;
5916a7dc1e6fSHang Lu }
5917bd32889eSCarlos Llamas case BINDER_GET_EXTENDED_ERROR:
5918bd32889eSCarlos Llamas ret = binder_ioctl_get_extended_error(thread, ubuf);
5919bd32889eSCarlos Llamas if (ret < 0)
5920bd32889eSCarlos Llamas goto err;
5921bd32889eSCarlos Llamas break;
5922777783e0SGreg Kroah-Hartman default:
5923777783e0SGreg Kroah-Hartman ret = -EINVAL;
5924777783e0SGreg Kroah-Hartman goto err;
5925777783e0SGreg Kroah-Hartman }
5926777783e0SGreg Kroah-Hartman ret = 0;
5927777783e0SGreg Kroah-Hartman err:
5928777783e0SGreg Kroah-Hartman if (thread)
592908dabceeSTodd Kjos thread->looper_need_return = false;
5930777783e0SGreg Kroah-Hartman wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
593195c16f9dSMarco Ballesio if (ret && ret != -EINTR)
5932777783e0SGreg Kroah-Hartman pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
5933777783e0SGreg Kroah-Hartman err_unlocked:
5934777783e0SGreg Kroah-Hartman trace_binder_ioctl_done(ret);
5935777783e0SGreg Kroah-Hartman return ret;
5936777783e0SGreg Kroah-Hartman }
5937777783e0SGreg Kroah-Hartman
binder_vma_open(struct vm_area_struct * vma)5938777783e0SGreg Kroah-Hartman static void binder_vma_open(struct vm_area_struct *vma)
5939777783e0SGreg Kroah-Hartman {
5940777783e0SGreg Kroah-Hartman struct binder_proc *proc = vma->vm_private_data;
5941777783e0SGreg Kroah-Hartman
5942777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5943777783e0SGreg Kroah-Hartman "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5944777783e0SGreg Kroah-Hartman proc->pid, vma->vm_start, vma->vm_end,
5945777783e0SGreg Kroah-Hartman (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5946777783e0SGreg Kroah-Hartman (unsigned long)pgprot_val(vma->vm_page_prot));
5947777783e0SGreg Kroah-Hartman }
5948777783e0SGreg Kroah-Hartman
binder_vma_close(struct vm_area_struct * vma)5949777783e0SGreg Kroah-Hartman static void binder_vma_close(struct vm_area_struct *vma)
5950777783e0SGreg Kroah-Hartman {
5951777783e0SGreg Kroah-Hartman struct binder_proc *proc = vma->vm_private_data;
5952777783e0SGreg Kroah-Hartman
5953777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5954777783e0SGreg Kroah-Hartman "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
5955777783e0SGreg Kroah-Hartman proc->pid, vma->vm_start, vma->vm_end,
5956777783e0SGreg Kroah-Hartman (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5957777783e0SGreg Kroah-Hartman (unsigned long)pgprot_val(vma->vm_page_prot));
595819c98724STodd Kjos binder_alloc_vma_close(&proc->alloc);
5959777783e0SGreg Kroah-Hartman }
5960777783e0SGreg Kroah-Hartman
binder_vm_fault(struct vm_fault * vmf)5961e19f70aaSSouptick Joarder static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
5962777783e0SGreg Kroah-Hartman {
5963777783e0SGreg Kroah-Hartman return VM_FAULT_SIGBUS;
5964777783e0SGreg Kroah-Hartman }
5965777783e0SGreg Kroah-Hartman
59667cbea8dcSKirill A. Shutemov static const struct vm_operations_struct binder_vm_ops = {
5967777783e0SGreg Kroah-Hartman .open = binder_vma_open,
5968777783e0SGreg Kroah-Hartman .close = binder_vma_close,
5969777783e0SGreg Kroah-Hartman .fault = binder_vm_fault,
5970777783e0SGreg Kroah-Hartman };
5971777783e0SGreg Kroah-Hartman
binder_mmap(struct file * filp,struct vm_area_struct * vma)597219c98724STodd Kjos static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
597319c98724STodd Kjos {
597419c98724STodd Kjos struct binder_proc *proc = filp->private_data;
597519c98724STodd Kjos
597619c98724STodd Kjos if (proc->tsk != current->group_leader)
597719c98724STodd Kjos return -EINVAL;
597819c98724STodd Kjos
597919c98724STodd Kjos binder_debug(BINDER_DEBUG_OPEN_CLOSE,
598019c98724STodd Kjos "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
598119c98724STodd Kjos __func__, proc->pid, vma->vm_start, vma->vm_end,
598219c98724STodd Kjos (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
598319c98724STodd Kjos (unsigned long)pgprot_val(vma->vm_page_prot));
598419c98724STodd Kjos
598519c98724STodd Kjos if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
59862a3809daSLiu Shixin pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
59872a3809daSLiu Shixin proc->pid, vma->vm_start, vma->vm_end, "bad vm_flags", -EPERM);
59882a3809daSLiu Shixin return -EPERM;
598919c98724STodd Kjos }
59901c71222eSSuren Baghdasaryan vm_flags_mod(vma, VM_DONTCOPY | VM_MIXEDMAP, VM_MAYWRITE);
5991720c2419SMinchan Kim
599219c98724STodd Kjos vma->vm_ops = &binder_vm_ops;
599319c98724STodd Kjos vma->vm_private_data = proc;
599419c98724STodd Kjos
59952a3809daSLiu Shixin return binder_alloc_mmap_handler(&proc->alloc, vma);
5996777783e0SGreg Kroah-Hartman }
5997777783e0SGreg Kroah-Hartman
binder_open(struct inode * nodp,struct file * filp)5998777783e0SGreg Kroah-Hartman static int binder_open(struct inode *nodp, struct file *filp)
5999777783e0SGreg Kroah-Hartman {
6000eb143f87SMartin Fuzzey struct binder_proc *proc, *itr;
6001ac4812c5SMartijn Coenen struct binder_device *binder_dev;
60024feb80faSHridya Valsaraju struct binderfs_info *info;
60034feb80faSHridya Valsaraju struct dentry *binder_binderfs_dir_entry_proc = NULL;
6004eb143f87SMartin Fuzzey bool existing_pid = false;
6005777783e0SGreg Kroah-Hartman
600600c41cddSElad Wexler binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
6007777783e0SGreg Kroah-Hartman current->group_leader->pid, current->pid);
6008777783e0SGreg Kroah-Hartman
6009777783e0SGreg Kroah-Hartman proc = kzalloc(sizeof(*proc), GFP_KERNEL);
6010777783e0SGreg Kroah-Hartman if (proc == NULL)
6011777783e0SGreg Kroah-Hartman return -ENOMEM;
601215d9da3fSCarlos Llamas
601315d9da3fSCarlos Llamas dbitmap_init(&proc->dmap);
60149630fe88STodd Kjos spin_lock_init(&proc->inner_lock);
60159630fe88STodd Kjos spin_lock_init(&proc->outer_lock);
6016c4ea41baSTodd Kjos get_task_struct(current->group_leader);
6017c4ea41baSTodd Kjos proc->tsk = current->group_leader;
601829bc22acSTodd Kjos proc->cred = get_cred(filp->f_cred);
6019777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&proc->todo);
6020432ff1e9SMarco Ballesio init_waitqueue_head(&proc->freeze_wait);
6021777783e0SGreg Kroah-Hartman proc->default_priority = task_nice(current);
60223ad20fe3SChristian Brauner /* binderfs stashes devices in i_private */
60234feb80faSHridya Valsaraju if (is_binderfs_device(nodp)) {
6024f0fe2c0fSChristian Brauner binder_dev = nodp->i_private;
60254feb80faSHridya Valsaraju info = nodp->i_sb->s_fs_info;
60264feb80faSHridya Valsaraju binder_binderfs_dir_entry_proc = info->proc_log_dir;
60274feb80faSHridya Valsaraju } else {
60283ad20fe3SChristian Brauner binder_dev = container_of(filp->private_data,
60293ad20fe3SChristian Brauner struct binder_device, miscdev);
60304feb80faSHridya Valsaraju }
6031f0fe2c0fSChristian Brauner refcount_inc(&binder_dev->ref);
6032ac4812c5SMartijn Coenen proc->context = &binder_dev->context;
603319c98724STodd Kjos binder_alloc_init(&proc->alloc);
6034777783e0SGreg Kroah-Hartman
6035777783e0SGreg Kroah-Hartman binder_stats_created(BINDER_STAT_PROC);
6036777783e0SGreg Kroah-Hartman proc->pid = current->group_leader->pid;
6037777783e0SGreg Kroah-Hartman INIT_LIST_HEAD(&proc->delivered_death);
6038d579b04aSYu-Ting Tseng INIT_LIST_HEAD(&proc->delivered_freeze);
60391b77e9dcSMartijn Coenen INIT_LIST_HEAD(&proc->waiting_threads);
6040777783e0SGreg Kroah-Hartman filp->private_data = proc;
6041777783e0SGreg Kroah-Hartman
6042c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6043eb143f87SMartin Fuzzey hlist_for_each_entry(itr, &binder_procs, proc_node) {
6044eb143f87SMartin Fuzzey if (itr->pid == proc->pid) {
6045eb143f87SMartin Fuzzey existing_pid = true;
6046eb143f87SMartin Fuzzey break;
6047eb143f87SMartin Fuzzey }
6048eb143f87SMartin Fuzzey }
6049c44b1231STodd Kjos hlist_add_head(&proc->proc_node, &binder_procs);
6050c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6051c44b1231STodd Kjos
6052eb143f87SMartin Fuzzey if (binder_debugfs_dir_entry_proc && !existing_pid) {
6053777783e0SGreg Kroah-Hartman char strbuf[11];
6054777783e0SGreg Kroah-Hartman
6055777783e0SGreg Kroah-Hartman snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
605614db3181SMartijn Coenen /*
6057eb143f87SMartin Fuzzey * proc debug entries are shared between contexts.
6058eb143f87SMartin Fuzzey * Only create for the first PID to avoid debugfs log spamming
6059eb143f87SMartin Fuzzey * The printing code will anyway print all contexts for a given
6060eb143f87SMartin Fuzzey * PID so this is not a problem.
606114db3181SMartijn Coenen */
606221d02ddfSHarsh Shandilya proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
606314db3181SMartijn Coenen binder_debugfs_dir_entry_proc,
606414db3181SMartijn Coenen (void *)(unsigned long)proc->pid,
6065c13e0a52SYangtao Li &proc_fops);
6066777783e0SGreg Kroah-Hartman }
6067777783e0SGreg Kroah-Hartman
6068eb143f87SMartin Fuzzey if (binder_binderfs_dir_entry_proc && !existing_pid) {
60694feb80faSHridya Valsaraju char strbuf[11];
60704feb80faSHridya Valsaraju struct dentry *binderfs_entry;
60714feb80faSHridya Valsaraju
60724feb80faSHridya Valsaraju snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
60734feb80faSHridya Valsaraju /*
60744feb80faSHridya Valsaraju * Similar to debugfs, the process specific log file is shared
6075eb143f87SMartin Fuzzey * between contexts. Only create for the first PID.
6076eb143f87SMartin Fuzzey * This is ok since same as debugfs, the log file will contain
6077eb143f87SMartin Fuzzey * information on all contexts of a given PID.
60784feb80faSHridya Valsaraju */
60794feb80faSHridya Valsaraju binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
60804feb80faSHridya Valsaraju strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
60814feb80faSHridya Valsaraju if (!IS_ERR(binderfs_entry)) {
60824feb80faSHridya Valsaraju proc->binderfs_entry = binderfs_entry;
60834feb80faSHridya Valsaraju } else {
60844feb80faSHridya Valsaraju int error;
60854feb80faSHridya Valsaraju
60864feb80faSHridya Valsaraju error = PTR_ERR(binderfs_entry);
60874feb80faSHridya Valsaraju pr_warn("Unable to create file %s in binderfs (error %d)\n",
60884feb80faSHridya Valsaraju strbuf, error);
60894feb80faSHridya Valsaraju }
60904feb80faSHridya Valsaraju }
60914feb80faSHridya Valsaraju
6092777783e0SGreg Kroah-Hartman return 0;
6093777783e0SGreg Kroah-Hartman }
6094777783e0SGreg Kroah-Hartman
binder_flush(struct file * filp,fl_owner_t id)6095777783e0SGreg Kroah-Hartman static int binder_flush(struct file *filp, fl_owner_t id)
6096777783e0SGreg Kroah-Hartman {
6097777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
6098777783e0SGreg Kroah-Hartman
6099777783e0SGreg Kroah-Hartman binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
6100777783e0SGreg Kroah-Hartman
6101777783e0SGreg Kroah-Hartman return 0;
6102777783e0SGreg Kroah-Hartman }
6103777783e0SGreg Kroah-Hartman
binder_deferred_flush(struct binder_proc * proc)6104777783e0SGreg Kroah-Hartman static void binder_deferred_flush(struct binder_proc *proc)
6105777783e0SGreg Kroah-Hartman {
6106777783e0SGreg Kroah-Hartman struct rb_node *n;
6107777783e0SGreg Kroah-Hartman int wake_count = 0;
6108777783e0SGreg Kroah-Hartman
61097bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
6110777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
6111777783e0SGreg Kroah-Hartman struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
6112777783e0SGreg Kroah-Hartman
611308dabceeSTodd Kjos thread->looper_need_return = true;
6114777783e0SGreg Kroah-Hartman if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
6115777783e0SGreg Kroah-Hartman wake_up_interruptible(&thread->wait);
6116777783e0SGreg Kroah-Hartman wake_count++;
6117777783e0SGreg Kroah-Hartman }
6118777783e0SGreg Kroah-Hartman }
61197bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
6120777783e0SGreg Kroah-Hartman
6121777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
6122777783e0SGreg Kroah-Hartman "binder_flush: %d woke %d threads\n", proc->pid,
6123777783e0SGreg Kroah-Hartman wake_count);
6124777783e0SGreg Kroah-Hartman }
6125777783e0SGreg Kroah-Hartman
binder_release(struct inode * nodp,struct file * filp)6126777783e0SGreg Kroah-Hartman static int binder_release(struct inode *nodp, struct file *filp)
6127777783e0SGreg Kroah-Hartman {
6128777783e0SGreg Kroah-Hartman struct binder_proc *proc = filp->private_data;
6129777783e0SGreg Kroah-Hartman
6130777783e0SGreg Kroah-Hartman debugfs_remove(proc->debugfs_entry);
61314feb80faSHridya Valsaraju
61324feb80faSHridya Valsaraju if (proc->binderfs_entry) {
61334feb80faSHridya Valsaraju binderfs_remove_file(proc->binderfs_entry);
61344feb80faSHridya Valsaraju proc->binderfs_entry = NULL;
61354feb80faSHridya Valsaraju }
61364feb80faSHridya Valsaraju
6137777783e0SGreg Kroah-Hartman binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
6138777783e0SGreg Kroah-Hartman
6139777783e0SGreg Kroah-Hartman return 0;
6140777783e0SGreg Kroah-Hartman }
6141777783e0SGreg Kroah-Hartman
binder_node_release(struct binder_node * node,int refs)6142777783e0SGreg Kroah-Hartman static int binder_node_release(struct binder_node *node, int refs)
6143777783e0SGreg Kroah-Hartman {
6144777783e0SGreg Kroah-Hartman struct binder_ref *ref;
6145777783e0SGreg Kroah-Hartman int death = 0;
6146ed29721eSTodd Kjos struct binder_proc *proc = node->proc;
6147777783e0SGreg Kroah-Hartman
614872196393STodd Kjos binder_release_work(proc, &node->async_todo);
6149ed29721eSTodd Kjos
6150673068eeSTodd Kjos binder_node_lock(node);
6151ed29721eSTodd Kjos binder_inner_proc_lock(proc);
615272196393STodd Kjos binder_dequeue_work_ilocked(&node->work);
6153adc18842STodd Kjos /*
6154adc18842STodd Kjos * The caller must have taken a temporary ref on the node,
6155adc18842STodd Kjos */
6156adc18842STodd Kjos BUG_ON(!node->tmp_refs);
6157adc18842STodd Kjos if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
6158ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
6159673068eeSTodd Kjos binder_node_unlock(node);
6160ed29721eSTodd Kjos binder_free_node(node);
6161777783e0SGreg Kroah-Hartman
6162777783e0SGreg Kroah-Hartman return refs;
6163777783e0SGreg Kroah-Hartman }
6164777783e0SGreg Kroah-Hartman
6165777783e0SGreg Kroah-Hartman node->proc = NULL;
6166777783e0SGreg Kroah-Hartman node->local_strong_refs = 0;
6167777783e0SGreg Kroah-Hartman node->local_weak_refs = 0;
6168ed29721eSTodd Kjos binder_inner_proc_unlock(proc);
6169c44b1231STodd Kjos
6170c44b1231STodd Kjos spin_lock(&binder_dead_nodes_lock);
6171777783e0SGreg Kroah-Hartman hlist_add_head(&node->dead_node, &binder_dead_nodes);
6172c44b1231STodd Kjos spin_unlock(&binder_dead_nodes_lock);
6173777783e0SGreg Kroah-Hartman
6174777783e0SGreg Kroah-Hartman hlist_for_each_entry(ref, &node->refs, node_entry) {
6175777783e0SGreg Kroah-Hartman refs++;
6176ab51ec6bSMartijn Coenen /*
6177ab51ec6bSMartijn Coenen * Need the node lock to synchronize
6178ab51ec6bSMartijn Coenen * with new notification requests and the
6179ab51ec6bSMartijn Coenen * inner lock to synchronize with queued
6180ab51ec6bSMartijn Coenen * death notifications.
6181ab51ec6bSMartijn Coenen */
6182ab51ec6bSMartijn Coenen binder_inner_proc_lock(ref->proc);
6183ab51ec6bSMartijn Coenen if (!ref->death) {
6184ab51ec6bSMartijn Coenen binder_inner_proc_unlock(ref->proc);
6185777783e0SGreg Kroah-Hartman continue;
6186ab51ec6bSMartijn Coenen }
6187777783e0SGreg Kroah-Hartman
6188777783e0SGreg Kroah-Hartman death++;
6189777783e0SGreg Kroah-Hartman
6190ab51ec6bSMartijn Coenen BUG_ON(!list_empty(&ref->death->work.entry));
6191777783e0SGreg Kroah-Hartman ref->death->work.type = BINDER_WORK_DEAD_BINDER;
619272196393STodd Kjos binder_enqueue_work_ilocked(&ref->death->work,
6193777783e0SGreg Kroah-Hartman &ref->proc->todo);
6194408c68b1SMartijn Coenen binder_wakeup_proc_ilocked(ref->proc);
619572196393STodd Kjos binder_inner_proc_unlock(ref->proc);
6196777783e0SGreg Kroah-Hartman }
6197777783e0SGreg Kroah-Hartman
6198777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
6199777783e0SGreg Kroah-Hartman "node %d now dead, refs %d, death %d\n",
6200777783e0SGreg Kroah-Hartman node->debug_id, refs, death);
6201673068eeSTodd Kjos binder_node_unlock(node);
6202adc18842STodd Kjos binder_put_node(node);
6203777783e0SGreg Kroah-Hartman
6204777783e0SGreg Kroah-Hartman return refs;
6205777783e0SGreg Kroah-Hartman }
6206777783e0SGreg Kroah-Hartman
binder_deferred_release(struct binder_proc * proc)6207777783e0SGreg Kroah-Hartman static void binder_deferred_release(struct binder_proc *proc)
6208777783e0SGreg Kroah-Hartman {
6209342e5c90SMartijn Coenen struct binder_context *context = proc->context;
6210777783e0SGreg Kroah-Hartman struct rb_node *n;
621119c98724STodd Kjos int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
6212777783e0SGreg Kroah-Hartman
6213c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6214777783e0SGreg Kroah-Hartman hlist_del(&proc->proc_node);
6215c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6216777783e0SGreg Kroah-Hartman
6217c44b1231STodd Kjos mutex_lock(&context->context_mgr_node_lock);
6218342e5c90SMartijn Coenen if (context->binder_context_mgr_node &&
6219342e5c90SMartijn Coenen context->binder_context_mgr_node->proc == proc) {
6220777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_DEAD_BINDER,
6221777783e0SGreg Kroah-Hartman "%s: %d context_mgr_node gone\n",
6222777783e0SGreg Kroah-Hartman __func__, proc->pid);
6223342e5c90SMartijn Coenen context->binder_context_mgr_node = NULL;
6224777783e0SGreg Kroah-Hartman }
6225c44b1231STodd Kjos mutex_unlock(&context->context_mgr_node_lock);
62267bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
62277a4408c6STodd Kjos /*
62287a4408c6STodd Kjos * Make sure proc stays alive after we
62297a4408c6STodd Kjos * remove all the threads
62307a4408c6STodd Kjos */
62317a4408c6STodd Kjos proc->tmp_ref++;
6232777783e0SGreg Kroah-Hartman
62337a4408c6STodd Kjos proc->is_dead = true;
6234432ff1e9SMarco Ballesio proc->is_frozen = false;
6235ae28c1beSMarco Ballesio proc->sync_recv = false;
6236ae28c1beSMarco Ballesio proc->async_recv = false;
6237777783e0SGreg Kroah-Hartman threads = 0;
6238777783e0SGreg Kroah-Hartman active_transactions = 0;
6239777783e0SGreg Kroah-Hartman while ((n = rb_first(&proc->threads))) {
6240777783e0SGreg Kroah-Hartman struct binder_thread *thread;
6241777783e0SGreg Kroah-Hartman
6242777783e0SGreg Kroah-Hartman thread = rb_entry(n, struct binder_thread, rb_node);
62437bd7b0e6STodd Kjos binder_inner_proc_unlock(proc);
6244777783e0SGreg Kroah-Hartman threads++;
62457a4408c6STodd Kjos active_transactions += binder_thread_release(proc, thread);
62467bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
6247777783e0SGreg Kroah-Hartman }
6248777783e0SGreg Kroah-Hartman
6249777783e0SGreg Kroah-Hartman nodes = 0;
6250777783e0SGreg Kroah-Hartman incoming_refs = 0;
6251777783e0SGreg Kroah-Hartman while ((n = rb_first(&proc->nodes))) {
6252777783e0SGreg Kroah-Hartman struct binder_node *node;
6253777783e0SGreg Kroah-Hartman
6254777783e0SGreg Kroah-Hartman node = rb_entry(n, struct binder_node, rb_node);
6255777783e0SGreg Kroah-Hartman nodes++;
6256adc18842STodd Kjos /*
6257adc18842STodd Kjos * take a temporary ref on the node before
6258adc18842STodd Kjos * calling binder_node_release() which will either
6259adc18842STodd Kjos * kfree() the node or call binder_put_node()
6260adc18842STodd Kjos */
6261da0fa9e4STodd Kjos binder_inc_node_tmpref_ilocked(node);
6262777783e0SGreg Kroah-Hartman rb_erase(&node->rb_node, &proc->nodes);
6263da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6264777783e0SGreg Kroah-Hartman incoming_refs = binder_node_release(node, incoming_refs);
6265da0fa9e4STodd Kjos binder_inner_proc_lock(proc);
6266777783e0SGreg Kroah-Hartman }
6267da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6268777783e0SGreg Kroah-Hartman
6269777783e0SGreg Kroah-Hartman outgoing_refs = 0;
62702c1838dcSTodd Kjos binder_proc_lock(proc);
6271777783e0SGreg Kroah-Hartman while ((n = rb_first(&proc->refs_by_desc))) {
6272777783e0SGreg Kroah-Hartman struct binder_ref *ref;
6273777783e0SGreg Kroah-Hartman
6274777783e0SGreg Kroah-Hartman ref = rb_entry(n, struct binder_ref, rb_node_desc);
6275777783e0SGreg Kroah-Hartman outgoing_refs++;
62762c1838dcSTodd Kjos binder_cleanup_ref_olocked(ref);
62772c1838dcSTodd Kjos binder_proc_unlock(proc);
6278372e3147STodd Kjos binder_free_ref(ref);
62792c1838dcSTodd Kjos binder_proc_lock(proc);
6280777783e0SGreg Kroah-Hartman }
62812c1838dcSTodd Kjos binder_proc_unlock(proc);
6282777783e0SGreg Kroah-Hartman
628372196393STodd Kjos binder_release_work(proc, &proc->todo);
628472196393STodd Kjos binder_release_work(proc, &proc->delivered_death);
62851db76ec2SCarlos Llamas binder_release_work(proc, &proc->delivered_freeze);
6286777783e0SGreg Kroah-Hartman
6287777783e0SGreg Kroah-Hartman binder_debug(BINDER_DEBUG_OPEN_CLOSE,
628819c98724STodd Kjos "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
6289777783e0SGreg Kroah-Hartman __func__, proc->pid, threads, nodes, incoming_refs,
629019c98724STodd Kjos outgoing_refs, active_transactions);
6291777783e0SGreg Kroah-Hartman
62927a4408c6STodd Kjos binder_proc_dec_tmpref(proc);
6293777783e0SGreg Kroah-Hartman }
6294777783e0SGreg Kroah-Hartman
binder_deferred_func(struct work_struct * work)6295777783e0SGreg Kroah-Hartman static void binder_deferred_func(struct work_struct *work)
6296777783e0SGreg Kroah-Hartman {
6297777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6298777783e0SGreg Kroah-Hartman
6299777783e0SGreg Kroah-Hartman int defer;
6300777783e0SGreg Kroah-Hartman
6301777783e0SGreg Kroah-Hartman do {
6302777783e0SGreg Kroah-Hartman mutex_lock(&binder_deferred_lock);
6303777783e0SGreg Kroah-Hartman if (!hlist_empty(&binder_deferred_list)) {
6304777783e0SGreg Kroah-Hartman proc = hlist_entry(binder_deferred_list.first,
6305777783e0SGreg Kroah-Hartman struct binder_proc, deferred_work_node);
6306777783e0SGreg Kroah-Hartman hlist_del_init(&proc->deferred_work_node);
6307777783e0SGreg Kroah-Hartman defer = proc->deferred_work;
6308777783e0SGreg Kroah-Hartman proc->deferred_work = 0;
6309777783e0SGreg Kroah-Hartman } else {
6310777783e0SGreg Kroah-Hartman proc = NULL;
6311777783e0SGreg Kroah-Hartman defer = 0;
6312777783e0SGreg Kroah-Hartman }
6313777783e0SGreg Kroah-Hartman mutex_unlock(&binder_deferred_lock);
6314777783e0SGreg Kroah-Hartman
6315777783e0SGreg Kroah-Hartman if (defer & BINDER_DEFERRED_FLUSH)
6316777783e0SGreg Kroah-Hartman binder_deferred_flush(proc);
6317777783e0SGreg Kroah-Hartman
6318777783e0SGreg Kroah-Hartman if (defer & BINDER_DEFERRED_RELEASE)
6319777783e0SGreg Kroah-Hartman binder_deferred_release(proc); /* frees proc */
6320777783e0SGreg Kroah-Hartman } while (proc);
6321777783e0SGreg Kroah-Hartman }
6322777783e0SGreg Kroah-Hartman static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
6323777783e0SGreg Kroah-Hartman
6324777783e0SGreg Kroah-Hartman static void
binder_defer_work(struct binder_proc * proc,enum binder_deferred_state defer)6325777783e0SGreg Kroah-Hartman binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
6326777783e0SGreg Kroah-Hartman {
6327777783e0SGreg Kroah-Hartman mutex_lock(&binder_deferred_lock);
6328777783e0SGreg Kroah-Hartman proc->deferred_work |= defer;
6329777783e0SGreg Kroah-Hartman if (hlist_unhashed(&proc->deferred_work_node)) {
6330777783e0SGreg Kroah-Hartman hlist_add_head(&proc->deferred_work_node,
6331777783e0SGreg Kroah-Hartman &binder_deferred_list);
63321beba52dSBhaktipriya Shridhar schedule_work(&binder_deferred_work);
6333777783e0SGreg Kroah-Hartman }
6334777783e0SGreg Kroah-Hartman mutex_unlock(&binder_deferred_lock);
6335777783e0SGreg Kroah-Hartman }
6336777783e0SGreg Kroah-Hartman
print_binder_transaction_ilocked(struct seq_file * m,struct binder_proc * proc,const char * prefix,struct binder_transaction * t)63375f2f6369STodd Kjos static void print_binder_transaction_ilocked(struct seq_file *m,
63385f2f6369STodd Kjos struct binder_proc *proc,
63395f2f6369STodd Kjos const char *prefix,
6340777783e0SGreg Kroah-Hartman struct binder_transaction *t)
6341777783e0SGreg Kroah-Hartman {
63425f2f6369STodd Kjos struct binder_proc *to_proc;
63435f2f6369STodd Kjos struct binder_buffer *buffer = t->buffer;
634480093619SChuang Zhang ktime_t current_time = ktime_get();
63455f2f6369STodd Kjos
63467a4408c6STodd Kjos spin_lock(&t->lock);
63475f2f6369STodd Kjos to_proc = t->to_proc;
6348777783e0SGreg Kroah-Hartman seq_printf(m,
634980093619SChuang Zhang "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d elapsed %lldms",
6350777783e0SGreg Kroah-Hartman prefix, t->debug_id, t,
6351c21c0f9aSChuang Zhang t->from_pid,
6352c21c0f9aSChuang Zhang t->from_tid,
63535f2f6369STodd Kjos to_proc ? to_proc->pid : 0,
6354777783e0SGreg Kroah-Hartman t->to_thread ? t->to_thread->pid : 0,
635580093619SChuang Zhang t->code, t->flags, t->priority, t->need_reply,
635680093619SChuang Zhang ktime_ms_delta(current_time, t->start_time));
63577a4408c6STodd Kjos spin_unlock(&t->lock);
63587a4408c6STodd Kjos
63595f2f6369STodd Kjos if (proc != to_proc) {
63605f2f6369STodd Kjos /*
63615f2f6369STodd Kjos * Can only safely deref buffer if we are holding the
63625f2f6369STodd Kjos * correct proc inner lock for this node
63635f2f6369STodd Kjos */
63645f2f6369STodd Kjos seq_puts(m, "\n");
63655f2f6369STodd Kjos return;
63665f2f6369STodd Kjos }
63675f2f6369STodd Kjos
63685f2f6369STodd Kjos if (buffer == NULL) {
6369777783e0SGreg Kroah-Hartman seq_puts(m, " buffer free\n");
6370777783e0SGreg Kroah-Hartman return;
6371777783e0SGreg Kroah-Hartman }
63725f2f6369STodd Kjos if (buffer->target_node)
63735f2f6369STodd Kjos seq_printf(m, " node %d", buffer->target_node->debug_id);
6374162c7973SCarlos Llamas seq_printf(m, " size %zd:%zd offset %lx\n",
63755f2f6369STodd Kjos buffer->data_size, buffer->offsets_size,
6376*170d1a37SCarlos Llamas buffer->user_data - proc->alloc.vm_start);
6377777783e0SGreg Kroah-Hartman }
6378777783e0SGreg 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)63795f2f6369STodd Kjos static void print_binder_work_ilocked(struct seq_file *m,
63805f2f6369STodd Kjos struct binder_proc *proc,
63815f2f6369STodd Kjos const char *prefix,
6382777783e0SGreg Kroah-Hartman const char *transaction_prefix,
6383777783e0SGreg Kroah-Hartman struct binder_work *w)
6384777783e0SGreg Kroah-Hartman {
6385777783e0SGreg Kroah-Hartman struct binder_node *node;
6386777783e0SGreg Kroah-Hartman struct binder_transaction *t;
6387777783e0SGreg Kroah-Hartman
6388777783e0SGreg Kroah-Hartman switch (w->type) {
6389777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION:
6390777783e0SGreg Kroah-Hartman t = container_of(w, struct binder_transaction, work);
63915f2f6369STodd Kjos print_binder_transaction_ilocked(
63925f2f6369STodd Kjos m, proc, transaction_prefix, t);
6393777783e0SGreg Kroah-Hartman break;
639426549d17STodd Kjos case BINDER_WORK_RETURN_ERROR: {
639526549d17STodd Kjos struct binder_error *e = container_of(
639626549d17STodd Kjos w, struct binder_error, work);
639726549d17STodd Kjos
639826549d17STodd Kjos seq_printf(m, "%stransaction error: %u\n",
639926549d17STodd Kjos prefix, e->cmd);
640026549d17STodd Kjos } break;
6401777783e0SGreg Kroah-Hartman case BINDER_WORK_TRANSACTION_COMPLETE:
6402777783e0SGreg Kroah-Hartman seq_printf(m, "%stransaction complete\n", prefix);
6403777783e0SGreg Kroah-Hartman break;
6404777783e0SGreg Kroah-Hartman case BINDER_WORK_NODE:
6405777783e0SGreg Kroah-Hartman node = container_of(w, struct binder_node, work);
6406777783e0SGreg Kroah-Hartman seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
6407777783e0SGreg Kroah-Hartman prefix, node->debug_id,
6408777783e0SGreg Kroah-Hartman (u64)node->ptr, (u64)node->cookie);
6409777783e0SGreg Kroah-Hartman break;
6410777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER:
6411777783e0SGreg Kroah-Hartman seq_printf(m, "%shas dead binder\n", prefix);
6412777783e0SGreg Kroah-Hartman break;
6413777783e0SGreg Kroah-Hartman case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
6414777783e0SGreg Kroah-Hartman seq_printf(m, "%shas cleared dead binder\n", prefix);
6415777783e0SGreg Kroah-Hartman break;
6416777783e0SGreg Kroah-Hartman case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
6417777783e0SGreg Kroah-Hartman seq_printf(m, "%shas cleared death notification\n", prefix);
6418777783e0SGreg Kroah-Hartman break;
6419830d7db7SCarlos Llamas case BINDER_WORK_FROZEN_BINDER:
6420830d7db7SCarlos Llamas seq_printf(m, "%shas frozen binder\n", prefix);
6421830d7db7SCarlos Llamas break;
6422595ea72eSCarlos Llamas case BINDER_WORK_CLEAR_FREEZE_NOTIFICATION:
6423595ea72eSCarlos Llamas seq_printf(m, "%shas cleared freeze notification\n", prefix);
6424595ea72eSCarlos Llamas break;
6425777783e0SGreg Kroah-Hartman default:
6426777783e0SGreg Kroah-Hartman seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
6427777783e0SGreg Kroah-Hartman break;
6428777783e0SGreg Kroah-Hartman }
6429777783e0SGreg Kroah-Hartman }
6430777783e0SGreg Kroah-Hartman
print_binder_thread_ilocked(struct seq_file * m,struct binder_thread * thread,int print_always)643172196393STodd Kjos static void print_binder_thread_ilocked(struct seq_file *m,
6432777783e0SGreg Kroah-Hartman struct binder_thread *thread,
6433777783e0SGreg Kroah-Hartman int print_always)
6434777783e0SGreg Kroah-Hartman {
6435777783e0SGreg Kroah-Hartman struct binder_transaction *t;
6436777783e0SGreg Kroah-Hartman struct binder_work *w;
6437777783e0SGreg Kroah-Hartman size_t start_pos = m->count;
6438777783e0SGreg Kroah-Hartman size_t header_pos;
6439777783e0SGreg Kroah-Hartman
64407a4408c6STodd Kjos seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
644108dabceeSTodd Kjos thread->pid, thread->looper,
64427a4408c6STodd Kjos thread->looper_need_return,
64437a4408c6STodd Kjos atomic_read(&thread->tmp_ref));
6444777783e0SGreg Kroah-Hartman header_pos = m->count;
6445777783e0SGreg Kroah-Hartman t = thread->transaction_stack;
6446777783e0SGreg Kroah-Hartman while (t) {
6447777783e0SGreg Kroah-Hartman if (t->from == thread) {
64485f2f6369STodd Kjos print_binder_transaction_ilocked(m, thread->proc,
6449777783e0SGreg Kroah-Hartman " outgoing transaction", t);
6450777783e0SGreg Kroah-Hartman t = t->from_parent;
6451777783e0SGreg Kroah-Hartman } else if (t->to_thread == thread) {
64525f2f6369STodd Kjos print_binder_transaction_ilocked(m, thread->proc,
6453777783e0SGreg Kroah-Hartman " incoming transaction", t);
6454777783e0SGreg Kroah-Hartman t = t->to_parent;
6455777783e0SGreg Kroah-Hartman } else {
64565f2f6369STodd Kjos print_binder_transaction_ilocked(m, thread->proc,
64575f2f6369STodd Kjos " bad transaction", t);
6458777783e0SGreg Kroah-Hartman t = NULL;
6459777783e0SGreg Kroah-Hartman }
6460777783e0SGreg Kroah-Hartman }
6461777783e0SGreg Kroah-Hartman list_for_each_entry(w, &thread->todo, entry) {
64625f2f6369STodd Kjos print_binder_work_ilocked(m, thread->proc, " ",
646372196393STodd Kjos " pending transaction", w);
6464777783e0SGreg Kroah-Hartman }
6465777783e0SGreg Kroah-Hartman if (!print_always && m->count == header_pos)
6466777783e0SGreg Kroah-Hartman m->count = start_pos;
6467777783e0SGreg Kroah-Hartman }
6468777783e0SGreg Kroah-Hartman
print_binder_node_nilocked(struct seq_file * m,struct binder_node * node)6469da0fa9e4STodd Kjos static void print_binder_node_nilocked(struct seq_file *m,
6470673068eeSTodd Kjos struct binder_node *node)
6471777783e0SGreg Kroah-Hartman {
6472777783e0SGreg Kroah-Hartman struct binder_ref *ref;
6473777783e0SGreg Kroah-Hartman struct binder_work *w;
6474777783e0SGreg Kroah-Hartman int count;
6475777783e0SGreg Kroah-Hartman
64763fa2601eSPierre Gondois count = hlist_count_nodes(&node->refs);
6477777783e0SGreg Kroah-Hartman
6478adc18842STodd Kjos seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
6479777783e0SGreg Kroah-Hartman node->debug_id, (u64)node->ptr, (u64)node->cookie,
6480777783e0SGreg Kroah-Hartman node->has_strong_ref, node->has_weak_ref,
6481777783e0SGreg Kroah-Hartman node->local_strong_refs, node->local_weak_refs,
6482adc18842STodd Kjos node->internal_strong_refs, count, node->tmp_refs);
6483777783e0SGreg Kroah-Hartman if (count) {
6484777783e0SGreg Kroah-Hartman seq_puts(m, " proc");
6485777783e0SGreg Kroah-Hartman hlist_for_each_entry(ref, &node->refs, node_entry)
6486777783e0SGreg Kroah-Hartman seq_printf(m, " %d", ref->proc->pid);
6487777783e0SGreg Kroah-Hartman }
6488777783e0SGreg Kroah-Hartman seq_puts(m, "\n");
648972196393STodd Kjos if (node->proc) {
6490777783e0SGreg Kroah-Hartman list_for_each_entry(w, &node->async_todo, entry)
64915f2f6369STodd Kjos print_binder_work_ilocked(m, node->proc, " ",
6492777783e0SGreg Kroah-Hartman " pending async transaction", w);
649372196393STodd Kjos }
6494777783e0SGreg Kroah-Hartman }
6495777783e0SGreg Kroah-Hartman
print_binder_ref_olocked(struct seq_file * m,struct binder_ref * ref)64962c1838dcSTodd Kjos static void print_binder_ref_olocked(struct seq_file *m,
64972c1838dcSTodd Kjos struct binder_ref *ref)
6498777783e0SGreg Kroah-Hartman {
6499673068eeSTodd Kjos binder_node_lock(ref->node);
6500372e3147STodd Kjos seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
6501372e3147STodd Kjos ref->data.debug_id, ref->data.desc,
6502372e3147STodd Kjos ref->node->proc ? "" : "dead ",
6503372e3147STodd Kjos ref->node->debug_id, ref->data.strong,
6504372e3147STodd Kjos ref->data.weak, ref->death);
6505673068eeSTodd Kjos binder_node_unlock(ref->node);
6506777783e0SGreg Kroah-Hartman }
6507777783e0SGreg Kroah-Hartman
print_binder_proc(struct seq_file * m,struct binder_proc * proc,int print_all)6508777783e0SGreg Kroah-Hartman static void print_binder_proc(struct seq_file *m,
6509777783e0SGreg Kroah-Hartman struct binder_proc *proc, int print_all)
6510777783e0SGreg Kroah-Hartman {
6511777783e0SGreg Kroah-Hartman struct binder_work *w;
6512777783e0SGreg Kroah-Hartman struct rb_node *n;
6513777783e0SGreg Kroah-Hartman size_t start_pos = m->count;
6514777783e0SGreg Kroah-Hartman size_t header_pos;
6515da0fa9e4STodd Kjos struct binder_node *last_node = NULL;
6516777783e0SGreg Kroah-Hartman
6517777783e0SGreg Kroah-Hartman seq_printf(m, "proc %d\n", proc->pid);
651814db3181SMartijn Coenen seq_printf(m, "context %s\n", proc->context->name);
6519777783e0SGreg Kroah-Hartman header_pos = m->count;
6520777783e0SGreg Kroah-Hartman
652172196393STodd Kjos binder_inner_proc_lock(proc);
6522777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
652372196393STodd Kjos print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
6524777783e0SGreg Kroah-Hartman rb_node), print_all);
6525da0fa9e4STodd Kjos
6526777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
6527777783e0SGreg Kroah-Hartman struct binder_node *node = rb_entry(n, struct binder_node,
6528777783e0SGreg Kroah-Hartman rb_node);
6529ecd589d8STodd Kjos if (!print_all && !node->has_async_transaction)
6530ecd589d8STodd Kjos continue;
6531ecd589d8STodd Kjos
6532da0fa9e4STodd Kjos /*
6533da0fa9e4STodd Kjos * take a temporary reference on the node so it
6534da0fa9e4STodd Kjos * survives and isn't removed from the tree
6535da0fa9e4STodd Kjos * while we print it.
6536da0fa9e4STodd Kjos */
6537da0fa9e4STodd Kjos binder_inc_node_tmpref_ilocked(node);
6538da0fa9e4STodd Kjos /* Need to drop inner lock to take node lock */
6539da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6540da0fa9e4STodd Kjos if (last_node)
6541da0fa9e4STodd Kjos binder_put_node(last_node);
6542da0fa9e4STodd Kjos binder_node_inner_lock(node);
6543da0fa9e4STodd Kjos print_binder_node_nilocked(m, node);
6544da0fa9e4STodd Kjos binder_node_inner_unlock(node);
6545da0fa9e4STodd Kjos last_node = node;
6546da0fa9e4STodd Kjos binder_inner_proc_lock(proc);
6547777783e0SGreg Kroah-Hartman }
6548da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6549da0fa9e4STodd Kjos if (last_node)
6550da0fa9e4STodd Kjos binder_put_node(last_node);
6551da0fa9e4STodd Kjos
6552777783e0SGreg Kroah-Hartman if (print_all) {
65532c1838dcSTodd Kjos binder_proc_lock(proc);
6554777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->refs_by_desc);
6555777783e0SGreg Kroah-Hartman n != NULL;
6556777783e0SGreg Kroah-Hartman n = rb_next(n))
65572c1838dcSTodd Kjos print_binder_ref_olocked(m, rb_entry(n,
65582c1838dcSTodd Kjos struct binder_ref,
6559777783e0SGreg Kroah-Hartman rb_node_desc));
65602c1838dcSTodd Kjos binder_proc_unlock(proc);
6561777783e0SGreg Kroah-Hartman }
656219c98724STodd Kjos binder_alloc_print_allocated(m, &proc->alloc);
656372196393STodd Kjos binder_inner_proc_lock(proc);
6564777783e0SGreg Kroah-Hartman list_for_each_entry(w, &proc->todo, entry)
65655f2f6369STodd Kjos print_binder_work_ilocked(m, proc, " ",
65665f2f6369STodd Kjos " pending transaction", w);
6567777783e0SGreg Kroah-Hartman list_for_each_entry(w, &proc->delivered_death, entry) {
6568777783e0SGreg Kroah-Hartman seq_puts(m, " has delivered dead binder\n");
6569777783e0SGreg Kroah-Hartman break;
6570777783e0SGreg Kroah-Hartman }
6571cb2aeb2eSCarlos Llamas list_for_each_entry(w, &proc->delivered_freeze, entry) {
6572cb2aeb2eSCarlos Llamas seq_puts(m, " has delivered freeze binder\n");
6573cb2aeb2eSCarlos Llamas break;
6574cb2aeb2eSCarlos Llamas }
657572196393STodd Kjos binder_inner_proc_unlock(proc);
6576777783e0SGreg Kroah-Hartman if (!print_all && m->count == header_pos)
6577777783e0SGreg Kroah-Hartman m->count = start_pos;
6578777783e0SGreg Kroah-Hartman }
6579777783e0SGreg Kroah-Hartman
6580777783e0SGreg Kroah-Hartman static const char * const binder_return_strings[] = {
6581777783e0SGreg Kroah-Hartman "BR_ERROR",
6582777783e0SGreg Kroah-Hartman "BR_OK",
6583777783e0SGreg Kroah-Hartman "BR_TRANSACTION",
6584777783e0SGreg Kroah-Hartman "BR_REPLY",
6585777783e0SGreg Kroah-Hartman "BR_ACQUIRE_RESULT",
6586777783e0SGreg Kroah-Hartman "BR_DEAD_REPLY",
6587777783e0SGreg Kroah-Hartman "BR_TRANSACTION_COMPLETE",
6588777783e0SGreg Kroah-Hartman "BR_INCREFS",
6589777783e0SGreg Kroah-Hartman "BR_ACQUIRE",
6590777783e0SGreg Kroah-Hartman "BR_RELEASE",
6591777783e0SGreg Kroah-Hartman "BR_DECREFS",
6592777783e0SGreg Kroah-Hartman "BR_ATTEMPT_ACQUIRE",
6593777783e0SGreg Kroah-Hartman "BR_NOOP",
6594777783e0SGreg Kroah-Hartman "BR_SPAWN_LOOPER",
6595777783e0SGreg Kroah-Hartman "BR_FINISHED",
6596777783e0SGreg Kroah-Hartman "BR_DEAD_BINDER",
6597777783e0SGreg Kroah-Hartman "BR_CLEAR_DEATH_NOTIFICATION_DONE",
659800516915SHang Lu "BR_FAILED_REPLY",
659900516915SHang Lu "BR_FROZEN_REPLY",
6600a7dc1e6fSHang Lu "BR_ONEWAY_SPAM_SUSPECT",
6601d579b04aSYu-Ting Tseng "BR_TRANSACTION_PENDING_FROZEN",
6602d579b04aSYu-Ting Tseng "BR_FROZEN_BINDER",
6603d579b04aSYu-Ting Tseng "BR_CLEAR_FREEZE_NOTIFICATION_DONE",
6604777783e0SGreg Kroah-Hartman };
6605777783e0SGreg Kroah-Hartman
6606777783e0SGreg Kroah-Hartman static const char * const binder_command_strings[] = {
6607777783e0SGreg Kroah-Hartman "BC_TRANSACTION",
6608777783e0SGreg Kroah-Hartman "BC_REPLY",
6609777783e0SGreg Kroah-Hartman "BC_ACQUIRE_RESULT",
6610777783e0SGreg Kroah-Hartman "BC_FREE_BUFFER",
6611777783e0SGreg Kroah-Hartman "BC_INCREFS",
6612777783e0SGreg Kroah-Hartman "BC_ACQUIRE",
6613777783e0SGreg Kroah-Hartman "BC_RELEASE",
6614777783e0SGreg Kroah-Hartman "BC_DECREFS",
6615777783e0SGreg Kroah-Hartman "BC_INCREFS_DONE",
6616777783e0SGreg Kroah-Hartman "BC_ACQUIRE_DONE",
6617777783e0SGreg Kroah-Hartman "BC_ATTEMPT_ACQUIRE",
6618777783e0SGreg Kroah-Hartman "BC_REGISTER_LOOPER",
6619777783e0SGreg Kroah-Hartman "BC_ENTER_LOOPER",
6620777783e0SGreg Kroah-Hartman "BC_EXIT_LOOPER",
6621777783e0SGreg Kroah-Hartman "BC_REQUEST_DEATH_NOTIFICATION",
6622777783e0SGreg Kroah-Hartman "BC_CLEAR_DEATH_NOTIFICATION",
66237980240bSMartijn Coenen "BC_DEAD_BINDER_DONE",
66247980240bSMartijn Coenen "BC_TRANSACTION_SG",
66257980240bSMartijn Coenen "BC_REPLY_SG",
6626d579b04aSYu-Ting Tseng "BC_REQUEST_FREEZE_NOTIFICATION",
6627d579b04aSYu-Ting Tseng "BC_CLEAR_FREEZE_NOTIFICATION",
6628d579b04aSYu-Ting Tseng "BC_FREEZE_NOTIFICATION_DONE",
6629777783e0SGreg Kroah-Hartman };
6630777783e0SGreg Kroah-Hartman
6631777783e0SGreg Kroah-Hartman static const char * const binder_objstat_strings[] = {
6632777783e0SGreg Kroah-Hartman "proc",
6633777783e0SGreg Kroah-Hartman "thread",
6634777783e0SGreg Kroah-Hartman "node",
6635777783e0SGreg Kroah-Hartman "ref",
6636777783e0SGreg Kroah-Hartman "death",
6637777783e0SGreg Kroah-Hartman "transaction",
6638d579b04aSYu-Ting Tseng "transaction_complete",
6639d579b04aSYu-Ting Tseng "freeze",
6640777783e0SGreg Kroah-Hartman };
6641777783e0SGreg Kroah-Hartman
print_binder_stats(struct seq_file * m,const char * prefix,struct binder_stats * stats)6642777783e0SGreg Kroah-Hartman static void print_binder_stats(struct seq_file *m, const char *prefix,
6643777783e0SGreg Kroah-Hartman struct binder_stats *stats)
6644777783e0SGreg Kroah-Hartman {
6645777783e0SGreg Kroah-Hartman int i;
6646777783e0SGreg Kroah-Hartman
6647777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
6648777783e0SGreg Kroah-Hartman ARRAY_SIZE(binder_command_strings));
6649777783e0SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
66500953c797SBadhri Jagan Sridharan int temp = atomic_read(&stats->bc[i]);
66510953c797SBadhri Jagan Sridharan
66520953c797SBadhri Jagan Sridharan if (temp)
6653777783e0SGreg Kroah-Hartman seq_printf(m, "%s%s: %d\n", prefix,
66540953c797SBadhri Jagan Sridharan binder_command_strings[i], temp);
6655777783e0SGreg Kroah-Hartman }
6656777783e0SGreg Kroah-Hartman
6657777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
6658777783e0SGreg Kroah-Hartman ARRAY_SIZE(binder_return_strings));
6659777783e0SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
66600953c797SBadhri Jagan Sridharan int temp = atomic_read(&stats->br[i]);
66610953c797SBadhri Jagan Sridharan
66620953c797SBadhri Jagan Sridharan if (temp)
6663777783e0SGreg Kroah-Hartman seq_printf(m, "%s%s: %d\n", prefix,
66640953c797SBadhri Jagan Sridharan binder_return_strings[i], temp);
6665777783e0SGreg Kroah-Hartman }
6666777783e0SGreg Kroah-Hartman
6667777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
6668777783e0SGreg Kroah-Hartman ARRAY_SIZE(binder_objstat_strings));
6669777783e0SGreg Kroah-Hartman BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
6670777783e0SGreg Kroah-Hartman ARRAY_SIZE(stats->obj_deleted));
6671777783e0SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
66720953c797SBadhri Jagan Sridharan int created = atomic_read(&stats->obj_created[i]);
66730953c797SBadhri Jagan Sridharan int deleted = atomic_read(&stats->obj_deleted[i]);
66740953c797SBadhri Jagan Sridharan
66750953c797SBadhri Jagan Sridharan if (created || deleted)
66760953c797SBadhri Jagan Sridharan seq_printf(m, "%s%s: active %d total %d\n",
66770953c797SBadhri Jagan Sridharan prefix,
6678777783e0SGreg Kroah-Hartman binder_objstat_strings[i],
66790953c797SBadhri Jagan Sridharan created - deleted,
66800953c797SBadhri Jagan Sridharan created);
6681777783e0SGreg Kroah-Hartman }
6682777783e0SGreg Kroah-Hartman }
6683777783e0SGreg Kroah-Hartman
print_binder_proc_stats(struct seq_file * m,struct binder_proc * proc)6684777783e0SGreg Kroah-Hartman static void print_binder_proc_stats(struct seq_file *m,
6685777783e0SGreg Kroah-Hartman struct binder_proc *proc)
6686777783e0SGreg Kroah-Hartman {
6687777783e0SGreg Kroah-Hartman struct binder_work *w;
66881b77e9dcSMartijn Coenen struct binder_thread *thread;
6689777783e0SGreg Kroah-Hartman struct rb_node *n;
66901b77e9dcSMartijn Coenen int count, strong, weak, ready_threads;
66917bd7b0e6STodd Kjos size_t free_async_space =
66927bd7b0e6STodd Kjos binder_alloc_get_free_async_space(&proc->alloc);
6693777783e0SGreg Kroah-Hartman
6694777783e0SGreg Kroah-Hartman seq_printf(m, "proc %d\n", proc->pid);
669514db3181SMartijn Coenen seq_printf(m, "context %s\n", proc->context->name);
6696777783e0SGreg Kroah-Hartman count = 0;
66971b77e9dcSMartijn Coenen ready_threads = 0;
66987bd7b0e6STodd Kjos binder_inner_proc_lock(proc);
6699777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
6700777783e0SGreg Kroah-Hartman count++;
67011b77e9dcSMartijn Coenen
67021b77e9dcSMartijn Coenen list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
67031b77e9dcSMartijn Coenen ready_threads++;
67041b77e9dcSMartijn Coenen
6705777783e0SGreg Kroah-Hartman seq_printf(m, " threads: %d\n", count);
6706777783e0SGreg Kroah-Hartman seq_printf(m, " requested threads: %d+%d/%d\n"
6707777783e0SGreg Kroah-Hartman " ready threads %d\n"
6708777783e0SGreg Kroah-Hartman " free async space %zd\n", proc->requested_threads,
6709777783e0SGreg Kroah-Hartman proc->requested_threads_started, proc->max_threads,
67101b77e9dcSMartijn Coenen ready_threads,
67117bd7b0e6STodd Kjos free_async_space);
6712777783e0SGreg Kroah-Hartman count = 0;
6713777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
6714777783e0SGreg Kroah-Hartman count++;
6715da0fa9e4STodd Kjos binder_inner_proc_unlock(proc);
6716777783e0SGreg Kroah-Hartman seq_printf(m, " nodes: %d\n", count);
6717777783e0SGreg Kroah-Hartman count = 0;
6718777783e0SGreg Kroah-Hartman strong = 0;
6719777783e0SGreg Kroah-Hartman weak = 0;
67202c1838dcSTodd Kjos binder_proc_lock(proc);
6721777783e0SGreg Kroah-Hartman for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
6722777783e0SGreg Kroah-Hartman struct binder_ref *ref = rb_entry(n, struct binder_ref,
6723777783e0SGreg Kroah-Hartman rb_node_desc);
6724777783e0SGreg Kroah-Hartman count++;
6725372e3147STodd Kjos strong += ref->data.strong;
6726372e3147STodd Kjos weak += ref->data.weak;
6727777783e0SGreg Kroah-Hartman }
67282c1838dcSTodd Kjos binder_proc_unlock(proc);
6729777783e0SGreg Kroah-Hartman seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
6730777783e0SGreg Kroah-Hartman
673119c98724STodd Kjos count = binder_alloc_get_allocated_count(&proc->alloc);
6732777783e0SGreg Kroah-Hartman seq_printf(m, " buffers: %d\n", count);
6733777783e0SGreg Kroah-Hartman
67348ef4665aSSherry Yang binder_alloc_print_pages(m, &proc->alloc);
67358ef4665aSSherry Yang
6736777783e0SGreg Kroah-Hartman count = 0;
673772196393STodd Kjos binder_inner_proc_lock(proc);
6738777783e0SGreg Kroah-Hartman list_for_each_entry(w, &proc->todo, entry) {
673972196393STodd Kjos if (w->type == BINDER_WORK_TRANSACTION)
6740777783e0SGreg Kroah-Hartman count++;
6741777783e0SGreg Kroah-Hartman }
674272196393STodd Kjos binder_inner_proc_unlock(proc);
6743777783e0SGreg Kroah-Hartman seq_printf(m, " pending transactions: %d\n", count);
6744777783e0SGreg Kroah-Hartman
6745777783e0SGreg Kroah-Hartman print_binder_stats(m, " ", &proc->stats);
6746777783e0SGreg Kroah-Hartman }
6747777783e0SGreg Kroah-Hartman
state_show(struct seq_file * m,void * unused)6748b7e241bbSCarlos Llamas static int state_show(struct seq_file *m, void *unused)
6749777783e0SGreg Kroah-Hartman {
6750777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6751777783e0SGreg Kroah-Hartman struct binder_node *node;
6752673068eeSTodd Kjos struct binder_node *last_node = NULL;
6753777783e0SGreg Kroah-Hartman
6754777783e0SGreg Kroah-Hartman seq_puts(m, "binder state:\n");
6755777783e0SGreg Kroah-Hartman
6756c44b1231STodd Kjos spin_lock(&binder_dead_nodes_lock);
6757777783e0SGreg Kroah-Hartman if (!hlist_empty(&binder_dead_nodes))
6758777783e0SGreg Kroah-Hartman seq_puts(m, "dead nodes:\n");
6759673068eeSTodd Kjos hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
6760673068eeSTodd Kjos /*
6761673068eeSTodd Kjos * take a temporary reference on the node so it
6762673068eeSTodd Kjos * survives and isn't removed from the list
6763673068eeSTodd Kjos * while we print it.
6764673068eeSTodd Kjos */
6765673068eeSTodd Kjos node->tmp_refs++;
6766c44b1231STodd Kjos spin_unlock(&binder_dead_nodes_lock);
6767673068eeSTodd Kjos if (last_node)
6768673068eeSTodd Kjos binder_put_node(last_node);
6769673068eeSTodd Kjos binder_node_lock(node);
6770da0fa9e4STodd Kjos print_binder_node_nilocked(m, node);
6771673068eeSTodd Kjos binder_node_unlock(node);
6772673068eeSTodd Kjos last_node = node;
6773673068eeSTodd Kjos spin_lock(&binder_dead_nodes_lock);
6774673068eeSTodd Kjos }
6775673068eeSTodd Kjos spin_unlock(&binder_dead_nodes_lock);
6776673068eeSTodd Kjos if (last_node)
6777673068eeSTodd Kjos binder_put_node(last_node);
6778777783e0SGreg Kroah-Hartman
6779c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6780777783e0SGreg Kroah-Hartman hlist_for_each_entry(proc, &binder_procs, proc_node)
6781777783e0SGreg Kroah-Hartman print_binder_proc(m, proc, 1);
6782c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6783a60b890fSTodd Kjos
6784777783e0SGreg Kroah-Hartman return 0;
6785777783e0SGreg Kroah-Hartman }
6786777783e0SGreg Kroah-Hartman
stats_show(struct seq_file * m,void * unused)6787b7e241bbSCarlos Llamas static int stats_show(struct seq_file *m, void *unused)
6788777783e0SGreg Kroah-Hartman {
6789777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6790777783e0SGreg Kroah-Hartman
6791777783e0SGreg Kroah-Hartman seq_puts(m, "binder stats:\n");
6792777783e0SGreg Kroah-Hartman
6793777783e0SGreg Kroah-Hartman print_binder_stats(m, "", &binder_stats);
6794777783e0SGreg Kroah-Hartman
6795c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6796777783e0SGreg Kroah-Hartman hlist_for_each_entry(proc, &binder_procs, proc_node)
6797777783e0SGreg Kroah-Hartman print_binder_proc_stats(m, proc);
6798c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6799a60b890fSTodd Kjos
6800777783e0SGreg Kroah-Hartman return 0;
6801777783e0SGreg Kroah-Hartman }
6802777783e0SGreg Kroah-Hartman
transactions_show(struct seq_file * m,void * unused)6803b7e241bbSCarlos Llamas static int transactions_show(struct seq_file *m, void *unused)
6804777783e0SGreg Kroah-Hartman {
6805777783e0SGreg Kroah-Hartman struct binder_proc *proc;
6806777783e0SGreg Kroah-Hartman
6807777783e0SGreg Kroah-Hartman seq_puts(m, "binder transactions:\n");
6808c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
6809777783e0SGreg Kroah-Hartman hlist_for_each_entry(proc, &binder_procs, proc_node)
6810777783e0SGreg Kroah-Hartman print_binder_proc(m, proc, 0);
6811c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6812a60b890fSTodd Kjos
6813777783e0SGreg Kroah-Hartman return 0;
6814777783e0SGreg Kroah-Hartman }
6815777783e0SGreg Kroah-Hartman
proc_show(struct seq_file * m,void * unused)6816c13e0a52SYangtao Li static int proc_show(struct seq_file *m, void *unused)
6817777783e0SGreg Kroah-Hartman {
681883050a4eSRiley Andrews struct binder_proc *itr;
681914db3181SMartijn Coenen int pid = (unsigned long)m->private;
6820777783e0SGreg Kroah-Hartman
6821c44b1231STodd Kjos mutex_lock(&binder_procs_lock);
682283050a4eSRiley Andrews hlist_for_each_entry(itr, &binder_procs, proc_node) {
682314db3181SMartijn Coenen if (itr->pid == pid) {
6824777783e0SGreg Kroah-Hartman seq_puts(m, "binder proc state:\n");
682514db3181SMartijn Coenen print_binder_proc(m, itr, 1);
682614db3181SMartijn Coenen }
682783050a4eSRiley Andrews }
6828c44b1231STodd Kjos mutex_unlock(&binder_procs_lock);
6829c44b1231STodd Kjos
6830777783e0SGreg Kroah-Hartman return 0;
6831777783e0SGreg Kroah-Hartman }
6832777783e0SGreg Kroah-Hartman
print_binder_transaction_log_entry(struct seq_file * m,struct binder_transaction_log_entry * e)6833777783e0SGreg Kroah-Hartman static void print_binder_transaction_log_entry(struct seq_file *m,
6834777783e0SGreg Kroah-Hartman struct binder_transaction_log_entry *e)
6835777783e0SGreg Kroah-Hartman {
6836d99c7333STodd Kjos int debug_id = READ_ONCE(e->debug_id_done);
6837d99c7333STodd Kjos /*
6838d99c7333STodd Kjos * read barrier to guarantee debug_id_done read before
6839d99c7333STodd Kjos * we print the log values
6840d99c7333STodd Kjos */
6841d99c7333STodd Kjos smp_rmb();
6842777783e0SGreg Kroah-Hartman seq_printf(m,
6843d99c7333STodd Kjos "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
6844777783e0SGreg Kroah-Hartman e->debug_id, (e->call_type == 2) ? "reply" :
6845777783e0SGreg Kroah-Hartman ((e->call_type == 1) ? "async" : "call "), e->from_proc,
684614db3181SMartijn Coenen e->from_thread, e->to_proc, e->to_thread, e->context_name,
684757ada2fbSTodd Kjos e->to_node, e->target_handle, e->data_size, e->offsets_size,
684857ada2fbSTodd Kjos e->return_error, e->return_error_param,
684957ada2fbSTodd Kjos e->return_error_line);
6850d99c7333STodd Kjos /*
6851d99c7333STodd Kjos * read-barrier to guarantee read of debug_id_done after
6852d99c7333STodd Kjos * done printing the fields of the entry
6853d99c7333STodd Kjos */
6854d99c7333STodd Kjos smp_rmb();
6855d99c7333STodd Kjos seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6856d99c7333STodd Kjos "\n" : " (incomplete)\n");
6857777783e0SGreg Kroah-Hartman }
6858777783e0SGreg Kroah-Hartman
transaction_log_show(struct seq_file * m,void * unused)6859b7e241bbSCarlos Llamas static int transaction_log_show(struct seq_file *m, void *unused)
6860777783e0SGreg Kroah-Hartman {
6861777783e0SGreg Kroah-Hartman struct binder_transaction_log *log = m->private;
6862d99c7333STodd Kjos unsigned int log_cur = atomic_read(&log->cur);
6863d99c7333STodd Kjos unsigned int count;
6864d99c7333STodd Kjos unsigned int cur;
6865777783e0SGreg Kroah-Hartman int i;
6866777783e0SGreg Kroah-Hartman
6867d99c7333STodd Kjos count = log_cur + 1;
6868d99c7333STodd Kjos cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6869d99c7333STodd Kjos 0 : count % ARRAY_SIZE(log->entry);
6870d99c7333STodd Kjos if (count > ARRAY_SIZE(log->entry) || log->full)
6871d99c7333STodd Kjos count = ARRAY_SIZE(log->entry);
6872d99c7333STodd Kjos for (i = 0; i < count; i++) {
6873d99c7333STodd Kjos unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6874d99c7333STodd Kjos
6875d99c7333STodd Kjos print_binder_transaction_log_entry(m, &log->entry[index]);
6876777783e0SGreg Kroah-Hartman }
6877777783e0SGreg Kroah-Hartman return 0;
6878777783e0SGreg Kroah-Hartman }
6879777783e0SGreg Kroah-Hartman
68803ad20fe3SChristian Brauner const struct file_operations binder_fops = {
6881777783e0SGreg Kroah-Hartman .owner = THIS_MODULE,
6882777783e0SGreg Kroah-Hartman .poll = binder_poll,
6883777783e0SGreg Kroah-Hartman .unlocked_ioctl = binder_ioctl,
68841832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl,
6885777783e0SGreg Kroah-Hartman .mmap = binder_mmap,
6886777783e0SGreg Kroah-Hartman .open = binder_open,
6887777783e0SGreg Kroah-Hartman .flush = binder_flush,
6888777783e0SGreg Kroah-Hartman .release = binder_release,
6889777783e0SGreg Kroah-Hartman };
6890777783e0SGreg Kroah-Hartman
6891b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(state);
6892b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(stats);
6893b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(transactions);
6894b7e241bbSCarlos Llamas DEFINE_SHOW_ATTRIBUTE(transaction_log);
6895b7e241bbSCarlos Llamas
6896b7e241bbSCarlos Llamas const struct binder_debugfs_entry binder_debugfs_entries[] = {
6897b7e241bbSCarlos Llamas {
6898b7e241bbSCarlos Llamas .name = "state",
6899b7e241bbSCarlos Llamas .mode = 0444,
6900b7e241bbSCarlos Llamas .fops = &state_fops,
6901b7e241bbSCarlos Llamas .data = NULL,
6902b7e241bbSCarlos Llamas },
6903b7e241bbSCarlos Llamas {
6904b7e241bbSCarlos Llamas .name = "stats",
6905b7e241bbSCarlos Llamas .mode = 0444,
6906b7e241bbSCarlos Llamas .fops = &stats_fops,
6907b7e241bbSCarlos Llamas .data = NULL,
6908b7e241bbSCarlos Llamas },
6909b7e241bbSCarlos Llamas {
6910b7e241bbSCarlos Llamas .name = "transactions",
6911b7e241bbSCarlos Llamas .mode = 0444,
6912b7e241bbSCarlos Llamas .fops = &transactions_fops,
6913b7e241bbSCarlos Llamas .data = NULL,
6914b7e241bbSCarlos Llamas },
6915b7e241bbSCarlos Llamas {
6916b7e241bbSCarlos Llamas .name = "transaction_log",
6917b7e241bbSCarlos Llamas .mode = 0444,
6918b7e241bbSCarlos Llamas .fops = &transaction_log_fops,
6919b7e241bbSCarlos Llamas .data = &binder_transaction_log,
6920b7e241bbSCarlos Llamas },
6921b7e241bbSCarlos Llamas {
6922b7e241bbSCarlos Llamas .name = "failed_transaction_log",
6923b7e241bbSCarlos Llamas .mode = 0444,
6924b7e241bbSCarlos Llamas .fops = &transaction_log_fops,
6925b7e241bbSCarlos Llamas .data = &binder_transaction_log_failed,
6926b7e241bbSCarlos Llamas },
6927b7e241bbSCarlos Llamas {} /* terminator */
6928b7e241bbSCarlos Llamas };
6929b7e241bbSCarlos Llamas
binder_add_device(struct binder_device * device)693012d909caSLi Li void binder_add_device(struct binder_device *device)
693112d909caSLi Li {
693212d909caSLi Li hlist_add_head(&device->hlist, &binder_devices);
693312d909caSLi Li }
693412d909caSLi Li
init_binder_device(const char * name)6935ac4812c5SMartijn Coenen static int __init init_binder_device(const char *name)
6936ac4812c5SMartijn Coenen {
6937ac4812c5SMartijn Coenen int ret;
6938ac4812c5SMartijn Coenen struct binder_device *binder_device;
6939ac4812c5SMartijn Coenen
6940ac4812c5SMartijn Coenen binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6941ac4812c5SMartijn Coenen if (!binder_device)
6942ac4812c5SMartijn Coenen return -ENOMEM;
6943ac4812c5SMartijn Coenen
6944ac4812c5SMartijn Coenen binder_device->miscdev.fops = &binder_fops;
6945ac4812c5SMartijn Coenen binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6946ac4812c5SMartijn Coenen binder_device->miscdev.name = name;
6947ac4812c5SMartijn Coenen
6948f0fe2c0fSChristian Brauner refcount_set(&binder_device->ref, 1);
6949ac4812c5SMartijn Coenen binder_device->context.binder_context_mgr_uid = INVALID_UID;
6950ac4812c5SMartijn Coenen binder_device->context.name = name;
6951c44b1231STodd Kjos mutex_init(&binder_device->context.context_mgr_node_lock);
6952ac4812c5SMartijn Coenen
6953ac4812c5SMartijn Coenen ret = misc_register(&binder_device->miscdev);
6954ac4812c5SMartijn Coenen if (ret < 0) {
6955ac4812c5SMartijn Coenen kfree(binder_device);
6956ac4812c5SMartijn Coenen return ret;
6957ac4812c5SMartijn Coenen }
6958ac4812c5SMartijn Coenen
6959ac4812c5SMartijn Coenen hlist_add_head(&binder_device->hlist, &binder_devices);
6960ac4812c5SMartijn Coenen
6961ac4812c5SMartijn Coenen return ret;
6962ac4812c5SMartijn Coenen }
6963ac4812c5SMartijn Coenen
binder_init(void)6964777783e0SGreg Kroah-Hartman static int __init binder_init(void)
6965777783e0SGreg Kroah-Hartman {
6966777783e0SGreg Kroah-Hartman int ret;
69675b9633afSChristian Brauner char *device_name, *device_tmp;
6968ac4812c5SMartijn Coenen struct binder_device *device;
6969ac4812c5SMartijn Coenen struct hlist_node *tmp;
69705b9633afSChristian Brauner char *device_names = NULL;
697151b080a4SWang Ming const struct binder_debugfs_entry *db_entry;
6972777783e0SGreg Kroah-Hartman
6973533dfb25STetsuo Handa ret = binder_alloc_shrinker_init();
6974533dfb25STetsuo Handa if (ret)
6975533dfb25STetsuo Handa return ret;
6976f2517eb7SSherry Yang
6977d99c7333STodd Kjos atomic_set(&binder_transaction_log.cur, ~0U);
6978d99c7333STodd Kjos atomic_set(&binder_transaction_log_failed.cur, ~0U);
6979d99c7333STodd Kjos
6980777783e0SGreg Kroah-Hartman binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6981b7e241bbSCarlos Llamas
6982b7e241bbSCarlos Llamas binder_for_each_debugfs_entry(db_entry)
6983b7e241bbSCarlos Llamas debugfs_create_file(db_entry->name,
6984b7e241bbSCarlos Llamas db_entry->mode,
6985b7e241bbSCarlos Llamas binder_debugfs_dir_entry_root,
6986b7e241bbSCarlos Llamas db_entry->data,
6987b7e241bbSCarlos Llamas db_entry->fops);
6988b7e241bbSCarlos Llamas
6989777783e0SGreg Kroah-Hartman binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6990777783e0SGreg Kroah-Hartman binder_debugfs_dir_entry_root);
6991ac4812c5SMartijn Coenen
6992ca2864c6SHridya Valsaraju if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
6993ca2864c6SHridya Valsaraju strcmp(binder_devices_param, "") != 0) {
6994ac4812c5SMartijn Coenen /*
6995ac4812c5SMartijn Coenen * Copy the module_parameter string, because we don't want to
6996ac4812c5SMartijn Coenen * tokenize it in-place.
6997ac4812c5SMartijn Coenen */
69986b6642daSRasmus Villemoes device_names = kstrdup(binder_devices_param, GFP_KERNEL);
6999ac4812c5SMartijn Coenen if (!device_names) {
7000ac4812c5SMartijn Coenen ret = -ENOMEM;
7001ac4812c5SMartijn Coenen goto err_alloc_device_names_failed;
7002ac4812c5SMartijn Coenen }
7003ac4812c5SMartijn Coenen
700422eb9476SChristian Brauner device_tmp = device_names;
700522eb9476SChristian Brauner while ((device_name = strsep(&device_tmp, ","))) {
7006ac4812c5SMartijn Coenen ret = init_binder_device(device_name);
7007ac4812c5SMartijn Coenen if (ret)
7008ac4812c5SMartijn Coenen goto err_init_binder_device_failed;
7009ac4812c5SMartijn Coenen }
7010793c8232SChristian Brauner }
7011ac4812c5SMartijn Coenen
70125b9633afSChristian Brauner ret = init_binderfs();
70135b9633afSChristian Brauner if (ret)
70145b9633afSChristian Brauner goto err_init_binder_device_failed;
7015ac4812c5SMartijn Coenen
7016ac4812c5SMartijn Coenen return ret;
7017ac4812c5SMartijn Coenen
7018ac4812c5SMartijn Coenen err_init_binder_device_failed:
7019ac4812c5SMartijn Coenen hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
7020ac4812c5SMartijn Coenen misc_deregister(&device->miscdev);
7021ac4812c5SMartijn Coenen hlist_del(&device->hlist);
7022ac4812c5SMartijn Coenen kfree(device);
7023ac4812c5SMartijn Coenen }
702422eb9476SChristian Brauner
702522eb9476SChristian Brauner kfree(device_names);
702622eb9476SChristian Brauner
7027ac4812c5SMartijn Coenen err_alloc_device_names_failed:
7028ac4812c5SMartijn Coenen debugfs_remove_recursive(binder_debugfs_dir_entry_root);
7029adb9743dSQi Zheng binder_alloc_shrinker_exit();
7030ac4812c5SMartijn Coenen
7031777783e0SGreg Kroah-Hartman return ret;
7032777783e0SGreg Kroah-Hartman }
7033777783e0SGreg Kroah-Hartman
7034777783e0SGreg Kroah-Hartman device_initcall(binder_init);
7035777783e0SGreg Kroah-Hartman
7036777783e0SGreg Kroah-Hartman #define CREATE_TRACE_POINTS
7037777783e0SGreg Kroah-Hartman #include "binder_trace.h"
7038777783e0SGreg Kroah-Hartman
7039777783e0SGreg Kroah-Hartman MODULE_LICENSE("GPL v2");
7040