blob: da025cc94cd9a971a9880afdc3855a269da824b5 [file] [log] [blame]
Todd Kjos076072a2017-04-21 14:32:11 -07001/*
2 * Copyright (C) 2017 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#ifndef _LINUX_BINDER_ALLOC_H
16#define _LINUX_BINDER_ALLOC_H
17
18#include <linux/rbtree.h>
19#include <linux/list.h>
20#include <linux/mm.h>
21#include <linux/rtmutex.h>
22#include <linux/vmalloc.h>
23#include <linux/slab.h>
Sherry Yang5828d702017-07-29 13:24:11 -070024#include <linux/list_lru.h>
wya893c9f02019-07-08 13:16:21 +080025
26#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
27#define BINDER_IPC_32BIT 1
28#endif
29
Todd Kjosd5049492019-02-08 10:35:14 -080030#include <uapi/linux/android/binder.h>
Todd Kjos076072a2017-04-21 14:32:11 -070031
Sherry Yang5828d702017-07-29 13:24:11 -070032extern struct list_lru binder_alloc_lru;
Todd Kjos076072a2017-04-21 14:32:11 -070033struct binder_transaction;
34
35/**
36 * struct binder_buffer - buffer used for binder transactions
37 * @entry: entry alloc->buffers
38 * @rb_node: node for allocated_buffers/free_buffers rb trees
Todd Kjose60bc942018-12-05 15:19:25 -080039 * @free: %true if buffer is free
Todd Kjos97c54a12020-11-20 15:37:43 -080040 * @clear_on_free: %true if buffer must be zeroed after use
Todd Kjose60bc942018-12-05 15:19:25 -080041 * @allow_user_free: %true if user is allowed to free buffer
42 * @async_transaction: %true if buffer is in use for an async txn
43 * @debug_id: unique ID for debugging
44 * @transaction: pointer to associated struct binder_transaction
45 * @target_node: struct binder_node associated with this buffer
46 * @data_size: size of @transaction data
47 * @offsets_size: size of array of offsets
48 * @extra_buffers_size: size of space for other objects (like sg lists)
Todd Kjos8539b1e2019-02-08 10:35:20 -080049 * @user_data: user pointer to base of buffer space
Martijn Coenen6e126c42020-08-21 14:25:44 +020050 * @pid: pid to attribute the buffer to (caller)
Todd Kjos076072a2017-04-21 14:32:11 -070051 *
52 * Bookkeeping structure for binder transaction buffers
53 */
54struct binder_buffer {
55 struct list_head entry; /* free and allocated entries by address */
56 struct rb_node rb_node; /* free entry by size or allocated entry */
57 /* by address */
58 unsigned free:1;
Todd Kjos97c54a12020-11-20 15:37:43 -080059 unsigned clear_on_free:1;
Todd Kjos076072a2017-04-21 14:32:11 -070060 unsigned allow_user_free:1;
61 unsigned async_transaction:1;
Todd Kjos97c54a12020-11-20 15:37:43 -080062 unsigned debug_id:28;
Todd Kjos076072a2017-04-21 14:32:11 -070063
64 struct binder_transaction *transaction;
65
66 struct binder_node *target_node;
67 size_t data_size;
68 size_t offsets_size;
69 size_t extra_buffers_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -080070 void __user *user_data;
Martijn Coenen6e126c42020-08-21 14:25:44 +020071 int pid;
Todd Kjos076072a2017-04-21 14:32:11 -070072};
73
74/**
Sherry Yang5828d702017-07-29 13:24:11 -070075 * struct binder_lru_page - page object used for binder shrinker
76 * @page_ptr: pointer to physical page in mmap'd space
77 * @lru: entry in binder_alloc_lru
78 * @alloc: binder_alloc for a proc
79 */
80struct binder_lru_page {
81 struct list_head lru;
82 struct page *page_ptr;
83 struct binder_alloc *alloc;
84};
85
86/**
Todd Kjos076072a2017-04-21 14:32:11 -070087 * struct binder_alloc - per-binder proc state for binder allocator
88 * @vma: vm_area_struct passed to mmap_handler
89 * (invarient after mmap)
90 * @tsk: tid for task that called init for this proc
91 * (invariant after init)
92 * @vma_vm_mm: copy of vma->vm_mm (invarient after mmap)
93 * @buffer: base of per-proc address space mapped via mmap
Todd Kjos076072a2017-04-21 14:32:11 -070094 * @buffers: list of all buffers for this proc
95 * @free_buffers: rb tree of buffers available for allocation
96 * sorted by size
97 * @allocated_buffers: rb tree of allocated buffers sorted by address
98 * @free_async_space: VA space available for async buffers. This is
99 * initialized at mmap time to 1/2 the full VA space
Sherry Yang5828d702017-07-29 13:24:11 -0700100 * @pages: array of binder_lru_page
Todd Kjos076072a2017-04-21 14:32:11 -0700101 * @buffer_size: size of address space specified via mmap
102 * @pid: pid for associated binder_proc (invariant after init)
Martijn Coenenc05ec292017-10-24 16:37:39 +0200103 * @pages_high: high watermark of offset in @pages
Todd Kjos076072a2017-04-21 14:32:11 -0700104 *
105 * Bookkeeping structure for per-proc address space management for binder
106 * buffers. It is normally initialized during binder_init() and binder_mmap()
107 * calls. The address space is used for both user-visible buffers and for
108 * struct binder_buffer objects used to track the user buffers
109 */
110struct binder_alloc {
111 struct mutex mutex;
Todd Kjos076072a2017-04-21 14:32:11 -0700112 struct vm_area_struct *vma;
113 struct mm_struct *vma_vm_mm;
Todd Kjos8539b1e2019-02-08 10:35:20 -0800114 void __user *buffer;
Todd Kjos076072a2017-04-21 14:32:11 -0700115 struct list_head buffers;
116 struct rb_root free_buffers;
117 struct rb_root allocated_buffers;
118 size_t free_async_space;
Sherry Yang5828d702017-07-29 13:24:11 -0700119 struct binder_lru_page *pages;
Todd Kjos076072a2017-04-21 14:32:11 -0700120 size_t buffer_size;
121 uint32_t buffer_free;
122 int pid;
Martijn Coenenc05ec292017-10-24 16:37:39 +0200123 size_t pages_high;
Todd Kjos076072a2017-04-21 14:32:11 -0700124};
125
Sherry Yang435416b2017-06-22 14:37:45 -0700126#ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST
127void binder_selftest_alloc(struct binder_alloc *alloc);
128#else
129static inline void binder_selftest_alloc(struct binder_alloc *alloc) {}
130#endif
Sherry Yang5828d702017-07-29 13:24:11 -0700131enum lru_status binder_alloc_free_page(struct list_head *item,
132 struct list_lru_one *lru,
133 spinlock_t *lock, void *cb_arg);
Todd Kjos076072a2017-04-21 14:32:11 -0700134extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
135 size_t data_size,
136 size_t offsets_size,
137 size_t extra_buffers_size,
Martijn Coenen6e126c42020-08-21 14:25:44 +0200138 int is_async,
139 int pid);
Todd Kjos076072a2017-04-21 14:32:11 -0700140extern void binder_alloc_init(struct binder_alloc *alloc);
Tetsuo Handaf8cb8222017-11-29 22:29:47 +0900141extern int binder_alloc_shrinker_init(void);
Todd Kjos076072a2017-04-21 14:32:11 -0700142extern void binder_alloc_vma_close(struct binder_alloc *alloc);
143extern struct binder_buffer *
144binder_alloc_prepare_to_free(struct binder_alloc *alloc,
145 uintptr_t user_ptr);
146extern void binder_alloc_free_buf(struct binder_alloc *alloc,
147 struct binder_buffer *buffer);
148extern int binder_alloc_mmap_handler(struct binder_alloc *alloc,
149 struct vm_area_struct *vma);
150extern void binder_alloc_deferred_release(struct binder_alloc *alloc);
151extern int binder_alloc_get_allocated_count(struct binder_alloc *alloc);
152extern void binder_alloc_print_allocated(struct seq_file *m,
153 struct binder_alloc *alloc);
Sherry Yang91004422017-08-22 17:26:57 -0700154void binder_alloc_print_pages(struct seq_file *m,
155 struct binder_alloc *alloc);
Todd Kjos076072a2017-04-21 14:32:11 -0700156
157/**
158 * binder_alloc_get_free_async_space() - get free space available for async
159 * @alloc: binder_alloc for this proc
160 *
161 * Return: the bytes remaining in the address-space for async transactions
162 */
163static inline size_t
164binder_alloc_get_free_async_space(struct binder_alloc *alloc)
165{
166 size_t free_async_space;
167
168 mutex_lock(&alloc->mutex);
169 free_async_space = alloc->free_async_space;
170 mutex_unlock(&alloc->mutex);
171 return free_async_space;
172}
173
Todd Kjosd5049492019-02-08 10:35:14 -0800174unsigned long
175binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
176 struct binder_buffer *buffer,
177 binder_size_t buffer_offset,
178 const void __user *from,
179 size_t bytes);
180
Todd Kjos90a570c2019-02-08 10:35:15 -0800181void binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
182 struct binder_buffer *buffer,
183 binder_size_t buffer_offset,
184 void *src,
185 size_t bytes);
186
187void binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
188 void *dest,
189 struct binder_buffer *buffer,
190 binder_size_t buffer_offset,
191 size_t bytes);
192
Todd Kjos076072a2017-04-21 14:32:11 -0700193#endif /* _LINUX_BINDER_ALLOC_H */
194