blob: f666dd62d88b34094d34547816494c265bb9d14d [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
Sherry Yangf2517eb2017-08-23 08:46:42 -070030struct list_lru binder_alloc_lru;
31
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 Llamas0b243682023-12-01 17:21:39 +0000179static void binder_free_page_range(struct binder_alloc *alloc,
180 unsigned long start, unsigned long end)
181{
182 struct binder_lru_page *page;
183 unsigned long page_addr;
184
185 trace_binder_update_page_range(alloc, false, start, end);
186
187 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
188 size_t index;
189 int ret;
190
191 index = (page_addr - alloc->buffer) / PAGE_SIZE;
192 page = &alloc->pages[index];
193
194 trace_binder_free_lru_start(alloc, index);
195
196 ret = list_lru_add(&binder_alloc_lru, &page->lru);
197 WARN_ON(!ret);
198
199 trace_binder_free_lru_end(alloc, index);
200 }
201}
202
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000203static int binder_install_single_page(struct binder_alloc *alloc,
204 struct binder_lru_page *lru_page,
205 unsigned long addr)
206{
207 struct page *page;
208 int ret = 0;
209
210 if (!mmget_not_zero(alloc->vma_vm_mm))
211 return -ESRCH;
212
213 mmap_write_lock(alloc->vma_vm_mm);
214 if (!alloc->vma) {
215 pr_err("%d: %s failed, no vma\n", alloc->pid, __func__);
216 ret = -ESRCH;
217 goto out;
218 }
219
220 page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
221 if (!page) {
222 pr_err("%d: failed to allocate page\n", alloc->pid);
223 ret = -ENOMEM;
224 goto out;
225 }
226
227 ret = vm_insert_page(alloc->vma, addr, page);
228 if (ret) {
229 pr_err("%d: %s failed to insert page at %lx with %d\n",
230 alloc->pid, __func__, addr, ret);
231 __free_page(page);
232 ret = -ENOMEM;
233 goto out;
234 }
235
236 lru_page->page_ptr = page;
237out:
238 mmap_write_unlock(alloc->vma_vm_mm);
239 mmput_async(alloc->vma_vm_mm);
240 return ret;
241}
242
Carlos Llamas0b243682023-12-01 17:21:39 +0000243static int binder_allocate_page_range(struct binder_alloc *alloc,
244 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700245{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000246 struct binder_lru_page *page;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000247 unsigned long page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700248
249 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamas0b243682023-12-01 17:21:39 +0000250 "%d: allocate pages %lx-%lx\n",
251 alloc->pid, start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700252
253 if (end <= start)
254 return 0;
255
Carlos Llamas0b243682023-12-01 17:21:39 +0000256 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700257
258 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000259 unsigned long index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700260 bool on_lru;
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000261 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700262
Sherry Yange41e1642017-08-23 08:46:43 -0700263 index = (page_addr - alloc->buffer) / PAGE_SIZE;
264 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700265
Sherry Yangf2517eb2017-08-23 08:46:42 -0700266 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700267 trace_binder_alloc_lru_start(alloc, index);
268
Sherry Yangf2517eb2017-08-23 08:46:42 -0700269 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
270 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700271
272 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700273 continue;
274 }
275
Sherry Yange41e1642017-08-23 08:46:43 -0700276 trace_binder_alloc_page_start(alloc, index);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000277
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000278 ret = binder_install_single_page(alloc, page, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700279 if (ret) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000280 binder_free_page_range(alloc, start, page_addr);
281 return ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700282 }
Sherry Yange41e1642017-08-23 08:46:43 -0700283
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100284 if (index + 1 > alloc->pages_high)
285 alloc->pages_high = index + 1;
286
Sherry Yange41e1642017-08-23 08:46:43 -0700287 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700288 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700289
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000290 return 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700291}
292
Minchan Kimda1b9562018-08-23 14:29:56 +0900293static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
294 struct vm_area_struct *vma)
295{
Carlos Llamasb094b042023-05-30 19:43:37 +0000296 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
297 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900298}
299
300static inline struct vm_area_struct *binder_alloc_get_vma(
301 struct binder_alloc *alloc)
302{
Carlos Llamasb094b042023-05-30 19:43:37 +0000303 /* pairs with smp_store_release in binder_alloc_set_vma() */
304 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900305}
306
Carlos Llamase1d195e2023-12-01 17:21:42 +0000307static void debug_no_space_locked(struct binder_alloc *alloc)
308{
309 size_t largest_alloc_size = 0;
310 struct binder_buffer *buffer;
311 size_t allocated_buffers = 0;
312 size_t largest_free_size = 0;
313 size_t total_alloc_size = 0;
314 size_t total_free_size = 0;
315 size_t free_buffers = 0;
316 size_t buffer_size;
317 struct rb_node *n;
318
319 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
320 buffer = rb_entry(n, struct binder_buffer, rb_node);
321 buffer_size = binder_alloc_buffer_size(alloc, buffer);
322 allocated_buffers++;
323 total_alloc_size += buffer_size;
324 if (buffer_size > largest_alloc_size)
325 largest_alloc_size = buffer_size;
326 }
327
328 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
329 buffer = rb_entry(n, struct binder_buffer, rb_node);
330 buffer_size = binder_alloc_buffer_size(alloc, buffer);
331 free_buffers++;
332 total_free_size += buffer_size;
333 if (buffer_size > largest_free_size)
334 largest_free_size = buffer_size;
335 }
336
337 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
338 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
339 total_alloc_size, allocated_buffers,
340 largest_alloc_size, total_free_size,
341 free_buffers, largest_free_size);
342}
343
Carlos Llamas26d06d92023-12-01 17:21:41 +0000344static bool debug_low_async_space_locked(struct binder_alloc *alloc)
Martijn Coenen261e7812020-08-21 14:25:44 +0200345{
346 /*
347 * Find the amount and size of buffers allocated by the current caller;
348 * The idea is that once we cross the threshold, whoever is responsible
349 * for the low async space is likely to try to send another async txn,
350 * and at some point we'll catch them in the act. This is more efficient
351 * than keeping a map per pid.
352 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200353 struct binder_buffer *buffer;
354 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000355 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200356 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000357 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200358
Carlos Llamas081ddad2023-12-01 17:21:43 +0000359 /*
360 * Only start detecting spammers once we have less than 20% of async
361 * space left (which is less than 10% of total buffer size).
362 */
363 if (alloc->free_async_space >= alloc->buffer_size / 10) {
364 alloc->oneway_spam_detected = false;
365 return false;
366 }
367
Martijn Coenen261e7812020-08-21 14:25:44 +0200368 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
369 n = rb_next(n)) {
370 buffer = rb_entry(n, struct binder_buffer, rb_node);
371 if (buffer->pid != pid)
372 continue;
373 if (!buffer->async_transaction)
374 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000375 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200376 num_buffers++;
377 }
378
379 /*
380 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800381 * async space (which is 25% of total buffer size). Oneway spam is only
382 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200383 */
384 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
385 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
386 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
387 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800388 if (!alloc->oneway_spam_detected) {
389 alloc->oneway_spam_detected = true;
390 return true;
391 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200392 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800393 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200394}
395
Carlos Llamasef524f42023-12-01 17:21:46 +0000396/* Callers preallocate @new_buffer, it is freed by this function if unused */
Xiongwei Song3f827242017-12-14 12:15:42 +0800397static struct binder_buffer *binder_alloc_new_buf_locked(
398 struct binder_alloc *alloc,
Carlos Llamasef524f42023-12-01 17:21:46 +0000399 struct binder_buffer *new_buffer,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000400 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000401 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700402{
403 struct rb_node *n = alloc->free_buffers.rb_node;
Todd Kjos0c972a02017-06-29 12:01:41 -0700404 struct rb_node *best_fit = NULL;
Carlos Llamasef524f42023-12-01 17:21:46 +0000405 struct binder_buffer *buffer;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000406 unsigned long has_page_addr;
407 unsigned long end_page_addr;
Carlos Llamasef524f42023-12-01 17:21:46 +0000408 size_t buffer_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700409 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700410
Zhuguangqing1174e452021-03-09 15:47:43 +0800411 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000412
Carlos Llamas11ca0762023-12-01 17:21:34 +0000413 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700414 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
415 "%d: binder_alloc_buf size %zd failed, no async space left\n",
416 alloc->pid, size);
Carlos Llamasef524f42023-12-01 17:21:46 +0000417 buffer = ERR_PTR(-ENOSPC);
418 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700419 }
420
421 while (n) {
422 buffer = rb_entry(n, struct binder_buffer, rb_node);
423 BUG_ON(!buffer->free);
424 buffer_size = binder_alloc_buffer_size(alloc, buffer);
425
426 if (size < buffer_size) {
427 best_fit = n;
428 n = n->rb_left;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000429 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700430 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000431 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700432 best_fit = n;
433 break;
434 }
435 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000436
Carlos Llamase1d195e2023-12-01 17:21:42 +0000437 if (unlikely(!best_fit)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700438 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
439 "%d: binder_alloc_buf size %zd failed, no address space\n",
440 alloc->pid, size);
Carlos Llamase1d195e2023-12-01 17:21:42 +0000441 debug_no_space_locked(alloc);
Carlos Llamasef524f42023-12-01 17:21:46 +0000442 buffer = ERR_PTR(-ENOSPC);
443 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700444 }
Carlos Llamase1d195e2023-12-01 17:21:42 +0000445
Todd Kjos0c972a02017-06-29 12:01:41 -0700446 if (n == NULL) {
447 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
448 buffer_size = binder_alloc_buffer_size(alloc, buffer);
449 }
450
451 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
452 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
453 alloc->pid, size, buffer, buffer_size);
454
Carlos Llamasc38a8982023-12-01 17:21:38 +0000455 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
Sherry Yang74310e02017-08-23 08:46:41 -0700456 WARN_ON(n && buffer_size != size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000457 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700458 if (end_page_addr > has_page_addr)
459 end_page_addr = has_page_addr;
Carlos Llamas0b243682023-12-01 17:21:39 +0000460 ret = binder_allocate_page_range(alloc, PAGE_ALIGN(buffer->user_data),
461 end_page_addr);
Carlos Llamasef524f42023-12-01 17:21:46 +0000462 if (ret) {
463 buffer = ERR_PTR(ret);
464 goto out;
465 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700466
Todd Kjos0c972a02017-06-29 12:01:41 -0700467 if (buffer_size != size) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000468 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700469 list_add(&new_buffer->entry, &buffer->entry);
470 new_buffer->free = 1;
471 binder_insert_free_buffer(alloc, new_buffer);
Carlos Llamasef524f42023-12-01 17:21:46 +0000472 new_buffer = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700473 }
Sherry Yang74310e02017-08-23 08:46:41 -0700474
475 rb_erase(best_fit, &alloc->free_buffers);
476 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800477 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700478 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700479 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
480 "%d: binder_alloc_buf size %zd got %pK\n",
481 alloc->pid, size, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700482 buffer->async_transaction = is_async;
Hang Lua7dc1e62021-04-09 17:40:46 +0800483 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700484 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000485 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700486 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
487 "%d: binder_alloc_buf size %zd async free %zd\n",
488 alloc->pid, size, alloc->free_async_space);
Carlos Llamas081ddad2023-12-01 17:21:43 +0000489 if (debug_low_async_space_locked(alloc))
490 buffer->oneway_spam_suspect = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700491 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000492
Carlos Llamasef524f42023-12-01 17:21:46 +0000493out:
494 /* Discard possibly unused new_buffer */
495 kfree(new_buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700496 return buffer;
497}
498
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000499/* Calculate the sanitized total size, returns 0 for invalid request */
500static inline size_t sanitized_size(size_t data_size,
501 size_t offsets_size,
502 size_t extra_buffers_size)
503{
504 size_t total, tmp;
505
506 /* Align to pointer size and check for overflows */
507 tmp = ALIGN(data_size, sizeof(void *)) +
508 ALIGN(offsets_size, sizeof(void *));
509 if (tmp < data_size || tmp < offsets_size)
510 return 0;
511 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
512 if (total < tmp || total < extra_buffers_size)
513 return 0;
514
515 /* Pad 0-sized buffers so they get a unique address */
516 total = max(total, sizeof(void *));
517
518 return total;
519}
520
Todd Kjos0c972a02017-06-29 12:01:41 -0700521/**
522 * binder_alloc_new_buf() - Allocate a new binder buffer
523 * @alloc: binder_alloc for this proc
524 * @data_size: size of user data buffer
525 * @offsets_size: user specified buffer offset
526 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
527 * @is_async: buffer for async transaction
528 *
529 * Allocate a new buffer given the requested sizes. Returns
530 * the kernel version of the buffer pointer. The size allocated
531 * is the sum of the three given sizes (each rounded up to
532 * pointer-sized boundary)
533 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000534 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700535 */
536struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
537 size_t data_size,
538 size_t offsets_size,
539 size_t extra_buffers_size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000540 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700541{
Carlos Llamasef524f42023-12-01 17:21:46 +0000542 struct binder_buffer *buffer, *next;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000543 size_t size;
544
545 /* Check binder_alloc is fully initialized */
546 if (!binder_alloc_get_vma(alloc)) {
547 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
548 "%d: binder_alloc_buf, no vma\n",
549 alloc->pid);
550 return ERR_PTR(-ESRCH);
551 }
552
553 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
554 if (unlikely(!size)) {
555 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
556 "%d: got transaction with invalid size %zd-%zd-%zd\n",
557 alloc->pid, data_size, offsets_size,
558 extra_buffers_size);
559 return ERR_PTR(-EINVAL);
560 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700561
Carlos Llamasef524f42023-12-01 17:21:46 +0000562 /* Preallocate the next buffer */
563 next = kzalloc(sizeof(*next), GFP_KERNEL);
564 if (!next)
565 return ERR_PTR(-ENOMEM);
566
Todd Kjos0c972a02017-06-29 12:01:41 -0700567 mutex_lock(&alloc->mutex);
Carlos Llamasef524f42023-12-01 17:21:46 +0000568 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000569 if (IS_ERR(buffer)) {
570 mutex_unlock(&alloc->mutex);
571 goto out;
572 }
573
574 buffer->data_size = data_size;
575 buffer->offsets_size = offsets_size;
576 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000577 buffer->pid = current->tgid;
Todd Kjos0c972a02017-06-29 12:01:41 -0700578 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000579
580out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700581 return buffer;
582}
583
Carlos Llamasc38a8982023-12-01 17:21:38 +0000584static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700585{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000586 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700587}
588
Carlos Llamasc38a8982023-12-01 17:21:38 +0000589static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700590{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000591 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700592}
593
594static void binder_delete_free_buffer(struct binder_alloc *alloc,
595 struct binder_buffer *buffer)
596{
597 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700598 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530599
Todd Kjos0c972a02017-06-29 12:01:41 -0700600 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700601 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700602 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700603 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
604 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700605 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000606 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800607 alloc->pid, buffer->user_data,
608 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700609 }
610
611 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700612 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700613 if (buffer_start_page(next) == buffer_start_page(buffer)) {
614 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700615 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000616 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700617 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800618 buffer->user_data,
619 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700620 }
621 }
Sherry Yang74310e02017-08-23 08:46:41 -0700622
Todd Kjosbde4a192019-02-08 10:35:20 -0800623 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700624 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000625 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800626 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700627 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700628 }
Sherry Yang74310e02017-08-23 08:46:41 -0700629
630 if (to_free) {
631 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000632 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800633 alloc->pid, buffer->user_data,
634 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000635 next ? next->user_data : 0);
Carlos Llamas0b243682023-12-01 17:21:39 +0000636 binder_free_page_range(alloc, buffer_start_page(buffer),
637 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700638 }
639 list_del(&buffer->entry);
640 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700641}
642
643static void binder_free_buf_locked(struct binder_alloc *alloc,
644 struct binder_buffer *buffer)
645{
646 size_t size, buffer_size;
647
648 buffer_size = binder_alloc_buffer_size(alloc, buffer);
649
650 size = ALIGN(buffer->data_size, sizeof(void *)) +
651 ALIGN(buffer->offsets_size, sizeof(void *)) +
652 ALIGN(buffer->extra_buffers_size, sizeof(void *));
653
654 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
655 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
656 alloc->pid, buffer, size, buffer_size);
657
658 BUG_ON(buffer->free);
659 BUG_ON(size > buffer_size);
660 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800661 BUG_ON(buffer->user_data < alloc->buffer);
662 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700663
664 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000665 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700666 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
667 "%d: binder_free_buf size %zd async free %zd\n",
668 alloc->pid, size, alloc->free_async_space);
669 }
670
Carlos Llamas0b243682023-12-01 17:21:39 +0000671 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
672 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700673
674 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
675 buffer->free = 1;
676 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700677 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700678
679 if (next->free) {
680 rb_erase(&next->rb_node, &alloc->free_buffers);
681 binder_delete_free_buffer(alloc, next);
682 }
683 }
684 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700685 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700686
687 if (prev->free) {
688 binder_delete_free_buffer(alloc, buffer);
689 rb_erase(&prev->rb_node, &alloc->free_buffers);
690 buffer = prev;
691 }
692 }
693 binder_insert_free_buffer(alloc, buffer);
694}
695
Carlos Llamas59e0d622023-12-01 17:21:44 +0000696/**
697 * binder_alloc_get_page() - get kernel pointer for given buffer offset
698 * @alloc: binder_alloc for this proc
699 * @buffer: binder buffer to be accessed
700 * @buffer_offset: offset into @buffer data
701 * @pgoffp: address to copy final page offset to
702 *
703 * Lookup the struct page corresponding to the address
704 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
705 * NULL, the byte-offset into the page is written there.
706 *
707 * The caller is responsible to ensure that the offset points
708 * to a valid address within the @buffer and that @buffer is
709 * not freeable by the user. Since it can't be freed, we are
710 * guaranteed that the corresponding elements of @alloc->pages[]
711 * cannot change.
712 *
713 * Return: struct page
714 */
715static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
716 struct binder_buffer *buffer,
717 binder_size_t buffer_offset,
718 pgoff_t *pgoffp)
719{
720 binder_size_t buffer_space_offset = buffer_offset +
721 (buffer->user_data - alloc->buffer);
722 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
723 size_t index = buffer_space_offset >> PAGE_SHIFT;
724 struct binder_lru_page *lru_page;
725
726 lru_page = &alloc->pages[index];
727 *pgoffp = pgoff;
728 return lru_page->page_ptr;
729}
730
731/**
732 * binder_alloc_clear_buf() - zero out buffer
733 * @alloc: binder_alloc for this proc
734 * @buffer: binder buffer to be cleared
735 *
736 * memset the given buffer to 0
737 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800738static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000739 struct binder_buffer *buffer)
740{
741 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
742 binder_size_t buffer_offset = 0;
743
744 while (bytes) {
745 unsigned long size;
746 struct page *page;
747 pgoff_t pgoff;
748 void *kptr;
749
750 page = binder_alloc_get_page(alloc, buffer,
751 buffer_offset, &pgoff);
752 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
753 kptr = kmap(page) + pgoff;
754 memset(kptr, 0, size);
755 kunmap(page);
756 bytes -= size;
757 buffer_offset += size;
758 }
759}
760
761
Todd Kjos0c972a02017-06-29 12:01:41 -0700762/**
763 * binder_alloc_free_buf() - free a binder buffer
764 * @alloc: binder_alloc for this proc
765 * @buffer: kernel pointer to buffer
766 *
YangHui4b463822020-08-18 09:34:04 +0800767 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700768 */
769void binder_alloc_free_buf(struct binder_alloc *alloc,
770 struct binder_buffer *buffer)
771{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800772 /*
773 * We could eliminate the call to binder_alloc_clear_buf()
774 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000775 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800776 * increase contention for the alloc mutex if clear_on_free
777 * is used frequently for large buffers. The mutex is not
778 * needed for correctness here.
779 */
780 if (buffer->clear_on_free) {
781 binder_alloc_clear_buf(alloc, buffer);
782 buffer->clear_on_free = false;
783 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700784 mutex_lock(&alloc->mutex);
785 binder_free_buf_locked(alloc, buffer);
786 mutex_unlock(&alloc->mutex);
787}
788
789/**
790 * binder_alloc_mmap_handler() - map virtual address space for proc
791 * @alloc: alloc structure for this proc
792 * @vma: vma passed to mmap()
793 *
794 * Called by binder_mmap() to initialize the space specified in
795 * vma for allocating binder buffers
796 *
797 * Return:
798 * 0 = success
799 * -EBUSY = address space already mapped
800 * -ENOMEM = failed to map memory to given address space
801 */
802int binder_alloc_mmap_handler(struct binder_alloc *alloc,
803 struct vm_area_struct *vma)
804{
Todd Kjos0c972a02017-06-29 12:01:41 -0700805 struct binder_buffer *buffer;
Carlos Llamasaf711932023-12-01 17:21:47 +0000806 const char *failure_string;
807 int ret, i;
Todd Kjos0c972a02017-06-29 12:01:41 -0700808
Carlos Llamasd276fb42022-11-04 23:12:35 +0000809 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
810 ret = -EINVAL;
811 failure_string = "invalid vma->vm_mm";
812 goto err_invalid_mm;
813 }
814
Todd Kjos0c972a02017-06-29 12:01:41 -0700815 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200816 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700817 ret = -EBUSY;
818 failure_string = "already mapped";
819 goto err_already_mapped;
820 }
Jann Horn45d02f72019-10-16 17:01:18 +0200821 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
822 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200823 mutex_unlock(&binder_alloc_mmap_lock);
824
Carlos Llamasc38a8982023-12-01 17:21:38 +0000825 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200826
Jann Horn45d02f72019-10-16 17:01:18 +0200827 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700828 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700829 GFP_KERNEL);
830 if (alloc->pages == NULL) {
831 ret = -ENOMEM;
832 failure_string = "alloc page array";
833 goto err_alloc_pages_failed;
834 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700835
Carlos Llamasaf711932023-12-01 17:21:47 +0000836 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
837 alloc->pages[i].alloc = alloc;
838 INIT_LIST_HEAD(&alloc->pages[i].lru);
839 }
840
Sherry Yang74310e02017-08-23 08:46:41 -0700841 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
842 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700843 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700844 failure_string = "alloc buffer struct";
845 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700846 }
Sherry Yang74310e02017-08-23 08:46:41 -0700847
Todd Kjosbde4a192019-02-08 10:35:20 -0800848 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700849 list_add(&buffer->entry, &alloc->buffers);
850 buffer->free = 1;
851 binder_insert_free_buffer(alloc, buffer);
852 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000853
854 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900855 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700856
857 return 0;
858
Sherry Yang74310e02017-08-23 08:46:41 -0700859err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700860 kfree(alloc->pages);
861 alloc->pages = NULL;
862err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000863 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200864 mutex_lock(&binder_alloc_mmap_lock);
865 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700866err_already_mapped:
867 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000868err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700869 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
870 "%s: %d %lx-%lx %s failed %d\n", __func__,
871 alloc->pid, vma->vm_start, vma->vm_end,
872 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700873 return ret;
874}
875
876
877void binder_alloc_deferred_release(struct binder_alloc *alloc)
878{
879 struct rb_node *n;
880 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700881 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700882
Todd Kjos0c972a02017-06-29 12:01:41 -0700883 buffers = 0;
884 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000885 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900886
Todd Kjos0c972a02017-06-29 12:01:41 -0700887 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700888 buffer = rb_entry(n, struct binder_buffer, rb_node);
889
890 /* Transaction should already have been freed */
891 BUG_ON(buffer->transaction);
892
Todd Kjos0f966cb2020-11-20 15:37:43 -0800893 if (buffer->clear_on_free) {
894 binder_alloc_clear_buf(alloc, buffer);
895 buffer->clear_on_free = false;
896 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700897 binder_free_buf_locked(alloc, buffer);
898 buffers++;
899 }
900
Sherry Yang74310e02017-08-23 08:46:41 -0700901 while (!list_empty(&alloc->buffers)) {
902 buffer = list_first_entry(&alloc->buffers,
903 struct binder_buffer, entry);
904 WARN_ON(!buffer->free);
905
906 list_del(&buffer->entry);
907 WARN_ON_ONCE(!list_empty(&alloc->buffers));
908 kfree(buffer);
909 }
910
Todd Kjos0c972a02017-06-29 12:01:41 -0700911 page_count = 0;
912 if (alloc->pages) {
913 int i;
914
915 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000916 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700917 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700918
Sherry Yangf2517eb2017-08-23 08:46:42 -0700919 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700920 continue;
921
Sherry Yangf2517eb2017-08-23 08:46:42 -0700922 on_lru = list_lru_del(&binder_alloc_lru,
923 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700924 page_addr = alloc->buffer + i * PAGE_SIZE;
925 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000926 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700927 __func__, alloc->pid, i, page_addr,
928 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700929 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700930 page_count++;
931 }
932 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700933 }
934 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400935 if (alloc->vma_vm_mm)
936 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700937
938 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
939 "%s: %d buffers %d, pages %d\n",
940 __func__, alloc->pid, buffers, page_count);
941}
942
943static void print_binder_buffer(struct seq_file *m, const char *prefix,
944 struct binder_buffer *buffer)
945{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000946 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800947 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700948 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700949 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700950 buffer->transaction ? "active" : "delivered");
951}
952
953/**
954 * binder_alloc_print_allocated() - print buffer info
955 * @m: seq_file for output via seq_printf()
956 * @alloc: binder_alloc for this proc
957 *
958 * Prints information about every buffer associated with
959 * the binder_alloc state to the given seq_file
960 */
961void binder_alloc_print_allocated(struct seq_file *m,
962 struct binder_alloc *alloc)
963{
964 struct rb_node *n;
965
966 mutex_lock(&alloc->mutex);
967 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
968 print_binder_buffer(m, " buffer",
969 rb_entry(n, struct binder_buffer, rb_node));
970 mutex_unlock(&alloc->mutex);
971}
972
973/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700974 * binder_alloc_print_pages() - print page usage
975 * @m: seq_file for output via seq_printf()
976 * @alloc: binder_alloc for this proc
977 */
978void binder_alloc_print_pages(struct seq_file *m,
979 struct binder_alloc *alloc)
980{
981 struct binder_lru_page *page;
982 int i;
983 int active = 0;
984 int lru = 0;
985 int free = 0;
986
987 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200988 /*
989 * Make sure the binder_alloc is fully initialized, otherwise we might
990 * read inconsistent state.
991 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000992 if (binder_alloc_get_vma(alloc) != NULL) {
993 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
994 page = &alloc->pages[i];
995 if (!page->page_ptr)
996 free++;
997 else if (list_empty(&page->lru))
998 active++;
999 else
1000 lru++;
1001 }
Sherry Yang8ef46652017-08-31 11:56:36 -07001002 }
1003 mutex_unlock(&alloc->mutex);
1004 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001005 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001006}
1007
1008/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001009 * binder_alloc_get_allocated_count() - return count of buffers
1010 * @alloc: binder_alloc for this proc
1011 *
1012 * Return: count of allocated buffers
1013 */
1014int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1015{
1016 struct rb_node *n;
1017 int count = 0;
1018
1019 mutex_lock(&alloc->mutex);
1020 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1021 count++;
1022 mutex_unlock(&alloc->mutex);
1023 return count;
1024}
1025
1026
1027/**
1028 * binder_alloc_vma_close() - invalidate address space
1029 * @alloc: binder_alloc for this proc
1030 *
1031 * Called from binder_vma_close() when releasing address space.
1032 * Clears alloc->vma to prevent new incoming transactions from
1033 * allocating more buffers.
1034 */
1035void binder_alloc_vma_close(struct binder_alloc *alloc)
1036{
Minchan Kimda1b9562018-08-23 14:29:56 +09001037 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001038}
1039
1040/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001041 * binder_alloc_free_page() - shrinker callback to free pages
1042 * @item: item to free
1043 * @lock: lock protecting the item
1044 * @cb_arg: callback argument
1045 *
1046 * Called from list_lru_walk() in binder_shrink_scan() to free
1047 * up pages when the system is under memory pressure.
1048 */
1049enum lru_status binder_alloc_free_page(struct list_head *item,
1050 struct list_lru_one *lru,
1051 spinlock_t *lock,
1052 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001053 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001054{
1055 struct mm_struct *mm = NULL;
1056 struct binder_lru_page *page = container_of(item,
1057 struct binder_lru_page,
1058 lru);
1059 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001060 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001061 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -07001062 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001063
1064 alloc = page->alloc;
1065 if (!mutex_trylock(&alloc->mutex))
1066 goto err_get_alloc_mutex_failed;
1067
1068 if (!page->page_ptr)
1069 goto err_page_already_freed;
1070
1071 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001072 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001073
1074 mm = alloc->vma_vm_mm;
1075 if (!mmget_not_zero(mm))
1076 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001077 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001078 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001079 vma = vma_lookup(mm, page_addr);
1080 if (vma && vma != binder_alloc_get_vma(alloc))
1081 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001082
Sherry Yanga1b22892017-10-03 16:15:00 -07001083 list_lru_isolate(lru, item);
1084 spin_unlock(lock);
1085
1086 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001087 trace_binder_unmap_user_start(alloc, index);
1088
Todd Kjosc41358a2019-02-08 10:35:19 -08001089 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001090
Sherry Yange41e1642017-08-23 08:46:43 -07001091 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001092 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001093 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001094 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001095
Sherry Yange41e1642017-08-23 08:46:43 -07001096 trace_binder_unmap_kernel_start(alloc, index);
1097
Sherry Yangf2517eb2017-08-23 08:46:42 -07001098 __free_page(page->page_ptr);
1099 page->page_ptr = NULL;
1100
Sherry Yange41e1642017-08-23 08:46:43 -07001101 trace_binder_unmap_kernel_end(alloc, index);
1102
Sherry Yanga1b22892017-10-03 16:15:00 -07001103 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001104 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001105 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001106
Carlos Llamas8dce2882023-12-01 17:21:31 +00001107err_invalid_vma:
1108 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001109err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001110 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001111err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001112err_page_already_freed:
1113 mutex_unlock(&alloc->mutex);
1114err_get_alloc_mutex_failed:
1115 return LRU_SKIP;
1116}
1117
1118static unsigned long
1119binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1120{
1121 unsigned long ret = list_lru_count(&binder_alloc_lru);
1122 return ret;
1123}
1124
1125static unsigned long
1126binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1127{
1128 unsigned long ret;
1129
1130 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1131 NULL, sc->nr_to_scan);
1132 return ret;
1133}
1134
Sherry Yangde7bbe32017-10-06 16:12:05 -04001135static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001136 .count_objects = binder_shrink_count,
1137 .scan_objects = binder_shrink_scan,
1138 .seeks = DEFAULT_SEEKS,
1139};
1140
1141/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001142 * binder_alloc_init() - called by binder_open() for per-proc initialization
1143 * @alloc: binder_alloc for this proc
1144 *
1145 * Called from binder_open() to initialize binder_alloc fields for
1146 * new binder proc
1147 */
1148void binder_alloc_init(struct binder_alloc *alloc)
1149{
Todd Kjos0c972a02017-06-29 12:01:41 -07001150 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001151 alloc->vma_vm_mm = current->mm;
1152 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001153 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001154 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001155}
1156
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001157int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001158{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001159 int ret = list_lru_init(&binder_alloc_lru);
1160
1161 if (ret == 0) {
1162 ret = register_shrinker(&binder_shrinker);
1163 if (ret)
1164 list_lru_destroy(&binder_alloc_lru);
1165 }
1166 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001167}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001168
Qi Zheng03eebad2023-06-25 15:49:37 +00001169void binder_alloc_shrinker_exit(void)
1170{
1171 unregister_shrinker(&binder_shrinker);
1172 list_lru_destroy(&binder_alloc_lru);
1173}
1174
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001175/**
1176 * check_buffer() - verify that buffer/offset is safe to access
1177 * @alloc: binder_alloc for this proc
1178 * @buffer: binder buffer to be accessed
1179 * @offset: offset into @buffer data
1180 * @bytes: bytes to access from offset
1181 *
1182 * Check that the @offset/@bytes are within the size of the given
1183 * @buffer and that the buffer is currently active and not freeable.
1184 * Offsets must also be multiples of sizeof(u32). The kernel is
1185 * allowed to touch the buffer in two cases:
1186 *
1187 * 1) when the buffer is being created:
1188 * (buffer->free == 0 && buffer->allow_user_free == 0)
1189 * 2) when the buffer is being torn down:
1190 * (buffer->free == 0 && buffer->transaction == NULL).
1191 *
1192 * Return: true if the buffer is safe to access
1193 */
1194static inline bool check_buffer(struct binder_alloc *alloc,
1195 struct binder_buffer *buffer,
1196 binder_size_t offset, size_t bytes)
1197{
1198 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1199
1200 return buffer_size >= bytes &&
1201 offset <= buffer_size - bytes &&
1202 IS_ALIGNED(offset, sizeof(u32)) &&
1203 !buffer->free &&
1204 (!buffer->allow_user_free || !buffer->transaction);
1205}
1206
1207/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001208 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1209 * @alloc: binder_alloc for this proc
1210 * @buffer: binder buffer to be accessed
1211 * @buffer_offset: offset into @buffer data
1212 * @from: userspace pointer to source buffer
1213 * @bytes: bytes to copy
1214 *
1215 * Copy bytes from source userspace to target buffer.
1216 *
1217 * Return: bytes remaining to be copied
1218 */
1219unsigned long
1220binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1221 struct binder_buffer *buffer,
1222 binder_size_t buffer_offset,
1223 const void __user *from,
1224 size_t bytes)
1225{
1226 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1227 return bytes;
1228
1229 while (bytes) {
1230 unsigned long size;
1231 unsigned long ret;
1232 struct page *page;
1233 pgoff_t pgoff;
1234 void *kptr;
1235
1236 page = binder_alloc_get_page(alloc, buffer,
1237 buffer_offset, &pgoff);
1238 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1239 kptr = kmap(page) + pgoff;
1240 ret = copy_from_user(kptr, from, size);
1241 kunmap(page);
1242 if (ret)
1243 return bytes - size + ret;
1244 bytes -= size;
1245 from += size;
1246 buffer_offset += size;
1247 }
1248 return 0;
1249}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001250
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001251static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1252 bool to_buffer,
1253 struct binder_buffer *buffer,
1254 binder_size_t buffer_offset,
1255 void *ptr,
1256 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001257{
1258 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001259 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1260 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001261
1262 while (bytes) {
1263 unsigned long size;
1264 struct page *page;
1265 pgoff_t pgoff;
1266 void *tmpptr;
1267 void *base_ptr;
1268
1269 page = binder_alloc_get_page(alloc, buffer,
1270 buffer_offset, &pgoff);
1271 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1272 base_ptr = kmap_atomic(page);
1273 tmpptr = base_ptr + pgoff;
1274 if (to_buffer)
1275 memcpy(tmpptr, ptr, size);
1276 else
1277 memcpy(ptr, tmpptr, size);
1278 /*
1279 * kunmap_atomic() takes care of flushing the cache
1280 * if this device has VIVT cache arch
1281 */
1282 kunmap_atomic(base_ptr);
1283 bytes -= size;
1284 pgoff = 0;
1285 ptr = ptr + size;
1286 buffer_offset += size;
1287 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001288 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001289}
1290
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001291int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1292 struct binder_buffer *buffer,
1293 binder_size_t buffer_offset,
1294 void *src,
1295 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001296{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001297 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1298 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001299}
1300
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001301int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1302 void *dest,
1303 struct binder_buffer *buffer,
1304 binder_size_t buffer_offset,
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, false, buffer, buffer_offset,
1308 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001309}
1310