blob: ecbca2aaada83cf7da4a56f8714999415edd877b [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Todd Kjos0c972a02017-06-29 12:01:41 -07002/* binder_alloc.c
3 *
4 * Android IPC Subsystem
5 *
6 * Copyright (C) 2007-2017 Google, Inc.
Todd Kjos0c972a02017-06-29 12:01:41 -07007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Todd Kjos0c972a02017-06-29 12:01:41 -070011#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>
Sherry Yangf2517eb2017-08-23 08:46:42 -070020#include <linux/list_lru.h>
Sherry Yang128f3802018-08-07 12:57:13 -070021#include <linux/ratelimit.h>
Guenter Roeck1e81c572018-07-23 14:47:23 -070022#include <asm/cacheflush.h>
Todd Kjos1a7c3d92019-02-08 10:35:14 -080023#include <linux/uaccess.h>
24#include <linux/highmem.h>
Jann Horn45d02f72019-10-16 17:01:18 +020025#include <linux/sizes.h>
Carlos Llamas3cd67282023-12-05 03:08:33 +000026#include "binder_internal.h"
Todd Kjos0c972a02017-06-29 12:01:41 -070027#include "binder_trace.h"
Zhuguangqing1174e452021-03-09 15:47:43 +080028#include <trace/hooks/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070029
Carlos Llamas4c82cba2023-12-01 17:21:51 +000030struct list_lru binder_freelist;
Sherry Yangf2517eb2017-08-23 08:46:42 -070031
Todd Kjos0c972a02017-06-29 12:01:41 -070032static DEFINE_MUTEX(binder_alloc_mmap_lock);
33
34enum {
Sherry Yang128f3802018-08-07 12:57:13 -070035 BINDER_DEBUG_USER_ERROR = 1U << 0,
Todd Kjos0c972a02017-06-29 12:01:41 -070036 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
37 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
38 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
39};
Sherry Yang128f3802018-08-07 12:57:13 -070040static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
Todd Kjos0c972a02017-06-29 12:01:41 -070041
42module_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) \
Sherry Yang128f3802018-08-07 12:57:13 -070048 pr_info_ratelimited(x); \
Todd Kjos0c972a02017-06-29 12:01:41 -070049 } while (0)
50
Sherry Yange21762192017-08-23 08:46:39 -070051static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
52{
53 return list_entry(buffer->entry.next, struct binder_buffer, entry);
54}
55
56static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
57{
58 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
59}
60
Todd Kjos0c972a02017-06-29 12:01:41 -070061static 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))
Todd Kjosbde4a192019-02-08 10:35:20 -080065 return alloc->buffer + alloc->buffer_size - buffer->user_data;
66 return binder_buffer_next(buffer)->user_data - buffer->user_data;
Todd Kjos0c972a02017-06-29 12:01:41 -070067}
68
69static 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
102static 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
Todd Kjosbde4a192019-02-08 10:35:20 -0800116 if (new_buffer->user_data < buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700117 p = &parent->rb_left;
Todd Kjosbde4a192019-02-08 10:35:20 -0800118 else if (new_buffer->user_data > buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700119 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
Todd Kjos53d311cf2017-06-29 12:01:51 -0700127static struct binder_buffer *binder_alloc_prepare_to_free_locked(
Todd Kjos0c972a02017-06-29 12:01:41 -0700128 struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000129 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700130{
131 struct rb_node *n = alloc->allocated_buffers.rb_node;
132 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700133
134 while (n) {
135 buffer = rb_entry(n, struct binder_buffer, rb_node);
136 BUG_ON(buffer->free);
137
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000138 if (user_ptr < (uintptr_t)buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700139 n = n->rb_left;
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000140 } else if (user_ptr > (uintptr_t)buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700141 n = n->rb_right;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000142 } else {
Todd Kjos53d311cf2017-06-29 12:01:51 -0700143 /*
144 * Guard against user threads attempting to
Todd Kjos7bada552018-11-06 15:55:32 -0800145 * free the buffer when in use by kernel or
146 * after it's already been freed.
Todd Kjos53d311cf2017-06-29 12:01:51 -0700147 */
Todd Kjos7bada552018-11-06 15:55:32 -0800148 if (!buffer->allow_user_free)
149 return ERR_PTR(-EPERM);
150 buffer->allow_user_free = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700151 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700152 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700153 }
154 return NULL;
155}
156
157/**
Joel Fernandes (Google)5dc54a02019-09-30 16:12:50 -0400158 * binder_alloc_prepare_to_free() - get buffer given user ptr
Todd Kjos0c972a02017-06-29 12:01:41 -0700159 * @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 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700168struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000169 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700170{
171 struct binder_buffer *buffer;
172
Carlos Llamas3cd67282023-12-05 03:08:33 +0000173 binder_alloc_lock(alloc);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700174 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Carlos Llamas3cd67282023-12-05 03:08:33 +0000175 binder_alloc_unlock(alloc);
Todd Kjos0c972a02017-06-29 12:01:41 -0700176 return buffer;
177}
178
Carlos Llamas477e8e82023-12-01 17:21:48 +0000179static inline void
180binder_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
187static inline struct page *
188binder_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
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000194static void binder_lru_freelist_add(struct binder_alloc *alloc,
195 unsigned long start, unsigned long end)
Carlos Llamas0b243682023-12-01 17:21:39 +0000196{
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
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000206 index = (page_addr - (uintptr_t)alloc->buffer) / PAGE_SIZE;
Carlos Llamas0b243682023-12-01 17:21:39 +0000207 page = &alloc->pages[index];
208
Carlos Llamas477e8e82023-12-01 17:21:48 +0000209 if (!binder_get_installed_page(page))
210 continue;
211
Carlos Llamas0b243682023-12-01 17:21:39 +0000212 trace_binder_free_lru_start(alloc, index);
213
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000214 ret = list_lru_add(&binder_freelist, &page->lru);
Carlos Llamas0b243682023-12-01 17:21:39 +0000215 WARN_ON(!ret);
216
217 trace_binder_free_lru_end(alloc, index);
218 }
219}
220
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000221static 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
Carlos Llamas477e8e82023-12-01 17:21:48 +0000231 /*
232 * Protected with mmap_sem in write mode as multiple tasks
233 * might race to install the same page.
234 */
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000235 mmap_write_lock(alloc->vma_vm_mm);
Carlos Llamas477e8e82023-12-01 17:21:48 +0000236 if (binder_get_installed_page(lru_page))
237 goto out;
238
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000239 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) {
Carlos Llamasf0667c82023-12-01 17:21:55 +0000254 pr_err("%d: %s failed to insert page at offset %lx with %d\n",
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000255 alloc->pid, __func__, addr - (uintptr_t)alloc->buffer,
256 ret);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000257 __free_page(page);
258 ret = -ENOMEM;
259 goto out;
260 }
261
Carlos Llamas477e8e82023-12-01 17:21:48 +0000262 /* Mark page installation complete and safe to use */
263 binder_set_installed_page(lru_page, page);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000264out:
265 mmap_write_unlock(alloc->vma_vm_mm);
266 mmput_async(alloc->vma_vm_mm);
267 return ret;
268}
269
Carlos Llamas477e8e82023-12-01 17:21:48 +0000270static 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
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000278 start = (uintptr_t)buffer->user_data & PAGE_MASK;
279 final = PAGE_ALIGN((uintptr_t)buffer->user_data + size);
Carlos Llamas477e8e82023-12-01 17:21:48 +0000280
281 for (page_addr = start; page_addr < final; page_addr += PAGE_SIZE) {
282 unsigned long index;
283 int ret;
284
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000285 index = (page_addr - (uintptr_t)alloc->buffer) / PAGE_SIZE;
Carlos Llamas477e8e82023-12-01 17:21:48 +0000286 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 */
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000304static void binder_lru_freelist_del(struct binder_alloc *alloc,
305 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700306{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000307 struct binder_lru_page *page;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000308 unsigned long page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700309
Carlos Llamas0b243682023-12-01 17:21:39 +0000310 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700311
312 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000313 unsigned long index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700314 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700315
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000316 index = (page_addr - (uintptr_t)alloc->buffer) / PAGE_SIZE;
Sherry Yange41e1642017-08-23 08:46:43 -0700317 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700318
Sherry Yangf2517eb2017-08-23 08:46:42 -0700319 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700320 trace_binder_alloc_lru_start(alloc, index);
321
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000322 on_lru = list_lru_del(&binder_freelist, &page->lru);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700323 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700324
325 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700326 continue;
327 }
328
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100329 if (index + 1 > alloc->pages_high)
330 alloc->pages_high = index + 1;
Todd Kjos0c972a02017-06-29 12:01:41 -0700331 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700332}
333
Minchan Kimda1b9562018-08-23 14:29:56 +0900334static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
335 struct vm_area_struct *vma)
336{
Carlos Llamasb094b042023-05-30 19:43:37 +0000337 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
338 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900339}
340
341static inline struct vm_area_struct *binder_alloc_get_vma(
342 struct binder_alloc *alloc)
343{
Carlos Llamasb094b042023-05-30 19:43:37 +0000344 /* pairs with smp_store_release in binder_alloc_set_vma() */
345 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900346}
347
Carlos Llamase1d195e2023-12-01 17:21:42 +0000348static 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
Carlos Llamas26d06d92023-12-01 17:21:41 +0000385static bool debug_low_async_space_locked(struct binder_alloc *alloc)
Martijn Coenen261e7812020-08-21 14:25:44 +0200386{
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 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200394 struct binder_buffer *buffer;
395 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000396 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200397 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000398 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200399
Carlos Llamas081ddad2023-12-01 17:21:43 +0000400 /*
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
Martijn Coenen261e7812020-08-21 14:25:44 +0200409 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;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000416 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200417 num_buffers++;
418 }
419
420 /*
421 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800422 * async space (which is 25% of total buffer size). Oneway spam is only
423 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200424 */
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);
Hang Lua7dc1e62021-04-09 17:40:46 +0800429 if (!alloc->oneway_spam_detected) {
430 alloc->oneway_spam_detected = true;
431 return true;
432 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200433 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800434 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200435}
436
Carlos Llamasef524f42023-12-01 17:21:46 +0000437/* Callers preallocate @new_buffer, it is freed by this function if unused */
Xiongwei Song3f827242017-12-14 12:15:42 +0800438static struct binder_buffer *binder_alloc_new_buf_locked(
439 struct binder_alloc *alloc,
Carlos Llamasef524f42023-12-01 17:21:46 +0000440 struct binder_buffer *new_buffer,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000441 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000442 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700443{
444 struct rb_node *n = alloc->free_buffers.rb_node;
Todd Kjos0c972a02017-06-29 12:01:41 -0700445 struct rb_node *best_fit = NULL;
Carlos Llamasef524f42023-12-01 17:21:46 +0000446 struct binder_buffer *buffer;
Carlos Llamas683f84a2023-12-01 17:21:52 +0000447 unsigned long next_used_page;
448 unsigned long curr_last_page;
Carlos Llamasef524f42023-12-01 17:21:46 +0000449 size_t buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700450
Zhuguangqing1174e452021-03-09 15:47:43 +0800451 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000452
Carlos Llamas11ca0762023-12-01 17:21:34 +0000453 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700454 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
455 "%d: binder_alloc_buf size %zd failed, no async space left\n",
456 alloc->pid, size);
Carlos Llamasef524f42023-12-01 17:21:46 +0000457 buffer = ERR_PTR(-ENOSPC);
458 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700459 }
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;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000469 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700470 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000471 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700472 best_fit = n;
473 break;
474 }
475 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000476
Carlos Llamase1d195e2023-12-01 17:21:42 +0000477 if (unlikely(!best_fit)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700478 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
479 "%d: binder_alloc_buf size %zd failed, no address space\n",
480 alloc->pid, size);
Carlos Llamase1d195e2023-12-01 17:21:42 +0000481 debug_no_space_locked(alloc);
Carlos Llamasef524f42023-12-01 17:21:46 +0000482 buffer = ERR_PTR(-ENOSPC);
483 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700484 }
Carlos Llamase1d195e2023-12-01 17:21:42 +0000485
Carlos Llamas356047f2023-12-01 17:21:50 +0000486 if (buffer_size != size) {
487 /* Found an oversized buffer and needs to be split */
Todd Kjos0c972a02017-06-29 12:01:41 -0700488 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
489 buffer_size = binder_alloc_buffer_size(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700490
Carlos Llamas356047f2023-12-01 17:21:50 +0000491 WARN_ON(n || buffer_size == size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000492 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700493 list_add(&new_buffer->entry, &buffer->entry);
494 new_buffer->free = 1;
495 binder_insert_free_buffer(alloc, new_buffer);
Carlos Llamasef524f42023-12-01 17:21:46 +0000496 new_buffer = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700497 }
Sherry Yang74310e02017-08-23 08:46:41 -0700498
Carlos Llamas356047f2023-12-01 17:21:50 +0000499 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
Carlos Llamas683f84a2023-12-01 17:21:52 +0000503 /*
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 */
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000509 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),
Carlos Llamas683f84a2023-12-01 17:21:52 +0000512 min(next_used_page, curr_last_page));
Carlos Llamas356047f2023-12-01 17:21:50 +0000513
514 rb_erase(&buffer->rb_node, &alloc->free_buffers);
Sherry Yang74310e02017-08-23 08:46:41 -0700515 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800516 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700517 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700518 buffer->async_transaction = is_async;
Hang Lua7dc1e62021-04-09 17:40:46 +0800519 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700520 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000521 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700522 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);
Carlos Llamas081ddad2023-12-01 17:21:43 +0000525 if (debug_low_async_space_locked(alloc))
526 buffer->oneway_spam_suspect = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700527 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000528
Carlos Llamasef524f42023-12-01 17:21:46 +0000529out:
530 /* Discard possibly unused new_buffer */
531 kfree(new_buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700532 return buffer;
533}
534
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000535/* Calculate the sanitized total size, returns 0 for invalid request */
536static 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
Todd Kjos0c972a02017-06-29 12:01:41 -0700557/**
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 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000570 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700571 */
572struct 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,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000576 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700577{
Carlos Llamasef524f42023-12-01 17:21:46 +0000578 struct binder_buffer *buffer, *next;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000579 size_t size;
Carlos Llamas477e8e82023-12-01 17:21:48 +0000580 int ret;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000581
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 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700598
Carlos Llamasef524f42023-12-01 17:21:46 +0000599 /* Preallocate the next buffer */
600 next = kzalloc(sizeof(*next), GFP_KERNEL);
601 if (!next)
602 return ERR_PTR(-ENOMEM);
603
Carlos Llamas3cd67282023-12-05 03:08:33 +0000604 binder_alloc_lock(alloc);
Carlos Llamasef524f42023-12-01 17:21:46 +0000605 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000606 if (IS_ERR(buffer)) {
Carlos Llamas3cd67282023-12-05 03:08:33 +0000607 binder_alloc_unlock(alloc);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000608 goto out;
609 }
610
611 buffer->data_size = data_size;
612 buffer->offsets_size = offsets_size;
613 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000614 buffer->pid = current->tgid;
Carlos Llamas3cd67282023-12-05 03:08:33 +0000615 binder_alloc_unlock(alloc);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000616
Carlos Llamas477e8e82023-12-01 17:21:48 +0000617 ret = binder_install_buffer_pages(alloc, buffer, size);
618 if (ret) {
619 binder_alloc_free_buf(alloc, buffer);
620 buffer = ERR_PTR(ret);
621 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000622out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700623 return buffer;
624}
625
Carlos Llamasc38a8982023-12-01 17:21:38 +0000626static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700627{
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000628 return (uintptr_t)buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700629}
630
Carlos Llamasc38a8982023-12-01 17:21:38 +0000631static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700632{
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000633 return ((uintptr_t)buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700634}
635
636static void binder_delete_free_buffer(struct binder_alloc *alloc,
637 struct binder_buffer *buffer)
638{
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000639 struct binder_buffer *prev, *next;
640
641 if (PAGE_ALIGNED(buffer->user_data))
642 goto skip_freelist;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530643
Todd Kjos0c972a02017-06-29 12:01:41 -0700644 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700645 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700646 BUG_ON(!prev->free);
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000647 if (prev_buffer_end_page(prev) == buffer_start_page(buffer))
648 goto skip_freelist;
Todd Kjos0c972a02017-06-29 12:01:41 -0700649
650 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700651 next = binder_buffer_next(buffer);
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000652 if (buffer_start_page(next) == buffer_start_page(buffer))
653 goto skip_freelist;
Todd Kjos0c972a02017-06-29 12:01:41 -0700654 }
Sherry Yang74310e02017-08-23 08:46:41 -0700655
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000656 binder_lru_freelist_add(alloc, buffer_start_page(buffer),
657 buffer_start_page(buffer) + PAGE_SIZE);
658skip_freelist:
Sherry Yang74310e02017-08-23 08:46:41 -0700659 list_del(&buffer->entry);
660 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700661}
662
663static 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);
Todd Kjosbde4a192019-02-08 10:35:20 -0800681 BUG_ON(buffer->user_data < alloc->buffer);
682 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700683
684 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000685 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700686 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
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000691 binder_lru_freelist_add(alloc, PAGE_ALIGN((uintptr_t)buffer->user_data),
692 ((uintptr_t)buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700693
694 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
695 buffer->free = 1;
696 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700697 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700698
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) {
Sherry Yange21762192017-08-23 08:46:39 -0700705 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700706
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
Carlos Llamas59e0d622023-12-01 17:21:44 +0000716/**
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 */
735static 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 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800758static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000759 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
Todd Kjos0c972a02017-06-29 12:01:41 -0700782/**
783 * binder_alloc_free_buf() - free a binder buffer
784 * @alloc: binder_alloc for this proc
785 * @buffer: kernel pointer to buffer
786 *
YangHui4b463822020-08-18 09:34:04 +0800787 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700788 */
789void binder_alloc_free_buf(struct binder_alloc *alloc,
790 struct binder_buffer *buffer)
791{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800792 /*
793 * We could eliminate the call to binder_alloc_clear_buf()
794 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000795 * binder_free_buf_locked(). However, that could
Carlos Llamas74ecd992023-12-01 17:21:57 +0000796 * increase contention for the alloc->lock if clear_on_free
797 * is used frequently for large buffers. This lock is not
Todd Kjos0f966cb2020-11-20 15:37:43 -0800798 * 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 }
Carlos Llamas3cd67282023-12-05 03:08:33 +0000804 binder_alloc_lock(alloc);
Todd Kjos0c972a02017-06-29 12:01:41 -0700805 binder_free_buf_locked(alloc, buffer);
Carlos Llamas3cd67282023-12-05 03:08:33 +0000806 binder_alloc_unlock(alloc);
Todd Kjos0c972a02017-06-29 12:01:41 -0700807}
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 */
822int binder_alloc_mmap_handler(struct binder_alloc *alloc,
823 struct vm_area_struct *vma)
824{
Todd Kjos0c972a02017-06-29 12:01:41 -0700825 struct binder_buffer *buffer;
Carlos Llamasaf711932023-12-01 17:21:47 +0000826 const char *failure_string;
827 int ret, i;
Todd Kjos0c972a02017-06-29 12:01:41 -0700828
Carlos Llamasd276fb42022-11-04 23:12:35 +0000829 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
Todd Kjos0c972a02017-06-29 12:01:41 -0700835 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200836 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700837 ret = -EBUSY;
838 failure_string = "already mapped";
839 goto err_already_mapped;
840 }
Jann Horn45d02f72019-10-16 17:01:18 +0200841 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
842 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200843 mutex_unlock(&binder_alloc_mmap_lock);
844
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000845 alloc->buffer = (void __user *)vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200846
Jann Horn45d02f72019-10-16 17:01:18 +0200847 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700848 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700849 GFP_KERNEL);
850 if (alloc->pages == NULL) {
851 ret = -ENOMEM;
852 failure_string = "alloc page array";
853 goto err_alloc_pages_failed;
854 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700855
Carlos Llamasaf711932023-12-01 17:21:47 +0000856 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
Sherry Yang74310e02017-08-23 08:46:41 -0700861 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
862 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700863 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700864 failure_string = "alloc buffer struct";
865 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700866 }
Sherry Yang74310e02017-08-23 08:46:41 -0700867
Todd Kjosbde4a192019-02-08 10:35:20 -0800868 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700869 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;
Carlos Llamasb094b042023-05-30 19:43:37 +0000873
874 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900875 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700876
877 return 0;
878
Sherry Yang74310e02017-08-23 08:46:41 -0700879err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700880 kfree(alloc->pages);
881 alloc->pages = NULL;
882err_alloc_pages_failed:
Carlos Llamas67121842024-02-01 23:11:53 +0000883 alloc->buffer = NULL;
Jann Horna7a74d72019-10-18 22:56:30 +0200884 mutex_lock(&binder_alloc_mmap_lock);
885 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700886err_already_mapped:
887 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000888err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700889 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);
Todd Kjos0c972a02017-06-29 12:01:41 -0700893 return ret;
894}
895
896
897void binder_alloc_deferred_release(struct binder_alloc *alloc)
898{
899 struct rb_node *n;
900 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700901 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700902
Todd Kjos0c972a02017-06-29 12:01:41 -0700903 buffers = 0;
Carlos Llamas3cd67282023-12-05 03:08:33 +0000904 binder_alloc_lock(alloc);
Carlos Llamasacd81932023-05-30 19:43:36 +0000905 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900906
Todd Kjos0c972a02017-06-29 12:01:41 -0700907 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700908 buffer = rb_entry(n, struct binder_buffer, rb_node);
909
910 /* Transaction should already have been freed */
911 BUG_ON(buffer->transaction);
912
Todd Kjos0f966cb2020-11-20 15:37:43 -0800913 if (buffer->clear_on_free) {
914 binder_alloc_clear_buf(alloc, buffer);
915 buffer->clear_on_free = false;
916 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700917 binder_free_buf_locked(alloc, buffer);
918 buffers++;
919 }
920
Sherry Yang74310e02017-08-23 08:46:41 -0700921 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
Todd Kjos0c972a02017-06-29 12:01:41 -0700931 page_count = 0;
932 if (alloc->pages) {
933 int i;
934
935 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000936 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700937 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700938
Sherry Yangf2517eb2017-08-23 08:46:42 -0700939 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700940 continue;
941
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000942 on_lru = list_lru_del(&binder_freelist,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700943 &alloc->pages[i].lru);
Carlos Llamas63f7ddea2023-12-05 02:41:13 +0000944 page_addr = (uintptr_t)alloc->buffer + i * PAGE_SIZE;
Todd Kjos0c972a02017-06-29 12:01:41 -0700945 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasf0667c82023-12-01 17:21:55 +0000946 "%s: %d: page %d %s\n",
947 __func__, alloc->pid, i,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700948 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700949 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700950 page_count++;
951 }
952 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700953 }
Carlos Llamas3cd67282023-12-05 03:08:33 +0000954 binder_alloc_unlock(alloc);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400955 if (alloc->vma_vm_mm)
956 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700957
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
Todd Kjos0c972a02017-06-29 12:01:41 -0700963/**
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 */
971void binder_alloc_print_allocated(struct seq_file *m,
972 struct binder_alloc *alloc)
973{
Carlos Llamasf6b1c042023-12-01 17:21:53 +0000974 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700975 struct rb_node *n;
976
Carlos Llamas3cd67282023-12-05 03:08:33 +0000977 binder_alloc_lock(alloc);
Carlos Llamasf6b1c042023-12-01 17:21:53 +0000978 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
979 buffer = rb_entry(n, struct binder_buffer, rb_node);
Carlos Llamasfb512ea2024-05-17 17:06:20 +0000980 seq_printf(m, " buffer %d: %tx size %zd:%zd:%zd %s\n",
Carlos Llamasf0667c82023-12-01 17:21:55 +0000981 buffer->debug_id,
982 buffer->user_data - alloc->buffer,
Carlos Llamasf6b1c042023-12-01 17:21:53 +0000983 buffer->data_size, buffer->offsets_size,
984 buffer->extra_buffers_size,
985 buffer->transaction ? "active" : "delivered");
986 }
Carlos Llamas3cd67282023-12-05 03:08:33 +0000987 binder_alloc_unlock(alloc);
Todd Kjos0c972a02017-06-29 12:01:41 -0700988}
989
990/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700991 * binder_alloc_print_pages() - print page usage
992 * @m: seq_file for output via seq_printf()
993 * @alloc: binder_alloc for this proc
994 */
995void 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
Carlos Llamas3cd67282023-12-05 03:08:33 +00001004 binder_alloc_lock(alloc);
Jann Horn8eb52a12019-10-18 22:56:29 +02001005 /*
1006 * Make sure the binder_alloc is fully initialized, otherwise we might
1007 * read inconsistent state.
1008 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +00001009 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 }
Sherry Yang8ef46652017-08-31 11:56:36 -07001019 }
Carlos Llamas3cd67282023-12-05 03:08:33 +00001020 binder_alloc_unlock(alloc);
Sherry Yang8ef46652017-08-31 11:56:36 -07001021 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001022 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001023}
1024
1025/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001026 * binder_alloc_get_allocated_count() - return count of buffers
1027 * @alloc: binder_alloc for this proc
1028 *
1029 * Return: count of allocated buffers
1030 */
1031int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1032{
1033 struct rb_node *n;
1034 int count = 0;
1035
Carlos Llamas3cd67282023-12-05 03:08:33 +00001036 binder_alloc_lock(alloc);
Todd Kjos0c972a02017-06-29 12:01:41 -07001037 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1038 count++;
Carlos Llamas3cd67282023-12-05 03:08:33 +00001039 binder_alloc_unlock(alloc);
Todd Kjos0c972a02017-06-29 12:01:41 -07001040 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 */
1052void binder_alloc_vma_close(struct binder_alloc *alloc)
1053{
Minchan Kimda1b9562018-08-23 14:29:56 +09001054 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001055}
1056
1057/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001058 * 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 */
1066enum lru_status binder_alloc_free_page(struct list_head *item,
1067 struct list_lru_one *lru,
1068 spinlock_t *lock,
1069 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001070 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001071{
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001072 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;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001077 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001078 size_t index;
1079
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001080 if (!mmget_not_zero(mm))
1081 goto err_mmget;
1082 if (!mmap_read_trylock(mm))
1083 goto err_mmap_read_lock_failed;
Carlos Llamas3cd67282023-12-05 03:08:33 +00001084 if (!binder_alloc_trylock(alloc))
Carlos Llamas74ecd992023-12-01 17:21:57 +00001085 goto err_get_alloc_lock_failed;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001086 if (!page->page_ptr)
1087 goto err_page_already_freed;
1088
1089 index = page - alloc->pages;
Carlos Llamas63f7ddea2023-12-05 02:41:13 +00001090 page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001091
Carlos Llamas8dce2882023-12-01 17:21:31 +00001092 vma = vma_lookup(mm, page_addr);
1093 if (vma && vma != binder_alloc_get_vma(alloc))
1094 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001095
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001096 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
Sherry Yanga1b22892017-10-03 16:15:00 -07001103 list_lru_isolate(lru, item);
Carlos Llamas3cd67282023-12-05 03:08:33 +00001104 binder_alloc_unlock(alloc);
Sherry Yanga1b22892017-10-03 16:15:00 -07001105 spin_unlock(lock);
1106
1107 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001108 trace_binder_unmap_user_start(alloc, index);
1109
Todd Kjosc41358a2019-02-08 10:35:19 -08001110 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001111
Sherry Yange41e1642017-08-23 08:46:43 -07001112 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001113 }
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001114
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001115 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001116 mmput_async(mm);
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001117 __free_page(page_to_free);
Sherry Yange41e1642017-08-23 08:46:43 -07001118
Sherry Yanga1b22892017-10-03 16:15:00 -07001119 spin_lock(lock);
Sherry Yanga1b22892017-10-03 16:15:00 -07001120 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001121
Carlos Llamas8dce2882023-12-01 17:21:31 +00001122err_invalid_vma:
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001123err_page_already_freed:
Carlos Llamas3cd67282023-12-05 03:08:33 +00001124 binder_alloc_unlock(alloc);
Carlos Llamas74ecd992023-12-01 17:21:57 +00001125err_get_alloc_lock_failed:
Carlos Llamas8dce2882023-12-01 17:21:31 +00001126 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001127err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001128 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001129err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001130 return LRU_SKIP;
1131}
1132
1133static unsigned long
1134binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1135{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001136 return list_lru_count(&binder_freelist);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001137}
1138
1139static unsigned long
1140binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1141{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001142 return list_lru_walk(&binder_freelist, binder_alloc_free_page,
Sherry Yangf2517eb2017-08-23 08:46:42 -07001143 NULL, sc->nr_to_scan);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001144}
1145
Sherry Yangde7bbe32017-10-06 16:12:05 -04001146static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001147 .count_objects = binder_shrink_count,
1148 .scan_objects = binder_shrink_scan,
1149 .seeks = DEFAULT_SEEKS,
1150};
1151
1152/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001153 * 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 */
1159void binder_alloc_init(struct binder_alloc *alloc)
1160{
Todd Kjos0c972a02017-06-29 12:01:41 -07001161 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001162 alloc->vma_vm_mm = current->mm;
1163 mmgrab(alloc->vma_vm_mm);
Carlos Llamas3cd67282023-12-05 03:08:33 +00001164 binder_alloc_lock_init(alloc);
Sherry Yang957ccc22017-08-31 10:26:06 -07001165 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001166}
1167
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001168int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001169{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001170 int ret = list_lru_init(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001171
1172 if (ret == 0) {
1173 ret = register_shrinker(&binder_shrinker);
1174 if (ret)
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001175 list_lru_destroy(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001176 }
1177 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001178}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001179
Qi Zheng03eebad2023-06-25 15:49:37 +00001180void binder_alloc_shrinker_exit(void)
1181{
1182 unregister_shrinker(&binder_shrinker);
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001183 list_lru_destroy(&binder_freelist);
Qi Zheng03eebad2023-06-25 15:49:37 +00001184}
1185
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001186/**
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 */
1205static 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/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001219 * 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 */
1230unsigned long
1231binder_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);
Fabio M. De Francescoe38087f2022-04-25 19:57:53 +02001250 kptr = kmap_local_page(page) + pgoff;
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001251 ret = copy_from_user(kptr, from, size);
Fabio M. De Francescoe38087f2022-04-25 19:57:53 +02001252 kunmap_local(kptr);
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001253 if (ret)
1254 return bytes - size + ret;
1255 bytes -= size;
1256 from += size;
1257 buffer_offset += size;
1258 }
1259 return 0;
1260}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001261
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001262static 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)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001268{
1269 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001270 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1271 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001272
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 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001299 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001300}
1301
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001302int 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)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001307{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001308 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1309 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001310}
1311
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001312int 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)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001317{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001318 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1319 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001320}
1321