binder_internal.h 20 KB

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