ttm_page_alloc.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  754. ttm->caching_state);
  755. if (unlikely(ret != 0)) {
  756. ttm_pool_unpopulate(ttm);
  757. return ret;
  758. }
  759. for (i = 0; i < ttm->num_pages; ++i) {
  760. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  761. PAGE_SIZE);
  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. continue;
  784. ttm_mem_global_free_page(ttm->glob->mem_glob, ttm->pages[i],
  785. PAGE_SIZE);
  786. }
  787. ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  788. ttm->caching_state);
  789. ttm->state = tt_unpopulated;
  790. }
  791. EXPORT_SYMBOL(ttm_pool_unpopulate);
  792. #if defined(CONFIG_SWIOTLB) || defined(CONFIG_INTEL_IOMMU)
  793. int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt)
  794. {
  795. unsigned i, j;
  796. int r;
  797. r = ttm_pool_populate(&tt->ttm);
  798. if (r)
  799. return r;
  800. for (i = 0; i < tt->ttm.num_pages; ++i) {
  801. struct page *p = tt->ttm.pages[i];
  802. size_t num_pages = 1;
  803. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  804. if (++p != tt->ttm.pages[j])
  805. break;
  806. ++num_pages;
  807. }
  808. tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
  809. 0, num_pages * PAGE_SIZE,
  810. DMA_BIDIRECTIONAL);
  811. if (dma_mapping_error(dev, tt->dma_address[i])) {
  812. while (i--) {
  813. dma_unmap_page(dev, tt->dma_address[i],
  814. PAGE_SIZE, DMA_BIDIRECTIONAL);
  815. tt->dma_address[i] = 0;
  816. }
  817. ttm_pool_unpopulate(&tt->ttm);
  818. return -EFAULT;
  819. }
  820. for (j = 1; j < num_pages; ++j) {
  821. tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
  822. ++i;
  823. }
  824. }
  825. return 0;
  826. }
  827. EXPORT_SYMBOL(ttm_populate_and_map_pages);
  828. void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
  829. {
  830. unsigned i, j;
  831. for (i = 0; i < tt->ttm.num_pages;) {
  832. struct page *p = tt->ttm.pages[i];
  833. size_t num_pages = 1;
  834. if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
  835. ++i;
  836. continue;
  837. }
  838. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  839. if (++p != tt->ttm.pages[j])
  840. break;
  841. ++num_pages;
  842. }
  843. dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
  844. DMA_BIDIRECTIONAL);
  845. i += num_pages;
  846. }
  847. ttm_pool_unpopulate(&tt->ttm);
  848. }
  849. EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
  850. #endif
  851. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  852. {
  853. struct ttm_page_pool *p;
  854. unsigned i;
  855. char *h[] = {"pool", "refills", "pages freed", "size"};
  856. if (!_manager) {
  857. seq_printf(m, "No pool allocator running.\n");
  858. return 0;
  859. }
  860. seq_printf(m, "%6s %12s %13s %8s\n",
  861. h[0], h[1], h[2], h[3]);
  862. for (i = 0; i < NUM_POOLS; ++i) {
  863. p = &_manager->pools[i];
  864. seq_printf(m, "%6s %12ld %13ld %8d\n",
  865. p->name, p->nrefills,
  866. p->nfrees, p->npages);
  867. }
  868. return 0;
  869. }
  870. EXPORT_SYMBOL(ttm_page_alloc_debugfs);