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>
31 #include <linux/freezer.h>
32 #include <linux/highmem.h>
34 #include "nvmap_priv.h"
36 #define NVMAP_TEST_PAGE_POOL_SHRINKER 1
37 #define PENDING_PAGES_SIZE (SZ_1M / PAGE_SIZE)
39 static bool enable_pp = 1;
42 static struct task_struct *background_allocator;
43 static DECLARE_WAIT_QUEUE_HEAD(nvmap_bg_wait);
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);
63 * Make sure any data in the caches is cleaned out before
64 * passing these pages to userspace. otherwise, It can lead to
65 * corruption in pages that get mapped as something
66 * other than WB in userspace and leaked kernel data.
68 * Must be called with pool->lock held.
70 static void pp_clean_cache(struct nvmap_page_pool *pool)
72 if (pool->contains_dirty_pages) {
73 inner_clean_cache_all();
75 pool->contains_dirty_pages = false;
79 static inline struct page *get_zero_list_page(struct nvmap_page_pool *pool)
83 if (list_empty(&pool->zero_list))
86 page = list_first_entry(&pool->zero_list, struct page, lru);
94 static inline struct page *get_page_list_page(struct nvmap_page_pool *pool)
98 if (list_empty(&pool->page_list))
101 page = list_first_entry(&pool->page_list, struct page, lru);
102 list_del(&page->lru);
109 static inline bool nvmap_bg_should_run(struct nvmap_page_pool *pool)
113 mutex_lock(&pool->lock);
114 ret = (pool->to_zero > 0);
115 mutex_unlock(&pool->lock);
120 static int nvmap_pp_zero_pages(struct page **pages, int nr)
124 for (i = 0; i < nr; i++)
125 clear_highpage(pages[i]);
130 static void nvmap_pp_do_background_zero_pages(struct nvmap_page_pool *pool)
137 * Statically declared array of pages to be zeroed in a batch,
138 * local to this thread but too big for the stack.
140 static struct page *pending_zero_pages[PENDING_PAGES_SIZE];
142 mutex_lock(&pool->lock);
143 for (i = 0; i < PENDING_PAGES_SIZE; i++) {
144 page = get_zero_list_page(pool);
147 pending_zero_pages[i] = page;
149 mutex_unlock(&pool->lock);
151 ret = nvmap_pp_zero_pages(pending_zero_pages, i);
157 mutex_lock(&pool->lock);
158 ret = __nvmap_page_pool_fill_lots_locked(pool, pending_zero_pages, i);
159 mutex_unlock(&pool->lock);
162 for (; ret < i; ret++)
163 __free_page(pending_zero_pages[ret]);
165 /* clean cache in the background so that allocations immediately
166 * after fill don't suffer the cache clean overhead.
168 mutex_lock(&pool->lock);
169 pp_clean_cache(pool);
170 mutex_unlock(&pool->lock);
174 * This thread fills the page pools with zeroed pages. We avoid releasing the
175 * pages directly back into the page pools since we would then have to zero
176 * them ourselves. Instead it is easier to just reallocate zeroed pages. This
177 * happens in the background so that the overhead of allocating zeroed pages is
178 * not directly seen by userspace. Of course if the page pools are empty user
181 static int nvmap_background_zero_thread(void *arg)
183 struct nvmap_page_pool *pool = &nvmap_dev->pool;
184 struct sched_param param = { .sched_priority = 0 };
186 pr_info("PP zeroing thread starting.\n");
189 sched_setscheduler(current, SCHED_IDLE, ¶m);
191 while (!kthread_should_stop()) {
192 while (nvmap_bg_should_run(pool)) {
193 nvmap_pp_do_background_zero_pages(pool);
196 wait_event_freezable(nvmap_bg_wait,
197 nvmap_bg_should_run(pool) ||
198 kthread_should_stop());
205 * This removes a page from the page pool. If ignore_disable is set, then
206 * the enable_pp flag is ignored.
208 static struct page *nvmap_page_pool_alloc_locked(struct nvmap_page_pool *pool,
213 if (!force_alloc && !enable_pp)
216 if (list_empty(&pool->page_list)) {
217 pp_miss_add(pool, 1);
221 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG))
222 BUG_ON(pool->count == 0);
224 pp_clean_cache(pool);
225 page = get_page_list_page(pool);
230 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
231 atomic_dec(&page->_count);
232 BUG_ON(atomic_read(&page->_count) != 1);
235 pp_alloc_add(pool, 1);
242 * Alloc a bunch of pages from the page pool. This will alloc as many as it can
243 * and return the number of pages allocated. Pages are placed into the passed
244 * array in a linear fashion starting from index 0.
246 int nvmap_page_pool_alloc_lots(struct nvmap_page_pool *pool,
247 struct page **pages, u32 nr)
255 mutex_lock(&pool->lock);
256 pp_clean_cache(pool);
258 real_nr = min_t(u32, nr, pool->count);
262 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG))
263 BUG_ON(list_empty(&pool->page_list));
264 page = get_page_list_page(pool);
266 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
267 atomic_dec(&page->_count);
268 BUG_ON(atomic_read(&page->_count) != 1);
271 mutex_unlock(&pool->lock);
273 pp_alloc_add(pool, ind);
274 pp_hit_add(pool, ind);
275 pp_miss_add(pool, nr - ind);
281 * Fill a bunch of pages into the page pool. This will fill as many as it can
282 * and return the number of pages filled. Pages are used from the start of the
283 * passed page pointer array in a linear fashion.
285 * You must lock the page pool before using this.
287 static int __nvmap_page_pool_fill_lots_locked(struct nvmap_page_pool *pool,
288 struct page **pages, u32 nr)
296 pool->contains_dirty_pages = true;
298 real_nr = min_t(u32, pool->max - pool->count, nr);
303 if (IS_ENABLED(CONFIG_NVMAP_PAGE_POOL_DEBUG)) {
304 atomic_inc(&pages[ind]->_count);
305 BUG_ON(atomic_read(&pages[ind]->_count) != 2);
307 list_add_tail(&pages[ind++]->lru, &pool->page_list);
311 pp_fill_add(pool, ind);
316 int nvmap_page_pool_fill_lots(struct nvmap_page_pool *pool,
317 struct page **pages, u32 nr)
321 mutex_lock(&pool->lock);
325 nr = min(nr, pool->max - pool->count - pool->to_zero);
327 for (i = 0; i < nr; i++) {
328 list_add_tail(&pages[i]->lru, &pool->zero_list);
332 wake_up_interruptible(&nvmap_bg_wait);
336 ret = __nvmap_page_pool_fill_lots_locked(pool, pages, nr);
338 mutex_unlock(&pool->lock);
344 * Free the passed number of pages from the page pool. This happen irregardless
345 * of whether ther page pools are enabled. This lets one disable the page pools
346 * and then free all the memory therein.
348 static int nvmap_page_pool_free(struct nvmap_page_pool *pool, int nr_free)
356 mutex_lock(&pool->lock);
358 page = get_zero_list_page(pool);
360 page = nvmap_page_pool_alloc_locked(pool, 1);
366 mutex_unlock(&pool->lock);
371 ulong nvmap_page_pool_get_unused_pages(void)
378 total = nvmap_dev->pool.count + nvmap_dev->pool.to_zero;
384 * Remove and free to the system all the pages currently in the page
385 * pool. This operation will happen even if the page pools are disabled.
387 int nvmap_page_pool_clear(void)
390 struct nvmap_page_pool *pool = &nvmap_dev->pool;
392 mutex_lock(&pool->lock);
394 while ((page = nvmap_page_pool_alloc_locked(pool, 1)) != NULL)
397 while (!list_empty(&pool->zero_list)) {
398 page = get_zero_list_page(pool);
402 /* For some reason, if an error occured... */
403 if (!list_empty(&pool->page_list) || !list_empty(&pool->zero_list)) {
404 mutex_unlock(&pool->lock);
408 mutex_unlock(&pool->lock);
414 * Resizes the page pool to the passed size. If the passed size is 0 then
415 * all associated resources are released back to the system. This operation
416 * will only occur if the page pools are enabled.
418 static void nvmap_page_pool_resize(struct nvmap_page_pool *pool, int size)
420 if (!enable_pp || size == pool->max || size < 0)
423 mutex_lock(&pool->lock);
425 while (pool->count > size)
426 __free_page(nvmap_page_pool_alloc_locked(pool, 0));
430 pr_debug("page pool resized to %d from %d pages\n", size, pool->max);
431 mutex_unlock(&pool->lock);
434 static int nvmap_page_pool_shrink(struct shrinker *shrinker,
435 struct shrink_control *sc)
437 int shrink_pages = sc->nr_to_scan;
442 pr_debug("sh_pages=%d", shrink_pages);
444 shrink_pages = nvmap_page_pool_free(&nvmap_dev->pool, shrink_pages);
446 return nvmap_page_pool_get_unused_pages();
449 static struct shrinker nvmap_page_pool_shrinker = {
450 .shrink = nvmap_page_pool_shrink,
454 static void shrink_page_pools(int *total_pages, int *available_pages)
456 struct shrink_control sc;
458 if (*total_pages == 0) {
459 sc.gfp_mask = GFP_KERNEL;
461 *total_pages = nvmap_page_pool_shrink(NULL, &sc);
463 sc.nr_to_scan = *total_pages;
464 *available_pages = nvmap_page_pool_shrink(NULL, &sc);
467 #if NVMAP_TEST_PAGE_POOL_SHRINKER
468 static int shrink_pp;
469 static int shrink_set(const char *arg, const struct kernel_param *kp)
471 int cpu = smp_processor_id();
472 unsigned long long t1, t2;
473 int total_pages, available_pages;
475 param_set_int(arg, kp);
478 total_pages = shrink_pp;
480 shrink_page_pools(&total_pages, &available_pages);
482 pr_debug("shrink page pools: time=%lldns, "
483 "total_pages_released=%d, free_pages_available=%d",
484 t2-t1, total_pages, available_pages);
489 static int shrink_get(char *buff, const struct kernel_param *kp)
491 return param_get_int(buff, kp);
494 static struct kernel_param_ops shrink_ops = {
499 module_param_cb(shrink_page_pools, &shrink_ops, &shrink_pp, 0644);
502 static int enable_pp_set(const char *arg, const struct kernel_param *kp)
506 ret = param_set_bool(arg, kp);
511 nvmap_page_pool_clear();
516 static int enable_pp_get(char *buff, const struct kernel_param *kp)
518 return param_get_int(buff, kp);
521 static struct kernel_param_ops enable_pp_ops = {
522 .get = enable_pp_get,
523 .set = enable_pp_set,
526 module_param_cb(enable_page_pools, &enable_pp_ops, &enable_pp, 0644);
528 static int pool_size_set(const char *arg, const struct kernel_param *kp)
530 param_set_int(arg, kp);
531 nvmap_page_pool_resize(&nvmap_dev->pool, pool_size);
535 static int pool_size_get(char *buff, const struct kernel_param *kp)
537 return param_get_int(buff, kp);
540 static struct kernel_param_ops pool_size_ops = {
541 .get = pool_size_get,
542 .set = pool_size_set,
545 module_param_cb(pool_size, &pool_size_ops, &pool_size, 0644);
547 int nvmap_page_pool_debugfs_init(struct dentry *nvmap_root)
549 struct dentry *pp_root;
554 pp_root = debugfs_create_dir("pagepool", nvmap_root);
558 debugfs_create_u32("page_pool_available_pages",
560 &nvmap_dev->pool.count);
561 debugfs_create_u32("page_pool_pages_to_zero",
563 &nvmap_dev->pool.to_zero);
564 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
565 debugfs_create_u64("page_pool_allocs",
567 &nvmap_dev->pool.allocs);
568 debugfs_create_u64("page_pool_fills",
570 &nvmap_dev->pool.fills);
571 debugfs_create_u64("page_pool_hits",
573 &nvmap_dev->pool.hits);
574 debugfs_create_u64("page_pool_misses",
576 &nvmap_dev->pool.misses);
582 int nvmap_page_pool_init(struct nvmap_device *dev)
584 unsigned long totalram_mb;
586 struct nvmap_page_pool *pool = &dev->pool;
588 memset(pool, 0x0, sizeof(*pool));
589 mutex_init(&pool->lock);
590 INIT_LIST_HEAD(&pool->page_list);
591 INIT_LIST_HEAD(&pool->zero_list);
594 totalram_mb = (info.totalram * info.mem_unit) >> 20;
595 pr_info("Total MB RAM: %lu\n", totalram_mb);
597 if (!CONFIG_NVMAP_PAGE_POOL_SIZE)
598 /* The ratio is pool pages per 1K ram pages. So, the >> 10. */
599 pool->max = (info.totalram * NVMAP_PP_POOL_SIZE) >> 10;
601 pool->max = CONFIG_NVMAP_PAGE_POOL_SIZE;
603 if (pool->max >= info.totalram)
605 pool_size = pool->max;
607 pr_info("nvmap page pool size: %u pages (%u MB)\n", pool->max,
608 (pool->max * info.mem_unit) >> 20);
610 background_allocator = kthread_run(nvmap_background_zero_thread,
612 if (IS_ERR_OR_NULL(background_allocator))
615 register_shrinker(&nvmap_page_pool_shrinker);
619 nvmap_page_pool_fini(dev);
623 int nvmap_page_pool_fini(struct nvmap_device *dev)
625 struct nvmap_page_pool *pool = &dev->pool;
627 if (!IS_ERR_OR_NULL(background_allocator))
628 kthread_stop(background_allocator);
630 WARN_ON(!list_empty(&pool->page_list));