]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/video/tegra/nvmap/nvmap_pp.c
1c2e1f715affb49a6c4cd037559cff25f6d2963c
[sojka/nv-tegra/linux-3.10.git] / drivers / video / tegra / nvmap / nvmap_pp.c
1 /*
2  * drivers/video/tegra/nvmap/nvmap_pp.c
3  *
4  * Manage page pools to speed up page allocation.
5  *
6  * Copyright (c) 2009-2014, NVIDIA CORPORATION. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #define pr_fmt(fmt) "%s: " fmt, __func__
24
25 #include <linux/kernel.h>
26 #include <linux/vmalloc.h>
27 #include <linux/moduleparam.h>
28 #include <linux/shrinker.h>
29 #include <linux/kthread.h>
30 #include <linux/debugfs.h>
31
32 #include "nvmap_priv.h"
33
34 #define NVMAP_TEST_PAGE_POOL_SHRINKER     1
35 #define PENDING_PAGES_SIZE                128
36 #define MIN_AVAILABLE_MB                  128
37
38 static bool enable_pp = 1;
39 static int pool_size;
40
41 static struct task_struct *background_allocator;
42 static struct page *pending_pages[PENDING_PAGES_SIZE];
43 static atomic_t bg_pages_to_fill;
44
45 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
46 static inline void __pp_dbg_var_add(u64 *dbg_var, u32 nr)
47 {
48         *dbg_var += nr;
49 }
50 #else
51 #define __pp_dbg_var_add(dbg_var, nr)
52 #endif
53
54 #define pp_alloc_add(pool, nr) __pp_dbg_var_add(&(pool)->allocs, nr)
55 #define pp_fill_add(pool, nr)  __pp_dbg_var_add(&(pool)->fills, nr)
56 #define pp_hit_add(pool, nr)   __pp_dbg_var_add(&(pool)->hits, nr)
57 #define pp_miss_add(pool, nr)  __pp_dbg_var_add(&(pool)->misses, nr)
58
59 static int __nvmap_page_pool_fill_lots_locked(struct nvmap_page_pool *pool,
60                                        struct page **pages, u32 nr);
61
62 static inline struct page *get_page_list_page(struct nvmap_page_pool *pool)
63 {
64         struct page *page;
65
66         if (list_empty(&pool->page_list))
67                 return NULL;
68
69         page = list_first_entry(&pool->page_list, struct page, lru);
70         list_del(&page->lru);
71
72         pool->count--;
73
74         return page;
75 }
76
77 /*
78  * Allocate n pages one by one. Not the most efficient allocation scheme ever;
79  * however, it will make it easier later on to handle single or small number of
80  * page allocations from the page pool being individually freed.
81  */
82 static int __nvmap_pp_alloc_n_pages(struct page **pages, int n, gfp_t flags)
83 {
84         int i;
85
86         for (i = 0; i < n; i++) {
87                 pages[i] = alloc_page(flags);
88                 if (!pages[i])
89                         goto no_mem;
90         }
91
92         return 0;
93
94 no_mem:
95         for (i -= 1; i >= 0; i--)
96                 __free_page(pages[i]);
97         return -ENOMEM;
98 }
99
100 /*
101  * Actually do the fill. This requires a few steps:
102  *
103  *  1. Allocate a bunch of pages.
104  *
105  *  2. Fill the page pool with the allocated pages. We don't want to hold the
106  *     PP lock for too long so this is the only time we hold the PP lock.
107  *
108  *  3. Rinse and repeat until we have allocated all the pages we think we need
109  *     or the page pool is full. Since we are not holding the lock for the
110  *     entire fill it is possible that other pages were filled into the pool.
111  *
112  *  4. Free any left over pages if the pool is filled before we finish.
113  */
114 static void nvmap_pp_do_background_fill(struct nvmap_page_pool *pool)
115 {
116         int err;
117         u32 pages = 0, nr, i;
118         gfp_t gfp = GFP_NVMAP | __GFP_NOMEMALLOC |
119                     __GFP_NORETRY | __GFP_NO_KSWAPD;
120
121         pages = (u32)atomic_xchg(&bg_pages_to_fill, pages);
122
123         if (!pages || !enable_pp)
124                 return;
125
126         /* If this param is set, force zero page allocation. */
127         if (zero_memory)
128                 gfp |= __GFP_ZERO;
129
130         do {
131                 nr = min_t(u32, PENDING_PAGES_SIZE, pages);
132                 err = __nvmap_pp_alloc_n_pages(pending_pages, nr, gfp);
133                 if (err) {
134                         pr_info("Failed to alloc %u pages for PP!\n", pages);
135                         return;
136                 }
137
138                 mutex_lock(&pool->lock);
139                 i = __nvmap_page_pool_fill_lots_locked(pool, pending_pages, nr);
140                 mutex_unlock(&pool->lock);
141                 pages -= nr;
142         } while (pages && i == nr);
143
144         for (; i < nr; i++)
145                 __free_page(pending_pages[i]);
146 }
147
148 /*
149  * This thread fills the page pools with zeroed pages. We avoid releasing the
150  * pages directly back into the page pools since we would then have to zero
151  * them ourselves. Instead it is easier to just reallocate zeroed pages. This
152  * happens in the background so that the overhead of allocating zeroed pages is
153  * not directly seen by userspace. Of course if the page pools are empty user
154  * space will suffer.
155  */
156 static int nvmap_background_zero_allocator(void *arg)
157 {
158         pr_info("PP alloc thread starting.\n");
159
160         while (1) {
161                 if (kthread_should_stop())
162                         break;
163
164                 nvmap_pp_do_background_fill(&nvmap_dev->pool);
165
166                 /* Pending work is done - go to sleep. */
167                 set_current_state(TASK_INTERRUPTIBLE);
168                 schedule();
169         }
170
171         return 0;
172 }
173
174 /*
175  * Call this if the background allocator should possibly wake up. This function
176  * will check to make sure its actually a good idea for that to happen before
177  * waking the allocator up.
178  */
179 static inline void nvmap_pp_wake_up_allocator(void)
180 {
181         struct nvmap_page_pool *pool = &nvmap_dev->pool;
182         struct sysinfo info;
183         int free_pages, tmp;
184
185         if (!enable_pp)
186                 return;
187
188         /* Hueristic: if we don't need to prefill explicitly zero'ed memory then
189          * lots of memory can be placed back in the pools by possible frees.
190          * Therefor don't fill the pool unless we really need to as we may get
191          * more memory without needing to alloc pages.
192          */
193         if (!zero_memory && pool->count > NVMAP_PP_ZERO_MEM_FILL_MIN)
194                 return;
195
196         if (pool->max - pool->count < NVMAP_PP_DEF_FILL_THRESH)
197                 return;
198
199         si_meminfo(&info);
200         free_pages = (int)info.freeram;
201
202         tmp = free_pages - (MIN_AVAILABLE_MB << (20 - PAGE_SHIFT));
203         if (tmp <= 0)
204                 return;
205
206         /* Let the background thread know how much memory to fill. */
207         atomic_set(&bg_pages_to_fill,
208                    min(tmp, (int)(pool->max - pool->count)));
209         wake_up_process(background_allocator);
210 }
211
212 /*
213  * This removes a page from the page pool. If ignore_disable is set, then
214  * the enable_pp flag is ignored.
215  */
216 static struct page *nvmap_page_pool_alloc_locked(struct nvmap_page_pool *pool,
217                                                  int force_alloc)
218 {
219         struct page *page;
220
221         if (!force_alloc && !enable_pp)
222                 return NULL;
223
224         if (list_empty(&pool->page_list)) {
225                 pp_miss_add(pool, 1);
226                 return NULL;
227         }
228
229         if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG))
230                 BUG_ON(pool->count == 0);
231
232         page = get_page_list_page(pool);
233         if (!page)
234                 return NULL;
235
236         /* Sanity check. */
237         if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
238                 atomic_dec(&page->_count);
239                 BUG_ON(atomic_read(&page->_count) != 1);
240         }
241
242         pp_alloc_add(pool, 1);
243         pp_hit_add(pool, 1);
244
245         return page;
246 }
247
248 /*
249  * Alloc a bunch of pages from the page pool. This will alloc as many as it can
250  * and return the number of pages allocated. Pages are placed into the passed
251  * array in a linear fashion starting from index 0.
252  *
253  * You must lock the page pool before using this.
254  */
255 static int __nvmap_page_pool_alloc_lots_locked(struct nvmap_page_pool *pool,
256                                         struct page **pages, u32 nr)
257 {
258         u32 real_nr;
259         u32 ind = 0;
260
261         if (!enable_pp)
262                 return 0;
263
264         real_nr = min_t(u32, nr, pool->count);
265
266         while (real_nr--) {
267                 struct page *page;
268                 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG))
269                         BUG_ON(list_empty(&pool->page_list));
270                 page = get_page_list_page(pool);
271                 pages[ind++] = page;
272                 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
273                         atomic_dec(&page->_count);
274                         BUG_ON(atomic_read(&page->_count) != 1);
275                 }
276         }
277
278         pp_alloc_add(pool, ind);
279         pp_hit_add(pool, ind);
280         pp_miss_add(pool, nr - ind);
281         nvmap_pp_wake_up_allocator();
282
283         return ind;
284 }
285
286 int nvmap_page_pool_alloc_lots(struct nvmap_page_pool *pool,
287                                 struct page **pages, u32 nr)
288 {
289         int ret;
290
291         mutex_lock(&pool->lock);
292         ret = __nvmap_page_pool_alloc_lots_locked(pool, pages, nr);
293         mutex_unlock(&pool->lock);
294
295         return ret;
296 }
297
298 /*
299  * This adds a page to the pool. Returns true if the passed page is added.
300  * That means if the pool is full this operation will fail.
301  */
302 static bool nvmap_page_pool_fill_locked(struct nvmap_page_pool *pool,
303                                         struct page *page)
304 {
305         if (!enable_pp)
306                 return false;
307
308         if (pool->count >= pool->max)
309                 return false;
310
311         /* Sanity check. */
312         if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
313                 atomic_inc(&page->_count);
314                 BUG_ON(atomic_read(&page->_count) != 2);
315                 BUG_ON(pool->count > pool->max);
316         }
317
318         list_add_tail(&page->lru, &pool->page_list);
319         pool->count++;
320         pp_fill_add(pool, 1);
321
322         return true;
323 }
324
325 /*
326  * Fill a bunch of pages into the page pool. This will fill as many as it can
327  * and return the number of pages filled. Pages are used from the start of the
328  * passed page pointer array in a linear fashion.
329  *
330  * You must lock the page pool before using this.
331  */
332 static int __nvmap_page_pool_fill_lots_locked(struct nvmap_page_pool *pool,
333                                        struct page **pages, u32 nr)
334 {
335         u32 real_nr;
336         u32 ind = 0;
337
338         if (!enable_pp)
339                 return 0;
340
341         real_nr = min_t(u32, pool->max - pool->count, nr);
342         if (real_nr == 0)
343                 return 0;
344
345         while (real_nr--) {
346                 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
347                         atomic_inc(&pages[ind]->_count);
348                         BUG_ON(atomic_read(&pages[ind]->_count) != 2);
349                 }
350                 list_add_tail(&pages[ind++]->lru, &pool->page_list);
351         }
352
353         pool->count += ind;
354         pp_fill_add(pool, ind);
355
356         return ind;
357 }
358
359 int nvmap_page_pool_fill_lots(struct nvmap_page_pool *pool,
360                                        struct page **pages, u32 nr)
361 {
362         int ret;
363
364         mutex_lock(&pool->lock);
365         ret = __nvmap_page_pool_fill_lots_locked(pool, pages, nr);
366         mutex_unlock(&pool->lock);
367
368         return ret;
369 }
370
371 bool nvmap_page_pool_fill(struct nvmap_page_pool *pool, struct page *page)
372 {
373         bool ret = false;
374
375         if (pool) {
376                 mutex_lock(&pool->lock);
377                 ret = nvmap_page_pool_fill_locked(pool, page);
378                 mutex_unlock(&pool->lock);
379         }
380
381         return ret;
382 }
383
384 /*
385  * Free the passed number of pages from the page pool. This happen irregardless
386  * of whether ther page pools are enabled. This lets one disable the page pools
387  * and then free all the memory therein.
388  */
389 static int nvmap_page_pool_free(struct nvmap_page_pool *pool, int nr_free)
390 {
391         int i = nr_free;
392         struct page *page;
393
394         if (!nr_free)
395                 return nr_free;
396
397         mutex_lock(&pool->lock);
398         while (i) {
399                 page = nvmap_page_pool_alloc_locked(pool, 1);
400                 if (!page)
401                         break;
402                 __free_page(page);
403                 i--;
404         }
405         mutex_unlock(&pool->lock);
406
407         return i;
408 }
409
410 ulong nvmap_page_pool_get_unused_pages(void)
411 {
412         int total = 0;
413
414         if (!nvmap_dev)
415                 return 0;
416
417         total = nvmap_dev->pool.count;
418
419         return total;
420 }
421
422 /*
423  * Remove and free to the system all the pages currently in the page
424  * pool. This operation will happen even if the page pools are disabled.
425  */
426 int nvmap_page_pool_clear(void)
427 {
428         struct page *page;
429         struct nvmap_page_pool *pool = &nvmap_dev->pool;
430
431         mutex_lock(&pool->lock);
432
433         while ((page = nvmap_page_pool_alloc_locked(pool, 1)) != NULL)
434                 __free_page(page);
435
436         /* For some reason, if an error occured... */
437         if (!list_empty(&pool->page_list)) {
438                 mutex_unlock(&pool->lock);
439                 return -ENOMEM;
440         }
441
442         mutex_unlock(&pool->lock);
443         nvmap_pp_wake_up_allocator();
444
445         return 0;
446 }
447
448 /*
449  * Resizes the page pool to the passed size. If the passed size is 0 then
450  * all associated resources are released back to the system. This operation
451  * will only occur if the page pools are enabled.
452  */
453 static void nvmap_page_pool_resize(struct nvmap_page_pool *pool, int size)
454 {
455         if (!enable_pp || size == pool->max || size < 0)
456                 return;
457
458         mutex_lock(&pool->lock);
459
460         while (pool->count > size)
461                 __free_page(nvmap_page_pool_alloc_locked(pool, 0));
462
463         pool->max = size;
464
465         pr_debug("page pool resized to %d from %d pages\n", size, pool->max);
466         mutex_unlock(&pool->lock);
467 }
468
469 static int nvmap_page_pool_shrink(struct shrinker *shrinker,
470                                   struct shrink_control *sc)
471 {
472         int shrink_pages = sc->nr_to_scan;
473
474         if (!shrink_pages)
475                 goto out;
476
477         pr_debug("sh_pages=%d", shrink_pages);
478
479         shrink_pages = nvmap_page_pool_free(&nvmap_dev->pool, shrink_pages);
480 out:
481         return nvmap_page_pool_get_unused_pages();
482 }
483
484 static struct shrinker nvmap_page_pool_shrinker = {
485         .shrink = nvmap_page_pool_shrink,
486         .seeks = 1,
487 };
488
489 static void shrink_page_pools(int *total_pages, int *available_pages)
490 {
491         struct shrink_control sc;
492
493         if (*total_pages == 0) {
494                 sc.gfp_mask = GFP_KERNEL;
495                 sc.nr_to_scan = 0;
496                 *total_pages = nvmap_page_pool_shrink(NULL, &sc);
497         }
498         sc.nr_to_scan = *total_pages;
499         *available_pages = nvmap_page_pool_shrink(NULL, &sc);
500 }
501
502 #if NVMAP_TEST_PAGE_POOL_SHRINKER
503 static int shrink_pp;
504 static int shrink_set(const char *arg, const struct kernel_param *kp)
505 {
506         int cpu = smp_processor_id();
507         unsigned long long t1, t2;
508         int total_pages, available_pages;
509
510         param_set_int(arg, kp);
511
512         if (shrink_pp) {
513                 total_pages = shrink_pp;
514                 t1 = cpu_clock(cpu);
515                 shrink_page_pools(&total_pages, &available_pages);
516                 t2 = cpu_clock(cpu);
517                 pr_debug("shrink page pools: time=%lldns, "
518                         "total_pages_released=%d, free_pages_available=%d",
519                         t2-t1, total_pages, available_pages);
520         }
521         return 0;
522 }
523
524 static int shrink_get(char *buff, const struct kernel_param *kp)
525 {
526         return param_get_int(buff, kp);
527 }
528
529 static struct kernel_param_ops shrink_ops = {
530         .get = shrink_get,
531         .set = shrink_set,
532 };
533
534 module_param_cb(shrink_page_pools, &shrink_ops, &shrink_pp, 0644);
535 #endif
536
537 static int enable_pp_set(const char *arg, const struct kernel_param *kp)
538 {
539         int ret;
540
541         ret = param_set_bool(arg, kp);
542         if (ret)
543                 return ret;
544
545         if (!enable_pp)
546                 nvmap_page_pool_clear();
547
548         return 0;
549 }
550
551 static int enable_pp_get(char *buff, const struct kernel_param *kp)
552 {
553         return param_get_int(buff, kp);
554 }
555
556 static struct kernel_param_ops enable_pp_ops = {
557         .get = enable_pp_get,
558         .set = enable_pp_set,
559 };
560
561 module_param_cb(enable_page_pools, &enable_pp_ops, &enable_pp, 0644);
562
563 static int pool_size_set(const char *arg, const struct kernel_param *kp)
564 {
565         param_set_int(arg, kp);
566         nvmap_page_pool_resize(&nvmap_dev->pool, pool_size);
567         return 0;
568 }
569
570 static int pool_size_get(char *buff, const struct kernel_param *kp)
571 {
572         return param_get_int(buff, kp);
573 }
574
575 static struct kernel_param_ops pool_size_ops = {
576         .get = pool_size_get,
577         .set = pool_size_set,
578 };
579
580 module_param_cb(pool_size, &pool_size_ops, &pool_size, 0644);
581
582 int nvmap_page_pool_debugfs_init(struct dentry *nvmap_root)
583 {
584         struct dentry *pp_root;
585
586         if (!nvmap_root)
587                 return -ENODEV;
588
589         pp_root = debugfs_create_dir("pagepool", nvmap_root);
590         if (!pp_root)
591                 return -ENODEV;
592
593         debugfs_create_u32("page_pool_available_pages",
594                            S_IRUGO, pp_root,
595                            &nvmap_dev->pool.count);
596 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
597         debugfs_create_u64("page_pool_allocs",
598                            S_IRUGO, pp_root,
599                            &nvmap_dev->pool.allocs);
600         debugfs_create_u64("page_pool_fills",
601                            S_IRUGO, pp_root,
602                            &nvmap_dev->pool.fills);
603         debugfs_create_u64("page_pool_hits",
604                            S_IRUGO, pp_root,
605                            &nvmap_dev->pool.hits);
606         debugfs_create_u64("page_pool_misses",
607                            S_IRUGO, pp_root,
608                            &nvmap_dev->pool.misses);
609 #endif
610
611         return 0;
612 }
613
614 int nvmap_page_pool_init(struct nvmap_device *dev)
615 {
616         static int reg = 1;
617         unsigned long totalram_mb;
618         struct sysinfo info;
619         struct nvmap_page_pool *pool = &dev->pool;
620 #ifdef CONFIG_NVMAP_PAGE_POOLS_INIT_FILLUP
621         int i;
622         struct page *page;
623         int pages_to_fill;
624         int highmem_pages = 0;
625 #endif
626
627         memset(pool, 0x0, sizeof(*pool));
628         mutex_init(&pool->lock);
629         INIT_LIST_HEAD(&pool->page_list);
630
631         si_meminfo(&info);
632         totalram_mb = (info.totalram * info.mem_unit) >> 20;
633         pr_info("Total MB RAM: %lu\n", totalram_mb);
634
635         if (!CONFIG_NVMAP_PAGE_POOL_SIZE)
636                 /* The ratio is pool pages per 1K ram pages. So, the >> 10. */
637                 pool->max = (info.totalram * NVMAP_PP_POOL_SIZE) >> 10;
638         else
639                 pool->max = CONFIG_NVMAP_PAGE_POOL_SIZE;
640
641         if (pool->max >= info.totalram)
642                 goto fail;
643         pool_size = pool->max;
644
645         pr_info("nvmap page pool size: %u pages (%u MB)\n", pool->max,
646                 (pool->max * info.mem_unit) >> 20);
647
648         if (reg) {
649                 reg = 0;
650                 register_shrinker(&nvmap_page_pool_shrinker);
651         }
652
653         background_allocator = kthread_create(nvmap_background_zero_allocator,
654                                             NULL, "nvmap-bz");
655         if (IS_ERR_OR_NULL(background_allocator))
656                 goto fail;
657
658 #ifdef CONFIG_NVMAP_PAGE_POOLS_INIT_FILLUP
659         pages_to_fill = CONFIG_NVMAP_PAGE_POOLS_INIT_FILLUP_SIZE * SZ_1M /
660                         PAGE_SIZE;
661         pages_to_fill = pages_to_fill ? : pool->count;
662
663         for (i = 0; i < pages_to_fill; i++) {
664                 page = alloc_page(GFP_NVMAP);
665                 if (!page)
666                         goto done;
667                 if (!nvmap_page_pool_fill_locked(pool, page)) {
668                         __free_page(page);
669                         goto done;
670                 }
671                 if (PageHighMem(page))
672                         highmem_pages++;
673         }
674
675         si_meminfo(&info);
676         pr_info("highmem=%d, pool_size=%d,"
677                 "totalram=%lu, freeram=%lu, totalhigh=%lu, freehigh=%lu\n",
678                 highmem_pages, pool->count,
679                 info.totalram, info.freeram, info.totalhigh, info.freehigh);
680 done:
681 #endif
682         return 0;
683 fail:
684         nvmap_page_pool_fini(dev);
685         return -ENOMEM;
686 }
687
688 int nvmap_page_pool_fini(struct nvmap_device *dev)
689 {
690         struct nvmap_page_pool *pool = &dev->pool;
691
692         if (!IS_ERR_OR_NULL(background_allocator))
693                 kthread_stop(background_allocator);
694
695         WARN_ON(!list_empty(&pool->page_list));
696
697         return 0;
698 }