blob: e0e1217f5d2c373223e02d2e0880b154625ac0c1 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Todd Kjos0c972a02017-06-29 12:01:41 -07002/* binder_alloc.c
3 *
4 * Android IPC Subsystem
5 *
6 * Copyright (C) 2007-2017 Google, Inc.
Todd Kjos0c972a02017-06-29 12:01:41 -07007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Todd Kjos0c972a02017-06-29 12:01:41 -070011#include <linux/list.h>
12#include <linux/sched/mm.h>
13#include <linux/module.h>
14#include <linux/rtmutex.h>
15#include <linux/rbtree.h>
16#include <linux/seq_file.h>
17#include <linux/vmalloc.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
Sherry Yangf2517eb2017-08-23 08:46:42 -070020#include <linux/list_lru.h>
Sherry Yang128f3802018-08-07 12:57:13 -070021#include <linux/ratelimit.h>
Guenter Roeck1e81c572018-07-23 14:47:23 -070022#include <asm/cacheflush.h>
Todd Kjos1a7c3d92019-02-08 10:35:14 -080023#include <linux/uaccess.h>
24#include <linux/highmem.h>
Jann Horn45d02f72019-10-16 17:01:18 +020025#include <linux/sizes.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070026#include "binder_alloc.h"
27#include "binder_trace.h"
Zhuguangqing1174e452021-03-09 15:47:43 +080028#include <trace/hooks/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070029
Sherry Yangf2517eb2017-08-23 08:46:42 -070030struct list_lru binder_alloc_lru;
31
Todd Kjos0c972a02017-06-29 12:01:41 -070032static DEFINE_MUTEX(binder_alloc_mmap_lock);
33
34enum {
Sherry Yang128f3802018-08-07 12:57:13 -070035 BINDER_DEBUG_USER_ERROR = 1U << 0,
Todd Kjos0c972a02017-06-29 12:01:41 -070036 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
37 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
38 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
39};
Sherry Yang128f3802018-08-07 12:57:13 -070040static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
Todd Kjos0c972a02017-06-29 12:01:41 -070041
42module_param_named(debug_mask, binder_alloc_debug_mask,
43 uint, 0644);
44
45#define binder_alloc_debug(mask, x...) \
46 do { \
47 if (binder_alloc_debug_mask & mask) \
Sherry Yang128f3802018-08-07 12:57:13 -070048 pr_info_ratelimited(x); \
Todd Kjos0c972a02017-06-29 12:01:41 -070049 } while (0)
50
Sherry Yange21762192017-08-23 08:46:39 -070051static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
52{
53 return list_entry(buffer->entry.next, struct binder_buffer, entry);
54}
55
56static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
57{
58 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
59}
60
Todd Kjos0c972a02017-06-29 12:01:41 -070061static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
62 struct binder_buffer *buffer)
63{
64 if (list_is_last(&buffer->entry, &alloc->buffers))
Todd Kjosbde4a192019-02-08 10:35:20 -080065 return alloc->buffer + alloc->buffer_size - buffer->user_data;
66 return binder_buffer_next(buffer)->user_data - buffer->user_data;
Todd Kjos0c972a02017-06-29 12:01:41 -070067}
68
69static void binder_insert_free_buffer(struct binder_alloc *alloc,
70 struct binder_buffer *new_buffer)
71{
72 struct rb_node **p = &alloc->free_buffers.rb_node;
73 struct rb_node *parent = NULL;
74 struct binder_buffer *buffer;
75 size_t buffer_size;
76 size_t new_buffer_size;
77
78 BUG_ON(!new_buffer->free);
79
80 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
81
82 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
83 "%d: add free buffer, size %zd, at %pK\n",
84 alloc->pid, new_buffer_size, new_buffer);
85
86 while (*p) {
87 parent = *p;
88 buffer = rb_entry(parent, struct binder_buffer, rb_node);
89 BUG_ON(!buffer->free);
90
91 buffer_size = binder_alloc_buffer_size(alloc, buffer);
92
93 if (new_buffer_size < buffer_size)
94 p = &parent->rb_left;
95 else
96 p = &parent->rb_right;
97 }
98 rb_link_node(&new_buffer->rb_node, parent, p);
99 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
100}
101
102static void binder_insert_allocated_buffer_locked(
103 struct binder_alloc *alloc, struct binder_buffer *new_buffer)
104{
105 struct rb_node **p = &alloc->allocated_buffers.rb_node;
106 struct rb_node *parent = NULL;
107 struct binder_buffer *buffer;
108
109 BUG_ON(new_buffer->free);
110
111 while (*p) {
112 parent = *p;
113 buffer = rb_entry(parent, struct binder_buffer, rb_node);
114 BUG_ON(buffer->free);
115
Todd Kjosbde4a192019-02-08 10:35:20 -0800116 if (new_buffer->user_data < buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700117 p = &parent->rb_left;
Todd Kjosbde4a192019-02-08 10:35:20 -0800118 else if (new_buffer->user_data > buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700119 p = &parent->rb_right;
120 else
121 BUG();
122 }
123 rb_link_node(&new_buffer->rb_node, parent, p);
124 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
125}
126
Todd Kjos53d311cf2017-06-29 12:01:51 -0700127static struct binder_buffer *binder_alloc_prepare_to_free_locked(
Todd Kjos0c972a02017-06-29 12:01:41 -0700128 struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000129 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700130{
131 struct rb_node *n = alloc->allocated_buffers.rb_node;
132 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700133
134 while (n) {
135 buffer = rb_entry(n, struct binder_buffer, rb_node);
136 BUG_ON(buffer->free);
137
Carlos Llamasc38a8982023-12-01 17:21:38 +0000138 if (user_ptr < buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700139 n = n->rb_left;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000140 } else if (user_ptr > buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700141 n = n->rb_right;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000142 } else {
Todd Kjos53d311cf2017-06-29 12:01:51 -0700143 /*
144 * Guard against user threads attempting to
Todd Kjos7bada552018-11-06 15:55:32 -0800145 * free the buffer when in use by kernel or
146 * after it's already been freed.
Todd Kjos53d311cf2017-06-29 12:01:51 -0700147 */
Todd Kjos7bada552018-11-06 15:55:32 -0800148 if (!buffer->allow_user_free)
149 return ERR_PTR(-EPERM);
150 buffer->allow_user_free = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700151 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700152 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700153 }
154 return NULL;
155}
156
157/**
Joel Fernandes (Google)5dc54a02019-09-30 16:12:50 -0400158 * binder_alloc_prepare_to_free() - get buffer given user ptr
Todd Kjos0c972a02017-06-29 12:01:41 -0700159 * @alloc: binder_alloc for this proc
160 * @user_ptr: User pointer to buffer data
161 *
162 * Validate userspace pointer to buffer data and return buffer corresponding to
163 * that user pointer. Search the rb tree for buffer that matches user data
164 * pointer.
165 *
166 * Return: Pointer to buffer or NULL
167 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700168struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000169 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700170{
171 struct binder_buffer *buffer;
172
173 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700174 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700175 mutex_unlock(&alloc->mutex);
176 return buffer;
177}
178
Carlos Llamas0b243682023-12-01 17:21:39 +0000179static void binder_free_page_range(struct binder_alloc *alloc,
180 unsigned long start, unsigned long end)
181{
182 struct binder_lru_page *page;
183 unsigned long page_addr;
184
185 trace_binder_update_page_range(alloc, false, start, end);
186
187 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
188 size_t index;
189 int ret;
190
191 index = (page_addr - alloc->buffer) / PAGE_SIZE;
192 page = &alloc->pages[index];
193
194 trace_binder_free_lru_start(alloc, index);
195
196 ret = list_lru_add(&binder_alloc_lru, &page->lru);
197 WARN_ON(!ret);
198
199 trace_binder_free_lru_end(alloc, index);
200 }
201}
202
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000203static int binder_install_single_page(struct binder_alloc *alloc,
204 struct binder_lru_page *lru_page,
205 unsigned long addr)
206{
207 struct page *page;
208 int ret = 0;
209
210 if (!mmget_not_zero(alloc->vma_vm_mm))
211 return -ESRCH;
212
213 mmap_write_lock(alloc->vma_vm_mm);
214 if (!alloc->vma) {
215 pr_err("%d: %s failed, no vma\n", alloc->pid, __func__);
216 ret = -ESRCH;
217 goto out;
218 }
219
220 page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
221 if (!page) {
222 pr_err("%d: failed to allocate page\n", alloc->pid);
223 ret = -ENOMEM;
224 goto out;
225 }
226
227 ret = vm_insert_page(alloc->vma, addr, page);
228 if (ret) {
229 pr_err("%d: %s failed to insert page at %lx with %d\n",
230 alloc->pid, __func__, addr, ret);
231 __free_page(page);
232 ret = -ENOMEM;
233 goto out;
234 }
235
236 lru_page->page_ptr = page;
237out:
238 mmap_write_unlock(alloc->vma_vm_mm);
239 mmput_async(alloc->vma_vm_mm);
240 return ret;
241}
242
Carlos Llamas0b243682023-12-01 17:21:39 +0000243static int binder_allocate_page_range(struct binder_alloc *alloc,
244 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700245{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000246 struct binder_lru_page *page;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000247 unsigned long page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700248
249 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamas0b243682023-12-01 17:21:39 +0000250 "%d: allocate pages %lx-%lx\n",
251 alloc->pid, start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700252
253 if (end <= start)
254 return 0;
255
Carlos Llamas0b243682023-12-01 17:21:39 +0000256 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700257
258 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000259 unsigned long index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700260 bool on_lru;
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000261 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700262
Sherry Yange41e1642017-08-23 08:46:43 -0700263 index = (page_addr - alloc->buffer) / PAGE_SIZE;
264 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700265
Sherry Yangf2517eb2017-08-23 08:46:42 -0700266 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700267 trace_binder_alloc_lru_start(alloc, index);
268
Sherry Yangf2517eb2017-08-23 08:46:42 -0700269 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
270 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700271
272 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700273 continue;
274 }
275
Sherry Yange41e1642017-08-23 08:46:43 -0700276 trace_binder_alloc_page_start(alloc, index);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000277
Sherry Yangf2517eb2017-08-23 08:46:42 -0700278 page->alloc = alloc;
279 INIT_LIST_HEAD(&page->lru);
280
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000281 ret = binder_install_single_page(alloc, page, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700282 if (ret) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000283 binder_free_page_range(alloc, start, page_addr);
284 return ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700285 }
Sherry Yange41e1642017-08-23 08:46:43 -0700286
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100287 if (index + 1 > alloc->pages_high)
288 alloc->pages_high = index + 1;
289
Sherry Yange41e1642017-08-23 08:46:43 -0700290 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700291 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700292
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000293 return 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700294}
295
Minchan Kimda1b9562018-08-23 14:29:56 +0900296static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
297 struct vm_area_struct *vma)
298{
Carlos Llamasb094b042023-05-30 19:43:37 +0000299 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
300 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900301}
302
303static inline struct vm_area_struct *binder_alloc_get_vma(
304 struct binder_alloc *alloc)
305{
Carlos Llamasb094b042023-05-30 19:43:37 +0000306 /* pairs with smp_store_release in binder_alloc_set_vma() */
307 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900308}
309
Carlos Llamase1d195e2023-12-01 17:21:42 +0000310static void debug_no_space_locked(struct binder_alloc *alloc)
311{
312 size_t largest_alloc_size = 0;
313 struct binder_buffer *buffer;
314 size_t allocated_buffers = 0;
315 size_t largest_free_size = 0;
316 size_t total_alloc_size = 0;
317 size_t total_free_size = 0;
318 size_t free_buffers = 0;
319 size_t buffer_size;
320 struct rb_node *n;
321
322 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
323 buffer = rb_entry(n, struct binder_buffer, rb_node);
324 buffer_size = binder_alloc_buffer_size(alloc, buffer);
325 allocated_buffers++;
326 total_alloc_size += buffer_size;
327 if (buffer_size > largest_alloc_size)
328 largest_alloc_size = buffer_size;
329 }
330
331 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
332 buffer = rb_entry(n, struct binder_buffer, rb_node);
333 buffer_size = binder_alloc_buffer_size(alloc, buffer);
334 free_buffers++;
335 total_free_size += buffer_size;
336 if (buffer_size > largest_free_size)
337 largest_free_size = buffer_size;
338 }
339
340 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
341 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
342 total_alloc_size, allocated_buffers,
343 largest_alloc_size, total_free_size,
344 free_buffers, largest_free_size);
345}
346
Carlos Llamas26d06d92023-12-01 17:21:41 +0000347static bool debug_low_async_space_locked(struct binder_alloc *alloc)
Martijn Coenen261e7812020-08-21 14:25:44 +0200348{
349 /*
350 * Find the amount and size of buffers allocated by the current caller;
351 * The idea is that once we cross the threshold, whoever is responsible
352 * for the low async space is likely to try to send another async txn,
353 * and at some point we'll catch them in the act. This is more efficient
354 * than keeping a map per pid.
355 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200356 struct binder_buffer *buffer;
357 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000358 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200359 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000360 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200361
Carlos Llamas081ddad2023-12-01 17:21:43 +0000362 /*
363 * Only start detecting spammers once we have less than 20% of async
364 * space left (which is less than 10% of total buffer size).
365 */
366 if (alloc->free_async_space >= alloc->buffer_size / 10) {
367 alloc->oneway_spam_detected = false;
368 return false;
369 }
370
Martijn Coenen261e7812020-08-21 14:25:44 +0200371 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
372 n = rb_next(n)) {
373 buffer = rb_entry(n, struct binder_buffer, rb_node);
374 if (buffer->pid != pid)
375 continue;
376 if (!buffer->async_transaction)
377 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000378 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200379 num_buffers++;
380 }
381
382 /*
383 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800384 * async space (which is 25% of total buffer size). Oneway spam is only
385 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200386 */
387 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
388 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
389 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
390 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800391 if (!alloc->oneway_spam_detected) {
392 alloc->oneway_spam_detected = true;
393 return true;
394 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200395 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800396 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200397}
398
Carlos Llamasef524f42023-12-01 17:21:46 +0000399/* Callers preallocate @new_buffer, it is freed by this function if unused */
Xiongwei Song3f827242017-12-14 12:15:42 +0800400static struct binder_buffer *binder_alloc_new_buf_locked(
401 struct binder_alloc *alloc,
Carlos Llamasef524f42023-12-01 17:21:46 +0000402 struct binder_buffer *new_buffer,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000403 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000404 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700405{
406 struct rb_node *n = alloc->free_buffers.rb_node;
Todd Kjos0c972a02017-06-29 12:01:41 -0700407 struct rb_node *best_fit = NULL;
Carlos Llamasef524f42023-12-01 17:21:46 +0000408 struct binder_buffer *buffer;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000409 unsigned long has_page_addr;
410 unsigned long end_page_addr;
Carlos Llamasef524f42023-12-01 17:21:46 +0000411 size_t buffer_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700412 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700413
Zhuguangqing1174e452021-03-09 15:47:43 +0800414 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000415
Carlos Llamas11ca0762023-12-01 17:21:34 +0000416 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700417 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
418 "%d: binder_alloc_buf size %zd failed, no async space left\n",
419 alloc->pid, size);
Carlos Llamasef524f42023-12-01 17:21:46 +0000420 buffer = ERR_PTR(-ENOSPC);
421 goto out;
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;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000432 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700433 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000434 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700435 best_fit = n;
436 break;
437 }
438 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000439
Carlos Llamase1d195e2023-12-01 17:21:42 +0000440 if (unlikely(!best_fit)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700441 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
442 "%d: binder_alloc_buf size %zd failed, no address space\n",
443 alloc->pid, size);
Carlos Llamase1d195e2023-12-01 17:21:42 +0000444 debug_no_space_locked(alloc);
Carlos Llamasef524f42023-12-01 17:21:46 +0000445 buffer = ERR_PTR(-ENOSPC);
446 goto out;
Todd Kjos0c972a02017-06-29 12:01:41 -0700447 }
Carlos Llamase1d195e2023-12-01 17:21:42 +0000448
Todd Kjos0c972a02017-06-29 12:01:41 -0700449 if (n == NULL) {
450 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
451 buffer_size = binder_alloc_buffer_size(alloc, buffer);
452 }
453
454 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
455 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
456 alloc->pid, size, buffer, buffer_size);
457
Carlos Llamasc38a8982023-12-01 17:21:38 +0000458 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
Sherry Yang74310e02017-08-23 08:46:41 -0700459 WARN_ON(n && buffer_size != size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000460 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700461 if (end_page_addr > has_page_addr)
462 end_page_addr = has_page_addr;
Carlos Llamas0b243682023-12-01 17:21:39 +0000463 ret = binder_allocate_page_range(alloc, PAGE_ALIGN(buffer->user_data),
464 end_page_addr);
Carlos Llamasef524f42023-12-01 17:21:46 +0000465 if (ret) {
466 buffer = ERR_PTR(ret);
467 goto out;
468 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700469
Todd Kjos0c972a02017-06-29 12:01:41 -0700470 if (buffer_size != size) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000471 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700472 list_add(&new_buffer->entry, &buffer->entry);
473 new_buffer->free = 1;
474 binder_insert_free_buffer(alloc, new_buffer);
Carlos Llamasef524f42023-12-01 17:21:46 +0000475 new_buffer = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700476 }
Sherry Yang74310e02017-08-23 08:46:41 -0700477
478 rb_erase(best_fit, &alloc->free_buffers);
479 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800480 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700481 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700482 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
483 "%d: binder_alloc_buf size %zd got %pK\n",
484 alloc->pid, size, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700485 buffer->async_transaction = is_async;
Hang Lua7dc1e62021-04-09 17:40:46 +0800486 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700487 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000488 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700489 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
490 "%d: binder_alloc_buf size %zd async free %zd\n",
491 alloc->pid, size, alloc->free_async_space);
Carlos Llamas081ddad2023-12-01 17:21:43 +0000492 if (debug_low_async_space_locked(alloc))
493 buffer->oneway_spam_suspect = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700494 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000495
Carlos Llamasef524f42023-12-01 17:21:46 +0000496out:
497 /* Discard possibly unused new_buffer */
498 kfree(new_buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700499 return buffer;
500}
501
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000502/* Calculate the sanitized total size, returns 0 for invalid request */
503static inline size_t sanitized_size(size_t data_size,
504 size_t offsets_size,
505 size_t extra_buffers_size)
506{
507 size_t total, tmp;
508
509 /* Align to pointer size and check for overflows */
510 tmp = ALIGN(data_size, sizeof(void *)) +
511 ALIGN(offsets_size, sizeof(void *));
512 if (tmp < data_size || tmp < offsets_size)
513 return 0;
514 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
515 if (total < tmp || total < extra_buffers_size)
516 return 0;
517
518 /* Pad 0-sized buffers so they get a unique address */
519 total = max(total, sizeof(void *));
520
521 return total;
522}
523
Todd Kjos0c972a02017-06-29 12:01:41 -0700524/**
525 * binder_alloc_new_buf() - Allocate a new binder buffer
526 * @alloc: binder_alloc for this proc
527 * @data_size: size of user data buffer
528 * @offsets_size: user specified buffer offset
529 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
530 * @is_async: buffer for async transaction
531 *
532 * Allocate a new buffer given the requested sizes. Returns
533 * the kernel version of the buffer pointer. The size allocated
534 * is the sum of the three given sizes (each rounded up to
535 * pointer-sized boundary)
536 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000537 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700538 */
539struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
540 size_t data_size,
541 size_t offsets_size,
542 size_t extra_buffers_size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000543 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700544{
Carlos Llamasef524f42023-12-01 17:21:46 +0000545 struct binder_buffer *buffer, *next;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000546 size_t size;
547
548 /* Check binder_alloc is fully initialized */
549 if (!binder_alloc_get_vma(alloc)) {
550 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
551 "%d: binder_alloc_buf, no vma\n",
552 alloc->pid);
553 return ERR_PTR(-ESRCH);
554 }
555
556 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
557 if (unlikely(!size)) {
558 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
559 "%d: got transaction with invalid size %zd-%zd-%zd\n",
560 alloc->pid, data_size, offsets_size,
561 extra_buffers_size);
562 return ERR_PTR(-EINVAL);
563 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700564
Carlos Llamasef524f42023-12-01 17:21:46 +0000565 /* Preallocate the next buffer */
566 next = kzalloc(sizeof(*next), GFP_KERNEL);
567 if (!next)
568 return ERR_PTR(-ENOMEM);
569
Todd Kjos0c972a02017-06-29 12:01:41 -0700570 mutex_lock(&alloc->mutex);
Carlos Llamasef524f42023-12-01 17:21:46 +0000571 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000572 if (IS_ERR(buffer)) {
573 mutex_unlock(&alloc->mutex);
574 goto out;
575 }
576
577 buffer->data_size = data_size;
578 buffer->offsets_size = offsets_size;
579 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000580 buffer->pid = current->tgid;
Todd Kjos0c972a02017-06-29 12:01:41 -0700581 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000582
583out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700584 return buffer;
585}
586
Carlos Llamasc38a8982023-12-01 17:21:38 +0000587static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700588{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000589 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700590}
591
Carlos Llamasc38a8982023-12-01 17:21:38 +0000592static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700593{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000594 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700595}
596
597static void binder_delete_free_buffer(struct binder_alloc *alloc,
598 struct binder_buffer *buffer)
599{
600 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700601 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530602
Todd Kjos0c972a02017-06-29 12:01:41 -0700603 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700604 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700605 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700606 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
607 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700608 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000609 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800610 alloc->pid, buffer->user_data,
611 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700612 }
613
614 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700615 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700616 if (buffer_start_page(next) == buffer_start_page(buffer)) {
617 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700618 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000619 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700620 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800621 buffer->user_data,
622 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700623 }
624 }
Sherry Yang74310e02017-08-23 08:46:41 -0700625
Todd Kjosbde4a192019-02-08 10:35:20 -0800626 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700627 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000628 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800629 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700630 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700631 }
Sherry Yang74310e02017-08-23 08:46:41 -0700632
633 if (to_free) {
634 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000635 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800636 alloc->pid, buffer->user_data,
637 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000638 next ? next->user_data : 0);
Carlos Llamas0b243682023-12-01 17:21:39 +0000639 binder_free_page_range(alloc, buffer_start_page(buffer),
640 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700641 }
642 list_del(&buffer->entry);
643 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700644}
645
646static void binder_free_buf_locked(struct binder_alloc *alloc,
647 struct binder_buffer *buffer)
648{
649 size_t size, buffer_size;
650
651 buffer_size = binder_alloc_buffer_size(alloc, buffer);
652
653 size = ALIGN(buffer->data_size, sizeof(void *)) +
654 ALIGN(buffer->offsets_size, sizeof(void *)) +
655 ALIGN(buffer->extra_buffers_size, sizeof(void *));
656
657 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
658 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
659 alloc->pid, buffer, size, buffer_size);
660
661 BUG_ON(buffer->free);
662 BUG_ON(size > buffer_size);
663 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800664 BUG_ON(buffer->user_data < alloc->buffer);
665 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700666
667 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000668 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700669 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
670 "%d: binder_free_buf size %zd async free %zd\n",
671 alloc->pid, size, alloc->free_async_space);
672 }
673
Carlos Llamas0b243682023-12-01 17:21:39 +0000674 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
675 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700676
677 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
678 buffer->free = 1;
679 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700680 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700681
682 if (next->free) {
683 rb_erase(&next->rb_node, &alloc->free_buffers);
684 binder_delete_free_buffer(alloc, next);
685 }
686 }
687 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700688 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700689
690 if (prev->free) {
691 binder_delete_free_buffer(alloc, buffer);
692 rb_erase(&prev->rb_node, &alloc->free_buffers);
693 buffer = prev;
694 }
695 }
696 binder_insert_free_buffer(alloc, buffer);
697}
698
Carlos Llamas59e0d622023-12-01 17:21:44 +0000699/**
700 * binder_alloc_get_page() - get kernel pointer for given buffer offset
701 * @alloc: binder_alloc for this proc
702 * @buffer: binder buffer to be accessed
703 * @buffer_offset: offset into @buffer data
704 * @pgoffp: address to copy final page offset to
705 *
706 * Lookup the struct page corresponding to the address
707 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
708 * NULL, the byte-offset into the page is written there.
709 *
710 * The caller is responsible to ensure that the offset points
711 * to a valid address within the @buffer and that @buffer is
712 * not freeable by the user. Since it can't be freed, we are
713 * guaranteed that the corresponding elements of @alloc->pages[]
714 * cannot change.
715 *
716 * Return: struct page
717 */
718static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
719 struct binder_buffer *buffer,
720 binder_size_t buffer_offset,
721 pgoff_t *pgoffp)
722{
723 binder_size_t buffer_space_offset = buffer_offset +
724 (buffer->user_data - alloc->buffer);
725 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
726 size_t index = buffer_space_offset >> PAGE_SHIFT;
727 struct binder_lru_page *lru_page;
728
729 lru_page = &alloc->pages[index];
730 *pgoffp = pgoff;
731 return lru_page->page_ptr;
732}
733
734/**
735 * binder_alloc_clear_buf() - zero out buffer
736 * @alloc: binder_alloc for this proc
737 * @buffer: binder buffer to be cleared
738 *
739 * memset the given buffer to 0
740 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800741static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000742 struct binder_buffer *buffer)
743{
744 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
745 binder_size_t buffer_offset = 0;
746
747 while (bytes) {
748 unsigned long size;
749 struct page *page;
750 pgoff_t pgoff;
751 void *kptr;
752
753 page = binder_alloc_get_page(alloc, buffer,
754 buffer_offset, &pgoff);
755 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
756 kptr = kmap(page) + pgoff;
757 memset(kptr, 0, size);
758 kunmap(page);
759 bytes -= size;
760 buffer_offset += size;
761 }
762}
763
764
Todd Kjos0c972a02017-06-29 12:01:41 -0700765/**
766 * binder_alloc_free_buf() - free a binder buffer
767 * @alloc: binder_alloc for this proc
768 * @buffer: kernel pointer to buffer
769 *
YangHui4b463822020-08-18 09:34:04 +0800770 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700771 */
772void binder_alloc_free_buf(struct binder_alloc *alloc,
773 struct binder_buffer *buffer)
774{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800775 /*
776 * We could eliminate the call to binder_alloc_clear_buf()
777 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000778 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800779 * increase contention for the alloc mutex if clear_on_free
780 * is used frequently for large buffers. The mutex is not
781 * needed for correctness here.
782 */
783 if (buffer->clear_on_free) {
784 binder_alloc_clear_buf(alloc, buffer);
785 buffer->clear_on_free = false;
786 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700787 mutex_lock(&alloc->mutex);
788 binder_free_buf_locked(alloc, buffer);
789 mutex_unlock(&alloc->mutex);
790}
791
792/**
793 * binder_alloc_mmap_handler() - map virtual address space for proc
794 * @alloc: alloc structure for this proc
795 * @vma: vma passed to mmap()
796 *
797 * Called by binder_mmap() to initialize the space specified in
798 * vma for allocating binder buffers
799 *
800 * Return:
801 * 0 = success
802 * -EBUSY = address space already mapped
803 * -ENOMEM = failed to map memory to given address space
804 */
805int binder_alloc_mmap_handler(struct binder_alloc *alloc,
806 struct vm_area_struct *vma)
807{
808 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700809 const char *failure_string;
810 struct binder_buffer *buffer;
811
Carlos Llamasd276fb42022-11-04 23:12:35 +0000812 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
813 ret = -EINVAL;
814 failure_string = "invalid vma->vm_mm";
815 goto err_invalid_mm;
816 }
817
Todd Kjos0c972a02017-06-29 12:01:41 -0700818 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200819 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700820 ret = -EBUSY;
821 failure_string = "already mapped";
822 goto err_already_mapped;
823 }
Jann Horn45d02f72019-10-16 17:01:18 +0200824 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
825 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200826 mutex_unlock(&binder_alloc_mmap_lock);
827
Carlos Llamasc38a8982023-12-01 17:21:38 +0000828 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200829
Jann Horn45d02f72019-10-16 17:01:18 +0200830 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700831 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700832 GFP_KERNEL);
833 if (alloc->pages == NULL) {
834 ret = -ENOMEM;
835 failure_string = "alloc page array";
836 goto err_alloc_pages_failed;
837 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700838
Sherry Yang74310e02017-08-23 08:46:41 -0700839 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
840 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700841 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700842 failure_string = "alloc buffer struct";
843 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700844 }
Sherry Yang74310e02017-08-23 08:46:41 -0700845
Todd Kjosbde4a192019-02-08 10:35:20 -0800846 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700847 list_add(&buffer->entry, &alloc->buffers);
848 buffer->free = 1;
849 binder_insert_free_buffer(alloc, buffer);
850 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000851
852 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900853 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700854
855 return 0;
856
Sherry Yang74310e02017-08-23 08:46:41 -0700857err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700858 kfree(alloc->pages);
859 alloc->pages = NULL;
860err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000861 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200862 mutex_lock(&binder_alloc_mmap_lock);
863 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700864err_already_mapped:
865 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000866err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700867 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
868 "%s: %d %lx-%lx %s failed %d\n", __func__,
869 alloc->pid, vma->vm_start, vma->vm_end,
870 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700871 return ret;
872}
873
874
875void binder_alloc_deferred_release(struct binder_alloc *alloc)
876{
877 struct rb_node *n;
878 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700879 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700880
Todd Kjos0c972a02017-06-29 12:01:41 -0700881 buffers = 0;
882 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000883 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900884
Todd Kjos0c972a02017-06-29 12:01:41 -0700885 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700886 buffer = rb_entry(n, struct binder_buffer, rb_node);
887
888 /* Transaction should already have been freed */
889 BUG_ON(buffer->transaction);
890
Todd Kjos0f966cb2020-11-20 15:37:43 -0800891 if (buffer->clear_on_free) {
892 binder_alloc_clear_buf(alloc, buffer);
893 buffer->clear_on_free = false;
894 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700895 binder_free_buf_locked(alloc, buffer);
896 buffers++;
897 }
898
Sherry Yang74310e02017-08-23 08:46:41 -0700899 while (!list_empty(&alloc->buffers)) {
900 buffer = list_first_entry(&alloc->buffers,
901 struct binder_buffer, entry);
902 WARN_ON(!buffer->free);
903
904 list_del(&buffer->entry);
905 WARN_ON_ONCE(!list_empty(&alloc->buffers));
906 kfree(buffer);
907 }
908
Todd Kjos0c972a02017-06-29 12:01:41 -0700909 page_count = 0;
910 if (alloc->pages) {
911 int i;
912
913 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000914 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700915 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700916
Sherry Yangf2517eb2017-08-23 08:46:42 -0700917 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700918 continue;
919
Sherry Yangf2517eb2017-08-23 08:46:42 -0700920 on_lru = list_lru_del(&binder_alloc_lru,
921 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700922 page_addr = alloc->buffer + i * PAGE_SIZE;
923 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000924 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700925 __func__, alloc->pid, i, page_addr,
926 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700927 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700928 page_count++;
929 }
930 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700931 }
932 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400933 if (alloc->vma_vm_mm)
934 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700935
936 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
937 "%s: %d buffers %d, pages %d\n",
938 __func__, alloc->pid, buffers, page_count);
939}
940
941static void print_binder_buffer(struct seq_file *m, const char *prefix,
942 struct binder_buffer *buffer)
943{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000944 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800945 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700946 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700947 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700948 buffer->transaction ? "active" : "delivered");
949}
950
951/**
952 * binder_alloc_print_allocated() - print buffer info
953 * @m: seq_file for output via seq_printf()
954 * @alloc: binder_alloc for this proc
955 *
956 * Prints information about every buffer associated with
957 * the binder_alloc state to the given seq_file
958 */
959void binder_alloc_print_allocated(struct seq_file *m,
960 struct binder_alloc *alloc)
961{
962 struct rb_node *n;
963
964 mutex_lock(&alloc->mutex);
965 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
966 print_binder_buffer(m, " buffer",
967 rb_entry(n, struct binder_buffer, rb_node));
968 mutex_unlock(&alloc->mutex);
969}
970
971/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700972 * binder_alloc_print_pages() - print page usage
973 * @m: seq_file for output via seq_printf()
974 * @alloc: binder_alloc for this proc
975 */
976void binder_alloc_print_pages(struct seq_file *m,
977 struct binder_alloc *alloc)
978{
979 struct binder_lru_page *page;
980 int i;
981 int active = 0;
982 int lru = 0;
983 int free = 0;
984
985 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200986 /*
987 * Make sure the binder_alloc is fully initialized, otherwise we might
988 * read inconsistent state.
989 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000990 if (binder_alloc_get_vma(alloc) != NULL) {
991 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
992 page = &alloc->pages[i];
993 if (!page->page_ptr)
994 free++;
995 else if (list_empty(&page->lru))
996 active++;
997 else
998 lru++;
999 }
Sherry Yang8ef46652017-08-31 11:56:36 -07001000 }
1001 mutex_unlock(&alloc->mutex);
1002 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001003 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001004}
1005
1006/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001007 * binder_alloc_get_allocated_count() - return count of buffers
1008 * @alloc: binder_alloc for this proc
1009 *
1010 * Return: count of allocated buffers
1011 */
1012int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1013{
1014 struct rb_node *n;
1015 int count = 0;
1016
1017 mutex_lock(&alloc->mutex);
1018 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1019 count++;
1020 mutex_unlock(&alloc->mutex);
1021 return count;
1022}
1023
1024
1025/**
1026 * binder_alloc_vma_close() - invalidate address space
1027 * @alloc: binder_alloc for this proc
1028 *
1029 * Called from binder_vma_close() when releasing address space.
1030 * Clears alloc->vma to prevent new incoming transactions from
1031 * allocating more buffers.
1032 */
1033void binder_alloc_vma_close(struct binder_alloc *alloc)
1034{
Minchan Kimda1b9562018-08-23 14:29:56 +09001035 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001036}
1037
1038/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001039 * binder_alloc_free_page() - shrinker callback to free pages
1040 * @item: item to free
1041 * @lock: lock protecting the item
1042 * @cb_arg: callback argument
1043 *
1044 * Called from list_lru_walk() in binder_shrink_scan() to free
1045 * up pages when the system is under memory pressure.
1046 */
1047enum lru_status binder_alloc_free_page(struct list_head *item,
1048 struct list_lru_one *lru,
1049 spinlock_t *lock,
1050 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001051 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001052{
1053 struct mm_struct *mm = NULL;
1054 struct binder_lru_page *page = container_of(item,
1055 struct binder_lru_page,
1056 lru);
1057 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001058 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001059 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -07001060 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001061
1062 alloc = page->alloc;
1063 if (!mutex_trylock(&alloc->mutex))
1064 goto err_get_alloc_mutex_failed;
1065
1066 if (!page->page_ptr)
1067 goto err_page_already_freed;
1068
1069 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001070 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001071
1072 mm = alloc->vma_vm_mm;
1073 if (!mmget_not_zero(mm))
1074 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001075 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001076 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001077 vma = vma_lookup(mm, page_addr);
1078 if (vma && vma != binder_alloc_get_vma(alloc))
1079 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001080
Sherry Yanga1b22892017-10-03 16:15:00 -07001081 list_lru_isolate(lru, item);
1082 spin_unlock(lock);
1083
1084 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001085 trace_binder_unmap_user_start(alloc, index);
1086
Todd Kjosc41358a2019-02-08 10:35:19 -08001087 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001088
Sherry Yange41e1642017-08-23 08:46:43 -07001089 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001090 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001091 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001092 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001093
Sherry Yange41e1642017-08-23 08:46:43 -07001094 trace_binder_unmap_kernel_start(alloc, index);
1095
Sherry Yangf2517eb2017-08-23 08:46:42 -07001096 __free_page(page->page_ptr);
1097 page->page_ptr = NULL;
1098
Sherry Yange41e1642017-08-23 08:46:43 -07001099 trace_binder_unmap_kernel_end(alloc, index);
1100
Sherry Yanga1b22892017-10-03 16:15:00 -07001101 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001102 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001103 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001104
Carlos Llamas8dce2882023-12-01 17:21:31 +00001105err_invalid_vma:
1106 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001107err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001108 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001109err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001110err_page_already_freed:
1111 mutex_unlock(&alloc->mutex);
1112err_get_alloc_mutex_failed:
1113 return LRU_SKIP;
1114}
1115
1116static unsigned long
1117binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1118{
1119 unsigned long ret = list_lru_count(&binder_alloc_lru);
1120 return ret;
1121}
1122
1123static unsigned long
1124binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1125{
1126 unsigned long ret;
1127
1128 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1129 NULL, sc->nr_to_scan);
1130 return ret;
1131}
1132
Sherry Yangde7bbe32017-10-06 16:12:05 -04001133static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001134 .count_objects = binder_shrink_count,
1135 .scan_objects = binder_shrink_scan,
1136 .seeks = DEFAULT_SEEKS,
1137};
1138
1139/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001140 * binder_alloc_init() - called by binder_open() for per-proc initialization
1141 * @alloc: binder_alloc for this proc
1142 *
1143 * Called from binder_open() to initialize binder_alloc fields for
1144 * new binder proc
1145 */
1146void binder_alloc_init(struct binder_alloc *alloc)
1147{
Todd Kjos0c972a02017-06-29 12:01:41 -07001148 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001149 alloc->vma_vm_mm = current->mm;
1150 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001151 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001152 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001153}
1154
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001155int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001156{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001157 int ret = list_lru_init(&binder_alloc_lru);
1158
1159 if (ret == 0) {
1160 ret = register_shrinker(&binder_shrinker);
1161 if (ret)
1162 list_lru_destroy(&binder_alloc_lru);
1163 }
1164 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001165}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001166
Qi Zheng03eebad2023-06-25 15:49:37 +00001167void binder_alloc_shrinker_exit(void)
1168{
1169 unregister_shrinker(&binder_shrinker);
1170 list_lru_destroy(&binder_alloc_lru);
1171}
1172
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001173/**
1174 * check_buffer() - verify that buffer/offset is safe to access
1175 * @alloc: binder_alloc for this proc
1176 * @buffer: binder buffer to be accessed
1177 * @offset: offset into @buffer data
1178 * @bytes: bytes to access from offset
1179 *
1180 * Check that the @offset/@bytes are within the size of the given
1181 * @buffer and that the buffer is currently active and not freeable.
1182 * Offsets must also be multiples of sizeof(u32). The kernel is
1183 * allowed to touch the buffer in two cases:
1184 *
1185 * 1) when the buffer is being created:
1186 * (buffer->free == 0 && buffer->allow_user_free == 0)
1187 * 2) when the buffer is being torn down:
1188 * (buffer->free == 0 && buffer->transaction == NULL).
1189 *
1190 * Return: true if the buffer is safe to access
1191 */
1192static inline bool check_buffer(struct binder_alloc *alloc,
1193 struct binder_buffer *buffer,
1194 binder_size_t offset, size_t bytes)
1195{
1196 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1197
1198 return buffer_size >= bytes &&
1199 offset <= buffer_size - bytes &&
1200 IS_ALIGNED(offset, sizeof(u32)) &&
1201 !buffer->free &&
1202 (!buffer->allow_user_free || !buffer->transaction);
1203}
1204
1205/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001206 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1207 * @alloc: binder_alloc for this proc
1208 * @buffer: binder buffer to be accessed
1209 * @buffer_offset: offset into @buffer data
1210 * @from: userspace pointer to source buffer
1211 * @bytes: bytes to copy
1212 *
1213 * Copy bytes from source userspace to target buffer.
1214 *
1215 * Return: bytes remaining to be copied
1216 */
1217unsigned long
1218binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1219 struct binder_buffer *buffer,
1220 binder_size_t buffer_offset,
1221 const void __user *from,
1222 size_t bytes)
1223{
1224 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1225 return bytes;
1226
1227 while (bytes) {
1228 unsigned long size;
1229 unsigned long ret;
1230 struct page *page;
1231 pgoff_t pgoff;
1232 void *kptr;
1233
1234 page = binder_alloc_get_page(alloc, buffer,
1235 buffer_offset, &pgoff);
1236 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1237 kptr = kmap(page) + pgoff;
1238 ret = copy_from_user(kptr, from, size);
1239 kunmap(page);
1240 if (ret)
1241 return bytes - size + ret;
1242 bytes -= size;
1243 from += size;
1244 buffer_offset += size;
1245 }
1246 return 0;
1247}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001248
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001249static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1250 bool to_buffer,
1251 struct binder_buffer *buffer,
1252 binder_size_t buffer_offset,
1253 void *ptr,
1254 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001255{
1256 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001257 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1258 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001259
1260 while (bytes) {
1261 unsigned long size;
1262 struct page *page;
1263 pgoff_t pgoff;
1264 void *tmpptr;
1265 void *base_ptr;
1266
1267 page = binder_alloc_get_page(alloc, buffer,
1268 buffer_offset, &pgoff);
1269 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1270 base_ptr = kmap_atomic(page);
1271 tmpptr = base_ptr + pgoff;
1272 if (to_buffer)
1273 memcpy(tmpptr, ptr, size);
1274 else
1275 memcpy(ptr, tmpptr, size);
1276 /*
1277 * kunmap_atomic() takes care of flushing the cache
1278 * if this device has VIVT cache arch
1279 */
1280 kunmap_atomic(base_ptr);
1281 bytes -= size;
1282 pgoff = 0;
1283 ptr = ptr + size;
1284 buffer_offset += size;
1285 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001286 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001287}
1288
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001289int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1290 struct binder_buffer *buffer,
1291 binder_size_t buffer_offset,
1292 void *src,
1293 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001294{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001295 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1296 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001297}
1298
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001299int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1300 void *dest,
1301 struct binder_buffer *buffer,
1302 binder_size_t buffer_offset,
1303 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001304{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001305 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1306 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001307}
1308