blob: f4c772fd53a22bd80fe4f8eddedfd84ed25d4b05 [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Todd Kjos9630fe82017-06-29 12:02:00 -070018/*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
Martijn Coenen1b77e9d2017-08-31 10:04:18 +020031 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
Todd Kjos9630fe82017-06-29 12:02:00 -070035 * binder_inner_proc_lock() and binder_inner_proc_unlock()
36 * are used to acq/rel
37 *
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
40 *
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
43 *
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
49 * ...
50 */
51
Anmol Sarma56b468f2012-10-30 22:35:43 +053052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054#include <asm/cacheflush.h>
55#include <linux/fdtable.h>
56#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000057#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058#include <linux/fs.h>
59#include <linux/list.h>
60#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061#include <linux/module.h>
62#include <linux/mutex.h>
63#include <linux/nsproxy.h>
64#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090066#include <linux/rbtree.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010067#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010068#include <linux/sched/mm.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070069#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090070#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080071#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050072#include <linux/security.h>
Todd Kjos9630fe82017-06-29 12:02:00 -070073#include <linux/spinlock.h>
Sherry Yangb3f83132018-08-07 12:57:13 -070074#include <linux/ratelimit.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090075
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020076#include <uapi/linux/android/binder.h>
Martijn Coenen37b34412017-06-06 17:04:42 -070077#include <uapi/linux/sched/types.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070078#include "binder_alloc.h"
Christian Brauner34fbd922018-12-14 13:11:14 +010079#include "binder_internal.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070080#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090081
Todd Kjosc44b1232017-06-29 12:01:43 -070082static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static DEFINE_MUTEX(binder_deferred_lock);
84
Martijn Coenenac4812c2017-02-03 14:40:48 -080085static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070087static DEFINE_MUTEX(binder_procs_lock);
88
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070090static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090091
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070092static struct dentry *binder_debugfs_dir_entry_root;
93static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070094static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090095
Yangtao Li8b2246a2018-11-30 20:26:30 -050096static int proc_show(struct seq_file *m, void *unused);
97DEFINE_SHOW_ATTRIBUTE(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090098
99/* This is only defined in include/asm-arm/sizes.h */
100#ifndef SZ_1K
101#define SZ_1K 0x400
102#endif
103
104#ifndef SZ_4M
105#define SZ_4M 0x400000
106#endif
107
108#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
109
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900110enum {
111 BINDER_DEBUG_USER_ERROR = 1U << 0,
112 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
113 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
114 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
115 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
116 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
117 BINDER_DEBUG_READ_WRITE = 1U << 6,
118 BINDER_DEBUG_USER_REFS = 1U << 7,
119 BINDER_DEBUG_THREADS = 1U << 8,
120 BINDER_DEBUG_TRANSACTION = 1U << 9,
121 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
122 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
123 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700124 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjos9630fe82017-06-29 12:02:00 -0700125 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900126};
127static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
128 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
Harsh Shandilyac5b47d22017-12-22 19:37:02 +0530129module_param_named(debug_mask, binder_debug_mask, uint, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900130
Hridya Valsarajue8fb3932019-08-08 15:27:25 -0700131char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
Martijn Coenenac4812c2017-02-03 14:40:48 -0800132module_param_named(devices, binder_devices_param, charp, 0444);
133
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900134static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
135static int binder_stop_on_user_error;
136
137static int binder_set_stop_on_user_error(const char *val,
Kees Cook549b2032017-10-17 19:04:42 -0700138 const struct kernel_param *kp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900139{
140 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900141
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142 ret = param_set_int(val, kp);
143 if (binder_stop_on_user_error < 2)
144 wake_up(&binder_user_error_wait);
145 return ret;
146}
147module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
Harsh Shandilyac5b47d22017-12-22 19:37:02 +0530148 param_get_int, &binder_stop_on_user_error, 0644);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900149
150#define binder_debug(mask, x...) \
151 do { \
152 if (binder_debug_mask & mask) \
Sherry Yangb3f83132018-08-07 12:57:13 -0700153 pr_info_ratelimited(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900154 } while (0)
155
156#define binder_user_error(x...) \
157 do { \
158 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherry Yangb3f83132018-08-07 12:57:13 -0700159 pr_info_ratelimited(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900160 if (binder_stop_on_user_error) \
161 binder_stop_on_user_error = 2; \
162 } while (0)
163
Carlos Llamasea1d78b2022-04-29 23:56:41 +0000164#define binder_set_extended_error(ee, _id, _command, _param) \
165 do { \
166 (ee)->id = _id; \
167 (ee)->command = _command; \
168 (ee)->param = _param; \
169 } while (0)
170
Martijn Coenenfeba3902017-02-03 14:40:45 -0800171#define to_flat_binder_object(hdr) \
172 container_of(hdr, struct flat_binder_object, hdr)
173
174#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
175
Martijn Coenen79802402017-02-03 14:40:51 -0800176#define to_binder_buffer_object(hdr) \
177 container_of(hdr, struct binder_buffer_object, hdr)
178
Martijn Coenendef95c72017-02-03 14:40:52 -0800179#define to_binder_fd_array_object(hdr) \
180 container_of(hdr, struct binder_fd_array_object, hdr)
181
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900182enum binder_stat_types {
183 BINDER_STAT_PROC,
184 BINDER_STAT_THREAD,
185 BINDER_STAT_NODE,
186 BINDER_STAT_REF,
187 BINDER_STAT_DEATH,
188 BINDER_STAT_TRANSACTION,
189 BINDER_STAT_TRANSACTION_COMPLETE,
190 BINDER_STAT_COUNT
191};
192
193struct binder_stats {
Li Liaf1f9842022-11-23 12:16:54 -0800194 atomic_t br[_IOC_NR(BR_TRANSACTION_PENDING_FROZEN) + 1];
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700195 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
196 atomic_t obj_created[BINDER_STAT_COUNT];
197 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900198};
199
200static struct binder_stats binder_stats;
201
202static inline void binder_stats_deleted(enum binder_stat_types type)
203{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700204 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900205}
206
207static inline void binder_stats_created(enum binder_stat_types type)
208{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700209 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900210}
211
Hridya Valsaraju37413932019-09-03 09:16:54 -0700212struct binder_transaction_log binder_transaction_log;
213struct binder_transaction_log binder_transaction_log_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214
215static struct binder_transaction_log_entry *binder_transaction_log_add(
216 struct binder_transaction_log *log)
217{
218 struct binder_transaction_log_entry *e;
Todd Kjosd99c7332017-06-29 12:01:53 -0700219 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900220
Todd Kjosd99c7332017-06-29 12:01:53 -0700221 if (cur >= ARRAY_SIZE(log->entry))
Gustavo A. R. Silva10340bf2018-01-23 12:04:27 -0600222 log->full = true;
Todd Kjosd99c7332017-06-29 12:01:53 -0700223 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
224 WRITE_ONCE(e->debug_id_done, 0);
225 /*
226 * write-barrier to synchronize access to e->debug_id_done.
227 * We make sure the initialized 0 value is seen before
228 * memset() other fields are zeroed by memset.
229 */
230 smp_wmb();
231 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900232 return e;
233}
234
Todd Kjos72196392017-06-29 12:02:02 -0700235/**
236 * struct binder_work - work enqueued on a worklist
237 * @entry: node enqueued on list
238 * @type: type of work to be performed
239 *
240 * There are separate work lists for proc, thread, and node (async).
241 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900242struct binder_work {
243 struct list_head entry;
Todd Kjos72196392017-06-29 12:02:02 -0700244
Todd Kjosbe84da12020-10-09 16:24:55 -0700245 enum binder_work_type {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900246 BINDER_WORK_TRANSACTION = 1,
247 BINDER_WORK_TRANSACTION_COMPLETE,
Li Liaf1f9842022-11-23 12:16:54 -0800248 BINDER_WORK_TRANSACTION_PENDING,
Hang Lu4dbc1682021-04-09 17:40:46 +0800249 BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT,
Todd Kjos26549d12017-06-29 12:01:55 -0700250 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900251 BINDER_WORK_NODE,
252 BINDER_WORK_DEAD_BINDER,
253 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
254 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
255 } type;
256};
257
Todd Kjos26549d12017-06-29 12:01:55 -0700258struct binder_error {
259 struct binder_work work;
260 uint32_t cmd;
261};
262
Todd Kjos9630fe82017-06-29 12:02:00 -0700263/**
264 * struct binder_node - binder node bookkeeping
265 * @debug_id: unique ID for debugging
266 * (invariant after initialized)
267 * @lock: lock for node fields
268 * @work: worklist element for node work
Todd Kjos72196392017-06-29 12:02:02 -0700269 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700270 * @rb_node: element for proc->nodes tree
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700271 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700272 * @dead_node: element for binder_dead_nodes list
273 * (protected by binder_dead_nodes_lock)
274 * @proc: binder_proc that owns this node
275 * (invariant after initialized)
276 * @refs: list of references on this node
Todd Kjos673068e2017-06-29 12:02:03 -0700277 * (protected by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700278 * @internal_strong_refs: used to take strong references when
279 * initiating a transaction
Todd Kjosed297212017-06-29 12:02:01 -0700280 * (protected by @proc->inner_lock if @proc
281 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700282 * @local_weak_refs: weak user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700283 * (protected by @proc->inner_lock if @proc
284 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700285 * @local_strong_refs: strong user refs from local process
Todd Kjosed297212017-06-29 12:02:01 -0700286 * (protected by @proc->inner_lock if @proc
287 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700288 * @tmp_refs: temporary kernel refs
Todd Kjosed297212017-06-29 12:02:01 -0700289 * (protected by @proc->inner_lock while @proc
290 * is valid, and by binder_dead_nodes_lock
291 * if @proc is NULL. During inc/dec and node release
292 * it is also protected by @lock to provide safety
293 * as the node dies and @proc becomes NULL)
Todd Kjos9630fe82017-06-29 12:02:00 -0700294 * @ptr: userspace pointer for node
295 * (invariant, no lock needed)
296 * @cookie: userspace cookie for node
297 * (invariant, no lock needed)
298 * @has_strong_ref: userspace notified of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700299 * (protected by @proc->inner_lock if @proc
300 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700301 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjosed297212017-06-29 12:02:01 -0700302 * (protected by @proc->inner_lock if @proc
303 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700304 * @has_weak_ref: userspace notified of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700305 * (protected by @proc->inner_lock if @proc
306 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700307 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjosed297212017-06-29 12:02:01 -0700308 * (protected by @proc->inner_lock if @proc
309 * and by @lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700310 * @has_async_transaction: async transaction to node in progress
Todd Kjos673068e2017-06-29 12:02:03 -0700311 * (protected by @lock)
Martijn Coenen69308b32017-06-07 09:29:14 -0700312 * @sched_policy: minimum scheduling policy for node
313 * (invariant after initialized)
Todd Kjos9630fe82017-06-29 12:02:00 -0700314 * @accept_fds: file descriptor operations supported for node
315 * (invariant after initialized)
316 * @min_priority: minimum scheduling priority
317 * (invariant after initialized)
Martijn Coenenfb92c342017-06-23 10:13:43 -0700318 * @inherit_rt: inherit RT scheduling policy from caller
Todd Kjos5312a992019-01-14 09:10:21 -0800319 * @txn_security_ctx: require sender's security context
Martijn Coenenfb92c342017-06-23 10:13:43 -0700320 * (invariant after initialized)
Todd Kjos9630fe82017-06-29 12:02:00 -0700321 * @async_todo: list of async work items
Todd Kjos72196392017-06-29 12:02:02 -0700322 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700323 *
324 * Bookkeeping structure for binder nodes.
325 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900326struct binder_node {
327 int debug_id;
Todd Kjos9630fe82017-06-29 12:02:00 -0700328 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900329 struct binder_work work;
330 union {
331 struct rb_node rb_node;
332 struct hlist_node dead_node;
333 };
334 struct binder_proc *proc;
335 struct hlist_head refs;
336 int internal_strong_refs;
337 int local_weak_refs;
338 int local_strong_refs;
Todd Kjosadc18842017-06-29 12:01:59 -0700339 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800340 binder_uintptr_t ptr;
341 binder_uintptr_t cookie;
Todd Kjosed297212017-06-29 12:02:01 -0700342 struct {
343 /*
344 * bitfield elements protected by
345 * proc inner_lock
346 */
347 u8 has_strong_ref:1;
348 u8 pending_strong_ref:1;
349 u8 has_weak_ref:1;
350 u8 pending_weak_ref:1;
351 };
352 struct {
353 /*
354 * invariant after initialization
355 */
Martijn Coenen69308b32017-06-07 09:29:14 -0700356 u8 sched_policy:2;
Martijn Coenenfb92c342017-06-23 10:13:43 -0700357 u8 inherit_rt:1;
Todd Kjosed297212017-06-29 12:02:01 -0700358 u8 accept_fds:1;
Todd Kjos5312a992019-01-14 09:10:21 -0800359 u8 txn_security_ctx:1;
Todd Kjosed297212017-06-29 12:02:01 -0700360 u8 min_priority;
361 };
362 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900363 struct list_head async_todo;
364};
365
366struct binder_ref_death {
Todd Kjos72196392017-06-29 12:02:02 -0700367 /**
368 * @work: worklist element for death notifications
369 * (protected by inner_lock of the proc that
370 * this ref belongs to)
371 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900372 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800373 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900374};
375
Todd Kjos372e3142017-06-29 12:01:58 -0700376/**
377 * struct binder_ref_data - binder_ref counts and id
378 * @debug_id: unique ID for the ref
379 * @desc: unique userspace handle for ref
380 * @strong: strong ref count (debugging only if not locked)
381 * @weak: weak ref count (debugging only if not locked)
382 *
383 * Structure to hold ref count and ref id information. Since
384 * the actual ref can only be accessed with a lock, this structure
385 * is used to return information about the ref to callers of
386 * ref inc/dec functions.
387 */
388struct binder_ref_data {
389 int debug_id;
390 uint32_t desc;
391 int strong;
392 int weak;
393};
394
395/**
396 * struct binder_ref - struct to track references on nodes
397 * @data: binder_ref_data containing id, handle, and current refcounts
398 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
399 * @rb_node_node: node for lookup by @node in proc's rb_tree
400 * @node_entry: list entry for node->refs list in target node
Todd Kjos673068e2017-06-29 12:02:03 -0700401 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700402 * @proc: binder_proc containing ref
403 * @node: binder_node of target node. When cleaning up a
404 * ref for deletion in binder_cleanup_ref, a non-NULL
405 * @node indicates the node must be freed
406 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenab51ec62017-06-29 12:02:10 -0700407 * (protected by @node->lock)
Todd Kjos372e3142017-06-29 12:01:58 -0700408 *
409 * Structure to track references from procA to target node (on procB). This
410 * structure is unsafe to access without holding @proc->outer_lock.
411 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900412struct binder_ref {
413 /* Lookups needed: */
414 /* node + proc => ref (transaction) */
415 /* desc + proc => ref (transaction, inc/dec ref) */
416 /* node => refs + procs (proc exit) */
Todd Kjos372e3142017-06-29 12:01:58 -0700417 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900418 struct rb_node rb_node_desc;
419 struct rb_node rb_node_node;
420 struct hlist_node node_entry;
421 struct binder_proc *proc;
422 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900423 struct binder_ref_death *death;
424};
425
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900426enum binder_deferred_state {
427 BINDER_DEFERRED_PUT_FILES = 0x01,
428 BINDER_DEFERRED_FLUSH = 0x02,
429 BINDER_DEFERRED_RELEASE = 0x04,
430};
431
Todd Kjos9630fe82017-06-29 12:02:00 -0700432/**
Martijn Coenen37b34412017-06-06 17:04:42 -0700433 * struct binder_priority - scheduler policy and priority
434 * @sched_policy scheduler policy
435 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
436 *
437 * The binder driver supports inheriting the following scheduler policies:
438 * SCHED_NORMAL
439 * SCHED_BATCH
440 * SCHED_FIFO
441 * SCHED_RR
442 */
443struct binder_priority {
444 unsigned int sched_policy;
445 int prio;
446};
447
448/**
Todd Kjos9630fe82017-06-29 12:02:00 -0700449 * struct binder_proc - binder process bookkeeping
450 * @proc_node: element for binder_procs list
451 * @threads: rbtree of binder_threads in this proc
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700452 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700453 * @nodes: rbtree of binder nodes associated with
454 * this proc ordered by node->ptr
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700455 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700456 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos2c1838d2017-06-29 12:02:08 -0700457 * (protected by @outer_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700458 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos2c1838d2017-06-29 12:02:08 -0700459 * (protected by @outer_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200460 * @waiting_threads: threads currently waiting for proc work
461 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700462 * @pid PID of group_leader of process
463 * (invariant after initialized)
464 * @tsk task_struct for group_leader of process
465 * (invariant after initialized)
466 * @files files_struct for process
Todd Kjos029876d2017-11-10 15:30:27 -0800467 * (protected by @files_lock)
468 * @files_lock mutex to protect @files
Todd Kjosae4a7b92021-10-12 09:56:12 -0700469 * @cred struct cred associated with the `struct file`
470 * in binder_open()
471 * (invariant after initialized)
Todd Kjos9630fe82017-06-29 12:02:00 -0700472 * @deferred_work_node: element for binder_deferred_list
473 * (protected by binder_deferred_lock)
474 * @deferred_work: bitmap of deferred work to perform
475 * (protected by binder_deferred_lock)
Marco Ballesio620ede42021-03-09 18:18:56 -0800476 * @outstanding_txns: number of transactions to be transmitted before
477 * processes in freeze_wait are woken up
478 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700479 * @is_dead: process is dead and awaiting free
480 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700481 * (protected by @inner_lock)
Marco Ballesio45490402020-09-10 13:39:20 -0700482 * @sync_recv: process received sync transactions since last frozen
Li Li503f84d2021-09-07 16:12:53 -0700483 * bit 0: received sync transaction after being frozen
484 * bit 1: new pending sync transaction during freezing
Marco Ballesio45490402020-09-10 13:39:20 -0700485 * (protected by @inner_lock)
486 * @async_recv: process received async transactions since last frozen
Marco Ballesio620ede42021-03-09 18:18:56 -0800487 * (protected by @inner_lock)
488 * @freeze_wait: waitqueue of processes waiting for all outstanding
489 * transactions to be processed
490 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700491 * @todo: list of work for this process
Todd Kjos72196392017-06-29 12:02:02 -0700492 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700493 * @stats: per-process binder statistics
494 * (atomics, no lock needed)
495 * @delivered_death: list of delivered death notification
Todd Kjos72196392017-06-29 12:02:02 -0700496 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700497 * @max_threads: cap on number of binder threads
Todd Kjosb3e68612017-06-29 12:02:07 -0700498 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700499 * @requested_threads: number of binder threads requested but not
500 * yet started. In current implementation, can
501 * only be 0 or 1.
Todd Kjosb3e68612017-06-29 12:02:07 -0700502 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700503 * @requested_threads_started: number binder threads started
Todd Kjosb3e68612017-06-29 12:02:07 -0700504 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700505 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700506 * (protected by @inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700507 * @default_priority: default scheduler priority
508 * (invariant after initialized)
509 * @debugfs_entry: debugfs node
510 * @alloc: binder allocator bookkeeping
511 * @context: binder_context for this proc
512 * (invariant after initialized)
513 * @inner_lock: can nest under outer_lock and/or node lock
514 * @outer_lock: no nesting under innor or node lock
515 * Lock order: 1) outer, 2) node, 3) inner
Hridya Valsaraju1405bf22019-09-03 09:16:55 -0700516 * @binderfs_entry: process-specific binderfs log file
Hang Lu4dbc1682021-04-09 17:40:46 +0800517 * @oneway_spam_detection_enabled: process enabled oneway spam detection
518 * or not
Todd Kjos9630fe82017-06-29 12:02:00 -0700519 *
520 * Bookkeeping structure for binder processes
521 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900522struct binder_proc {
523 struct hlist_node proc_node;
524 struct rb_root threads;
525 struct rb_root nodes;
526 struct rb_root refs_by_desc;
527 struct rb_root refs_by_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200528 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900529 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900530 struct task_struct *tsk;
531 struct files_struct *files;
Todd Kjos029876d2017-11-10 15:30:27 -0800532 struct mutex files_lock;
Todd Kjosae4a7b92021-10-12 09:56:12 -0700533 const struct cred *cred;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900534 struct hlist_node deferred_work_node;
535 int deferred_work;
Marco Ballesio620ede42021-03-09 18:18:56 -0800536 int outstanding_txns;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700537 bool is_dead;
Marco Ballesio620ede42021-03-09 18:18:56 -0800538 bool is_frozen;
Marco Ballesio45490402020-09-10 13:39:20 -0700539 bool sync_recv;
540 bool async_recv;
Marco Ballesio620ede42021-03-09 18:18:56 -0800541 wait_queue_head_t freeze_wait;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900542
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900543 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900544 struct binder_stats stats;
545 struct list_head delivered_death;
Carlos Llamasf642f362024-04-21 17:37:49 +0000546 u32 max_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900547 int requested_threads;
548 int requested_threads_started;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700549 int tmp_ref;
Martijn Coenen37b34412017-06-06 17:04:42 -0700550 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700551 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700552 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800553 struct binder_context *context;
Todd Kjos9630fe82017-06-29 12:02:00 -0700554 spinlock_t inner_lock;
555 spinlock_t outer_lock;
Hridya Valsaraju1405bf22019-09-03 09:16:55 -0700556 struct dentry *binderfs_entry;
Hang Lu4dbc1682021-04-09 17:40:46 +0800557 bool oneway_spam_detection_enabled;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900558};
559
560enum {
561 BINDER_LOOPER_STATE_REGISTERED = 0x01,
562 BINDER_LOOPER_STATE_ENTERED = 0x02,
563 BINDER_LOOPER_STATE_EXITED = 0x04,
564 BINDER_LOOPER_STATE_INVALID = 0x08,
565 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200566 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900567};
568
Todd Kjos9630fe82017-06-29 12:02:00 -0700569/**
570 * struct binder_thread - binder thread bookkeeping
571 * @proc: binder process for this thread
572 * (invariant after initialization)
573 * @rb_node: element for proc->threads rbtree
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700574 * (protected by @proc->inner_lock)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200575 * @waiting_thread_node: element for @proc->waiting_threads list
576 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700577 * @pid: PID for this thread
578 * (invariant after initialization)
579 * @looper: bitmap of looping state
580 * (only accessed by this thread)
581 * @looper_needs_return: looping thread needs to exit driver
582 * (no lock needed)
583 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700584 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700585 * @todo: list of work to do for this thread
Todd Kjos72196392017-06-29 12:02:02 -0700586 * (protected by @proc->inner_lock)
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200587 * @process_todo: whether work in @todo should be processed
588 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700589 * @return_error: transaction errors reported by this thread
590 * (only accessed by this thread)
591 * @reply_error: transaction errors reported by target thread
Martijn Coenen0b89d692017-06-29 12:02:06 -0700592 * (protected by @proc->inner_lock)
Carlos Llamasea1d78b2022-04-29 23:56:41 +0000593 * @ee: extended error information from this thread
594 * (protected by @proc->inner_lock)
Todd Kjos9630fe82017-06-29 12:02:00 -0700595 * @wait: wait queue for thread work
596 * @stats: per-thread statistics
597 * (atomics, no lock needed)
598 * @tmp_ref: temporary reference to indicate thread is in use
599 * (atomic since @proc->inner_lock cannot
600 * always be acquired)
601 * @is_dead: thread is dead and awaiting free
602 * when outstanding transactions are cleaned up
Todd Kjos7bd7b0e2017-06-29 12:02:05 -0700603 * (protected by @proc->inner_lock)
Martijn Coenen7a6edeb2017-06-07 10:02:12 -0700604 * @task: struct task_struct for this thread
Todd Kjos9630fe82017-06-29 12:02:00 -0700605 *
606 * Bookkeeping structure for binder threads.
607 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608struct binder_thread {
609 struct binder_proc *proc;
610 struct rb_node rb_node;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +0200611 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900612 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700613 int looper; /* only modified by this thread */
614 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900615 struct binder_transaction *transaction_stack;
616 struct list_head todo;
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200617 bool process_todo;
Todd Kjos26549d12017-06-29 12:01:55 -0700618 struct binder_error return_error;
619 struct binder_error reply_error;
Carlos Llamasea1d78b2022-04-29 23:56:41 +0000620 struct binder_extended_error ee;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900621 wait_queue_head_t wait;
622 struct binder_stats stats;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700623 atomic_t tmp_ref;
624 bool is_dead;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -0700625 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900626};
627
628struct binder_transaction {
629 int debug_id;
630 struct binder_work work;
631 struct binder_thread *from;
632 struct binder_transaction *from_parent;
633 struct binder_proc *to_proc;
634 struct binder_thread *to_thread;
635 struct binder_transaction *to_parent;
636 unsigned need_reply:1;
637 /* unsigned is_dead:1; */ /* not used at the moment */
638
639 struct binder_buffer *buffer;
640 unsigned int code;
641 unsigned int flags;
Martijn Coenen37b34412017-06-06 17:04:42 -0700642 struct binder_priority priority;
643 struct binder_priority saved_priority;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -0700644 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600645 kuid_t sender_euid;
Todd Kjos5312a992019-01-14 09:10:21 -0800646 binder_uintptr_t security_ctx;
Todd Kjos7a4408c2017-06-29 12:01:57 -0700647 /**
648 * @lock: protects @from, @to_proc, and @to_thread
649 *
650 * @from, @to_proc, and @to_thread can be set to NULL
651 * during thread teardown
652 */
653 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900654};
655
Todd Kjos9630fe82017-06-29 12:02:00 -0700656/**
Todd Kjos5088f132019-02-08 10:35:16 -0800657 * struct binder_object - union of flat binder object types
658 * @hdr: generic object header
659 * @fbo: binder object (nodes and refs)
660 * @fdo: file descriptor object
661 * @bbo: binder buffer pointer
662 * @fdao: file descriptor array
663 *
664 * Used for type-independent object copies
665 */
666struct binder_object {
667 union {
668 struct binder_object_header hdr;
669 struct flat_binder_object fbo;
670 struct binder_fd_object fdo;
671 struct binder_buffer_object bbo;
672 struct binder_fd_array_object fdao;
673 };
674};
675
676/**
Todd Kjos9630fe82017-06-29 12:02:00 -0700677 * binder_proc_lock() - Acquire outer lock for given binder_proc
678 * @proc: struct binder_proc to acquire
679 *
680 * Acquires proc->outer_lock. Used to protect binder_ref
681 * structures associated with the given proc.
682 */
683#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
684static void
685_binder_proc_lock(struct binder_proc *proc, int line)
686{
687 binder_debug(BINDER_DEBUG_SPINLOCKS,
688 "%s: line=%d\n", __func__, line);
689 spin_lock(&proc->outer_lock);
690}
691
692/**
693 * binder_proc_unlock() - Release spinlock for given binder_proc
Randy Dunlap63886072023-01-17 10:37:45 -0800694 * @proc: struct binder_proc to acquire
Todd Kjos9630fe82017-06-29 12:02:00 -0700695 *
696 * Release lock acquired via binder_proc_lock()
697 */
Randy Dunlap63886072023-01-17 10:37:45 -0800698#define binder_proc_unlock(proc) _binder_proc_unlock(proc, __LINE__)
Todd Kjos9630fe82017-06-29 12:02:00 -0700699static void
700_binder_proc_unlock(struct binder_proc *proc, int line)
701{
702 binder_debug(BINDER_DEBUG_SPINLOCKS,
703 "%s: line=%d\n", __func__, line);
704 spin_unlock(&proc->outer_lock);
705}
706
707/**
708 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
709 * @proc: struct binder_proc to acquire
710 *
711 * Acquires proc->inner_lock. Used to protect todo lists
712 */
713#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
714static void
715_binder_inner_proc_lock(struct binder_proc *proc, int line)
716{
717 binder_debug(BINDER_DEBUG_SPINLOCKS,
718 "%s: line=%d\n", __func__, line);
719 spin_lock(&proc->inner_lock);
720}
721
722/**
723 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
724 * @proc: struct binder_proc to acquire
725 *
726 * Release lock acquired via binder_inner_proc_lock()
727 */
728#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
729static void
730_binder_inner_proc_unlock(struct binder_proc *proc, int line)
731{
732 binder_debug(BINDER_DEBUG_SPINLOCKS,
733 "%s: line=%d\n", __func__, line);
734 spin_unlock(&proc->inner_lock);
735}
736
737/**
738 * binder_node_lock() - Acquire spinlock for given binder_node
739 * @node: struct binder_node to acquire
740 *
741 * Acquires node->lock. Used to protect binder_node fields
742 */
743#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
744static void
745_binder_node_lock(struct binder_node *node, int line)
746{
747 binder_debug(BINDER_DEBUG_SPINLOCKS,
748 "%s: line=%d\n", __func__, line);
749 spin_lock(&node->lock);
750}
751
752/**
753 * binder_node_unlock() - Release spinlock for given binder_proc
754 * @node: struct binder_node to acquire
755 *
756 * Release lock acquired via binder_node_lock()
757 */
758#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
759static void
760_binder_node_unlock(struct binder_node *node, int line)
761{
762 binder_debug(BINDER_DEBUG_SPINLOCKS,
763 "%s: line=%d\n", __func__, line);
764 spin_unlock(&node->lock);
765}
766
Todd Kjos673068e2017-06-29 12:02:03 -0700767/**
768 * binder_node_inner_lock() - Acquire node and inner locks
769 * @node: struct binder_node to acquire
770 *
771 * Acquires node->lock. If node->proc also acquires
772 * proc->inner_lock. Used to protect binder_node fields
773 */
774#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
775static void
776_binder_node_inner_lock(struct binder_node *node, int line)
777{
778 binder_debug(BINDER_DEBUG_SPINLOCKS,
779 "%s: line=%d\n", __func__, line);
780 spin_lock(&node->lock);
781 if (node->proc)
782 binder_inner_proc_lock(node->proc);
783}
784
785/**
Randy Dunlap63886072023-01-17 10:37:45 -0800786 * binder_node_inner_unlock() - Release node and inner locks
Todd Kjos673068e2017-06-29 12:02:03 -0700787 * @node: struct binder_node to acquire
788 *
789 * Release lock acquired via binder_node_lock()
790 */
791#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
792static void
793_binder_node_inner_unlock(struct binder_node *node, int line)
794{
795 struct binder_proc *proc = node->proc;
796
797 binder_debug(BINDER_DEBUG_SPINLOCKS,
798 "%s: line=%d\n", __func__, line);
799 if (proc)
800 binder_inner_proc_unlock(proc);
801 spin_unlock(&node->lock);
802}
803
Todd Kjos72196392017-06-29 12:02:02 -0700804static bool binder_worklist_empty_ilocked(struct list_head *list)
805{
806 return list_empty(list);
807}
808
809/**
810 * binder_worklist_empty() - Check if no items on the work list
811 * @proc: binder_proc associated with list
812 * @list: list to check
813 *
814 * Return: true if there are no items on list, else false
815 */
816static bool binder_worklist_empty(struct binder_proc *proc,
817 struct list_head *list)
818{
819 bool ret;
820
821 binder_inner_proc_lock(proc);
822 ret = binder_worklist_empty_ilocked(list);
823 binder_inner_proc_unlock(proc);
824 return ret;
825}
826
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200827/**
828 * binder_enqueue_work_ilocked() - Add an item to the work list
829 * @work: struct binder_work to add to list
830 * @target_list: list to add work to
831 *
832 * Adds the work to the specified list. Asserts that work
833 * is not already on a list.
834 *
835 * Requires the proc->inner_lock to be held.
836 */
Todd Kjos72196392017-06-29 12:02:02 -0700837static void
838binder_enqueue_work_ilocked(struct binder_work *work,
839 struct list_head *target_list)
840{
841 BUG_ON(target_list == NULL);
842 BUG_ON(work->entry.next && !list_empty(&work->entry));
843 list_add_tail(&work->entry, target_list);
844}
845
846/**
Martijn Coenenad4cc172017-11-13 09:55:21 +0100847 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200848 * @thread: thread to queue work to
Todd Kjos72196392017-06-29 12:02:02 -0700849 * @work: struct binder_work to add to list
Todd Kjos72196392017-06-29 12:02:02 -0700850 *
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200851 * Adds the work to the todo list of the thread. Doesn't set the process_todo
852 * flag, which means that (if it wasn't already set) the thread will go to
853 * sleep without handling this work when it calls read.
854 *
855 * Requires the proc->inner_lock to be held.
Todd Kjos72196392017-06-29 12:02:02 -0700856 */
857static void
Martijn Coenenad4cc172017-11-13 09:55:21 +0100858binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
859 struct binder_work *work)
Todd Kjos72196392017-06-29 12:02:02 -0700860{
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200861 binder_enqueue_work_ilocked(work, &thread->todo);
862}
863
864/**
865 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
866 * @thread: thread to queue work to
867 * @work: struct binder_work to add to list
868 *
869 * Adds the work to the todo list of the thread, and enables processing
870 * of the todo queue.
871 *
872 * Requires the proc->inner_lock to be held.
873 */
874static void
875binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
876 struct binder_work *work)
877{
878 binder_enqueue_work_ilocked(work, &thread->todo);
Carlos Llamasaf4fc462024-01-31 21:53:46 +0000879
880 /* (e)poll-based threads require an explicit wakeup signal when
881 * queuing their own work; they rely on these events to consume
882 * messages without I/O block. Without it, threads risk waiting
883 * indefinitely without handling the work.
884 */
885 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
886 thread->pid == current->pid && !thread->process_todo)
887 wake_up_interruptible_sync(&thread->wait);
888
Martijn Coenen8e65ef22017-10-19 15:04:46 +0200889 thread->process_todo = true;
890}
891
892/**
893 * binder_enqueue_thread_work() - Add an item to the thread work list
894 * @thread: thread to queue work to
895 * @work: struct binder_work to add to list
896 *
897 * Adds the work to the todo list of the thread, and enables processing
898 * of the todo queue.
899 */
900static void
901binder_enqueue_thread_work(struct binder_thread *thread,
902 struct binder_work *work)
903{
904 binder_inner_proc_lock(thread->proc);
905 binder_enqueue_thread_work_ilocked(thread, work);
906 binder_inner_proc_unlock(thread->proc);
Todd Kjos72196392017-06-29 12:02:02 -0700907}
908
909static void
910binder_dequeue_work_ilocked(struct binder_work *work)
911{
912 list_del_init(&work->entry);
913}
914
915/**
916 * binder_dequeue_work() - Removes an item from the work list
917 * @proc: binder_proc associated with list
918 * @work: struct binder_work to remove from list
919 *
920 * Removes the specified work item from whatever list it is on.
921 * Can safely be called if work is not on any list.
922 */
923static void
924binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
925{
926 binder_inner_proc_lock(proc);
927 binder_dequeue_work_ilocked(work);
928 binder_inner_proc_unlock(proc);
929}
930
931static struct binder_work *binder_dequeue_work_head_ilocked(
932 struct list_head *list)
933{
934 struct binder_work *w;
935
936 w = list_first_entry_or_null(list, struct binder_work, entry);
937 if (w)
938 list_del_init(&w->entry);
939 return w;
940}
941
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900942static void
943binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos7a4408c2017-06-29 12:01:57 -0700944static void binder_free_thread(struct binder_thread *thread);
945static void binder_free_proc(struct binder_proc *proc);
Todd Kjosda0fa9e2017-06-29 12:02:04 -0700946static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900947
Sachin Kamatefde99c2012-08-17 16:39:36 +0530948static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900949{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900950 unsigned long rlim_cur;
951 unsigned long irqs;
Todd Kjos029876d2017-11-10 15:30:27 -0800952 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900953
Todd Kjos029876d2017-11-10 15:30:27 -0800954 mutex_lock(&proc->files_lock);
955 if (proc->files == NULL) {
956 ret = -ESRCH;
957 goto err;
958 }
959 if (!lock_task_sighand(proc->tsk, &irqs)) {
960 ret = -EMFILE;
961 goto err;
962 }
Al Virodcfadfa2012-08-12 17:27:30 -0400963 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
964 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900965
Todd Kjos029876d2017-11-10 15:30:27 -0800966 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
967err:
968 mutex_unlock(&proc->files_lock);
969 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900970}
971
972/*
973 * copied from fd_install
974 */
975static void task_fd_install(
976 struct binder_proc *proc, unsigned int fd, struct file *file)
977{
Todd Kjos029876d2017-11-10 15:30:27 -0800978 mutex_lock(&proc->files_lock);
Al Virof869e8a2012-08-15 21:06:33 -0400979 if (proc->files)
980 __fd_install(proc->files, fd, file);
Todd Kjos029876d2017-11-10 15:30:27 -0800981 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900982}
983
984/*
985 * copied from sys_close
986 */
987static long task_close_fd(struct binder_proc *proc, unsigned int fd)
988{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900989 int retval;
990
Todd Kjos029876d2017-11-10 15:30:27 -0800991 mutex_lock(&proc->files_lock);
992 if (proc->files == NULL) {
993 retval = -ESRCH;
994 goto err;
995 }
Al Viro483ce1d2012-08-19 12:04:24 -0400996 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900997 /* can't restart close syscall because file table entry was cleared */
998 if (unlikely(retval == -ERESTARTSYS ||
999 retval == -ERESTARTNOINTR ||
1000 retval == -ERESTARTNOHAND ||
1001 retval == -ERESTART_RESTARTBLOCK))
1002 retval = -EINTR;
Todd Kjos029876d2017-11-10 15:30:27 -08001003err:
1004 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001005 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001006}
1007
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001008static bool binder_has_work_ilocked(struct binder_thread *thread,
1009 bool do_proc_work)
1010{
Martijn Coenen8e65ef22017-10-19 15:04:46 +02001011 return thread->process_todo ||
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001012 thread->looper_need_return ||
1013 (do_proc_work &&
1014 !binder_worklist_empty_ilocked(&thread->proc->todo));
1015}
1016
1017static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1018{
1019 bool has_work;
1020
1021 binder_inner_proc_lock(thread->proc);
1022 has_work = binder_has_work_ilocked(thread, do_proc_work);
1023 binder_inner_proc_unlock(thread->proc);
1024
1025 return has_work;
1026}
1027
1028static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1029{
1030 return !thread->transaction_stack &&
1031 binder_worklist_empty_ilocked(&thread->todo) &&
1032 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1033 BINDER_LOOPER_STATE_REGISTERED));
1034}
1035
1036static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1037 bool sync)
1038{
1039 struct rb_node *n;
1040 struct binder_thread *thread;
1041
1042 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1043 thread = rb_entry(n, struct binder_thread, rb_node);
1044 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1045 binder_available_for_proc_work_ilocked(thread)) {
1046 if (sync)
1047 wake_up_interruptible_sync(&thread->wait);
1048 else
1049 wake_up_interruptible(&thread->wait);
1050 }
1051 }
1052}
1053
Martijn Coenen408c68b2017-08-31 10:04:19 +02001054/**
1055 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1056 * @proc: process to select a thread from
1057 *
1058 * Note that calling this function moves the thread off the waiting_threads
1059 * list, so it can only be woken up by the caller of this function, or a
1060 * signal. Therefore, callers *should* always wake up the thread this function
1061 * returns.
1062 *
1063 * Return: If there's a thread currently waiting for process work,
1064 * returns that thread. Otherwise returns NULL.
1065 */
1066static struct binder_thread *
1067binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001068{
1069 struct binder_thread *thread;
1070
Martijn Coenen858b2712017-08-31 10:04:26 +02001071 assert_spin_locked(&proc->inner_lock);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001072 thread = list_first_entry_or_null(&proc->waiting_threads,
1073 struct binder_thread,
1074 waiting_thread_node);
1075
Martijn Coenen408c68b2017-08-31 10:04:19 +02001076 if (thread)
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001077 list_del_init(&thread->waiting_thread_node);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001078
1079 return thread;
1080}
1081
1082/**
1083 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1084 * @proc: process to wake up a thread in
1085 * @thread: specific thread to wake-up (may be NULL)
1086 * @sync: whether to do a synchronous wake-up
1087 *
1088 * This function wakes up a thread in the @proc process.
1089 * The caller may provide a specific thread to wake-up in
1090 * the @thread parameter. If @thread is NULL, this function
1091 * will wake up threads that have called poll().
1092 *
1093 * Note that for this function to work as expected, callers
1094 * should first call binder_select_thread() to find a thread
1095 * to handle the work (if they don't have a thread already),
1096 * and pass the result into the @thread parameter.
1097 */
1098static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1099 struct binder_thread *thread,
1100 bool sync)
1101{
Martijn Coenen858b2712017-08-31 10:04:26 +02001102 assert_spin_locked(&proc->inner_lock);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001103
1104 if (thread) {
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02001105 if (sync)
1106 wake_up_interruptible_sync(&thread->wait);
1107 else
1108 wake_up_interruptible(&thread->wait);
1109 return;
1110 }
1111
1112 /* Didn't find a thread waiting for proc work; this can happen
1113 * in two scenarios:
1114 * 1. All threads are busy handling transactions
1115 * In that case, one of those threads should call back into
1116 * the kernel driver soon and pick up this work.
1117 * 2. Threads are using the (e)poll interface, in which case
1118 * they may be blocked on the waitqueue without having been
1119 * added to waiting_threads. For this case, we just iterate
1120 * over all threads not handling transaction work, and
1121 * wake them all up. We wake all because we don't know whether
1122 * a thread that called into (e)poll is handling non-binder
1123 * work currently.
1124 */
1125 binder_wakeup_poll_threads_ilocked(proc, sync);
1126}
1127
Martijn Coenen408c68b2017-08-31 10:04:19 +02001128static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1129{
1130 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1131
1132 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1133}
1134
Martijn Coenen37b34412017-06-06 17:04:42 -07001135static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001136{
Martijn Coenen37b34412017-06-06 17:04:42 -07001137 return policy == SCHED_FIFO || policy == SCHED_RR;
1138}
Seunghun Lee10f62862014-05-01 01:30:23 +09001139
Martijn Coenen37b34412017-06-06 17:04:42 -07001140static bool is_fair_policy(int policy)
1141{
1142 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1143}
1144
1145static bool binder_supported_policy(int policy)
1146{
1147 return is_fair_policy(policy) || is_rt_policy(policy);
1148}
1149
1150static int to_userspace_prio(int policy, int kernel_priority)
1151{
1152 if (is_fair_policy(policy))
1153 return PRIO_TO_NICE(kernel_priority);
1154 else
1155 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1156}
1157
1158static int to_kernel_prio(int policy, int user_priority)
1159{
1160 if (is_fair_policy(policy))
1161 return NICE_TO_PRIO(user_priority);
1162 else
1163 return MAX_USER_RT_PRIO - 1 - user_priority;
1164}
1165
Martijn Coenen22b061b2017-05-26 10:48:56 -07001166static void binder_do_set_priority(struct task_struct *task,
1167 struct binder_priority desired,
1168 bool verify)
Martijn Coenen37b34412017-06-06 17:04:42 -07001169{
1170 int priority; /* user-space prio value */
1171 bool has_cap_nice;
1172 unsigned int policy = desired.sched_policy;
1173
1174 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001175 return;
Martijn Coenen37b34412017-06-06 17:04:42 -07001176
1177 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1178
1179 priority = to_userspace_prio(policy, desired.prio);
1180
Martijn Coenen22b061b2017-05-26 10:48:56 -07001181 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen37b34412017-06-06 17:04:42 -07001182 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1183
1184 if (max_rtprio == 0) {
1185 policy = SCHED_NORMAL;
1186 priority = MIN_NICE;
1187 } else if (priority > max_rtprio) {
1188 priority = max_rtprio;
1189 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001190 }
Martijn Coenen37b34412017-06-06 17:04:42 -07001191
Martijn Coenen22b061b2017-05-26 10:48:56 -07001192 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen37b34412017-06-06 17:04:42 -07001193 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1194
1195 if (min_nice > MAX_NICE) {
1196 binder_user_error("%d RLIMIT_NICE not set\n",
1197 task->pid);
1198 return;
1199 } else if (priority < min_nice) {
1200 priority = min_nice;
1201 }
1202 }
1203
1204 if (policy != desired.sched_policy ||
1205 to_kernel_prio(policy, priority) != desired.prio)
1206 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1207 "%d: priority %d not allowed, using %d instead\n",
1208 task->pid, desired.prio,
1209 to_kernel_prio(policy, priority));
1210
Martijn Coenen67cf9712017-05-08 09:33:22 -07001211 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1212 to_kernel_prio(policy, priority),
1213 desired.prio);
1214
Martijn Coenen37b34412017-06-06 17:04:42 -07001215 /* Set the actual priority */
1216 if (task->policy != policy || is_rt_policy(policy)) {
1217 struct sched_param params;
1218
1219 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1220
1221 sched_setscheduler_nocheck(task,
1222 policy | SCHED_RESET_ON_FORK,
1223 &params);
1224 }
1225 if (is_fair_policy(policy))
1226 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001227}
1228
Martijn Coenen22b061b2017-05-26 10:48:56 -07001229static void binder_set_priority(struct task_struct *task,
1230 struct binder_priority desired)
1231{
1232 binder_do_set_priority(task, desired, /* verify = */ true);
1233}
1234
1235static void binder_restore_priority(struct task_struct *task,
1236 struct binder_priority desired)
1237{
1238 binder_do_set_priority(task, desired, /* verify = */ false);
1239}
1240
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07001241static void binder_transaction_priority(struct task_struct *task,
1242 struct binder_transaction *t,
Martijn Coenenfb92c342017-06-23 10:13:43 -07001243 struct binder_priority node_prio,
1244 bool inherit_rt)
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07001245{
Ganesh Mahendran9b608f42017-09-27 15:12:25 +08001246 struct binder_priority desired_prio = t->priority;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07001247
1248 if (t->set_priority_called)
1249 return;
1250
1251 t->set_priority_called = true;
1252 t->saved_priority.sched_policy = task->policy;
1253 t->saved_priority.prio = task->normal_prio;
1254
Martijn Coenenfb92c342017-06-23 10:13:43 -07001255 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1256 desired_prio.prio = NICE_TO_PRIO(0);
1257 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenfb92c342017-06-23 10:13:43 -07001258 }
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07001259
1260 if (node_prio.prio < t->priority.prio ||
1261 (node_prio.prio == t->priority.prio &&
1262 node_prio.sched_policy == SCHED_FIFO)) {
1263 /*
1264 * In case the minimum priority on the node is
1265 * higher (lower value), use that priority. If
1266 * the priority is the same, but the node uses
1267 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1268 * run unbounded, unlike SCHED_RR.
1269 */
1270 desired_prio = node_prio;
1271 }
1272
1273 binder_set_priority(task, desired_prio);
1274}
1275
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001276static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1277 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001278{
1279 struct rb_node *n = proc->nodes.rb_node;
1280 struct binder_node *node;
1281
Martijn Coenen858b2712017-08-31 10:04:26 +02001282 assert_spin_locked(&proc->inner_lock);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001283
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001284 while (n) {
1285 node = rb_entry(n, struct binder_node, rb_node);
1286
1287 if (ptr < node->ptr)
1288 n = n->rb_left;
1289 else if (ptr > node->ptr)
1290 n = n->rb_right;
Todd Kjosadc18842017-06-29 12:01:59 -07001291 else {
1292 /*
1293 * take an implicit weak reference
1294 * to ensure node stays alive until
1295 * call to binder_put_node()
1296 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001297 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001298 return node;
Todd Kjosadc18842017-06-29 12:01:59 -07001299 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001300 }
1301 return NULL;
1302}
1303
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001304static struct binder_node *binder_get_node(struct binder_proc *proc,
1305 binder_uintptr_t ptr)
1306{
1307 struct binder_node *node;
1308
1309 binder_inner_proc_lock(proc);
1310 node = binder_get_node_ilocked(proc, ptr);
1311 binder_inner_proc_unlock(proc);
1312 return node;
1313}
1314
1315static struct binder_node *binder_init_node_ilocked(
1316 struct binder_proc *proc,
1317 struct binder_node *new_node,
1318 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001319{
1320 struct rb_node **p = &proc->nodes.rb_node;
1321 struct rb_node *parent = NULL;
1322 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07001323 binder_uintptr_t ptr = fp ? fp->binder : 0;
1324 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1325 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen69308b32017-06-07 09:29:14 -07001326 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001327
Martijn Coenen858b2712017-08-31 10:04:26 +02001328 assert_spin_locked(&proc->inner_lock);
1329
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330 while (*p) {
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001331
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001332 parent = *p;
1333 node = rb_entry(parent, struct binder_node, rb_node);
1334
1335 if (ptr < node->ptr)
1336 p = &(*p)->rb_left;
1337 else if (ptr > node->ptr)
1338 p = &(*p)->rb_right;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001339 else {
1340 /*
1341 * A matching node is already in
1342 * the rb tree. Abandon the init
1343 * and return it.
1344 */
1345 binder_inc_node_tmpref_ilocked(node);
1346 return node;
1347 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001348 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001349 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001350 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosadc18842017-06-29 12:01:59 -07001351 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001352 rb_link_node(&node->rb_node, parent, p);
1353 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -07001354 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001355 node->proc = proc;
1356 node->ptr = ptr;
1357 node->cookie = cookie;
1358 node->work.type = BINDER_WORK_NODE;
Martijn Coenen69308b32017-06-07 09:29:14 -07001359 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendranff265052017-09-26 17:56:25 +08001360 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen69308b32017-06-07 09:29:14 -07001361 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1362 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjos673068e2017-06-29 12:02:03 -07001363 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenfb92c342017-06-23 10:13:43 -07001364 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjos5312a992019-01-14 09:10:21 -08001365 node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX);
Todd Kjos9630fe82017-06-29 12:02:00 -07001366 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001367 INIT_LIST_HEAD(&node->work.entry);
1368 INIT_LIST_HEAD(&node->async_todo);
1369 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001370 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001371 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001372 (u64)node->ptr, (u64)node->cookie);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07001373
1374 return node;
1375}
1376
1377static struct binder_node *binder_new_node(struct binder_proc *proc,
1378 struct flat_binder_object *fp)
1379{
1380 struct binder_node *node;
1381 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1382
1383 if (!new_node)
1384 return NULL;
1385 binder_inner_proc_lock(proc);
1386 node = binder_init_node_ilocked(proc, new_node, fp);
1387 binder_inner_proc_unlock(proc);
1388 if (node != new_node)
1389 /*
1390 * The node was already added by another thread
1391 */
1392 kfree(new_node);
1393
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001394 return node;
1395}
1396
Todd Kjosed297212017-06-29 12:02:01 -07001397static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001398{
Todd Kjosed297212017-06-29 12:02:01 -07001399 kfree(node);
1400 binder_stats_deleted(BINDER_STAT_NODE);
1401}
1402
Todd Kjos673068e2017-06-29 12:02:03 -07001403static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1404 int internal,
1405 struct list_head *target_list)
Todd Kjosed297212017-06-29 12:02:01 -07001406{
Todd Kjos673068e2017-06-29 12:02:03 -07001407 struct binder_proc *proc = node->proc;
1408
Martijn Coenen858b2712017-08-31 10:04:26 +02001409 assert_spin_locked(&node->lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001410 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001411 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001412 if (strong) {
1413 if (internal) {
1414 if (target_list == NULL &&
1415 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -08001416 !(node->proc &&
1417 node == node->proc->context->binder_context_mgr_node &&
1418 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301419 pr_err("invalid inc strong node for %d\n",
1420 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001421 return -EINVAL;
1422 }
1423 node->internal_strong_refs++;
1424 } else
1425 node->local_strong_refs++;
1426 if (!node->has_strong_ref && target_list) {
Todd Kjos72196392017-06-29 12:02:02 -07001427 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen8e65ef22017-10-19 15:04:46 +02001428 /*
1429 * Note: this function is the only place where we queue
1430 * directly to a thread->todo without using the
1431 * corresponding binder_enqueue_thread_work() helper
1432 * functions; in this case it's ok to not set the
1433 * process_todo flag, since we know this node work will
1434 * always be followed by other work that starts queue
1435 * processing: in case of synchronous transactions, a
1436 * BR_REPLY or BR_ERROR; in case of oneway
1437 * transactions, a BR_TRANSACTION_COMPLETE.
1438 */
Todd Kjos72196392017-06-29 12:02:02 -07001439 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001440 }
1441 } else {
1442 if (!internal)
1443 node->local_weak_refs++;
1444 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1445 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301446 pr_err("invalid inc weak node for %d\n",
1447 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001448 return -EINVAL;
1449 }
Martijn Coenen8e65ef22017-10-19 15:04:46 +02001450 /*
1451 * See comment above
1452 */
Todd Kjos72196392017-06-29 12:02:02 -07001453 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001454 }
1455 }
1456 return 0;
1457}
1458
Todd Kjosed297212017-06-29 12:02:01 -07001459static int binder_inc_node(struct binder_node *node, int strong, int internal,
1460 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001461{
Todd Kjosed297212017-06-29 12:02:01 -07001462 int ret;
1463
Todd Kjos673068e2017-06-29 12:02:03 -07001464 binder_node_inner_lock(node);
1465 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1466 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001467
1468 return ret;
1469}
1470
Todd Kjos673068e2017-06-29 12:02:03 -07001471static bool binder_dec_node_nilocked(struct binder_node *node,
1472 int strong, int internal)
Todd Kjosed297212017-06-29 12:02:01 -07001473{
1474 struct binder_proc *proc = node->proc;
1475
Martijn Coenen858b2712017-08-31 10:04:26 +02001476 assert_spin_locked(&node->lock);
Todd Kjosed297212017-06-29 12:02:01 -07001477 if (proc)
Martijn Coenen858b2712017-08-31 10:04:26 +02001478 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001479 if (strong) {
1480 if (internal)
1481 node->internal_strong_refs--;
1482 else
1483 node->local_strong_refs--;
1484 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjosed297212017-06-29 12:02:01 -07001485 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001486 } else {
1487 if (!internal)
1488 node->local_weak_refs--;
Todd Kjosadc18842017-06-29 12:01:59 -07001489 if (node->local_weak_refs || node->tmp_refs ||
1490 !hlist_empty(&node->refs))
Todd Kjosed297212017-06-29 12:02:01 -07001491 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001492 }
Todd Kjosed297212017-06-29 12:02:01 -07001493
1494 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001495 if (list_empty(&node->work.entry)) {
Todd Kjos72196392017-06-29 12:02:02 -07001496 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02001497 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 }
1499 } else {
1500 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosadc18842017-06-29 12:01:59 -07001501 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjosed297212017-06-29 12:02:01 -07001502 if (proc) {
Todd Kjos72196392017-06-29 12:02:02 -07001503 binder_dequeue_work_ilocked(&node->work);
1504 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001505 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301506 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001507 node->debug_id);
1508 } else {
Todd Kjos72196392017-06-29 12:02:02 -07001509 BUG_ON(!list_empty(&node->work.entry));
Todd Kjosc44b1232017-06-29 12:01:43 -07001510 spin_lock(&binder_dead_nodes_lock);
Todd Kjosed297212017-06-29 12:02:01 -07001511 /*
1512 * tmp_refs could have changed so
1513 * check it again
1514 */
1515 if (node->tmp_refs) {
1516 spin_unlock(&binder_dead_nodes_lock);
1517 return false;
1518 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001519 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07001520 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301522 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001523 node->debug_id);
1524 }
Todd Kjosed297212017-06-29 12:02:01 -07001525 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001526 }
1527 }
Todd Kjosed297212017-06-29 12:02:01 -07001528 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529}
1530
Todd Kjosed297212017-06-29 12:02:01 -07001531static void binder_dec_node(struct binder_node *node, int strong, int internal)
1532{
1533 bool free_node;
1534
Todd Kjos673068e2017-06-29 12:02:03 -07001535 binder_node_inner_lock(node);
1536 free_node = binder_dec_node_nilocked(node, strong, internal);
1537 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001538 if (free_node)
1539 binder_free_node(node);
1540}
1541
1542static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosadc18842017-06-29 12:01:59 -07001543{
1544 /*
1545 * No call to binder_inc_node() is needed since we
1546 * don't need to inform userspace of any changes to
1547 * tmp_refs
1548 */
1549 node->tmp_refs++;
1550}
1551
1552/**
Todd Kjosed297212017-06-29 12:02:01 -07001553 * binder_inc_node_tmpref() - take a temporary reference on node
1554 * @node: node to reference
1555 *
1556 * Take reference on node to prevent the node from being freed
1557 * while referenced only by a local variable. The inner lock is
1558 * needed to serialize with the node work on the queue (which
1559 * isn't needed after the node is dead). If the node is dead
1560 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1561 * node->tmp_refs against dead-node-only cases where the node
1562 * lock cannot be acquired (eg traversing the dead node list to
1563 * print nodes)
1564 */
1565static void binder_inc_node_tmpref(struct binder_node *node)
1566{
Todd Kjos673068e2017-06-29 12:02:03 -07001567 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001568 if (node->proc)
1569 binder_inner_proc_lock(node->proc);
1570 else
1571 spin_lock(&binder_dead_nodes_lock);
1572 binder_inc_node_tmpref_ilocked(node);
1573 if (node->proc)
1574 binder_inner_proc_unlock(node->proc);
1575 else
1576 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07001577 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001578}
1579
1580/**
Todd Kjosadc18842017-06-29 12:01:59 -07001581 * binder_dec_node_tmpref() - remove a temporary reference on node
1582 * @node: node to reference
1583 *
1584 * Release temporary reference on node taken via binder_inc_node_tmpref()
1585 */
1586static void binder_dec_node_tmpref(struct binder_node *node)
1587{
Todd Kjosed297212017-06-29 12:02:01 -07001588 bool free_node;
1589
Todd Kjos673068e2017-06-29 12:02:03 -07001590 binder_node_inner_lock(node);
1591 if (!node->proc)
Todd Kjosed297212017-06-29 12:02:01 -07001592 spin_lock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001593 node->tmp_refs--;
1594 BUG_ON(node->tmp_refs < 0);
Todd Kjosed297212017-06-29 12:02:01 -07001595 if (!node->proc)
1596 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosadc18842017-06-29 12:01:59 -07001597 /*
1598 * Call binder_dec_node() to check if all refcounts are 0
1599 * and cleanup is needed. Calling with strong=0 and internal=1
1600 * causes no actual reference to be released in binder_dec_node().
1601 * If that changes, a change is needed here too.
1602 */
Todd Kjos673068e2017-06-29 12:02:03 -07001603 free_node = binder_dec_node_nilocked(node, 0, 1);
1604 binder_node_inner_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07001605 if (free_node)
1606 binder_free_node(node);
Todd Kjosadc18842017-06-29 12:01:59 -07001607}
1608
1609static void binder_put_node(struct binder_node *node)
1610{
1611 binder_dec_node_tmpref(node);
1612}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001613
Todd Kjos2c1838d2017-06-29 12:02:08 -07001614static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1615 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616{
1617 struct rb_node *n = proc->refs_by_desc.rb_node;
1618 struct binder_ref *ref;
1619
1620 while (n) {
1621 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1622
Todd Kjos372e3142017-06-29 12:01:58 -07001623 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001624 n = n->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001625 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001626 n = n->rb_right;
Todd Kjos372e3142017-06-29 12:01:58 -07001627 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001628 binder_user_error("tried to use weak ref as strong ref\n");
1629 return NULL;
1630 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001631 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001632 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001633 }
1634 return NULL;
1635}
1636
Todd Kjos372e3142017-06-29 12:01:58 -07001637/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001638 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjos372e3142017-06-29 12:01:58 -07001639 * @proc: binder_proc that owns the ref
1640 * @node: binder_node of target
1641 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1642 *
1643 * Look up the ref for the given node and return it if it exists
1644 *
1645 * If it doesn't exist and the caller provides a newly allocated
1646 * ref, initialize the fields of the newly allocated ref and insert
1647 * into the given proc rb_trees and node refs list.
1648 *
1649 * Return: the ref for node. It is possible that another thread
1650 * allocated/initialized the ref first in which case the
1651 * returned ref would be different than the passed-in
1652 * new_ref. new_ref must be kfree'd by the caller in
1653 * this case.
1654 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001655static struct binder_ref *binder_get_ref_for_node_olocked(
1656 struct binder_proc *proc,
1657 struct binder_node *node,
1658 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659{
Todd Kjos372e3142017-06-29 12:01:58 -07001660 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001661 struct rb_node **p = &proc->refs_by_node.rb_node;
1662 struct rb_node *parent = NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001663 struct binder_ref *ref;
1664 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001665
1666 while (*p) {
1667 parent = *p;
1668 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1669
1670 if (node < ref->node)
1671 p = &(*p)->rb_left;
1672 else if (node > ref->node)
1673 p = &(*p)->rb_right;
1674 else
1675 return ref;
1676 }
Todd Kjos372e3142017-06-29 12:01:58 -07001677 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001678 return NULL;
Todd Kjos372e3142017-06-29 12:01:58 -07001679
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001680 binder_stats_created(BINDER_STAT_REF);
Todd Kjos372e3142017-06-29 12:01:58 -07001681 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001682 new_ref->proc = proc;
1683 new_ref->node = node;
1684 rb_link_node(&new_ref->rb_node_node, parent, p);
1685 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1686
Todd Kjos372e3142017-06-29 12:01:58 -07001687 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001688 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1689 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjos372e3142017-06-29 12:01:58 -07001690 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001691 break;
Todd Kjos372e3142017-06-29 12:01:58 -07001692 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001693 }
1694
1695 p = &proc->refs_by_desc.rb_node;
1696 while (*p) {
1697 parent = *p;
1698 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1699
Todd Kjos372e3142017-06-29 12:01:58 -07001700 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001701 p = &(*p)->rb_left;
Todd Kjos372e3142017-06-29 12:01:58 -07001702 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703 p = &(*p)->rb_right;
1704 else
1705 BUG();
1706 }
1707 rb_link_node(&new_ref->rb_node_desc, parent, p);
1708 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos673068e2017-06-29 12:02:03 -07001709
1710 binder_node_lock(node);
Todd Kjose4cffcf2017-06-29 12:01:50 -07001711 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712
Todd Kjose4cffcf2017-06-29 12:01:50 -07001713 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1714 "%d new ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001715 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjose4cffcf2017-06-29 12:01:50 -07001716 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07001717 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001718 return new_ref;
1719}
1720
Todd Kjos2c1838d2017-06-29 12:02:08 -07001721static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001722{
Todd Kjosed297212017-06-29 12:02:01 -07001723 bool delete_node = false;
Todd Kjosed297212017-06-29 12:02:01 -07001724
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001725 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301726 "%d delete ref %d desc %d for node %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001727 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301728 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001729
1730 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1731 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjos372e3142017-06-29 12:01:58 -07001732
Todd Kjos673068e2017-06-29 12:02:03 -07001733 binder_node_inner_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001734 if (ref->data.strong)
Todd Kjos673068e2017-06-29 12:02:03 -07001735 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjos372e3142017-06-29 12:01:58 -07001736
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001737 hlist_del(&ref->node_entry);
Todd Kjos673068e2017-06-29 12:02:03 -07001738 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1739 binder_node_inner_unlock(ref->node);
Todd Kjosed297212017-06-29 12:02:01 -07001740 /*
1741 * Clear ref->node unless we want the caller to free the node
1742 */
1743 if (!delete_node) {
1744 /*
1745 * The caller uses ref->node to determine
1746 * whether the node needs to be freed. Clear
1747 * it since the node is still alive.
1748 */
1749 ref->node = NULL;
1750 }
Todd Kjos372e3142017-06-29 12:01:58 -07001751
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001752 if (ref->death) {
1753 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301754 "%d delete ref %d desc %d has death notification\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001755 ref->proc->pid, ref->data.debug_id,
1756 ref->data.desc);
Todd Kjos72196392017-06-29 12:02:02 -07001757 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001758 binder_stats_deleted(BINDER_STAT_DEATH);
1759 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001760 binder_stats_deleted(BINDER_STAT_REF);
1761}
1762
Todd Kjos372e3142017-06-29 12:01:58 -07001763/**
Todd Kjos2c1838d2017-06-29 12:02:08 -07001764 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001765 * @ref: ref to be incremented
1766 * @strong: if true, strong increment, else weak
1767 * @target_list: list to queue node work on
1768 *
Todd Kjos2c1838d2017-06-29 12:02:08 -07001769 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjos372e3142017-06-29 12:01:58 -07001770 *
1771 * Return: 0, if successful, else errno
1772 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001773static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1774 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001775{
1776 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001777
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001778 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001779 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001780 ret = binder_inc_node(ref->node, 1, 1, target_list);
1781 if (ret)
1782 return ret;
1783 }
Todd Kjos372e3142017-06-29 12:01:58 -07001784 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001785 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001786 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001787 ret = binder_inc_node(ref->node, 0, 1, target_list);
1788 if (ret)
1789 return ret;
1790 }
Todd Kjos372e3142017-06-29 12:01:58 -07001791 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001792 }
1793 return 0;
1794}
1795
Todd Kjos372e3142017-06-29 12:01:58 -07001796/**
Randy Dunlap63886072023-01-17 10:37:45 -08001797 * binder_dec_ref_olocked() - dec the ref for given handle
Todd Kjos372e3142017-06-29 12:01:58 -07001798 * @ref: ref to be decremented
1799 * @strong: if true, strong decrement, else weak
1800 *
1801 * Decrement the ref.
1802 *
Randy Dunlap63886072023-01-17 10:37:45 -08001803 * Return: %true if ref is cleaned up and ready to be freed.
Todd Kjos372e3142017-06-29 12:01:58 -07001804 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07001805static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001806{
1807 if (strong) {
Todd Kjos372e3142017-06-29 12:01:58 -07001808 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301809 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001810 ref->proc->pid, ref->data.debug_id,
1811 ref->data.desc, ref->data.strong,
1812 ref->data.weak);
1813 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001814 }
Todd Kjos372e3142017-06-29 12:01:58 -07001815 ref->data.strong--;
Todd Kjosed297212017-06-29 12:02:01 -07001816 if (ref->data.strong == 0)
1817 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001818 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07001819 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301820 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjos372e3142017-06-29 12:01:58 -07001821 ref->proc->pid, ref->data.debug_id,
1822 ref->data.desc, ref->data.strong,
1823 ref->data.weak);
1824 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001825 }
Todd Kjos372e3142017-06-29 12:01:58 -07001826 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001827 }
Todd Kjos372e3142017-06-29 12:01:58 -07001828 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001829 binder_cleanup_ref_olocked(ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001830 return true;
1831 }
1832 return false;
1833}
1834
1835/**
1836 * binder_get_node_from_ref() - get the node from the given proc/desc
1837 * @proc: proc containing the ref
1838 * @desc: the handle associated with the ref
1839 * @need_strong_ref: if true, only return node if ref is strong
1840 * @rdata: the id/refcount data for the ref
1841 *
1842 * Given a proc and ref handle, return the associated binder_node
1843 *
1844 * Return: a binder_node or NULL if not found or not strong when strong required
1845 */
1846static struct binder_node *binder_get_node_from_ref(
1847 struct binder_proc *proc,
1848 u32 desc, bool need_strong_ref,
1849 struct binder_ref_data *rdata)
1850{
1851 struct binder_node *node;
1852 struct binder_ref *ref;
1853
Todd Kjos2c1838d2017-06-29 12:02:08 -07001854 binder_proc_lock(proc);
1855 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001856 if (!ref)
1857 goto err_no_ref;
1858 node = ref->node;
Todd Kjosadc18842017-06-29 12:01:59 -07001859 /*
1860 * Take an implicit reference on the node to ensure
1861 * it stays alive until the call to binder_put_node()
1862 */
1863 binder_inc_node_tmpref(node);
Todd Kjos372e3142017-06-29 12:01:58 -07001864 if (rdata)
1865 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001866 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001867
1868 return node;
1869
1870err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001871 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001872 return NULL;
1873}
1874
1875/**
1876 * binder_free_ref() - free the binder_ref
1877 * @ref: ref to free
1878 *
Todd Kjosed297212017-06-29 12:02:01 -07001879 * Free the binder_ref. Free the binder_node indicated by ref->node
1880 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjos372e3142017-06-29 12:01:58 -07001881 */
1882static void binder_free_ref(struct binder_ref *ref)
1883{
Todd Kjosed297212017-06-29 12:02:01 -07001884 if (ref->node)
1885 binder_free_node(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07001886 kfree(ref->death);
1887 kfree(ref);
1888}
1889
1890/**
1891 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1892 * @proc: proc containing the ref
1893 * @desc: the handle associated with the ref
1894 * @increment: true=inc reference, false=dec reference
1895 * @strong: true=strong reference, false=weak reference
1896 * @rdata: the id/refcount data for the ref
1897 *
1898 * Given a proc and ref handle, increment or decrement the ref
1899 * according to "increment" arg.
1900 *
1901 * Return: 0 if successful, else errno
1902 */
1903static int binder_update_ref_for_handle(struct binder_proc *proc,
1904 uint32_t desc, bool increment, bool strong,
1905 struct binder_ref_data *rdata)
1906{
1907 int ret = 0;
1908 struct binder_ref *ref;
1909 bool delete_ref = false;
1910
Todd Kjos2c1838d2017-06-29 12:02:08 -07001911 binder_proc_lock(proc);
1912 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001913 if (!ref) {
1914 ret = -EINVAL;
1915 goto err_no_ref;
1916 }
1917 if (increment)
Todd Kjos2c1838d2017-06-29 12:02:08 -07001918 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001919 else
Todd Kjos2c1838d2017-06-29 12:02:08 -07001920 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjos372e3142017-06-29 12:01:58 -07001921
1922 if (rdata)
1923 *rdata = ref->data;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001924 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001925
1926 if (delete_ref)
1927 binder_free_ref(ref);
1928 return ret;
1929
1930err_no_ref:
Todd Kjos2c1838d2017-06-29 12:02:08 -07001931 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001932 return ret;
1933}
1934
1935/**
1936 * binder_dec_ref_for_handle() - dec the ref for given handle
1937 * @proc: proc containing the ref
1938 * @desc: the handle associated with the ref
1939 * @strong: true=strong reference, false=weak reference
1940 * @rdata: the id/refcount data for the ref
1941 *
1942 * Just calls binder_update_ref_for_handle() to decrement the ref.
1943 *
1944 * Return: 0 if successful, else errno
1945 */
1946static int binder_dec_ref_for_handle(struct binder_proc *proc,
1947 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1948{
1949 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1950}
1951
1952
1953/**
1954 * binder_inc_ref_for_node() - increment the ref for given proc/node
1955 * @proc: proc containing the ref
1956 * @node: target node
1957 * @strong: true=strong reference, false=weak reference
1958 * @target_list: worklist to use if node is incremented
1959 * @rdata: the id/refcount data for the ref
1960 *
1961 * Given a proc and node, increment the ref. Create the ref if it
1962 * doesn't already exist
1963 *
1964 * Return: 0 if successful, else errno
1965 */
1966static int binder_inc_ref_for_node(struct binder_proc *proc,
1967 struct binder_node *node,
1968 bool strong,
1969 struct list_head *target_list,
1970 struct binder_ref_data *rdata)
1971{
1972 struct binder_ref *ref;
1973 struct binder_ref *new_ref = NULL;
1974 int ret = 0;
1975
Todd Kjos2c1838d2017-06-29 12:02:08 -07001976 binder_proc_lock(proc);
1977 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjos372e3142017-06-29 12:01:58 -07001978 if (!ref) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07001979 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07001980 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1981 if (!new_ref)
1982 return -ENOMEM;
Todd Kjos2c1838d2017-06-29 12:02:08 -07001983 binder_proc_lock(proc);
1984 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjos372e3142017-06-29 12:01:58 -07001985 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07001986 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjos372e3142017-06-29 12:01:58 -07001987 *rdata = ref->data;
Carlos Llamas7c57e752022-08-01 18:25:11 +00001988 if (ret && ref == new_ref) {
1989 /*
1990 * Cleanup the failed reference here as the target
1991 * could now be dead and have already released its
1992 * references by now. Calling on the new reference
1993 * with strong=0 and a tmp_refs will not decrement
1994 * the node. The new_ref gets kfree'd below.
1995 */
1996 binder_cleanup_ref_olocked(new_ref);
1997 ref = NULL;
1998 }
1999
Todd Kjos2c1838d2017-06-29 12:02:08 -07002000 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002001 if (new_ref && ref != new_ref)
2002 /*
2003 * Another thread created the ref first so
2004 * free the one we allocated
2005 */
2006 kfree(new_ref);
2007 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002008}
2009
Martijn Coenen0b89d692017-06-29 12:02:06 -07002010static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2011 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002012{
Todd Kjosb6d282c2017-06-29 12:01:54 -07002013 BUG_ON(!target_thread);
Martijn Coenen858b2712017-08-31 10:04:26 +02002014 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjosb6d282c2017-06-29 12:01:54 -07002015 BUG_ON(target_thread->transaction_stack != t);
2016 BUG_ON(target_thread->transaction_stack->from != target_thread);
2017 target_thread->transaction_stack =
2018 target_thread->transaction_stack->from_parent;
2019 t->from = NULL;
2020}
2021
Todd Kjos7a4408c2017-06-29 12:01:57 -07002022/**
2023 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2024 * @thread: thread to decrement
2025 *
2026 * A thread needs to be kept alive while being used to create or
2027 * handle a transaction. binder_get_txn_from() is used to safely
2028 * extract t->from from a binder_transaction and keep the thread
2029 * indicated by t->from from being freed. When done with that
2030 * binder_thread, this function is called to decrement the
2031 * tmp_ref and free if appropriate (thread has been released
2032 * and no transaction being processed by the driver)
2033 */
2034static void binder_thread_dec_tmpref(struct binder_thread *thread)
2035{
2036 /*
2037 * atomic is used to protect the counter value while
2038 * it cannot reach zero or thread->is_dead is false
Todd Kjos7a4408c2017-06-29 12:01:57 -07002039 */
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002040 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002041 atomic_dec(&thread->tmp_ref);
2042 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002043 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002044 binder_free_thread(thread);
2045 return;
2046 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002047 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002048}
2049
2050/**
2051 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2052 * @proc: proc to decrement
2053 *
2054 * A binder_proc needs to be kept alive while being used to create or
2055 * handle a transaction. proc->tmp_ref is incremented when
2056 * creating a new transaction or the binder_proc is currently in-use
2057 * by threads that are being released. When done with the binder_proc,
2058 * this function is called to decrement the counter and free the
2059 * proc if appropriate (proc has been released, all threads have
2060 * been released and not currenly in-use to process a transaction).
2061 */
2062static void binder_proc_dec_tmpref(struct binder_proc *proc)
2063{
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002064 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002065 proc->tmp_ref--;
2066 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
2067 !proc->tmp_ref) {
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002068 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002069 binder_free_proc(proc);
2070 return;
2071 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07002072 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002073}
2074
2075/**
2076 * binder_get_txn_from() - safely extract the "from" thread in transaction
2077 * @t: binder transaction for t->from
2078 *
2079 * Atomically return the "from" thread and increment the tmp_ref
2080 * count for the thread to ensure it stays alive until
2081 * binder_thread_dec_tmpref() is called.
2082 *
2083 * Return: the value of t->from
2084 */
2085static struct binder_thread *binder_get_txn_from(
2086 struct binder_transaction *t)
2087{
2088 struct binder_thread *from;
2089
2090 spin_lock(&t->lock);
2091 from = t->from;
2092 if (from)
2093 atomic_inc(&from->tmp_ref);
2094 spin_unlock(&t->lock);
2095 return from;
2096}
2097
Martijn Coenen0b89d692017-06-29 12:02:06 -07002098/**
2099 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2100 * @t: binder transaction for t->from
2101 *
2102 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2103 * to guarantee that the thread cannot be released while operating on it.
2104 * The caller must call binder_inner_proc_unlock() to release the inner lock
2105 * as well as call binder_dec_thread_txn() to release the reference.
2106 *
2107 * Return: the value of t->from
2108 */
2109static struct binder_thread *binder_get_txn_from_and_acq_inner(
2110 struct binder_transaction *t)
2111{
2112 struct binder_thread *from;
2113
2114 from = binder_get_txn_from(t);
2115 if (!from)
2116 return NULL;
2117 binder_inner_proc_lock(from->proc);
2118 if (t->from) {
2119 BUG_ON(from != t->from);
2120 return from;
2121 }
2122 binder_inner_proc_unlock(from->proc);
2123 binder_thread_dec_tmpref(from);
2124 return NULL;
2125}
2126
Todd Kjosb6d282c2017-06-29 12:01:54 -07002127static void binder_free_transaction(struct binder_transaction *t)
2128{
Todd Kjosa4a3c072019-06-12 13:29:27 -07002129 struct binder_proc *target_proc = t->to_proc;
2130
2131 if (target_proc) {
2132 binder_inner_proc_lock(target_proc);
Marco Ballesio620ede42021-03-09 18:18:56 -08002133 target_proc->outstanding_txns--;
2134 if (target_proc->outstanding_txns < 0)
2135 pr_warn("%s: Unexpected outstanding_txns %d\n",
2136 __func__, target_proc->outstanding_txns);
2137 if (!target_proc->outstanding_txns && target_proc->is_frozen)
2138 wake_up_interruptible_all(&target_proc->freeze_wait);
Todd Kjosa4a3c072019-06-12 13:29:27 -07002139 if (t->buffer)
2140 t->buffer->transaction = NULL;
2141 binder_inner_proc_unlock(target_proc);
2142 }
2143 /*
2144 * If the transaction has no target_proc, then
2145 * t->buffer->transaction has already been cleared.
2146 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002147 kfree(t);
2148 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2149}
2150
2151static void binder_send_failed_reply(struct binder_transaction *t,
2152 uint32_t error_code)
2153{
2154 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002155 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002156
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002157 BUG_ON(t->flags & TF_ONE_WAY);
2158 while (1) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07002159 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002160 if (target_thread) {
Todd Kjos26549d12017-06-29 12:01:55 -07002161 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2162 "send failed reply for transaction %d to %d:%d\n",
2163 t->debug_id,
2164 target_thread->proc->pid,
2165 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002166
Martijn Coenen0b89d692017-06-29 12:02:06 -07002167 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos26549d12017-06-29 12:01:55 -07002168 if (target_thread->reply_error.cmd == BR_OK) {
2169 target_thread->reply_error.cmd = error_code;
Martijn Coenen8e65ef22017-10-19 15:04:46 +02002170 binder_enqueue_thread_work_ilocked(
2171 target_thread,
2172 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002173 wake_up_interruptible(&target_thread->wait);
2174 } else {
Todd Kjos129926c2018-02-07 12:38:47 -08002175 /*
2176 * Cannot get here for normal operation, but
2177 * we can if multiple synchronous transactions
2178 * are sent without blocking for responses.
2179 * Just ignore the 2nd error in this case.
2180 */
2181 pr_warn("Unexpected reply error: %u\n",
2182 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002183 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07002184 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07002185 binder_thread_dec_tmpref(target_thread);
Todd Kjos26549d12017-06-29 12:01:55 -07002186 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002187 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002188 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002189 next = t->from_parent;
2190
2191 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2192 "send failed reply for transaction %d, target dead\n",
2193 t->debug_id);
2194
Todd Kjosb6d282c2017-06-29 12:01:54 -07002195 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002196 if (next == NULL) {
2197 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2198 "reply failed, no target thread at root\n");
2199 return;
2200 }
2201 t = next;
2202 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2203 "reply failed, no target thread -- retry %d\n",
2204 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002205 }
2206}
2207
Martijn Coenenfeba3902017-02-03 14:40:45 -08002208/**
Martijn Coenenfd822492017-08-24 15:23:36 +02002209 * binder_cleanup_transaction() - cleans up undelivered transaction
2210 * @t: transaction that needs to be cleaned up
2211 * @reason: reason the transaction wasn't delivered
2212 * @error_code: error to return to caller (if synchronous call)
2213 */
2214static void binder_cleanup_transaction(struct binder_transaction *t,
2215 const char *reason,
2216 uint32_t error_code)
2217{
2218 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2219 binder_send_failed_reply(t, error_code);
2220 } else {
2221 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2222 "undelivered transaction %d, %s\n",
2223 t->debug_id, reason);
2224 binder_free_transaction(t);
2225 }
2226}
2227
2228/**
Todd Kjos5088f132019-02-08 10:35:16 -08002229 * binder_get_object() - gets object and checks for valid metadata
2230 * @proc: binder_proc owning the buffer
Martijn Coenenfeba3902017-02-03 14:40:45 -08002231 * @buffer: binder_buffer that we're parsing.
Todd Kjos5088f132019-02-08 10:35:16 -08002232 * @offset: offset in the @buffer at which to validate an object.
2233 * @object: struct binder_object to read into
Martijn Coenenfeba3902017-02-03 14:40:45 -08002234 *
2235 * Return: If there's a valid metadata object at @offset in @buffer, the
Todd Kjos5088f132019-02-08 10:35:16 -08002236 * size of that object. Otherwise, it returns zero. The object
2237 * is read into the struct binder_object pointed to by @object.
Martijn Coenenfeba3902017-02-03 14:40:45 -08002238 */
Todd Kjos5088f132019-02-08 10:35:16 -08002239static size_t binder_get_object(struct binder_proc *proc,
2240 struct binder_buffer *buffer,
2241 unsigned long offset,
2242 struct binder_object *object)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002243{
Todd Kjos5088f132019-02-08 10:35:16 -08002244 size_t read_size;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002245 struct binder_object_header *hdr;
2246 size_t object_size = 0;
2247
Todd Kjos5088f132019-02-08 10:35:16 -08002248 read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset);
Todd Kjosc1a28982019-03-19 09:53:01 -07002249 if (offset > buffer->data_size || read_size < sizeof(*hdr) ||
2250 !IS_ALIGNED(offset, sizeof(u32)))
Martijn Coenenfeba3902017-02-03 14:40:45 -08002251 return 0;
Todd Kjos5088f132019-02-08 10:35:16 -08002252 binder_alloc_copy_from_buffer(&proc->alloc, object, buffer,
2253 offset, read_size);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002254
Todd Kjos5088f132019-02-08 10:35:16 -08002255 /* Ok, now see if we read a complete object. */
2256 hdr = &object->hdr;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002257 switch (hdr->type) {
2258 case BINDER_TYPE_BINDER:
2259 case BINDER_TYPE_WEAK_BINDER:
2260 case BINDER_TYPE_HANDLE:
2261 case BINDER_TYPE_WEAK_HANDLE:
2262 object_size = sizeof(struct flat_binder_object);
2263 break;
2264 case BINDER_TYPE_FD:
2265 object_size = sizeof(struct binder_fd_object);
2266 break;
Martijn Coenen79802402017-02-03 14:40:51 -08002267 case BINDER_TYPE_PTR:
2268 object_size = sizeof(struct binder_buffer_object);
2269 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002270 case BINDER_TYPE_FDA:
2271 object_size = sizeof(struct binder_fd_array_object);
2272 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002273 default:
2274 return 0;
2275 }
2276 if (offset <= buffer->data_size - object_size &&
2277 buffer->data_size >= object_size)
2278 return object_size;
2279 else
2280 return 0;
2281}
2282
Martijn Coenen79802402017-02-03 14:40:51 -08002283/**
2284 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
Todd Kjos5f46f332019-02-08 10:35:17 -08002285 * @proc: binder_proc owning the buffer
Martijn Coenen79802402017-02-03 14:40:51 -08002286 * @b: binder_buffer containing the object
Todd Kjos5f46f332019-02-08 10:35:17 -08002287 * @object: struct binder_object to read into
Martijn Coenen79802402017-02-03 14:40:51 -08002288 * @index: index in offset array at which the binder_buffer_object is
2289 * located
Todd Kjos5f46f332019-02-08 10:35:17 -08002290 * @start_offset: points to the start of the offset array
2291 * @object_offsetp: offset of @object read from @b
Martijn Coenen79802402017-02-03 14:40:51 -08002292 * @num_valid: the number of valid offsets in the offset array
2293 *
2294 * Return: If @index is within the valid range of the offset array
2295 * described by @start and @num_valid, and if there's a valid
2296 * binder_buffer_object at the offset found in index @index
2297 * of the offset array, that object is returned. Otherwise,
2298 * %NULL is returned.
2299 * Note that the offset found in index @index itself is not
2300 * verified; this function assumes that @num_valid elements
2301 * from @start were previously verified to have valid offsets.
Todd Kjos5f46f332019-02-08 10:35:17 -08002302 * If @object_offsetp is non-NULL, then the offset within
2303 * @b is written to it.
Martijn Coenen79802402017-02-03 14:40:51 -08002304 */
Todd Kjos5f46f332019-02-08 10:35:17 -08002305static struct binder_buffer_object *binder_validate_ptr(
2306 struct binder_proc *proc,
2307 struct binder_buffer *b,
2308 struct binder_object *object,
2309 binder_size_t index,
2310 binder_size_t start_offset,
2311 binder_size_t *object_offsetp,
2312 binder_size_t num_valid)
Martijn Coenen79802402017-02-03 14:40:51 -08002313{
Todd Kjos5f46f332019-02-08 10:35:17 -08002314 size_t object_size;
2315 binder_size_t object_offset;
2316 unsigned long buffer_offset;
Martijn Coenen79802402017-02-03 14:40:51 -08002317
2318 if (index >= num_valid)
2319 return NULL;
2320
Todd Kjos5f46f332019-02-08 10:35:17 -08002321 buffer_offset = start_offset + sizeof(binder_size_t) * index;
2322 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2323 b, buffer_offset, sizeof(object_offset));
2324 object_size = binder_get_object(proc, b, object_offset, object);
2325 if (!object_size || object->hdr.type != BINDER_TYPE_PTR)
Martijn Coenen79802402017-02-03 14:40:51 -08002326 return NULL;
Todd Kjos5f46f332019-02-08 10:35:17 -08002327 if (object_offsetp)
2328 *object_offsetp = object_offset;
Martijn Coenen79802402017-02-03 14:40:51 -08002329
Todd Kjos5f46f332019-02-08 10:35:17 -08002330 return &object->bbo;
Martijn Coenen79802402017-02-03 14:40:51 -08002331}
2332
2333/**
2334 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
Todd Kjos5f46f332019-02-08 10:35:17 -08002335 * @proc: binder_proc owning the buffer
Martijn Coenen79802402017-02-03 14:40:51 -08002336 * @b: transaction buffer
Todd Kjos5f46f332019-02-08 10:35:17 -08002337 * @objects_start_offset: offset to start of objects buffer
2338 * @buffer_obj_offset: offset to binder_buffer_object in which to fix up
2339 * @fixup_offset: start offset in @buffer to fix up
2340 * @last_obj_offset: offset to last binder_buffer_object that we fixed
2341 * @last_min_offset: minimum fixup offset in object at @last_obj_offset
Martijn Coenen79802402017-02-03 14:40:51 -08002342 *
2343 * Return: %true if a fixup in buffer @buffer at offset @offset is
2344 * allowed.
2345 *
2346 * For safety reasons, we only allow fixups inside a buffer to happen
2347 * at increasing offsets; additionally, we only allow fixup on the last
2348 * buffer object that was verified, or one of its parents.
2349 *
2350 * Example of what is allowed:
2351 *
2352 * A
2353 * B (parent = A, offset = 0)
2354 * C (parent = A, offset = 16)
2355 * D (parent = C, offset = 0)
2356 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2357 *
2358 * Examples of what is not allowed:
2359 *
2360 * Decreasing offsets within the same parent:
2361 * A
2362 * C (parent = A, offset = 16)
2363 * B (parent = A, offset = 0) // decreasing offset within A
2364 *
2365 * Referring to a parent that wasn't the last object or any of its parents:
2366 * A
2367 * B (parent = A, offset = 0)
2368 * C (parent = A, offset = 0)
2369 * C (parent = A, offset = 16)
2370 * D (parent = B, offset = 0) // B is not A or any of A's parents
2371 */
Todd Kjos5f46f332019-02-08 10:35:17 -08002372static bool binder_validate_fixup(struct binder_proc *proc,
2373 struct binder_buffer *b,
2374 binder_size_t objects_start_offset,
2375 binder_size_t buffer_obj_offset,
Martijn Coenen79802402017-02-03 14:40:51 -08002376 binder_size_t fixup_offset,
Todd Kjos5f46f332019-02-08 10:35:17 -08002377 binder_size_t last_obj_offset,
Martijn Coenen79802402017-02-03 14:40:51 -08002378 binder_size_t last_min_offset)
2379{
Todd Kjos5f46f332019-02-08 10:35:17 -08002380 if (!last_obj_offset) {
Martijn Coenen79802402017-02-03 14:40:51 -08002381 /* Nothing to fix up in */
2382 return false;
2383 }
2384
Todd Kjos5f46f332019-02-08 10:35:17 -08002385 while (last_obj_offset != buffer_obj_offset) {
2386 unsigned long buffer_offset;
2387 struct binder_object last_object;
2388 struct binder_buffer_object *last_bbo;
2389 size_t object_size = binder_get_object(proc, b, last_obj_offset,
2390 &last_object);
2391 if (object_size != sizeof(*last_bbo))
2392 return false;
2393
2394 last_bbo = &last_object.bbo;
Martijn Coenen79802402017-02-03 14:40:51 -08002395 /*
2396 * Safe to retrieve the parent of last_obj, since it
2397 * was already previously verified by the driver.
2398 */
Todd Kjos5f46f332019-02-08 10:35:17 -08002399 if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
Martijn Coenen79802402017-02-03 14:40:51 -08002400 return false;
Todd Kjos5f46f332019-02-08 10:35:17 -08002401 last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t);
2402 buffer_offset = objects_start_offset +
2403 sizeof(binder_size_t) * last_bbo->parent,
2404 binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset,
2405 b, buffer_offset,
2406 sizeof(last_obj_offset));
Martijn Coenen79802402017-02-03 14:40:51 -08002407 }
2408 return (fixup_offset >= last_min_offset);
2409}
2410
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002411static void binder_transaction_buffer_release(struct binder_proc *proc,
2412 struct binder_buffer *buffer,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002413 binder_size_t failed_at,
2414 bool is_failure)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002415{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002416 int debug_id = buffer->debug_id;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002417 binder_size_t off_start_offset, buffer_offset, off_end_offset;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002418
2419 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002420 "%d buffer release %d, size %zd-%zd, failed at %llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002421 proc->pid, buffer->debug_id,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002422 buffer->data_size, buffer->offsets_size,
2423 (unsigned long long)failed_at);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002424
2425 if (buffer->target_node)
2426 binder_dec_node(buffer->target_node, 1, 0);
2427
Todd Kjos5f46f332019-02-08 10:35:17 -08002428 off_start_offset = ALIGN(buffer->data_size, sizeof(void *));
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002429 off_end_offset = is_failure ? failed_at :
2430 off_start_offset + buffer->offsets_size;
2431 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
2432 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002433 struct binder_object_header *hdr;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002434 size_t object_size;
Todd Kjos5088f132019-02-08 10:35:16 -08002435 struct binder_object object;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002436 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09002437
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002438 binder_alloc_copy_from_buffer(&proc->alloc, &object_offset,
2439 buffer, buffer_offset,
2440 sizeof(object_offset));
Todd Kjos5088f132019-02-08 10:35:16 -08002441 object_size = binder_get_object(proc, buffer,
2442 object_offset, &object);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002443 if (object_size == 0) {
2444 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002445 debug_id, (u64)object_offset, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002446 continue;
2447 }
Todd Kjos5088f132019-02-08 10:35:16 -08002448 hdr = &object.hdr;
Martijn Coenenfeba3902017-02-03 14:40:45 -08002449 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002450 case BINDER_TYPE_BINDER:
2451 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002452 struct flat_binder_object *fp;
2453 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002454
Martijn Coenenfeba3902017-02-03 14:40:45 -08002455 fp = to_flat_binder_object(hdr);
2456 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002457 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002458 pr_err("transaction release %d bad node %016llx\n",
2459 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002460 break;
2461 }
2462 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002463 " node %d u%016llx\n",
2464 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08002465 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2466 0);
Todd Kjosadc18842017-06-29 12:01:59 -07002467 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002468 } break;
2469 case BINDER_TYPE_HANDLE:
2470 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08002471 struct flat_binder_object *fp;
Todd Kjos372e3142017-06-29 12:01:58 -07002472 struct binder_ref_data rdata;
2473 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002474
Martijn Coenenfeba3902017-02-03 14:40:45 -08002475 fp = to_flat_binder_object(hdr);
Todd Kjos372e3142017-06-29 12:01:58 -07002476 ret = binder_dec_ref_for_handle(proc, fp->handle,
2477 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2478
2479 if (ret) {
2480 pr_err("transaction release %d bad handle %d, ret = %d\n",
2481 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002482 break;
2483 }
2484 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos372e3142017-06-29 12:01:58 -07002485 " ref %d desc %d\n",
2486 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002487 } break;
2488
Martijn Coenenfeba3902017-02-03 14:40:45 -08002489 case BINDER_TYPE_FD: {
2490 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2491
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002492 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08002493 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002494 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08002495 task_close_fd(proc, fp->fd);
2496 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08002497 case BINDER_TYPE_PTR:
2498 /*
2499 * Nothing to do here, this will get cleaned up when the
2500 * transaction buffer gets freed
2501 */
2502 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08002503 case BINDER_TYPE_FDA: {
2504 struct binder_fd_array_object *fda;
2505 struct binder_buffer_object *parent;
Todd Kjos5f46f332019-02-08 10:35:17 -08002506 struct binder_object ptr_object;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002507 binder_size_t fda_offset;
Martijn Coenendef95c72017-02-03 14:40:52 -08002508 size_t fd_index;
2509 binder_size_t fd_buf_size;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002510 binder_size_t num_valid;
Martijn Coenendef95c72017-02-03 14:40:52 -08002511
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002512 num_valid = (buffer_offset - off_start_offset) /
2513 sizeof(binder_size_t);
Martijn Coenendef95c72017-02-03 14:40:52 -08002514 fda = to_binder_fd_array_object(hdr);
Todd Kjos5f46f332019-02-08 10:35:17 -08002515 parent = binder_validate_ptr(proc, buffer, &ptr_object,
2516 fda->parent,
2517 off_start_offset,
2518 NULL,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002519 num_valid);
Martijn Coenendef95c72017-02-03 14:40:52 -08002520 if (!parent) {
2521 pr_err("transaction release %d bad parent offset",
2522 debug_id);
2523 continue;
2524 }
Martijn Coenendef95c72017-02-03 14:40:52 -08002525 fd_buf_size = sizeof(u32) * fda->num_fds;
2526 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2527 pr_err("transaction release %d invalid number of fds (%lld)\n",
2528 debug_id, (u64)fda->num_fds);
2529 continue;
2530 }
2531 if (fd_buf_size > parent->length ||
2532 fda->parent_offset > parent->length - fd_buf_size) {
2533 /* No space for all file descriptors here. */
2534 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2535 debug_id, (u64)fda->num_fds);
2536 continue;
2537 }
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002538 /*
2539 * the source data for binder_buffer_object is visible
2540 * to user-space and the @buffer element is the user
2541 * pointer to the buffer_object containing the fd_array.
2542 * Convert the address to an offset relative to
2543 * the base of the transaction buffer.
2544 */
2545 fda_offset =
2546 (parent->buffer - (uintptr_t)buffer->user_data) +
2547 fda->parent_offset;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002548 for (fd_index = 0; fd_index < fda->num_fds;
2549 fd_index++) {
2550 u32 fd;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002551 binder_size_t offset = fda_offset +
2552 fd_index * sizeof(fd);
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002553
2554 binder_alloc_copy_from_buffer(&proc->alloc,
2555 &fd,
2556 buffer,
2557 offset,
2558 sizeof(fd));
2559 task_close_fd(proc, fd);
2560 }
Martijn Coenendef95c72017-02-03 14:40:52 -08002561 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002562 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002563 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08002564 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002565 break;
2566 }
2567 }
2568}
2569
Martijn Coenena056af42017-02-03 14:40:49 -08002570static int binder_translate_binder(struct flat_binder_object *fp,
2571 struct binder_transaction *t,
2572 struct binder_thread *thread)
2573{
2574 struct binder_node *node;
Martijn Coenena056af42017-02-03 14:40:49 -08002575 struct binder_proc *proc = thread->proc;
2576 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002577 struct binder_ref_data rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002578 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002579
2580 node = binder_get_node(proc, fp->binder);
2581 if (!node) {
Todd Kjos673068e2017-06-29 12:02:03 -07002582 node = binder_new_node(proc, fp);
Martijn Coenena056af42017-02-03 14:40:49 -08002583 if (!node)
2584 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08002585 }
2586 if (fp->cookie != node->cookie) {
2587 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2588 proc->pid, thread->pid, (u64)fp->binder,
2589 node->debug_id, (u64)fp->cookie,
2590 (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07002591 ret = -EINVAL;
2592 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002593 }
Todd Kjos9693ca72021-10-12 09:56:13 -07002594 if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
Todd Kjosadc18842017-06-29 12:01:59 -07002595 ret = -EPERM;
2596 goto done;
2597 }
Martijn Coenena056af42017-02-03 14:40:49 -08002598
Todd Kjos372e3142017-06-29 12:01:58 -07002599 ret = binder_inc_ref_for_node(target_proc, node,
2600 fp->hdr.type == BINDER_TYPE_BINDER,
2601 &thread->todo, &rdata);
2602 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002603 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002604
2605 if (fp->hdr.type == BINDER_TYPE_BINDER)
2606 fp->hdr.type = BINDER_TYPE_HANDLE;
2607 else
2608 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2609 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002610 fp->handle = rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002611 fp->cookie = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002612
Todd Kjos372e3142017-06-29 12:01:58 -07002613 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002614 binder_debug(BINDER_DEBUG_TRANSACTION,
2615 " node %d u%016llx -> ref %d desc %d\n",
2616 node->debug_id, (u64)node->ptr,
Todd Kjos372e3142017-06-29 12:01:58 -07002617 rdata.debug_id, rdata.desc);
Todd Kjosadc18842017-06-29 12:01:59 -07002618done:
2619 binder_put_node(node);
2620 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002621}
2622
2623static int binder_translate_handle(struct flat_binder_object *fp,
2624 struct binder_transaction *t,
2625 struct binder_thread *thread)
2626{
Martijn Coenena056af42017-02-03 14:40:49 -08002627 struct binder_proc *proc = thread->proc;
2628 struct binder_proc *target_proc = t->to_proc;
Todd Kjos372e3142017-06-29 12:01:58 -07002629 struct binder_node *node;
2630 struct binder_ref_data src_rdata;
Todd Kjosadc18842017-06-29 12:01:59 -07002631 int ret = 0;
Martijn Coenena056af42017-02-03 14:40:49 -08002632
Todd Kjos372e3142017-06-29 12:01:58 -07002633 node = binder_get_node_from_ref(proc, fp->handle,
2634 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2635 if (!node) {
Martijn Coenena056af42017-02-03 14:40:49 -08002636 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2637 proc->pid, thread->pid, fp->handle);
2638 return -EINVAL;
2639 }
Todd Kjos9693ca72021-10-12 09:56:13 -07002640 if (security_binder_transfer_binder(proc->cred, target_proc->cred)) {
Todd Kjosadc18842017-06-29 12:01:59 -07002641 ret = -EPERM;
2642 goto done;
2643 }
Martijn Coenena056af42017-02-03 14:40:49 -08002644
Todd Kjos673068e2017-06-29 12:02:03 -07002645 binder_node_lock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002646 if (node->proc == target_proc) {
Martijn Coenena056af42017-02-03 14:40:49 -08002647 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2648 fp->hdr.type = BINDER_TYPE_BINDER;
2649 else
2650 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjos372e3142017-06-29 12:01:58 -07002651 fp->binder = node->ptr;
2652 fp->cookie = node->cookie;
Todd Kjos673068e2017-06-29 12:02:03 -07002653 if (node->proc)
2654 binder_inner_proc_lock(node->proc);
2655 binder_inc_node_nilocked(node,
2656 fp->hdr.type == BINDER_TYPE_BINDER,
2657 0, NULL);
2658 if (node->proc)
2659 binder_inner_proc_unlock(node->proc);
Todd Kjos372e3142017-06-29 12:01:58 -07002660 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002661 binder_debug(BINDER_DEBUG_TRANSACTION,
2662 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002663 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2664 (u64)node->ptr);
Todd Kjos673068e2017-06-29 12:02:03 -07002665 binder_node_unlock(node);
Martijn Coenena056af42017-02-03 14:40:49 -08002666 } else {
Todd Kjos372e3142017-06-29 12:01:58 -07002667 struct binder_ref_data dest_rdata;
Martijn Coenena056af42017-02-03 14:40:49 -08002668
Todd Kjos673068e2017-06-29 12:02:03 -07002669 binder_node_unlock(node);
Todd Kjos372e3142017-06-29 12:01:58 -07002670 ret = binder_inc_ref_for_node(target_proc, node,
2671 fp->hdr.type == BINDER_TYPE_HANDLE,
2672 NULL, &dest_rdata);
2673 if (ret)
Todd Kjosadc18842017-06-29 12:01:59 -07002674 goto done;
Martijn Coenena056af42017-02-03 14:40:49 -08002675
2676 fp->binder = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002677 fp->handle = dest_rdata.desc;
Martijn Coenena056af42017-02-03 14:40:49 -08002678 fp->cookie = 0;
Todd Kjos372e3142017-06-29 12:01:58 -07002679 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2680 &dest_rdata);
Martijn Coenena056af42017-02-03 14:40:49 -08002681 binder_debug(BINDER_DEBUG_TRANSACTION,
2682 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjos372e3142017-06-29 12:01:58 -07002683 src_rdata.debug_id, src_rdata.desc,
2684 dest_rdata.debug_id, dest_rdata.desc,
2685 node->debug_id);
Martijn Coenena056af42017-02-03 14:40:49 -08002686 }
Todd Kjosadc18842017-06-29 12:01:59 -07002687done:
2688 binder_put_node(node);
2689 return ret;
Martijn Coenena056af42017-02-03 14:40:49 -08002690}
2691
2692static int binder_translate_fd(int fd,
2693 struct binder_transaction *t,
2694 struct binder_thread *thread,
2695 struct binder_transaction *in_reply_to)
2696{
2697 struct binder_proc *proc = thread->proc;
2698 struct binder_proc *target_proc = t->to_proc;
2699 int target_fd;
2700 struct file *file;
2701 int ret;
2702 bool target_allows_fd;
2703
2704 if (in_reply_to)
2705 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2706 else
2707 target_allows_fd = t->buffer->target_node->accept_fds;
2708 if (!target_allows_fd) {
2709 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2710 proc->pid, thread->pid,
2711 in_reply_to ? "reply" : "transaction",
2712 fd);
2713 ret = -EPERM;
2714 goto err_fd_not_accepted;
2715 }
2716
2717 file = fget(fd);
2718 if (!file) {
2719 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2720 proc->pid, thread->pid, fd);
2721 ret = -EBADF;
2722 goto err_fget;
2723 }
Todd Kjos9693ca72021-10-12 09:56:13 -07002724 ret = security_binder_transfer_file(proc->cred, target_proc->cred, file);
Martijn Coenena056af42017-02-03 14:40:49 -08002725 if (ret < 0) {
2726 ret = -EPERM;
2727 goto err_security;
2728 }
2729
2730 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2731 if (target_fd < 0) {
2732 ret = -ENOMEM;
2733 goto err_get_unused_fd;
2734 }
2735 task_fd_install(target_proc, target_fd, file);
2736 trace_binder_transaction_fd(t, fd, target_fd);
2737 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2738 fd, target_fd);
2739
2740 return target_fd;
2741
2742err_get_unused_fd:
2743err_security:
2744 fput(file);
2745err_fget:
2746err_fd_not_accepted:
2747 return ret;
2748}
2749
Martijn Coenendef95c72017-02-03 14:40:52 -08002750static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2751 struct binder_buffer_object *parent,
2752 struct binder_transaction *t,
2753 struct binder_thread *thread,
2754 struct binder_transaction *in_reply_to)
2755{
2756 binder_size_t fdi, fd_buf_size, num_installed_fds;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002757 binder_size_t fda_offset;
Martijn Coenendef95c72017-02-03 14:40:52 -08002758 int target_fd;
Martijn Coenendef95c72017-02-03 14:40:52 -08002759 struct binder_proc *proc = thread->proc;
2760 struct binder_proc *target_proc = t->to_proc;
2761
2762 fd_buf_size = sizeof(u32) * fda->num_fds;
2763 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2764 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2765 proc->pid, thread->pid, (u64)fda->num_fds);
2766 return -EINVAL;
2767 }
2768 if (fd_buf_size > parent->length ||
2769 fda->parent_offset > parent->length - fd_buf_size) {
2770 /* No space for all file descriptors here. */
2771 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2772 proc->pid, thread->pid, (u64)fda->num_fds);
2773 return -EINVAL;
2774 }
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002775 /*
2776 * the source data for binder_buffer_object is visible
2777 * to user-space and the @buffer element is the user
2778 * pointer to the buffer_object containing the fd_array.
2779 * Convert the address to an offset relative to
2780 * the base of the transaction buffer.
2781 */
2782 fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) +
2783 fda->parent_offset;
2784 if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) {
Martijn Coenendef95c72017-02-03 14:40:52 -08002785 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2786 proc->pid, thread->pid);
2787 return -EINVAL;
2788 }
2789 for (fdi = 0; fdi < fda->num_fds; fdi++) {
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002790 u32 fd;
Todd Kjose7d41262019-03-27 16:12:31 -07002791
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002792 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002793
2794 binder_alloc_copy_from_buffer(&target_proc->alloc,
2795 &fd, t->buffer,
2796 offset, sizeof(fd));
2797 target_fd = binder_translate_fd(fd, t, thread, in_reply_to);
Martijn Coenendef95c72017-02-03 14:40:52 -08002798 if (target_fd < 0)
2799 goto err_translate_fd_failed;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002800 binder_alloc_copy_to_buffer(&target_proc->alloc,
2801 t->buffer, offset,
2802 &target_fd, sizeof(fd));
Martijn Coenendef95c72017-02-03 14:40:52 -08002803 }
2804 return 0;
2805
2806err_translate_fd_failed:
2807 /*
2808 * Failed to allocate fd or security error, free fds
2809 * installed so far.
2810 */
2811 num_installed_fds = fdi;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002812 for (fdi = 0; fdi < num_installed_fds; fdi++) {
2813 u32 fd;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002814 binder_size_t offset = fda_offset + fdi * sizeof(fd);
Todd Kjos4d01f6b2019-02-08 10:35:15 -08002815 binder_alloc_copy_from_buffer(&target_proc->alloc,
2816 &fd, t->buffer,
2817 offset, sizeof(fd));
2818 task_close_fd(target_proc, fd);
2819 }
Martijn Coenendef95c72017-02-03 14:40:52 -08002820 return target_fd;
2821}
2822
Martijn Coenen79802402017-02-03 14:40:51 -08002823static int binder_fixup_parent(struct binder_transaction *t,
2824 struct binder_thread *thread,
2825 struct binder_buffer_object *bp,
Todd Kjos5f46f332019-02-08 10:35:17 -08002826 binder_size_t off_start_offset,
Martijn Coenen79802402017-02-03 14:40:51 -08002827 binder_size_t num_valid,
Todd Kjos5f46f332019-02-08 10:35:17 -08002828 binder_size_t last_fixup_obj_off,
Martijn Coenen79802402017-02-03 14:40:51 -08002829 binder_size_t last_fixup_min_off)
2830{
2831 struct binder_buffer_object *parent;
Martijn Coenen79802402017-02-03 14:40:51 -08002832 struct binder_buffer *b = t->buffer;
2833 struct binder_proc *proc = thread->proc;
2834 struct binder_proc *target_proc = t->to_proc;
Todd Kjos5f46f332019-02-08 10:35:17 -08002835 struct binder_object object;
2836 binder_size_t buffer_offset;
2837 binder_size_t parent_offset;
Martijn Coenen79802402017-02-03 14:40:51 -08002838
2839 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2840 return 0;
2841
Todd Kjos5f46f332019-02-08 10:35:17 -08002842 parent = binder_validate_ptr(target_proc, b, &object, bp->parent,
2843 off_start_offset, &parent_offset,
2844 num_valid);
Martijn Coenen79802402017-02-03 14:40:51 -08002845 if (!parent) {
2846 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2847 proc->pid, thread->pid);
2848 return -EINVAL;
2849 }
2850
Todd Kjos5f46f332019-02-08 10:35:17 -08002851 if (!binder_validate_fixup(target_proc, b, off_start_offset,
2852 parent_offset, bp->parent_offset,
2853 last_fixup_obj_off,
Martijn Coenen79802402017-02-03 14:40:51 -08002854 last_fixup_min_off)) {
2855 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2856 proc->pid, thread->pid);
2857 return -EINVAL;
2858 }
2859
2860 if (parent->length < sizeof(binder_uintptr_t) ||
2861 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2862 /* No space for a pointer here! */
2863 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2864 proc->pid, thread->pid);
2865 return -EINVAL;
2866 }
Todd Kjos5f46f332019-02-08 10:35:17 -08002867 buffer_offset = bp->parent_offset +
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08002868 (uintptr_t)parent->buffer - (uintptr_t)b->user_data;
Todd Kjos5f46f332019-02-08 10:35:17 -08002869 binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset,
2870 &bp->buffer, sizeof(bp->buffer));
Martijn Coenen79802402017-02-03 14:40:51 -08002871
2872 return 0;
2873}
2874
Martijn Coenen408c68b2017-08-31 10:04:19 +02002875/**
Li Li83838b02022-05-26 15:00:18 -07002876 * binder_can_update_transaction() - Can a txn be superseded by an updated one?
2877 * @t1: the pending async txn in the frozen process
2878 * @t2: the new async txn to supersede the outdated pending one
2879 *
2880 * Return: true if t2 can supersede t1
2881 * false if t2 can not supersede t1
2882 */
2883static bool binder_can_update_transaction(struct binder_transaction *t1,
2884 struct binder_transaction *t2)
2885{
2886 if ((t1->flags & t2->flags & (TF_ONE_WAY | TF_UPDATE_TXN)) !=
2887 (TF_ONE_WAY | TF_UPDATE_TXN) || !t1->to_proc || !t2->to_proc)
2888 return false;
2889 if (t1->to_proc->tsk == t2->to_proc->tsk && t1->code == t2->code &&
2890 t1->flags == t2->flags && t1->buffer->pid == t2->buffer->pid &&
2891 t1->buffer->target_node->ptr == t2->buffer->target_node->ptr &&
2892 t1->buffer->target_node->cookie == t2->buffer->target_node->cookie)
2893 return true;
2894 return false;
2895}
2896
2897/**
2898 * binder_find_outdated_transaction_ilocked() - Find the outdated transaction
2899 * @t: new async transaction
2900 * @target_list: list to find outdated transaction
2901 *
2902 * Return: the outdated transaction if found
2903 * NULL if no outdated transacton can be found
2904 *
2905 * Requires the proc->inner_lock to be held.
2906 */
2907static struct binder_transaction *
2908binder_find_outdated_transaction_ilocked(struct binder_transaction *t,
2909 struct list_head *target_list)
2910{
2911 struct binder_work *w;
2912
2913 list_for_each_entry(w, target_list, entry) {
2914 struct binder_transaction *t_queued;
2915
2916 if (w->type != BINDER_WORK_TRANSACTION)
2917 continue;
2918 t_queued = container_of(w, struct binder_transaction, work);
2919 if (binder_can_update_transaction(t_queued, t))
2920 return t_queued;
2921 }
2922 return NULL;
2923}
2924
2925/**
Martijn Coenen408c68b2017-08-31 10:04:19 +02002926 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2927 * @t: transaction to send
2928 * @proc: process to send the transaction to
2929 * @thread: thread in @proc to send the transaction to (may be NULL)
2930 *
2931 * This function queues a transaction to the specified process. It will try
2932 * to find a thread in the target process to handle the transaction and
2933 * wake it up. If no thread is found, the work is queued to the proc
2934 * waitqueue.
2935 *
2936 * If the @thread parameter is not NULL, the transaction is always queued
2937 * to the waitlist of that specific thread.
2938 *
Marco Ballesio620ede42021-03-09 18:18:56 -08002939 * Return: 0 if the transaction was successfully queued
2940 * BR_DEAD_REPLY if the target process or thread is dead
Li Liaf1f9842022-11-23 12:16:54 -08002941 * BR_FROZEN_REPLY if the target process or thread is frozen and
2942 * the sync transaction was rejected
2943 * BR_TRANSACTION_PENDING_FROZEN if the target process is frozen
2944 * and the async transaction was successfully queued
Martijn Coenen408c68b2017-08-31 10:04:19 +02002945 */
Marco Ballesio620ede42021-03-09 18:18:56 -08002946static int binder_proc_transaction(struct binder_transaction *t,
Martijn Coenen408c68b2017-08-31 10:04:19 +02002947 struct binder_proc *proc,
2948 struct binder_thread *thread)
2949{
Martijn Coenen408c68b2017-08-31 10:04:19 +02002950 struct binder_node *node = t->buffer->target_node;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07002951 struct binder_priority node_prio;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002952 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen8e65ef22017-10-19 15:04:46 +02002953 bool pending_async = false;
Li Li83838b02022-05-26 15:00:18 -07002954 struct binder_transaction *t_outdated = NULL;
Li Liaf1f9842022-11-23 12:16:54 -08002955 bool frozen = false;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002956
2957 BUG_ON(!node);
2958 binder_node_lock(node);
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07002959 node_prio.prio = node->min_priority;
2960 node_prio.sched_policy = node->sched_policy;
2961
Martijn Coenen408c68b2017-08-31 10:04:19 +02002962 if (oneway) {
2963 BUG_ON(thread);
2964 if (node->has_async_transaction) {
Martijn Coenen8e65ef22017-10-19 15:04:46 +02002965 pending_async = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002966 } else {
Gustavo A. R. Silva10340bf2018-01-23 12:04:27 -06002967 node->has_async_transaction = true;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002968 }
2969 }
2970
2971 binder_inner_proc_lock(proc);
Marco Ballesio45490402020-09-10 13:39:20 -07002972 if (proc->is_frozen) {
Li Liaf1f9842022-11-23 12:16:54 -08002973 frozen = true;
Marco Ballesio45490402020-09-10 13:39:20 -07002974 proc->sync_recv |= !oneway;
2975 proc->async_recv |= oneway;
2976 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02002977
Li Liaf1f9842022-11-23 12:16:54 -08002978 if ((frozen && !oneway) || proc->is_dead ||
Marco Ballesio45490402020-09-10 13:39:20 -07002979 (thread && thread->is_dead)) {
Marco Ballesio620ede42021-03-09 18:18:56 -08002980 bool proc_is_dead = proc->is_dead
2981 || (thread && thread->is_dead);
Martijn Coenen408c68b2017-08-31 10:04:19 +02002982 binder_inner_proc_unlock(proc);
2983 binder_node_unlock(node);
Marco Ballesio620ede42021-03-09 18:18:56 -08002984 return proc_is_dead ? BR_DEAD_REPLY : BR_FROZEN_REPLY;
Martijn Coenen408c68b2017-08-31 10:04:19 +02002985 }
2986
Martijn Coenen8e65ef22017-10-19 15:04:46 +02002987 if (!thread && !pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02002988 thread = binder_select_thread_ilocked(proc);
2989
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07002990 if (thread) {
Martijn Coenenfb92c342017-06-23 10:13:43 -07002991 binder_transaction_priority(thread->task, t, node_prio,
2992 node->inherit_rt);
Martijn Coenen8e65ef22017-10-19 15:04:46 +02002993 binder_enqueue_thread_work_ilocked(thread, &t->work);
2994 } else if (!pending_async) {
2995 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07002996 } else {
Li Liaf1f9842022-11-23 12:16:54 -08002997 if ((t->flags & TF_UPDATE_TXN) && frozen) {
Li Li83838b02022-05-26 15:00:18 -07002998 t_outdated = binder_find_outdated_transaction_ilocked(t,
2999 &node->async_todo);
3000 if (t_outdated) {
3001 binder_debug(BINDER_DEBUG_TRANSACTION,
3002 "txn %d supersedes %d\n",
3003 t->debug_id, t_outdated->debug_id);
3004 list_del_init(&t_outdated->work.entry);
3005 proc->outstanding_txns--;
3006 }
3007 }
Martijn Coenen8e65ef22017-10-19 15:04:46 +02003008 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07003009 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02003010
Martijn Coenen8e65ef22017-10-19 15:04:46 +02003011 if (!pending_async)
Martijn Coenen408c68b2017-08-31 10:04:19 +02003012 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
3013
Marco Ballesio620ede42021-03-09 18:18:56 -08003014 proc->outstanding_txns++;
Martijn Coenen408c68b2017-08-31 10:04:19 +02003015 binder_inner_proc_unlock(proc);
3016 binder_node_unlock(node);
3017
Li Li83838b02022-05-26 15:00:18 -07003018 /*
3019 * To reduce potential contention, free the outdated transaction and
3020 * buffer after releasing the locks.
3021 */
3022 if (t_outdated) {
3023 struct binder_buffer *buffer = t_outdated->buffer;
3024
3025 t_outdated->buffer = NULL;
3026 buffer->transaction = NULL;
3027 trace_binder_transaction_update_buffer_release(buffer);
3028 binder_transaction_buffer_release(proc, buffer, 0, false);
3029 binder_alloc_free_buf(&proc->alloc, buffer);
3030 kfree(t_outdated);
3031 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3032 }
3033
Li Liaf1f9842022-11-23 12:16:54 -08003034 if (oneway && frozen)
3035 return BR_TRANSACTION_PENDING_FROZEN;
3036
Marco Ballesio620ede42021-03-09 18:18:56 -08003037 return 0;
Martijn Coenen408c68b2017-08-31 10:04:19 +02003038}
3039
Todd Kjos512cf462017-09-29 15:39:49 -07003040/**
3041 * binder_get_node_refs_for_txn() - Get required refs on node for txn
3042 * @node: struct binder_node for which to get refs
Randy Dunlap63886072023-01-17 10:37:45 -08003043 * @procp: returns @node->proc if valid
3044 * @error: if no @procp then returns BR_DEAD_REPLY
Todd Kjos512cf462017-09-29 15:39:49 -07003045 *
3046 * User-space normally keeps the node alive when creating a transaction
3047 * since it has a reference to the target. The local strong ref keeps it
3048 * alive if the sending process dies before the target process processes
3049 * the transaction. If the source process is malicious or has a reference
3050 * counting bug, relying on the local strong ref can fail.
3051 *
3052 * Since user-space can cause the local strong ref to go away, we also take
3053 * a tmpref on the node to ensure it survives while we are constructing
3054 * the transaction. We also need a tmpref on the proc while we are
3055 * constructing the transaction, so we take that here as well.
3056 *
3057 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
Randy Dunlap63886072023-01-17 10:37:45 -08003058 * Also sets @procp if valid. If the @node->proc is NULL indicating that the
3059 * target proc has died, @error is set to BR_DEAD_REPLY.
Todd Kjos512cf462017-09-29 15:39:49 -07003060 */
3061static struct binder_node *binder_get_node_refs_for_txn(
3062 struct binder_node *node,
3063 struct binder_proc **procp,
3064 uint32_t *error)
3065{
3066 struct binder_node *target_node = NULL;
3067
3068 binder_node_inner_lock(node);
3069 if (node->proc) {
3070 target_node = node;
3071 binder_inc_node_nilocked(node, 1, 0, NULL);
3072 binder_inc_node_tmpref_ilocked(node);
3073 node->proc->tmp_ref++;
3074 *procp = node->proc;
3075 } else
3076 *error = BR_DEAD_REPLY;
3077 binder_node_inner_unlock(node);
3078
3079 return target_node;
3080}
3081
Carlos Llamasea1d78b2022-04-29 23:56:41 +00003082static void binder_set_txn_from_error(struct binder_transaction *t, int id,
3083 uint32_t command, int32_t param)
3084{
3085 struct binder_thread *from = binder_get_txn_from_and_acq_inner(t);
3086
3087 if (!from) {
3088 /* annotation for sparse */
3089 __release(&from->proc->inner_lock);
3090 return;
3091 }
3092
3093 /* don't override existing errors */
3094 if (from->ee.command == BR_OK)
3095 binder_set_extended_error(&from->ee, id, command, param);
3096 binder_inner_proc_unlock(from->proc);
3097 binder_thread_dec_tmpref(from);
3098}
3099
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003100static void binder_transaction(struct binder_proc *proc,
3101 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08003102 struct binder_transaction_data *tr, int reply,
3103 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003104{
Martijn Coenena056af42017-02-03 14:40:49 -08003105 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003106 struct binder_transaction *t;
3107 struct binder_work *tcomplete;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003108 binder_size_t buffer_offset = 0;
3109 binder_size_t off_start_offset, off_end_offset;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003110 binder_size_t off_min;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003111 binder_size_t sg_buf_offset, sg_buf_end_offset;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003112 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003113 struct binder_thread *target_thread = NULL;
3114 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 struct binder_transaction *in_reply_to = NULL;
3116 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003117 uint32_t return_error = 0;
3118 uint32_t return_error_param = 0;
3119 uint32_t return_error_line = 0;
Todd Kjos5f46f332019-02-08 10:35:17 -08003120 binder_size_t last_fixup_obj_off = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08003121 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003122 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07003123 int t_debug_id = atomic_inc_return(&binder_last_id);
Todd Kjos5312a992019-01-14 09:10:21 -08003124 char *secctx = NULL;
3125 u32 secctx_sz = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003126
3127 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07003128 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003129 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
3130 e->from_proc = proc->pid;
3131 e->from_thread = thread->pid;
3132 e->target_handle = tr->target.handle;
3133 e->data_size = tr->data_size;
3134 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08003135 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003136
Carlos Llamasea1d78b2022-04-29 23:56:41 +00003137 binder_inner_proc_lock(proc);
3138 binder_set_extended_error(&thread->ee, t_debug_id, BR_OK, 0);
3139 binder_inner_proc_unlock(proc);
3140
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003141 if (reply) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003142 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 in_reply_to = thread->transaction_stack;
3144 if (in_reply_to == NULL) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003145 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303146 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003147 proc->pid, thread->pid);
3148 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003149 return_error_param = -EPROTO;
3150 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003151 goto err_empty_call_stack;
3152 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 if (in_reply_to->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07003154 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303155 binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 proc->pid, thread->pid, in_reply_to->debug_id,
3157 in_reply_to->to_proc ?
3158 in_reply_to->to_proc->pid : 0,
3159 in_reply_to->to_thread ?
3160 in_reply_to->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003161 spin_unlock(&in_reply_to->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003162 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003163 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003164 return_error_param = -EPROTO;
3165 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003166 in_reply_to = NULL;
3167 goto err_bad_call_stack;
3168 }
3169 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003170 binder_inner_proc_unlock(proc);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003171 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003172 if (target_thread == NULL) {
3173 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003174 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003175 goto err_dead_binder;
3176 }
3177 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303178 binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003179 proc->pid, thread->pid,
3180 target_thread->transaction_stack ?
3181 target_thread->transaction_stack->debug_id : 0,
3182 in_reply_to->debug_id);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003183 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003184 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003185 return_error_param = -EPROTO;
3186 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003187 in_reply_to = NULL;
3188 target_thread = NULL;
3189 goto err_dead_binder;
3190 }
3191 target_proc = target_thread->proc;
Todd Kjos7a4408c2017-06-29 12:01:57 -07003192 target_proc->tmp_ref++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003193 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 } else {
3195 if (tr->target.handle) {
3196 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09003197
Todd Kjoseb349832017-06-29 12:01:56 -07003198 /*
3199 * There must already be a strong ref
3200 * on this node. If so, do a strong
3201 * increment on the node to ensure it
3202 * stays alive until the transaction is
3203 * done.
3204 */
Todd Kjos2c1838d2017-06-29 12:02:08 -07003205 binder_proc_lock(proc);
3206 ref = binder_get_ref_olocked(proc, tr->target.handle,
3207 true);
Todd Kjoseb349832017-06-29 12:01:56 -07003208 if (ref) {
Todd Kjos512cf462017-09-29 15:39:49 -07003209 target_node = binder_get_node_refs_for_txn(
3210 ref->node, &target_proc,
3211 &return_error);
3212 } else {
3213 binder_user_error("%d:%d got transaction to invalid handle\n",
3214 proc->pid, thread->pid);
3215 return_error = BR_FAILED_REPLY;
Todd Kjoseb349832017-06-29 12:01:56 -07003216 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07003217 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003218 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07003219 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08003220 target_node = context->binder_context_mgr_node;
Todd Kjos512cf462017-09-29 15:39:49 -07003221 if (target_node)
3222 target_node = binder_get_node_refs_for_txn(
3223 target_node, &target_proc,
3224 &return_error);
3225 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003226 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07003227 mutex_unlock(&context->context_mgr_node_lock);
Hridya Valsaraju74402b52019-07-15 12:18:04 -07003228 if (target_node && target_proc->pid == proc->pid) {
Martijn Coenenfd0485e2018-03-28 11:14:50 +02003229 binder_user_error("%d:%d got transaction to context manager from process owning it\n",
3230 proc->pid, thread->pid);
3231 return_error = BR_FAILED_REPLY;
3232 return_error_param = -EINVAL;
3233 return_error_line = __LINE__;
3234 goto err_invalid_target_handle;
3235 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003236 }
Todd Kjos512cf462017-09-29 15:39:49 -07003237 if (!target_node) {
3238 /*
3239 * return_error is set above
3240 */
3241 return_error_param = -EINVAL;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003242 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003243 goto err_dead_binder;
3244 }
Todd Kjos512cf462017-09-29 15:39:49 -07003245 e->to_node = target_node->debug_id;
Todd Kjos9693ca72021-10-12 09:56:13 -07003246 if (security_binder_transaction(proc->cred,
3247 target_proc->cred) < 0) {
Stephen Smalley79af7302015-01-21 10:54:10 -05003248 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003249 return_error_param = -EPERM;
3250 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003251 goto err_invalid_target_handle;
3252 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07003253 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003254 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3255 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003256
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003257 tmp = thread->transaction_stack;
3258 if (tmp->to_thread != thread) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07003259 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303260 binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003261 proc->pid, thread->pid, tmp->debug_id,
3262 tmp->to_proc ? tmp->to_proc->pid : 0,
3263 tmp->to_thread ?
3264 tmp->to_thread->pid : 0);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003265 spin_unlock(&tmp->lock);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003266 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003267 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003268 return_error_param = -EPROTO;
3269 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270 goto err_bad_call_stack;
3271 }
3272 while (tmp) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07003273 struct binder_thread *from;
3274
3275 spin_lock(&tmp->lock);
3276 from = tmp->from;
3277 if (from && from->proc == target_proc) {
3278 atomic_inc(&from->tmp_ref);
3279 target_thread = from;
3280 spin_unlock(&tmp->lock);
3281 break;
3282 }
3283 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003284 tmp = tmp->from_parent;
3285 }
3286 }
Martijn Coenen0b89d692017-06-29 12:02:06 -07003287 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003288 }
Martijn Coenen408c68b2017-08-31 10:04:19 +02003289 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003290 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003291 e->to_proc = target_proc->pid;
3292
3293 /* TODO: reuse incoming transaction for reply */
3294 t = kzalloc(sizeof(*t), GFP_KERNEL);
3295 if (t == NULL) {
3296 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003297 return_error_param = -ENOMEM;
3298 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003299 goto err_alloc_t_failed;
3300 }
3301 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003302 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303
3304 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3305 if (tcomplete == NULL) {
3306 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003307 return_error_param = -ENOMEM;
3308 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003309 goto err_alloc_tcomplete_failed;
3310 }
3311 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3312
Todd Kjosd99c7332017-06-29 12:01:53 -07003313 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314
3315 if (reply)
3316 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08003317 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003318 proc->pid, thread->pid, t->debug_id,
3319 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003320 (u64)tr->data.ptr.buffer,
3321 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08003322 (u64)tr->data_size, (u64)tr->offsets_size,
3323 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003324 else
3325 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08003326 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 proc->pid, thread->pid, t->debug_id,
3328 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003329 (u64)tr->data.ptr.buffer,
3330 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08003331 (u64)tr->data_size, (u64)tr->offsets_size,
3332 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003333
3334 if (!reply && !(tr->flags & TF_ONE_WAY))
3335 t->from = thread;
3336 else
3337 t->from = NULL;
Todd Kjos1c929ec2021-11-12 10:07:20 -08003338 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003339 t->to_proc = target_proc;
3340 t->to_thread = target_thread;
3341 t->code = tr->code;
3342 t->flags = tr->flags;
Martijn Coenen37b34412017-06-06 17:04:42 -07003343 if (!(t->flags & TF_ONE_WAY) &&
3344 binder_supported_policy(current->policy)) {
3345 /* Inherit supported policies for synchronous transactions */
3346 t->priority.sched_policy = current->policy;
3347 t->priority.prio = current->normal_prio;
3348 } else {
3349 /* Otherwise, fall back to the default priority */
3350 t->priority = target_proc->default_priority;
3351 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003352
Todd Kjos5312a992019-01-14 09:10:21 -08003353 if (target_node && target_node->txn_security_ctx) {
3354 u32 secid;
Todd Kjos53025742019-04-24 12:31:18 -07003355 size_t added_size;
Carlos Llamas1212acf2021-07-20 17:23:46 +00003356 int max_retries = 100;
Todd Kjos5312a992019-01-14 09:10:21 -08003357
3358 security_task_getsecid(proc->tsk, &secid);
Carlos Llamas1212acf2021-07-20 17:23:46 +00003359 retry_alloc:
Todd Kjos5312a992019-01-14 09:10:21 -08003360 ret = security_secid_to_secctx(secid, &secctx, &secctx_sz);
Carlos Llamas1212acf2021-07-20 17:23:46 +00003361 if (ret == -ENOMEM && max_retries-- > 0) {
3362 struct page *dummy_page;
3363
3364 /*
3365 * security_secid_to_secctx() can fail because of a
3366 * GFP_ATOMIC allocation in which case -ENOMEM is
3367 * returned. This needs to be retried, but there is
3368 * currently no way to tell userspace to retry so we
3369 * do it here. We make sure there is still available
3370 * memory first and then retry.
3371 */
3372 dummy_page = alloc_page(GFP_KERNEL);
3373 if (dummy_page) {
3374 __free_page(dummy_page);
3375 goto retry_alloc;
3376 }
3377 }
Todd Kjos5312a992019-01-14 09:10:21 -08003378 if (ret) {
3379 return_error = BR_FAILED_REPLY;
3380 return_error_param = ret;
3381 return_error_line = __LINE__;
3382 goto err_get_secctx_failed;
3383 }
Todd Kjos53025742019-04-24 12:31:18 -07003384 added_size = ALIGN(secctx_sz, sizeof(u64));
3385 extra_buffers_size += added_size;
3386 if (extra_buffers_size < added_size) {
3387 /* integer overflow of extra_buffers_size */
3388 return_error = BR_FAILED_REPLY;
3389 return_error_param = EINVAL;
3390 return_error_line = __LINE__;
3391 goto err_bad_extra_size;
3392 }
Todd Kjos5312a992019-01-14 09:10:21 -08003393 }
3394
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003395 trace_binder_transaction(reply, t, target_node);
3396
Todd Kjos19c98722017-06-29 12:01:40 -07003397 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08003398 tr->offsets_size, extra_buffers_size,
Martijn Coenen370fe262020-08-21 14:25:44 +02003399 !reply && (t->flags & TF_ONE_WAY), current->tgid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07003400 if (IS_ERR(t->buffer)) {
3401 /*
3402 * -ESRCH indicates VMA cleared. The target is dying.
3403 */
3404 return_error_param = PTR_ERR(t->buffer);
3405 return_error = return_error_param == -ESRCH ?
3406 BR_DEAD_REPLY : BR_FAILED_REPLY;
3407 return_error_line = __LINE__;
3408 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409 goto err_binder_alloc_buf_failed;
3410 }
Todd Kjos5312a992019-01-14 09:10:21 -08003411 if (secctx) {
3412 size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) +
3413 ALIGN(tr->offsets_size, sizeof(void *)) +
3414 ALIGN(extra_buffers_size, sizeof(void *)) -
3415 ALIGN(secctx_sz, sizeof(u64));
Todd Kjos5312a992019-01-14 09:10:21 -08003416
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003417 t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003418 binder_alloc_copy_to_buffer(&target_proc->alloc,
3419 t->buffer, buf_offset,
3420 secctx, secctx_sz);
Todd Kjos5312a992019-01-14 09:10:21 -08003421 security_release_secctx(secctx, secctx_sz);
3422 secctx = NULL;
3423 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424 t->buffer->debug_id = t->debug_id;
3425 t->buffer->transaction = t;
3426 t->buffer->target_node = target_node;
Todd Kjos451fffd2020-11-20 15:37:43 -08003427 t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003428 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429
Todd Kjosaf6f1cb2019-02-08 10:35:14 -08003430 if (binder_alloc_copy_user_to_buffer(
3431 &target_proc->alloc,
3432 t->buffer, 0,
3433 (const void __user *)
3434 (uintptr_t)tr->data.ptr.buffer,
3435 tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303436 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3437 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003438 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003439 return_error_param = -EFAULT;
3440 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003441 goto err_copy_data_failed;
3442 }
Todd Kjosaf6f1cb2019-02-08 10:35:14 -08003443 if (binder_alloc_copy_user_to_buffer(
3444 &target_proc->alloc,
3445 t->buffer,
3446 ALIGN(tr->data_size, sizeof(void *)),
3447 (const void __user *)
3448 (uintptr_t)tr->data.ptr.offsets,
3449 tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303450 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3451 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003453 return_error_param = -EFAULT;
3454 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003455 goto err_copy_data_failed;
3456 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003457 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3458 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3459 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003461 return_error_param = -EINVAL;
3462 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003463 goto err_bad_offset;
3464 }
Martijn Coenen79802402017-02-03 14:40:51 -08003465 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3466 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3467 proc->pid, thread->pid,
3468 (u64)extra_buffers_size);
3469 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003470 return_error_param = -EINVAL;
3471 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003472 goto err_bad_offset;
3473 }
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003474 off_start_offset = ALIGN(tr->data_size, sizeof(void *));
3475 buffer_offset = off_start_offset;
3476 off_end_offset = off_start_offset + tr->offsets_size;
3477 sg_buf_offset = ALIGN(off_end_offset, sizeof(void *));
Martijn Coenen4be99872019-07-09 13:09:23 +02003478 sg_buf_end_offset = sg_buf_offset + extra_buffers_size -
3479 ALIGN(secctx_sz, sizeof(u64));
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003480 off_min = 0;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003481 for (buffer_offset = off_start_offset; buffer_offset < off_end_offset;
3482 buffer_offset += sizeof(binder_size_t)) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003483 struct binder_object_header *hdr;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003484 size_t object_size;
Todd Kjos5088f132019-02-08 10:35:16 -08003485 struct binder_object object;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003486 binder_size_t object_offset;
Seunghun Lee10f62862014-05-01 01:30:23 +09003487
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003488 binder_alloc_copy_from_buffer(&target_proc->alloc,
3489 &object_offset,
3490 t->buffer,
3491 buffer_offset,
3492 sizeof(object_offset));
Todd Kjos5088f132019-02-08 10:35:16 -08003493 object_size = binder_get_object(target_proc, t->buffer,
3494 object_offset, &object);
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003495 if (object_size == 0 || object_offset < off_min) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003496 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003497 proc->pid, thread->pid,
3498 (u64)object_offset,
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003499 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08003500 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003501 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003502 return_error_param = -EINVAL;
3503 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003504 goto err_bad_offset;
3505 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003506
Todd Kjos5088f132019-02-08 10:35:16 -08003507 hdr = &object.hdr;
Todd Kjos4d01f6b2019-02-08 10:35:15 -08003508 off_min = object_offset + object_size;
Martijn Coenenfeba3902017-02-03 14:40:45 -08003509 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003510 case BINDER_TYPE_BINDER:
3511 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003512 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003513
Martijn Coenenfeba3902017-02-03 14:40:45 -08003514 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003515 ret = binder_translate_binder(fp, t, thread);
3516 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003517 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003518 return_error_param = ret;
3519 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003520 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 }
Todd Kjos5088f132019-02-08 10:35:16 -08003522 binder_alloc_copy_to_buffer(&target_proc->alloc,
3523 t->buffer, object_offset,
3524 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003525 } break;
3526 case BINDER_TYPE_HANDLE:
3527 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003528 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003529
Martijn Coenenfeba3902017-02-03 14:40:45 -08003530 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003531 ret = binder_translate_handle(fp, t, thread);
3532 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003533 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003534 return_error_param = ret;
3535 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003536 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 }
Todd Kjos5088f132019-02-08 10:35:16 -08003538 binder_alloc_copy_to_buffer(&target_proc->alloc,
3539 t->buffer, object_offset,
3540 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 } break;
3542
3543 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08003544 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08003545 int target_fd = binder_translate_fd(fp->fd, t, thread,
3546 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003547
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003548 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003550 return_error_param = target_fd;
3551 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08003552 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08003554 fp->pad_binder = 0;
3555 fp->fd = target_fd;
Todd Kjos5088f132019-02-08 10:35:16 -08003556 binder_alloc_copy_to_buffer(&target_proc->alloc,
3557 t->buffer, object_offset,
3558 fp, sizeof(*fp));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003559 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08003560 case BINDER_TYPE_FDA: {
Todd Kjos5f46f332019-02-08 10:35:17 -08003561 struct binder_object ptr_object;
3562 binder_size_t parent_offset;
Martijn Coenendef95c72017-02-03 14:40:52 -08003563 struct binder_fd_array_object *fda =
3564 to_binder_fd_array_object(hdr);
Todd Kjosfe7096a2019-12-13 12:25:31 -08003565 size_t num_valid = (buffer_offset - off_start_offset) /
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003566 sizeof(binder_size_t);
Martijn Coenendef95c72017-02-03 14:40:52 -08003567 struct binder_buffer_object *parent =
Todd Kjos5f46f332019-02-08 10:35:17 -08003568 binder_validate_ptr(target_proc, t->buffer,
3569 &ptr_object, fda->parent,
3570 off_start_offset,
3571 &parent_offset,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003572 num_valid);
Martijn Coenendef95c72017-02-03 14:40:52 -08003573 if (!parent) {
3574 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3575 proc->pid, thread->pid);
3576 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003577 return_error_param = -EINVAL;
3578 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003579 goto err_bad_parent;
3580 }
Todd Kjos5f46f332019-02-08 10:35:17 -08003581 if (!binder_validate_fixup(target_proc, t->buffer,
3582 off_start_offset,
3583 parent_offset,
3584 fda->parent_offset,
3585 last_fixup_obj_off,
Martijn Coenendef95c72017-02-03 14:40:52 -08003586 last_fixup_min_off)) {
3587 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3588 proc->pid, thread->pid);
3589 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003590 return_error_param = -EINVAL;
3591 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003592 goto err_bad_parent;
3593 }
3594 ret = binder_translate_fd_array(fda, parent, t, thread,
3595 in_reply_to);
3596 if (ret < 0) {
3597 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003598 return_error_param = ret;
3599 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08003600 goto err_translate_failed;
3601 }
Todd Kjos5f46f332019-02-08 10:35:17 -08003602 last_fixup_obj_off = parent_offset;
Martijn Coenendef95c72017-02-03 14:40:52 -08003603 last_fixup_min_off =
3604 fda->parent_offset + sizeof(u32) * fda->num_fds;
3605 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08003606 case BINDER_TYPE_PTR: {
3607 struct binder_buffer_object *bp =
3608 to_binder_buffer_object(hdr);
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003609 size_t buf_left = sg_buf_end_offset - sg_buf_offset;
3610 size_t num_valid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611
Martijn Coenen79802402017-02-03 14:40:51 -08003612 if (bp->length > buf_left) {
3613 binder_user_error("%d:%d got transaction with too large buffer\n",
3614 proc->pid, thread->pid);
3615 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003616 return_error_param = -EINVAL;
3617 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003618 goto err_bad_offset;
3619 }
Todd Kjosaf6f1cb2019-02-08 10:35:14 -08003620 if (binder_alloc_copy_user_to_buffer(
3621 &target_proc->alloc,
3622 t->buffer,
3623 sg_buf_offset,
3624 (const void __user *)
3625 (uintptr_t)bp->buffer,
3626 bp->length)) {
Martijn Coenen79802402017-02-03 14:40:51 -08003627 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3628 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07003629 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08003630 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003631 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003632 goto err_copy_data_failed;
3633 }
3634 /* Fixup buffer pointer to target proc address space */
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003635 bp->buffer = (uintptr_t)
3636 t->buffer->user_data + sg_buf_offset;
3637 sg_buf_offset += ALIGN(bp->length, sizeof(u64));
Martijn Coenen79802402017-02-03 14:40:51 -08003638
Todd Kjosfe7096a2019-12-13 12:25:31 -08003639 num_valid = (buffer_offset - off_start_offset) /
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003640 sizeof(binder_size_t);
Todd Kjos5f46f332019-02-08 10:35:17 -08003641 ret = binder_fixup_parent(t, thread, bp,
3642 off_start_offset,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003643 num_valid,
Todd Kjos5f46f332019-02-08 10:35:17 -08003644 last_fixup_obj_off,
Martijn Coenen79802402017-02-03 14:40:51 -08003645 last_fixup_min_off);
3646 if (ret < 0) {
3647 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003648 return_error_param = ret;
3649 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08003650 goto err_translate_failed;
3651 }
Todd Kjos5088f132019-02-08 10:35:16 -08003652 binder_alloc_copy_to_buffer(&target_proc->alloc,
3653 t->buffer, object_offset,
3654 bp, sizeof(*bp));
Todd Kjos5f46f332019-02-08 10:35:17 -08003655 last_fixup_obj_off = object_offset;
Martijn Coenen79802402017-02-03 14:40:51 -08003656 last_fixup_min_off = 0;
3657 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003658 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003659 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08003660 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07003662 return_error_param = -EINVAL;
3663 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664 goto err_bad_object_type;
3665 }
3666 }
Hang Lu4dbc1682021-04-09 17:40:46 +08003667 if (t->buffer->oneway_spam_suspect)
3668 tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT;
3669 else
3670 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos673068e2017-06-29 12:02:03 -07003671 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjosccae6f62017-06-29 12:01:48 -07003672
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673 if (reply) {
Martijn Coenen8e65ef22017-10-19 15:04:46 +02003674 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003675 binder_inner_proc_lock(target_proc);
Li Li503f84d2021-09-07 16:12:53 -07003676 if (target_thread->is_dead) {
3677 return_error = BR_DEAD_REPLY;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003678 binder_inner_proc_unlock(target_proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003679 goto err_dead_proc_or_thread;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003680 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003681 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003682 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen8e65ef22017-10-19 15:04:46 +02003683 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Marco Ballesio620ede42021-03-09 18:18:56 -08003684 target_proc->outstanding_txns++;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003685 binder_inner_proc_unlock(target_proc);
Martijn Coenen408c68b2017-08-31 10:04:19 +02003686 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenen22b061b2017-05-26 10:48:56 -07003687 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjosb6d282c2017-06-29 12:01:54 -07003688 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003689 } else if (!(t->flags & TF_ONE_WAY)) {
3690 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen0b89d692017-06-29 12:02:06 -07003691 binder_inner_proc_lock(proc);
Martijn Coenenad4cc172017-11-13 09:55:21 +01003692 /*
3693 * Defer the TRANSACTION_COMPLETE, so we don't return to
3694 * userspace immediately; this allows the target process to
3695 * immediately start processing this transaction, reducing
3696 * latency. We will then return the TRANSACTION_COMPLETE when
3697 * the target replies (or there is an error).
3698 */
3699 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700 t->need_reply = 1;
3701 t->from_parent = thread->transaction_stack;
3702 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07003703 binder_inner_proc_unlock(proc);
Marco Ballesio620ede42021-03-09 18:18:56 -08003704 return_error = binder_proc_transaction(t,
3705 target_proc, target_thread);
3706 if (return_error) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07003707 binder_inner_proc_lock(proc);
3708 binder_pop_transaction_ilocked(thread, t);
3709 binder_inner_proc_unlock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07003710 goto err_dead_proc_or_thread;
3711 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 } else {
3713 BUG_ON(target_node == NULL);
3714 BUG_ON(t->buffer->async_transaction != 1);
Marco Ballesio620ede42021-03-09 18:18:56 -08003715 return_error = binder_proc_transaction(t, target_proc, NULL);
Li Liaf1f9842022-11-23 12:16:54 -08003716 /*
3717 * Let the caller know when async transaction reaches a frozen
3718 * process and is put in a pending queue, waiting for the target
3719 * process to be unfrozen.
3720 */
3721 if (return_error == BR_TRANSACTION_PENDING_FROZEN)
3722 tcomplete->type = BINDER_WORK_TRANSACTION_PENDING;
3723 binder_enqueue_thread_work(thread, tcomplete);
3724 if (return_error &&
3725 return_error != BR_TRANSACTION_PENDING_FROZEN)
Todd Kjos7a4408c2017-06-29 12:01:57 -07003726 goto err_dead_proc_or_thread;
Riley Andrews00b40d62017-06-29 12:01:37 -07003727 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07003728 if (target_thread)
3729 binder_thread_dec_tmpref(target_thread);
3730 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003731 if (target_node)
3732 binder_dec_node_tmpref(target_node);
Todd Kjosd99c7332017-06-29 12:01:53 -07003733 /*
3734 * write barrier to synchronize with initialization
3735 * of log entry
3736 */
3737 smp_wmb();
3738 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 return;
3740
Todd Kjos7a4408c2017-06-29 12:01:57 -07003741err_dead_proc_or_thread:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003742 return_error_line = __LINE__;
Xu YiPingd53bebd2017-09-05 10:21:52 -07003743 binder_dequeue_work(proc, tcomplete);
Martijn Coenena056af42017-02-03 14:40:49 -08003744err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003745err_bad_object_type:
3746err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08003747err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003749 trace_binder_transaction_failed_buffer_release(t->buffer);
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08003750 binder_transaction_buffer_release(target_proc, t->buffer,
3751 buffer_offset, true);
Todd Kjos512cf462017-09-29 15:39:49 -07003752 if (target_node)
3753 binder_dec_node_tmpref(target_node);
Todd Kjoseb349832017-06-29 12:01:56 -07003754 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003755 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07003756 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003757err_binder_alloc_buf_failed:
Todd Kjos53025742019-04-24 12:31:18 -07003758err_bad_extra_size:
Todd Kjos5312a992019-01-14 09:10:21 -08003759 if (secctx)
3760 security_release_secctx(secctx, secctx_sz);
3761err_get_secctx_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003762 kfree(tcomplete);
3763 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3764err_alloc_tcomplete_failed:
3765 kfree(t);
3766 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3767err_alloc_t_failed:
3768err_bad_call_stack:
3769err_empty_call_stack:
3770err_dead_binder:
3771err_invalid_target_handle:
Todd Kjos7a4408c2017-06-29 12:01:57 -07003772 if (target_thread)
3773 binder_thread_dec_tmpref(target_thread);
3774 if (target_proc)
3775 binder_proc_dec_tmpref(target_proc);
Todd Kjos512cf462017-09-29 15:39:49 -07003776 if (target_node) {
Todd Kjoseb349832017-06-29 12:01:56 -07003777 binder_dec_node(target_node, 1, 0);
Todd Kjos512cf462017-09-29 15:39:49 -07003778 binder_dec_node_tmpref(target_node);
3779 }
Todd Kjoseb349832017-06-29 12:01:56 -07003780
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003782 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3783 proc->pid, thread->pid, return_error, return_error_param,
3784 (u64)tr->data_size, (u64)tr->offsets_size,
3785 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003786
3787 {
3788 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003789
Todd Kjos57ada2f2017-06-29 12:01:46 -07003790 e->return_error = return_error;
3791 e->return_error_param = return_error_param;
3792 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003793 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3794 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07003795 /*
3796 * write barrier to synchronize with initialization
3797 * of log entry
3798 */
3799 smp_wmb();
3800 WRITE_ONCE(e->debug_id_done, t_debug_id);
3801 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003802 }
3803
Todd Kjos26549d12017-06-29 12:01:55 -07003804 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003805 if (in_reply_to) {
Martijn Coenen22b061b2017-05-26 10:48:56 -07003806 binder_restore_priority(current, in_reply_to->saved_priority);
Carlos Llamasea1d78b2022-04-29 23:56:41 +00003807 binder_set_txn_from_error(in_reply_to, t_debug_id,
3808 return_error, return_error_param);
Todd Kjos26549d12017-06-29 12:01:55 -07003809 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen8e65ef22017-10-19 15:04:46 +02003810 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003811 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos26549d12017-06-29 12:01:55 -07003812 } else {
Carlos Llamasea1d78b2022-04-29 23:56:41 +00003813 binder_inner_proc_lock(proc);
3814 binder_set_extended_error(&thread->ee, t_debug_id,
3815 return_error, return_error_param);
3816 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07003817 thread->return_error.cmd = return_error;
Martijn Coenen8e65ef22017-10-19 15:04:46 +02003818 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos26549d12017-06-29 12:01:55 -07003819 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003820}
3821
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003822static int binder_thread_write(struct binder_proc *proc,
3823 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003824 binder_uintptr_t binder_buffer, size_t size,
3825 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003826{
3827 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08003828 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003829 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003830 void __user *ptr = buffer + *consumed;
3831 void __user *end = buffer + size;
3832
Todd Kjos26549d12017-06-29 12:01:55 -07003833 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjos372e3142017-06-29 12:01:58 -07003834 int ret;
3835
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003836 if (get_user(cmd, (uint32_t __user *)ptr))
3837 return -EFAULT;
3838 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003839 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003840 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003841 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3842 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3843 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003844 }
3845 switch (cmd) {
3846 case BC_INCREFS:
3847 case BC_ACQUIRE:
3848 case BC_RELEASE:
3849 case BC_DECREFS: {
3850 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851 const char *debug_string;
Todd Kjos372e3142017-06-29 12:01:58 -07003852 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3853 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3854 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003855
3856 if (get_user(target, (uint32_t __user *)ptr))
3857 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07003858
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859 ptr += sizeof(uint32_t);
Todd Kjos372e3142017-06-29 12:01:58 -07003860 ret = -1;
3861 if (increment && !target) {
Todd Kjosc44b1232017-06-29 12:01:43 -07003862 struct binder_node *ctx_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -07003863 mutex_lock(&context->context_mgr_node_lock);
3864 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjos5206f782020-08-28 08:55:25 -07003865 if (ctx_mgr_node)
Todd Kjos372e3142017-06-29 12:01:58 -07003866 ret = binder_inc_ref_for_node(
3867 proc, ctx_mgr_node,
3868 strong, NULL, &rdata);
Todd Kjosc44b1232017-06-29 12:01:43 -07003869 mutex_unlock(&context->context_mgr_node_lock);
3870 }
Todd Kjos372e3142017-06-29 12:01:58 -07003871 if (ret)
3872 ret = binder_update_ref_for_handle(
3873 proc, target, increment, strong,
3874 &rdata);
3875 if (!ret && rdata.desc != target) {
3876 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3877 proc->pid, thread->pid,
3878 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003879 }
3880 switch (cmd) {
3881 case BC_INCREFS:
3882 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003883 break;
3884 case BC_ACQUIRE:
3885 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003886 break;
3887 case BC_RELEASE:
3888 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003889 break;
3890 case BC_DECREFS:
3891 default:
3892 debug_string = "DecRefs";
Todd Kjos372e3142017-06-29 12:01:58 -07003893 break;
3894 }
3895 if (ret) {
3896 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3897 proc->pid, thread->pid, debug_string,
3898 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003899 break;
3900 }
3901 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjos372e3142017-06-29 12:01:58 -07003902 "%d:%d %s ref %d desc %d s %d w %d\n",
3903 proc->pid, thread->pid, debug_string,
3904 rdata.debug_id, rdata.desc, rdata.strong,
3905 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003906 break;
3907 }
3908 case BC_INCREFS_DONE:
3909 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003910 binder_uintptr_t node_ptr;
3911 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003912 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07003913 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003914
Arve Hjønnevågda498892014-02-21 14:40:26 -08003915 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003916 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003917 ptr += sizeof(binder_uintptr_t);
3918 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003920 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003921 node = binder_get_node(proc, node_ptr);
3922 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003923 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003924 proc->pid, thread->pid,
3925 cmd == BC_INCREFS_DONE ?
3926 "BC_INCREFS_DONE" :
3927 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003928 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003929 break;
3930 }
3931 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003932 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003933 proc->pid, thread->pid,
3934 cmd == BC_INCREFS_DONE ?
3935 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003936 (u64)node_ptr, node->debug_id,
3937 (u64)cookie, (u64)node->cookie);
Todd Kjosadc18842017-06-29 12:01:59 -07003938 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 break;
3940 }
Todd Kjos673068e2017-06-29 12:02:03 -07003941 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003942 if (cmd == BC_ACQUIRE_DONE) {
3943 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303944 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003945 proc->pid, thread->pid,
3946 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003947 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003948 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003949 break;
3950 }
3951 node->pending_strong_ref = 0;
3952 } else {
3953 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303954 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003955 proc->pid, thread->pid,
3956 node->debug_id);
Todd Kjos673068e2017-06-29 12:02:03 -07003957 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003958 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003959 break;
3960 }
3961 node->pending_weak_ref = 0;
3962 }
Todd Kjos673068e2017-06-29 12:02:03 -07003963 free_node = binder_dec_node_nilocked(node,
3964 cmd == BC_ACQUIRE_DONE, 0);
3965 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003966 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosadc18842017-06-29 12:01:59 -07003967 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003968 proc->pid, thread->pid,
3969 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosadc18842017-06-29 12:01:59 -07003970 node->debug_id, node->local_strong_refs,
3971 node->local_weak_refs, node->tmp_refs);
Todd Kjos673068e2017-06-29 12:02:03 -07003972 binder_node_inner_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07003973 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003974 break;
3975 }
3976 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303977 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003978 return -EINVAL;
3979 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303980 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003981 return -EINVAL;
3982
3983 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003984 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003985 struct binder_buffer *buffer;
3986
Arve Hjønnevågda498892014-02-21 14:40:26 -08003987 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003988 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003989 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003990
Todd Kjos53d311cf2017-06-29 12:01:51 -07003991 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3992 data_ptr);
Todd Kjosfd6cc332018-11-06 15:55:32 -08003993 if (IS_ERR_OR_NULL(buffer)) {
3994 if (PTR_ERR(buffer) == -EPERM) {
3995 binder_user_error(
3996 "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
3997 proc->pid, thread->pid,
3998 (u64)data_ptr);
3999 } else {
4000 binder_user_error(
4001 "%d:%d BC_FREE_BUFFER u%016llx no match\n",
4002 proc->pid, thread->pid,
4003 (u64)data_ptr);
4004 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004005 break;
4006 }
4007 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004008 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
4009 proc->pid, thread->pid, (u64)data_ptr,
4010 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004011 buffer->transaction ? "active" : "finished");
4012
Todd Kjosa4a3c072019-06-12 13:29:27 -07004013 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014 if (buffer->transaction) {
4015 buffer->transaction->buffer = NULL;
4016 buffer->transaction = NULL;
4017 }
Todd Kjosa4a3c072019-06-12 13:29:27 -07004018 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004019 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos72196392017-06-29 12:02:02 -07004020 struct binder_node *buf_node;
4021 struct binder_work *w;
4022
4023 buf_node = buffer->target_node;
Todd Kjos673068e2017-06-29 12:02:03 -07004024 binder_node_inner_lock(buf_node);
Todd Kjos72196392017-06-29 12:02:02 -07004025 BUG_ON(!buf_node->has_async_transaction);
4026 BUG_ON(buf_node->proc != proc);
Todd Kjos72196392017-06-29 12:02:02 -07004027 w = binder_dequeue_work_head_ilocked(
4028 &buf_node->async_todo);
Martijn Coenen3a6430c2017-08-31 10:04:29 +02004029 if (!w) {
Gustavo A. R. Silva10340bf2018-01-23 12:04:27 -06004030 buf_node->has_async_transaction = false;
Martijn Coenen3a6430c2017-08-31 10:04:29 +02004031 } else {
Todd Kjos72196392017-06-29 12:02:02 -07004032 binder_enqueue_work_ilocked(
Martijn Coenen3a6430c2017-08-31 10:04:29 +02004033 w, &proc->todo);
4034 binder_wakeup_proc_ilocked(proc);
4035 }
Todd Kjos673068e2017-06-29 12:02:03 -07004036 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004037 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004038 trace_binder_transaction_buffer_release(buffer);
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08004039 binder_transaction_buffer_release(proc, buffer, 0, false);
Todd Kjos19c98722017-06-29 12:01:40 -07004040 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004041 break;
4042 }
4043
Martijn Coenen79802402017-02-03 14:40:51 -08004044 case BC_TRANSACTION_SG:
4045 case BC_REPLY_SG: {
4046 struct binder_transaction_data_sg tr;
4047
4048 if (copy_from_user(&tr, ptr, sizeof(tr)))
4049 return -EFAULT;
4050 ptr += sizeof(tr);
4051 binder_transaction(proc, thread, &tr.transaction_data,
4052 cmd == BC_REPLY_SG, tr.buffers_size);
4053 break;
4054 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004055 case BC_TRANSACTION:
4056 case BC_REPLY: {
4057 struct binder_transaction_data tr;
4058
4059 if (copy_from_user(&tr, ptr, sizeof(tr)))
4060 return -EFAULT;
4061 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08004062 binder_transaction(proc, thread, &tr,
4063 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004064 break;
4065 }
4066
4067 case BC_REGISTER_LOOPER:
4068 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304069 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004070 proc->pid, thread->pid);
Todd Kjosb3e68612017-06-29 12:02:07 -07004071 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004072 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
4073 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05304074 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004075 proc->pid, thread->pid);
4076 } else if (proc->requested_threads == 0) {
4077 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05304078 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004079 proc->pid, thread->pid);
4080 } else {
4081 proc->requested_threads--;
4082 proc->requested_threads_started++;
4083 }
4084 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosb3e68612017-06-29 12:02:07 -07004085 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004086 break;
4087 case BC_ENTER_LOOPER:
4088 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304089 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004090 proc->pid, thread->pid);
4091 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
4092 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05304093 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004094 proc->pid, thread->pid);
4095 }
4096 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
4097 break;
4098 case BC_EXIT_LOOPER:
4099 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304100 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004101 proc->pid, thread->pid);
4102 thread->looper |= BINDER_LOOPER_STATE_EXITED;
4103 break;
4104
4105 case BC_REQUEST_DEATH_NOTIFICATION:
4106 case BC_CLEAR_DEATH_NOTIFICATION: {
4107 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004108 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004109 struct binder_ref *ref;
Todd Kjos2c1838d2017-06-29 12:02:08 -07004110 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004111
4112 if (get_user(target, (uint32_t __user *)ptr))
4113 return -EFAULT;
4114 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08004115 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004116 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004117 ptr += sizeof(binder_uintptr_t);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004118 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
4119 /*
4120 * Allocate memory for death notification
4121 * before taking lock
4122 */
4123 death = kzalloc(sizeof(*death), GFP_KERNEL);
4124 if (death == NULL) {
4125 WARN_ON(thread->return_error.cmd !=
4126 BR_OK);
4127 thread->return_error.cmd = BR_ERROR;
Martijn Coenen8e65ef22017-10-19 15:04:46 +02004128 binder_enqueue_thread_work(
4129 thread,
4130 &thread->return_error.work);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004131 binder_debug(
4132 BINDER_DEBUG_FAILED_TRANSACTION,
4133 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
4134 proc->pid, thread->pid);
4135 break;
4136 }
4137 }
4138 binder_proc_lock(proc);
4139 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004140 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304141 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004142 proc->pid, thread->pid,
4143 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4144 "BC_REQUEST_DEATH_NOTIFICATION" :
4145 "BC_CLEAR_DEATH_NOTIFICATION",
4146 target);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004147 binder_proc_unlock(proc);
4148 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004149 break;
4150 }
4151
4152 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004153 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004154 proc->pid, thread->pid,
4155 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
4156 "BC_REQUEST_DEATH_NOTIFICATION" :
4157 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjos372e3142017-06-29 12:01:58 -07004158 (u64)cookie, ref->data.debug_id,
4159 ref->data.desc, ref->data.strong,
4160 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004161
Martijn Coenenab51ec62017-06-29 12:02:10 -07004162 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004163 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
4164 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304165 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004166 proc->pid, thread->pid);
Martijn Coenenab51ec62017-06-29 12:02:10 -07004167 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004168 binder_proc_unlock(proc);
4169 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004170 break;
4171 }
4172 binder_stats_created(BINDER_STAT_DEATH);
4173 INIT_LIST_HEAD(&death->work.entry);
4174 death->cookie = cookie;
4175 ref->death = death;
4176 if (ref->node->proc == NULL) {
4177 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenenbb745622017-08-31 10:04:28 +02004178
4179 binder_inner_proc_lock(proc);
4180 binder_enqueue_work_ilocked(
4181 &ref->death->work, &proc->todo);
4182 binder_wakeup_proc_ilocked(proc);
4183 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004184 }
4185 } else {
4186 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304187 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004188 proc->pid, thread->pid);
Todd Kjos673068e2017-06-29 12:02:03 -07004189 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004190 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004191 break;
4192 }
4193 death = ref->death;
4194 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004195 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004196 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004197 (u64)death->cookie,
4198 (u64)cookie);
Todd Kjos673068e2017-06-29 12:02:03 -07004199 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004200 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004201 break;
4202 }
4203 ref->death = NULL;
Todd Kjos72196392017-06-29 12:02:02 -07004204 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004205 if (list_empty(&death->work.entry)) {
4206 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07004207 if (thread->looper &
4208 (BINDER_LOOPER_STATE_REGISTERED |
4209 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen8e65ef22017-10-19 15:04:46 +02004210 binder_enqueue_thread_work_ilocked(
4211 thread,
4212 &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07004213 else {
4214 binder_enqueue_work_ilocked(
4215 &death->work,
4216 &proc->todo);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004217 binder_wakeup_proc_ilocked(
Martijn Coenen408c68b2017-08-31 10:04:19 +02004218 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004219 }
4220 } else {
4221 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
4222 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
4223 }
Todd Kjos72196392017-06-29 12:02:02 -07004224 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004225 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07004226 binder_node_unlock(ref->node);
Todd Kjos2c1838d2017-06-29 12:02:08 -07004227 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004228 } break;
4229 case BC_DEAD_BINDER_DONE: {
4230 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08004231 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09004233
Arve Hjønnevågda498892014-02-21 14:40:26 -08004234 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004235 return -EFAULT;
4236
Lisa Du7a64cd82016-02-17 09:32:52 +08004237 ptr += sizeof(cookie);
Todd Kjos72196392017-06-29 12:02:02 -07004238 binder_inner_proc_lock(proc);
4239 list_for_each_entry(w, &proc->delivered_death,
4240 entry) {
4241 struct binder_ref_death *tmp_death =
4242 container_of(w,
4243 struct binder_ref_death,
4244 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09004245
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004246 if (tmp_death->cookie == cookie) {
4247 death = tmp_death;
4248 break;
4249 }
4250 }
4251 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjosb46af092018-02-07 13:57:37 -08004252 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004253 proc->pid, thread->pid, (u64)cookie,
4254 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004255 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004256 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
4257 proc->pid, thread->pid, (u64)cookie);
Todd Kjos72196392017-06-29 12:02:02 -07004258 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004259 break;
4260 }
Todd Kjos72196392017-06-29 12:02:02 -07004261 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004262 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
4263 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos72196392017-06-29 12:02:02 -07004264 if (thread->looper &
4265 (BINDER_LOOPER_STATE_REGISTERED |
4266 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen8e65ef22017-10-19 15:04:46 +02004267 binder_enqueue_thread_work_ilocked(
4268 thread, &death->work);
Todd Kjos72196392017-06-29 12:02:02 -07004269 else {
4270 binder_enqueue_work_ilocked(
4271 &death->work,
4272 &proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02004273 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004274 }
4275 }
Todd Kjos72196392017-06-29 12:02:02 -07004276 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004277 } break;
4278
4279 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304280 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004281 proc->pid, thread->pid, cmd);
4282 return -EINVAL;
4283 }
4284 *consumed = ptr - buffer;
4285 }
4286 return 0;
4287}
4288
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02004289static void binder_stat_br(struct binder_proc *proc,
4290 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004291{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004292 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004293 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07004294 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
4295 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
4296 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004297 }
4298}
4299
Todd Kjos26b47d82017-06-29 12:01:47 -07004300static int binder_put_node_cmd(struct binder_proc *proc,
4301 struct binder_thread *thread,
4302 void __user **ptrp,
4303 binder_uintptr_t node_ptr,
4304 binder_uintptr_t node_cookie,
4305 int node_debug_id,
4306 uint32_t cmd, const char *cmd_name)
4307{
4308 void __user *ptr = *ptrp;
4309
4310 if (put_user(cmd, (uint32_t __user *)ptr))
4311 return -EFAULT;
4312 ptr += sizeof(uint32_t);
4313
4314 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
4315 return -EFAULT;
4316 ptr += sizeof(binder_uintptr_t);
4317
4318 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
4319 return -EFAULT;
4320 ptr += sizeof(binder_uintptr_t);
4321
4322 binder_stat_br(proc, thread, cmd);
4323 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
4324 proc->pid, thread->pid, cmd_name, node_debug_id,
4325 (u64)node_ptr, (u64)node_cookie);
4326
4327 *ptrp = ptr;
4328 return 0;
4329}
4330
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004331static int binder_wait_for_work(struct binder_thread *thread,
4332 bool do_proc_work)
4333{
4334 DEFINE_WAIT(wait);
4335 struct binder_proc *proc = thread->proc;
4336 int ret = 0;
4337
4338 freezer_do_not_count();
4339 binder_inner_proc_lock(proc);
4340 for (;;) {
4341 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
4342 if (binder_has_work_ilocked(thread, do_proc_work))
4343 break;
4344 if (do_proc_work)
4345 list_add(&thread->waiting_thread_node,
4346 &proc->waiting_threads);
4347 binder_inner_proc_unlock(proc);
4348 schedule();
4349 binder_inner_proc_lock(proc);
4350 list_del_init(&thread->waiting_thread_node);
4351 if (signal_pending(current)) {
Marco Ballesioc09a5802020-09-16 09:46:36 -07004352 ret = -EINTR;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004353 break;
4354 }
4355 }
4356 finish_wait(&thread->wait, &wait);
4357 binder_inner_proc_unlock(proc);
4358 freezer_count();
4359
4360 return ret;
4361}
4362
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004363static int binder_thread_read(struct binder_proc *proc,
4364 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004365 binder_uintptr_t binder_buffer, size_t size,
4366 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004367{
Arve Hjønnevågda498892014-02-21 14:40:26 -08004368 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004369 void __user *ptr = buffer + *consumed;
4370 void __user *end = buffer + size;
4371
4372 int ret = 0;
4373 int wait_for_proc_work;
4374
4375 if (*consumed == 0) {
4376 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4377 return -EFAULT;
4378 ptr += sizeof(uint32_t);
4379 }
4380
4381retry:
Martijn Coenen0b89d692017-06-29 12:02:06 -07004382 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004383 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen0b89d692017-06-29 12:02:06 -07004384 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004385
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004386 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004387
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004388 trace_binder_wait_for_work(wait_for_proc_work,
4389 !!thread->transaction_stack,
Todd Kjos72196392017-06-29 12:02:02 -07004390 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004391 if (wait_for_proc_work) {
4392 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4393 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304394 binder_user_error("%d:%d ERROR: Thread waiting for process work before calling BC_REGISTER_LOOPER or BC_ENTER_LOOPER (state %x)\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004395 proc->pid, thread->pid, thread->looper);
4396 wait_event_interruptible(binder_user_error_wait,
4397 binder_stop_on_user_error < 2);
4398 }
Martijn Coenen22b061b2017-05-26 10:48:56 -07004399 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004400 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004401
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004402 if (non_block) {
4403 if (!binder_has_work(thread, wait_for_proc_work))
4404 ret = -EAGAIN;
4405 } else {
4406 ret = binder_wait_for_work(thread, wait_for_proc_work);
4407 }
4408
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004409 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4410
4411 if (ret)
4412 return ret;
4413
4414 while (1) {
4415 uint32_t cmd;
Todd Kjos5312a992019-01-14 09:10:21 -08004416 struct binder_transaction_data_secctx tr;
4417 struct binder_transaction_data *trd = &tr.transaction_data;
Todd Kjos72196392017-06-29 12:02:02 -07004418 struct binder_work *w = NULL;
4419 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004420 struct binder_transaction *t = NULL;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004421 struct binder_thread *t_from;
Todd Kjos5312a992019-01-14 09:10:21 -08004422 size_t trsize = sizeof(*trd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004423
Todd Kjosed297212017-06-29 12:02:01 -07004424 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004425 if (!binder_worklist_empty_ilocked(&thread->todo))
4426 list = &thread->todo;
4427 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4428 wait_for_proc_work)
4429 list = &proc->todo;
4430 else {
4431 binder_inner_proc_unlock(proc);
4432
Dmitry Voytik395262a2014-09-08 18:16:34 +04004433 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07004434 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004435 goto retry;
4436 break;
4437 }
4438
Todd Kjosed297212017-06-29 12:02:01 -07004439 if (end - ptr < sizeof(tr) + 4) {
4440 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004441 break;
Todd Kjosed297212017-06-29 12:02:01 -07004442 }
Todd Kjos72196392017-06-29 12:02:02 -07004443 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen8e65ef22017-10-19 15:04:46 +02004444 if (binder_worklist_empty_ilocked(&thread->todo))
4445 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004446
4447 switch (w->type) {
4448 case BINDER_WORK_TRANSACTION: {
Todd Kjosed297212017-06-29 12:02:01 -07004449 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004450 t = container_of(w, struct binder_transaction, work);
4451 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004452 case BINDER_WORK_RETURN_ERROR: {
4453 struct binder_error *e = container_of(
4454 w, struct binder_error, work);
4455
4456 WARN_ON(e->cmd == BR_OK);
Todd Kjosed297212017-06-29 12:02:01 -07004457 binder_inner_proc_unlock(proc);
Todd Kjos26549d12017-06-29 12:01:55 -07004458 if (put_user(e->cmd, (uint32_t __user *)ptr))
4459 return -EFAULT;
宋金时30814a12018-05-10 02:05:03 +00004460 cmd = e->cmd;
Todd Kjos26549d12017-06-29 12:01:55 -07004461 e->cmd = BR_OK;
4462 ptr += sizeof(uint32_t);
4463
Todd Kjos4f9adc82017-08-08 15:48:36 -07004464 binder_stat_br(proc, thread, e->cmd);
Todd Kjos26549d12017-06-29 12:01:55 -07004465 } break;
Hang Lu4dbc1682021-04-09 17:40:46 +08004466 case BINDER_WORK_TRANSACTION_COMPLETE:
Li Liaf1f9842022-11-23 12:16:54 -08004467 case BINDER_WORK_TRANSACTION_PENDING:
Hang Lu4dbc1682021-04-09 17:40:46 +08004468 case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: {
4469 if (proc->oneway_spam_detection_enabled &&
4470 w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT)
4471 cmd = BR_ONEWAY_SPAM_SUSPECT;
Li Liaf1f9842022-11-23 12:16:54 -08004472 else if (w->type == BINDER_WORK_TRANSACTION_PENDING)
4473 cmd = BR_TRANSACTION_PENDING_FROZEN;
Hang Lu4dbc1682021-04-09 17:40:46 +08004474 else
4475 cmd = BR_TRANSACTION_COMPLETE;
Todd Kjosed297212017-06-29 12:02:01 -07004476 binder_inner_proc_unlock(proc);
Todd Kjos67f6f4a2019-06-21 10:54:15 -07004477 kfree(w);
4478 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004479 if (put_user(cmd, (uint32_t __user *)ptr))
4480 return -EFAULT;
4481 ptr += sizeof(uint32_t);
4482
4483 binder_stat_br(proc, thread, cmd);
4484 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304485 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004486 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004487 } break;
4488 case BINDER_WORK_NODE: {
4489 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07004490 int strong, weak;
4491 binder_uintptr_t node_ptr = node->ptr;
4492 binder_uintptr_t node_cookie = node->cookie;
4493 int node_debug_id = node->debug_id;
4494 int has_weak_ref;
4495 int has_strong_ref;
4496 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004497
Todd Kjos26b47d82017-06-29 12:01:47 -07004498 BUG_ON(proc != node->proc);
4499 strong = node->internal_strong_refs ||
4500 node->local_strong_refs;
4501 weak = !hlist_empty(&node->refs) ||
Todd Kjosadc18842017-06-29 12:01:59 -07004502 node->local_weak_refs ||
4503 node->tmp_refs || strong;
Todd Kjos26b47d82017-06-29 12:01:47 -07004504 has_strong_ref = node->has_strong_ref;
4505 has_weak_ref = node->has_weak_ref;
4506
4507 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004508 node->has_weak_ref = 1;
4509 node->pending_weak_ref = 1;
4510 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07004511 }
4512 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004513 node->has_strong_ref = 1;
4514 node->pending_strong_ref = 1;
4515 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07004516 }
4517 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004518 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07004519 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004520 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07004521 if (!weak && !strong) {
4522 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4523 "%d:%d node %d u%016llx c%016llx deleted\n",
4524 proc->pid, thread->pid,
4525 node_debug_id,
4526 (u64)node_ptr,
4527 (u64)node_cookie);
4528 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosed297212017-06-29 12:02:01 -07004529 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07004530 binder_node_lock(node);
4531 /*
4532 * Acquire the node lock before freeing the
4533 * node to serialize with other threads that
4534 * may have been holding the node lock while
4535 * decrementing this node (avoids race where
4536 * this thread frees while the other thread
4537 * is unlocking the node after the final
4538 * decrement)
4539 */
4540 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07004541 binder_free_node(node);
4542 } else
4543 binder_inner_proc_unlock(proc);
4544
Todd Kjos26b47d82017-06-29 12:01:47 -07004545 if (weak && !has_weak_ref)
4546 ret = binder_put_node_cmd(
4547 proc, thread, &ptr, node_ptr,
4548 node_cookie, node_debug_id,
4549 BR_INCREFS, "BR_INCREFS");
4550 if (!ret && strong && !has_strong_ref)
4551 ret = binder_put_node_cmd(
4552 proc, thread, &ptr, node_ptr,
4553 node_cookie, node_debug_id,
4554 BR_ACQUIRE, "BR_ACQUIRE");
4555 if (!ret && !strong && has_strong_ref)
4556 ret = binder_put_node_cmd(
4557 proc, thread, &ptr, node_ptr,
4558 node_cookie, node_debug_id,
4559 BR_RELEASE, "BR_RELEASE");
4560 if (!ret && !weak && has_weak_ref)
4561 ret = binder_put_node_cmd(
4562 proc, thread, &ptr, node_ptr,
4563 node_cookie, node_debug_id,
4564 BR_DECREFS, "BR_DECREFS");
4565 if (orig_ptr == ptr)
4566 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4567 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4568 proc->pid, thread->pid,
4569 node_debug_id,
4570 (u64)node_ptr,
4571 (u64)node_cookie);
4572 if (ret)
4573 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574 } break;
4575 case BINDER_WORK_DEAD_BINDER:
4576 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4577 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4578 struct binder_ref_death *death;
4579 uint32_t cmd;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004580 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004581
4582 death = container_of(w, struct binder_ref_death, work);
4583 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4584 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4585 else
4586 cmd = BR_DEAD_BINDER;
Martijn Coenenab51ec62017-06-29 12:02:10 -07004587 cookie = death->cookie;
4588
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004589 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004590 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004591 proc->pid, thread->pid,
4592 cmd == BR_DEAD_BINDER ?
4593 "BR_DEAD_BINDER" :
4594 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenab51ec62017-06-29 12:02:10 -07004595 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004596 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenab51ec62017-06-29 12:02:10 -07004597 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004598 kfree(death);
4599 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjosed297212017-06-29 12:02:01 -07004600 } else {
Todd Kjos72196392017-06-29 12:02:02 -07004601 binder_enqueue_work_ilocked(
4602 w, &proc->delivered_death);
Todd Kjosed297212017-06-29 12:02:01 -07004603 binder_inner_proc_unlock(proc);
4604 }
Martijn Coenenab51ec62017-06-29 12:02:10 -07004605 if (put_user(cmd, (uint32_t __user *)ptr))
4606 return -EFAULT;
4607 ptr += sizeof(uint32_t);
4608 if (put_user(cookie,
4609 (binder_uintptr_t __user *)ptr))
4610 return -EFAULT;
4611 ptr += sizeof(binder_uintptr_t);
4612 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004613 if (cmd == BR_DEAD_BINDER)
4614 goto done; /* DEAD_BINDER notifications can cause transactions */
4615 } break;
4616 }
4617
4618 if (!t)
4619 continue;
4620
4621 BUG_ON(t->buffer == NULL);
4622 if (t->buffer->target_node) {
4623 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07004624 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004625
Todd Kjos5312a992019-01-14 09:10:21 -08004626 trd->target.ptr = target_node->ptr;
4627 trd->cookie = target_node->cookie;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07004628 node_prio.sched_policy = target_node->sched_policy;
4629 node_prio.prio = target_node->min_priority;
Martijn Coenenfb92c342017-06-23 10:13:43 -07004630 binder_transaction_priority(current, t, node_prio,
4631 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004632 cmd = BR_TRANSACTION;
4633 } else {
Todd Kjos5312a992019-01-14 09:10:21 -08004634 trd->target.ptr = 0;
4635 trd->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004636 cmd = BR_REPLY;
4637 }
Todd Kjos5312a992019-01-14 09:10:21 -08004638 trd->code = t->code;
4639 trd->flags = t->flags;
4640 trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004641
Todd Kjos7a4408c2017-06-29 12:01:57 -07004642 t_from = binder_get_txn_from(t);
4643 if (t_from) {
4644 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004645
Todd Kjos5312a992019-01-14 09:10:21 -08004646 trd->sender_pid =
4647 task_tgid_nr_ns(sender,
4648 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004649 } else {
Todd Kjos5312a992019-01-14 09:10:21 -08004650 trd->sender_pid = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004651 }
4652
Todd Kjos5312a992019-01-14 09:10:21 -08004653 trd->data_size = t->buffer->data_size;
4654 trd->offsets_size = t->buffer->offsets_size;
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08004655 trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data;
Todd Kjos5312a992019-01-14 09:10:21 -08004656 trd->data.ptr.offsets = trd->data.ptr.buffer +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004657 ALIGN(t->buffer->data_size,
4658 sizeof(void *));
4659
Todd Kjos5312a992019-01-14 09:10:21 -08004660 tr.secctx = t->security_ctx;
4661 if (t->security_ctx) {
4662 cmd = BR_TRANSACTION_SEC_CTX;
4663 trsize = sizeof(tr);
4664 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07004665 if (put_user(cmd, (uint32_t __user *)ptr)) {
4666 if (t_from)
4667 binder_thread_dec_tmpref(t_from);
Martijn Coenenfd822492017-08-24 15:23:36 +02004668
4669 binder_cleanup_transaction(t, "put_user failed",
4670 BR_FAILED_REPLY);
4671
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004672 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004673 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004674 ptr += sizeof(uint32_t);
Todd Kjos5312a992019-01-14 09:10:21 -08004675 if (copy_to_user(ptr, &tr, trsize)) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004676 if (t_from)
4677 binder_thread_dec_tmpref(t_from);
Martijn Coenenfd822492017-08-24 15:23:36 +02004678
4679 binder_cleanup_transaction(t, "copy_to_user failed",
4680 BR_FAILED_REPLY);
4681
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004682 return -EFAULT;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004683 }
Todd Kjos5312a992019-01-14 09:10:21 -08004684 ptr += trsize;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004685
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004686 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004687 binder_stat_br(proc, thread, cmd);
4688 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004689 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004690 proc->pid, thread->pid,
4691 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
Todd Kjos5312a992019-01-14 09:10:21 -08004692 (cmd == BR_TRANSACTION_SEC_CTX) ?
4693 "BR_TRANSACTION_SEC_CTX" : "BR_REPLY",
Todd Kjos7a4408c2017-06-29 12:01:57 -07004694 t->debug_id, t_from ? t_from->proc->pid : 0,
4695 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004696 t->buffer->data_size, t->buffer->offsets_size,
Todd Kjos5312a992019-01-14 09:10:21 -08004697 (u64)trd->data.ptr.buffer,
4698 (u64)trd->data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004699
Todd Kjos7a4408c2017-06-29 12:01:57 -07004700 if (t_from)
4701 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004702 t->buffer->allow_user_free = 1;
Todd Kjos5312a992019-01-14 09:10:21 -08004703 if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen0b89d692017-06-29 12:02:06 -07004704 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004705 t->to_parent = thread->transaction_stack;
4706 t->to_thread = thread;
4707 thread->transaction_stack = t;
Martijn Coenen0b89d692017-06-29 12:02:06 -07004708 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004709 } else {
Todd Kjosb6d282c2017-06-29 12:01:54 -07004710 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004711 }
4712 break;
4713 }
4714
4715done:
4716
4717 *consumed = ptr - buffer;
Todd Kjosb3e68612017-06-29 12:02:07 -07004718 binder_inner_proc_lock(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004719 if (proc->requested_threads == 0 &&
4720 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004721 proc->requested_threads_started < proc->max_threads &&
4722 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4723 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4724 /*spawn a new thread if we leave this out */) {
4725 proc->requested_threads++;
Todd Kjosb3e68612017-06-29 12:02:07 -07004726 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004727 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304728 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004729 proc->pid, thread->pid);
4730 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4731 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004732 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosb3e68612017-06-29 12:02:07 -07004733 } else
4734 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004735 return 0;
4736}
4737
Todd Kjos72196392017-06-29 12:02:02 -07004738static void binder_release_work(struct binder_proc *proc,
4739 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004740{
4741 struct binder_work *w;
Todd Kjosbe84da12020-10-09 16:24:55 -07004742 enum binder_work_type wtype;
Seunghun Lee10f62862014-05-01 01:30:23 +09004743
Todd Kjos72196392017-06-29 12:02:02 -07004744 while (1) {
Todd Kjosbe84da12020-10-09 16:24:55 -07004745 binder_inner_proc_lock(proc);
4746 w = binder_dequeue_work_head_ilocked(list);
4747 wtype = w ? w->type : 0;
4748 binder_inner_proc_unlock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07004749 if (!w)
4750 return;
4751
Todd Kjosbe84da12020-10-09 16:24:55 -07004752 switch (wtype) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004753 case BINDER_WORK_TRANSACTION: {
4754 struct binder_transaction *t;
4755
4756 t = container_of(w, struct binder_transaction, work);
Martijn Coenenfd822492017-08-24 15:23:36 +02004757
4758 binder_cleanup_transaction(t, "process died.",
4759 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004760 } break;
Todd Kjos26549d12017-06-29 12:01:55 -07004761 case BINDER_WORK_RETURN_ERROR: {
4762 struct binder_error *e = container_of(
4763 w, struct binder_error, work);
4764
4765 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4766 "undelivered TRANSACTION_ERROR: %u\n",
4767 e->cmd);
4768 } break;
Carlos Llamasea134932023-09-22 17:51:37 +00004769 case BINDER_WORK_TRANSACTION_PENDING:
4770 case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004771 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004772 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304773 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004774 kfree(w);
4775 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4776 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004777 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4778 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4779 struct binder_ref_death *death;
4780
4781 death = container_of(w, struct binder_ref_death, work);
4782 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004783 "undelivered death notification, %016llx\n",
4784 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004785 kfree(death);
4786 binder_stats_deleted(BINDER_STAT_DEATH);
4787 } break;
Todd Kjosbe84da12020-10-09 16:24:55 -07004788 case BINDER_WORK_NODE:
4789 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004790 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304791 pr_err("unexpected work type, %d, not freed\n",
Todd Kjosbe84da12020-10-09 16:24:55 -07004792 wtype);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004793 break;
4794 }
4795 }
4796
4797}
4798
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004799static struct binder_thread *binder_get_thread_ilocked(
4800 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004801{
4802 struct binder_thread *thread = NULL;
4803 struct rb_node *parent = NULL;
4804 struct rb_node **p = &proc->threads.rb_node;
4805
4806 while (*p) {
4807 parent = *p;
4808 thread = rb_entry(parent, struct binder_thread, rb_node);
4809
4810 if (current->pid < thread->pid)
4811 p = &(*p)->rb_left;
4812 else if (current->pid > thread->pid)
4813 p = &(*p)->rb_right;
4814 else
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004815 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004816 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004817 if (!new_thread)
4818 return NULL;
4819 thread = new_thread;
4820 binder_stats_created(BINDER_STAT_THREAD);
4821 thread->proc = proc;
4822 thread->pid = current->pid;
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07004823 get_task_struct(current);
4824 thread->task = current;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004825 atomic_set(&thread->tmp_ref, 0);
4826 init_waitqueue_head(&thread->wait);
4827 INIT_LIST_HEAD(&thread->todo);
4828 rb_link_node(&thread->rb_node, parent, p);
4829 rb_insert_color(&thread->rb_node, &proc->threads);
4830 thread->looper_need_return = true;
4831 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4832 thread->return_error.cmd = BR_OK;
4833 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4834 thread->reply_error.cmd = BR_OK;
Carlos Llamasea1d78b2022-04-29 23:56:41 +00004835 thread->ee.command = BR_OK;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004836 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004837 return thread;
4838}
4839
4840static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4841{
4842 struct binder_thread *thread;
4843 struct binder_thread *new_thread;
4844
4845 binder_inner_proc_lock(proc);
4846 thread = binder_get_thread_ilocked(proc, NULL);
4847 binder_inner_proc_unlock(proc);
4848 if (!thread) {
4849 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4850 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004851 return NULL;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004852 binder_inner_proc_lock(proc);
4853 thread = binder_get_thread_ilocked(proc, new_thread);
4854 binder_inner_proc_unlock(proc);
4855 if (thread != new_thread)
4856 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004857 }
4858 return thread;
4859}
4860
Todd Kjos7a4408c2017-06-29 12:01:57 -07004861static void binder_free_proc(struct binder_proc *proc)
4862{
Todd Kjos9cf08b82020-06-22 13:07:15 -07004863 struct binder_device *device;
4864
Todd Kjos7a4408c2017-06-29 12:01:57 -07004865 BUG_ON(!list_empty(&proc->todo));
4866 BUG_ON(!list_empty(&proc->delivered_death));
Marco Ballesio620ede42021-03-09 18:18:56 -08004867 WARN_ON(proc->outstanding_txns);
Todd Kjos9cf08b82020-06-22 13:07:15 -07004868 device = container_of(proc->context, struct binder_device, context);
4869 if (refcount_dec_and_test(&device->ref)) {
4870 kfree(proc->context->name);
4871 kfree(device);
4872 }
Todd Kjos7a4408c2017-06-29 12:01:57 -07004873 binder_alloc_deferred_release(&proc->alloc);
4874 put_task_struct(proc->tsk);
Todd Kjosae4a7b92021-10-12 09:56:12 -07004875 put_cred(proc->cred);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004876 binder_stats_deleted(BINDER_STAT_PROC);
4877 kfree(proc);
4878}
4879
4880static void binder_free_thread(struct binder_thread *thread)
4881{
4882 BUG_ON(!list_empty(&thread->todo));
4883 binder_stats_deleted(BINDER_STAT_THREAD);
4884 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen7a6edeb2017-06-07 10:02:12 -07004885 put_task_struct(thread->task);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004886 kfree(thread);
4887}
4888
4889static int binder_thread_release(struct binder_proc *proc,
4890 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004891{
4892 struct binder_transaction *t;
4893 struct binder_transaction *send_reply = NULL;
4894 int active_transactions = 0;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004895 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004896
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004897 binder_inner_proc_lock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004898 /*
4899 * take a ref on the proc so it survives
4900 * after we remove this thread from proc->threads.
4901 * The corresponding dec is when we actually
4902 * free the thread in binder_free_thread()
4903 */
4904 proc->tmp_ref++;
4905 /*
4906 * take a ref on this thread to ensure it
4907 * survives while we are releasing it
4908 */
4909 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004910 rb_erase(&thread->rb_node, &proc->threads);
4911 t = thread->transaction_stack;
Todd Kjos7a4408c2017-06-29 12:01:57 -07004912 if (t) {
4913 spin_lock(&t->lock);
4914 if (t->to_thread == thread)
4915 send_reply = t;
4916 }
4917 thread->is_dead = true;
4918
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004919 while (t) {
Todd Kjos7a4408c2017-06-29 12:01:57 -07004920 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004921 active_transactions++;
4922 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304923 "release %d:%d transaction %d %s, still active\n",
4924 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004925 t->debug_id,
4926 (t->to_thread == thread) ? "in" : "out");
4927
4928 if (t->to_thread == thread) {
Marco Ballesio620ede42021-03-09 18:18:56 -08004929 t->to_proc->outstanding_txns--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004930 t->to_proc = NULL;
4931 t->to_thread = NULL;
4932 if (t->buffer) {
4933 t->buffer->transaction = NULL;
4934 t->buffer = NULL;
4935 }
4936 t = t->to_parent;
4937 } else if (t->from == thread) {
4938 t->from = NULL;
4939 t = t->from_parent;
4940 } else
4941 BUG();
Todd Kjos7a4408c2017-06-29 12:01:57 -07004942 spin_unlock(&last_t->lock);
4943 if (t)
4944 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004945 }
Martijn Coenen7a3cee42018-01-05 11:27:07 +01004946
4947 /*
Eric Biggers7bfd8e12021-12-10 16:19:25 -08004948 * If this thread used poll, make sure we remove the waitqueue from any
4949 * poll data structures holding it.
Martijn Coenen7a3cee42018-01-05 11:27:07 +01004950 */
Eric Biggers7bfd8e12021-12-10 16:19:25 -08004951 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4952 wake_up_pollfree(&thread->wait);
Martijn Coenen7a3cee42018-01-05 11:27:07 +01004953
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07004954 binder_inner_proc_unlock(thread->proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004955
Martijn Coenen441b5d12018-02-16 09:47:15 +01004956 /*
Eric Biggers7bfd8e12021-12-10 16:19:25 -08004957 * This is needed to avoid races between wake_up_pollfree() above and
4958 * someone else removing the last entry from the queue for other reasons
4959 * (e.g. ep_remove_wait_queue() being called due to an epoll file
4960 * descriptor being closed). Such other users hold an RCU read lock, so
4961 * we can be sure they're done after we call synchronize_rcu().
Martijn Coenen441b5d12018-02-16 09:47:15 +01004962 */
4963 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4964 synchronize_rcu();
4965
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004966 if (send_reply)
4967 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos72196392017-06-29 12:02:02 -07004968 binder_release_work(proc, &thread->todo);
Todd Kjos7a4408c2017-06-29 12:01:57 -07004969 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004970 return active_transactions;
4971}
4972
4973static unsigned int binder_poll(struct file *filp,
4974 struct poll_table_struct *wait)
4975{
4976 struct binder_proc *proc = filp->private_data;
4977 struct binder_thread *thread = NULL;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004978 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004979
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004980 thread = binder_get_thread(proc);
Eric Biggers047ba512018-01-30 23:11:24 -08004981 if (!thread)
4982 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004983
Martijn Coenen0b89d692017-06-29 12:02:06 -07004984 binder_inner_proc_lock(thread->proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004985 thread->looper |= BINDER_LOOPER_STATE_POLL;
4986 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4987
Martijn Coenen0b89d692017-06-29 12:02:06 -07004988 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004989
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004990 poll_wait(filp, &thread->wait, wait);
4991
Martijn Coenen66b83a42017-10-09 14:26:56 +02004992 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02004993 return POLLIN;
4994
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004995 return 0;
4996}
4997
Jiazi.Li5be7e3c2022-11-15 20:03:51 +08004998static int binder_ioctl_write_read(struct file *filp, unsigned long arg,
Tair Rzayev78260ac2014-06-03 22:27:21 +03004999 struct binder_thread *thread)
5000{
5001 int ret = 0;
5002 struct binder_proc *proc = filp->private_data;
Tair Rzayev78260ac2014-06-03 22:27:21 +03005003 void __user *ubuf = (void __user *)arg;
5004 struct binder_write_read bwr;
5005
Tair Rzayev78260ac2014-06-03 22:27:21 +03005006 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
5007 ret = -EFAULT;
5008 goto out;
5009 }
5010 binder_debug(BINDER_DEBUG_READ_WRITE,
5011 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
5012 proc->pid, thread->pid,
5013 (u64)bwr.write_size, (u64)bwr.write_buffer,
5014 (u64)bwr.read_size, (u64)bwr.read_buffer);
5015
5016 if (bwr.write_size > 0) {
5017 ret = binder_thread_write(proc, thread,
5018 bwr.write_buffer,
5019 bwr.write_size,
5020 &bwr.write_consumed);
5021 trace_binder_write_done(ret);
5022 if (ret < 0) {
5023 bwr.read_consumed = 0;
5024 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
5025 ret = -EFAULT;
5026 goto out;
5027 }
5028 }
5029 if (bwr.read_size > 0) {
5030 ret = binder_thread_read(proc, thread, bwr.read_buffer,
5031 bwr.read_size,
5032 &bwr.read_consumed,
5033 filp->f_flags & O_NONBLOCK);
5034 trace_binder_read_done(ret);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005035 binder_inner_proc_lock(proc);
5036 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen408c68b2017-08-31 10:04:19 +02005037 binder_wakeup_proc_ilocked(proc);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005038 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005039 if (ret < 0) {
5040 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
5041 ret = -EFAULT;
5042 goto out;
5043 }
5044 }
5045 binder_debug(BINDER_DEBUG_READ_WRITE,
5046 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
5047 proc->pid, thread->pid,
5048 (u64)bwr.write_consumed, (u64)bwr.write_size,
5049 (u64)bwr.read_consumed, (u64)bwr.read_size);
5050 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
5051 ret = -EFAULT;
5052 goto out;
5053 }
5054out:
5055 return ret;
5056}
5057
Todd Kjos5312a992019-01-14 09:10:21 -08005058static int binder_ioctl_set_ctx_mgr(struct file *filp,
5059 struct flat_binder_object *fbo)
Tair Rzayev78260ac2014-06-03 22:27:21 +03005060{
5061 int ret = 0;
5062 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08005063 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07005064 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03005065 kuid_t curr_euid = current_euid();
5066
Todd Kjosc44b1232017-06-29 12:01:43 -07005067 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08005068 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005069 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
5070 ret = -EBUSY;
5071 goto out;
5072 }
Todd Kjos9693ca72021-10-12 09:56:13 -07005073 ret = security_binder_set_context_mgr(proc->cred);
Stephen Smalley79af7302015-01-21 10:54:10 -05005074 if (ret < 0)
5075 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08005076 if (uid_valid(context->binder_context_mgr_uid)) {
5077 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005078 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
5079 from_kuid(&init_user_ns, curr_euid),
5080 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08005081 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03005082 ret = -EPERM;
5083 goto out;
5084 }
5085 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08005086 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03005087 }
Todd Kjos5312a992019-01-14 09:10:21 -08005088 new_node = binder_new_node(proc, fbo);
Todd Kjosc44b1232017-06-29 12:01:43 -07005089 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005090 ret = -ENOMEM;
5091 goto out;
5092 }
Todd Kjos673068e2017-06-29 12:02:03 -07005093 binder_node_lock(new_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07005094 new_node->local_weak_refs++;
5095 new_node->local_strong_refs++;
5096 new_node->has_strong_ref = 1;
5097 new_node->has_weak_ref = 1;
5098 context->binder_context_mgr_node = new_node;
Todd Kjos673068e2017-06-29 12:02:03 -07005099 binder_node_unlock(new_node);
Todd Kjosadc18842017-06-29 12:01:59 -07005100 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005101out:
Todd Kjosc44b1232017-06-29 12:01:43 -07005102 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005103 return ret;
5104}
5105
Martijn Coenen666c4202018-08-25 13:50:56 -07005106static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc,
5107 struct binder_node_info_for_ref *info)
5108{
5109 struct binder_node *node;
5110 struct binder_context *context = proc->context;
5111 __u32 handle = info->handle;
5112
5113 if (info->strong_count || info->weak_count || info->reserved1 ||
5114 info->reserved2 || info->reserved3) {
5115 binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.",
5116 proc->pid);
5117 return -EINVAL;
5118 }
5119
5120 /* This ioctl may only be used by the context manager */
5121 mutex_lock(&context->context_mgr_node_lock);
5122 if (!context->binder_context_mgr_node ||
5123 context->binder_context_mgr_node->proc != proc) {
5124 mutex_unlock(&context->context_mgr_node_lock);
5125 return -EPERM;
5126 }
5127 mutex_unlock(&context->context_mgr_node_lock);
5128
5129 node = binder_get_node_from_ref(proc, handle, true, NULL);
5130 if (!node)
5131 return -EINVAL;
5132
5133 info->strong_count = node->local_strong_refs +
5134 node->internal_strong_refs;
5135 info->weak_count = node->local_weak_refs;
5136
5137 binder_put_node(node);
5138
5139 return 0;
5140}
5141
Colin Crossabcc6152017-08-31 10:04:24 +02005142static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
5143 struct binder_node_debug_info *info)
5144{
5145 struct rb_node *n;
5146 binder_uintptr_t ptr = info->ptr;
5147
5148 memset(info, 0, sizeof(*info));
5149
5150 binder_inner_proc_lock(proc);
5151 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
5152 struct binder_node *node = rb_entry(n, struct binder_node,
5153 rb_node);
5154 if (node->ptr > ptr) {
5155 info->ptr = node->ptr;
5156 info->cookie = node->cookie;
5157 info->has_strong_ref = node->has_strong_ref;
5158 info->has_weak_ref = node->has_weak_ref;
5159 break;
5160 }
5161 }
5162 binder_inner_proc_unlock(proc);
5163
5164 return 0;
5165}
5166
Li Li503f84d2021-09-07 16:12:53 -07005167static bool binder_txns_pending_ilocked(struct binder_proc *proc)
5168{
5169 struct rb_node *n;
5170 struct binder_thread *thread;
5171
5172 if (proc->outstanding_txns > 0)
5173 return true;
5174
5175 for (n = rb_first(&proc->threads); n; n = rb_next(n)) {
5176 thread = rb_entry(n, struct binder_thread, rb_node);
5177 if (thread->transaction_stack)
5178 return true;
5179 }
5180 return false;
5181}
5182
Marco Ballesiod819dde2021-01-07 17:02:50 -08005183static int binder_ioctl_freeze(struct binder_freeze_info *info,
5184 struct binder_proc *target_proc)
5185{
5186 int ret = 0;
5187
5188 if (!info->enable) {
5189 binder_inner_proc_lock(target_proc);
5190 target_proc->sync_recv = false;
5191 target_proc->async_recv = false;
5192 target_proc->is_frozen = false;
5193 binder_inner_proc_unlock(target_proc);
5194 return 0;
5195 }
5196
5197 /*
5198 * Freezing the target. Prevent new transactions by
5199 * setting frozen state. If timeout specified, wait
5200 * for transactions to drain.
5201 */
5202 binder_inner_proc_lock(target_proc);
5203 target_proc->sync_recv = false;
5204 target_proc->async_recv = false;
5205 target_proc->is_frozen = true;
5206 binder_inner_proc_unlock(target_proc);
5207
5208 if (info->timeout_ms > 0)
5209 ret = wait_event_interruptible_timeout(
5210 target_proc->freeze_wait,
5211 (!target_proc->outstanding_txns),
5212 msecs_to_jiffies(info->timeout_ms));
5213
Li Li503f84d2021-09-07 16:12:53 -07005214 /* Check pending transactions that wait for reply */
5215 if (ret >= 0) {
5216 binder_inner_proc_lock(target_proc);
5217 if (binder_txns_pending_ilocked(target_proc))
5218 ret = -EAGAIN;
5219 binder_inner_proc_unlock(target_proc);
5220 }
Marco Ballesiod819dde2021-01-07 17:02:50 -08005221
5222 if (ret < 0) {
5223 binder_inner_proc_lock(target_proc);
5224 target_proc->is_frozen = false;
5225 binder_inner_proc_unlock(target_proc);
5226 }
5227
5228 return ret;
5229}
5230
Marco Ballesio45490402020-09-10 13:39:20 -07005231static int binder_ioctl_get_freezer_info(
5232 struct binder_frozen_status_info *info)
5233{
5234 struct binder_proc *target_proc;
5235 bool found = false;
Li Li503f84d2021-09-07 16:12:53 -07005236 __u32 txns_pending;
Marco Ballesio45490402020-09-10 13:39:20 -07005237
Marco Ballesiod819dde2021-01-07 17:02:50 -08005238 info->sync_recv = 0;
5239 info->async_recv = 0;
5240
Marco Ballesio45490402020-09-10 13:39:20 -07005241 mutex_lock(&binder_procs_lock);
5242 hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5243 if (target_proc->pid == info->pid) {
5244 found = true;
5245 binder_inner_proc_lock(target_proc);
Li Li503f84d2021-09-07 16:12:53 -07005246 txns_pending = binder_txns_pending_ilocked(target_proc);
5247 info->sync_recv |= target_proc->sync_recv |
5248 (txns_pending << 1);
Marco Ballesiod819dde2021-01-07 17:02:50 -08005249 info->async_recv |= target_proc->async_recv;
Marco Ballesio45490402020-09-10 13:39:20 -07005250 binder_inner_proc_unlock(target_proc);
Marco Ballesio45490402020-09-10 13:39:20 -07005251 }
5252 }
5253 mutex_unlock(&binder_procs_lock);
5254
5255 if (!found)
5256 return -EINVAL;
5257
Marco Ballesio45490402020-09-10 13:39:20 -07005258 return 0;
5259}
5260
Carlos Llamasea1d78b2022-04-29 23:56:41 +00005261static int binder_ioctl_get_extended_error(struct binder_thread *thread,
5262 void __user *ubuf)
5263{
Schspa Shi44c2e232022-05-18 09:17:54 +08005264 struct binder_extended_error ee;
Carlos Llamasea1d78b2022-04-29 23:56:41 +00005265
5266 binder_inner_proc_lock(thread->proc);
Schspa Shi44c2e232022-05-18 09:17:54 +08005267 ee = thread->ee;
5268 binder_set_extended_error(&thread->ee, 0, BR_OK, 0);
Carlos Llamasea1d78b2022-04-29 23:56:41 +00005269 binder_inner_proc_unlock(thread->proc);
5270
Schspa Shi44c2e232022-05-18 09:17:54 +08005271 if (copy_to_user(ubuf, &ee, sizeof(ee)))
5272 return -EFAULT;
5273
Carlos Llamasea1d78b2022-04-29 23:56:41 +00005274 return 0;
5275}
5276
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
5278{
5279 int ret;
5280 struct binder_proc *proc = filp->private_data;
5281 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005282 void __user *ubuf = (void __user *)arg;
5283
Tair Rzayev78260ac2014-06-03 22:27:21 +03005284 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
5285 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005286
Sherry Yang4175e2b2017-08-23 08:46:40 -07005287 binder_selftest_alloc(&proc->alloc);
5288
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005289 trace_binder_ioctl(cmd, arg);
5290
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005291 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
5292 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005293 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005294
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005295 thread = binder_get_thread(proc);
5296 if (thread == NULL) {
5297 ret = -ENOMEM;
5298 goto err;
5299 }
5300
5301 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03005302 case BINDER_WRITE_READ:
Jiazi.Li5be7e3c2022-11-15 20:03:51 +08005303 ret = binder_ioctl_write_read(filp, arg, thread);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005304 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005305 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005306 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07005307 case BINDER_SET_MAX_THREADS: {
Carlos Llamasf642f362024-04-21 17:37:49 +00005308 u32 max_threads;
Todd Kjosb3e68612017-06-29 12:02:07 -07005309
5310 if (copy_from_user(&max_threads, ubuf,
5311 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005312 ret = -EINVAL;
5313 goto err;
5314 }
Todd Kjosb3e68612017-06-29 12:02:07 -07005315 binder_inner_proc_lock(proc);
5316 proc->max_threads = max_threads;
5317 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005318 break;
Todd Kjosb3e68612017-06-29 12:02:07 -07005319 }
Todd Kjos5312a992019-01-14 09:10:21 -08005320 case BINDER_SET_CONTEXT_MGR_EXT: {
5321 struct flat_binder_object fbo;
5322
5323 if (copy_from_user(&fbo, ubuf, sizeof(fbo))) {
5324 ret = -EINVAL;
5325 goto err;
5326 }
5327 ret = binder_ioctl_set_ctx_mgr(filp, &fbo);
5328 if (ret)
5329 goto err;
5330 break;
5331 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005332 case BINDER_SET_CONTEXT_MGR:
Todd Kjos5312a992019-01-14 09:10:21 -08005333 ret = binder_ioctl_set_ctx_mgr(filp, NULL);
Tair Rzayev78260ac2014-06-03 22:27:21 +03005334 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005335 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005336 break;
5337 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05305338 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005339 proc->pid, thread->pid);
Todd Kjos7a4408c2017-06-29 12:01:57 -07005340 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005341 thread = NULL;
5342 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005343 case BINDER_VERSION: {
5344 struct binder_version __user *ver = ubuf;
5345
Mathieu Maret36c89c02014-04-15 12:03:05 +02005346 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
5347 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005348 ret = -EINVAL;
5349 goto err;
5350 }
5351 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02005352 }
Martijn Coenen666c4202018-08-25 13:50:56 -07005353 case BINDER_GET_NODE_INFO_FOR_REF: {
5354 struct binder_node_info_for_ref info;
5355
5356 if (copy_from_user(&info, ubuf, sizeof(info))) {
5357 ret = -EFAULT;
5358 goto err;
5359 }
5360
5361 ret = binder_ioctl_get_node_info_for_ref(proc, &info);
5362 if (ret < 0)
5363 goto err;
5364
5365 if (copy_to_user(ubuf, &info, sizeof(info))) {
5366 ret = -EFAULT;
5367 goto err;
5368 }
5369
5370 break;
5371 }
Colin Crossabcc6152017-08-31 10:04:24 +02005372 case BINDER_GET_NODE_DEBUG_INFO: {
5373 struct binder_node_debug_info info;
5374
5375 if (copy_from_user(&info, ubuf, sizeof(info))) {
5376 ret = -EFAULT;
5377 goto err;
5378 }
5379
5380 ret = binder_ioctl_get_node_debug_info(proc, &info);
5381 if (ret < 0)
5382 goto err;
5383
5384 if (copy_to_user(ubuf, &info, sizeof(info))) {
5385 ret = -EFAULT;
5386 goto err;
5387 }
5388 break;
5389 }
Marco Ballesio620ede42021-03-09 18:18:56 -08005390 case BINDER_FREEZE: {
5391 struct binder_freeze_info info;
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005392 struct binder_proc **target_procs = NULL, *target_proc;
5393 int target_procs_count = 0, i = 0;
5394
5395 ret = 0;
Marco Ballesio620ede42021-03-09 18:18:56 -08005396
5397 if (copy_from_user(&info, ubuf, sizeof(info))) {
5398 ret = -EFAULT;
5399 goto err;
5400 }
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005401
Marco Ballesio620ede42021-03-09 18:18:56 -08005402 mutex_lock(&binder_procs_lock);
5403 hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005404 if (target_proc->pid == info.pid)
5405 target_procs_count++;
5406 }
Marco Ballesiod819dde2021-01-07 17:02:50 -08005407
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005408 if (target_procs_count == 0) {
5409 mutex_unlock(&binder_procs_lock);
5410 ret = -EINVAL;
5411 goto err;
5412 }
Marco Ballesiod819dde2021-01-07 17:02:50 -08005413
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005414 target_procs = kmalloc(sizeof(struct binder_proc *) *
5415 target_procs_count,
5416 GFP_KERNEL);
Marco Ballesiod819dde2021-01-07 17:02:50 -08005417
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005418 if (!target_procs) {
5419 mutex_unlock(&binder_procs_lock);
5420 ret = -ENOMEM;
5421 goto err;
5422 }
5423
5424 hlist_for_each_entry(target_proc, &binder_procs, proc_node) {
5425 if (target_proc->pid != info.pid)
5426 continue;
5427
5428 binder_inner_proc_lock(target_proc);
5429 target_proc->tmp_ref++;
5430 binder_inner_proc_unlock(target_proc);
5431
5432 target_procs[i++] = target_proc;
Marco Ballesio620ede42021-03-09 18:18:56 -08005433 }
5434 mutex_unlock(&binder_procs_lock);
Marco Ballesiod819dde2021-01-07 17:02:50 -08005435
Marco Ballesio9ad13ab2021-01-22 09:26:14 -08005436 for (i = 0; i < target_procs_count; i++) {
5437 if (ret >= 0)
5438 ret = binder_ioctl_freeze(&info,
5439 target_procs[i]);
5440
5441 binder_proc_dec_tmpref(target_procs[i]);
5442 }
5443
5444 kfree(target_procs);
5445
Marco Ballesio620ede42021-03-09 18:18:56 -08005446 if (ret < 0)
5447 goto err;
5448 break;
5449 }
Marco Ballesio45490402020-09-10 13:39:20 -07005450 case BINDER_GET_FROZEN_INFO: {
5451 struct binder_frozen_status_info info;
5452
5453 if (copy_from_user(&info, ubuf, sizeof(info))) {
5454 ret = -EFAULT;
5455 goto err;
5456 }
5457
5458 ret = binder_ioctl_get_freezer_info(&info);
5459 if (ret < 0)
5460 goto err;
5461
5462 if (copy_to_user(ubuf, &info, sizeof(info))) {
5463 ret = -EFAULT;
5464 goto err;
5465 }
5466 break;
5467 }
Hang Lu4dbc1682021-04-09 17:40:46 +08005468 case BINDER_ENABLE_ONEWAY_SPAM_DETECTION: {
5469 uint32_t enable;
5470
5471 if (copy_from_user(&enable, ubuf, sizeof(enable))) {
Luca Stefani68bbe372021-05-06 21:37:25 +02005472 ret = -EFAULT;
Hang Lu4dbc1682021-04-09 17:40:46 +08005473 goto err;
5474 }
5475 binder_inner_proc_lock(proc);
5476 proc->oneway_spam_detection_enabled = (bool)enable;
5477 binder_inner_proc_unlock(proc);
5478 break;
5479 }
Carlos Llamasea1d78b2022-04-29 23:56:41 +00005480 case BINDER_GET_EXTENDED_ERROR:
5481 ret = binder_ioctl_get_extended_error(thread, ubuf);
5482 if (ret < 0)
5483 goto err;
5484 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005485 default:
5486 ret = -EINVAL;
5487 goto err;
5488 }
5489 ret = 0;
5490err:
5491 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07005492 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005493 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
Marco Ballesio49fd0542021-01-08 08:06:32 -08005494 if (ret && ret != -EINTR)
Anmol Sarma56b468f2012-10-30 22:35:43 +05305495 pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005496err_unlocked:
5497 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005498 return ret;
5499}
5500
5501static void binder_vma_open(struct vm_area_struct *vma)
5502{
5503 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005504
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005505 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305506 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005507 proc->pid, vma->vm_start, vma->vm_end,
5508 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5509 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510}
5511
5512static void binder_vma_close(struct vm_area_struct *vma)
5513{
5514 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005515
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005516 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05305517 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005518 proc->pid, vma->vm_start, vma->vm_end,
5519 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5520 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07005521 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005522 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
5523}
5524
Dave Jiang11bac802017-02-24 14:56:41 -08005525static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05305526{
5527 return VM_FAULT_SIGBUS;
5528}
5529
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07005530static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005531 .open = binder_vma_open,
5532 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05305533 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005534};
5535
Todd Kjos19c98722017-06-29 12:01:40 -07005536static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
5537{
5538 int ret;
5539 struct binder_proc *proc = filp->private_data;
5540 const char *failure_string;
5541
5542 if (proc->tsk != current->group_leader)
5543 return -EINVAL;
5544
5545 if ((vma->vm_end - vma->vm_start) > SZ_4M)
5546 vma->vm_end = vma->vm_start + SZ_4M;
5547
5548 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5549 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
5550 __func__, proc->pid, vma->vm_start, vma->vm_end,
5551 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
5552 (unsigned long)pgprot_val(vma->vm_page_prot));
5553
5554 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
5555 ret = -EPERM;
5556 failure_string = "bad vm_flags";
5557 goto err_bad_arg;
5558 }
Minchan Kim864095d2018-05-07 23:15:37 +09005559 vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP;
5560 vma->vm_flags &= ~VM_MAYWRITE;
5561
Todd Kjos19c98722017-06-29 12:01:40 -07005562 vma->vm_ops = &binder_vm_ops;
5563 vma->vm_private_data = proc;
5564
5565 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
5566 if (ret)
5567 return ret;
Todd Kjos029876d2017-11-10 15:30:27 -08005568 mutex_lock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07005569 proc->files = get_files_struct(current);
Todd Kjos029876d2017-11-10 15:30:27 -08005570 mutex_unlock(&proc->files_lock);
Todd Kjos19c98722017-06-29 12:01:40 -07005571 return 0;
5572
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005573err_bad_arg:
Elad Wexler47e2dd32017-12-29 11:03:37 +02005574 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
5576 return ret;
5577}
5578
5579static int binder_open(struct inode *nodp, struct file *filp)
5580{
5581 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08005582 struct binder_device *binder_dev;
Hridya Valsaraju1405bf22019-09-03 09:16:55 -07005583 struct binderfs_info *info;
5584 struct dentry *binder_binderfs_dir_entry_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005585
Elad Wexler47e2dd32017-12-29 11:03:37 +02005586 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005587 current->group_leader->pid, current->pid);
5588
5589 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
5590 if (proc == NULL)
5591 return -ENOMEM;
Todd Kjos9630fe82017-06-29 12:02:00 -07005592 spin_lock_init(&proc->inner_lock);
5593 spin_lock_init(&proc->outer_lock);
Todd Kjosc4ea41b2017-06-29 12:01:36 -07005594 get_task_struct(current->group_leader);
5595 proc->tsk = current->group_leader;
Todd Kjos029876d2017-11-10 15:30:27 -08005596 mutex_init(&proc->files_lock);
Todd Kjosae4a7b92021-10-12 09:56:12 -07005597 proc->cred = get_cred(filp->f_cred);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005598 INIT_LIST_HEAD(&proc->todo);
Marco Ballesio620ede42021-03-09 18:18:56 -08005599 init_waitqueue_head(&proc->freeze_wait);
Martijn Coenen37b34412017-06-06 17:04:42 -07005600 if (binder_supported_policy(current->policy)) {
5601 proc->default_priority.sched_policy = current->policy;
5602 proc->default_priority.prio = current->normal_prio;
5603 } else {
5604 proc->default_priority.sched_policy = SCHED_NORMAL;
5605 proc->default_priority.prio = NICE_TO_PRIO(0);
5606 }
5607
Christian Brauner34fbd922018-12-14 13:11:14 +01005608 /* binderfs stashes devices in i_private */
Hridya Valsaraju1405bf22019-09-03 09:16:55 -07005609 if (is_binderfs_device(nodp)) {
Christian Brauner63e14d32020-03-03 17:43:40 +01005610 binder_dev = nodp->i_private;
Hridya Valsaraju1405bf22019-09-03 09:16:55 -07005611 info = nodp->i_sb->s_fs_info;
5612 binder_binderfs_dir_entry_proc = info->proc_log_dir;
5613 } else {
Christian Brauner34fbd922018-12-14 13:11:14 +01005614 binder_dev = container_of(filp->private_data,
5615 struct binder_device, miscdev);
Hridya Valsaraju1405bf22019-09-03 09:16:55 -07005616 }
Christian Braunerbbfd2162020-03-03 17:43:40 +01005617 refcount_inc(&binder_dev->ref);
Martijn Coenenac4812c2017-02-03 14:40:48 -08005618 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07005619 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005620
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005621 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005622 proc->pid = current->group_leader->pid;
5623 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02005624 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005625 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005626
Todd Kjosc44b1232017-06-29 12:01:43 -07005627 mutex_lock(&binder_procs_lock);
5628 hlist_add_head(&proc->proc_node, &binder_procs);
5629 mutex_unlock(&binder_procs_lock);
5630
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005631 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005632 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09005633
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005634 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08005635 /*
5636 * proc debug entries are shared between contexts, so
5637 * this will fail if the process tries to open the driver
5638 * again with a different context. The priting code will
5639 * anyway print all contexts that a given PID has, so this
5640 * is not a problem.
5641 */
Harsh Shandilyac5b47d22017-12-22 19:37:02 +05305642 proc->debugfs_entry = debugfs_create_file(strbuf, 0444,
Martijn Coenen14db3182017-02-03 14:40:47 -08005643 binder_debugfs_dir_entry_proc,
5644 (void *)(unsigned long)proc->pid,
Yangtao Li8b2246a2018-11-30 20:26:30 -05005645 &proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005646 }
5647
Hridya Valsaraju1405bf22019-09-03 09:16:55 -07005648 if (binder_binderfs_dir_entry_proc) {
5649 char strbuf[11];
5650 struct dentry *binderfs_entry;
5651
5652 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
5653 /*
5654 * Similar to debugfs, the process specific log file is shared
5655 * between contexts. If the file has already been created for a
5656 * process, the following binderfs_create_file() call will
5657 * fail with error code EEXIST if another context of the same
5658 * process invoked binder_open(). This is ok since same as
5659 * debugfs, the log file will contain information on all
5660 * contexts of a given PID.
5661 */
5662 binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc,
5663 strbuf, &proc_fops, (void *)(unsigned long)proc->pid);
5664 if (!IS_ERR(binderfs_entry)) {
5665 proc->binderfs_entry = binderfs_entry;
5666 } else {
5667 int error;
5668
5669 error = PTR_ERR(binderfs_entry);
5670 if (error != -EEXIST) {
5671 pr_warn("Unable to create file %s in binderfs (error %d)\n",
5672 strbuf, error);
5673 }
5674 }
5675 }
5676
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005677 return 0;
5678}
5679
5680static int binder_flush(struct file *filp, fl_owner_t id)
5681{
5682 struct binder_proc *proc = filp->private_data;
5683
5684 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
5685
5686 return 0;
5687}
5688
5689static void binder_deferred_flush(struct binder_proc *proc)
5690{
5691 struct rb_node *n;
5692 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09005693
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005694 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005695 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
5696 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09005697
Todd Kjos08dabce2017-06-29 12:01:49 -07005698 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005699 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
5700 wake_up_interruptible(&thread->wait);
5701 wake_count++;
5702 }
5703 }
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005704 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005705
5706 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
5707 "binder_flush: %d woke %d threads\n", proc->pid,
5708 wake_count);
5709}
5710
5711static int binder_release(struct inode *nodp, struct file *filp)
5712{
5713 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09005714
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005715 debugfs_remove(proc->debugfs_entry);
Hridya Valsaraju1405bf22019-09-03 09:16:55 -07005716
5717 if (proc->binderfs_entry) {
5718 binderfs_remove_file(proc->binderfs_entry);
5719 proc->binderfs_entry = NULL;
5720 }
5721
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005722 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5723
5724 return 0;
5725}
5726
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005727static int binder_node_release(struct binder_node *node, int refs)
5728{
5729 struct binder_ref *ref;
5730 int death = 0;
Todd Kjosed297212017-06-29 12:02:01 -07005731 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005732
Todd Kjos72196392017-06-29 12:02:02 -07005733 binder_release_work(proc, &node->async_todo);
Todd Kjosed297212017-06-29 12:02:01 -07005734
Todd Kjos673068e2017-06-29 12:02:03 -07005735 binder_node_lock(node);
Todd Kjosed297212017-06-29 12:02:01 -07005736 binder_inner_proc_lock(proc);
Todd Kjos72196392017-06-29 12:02:02 -07005737 binder_dequeue_work_ilocked(&node->work);
Todd Kjosadc18842017-06-29 12:01:59 -07005738 /*
5739 * The caller must have taken a temporary ref on the node,
5740 */
5741 BUG_ON(!node->tmp_refs);
5742 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjosed297212017-06-29 12:02:01 -07005743 binder_inner_proc_unlock(proc);
Todd Kjos673068e2017-06-29 12:02:03 -07005744 binder_node_unlock(node);
Todd Kjosed297212017-06-29 12:02:01 -07005745 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005746
5747 return refs;
5748 }
5749
5750 node->proc = NULL;
5751 node->local_strong_refs = 0;
5752 node->local_weak_refs = 0;
Todd Kjosed297212017-06-29 12:02:01 -07005753 binder_inner_proc_unlock(proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07005754
5755 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005756 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07005757 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005758
5759 hlist_for_each_entry(ref, &node->refs, node_entry) {
5760 refs++;
Martijn Coenenab51ec62017-06-29 12:02:10 -07005761 /*
5762 * Need the node lock to synchronize
5763 * with new notification requests and the
5764 * inner lock to synchronize with queued
5765 * death notifications.
5766 */
5767 binder_inner_proc_lock(ref->proc);
5768 if (!ref->death) {
5769 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005770 continue;
Martijn Coenenab51ec62017-06-29 12:02:10 -07005771 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005772
5773 death++;
5774
Martijn Coenenab51ec62017-06-29 12:02:10 -07005775 BUG_ON(!list_empty(&ref->death->work.entry));
5776 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5777 binder_enqueue_work_ilocked(&ref->death->work,
5778 &ref->proc->todo);
Martijn Coenen408c68b2017-08-31 10:04:19 +02005779 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos72196392017-06-29 12:02:02 -07005780 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005781 }
5782
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005783 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5784 "node %d now dead, refs %d, death %d\n",
5785 node->debug_id, refs, death);
Todd Kjos673068e2017-06-29 12:02:03 -07005786 binder_node_unlock(node);
Todd Kjosadc18842017-06-29 12:01:59 -07005787 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005788
5789 return refs;
5790}
5791
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005792static void binder_deferred_release(struct binder_proc *proc)
5793{
Martijn Coenen342e5c92017-02-03 14:40:46 -08005794 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005795 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07005796 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005797
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005798 BUG_ON(proc->files);
5799
Todd Kjosc44b1232017-06-29 12:01:43 -07005800 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005801 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07005802 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005803
Todd Kjosc44b1232017-06-29 12:01:43 -07005804 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08005805 if (context->binder_context_mgr_node &&
5806 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005807 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005808 "%s: %d context_mgr_node gone\n",
5809 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08005810 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005811 }
Todd Kjosc44b1232017-06-29 12:01:43 -07005812 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005813 binder_inner_proc_lock(proc);
Todd Kjos7a4408c2017-06-29 12:01:57 -07005814 /*
5815 * Make sure proc stays alive after we
5816 * remove all the threads
5817 */
5818 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005819
Todd Kjos7a4408c2017-06-29 12:01:57 -07005820 proc->is_dead = true;
Marco Ballesio620ede42021-03-09 18:18:56 -08005821 proc->is_frozen = false;
Marco Ballesio45490402020-09-10 13:39:20 -07005822 proc->sync_recv = false;
5823 proc->async_recv = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005824 threads = 0;
5825 active_transactions = 0;
5826 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005827 struct binder_thread *thread;
5828
5829 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005830 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005831 threads++;
Todd Kjos7a4408c2017-06-29 12:01:57 -07005832 active_transactions += binder_thread_release(proc, thread);
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07005833 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005834 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005835
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005836 nodes = 0;
5837 incoming_refs = 0;
5838 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005839 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005840
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005841 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005842 nodes++;
Todd Kjosadc18842017-06-29 12:01:59 -07005843 /*
5844 * take a temporary ref on the node before
5845 * calling binder_node_release() which will either
5846 * kfree() the node or call binder_put_node()
5847 */
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005848 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005849 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005850 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005851 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005852 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005853 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07005854 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005855
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005856 outgoing_refs = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005857 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005858 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005859 struct binder_ref *ref;
5860
5861 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005862 outgoing_refs++;
Todd Kjos2c1838d2017-06-29 12:02:08 -07005863 binder_cleanup_ref_olocked(ref);
5864 binder_proc_unlock(proc);
Todd Kjos372e3142017-06-29 12:01:58 -07005865 binder_free_ref(ref);
Todd Kjos2c1838d2017-06-29 12:02:08 -07005866 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005867 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07005868 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005869
Todd Kjos72196392017-06-29 12:02:02 -07005870 binder_release_work(proc, &proc->todo);
5871 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005872
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005873 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07005874 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005875 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07005876 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005877
Todd Kjos7a4408c2017-06-29 12:01:57 -07005878 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005879}
5880
5881static void binder_deferred_func(struct work_struct *work)
5882{
5883 struct binder_proc *proc;
5884 struct files_struct *files;
5885
5886 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005887
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005888 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005889 mutex_lock(&binder_deferred_lock);
5890 if (!hlist_empty(&binder_deferred_list)) {
5891 proc = hlist_entry(binder_deferred_list.first,
5892 struct binder_proc, deferred_work_node);
5893 hlist_del_init(&proc->deferred_work_node);
5894 defer = proc->deferred_work;
5895 proc->deferred_work = 0;
5896 } else {
5897 proc = NULL;
5898 defer = 0;
5899 }
5900 mutex_unlock(&binder_deferred_lock);
5901
5902 files = NULL;
5903 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjos029876d2017-11-10 15:30:27 -08005904 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005905 files = proc->files;
5906 if (files)
5907 proc->files = NULL;
Todd Kjos029876d2017-11-10 15:30:27 -08005908 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005909 }
5910
5911 if (defer & BINDER_DEFERRED_FLUSH)
5912 binder_deferred_flush(proc);
5913
5914 if (defer & BINDER_DEFERRED_RELEASE)
5915 binder_deferred_release(proc); /* frees proc */
5916
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005917 if (files)
5918 put_files_struct(files);
5919 } while (proc);
5920}
5921static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5922
5923static void
5924binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5925{
5926 mutex_lock(&binder_deferred_lock);
5927 proc->deferred_work |= defer;
5928 if (hlist_unhashed(&proc->deferred_work_node)) {
5929 hlist_add_head(&proc->deferred_work_node,
5930 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305931 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005932 }
5933 mutex_unlock(&binder_deferred_lock);
5934}
5935
Todd Kjos5f2f6362017-06-29 12:02:09 -07005936static void print_binder_transaction_ilocked(struct seq_file *m,
5937 struct binder_proc *proc,
5938 const char *prefix,
5939 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005940{
Todd Kjos5f2f6362017-06-29 12:02:09 -07005941 struct binder_proc *to_proc;
5942 struct binder_buffer *buffer = t->buffer;
5943
Todd Kjos7a4408c2017-06-29 12:01:57 -07005944 spin_lock(&t->lock);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005945 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005946 seq_printf(m,
Greg Kroah-Hartmanaf3b8e62018-02-26 09:22:41 +01005947 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %d:%d r%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005948 prefix, t->debug_id, t,
5949 t->from ? t->from->proc->pid : 0,
5950 t->from ? t->from->pid : 0,
Todd Kjos5f2f6362017-06-29 12:02:09 -07005951 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005952 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen37b34412017-06-06 17:04:42 -07005953 t->code, t->flags, t->priority.sched_policy,
5954 t->priority.prio, t->need_reply);
Todd Kjos7a4408c2017-06-29 12:01:57 -07005955 spin_unlock(&t->lock);
5956
Todd Kjos5f2f6362017-06-29 12:02:09 -07005957 if (proc != to_proc) {
5958 /*
5959 * Can only safely deref buffer if we are holding the
5960 * correct proc inner lock for this node
5961 */
5962 seq_puts(m, "\n");
5963 return;
5964 }
5965
5966 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005967 seq_puts(m, " buffer free\n");
5968 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005969 }
Todd Kjos5f2f6362017-06-29 12:02:09 -07005970 if (buffer->target_node)
5971 seq_printf(m, " node %d", buffer->target_node->debug_id);
Todd Kjosb46af092018-02-07 13:57:37 -08005972 seq_printf(m, " size %zd:%zd data %pK\n",
Todd Kjos5f2f6362017-06-29 12:02:09 -07005973 buffer->data_size, buffer->offsets_size,
Todd Kjos9fcfd1e2019-02-08 10:35:20 -08005974 buffer->user_data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005975}
5976
Todd Kjos5f2f6362017-06-29 12:02:09 -07005977static void print_binder_work_ilocked(struct seq_file *m,
5978 struct binder_proc *proc,
5979 const char *prefix,
5980 const char *transaction_prefix,
5981 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005982{
5983 struct binder_node *node;
5984 struct binder_transaction *t;
5985
5986 switch (w->type) {
5987 case BINDER_WORK_TRANSACTION:
5988 t = container_of(w, struct binder_transaction, work);
Todd Kjos5f2f6362017-06-29 12:02:09 -07005989 print_binder_transaction_ilocked(
5990 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005991 break;
Todd Kjos26549d12017-06-29 12:01:55 -07005992 case BINDER_WORK_RETURN_ERROR: {
5993 struct binder_error *e = container_of(
5994 w, struct binder_error, work);
5995
5996 seq_printf(m, "%stransaction error: %u\n",
5997 prefix, e->cmd);
5998 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005999 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006000 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006001 break;
6002 case BINDER_WORK_NODE:
6003 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08006004 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
6005 prefix, node->debug_id,
6006 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006007 break;
6008 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006009 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006010 break;
6011 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006012 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006013 break;
6014 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006015 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006016 break;
6017 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006018 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006019 break;
6020 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006021}
6022
Todd Kjos72196392017-06-29 12:02:02 -07006023static void print_binder_thread_ilocked(struct seq_file *m,
6024 struct binder_thread *thread,
6025 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006026{
6027 struct binder_transaction *t;
6028 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006029 size_t start_pos = m->count;
6030 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006031
Todd Kjos7a4408c2017-06-29 12:01:57 -07006032 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos08dabce2017-06-29 12:01:49 -07006033 thread->pid, thread->looper,
Todd Kjos7a4408c2017-06-29 12:01:57 -07006034 thread->looper_need_return,
6035 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006036 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006037 t = thread->transaction_stack;
6038 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006039 if (t->from == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07006040 print_binder_transaction_ilocked(m, thread->proc,
6041 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006042 t = t->from_parent;
6043 } else if (t->to_thread == thread) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07006044 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006045 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006046 t = t->to_parent;
6047 } else {
Todd Kjos5f2f6362017-06-29 12:02:09 -07006048 print_binder_transaction_ilocked(m, thread->proc,
6049 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006050 t = NULL;
6051 }
6052 }
6053 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos5f2f6362017-06-29 12:02:09 -07006054 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07006055 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006056 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006057 if (!print_always && m->count == header_pos)
6058 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006059}
6060
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006061static void print_binder_node_nilocked(struct seq_file *m,
6062 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006063{
6064 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006065 struct binder_work *w;
6066 int count;
6067
6068 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08006069 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006070 count++;
6071
Martijn Coenen69308b32017-06-07 09:29:14 -07006072 seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d",
Arve Hjønnevågda498892014-02-21 14:40:26 -08006073 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen69308b32017-06-07 09:29:14 -07006074 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006075 node->has_strong_ref, node->has_weak_ref,
6076 node->local_strong_refs, node->local_weak_refs,
Todd Kjosadc18842017-06-29 12:01:59 -07006077 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006078 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006079 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08006080 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006081 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006082 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006083 seq_puts(m, "\n");
Todd Kjos72196392017-06-29 12:02:02 -07006084 if (node->proc) {
Todd Kjos72196392017-06-29 12:02:02 -07006085 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07006086 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos72196392017-06-29 12:02:02 -07006087 " pending async transaction", w);
Todd Kjos72196392017-06-29 12:02:02 -07006088 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006089}
6090
Todd Kjos2c1838d2017-06-29 12:02:08 -07006091static void print_binder_ref_olocked(struct seq_file *m,
6092 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006093{
Todd Kjos673068e2017-06-29 12:02:03 -07006094 binder_node_lock(ref->node);
Todd Kjos372e3142017-06-29 12:01:58 -07006095 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
6096 ref->data.debug_id, ref->data.desc,
6097 ref->node->proc ? "" : "dead ",
6098 ref->node->debug_id, ref->data.strong,
6099 ref->data.weak, ref->death);
Todd Kjos673068e2017-06-29 12:02:03 -07006100 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006101}
6102
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006103static void print_binder_proc(struct seq_file *m,
6104 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006105{
6106 struct binder_work *w;
6107 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006108 size_t start_pos = m->count;
6109 size_t header_pos;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006110 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006111
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006112 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08006113 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006114 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006115
Todd Kjos72196392017-06-29 12:02:02 -07006116 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006117 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos72196392017-06-29 12:02:02 -07006118 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006119 rb_node), print_all);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006120
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006121 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006122 struct binder_node *node = rb_entry(n, struct binder_node,
6123 rb_node);
Todd Kjoscd4b7552018-12-05 15:19:26 -08006124 if (!print_all && !node->has_async_transaction)
6125 continue;
6126
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006127 /*
6128 * take a temporary reference on the node so it
6129 * survives and isn't removed from the tree
6130 * while we print it.
6131 */
6132 binder_inc_node_tmpref_ilocked(node);
6133 /* Need to drop inner lock to take node lock */
6134 binder_inner_proc_unlock(proc);
6135 if (last_node)
6136 binder_put_node(last_node);
6137 binder_node_inner_lock(node);
6138 print_binder_node_nilocked(m, node);
6139 binder_node_inner_unlock(node);
6140 last_node = node;
6141 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006142 }
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006143 binder_inner_proc_unlock(proc);
6144 if (last_node)
6145 binder_put_node(last_node);
6146
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006147 if (print_all) {
Todd Kjos2c1838d2017-06-29 12:02:08 -07006148 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006149 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006150 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006151 n = rb_next(n))
Todd Kjos2c1838d2017-06-29 12:02:08 -07006152 print_binder_ref_olocked(m, rb_entry(n,
6153 struct binder_ref,
6154 rb_node_desc));
6155 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006156 }
Todd Kjos19c98722017-06-29 12:01:40 -07006157 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos72196392017-06-29 12:02:02 -07006158 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006159 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos5f2f6362017-06-29 12:02:09 -07006160 print_binder_work_ilocked(m, proc, " ",
6161 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006162 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006163 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006164 break;
6165 }
Todd Kjos72196392017-06-29 12:02:02 -07006166 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006167 if (!print_all && m->count == header_pos)
6168 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006169}
6170
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10006171static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006172 "BR_ERROR",
6173 "BR_OK",
6174 "BR_TRANSACTION",
6175 "BR_REPLY",
6176 "BR_ACQUIRE_RESULT",
6177 "BR_DEAD_REPLY",
6178 "BR_TRANSACTION_COMPLETE",
6179 "BR_INCREFS",
6180 "BR_ACQUIRE",
6181 "BR_RELEASE",
6182 "BR_DECREFS",
6183 "BR_ATTEMPT_ACQUIRE",
6184 "BR_NOOP",
6185 "BR_SPAWN_LOOPER",
6186 "BR_FINISHED",
6187 "BR_DEAD_BINDER",
6188 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Hang Lu115e9e72021-04-09 17:40:45 +08006189 "BR_FAILED_REPLY",
6190 "BR_FROZEN_REPLY",
Hang Lu4dbc1682021-04-09 17:40:46 +08006191 "BR_ONEWAY_SPAM_SUSPECT",
Li Liaf1f9842022-11-23 12:16:54 -08006192 "BR_TRANSACTION_PENDING_FROZEN"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006193};
6194
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10006195static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006196 "BC_TRANSACTION",
6197 "BC_REPLY",
6198 "BC_ACQUIRE_RESULT",
6199 "BC_FREE_BUFFER",
6200 "BC_INCREFS",
6201 "BC_ACQUIRE",
6202 "BC_RELEASE",
6203 "BC_DECREFS",
6204 "BC_INCREFS_DONE",
6205 "BC_ACQUIRE_DONE",
6206 "BC_ATTEMPT_ACQUIRE",
6207 "BC_REGISTER_LOOPER",
6208 "BC_ENTER_LOOPER",
6209 "BC_EXIT_LOOPER",
6210 "BC_REQUEST_DEATH_NOTIFICATION",
6211 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08006212 "BC_DEAD_BINDER_DONE",
6213 "BC_TRANSACTION_SG",
6214 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006215};
6216
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10006217static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006218 "proc",
6219 "thread",
6220 "node",
6221 "ref",
6222 "death",
6223 "transaction",
6224 "transaction_complete"
6225};
6226
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006227static void print_binder_stats(struct seq_file *m, const char *prefix,
6228 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006229{
6230 int i;
6231
6232 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006233 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006234 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07006235 int temp = atomic_read(&stats->bc[i]);
6236
6237 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006238 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07006239 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006240 }
6241
6242 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006243 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006244 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07006245 int temp = atomic_read(&stats->br[i]);
6246
6247 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006248 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07006249 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006250 }
6251
6252 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006253 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006254 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006255 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006256 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07006257 int created = atomic_read(&stats->obj_created[i]);
6258 int deleted = atomic_read(&stats->obj_deleted[i]);
6259
6260 if (created || deleted)
6261 seq_printf(m, "%s%s: active %d total %d\n",
6262 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006263 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07006264 created - deleted,
6265 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006266 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006267}
6268
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006269static void print_binder_proc_stats(struct seq_file *m,
6270 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006271{
6272 struct binder_work *w;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02006273 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006274 struct rb_node *n;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02006275 int count, strong, weak, ready_threads;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07006276 size_t free_async_space =
6277 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006278
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006279 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08006280 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006281 count = 0;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02006282 ready_threads = 0;
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07006283 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006284 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
6285 count++;
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02006286
6287 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
6288 ready_threads++;
6289
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006290 seq_printf(m, " threads: %d\n", count);
6291 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006292 " ready threads %d\n"
6293 " free async space %zd\n", proc->requested_threads,
6294 proc->requested_threads_started, proc->max_threads,
Martijn Coenen1b77e9d2017-08-31 10:04:18 +02006295 ready_threads,
Todd Kjos7bd7b0e2017-06-29 12:02:05 -07006296 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006297 count = 0;
6298 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
6299 count++;
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006300 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006301 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006302 count = 0;
6303 strong = 0;
6304 weak = 0;
Todd Kjos2c1838d2017-06-29 12:02:08 -07006305 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006306 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
6307 struct binder_ref *ref = rb_entry(n, struct binder_ref,
6308 rb_node_desc);
6309 count++;
Todd Kjos372e3142017-06-29 12:01:58 -07006310 strong += ref->data.strong;
6311 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006312 }
Todd Kjos2c1838d2017-06-29 12:02:08 -07006313 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006314 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006315
Todd Kjos19c98722017-06-29 12:01:40 -07006316 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006317 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006318
Sherry Yang8ef46652017-08-31 11:56:36 -07006319 binder_alloc_print_pages(m, &proc->alloc);
6320
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006321 count = 0;
Todd Kjos72196392017-06-29 12:02:02 -07006322 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006323 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos72196392017-06-29 12:02:02 -07006324 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006325 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006326 }
Todd Kjos72196392017-06-29 12:02:02 -07006327 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006328 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006329
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006330 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006331}
6332
6333
Hridya Valsarajua575fb22019-11-01 15:22:42 -07006334int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006335{
6336 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006337 struct binder_node *node;
Todd Kjos673068e2017-06-29 12:02:03 -07006338 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006339
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006340 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006341
Todd Kjosc44b1232017-06-29 12:01:43 -07006342 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006343 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006344 seq_puts(m, "dead nodes:\n");
Todd Kjos673068e2017-06-29 12:02:03 -07006345 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
6346 /*
6347 * take a temporary reference on the node so it
6348 * survives and isn't removed from the list
6349 * while we print it.
6350 */
6351 node->tmp_refs++;
6352 spin_unlock(&binder_dead_nodes_lock);
6353 if (last_node)
6354 binder_put_node(last_node);
6355 binder_node_lock(node);
Todd Kjosda0fa9e2017-06-29 12:02:04 -07006356 print_binder_node_nilocked(m, node);
Todd Kjos673068e2017-06-29 12:02:03 -07006357 binder_node_unlock(node);
6358 last_node = node;
6359 spin_lock(&binder_dead_nodes_lock);
6360 }
Todd Kjosc44b1232017-06-29 12:01:43 -07006361 spin_unlock(&binder_dead_nodes_lock);
Todd Kjos673068e2017-06-29 12:02:03 -07006362 if (last_node)
6363 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006364
Todd Kjosc44b1232017-06-29 12:01:43 -07006365 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006366 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006367 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07006368 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07006369
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006370 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006371}
6372
Hridya Valsarajua575fb22019-11-01 15:22:42 -07006373int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006374{
6375 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006376
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006377 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006378
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006379 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006380
Todd Kjosc44b1232017-06-29 12:01:43 -07006381 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006382 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006383 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07006384 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07006385
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006386 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006387}
6388
Hridya Valsarajua575fb22019-11-01 15:22:42 -07006389int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006390{
6391 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006392
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006393 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07006394 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006395 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006396 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07006397 mutex_unlock(&binder_procs_lock);
Todd Kjosa60b8902017-06-29 12:02:11 -07006398
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006399 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006400}
6401
Yangtao Li8b2246a2018-11-30 20:26:30 -05006402static int proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006403{
Riley Andrews83050a42016-02-09 21:05:33 -08006404 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08006405 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006406
Todd Kjosc44b1232017-06-29 12:01:43 -07006407 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08006408 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08006409 if (itr->pid == pid) {
6410 seq_puts(m, "binder proc state:\n");
6411 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08006412 }
6413 }
Todd Kjosc44b1232017-06-29 12:01:43 -07006414 mutex_unlock(&binder_procs_lock);
6415
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006416 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006417}
6418
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006419static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006420 struct binder_transaction_log_entry *e)
6421{
Todd Kjosd99c7332017-06-29 12:01:53 -07006422 int debug_id = READ_ONCE(e->debug_id_done);
6423 /*
6424 * read barrier to guarantee debug_id_done read before
6425 * we print the log values
6426 */
6427 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006428 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07006429 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006430 e->debug_id, (e->call_type == 2) ? "reply" :
6431 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08006432 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07006433 e->to_node, e->target_handle, e->data_size, e->offsets_size,
6434 e->return_error, e->return_error_param,
6435 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07006436 /*
6437 * read-barrier to guarantee read of debug_id_done after
6438 * done printing the fields of the entry
6439 */
6440 smp_rmb();
6441 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
6442 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006443}
6444
Hridya Valsaraju37413932019-09-03 09:16:54 -07006445int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006446{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006447 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07006448 unsigned int log_cur = atomic_read(&log->cur);
6449 unsigned int count;
6450 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006451 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006452
Todd Kjosd99c7332017-06-29 12:01:53 -07006453 count = log_cur + 1;
6454 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
6455 0 : count % ARRAY_SIZE(log->entry);
6456 if (count > ARRAY_SIZE(log->entry) || log->full)
6457 count = ARRAY_SIZE(log->entry);
6458 for (i = 0; i < count; i++) {
6459 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
6460
6461 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006462 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07006463 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006464}
6465
Christian Brauner34fbd922018-12-14 13:11:14 +01006466const struct file_operations binder_fops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006467 .owner = THIS_MODULE,
6468 .poll = binder_poll,
6469 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08006470 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006471 .mmap = binder_mmap,
6472 .open = binder_open,
6473 .flush = binder_flush,
6474 .release = binder_release,
6475};
6476
Martijn Coenenac4812c2017-02-03 14:40:48 -08006477static int __init init_binder_device(const char *name)
6478{
6479 int ret;
6480 struct binder_device *binder_device;
6481
6482 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
6483 if (!binder_device)
6484 return -ENOMEM;
6485
6486 binder_device->miscdev.fops = &binder_fops;
6487 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
6488 binder_device->miscdev.name = name;
6489
Christian Braunerbbfd2162020-03-03 17:43:40 +01006490 refcount_set(&binder_device->ref, 1);
Martijn Coenenac4812c2017-02-03 14:40:48 -08006491 binder_device->context.binder_context_mgr_uid = INVALID_UID;
6492 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07006493 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08006494
6495 ret = misc_register(&binder_device->miscdev);
6496 if (ret < 0) {
6497 kfree(binder_device);
6498 return ret;
6499 }
6500
6501 hlist_add_head(&binder_device->hlist, &binder_devices);
6502
6503 return ret;
6504}
6505
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006506static int __init binder_init(void)
6507{
6508 int ret;
Christian Braunerbf4b5da2019-01-31 01:25:02 +01006509 char *device_name, *device_tmp;
Martijn Coenenac4812c2017-02-03 14:40:48 -08006510 struct binder_device *device;
6511 struct hlist_node *tmp;
Christian Braunerbf4b5da2019-01-31 01:25:02 +01006512 char *device_names = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006513
Tetsuo Handa2db8e0b2017-11-29 22:29:47 +09006514 ret = binder_alloc_shrinker_init();
6515 if (ret)
6516 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07006517
Todd Kjosd99c7332017-06-29 12:01:53 -07006518 atomic_set(&binder_transaction_log.cur, ~0U);
6519 atomic_set(&binder_transaction_log_failed.cur, ~0U);
6520
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006521 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
6522 if (binder_debugfs_dir_entry_root)
6523 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
6524 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08006525
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006526 if (binder_debugfs_dir_entry_root) {
6527 debugfs_create_file("state",
Harsh Shandilyac5b47d22017-12-22 19:37:02 +05306528 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006529 binder_debugfs_dir_entry_root,
6530 NULL,
Hridya Valsarajua575fb22019-11-01 15:22:42 -07006531 &binder_state_fops);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006532 debugfs_create_file("stats",
Harsh Shandilyac5b47d22017-12-22 19:37:02 +05306533 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006534 binder_debugfs_dir_entry_root,
6535 NULL,
Hridya Valsarajua575fb22019-11-01 15:22:42 -07006536 &binder_stats_fops);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006537 debugfs_create_file("transactions",
Harsh Shandilyac5b47d22017-12-22 19:37:02 +05306538 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006539 binder_debugfs_dir_entry_root,
6540 NULL,
Hridya Valsarajua575fb22019-11-01 15:22:42 -07006541 &binder_transactions_fops);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006542 debugfs_create_file("transaction_log",
Harsh Shandilyac5b47d22017-12-22 19:37:02 +05306543 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006544 binder_debugfs_dir_entry_root,
6545 &binder_transaction_log,
Hridya Valsaraju37413932019-09-03 09:16:54 -07006546 &binder_transaction_log_fops);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006547 debugfs_create_file("failed_transaction_log",
Harsh Shandilyac5b47d22017-12-22 19:37:02 +05306548 0444,
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07006549 binder_debugfs_dir_entry_root,
6550 &binder_transaction_log_failed,
Hridya Valsaraju37413932019-09-03 09:16:54 -07006551 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006552 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08006553
Hridya Valsarajue8fb3932019-08-08 15:27:25 -07006554 if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) &&
6555 strcmp(binder_devices_param, "") != 0) {
Christian Brauner574e6b02019-01-26 11:23:20 +01006556 /*
6557 * Copy the module_parameter string, because we don't want to
6558 * tokenize it in-place.
6559 */
6560 device_names = kstrdup(binder_devices_param, GFP_KERNEL);
6561 if (!device_names) {
6562 ret = -ENOMEM;
6563 goto err_alloc_device_names_failed;
6564 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08006565
Christian Brauner574e6b02019-01-26 11:23:20 +01006566 device_tmp = device_names;
6567 while ((device_name = strsep(&device_tmp, ","))) {
6568 ret = init_binder_device(device_name);
6569 if (ret)
6570 goto err_init_binder_device_failed;
6571 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08006572 }
6573
Christian Braunerbf4b5da2019-01-31 01:25:02 +01006574 ret = init_binderfs();
6575 if (ret)
6576 goto err_init_binder_device_failed;
6577
Martijn Coenenac4812c2017-02-03 14:40:48 -08006578 return ret;
6579
6580err_init_binder_device_failed:
6581 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
6582 misc_deregister(&device->miscdev);
6583 hlist_del(&device->hlist);
6584 kfree(device);
6585 }
Christian Brauner22eb9472017-08-21 16:13:28 +02006586
6587 kfree(device_names);
6588
Martijn Coenenac4812c2017-02-03 14:40:48 -08006589err_alloc_device_names_failed:
6590 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
Qi Zheng486dd742023-06-25 15:49:37 +00006591 binder_alloc_shrinker_exit();
Martijn Coenenac4812c2017-02-03 14:40:48 -08006592
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006593 return ret;
6594}
6595
6596device_initcall(binder_init);
6597
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07006598#define CREATE_TRACE_POINTS
6599#include "binder_trace.h"
6600
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09006601MODULE_LICENSE("GPL v2");