ttm_page_alloc.c 29 KB

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