]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - drivers/staging/android/ion/ion_page_pool.c
staging: android: ion: Remove crufty cache support
[thirdparty/kernel/linux.git] / drivers / staging / android / ion / ion_page_pool.c
CommitLineData
0214c7f2
RSZ
1/*
2 * drivers/staging/android/ion/ion_mem_pool.c
3 *
4 * Copyright (C) 2011 Google, Inc.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
797a95c4 17#include <linux/debugfs.h>
0214c7f2
RSZ
18#include <linux/dma-mapping.h>
19#include <linux/err.h>
797a95c4 20#include <linux/fs.h>
0214c7f2 21#include <linux/list.h>
30d79792 22#include <linux/init.h>
0214c7f2 23#include <linux/slab.h>
0cd2dc4d 24#include <linux/swap.h>
0214c7f2
RSZ
25#include "ion_priv.h"
26
0214c7f2
RSZ
27static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
28{
29 struct page *page = alloc_pages(pool->gfp_mask, pool->order);
30
31 if (!page)
32 return NULL;
0214c7f2
RSZ
33 return page;
34}
35
36static void ion_page_pool_free_pages(struct ion_page_pool *pool,
37 struct page *page)
38{
39 __free_pages(page, pool->order);
40}
41
42static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
43{
efee5a0c 44 mutex_lock(&pool->mutex);
0fb9b815 45 if (PageHighMem(page)) {
38c003b1 46 list_add_tail(&page->lru, &pool->high_items);
0fb9b815
RSZ
47 pool->high_count++;
48 } else {
38c003b1 49 list_add_tail(&page->lru, &pool->low_items);
0fb9b815
RSZ
50 pool->low_count++;
51 }
efee5a0c 52 mutex_unlock(&pool->mutex);
0214c7f2
RSZ
53 return 0;
54}
55
0fb9b815 56static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
0214c7f2 57{
0214c7f2
RSZ
58 struct page *page;
59
0fb9b815
RSZ
60 if (high) {
61 BUG_ON(!pool->high_count);
38c003b1 62 page = list_first_entry(&pool->high_items, struct page, lru);
0fb9b815
RSZ
63 pool->high_count--;
64 } else {
65 BUG_ON(!pool->low_count);
38c003b1 66 page = list_first_entry(&pool->low_items, struct page, lru);
0fb9b815
RSZ
67 pool->low_count--;
68 }
0214c7f2 69
38c003b1 70 list_del(&page->lru);
0214c7f2
RSZ
71 return page;
72}
73
79240748 74struct page *ion_page_pool_alloc(struct ion_page_pool *pool)
0214c7f2
RSZ
75{
76 struct page *page = NULL;
77
78 BUG_ON(!pool);
79
80 mutex_lock(&pool->mutex);
0fb9b815
RSZ
81 if (pool->high_count)
82 page = ion_page_pool_remove(pool, true);
83 else if (pool->low_count)
84 page = ion_page_pool_remove(pool, false);
0214c7f2
RSZ
85 mutex_unlock(&pool->mutex);
86
0fb9b815
RSZ
87 if (!page)
88 page = ion_page_pool_alloc_pages(pool);
89
0214c7f2
RSZ
90 return page;
91}
92
e1d855b0 93void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
0214c7f2
RSZ
94{
95 int ret;
96
bdeb9f1c
HS
97 BUG_ON(pool->order != compound_order(page));
98
0214c7f2
RSZ
99 ret = ion_page_pool_add(pool, page);
100 if (ret)
101 ion_page_pool_free_pages(pool, page);
0214c7f2
RSZ
102}
103
ea313b5f 104static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
797a95c4 105{
80cb77dc 106 int count = pool->low_count;
797a95c4 107
80cb77dc
HS
108 if (high)
109 count += pool->high_count;
110
111 return count << pool->order;
797a95c4
RSZ
112}
113
ea313b5f 114int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
679011bd 115 int nr_to_scan)
0214c7f2 116{
b44d9ce3 117 int freed = 0;
0fb9b815
RSZ
118 bool high;
119
0cd2dc4d 120 if (current_is_kswapd())
17fbab1e 121 high = true;
0cd2dc4d
HS
122 else
123 high = !!(gfp_mask & __GFP_HIGHMEM);
0214c7f2 124
797a95c4 125 if (nr_to_scan == 0)
ea313b5f
RSZ
126 return ion_page_pool_total(pool, high);
127
b44d9ce3 128 while (freed < nr_to_scan) {
ea313b5f
RSZ
129 struct page *page;
130
131 mutex_lock(&pool->mutex);
ce3d1093 132 if (pool->low_count) {
ea313b5f 133 page = ion_page_pool_remove(pool, false);
ce3d1093
CC
134 } else if (high && pool->high_count) {
135 page = ion_page_pool_remove(pool, true);
ea313b5f 136 } else {
0fb9b815 137 mutex_unlock(&pool->mutex);
ea313b5f 138 break;
0214c7f2 139 }
ea313b5f
RSZ
140 mutex_unlock(&pool->mutex);
141 ion_page_pool_free_pages(pool, page);
b44d9ce3 142 freed += (1 << pool->order);
0214c7f2 143 }
0214c7f2 144
b9daf0b6 145 return freed;
0214c7f2
RSZ
146}
147
e7f63771
CF
148struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
149 bool cached)
0214c7f2 150{
8fb78ad6
BM
151 struct ion_page_pool *pool = kmalloc(sizeof(*pool), GFP_KERNEL);
152
0214c7f2
RSZ
153 if (!pool)
154 return NULL;
0fb9b815
RSZ
155 pool->high_count = 0;
156 pool->low_count = 0;
157 INIT_LIST_HEAD(&pool->low_items);
158 INIT_LIST_HEAD(&pool->high_items);
bdeb9f1c 159 pool->gfp_mask = gfp_mask | __GFP_COMP;
0214c7f2
RSZ
160 pool->order = order;
161 mutex_init(&pool->mutex);
797a95c4 162 plist_node_init(&pool->list, order);
e7f63771
CF
163 if (cached)
164 pool->cached = true;
0214c7f2
RSZ
165
166 return pool;
167}
168
169void ion_page_pool_destroy(struct ion_page_pool *pool)
170{
0214c7f2
RSZ
171 kfree(pool);
172}
173
797a95c4
RSZ
174static int __init ion_page_pool_init(void)
175{
797a95c4
RSZ
176 return 0;
177}
30d79792 178device_initcall(ion_page_pool_init);