ttm_page_alloc.c 21 KB

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