blob: e6b06c81b5daa1304b867a021cf83e8a76d17282 [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
40 * @allow_user_free: %true if user is allowed to free buffer
41 * @async_transaction: %true if buffer is in use for an async txn
42 * @debug_id: unique ID for debugging
43 * @transaction: pointer to associated struct binder_transaction
44 * @target_node: struct binder_node associated with this buffer
45 * @data_size: size of @transaction data
46 * @offsets_size: size of array of offsets
47 * @extra_buffers_size: size of space for other objects (like sg lists)
Todd Kjos8539b1e2019-02-08 10:35:20 -080048 * @user_data: user pointer to base of buffer space
Martijn Coenen6e126c42020-08-21 14:25:44 +020049 * @pid: pid to attribute the buffer to (caller)
Todd Kjos076072a2017-04-21 14:32:11 -070050 *
51 * Bookkeeping structure for binder transaction buffers
52 */
53struct binder_buffer {
54 struct list_head entry; /* free and allocated entries by address */
55 struct rb_node rb_node; /* free entry by size or allocated entry */
56 /* by address */
57 unsigned free:1;
58 unsigned allow_user_free:1;
59 unsigned async_transaction:1;
Todd Kjosd29b73e2018-11-06 15:55:32 -080060 unsigned debug_id:29;
Todd Kjos076072a2017-04-21 14:32:11 -070061
62 struct binder_transaction *transaction;
63
64 struct binder_node *target_node;
65 size_t data_size;
66 size_t offsets_size;
67 size_t extra_buffers_size;
Todd Kjos8539b1e2019-02-08 10:35:20 -080068 void __user *user_data;
Martijn Coenen6e126c42020-08-21 14:25:44 +020069 int pid;
Todd Kjos076072a2017-04-21 14:32:11 -070070};
71
72/**
Sherry Yang5828d702017-07-29 13:24:11 -070073 * struct binder_lru_page - page object used for binder shrinker
74 * @page_ptr: pointer to physical page in mmap'd space
75 * @lru: entry in binder_alloc_lru
76 * @alloc: binder_alloc for a proc
77 */
78struct binder_lru_page {
79 struct list_head lru;
80 struct page *page_ptr;
81 struct binder_alloc *alloc;
82};
83
84/**
Todd Kjos076072a2017-04-21 14:32:11 -070085 * struct binder_alloc - per-binder proc state for binder allocator
86 * @vma: vm_area_struct passed to mmap_handler
87 * (invarient after mmap)
88 * @tsk: tid for task that called init for this proc
89 * (invariant after init)
90 * @vma_vm_mm: copy of vma->vm_mm (invarient after mmap)
91 * @buffer: base of per-proc address space mapped via mmap
Todd Kjos076072a2017-04-21 14:32:11 -070092 * @buffers: list of all buffers for this proc
93 * @free_buffers: rb tree of buffers available for allocation
94 * sorted by size
95 * @allocated_buffers: rb tree of allocated buffers sorted by address
96 * @free_async_space: VA space available for async buffers. This is
97 * initialized at mmap time to 1/2 the full VA space
Sherry Yang5828d702017-07-29 13:24:11 -070098 * @pages: array of binder_lru_page
Todd Kjos076072a2017-04-21 14:32:11 -070099 * @buffer_size: size of address space specified via mmap
100 * @pid: pid for associated binder_proc (invariant after init)
Martijn Coenenc05ec292017-10-24 16:37:39 +0200101 * @pages_high: high watermark of offset in @pages
Todd Kjos076072a2017-04-21 14:32:11 -0700102 *
103 * Bookkeeping structure for per-proc address space management for binder
104 * buffers. It is normally initialized during binder_init() and binder_mmap()
105 * calls. The address space is used for both user-visible buffers and for
106 * struct binder_buffer objects used to track the user buffers
107 */
108struct binder_alloc {
109 struct mutex mutex;
Todd Kjos076072a2017-04-21 14:32:11 -0700110 struct vm_area_struct *vma;
111 struct mm_struct *vma_vm_mm;
Todd Kjos8539b1e2019-02-08 10:35:20 -0800112 void __user *buffer;
Todd Kjos076072a2017-04-21 14:32:11 -0700113 struct list_head buffers;
114 struct rb_root free_buffers;
115 struct rb_root allocated_buffers;
116 size_t free_async_space;
Sherry Yang5828d702017-07-29 13:24:11 -0700117 struct binder_lru_page *pages;
Todd Kjos076072a2017-04-21 14:32:11 -0700118 size_t buffer_size;
119 uint32_t buffer_free;
120 int pid;
Martijn Coenenc05ec292017-10-24 16:37:39 +0200121 size_t pages_high;
Todd Kjos076072a2017-04-21 14:32:11 -0700122};
123
Sherry Yang435416b2017-06-22 14:37:45 -0700124#ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST
125void binder_selftest_alloc(struct binder_alloc *alloc);
126#else
127static inline void binder_selftest_alloc(struct binder_alloc *alloc) {}
128#endif
Sherry Yang5828d702017-07-29 13:24:11 -0700129enum lru_status binder_alloc_free_page(struct list_head *item,
130 struct list_lru_one *lru,
131 spinlock_t *lock, void *cb_arg);
Todd Kjos076072a2017-04-21 14:32:11 -0700132extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
133 size_t data_size,
134 size_t offsets_size,
135 size_t extra_buffers_size,
Martijn Coenen6e126c42020-08-21 14:25:44 +0200136 int is_async,
137 int pid);
Todd Kjos076072a2017-04-21 14:32:11 -0700138extern void binder_alloc_init(struct binder_alloc *alloc);
Tetsuo Handaf8cb8222017-11-29 22:29:47 +0900139extern int binder_alloc_shrinker_init(void);
Todd Kjos076072a2017-04-21 14:32:11 -0700140extern void binder_alloc_vma_close(struct binder_alloc *alloc);
141extern struct binder_buffer *
142binder_alloc_prepare_to_free(struct binder_alloc *alloc,
143 uintptr_t user_ptr);
144extern void binder_alloc_free_buf(struct binder_alloc *alloc,
145 struct binder_buffer *buffer);
146extern int binder_alloc_mmap_handler(struct binder_alloc *alloc,
147 struct vm_area_struct *vma);
148extern void binder_alloc_deferred_release(struct binder_alloc *alloc);
149extern int binder_alloc_get_allocated_count(struct binder_alloc *alloc);
150extern void binder_alloc_print_allocated(struct seq_file *m,
151 struct binder_alloc *alloc);
Sherry Yang91004422017-08-22 17:26:57 -0700152void binder_alloc_print_pages(struct seq_file *m,
153 struct binder_alloc *alloc);
Todd Kjos076072a2017-04-21 14:32:11 -0700154
155/**
156 * binder_alloc_get_free_async_space() - get free space available for async
157 * @alloc: binder_alloc for this proc
158 *
159 * Return: the bytes remaining in the address-space for async transactions
160 */
161static inline size_t
162binder_alloc_get_free_async_space(struct binder_alloc *alloc)
163{
164 size_t free_async_space;
165
166 mutex_lock(&alloc->mutex);
167 free_async_space = alloc->free_async_space;
168 mutex_unlock(&alloc->mutex);
169 return free_async_space;
170}
171
Todd Kjosd5049492019-02-08 10:35:14 -0800172unsigned long
173binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
174 struct binder_buffer *buffer,
175 binder_size_t buffer_offset,
176 const void __user *from,
177 size_t bytes);
178
Todd Kjos90a570c2019-02-08 10:35:15 -0800179void binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
180 struct binder_buffer *buffer,
181 binder_size_t buffer_offset,
182 void *src,
183 size_t bytes);
184
185void binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
186 void *dest,
187 struct binder_buffer *buffer,
188 binder_size_t buffer_offset,
189 size_t bytes);
190
Todd Kjos076072a2017-04-21 14:32:11 -0700191#endif /* _LINUX_BINDER_ALLOC_H */
192