2 * Copyright (c) Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Authors: Dave Airlie <airlied@redhat.com>
24 * Jerome Glisse <jglisse@redhat.com>
25 * Pauli Nieminen <suokkos@gmail.com>
28 /* simple list based uncached page pool
29 * - Pool collects resently freed pages for reuse
30 * - Use page->lru to keep a free list
31 * - doesn't track currently in use pages
34 #define pr_fmt(fmt) "[TTM] " fmt
36 #include <linux/list.h>
37 #include <linux/spinlock.h>
38 #include <linux/highmem.h>
39 #include <linux/mm_types.h>
40 #include <linux/module.h>
42 #include <linux/seq_file.h> /* for seq_printf */
43 #include <linux/slab.h>
44 #include <linux/dma-mapping.h>
46 #include <linux/atomic.h>
48 #include <drm/ttm/ttm_bo_driver.h>
49 #include <drm/ttm/ttm_page_alloc.h>
51 #include "ttm_set_memory.h"
53 #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
54 #define SMALL_ALLOCATION 16
55 #define FREE_ALL_PAGES (~0U)
56 /* times are in msecs */
57 #define PAGE_FREE_INTERVAL 1000
60 * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
62 * @lock: Protects the shared pool from concurrnet access. Must be used with
63 * irqsave/irqrestore variants because pool allocator maybe called from
65 * @fill_lock: Prevent concurrent calls to fill.
66 * @list: Pool of free uc/wc pages for fast reuse.
67 * @gfp_flags: Flags to pass for alloc_page.
68 * @npages: Number of pages in pool.
70 struct ttm_page_pool {
73 struct list_head list;
78 unsigned long nrefills;
83 * Limits for the pool. They are handled without locks because only place where
84 * they may change is in sysfs store. They won't have immediate effect anyway
85 * so forcing serialization to access them is pointless.
88 struct ttm_pool_opts {
97 * struct ttm_pool_manager - Holds memory pools for fst allocation
99 * Manager is read only object for pool code so it doesn't need locking.
101 * @free_interval: minimum number of jiffies between freeing pages from pool.
102 * @page_alloc_inited: reference counting for pool allocation.
103 * @work: Work that is used to shrink the pool. Work is only run when there is
104 * some pages to free.
105 * @small_allocation: Limit in number of pages what is small allocation.
107 * @pools: All pool objects in use.
109 struct ttm_pool_manager {
111 struct shrinker mm_shrink;
112 struct ttm_pool_opts options;
115 struct ttm_page_pool pools[NUM_POOLS];
117 struct ttm_page_pool wc_pool;
118 struct ttm_page_pool uc_pool;
119 struct ttm_page_pool wc_pool_dma32;
120 struct ttm_page_pool uc_pool_dma32;
121 struct ttm_page_pool wc_pool_huge;
122 struct ttm_page_pool uc_pool_huge;
127 static struct attribute ttm_page_pool_max = {
128 .name = "pool_max_size",
129 .mode = S_IRUGO | S_IWUSR
131 static struct attribute ttm_page_pool_small = {
132 .name = "pool_small_allocation",
133 .mode = S_IRUGO | S_IWUSR
135 static struct attribute ttm_page_pool_alloc_size = {
136 .name = "pool_allocation_size",
137 .mode = S_IRUGO | S_IWUSR
140 static struct attribute *ttm_pool_attrs[] = {
142 &ttm_page_pool_small,
143 &ttm_page_pool_alloc_size,
147 static void ttm_pool_kobj_release(struct kobject *kobj)
149 struct ttm_pool_manager *m =
150 container_of(kobj, struct ttm_pool_manager, kobj);
154 static ssize_t ttm_pool_store(struct kobject *kobj,
155 struct attribute *attr, const char *buffer, size_t size)
157 struct ttm_pool_manager *m =
158 container_of(kobj, struct ttm_pool_manager, kobj);
161 chars = sscanf(buffer, "%u", &val);
165 /* Convert kb to number of pages */
166 val = val / (PAGE_SIZE >> 10);
168 if (attr == &ttm_page_pool_max)
169 m->options.max_size = val;
170 else if (attr == &ttm_page_pool_small)
171 m->options.small = val;
172 else if (attr == &ttm_page_pool_alloc_size) {
173 if (val > NUM_PAGES_TO_ALLOC*8) {
174 pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
175 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
176 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
178 } else if (val > NUM_PAGES_TO_ALLOC) {
179 pr_warn("Setting allocation size to larger than %lu is not recommended\n",
180 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
182 m->options.alloc_size = val;
188 static ssize_t ttm_pool_show(struct kobject *kobj,
189 struct attribute *attr, char *buffer)
191 struct ttm_pool_manager *m =
192 container_of(kobj, struct ttm_pool_manager, kobj);
195 if (attr == &ttm_page_pool_max)
196 val = m->options.max_size;
197 else if (attr == &ttm_page_pool_small)
198 val = m->options.small;
199 else if (attr == &ttm_page_pool_alloc_size)
200 val = m->options.alloc_size;
202 val = val * (PAGE_SIZE >> 10);
204 return snprintf(buffer, PAGE_SIZE, "%u\n", val);
207 static const struct sysfs_ops ttm_pool_sysfs_ops = {
208 .show = &ttm_pool_show,
209 .store = &ttm_pool_store,
212 static struct kobj_type ttm_pool_kobj_type = {
213 .release = &ttm_pool_kobj_release,
214 .sysfs_ops = &ttm_pool_sysfs_ops,
215 .default_attrs = ttm_pool_attrs,
218 static struct ttm_pool_manager *_manager;
221 * Select the right pool or requested caching state and ttm flags. */
222 static struct ttm_page_pool *ttm_get_pool(int flags, bool huge,
223 enum ttm_caching_state cstate)
227 if (cstate == tt_cached)
235 if (flags & TTM_PAGE_FLAG_DMA32) {
244 return &_manager->pools[pool_index];
247 /* set memory back to wb and free the pages. */
248 static void ttm_pages_put(struct page *pages[], unsigned npages,
251 unsigned int i, pages_nr = (1 << order);
254 if (ttm_set_pages_array_wb(pages, npages))
255 pr_err("Failed to set %d pages to wb!\n", npages);
258 for (i = 0; i < npages; ++i) {
260 if (ttm_set_pages_wb(pages[i], pages_nr))
261 pr_err("Failed to set %d pages to wb!\n", pages_nr);
263 __free_pages(pages[i], order);
267 static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
268 unsigned freed_pages)
270 pool->npages -= freed_pages;
271 pool->nfrees += freed_pages;
275 * Free pages from pool.
277 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
278 * number of pages in one go.
280 * @pool: to free the pages from
281 * @free_all: If set to true will free all pages in pool
282 * @use_static: Safe to use static buffer
284 static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
287 static struct page *static_buf[NUM_PAGES_TO_ALLOC];
288 unsigned long irq_flags;
290 struct page **pages_to_free;
291 unsigned freed_pages = 0,
292 npages_to_free = nr_free;
294 if (NUM_PAGES_TO_ALLOC < nr_free)
295 npages_to_free = NUM_PAGES_TO_ALLOC;
298 pages_to_free = static_buf;
300 pages_to_free = kmalloc_array(npages_to_free,
301 sizeof(struct page *),
303 if (!pages_to_free) {
304 pr_debug("Failed to allocate memory for pool free operation\n");
309 spin_lock_irqsave(&pool->lock, irq_flags);
311 list_for_each_entry_reverse(p, &pool->list, lru) {
312 if (freed_pages >= npages_to_free)
315 pages_to_free[freed_pages++] = p;
316 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
317 if (freed_pages >= NUM_PAGES_TO_ALLOC) {
318 /* remove range of pages from the pool */
319 __list_del(p->lru.prev, &pool->list);
321 ttm_pool_update_free_locked(pool, freed_pages);
323 * Because changing page caching is costly
324 * we unlock the pool to prevent stalling.
326 spin_unlock_irqrestore(&pool->lock, irq_flags);
328 ttm_pages_put(pages_to_free, freed_pages, pool->order);
329 if (likely(nr_free != FREE_ALL_PAGES))
330 nr_free -= freed_pages;
332 if (NUM_PAGES_TO_ALLOC >= nr_free)
333 npages_to_free = nr_free;
335 npages_to_free = NUM_PAGES_TO_ALLOC;
339 /* free all so restart the processing */
343 /* Not allowed to fall through or break because
344 * following context is inside spinlock while we are
352 /* remove range of pages from the pool */
354 __list_del(&p->lru, &pool->list);
356 ttm_pool_update_free_locked(pool, freed_pages);
357 nr_free -= freed_pages;
360 spin_unlock_irqrestore(&pool->lock, irq_flags);
363 ttm_pages_put(pages_to_free, freed_pages, pool->order);
365 if (pages_to_free != static_buf)
366 kfree(pages_to_free);
371 * Callback for mm to request pool to reduce number of page held.
373 * XXX: (dchinner) Deadlock warning!
375 * This code is crying out for a shrinker per pool....
378 ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
380 static DEFINE_MUTEX(lock);
381 static unsigned start_pool;
383 unsigned pool_offset;
384 struct ttm_page_pool *pool;
385 int shrink_pages = sc->nr_to_scan;
386 unsigned long freed = 0;
387 unsigned int nr_free_pool;
389 if (!mutex_trylock(&lock))
391 pool_offset = ++start_pool % NUM_POOLS;
392 /* select start pool in round robin fashion */
393 for (i = 0; i < NUM_POOLS; ++i) {
394 unsigned nr_free = shrink_pages;
397 if (shrink_pages == 0)
400 pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
401 page_nr = (1 << pool->order);
402 /* OK to use static buffer since global mutex is held. */
403 nr_free_pool = roundup(nr_free, page_nr) >> pool->order;
404 shrink_pages = ttm_page_pool_free(pool, nr_free_pool, true);
405 freed += (nr_free_pool - shrink_pages) << pool->order;
406 if (freed >= sc->nr_to_scan)
408 shrink_pages <<= pool->order;
416 ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
419 unsigned long count = 0;
420 struct ttm_page_pool *pool;
422 for (i = 0; i < NUM_POOLS; ++i) {
423 pool = &_manager->pools[i];
424 count += (pool->npages << pool->order);
430 static int ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
432 manager->mm_shrink.count_objects = ttm_pool_shrink_count;
433 manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
434 manager->mm_shrink.seeks = 1;
435 return register_shrinker(&manager->mm_shrink);
438 static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
440 unregister_shrinker(&manager->mm_shrink);
443 static int ttm_set_pages_caching(struct page **pages,
444 enum ttm_caching_state cstate, unsigned cpages)
447 /* Set page caching */
450 r = ttm_set_pages_array_uc(pages, cpages);
452 pr_err("Failed to set %d pages to uc!\n", cpages);
455 r = ttm_set_pages_array_wc(pages, cpages);
457 pr_err("Failed to set %d pages to wc!\n", cpages);
466 * Free pages the pages that failed to change the caching state. If there is
467 * any pages that have changed their caching state already put them to the
470 static void ttm_handle_caching_failure(struct page **failed_pages,
475 /* Failed pages have to be freed */
476 for (i = 0; i < cpages; ++i) {
477 list_del(&failed_pages[i]->lru);
478 __free_page(failed_pages[i]);
483 * Allocate new pages with correct caching.
485 * This function is reentrant if caller updates count depending on number of
486 * pages returned in pages array.
488 static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
489 int ttm_flags, enum ttm_caching_state cstate,
490 unsigned count, unsigned order)
492 struct page **caching_array;
495 unsigned i, j, cpages;
496 unsigned npages = 1 << order;
497 unsigned max_cpages = min(count << order, (unsigned)NUM_PAGES_TO_ALLOC);
499 /* allocate array for page caching change */
500 caching_array = kmalloc_array(max_cpages, sizeof(struct page *),
503 if (!caching_array) {
504 pr_debug("Unable to allocate table for new pages\n");
508 for (i = 0, cpages = 0; i < count; ++i) {
509 p = alloc_pages(gfp_flags, order);
512 pr_debug("Unable to get page %u\n", i);
514 /* store already allocated pages in the pool after
515 * setting the caching state */
517 r = ttm_set_pages_caching(caching_array,
520 ttm_handle_caching_failure(caching_array,
527 list_add(&p->lru, pages);
529 #ifdef CONFIG_HIGHMEM
530 /* gfp flags of highmem page should never be dma32 so we
531 * we should be fine in such case
537 for (j = 0; j < npages; ++j) {
538 caching_array[cpages++] = p++;
539 if (cpages == max_cpages) {
541 r = ttm_set_pages_caching(caching_array,
544 ttm_handle_caching_failure(caching_array,
554 r = ttm_set_pages_caching(caching_array, cstate, cpages);
556 ttm_handle_caching_failure(caching_array, cpages);
559 kfree(caching_array);
565 * Fill the given pool if there aren't enough pages and the requested number of
568 static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
569 enum ttm_caching_state cstate,
570 unsigned count, unsigned long *irq_flags)
576 * Only allow one pool fill operation at a time.
577 * If pool doesn't have enough pages for the allocation new pages are
578 * allocated from outside of pool.
583 pool->fill_lock = true;
585 /* If allocation request is small and there are not enough
586 * pages in a pool we fill the pool up first. */
587 if (count < _manager->options.small
588 && count > pool->npages) {
589 struct list_head new_pages;
590 unsigned alloc_size = _manager->options.alloc_size;
593 * Can't change page caching if in irqsave context. We have to
594 * drop the pool->lock.
596 spin_unlock_irqrestore(&pool->lock, *irq_flags);
598 INIT_LIST_HEAD(&new_pages);
599 r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
600 cstate, alloc_size, 0);
601 spin_lock_irqsave(&pool->lock, *irq_flags);
604 list_splice(&new_pages, &pool->list);
606 pool->npages += alloc_size;
608 pr_debug("Failed to fill pool (%p)\n", pool);
609 /* If we have any pages left put them to the pool. */
610 list_for_each_entry(p, &new_pages, lru) {
613 list_splice(&new_pages, &pool->list);
614 pool->npages += cpages;
618 pool->fill_lock = false;
622 * Allocate pages from the pool and put them on the return list.
624 * @return zero for success or negative error code.
626 static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
627 struct list_head *pages,
629 enum ttm_caching_state cstate,
630 unsigned count, unsigned order)
632 unsigned long irq_flags;
637 spin_lock_irqsave(&pool->lock, irq_flags);
639 ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
642 if (count >= pool->npages) {
643 /* take all pages from the pool */
644 list_splice_init(&pool->list, pages);
645 count -= pool->npages;
649 /* find the last pages to include for requested number of pages. Split
650 * pool to begin and halve it to reduce search space. */
651 if (count <= pool->npages/2) {
653 list_for_each(p, &pool->list) {
658 i = pool->npages + 1;
659 list_for_each_prev(p, &pool->list) {
664 /* Cut 'count' number of pages from the pool */
665 list_cut_position(pages, &pool->list, p);
666 pool->npages -= count;
669 spin_unlock_irqrestore(&pool->lock, irq_flags);
671 /* clear the pages coming from the pool if requested */
672 if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
675 list_for_each_entry(page, pages, lru) {
676 if (PageHighMem(page))
677 clear_highpage(page);
679 clear_page(page_address(page));
683 /* If pool didn't have enough pages allocate new one. */
685 gfp_t gfp_flags = pool->gfp_flags;
687 /* set zero flag for page allocation if required */
688 if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
689 gfp_flags |= __GFP_ZERO;
691 if (ttm_flags & TTM_PAGE_FLAG_NO_RETRY)
692 gfp_flags |= __GFP_RETRY_MAYFAIL;
694 /* ttm_alloc_new_pages doesn't reference pool so we can run
695 * multiple requests in parallel.
697 r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
704 /* Put all pages in pages list to correct pool to wait for reuse */
705 static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
706 enum ttm_caching_state cstate)
708 struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
709 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
710 struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
712 unsigned long irq_flags;
716 /* No pool for this memory type so free the pages */
719 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
720 struct page *p = pages[i];
722 unsigned order = 0, j;
729 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
730 if (!(flags & TTM_PAGE_FLAG_DMA32) &&
731 (npages - i) >= HPAGE_PMD_NR) {
732 for (j = 1; j < HPAGE_PMD_NR; ++j)
733 if (++p != pages[i + j])
736 if (j == HPAGE_PMD_NR)
737 order = HPAGE_PMD_ORDER;
741 if (page_count(pages[i]) != 1)
742 pr_err("Erroneous page count. Leaking pages.\n");
743 __free_pages(pages[i], order);
755 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
757 unsigned max_size, n2free;
759 spin_lock_irqsave(&huge->lock, irq_flags);
760 while ((npages - i) >= HPAGE_PMD_NR) {
761 struct page *p = pages[i];
767 for (j = 1; j < HPAGE_PMD_NR; ++j)
768 if (++p != pages[i + j])
771 if (j != HPAGE_PMD_NR)
774 list_add_tail(&pages[i]->lru, &huge->list);
776 for (j = 0; j < HPAGE_PMD_NR; ++j)
781 /* Check that we don't go over the pool limit */
782 max_size = _manager->options.max_size;
783 max_size /= HPAGE_PMD_NR;
784 if (huge->npages > max_size)
785 n2free = huge->npages - max_size;
788 spin_unlock_irqrestore(&huge->lock, irq_flags);
790 ttm_page_pool_free(huge, n2free, false);
794 spin_lock_irqsave(&pool->lock, irq_flags);
797 if (page_count(pages[i]) != 1)
798 pr_err("Erroneous page count. Leaking pages.\n");
799 list_add_tail(&pages[i]->lru, &pool->list);
805 /* Check that we don't go over the pool limit */
807 if (pool->npages > _manager->options.max_size) {
808 npages = pool->npages - _manager->options.max_size;
809 /* free at least NUM_PAGES_TO_ALLOC number of pages
810 * to reduce calls to set_memory_wb */
811 if (npages < NUM_PAGES_TO_ALLOC)
812 npages = NUM_PAGES_TO_ALLOC;
814 spin_unlock_irqrestore(&pool->lock, irq_flags);
816 ttm_page_pool_free(pool, npages, false);
820 * On success pages list will hold count number of correctly
823 static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
824 enum ttm_caching_state cstate)
826 struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
827 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
828 struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
830 struct list_head plist;
831 struct page *p = NULL;
832 unsigned count, first;
835 /* No pool for cached pages */
837 gfp_t gfp_flags = GFP_USER;
839 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
843 /* set zero flag for page allocation if required */
844 if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
845 gfp_flags |= __GFP_ZERO;
847 if (flags & TTM_PAGE_FLAG_NO_RETRY)
848 gfp_flags |= __GFP_RETRY_MAYFAIL;
850 if (flags & TTM_PAGE_FLAG_DMA32)
851 gfp_flags |= GFP_DMA32;
853 gfp_flags |= GFP_HIGHUSER;
856 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
857 if (!(gfp_flags & GFP_DMA32)) {
858 while (npages >= HPAGE_PMD_NR) {
859 gfp_t huge_flags = gfp_flags;
861 huge_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
862 __GFP_KSWAPD_RECLAIM;
863 huge_flags &= ~__GFP_MOVABLE;
864 huge_flags &= ~__GFP_COMP;
865 p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
869 for (j = 0; j < HPAGE_PMD_NR; ++j)
872 npages -= HPAGE_PMD_NR;
879 p = alloc_page(gfp_flags);
881 pr_debug("Unable to allocate page\n");
885 /* Swap the pages if we detect consecutive order */
886 if (i > first && pages[i - 1] == p - 1)
887 swap(p, pages[i - 1]);
897 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
898 if (huge && npages >= HPAGE_PMD_NR) {
899 INIT_LIST_HEAD(&plist);
900 ttm_page_pool_get_pages(huge, &plist, flags, cstate,
901 npages / HPAGE_PMD_NR,
904 list_for_each_entry(p, &plist, lru) {
907 for (j = 0; j < HPAGE_PMD_NR; ++j)
908 pages[count++] = &p[j];
913 INIT_LIST_HEAD(&plist);
914 r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
918 list_for_each_entry(p, &plist, lru) {
919 struct page *tmp = p;
921 /* Swap the pages if we detect consecutive order */
922 if (count > first && pages[count - 1] == tmp - 1)
923 swap(tmp, pages[count - 1]);
924 pages[count++] = tmp;
928 /* If there is any pages in the list put them back to
931 pr_debug("Failed to allocate extra pages for large request\n");
932 ttm_put_pages(pages, count, flags, cstate);
939 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
940 char *name, unsigned int order)
942 spin_lock_init(&pool->lock);
943 pool->fill_lock = false;
944 INIT_LIST_HEAD(&pool->list);
945 pool->npages = pool->nfrees = 0;
946 pool->gfp_flags = flags;
951 int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
954 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
955 unsigned order = HPAGE_PMD_ORDER;
962 pr_info("Initializing pool allocator\n");
964 _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
968 ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
970 ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
972 ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
973 GFP_USER | GFP_DMA32, "wc dma", 0);
975 ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
976 GFP_USER | GFP_DMA32, "uc dma", 0);
978 ttm_page_pool_init_locked(&_manager->wc_pool_huge,
979 (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
980 __GFP_KSWAPD_RECLAIM) &
981 ~(__GFP_MOVABLE | __GFP_COMP),
984 ttm_page_pool_init_locked(&_manager->uc_pool_huge,
985 (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
986 __GFP_KSWAPD_RECLAIM) &
987 ~(__GFP_MOVABLE | __GFP_COMP)
990 _manager->options.max_size = max_pages;
991 _manager->options.small = SMALL_ALLOCATION;
992 _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
994 ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
995 &glob->kobj, "pool");
996 if (unlikely(ret != 0))
999 ret = ttm_pool_mm_shrink_init(_manager);
1000 if (unlikely(ret != 0))
1005 kobject_put(&_manager->kobj);
1010 void ttm_page_alloc_fini(void)
1014 pr_info("Finalizing pool allocator\n");
1015 ttm_pool_mm_shrink_fini(_manager);
1017 /* OK to use static buffer since global mutex is no longer used. */
1018 for (i = 0; i < NUM_POOLS; ++i)
1019 ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
1021 kobject_put(&_manager->kobj);
1026 ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
1028 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1031 if (mem_count_update == 0)
1034 for (i = 0; i < mem_count_update; ++i) {
1038 ttm_mem_global_free_page(mem_glob, ttm->pages[i], PAGE_SIZE);
1042 ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1043 ttm->caching_state);
1044 ttm_tt_set_unpopulated(ttm);
1047 int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
1049 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1053 if (ttm_tt_is_populated(ttm))
1056 if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx))
1059 ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1060 ttm->caching_state);
1061 if (unlikely(ret != 0)) {
1062 ttm_pool_unpopulate_helper(ttm, 0);
1066 for (i = 0; i < ttm->num_pages; ++i) {
1067 ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
1069 if (unlikely(ret != 0)) {
1070 ttm_pool_unpopulate_helper(ttm, i);
1075 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
1076 ret = ttm_tt_swapin(ttm);
1077 if (unlikely(ret != 0)) {
1078 ttm_pool_unpopulate(ttm);
1083 ttm_tt_set_populated(ttm);
1086 EXPORT_SYMBOL(ttm_pool_populate);
1088 void ttm_pool_unpopulate(struct ttm_tt *ttm)
1090 ttm_pool_unpopulate_helper(ttm, ttm->num_pages);
1092 EXPORT_SYMBOL(ttm_pool_unpopulate);
1094 int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt,
1095 struct ttm_operation_ctx *ctx)
1100 r = ttm_pool_populate(&tt->ttm, ctx);
1104 for (i = 0; i < tt->ttm.num_pages; ++i) {
1105 struct page *p = tt->ttm.pages[i];
1106 size_t num_pages = 1;
1108 for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1109 if (++p != tt->ttm.pages[j])
1115 tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
1116 0, num_pages * PAGE_SIZE,
1118 if (dma_mapping_error(dev, tt->dma_address[i])) {
1120 dma_unmap_page(dev, tt->dma_address[i],
1121 PAGE_SIZE, DMA_BIDIRECTIONAL);
1122 tt->dma_address[i] = 0;
1124 ttm_pool_unpopulate(&tt->ttm);
1128 for (j = 1; j < num_pages; ++j) {
1129 tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
1135 EXPORT_SYMBOL(ttm_populate_and_map_pages);
1137 void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
1141 for (i = 0; i < tt->ttm.num_pages;) {
1142 struct page *p = tt->ttm.pages[i];
1143 size_t num_pages = 1;
1145 if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
1150 for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1151 if (++p != tt->ttm.pages[j])
1157 dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
1162 ttm_pool_unpopulate(&tt->ttm);
1164 EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
1166 int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
1168 struct ttm_page_pool *p;
1170 char *h[] = {"pool", "refills", "pages freed", "size"};
1172 seq_printf(m, "No pool allocator running.\n");
1175 seq_printf(m, "%7s %12s %13s %8s\n",
1176 h[0], h[1], h[2], h[3]);
1177 for (i = 0; i < NUM_POOLS; ++i) {
1178 p = &_manager->pools[i];
1180 seq_printf(m, "%7s %12ld %13ld %8d\n",
1181 p->name, p->nrefills,
1182 p->nfrees, p->npages);
1186 EXPORT_SYMBOL(ttm_page_alloc_debugfs);