ttm_page_alloc.c 21 KB

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