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
33 #include <linux/list.h>
34 #include <linux/spinlock.h>
35 #include <linux/highmem.h>
36 #include <linux/mm_types.h>
37 #include <linux/module.h>
39 #include <linux/seq_file.h> /* for seq_printf */
40 #include <linux/slab.h>
42 #include <asm/atomic.h>
44 #include "ttm/ttm_bo_driver.h"
45 #include "ttm/ttm_page_alloc.h"
51 #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
52 #define SMALL_ALLOCATION 16
53 #define FREE_ALL_PAGES (~0U)
54 /* times are in msecs */
55 #define PAGE_FREE_INTERVAL 1000
58 * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
60 * @lock: Protects the shared pool from concurrnet access. Must be used with
61 * irqsave/irqrestore variants because pool allocator maybe called from
63 * @fill_lock: Prevent concurrent calls to fill.
64 * @list: Pool of free uc/wc pages for fast reuse.
65 * @gfp_flags: Flags to pass for alloc_page.
66 * @npages: Number of pages in pool.
68 struct ttm_page_pool
{
71 struct list_head list
;
76 unsigned long nrefills
;
80 * Limits for the pool. They are handled without locks because only place where
81 * they may change is in sysfs store. They won't have immediate effect anyway
82 * so forcing serialization to access them is pointless.
85 struct ttm_pool_opts
{
94 * struct ttm_pool_manager - Holds memory pools for fst allocation
96 * Manager is read only object for pool code so it doesn't need locking.
98 * @free_interval: minimum number of jiffies between freeing pages from pool.
99 * @page_alloc_inited: reference counting for pool allocation.
100 * @work: Work that is used to shrink the pool. Work is only run when there is
101 * some pages to free.
102 * @small_allocation: Limit in number of pages what is small allocation.
104 * @pools: All pool objects in use.
106 struct ttm_pool_manager
{
108 struct shrinker mm_shrink
;
109 struct ttm_pool_opts options
;
112 struct ttm_page_pool pools
[NUM_POOLS
];
114 struct ttm_page_pool wc_pool
;
115 struct ttm_page_pool uc_pool
;
116 struct ttm_page_pool wc_pool_dma32
;
117 struct ttm_page_pool uc_pool_dma32
;
122 static struct attribute ttm_page_pool_max
= {
123 .name
= "pool_max_size",
124 .mode
= S_IRUGO
| S_IWUSR
126 static struct attribute ttm_page_pool_small
= {
127 .name
= "pool_small_allocation",
128 .mode
= S_IRUGO
| S_IWUSR
130 static struct attribute ttm_page_pool_alloc_size
= {
131 .name
= "pool_allocation_size",
132 .mode
= S_IRUGO
| S_IWUSR
135 static struct attribute
*ttm_pool_attrs
[] = {
137 &ttm_page_pool_small
,
138 &ttm_page_pool_alloc_size
,
142 static void ttm_pool_kobj_release(struct kobject
*kobj
)
144 struct ttm_pool_manager
*m
=
145 container_of(kobj
, struct ttm_pool_manager
, kobj
);
149 static ssize_t
ttm_pool_store(struct kobject
*kobj
,
150 struct attribute
*attr
, const char *buffer
, size_t size
)
152 struct ttm_pool_manager
*m
=
153 container_of(kobj
, struct ttm_pool_manager
, kobj
);
156 chars
= sscanf(buffer
, "%u", &val
);
160 /* Convert kb to number of pages */
161 val
= val
/ (PAGE_SIZE
>> 10);
163 if (attr
== &ttm_page_pool_max
)
164 m
->options
.max_size
= val
;
165 else if (attr
== &ttm_page_pool_small
)
166 m
->options
.small
= val
;
167 else if (attr
== &ttm_page_pool_alloc_size
) {
168 if (val
> NUM_PAGES_TO_ALLOC
*8) {
169 printk(KERN_ERR TTM_PFX
170 "Setting allocation size to %lu "
171 "is not allowed. Recommended size is "
173 NUM_PAGES_TO_ALLOC
*(PAGE_SIZE
>> 7),
174 NUM_PAGES_TO_ALLOC
*(PAGE_SIZE
>> 10));
176 } else if (val
> NUM_PAGES_TO_ALLOC
) {
177 printk(KERN_WARNING TTM_PFX
178 "Setting allocation size to "
179 "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 static int set_pages_array_wb(struct page
**pages
, int addrinarray
)
226 for (i
= 0; i
< addrinarray
; i
++)
227 unmap_page_from_agp(pages
[i
]);
232 static int set_pages_array_wc(struct page
**pages
, int addrinarray
)
237 for (i
= 0; i
< addrinarray
; i
++)
238 map_page_into_agp(pages
[i
]);
243 static int set_pages_array_uc(struct page
**pages
, int addrinarray
)
248 for (i
= 0; i
< addrinarray
; i
++)
249 map_page_into_agp(pages
[i
]);
256 * Select the right pool or requested caching state and ttm flags. */
257 static struct ttm_page_pool
*ttm_get_pool(int flags
,
258 enum ttm_caching_state cstate
)
262 if (cstate
== tt_cached
)
270 if (flags
& TTM_PAGE_FLAG_DMA32
)
273 return &_manager
->pools
[pool_index
];
276 /* set memory back to wb and free the pages. */
277 static void ttm_pages_put(struct page
*pages
[], unsigned npages
)
280 if (set_pages_array_wb(pages
, npages
))
281 printk(KERN_ERR TTM_PFX
"Failed to set %d pages to wb!\n",
283 for (i
= 0; i
< npages
; ++i
)
284 __free_page(pages
[i
]);
287 static void ttm_pool_update_free_locked(struct ttm_page_pool
*pool
,
288 unsigned freed_pages
)
290 pool
->npages
-= freed_pages
;
291 pool
->nfrees
+= freed_pages
;
295 * Free pages from pool.
297 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
298 * number of pages in one go.
300 * @pool: to free the pages from
301 * @free_all: If set to true will free all pages in pool
303 static int ttm_page_pool_free(struct ttm_page_pool
*pool
, unsigned nr_free
)
305 unsigned long irq_flags
;
307 struct page
**pages_to_free
;
308 unsigned freed_pages
= 0,
309 npages_to_free
= nr_free
;
311 if (NUM_PAGES_TO_ALLOC
< nr_free
)
312 npages_to_free
= NUM_PAGES_TO_ALLOC
;
314 pages_to_free
= kmalloc(npages_to_free
* sizeof(struct page
*),
316 if (!pages_to_free
) {
317 printk(KERN_ERR TTM_PFX
318 "Failed to allocate memory for pool free operation.\n");
323 spin_lock_irqsave(&pool
->lock
, irq_flags
);
325 list_for_each_entry_reverse(p
, &pool
->list
, lru
) {
326 if (freed_pages
>= npages_to_free
)
329 pages_to_free
[freed_pages
++] = p
;
330 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
331 if (freed_pages
>= NUM_PAGES_TO_ALLOC
) {
332 /* remove range of pages from the pool */
333 __list_del(p
->lru
.prev
, &pool
->list
);
335 ttm_pool_update_free_locked(pool
, freed_pages
);
337 * Because changing page caching is costly
338 * we unlock the pool to prevent stalling.
340 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
342 ttm_pages_put(pages_to_free
, freed_pages
);
343 if (likely(nr_free
!= FREE_ALL_PAGES
))
344 nr_free
-= freed_pages
;
346 if (NUM_PAGES_TO_ALLOC
>= nr_free
)
347 npages_to_free
= nr_free
;
349 npages_to_free
= NUM_PAGES_TO_ALLOC
;
353 /* free all so restart the processing */
357 /* Not allowed to fall tough or break because
358 * following context is inside spinlock while we are
366 /* remove range of pages from the pool */
368 __list_del(&p
->lru
, &pool
->list
);
370 ttm_pool_update_free_locked(pool
, freed_pages
);
371 nr_free
-= freed_pages
;
374 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
377 ttm_pages_put(pages_to_free
, freed_pages
);
379 kfree(pages_to_free
);
383 /* Get good estimation how many pages are free in pools */
384 static int ttm_pool_get_num_unused_pages(void)
388 for (i
= 0; i
< NUM_POOLS
; ++i
)
389 total
+= _manager
->pools
[i
].npages
;
395 * Callback for mm to request pool to reduce number of page held.
397 static int ttm_pool_mm_shrink(struct shrinker
*shrink
, int shrink_pages
, gfp_t gfp_mask
)
399 static atomic_t start_pool
= ATOMIC_INIT(0);
401 unsigned pool_offset
= atomic_add_return(1, &start_pool
);
402 struct ttm_page_pool
*pool
;
404 pool_offset
= pool_offset
% NUM_POOLS
;
405 /* select start pool in round robin fashion */
406 for (i
= 0; i
< NUM_POOLS
; ++i
) {
407 unsigned nr_free
= shrink_pages
;
408 if (shrink_pages
== 0)
410 pool
= &_manager
->pools
[(i
+ pool_offset
)%NUM_POOLS
];
411 shrink_pages
= ttm_page_pool_free(pool
, nr_free
);
413 /* return estimated number of unused pages in pool */
414 return ttm_pool_get_num_unused_pages();
417 static void ttm_pool_mm_shrink_init(struct ttm_pool_manager
*manager
)
419 manager
->mm_shrink
.shrink
= &ttm_pool_mm_shrink
;
420 manager
->mm_shrink
.seeks
= 1;
421 register_shrinker(&manager
->mm_shrink
);
424 static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager
*manager
)
426 unregister_shrinker(&manager
->mm_shrink
);
429 static int ttm_set_pages_caching(struct page
**pages
,
430 enum ttm_caching_state cstate
, unsigned cpages
)
433 /* Set page caching */
436 r
= set_pages_array_uc(pages
, cpages
);
438 printk(KERN_ERR TTM_PFX
439 "Failed to set %d pages to uc!\n",
443 r
= set_pages_array_wc(pages
, cpages
);
445 printk(KERN_ERR TTM_PFX
446 "Failed to set %d pages to wc!\n",
456 * Free pages the pages that failed to change the caching state. If there is
457 * any pages that have changed their caching state already put them to the
460 static void ttm_handle_caching_state_failure(struct list_head
*pages
,
461 int ttm_flags
, enum ttm_caching_state cstate
,
462 struct page
**failed_pages
, unsigned cpages
)
465 /* Failed pages have to be freed */
466 for (i
= 0; i
< cpages
; ++i
) {
467 list_del(&failed_pages
[i
]->lru
);
468 __free_page(failed_pages
[i
]);
473 * Allocate new pages with correct caching.
475 * This function is reentrant if caller updates count depending on number of
476 * pages returned in pages array.
478 static int ttm_alloc_new_pages(struct list_head
*pages
, int gfp_flags
,
479 int ttm_flags
, enum ttm_caching_state cstate
, unsigned count
)
481 struct page
**caching_array
;
485 unsigned max_cpages
= min(count
,
486 (unsigned)(PAGE_SIZE
/sizeof(struct page
*)));
488 /* allocate array for page caching change */
489 caching_array
= kmalloc(max_cpages
*sizeof(struct page
*), GFP_KERNEL
);
491 if (!caching_array
) {
492 printk(KERN_ERR TTM_PFX
493 "Unable to allocate table for new pages.");
497 for (i
= 0, cpages
= 0; i
< count
; ++i
) {
498 p
= alloc_page(gfp_flags
);
501 printk(KERN_ERR TTM_PFX
"Unable to get page %u.\n", i
);
503 /* store already allocated pages in the pool after
504 * setting the caching state */
506 r
= ttm_set_pages_caching(caching_array
,
509 ttm_handle_caching_state_failure(pages
,
511 caching_array
, cpages
);
517 #ifdef CONFIG_HIGHMEM
518 /* gfp flags of highmem page should never be dma32 so we
519 * we should be fine in such case
524 caching_array
[cpages
++] = p
;
525 if (cpages
== max_cpages
) {
527 r
= ttm_set_pages_caching(caching_array
,
530 ttm_handle_caching_state_failure(pages
,
532 caching_array
, cpages
);
539 list_add(&p
->lru
, pages
);
543 r
= ttm_set_pages_caching(caching_array
, cstate
, cpages
);
545 ttm_handle_caching_state_failure(pages
,
547 caching_array
, cpages
);
550 kfree(caching_array
);
556 * Fill the given pool if there isn't enough pages and requested number of
559 static void ttm_page_pool_fill_locked(struct ttm_page_pool
*pool
,
560 int ttm_flags
, enum ttm_caching_state cstate
, unsigned count
,
561 unsigned long *irq_flags
)
567 * Only allow one pool fill operation at a time.
568 * If pool doesn't have enough pages for the allocation new pages are
569 * allocated from outside of pool.
574 pool
->fill_lock
= true;
576 /* If allocation request is small and there is not enough
577 * pages in pool we fill the pool first */
578 if (count
< _manager
->options
.small
579 && count
> pool
->npages
) {
580 struct list_head new_pages
;
581 unsigned alloc_size
= _manager
->options
.alloc_size
;
584 * Can't change page caching if in irqsave context. We have to
585 * drop the pool->lock.
587 spin_unlock_irqrestore(&pool
->lock
, *irq_flags
);
589 INIT_LIST_HEAD(&new_pages
);
590 r
= ttm_alloc_new_pages(&new_pages
, pool
->gfp_flags
, ttm_flags
,
592 spin_lock_irqsave(&pool
->lock
, *irq_flags
);
595 list_splice(&new_pages
, &pool
->list
);
597 pool
->npages
+= alloc_size
;
599 printk(KERN_ERR TTM_PFX
600 "Failed to fill pool (%p).", pool
);
601 /* If we have any pages left put them to the pool. */
602 list_for_each_entry(p
, &pool
->list
, lru
) {
605 list_splice(&new_pages
, &pool
->list
);
606 pool
->npages
+= cpages
;
610 pool
->fill_lock
= false;
614 * Cut count nubmer of pages from the pool and put them to return list
616 * @return count of pages still to allocate to fill the request.
618 static unsigned ttm_page_pool_get_pages(struct ttm_page_pool
*pool
,
619 struct list_head
*pages
, int ttm_flags
,
620 enum ttm_caching_state cstate
, unsigned count
)
622 unsigned long irq_flags
;
626 spin_lock_irqsave(&pool
->lock
, irq_flags
);
627 ttm_page_pool_fill_locked(pool
, ttm_flags
, cstate
, count
, &irq_flags
);
629 if (count
>= pool
->npages
) {
630 /* take all pages from the pool */
631 list_splice_init(&pool
->list
, pages
);
632 count
-= pool
->npages
;
636 /* find the last pages to include for requested number of pages. Split
637 * pool to begin and halves to reduce search space. */
638 if (count
<= pool
->npages
/2) {
640 list_for_each(p
, &pool
->list
) {
645 i
= pool
->npages
+ 1;
646 list_for_each_prev(p
, &pool
->list
) {
651 /* Cut count number of pages from pool */
652 list_cut_position(pages
, &pool
->list
, p
);
653 pool
->npages
-= count
;
656 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
661 * On success pages list will hold count number of correctly
664 int ttm_get_pages(struct list_head
*pages
, int flags
,
665 enum ttm_caching_state cstate
, unsigned count
)
667 struct ttm_page_pool
*pool
= ttm_get_pool(flags
, cstate
);
668 struct page
*p
= NULL
;
669 int gfp_flags
= GFP_USER
;
672 /* set zero flag for page allocation if required */
673 if (flags
& TTM_PAGE_FLAG_ZERO_ALLOC
)
674 gfp_flags
|= __GFP_ZERO
;
676 /* No pool for cached pages */
678 if (flags
& TTM_PAGE_FLAG_DMA32
)
679 gfp_flags
|= GFP_DMA32
;
681 gfp_flags
|= GFP_HIGHUSER
;
683 for (r
= 0; r
< count
; ++r
) {
684 p
= alloc_page(gfp_flags
);
687 printk(KERN_ERR TTM_PFX
688 "Unable to allocate page.");
692 list_add(&p
->lru
, pages
);
698 /* combine zero flag to pool flags */
699 gfp_flags
|= pool
->gfp_flags
;
701 /* First we take pages from the pool */
702 count
= ttm_page_pool_get_pages(pool
, pages
, flags
, cstate
, count
);
704 /* clear the pages coming from the pool if requested */
705 if (flags
& TTM_PAGE_FLAG_ZERO_ALLOC
) {
706 list_for_each_entry(p
, pages
, lru
) {
707 clear_page(page_address(p
));
711 /* If pool didn't have enough pages allocate new one. */
713 /* ttm_alloc_new_pages doesn't reference pool so we can run
714 * multiple requests in parallel.
716 r
= ttm_alloc_new_pages(pages
, gfp_flags
, flags
, cstate
, count
);
718 /* If there is any pages in the list put them back to
720 printk(KERN_ERR TTM_PFX
721 "Failed to allocate extra pages "
722 "for large request.");
723 ttm_put_pages(pages
, 0, flags
, cstate
);
732 /* Put all pages in pages list to correct pool to wait for reuse */
733 void ttm_put_pages(struct list_head
*pages
, unsigned page_count
, int flags
,
734 enum ttm_caching_state cstate
)
736 unsigned long irq_flags
;
737 struct ttm_page_pool
*pool
= ttm_get_pool(flags
, cstate
);
738 struct page
*p
, *tmp
;
741 /* No pool for this memory type so free the pages */
743 list_for_each_entry_safe(p
, tmp
, pages
, lru
) {
746 /* Make the pages list empty */
747 INIT_LIST_HEAD(pages
);
750 if (page_count
== 0) {
751 list_for_each_entry_safe(p
, tmp
, pages
, lru
) {
756 spin_lock_irqsave(&pool
->lock
, irq_flags
);
757 list_splice_init(pages
, &pool
->list
);
758 pool
->npages
+= page_count
;
759 /* Check that we don't go over the pool limit */
761 if (pool
->npages
> _manager
->options
.max_size
) {
762 page_count
= pool
->npages
- _manager
->options
.max_size
;
763 /* free at least NUM_PAGES_TO_ALLOC number of pages
764 * to reduce calls to set_memory_wb */
765 if (page_count
< NUM_PAGES_TO_ALLOC
)
766 page_count
= NUM_PAGES_TO_ALLOC
;
768 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
770 ttm_page_pool_free(pool
, page_count
);
773 static void ttm_page_pool_init_locked(struct ttm_page_pool
*pool
, int flags
,
776 spin_lock_init(&pool
->lock
);
777 pool
->fill_lock
= false;
778 INIT_LIST_HEAD(&pool
->list
);
779 pool
->npages
= pool
->nfrees
= 0;
780 pool
->gfp_flags
= flags
;
784 int ttm_page_alloc_init(struct ttm_mem_global
*glob
, unsigned max_pages
)
790 printk(KERN_INFO TTM_PFX
"Initializing pool allocator.\n");
792 _manager
= kzalloc(sizeof(*_manager
), GFP_KERNEL
);
794 ttm_page_pool_init_locked(&_manager
->wc_pool
, GFP_HIGHUSER
, "wc");
796 ttm_page_pool_init_locked(&_manager
->uc_pool
, GFP_HIGHUSER
, "uc");
798 ttm_page_pool_init_locked(&_manager
->wc_pool_dma32
,
799 GFP_USER
| GFP_DMA32
, "wc dma");
801 ttm_page_pool_init_locked(&_manager
->uc_pool_dma32
,
802 GFP_USER
| GFP_DMA32
, "uc dma");
804 _manager
->options
.max_size
= max_pages
;
805 _manager
->options
.small
= SMALL_ALLOCATION
;
806 _manager
->options
.alloc_size
= NUM_PAGES_TO_ALLOC
;
808 ret
= kobject_init_and_add(&_manager
->kobj
, &ttm_pool_kobj_type
,
809 &glob
->kobj
, "pool");
810 if (unlikely(ret
!= 0)) {
811 kobject_put(&_manager
->kobj
);
816 ttm_pool_mm_shrink_init(_manager
);
821 void ttm_page_alloc_fini()
825 printk(KERN_INFO TTM_PFX
"Finalizing pool allocator.\n");
826 ttm_pool_mm_shrink_fini(_manager
);
828 for (i
= 0; i
< NUM_POOLS
; ++i
)
829 ttm_page_pool_free(&_manager
->pools
[i
], FREE_ALL_PAGES
);
831 kobject_put(&_manager
->kobj
);
835 int ttm_page_alloc_debugfs(struct seq_file
*m
, void *data
)
837 struct ttm_page_pool
*p
;
839 char *h
[] = {"pool", "refills", "pages freed", "size"};
841 seq_printf(m
, "No pool allocator running.\n");
844 seq_printf(m
, "%6s %12s %13s %8s\n",
845 h
[0], h
[1], h
[2], h
[3]);
846 for (i
= 0; i
< NUM_POOLS
; ++i
) {
847 p
= &_manager
->pools
[i
];
849 seq_printf(m
, "%6s %12ld %13ld %8d\n",
850 p
->name
, p
->nrefills
,
851 p
->nfrees
, p
->npages
);
855 EXPORT_SYMBOL(ttm_page_alloc_debugfs
);