blob: eddfe6bd51b8a66fb1f481aab8720b3753b923c2 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Todd Kjos0c972a02017-06-29 12:01:41 -07002/* binder_alloc.c
3 *
4 * Android IPC Subsystem
5 *
6 * Copyright (C) 2007-2017 Google, Inc.
Todd Kjos0c972a02017-06-29 12:01:41 -07007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Todd Kjos0c972a02017-06-29 12:01:41 -070011#include <linux/list.h>
12#include <linux/sched/mm.h>
13#include <linux/module.h>
14#include <linux/rtmutex.h>
15#include <linux/rbtree.h>
16#include <linux/seq_file.h>
17#include <linux/vmalloc.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
Sherry Yangf2517eb2017-08-23 08:46:42 -070020#include <linux/list_lru.h>
Sherry Yang128f3802018-08-07 12:57:13 -070021#include <linux/ratelimit.h>
Guenter Roeck1e81c572018-07-23 14:47:23 -070022#include <asm/cacheflush.h>
Todd Kjos1a7c3d92019-02-08 10:35:14 -080023#include <linux/uaccess.h>
24#include <linux/highmem.h>
Jann Horn45d02f72019-10-16 17:01:18 +020025#include <linux/sizes.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070026#include "binder_alloc.h"
27#include "binder_trace.h"
Zhuguangqing1174e452021-03-09 15:47:43 +080028#include <trace/hooks/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070029
Sherry Yangf2517eb2017-08-23 08:46:42 -070030struct list_lru binder_alloc_lru;
31
Todd Kjos0c972a02017-06-29 12:01:41 -070032static DEFINE_MUTEX(binder_alloc_mmap_lock);
33
34enum {
Sherry Yang128f3802018-08-07 12:57:13 -070035 BINDER_DEBUG_USER_ERROR = 1U << 0,
Todd Kjos0c972a02017-06-29 12:01:41 -070036 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
37 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
38 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
39};
Sherry Yang128f3802018-08-07 12:57:13 -070040static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
Todd Kjos0c972a02017-06-29 12:01:41 -070041
42module_param_named(debug_mask, binder_alloc_debug_mask,
43 uint, 0644);
44
45#define binder_alloc_debug(mask, x...) \
46 do { \
47 if (binder_alloc_debug_mask & mask) \
Sherry Yang128f3802018-08-07 12:57:13 -070048 pr_info_ratelimited(x); \
Todd Kjos0c972a02017-06-29 12:01:41 -070049 } while (0)
50
Sherry Yange21762192017-08-23 08:46:39 -070051static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
52{
53 return list_entry(buffer->entry.next, struct binder_buffer, entry);
54}
55
56static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
57{
58 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
59}
60
Todd Kjos0c972a02017-06-29 12:01:41 -070061static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
62 struct binder_buffer *buffer)
63{
64 if (list_is_last(&buffer->entry, &alloc->buffers))
Todd Kjosbde4a192019-02-08 10:35:20 -080065 return alloc->buffer + alloc->buffer_size - buffer->user_data;
66 return binder_buffer_next(buffer)->user_data - buffer->user_data;
Todd Kjos0c972a02017-06-29 12:01:41 -070067}
68
69static void binder_insert_free_buffer(struct binder_alloc *alloc,
70 struct binder_buffer *new_buffer)
71{
72 struct rb_node **p = &alloc->free_buffers.rb_node;
73 struct rb_node *parent = NULL;
74 struct binder_buffer *buffer;
75 size_t buffer_size;
76 size_t new_buffer_size;
77
78 BUG_ON(!new_buffer->free);
79
80 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
81
82 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
83 "%d: add free buffer, size %zd, at %pK\n",
84 alloc->pid, new_buffer_size, new_buffer);
85
86 while (*p) {
87 parent = *p;
88 buffer = rb_entry(parent, struct binder_buffer, rb_node);
89 BUG_ON(!buffer->free);
90
91 buffer_size = binder_alloc_buffer_size(alloc, buffer);
92
93 if (new_buffer_size < buffer_size)
94 p = &parent->rb_left;
95 else
96 p = &parent->rb_right;
97 }
98 rb_link_node(&new_buffer->rb_node, parent, p);
99 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
100}
101
102static void binder_insert_allocated_buffer_locked(
103 struct binder_alloc *alloc, struct binder_buffer *new_buffer)
104{
105 struct rb_node **p = &alloc->allocated_buffers.rb_node;
106 struct rb_node *parent = NULL;
107 struct binder_buffer *buffer;
108
109 BUG_ON(new_buffer->free);
110
111 while (*p) {
112 parent = *p;
113 buffer = rb_entry(parent, struct binder_buffer, rb_node);
114 BUG_ON(buffer->free);
115
Todd Kjosbde4a192019-02-08 10:35:20 -0800116 if (new_buffer->user_data < buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700117 p = &parent->rb_left;
Todd Kjosbde4a192019-02-08 10:35:20 -0800118 else if (new_buffer->user_data > buffer->user_data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700119 p = &parent->rb_right;
120 else
121 BUG();
122 }
123 rb_link_node(&new_buffer->rb_node, parent, p);
124 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
125}
126
Todd Kjos53d311cf2017-06-29 12:01:51 -0700127static struct binder_buffer *binder_alloc_prepare_to_free_locked(
Todd Kjos0c972a02017-06-29 12:01:41 -0700128 struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000129 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700130{
131 struct rb_node *n = alloc->allocated_buffers.rb_node;
132 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700133
134 while (n) {
135 buffer = rb_entry(n, struct binder_buffer, rb_node);
136 BUG_ON(buffer->free);
137
Carlos Llamasc38a8982023-12-01 17:21:38 +0000138 if (user_ptr < buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700139 n = n->rb_left;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000140 } else if (user_ptr > buffer->user_data) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700141 n = n->rb_right;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000142 } else {
Todd Kjos53d311cf2017-06-29 12:01:51 -0700143 /*
144 * Guard against user threads attempting to
Todd Kjos7bada552018-11-06 15:55:32 -0800145 * free the buffer when in use by kernel or
146 * after it's already been freed.
Todd Kjos53d311cf2017-06-29 12:01:51 -0700147 */
Todd Kjos7bada552018-11-06 15:55:32 -0800148 if (!buffer->allow_user_free)
149 return ERR_PTR(-EPERM);
150 buffer->allow_user_free = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700151 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700152 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700153 }
154 return NULL;
155}
156
157/**
Joel Fernandes (Google)5dc54a02019-09-30 16:12:50 -0400158 * binder_alloc_prepare_to_free() - get buffer given user ptr
Todd Kjos0c972a02017-06-29 12:01:41 -0700159 * @alloc: binder_alloc for this proc
160 * @user_ptr: User pointer to buffer data
161 *
162 * Validate userspace pointer to buffer data and return buffer corresponding to
163 * that user pointer. Search the rb tree for buffer that matches user data
164 * pointer.
165 *
166 * Return: Pointer to buffer or NULL
167 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700168struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000169 unsigned long user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700170{
171 struct binder_buffer *buffer;
172
173 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700174 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700175 mutex_unlock(&alloc->mutex);
176 return buffer;
177}
178
Carlos Llamas0b243682023-12-01 17:21:39 +0000179static void binder_free_page_range(struct binder_alloc *alloc,
180 unsigned long start, unsigned long end)
181{
182 struct binder_lru_page *page;
183 unsigned long page_addr;
184
185 trace_binder_update_page_range(alloc, false, start, end);
186
187 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
188 size_t index;
189 int ret;
190
191 index = (page_addr - alloc->buffer) / PAGE_SIZE;
192 page = &alloc->pages[index];
193
194 trace_binder_free_lru_start(alloc, index);
195
196 ret = list_lru_add(&binder_alloc_lru, &page->lru);
197 WARN_ON(!ret);
198
199 trace_binder_free_lru_end(alloc, index);
200 }
201}
202
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000203static int binder_install_single_page(struct binder_alloc *alloc,
204 struct binder_lru_page *lru_page,
205 unsigned long addr)
206{
207 struct page *page;
208 int ret = 0;
209
210 if (!mmget_not_zero(alloc->vma_vm_mm))
211 return -ESRCH;
212
213 mmap_write_lock(alloc->vma_vm_mm);
214 if (!alloc->vma) {
215 pr_err("%d: %s failed, no vma\n", alloc->pid, __func__);
216 ret = -ESRCH;
217 goto out;
218 }
219
220 page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
221 if (!page) {
222 pr_err("%d: failed to allocate page\n", alloc->pid);
223 ret = -ENOMEM;
224 goto out;
225 }
226
227 ret = vm_insert_page(alloc->vma, addr, page);
228 if (ret) {
229 pr_err("%d: %s failed to insert page at %lx with %d\n",
230 alloc->pid, __func__, addr, ret);
231 __free_page(page);
232 ret = -ENOMEM;
233 goto out;
234 }
235
236 lru_page->page_ptr = page;
237out:
238 mmap_write_unlock(alloc->vma_vm_mm);
239 mmput_async(alloc->vma_vm_mm);
240 return ret;
241}
242
Carlos Llamas0b243682023-12-01 17:21:39 +0000243static int binder_allocate_page_range(struct binder_alloc *alloc,
244 unsigned long start, unsigned long end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700245{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000246 struct binder_lru_page *page;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000247 unsigned long page_addr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700248
249 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamas0b243682023-12-01 17:21:39 +0000250 "%d: allocate pages %lx-%lx\n",
251 alloc->pid, start, end);
Todd Kjos0c972a02017-06-29 12:01:41 -0700252
253 if (end <= start)
254 return 0;
255
Carlos Llamas0b243682023-12-01 17:21:39 +0000256 trace_binder_update_page_range(alloc, true, start, end);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700257
258 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000259 unsigned long index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700260 bool on_lru;
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000261 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700262
Sherry Yange41e1642017-08-23 08:46:43 -0700263 index = (page_addr - alloc->buffer) / PAGE_SIZE;
264 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700265
Sherry Yangf2517eb2017-08-23 08:46:42 -0700266 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700267 trace_binder_alloc_lru_start(alloc, index);
268
Sherry Yangf2517eb2017-08-23 08:46:42 -0700269 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
270 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700271
272 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700273 continue;
274 }
275
Sherry Yange41e1642017-08-23 08:46:43 -0700276 trace_binder_alloc_page_start(alloc, index);
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000277
Sherry Yangf2517eb2017-08-23 08:46:42 -0700278 page->alloc = alloc;
279 INIT_LIST_HEAD(&page->lru);
280
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000281 ret = binder_install_single_page(alloc, page, page_addr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700282 if (ret) {
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000283 binder_free_page_range(alloc, start, page_addr);
284 return ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700285 }
Sherry Yange41e1642017-08-23 08:46:43 -0700286
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100287 if (index + 1 > alloc->pages_high)
288 alloc->pages_high = index + 1;
289
Sherry Yange41e1642017-08-23 08:46:43 -0700290 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700291 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700292
Carlos Llamasb23dbdb2023-12-01 17:21:45 +0000293 return 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700294}
295
Minchan Kimda1b9562018-08-23 14:29:56 +0900296static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
297 struct vm_area_struct *vma)
298{
Carlos Llamasb094b042023-05-30 19:43:37 +0000299 /* pairs with smp_load_acquire in binder_alloc_get_vma() */
300 smp_store_release(&alloc->vma, vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900301}
302
303static inline struct vm_area_struct *binder_alloc_get_vma(
304 struct binder_alloc *alloc)
305{
Carlos Llamasb094b042023-05-30 19:43:37 +0000306 /* pairs with smp_store_release in binder_alloc_set_vma() */
307 return smp_load_acquire(&alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900308}
309
Carlos Llamase1d195e2023-12-01 17:21:42 +0000310static void debug_no_space_locked(struct binder_alloc *alloc)
311{
312 size_t largest_alloc_size = 0;
313 struct binder_buffer *buffer;
314 size_t allocated_buffers = 0;
315 size_t largest_free_size = 0;
316 size_t total_alloc_size = 0;
317 size_t total_free_size = 0;
318 size_t free_buffers = 0;
319 size_t buffer_size;
320 struct rb_node *n;
321
322 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
323 buffer = rb_entry(n, struct binder_buffer, rb_node);
324 buffer_size = binder_alloc_buffer_size(alloc, buffer);
325 allocated_buffers++;
326 total_alloc_size += buffer_size;
327 if (buffer_size > largest_alloc_size)
328 largest_alloc_size = buffer_size;
329 }
330
331 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
332 buffer = rb_entry(n, struct binder_buffer, rb_node);
333 buffer_size = binder_alloc_buffer_size(alloc, buffer);
334 free_buffers++;
335 total_free_size += buffer_size;
336 if (buffer_size > largest_free_size)
337 largest_free_size = buffer_size;
338 }
339
340 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
341 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
342 total_alloc_size, allocated_buffers,
343 largest_alloc_size, total_free_size,
344 free_buffers, largest_free_size);
345}
346
Carlos Llamas26d06d92023-12-01 17:21:41 +0000347static bool debug_low_async_space_locked(struct binder_alloc *alloc)
Martijn Coenen261e7812020-08-21 14:25:44 +0200348{
349 /*
350 * Find the amount and size of buffers allocated by the current caller;
351 * The idea is that once we cross the threshold, whoever is responsible
352 * for the low async space is likely to try to send another async txn,
353 * and at some point we'll catch them in the act. This is more efficient
354 * than keeping a map per pid.
355 */
Martijn Coenen261e7812020-08-21 14:25:44 +0200356 struct binder_buffer *buffer;
357 size_t total_alloc_size = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000358 int pid = current->tgid;
Martijn Coenen261e7812020-08-21 14:25:44 +0200359 size_t num_buffers = 0;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000360 struct rb_node *n;
Martijn Coenen261e7812020-08-21 14:25:44 +0200361
Carlos Llamas081ddad2023-12-01 17:21:43 +0000362 /*
363 * Only start detecting spammers once we have less than 20% of async
364 * space left (which is less than 10% of total buffer size).
365 */
366 if (alloc->free_async_space >= alloc->buffer_size / 10) {
367 alloc->oneway_spam_detected = false;
368 return false;
369 }
370
Martijn Coenen261e7812020-08-21 14:25:44 +0200371 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
372 n = rb_next(n)) {
373 buffer = rb_entry(n, struct binder_buffer, rb_node);
374 if (buffer->pid != pid)
375 continue;
376 if (!buffer->async_transaction)
377 continue;
Carlos Llamas11ca0762023-12-01 17:21:34 +0000378 total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
Martijn Coenen261e7812020-08-21 14:25:44 +0200379 num_buffers++;
380 }
381
382 /*
383 * Warn if this pid has more than 50 transactions, or more than 50% of
Hang Lua7dc1e62021-04-09 17:40:46 +0800384 * async space (which is 25% of total buffer size). Oneway spam is only
385 * detected when the threshold is exceeded.
Martijn Coenen261e7812020-08-21 14:25:44 +0200386 */
387 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
388 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
389 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
390 alloc->pid, pid, num_buffers, total_alloc_size);
Hang Lua7dc1e62021-04-09 17:40:46 +0800391 if (!alloc->oneway_spam_detected) {
392 alloc->oneway_spam_detected = true;
393 return true;
394 }
Martijn Coenen261e7812020-08-21 14:25:44 +0200395 }
Hang Lua7dc1e62021-04-09 17:40:46 +0800396 return false;
Martijn Coenen261e7812020-08-21 14:25:44 +0200397}
398
Xiongwei Song3f827242017-12-14 12:15:42 +0800399static struct binder_buffer *binder_alloc_new_buf_locked(
400 struct binder_alloc *alloc,
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000401 size_t size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000402 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700403{
404 struct rb_node *n = alloc->free_buffers.rb_node;
405 struct binder_buffer *buffer;
406 size_t buffer_size;
407 struct rb_node *best_fit = NULL;
Carlos Llamasc38a8982023-12-01 17:21:38 +0000408 unsigned long has_page_addr;
409 unsigned long end_page_addr;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700410 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700411
Zhuguangqing1174e452021-03-09 15:47:43 +0800412 trace_android_vh_binder_alloc_new_buf_locked(size, &alloc->free_async_space, is_async);
Carlos Llamas65cf1582023-12-01 17:21:33 +0000413
Carlos Llamas11ca0762023-12-01 17:21:34 +0000414 if (is_async && alloc->free_async_space < size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700415 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
416 "%d: binder_alloc_buf size %zd failed, no async space left\n",
417 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700418 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700419 }
420
421 while (n) {
422 buffer = rb_entry(n, struct binder_buffer, rb_node);
423 BUG_ON(!buffer->free);
424 buffer_size = binder_alloc_buffer_size(alloc, buffer);
425
426 if (size < buffer_size) {
427 best_fit = n;
428 n = n->rb_left;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000429 } else if (size > buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700430 n = n->rb_right;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000431 } else {
Todd Kjos0c972a02017-06-29 12:01:41 -0700432 best_fit = n;
433 break;
434 }
435 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000436
Carlos Llamase1d195e2023-12-01 17:21:42 +0000437 if (unlikely(!best_fit)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700438 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
439 "%d: binder_alloc_buf size %zd failed, no address space\n",
440 alloc->pid, size);
Carlos Llamase1d195e2023-12-01 17:21:42 +0000441 debug_no_space_locked(alloc);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700442 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700443 }
Carlos Llamase1d195e2023-12-01 17:21:42 +0000444
Todd Kjos0c972a02017-06-29 12:01:41 -0700445 if (n == NULL) {
446 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
447 buffer_size = binder_alloc_buffer_size(alloc, buffer);
448 }
449
450 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
451 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
452 alloc->pid, size, buffer, buffer_size);
453
Carlos Llamasc38a8982023-12-01 17:21:38 +0000454 has_page_addr = (buffer->user_data + buffer_size) & PAGE_MASK;
Sherry Yang74310e02017-08-23 08:46:41 -0700455 WARN_ON(n && buffer_size != size);
Carlos Llamasc38a8982023-12-01 17:21:38 +0000456 end_page_addr = PAGE_ALIGN(buffer->user_data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700457 if (end_page_addr > has_page_addr)
458 end_page_addr = has_page_addr;
Carlos Llamas0b243682023-12-01 17:21:39 +0000459 ret = binder_allocate_page_range(alloc, PAGE_ALIGN(buffer->user_data),
460 end_page_addr);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700461 if (ret)
462 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700463
Todd Kjos0c972a02017-06-29 12:01:41 -0700464 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700465 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700466
Sherry Yang74310e02017-08-23 08:46:41 -0700467 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
468 if (!new_buffer) {
469 pr_err("%s: %d failed to alloc new buffer struct\n",
470 __func__, alloc->pid);
471 goto err_alloc_buf_struct_failed;
472 }
Carlos Llamasc38a8982023-12-01 17:21:38 +0000473 new_buffer->user_data = buffer->user_data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700474 list_add(&new_buffer->entry, &buffer->entry);
475 new_buffer->free = 1;
476 binder_insert_free_buffer(alloc, new_buffer);
477 }
Sherry Yang74310e02017-08-23 08:46:41 -0700478
479 rb_erase(best_fit, &alloc->free_buffers);
480 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800481 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700482 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700483 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
484 "%d: binder_alloc_buf size %zd got %pK\n",
485 alloc->pid, size, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700486 buffer->async_transaction = is_async;
Hang Lua7dc1e62021-04-09 17:40:46 +0800487 buffer->oneway_spam_suspect = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700488 if (is_async) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000489 alloc->free_async_space -= size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700490 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
491 "%d: binder_alloc_buf size %zd async free %zd\n",
492 alloc->pid, size, alloc->free_async_space);
Carlos Llamas081ddad2023-12-01 17:21:43 +0000493 if (debug_low_async_space_locked(alloc))
494 buffer->oneway_spam_suspect = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700495 }
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000496
Todd Kjos0c972a02017-06-29 12:01:41 -0700497 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700498
499err_alloc_buf_struct_failed:
Carlos Llamas0b243682023-12-01 17:21:39 +0000500 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
501 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700502 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700503}
504
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000505/* Calculate the sanitized total size, returns 0 for invalid request */
506static inline size_t sanitized_size(size_t data_size,
507 size_t offsets_size,
508 size_t extra_buffers_size)
509{
510 size_t total, tmp;
511
512 /* Align to pointer size and check for overflows */
513 tmp = ALIGN(data_size, sizeof(void *)) +
514 ALIGN(offsets_size, sizeof(void *));
515 if (tmp < data_size || tmp < offsets_size)
516 return 0;
517 total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
518 if (total < tmp || total < extra_buffers_size)
519 return 0;
520
521 /* Pad 0-sized buffers so they get a unique address */
522 total = max(total, sizeof(void *));
523
524 return total;
525}
526
Todd Kjos0c972a02017-06-29 12:01:41 -0700527/**
528 * binder_alloc_new_buf() - Allocate a new binder buffer
529 * @alloc: binder_alloc for this proc
530 * @data_size: size of user data buffer
531 * @offsets_size: user specified buffer offset
532 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
533 * @is_async: buffer for async transaction
534 *
535 * Allocate a new buffer given the requested sizes. Returns
536 * the kernel version of the buffer pointer. The size allocated
537 * is the sum of the three given sizes (each rounded up to
538 * pointer-sized boundary)
539 *
Carlos Llamas2a250a12023-12-01 17:21:36 +0000540 * Return: The allocated buffer or %ERR_PTR(-errno) if error
Todd Kjos0c972a02017-06-29 12:01:41 -0700541 */
542struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
543 size_t data_size,
544 size_t offsets_size,
545 size_t extra_buffers_size,
Carlos Llamas26d06d92023-12-01 17:21:41 +0000546 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700547{
548 struct binder_buffer *buffer;
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000549 size_t size;
550
551 /* Check binder_alloc is fully initialized */
552 if (!binder_alloc_get_vma(alloc)) {
553 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
554 "%d: binder_alloc_buf, no vma\n",
555 alloc->pid);
556 return ERR_PTR(-ESRCH);
557 }
558
559 size = sanitized_size(data_size, offsets_size, extra_buffers_size);
560 if (unlikely(!size)) {
561 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
562 "%d: got transaction with invalid size %zd-%zd-%zd\n",
563 alloc->pid, data_size, offsets_size,
564 extra_buffers_size);
565 return ERR_PTR(-EINVAL);
566 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700567
568 mutex_lock(&alloc->mutex);
Carlos Llamas26d06d92023-12-01 17:21:41 +0000569 buffer = binder_alloc_new_buf_locked(alloc, size, is_async);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000570 if (IS_ERR(buffer)) {
571 mutex_unlock(&alloc->mutex);
572 goto out;
573 }
574
575 buffer->data_size = data_size;
576 buffer->offsets_size = offsets_size;
577 buffer->extra_buffers_size = extra_buffers_size;
Carlos Llamas26d06d92023-12-01 17:21:41 +0000578 buffer->pid = current->tgid;
Todd Kjos0c972a02017-06-29 12:01:41 -0700579 mutex_unlock(&alloc->mutex);
Carlos Llamasd5c44f92023-12-01 17:21:40 +0000580
581out:
Todd Kjos0c972a02017-06-29 12:01:41 -0700582 return buffer;
583}
584
Carlos Llamasc38a8982023-12-01 17:21:38 +0000585static unsigned long buffer_start_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700586{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000587 return buffer->user_data & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700588}
589
Carlos Llamasc38a8982023-12-01 17:21:38 +0000590static unsigned long prev_buffer_end_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 - 1) & PAGE_MASK;
Todd Kjos0c972a02017-06-29 12:01:41 -0700593}
594
595static void binder_delete_free_buffer(struct binder_alloc *alloc,
596 struct binder_buffer *buffer)
597{
598 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700599 bool to_free = true;
Mrinal Pandey4df97722020-07-24 18:42:54 +0530600
Todd Kjos0c972a02017-06-29 12:01:41 -0700601 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700602 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700603 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700604 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
605 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700606 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000607 "%d: merge free, buffer %lx share page with %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800608 alloc->pid, buffer->user_data,
609 prev->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700610 }
611
612 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700613 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700614 if (buffer_start_page(next) == buffer_start_page(buffer)) {
615 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700616 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000617 "%d: merge free, buffer %lx share page with %lx\n",
Sherry Yang74310e02017-08-23 08:46:41 -0700618 alloc->pid,
Todd Kjosbde4a192019-02-08 10:35:20 -0800619 buffer->user_data,
620 next->user_data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700621 }
622 }
Sherry Yang74310e02017-08-23 08:46:41 -0700623
Todd Kjosbde4a192019-02-08 10:35:20 -0800624 if (PAGE_ALIGNED(buffer->user_data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700625 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000626 "%d: merge free, buffer start %lx is page aligned\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800627 alloc->pid, buffer->user_data);
Sherry Yang74310e02017-08-23 08:46:41 -0700628 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700629 }
Sherry Yang74310e02017-08-23 08:46:41 -0700630
631 if (to_free) {
632 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000633 "%d: merge free, buffer %lx do not share page with %lx or %lx\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800634 alloc->pid, buffer->user_data,
635 prev->user_data,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000636 next ? next->user_data : 0);
Carlos Llamas0b243682023-12-01 17:21:39 +0000637 binder_free_page_range(alloc, buffer_start_page(buffer),
638 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700639 }
640 list_del(&buffer->entry);
641 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700642}
643
644static void binder_free_buf_locked(struct binder_alloc *alloc,
645 struct binder_buffer *buffer)
646{
647 size_t size, buffer_size;
648
649 buffer_size = binder_alloc_buffer_size(alloc, buffer);
650
651 size = ALIGN(buffer->data_size, sizeof(void *)) +
652 ALIGN(buffer->offsets_size, sizeof(void *)) +
653 ALIGN(buffer->extra_buffers_size, sizeof(void *));
654
655 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
656 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
657 alloc->pid, buffer, size, buffer_size);
658
659 BUG_ON(buffer->free);
660 BUG_ON(size > buffer_size);
661 BUG_ON(buffer->transaction != NULL);
Todd Kjosbde4a192019-02-08 10:35:20 -0800662 BUG_ON(buffer->user_data < alloc->buffer);
663 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700664
665 if (buffer->async_transaction) {
Carlos Llamas11ca0762023-12-01 17:21:34 +0000666 alloc->free_async_space += buffer_size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700667 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
668 "%d: binder_free_buf size %zd async free %zd\n",
669 alloc->pid, size, alloc->free_async_space);
670 }
671
Carlos Llamas0b243682023-12-01 17:21:39 +0000672 binder_free_page_range(alloc, PAGE_ALIGN(buffer->user_data),
673 (buffer->user_data + buffer_size) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700674
675 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
676 buffer->free = 1;
677 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700678 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700679
680 if (next->free) {
681 rb_erase(&next->rb_node, &alloc->free_buffers);
682 binder_delete_free_buffer(alloc, next);
683 }
684 }
685 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700686 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700687
688 if (prev->free) {
689 binder_delete_free_buffer(alloc, buffer);
690 rb_erase(&prev->rb_node, &alloc->free_buffers);
691 buffer = prev;
692 }
693 }
694 binder_insert_free_buffer(alloc, buffer);
695}
696
Carlos Llamas59e0d622023-12-01 17:21:44 +0000697/**
698 * binder_alloc_get_page() - get kernel pointer for given buffer offset
699 * @alloc: binder_alloc for this proc
700 * @buffer: binder buffer to be accessed
701 * @buffer_offset: offset into @buffer data
702 * @pgoffp: address to copy final page offset to
703 *
704 * Lookup the struct page corresponding to the address
705 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
706 * NULL, the byte-offset into the page is written there.
707 *
708 * The caller is responsible to ensure that the offset points
709 * to a valid address within the @buffer and that @buffer is
710 * not freeable by the user. Since it can't be freed, we are
711 * guaranteed that the corresponding elements of @alloc->pages[]
712 * cannot change.
713 *
714 * Return: struct page
715 */
716static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
717 struct binder_buffer *buffer,
718 binder_size_t buffer_offset,
719 pgoff_t *pgoffp)
720{
721 binder_size_t buffer_space_offset = buffer_offset +
722 (buffer->user_data - alloc->buffer);
723 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
724 size_t index = buffer_space_offset >> PAGE_SHIFT;
725 struct binder_lru_page *lru_page;
726
727 lru_page = &alloc->pages[index];
728 *pgoffp = pgoff;
729 return lru_page->page_ptr;
730}
731
732/**
733 * binder_alloc_clear_buf() - zero out buffer
734 * @alloc: binder_alloc for this proc
735 * @buffer: binder buffer to be cleared
736 *
737 * memset the given buffer to 0
738 */
Todd Kjos0f966cb2020-11-20 15:37:43 -0800739static void binder_alloc_clear_buf(struct binder_alloc *alloc,
Carlos Llamas59e0d622023-12-01 17:21:44 +0000740 struct binder_buffer *buffer)
741{
742 size_t bytes = binder_alloc_buffer_size(alloc, buffer);
743 binder_size_t buffer_offset = 0;
744
745 while (bytes) {
746 unsigned long size;
747 struct page *page;
748 pgoff_t pgoff;
749 void *kptr;
750
751 page = binder_alloc_get_page(alloc, buffer,
752 buffer_offset, &pgoff);
753 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
754 kptr = kmap(page) + pgoff;
755 memset(kptr, 0, size);
756 kunmap(page);
757 bytes -= size;
758 buffer_offset += size;
759 }
760}
761
762
Todd Kjos0c972a02017-06-29 12:01:41 -0700763/**
764 * binder_alloc_free_buf() - free a binder buffer
765 * @alloc: binder_alloc for this proc
766 * @buffer: kernel pointer to buffer
767 *
YangHui4b463822020-08-18 09:34:04 +0800768 * Free the buffer allocated via binder_alloc_new_buf()
Todd Kjos0c972a02017-06-29 12:01:41 -0700769 */
770void binder_alloc_free_buf(struct binder_alloc *alloc,
771 struct binder_buffer *buffer)
772{
Todd Kjos0f966cb2020-11-20 15:37:43 -0800773 /*
774 * We could eliminate the call to binder_alloc_clear_buf()
775 * from binder_alloc_deferred_release() by moving this to
Carlos Llamas26f0c012023-12-01 17:21:35 +0000776 * binder_free_buf_locked(). However, that could
Todd Kjos0f966cb2020-11-20 15:37:43 -0800777 * increase contention for the alloc mutex if clear_on_free
778 * is used frequently for large buffers. The mutex is not
779 * needed for correctness here.
780 */
781 if (buffer->clear_on_free) {
782 binder_alloc_clear_buf(alloc, buffer);
783 buffer->clear_on_free = false;
784 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700785 mutex_lock(&alloc->mutex);
786 binder_free_buf_locked(alloc, buffer);
787 mutex_unlock(&alloc->mutex);
788}
789
790/**
791 * binder_alloc_mmap_handler() - map virtual address space for proc
792 * @alloc: alloc structure for this proc
793 * @vma: vma passed to mmap()
794 *
795 * Called by binder_mmap() to initialize the space specified in
796 * vma for allocating binder buffers
797 *
798 * Return:
799 * 0 = success
800 * -EBUSY = address space already mapped
801 * -ENOMEM = failed to map memory to given address space
802 */
803int binder_alloc_mmap_handler(struct binder_alloc *alloc,
804 struct vm_area_struct *vma)
805{
806 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700807 const char *failure_string;
808 struct binder_buffer *buffer;
809
Carlos Llamasd276fb42022-11-04 23:12:35 +0000810 if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
811 ret = -EINVAL;
812 failure_string = "invalid vma->vm_mm";
813 goto err_invalid_mm;
814 }
815
Todd Kjos0c972a02017-06-29 12:01:41 -0700816 mutex_lock(&binder_alloc_mmap_lock);
Jann Horna7a74d72019-10-18 22:56:30 +0200817 if (alloc->buffer_size) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700818 ret = -EBUSY;
819 failure_string = "already mapped";
820 goto err_already_mapped;
821 }
Jann Horn45d02f72019-10-16 17:01:18 +0200822 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
823 SZ_4M);
Jann Horna7a74d72019-10-18 22:56:30 +0200824 mutex_unlock(&binder_alloc_mmap_lock);
825
Carlos Llamasc38a8982023-12-01 17:21:38 +0000826 alloc->buffer = vma->vm_start;
Jann Horna7a74d72019-10-18 22:56:30 +0200827
Jann Horn45d02f72019-10-16 17:01:18 +0200828 alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
Kees Cook6396bb22018-06-12 14:03:40 -0700829 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700830 GFP_KERNEL);
831 if (alloc->pages == NULL) {
832 ret = -ENOMEM;
833 failure_string = "alloc page array";
834 goto err_alloc_pages_failed;
835 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700836
Sherry Yang74310e02017-08-23 08:46:41 -0700837 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
838 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700839 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700840 failure_string = "alloc buffer struct";
841 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700842 }
Sherry Yang74310e02017-08-23 08:46:41 -0700843
Todd Kjosbde4a192019-02-08 10:35:20 -0800844 buffer->user_data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700845 list_add(&buffer->entry, &alloc->buffers);
846 buffer->free = 1;
847 binder_insert_free_buffer(alloc, buffer);
848 alloc->free_async_space = alloc->buffer_size / 2;
Carlos Llamasb094b042023-05-30 19:43:37 +0000849
850 /* Signal binder_alloc is fully initialized */
Minchan Kimda1b9562018-08-23 14:29:56 +0900851 binder_alloc_set_vma(alloc, vma);
Todd Kjos0c972a02017-06-29 12:01:41 -0700852
853 return 0;
854
Sherry Yang74310e02017-08-23 08:46:41 -0700855err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700856 kfree(alloc->pages);
857 alloc->pages = NULL;
858err_alloc_pages_failed:
Carlos Llamasc38a8982023-12-01 17:21:38 +0000859 alloc->buffer = 0;
Jann Horna7a74d72019-10-18 22:56:30 +0200860 mutex_lock(&binder_alloc_mmap_lock);
861 alloc->buffer_size = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700862err_already_mapped:
863 mutex_unlock(&binder_alloc_mmap_lock);
Carlos Llamasd276fb42022-11-04 23:12:35 +0000864err_invalid_mm:
Sherry Yang128f3802018-08-07 12:57:13 -0700865 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
866 "%s: %d %lx-%lx %s failed %d\n", __func__,
867 alloc->pid, vma->vm_start, vma->vm_end,
868 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700869 return ret;
870}
871
872
873void binder_alloc_deferred_release(struct binder_alloc *alloc)
874{
875 struct rb_node *n;
876 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700877 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700878
Todd Kjos0c972a02017-06-29 12:01:41 -0700879 buffers = 0;
880 mutex_lock(&alloc->mutex);
Carlos Llamasacd81932023-05-30 19:43:36 +0000881 BUG_ON(alloc->vma);
Minchan Kimda1b9562018-08-23 14:29:56 +0900882
Todd Kjos0c972a02017-06-29 12:01:41 -0700883 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700884 buffer = rb_entry(n, struct binder_buffer, rb_node);
885
886 /* Transaction should already have been freed */
887 BUG_ON(buffer->transaction);
888
Todd Kjos0f966cb2020-11-20 15:37:43 -0800889 if (buffer->clear_on_free) {
890 binder_alloc_clear_buf(alloc, buffer);
891 buffer->clear_on_free = false;
892 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700893 binder_free_buf_locked(alloc, buffer);
894 buffers++;
895 }
896
Sherry Yang74310e02017-08-23 08:46:41 -0700897 while (!list_empty(&alloc->buffers)) {
898 buffer = list_first_entry(&alloc->buffers,
899 struct binder_buffer, entry);
900 WARN_ON(!buffer->free);
901
902 list_del(&buffer->entry);
903 WARN_ON_ONCE(!list_empty(&alloc->buffers));
904 kfree(buffer);
905 }
906
Todd Kjos0c972a02017-06-29 12:01:41 -0700907 page_count = 0;
908 if (alloc->pages) {
909 int i;
910
911 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
Carlos Llamasc38a8982023-12-01 17:21:38 +0000912 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700913 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700914
Sherry Yangf2517eb2017-08-23 08:46:42 -0700915 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700916 continue;
917
Sherry Yangf2517eb2017-08-23 08:46:42 -0700918 on_lru = list_lru_del(&binder_alloc_lru,
919 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700920 page_addr = alloc->buffer + i * PAGE_SIZE;
921 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Carlos Llamasc38a8982023-12-01 17:21:38 +0000922 "%s: %d: page %d at %lx %s\n",
Sherry Yangf2517eb2017-08-23 08:46:42 -0700923 __func__, alloc->pid, i, page_addr,
924 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700925 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700926 page_count++;
927 }
928 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700929 }
930 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400931 if (alloc->vma_vm_mm)
932 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700933
934 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
935 "%s: %d buffers %d, pages %d\n",
936 __func__, alloc->pid, buffers, page_count);
937}
938
939static void print_binder_buffer(struct seq_file *m, const char *prefix,
940 struct binder_buffer *buffer)
941{
Carlos Llamasc38a8982023-12-01 17:21:38 +0000942 seq_printf(m, "%s %d: %lx size %zd:%zd:%zd %s\n",
Todd Kjosbde4a192019-02-08 10:35:20 -0800943 prefix, buffer->debug_id, buffer->user_data,
Todd Kjos0c972a02017-06-29 12:01:41 -0700944 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700945 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700946 buffer->transaction ? "active" : "delivered");
947}
948
949/**
950 * binder_alloc_print_allocated() - print buffer info
951 * @m: seq_file for output via seq_printf()
952 * @alloc: binder_alloc for this proc
953 *
954 * Prints information about every buffer associated with
955 * the binder_alloc state to the given seq_file
956 */
957void binder_alloc_print_allocated(struct seq_file *m,
958 struct binder_alloc *alloc)
959{
960 struct rb_node *n;
961
962 mutex_lock(&alloc->mutex);
963 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
964 print_binder_buffer(m, " buffer",
965 rb_entry(n, struct binder_buffer, rb_node));
966 mutex_unlock(&alloc->mutex);
967}
968
969/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700970 * binder_alloc_print_pages() - print page usage
971 * @m: seq_file for output via seq_printf()
972 * @alloc: binder_alloc for this proc
973 */
974void binder_alloc_print_pages(struct seq_file *m,
975 struct binder_alloc *alloc)
976{
977 struct binder_lru_page *page;
978 int i;
979 int active = 0;
980 int lru = 0;
981 int free = 0;
982
983 mutex_lock(&alloc->mutex);
Jann Horn8eb52a12019-10-18 22:56:29 +0200984 /*
985 * Make sure the binder_alloc is fully initialized, otherwise we might
986 * read inconsistent state.
987 */
Carlos Llamas45efb0a2023-05-30 19:43:35 +0000988 if (binder_alloc_get_vma(alloc) != NULL) {
989 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
990 page = &alloc->pages[i];
991 if (!page->page_ptr)
992 free++;
993 else if (list_empty(&page->lru))
994 active++;
995 else
996 lru++;
997 }
Sherry Yang8ef46652017-08-31 11:56:36 -0700998 }
999 mutex_unlock(&alloc->mutex);
1000 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +01001001 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -07001002}
1003
1004/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001005 * binder_alloc_get_allocated_count() - return count of buffers
1006 * @alloc: binder_alloc for this proc
1007 *
1008 * Return: count of allocated buffers
1009 */
1010int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
1011{
1012 struct rb_node *n;
1013 int count = 0;
1014
1015 mutex_lock(&alloc->mutex);
1016 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
1017 count++;
1018 mutex_unlock(&alloc->mutex);
1019 return count;
1020}
1021
1022
1023/**
1024 * binder_alloc_vma_close() - invalidate address space
1025 * @alloc: binder_alloc for this proc
1026 *
1027 * Called from binder_vma_close() when releasing address space.
1028 * Clears alloc->vma to prevent new incoming transactions from
1029 * allocating more buffers.
1030 */
1031void binder_alloc_vma_close(struct binder_alloc *alloc)
1032{
Minchan Kimda1b9562018-08-23 14:29:56 +09001033 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -07001034}
1035
1036/**
Sherry Yangf2517eb2017-08-23 08:46:42 -07001037 * binder_alloc_free_page() - shrinker callback to free pages
1038 * @item: item to free
1039 * @lock: lock protecting the item
1040 * @cb_arg: callback argument
1041 *
1042 * Called from list_lru_walk() in binder_shrink_scan() to free
1043 * up pages when the system is under memory pressure.
1044 */
1045enum lru_status binder_alloc_free_page(struct list_head *item,
1046 struct list_lru_one *lru,
1047 spinlock_t *lock,
1048 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -08001049 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001050{
1051 struct mm_struct *mm = NULL;
1052 struct binder_lru_page *page = container_of(item,
1053 struct binder_lru_page,
1054 lru);
1055 struct binder_alloc *alloc;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001056 unsigned long page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001057 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -07001058 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001059
1060 alloc = page->alloc;
1061 if (!mutex_trylock(&alloc->mutex))
1062 goto err_get_alloc_mutex_failed;
1063
1064 if (!page->page_ptr)
1065 goto err_page_already_freed;
1066
1067 index = page - alloc->pages;
Carlos Llamasc38a8982023-12-01 17:21:38 +00001068 page_addr = alloc->buffer + index * PAGE_SIZE;
Todd Kjos5cec2d22019-03-01 15:06:06 -08001069
1070 mm = alloc->vma_vm_mm;
1071 if (!mmget_not_zero(mm))
1072 goto err_mmget;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001073 if (!mmap_read_trylock(mm))
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001074 goto err_mmap_read_lock_failed;
Carlos Llamas8dce2882023-12-01 17:21:31 +00001075 vma = vma_lookup(mm, page_addr);
1076 if (vma && vma != binder_alloc_get_vma(alloc))
1077 goto err_invalid_vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001078
Sherry Yanga1b22892017-10-03 16:15:00 -07001079 list_lru_isolate(lru, item);
1080 spin_unlock(lock);
1081
1082 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -07001083 trace_binder_unmap_user_start(alloc, index);
1084
Todd Kjosc41358a2019-02-08 10:35:19 -08001085 zap_page_range(vma, page_addr, PAGE_SIZE);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001086
Sherry Yange41e1642017-08-23 08:46:43 -07001087 trace_binder_unmap_user_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001088 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001089 mmap_read_unlock(mm);
Tetsuo Handaf867c772020-07-17 00:12:15 +09001090 mmput_async(mm);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001091
Sherry Yange41e1642017-08-23 08:46:43 -07001092 trace_binder_unmap_kernel_start(alloc, index);
1093
Sherry Yangf2517eb2017-08-23 08:46:42 -07001094 __free_page(page->page_ptr);
1095 page->page_ptr = NULL;
1096
Sherry Yange41e1642017-08-23 08:46:43 -07001097 trace_binder_unmap_kernel_end(alloc, index);
1098
Sherry Yanga1b22892017-10-03 16:15:00 -07001099 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -07001100 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -07001101 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001102
Carlos Llamas8dce2882023-12-01 17:21:31 +00001103err_invalid_vma:
1104 mmap_read_unlock(mm);
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001105err_mmap_read_lock_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -07001106 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -04001107err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -07001108err_page_already_freed:
1109 mutex_unlock(&alloc->mutex);
1110err_get_alloc_mutex_failed:
1111 return LRU_SKIP;
1112}
1113
1114static unsigned long
1115binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1116{
1117 unsigned long ret = list_lru_count(&binder_alloc_lru);
1118 return ret;
1119}
1120
1121static unsigned long
1122binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1123{
1124 unsigned long ret;
1125
1126 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
1127 NULL, sc->nr_to_scan);
1128 return ret;
1129}
1130
Sherry Yangde7bbe32017-10-06 16:12:05 -04001131static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -07001132 .count_objects = binder_shrink_count,
1133 .scan_objects = binder_shrink_scan,
1134 .seeks = DEFAULT_SEEKS,
1135};
1136
1137/**
Todd Kjos0c972a02017-06-29 12:01:41 -07001138 * binder_alloc_init() - called by binder_open() for per-proc initialization
1139 * @alloc: binder_alloc for this proc
1140 *
1141 * Called from binder_open() to initialize binder_alloc fields for
1142 * new binder proc
1143 */
1144void binder_alloc_init(struct binder_alloc *alloc)
1145{
Todd Kjos0c972a02017-06-29 12:01:41 -07001146 alloc->pid = current->group_leader->pid;
Carlos Llamas81203ab2022-08-29 20:12:48 +00001147 alloc->vma_vm_mm = current->mm;
1148 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -07001149 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001150 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001151}
1152
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001153int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001154{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001155 int ret = list_lru_init(&binder_alloc_lru);
1156
1157 if (ret == 0) {
1158 ret = register_shrinker(&binder_shrinker);
1159 if (ret)
1160 list_lru_destroy(&binder_alloc_lru);
1161 }
1162 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001163}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001164
Qi Zheng03eebad2023-06-25 15:49:37 +00001165void binder_alloc_shrinker_exit(void)
1166{
1167 unregister_shrinker(&binder_shrinker);
1168 list_lru_destroy(&binder_alloc_lru);
1169}
1170
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001171/**
1172 * check_buffer() - verify that buffer/offset is safe to access
1173 * @alloc: binder_alloc for this proc
1174 * @buffer: binder buffer to be accessed
1175 * @offset: offset into @buffer data
1176 * @bytes: bytes to access from offset
1177 *
1178 * Check that the @offset/@bytes are within the size of the given
1179 * @buffer and that the buffer is currently active and not freeable.
1180 * Offsets must also be multiples of sizeof(u32). The kernel is
1181 * allowed to touch the buffer in two cases:
1182 *
1183 * 1) when the buffer is being created:
1184 * (buffer->free == 0 && buffer->allow_user_free == 0)
1185 * 2) when the buffer is being torn down:
1186 * (buffer->free == 0 && buffer->transaction == NULL).
1187 *
1188 * Return: true if the buffer is safe to access
1189 */
1190static inline bool check_buffer(struct binder_alloc *alloc,
1191 struct binder_buffer *buffer,
1192 binder_size_t offset, size_t bytes)
1193{
1194 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1195
1196 return buffer_size >= bytes &&
1197 offset <= buffer_size - bytes &&
1198 IS_ALIGNED(offset, sizeof(u32)) &&
1199 !buffer->free &&
1200 (!buffer->allow_user_free || !buffer->transaction);
1201}
1202
1203/**
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001204 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1205 * @alloc: binder_alloc for this proc
1206 * @buffer: binder buffer to be accessed
1207 * @buffer_offset: offset into @buffer data
1208 * @from: userspace pointer to source buffer
1209 * @bytes: bytes to copy
1210 *
1211 * Copy bytes from source userspace to target buffer.
1212 *
1213 * Return: bytes remaining to be copied
1214 */
1215unsigned long
1216binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1217 struct binder_buffer *buffer,
1218 binder_size_t buffer_offset,
1219 const void __user *from,
1220 size_t bytes)
1221{
1222 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1223 return bytes;
1224
1225 while (bytes) {
1226 unsigned long size;
1227 unsigned long ret;
1228 struct page *page;
1229 pgoff_t pgoff;
1230 void *kptr;
1231
1232 page = binder_alloc_get_page(alloc, buffer,
1233 buffer_offset, &pgoff);
1234 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1235 kptr = kmap(page) + pgoff;
1236 ret = copy_from_user(kptr, from, size);
1237 kunmap(page);
1238 if (ret)
1239 return bytes - size + ret;
1240 bytes -= size;
1241 from += size;
1242 buffer_offset += size;
1243 }
1244 return 0;
1245}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001246
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001247static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1248 bool to_buffer,
1249 struct binder_buffer *buffer,
1250 binder_size_t buffer_offset,
1251 void *ptr,
1252 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001253{
1254 /* All copies must be 32-bit aligned and 32-bit size */
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001255 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1256 return -EINVAL;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001257
1258 while (bytes) {
1259 unsigned long size;
1260 struct page *page;
1261 pgoff_t pgoff;
1262 void *tmpptr;
1263 void *base_ptr;
1264
1265 page = binder_alloc_get_page(alloc, buffer,
1266 buffer_offset, &pgoff);
1267 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1268 base_ptr = kmap_atomic(page);
1269 tmpptr = base_ptr + pgoff;
1270 if (to_buffer)
1271 memcpy(tmpptr, ptr, size);
1272 else
1273 memcpy(ptr, tmpptr, size);
1274 /*
1275 * kunmap_atomic() takes care of flushing the cache
1276 * if this device has VIVT cache arch
1277 */
1278 kunmap_atomic(base_ptr);
1279 bytes -= size;
1280 pgoff = 0;
1281 ptr = ptr + size;
1282 buffer_offset += size;
1283 }
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001284 return 0;
Todd Kjos8ced0c62019-02-08 10:35:15 -08001285}
1286
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001287int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1288 struct binder_buffer *buffer,
1289 binder_size_t buffer_offset,
1290 void *src,
1291 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001292{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001293 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1294 src, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001295}
1296
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001297int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1298 void *dest,
1299 struct binder_buffer *buffer,
1300 binder_size_t buffer_offset,
1301 size_t bytes)
Todd Kjos8ced0c62019-02-08 10:35:15 -08001302{
Todd Kjosbb4a2e482019-06-28 09:50:12 -07001303 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1304 dest, bytes);
Todd Kjos8ced0c62019-02-08 10:35:15 -08001305}
1306