binder_internal.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_BINDER_INTERNAL_H
  3. #define _LINUX_BINDER_INTERNAL_H
  4. #include <linux/export.h>
  5. #include <linux/fs.h>
  6. #include <linux/list.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/mutex.h>
  9. #include <linux/refcount.h>
  10. #include <linux/stddef.h>
  11. #include <linux/types.h>
  12. #include <linux/uidgid.h>
  13. #include <uapi/linux/android/binderfs.h>
  14. #include "binder_alloc.h"
  15. struct binder_context {
  16. struct binder_node *binder_context_mgr_node;
  17. struct mutex context_mgr_node_lock;
  18. kuid_t binder_context_mgr_uid;
  19. const char *name;
  20. };
  21. /**
  22. * struct binder_device - information about a binder device node
  23. * @hlist: list of binder devices (only used for devices requested via
  24. * CONFIG_ANDROID_BINDER_DEVICES)
  25. * @miscdev: information about a binder character device node
  26. * @context: binder context information
  27. * @binderfs_inode: This is the inode of the root dentry of the super block
  28. * belonging to a binderfs mount.
  29. */
  30. struct binder_device {
  31. struct hlist_node hlist;
  32. struct miscdevice miscdev;
  33. struct binder_context context;
  34. struct inode *binderfs_inode;
  35. refcount_t ref;
  36. };
  37. /**
  38. * binderfs_mount_opts - mount options for binderfs
  39. * @max: maximum number of allocatable binderfs binder devices
  40. * @stats_mode: enable binder stats in binderfs.
  41. */
  42. struct binderfs_mount_opts {
  43. int max;
  44. int stats_mode;
  45. };
  46. /**
  47. * binderfs_info - information about a binderfs mount
  48. * @ipc_ns: The ipc namespace the binderfs mount belongs to.
  49. * @control_dentry: This records the dentry of this binderfs mount
  50. * binder-control device.
  51. * @root_uid: uid that needs to be used when a new binder device is
  52. * created.
  53. * @root_gid: gid that needs to be used when a new binder device is
  54. * created.
  55. * @mount_opts: The mount options in use.
  56. * @device_count: The current number of allocated binder devices.
  57. * @proc_log_dir: Pointer to the directory dentry containing process-specific
  58. * logs.
  59. */
  60. struct binderfs_info {
  61. struct ipc_namespace *ipc_ns;
  62. struct dentry *control_dentry;
  63. kuid_t root_uid;
  64. kgid_t root_gid;
  65. struct binderfs_mount_opts mount_opts;
  66. int device_count;
  67. struct dentry *proc_log_dir;
  68. };
  69. extern const struct file_operations binder_fops;
  70. extern char *binder_devices_param;
  71. #ifdef CONFIG_ANDROID_BINDERFS
  72. extern bool is_binderfs_device(const struct inode *inode);
  73. extern struct dentry *binderfs_create_file(struct dentry *dir, const char *name,
  74. const struct file_operations *fops,
  75. void *data);
  76. extern void binderfs_remove_file(struct dentry *dentry);
  77. #else
  78. static inline bool is_binderfs_device(const struct inode *inode)
  79. {
  80. return false;
  81. }
  82. static inline struct dentry *binderfs_create_file(struct dentry *dir,
  83. const char *name,
  84. const struct file_operations *fops,
  85. void *data)
  86. {
  87. return NULL;
  88. }
  89. static inline void binderfs_remove_file(struct dentry *dentry) {}
  90. #endif
  91. #ifdef CONFIG_ANDROID_BINDERFS
  92. extern int __init init_binderfs(void);
  93. #else
  94. static inline int __init init_binderfs(void)
  95. {
  96. return 0;
  97. }
  98. #endif
  99. struct binder_debugfs_entry {
  100. const char *name;
  101. umode_t mode;
  102. const struct file_operations *fops;
  103. void *data;
  104. };
  105. extern const struct binder_debugfs_entry binder_debugfs_entries[];
  106. #define binder_for_each_debugfs_entry(entry) \
  107. for ((entry) = binder_debugfs_entries; \
  108. (entry)->name; \
  109. (entry)++)
  110. enum binder_stat_types {
  111. BINDER_STAT_PROC,
  112. BINDER_STAT_THREAD,
  113. BINDER_STAT_NODE,
  114. BINDER_STAT_REF,
  115. BINDER_STAT_DEATH,
  116. BINDER_STAT_TRANSACTION,
  117. BINDER_STAT_TRANSACTION_COMPLETE,
  118. BINDER_STAT_COUNT
  119. };
  120. struct binder_stats {
  121. atomic_t br[_IOC_NR(BR_ONEWAY_SPAM_SUSPECT) + 1];
  122. atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
  123. atomic_t obj_created[BINDER_STAT_COUNT];
  124. atomic_t obj_deleted[BINDER_STAT_COUNT];
  125. };
  126. /**
  127. * struct binder_work - work enqueued on a worklist
  128. * @entry: node enqueued on list
  129. * @type: type of work to be performed
  130. *
  131. * There are separate work lists for proc, thread, and node (async).
  132. */
  133. struct binder_work {
  134. struct list_head entry;
  135. enum binder_work_type {
  136. BINDER_WORK_TRANSACTION = 1,
  137. BINDER_WORK_TRANSACTION_COMPLETE,
  138. BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT,
  139. BINDER_WORK_RETURN_ERROR,
  140. BINDER_WORK_NODE,
  141. BINDER_WORK_DEAD_BINDER,
  142. BINDER_WORK_DEAD_BINDER_AND_CLEAR,
  143. BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
  144. } type;
  145. };
  146. struct binder_error {
  147. struct binder_work work;
  148. uint32_t cmd;
  149. };
  150. /**
  151. * struct binder_node - binder node bookkeeping
  152. * @debug_id: unique ID for debugging
  153. * (invariant after initialized)
  154. * @lock: lock for node fields
  155. * @work: worklist element for node work
  156. * (protected by @proc->inner_lock)
  157. * @rb_node: element for proc->nodes tree
  158. * (protected by @proc->inner_lock)
  159. * @dead_node: element for binder_dead_nodes list
  160. * (protected by binder_dead_nodes_lock)
  161. * @proc: binder_proc that owns this node
  162. * (invariant after initialized)
  163. * @refs: list of references on this node
  164. * (protected by @lock)
  165. * @internal_strong_refs: used to take strong references when
  166. * initiating a transaction
  167. * (protected by @proc->inner_lock if @proc
  168. * and by @lock)
  169. * @local_weak_refs: weak user refs from local process
  170. * (protected by @proc->inner_lock if @proc
  171. * and by @lock)
  172. * @local_strong_refs: strong user refs from local process
  173. * (protected by @proc->inner_lock if @proc
  174. * and by @lock)
  175. * @tmp_refs: temporary kernel refs
  176. * (protected by @proc->inner_lock while @proc
  177. * is valid, and by binder_dead_nodes_lock
  178. * if @proc is NULL. During inc/dec and node release
  179. * it is also protected by @lock to provide safety
  180. * as the node dies and @proc becomes NULL)
  181. * @ptr: userspace pointer for node
  182. * (invariant, no lock needed)
  183. * @cookie: userspace cookie for node
  184. * (invariant, no lock needed)
  185. * @has_strong_ref: userspace notified of strong ref
  186. * (protected by @proc->inner_lock if @proc
  187. * and by @lock)
  188. * @pending_strong_ref: userspace has acked notification of strong ref
  189. * (protected by @proc->inner_lock if @proc
  190. * and by @lock)
  191. * @has_weak_ref: userspace notified of weak ref
  192. * (protected by @proc->inner_lock if @proc
  193. * and by @lock)
  194. * @pending_weak_ref: userspace has acked notification of weak ref
  195. * (protected by @proc->inner_lock if @proc
  196. * and by @lock)
  197. * @has_async_transaction: async transaction to node in progress
  198. * (protected by @lock)
  199. * @sched_policy: minimum scheduling policy for node
  200. * (invariant after initialized)
  201. * @accept_fds: file descriptor operations supported for node
  202. * (invariant after initialized)
  203. * @min_priority: minimum scheduling priority
  204. * (invariant after initialized)
  205. * @inherit_rt: inherit RT scheduling policy from caller
  206. * @txn_security_ctx: require sender's security context
  207. * (invariant after initialized)
  208. * @async_todo: list of async work items
  209. * (protected by @proc->inner_lock)
  210. *
  211. * Bookkeeping structure for binder nodes.
  212. */
  213. struct binder_node {
  214. int debug_id;
  215. spinlock_t lock;
  216. struct binder_work work;
  217. union {
  218. struct rb_node rb_node;
  219. struct hlist_node dead_node;
  220. };
  221. struct binder_proc *proc;
  222. struct hlist_head refs;
  223. int internal_strong_refs;
  224. int local_weak_refs;
  225. int local_strong_refs;
  226. int tmp_refs;
  227. binder_uintptr_t ptr;
  228. binder_uintptr_t cookie;
  229. struct {
  230. /*
  231. * bitfield elements protected by
  232. * proc inner_lock
  233. */
  234. u8 has_strong_ref:1;
  235. u8 pending_strong_ref:1;
  236. u8 has_weak_ref:1;
  237. u8 pending_weak_ref:1;
  238. };
  239. struct {
  240. /*
  241. * invariant after initialization
  242. */
  243. u8 sched_policy:2;
  244. u8 inherit_rt:1;
  245. u8 accept_fds:1;
  246. u8 txn_security_ctx:1;
  247. u8 min_priority;
  248. };
  249. bool has_async_transaction;
  250. struct list_head async_todo;
  251. };
  252. struct binder_ref_death {
  253. /**
  254. * @work: worklist element for death notifications
  255. * (protected by inner_lock of the proc that
  256. * this ref belongs to)
  257. */
  258. struct binder_work work;
  259. binder_uintptr_t cookie;
  260. };
  261. /**
  262. * struct binder_ref_data - binder_ref counts and id
  263. * @debug_id: unique ID for the ref
  264. * @desc: unique userspace handle for ref
  265. * @strong: strong ref count (debugging only if not locked)
  266. * @weak: weak ref count (debugging only if not locked)
  267. *
  268. * Structure to hold ref count and ref id information. Since
  269. * the actual ref can only be accessed with a lock, this structure
  270. * is used to return information about the ref to callers of
  271. * ref inc/dec functions.
  272. */
  273. struct binder_ref_data {
  274. int debug_id;
  275. uint32_t desc;
  276. int strong;
  277. int weak;
  278. };
  279. /**
  280. * struct binder_ref - struct to track references on nodes
  281. * @data: binder_ref_data containing id, handle, and current refcounts
  282. * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
  283. * @rb_node_node: node for lookup by @node in proc's rb_tree
  284. * @node_entry: list entry for node->refs list in target node
  285. * (protected by @node->lock)
  286. * @proc: binder_proc containing ref
  287. * @node: binder_node of target node. When cleaning up a
  288. * ref for deletion in binder_cleanup_ref, a non-NULL
  289. * @node indicates the node must be freed
  290. * @death: pointer to death notification (ref_death) if requested
  291. * (protected by @node->lock)
  292. *
  293. * Structure to track references from procA to target node (on procB). This
  294. * structure is unsafe to access without holding @proc->outer_lock.
  295. */
  296. struct binder_ref {
  297. /* Lookups needed: */
  298. /* node + proc => ref (transaction) */
  299. /* desc + proc => ref (transaction, inc/dec ref) */
  300. /* node => refs + procs (proc exit) */
  301. struct binder_ref_data data;
  302. struct rb_node rb_node_desc;
  303. struct rb_node rb_node_node;
  304. struct hlist_node node_entry;
  305. struct binder_proc *proc;
  306. struct binder_node *node;
  307. struct binder_ref_death *death;
  308. };
  309. /**
  310. * struct binder_priority - scheduler policy and priority
  311. * @sched_policy scheduler policy
  312. * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
  313. *
  314. * The binder driver supports inheriting the following scheduler policies:
  315. * SCHED_NORMAL
  316. * SCHED_BATCH
  317. * SCHED_FIFO
  318. * SCHED_RR
  319. */
  320. struct binder_priority {
  321. unsigned int sched_policy;
  322. int prio;
  323. };
  324. enum binder_prio_state {
  325. BINDER_PRIO_SET, /* desired priority set */
  326. BINDER_PRIO_PENDING, /* initiated a saved priority restore */
  327. BINDER_PRIO_ABORT, /* abort the pending priority restore */
  328. };
  329. /**
  330. * struct binder_proc - binder process bookkeeping
  331. * @proc_node: element for binder_procs list
  332. * @threads: rbtree of binder_threads in this proc
  333. * (protected by @inner_lock)
  334. * @nodes: rbtree of binder nodes associated with
  335. * this proc ordered by node->ptr
  336. * (protected by @inner_lock)
  337. * @refs_by_desc: rbtree of refs ordered by ref->desc
  338. * (protected by @outer_lock)
  339. * @refs_by_node: rbtree of refs ordered by ref->node
  340. * (protected by @outer_lock)
  341. * @waiting_threads: threads currently waiting for proc work
  342. * (protected by @inner_lock)
  343. * @pid PID of group_leader of process
  344. * (invariant after initialized)
  345. * @tsk task_struct for group_leader of process
  346. * (invariant after initialized)
  347. * @cred struct cred associated with the `struct file`
  348. * in binder_open()
  349. * (invariant after initialized)
  350. * @deferred_work_node: element for binder_deferred_list
  351. * (protected by binder_deferred_lock)
  352. * @deferred_work: bitmap of deferred work to perform
  353. * (protected by binder_deferred_lock)
  354. * @outstanding_txns: number of transactions to be transmitted before
  355. * processes in freeze_wait are woken up
  356. * (protected by @inner_lock)
  357. * @is_dead: process is dead and awaiting free
  358. * when outstanding transactions are cleaned up
  359. * (protected by @inner_lock)
  360. * @is_frozen: process is frozen and unable to service
  361. * binder transactions
  362. * (protected by @inner_lock)
  363. * @sync_recv: process received sync transactions since last frozen
  364. * bit 0: received sync transaction after being frozen
  365. * bit 1: new pending sync transaction during freezing
  366. * (protected by @inner_lock)
  367. * @async_recv: process received async transactions since last frozen
  368. * (protected by @inner_lock)
  369. * @freeze_wait: waitqueue of processes waiting for all outstanding
  370. * transactions to be processed
  371. * (protected by @inner_lock)
  372. * @todo: list of work for this process
  373. * (protected by @inner_lock)
  374. * @stats: per-process binder statistics
  375. * (atomics, no lock needed)
  376. * @delivered_death: list of delivered death notification
  377. * (protected by @inner_lock)
  378. * @max_threads: cap on number of binder threads
  379. * (protected by @inner_lock)
  380. * @requested_threads: number of binder threads requested but not
  381. * yet started. In current implementation, can
  382. * only be 0 or 1.
  383. * (protected by @inner_lock)
  384. * @requested_threads_started: number binder threads started
  385. * (protected by @inner_lock)
  386. * @tmp_ref: temporary reference to indicate proc is in use
  387. * (protected by @inner_lock)
  388. * @default_priority: default scheduler priority
  389. * (invariant after initialized)
  390. * @debugfs_entry: debugfs node
  391. * @alloc: binder allocator bookkeeping
  392. * @context: binder_context for this proc
  393. * (invariant after initialized)
  394. * @inner_lock: can nest under outer_lock and/or node lock
  395. * @outer_lock: no nesting under innor or node lock
  396. * Lock order: 1) outer, 2) node, 3) inner
  397. * @binderfs_entry: process-specific binderfs log file
  398. * @oneway_spam_detection_enabled: process enabled oneway spam detection
  399. * or not
  400. *
  401. * Bookkeeping structure for binder processes
  402. */
  403. struct binder_proc {
  404. struct hlist_node proc_node;
  405. struct rb_root threads;
  406. struct rb_root nodes;
  407. struct rb_root refs_by_desc;
  408. struct rb_root refs_by_node;
  409. struct list_head waiting_threads;
  410. int pid;
  411. struct task_struct *tsk;
  412. const struct cred *cred;
  413. struct hlist_node deferred_work_node;
  414. int deferred_work;
  415. int outstanding_txns;
  416. bool is_dead;
  417. bool is_frozen;
  418. bool sync_recv;
  419. bool async_recv;
  420. wait_queue_head_t freeze_wait;
  421. struct list_head todo;
  422. struct binder_stats stats;
  423. struct list_head delivered_death;
  424. int max_threads;
  425. int requested_threads;
  426. int requested_threads_started;
  427. int tmp_ref;
  428. struct binder_priority default_priority;
  429. struct dentry *debugfs_entry;
  430. struct binder_alloc alloc;
  431. struct binder_context *context;
  432. spinlock_t inner_lock;
  433. spinlock_t outer_lock;
  434. struct dentry *binderfs_entry;
  435. bool oneway_spam_detection_enabled;
  436. };
  437. /**
  438. * struct binder_thread - binder thread bookkeeping
  439. * @proc: binder process for this thread
  440. * (invariant after initialization)
  441. * @rb_node: element for proc->threads rbtree
  442. * (protected by @proc->inner_lock)
  443. * @waiting_thread_node: element for @proc->waiting_threads list
  444. * (protected by @proc->inner_lock)
  445. * @pid: PID for this thread
  446. * (invariant after initialization)
  447. * @looper: bitmap of looping state
  448. * (only accessed by this thread)
  449. * @looper_needs_return: looping thread needs to exit driver
  450. * (no lock needed)
  451. * @transaction_stack: stack of in-progress transactions for this thread
  452. * (protected by @proc->inner_lock)
  453. * @todo: list of work to do for this thread
  454. * (protected by @proc->inner_lock)
  455. * @process_todo: whether work in @todo should be processed
  456. * (protected by @proc->inner_lock)
  457. * @return_error: transaction errors reported by this thread
  458. * (only accessed by this thread)
  459. * @reply_error: transaction errors reported by target thread
  460. * (protected by @proc->inner_lock)
  461. * @ee: extended error information from this thread
  462. * (protected by @proc->inner_lock)
  463. * @wait: wait queue for thread work
  464. * @stats: per-thread statistics
  465. * (atomics, no lock needed)
  466. * @tmp_ref: temporary reference to indicate thread is in use
  467. * (atomic since @proc->inner_lock cannot
  468. * always be acquired)
  469. * @is_dead: thread is dead and awaiting free
  470. * when outstanding transactions are cleaned up
  471. * (protected by @proc->inner_lock)
  472. * @task: struct task_struct for this thread
  473. * @prio_lock: protects thread priority fields
  474. * @prio_next: saved priority to be restored next
  475. * (protected by @prio_lock)
  476. * @prio_state: state of the priority restore process as
  477. * defined by enum binder_prio_state
  478. * (protected by @prio_lock)
  479. *
  480. * Bookkeeping structure for binder threads.
  481. */
  482. struct binder_thread {
  483. struct binder_proc *proc;
  484. struct rb_node rb_node;
  485. struct list_head waiting_thread_node;
  486. int pid;
  487. int looper; /* only modified by this thread */
  488. bool looper_need_return; /* can be written by other thread */
  489. struct binder_transaction *transaction_stack;
  490. struct list_head todo;
  491. bool process_todo;
  492. struct binder_error return_error;
  493. struct binder_error reply_error;
  494. struct binder_extended_error ee;
  495. wait_queue_head_t wait;
  496. struct binder_stats stats;
  497. atomic_t tmp_ref;
  498. bool is_dead;
  499. struct task_struct *task;
  500. spinlock_t prio_lock;
  501. struct binder_priority prio_next;
  502. enum binder_prio_state prio_state;
  503. };
  504. /**
  505. * struct binder_txn_fd_fixup - transaction fd fixup list element
  506. * @fixup_entry: list entry
  507. * @file: struct file to be associated with new fd
  508. * @offset: offset in buffer data to this fixup
  509. * @target_fd: fd to use by the target to install @file
  510. *
  511. * List element for fd fixups in a transaction. Since file
  512. * descriptors need to be allocated in the context of the
  513. * target process, we pass each fd to be processed in this
  514. * struct.
  515. */
  516. struct binder_txn_fd_fixup {
  517. struct list_head fixup_entry;
  518. struct file *file;
  519. size_t offset;
  520. int target_fd;
  521. };
  522. struct binder_transaction {
  523. int debug_id;
  524. struct binder_work work;
  525. struct binder_thread *from;
  526. pid_t from_pid;
  527. pid_t from_tid;
  528. struct binder_transaction *from_parent;
  529. struct binder_proc *to_proc;
  530. struct binder_thread *to_thread;
  531. struct binder_transaction *to_parent;
  532. unsigned need_reply:1;
  533. /* unsigned is_dead:1; */ /* not used at the moment */
  534. struct binder_buffer *buffer;
  535. unsigned int code;
  536. unsigned int flags;
  537. struct binder_priority priority;
  538. struct binder_priority saved_priority;
  539. bool set_priority_called;
  540. bool is_nested;
  541. kuid_t sender_euid;
  542. ktime_t start_time;
  543. struct list_head fd_fixups;
  544. binder_uintptr_t security_ctx;
  545. /**
  546. * @lock: protects @from, @to_proc, and @to_thread
  547. *
  548. * @from, @to_proc, and @to_thread can be set to NULL
  549. * during thread teardown
  550. */
  551. spinlock_t lock;
  552. ANDROID_VENDOR_DATA(1);
  553. };
  554. /**
  555. * struct binder_object - union of flat binder object types
  556. * @hdr: generic object header
  557. * @fbo: binder object (nodes and refs)
  558. * @fdo: file descriptor object
  559. * @bbo: binder buffer pointer
  560. * @fdao: file descriptor array
  561. *
  562. * Used for type-independent object copies
  563. */
  564. struct binder_object {
  565. union {
  566. struct binder_object_header hdr;
  567. struct flat_binder_object fbo;
  568. struct binder_fd_object fdo;
  569. struct binder_buffer_object bbo;
  570. struct binder_fd_array_object fdao;
  571. };
  572. };
  573. #endif /* _LINUX_BINDER_INTERNAL_H */