]> Pileus Git - ~andy/linux/blob - drivers/staging/android/ion/ion_system_heap.c
ion: Fix two small issues in system_heap allocation
[~andy/linux] / drivers / staging / android / ion / ion_system_heap.c
1 /*
2  * drivers/staging/android/ion/ion_system_heap.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 <asm/page.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/highmem.h>
21 #include <linux/mm.h>
22 #include <linux/scatterlist.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include "ion.h"
27 #include "ion_priv.h"
28
29 static unsigned int high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO |
30                                             __GFP_NOWARN | __GFP_NORETRY) &
31                                            ~__GFP_WAIT;
32 static unsigned int low_order_gfp_flags  = (GFP_HIGHUSER | __GFP_ZERO |
33                                          __GFP_NOWARN);
34 static const unsigned int orders[] = {8, 4, 0};
35 static const int num_orders = ARRAY_SIZE(orders);
36 static int order_to_index(unsigned int order)
37 {
38         int i;
39         for (i = 0; i < num_orders; i++)
40                 if (order == orders[i])
41                         return i;
42         BUG();
43         return -1;
44 }
45
46 static unsigned int order_to_size(int order)
47 {
48         return PAGE_SIZE << order;
49 }
50
51 struct ion_system_heap {
52         struct ion_heap heap;
53         struct ion_page_pool **pools;
54 };
55
56 struct page_info {
57         struct page *page;
58         unsigned int order;
59         struct list_head list;
60 };
61
62 static struct page *alloc_buffer_page(struct ion_system_heap *heap,
63                                       struct ion_buffer *buffer,
64                                       unsigned long order)
65 {
66         bool cached = ion_buffer_cached(buffer);
67         struct ion_page_pool *pool = heap->pools[order_to_index(order)];
68         struct page *page;
69
70         if (!cached) {
71                 page = ion_page_pool_alloc(pool);
72         } else {
73                 gfp_t gfp_flags = low_order_gfp_flags;
74
75                 if (order > 4)
76                         gfp_flags = high_order_gfp_flags;
77                 page = ion_heap_alloc_pages(buffer, gfp_flags, order);
78                 if (!page)
79                         return 0;
80                 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order,
81                                                 DMA_BIDIRECTIONAL);
82         }
83         if (!page)
84                 return 0;
85
86         return page;
87 }
88
89 static void free_buffer_page(struct ion_system_heap *heap,
90                              struct ion_buffer *buffer, struct page *page,
91                              unsigned int order)
92 {
93         bool cached = ion_buffer_cached(buffer);
94         bool split_pages = ion_buffer_fault_user_mappings(buffer);
95         int i;
96
97         if (!cached) {
98                 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
99                 ion_page_pool_free(pool, page);
100         } else if (split_pages) {
101                 for (i = 0; i < (1 << order); i++)
102                         __free_page(page + i);
103         } else {
104                 __free_pages(page, order);
105         }
106 }
107
108
109 static struct page_info *alloc_largest_available(struct ion_system_heap *heap,
110                                                  struct ion_buffer *buffer,
111                                                  unsigned long size,
112                                                  unsigned int max_order)
113 {
114         struct page *page;
115         struct page_info *info;
116         int i;
117
118         for (i = 0; i < num_orders; i++) {
119                 if (size < order_to_size(orders[i]))
120                         continue;
121                 if (max_order < orders[i])
122                         continue;
123
124                 page = alloc_buffer_page(heap, buffer, orders[i]);
125                 if (!page)
126                         continue;
127
128                 info = kmalloc(sizeof(struct page_info), GFP_KERNEL);
129                 info->page = page;
130                 info->order = orders[i];
131                 return info;
132         }
133         return NULL;
134 }
135
136 static int ion_system_heap_allocate(struct ion_heap *heap,
137                                      struct ion_buffer *buffer,
138                                      unsigned long size, unsigned long align,
139                                      unsigned long flags)
140 {
141         struct ion_system_heap *sys_heap = container_of(heap,
142                                                         struct ion_system_heap,
143                                                         heap);
144         struct sg_table *table;
145         struct scatterlist *sg;
146         int ret;
147         struct list_head pages;
148         struct page_info *info, *tmp_info;
149         int i = 0;
150         long size_remaining = PAGE_ALIGN(size);
151         unsigned int max_order = orders[0];
152
153         if (align > PAGE_SIZE)
154                 return -EINVAL;
155
156         if (ion_buffer_fault_user_mappings(buffer))
157                 return -EINVAL;
158
159         INIT_LIST_HEAD(&pages);
160         while (size_remaining > 0) {
161                 info = alloc_largest_available(sys_heap, buffer, size_remaining, max_order);
162                 if (!info)
163                         goto err;
164                 list_add_tail(&info->list, &pages);
165                 size_remaining -= (1 << info->order) * PAGE_SIZE;
166                 max_order = info->order;
167                 i++;
168         }
169         table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
170         if (!table)
171                 goto err;
172
173         ret = sg_alloc_table(table, i, GFP_KERNEL);
174         if (ret)
175                 goto err1;
176
177         sg = table->sgl;
178         list_for_each_entry_safe(info, tmp_info, &pages, list) {
179                 struct page *page = info->page;
180                 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0);
181                 sg = sg_next(sg);
182                 list_del(&info->list);
183                 kfree(info);
184         }
185
186         buffer->priv_virt = table;
187         return 0;
188 err1:
189         kfree(table);
190 err:
191         list_for_each_entry_safe(info, tmp_info, &pages, list) {
192                 free_buffer_page(sys_heap, buffer, info->page, info->order);
193                 kfree(info);
194         }
195         return -ENOMEM;
196 }
197
198 void ion_system_heap_free(struct ion_buffer *buffer)
199 {
200         struct ion_heap *heap = buffer->heap;
201         struct ion_system_heap *sys_heap = container_of(heap,
202                                                         struct ion_system_heap,
203                                                         heap);
204         struct sg_table *table = buffer->sg_table;
205         bool cached = ion_buffer_cached(buffer);
206         struct scatterlist *sg;
207         LIST_HEAD(pages);
208         int i;
209
210         /* uncached pages come from the page pools, zero them before returning
211            for security purposes (other allocations are zerod at alloc time */
212         if (!cached)
213                 ion_heap_buffer_zero(buffer);
214
215         for_each_sg(table->sgl, sg, table->nents, i)
216                 free_buffer_page(sys_heap, buffer, sg_page(sg),
217                                 get_order(sg->length));
218         sg_free_table(table);
219         kfree(table);
220 }
221
222 struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
223                                          struct ion_buffer *buffer)
224 {
225         return buffer->priv_virt;
226 }
227
228 void ion_system_heap_unmap_dma(struct ion_heap *heap,
229                                struct ion_buffer *buffer)
230 {
231         return;
232 }
233
234 static struct ion_heap_ops system_heap_ops = {
235         .allocate = ion_system_heap_allocate,
236         .free = ion_system_heap_free,
237         .map_dma = ion_system_heap_map_dma,
238         .unmap_dma = ion_system_heap_unmap_dma,
239         .map_kernel = ion_heap_map_kernel,
240         .unmap_kernel = ion_heap_unmap_kernel,
241         .map_user = ion_heap_map_user,
242 };
243
244 static int ion_system_heap_shrink(struct shrinker *shrinker,
245                                   struct shrink_control *sc) {
246
247         struct ion_heap *heap = container_of(shrinker, struct ion_heap,
248                                              shrinker);
249         struct ion_system_heap *sys_heap = container_of(heap,
250                                                         struct ion_system_heap,
251                                                         heap);
252         int nr_total = 0;
253         int nr_freed = 0;
254         int i;
255
256         if (sc->nr_to_scan == 0)
257                 goto end;
258
259         /* shrink the free list first, no point in zeroing the memory if
260            we're just going to reclaim it */
261         nr_freed += ion_heap_freelist_drain(heap, sc->nr_to_scan * PAGE_SIZE) /
262                 PAGE_SIZE;
263
264         if (nr_freed >= sc->nr_to_scan)
265                 goto end;
266
267         for (i = 0; i < num_orders; i++) {
268                 struct ion_page_pool *pool = sys_heap->pools[i];
269
270                 nr_freed += ion_page_pool_shrink(pool, sc->gfp_mask,
271                                                  sc->nr_to_scan);
272                 if (nr_freed >= sc->nr_to_scan)
273                         break;
274         }
275
276 end:
277         /* total number of items is whatever the page pools are holding
278            plus whatever's in the freelist */
279         for (i = 0; i < num_orders; i++) {
280                 struct ion_page_pool *pool = sys_heap->pools[i];
281                 nr_total += ion_page_pool_shrink(pool, sc->gfp_mask, 0);
282         }
283         nr_total += ion_heap_freelist_size(heap) / PAGE_SIZE;
284         return nr_total;
285
286 }
287
288 static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
289                                       void *unused)
290 {
291
292         struct ion_system_heap *sys_heap = container_of(heap,
293                                                         struct ion_system_heap,
294                                                         heap);
295         int i;
296         for (i = 0; i < num_orders; i++) {
297                 struct ion_page_pool *pool = sys_heap->pools[i];
298                 seq_printf(s, "%d order %u highmem pages in pool = %lu total\n",
299                            pool->high_count, pool->order,
300                            (1 << pool->order) * PAGE_SIZE * pool->high_count);
301                 seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n",
302                            pool->low_count, pool->order,
303                            (1 << pool->order) * PAGE_SIZE * pool->low_count);
304         }
305         return 0;
306 }
307
308 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
309 {
310         struct ion_system_heap *heap;
311         int i;
312
313         heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL);
314         if (!heap)
315                 return ERR_PTR(-ENOMEM);
316         heap->heap.ops = &system_heap_ops;
317         heap->heap.type = ION_HEAP_TYPE_SYSTEM;
318         heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
319         heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders,
320                               GFP_KERNEL);
321         if (!heap->pools)
322                 goto err_alloc_pools;
323         for (i = 0; i < num_orders; i++) {
324                 struct ion_page_pool *pool;
325                 gfp_t gfp_flags = low_order_gfp_flags;
326
327                 if (orders[i] > 4)
328                         gfp_flags = high_order_gfp_flags;
329                 pool = ion_page_pool_create(gfp_flags, orders[i]);
330                 if (!pool)
331                         goto err_create_pool;
332                 heap->pools[i] = pool;
333         }
334
335         heap->heap.shrinker.shrink = ion_system_heap_shrink;
336         heap->heap.shrinker.seeks = DEFAULT_SEEKS;
337         heap->heap.shrinker.batch = 0;
338         register_shrinker(&heap->heap.shrinker);
339         heap->heap.debug_show = ion_system_heap_debug_show;
340         return &heap->heap;
341 err_create_pool:
342         for (i = 0; i < num_orders; i++)
343                 if (heap->pools[i])
344                         ion_page_pool_destroy(heap->pools[i]);
345         kfree(heap->pools);
346 err_alloc_pools:
347         kfree(heap);
348         return ERR_PTR(-ENOMEM);
349 }
350
351 void ion_system_heap_destroy(struct ion_heap *heap)
352 {
353         struct ion_system_heap *sys_heap = container_of(heap,
354                                                         struct ion_system_heap,
355                                                         heap);
356         int i;
357
358         for (i = 0; i < num_orders; i++)
359                 ion_page_pool_destroy(sys_heap->pools[i]);
360         kfree(sys_heap->pools);
361         kfree(sys_heap);
362 }
363
364 static int ion_system_contig_heap_allocate(struct ion_heap *heap,
365                                            struct ion_buffer *buffer,
366                                            unsigned long len,
367                                            unsigned long align,
368                                            unsigned long flags)
369 {
370         int order = get_order(len);
371
372         if (align > (PAGE_SIZE << order))
373                 return -EINVAL;
374
375         if (ion_buffer_fault_user_mappings(buffer))
376                 return -EINVAL;
377
378         buffer->priv_virt = kzalloc(len, GFP_KERNEL);
379         if (!buffer->priv_virt)
380                 return -ENOMEM;
381         return 0;
382 }
383
384 void ion_system_contig_heap_free(struct ion_buffer *buffer)
385 {
386         kfree(buffer->priv_virt);
387 }
388
389 static int ion_system_contig_heap_phys(struct ion_heap *heap,
390                                        struct ion_buffer *buffer,
391                                        ion_phys_addr_t *addr, size_t *len)
392 {
393         *addr = virt_to_phys(buffer->priv_virt);
394         *len = buffer->size;
395         return 0;
396 }
397
398 struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
399                                                 struct ion_buffer *buffer)
400 {
401         struct sg_table *table;
402         int ret;
403
404         table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
405         if (!table)
406                 return ERR_PTR(-ENOMEM);
407         ret = sg_alloc_table(table, 1, GFP_KERNEL);
408         if (ret) {
409                 kfree(table);
410                 return ERR_PTR(ret);
411         }
412         sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size,
413                     0);
414         return table;
415 }
416
417 void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
418                                       struct ion_buffer *buffer)
419 {
420         sg_free_table(buffer->sg_table);
421         kfree(buffer->sg_table);
422 }
423
424 static struct ion_heap_ops kmalloc_ops = {
425         .allocate = ion_system_contig_heap_allocate,
426         .free = ion_system_contig_heap_free,
427         .phys = ion_system_contig_heap_phys,
428         .map_dma = ion_system_contig_heap_map_dma,
429         .unmap_dma = ion_system_contig_heap_unmap_dma,
430         .map_kernel = ion_heap_map_kernel,
431         .unmap_kernel = ion_heap_unmap_kernel,
432         .map_user = ion_heap_map_user,
433 };
434
435 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
436 {
437         struct ion_heap *heap;
438
439         heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
440         if (!heap)
441                 return ERR_PTR(-ENOMEM);
442         heap->ops = &kmalloc_ops;
443         heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
444         return heap;
445 }
446
447 void ion_system_contig_heap_destroy(struct ion_heap *heap)
448 {
449         kfree(heap);
450 }
451