1 // SPDX-License-Identifier: GPL-2.0-only
2 /* binder_alloc.c
3 *
4 * Android IPC Subsystem
5 *
6 * Copyright (C) 2007-2017 Google, Inc.
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/list.h>
12 #include <linux/sched/mm.h>
13 #include <linux/module.h>
14 #include <linux/rtmutex.h>
15 #include <linux/rbtree.h>
16 #include <linux/seq_file.h>
17 #include <linux/vmalloc.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/list_lru.h>
21 #include <linux/ratelimit.h>
22 #include <asm/cacheflush.h>
23 #include <linux/uaccess.h>
24 #include <linux/highmem.h>
25 #include <linux/sizes.h>
26 #include "binder_internal.h"
27 #include "binder_trace.h"
28 #include <trace/hooks/binder.h>
29
30 struct list_lru binder_freelist;
31
32 static DEFINE_MUTEX(binder_alloc_mmap_lock);
33
34 enum {
35 BINDER_DEBUG_USER_ERROR = 1U << 0,
36 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
37 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
38 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
39 };
40 static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
41
42 module_param_named(debug_mask, binder_alloc_debug_mask,
43 uint, 0644);
44
45 #define binder_alloc_debug(mask, x...) \
46 do { \
47 if (binder_alloc_debug_mask & mask) \
48 pr_info_ratelimited(x); \
49 } while (0)
50
binder_buffer_next(struct binder_buffer * buffer)51 static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
52 {
53 return list_entry(buffer->entry.next, struct binder_buffer, entry);
54 }
55
binder_buffer_prev(struct binder_buffer * buffer)56 static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
57 {
58 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
59 }
60
binder_alloc_buffer_size(struct binder_alloc * alloc,struct binder_buffer * buffer)61 static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
62 struct binder_buffer *buffer)
63 {
64 if (list_is_last(&buffer->entry, &alloc->buffers))
65 return alloc->buffer + alloc->buffer_size - buffer->user_data;
66 return binder_buffer_next(buffer)->user_data - buffer->user_data;
67 }
68
binder_insert_free_buffer(struct binder_alloc * alloc,struct binder_buffer * new_buffer)69 static void binder_insert_free_buffer(struct binder_alloc *alloc,
70 struct binder_buffer *new_buffer)
71 {
72 struct rb_node **p = &alloc->free_buffers.rb_node;
73 struct rb_node *parent = NULL;
74 struct binder_buffer *buffer;
75 size_t buffer_size;
76 size_t new_buffer_size;
77
78 BUG_ON(!new_buffer->free);
79
80 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
81
82 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
83 "%d: add free buffer, size %zd, at %pK\n",
84 alloc->pid, new_buffer_size, new_buffer);
85
86 while (*p) {
87 parent = *p;
88 buffer = rb_entry(parent, struct binder_buffer, rb_node);
89 BUG_ON(!buffer->free);
90
91 buffer_size = binder_alloc_buffer_size(alloc, buffer);
92
93 if (new_buffer_size < buffer_size)
94 p = &parent->rb_left;
95 else
96 p = &parent->rb_right;
97 }
98 rb_link_node(&new_buffer->rb_node, parent, p);
99 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
100 }
101
binder_insert_allocated_buffer_locked(struct binder_alloc * alloc,struct binder_buffer * new_buffer)102 static void binder_insert_allocated_buffer_locked(
103 struct binder_alloc *alloc, struct binder_buffer *new_buffer)
104 {
105 struct rb_node **p = &alloc->allocated_buffers.rb_node;
106 struct rb_node *parent = NULL;
107 struct binder_buffer *buffer;
108
109 BUG_ON(new_buffer->free);
110
111 while (*p) {
112 parent = *p;
113 buffer = rb_entry(parent, struct binder_buffer, rb_node);
114 BUG_ON(buffer->free);
115
116 if (new_buffer->user_data < buffer->user_data)
117 p = &parent->rb_left;
118 else if (new_buffer->user_data > buffer->user_data)
119 p = &parent->rb_right;
120 else
121 BUG();
122 }
123 rb_link_node(&new_buffer->rb_node, parent, p);
124 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
125 }
126
binder_alloc_prepare_to_free_locked(struct binder_alloc * alloc,unsigned long user_ptr)127 static struct binder_buffer *binder_alloc_prepare_to_free_locked(
128 struct binder_alloc *alloc,
129 unsigned long user_ptr)
130 {
131 struct rb_node *n = alloc->allocated_buffers.rb_node;
132 struct binder_buffer *buffer;
133
134 while (n) {
135 buffer = rb_entry(n, struct binder_buffer, rb_node);
136 BUG_ON(buffer->free);
137
138 if (user_ptr < (uintptr_t)buffer->user_data) {
139 n = n->rb_left;
140 } else if (user_ptr > (uintptr_t)buffer->user_data) {
141 n = n->rb_right;
142 } else {
143 /*
144 * Guard against user threads attempting to
145 * free the buffer when in use by kernel or
146 * after it's already been freed.
147 */
148 if (!buffer->allow_user_free)
149 return ERR_PTR(-EPERM);
150 buffer->allow_user_free = 0;
151 return buffer;
152 }
153 }
154 return NULL;
155 }
156
157 /**
158 * binder_alloc_prepare_to_free() - get buffer given user ptr
159 * @alloc: binder_alloc for this proc
160 * @user_ptr: User pointer to buffer data
161 *
162 * Validate userspace pointer to buffer data and return buffer corresponding to
163 * that user pointer. Search the rb tree for buffer that matches user data
164 * pointer.
165 *
166 * Return: Pointer to buffer or NULL
167 */
binder_alloc_prepare_to_free(struct binder_alloc * alloc,unsigned long user_ptr)168 struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
169 unsigned long user_ptr)
170 {
171 struct binder_buffer *buffer;
172
173 binder_alloc_lock(alloc);
174 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
175 binder_alloc_unlock(alloc);
176 return buffer;
177 }
178
179 static inline void
binder_set_installed_page(struct binder_lru_page * lru_page,struct page * page)180 binder_set_installed_page(struct binder_lru_page *lru_page,
181 struct page *page)
182 {
183 /* Pairs with acquire in binder_get_installed_page() */
184 smp_store_release(&lru_page->page_ptr, page);
185 }
186
187 static inline struct page *
binder_get_installed_page(struct binder_lru_page * lru_page)188 binder_get_installed_page(struct binder_lru_page *lru_page)
189 {
190 /* Pairs with release in binder_set_installed_page() */
191 return smp_load_acquire(&lru_page->page_ptr);
192 }
193
binder_lru_freelist_add(struct binder_alloc * alloc,unsigned long start,unsigned long end)194 static void binder_lru_freelist_add(struct binder_alloc *alloc,
195 unsigned long start, unsigned long end)
196 {
197 struct binder_lru_page *page;
198 unsigned long page_addr;
199
200 trace_binder_update_page_range(alloc, false, start, end);
201
202 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
203 size_t index;
204 int ret;
205
206 index = (page_addr - (uintptr_t)alloc->buffer) / PAGE_SIZE;
207 page = &alloc->pages[index];
208
209 if (!binder_get_installed_page(page))
210 continue;
211
212 trace_binder_free_lru_start(alloc, index);
213
214 ret = list_lru_add(&binder_freelist, &page->lru);
215 WARN_ON(!ret);
216
217 trace_binder_free_lru_end(alloc, index);
218 }
219 }
220
binder_install_single_page(struct binder_alloc * alloc,struct binder_lru_page * lru_page,unsigned long addr)221 static int binder_install_single_page(struct binder_alloc *alloc,
222 struct binder_lru_page *lru_page,
223 unsigned long addr)
224 {
225 struct page *page;
226 int ret = 0;
227
228 if (!mmget_not_zero(alloc->vma_vm_mm))
229 return -ESRCH;
230
231 /*
232 * Protected with mmap_sem in write mode as multiple tasks
233 * might race to install the same page.
234 */
235 mmap_write_lock(alloc->vma_vm_mm);
236 if (binder_get_installed_page(lru_page))
237 goto out;
238
239 if (!alloc->vma) {
240 pr_err("%d: %s failed, no vma\n", alloc->pid, __func__);
241 ret = -ESRCH;
242 goto out;
243 }
244
245 page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
246 if (!page) {
247 pr_err("%d: failed to allocate page\n", alloc->pid);
248 ret = -ENOMEM;
249 goto out;
250 }
251
252 ret = vm_insert_page(alloc->vma, addr, page);
253 if (ret) {
254 pr_err("%d: %s failed to insert page at offset %lx with %d\n",
255 alloc->pid, __func__, addr - (uintptr_t)alloc->buffer,
256 ret);
257 __free_page(page);
258 ret = -ENOMEM;
259 goto out;
260 }
261
262 /* Mark page installation complete and safe to use */
263 binder_set_installed_page(lru_page, page);
264 out:
265 mmap_write_unlock(alloc->vma_vm_mm);
266 mmput_async(alloc->vma_vm_mm);
267 return ret;
268 }
269
binder_install_buffer_pages(struct binder_alloc * alloc,struct binder_buffer * buffer,size_t size)270 static int binder_install_buffer_pages(struct binder_alloc *alloc,
271 struct binder_buffer *buffer,
272 size_t size)
273 {
274 struct binder_lru_page *page;
275 unsigned long start, final;
276 unsigned long page_addr;
277
278 start = (uintptr_t)buffer->user_data & PAGE_MASK;
279 final = PAGE_ALIGN((uintptr_t)buffer->user_data + size);
280
281 for (page_addr = start; page_addr < final; page_addr += PAGE_SIZE) {
282 unsigned long index;
283 int ret;
284
285 index = (page_addr - (uintptr_t)alloc->buffer) / PAGE_SIZE;
286 page = &alloc->pages[index];
287
288 if (binder_get_installed_page(page))
289 continue;
290
291 trace_binder_alloc_page_start(alloc, index);
292
293 ret = binder_install_single_page(alloc, page, page_addr);
294 if (ret)
295 return ret;
296
297 trace_binder_alloc_page_end(alloc, index);
298 }
299
300 return 0;
301 }
302
303 /* The range of pages should exclude those shared with other buffers */
binder_lru_freelist_del(struct binder_alloc * alloc,unsigned long start,unsigned long end)304 static void binder_lru_freelist_del(struct binder_alloc *alloc,
305 unsigned long start, unsigned long end)
306 {
307 struct binder_lru_page *page;
308 unsigned long page_addr;
309
310 trace_binder_update_page_range(alloc, true, start, end);
311
312 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
313 unsigned long index;
314 bool on_lru;
315
316 index = (page_addr - (uintptr_t)alloc->buffer) / PAGE_SIZE;
317 page = &alloc->pages[index];
318
319 if (page->page_ptr) {
320 trace_binder_alloc_lru_start(alloc, index);
321
322 on_lru = list_lru_del(&binder_freelist, &page->lru);
323 WARN_ON(!on_lru);
324
325 trace_binder_alloc_lru_end(alloc, index);
326 continue;
327 }
328
329 if (index + 1 > alloc->pages_high)
330 alloc->pages_high = index + 1;
331 }
332 }
333
binder_alloc_set_vma(struct binder_alloc * alloc,struct vm_area_struct * vma)334 static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
335 struct vm_area_struct *vma)
336 {
337 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
338 smp_store_release(&alloc->vma, vma);
339 }
340
binder_alloc_get_vma(struct binder_alloc * alloc)341 static inline struct vm_area_struct *binder_alloc_get_vma(
342 struct binder_alloc *alloc)
343 {
344 /* pairs with smp_store_release in binder_alloc_set_vma() */
345 return smp_load_acquire(&alloc->vma);
346 }
347
debug_no_space_locked(struct binder_alloc * alloc)348 static void debug_no_space_locked(struct binder_alloc *alloc)
349 {
350 size_t largest_alloc_size = 0;
351 struct binder_buffer *buffer;
352 size_t allocated_buffers = 0;
353 size_t largest_free_size = 0;
354 size_t total_alloc_size = 0;
355 size_t total_free_size = 0;
356 size_t free_buffers = 0;
357 size_t buffer_size;
358 struct rb_node *n;
359
360 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
361 buffer = rb_entry(n, struct binder_buffer, rb_node);
362 buffer_size = binder_alloc_buffer_size(alloc, buffer);
363 allocated_buffers++;
364 total_alloc_size += buffer_size;
365 if (buffer_size > largest_alloc_size)
366 largest_alloc_size = buffer_size;
367 }
368
369 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
370 buffer = rb_entry(n, struct binder_buffer, rb_node);
371 buffer_size = binder_alloc_buffer_size(alloc, buffer);
372 free_buffers++;
373 total_free_size += buffer_size;
374 if (buffer_size > largest_free_size)
375 largest_free_size = buffer_size;
376 }
377
378 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
379 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
380 total_alloc_size, allocated_buffers,
381 largest_alloc_size, total_free_size,
382 free_buffers, largest_free_size);
383 }
384
debug_low_async_space_locked(struct binder_alloc * alloc)385 static bool debug_low_async_space_locked(struct binder_alloc *alloc)
386 {
387 /*
388 * Find the amount and size of buffers allocated by the current caller;
389 * The idea is that once we cross the threshold, whoever is responsible
390 * for the low async space is likely to try to send another async txn,
391 * and at some point we'll catch them in the act. This is more efficient
392 * than keeping a map per pid.
393 */
394 struct binder_buffer *buffer;
395 size_t total_alloc_size = 0;
396 int pid = current->tgid;
397 size_t num_buffers = 0;
398 struct rb_node *n;
399
400 /*
401 * Only start detecting spammers once we have less than 20% of async
402 * space left (which is less than 10% of total buffer size).
403 */
404 if (alloc->free_async_space >= alloc->buffer_size / 10) {
405 alloc->oneway_spam_detected = false;
406 return false;
407 }
408
409 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
410 n = rb_next(n)) {
411 buffer = rb_entry(n, struct binder_buffer, rb_node);
412 if (buffer->pid != pid)
413 continue;
414 if (!buffer->async_transaction)
415 continue;
416 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
417 num_buffers++;
418 }
419
420 /*
421 * Warn if this pid has more than 50 transactions, or more than 50% of
422 * async space (which is 25% of total buffer size). Oneway spam is only
423 * detected when the threshold is exceeded.
424 */
425 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
426 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
427 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
428 alloc->pid, pid, num_buffers, total_alloc_size);
429 if (!alloc->oneway_spam_detected) {
430 alloc->oneway_spam_detected = true;
431 return true;
432 }
433 }
434 return false;
435 }
436
437 /* Callers preallocate @new_buffer, it is freed by this function if unused */
binder_alloc_new_buf_locked(struct binder_alloc * alloc,struct binder_buffer * new_buffer,size_t size,int is_async)438 static struct binder_buffer *binder_alloc_new_buf_locked(
439 struct binder_alloc *alloc,
440 struct binder_buffer *new_buffer,
441 size_t size,
442 int is_async)
443 {
444 struct rb_node *n = alloc->free_buffers.rb_node;
445 struct rb_node *best_fit = NULL;
446 struct binder_buffer *buffer;
447 unsigned long next_used_page;
448 unsigned long curr_last_page;
449 size_t buffer_size;
450
451 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
452
453 if (is_async && alloc->free_async_space < size) {
454 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
455 "%d: binder_alloc_buf size %zd failed, no async space left\n",
456 alloc->pid, size);
457 buffer = ERR_PTR(-ENOSPC);
458 goto out;
459 }
460
461 while (n) {
462 buffer = rb_entry(n, struct binder_buffer, rb_node);
463 BUG_ON(!buffer->free);
464 buffer_size = binder_alloc_buffer_size(alloc, buffer);
465
466 if (size < buffer_size) {
467 best_fit = n;
468 n = n->rb_left;
469 } else if (size > buffer_size) {
470 n = n->rb_right;
471 } else {
472 best_fit = n;
473 break;
474 }
475 }
476
477 if (unlikely(!best_fit)) {
478 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
479 "%d: binder_alloc_buf size %zd failed, no address space\n",
480 alloc->pid, size);
481 debug_no_space_locked(alloc);
482 buffer = ERR_PTR(-ENOSPC);
483 goto out;
484 }
485
486 if (buffer_size != size) {
487 /* Found an oversized buffer and needs to be split */
488 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
489 buffer_size = binder_alloc_buffer_size(alloc, buffer);
490
491 WARN_ON(n || buffer_size == size);
492 new_buffer->user_data = buffer->user_data + size;
493 list_add(&new_buffer->entry, &buffer->entry);
494 new_buffer->free = 1;
495 binder_insert_free_buffer(alloc, new_buffer);
496 new_buffer = NULL;
497 }
498
499 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
500 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
501 alloc->pid, size, buffer, buffer_size);
502
503 /*
504 * Now we remove the pages from the freelist. A clever calculation
505 * with buffer_size determines if the last page is shared with an
506 * adjacent in-use buffer. In such case, the page has been already
507 * removed from the freelist so we trim our range short.
508 */
509 next_used_page = ((uintptr_t)buffer->user_data + buffer_size) & PAGE_MASK;
510 curr_last_page = PAGE_ALIGN((uintptr_t)buffer->user_data + size);
511 binder_lru_freelist_del(alloc, PAGE_ALIGN((uintptr_t)buffer->user_data),
512 min(next_used_page, curr_last_page));
513
514 rb_erase(&buffer->rb_node, &alloc->free_buffers);
515 buffer->free = 0;
516 buffer->allow_user_free = 0;
517 binder_insert_allocated_buffer_locked(alloc, buffer);
518 buffer->async_transaction = is_async;
519 buffer->oneway_spam_suspect = false;
520 if (is_async) {
521 alloc->free_async_space -= size;
522 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
523 "%d: binder_alloc_buf size %zd async free %zd\n",
524 alloc->pid, size, alloc->free_async_space);
525 if (debug_low_async_space_locked(alloc))
526 buffer->oneway_spam_suspect = true;
527 }
528
529 out:
530 /* Discard possibly unused new_buffer */
531 kfree(new_buffer);
532 return buffer;
533 }
534
535 /* Calculate the sanitized total size, returns 0 for invalid request */
sanitized_size(size_t data_size,size_t offsets_size,size_t extra_buffers_size)536 static inline size_t sanitized_size(size_t data_size,
537 size_t offsets_size,
538 size_t extra_buffers_size)
539 {
540 size_t total, tmp;
541
542 /* Align to pointer size and check for overflows */
543 tmp = ALIGN(data_size, sizeof(void *)) +
544 ALIGN(offsets_size, sizeof(void *));
545 if (tmp < data_size || tmp < offsets_size)
546 return 0;
547 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
548 if (total < tmp || total < extra_buffers_size)
549 return 0;
550
551 /* Pad 0-sized buffers so they get a unique address */
552 total = max(total, sizeof(void *));
553
554 return total;
555 }
556
557 /**
558 * binder_alloc_new_buf() - Allocate a new binder buffer
559 * @alloc: binder_alloc for this proc
560 * @data_size: size of user data buffer
561 * @offsets_size: user specified buffer offset
562 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
563 * @is_async: buffer for async transaction
564 *
565 * Allocate a new buffer given the requested sizes. Returns
566 * the kernel version of the buffer pointer. The size allocated
567 * is the sum of the three given sizes (each rounded up to
568 * pointer-sized boundary)
569 *
570 * Return: The allocated buffer or %ERR_PTR(-errno) if error
571 */
binder_alloc_new_buf(struct binder_alloc * alloc,size_t data_size,size_t offsets_size,size_t extra_buffers_size,int is_async)572 struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
573 size_t data_size,
574 size_t offsets_size,
575 size_t extra_buffers_size,
576 int is_async)
577 {
578 struct binder_buffer *buffer, *next;
579 size_t size;
580 int ret;
581
582 /* Check binder_alloc is fully initialized */
583 if (!binder_alloc_get_vma(alloc)) {
584 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
585 "%d: binder_alloc_buf, no vma\n",
586 alloc->pid);
587 return ERR_PTR(-ESRCH);
588 }
589
590 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
591 if (unlikely(!size)) {
592 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
593 "%d: got transaction with invalid size %zd-%zd-%zd\n",
594 alloc->pid, data_size, offsets_size,
595 extra_buffers_size);
596 return ERR_PTR(-EINVAL);
597 }
598
599 /* Preallocate the next buffer */
600 next = kzalloc(sizeof(*next), GFP_KERNEL);
601 if (!next)
602 return ERR_PTR(-ENOMEM);
603
604 binder_alloc_lock(alloc);
605 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
606 if (IS_ERR(buffer)) {
607 binder_alloc_unlock(alloc);
608 goto out;
609 }
610
611 buffer->data_size = data_size;
612 buffer->offsets_size = offsets_size;
613 buffer->extra_buffers_size = extra_buffers_size;
614 buffer->pid = current->tgid;
615 binder_alloc_unlock(alloc);
616
617 ret = binder_install_buffer_pages(alloc, buffer, size);
618 if (ret) {
619 binder_alloc_free_buf(alloc, buffer);
620 buffer = ERR_PTR(ret);
621 }
622 out:
623 return buffer;
624 }
625
buffer_start_page(struct binder_buffer * buffer)626 static unsigned long buffer_start_page(struct binder_buffer *buffer)
627 {
628 return (uintptr_t)buffer->user_data & PAGE_MASK;
629 }
630
prev_buffer_end_page(struct binder_buffer * buffer)631 static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
632 {
633 return ((uintptr_t)buffer->user_data - 1) & PAGE_MASK;
634 }
635
binder_delete_free_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer)636 static void binder_delete_free_buffer(struct binder_alloc *alloc,
637 struct binder_buffer *buffer)
638 {
639 struct binder_buffer *prev, *next;
640
641 if (PAGE_ALIGNED(buffer->user_data))
642 goto skip_freelist;
643
644 BUG_ON(alloc->buffers.next == &buffer->entry);
645 prev = binder_buffer_prev(buffer);
646 BUG_ON(!prev->free);
647 if (prev_buffer_end_page(prev) == buffer_start_page(buffer))
648 goto skip_freelist;
649
650 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
651 next = binder_buffer_next(buffer);
652 if (buffer_start_page(next) == buffer_start_page(buffer))
653 goto skip_freelist;
654 }
655
656 binder_lru_freelist_add(alloc, buffer_start_page(buffer),
657 buffer_start_page(buffer) + PAGE_SIZE);
658 skip_freelist:
659 list_del(&buffer->entry);
660 kfree(buffer);
661 }
662
binder_free_buf_locked(struct binder_alloc * alloc,struct binder_buffer * buffer)663 static void binder_free_buf_locked(struct binder_alloc *alloc,
664 struct binder_buffer *buffer)
665 {
666 size_t size, buffer_size;
667
668 buffer_size = binder_alloc_buffer_size(alloc, buffer);
669
670 size = ALIGN(buffer->data_size, sizeof(void *)) +
671 ALIGN(buffer->offsets_size, sizeof(void *)) +
672 ALIGN(buffer->extra_buffers_size, sizeof(void *));
673
674 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
675 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
676 alloc->pid, buffer, size, buffer_size);
677
678 BUG_ON(buffer->free);
679 BUG_ON(size > buffer_size);
680 BUG_ON(buffer->transaction != NULL);
681 BUG_ON(buffer->user_data < alloc->buffer);
682 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
683
684 if (buffer->async_transaction) {
685 alloc->free_async_space += buffer_size;
686 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
687 "%d: binder_free_buf size %zd async free %zd\n",
688 alloc->pid, size, alloc->free_async_space);
689 }
690
691 binder_lru_freelist_add(alloc, PAGE_ALIGN((uintptr_t)buffer->user_data),
692 ((uintptr_t)buffer->user_data + buffer_size) & PAGE_MASK);
693
694 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
695 buffer->free = 1;
696 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
697 struct binder_buffer *next = binder_buffer_next(buffer);
698
699 if (next->free) {
700 rb_erase(&next->rb_node, &alloc->free_buffers);
701 binder_delete_free_buffer(alloc, next);
702 }
703 }
704 if (alloc->buffers.next != &buffer->entry) {
705 struct binder_buffer *prev = binder_buffer_prev(buffer);
706
707 if (prev->free) {
708 binder_delete_free_buffer(alloc, buffer);
709 rb_erase(&prev->rb_node, &alloc->free_buffers);
710 buffer = prev;
711 }
712 }
713 binder_insert_free_buffer(alloc, buffer);
714 }
715
716 /**
717 * binder_alloc_get_page() - get kernel pointer for given buffer offset
718 * @alloc: binder_alloc for this proc
719 * @buffer: binder buffer to be accessed
720 * @buffer_offset: offset into @buffer data
721 * @pgoffp: address to copy final page offset to
722 *
723 * Lookup the struct page corresponding to the address
724 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
725 * NULL, the byte-offset into the page is written there.
726 *
727 * The caller is responsible to ensure that the offset points
728 * to a valid address within the @buffer and that @buffer is
729 * not freeable by the user. Since it can't be freed, we are
730 * guaranteed that the corresponding elements of @alloc->pages[]
731 * cannot change.
732 *
733 * Return: struct page
734 */
binder_alloc_get_page(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t buffer_offset,pgoff_t * pgoffp)735 static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
736 struct binder_buffer *buffer,
737 binder_size_t buffer_offset,
738 pgoff_t *pgoffp)
739 {
740 binder_size_t buffer_space_offset = buffer_offset +
741 (buffer->user_data - alloc->buffer);
742 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
743 size_t index = buffer_space_offset >> PAGE_SHIFT;
744 struct binder_lru_page *lru_page;
745
746 lru_page = &alloc->pages[index];
747 *pgoffp = pgoff;
748 return lru_page->page_ptr;
749 }
750
751 /**
752 * binder_alloc_clear_buf() - zero out buffer
753 * @alloc: binder_alloc for this proc
754 * @buffer: binder buffer to be cleared
755 *
756 * memset the given buffer to 0
757 */
binder_alloc_clear_buf(struct binder_alloc * alloc,struct binder_buffer * buffer)758 static void binder_alloc_clear_buf(struct binder_alloc *alloc,
759 struct binder_buffer *buffer)
760 {
761 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
762 binder_size_t buffer_offset = 0;
763
764 while (bytes) {
765 unsigned long size;
766 struct page *page;
767 pgoff_t pgoff;
768 void *kptr;
769
770 page = binder_alloc_get_page(alloc, buffer,
771 buffer_offset, &pgoff);
772 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
773 kptr = kmap(page) + pgoff;
774 memset(kptr, 0, size);
775 kunmap(page);
776 bytes -= size;
777 buffer_offset += size;
778 }
779 }
780
781
782 /**
783 * binder_alloc_free_buf() - free a binder buffer
784 * @alloc: binder_alloc for this proc
785 * @buffer: kernel pointer to buffer
786 *
787 * Free the buffer allocated via binder_alloc_new_buf()
788 */
binder_alloc_free_buf(struct binder_alloc * alloc,struct binder_buffer * buffer)789 void binder_alloc_free_buf(struct binder_alloc *alloc,
790 struct binder_buffer *buffer)
791 {
792 /*
793 * We could eliminate the call to binder_alloc_clear_buf()
794 * from binder_alloc_deferred_release() by moving this to
795 * binder_free_buf_locked(). However, that could
796 * increase contention for the alloc->lock if clear_on_free
797 * is used frequently for large buffers. This lock is not
798 * needed for correctness here.
799 */
800 if (buffer->clear_on_free) {
801 binder_alloc_clear_buf(alloc, buffer);
802 buffer->clear_on_free = false;
803 }
804 binder_alloc_lock(alloc);
805 binder_free_buf_locked(alloc, buffer);
806 binder_alloc_unlock(alloc);
807 }
808
809 /**
810 * binder_alloc_mmap_handler() - map virtual address space for proc
811 * @alloc: alloc structure for this proc
812 * @vma: vma passed to mmap()
813 *
814 * Called by binder_mmap() to initialize the space specified in
815 * vma for allocating binder buffers
816 *
817 * Return:
818 * 0 = success
819 * -EBUSY = address space already mapped
820 * -ENOMEM = failed to map memory to given address space
821 */
binder_alloc_mmap_handler(struct binder_alloc * alloc,struct vm_area_struct * vma)822 int binder_alloc_mmap_handler(struct binder_alloc *alloc,
823 struct vm_area_struct *vma)
824 {
825 struct binder_buffer *buffer;
826 const char *failure_string;
827 int ret, i;
828
829 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
830 ret = -EINVAL;
831 failure_string = "invalid vma->vm_mm";
832 goto err_invalid_mm;
833 }
834
835 mutex_lock(&binder_alloc_mmap_lock);
836 if (alloc->buffer_size) {
837 ret = -EBUSY;
838 failure_string = "already mapped";
839 goto err_already_mapped;
840 }
841 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
842 SZ_4M);
843 mutex_unlock(&binder_alloc_mmap_lock);
844
845 alloc->buffer = (void __user *)vma->vm_start;
846
847 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
848 sizeof(alloc->pages[0]),
849 GFP_KERNEL);
850 if (alloc->pages == NULL) {
851 ret = -ENOMEM;
852 failure_string = "alloc page array";
853 goto err_alloc_pages_failed;
854 }
855
856 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
857 alloc->pages[i].alloc = alloc;
858 INIT_LIST_HEAD(&alloc->pages[i].lru);
859 }
860
861 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
862 if (!buffer) {
863 ret = -ENOMEM;
864 failure_string = "alloc buffer struct";
865 goto err_alloc_buf_struct_failed;
866 }
867
868 buffer->user_data = alloc->buffer;
869 list_add(&buffer->entry, &alloc->buffers);
870 buffer->free = 1;
871 binder_insert_free_buffer(alloc, buffer);
872 alloc->free_async_space = alloc->buffer_size / 2;
873
874 /* Signal binder_alloc is fully initialized */
875 binder_alloc_set_vma(alloc, vma);
876
877 return 0;
878
879 err_alloc_buf_struct_failed:
880 kfree(alloc->pages);
881 alloc->pages = NULL;
882 err_alloc_pages_failed:
883 alloc->buffer = NULL;
884 mutex_lock(&binder_alloc_mmap_lock);
885 alloc->buffer_size = 0;
886 err_already_mapped:
887 mutex_unlock(&binder_alloc_mmap_lock);
888 err_invalid_mm:
889 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
890 "%s: %d %lx-%lx %s failed %d\n", __func__,
891 alloc->pid, vma->vm_start, vma->vm_end,
892 failure_string, ret);
893 return ret;
894 }
895
896
binder_alloc_deferred_release(struct binder_alloc * alloc)897 void binder_alloc_deferred_release(struct binder_alloc *alloc)
898 {
899 struct rb_node *n;
900 int buffers, page_count;
901 struct binder_buffer *buffer;
902
903 buffers = 0;
904 binder_alloc_lock(alloc);
905 BUG_ON(alloc->vma);
906
907 while ((n = rb_first(&alloc->allocated_buffers))) {
908 buffer = rb_entry(n, struct binder_buffer, rb_node);
909
910 /* Transaction should already have been freed */
911 BUG_ON(buffer->transaction);
912
913 if (buffer->clear_on_free) {
914 binder_alloc_clear_buf(alloc, buffer);
915 buffer->clear_on_free = false;
916 }
917 binder_free_buf_locked(alloc, buffer);
918 buffers++;
919 }
920
921 while (!list_empty(&alloc->buffers)) {
922 buffer = list_first_entry(&alloc->buffers,
923 struct binder_buffer, entry);
924 WARN_ON(!buffer->free);
925
926 list_del(&buffer->entry);
927 WARN_ON_ONCE(!list_empty(&alloc->buffers));
928 kfree(buffer);
929 }
930
931 page_count = 0;
932 if (alloc->pages) {
933 int i;
934
935 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
936 unsigned long page_addr;
937 bool on_lru;
938
939 if (!alloc->pages[i].page_ptr)
940 continue;
941
942 on_lru = list_lru_del(&binder_freelist,
943 &alloc->pages[i].lru);
944 page_addr = (uintptr_t)alloc->buffer + i * PAGE_SIZE;
945 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
946 "%s: %d: page %d %s\n",
947 __func__, alloc->pid, i,
948 on_lru ? "on lru" : "active");
949 __free_page(alloc->pages[i].page_ptr);
950 page_count++;
951 }
952 kfree(alloc->pages);
953 }
954 binder_alloc_unlock(alloc);
955 if (alloc->vma_vm_mm)
956 mmdrop(alloc->vma_vm_mm);
957
958 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
959 "%s: %d buffers %d, pages %d\n",
960 __func__, alloc->pid, buffers, page_count);
961 }
962
963 /**
964 * binder_alloc_print_allocated() - print buffer info
965 * @m: seq_file for output via seq_printf()
966 * @alloc: binder_alloc for this proc
967 *
968 * Prints information about every buffer associated with
969 * the binder_alloc state to the given seq_file
970 */
binder_alloc_print_allocated(struct seq_file * m,struct binder_alloc * alloc)971 void binder_alloc_print_allocated(struct seq_file *m,
972 struct binder_alloc *alloc)
973 {
974 struct binder_buffer *buffer;
975 struct rb_node *n;
976
977 binder_alloc_lock(alloc);
978 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
979 buffer = rb_entry(n, struct binder_buffer, rb_node);
980 seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n",
981 buffer->debug_id,
982 buffer->user_data - alloc->buffer,
983 buffer->data_size, buffer->offsets_size,
984 buffer->extra_buffers_size,
985 buffer->transaction ? "active" : "delivered");
986 }
987 binder_alloc_unlock(alloc);
988 }
989
990 /**
991 * binder_alloc_print_pages() - print page usage
992 * @m: seq_file for output via seq_printf()
993 * @alloc: binder_alloc for this proc
994 */
binder_alloc_print_pages(struct seq_file * m,struct binder_alloc * alloc)995 void binder_alloc_print_pages(struct seq_file *m,
996 struct binder_alloc *alloc)
997 {
998 struct binder_lru_page *page;
999 int i;
1000 int active = 0;
1001 int lru = 0;
1002 int free = 0;
1003
1004 binder_alloc_lock(alloc);
1005 /*
1006 * Make sure the binder_alloc is fully initialized, otherwise we might
1007 * read inconsistent state.
1008 */
1009 if (binder_alloc_get_vma(alloc) != NULL) {
1010 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
1011 page = &alloc->pages[i];
1012 if (!page->page_ptr)
1013 free++;
1014 else if (list_empty(&page->lru))
1015 active++;
1016 else
1017 lru++;
1018 }
1019 }
1020 binder_alloc_unlock(alloc);
1021 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
1022 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
1023 }
1024
1025 /**
1026 * binder_alloc_get_allocated_count() - return count of buffers
1027 * @alloc: binder_alloc for this proc
1028 *
1029 * Return: count of allocated buffers
1030 */
binder_alloc_get_allocated_count(struct binder_alloc * alloc)1031 int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1032 {
1033 struct rb_node *n;
1034 int count = 0;
1035
1036 binder_alloc_lock(alloc);
1037 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1038 count++;
1039 binder_alloc_unlock(alloc);
1040 return count;
1041 }
1042
1043
1044 /**
1045 * binder_alloc_vma_close() - invalidate address space
1046 * @alloc: binder_alloc for this proc
1047 *
1048 * Called from binder_vma_close() when releasing address space.
1049 * Clears alloc->vma to prevent new incoming transactions from
1050 * allocating more buffers.
1051 */
binder_alloc_vma_close(struct binder_alloc * alloc)1052 void binder_alloc_vma_close(struct binder_alloc *alloc)
1053 {
1054 binder_alloc_set_vma(alloc, NULL);
1055 }
1056
1057 /**
1058 * binder_alloc_free_page() - shrinker callback to free pages
1059 * @item: item to free
1060 * @lock: lock protecting the item
1061 * @cb_arg: callback argument
1062 *
1063 * Called from list_lru_walk() in binder_shrink_scan() to free
1064 * up pages when the system is under memory pressure.
1065 */
binder_alloc_free_page(struct list_head * item,struct list_lru_one * lru,spinlock_t * lock,void * cb_arg)1066 enum lru_status binder_alloc_free_page(struct list_head *item,
1067 struct list_lru_one *lru,
1068 spinlock_t *lock,
1069 void *cb_arg)
1070 __must_hold(lock)
1071 {
1072 struct binder_lru_page *page = container_of(item, typeof(*page), lru);
1073 struct binder_alloc *alloc = page->alloc;
1074 struct mm_struct *mm = alloc->vma_vm_mm;
1075 struct vm_area_struct *vma;
1076 struct page *page_to_free;
1077 unsigned long page_addr;
1078 size_t index;
1079
1080 if (!mmget_not_zero(mm))
1081 goto err_mmget;
1082 if (!mmap_read_trylock(mm))
1083 goto err_mmap_read_lock_failed;
1084 if (!binder_alloc_trylock(alloc))
1085 goto err_get_alloc_lock_failed;
1086 if (!page->page_ptr)
1087 goto err_page_already_freed;
1088
1089 index = page - alloc->pages;
1090 page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
1091
1092 vma = vma_lookup(mm, page_addr);
1093 if (vma && vma != binder_alloc_get_vma(alloc))
1094 goto err_invalid_vma;
1095
1096 trace_binder_unmap_kernel_start(alloc, index);
1097
1098 page_to_free = page->page_ptr;
1099 page->page_ptr = NULL;
1100
1101 trace_binder_unmap_kernel_end(alloc, index);
1102
1103 list_lru_isolate(lru, item);
1104 binder_alloc_unlock(alloc);
1105 spin_unlock(lock);
1106
1107 if (vma) {
1108 trace_binder_unmap_user_start(alloc, index);
1109
1110 zap_page_range(vma, page_addr, PAGE_SIZE);
1111
1112 trace_binder_unmap_user_end(alloc, index);
1113 }
1114
1115 mmap_read_unlock(mm);
1116 mmput_async(mm);
1117 __free_page(page_to_free);
1118
1119 spin_lock(lock);
1120 return LRU_REMOVED_RETRY;
1121
1122 err_invalid_vma:
1123 err_page_already_freed:
1124 binder_alloc_unlock(alloc);
1125 err_get_alloc_lock_failed:
1126 mmap_read_unlock(mm);
1127 err_mmap_read_lock_failed:
1128 mmput_async(mm);
1129 err_mmget:
1130 return LRU_SKIP;
1131 }
1132
1133 static unsigned long
binder_shrink_count(struct shrinker * shrink,struct shrink_control * sc)1134 binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1135 {
1136 return list_lru_count(&binder_freelist);
1137 }
1138
1139 static unsigned long
binder_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)1140 binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1141 {
1142 return list_lru_walk(&binder_freelist, binder_alloc_free_page,
1143 NULL, sc->nr_to_scan);
1144 }
1145
1146 static struct shrinker binder_shrinker = {
1147 .count_objects = binder_shrink_count,
1148 .scan_objects = binder_shrink_scan,
1149 .seeks = DEFAULT_SEEKS,
1150 };
1151
1152 /**
1153 * binder_alloc_init() - called by binder_open() for per-proc initialization
1154 * @alloc: binder_alloc for this proc
1155 *
1156 * Called from binder_open() to initialize binder_alloc fields for
1157 * new binder proc
1158 */
binder_alloc_init(struct binder_alloc * alloc)1159 void binder_alloc_init(struct binder_alloc *alloc)
1160 {
1161 alloc->pid = current->group_leader->pid;
1162 alloc->vma_vm_mm = current->mm;
1163 mmgrab(alloc->vma_vm_mm);
1164 binder_alloc_lock_init(alloc);
1165 INIT_LIST_HEAD(&alloc->buffers);
1166 }
1167
binder_alloc_shrinker_init(void)1168 int binder_alloc_shrinker_init(void)
1169 {
1170 int ret = list_lru_init(&binder_freelist);
1171
1172 if (ret == 0) {
1173 ret = register_shrinker(&binder_shrinker);
1174 if (ret)
1175 list_lru_destroy(&binder_freelist);
1176 }
1177 return ret;
1178 }
1179
binder_alloc_shrinker_exit(void)1180 void binder_alloc_shrinker_exit(void)
1181 {
1182 unregister_shrinker(&binder_shrinker);
1183 list_lru_destroy(&binder_freelist);
1184 }
1185
1186 /**
1187 * check_buffer() - verify that buffer/offset is safe to access
1188 * @alloc: binder_alloc for this proc
1189 * @buffer: binder buffer to be accessed
1190 * @offset: offset into @buffer data
1191 * @bytes: bytes to access from offset
1192 *
1193 * Check that the @offset/@bytes are within the size of the given
1194 * @buffer and that the buffer is currently active and not freeable.
1195 * Offsets must also be multiples of sizeof(u32). The kernel is
1196 * allowed to touch the buffer in two cases:
1197 *
1198 * 1) when the buffer is being created:
1199 * (buffer->free == 0 && buffer->allow_user_free == 0)
1200 * 2) when the buffer is being torn down:
1201 * (buffer->free == 0 && buffer->transaction == NULL).
1202 *
1203 * Return: true if the buffer is safe to access
1204 */
check_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t offset,size_t bytes)1205 static inline bool check_buffer(struct binder_alloc *alloc,
1206 struct binder_buffer *buffer,
1207 binder_size_t offset, size_t bytes)
1208 {
1209 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1210
1211 return buffer_size >= bytes &&
1212 offset <= buffer_size - bytes &&
1213 IS_ALIGNED(offset, sizeof(u32)) &&
1214 !buffer->free &&
1215 (!buffer->allow_user_free || !buffer->transaction);
1216 }
1217
1218 /**
1219 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1220 * @alloc: binder_alloc for this proc
1221 * @buffer: binder buffer to be accessed
1222 * @buffer_offset: offset into @buffer data
1223 * @from: userspace pointer to source buffer
1224 * @bytes: bytes to copy
1225 *
1226 * Copy bytes from source userspace to target buffer.
1227 *
1228 * Return: bytes remaining to be copied
1229 */
1230 unsigned long
binder_alloc_copy_user_to_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t buffer_offset,const void __user * from,size_t bytes)1231 binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1232 struct binder_buffer *buffer,
1233 binder_size_t buffer_offset,
1234 const void __user *from,
1235 size_t bytes)
1236 {
1237 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1238 return bytes;
1239
1240 while (bytes) {
1241 unsigned long size;
1242 unsigned long ret;
1243 struct page *page;
1244 pgoff_t pgoff;
1245 void *kptr;
1246
1247 page = binder_alloc_get_page(alloc, buffer,
1248 buffer_offset, &pgoff);
1249 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1250 kptr = kmap(page) + pgoff;
1251 ret = copy_from_user(kptr, from, size);
1252 kunmap(page);
1253 if (ret)
1254 return bytes - size + ret;
1255 bytes -= size;
1256 from += size;
1257 buffer_offset += size;
1258 }
1259 return 0;
1260 }
1261
binder_alloc_do_buffer_copy(struct binder_alloc * alloc,bool to_buffer,struct binder_buffer * buffer,binder_size_t buffer_offset,void * ptr,size_t bytes)1262 static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1263 bool to_buffer,
1264 struct binder_buffer *buffer,
1265 binder_size_t buffer_offset,
1266 void *ptr,
1267 size_t bytes)
1268 {
1269 /* All copies must be 32-bit aligned and 32-bit size */
1270 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1271 return -EINVAL;
1272
1273 while (bytes) {
1274 unsigned long size;
1275 struct page *page;
1276 pgoff_t pgoff;
1277 void *tmpptr;
1278 void *base_ptr;
1279
1280 page = binder_alloc_get_page(alloc, buffer,
1281 buffer_offset, &pgoff);
1282 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1283 base_ptr = kmap_atomic(page);
1284 tmpptr = base_ptr + pgoff;
1285 if (to_buffer)
1286 memcpy(tmpptr, ptr, size);
1287 else
1288 memcpy(ptr, tmpptr, size);
1289 /*
1290 * kunmap_atomic() takes care of flushing the cache
1291 * if this device has VIVT cache arch
1292 */
1293 kunmap_atomic(base_ptr);
1294 bytes -= size;
1295 pgoff = 0;
1296 ptr = ptr + size;
1297 buffer_offset += size;
1298 }
1299 return 0;
1300 }
1301
binder_alloc_copy_to_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t buffer_offset,void * src,size_t bytes)1302 int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1303 struct binder_buffer *buffer,
1304 binder_size_t buffer_offset,
1305 void *src,
1306 size_t bytes)
1307 {
1308 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1309 src, bytes);
1310 }
1311
binder_alloc_copy_from_buffer(struct binder_alloc * alloc,void * dest,struct binder_buffer * buffer,binder_size_t buffer_offset,size_t bytes)1312 int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1313 void *dest,
1314 struct binder_buffer *buffer,
1315 binder_size_t buffer_offset,
1316 size_t bytes)
1317 {
1318 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1319 dest, bytes);
1320 }
1321
1322