ttm_pool.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2020 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Christian König
  24. */
  25. /* Pooling of allocated pages is necessary because changing the caching
  26. * attributes on x86 of the linear mapping requires a costly cross CPU TLB
  27. * invalidate for those addresses.
  28. *
  29. * Additional to that allocations from the DMA coherent API are pooled as well
  30. * cause they are rather slow compared to alloc_pages+map.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/highmem.h>
  36. #include <linux/sched/mm.h>
  37. #ifdef CONFIG_X86
  38. #include <asm/set_memory.h>
  39. #endif
  40. #include <drm/ttm/ttm_pool.h>
  41. #include <drm/ttm/ttm_tt.h>
  42. #include <drm/ttm/ttm_bo.h>
  43. #include "ttm_module.h"
  44. /**
  45. * struct ttm_pool_dma - Helper object for coherent DMA mappings
  46. *
  47. * @addr: original DMA address returned for the mapping
  48. * @vaddr: original vaddr return for the mapping and order in the lower bits
  49. */
  50. struct ttm_pool_dma {
  51. dma_addr_t addr;
  52. unsigned long vaddr;
  53. };
  54. static unsigned long page_pool_size;
  55. MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
  56. module_param(page_pool_size, ulong, 0644);
  57. static atomic_long_t allocated_pages;
  58. static struct ttm_pool_type global_write_combined[NR_PAGE_ORDERS];
  59. static struct ttm_pool_type global_uncached[NR_PAGE_ORDERS];
  60. static struct ttm_pool_type global_dma32_write_combined[NR_PAGE_ORDERS];
  61. static struct ttm_pool_type global_dma32_uncached[NR_PAGE_ORDERS];
  62. static spinlock_t shrinker_lock;
  63. static struct list_head shrinker_list;
  64. static struct shrinker *mm_shrinker;
  65. static DECLARE_RWSEM(pool_shrink_rwsem);
  66. /* Allocate pages of size 1 << order with the given gfp_flags */
  67. static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
  68. unsigned int order)
  69. {
  70. unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
  71. struct ttm_pool_dma *dma;
  72. struct page *p;
  73. void *vaddr;
  74. /* Don't set the __GFP_COMP flag for higher order allocations.
  75. * Mapping pages directly into an userspace process and calling
  76. * put_page() on a TTM allocated page is illegal.
  77. */
  78. if (order)
  79. gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
  80. __GFP_THISNODE;
  81. if (!pool->use_dma_alloc) {
  82. p = alloc_pages_node(pool->nid, gfp_flags, order);
  83. if (p)
  84. p->private = order;
  85. return p;
  86. }
  87. dma = kmalloc(sizeof(*dma), GFP_KERNEL);
  88. if (!dma)
  89. return NULL;
  90. if (order)
  91. attr |= DMA_ATTR_NO_WARN;
  92. vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
  93. &dma->addr, gfp_flags, attr);
  94. if (!vaddr)
  95. goto error_free;
  96. /* TODO: This is an illegal abuse of the DMA API, but we need to rework
  97. * TTM page fault handling and extend the DMA API to clean this up.
  98. */
  99. if (is_vmalloc_addr(vaddr))
  100. p = vmalloc_to_page(vaddr);
  101. else
  102. p = virt_to_page(vaddr);
  103. dma->vaddr = (unsigned long)vaddr | order;
  104. p->private = (unsigned long)dma;
  105. return p;
  106. error_free:
  107. kfree(dma);
  108. return NULL;
  109. }
  110. /* Reset the caching and pages of size 1 << order */
  111. static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
  112. unsigned int order, struct page *p)
  113. {
  114. unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
  115. struct ttm_pool_dma *dma;
  116. void *vaddr;
  117. #ifdef CONFIG_X86
  118. /* We don't care that set_pages_wb is inefficient here. This is only
  119. * used when we have to shrink and CPU overhead is irrelevant then.
  120. */
  121. if (caching != ttm_cached && !PageHighMem(p))
  122. set_pages_wb(p, 1 << order);
  123. #endif
  124. if (!pool || !pool->use_dma_alloc) {
  125. __free_pages(p, order);
  126. return;
  127. }
  128. if (order)
  129. attr |= DMA_ATTR_NO_WARN;
  130. dma = (void *)p->private;
  131. vaddr = (void *)(dma->vaddr & PAGE_MASK);
  132. dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
  133. attr);
  134. kfree(dma);
  135. }
  136. /* Apply a new caching to an array of pages */
  137. static int ttm_pool_apply_caching(struct page **first, struct page **last,
  138. enum ttm_caching caching)
  139. {
  140. #ifdef CONFIG_X86
  141. unsigned int num_pages = last - first;
  142. if (!num_pages)
  143. return 0;
  144. switch (caching) {
  145. case ttm_cached:
  146. break;
  147. case ttm_write_combined:
  148. return set_pages_array_wc(first, num_pages);
  149. case ttm_uncached:
  150. return set_pages_array_uc(first, num_pages);
  151. }
  152. #endif
  153. return 0;
  154. }
  155. /* Map pages of 1 << order size and fill the DMA address array */
  156. static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
  157. struct page *p, dma_addr_t **dma_addr)
  158. {
  159. dma_addr_t addr;
  160. unsigned int i;
  161. if (pool->use_dma_alloc) {
  162. struct ttm_pool_dma *dma = (void *)p->private;
  163. addr = dma->addr;
  164. } else {
  165. size_t size = (1ULL << order) * PAGE_SIZE;
  166. addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
  167. if (dma_mapping_error(pool->dev, addr))
  168. return -EFAULT;
  169. }
  170. for (i = 1 << order; i ; --i) {
  171. *(*dma_addr)++ = addr;
  172. addr += PAGE_SIZE;
  173. }
  174. return 0;
  175. }
  176. /* Unmap pages of 1 << order size */
  177. static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
  178. unsigned int num_pages)
  179. {
  180. /* Unmapped while freeing the page */
  181. if (pool->use_dma_alloc)
  182. return;
  183. dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
  184. DMA_BIDIRECTIONAL);
  185. }
  186. /* Give pages into a specific pool_type */
  187. static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
  188. {
  189. unsigned int i, num_pages = 1 << pt->order;
  190. for (i = 0; i < num_pages; ++i) {
  191. if (PageHighMem(p))
  192. clear_highpage(p + i);
  193. else
  194. clear_page(page_address(p + i));
  195. }
  196. spin_lock(&pt->lock);
  197. list_add(&p->lru, &pt->pages);
  198. spin_unlock(&pt->lock);
  199. atomic_long_add(1 << pt->order, &allocated_pages);
  200. }
  201. /* Take pages from a specific pool_type, return NULL when nothing available */
  202. static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
  203. {
  204. struct page *p;
  205. spin_lock(&pt->lock);
  206. p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
  207. if (p) {
  208. atomic_long_sub(1 << pt->order, &allocated_pages);
  209. list_del(&p->lru);
  210. }
  211. spin_unlock(&pt->lock);
  212. return p;
  213. }
  214. /* Initialize and add a pool type to the global shrinker list */
  215. static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
  216. enum ttm_caching caching, unsigned int order)
  217. {
  218. pt->pool = pool;
  219. pt->caching = caching;
  220. pt->order = order;
  221. spin_lock_init(&pt->lock);
  222. INIT_LIST_HEAD(&pt->pages);
  223. spin_lock(&shrinker_lock);
  224. list_add_tail(&pt->shrinker_list, &shrinker_list);
  225. spin_unlock(&shrinker_lock);
  226. }
  227. /* Remove a pool_type from the global shrinker list and free all pages */
  228. static void ttm_pool_type_fini(struct ttm_pool_type *pt)
  229. {
  230. struct page *p;
  231. spin_lock(&shrinker_lock);
  232. list_del(&pt->shrinker_list);
  233. spin_unlock(&shrinker_lock);
  234. while ((p = ttm_pool_type_take(pt)))
  235. ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
  236. }
  237. /* Return the pool_type to use for the given caching and order */
  238. static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
  239. enum ttm_caching caching,
  240. unsigned int order)
  241. {
  242. if (pool->use_dma_alloc)
  243. return &pool->caching[caching].orders[order];
  244. #ifdef CONFIG_X86
  245. switch (caching) {
  246. case ttm_write_combined:
  247. if (pool->nid != NUMA_NO_NODE)
  248. return &pool->caching[caching].orders[order];
  249. if (pool->use_dma32)
  250. return &global_dma32_write_combined[order];
  251. return &global_write_combined[order];
  252. case ttm_uncached:
  253. if (pool->nid != NUMA_NO_NODE)
  254. return &pool->caching[caching].orders[order];
  255. if (pool->use_dma32)
  256. return &global_dma32_uncached[order];
  257. return &global_uncached[order];
  258. default:
  259. break;
  260. }
  261. #endif
  262. return NULL;
  263. }
  264. /* Free pages using the global shrinker list */
  265. static unsigned int ttm_pool_shrink(void)
  266. {
  267. struct ttm_pool_type *pt;
  268. unsigned int num_pages;
  269. struct page *p;
  270. down_read(&pool_shrink_rwsem);
  271. spin_lock(&shrinker_lock);
  272. pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
  273. list_move_tail(&pt->shrinker_list, &shrinker_list);
  274. spin_unlock(&shrinker_lock);
  275. p = ttm_pool_type_take(pt);
  276. if (p) {
  277. ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
  278. num_pages = 1 << pt->order;
  279. } else {
  280. num_pages = 0;
  281. }
  282. up_read(&pool_shrink_rwsem);
  283. return num_pages;
  284. }
  285. /* Return the allocation order based for a page */
  286. static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
  287. {
  288. if (pool->use_dma_alloc) {
  289. struct ttm_pool_dma *dma = (void *)p->private;
  290. return dma->vaddr & ~PAGE_MASK;
  291. }
  292. return p->private;
  293. }
  294. /* Called when we got a page, either from a pool or newly allocated */
  295. static int ttm_pool_page_allocated(struct ttm_pool *pool, unsigned int order,
  296. struct page *p, dma_addr_t **dma_addr,
  297. unsigned long *num_pages,
  298. struct page ***pages)
  299. {
  300. unsigned int i;
  301. int r;
  302. if (*dma_addr) {
  303. r = ttm_pool_map(pool, order, p, dma_addr);
  304. if (r)
  305. return r;
  306. }
  307. *num_pages -= 1 << order;
  308. for (i = 1 << order; i; --i, ++(*pages), ++p)
  309. **pages = p;
  310. return 0;
  311. }
  312. /**
  313. * ttm_pool_free_range() - Free a range of TTM pages
  314. * @pool: The pool used for allocating.
  315. * @tt: The struct ttm_tt holding the page pointers.
  316. * @caching: The page caching mode used by the range.
  317. * @start_page: index for first page to free.
  318. * @end_page: index for last page to free + 1.
  319. *
  320. * During allocation the ttm_tt page-vector may be populated with ranges of
  321. * pages with different attributes if allocation hit an error without being
  322. * able to completely fulfill the allocation. This function can be used
  323. * to free these individual ranges.
  324. */
  325. static void ttm_pool_free_range(struct ttm_pool *pool, struct ttm_tt *tt,
  326. enum ttm_caching caching,
  327. pgoff_t start_page, pgoff_t end_page)
  328. {
  329. struct page **pages = &tt->pages[start_page];
  330. unsigned int order;
  331. pgoff_t i, nr;
  332. for (i = start_page; i < end_page; i += nr, pages += nr) {
  333. struct ttm_pool_type *pt = NULL;
  334. order = ttm_pool_page_order(pool, *pages);
  335. nr = (1UL << order);
  336. if (tt->dma_address)
  337. ttm_pool_unmap(pool, tt->dma_address[i], nr);
  338. pt = ttm_pool_select_type(pool, caching, order);
  339. if (pt)
  340. ttm_pool_type_give(pt, *pages);
  341. else
  342. ttm_pool_free_page(pool, caching, order, *pages);
  343. }
  344. }
  345. /**
  346. * ttm_pool_alloc - Fill a ttm_tt object
  347. *
  348. * @pool: ttm_pool to use
  349. * @tt: ttm_tt object to fill
  350. * @ctx: operation context
  351. *
  352. * Fill the ttm_tt object with pages and also make sure to DMA map them when
  353. * necessary.
  354. *
  355. * Returns: 0 on successe, negative error code otherwise.
  356. */
  357. int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  358. struct ttm_operation_ctx *ctx)
  359. {
  360. pgoff_t num_pages = tt->num_pages;
  361. dma_addr_t *dma_addr = tt->dma_address;
  362. struct page **caching = tt->pages;
  363. struct page **pages = tt->pages;
  364. enum ttm_caching page_caching;
  365. gfp_t gfp_flags = GFP_USER;
  366. pgoff_t caching_divide;
  367. unsigned int order;
  368. struct page *p;
  369. int r;
  370. WARN_ON(!num_pages || ttm_tt_is_populated(tt));
  371. WARN_ON(dma_addr && !pool->dev);
  372. if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
  373. gfp_flags |= __GFP_ZERO;
  374. if (ctx->gfp_retry_mayfail)
  375. gfp_flags |= __GFP_RETRY_MAYFAIL;
  376. if (pool->use_dma32)
  377. gfp_flags |= GFP_DMA32;
  378. else
  379. gfp_flags |= GFP_HIGHUSER;
  380. for (order = min_t(unsigned int, MAX_PAGE_ORDER, __fls(num_pages));
  381. num_pages;
  382. order = min_t(unsigned int, order, __fls(num_pages))) {
  383. struct ttm_pool_type *pt;
  384. page_caching = tt->caching;
  385. pt = ttm_pool_select_type(pool, tt->caching, order);
  386. p = pt ? ttm_pool_type_take(pt) : NULL;
  387. if (p) {
  388. r = ttm_pool_apply_caching(caching, pages,
  389. tt->caching);
  390. if (r)
  391. goto error_free_page;
  392. caching = pages;
  393. do {
  394. r = ttm_pool_page_allocated(pool, order, p,
  395. &dma_addr,
  396. &num_pages,
  397. &pages);
  398. if (r)
  399. goto error_free_page;
  400. caching = pages;
  401. if (num_pages < (1 << order))
  402. break;
  403. p = ttm_pool_type_take(pt);
  404. } while (p);
  405. }
  406. page_caching = ttm_cached;
  407. while (num_pages >= (1 << order) &&
  408. (p = ttm_pool_alloc_page(pool, gfp_flags, order))) {
  409. if (PageHighMem(p)) {
  410. r = ttm_pool_apply_caching(caching, pages,
  411. tt->caching);
  412. if (r)
  413. goto error_free_page;
  414. caching = pages;
  415. }
  416. r = ttm_pool_page_allocated(pool, order, p, &dma_addr,
  417. &num_pages, &pages);
  418. if (r)
  419. goto error_free_page;
  420. if (PageHighMem(p))
  421. caching = pages;
  422. }
  423. if (!p) {
  424. if (order) {
  425. --order;
  426. continue;
  427. }
  428. r = -ENOMEM;
  429. goto error_free_all;
  430. }
  431. }
  432. r = ttm_pool_apply_caching(caching, pages, tt->caching);
  433. if (r)
  434. goto error_free_all;
  435. return 0;
  436. error_free_page:
  437. ttm_pool_free_page(pool, page_caching, order, p);
  438. error_free_all:
  439. num_pages = tt->num_pages - num_pages;
  440. caching_divide = caching - tt->pages;
  441. ttm_pool_free_range(pool, tt, tt->caching, 0, caching_divide);
  442. ttm_pool_free_range(pool, tt, ttm_cached, caching_divide, num_pages);
  443. return r;
  444. }
  445. EXPORT_SYMBOL(ttm_pool_alloc);
  446. /**
  447. * ttm_pool_free - Free the backing pages from a ttm_tt object
  448. *
  449. * @pool: Pool to give pages back to.
  450. * @tt: ttm_tt object to unpopulate
  451. *
  452. * Give the packing pages back to a pool or free them
  453. */
  454. void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
  455. {
  456. ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
  457. while (atomic_long_read(&allocated_pages) > page_pool_size)
  458. ttm_pool_shrink();
  459. }
  460. EXPORT_SYMBOL(ttm_pool_free);
  461. /**
  462. * ttm_pool_init - Initialize a pool
  463. *
  464. * @pool: the pool to initialize
  465. * @dev: device for DMA allocations and mappings
  466. * @nid: NUMA node to use for allocations
  467. * @use_dma_alloc: true if coherent DMA alloc should be used
  468. * @use_dma32: true if GFP_DMA32 should be used
  469. *
  470. * Initialize the pool and its pool types.
  471. */
  472. void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
  473. int nid, bool use_dma_alloc, bool use_dma32)
  474. {
  475. unsigned int i, j;
  476. WARN_ON(!dev && use_dma_alloc);
  477. pool->dev = dev;
  478. pool->nid = nid;
  479. pool->use_dma_alloc = use_dma_alloc;
  480. pool->use_dma32 = use_dma32;
  481. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  482. for (j = 0; j < NR_PAGE_ORDERS; ++j) {
  483. struct ttm_pool_type *pt;
  484. /* Initialize only pool types which are actually used */
  485. pt = ttm_pool_select_type(pool, i, j);
  486. if (pt != &pool->caching[i].orders[j])
  487. continue;
  488. ttm_pool_type_init(pt, pool, i, j);
  489. }
  490. }
  491. }
  492. EXPORT_SYMBOL(ttm_pool_init);
  493. /**
  494. * ttm_pool_synchronize_shrinkers - Wait for all running shrinkers to complete.
  495. *
  496. * This is useful to guarantee that all shrinker invocations have seen an
  497. * update, before freeing memory, similar to rcu.
  498. */
  499. static void ttm_pool_synchronize_shrinkers(void)
  500. {
  501. down_write(&pool_shrink_rwsem);
  502. up_write(&pool_shrink_rwsem);
  503. }
  504. /**
  505. * ttm_pool_fini - Cleanup a pool
  506. *
  507. * @pool: the pool to clean up
  508. *
  509. * Free all pages in the pool and unregister the types from the global
  510. * shrinker.
  511. */
  512. void ttm_pool_fini(struct ttm_pool *pool)
  513. {
  514. unsigned int i, j;
  515. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  516. for (j = 0; j < NR_PAGE_ORDERS; ++j) {
  517. struct ttm_pool_type *pt;
  518. pt = ttm_pool_select_type(pool, i, j);
  519. if (pt != &pool->caching[i].orders[j])
  520. continue;
  521. ttm_pool_type_fini(pt);
  522. }
  523. }
  524. /* We removed the pool types from the LRU, but we need to also make sure
  525. * that no shrinker is concurrently freeing pages from the pool.
  526. */
  527. ttm_pool_synchronize_shrinkers();
  528. }
  529. EXPORT_SYMBOL(ttm_pool_fini);
  530. static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
  531. struct shrink_control *sc)
  532. {
  533. unsigned long num_freed = 0;
  534. do
  535. num_freed += ttm_pool_shrink();
  536. while (num_freed < sc->nr_to_scan &&
  537. atomic_long_read(&allocated_pages));
  538. sc->nr_scanned = num_freed;
  539. return num_freed ?: SHRINK_STOP;
  540. }
  541. /* Return the number of pages available or SHRINK_EMPTY if we have none */
  542. static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
  543. struct shrink_control *sc)
  544. {
  545. unsigned long num_pages = atomic_long_read(&allocated_pages);
  546. return num_pages ? num_pages : SHRINK_EMPTY;
  547. }
  548. #ifdef CONFIG_DEBUG_FS
  549. /* Count the number of pages available in a pool_type */
  550. static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
  551. {
  552. unsigned int count = 0;
  553. struct page *p;
  554. spin_lock(&pt->lock);
  555. /* Only used for debugfs, the overhead doesn't matter */
  556. list_for_each_entry(p, &pt->pages, lru)
  557. ++count;
  558. spin_unlock(&pt->lock);
  559. return count;
  560. }
  561. /* Print a nice header for the order */
  562. static void ttm_pool_debugfs_header(struct seq_file *m)
  563. {
  564. unsigned int i;
  565. seq_puts(m, "\t ");
  566. for (i = 0; i < NR_PAGE_ORDERS; ++i)
  567. seq_printf(m, " ---%2u---", i);
  568. seq_puts(m, "\n");
  569. }
  570. /* Dump information about the different pool types */
  571. static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
  572. struct seq_file *m)
  573. {
  574. unsigned int i;
  575. for (i = 0; i < NR_PAGE_ORDERS; ++i)
  576. seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
  577. seq_puts(m, "\n");
  578. }
  579. /* Dump the total amount of allocated pages */
  580. static void ttm_pool_debugfs_footer(struct seq_file *m)
  581. {
  582. seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
  583. atomic_long_read(&allocated_pages), page_pool_size);
  584. }
  585. /* Dump the information for the global pools */
  586. static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
  587. {
  588. ttm_pool_debugfs_header(m);
  589. spin_lock(&shrinker_lock);
  590. seq_puts(m, "wc\t:");
  591. ttm_pool_debugfs_orders(global_write_combined, m);
  592. seq_puts(m, "uc\t:");
  593. ttm_pool_debugfs_orders(global_uncached, m);
  594. seq_puts(m, "wc 32\t:");
  595. ttm_pool_debugfs_orders(global_dma32_write_combined, m);
  596. seq_puts(m, "uc 32\t:");
  597. ttm_pool_debugfs_orders(global_dma32_uncached, m);
  598. spin_unlock(&shrinker_lock);
  599. ttm_pool_debugfs_footer(m);
  600. return 0;
  601. }
  602. DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
  603. /**
  604. * ttm_pool_debugfs - Debugfs dump function for a pool
  605. *
  606. * @pool: the pool to dump the information for
  607. * @m: seq_file to dump to
  608. *
  609. * Make a debugfs dump with the per pool and global information.
  610. */
  611. int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
  612. {
  613. unsigned int i;
  614. if (!pool->use_dma_alloc) {
  615. seq_puts(m, "unused\n");
  616. return 0;
  617. }
  618. ttm_pool_debugfs_header(m);
  619. spin_lock(&shrinker_lock);
  620. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  621. seq_puts(m, "DMA ");
  622. switch (i) {
  623. case ttm_cached:
  624. seq_puts(m, "\t:");
  625. break;
  626. case ttm_write_combined:
  627. seq_puts(m, "wc\t:");
  628. break;
  629. case ttm_uncached:
  630. seq_puts(m, "uc\t:");
  631. break;
  632. }
  633. ttm_pool_debugfs_orders(pool->caching[i].orders, m);
  634. }
  635. spin_unlock(&shrinker_lock);
  636. ttm_pool_debugfs_footer(m);
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(ttm_pool_debugfs);
  640. /* Test the shrinker functions and dump the result */
  641. static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
  642. {
  643. struct shrink_control sc = { .gfp_mask = GFP_NOFS };
  644. fs_reclaim_acquire(GFP_KERNEL);
  645. seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
  646. ttm_pool_shrinker_scan(mm_shrinker, &sc));
  647. fs_reclaim_release(GFP_KERNEL);
  648. return 0;
  649. }
  650. DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
  651. #endif
  652. /**
  653. * ttm_pool_mgr_init - Initialize globals
  654. *
  655. * @num_pages: default number of pages
  656. *
  657. * Initialize the global locks and lists for the MM shrinker.
  658. */
  659. int ttm_pool_mgr_init(unsigned long num_pages)
  660. {
  661. unsigned int i;
  662. if (!page_pool_size)
  663. page_pool_size = num_pages;
  664. spin_lock_init(&shrinker_lock);
  665. INIT_LIST_HEAD(&shrinker_list);
  666. for (i = 0; i < NR_PAGE_ORDERS; ++i) {
  667. ttm_pool_type_init(&global_write_combined[i], NULL,
  668. ttm_write_combined, i);
  669. ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
  670. ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
  671. ttm_write_combined, i);
  672. ttm_pool_type_init(&global_dma32_uncached[i], NULL,
  673. ttm_uncached, i);
  674. }
  675. #ifdef CONFIG_DEBUG_FS
  676. debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
  677. &ttm_pool_debugfs_globals_fops);
  678. debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
  679. &ttm_pool_debugfs_shrink_fops);
  680. #endif
  681. mm_shrinker = shrinker_alloc(0, "drm-ttm_pool");
  682. if (!mm_shrinker)
  683. return -ENOMEM;
  684. mm_shrinker->count_objects = ttm_pool_shrinker_count;
  685. mm_shrinker->scan_objects = ttm_pool_shrinker_scan;
  686. mm_shrinker->seeks = 1;
  687. shrinker_register(mm_shrinker);
  688. return 0;
  689. }
  690. /**
  691. * ttm_pool_mgr_fini - Finalize globals
  692. *
  693. * Cleanup the global pools and unregister the MM shrinker.
  694. */
  695. void ttm_pool_mgr_fini(void)
  696. {
  697. unsigned int i;
  698. for (i = 0; i < NR_PAGE_ORDERS; ++i) {
  699. ttm_pool_type_fini(&global_write_combined[i]);
  700. ttm_pool_type_fini(&global_uncached[i]);
  701. ttm_pool_type_fini(&global_dma32_write_combined[i]);
  702. ttm_pool_type_fini(&global_dma32_uncached[i]);
  703. }
  704. shrinker_free(mm_shrinker);
  705. WARN_ON(!list_empty(&shrinker_list));
  706. }