blob: bd16fcc4e9f5806259f2adec3b14a353c8162e60 [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
Carlos Llamas26d06d92023-12-01 17:21:41 +0000323static bool debug_low_async_space_locked(struct binder_alloc *alloc)
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 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200332 struct binder_buffer *buffer;
333 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000334 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200335 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000336 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200337
338 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
339 n = rb_next(n)) {
340 buffer = rb_entry(n, struct binder_buffer, rb_node);
341 if (buffer->pid != pid)
342 continue;
343 if (!buffer->async_transaction)
344 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000345 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200346 num_buffers++;
347 }
348
349 /*
350 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800351 * async space (which is 25% of total buffer size). Oneway spam is only
352 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200353 */
354 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
355 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
356 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
357 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800358 if (!alloc->oneway_spam_detected) {
359 alloc->oneway_spam_detected = true;
360 return true;
361 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200362 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800363 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200364}
365
Xiongwei Song3f827242017-12-14 12:15:42 +0800366static struct binder_buffer *binder_alloc_new_buf_locked(
367 struct binder_alloc *alloc,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000368 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000369 int is_async)
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;
Hang Lua7dc1e62021-04-09 17:40:46 +0800482 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700483 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000484 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700485 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
486 "%d: binder_alloc_buf size %zd async free %zd\n",
487 alloc->pid, size, alloc->free_async_space);
Martijn Coenen261e7812020-08-21 14:25:44 +0200488 if (alloc->free_async_space < alloc->buffer_size / 10) {
489 /*
490 * Start detecting spammers once we have less than 20%
491 * of async space left (which is less than 10% of total
492 * buffer size).
493 */
Carlos Llamas26d06d92023-12-01 17:21:41 +0000494 buffer->oneway_spam_suspect = debug_low_async_space_locked(alloc);
Hang Lua7dc1e62021-04-09 17:40:46 +0800495 } else {
496 alloc->oneway_spam_detected = false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200497 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700498 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000499
Todd Kjos0c972a02017-06-29 12:01:41 -0700500 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700501
502err_alloc_buf_struct_failed:
Carlos Llamas0b243682023-12-01 17:21:39 +0000503 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
504 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700505 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700506}
507
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000508/* Calculate the sanitized total size, returns 0 for invalid request */
509static inline size_t sanitized_size(size_t data_size,
510 size_t offsets_size,
511 size_t extra_buffers_size)
512{
513 size_t total, tmp;
514
515 /* Align to pointer size and check for overflows */
516 tmp = ALIGN(data_size, sizeof(void *)) +
517 ALIGN(offsets_size, sizeof(void *));
518 if (tmp < data_size || tmp < offsets_size)
519 return 0;
520 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
521 if (total < tmp || total < extra_buffers_size)
522 return 0;
523
524 /* Pad 0-sized buffers so they get a unique address */
525 total = max(total, sizeof(void *));
526
527 return total;
528}
529
Todd Kjos0c972a02017-06-29 12:01:41 -0700530/**
531 * binder_alloc_new_buf() - Allocate a new binder buffer
532 * @alloc: binder_alloc for this proc
533 * @data_size: size of user data buffer
534 * @offsets_size: user specified buffer offset
535 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
536 * @is_async: buffer for async transaction
537 *
538 * Allocate a new buffer given the requested sizes. Returns
539 * the kernel version of the buffer pointer. The size allocated
540 * is the sum of the three given sizes (each rounded up to
541 * pointer-sized boundary)
542 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000543 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700544 */
545struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
546 size_t data_size,
547 size_t offsets_size,
548 size_t extra_buffers_size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000549 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700550{
551 struct binder_buffer *buffer;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000552 size_t size;
553
554 /* Check binder_alloc is fully initialized */
555 if (!binder_alloc_get_vma(alloc)) {
556 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
557 "%d: binder_alloc_buf, no vma\n",
558 alloc->pid);
559 return ERR_PTR(-ESRCH);
560 }
561
562 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
563 if (unlikely(!size)) {
564 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
565 "%d: got transaction with invalid size %zd-%zd-%zd\n",
566 alloc->pid, data_size, offsets_size,
567 extra_buffers_size);
568 return ERR_PTR(-EINVAL);
569 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700570
571 mutex_lock(&alloc->mutex);
Carlos Llamas26d06d92023-12-01 17:21:41 +0000572 buffer = binder_alloc_new_buf_locked(alloc, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000573 if (IS_ERR(buffer)) {
574 mutex_unlock(&alloc->mutex);
575 goto out;
576 }
577
578 buffer->data_size = data_size;
579 buffer->offsets_size = offsets_size;
580 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000581 buffer->pid = current->tgid;
Todd Kjos0c972a02017-06-29 12:01:41 -0700582 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000583
584out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700585 return buffer;
586}
587
Carlos Llamasc38a8982023-12-01 17:21:38 +0000588static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700589{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000590 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700591}
592
Carlos Llamasc38a8982023-12-01 17:21:38 +0000593static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700594{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000595 return (buffer->user_data - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700596}
597
598static void binder_delete_free_buffer(struct binder_alloc *alloc,
599 struct binder_buffer *buffer)
600{
601 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700602 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530603
Todd Kjos0c972a02017-06-29 12:01:41 -0700604 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700605 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700606 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700607 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
608 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700609 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000610 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800611 alloc->pid, buffer->user_data,
612 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700613 }
614
615 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700616 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700617 if (buffer_start_page(next) == buffer_start_page(buffer)) {
618 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700619 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000620 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700621 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800622 buffer->user_data,
623 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700624 }
625 }
Sherry Yang74310e02017-08-23 08:46:41 -0700626
Todd Kjosbde4a192019-02-08 10:35:20 -0800627 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700628 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000629 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800630 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700631 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700632 }
Sherry Yang74310e02017-08-23 08:46:41 -0700633
634 if (to_free) {
635 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000636 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800637 alloc->pid, buffer->user_data,
638 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000639 next ? next->user_data : 0);
Carlos Llamas0b243682023-12-01 17:21:39 +0000640 binder_free_page_range(alloc, buffer_start_page(buffer),
641 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700642 }
643 list_del(&buffer->entry);
644 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700645}
646
647static void binder_free_buf_locked(struct binder_alloc *alloc,
648 struct binder_buffer *buffer)
649{
650 size_t size, buffer_size;
651
652 buffer_size = binder_alloc_buffer_size(alloc, buffer);
653
654 size = ALIGN(buffer->data_size, sizeof(void *)) +
655 ALIGN(buffer->offsets_size, sizeof(void *)) +
656 ALIGN(buffer->extra_buffers_size, sizeof(void *));
657
658 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
659 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
660 alloc->pid, buffer, size, buffer_size);
661
662 BUG_ON(buffer->free);
663 BUG_ON(size > buffer_size);
664 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800665 BUG_ON(buffer->user_data < alloc->buffer);
666 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700667
668 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000669 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700670 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
671 "%d: binder_free_buf size %zd async free %zd\n",
672 alloc->pid, size, alloc->free_async_space);
673 }
674
Carlos Llamas0b243682023-12-01 17:21:39 +0000675 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
676 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700677
678 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
679 buffer->free = 1;
680 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700681 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700682
683 if (next->free) {
684 rb_erase(&next->rb_node, &alloc->free_buffers);
685 binder_delete_free_buffer(alloc, next);
686 }
687 }
688 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700689 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700690
691 if (prev->free) {
692 binder_delete_free_buffer(alloc, buffer);
693 rb_erase(&prev->rb_node, &alloc->free_buffers);
694 buffer = prev;
695 }
696 }
697 binder_insert_free_buffer(alloc, buffer);
698}
699
Todd Kjos0f966cb2020-11-20 15:37:43 -0800700static void binder_alloc_clear_buf(struct binder_alloc *alloc,
701 struct binder_buffer *buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700702/**
703 * binder_alloc_free_buf() - free a binder buffer
704 * @alloc: binder_alloc for this proc
705 * @buffer: kernel pointer to buffer
706 *
YangHui4b463822020-08-18 09:34:04 +0800707 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700708 */
709void binder_alloc_free_buf(struct binder_alloc *alloc,
710 struct binder_buffer *buffer)
711{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800712 /*
713 * We could eliminate the call to binder_alloc_clear_buf()
714 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000715 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800716 * increase contention for the alloc mutex if clear_on_free
717 * is used frequently for large buffers. The mutex is not
718 * needed for correctness here.
719 */
720 if (buffer->clear_on_free) {
721 binder_alloc_clear_buf(alloc, buffer);
722 buffer->clear_on_free = false;
723 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700724 mutex_lock(&alloc->mutex);
725 binder_free_buf_locked(alloc, buffer);
726 mutex_unlock(&alloc->mutex);
727}
728
729/**
730 * binder_alloc_mmap_handler() - map virtual address space for proc
731 * @alloc: alloc structure for this proc
732 * @vma: vma passed to mmap()
733 *
734 * Called by binder_mmap() to initialize the space specified in
735 * vma for allocating binder buffers
736 *
737 * Return:
738 * 0 = success
739 * -EBUSY = address space already mapped
740 * -ENOMEM = failed to map memory to given address space
741 */
742int binder_alloc_mmap_handler(struct binder_alloc *alloc,
743 struct vm_area_struct *vma)
744{
745 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700746 const char *failure_string;
747 struct binder_buffer *buffer;
748
Carlos Llamasd276fb42022-11-04 23:12:35 +0000749 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
750 ret = -EINVAL;
751 failure_string = "invalid vma->vm_mm";
752 goto err_invalid_mm;
753 }
754
Todd Kjos0c972a02017-06-29 12:01:41 -0700755 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200756 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700757 ret = -EBUSY;
758 failure_string = "already mapped";
759 goto err_already_mapped;
760 }
Jann Horn45d02f72019-10-16 17:01:18 +0200761 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
762 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200763 mutex_unlock(&binder_alloc_mmap_lock);
764
Carlos Llamasc38a8982023-12-01 17:21:38 +0000765 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200766
Jann Horn45d02f72019-10-16 17:01:18 +0200767 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700768 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700769 GFP_KERNEL);
770 if (alloc->pages == NULL) {
771 ret = -ENOMEM;
772 failure_string = "alloc page array";
773 goto err_alloc_pages_failed;
774 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700775
Sherry Yang74310e02017-08-23 08:46:41 -0700776 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
777 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700778 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700779 failure_string = "alloc buffer struct";
780 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700781 }
Sherry Yang74310e02017-08-23 08:46:41 -0700782
Todd Kjosbde4a192019-02-08 10:35:20 -0800783 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700784 list_add(&buffer->entry, &alloc->buffers);
785 buffer->free = 1;
786 binder_insert_free_buffer(alloc, buffer);
787 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000788
789 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900790 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700791
792 return 0;
793
Sherry Yang74310e02017-08-23 08:46:41 -0700794err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700795 kfree(alloc->pages);
796 alloc->pages = NULL;
797err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000798 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200799 mutex_lock(&binder_alloc_mmap_lock);
800 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700801err_already_mapped:
802 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000803err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700804 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
805 "%s: %d %lx-%lx %s failed %d\n", __func__,
806 alloc->pid, vma->vm_start, vma->vm_end,
807 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700808 return ret;
809}
810
811
812void binder_alloc_deferred_release(struct binder_alloc *alloc)
813{
814 struct rb_node *n;
815 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700816 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700817
Todd Kjos0c972a02017-06-29 12:01:41 -0700818 buffers = 0;
819 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000820 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900821
Todd Kjos0c972a02017-06-29 12:01:41 -0700822 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700823 buffer = rb_entry(n, struct binder_buffer, rb_node);
824
825 /* Transaction should already have been freed */
826 BUG_ON(buffer->transaction);
827
Todd Kjos0f966cb2020-11-20 15:37:43 -0800828 if (buffer->clear_on_free) {
829 binder_alloc_clear_buf(alloc, buffer);
830 buffer->clear_on_free = false;
831 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700832 binder_free_buf_locked(alloc, buffer);
833 buffers++;
834 }
835
Sherry Yang74310e02017-08-23 08:46:41 -0700836 while (!list_empty(&alloc->buffers)) {
837 buffer = list_first_entry(&alloc->buffers,
838 struct binder_buffer, entry);
839 WARN_ON(!buffer->free);
840
841 list_del(&buffer->entry);
842 WARN_ON_ONCE(!list_empty(&alloc->buffers));
843 kfree(buffer);
844 }
845
Todd Kjos0c972a02017-06-29 12:01:41 -0700846 page_count = 0;
847 if (alloc->pages) {
848 int i;
849
850 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000851 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700852 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700853
Sherry Yangf2517eb2017-08-23 08:46:42 -0700854 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700855 continue;
856
Sherry Yangf2517eb2017-08-23 08:46:42 -0700857 on_lru = list_lru_del(&binder_alloc_lru,
858 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700859 page_addr = alloc->buffer + i * PAGE_SIZE;
860 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000861 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700862 __func__, alloc->pid, i, page_addr,
863 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700864 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700865 page_count++;
866 }
867 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700868 }
869 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400870 if (alloc->vma_vm_mm)
871 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700872
873 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
874 "%s: %d buffers %d, pages %d\n",
875 __func__, alloc->pid, buffers, page_count);
876}
877
878static void print_binder_buffer(struct seq_file *m, const char *prefix,
879 struct binder_buffer *buffer)
880{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000881 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800882 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700883 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700884 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700885 buffer->transaction ? "active" : "delivered");
886}
887
888/**
889 * binder_alloc_print_allocated() - print buffer info
890 * @m: seq_file for output via seq_printf()
891 * @alloc: binder_alloc for this proc
892 *
893 * Prints information about every buffer associated with
894 * the binder_alloc state to the given seq_file
895 */
896void binder_alloc_print_allocated(struct seq_file *m,
897 struct binder_alloc *alloc)
898{
899 struct rb_node *n;
900
901 mutex_lock(&alloc->mutex);
902 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
903 print_binder_buffer(m, " buffer",
904 rb_entry(n, struct binder_buffer, rb_node));
905 mutex_unlock(&alloc->mutex);
906}
907
908/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700909 * binder_alloc_print_pages() - print page usage
910 * @m: seq_file for output via seq_printf()
911 * @alloc: binder_alloc for this proc
912 */
913void binder_alloc_print_pages(struct seq_file *m,
914 struct binder_alloc *alloc)
915{
916 struct binder_lru_page *page;
917 int i;
918 int active = 0;
919 int lru = 0;
920 int free = 0;
921
922 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200923 /*
924 * Make sure the binder_alloc is fully initialized, otherwise we might
925 * read inconsistent state.
926 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000927 if (binder_alloc_get_vma(alloc) != NULL) {
928 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
929 page = &alloc->pages[i];
930 if (!page->page_ptr)
931 free++;
932 else if (list_empty(&page->lru))
933 active++;
934 else
935 lru++;
936 }
Sherry Yang8ef46652017-08-31 11:56:36 -0700937 }
938 mutex_unlock(&alloc->mutex);
939 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100940 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -0700941}
942
943/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700944 * binder_alloc_get_allocated_count() - return count of buffers
945 * @alloc: binder_alloc for this proc
946 *
947 * Return: count of allocated buffers
948 */
949int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
950{
951 struct rb_node *n;
952 int count = 0;
953
954 mutex_lock(&alloc->mutex);
955 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
956 count++;
957 mutex_unlock(&alloc->mutex);
958 return count;
959}
960
961
962/**
963 * binder_alloc_vma_close() - invalidate address space
964 * @alloc: binder_alloc for this proc
965 *
966 * Called from binder_vma_close() when releasing address space.
967 * Clears alloc->vma to prevent new incoming transactions from
968 * allocating more buffers.
969 */
970void binder_alloc_vma_close(struct binder_alloc *alloc)
971{
Minchan Kimda1b9562018-08-23 14:29:56 +0900972 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700973}
974
975/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700976 * binder_alloc_free_page() - shrinker callback to free pages
977 * @item: item to free
978 * @lock: lock protecting the item
979 * @cb_arg: callback argument
980 *
981 * Called from list_lru_walk() in binder_shrink_scan() to free
982 * up pages when the system is under memory pressure.
983 */
984enum lru_status binder_alloc_free_page(struct list_head *item,
985 struct list_lru_one *lru,
986 spinlock_t *lock,
987 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -0800988 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -0700989{
990 struct mm_struct *mm = NULL;
991 struct binder_lru_page *page = container_of(item,
992 struct binder_lru_page,
993 lru);
994 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000995 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700996 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -0700997 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700998
999 alloc = page->alloc;
1000 if (!mutex_trylock(&alloc->mutex))
1001 goto err_get_alloc_mutex_failed;
1002
1003 if (!page->page_ptr)
1004 goto err_page_already_freed;
1005
1006 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001007 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001008
1009 mm = alloc->vma_vm_mm;
1010 if (!mmget_not_zero(mm))
1011 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001012 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001013 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001014 vma = vma_lookup(mm, page_addr);
1015 if (vma && vma != binder_alloc_get_vma(alloc))
1016 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001017
Sherry Yanga1b22892017-10-03 16:15:00 -07001018 list_lru_isolate(lru, item);
1019 spin_unlock(lock);
1020
1021 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001022 trace_binder_unmap_user_start(alloc, index);
1023
Todd Kjosc41358a2019-02-08 10:35:19 -08001024 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001025
Sherry Yange41e1642017-08-23 08:46:43 -07001026 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001027 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001028 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001029 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001030
Sherry Yange41e1642017-08-23 08:46:43 -07001031 trace_binder_unmap_kernel_start(alloc, index);
1032
Sherry Yangf2517eb2017-08-23 08:46:42 -07001033 __free_page(page->page_ptr);
1034 page->page_ptr = NULL;
1035
Sherry Yange41e1642017-08-23 08:46:43 -07001036 trace_binder_unmap_kernel_end(alloc, index);
1037
Sherry Yanga1b22892017-10-03 16:15:00 -07001038 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001039 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001040 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001041
Carlos Llamas8dce2882023-12-01 17:21:31 +00001042err_invalid_vma:
1043 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001044err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001045 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001046err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001047err_page_already_freed:
1048 mutex_unlock(&alloc->mutex);
1049err_get_alloc_mutex_failed:
1050 return LRU_SKIP;
1051}
1052
1053static unsigned long
1054binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1055{
1056 unsigned long ret = list_lru_count(&binder_alloc_lru);
1057 return ret;
1058}
1059
1060static unsigned long
1061binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1062{
1063 unsigned long ret;
1064
1065 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1066 NULL, sc->nr_to_scan);
1067 return ret;
1068}
1069
Sherry Yangde7bbe32017-10-06 16:12:05 -04001070static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001071 .count_objects = binder_shrink_count,
1072 .scan_objects = binder_shrink_scan,
1073 .seeks = DEFAULT_SEEKS,
1074};
1075
1076/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001077 * binder_alloc_init() - called by binder_open() for per-proc initialization
1078 * @alloc: binder_alloc for this proc
1079 *
1080 * Called from binder_open() to initialize binder_alloc fields for
1081 * new binder proc
1082 */
1083void binder_alloc_init(struct binder_alloc *alloc)
1084{
Todd Kjos0c972a02017-06-29 12:01:41 -07001085 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001086 alloc->vma_vm_mm = current->mm;
1087 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001088 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001089 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001090}
1091
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001092int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001093{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001094 int ret = list_lru_init(&binder_alloc_lru);
1095
1096 if (ret == 0) {
1097 ret = register_shrinker(&binder_shrinker);
1098 if (ret)
1099 list_lru_destroy(&binder_alloc_lru);
1100 }
1101 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001102}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001103
Qi Zheng03eebad2023-06-25 15:49:37 +00001104void binder_alloc_shrinker_exit(void)
1105{
1106 unregister_shrinker(&binder_shrinker);
1107 list_lru_destroy(&binder_alloc_lru);
1108}
1109
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001110/**
1111 * check_buffer() - verify that buffer/offset is safe to access
1112 * @alloc: binder_alloc for this proc
1113 * @buffer: binder buffer to be accessed
1114 * @offset: offset into @buffer data
1115 * @bytes: bytes to access from offset
1116 *
1117 * Check that the @offset/@bytes are within the size of the given
1118 * @buffer and that the buffer is currently active and not freeable.
1119 * Offsets must also be multiples of sizeof(u32). The kernel is
1120 * allowed to touch the buffer in two cases:
1121 *
1122 * 1) when the buffer is being created:
1123 * (buffer->free == 0 && buffer->allow_user_free == 0)
1124 * 2) when the buffer is being torn down:
1125 * (buffer->free == 0 && buffer->transaction == NULL).
1126 *
1127 * Return: true if the buffer is safe to access
1128 */
1129static inline bool check_buffer(struct binder_alloc *alloc,
1130 struct binder_buffer *buffer,
1131 binder_size_t offset, size_t bytes)
1132{
1133 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1134
1135 return buffer_size >= bytes &&
1136 offset <= buffer_size - bytes &&
1137 IS_ALIGNED(offset, sizeof(u32)) &&
1138 !buffer->free &&
1139 (!buffer->allow_user_free || !buffer->transaction);
1140}
1141
1142/**
1143 * binder_alloc_get_page() - get kernel pointer for given buffer offset
1144 * @alloc: binder_alloc for this proc
1145 * @buffer: binder buffer to be accessed
1146 * @buffer_offset: offset into @buffer data
1147 * @pgoffp: address to copy final page offset to
1148 *
1149 * Lookup the struct page corresponding to the address
Todd Kjosbde4a192019-02-08 10:35:20 -08001150 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001151 * NULL, the byte-offset into the page is written there.
1152 *
1153 * The caller is responsible to ensure that the offset points
1154 * to a valid address within the @buffer and that @buffer is
1155 * not freeable by the user. Since it can't be freed, we are
1156 * guaranteed that the corresponding elements of @alloc->pages[]
1157 * cannot change.
1158 *
1159 * Return: struct page
1160 */
1161static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
1162 struct binder_buffer *buffer,
1163 binder_size_t buffer_offset,
1164 pgoff_t *pgoffp)
1165{
1166 binder_size_t buffer_space_offset = buffer_offset +
Todd Kjosbde4a192019-02-08 10:35:20 -08001167 (buffer->user_data - alloc->buffer);
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001168 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
1169 size_t index = buffer_space_offset >> PAGE_SHIFT;
1170 struct binder_lru_page *lru_page;
1171
1172 lru_page = &alloc->pages[index];
1173 *pgoffp = pgoff;
1174 return lru_page->page_ptr;
1175}
1176
1177/**
Todd Kjos0f966cb2020-11-20 15:37:43 -08001178 * binder_alloc_clear_buf() - zero out buffer
1179 * @alloc: binder_alloc for this proc
1180 * @buffer: binder buffer to be cleared
1181 *
1182 * memset the given buffer to 0
1183 */
1184static void binder_alloc_clear_buf(struct binder_alloc *alloc,
1185 struct binder_buffer *buffer)
1186{
1187 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
1188 binder_size_t buffer_offset = 0;
1189
1190 while (bytes) {
1191 unsigned long size;
1192 struct page *page;
1193 pgoff_t pgoff;
1194 void *kptr;
1195
1196 page = binder_alloc_get_page(alloc, buffer,
1197 buffer_offset, &pgoff);
1198 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1199 kptr = kmap(page) + pgoff;
1200 memset(kptr, 0, size);
1201 kunmap(page);
1202 bytes -= size;
1203 buffer_offset += size;
1204 }
1205}
1206
1207/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001208 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1209 * @alloc: binder_alloc for this proc
1210 * @buffer: binder buffer to be accessed
1211 * @buffer_offset: offset into @buffer data
1212 * @from: userspace pointer to source buffer
1213 * @bytes: bytes to copy
1214 *
1215 * Copy bytes from source userspace to target buffer.
1216 *
1217 * Return: bytes remaining to be copied
1218 */
1219unsigned long
1220binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1221 struct binder_buffer *buffer,
1222 binder_size_t buffer_offset,
1223 const void __user *from,
1224 size_t bytes)
1225{
1226 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1227 return bytes;
1228
1229 while (bytes) {
1230 unsigned long size;
1231 unsigned long ret;
1232 struct page *page;
1233 pgoff_t pgoff;
1234 void *kptr;
1235
1236 page = binder_alloc_get_page(alloc, buffer,
1237 buffer_offset, &pgoff);
1238 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1239 kptr = kmap(page) + pgoff;
1240 ret = copy_from_user(kptr, from, size);
1241 kunmap(page);
1242 if (ret)
1243 return bytes - size + ret;
1244 bytes -= size;
1245 from += size;
1246 buffer_offset += size;
1247 }
1248 return 0;
1249}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001250
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001251static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1252 bool to_buffer,
1253 struct binder_buffer *buffer,
1254 binder_size_t buffer_offset,
1255 void *ptr,
1256 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001257{
1258 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001259 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1260 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001261
1262 while (bytes) {
1263 unsigned long size;
1264 struct page *page;
1265 pgoff_t pgoff;
1266 void *tmpptr;
1267 void *base_ptr;
1268
1269 page = binder_alloc_get_page(alloc, buffer,
1270 buffer_offset, &pgoff);
1271 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1272 base_ptr = kmap_atomic(page);
1273 tmpptr = base_ptr + pgoff;
1274 if (to_buffer)
1275 memcpy(tmpptr, ptr, size);
1276 else
1277 memcpy(ptr, tmpptr, size);
1278 /*
1279 * kunmap_atomic() takes care of flushing the cache
1280 * if this device has VIVT cache arch
1281 */
1282 kunmap_atomic(base_ptr);
1283 bytes -= size;
1284 pgoff = 0;
1285 ptr = ptr + size;
1286 buffer_offset += size;
1287 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001288 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001289}
1290
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001291int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1292 struct binder_buffer *buffer,
1293 binder_size_t buffer_offset,
1294 void *src,
1295 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001296{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001297 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1298 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001299}
1300
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001301int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1302 void *dest,
1303 struct binder_buffer *buffer,
1304 binder_size_t buffer_offset,
1305 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001306{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001307 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1308 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001309}
1310