/* Pooling of allocated pages is necessary because changing the caching *attributesonx86ofthelinearmappingrequiresacostlycrossCPUTLB *invalidateforthoseaddresses. * *AdditionaltothatallocationsfromtheDMAcoherentAPIarepooledaswell *causetheyareratherslowcomparedtoalloc_pages+map.
*/
/* Allocate pages of size 1 << order with the given gfp_flags */ staticstruct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, unsignedint order)
{ unsignedlong attr = DMA_ATTR_FORCE_CONTIGUOUS; struct ttm_pool_dma *dma; struct page *p; void *vaddr;
/* Don't set the __GFP_COMP flag for higher order allocations. *Mappingpagesdirectlyintoanuserspaceprocessandcalling *put_page()onaTTMallocatedpageisillegal.
*/ if (order)
gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
__GFP_THISNODE;
if (!pool->use_dma_alloc) {
p = alloc_pages_node(pool->nid, gfp_flags, order); if (p)
p->private = order; return p;
}
dma = kmalloc(sizeof(*dma), GFP_KERNEL); if (!dma) return NULL;
/* TODO: This is an illegal abuse of the DMA API, but we need to rework *TTMpagefaulthandlingandextendtheDMAAPItocleanthisup.
*/ if (is_vmalloc_addr(vaddr))
p = vmalloc_to_page(vaddr); else
p = virt_to_page(vaddr);
/* Reset the caching and pages of size 1 << order */ staticvoid ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching, unsignedint order, struct page *p)
{ unsignedlong attr = DMA_ATTR_FORCE_CONTIGUOUS; struct ttm_pool_dma *dma; void *vaddr;
#ifdef CONFIG_X86 /* We don't care that set_pages_wb is inefficient here. This is only *usedwhenwehavetoshrinkandCPUoverheadisirrelevantthen.
*/ if (caching != ttm_cached && !PageHighMem(p))
set_pages_wb(p, 1 << order); #endif
if (!pool || !pool->use_dma_alloc) {
__free_pages(p, order); return;
}
/* Give pages into a specific pool_type */ staticvoid ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
{ unsignedint i, num_pages = 1 << pt->order;
for (i = 0; i < num_pages; ++i) { if (PageHighMem(p))
clear_highpage(p + i); else
clear_page(page_address(p + i));
}
/* Take pages from a specific pool_type, return NULL when nothing available */ staticstruct page *ttm_pool_type_take(struct ttm_pool_type *pt)
{ struct page *p;
spin_lock(&pt->lock);
p = list_first_entry_or_null(&pt->pages, typeof(*p), lru); if (p) {
atomic_long_sub(1 << pt->order, &allocated_pages);
list_del(&p->lru);
}
spin_unlock(&pt->lock);
return p;
}
/* Initialize and add a pool type to the global shrinker list */ staticvoid ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool, enum ttm_caching caching, unsignedint order)
{
pt->pool = pool;
pt->caching = caching;
pt->order = order;
spin_lock_init(&pt->lock);
INIT_LIST_HEAD(&pt->pages);
while ((p = ttm_pool_type_take(pt)))
ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
}
/* Return the pool_type to use for the given caching and order */ staticstruct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool, enum ttm_caching caching, unsignedint order)
{ if (pool->use_dma_alloc) return &pool->caching[caching].orders[order];
#ifdef CONFIG_X86 switch (caching) { case ttm_write_combined: if (pool->nid != NUMA_NO_NODE) return &pool->caching[caching].orders[order];
if (pool->use_dma32) return &global_dma32_write_combined[order];
return &global_write_combined[order]; case ttm_uncached: if (pool->nid != NUMA_NO_NODE) return &pool->caching[caching].orders[order];
if (pool->use_dma32) return &global_dma32_uncached[order];
/* Free pages using the global shrinker list */ staticunsignedint ttm_pool_shrink(void)
{ struct ttm_pool_type *pt; unsignedint num_pages; struct page *p;
/* Return the allocation order based for a page */ staticunsignedint ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
{ if (pool->use_dma_alloc) { struct ttm_pool_dma *dma = (void *)p->private;
{
pgoff_t i, nr = 1UL << restore->order; struct page **first_page = alloc->pages; struct page *p; int ret = 0;
for (i = restore->restored_pages; i < nr; ++i) {
p = first_page[i]; if (ttm_backup_page_ptr_is_handle(p)) { unsignedlong handle = ttm_backup_page_ptr_to_handle(p);
if (IS_ENABLED(CONFIG_FAULT_INJECTION) && ctx->interruptible &&
should_fail(&backup_fault_inject, 1)) {
ret = -EINTR; break;
}
if (handle == 0) {
restore->restored_pages++; continue;
}
ret = ttm_backup_copy_page(backup, restore->alloced_page + i,
handle, ctx->interruptible); if (ret) break;
if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
gfp_flags |= __GFP_ZERO;
if (ctx->gfp_retry_mayfail)
gfp_flags |= __GFP_RETRY_MAYFAIL;
if (pool->use_dma32)
gfp_flags |= GFP_DMA32; else
gfp_flags |= GFP_HIGHUSER;
page_caching = tt->caching;
allow_pools = true; for (order = ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc);
alloc->remaining_pages;
order = ttm_pool_alloc_find_order(order, alloc)) { struct ttm_pool_type *pt;
/* First, try to allocate a page from a pool if one exists. */
p = NULL;
pt = ttm_pool_select_type(pool, page_caching, order); if (pt && allow_pools)
p = ttm_pool_type_take(pt); /* *Ifthatfailsorpreviouslyfailed,allocatefromsystem. *Notethatthisalsodisallowsadditionalpoolallocationsusing *write-backcachedpoolsofthesameorder.Considerremoving *thatbehaviour.
*/ if (!p) {
page_caching = ttm_cached;
allow_pools = false;
p = ttm_pool_alloc_page(pool, gfp_flags, order);
} /* If that fails, lower the order if possible and retry. */ if (!p) { if (order) {
--order;
page_caching = tt->caching;
allow_pools = true; continue;
}
r = -ENOMEM; goto error_free_all;
}
r = ttm_pool_page_allocated(pool, order, p, page_caching, alloc,
restore); if (r) goto error_free_page;
if (ttm_pool_restore_valid(restore)) {
r = ttm_pool_restore_commit(restore, tt->backup, ctx, alloc); if (r) goto error_free_all;
}
}
r = ttm_pool_apply_caching(alloc); if (r) goto error_free_all;
if (WARN_ON(ttm_tt_is_backed_up(tt))) return -EINVAL;
if ((!ttm_backup_bytes_avail() && !flags->purge) ||
pool->use_dma_alloc || ttm_tt_is_backed_up(tt)) return -EBUSY;
#ifdef CONFIG_X86 /* Anything returned to the system needs to be cached. */ if (tt->caching != ttm_cached)
set_pages_array_wb(tt->pages, tt->num_pages); #endif
if (tt->dma_address || flags->purge) { for (i = 0; i < tt->num_pages; i += num_pages) { unsignedint order;
/* Pretend doing fault injection by shrinking only half of the pages. */ if (IS_ENABLED(CONFIG_FAULT_INJECTION) && should_fail(&backup_fault_inject, 1))
num_pages = DIV_ROUND_UP(num_pages, 2);
for (i = 0; i < num_pages; ++i) {
s64 shandle;
page = tt->pages[i]; if (unlikely(!page)) continue;
ttm_pool_split_for_swap(pool, page);
shandle = ttm_backup_backup_page(backup, page, flags->writeback, i,
gfp, alloc_gfp); if (shandle < 0) { /* We allow partially shrunken tts */
ret = shandle; break;
}
handle = shandle;
tt->pages[i] = ttm_backup_handle_to_page_ptr(handle);
put_page(page);
shrunken++;
}
for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) { for (j = 0; j < NR_PAGE_ORDERS; ++j) { struct ttm_pool_type *pt;
pt = ttm_pool_select_type(pool, i, j); if (pt != &pool->caching[i].orders[j]) continue;
ttm_pool_type_fini(pt);
}
}
/* We removed the pool types from the LRU, but we need to also make sure *thatnoshrinkerisconcurrentlyfreeingpagesfromthepool.
*/
ttm_pool_synchronize_shrinkers();
}
EXPORT_SYMBOL(ttm_pool_fini);
/* Free average pool number of pages. */ #define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)
do
num_freed += ttm_pool_shrink(); while (num_freed < sc->nr_to_scan &&
atomic_long_read(&allocated_pages));
sc->nr_scanned = num_freed;
return num_freed ?: SHRINK_STOP;
}
/* Return the number of pages available or SHRINK_EMPTY if we have none */ staticunsignedlong ttm_pool_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
{ unsignedlong num_pages = atomic_long_read(&allocated_pages);
return num_pages ? num_pages : SHRINK_EMPTY;
}
#ifdef CONFIG_DEBUG_FS /* Count the number of pages available in a pool_type */ staticunsignedint ttm_pool_type_count(struct ttm_pool_type *pt)
{ unsignedint count = 0; struct page *p;
spin_lock(&pt->lock); /* Only used for debugfs, the overhead doesn't matter */
list_for_each_entry(p, &pt->pages, lru)
++count;
spin_unlock(&pt->lock);
return count;
}
/* Print a nice header for the order */ staticvoid ttm_pool_debugfs_header(struct seq_file *m)
{ unsignedint i;
seq_puts(m, "\t "); for (i = 0; i < NR_PAGE_ORDERS; ++i)
seq_printf(m, " ---%2u---", i);
seq_puts(m, "\n");
}
/* Dump information about the different pool types */ staticvoid ttm_pool_debugfs_orders(struct ttm_pool_type *pt, struct seq_file *m)
{ unsignedint i;
for (i = 0; i < NR_PAGE_ORDERS; ++i)
seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
seq_puts(m, "\n");
}
/* Dump the total amount of allocated pages */ staticvoid ttm_pool_debugfs_footer(struct seq_file *m)
{
seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
atomic_long_read(&allocated_pages), page_pool_size);
}
/* Dump the information for the global pools */ staticint ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
{
ttm_pool_debugfs_header(m);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.