blob: 328887f0a7407d5f3f3dc0f5ec5711246b2260d4 [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) {
Carlos Llamasf0667c82023-12-01 17:21:55 +0000254 pr_err("%d: %s failed to insert page at offset %lx with %d\n",
255 alloc->pid, __func__, addr - alloc->buffer, ret);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000256 __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
Carlos Llamas0b243682023-12-01 17:21:39 +0000309 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700310
311 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000312 unsigned long index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700313 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700314
Sherry Yange41e1642017-08-23 08:46:43 -0700315 index = (page_addr - alloc->buffer) / PAGE_SIZE;
316 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700317
Sherry Yangf2517eb2017-08-23 08:46:42 -0700318 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700319 trace_binder_alloc_lru_start(alloc, index);
320
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000321 on_lru = list_lru_del(&binder_freelist, &page->lru);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700322 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700323
324 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700325 continue;
326 }
327
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100328 if (index + 1 > alloc->pages_high)
329 alloc->pages_high = index + 1;
Todd Kjos0c972a02017-06-29 12:01:41 -0700330 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700331}
332
Minchan Kimda1b9562018-08-23 14:29:56 +0900333static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
334 struct vm_area_struct *vma)
335{
Carlos Llamasb094b042023-05-30 19:43:37 +0000336 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
337 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900338}
339
340static inline struct vm_area_struct *binder_alloc_get_vma(
341 struct binder_alloc *alloc)
342{
Carlos Llamasb094b042023-05-30 19:43:37 +0000343 /* pairs with smp_store_release in binder_alloc_set_vma() */
344 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900345}
346
Carlos Llamase1d195e2023-12-01 17:21:42 +0000347static void debug_no_space_locked(struct binder_alloc *alloc)
348{
349 size_t largest_alloc_size = 0;
350 struct binder_buffer *buffer;
351 size_t allocated_buffers = 0;
352 size_t largest_free_size = 0;
353 size_t total_alloc_size = 0;
354 size_t total_free_size = 0;
355 size_t free_buffers = 0;
356 size_t buffer_size;
357 struct rb_node *n;
358
359 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
360 buffer = rb_entry(n, struct binder_buffer, rb_node);
361 buffer_size = binder_alloc_buffer_size(alloc, buffer);
362 allocated_buffers++;
363 total_alloc_size += buffer_size;
364 if (buffer_size > largest_alloc_size)
365 largest_alloc_size = buffer_size;
366 }
367
368 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
369 buffer = rb_entry(n, struct binder_buffer, rb_node);
370 buffer_size = binder_alloc_buffer_size(alloc, buffer);
371 free_buffers++;
372 total_free_size += buffer_size;
373 if (buffer_size > largest_free_size)
374 largest_free_size = buffer_size;
375 }
376
377 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
378 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
379 total_alloc_size, allocated_buffers,
380 largest_alloc_size, total_free_size,
381 free_buffers, largest_free_size);
382}
383
Carlos Llamas26d06d92023-12-01 17:21:41 +0000384static bool debug_low_async_space_locked(struct binder_alloc *alloc)
Martijn Coenen261e7812020-08-21 14:25:44 +0200385{
386 /*
387 * Find the amount and size of buffers allocated by the current caller;
388 * The idea is that once we cross the threshold, whoever is responsible
389 * for the low async space is likely to try to send another async txn,
390 * and at some point we'll catch them in the act. This is more efficient
391 * than keeping a map per pid.
392 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200393 struct binder_buffer *buffer;
394 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000395 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200396 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000397 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200398
Carlos Llamas081ddad2023-12-01 17:21:43 +0000399 /*
400 * Only start detecting spammers once we have less than 20% of async
401 * space left (which is less than 10% of total buffer size).
402 */
403 if (alloc->free_async_space >= alloc->buffer_size / 10) {
404 alloc->oneway_spam_detected = false;
405 return false;
406 }
407
Martijn Coenen261e7812020-08-21 14:25:44 +0200408 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
409 n = rb_next(n)) {
410 buffer = rb_entry(n, struct binder_buffer, rb_node);
411 if (buffer->pid != pid)
412 continue;
413 if (!buffer->async_transaction)
414 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000415 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200416 num_buffers++;
417 }
418
419 /*
420 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800421 * async space (which is 25% of total buffer size). Oneway spam is only
422 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200423 */
424 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
425 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
426 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
427 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800428 if (!alloc->oneway_spam_detected) {
429 alloc->oneway_spam_detected = true;
430 return true;
431 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200432 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800433 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200434}
435
Carlos Llamasef524f42023-12-01 17:21:46 +0000436/* Callers preallocate @new_buffer, it is freed by this function if unused */
Xiongwei Song3f827242017-12-14 12:15:42 +0800437static struct binder_buffer *binder_alloc_new_buf_locked(
438 struct binder_alloc *alloc,
Carlos Llamasef524f42023-12-01 17:21:46 +0000439 struct binder_buffer *new_buffer,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000440 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000441 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700442{
443 struct rb_node *n = alloc->free_buffers.rb_node;
Todd Kjos0c972a02017-06-29 12:01:41 -0700444 struct rb_node *best_fit = NULL;
Carlos Llamasef524f42023-12-01 17:21:46 +0000445 struct binder_buffer *buffer;
Carlos Llamas683f84a2023-12-01 17:21:52 +0000446 unsigned long next_used_page;
447 unsigned long curr_last_page;
Carlos Llamasef524f42023-12-01 17:21:46 +0000448 size_t buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700449
Zhuguangqing1174e452021-03-09 15:47:43 +0800450 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000451
Carlos Llamas11ca0762023-12-01 17:21:34 +0000452 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700453 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
454 "%d: binder_alloc_buf size %zd failed, no async space left\n",
455 alloc->pid, size);
Carlos Llamasef524f42023-12-01 17:21:46 +0000456 buffer = ERR_PTR(-ENOSPC);
457 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700458 }
459
460 while (n) {
461 buffer = rb_entry(n, struct binder_buffer, rb_node);
462 BUG_ON(!buffer->free);
463 buffer_size = binder_alloc_buffer_size(alloc, buffer);
464
465 if (size < buffer_size) {
466 best_fit = n;
467 n = n->rb_left;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000468 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700469 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000470 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700471 best_fit = n;
472 break;
473 }
474 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000475
Carlos Llamase1d195e2023-12-01 17:21:42 +0000476 if (unlikely(!best_fit)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700477 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
478 "%d: binder_alloc_buf size %zd failed, no address space\n",
479 alloc->pid, size);
Carlos Llamase1d195e2023-12-01 17:21:42 +0000480 debug_no_space_locked(alloc);
Carlos Llamasef524f42023-12-01 17:21:46 +0000481 buffer = ERR_PTR(-ENOSPC);
482 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700483 }
Carlos Llamase1d195e2023-12-01 17:21:42 +0000484
Carlos Llamas356047f2023-12-01 17:21:50 +0000485 if (buffer_size != size) {
486 /* Found an oversized buffer and needs to be split */
Todd Kjos0c972a02017-06-29 12:01:41 -0700487 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
488 buffer_size = binder_alloc_buffer_size(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700489
Carlos Llamas356047f2023-12-01 17:21:50 +0000490 WARN_ON(n || buffer_size == size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000491 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700492 list_add(&new_buffer->entry, &buffer->entry);
493 new_buffer->free = 1;
494 binder_insert_free_buffer(alloc, new_buffer);
Carlos Llamasef524f42023-12-01 17:21:46 +0000495 new_buffer = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700496 }
Sherry Yang74310e02017-08-23 08:46:41 -0700497
Carlos Llamas356047f2023-12-01 17:21:50 +0000498 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
499 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
500 alloc->pid, size, buffer, buffer_size);
501
Carlos Llamas683f84a2023-12-01 17:21:52 +0000502 /*
503 * Now we remove the pages from the freelist. A clever calculation
504 * with buffer_size determines if the last page is shared with an
505 * adjacent in-use buffer. In such case, the page has been already
506 * removed from the freelist so we trim our range short.
507 */
508 next_used_page = (buffer->user_data + buffer_size) & PAGE_MASK;
509 curr_last_page = PAGE_ALIGN(buffer->user_data + size);
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000510 binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data),
Carlos Llamas683f84a2023-12-01 17:21:52 +0000511 min(next_used_page, curr_last_page));
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{
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000638 struct binder_buffer *prev, *next;
639
640 if (PAGE_ALIGNED(buffer->user_data))
641 goto skip_freelist;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530642
Todd Kjos0c972a02017-06-29 12:01:41 -0700643 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700644 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700645 BUG_ON(!prev->free);
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000646 if (prev_buffer_end_page(prev) == buffer_start_page(buffer))
647 goto skip_freelist;
Todd Kjos0c972a02017-06-29 12:01:41 -0700648
649 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700650 next = binder_buffer_next(buffer);
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000651 if (buffer_start_page(next) == buffer_start_page(buffer))
652 goto skip_freelist;
Todd Kjos0c972a02017-06-29 12:01:41 -0700653 }
Sherry Yang74310e02017-08-23 08:46:41 -0700654
Carlos Llamasb93c9f82023-12-01 17:21:54 +0000655 binder_lru_freelist_add(alloc, buffer_start_page(buffer),
656 buffer_start_page(buffer) + PAGE_SIZE);
657skip_freelist:
Sherry Yang74310e02017-08-23 08:46:41 -0700658 list_del(&buffer->entry);
659 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700660}
661
662static void binder_free_buf_locked(struct binder_alloc *alloc,
663 struct binder_buffer *buffer)
664{
665 size_t size, buffer_size;
666
667 buffer_size = binder_alloc_buffer_size(alloc, buffer);
668
669 size = ALIGN(buffer->data_size, sizeof(void *)) +
670 ALIGN(buffer->offsets_size, sizeof(void *)) +
671 ALIGN(buffer->extra_buffers_size, sizeof(void *));
672
673 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
674 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
675 alloc->pid, buffer, size, buffer_size);
676
677 BUG_ON(buffer->free);
678 BUG_ON(size > buffer_size);
679 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800680 BUG_ON(buffer->user_data < alloc->buffer);
681 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700682
683 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000684 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700685 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
686 "%d: binder_free_buf size %zd async free %zd\n",
687 alloc->pid, size, alloc->free_async_space);
688 }
689
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000690 binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data),
691 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700692
693 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
694 buffer->free = 1;
695 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700696 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700697
698 if (next->free) {
699 rb_erase(&next->rb_node, &alloc->free_buffers);
700 binder_delete_free_buffer(alloc, next);
701 }
702 }
703 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700704 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700705
706 if (prev->free) {
707 binder_delete_free_buffer(alloc, buffer);
708 rb_erase(&prev->rb_node, &alloc->free_buffers);
709 buffer = prev;
710 }
711 }
712 binder_insert_free_buffer(alloc, buffer);
713}
714
Carlos Llamas59e0d622023-12-01 17:21:44 +0000715/**
716 * binder_alloc_get_page() - get kernel pointer for given buffer offset
717 * @alloc: binder_alloc for this proc
718 * @buffer: binder buffer to be accessed
719 * @buffer_offset: offset into @buffer data
720 * @pgoffp: address to copy final page offset to
721 *
722 * Lookup the struct page corresponding to the address
723 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
724 * NULL, the byte-offset into the page is written there.
725 *
726 * The caller is responsible to ensure that the offset points
727 * to a valid address within the @buffer and that @buffer is
728 * not freeable by the user. Since it can't be freed, we are
729 * guaranteed that the corresponding elements of @alloc->pages[]
730 * cannot change.
731 *
732 * Return: struct page
733 */
734static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
735 struct binder_buffer *buffer,
736 binder_size_t buffer_offset,
737 pgoff_t *pgoffp)
738{
739 binder_size_t buffer_space_offset = buffer_offset +
740 (buffer->user_data - alloc->buffer);
741 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
742 size_t index = buffer_space_offset >> PAGE_SHIFT;
743 struct binder_lru_page *lru_page;
744
745 lru_page = &alloc->pages[index];
746 *pgoffp = pgoff;
747 return lru_page->page_ptr;
748}
749
750/**
751 * binder_alloc_clear_buf() - zero out buffer
752 * @alloc: binder_alloc for this proc
753 * @buffer: binder buffer to be cleared
754 *
755 * memset the given buffer to 0
756 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800757static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000758 struct binder_buffer *buffer)
759{
760 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
761 binder_size_t buffer_offset = 0;
762
763 while (bytes) {
764 unsigned long size;
765 struct page *page;
766 pgoff_t pgoff;
767 void *kptr;
768
769 page = binder_alloc_get_page(alloc, buffer,
770 buffer_offset, &pgoff);
771 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
772 kptr = kmap(page) + pgoff;
773 memset(kptr, 0, size);
774 kunmap(page);
775 bytes -= size;
776 buffer_offset += size;
777 }
778}
779
780
Todd Kjos0c972a02017-06-29 12:01:41 -0700781/**
782 * binder_alloc_free_buf() - free a binder buffer
783 * @alloc: binder_alloc for this proc
784 * @buffer: kernel pointer to buffer
785 *
YangHui4b463822020-08-18 09:34:04 +0800786 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700787 */
788void binder_alloc_free_buf(struct binder_alloc *alloc,
789 struct binder_buffer *buffer)
790{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800791 /*
792 * We could eliminate the call to binder_alloc_clear_buf()
793 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000794 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800795 * increase contention for the alloc mutex if clear_on_free
796 * is used frequently for large buffers. The mutex is not
797 * needed for correctness here.
798 */
799 if (buffer->clear_on_free) {
800 binder_alloc_clear_buf(alloc, buffer);
801 buffer->clear_on_free = false;
802 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700803 mutex_lock(&alloc->mutex);
804 binder_free_buf_locked(alloc, buffer);
805 mutex_unlock(&alloc->mutex);
806}
807
808/**
809 * binder_alloc_mmap_handler() - map virtual address space for proc
810 * @alloc: alloc structure for this proc
811 * @vma: vma passed to mmap()
812 *
813 * Called by binder_mmap() to initialize the space specified in
814 * vma for allocating binder buffers
815 *
816 * Return:
817 * 0 = success
818 * -EBUSY = address space already mapped
819 * -ENOMEM = failed to map memory to given address space
820 */
821int binder_alloc_mmap_handler(struct binder_alloc *alloc,
822 struct vm_area_struct *vma)
823{
Todd Kjos0c972a02017-06-29 12:01:41 -0700824 struct binder_buffer *buffer;
Carlos Llamasaf711932023-12-01 17:21:47 +0000825 const char *failure_string;
826 int ret, i;
Todd Kjos0c972a02017-06-29 12:01:41 -0700827
Carlos Llamasd276fb42022-11-04 23:12:35 +0000828 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
829 ret = -EINVAL;
830 failure_string = "invalid vma->vm_mm";
831 goto err_invalid_mm;
832 }
833
Todd Kjos0c972a02017-06-29 12:01:41 -0700834 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200835 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700836 ret = -EBUSY;
837 failure_string = "already mapped";
838 goto err_already_mapped;
839 }
Jann Horn45d02f72019-10-16 17:01:18 +0200840 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
841 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200842 mutex_unlock(&binder_alloc_mmap_lock);
843
Carlos Llamasc38a8982023-12-01 17:21:38 +0000844 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200845
Jann Horn45d02f72019-10-16 17:01:18 +0200846 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700847 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700848 GFP_KERNEL);
849 if (alloc->pages == NULL) {
850 ret = -ENOMEM;
851 failure_string = "alloc page array";
852 goto err_alloc_pages_failed;
853 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700854
Carlos Llamasaf711932023-12-01 17:21:47 +0000855 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
856 alloc->pages[i].alloc = alloc;
857 INIT_LIST_HEAD(&alloc->pages[i].lru);
858 }
859
Sherry Yang74310e02017-08-23 08:46:41 -0700860 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
861 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700862 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700863 failure_string = "alloc buffer struct";
864 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700865 }
Sherry Yang74310e02017-08-23 08:46:41 -0700866
Todd Kjosbde4a192019-02-08 10:35:20 -0800867 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700868 list_add(&buffer->entry, &alloc->buffers);
869 buffer->free = 1;
870 binder_insert_free_buffer(alloc, buffer);
871 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000872
873 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900874 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700875
876 return 0;
877
Sherry Yang74310e02017-08-23 08:46:41 -0700878err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700879 kfree(alloc->pages);
880 alloc->pages = NULL;
881err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000882 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200883 mutex_lock(&binder_alloc_mmap_lock);
884 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700885err_already_mapped:
886 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000887err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700888 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
889 "%s: %d %lx-%lx %s failed %d\n", __func__,
890 alloc->pid, vma->vm_start, vma->vm_end,
891 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700892 return ret;
893}
894
895
896void binder_alloc_deferred_release(struct binder_alloc *alloc)
897{
898 struct rb_node *n;
899 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700900 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700901
Todd Kjos0c972a02017-06-29 12:01:41 -0700902 buffers = 0;
903 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000904 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900905
Todd Kjos0c972a02017-06-29 12:01:41 -0700906 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700907 buffer = rb_entry(n, struct binder_buffer, rb_node);
908
909 /* Transaction should already have been freed */
910 BUG_ON(buffer->transaction);
911
Todd Kjos0f966cb2020-11-20 15:37:43 -0800912 if (buffer->clear_on_free) {
913 binder_alloc_clear_buf(alloc, buffer);
914 buffer->clear_on_free = false;
915 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700916 binder_free_buf_locked(alloc, buffer);
917 buffers++;
918 }
919
Sherry Yang74310e02017-08-23 08:46:41 -0700920 while (!list_empty(&alloc->buffers)) {
921 buffer = list_first_entry(&alloc->buffers,
922 struct binder_buffer, entry);
923 WARN_ON(!buffer->free);
924
925 list_del(&buffer->entry);
926 WARN_ON_ONCE(!list_empty(&alloc->buffers));
927 kfree(buffer);
928 }
929
Todd Kjos0c972a02017-06-29 12:01:41 -0700930 page_count = 0;
931 if (alloc->pages) {
932 int i;
933
934 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000935 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700936 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700937
Sherry Yangf2517eb2017-08-23 08:46:42 -0700938 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700939 continue;
940
Carlos Llamas4c82cba2023-12-01 17:21:51 +0000941 on_lru = list_lru_del(&binder_freelist,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700942 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700943 page_addr = alloc->buffer + i * PAGE_SIZE;
944 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasf0667c82023-12-01 17:21:55 +0000945 "%s: %d: page %d %s\n",
946 __func__, alloc->pid, i,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700947 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700948 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700949 page_count++;
950 }
951 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700952 }
953 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400954 if (alloc->vma_vm_mm)
955 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700956
957 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
958 "%s: %d buffers %d, pages %d\n",
959 __func__, alloc->pid, buffers, page_count);
960}
961
Todd Kjos0c972a02017-06-29 12:01:41 -0700962/**
963 * binder_alloc_print_allocated() - print buffer info
964 * @m: seq_file for output via seq_printf()
965 * @alloc: binder_alloc for this proc
966 *
967 * Prints information about every buffer associated with
968 * the binder_alloc state to the given seq_file
969 */
970void binder_alloc_print_allocated(struct seq_file *m,
971 struct binder_alloc *alloc)
972{
Carlos Llamasf6b1c042023-12-01 17:21:53 +0000973 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700974 struct rb_node *n;
975
976 mutex_lock(&alloc->mutex);
Carlos Llamasf6b1c042023-12-01 17:21:53 +0000977 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
978 buffer = rb_entry(n, struct binder_buffer, rb_node);
979 seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n",
Carlos Llamasf0667c82023-12-01 17:21:55 +0000980 buffer->debug_id,
981 buffer->user_data - alloc->buffer,
Carlos Llamasf6b1c042023-12-01 17:21:53 +0000982 buffer->data_size, buffer->offsets_size,
983 buffer->extra_buffers_size,
984 buffer->transaction ? "active" : "delivered");
985 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700986 mutex_unlock(&alloc->mutex);
987}
988
989/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700990 * binder_alloc_print_pages() - print page usage
991 * @m: seq_file for output via seq_printf()
992 * @alloc: binder_alloc for this proc
993 */
994void binder_alloc_print_pages(struct seq_file *m,
995 struct binder_alloc *alloc)
996{
997 struct binder_lru_page *page;
998 int i;
999 int active = 0;
1000 int lru = 0;
1001 int free = 0;
1002
1003 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +02001004 /*
1005 * Make sure the binder_alloc is fully initialized, otherwise we might
1006 * read inconsistent state.
1007 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +00001008 if (binder_alloc_get_vma(alloc) != NULL) {
1009 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
1010 page = &alloc->pages[i];
1011 if (!page->page_ptr)
1012 free++;
1013 else if (list_empty(&page->lru))
1014 active++;
1015 else
1016 lru++;
1017 }
Sherry Yang8ef46652017-08-31 11:56:36 -07001018 }
1019 mutex_unlock(&alloc->mutex);
1020 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001021 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001022}
1023
1024/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001025 * binder_alloc_get_allocated_count() - return count of buffers
1026 * @alloc: binder_alloc for this proc
1027 *
1028 * Return: count of allocated buffers
1029 */
1030int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1031{
1032 struct rb_node *n;
1033 int count = 0;
1034
1035 mutex_lock(&alloc->mutex);
1036 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1037 count++;
1038 mutex_unlock(&alloc->mutex);
1039 return count;
1040}
1041
1042
1043/**
1044 * binder_alloc_vma_close() - invalidate address space
1045 * @alloc: binder_alloc for this proc
1046 *
1047 * Called from binder_vma_close() when releasing address space.
1048 * Clears alloc->vma to prevent new incoming transactions from
1049 * allocating more buffers.
1050 */
1051void binder_alloc_vma_close(struct binder_alloc *alloc)
1052{
Minchan Kimda1b9562018-08-23 14:29:56 +09001053 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001054}
1055
1056/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001057 * binder_alloc_free_page() - shrinker callback to free pages
1058 * @item: item to free
1059 * @lock: lock protecting the item
1060 * @cb_arg: callback argument
1061 *
1062 * Called from list_lru_walk() in binder_shrink_scan() to free
1063 * up pages when the system is under memory pressure.
1064 */
1065enum lru_status binder_alloc_free_page(struct list_head *item,
1066 struct list_lru_one *lru,
1067 spinlock_t *lock,
1068 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001069 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001070{
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001071 struct binder_lru_page *page = container_of(item, typeof(*page), lru);
1072 struct binder_alloc *alloc = page->alloc;
1073 struct mm_struct *mm = alloc->vma_vm_mm;
1074 struct vm_area_struct *vma;
1075 struct page *page_to_free;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001076 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001077 size_t index;
1078
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001079 if (!mmget_not_zero(mm))
1080 goto err_mmget;
1081 if (!mmap_read_trylock(mm))
1082 goto err_mmap_read_lock_failed;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001083 if (!mutex_trylock(&alloc->mutex))
1084 goto err_get_alloc_mutex_failed;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001085 if (!page->page_ptr)
1086 goto err_page_already_freed;
1087
1088 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001089 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001090
Carlos Llamas8dce2882023-12-01 17:21:31 +00001091 vma = vma_lookup(mm, page_addr);
1092 if (vma && vma != binder_alloc_get_vma(alloc))
1093 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001094
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001095 trace_binder_unmap_kernel_start(alloc, index);
1096
1097 page_to_free = page->page_ptr;
1098 page->page_ptr = NULL;
1099
1100 trace_binder_unmap_kernel_end(alloc, index);
1101
Sherry Yanga1b22892017-10-03 16:15:00 -07001102 list_lru_isolate(lru, item);
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001103 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001104 spin_unlock(lock);
1105
1106 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001107 trace_binder_unmap_user_start(alloc, index);
1108
Todd Kjosc41358a2019-02-08 10:35:19 -08001109 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001110
Sherry Yange41e1642017-08-23 08:46:43 -07001111 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001112 }
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001113
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001114 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001115 mmput_async(mm);
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001116 __free_page(page_to_free);
Sherry Yange41e1642017-08-23 08:46:43 -07001117
Sherry Yanga1b22892017-10-03 16:15:00 -07001118 spin_lock(lock);
Sherry Yanga1b22892017-10-03 16:15:00 -07001119 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001120
Carlos Llamas8dce2882023-12-01 17:21:31 +00001121err_invalid_vma:
Carlos Llamas5a8658e2023-12-01 17:21:56 +00001122err_page_already_freed:
1123 mutex_unlock(&alloc->mutex);
1124err_get_alloc_mutex_failed:
Carlos Llamas8dce2882023-12-01 17:21:31 +00001125 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001126err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001127 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001128err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001129 return LRU_SKIP;
1130}
1131
1132static unsigned long
1133binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1134{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001135 return list_lru_count(&binder_freelist);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001136}
1137
1138static unsigned long
1139binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1140{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001141 return list_lru_walk(&binder_freelist, binder_alloc_free_page,
Sherry Yangf2517eb2017-08-23 08:46:42 -07001142 NULL, sc->nr_to_scan);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001143}
1144
Sherry Yangde7bbe32017-10-06 16:12:05 -04001145static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001146 .count_objects = binder_shrink_count,
1147 .scan_objects = binder_shrink_scan,
1148 .seeks = DEFAULT_SEEKS,
1149};
1150
1151/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001152 * binder_alloc_init() - called by binder_open() for per-proc initialization
1153 * @alloc: binder_alloc for this proc
1154 *
1155 * Called from binder_open() to initialize binder_alloc fields for
1156 * new binder proc
1157 */
1158void binder_alloc_init(struct binder_alloc *alloc)
1159{
Todd Kjos0c972a02017-06-29 12:01:41 -07001160 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001161 alloc->vma_vm_mm = current->mm;
1162 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001163 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001164 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001165}
1166
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001167int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001168{
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001169 int ret = list_lru_init(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001170
1171 if (ret == 0) {
1172 ret = register_shrinker(&binder_shrinker);
1173 if (ret)
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001174 list_lru_destroy(&binder_freelist);
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001175 }
1176 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001177}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001178
Qi Zheng03eebad2023-06-25 15:49:37 +00001179void binder_alloc_shrinker_exit(void)
1180{
1181 unregister_shrinker(&binder_shrinker);
Carlos Llamas4c82cba2023-12-01 17:21:51 +00001182 list_lru_destroy(&binder_freelist);
Qi Zheng03eebad2023-06-25 15:49:37 +00001183}
1184
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001185/**
1186 * check_buffer() - verify that buffer/offset is safe to access
1187 * @alloc: binder_alloc for this proc
1188 * @buffer: binder buffer to be accessed
1189 * @offset: offset into @buffer data
1190 * @bytes: bytes to access from offset
1191 *
1192 * Check that the @offset/@bytes are within the size of the given
1193 * @buffer and that the buffer is currently active and not freeable.
1194 * Offsets must also be multiples of sizeof(u32). The kernel is
1195 * allowed to touch the buffer in two cases:
1196 *
1197 * 1) when the buffer is being created:
1198 * (buffer->free == 0 && buffer->allow_user_free == 0)
1199 * 2) when the buffer is being torn down:
1200 * (buffer->free == 0 && buffer->transaction == NULL).
1201 *
1202 * Return: true if the buffer is safe to access
1203 */
1204static inline bool check_buffer(struct binder_alloc *alloc,
1205 struct binder_buffer *buffer,
1206 binder_size_t offset, size_t bytes)
1207{
1208 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1209
1210 return buffer_size >= bytes &&
1211 offset <= buffer_size - bytes &&
1212 IS_ALIGNED(offset, sizeof(u32)) &&
1213 !buffer->free &&
1214 (!buffer->allow_user_free || !buffer->transaction);
1215}
1216
1217/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001218 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1219 * @alloc: binder_alloc for this proc
1220 * @buffer: binder buffer to be accessed
1221 * @buffer_offset: offset into @buffer data
1222 * @from: userspace pointer to source buffer
1223 * @bytes: bytes to copy
1224 *
1225 * Copy bytes from source userspace to target buffer.
1226 *
1227 * Return: bytes remaining to be copied
1228 */
1229unsigned long
1230binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1231 struct binder_buffer *buffer,
1232 binder_size_t buffer_offset,
1233 const void __user *from,
1234 size_t bytes)
1235{
1236 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1237 return bytes;
1238
1239 while (bytes) {
1240 unsigned long size;
1241 unsigned long ret;
1242 struct page *page;
1243 pgoff_t pgoff;
1244 void *kptr;
1245
1246 page = binder_alloc_get_page(alloc, buffer,
1247 buffer_offset, &pgoff);
1248 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1249 kptr = kmap(page) + pgoff;
1250 ret = copy_from_user(kptr, from, size);
1251 kunmap(page);
1252 if (ret)
1253 return bytes - size + ret;
1254 bytes -= size;
1255 from += size;
1256 buffer_offset += size;
1257 }
1258 return 0;
1259}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001260
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001261static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1262 bool to_buffer,
1263 struct binder_buffer *buffer,
1264 binder_size_t buffer_offset,
1265 void *ptr,
1266 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001267{
1268 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001269 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1270 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001271
1272 while (bytes) {
1273 unsigned long size;
1274 struct page *page;
1275 pgoff_t pgoff;
1276 void *tmpptr;
1277 void *base_ptr;
1278
1279 page = binder_alloc_get_page(alloc, buffer,
1280 buffer_offset, &pgoff);
1281 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1282 base_ptr = kmap_atomic(page);
1283 tmpptr = base_ptr + pgoff;
1284 if (to_buffer)
1285 memcpy(tmpptr, ptr, size);
1286 else
1287 memcpy(ptr, tmpptr, size);
1288 /*
1289 * kunmap_atomic() takes care of flushing the cache
1290 * if this device has VIVT cache arch
1291 */
1292 kunmap_atomic(base_ptr);
1293 bytes -= size;
1294 pgoff = 0;
1295 ptr = ptr + size;
1296 buffer_offset += size;
1297 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001298 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001299}
1300
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001301int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1302 struct binder_buffer *buffer,
1303 binder_size_t buffer_offset,
1304 void *src,
1305 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001306{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001307 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1308 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001309}
1310
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001311int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1312 void *dest,
1313 struct binder_buffer *buffer,
1314 binder_size_t buffer_offset,
1315 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001316{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001317 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1318 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001319}
1320