binder_alloc.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2017 Google, Inc.
  4. */
  5. #ifndef _LINUX_BINDER_ALLOC_H
  6. #define _LINUX_BINDER_ALLOC_H
  7. #include <linux/rbtree.h>
  8. #include <linux/list.h>
  9. #include <linux/mm.h>
  10. #include <linux/rtmutex.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/slab.h>
  13. #include <linux/list_lru.h>
  14. #include <uapi/linux/android/binder.h>
  15. extern struct list_lru binder_alloc_lru;
  16. struct binder_transaction;
  17. /**
  18. * struct binder_buffer - buffer used for binder transactions
  19. * @entry: entry alloc->buffers
  20. * @rb_node: node for allocated_buffers/free_buffers rb trees
  21. * @free: %true if buffer is free
  22. * @clear_on_free: %true if buffer must be zeroed after use
  23. * @allow_user_free: %true if user is allowed to free buffer
  24. * @async_transaction: %true if buffer is in use for an async txn
  25. * @debug_id: unique ID for debugging
  26. * @transaction: pointer to associated struct binder_transaction
  27. * @target_node: struct binder_node associated with this buffer
  28. * @data_size: size of @transaction data
  29. * @offsets_size: size of array of offsets
  30. * @extra_buffers_size: size of space for other objects (like sg lists)
  31. * @user_data: user pointer to base of buffer space
  32. *
  33. * Bookkeeping structure for binder transaction buffers
  34. */
  35. struct binder_buffer {
  36. struct list_head entry; /* free and allocated entries by address */
  37. struct rb_node rb_node; /* free entry by size or allocated entry */
  38. /* by address */
  39. unsigned free:1;
  40. unsigned clear_on_free:1;
  41. unsigned allow_user_free:1;
  42. unsigned async_transaction:1;
  43. unsigned debug_id:28;
  44. struct binder_transaction *transaction;
  45. struct binder_node *target_node;
  46. size_t data_size;
  47. size_t offsets_size;
  48. size_t extra_buffers_size;
  49. void __user *user_data;
  50. };
  51. /**
  52. * struct binder_lru_page - page object used for binder shrinker
  53. * @page_ptr: pointer to physical page in mmap'd space
  54. * @lru: entry in binder_alloc_lru
  55. * @alloc: binder_alloc for a proc
  56. */
  57. struct binder_lru_page {
  58. struct list_head lru;
  59. struct page *page_ptr;
  60. struct binder_alloc *alloc;
  61. };
  62. /**
  63. * struct binder_alloc - per-binder proc state for binder allocator
  64. * @vma: vm_area_struct passed to mmap_handler
  65. * (invarient after mmap)
  66. * @tsk: tid for task that called init for this proc
  67. * (invariant after init)
  68. * @vma_vm_mm: copy of vma->vm_mm (invarient after mmap)
  69. * @buffer: base of per-proc address space mapped via mmap
  70. * @buffers: list of all buffers for this proc
  71. * @free_buffers: rb tree of buffers available for allocation
  72. * sorted by size
  73. * @allocated_buffers: rb tree of allocated buffers sorted by address
  74. * @free_async_space: VA space available for async buffers. This is
  75. * initialized at mmap time to 1/2 the full VA space
  76. * @pages: array of binder_lru_page
  77. * @buffer_size: size of address space specified via mmap
  78. * @pid: pid for associated binder_proc (invariant after init)
  79. * @pages_high: high watermark of offset in @pages
  80. *
  81. * Bookkeeping structure for per-proc address space management for binder
  82. * buffers. It is normally initialized during binder_init() and binder_mmap()
  83. * calls. The address space is used for both user-visible buffers and for
  84. * struct binder_buffer objects used to track the user buffers
  85. */
  86. struct binder_alloc {
  87. struct mutex mutex;
  88. struct vm_area_struct *vma;
  89. struct mm_struct *vma_vm_mm;
  90. void __user *buffer;
  91. struct list_head buffers;
  92. struct rb_root free_buffers;
  93. struct rb_root allocated_buffers;
  94. size_t free_async_space;
  95. struct binder_lru_page *pages;
  96. size_t buffer_size;
  97. uint32_t buffer_free;
  98. int pid;
  99. size_t pages_high;
  100. };
  101. #ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST
  102. void binder_selftest_alloc(struct binder_alloc *alloc);
  103. #else
  104. static inline void binder_selftest_alloc(struct binder_alloc *alloc) {}
  105. #endif
  106. enum lru_status binder_alloc_free_page(struct list_head *item,
  107. struct list_lru_one *lru,
  108. spinlock_t *lock, void *cb_arg);
  109. extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
  110. size_t data_size,
  111. size_t offsets_size,
  112. size_t extra_buffers_size,
  113. int is_async);
  114. extern void binder_alloc_init(struct binder_alloc *alloc);
  115. extern int binder_alloc_shrinker_init(void);
  116. extern void binder_alloc_vma_close(struct binder_alloc *alloc);
  117. extern struct binder_buffer *
  118. binder_alloc_prepare_to_free(struct binder_alloc *alloc,
  119. uintptr_t user_ptr);
  120. extern void binder_alloc_free_buf(struct binder_alloc *alloc,
  121. struct binder_buffer *buffer);
  122. extern int binder_alloc_mmap_handler(struct binder_alloc *alloc,
  123. struct vm_area_struct *vma);
  124. extern void binder_alloc_deferred_release(struct binder_alloc *alloc);
  125. extern int binder_alloc_get_allocated_count(struct binder_alloc *alloc);
  126. extern void binder_alloc_print_allocated(struct seq_file *m,
  127. struct binder_alloc *alloc);
  128. void binder_alloc_print_pages(struct seq_file *m,
  129. struct binder_alloc *alloc);
  130. /**
  131. * binder_alloc_get_free_async_space() - get free space available for async
  132. * @alloc: binder_alloc for this proc
  133. *
  134. * Return: the bytes remaining in the address-space for async transactions
  135. */
  136. static inline size_t
  137. binder_alloc_get_free_async_space(struct binder_alloc *alloc)
  138. {
  139. size_t free_async_space;
  140. mutex_lock(&alloc->mutex);
  141. free_async_space = alloc->free_async_space;
  142. mutex_unlock(&alloc->mutex);
  143. return free_async_space;
  144. }
  145. unsigned long
  146. binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
  147. struct binder_buffer *buffer,
  148. binder_size_t buffer_offset,
  149. const void __user *from,
  150. size_t bytes);
  151. int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
  152. struct binder_buffer *buffer,
  153. binder_size_t buffer_offset,
  154. void *src,
  155. size_t bytes);
  156. int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
  157. void *dest,
  158. struct binder_buffer *buffer,
  159. binder_size_t buffer_offset,
  160. size_t bytes);
  161. #endif /* _LINUX_BINDER_ALLOC_H */