i915_gem_evict.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "../i915_selftest.h"
  25. #include "lib_sw_fence.h"
  26. #include "mock_context.h"
  27. #include "mock_drm.h"
  28. #include "mock_gem_device.h"
  29. static int populate_ggtt(struct drm_i915_private *i915)
  30. {
  31. struct drm_i915_gem_object *obj;
  32. u64 size;
  33. for (size = 0;
  34. size + I915_GTT_PAGE_SIZE <= i915->ggtt.base.total;
  35. size += I915_GTT_PAGE_SIZE) {
  36. struct i915_vma *vma;
  37. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  38. if (IS_ERR(obj))
  39. return PTR_ERR(obj);
  40. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  41. if (IS_ERR(vma))
  42. return PTR_ERR(vma);
  43. }
  44. if (!list_empty(&i915->mm.unbound_list)) {
  45. size = 0;
  46. list_for_each_entry(obj, &i915->mm.unbound_list, mm.link)
  47. size++;
  48. pr_err("Found %lld objects unbound!\n", size);
  49. return -EINVAL;
  50. }
  51. if (list_empty(&i915->ggtt.base.inactive_list)) {
  52. pr_err("No objects on the GGTT inactive list!\n");
  53. return -EINVAL;
  54. }
  55. return 0;
  56. }
  57. static void unpin_ggtt(struct drm_i915_private *i915)
  58. {
  59. struct i915_vma *vma;
  60. list_for_each_entry(vma, &i915->ggtt.base.inactive_list, vm_link)
  61. i915_vma_unpin(vma);
  62. }
  63. static void cleanup_objects(struct drm_i915_private *i915)
  64. {
  65. struct drm_i915_gem_object *obj, *on;
  66. list_for_each_entry_safe(obj, on, &i915->mm.unbound_list, mm.link)
  67. i915_gem_object_put(obj);
  68. list_for_each_entry_safe(obj, on, &i915->mm.bound_list, mm.link)
  69. i915_gem_object_put(obj);
  70. mutex_unlock(&i915->drm.struct_mutex);
  71. i915_gem_drain_freed_objects(i915);
  72. mutex_lock(&i915->drm.struct_mutex);
  73. }
  74. static int igt_evict_something(void *arg)
  75. {
  76. struct drm_i915_private *i915 = arg;
  77. struct i915_ggtt *ggtt = &i915->ggtt;
  78. int err;
  79. /* Fill the GGTT with pinned objects and try to evict one. */
  80. err = populate_ggtt(i915);
  81. if (err)
  82. goto cleanup;
  83. /* Everything is pinned, nothing should happen */
  84. err = i915_gem_evict_something(&ggtt->base,
  85. I915_GTT_PAGE_SIZE, 0, 0,
  86. 0, U64_MAX,
  87. 0);
  88. if (err != -ENOSPC) {
  89. pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
  90. err);
  91. goto cleanup;
  92. }
  93. unpin_ggtt(i915);
  94. /* Everything is unpinned, we should be able to evict something */
  95. err = i915_gem_evict_something(&ggtt->base,
  96. I915_GTT_PAGE_SIZE, 0, 0,
  97. 0, U64_MAX,
  98. 0);
  99. if (err) {
  100. pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
  101. err);
  102. goto cleanup;
  103. }
  104. cleanup:
  105. cleanup_objects(i915);
  106. return err;
  107. }
  108. static int igt_overcommit(void *arg)
  109. {
  110. struct drm_i915_private *i915 = arg;
  111. struct drm_i915_gem_object *obj;
  112. struct i915_vma *vma;
  113. int err;
  114. /* Fill the GGTT with pinned objects and then try to pin one more.
  115. * We expect it to fail.
  116. */
  117. err = populate_ggtt(i915);
  118. if (err)
  119. goto cleanup;
  120. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  121. if (IS_ERR(obj)) {
  122. err = PTR_ERR(obj);
  123. goto cleanup;
  124. }
  125. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  126. if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
  127. pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
  128. err = -EINVAL;
  129. goto cleanup;
  130. }
  131. cleanup:
  132. cleanup_objects(i915);
  133. return err;
  134. }
  135. static int igt_evict_for_vma(void *arg)
  136. {
  137. struct drm_i915_private *i915 = arg;
  138. struct i915_ggtt *ggtt = &i915->ggtt;
  139. struct drm_mm_node target = {
  140. .start = 0,
  141. .size = 4096,
  142. };
  143. int err;
  144. /* Fill the GGTT with pinned objects and try to evict a range. */
  145. err = populate_ggtt(i915);
  146. if (err)
  147. goto cleanup;
  148. /* Everything is pinned, nothing should happen */
  149. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  150. if (err != -ENOSPC) {
  151. pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
  152. err);
  153. goto cleanup;
  154. }
  155. unpin_ggtt(i915);
  156. /* Everything is unpinned, we should be able to evict the node */
  157. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  158. if (err) {
  159. pr_err("i915_gem_evict_for_node returned err=%d\n",
  160. err);
  161. goto cleanup;
  162. }
  163. cleanup:
  164. cleanup_objects(i915);
  165. return err;
  166. }
  167. static void mock_color_adjust(const struct drm_mm_node *node,
  168. unsigned long color,
  169. u64 *start,
  170. u64 *end)
  171. {
  172. }
  173. static int igt_evict_for_cache_color(void *arg)
  174. {
  175. struct drm_i915_private *i915 = arg;
  176. struct i915_ggtt *ggtt = &i915->ggtt;
  177. const unsigned long flags = PIN_OFFSET_FIXED;
  178. struct drm_mm_node target = {
  179. .start = I915_GTT_PAGE_SIZE * 2,
  180. .size = I915_GTT_PAGE_SIZE,
  181. .color = I915_CACHE_LLC,
  182. };
  183. struct drm_i915_gem_object *obj;
  184. struct i915_vma *vma;
  185. int err;
  186. /* Currently the use of color_adjust is limited to cache domains within
  187. * the ggtt, and so the presence of mm.color_adjust is assumed to be
  188. * i915_gtt_color_adjust throughout our driver, so using a mock color
  189. * adjust will work just fine for our purposes.
  190. */
  191. ggtt->base.mm.color_adjust = mock_color_adjust;
  192. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  193. if (IS_ERR(obj)) {
  194. err = PTR_ERR(obj);
  195. goto cleanup;
  196. }
  197. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  198. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  199. I915_GTT_PAGE_SIZE | flags);
  200. if (IS_ERR(vma)) {
  201. pr_err("[0]i915_gem_object_ggtt_pin failed\n");
  202. err = PTR_ERR(vma);
  203. goto cleanup;
  204. }
  205. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  206. if (IS_ERR(obj)) {
  207. err = PTR_ERR(obj);
  208. goto cleanup;
  209. }
  210. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  211. /* Neighbouring; same colour - should fit */
  212. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  213. (I915_GTT_PAGE_SIZE * 2) | flags);
  214. if (IS_ERR(vma)) {
  215. pr_err("[1]i915_gem_object_ggtt_pin failed\n");
  216. err = PTR_ERR(vma);
  217. goto cleanup;
  218. }
  219. i915_vma_unpin(vma);
  220. /* Remove just the second vma */
  221. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  222. if (err) {
  223. pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
  224. goto cleanup;
  225. }
  226. /* Attempt to remove the first *pinned* vma, by removing the (empty)
  227. * neighbour -- this should fail.
  228. */
  229. target.color = I915_CACHE_L3_LLC;
  230. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  231. if (!err) {
  232. pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
  233. err = -EINVAL;
  234. goto cleanup;
  235. }
  236. err = 0;
  237. cleanup:
  238. unpin_ggtt(i915);
  239. cleanup_objects(i915);
  240. ggtt->base.mm.color_adjust = NULL;
  241. return err;
  242. }
  243. static int igt_evict_vm(void *arg)
  244. {
  245. struct drm_i915_private *i915 = arg;
  246. struct i915_ggtt *ggtt = &i915->ggtt;
  247. int err;
  248. /* Fill the GGTT with pinned objects and try to evict everything. */
  249. err = populate_ggtt(i915);
  250. if (err)
  251. goto cleanup;
  252. /* Everything is pinned, nothing should happen */
  253. err = i915_gem_evict_vm(&ggtt->base);
  254. if (err) {
  255. pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
  256. err);
  257. goto cleanup;
  258. }
  259. unpin_ggtt(i915);
  260. err = i915_gem_evict_vm(&ggtt->base);
  261. if (err) {
  262. pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
  263. err);
  264. goto cleanup;
  265. }
  266. cleanup:
  267. cleanup_objects(i915);
  268. return err;
  269. }
  270. static int igt_evict_contexts(void *arg)
  271. {
  272. const u64 PRETEND_GGTT_SIZE = 16ull << 20;
  273. struct drm_i915_private *i915 = arg;
  274. struct intel_engine_cs *engine;
  275. enum intel_engine_id id;
  276. struct reserved {
  277. struct drm_mm_node node;
  278. struct reserved *next;
  279. } *reserved = NULL;
  280. struct drm_mm_node hole;
  281. unsigned long count;
  282. int err;
  283. /*
  284. * The purpose of this test is to verify that we will trigger an
  285. * eviction in the GGTT when constructing a request that requires
  286. * additional space in the GGTT for pinning the context. This space
  287. * is not directly tied to the request so reclaiming it requires
  288. * extra work.
  289. *
  290. * As such this test is only meaningful for full-ppgtt environments
  291. * where the GTT space of the request is separate from the GGTT
  292. * allocation required to build the request.
  293. */
  294. if (!USES_FULL_PPGTT(i915))
  295. return 0;
  296. mutex_lock(&i915->drm.struct_mutex);
  297. intel_runtime_pm_get(i915);
  298. /* Reserve a block so that we know we have enough to fit a few rq */
  299. memset(&hole, 0, sizeof(hole));
  300. err = i915_gem_gtt_insert(&i915->ggtt.base, &hole,
  301. PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
  302. 0, i915->ggtt.base.total,
  303. PIN_NOEVICT);
  304. if (err)
  305. goto out_locked;
  306. /* Make the GGTT appear small by filling it with unevictable nodes */
  307. count = 0;
  308. do {
  309. struct reserved *r;
  310. r = kcalloc(1, sizeof(*r), GFP_KERNEL);
  311. if (!r) {
  312. err = -ENOMEM;
  313. goto out_locked;
  314. }
  315. if (i915_gem_gtt_insert(&i915->ggtt.base, &r->node,
  316. 1ul << 20, 0, I915_COLOR_UNEVICTABLE,
  317. 0, i915->ggtt.base.total,
  318. PIN_NOEVICT)) {
  319. kfree(r);
  320. break;
  321. }
  322. r->next = reserved;
  323. reserved = r;
  324. count++;
  325. } while (1);
  326. drm_mm_remove_node(&hole);
  327. mutex_unlock(&i915->drm.struct_mutex);
  328. pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
  329. /* Overfill the GGTT with context objects and so try to evict one. */
  330. for_each_engine(engine, i915, id) {
  331. struct i915_sw_fence fence;
  332. struct drm_file *file;
  333. file = mock_file(i915);
  334. if (IS_ERR(file))
  335. return PTR_ERR(file);
  336. count = 0;
  337. mutex_lock(&i915->drm.struct_mutex);
  338. onstack_fence_init(&fence);
  339. do {
  340. struct i915_request *rq;
  341. struct i915_gem_context *ctx;
  342. ctx = live_context(i915, file);
  343. if (!ctx)
  344. break;
  345. /* We will need some GGTT space for the rq's context */
  346. igt_evict_ctl.fail_if_busy = true;
  347. rq = i915_request_alloc(engine, ctx);
  348. igt_evict_ctl.fail_if_busy = false;
  349. if (IS_ERR(rq)) {
  350. /* When full, fail_if_busy will trigger EBUSY */
  351. if (PTR_ERR(rq) != -EBUSY) {
  352. pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n",
  353. ctx->hw_id, engine->name,
  354. (int)PTR_ERR(rq));
  355. err = PTR_ERR(rq);
  356. }
  357. break;
  358. }
  359. /* Keep every request/ctx pinned until we are full */
  360. err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
  361. &fence,
  362. GFP_KERNEL);
  363. if (err < 0)
  364. break;
  365. i915_request_add(rq);
  366. count++;
  367. err = 0;
  368. } while(1);
  369. mutex_unlock(&i915->drm.struct_mutex);
  370. onstack_fence_fini(&fence);
  371. pr_info("Submitted %lu contexts/requests on %s\n",
  372. count, engine->name);
  373. mock_file_free(i915, file);
  374. if (err)
  375. break;
  376. }
  377. mutex_lock(&i915->drm.struct_mutex);
  378. out_locked:
  379. while (reserved) {
  380. struct reserved *next = reserved->next;
  381. drm_mm_remove_node(&reserved->node);
  382. kfree(reserved);
  383. reserved = next;
  384. }
  385. if (drm_mm_node_allocated(&hole))
  386. drm_mm_remove_node(&hole);
  387. intel_runtime_pm_put(i915);
  388. mutex_unlock(&i915->drm.struct_mutex);
  389. return err;
  390. }
  391. int i915_gem_evict_mock_selftests(void)
  392. {
  393. static const struct i915_subtest tests[] = {
  394. SUBTEST(igt_evict_something),
  395. SUBTEST(igt_evict_for_vma),
  396. SUBTEST(igt_evict_for_cache_color),
  397. SUBTEST(igt_evict_vm),
  398. SUBTEST(igt_overcommit),
  399. };
  400. struct drm_i915_private *i915;
  401. int err;
  402. i915 = mock_gem_device();
  403. if (!i915)
  404. return -ENOMEM;
  405. mutex_lock(&i915->drm.struct_mutex);
  406. err = i915_subtests(tests, i915);
  407. mutex_unlock(&i915->drm.struct_mutex);
  408. drm_dev_unref(&i915->drm);
  409. return err;
  410. }
  411. int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
  412. {
  413. static const struct i915_subtest tests[] = {
  414. SUBTEST(igt_evict_contexts),
  415. };
  416. return i915_subtests(tests, i915);
  417. }