ttm_page_alloc.c 27 KB

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