ttm_page_alloc.c 21 KB

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