blob: e26aa84acd46e115c5af3e74da4a4d58efa12c70 [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>
Todd Kjos0c972a02017-06-29 12:01:41 -070026#include "binder_alloc.h"
27#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 Llamasc38a8982023-12-01 17:21:38 +0000138 if (user_ptr < buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700139 n = n->rb_left;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000140 } else if (user_ptr > 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
173 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700174 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700175 mutex_unlock(&alloc->mutex);
176 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
206 index = (page_addr - alloc->buffer) / PAGE_SIZE;
207 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) {
254 pr_err("%d: %s failed to insert page at %lx with %d\n",
255 alloc->pid, __func__, addr, ret);
256 __free_page(page);
257 ret = -ENOMEM;
258 goto out;
259 }
260
Carlos Llamas477e8e82023-12-01 17:21:48 +0000261 /* Mark page installation complete and safe to use */
262 binder_set_installed_page(lru_page, page);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000263out:
264 mmap_write_unlock(alloc->vma_vm_mm);
265 mmput_async(alloc->vma_vm_mm);
266 return ret;
267}
268
Carlos Llamas477e8e82023-12-01 17:21:48 +0000269static int binder_install_buffer_pages(struct binder_alloc *alloc,
270 struct binder_buffer *buffer,
271 size_t size)
272{
273 struct binder_lru_page *page;
274 unsigned long start, final;
275 unsigned long page_addr;
276
277 start = buffer->user_data & PAGE_MASK;
278 final = PAGE_ALIGN(buffer->user_data + size);
279
280 for (page_addr = start; page_addr < final; page_addr += PAGE_SIZE) {
281 unsigned long index;
282 int ret;
283
284 index = (page_addr - alloc->buffer) / PAGE_SIZE;
285 page = &alloc->pages[index];
286
287 if (binder_get_installed_page(page))
288 continue;
289
290 trace_binder_alloc_page_start(alloc, index);
291
292 ret = binder_install_single_page(alloc, page, page_addr);
293 if (ret)
294 return ret;
295
296 trace_binder_alloc_page_end(alloc, index);
297 }
298
299 return 0;
300}
301
302/* The range of pages should exclude those shared with other buffers */
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000303static void binder_lru_freelist_del(struct binder_alloc *alloc,
304 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700305{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000306 struct binder_lru_page *page;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000307 unsigned long page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700308
309 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000310 "%d: pages %lx-%lx\n",
Carlos Llamas0b243682023-12-01 17:21:39 +0000311 alloc->pid, start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700312
Carlos Llamas0b243682023-12-01 17:21:39 +0000313 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700314
315 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000316 unsigned long index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700317 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700318
Sherry Yange41e1642017-08-23 08:46:43 -0700319 index = (page_addr - alloc->buffer) / PAGE_SIZE;
320 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700321
Sherry Yangf2517eb2017-08-23 08:46:42 -0700322 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700323 trace_binder_alloc_lru_start(alloc, index);
324
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000325 on_lru = list_lru_del(&binder_freelist, &page->lru);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700326 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700327
328 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700329 continue;
330 }
331
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100332 if (index + 1 > alloc->pages_high)
333 alloc->pages_high = index + 1;
Todd Kjos0c972a02017-06-29 12:01:41 -0700334 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700335}
336
Minchan Kimda1b9562018-08-23 14:29:56 +0900337static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
338 struct vm_area_struct *vma)
339{
Carlos Llamasb094b042023-05-30 19:43:37 +0000340 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
341 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900342}
343
344static inline struct vm_area_struct *binder_alloc_get_vma(
345 struct binder_alloc *alloc)
346{
Carlos Llamasb094b042023-05-30 19:43:37 +0000347 /* pairs with smp_store_release in binder_alloc_set_vma() */
348 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900349}
350
Carlos Llamase1d195e2023-12-01 17:21:42 +0000351static void debug_no_space_locked(struct binder_alloc *alloc)
352{
353 size_t largest_alloc_size = 0;
354 struct binder_buffer *buffer;
355 size_t allocated_buffers = 0;
356 size_t largest_free_size = 0;
357 size_t total_alloc_size = 0;
358 size_t total_free_size = 0;
359 size_t free_buffers = 0;
360 size_t buffer_size;
361 struct rb_node *n;
362
363 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
364 buffer = rb_entry(n, struct binder_buffer, rb_node);
365 buffer_size = binder_alloc_buffer_size(alloc, buffer);
366 allocated_buffers++;
367 total_alloc_size += buffer_size;
368 if (buffer_size > largest_alloc_size)
369 largest_alloc_size = buffer_size;
370 }
371
372 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
373 buffer = rb_entry(n, struct binder_buffer, rb_node);
374 buffer_size = binder_alloc_buffer_size(alloc, buffer);
375 free_buffers++;
376 total_free_size += buffer_size;
377 if (buffer_size > largest_free_size)
378 largest_free_size = buffer_size;
379 }
380
381 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
382 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
383 total_alloc_size, allocated_buffers,
384 largest_alloc_size, total_free_size,
385 free_buffers, largest_free_size);
386}
387
Carlos Llamas26d06d92023-12-01 17:21:41 +0000388static bool debug_low_async_space_locked(struct binder_alloc *alloc)
Martijn Coenen261e7812020-08-21 14:25:44 +0200389{
390 /*
391 * Find the amount and size of buffers allocated by the current caller;
392 * The idea is that once we cross the threshold, whoever is responsible
393 * for the low async space is likely to try to send another async txn,
394 * and at some point we'll catch them in the act. This is more efficient
395 * than keeping a map per pid.
396 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200397 struct binder_buffer *buffer;
398 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000399 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200400 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000401 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200402
Carlos Llamas081ddad2023-12-01 17:21:43 +0000403 /*
404 * Only start detecting spammers once we have less than 20% of async
405 * space left (which is less than 10% of total buffer size).
406 */
407 if (alloc->free_async_space >= alloc->buffer_size / 10) {
408 alloc->oneway_spam_detected = false;
409 return false;
410 }
411
Martijn Coenen261e7812020-08-21 14:25:44 +0200412 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
413 n = rb_next(n)) {
414 buffer = rb_entry(n, struct binder_buffer, rb_node);
415 if (buffer->pid != pid)
416 continue;
417 if (!buffer->async_transaction)
418 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000419 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200420 num_buffers++;
421 }
422
423 /*
424 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800425 * async space (which is 25% of total buffer size). Oneway spam is only
426 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200427 */
428 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
429 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
430 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
431 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800432 if (!alloc->oneway_spam_detected) {
433 alloc->oneway_spam_detected = true;
434 return true;
435 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200436 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800437 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200438}
439
Carlos Llamasef524f42023-12-01 17:21:46 +0000440/* Callers preallocate @new_buffer, it is freed by this function if unused */
Xiongwei Song3f827242017-12-14 12:15:42 +0800441static struct binder_buffer *binder_alloc_new_buf_locked(
442 struct binder_alloc *alloc,
Carlos Llamasef524f42023-12-01 17:21:46 +0000443 struct binder_buffer *new_buffer,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000444 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000445 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700446{
447 struct rb_node *n = alloc->free_buffers.rb_node;
Todd Kjos0c972a02017-06-29 12:01:41 -0700448 struct rb_node *best_fit = NULL;
Carlos Llamasef524f42023-12-01 17:21:46 +0000449 struct binder_buffer *buffer;
Carlos Llamas683f84a2023-12-01 17:21:52 +0000450 unsigned long next_used_page;
451 unsigned long curr_last_page;
Carlos Llamasef524f42023-12-01 17:21:46 +0000452 size_t buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700453
Zhuguangqing1174e452021-03-09 15:47:43 +0800454 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000455
Carlos Llamas11ca0762023-12-01 17:21:34 +0000456 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700457 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
458 "%d: binder_alloc_buf size %zd failed, no async space left\n",
459 alloc->pid, size);
Carlos Llamasef524f42023-12-01 17:21:46 +0000460 buffer = ERR_PTR(-ENOSPC);
461 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700462 }
463
464 while (n) {
465 buffer = rb_entry(n, struct binder_buffer, rb_node);
466 BUG_ON(!buffer->free);
467 buffer_size = binder_alloc_buffer_size(alloc, buffer);
468
469 if (size < buffer_size) {
470 best_fit = n;
471 n = n->rb_left;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000472 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700473 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000474 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700475 best_fit = n;
476 break;
477 }
478 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000479
Carlos Llamase1d195e2023-12-01 17:21:42 +0000480 if (unlikely(!best_fit)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700481 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
482 "%d: binder_alloc_buf size %zd failed, no address space\n",
483 alloc->pid, size);
Carlos Llamase1d195e2023-12-01 17:21:42 +0000484 debug_no_space_locked(alloc);
Carlos Llamasef524f42023-12-01 17:21:46 +0000485 buffer = ERR_PTR(-ENOSPC);
486 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700487 }
Carlos Llamase1d195e2023-12-01 17:21:42 +0000488
Carlos Llamas356047f2023-12-01 17:21:50 +0000489 if (buffer_size != size) {
490 /* Found an oversized buffer and needs to be split */
Todd Kjos0c972a02017-06-29 12:01:41 -0700491 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
492 buffer_size = binder_alloc_buffer_size(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700493
Carlos Llamas356047f2023-12-01 17:21:50 +0000494 WARN_ON(n || buffer_size == size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000495 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700496 list_add(&new_buffer->entry, &buffer->entry);
497 new_buffer->free = 1;
498 binder_insert_free_buffer(alloc, new_buffer);
Carlos Llamasef524f42023-12-01 17:21:46 +0000499 new_buffer = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700500 }
Sherry Yang74310e02017-08-23 08:46:41 -0700501
Carlos Llamas356047f2023-12-01 17:21:50 +0000502 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
503 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
504 alloc->pid, size, buffer, buffer_size);
505
Carlos Llamas683f84a2023-12-01 17:21:52 +0000506 /*
507 * Now we remove the pages from the freelist. A clever calculation
508 * with buffer_size determines if the last page is shared with an
509 * adjacent in-use buffer. In such case, the page has been already
510 * removed from the freelist so we trim our range short.
511 */
512 next_used_page = (buffer->user_data + buffer_size) & PAGE_MASK;
513 curr_last_page = PAGE_ALIGN(buffer->user_data + size);
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000514 binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data),
Carlos Llamas683f84a2023-12-01 17:21:52 +0000515 min(next_used_page, curr_last_page));
Carlos Llamas356047f2023-12-01 17:21:50 +0000516
517 rb_erase(&buffer->rb_node, &alloc->free_buffers);
Sherry Yang74310e02017-08-23 08:46:41 -0700518 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800519 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700520 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700521 buffer->async_transaction = is_async;
Hang Lua7dc1e62021-04-09 17:40:46 +0800522 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700523 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000524 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700525 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
526 "%d: binder_alloc_buf size %zd async free %zd\n",
527 alloc->pid, size, alloc->free_async_space);
Carlos Llamas081ddad2023-12-01 17:21:43 +0000528 if (debug_low_async_space_locked(alloc))
529 buffer->oneway_spam_suspect = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700530 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000531
Carlos Llamasef524f42023-12-01 17:21:46 +0000532out:
533 /* Discard possibly unused new_buffer */
534 kfree(new_buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700535 return buffer;
536}
537
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000538/* Calculate the sanitized total size, returns 0 for invalid request */
539static inline size_t sanitized_size(size_t data_size,
540 size_t offsets_size,
541 size_t extra_buffers_size)
542{
543 size_t total, tmp;
544
545 /* Align to pointer size and check for overflows */
546 tmp = ALIGN(data_size, sizeof(void *)) +
547 ALIGN(offsets_size, sizeof(void *));
548 if (tmp < data_size || tmp < offsets_size)
549 return 0;
550 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
551 if (total < tmp || total < extra_buffers_size)
552 return 0;
553
554 /* Pad 0-sized buffers so they get a unique address */
555 total = max(total, sizeof(void *));
556
557 return total;
558}
559
Todd Kjos0c972a02017-06-29 12:01:41 -0700560/**
561 * binder_alloc_new_buf() - Allocate a new binder buffer
562 * @alloc: binder_alloc for this proc
563 * @data_size: size of user data buffer
564 * @offsets_size: user specified buffer offset
565 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
566 * @is_async: buffer for async transaction
567 *
568 * Allocate a new buffer given the requested sizes. Returns
569 * the kernel version of the buffer pointer. The size allocated
570 * is the sum of the three given sizes (each rounded up to
571 * pointer-sized boundary)
572 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000573 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700574 */
575struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
576 size_t data_size,
577 size_t offsets_size,
578 size_t extra_buffers_size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000579 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700580{
Carlos Llamasef524f42023-12-01 17:21:46 +0000581 struct binder_buffer *buffer, *next;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000582 size_t size;
Carlos Llamas477e8e82023-12-01 17:21:48 +0000583 int ret;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000584
585 /* Check binder_alloc is fully initialized */
586 if (!binder_alloc_get_vma(alloc)) {
587 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
588 "%d: binder_alloc_buf, no vma\n",
589 alloc->pid);
590 return ERR_PTR(-ESRCH);
591 }
592
593 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
594 if (unlikely(!size)) {
595 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
596 "%d: got transaction with invalid size %zd-%zd-%zd\n",
597 alloc->pid, data_size, offsets_size,
598 extra_buffers_size);
599 return ERR_PTR(-EINVAL);
600 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700601
Carlos Llamasef524f42023-12-01 17:21:46 +0000602 /* Preallocate the next buffer */
603 next = kzalloc(sizeof(*next), GFP_KERNEL);
604 if (!next)
605 return ERR_PTR(-ENOMEM);
606
Todd Kjos0c972a02017-06-29 12:01:41 -0700607 mutex_lock(&alloc->mutex);
Carlos Llamasef524f42023-12-01 17:21:46 +0000608 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000609 if (IS_ERR(buffer)) {
610 mutex_unlock(&alloc->mutex);
611 goto out;
612 }
613
614 buffer->data_size = data_size;
615 buffer->offsets_size = offsets_size;
616 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000617 buffer->pid = current->tgid;
Todd Kjos0c972a02017-06-29 12:01:41 -0700618 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000619
Carlos Llamas477e8e82023-12-01 17:21:48 +0000620 ret = binder_install_buffer_pages(alloc, buffer, size);
621 if (ret) {
622 binder_alloc_free_buf(alloc, buffer);
623 buffer = ERR_PTR(ret);
624 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000625out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700626 return buffer;
627}
628
Carlos Llamasc38a8982023-12-01 17:21:38 +0000629static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700630{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000631 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700632}
633
Carlos Llamasc38a8982023-12-01 17:21:38 +0000634static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700635{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000636 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700637}
638
639static void binder_delete_free_buffer(struct binder_alloc *alloc,
640 struct binder_buffer *buffer)
641{
642 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700643 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530644
Todd Kjos0c972a02017-06-29 12:01:41 -0700645 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700646 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700647 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700648 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
649 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700650 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000651 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800652 alloc->pid, buffer->user_data,
653 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700654 }
655
656 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700657 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700658 if (buffer_start_page(next) == buffer_start_page(buffer)) {
659 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700660 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000661 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700662 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800663 buffer->user_data,
664 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700665 }
666 }
Sherry Yang74310e02017-08-23 08:46:41 -0700667
Todd Kjosbde4a192019-02-08 10:35:20 -0800668 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700669 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000670 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800671 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700672 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700673 }
Sherry Yang74310e02017-08-23 08:46:41 -0700674
675 if (to_free) {
676 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000677 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800678 alloc->pid, buffer->user_data,
679 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000680 next ? next->user_data : 0);
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000681 binder_lru_freelist_add(alloc, buffer_start_page(buffer),
682 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700683 }
684 list_del(&buffer->entry);
685 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700686}
687
688static void binder_free_buf_locked(struct binder_alloc *alloc,
689 struct binder_buffer *buffer)
690{
691 size_t size, buffer_size;
692
693 buffer_size = binder_alloc_buffer_size(alloc, buffer);
694
695 size = ALIGN(buffer->data_size, sizeof(void *)) +
696 ALIGN(buffer->offsets_size, sizeof(void *)) +
697 ALIGN(buffer->extra_buffers_size, sizeof(void *));
698
699 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
700 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
701 alloc->pid, buffer, size, buffer_size);
702
703 BUG_ON(buffer->free);
704 BUG_ON(size > buffer_size);
705 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800706 BUG_ON(buffer->user_data < alloc->buffer);
707 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700708
709 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000710 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700711 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
712 "%d: binder_free_buf size %zd async free %zd\n",
713 alloc->pid, size, alloc->free_async_space);
714 }
715
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000716 binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data),
717 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700718
719 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
720 buffer->free = 1;
721 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700722 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700723
724 if (next->free) {
725 rb_erase(&next->rb_node, &alloc->free_buffers);
726 binder_delete_free_buffer(alloc, next);
727 }
728 }
729 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700730 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700731
732 if (prev->free) {
733 binder_delete_free_buffer(alloc, buffer);
734 rb_erase(&prev->rb_node, &alloc->free_buffers);
735 buffer = prev;
736 }
737 }
738 binder_insert_free_buffer(alloc, buffer);
739}
740
Carlos Llamas59e0d622023-12-01 17:21:44 +0000741/**
742 * binder_alloc_get_page() - get kernel pointer for given buffer offset
743 * @alloc: binder_alloc for this proc
744 * @buffer: binder buffer to be accessed
745 * @buffer_offset: offset into @buffer data
746 * @pgoffp: address to copy final page offset to
747 *
748 * Lookup the struct page corresponding to the address
749 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
750 * NULL, the byte-offset into the page is written there.
751 *
752 * The caller is responsible to ensure that the offset points
753 * to a valid address within the @buffer and that @buffer is
754 * not freeable by the user. Since it can't be freed, we are
755 * guaranteed that the corresponding elements of @alloc->pages[]
756 * cannot change.
757 *
758 * Return: struct page
759 */
760static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
761 struct binder_buffer *buffer,
762 binder_size_t buffer_offset,
763 pgoff_t *pgoffp)
764{
765 binder_size_t buffer_space_offset = buffer_offset +
766 (buffer->user_data - alloc->buffer);
767 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
768 size_t index = buffer_space_offset >> PAGE_SHIFT;
769 struct binder_lru_page *lru_page;
770
771 lru_page = &alloc->pages[index];
772 *pgoffp = pgoff;
773 return lru_page->page_ptr;
774}
775
776/**
777 * binder_alloc_clear_buf() - zero out buffer
778 * @alloc: binder_alloc for this proc
779 * @buffer: binder buffer to be cleared
780 *
781 * memset the given buffer to 0
782 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800783static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000784 struct binder_buffer *buffer)
785{
786 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
787 binder_size_t buffer_offset = 0;
788
789 while (bytes) {
790 unsigned long size;
791 struct page *page;
792 pgoff_t pgoff;
793 void *kptr;
794
795 page = binder_alloc_get_page(alloc, buffer,
796 buffer_offset, &pgoff);
797 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
798 kptr = kmap(page) + pgoff;
799 memset(kptr, 0, size);
800 kunmap(page);
801 bytes -= size;
802 buffer_offset += size;
803 }
804}
805
806
Todd Kjos0c972a02017-06-29 12:01:41 -0700807/**
808 * binder_alloc_free_buf() - free a binder buffer
809 * @alloc: binder_alloc for this proc
810 * @buffer: kernel pointer to buffer
811 *
YangHui4b463822020-08-18 09:34:04 +0800812 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700813 */
814void binder_alloc_free_buf(struct binder_alloc *alloc,
815 struct binder_buffer *buffer)
816{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800817 /*
818 * We could eliminate the call to binder_alloc_clear_buf()
819 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000820 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800821 * increase contention for the alloc mutex if clear_on_free
822 * is used frequently for large buffers. The mutex is not
823 * needed for correctness here.
824 */
825 if (buffer->clear_on_free) {
826 binder_alloc_clear_buf(alloc, buffer);
827 buffer->clear_on_free = false;
828 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700829 mutex_lock(&alloc->mutex);
830 binder_free_buf_locked(alloc, buffer);
831 mutex_unlock(&alloc->mutex);
832}
833
834/**
835 * binder_alloc_mmap_handler() - map virtual address space for proc
836 * @alloc: alloc structure for this proc
837 * @vma: vma passed to mmap()
838 *
839 * Called by binder_mmap() to initialize the space specified in
840 * vma for allocating binder buffers
841 *
842 * Return:
843 * 0 = success
844 * -EBUSY = address space already mapped
845 * -ENOMEM = failed to map memory to given address space
846 */
847int binder_alloc_mmap_handler(struct binder_alloc *alloc,
848 struct vm_area_struct *vma)
849{
Todd Kjos0c972a02017-06-29 12:01:41 -0700850 struct binder_buffer *buffer;
Carlos Llamasaf711932023-12-01 17:21:47 +0000851 const char *failure_string;
852 int ret, i;
Todd Kjos0c972a02017-06-29 12:01:41 -0700853
Carlos Llamasd276fb42022-11-04 23:12:35 +0000854 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
855 ret = -EINVAL;
856 failure_string = "invalid vma->vm_mm";
857 goto err_invalid_mm;
858 }
859
Todd Kjos0c972a02017-06-29 12:01:41 -0700860 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200861 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700862 ret = -EBUSY;
863 failure_string = "already mapped";
864 goto err_already_mapped;
865 }
Jann Horn45d02f72019-10-16 17:01:18 +0200866 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
867 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200868 mutex_unlock(&binder_alloc_mmap_lock);
869
Carlos Llamasc38a8982023-12-01 17:21:38 +0000870 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200871
Jann Horn45d02f72019-10-16 17:01:18 +0200872 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700873 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700874 GFP_KERNEL);
875 if (alloc->pages == NULL) {
876 ret = -ENOMEM;
877 failure_string = "alloc page array";
878 goto err_alloc_pages_failed;
879 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700880
Carlos Llamasaf711932023-12-01 17:21:47 +0000881 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
882 alloc->pages[i].alloc = alloc;
883 INIT_LIST_HEAD(&alloc->pages[i].lru);
884 }
885
Sherry Yang74310e02017-08-23 08:46:41 -0700886 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
887 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700888 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700889 failure_string = "alloc buffer struct";
890 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700891 }
Sherry Yang74310e02017-08-23 08:46:41 -0700892
Todd Kjosbde4a192019-02-08 10:35:20 -0800893 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700894 list_add(&buffer->entry, &alloc->buffers);
895 buffer->free = 1;
896 binder_insert_free_buffer(alloc, buffer);
897 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000898
899 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900900 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700901
902 return 0;
903
Sherry Yang74310e02017-08-23 08:46:41 -0700904err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700905 kfree(alloc->pages);
906 alloc->pages = NULL;
907err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000908 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200909 mutex_lock(&binder_alloc_mmap_lock);
910 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700911err_already_mapped:
912 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000913err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700914 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
915 "%s: %d %lx-%lx %s failed %d\n", __func__,
916 alloc->pid, vma->vm_start, vma->vm_end,
917 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700918 return ret;
919}
920
921
922void binder_alloc_deferred_release(struct binder_alloc *alloc)
923{
924 struct rb_node *n;
925 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700926 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700927
Todd Kjos0c972a02017-06-29 12:01:41 -0700928 buffers = 0;
929 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000930 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900931
Todd Kjos0c972a02017-06-29 12:01:41 -0700932 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700933 buffer = rb_entry(n, struct binder_buffer, rb_node);
934
935 /* Transaction should already have been freed */
936 BUG_ON(buffer->transaction);
937
Todd Kjos0f966cb2020-11-20 15:37:43 -0800938 if (buffer->clear_on_free) {
939 binder_alloc_clear_buf(alloc, buffer);
940 buffer->clear_on_free = false;
941 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700942 binder_free_buf_locked(alloc, buffer);
943 buffers++;
944 }
945
Sherry Yang74310e02017-08-23 08:46:41 -0700946 while (!list_empty(&alloc->buffers)) {
947 buffer = list_first_entry(&alloc->buffers,
948 struct binder_buffer, entry);
949 WARN_ON(!buffer->free);
950
951 list_del(&buffer->entry);
952 WARN_ON_ONCE(!list_empty(&alloc->buffers));
953 kfree(buffer);
954 }
955
Todd Kjos0c972a02017-06-29 12:01:41 -0700956 page_count = 0;
957 if (alloc->pages) {
958 int i;
959
960 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000961 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700962 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700963
Sherry Yangf2517eb2017-08-23 08:46:42 -0700964 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700965 continue;
966
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000967 on_lru = list_lru_del(&binder_freelist,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700968 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700969 page_addr = alloc->buffer + i * PAGE_SIZE;
970 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000971 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700972 __func__, alloc->pid, i, page_addr,
973 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700974 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700975 page_count++;
976 }
977 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700978 }
979 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400980 if (alloc->vma_vm_mm)
981 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700982
983 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
984 "%s: %d buffers %d, pages %d\n",
985 __func__, alloc->pid, buffers, page_count);
986}
987
988static void print_binder_buffer(struct seq_file *m, const char *prefix,
989 struct binder_buffer *buffer)
990{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000991 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800992 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700993 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700994 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700995 buffer->transaction ? "active" : "delivered");
996}
997
998/**
999 * binder_alloc_print_allocated() - print buffer info
1000 * @m: seq_file for output via seq_printf()
1001 * @alloc: binder_alloc for this proc
1002 *
1003 * Prints information about every buffer associated with
1004 * the binder_alloc state to the given seq_file
1005 */
1006void binder_alloc_print_allocated(struct seq_file *m,
1007 struct binder_alloc *alloc)
1008{
1009 struct rb_node *n;
1010
1011 mutex_lock(&alloc->mutex);
1012 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1013 print_binder_buffer(m, " buffer",
1014 rb_entry(n, struct binder_buffer, rb_node));
1015 mutex_unlock(&alloc->mutex);
1016}
1017
1018/**
Sherry Yang8ef46652017-08-31 11:56:36 -07001019 * binder_alloc_print_pages() - print page usage
1020 * @m: seq_file for output via seq_printf()
1021 * @alloc: binder_alloc for this proc
1022 */
1023void binder_alloc_print_pages(struct seq_file *m,
1024 struct binder_alloc *alloc)
1025{
1026 struct binder_lru_page *page;
1027 int i;
1028 int active = 0;
1029 int lru = 0;
1030 int free = 0;
1031
1032 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +02001033 /*
1034 * Make sure the binder_alloc is fully initialized, otherwise we might
1035 * read inconsistent state.
1036 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +00001037 if (binder_alloc_get_vma(alloc) != NULL) {
1038 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
1039 page = &alloc->pages[i];
1040 if (!page->page_ptr)
1041 free++;
1042 else if (list_empty(&page->lru))
1043 active++;
1044 else
1045 lru++;
1046 }
Sherry Yang8ef46652017-08-31 11:56:36 -07001047 }
1048 mutex_unlock(&alloc->mutex);
1049 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001050 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001051}
1052
1053/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001054 * binder_alloc_get_allocated_count() - return count of buffers
1055 * @alloc: binder_alloc for this proc
1056 *
1057 * Return: count of allocated buffers
1058 */
1059int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1060{
1061 struct rb_node *n;
1062 int count = 0;
1063
1064 mutex_lock(&alloc->mutex);
1065 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1066 count++;
1067 mutex_unlock(&alloc->mutex);
1068 return count;
1069}
1070
1071
1072/**
1073 * binder_alloc_vma_close() - invalidate address space
1074 * @alloc: binder_alloc for this proc
1075 *
1076 * Called from binder_vma_close() when releasing address space.
1077 * Clears alloc->vma to prevent new incoming transactions from
1078 * allocating more buffers.
1079 */
1080void binder_alloc_vma_close(struct binder_alloc *alloc)
1081{
Minchan Kimda1b9562018-08-23 14:29:56 +09001082 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001083}
1084
1085/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001086 * binder_alloc_free_page() - shrinker callback to free pages
1087 * @item: item to free
1088 * @lock: lock protecting the item
1089 * @cb_arg: callback argument
1090 *
1091 * Called from list_lru_walk() in binder_shrink_scan() to free
1092 * up pages when the system is under memory pressure.
1093 */
1094enum lru_status binder_alloc_free_page(struct list_head *item,
1095 struct list_lru_one *lru,
1096 spinlock_t *lock,
1097 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001098 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001099{
1100 struct mm_struct *mm = NULL;
1101 struct binder_lru_page *page = container_of(item,
1102 struct binder_lru_page,
1103 lru);
1104 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001105 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001106 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -07001107 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001108
1109 alloc = page->alloc;
1110 if (!mutex_trylock(&alloc->mutex))
1111 goto err_get_alloc_mutex_failed;
1112
1113 if (!page->page_ptr)
1114 goto err_page_already_freed;
1115
1116 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001117 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001118
1119 mm = alloc->vma_vm_mm;
1120 if (!mmget_not_zero(mm))
1121 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001122 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001123 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001124 vma = vma_lookup(mm, page_addr);
1125 if (vma && vma != binder_alloc_get_vma(alloc))
1126 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001127
Sherry Yanga1b22892017-10-03 16:15:00 -07001128 list_lru_isolate(lru, item);
1129 spin_unlock(lock);
1130
1131 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001132 trace_binder_unmap_user_start(alloc, index);
1133
Todd Kjosc41358a2019-02-08 10:35:19 -08001134 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001135
Sherry Yange41e1642017-08-23 08:46:43 -07001136 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001137 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001138 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001139 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001140
Sherry Yange41e1642017-08-23 08:46:43 -07001141 trace_binder_unmap_kernel_start(alloc, index);
1142
Sherry Yangf2517eb2017-08-23 08:46:42 -07001143 __free_page(page->page_ptr);
1144 page->page_ptr = NULL;
1145
Sherry Yange41e1642017-08-23 08:46:43 -07001146 trace_binder_unmap_kernel_end(alloc, index);
1147
Sherry Yanga1b22892017-10-03 16:15:00 -07001148 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001149 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001150 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001151
Carlos Llamas8dce2882023-12-01 17:21:31 +00001152err_invalid_vma:
1153 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001154err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001155 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001156err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001157err_page_already_freed:
1158 mutex_unlock(&alloc->mutex);
1159err_get_alloc_mutex_failed:
1160 return LRU_SKIP;
1161}
1162
1163static unsigned long
1164binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1165{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001166 return list_lru_count(&binder_freelist);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001167}
1168
1169static unsigned long
1170binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1171{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001172 return list_lru_walk(&binder_freelist, binder_alloc_free_page,
Sherry Yangf2517eb2017-08-23 08:46:42 -07001173 NULL, sc->nr_to_scan);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001174}
1175
Sherry Yangde7bbe32017-10-06 16:12:05 -04001176static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001177 .count_objects = binder_shrink_count,
1178 .scan_objects = binder_shrink_scan,
1179 .seeks = DEFAULT_SEEKS,
1180};
1181
1182/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001183 * binder_alloc_init() - called by binder_open() for per-proc initialization
1184 * @alloc: binder_alloc for this proc
1185 *
1186 * Called from binder_open() to initialize binder_alloc fields for
1187 * new binder proc
1188 */
1189void binder_alloc_init(struct binder_alloc *alloc)
1190{
Todd Kjos0c972a02017-06-29 12:01:41 -07001191 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001192 alloc->vma_vm_mm = current->mm;
1193 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001194 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001195 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001196}
1197
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001198int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001199{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001200 int ret = list_lru_init(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001201
1202 if (ret == 0) {
1203 ret = register_shrinker(&binder_shrinker);
1204 if (ret)
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001205 list_lru_destroy(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001206 }
1207 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001208}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001209
Qi Zheng03eebad2023-06-25 15:49:37 +00001210void binder_alloc_shrinker_exit(void)
1211{
1212 unregister_shrinker(&binder_shrinker);
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001213 list_lru_destroy(&binder_freelist);
Qi Zheng03eebad2023-06-25 15:49:37 +00001214}
1215
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001216/**
1217 * check_buffer() - verify that buffer/offset is safe to access
1218 * @alloc: binder_alloc for this proc
1219 * @buffer: binder buffer to be accessed
1220 * @offset: offset into @buffer data
1221 * @bytes: bytes to access from offset
1222 *
1223 * Check that the @offset/@bytes are within the size of the given
1224 * @buffer and that the buffer is currently active and not freeable.
1225 * Offsets must also be multiples of sizeof(u32). The kernel is
1226 * allowed to touch the buffer in two cases:
1227 *
1228 * 1) when the buffer is being created:
1229 * (buffer->free == 0 && buffer->allow_user_free == 0)
1230 * 2) when the buffer is being torn down:
1231 * (buffer->free == 0 && buffer->transaction == NULL).
1232 *
1233 * Return: true if the buffer is safe to access
1234 */
1235static inline bool check_buffer(struct binder_alloc *alloc,
1236 struct binder_buffer *buffer,
1237 binder_size_t offset, size_t bytes)
1238{
1239 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1240
1241 return buffer_size >= bytes &&
1242 offset <= buffer_size - bytes &&
1243 IS_ALIGNED(offset, sizeof(u32)) &&
1244 !buffer->free &&
1245 (!buffer->allow_user_free || !buffer->transaction);
1246}
1247
1248/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001249 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1250 * @alloc: binder_alloc for this proc
1251 * @buffer: binder buffer to be accessed
1252 * @buffer_offset: offset into @buffer data
1253 * @from: userspace pointer to source buffer
1254 * @bytes: bytes to copy
1255 *
1256 * Copy bytes from source userspace to target buffer.
1257 *
1258 * Return: bytes remaining to be copied
1259 */
1260unsigned long
1261binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1262 struct binder_buffer *buffer,
1263 binder_size_t buffer_offset,
1264 const void __user *from,
1265 size_t bytes)
1266{
1267 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1268 return bytes;
1269
1270 while (bytes) {
1271 unsigned long size;
1272 unsigned long ret;
1273 struct page *page;
1274 pgoff_t pgoff;
1275 void *kptr;
1276
1277 page = binder_alloc_get_page(alloc, buffer,
1278 buffer_offset, &pgoff);
1279 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1280 kptr = kmap(page) + pgoff;
1281 ret = copy_from_user(kptr, from, size);
1282 kunmap(page);
1283 if (ret)
1284 return bytes - size + ret;
1285 bytes -= size;
1286 from += size;
1287 buffer_offset += size;
1288 }
1289 return 0;
1290}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001291
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001292static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1293 bool to_buffer,
1294 struct binder_buffer *buffer,
1295 binder_size_t buffer_offset,
1296 void *ptr,
1297 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001298{
1299 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001300 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1301 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001302
1303 while (bytes) {
1304 unsigned long size;
1305 struct page *page;
1306 pgoff_t pgoff;
1307 void *tmpptr;
1308 void *base_ptr;
1309
1310 page = binder_alloc_get_page(alloc, buffer,
1311 buffer_offset, &pgoff);
1312 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1313 base_ptr = kmap_atomic(page);
1314 tmpptr = base_ptr + pgoff;
1315 if (to_buffer)
1316 memcpy(tmpptr, ptr, size);
1317 else
1318 memcpy(ptr, tmpptr, size);
1319 /*
1320 * kunmap_atomic() takes care of flushing the cache
1321 * if this device has VIVT cache arch
1322 */
1323 kunmap_atomic(base_ptr);
1324 bytes -= size;
1325 pgoff = 0;
1326 ptr = ptr + size;
1327 buffer_offset += size;
1328 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001329 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001330}
1331
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001332int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1333 struct binder_buffer *buffer,
1334 binder_size_t buffer_offset,
1335 void *src,
1336 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001337{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001338 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1339 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001340}
1341
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001342int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1343 void *dest,
1344 struct binder_buffer *buffer,
1345 binder_size_t buffer_offset,
1346 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001347{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001348 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1349 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001350}
1351