blob: de880cd2e775c974a44a3727dbc09e0a9cb9cf10 [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
203static int binder_allocate_page_range(struct binder_alloc *alloc,
204 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700205{
Sherry Yang6ae33b92017-09-16 01:11:56 -0400206 struct vm_area_struct *vma = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000207 struct binder_lru_page *page;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700208 struct mm_struct *mm = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000209 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700210 bool need_mm = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700211
212 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamas0b243682023-12-01 17:21:39 +0000213 "%d: allocate pages %lx-%lx\n",
214 alloc->pid, start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700215
216 if (end <= start)
217 return 0;
218
Carlos Llamas0b243682023-12-01 17:21:39 +0000219 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700220
221 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
222 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
223 if (!page->page_ptr) {
224 need_mm = true;
225 break;
226 }
227 }
228
Greg Kroah-Hartman6fbf2482017-10-23 17:21:44 +0200229 if (need_mm && mmget_not_zero(alloc->vma_vm_mm))
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400230 mm = alloc->vma_vm_mm;
Todd Kjos0c972a02017-06-29 12:01:41 -0700231
232 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000233 mmap_write_lock(mm);
Carlos Llamasacd81932023-05-30 19:43:36 +0000234 vma = alloc->vma;
Todd Kjos0c972a02017-06-29 12:01:41 -0700235 }
236
Sherry Yangf2517eb2017-08-23 08:46:42 -0700237 if (!vma && need_mm) {
Sherry Yang128f3802018-08-07 12:57:13 -0700238 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
239 "%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
240 alloc->pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700241 goto err_no_vma;
242 }
243
244 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
245 int ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700246 bool on_lru;
Sherry Yange41e1642017-08-23 08:46:43 -0700247 size_t index;
Todd Kjos0c972a02017-06-29 12:01:41 -0700248
Sherry Yange41e1642017-08-23 08:46:43 -0700249 index = (page_addr - alloc->buffer) / PAGE_SIZE;
250 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700251
Sherry Yangf2517eb2017-08-23 08:46:42 -0700252 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700253 trace_binder_alloc_lru_start(alloc, index);
254
Sherry Yangf2517eb2017-08-23 08:46:42 -0700255 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
256 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700257
258 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700259 continue;
260 }
261
262 if (WARN_ON(!vma))
263 goto err_page_ptr_cleared;
264
Sherry Yange41e1642017-08-23 08:46:43 -0700265 trace_binder_alloc_page_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700266 page->page_ptr = alloc_page(GFP_KERNEL |
267 __GFP_HIGHMEM |
268 __GFP_ZERO);
269 if (!page->page_ptr) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000270 pr_err("%d: binder_alloc_buf failed for page at %lx\n",
271 alloc->pid, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700272 goto err_alloc_page_failed;
273 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700274 page->alloc = alloc;
275 INIT_LIST_HEAD(&page->lru);
276
Carlos Llamasc38a8982023-12-01 17:21:38 +0000277 ret = vm_insert_page(vma, page_addr, page->page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700278 if (ret) {
279 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Carlos Llamasc38a8982023-12-01 17:21:38 +0000280 alloc->pid, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700281 goto err_vm_insert_page_failed;
282 }
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 }
289 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000290 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000291 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700292 }
293 return 0;
294
Todd Kjos0c972a02017-06-29 12:01:41 -0700295err_vm_insert_page_failed:
Carlos Llamas0b243682023-12-01 17:21:39 +0000296 __free_page(page->page_ptr);
297 page->page_ptr = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700298err_alloc_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700299err_page_ptr_cleared:
Carlos Llamas0b243682023-12-01 17:21:39 +0000300 binder_free_page_range(alloc, start, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700301err_no_vma:
302 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000303 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000304 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700305 }
Todd Kjos57ada2f2017-06-29 12:01:46 -0700306 return vma ? -ENOMEM : -ESRCH;
Todd Kjos0c972a02017-06-29 12:01:41 -0700307}
308
Minchan Kimda1b9562018-08-23 14:29:56 +0900309static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
310 struct vm_area_struct *vma)
311{
Carlos Llamasb094b042023-05-30 19:43:37 +0000312 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
313 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900314}
315
316static inline struct vm_area_struct *binder_alloc_get_vma(
317 struct binder_alloc *alloc)
318{
Carlos Llamasb094b042023-05-30 19:43:37 +0000319 /* pairs with smp_store_release in binder_alloc_set_vma() */
320 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900321}
322
Hang Lua7dc1e62021-04-09 17:40:46 +0800323static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid)
Martijn Coenen261e7812020-08-21 14:25:44 +0200324{
325 /*
326 * Find the amount and size of buffers allocated by the current caller;
327 * The idea is that once we cross the threshold, whoever is responsible
328 * for the low async space is likely to try to send another async txn,
329 * and at some point we'll catch them in the act. This is more efficient
330 * than keeping a map per pid.
331 */
Colin Ian King7369fa42020-09-10 16:12:21 +0100332 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200333 struct binder_buffer *buffer;
334 size_t total_alloc_size = 0;
335 size_t num_buffers = 0;
336
337 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
338 n = rb_next(n)) {
339 buffer = rb_entry(n, struct binder_buffer, rb_node);
340 if (buffer->pid != pid)
341 continue;
342 if (!buffer->async_transaction)
343 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000344 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200345 num_buffers++;
346 }
347
348 /*
349 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800350 * async space (which is 25% of total buffer size). Oneway spam is only
351 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200352 */
353 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
354 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
355 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
356 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800357 if (!alloc->oneway_spam_detected) {
358 alloc->oneway_spam_detected = true;
359 return true;
360 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200361 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800362 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200363}
364
Xiongwei Song3f827242017-12-14 12:15:42 +0800365static struct binder_buffer *binder_alloc_new_buf_locked(
366 struct binder_alloc *alloc,
367 size_t data_size,
368 size_t offsets_size,
369 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200370 int is_async,
371 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700372{
373 struct rb_node *n = alloc->free_buffers.rb_node;
374 struct binder_buffer *buffer;
375 size_t buffer_size;
376 struct rb_node *best_fit = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700377 size_t size, data_offsets_size;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000378 unsigned long has_page_addr;
379 unsigned long end_page_addr;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700380 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700381
Carlos Llamasb094b042023-05-30 19:43:37 +0000382 /* Check binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900383 if (!binder_alloc_get_vma(alloc)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700384 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
385 "%d: binder_alloc_buf, no vma\n",
386 alloc->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700387 return ERR_PTR(-ESRCH);
Todd Kjos0c972a02017-06-29 12:01:41 -0700388 }
389
390 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
391 ALIGN(offsets_size, sizeof(void *));
392
393 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
394 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
395 "%d: got transaction with invalid size %zd-%zd\n",
396 alloc->pid, data_size, offsets_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700397 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700398 }
399 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
400 if (size < data_offsets_size || size < extra_buffers_size) {
401 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
402 "%d: got transaction with invalid extra_buffers_size %zd\n",
403 alloc->pid, extra_buffers_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700404 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700405 }
Zhuguangqing1174e452021-03-09 15:47:43 +0800406 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000407
408 /* Pad 0-size buffers so they get assigned unique addresses */
409 size = max(size, sizeof(void *));
410
Carlos Llamas11ca0762023-12-01 17:21:34 +0000411 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700412 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
413 "%d: binder_alloc_buf size %zd failed, no async space left\n",
414 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700415 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700416 }
417
418 while (n) {
419 buffer = rb_entry(n, struct binder_buffer, rb_node);
420 BUG_ON(!buffer->free);
421 buffer_size = binder_alloc_buffer_size(alloc, buffer);
422
423 if (size < buffer_size) {
424 best_fit = n;
425 n = n->rb_left;
426 } else if (size > buffer_size)
427 n = n->rb_right;
428 else {
429 best_fit = n;
430 break;
431 }
432 }
433 if (best_fit == NULL) {
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700434 size_t allocated_buffers = 0;
435 size_t largest_alloc_size = 0;
436 size_t total_alloc_size = 0;
437 size_t free_buffers = 0;
438 size_t largest_free_size = 0;
439 size_t total_free_size = 0;
440
441 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
442 n = rb_next(n)) {
443 buffer = rb_entry(n, struct binder_buffer, rb_node);
444 buffer_size = binder_alloc_buffer_size(alloc, buffer);
445 allocated_buffers++;
446 total_alloc_size += buffer_size;
447 if (buffer_size > largest_alloc_size)
448 largest_alloc_size = buffer_size;
449 }
450 for (n = rb_first(&alloc->free_buffers); n != NULL;
451 n = rb_next(n)) {
452 buffer = rb_entry(n, struct binder_buffer, rb_node);
453 buffer_size = binder_alloc_buffer_size(alloc, buffer);
454 free_buffers++;
455 total_free_size += buffer_size;
456 if (buffer_size > largest_free_size)
457 largest_free_size = buffer_size;
458 }
Sherry Yang128f3802018-08-07 12:57:13 -0700459 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
460 "%d: binder_alloc_buf size %zd failed, no address space\n",
461 alloc->pid, size);
462 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
463 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
464 total_alloc_size, allocated_buffers,
465 largest_alloc_size, total_free_size,
466 free_buffers, largest_free_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700467 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700468 }
469 if (n == NULL) {
470 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
471 buffer_size = binder_alloc_buffer_size(alloc, buffer);
472 }
473
474 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
475 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
476 alloc->pid, size, buffer, buffer_size);
477
Carlos Llamasc38a8982023-12-01 17:21:38 +0000478 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
Sherry Yang74310e02017-08-23 08:46:41 -0700479 WARN_ON(n && buffer_size != size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000480 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700481 if (end_page_addr > has_page_addr)
482 end_page_addr = has_page_addr;
Carlos Llamas0b243682023-12-01 17:21:39 +0000483 ret = binder_allocate_page_range(alloc, PAGE_ALIGN(buffer->user_data),
484 end_page_addr);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700485 if (ret)
486 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700487
Todd Kjos0c972a02017-06-29 12:01:41 -0700488 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700489 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700490
Sherry Yang74310e02017-08-23 08:46:41 -0700491 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
492 if (!new_buffer) {
493 pr_err("%s: %d failed to alloc new buffer struct\n",
494 __func__, alloc->pid);
495 goto err_alloc_buf_struct_failed;
496 }
Carlos Llamasc38a8982023-12-01 17:21:38 +0000497 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700498 list_add(&new_buffer->entry, &buffer->entry);
499 new_buffer->free = 1;
500 binder_insert_free_buffer(alloc, new_buffer);
501 }
Sherry Yang74310e02017-08-23 08:46:41 -0700502
503 rb_erase(best_fit, &alloc->free_buffers);
504 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800505 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700506 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700507 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
508 "%d: binder_alloc_buf size %zd got %pK\n",
509 alloc->pid, size, buffer);
510 buffer->data_size = data_size;
511 buffer->offsets_size = offsets_size;
512 buffer->async_transaction = is_async;
513 buffer->extra_buffers_size = extra_buffers_size;
Martijn Coenen261e7812020-08-21 14:25:44 +0200514 buffer->pid = pid;
Hang Lua7dc1e62021-04-09 17:40:46 +0800515 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700516 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000517 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700518 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
519 "%d: binder_alloc_buf size %zd async free %zd\n",
520 alloc->pid, size, alloc->free_async_space);
Martijn Coenen261e7812020-08-21 14:25:44 +0200521 if (alloc->free_async_space < alloc->buffer_size / 10) {
522 /*
523 * Start detecting spammers once we have less than 20%
524 * of async space left (which is less than 10% of total
525 * buffer size).
526 */
Hang Lua7dc1e62021-04-09 17:40:46 +0800527 buffer->oneway_spam_suspect = debug_low_async_space_locked(alloc, pid);
528 } else {
529 alloc->oneway_spam_detected = false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200530 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700531 }
532 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700533
534err_alloc_buf_struct_failed:
Carlos Llamas0b243682023-12-01 17:21:39 +0000535 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
536 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700537 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700538}
539
540/**
541 * binder_alloc_new_buf() - Allocate a new binder buffer
542 * @alloc: binder_alloc for this proc
543 * @data_size: size of user data buffer
544 * @offsets_size: user specified buffer offset
545 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
546 * @is_async: buffer for async transaction
Martijn Coenen261e7812020-08-21 14:25:44 +0200547 * @pid: pid to attribute allocation to (used for debugging)
Todd Kjos0c972a02017-06-29 12:01:41 -0700548 *
549 * Allocate a new buffer given the requested sizes. Returns
550 * the kernel version of the buffer pointer. The size allocated
551 * is the sum of the three given sizes (each rounded up to
552 * pointer-sized boundary)
553 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000554 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700555 */
556struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
557 size_t data_size,
558 size_t offsets_size,
559 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200560 int is_async,
561 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700562{
563 struct binder_buffer *buffer;
564
565 mutex_lock(&alloc->mutex);
566 buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200567 extra_buffers_size, is_async, pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700568 mutex_unlock(&alloc->mutex);
569 return buffer;
570}
571
Carlos Llamasc38a8982023-12-01 17:21:38 +0000572static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700573{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000574 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700575}
576
Carlos Llamasc38a8982023-12-01 17:21:38 +0000577static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700578{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000579 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700580}
581
582static void binder_delete_free_buffer(struct binder_alloc *alloc,
583 struct binder_buffer *buffer)
584{
585 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700586 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530587
Todd Kjos0c972a02017-06-29 12:01:41 -0700588 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700589 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700590 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700591 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
592 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700593 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000594 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800595 alloc->pid, buffer->user_data,
596 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700597 }
598
599 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700600 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700601 if (buffer_start_page(next) == buffer_start_page(buffer)) {
602 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700603 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000604 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700605 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800606 buffer->user_data,
607 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700608 }
609 }
Sherry Yang74310e02017-08-23 08:46:41 -0700610
Todd Kjosbde4a192019-02-08 10:35:20 -0800611 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700612 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000613 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800614 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700615 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700616 }
Sherry Yang74310e02017-08-23 08:46:41 -0700617
618 if (to_free) {
619 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000620 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800621 alloc->pid, buffer->user_data,
622 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000623 next ? next->user_data : 0);
Carlos Llamas0b243682023-12-01 17:21:39 +0000624 binder_free_page_range(alloc, buffer_start_page(buffer),
625 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700626 }
627 list_del(&buffer->entry);
628 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700629}
630
631static void binder_free_buf_locked(struct binder_alloc *alloc,
632 struct binder_buffer *buffer)
633{
634 size_t size, buffer_size;
635
636 buffer_size = binder_alloc_buffer_size(alloc, buffer);
637
638 size = ALIGN(buffer->data_size, sizeof(void *)) +
639 ALIGN(buffer->offsets_size, sizeof(void *)) +
640 ALIGN(buffer->extra_buffers_size, sizeof(void *));
641
642 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
643 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
644 alloc->pid, buffer, size, buffer_size);
645
646 BUG_ON(buffer->free);
647 BUG_ON(size > buffer_size);
648 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800649 BUG_ON(buffer->user_data < alloc->buffer);
650 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700651
652 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000653 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700654 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
655 "%d: binder_free_buf size %zd async free %zd\n",
656 alloc->pid, size, alloc->free_async_space);
657 }
658
Carlos Llamas0b243682023-12-01 17:21:39 +0000659 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
660 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700661
662 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
663 buffer->free = 1;
664 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700665 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700666
667 if (next->free) {
668 rb_erase(&next->rb_node, &alloc->free_buffers);
669 binder_delete_free_buffer(alloc, next);
670 }
671 }
672 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700673 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700674
675 if (prev->free) {
676 binder_delete_free_buffer(alloc, buffer);
677 rb_erase(&prev->rb_node, &alloc->free_buffers);
678 buffer = prev;
679 }
680 }
681 binder_insert_free_buffer(alloc, buffer);
682}
683
Todd Kjos0f966cb2020-11-20 15:37:43 -0800684static void binder_alloc_clear_buf(struct binder_alloc *alloc,
685 struct binder_buffer *buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700686/**
687 * binder_alloc_free_buf() - free a binder buffer
688 * @alloc: binder_alloc for this proc
689 * @buffer: kernel pointer to buffer
690 *
YangHui4b463822020-08-18 09:34:04 +0800691 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700692 */
693void binder_alloc_free_buf(struct binder_alloc *alloc,
694 struct binder_buffer *buffer)
695{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800696 /*
697 * We could eliminate the call to binder_alloc_clear_buf()
698 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000699 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800700 * increase contention for the alloc mutex if clear_on_free
701 * is used frequently for large buffers. The mutex is not
702 * needed for correctness here.
703 */
704 if (buffer->clear_on_free) {
705 binder_alloc_clear_buf(alloc, buffer);
706 buffer->clear_on_free = false;
707 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700708 mutex_lock(&alloc->mutex);
709 binder_free_buf_locked(alloc, buffer);
710 mutex_unlock(&alloc->mutex);
711}
712
713/**
714 * binder_alloc_mmap_handler() - map virtual address space for proc
715 * @alloc: alloc structure for this proc
716 * @vma: vma passed to mmap()
717 *
718 * Called by binder_mmap() to initialize the space specified in
719 * vma for allocating binder buffers
720 *
721 * Return:
722 * 0 = success
723 * -EBUSY = address space already mapped
724 * -ENOMEM = failed to map memory to given address space
725 */
726int binder_alloc_mmap_handler(struct binder_alloc *alloc,
727 struct vm_area_struct *vma)
728{
729 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700730 const char *failure_string;
731 struct binder_buffer *buffer;
732
Carlos Llamasd276fb42022-11-04 23:12:35 +0000733 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
734 ret = -EINVAL;
735 failure_string = "invalid vma->vm_mm";
736 goto err_invalid_mm;
737 }
738
Todd Kjos0c972a02017-06-29 12:01:41 -0700739 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200740 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700741 ret = -EBUSY;
742 failure_string = "already mapped";
743 goto err_already_mapped;
744 }
Jann Horn45d02f72019-10-16 17:01:18 +0200745 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
746 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200747 mutex_unlock(&binder_alloc_mmap_lock);
748
Carlos Llamasc38a8982023-12-01 17:21:38 +0000749 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200750
Jann Horn45d02f72019-10-16 17:01:18 +0200751 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700752 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700753 GFP_KERNEL);
754 if (alloc->pages == NULL) {
755 ret = -ENOMEM;
756 failure_string = "alloc page array";
757 goto err_alloc_pages_failed;
758 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700759
Sherry Yang74310e02017-08-23 08:46:41 -0700760 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
761 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700762 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700763 failure_string = "alloc buffer struct";
764 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700765 }
Sherry Yang74310e02017-08-23 08:46:41 -0700766
Todd Kjosbde4a192019-02-08 10:35:20 -0800767 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700768 list_add(&buffer->entry, &alloc->buffers);
769 buffer->free = 1;
770 binder_insert_free_buffer(alloc, buffer);
771 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000772
773 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900774 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700775
776 return 0;
777
Sherry Yang74310e02017-08-23 08:46:41 -0700778err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700779 kfree(alloc->pages);
780 alloc->pages = NULL;
781err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000782 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200783 mutex_lock(&binder_alloc_mmap_lock);
784 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700785err_already_mapped:
786 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000787err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700788 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
789 "%s: %d %lx-%lx %s failed %d\n", __func__,
790 alloc->pid, vma->vm_start, vma->vm_end,
791 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700792 return ret;
793}
794
795
796void binder_alloc_deferred_release(struct binder_alloc *alloc)
797{
798 struct rb_node *n;
799 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700800 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700801
Todd Kjos0c972a02017-06-29 12:01:41 -0700802 buffers = 0;
803 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000804 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900805
Todd Kjos0c972a02017-06-29 12:01:41 -0700806 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700807 buffer = rb_entry(n, struct binder_buffer, rb_node);
808
809 /* Transaction should already have been freed */
810 BUG_ON(buffer->transaction);
811
Todd Kjos0f966cb2020-11-20 15:37:43 -0800812 if (buffer->clear_on_free) {
813 binder_alloc_clear_buf(alloc, buffer);
814 buffer->clear_on_free = false;
815 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700816 binder_free_buf_locked(alloc, buffer);
817 buffers++;
818 }
819
Sherry Yang74310e02017-08-23 08:46:41 -0700820 while (!list_empty(&alloc->buffers)) {
821 buffer = list_first_entry(&alloc->buffers,
822 struct binder_buffer, entry);
823 WARN_ON(!buffer->free);
824
825 list_del(&buffer->entry);
826 WARN_ON_ONCE(!list_empty(&alloc->buffers));
827 kfree(buffer);
828 }
829
Todd Kjos0c972a02017-06-29 12:01:41 -0700830 page_count = 0;
831 if (alloc->pages) {
832 int i;
833
834 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000835 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700836 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700837
Sherry Yangf2517eb2017-08-23 08:46:42 -0700838 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700839 continue;
840
Sherry Yangf2517eb2017-08-23 08:46:42 -0700841 on_lru = list_lru_del(&binder_alloc_lru,
842 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700843 page_addr = alloc->buffer + i * PAGE_SIZE;
844 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000845 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700846 __func__, alloc->pid, i, page_addr,
847 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700848 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700849 page_count++;
850 }
851 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700852 }
853 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400854 if (alloc->vma_vm_mm)
855 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700856
857 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
858 "%s: %d buffers %d, pages %d\n",
859 __func__, alloc->pid, buffers, page_count);
860}
861
862static void print_binder_buffer(struct seq_file *m, const char *prefix,
863 struct binder_buffer *buffer)
864{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000865 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800866 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700867 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700868 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700869 buffer->transaction ? "active" : "delivered");
870}
871
872/**
873 * binder_alloc_print_allocated() - print buffer info
874 * @m: seq_file for output via seq_printf()
875 * @alloc: binder_alloc for this proc
876 *
877 * Prints information about every buffer associated with
878 * the binder_alloc state to the given seq_file
879 */
880void binder_alloc_print_allocated(struct seq_file *m,
881 struct binder_alloc *alloc)
882{
883 struct rb_node *n;
884
885 mutex_lock(&alloc->mutex);
886 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
887 print_binder_buffer(m, " buffer",
888 rb_entry(n, struct binder_buffer, rb_node));
889 mutex_unlock(&alloc->mutex);
890}
891
892/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700893 * binder_alloc_print_pages() - print page usage
894 * @m: seq_file for output via seq_printf()
895 * @alloc: binder_alloc for this proc
896 */
897void binder_alloc_print_pages(struct seq_file *m,
898 struct binder_alloc *alloc)
899{
900 struct binder_lru_page *page;
901 int i;
902 int active = 0;
903 int lru = 0;
904 int free = 0;
905
906 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200907 /*
908 * Make sure the binder_alloc is fully initialized, otherwise we might
909 * read inconsistent state.
910 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000911 if (binder_alloc_get_vma(alloc) != NULL) {
912 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
913 page = &alloc->pages[i];
914 if (!page->page_ptr)
915 free++;
916 else if (list_empty(&page->lru))
917 active++;
918 else
919 lru++;
920 }
Sherry Yang8ef46652017-08-31 11:56:36 -0700921 }
922 mutex_unlock(&alloc->mutex);
923 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100924 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -0700925}
926
927/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700928 * binder_alloc_get_allocated_count() - return count of buffers
929 * @alloc: binder_alloc for this proc
930 *
931 * Return: count of allocated buffers
932 */
933int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
934{
935 struct rb_node *n;
936 int count = 0;
937
938 mutex_lock(&alloc->mutex);
939 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
940 count++;
941 mutex_unlock(&alloc->mutex);
942 return count;
943}
944
945
946/**
947 * binder_alloc_vma_close() - invalidate address space
948 * @alloc: binder_alloc for this proc
949 *
950 * Called from binder_vma_close() when releasing address space.
951 * Clears alloc->vma to prevent new incoming transactions from
952 * allocating more buffers.
953 */
954void binder_alloc_vma_close(struct binder_alloc *alloc)
955{
Minchan Kimda1b9562018-08-23 14:29:56 +0900956 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700957}
958
959/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700960 * binder_alloc_free_page() - shrinker callback to free pages
961 * @item: item to free
962 * @lock: lock protecting the item
963 * @cb_arg: callback argument
964 *
965 * Called from list_lru_walk() in binder_shrink_scan() to free
966 * up pages when the system is under memory pressure.
967 */
968enum lru_status binder_alloc_free_page(struct list_head *item,
969 struct list_lru_one *lru,
970 spinlock_t *lock,
971 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -0800972 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -0700973{
974 struct mm_struct *mm = NULL;
975 struct binder_lru_page *page = container_of(item,
976 struct binder_lru_page,
977 lru);
978 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000979 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700980 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -0700981 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700982
983 alloc = page->alloc;
984 if (!mutex_trylock(&alloc->mutex))
985 goto err_get_alloc_mutex_failed;
986
987 if (!page->page_ptr)
988 goto err_page_already_freed;
989
990 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000991 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -0800992
993 mm = alloc->vma_vm_mm;
994 if (!mmget_not_zero(mm))
995 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700996 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -0700997 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +0000998 vma = vma_lookup(mm, page_addr);
999 if (vma && vma != binder_alloc_get_vma(alloc))
1000 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001001
Sherry Yanga1b22892017-10-03 16:15:00 -07001002 list_lru_isolate(lru, item);
1003 spin_unlock(lock);
1004
1005 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001006 trace_binder_unmap_user_start(alloc, index);
1007
Todd Kjosc41358a2019-02-08 10:35:19 -08001008 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001009
Sherry Yange41e1642017-08-23 08:46:43 -07001010 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001011 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001012 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001013 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001014
Sherry Yange41e1642017-08-23 08:46:43 -07001015 trace_binder_unmap_kernel_start(alloc, index);
1016
Sherry Yangf2517eb2017-08-23 08:46:42 -07001017 __free_page(page->page_ptr);
1018 page->page_ptr = NULL;
1019
Sherry Yange41e1642017-08-23 08:46:43 -07001020 trace_binder_unmap_kernel_end(alloc, index);
1021
Sherry Yanga1b22892017-10-03 16:15:00 -07001022 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001023 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001024 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001025
Carlos Llamas8dce2882023-12-01 17:21:31 +00001026err_invalid_vma:
1027 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001028err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001029 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001030err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001031err_page_already_freed:
1032 mutex_unlock(&alloc->mutex);
1033err_get_alloc_mutex_failed:
1034 return LRU_SKIP;
1035}
1036
1037static unsigned long
1038binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1039{
1040 unsigned long ret = list_lru_count(&binder_alloc_lru);
1041 return ret;
1042}
1043
1044static unsigned long
1045binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1046{
1047 unsigned long ret;
1048
1049 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1050 NULL, sc->nr_to_scan);
1051 return ret;
1052}
1053
Sherry Yangde7bbe32017-10-06 16:12:05 -04001054static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001055 .count_objects = binder_shrink_count,
1056 .scan_objects = binder_shrink_scan,
1057 .seeks = DEFAULT_SEEKS,
1058};
1059
1060/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001061 * binder_alloc_init() - called by binder_open() for per-proc initialization
1062 * @alloc: binder_alloc for this proc
1063 *
1064 * Called from binder_open() to initialize binder_alloc fields for
1065 * new binder proc
1066 */
1067void binder_alloc_init(struct binder_alloc *alloc)
1068{
Todd Kjos0c972a02017-06-29 12:01:41 -07001069 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001070 alloc->vma_vm_mm = current->mm;
1071 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001072 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001073 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001074}
1075
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001076int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001077{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001078 int ret = list_lru_init(&binder_alloc_lru);
1079
1080 if (ret == 0) {
1081 ret = register_shrinker(&binder_shrinker);
1082 if (ret)
1083 list_lru_destroy(&binder_alloc_lru);
1084 }
1085 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001086}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001087
Qi Zheng03eebad2023-06-25 15:49:37 +00001088void binder_alloc_shrinker_exit(void)
1089{
1090 unregister_shrinker(&binder_shrinker);
1091 list_lru_destroy(&binder_alloc_lru);
1092}
1093
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001094/**
1095 * check_buffer() - verify that buffer/offset is safe to access
1096 * @alloc: binder_alloc for this proc
1097 * @buffer: binder buffer to be accessed
1098 * @offset: offset into @buffer data
1099 * @bytes: bytes to access from offset
1100 *
1101 * Check that the @offset/@bytes are within the size of the given
1102 * @buffer and that the buffer is currently active and not freeable.
1103 * Offsets must also be multiples of sizeof(u32). The kernel is
1104 * allowed to touch the buffer in two cases:
1105 *
1106 * 1) when the buffer is being created:
1107 * (buffer->free == 0 && buffer->allow_user_free == 0)
1108 * 2) when the buffer is being torn down:
1109 * (buffer->free == 0 && buffer->transaction == NULL).
1110 *
1111 * Return: true if the buffer is safe to access
1112 */
1113static inline bool check_buffer(struct binder_alloc *alloc,
1114 struct binder_buffer *buffer,
1115 binder_size_t offset, size_t bytes)
1116{
1117 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1118
1119 return buffer_size >= bytes &&
1120 offset <= buffer_size - bytes &&
1121 IS_ALIGNED(offset, sizeof(u32)) &&
1122 !buffer->free &&
1123 (!buffer->allow_user_free || !buffer->transaction);
1124}
1125
1126/**
1127 * binder_alloc_get_page() - get kernel pointer for given buffer offset
1128 * @alloc: binder_alloc for this proc
1129 * @buffer: binder buffer to be accessed
1130 * @buffer_offset: offset into @buffer data
1131 * @pgoffp: address to copy final page offset to
1132 *
1133 * Lookup the struct page corresponding to the address
Todd Kjosbde4a192019-02-08 10:35:20 -08001134 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001135 * NULL, the byte-offset into the page is written there.
1136 *
1137 * The caller is responsible to ensure that the offset points
1138 * to a valid address within the @buffer and that @buffer is
1139 * not freeable by the user. Since it can't be freed, we are
1140 * guaranteed that the corresponding elements of @alloc->pages[]
1141 * cannot change.
1142 *
1143 * Return: struct page
1144 */
1145static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
1146 struct binder_buffer *buffer,
1147 binder_size_t buffer_offset,
1148 pgoff_t *pgoffp)
1149{
1150 binder_size_t buffer_space_offset = buffer_offset +
Todd Kjosbde4a192019-02-08 10:35:20 -08001151 (buffer->user_data - alloc->buffer);
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001152 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
1153 size_t index = buffer_space_offset >> PAGE_SHIFT;
1154 struct binder_lru_page *lru_page;
1155
1156 lru_page = &alloc->pages[index];
1157 *pgoffp = pgoff;
1158 return lru_page->page_ptr;
1159}
1160
1161/**
Todd Kjos0f966cb2020-11-20 15:37:43 -08001162 * binder_alloc_clear_buf() - zero out buffer
1163 * @alloc: binder_alloc for this proc
1164 * @buffer: binder buffer to be cleared
1165 *
1166 * memset the given buffer to 0
1167 */
1168static void binder_alloc_clear_buf(struct binder_alloc *alloc,
1169 struct binder_buffer *buffer)
1170{
1171 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
1172 binder_size_t buffer_offset = 0;
1173
1174 while (bytes) {
1175 unsigned long size;
1176 struct page *page;
1177 pgoff_t pgoff;
1178 void *kptr;
1179
1180 page = binder_alloc_get_page(alloc, buffer,
1181 buffer_offset, &pgoff);
1182 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1183 kptr = kmap(page) + pgoff;
1184 memset(kptr, 0, size);
1185 kunmap(page);
1186 bytes -= size;
1187 buffer_offset += size;
1188 }
1189}
1190
1191/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001192 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1193 * @alloc: binder_alloc for this proc
1194 * @buffer: binder buffer to be accessed
1195 * @buffer_offset: offset into @buffer data
1196 * @from: userspace pointer to source buffer
1197 * @bytes: bytes to copy
1198 *
1199 * Copy bytes from source userspace to target buffer.
1200 *
1201 * Return: bytes remaining to be copied
1202 */
1203unsigned long
1204binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1205 struct binder_buffer *buffer,
1206 binder_size_t buffer_offset,
1207 const void __user *from,
1208 size_t bytes)
1209{
1210 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1211 return bytes;
1212
1213 while (bytes) {
1214 unsigned long size;
1215 unsigned long ret;
1216 struct page *page;
1217 pgoff_t pgoff;
1218 void *kptr;
1219
1220 page = binder_alloc_get_page(alloc, buffer,
1221 buffer_offset, &pgoff);
1222 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1223 kptr = kmap(page) + pgoff;
1224 ret = copy_from_user(kptr, from, size);
1225 kunmap(page);
1226 if (ret)
1227 return bytes - size + ret;
1228 bytes -= size;
1229 from += size;
1230 buffer_offset += size;
1231 }
1232 return 0;
1233}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001234
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001235static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1236 bool to_buffer,
1237 struct binder_buffer *buffer,
1238 binder_size_t buffer_offset,
1239 void *ptr,
1240 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001241{
1242 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001243 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1244 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001245
1246 while (bytes) {
1247 unsigned long size;
1248 struct page *page;
1249 pgoff_t pgoff;
1250 void *tmpptr;
1251 void *base_ptr;
1252
1253 page = binder_alloc_get_page(alloc, buffer,
1254 buffer_offset, &pgoff);
1255 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1256 base_ptr = kmap_atomic(page);
1257 tmpptr = base_ptr + pgoff;
1258 if (to_buffer)
1259 memcpy(tmpptr, ptr, size);
1260 else
1261 memcpy(ptr, tmpptr, size);
1262 /*
1263 * kunmap_atomic() takes care of flushing the cache
1264 * if this device has VIVT cache arch
1265 */
1266 kunmap_atomic(base_ptr);
1267 bytes -= size;
1268 pgoff = 0;
1269 ptr = ptr + size;
1270 buffer_offset += size;
1271 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001272 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001273}
1274
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001275int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1276 struct binder_buffer *buffer,
1277 binder_size_t buffer_offset,
1278 void *src,
1279 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001280{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001281 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1282 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001283}
1284
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001285int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1286 void *dest,
1287 struct binder_buffer *buffer,
1288 binder_size_t buffer_offset,
1289 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001290{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001291 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1292 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001293}
1294