ttm_bo.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. */
  31. #define pr_fmt(fmt) "[TTM] " fmt
  32. #include <drm/drm_print.h>
  33. #include <drm/ttm/ttm_allocation.h>
  34. #include <drm/ttm/ttm_bo.h>
  35. #include <drm/ttm/ttm_placement.h>
  36. #include <drm/ttm/ttm_tt.h>
  37. #include <linux/export.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/slab.h>
  40. #include <linux/sched.h>
  41. #include <linux/mm.h>
  42. #include <linux/file.h>
  43. #include <linux/module.h>
  44. #include <linux/atomic.h>
  45. #include <linux/cgroup_dmem.h>
  46. #include <linux/dma-resv.h>
  47. #include "ttm_module.h"
  48. #include "ttm_bo_internal.h"
  49. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  50. struct ttm_placement *placement)
  51. {
  52. struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, TTM_PFX);
  53. struct ttm_resource_manager *man;
  54. int i, mem_type;
  55. for (i = 0; i < placement->num_placement; i++) {
  56. mem_type = placement->placement[i].mem_type;
  57. drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
  58. i, placement->placement[i].flags, mem_type);
  59. man = ttm_manager_type(bo->bdev, mem_type);
  60. ttm_resource_manager_debug(man, &p);
  61. }
  62. }
  63. /**
  64. * ttm_bo_move_to_lru_tail
  65. *
  66. * @bo: The buffer object.
  67. *
  68. * Move this BO to the tail of all lru lists used to lookup and reserve an
  69. * object. This function must be called with struct ttm_global::lru_lock
  70. * held, and is used to make a BO less likely to be considered for eviction.
  71. */
  72. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
  73. {
  74. dma_resv_assert_held(bo->base.resv);
  75. if (bo->resource)
  76. ttm_resource_move_to_lru_tail(bo->resource);
  77. }
  78. EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
  79. /**
  80. * ttm_bo_set_bulk_move - update BOs bulk move object
  81. *
  82. * @bo: The buffer object.
  83. * @bulk: bulk move structure
  84. *
  85. * Update the BOs bulk move object, making sure that resources are added/removed
  86. * as well. A bulk move allows to move many resource on the LRU at once,
  87. * resulting in much less overhead of maintaining the LRU.
  88. * The only requirement is that the resources stay together on the LRU and are
  89. * never separated. This is enforces by setting the bulk_move structure on a BO.
  90. * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of
  91. * their LRU list.
  92. */
  93. void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
  94. struct ttm_lru_bulk_move *bulk)
  95. {
  96. dma_resv_assert_held(bo->base.resv);
  97. if (bo->bulk_move == bulk)
  98. return;
  99. spin_lock(&bo->bdev->lru_lock);
  100. if (bo->resource)
  101. ttm_resource_del_bulk_move(bo->resource, bo);
  102. bo->bulk_move = bulk;
  103. if (bo->resource)
  104. ttm_resource_add_bulk_move(bo->resource, bo);
  105. spin_unlock(&bo->bdev->lru_lock);
  106. }
  107. EXPORT_SYMBOL(ttm_bo_set_bulk_move);
  108. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  109. struct ttm_resource *mem, bool evict,
  110. struct ttm_operation_ctx *ctx,
  111. struct ttm_place *hop)
  112. {
  113. struct ttm_device *bdev = bo->bdev;
  114. bool old_use_tt, new_use_tt;
  115. int ret;
  116. old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
  117. new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
  118. ttm_bo_unmap_virtual(bo);
  119. /*
  120. * Create and bind a ttm if required.
  121. */
  122. if (new_use_tt) {
  123. /* Zero init the new TTM structure if the old location should
  124. * have used one as well.
  125. */
  126. ret = ttm_tt_create(bo, old_use_tt);
  127. if (ret)
  128. goto out_err;
  129. if (mem->mem_type != TTM_PL_SYSTEM) {
  130. ret = ttm_bo_populate(bo, ctx);
  131. if (ret)
  132. goto out_err;
  133. }
  134. }
  135. ret = dma_resv_reserve_fences(bo->base.resv, 1);
  136. if (ret)
  137. goto out_err;
  138. ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
  139. if (ret) {
  140. if (ret == -EMULTIHOP)
  141. return ret;
  142. goto out_err;
  143. }
  144. ctx->bytes_moved += bo->base.size;
  145. return 0;
  146. out_err:
  147. if (!old_use_tt)
  148. ttm_bo_tt_destroy(bo);
  149. return ret;
  150. }
  151. /*
  152. * Call bo::reserved.
  153. * Will release GPU memory type usage on destruction.
  154. * This is the place to put in driver specific hooks to release
  155. * driver private resources.
  156. * Will release the bo::reserved lock.
  157. */
  158. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  159. {
  160. if (bo->bdev->funcs->delete_mem_notify)
  161. bo->bdev->funcs->delete_mem_notify(bo);
  162. ttm_bo_tt_destroy(bo);
  163. ttm_resource_free(bo, &bo->resource);
  164. }
  165. static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
  166. {
  167. int r;
  168. if (bo->base.resv == &bo->base._resv)
  169. return 0;
  170. BUG_ON(!dma_resv_trylock(&bo->base._resv));
  171. r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
  172. dma_resv_unlock(&bo->base._resv);
  173. if (r)
  174. return r;
  175. if (bo->type != ttm_bo_type_sg) {
  176. /* This works because the BO is about to be destroyed and nobody
  177. * reference it any more. The only tricky case is the trylock on
  178. * the resv object while holding the lru_lock.
  179. */
  180. spin_lock(&bo->bdev->lru_lock);
  181. bo->base.resv = &bo->base._resv;
  182. spin_unlock(&bo->bdev->lru_lock);
  183. }
  184. return r;
  185. }
  186. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  187. {
  188. struct dma_resv *resv = &bo->base._resv;
  189. struct dma_resv_iter cursor;
  190. struct dma_fence *fence;
  191. dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
  192. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  193. if (!fence->ops->signaled)
  194. dma_fence_enable_sw_signaling(fence);
  195. }
  196. dma_resv_iter_end(&cursor);
  197. }
  198. /*
  199. * Block for the dma_resv object to become idle, lock the buffer and clean up
  200. * the resource and tt object.
  201. */
  202. static void ttm_bo_delayed_delete(struct work_struct *work)
  203. {
  204. struct ttm_buffer_object *bo;
  205. bo = container_of(work, typeof(*bo), delayed_delete);
  206. dma_resv_wait_timeout(&bo->base._resv, DMA_RESV_USAGE_BOOKKEEP, false,
  207. MAX_SCHEDULE_TIMEOUT);
  208. dma_resv_lock(bo->base.resv, NULL);
  209. ttm_bo_cleanup_memtype_use(bo);
  210. dma_resv_unlock(bo->base.resv);
  211. ttm_bo_put(bo);
  212. }
  213. static void ttm_bo_release(struct kref *kref)
  214. {
  215. struct ttm_buffer_object *bo =
  216. container_of(kref, struct ttm_buffer_object, kref);
  217. struct ttm_device *bdev = bo->bdev;
  218. int ret;
  219. WARN_ON_ONCE(bo->pin_count);
  220. WARN_ON_ONCE(bo->bulk_move);
  221. if (!bo->deleted) {
  222. ret = ttm_bo_individualize_resv(bo);
  223. if (ret) {
  224. /* Last resort, if we fail to allocate memory for the
  225. * fences block for the BO to become idle
  226. */
  227. dma_resv_wait_timeout(bo->base.resv,
  228. DMA_RESV_USAGE_BOOKKEEP, false,
  229. 30 * HZ);
  230. }
  231. if (bdev->funcs->release_notify)
  232. bdev->funcs->release_notify(bo);
  233. drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
  234. ttm_mem_io_free(bdev, bo->resource);
  235. if (!dma_resv_test_signaled(&bo->base._resv,
  236. DMA_RESV_USAGE_BOOKKEEP) ||
  237. (want_init_on_free() && (bo->ttm != NULL)) ||
  238. bo->type == ttm_bo_type_sg ||
  239. !dma_resv_trylock(bo->base.resv)) {
  240. /* The BO is not idle, resurrect it for delayed destroy */
  241. ttm_bo_flush_all_fences(bo);
  242. bo->deleted = true;
  243. spin_lock(&bdev->lru_lock);
  244. /*
  245. * Make pinned bos immediately available to
  246. * shrinkers, now that they are queued for
  247. * destruction.
  248. *
  249. * FIXME: QXL is triggering this. Can be removed when the
  250. * driver is fixed.
  251. */
  252. if (bo->pin_count) {
  253. bo->pin_count = 0;
  254. ttm_resource_move_to_lru_tail(bo->resource);
  255. }
  256. kref_init(&bo->kref);
  257. spin_unlock(&bdev->lru_lock);
  258. INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete);
  259. /* Schedule the worker on the closest NUMA node. This
  260. * improves performance since system memory might be
  261. * cleared on free and that is best done on a CPU core
  262. * close to it.
  263. */
  264. queue_work_node(bdev->pool.nid, bdev->wq, &bo->delayed_delete);
  265. return;
  266. }
  267. ttm_bo_cleanup_memtype_use(bo);
  268. dma_resv_unlock(bo->base.resv);
  269. }
  270. atomic_dec(&ttm_glob.bo_count);
  271. bo->destroy(bo);
  272. }
  273. /* TODO: remove! */
  274. void ttm_bo_put(struct ttm_buffer_object *bo)
  275. {
  276. kref_put(&bo->kref, ttm_bo_release);
  277. }
  278. void ttm_bo_fini(struct ttm_buffer_object *bo)
  279. {
  280. ttm_bo_put(bo);
  281. }
  282. EXPORT_SYMBOL(ttm_bo_fini);
  283. static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
  284. struct ttm_operation_ctx *ctx,
  285. struct ttm_place *hop)
  286. {
  287. struct ttm_placement hop_placement;
  288. struct ttm_resource *hop_mem;
  289. int ret;
  290. hop_placement.num_placement = 1;
  291. hop_placement.placement = hop;
  292. /* find space in the bounce domain */
  293. ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
  294. if (ret)
  295. return ret;
  296. /* move to the bounce domain */
  297. ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
  298. if (ret) {
  299. ttm_resource_free(bo, &hop_mem);
  300. return ret;
  301. }
  302. return 0;
  303. }
  304. static int ttm_bo_evict(struct ttm_buffer_object *bo,
  305. struct ttm_operation_ctx *ctx)
  306. {
  307. struct ttm_resource *evict_mem;
  308. struct ttm_placement placement;
  309. struct ttm_place hop;
  310. int ret = 0;
  311. memset(&hop, 0, sizeof(hop));
  312. dma_resv_assert_held(bo->base.resv);
  313. placement.num_placement = 0;
  314. bo->bdev->funcs->evict_flags(bo, &placement);
  315. if (!placement.num_placement) {
  316. ret = ttm_bo_wait_ctx(bo, ctx);
  317. if (ret)
  318. return ret;
  319. /*
  320. * Since we've already synced, this frees backing store
  321. * immediately.
  322. */
  323. return ttm_bo_pipeline_gutting(bo);
  324. }
  325. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
  326. if (ret) {
  327. if (ret != -ERESTARTSYS) {
  328. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  329. bo);
  330. ttm_bo_mem_space_debug(bo, &placement);
  331. }
  332. goto out;
  333. }
  334. do {
  335. ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
  336. if (ret != -EMULTIHOP)
  337. break;
  338. ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
  339. } while (!ret);
  340. if (ret) {
  341. ttm_resource_free(bo, &evict_mem);
  342. if (ret != -ERESTARTSYS && ret != -EINTR)
  343. pr_err("Buffer eviction failed\n");
  344. }
  345. out:
  346. return ret;
  347. }
  348. /**
  349. * ttm_bo_eviction_valuable
  350. *
  351. * @bo: The buffer object to evict
  352. * @place: the placement we need to make room for
  353. *
  354. * Check if it is valuable to evict the BO to make room for the given placement.
  355. */
  356. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  357. const struct ttm_place *place)
  358. {
  359. struct ttm_resource *res = bo->resource;
  360. dma_resv_assert_held(bo->base.resv);
  361. if (res->mem_type == TTM_PL_SYSTEM)
  362. return true;
  363. /* Don't evict this BO if it's outside of the
  364. * requested placement range
  365. */
  366. return ttm_resource_intersects(bo->bdev, res, place, bo->base.size);
  367. }
  368. EXPORT_SYMBOL(ttm_bo_eviction_valuable);
  369. /**
  370. * ttm_bo_evict_first() - Evict the first bo on the manager's LRU list.
  371. * @bdev: The ttm device.
  372. * @man: The manager whose bo to evict.
  373. * @ctx: The TTM operation ctx governing the eviction.
  374. *
  375. * Return: 0 if successful or the resource disappeared. Negative error code on error.
  376. */
  377. int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man,
  378. struct ttm_operation_ctx *ctx)
  379. {
  380. struct ttm_resource_cursor cursor;
  381. struct ttm_buffer_object *bo;
  382. struct ttm_resource *res;
  383. unsigned int mem_type;
  384. int ret = 0;
  385. spin_lock(&bdev->lru_lock);
  386. ttm_resource_cursor_init(&cursor, man);
  387. res = ttm_resource_manager_first(&cursor);
  388. ttm_resource_cursor_fini(&cursor);
  389. if (!res) {
  390. ret = -ENOENT;
  391. goto out_no_ref;
  392. }
  393. bo = res->bo;
  394. if (!ttm_bo_get_unless_zero(bo))
  395. goto out_no_ref;
  396. mem_type = res->mem_type;
  397. spin_unlock(&bdev->lru_lock);
  398. ret = ttm_bo_reserve(bo, ctx->interruptible, ctx->no_wait_gpu, NULL);
  399. if (ret)
  400. goto out_no_lock;
  401. if (!bo->resource || bo->resource->mem_type != mem_type)
  402. goto out_bo_moved;
  403. if (bo->deleted) {
  404. ret = ttm_bo_wait_ctx(bo, ctx);
  405. if (!ret)
  406. ttm_bo_cleanup_memtype_use(bo);
  407. } else {
  408. ret = ttm_bo_evict(bo, ctx);
  409. }
  410. out_bo_moved:
  411. dma_resv_unlock(bo->base.resv);
  412. out_no_lock:
  413. ttm_bo_put(bo);
  414. return ret;
  415. out_no_ref:
  416. spin_unlock(&bdev->lru_lock);
  417. return ret;
  418. }
  419. /**
  420. * struct ttm_bo_evict_walk - Parameters for the evict walk.
  421. */
  422. struct ttm_bo_evict_walk {
  423. /** @walk: The walk base parameters. */
  424. struct ttm_lru_walk walk;
  425. /** @place: The place passed to the resource allocation. */
  426. const struct ttm_place *place;
  427. /** @evictor: The buffer object we're trying to make room for. */
  428. struct ttm_buffer_object *evictor;
  429. /** @res: The allocated resource if any. */
  430. struct ttm_resource **res;
  431. /** @evicted: Number of successful evictions. */
  432. unsigned long evicted;
  433. /** @limit_pool: Which pool limit we should test against */
  434. struct dmem_cgroup_pool_state *limit_pool;
  435. /** @try_low: Whether we should attempt to evict BO's with low watermark threshold */
  436. bool try_low;
  437. /** @hit_low: If we cannot evict a bo when @try_low is false (first pass) */
  438. bool hit_low;
  439. };
  440. static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
  441. {
  442. struct ttm_bo_evict_walk *evict_walk =
  443. container_of(walk, typeof(*evict_walk), walk);
  444. s64 lret;
  445. if (!dmem_cgroup_state_evict_valuable(evict_walk->limit_pool, bo->resource->css,
  446. evict_walk->try_low, &evict_walk->hit_low))
  447. return 0;
  448. if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->place))
  449. return 0;
  450. if (bo->deleted) {
  451. lret = ttm_bo_wait_ctx(bo, walk->arg.ctx);
  452. if (!lret)
  453. ttm_bo_cleanup_memtype_use(bo);
  454. } else {
  455. lret = ttm_bo_evict(bo, walk->arg.ctx);
  456. }
  457. if (lret)
  458. goto out;
  459. evict_walk->evicted++;
  460. if (evict_walk->res)
  461. lret = ttm_resource_alloc(evict_walk->evictor, evict_walk->place,
  462. evict_walk->res, NULL);
  463. if (lret == 0)
  464. return 1;
  465. out:
  466. /* Errors that should terminate the walk. */
  467. if (lret == -ENOSPC)
  468. return -EBUSY;
  469. return lret;
  470. }
  471. static const struct ttm_lru_walk_ops ttm_evict_walk_ops = {
  472. .process_bo = ttm_bo_evict_cb,
  473. };
  474. static int ttm_bo_evict_alloc(struct ttm_device *bdev,
  475. struct ttm_resource_manager *man,
  476. const struct ttm_place *place,
  477. struct ttm_buffer_object *evictor,
  478. struct ttm_operation_ctx *ctx,
  479. struct ww_acquire_ctx *ticket,
  480. struct ttm_resource **res,
  481. struct dmem_cgroup_pool_state *limit_pool)
  482. {
  483. struct ttm_bo_evict_walk evict_walk = {
  484. .walk = {
  485. .ops = &ttm_evict_walk_ops,
  486. .arg = {
  487. .ctx = ctx,
  488. .ticket = ticket,
  489. }
  490. },
  491. .place = place,
  492. .evictor = evictor,
  493. .res = res,
  494. .limit_pool = limit_pool,
  495. };
  496. s64 lret;
  497. evict_walk.walk.arg.trylock_only = true;
  498. lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
  499. /* One more attempt if we hit low limit? */
  500. if (!lret && evict_walk.hit_low) {
  501. evict_walk.try_low = true;
  502. lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
  503. }
  504. if (lret || !ticket)
  505. goto out;
  506. /* Reset low limit */
  507. evict_walk.try_low = evict_walk.hit_low = false;
  508. /* If ticket-locking, repeat while making progress. */
  509. evict_walk.walk.arg.trylock_only = false;
  510. retry:
  511. do {
  512. /* The walk may clear the evict_walk.walk.ticket field */
  513. evict_walk.walk.arg.ticket = ticket;
  514. evict_walk.evicted = 0;
  515. lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
  516. } while (!lret && evict_walk.evicted);
  517. /* We hit the low limit? Try once more */
  518. if (!lret && evict_walk.hit_low && !evict_walk.try_low) {
  519. evict_walk.try_low = true;
  520. goto retry;
  521. }
  522. out:
  523. if (lret < 0)
  524. return lret;
  525. if (lret == 0)
  526. return -EBUSY;
  527. return 0;
  528. }
  529. /**
  530. * ttm_bo_pin - Pin the buffer object.
  531. * @bo: The buffer object to pin
  532. *
  533. * Make sure the buffer is not evicted any more during memory pressure.
  534. * @bo must be unpinned again by calling ttm_bo_unpin().
  535. */
  536. void ttm_bo_pin(struct ttm_buffer_object *bo)
  537. {
  538. dma_resv_assert_held(bo->base.resv);
  539. WARN_ON_ONCE(!kref_read(&bo->kref));
  540. spin_lock(&bo->bdev->lru_lock);
  541. if (bo->resource)
  542. ttm_resource_del_bulk_move(bo->resource, bo);
  543. if (!bo->pin_count++ && bo->resource)
  544. ttm_resource_move_to_lru_tail(bo->resource);
  545. spin_unlock(&bo->bdev->lru_lock);
  546. }
  547. EXPORT_SYMBOL(ttm_bo_pin);
  548. /**
  549. * ttm_bo_unpin - Unpin the buffer object.
  550. * @bo: The buffer object to unpin
  551. *
  552. * Allows the buffer object to be evicted again during memory pressure.
  553. */
  554. void ttm_bo_unpin(struct ttm_buffer_object *bo)
  555. {
  556. dma_resv_assert_held(bo->base.resv);
  557. WARN_ON_ONCE(!kref_read(&bo->kref));
  558. if (WARN_ON_ONCE(!bo->pin_count))
  559. return;
  560. spin_lock(&bo->bdev->lru_lock);
  561. if (!--bo->pin_count && bo->resource) {
  562. ttm_resource_add_bulk_move(bo->resource, bo);
  563. ttm_resource_move_to_lru_tail(bo->resource);
  564. }
  565. spin_unlock(&bo->bdev->lru_lock);
  566. }
  567. EXPORT_SYMBOL(ttm_bo_unpin);
  568. /*
  569. * Add the pipelined eviction fencesto the BO as kernel dependency and reserve new
  570. * fence slots.
  571. */
  572. static int ttm_bo_add_pipelined_eviction_fences(struct ttm_buffer_object *bo,
  573. struct ttm_resource_manager *man,
  574. bool no_wait_gpu)
  575. {
  576. struct dma_fence *fence;
  577. int i;
  578. spin_lock(&man->eviction_lock);
  579. for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) {
  580. fence = man->eviction_fences[i];
  581. if (!fence)
  582. continue;
  583. if (no_wait_gpu) {
  584. if (!dma_fence_is_signaled(fence)) {
  585. spin_unlock(&man->eviction_lock);
  586. return -EBUSY;
  587. }
  588. } else {
  589. dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
  590. }
  591. }
  592. spin_unlock(&man->eviction_lock);
  593. /* TODO: this call should be removed. */
  594. return dma_resv_reserve_fences(bo->base.resv, 1);
  595. }
  596. /**
  597. * ttm_bo_alloc_resource - Allocate backing store for a BO
  598. *
  599. * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
  600. * @placement: Proposed new placement for the buffer object
  601. * @ctx: if and how to sleep, lock buffers and alloc memory
  602. * @force_space: If we should evict buffers to force space
  603. * @res: The resulting struct ttm_resource.
  604. *
  605. * Allocates a resource for the buffer object pointed to by @bo, using the
  606. * placement flags in @placement, potentially evicting other buffer objects when
  607. * @force_space is true.
  608. * This function may sleep while waiting for resources to become available.
  609. * Returns:
  610. * -EBUSY: No space available (only if no_wait == true).
  611. * -ENOSPC: Could not allocate space for the buffer object, either due to
  612. * fragmentation or concurrent allocators.
  613. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  614. */
  615. static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo,
  616. struct ttm_placement *placement,
  617. struct ttm_operation_ctx *ctx,
  618. bool force_space,
  619. struct ttm_resource **res)
  620. {
  621. struct ttm_device *bdev = bo->bdev;
  622. struct ww_acquire_ctx *ticket;
  623. int i, ret;
  624. ticket = dma_resv_locking_ctx(bo->base.resv);
  625. ret = dma_resv_reserve_fences(bo->base.resv, TTM_NUM_MOVE_FENCES);
  626. if (unlikely(ret))
  627. return ret;
  628. for (i = 0; i < placement->num_placement; ++i) {
  629. const struct ttm_place *place = &placement->placement[i];
  630. struct dmem_cgroup_pool_state *limit_pool = NULL;
  631. struct ttm_resource_manager *man;
  632. bool may_evict;
  633. man = ttm_manager_type(bdev, place->mem_type);
  634. if (!man || !ttm_resource_manager_used(man))
  635. continue;
  636. if (place->flags & (force_space ? TTM_PL_FLAG_DESIRED :
  637. TTM_PL_FLAG_FALLBACK))
  638. continue;
  639. may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM);
  640. ret = ttm_resource_alloc(bo, place, res, force_space ? &limit_pool : NULL);
  641. if (ret) {
  642. if (ret != -ENOSPC && ret != -EAGAIN) {
  643. dmem_cgroup_pool_state_put(limit_pool);
  644. return ret;
  645. }
  646. if (!may_evict) {
  647. dmem_cgroup_pool_state_put(limit_pool);
  648. continue;
  649. }
  650. ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx,
  651. ticket, res, limit_pool);
  652. dmem_cgroup_pool_state_put(limit_pool);
  653. if (ret == -EBUSY)
  654. continue;
  655. if (ret)
  656. return ret;
  657. }
  658. ret = ttm_bo_add_pipelined_eviction_fences(bo, man, ctx->no_wait_gpu);
  659. if (unlikely(ret)) {
  660. ttm_resource_free(bo, res);
  661. if (ret == -EBUSY)
  662. continue;
  663. return ret;
  664. }
  665. return 0;
  666. }
  667. return -ENOSPC;
  668. }
  669. /*
  670. * ttm_bo_mem_space - Wrapper around ttm_bo_alloc_resource
  671. *
  672. * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
  673. * @placement: Proposed new placement for the buffer object
  674. * @res: The resulting struct ttm_resource.
  675. * @ctx: if and how to sleep, lock buffers and alloc memory
  676. *
  677. * Tries both idle allocation and forcefully eviction of buffers. See
  678. * ttm_bo_alloc_resource for details.
  679. */
  680. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  681. struct ttm_placement *placement,
  682. struct ttm_resource **res,
  683. struct ttm_operation_ctx *ctx)
  684. {
  685. bool force_space = false;
  686. int ret;
  687. do {
  688. ret = ttm_bo_alloc_resource(bo, placement, ctx,
  689. force_space, res);
  690. force_space = !force_space;
  691. } while (ret == -ENOSPC && force_space);
  692. return ret;
  693. }
  694. EXPORT_SYMBOL(ttm_bo_mem_space);
  695. /**
  696. * ttm_bo_validate
  697. *
  698. * @bo: The buffer object.
  699. * @placement: Proposed placement for the buffer object.
  700. * @ctx: validation parameters.
  701. *
  702. * Changes placement and caching policy of the buffer object
  703. * according proposed placement.
  704. * Returns
  705. * -EINVAL on invalid proposed placement.
  706. * -ENOMEM on out-of-memory condition.
  707. * -EBUSY if no_wait is true and buffer busy.
  708. * -ERESTARTSYS if interrupted by a signal.
  709. */
  710. int ttm_bo_validate(struct ttm_buffer_object *bo,
  711. struct ttm_placement *placement,
  712. struct ttm_operation_ctx *ctx)
  713. {
  714. struct ttm_resource *res;
  715. struct ttm_place hop;
  716. bool force_space;
  717. int ret;
  718. dma_resv_assert_held(bo->base.resv);
  719. /*
  720. * Remove the backing store if no placement is given.
  721. */
  722. if (!placement->num_placement)
  723. return ttm_bo_pipeline_gutting(bo);
  724. force_space = false;
  725. do {
  726. /* Check whether we need to move buffer. */
  727. if (bo->resource &&
  728. ttm_resource_compatible(bo->resource, placement,
  729. force_space))
  730. return 0;
  731. /* Moving of pinned BOs is forbidden */
  732. if (bo->pin_count)
  733. return -EINVAL;
  734. /*
  735. * Determine where to move the buffer.
  736. *
  737. * If driver determines move is going to need
  738. * an extra step then it will return -EMULTIHOP
  739. * and the buffer will be moved to the temporary
  740. * stop and the driver will be called to make
  741. * the second hop.
  742. */
  743. ret = ttm_bo_alloc_resource(bo, placement, ctx, force_space,
  744. &res);
  745. force_space = !force_space;
  746. if (ret == -ENOSPC)
  747. continue;
  748. if (ret)
  749. return ret;
  750. bounce:
  751. ret = ttm_bo_handle_move_mem(bo, res, false, ctx, &hop);
  752. if (ret == -EMULTIHOP) {
  753. ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
  754. /* try and move to final place now. */
  755. if (!ret)
  756. goto bounce;
  757. }
  758. if (ret) {
  759. ttm_resource_free(bo, &res);
  760. return ret;
  761. }
  762. } while (ret && force_space);
  763. /* For backward compatibility with userspace */
  764. if (ret == -ENOSPC)
  765. return bo->bdev->alloc_flags & TTM_ALLOCATION_PROPAGATE_ENOSPC ?
  766. ret : -ENOMEM;
  767. /*
  768. * We might need to add a TTM.
  769. */
  770. if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
  771. ret = ttm_tt_create(bo, true);
  772. if (ret)
  773. return ret;
  774. }
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(ttm_bo_validate);
  778. /**
  779. * ttm_bo_init_reserved
  780. *
  781. * @bdev: Pointer to a ttm_device struct.
  782. * @bo: Pointer to a ttm_buffer_object to be initialized.
  783. * @type: Requested type of buffer object.
  784. * @placement: Initial placement for buffer object.
  785. * @alignment: Data alignment in pages.
  786. * @ctx: TTM operation context for memory allocation.
  787. * @sg: Scatter-gather table.
  788. * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
  789. * @destroy: Destroy function. Use NULL for kfree().
  790. *
  791. * This function initializes a pre-allocated struct ttm_buffer_object.
  792. * As this object may be part of a larger structure, this function,
  793. * together with the @destroy function, enables driver-specific objects
  794. * derived from a ttm_buffer_object.
  795. *
  796. * On successful return, the caller owns an object kref to @bo. The kref and
  797. * list_kref are usually set to 1, but note that in some situations, other
  798. * tasks may already be holding references to @bo as well.
  799. * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
  800. * and it is the caller's responsibility to call ttm_bo_unreserve.
  801. *
  802. * If a failure occurs, the function will call the @destroy function. Thus,
  803. * after a failure, dereferencing @bo is illegal and will likely cause memory
  804. * corruption.
  805. *
  806. * Returns
  807. * -ENOMEM: Out of memory.
  808. * -EINVAL: Invalid placement flags.
  809. * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
  810. */
  811. int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
  812. enum ttm_bo_type type, struct ttm_placement *placement,
  813. uint32_t alignment, struct ttm_operation_ctx *ctx,
  814. struct sg_table *sg, struct dma_resv *resv,
  815. void (*destroy) (struct ttm_buffer_object *))
  816. {
  817. int ret;
  818. kref_init(&bo->kref);
  819. bo->bdev = bdev;
  820. bo->type = type;
  821. bo->page_alignment = alignment;
  822. bo->destroy = destroy;
  823. bo->pin_count = 0;
  824. bo->sg = sg;
  825. bo->bulk_move = NULL;
  826. if (resv)
  827. bo->base.resv = resv;
  828. else
  829. bo->base.resv = &bo->base._resv;
  830. atomic_inc(&ttm_glob.bo_count);
  831. /*
  832. * For ttm_bo_type_device buffers, allocate
  833. * address space from the device.
  834. */
  835. if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
  836. ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
  837. PFN_UP(bo->base.size));
  838. if (ret)
  839. goto err_put;
  840. }
  841. /* passed reservation objects should already be locked,
  842. * since otherwise lockdep will be angered in radeon.
  843. */
  844. if (!resv)
  845. WARN_ON(!dma_resv_trylock(bo->base.resv));
  846. else
  847. dma_resv_assert_held(resv);
  848. ret = ttm_bo_validate(bo, placement, ctx);
  849. if (unlikely(ret))
  850. goto err_unlock;
  851. return 0;
  852. err_unlock:
  853. if (!resv)
  854. dma_resv_unlock(bo->base.resv);
  855. err_put:
  856. ttm_bo_put(bo);
  857. return ret;
  858. }
  859. EXPORT_SYMBOL(ttm_bo_init_reserved);
  860. /**
  861. * ttm_bo_init_validate
  862. *
  863. * @bdev: Pointer to a ttm_device struct.
  864. * @bo: Pointer to a ttm_buffer_object to be initialized.
  865. * @type: Requested type of buffer object.
  866. * @placement: Initial placement for buffer object.
  867. * @alignment: Data alignment in pages.
  868. * @interruptible: If needing to sleep to wait for GPU resources,
  869. * sleep interruptible.
  870. * pinned in physical memory. If this behaviour is not desired, this member
  871. * holds a pointer to a persistent shmem object. Typically, this would
  872. * point to the shmem object backing a GEM object if TTM is used to back a
  873. * GEM user interface.
  874. * @sg: Scatter-gather table.
  875. * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
  876. * @destroy: Destroy function. Use NULL for kfree().
  877. *
  878. * This function initializes a pre-allocated struct ttm_buffer_object.
  879. * As this object may be part of a larger structure, this function,
  880. * together with the @destroy function,
  881. * enables driver-specific objects derived from a ttm_buffer_object.
  882. *
  883. * On successful return, the caller owns an object kref to @bo. The kref and
  884. * list_kref are usually set to 1, but note that in some situations, other
  885. * tasks may already be holding references to @bo as well.
  886. *
  887. * If a failure occurs, the function will call the @destroy function, Thus,
  888. * after a failure, dereferencing @bo is illegal and will likely cause memory
  889. * corruption.
  890. *
  891. * Returns
  892. * -ENOMEM: Out of memory.
  893. * -EINVAL: Invalid placement flags.
  894. * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
  895. */
  896. int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
  897. enum ttm_bo_type type, struct ttm_placement *placement,
  898. uint32_t alignment, bool interruptible,
  899. struct sg_table *sg, struct dma_resv *resv,
  900. void (*destroy) (struct ttm_buffer_object *))
  901. {
  902. struct ttm_operation_ctx ctx = { .interruptible = interruptible };
  903. int ret;
  904. ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
  905. sg, resv, destroy);
  906. if (ret)
  907. return ret;
  908. if (!resv)
  909. ttm_bo_unreserve(bo);
  910. return 0;
  911. }
  912. EXPORT_SYMBOL(ttm_bo_init_validate);
  913. /*
  914. * buffer object vm functions.
  915. */
  916. /**
  917. * ttm_bo_unmap_virtual
  918. *
  919. * @bo: tear down the virtual mappings for this BO
  920. */
  921. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  922. {
  923. struct ttm_device *bdev = bo->bdev;
  924. drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
  925. ttm_mem_io_free(bdev, bo->resource);
  926. }
  927. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  928. /**
  929. * ttm_bo_wait_ctx - wait for buffer idle.
  930. *
  931. * @bo: The buffer object.
  932. * @ctx: defines how to wait
  933. *
  934. * Waits for the buffer to be idle. Used timeout depends on the context.
  935. * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
  936. * zero on success.
  937. */
  938. int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
  939. {
  940. long ret;
  941. if (ctx->no_wait_gpu) {
  942. if (dma_resv_test_signaled(bo->base.resv,
  943. DMA_RESV_USAGE_BOOKKEEP))
  944. return 0;
  945. else
  946. return -EBUSY;
  947. }
  948. ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
  949. ctx->interruptible, 15 * HZ);
  950. if (unlikely(ret < 0))
  951. return ret;
  952. if (unlikely(ret == 0))
  953. return -EBUSY;
  954. return 0;
  955. }
  956. EXPORT_SYMBOL(ttm_bo_wait_ctx);
  957. /**
  958. * struct ttm_bo_swapout_walk - Parameters for the swapout walk
  959. */
  960. struct ttm_bo_swapout_walk {
  961. /** @walk: The walk base parameters. */
  962. struct ttm_lru_walk walk;
  963. /** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */
  964. gfp_t gfp_flags;
  965. /** @hit_low: Whether we should attempt to swap BO's with low watermark threshold */
  966. /** @evict_low: If we cannot swap a bo when @try_low is false (first pass) */
  967. bool hit_low, evict_low;
  968. };
  969. static s64
  970. ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
  971. {
  972. struct ttm_place place = { .mem_type = bo->resource->mem_type };
  973. struct ttm_bo_swapout_walk *swapout_walk =
  974. container_of(walk, typeof(*swapout_walk), walk);
  975. struct ttm_operation_ctx *ctx = walk->arg.ctx;
  976. struct ttm_device *bdev = bo->bdev;
  977. struct ttm_tt *tt = bo->ttm;
  978. s64 ret;
  979. /*
  980. * While the bo may already reside in SYSTEM placement, set
  981. * SYSTEM as new placement to cover also the move further below.
  982. * The driver may use the fact that we're moving from SYSTEM
  983. * as an indication that we're about to swap out.
  984. */
  985. if (bo->pin_count || !bdev->funcs->eviction_valuable(bo, &place)) {
  986. ret = -EBUSY;
  987. goto out;
  988. }
  989. if (!tt || !ttm_tt_is_populated(tt) ||
  990. tt->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_SWAPPED)) {
  991. ret = -EBUSY;
  992. goto out;
  993. }
  994. if (bo->deleted) {
  995. pgoff_t num_pages = tt->num_pages;
  996. ret = ttm_bo_wait_ctx(bo, ctx);
  997. if (ret)
  998. goto out;
  999. ttm_bo_cleanup_memtype_use(bo);
  1000. ret = num_pages;
  1001. goto out;
  1002. }
  1003. /*
  1004. * Move to system cached
  1005. */
  1006. if (bo->resource->mem_type != TTM_PL_SYSTEM) {
  1007. struct ttm_resource *evict_mem;
  1008. struct ttm_place hop;
  1009. memset(&hop, 0, sizeof(hop));
  1010. place.mem_type = TTM_PL_SYSTEM;
  1011. ret = ttm_resource_alloc(bo, &place, &evict_mem, NULL);
  1012. if (ret)
  1013. goto out;
  1014. ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
  1015. if (ret) {
  1016. WARN(ret == -EMULTIHOP,
  1017. "Unexpected multihop in swapout - likely driver bug.\n");
  1018. ttm_resource_free(bo, &evict_mem);
  1019. goto out;
  1020. }
  1021. }
  1022. /*
  1023. * Make sure BO is idle.
  1024. */
  1025. ret = ttm_bo_wait_ctx(bo, ctx);
  1026. if (ret)
  1027. goto out;
  1028. ttm_bo_unmap_virtual(bo);
  1029. if (bdev->funcs->swap_notify)
  1030. bdev->funcs->swap_notify(bo);
  1031. if (ttm_tt_is_populated(tt)) {
  1032. spin_lock(&bdev->lru_lock);
  1033. ttm_resource_del_bulk_move(bo->resource, bo);
  1034. spin_unlock(&bdev->lru_lock);
  1035. ret = ttm_tt_swapout(bdev, tt, swapout_walk->gfp_flags);
  1036. spin_lock(&bdev->lru_lock);
  1037. if (ret)
  1038. ttm_resource_add_bulk_move(bo->resource, bo);
  1039. ttm_resource_move_to_lru_tail(bo->resource);
  1040. spin_unlock(&bdev->lru_lock);
  1041. }
  1042. out:
  1043. /* Consider -ENOMEM and -ENOSPC non-fatal. */
  1044. if (ret == -ENOMEM || ret == -ENOSPC)
  1045. ret = -EBUSY;
  1046. return ret;
  1047. }
  1048. const struct ttm_lru_walk_ops ttm_swap_ops = {
  1049. .process_bo = ttm_bo_swapout_cb,
  1050. };
  1051. /**
  1052. * ttm_bo_swapout() - Swap out buffer objects on the LRU list to shmem.
  1053. * @bdev: The ttm device.
  1054. * @ctx: The ttm_operation_ctx governing the swapout operation.
  1055. * @man: The resource manager whose resources / buffer objects are
  1056. * goint to be swapped out.
  1057. * @gfp_flags: The gfp flags used for shmem page allocations.
  1058. * @target: The desired number of bytes to swap out.
  1059. *
  1060. * Return: The number of bytes actually swapped out, or negative error code
  1061. * on error.
  1062. */
  1063. s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  1064. struct ttm_resource_manager *man, gfp_t gfp_flags,
  1065. s64 target)
  1066. {
  1067. struct ttm_bo_swapout_walk swapout_walk = {
  1068. .walk = {
  1069. .ops = &ttm_swap_ops,
  1070. .arg = {
  1071. .ctx = ctx,
  1072. .trylock_only = true,
  1073. },
  1074. },
  1075. .gfp_flags = gfp_flags,
  1076. };
  1077. return ttm_lru_walk_for_evict(&swapout_walk.walk, bdev, man, target);
  1078. }
  1079. void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
  1080. {
  1081. if (bo->ttm == NULL)
  1082. return;
  1083. ttm_tt_unpopulate(bo->bdev, bo->ttm);
  1084. ttm_tt_destroy(bo->bdev, bo->ttm);
  1085. bo->ttm = NULL;
  1086. }
  1087. /**
  1088. * ttm_bo_populate() - Ensure that a buffer object has backing pages
  1089. * @bo: The buffer object
  1090. * @ctx: The ttm_operation_ctx governing the operation.
  1091. *
  1092. * For buffer objects in a memory type whose manager uses
  1093. * struct ttm_tt for backing pages, ensure those backing pages
  1094. * are present and with valid content. The bo's resource is also
  1095. * placed on the correct LRU list if it was previously swapped
  1096. * out.
  1097. *
  1098. * Return: 0 if successful, negative error code on failure.
  1099. * Note: May return -EINTR or -ERESTARTSYS if @ctx::interruptible
  1100. * is set to true.
  1101. */
  1102. int ttm_bo_populate(struct ttm_buffer_object *bo,
  1103. struct ttm_operation_ctx *ctx)
  1104. {
  1105. struct ttm_device *bdev = bo->bdev;
  1106. struct ttm_tt *tt = bo->ttm;
  1107. bool swapped;
  1108. int ret;
  1109. dma_resv_assert_held(bo->base.resv);
  1110. if (!tt)
  1111. return 0;
  1112. swapped = ttm_tt_is_swapped(tt);
  1113. ret = ttm_tt_populate(bdev, tt, ctx);
  1114. if (ret)
  1115. return ret;
  1116. if (swapped && !ttm_tt_is_swapped(tt) && !bo->pin_count &&
  1117. bo->resource) {
  1118. spin_lock(&bdev->lru_lock);
  1119. ttm_resource_add_bulk_move(bo->resource, bo);
  1120. ttm_resource_move_to_lru_tail(bo->resource);
  1121. spin_unlock(&bdev->lru_lock);
  1122. }
  1123. return 0;
  1124. }
  1125. EXPORT_SYMBOL(ttm_bo_populate);
  1126. int ttm_bo_setup_export(struct ttm_buffer_object *bo,
  1127. struct ttm_operation_ctx *ctx)
  1128. {
  1129. int ret;
  1130. ret = ttm_bo_reserve(bo, false, false, NULL);
  1131. if (ret != 0)
  1132. return ret;
  1133. ret = ttm_bo_populate(bo, ctx);
  1134. ttm_bo_unreserve(bo);
  1135. return ret;
  1136. }
  1137. EXPORT_SYMBOL(ttm_bo_setup_export);