ttm_page_alloc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. for (j = 0; j < HPAGE_PMD_NR; ++j)
  636. if (p++ != pages[i + j])
  637. break;
  638. if (j == HPAGE_PMD_NR)
  639. order = HPAGE_PMD_ORDER;
  640. }
  641. #endif
  642. if (page_count(pages[i]) != 1)
  643. pr_err("Erroneous page count. Leaking pages.\n");
  644. __free_pages(pages[i], order);
  645. j = 1 << order;
  646. while (j) {
  647. pages[i++] = NULL;
  648. --j;
  649. }
  650. }
  651. return;
  652. }
  653. i = 0;
  654. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  655. if (huge) {
  656. unsigned max_size, n2free;
  657. spin_lock_irqsave(&huge->lock, irq_flags);
  658. while (i < npages) {
  659. struct page *p = pages[i];
  660. unsigned j;
  661. if (!p)
  662. break;
  663. for (j = 0; j < HPAGE_PMD_NR; ++j)
  664. if (p++ != pages[i + j])
  665. break;
  666. if (j != HPAGE_PMD_NR)
  667. break;
  668. list_add_tail(&pages[i]->lru, &huge->list);
  669. for (j = 0; j < HPAGE_PMD_NR; ++j)
  670. pages[i++] = NULL;
  671. huge->npages++;
  672. }
  673. /* Check that we don't go over the pool limit */
  674. max_size = _manager->options.max_size;
  675. max_size /= HPAGE_PMD_NR;
  676. if (huge->npages > max_size)
  677. n2free = huge->npages - max_size;
  678. else
  679. n2free = 0;
  680. spin_unlock_irqrestore(&huge->lock, irq_flags);
  681. if (n2free)
  682. ttm_page_pool_free(huge, n2free, false);
  683. }
  684. #endif
  685. spin_lock_irqsave(&pool->lock, irq_flags);
  686. while (i < npages) {
  687. if (pages[i]) {
  688. if (page_count(pages[i]) != 1)
  689. pr_err("Erroneous page count. Leaking pages.\n");
  690. list_add_tail(&pages[i]->lru, &pool->list);
  691. pages[i] = NULL;
  692. pool->npages++;
  693. }
  694. ++i;
  695. }
  696. /* Check that we don't go over the pool limit */
  697. npages = 0;
  698. if (pool->npages > _manager->options.max_size) {
  699. npages = pool->npages - _manager->options.max_size;
  700. /* free at least NUM_PAGES_TO_ALLOC number of pages
  701. * to reduce calls to set_memory_wb */
  702. if (npages < NUM_PAGES_TO_ALLOC)
  703. npages = NUM_PAGES_TO_ALLOC;
  704. }
  705. spin_unlock_irqrestore(&pool->lock, irq_flags);
  706. if (npages)
  707. ttm_page_pool_free(pool, npages, false);
  708. }
  709. /*
  710. * On success pages list will hold count number of correctly
  711. * cached pages.
  712. */
  713. static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
  714. enum ttm_caching_state cstate)
  715. {
  716. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  717. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  718. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  719. #endif
  720. struct list_head plist;
  721. struct page *p = NULL;
  722. unsigned count, first;
  723. int r;
  724. /* No pool for cached pages */
  725. if (pool == NULL) {
  726. gfp_t gfp_flags = GFP_USER;
  727. unsigned i;
  728. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  729. unsigned j;
  730. #endif
  731. /* set zero flag for page allocation if required */
  732. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  733. gfp_flags |= __GFP_ZERO;
  734. if (flags & TTM_PAGE_FLAG_NO_RETRY)
  735. gfp_flags |= __GFP_RETRY_MAYFAIL;
  736. if (flags & TTM_PAGE_FLAG_DMA32)
  737. gfp_flags |= GFP_DMA32;
  738. else
  739. gfp_flags |= GFP_HIGHUSER;
  740. i = 0;
  741. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  742. if (!(gfp_flags & GFP_DMA32)) {
  743. while (npages >= HPAGE_PMD_NR) {
  744. gfp_t huge_flags = gfp_flags;
  745. huge_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
  746. __GFP_KSWAPD_RECLAIM;
  747. huge_flags &= ~__GFP_MOVABLE;
  748. huge_flags &= ~__GFP_COMP;
  749. p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
  750. if (!p)
  751. break;
  752. for (j = 0; j < HPAGE_PMD_NR; ++j)
  753. pages[i++] = p++;
  754. npages -= HPAGE_PMD_NR;
  755. }
  756. }
  757. #endif
  758. first = i;
  759. while (npages) {
  760. p = alloc_page(gfp_flags);
  761. if (!p) {
  762. pr_debug("Unable to allocate page\n");
  763. return -ENOMEM;
  764. }
  765. /* Swap the pages if we detect consecutive order */
  766. if (i > first && pages[i - 1] == p - 1)
  767. swap(p, pages[i - 1]);
  768. pages[i++] = p;
  769. --npages;
  770. }
  771. return 0;
  772. }
  773. count = 0;
  774. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  775. if (huge && npages >= HPAGE_PMD_NR) {
  776. INIT_LIST_HEAD(&plist);
  777. ttm_page_pool_get_pages(huge, &plist, flags, cstate,
  778. npages / HPAGE_PMD_NR,
  779. HPAGE_PMD_ORDER);
  780. list_for_each_entry(p, &plist, lru) {
  781. unsigned j;
  782. for (j = 0; j < HPAGE_PMD_NR; ++j)
  783. pages[count++] = &p[j];
  784. }
  785. }
  786. #endif
  787. INIT_LIST_HEAD(&plist);
  788. r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
  789. npages - count, 0);
  790. first = count;
  791. list_for_each_entry(p, &plist, lru) {
  792. struct page *tmp = p;
  793. /* Swap the pages if we detect consecutive order */
  794. if (count > first && pages[count - 1] == tmp - 1)
  795. swap(tmp, pages[count - 1]);
  796. pages[count++] = tmp;
  797. }
  798. if (r) {
  799. /* If there is any pages in the list put them back to
  800. * the pool.
  801. */
  802. pr_debug("Failed to allocate extra pages for large request\n");
  803. ttm_put_pages(pages, count, flags, cstate);
  804. return r;
  805. }
  806. return 0;
  807. }
  808. static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
  809. char *name, unsigned int order)
  810. {
  811. spin_lock_init(&pool->lock);
  812. pool->fill_lock = false;
  813. INIT_LIST_HEAD(&pool->list);
  814. pool->npages = pool->nfrees = 0;
  815. pool->gfp_flags = flags;
  816. pool->name = name;
  817. pool->order = order;
  818. }
  819. int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  820. {
  821. int ret;
  822. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  823. unsigned order = HPAGE_PMD_ORDER;
  824. #else
  825. unsigned order = 0;
  826. #endif
  827. WARN_ON(_manager);
  828. pr_info("Initializing pool allocator\n");
  829. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  830. if (!_manager)
  831. return -ENOMEM;
  832. ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
  833. ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
  834. ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
  835. GFP_USER | GFP_DMA32, "wc dma", 0);
  836. ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
  837. GFP_USER | GFP_DMA32, "uc dma", 0);
  838. ttm_page_pool_init_locked(&_manager->wc_pool_huge,
  839. (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
  840. __GFP_KSWAPD_RECLAIM) &
  841. ~(__GFP_MOVABLE | __GFP_COMP),
  842. "wc huge", order);
  843. ttm_page_pool_init_locked(&_manager->uc_pool_huge,
  844. (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
  845. __GFP_KSWAPD_RECLAIM) &
  846. ~(__GFP_MOVABLE | __GFP_COMP)
  847. , "uc huge", order);
  848. _manager->options.max_size = max_pages;
  849. _manager->options.small = SMALL_ALLOCATION;
  850. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  851. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  852. &glob->kobj, "pool");
  853. if (unlikely(ret != 0))
  854. goto error;
  855. ret = ttm_pool_mm_shrink_init(_manager);
  856. if (unlikely(ret != 0))
  857. goto error;
  858. return 0;
  859. error:
  860. kobject_put(&_manager->kobj);
  861. _manager = NULL;
  862. return ret;
  863. }
  864. void ttm_page_alloc_fini(void)
  865. {
  866. int i;
  867. pr_info("Finalizing pool allocator\n");
  868. ttm_pool_mm_shrink_fini(_manager);
  869. /* OK to use static buffer since global mutex is no longer used. */
  870. for (i = 0; i < NUM_POOLS; ++i)
  871. ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
  872. kobject_put(&_manager->kobj);
  873. _manager = NULL;
  874. }
  875. static void
  876. ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
  877. {
  878. struct ttm_mem_global *mem_glob = ttm->bdev->glob->mem_glob;
  879. unsigned i;
  880. if (mem_count_update == 0)
  881. goto put_pages;
  882. for (i = 0; i < mem_count_update; ++i) {
  883. if (!ttm->pages[i])
  884. continue;
  885. ttm_mem_global_free_page(mem_glob, ttm->pages[i], PAGE_SIZE);
  886. }
  887. put_pages:
  888. ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  889. ttm->caching_state);
  890. ttm->state = tt_unpopulated;
  891. }
  892. int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  893. {
  894. struct ttm_mem_global *mem_glob = ttm->bdev->glob->mem_glob;
  895. unsigned i;
  896. int ret;
  897. if (ttm->state != tt_unpopulated)
  898. return 0;
  899. if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx))
  900. return -ENOMEM;
  901. ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  902. ttm->caching_state);
  903. if (unlikely(ret != 0)) {
  904. ttm_pool_unpopulate_helper(ttm, 0);
  905. return ret;
  906. }
  907. for (i = 0; i < ttm->num_pages; ++i) {
  908. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  909. PAGE_SIZE, ctx);
  910. if (unlikely(ret != 0)) {
  911. ttm_pool_unpopulate_helper(ttm, i);
  912. return -ENOMEM;
  913. }
  914. }
  915. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  916. ret = ttm_tt_swapin(ttm);
  917. if (unlikely(ret != 0)) {
  918. ttm_pool_unpopulate(ttm);
  919. return ret;
  920. }
  921. }
  922. ttm->state = tt_unbound;
  923. return 0;
  924. }
  925. EXPORT_SYMBOL(ttm_pool_populate);
  926. void ttm_pool_unpopulate(struct ttm_tt *ttm)
  927. {
  928. ttm_pool_unpopulate_helper(ttm, ttm->num_pages);
  929. }
  930. EXPORT_SYMBOL(ttm_pool_unpopulate);
  931. int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt,
  932. struct ttm_operation_ctx *ctx)
  933. {
  934. unsigned i, j;
  935. int r;
  936. r = ttm_pool_populate(&tt->ttm, ctx);
  937. if (r)
  938. return r;
  939. for (i = 0; i < tt->ttm.num_pages; ++i) {
  940. struct page *p = tt->ttm.pages[i];
  941. size_t num_pages = 1;
  942. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  943. if (++p != tt->ttm.pages[j])
  944. break;
  945. ++num_pages;
  946. }
  947. tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
  948. 0, num_pages * PAGE_SIZE,
  949. DMA_BIDIRECTIONAL);
  950. if (dma_mapping_error(dev, tt->dma_address[i])) {
  951. while (i--) {
  952. dma_unmap_page(dev, tt->dma_address[i],
  953. PAGE_SIZE, DMA_BIDIRECTIONAL);
  954. tt->dma_address[i] = 0;
  955. }
  956. ttm_pool_unpopulate(&tt->ttm);
  957. return -EFAULT;
  958. }
  959. for (j = 1; j < num_pages; ++j) {
  960. tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
  961. ++i;
  962. }
  963. }
  964. return 0;
  965. }
  966. EXPORT_SYMBOL(ttm_populate_and_map_pages);
  967. void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
  968. {
  969. unsigned i, j;
  970. for (i = 0; i < tt->ttm.num_pages;) {
  971. struct page *p = tt->ttm.pages[i];
  972. size_t num_pages = 1;
  973. if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
  974. ++i;
  975. continue;
  976. }
  977. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  978. if (++p != tt->ttm.pages[j])
  979. break;
  980. ++num_pages;
  981. }
  982. dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
  983. DMA_BIDIRECTIONAL);
  984. i += num_pages;
  985. }
  986. ttm_pool_unpopulate(&tt->ttm);
  987. }
  988. EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
  989. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  990. {
  991. struct ttm_page_pool *p;
  992. unsigned i;
  993. char *h[] = {"pool", "refills", "pages freed", "size"};
  994. if (!_manager) {
  995. seq_printf(m, "No pool allocator running.\n");
  996. return 0;
  997. }
  998. seq_printf(m, "%7s %12s %13s %8s\n",
  999. h[0], h[1], h[2], h[3]);
  1000. for (i = 0; i < NUM_POOLS; ++i) {
  1001. p = &_manager->pools[i];
  1002. seq_printf(m, "%7s %12ld %13ld %8d\n",
  1003. p->name, p->nrefills,
  1004. p->nfrees, p->npages);
  1005. }
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(ttm_page_alloc_debugfs);