ttm_page_alloc.c 24 KB

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