binder_internal.h 21 KB

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