blob: 11a08bf72bcce24892403265e7d9fc7d1f35a8d1 [file] [log] [blame]
Todd Kjos0c972a02017-06-29 12:01:41 -07001/* binder_alloc.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2017 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <asm/cacheflush.h>
21#include <linux/list.h>
22#include <linux/sched/mm.h>
23#include <linux/module.h>
24#include <linux/rtmutex.h>
25#include <linux/rbtree.h>
26#include <linux/seq_file.h>
27#include <linux/vmalloc.h>
28#include <linux/slab.h>
29#include <linux/sched.h>
Sherry Yangf2517eb2017-08-23 08:46:42 -070030#include <linux/list_lru.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070031#include "binder_alloc.h"
32#include "binder_trace.h"
33
Sherry Yangf2517eb2017-08-23 08:46:42 -070034struct list_lru binder_alloc_lru;
35
Todd Kjos0c972a02017-06-29 12:01:41 -070036static DEFINE_MUTEX(binder_alloc_mmap_lock);
37
38enum {
39 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
40 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
41 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
42};
43static uint32_t binder_alloc_debug_mask;
44
45module_param_named(debug_mask, binder_alloc_debug_mask,
46 uint, 0644);
47
48#define binder_alloc_debug(mask, x...) \
49 do { \
50 if (binder_alloc_debug_mask & mask) \
51 pr_info(x); \
52 } while (0)
53
Sherry Yange21762192017-08-23 08:46:39 -070054static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
55{
56 return list_entry(buffer->entry.next, struct binder_buffer, entry);
57}
58
59static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
60{
61 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
62}
63
Todd Kjos0c972a02017-06-29 12:01:41 -070064static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
65 struct binder_buffer *buffer)
66{
67 if (list_is_last(&buffer->entry, &alloc->buffers))
Sherry Yang74310e02017-08-23 08:46:41 -070068 return (u8 *)alloc->buffer +
69 alloc->buffer_size - (u8 *)buffer->data;
70 return (u8 *)binder_buffer_next(buffer)->data - (u8 *)buffer->data;
Todd Kjos0c972a02017-06-29 12:01:41 -070071}
72
73static void binder_insert_free_buffer(struct binder_alloc *alloc,
74 struct binder_buffer *new_buffer)
75{
76 struct rb_node **p = &alloc->free_buffers.rb_node;
77 struct rb_node *parent = NULL;
78 struct binder_buffer *buffer;
79 size_t buffer_size;
80 size_t new_buffer_size;
81
82 BUG_ON(!new_buffer->free);
83
84 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
85
86 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
87 "%d: add free buffer, size %zd, at %pK\n",
88 alloc->pid, new_buffer_size, new_buffer);
89
90 while (*p) {
91 parent = *p;
92 buffer = rb_entry(parent, struct binder_buffer, rb_node);
93 BUG_ON(!buffer->free);
94
95 buffer_size = binder_alloc_buffer_size(alloc, buffer);
96
97 if (new_buffer_size < buffer_size)
98 p = &parent->rb_left;
99 else
100 p = &parent->rb_right;
101 }
102 rb_link_node(&new_buffer->rb_node, parent, p);
103 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
104}
105
106static void binder_insert_allocated_buffer_locked(
107 struct binder_alloc *alloc, struct binder_buffer *new_buffer)
108{
109 struct rb_node **p = &alloc->allocated_buffers.rb_node;
110 struct rb_node *parent = NULL;
111 struct binder_buffer *buffer;
112
113 BUG_ON(new_buffer->free);
114
115 while (*p) {
116 parent = *p;
117 buffer = rb_entry(parent, struct binder_buffer, rb_node);
118 BUG_ON(buffer->free);
119
Sherry Yang74310e02017-08-23 08:46:41 -0700120 if (new_buffer->data < buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700121 p = &parent->rb_left;
Sherry Yang74310e02017-08-23 08:46:41 -0700122 else if (new_buffer->data > buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700123 p = &parent->rb_right;
124 else
125 BUG();
126 }
127 rb_link_node(&new_buffer->rb_node, parent, p);
128 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
129}
130
Todd Kjos53d311cf2017-06-29 12:01:51 -0700131static struct binder_buffer *binder_alloc_prepare_to_free_locked(
Todd Kjos0c972a02017-06-29 12:01:41 -0700132 struct binder_alloc *alloc,
133 uintptr_t user_ptr)
134{
135 struct rb_node *n = alloc->allocated_buffers.rb_node;
136 struct binder_buffer *buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700137 void *kern_ptr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700138
Sherry Yang74310e02017-08-23 08:46:41 -0700139 kern_ptr = (void *)(user_ptr - alloc->user_buffer_offset);
Todd Kjos0c972a02017-06-29 12:01:41 -0700140
141 while (n) {
142 buffer = rb_entry(n, struct binder_buffer, rb_node);
143 BUG_ON(buffer->free);
144
Sherry Yang74310e02017-08-23 08:46:41 -0700145 if (kern_ptr < buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700146 n = n->rb_left;
Sherry Yang74310e02017-08-23 08:46:41 -0700147 else if (kern_ptr > buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700148 n = n->rb_right;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700149 else {
150 /*
151 * Guard against user threads attempting to
152 * free the buffer twice
153 */
154 if (buffer->free_in_progress) {
155 pr_err("%d:%d FREE_BUFFER u%016llx user freed buffer twice\n",
156 alloc->pid, current->pid, (u64)user_ptr);
157 return NULL;
158 }
159 buffer->free_in_progress = 1;
Todd Kjos0c972a02017-06-29 12:01:41 -0700160 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700161 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700162 }
163 return NULL;
164}
165
166/**
167 * binder_alloc_buffer_lookup() - get buffer given user ptr
168 * @alloc: binder_alloc for this proc
169 * @user_ptr: User pointer to buffer data
170 *
171 * Validate userspace pointer to buffer data and return buffer corresponding to
172 * that user pointer. Search the rb tree for buffer that matches user data
173 * pointer.
174 *
175 * Return: Pointer to buffer or NULL
176 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700177struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
178 uintptr_t user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700179{
180 struct binder_buffer *buffer;
181
182 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700183 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700184 mutex_unlock(&alloc->mutex);
185 return buffer;
186}
187
188static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
189 void *start, void *end,
190 struct vm_area_struct *vma)
191{
192 void *page_addr;
193 unsigned long user_page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700194 struct binder_lru_page *page;
195 struct mm_struct *mm = NULL;
196 bool need_mm = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700197
198 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
199 "%d: %s pages %pK-%pK\n", alloc->pid,
200 allocate ? "allocate" : "free", start, end);
201
202 if (end <= start)
203 return 0;
204
205 trace_binder_update_page_range(alloc, allocate, start, end);
206
Sherry Yangf2517eb2017-08-23 08:46:42 -0700207 if (allocate == 0)
208 goto free_range;
209
210 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
211 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
212 if (!page->page_ptr) {
213 need_mm = true;
214 break;
215 }
216 }
217
218 if (!vma && need_mm)
Todd Kjos0c972a02017-06-29 12:01:41 -0700219 mm = get_task_mm(alloc->tsk);
220
221 if (mm) {
222 down_write(&mm->mmap_sem);
223 vma = alloc->vma;
224 if (vma && mm != alloc->vma_vm_mm) {
225 pr_err("%d: vma mm and task mm mismatch\n",
226 alloc->pid);
227 vma = NULL;
228 }
229 }
230
Sherry Yangf2517eb2017-08-23 08:46:42 -0700231 if (!vma && need_mm) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700232 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
233 alloc->pid);
234 goto err_no_vma;
235 }
236
237 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
238 int ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700239 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700240
241 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
242
Sherry Yangf2517eb2017-08-23 08:46:42 -0700243 if (page->page_ptr) {
244 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
245 WARN_ON(!on_lru);
246 continue;
247 }
248
249 if (WARN_ON(!vma))
250 goto err_page_ptr_cleared;
251
252 page->page_ptr = alloc_page(GFP_KERNEL |
253 __GFP_HIGHMEM |
254 __GFP_ZERO);
255 if (!page->page_ptr) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700256 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
257 alloc->pid, page_addr);
258 goto err_alloc_page_failed;
259 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700260 page->alloc = alloc;
261 INIT_LIST_HEAD(&page->lru);
262
Todd Kjos0c972a02017-06-29 12:01:41 -0700263 ret = map_kernel_range_noflush((unsigned long)page_addr,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700264 PAGE_SIZE, PAGE_KERNEL,
265 &page->page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700266 flush_cache_vmap((unsigned long)page_addr,
267 (unsigned long)page_addr + PAGE_SIZE);
268 if (ret != 1) {
269 pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
270 alloc->pid, page_addr);
271 goto err_map_kernel_failed;
272 }
273 user_page_addr =
274 (uintptr_t)page_addr + alloc->user_buffer_offset;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700275 ret = vm_insert_page(vma, user_page_addr, page[0].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700276 if (ret) {
277 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
278 alloc->pid, user_page_addr);
279 goto err_vm_insert_page_failed;
280 }
281 /* vm_insert_page does not seem to increment the refcount */
282 }
283 if (mm) {
284 up_write(&mm->mmap_sem);
285 mmput(mm);
286 }
287 return 0;
288
289free_range:
290 for (page_addr = end - PAGE_SIZE; page_addr >= start;
291 page_addr -= PAGE_SIZE) {
Sherry Yangf2517eb2017-08-23 08:46:42 -0700292 bool ret;
293
Todd Kjos0c972a02017-06-29 12:01:41 -0700294 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
Sherry Yangf2517eb2017-08-23 08:46:42 -0700295
296 ret = list_lru_add(&binder_alloc_lru, &page->lru);
297 WARN_ON(!ret);
298 continue;
299
Todd Kjos0c972a02017-06-29 12:01:41 -0700300err_vm_insert_page_failed:
301 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
302err_map_kernel_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700303 __free_page(page->page_ptr);
304 page->page_ptr = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700305err_alloc_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700306err_page_ptr_cleared:
Todd Kjos0c972a02017-06-29 12:01:41 -0700307 ;
308 }
309err_no_vma:
310 if (mm) {
311 up_write(&mm->mmap_sem);
312 mmput(mm);
313 }
Todd Kjos57ada2f2017-06-29 12:01:46 -0700314 return vma ? -ENOMEM : -ESRCH;
Todd Kjos0c972a02017-06-29 12:01:41 -0700315}
316
317struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc,
318 size_t data_size,
319 size_t offsets_size,
320 size_t extra_buffers_size,
321 int is_async)
322{
323 struct rb_node *n = alloc->free_buffers.rb_node;
324 struct binder_buffer *buffer;
325 size_t buffer_size;
326 struct rb_node *best_fit = NULL;
327 void *has_page_addr;
328 void *end_page_addr;
329 size_t size, data_offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700330 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700331
332 if (alloc->vma == NULL) {
333 pr_err("%d: binder_alloc_buf, no vma\n",
334 alloc->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700335 return ERR_PTR(-ESRCH);
Todd Kjos0c972a02017-06-29 12:01:41 -0700336 }
337
338 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
339 ALIGN(offsets_size, sizeof(void *));
340
341 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
342 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
343 "%d: got transaction with invalid size %zd-%zd\n",
344 alloc->pid, data_size, offsets_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700345 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700346 }
347 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
348 if (size < data_offsets_size || size < extra_buffers_size) {
349 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
350 "%d: got transaction with invalid extra_buffers_size %zd\n",
351 alloc->pid, extra_buffers_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700352 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700353 }
354 if (is_async &&
355 alloc->free_async_space < size + sizeof(struct binder_buffer)) {
356 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
357 "%d: binder_alloc_buf size %zd failed, no async space left\n",
358 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700359 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700360 }
361
Sherry Yang74310e02017-08-23 08:46:41 -0700362 /* Pad 0-size buffers so they get assigned unique addresses */
363 size = max(size, sizeof(void *));
364
Todd Kjos0c972a02017-06-29 12:01:41 -0700365 while (n) {
366 buffer = rb_entry(n, struct binder_buffer, rb_node);
367 BUG_ON(!buffer->free);
368 buffer_size = binder_alloc_buffer_size(alloc, buffer);
369
370 if (size < buffer_size) {
371 best_fit = n;
372 n = n->rb_left;
373 } else if (size > buffer_size)
374 n = n->rb_right;
375 else {
376 best_fit = n;
377 break;
378 }
379 }
380 if (best_fit == NULL) {
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700381 size_t allocated_buffers = 0;
382 size_t largest_alloc_size = 0;
383 size_t total_alloc_size = 0;
384 size_t free_buffers = 0;
385 size_t largest_free_size = 0;
386 size_t total_free_size = 0;
387
388 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
389 n = rb_next(n)) {
390 buffer = rb_entry(n, struct binder_buffer, rb_node);
391 buffer_size = binder_alloc_buffer_size(alloc, buffer);
392 allocated_buffers++;
393 total_alloc_size += buffer_size;
394 if (buffer_size > largest_alloc_size)
395 largest_alloc_size = buffer_size;
396 }
397 for (n = rb_first(&alloc->free_buffers); n != NULL;
398 n = rb_next(n)) {
399 buffer = rb_entry(n, struct binder_buffer, rb_node);
400 buffer_size = binder_alloc_buffer_size(alloc, buffer);
401 free_buffers++;
402 total_free_size += buffer_size;
403 if (buffer_size > largest_free_size)
404 largest_free_size = buffer_size;
405 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700406 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
407 alloc->pid, size);
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700408 pr_err("allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
409 total_alloc_size, allocated_buffers, largest_alloc_size,
410 total_free_size, free_buffers, largest_free_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700411 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700412 }
413 if (n == NULL) {
414 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
415 buffer_size = binder_alloc_buffer_size(alloc, buffer);
416 }
417
418 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
419 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
420 alloc->pid, size, buffer, buffer_size);
421
422 has_page_addr =
423 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
Sherry Yang74310e02017-08-23 08:46:41 -0700424 WARN_ON(n && buffer_size != size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700425 end_page_addr =
Sherry Yang74310e02017-08-23 08:46:41 -0700426 (void *)PAGE_ALIGN((uintptr_t)buffer->data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700427 if (end_page_addr > has_page_addr)
428 end_page_addr = has_page_addr;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700429 ret = binder_update_page_range(alloc, 1,
430 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL);
431 if (ret)
432 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700433
Todd Kjos0c972a02017-06-29 12:01:41 -0700434 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700435 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700436
Sherry Yang74310e02017-08-23 08:46:41 -0700437 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
438 if (!new_buffer) {
439 pr_err("%s: %d failed to alloc new buffer struct\n",
440 __func__, alloc->pid);
441 goto err_alloc_buf_struct_failed;
442 }
443 new_buffer->data = (u8 *)buffer->data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700444 list_add(&new_buffer->entry, &buffer->entry);
445 new_buffer->free = 1;
446 binder_insert_free_buffer(alloc, new_buffer);
447 }
Sherry Yang74310e02017-08-23 08:46:41 -0700448
449 rb_erase(best_fit, &alloc->free_buffers);
450 buffer->free = 0;
451 buffer->free_in_progress = 0;
452 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700453 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
454 "%d: binder_alloc_buf size %zd got %pK\n",
455 alloc->pid, size, buffer);
456 buffer->data_size = data_size;
457 buffer->offsets_size = offsets_size;
458 buffer->async_transaction = is_async;
459 buffer->extra_buffers_size = extra_buffers_size;
460 if (is_async) {
461 alloc->free_async_space -= size + sizeof(struct binder_buffer);
462 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
463 "%d: binder_alloc_buf size %zd async free %zd\n",
464 alloc->pid, size, alloc->free_async_space);
465 }
466 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700467
468err_alloc_buf_struct_failed:
469 binder_update_page_range(alloc, 0,
470 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
471 end_page_addr, NULL);
472 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700473}
474
475/**
476 * binder_alloc_new_buf() - Allocate a new binder buffer
477 * @alloc: binder_alloc for this proc
478 * @data_size: size of user data buffer
479 * @offsets_size: user specified buffer offset
480 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
481 * @is_async: buffer for async transaction
482 *
483 * Allocate a new buffer given the requested sizes. Returns
484 * the kernel version of the buffer pointer. The size allocated
485 * is the sum of the three given sizes (each rounded up to
486 * pointer-sized boundary)
487 *
488 * Return: The allocated buffer or %NULL if error
489 */
490struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
491 size_t data_size,
492 size_t offsets_size,
493 size_t extra_buffers_size,
494 int is_async)
495{
496 struct binder_buffer *buffer;
497
498 mutex_lock(&alloc->mutex);
499 buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
500 extra_buffers_size, is_async);
501 mutex_unlock(&alloc->mutex);
502 return buffer;
503}
504
505static void *buffer_start_page(struct binder_buffer *buffer)
506{
Sherry Yang74310e02017-08-23 08:46:41 -0700507 return (void *)((uintptr_t)buffer->data & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700508}
509
Sherry Yang74310e02017-08-23 08:46:41 -0700510static void *prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700511{
Sherry Yang74310e02017-08-23 08:46:41 -0700512 return (void *)(((uintptr_t)(buffer->data) - 1) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700513}
514
515static void binder_delete_free_buffer(struct binder_alloc *alloc,
516 struct binder_buffer *buffer)
517{
518 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700519 bool to_free = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700520 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700521 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700522 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700523 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
524 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700525 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700526 "%d: merge free, buffer %pK share page with %pK\n",
527 alloc->pid, buffer->data, prev->data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700528 }
529
530 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700531 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700532 if (buffer_start_page(next) == buffer_start_page(buffer)) {
533 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700534 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700535 "%d: merge free, buffer %pK share page with %pK\n",
536 alloc->pid,
537 buffer->data,
538 next->data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700539 }
540 }
Sherry Yang74310e02017-08-23 08:46:41 -0700541
542 if (PAGE_ALIGNED(buffer->data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700543 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700544 "%d: merge free, buffer start %pK is page aligned\n",
545 alloc->pid, buffer->data);
546 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700547 }
Sherry Yang74310e02017-08-23 08:46:41 -0700548
549 if (to_free) {
550 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
551 "%d: merge free, buffer %pK do not share page with %pK or %pK\n",
552 alloc->pid, buffer->data,
553 prev->data, next->data);
554 binder_update_page_range(alloc, 0, buffer_start_page(buffer),
555 buffer_start_page(buffer) + PAGE_SIZE,
556 NULL);
557 }
558 list_del(&buffer->entry);
559 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700560}
561
562static void binder_free_buf_locked(struct binder_alloc *alloc,
563 struct binder_buffer *buffer)
564{
565 size_t size, buffer_size;
566
567 buffer_size = binder_alloc_buffer_size(alloc, buffer);
568
569 size = ALIGN(buffer->data_size, sizeof(void *)) +
570 ALIGN(buffer->offsets_size, sizeof(void *)) +
571 ALIGN(buffer->extra_buffers_size, sizeof(void *));
572
573 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
574 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
575 alloc->pid, buffer, size, buffer_size);
576
577 BUG_ON(buffer->free);
578 BUG_ON(size > buffer_size);
579 BUG_ON(buffer->transaction != NULL);
Sherry Yang74310e02017-08-23 08:46:41 -0700580 BUG_ON(buffer->data < alloc->buffer);
581 BUG_ON(buffer->data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700582
583 if (buffer->async_transaction) {
584 alloc->free_async_space += size + sizeof(struct binder_buffer);
585
586 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
587 "%d: binder_free_buf size %zd async free %zd\n",
588 alloc->pid, size, alloc->free_async_space);
589 }
590
591 binder_update_page_range(alloc, 0,
592 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
593 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
594 NULL);
595
596 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
597 buffer->free = 1;
598 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700599 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700600
601 if (next->free) {
602 rb_erase(&next->rb_node, &alloc->free_buffers);
603 binder_delete_free_buffer(alloc, next);
604 }
605 }
606 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700607 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700608
609 if (prev->free) {
610 binder_delete_free_buffer(alloc, buffer);
611 rb_erase(&prev->rb_node, &alloc->free_buffers);
612 buffer = prev;
613 }
614 }
615 binder_insert_free_buffer(alloc, buffer);
616}
617
618/**
619 * binder_alloc_free_buf() - free a binder buffer
620 * @alloc: binder_alloc for this proc
621 * @buffer: kernel pointer to buffer
622 *
623 * Free the buffer allocated via binder_alloc_new_buffer()
624 */
625void binder_alloc_free_buf(struct binder_alloc *alloc,
626 struct binder_buffer *buffer)
627{
628 mutex_lock(&alloc->mutex);
629 binder_free_buf_locked(alloc, buffer);
630 mutex_unlock(&alloc->mutex);
631}
632
633/**
634 * binder_alloc_mmap_handler() - map virtual address space for proc
635 * @alloc: alloc structure for this proc
636 * @vma: vma passed to mmap()
637 *
638 * Called by binder_mmap() to initialize the space specified in
639 * vma for allocating binder buffers
640 *
641 * Return:
642 * 0 = success
643 * -EBUSY = address space already mapped
644 * -ENOMEM = failed to map memory to given address space
645 */
646int binder_alloc_mmap_handler(struct binder_alloc *alloc,
647 struct vm_area_struct *vma)
648{
649 int ret;
650 struct vm_struct *area;
651 const char *failure_string;
652 struct binder_buffer *buffer;
653
654 mutex_lock(&binder_alloc_mmap_lock);
655 if (alloc->buffer) {
656 ret = -EBUSY;
657 failure_string = "already mapped";
658 goto err_already_mapped;
659 }
660
661 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
662 if (area == NULL) {
663 ret = -ENOMEM;
664 failure_string = "get_vm_area";
665 goto err_get_vm_area_failed;
666 }
667 alloc->buffer = area->addr;
668 alloc->user_buffer_offset =
669 vma->vm_start - (uintptr_t)alloc->buffer;
670 mutex_unlock(&binder_alloc_mmap_lock);
671
672#ifdef CONFIG_CPU_CACHE_VIPT
673 if (cache_is_vipt_aliasing()) {
674 while (CACHE_COLOUR(
675 (vma->vm_start ^ (uint32_t)alloc->buffer))) {
676 pr_info("%s: %d %lx-%lx maps %pK bad alignment\n",
677 __func__, alloc->pid, vma->vm_start,
678 vma->vm_end, alloc->buffer);
679 vma->vm_start += PAGE_SIZE;
680 }
681 }
682#endif
683 alloc->pages = kzalloc(sizeof(alloc->pages[0]) *
684 ((vma->vm_end - vma->vm_start) / PAGE_SIZE),
685 GFP_KERNEL);
686 if (alloc->pages == NULL) {
687 ret = -ENOMEM;
688 failure_string = "alloc page array";
689 goto err_alloc_pages_failed;
690 }
691 alloc->buffer_size = vma->vm_end - vma->vm_start;
692
Sherry Yang74310e02017-08-23 08:46:41 -0700693 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
694 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700695 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700696 failure_string = "alloc buffer struct";
697 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700698 }
Sherry Yang74310e02017-08-23 08:46:41 -0700699
700 buffer->data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700701 INIT_LIST_HEAD(&alloc->buffers);
702 list_add(&buffer->entry, &alloc->buffers);
703 buffer->free = 1;
704 binder_insert_free_buffer(alloc, buffer);
705 alloc->free_async_space = alloc->buffer_size / 2;
706 barrier();
707 alloc->vma = vma;
708 alloc->vma_vm_mm = vma->vm_mm;
709
710 return 0;
711
Sherry Yang74310e02017-08-23 08:46:41 -0700712err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700713 kfree(alloc->pages);
714 alloc->pages = NULL;
715err_alloc_pages_failed:
716 mutex_lock(&binder_alloc_mmap_lock);
717 vfree(alloc->buffer);
718 alloc->buffer = NULL;
719err_get_vm_area_failed:
720err_already_mapped:
721 mutex_unlock(&binder_alloc_mmap_lock);
722 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
723 alloc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
724 return ret;
725}
726
727
728void binder_alloc_deferred_release(struct binder_alloc *alloc)
729{
730 struct rb_node *n;
731 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700732 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700733
734 BUG_ON(alloc->vma);
735
736 buffers = 0;
737 mutex_lock(&alloc->mutex);
738 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700739 buffer = rb_entry(n, struct binder_buffer, rb_node);
740
741 /* Transaction should already have been freed */
742 BUG_ON(buffer->transaction);
743
744 binder_free_buf_locked(alloc, buffer);
745 buffers++;
746 }
747
Sherry Yang74310e02017-08-23 08:46:41 -0700748 while (!list_empty(&alloc->buffers)) {
749 buffer = list_first_entry(&alloc->buffers,
750 struct binder_buffer, entry);
751 WARN_ON(!buffer->free);
752
753 list_del(&buffer->entry);
754 WARN_ON_ONCE(!list_empty(&alloc->buffers));
755 kfree(buffer);
756 }
757
Todd Kjos0c972a02017-06-29 12:01:41 -0700758 page_count = 0;
759 if (alloc->pages) {
760 int i;
761
762 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
763 void *page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700764 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700765
Sherry Yangf2517eb2017-08-23 08:46:42 -0700766 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700767 continue;
768
Sherry Yangf2517eb2017-08-23 08:46:42 -0700769 on_lru = list_lru_del(&binder_alloc_lru,
770 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700771 page_addr = alloc->buffer + i * PAGE_SIZE;
772 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700773 "%s: %d: page %d at %pK %s\n",
774 __func__, alloc->pid, i, page_addr,
775 on_lru ? "on lru" : "active");
Todd Kjos0c972a02017-06-29 12:01:41 -0700776 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700777 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700778 page_count++;
779 }
780 kfree(alloc->pages);
781 vfree(alloc->buffer);
782 }
783 mutex_unlock(&alloc->mutex);
784
785 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
786 "%s: %d buffers %d, pages %d\n",
787 __func__, alloc->pid, buffers, page_count);
788}
789
790static void print_binder_buffer(struct seq_file *m, const char *prefix,
791 struct binder_buffer *buffer)
792{
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700793 seq_printf(m, "%s %d: %pK size %zd:%zd:%zd %s\n",
Todd Kjos0c972a02017-06-29 12:01:41 -0700794 prefix, buffer->debug_id, buffer->data,
795 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700796 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700797 buffer->transaction ? "active" : "delivered");
798}
799
800/**
801 * binder_alloc_print_allocated() - print buffer info
802 * @m: seq_file for output via seq_printf()
803 * @alloc: binder_alloc for this proc
804 *
805 * Prints information about every buffer associated with
806 * the binder_alloc state to the given seq_file
807 */
808void binder_alloc_print_allocated(struct seq_file *m,
809 struct binder_alloc *alloc)
810{
811 struct rb_node *n;
812
813 mutex_lock(&alloc->mutex);
814 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
815 print_binder_buffer(m, " buffer",
816 rb_entry(n, struct binder_buffer, rb_node));
817 mutex_unlock(&alloc->mutex);
818}
819
820/**
821 * binder_alloc_get_allocated_count() - return count of buffers
822 * @alloc: binder_alloc for this proc
823 *
824 * Return: count of allocated buffers
825 */
826int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
827{
828 struct rb_node *n;
829 int count = 0;
830
831 mutex_lock(&alloc->mutex);
832 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
833 count++;
834 mutex_unlock(&alloc->mutex);
835 return count;
836}
837
838
839/**
840 * binder_alloc_vma_close() - invalidate address space
841 * @alloc: binder_alloc for this proc
842 *
843 * Called from binder_vma_close() when releasing address space.
844 * Clears alloc->vma to prevent new incoming transactions from
845 * allocating more buffers.
846 */
847void binder_alloc_vma_close(struct binder_alloc *alloc)
848{
849 WRITE_ONCE(alloc->vma, NULL);
850 WRITE_ONCE(alloc->vma_vm_mm, NULL);
851}
852
853/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700854 * binder_alloc_free_page() - shrinker callback to free pages
855 * @item: item to free
856 * @lock: lock protecting the item
857 * @cb_arg: callback argument
858 *
859 * Called from list_lru_walk() in binder_shrink_scan() to free
860 * up pages when the system is under memory pressure.
861 */
862enum lru_status binder_alloc_free_page(struct list_head *item,
863 struct list_lru_one *lru,
864 spinlock_t *lock,
865 void *cb_arg)
866{
867 struct mm_struct *mm = NULL;
868 struct binder_lru_page *page = container_of(item,
869 struct binder_lru_page,
870 lru);
871 struct binder_alloc *alloc;
872 uintptr_t page_addr;
873 size_t index;
874
875 alloc = page->alloc;
876 if (!mutex_trylock(&alloc->mutex))
877 goto err_get_alloc_mutex_failed;
878
879 if (!page->page_ptr)
880 goto err_page_already_freed;
881
882 index = page - alloc->pages;
883 page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
884 if (alloc->vma) {
885 mm = get_task_mm(alloc->tsk);
886 if (!mm)
887 goto err_get_task_mm_failed;
888 if (!down_write_trylock(&mm->mmap_sem))
889 goto err_down_write_mmap_sem_failed;
890
891 zap_page_range(alloc->vma,
892 page_addr + alloc->user_buffer_offset,
893 PAGE_SIZE);
894
895 up_write(&mm->mmap_sem);
896 mmput(mm);
897 }
898
899 unmap_kernel_range(page_addr, PAGE_SIZE);
900 __free_page(page->page_ptr);
901 page->page_ptr = NULL;
902
903 list_lru_isolate(lru, item);
904
905 mutex_unlock(&alloc->mutex);
906 return LRU_REMOVED;
907
908err_down_write_mmap_sem_failed:
909 mmput(mm);
910err_get_task_mm_failed:
911err_page_already_freed:
912 mutex_unlock(&alloc->mutex);
913err_get_alloc_mutex_failed:
914 return LRU_SKIP;
915}
916
917static unsigned long
918binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
919{
920 unsigned long ret = list_lru_count(&binder_alloc_lru);
921 return ret;
922}
923
924static unsigned long
925binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
926{
927 unsigned long ret;
928
929 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
930 NULL, sc->nr_to_scan);
931 return ret;
932}
933
934struct shrinker binder_shrinker = {
935 .count_objects = binder_shrink_count,
936 .scan_objects = binder_shrink_scan,
937 .seeks = DEFAULT_SEEKS,
938};
939
940/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700941 * binder_alloc_init() - called by binder_open() for per-proc initialization
942 * @alloc: binder_alloc for this proc
943 *
944 * Called from binder_open() to initialize binder_alloc fields for
945 * new binder proc
946 */
947void binder_alloc_init(struct binder_alloc *alloc)
948{
949 alloc->tsk = current->group_leader;
950 alloc->pid = current->group_leader->pid;
951 mutex_init(&alloc->mutex);
952}
953
Sherry Yangf2517eb2017-08-23 08:46:42 -0700954void binder_alloc_shrinker_init(void)
955{
956 list_lru_init(&binder_alloc_lru);
957 register_shrinker(&binder_shrinker);
958}