blob: 992334d4bf7d15f2bce02be61b36444a1e29fed0 [file] [log] [blame]
Daniel Vetter6bc22e32012-07-23 09:49:06 +02001/*
Daniel Vetter4901b5f2013-07-25 21:09:43 +02002 * Copyright © 2012-2013 Intel Corporation
Daniel Vetter6bc22e32012-07-23 09:49:06 +02003 *
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 * Authors:
24 * Daniel Vetter <daniel.vetter@ffwll.ch>
25 *
26 */
27
28/*
29 * Testcase: Check whether prime import/export works on the same device
30 *
31 * ... but with different fds, i.e. the wayland usecase.
32 */
33
Daniel Vetter4901b5f2013-07-25 21:09:43 +020034#define _GNU_SOURCE
Thomas Wood804e11f2015-08-17 17:57:43 +010035#include "igt.h"
Daniel Vetter6bc22e32012-07-23 09:49:06 +020036#include <unistd.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
Daniel Vetter6bc22e32012-07-23 09:49:06 +020040#include <fcntl.h>
41#include <inttypes.h>
42#include <errno.h>
43#include <sys/stat.h>
Daniel Vetter6bc22e32012-07-23 09:49:06 +020044#include <sys/ioctl.h>
Daniel Vetter4901b5f2013-07-25 21:09:43 +020045#include <pthread.h>
46
Daniel Vetter6bc22e32012-07-23 09:49:06 +020047#include "drm.h"
Daniel Vetter6bc22e32012-07-23 09:49:06 +020048
Thomas Woodb2ac2642014-11-28 11:02:44 +000049IGT_TEST_DESCRIPTION("Check whether prime import/export works on the same"
Derek Morton3953d2d2015-12-14 09:59:17 +000050 " device... but with different fds.");
Thomas Woodb2ac2642014-11-28 11:02:44 +000051
Daniel Vetter6bc22e32012-07-23 09:49:06 +020052#define BO_SIZE (16*1024)
53
Imre Deak70afbea2013-04-17 23:18:02 +030054static char counter;
Daniel Vetter4901b5f2013-07-25 21:09:43 +020055volatile int pls_die = 0;
Imre Deak70afbea2013-04-17 23:18:02 +030056
Daniel Vetter6bc22e32012-07-23 09:49:06 +020057static void
58check_bo(int fd1, uint32_t handle1, int fd2, uint32_t handle2)
59{
60 char *ptr1, *ptr2;
Daniel Vetter5d9e2232012-07-23 10:06:25 +020061 int i;
Daniel Vetter6bc22e32012-07-23 09:49:06 +020062
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +030063 ptr1 = gem_mmap__gtt(fd1, handle1, BO_SIZE, PROT_READ | PROT_WRITE);
64 ptr2 = gem_mmap__gtt(fd2, handle2, BO_SIZE, PROT_READ | PROT_WRITE);
Daniel Vetter6bc22e32012-07-23 09:49:06 +020065
Daniel Vetter5d9e2232012-07-23 10:06:25 +020066 /* check whether it's still our old object first. */
67 for (i = 0; i < BO_SIZE; i++) {
Daniel Vetter83440952013-08-13 12:35:58 +020068 igt_assert(ptr1[i] == counter);
69 igt_assert(ptr2[i] == counter);
Daniel Vetter5d9e2232012-07-23 10:06:25 +020070 }
71
72 counter++;
73
Daniel Vetter6bc22e32012-07-23 09:49:06 +020074 memset(ptr1, counter, BO_SIZE);
Daniel Vetter83440952013-08-13 12:35:58 +020075 igt_assert(memcmp(ptr1, ptr2, BO_SIZE) == 0);
Daniel Vetter6bc22e32012-07-23 09:49:06 +020076
77 munmap(ptr1, BO_SIZE);
78 munmap(ptr2, BO_SIZE);
Daniel Vetter6bc22e32012-07-23 09:49:06 +020079}
80
Imre Deak70afbea2013-04-17 23:18:02 +030081static void test_with_fd_dup(void)
82{
83 int fd1, fd2;
84 uint32_t handle, handle_import;
85 int dma_buf_fd1, dma_buf_fd2;
86
87 counter = 0;
88
Micah Fedkec81d2932015-07-22 21:54:02 +000089 fd1 = drm_open_driver(DRIVER_INTEL);
90 fd2 = drm_open_driver(DRIVER_INTEL);
Imre Deak70afbea2013-04-17 23:18:02 +030091
92 handle = gem_create(fd1, BO_SIZE);
93
94 dma_buf_fd1 = prime_handle_to_fd(fd1, handle);
95 gem_close(fd1, handle);
96
97 dma_buf_fd2 = dup(dma_buf_fd1);
98 close(dma_buf_fd1);
99 handle_import = prime_fd_to_handle(fd2, dma_buf_fd2);
100 check_bo(fd2, handle_import, fd2, handle_import);
101
102 close(dma_buf_fd2);
103 check_bo(fd2, handle_import, fd2, handle_import);
104
105 close(fd1);
106 close(fd2);
107}
108
109static void test_with_two_bos(void)
110{
111 int fd1, fd2;
112 uint32_t handle1, handle2, handle_import;
113 int dma_buf_fd;
114
115 counter = 0;
116
Micah Fedkec81d2932015-07-22 21:54:02 +0000117 fd1 = drm_open_driver(DRIVER_INTEL);
118 fd2 = drm_open_driver(DRIVER_INTEL);
Imre Deak70afbea2013-04-17 23:18:02 +0300119
120 handle1 = gem_create(fd1, BO_SIZE);
121 handle2 = gem_create(fd1, BO_SIZE);
122
123 dma_buf_fd = prime_handle_to_fd(fd1, handle1);
124 handle_import = prime_fd_to_handle(fd2, dma_buf_fd);
125
126 close(dma_buf_fd);
127 gem_close(fd1, handle1);
128
129 dma_buf_fd = prime_handle_to_fd(fd1, handle2);
130 handle_import = prime_fd_to_handle(fd2, dma_buf_fd);
131 check_bo(fd1, handle2, fd2, handle_import);
132
133 gem_close(fd1, handle2);
134 close(dma_buf_fd);
135
136 check_bo(fd2, handle_import, fd2, handle_import);
137
138 close(fd1);
139 close(fd2);
140}
141
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200142static void test_with_one_bo_two_files(void)
143{
144 int fd1, fd2;
145 uint32_t handle_import, handle_open, handle_orig, flink_name;
Daniel Vetter00280fa2013-08-07 18:36:31 +0200146 int dma_buf_fd1, dma_buf_fd2;
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200147
Micah Fedkec81d2932015-07-22 21:54:02 +0000148 fd1 = drm_open_driver(DRIVER_INTEL);
149 fd2 = drm_open_driver(DRIVER_INTEL);
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200150
151 handle_orig = gem_create(fd1, BO_SIZE);
Daniel Vetter00280fa2013-08-07 18:36:31 +0200152 dma_buf_fd1 = prime_handle_to_fd(fd1, handle_orig);
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200153
154 flink_name = gem_flink(fd1, handle_orig);
155 handle_open = gem_open(fd2, flink_name);
156
Daniel Vetter00280fa2013-08-07 18:36:31 +0200157 dma_buf_fd2 = prime_handle_to_fd(fd2, handle_open);
158 handle_import = prime_fd_to_handle(fd2, dma_buf_fd2);
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200159
Derek Morton3953d2d2015-12-14 09:59:17 +0000160 /* dma-buf self importing an flink bo should give the same handle */
Matt Roper07be8fe2015-03-05 15:01:00 -0800161 igt_assert_eq_u32(handle_import, handle_open);
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200162
163 close(fd1);
164 close(fd2);
Daniel Vetter00280fa2013-08-07 18:36:31 +0200165 close(dma_buf_fd1);
166 close(dma_buf_fd2);
Daniel Vetter8e11ab42013-08-07 18:12:20 +0200167}
168
Imre Deak70afbea2013-04-17 23:18:02 +0300169static void test_with_one_bo(void)
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200170{
171 int fd1, fd2;
172 uint32_t handle, handle_import1, handle_import2, handle_selfimport;
173 int dma_buf_fd;
174
Micah Fedkec81d2932015-07-22 21:54:02 +0000175 fd1 = drm_open_driver(DRIVER_INTEL);
176 fd2 = drm_open_driver(DRIVER_INTEL);
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200177
178 handle = gem_create(fd1, BO_SIZE);
179
180 dma_buf_fd = prime_handle_to_fd(fd1, handle);
181 handle_import1 = prime_fd_to_handle(fd2, dma_buf_fd);
182
183 check_bo(fd1, handle, fd2, handle_import1);
184
185 /* reimport should give us the same handle so that userspace can check
186 * whether it has that bo already somewhere. */
187 handle_import2 = prime_fd_to_handle(fd2, dma_buf_fd);
Matt Roper07be8fe2015-03-05 15:01:00 -0800188 igt_assert_eq_u32(handle_import1, handle_import2);
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200189
190 /* Same for re-importing on the exporting fd. */
191 handle_selfimport = prime_fd_to_handle(fd1, dma_buf_fd);
Matt Roper07be8fe2015-03-05 15:01:00 -0800192 igt_assert_eq_u32(handle, handle_selfimport);
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200193
194 /* close dma_buf, check whether nothing disappears. */
195 close(dma_buf_fd);
196 check_bo(fd1, handle, fd2, handle_import1);
197
198 gem_close(fd1, handle);
199 check_bo(fd2, handle_import1, fd2, handle_import1);
200
201 /* re-import into old exporter */
202 dma_buf_fd = prime_handle_to_fd(fd2, handle_import1);
Daniel Vetter5d9e2232012-07-23 10:06:25 +0200203 /* but drop all references to the obj in between */
204 gem_close(fd2, handle_import1);
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200205 handle = prime_fd_to_handle(fd1, dma_buf_fd);
Daniel Vetter5d9e2232012-07-23 10:06:25 +0200206 handle_import1 = prime_fd_to_handle(fd2, dma_buf_fd);
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200207 check_bo(fd1, handle, fd2, handle_import1);
208
209 /* Completely rip out exporting fd. */
210 close(fd1);
211 check_bo(fd2, handle_import1, fd2, handle_import1);
Imre Deak70afbea2013-04-17 23:18:02 +0300212}
213
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200214static void *thread_fn_reimport_vs_close(void *p)
215{
216 struct drm_gem_close close_bo;
217 int *fds = p;
218 int fd = fds[0];
219 int dma_buf_fd = fds[1];
220 uint32_t handle;
221
222 while (!pls_die) {
223 handle = prime_fd_to_handle(fd, dma_buf_fd);
224
225 close_bo.handle = handle;
226 ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
227 }
228
229 return (void *)0;
230}
231
232static void test_reimport_close_race(void)
233{
234 pthread_t *threads;
235 int r, i, num_threads;
236 int fds[2];
Oscar Mateoece21fa2013-11-05 10:56:24 +0000237 int obj_count;
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200238 void *status;
239 uint32_t handle;
Mika Kuoppala8741c222014-05-07 16:46:19 +0300240 int fake;
241
242 /* Allocate exit handler fds in here so that we dont screw
243 * up the counts */
Micah Fedkec81d2932015-07-22 21:54:02 +0000244 fake = drm_open_driver(DRIVER_INTEL);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200245
Derek Morton3953d2d2015-12-14 09:59:17 +0000246 obj_count = igt_get_stable_obj_count(fake);
Oscar Mateoece21fa2013-11-05 10:56:24 +0000247
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200248 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
249
250 threads = calloc(num_threads, sizeof(pthread_t));
251
Micah Fedkec81d2932015-07-22 21:54:02 +0000252 fds[0] = drm_open_driver(DRIVER_INTEL);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200253
254 handle = gem_create(fds[0], BO_SIZE);
255
256 fds[1] = prime_handle_to_fd(fds[0], handle);
257
258 for (i = 0; i < num_threads; i++) {
259 r = pthread_create(&threads[i], NULL,
260 thread_fn_reimport_vs_close,
261 (void *)(uintptr_t)fds);
Matt Roper07be8fe2015-03-05 15:01:00 -0800262 igt_assert_eq(r, 0);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200263 }
264
265 sleep(5);
266
267 pls_die = 1;
268
269 for (i = 0; i < num_threads; i++) {
270 pthread_join(threads[i], &status);
Daniel Vetter83440952013-08-13 12:35:58 +0200271 igt_assert(status == 0);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200272 }
273
274 close(fds[0]);
275 close(fds[1]);
276
Derek Morton3953d2d2015-12-14 09:59:17 +0000277 obj_count = igt_get_stable_obj_count(fake) - obj_count;
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200278
Daniel Vettere624fa82014-05-14 00:36:04 +0200279 igt_info("leaked %i objects\n", obj_count);
Mika Kuoppala8741c222014-05-07 16:46:19 +0300280
281 close(fake);
282
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100283 igt_assert_eq(obj_count, 0);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200284}
285
286static void *thread_fn_export_vs_close(void *p)
287{
288 struct drm_prime_handle prime_h2f;
289 struct drm_gem_close close_bo;
290 int fd = (uintptr_t)p;
291 uint32_t handle;
292
293 while (!pls_die) {
Daniel Vettera285f032013-08-07 14:54:00 +0200294 /* We want to race gem close against prime export on handle one.*/
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200295 handle = gem_create(fd, 4096);
296 if (handle != 1)
297 gem_close(fd, handle);
298
299 /* raw ioctl since we expect this to fail */
300
301 /* WTF: for gem_flink_race I've unconditionally used handle == 1
302 * here, but with prime it seems to help a _lot_ to use
303 * something more random. */
304 prime_h2f.handle = 1;
305 prime_h2f.flags = DRM_CLOEXEC;
306 prime_h2f.fd = -1;
307
308 ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_h2f);
309
310 close_bo.handle = 1;
311 ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
312
313 close(prime_h2f.fd);
314 }
315
316 return (void *)0;
317}
318
319static void test_export_close_race(void)
320{
321 pthread_t *threads;
322 int r, i, num_threads;
323 int fd;
Oscar Mateoece21fa2013-11-05 10:56:24 +0000324 int obj_count;
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200325 void *status;
Thomas Daniel51d87b82015-01-19 16:34:51 +0000326 int fake;
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200327
328 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
329
330 threads = calloc(num_threads, sizeof(pthread_t));
331
Thomas Daniel51d87b82015-01-19 16:34:51 +0000332 /* Allocate exit handler fds in here so that we dont screw
333 * up the counts */
Micah Fedkec81d2932015-07-22 21:54:02 +0000334 fake = drm_open_driver(DRIVER_INTEL);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200335
Derek Morton3953d2d2015-12-14 09:59:17 +0000336 obj_count = igt_get_stable_obj_count(fake);
Daniel Vetter08f0e1c2014-11-18 13:49:29 +0100337
Micah Fedkec81d2932015-07-22 21:54:02 +0000338 fd = drm_open_driver(DRIVER_INTEL);
Thomas Daniel51d87b82015-01-19 16:34:51 +0000339
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200340 for (i = 0; i < num_threads; i++) {
341 r = pthread_create(&threads[i], NULL,
342 thread_fn_export_vs_close,
343 (void *)(uintptr_t)fd);
Matt Roper07be8fe2015-03-05 15:01:00 -0800344 igt_assert_eq(r, 0);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200345 }
346
347 sleep(5);
348
349 pls_die = 1;
350
351 for (i = 0; i < num_threads; i++) {
352 pthread_join(threads[i], &status);
Daniel Vetter83440952013-08-13 12:35:58 +0200353 igt_assert(status == 0);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200354 }
355
356 close(fd);
357
Derek Morton3953d2d2015-12-14 09:59:17 +0000358 obj_count = igt_get_stable_obj_count(fake) - obj_count;
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200359
Daniel Vettere624fa82014-05-14 00:36:04 +0200360 igt_info("leaked %i objects\n", obj_count);
Thomas Daniel51d87b82015-01-19 16:34:51 +0000361
362 close(fake);
363
Chris Wilson3b94d3f2014-08-29 13:11:40 +0100364 igt_assert_eq(obj_count, 0);
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200365}
366
Daniel Vettere58891d2013-08-15 14:00:23 +0200367static void test_llseek_size(void)
368{
369 int fd, i;
370 uint32_t handle;
371 int dma_buf_fd;
372
373 counter = 0;
374
Micah Fedkec81d2932015-07-22 21:54:02 +0000375 fd = drm_open_driver(DRIVER_INTEL);
Daniel Vettere58891d2013-08-15 14:00:23 +0200376
377
378 for (i = 0; i < 10; i++) {
379 int bufsz = 4096 << i;
380
381 handle = gem_create(fd, bufsz);
382 dma_buf_fd = prime_handle_to_fd(fd, handle);
383
384 gem_close(fd, handle);
385
386 igt_assert(prime_get_size(dma_buf_fd) == bufsz);
387
388 close(dma_buf_fd);
389 }
390
391 close(fd);
392}
393
394static void test_llseek_bad(void)
395{
396 int fd;
397 uint32_t handle;
398 int dma_buf_fd;
399
400 counter = 0;
401
Micah Fedkec81d2932015-07-22 21:54:02 +0000402 fd = drm_open_driver(DRIVER_INTEL);
Daniel Vettere58891d2013-08-15 14:00:23 +0200403
404
405 handle = gem_create(fd, BO_SIZE);
406 dma_buf_fd = prime_handle_to_fd(fd, handle);
407
408 gem_close(fd, handle);
409
410 igt_require(lseek(dma_buf_fd, 0, SEEK_END) >= 0);
411
412 igt_assert(lseek(dma_buf_fd, -1, SEEK_END) == -1 && errno == EINVAL);
413 igt_assert(lseek(dma_buf_fd, 1, SEEK_SET) == -1 && errno == EINVAL);
414 igt_assert(lseek(dma_buf_fd, BO_SIZE, SEEK_SET) == -1 && errno == EINVAL);
415 igt_assert(lseek(dma_buf_fd, BO_SIZE + 1, SEEK_SET) == -1 && errno == EINVAL);
416 igt_assert(lseek(dma_buf_fd, BO_SIZE - 1, SEEK_SET) == -1 && errno == EINVAL);
417
418 close(dma_buf_fd);
419
420 close(fd);
421}
422
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100423igt_main
Imre Deak70afbea2013-04-17 23:18:02 +0300424{
425 struct {
426 const char *name;
427 void (*fn)(void);
428 } tests[] = {
Jesse Barnese920d712015-08-13 12:57:22 -0700429 { "basic-with_one_bo", test_with_one_bo },
430 { "basic-with_one_bo_two_files", test_with_one_bo_two_files },
431 { "basic-with_two_bos", test_with_two_bos },
432 { "basic-with_fd_dup", test_with_fd_dup },
Daniel Vetter4901b5f2013-07-25 21:09:43 +0200433 { "export-vs-gem_close-race", test_export_close_race },
434 { "reimport-vs-gem_close-race", test_reimport_close_race },
Jesse Barnese920d712015-08-13 12:57:22 -0700435 { "basic-llseek-size", test_llseek_size },
436 { "basic-llseek-bad", test_llseek_bad },
Imre Deak70afbea2013-04-17 23:18:02 +0300437 };
438 int i;
439
Imre Deak70afbea2013-04-17 23:18:02 +0300440 for (i = 0; i < ARRAY_SIZE(tests); i++) {
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200441 igt_subtest(tests[i].name)
Imre Deak70afbea2013-04-17 23:18:02 +0300442 tests[i].fn();
443 }
Daniel Vetter6bc22e32012-07-23 09:49:06 +0200444}