ttm_page_alloc.c 21 KB

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