blob: d4cbe4b3947a60a12794324577723eaf573296bb [file] [log] [blame]
Todd Kjos0c972a02017-06-29 12:01:41 -07001/* binder_alloc.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2017 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Todd Kjos0c972a02017-06-29 12:01:41 -070020#include <linux/list.h>
21#include <linux/sched/mm.h>
22#include <linux/module.h>
23#include <linux/rtmutex.h>
24#include <linux/rbtree.h>
25#include <linux/seq_file.h>
26#include <linux/vmalloc.h>
27#include <linux/slab.h>
28#include <linux/sched.h>
Sherry Yangf2517eb2017-08-23 08:46:42 -070029#include <linux/list_lru.h>
Sherry Yang128f3802018-08-07 12:57:13 -070030#include <linux/ratelimit.h>
Guenter Roeck1e81c572018-07-23 14:47:23 -070031#include <asm/cacheflush.h>
Todd Kjos1a7c3d92019-02-08 10:35:14 -080032#include <linux/uaccess.h>
33#include <linux/highmem.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070034#include "binder_alloc.h"
35#include "binder_trace.h"
36
Sherry Yangf2517eb2017-08-23 08:46:42 -070037struct list_lru binder_alloc_lru;
38
Todd Kjos0c972a02017-06-29 12:01:41 -070039static DEFINE_MUTEX(binder_alloc_mmap_lock);
40
41enum {
Sherry Yang128f3802018-08-07 12:57:13 -070042 BINDER_DEBUG_USER_ERROR = 1U << 0,
Todd Kjos0c972a02017-06-29 12:01:41 -070043 BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
44 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
45 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
46};
Sherry Yang128f3802018-08-07 12:57:13 -070047static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
Todd Kjos0c972a02017-06-29 12:01:41 -070048
49module_param_named(debug_mask, binder_alloc_debug_mask,
50 uint, 0644);
51
52#define binder_alloc_debug(mask, x...) \
53 do { \
54 if (binder_alloc_debug_mask & mask) \
Sherry Yang128f3802018-08-07 12:57:13 -070055 pr_info_ratelimited(x); \
Todd Kjos0c972a02017-06-29 12:01:41 -070056 } while (0)
57
Sherry Yange21762192017-08-23 08:46:39 -070058static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
59{
60 return list_entry(buffer->entry.next, struct binder_buffer, entry);
61}
62
63static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
64{
65 return list_entry(buffer->entry.prev, struct binder_buffer, entry);
66}
67
Todd Kjos0c972a02017-06-29 12:01:41 -070068static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
69 struct binder_buffer *buffer)
70{
71 if (list_is_last(&buffer->entry, &alloc->buffers))
Sherry Yang74310e02017-08-23 08:46:41 -070072 return (u8 *)alloc->buffer +
73 alloc->buffer_size - (u8 *)buffer->data;
74 return (u8 *)binder_buffer_next(buffer)->data - (u8 *)buffer->data;
Todd Kjos0c972a02017-06-29 12:01:41 -070075}
76
77static void binder_insert_free_buffer(struct binder_alloc *alloc,
78 struct binder_buffer *new_buffer)
79{
80 struct rb_node **p = &alloc->free_buffers.rb_node;
81 struct rb_node *parent = NULL;
82 struct binder_buffer *buffer;
83 size_t buffer_size;
84 size_t new_buffer_size;
85
86 BUG_ON(!new_buffer->free);
87
88 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
89
90 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
91 "%d: add free buffer, size %zd, at %pK\n",
92 alloc->pid, new_buffer_size, new_buffer);
93
94 while (*p) {
95 parent = *p;
96 buffer = rb_entry(parent, struct binder_buffer, rb_node);
97 BUG_ON(!buffer->free);
98
99 buffer_size = binder_alloc_buffer_size(alloc, buffer);
100
101 if (new_buffer_size < buffer_size)
102 p = &parent->rb_left;
103 else
104 p = &parent->rb_right;
105 }
106 rb_link_node(&new_buffer->rb_node, parent, p);
107 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
108}
109
110static void binder_insert_allocated_buffer_locked(
111 struct binder_alloc *alloc, struct binder_buffer *new_buffer)
112{
113 struct rb_node **p = &alloc->allocated_buffers.rb_node;
114 struct rb_node *parent = NULL;
115 struct binder_buffer *buffer;
116
117 BUG_ON(new_buffer->free);
118
119 while (*p) {
120 parent = *p;
121 buffer = rb_entry(parent, struct binder_buffer, rb_node);
122 BUG_ON(buffer->free);
123
Sherry Yang74310e02017-08-23 08:46:41 -0700124 if (new_buffer->data < buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700125 p = &parent->rb_left;
Sherry Yang74310e02017-08-23 08:46:41 -0700126 else if (new_buffer->data > buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700127 p = &parent->rb_right;
128 else
129 BUG();
130 }
131 rb_link_node(&new_buffer->rb_node, parent, p);
132 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
133}
134
Todd Kjos53d311cf2017-06-29 12:01:51 -0700135static struct binder_buffer *binder_alloc_prepare_to_free_locked(
Todd Kjos0c972a02017-06-29 12:01:41 -0700136 struct binder_alloc *alloc,
137 uintptr_t user_ptr)
138{
139 struct rb_node *n = alloc->allocated_buffers.rb_node;
140 struct binder_buffer *buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700141 void *kern_ptr;
Todd Kjos0c972a02017-06-29 12:01:41 -0700142
Sherry Yang74310e02017-08-23 08:46:41 -0700143 kern_ptr = (void *)(user_ptr - alloc->user_buffer_offset);
Todd Kjos0c972a02017-06-29 12:01:41 -0700144
145 while (n) {
146 buffer = rb_entry(n, struct binder_buffer, rb_node);
147 BUG_ON(buffer->free);
148
Sherry Yang74310e02017-08-23 08:46:41 -0700149 if (kern_ptr < buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700150 n = n->rb_left;
Sherry Yang74310e02017-08-23 08:46:41 -0700151 else if (kern_ptr > buffer->data)
Todd Kjos0c972a02017-06-29 12:01:41 -0700152 n = n->rb_right;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700153 else {
154 /*
155 * Guard against user threads attempting to
Todd Kjos7bada552018-11-06 15:55:32 -0800156 * free the buffer when in use by kernel or
157 * after it's already been freed.
Todd Kjos53d311cf2017-06-29 12:01:51 -0700158 */
Todd Kjos7bada552018-11-06 15:55:32 -0800159 if (!buffer->allow_user_free)
160 return ERR_PTR(-EPERM);
161 buffer->allow_user_free = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700162 return buffer;
Todd Kjos53d311cf2017-06-29 12:01:51 -0700163 }
Todd Kjos0c972a02017-06-29 12:01:41 -0700164 }
165 return NULL;
166}
167
168/**
169 * binder_alloc_buffer_lookup() - get buffer given user ptr
170 * @alloc: binder_alloc for this proc
171 * @user_ptr: User pointer to buffer data
172 *
173 * Validate userspace pointer to buffer data and return buffer corresponding to
174 * that user pointer. Search the rb tree for buffer that matches user data
175 * pointer.
176 *
177 * Return: Pointer to buffer or NULL
178 */
Todd Kjos53d311cf2017-06-29 12:01:51 -0700179struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
180 uintptr_t user_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700181{
182 struct binder_buffer *buffer;
183
184 mutex_lock(&alloc->mutex);
Todd Kjos53d311cf2017-06-29 12:01:51 -0700185 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700186 mutex_unlock(&alloc->mutex);
187 return buffer;
188}
189
190static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
Sherry Yang6ae33b92017-09-16 01:11:56 -0400191 void *start, void *end)
Todd Kjos0c972a02017-06-29 12:01:41 -0700192{
193 void *page_addr;
194 unsigned long user_page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700195 struct binder_lru_page *page;
Sherry Yang6ae33b92017-09-16 01:11:56 -0400196 struct vm_area_struct *vma = NULL;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700197 struct mm_struct *mm = NULL;
198 bool need_mm = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700199
200 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
201 "%d: %s pages %pK-%pK\n", alloc->pid,
202 allocate ? "allocate" : "free", start, end);
203
204 if (end <= start)
205 return 0;
206
207 trace_binder_update_page_range(alloc, allocate, start, end);
208
Sherry Yangf2517eb2017-08-23 08:46:42 -0700209 if (allocate == 0)
210 goto free_range;
211
212 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
213 page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
214 if (!page->page_ptr) {
215 need_mm = true;
216 break;
217 }
218 }
219
Greg Kroah-Hartman6fbf2482017-10-23 17:21:44 +0200220 if (need_mm && mmget_not_zero(alloc->vma_vm_mm))
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400221 mm = alloc->vma_vm_mm;
Todd Kjos0c972a02017-06-29 12:01:41 -0700222
223 if (mm) {
Minchan Kim720c24192018-05-07 23:15:37 +0900224 down_read(&mm->mmap_sem);
Todd Kjos0c972a02017-06-29 12:01:41 -0700225 vma = alloc->vma;
Todd Kjos0c972a02017-06-29 12:01:41 -0700226 }
227
Sherry Yangf2517eb2017-08-23 08:46:42 -0700228 if (!vma && need_mm) {
Sherry Yang128f3802018-08-07 12:57:13 -0700229 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
230 "%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
231 alloc->pid);
Todd Kjos0c972a02017-06-29 12:01:41 -0700232 goto err_no_vma;
233 }
234
235 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
236 int ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700237 bool on_lru;
Sherry Yange41e1642017-08-23 08:46:43 -0700238 size_t index;
Todd Kjos0c972a02017-06-29 12:01:41 -0700239
Sherry Yange41e1642017-08-23 08:46:43 -0700240 index = (page_addr - alloc->buffer) / PAGE_SIZE;
241 page = &alloc->pages[index];
Todd Kjos0c972a02017-06-29 12:01:41 -0700242
Sherry Yangf2517eb2017-08-23 08:46:42 -0700243 if (page->page_ptr) {
Sherry Yange41e1642017-08-23 08:46:43 -0700244 trace_binder_alloc_lru_start(alloc, index);
245
Sherry Yangf2517eb2017-08-23 08:46:42 -0700246 on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
247 WARN_ON(!on_lru);
Sherry Yange41e1642017-08-23 08:46:43 -0700248
249 trace_binder_alloc_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700250 continue;
251 }
252
253 if (WARN_ON(!vma))
254 goto err_page_ptr_cleared;
255
Sherry Yange41e1642017-08-23 08:46:43 -0700256 trace_binder_alloc_page_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700257 page->page_ptr = alloc_page(GFP_KERNEL |
258 __GFP_HIGHMEM |
259 __GFP_ZERO);
260 if (!page->page_ptr) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700261 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
262 alloc->pid, page_addr);
263 goto err_alloc_page_failed;
264 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700265 page->alloc = alloc;
266 INIT_LIST_HEAD(&page->lru);
267
Todd Kjos0c972a02017-06-29 12:01:41 -0700268 user_page_addr =
269 (uintptr_t)page_addr + alloc->user_buffer_offset;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700270 ret = vm_insert_page(vma, user_page_addr, page[0].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700271 if (ret) {
272 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
273 alloc->pid, user_page_addr);
274 goto err_vm_insert_page_failed;
275 }
Sherry Yange41e1642017-08-23 08:46:43 -0700276
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100277 if (index + 1 > alloc->pages_high)
278 alloc->pages_high = index + 1;
279
Sherry Yange41e1642017-08-23 08:46:43 -0700280 trace_binder_alloc_page_end(alloc, index);
Todd Kjos0c972a02017-06-29 12:01:41 -0700281 /* vm_insert_page does not seem to increment the refcount */
282 }
283 if (mm) {
Minchan Kim720c24192018-05-07 23:15:37 +0900284 up_read(&mm->mmap_sem);
Todd Kjos0c972a02017-06-29 12:01:41 -0700285 mmput(mm);
286 }
287 return 0;
288
289free_range:
290 for (page_addr = end - PAGE_SIZE; page_addr >= start;
291 page_addr -= PAGE_SIZE) {
Sherry Yangf2517eb2017-08-23 08:46:42 -0700292 bool ret;
Sherry Yange41e1642017-08-23 08:46:43 -0700293 size_t index;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700294
Sherry Yange41e1642017-08-23 08:46:43 -0700295 index = (page_addr - alloc->buffer) / PAGE_SIZE;
296 page = &alloc->pages[index];
297
298 trace_binder_free_lru_start(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700299
300 ret = list_lru_add(&binder_alloc_lru, &page->lru);
301 WARN_ON(!ret);
Sherry Yange41e1642017-08-23 08:46:43 -0700302
303 trace_binder_free_lru_end(alloc, index);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700304 continue;
305
Todd Kjos0c972a02017-06-29 12:01:41 -0700306err_vm_insert_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700307 __free_page(page->page_ptr);
308 page->page_ptr = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700309err_alloc_page_failed:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700310err_page_ptr_cleared:
Todd Kjos0c972a02017-06-29 12:01:41 -0700311 ;
312 }
313err_no_vma:
314 if (mm) {
Minchan Kim720c24192018-05-07 23:15:37 +0900315 up_read(&mm->mmap_sem);
Todd Kjos0c972a02017-06-29 12:01:41 -0700316 mmput(mm);
317 }
Todd Kjos57ada2f2017-06-29 12:01:46 -0700318 return vma ? -ENOMEM : -ESRCH;
Todd Kjos0c972a02017-06-29 12:01:41 -0700319}
320
Minchan Kimda1b9562018-08-23 14:29:56 +0900321
322static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
323 struct vm_area_struct *vma)
324{
325 if (vma)
326 alloc->vma_vm_mm = vma->vm_mm;
327 /*
328 * If we see alloc->vma is not NULL, buffer data structures set up
329 * completely. Look at smp_rmb side binder_alloc_get_vma.
330 * We also want to guarantee new alloc->vma_vm_mm is always visible
331 * if alloc->vma is set.
332 */
333 smp_wmb();
334 alloc->vma = vma;
335}
336
337static inline struct vm_area_struct *binder_alloc_get_vma(
338 struct binder_alloc *alloc)
339{
340 struct vm_area_struct *vma = NULL;
341
342 if (alloc->vma) {
343 /* Look at description in binder_alloc_set_vma */
344 smp_rmb();
345 vma = alloc->vma;
346 }
347 return vma;
348}
349
Xiongwei Song3f827242017-12-14 12:15:42 +0800350static struct binder_buffer *binder_alloc_new_buf_locked(
351 struct binder_alloc *alloc,
352 size_t data_size,
353 size_t offsets_size,
354 size_t extra_buffers_size,
355 int is_async)
Todd Kjos0c972a02017-06-29 12:01:41 -0700356{
357 struct rb_node *n = alloc->free_buffers.rb_node;
358 struct binder_buffer *buffer;
359 size_t buffer_size;
360 struct rb_node *best_fit = NULL;
361 void *has_page_addr;
362 void *end_page_addr;
363 size_t size, data_offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700364 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700365
Minchan Kimda1b9562018-08-23 14:29:56 +0900366 if (!binder_alloc_get_vma(alloc)) {
Sherry Yang128f3802018-08-07 12:57:13 -0700367 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
368 "%d: binder_alloc_buf, no vma\n",
369 alloc->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700370 return ERR_PTR(-ESRCH);
Todd Kjos0c972a02017-06-29 12:01:41 -0700371 }
372
373 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
374 ALIGN(offsets_size, sizeof(void *));
375
376 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
377 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
378 "%d: got transaction with invalid size %zd-%zd\n",
379 alloc->pid, data_size, offsets_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700380 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700381 }
382 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
383 if (size < data_offsets_size || size < extra_buffers_size) {
384 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
385 "%d: got transaction with invalid extra_buffers_size %zd\n",
386 alloc->pid, extra_buffers_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700387 return ERR_PTR(-EINVAL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700388 }
389 if (is_async &&
390 alloc->free_async_space < size + sizeof(struct binder_buffer)) {
391 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
392 "%d: binder_alloc_buf size %zd failed, no async space left\n",
393 alloc->pid, size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700394 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700395 }
396
Sherry Yang74310e02017-08-23 08:46:41 -0700397 /* Pad 0-size buffers so they get assigned unique addresses */
398 size = max(size, sizeof(void *));
399
Todd Kjos0c972a02017-06-29 12:01:41 -0700400 while (n) {
401 buffer = rb_entry(n, struct binder_buffer, rb_node);
402 BUG_ON(!buffer->free);
403 buffer_size = binder_alloc_buffer_size(alloc, buffer);
404
405 if (size < buffer_size) {
406 best_fit = n;
407 n = n->rb_left;
408 } else if (size > buffer_size)
409 n = n->rb_right;
410 else {
411 best_fit = n;
412 break;
413 }
414 }
415 if (best_fit == NULL) {
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700416 size_t allocated_buffers = 0;
417 size_t largest_alloc_size = 0;
418 size_t total_alloc_size = 0;
419 size_t free_buffers = 0;
420 size_t largest_free_size = 0;
421 size_t total_free_size = 0;
422
423 for (n = rb_first(&alloc->allocated_buffers); n != NULL;
424 n = rb_next(n)) {
425 buffer = rb_entry(n, struct binder_buffer, rb_node);
426 buffer_size = binder_alloc_buffer_size(alloc, buffer);
427 allocated_buffers++;
428 total_alloc_size += buffer_size;
429 if (buffer_size > largest_alloc_size)
430 largest_alloc_size = buffer_size;
431 }
432 for (n = rb_first(&alloc->free_buffers); n != NULL;
433 n = rb_next(n)) {
434 buffer = rb_entry(n, struct binder_buffer, rb_node);
435 buffer_size = binder_alloc_buffer_size(alloc, buffer);
436 free_buffers++;
437 total_free_size += buffer_size;
438 if (buffer_size > largest_free_size)
439 largest_free_size = buffer_size;
440 }
Sherry Yang128f3802018-08-07 12:57:13 -0700441 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
442 "%d: binder_alloc_buf size %zd failed, no address space\n",
443 alloc->pid, size);
444 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
445 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
446 total_alloc_size, allocated_buffers,
447 largest_alloc_size, total_free_size,
448 free_buffers, largest_free_size);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700449 return ERR_PTR(-ENOSPC);
Todd Kjos0c972a02017-06-29 12:01:41 -0700450 }
451 if (n == NULL) {
452 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
453 buffer_size = binder_alloc_buffer_size(alloc, buffer);
454 }
455
456 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
457 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
458 alloc->pid, size, buffer, buffer_size);
459
460 has_page_addr =
461 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
Sherry Yang74310e02017-08-23 08:46:41 -0700462 WARN_ON(n && buffer_size != size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700463 end_page_addr =
Sherry Yang74310e02017-08-23 08:46:41 -0700464 (void *)PAGE_ALIGN((uintptr_t)buffer->data + size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700465 if (end_page_addr > has_page_addr)
466 end_page_addr = has_page_addr;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700467 ret = binder_update_page_range(alloc, 1,
Sherry Yang6ae33b92017-09-16 01:11:56 -0400468 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr);
Todd Kjos57ada2f2017-06-29 12:01:46 -0700469 if (ret)
470 return ERR_PTR(ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700471
Todd Kjos0c972a02017-06-29 12:01:41 -0700472 if (buffer_size != size) {
Sherry Yang74310e02017-08-23 08:46:41 -0700473 struct binder_buffer *new_buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700474
Sherry Yang74310e02017-08-23 08:46:41 -0700475 new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
476 if (!new_buffer) {
477 pr_err("%s: %d failed to alloc new buffer struct\n",
478 __func__, alloc->pid);
479 goto err_alloc_buf_struct_failed;
480 }
481 new_buffer->data = (u8 *)buffer->data + size;
Todd Kjos0c972a02017-06-29 12:01:41 -0700482 list_add(&new_buffer->entry, &buffer->entry);
483 new_buffer->free = 1;
484 binder_insert_free_buffer(alloc, new_buffer);
485 }
Sherry Yang74310e02017-08-23 08:46:41 -0700486
487 rb_erase(best_fit, &alloc->free_buffers);
488 buffer->free = 0;
Todd Kjos7bada552018-11-06 15:55:32 -0800489 buffer->allow_user_free = 0;
Sherry Yang74310e02017-08-23 08:46:41 -0700490 binder_insert_allocated_buffer_locked(alloc, buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700491 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
492 "%d: binder_alloc_buf size %zd got %pK\n",
493 alloc->pid, size, buffer);
494 buffer->data_size = data_size;
495 buffer->offsets_size = offsets_size;
496 buffer->async_transaction = is_async;
497 buffer->extra_buffers_size = extra_buffers_size;
498 if (is_async) {
499 alloc->free_async_space -= size + sizeof(struct binder_buffer);
500 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
501 "%d: binder_alloc_buf size %zd async free %zd\n",
502 alloc->pid, size, alloc->free_async_space);
503 }
504 return buffer;
Sherry Yang74310e02017-08-23 08:46:41 -0700505
506err_alloc_buf_struct_failed:
507 binder_update_page_range(alloc, 0,
508 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400509 end_page_addr);
Sherry Yang74310e02017-08-23 08:46:41 -0700510 return ERR_PTR(-ENOMEM);
Todd Kjos0c972a02017-06-29 12:01:41 -0700511}
512
513/**
514 * binder_alloc_new_buf() - Allocate a new binder buffer
515 * @alloc: binder_alloc for this proc
516 * @data_size: size of user data buffer
517 * @offsets_size: user specified buffer offset
518 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
519 * @is_async: buffer for async transaction
520 *
521 * Allocate a new buffer given the requested sizes. Returns
522 * the kernel version of the buffer pointer. The size allocated
523 * is the sum of the three given sizes (each rounded up to
524 * pointer-sized boundary)
525 *
526 * Return: The allocated buffer or %NULL if error
527 */
528struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
529 size_t data_size,
530 size_t offsets_size,
531 size_t extra_buffers_size,
532 int is_async)
533{
534 struct binder_buffer *buffer;
535
536 mutex_lock(&alloc->mutex);
537 buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
538 extra_buffers_size, is_async);
539 mutex_unlock(&alloc->mutex);
540 return buffer;
541}
542
543static void *buffer_start_page(struct binder_buffer *buffer)
544{
Sherry Yang74310e02017-08-23 08:46:41 -0700545 return (void *)((uintptr_t)buffer->data & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700546}
547
Sherry Yang74310e02017-08-23 08:46:41 -0700548static void *prev_buffer_end_page(struct binder_buffer *buffer)
Todd Kjos0c972a02017-06-29 12:01:41 -0700549{
Sherry Yang74310e02017-08-23 08:46:41 -0700550 return (void *)(((uintptr_t)(buffer->data) - 1) & PAGE_MASK);
Todd Kjos0c972a02017-06-29 12:01:41 -0700551}
552
553static void binder_delete_free_buffer(struct binder_alloc *alloc,
554 struct binder_buffer *buffer)
555{
556 struct binder_buffer *prev, *next = NULL;
Sherry Yang74310e02017-08-23 08:46:41 -0700557 bool to_free = true;
Todd Kjos0c972a02017-06-29 12:01:41 -0700558 BUG_ON(alloc->buffers.next == &buffer->entry);
Sherry Yange21762192017-08-23 08:46:39 -0700559 prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700560 BUG_ON(!prev->free);
Sherry Yang74310e02017-08-23 08:46:41 -0700561 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
562 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700563 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700564 "%d: merge free, buffer %pK share page with %pK\n",
565 alloc->pid, buffer->data, prev->data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700566 }
567
568 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700569 next = binder_buffer_next(buffer);
Sherry Yang74310e02017-08-23 08:46:41 -0700570 if (buffer_start_page(next) == buffer_start_page(buffer)) {
571 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700572 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700573 "%d: merge free, buffer %pK share page with %pK\n",
574 alloc->pid,
575 buffer->data,
576 next->data);
Todd Kjos0c972a02017-06-29 12:01:41 -0700577 }
578 }
Sherry Yang74310e02017-08-23 08:46:41 -0700579
580 if (PAGE_ALIGNED(buffer->data)) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700581 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yang74310e02017-08-23 08:46:41 -0700582 "%d: merge free, buffer start %pK is page aligned\n",
583 alloc->pid, buffer->data);
584 to_free = false;
Todd Kjos0c972a02017-06-29 12:01:41 -0700585 }
Sherry Yang74310e02017-08-23 08:46:41 -0700586
587 if (to_free) {
588 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
589 "%d: merge free, buffer %pK do not share page with %pK or %pK\n",
590 alloc->pid, buffer->data,
Sherry Yangae65c852017-10-20 20:58:59 -0400591 prev->data, next ? next->data : NULL);
Sherry Yang74310e02017-08-23 08:46:41 -0700592 binder_update_page_range(alloc, 0, buffer_start_page(buffer),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400593 buffer_start_page(buffer) + PAGE_SIZE);
Sherry Yang74310e02017-08-23 08:46:41 -0700594 }
595 list_del(&buffer->entry);
596 kfree(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700597}
598
599static void binder_free_buf_locked(struct binder_alloc *alloc,
600 struct binder_buffer *buffer)
601{
602 size_t size, buffer_size;
603
604 buffer_size = binder_alloc_buffer_size(alloc, buffer);
605
606 size = ALIGN(buffer->data_size, sizeof(void *)) +
607 ALIGN(buffer->offsets_size, sizeof(void *)) +
608 ALIGN(buffer->extra_buffers_size, sizeof(void *));
609
610 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
611 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
612 alloc->pid, buffer, size, buffer_size);
613
614 BUG_ON(buffer->free);
615 BUG_ON(size > buffer_size);
616 BUG_ON(buffer->transaction != NULL);
Sherry Yang74310e02017-08-23 08:46:41 -0700617 BUG_ON(buffer->data < alloc->buffer);
618 BUG_ON(buffer->data > alloc->buffer + alloc->buffer_size);
Todd Kjos0c972a02017-06-29 12:01:41 -0700619
620 if (buffer->async_transaction) {
621 alloc->free_async_space += size + sizeof(struct binder_buffer);
622
623 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
624 "%d: binder_free_buf size %zd async free %zd\n",
625 alloc->pid, size, alloc->free_async_space);
626 }
627
628 binder_update_page_range(alloc, 0,
629 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
Sherry Yang6ae33b92017-09-16 01:11:56 -0400630 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK));
Todd Kjos0c972a02017-06-29 12:01:41 -0700631
632 rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
633 buffer->free = 1;
634 if (!list_is_last(&buffer->entry, &alloc->buffers)) {
Sherry Yange21762192017-08-23 08:46:39 -0700635 struct binder_buffer *next = binder_buffer_next(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700636
637 if (next->free) {
638 rb_erase(&next->rb_node, &alloc->free_buffers);
639 binder_delete_free_buffer(alloc, next);
640 }
641 }
642 if (alloc->buffers.next != &buffer->entry) {
Sherry Yange21762192017-08-23 08:46:39 -0700643 struct binder_buffer *prev = binder_buffer_prev(buffer);
Todd Kjos0c972a02017-06-29 12:01:41 -0700644
645 if (prev->free) {
646 binder_delete_free_buffer(alloc, buffer);
647 rb_erase(&prev->rb_node, &alloc->free_buffers);
648 buffer = prev;
649 }
650 }
651 binder_insert_free_buffer(alloc, buffer);
652}
653
654/**
655 * binder_alloc_free_buf() - free a binder buffer
656 * @alloc: binder_alloc for this proc
657 * @buffer: kernel pointer to buffer
658 *
659 * Free the buffer allocated via binder_alloc_new_buffer()
660 */
661void binder_alloc_free_buf(struct binder_alloc *alloc,
662 struct binder_buffer *buffer)
663{
664 mutex_lock(&alloc->mutex);
665 binder_free_buf_locked(alloc, buffer);
666 mutex_unlock(&alloc->mutex);
667}
668
669/**
670 * binder_alloc_mmap_handler() - map virtual address space for proc
671 * @alloc: alloc structure for this proc
672 * @vma: vma passed to mmap()
673 *
674 * Called by binder_mmap() to initialize the space specified in
675 * vma for allocating binder buffers
676 *
677 * Return:
678 * 0 = success
679 * -EBUSY = address space already mapped
680 * -ENOMEM = failed to map memory to given address space
681 */
682int binder_alloc_mmap_handler(struct binder_alloc *alloc,
683 struct vm_area_struct *vma)
684{
685 int ret;
Todd Kjos0c972a02017-06-29 12:01:41 -0700686 const char *failure_string;
687 struct binder_buffer *buffer;
688
689 mutex_lock(&binder_alloc_mmap_lock);
690 if (alloc->buffer) {
691 ret = -EBUSY;
692 failure_string = "already mapped";
693 goto err_already_mapped;
694 }
695
Todd Kjos88021162019-02-08 10:35:18 -0800696 alloc->buffer = (void *)vma->vm_start;
697 alloc->user_buffer_offset = 0;
Todd Kjos0c972a02017-06-29 12:01:41 -0700698 mutex_unlock(&binder_alloc_mmap_lock);
699
Kees Cook6396bb22018-06-12 14:03:40 -0700700 alloc->pages = kcalloc((vma->vm_end - vma->vm_start) / PAGE_SIZE,
701 sizeof(alloc->pages[0]),
Todd Kjos0c972a02017-06-29 12:01:41 -0700702 GFP_KERNEL);
703 if (alloc->pages == NULL) {
704 ret = -ENOMEM;
705 failure_string = "alloc page array";
706 goto err_alloc_pages_failed;
707 }
708 alloc->buffer_size = vma->vm_end - vma->vm_start;
709
Sherry Yang74310e02017-08-23 08:46:41 -0700710 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
711 if (!buffer) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700712 ret = -ENOMEM;
Sherry Yang74310e02017-08-23 08:46:41 -0700713 failure_string = "alloc buffer struct";
714 goto err_alloc_buf_struct_failed;
Todd Kjos0c972a02017-06-29 12:01:41 -0700715 }
Sherry Yang74310e02017-08-23 08:46:41 -0700716
717 buffer->data = alloc->buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700718 list_add(&buffer->entry, &alloc->buffers);
719 buffer->free = 1;
720 binder_insert_free_buffer(alloc, buffer);
721 alloc->free_async_space = alloc->buffer_size / 2;
Minchan Kimda1b9562018-08-23 14:29:56 +0900722 binder_alloc_set_vma(alloc, vma);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400723 mmgrab(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700724
725 return 0;
726
Sherry Yang74310e02017-08-23 08:46:41 -0700727err_alloc_buf_struct_failed:
Todd Kjos0c972a02017-06-29 12:01:41 -0700728 kfree(alloc->pages);
729 alloc->pages = NULL;
730err_alloc_pages_failed:
731 mutex_lock(&binder_alloc_mmap_lock);
Todd Kjos0c972a02017-06-29 12:01:41 -0700732 alloc->buffer = NULL;
Todd Kjos0c972a02017-06-29 12:01:41 -0700733err_already_mapped:
734 mutex_unlock(&binder_alloc_mmap_lock);
Sherry Yang128f3802018-08-07 12:57:13 -0700735 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
736 "%s: %d %lx-%lx %s failed %d\n", __func__,
737 alloc->pid, vma->vm_start, vma->vm_end,
738 failure_string, ret);
Todd Kjos0c972a02017-06-29 12:01:41 -0700739 return ret;
740}
741
742
743void binder_alloc_deferred_release(struct binder_alloc *alloc)
744{
745 struct rb_node *n;
746 int buffers, page_count;
Sherry Yang74310e02017-08-23 08:46:41 -0700747 struct binder_buffer *buffer;
Todd Kjos0c972a02017-06-29 12:01:41 -0700748
Todd Kjos0c972a02017-06-29 12:01:41 -0700749 buffers = 0;
750 mutex_lock(&alloc->mutex);
Minchan Kimda1b9562018-08-23 14:29:56 +0900751 BUG_ON(alloc->vma);
752
Todd Kjos0c972a02017-06-29 12:01:41 -0700753 while ((n = rb_first(&alloc->allocated_buffers))) {
Todd Kjos0c972a02017-06-29 12:01:41 -0700754 buffer = rb_entry(n, struct binder_buffer, rb_node);
755
756 /* Transaction should already have been freed */
757 BUG_ON(buffer->transaction);
758
759 binder_free_buf_locked(alloc, buffer);
760 buffers++;
761 }
762
Sherry Yang74310e02017-08-23 08:46:41 -0700763 while (!list_empty(&alloc->buffers)) {
764 buffer = list_first_entry(&alloc->buffers,
765 struct binder_buffer, entry);
766 WARN_ON(!buffer->free);
767
768 list_del(&buffer->entry);
769 WARN_ON_ONCE(!list_empty(&alloc->buffers));
770 kfree(buffer);
771 }
772
Todd Kjos0c972a02017-06-29 12:01:41 -0700773 page_count = 0;
774 if (alloc->pages) {
775 int i;
776
777 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
778 void *page_addr;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700779 bool on_lru;
Todd Kjos0c972a02017-06-29 12:01:41 -0700780
Sherry Yangf2517eb2017-08-23 08:46:42 -0700781 if (!alloc->pages[i].page_ptr)
Todd Kjos0c972a02017-06-29 12:01:41 -0700782 continue;
783
Sherry Yangf2517eb2017-08-23 08:46:42 -0700784 on_lru = list_lru_del(&binder_alloc_lru,
785 &alloc->pages[i].lru);
Todd Kjos0c972a02017-06-29 12:01:41 -0700786 page_addr = alloc->buffer + i * PAGE_SIZE;
787 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700788 "%s: %d: page %d at %pK %s\n",
789 __func__, alloc->pid, i, page_addr,
790 on_lru ? "on lru" : "active");
Sherry Yangf2517eb2017-08-23 08:46:42 -0700791 __free_page(alloc->pages[i].page_ptr);
Todd Kjos0c972a02017-06-29 12:01:41 -0700792 page_count++;
793 }
794 kfree(alloc->pages);
Todd Kjos0c972a02017-06-29 12:01:41 -0700795 }
796 mutex_unlock(&alloc->mutex);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400797 if (alloc->vma_vm_mm)
798 mmdrop(alloc->vma_vm_mm);
Todd Kjos0c972a02017-06-29 12:01:41 -0700799
800 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
801 "%s: %d buffers %d, pages %d\n",
802 __func__, alloc->pid, buffers, page_count);
803}
804
805static void print_binder_buffer(struct seq_file *m, const char *prefix,
806 struct binder_buffer *buffer)
807{
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700808 seq_printf(m, "%s %d: %pK size %zd:%zd:%zd %s\n",
Todd Kjos0c972a02017-06-29 12:01:41 -0700809 prefix, buffer->debug_id, buffer->data,
810 buffer->data_size, buffer->offsets_size,
Martijn Coenenb05a68e2017-06-29 12:01:52 -0700811 buffer->extra_buffers_size,
Todd Kjos0c972a02017-06-29 12:01:41 -0700812 buffer->transaction ? "active" : "delivered");
813}
814
815/**
816 * binder_alloc_print_allocated() - print buffer info
817 * @m: seq_file for output via seq_printf()
818 * @alloc: binder_alloc for this proc
819 *
820 * Prints information about every buffer associated with
821 * the binder_alloc state to the given seq_file
822 */
823void binder_alloc_print_allocated(struct seq_file *m,
824 struct binder_alloc *alloc)
825{
826 struct rb_node *n;
827
828 mutex_lock(&alloc->mutex);
829 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
830 print_binder_buffer(m, " buffer",
831 rb_entry(n, struct binder_buffer, rb_node));
832 mutex_unlock(&alloc->mutex);
833}
834
835/**
Sherry Yang8ef46652017-08-31 11:56:36 -0700836 * binder_alloc_print_pages() - print page usage
837 * @m: seq_file for output via seq_printf()
838 * @alloc: binder_alloc for this proc
839 */
840void binder_alloc_print_pages(struct seq_file *m,
841 struct binder_alloc *alloc)
842{
843 struct binder_lru_page *page;
844 int i;
845 int active = 0;
846 int lru = 0;
847 int free = 0;
848
849 mutex_lock(&alloc->mutex);
850 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
851 page = &alloc->pages[i];
852 if (!page->page_ptr)
853 free++;
854 else if (list_empty(&page->lru))
855 active++;
856 else
857 lru++;
858 }
859 mutex_unlock(&alloc->mutex);
860 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
Martijn Coenen8d9a3ab62017-11-13 10:06:56 +0100861 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
Sherry Yang8ef46652017-08-31 11:56:36 -0700862}
863
864/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700865 * binder_alloc_get_allocated_count() - return count of buffers
866 * @alloc: binder_alloc for this proc
867 *
868 * Return: count of allocated buffers
869 */
870int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
871{
872 struct rb_node *n;
873 int count = 0;
874
875 mutex_lock(&alloc->mutex);
876 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
877 count++;
878 mutex_unlock(&alloc->mutex);
879 return count;
880}
881
882
883/**
884 * binder_alloc_vma_close() - invalidate address space
885 * @alloc: binder_alloc for this proc
886 *
887 * Called from binder_vma_close() when releasing address space.
888 * Clears alloc->vma to prevent new incoming transactions from
889 * allocating more buffers.
890 */
891void binder_alloc_vma_close(struct binder_alloc *alloc)
892{
Minchan Kimda1b9562018-08-23 14:29:56 +0900893 binder_alloc_set_vma(alloc, NULL);
Todd Kjos0c972a02017-06-29 12:01:41 -0700894}
895
896/**
Sherry Yangf2517eb2017-08-23 08:46:42 -0700897 * binder_alloc_free_page() - shrinker callback to free pages
898 * @item: item to free
899 * @lock: lock protecting the item
900 * @cb_arg: callback argument
901 *
902 * Called from list_lru_walk() in binder_shrink_scan() to free
903 * up pages when the system is under memory pressure.
904 */
905enum lru_status binder_alloc_free_page(struct list_head *item,
906 struct list_lru_one *lru,
907 spinlock_t *lock,
908 void *cb_arg)
Todd Kjos324fa642018-11-06 15:56:31 -0800909 __must_hold(lock)
Sherry Yangf2517eb2017-08-23 08:46:42 -0700910{
911 struct mm_struct *mm = NULL;
912 struct binder_lru_page *page = container_of(item,
913 struct binder_lru_page,
914 lru);
915 struct binder_alloc *alloc;
916 uintptr_t page_addr;
917 size_t index;
Sherry Yanga1b22892017-10-03 16:15:00 -0700918 struct vm_area_struct *vma;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700919
920 alloc = page->alloc;
921 if (!mutex_trylock(&alloc->mutex))
922 goto err_get_alloc_mutex_failed;
923
924 if (!page->page_ptr)
925 goto err_page_already_freed;
926
927 index = page - alloc->pages;
928 page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
Minchan Kimda1b9562018-08-23 14:29:56 +0900929 vma = binder_alloc_get_vma(alloc);
Sherry Yanga1b22892017-10-03 16:15:00 -0700930 if (vma) {
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400931 if (!mmget_not_zero(alloc->vma_vm_mm))
932 goto err_mmget;
933 mm = alloc->vma_vm_mm;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700934 if (!down_write_trylock(&mm->mmap_sem))
935 goto err_down_write_mmap_sem_failed;
Sherry Yanga1b22892017-10-03 16:15:00 -0700936 }
Sherry Yangf2517eb2017-08-23 08:46:42 -0700937
Sherry Yanga1b22892017-10-03 16:15:00 -0700938 list_lru_isolate(lru, item);
939 spin_unlock(lock);
940
941 if (vma) {
Sherry Yange41e1642017-08-23 08:46:43 -0700942 trace_binder_unmap_user_start(alloc, index);
943
Sherry Yanga1b22892017-10-03 16:15:00 -0700944 zap_page_range(vma,
Sherry Yangf2517eb2017-08-23 08:46:42 -0700945 page_addr + alloc->user_buffer_offset,
946 PAGE_SIZE);
947
Sherry Yange41e1642017-08-23 08:46:43 -0700948 trace_binder_unmap_user_end(alloc, index);
949
Sherry Yangf2517eb2017-08-23 08:46:42 -0700950 up_write(&mm->mmap_sem);
951 mmput(mm);
952 }
953
Sherry Yange41e1642017-08-23 08:46:43 -0700954 trace_binder_unmap_kernel_start(alloc, index);
955
Sherry Yangf2517eb2017-08-23 08:46:42 -0700956 __free_page(page->page_ptr);
957 page->page_ptr = NULL;
958
Sherry Yange41e1642017-08-23 08:46:43 -0700959 trace_binder_unmap_kernel_end(alloc, index);
960
Sherry Yanga1b22892017-10-03 16:15:00 -0700961 spin_lock(lock);
Sherry Yangf2517eb2017-08-23 08:46:42 -0700962 mutex_unlock(&alloc->mutex);
Sherry Yanga1b22892017-10-03 16:15:00 -0700963 return LRU_REMOVED_RETRY;
Sherry Yangf2517eb2017-08-23 08:46:42 -0700964
965err_down_write_mmap_sem_failed:
Sherry Yanga1b22892017-10-03 16:15:00 -0700966 mmput_async(mm);
Sherry Yanga0c2baa2017-10-20 20:58:58 -0400967err_mmget:
Sherry Yangf2517eb2017-08-23 08:46:42 -0700968err_page_already_freed:
969 mutex_unlock(&alloc->mutex);
970err_get_alloc_mutex_failed:
971 return LRU_SKIP;
972}
973
974static unsigned long
975binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
976{
977 unsigned long ret = list_lru_count(&binder_alloc_lru);
978 return ret;
979}
980
981static unsigned long
982binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
983{
984 unsigned long ret;
985
986 ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
987 NULL, sc->nr_to_scan);
988 return ret;
989}
990
Sherry Yangde7bbe32017-10-06 16:12:05 -0400991static struct shrinker binder_shrinker = {
Sherry Yangf2517eb2017-08-23 08:46:42 -0700992 .count_objects = binder_shrink_count,
993 .scan_objects = binder_shrink_scan,
994 .seeks = DEFAULT_SEEKS,
995};
996
997/**
Todd Kjos0c972a02017-06-29 12:01:41 -0700998 * binder_alloc_init() - called by binder_open() for per-proc initialization
999 * @alloc: binder_alloc for this proc
1000 *
1001 * Called from binder_open() to initialize binder_alloc fields for
1002 * new binder proc
1003 */
1004void binder_alloc_init(struct binder_alloc *alloc)
1005{
Todd Kjos0c972a02017-06-29 12:01:41 -07001006 alloc->pid = current->group_leader->pid;
1007 mutex_init(&alloc->mutex);
Sherry Yang957ccc22017-08-31 10:26:06 -07001008 INIT_LIST_HEAD(&alloc->buffers);
Todd Kjos0c972a02017-06-29 12:01:41 -07001009}
1010
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001011int binder_alloc_shrinker_init(void)
Sherry Yangf2517eb2017-08-23 08:46:42 -07001012{
Tetsuo Handa533dfb22017-11-29 22:29:47 +09001013 int ret = list_lru_init(&binder_alloc_lru);
1014
1015 if (ret == 0) {
1016 ret = register_shrinker(&binder_shrinker);
1017 if (ret)
1018 list_lru_destroy(&binder_alloc_lru);
1019 }
1020 return ret;
Sherry Yangf2517eb2017-08-23 08:46:42 -07001021}
Todd Kjos1a7c3d92019-02-08 10:35:14 -08001022
1023/**
1024 * check_buffer() - verify that buffer/offset is safe to access
1025 * @alloc: binder_alloc for this proc
1026 * @buffer: binder buffer to be accessed
1027 * @offset: offset into @buffer data
1028 * @bytes: bytes to access from offset
1029 *
1030 * Check that the @offset/@bytes are within the size of the given
1031 * @buffer and that the buffer is currently active and not freeable.
1032 * Offsets must also be multiples of sizeof(u32). The kernel is
1033 * allowed to touch the buffer in two cases:
1034 *
1035 * 1) when the buffer is being created:
1036 * (buffer->free == 0 && buffer->allow_user_free == 0)
1037 * 2) when the buffer is being torn down:
1038 * (buffer->free == 0 && buffer->transaction == NULL).
1039 *
1040 * Return: true if the buffer is safe to access
1041 */
1042static inline bool check_buffer(struct binder_alloc *alloc,
1043 struct binder_buffer *buffer,
1044 binder_size_t offset, size_t bytes)
1045{
1046 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
1047
1048 return buffer_size >= bytes &&
1049 offset <= buffer_size - bytes &&
1050 IS_ALIGNED(offset, sizeof(u32)) &&
1051 !buffer->free &&
1052 (!buffer->allow_user_free || !buffer->transaction);
1053}
1054
1055/**
1056 * binder_alloc_get_page() - get kernel pointer for given buffer offset
1057 * @alloc: binder_alloc for this proc
1058 * @buffer: binder buffer to be accessed
1059 * @buffer_offset: offset into @buffer data
1060 * @pgoffp: address to copy final page offset to
1061 *
1062 * Lookup the struct page corresponding to the address
1063 * at @buffer_offset into @buffer->data. If @pgoffp is not
1064 * NULL, the byte-offset into the page is written there.
1065 *
1066 * The caller is responsible to ensure that the offset points
1067 * to a valid address within the @buffer and that @buffer is
1068 * not freeable by the user. Since it can't be freed, we are
1069 * guaranteed that the corresponding elements of @alloc->pages[]
1070 * cannot change.
1071 *
1072 * Return: struct page
1073 */
1074static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
1075 struct binder_buffer *buffer,
1076 binder_size_t buffer_offset,
1077 pgoff_t *pgoffp)
1078{
1079 binder_size_t buffer_space_offset = buffer_offset +
1080 (buffer->data - alloc->buffer);
1081 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
1082 size_t index = buffer_space_offset >> PAGE_SHIFT;
1083 struct binder_lru_page *lru_page;
1084
1085 lru_page = &alloc->pages[index];
1086 *pgoffp = pgoff;
1087 return lru_page->page_ptr;
1088}
1089
1090/**
1091 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1092 * @alloc: binder_alloc for this proc
1093 * @buffer: binder buffer to be accessed
1094 * @buffer_offset: offset into @buffer data
1095 * @from: userspace pointer to source buffer
1096 * @bytes: bytes to copy
1097 *
1098 * Copy bytes from source userspace to target buffer.
1099 *
1100 * Return: bytes remaining to be copied
1101 */
1102unsigned long
1103binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
1104 struct binder_buffer *buffer,
1105 binder_size_t buffer_offset,
1106 const void __user *from,
1107 size_t bytes)
1108{
1109 if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1110 return bytes;
1111
1112 while (bytes) {
1113 unsigned long size;
1114 unsigned long ret;
1115 struct page *page;
1116 pgoff_t pgoff;
1117 void *kptr;
1118
1119 page = binder_alloc_get_page(alloc, buffer,
1120 buffer_offset, &pgoff);
1121 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1122 kptr = kmap(page) + pgoff;
1123 ret = copy_from_user(kptr, from, size);
1124 kunmap(page);
1125 if (ret)
1126 return bytes - size + ret;
1127 bytes -= size;
1128 from += size;
1129 buffer_offset += size;
1130 }
1131 return 0;
1132}
Todd Kjos8ced0c62019-02-08 10:35:15 -08001133
1134static void binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
1135 bool to_buffer,
1136 struct binder_buffer *buffer,
1137 binder_size_t buffer_offset,
1138 void *ptr,
1139 size_t bytes)
1140{
1141 /* All copies must be 32-bit aligned and 32-bit size */
1142 BUG_ON(!check_buffer(alloc, buffer, buffer_offset, bytes));
1143
1144 while (bytes) {
1145 unsigned long size;
1146 struct page *page;
1147 pgoff_t pgoff;
1148 void *tmpptr;
1149 void *base_ptr;
1150
1151 page = binder_alloc_get_page(alloc, buffer,
1152 buffer_offset, &pgoff);
1153 size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
1154 base_ptr = kmap_atomic(page);
1155 tmpptr = base_ptr + pgoff;
1156 if (to_buffer)
1157 memcpy(tmpptr, ptr, size);
1158 else
1159 memcpy(ptr, tmpptr, size);
1160 /*
1161 * kunmap_atomic() takes care of flushing the cache
1162 * if this device has VIVT cache arch
1163 */
1164 kunmap_atomic(base_ptr);
1165 bytes -= size;
1166 pgoff = 0;
1167 ptr = ptr + size;
1168 buffer_offset += size;
1169 }
1170}
1171
1172void binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
1173 struct binder_buffer *buffer,
1174 binder_size_t buffer_offset,
1175 void *src,
1176 size_t bytes)
1177{
1178 binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
1179 src, bytes);
1180}
1181
1182void binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
1183 void *dest,
1184 struct binder_buffer *buffer,
1185 binder_size_t buffer_offset,
1186 size_t bytes)
1187{
1188 binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
1189 dest, bytes);
1190}
1191