blob: 2510f88db2d2b3d0c4e1f27d25751197ce4c1209 [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,
129 uintptr_t user_ptr)
130{
131 struct rb_node *n = alloc->allocated_buffers.rb_node;
132 struct binder_buffer *buffer;
Todd Kjosbde4a192019-02-08 10:35:20 -0800133 void __user *uptr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700134
Todd Kjosbde4a192019-02-08 10:35:20 -0800135 uptr = (void __user *)user_ptr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700136
137 while (n) {
138 buffer = rb_entry(n, struct binder_buffer, rb_node);
139 BUG_ON(buffer->free);
140
Todd Kjosbde4a192019-02-08 10:35:20 -0800141 if (uptr < buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700142 n = n->rb_left;
Todd Kjosbde4a192019-02-08 10:35:20 -0800143 else if (uptr > buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700144 n = n->rb_right;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700145 else {
146 /*
147 * Guard against user threads attempting to
Todd Kjos7bada552018-11-06 15:55:32 -0800148 * free the buffer when in use by kernel or
149 * after it's already been freed.
Todd Kjos53d311cf2017-06-29 12:01:51 -0700150 */
Todd Kjos7bada552018-11-06 15:55:32 -0800151 if (!buffer->allow_user_free)
152 return ERR_PTR(-EPERM);
153 buffer->allow_user_free = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700154 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700155 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700156 }
157 return NULL;
158}
159
160/**
Joel Fernandes (Google)5dc54a02019-09-30 16:12:50 -0400161 * binder_alloc_prepare_to_free() - get buffer given user ptr
Todd Kjos0c972a02017-06-29 12:01:41 -0700162 * @alloc: binder_alloc for this proc
163 * @user_ptr: User pointer to buffer data
164 *
165 * Validate userspace pointer to buffer data and return buffer corresponding to
166 * that user pointer. Search the rb tree for buffer that matches user data
167 * pointer.
168 *
169 * Return: Pointer to buffer or NULL
170 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700171struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
172 uintptr_t user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700173{
174 struct binder_buffer *buffer;
175
176 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700177 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700178 mutex_unlock(&alloc->mutex);
179 return buffer;
180}
181
182static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
Todd Kjosbde4a192019-02-08 10:35:20 -0800183 void __user *start, void __user *end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700184{
Todd Kjosbde4a192019-02-08 10:35:20 -0800185 void __user *page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700186 unsigned long user_page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700187 struct binder_lru_page *page;
Sherry Yang6ae33b92017-09-16 01:11:56 -0400188 struct vm_area_struct *vma = NULL;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700189 struct mm_struct *mm = NULL;
190 bool need_mm = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700191
192 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
193 "%d: %s pages %pK-%pK\n", alloc->pid,
194 allocate ? "allocate" : "free", start, end);
195
196 if (end <= start)
197 return 0;
198
199 trace_binder_update_page_range(alloc, allocate, start, end);
200
Sherry Yangf2517eb2017-08-23 08:46:42 -0700201 if (allocate == 0)
202 goto free_range;
203
204 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
205 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
206 if (!page->page_ptr) {
207 need_mm = true;
208 break;
209 }
210 }
211
Greg Kroah-Hartman6fbf2482017-10-23 17:21:44 +0200212 if (need_mm && mmget_not_zero(alloc->vma_vm_mm))
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400213 mm = alloc->vma_vm_mm;
Todd Kjos0c972a02017-06-29 12:01:41 -0700214
215 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000216 mmap_write_lock(mm);
Carlos Llamasacd81932023-05-30 19:43:36 +0000217 vma = alloc->vma;
Todd Kjos0c972a02017-06-29 12:01:41 -0700218 }
219
Sherry Yangf2517eb2017-08-23 08:46:42 -0700220 if (!vma && need_mm) {
Sherry Yang128f3802018-08-07 12:57:13 -0700221 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
222 "%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
223 alloc->pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700224 goto err_no_vma;
225 }
226
227 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
228 int ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700229 bool on_lru;
Sherry Yange41e1642017-08-23 08:46:43 -0700230 size_t index;
Todd Kjos0c972a02017-06-29 12:01:41 -0700231
Sherry Yange41e1642017-08-23 08:46:43 -0700232 index = (page_addr - alloc->buffer) / PAGE_SIZE;
233 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700234
Sherry Yangf2517eb2017-08-23 08:46:42 -0700235 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700236 trace_binder_alloc_lru_start(alloc, index);
237
Sherry Yangf2517eb2017-08-23 08:46:42 -0700238 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
239 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700240
241 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700242 continue;
243 }
244
245 if (WARN_ON(!vma))
246 goto err_page_ptr_cleared;
247
Sherry Yange41e1642017-08-23 08:46:43 -0700248 trace_binder_alloc_page_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700249 page->page_ptr = alloc_page(GFP_KERNEL |
250 __GFP_HIGHMEM |
251 __GFP_ZERO);
252 if (!page->page_ptr) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700253 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
254 alloc->pid, page_addr);
255 goto err_alloc_page_failed;
256 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700257 page->alloc = alloc;
258 INIT_LIST_HEAD(&page->lru);
259
Todd Kjosc41358a2019-02-08 10:35:19 -0800260 user_page_addr = (uintptr_t)page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700261 ret = vm_insert_page(vma, user_page_addr, page[0].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700262 if (ret) {
263 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
264 alloc->pid, user_page_addr);
265 goto err_vm_insert_page_failed;
266 }
Sherry Yange41e1642017-08-23 08:46:43 -0700267
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100268 if (index + 1 > alloc->pages_high)
269 alloc->pages_high = index + 1;
270
Sherry Yange41e1642017-08-23 08:46:43 -0700271 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700272 }
273 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000274 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000275 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700276 }
277 return 0;
278
279free_range:
Jann Horn2a9edd02019-10-18 22:56:31 +0200280 for (page_addr = end - PAGE_SIZE; 1; page_addr -= PAGE_SIZE) {
Sherry Yangf2517eb2017-08-23 08:46:42 -0700281 bool ret;
Sherry Yange41e1642017-08-23 08:46:43 -0700282 size_t index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700283
Sherry Yange41e1642017-08-23 08:46:43 -0700284 index = (page_addr - alloc->buffer) / PAGE_SIZE;
285 page = &alloc->pages[index];
286
287 trace_binder_free_lru_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700288
289 ret = list_lru_add(&binder_alloc_lru, &page->lru);
290 WARN_ON(!ret);
Sherry Yange41e1642017-08-23 08:46:43 -0700291
292 trace_binder_free_lru_end(alloc, index);
Jann Horn2a9edd02019-10-18 22:56:31 +0200293 if (page_addr == start)
294 break;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700295 continue;
296
Todd Kjos0c972a02017-06-29 12:01:41 -0700297err_vm_insert_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700298 __free_page(page->page_ptr);
299 page->page_ptr = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700300err_alloc_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700301err_page_ptr_cleared:
Jann Horn2a9edd02019-10-18 22:56:31 +0200302 if (page_addr == start)
303 break;
Todd Kjos0c972a02017-06-29 12:01:41 -0700304 }
305err_no_vma:
306 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000307 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000308 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700309 }
Todd Kjos57ada2f2017-06-29 12:01:46 -0700310 return vma ? -ENOMEM : -ESRCH;
Todd Kjos0c972a02017-06-29 12:01:41 -0700311}
312
Minchan Kimda1b9562018-08-23 14:29:56 +0900313static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
314 struct vm_area_struct *vma)
315{
Carlos Llamasb094b042023-05-30 19:43:37 +0000316 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
317 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900318}
319
320static inline struct vm_area_struct *binder_alloc_get_vma(
321 struct binder_alloc *alloc)
322{
Carlos Llamasb094b042023-05-30 19:43:37 +0000323 /* pairs with smp_store_release in binder_alloc_set_vma() */
324 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900325}
326
Hang Lua7dc1e62021-04-09 17:40:46 +0800327static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid)
Martijn Coenen261e7812020-08-21 14:25:44 +0200328{
329 /*
330 * Find the amount and size of buffers allocated by the current caller;
331 * The idea is that once we cross the threshold, whoever is responsible
332 * for the low async space is likely to try to send another async txn,
333 * and at some point we'll catch them in the act. This is more efficient
334 * than keeping a map per pid.
335 */
Colin Ian King7369fa42020-09-10 16:12:21 +0100336 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200337 struct binder_buffer *buffer;
338 size_t total_alloc_size = 0;
339 size_t num_buffers = 0;
340
341 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
342 n = rb_next(n)) {
343 buffer = rb_entry(n, struct binder_buffer, rb_node);
344 if (buffer->pid != pid)
345 continue;
346 if (!buffer->async_transaction)
347 continue;
348 total_alloc_size += binder_alloc_buffer_size(alloc, buffer)
349 + sizeof(struct binder_buffer);
350 num_buffers++;
351 }
352
353 /*
354 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800355 * async space (which is 25% of total buffer size). Oneway spam is only
356 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200357 */
358 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
359 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
360 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
361 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800362 if (!alloc->oneway_spam_detected) {
363 alloc->oneway_spam_detected = true;
364 return true;
365 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200366 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800367 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200368}
369
Xiongwei Song3f827242017-12-14 12:15:42 +0800370static struct binder_buffer *binder_alloc_new_buf_locked(
371 struct binder_alloc *alloc,
372 size_t data_size,
373 size_t offsets_size,
374 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200375 int is_async,
376 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700377{
378 struct rb_node *n = alloc->free_buffers.rb_node;
379 struct binder_buffer *buffer;
380 size_t buffer_size;
381 struct rb_node *best_fit = NULL;
Todd Kjosbde4a192019-02-08 10:35:20 -0800382 void __user *has_page_addr;
383 void __user *end_page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700384 size_t size, data_offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700385 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700386
Carlos Llamasb094b042023-05-30 19:43:37 +0000387 /* Check binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900388 if (!binder_alloc_get_vma(alloc)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700389 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
390 "%d: binder_alloc_buf, no vma\n",
391 alloc->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700392 return ERR_PTR(-ESRCH);
Todd Kjos0c972a02017-06-29 12:01:41 -0700393 }
394
395 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
396 ALIGN(offsets_size, sizeof(void *));
397
398 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
399 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
400 "%d: got transaction with invalid size %zd-%zd\n",
401 alloc->pid, data_size, offsets_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700402 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700403 }
404 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
405 if (size < data_offsets_size || size < extra_buffers_size) {
406 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
407 "%d: got transaction with invalid extra_buffers_size %zd\n",
408 alloc->pid, extra_buffers_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700409 return ERR_PTR(-EINVAL);
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
413 /* Pad 0-size buffers so they get assigned unique addresses */
414 size = max(size, sizeof(void *));
415
Todd Kjos0c972a02017-06-29 12:01:41 -0700416 if (is_async &&
417 alloc->free_async_space < size + sizeof(struct binder_buffer)) {
418 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
419 "%d: binder_alloc_buf size %zd failed, no async space left\n",
420 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700421 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700422 }
423
424 while (n) {
425 buffer = rb_entry(n, struct binder_buffer, rb_node);
426 BUG_ON(!buffer->free);
427 buffer_size = binder_alloc_buffer_size(alloc, buffer);
428
429 if (size < buffer_size) {
430 best_fit = n;
431 n = n->rb_left;
432 } else if (size > buffer_size)
433 n = n->rb_right;
434 else {
435 best_fit = n;
436 break;
437 }
438 }
439 if (best_fit == NULL) {
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700440 size_t allocated_buffers = 0;
441 size_t largest_alloc_size = 0;
442 size_t total_alloc_size = 0;
443 size_t free_buffers = 0;
444 size_t largest_free_size = 0;
445 size_t total_free_size = 0;
446
447 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
448 n = rb_next(n)) {
449 buffer = rb_entry(n, struct binder_buffer, rb_node);
450 buffer_size = binder_alloc_buffer_size(alloc, buffer);
451 allocated_buffers++;
452 total_alloc_size += buffer_size;
453 if (buffer_size > largest_alloc_size)
454 largest_alloc_size = buffer_size;
455 }
456 for (n = rb_first(&alloc->free_buffers); n != NULL;
457 n = rb_next(n)) {
458 buffer = rb_entry(n, struct binder_buffer, rb_node);
459 buffer_size = binder_alloc_buffer_size(alloc, buffer);
460 free_buffers++;
461 total_free_size += buffer_size;
462 if (buffer_size > largest_free_size)
463 largest_free_size = buffer_size;
464 }
Sherry Yang128f3802018-08-07 12:57:13 -0700465 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
466 "%d: binder_alloc_buf size %zd failed, no address space\n",
467 alloc->pid, size);
468 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
469 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
470 total_alloc_size, allocated_buffers,
471 largest_alloc_size, total_free_size,
472 free_buffers, largest_free_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700473 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700474 }
475 if (n == NULL) {
476 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
477 buffer_size = binder_alloc_buffer_size(alloc, buffer);
478 }
479
480 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
481 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
482 alloc->pid, size, buffer, buffer_size);
483
Todd Kjosbde4a192019-02-08 10:35:20 -0800484 has_page_addr = (void __user *)
485 (((uintptr_t)buffer->user_data + buffer_size) & PAGE_MASK);
Sherry Yang74310e02017-08-23 08:46:41 -0700486 WARN_ON(n && buffer_size != size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700487 end_page_addr =
Todd Kjosbde4a192019-02-08 10:35:20 -0800488 (void __user *)PAGE_ALIGN((uintptr_t)buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700489 if (end_page_addr > has_page_addr)
490 end_page_addr = has_page_addr;
Todd Kjosbde4a192019-02-08 10:35:20 -0800491 ret = binder_update_page_range(alloc, 1, (void __user *)
492 PAGE_ALIGN((uintptr_t)buffer->user_data), end_page_addr);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700493 if (ret)
494 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700495
Todd Kjos0c972a02017-06-29 12:01:41 -0700496 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700497 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700498
Sherry Yang74310e02017-08-23 08:46:41 -0700499 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
500 if (!new_buffer) {
501 pr_err("%s: %d failed to alloc new buffer struct\n",
502 __func__, alloc->pid);
503 goto err_alloc_buf_struct_failed;
504 }
Todd Kjosbde4a192019-02-08 10:35:20 -0800505 new_buffer->user_data = (u8 __user *)buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700506 list_add(&new_buffer->entry, &buffer->entry);
507 new_buffer->free = 1;
508 binder_insert_free_buffer(alloc, new_buffer);
509 }
Sherry Yang74310e02017-08-23 08:46:41 -0700510
511 rb_erase(best_fit, &alloc->free_buffers);
512 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800513 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700514 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700515 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
516 "%d: binder_alloc_buf size %zd got %pK\n",
517 alloc->pid, size, buffer);
518 buffer->data_size = data_size;
519 buffer->offsets_size = offsets_size;
520 buffer->async_transaction = is_async;
521 buffer->extra_buffers_size = extra_buffers_size;
Martijn Coenen261e7812020-08-21 14:25:44 +0200522 buffer->pid = pid;
Hang Lua7dc1e62021-04-09 17:40:46 +0800523 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700524 if (is_async) {
525 alloc->free_async_space -= size + sizeof(struct binder_buffer);
526 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
527 "%d: binder_alloc_buf size %zd async free %zd\n",
528 alloc->pid, size, alloc->free_async_space);
Martijn Coenen261e7812020-08-21 14:25:44 +0200529 if (alloc->free_async_space < alloc->buffer_size / 10) {
530 /*
531 * Start detecting spammers once we have less than 20%
532 * of async space left (which is less than 10% of total
533 * buffer size).
534 */
Hang Lua7dc1e62021-04-09 17:40:46 +0800535 buffer->oneway_spam_suspect = debug_low_async_space_locked(alloc, pid);
536 } else {
537 alloc->oneway_spam_detected = false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200538 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700539 }
540 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700541
542err_alloc_buf_struct_failed:
Todd Kjosbde4a192019-02-08 10:35:20 -0800543 binder_update_page_range(alloc, 0, (void __user *)
544 PAGE_ALIGN((uintptr_t)buffer->user_data),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400545 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700546 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700547}
548
549/**
550 * binder_alloc_new_buf() - Allocate a new binder buffer
551 * @alloc: binder_alloc for this proc
552 * @data_size: size of user data buffer
553 * @offsets_size: user specified buffer offset
554 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
555 * @is_async: buffer for async transaction
Martijn Coenen261e7812020-08-21 14:25:44 +0200556 * @pid: pid to attribute allocation to (used for debugging)
Todd Kjos0c972a02017-06-29 12:01:41 -0700557 *
558 * Allocate a new buffer given the requested sizes. Returns
559 * the kernel version of the buffer pointer. The size allocated
560 * is the sum of the three given sizes (each rounded up to
561 * pointer-sized boundary)
562 *
563 * Return: The allocated buffer or %NULL if error
564 */
565struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
566 size_t data_size,
567 size_t offsets_size,
568 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200569 int is_async,
570 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700571{
572 struct binder_buffer *buffer;
573
574 mutex_lock(&alloc->mutex);
575 buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200576 extra_buffers_size, is_async, pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700577 mutex_unlock(&alloc->mutex);
578 return buffer;
579}
580
Todd Kjosbde4a192019-02-08 10:35:20 -0800581static void __user *buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700582{
Todd Kjosbde4a192019-02-08 10:35:20 -0800583 return (void __user *)((uintptr_t)buffer->user_data & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700584}
585
Todd Kjosbde4a192019-02-08 10:35:20 -0800586static void __user *prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700587{
Todd Kjosbde4a192019-02-08 10:35:20 -0800588 return (void __user *)
589 (((uintptr_t)(buffer->user_data) - 1) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700590}
591
592static void binder_delete_free_buffer(struct binder_alloc *alloc,
593 struct binder_buffer *buffer)
594{
595 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700596 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530597
Todd Kjos0c972a02017-06-29 12:01:41 -0700598 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700599 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700600 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700601 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
602 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700603 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700604 "%d: merge free, buffer %pK share page with %pK\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800605 alloc->pid, buffer->user_data,
606 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700607 }
608
609 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700610 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700611 if (buffer_start_page(next) == buffer_start_page(buffer)) {
612 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700613 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700614 "%d: merge free, buffer %pK share page with %pK\n",
615 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800616 buffer->user_data,
617 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700618 }
619 }
Sherry Yang74310e02017-08-23 08:46:41 -0700620
Todd Kjosbde4a192019-02-08 10:35:20 -0800621 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700622 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700623 "%d: merge free, buffer start %pK is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800624 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700625 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700626 }
Sherry Yang74310e02017-08-23 08:46:41 -0700627
628 if (to_free) {
629 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
630 "%d: merge free, buffer %pK do not share page with %pK or %pK\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800631 alloc->pid, buffer->user_data,
632 prev->user_data,
633 next ? next->user_data : NULL);
Sherry Yang74310e02017-08-23 08:46:41 -0700634 binder_update_page_range(alloc, 0, buffer_start_page(buffer),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400635 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700636 }
637 list_del(&buffer->entry);
638 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700639}
640
641static void binder_free_buf_locked(struct binder_alloc *alloc,
642 struct binder_buffer *buffer)
643{
644 size_t size, buffer_size;
645
646 buffer_size = binder_alloc_buffer_size(alloc, buffer);
647
648 size = ALIGN(buffer->data_size, sizeof(void *)) +
649 ALIGN(buffer->offsets_size, sizeof(void *)) +
650 ALIGN(buffer->extra_buffers_size, sizeof(void *));
651
652 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
653 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
654 alloc->pid, buffer, size, buffer_size);
655
656 BUG_ON(buffer->free);
657 BUG_ON(size > buffer_size);
658 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800659 BUG_ON(buffer->user_data < alloc->buffer);
660 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700661
662 if (buffer->async_transaction) {
Todd Kjos17691ba2021-12-20 11:01:50 -0800663 alloc->free_async_space += buffer_size + sizeof(struct binder_buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700664
665 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
666 "%d: binder_free_buf size %zd async free %zd\n",
667 alloc->pid, size, alloc->free_async_space);
668 }
669
670 binder_update_page_range(alloc, 0,
Todd Kjosbde4a192019-02-08 10:35:20 -0800671 (void __user *)PAGE_ALIGN((uintptr_t)buffer->user_data),
672 (void __user *)(((uintptr_t)
673 buffer->user_data + buffer_size) & PAGE_MASK));
Todd Kjos0c972a02017-06-29 12:01:41 -0700674
675 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
676 buffer->free = 1;
677 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700678 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700679
680 if (next->free) {
681 rb_erase(&next->rb_node, &alloc->free_buffers);
682 binder_delete_free_buffer(alloc, next);
683 }
684 }
685 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700686 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700687
688 if (prev->free) {
689 binder_delete_free_buffer(alloc, buffer);
690 rb_erase(&prev->rb_node, &alloc->free_buffers);
691 buffer = prev;
692 }
693 }
694 binder_insert_free_buffer(alloc, buffer);
695}
696
Todd Kjos0f966cb2020-11-20 15:37:43 -0800697static void binder_alloc_clear_buf(struct binder_alloc *alloc,
698 struct binder_buffer *buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700699/**
700 * binder_alloc_free_buf() - free a binder buffer
701 * @alloc: binder_alloc for this proc
702 * @buffer: kernel pointer to buffer
703 *
YangHui4b463822020-08-18 09:34:04 +0800704 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700705 */
706void binder_alloc_free_buf(struct binder_alloc *alloc,
707 struct binder_buffer *buffer)
708{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800709 /*
710 * We could eliminate the call to binder_alloc_clear_buf()
711 * from binder_alloc_deferred_release() by moving this to
712 * binder_alloc_free_buf_locked(). However, that could
713 * increase contention for the alloc mutex if clear_on_free
714 * is used frequently for large buffers. The mutex is not
715 * needed for correctness here.
716 */
717 if (buffer->clear_on_free) {
718 binder_alloc_clear_buf(alloc, buffer);
719 buffer->clear_on_free = false;
720 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700721 mutex_lock(&alloc->mutex);
722 binder_free_buf_locked(alloc, buffer);
723 mutex_unlock(&alloc->mutex);
724}
725
726/**
727 * binder_alloc_mmap_handler() - map virtual address space for proc
728 * @alloc: alloc structure for this proc
729 * @vma: vma passed to mmap()
730 *
731 * Called by binder_mmap() to initialize the space specified in
732 * vma for allocating binder buffers
733 *
734 * Return:
735 * 0 = success
736 * -EBUSY = address space already mapped
737 * -ENOMEM = failed to map memory to given address space
738 */
739int binder_alloc_mmap_handler(struct binder_alloc *alloc,
740 struct vm_area_struct *vma)
741{
742 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700743 const char *failure_string;
744 struct binder_buffer *buffer;
745
Carlos Llamasd276fb42022-11-04 23:12:35 +0000746 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
747 ret = -EINVAL;
748 failure_string = "invalid vma->vm_mm";
749 goto err_invalid_mm;
750 }
751
Todd Kjos0c972a02017-06-29 12:01:41 -0700752 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200753 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700754 ret = -EBUSY;
755 failure_string = "already mapped";
756 goto err_already_mapped;
757 }
Jann Horn45d02f72019-10-16 17:01:18 +0200758 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
759 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200760 mutex_unlock(&binder_alloc_mmap_lock);
761
762 alloc->buffer = (void __user *)vma->vm_start;
763
Jann Horn45d02f72019-10-16 17:01:18 +0200764 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700765 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700766 GFP_KERNEL);
767 if (alloc->pages == NULL) {
768 ret = -ENOMEM;
769 failure_string = "alloc page array";
770 goto err_alloc_pages_failed;
771 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700772
Sherry Yang74310e02017-08-23 08:46:41 -0700773 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
774 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700775 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700776 failure_string = "alloc buffer struct";
777 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700778 }
Sherry Yang74310e02017-08-23 08:46:41 -0700779
Todd Kjosbde4a192019-02-08 10:35:20 -0800780 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700781 list_add(&buffer->entry, &alloc->buffers);
782 buffer->free = 1;
783 binder_insert_free_buffer(alloc, buffer);
784 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000785
786 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900787 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700788
789 return 0;
790
Sherry Yang74310e02017-08-23 08:46:41 -0700791err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700792 kfree(alloc->pages);
793 alloc->pages = NULL;
794err_alloc_pages_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700795 alloc->buffer = NULL;
Jann Horna7a74d72019-10-18 22:56:30 +0200796 mutex_lock(&binder_alloc_mmap_lock);
797 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700798err_already_mapped:
799 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000800err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700801 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
802 "%s: %d %lx-%lx %s failed %d\n", __func__,
803 alloc->pid, vma->vm_start, vma->vm_end,
804 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700805 return ret;
806}
807
808
809void binder_alloc_deferred_release(struct binder_alloc *alloc)
810{
811 struct rb_node *n;
812 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700813 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700814
Todd Kjos0c972a02017-06-29 12:01:41 -0700815 buffers = 0;
816 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000817 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900818
Todd Kjos0c972a02017-06-29 12:01:41 -0700819 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700820 buffer = rb_entry(n, struct binder_buffer, rb_node);
821
822 /* Transaction should already have been freed */
823 BUG_ON(buffer->transaction);
824
Todd Kjos0f966cb2020-11-20 15:37:43 -0800825 if (buffer->clear_on_free) {
826 binder_alloc_clear_buf(alloc, buffer);
827 buffer->clear_on_free = false;
828 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700829 binder_free_buf_locked(alloc, buffer);
830 buffers++;
831 }
832
Sherry Yang74310e02017-08-23 08:46:41 -0700833 while (!list_empty(&alloc->buffers)) {
834 buffer = list_first_entry(&alloc->buffers,
835 struct binder_buffer, entry);
836 WARN_ON(!buffer->free);
837
838 list_del(&buffer->entry);
839 WARN_ON_ONCE(!list_empty(&alloc->buffers));
840 kfree(buffer);
841 }
842
Todd Kjos0c972a02017-06-29 12:01:41 -0700843 page_count = 0;
844 if (alloc->pages) {
845 int i;
846
847 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Todd Kjosbde4a192019-02-08 10:35:20 -0800848 void __user *page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700849 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700850
Sherry Yangf2517eb2017-08-23 08:46:42 -0700851 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700852 continue;
853
Sherry Yangf2517eb2017-08-23 08:46:42 -0700854 on_lru = list_lru_del(&binder_alloc_lru,
855 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700856 page_addr = alloc->buffer + i * PAGE_SIZE;
857 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700858 "%s: %d: page %d at %pK %s\n",
859 __func__, alloc->pid, i, page_addr,
860 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700861 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700862 page_count++;
863 }
864 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700865 }
866 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400867 if (alloc->vma_vm_mm)
868 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700869
870 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
871 "%s: %d buffers %d, pages %d\n",
872 __func__, alloc->pid, buffers, page_count);
873}
874
875static void print_binder_buffer(struct seq_file *m, const char *prefix,
876 struct binder_buffer *buffer)
877{
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700878 seq_printf(m, "%s %d: %pK size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800879 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700880 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700881 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700882 buffer->transaction ? "active" : "delivered");
883}
884
885/**
886 * binder_alloc_print_allocated() - print buffer info
887 * @m: seq_file for output via seq_printf()
888 * @alloc: binder_alloc for this proc
889 *
890 * Prints information about every buffer associated with
891 * the binder_alloc state to the given seq_file
892 */
893void binder_alloc_print_allocated(struct seq_file *m,
894 struct binder_alloc *alloc)
895{
896 struct rb_node *n;
897
898 mutex_lock(&alloc->mutex);
899 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
900 print_binder_buffer(m, " buffer",
901 rb_entry(n, struct binder_buffer, rb_node));
902 mutex_unlock(&alloc->mutex);
903}
904
905/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700906 * binder_alloc_print_pages() - print page usage
907 * @m: seq_file for output via seq_printf()
908 * @alloc: binder_alloc for this proc
909 */
910void binder_alloc_print_pages(struct seq_file *m,
911 struct binder_alloc *alloc)
912{
913 struct binder_lru_page *page;
914 int i;
915 int active = 0;
916 int lru = 0;
917 int free = 0;
918
919 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200920 /*
921 * Make sure the binder_alloc is fully initialized, otherwise we might
922 * read inconsistent state.
923 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000924 if (binder_alloc_get_vma(alloc) != NULL) {
925 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
926 page = &alloc->pages[i];
927 if (!page->page_ptr)
928 free++;
929 else if (list_empty(&page->lru))
930 active++;
931 else
932 lru++;
933 }
Sherry Yang8ef46652017-08-31 11:56:36 -0700934 }
935 mutex_unlock(&alloc->mutex);
936 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100937 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -0700938}
939
940/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700941 * binder_alloc_get_allocated_count() - return count of buffers
942 * @alloc: binder_alloc for this proc
943 *
944 * Return: count of allocated buffers
945 */
946int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
947{
948 struct rb_node *n;
949 int count = 0;
950
951 mutex_lock(&alloc->mutex);
952 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
953 count++;
954 mutex_unlock(&alloc->mutex);
955 return count;
956}
957
958
959/**
960 * binder_alloc_vma_close() - invalidate address space
961 * @alloc: binder_alloc for this proc
962 *
963 * Called from binder_vma_close() when releasing address space.
964 * Clears alloc->vma to prevent new incoming transactions from
965 * allocating more buffers.
966 */
967void binder_alloc_vma_close(struct binder_alloc *alloc)
968{
Minchan Kimda1b9562018-08-23 14:29:56 +0900969 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700970}
971
972/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700973 * binder_alloc_free_page() - shrinker callback to free pages
974 * @item: item to free
975 * @lock: lock protecting the item
976 * @cb_arg: callback argument
977 *
978 * Called from list_lru_walk() in binder_shrink_scan() to free
979 * up pages when the system is under memory pressure.
980 */
981enum lru_status binder_alloc_free_page(struct list_head *item,
982 struct list_lru_one *lru,
983 spinlock_t *lock,
984 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -0800985 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -0700986{
987 struct mm_struct *mm = NULL;
988 struct binder_lru_page *page = container_of(item,
989 struct binder_lru_page,
990 lru);
991 struct binder_alloc *alloc;
992 uintptr_t page_addr;
993 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -0700994 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700995
996 alloc = page->alloc;
997 if (!mutex_trylock(&alloc->mutex))
998 goto err_get_alloc_mutex_failed;
999
1000 if (!page->page_ptr)
1001 goto err_page_already_freed;
1002
1003 index = page - alloc->pages;
1004 page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001005
1006 mm = alloc->vma_vm_mm;
1007 if (!mmget_not_zero(mm))
1008 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001009 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001010 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001011 vma = vma_lookup(mm, page_addr);
1012 if (vma && vma != binder_alloc_get_vma(alloc))
1013 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001014
Sherry Yanga1b22892017-10-03 16:15:00 -07001015 list_lru_isolate(lru, item);
1016 spin_unlock(lock);
1017
1018 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001019 trace_binder_unmap_user_start(alloc, index);
1020
Todd Kjosc41358a2019-02-08 10:35:19 -08001021 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001022
Sherry Yange41e1642017-08-23 08:46:43 -07001023 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001024 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001025 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001026 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001027
Sherry Yange41e1642017-08-23 08:46:43 -07001028 trace_binder_unmap_kernel_start(alloc, index);
1029
Sherry Yangf2517eb2017-08-23 08:46:42 -07001030 __free_page(page->page_ptr);
1031 page->page_ptr = NULL;
1032
Sherry Yange41e1642017-08-23 08:46:43 -07001033 trace_binder_unmap_kernel_end(alloc, index);
1034
Sherry Yanga1b22892017-10-03 16:15:00 -07001035 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001036 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001037 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001038
Carlos Llamas8dce2882023-12-01 17:21:31 +00001039err_invalid_vma:
1040 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001041err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001042 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001043err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001044err_page_already_freed:
1045 mutex_unlock(&alloc->mutex);
1046err_get_alloc_mutex_failed:
1047 return LRU_SKIP;
1048}
1049
1050static unsigned long
1051binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1052{
1053 unsigned long ret = list_lru_count(&binder_alloc_lru);
1054 return ret;
1055}
1056
1057static unsigned long
1058binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1059{
1060 unsigned long ret;
1061
1062 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1063 NULL, sc->nr_to_scan);
1064 return ret;
1065}
1066
Sherry Yangde7bbe32017-10-06 16:12:05 -04001067static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001068 .count_objects = binder_shrink_count,
1069 .scan_objects = binder_shrink_scan,
1070 .seeks = DEFAULT_SEEKS,
1071};
1072
1073/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001074 * binder_alloc_init() - called by binder_open() for per-proc initialization
1075 * @alloc: binder_alloc for this proc
1076 *
1077 * Called from binder_open() to initialize binder_alloc fields for
1078 * new binder proc
1079 */
1080void binder_alloc_init(struct binder_alloc *alloc)
1081{
Todd Kjos0c972a02017-06-29 12:01:41 -07001082 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001083 alloc->vma_vm_mm = current->mm;
1084 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001085 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001086 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001087}
1088
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001089int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001090{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001091 int ret = list_lru_init(&binder_alloc_lru);
1092
1093 if (ret == 0) {
1094 ret = register_shrinker(&binder_shrinker);
1095 if (ret)
1096 list_lru_destroy(&binder_alloc_lru);
1097 }
1098 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001099}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001100
Qi Zheng03eebad2023-06-25 15:49:37 +00001101void binder_alloc_shrinker_exit(void)
1102{
1103 unregister_shrinker(&binder_shrinker);
1104 list_lru_destroy(&binder_alloc_lru);
1105}
1106
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001107/**
1108 * check_buffer() - verify that buffer/offset is safe to access
1109 * @alloc: binder_alloc for this proc
1110 * @buffer: binder buffer to be accessed
1111 * @offset: offset into @buffer data
1112 * @bytes: bytes to access from offset
1113 *
1114 * Check that the @offset/@bytes are within the size of the given
1115 * @buffer and that the buffer is currently active and not freeable.
1116 * Offsets must also be multiples of sizeof(u32). The kernel is
1117 * allowed to touch the buffer in two cases:
1118 *
1119 * 1) when the buffer is being created:
1120 * (buffer->free == 0 && buffer->allow_user_free == 0)
1121 * 2) when the buffer is being torn down:
1122 * (buffer->free == 0 && buffer->transaction == NULL).
1123 *
1124 * Return: true if the buffer is safe to access
1125 */
1126static inline bool check_buffer(struct binder_alloc *alloc,
1127 struct binder_buffer *buffer,
1128 binder_size_t offset, size_t bytes)
1129{
1130 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1131
1132 return buffer_size >= bytes &&
1133 offset <= buffer_size - bytes &&
1134 IS_ALIGNED(offset, sizeof(u32)) &&
1135 !buffer->free &&
1136 (!buffer->allow_user_free || !buffer->transaction);
1137}
1138
1139/**
1140 * binder_alloc_get_page() - get kernel pointer for given buffer offset
1141 * @alloc: binder_alloc for this proc
1142 * @buffer: binder buffer to be accessed
1143 * @buffer_offset: offset into @buffer data
1144 * @pgoffp: address to copy final page offset to
1145 *
1146 * Lookup the struct page corresponding to the address
Todd Kjosbde4a192019-02-08 10:35:20 -08001147 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001148 * NULL, the byte-offset into the page is written there.
1149 *
1150 * The caller is responsible to ensure that the offset points
1151 * to a valid address within the @buffer and that @buffer is
1152 * not freeable by the user. Since it can't be freed, we are
1153 * guaranteed that the corresponding elements of @alloc->pages[]
1154 * cannot change.
1155 *
1156 * Return: struct page
1157 */
1158static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
1159 struct binder_buffer *buffer,
1160 binder_size_t buffer_offset,
1161 pgoff_t *pgoffp)
1162{
1163 binder_size_t buffer_space_offset = buffer_offset +
Todd Kjosbde4a192019-02-08 10:35:20 -08001164 (buffer->user_data - alloc->buffer);
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001165 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
1166 size_t index = buffer_space_offset >> PAGE_SHIFT;
1167 struct binder_lru_page *lru_page;
1168
1169 lru_page = &alloc->pages[index];
1170 *pgoffp = pgoff;
1171 return lru_page->page_ptr;
1172}
1173
1174/**
Todd Kjos0f966cb2020-11-20 15:37:43 -08001175 * binder_alloc_clear_buf() - zero out buffer
1176 * @alloc: binder_alloc for this proc
1177 * @buffer: binder buffer to be cleared
1178 *
1179 * memset the given buffer to 0
1180 */
1181static void binder_alloc_clear_buf(struct binder_alloc *alloc,
1182 struct binder_buffer *buffer)
1183{
1184 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
1185 binder_size_t buffer_offset = 0;
1186
1187 while (bytes) {
1188 unsigned long size;
1189 struct page *page;
1190 pgoff_t pgoff;
1191 void *kptr;
1192
1193 page = binder_alloc_get_page(alloc, buffer,
1194 buffer_offset, &pgoff);
1195 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1196 kptr = kmap(page) + pgoff;
1197 memset(kptr, 0, size);
1198 kunmap(page);
1199 bytes -= size;
1200 buffer_offset += size;
1201 }
1202}
1203
1204/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001205 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1206 * @alloc: binder_alloc for this proc
1207 * @buffer: binder buffer to be accessed
1208 * @buffer_offset: offset into @buffer data
1209 * @from: userspace pointer to source buffer
1210 * @bytes: bytes to copy
1211 *
1212 * Copy bytes from source userspace to target buffer.
1213 *
1214 * Return: bytes remaining to be copied
1215 */
1216unsigned long
1217binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1218 struct binder_buffer *buffer,
1219 binder_size_t buffer_offset,
1220 const void __user *from,
1221 size_t bytes)
1222{
1223 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1224 return bytes;
1225
1226 while (bytes) {
1227 unsigned long size;
1228 unsigned long ret;
1229 struct page *page;
1230 pgoff_t pgoff;
1231 void *kptr;
1232
1233 page = binder_alloc_get_page(alloc, buffer,
1234 buffer_offset, &pgoff);
1235 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1236 kptr = kmap(page) + pgoff;
1237 ret = copy_from_user(kptr, from, size);
1238 kunmap(page);
1239 if (ret)
1240 return bytes - size + ret;
1241 bytes -= size;
1242 from += size;
1243 buffer_offset += size;
1244 }
1245 return 0;
1246}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001247
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001248static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1249 bool to_buffer,
1250 struct binder_buffer *buffer,
1251 binder_size_t buffer_offset,
1252 void *ptr,
1253 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001254{
1255 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001256 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1257 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001258
1259 while (bytes) {
1260 unsigned long size;
1261 struct page *page;
1262 pgoff_t pgoff;
1263 void *tmpptr;
1264 void *base_ptr;
1265
1266 page = binder_alloc_get_page(alloc, buffer,
1267 buffer_offset, &pgoff);
1268 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1269 base_ptr = kmap_atomic(page);
1270 tmpptr = base_ptr + pgoff;
1271 if (to_buffer)
1272 memcpy(tmpptr, ptr, size);
1273 else
1274 memcpy(ptr, tmpptr, size);
1275 /*
1276 * kunmap_atomic() takes care of flushing the cache
1277 * if this device has VIVT cache arch
1278 */
1279 kunmap_atomic(base_ptr);
1280 bytes -= size;
1281 pgoff = 0;
1282 ptr = ptr + size;
1283 buffer_offset += size;
1284 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001285 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001286}
1287
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001288int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1289 struct binder_buffer *buffer,
1290 binder_size_t buffer_offset,
1291 void *src,
1292 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001293{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001294 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1295 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001296}
1297
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001298int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1299 void *dest,
1300 struct binder_buffer *buffer,
1301 binder_size_t buffer_offset,
1302 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001303{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001304 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1305 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001306}
1307