ttm_page_alloc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. #define pr_fmt(fmt) "[TTM] " fmt
  32. #include <linux/list.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/highmem.h>
  35. #include <linux/mm_types.h>
  36. #include <linux/module.h>
  37. #include <linux/mm.h>
  38. #include <linux/seq_file.h> /* for seq_printf */
  39. #include <linux/slab.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/atomic.h>
  42. #include <drm/ttm/ttm_bo_driver.h>
  43. #include <drm/ttm/ttm_page_alloc.h>
  44. #ifdef TTM_HAS_AGP
  45. #include <asm/agp.h>
  46. #endif
  47. #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
  48. #define SMALL_ALLOCATION 16
  49. #define FREE_ALL_PAGES (~0U)
  50. /* times are in msecs */
  51. #define PAGE_FREE_INTERVAL 1000
  52. /**
  53. * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
  54. *
  55. * @lock: Protects the shared pool from concurrnet access. Must be used with
  56. * irqsave/irqrestore variants because pool allocator maybe called from
  57. * delayed work.
  58. * @fill_lock: Prevent concurrent calls to fill.
  59. * @list: Pool of free uc/wc pages for fast reuse.
  60. * @gfp_flags: Flags to pass for alloc_page.
  61. * @npages: Number of pages in pool.
  62. */
  63. struct ttm_page_pool {
  64. spinlock_t lock;
  65. bool fill_lock;
  66. struct list_head list;
  67. gfp_t gfp_flags;
  68. unsigned npages;
  69. char *name;
  70. unsigned long nfrees;
  71. unsigned long nrefills;
  72. };
  73. /**
  74. * Limits for the pool. They are handled without locks because only place where
  75. * they may change is in sysfs store. They won't have immediate effect anyway
  76. * so forcing serialization to access them is pointless.
  77. */
  78. struct ttm_pool_opts {
  79. unsigned alloc_size;
  80. unsigned max_size;
  81. unsigned small;
  82. };
  83. #define NUM_POOLS 4
  84. /**
  85. * struct ttm_pool_manager - Holds memory pools for fst allocation
  86. *
  87. * Manager is read only object for pool code so it doesn't need locking.
  88. *
  89. * @free_interval: minimum number of jiffies between freeing pages from pool.
  90. * @page_alloc_inited: reference counting for pool allocation.
  91. * @work: Work that is used to shrink the pool. Work is only run when there is
  92. * some pages to free.
  93. * @small_allocation: Limit in number of pages what is small allocation.
  94. *
  95. * @pools: All pool objects in use.
  96. **/
  97. struct ttm_pool_manager {
  98. struct kobject kobj;
  99. struct shrinker mm_shrink;
  100. struct ttm_pool_opts options;
  101. union {
  102. struct ttm_page_pool pools[NUM_POOLS];
  103. struct {
  104. struct ttm_page_pool wc_pool;
  105. struct ttm_page_pool uc_pool;
  106. struct ttm_page_pool wc_pool_dma32;
  107. struct ttm_page_pool uc_pool_dma32;
  108. } ;
  109. };
  110. };
  111. static struct attribute ttm_page_pool_max = {
  112. .name = "pool_max_size",
  113. .mode = S_IRUGO | S_IWUSR
  114. };
  115. static struct attribute ttm_page_pool_small = {
  116. .name = "pool_small_allocation",
  117. .mode = S_IRUGO | S_IWUSR
  118. };
  119. static struct attribute ttm_page_pool_alloc_size = {
  120. .name = "pool_allocation_size",
  121. .mode = S_IRUGO | S_IWUSR
  122. };
  123. static struct attribute *ttm_pool_attrs[] = {
  124. &ttm_page_pool_max,
  125. &ttm_page_pool_small,
  126. &ttm_page_pool_alloc_size,
  127. NULL
  128. };
  129. static void ttm_pool_kobj_release(struct kobject *kobj)
  130. {
  131. struct ttm_pool_manager *m =
  132. container_of(kobj, struct ttm_pool_manager, kobj);
  133. kfree(m);
  134. }
  135. static ssize_t ttm_pool_store(struct kobject *kobj,
  136. struct attribute *attr, const char *buffer, size_t size)
  137. {
  138. struct ttm_pool_manager *m =
  139. container_of(kobj, struct ttm_pool_manager, kobj);
  140. int chars;
  141. unsigned val;
  142. chars = sscanf(buffer, "%u", &val);
  143. if (chars == 0)
  144. return size;
  145. /* Convert kb to number of pages */
  146. val = val / (PAGE_SIZE >> 10);
  147. if (attr == &ttm_page_pool_max)
  148. m->options.max_size = val;
  149. else if (attr == &ttm_page_pool_small)
  150. m->options.small = val;
  151. else if (attr == &ttm_page_pool_alloc_size) {
  152. if (val > NUM_PAGES_TO_ALLOC*8) {
  153. pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
  154. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
  155. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  156. return size;
  157. } else if (val > NUM_PAGES_TO_ALLOC) {
  158. pr_warn("Setting allocation size to larger than %lu is not recommended\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. #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. pr_err("Failed to set %d pages to wb!\n", npages);
  241. for (i = 0; i < npages; ++i)
  242. __free_page(pages[i]);
  243. }
  244. static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
  245. unsigned freed_pages)
  246. {
  247. pool->npages -= freed_pages;
  248. pool->nfrees += freed_pages;
  249. }
  250. /**
  251. * Free pages from pool.
  252. *
  253. * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
  254. * number of pages in one go.
  255. *
  256. * @pool: to free the pages from
  257. * @free_all: If set to true will free all pages in pool
  258. * @use_static: Safe to use static buffer
  259. **/
  260. static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
  261. bool use_static)
  262. {
  263. static struct page *static_buf[NUM_PAGES_TO_ALLOC];
  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. if (use_static)
  272. pages_to_free = static_buf;
  273. else
  274. pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
  275. GFP_KERNEL);
  276. if (!pages_to_free) {
  277. pr_err("Failed to allocate memory for pool free operation\n");
  278. return 0;
  279. }
  280. restart:
  281. spin_lock_irqsave(&pool->lock, irq_flags);
  282. list_for_each_entry_reverse(p, &pool->list, lru) {
  283. if (freed_pages >= npages_to_free)
  284. break;
  285. pages_to_free[freed_pages++] = p;
  286. /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
  287. if (freed_pages >= NUM_PAGES_TO_ALLOC) {
  288. /* remove range of pages from the pool */
  289. __list_del(p->lru.prev, &pool->list);
  290. ttm_pool_update_free_locked(pool, freed_pages);
  291. /**
  292. * Because changing page caching is costly
  293. * we unlock the pool to prevent stalling.
  294. */
  295. spin_unlock_irqrestore(&pool->lock, irq_flags);
  296. ttm_pages_put(pages_to_free, freed_pages);
  297. if (likely(nr_free != FREE_ALL_PAGES))
  298. nr_free -= freed_pages;
  299. if (NUM_PAGES_TO_ALLOC >= nr_free)
  300. npages_to_free = nr_free;
  301. else
  302. npages_to_free = NUM_PAGES_TO_ALLOC;
  303. freed_pages = 0;
  304. /* free all so restart the processing */
  305. if (nr_free)
  306. goto restart;
  307. /* Not allowed to fall through or break because
  308. * following context is inside spinlock while we are
  309. * outside here.
  310. */
  311. goto out;
  312. }
  313. }
  314. /* remove range of pages from the pool */
  315. if (freed_pages) {
  316. __list_del(&p->lru, &pool->list);
  317. ttm_pool_update_free_locked(pool, freed_pages);
  318. nr_free -= freed_pages;
  319. }
  320. spin_unlock_irqrestore(&pool->lock, irq_flags);
  321. if (freed_pages)
  322. ttm_pages_put(pages_to_free, freed_pages);
  323. out:
  324. if (pages_to_free != static_buf)
  325. kfree(pages_to_free);
  326. return nr_free;
  327. }
  328. /**
  329. * Callback for mm to request pool to reduce number of page held.
  330. *
  331. * XXX: (dchinner) Deadlock warning!
  332. *
  333. * This code is crying out for a shrinker per pool....
  334. */
  335. static unsigned long
  336. ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  337. {
  338. static DEFINE_MUTEX(lock);
  339. static unsigned start_pool;
  340. unsigned i;
  341. unsigned pool_offset;
  342. struct ttm_page_pool *pool;
  343. int shrink_pages = sc->nr_to_scan;
  344. unsigned long freed = 0;
  345. if (!mutex_trylock(&lock))
  346. return SHRINK_STOP;
  347. pool_offset = ++start_pool % NUM_POOLS;
  348. /* select start pool in round robin fashion */
  349. for (i = 0; i < NUM_POOLS; ++i) {
  350. unsigned nr_free = shrink_pages;
  351. if (shrink_pages == 0)
  352. break;
  353. pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
  354. /* OK to use static buffer since global mutex is held. */
  355. shrink_pages = ttm_page_pool_free(pool, nr_free, true);
  356. freed += nr_free - shrink_pages;
  357. }
  358. mutex_unlock(&lock);
  359. return freed;
  360. }
  361. static unsigned long
  362. ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  363. {
  364. unsigned i;
  365. unsigned long count = 0;
  366. for (i = 0; i < NUM_POOLS; ++i)
  367. count += _manager->pools[i].npages;
  368. return count;
  369. }
  370. static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  371. {
  372. manager->mm_shrink.count_objects = ttm_pool_shrink_count;
  373. manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
  374. manager->mm_shrink.seeks = 1;
  375. register_shrinker(&manager->mm_shrink);
  376. }
  377. static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  378. {
  379. unregister_shrinker(&manager->mm_shrink);
  380. }
  381. static int ttm_set_pages_caching(struct page **pages,
  382. enum ttm_caching_state cstate, unsigned cpages)
  383. {
  384. int r = 0;
  385. /* Set page caching */
  386. switch (cstate) {
  387. case tt_uncached:
  388. r = set_pages_array_uc(pages, cpages);
  389. if (r)
  390. pr_err("Failed to set %d pages to uc!\n", cpages);
  391. break;
  392. case tt_wc:
  393. r = set_pages_array_wc(pages, cpages);
  394. if (r)
  395. pr_err("Failed to set %d pages to wc!\n", cpages);
  396. break;
  397. default:
  398. break;
  399. }
  400. return r;
  401. }
  402. /**
  403. * Free pages the pages that failed to change the caching state. If there is
  404. * any pages that have changed their caching state already put them to the
  405. * pool.
  406. */
  407. static void ttm_handle_caching_state_failure(struct list_head *pages,
  408. int ttm_flags, enum ttm_caching_state cstate,
  409. struct page **failed_pages, unsigned cpages)
  410. {
  411. unsigned i;
  412. /* Failed pages have to be freed */
  413. for (i = 0; i < cpages; ++i) {
  414. list_del(&failed_pages[i]->lru);
  415. __free_page(failed_pages[i]);
  416. }
  417. }
  418. /**
  419. * Allocate new pages with correct caching.
  420. *
  421. * This function is reentrant if caller updates count depending on number of
  422. * pages returned in pages array.
  423. */
  424. static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
  425. int ttm_flags, enum ttm_caching_state cstate, unsigned count)
  426. {
  427. struct page **caching_array;
  428. struct page *p;
  429. int r = 0;
  430. unsigned i, cpages;
  431. unsigned max_cpages = min(count,
  432. (unsigned)(PAGE_SIZE/sizeof(struct page *)));
  433. /* allocate array for page caching change */
  434. caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
  435. if (!caching_array) {
  436. pr_err("Unable to allocate table for new pages\n");
  437. return -ENOMEM;
  438. }
  439. for (i = 0, cpages = 0; i < count; ++i) {
  440. p = alloc_page(gfp_flags);
  441. if (!p) {
  442. pr_err("Unable to get page %u\n", i);
  443. /* store already allocated pages in the pool after
  444. * setting the caching state */
  445. if (cpages) {
  446. r = ttm_set_pages_caching(caching_array,
  447. cstate, cpages);
  448. if (r)
  449. ttm_handle_caching_state_failure(pages,
  450. ttm_flags, cstate,
  451. caching_array, cpages);
  452. }
  453. r = -ENOMEM;
  454. goto out;
  455. }
  456. #ifdef CONFIG_HIGHMEM
  457. /* gfp flags of highmem page should never be dma32 so we
  458. * we should be fine in such case
  459. */
  460. if (!PageHighMem(p))
  461. #endif
  462. {
  463. caching_array[cpages++] = p;
  464. if (cpages == max_cpages) {
  465. r = ttm_set_pages_caching(caching_array,
  466. cstate, cpages);
  467. if (r) {
  468. ttm_handle_caching_state_failure(pages,
  469. ttm_flags, cstate,
  470. caching_array, cpages);
  471. goto out;
  472. }
  473. cpages = 0;
  474. }
  475. }
  476. list_add(&p->lru, pages);
  477. }
  478. if (cpages) {
  479. r = ttm_set_pages_caching(caching_array, cstate, cpages);
  480. if (r)
  481. ttm_handle_caching_state_failure(pages,
  482. ttm_flags, cstate,
  483. caching_array, cpages);
  484. }
  485. out:
  486. kfree(caching_array);
  487. return r;
  488. }
  489. /**
  490. * Fill the given pool if there aren't enough pages and the requested number of
  491. * pages is small.
  492. */
  493. static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
  494. int ttm_flags, enum ttm_caching_state cstate, unsigned count,
  495. unsigned long *irq_flags)
  496. {
  497. struct page *p;
  498. int r;
  499. unsigned cpages = 0;
  500. /**
  501. * Only allow one pool fill operation at a time.
  502. * If pool doesn't have enough pages for the allocation new pages are
  503. * allocated from outside of pool.
  504. */
  505. if (pool->fill_lock)
  506. return;
  507. pool->fill_lock = true;
  508. /* If allocation request is small and there are not enough
  509. * pages in a pool we fill the pool up first. */
  510. if (count < _manager->options.small
  511. && count > pool->npages) {
  512. struct list_head new_pages;
  513. unsigned alloc_size = _manager->options.alloc_size;
  514. /**
  515. * Can't change page caching if in irqsave context. We have to
  516. * drop the pool->lock.
  517. */
  518. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  519. INIT_LIST_HEAD(&new_pages);
  520. r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
  521. cstate, alloc_size);
  522. spin_lock_irqsave(&pool->lock, *irq_flags);
  523. if (!r) {
  524. list_splice(&new_pages, &pool->list);
  525. ++pool->nrefills;
  526. pool->npages += alloc_size;
  527. } else {
  528. pr_err("Failed to fill pool (%p)\n", pool);
  529. /* If we have any pages left put them to the pool. */
  530. list_for_each_entry(p, &new_pages, lru) {
  531. ++cpages;
  532. }
  533. list_splice(&new_pages, &pool->list);
  534. pool->npages += cpages;
  535. }
  536. }
  537. pool->fill_lock = false;
  538. }
  539. /**
  540. * Cut 'count' number of pages from the pool and put them on the return list.
  541. *
  542. * @return count of pages still required to fulfill the request.
  543. */
  544. static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
  545. struct list_head *pages,
  546. int ttm_flags,
  547. enum ttm_caching_state cstate,
  548. unsigned count)
  549. {
  550. unsigned long irq_flags;
  551. struct list_head *p;
  552. unsigned i;
  553. spin_lock_irqsave(&pool->lock, irq_flags);
  554. ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count, &irq_flags);
  555. if (count >= pool->npages) {
  556. /* take all pages from the pool */
  557. list_splice_init(&pool->list, pages);
  558. count -= pool->npages;
  559. pool->npages = 0;
  560. goto out;
  561. }
  562. /* find the last pages to include for requested number of pages. Split
  563. * pool to begin and halve it to reduce search space. */
  564. if (count <= pool->npages/2) {
  565. i = 0;
  566. list_for_each(p, &pool->list) {
  567. if (++i == count)
  568. break;
  569. }
  570. } else {
  571. i = pool->npages + 1;
  572. list_for_each_prev(p, &pool->list) {
  573. if (--i == count)
  574. break;
  575. }
  576. }
  577. /* Cut 'count' number of pages from the pool */
  578. list_cut_position(pages, &pool->list, p);
  579. pool->npages -= count;
  580. count = 0;
  581. out:
  582. spin_unlock_irqrestore(&pool->lock, irq_flags);
  583. return count;
  584. }
  585. /* Put all pages in pages list to correct pool to wait for reuse */
  586. static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
  587. enum ttm_caching_state cstate)
  588. {
  589. unsigned long irq_flags;
  590. struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
  591. unsigned i;
  592. if (pool == NULL) {
  593. /* No pool for this memory type so free the pages */
  594. for (i = 0; i < npages; i++) {
  595. if (pages[i]) {
  596. if (page_count(pages[i]) != 1)
  597. pr_err("Erroneous page count. Leaking pages.\n");
  598. __free_page(pages[i]);
  599. pages[i] = NULL;
  600. }
  601. }
  602. return;
  603. }
  604. spin_lock_irqsave(&pool->lock, irq_flags);
  605. for (i = 0; i < npages; i++) {
  606. if (pages[i]) {
  607. if (page_count(pages[i]) != 1)
  608. pr_err("Erroneous page count. Leaking pages.\n");
  609. list_add_tail(&pages[i]->lru, &pool->list);
  610. pages[i] = NULL;
  611. pool->npages++;
  612. }
  613. }
  614. /* Check that we don't go over the pool limit */
  615. npages = 0;
  616. if (pool->npages > _manager->options.max_size) {
  617. npages = pool->npages - _manager->options.max_size;
  618. /* free at least NUM_PAGES_TO_ALLOC number of pages
  619. * to reduce calls to set_memory_wb */
  620. if (npages < NUM_PAGES_TO_ALLOC)
  621. npages = NUM_PAGES_TO_ALLOC;
  622. }
  623. spin_unlock_irqrestore(&pool->lock, irq_flags);
  624. if (npages)
  625. ttm_page_pool_free(pool, npages, false);
  626. }
  627. /*
  628. * On success pages list will hold count number of correctly
  629. * cached pages.
  630. */
  631. static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
  632. enum ttm_caching_state cstate)
  633. {
  634. struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
  635. struct list_head plist;
  636. struct page *p = NULL;
  637. gfp_t gfp_flags = GFP_USER;
  638. unsigned count;
  639. int r;
  640. /* set zero flag for page allocation if required */
  641. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  642. gfp_flags |= __GFP_ZERO;
  643. /* No pool for cached pages */
  644. if (pool == NULL) {
  645. if (flags & TTM_PAGE_FLAG_DMA32)
  646. gfp_flags |= GFP_DMA32;
  647. else
  648. gfp_flags |= GFP_HIGHUSER;
  649. for (r = 0; r < npages; ++r) {
  650. p = alloc_page(gfp_flags);
  651. if (!p) {
  652. pr_err("Unable to allocate page\n");
  653. return -ENOMEM;
  654. }
  655. pages[r] = p;
  656. }
  657. return 0;
  658. }
  659. /* combine zero flag to pool flags */
  660. gfp_flags |= pool->gfp_flags;
  661. /* First we take pages from the pool */
  662. INIT_LIST_HEAD(&plist);
  663. npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages);
  664. count = 0;
  665. list_for_each_entry(p, &plist, lru) {
  666. pages[count++] = p;
  667. }
  668. /* clear the pages coming from the pool if requested */
  669. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
  670. list_for_each_entry(p, &plist, lru) {
  671. if (PageHighMem(p))
  672. clear_highpage(p);
  673. else
  674. clear_page(page_address(p));
  675. }
  676. }
  677. /* If pool didn't have enough pages allocate new one. */
  678. if (npages > 0) {
  679. /* ttm_alloc_new_pages doesn't reference pool so we can run
  680. * multiple requests in parallel.
  681. **/
  682. INIT_LIST_HEAD(&plist);
  683. r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate, npages);
  684. list_for_each_entry(p, &plist, lru) {
  685. pages[count++] = p;
  686. }
  687. if (r) {
  688. /* If there is any pages in the list put them back to
  689. * the pool. */
  690. pr_err("Failed to allocate extra pages for large request\n");
  691. ttm_put_pages(pages, count, flags, cstate);
  692. return r;
  693. }
  694. }
  695. return 0;
  696. }
  697. static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
  698. char *name)
  699. {
  700. spin_lock_init(&pool->lock);
  701. pool->fill_lock = false;
  702. INIT_LIST_HEAD(&pool->list);
  703. pool->npages = pool->nfrees = 0;
  704. pool->gfp_flags = flags;
  705. pool->name = name;
  706. }
  707. int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  708. {
  709. int ret;
  710. WARN_ON(_manager);
  711. pr_info("Initializing pool allocator\n");
  712. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  713. if (!_manager)
  714. return -ENOMEM;
  715. ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc");
  716. ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc");
  717. ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
  718. GFP_USER | GFP_DMA32, "wc dma");
  719. ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
  720. GFP_USER | GFP_DMA32, "uc dma");
  721. _manager->options.max_size = max_pages;
  722. _manager->options.small = SMALL_ALLOCATION;
  723. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  724. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  725. &glob->kobj, "pool");
  726. if (unlikely(ret != 0)) {
  727. kobject_put(&_manager->kobj);
  728. _manager = NULL;
  729. return ret;
  730. }
  731. ttm_pool_mm_shrink_init(_manager);
  732. return 0;
  733. }
  734. void ttm_page_alloc_fini(void)
  735. {
  736. int i;
  737. pr_info("Finalizing pool allocator\n");
  738. ttm_pool_mm_shrink_fini(_manager);
  739. /* OK to use static buffer since global mutex is no longer used. */
  740. for (i = 0; i < NUM_POOLS; ++i)
  741. ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
  742. kobject_put(&_manager->kobj);
  743. _manager = NULL;
  744. }
  745. int ttm_pool_populate(struct ttm_tt *ttm)
  746. {
  747. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  748. unsigned i;
  749. int ret;
  750. if (ttm->state != tt_unpopulated)
  751. return 0;
  752. for (i = 0; i < ttm->num_pages; ++i) {
  753. ret = ttm_get_pages(&ttm->pages[i], 1,
  754. ttm->page_flags,
  755. ttm->caching_state);
  756. if (ret != 0) {
  757. ttm_pool_unpopulate(ttm);
  758. return -ENOMEM;
  759. }
  760. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  761. false, false);
  762. if (unlikely(ret != 0)) {
  763. ttm_pool_unpopulate(ttm);
  764. return -ENOMEM;
  765. }
  766. }
  767. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  768. ret = ttm_tt_swapin(ttm);
  769. if (unlikely(ret != 0)) {
  770. ttm_pool_unpopulate(ttm);
  771. return ret;
  772. }
  773. }
  774. ttm->state = tt_unbound;
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(ttm_pool_populate);
  778. void ttm_pool_unpopulate(struct ttm_tt *ttm)
  779. {
  780. unsigned i;
  781. for (i = 0; i < ttm->num_pages; ++i) {
  782. if (ttm->pages[i]) {
  783. ttm_mem_global_free_page(ttm->glob->mem_glob,
  784. ttm->pages[i]);
  785. ttm_put_pages(&ttm->pages[i], 1,
  786. ttm->page_flags,
  787. ttm->caching_state);
  788. }
  789. }
  790. ttm->state = tt_unpopulated;
  791. }
  792. EXPORT_SYMBOL(ttm_pool_unpopulate);
  793. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  794. {
  795. struct ttm_page_pool *p;
  796. unsigned i;
  797. char *h[] = {"pool", "refills", "pages freed", "size"};
  798. if (!_manager) {
  799. seq_printf(m, "No pool allocator running.\n");
  800. return 0;
  801. }
  802. seq_printf(m, "%6s %12s %13s %8s\n",
  803. h[0], h[1], h[2], h[3]);
  804. for (i = 0; i < NUM_POOLS; ++i) {
  805. p = &_manager->pools[i];
  806. seq_printf(m, "%6s %12ld %13ld %8d\n",
  807. p->name, p->nrefills,
  808. p->nfrees, p->npages);
  809. }
  810. return 0;
  811. }
  812. EXPORT_SYMBOL(ttm_page_alloc_debugfs);