1 /*
2  * Copyright (C) 2017 Google, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 
15 #ifndef _LINUX_BINDER_ALLOC_H
16 #define _LINUX_BINDER_ALLOC_H
17 
18 #include <linux/rbtree.h>
19 #include <linux/list.h>
20 #include <linux/mm.h>
21 #include <linux/rtmutex.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
24 #include <linux/list_lru.h>
25 
26 extern struct list_lru binder_alloc_lru;
27 struct binder_transaction;
28 
29 /**
30  * struct binder_buffer - buffer used for binder transactions
31  * @entry:              entry alloc->buffers
32  * @rb_node:            node for allocated_buffers/free_buffers rb trees
33  * @free:               %true if buffer is free
34  * @allow_user_free:    %true if user is allowed to free buffer
35  * @async_transaction:  %true if buffer is in use for an async txn
36  * @debug_id:           unique ID for debugging
37  * @transaction:        pointer to associated struct binder_transaction
38  * @target_node:        struct binder_node associated with this buffer
39  * @data_size:          size of @transaction data
40  * @offsets_size:       size of array of offsets
41  * @extra_buffers_size: size of space for other objects (like sg lists)
42  * @data:               pointer to base of buffer space
43  *
44  * Bookkeeping structure for binder transaction buffers
45  */
46 struct binder_buffer {
47 	struct list_head entry; /* free and allocated entries by address */
48 	struct rb_node rb_node; /* free entry by size or allocated entry */
49 				/* by address */
50 	unsigned free:1;
51 	unsigned allow_user_free:1;
52 	unsigned async_transaction:1;
53 	unsigned debug_id:29;
54 
55 	struct binder_transaction *transaction;
56 
57 	struct binder_node *target_node;
58 	size_t data_size;
59 	size_t offsets_size;
60 	size_t extra_buffers_size;
61 	void *data;
62 };
63 
64 /**
65  * struct binder_lru_page - page object used for binder shrinker
66  * @page_ptr: pointer to physical page in mmap'd space
67  * @lru:      entry in binder_alloc_lru
68  * @alloc:    binder_alloc for a proc
69  */
70 struct binder_lru_page {
71 	struct list_head lru;
72 	struct page *page_ptr;
73 	struct binder_alloc *alloc;
74 };
75 
76 /**
77  * struct binder_alloc - per-binder proc state for binder allocator
78  * @vma:                vm_area_struct passed to mmap_handler
79  *                      (invarient after mmap)
80  * @tsk:                tid for task that called init for this proc
81  *                      (invariant after init)
82  * @vma_vm_mm:          copy of vma->vm_mm (invarient after mmap)
83  * @buffer:             base of per-proc address space mapped via mmap
84  * @user_buffer_offset: offset between user and kernel VAs for buffer
85  * @buffers:            list of all buffers for this proc
86  * @free_buffers:       rb tree of buffers available for allocation
87  *                      sorted by size
88  * @allocated_buffers:  rb tree of allocated buffers sorted by address
89  * @free_async_space:   VA space available for async buffers. This is
90  *                      initialized at mmap time to 1/2 the full VA space
91  * @pages:              array of binder_lru_page
92  * @buffer_size:        size of address space specified via mmap
93  * @pid:                pid for associated binder_proc (invariant after init)
94  * @pages_high:         high watermark of offset in @pages
95  *
96  * Bookkeeping structure for per-proc address space management for binder
97  * buffers. It is normally initialized during binder_init() and binder_mmap()
98  * calls. The address space is used for both user-visible buffers and for
99  * struct binder_buffer objects used to track the user buffers
100  */
101 struct binder_alloc {
102 	struct mutex mutex;
103 	struct vm_area_struct *vma;
104 	struct mm_struct *vma_vm_mm;
105 	void *buffer;
106 	ptrdiff_t user_buffer_offset;
107 	struct list_head buffers;
108 	struct rb_root free_buffers;
109 	struct rb_root allocated_buffers;
110 	size_t free_async_space;
111 	struct binder_lru_page *pages;
112 	size_t buffer_size;
113 	uint32_t buffer_free;
114 	int pid;
115 	size_t pages_high;
116 };
117 
118 #ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST
119 void binder_selftest_alloc(struct binder_alloc *alloc);
120 #else
121 static inline void binder_selftest_alloc(struct binder_alloc *alloc) {}
122 #endif
123 enum lru_status binder_alloc_free_page(struct list_head *item,
124 				       struct list_lru_one *lru,
125 				       spinlock_t *lock, void *cb_arg);
126 extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
127 						  size_t data_size,
128 						  size_t offsets_size,
129 						  size_t extra_buffers_size,
130 						  int is_async);
131 extern void binder_alloc_init(struct binder_alloc *alloc);
132 extern int binder_alloc_shrinker_init(void);
133 extern void binder_alloc_vma_close(struct binder_alloc *alloc);
134 extern struct binder_buffer *
135 binder_alloc_prepare_to_free(struct binder_alloc *alloc,
136 			     uintptr_t user_ptr);
137 extern void binder_alloc_free_buf(struct binder_alloc *alloc,
138 				  struct binder_buffer *buffer);
139 extern int binder_alloc_mmap_handler(struct binder_alloc *alloc,
140 				     struct vm_area_struct *vma);
141 extern void binder_alloc_deferred_release(struct binder_alloc *alloc);
142 extern int binder_alloc_get_allocated_count(struct binder_alloc *alloc);
143 extern void binder_alloc_print_allocated(struct seq_file *m,
144 					 struct binder_alloc *alloc);
145 void binder_alloc_print_pages(struct seq_file *m,
146 			      struct binder_alloc *alloc);
147 
148 /**
149  * binder_alloc_get_free_async_space() - get free space available for async
150  * @alloc:	binder_alloc for this proc
151  *
152  * Return:	the bytes remaining in the address-space for async transactions
153  */
154 static inline size_t
155 binder_alloc_get_free_async_space(struct binder_alloc *alloc)
156 {
157 	size_t free_async_space;
158 
159 	mutex_lock(&alloc->mutex);
160 	free_async_space = alloc->free_async_space;
161 	mutex_unlock(&alloc->mutex);
162 	return free_async_space;
163 }
164 
165 /**
166  * binder_alloc_get_user_buffer_offset() - get offset between kernel/user addrs
167  * @alloc:	binder_alloc for this proc
168  *
169  * Return:	the offset between kernel and user-space addresses to use for
170  * virtual address conversion
171  */
172 static inline ptrdiff_t
173 binder_alloc_get_user_buffer_offset(struct binder_alloc *alloc)
174 {
175 	/*
176 	 * user_buffer_offset is constant if vma is set and
177 	 * undefined if vma is not set. It is possible to
178 	 * get here with !alloc->vma if the target process
179 	 * is dying while a transaction is being initiated.
180 	 * Returning the old value is ok in this case and
181 	 * the transaction will fail.
182 	 */
183 	return alloc->user_buffer_offset;
184 }
185 
186 #endif /* _LINUX_BINDER_ALLOC_H */
187 
188