1 /*
2 * Copyright © 2012-2013 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 * 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
34 #include "igt.h"
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/ioctl.h>
44 #include <pthread.h>
45
46 #include "drm.h"
47
48 IGT_TEST_DESCRIPTION("Check whether prime import/export works on the same"
49 " device... but with different fds.");
50
51 #define BO_SIZE (16*1024)
52
53 static char counter;
54 volatile int pls_die = 0;
55
56 static void
check_bo(int fd1,uint32_t handle1,int fd2,uint32_t handle2)57 check_bo(int fd1, uint32_t handle1, int fd2, uint32_t handle2)
58 {
59 char *ptr1, *ptr2;
60 int i;
61
62
63 ptr1 = gem_mmap__cpu(fd1, handle1, 0, BO_SIZE, PROT_READ | PROT_WRITE);
64 ptr2 = gem_mmap__cpu(fd2, handle2, 0, BO_SIZE, PROT_READ | PROT_WRITE);
65
66 gem_set_domain(fd1, handle1, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
67 gem_set_domain(fd2, handle2, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
68
69 /* check whether it's still our old object first. */
70 for (i = 0; i < BO_SIZE; i++) {
71 igt_assert(ptr1[i] == counter);
72 igt_assert(ptr2[i] == counter);
73 }
74
75 counter++;
76
77 memset(ptr1, counter, BO_SIZE);
78 igt_assert(memcmp(ptr1, ptr2, BO_SIZE) == 0);
79
80 munmap(ptr1, BO_SIZE);
81 munmap(ptr2, BO_SIZE);
82 }
83
test_with_fd_dup(void)84 static void test_with_fd_dup(void)
85 {
86 int fd1, fd2;
87 uint32_t handle, handle_import;
88 int dma_buf_fd1, dma_buf_fd2;
89
90 counter = 0;
91
92 fd1 = drm_open_driver(DRIVER_INTEL);
93 fd2 = drm_open_driver(DRIVER_INTEL);
94
95 handle = gem_create(fd1, BO_SIZE);
96
97 dma_buf_fd1 = prime_handle_to_fd(fd1, handle);
98 gem_close(fd1, handle);
99
100 dma_buf_fd2 = dup(dma_buf_fd1);
101 close(dma_buf_fd1);
102 handle_import = prime_fd_to_handle(fd2, dma_buf_fd2);
103 check_bo(fd2, handle_import, fd2, handle_import);
104
105 close(dma_buf_fd2);
106 check_bo(fd2, handle_import, fd2, handle_import);
107
108 close(fd1);
109 close(fd2);
110 }
111
test_with_two_bos(void)112 static void test_with_two_bos(void)
113 {
114 int fd1, fd2;
115 uint32_t handle1, handle2, handle_import;
116 int dma_buf_fd;
117
118 counter = 0;
119
120 fd1 = drm_open_driver(DRIVER_INTEL);
121 fd2 = drm_open_driver(DRIVER_INTEL);
122
123 handle1 = gem_create(fd1, BO_SIZE);
124 handle2 = gem_create(fd1, BO_SIZE);
125
126 dma_buf_fd = prime_handle_to_fd(fd1, handle1);
127 handle_import = prime_fd_to_handle(fd2, dma_buf_fd);
128
129 close(dma_buf_fd);
130 gem_close(fd1, handle1);
131
132 dma_buf_fd = prime_handle_to_fd(fd1, handle2);
133 handle_import = prime_fd_to_handle(fd2, dma_buf_fd);
134 check_bo(fd1, handle2, fd2, handle_import);
135
136 gem_close(fd1, handle2);
137 close(dma_buf_fd);
138
139 check_bo(fd2, handle_import, fd2, handle_import);
140
141 close(fd1);
142 close(fd2);
143 }
144
test_with_one_bo_two_files(void)145 static void test_with_one_bo_two_files(void)
146 {
147 int fd1, fd2;
148 uint32_t handle_import, handle_open, handle_orig, flink_name;
149 int dma_buf_fd1, dma_buf_fd2;
150
151 fd1 = drm_open_driver(DRIVER_INTEL);
152 fd2 = drm_open_driver(DRIVER_INTEL);
153
154 handle_orig = gem_create(fd1, BO_SIZE);
155 dma_buf_fd1 = prime_handle_to_fd(fd1, handle_orig);
156
157 flink_name = gem_flink(fd1, handle_orig);
158 handle_open = gem_open(fd2, flink_name);
159
160 dma_buf_fd2 = prime_handle_to_fd(fd2, handle_open);
161 handle_import = prime_fd_to_handle(fd2, dma_buf_fd2);
162
163 /* dma-buf self importing an flink bo should give the same handle */
164 igt_assert_eq_u32(handle_import, handle_open);
165
166 close(fd1);
167 close(fd2);
168 close(dma_buf_fd1);
169 close(dma_buf_fd2);
170 }
171
test_with_one_bo(void)172 static void test_with_one_bo(void)
173 {
174 int fd1, fd2;
175 uint32_t handle, handle_import1, handle_import2, handle_selfimport;
176 int dma_buf_fd;
177
178 fd1 = drm_open_driver(DRIVER_INTEL);
179 fd2 = drm_open_driver(DRIVER_INTEL);
180
181 handle = gem_create(fd1, BO_SIZE);
182
183 dma_buf_fd = prime_handle_to_fd(fd1, handle);
184 handle_import1 = prime_fd_to_handle(fd2, dma_buf_fd);
185
186 check_bo(fd1, handle, fd2, handle_import1);
187
188 /* reimport should give us the same handle so that userspace can check
189 * whether it has that bo already somewhere. */
190 handle_import2 = prime_fd_to_handle(fd2, dma_buf_fd);
191 igt_assert_eq_u32(handle_import1, handle_import2);
192
193 /* Same for re-importing on the exporting fd. */
194 handle_selfimport = prime_fd_to_handle(fd1, dma_buf_fd);
195 igt_assert_eq_u32(handle, handle_selfimport);
196
197 /* close dma_buf, check whether nothing disappears. */
198 close(dma_buf_fd);
199 check_bo(fd1, handle, fd2, handle_import1);
200
201 gem_close(fd1, handle);
202 check_bo(fd2, handle_import1, fd2, handle_import1);
203
204 /* re-import into old exporter */
205 dma_buf_fd = prime_handle_to_fd(fd2, handle_import1);
206 /* but drop all references to the obj in between */
207 gem_close(fd2, handle_import1);
208 handle = prime_fd_to_handle(fd1, dma_buf_fd);
209 handle_import1 = prime_fd_to_handle(fd2, dma_buf_fd);
210 check_bo(fd1, handle, fd2, handle_import1);
211
212 /* Completely rip out exporting fd. */
213 close(fd1);
214 check_bo(fd2, handle_import1, fd2, handle_import1);
215 }
216
thread_fn_reimport_vs_close(void * p)217 static void *thread_fn_reimport_vs_close(void *p)
218 {
219 struct drm_gem_close close_bo;
220 int *fds = p;
221 int fd = fds[0];
222 int dma_buf_fd = fds[1];
223 uint32_t handle;
224
225 while (!pls_die) {
226 handle = prime_fd_to_handle(fd, dma_buf_fd);
227
228 close_bo.handle = handle;
229 ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
230 }
231
232 return (void *)0;
233 }
234
test_reimport_close_race(void)235 static void test_reimport_close_race(void)
236 {
237 pthread_t *threads;
238 int r, i, num_threads;
239 int fds[2];
240 int obj_count;
241 void *status;
242 uint32_t handle;
243 int fake;
244
245 /* Allocate exit handler fds in here so that we dont screw
246 * up the counts */
247 fake = drm_open_driver(DRIVER_INTEL);
248
249 obj_count = igt_get_stable_obj_count(fake);
250
251 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
252
253 threads = calloc(num_threads, sizeof(pthread_t));
254
255 fds[0] = drm_open_driver(DRIVER_INTEL);
256
257 handle = gem_create(fds[0], BO_SIZE);
258
259 fds[1] = prime_handle_to_fd(fds[0], handle);
260
261 for (i = 0; i < num_threads; i++) {
262 r = pthread_create(&threads[i], NULL,
263 thread_fn_reimport_vs_close,
264 (void *)(uintptr_t)fds);
265 igt_assert_eq(r, 0);
266 }
267
268 sleep(5);
269
270 pls_die = 1;
271
272 for (i = 0; i < num_threads; i++) {
273 pthread_join(threads[i], &status);
274 igt_assert(status == 0);
275 }
276
277 close(fds[0]);
278 close(fds[1]);
279
280 obj_count = igt_get_stable_obj_count(fake) - obj_count;
281
282 igt_info("leaked %i objects\n", obj_count);
283
284 close(fake);
285
286 igt_assert_eq(obj_count, 0);
287 }
288
thread_fn_export_vs_close(void * p)289 static void *thread_fn_export_vs_close(void *p)
290 {
291 struct drm_prime_handle prime_h2f;
292 struct drm_gem_close close_bo;
293 int fd = (uintptr_t)p;
294 uint32_t handle;
295
296 while (!pls_die) {
297 /* We want to race gem close against prime export on handle one.*/
298 handle = gem_create(fd, 4096);
299 if (handle != 1)
300 gem_close(fd, handle);
301
302 /* raw ioctl since we expect this to fail */
303
304 /* WTF: for gem_flink_race I've unconditionally used handle == 1
305 * here, but with prime it seems to help a _lot_ to use
306 * something more random. */
307 prime_h2f.handle = 1;
308 prime_h2f.flags = DRM_CLOEXEC;
309 prime_h2f.fd = -1;
310
311 ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_h2f);
312
313 close_bo.handle = 1;
314 ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
315
316 close(prime_h2f.fd);
317 }
318
319 return (void *)0;
320 }
321
test_export_close_race(void)322 static void test_export_close_race(void)
323 {
324 pthread_t *threads;
325 int r, i, num_threads;
326 int fd;
327 int obj_count;
328 void *status;
329 int fake;
330
331 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
332
333 threads = calloc(num_threads, sizeof(pthread_t));
334
335 /* Allocate exit handler fds in here so that we dont screw
336 * up the counts */
337 fake = drm_open_driver(DRIVER_INTEL);
338
339 obj_count = igt_get_stable_obj_count(fake);
340
341 fd = drm_open_driver(DRIVER_INTEL);
342
343 for (i = 0; i < num_threads; i++) {
344 r = pthread_create(&threads[i], NULL,
345 thread_fn_export_vs_close,
346 (void *)(uintptr_t)fd);
347 igt_assert_eq(r, 0);
348 }
349
350 sleep(5);
351
352 pls_die = 1;
353
354 for (i = 0; i < num_threads; i++) {
355 pthread_join(threads[i], &status);
356 igt_assert(status == 0);
357 }
358
359 close(fd);
360
361 obj_count = igt_get_stable_obj_count(fake) - obj_count;
362
363 igt_info("leaked %i objects\n", obj_count);
364
365 close(fake);
366
367 igt_assert_eq(obj_count, 0);
368 }
369
test_llseek_size(void)370 static void test_llseek_size(void)
371 {
372 int fd, i;
373 uint32_t handle;
374 int dma_buf_fd;
375
376 counter = 0;
377
378 fd = drm_open_driver(DRIVER_INTEL);
379
380
381 for (i = 0; i < 10; i++) {
382 int bufsz = 4096 << i;
383
384 handle = gem_create(fd, bufsz);
385 dma_buf_fd = prime_handle_to_fd(fd, handle);
386
387 gem_close(fd, handle);
388
389 igt_assert(prime_get_size(dma_buf_fd) == bufsz);
390
391 close(dma_buf_fd);
392 }
393
394 close(fd);
395 }
396
test_llseek_bad(void)397 static void test_llseek_bad(void)
398 {
399 int fd;
400 uint32_t handle;
401 int dma_buf_fd;
402
403 counter = 0;
404
405 fd = drm_open_driver(DRIVER_INTEL);
406
407
408 handle = gem_create(fd, BO_SIZE);
409 dma_buf_fd = prime_handle_to_fd(fd, handle);
410
411 gem_close(fd, handle);
412
413 igt_require(lseek(dma_buf_fd, 0, SEEK_END) >= 0);
414
415 igt_assert(lseek(dma_buf_fd, -1, SEEK_END) == -1 && errno == EINVAL);
416 igt_assert(lseek(dma_buf_fd, 1, SEEK_SET) == -1 && errno == EINVAL);
417 igt_assert(lseek(dma_buf_fd, BO_SIZE, SEEK_SET) == -1 && errno == EINVAL);
418 igt_assert(lseek(dma_buf_fd, BO_SIZE + 1, SEEK_SET) == -1 && errno == EINVAL);
419 igt_assert(lseek(dma_buf_fd, BO_SIZE - 1, SEEK_SET) == -1 && errno == EINVAL);
420
421 close(dma_buf_fd);
422
423 close(fd);
424 }
425
426 igt_main
427 {
428 struct {
429 const char *name;
430 void (*fn)(void);
431 } tests[] = {
432 { "basic-with_one_bo", test_with_one_bo },
433 { "basic-with_one_bo_two_files", test_with_one_bo_two_files },
434 { "basic-with_two_bos", test_with_two_bos },
435 { "basic-with_fd_dup", test_with_fd_dup },
436 { "export-vs-gem_close-race", test_export_close_race },
437 { "reimport-vs-gem_close-race", test_reimport_close_race },
438 { "basic-llseek-size", test_llseek_size },
439 { "basic-llseek-bad", test_llseek_bad },
440 };
441 int i;
442
443 for (i = 0; i < ARRAY_SIZE(tests); i++) {
444 igt_subtest(tests[i].name)
445 tests[i].fn();
446 }
447 }
448