]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - 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
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
17 #include <linux/debugfs.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/fs.h>
21 #include <linux/list.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/swap.h>
25 #include "ion_priv.h"
26
27 static 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;
33 return page;
34 }
35
36 static void ion_page_pool_free_pages(struct ion_page_pool *pool,
37 struct page *page)
38 {
39 __free_pages(page, pool->order);
40 }
41
42 static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
43 {
44 mutex_lock(&pool->mutex);
45 if (PageHighMem(page)) {
46 list_add_tail(&page->lru, &pool->high_items);
47 pool->high_count++;
48 } else {
49 list_add_tail(&page->lru, &pool->low_items);
50 pool->low_count++;
51 }
52 mutex_unlock(&pool->mutex);
53 return 0;
54 }
55
56 static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
57 {
58 struct page *page;
59
60 if (high) {
61 BUG_ON(!pool->high_count);
62 page = list_first_entry(&pool->high_items, struct page, lru);
63 pool->high_count--;
64 } else {
65 BUG_ON(!pool->low_count);
66 page = list_first_entry(&pool->low_items, struct page, lru);
67 pool->low_count--;
68 }
69
70 list_del(&page->lru);
71 return page;
72 }
73
74 struct page *ion_page_pool_alloc(struct ion_page_pool *pool)
75 {
76 struct page *page = NULL;
77
78 BUG_ON(!pool);
79
80 mutex_lock(&pool->mutex);
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);
85 mutex_unlock(&pool->mutex);
86
87 if (!page)
88 page = ion_page_pool_alloc_pages(pool);
89
90 return page;
91 }
92
93 void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
94 {
95 int ret;
96
97 BUG_ON(pool->order != compound_order(page));
98
99 ret = ion_page_pool_add(pool, page);
100 if (ret)
101 ion_page_pool_free_pages(pool, page);
102 }
103
104 static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
105 {
106 int count = pool->low_count;
107
108 if (high)
109 count += pool->high_count;
110
111 return count << pool->order;
112 }
113
114 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
115 int nr_to_scan)
116 {
117 int freed = 0;
118 bool high;
119
120 if (current_is_kswapd())
121 high = true;
122 else
123 high = !!(gfp_mask & __GFP_HIGHMEM);
124
125 if (nr_to_scan == 0)
126 return ion_page_pool_total(pool, high);
127
128 while (freed < nr_to_scan) {
129 struct page *page;
130
131 mutex_lock(&pool->mutex);
132 if (pool->low_count) {
133 page = ion_page_pool_remove(pool, false);
134 } else if (high && pool->high_count) {
135 page = ion_page_pool_remove(pool, true);
136 } else {
137 mutex_unlock(&pool->mutex);
138 break;
139 }
140 mutex_unlock(&pool->mutex);
141 ion_page_pool_free_pages(pool, page);
142 freed += (1 << pool->order);
143 }
144
145 return freed;
146 }
147
148 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
149 bool cached)
150 {
151 struct ion_page_pool *pool = kmalloc(sizeof(*pool), GFP_KERNEL);
152
153 if (!pool)
154 return NULL;
155 pool->high_count = 0;
156 pool->low_count = 0;
157 INIT_LIST_HEAD(&pool->low_items);
158 INIT_LIST_HEAD(&pool->high_items);
159 pool->gfp_mask = gfp_mask | __GFP_COMP;
160 pool->order = order;
161 mutex_init(&pool->mutex);
162 plist_node_init(&pool->list, order);
163 if (cached)
164 pool->cached = true;
165
166 return pool;
167 }
168
169 void ion_page_pool_destroy(struct ion_page_pool *pool)
170 {
171 kfree(pool);
172 }
173
174 static int __init ion_page_pool_init(void)
175 {
176 return 0;
177 }
178 device_initcall(ion_page_pool_init);