19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20c972a05STodd Kjos /* binder_alloc.c
30c972a05STodd Kjos *
40c972a05STodd Kjos * Android IPC Subsystem
50c972a05STodd Kjos *
60c972a05STodd Kjos * Copyright (C) 2007-2017 Google, Inc.
70c972a05STodd Kjos */
80c972a05STodd Kjos
90c972a05STodd Kjos #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
100c972a05STodd Kjos
110c972a05STodd Kjos #include <linux/list.h>
120c972a05STodd Kjos #include <linux/sched/mm.h>
130c972a05STodd Kjos #include <linux/module.h>
140c972a05STodd Kjos #include <linux/rtmutex.h>
150c972a05STodd Kjos #include <linux/rbtree.h>
160c972a05STodd Kjos #include <linux/seq_file.h>
170c972a05STodd Kjos #include <linux/vmalloc.h>
180c972a05STodd Kjos #include <linux/slab.h>
190c972a05STodd Kjos #include <linux/sched.h>
20f2517eb7SSherry Yang #include <linux/list_lru.h>
21128f3804SSherry Yang #include <linux/ratelimit.h>
221e81c57bSGuenter Roeck #include <asm/cacheflush.h>
231a7c3d9bSTodd Kjos #include <linux/uaccess.h>
241a7c3d9bSTodd Kjos #include <linux/highmem.h>
2545d02f79SJann Horn #include <linux/sizes.h>
260c972a05STodd Kjos #include "binder_alloc.h"
270c972a05STodd Kjos #include "binder_trace.h"
280c972a05STodd Kjos
29ea9cdbf0SCarlos Llamas struct list_lru binder_freelist;
30f2517eb7SSherry Yang
310c972a05STodd Kjos static DEFINE_MUTEX(binder_alloc_mmap_lock);
320c972a05STodd Kjos
330c972a05STodd Kjos enum {
34128f3804SSherry Yang BINDER_DEBUG_USER_ERROR = 1U << 0,
350c972a05STodd Kjos BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
360c972a05STodd Kjos BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
370c972a05STodd Kjos BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
380c972a05STodd Kjos };
39128f3804SSherry Yang static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
400c972a05STodd Kjos
410c972a05STodd Kjos module_param_named(debug_mask, binder_alloc_debug_mask,
420c972a05STodd Kjos uint, 0644);
430c972a05STodd Kjos
440c972a05STodd Kjos #define binder_alloc_debug(mask, x...) \
450c972a05STodd Kjos do { \
460c972a05STodd Kjos if (binder_alloc_debug_mask & mask) \
47128f3804SSherry Yang pr_info_ratelimited(x); \
480c972a05STodd Kjos } while (0)
490c972a05STodd Kjos
binder_buffer_next(struct binder_buffer * buffer)50e2176219SSherry Yang static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
51e2176219SSherry Yang {
52e2176219SSherry Yang return list_entry(buffer->entry.next, struct binder_buffer, entry);
53e2176219SSherry Yang }
54e2176219SSherry Yang
binder_buffer_prev(struct binder_buffer * buffer)55e2176219SSherry Yang static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
56e2176219SSherry Yang {
57e2176219SSherry Yang return list_entry(buffer->entry.prev, struct binder_buffer, entry);
58e2176219SSherry Yang }
59e2176219SSherry Yang
binder_alloc_buffer_size(struct binder_alloc * alloc,struct binder_buffer * buffer)600c972a05STodd Kjos static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
610c972a05STodd Kjos struct binder_buffer *buffer)
620c972a05STodd Kjos {
630c972a05STodd Kjos if (list_is_last(&buffer->entry, &alloc->buffers))
640a7bf686SCarlos Llamas return alloc->vm_start + alloc->buffer_size - buffer->user_data;
65bde4a19fSTodd Kjos return binder_buffer_next(buffer)->user_data - buffer->user_data;
660c972a05STodd Kjos }
670c972a05STodd Kjos
binder_insert_free_buffer(struct binder_alloc * alloc,struct binder_buffer * new_buffer)680c972a05STodd Kjos static void binder_insert_free_buffer(struct binder_alloc *alloc,
690c972a05STodd Kjos struct binder_buffer *new_buffer)
700c972a05STodd Kjos {
710c972a05STodd Kjos struct rb_node **p = &alloc->free_buffers.rb_node;
720c972a05STodd Kjos struct rb_node *parent = NULL;
730c972a05STodd Kjos struct binder_buffer *buffer;
740c972a05STodd Kjos size_t buffer_size;
750c972a05STodd Kjos size_t new_buffer_size;
760c972a05STodd Kjos
770c972a05STodd Kjos BUG_ON(!new_buffer->free);
780c972a05STodd Kjos
790c972a05STodd Kjos new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
800c972a05STodd Kjos
810c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
820c972a05STodd Kjos "%d: add free buffer, size %zd, at %pK\n",
830c972a05STodd Kjos alloc->pid, new_buffer_size, new_buffer);
840c972a05STodd Kjos
850c972a05STodd Kjos while (*p) {
860c972a05STodd Kjos parent = *p;
870c972a05STodd Kjos buffer = rb_entry(parent, struct binder_buffer, rb_node);
880c972a05STodd Kjos BUG_ON(!buffer->free);
890c972a05STodd Kjos
900c972a05STodd Kjos buffer_size = binder_alloc_buffer_size(alloc, buffer);
910c972a05STodd Kjos
920c972a05STodd Kjos if (new_buffer_size < buffer_size)
930c972a05STodd Kjos p = &parent->rb_left;
940c972a05STodd Kjos else
950c972a05STodd Kjos p = &parent->rb_right;
960c972a05STodd Kjos }
970c972a05STodd Kjos rb_link_node(&new_buffer->rb_node, parent, p);
980c972a05STodd Kjos rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
990c972a05STodd Kjos }
1000c972a05STodd Kjos
binder_insert_allocated_buffer_locked(struct binder_alloc * alloc,struct binder_buffer * new_buffer)1010c972a05STodd Kjos static void binder_insert_allocated_buffer_locked(
1020c972a05STodd Kjos struct binder_alloc *alloc, struct binder_buffer *new_buffer)
1030c972a05STodd Kjos {
1040c972a05STodd Kjos struct rb_node **p = &alloc->allocated_buffers.rb_node;
1050c972a05STodd Kjos struct rb_node *parent = NULL;
1060c972a05STodd Kjos struct binder_buffer *buffer;
1070c972a05STodd Kjos
1080c972a05STodd Kjos BUG_ON(new_buffer->free);
1090c972a05STodd Kjos
1100c972a05STodd Kjos while (*p) {
1110c972a05STodd Kjos parent = *p;
1120c972a05STodd Kjos buffer = rb_entry(parent, struct binder_buffer, rb_node);
1130c972a05STodd Kjos BUG_ON(buffer->free);
1140c972a05STodd Kjos
115bde4a19fSTodd Kjos if (new_buffer->user_data < buffer->user_data)
1160c972a05STodd Kjos p = &parent->rb_left;
117bde4a19fSTodd Kjos else if (new_buffer->user_data > buffer->user_data)
1180c972a05STodd Kjos p = &parent->rb_right;
1190c972a05STodd Kjos else
1200c972a05STodd Kjos BUG();
1210c972a05STodd Kjos }
1220c972a05STodd Kjos rb_link_node(&new_buffer->rb_node, parent, p);
1230c972a05STodd Kjos rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
1240c972a05STodd Kjos }
1250c972a05STodd Kjos
binder_alloc_prepare_to_free_locked(struct binder_alloc * alloc,unsigned long user_ptr)12653d311cfSTodd Kjos static struct binder_buffer *binder_alloc_prepare_to_free_locked(
1270c972a05STodd Kjos struct binder_alloc *alloc,
128df9aabeaSCarlos Llamas unsigned long user_ptr)
1290c972a05STodd Kjos {
1300c972a05STodd Kjos struct rb_node *n = alloc->allocated_buffers.rb_node;
1310c972a05STodd Kjos struct binder_buffer *buffer;
1320c972a05STodd Kjos
1330c972a05STodd Kjos while (n) {
1340c972a05STodd Kjos buffer = rb_entry(n, struct binder_buffer, rb_node);
1350c972a05STodd Kjos BUG_ON(buffer->free);
1360c972a05STodd Kjos
137df9aabeaSCarlos Llamas if (user_ptr < buffer->user_data) {
1380c972a05STodd Kjos n = n->rb_left;
139df9aabeaSCarlos Llamas } else if (user_ptr > buffer->user_data) {
1400c972a05STodd Kjos n = n->rb_right;
141df9aabeaSCarlos Llamas } else {
14253d311cfSTodd Kjos /*
14353d311cfSTodd Kjos * Guard against user threads attempting to
1447bada55aSTodd Kjos * free the buffer when in use by kernel or
1457bada55aSTodd Kjos * after it's already been freed.
14653d311cfSTodd Kjos */
1477bada55aSTodd Kjos if (!buffer->allow_user_free)
1487bada55aSTodd Kjos return ERR_PTR(-EPERM);
1497bada55aSTodd Kjos buffer->allow_user_free = 0;
1500c972a05STodd Kjos return buffer;
1510c972a05STodd Kjos }
15253d311cfSTodd Kjos }
1530c972a05STodd Kjos return NULL;
1540c972a05STodd Kjos }
1550c972a05STodd Kjos
1560c972a05STodd Kjos /**
1575dc54a06SJoel Fernandes (Google) * binder_alloc_prepare_to_free() - get buffer given user ptr
1580c972a05STodd Kjos * @alloc: binder_alloc for this proc
1590c972a05STodd Kjos * @user_ptr: User pointer to buffer data
1600c972a05STodd Kjos *
1610c972a05STodd Kjos * Validate userspace pointer to buffer data and return buffer corresponding to
1620c972a05STodd Kjos * that user pointer. Search the rb tree for buffer that matches user data
1630c972a05STodd Kjos * pointer.
1640c972a05STodd Kjos *
1650c972a05STodd Kjos * Return: Pointer to buffer or NULL
1660c972a05STodd Kjos */
binder_alloc_prepare_to_free(struct binder_alloc * alloc,unsigned long user_ptr)16753d311cfSTodd Kjos struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
168df9aabeaSCarlos Llamas unsigned long user_ptr)
1690c972a05STodd Kjos {
1700c972a05STodd Kjos struct binder_buffer *buffer;
1710c972a05STodd Kjos
1728b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
17353d311cfSTodd Kjos buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
1748b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
1750c972a05STodd Kjos return buffer;
1760c972a05STodd Kjos }
1770c972a05STodd Kjos
17837ebbb4fSCarlos Llamas static inline void
binder_set_installed_page(struct binder_alloc * alloc,unsigned long index,struct page * page)179f909f030SCarlos Llamas binder_set_installed_page(struct binder_alloc *alloc,
180f909f030SCarlos Llamas unsigned long index,
18137ebbb4fSCarlos Llamas struct page *page)
1820c972a05STodd Kjos {
18337ebbb4fSCarlos Llamas /* Pairs with acquire in binder_get_installed_page() */
184f909f030SCarlos Llamas smp_store_release(&alloc->pages[index], page);
18537ebbb4fSCarlos Llamas }
18637ebbb4fSCarlos Llamas
18737ebbb4fSCarlos Llamas static inline struct page *
binder_get_installed_page(struct binder_alloc * alloc,unsigned long index)188f909f030SCarlos Llamas binder_get_installed_page(struct binder_alloc *alloc, unsigned long index)
18937ebbb4fSCarlos Llamas {
19037ebbb4fSCarlos Llamas /* Pairs with release in binder_set_installed_page() */
191f909f030SCarlos Llamas return smp_load_acquire(&alloc->pages[index]);
19237ebbb4fSCarlos Llamas }
19337ebbb4fSCarlos Llamas
binder_lru_freelist_add(struct binder_alloc * alloc,unsigned long start,unsigned long end)194ea9cdbf0SCarlos Llamas static void binder_lru_freelist_add(struct binder_alloc *alloc,
1950d35bf3bSCarlos Llamas unsigned long start, unsigned long end)
1960d35bf3bSCarlos Llamas {
1970d35bf3bSCarlos Llamas unsigned long page_addr;
198f909f030SCarlos Llamas struct page *page;
1990c972a05STodd Kjos
2000d35bf3bSCarlos Llamas trace_binder_update_page_range(alloc, false, start, end);
201f2517eb7SSherry Yang
202f2517eb7SSherry Yang for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
203e41e164cSSherry Yang size_t index;
2040d35bf3bSCarlos Llamas int ret;
2050d35bf3bSCarlos Llamas
2060a7bf686SCarlos Llamas index = (page_addr - alloc->vm_start) / PAGE_SIZE;
207f909f030SCarlos Llamas page = binder_get_installed_page(alloc, index);
208f909f030SCarlos Llamas if (!page)
20937ebbb4fSCarlos Llamas continue;
21037ebbb4fSCarlos Llamas
2110d35bf3bSCarlos Llamas trace_binder_free_lru_start(alloc, index);
2120d35bf3bSCarlos Llamas
21349d2562cSCarlos Llamas ret = list_lru_add(&binder_freelist,
214f909f030SCarlos Llamas page_to_lru(page),
215f909f030SCarlos Llamas page_to_nid(page),
21649d2562cSCarlos Llamas NULL);
2170d35bf3bSCarlos Llamas WARN_ON(!ret);
2180d35bf3bSCarlos Llamas
2190d35bf3bSCarlos Llamas trace_binder_free_lru_end(alloc, index);
2200d35bf3bSCarlos Llamas }
2210d35bf3bSCarlos Llamas }
2220d35bf3bSCarlos Llamas
223072010abSCarlos Llamas static inline
binder_alloc_set_mapped(struct binder_alloc * alloc,bool state)224072010abSCarlos Llamas void binder_alloc_set_mapped(struct binder_alloc *alloc, bool state)
225072010abSCarlos Llamas {
226072010abSCarlos Llamas /* pairs with smp_load_acquire in binder_alloc_is_mapped() */
227072010abSCarlos Llamas smp_store_release(&alloc->mapped, state);
228072010abSCarlos Llamas }
229072010abSCarlos Llamas
binder_alloc_is_mapped(struct binder_alloc * alloc)230072010abSCarlos Llamas static inline bool binder_alloc_is_mapped(struct binder_alloc *alloc)
231072010abSCarlos Llamas {
232072010abSCarlos Llamas /* pairs with smp_store_release in binder_alloc_set_mapped() */
233072010abSCarlos Llamas return smp_load_acquire(&alloc->mapped);
234072010abSCarlos Llamas }
235072010abSCarlos Llamas
binder_page_lookup(struct binder_alloc * alloc,unsigned long addr)2369e2aa765SCarlos Llamas static struct page *binder_page_lookup(struct binder_alloc *alloc,
2379e2aa765SCarlos Llamas unsigned long addr)
2389e2aa765SCarlos Llamas {
2399e2aa765SCarlos Llamas struct mm_struct *mm = alloc->mm;
2409e2aa765SCarlos Llamas struct page *page;
2419e2aa765SCarlos Llamas long npages = 0;
2429e2aa765SCarlos Llamas
2439e2aa765SCarlos Llamas /*
2449e2aa765SCarlos Llamas * Find an existing page in the remote mm. If missing,
2459e2aa765SCarlos Llamas * don't attempt to fault-in just propagate an error.
2469e2aa765SCarlos Llamas */
2479e2aa765SCarlos Llamas mmap_read_lock(mm);
2489e2aa765SCarlos Llamas if (binder_alloc_is_mapped(alloc))
2499e2aa765SCarlos Llamas npages = get_user_pages_remote(mm, addr, 1, FOLL_NOFAULT,
2509e2aa765SCarlos Llamas &page, NULL);
2519e2aa765SCarlos Llamas mmap_read_unlock(mm);
2529e2aa765SCarlos Llamas
2539e2aa765SCarlos Llamas return npages > 0 ? page : NULL;
2549e2aa765SCarlos Llamas }
2559e2aa765SCarlos Llamas
binder_page_insert(struct binder_alloc * alloc,unsigned long addr,struct page * page)2569e2aa765SCarlos Llamas static int binder_page_insert(struct binder_alloc *alloc,
2579e2aa765SCarlos Llamas unsigned long addr,
2589e2aa765SCarlos Llamas struct page *page)
2599e2aa765SCarlos Llamas {
2609e2aa765SCarlos Llamas struct mm_struct *mm = alloc->mm;
2619e2aa765SCarlos Llamas struct vm_area_struct *vma;
2629e2aa765SCarlos Llamas int ret = -ESRCH;
2639e2aa765SCarlos Llamas
2649e2aa765SCarlos Llamas /* attempt per-vma lock first */
2659e2aa765SCarlos Llamas vma = lock_vma_under_rcu(mm, addr);
2669e2aa765SCarlos Llamas if (vma) {
2679e2aa765SCarlos Llamas if (binder_alloc_is_mapped(alloc))
2689e2aa765SCarlos Llamas ret = vm_insert_page(vma, addr, page);
2699e2aa765SCarlos Llamas vma_end_read(vma);
2709e2aa765SCarlos Llamas return ret;
2719e2aa765SCarlos Llamas }
2729e2aa765SCarlos Llamas
2739e2aa765SCarlos Llamas /* fall back to mmap_lock */
2749e2aa765SCarlos Llamas mmap_read_lock(mm);
2759e2aa765SCarlos Llamas vma = vma_lookup(mm, addr);
2769e2aa765SCarlos Llamas if (vma && binder_alloc_is_mapped(alloc))
2779e2aa765SCarlos Llamas ret = vm_insert_page(vma, addr, page);
2789e2aa765SCarlos Llamas mmap_read_unlock(mm);
2799e2aa765SCarlos Llamas
2809e2aa765SCarlos Llamas return ret;
2819e2aa765SCarlos Llamas }
2829e2aa765SCarlos Llamas
binder_page_alloc(struct binder_alloc * alloc,unsigned long index)283f909f030SCarlos Llamas static struct page *binder_page_alloc(struct binder_alloc *alloc,
284f909f030SCarlos Llamas unsigned long index)
285f909f030SCarlos Llamas {
286f909f030SCarlos Llamas struct binder_shrinker_mdata *mdata;
287f909f030SCarlos Llamas struct page *page;
288f909f030SCarlos Llamas
289f909f030SCarlos Llamas page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
290f909f030SCarlos Llamas if (!page)
291f909f030SCarlos Llamas return NULL;
292f909f030SCarlos Llamas
293f909f030SCarlos Llamas /* allocate and install shrinker metadata under page->private */
294f909f030SCarlos Llamas mdata = kzalloc(sizeof(*mdata), GFP_KERNEL);
295f909f030SCarlos Llamas if (!mdata) {
296f909f030SCarlos Llamas __free_page(page);
297f909f030SCarlos Llamas return NULL;
298f909f030SCarlos Llamas }
299f909f030SCarlos Llamas
300f909f030SCarlos Llamas mdata->alloc = alloc;
301f909f030SCarlos Llamas mdata->page_index = index;
302f909f030SCarlos Llamas INIT_LIST_HEAD(&mdata->lru);
303f909f030SCarlos Llamas set_page_private(page, (unsigned long)mdata);
304f909f030SCarlos Llamas
305f909f030SCarlos Llamas return page;
306f909f030SCarlos Llamas }
307f909f030SCarlos Llamas
binder_free_page(struct page * page)308f909f030SCarlos Llamas static void binder_free_page(struct page *page)
309f909f030SCarlos Llamas {
310f909f030SCarlos Llamas kfree((struct binder_shrinker_mdata *)page_private(page));
311f909f030SCarlos Llamas __free_page(page);
312f909f030SCarlos Llamas }
313f909f030SCarlos Llamas
binder_install_single_page(struct binder_alloc * alloc,unsigned long index,unsigned long addr)314ea2735ceSCarlos Llamas static int binder_install_single_page(struct binder_alloc *alloc,
315f909f030SCarlos Llamas unsigned long index,
316ea2735ceSCarlos Llamas unsigned long addr)
317ea2735ceSCarlos Llamas {
318ea2735ceSCarlos Llamas struct page *page;
319d1716b4bSCarlos Llamas int ret;
320ea2735ceSCarlos Llamas
321ea2735ceSCarlos Llamas if (!mmget_not_zero(alloc->mm))
322ea2735ceSCarlos Llamas return -ESRCH;
323ea2735ceSCarlos Llamas
324f909f030SCarlos Llamas page = binder_page_alloc(alloc, index);
325ea2735ceSCarlos Llamas if (!page) {
326ea2735ceSCarlos Llamas ret = -ENOMEM;
327ea2735ceSCarlos Llamas goto out;
328ea2735ceSCarlos Llamas }
329ea2735ceSCarlos Llamas
3309e2aa765SCarlos Llamas ret = binder_page_insert(alloc, addr, page);
331d1716b4bSCarlos Llamas switch (ret) {
332d1716b4bSCarlos Llamas case -EBUSY:
333d1716b4bSCarlos Llamas /*
334d1716b4bSCarlos Llamas * EBUSY is ok. Someone installed the pte first but the
335f909f030SCarlos Llamas * alloc->pages[index] has not been updated yet. Discard
336d1716b4bSCarlos Llamas * our page and look up the one already installed.
337d1716b4bSCarlos Llamas */
338d1716b4bSCarlos Llamas ret = 0;
339f909f030SCarlos Llamas binder_free_page(page);
3409e2aa765SCarlos Llamas page = binder_page_lookup(alloc, addr);
3419e2aa765SCarlos Llamas if (!page) {
342d1716b4bSCarlos Llamas pr_err("%d: failed to find page at offset %lx\n",
3430a7bf686SCarlos Llamas alloc->pid, addr - alloc->vm_start);
344d1716b4bSCarlos Llamas ret = -ESRCH;
345d1716b4bSCarlos Llamas break;
346d1716b4bSCarlos Llamas }
347d1716b4bSCarlos Llamas fallthrough;
348d1716b4bSCarlos Llamas case 0:
34937ebbb4fSCarlos Llamas /* Mark page installation complete and safe to use */
350f909f030SCarlos Llamas binder_set_installed_page(alloc, index, page);
351d1716b4bSCarlos Llamas break;
352d1716b4bSCarlos Llamas default:
353f909f030SCarlos Llamas binder_free_page(page);
354d1716b4bSCarlos Llamas pr_err("%d: %s failed to insert page at offset %lx with %d\n",
3550a7bf686SCarlos Llamas alloc->pid, __func__, addr - alloc->vm_start, ret);
356d1716b4bSCarlos Llamas break;
357d1716b4bSCarlos Llamas }
358ea2735ceSCarlos Llamas out:
359ea2735ceSCarlos Llamas mmput_async(alloc->mm);
360ea2735ceSCarlos Llamas return ret;
361ea2735ceSCarlos Llamas }
362ea2735ceSCarlos Llamas
binder_install_buffer_pages(struct binder_alloc * alloc,struct binder_buffer * buffer,size_t size)36337ebbb4fSCarlos Llamas static int binder_install_buffer_pages(struct binder_alloc *alloc,
36437ebbb4fSCarlos Llamas struct binder_buffer *buffer,
36537ebbb4fSCarlos Llamas size_t size)
36637ebbb4fSCarlos Llamas {
36737ebbb4fSCarlos Llamas unsigned long start, final;
36837ebbb4fSCarlos Llamas unsigned long page_addr;
36937ebbb4fSCarlos Llamas
37037ebbb4fSCarlos Llamas start = buffer->user_data & PAGE_MASK;
37137ebbb4fSCarlos Llamas final = PAGE_ALIGN(buffer->user_data + size);
37237ebbb4fSCarlos Llamas
37337ebbb4fSCarlos Llamas for (page_addr = start; page_addr < final; page_addr += PAGE_SIZE) {
37437ebbb4fSCarlos Llamas unsigned long index;
37537ebbb4fSCarlos Llamas int ret;
37637ebbb4fSCarlos Llamas
3770a7bf686SCarlos Llamas index = (page_addr - alloc->vm_start) / PAGE_SIZE;
378f909f030SCarlos Llamas if (binder_get_installed_page(alloc, index))
37937ebbb4fSCarlos Llamas continue;
38037ebbb4fSCarlos Llamas
38137ebbb4fSCarlos Llamas trace_binder_alloc_page_start(alloc, index);
38237ebbb4fSCarlos Llamas
383f909f030SCarlos Llamas ret = binder_install_single_page(alloc, index, page_addr);
38437ebbb4fSCarlos Llamas if (ret)
38537ebbb4fSCarlos Llamas return ret;
38637ebbb4fSCarlos Llamas
38737ebbb4fSCarlos Llamas trace_binder_alloc_page_end(alloc, index);
38837ebbb4fSCarlos Llamas }
38937ebbb4fSCarlos Llamas
39037ebbb4fSCarlos Llamas return 0;
39137ebbb4fSCarlos Llamas }
39237ebbb4fSCarlos Llamas
39337ebbb4fSCarlos Llamas /* The range of pages should exclude those shared with other buffers */
binder_lru_freelist_del(struct binder_alloc * alloc,unsigned long start,unsigned long end)394ea9cdbf0SCarlos Llamas static void binder_lru_freelist_del(struct binder_alloc *alloc,
395df9aabeaSCarlos Llamas unsigned long start, unsigned long end)
3960c972a05STodd Kjos {
397df9aabeaSCarlos Llamas unsigned long page_addr;
398f909f030SCarlos Llamas struct page *page;
3990c972a05STodd Kjos
4000d35bf3bSCarlos Llamas trace_binder_update_page_range(alloc, true, start, end);
401f2517eb7SSherry Yang
402f2517eb7SSherry Yang for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
403ea2735ceSCarlos Llamas unsigned long index;
4040c972a05STodd Kjos bool on_lru;
4050c972a05STodd Kjos
4060a7bf686SCarlos Llamas index = (page_addr - alloc->vm_start) / PAGE_SIZE;
407f909f030SCarlos Llamas page = binder_get_installed_page(alloc, index);
4080c972a05STodd Kjos
409f909f030SCarlos Llamas if (page) {
410e41e164cSSherry Yang trace_binder_alloc_lru_start(alloc, index);
411e41e164cSSherry Yang
41249d2562cSCarlos Llamas on_lru = list_lru_del(&binder_freelist,
413f909f030SCarlos Llamas page_to_lru(page),
414f909f030SCarlos Llamas page_to_nid(page),
41549d2562cSCarlos Llamas NULL);
416f2517eb7SSherry Yang WARN_ON(!on_lru);
417e41e164cSSherry Yang
418e41e164cSSherry Yang trace_binder_alloc_lru_end(alloc, index);
419f2517eb7SSherry Yang continue;
420f2517eb7SSherry Yang }
421f2517eb7SSherry Yang
4228d9a3ab6SMartijn Coenen if (index + 1 > alloc->pages_high)
4238d9a3ab6SMartijn Coenen alloc->pages_high = index + 1;
4240c972a05STodd Kjos }
4250c972a05STodd Kjos }
4260c972a05STodd Kjos
debug_no_space_locked(struct binder_alloc * alloc)4279409af24SCarlos Llamas static void debug_no_space_locked(struct binder_alloc *alloc)
4289409af24SCarlos Llamas {
4299409af24SCarlos Llamas size_t largest_alloc_size = 0;
4309409af24SCarlos Llamas struct binder_buffer *buffer;
4319409af24SCarlos Llamas size_t allocated_buffers = 0;
4329409af24SCarlos Llamas size_t largest_free_size = 0;
4339409af24SCarlos Llamas size_t total_alloc_size = 0;
4349409af24SCarlos Llamas size_t total_free_size = 0;
4359409af24SCarlos Llamas size_t free_buffers = 0;
4369409af24SCarlos Llamas size_t buffer_size;
4379409af24SCarlos Llamas struct rb_node *n;
4389409af24SCarlos Llamas
4399409af24SCarlos Llamas for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
4409409af24SCarlos Llamas buffer = rb_entry(n, struct binder_buffer, rb_node);
4419409af24SCarlos Llamas buffer_size = binder_alloc_buffer_size(alloc, buffer);
4429409af24SCarlos Llamas allocated_buffers++;
4439409af24SCarlos Llamas total_alloc_size += buffer_size;
4449409af24SCarlos Llamas if (buffer_size > largest_alloc_size)
4459409af24SCarlos Llamas largest_alloc_size = buffer_size;
4469409af24SCarlos Llamas }
4479409af24SCarlos Llamas
4489409af24SCarlos Llamas for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
4499409af24SCarlos Llamas buffer = rb_entry(n, struct binder_buffer, rb_node);
4509409af24SCarlos Llamas buffer_size = binder_alloc_buffer_size(alloc, buffer);
4519409af24SCarlos Llamas free_buffers++;
4529409af24SCarlos Llamas total_free_size += buffer_size;
4539409af24SCarlos Llamas if (buffer_size > largest_free_size)
4549409af24SCarlos Llamas largest_free_size = buffer_size;
4559409af24SCarlos Llamas }
4569409af24SCarlos Llamas
4579409af24SCarlos Llamas binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
4589409af24SCarlos Llamas "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
4599409af24SCarlos Llamas total_alloc_size, allocated_buffers,
4609409af24SCarlos Llamas largest_alloc_size, total_free_size,
4619409af24SCarlos Llamas free_buffers, largest_free_size);
4629409af24SCarlos Llamas }
4639409af24SCarlos Llamas
debug_low_async_space_locked(struct binder_alloc * alloc)46489f71743SCarlos Llamas static bool debug_low_async_space_locked(struct binder_alloc *alloc)
465261e7818SMartijn Coenen {
466261e7818SMartijn Coenen /*
467261e7818SMartijn Coenen * Find the amount and size of buffers allocated by the current caller;
468261e7818SMartijn Coenen * The idea is that once we cross the threshold, whoever is responsible
469261e7818SMartijn Coenen * for the low async space is likely to try to send another async txn,
470261e7818SMartijn Coenen * and at some point we'll catch them in the act. This is more efficient
471261e7818SMartijn Coenen * than keeping a map per pid.
472261e7818SMartijn Coenen */
473261e7818SMartijn Coenen struct binder_buffer *buffer;
474261e7818SMartijn Coenen size_t total_alloc_size = 0;
47589f71743SCarlos Llamas int pid = current->tgid;
476261e7818SMartijn Coenen size_t num_buffers = 0;
47789f71743SCarlos Llamas struct rb_node *n;
478261e7818SMartijn Coenen
479c13500eaSCarlos Llamas /*
480c13500eaSCarlos Llamas * Only start detecting spammers once we have less than 20% of async
481c13500eaSCarlos Llamas * space left (which is less than 10% of total buffer size).
482c13500eaSCarlos Llamas */
483c13500eaSCarlos Llamas if (alloc->free_async_space >= alloc->buffer_size / 10) {
484c13500eaSCarlos Llamas alloc->oneway_spam_detected = false;
485c13500eaSCarlos Llamas return false;
486c13500eaSCarlos Llamas }
487261e7818SMartijn Coenen
488261e7818SMartijn Coenen for (n = rb_first(&alloc->allocated_buffers); n != NULL;
489261e7818SMartijn Coenen n = rb_next(n)) {
490261e7818SMartijn Coenen buffer = rb_entry(n, struct binder_buffer, rb_node);
491261e7818SMartijn Coenen if (buffer->pid != pid)
492261e7818SMartijn Coenen continue;
493261e7818SMartijn Coenen if (!buffer->async_transaction)
494261e7818SMartijn Coenen continue;
495c6d05e07SCarlos Llamas total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
496261e7818SMartijn Coenen num_buffers++;
497261e7818SMartijn Coenen }
498261e7818SMartijn Coenen
499261e7818SMartijn Coenen /*
500261e7818SMartijn Coenen * Warn if this pid has more than 50 transactions, or more than 50% of
501a7dc1e6fSHang Lu * async space (which is 25% of total buffer size). Oneway spam is only
502a7dc1e6fSHang Lu * detected when the threshold is exceeded.
503261e7818SMartijn Coenen */
504261e7818SMartijn Coenen if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
505261e7818SMartijn Coenen binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
506261e7818SMartijn Coenen "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
507261e7818SMartijn Coenen alloc->pid, pid, num_buffers, total_alloc_size);
508a7dc1e6fSHang Lu if (!alloc->oneway_spam_detected) {
509a7dc1e6fSHang Lu alloc->oneway_spam_detected = true;
510a7dc1e6fSHang Lu return true;
511261e7818SMartijn Coenen }
512261e7818SMartijn Coenen }
513a7dc1e6fSHang Lu return false;
514a7dc1e6fSHang Lu }
515261e7818SMartijn Coenen
516c7ac30faSCarlos Llamas /* Callers preallocate @new_buffer, it is freed by this function if unused */
binder_alloc_new_buf_locked(struct binder_alloc * alloc,struct binder_buffer * new_buffer,size_t size,int is_async)5173f827245SXiongwei Song static struct binder_buffer *binder_alloc_new_buf_locked(
5183f827245SXiongwei Song struct binder_alloc *alloc,
519c7ac30faSCarlos Llamas struct binder_buffer *new_buffer,
520377e1684SCarlos Llamas size_t size,
52189f71743SCarlos Llamas int is_async)
5220c972a05STodd Kjos {
5230c972a05STodd Kjos struct rb_node *n = alloc->free_buffers.rb_node;
5240c972a05STodd Kjos struct rb_node *best_fit = NULL;
525c7ac30faSCarlos Llamas struct binder_buffer *buffer;
52667dcc880SCarlos Llamas unsigned long next_used_page;
52767dcc880SCarlos Llamas unsigned long curr_last_page;
528c7ac30faSCarlos Llamas size_t buffer_size;
5290c972a05STodd Kjos
530c6d05e07SCarlos Llamas if (is_async && alloc->free_async_space < size) {
5310c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
5320c972a05STodd Kjos "%d: binder_alloc_buf size %zd failed, no async space left\n",
5330c972a05STodd Kjos alloc->pid, size);
534c7ac30faSCarlos Llamas buffer = ERR_PTR(-ENOSPC);
535c7ac30faSCarlos Llamas goto out;
5360c972a05STodd Kjos }
5370c972a05STodd Kjos
5380c972a05STodd Kjos while (n) {
5390c972a05STodd Kjos buffer = rb_entry(n, struct binder_buffer, rb_node);
5400c972a05STodd Kjos BUG_ON(!buffer->free);
5410c972a05STodd Kjos buffer_size = binder_alloc_buffer_size(alloc, buffer);
5420c972a05STodd Kjos
5430c972a05STodd Kjos if (size < buffer_size) {
5440c972a05STodd Kjos best_fit = n;
5450c972a05STodd Kjos n = n->rb_left;
546377e1684SCarlos Llamas } else if (size > buffer_size) {
5470c972a05STodd Kjos n = n->rb_right;
548377e1684SCarlos Llamas } else {
5490c972a05STodd Kjos best_fit = n;
5500c972a05STodd Kjos break;
5510c972a05STodd Kjos }
5520c972a05STodd Kjos }
553377e1684SCarlos Llamas
5549409af24SCarlos Llamas if (unlikely(!best_fit)) {
555128f3804SSherry Yang binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
556128f3804SSherry Yang "%d: binder_alloc_buf size %zd failed, no address space\n",
5570c972a05STodd Kjos alloc->pid, size);
5589409af24SCarlos Llamas debug_no_space_locked(alloc);
559c7ac30faSCarlos Llamas buffer = ERR_PTR(-ENOSPC);
560c7ac30faSCarlos Llamas goto out;
5610c972a05STodd Kjos }
5629409af24SCarlos Llamas
563de0e6573SCarlos Llamas if (buffer_size != size) {
564de0e6573SCarlos Llamas /* Found an oversized buffer and needs to be split */
5650c972a05STodd Kjos buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
5660c972a05STodd Kjos buffer_size = binder_alloc_buffer_size(alloc, buffer);
5670c972a05STodd Kjos
568de0e6573SCarlos Llamas WARN_ON(n || buffer_size == size);
569df9aabeaSCarlos Llamas new_buffer->user_data = buffer->user_data + size;
5700c972a05STodd Kjos list_add(&new_buffer->entry, &buffer->entry);
5710c972a05STodd Kjos new_buffer->free = 1;
5720c972a05STodd Kjos binder_insert_free_buffer(alloc, new_buffer);
573c7ac30faSCarlos Llamas new_buffer = NULL;
5740c972a05STodd Kjos }
57574310e06SSherry Yang
576de0e6573SCarlos Llamas binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
577de0e6573SCarlos Llamas "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
578de0e6573SCarlos Llamas alloc->pid, size, buffer, buffer_size);
579de0e6573SCarlos Llamas
58067dcc880SCarlos Llamas /*
58167dcc880SCarlos Llamas * Now we remove the pages from the freelist. A clever calculation
58267dcc880SCarlos Llamas * with buffer_size determines if the last page is shared with an
58367dcc880SCarlos Llamas * adjacent in-use buffer. In such case, the page has been already
58467dcc880SCarlos Llamas * removed from the freelist so we trim our range short.
58567dcc880SCarlos Llamas */
58667dcc880SCarlos Llamas next_used_page = (buffer->user_data + buffer_size) & PAGE_MASK;
58767dcc880SCarlos Llamas curr_last_page = PAGE_ALIGN(buffer->user_data + size);
588ea9cdbf0SCarlos Llamas binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data),
58967dcc880SCarlos Llamas min(next_used_page, curr_last_page));
590de0e6573SCarlos Llamas
591de0e6573SCarlos Llamas rb_erase(&buffer->rb_node, &alloc->free_buffers);
59274310e06SSherry Yang buffer->free = 0;
5937bada55aSTodd Kjos buffer->allow_user_free = 0;
59474310e06SSherry Yang binder_insert_allocated_buffer_locked(alloc, buffer);
5950c972a05STodd Kjos buffer->async_transaction = is_async;
596a7dc1e6fSHang Lu buffer->oneway_spam_suspect = false;
5970c972a05STodd Kjos if (is_async) {
598c6d05e07SCarlos Llamas alloc->free_async_space -= size;
5990c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
6000c972a05STodd Kjos "%d: binder_alloc_buf size %zd async free %zd\n",
6010c972a05STodd Kjos alloc->pid, size, alloc->free_async_space);
602c13500eaSCarlos Llamas if (debug_low_async_space_locked(alloc))
603c13500eaSCarlos Llamas buffer->oneway_spam_suspect = true;
6040c972a05STodd Kjos }
605377e1684SCarlos Llamas
606c7ac30faSCarlos Llamas out:
607c7ac30faSCarlos Llamas /* Discard possibly unused new_buffer */
608c7ac30faSCarlos Llamas kfree(new_buffer);
6090c972a05STodd Kjos return buffer;
6100c972a05STodd Kjos }
6110c972a05STodd Kjos
612377e1684SCarlos Llamas /* Calculate the sanitized total size, returns 0 for invalid request */
sanitized_size(size_t data_size,size_t offsets_size,size_t extra_buffers_size)613377e1684SCarlos Llamas static inline size_t sanitized_size(size_t data_size,
614377e1684SCarlos Llamas size_t offsets_size,
615377e1684SCarlos Llamas size_t extra_buffers_size)
616377e1684SCarlos Llamas {
617377e1684SCarlos Llamas size_t total, tmp;
618377e1684SCarlos Llamas
619377e1684SCarlos Llamas /* Align to pointer size and check for overflows */
620377e1684SCarlos Llamas tmp = ALIGN(data_size, sizeof(void *)) +
621377e1684SCarlos Llamas ALIGN(offsets_size, sizeof(void *));
622377e1684SCarlos Llamas if (tmp < data_size || tmp < offsets_size)
623377e1684SCarlos Llamas return 0;
624377e1684SCarlos Llamas total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
625377e1684SCarlos Llamas if (total < tmp || total < extra_buffers_size)
626377e1684SCarlos Llamas return 0;
627377e1684SCarlos Llamas
628377e1684SCarlos Llamas /* Pad 0-sized buffers so they get a unique address */
629377e1684SCarlos Llamas total = max(total, sizeof(void *));
630377e1684SCarlos Llamas
631377e1684SCarlos Llamas return total;
632377e1684SCarlos Llamas }
633377e1684SCarlos Llamas
6340c972a05STodd Kjos /**
6350c972a05STodd Kjos * binder_alloc_new_buf() - Allocate a new binder buffer
6360c972a05STodd Kjos * @alloc: binder_alloc for this proc
6370c972a05STodd Kjos * @data_size: size of user data buffer
6380c972a05STodd Kjos * @offsets_size: user specified buffer offset
6390c972a05STodd Kjos * @extra_buffers_size: size of extra space for meta-data (eg, security context)
6400c972a05STodd Kjos * @is_async: buffer for async transaction
6410c972a05STodd Kjos *
6420c972a05STodd Kjos * Allocate a new buffer given the requested sizes. Returns
6430c972a05STodd Kjos * the kernel version of the buffer pointer. The size allocated
6440c972a05STodd Kjos * is the sum of the three given sizes (each rounded up to
6450c972a05STodd Kjos * pointer-sized boundary)
6460c972a05STodd Kjos *
647e1090371SCarlos Llamas * Return: The allocated buffer or %ERR_PTR(-errno) if error
6480c972a05STodd Kjos */
binder_alloc_new_buf(struct binder_alloc * alloc,size_t data_size,size_t offsets_size,size_t extra_buffers_size,int is_async)6490c972a05STodd Kjos struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
6500c972a05STodd Kjos size_t data_size,
6510c972a05STodd Kjos size_t offsets_size,
6520c972a05STodd Kjos size_t extra_buffers_size,
65389f71743SCarlos Llamas int is_async)
6540c972a05STodd Kjos {
655c7ac30faSCarlos Llamas struct binder_buffer *buffer, *next;
656377e1684SCarlos Llamas size_t size;
6570c972a05STodd Kjos int ret;
6580c972a05STodd Kjos
6590c972a05STodd Kjos /* Check binder_alloc is fully initialized */
660072010abSCarlos Llamas if (!binder_alloc_is_mapped(alloc)) {
6610c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
6620c972a05STodd Kjos "%d: binder_alloc_buf, no vma\n",
6630c972a05STodd Kjos alloc->pid);
6640c972a05STodd Kjos return ERR_PTR(-ESRCH);
6650c972a05STodd Kjos }
6660c972a05STodd Kjos
667377e1684SCarlos Llamas size = sanitized_size(data_size, offsets_size, extra_buffers_size);
668377e1684SCarlos Llamas if (unlikely(!size)) {
6690c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
670377e1684SCarlos Llamas "%d: got transaction with invalid size %zd-%zd-%zd\n",
671377e1684SCarlos Llamas alloc->pid, data_size, offsets_size,
672377e1684SCarlos Llamas extra_buffers_size);
6730c972a05STodd Kjos return ERR_PTR(-EINVAL);
6740c972a05STodd Kjos }
6750c972a05STodd Kjos
676c7ac30faSCarlos Llamas /* Preallocate the next buffer */
677c7ac30faSCarlos Llamas next = kzalloc(sizeof(*next), GFP_KERNEL);
678c7ac30faSCarlos Llamas if (!next)
679c7ac30faSCarlos Llamas return ERR_PTR(-ENOMEM);
680c7ac30faSCarlos Llamas
6818b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
682c7ac30faSCarlos Llamas buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
683377e1684SCarlos Llamas if (IS_ERR(buffer)) {
6848b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
685377e1684SCarlos Llamas goto out;
6860c972a05STodd Kjos }
6870c972a05STodd Kjos
6880c972a05STodd Kjos buffer->data_size = data_size;
6890c972a05STodd Kjos buffer->offsets_size = offsets_size;
6900c972a05STodd Kjos buffer->extra_buffers_size = extra_buffers_size;
69189f71743SCarlos Llamas buffer->pid = current->tgid;
6928b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
6930c972a05STodd Kjos
69437ebbb4fSCarlos Llamas ret = binder_install_buffer_pages(alloc, buffer, size);
69537ebbb4fSCarlos Llamas if (ret) {
69637ebbb4fSCarlos Llamas binder_alloc_free_buf(alloc, buffer);
69737ebbb4fSCarlos Llamas buffer = ERR_PTR(ret);
6980c972a05STodd Kjos }
699377e1684SCarlos Llamas out:
7000c972a05STodd Kjos return buffer;
7010c972a05STodd Kjos }
7020c972a05STodd Kjos
buffer_start_page(struct binder_buffer * buffer)703df9aabeaSCarlos Llamas static unsigned long buffer_start_page(struct binder_buffer *buffer)
7040c972a05STodd Kjos {
705df9aabeaSCarlos Llamas return buffer->user_data & PAGE_MASK;
7060c972a05STodd Kjos }
7070c972a05STodd Kjos
prev_buffer_end_page(struct binder_buffer * buffer)708df9aabeaSCarlos Llamas static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
7090c972a05STodd Kjos {
710df9aabeaSCarlos Llamas return (buffer->user_data - 1) & PAGE_MASK;
7110c972a05STodd Kjos }
7120c972a05STodd Kjos
binder_delete_free_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer)7130c972a05STodd Kjos static void binder_delete_free_buffer(struct binder_alloc *alloc,
7140c972a05STodd Kjos struct binder_buffer *buffer)
7150c972a05STodd Kjos {
716f07b83a4SCarlos Llamas struct binder_buffer *prev, *next;
717f07b83a4SCarlos Llamas
718f07b83a4SCarlos Llamas if (PAGE_ALIGNED(buffer->user_data))
719f07b83a4SCarlos Llamas goto skip_freelist;
7204df9772cSMrinal Pandey
7210c972a05STodd Kjos BUG_ON(alloc->buffers.next == &buffer->entry);
722e2176219SSherry Yang prev = binder_buffer_prev(buffer);
7230c972a05STodd Kjos BUG_ON(!prev->free);
724f07b83a4SCarlos Llamas if (prev_buffer_end_page(prev) == buffer_start_page(buffer))
725f07b83a4SCarlos Llamas goto skip_freelist;
7260c972a05STodd Kjos
7270c972a05STodd Kjos if (!list_is_last(&buffer->entry, &alloc->buffers)) {
728e2176219SSherry Yang next = binder_buffer_next(buffer);
729f07b83a4SCarlos Llamas if (buffer_start_page(next) == buffer_start_page(buffer))
730f07b83a4SCarlos Llamas goto skip_freelist;
7310c972a05STodd Kjos }
73274310e06SSherry Yang
733ea9cdbf0SCarlos Llamas binder_lru_freelist_add(alloc, buffer_start_page(buffer),
7346ae33b9cSSherry Yang buffer_start_page(buffer) + PAGE_SIZE);
735f07b83a4SCarlos Llamas skip_freelist:
7360c972a05STodd Kjos list_del(&buffer->entry);
73774310e06SSherry Yang kfree(buffer);
7380c972a05STodd Kjos }
7390c972a05STodd Kjos
binder_free_buf_locked(struct binder_alloc * alloc,struct binder_buffer * buffer)7400c972a05STodd Kjos static void binder_free_buf_locked(struct binder_alloc *alloc,
7410c972a05STodd Kjos struct binder_buffer *buffer)
7420c972a05STodd Kjos {
7430c972a05STodd Kjos size_t size, buffer_size;
7440c972a05STodd Kjos
7450c972a05STodd Kjos buffer_size = binder_alloc_buffer_size(alloc, buffer);
7460c972a05STodd Kjos
7470c972a05STodd Kjos size = ALIGN(buffer->data_size, sizeof(void *)) +
7480c972a05STodd Kjos ALIGN(buffer->offsets_size, sizeof(void *)) +
7490c972a05STodd Kjos ALIGN(buffer->extra_buffers_size, sizeof(void *));
7500c972a05STodd Kjos
7510c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
7520c972a05STodd Kjos "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
7530c972a05STodd Kjos alloc->pid, buffer, size, buffer_size);
7540c972a05STodd Kjos
7550c972a05STodd Kjos BUG_ON(buffer->free);
7560c972a05STodd Kjos BUG_ON(size > buffer_size);
7570c972a05STodd Kjos BUG_ON(buffer->transaction != NULL);
7580a7bf686SCarlos Llamas BUG_ON(buffer->user_data < alloc->vm_start);
7590a7bf686SCarlos Llamas BUG_ON(buffer->user_data > alloc->vm_start + alloc->buffer_size);
7600c972a05STodd Kjos
7610c972a05STodd Kjos if (buffer->async_transaction) {
762c6d05e07SCarlos Llamas alloc->free_async_space += buffer_size;
7630c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
7640c972a05STodd Kjos "%d: binder_free_buf size %zd async free %zd\n",
7650c972a05STodd Kjos alloc->pid, size, alloc->free_async_space);
7660c972a05STodd Kjos }
7670c972a05STodd Kjos
768ea9cdbf0SCarlos Llamas binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data),
769df9aabeaSCarlos Llamas (buffer->user_data + buffer_size) & PAGE_MASK);
7700c972a05STodd Kjos
7710c972a05STodd Kjos rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
7720c972a05STodd Kjos buffer->free = 1;
7730c972a05STodd Kjos if (!list_is_last(&buffer->entry, &alloc->buffers)) {
774e2176219SSherry Yang struct binder_buffer *next = binder_buffer_next(buffer);
7750c972a05STodd Kjos
7760c972a05STodd Kjos if (next->free) {
7770c972a05STodd Kjos rb_erase(&next->rb_node, &alloc->free_buffers);
7780c972a05STodd Kjos binder_delete_free_buffer(alloc, next);
7790c972a05STodd Kjos }
7800c972a05STodd Kjos }
7810c972a05STodd Kjos if (alloc->buffers.next != &buffer->entry) {
782e2176219SSherry Yang struct binder_buffer *prev = binder_buffer_prev(buffer);
7830c972a05STodd Kjos
7840c972a05STodd Kjos if (prev->free) {
7850c972a05STodd Kjos binder_delete_free_buffer(alloc, buffer);
7860c972a05STodd Kjos rb_erase(&prev->rb_node, &alloc->free_buffers);
7870c972a05STodd Kjos buffer = prev;
7880c972a05STodd Kjos }
7890c972a05STodd Kjos }
7900c972a05STodd Kjos binder_insert_free_buffer(alloc, buffer);
7910c972a05STodd Kjos }
7920c972a05STodd Kjos
7931a7c3d9bSTodd Kjos /**
7941a7c3d9bSTodd Kjos * binder_alloc_get_page() - get kernel pointer for given buffer offset
7951a7c3d9bSTodd Kjos * @alloc: binder_alloc for this proc
7961a7c3d9bSTodd Kjos * @buffer: binder buffer to be accessed
7971a7c3d9bSTodd Kjos * @buffer_offset: offset into @buffer data
7981a7c3d9bSTodd Kjos * @pgoffp: address to copy final page offset to
7991a7c3d9bSTodd Kjos *
8001a7c3d9bSTodd Kjos * Lookup the struct page corresponding to the address
801bde4a19fSTodd Kjos * at @buffer_offset into @buffer->user_data. If @pgoffp is not
8021a7c3d9bSTodd Kjos * NULL, the byte-offset into the page is written there.
8031a7c3d9bSTodd Kjos *
8041a7c3d9bSTodd Kjos * The caller is responsible to ensure that the offset points
8051a7c3d9bSTodd Kjos * to a valid address within the @buffer and that @buffer is
8061a7c3d9bSTodd Kjos * not freeable by the user. Since it can't be freed, we are
8071a7c3d9bSTodd Kjos * guaranteed that the corresponding elements of @alloc->pages[]
8081a7c3d9bSTodd Kjos * cannot change.
8091a7c3d9bSTodd Kjos *
8101a7c3d9bSTodd Kjos * Return: struct page
8111a7c3d9bSTodd Kjos */
binder_alloc_get_page(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t buffer_offset,pgoff_t * pgoffp)8121a7c3d9bSTodd Kjos static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
8131a7c3d9bSTodd Kjos struct binder_buffer *buffer,
8141a7c3d9bSTodd Kjos binder_size_t buffer_offset,
8151a7c3d9bSTodd Kjos pgoff_t *pgoffp)
8161a7c3d9bSTodd Kjos {
8171a7c3d9bSTodd Kjos binder_size_t buffer_space_offset = buffer_offset +
8180a7bf686SCarlos Llamas (buffer->user_data - alloc->vm_start);
8191a7c3d9bSTodd Kjos pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
8201a7c3d9bSTodd Kjos size_t index = buffer_space_offset >> PAGE_SHIFT;
8211a7c3d9bSTodd Kjos
8221a7c3d9bSTodd Kjos *pgoffp = pgoff;
823f909f030SCarlos Llamas
824f909f030SCarlos Llamas return alloc->pages[index];
8251a7c3d9bSTodd Kjos }
8261a7c3d9bSTodd Kjos
8271a7c3d9bSTodd Kjos /**
8280f966cbaSTodd Kjos * binder_alloc_clear_buf() - zero out buffer
8290f966cbaSTodd Kjos * @alloc: binder_alloc for this proc
8300f966cbaSTodd Kjos * @buffer: binder buffer to be cleared
8310f966cbaSTodd Kjos *
8320f966cbaSTodd Kjos * memset the given buffer to 0
8330f966cbaSTodd Kjos */
binder_alloc_clear_buf(struct binder_alloc * alloc,struct binder_buffer * buffer)8340f966cbaSTodd Kjos static void binder_alloc_clear_buf(struct binder_alloc *alloc,
8350f966cbaSTodd Kjos struct binder_buffer *buffer)
8360f966cbaSTodd Kjos {
8370f966cbaSTodd Kjos size_t bytes = binder_alloc_buffer_size(alloc, buffer);
8380f966cbaSTodd Kjos binder_size_t buffer_offset = 0;
8390f966cbaSTodd Kjos
8400f966cbaSTodd Kjos while (bytes) {
8410f966cbaSTodd Kjos unsigned long size;
8420f966cbaSTodd Kjos struct page *page;
8430f966cbaSTodd Kjos pgoff_t pgoff;
8440f966cbaSTodd Kjos
8450f966cbaSTodd Kjos page = binder_alloc_get_page(alloc, buffer,
8460f966cbaSTodd Kjos buffer_offset, &pgoff);
8470f966cbaSTodd Kjos size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
84826eff2d6SFabio M. De Francesco memset_page(page, pgoff, 0, size);
8490f966cbaSTodd Kjos bytes -= size;
8500f966cbaSTodd Kjos buffer_offset += size;
8510f966cbaSTodd Kjos }
8520f966cbaSTodd Kjos }
8530f966cbaSTodd Kjos
8540f966cbaSTodd Kjos /**
8550c972a05STodd Kjos * binder_alloc_free_buf() - free a binder buffer
8560c972a05STodd Kjos * @alloc: binder_alloc for this proc
8570c972a05STodd Kjos * @buffer: kernel pointer to buffer
8580c972a05STodd Kjos *
8590c972a05STodd Kjos * Free the buffer allocated via binder_alloc_new_buf()
8600c972a05STodd Kjos */
binder_alloc_free_buf(struct binder_alloc * alloc,struct binder_buffer * buffer)8610c972a05STodd Kjos void binder_alloc_free_buf(struct binder_alloc *alloc,
8620c972a05STodd Kjos struct binder_buffer *buffer)
8630c972a05STodd Kjos {
8640c972a05STodd Kjos /*
8650c972a05STodd Kjos * We could eliminate the call to binder_alloc_clear_buf()
8660c972a05STodd Kjos * from binder_alloc_deferred_release() by moving this to
867122a3c1cSCarlos Llamas * binder_free_buf_locked(). However, that could
8688b52c726SCarlos Llamas * increase contention for the alloc mutex if clear_on_free
8698b52c726SCarlos Llamas * is used frequently for large buffers. The mutex is not
8700c972a05STodd Kjos * needed for correctness here.
8710c972a05STodd Kjos */
8720c972a05STodd Kjos if (buffer->clear_on_free) {
8730c972a05STodd Kjos binder_alloc_clear_buf(alloc, buffer);
8740c972a05STodd Kjos buffer->clear_on_free = false;
8750c972a05STodd Kjos }
8768b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
8770c972a05STodd Kjos binder_free_buf_locked(alloc, buffer);
8788b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
8790c972a05STodd Kjos }
8800c972a05STodd Kjos
8810c972a05STodd Kjos /**
8820c972a05STodd Kjos * binder_alloc_mmap_handler() - map virtual address space for proc
8830c972a05STodd Kjos * @alloc: alloc structure for this proc
8840c972a05STodd Kjos * @vma: vma passed to mmap()
8850c972a05STodd Kjos *
8860c972a05STodd Kjos * Called by binder_mmap() to initialize the space specified in
8870c972a05STodd Kjos * vma for allocating binder buffers
8880c972a05STodd Kjos *
8890c972a05STodd Kjos * Return:
8900c972a05STodd Kjos * 0 = success
8910c972a05STodd Kjos * -EBUSY = address space already mapped
8920c972a05STodd Kjos * -ENOMEM = failed to map memory to given address space
8930c972a05STodd Kjos */
binder_alloc_mmap_handler(struct binder_alloc * alloc,struct vm_area_struct * vma)8940c972a05STodd Kjos int binder_alloc_mmap_handler(struct binder_alloc *alloc,
8950c972a05STodd Kjos struct vm_area_struct *vma)
8960c972a05STodd Kjos {
8970c972a05STodd Kjos struct binder_buffer *buffer;
89868aef12dSCarlos Llamas const char *failure_string;
899f909f030SCarlos Llamas int ret;
9000c972a05STodd Kjos
9010c972a05STodd Kjos if (unlikely(vma->vm_mm != alloc->mm)) {
9020c972a05STodd Kjos ret = -EINVAL;
9030c972a05STodd Kjos failure_string = "invalid vma->vm_mm";
9040c972a05STodd Kjos goto err_invalid_mm;
9050c972a05STodd Kjos }
9060c972a05STodd Kjos
9070c972a05STodd Kjos mutex_lock(&binder_alloc_mmap_lock);
9080c972a05STodd Kjos if (alloc->buffer_size) {
9090c972a05STodd Kjos ret = -EBUSY;
9100c972a05STodd Kjos failure_string = "already mapped";
9110c972a05STodd Kjos goto err_already_mapped;
9120c972a05STodd Kjos }
9130c972a05STodd Kjos alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
9140c972a05STodd Kjos SZ_4M);
9150c972a05STodd Kjos mutex_unlock(&binder_alloc_mmap_lock);
9160c972a05STodd Kjos
9170a7bf686SCarlos Llamas alloc->vm_start = vma->vm_start;
9180c972a05STodd Kjos
91936c55ce8SLei Liu alloc->pages = kvcalloc(alloc->buffer_size / PAGE_SIZE,
9200c972a05STodd Kjos sizeof(alloc->pages[0]),
9210c972a05STodd Kjos GFP_KERNEL);
922f909f030SCarlos Llamas if (!alloc->pages) {
9230c972a05STodd Kjos ret = -ENOMEM;
9240c972a05STodd Kjos failure_string = "alloc page array";
925aac6830eSGanesh Mahendran goto err_alloc_pages_failed;
9260c972a05STodd Kjos }
9270c972a05STodd Kjos
9280c972a05STodd Kjos buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
9290c972a05STodd Kjos if (!buffer) {
9300c972a05STodd Kjos ret = -ENOMEM;
9310c972a05STodd Kjos failure_string = "alloc buffer struct";
9320c972a05STodd Kjos goto err_alloc_buf_struct_failed;
9330c972a05STodd Kjos }
9340c972a05STodd Kjos
9350a7bf686SCarlos Llamas buffer->user_data = alloc->vm_start;
9360c972a05STodd Kjos list_add(&buffer->entry, &alloc->buffers);
9370c972a05STodd Kjos buffer->free = 1;
9380c972a05STodd Kjos binder_insert_free_buffer(alloc, buffer);
9390c972a05STodd Kjos alloc->free_async_space = alloc->buffer_size / 2;
9400c972a05STodd Kjos
9410c972a05STodd Kjos /* Signal binder_alloc is fully initialized */
942072010abSCarlos Llamas binder_alloc_set_mapped(alloc, true);
9430c972a05STodd Kjos
9440c972a05STodd Kjos return 0;
9450c972a05STodd Kjos
9460c972a05STodd Kjos err_alloc_buf_struct_failed:
94736c55ce8SLei Liu kvfree(alloc->pages);
9480c972a05STodd Kjos alloc->pages = NULL;
9496396bb22SKees Cook err_alloc_pages_failed:
9500a7bf686SCarlos Llamas alloc->vm_start = 0;
9510c972a05STodd Kjos mutex_lock(&binder_alloc_mmap_lock);
9520c972a05STodd Kjos alloc->buffer_size = 0;
9530c972a05STodd Kjos err_already_mapped:
9540c972a05STodd Kjos mutex_unlock(&binder_alloc_mmap_lock);
9550c972a05STodd Kjos err_invalid_mm:
9560c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
9570c972a05STodd Kjos "%s: %d %lx-%lx %s failed %d\n", __func__,
9580c972a05STodd Kjos alloc->pid, vma->vm_start, vma->vm_end,
95974310e06SSherry Yang failure_string, ret);
96074310e06SSherry Yang return ret;
9610c972a05STodd Kjos }
96274310e06SSherry Yang
96374310e06SSherry Yang
binder_alloc_deferred_release(struct binder_alloc * alloc)9640c972a05STodd Kjos void binder_alloc_deferred_release(struct binder_alloc *alloc)
96574310e06SSherry Yang {
96674310e06SSherry Yang struct rb_node *n;
9670c972a05STodd Kjos int buffers, page_count;
9680c972a05STodd Kjos struct binder_buffer *buffer;
9690c972a05STodd Kjos
9700c972a05STodd Kjos buffers = 0;
9718b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
972072010abSCarlos Llamas BUG_ON(alloc->mapped);
9730c972a05STodd Kjos
9740c972a05STodd Kjos while ((n = rb_first(&alloc->allocated_buffers))) {
9750c972a05STodd Kjos buffer = rb_entry(n, struct binder_buffer, rb_node);
9760c972a05STodd Kjos
9770c972a05STodd Kjos /* Transaction should already have been freed */
9780c972a05STodd Kjos BUG_ON(buffer->transaction);
979da1b9564SMinchan Kim
980a0c2baafSSherry Yang if (buffer->clear_on_free) {
9810c972a05STodd Kjos binder_alloc_clear_buf(alloc, buffer);
9820c972a05STodd Kjos buffer->clear_on_free = false;
9830c972a05STodd Kjos }
98474310e06SSherry Yang binder_free_buf_locked(alloc, buffer);
9850c972a05STodd Kjos buffers++;
9860c972a05STodd Kjos }
9870c972a05STodd Kjos
9880c972a05STodd Kjos while (!list_empty(&alloc->buffers)) {
9890c972a05STodd Kjos buffer = list_first_entry(&alloc->buffers,
9900c972a05STodd Kjos struct binder_buffer, entry);
9910c972a05STodd Kjos WARN_ON(!buffer->free);
9920c972a05STodd Kjos
9930c972a05STodd Kjos list_del(&buffer->entry);
994128f3804SSherry Yang WARN_ON_ONCE(!list_empty(&alloc->buffers));
995128f3804SSherry Yang kfree(buffer);
996128f3804SSherry Yang }
997128f3804SSherry Yang
9980c972a05STodd Kjos page_count = 0;
9990c972a05STodd Kjos if (alloc->pages) {
10000c972a05STodd Kjos int i;
10010c972a05STodd Kjos
10020c972a05STodd Kjos for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
1003f909f030SCarlos Llamas struct page *page;
100474310e06SSherry Yang bool on_lru;
10050c972a05STodd Kjos
1006f909f030SCarlos Llamas page = binder_get_installed_page(alloc, i);
1007f909f030SCarlos Llamas if (!page)
10080c972a05STodd Kjos continue;
1009da1b9564SMinchan Kim
101049d2562cSCarlos Llamas on_lru = list_lru_del(&binder_freelist,
1011f909f030SCarlos Llamas page_to_lru(page),
1012f909f030SCarlos Llamas page_to_nid(page),
101349d2562cSCarlos Llamas NULL);
10140c972a05STodd Kjos binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
1015162c7973SCarlos Llamas "%s: %d: page %d %s\n",
1016162c7973SCarlos Llamas __func__, alloc->pid, i,
10170c972a05STodd Kjos on_lru ? "on lru" : "active");
1018f909f030SCarlos Llamas binder_free_page(page);
10190c972a05STodd Kjos page_count++;
10200c972a05STodd Kjos }
102174310e06SSherry Yang }
10228b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
10232c10a20fSMukesh Ojha kvfree(alloc->pages);
102474310e06SSherry Yang if (alloc->mm)
102574310e06SSherry Yang mmdrop(alloc->mm);
102674310e06SSherry Yang
102774310e06SSherry Yang binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
102874310e06SSherry Yang "%s: %d buffers %d, pages %d\n",
102974310e06SSherry Yang __func__, alloc->pid, buffers, page_count);
103074310e06SSherry Yang }
103174310e06SSherry Yang
10320c972a05STodd Kjos /**
1033f2517eb7SSherry Yang * binder_alloc_print_allocated() - print buffer info
1034f2517eb7SSherry Yang * @m: seq_file for output via seq_printf()
10350c972a05STodd Kjos * @alloc: binder_alloc for this proc
10360c972a05STodd Kjos *
1037f2517eb7SSherry Yang * Prints information about every buffer associated with
1038f2517eb7SSherry Yang * the binder_alloc state to the given seq_file
1039f2517eb7SSherry Yang */
binder_alloc_print_allocated(struct seq_file * m,struct binder_alloc * alloc)10400c972a05STodd Kjos void binder_alloc_print_allocated(struct seq_file *m,
1041f2517eb7SSherry Yang struct binder_alloc *alloc)
10420c972a05STodd Kjos {
10438e905217SCarlos Llamas struct binder_buffer *buffer;
10440c972a05STodd Kjos struct rb_node *n;
10450c972a05STodd Kjos
10468b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
10478e905217SCarlos Llamas for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
10488e905217SCarlos Llamas buffer = rb_entry(n, struct binder_buffer, rb_node);
10498e905217SCarlos Llamas seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n",
1050162c7973SCarlos Llamas buffer->debug_id,
10510a7bf686SCarlos Llamas buffer->user_data - alloc->vm_start,
10528e905217SCarlos Llamas buffer->data_size, buffer->offsets_size,
10538e905217SCarlos Llamas buffer->extra_buffers_size,
10548e905217SCarlos Llamas buffer->transaction ? "active" : "delivered");
10558e905217SCarlos Llamas }
10568b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
10570c972a05STodd Kjos }
10580c972a05STodd Kjos
10590c972a05STodd Kjos /**
10600c972a05STodd Kjos * binder_alloc_print_pages() - print page usage
10610c972a05STodd Kjos * @m: seq_file for output via seq_printf()
10620c972a05STodd Kjos * @alloc: binder_alloc for this proc
10630c972a05STodd Kjos */
binder_alloc_print_pages(struct seq_file * m,struct binder_alloc * alloc)10640c972a05STodd Kjos void binder_alloc_print_pages(struct seq_file *m,
10650c972a05STodd Kjos struct binder_alloc *alloc)
1066b05a68e9SMartijn Coenen {
1067f909f030SCarlos Llamas struct page *page;
10680c972a05STodd Kjos int i;
1069b05a68e9SMartijn Coenen int active = 0;
10700c972a05STodd Kjos int lru = 0;
10710c972a05STodd Kjos int free = 0;
10720c972a05STodd Kjos
10738b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
10740c972a05STodd Kjos /*
10750c972a05STodd Kjos * Make sure the binder_alloc is fully initialized, otherwise we might
10760c972a05STodd Kjos * read inconsistent state.
10770c972a05STodd Kjos */
1078072010abSCarlos Llamas if (binder_alloc_is_mapped(alloc)) {
10790c972a05STodd Kjos for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
1080f909f030SCarlos Llamas page = binder_get_installed_page(alloc, i);
1081f909f030SCarlos Llamas if (!page)
10820c972a05STodd Kjos free++;
1083f909f030SCarlos Llamas else if (list_empty(page_to_lru(page)))
10840c972a05STodd Kjos active++;
10850c972a05STodd Kjos else
10860c972a05STodd Kjos lru++;
10870c972a05STodd Kjos }
10880c972a05STodd Kjos }
10898b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
10908ef4665aSSherry Yang seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
10918ef4665aSSherry Yang seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
10928ef4665aSSherry Yang }
10938ef4665aSSherry Yang
10948ef4665aSSherry Yang /**
10958ef4665aSSherry Yang * binder_alloc_get_allocated_count() - return count of buffers
10968ef4665aSSherry Yang * @alloc: binder_alloc for this proc
10978ef4665aSSherry Yang *
10988ef4665aSSherry Yang * Return: count of allocated buffers
10998ef4665aSSherry Yang */
binder_alloc_get_allocated_count(struct binder_alloc * alloc)11008ef4665aSSherry Yang int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
11018ef4665aSSherry Yang {
11028ef4665aSSherry Yang struct rb_node *n;
11038ef4665aSSherry Yang int count = 0;
11048ef4665aSSherry Yang
11058b52c726SCarlos Llamas mutex_lock(&alloc->mutex);
11068ef4665aSSherry Yang for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
11078ef4665aSSherry Yang count++;
11088b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
11098ef4665aSSherry Yang return count;
11108ef4665aSSherry Yang }
11118ef4665aSSherry Yang
11128ef4665aSSherry Yang
11138ef4665aSSherry Yang /**
11148ef4665aSSherry Yang * binder_alloc_vma_close() - invalidate address space
11158ef4665aSSherry Yang * @alloc: binder_alloc for this proc
11168ef4665aSSherry Yang *
11178ef4665aSSherry Yang * Called from binder_vma_close() when releasing address space.
1118072010abSCarlos Llamas * Clears alloc->mapped to prevent new incoming transactions from
11190c972a05STodd Kjos * allocating more buffers.
11200c972a05STodd Kjos */
binder_alloc_vma_close(struct binder_alloc * alloc)11210c972a05STodd Kjos void binder_alloc_vma_close(struct binder_alloc *alloc)
11220c972a05STodd Kjos {
1123072010abSCarlos Llamas binder_alloc_set_mapped(alloc, false);
11240c972a05STodd Kjos }
11250c972a05STodd Kjos
11260c972a05STodd Kjos /**
11270c972a05STodd Kjos * binder_alloc_free_page() - shrinker callback to free pages
11280c972a05STodd Kjos * @item: item to free
1129da0c0251SKairui Song * @lru: list_lru instance of the item
11300c972a05STodd Kjos * @cb_arg: callback argument
11310c972a05STodd Kjos *
11320c972a05STodd Kjos * Called from list_lru_walk() in binder_shrink_scan() to free
11330c972a05STodd Kjos * up pages when the system is under memory pressure.
11340c972a05STodd Kjos */
binder_alloc_free_page(struct list_head * item,struct list_lru_one * lru,void * cb_arg)11350c972a05STodd Kjos enum lru_status binder_alloc_free_page(struct list_head *item,
11360c972a05STodd Kjos struct list_lru_one *lru,
11370c972a05STodd Kjos void *cb_arg)
1138da0c0251SKairui Song __must_hold(&lru->lock)
11390c972a05STodd Kjos {
1140f909f030SCarlos Llamas struct binder_shrinker_mdata *mdata = container_of(item, typeof(*mdata), lru);
1141f909f030SCarlos Llamas struct binder_alloc *alloc = mdata->alloc;
1142e50f4e6cSCarlos Llamas struct mm_struct *mm = alloc->mm;
1143e50f4e6cSCarlos Llamas struct vm_area_struct *vma;
1144e50f4e6cSCarlos Llamas struct page *page_to_free;
1145df9aabeaSCarlos Llamas unsigned long page_addr;
1146*95bc2d4aSCarlos Llamas int mm_locked = 0;
1147f2517eb7SSherry Yang size_t index;
1148f2517eb7SSherry Yang
1149e50f4e6cSCarlos Llamas if (!mmget_not_zero(mm))
1150e50f4e6cSCarlos Llamas goto err_mmget;
1151f2517eb7SSherry Yang
1152f909f030SCarlos Llamas index = mdata->page_index;
11530a7bf686SCarlos Llamas page_addr = alloc->vm_start + index * PAGE_SIZE;
1154f2517eb7SSherry Yang
1155*95bc2d4aSCarlos Llamas /* attempt per-vma lock first */
1156*95bc2d4aSCarlos Llamas vma = lock_vma_under_rcu(mm, page_addr);
1157*95bc2d4aSCarlos Llamas if (!vma) {
1158*95bc2d4aSCarlos Llamas /* fall back to mmap_lock */
1159*95bc2d4aSCarlos Llamas if (!mmap_read_trylock(mm))
1160*95bc2d4aSCarlos Llamas goto err_mmap_read_lock_failed;
1161*95bc2d4aSCarlos Llamas mm_locked = 1;
11623f489c20SCarlos Llamas vma = vma_lookup(mm, page_addr);
1163*95bc2d4aSCarlos Llamas }
1164*95bc2d4aSCarlos Llamas
1165*95bc2d4aSCarlos Llamas if (!mutex_trylock(&alloc->mutex))
1166*95bc2d4aSCarlos Llamas goto err_get_alloc_mutex_failed;
1167*95bc2d4aSCarlos Llamas
1168072010abSCarlos Llamas /*
1169072010abSCarlos Llamas * Since a binder_alloc can only be mapped once, we ensure
1170072010abSCarlos Llamas * the vma corresponds to this mapping by checking whether
1171072010abSCarlos Llamas * the binder_alloc is still mapped.
1172072010abSCarlos Llamas */
1173072010abSCarlos Llamas if (vma && !binder_alloc_is_mapped(alloc))
11743f489c20SCarlos Llamas goto err_invalid_vma;
1175e41e164cSSherry Yang
1176e50f4e6cSCarlos Llamas trace_binder_unmap_kernel_start(alloc, index);
1177e50f4e6cSCarlos Llamas
1178f909f030SCarlos Llamas page_to_free = alloc->pages[index];
1179f909f030SCarlos Llamas binder_set_installed_page(alloc, index, NULL);
1180e50f4e6cSCarlos Llamas
1181e50f4e6cSCarlos Llamas trace_binder_unmap_kernel_end(alloc, index);
1182e50f4e6cSCarlos Llamas
1183e41e164cSSherry Yang list_lru_isolate(lru, item);
1184da0c0251SKairui Song spin_unlock(&lru->lock);
1185f2517eb7SSherry Yang
1186a1b2289cSSherry Yang if (vma) {
1187f2517eb7SSherry Yang trace_binder_unmap_user_start(alloc, index);
1188f2517eb7SSherry Yang
1189a1b2289cSSherry Yang zap_page_range_single(vma, page_addr, PAGE_SIZE, NULL);
1190a0c2baafSSherry Yang
1191f2517eb7SSherry Yang trace_binder_unmap_user_end(alloc, index);
1192f2517eb7SSherry Yang }
1193e50f4e6cSCarlos Llamas
1194d1716b4bSCarlos Llamas mutex_unlock(&alloc->mutex);
1195*95bc2d4aSCarlos Llamas if (mm_locked)
1196f2517eb7SSherry Yang mmap_read_unlock(mm);
1197*95bc2d4aSCarlos Llamas else
1198*95bc2d4aSCarlos Llamas vma_end_read(vma);
1199f2517eb7SSherry Yang mmput_async(mm);
1200f909f030SCarlos Llamas binder_free_page(page_to_free);
1201f2517eb7SSherry Yang
1202f2517eb7SSherry Yang return LRU_REMOVED_RETRY;
1203f2517eb7SSherry Yang
12043f489c20SCarlos Llamas err_invalid_vma:
12058b52c726SCarlos Llamas mutex_unlock(&alloc->mutex);
12068b52c726SCarlos Llamas err_get_alloc_mutex_failed:
1207*95bc2d4aSCarlos Llamas if (mm_locked)
12083f489c20SCarlos Llamas mmap_read_unlock(mm);
1209*95bc2d4aSCarlos Llamas else
1210*95bc2d4aSCarlos Llamas vma_end_read(vma);
1211f2517eb7SSherry Yang err_mmap_read_lock_failed:
1212f2517eb7SSherry Yang mmput_async(mm);
1213f2517eb7SSherry Yang err_mmget:
1214f2517eb7SSherry Yang return LRU_SKIP;
1215de7bbe3dSSherry Yang }
1216f2517eb7SSherry Yang
1217f2517eb7SSherry Yang static unsigned long
binder_shrink_count(struct shrinker * shrink,struct shrink_control * sc)1218f2517eb7SSherry Yang binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1219f2517eb7SSherry Yang {
1220ea9cdbf0SCarlos Llamas return list_lru_count(&binder_freelist);
1221f2517eb7SSherry Yang }
12220c972a05STodd Kjos
12230c972a05STodd Kjos static unsigned long
binder_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)12240c972a05STodd Kjos binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
12250c972a05STodd Kjos {
1226ea9cdbf0SCarlos Llamas return list_lru_walk(&binder_freelist, binder_alloc_free_page,
12270c972a05STodd Kjos NULL, sc->nr_to_scan);
12280c972a05STodd Kjos }
12290c972a05STodd Kjos
12300c972a05STodd Kjos static struct shrinker *binder_shrinker;
12310c972a05STodd Kjos
1232957ccc2bSSherry Yang /**
12330c972a05STodd Kjos * binder_alloc_init() - called by binder_open() for per-proc initialization
12340c972a05STodd Kjos * @alloc: binder_alloc for this proc
1235533dfb25STetsuo Handa *
1236f2517eb7SSherry Yang * Called from binder_open() to initialize binder_alloc fields for
1237533dfb25STetsuo Handa * new binder proc
1238533dfb25STetsuo Handa */
binder_alloc_init(struct binder_alloc * alloc)1239533dfb25STetsuo Handa void binder_alloc_init(struct binder_alloc *alloc)
1240533dfb25STetsuo Handa {
1241533dfb25STetsuo Handa alloc->pid = current->group_leader->pid;
1242533dfb25STetsuo Handa alloc->mm = current->mm;
1243533dfb25STetsuo Handa mmgrab(alloc->mm);
12448b52c726SCarlos Llamas mutex_init(&alloc->mutex);
12451a7c3d9bSTodd Kjos INIT_LIST_HEAD(&alloc->buffers);
12461a7c3d9bSTodd Kjos }
12471a7c3d9bSTodd Kjos
binder_alloc_shrinker_init(void)12481a7c3d9bSTodd Kjos int binder_alloc_shrinker_init(void)
12491a7c3d9bSTodd Kjos {
12501a7c3d9bSTodd Kjos int ret;
12511a7c3d9bSTodd Kjos
1252ea9cdbf0SCarlos Llamas ret = list_lru_init(&binder_freelist);
12531a7c3d9bSTodd Kjos if (ret)
12541a7c3d9bSTodd Kjos return ret;
12551a7c3d9bSTodd Kjos
12561a7c3d9bSTodd Kjos binder_shrinker = shrinker_alloc(0, "android-binder");
12571a7c3d9bSTodd Kjos if (!binder_shrinker) {
1258ea9cdbf0SCarlos Llamas list_lru_destroy(&binder_freelist);
12591a7c3d9bSTodd Kjos return -ENOMEM;
12601a7c3d9bSTodd Kjos }
12611a7c3d9bSTodd Kjos
12621a7c3d9bSTodd Kjos binder_shrinker->count_objects = binder_shrink_count;
12631a7c3d9bSTodd Kjos binder_shrinker->scan_objects = binder_shrink_scan;
12641a7c3d9bSTodd Kjos
12651a7c3d9bSTodd Kjos shrinker_register(binder_shrinker);
12661a7c3d9bSTodd Kjos
12671a7c3d9bSTodd Kjos return 0;
12681a7c3d9bSTodd Kjos }
12691a7c3d9bSTodd Kjos
binder_alloc_shrinker_exit(void)12701a7c3d9bSTodd Kjos void binder_alloc_shrinker_exit(void)
12711a7c3d9bSTodd Kjos {
12721a7c3d9bSTodd Kjos shrinker_free(binder_shrinker);
1273ea9cdbf0SCarlos Llamas list_lru_destroy(&binder_freelist);
12741a7c3d9bSTodd Kjos }
12751a7c3d9bSTodd Kjos
12761a7c3d9bSTodd Kjos /**
12771a7c3d9bSTodd Kjos * check_buffer() - verify that buffer/offset is safe to access
12781a7c3d9bSTodd Kjos * @alloc: binder_alloc for this proc
12791a7c3d9bSTodd Kjos * @buffer: binder buffer to be accessed
12801a7c3d9bSTodd Kjos * @offset: offset into @buffer data
12811a7c3d9bSTodd Kjos * @bytes: bytes to access from offset
12821a7c3d9bSTodd Kjos *
12831a7c3d9bSTodd Kjos * Check that the @offset/@bytes are within the size of the given
12841a7c3d9bSTodd Kjos * @buffer and that the buffer is currently active and not freeable.
12851a7c3d9bSTodd Kjos * Offsets must also be multiples of sizeof(u32). The kernel is
12861a7c3d9bSTodd Kjos * allowed to touch the buffer in two cases:
12871a7c3d9bSTodd Kjos *
12881a7c3d9bSTodd Kjos * 1) when the buffer is being created:
12891a7c3d9bSTodd Kjos * (buffer->free == 0 && buffer->allow_user_free == 0)
12901a7c3d9bSTodd Kjos * 2) when the buffer is being torn down:
12911a7c3d9bSTodd Kjos * (buffer->free == 0 && buffer->transaction == NULL).
12921a7c3d9bSTodd Kjos *
12931a7c3d9bSTodd Kjos * Return: true if the buffer is safe to access
12941a7c3d9bSTodd Kjos */
check_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t offset,size_t bytes)12951a7c3d9bSTodd Kjos static inline bool check_buffer(struct binder_alloc *alloc,
12961a7c3d9bSTodd Kjos struct binder_buffer *buffer,
12971a7c3d9bSTodd Kjos binder_size_t offset, size_t bytes)
12981a7c3d9bSTodd Kjos {
12991a7c3d9bSTodd Kjos size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
13001a7c3d9bSTodd Kjos
13011a7c3d9bSTodd Kjos return buffer_size >= bytes &&
13021a7c3d9bSTodd Kjos offset <= buffer_size - bytes &&
13031a7c3d9bSTodd Kjos IS_ALIGNED(offset, sizeof(u32)) &&
13041a7c3d9bSTodd Kjos !buffer->free &&
13051a7c3d9bSTodd Kjos (!buffer->allow_user_free || !buffer->transaction);
13061a7c3d9bSTodd Kjos }
13071a7c3d9bSTodd Kjos
13081a7c3d9bSTodd Kjos /**
13091a7c3d9bSTodd Kjos * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
13101a7c3d9bSTodd Kjos * @alloc: binder_alloc for this proc
13111a7c3d9bSTodd Kjos * @buffer: binder buffer to be accessed
13121a7c3d9bSTodd Kjos * @buffer_offset: offset into @buffer data
13131a7c3d9bSTodd Kjos * @from: userspace pointer to source buffer
13141a7c3d9bSTodd Kjos * @bytes: bytes to copy
13151a7c3d9bSTodd Kjos *
13161a7c3d9bSTodd Kjos * Copy bytes from source userspace to target buffer.
13171a7c3d9bSTodd Kjos *
13181a7c3d9bSTodd Kjos * Return: bytes remaining to be copied
13191a7c3d9bSTodd Kjos */
13201a7c3d9bSTodd Kjos unsigned long
binder_alloc_copy_user_to_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t buffer_offset,const void __user * from,size_t bytes)13211a7c3d9bSTodd Kjos binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
13221a7c3d9bSTodd Kjos struct binder_buffer *buffer,
13231a7c3d9bSTodd Kjos binder_size_t buffer_offset,
13241a7c3d9bSTodd Kjos const void __user *from,
13251a7c3d9bSTodd Kjos size_t bytes)
13261a7c3d9bSTodd Kjos {
13271a7c3d9bSTodd Kjos if (!check_buffer(alloc, buffer, buffer_offset, bytes))
13281a7c3d9bSTodd Kjos return bytes;
13291a7c3d9bSTodd Kjos
13301a7c3d9bSTodd Kjos while (bytes) {
13311a7c3d9bSTodd Kjos unsigned long size;
13321a7c3d9bSTodd Kjos unsigned long ret;
13331a7c3d9bSTodd Kjos struct page *page;
13341a7c3d9bSTodd Kjos pgoff_t pgoff;
13351a7c3d9bSTodd Kjos void *kptr;
13361a7c3d9bSTodd Kjos
13371a7c3d9bSTodd Kjos page = binder_alloc_get_page(alloc, buffer,
13381a7c3d9bSTodd Kjos buffer_offset, &pgoff);
13391a7c3d9bSTodd Kjos size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
13401d625960SFabio M. De Francesco kptr = kmap_local_page(page) + pgoff;
13411a7c3d9bSTodd Kjos ret = copy_from_user(kptr, from, size);
13421d625960SFabio M. De Francesco kunmap_local(kptr);
13431a7c3d9bSTodd Kjos if (ret)
13441a7c3d9bSTodd Kjos return bytes - size + ret;
13451a7c3d9bSTodd Kjos bytes -= size;
13461a7c3d9bSTodd Kjos from += size;
13471a7c3d9bSTodd Kjos buffer_offset += size;
13481a7c3d9bSTodd Kjos }
13491a7c3d9bSTodd Kjos return 0;
13501a7c3d9bSTodd Kjos }
13518ced0c62STodd Kjos
binder_alloc_do_buffer_copy(struct binder_alloc * alloc,bool to_buffer,struct binder_buffer * buffer,binder_size_t buffer_offset,void * ptr,size_t bytes)1352bb4a2e48STodd Kjos static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
13538ced0c62STodd Kjos bool to_buffer,
13548ced0c62STodd Kjos struct binder_buffer *buffer,
13558ced0c62STodd Kjos binder_size_t buffer_offset,
13568ced0c62STodd Kjos void *ptr,
13578ced0c62STodd Kjos size_t bytes)
13588ced0c62STodd Kjos {
13598ced0c62STodd Kjos /* All copies must be 32-bit aligned and 32-bit size */
1360bb4a2e48STodd Kjos if (!check_buffer(alloc, buffer, buffer_offset, bytes))
1361bb4a2e48STodd Kjos return -EINVAL;
13628ced0c62STodd Kjos
13638ced0c62STodd Kjos while (bytes) {
13648ced0c62STodd Kjos unsigned long size;
13658ced0c62STodd Kjos struct page *page;
13668ced0c62STodd Kjos pgoff_t pgoff;
13678ced0c62STodd Kjos
13688ced0c62STodd Kjos page = binder_alloc_get_page(alloc, buffer,
13698ced0c62STodd Kjos buffer_offset, &pgoff);
13708ced0c62STodd Kjos size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
13718ced0c62STodd Kjos if (to_buffer)
1372e88a6a8fSFabio M. De Francesco memcpy_to_page(page, pgoff, ptr, size);
13738ced0c62STodd Kjos else
1374e88a6a8fSFabio M. De Francesco memcpy_from_page(ptr, page, pgoff, size);
13758ced0c62STodd Kjos bytes -= size;
13768ced0c62STodd Kjos pgoff = 0;
13778ced0c62STodd Kjos ptr = ptr + size;
13788ced0c62STodd Kjos buffer_offset += size;
13798ced0c62STodd Kjos }
1380bb4a2e48STodd Kjos return 0;
13818ced0c62STodd Kjos }
13828ced0c62STodd Kjos
binder_alloc_copy_to_buffer(struct binder_alloc * alloc,struct binder_buffer * buffer,binder_size_t buffer_offset,void * src,size_t bytes)1383bb4a2e48STodd Kjos int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
13848ced0c62STodd Kjos struct binder_buffer *buffer,
13858ced0c62STodd Kjos binder_size_t buffer_offset,
13868ced0c62STodd Kjos void *src,
13878ced0c62STodd Kjos size_t bytes)
13888ced0c62STodd Kjos {
1389bb4a2e48STodd Kjos return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
13908ced0c62STodd Kjos src, bytes);
13918ced0c62STodd Kjos }
13928ced0c62STodd Kjos
binder_alloc_copy_from_buffer(struct binder_alloc * alloc,void * dest,struct binder_buffer * buffer,binder_size_t buffer_offset,size_t bytes)1393bb4a2e48STodd Kjos int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
13948ced0c62STodd Kjos void *dest,
13958ced0c62STodd Kjos struct binder_buffer *buffer,
13968ced0c62STodd Kjos binder_size_t buffer_offset,
13978ced0c62STodd Kjos size_t bytes)
13988ced0c62STodd Kjos {
1399bb4a2e48STodd Kjos return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
14008ced0c62STodd Kjos dest, bytes);
14018ced0c62STodd Kjos }
1402