drm/i915: Add locking to i915_gem_evict_vm(), v3.
[linux-block.git] / drivers / gpu / drm / i915 / selftests / i915_gem_evict.c
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
25 #include "gem/i915_gem_pm.h"
26 #include "gem/selftests/igt_gem_utils.h"
27 #include "gem/selftests/mock_context.h"
28 #include "gt/intel_gt.h"
29
30 #include "i915_selftest.h"
31
32 #include "igt_flush_test.h"
33 #include "lib_sw_fence.h"
34 #include "mock_drm.h"
35 #include "mock_gem_device.h"
36
37 static void quirk_add(struct drm_i915_gem_object *obj,
38                       struct list_head *objects)
39 {
40         /* quirk is only for live tiled objects, use it to declare ownership */
41         GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
42         i915_gem_object_set_tiling_quirk(obj);
43         list_add(&obj->st_link, objects);
44 }
45
46 static int populate_ggtt(struct i915_ggtt *ggtt, struct list_head *objects)
47 {
48         struct drm_i915_gem_object *obj;
49         unsigned long count;
50
51         count = 0;
52         do {
53                 struct i915_vma *vma;
54
55                 obj = i915_gem_object_create_internal(ggtt->vm.i915,
56                                                       I915_GTT_PAGE_SIZE);
57                 if (IS_ERR(obj))
58                         return PTR_ERR(obj);
59
60                 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
61                 if (IS_ERR(vma)) {
62                         i915_gem_object_put(obj);
63                         if (vma == ERR_PTR(-ENOSPC))
64                                 break;
65
66                         return PTR_ERR(vma);
67                 }
68
69                 quirk_add(obj, objects);
70                 count++;
71         } while (1);
72         pr_debug("Filled GGTT with %lu pages [%llu total]\n",
73                  count, ggtt->vm.total / PAGE_SIZE);
74
75         if (list_empty(&ggtt->vm.bound_list)) {
76                 pr_err("No objects on the GGTT inactive list!\n");
77                 return -EINVAL;
78         }
79
80         return 0;
81 }
82
83 static void unpin_ggtt(struct i915_ggtt *ggtt)
84 {
85         struct i915_vma *vma;
86
87         list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
88                 if (i915_gem_object_has_tiling_quirk(vma->obj))
89                         i915_vma_unpin(vma);
90 }
91
92 static void cleanup_objects(struct i915_ggtt *ggtt, struct list_head *list)
93 {
94         struct drm_i915_gem_object *obj, *on;
95
96         list_for_each_entry_safe(obj, on, list, st_link) {
97                 GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
98                 i915_gem_object_set_tiling_quirk(obj);
99                 i915_gem_object_put(obj);
100         }
101
102         i915_gem_drain_freed_objects(ggtt->vm.i915);
103 }
104
105 static int igt_evict_something(void *arg)
106 {
107         struct intel_gt *gt = arg;
108         struct i915_ggtt *ggtt = gt->ggtt;
109         LIST_HEAD(objects);
110         int err;
111
112         /* Fill the GGTT with pinned objects and try to evict one. */
113
114         err = populate_ggtt(ggtt, &objects);
115         if (err)
116                 goto cleanup;
117
118         /* Everything is pinned, nothing should happen */
119         mutex_lock(&ggtt->vm.mutex);
120         err = i915_gem_evict_something(&ggtt->vm,
121                                        I915_GTT_PAGE_SIZE, 0, 0,
122                                        0, U64_MAX,
123                                        0);
124         mutex_unlock(&ggtt->vm.mutex);
125         if (err != -ENOSPC) {
126                 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
127                        err);
128                 goto cleanup;
129         }
130
131         unpin_ggtt(ggtt);
132
133         /* Everything is unpinned, we should be able to evict something */
134         mutex_lock(&ggtt->vm.mutex);
135         err = i915_gem_evict_something(&ggtt->vm,
136                                        I915_GTT_PAGE_SIZE, 0, 0,
137                                        0, U64_MAX,
138                                        0);
139         mutex_unlock(&ggtt->vm.mutex);
140         if (err) {
141                 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
142                        err);
143                 goto cleanup;
144         }
145
146 cleanup:
147         cleanup_objects(ggtt, &objects);
148         return err;
149 }
150
151 static int igt_overcommit(void *arg)
152 {
153         struct intel_gt *gt = arg;
154         struct i915_ggtt *ggtt = gt->ggtt;
155         struct drm_i915_gem_object *obj;
156         struct i915_vma *vma;
157         LIST_HEAD(objects);
158         int err;
159
160         /* Fill the GGTT with pinned objects and then try to pin one more.
161          * We expect it to fail.
162          */
163
164         err = populate_ggtt(ggtt, &objects);
165         if (err)
166                 goto cleanup;
167
168         obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
169         if (IS_ERR(obj)) {
170                 err = PTR_ERR(obj);
171                 goto cleanup;
172         }
173
174         quirk_add(obj, &objects);
175
176         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
177         if (vma != ERR_PTR(-ENOSPC)) {
178                 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR_OR_ZERO(vma));
179                 err = -EINVAL;
180                 goto cleanup;
181         }
182
183 cleanup:
184         cleanup_objects(ggtt, &objects);
185         return err;
186 }
187
188 static int igt_evict_for_vma(void *arg)
189 {
190         struct intel_gt *gt = arg;
191         struct i915_ggtt *ggtt = gt->ggtt;
192         struct drm_mm_node target = {
193                 .start = 0,
194                 .size = 4096,
195         };
196         LIST_HEAD(objects);
197         int err;
198
199         /* Fill the GGTT with pinned objects and try to evict a range. */
200
201         err = populate_ggtt(ggtt, &objects);
202         if (err)
203                 goto cleanup;
204
205         /* Everything is pinned, nothing should happen */
206         mutex_lock(&ggtt->vm.mutex);
207         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
208         mutex_unlock(&ggtt->vm.mutex);
209         if (err != -ENOSPC) {
210                 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
211                        err);
212                 goto cleanup;
213         }
214
215         unpin_ggtt(ggtt);
216
217         /* Everything is unpinned, we should be able to evict the node */
218         mutex_lock(&ggtt->vm.mutex);
219         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
220         mutex_unlock(&ggtt->vm.mutex);
221         if (err) {
222                 pr_err("i915_gem_evict_for_node returned err=%d\n",
223                        err);
224                 goto cleanup;
225         }
226
227 cleanup:
228         cleanup_objects(ggtt, &objects);
229         return err;
230 }
231
232 static void mock_color_adjust(const struct drm_mm_node *node,
233                               unsigned long color,
234                               u64 *start,
235                               u64 *end)
236 {
237 }
238
239 static int igt_evict_for_cache_color(void *arg)
240 {
241         struct intel_gt *gt = arg;
242         struct i915_ggtt *ggtt = gt->ggtt;
243         const unsigned long flags = PIN_OFFSET_FIXED;
244         struct drm_mm_node target = {
245                 .start = I915_GTT_PAGE_SIZE * 2,
246                 .size = I915_GTT_PAGE_SIZE,
247                 .color = I915_CACHE_LLC,
248         };
249         struct drm_i915_gem_object *obj;
250         struct i915_vma *vma;
251         LIST_HEAD(objects);
252         int err;
253
254         /*
255          * Currently the use of color_adjust for the GGTT is limited to cache
256          * coloring and guard pages, and so the presence of mm.color_adjust for
257          * the GGTT is assumed to be i915_ggtt_color_adjust, hence using a mock
258          * color adjust will work just fine for our purposes.
259          */
260         ggtt->vm.mm.color_adjust = mock_color_adjust;
261         GEM_BUG_ON(!i915_vm_has_cache_coloring(&ggtt->vm));
262
263         obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
264         if (IS_ERR(obj)) {
265                 err = PTR_ERR(obj);
266                 goto cleanup;
267         }
268         i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
269         quirk_add(obj, &objects);
270
271         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
272                                        I915_GTT_PAGE_SIZE | flags);
273         if (IS_ERR(vma)) {
274                 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
275                 err = PTR_ERR(vma);
276                 goto cleanup;
277         }
278
279         obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
280         if (IS_ERR(obj)) {
281                 err = PTR_ERR(obj);
282                 goto cleanup;
283         }
284         i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
285         quirk_add(obj, &objects);
286
287         /* Neighbouring; same colour - should fit */
288         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
289                                        (I915_GTT_PAGE_SIZE * 2) | flags);
290         if (IS_ERR(vma)) {
291                 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
292                 err = PTR_ERR(vma);
293                 goto cleanup;
294         }
295
296         i915_vma_unpin(vma);
297
298         /* Remove just the second vma */
299         mutex_lock(&ggtt->vm.mutex);
300         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
301         mutex_unlock(&ggtt->vm.mutex);
302         if (err) {
303                 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
304                 goto cleanup;
305         }
306
307         /* Attempt to remove the first *pinned* vma, by removing the (empty)
308          * neighbour -- this should fail.
309          */
310         target.color = I915_CACHE_L3_LLC;
311
312         mutex_lock(&ggtt->vm.mutex);
313         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
314         mutex_unlock(&ggtt->vm.mutex);
315         if (!err) {
316                 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
317                 err = -EINVAL;
318                 goto cleanup;
319         }
320
321         err = 0;
322
323 cleanup:
324         unpin_ggtt(ggtt);
325         cleanup_objects(ggtt, &objects);
326         ggtt->vm.mm.color_adjust = NULL;
327         return err;
328 }
329
330 static int igt_evict_vm(void *arg)
331 {
332         struct intel_gt *gt = arg;
333         struct i915_ggtt *ggtt = gt->ggtt;
334         struct i915_gem_ww_ctx ww;
335         LIST_HEAD(objects);
336         int err;
337
338         /* Fill the GGTT with pinned objects and try to evict everything. */
339
340         err = populate_ggtt(ggtt, &objects);
341         if (err)
342                 goto cleanup;
343
344         /* Everything is pinned, nothing should happen */
345         mutex_lock(&ggtt->vm.mutex);
346         err = i915_gem_evict_vm(&ggtt->vm, NULL);
347         mutex_unlock(&ggtt->vm.mutex);
348         if (err) {
349                 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
350                        err);
351                 goto cleanup;
352         }
353
354         unpin_ggtt(ggtt);
355
356         for_i915_gem_ww(&ww, err, false) {
357                 mutex_lock(&ggtt->vm.mutex);
358                 err = i915_gem_evict_vm(&ggtt->vm, &ww);
359                 mutex_unlock(&ggtt->vm.mutex);
360         }
361
362         if (err) {
363                 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
364                        err);
365                 goto cleanup;
366         }
367
368 cleanup:
369         cleanup_objects(ggtt, &objects);
370         return err;
371 }
372
373 static int igt_evict_contexts(void *arg)
374 {
375         const u64 PRETEND_GGTT_SIZE = 16ull << 20;
376         struct intel_gt *gt = arg;
377         struct i915_ggtt *ggtt = gt->ggtt;
378         struct drm_i915_private *i915 = gt->i915;
379         struct intel_engine_cs *engine;
380         enum intel_engine_id id;
381         struct reserved {
382                 struct drm_mm_node node;
383                 struct reserved *next;
384         } *reserved = NULL;
385         intel_wakeref_t wakeref;
386         struct drm_mm_node hole;
387         unsigned long count;
388         int err;
389
390         /*
391          * The purpose of this test is to verify that we will trigger an
392          * eviction in the GGTT when constructing a request that requires
393          * additional space in the GGTT for pinning the context. This space
394          * is not directly tied to the request so reclaiming it requires
395          * extra work.
396          *
397          * As such this test is only meaningful for full-ppgtt environments
398          * where the GTT space of the request is separate from the GGTT
399          * allocation required to build the request.
400          */
401         if (!HAS_FULL_PPGTT(i915))
402                 return 0;
403
404         wakeref = intel_runtime_pm_get(&i915->runtime_pm);
405
406         /* Reserve a block so that we know we have enough to fit a few rq */
407         memset(&hole, 0, sizeof(hole));
408         mutex_lock(&ggtt->vm.mutex);
409         err = i915_gem_gtt_insert(&ggtt->vm, &hole,
410                                   PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
411                                   0, ggtt->vm.total,
412                                   PIN_NOEVICT);
413         if (err)
414                 goto out_locked;
415
416         /* Make the GGTT appear small by filling it with unevictable nodes */
417         count = 0;
418         do {
419                 struct reserved *r;
420
421                 mutex_unlock(&ggtt->vm.mutex);
422                 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
423                 mutex_lock(&ggtt->vm.mutex);
424                 if (!r) {
425                         err = -ENOMEM;
426                         goto out_locked;
427                 }
428
429                 if (i915_gem_gtt_insert(&ggtt->vm, &r->node,
430                                         1ul << 20, 0, I915_COLOR_UNEVICTABLE,
431                                         0, ggtt->vm.total,
432                                         PIN_NOEVICT)) {
433                         kfree(r);
434                         break;
435                 }
436
437                 r->next = reserved;
438                 reserved = r;
439
440                 count++;
441         } while (1);
442         drm_mm_remove_node(&hole);
443         mutex_unlock(&ggtt->vm.mutex);
444         pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
445
446         /* Overfill the GGTT with context objects and so try to evict one. */
447         for_each_engine(engine, gt, id) {
448                 struct i915_sw_fence fence;
449                 struct i915_request *last = NULL;
450
451                 count = 0;
452                 onstack_fence_init(&fence);
453                 do {
454                         struct intel_context *ce;
455                         struct i915_request *rq;
456
457                         ce = intel_context_create(engine);
458                         if (IS_ERR(ce))
459                                 break;
460
461                         /* We will need some GGTT space for the rq's context */
462                         igt_evict_ctl.fail_if_busy = true;
463                         rq = intel_context_create_request(ce);
464                         igt_evict_ctl.fail_if_busy = false;
465                         intel_context_put(ce);
466
467                         if (IS_ERR(rq)) {
468                                 /* When full, fail_if_busy will trigger EBUSY */
469                                 if (PTR_ERR(rq) != -EBUSY) {
470                                         pr_err("Unexpected error from request alloc (on %s): %d\n",
471                                                engine->name,
472                                                (int)PTR_ERR(rq));
473                                         err = PTR_ERR(rq);
474                                 }
475                                 break;
476                         }
477
478                         /* Keep every request/ctx pinned until we are full */
479                         err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
480                                                                &fence,
481                                                                GFP_KERNEL);
482                         if (err < 0)
483                                 break;
484
485                         i915_request_add(rq);
486                         count++;
487                         if (last)
488                                 i915_request_put(last);
489                         last = i915_request_get(rq);
490                         err = 0;
491                 } while(1);
492                 onstack_fence_fini(&fence);
493                 pr_info("Submitted %lu contexts/requests on %s\n",
494                         count, engine->name);
495                 if (err)
496                         break;
497                 if (last) {
498                         if (i915_request_wait(last, 0, HZ) < 0) {
499                                 err = -EIO;
500                                 i915_request_put(last);
501                                 pr_err("Failed waiting for last request (on %s)",
502                                        engine->name);
503                                 break;
504                         }
505                         i915_request_put(last);
506                 }
507                 err = intel_gt_wait_for_idle(engine->gt, HZ * 3);
508                 if (err) {
509                         pr_err("Failed to idle GT (on %s)", engine->name);
510                         break;
511                 }
512         }
513
514         mutex_lock(&ggtt->vm.mutex);
515 out_locked:
516         if (igt_flush_test(i915))
517                 err = -EIO;
518         while (reserved) {
519                 struct reserved *next = reserved->next;
520
521                 drm_mm_remove_node(&reserved->node);
522                 kfree(reserved);
523
524                 reserved = next;
525         }
526         if (drm_mm_node_allocated(&hole))
527                 drm_mm_remove_node(&hole);
528         mutex_unlock(&ggtt->vm.mutex);
529         intel_runtime_pm_put(&i915->runtime_pm, wakeref);
530
531         return err;
532 }
533
534 int i915_gem_evict_mock_selftests(void)
535 {
536         static const struct i915_subtest tests[] = {
537                 SUBTEST(igt_evict_something),
538                 SUBTEST(igt_evict_for_vma),
539                 SUBTEST(igt_evict_for_cache_color),
540                 SUBTEST(igt_evict_vm),
541                 SUBTEST(igt_overcommit),
542         };
543         struct drm_i915_private *i915;
544         intel_wakeref_t wakeref;
545         int err = 0;
546
547         i915 = mock_gem_device();
548         if (!i915)
549                 return -ENOMEM;
550
551         with_intel_runtime_pm(&i915->runtime_pm, wakeref)
552                 err = i915_subtests(tests, to_gt(i915));
553
554         mock_destroy_device(i915);
555         return err;
556 }
557
558 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
559 {
560         static const struct i915_subtest tests[] = {
561                 SUBTEST(igt_evict_contexts),
562         };
563
564         if (intel_gt_is_wedged(to_gt(i915)))
565                 return 0;
566
567         return intel_gt_live_subtests(tests, to_gt(i915));
568 }