ttm_page_alloc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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_array(npages_to_free,
  303. sizeof(struct page *),
  304. GFP_KERNEL);
  305. if (!pages_to_free) {
  306. pr_debug("Failed to allocate memory for pool free operation\n");
  307. return 0;
  308. }
  309. restart:
  310. spin_lock_irqsave(&pool->lock, irq_flags);
  311. list_for_each_entry_reverse(p, &pool->list, lru) {
  312. if (freed_pages >= npages_to_free)
  313. break;
  314. pages_to_free[freed_pages++] = p;
  315. /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
  316. if (freed_pages >= NUM_PAGES_TO_ALLOC) {
  317. /* remove range of pages from the pool */
  318. __list_del(p->lru.prev, &pool->list);
  319. ttm_pool_update_free_locked(pool, freed_pages);
  320. /**
  321. * Because changing page caching is costly
  322. * we unlock the pool to prevent stalling.
  323. */
  324. spin_unlock_irqrestore(&pool->lock, irq_flags);
  325. ttm_pages_put(pages_to_free, freed_pages, pool->order);
  326. if (likely(nr_free != FREE_ALL_PAGES))
  327. nr_free -= freed_pages;
  328. if (NUM_PAGES_TO_ALLOC >= nr_free)
  329. npages_to_free = nr_free;
  330. else
  331. npages_to_free = NUM_PAGES_TO_ALLOC;
  332. freed_pages = 0;
  333. /* free all so restart the processing */
  334. if (nr_free)
  335. goto restart;
  336. /* Not allowed to fall through or break because
  337. * following context is inside spinlock while we are
  338. * outside here.
  339. */
  340. goto out;
  341. }
  342. }
  343. /* remove range of pages from the pool */
  344. if (freed_pages) {
  345. __list_del(&p->lru, &pool->list);
  346. ttm_pool_update_free_locked(pool, freed_pages);
  347. nr_free -= freed_pages;
  348. }
  349. spin_unlock_irqrestore(&pool->lock, irq_flags);
  350. if (freed_pages)
  351. ttm_pages_put(pages_to_free, freed_pages, pool->order);
  352. out:
  353. if (pages_to_free != static_buf)
  354. kfree(pages_to_free);
  355. return nr_free;
  356. }
  357. /**
  358. * Callback for mm to request pool to reduce number of page held.
  359. *
  360. * XXX: (dchinner) Deadlock warning!
  361. *
  362. * This code is crying out for a shrinker per pool....
  363. */
  364. static unsigned long
  365. ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  366. {
  367. static DEFINE_MUTEX(lock);
  368. static unsigned start_pool;
  369. unsigned i;
  370. unsigned pool_offset;
  371. struct ttm_page_pool *pool;
  372. int shrink_pages = sc->nr_to_scan;
  373. unsigned long freed = 0;
  374. unsigned int nr_free_pool;
  375. if (!mutex_trylock(&lock))
  376. return SHRINK_STOP;
  377. pool_offset = ++start_pool % NUM_POOLS;
  378. /* select start pool in round robin fashion */
  379. for (i = 0; i < NUM_POOLS; ++i) {
  380. unsigned nr_free = shrink_pages;
  381. unsigned page_nr;
  382. if (shrink_pages == 0)
  383. break;
  384. pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
  385. page_nr = (1 << pool->order);
  386. /* OK to use static buffer since global mutex is held. */
  387. nr_free_pool = roundup(nr_free, page_nr) >> pool->order;
  388. shrink_pages = ttm_page_pool_free(pool, nr_free_pool, true);
  389. freed += (nr_free_pool - shrink_pages) << pool->order;
  390. if (freed >= sc->nr_to_scan)
  391. break;
  392. shrink_pages <<= pool->order;
  393. }
  394. mutex_unlock(&lock);
  395. return freed;
  396. }
  397. static unsigned long
  398. ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  399. {
  400. unsigned i;
  401. unsigned long count = 0;
  402. struct ttm_page_pool *pool;
  403. for (i = 0; i < NUM_POOLS; ++i) {
  404. pool = &_manager->pools[i];
  405. count += (pool->npages << pool->order);
  406. }
  407. return count;
  408. }
  409. static int ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  410. {
  411. manager->mm_shrink.count_objects = ttm_pool_shrink_count;
  412. manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
  413. manager->mm_shrink.seeks = 1;
  414. return register_shrinker(&manager->mm_shrink);
  415. }
  416. static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  417. {
  418. unregister_shrinker(&manager->mm_shrink);
  419. }
  420. static int ttm_set_pages_caching(struct page **pages,
  421. enum ttm_caching_state cstate, unsigned cpages)
  422. {
  423. int r = 0;
  424. /* Set page caching */
  425. switch (cstate) {
  426. case tt_uncached:
  427. r = set_pages_array_uc(pages, cpages);
  428. if (r)
  429. pr_err("Failed to set %d pages to uc!\n", cpages);
  430. break;
  431. case tt_wc:
  432. r = set_pages_array_wc(pages, cpages);
  433. if (r)
  434. pr_err("Failed to set %d pages to wc!\n", cpages);
  435. break;
  436. default:
  437. break;
  438. }
  439. return r;
  440. }
  441. /**
  442. * Free pages the pages that failed to change the caching state. If there is
  443. * any pages that have changed their caching state already put them to the
  444. * pool.
  445. */
  446. static void ttm_handle_caching_state_failure(struct list_head *pages,
  447. int ttm_flags, enum ttm_caching_state cstate,
  448. struct page **failed_pages, unsigned cpages)
  449. {
  450. unsigned i;
  451. /* Failed pages have to be freed */
  452. for (i = 0; i < cpages; ++i) {
  453. list_del(&failed_pages[i]->lru);
  454. __free_page(failed_pages[i]);
  455. }
  456. }
  457. /**
  458. * Allocate new pages with correct caching.
  459. *
  460. * This function is reentrant if caller updates count depending on number of
  461. * pages returned in pages array.
  462. */
  463. static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
  464. int ttm_flags, enum ttm_caching_state cstate,
  465. unsigned count, unsigned order)
  466. {
  467. struct page **caching_array;
  468. struct page *p;
  469. int r = 0;
  470. unsigned i, j, cpages;
  471. unsigned npages = 1 << order;
  472. unsigned max_cpages = min(count << order, (unsigned)NUM_PAGES_TO_ALLOC);
  473. /* allocate array for page caching change */
  474. caching_array = kmalloc_array(max_cpages, sizeof(struct page *),
  475. GFP_KERNEL);
  476. if (!caching_array) {
  477. pr_debug("Unable to allocate table for new pages\n");
  478. return -ENOMEM;
  479. }
  480. for (i = 0, cpages = 0; i < count; ++i) {
  481. p = alloc_pages(gfp_flags, order);
  482. if (!p) {
  483. pr_debug("Unable to get page %u\n", i);
  484. /* store already allocated pages in the pool after
  485. * setting the caching state */
  486. if (cpages) {
  487. r = ttm_set_pages_caching(caching_array,
  488. cstate, cpages);
  489. if (r)
  490. ttm_handle_caching_state_failure(pages,
  491. ttm_flags, cstate,
  492. caching_array, cpages);
  493. }
  494. r = -ENOMEM;
  495. goto out;
  496. }
  497. list_add(&p->lru, pages);
  498. #ifdef CONFIG_HIGHMEM
  499. /* gfp flags of highmem page should never be dma32 so we
  500. * we should be fine in such case
  501. */
  502. if (PageHighMem(p))
  503. continue;
  504. #endif
  505. for (j = 0; j < npages; ++j) {
  506. caching_array[cpages++] = p++;
  507. if (cpages == max_cpages) {
  508. r = ttm_set_pages_caching(caching_array,
  509. cstate, cpages);
  510. if (r) {
  511. ttm_handle_caching_state_failure(pages,
  512. ttm_flags, cstate,
  513. caching_array, cpages);
  514. goto out;
  515. }
  516. cpages = 0;
  517. }
  518. }
  519. }
  520. if (cpages) {
  521. r = ttm_set_pages_caching(caching_array, cstate, cpages);
  522. if (r)
  523. ttm_handle_caching_state_failure(pages,
  524. ttm_flags, cstate,
  525. caching_array, cpages);
  526. }
  527. out:
  528. kfree(caching_array);
  529. return r;
  530. }
  531. /**
  532. * Fill the given pool if there aren't enough pages and the requested number of
  533. * pages is small.
  534. */
  535. static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
  536. enum ttm_caching_state cstate,
  537. unsigned count, unsigned long *irq_flags)
  538. {
  539. struct page *p;
  540. int r;
  541. unsigned cpages = 0;
  542. /**
  543. * Only allow one pool fill operation at a time.
  544. * If pool doesn't have enough pages for the allocation new pages are
  545. * allocated from outside of pool.
  546. */
  547. if (pool->fill_lock)
  548. return;
  549. pool->fill_lock = true;
  550. /* If allocation request is small and there are not enough
  551. * pages in a pool we fill the pool up first. */
  552. if (count < _manager->options.small
  553. && count > pool->npages) {
  554. struct list_head new_pages;
  555. unsigned alloc_size = _manager->options.alloc_size;
  556. /**
  557. * Can't change page caching if in irqsave context. We have to
  558. * drop the pool->lock.
  559. */
  560. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  561. INIT_LIST_HEAD(&new_pages);
  562. r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
  563. cstate, alloc_size, 0);
  564. spin_lock_irqsave(&pool->lock, *irq_flags);
  565. if (!r) {
  566. list_splice(&new_pages, &pool->list);
  567. ++pool->nrefills;
  568. pool->npages += alloc_size;
  569. } else {
  570. pr_debug("Failed to fill pool (%p)\n", pool);
  571. /* If we have any pages left put them to the pool. */
  572. list_for_each_entry(p, &new_pages, lru) {
  573. ++cpages;
  574. }
  575. list_splice(&new_pages, &pool->list);
  576. pool->npages += cpages;
  577. }
  578. }
  579. pool->fill_lock = false;
  580. }
  581. /**
  582. * Allocate pages from the pool and put them on the return list.
  583. *
  584. * @return zero for success or negative error code.
  585. */
  586. static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
  587. struct list_head *pages,
  588. int ttm_flags,
  589. enum ttm_caching_state cstate,
  590. unsigned count, unsigned order)
  591. {
  592. unsigned long irq_flags;
  593. struct list_head *p;
  594. unsigned i;
  595. int r = 0;
  596. spin_lock_irqsave(&pool->lock, irq_flags);
  597. if (!order)
  598. ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
  599. &irq_flags);
  600. if (count >= pool->npages) {
  601. /* take all pages from the pool */
  602. list_splice_init(&pool->list, pages);
  603. count -= pool->npages;
  604. pool->npages = 0;
  605. goto out;
  606. }
  607. /* find the last pages to include for requested number of pages. Split
  608. * pool to begin and halve it to reduce search space. */
  609. if (count <= pool->npages/2) {
  610. i = 0;
  611. list_for_each(p, &pool->list) {
  612. if (++i == count)
  613. break;
  614. }
  615. } else {
  616. i = pool->npages + 1;
  617. list_for_each_prev(p, &pool->list) {
  618. if (--i == count)
  619. break;
  620. }
  621. }
  622. /* Cut 'count' number of pages from the pool */
  623. list_cut_position(pages, &pool->list, p);
  624. pool->npages -= count;
  625. count = 0;
  626. out:
  627. spin_unlock_irqrestore(&pool->lock, irq_flags);
  628. /* clear the pages coming from the pool if requested */
  629. if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
  630. struct page *page;
  631. list_for_each_entry(page, pages, lru) {
  632. if (PageHighMem(page))
  633. clear_highpage(page);
  634. else
  635. clear_page(page_address(page));
  636. }
  637. }
  638. /* If pool didn't have enough pages allocate new one. */
  639. if (count) {
  640. gfp_t gfp_flags = pool->gfp_flags;
  641. /* set zero flag for page allocation if required */
  642. if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  643. gfp_flags |= __GFP_ZERO;
  644. if (ttm_flags & TTM_PAGE_FLAG_NO_RETRY)
  645. gfp_flags |= __GFP_RETRY_MAYFAIL;
  646. /* ttm_alloc_new_pages doesn't reference pool so we can run
  647. * multiple requests in parallel.
  648. **/
  649. r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
  650. count, order);
  651. }
  652. return r;
  653. }
  654. /* Put all pages in pages list to correct pool to wait for reuse */
  655. static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
  656. enum ttm_caching_state cstate)
  657. {
  658. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  659. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  660. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  661. #endif
  662. unsigned long irq_flags;
  663. unsigned i;
  664. if (pool == NULL) {
  665. /* No pool for this memory type so free the pages */
  666. i = 0;
  667. while (i < npages) {
  668. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  669. struct page *p = pages[i];
  670. #endif
  671. unsigned order = 0, j;
  672. if (!pages[i]) {
  673. ++i;
  674. continue;
  675. }
  676. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  677. if (!(flags & TTM_PAGE_FLAG_DMA32)) {
  678. for (j = 0; j < HPAGE_PMD_NR; ++j)
  679. if (p++ != pages[i + j])
  680. break;
  681. if (j == HPAGE_PMD_NR)
  682. order = HPAGE_PMD_ORDER;
  683. }
  684. #endif
  685. if (page_count(pages[i]) != 1)
  686. pr_err("Erroneous page count. Leaking pages.\n");
  687. __free_pages(pages[i], order);
  688. j = 1 << order;
  689. while (j) {
  690. pages[i++] = NULL;
  691. --j;
  692. }
  693. }
  694. return;
  695. }
  696. i = 0;
  697. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  698. if (huge) {
  699. unsigned max_size, n2free;
  700. spin_lock_irqsave(&huge->lock, irq_flags);
  701. while (i < npages) {
  702. struct page *p = pages[i];
  703. unsigned j;
  704. if (!p)
  705. break;
  706. for (j = 0; j < HPAGE_PMD_NR; ++j)
  707. if (p++ != pages[i + j])
  708. break;
  709. if (j != HPAGE_PMD_NR)
  710. break;
  711. list_add_tail(&pages[i]->lru, &huge->list);
  712. for (j = 0; j < HPAGE_PMD_NR; ++j)
  713. pages[i++] = NULL;
  714. huge->npages++;
  715. }
  716. /* Check that we don't go over the pool limit */
  717. max_size = _manager->options.max_size;
  718. max_size /= HPAGE_PMD_NR;
  719. if (huge->npages > max_size)
  720. n2free = huge->npages - max_size;
  721. else
  722. n2free = 0;
  723. spin_unlock_irqrestore(&huge->lock, irq_flags);
  724. if (n2free)
  725. ttm_page_pool_free(huge, n2free, false);
  726. }
  727. #endif
  728. spin_lock_irqsave(&pool->lock, irq_flags);
  729. while (i < npages) {
  730. if (pages[i]) {
  731. if (page_count(pages[i]) != 1)
  732. pr_err("Erroneous page count. Leaking pages.\n");
  733. list_add_tail(&pages[i]->lru, &pool->list);
  734. pages[i] = NULL;
  735. pool->npages++;
  736. }
  737. ++i;
  738. }
  739. /* Check that we don't go over the pool limit */
  740. npages = 0;
  741. if (pool->npages > _manager->options.max_size) {
  742. npages = pool->npages - _manager->options.max_size;
  743. /* free at least NUM_PAGES_TO_ALLOC number of pages
  744. * to reduce calls to set_memory_wb */
  745. if (npages < NUM_PAGES_TO_ALLOC)
  746. npages = NUM_PAGES_TO_ALLOC;
  747. }
  748. spin_unlock_irqrestore(&pool->lock, irq_flags);
  749. if (npages)
  750. ttm_page_pool_free(pool, npages, false);
  751. }
  752. /*
  753. * On success pages list will hold count number of correctly
  754. * cached pages.
  755. */
  756. static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
  757. enum ttm_caching_state cstate)
  758. {
  759. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  760. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  761. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  762. #endif
  763. struct list_head plist;
  764. struct page *p = NULL;
  765. unsigned count, first;
  766. int r;
  767. /* No pool for cached pages */
  768. if (pool == NULL) {
  769. gfp_t gfp_flags = GFP_USER;
  770. unsigned i;
  771. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  772. unsigned j;
  773. #endif
  774. /* set zero flag for page allocation if required */
  775. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  776. gfp_flags |= __GFP_ZERO;
  777. if (flags & TTM_PAGE_FLAG_NO_RETRY)
  778. gfp_flags |= __GFP_RETRY_MAYFAIL;
  779. if (flags & TTM_PAGE_FLAG_DMA32)
  780. gfp_flags |= GFP_DMA32;
  781. else
  782. gfp_flags |= GFP_HIGHUSER;
  783. i = 0;
  784. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  785. if (!(gfp_flags & GFP_DMA32)) {
  786. while (npages >= HPAGE_PMD_NR) {
  787. gfp_t huge_flags = gfp_flags;
  788. huge_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
  789. __GFP_KSWAPD_RECLAIM;
  790. huge_flags &= ~__GFP_MOVABLE;
  791. huge_flags &= ~__GFP_COMP;
  792. p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
  793. if (!p)
  794. break;
  795. for (j = 0; j < HPAGE_PMD_NR; ++j)
  796. pages[i++] = p++;
  797. npages -= HPAGE_PMD_NR;
  798. }
  799. }
  800. #endif
  801. first = i;
  802. while (npages) {
  803. p = alloc_page(gfp_flags);
  804. if (!p) {
  805. pr_debug("Unable to allocate page\n");
  806. return -ENOMEM;
  807. }
  808. /* Swap the pages if we detect consecutive order */
  809. if (i > first && pages[i - 1] == p - 1)
  810. swap(p, pages[i - 1]);
  811. pages[i++] = p;
  812. --npages;
  813. }
  814. return 0;
  815. }
  816. count = 0;
  817. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  818. if (huge && npages >= HPAGE_PMD_NR) {
  819. INIT_LIST_HEAD(&plist);
  820. ttm_page_pool_get_pages(huge, &plist, flags, cstate,
  821. npages / HPAGE_PMD_NR,
  822. HPAGE_PMD_ORDER);
  823. list_for_each_entry(p, &plist, lru) {
  824. unsigned j;
  825. for (j = 0; j < HPAGE_PMD_NR; ++j)
  826. pages[count++] = &p[j];
  827. }
  828. }
  829. #endif
  830. INIT_LIST_HEAD(&plist);
  831. r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
  832. npages - count, 0);
  833. first = count;
  834. list_for_each_entry(p, &plist, lru) {
  835. struct page *tmp = p;
  836. /* Swap the pages if we detect consecutive order */
  837. if (count > first && pages[count - 1] == tmp - 1)
  838. swap(tmp, pages[count - 1]);
  839. pages[count++] = tmp;
  840. }
  841. if (r) {
  842. /* If there is any pages in the list put them back to
  843. * the pool.
  844. */
  845. pr_debug("Failed to allocate extra pages for large request\n");
  846. ttm_put_pages(pages, count, flags, cstate);
  847. return r;
  848. }
  849. return 0;
  850. }
  851. static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
  852. char *name, unsigned int order)
  853. {
  854. spin_lock_init(&pool->lock);
  855. pool->fill_lock = false;
  856. INIT_LIST_HEAD(&pool->list);
  857. pool->npages = pool->nfrees = 0;
  858. pool->gfp_flags = flags;
  859. pool->name = name;
  860. pool->order = order;
  861. }
  862. int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  863. {
  864. int ret;
  865. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  866. unsigned order = HPAGE_PMD_ORDER;
  867. #else
  868. unsigned order = 0;
  869. #endif
  870. WARN_ON(_manager);
  871. pr_info("Initializing pool allocator\n");
  872. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  873. if (!_manager)
  874. return -ENOMEM;
  875. ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
  876. ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
  877. ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
  878. GFP_USER | GFP_DMA32, "wc dma", 0);
  879. ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
  880. GFP_USER | GFP_DMA32, "uc dma", 0);
  881. ttm_page_pool_init_locked(&_manager->wc_pool_huge,
  882. (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
  883. __GFP_KSWAPD_RECLAIM) &
  884. ~(__GFP_MOVABLE | __GFP_COMP),
  885. "wc huge", order);
  886. ttm_page_pool_init_locked(&_manager->uc_pool_huge,
  887. (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
  888. __GFP_KSWAPD_RECLAIM) &
  889. ~(__GFP_MOVABLE | __GFP_COMP)
  890. , "uc huge", order);
  891. _manager->options.max_size = max_pages;
  892. _manager->options.small = SMALL_ALLOCATION;
  893. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  894. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  895. &glob->kobj, "pool");
  896. if (unlikely(ret != 0))
  897. goto error;
  898. ret = ttm_pool_mm_shrink_init(_manager);
  899. if (unlikely(ret != 0))
  900. goto error;
  901. return 0;
  902. error:
  903. kobject_put(&_manager->kobj);
  904. _manager = NULL;
  905. return ret;
  906. }
  907. void ttm_page_alloc_fini(void)
  908. {
  909. int i;
  910. pr_info("Finalizing pool allocator\n");
  911. ttm_pool_mm_shrink_fini(_manager);
  912. /* OK to use static buffer since global mutex is no longer used. */
  913. for (i = 0; i < NUM_POOLS; ++i)
  914. ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
  915. kobject_put(&_manager->kobj);
  916. _manager = NULL;
  917. }
  918. static void
  919. ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
  920. {
  921. struct ttm_mem_global *mem_glob = ttm->bdev->glob->mem_glob;
  922. unsigned i;
  923. if (mem_count_update == 0)
  924. goto put_pages;
  925. for (i = 0; i < mem_count_update; ++i) {
  926. if (!ttm->pages[i])
  927. continue;
  928. ttm_mem_global_free_page(mem_glob, ttm->pages[i], PAGE_SIZE);
  929. }
  930. put_pages:
  931. ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  932. ttm->caching_state);
  933. ttm->state = tt_unpopulated;
  934. }
  935. int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  936. {
  937. struct ttm_mem_global *mem_glob = ttm->bdev->glob->mem_glob;
  938. unsigned i;
  939. int ret;
  940. if (ttm->state != tt_unpopulated)
  941. return 0;
  942. if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx))
  943. return -ENOMEM;
  944. ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  945. ttm->caching_state);
  946. if (unlikely(ret != 0)) {
  947. ttm_pool_unpopulate_helper(ttm, 0);
  948. return ret;
  949. }
  950. for (i = 0; i < ttm->num_pages; ++i) {
  951. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  952. PAGE_SIZE, ctx);
  953. if (unlikely(ret != 0)) {
  954. ttm_pool_unpopulate_helper(ttm, i);
  955. return -ENOMEM;
  956. }
  957. }
  958. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  959. ret = ttm_tt_swapin(ttm);
  960. if (unlikely(ret != 0)) {
  961. ttm_pool_unpopulate(ttm);
  962. return ret;
  963. }
  964. }
  965. ttm->state = tt_unbound;
  966. return 0;
  967. }
  968. EXPORT_SYMBOL(ttm_pool_populate);
  969. void ttm_pool_unpopulate(struct ttm_tt *ttm)
  970. {
  971. ttm_pool_unpopulate_helper(ttm, ttm->num_pages);
  972. }
  973. EXPORT_SYMBOL(ttm_pool_unpopulate);
  974. int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt,
  975. struct ttm_operation_ctx *ctx)
  976. {
  977. unsigned i, j;
  978. int r;
  979. r = ttm_pool_populate(&tt->ttm, ctx);
  980. if (r)
  981. return r;
  982. for (i = 0; i < tt->ttm.num_pages; ++i) {
  983. struct page *p = tt->ttm.pages[i];
  984. size_t num_pages = 1;
  985. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  986. if (++p != tt->ttm.pages[j])
  987. break;
  988. ++num_pages;
  989. }
  990. tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
  991. 0, num_pages * PAGE_SIZE,
  992. DMA_BIDIRECTIONAL);
  993. if (dma_mapping_error(dev, tt->dma_address[i])) {
  994. while (i--) {
  995. dma_unmap_page(dev, tt->dma_address[i],
  996. PAGE_SIZE, DMA_BIDIRECTIONAL);
  997. tt->dma_address[i] = 0;
  998. }
  999. ttm_pool_unpopulate(&tt->ttm);
  1000. return -EFAULT;
  1001. }
  1002. for (j = 1; j < num_pages; ++j) {
  1003. tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
  1004. ++i;
  1005. }
  1006. }
  1007. return 0;
  1008. }
  1009. EXPORT_SYMBOL(ttm_populate_and_map_pages);
  1010. void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
  1011. {
  1012. unsigned i, j;
  1013. for (i = 0; i < tt->ttm.num_pages;) {
  1014. struct page *p = tt->ttm.pages[i];
  1015. size_t num_pages = 1;
  1016. if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
  1017. ++i;
  1018. continue;
  1019. }
  1020. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  1021. if (++p != tt->ttm.pages[j])
  1022. break;
  1023. ++num_pages;
  1024. }
  1025. dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
  1026. DMA_BIDIRECTIONAL);
  1027. i += num_pages;
  1028. }
  1029. ttm_pool_unpopulate(&tt->ttm);
  1030. }
  1031. EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
  1032. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  1033. {
  1034. struct ttm_page_pool *p;
  1035. unsigned i;
  1036. char *h[] = {"pool", "refills", "pages freed", "size"};
  1037. if (!_manager) {
  1038. seq_printf(m, "No pool allocator running.\n");
  1039. return 0;
  1040. }
  1041. seq_printf(m, "%7s %12s %13s %8s\n",
  1042. h[0], h[1], h[2], h[3]);
  1043. for (i = 0; i < NUM_POOLS; ++i) {
  1044. p = &_manager->pools[i];
  1045. seq_printf(m, "%7s %12ld %13ld %8d\n",
  1046. p->name, p->nrefills,
  1047. p->nfrees, p->npages);
  1048. }
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL(ttm_page_alloc_debugfs);