ttm_pool.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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/export.h>
  33. #include <linux/module.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/debugfs.h>
  36. #include <linux/highmem.h>
  37. #include <linux/sched/mm.h>
  38. #ifdef CONFIG_X86
  39. #include <asm/set_memory.h>
  40. #endif
  41. #include <drm/ttm/ttm_backup.h>
  42. #include <drm/ttm/ttm_pool.h>
  43. #include <drm/ttm/ttm_tt.h>
  44. #include <drm/ttm/ttm_bo.h>
  45. #include "ttm_module.h"
  46. #include "ttm_pool_internal.h"
  47. #ifdef CONFIG_FAULT_INJECTION
  48. #include <linux/fault-inject.h>
  49. static DECLARE_FAULT_ATTR(backup_fault_inject);
  50. #else
  51. #define should_fail(...) false
  52. #endif
  53. /**
  54. * struct ttm_pool_dma - Helper object for coherent DMA mappings
  55. *
  56. * @addr: original DMA address returned for the mapping
  57. * @vaddr: original vaddr return for the mapping and order in the lower bits
  58. */
  59. struct ttm_pool_dma {
  60. dma_addr_t addr;
  61. unsigned long vaddr;
  62. };
  63. /**
  64. * struct ttm_pool_alloc_state - Current state of the tt page allocation process
  65. * @pages: Pointer to the next tt page pointer to populate.
  66. * @caching_divide: Pointer to the first page pointer whose page has a staged but
  67. * not committed caching transition from write-back to @tt_caching.
  68. * @dma_addr: Pointer to the next tt dma_address entry to populate if any.
  69. * @remaining_pages: Remaining pages to populate.
  70. * @tt_caching: The requested cpu-caching for the pages allocated.
  71. */
  72. struct ttm_pool_alloc_state {
  73. struct page **pages;
  74. struct page **caching_divide;
  75. dma_addr_t *dma_addr;
  76. pgoff_t remaining_pages;
  77. enum ttm_caching tt_caching;
  78. };
  79. /**
  80. * struct ttm_pool_tt_restore - State representing restore from backup
  81. * @pool: The pool used for page allocation while restoring.
  82. * @snapshot_alloc: A snapshot of the most recent struct ttm_pool_alloc_state.
  83. * @alloced_page: Pointer to the page most recently allocated from a pool or system.
  84. * @first_dma: The dma address corresponding to @alloced_page if dma_mapping
  85. * is requested.
  86. * @alloced_pages: The number of allocated pages present in the struct ttm_tt
  87. * page vector from this restore session.
  88. * @restored_pages: The number of 4K pages restored for @alloced_page (which
  89. * is typically a multi-order page).
  90. * @page_caching: The struct ttm_tt requested caching
  91. * @order: The order of @alloced_page.
  92. *
  93. * Recovery from backup might fail when we've recovered less than the
  94. * full ttm_tt. In order not to loose any data (yet), keep information
  95. * around that allows us to restart a failed ttm backup recovery.
  96. */
  97. struct ttm_pool_tt_restore {
  98. struct ttm_pool *pool;
  99. struct ttm_pool_alloc_state snapshot_alloc;
  100. struct page *alloced_page;
  101. dma_addr_t first_dma;
  102. pgoff_t alloced_pages;
  103. pgoff_t restored_pages;
  104. enum ttm_caching page_caching;
  105. unsigned int order;
  106. };
  107. static unsigned long page_pool_size;
  108. MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
  109. module_param(page_pool_size, ulong, 0644);
  110. static atomic_long_t allocated_pages;
  111. static struct ttm_pool_type global_write_combined[NR_PAGE_ORDERS];
  112. static struct ttm_pool_type global_uncached[NR_PAGE_ORDERS];
  113. static struct ttm_pool_type global_dma32_write_combined[NR_PAGE_ORDERS];
  114. static struct ttm_pool_type global_dma32_uncached[NR_PAGE_ORDERS];
  115. static spinlock_t shrinker_lock;
  116. static struct list_head shrinker_list;
  117. static struct shrinker *mm_shrinker;
  118. static DECLARE_RWSEM(pool_shrink_rwsem);
  119. /* Allocate pages of size 1 << order with the given gfp_flags */
  120. static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
  121. unsigned int order)
  122. {
  123. const unsigned int beneficial_order = ttm_pool_beneficial_order(pool);
  124. unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
  125. struct ttm_pool_dma *dma;
  126. struct page *p;
  127. void *vaddr;
  128. /* Don't set the __GFP_COMP flag for higher order allocations.
  129. * Mapping pages directly into an userspace process and calling
  130. * put_page() on a TTM allocated page is illegal.
  131. */
  132. if (order)
  133. gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
  134. __GFP_THISNODE;
  135. /*
  136. * Do not add latency to the allocation path for allocations orders
  137. * device tolds us do not bring them additional performance gains.
  138. */
  139. if (beneficial_order && order > beneficial_order)
  140. gfp_flags &= ~__GFP_DIRECT_RECLAIM;
  141. if (!ttm_pool_uses_dma_alloc(pool)) {
  142. p = alloc_pages_node(pool->nid, gfp_flags, order);
  143. if (p)
  144. p->private = order;
  145. return p;
  146. }
  147. dma = kmalloc_obj(*dma);
  148. if (!dma)
  149. return NULL;
  150. if (order)
  151. attr |= DMA_ATTR_NO_WARN;
  152. vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
  153. &dma->addr, gfp_flags, attr);
  154. if (!vaddr)
  155. goto error_free;
  156. /* TODO: This is an illegal abuse of the DMA API, but we need to rework
  157. * TTM page fault handling and extend the DMA API to clean this up.
  158. */
  159. if (is_vmalloc_addr(vaddr))
  160. p = vmalloc_to_page(vaddr);
  161. else
  162. p = virt_to_page(vaddr);
  163. dma->vaddr = (unsigned long)vaddr | order;
  164. p->private = (unsigned long)dma;
  165. return p;
  166. error_free:
  167. kfree(dma);
  168. return NULL;
  169. }
  170. /* Reset the caching and pages of size 1 << order */
  171. static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
  172. unsigned int order, struct page *p)
  173. {
  174. unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
  175. struct ttm_pool_dma *dma;
  176. void *vaddr;
  177. #ifdef CONFIG_X86
  178. /* We don't care that set_pages_wb is inefficient here. This is only
  179. * used when we have to shrink and CPU overhead is irrelevant then.
  180. */
  181. if (caching != ttm_cached && !PageHighMem(p))
  182. set_pages_wb(p, 1 << order);
  183. #endif
  184. if (!pool || !ttm_pool_uses_dma_alloc(pool)) {
  185. __free_pages(p, order);
  186. return;
  187. }
  188. if (order)
  189. attr |= DMA_ATTR_NO_WARN;
  190. dma = (void *)p->private;
  191. vaddr = (void *)(dma->vaddr & PAGE_MASK);
  192. dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
  193. attr);
  194. kfree(dma);
  195. }
  196. /* Apply any cpu-caching deferred during page allocation */
  197. static int ttm_pool_apply_caching(struct ttm_pool_alloc_state *alloc)
  198. {
  199. #ifdef CONFIG_X86
  200. unsigned int num_pages = alloc->pages - alloc->caching_divide;
  201. if (!num_pages)
  202. return 0;
  203. switch (alloc->tt_caching) {
  204. case ttm_cached:
  205. break;
  206. case ttm_write_combined:
  207. return set_pages_array_wc(alloc->caching_divide, num_pages);
  208. case ttm_uncached:
  209. return set_pages_array_uc(alloc->caching_divide, num_pages);
  210. }
  211. #endif
  212. alloc->caching_divide = alloc->pages;
  213. return 0;
  214. }
  215. /* DMA Map pages of 1 << order size and return the resulting dma_address. */
  216. static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
  217. struct page *p, dma_addr_t *dma_addr)
  218. {
  219. dma_addr_t addr;
  220. if (ttm_pool_uses_dma_alloc(pool)) {
  221. struct ttm_pool_dma *dma = (void *)p->private;
  222. addr = dma->addr;
  223. } else {
  224. size_t size = (1ULL << order) * PAGE_SIZE;
  225. addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
  226. if (dma_mapping_error(pool->dev, addr))
  227. return -EFAULT;
  228. }
  229. *dma_addr = addr;
  230. return 0;
  231. }
  232. /* Unmap pages of 1 << order size */
  233. static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
  234. unsigned int num_pages)
  235. {
  236. /* Unmapped while freeing the page */
  237. if (ttm_pool_uses_dma_alloc(pool))
  238. return;
  239. dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
  240. DMA_BIDIRECTIONAL);
  241. }
  242. /* Give pages into a specific pool_type */
  243. static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
  244. {
  245. unsigned int i, num_pages = 1 << pt->order;
  246. for (i = 0; i < num_pages; ++i) {
  247. if (PageHighMem(p))
  248. clear_highpage(p + i);
  249. else
  250. clear_page(page_address(p + i));
  251. }
  252. spin_lock(&pt->lock);
  253. list_add(&p->lru, &pt->pages);
  254. spin_unlock(&pt->lock);
  255. atomic_long_add(1 << pt->order, &allocated_pages);
  256. }
  257. /* Take pages from a specific pool_type, return NULL when nothing available */
  258. static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
  259. {
  260. struct page *p;
  261. spin_lock(&pt->lock);
  262. p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
  263. if (p) {
  264. atomic_long_sub(1 << pt->order, &allocated_pages);
  265. list_del(&p->lru);
  266. }
  267. spin_unlock(&pt->lock);
  268. return p;
  269. }
  270. /* Initialize and add a pool type to the global shrinker list */
  271. static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
  272. enum ttm_caching caching, unsigned int order)
  273. {
  274. pt->pool = pool;
  275. pt->caching = caching;
  276. pt->order = order;
  277. spin_lock_init(&pt->lock);
  278. INIT_LIST_HEAD(&pt->pages);
  279. spin_lock(&shrinker_lock);
  280. list_add_tail(&pt->shrinker_list, &shrinker_list);
  281. spin_unlock(&shrinker_lock);
  282. }
  283. /* Remove a pool_type from the global shrinker list and free all pages */
  284. static void ttm_pool_type_fini(struct ttm_pool_type *pt)
  285. {
  286. struct page *p;
  287. spin_lock(&shrinker_lock);
  288. list_del(&pt->shrinker_list);
  289. spin_unlock(&shrinker_lock);
  290. while ((p = ttm_pool_type_take(pt)))
  291. ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
  292. }
  293. /* Return the pool_type to use for the given caching and order */
  294. static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
  295. enum ttm_caching caching,
  296. unsigned int order)
  297. {
  298. if (ttm_pool_uses_dma_alloc(pool))
  299. return &pool->caching[caching].orders[order];
  300. #ifdef CONFIG_X86
  301. switch (caching) {
  302. case ttm_write_combined:
  303. if (pool->nid != NUMA_NO_NODE)
  304. return &pool->caching[caching].orders[order];
  305. if (ttm_pool_uses_dma32(pool))
  306. return &global_dma32_write_combined[order];
  307. return &global_write_combined[order];
  308. case ttm_uncached:
  309. if (pool->nid != NUMA_NO_NODE)
  310. return &pool->caching[caching].orders[order];
  311. if (ttm_pool_uses_dma32(pool))
  312. return &global_dma32_uncached[order];
  313. return &global_uncached[order];
  314. default:
  315. break;
  316. }
  317. #endif
  318. return NULL;
  319. }
  320. /* Free pages using the global shrinker list */
  321. static unsigned int ttm_pool_shrink(void)
  322. {
  323. struct ttm_pool_type *pt;
  324. unsigned int num_pages;
  325. struct page *p;
  326. down_read(&pool_shrink_rwsem);
  327. spin_lock(&shrinker_lock);
  328. pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
  329. list_move_tail(&pt->shrinker_list, &shrinker_list);
  330. spin_unlock(&shrinker_lock);
  331. p = ttm_pool_type_take(pt);
  332. if (p) {
  333. ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
  334. num_pages = 1 << pt->order;
  335. } else {
  336. num_pages = 0;
  337. }
  338. up_read(&pool_shrink_rwsem);
  339. return num_pages;
  340. }
  341. /* Return the allocation order based for a page */
  342. static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
  343. {
  344. if (ttm_pool_uses_dma_alloc(pool)) {
  345. struct ttm_pool_dma *dma = (void *)p->private;
  346. return dma->vaddr & ~PAGE_MASK;
  347. }
  348. return p->private;
  349. }
  350. /*
  351. * Split larger pages so that we can free each PAGE_SIZE page as soon
  352. * as it has been backed up, in order to avoid memory pressure during
  353. * reclaim.
  354. */
  355. static void ttm_pool_split_for_swap(struct ttm_pool *pool, struct page *p)
  356. {
  357. unsigned int order = ttm_pool_page_order(pool, p);
  358. pgoff_t nr;
  359. if (!order)
  360. return;
  361. split_page(p, order);
  362. nr = 1UL << order;
  363. while (nr--)
  364. (p++)->private = 0;
  365. }
  366. /**
  367. * DOC: Partial backup and restoration of a struct ttm_tt.
  368. *
  369. * Swapout using ttm_backup_backup_page() and swapin using
  370. * ttm_backup_copy_page() may fail.
  371. * The former most likely due to lack of swap-space or memory, the latter due
  372. * to lack of memory or because of signal interruption during waits.
  373. *
  374. * Backup failure is easily handled by using a ttm_tt pages vector that holds
  375. * both backup handles and page pointers. This has to be taken into account when
  376. * restoring such a ttm_tt from backup, and when freeing it while backed up.
  377. * When restoring, for simplicity, new pages are actually allocated from the
  378. * pool and the contents of any old pages are copied in and then the old pages
  379. * are released.
  380. *
  381. * For restoration failures, the struct ttm_pool_tt_restore holds sufficient state
  382. * to be able to resume an interrupted restore, and that structure is freed once
  383. * the restoration is complete. If the struct ttm_tt is destroyed while there
  384. * is a valid struct ttm_pool_tt_restore attached, that is also properly taken
  385. * care of.
  386. */
  387. /* Is restore ongoing for the currently allocated page? */
  388. static bool ttm_pool_restore_valid(const struct ttm_pool_tt_restore *restore)
  389. {
  390. return restore && restore->restored_pages < (1 << restore->order);
  391. }
  392. /* DMA unmap and free a multi-order page, either to the relevant pool or to system. */
  393. static pgoff_t ttm_pool_unmap_and_free(struct ttm_pool *pool, struct page *page,
  394. const dma_addr_t *dma_addr, enum ttm_caching caching)
  395. {
  396. struct ttm_pool_type *pt = NULL;
  397. unsigned int order;
  398. pgoff_t nr;
  399. if (pool) {
  400. order = ttm_pool_page_order(pool, page);
  401. nr = (1UL << order);
  402. if (dma_addr)
  403. ttm_pool_unmap(pool, *dma_addr, nr);
  404. pt = ttm_pool_select_type(pool, caching, order);
  405. } else {
  406. order = page->private;
  407. nr = (1UL << order);
  408. }
  409. if (pt)
  410. ttm_pool_type_give(pt, page);
  411. else
  412. ttm_pool_free_page(pool, caching, order, page);
  413. return nr;
  414. }
  415. /* Populate the page-array using the most recent allocated multi-order page. */
  416. static void ttm_pool_allocated_page_commit(struct page *allocated,
  417. dma_addr_t first_dma,
  418. struct ttm_pool_alloc_state *alloc,
  419. pgoff_t nr)
  420. {
  421. pgoff_t i;
  422. for (i = 0; i < nr; ++i)
  423. *alloc->pages++ = allocated++;
  424. alloc->remaining_pages -= nr;
  425. if (!alloc->dma_addr)
  426. return;
  427. for (i = 0; i < nr; ++i) {
  428. *alloc->dma_addr++ = first_dma;
  429. first_dma += PAGE_SIZE;
  430. }
  431. }
  432. /*
  433. * When restoring, restore backed-up content to the newly allocated page and
  434. * if successful, populate the page-table and dma-address arrays.
  435. */
  436. static int ttm_pool_restore_commit(struct ttm_pool_tt_restore *restore,
  437. struct file *backup,
  438. const struct ttm_operation_ctx *ctx,
  439. struct ttm_pool_alloc_state *alloc)
  440. {
  441. pgoff_t i, nr = 1UL << restore->order;
  442. struct page **first_page = alloc->pages;
  443. struct page *p;
  444. int ret = 0;
  445. for (i = restore->restored_pages; i < nr; ++i) {
  446. p = first_page[i];
  447. if (ttm_backup_page_ptr_is_handle(p)) {
  448. unsigned long handle = ttm_backup_page_ptr_to_handle(p);
  449. if (IS_ENABLED(CONFIG_FAULT_INJECTION) && ctx->interruptible &&
  450. should_fail(&backup_fault_inject, 1)) {
  451. ret = -EINTR;
  452. break;
  453. }
  454. if (handle == 0) {
  455. restore->restored_pages++;
  456. continue;
  457. }
  458. ret = ttm_backup_copy_page(backup, restore->alloced_page + i,
  459. handle, ctx->interruptible);
  460. if (ret)
  461. break;
  462. ttm_backup_drop(backup, handle);
  463. } else if (p) {
  464. /*
  465. * We could probably avoid splitting the old page
  466. * using clever logic, but ATM we don't care, as
  467. * we prioritize releasing memory ASAP. Note that
  468. * here, the old retained page is always write-back
  469. * cached.
  470. */
  471. ttm_pool_split_for_swap(restore->pool, p);
  472. copy_highpage(restore->alloced_page + i, p);
  473. __free_pages(p, 0);
  474. }
  475. restore->restored_pages++;
  476. first_page[i] = ttm_backup_handle_to_page_ptr(0);
  477. }
  478. if (ret) {
  479. if (!restore->restored_pages) {
  480. dma_addr_t *dma_addr = alloc->dma_addr ? &restore->first_dma : NULL;
  481. ttm_pool_unmap_and_free(restore->pool, restore->alloced_page,
  482. dma_addr, restore->page_caching);
  483. restore->restored_pages = nr;
  484. }
  485. return ret;
  486. }
  487. ttm_pool_allocated_page_commit(restore->alloced_page, restore->first_dma,
  488. alloc, nr);
  489. if (restore->page_caching == alloc->tt_caching || PageHighMem(restore->alloced_page))
  490. alloc->caching_divide = alloc->pages;
  491. restore->snapshot_alloc = *alloc;
  492. restore->alloced_pages += nr;
  493. return 0;
  494. }
  495. /* If restoring, save information needed for ttm_pool_restore_commit(). */
  496. static void
  497. ttm_pool_page_allocated_restore(struct ttm_pool *pool, unsigned int order,
  498. struct page *p,
  499. enum ttm_caching page_caching,
  500. dma_addr_t first_dma,
  501. struct ttm_pool_tt_restore *restore,
  502. const struct ttm_pool_alloc_state *alloc)
  503. {
  504. restore->pool = pool;
  505. restore->order = order;
  506. restore->restored_pages = 0;
  507. restore->page_caching = page_caching;
  508. restore->first_dma = first_dma;
  509. restore->alloced_page = p;
  510. restore->snapshot_alloc = *alloc;
  511. }
  512. /*
  513. * Called when we got a page, either from a pool or newly allocated.
  514. * if needed, dma map the page and populate the dma address array.
  515. * Populate the page address array.
  516. * If the caching is consistent, update any deferred caching. Otherwise
  517. * stage this page for an upcoming deferred caching update.
  518. */
  519. static int ttm_pool_page_allocated(struct ttm_pool *pool, unsigned int order,
  520. struct page *p, enum ttm_caching page_caching,
  521. struct ttm_pool_alloc_state *alloc,
  522. struct ttm_pool_tt_restore *restore)
  523. {
  524. bool caching_consistent;
  525. dma_addr_t first_dma;
  526. int r = 0;
  527. caching_consistent = (page_caching == alloc->tt_caching) || PageHighMem(p);
  528. if (caching_consistent) {
  529. r = ttm_pool_apply_caching(alloc);
  530. if (r)
  531. return r;
  532. }
  533. if (alloc->dma_addr) {
  534. r = ttm_pool_map(pool, order, p, &first_dma);
  535. if (r)
  536. return r;
  537. }
  538. if (restore) {
  539. ttm_pool_page_allocated_restore(pool, order, p, page_caching,
  540. first_dma, restore, alloc);
  541. } else {
  542. ttm_pool_allocated_page_commit(p, first_dma, alloc, 1UL << order);
  543. if (caching_consistent)
  544. alloc->caching_divide = alloc->pages;
  545. }
  546. return 0;
  547. }
  548. /**
  549. * ttm_pool_free_range() - Free a range of TTM pages
  550. * @pool: The pool used for allocating.
  551. * @tt: The struct ttm_tt holding the page pointers.
  552. * @caching: The page caching mode used by the range.
  553. * @start_page: index for first page to free.
  554. * @end_page: index for last page to free + 1.
  555. *
  556. * During allocation the ttm_tt page-vector may be populated with ranges of
  557. * pages with different attributes if allocation hit an error without being
  558. * able to completely fulfill the allocation. This function can be used
  559. * to free these individual ranges.
  560. */
  561. static void ttm_pool_free_range(struct ttm_pool *pool, struct ttm_tt *tt,
  562. enum ttm_caching caching,
  563. pgoff_t start_page, pgoff_t end_page)
  564. {
  565. struct page **pages = &tt->pages[start_page];
  566. struct file *backup = tt->backup;
  567. pgoff_t i, nr;
  568. for (i = start_page; i < end_page; i += nr, pages += nr) {
  569. struct page *p = *pages;
  570. nr = 1;
  571. if (ttm_backup_page_ptr_is_handle(p)) {
  572. unsigned long handle = ttm_backup_page_ptr_to_handle(p);
  573. if (handle != 0)
  574. ttm_backup_drop(backup, handle);
  575. } else if (p) {
  576. dma_addr_t *dma_addr = tt->dma_address ?
  577. tt->dma_address + i : NULL;
  578. nr = ttm_pool_unmap_and_free(pool, p, dma_addr, caching);
  579. }
  580. }
  581. }
  582. static void ttm_pool_alloc_state_init(const struct ttm_tt *tt,
  583. struct ttm_pool_alloc_state *alloc)
  584. {
  585. alloc->pages = tt->pages;
  586. alloc->caching_divide = tt->pages;
  587. alloc->dma_addr = tt->dma_address;
  588. alloc->remaining_pages = tt->num_pages;
  589. alloc->tt_caching = tt->caching;
  590. }
  591. /*
  592. * Find a suitable allocation order based on highest desired order
  593. * and number of remaining pages
  594. */
  595. static unsigned int ttm_pool_alloc_find_order(unsigned int highest,
  596. const struct ttm_pool_alloc_state *alloc)
  597. {
  598. return min_t(unsigned int, highest, __fls(alloc->remaining_pages));
  599. }
  600. static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  601. const struct ttm_operation_ctx *ctx,
  602. struct ttm_pool_alloc_state *alloc,
  603. struct ttm_pool_tt_restore *restore)
  604. {
  605. enum ttm_caching page_caching;
  606. gfp_t gfp_flags = GFP_USER;
  607. pgoff_t caching_divide;
  608. unsigned int order;
  609. bool allow_pools;
  610. struct page *p;
  611. int r;
  612. WARN_ON(!alloc->remaining_pages || ttm_tt_is_populated(tt));
  613. WARN_ON(alloc->dma_addr && !pool->dev);
  614. if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
  615. gfp_flags |= __GFP_ZERO;
  616. if (ctx->gfp_retry_mayfail)
  617. gfp_flags |= __GFP_RETRY_MAYFAIL;
  618. if (ttm_pool_uses_dma32(pool))
  619. gfp_flags |= GFP_DMA32;
  620. else
  621. gfp_flags |= GFP_HIGHUSER;
  622. page_caching = tt->caching;
  623. allow_pools = true;
  624. for (order = ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc);
  625. alloc->remaining_pages;
  626. order = ttm_pool_alloc_find_order(order, alloc)) {
  627. struct ttm_pool_type *pt;
  628. /* First, try to allocate a page from a pool if one exists. */
  629. p = NULL;
  630. pt = ttm_pool_select_type(pool, page_caching, order);
  631. if (pt && allow_pools)
  632. p = ttm_pool_type_take(pt);
  633. /*
  634. * If that fails or previously failed, allocate from system.
  635. * Note that this also disallows additional pool allocations using
  636. * write-back cached pools of the same order. Consider removing
  637. * that behaviour.
  638. */
  639. if (!p) {
  640. page_caching = ttm_cached;
  641. allow_pools = false;
  642. p = ttm_pool_alloc_page(pool, gfp_flags, order);
  643. }
  644. /* If that fails, lower the order if possible and retry. */
  645. if (!p) {
  646. if (order) {
  647. --order;
  648. page_caching = tt->caching;
  649. allow_pools = true;
  650. continue;
  651. }
  652. r = -ENOMEM;
  653. goto error_free_all;
  654. }
  655. r = ttm_pool_page_allocated(pool, order, p, page_caching, alloc,
  656. restore);
  657. if (r)
  658. goto error_free_page;
  659. if (ttm_pool_restore_valid(restore)) {
  660. r = ttm_pool_restore_commit(restore, tt->backup, ctx, alloc);
  661. if (r)
  662. goto error_free_all;
  663. }
  664. }
  665. r = ttm_pool_apply_caching(alloc);
  666. if (r)
  667. goto error_free_all;
  668. kfree(tt->restore);
  669. tt->restore = NULL;
  670. return 0;
  671. error_free_page:
  672. ttm_pool_free_page(pool, page_caching, order, p);
  673. error_free_all:
  674. if (tt->restore)
  675. return r;
  676. caching_divide = alloc->caching_divide - tt->pages;
  677. ttm_pool_free_range(pool, tt, tt->caching, 0, caching_divide);
  678. ttm_pool_free_range(pool, tt, ttm_cached, caching_divide,
  679. tt->num_pages - alloc->remaining_pages);
  680. return r;
  681. }
  682. /**
  683. * ttm_pool_alloc - Fill a ttm_tt object
  684. *
  685. * @pool: ttm_pool to use
  686. * @tt: ttm_tt object to fill
  687. * @ctx: operation context
  688. *
  689. * Fill the ttm_tt object with pages and also make sure to DMA map them when
  690. * necessary.
  691. *
  692. * Returns: 0 on successe, negative error code otherwise.
  693. */
  694. int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  695. struct ttm_operation_ctx *ctx)
  696. {
  697. struct ttm_pool_alloc_state alloc;
  698. if (WARN_ON(ttm_tt_is_backed_up(tt)))
  699. return -EINVAL;
  700. ttm_pool_alloc_state_init(tt, &alloc);
  701. return __ttm_pool_alloc(pool, tt, ctx, &alloc, NULL);
  702. }
  703. EXPORT_SYMBOL(ttm_pool_alloc);
  704. /**
  705. * ttm_pool_restore_and_alloc - Fill a ttm_tt, restoring previously backed-up
  706. * content.
  707. *
  708. * @pool: ttm_pool to use
  709. * @tt: ttm_tt object to fill
  710. * @ctx: operation context
  711. *
  712. * Fill the ttm_tt object with pages and also make sure to DMA map them when
  713. * necessary. Read in backed-up content.
  714. *
  715. * Returns: 0 on successe, negative error code otherwise.
  716. */
  717. int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  718. const struct ttm_operation_ctx *ctx)
  719. {
  720. struct ttm_pool_tt_restore *restore = tt->restore;
  721. struct ttm_pool_alloc_state alloc;
  722. if (WARN_ON(!ttm_tt_is_backed_up(tt)))
  723. return -EINVAL;
  724. if (!restore) {
  725. gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
  726. ttm_pool_alloc_state_init(tt, &alloc);
  727. if (ctx->gfp_retry_mayfail)
  728. gfp |= __GFP_RETRY_MAYFAIL;
  729. restore = kzalloc_obj(*restore, gfp);
  730. if (!restore)
  731. return -ENOMEM;
  732. restore->snapshot_alloc = alloc;
  733. restore->pool = pool;
  734. restore->restored_pages = 1;
  735. tt->restore = restore;
  736. } else {
  737. alloc = restore->snapshot_alloc;
  738. if (ttm_pool_restore_valid(restore)) {
  739. int ret = ttm_pool_restore_commit(restore, tt->backup,
  740. ctx, &alloc);
  741. if (ret)
  742. return ret;
  743. }
  744. if (!alloc.remaining_pages)
  745. return 0;
  746. }
  747. return __ttm_pool_alloc(pool, tt, ctx, &alloc, restore);
  748. }
  749. /**
  750. * ttm_pool_free - Free the backing pages from a ttm_tt object
  751. *
  752. * @pool: Pool to give pages back to.
  753. * @tt: ttm_tt object to unpopulate
  754. *
  755. * Give the packing pages back to a pool or free them
  756. */
  757. void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
  758. {
  759. ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
  760. while (atomic_long_read(&allocated_pages) > page_pool_size)
  761. ttm_pool_shrink();
  762. }
  763. EXPORT_SYMBOL(ttm_pool_free);
  764. /**
  765. * ttm_pool_drop_backed_up() - Release content of a swapped-out struct ttm_tt
  766. * @tt: The struct ttm_tt.
  767. *
  768. * Release handles with associated content or any remaining pages of
  769. * a backed-up struct ttm_tt.
  770. */
  771. void ttm_pool_drop_backed_up(struct ttm_tt *tt)
  772. {
  773. struct ttm_pool_tt_restore *restore;
  774. pgoff_t start_page = 0;
  775. WARN_ON(!ttm_tt_is_backed_up(tt));
  776. restore = tt->restore;
  777. /*
  778. * Unmap and free any uncommitted restore page.
  779. * any tt page-array backup entries already read back has
  780. * been cleared already
  781. */
  782. if (ttm_pool_restore_valid(restore)) {
  783. dma_addr_t *dma_addr = tt->dma_address ? &restore->first_dma : NULL;
  784. ttm_pool_unmap_and_free(restore->pool, restore->alloced_page,
  785. dma_addr, restore->page_caching);
  786. restore->restored_pages = 1UL << restore->order;
  787. }
  788. /*
  789. * If a restore is ongoing, part of the tt pages may have a
  790. * caching different than writeback.
  791. */
  792. if (restore) {
  793. pgoff_t mid = restore->snapshot_alloc.caching_divide - tt->pages;
  794. start_page = restore->alloced_pages;
  795. WARN_ON(mid > start_page);
  796. /* Pages that might be dma-mapped and non-cached */
  797. ttm_pool_free_range(restore->pool, tt, tt->caching,
  798. 0, mid);
  799. /* Pages that might be dma-mapped but cached */
  800. ttm_pool_free_range(restore->pool, tt, ttm_cached,
  801. mid, restore->alloced_pages);
  802. kfree(restore);
  803. tt->restore = NULL;
  804. }
  805. ttm_pool_free_range(NULL, tt, ttm_cached, start_page, tt->num_pages);
  806. }
  807. /**
  808. * ttm_pool_backup() - Back up or purge a struct ttm_tt
  809. * @pool: The pool used when allocating the struct ttm_tt.
  810. * @tt: The struct ttm_tt.
  811. * @flags: Flags to govern the backup behaviour.
  812. *
  813. * Back up or purge a struct ttm_tt. If @purge is true, then
  814. * all pages will be freed directly to the system rather than to the pool
  815. * they were allocated from, making the function behave similarly to
  816. * ttm_pool_free(). If @purge is false the pages will be backed up instead,
  817. * exchanged for handles.
  818. * A subsequent call to ttm_pool_restore_and_alloc() will then read back the content and
  819. * a subsequent call to ttm_pool_drop_backed_up() will drop it.
  820. * If backup of a page fails for whatever reason, @ttm will still be
  821. * partially backed up, retaining those pages for which backup fails.
  822. * In that case, this function can be retried, possibly after freeing up
  823. * memory resources.
  824. *
  825. * Return: Number of pages actually backed up or freed, or negative
  826. * error code on error.
  827. */
  828. long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt,
  829. const struct ttm_backup_flags *flags)
  830. {
  831. struct file *backup = tt->backup;
  832. struct page *page;
  833. unsigned long handle;
  834. gfp_t alloc_gfp;
  835. gfp_t gfp;
  836. int ret = 0;
  837. pgoff_t shrunken = 0;
  838. pgoff_t i, num_pages;
  839. if (WARN_ON(ttm_tt_is_backed_up(tt)))
  840. return -EINVAL;
  841. if ((!ttm_backup_bytes_avail() && !flags->purge) ||
  842. ttm_pool_uses_dma_alloc(pool) || ttm_tt_is_backed_up(tt))
  843. return -EBUSY;
  844. #ifdef CONFIG_X86
  845. /* Anything returned to the system needs to be cached. */
  846. if (tt->caching != ttm_cached)
  847. set_pages_array_wb(tt->pages, tt->num_pages);
  848. #endif
  849. if (tt->dma_address || flags->purge) {
  850. for (i = 0; i < tt->num_pages; i += num_pages) {
  851. unsigned int order;
  852. page = tt->pages[i];
  853. if (unlikely(!page)) {
  854. num_pages = 1;
  855. continue;
  856. }
  857. order = ttm_pool_page_order(pool, page);
  858. num_pages = 1UL << order;
  859. if (tt->dma_address)
  860. ttm_pool_unmap(pool, tt->dma_address[i],
  861. num_pages);
  862. if (flags->purge) {
  863. shrunken += num_pages;
  864. page->private = 0;
  865. __free_pages(page, order);
  866. memset(tt->pages + i, 0,
  867. num_pages * sizeof(*tt->pages));
  868. }
  869. }
  870. }
  871. if (flags->purge)
  872. return shrunken;
  873. if (ttm_pool_uses_dma32(pool))
  874. gfp = GFP_DMA32;
  875. else
  876. gfp = GFP_HIGHUSER;
  877. alloc_gfp = GFP_KERNEL | __GFP_HIGH | __GFP_NOWARN | __GFP_RETRY_MAYFAIL;
  878. num_pages = tt->num_pages;
  879. /* Pretend doing fault injection by shrinking only half of the pages. */
  880. if (IS_ENABLED(CONFIG_FAULT_INJECTION) && should_fail(&backup_fault_inject, 1))
  881. num_pages = DIV_ROUND_UP(num_pages, 2);
  882. for (i = 0; i < num_pages; ++i) {
  883. s64 shandle;
  884. page = tt->pages[i];
  885. if (unlikely(!page))
  886. continue;
  887. ttm_pool_split_for_swap(pool, page);
  888. shandle = ttm_backup_backup_page(backup, page, flags->writeback, i,
  889. gfp, alloc_gfp);
  890. if (shandle < 0) {
  891. /* We allow partially shrunken tts */
  892. ret = shandle;
  893. break;
  894. }
  895. handle = shandle;
  896. tt->pages[i] = ttm_backup_handle_to_page_ptr(handle);
  897. put_page(page);
  898. shrunken++;
  899. }
  900. return shrunken ? shrunken : ret;
  901. }
  902. /**
  903. * ttm_pool_init - Initialize a pool
  904. *
  905. * @pool: the pool to initialize
  906. * @dev: device for DMA allocations and mappings
  907. * @nid: NUMA node to use for allocations
  908. * @alloc_flags: TTM_ALLOCATION_POOL_* flags
  909. *
  910. * Initialize the pool and its pool types.
  911. */
  912. void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
  913. int nid, unsigned int alloc_flags)
  914. {
  915. unsigned int i, j;
  916. WARN_ON(!dev && ttm_pool_uses_dma_alloc(pool));
  917. pool->dev = dev;
  918. pool->nid = nid;
  919. pool->alloc_flags = alloc_flags;
  920. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  921. for (j = 0; j < NR_PAGE_ORDERS; ++j) {
  922. struct ttm_pool_type *pt;
  923. /* Initialize only pool types which are actually used */
  924. pt = ttm_pool_select_type(pool, i, j);
  925. if (pt != &pool->caching[i].orders[j])
  926. continue;
  927. ttm_pool_type_init(pt, pool, i, j);
  928. }
  929. }
  930. }
  931. EXPORT_SYMBOL(ttm_pool_init);
  932. /**
  933. * ttm_pool_synchronize_shrinkers - Wait for all running shrinkers to complete.
  934. *
  935. * This is useful to guarantee that all shrinker invocations have seen an
  936. * update, before freeing memory, similar to rcu.
  937. */
  938. static void ttm_pool_synchronize_shrinkers(void)
  939. {
  940. down_write(&pool_shrink_rwsem);
  941. up_write(&pool_shrink_rwsem);
  942. }
  943. /**
  944. * ttm_pool_fini - Cleanup a pool
  945. *
  946. * @pool: the pool to clean up
  947. *
  948. * Free all pages in the pool and unregister the types from the global
  949. * shrinker.
  950. */
  951. void ttm_pool_fini(struct ttm_pool *pool)
  952. {
  953. unsigned int i, j;
  954. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  955. for (j = 0; j < NR_PAGE_ORDERS; ++j) {
  956. struct ttm_pool_type *pt;
  957. pt = ttm_pool_select_type(pool, i, j);
  958. if (pt != &pool->caching[i].orders[j])
  959. continue;
  960. ttm_pool_type_fini(pt);
  961. }
  962. }
  963. /* We removed the pool types from the LRU, but we need to also make sure
  964. * that no shrinker is concurrently freeing pages from the pool.
  965. */
  966. ttm_pool_synchronize_shrinkers();
  967. }
  968. EXPORT_SYMBOL(ttm_pool_fini);
  969. /* Free average pool number of pages. */
  970. #define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)
  971. static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
  972. struct shrink_control *sc)
  973. {
  974. unsigned long num_freed = 0;
  975. do
  976. num_freed += ttm_pool_shrink();
  977. while (num_freed < sc->nr_to_scan &&
  978. atomic_long_read(&allocated_pages));
  979. sc->nr_scanned = num_freed;
  980. return num_freed ?: SHRINK_STOP;
  981. }
  982. /* Return the number of pages available or SHRINK_EMPTY if we have none */
  983. static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
  984. struct shrink_control *sc)
  985. {
  986. unsigned long num_pages = atomic_long_read(&allocated_pages);
  987. return num_pages ? num_pages : SHRINK_EMPTY;
  988. }
  989. #ifdef CONFIG_DEBUG_FS
  990. /* Count the number of pages available in a pool_type */
  991. static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
  992. {
  993. unsigned int count = 0;
  994. struct page *p;
  995. spin_lock(&pt->lock);
  996. /* Only used for debugfs, the overhead doesn't matter */
  997. list_for_each_entry(p, &pt->pages, lru)
  998. ++count;
  999. spin_unlock(&pt->lock);
  1000. return count;
  1001. }
  1002. /* Print a nice header for the order */
  1003. static void ttm_pool_debugfs_header(struct seq_file *m)
  1004. {
  1005. unsigned int i;
  1006. seq_puts(m, "\t ");
  1007. for (i = 0; i < NR_PAGE_ORDERS; ++i)
  1008. seq_printf(m, " ---%2u---", i);
  1009. seq_puts(m, "\n");
  1010. }
  1011. /* Dump information about the different pool types */
  1012. static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
  1013. struct seq_file *m)
  1014. {
  1015. unsigned int i;
  1016. for (i = 0; i < NR_PAGE_ORDERS; ++i)
  1017. seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
  1018. seq_puts(m, "\n");
  1019. }
  1020. /* Dump the total amount of allocated pages */
  1021. static void ttm_pool_debugfs_footer(struct seq_file *m)
  1022. {
  1023. seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
  1024. atomic_long_read(&allocated_pages), page_pool_size);
  1025. }
  1026. /* Dump the information for the global pools */
  1027. static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
  1028. {
  1029. ttm_pool_debugfs_header(m);
  1030. spin_lock(&shrinker_lock);
  1031. seq_puts(m, "wc\t:");
  1032. ttm_pool_debugfs_orders(global_write_combined, m);
  1033. seq_puts(m, "uc\t:");
  1034. ttm_pool_debugfs_orders(global_uncached, m);
  1035. seq_puts(m, "wc 32\t:");
  1036. ttm_pool_debugfs_orders(global_dma32_write_combined, m);
  1037. seq_puts(m, "uc 32\t:");
  1038. ttm_pool_debugfs_orders(global_dma32_uncached, m);
  1039. spin_unlock(&shrinker_lock);
  1040. ttm_pool_debugfs_footer(m);
  1041. return 0;
  1042. }
  1043. DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
  1044. /**
  1045. * ttm_pool_debugfs - Debugfs dump function for a pool
  1046. *
  1047. * @pool: the pool to dump the information for
  1048. * @m: seq_file to dump to
  1049. *
  1050. * Make a debugfs dump with the per pool and global information.
  1051. */
  1052. int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
  1053. {
  1054. unsigned int i;
  1055. if (!ttm_pool_uses_dma_alloc(pool) && pool->nid == NUMA_NO_NODE) {
  1056. seq_puts(m, "unused\n");
  1057. return 0;
  1058. }
  1059. ttm_pool_debugfs_header(m);
  1060. spin_lock(&shrinker_lock);
  1061. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  1062. if (!ttm_pool_select_type(pool, i, 0))
  1063. continue;
  1064. if (ttm_pool_uses_dma_alloc(pool))
  1065. seq_puts(m, "DMA ");
  1066. else
  1067. seq_printf(m, "N%d ", pool->nid);
  1068. switch (i) {
  1069. case ttm_cached:
  1070. seq_puts(m, "\t:");
  1071. break;
  1072. case ttm_write_combined:
  1073. seq_puts(m, "wc\t:");
  1074. break;
  1075. case ttm_uncached:
  1076. seq_puts(m, "uc\t:");
  1077. break;
  1078. }
  1079. ttm_pool_debugfs_orders(pool->caching[i].orders, m);
  1080. }
  1081. spin_unlock(&shrinker_lock);
  1082. ttm_pool_debugfs_footer(m);
  1083. return 0;
  1084. }
  1085. EXPORT_SYMBOL(ttm_pool_debugfs);
  1086. /* Test the shrinker functions and dump the result */
  1087. static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
  1088. {
  1089. struct shrink_control sc = {
  1090. .gfp_mask = GFP_NOFS,
  1091. .nr_to_scan = TTM_SHRINKER_BATCH,
  1092. };
  1093. unsigned long count;
  1094. fs_reclaim_acquire(GFP_KERNEL);
  1095. count = ttm_pool_shrinker_count(mm_shrinker, &sc);
  1096. seq_printf(m, "%lu/%lu\n", count,
  1097. ttm_pool_shrinker_scan(mm_shrinker, &sc));
  1098. fs_reclaim_release(GFP_KERNEL);
  1099. return 0;
  1100. }
  1101. DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
  1102. #endif
  1103. /**
  1104. * ttm_pool_mgr_init - Initialize globals
  1105. *
  1106. * @num_pages: default number of pages
  1107. *
  1108. * Initialize the global locks and lists for the MM shrinker.
  1109. */
  1110. int ttm_pool_mgr_init(unsigned long num_pages)
  1111. {
  1112. unsigned int i;
  1113. if (!page_pool_size)
  1114. page_pool_size = num_pages;
  1115. spin_lock_init(&shrinker_lock);
  1116. INIT_LIST_HEAD(&shrinker_list);
  1117. for (i = 0; i < NR_PAGE_ORDERS; ++i) {
  1118. ttm_pool_type_init(&global_write_combined[i], NULL,
  1119. ttm_write_combined, i);
  1120. ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
  1121. ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
  1122. ttm_write_combined, i);
  1123. ttm_pool_type_init(&global_dma32_uncached[i], NULL,
  1124. ttm_uncached, i);
  1125. }
  1126. #ifdef CONFIG_DEBUG_FS
  1127. debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
  1128. &ttm_pool_debugfs_globals_fops);
  1129. debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
  1130. &ttm_pool_debugfs_shrink_fops);
  1131. #ifdef CONFIG_FAULT_INJECTION
  1132. fault_create_debugfs_attr("backup_fault_inject", ttm_debugfs_root,
  1133. &backup_fault_inject);
  1134. #endif
  1135. #endif
  1136. mm_shrinker = shrinker_alloc(0, "drm-ttm_pool");
  1137. if (!mm_shrinker)
  1138. return -ENOMEM;
  1139. mm_shrinker->count_objects = ttm_pool_shrinker_count;
  1140. mm_shrinker->scan_objects = ttm_pool_shrinker_scan;
  1141. mm_shrinker->batch = TTM_SHRINKER_BATCH;
  1142. mm_shrinker->seeks = 1;
  1143. shrinker_register(mm_shrinker);
  1144. return 0;
  1145. }
  1146. /**
  1147. * ttm_pool_mgr_fini - Finalize globals
  1148. *
  1149. * Cleanup the global pools and unregister the MM shrinker.
  1150. */
  1151. void ttm_pool_mgr_fini(void)
  1152. {
  1153. unsigned int i;
  1154. for (i = 0; i < NR_PAGE_ORDERS; ++i) {
  1155. ttm_pool_type_fini(&global_write_combined[i]);
  1156. ttm_pool_type_fini(&global_uncached[i]);
  1157. ttm_pool_type_fini(&global_dma32_write_combined[i]);
  1158. ttm_pool_type_fini(&global_dma32_uncached[i]);
  1159. }
  1160. shrinker_free(mm_shrinker);
  1161. WARN_ON(!list_empty(&shrinker_list));
  1162. }