ttm_page_alloc.c 29 KB

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