2 * drivers/video/tegra/nvmap/nvmap_pp.c
4 * Manage page pools to speed up page allocation.
6 * Copyright (c) 2009-2014, NVIDIA CORPORATION. All rights reserved.
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.
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
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.
23 #define pr_fmt(fmt) "%s: " fmt, __func__
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>
32 #include "nvmap_priv.h"
34 #define NVMAP_TEST_PAGE_POOL_SHRINKER 1
35 #define PENDING_PAGES_SIZE 128
36 #define MIN_AVAILABLE_MB 128
38 static bool enable_pp = 1;
41 static struct task_struct *background_allocator;
42 static struct page *pending_pages[PENDING_PAGES_SIZE];
43 static atomic_t bg_pages_to_fill;
45 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
46 static inline void __pp_dbg_var_add(u64 *dbg_var, u32 nr)
51 #define __pp_dbg_var_add(dbg_var, nr)
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)
59 static int __nvmap_page_pool_fill_lots_locked(struct nvmap_page_pool *pool,
60 struct page **pages, u32 nr);
62 static inline struct page *get_page_list_page(struct nvmap_page_pool *pool)
66 if (list_empty(&pool->page_list))
69 page = list_first_entry(&pool->page_list, struct page, lru);
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.
82 static int __nvmap_pp_alloc_n_pages(struct page **pages, int n, gfp_t flags)
86 for (i = 0; i < n; i++) {
87 pages[i] = alloc_page(flags);
95 for (i -= 1; i >= 0; i--)
96 __free_page(pages[i]);
101 * Actually do the fill. This requires a few steps:
103 * 1. Allocate a bunch of pages.
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.
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.
112 * 4. Free any left over pages if the pool is filled before we finish.
114 static void nvmap_pp_do_background_fill(struct nvmap_page_pool *pool)
117 u32 pages = 0, nr, i;
118 gfp_t gfp = GFP_NVMAP | __GFP_NOMEMALLOC |
119 __GFP_NORETRY | __GFP_NO_KSWAPD;
121 pages = (u32)atomic_xchg(&bg_pages_to_fill, pages);
123 if (!pages || !enable_pp)
126 /* If this param is set, force zero page allocation. */
131 nr = min_t(u32, PENDING_PAGES_SIZE, pages);
132 err = __nvmap_pp_alloc_n_pages(pending_pages, nr, gfp);
134 pr_info("Failed to alloc %u pages for PP!\n", pages);
138 mutex_lock(&pool->lock);
139 i = __nvmap_page_pool_fill_lots_locked(pool, pending_pages, nr);
140 mutex_unlock(&pool->lock);
142 } while (pages && i == nr);
145 __free_page(pending_pages[i]);
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
156 static int nvmap_background_zero_allocator(void *arg)
158 pr_info("PP alloc thread starting.\n");
161 if (kthread_should_stop())
164 nvmap_pp_do_background_fill(&nvmap_dev->pool);
166 /* Pending work is done - go to sleep. */
167 set_current_state(TASK_INTERRUPTIBLE);
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.
179 static inline void nvmap_pp_wake_up_allocator(void)
181 struct nvmap_page_pool *pool = &nvmap_dev->pool;
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.
193 if (!zero_memory && pool->count > NVMAP_PP_ZERO_MEM_FILL_MIN)
196 if (pool->max - pool->count < NVMAP_PP_DEF_FILL_THRESH)
200 free_pages = (int)info.freeram;
202 tmp = free_pages - (MIN_AVAILABLE_MB << (20 - PAGE_SHIFT));
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);
213 * This removes a page from the page pool. If ignore_disable is set, then
214 * the enable_pp flag is ignored.
216 static struct page *nvmap_page_pool_alloc_locked(struct nvmap_page_pool *pool,
221 if (!force_alloc && !enable_pp)
224 if (list_empty(&pool->page_list)) {
225 pp_miss_add(pool, 1);
229 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG))
230 BUG_ON(pool->count == 0);
232 page = get_page_list_page(pool);
237 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
238 atomic_dec(&page->_count);
239 BUG_ON(atomic_read(&page->_count) != 1);
242 pp_alloc_add(pool, 1);
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.
253 * You must lock the page pool before using this.
255 static int __nvmap_page_pool_alloc_lots_locked(struct nvmap_page_pool *pool,
256 struct page **pages, u32 nr)
264 real_nr = min_t(u32, nr, pool->count);
268 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG))
269 BUG_ON(list_empty(&pool->page_list));
270 page = get_page_list_page(pool);
272 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
273 atomic_dec(&page->_count);
274 BUG_ON(atomic_read(&page->_count) != 1);
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();
286 int nvmap_page_pool_alloc_lots(struct nvmap_page_pool *pool,
287 struct page **pages, u32 nr)
291 mutex_lock(&pool->lock);
292 ret = __nvmap_page_pool_alloc_lots_locked(pool, pages, nr);
293 mutex_unlock(&pool->lock);
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.
302 static bool nvmap_page_pool_fill_locked(struct nvmap_page_pool *pool,
308 if (pool->count >= pool->max)
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);
318 list_add_tail(&page->lru, &pool->page_list);
320 pp_fill_add(pool, 1);
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.
330 * You must lock the page pool before using this.
332 static int __nvmap_page_pool_fill_lots_locked(struct nvmap_page_pool *pool,
333 struct page **pages, u32 nr)
341 real_nr = min_t(u32, pool->max - pool->count, 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);
350 list_add_tail(&pages[ind++]->lru, &pool->page_list);
354 pp_fill_add(pool, ind);
359 int nvmap_page_pool_fill_lots(struct nvmap_page_pool *pool,
360 struct page **pages, u32 nr)
364 mutex_lock(&pool->lock);
365 ret = __nvmap_page_pool_fill_lots_locked(pool, pages, nr);
366 mutex_unlock(&pool->lock);
371 bool nvmap_page_pool_fill(struct nvmap_page_pool *pool, struct page *page)
376 mutex_lock(&pool->lock);
377 ret = nvmap_page_pool_fill_locked(pool, page);
378 mutex_unlock(&pool->lock);
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.
389 static int nvmap_page_pool_free(struct nvmap_page_pool *pool, int nr_free)
397 mutex_lock(&pool->lock);
399 page = nvmap_page_pool_alloc_locked(pool, 1);
405 mutex_unlock(&pool->lock);
410 ulong nvmap_page_pool_get_unused_pages(void)
417 total = nvmap_dev->pool.count;
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.
426 int nvmap_page_pool_clear(void)
429 struct nvmap_page_pool *pool = &nvmap_dev->pool;
431 mutex_lock(&pool->lock);
433 while ((page = nvmap_page_pool_alloc_locked(pool, 1)) != NULL)
436 /* For some reason, if an error occured... */
437 if (!list_empty(&pool->page_list)) {
438 mutex_unlock(&pool->lock);
442 mutex_unlock(&pool->lock);
443 nvmap_pp_wake_up_allocator();
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.
453 static void nvmap_page_pool_resize(struct nvmap_page_pool *pool, int size)
455 if (!enable_pp || size == pool->max || size < 0)
458 mutex_lock(&pool->lock);
460 while (pool->count > size)
461 __free_page(nvmap_page_pool_alloc_locked(pool, 0));
465 pr_debug("page pool resized to %d from %d pages\n", size, pool->max);
466 mutex_unlock(&pool->lock);
469 static int nvmap_page_pool_shrink(struct shrinker *shrinker,
470 struct shrink_control *sc)
472 int shrink_pages = sc->nr_to_scan;
477 pr_debug("sh_pages=%d", shrink_pages);
479 shrink_pages = nvmap_page_pool_free(&nvmap_dev->pool, shrink_pages);
481 return nvmap_page_pool_get_unused_pages();
484 static struct shrinker nvmap_page_pool_shrinker = {
485 .shrink = nvmap_page_pool_shrink,
489 static void shrink_page_pools(int *total_pages, int *available_pages)
491 struct shrink_control sc;
493 if (*total_pages == 0) {
494 sc.gfp_mask = GFP_KERNEL;
496 *total_pages = nvmap_page_pool_shrink(NULL, &sc);
498 sc.nr_to_scan = *total_pages;
499 *available_pages = nvmap_page_pool_shrink(NULL, &sc);
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)
506 int cpu = smp_processor_id();
507 unsigned long long t1, t2;
508 int total_pages, available_pages;
510 param_set_int(arg, kp);
513 total_pages = shrink_pp;
515 shrink_page_pools(&total_pages, &available_pages);
517 pr_debug("shrink page pools: time=%lldns, "
518 "total_pages_released=%d, free_pages_available=%d",
519 t2-t1, total_pages, available_pages);
524 static int shrink_get(char *buff, const struct kernel_param *kp)
526 return param_get_int(buff, kp);
529 static struct kernel_param_ops shrink_ops = {
534 module_param_cb(shrink_page_pools, &shrink_ops, &shrink_pp, 0644);
537 static int enable_pp_set(const char *arg, const struct kernel_param *kp)
541 ret = param_set_bool(arg, kp);
546 nvmap_page_pool_clear();
551 static int enable_pp_get(char *buff, const struct kernel_param *kp)
553 return param_get_int(buff, kp);
556 static struct kernel_param_ops enable_pp_ops = {
557 .get = enable_pp_get,
558 .set = enable_pp_set,
561 module_param_cb(enable_page_pools, &enable_pp_ops, &enable_pp, 0644);
563 static int pool_size_set(const char *arg, const struct kernel_param *kp)
565 param_set_int(arg, kp);
566 nvmap_page_pool_resize(&nvmap_dev->pool, pool_size);
570 static int pool_size_get(char *buff, const struct kernel_param *kp)
572 return param_get_int(buff, kp);
575 static struct kernel_param_ops pool_size_ops = {
576 .get = pool_size_get,
577 .set = pool_size_set,
580 module_param_cb(pool_size, &pool_size_ops, &pool_size, 0644);
582 int nvmap_page_pool_debugfs_init(struct dentry *nvmap_root)
584 struct dentry *pp_root;
589 pp_root = debugfs_create_dir("pagepool", nvmap_root);
593 debugfs_create_u32("page_pool_available_pages",
595 &nvmap_dev->pool.count);
596 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
597 debugfs_create_u64("page_pool_allocs",
599 &nvmap_dev->pool.allocs);
600 debugfs_create_u64("page_pool_fills",
602 &nvmap_dev->pool.fills);
603 debugfs_create_u64("page_pool_hits",
605 &nvmap_dev->pool.hits);
606 debugfs_create_u64("page_pool_misses",
608 &nvmap_dev->pool.misses);
614 int nvmap_page_pool_init(struct nvmap_device *dev)
617 unsigned long totalram_mb;
619 struct nvmap_page_pool *pool = &dev->pool;
620 #ifdef CONFIG_NVMAP_PAGE_POOLS_INIT_FILLUP
624 int highmem_pages = 0;
627 memset(pool, 0x0, sizeof(*pool));
628 mutex_init(&pool->lock);
629 INIT_LIST_HEAD(&pool->page_list);
632 totalram_mb = (info.totalram * info.mem_unit) >> 20;
633 pr_info("Total MB RAM: %lu\n", totalram_mb);
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;
639 pool->max = CONFIG_NVMAP_PAGE_POOL_SIZE;
641 if (pool->max >= info.totalram)
643 pool_size = pool->max;
645 pr_info("nvmap page pool size: %u pages (%u MB)\n", pool->max,
646 (pool->max * info.mem_unit) >> 20);
650 register_shrinker(&nvmap_page_pool_shrinker);
653 background_allocator = kthread_create(nvmap_background_zero_allocator,
655 if (IS_ERR_OR_NULL(background_allocator))
658 #ifdef CONFIG_NVMAP_PAGE_POOLS_INIT_FILLUP
659 pages_to_fill = CONFIG_NVMAP_PAGE_POOLS_INIT_FILLUP_SIZE * SZ_1M /
661 pages_to_fill = pages_to_fill ? : pool->count;
663 for (i = 0; i < pages_to_fill; i++) {
664 page = alloc_page(GFP_NVMAP);
667 if (!nvmap_page_pool_fill_locked(pool, page)) {
671 if (PageHighMem(page))
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);
684 nvmap_page_pool_fini(dev);
688 int nvmap_page_pool_fini(struct nvmap_device *dev)
690 struct nvmap_page_pool *pool = &dev->pool;
692 if (!IS_ERR_OR_NULL(background_allocator))
693 kthread_stop(background_allocator);
695 WARN_ON(!list_empty(&pool->page_list));