ttm_page_alloc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. * Copyright (c) Red Hat Inc.
  3. * Permission is hereby granted, free of charge, to any person obtaining a
  4. * copy of this software and associated documentation files (the "Software"),
  5. * to deal in the Software without restriction, including without limitation
  6. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  7. * and/or sell copies of the Software, and to permit persons to whom the
  8. * Software is furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice (including the
  11. * next paragraph) shall be included in all copies or substantial portions
  12. * of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie <airlied@redhat.com>
  23. * Jerome Glisse <jglisse@redhat.com>
  24. * Pauli Nieminen <suokkos@gmail.com>
  25. */
  26. /* simple list based uncached page pool
  27. * - Pool collects resently freed pages for reuse
  28. * - Use page->lru to keep a free list
  29. * - doesn't track currently in use pages
  30. */
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/highmem.h>
  34. #include <linux/mm_types.h>
  35. #include <linux/module.h>
  36. #include <linux/mm.h>
  37. #include <linux/seq_file.h> /* for seq_printf */
  38. #include <linux/slab.h>
  39. #include <linux/dma-mapping.h>
  40. #include <asm/atomic.h>
  41. #include "ttm/ttm_bo_driver.h"
  42. #include "ttm/ttm_page_alloc.h"
  43. #ifdef TTM_HAS_AGP
  44. #include <asm/agp.h>
  45. #endif
  46. #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
  47. #define SMALL_ALLOCATION 16
  48. #define FREE_ALL_PAGES (~0U)
  49. /* times are in msecs */
  50. #define PAGE_FREE_INTERVAL 1000
  51. /**
  52. * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
  53. *
  54. * @lock: Protects the shared pool from concurrnet access. Must be used with
  55. * irqsave/irqrestore variants because pool allocator maybe called from
  56. * delayed work.
  57. * @fill_lock: Prevent concurrent calls to fill.
  58. * @list: Pool of free uc/wc pages for fast reuse.
  59. * @gfp_flags: Flags to pass for alloc_page.
  60. * @npages: Number of pages in pool.
  61. */
  62. struct ttm_page_pool {
  63. spinlock_t lock;
  64. bool fill_lock;
  65. struct list_head list;
  66. gfp_t gfp_flags;
  67. unsigned npages;
  68. char *name;
  69. unsigned long nfrees;
  70. unsigned long nrefills;
  71. };
  72. /**
  73. * Limits for the pool. They are handled without locks because only place where
  74. * they may change is in sysfs store. They won't have immediate effect anyway
  75. * so forcing serialization to access them is pointless.
  76. */
  77. struct ttm_pool_opts {
  78. unsigned alloc_size;
  79. unsigned max_size;
  80. unsigned small;
  81. };
  82. #define NUM_POOLS 4
  83. /**
  84. * struct ttm_pool_manager - Holds memory pools for fst allocation
  85. *
  86. * Manager is read only object for pool code so it doesn't need locking.
  87. *
  88. * @free_interval: minimum number of jiffies between freeing pages from pool.
  89. * @page_alloc_inited: reference counting for pool allocation.
  90. * @work: Work that is used to shrink the pool. Work is only run when there is
  91. * some pages to free.
  92. * @small_allocation: Limit in number of pages what is small allocation.
  93. *
  94. * @pools: All pool objects in use.
  95. **/
  96. struct ttm_pool_manager {
  97. struct kobject kobj;
  98. struct shrinker mm_shrink;
  99. struct ttm_pool_opts options;
  100. union {
  101. struct ttm_page_pool pools[NUM_POOLS];
  102. struct {
  103. struct ttm_page_pool wc_pool;
  104. struct ttm_page_pool uc_pool;
  105. struct ttm_page_pool wc_pool_dma32;
  106. struct ttm_page_pool uc_pool_dma32;
  107. } ;
  108. };
  109. };
  110. static struct attribute ttm_page_pool_max = {
  111. .name = "pool_max_size",
  112. .mode = S_IRUGO | S_IWUSR
  113. };
  114. static struct attribute ttm_page_pool_small = {
  115. .name = "pool_small_allocation",
  116. .mode = S_IRUGO | S_IWUSR
  117. };
  118. static struct attribute ttm_page_pool_alloc_size = {
  119. .name = "pool_allocation_size",
  120. .mode = S_IRUGO | S_IWUSR
  121. };
  122. static struct attribute *ttm_pool_attrs[] = {
  123. &ttm_page_pool_max,
  124. &ttm_page_pool_small,
  125. &ttm_page_pool_alloc_size,
  126. NULL
  127. };
  128. static void ttm_pool_kobj_release(struct kobject *kobj)
  129. {
  130. struct ttm_pool_manager *m =
  131. container_of(kobj, struct ttm_pool_manager, kobj);
  132. kfree(m);
  133. }
  134. static ssize_t ttm_pool_store(struct kobject *kobj,
  135. struct attribute *attr, const char *buffer, size_t size)
  136. {
  137. struct ttm_pool_manager *m =
  138. container_of(kobj, struct ttm_pool_manager, kobj);
  139. int chars;
  140. unsigned val;
  141. chars = sscanf(buffer, "%u", &val);
  142. if (chars == 0)
  143. return size;
  144. /* Convert kb to number of pages */
  145. val = val / (PAGE_SIZE >> 10);
  146. if (attr == &ttm_page_pool_max)
  147. m->options.max_size = val;
  148. else if (attr == &ttm_page_pool_small)
  149. m->options.small = val;
  150. else if (attr == &ttm_page_pool_alloc_size) {
  151. if (val > NUM_PAGES_TO_ALLOC*8) {
  152. printk(KERN_ERR TTM_PFX
  153. "Setting allocation size to %lu "
  154. "is not allowed. Recommended size is "
  155. "%lu\n",
  156. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
  157. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  158. return size;
  159. } else if (val > NUM_PAGES_TO_ALLOC) {
  160. printk(KERN_WARNING TTM_PFX
  161. "Setting allocation size to "
  162. "larger than %lu is not recommended.\n",
  163. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  164. }
  165. m->options.alloc_size = val;
  166. }
  167. return size;
  168. }
  169. static ssize_t ttm_pool_show(struct kobject *kobj,
  170. struct attribute *attr, char *buffer)
  171. {
  172. struct ttm_pool_manager *m =
  173. container_of(kobj, struct ttm_pool_manager, kobj);
  174. unsigned val = 0;
  175. if (attr == &ttm_page_pool_max)
  176. val = m->options.max_size;
  177. else if (attr == &ttm_page_pool_small)
  178. val = m->options.small;
  179. else if (attr == &ttm_page_pool_alloc_size)
  180. val = m->options.alloc_size;
  181. val = val * (PAGE_SIZE >> 10);
  182. return snprintf(buffer, PAGE_SIZE, "%u\n", val);
  183. }
  184. static const struct sysfs_ops ttm_pool_sysfs_ops = {
  185. .show = &ttm_pool_show,
  186. .store = &ttm_pool_store,
  187. };
  188. static struct kobj_type ttm_pool_kobj_type = {
  189. .release = &ttm_pool_kobj_release,
  190. .sysfs_ops = &ttm_pool_sysfs_ops,
  191. .default_attrs = ttm_pool_attrs,
  192. };
  193. static struct ttm_pool_manager *_manager;
  194. #ifndef CONFIG_X86
  195. static int set_pages_array_wb(struct page **pages, int addrinarray)
  196. {
  197. #ifdef TTM_HAS_AGP
  198. int i;
  199. for (i = 0; i < addrinarray; i++)
  200. unmap_page_from_agp(pages[i]);
  201. #endif
  202. return 0;
  203. }
  204. static int set_pages_array_wc(struct page **pages, int addrinarray)
  205. {
  206. #ifdef TTM_HAS_AGP
  207. int i;
  208. for (i = 0; i < addrinarray; i++)
  209. map_page_into_agp(pages[i]);
  210. #endif
  211. return 0;
  212. }
  213. static int set_pages_array_uc(struct page **pages, int addrinarray)
  214. {
  215. #ifdef TTM_HAS_AGP
  216. int i;
  217. for (i = 0; i < addrinarray; i++)
  218. map_page_into_agp(pages[i]);
  219. #endif
  220. return 0;
  221. }
  222. #endif
  223. /**
  224. * Select the right pool or requested caching state and ttm flags. */
  225. static struct ttm_page_pool *ttm_get_pool(int flags,
  226. enum ttm_caching_state cstate)
  227. {
  228. int pool_index;
  229. if (cstate == tt_cached)
  230. return NULL;
  231. if (cstate == tt_wc)
  232. pool_index = 0x0;
  233. else
  234. pool_index = 0x1;
  235. if (flags & TTM_PAGE_FLAG_DMA32)
  236. pool_index |= 0x2;
  237. return &_manager->pools[pool_index];
  238. }
  239. /* set memory back to wb and free the pages. */
  240. static void ttm_pages_put(struct page *pages[], unsigned npages)
  241. {
  242. unsigned i;
  243. if (set_pages_array_wb(pages, npages))
  244. printk(KERN_ERR TTM_PFX "Failed to set %d pages to wb!\n",
  245. npages);
  246. for (i = 0; i < npages; ++i)
  247. __free_page(pages[i]);
  248. }
  249. static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
  250. unsigned freed_pages)
  251. {
  252. pool->npages -= freed_pages;
  253. pool->nfrees += freed_pages;
  254. }
  255. /**
  256. * Free pages from pool.
  257. *
  258. * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
  259. * number of pages in one go.
  260. *
  261. * @pool: to free the pages from
  262. * @free_all: If set to true will free all pages in pool
  263. **/
  264. static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free)
  265. {
  266. unsigned long irq_flags;
  267. struct page *p;
  268. struct page **pages_to_free;
  269. unsigned freed_pages = 0,
  270. npages_to_free = nr_free;
  271. if (NUM_PAGES_TO_ALLOC < nr_free)
  272. npages_to_free = NUM_PAGES_TO_ALLOC;
  273. pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
  274. GFP_KERNEL);
  275. if (!pages_to_free) {
  276. printk(KERN_ERR TTM_PFX
  277. "Failed to allocate memory for pool free operation.\n");
  278. return 0;
  279. }
  280. restart:
  281. spin_lock_irqsave(&pool->lock, irq_flags);
  282. list_for_each_entry_reverse(p, &pool->list, lru) {
  283. if (freed_pages >= npages_to_free)
  284. break;
  285. pages_to_free[freed_pages++] = p;
  286. /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
  287. if (freed_pages >= NUM_PAGES_TO_ALLOC) {
  288. /* remove range of pages from the pool */
  289. __list_del(p->lru.prev, &pool->list);
  290. ttm_pool_update_free_locked(pool, freed_pages);
  291. /**
  292. * Because changing page caching is costly
  293. * we unlock the pool to prevent stalling.
  294. */
  295. spin_unlock_irqrestore(&pool->lock, irq_flags);
  296. ttm_pages_put(pages_to_free, freed_pages);
  297. if (likely(nr_free != FREE_ALL_PAGES))
  298. nr_free -= freed_pages;
  299. if (NUM_PAGES_TO_ALLOC >= nr_free)
  300. npages_to_free = nr_free;
  301. else
  302. npages_to_free = NUM_PAGES_TO_ALLOC;
  303. freed_pages = 0;
  304. /* free all so restart the processing */
  305. if (nr_free)
  306. goto restart;
  307. /* Not allowed to fall tough or break because
  308. * following context is inside spinlock while we are
  309. * outside here.
  310. */
  311. goto out;
  312. }
  313. }
  314. /* remove range of pages from the pool */
  315. if (freed_pages) {
  316. __list_del(&p->lru, &pool->list);
  317. ttm_pool_update_free_locked(pool, freed_pages);
  318. nr_free -= freed_pages;
  319. }
  320. spin_unlock_irqrestore(&pool->lock, irq_flags);
  321. if (freed_pages)
  322. ttm_pages_put(pages_to_free, freed_pages);
  323. out:
  324. kfree(pages_to_free);
  325. return nr_free;
  326. }
  327. /* Get good estimation how many pages are free in pools */
  328. static int ttm_pool_get_num_unused_pages(void)
  329. {
  330. unsigned i;
  331. int total = 0;
  332. for (i = 0; i < NUM_POOLS; ++i)
  333. total += _manager->pools[i].npages;
  334. return total;
  335. }
  336. /**
  337. * Callback for mm to request pool to reduce number of page held.
  338. */
  339. static int ttm_pool_mm_shrink(struct shrinker *shrink, int shrink_pages, gfp_t gfp_mask)
  340. {
  341. static atomic_t start_pool = ATOMIC_INIT(0);
  342. unsigned i;
  343. unsigned pool_offset = atomic_add_return(1, &start_pool);
  344. struct ttm_page_pool *pool;
  345. pool_offset = pool_offset % NUM_POOLS;
  346. /* select start pool in round robin fashion */
  347. for (i = 0; i < NUM_POOLS; ++i) {
  348. unsigned nr_free = shrink_pages;
  349. if (shrink_pages == 0)
  350. break;
  351. pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
  352. shrink_pages = ttm_page_pool_free(pool, nr_free);
  353. }
  354. /* return estimated number of unused pages in pool */
  355. return ttm_pool_get_num_unused_pages();
  356. }
  357. static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  358. {
  359. manager->mm_shrink.shrink = &ttm_pool_mm_shrink;
  360. manager->mm_shrink.seeks = 1;
  361. register_shrinker(&manager->mm_shrink);
  362. }
  363. static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  364. {
  365. unregister_shrinker(&manager->mm_shrink);
  366. }
  367. static int ttm_set_pages_caching(struct page **pages,
  368. enum ttm_caching_state cstate, unsigned cpages)
  369. {
  370. int r = 0;
  371. /* Set page caching */
  372. switch (cstate) {
  373. case tt_uncached:
  374. r = set_pages_array_uc(pages, cpages);
  375. if (r)
  376. printk(KERN_ERR TTM_PFX
  377. "Failed to set %d pages to uc!\n",
  378. cpages);
  379. break;
  380. case tt_wc:
  381. r = set_pages_array_wc(pages, cpages);
  382. if (r)
  383. printk(KERN_ERR TTM_PFX
  384. "Failed to set %d pages to wc!\n",
  385. cpages);
  386. break;
  387. default:
  388. break;
  389. }
  390. return r;
  391. }
  392. /**
  393. * Free pages the pages that failed to change the caching state. If there is
  394. * any pages that have changed their caching state already put them to the
  395. * pool.
  396. */
  397. static void ttm_handle_caching_state_failure(struct list_head *pages,
  398. int ttm_flags, enum ttm_caching_state cstate,
  399. struct page **failed_pages, unsigned cpages)
  400. {
  401. unsigned i;
  402. /* Failed pages have to be freed */
  403. for (i = 0; i < cpages; ++i) {
  404. list_del(&failed_pages[i]->lru);
  405. __free_page(failed_pages[i]);
  406. }
  407. }
  408. /**
  409. * Allocate new pages with correct caching.
  410. *
  411. * This function is reentrant if caller updates count depending on number of
  412. * pages returned in pages array.
  413. */
  414. static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
  415. int ttm_flags, enum ttm_caching_state cstate, unsigned count)
  416. {
  417. struct page **caching_array;
  418. struct page *p;
  419. int r = 0;
  420. unsigned i, cpages;
  421. unsigned max_cpages = min(count,
  422. (unsigned)(PAGE_SIZE/sizeof(struct page *)));
  423. /* allocate array for page caching change */
  424. caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
  425. if (!caching_array) {
  426. printk(KERN_ERR TTM_PFX
  427. "Unable to allocate table for new pages.");
  428. return -ENOMEM;
  429. }
  430. for (i = 0, cpages = 0; i < count; ++i) {
  431. p = alloc_page(gfp_flags);
  432. if (!p) {
  433. printk(KERN_ERR TTM_PFX "Unable to get page %u.\n", i);
  434. /* store already allocated pages in the pool after
  435. * setting the caching state */
  436. if (cpages) {
  437. r = ttm_set_pages_caching(caching_array,
  438. cstate, cpages);
  439. if (r)
  440. ttm_handle_caching_state_failure(pages,
  441. ttm_flags, cstate,
  442. caching_array, cpages);
  443. }
  444. r = -ENOMEM;
  445. goto out;
  446. }
  447. #ifdef CONFIG_HIGHMEM
  448. /* gfp flags of highmem page should never be dma32 so we
  449. * we should be fine in such case
  450. */
  451. if (!PageHighMem(p))
  452. #endif
  453. {
  454. caching_array[cpages++] = p;
  455. if (cpages == max_cpages) {
  456. r = ttm_set_pages_caching(caching_array,
  457. cstate, cpages);
  458. if (r) {
  459. ttm_handle_caching_state_failure(pages,
  460. ttm_flags, cstate,
  461. caching_array, cpages);
  462. goto out;
  463. }
  464. cpages = 0;
  465. }
  466. }
  467. list_add(&p->lru, pages);
  468. }
  469. if (cpages) {
  470. r = ttm_set_pages_caching(caching_array, cstate, cpages);
  471. if (r)
  472. ttm_handle_caching_state_failure(pages,
  473. ttm_flags, cstate,
  474. caching_array, cpages);
  475. }
  476. out:
  477. kfree(caching_array);
  478. return r;
  479. }
  480. /**
  481. * Fill the given pool if there isn't enough pages and requested number of
  482. * pages is small.
  483. */
  484. static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
  485. int ttm_flags, enum ttm_caching_state cstate, unsigned count,
  486. unsigned long *irq_flags)
  487. {
  488. struct page *p;
  489. int r;
  490. unsigned cpages = 0;
  491. /**
  492. * Only allow one pool fill operation at a time.
  493. * If pool doesn't have enough pages for the allocation new pages are
  494. * allocated from outside of pool.
  495. */
  496. if (pool->fill_lock)
  497. return;
  498. pool->fill_lock = true;
  499. /* If allocation request is small and there is not enough
  500. * pages in pool we fill the pool first */
  501. if (count < _manager->options.small
  502. && count > pool->npages) {
  503. struct list_head new_pages;
  504. unsigned alloc_size = _manager->options.alloc_size;
  505. /**
  506. * Can't change page caching if in irqsave context. We have to
  507. * drop the pool->lock.
  508. */
  509. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  510. INIT_LIST_HEAD(&new_pages);
  511. r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
  512. cstate, alloc_size);
  513. spin_lock_irqsave(&pool->lock, *irq_flags);
  514. if (!r) {
  515. list_splice(&new_pages, &pool->list);
  516. ++pool->nrefills;
  517. pool->npages += alloc_size;
  518. } else {
  519. printk(KERN_ERR TTM_PFX
  520. "Failed to fill pool (%p).", pool);
  521. /* If we have any pages left put them to the pool. */
  522. list_for_each_entry(p, &pool->list, lru) {
  523. ++cpages;
  524. }
  525. list_splice(&new_pages, &pool->list);
  526. pool->npages += cpages;
  527. }
  528. }
  529. pool->fill_lock = false;
  530. }
  531. /**
  532. * Cut count nubmer of pages from the pool and put them to return list
  533. *
  534. * @return count of pages still to allocate to fill the request.
  535. */
  536. static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
  537. struct list_head *pages, int ttm_flags,
  538. enum ttm_caching_state cstate, unsigned count)
  539. {
  540. unsigned long irq_flags;
  541. struct list_head *p;
  542. unsigned i;
  543. spin_lock_irqsave(&pool->lock, irq_flags);
  544. ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count, &irq_flags);
  545. if (count >= pool->npages) {
  546. /* take all pages from the pool */
  547. list_splice_init(&pool->list, pages);
  548. count -= pool->npages;
  549. pool->npages = 0;
  550. goto out;
  551. }
  552. /* find the last pages to include for requested number of pages. Split
  553. * pool to begin and halves to reduce search space. */
  554. if (count <= pool->npages/2) {
  555. i = 0;
  556. list_for_each(p, &pool->list) {
  557. if (++i == count)
  558. break;
  559. }
  560. } else {
  561. i = pool->npages + 1;
  562. list_for_each_prev(p, &pool->list) {
  563. if (--i == count)
  564. break;
  565. }
  566. }
  567. /* Cut count number of pages from pool */
  568. list_cut_position(pages, &pool->list, p);
  569. pool->npages -= count;
  570. count = 0;
  571. out:
  572. spin_unlock_irqrestore(&pool->lock, irq_flags);
  573. return count;
  574. }
  575. /*
  576. * On success pages list will hold count number of correctly
  577. * cached pages.
  578. */
  579. int ttm_get_pages(struct list_head *pages, int flags,
  580. enum ttm_caching_state cstate, unsigned count,
  581. dma_addr_t *dma_address)
  582. {
  583. struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
  584. struct page *p = NULL;
  585. gfp_t gfp_flags = GFP_USER;
  586. int r;
  587. /* set zero flag for page allocation if required */
  588. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  589. gfp_flags |= __GFP_ZERO;
  590. /* No pool for cached pages */
  591. if (pool == NULL) {
  592. if (flags & TTM_PAGE_FLAG_DMA32)
  593. gfp_flags |= GFP_DMA32;
  594. else
  595. gfp_flags |= GFP_HIGHUSER;
  596. for (r = 0; r < count; ++r) {
  597. if ((flags & TTM_PAGE_FLAG_DMA32) && dma_address) {
  598. void *addr;
  599. addr = dma_alloc_coherent(NULL, PAGE_SIZE,
  600. &dma_address[r],
  601. gfp_flags);
  602. if (addr == NULL)
  603. return -ENOMEM;
  604. p = virt_to_page(addr);
  605. } else
  606. p = alloc_page(gfp_flags);
  607. if (!p) {
  608. printk(KERN_ERR TTM_PFX
  609. "Unable to allocate page.");
  610. return -ENOMEM;
  611. }
  612. list_add(&p->lru, pages);
  613. }
  614. return 0;
  615. }
  616. /* combine zero flag to pool flags */
  617. gfp_flags |= pool->gfp_flags;
  618. /* First we take pages from the pool */
  619. count = ttm_page_pool_get_pages(pool, pages, flags, cstate, count);
  620. /* clear the pages coming from the pool if requested */
  621. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
  622. list_for_each_entry(p, pages, lru) {
  623. clear_page(page_address(p));
  624. }
  625. }
  626. /* If pool didn't have enough pages allocate new one. */
  627. if (count > 0) {
  628. /* ttm_alloc_new_pages doesn't reference pool so we can run
  629. * multiple requests in parallel.
  630. **/
  631. r = ttm_alloc_new_pages(pages, gfp_flags, flags, cstate, count);
  632. if (r) {
  633. /* If there is any pages in the list put them back to
  634. * the pool. */
  635. printk(KERN_ERR TTM_PFX
  636. "Failed to allocate extra pages "
  637. "for large request.");
  638. ttm_put_pages(pages, 0, flags, cstate, NULL);
  639. return r;
  640. }
  641. }
  642. return 0;
  643. }
  644. /* Put all pages in pages list to correct pool to wait for reuse */
  645. void ttm_put_pages(struct list_head *pages, unsigned page_count, int flags,
  646. enum ttm_caching_state cstate, dma_addr_t *dma_address)
  647. {
  648. unsigned long irq_flags;
  649. struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
  650. struct page *p, *tmp;
  651. unsigned r;
  652. if (pool == NULL) {
  653. /* No pool for this memory type so free the pages */
  654. r = page_count-1;
  655. list_for_each_entry_safe(p, tmp, pages, lru) {
  656. if ((flags & TTM_PAGE_FLAG_DMA32) && dma_address) {
  657. void *addr = page_address(p);
  658. WARN_ON(!addr || !dma_address[r]);
  659. if (addr)
  660. dma_free_coherent(NULL, PAGE_SIZE,
  661. addr,
  662. dma_address[r]);
  663. dma_address[r] = 0;
  664. } else
  665. __free_page(p);
  666. r--;
  667. }
  668. /* Make the pages list empty */
  669. INIT_LIST_HEAD(pages);
  670. return;
  671. }
  672. if (page_count == 0) {
  673. list_for_each_entry_safe(p, tmp, pages, lru) {
  674. ++page_count;
  675. }
  676. }
  677. spin_lock_irqsave(&pool->lock, irq_flags);
  678. list_splice_init(pages, &pool->list);
  679. pool->npages += page_count;
  680. /* Check that we don't go over the pool limit */
  681. page_count = 0;
  682. if (pool->npages > _manager->options.max_size) {
  683. page_count = pool->npages - _manager->options.max_size;
  684. /* free at least NUM_PAGES_TO_ALLOC number of pages
  685. * to reduce calls to set_memory_wb */
  686. if (page_count < NUM_PAGES_TO_ALLOC)
  687. page_count = NUM_PAGES_TO_ALLOC;
  688. }
  689. spin_unlock_irqrestore(&pool->lock, irq_flags);
  690. if (page_count)
  691. ttm_page_pool_free(pool, page_count);
  692. }
  693. static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
  694. char *name)
  695. {
  696. spin_lock_init(&pool->lock);
  697. pool->fill_lock = false;
  698. INIT_LIST_HEAD(&pool->list);
  699. pool->npages = pool->nfrees = 0;
  700. pool->gfp_flags = flags;
  701. pool->name = name;
  702. }
  703. int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  704. {
  705. int ret;
  706. WARN_ON(_manager);
  707. printk(KERN_INFO TTM_PFX "Initializing pool allocator.\n");
  708. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  709. ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc");
  710. ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc");
  711. ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
  712. GFP_USER | GFP_DMA32, "wc dma");
  713. ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
  714. GFP_USER | GFP_DMA32, "uc dma");
  715. _manager->options.max_size = max_pages;
  716. _manager->options.small = SMALL_ALLOCATION;
  717. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  718. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  719. &glob->kobj, "pool");
  720. if (unlikely(ret != 0)) {
  721. kobject_put(&_manager->kobj);
  722. _manager = NULL;
  723. return ret;
  724. }
  725. ttm_pool_mm_shrink_init(_manager);
  726. return 0;
  727. }
  728. void ttm_page_alloc_fini(void)
  729. {
  730. int i;
  731. printk(KERN_INFO TTM_PFX "Finalizing pool allocator.\n");
  732. ttm_pool_mm_shrink_fini(_manager);
  733. for (i = 0; i < NUM_POOLS; ++i)
  734. ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES);
  735. kobject_put(&_manager->kobj);
  736. _manager = NULL;
  737. }
  738. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  739. {
  740. struct ttm_page_pool *p;
  741. unsigned i;
  742. char *h[] = {"pool", "refills", "pages freed", "size"};
  743. if (!_manager) {
  744. seq_printf(m, "No pool allocator running.\n");
  745. return 0;
  746. }
  747. seq_printf(m, "%6s %12s %13s %8s\n",
  748. h[0], h[1], h[2], h[3]);
  749. for (i = 0; i < NUM_POOLS; ++i) {
  750. p = &_manager->pools[i];
  751. seq_printf(m, "%6s %12ld %13ld %8d\n",
  752. p->name, p->nrefills,
  753. p->nfrees, p->npages);
  754. }
  755. return 0;
  756. }
  757. EXPORT_SYMBOL(ttm_page_alloc_debugfs);