blob: de9625fc3b02c43d6f7e27027a72a3b5737bc903 [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
179static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000180 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700181{
Sherry Yang6ae33b92017-09-16 01:11:56 -0400182 struct vm_area_struct *vma = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000183 struct binder_lru_page *page;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700184 struct mm_struct *mm = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000185 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700186 bool need_mm = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700187
188 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000189 "%d: %s allocate pages %lx-%lx\n", alloc->pid,
190 allocate ? "allocate" : "free", start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700191
192 if (end <= start)
193 return 0;
194
195 trace_binder_update_page_range(alloc, allocate, start, end);
196
Sherry Yangf2517eb2017-08-23 08:46:42 -0700197 if (allocate == 0)
198 goto free_range;
199
200 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
201 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
202 if (!page->page_ptr) {
203 need_mm = true;
204 break;
205 }
206 }
207
Greg Kroah-Hartman6fbf2482017-10-23 17:21:44 +0200208 if (need_mm && mmget_not_zero(alloc->vma_vm_mm))
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400209 mm = alloc->vma_vm_mm;
Todd Kjos0c972a02017-06-29 12:01:41 -0700210
211 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000212 mmap_write_lock(mm);
Carlos Llamasacd81932023-05-30 19:43:36 +0000213 vma = alloc->vma;
Todd Kjos0c972a02017-06-29 12:01:41 -0700214 }
215
Sherry Yangf2517eb2017-08-23 08:46:42 -0700216 if (!vma && need_mm) {
Sherry Yang128f3802018-08-07 12:57:13 -0700217 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
218 "%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
219 alloc->pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700220 goto err_no_vma;
221 }
222
223 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
224 int ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700225 bool on_lru;
Sherry Yange41e1642017-08-23 08:46:43 -0700226 size_t index;
Todd Kjos0c972a02017-06-29 12:01:41 -0700227
Sherry Yange41e1642017-08-23 08:46:43 -0700228 index = (page_addr - alloc->buffer) / PAGE_SIZE;
229 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700230
Sherry Yangf2517eb2017-08-23 08:46:42 -0700231 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700232 trace_binder_alloc_lru_start(alloc, index);
233
Sherry Yangf2517eb2017-08-23 08:46:42 -0700234 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
235 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700236
237 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700238 continue;
239 }
240
241 if (WARN_ON(!vma))
242 goto err_page_ptr_cleared;
243
Sherry Yange41e1642017-08-23 08:46:43 -0700244 trace_binder_alloc_page_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700245 page->page_ptr = alloc_page(GFP_KERNEL |
246 __GFP_HIGHMEM |
247 __GFP_ZERO);
248 if (!page->page_ptr) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000249 pr_err("%d: binder_alloc_buf failed for page at %lx\n",
250 alloc->pid, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700251 goto err_alloc_page_failed;
252 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700253 page->alloc = alloc;
254 INIT_LIST_HEAD(&page->lru);
255
Carlos Llamasc38a8982023-12-01 17:21:38 +0000256 ret = vm_insert_page(vma, page_addr, page->page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700257 if (ret) {
258 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Carlos Llamasc38a8982023-12-01 17:21:38 +0000259 alloc->pid, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700260 goto err_vm_insert_page_failed;
261 }
Sherry Yange41e1642017-08-23 08:46:43 -0700262
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100263 if (index + 1 > alloc->pages_high)
264 alloc->pages_high = index + 1;
265
Sherry Yange41e1642017-08-23 08:46:43 -0700266 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700267 }
268 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000269 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000270 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700271 }
272 return 0;
273
274free_range:
Jann Horn2a9edd02019-10-18 22:56:31 +0200275 for (page_addr = end - PAGE_SIZE; 1; page_addr -= PAGE_SIZE) {
Sherry Yangf2517eb2017-08-23 08:46:42 -0700276 bool ret;
Sherry Yange41e1642017-08-23 08:46:43 -0700277 size_t index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700278
Sherry Yange41e1642017-08-23 08:46:43 -0700279 index = (page_addr - alloc->buffer) / PAGE_SIZE;
280 page = &alloc->pages[index];
281
282 trace_binder_free_lru_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700283
284 ret = list_lru_add(&binder_alloc_lru, &page->lru);
285 WARN_ON(!ret);
Sherry Yange41e1642017-08-23 08:46:43 -0700286
287 trace_binder_free_lru_end(alloc, index);
Jann Horn2a9edd02019-10-18 22:56:31 +0200288 if (page_addr == start)
289 break;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700290 continue;
291
Todd Kjos0c972a02017-06-29 12:01:41 -0700292err_vm_insert_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700293 __free_page(page->page_ptr);
294 page->page_ptr = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700295err_alloc_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700296err_page_ptr_cleared:
Jann Horn2a9edd02019-10-18 22:56:31 +0200297 if (page_addr == start)
298 break;
Todd Kjos0c972a02017-06-29 12:01:41 -0700299 }
300err_no_vma:
301 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000302 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000303 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700304 }
Todd Kjos57ada2f2017-06-29 12:01:46 -0700305 return vma ? -ENOMEM : -ESRCH;
Todd Kjos0c972a02017-06-29 12:01:41 -0700306}
307
Minchan Kimda1b9562018-08-23 14:29:56 +0900308static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
309 struct vm_area_struct *vma)
310{
Carlos Llamasb094b042023-05-30 19:43:37 +0000311 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
312 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900313}
314
315static inline struct vm_area_struct *binder_alloc_get_vma(
316 struct binder_alloc *alloc)
317{
Carlos Llamasb094b042023-05-30 19:43:37 +0000318 /* pairs with smp_store_release in binder_alloc_set_vma() */
319 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900320}
321
Hang Lua7dc1e62021-04-09 17:40:46 +0800322static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid)
Martijn Coenen261e7812020-08-21 14:25:44 +0200323{
324 /*
325 * Find the amount and size of buffers allocated by the current caller;
326 * The idea is that once we cross the threshold, whoever is responsible
327 * for the low async space is likely to try to send another async txn,
328 * and at some point we'll catch them in the act. This is more efficient
329 * than keeping a map per pid.
330 */
Colin Ian King7369fa42020-09-10 16:12:21 +0100331 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200332 struct binder_buffer *buffer;
333 size_t total_alloc_size = 0;
334 size_t num_buffers = 0;
335
336 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
337 n = rb_next(n)) {
338 buffer = rb_entry(n, struct binder_buffer, rb_node);
339 if (buffer->pid != pid)
340 continue;
341 if (!buffer->async_transaction)
342 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000343 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200344 num_buffers++;
345 }
346
347 /*
348 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800349 * async space (which is 25% of total buffer size). Oneway spam is only
350 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200351 */
352 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
353 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
354 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
355 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800356 if (!alloc->oneway_spam_detected) {
357 alloc->oneway_spam_detected = true;
358 return true;
359 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200360 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800361 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200362}
363
Xiongwei Song3f827242017-12-14 12:15:42 +0800364static struct binder_buffer *binder_alloc_new_buf_locked(
365 struct binder_alloc *alloc,
366 size_t data_size,
367 size_t offsets_size,
368 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200369 int is_async,
370 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700371{
372 struct rb_node *n = alloc->free_buffers.rb_node;
373 struct binder_buffer *buffer;
374 size_t buffer_size;
375 struct rb_node *best_fit = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700376 size_t size, data_offsets_size;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000377 unsigned long has_page_addr;
378 unsigned long end_page_addr;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700379 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700380
Carlos Llamasb094b042023-05-30 19:43:37 +0000381 /* Check binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900382 if (!binder_alloc_get_vma(alloc)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700383 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
384 "%d: binder_alloc_buf, no vma\n",
385 alloc->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700386 return ERR_PTR(-ESRCH);
Todd Kjos0c972a02017-06-29 12:01:41 -0700387 }
388
389 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
390 ALIGN(offsets_size, sizeof(void *));
391
392 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
393 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
394 "%d: got transaction with invalid size %zd-%zd\n",
395 alloc->pid, data_size, offsets_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700396 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700397 }
398 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
399 if (size < data_offsets_size || size < extra_buffers_size) {
400 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
401 "%d: got transaction with invalid extra_buffers_size %zd\n",
402 alloc->pid, extra_buffers_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700403 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700404 }
Zhuguangqing1174e452021-03-09 15:47:43 +0800405 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000406
407 /* Pad 0-size buffers so they get assigned unique addresses */
408 size = max(size, sizeof(void *));
409
Carlos Llamas11ca0762023-12-01 17:21:34 +0000410 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700411 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
412 "%d: binder_alloc_buf size %zd failed, no async space left\n",
413 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700414 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700415 }
416
417 while (n) {
418 buffer = rb_entry(n, struct binder_buffer, rb_node);
419 BUG_ON(!buffer->free);
420 buffer_size = binder_alloc_buffer_size(alloc, buffer);
421
422 if (size < buffer_size) {
423 best_fit = n;
424 n = n->rb_left;
425 } else if (size > buffer_size)
426 n = n->rb_right;
427 else {
428 best_fit = n;
429 break;
430 }
431 }
432 if (best_fit == NULL) {
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700433 size_t allocated_buffers = 0;
434 size_t largest_alloc_size = 0;
435 size_t total_alloc_size = 0;
436 size_t free_buffers = 0;
437 size_t largest_free_size = 0;
438 size_t total_free_size = 0;
439
440 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
441 n = rb_next(n)) {
442 buffer = rb_entry(n, struct binder_buffer, rb_node);
443 buffer_size = binder_alloc_buffer_size(alloc, buffer);
444 allocated_buffers++;
445 total_alloc_size += buffer_size;
446 if (buffer_size > largest_alloc_size)
447 largest_alloc_size = buffer_size;
448 }
449 for (n = rb_first(&alloc->free_buffers); n != NULL;
450 n = rb_next(n)) {
451 buffer = rb_entry(n, struct binder_buffer, rb_node);
452 buffer_size = binder_alloc_buffer_size(alloc, buffer);
453 free_buffers++;
454 total_free_size += buffer_size;
455 if (buffer_size > largest_free_size)
456 largest_free_size = buffer_size;
457 }
Sherry Yang128f3802018-08-07 12:57:13 -0700458 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
459 "%d: binder_alloc_buf size %zd failed, no address space\n",
460 alloc->pid, size);
461 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
462 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
463 total_alloc_size, allocated_buffers,
464 largest_alloc_size, total_free_size,
465 free_buffers, largest_free_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700466 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700467 }
468 if (n == NULL) {
469 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
470 buffer_size = binder_alloc_buffer_size(alloc, buffer);
471 }
472
473 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
474 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
475 alloc->pid, size, buffer, buffer_size);
476
Carlos Llamasc38a8982023-12-01 17:21:38 +0000477 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
Sherry Yang74310e02017-08-23 08:46:41 -0700478 WARN_ON(n && buffer_size != size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000479 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700480 if (end_page_addr > has_page_addr)
481 end_page_addr = has_page_addr;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000482 ret = binder_update_page_range(alloc, 1, PAGE_ALIGN(buffer->user_data),
483 end_page_addr);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700484 if (ret)
485 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700486
Todd Kjos0c972a02017-06-29 12:01:41 -0700487 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700488 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700489
Sherry Yang74310e02017-08-23 08:46:41 -0700490 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
491 if (!new_buffer) {
492 pr_err("%s: %d failed to alloc new buffer struct\n",
493 __func__, alloc->pid);
494 goto err_alloc_buf_struct_failed;
495 }
Carlos Llamasc38a8982023-12-01 17:21:38 +0000496 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700497 list_add(&new_buffer->entry, &buffer->entry);
498 new_buffer->free = 1;
499 binder_insert_free_buffer(alloc, new_buffer);
500 }
Sherry Yang74310e02017-08-23 08:46:41 -0700501
502 rb_erase(best_fit, &alloc->free_buffers);
503 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800504 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700505 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700506 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
507 "%d: binder_alloc_buf size %zd got %pK\n",
508 alloc->pid, size, buffer);
509 buffer->data_size = data_size;
510 buffer->offsets_size = offsets_size;
511 buffer->async_transaction = is_async;
512 buffer->extra_buffers_size = extra_buffers_size;
Martijn Coenen261e7812020-08-21 14:25:44 +0200513 buffer->pid = pid;
Hang Lua7dc1e62021-04-09 17:40:46 +0800514 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700515 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000516 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700517 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
518 "%d: binder_alloc_buf size %zd async free %zd\n",
519 alloc->pid, size, alloc->free_async_space);
Martijn Coenen261e7812020-08-21 14:25:44 +0200520 if (alloc->free_async_space < alloc->buffer_size / 10) {
521 /*
522 * Start detecting spammers once we have less than 20%
523 * of async space left (which is less than 10% of total
524 * buffer size).
525 */
Hang Lua7dc1e62021-04-09 17:40:46 +0800526 buffer->oneway_spam_suspect = debug_low_async_space_locked(alloc, pid);
527 } else {
528 alloc->oneway_spam_detected = false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200529 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700530 }
531 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700532
533err_alloc_buf_struct_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000534 binder_update_page_range(alloc, 0, PAGE_ALIGN(buffer->user_data),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400535 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700536 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700537}
538
539/**
540 * binder_alloc_new_buf() - Allocate a new binder buffer
541 * @alloc: binder_alloc for this proc
542 * @data_size: size of user data buffer
543 * @offsets_size: user specified buffer offset
544 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
545 * @is_async: buffer for async transaction
Martijn Coenen261e7812020-08-21 14:25:44 +0200546 * @pid: pid to attribute allocation to (used for debugging)
Todd Kjos0c972a02017-06-29 12:01:41 -0700547 *
548 * Allocate a new buffer given the requested sizes. Returns
549 * the kernel version of the buffer pointer. The size allocated
550 * is the sum of the three given sizes (each rounded up to
551 * pointer-sized boundary)
552 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000553 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700554 */
555struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
556 size_t data_size,
557 size_t offsets_size,
558 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200559 int is_async,
560 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700561{
562 struct binder_buffer *buffer;
563
564 mutex_lock(&alloc->mutex);
565 buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200566 extra_buffers_size, is_async, pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700567 mutex_unlock(&alloc->mutex);
568 return buffer;
569}
570
Carlos Llamasc38a8982023-12-01 17:21:38 +0000571static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700572{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000573 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700574}
575
Carlos Llamasc38a8982023-12-01 17:21:38 +0000576static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700577{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000578 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700579}
580
581static void binder_delete_free_buffer(struct binder_alloc *alloc,
582 struct binder_buffer *buffer)
583{
584 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700585 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530586
Todd Kjos0c972a02017-06-29 12:01:41 -0700587 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700588 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700589 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700590 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
591 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700592 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000593 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800594 alloc->pid, buffer->user_data,
595 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700596 }
597
598 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700599 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700600 if (buffer_start_page(next) == buffer_start_page(buffer)) {
601 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700602 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000603 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700604 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800605 buffer->user_data,
606 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700607 }
608 }
Sherry Yang74310e02017-08-23 08:46:41 -0700609
Todd Kjosbde4a192019-02-08 10:35:20 -0800610 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700611 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000612 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800613 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700614 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700615 }
Sherry Yang74310e02017-08-23 08:46:41 -0700616
617 if (to_free) {
618 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000619 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800620 alloc->pid, buffer->user_data,
621 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000622 next ? next->user_data : 0);
Sherry Yang74310e02017-08-23 08:46:41 -0700623 binder_update_page_range(alloc, 0, buffer_start_page(buffer),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400624 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700625 }
626 list_del(&buffer->entry);
627 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700628}
629
630static void binder_free_buf_locked(struct binder_alloc *alloc,
631 struct binder_buffer *buffer)
632{
633 size_t size, buffer_size;
634
635 buffer_size = binder_alloc_buffer_size(alloc, buffer);
636
637 size = ALIGN(buffer->data_size, sizeof(void *)) +
638 ALIGN(buffer->offsets_size, sizeof(void *)) +
639 ALIGN(buffer->extra_buffers_size, sizeof(void *));
640
641 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
642 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
643 alloc->pid, buffer, size, buffer_size);
644
645 BUG_ON(buffer->free);
646 BUG_ON(size > buffer_size);
647 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800648 BUG_ON(buffer->user_data < alloc->buffer);
649 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700650
651 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000652 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700653 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
654 "%d: binder_free_buf size %zd async free %zd\n",
655 alloc->pid, size, alloc->free_async_space);
656 }
657
Carlos Llamasc38a8982023-12-01 17:21:38 +0000658 binder_update_page_range(alloc, 0, PAGE_ALIGN(buffer->user_data),
659 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700660
661 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
662 buffer->free = 1;
663 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700664 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700665
666 if (next->free) {
667 rb_erase(&next->rb_node, &alloc->free_buffers);
668 binder_delete_free_buffer(alloc, next);
669 }
670 }
671 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700672 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700673
674 if (prev->free) {
675 binder_delete_free_buffer(alloc, buffer);
676 rb_erase(&prev->rb_node, &alloc->free_buffers);
677 buffer = prev;
678 }
679 }
680 binder_insert_free_buffer(alloc, buffer);
681}
682
Todd Kjos0f966cb2020-11-20 15:37:43 -0800683static void binder_alloc_clear_buf(struct binder_alloc *alloc,
684 struct binder_buffer *buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700685/**
686 * binder_alloc_free_buf() - free a binder buffer
687 * @alloc: binder_alloc for this proc
688 * @buffer: kernel pointer to buffer
689 *
YangHui4b463822020-08-18 09:34:04 +0800690 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700691 */
692void binder_alloc_free_buf(struct binder_alloc *alloc,
693 struct binder_buffer *buffer)
694{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800695 /*
696 * We could eliminate the call to binder_alloc_clear_buf()
697 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000698 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800699 * increase contention for the alloc mutex if clear_on_free
700 * is used frequently for large buffers. The mutex is not
701 * needed for correctness here.
702 */
703 if (buffer->clear_on_free) {
704 binder_alloc_clear_buf(alloc, buffer);
705 buffer->clear_on_free = false;
706 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700707 mutex_lock(&alloc->mutex);
708 binder_free_buf_locked(alloc, buffer);
709 mutex_unlock(&alloc->mutex);
710}
711
712/**
713 * binder_alloc_mmap_handler() - map virtual address space for proc
714 * @alloc: alloc structure for this proc
715 * @vma: vma passed to mmap()
716 *
717 * Called by binder_mmap() to initialize the space specified in
718 * vma for allocating binder buffers
719 *
720 * Return:
721 * 0 = success
722 * -EBUSY = address space already mapped
723 * -ENOMEM = failed to map memory to given address space
724 */
725int binder_alloc_mmap_handler(struct binder_alloc *alloc,
726 struct vm_area_struct *vma)
727{
728 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700729 const char *failure_string;
730 struct binder_buffer *buffer;
731
Carlos Llamasd276fb42022-11-04 23:12:35 +0000732 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
733 ret = -EINVAL;
734 failure_string = "invalid vma->vm_mm";
735 goto err_invalid_mm;
736 }
737
Todd Kjos0c972a02017-06-29 12:01:41 -0700738 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200739 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700740 ret = -EBUSY;
741 failure_string = "already mapped";
742 goto err_already_mapped;
743 }
Jann Horn45d02f72019-10-16 17:01:18 +0200744 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
745 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200746 mutex_unlock(&binder_alloc_mmap_lock);
747
Carlos Llamasc38a8982023-12-01 17:21:38 +0000748 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200749
Jann Horn45d02f72019-10-16 17:01:18 +0200750 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700751 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700752 GFP_KERNEL);
753 if (alloc->pages == NULL) {
754 ret = -ENOMEM;
755 failure_string = "alloc page array";
756 goto err_alloc_pages_failed;
757 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700758
Sherry Yang74310e02017-08-23 08:46:41 -0700759 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
760 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700761 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700762 failure_string = "alloc buffer struct";
763 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700764 }
Sherry Yang74310e02017-08-23 08:46:41 -0700765
Todd Kjosbde4a192019-02-08 10:35:20 -0800766 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700767 list_add(&buffer->entry, &alloc->buffers);
768 buffer->free = 1;
769 binder_insert_free_buffer(alloc, buffer);
770 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000771
772 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900773 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700774
775 return 0;
776
Sherry Yang74310e02017-08-23 08:46:41 -0700777err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700778 kfree(alloc->pages);
779 alloc->pages = NULL;
780err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000781 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200782 mutex_lock(&binder_alloc_mmap_lock);
783 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700784err_already_mapped:
785 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000786err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700787 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
788 "%s: %d %lx-%lx %s failed %d\n", __func__,
789 alloc->pid, vma->vm_start, vma->vm_end,
790 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700791 return ret;
792}
793
794
795void binder_alloc_deferred_release(struct binder_alloc *alloc)
796{
797 struct rb_node *n;
798 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700799 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700800
Todd Kjos0c972a02017-06-29 12:01:41 -0700801 buffers = 0;
802 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000803 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900804
Todd Kjos0c972a02017-06-29 12:01:41 -0700805 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700806 buffer = rb_entry(n, struct binder_buffer, rb_node);
807
808 /* Transaction should already have been freed */
809 BUG_ON(buffer->transaction);
810
Todd Kjos0f966cb2020-11-20 15:37:43 -0800811 if (buffer->clear_on_free) {
812 binder_alloc_clear_buf(alloc, buffer);
813 buffer->clear_on_free = false;
814 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700815 binder_free_buf_locked(alloc, buffer);
816 buffers++;
817 }
818
Sherry Yang74310e02017-08-23 08:46:41 -0700819 while (!list_empty(&alloc->buffers)) {
820 buffer = list_first_entry(&alloc->buffers,
821 struct binder_buffer, entry);
822 WARN_ON(!buffer->free);
823
824 list_del(&buffer->entry);
825 WARN_ON_ONCE(!list_empty(&alloc->buffers));
826 kfree(buffer);
827 }
828
Todd Kjos0c972a02017-06-29 12:01:41 -0700829 page_count = 0;
830 if (alloc->pages) {
831 int i;
832
833 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000834 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700835 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700836
Sherry Yangf2517eb2017-08-23 08:46:42 -0700837 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700838 continue;
839
Sherry Yangf2517eb2017-08-23 08:46:42 -0700840 on_lru = list_lru_del(&binder_alloc_lru,
841 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700842 page_addr = alloc->buffer + i * PAGE_SIZE;
843 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000844 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700845 __func__, alloc->pid, i, page_addr,
846 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700847 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700848 page_count++;
849 }
850 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700851 }
852 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400853 if (alloc->vma_vm_mm)
854 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700855
856 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
857 "%s: %d buffers %d, pages %d\n",
858 __func__, alloc->pid, buffers, page_count);
859}
860
861static void print_binder_buffer(struct seq_file *m, const char *prefix,
862 struct binder_buffer *buffer)
863{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000864 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800865 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700866 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700867 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700868 buffer->transaction ? "active" : "delivered");
869}
870
871/**
872 * binder_alloc_print_allocated() - print buffer info
873 * @m: seq_file for output via seq_printf()
874 * @alloc: binder_alloc for this proc
875 *
876 * Prints information about every buffer associated with
877 * the binder_alloc state to the given seq_file
878 */
879void binder_alloc_print_allocated(struct seq_file *m,
880 struct binder_alloc *alloc)
881{
882 struct rb_node *n;
883
884 mutex_lock(&alloc->mutex);
885 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
886 print_binder_buffer(m, " buffer",
887 rb_entry(n, struct binder_buffer, rb_node));
888 mutex_unlock(&alloc->mutex);
889}
890
891/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700892 * binder_alloc_print_pages() - print page usage
893 * @m: seq_file for output via seq_printf()
894 * @alloc: binder_alloc for this proc
895 */
896void binder_alloc_print_pages(struct seq_file *m,
897 struct binder_alloc *alloc)
898{
899 struct binder_lru_page *page;
900 int i;
901 int active = 0;
902 int lru = 0;
903 int free = 0;
904
905 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200906 /*
907 * Make sure the binder_alloc is fully initialized, otherwise we might
908 * read inconsistent state.
909 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000910 if (binder_alloc_get_vma(alloc) != NULL) {
911 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
912 page = &alloc->pages[i];
913 if (!page->page_ptr)
914 free++;
915 else if (list_empty(&page->lru))
916 active++;
917 else
918 lru++;
919 }
Sherry Yang8ef46652017-08-31 11:56:36 -0700920 }
921 mutex_unlock(&alloc->mutex);
922 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100923 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -0700924}
925
926/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700927 * binder_alloc_get_allocated_count() - return count of buffers
928 * @alloc: binder_alloc for this proc
929 *
930 * Return: count of allocated buffers
931 */
932int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
933{
934 struct rb_node *n;
935 int count = 0;
936
937 mutex_lock(&alloc->mutex);
938 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
939 count++;
940 mutex_unlock(&alloc->mutex);
941 return count;
942}
943
944
945/**
946 * binder_alloc_vma_close() - invalidate address space
947 * @alloc: binder_alloc for this proc
948 *
949 * Called from binder_vma_close() when releasing address space.
950 * Clears alloc->vma to prevent new incoming transactions from
951 * allocating more buffers.
952 */
953void binder_alloc_vma_close(struct binder_alloc *alloc)
954{
Minchan Kimda1b9562018-08-23 14:29:56 +0900955 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700956}
957
958/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700959 * binder_alloc_free_page() - shrinker callback to free pages
960 * @item: item to free
961 * @lock: lock protecting the item
962 * @cb_arg: callback argument
963 *
964 * Called from list_lru_walk() in binder_shrink_scan() to free
965 * up pages when the system is under memory pressure.
966 */
967enum lru_status binder_alloc_free_page(struct list_head *item,
968 struct list_lru_one *lru,
969 spinlock_t *lock,
970 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -0800971 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -0700972{
973 struct mm_struct *mm = NULL;
974 struct binder_lru_page *page = container_of(item,
975 struct binder_lru_page,
976 lru);
977 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000978 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700979 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -0700980 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700981
982 alloc = page->alloc;
983 if (!mutex_trylock(&alloc->mutex))
984 goto err_get_alloc_mutex_failed;
985
986 if (!page->page_ptr)
987 goto err_page_already_freed;
988
989 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000990 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -0800991
992 mm = alloc->vma_vm_mm;
993 if (!mmget_not_zero(mm))
994 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700995 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -0700996 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +0000997 vma = vma_lookup(mm, page_addr);
998 if (vma && vma != binder_alloc_get_vma(alloc))
999 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001000
Sherry Yanga1b22892017-10-03 16:15:00 -07001001 list_lru_isolate(lru, item);
1002 spin_unlock(lock);
1003
1004 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001005 trace_binder_unmap_user_start(alloc, index);
1006
Todd Kjosc41358a2019-02-08 10:35:19 -08001007 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001008
Sherry Yange41e1642017-08-23 08:46:43 -07001009 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001010 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001011 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001012 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001013
Sherry Yange41e1642017-08-23 08:46:43 -07001014 trace_binder_unmap_kernel_start(alloc, index);
1015
Sherry Yangf2517eb2017-08-23 08:46:42 -07001016 __free_page(page->page_ptr);
1017 page->page_ptr = NULL;
1018
Sherry Yange41e1642017-08-23 08:46:43 -07001019 trace_binder_unmap_kernel_end(alloc, index);
1020
Sherry Yanga1b22892017-10-03 16:15:00 -07001021 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001022 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001023 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001024
Carlos Llamas8dce2882023-12-01 17:21:31 +00001025err_invalid_vma:
1026 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001027err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001028 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001029err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001030err_page_already_freed:
1031 mutex_unlock(&alloc->mutex);
1032err_get_alloc_mutex_failed:
1033 return LRU_SKIP;
1034}
1035
1036static unsigned long
1037binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1038{
1039 unsigned long ret = list_lru_count(&binder_alloc_lru);
1040 return ret;
1041}
1042
1043static unsigned long
1044binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1045{
1046 unsigned long ret;
1047
1048 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1049 NULL, sc->nr_to_scan);
1050 return ret;
1051}
1052
Sherry Yangde7bbe32017-10-06 16:12:05 -04001053static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001054 .count_objects = binder_shrink_count,
1055 .scan_objects = binder_shrink_scan,
1056 .seeks = DEFAULT_SEEKS,
1057};
1058
1059/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001060 * binder_alloc_init() - called by binder_open() for per-proc initialization
1061 * @alloc: binder_alloc for this proc
1062 *
1063 * Called from binder_open() to initialize binder_alloc fields for
1064 * new binder proc
1065 */
1066void binder_alloc_init(struct binder_alloc *alloc)
1067{
Todd Kjos0c972a02017-06-29 12:01:41 -07001068 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001069 alloc->vma_vm_mm = current->mm;
1070 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001071 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001072 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001073}
1074
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001075int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001076{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001077 int ret = list_lru_init(&binder_alloc_lru);
1078
1079 if (ret == 0) {
1080 ret = register_shrinker(&binder_shrinker);
1081 if (ret)
1082 list_lru_destroy(&binder_alloc_lru);
1083 }
1084 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001085}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001086
Qi Zheng03eebad2023-06-25 15:49:37 +00001087void binder_alloc_shrinker_exit(void)
1088{
1089 unregister_shrinker(&binder_shrinker);
1090 list_lru_destroy(&binder_alloc_lru);
1091}
1092
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001093/**
1094 * check_buffer() - verify that buffer/offset is safe to access
1095 * @alloc: binder_alloc for this proc
1096 * @buffer: binder buffer to be accessed
1097 * @offset: offset into @buffer data
1098 * @bytes: bytes to access from offset
1099 *
1100 * Check that the @offset/@bytes are within the size of the given
1101 * @buffer and that the buffer is currently active and not freeable.
1102 * Offsets must also be multiples of sizeof(u32). The kernel is
1103 * allowed to touch the buffer in two cases:
1104 *
1105 * 1) when the buffer is being created:
1106 * (buffer->free == 0 && buffer->allow_user_free == 0)
1107 * 2) when the buffer is being torn down:
1108 * (buffer->free == 0 && buffer->transaction == NULL).
1109 *
1110 * Return: true if the buffer is safe to access
1111 */
1112static inline bool check_buffer(struct binder_alloc *alloc,
1113 struct binder_buffer *buffer,
1114 binder_size_t offset, size_t bytes)
1115{
1116 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1117
1118 return buffer_size >= bytes &&
1119 offset <= buffer_size - bytes &&
1120 IS_ALIGNED(offset, sizeof(u32)) &&
1121 !buffer->free &&
1122 (!buffer->allow_user_free || !buffer->transaction);
1123}
1124
1125/**
1126 * binder_alloc_get_page() - get kernel pointer for given buffer offset
1127 * @alloc: binder_alloc for this proc
1128 * @buffer: binder buffer to be accessed
1129 * @buffer_offset: offset into @buffer data
1130 * @pgoffp: address to copy final page offset to
1131 *
1132 * Lookup the struct page corresponding to the address
Todd Kjosbde4a192019-02-08 10:35:20 -08001133 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001134 * NULL, the byte-offset into the page is written there.
1135 *
1136 * The caller is responsible to ensure that the offset points
1137 * to a valid address within the @buffer and that @buffer is
1138 * not freeable by the user. Since it can't be freed, we are
1139 * guaranteed that the corresponding elements of @alloc->pages[]
1140 * cannot change.
1141 *
1142 * Return: struct page
1143 */
1144static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
1145 struct binder_buffer *buffer,
1146 binder_size_t buffer_offset,
1147 pgoff_t *pgoffp)
1148{
1149 binder_size_t buffer_space_offset = buffer_offset +
Todd Kjosbde4a192019-02-08 10:35:20 -08001150 (buffer->user_data - alloc->buffer);
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001151 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
1152 size_t index = buffer_space_offset >> PAGE_SHIFT;
1153 struct binder_lru_page *lru_page;
1154
1155 lru_page = &alloc->pages[index];
1156 *pgoffp = pgoff;
1157 return lru_page->page_ptr;
1158}
1159
1160/**
Todd Kjos0f966cb2020-11-20 15:37:43 -08001161 * binder_alloc_clear_buf() - zero out buffer
1162 * @alloc: binder_alloc for this proc
1163 * @buffer: binder buffer to be cleared
1164 *
1165 * memset the given buffer to 0
1166 */
1167static void binder_alloc_clear_buf(struct binder_alloc *alloc,
1168 struct binder_buffer *buffer)
1169{
1170 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
1171 binder_size_t buffer_offset = 0;
1172
1173 while (bytes) {
1174 unsigned long size;
1175 struct page *page;
1176 pgoff_t pgoff;
1177 void *kptr;
1178
1179 page = binder_alloc_get_page(alloc, buffer,
1180 buffer_offset, &pgoff);
1181 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1182 kptr = kmap(page) + pgoff;
1183 memset(kptr, 0, size);
1184 kunmap(page);
1185 bytes -= size;
1186 buffer_offset += size;
1187 }
1188}
1189
1190/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001191 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1192 * @alloc: binder_alloc for this proc
1193 * @buffer: binder buffer to be accessed
1194 * @buffer_offset: offset into @buffer data
1195 * @from: userspace pointer to source buffer
1196 * @bytes: bytes to copy
1197 *
1198 * Copy bytes from source userspace to target buffer.
1199 *
1200 * Return: bytes remaining to be copied
1201 */
1202unsigned long
1203binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1204 struct binder_buffer *buffer,
1205 binder_size_t buffer_offset,
1206 const void __user *from,
1207 size_t bytes)
1208{
1209 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1210 return bytes;
1211
1212 while (bytes) {
1213 unsigned long size;
1214 unsigned long ret;
1215 struct page *page;
1216 pgoff_t pgoff;
1217 void *kptr;
1218
1219 page = binder_alloc_get_page(alloc, buffer,
1220 buffer_offset, &pgoff);
1221 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1222 kptr = kmap(page) + pgoff;
1223 ret = copy_from_user(kptr, from, size);
1224 kunmap(page);
1225 if (ret)
1226 return bytes - size + ret;
1227 bytes -= size;
1228 from += size;
1229 buffer_offset += size;
1230 }
1231 return 0;
1232}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001233
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001234static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1235 bool to_buffer,
1236 struct binder_buffer *buffer,
1237 binder_size_t buffer_offset,
1238 void *ptr,
1239 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001240{
1241 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001242 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1243 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001244
1245 while (bytes) {
1246 unsigned long size;
1247 struct page *page;
1248 pgoff_t pgoff;
1249 void *tmpptr;
1250 void *base_ptr;
1251
1252 page = binder_alloc_get_page(alloc, buffer,
1253 buffer_offset, &pgoff);
1254 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1255 base_ptr = kmap_atomic(page);
1256 tmpptr = base_ptr + pgoff;
1257 if (to_buffer)
1258 memcpy(tmpptr, ptr, size);
1259 else
1260 memcpy(ptr, tmpptr, size);
1261 /*
1262 * kunmap_atomic() takes care of flushing the cache
1263 * if this device has VIVT cache arch
1264 */
1265 kunmap_atomic(base_ptr);
1266 bytes -= size;
1267 pgoff = 0;
1268 ptr = ptr + size;
1269 buffer_offset += size;
1270 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001271 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001272}
1273
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001274int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1275 struct binder_buffer *buffer,
1276 binder_size_t buffer_offset,
1277 void *src,
1278 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001279{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001280 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1281 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001282}
1283
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001284int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1285 void *dest,
1286 struct binder_buffer *buffer,
1287 binder_size_t buffer_offset,
1288 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001289{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001290 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1291 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001292}
1293