blob: adfdf7584ff8c7859efb1d6ab20fc5bd4d37d6b9 [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 Llamasc38a8982023-12-01 17:21:38 +0000450 unsigned long has_page_addr;
451 unsigned long end_page_addr;
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
506 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
507 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
508 if (end_page_addr > has_page_addr)
509 end_page_addr = has_page_addr;
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000510 binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data),
511 end_page_addr);
Carlos Llamas356047f2023-12-01 17:21:50 +0000512
513 rb_erase(&buffer->rb_node, &alloc->free_buffers);
Sherry Yang74310e02017-08-23 08:46:41 -0700514 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800515 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700516 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700517 buffer->async_transaction = is_async;
Hang Lua7dc1e62021-04-09 17:40:46 +0800518 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700519 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000520 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700521 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
522 "%d: binder_alloc_buf size %zd async free %zd\n",
523 alloc->pid, size, alloc->free_async_space);
Carlos Llamas081ddad2023-12-01 17:21:43 +0000524 if (debug_low_async_space_locked(alloc))
525 buffer->oneway_spam_suspect = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700526 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000527
Carlos Llamasef524f42023-12-01 17:21:46 +0000528out:
529 /* Discard possibly unused new_buffer */
530 kfree(new_buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700531 return buffer;
532}
533
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000534/* Calculate the sanitized total size, returns 0 for invalid request */
535static inline size_t sanitized_size(size_t data_size,
536 size_t offsets_size,
537 size_t extra_buffers_size)
538{
539 size_t total, tmp;
540
541 /* Align to pointer size and check for overflows */
542 tmp = ALIGN(data_size, sizeof(void *)) +
543 ALIGN(offsets_size, sizeof(void *));
544 if (tmp < data_size || tmp < offsets_size)
545 return 0;
546 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
547 if (total < tmp || total < extra_buffers_size)
548 return 0;
549
550 /* Pad 0-sized buffers so they get a unique address */
551 total = max(total, sizeof(void *));
552
553 return total;
554}
555
Todd Kjos0c972a02017-06-29 12:01:41 -0700556/**
557 * binder_alloc_new_buf() - Allocate a new binder buffer
558 * @alloc: binder_alloc for this proc
559 * @data_size: size of user data buffer
560 * @offsets_size: user specified buffer offset
561 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
562 * @is_async: buffer for async transaction
563 *
564 * Allocate a new buffer given the requested sizes. Returns
565 * the kernel version of the buffer pointer. The size allocated
566 * is the sum of the three given sizes (each rounded up to
567 * pointer-sized boundary)
568 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000569 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700570 */
571struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
572 size_t data_size,
573 size_t offsets_size,
574 size_t extra_buffers_size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000575 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700576{
Carlos Llamasef524f42023-12-01 17:21:46 +0000577 struct binder_buffer *buffer, *next;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000578 size_t size;
Carlos Llamas477e8e82023-12-01 17:21:48 +0000579 int ret;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000580
581 /* Check binder_alloc is fully initialized */
582 if (!binder_alloc_get_vma(alloc)) {
583 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
584 "%d: binder_alloc_buf, no vma\n",
585 alloc->pid);
586 return ERR_PTR(-ESRCH);
587 }
588
589 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
590 if (unlikely(!size)) {
591 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
592 "%d: got transaction with invalid size %zd-%zd-%zd\n",
593 alloc->pid, data_size, offsets_size,
594 extra_buffers_size);
595 return ERR_PTR(-EINVAL);
596 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700597
Carlos Llamasef524f42023-12-01 17:21:46 +0000598 /* Preallocate the next buffer */
599 next = kzalloc(sizeof(*next), GFP_KERNEL);
600 if (!next)
601 return ERR_PTR(-ENOMEM);
602
Todd Kjos0c972a02017-06-29 12:01:41 -0700603 mutex_lock(&alloc->mutex);
Carlos Llamasef524f42023-12-01 17:21:46 +0000604 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000605 if (IS_ERR(buffer)) {
606 mutex_unlock(&alloc->mutex);
607 goto out;
608 }
609
610 buffer->data_size = data_size;
611 buffer->offsets_size = offsets_size;
612 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000613 buffer->pid = current->tgid;
Todd Kjos0c972a02017-06-29 12:01:41 -0700614 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000615
Carlos Llamas477e8e82023-12-01 17:21:48 +0000616 ret = binder_install_buffer_pages(alloc, buffer, size);
617 if (ret) {
618 binder_alloc_free_buf(alloc, buffer);
619 buffer = ERR_PTR(ret);
620 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000621out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700622 return buffer;
623}
624
Carlos Llamasc38a8982023-12-01 17:21:38 +0000625static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700626{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000627 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700628}
629
Carlos Llamasc38a8982023-12-01 17:21:38 +0000630static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700631{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000632 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700633}
634
635static void binder_delete_free_buffer(struct binder_alloc *alloc,
636 struct binder_buffer *buffer)
637{
638 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700639 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530640
Todd Kjos0c972a02017-06-29 12:01:41 -0700641 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700642 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700643 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700644 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
645 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700646 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000647 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800648 alloc->pid, buffer->user_data,
649 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700650 }
651
652 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700653 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700654 if (buffer_start_page(next) == buffer_start_page(buffer)) {
655 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700656 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000657 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700658 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800659 buffer->user_data,
660 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700661 }
662 }
Sherry Yang74310e02017-08-23 08:46:41 -0700663
Todd Kjosbde4a192019-02-08 10:35:20 -0800664 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700665 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000666 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800667 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700668 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700669 }
Sherry Yang74310e02017-08-23 08:46:41 -0700670
671 if (to_free) {
672 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000673 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800674 alloc->pid, buffer->user_data,
675 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000676 next ? next->user_data : 0);
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000677 binder_lru_freelist_add(alloc, buffer_start_page(buffer),
678 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700679 }
680 list_del(&buffer->entry);
681 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700682}
683
684static void binder_free_buf_locked(struct binder_alloc *alloc,
685 struct binder_buffer *buffer)
686{
687 size_t size, buffer_size;
688
689 buffer_size = binder_alloc_buffer_size(alloc, buffer);
690
691 size = ALIGN(buffer->data_size, sizeof(void *)) +
692 ALIGN(buffer->offsets_size, sizeof(void *)) +
693 ALIGN(buffer->extra_buffers_size, sizeof(void *));
694
695 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
696 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
697 alloc->pid, buffer, size, buffer_size);
698
699 BUG_ON(buffer->free);
700 BUG_ON(size > buffer_size);
701 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800702 BUG_ON(buffer->user_data < alloc->buffer);
703 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700704
705 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000706 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700707 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
708 "%d: binder_free_buf size %zd async free %zd\n",
709 alloc->pid, size, alloc->free_async_space);
710 }
711
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000712 binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data),
713 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700714
715 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
716 buffer->free = 1;
717 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700718 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700719
720 if (next->free) {
721 rb_erase(&next->rb_node, &alloc->free_buffers);
722 binder_delete_free_buffer(alloc, next);
723 }
724 }
725 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700726 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700727
728 if (prev->free) {
729 binder_delete_free_buffer(alloc, buffer);
730 rb_erase(&prev->rb_node, &alloc->free_buffers);
731 buffer = prev;
732 }
733 }
734 binder_insert_free_buffer(alloc, buffer);
735}
736
Carlos Llamas59e0d622023-12-01 17:21:44 +0000737/**
738 * binder_alloc_get_page() - get kernel pointer for given buffer offset
739 * @alloc: binder_alloc for this proc
740 * @buffer: binder buffer to be accessed
741 * @buffer_offset: offset into @buffer data
742 * @pgoffp: address to copy final page offset to
743 *
744 * Lookup the struct page corresponding to the address
745 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
746 * NULL, the byte-offset into the page is written there.
747 *
748 * The caller is responsible to ensure that the offset points
749 * to a valid address within the @buffer and that @buffer is
750 * not freeable by the user. Since it can't be freed, we are
751 * guaranteed that the corresponding elements of @alloc->pages[]
752 * cannot change.
753 *
754 * Return: struct page
755 */
756static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
757 struct binder_buffer *buffer,
758 binder_size_t buffer_offset,
759 pgoff_t *pgoffp)
760{
761 binder_size_t buffer_space_offset = buffer_offset +
762 (buffer->user_data - alloc->buffer);
763 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
764 size_t index = buffer_space_offset >> PAGE_SHIFT;
765 struct binder_lru_page *lru_page;
766
767 lru_page = &alloc->pages[index];
768 *pgoffp = pgoff;
769 return lru_page->page_ptr;
770}
771
772/**
773 * binder_alloc_clear_buf() - zero out buffer
774 * @alloc: binder_alloc for this proc
775 * @buffer: binder buffer to be cleared
776 *
777 * memset the given buffer to 0
778 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800779static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000780 struct binder_buffer *buffer)
781{
782 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
783 binder_size_t buffer_offset = 0;
784
785 while (bytes) {
786 unsigned long size;
787 struct page *page;
788 pgoff_t pgoff;
789 void *kptr;
790
791 page = binder_alloc_get_page(alloc, buffer,
792 buffer_offset, &pgoff);
793 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
794 kptr = kmap(page) + pgoff;
795 memset(kptr, 0, size);
796 kunmap(page);
797 bytes -= size;
798 buffer_offset += size;
799 }
800}
801
802
Todd Kjos0c972a02017-06-29 12:01:41 -0700803/**
804 * binder_alloc_free_buf() - free a binder buffer
805 * @alloc: binder_alloc for this proc
806 * @buffer: kernel pointer to buffer
807 *
YangHui4b463822020-08-18 09:34:04 +0800808 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700809 */
810void binder_alloc_free_buf(struct binder_alloc *alloc,
811 struct binder_buffer *buffer)
812{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800813 /*
814 * We could eliminate the call to binder_alloc_clear_buf()
815 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000816 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800817 * increase contention for the alloc mutex if clear_on_free
818 * is used frequently for large buffers. The mutex is not
819 * needed for correctness here.
820 */
821 if (buffer->clear_on_free) {
822 binder_alloc_clear_buf(alloc, buffer);
823 buffer->clear_on_free = false;
824 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700825 mutex_lock(&alloc->mutex);
826 binder_free_buf_locked(alloc, buffer);
827 mutex_unlock(&alloc->mutex);
828}
829
830/**
831 * binder_alloc_mmap_handler() - map virtual address space for proc
832 * @alloc: alloc structure for this proc
833 * @vma: vma passed to mmap()
834 *
835 * Called by binder_mmap() to initialize the space specified in
836 * vma for allocating binder buffers
837 *
838 * Return:
839 * 0 = success
840 * -EBUSY = address space already mapped
841 * -ENOMEM = failed to map memory to given address space
842 */
843int binder_alloc_mmap_handler(struct binder_alloc *alloc,
844 struct vm_area_struct *vma)
845{
Todd Kjos0c972a02017-06-29 12:01:41 -0700846 struct binder_buffer *buffer;
Carlos Llamasaf711932023-12-01 17:21:47 +0000847 const char *failure_string;
848 int ret, i;
Todd Kjos0c972a02017-06-29 12:01:41 -0700849
Carlos Llamasd276fb42022-11-04 23:12:35 +0000850 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
851 ret = -EINVAL;
852 failure_string = "invalid vma->vm_mm";
853 goto err_invalid_mm;
854 }
855
Todd Kjos0c972a02017-06-29 12:01:41 -0700856 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200857 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700858 ret = -EBUSY;
859 failure_string = "already mapped";
860 goto err_already_mapped;
861 }
Jann Horn45d02f72019-10-16 17:01:18 +0200862 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
863 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200864 mutex_unlock(&binder_alloc_mmap_lock);
865
Carlos Llamasc38a8982023-12-01 17:21:38 +0000866 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200867
Jann Horn45d02f72019-10-16 17:01:18 +0200868 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700869 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700870 GFP_KERNEL);
871 if (alloc->pages == NULL) {
872 ret = -ENOMEM;
873 failure_string = "alloc page array";
874 goto err_alloc_pages_failed;
875 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700876
Carlos Llamasaf711932023-12-01 17:21:47 +0000877 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
878 alloc->pages[i].alloc = alloc;
879 INIT_LIST_HEAD(&alloc->pages[i].lru);
880 }
881
Sherry Yang74310e02017-08-23 08:46:41 -0700882 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
883 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700884 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700885 failure_string = "alloc buffer struct";
886 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700887 }
Sherry Yang74310e02017-08-23 08:46:41 -0700888
Todd Kjosbde4a192019-02-08 10:35:20 -0800889 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700890 list_add(&buffer->entry, &alloc->buffers);
891 buffer->free = 1;
892 binder_insert_free_buffer(alloc, buffer);
893 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000894
895 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900896 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700897
898 return 0;
899
Sherry Yang74310e02017-08-23 08:46:41 -0700900err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700901 kfree(alloc->pages);
902 alloc->pages = NULL;
903err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000904 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200905 mutex_lock(&binder_alloc_mmap_lock);
906 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700907err_already_mapped:
908 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000909err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700910 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
911 "%s: %d %lx-%lx %s failed %d\n", __func__,
912 alloc->pid, vma->vm_start, vma->vm_end,
913 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700914 return ret;
915}
916
917
918void binder_alloc_deferred_release(struct binder_alloc *alloc)
919{
920 struct rb_node *n;
921 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700922 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700923
Todd Kjos0c972a02017-06-29 12:01:41 -0700924 buffers = 0;
925 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000926 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900927
Todd Kjos0c972a02017-06-29 12:01:41 -0700928 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700929 buffer = rb_entry(n, struct binder_buffer, rb_node);
930
931 /* Transaction should already have been freed */
932 BUG_ON(buffer->transaction);
933
Todd Kjos0f966cb2020-11-20 15:37:43 -0800934 if (buffer->clear_on_free) {
935 binder_alloc_clear_buf(alloc, buffer);
936 buffer->clear_on_free = false;
937 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700938 binder_free_buf_locked(alloc, buffer);
939 buffers++;
940 }
941
Sherry Yang74310e02017-08-23 08:46:41 -0700942 while (!list_empty(&alloc->buffers)) {
943 buffer = list_first_entry(&alloc->buffers,
944 struct binder_buffer, entry);
945 WARN_ON(!buffer->free);
946
947 list_del(&buffer->entry);
948 WARN_ON_ONCE(!list_empty(&alloc->buffers));
949 kfree(buffer);
950 }
951
Todd Kjos0c972a02017-06-29 12:01:41 -0700952 page_count = 0;
953 if (alloc->pages) {
954 int i;
955
956 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000957 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700958 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700959
Sherry Yangf2517eb2017-08-23 08:46:42 -0700960 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700961 continue;
962
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000963 on_lru = list_lru_del(&binder_freelist,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700964 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700965 page_addr = alloc->buffer + i * PAGE_SIZE;
966 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000967 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700968 __func__, alloc->pid, i, page_addr,
969 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700970 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700971 page_count++;
972 }
973 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700974 }
975 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400976 if (alloc->vma_vm_mm)
977 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700978
979 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
980 "%s: %d buffers %d, pages %d\n",
981 __func__, alloc->pid, buffers, page_count);
982}
983
984static void print_binder_buffer(struct seq_file *m, const char *prefix,
985 struct binder_buffer *buffer)
986{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000987 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800988 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700989 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700990 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700991 buffer->transaction ? "active" : "delivered");
992}
993
994/**
995 * binder_alloc_print_allocated() - print buffer info
996 * @m: seq_file for output via seq_printf()
997 * @alloc: binder_alloc for this proc
998 *
999 * Prints information about every buffer associated with
1000 * the binder_alloc state to the given seq_file
1001 */
1002void binder_alloc_print_allocated(struct seq_file *m,
1003 struct binder_alloc *alloc)
1004{
1005 struct rb_node *n;
1006
1007 mutex_lock(&alloc->mutex);
1008 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1009 print_binder_buffer(m, " buffer",
1010 rb_entry(n, struct binder_buffer, rb_node));
1011 mutex_unlock(&alloc->mutex);
1012}
1013
1014/**
Sherry Yang8ef46652017-08-31 11:56:36 -07001015 * binder_alloc_print_pages() - print page usage
1016 * @m: seq_file for output via seq_printf()
1017 * @alloc: binder_alloc for this proc
1018 */
1019void binder_alloc_print_pages(struct seq_file *m,
1020 struct binder_alloc *alloc)
1021{
1022 struct binder_lru_page *page;
1023 int i;
1024 int active = 0;
1025 int lru = 0;
1026 int free = 0;
1027
1028 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +02001029 /*
1030 * Make sure the binder_alloc is fully initialized, otherwise we might
1031 * read inconsistent state.
1032 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +00001033 if (binder_alloc_get_vma(alloc) != NULL) {
1034 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
1035 page = &alloc->pages[i];
1036 if (!page->page_ptr)
1037 free++;
1038 else if (list_empty(&page->lru))
1039 active++;
1040 else
1041 lru++;
1042 }
Sherry Yang8ef46652017-08-31 11:56:36 -07001043 }
1044 mutex_unlock(&alloc->mutex);
1045 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001046 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001047}
1048
1049/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001050 * binder_alloc_get_allocated_count() - return count of buffers
1051 * @alloc: binder_alloc for this proc
1052 *
1053 * Return: count of allocated buffers
1054 */
1055int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1056{
1057 struct rb_node *n;
1058 int count = 0;
1059
1060 mutex_lock(&alloc->mutex);
1061 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1062 count++;
1063 mutex_unlock(&alloc->mutex);
1064 return count;
1065}
1066
1067
1068/**
1069 * binder_alloc_vma_close() - invalidate address space
1070 * @alloc: binder_alloc for this proc
1071 *
1072 * Called from binder_vma_close() when releasing address space.
1073 * Clears alloc->vma to prevent new incoming transactions from
1074 * allocating more buffers.
1075 */
1076void binder_alloc_vma_close(struct binder_alloc *alloc)
1077{
Minchan Kimda1b9562018-08-23 14:29:56 +09001078 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001079}
1080
1081/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001082 * binder_alloc_free_page() - shrinker callback to free pages
1083 * @item: item to free
1084 * @lock: lock protecting the item
1085 * @cb_arg: callback argument
1086 *
1087 * Called from list_lru_walk() in binder_shrink_scan() to free
1088 * up pages when the system is under memory pressure.
1089 */
1090enum lru_status binder_alloc_free_page(struct list_head *item,
1091 struct list_lru_one *lru,
1092 spinlock_t *lock,
1093 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001094 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001095{
1096 struct mm_struct *mm = NULL;
1097 struct binder_lru_page *page = container_of(item,
1098 struct binder_lru_page,
1099 lru);
1100 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001101 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001102 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -07001103 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001104
1105 alloc = page->alloc;
1106 if (!mutex_trylock(&alloc->mutex))
1107 goto err_get_alloc_mutex_failed;
1108
1109 if (!page->page_ptr)
1110 goto err_page_already_freed;
1111
1112 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001113 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001114
1115 mm = alloc->vma_vm_mm;
1116 if (!mmget_not_zero(mm))
1117 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001118 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001119 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001120 vma = vma_lookup(mm, page_addr);
1121 if (vma && vma != binder_alloc_get_vma(alloc))
1122 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001123
Sherry Yanga1b22892017-10-03 16:15:00 -07001124 list_lru_isolate(lru, item);
1125 spin_unlock(lock);
1126
1127 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001128 trace_binder_unmap_user_start(alloc, index);
1129
Todd Kjosc41358a2019-02-08 10:35:19 -08001130 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001131
Sherry Yange41e1642017-08-23 08:46:43 -07001132 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001133 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001134 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001135 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001136
Sherry Yange41e1642017-08-23 08:46:43 -07001137 trace_binder_unmap_kernel_start(alloc, index);
1138
Sherry Yangf2517eb2017-08-23 08:46:42 -07001139 __free_page(page->page_ptr);
1140 page->page_ptr = NULL;
1141
Sherry Yange41e1642017-08-23 08:46:43 -07001142 trace_binder_unmap_kernel_end(alloc, index);
1143
Sherry Yanga1b22892017-10-03 16:15:00 -07001144 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001145 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001146 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001147
Carlos Llamas8dce2882023-12-01 17:21:31 +00001148err_invalid_vma:
1149 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001150err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001151 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001152err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001153err_page_already_freed:
1154 mutex_unlock(&alloc->mutex);
1155err_get_alloc_mutex_failed:
1156 return LRU_SKIP;
1157}
1158
1159static unsigned long
1160binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1161{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001162 return list_lru_count(&binder_freelist);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001163}
1164
1165static unsigned long
1166binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1167{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001168 return list_lru_walk(&binder_freelist, binder_alloc_free_page,
Sherry Yangf2517eb2017-08-23 08:46:42 -07001169 NULL, sc->nr_to_scan);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001170}
1171
Sherry Yangde7bbe32017-10-06 16:12:05 -04001172static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001173 .count_objects = binder_shrink_count,
1174 .scan_objects = binder_shrink_scan,
1175 .seeks = DEFAULT_SEEKS,
1176};
1177
1178/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001179 * binder_alloc_init() - called by binder_open() for per-proc initialization
1180 * @alloc: binder_alloc for this proc
1181 *
1182 * Called from binder_open() to initialize binder_alloc fields for
1183 * new binder proc
1184 */
1185void binder_alloc_init(struct binder_alloc *alloc)
1186{
Todd Kjos0c972a02017-06-29 12:01:41 -07001187 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001188 alloc->vma_vm_mm = current->mm;
1189 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001190 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001191 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001192}
1193
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001194int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001195{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001196 int ret = list_lru_init(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001197
1198 if (ret == 0) {
1199 ret = register_shrinker(&binder_shrinker);
1200 if (ret)
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001201 list_lru_destroy(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001202 }
1203 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001204}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001205
Qi Zheng03eebad2023-06-25 15:49:37 +00001206void binder_alloc_shrinker_exit(void)
1207{
1208 unregister_shrinker(&binder_shrinker);
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001209 list_lru_destroy(&binder_freelist);
Qi Zheng03eebad2023-06-25 15:49:37 +00001210}
1211
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001212/**
1213 * check_buffer() - verify that buffer/offset is safe to access
1214 * @alloc: binder_alloc for this proc
1215 * @buffer: binder buffer to be accessed
1216 * @offset: offset into @buffer data
1217 * @bytes: bytes to access from offset
1218 *
1219 * Check that the @offset/@bytes are within the size of the given
1220 * @buffer and that the buffer is currently active and not freeable.
1221 * Offsets must also be multiples of sizeof(u32). The kernel is
1222 * allowed to touch the buffer in two cases:
1223 *
1224 * 1) when the buffer is being created:
1225 * (buffer->free == 0 && buffer->allow_user_free == 0)
1226 * 2) when the buffer is being torn down:
1227 * (buffer->free == 0 && buffer->transaction == NULL).
1228 *
1229 * Return: true if the buffer is safe to access
1230 */
1231static inline bool check_buffer(struct binder_alloc *alloc,
1232 struct binder_buffer *buffer,
1233 binder_size_t offset, size_t bytes)
1234{
1235 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1236
1237 return buffer_size >= bytes &&
1238 offset <= buffer_size - bytes &&
1239 IS_ALIGNED(offset, sizeof(u32)) &&
1240 !buffer->free &&
1241 (!buffer->allow_user_free || !buffer->transaction);
1242}
1243
1244/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001245 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1246 * @alloc: binder_alloc for this proc
1247 * @buffer: binder buffer to be accessed
1248 * @buffer_offset: offset into @buffer data
1249 * @from: userspace pointer to source buffer
1250 * @bytes: bytes to copy
1251 *
1252 * Copy bytes from source userspace to target buffer.
1253 *
1254 * Return: bytes remaining to be copied
1255 */
1256unsigned long
1257binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1258 struct binder_buffer *buffer,
1259 binder_size_t buffer_offset,
1260 const void __user *from,
1261 size_t bytes)
1262{
1263 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1264 return bytes;
1265
1266 while (bytes) {
1267 unsigned long size;
1268 unsigned long ret;
1269 struct page *page;
1270 pgoff_t pgoff;
1271 void *kptr;
1272
1273 page = binder_alloc_get_page(alloc, buffer,
1274 buffer_offset, &pgoff);
1275 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1276 kptr = kmap(page) + pgoff;
1277 ret = copy_from_user(kptr, from, size);
1278 kunmap(page);
1279 if (ret)
1280 return bytes - size + ret;
1281 bytes -= size;
1282 from += size;
1283 buffer_offset += size;
1284 }
1285 return 0;
1286}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001287
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001288static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1289 bool to_buffer,
1290 struct binder_buffer *buffer,
1291 binder_size_t buffer_offset,
1292 void *ptr,
1293 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001294{
1295 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001296 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1297 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001298
1299 while (bytes) {
1300 unsigned long size;
1301 struct page *page;
1302 pgoff_t pgoff;
1303 void *tmpptr;
1304 void *base_ptr;
1305
1306 page = binder_alloc_get_page(alloc, buffer,
1307 buffer_offset, &pgoff);
1308 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1309 base_ptr = kmap_atomic(page);
1310 tmpptr = base_ptr + pgoff;
1311 if (to_buffer)
1312 memcpy(tmpptr, ptr, size);
1313 else
1314 memcpy(ptr, tmpptr, size);
1315 /*
1316 * kunmap_atomic() takes care of flushing the cache
1317 * if this device has VIVT cache arch
1318 */
1319 kunmap_atomic(base_ptr);
1320 bytes -= size;
1321 pgoff = 0;
1322 ptr = ptr + size;
1323 buffer_offset += size;
1324 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001325 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001326}
1327
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001328int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1329 struct binder_buffer *buffer,
1330 binder_size_t buffer_offset,
1331 void *src,
1332 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001333{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001334 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1335 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001336}
1337
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001338int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1339 void *dest,
1340 struct binder_buffer *buffer,
1341 binder_size_t buffer_offset,
1342 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001343{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001344 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1345 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001346}
1347