1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* drivers/gpu/ion/ion_mem_pool.c
*
* Copyright (C) 2011 Google, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/debugfs.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/slab.h>
#include "ion_priv.h"
static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
{
struct page *page = alloc_pages(pool->gfp_mask, pool->order);
if (!page)
return NULL;
/* this is only being used to flush the page for dma,
this api is not really suitable for calling from a driver
but no better way to flush a page for dma exist at this time */
#ifndef CONFIG_ION_EXYNOS
arm_dma_ops.sync_single_for_device(NULL,
pfn_to_dma(NULL, page_to_pfn(page)),
PAGE_SIZE << pool->order,
DMA_BIDIRECTIONAL);
#endif
return page;
}
static void ion_page_pool_free_pages(struct ion_page_pool *pool,
struct page *page)
{
__free_pages(page, pool->order);
}
static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
{
BUG_ON(page->lru.next != LIST_POISON1 ||
page->lru.prev != LIST_POISON2);
mutex_lock(&pool->mutex);
if (PageHighMem(page)) {
list_add_tail(&page->lru, &pool->high_items);
pool->high_count++;
} else {
list_add_tail(&page->lru, &pool->low_items);
pool->low_count++;
}
mutex_unlock(&pool->mutex);
return 0;
}
static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
{
struct page *page;
if (high) {
BUG_ON(!pool->high_count);
page = list_first_entry(&pool->high_items, struct page, lru);
pool->high_count--;
} else {
BUG_ON(!pool->low_count);
page = list_first_entry(&pool->low_items, struct page, lru);
pool->low_count--;
}
list_del(&page->lru);
return page;
}
void *ion_page_pool_alloc(struct ion_page_pool *pool,
bool try_again, bool *from_pool)
{
struct page *page = NULL;
BUG_ON(!pool);
mutex_lock(&pool->mutex);
if (pool->high_count)
page = ion_page_pool_remove(pool, true);
else if (pool->low_count)
page = ion_page_pool_remove(pool, false);
mutex_unlock(&pool->mutex);
if (!page && try_again) {
page = ion_page_pool_alloc_pages(pool);
*from_pool = false;
} else {
*from_pool = true;
}
return page;
}
void ion_page_pool_free(struct ion_page_pool *pool, struct page* page)
{
int ret;
ret = ion_page_pool_add(pool, page);
if (ret)
ion_page_pool_free_pages(pool, page);
}
static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
{
int total = 0;
total = high ? (pool->high_count + pool->low_count) *
(1 << pool->order) :
pool->low_count * (1 << pool->order);
return total;
}
static bool __init_pages_for_preload(struct page *page, int order,
bool zero, bool flush)
{
int n_pages = 1 << order;
struct page **pages = NULL;
int i;
void *va;
bool ret = true;
if (!zero && !flush)
return true;
if (PageHighMem(page)) {
pages = kmalloc(sizeof(*pages) * n_pages, GFP_KERNEL);
if (!pages) {
pr_warn("%s: aborted due to nomemory (order %d)\n",
__func__, order);
return false;
}
for (i = 0; i < n_pages; i++)
pages[i] = &page[i];
va = vm_map_ram(pages, n_pages, 0, PAGE_KERNEL);
if (!va) {
pr_warn("%s: aborted due to novm (order %d)\n",
__func__, order);
ret = false;
goto err_vm_map;
}
} else {
va = page_address(page);
}
if (zero)
memset(va, 0, n_pages * PAGE_SIZE);
if (flush)
dmac_flush_range(va, va + n_pages * PAGE_SIZE);
if (PageHighMem(page))
vm_unmap_ram(va, n_pages);
err_vm_map:
if (PageHighMem(page))
kfree(pages);
return ret;
}
/*
* This function is called for order-0 page preloading to prevent
* holding too many order-0 pages in the pool and to relieve memory
* fragmentation to make the larger order page population successful later
*/
void ion_page_pool_preload_prepare(struct ion_page_pool *pool, long num_pages)
{
long pages_in_pool = pool->high_count + pool->low_count;
long freed = 0;
BUG_ON(pool->order != 0);
while (pages_in_pool-- > num_pages) {
struct page *page;
mutex_lock(&pool->mutex);
page = ion_page_pool_remove(pool, !pool->low_count);
mutex_unlock(&pool->mutex);
if (!page)
break;
ion_page_pool_free_pages(pool, page);
freed++;
}
if (freed)
pr_info("%s: %ld order-0 pages are shrinked\n",
__func__, freed);
}
long ion_page_pool_preload(struct ion_page_pool *pool,
struct ion_page_pool *alt_pool,
unsigned int alloc_flags, long num_pages)
{
long pages_required;
BUG_ON(pool->order != alt_pool->order);
/*
* the number of pages to preload needs to be tuned because the
* preloaded can be allocated to another thread that did not request
* preloading. Because we have no better idea about the optimal number
* of pages to preload currently, this function just tries that the pool
* has enough pages for the preload request.
*/
pages_required = num_pages - (pool->high_count + pool->low_count);
pr_info("%s: order %d pages requested - %ld, to preload - %ld\n",
__func__, pool->order, num_pages, pages_required);
if (pages_required <= 0)
return num_pages;
/* enlarge the target pool first */
while (pages_required > 0) {
struct page *page;
page = alloc_pages(pool->gfp_mask, pool->order);
if (!page)
break;
if (!__init_pages_for_preload(page, pool->order,
true, !(alloc_flags & ION_FLAG_CACHED))) {
__free_pages(page, pool->order);
return pages_required;
}
if (ion_page_pool_add(pool, page)) {
__free_pages(page, pool->order);
pr_warn("%s: aborted due to nomemory (order %d)\n",
__func__, pool->order);
return pages_required;
}
pages_required--;
}
/* take pages from alternative pool if the page allocator fails */
while (pages_required > 0) {
struct page *page;
bool from_pool; /* no use */
page = ion_page_pool_alloc(alt_pool, false, &from_pool);
if (!page) {
pr_warn("%s: aborted due to no page (order %d)\n",
__func__, pool->order);
break;
}
if (!__init_pages_for_preload(page, pool->order,
false, !(alloc_flags & ION_FLAG_CACHED))) {
/*
* instead of returning the page to the pool,
* just free the page due to lack of memory.
*/
__free_pages(page, pool->order);
pages_required++; /* to be decremented below */
}
if (ion_page_pool_add(pool, page)) {
__free_pages(page, pool->order);
pr_warn("%s: aborted due to nomemory (order %d)\n",
__func__, pool->order);
return pages_required;
}
pages_required--;
}
if (pages_required > 0)
pr_warn("%s: %ld pages are not preloaded for order %d\n",
__func__, pages_required, pool->order);
return num_pages - pages_required;
}
int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
int nr_to_scan)
{
int nr_freed = 0;
bool high;
high = gfp_mask & __GFP_HIGHMEM;
if (nr_to_scan == 0)
return ion_page_pool_total(pool, high);
while (nr_freed < nr_to_scan) {
struct page *page;
mutex_lock(&pool->mutex);
if (pool->low_count) {
page = ion_page_pool_remove(pool, false);
} else if (high && pool->high_count) {
page = ion_page_pool_remove(pool, true);
} else {
mutex_unlock(&pool->mutex);
break;
}
mutex_unlock(&pool->mutex);
ion_page_pool_free_pages(pool, page);
nr_freed += (1 << pool->order);
}
return nr_freed;
}
struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order)
{
struct ion_page_pool *pool = kmalloc(sizeof(struct ion_page_pool),
GFP_KERNEL);
if (!pool)
return NULL;
pool->high_count = 0;
pool->low_count = 0;
INIT_LIST_HEAD(&pool->low_items);
INIT_LIST_HEAD(&pool->high_items);
pool->gfp_mask = gfp_mask;
pool->order = order;
mutex_init(&pool->mutex);
plist_node_init(&pool->list, order);
return pool;
}
void ion_page_pool_destroy(struct ion_page_pool *pool)
{
kfree(pool);
}
static int __init ion_page_pool_init(void)
{
return 0;
}
static void __exit ion_page_pool_exit(void)
{
}
module_init(ion_page_pool_init);
module_exit(ion_page_pool_exit);
|