blob: c0f081f6e34e3ab4cb680dacdc4d096930849564 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Todd Kjos0c972a02017-06-29 12:01:41 -07002/* binder_alloc.c
3 *
4 * Android IPC Subsystem
5 *
6 * Copyright (C) 2007-2017 Google, Inc.
Todd Kjos0c972a02017-06-29 12:01:41 -07007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Todd Kjos0c972a02017-06-29 12:01:41 -070011#include <linux/list.h>
12#include <linux/sched/mm.h>
13#include <linux/module.h>
14#include <linux/rtmutex.h>
15#include <linux/rbtree.h>
16#include <linux/seq_file.h>
17#include <linux/vmalloc.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
Sherry Yangf2517eb2017-08-23 08:46:42 -070020#include <linux/list_lru.h>
Sherry Yang128f3802018-08-07 12:57:13 -070021#include <linux/ratelimit.h>
Guenter Roeck1e81c572018-07-23 14:47:23 -070022#include <asm/cacheflush.h>
Todd Kjos1a7c3d92019-02-08 10:35:14 -080023#include <linux/uaccess.h>
24#include <linux/highmem.h>
Jann Horn45d02f72019-10-16 17:01:18 +020025#include <linux/sizes.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070026#include "binder_alloc.h"
27#include "binder_trace.h"
Zhuguangqing1174e452021-03-09 15:47:43 +080028#include <trace/hooks/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070029
Sherry Yangf2517eb2017-08-23 08:46:42 -070030struct list_lru binder_alloc_lru;
31
Todd Kjos0c972a02017-06-29 12:01:41 -070032static DEFINE_MUTEX(binder_alloc_mmap_lock);
33
34enum {
Sherry Yang128f3802018-08-07 12:57:13 -070035 BINDER_DEBUG_USER_ERROR = 1U << 0,
Todd Kjos0c972a02017-06-29 12:01:41 -070036 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
37 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
38 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
39};
Sherry Yang128f3802018-08-07 12:57:13 -070040static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
Todd Kjos0c972a02017-06-29 12:01:41 -070041
42module_param_named(debug_mask, binder_alloc_debug_mask,
43 uint, 0644);
44
45#define binder_alloc_debug(mask, x...) \
46 do { \
47 if (binder_alloc_debug_mask & mask) \
Sherry Yang128f3802018-08-07 12:57:13 -070048 pr_info_ratelimited(x); \
Todd Kjos0c972a02017-06-29 12:01:41 -070049 } while (0)
50
Sherry Yange21762192017-08-23 08:46:39 -070051static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
52{
53 return list_entry(buffer->entry.next, struct binder_buffer, entry);
54}
55
56static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
57{
58 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
59}
60
Todd Kjos0c972a02017-06-29 12:01:41 -070061static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
62 struct binder_buffer *buffer)
63{
64 if (list_is_last(&buffer->entry, &alloc->buffers))
Todd Kjosbde4a192019-02-08 10:35:20 -080065 return alloc->buffer + alloc->buffer_size - buffer->user_data;
66 return binder_buffer_next(buffer)->user_data - buffer->user_data;
Todd Kjos0c972a02017-06-29 12:01:41 -070067}
68
69static void binder_insert_free_buffer(struct binder_alloc *alloc,
70 struct binder_buffer *new_buffer)
71{
72 struct rb_node **p = &alloc->free_buffers.rb_node;
73 struct rb_node *parent = NULL;
74 struct binder_buffer *buffer;
75 size_t buffer_size;
76 size_t new_buffer_size;
77
78 BUG_ON(!new_buffer->free);
79
80 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
81
82 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
83 "%d: add free buffer, size %zd, at %pK\n",
84 alloc->pid, new_buffer_size, new_buffer);
85
86 while (*p) {
87 parent = *p;
88 buffer = rb_entry(parent, struct binder_buffer, rb_node);
89 BUG_ON(!buffer->free);
90
91 buffer_size = binder_alloc_buffer_size(alloc, buffer);
92
93 if (new_buffer_size < buffer_size)
94 p = &parent->rb_left;
95 else
96 p = &parent->rb_right;
97 }
98 rb_link_node(&new_buffer->rb_node, parent, p);
99 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
100}
101
102static void binder_insert_allocated_buffer_locked(
103 struct binder_alloc *alloc, struct binder_buffer *new_buffer)
104{
105 struct rb_node **p = &alloc->allocated_buffers.rb_node;
106 struct rb_node *parent = NULL;
107 struct binder_buffer *buffer;
108
109 BUG_ON(new_buffer->free);
110
111 while (*p) {
112 parent = *p;
113 buffer = rb_entry(parent, struct binder_buffer, rb_node);
114 BUG_ON(buffer->free);
115
Todd Kjosbde4a192019-02-08 10:35:20 -0800116 if (new_buffer->user_data < buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700117 p = &parent->rb_left;
Todd Kjosbde4a192019-02-08 10:35:20 -0800118 else if (new_buffer->user_data > buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700119 p = &parent->rb_right;
120 else
121 BUG();
122 }
123 rb_link_node(&new_buffer->rb_node, parent, p);
124 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
125}
126
Todd Kjos53d311cf2017-06-29 12:01:51 -0700127static struct binder_buffer *binder_alloc_prepare_to_free_locked(
Todd Kjos0c972a02017-06-29 12:01:41 -0700128 struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000129 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700130{
131 struct rb_node *n = alloc->allocated_buffers.rb_node;
132 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700133
134 while (n) {
135 buffer = rb_entry(n, struct binder_buffer, rb_node);
136 BUG_ON(buffer->free);
137
Carlos Llamasc38a8982023-12-01 17:21:38 +0000138 if (user_ptr < buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700139 n = n->rb_left;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000140 } else if (user_ptr > buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700141 n = n->rb_right;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000142 } else {
Todd Kjos53d311cf2017-06-29 12:01:51 -0700143 /*
144 * Guard against user threads attempting to
Todd Kjos7bada552018-11-06 15:55:32 -0800145 * free the buffer when in use by kernel or
146 * after it's already been freed.
Todd Kjos53d311cf2017-06-29 12:01:51 -0700147 */
Todd Kjos7bada552018-11-06 15:55:32 -0800148 if (!buffer->allow_user_free)
149 return ERR_PTR(-EPERM);
150 buffer->allow_user_free = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700151 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700152 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700153 }
154 return NULL;
155}
156
157/**
Joel Fernandes (Google)5dc54a02019-09-30 16:12:50 -0400158 * binder_alloc_prepare_to_free() - get buffer given user ptr
Todd Kjos0c972a02017-06-29 12:01:41 -0700159 * @alloc: binder_alloc for this proc
160 * @user_ptr: User pointer to buffer data
161 *
162 * Validate userspace pointer to buffer data and return buffer corresponding to
163 * that user pointer. Search the rb tree for buffer that matches user data
164 * pointer.
165 *
166 * Return: Pointer to buffer or NULL
167 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700168struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000169 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700170{
171 struct binder_buffer *buffer;
172
173 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700174 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700175 mutex_unlock(&alloc->mutex);
176 return buffer;
177}
178
Carlos Llamas0b243682023-12-01 17:21:39 +0000179static void binder_free_page_range(struct binder_alloc *alloc,
180 unsigned long start, unsigned long end)
181{
182 struct binder_lru_page *page;
183 unsigned long page_addr;
184
185 trace_binder_update_page_range(alloc, false, start, end);
186
187 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
188 size_t index;
189 int ret;
190
191 index = (page_addr - alloc->buffer) / PAGE_SIZE;
192 page = &alloc->pages[index];
193
194 trace_binder_free_lru_start(alloc, index);
195
196 ret = list_lru_add(&binder_alloc_lru, &page->lru);
197 WARN_ON(!ret);
198
199 trace_binder_free_lru_end(alloc, index);
200 }
201}
202
203static int binder_allocate_page_range(struct binder_alloc *alloc,
204 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700205{
Sherry Yang6ae33b92017-09-16 01:11:56 -0400206 struct vm_area_struct *vma = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000207 struct binder_lru_page *page;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700208 struct mm_struct *mm = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000209 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700210 bool need_mm = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700211
212 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamas0b243682023-12-01 17:21:39 +0000213 "%d: allocate pages %lx-%lx\n",
214 alloc->pid, start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700215
216 if (end <= start)
217 return 0;
218
Carlos Llamas0b243682023-12-01 17:21:39 +0000219 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700220
221 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
222 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
223 if (!page->page_ptr) {
224 need_mm = true;
225 break;
226 }
227 }
228
Greg Kroah-Hartman6fbf2482017-10-23 17:21:44 +0200229 if (need_mm && mmget_not_zero(alloc->vma_vm_mm))
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400230 mm = alloc->vma_vm_mm;
Todd Kjos0c972a02017-06-29 12:01:41 -0700231
232 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000233 mmap_write_lock(mm);
Carlos Llamasacd81932023-05-30 19:43:36 +0000234 vma = alloc->vma;
Todd Kjos0c972a02017-06-29 12:01:41 -0700235 }
236
Sherry Yangf2517eb2017-08-23 08:46:42 -0700237 if (!vma && need_mm) {
Sherry Yang128f3802018-08-07 12:57:13 -0700238 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
239 "%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
240 alloc->pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700241 goto err_no_vma;
242 }
243
244 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
245 int ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700246 bool on_lru;
Sherry Yange41e1642017-08-23 08:46:43 -0700247 size_t index;
Todd Kjos0c972a02017-06-29 12:01:41 -0700248
Sherry Yange41e1642017-08-23 08:46:43 -0700249 index = (page_addr - alloc->buffer) / PAGE_SIZE;
250 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700251
Sherry Yangf2517eb2017-08-23 08:46:42 -0700252 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700253 trace_binder_alloc_lru_start(alloc, index);
254
Sherry Yangf2517eb2017-08-23 08:46:42 -0700255 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
256 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700257
258 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700259 continue;
260 }
261
262 if (WARN_ON(!vma))
263 goto err_page_ptr_cleared;
264
Sherry Yange41e1642017-08-23 08:46:43 -0700265 trace_binder_alloc_page_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700266 page->page_ptr = alloc_page(GFP_KERNEL |
267 __GFP_HIGHMEM |
268 __GFP_ZERO);
269 if (!page->page_ptr) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000270 pr_err("%d: binder_alloc_buf failed for page at %lx\n",
271 alloc->pid, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700272 goto err_alloc_page_failed;
273 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700274 page->alloc = alloc;
275 INIT_LIST_HEAD(&page->lru);
276
Carlos Llamasc38a8982023-12-01 17:21:38 +0000277 ret = vm_insert_page(vma, page_addr, page->page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700278 if (ret) {
279 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Carlos Llamasc38a8982023-12-01 17:21:38 +0000280 alloc->pid, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700281 goto err_vm_insert_page_failed;
282 }
Sherry Yange41e1642017-08-23 08:46:43 -0700283
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100284 if (index + 1 > alloc->pages_high)
285 alloc->pages_high = index + 1;
286
Sherry Yange41e1642017-08-23 08:46:43 -0700287 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700288 }
289 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000290 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000291 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700292 }
293 return 0;
294
Todd Kjos0c972a02017-06-29 12:01:41 -0700295err_vm_insert_page_failed:
Carlos Llamas0b243682023-12-01 17:21:39 +0000296 __free_page(page->page_ptr);
297 page->page_ptr = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700298err_alloc_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700299err_page_ptr_cleared:
Carlos Llamas0b243682023-12-01 17:21:39 +0000300 binder_free_page_range(alloc, start, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700301err_no_vma:
302 if (mm) {
Carlos Llamas0270aeeb2023-05-30 19:43:38 +0000303 mmap_write_unlock(mm);
Carlos Llamas1787ddd2023-12-01 17:21:32 +0000304 mmput_async(mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700305 }
Todd Kjos57ada2f2017-06-29 12:01:46 -0700306 return vma ? -ENOMEM : -ESRCH;
Todd Kjos0c972a02017-06-29 12:01:41 -0700307}
308
Minchan Kimda1b9562018-08-23 14:29:56 +0900309static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
310 struct vm_area_struct *vma)
311{
Carlos Llamasb094b042023-05-30 19:43:37 +0000312 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
313 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900314}
315
316static inline struct vm_area_struct *binder_alloc_get_vma(
317 struct binder_alloc *alloc)
318{
Carlos Llamasb094b042023-05-30 19:43:37 +0000319 /* pairs with smp_store_release in binder_alloc_set_vma() */
320 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900321}
322
Hang Lua7dc1e62021-04-09 17:40:46 +0800323static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid)
Martijn Coenen261e7812020-08-21 14:25:44 +0200324{
325 /*
326 * Find the amount and size of buffers allocated by the current caller;
327 * The idea is that once we cross the threshold, whoever is responsible
328 * for the low async space is likely to try to send another async txn,
329 * and at some point we'll catch them in the act. This is more efficient
330 * than keeping a map per pid.
331 */
Colin Ian King7369fa42020-09-10 16:12:21 +0100332 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200333 struct binder_buffer *buffer;
334 size_t total_alloc_size = 0;
335 size_t num_buffers = 0;
336
337 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
338 n = rb_next(n)) {
339 buffer = rb_entry(n, struct binder_buffer, rb_node);
340 if (buffer->pid != pid)
341 continue;
342 if (!buffer->async_transaction)
343 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000344 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200345 num_buffers++;
346 }
347
348 /*
349 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800350 * async space (which is 25% of total buffer size). Oneway spam is only
351 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200352 */
353 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
354 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
355 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
356 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800357 if (!alloc->oneway_spam_detected) {
358 alloc->oneway_spam_detected = true;
359 return true;
360 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200361 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800362 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200363}
364
Xiongwei Song3f827242017-12-14 12:15:42 +0800365static struct binder_buffer *binder_alloc_new_buf_locked(
366 struct binder_alloc *alloc,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000367 size_t size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200368 int is_async,
369 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700370{
371 struct rb_node *n = alloc->free_buffers.rb_node;
372 struct binder_buffer *buffer;
373 size_t buffer_size;
374 struct rb_node *best_fit = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000375 unsigned long has_page_addr;
376 unsigned long end_page_addr;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700377 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700378
Zhuguangqing1174e452021-03-09 15:47:43 +0800379 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000380
Carlos Llamas11ca0762023-12-01 17:21:34 +0000381 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700382 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
383 "%d: binder_alloc_buf size %zd failed, no async space left\n",
384 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700385 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700386 }
387
388 while (n) {
389 buffer = rb_entry(n, struct binder_buffer, rb_node);
390 BUG_ON(!buffer->free);
391 buffer_size = binder_alloc_buffer_size(alloc, buffer);
392
393 if (size < buffer_size) {
394 best_fit = n;
395 n = n->rb_left;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000396 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700397 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000398 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700399 best_fit = n;
400 break;
401 }
402 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000403
Todd Kjos0c972a02017-06-29 12:01:41 -0700404 if (best_fit == NULL) {
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700405 size_t allocated_buffers = 0;
406 size_t largest_alloc_size = 0;
407 size_t total_alloc_size = 0;
408 size_t free_buffers = 0;
409 size_t largest_free_size = 0;
410 size_t total_free_size = 0;
411
412 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
413 n = rb_next(n)) {
414 buffer = rb_entry(n, struct binder_buffer, rb_node);
415 buffer_size = binder_alloc_buffer_size(alloc, buffer);
416 allocated_buffers++;
417 total_alloc_size += buffer_size;
418 if (buffer_size > largest_alloc_size)
419 largest_alloc_size = buffer_size;
420 }
421 for (n = rb_first(&alloc->free_buffers); n != NULL;
422 n = rb_next(n)) {
423 buffer = rb_entry(n, struct binder_buffer, rb_node);
424 buffer_size = binder_alloc_buffer_size(alloc, buffer);
425 free_buffers++;
426 total_free_size += buffer_size;
427 if (buffer_size > largest_free_size)
428 largest_free_size = buffer_size;
429 }
Sherry Yang128f3802018-08-07 12:57:13 -0700430 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
431 "%d: binder_alloc_buf size %zd failed, no address space\n",
432 alloc->pid, size);
433 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
434 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
435 total_alloc_size, allocated_buffers,
436 largest_alloc_size, total_free_size,
437 free_buffers, largest_free_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700438 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700439 }
440 if (n == NULL) {
441 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
442 buffer_size = binder_alloc_buffer_size(alloc, buffer);
443 }
444
445 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
446 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
447 alloc->pid, size, buffer, buffer_size);
448
Carlos Llamasc38a8982023-12-01 17:21:38 +0000449 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
Sherry Yang74310e02017-08-23 08:46:41 -0700450 WARN_ON(n && buffer_size != size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000451 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700452 if (end_page_addr > has_page_addr)
453 end_page_addr = has_page_addr;
Carlos Llamas0b243682023-12-01 17:21:39 +0000454 ret = binder_allocate_page_range(alloc, PAGE_ALIGN(buffer->user_data),
455 end_page_addr);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700456 if (ret)
457 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700458
Todd Kjos0c972a02017-06-29 12:01:41 -0700459 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700460 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700461
Sherry Yang74310e02017-08-23 08:46:41 -0700462 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
463 if (!new_buffer) {
464 pr_err("%s: %d failed to alloc new buffer struct\n",
465 __func__, alloc->pid);
466 goto err_alloc_buf_struct_failed;
467 }
Carlos Llamasc38a8982023-12-01 17:21:38 +0000468 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700469 list_add(&new_buffer->entry, &buffer->entry);
470 new_buffer->free = 1;
471 binder_insert_free_buffer(alloc, new_buffer);
472 }
Sherry Yang74310e02017-08-23 08:46:41 -0700473
474 rb_erase(best_fit, &alloc->free_buffers);
475 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800476 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700477 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700478 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
479 "%d: binder_alloc_buf size %zd got %pK\n",
480 alloc->pid, size, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700481 buffer->async_transaction = is_async;
Martijn Coenen261e7812020-08-21 14:25:44 +0200482 buffer->pid = pid;
Hang Lua7dc1e62021-04-09 17:40:46 +0800483 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700484 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000485 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700486 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
487 "%d: binder_alloc_buf size %zd async free %zd\n",
488 alloc->pid, size, alloc->free_async_space);
Martijn Coenen261e7812020-08-21 14:25:44 +0200489 if (alloc->free_async_space < alloc->buffer_size / 10) {
490 /*
491 * Start detecting spammers once we have less than 20%
492 * of async space left (which is less than 10% of total
493 * buffer size).
494 */
Hang Lua7dc1e62021-04-09 17:40:46 +0800495 buffer->oneway_spam_suspect = debug_low_async_space_locked(alloc, pid);
496 } else {
497 alloc->oneway_spam_detected = false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200498 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700499 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000500
Todd Kjos0c972a02017-06-29 12:01:41 -0700501 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700502
503err_alloc_buf_struct_failed:
Carlos Llamas0b243682023-12-01 17:21:39 +0000504 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
505 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700506 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700507}
508
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000509/* Calculate the sanitized total size, returns 0 for invalid request */
510static inline size_t sanitized_size(size_t data_size,
511 size_t offsets_size,
512 size_t extra_buffers_size)
513{
514 size_t total, tmp;
515
516 /* Align to pointer size and check for overflows */
517 tmp = ALIGN(data_size, sizeof(void *)) +
518 ALIGN(offsets_size, sizeof(void *));
519 if (tmp < data_size || tmp < offsets_size)
520 return 0;
521 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
522 if (total < tmp || total < extra_buffers_size)
523 return 0;
524
525 /* Pad 0-sized buffers so they get a unique address */
526 total = max(total, sizeof(void *));
527
528 return total;
529}
530
Todd Kjos0c972a02017-06-29 12:01:41 -0700531/**
532 * binder_alloc_new_buf() - Allocate a new binder buffer
533 * @alloc: binder_alloc for this proc
534 * @data_size: size of user data buffer
535 * @offsets_size: user specified buffer offset
536 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
537 * @is_async: buffer for async transaction
Martijn Coenen261e7812020-08-21 14:25:44 +0200538 * @pid: pid to attribute allocation to (used for debugging)
Todd Kjos0c972a02017-06-29 12:01:41 -0700539 *
540 * Allocate a new buffer given the requested sizes. Returns
541 * the kernel version of the buffer pointer. The size allocated
542 * is the sum of the three given sizes (each rounded up to
543 * pointer-sized boundary)
544 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000545 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700546 */
547struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
548 size_t data_size,
549 size_t offsets_size,
550 size_t extra_buffers_size,
Martijn Coenen261e7812020-08-21 14:25:44 +0200551 int is_async,
552 int pid)
Todd Kjos0c972a02017-06-29 12:01:41 -0700553{
554 struct binder_buffer *buffer;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000555 size_t size;
556
557 /* Check binder_alloc is fully initialized */
558 if (!binder_alloc_get_vma(alloc)) {
559 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
560 "%d: binder_alloc_buf, no vma\n",
561 alloc->pid);
562 return ERR_PTR(-ESRCH);
563 }
564
565 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
566 if (unlikely(!size)) {
567 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
568 "%d: got transaction with invalid size %zd-%zd-%zd\n",
569 alloc->pid, data_size, offsets_size,
570 extra_buffers_size);
571 return ERR_PTR(-EINVAL);
572 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700573
574 mutex_lock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000575 buffer = binder_alloc_new_buf_locked(alloc, size, is_async, pid);
576 if (IS_ERR(buffer)) {
577 mutex_unlock(&alloc->mutex);
578 goto out;
579 }
580
581 buffer->data_size = data_size;
582 buffer->offsets_size = offsets_size;
583 buffer->extra_buffers_size = extra_buffers_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700584 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000585
586out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700587 return buffer;
588}
589
Carlos Llamasc38a8982023-12-01 17:21:38 +0000590static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700591{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000592 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700593}
594
Carlos Llamasc38a8982023-12-01 17:21:38 +0000595static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700596{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000597 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700598}
599
600static void binder_delete_free_buffer(struct binder_alloc *alloc,
601 struct binder_buffer *buffer)
602{
603 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700604 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530605
Todd Kjos0c972a02017-06-29 12:01:41 -0700606 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700607 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700608 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700609 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
610 to_free = false;
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 %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800613 alloc->pid, buffer->user_data,
614 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700615 }
616
617 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700618 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700619 if (buffer_start_page(next) == buffer_start_page(buffer)) {
620 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700621 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000622 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700623 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800624 buffer->user_data,
625 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700626 }
627 }
Sherry Yang74310e02017-08-23 08:46:41 -0700628
Todd Kjosbde4a192019-02-08 10:35:20 -0800629 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700630 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000631 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800632 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700633 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700634 }
Sherry Yang74310e02017-08-23 08:46:41 -0700635
636 if (to_free) {
637 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000638 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800639 alloc->pid, buffer->user_data,
640 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000641 next ? next->user_data : 0);
Carlos Llamas0b243682023-12-01 17:21:39 +0000642 binder_free_page_range(alloc, buffer_start_page(buffer),
643 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700644 }
645 list_del(&buffer->entry);
646 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700647}
648
649static void binder_free_buf_locked(struct binder_alloc *alloc,
650 struct binder_buffer *buffer)
651{
652 size_t size, buffer_size;
653
654 buffer_size = binder_alloc_buffer_size(alloc, buffer);
655
656 size = ALIGN(buffer->data_size, sizeof(void *)) +
657 ALIGN(buffer->offsets_size, sizeof(void *)) +
658 ALIGN(buffer->extra_buffers_size, sizeof(void *));
659
660 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
661 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
662 alloc->pid, buffer, size, buffer_size);
663
664 BUG_ON(buffer->free);
665 BUG_ON(size > buffer_size);
666 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800667 BUG_ON(buffer->user_data < alloc->buffer);
668 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700669
670 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000671 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700672 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
673 "%d: binder_free_buf size %zd async free %zd\n",
674 alloc->pid, size, alloc->free_async_space);
675 }
676
Carlos Llamas0b243682023-12-01 17:21:39 +0000677 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
678 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700679
680 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
681 buffer->free = 1;
682 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700683 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700684
685 if (next->free) {
686 rb_erase(&next->rb_node, &alloc->free_buffers);
687 binder_delete_free_buffer(alloc, next);
688 }
689 }
690 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700691 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700692
693 if (prev->free) {
694 binder_delete_free_buffer(alloc, buffer);
695 rb_erase(&prev->rb_node, &alloc->free_buffers);
696 buffer = prev;
697 }
698 }
699 binder_insert_free_buffer(alloc, buffer);
700}
701
Todd Kjos0f966cb2020-11-20 15:37:43 -0800702static void binder_alloc_clear_buf(struct binder_alloc *alloc,
703 struct binder_buffer *buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700704/**
705 * binder_alloc_free_buf() - free a binder buffer
706 * @alloc: binder_alloc for this proc
707 * @buffer: kernel pointer to buffer
708 *
YangHui4b463822020-08-18 09:34:04 +0800709 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700710 */
711void binder_alloc_free_buf(struct binder_alloc *alloc,
712 struct binder_buffer *buffer)
713{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800714 /*
715 * We could eliminate the call to binder_alloc_clear_buf()
716 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000717 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800718 * increase contention for the alloc mutex if clear_on_free
719 * is used frequently for large buffers. The mutex is not
720 * needed for correctness here.
721 */
722 if (buffer->clear_on_free) {
723 binder_alloc_clear_buf(alloc, buffer);
724 buffer->clear_on_free = false;
725 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700726 mutex_lock(&alloc->mutex);
727 binder_free_buf_locked(alloc, buffer);
728 mutex_unlock(&alloc->mutex);
729}
730
731/**
732 * binder_alloc_mmap_handler() - map virtual address space for proc
733 * @alloc: alloc structure for this proc
734 * @vma: vma passed to mmap()
735 *
736 * Called by binder_mmap() to initialize the space specified in
737 * vma for allocating binder buffers
738 *
739 * Return:
740 * 0 = success
741 * -EBUSY = address space already mapped
742 * -ENOMEM = failed to map memory to given address space
743 */
744int binder_alloc_mmap_handler(struct binder_alloc *alloc,
745 struct vm_area_struct *vma)
746{
747 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700748 const char *failure_string;
749 struct binder_buffer *buffer;
750
Carlos Llamasd276fb42022-11-04 23:12:35 +0000751 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
752 ret = -EINVAL;
753 failure_string = "invalid vma->vm_mm";
754 goto err_invalid_mm;
755 }
756
Todd Kjos0c972a02017-06-29 12:01:41 -0700757 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200758 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700759 ret = -EBUSY;
760 failure_string = "already mapped";
761 goto err_already_mapped;
762 }
Jann Horn45d02f72019-10-16 17:01:18 +0200763 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
764 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200765 mutex_unlock(&binder_alloc_mmap_lock);
766
Carlos Llamasc38a8982023-12-01 17:21:38 +0000767 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200768
Jann Horn45d02f72019-10-16 17:01:18 +0200769 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700770 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700771 GFP_KERNEL);
772 if (alloc->pages == NULL) {
773 ret = -ENOMEM;
774 failure_string = "alloc page array";
775 goto err_alloc_pages_failed;
776 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700777
Sherry Yang74310e02017-08-23 08:46:41 -0700778 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
779 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700780 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700781 failure_string = "alloc buffer struct";
782 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700783 }
Sherry Yang74310e02017-08-23 08:46:41 -0700784
Todd Kjosbde4a192019-02-08 10:35:20 -0800785 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700786 list_add(&buffer->entry, &alloc->buffers);
787 buffer->free = 1;
788 binder_insert_free_buffer(alloc, buffer);
789 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000790
791 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900792 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700793
794 return 0;
795
Sherry Yang74310e02017-08-23 08:46:41 -0700796err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700797 kfree(alloc->pages);
798 alloc->pages = NULL;
799err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000800 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200801 mutex_lock(&binder_alloc_mmap_lock);
802 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700803err_already_mapped:
804 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000805err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700806 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
807 "%s: %d %lx-%lx %s failed %d\n", __func__,
808 alloc->pid, vma->vm_start, vma->vm_end,
809 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700810 return ret;
811}
812
813
814void binder_alloc_deferred_release(struct binder_alloc *alloc)
815{
816 struct rb_node *n;
817 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700818 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700819
Todd Kjos0c972a02017-06-29 12:01:41 -0700820 buffers = 0;
821 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000822 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900823
Todd Kjos0c972a02017-06-29 12:01:41 -0700824 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700825 buffer = rb_entry(n, struct binder_buffer, rb_node);
826
827 /* Transaction should already have been freed */
828 BUG_ON(buffer->transaction);
829
Todd Kjos0f966cb2020-11-20 15:37:43 -0800830 if (buffer->clear_on_free) {
831 binder_alloc_clear_buf(alloc, buffer);
832 buffer->clear_on_free = false;
833 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700834 binder_free_buf_locked(alloc, buffer);
835 buffers++;
836 }
837
Sherry Yang74310e02017-08-23 08:46:41 -0700838 while (!list_empty(&alloc->buffers)) {
839 buffer = list_first_entry(&alloc->buffers,
840 struct binder_buffer, entry);
841 WARN_ON(!buffer->free);
842
843 list_del(&buffer->entry);
844 WARN_ON_ONCE(!list_empty(&alloc->buffers));
845 kfree(buffer);
846 }
847
Todd Kjos0c972a02017-06-29 12:01:41 -0700848 page_count = 0;
849 if (alloc->pages) {
850 int i;
851
852 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000853 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700854 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700855
Sherry Yangf2517eb2017-08-23 08:46:42 -0700856 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700857 continue;
858
Sherry Yangf2517eb2017-08-23 08:46:42 -0700859 on_lru = list_lru_del(&binder_alloc_lru,
860 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700861 page_addr = alloc->buffer + i * PAGE_SIZE;
862 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000863 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700864 __func__, alloc->pid, i, page_addr,
865 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700866 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700867 page_count++;
868 }
869 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700870 }
871 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400872 if (alloc->vma_vm_mm)
873 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700874
875 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
876 "%s: %d buffers %d, pages %d\n",
877 __func__, alloc->pid, buffers, page_count);
878}
879
880static void print_binder_buffer(struct seq_file *m, const char *prefix,
881 struct binder_buffer *buffer)
882{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000883 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800884 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700885 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700886 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700887 buffer->transaction ? "active" : "delivered");
888}
889
890/**
891 * binder_alloc_print_allocated() - print buffer info
892 * @m: seq_file for output via seq_printf()
893 * @alloc: binder_alloc for this proc
894 *
895 * Prints information about every buffer associated with
896 * the binder_alloc state to the given seq_file
897 */
898void binder_alloc_print_allocated(struct seq_file *m,
899 struct binder_alloc *alloc)
900{
901 struct rb_node *n;
902
903 mutex_lock(&alloc->mutex);
904 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
905 print_binder_buffer(m, " buffer",
906 rb_entry(n, struct binder_buffer, rb_node));
907 mutex_unlock(&alloc->mutex);
908}
909
910/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700911 * binder_alloc_print_pages() - print page usage
912 * @m: seq_file for output via seq_printf()
913 * @alloc: binder_alloc for this proc
914 */
915void binder_alloc_print_pages(struct seq_file *m,
916 struct binder_alloc *alloc)
917{
918 struct binder_lru_page *page;
919 int i;
920 int active = 0;
921 int lru = 0;
922 int free = 0;
923
924 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200925 /*
926 * Make sure the binder_alloc is fully initialized, otherwise we might
927 * read inconsistent state.
928 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000929 if (binder_alloc_get_vma(alloc) != NULL) {
930 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
931 page = &alloc->pages[i];
932 if (!page->page_ptr)
933 free++;
934 else if (list_empty(&page->lru))
935 active++;
936 else
937 lru++;
938 }
Sherry Yang8ef46652017-08-31 11:56:36 -0700939 }
940 mutex_unlock(&alloc->mutex);
941 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100942 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -0700943}
944
945/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700946 * binder_alloc_get_allocated_count() - return count of buffers
947 * @alloc: binder_alloc for this proc
948 *
949 * Return: count of allocated buffers
950 */
951int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
952{
953 struct rb_node *n;
954 int count = 0;
955
956 mutex_lock(&alloc->mutex);
957 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
958 count++;
959 mutex_unlock(&alloc->mutex);
960 return count;
961}
962
963
964/**
965 * binder_alloc_vma_close() - invalidate address space
966 * @alloc: binder_alloc for this proc
967 *
968 * Called from binder_vma_close() when releasing address space.
969 * Clears alloc->vma to prevent new incoming transactions from
970 * allocating more buffers.
971 */
972void binder_alloc_vma_close(struct binder_alloc *alloc)
973{
Minchan Kimda1b9562018-08-23 14:29:56 +0900974 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700975}
976
977/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700978 * binder_alloc_free_page() - shrinker callback to free pages
979 * @item: item to free
980 * @lock: lock protecting the item
981 * @cb_arg: callback argument
982 *
983 * Called from list_lru_walk() in binder_shrink_scan() to free
984 * up pages when the system is under memory pressure.
985 */
986enum lru_status binder_alloc_free_page(struct list_head *item,
987 struct list_lru_one *lru,
988 spinlock_t *lock,
989 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -0800990 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -0700991{
992 struct mm_struct *mm = NULL;
993 struct binder_lru_page *page = container_of(item,
994 struct binder_lru_page,
995 lru);
996 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000997 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700998 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -0700999 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001000
1001 alloc = page->alloc;
1002 if (!mutex_trylock(&alloc->mutex))
1003 goto err_get_alloc_mutex_failed;
1004
1005 if (!page->page_ptr)
1006 goto err_page_already_freed;
1007
1008 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001009 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001010
1011 mm = alloc->vma_vm_mm;
1012 if (!mmget_not_zero(mm))
1013 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001014 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001015 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001016 vma = vma_lookup(mm, page_addr);
1017 if (vma && vma != binder_alloc_get_vma(alloc))
1018 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001019
Sherry Yanga1b22892017-10-03 16:15:00 -07001020 list_lru_isolate(lru, item);
1021 spin_unlock(lock);
1022
1023 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001024 trace_binder_unmap_user_start(alloc, index);
1025
Todd Kjosc41358a2019-02-08 10:35:19 -08001026 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001027
Sherry Yange41e1642017-08-23 08:46:43 -07001028 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001029 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001030 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001031 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001032
Sherry Yange41e1642017-08-23 08:46:43 -07001033 trace_binder_unmap_kernel_start(alloc, index);
1034
Sherry Yangf2517eb2017-08-23 08:46:42 -07001035 __free_page(page->page_ptr);
1036 page->page_ptr = NULL;
1037
Sherry Yange41e1642017-08-23 08:46:43 -07001038 trace_binder_unmap_kernel_end(alloc, index);
1039
Sherry Yanga1b22892017-10-03 16:15:00 -07001040 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001041 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001042 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001043
Carlos Llamas8dce2882023-12-01 17:21:31 +00001044err_invalid_vma:
1045 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001046err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001047 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001048err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001049err_page_already_freed:
1050 mutex_unlock(&alloc->mutex);
1051err_get_alloc_mutex_failed:
1052 return LRU_SKIP;
1053}
1054
1055static unsigned long
1056binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1057{
1058 unsigned long ret = list_lru_count(&binder_alloc_lru);
1059 return ret;
1060}
1061
1062static unsigned long
1063binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1064{
1065 unsigned long ret;
1066
1067 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1068 NULL, sc->nr_to_scan);
1069 return ret;
1070}
1071
Sherry Yangde7bbe32017-10-06 16:12:05 -04001072static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001073 .count_objects = binder_shrink_count,
1074 .scan_objects = binder_shrink_scan,
1075 .seeks = DEFAULT_SEEKS,
1076};
1077
1078/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001079 * binder_alloc_init() - called by binder_open() for per-proc initialization
1080 * @alloc: binder_alloc for this proc
1081 *
1082 * Called from binder_open() to initialize binder_alloc fields for
1083 * new binder proc
1084 */
1085void binder_alloc_init(struct binder_alloc *alloc)
1086{
Todd Kjos0c972a02017-06-29 12:01:41 -07001087 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001088 alloc->vma_vm_mm = current->mm;
1089 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001090 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001091 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001092}
1093
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001094int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001095{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001096 int ret = list_lru_init(&binder_alloc_lru);
1097
1098 if (ret == 0) {
1099 ret = register_shrinker(&binder_shrinker);
1100 if (ret)
1101 list_lru_destroy(&binder_alloc_lru);
1102 }
1103 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001104}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001105
Qi Zheng03eebad2023-06-25 15:49:37 +00001106void binder_alloc_shrinker_exit(void)
1107{
1108 unregister_shrinker(&binder_shrinker);
1109 list_lru_destroy(&binder_alloc_lru);
1110}
1111
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001112/**
1113 * check_buffer() - verify that buffer/offset is safe to access
1114 * @alloc: binder_alloc for this proc
1115 * @buffer: binder buffer to be accessed
1116 * @offset: offset into @buffer data
1117 * @bytes: bytes to access from offset
1118 *
1119 * Check that the @offset/@bytes are within the size of the given
1120 * @buffer and that the buffer is currently active and not freeable.
1121 * Offsets must also be multiples of sizeof(u32). The kernel is
1122 * allowed to touch the buffer in two cases:
1123 *
1124 * 1) when the buffer is being created:
1125 * (buffer->free == 0 && buffer->allow_user_free == 0)
1126 * 2) when the buffer is being torn down:
1127 * (buffer->free == 0 && buffer->transaction == NULL).
1128 *
1129 * Return: true if the buffer is safe to access
1130 */
1131static inline bool check_buffer(struct binder_alloc *alloc,
1132 struct binder_buffer *buffer,
1133 binder_size_t offset, size_t bytes)
1134{
1135 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1136
1137 return buffer_size >= bytes &&
1138 offset <= buffer_size - bytes &&
1139 IS_ALIGNED(offset, sizeof(u32)) &&
1140 !buffer->free &&
1141 (!buffer->allow_user_free || !buffer->transaction);
1142}
1143
1144/**
1145 * binder_alloc_get_page() - get kernel pointer for given buffer offset
1146 * @alloc: binder_alloc for this proc
1147 * @buffer: binder buffer to be accessed
1148 * @buffer_offset: offset into @buffer data
1149 * @pgoffp: address to copy final page offset to
1150 *
1151 * Lookup the struct page corresponding to the address
Todd Kjosbde4a192019-02-08 10:35:20 -08001152 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001153 * NULL, the byte-offset into the page is written there.
1154 *
1155 * The caller is responsible to ensure that the offset points
1156 * to a valid address within the @buffer and that @buffer is
1157 * not freeable by the user. Since it can't be freed, we are
1158 * guaranteed that the corresponding elements of @alloc->pages[]
1159 * cannot change.
1160 *
1161 * Return: struct page
1162 */
1163static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
1164 struct binder_buffer *buffer,
1165 binder_size_t buffer_offset,
1166 pgoff_t *pgoffp)
1167{
1168 binder_size_t buffer_space_offset = buffer_offset +
Todd Kjosbde4a192019-02-08 10:35:20 -08001169 (buffer->user_data - alloc->buffer);
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001170 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
1171 size_t index = buffer_space_offset >> PAGE_SHIFT;
1172 struct binder_lru_page *lru_page;
1173
1174 lru_page = &alloc->pages[index];
1175 *pgoffp = pgoff;
1176 return lru_page->page_ptr;
1177}
1178
1179/**
Todd Kjos0f966cb2020-11-20 15:37:43 -08001180 * binder_alloc_clear_buf() - zero out buffer
1181 * @alloc: binder_alloc for this proc
1182 * @buffer: binder buffer to be cleared
1183 *
1184 * memset the given buffer to 0
1185 */
1186static void binder_alloc_clear_buf(struct binder_alloc *alloc,
1187 struct binder_buffer *buffer)
1188{
1189 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
1190 binder_size_t buffer_offset = 0;
1191
1192 while (bytes) {
1193 unsigned long size;
1194 struct page *page;
1195 pgoff_t pgoff;
1196 void *kptr;
1197
1198 page = binder_alloc_get_page(alloc, buffer,
1199 buffer_offset, &pgoff);
1200 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1201 kptr = kmap(page) + pgoff;
1202 memset(kptr, 0, size);
1203 kunmap(page);
1204 bytes -= size;
1205 buffer_offset += size;
1206 }
1207}
1208
1209/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001210 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1211 * @alloc: binder_alloc for this proc
1212 * @buffer: binder buffer to be accessed
1213 * @buffer_offset: offset into @buffer data
1214 * @from: userspace pointer to source buffer
1215 * @bytes: bytes to copy
1216 *
1217 * Copy bytes from source userspace to target buffer.
1218 *
1219 * Return: bytes remaining to be copied
1220 */
1221unsigned long
1222binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1223 struct binder_buffer *buffer,
1224 binder_size_t buffer_offset,
1225 const void __user *from,
1226 size_t bytes)
1227{
1228 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1229 return bytes;
1230
1231 while (bytes) {
1232 unsigned long size;
1233 unsigned long ret;
1234 struct page *page;
1235 pgoff_t pgoff;
1236 void *kptr;
1237
1238 page = binder_alloc_get_page(alloc, buffer,
1239 buffer_offset, &pgoff);
1240 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1241 kptr = kmap(page) + pgoff;
1242 ret = copy_from_user(kptr, from, size);
1243 kunmap(page);
1244 if (ret)
1245 return bytes - size + ret;
1246 bytes -= size;
1247 from += size;
1248 buffer_offset += size;
1249 }
1250 return 0;
1251}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001252
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001253static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1254 bool to_buffer,
1255 struct binder_buffer *buffer,
1256 binder_size_t buffer_offset,
1257 void *ptr,
1258 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001259{
1260 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001261 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1262 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001263
1264 while (bytes) {
1265 unsigned long size;
1266 struct page *page;
1267 pgoff_t pgoff;
1268 void *tmpptr;
1269 void *base_ptr;
1270
1271 page = binder_alloc_get_page(alloc, buffer,
1272 buffer_offset, &pgoff);
1273 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1274 base_ptr = kmap_atomic(page);
1275 tmpptr = base_ptr + pgoff;
1276 if (to_buffer)
1277 memcpy(tmpptr, ptr, size);
1278 else
1279 memcpy(ptr, tmpptr, size);
1280 /*
1281 * kunmap_atomic() takes care of flushing the cache
1282 * if this device has VIVT cache arch
1283 */
1284 kunmap_atomic(base_ptr);
1285 bytes -= size;
1286 pgoff = 0;
1287 ptr = ptr + size;
1288 buffer_offset += size;
1289 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001290 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001291}
1292
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001293int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1294 struct binder_buffer *buffer,
1295 binder_size_t buffer_offset,
1296 void *src,
1297 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001298{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001299 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1300 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001301}
1302
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001303int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1304 void *dest,
1305 struct binder_buffer *buffer,
1306 binder_size_t buffer_offset,
1307 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001308{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001309 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1310 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001311}
1312