13ad20fe3SChristian Brauner /* SPDX-License-Identifier: GPL-2.0 */
23ad20fe3SChristian Brauner
33ad20fe3SChristian Brauner #ifndef _LINUX_BINDER_INTERNAL_H
43ad20fe3SChristian Brauner #define _LINUX_BINDER_INTERNAL_H
53ad20fe3SChristian Brauner
63ad20fe3SChristian Brauner #include <linux/export.h>
73ad20fe3SChristian Brauner #include <linux/fs.h>
83ad20fe3SChristian Brauner #include <linux/list.h>
93ad20fe3SChristian Brauner #include <linux/miscdevice.h>
103ad20fe3SChristian Brauner #include <linux/mutex.h>
11f0fe2c0fSChristian Brauner #include <linux/refcount.h>
123ad20fe3SChristian Brauner #include <linux/stddef.h>
133ad20fe3SChristian Brauner #include <linux/types.h>
143ad20fe3SChristian Brauner #include <linux/uidgid.h>
15421518a2SFrankie.Chang #include <uapi/linux/android/binderfs.h>
16421518a2SFrankie.Chang #include "binder_alloc.h"
173ad20fe3SChristian Brauner
183ad20fe3SChristian Brauner struct binder_context {
193ad20fe3SChristian Brauner struct binder_node *binder_context_mgr_node;
203ad20fe3SChristian Brauner struct mutex context_mgr_node_lock;
213ad20fe3SChristian Brauner kuid_t binder_context_mgr_uid;
223ad20fe3SChristian Brauner const char *name;
233ad20fe3SChristian Brauner };
243ad20fe3SChristian Brauner
253ad20fe3SChristian Brauner /**
263ad20fe3SChristian Brauner * struct binder_device - information about a binder device node
273ad20fe3SChristian Brauner * @hlist: list of binder devices (only used for devices requested via
283ad20fe3SChristian Brauner * CONFIG_ANDROID_BINDER_DEVICES)
293ad20fe3SChristian Brauner * @miscdev: information about a binder character device node
303ad20fe3SChristian Brauner * @context: binder context information
313ad20fe3SChristian Brauner * @binderfs_inode: This is the inode of the root dentry of the super block
323ad20fe3SChristian Brauner * belonging to a binderfs mount.
333ad20fe3SChristian Brauner */
343ad20fe3SChristian Brauner struct binder_device {
353ad20fe3SChristian Brauner struct hlist_node hlist;
363ad20fe3SChristian Brauner struct miscdevice miscdev;
373ad20fe3SChristian Brauner struct binder_context context;
383ad20fe3SChristian Brauner struct inode *binderfs_inode;
39f0fe2c0fSChristian Brauner refcount_t ref;
403ad20fe3SChristian Brauner };
413ad20fe3SChristian Brauner
424feb80faSHridya Valsaraju /**
434feb80faSHridya Valsaraju * binderfs_mount_opts - mount options for binderfs
444feb80faSHridya Valsaraju * @max: maximum number of allocatable binderfs binder devices
454feb80faSHridya Valsaraju * @stats_mode: enable binder stats in binderfs.
464feb80faSHridya Valsaraju */
474feb80faSHridya Valsaraju struct binderfs_mount_opts {
484feb80faSHridya Valsaraju int max;
494feb80faSHridya Valsaraju int stats_mode;
504feb80faSHridya Valsaraju };
514feb80faSHridya Valsaraju
524feb80faSHridya Valsaraju /**
534feb80faSHridya Valsaraju * binderfs_info - information about a binderfs mount
544feb80faSHridya Valsaraju * @ipc_ns: The ipc namespace the binderfs mount belongs to.
554feb80faSHridya Valsaraju * @control_dentry: This records the dentry of this binderfs mount
564feb80faSHridya Valsaraju * binder-control device.
574feb80faSHridya Valsaraju * @root_uid: uid that needs to be used when a new binder device is
584feb80faSHridya Valsaraju * created.
594feb80faSHridya Valsaraju * @root_gid: gid that needs to be used when a new binder device is
604feb80faSHridya Valsaraju * created.
614feb80faSHridya Valsaraju * @mount_opts: The mount options in use.
624feb80faSHridya Valsaraju * @device_count: The current number of allocated binder devices.
634feb80faSHridya Valsaraju * @proc_log_dir: Pointer to the directory dentry containing process-specific
644feb80faSHridya Valsaraju * logs.
654feb80faSHridya Valsaraju */
664feb80faSHridya Valsaraju struct binderfs_info {
674feb80faSHridya Valsaraju struct ipc_namespace *ipc_ns;
684feb80faSHridya Valsaraju struct dentry *control_dentry;
694feb80faSHridya Valsaraju kuid_t root_uid;
704feb80faSHridya Valsaraju kgid_t root_gid;
714feb80faSHridya Valsaraju struct binderfs_mount_opts mount_opts;
724feb80faSHridya Valsaraju int device_count;
734feb80faSHridya Valsaraju struct dentry *proc_log_dir;
744feb80faSHridya Valsaraju };
754feb80faSHridya Valsaraju
763ad20fe3SChristian Brauner extern const struct file_operations binder_fops;
773ad20fe3SChristian Brauner
78ca2864c6SHridya Valsaraju extern char *binder_devices_param;
79ca2864c6SHridya Valsaraju
803ad20fe3SChristian Brauner #ifdef CONFIG_ANDROID_BINDERFS
813ad20fe3SChristian Brauner extern bool is_binderfs_device(const struct inode *inode);
824feb80faSHridya Valsaraju extern struct dentry *binderfs_create_file(struct dentry *dir, const char *name,
834feb80faSHridya Valsaraju const struct file_operations *fops,
844feb80faSHridya Valsaraju void *data);
854feb80faSHridya Valsaraju extern void binderfs_remove_file(struct dentry *dentry);
863ad20fe3SChristian Brauner #else
is_binderfs_device(const struct inode * inode)873ad20fe3SChristian Brauner static inline bool is_binderfs_device(const struct inode *inode)
883ad20fe3SChristian Brauner {
893ad20fe3SChristian Brauner return false;
903ad20fe3SChristian Brauner }
binderfs_create_file(struct dentry * dir,const char * name,const struct file_operations * fops,void * data)914feb80faSHridya Valsaraju static inline struct dentry *binderfs_create_file(struct dentry *dir,
924feb80faSHridya Valsaraju const char *name,
934feb80faSHridya Valsaraju const struct file_operations *fops,
944feb80faSHridya Valsaraju void *data)
954feb80faSHridya Valsaraju {
964feb80faSHridya Valsaraju return NULL;
974feb80faSHridya Valsaraju }
binderfs_remove_file(struct dentry * dentry)984feb80faSHridya Valsaraju static inline void binderfs_remove_file(struct dentry *dentry) {}
993ad20fe3SChristian Brauner #endif
1003ad20fe3SChristian Brauner
1015b9633afSChristian Brauner #ifdef CONFIG_ANDROID_BINDERFS
1025b9633afSChristian Brauner extern int __init init_binderfs(void);
1035b9633afSChristian Brauner #else
init_binderfs(void)1045b9633afSChristian Brauner static inline int __init init_binderfs(void)
1055b9633afSChristian Brauner {
1065b9633afSChristian Brauner return 0;
1075b9633afSChristian Brauner }
1085b9633afSChristian Brauner #endif
1095b9633afSChristian Brauner
110b7e241bbSCarlos Llamas struct binder_debugfs_entry {
111b7e241bbSCarlos Llamas const char *name;
112b7e241bbSCarlos Llamas umode_t mode;
113b7e241bbSCarlos Llamas const struct file_operations *fops;
114b7e241bbSCarlos Llamas void *data;
11503e2e07eSHridya Valsaraju };
11603e2e07eSHridya Valsaraju
117b7e241bbSCarlos Llamas extern const struct binder_debugfs_entry binder_debugfs_entries[];
118b7e241bbSCarlos Llamas
119b7e241bbSCarlos Llamas #define binder_for_each_debugfs_entry(entry) \
120b7e241bbSCarlos Llamas for ((entry) = binder_debugfs_entries; \
121b7e241bbSCarlos Llamas (entry)->name; \
122b7e241bbSCarlos Llamas (entry)++)
12303e2e07eSHridya Valsaraju
124421518a2SFrankie.Chang enum binder_stat_types {
125421518a2SFrankie.Chang BINDER_STAT_PROC,
126421518a2SFrankie.Chang BINDER_STAT_THREAD,
127421518a2SFrankie.Chang BINDER_STAT_NODE,
128421518a2SFrankie.Chang BINDER_STAT_REF,
129421518a2SFrankie.Chang BINDER_STAT_DEATH,
130421518a2SFrankie.Chang BINDER_STAT_TRANSACTION,
131421518a2SFrankie.Chang BINDER_STAT_TRANSACTION_COMPLETE,
132421518a2SFrankie.Chang BINDER_STAT_COUNT
133421518a2SFrankie.Chang };
134421518a2SFrankie.Chang
135421518a2SFrankie.Chang struct binder_stats {
1360567461aSLi Li atomic_t br[_IOC_NR(BR_TRANSACTION_PENDING_FROZEN) + 1];
137421518a2SFrankie.Chang atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
138421518a2SFrankie.Chang atomic_t obj_created[BINDER_STAT_COUNT];
139421518a2SFrankie.Chang atomic_t obj_deleted[BINDER_STAT_COUNT];
140421518a2SFrankie.Chang };
141421518a2SFrankie.Chang
142421518a2SFrankie.Chang /**
143421518a2SFrankie.Chang * struct binder_work - work enqueued on a worklist
144421518a2SFrankie.Chang * @entry: node enqueued on list
145421518a2SFrankie.Chang * @type: type of work to be performed
146421518a2SFrankie.Chang *
147421518a2SFrankie.Chang * There are separate work lists for proc, thread, and node (async).
148421518a2SFrankie.Chang */
149421518a2SFrankie.Chang struct binder_work {
150421518a2SFrankie.Chang struct list_head entry;
151421518a2SFrankie.Chang
152421518a2SFrankie.Chang enum binder_work_type {
153421518a2SFrankie.Chang BINDER_WORK_TRANSACTION = 1,
154421518a2SFrankie.Chang BINDER_WORK_TRANSACTION_COMPLETE,
1550567461aSLi Li BINDER_WORK_TRANSACTION_PENDING,
156a7dc1e6fSHang Lu BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT,
157421518a2SFrankie.Chang BINDER_WORK_RETURN_ERROR,
158421518a2SFrankie.Chang BINDER_WORK_NODE,
159421518a2SFrankie.Chang BINDER_WORK_DEAD_BINDER,
160421518a2SFrankie.Chang BINDER_WORK_DEAD_BINDER_AND_CLEAR,
161421518a2SFrankie.Chang BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
162421518a2SFrankie.Chang } type;
163421518a2SFrankie.Chang };
164421518a2SFrankie.Chang
165421518a2SFrankie.Chang struct binder_error {
166421518a2SFrankie.Chang struct binder_work work;
167421518a2SFrankie.Chang uint32_t cmd;
168421518a2SFrankie.Chang };
169421518a2SFrankie.Chang
170421518a2SFrankie.Chang /**
171421518a2SFrankie.Chang * struct binder_node - binder node bookkeeping
172421518a2SFrankie.Chang * @debug_id: unique ID for debugging
173421518a2SFrankie.Chang * (invariant after initialized)
174421518a2SFrankie.Chang * @lock: lock for node fields
175421518a2SFrankie.Chang * @work: worklist element for node work
176421518a2SFrankie.Chang * (protected by @proc->inner_lock)
177421518a2SFrankie.Chang * @rb_node: element for proc->nodes tree
178421518a2SFrankie.Chang * (protected by @proc->inner_lock)
179421518a2SFrankie.Chang * @dead_node: element for binder_dead_nodes list
180421518a2SFrankie.Chang * (protected by binder_dead_nodes_lock)
181421518a2SFrankie.Chang * @proc: binder_proc that owns this node
182421518a2SFrankie.Chang * (invariant after initialized)
183421518a2SFrankie.Chang * @refs: list of references on this node
184421518a2SFrankie.Chang * (protected by @lock)
185421518a2SFrankie.Chang * @internal_strong_refs: used to take strong references when
186421518a2SFrankie.Chang * initiating a transaction
187421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
188421518a2SFrankie.Chang * and by @lock)
189421518a2SFrankie.Chang * @local_weak_refs: weak user refs from local process
190421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
191421518a2SFrankie.Chang * and by @lock)
192421518a2SFrankie.Chang * @local_strong_refs: strong user refs from local process
193421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
194421518a2SFrankie.Chang * and by @lock)
195421518a2SFrankie.Chang * @tmp_refs: temporary kernel refs
196421518a2SFrankie.Chang * (protected by @proc->inner_lock while @proc
197421518a2SFrankie.Chang * is valid, and by binder_dead_nodes_lock
198421518a2SFrankie.Chang * if @proc is NULL. During inc/dec and node release
199421518a2SFrankie.Chang * it is also protected by @lock to provide safety
200421518a2SFrankie.Chang * as the node dies and @proc becomes NULL)
201421518a2SFrankie.Chang * @ptr: userspace pointer for node
202421518a2SFrankie.Chang * (invariant, no lock needed)
203421518a2SFrankie.Chang * @cookie: userspace cookie for node
204421518a2SFrankie.Chang * (invariant, no lock needed)
205421518a2SFrankie.Chang * @has_strong_ref: userspace notified of strong ref
206421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
207421518a2SFrankie.Chang * and by @lock)
208421518a2SFrankie.Chang * @pending_strong_ref: userspace has acked notification of strong ref
209421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
210421518a2SFrankie.Chang * and by @lock)
211421518a2SFrankie.Chang * @has_weak_ref: userspace notified of weak ref
212421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
213421518a2SFrankie.Chang * and by @lock)
214421518a2SFrankie.Chang * @pending_weak_ref: userspace has acked notification of weak ref
215421518a2SFrankie.Chang * (protected by @proc->inner_lock if @proc
216421518a2SFrankie.Chang * and by @lock)
217421518a2SFrankie.Chang * @has_async_transaction: async transaction to node in progress
218421518a2SFrankie.Chang * (protected by @lock)
219421518a2SFrankie.Chang * @accept_fds: file descriptor operations supported for node
220421518a2SFrankie.Chang * (invariant after initialized)
221421518a2SFrankie.Chang * @min_priority: minimum scheduling priority
222421518a2SFrankie.Chang * (invariant after initialized)
223421518a2SFrankie.Chang * @txn_security_ctx: require sender's security context
224421518a2SFrankie.Chang * (invariant after initialized)
225421518a2SFrankie.Chang * @async_todo: list of async work items
226421518a2SFrankie.Chang * (protected by @proc->inner_lock)
227421518a2SFrankie.Chang *
228421518a2SFrankie.Chang * Bookkeeping structure for binder nodes.
229421518a2SFrankie.Chang */
230421518a2SFrankie.Chang struct binder_node {
231421518a2SFrankie.Chang int debug_id;
232421518a2SFrankie.Chang spinlock_t lock;
233421518a2SFrankie.Chang struct binder_work work;
234421518a2SFrankie.Chang union {
235421518a2SFrankie.Chang struct rb_node rb_node;
236421518a2SFrankie.Chang struct hlist_node dead_node;
237421518a2SFrankie.Chang };
238421518a2SFrankie.Chang struct binder_proc *proc;
239421518a2SFrankie.Chang struct hlist_head refs;
240421518a2SFrankie.Chang int internal_strong_refs;
241421518a2SFrankie.Chang int local_weak_refs;
242421518a2SFrankie.Chang int local_strong_refs;
243421518a2SFrankie.Chang int tmp_refs;
244421518a2SFrankie.Chang binder_uintptr_t ptr;
245421518a2SFrankie.Chang binder_uintptr_t cookie;
246421518a2SFrankie.Chang struct {
247421518a2SFrankie.Chang /*
248421518a2SFrankie.Chang * bitfield elements protected by
249421518a2SFrankie.Chang * proc inner_lock
250421518a2SFrankie.Chang */
251421518a2SFrankie.Chang u8 has_strong_ref:1;
252421518a2SFrankie.Chang u8 pending_strong_ref:1;
253421518a2SFrankie.Chang u8 has_weak_ref:1;
254421518a2SFrankie.Chang u8 pending_weak_ref:1;
255421518a2SFrankie.Chang };
256421518a2SFrankie.Chang struct {
257421518a2SFrankie.Chang /*
258421518a2SFrankie.Chang * invariant after initialization
259421518a2SFrankie.Chang */
260421518a2SFrankie.Chang u8 accept_fds:1;
261421518a2SFrankie.Chang u8 txn_security_ctx:1;
262421518a2SFrankie.Chang u8 min_priority;
263421518a2SFrankie.Chang };
264421518a2SFrankie.Chang bool has_async_transaction;
265421518a2SFrankie.Chang struct list_head async_todo;
266421518a2SFrankie.Chang };
267421518a2SFrankie.Chang
268421518a2SFrankie.Chang struct binder_ref_death {
269421518a2SFrankie.Chang /**
270421518a2SFrankie.Chang * @work: worklist element for death notifications
271421518a2SFrankie.Chang * (protected by inner_lock of the proc that
272421518a2SFrankie.Chang * this ref belongs to)
273421518a2SFrankie.Chang */
274421518a2SFrankie.Chang struct binder_work work;
275421518a2SFrankie.Chang binder_uintptr_t cookie;
276421518a2SFrankie.Chang };
277421518a2SFrankie.Chang
278421518a2SFrankie.Chang /**
279421518a2SFrankie.Chang * struct binder_ref_data - binder_ref counts and id
280421518a2SFrankie.Chang * @debug_id: unique ID for the ref
281421518a2SFrankie.Chang * @desc: unique userspace handle for ref
282421518a2SFrankie.Chang * @strong: strong ref count (debugging only if not locked)
283421518a2SFrankie.Chang * @weak: weak ref count (debugging only if not locked)
284421518a2SFrankie.Chang *
285421518a2SFrankie.Chang * Structure to hold ref count and ref id information. Since
286421518a2SFrankie.Chang * the actual ref can only be accessed with a lock, this structure
287421518a2SFrankie.Chang * is used to return information about the ref to callers of
288421518a2SFrankie.Chang * ref inc/dec functions.
289421518a2SFrankie.Chang */
290421518a2SFrankie.Chang struct binder_ref_data {
291421518a2SFrankie.Chang int debug_id;
292421518a2SFrankie.Chang uint32_t desc;
293421518a2SFrankie.Chang int strong;
294421518a2SFrankie.Chang int weak;
295421518a2SFrankie.Chang };
296421518a2SFrankie.Chang
297421518a2SFrankie.Chang /**
298421518a2SFrankie.Chang * struct binder_ref - struct to track references on nodes
299421518a2SFrankie.Chang * @data: binder_ref_data containing id, handle, and current refcounts
300421518a2SFrankie.Chang * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
301421518a2SFrankie.Chang * @rb_node_node: node for lookup by @node in proc's rb_tree
302421518a2SFrankie.Chang * @node_entry: list entry for node->refs list in target node
303421518a2SFrankie.Chang * (protected by @node->lock)
304421518a2SFrankie.Chang * @proc: binder_proc containing ref
305421518a2SFrankie.Chang * @node: binder_node of target node. When cleaning up a
306421518a2SFrankie.Chang * ref for deletion in binder_cleanup_ref, a non-NULL
307421518a2SFrankie.Chang * @node indicates the node must be freed
308421518a2SFrankie.Chang * @death: pointer to death notification (ref_death) if requested
309421518a2SFrankie.Chang * (protected by @node->lock)
310421518a2SFrankie.Chang *
311421518a2SFrankie.Chang * Structure to track references from procA to target node (on procB). This
312421518a2SFrankie.Chang * structure is unsafe to access without holding @proc->outer_lock.
313421518a2SFrankie.Chang */
314421518a2SFrankie.Chang struct binder_ref {
315421518a2SFrankie.Chang /* Lookups needed: */
316421518a2SFrankie.Chang /* node + proc => ref (transaction) */
317421518a2SFrankie.Chang /* desc + proc => ref (transaction, inc/dec ref) */
318421518a2SFrankie.Chang /* node => refs + procs (proc exit) */
319421518a2SFrankie.Chang struct binder_ref_data data;
320421518a2SFrankie.Chang struct rb_node rb_node_desc;
321421518a2SFrankie.Chang struct rb_node rb_node_node;
322421518a2SFrankie.Chang struct hlist_node node_entry;
323421518a2SFrankie.Chang struct binder_proc *proc;
324421518a2SFrankie.Chang struct binder_node *node;
325421518a2SFrankie.Chang struct binder_ref_death *death;
326421518a2SFrankie.Chang };
327421518a2SFrankie.Chang
328421518a2SFrankie.Chang /**
329421518a2SFrankie.Chang * struct binder_proc - binder process bookkeeping
330421518a2SFrankie.Chang * @proc_node: element for binder_procs list
331421518a2SFrankie.Chang * @threads: rbtree of binder_threads in this proc
332421518a2SFrankie.Chang * (protected by @inner_lock)
333421518a2SFrankie.Chang * @nodes: rbtree of binder nodes associated with
334421518a2SFrankie.Chang * this proc ordered by node->ptr
335421518a2SFrankie.Chang * (protected by @inner_lock)
336421518a2SFrankie.Chang * @refs_by_desc: rbtree of refs ordered by ref->desc
337421518a2SFrankie.Chang * (protected by @outer_lock)
338421518a2SFrankie.Chang * @refs_by_node: rbtree of refs ordered by ref->node
339421518a2SFrankie.Chang * (protected by @outer_lock)
340421518a2SFrankie.Chang * @waiting_threads: threads currently waiting for proc work
341421518a2SFrankie.Chang * (protected by @inner_lock)
342421518a2SFrankie.Chang * @pid PID of group_leader of process
343421518a2SFrankie.Chang * (invariant after initialized)
344421518a2SFrankie.Chang * @tsk task_struct for group_leader of process
345421518a2SFrankie.Chang * (invariant after initialized)
34629bc22acSTodd Kjos * @cred struct cred associated with the `struct file`
34729bc22acSTodd Kjos * in binder_open()
34829bc22acSTodd Kjos * (invariant after initialized)
349421518a2SFrankie.Chang * @deferred_work_node: element for binder_deferred_list
350421518a2SFrankie.Chang * (protected by binder_deferred_lock)
351421518a2SFrankie.Chang * @deferred_work: bitmap of deferred work to perform
352421518a2SFrankie.Chang * (protected by binder_deferred_lock)
353432ff1e9SMarco Ballesio * @outstanding_txns: number of transactions to be transmitted before
354432ff1e9SMarco Ballesio * processes in freeze_wait are woken up
355432ff1e9SMarco Ballesio * (protected by @inner_lock)
356421518a2SFrankie.Chang * @is_dead: process is dead and awaiting free
357421518a2SFrankie.Chang * when outstanding transactions are cleaned up
358421518a2SFrankie.Chang * (protected by @inner_lock)
359432ff1e9SMarco Ballesio * @is_frozen: process is frozen and unable to service
360432ff1e9SMarco Ballesio * binder transactions
361432ff1e9SMarco Ballesio * (protected by @inner_lock)
362ae28c1beSMarco Ballesio * @sync_recv: process received sync transactions since last frozen
363b564171aSLi Li * bit 0: received sync transaction after being frozen
364b564171aSLi Li * bit 1: new pending sync transaction during freezing
365ae28c1beSMarco Ballesio * (protected by @inner_lock)
366ae28c1beSMarco Ballesio * @async_recv: process received async transactions since last frozen
367ae28c1beSMarco Ballesio * (protected by @inner_lock)
368432ff1e9SMarco Ballesio * @freeze_wait: waitqueue of processes waiting for all outstanding
369432ff1e9SMarco Ballesio * transactions to be processed
370432ff1e9SMarco Ballesio * (protected by @inner_lock)
371421518a2SFrankie.Chang * @todo: list of work for this process
372421518a2SFrankie.Chang * (protected by @inner_lock)
373421518a2SFrankie.Chang * @stats: per-process binder statistics
374421518a2SFrankie.Chang * (atomics, no lock needed)
375421518a2SFrankie.Chang * @delivered_death: list of delivered death notification
376421518a2SFrankie.Chang * (protected by @inner_lock)
377421518a2SFrankie.Chang * @max_threads: cap on number of binder threads
378421518a2SFrankie.Chang * (protected by @inner_lock)
379421518a2SFrankie.Chang * @requested_threads: number of binder threads requested but not
380421518a2SFrankie.Chang * yet started. In current implementation, can
381421518a2SFrankie.Chang * only be 0 or 1.
382421518a2SFrankie.Chang * (protected by @inner_lock)
383421518a2SFrankie.Chang * @requested_threads_started: number binder threads started
384421518a2SFrankie.Chang * (protected by @inner_lock)
385421518a2SFrankie.Chang * @tmp_ref: temporary reference to indicate proc is in use
386421518a2SFrankie.Chang * (protected by @inner_lock)
387421518a2SFrankie.Chang * @default_priority: default scheduler priority
388421518a2SFrankie.Chang * (invariant after initialized)
389421518a2SFrankie.Chang * @debugfs_entry: debugfs node
390421518a2SFrankie.Chang * @alloc: binder allocator bookkeeping
391421518a2SFrankie.Chang * @context: binder_context for this proc
392421518a2SFrankie.Chang * (invariant after initialized)
393421518a2SFrankie.Chang * @inner_lock: can nest under outer_lock and/or node lock
394421518a2SFrankie.Chang * @outer_lock: no nesting under innor or node lock
395421518a2SFrankie.Chang * Lock order: 1) outer, 2) node, 3) inner
396421518a2SFrankie.Chang * @binderfs_entry: process-specific binderfs log file
397a7dc1e6fSHang Lu * @oneway_spam_detection_enabled: process enabled oneway spam detection
398a7dc1e6fSHang Lu * or not
399421518a2SFrankie.Chang *
400421518a2SFrankie.Chang * Bookkeeping structure for binder processes
401421518a2SFrankie.Chang */
402421518a2SFrankie.Chang struct binder_proc {
403421518a2SFrankie.Chang struct hlist_node proc_node;
404421518a2SFrankie.Chang struct rb_root threads;
405421518a2SFrankie.Chang struct rb_root nodes;
406421518a2SFrankie.Chang struct rb_root refs_by_desc;
407421518a2SFrankie.Chang struct rb_root refs_by_node;
408421518a2SFrankie.Chang struct list_head waiting_threads;
409421518a2SFrankie.Chang int pid;
410421518a2SFrankie.Chang struct task_struct *tsk;
41129bc22acSTodd Kjos const struct cred *cred;
412421518a2SFrankie.Chang struct hlist_node deferred_work_node;
413421518a2SFrankie.Chang int deferred_work;
414432ff1e9SMarco Ballesio int outstanding_txns;
415421518a2SFrankie.Chang bool is_dead;
416432ff1e9SMarco Ballesio bool is_frozen;
417ae28c1beSMarco Ballesio bool sync_recv;
418ae28c1beSMarco Ballesio bool async_recv;
419432ff1e9SMarco Ballesio wait_queue_head_t freeze_wait;
420421518a2SFrankie.Chang
421421518a2SFrankie.Chang struct list_head todo;
422421518a2SFrankie.Chang struct binder_stats stats;
423421518a2SFrankie.Chang struct list_head delivered_death;
424*71df2cb2SCarlos Llamas u32 max_threads;
425421518a2SFrankie.Chang int requested_threads;
426421518a2SFrankie.Chang int requested_threads_started;
427421518a2SFrankie.Chang int tmp_ref;
428421518a2SFrankie.Chang long default_priority;
429421518a2SFrankie.Chang struct dentry *debugfs_entry;
430421518a2SFrankie.Chang struct binder_alloc alloc;
431421518a2SFrankie.Chang struct binder_context *context;
432421518a2SFrankie.Chang spinlock_t inner_lock;
433421518a2SFrankie.Chang spinlock_t outer_lock;
434421518a2SFrankie.Chang struct dentry *binderfs_entry;
435a7dc1e6fSHang Lu bool oneway_spam_detection_enabled;
436421518a2SFrankie.Chang };
437421518a2SFrankie.Chang
438421518a2SFrankie.Chang /**
439421518a2SFrankie.Chang * struct binder_thread - binder thread bookkeeping
440421518a2SFrankie.Chang * @proc: binder process for this thread
441421518a2SFrankie.Chang * (invariant after initialization)
442421518a2SFrankie.Chang * @rb_node: element for proc->threads rbtree
443421518a2SFrankie.Chang * (protected by @proc->inner_lock)
444421518a2SFrankie.Chang * @waiting_thread_node: element for @proc->waiting_threads list
445421518a2SFrankie.Chang * (protected by @proc->inner_lock)
446421518a2SFrankie.Chang * @pid: PID for this thread
447421518a2SFrankie.Chang * (invariant after initialization)
448421518a2SFrankie.Chang * @looper: bitmap of looping state
449421518a2SFrankie.Chang * (only accessed by this thread)
450421518a2SFrankie.Chang * @looper_needs_return: looping thread needs to exit driver
451421518a2SFrankie.Chang * (no lock needed)
452421518a2SFrankie.Chang * @transaction_stack: stack of in-progress transactions for this thread
453421518a2SFrankie.Chang * (protected by @proc->inner_lock)
454421518a2SFrankie.Chang * @todo: list of work to do for this thread
455421518a2SFrankie.Chang * (protected by @proc->inner_lock)
456421518a2SFrankie.Chang * @process_todo: whether work in @todo should be processed
457421518a2SFrankie.Chang * (protected by @proc->inner_lock)
458421518a2SFrankie.Chang * @return_error: transaction errors reported by this thread
459421518a2SFrankie.Chang * (only accessed by this thread)
460421518a2SFrankie.Chang * @reply_error: transaction errors reported by target thread
461421518a2SFrankie.Chang * (protected by @proc->inner_lock)
462bd32889eSCarlos Llamas * @ee: extended error information from this thread
463bd32889eSCarlos Llamas * (protected by @proc->inner_lock)
464421518a2SFrankie.Chang * @wait: wait queue for thread work
465421518a2SFrankie.Chang * @stats: per-thread statistics
466421518a2SFrankie.Chang * (atomics, no lock needed)
467421518a2SFrankie.Chang * @tmp_ref: temporary reference to indicate thread is in use
468421518a2SFrankie.Chang * (atomic since @proc->inner_lock cannot
469421518a2SFrankie.Chang * always be acquired)
470421518a2SFrankie.Chang * @is_dead: thread is dead and awaiting free
471421518a2SFrankie.Chang * when outstanding transactions are cleaned up
472421518a2SFrankie.Chang * (protected by @proc->inner_lock)
473421518a2SFrankie.Chang *
474421518a2SFrankie.Chang * Bookkeeping structure for binder threads.
475421518a2SFrankie.Chang */
476421518a2SFrankie.Chang struct binder_thread {
477421518a2SFrankie.Chang struct binder_proc *proc;
478421518a2SFrankie.Chang struct rb_node rb_node;
479421518a2SFrankie.Chang struct list_head waiting_thread_node;
480421518a2SFrankie.Chang int pid;
481421518a2SFrankie.Chang int looper; /* only modified by this thread */
482421518a2SFrankie.Chang bool looper_need_return; /* can be written by other thread */
483421518a2SFrankie.Chang struct binder_transaction *transaction_stack;
484421518a2SFrankie.Chang struct list_head todo;
485421518a2SFrankie.Chang bool process_todo;
486421518a2SFrankie.Chang struct binder_error return_error;
487421518a2SFrankie.Chang struct binder_error reply_error;
488bd32889eSCarlos Llamas struct binder_extended_error ee;
489421518a2SFrankie.Chang wait_queue_head_t wait;
490421518a2SFrankie.Chang struct binder_stats stats;
491421518a2SFrankie.Chang atomic_t tmp_ref;
492421518a2SFrankie.Chang bool is_dead;
493421518a2SFrankie.Chang };
494421518a2SFrankie.Chang
495421518a2SFrankie.Chang /**
496421518a2SFrankie.Chang * struct binder_txn_fd_fixup - transaction fd fixup list element
497421518a2SFrankie.Chang * @fixup_entry: list entry
498421518a2SFrankie.Chang * @file: struct file to be associated with new fd
499421518a2SFrankie.Chang * @offset: offset in buffer data to this fixup
500a8a570c6SCarlos Llamas * @target_fd: fd to use by the target to install @file
501421518a2SFrankie.Chang *
502421518a2SFrankie.Chang * List element for fd fixups in a transaction. Since file
503421518a2SFrankie.Chang * descriptors need to be allocated in the context of the
504421518a2SFrankie.Chang * target process, we pass each fd to be processed in this
505421518a2SFrankie.Chang * struct.
506421518a2SFrankie.Chang */
507421518a2SFrankie.Chang struct binder_txn_fd_fixup {
508421518a2SFrankie.Chang struct list_head fixup_entry;
509421518a2SFrankie.Chang struct file *file;
510421518a2SFrankie.Chang size_t offset;
511a8a570c6SCarlos Llamas int target_fd;
512421518a2SFrankie.Chang };
513421518a2SFrankie.Chang
514421518a2SFrankie.Chang struct binder_transaction {
515421518a2SFrankie.Chang int debug_id;
516421518a2SFrankie.Chang struct binder_work work;
517421518a2SFrankie.Chang struct binder_thread *from;
518c21c0f9aSChuang Zhang pid_t from_pid;
519c21c0f9aSChuang Zhang pid_t from_tid;
520421518a2SFrankie.Chang struct binder_transaction *from_parent;
521421518a2SFrankie.Chang struct binder_proc *to_proc;
522421518a2SFrankie.Chang struct binder_thread *to_thread;
523421518a2SFrankie.Chang struct binder_transaction *to_parent;
524421518a2SFrankie.Chang unsigned need_reply:1;
525421518a2SFrankie.Chang /* unsigned is_dead:1; */ /* not used at the moment */
526421518a2SFrankie.Chang
527421518a2SFrankie.Chang struct binder_buffer *buffer;
528421518a2SFrankie.Chang unsigned int code;
529421518a2SFrankie.Chang unsigned int flags;
530421518a2SFrankie.Chang long priority;
531421518a2SFrankie.Chang long saved_priority;
532421518a2SFrankie.Chang kuid_t sender_euid;
53380093619SChuang Zhang ktime_t start_time;
534421518a2SFrankie.Chang struct list_head fd_fixups;
535421518a2SFrankie.Chang binder_uintptr_t security_ctx;
536421518a2SFrankie.Chang /**
537421518a2SFrankie.Chang * @lock: protects @from, @to_proc, and @to_thread
538421518a2SFrankie.Chang *
539421518a2SFrankie.Chang * @from, @to_proc, and @to_thread can be set to NULL
540421518a2SFrankie.Chang * during thread teardown
541421518a2SFrankie.Chang */
542421518a2SFrankie.Chang spinlock_t lock;
543421518a2SFrankie.Chang };
544421518a2SFrankie.Chang
545421518a2SFrankie.Chang /**
546421518a2SFrankie.Chang * struct binder_object - union of flat binder object types
547421518a2SFrankie.Chang * @hdr: generic object header
548421518a2SFrankie.Chang * @fbo: binder object (nodes and refs)
549421518a2SFrankie.Chang * @fdo: file descriptor object
550421518a2SFrankie.Chang * @bbo: binder buffer pointer
551421518a2SFrankie.Chang * @fdao: file descriptor array
552421518a2SFrankie.Chang *
553421518a2SFrankie.Chang * Used for type-independent object copies
554421518a2SFrankie.Chang */
555421518a2SFrankie.Chang struct binder_object {
556421518a2SFrankie.Chang union {
557421518a2SFrankie.Chang struct binder_object_header hdr;
558421518a2SFrankie.Chang struct flat_binder_object fbo;
559421518a2SFrankie.Chang struct binder_fd_object fdo;
560421518a2SFrankie.Chang struct binder_buffer_object bbo;
561421518a2SFrankie.Chang struct binder_fd_array_object fdao;
562421518a2SFrankie.Chang };
563421518a2SFrankie.Chang };
564421518a2SFrankie.Chang
5653ad20fe3SChristian Brauner #endif /* _LINUX_BINDER_INTERNAL_H */
566