Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 1 | /* |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 2 | * Copyright © 2012-2013 Intel Corporation |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 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 | |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 34 | #define _GNU_SOURCE |
Thomas Wood | 804e11f | 2015-08-17 17:57:43 +0100 | [diff] [blame] | 35 | #include "igt.h" |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stdio.h> |
| 39 | #include <string.h> |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 40 | #include <fcntl.h> |
| 41 | #include <inttypes.h> |
| 42 | #include <errno.h> |
| 43 | #include <sys/stat.h> |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 44 | #include <sys/ioctl.h> |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 45 | #include <pthread.h> |
| 46 | |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 47 | #include "drm.h" |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 48 | |
Thomas Wood | b2ac264 | 2014-11-28 11:02:44 +0000 | [diff] [blame] | 49 | IGT_TEST_DESCRIPTION("Check whether prime import/export works on the same" |
Derek Morton | 3953d2d | 2015-12-14 09:59:17 +0000 | [diff] [blame] | 50 | " device... but with different fds."); |
Thomas Wood | b2ac264 | 2014-11-28 11:02:44 +0000 | [diff] [blame] | 51 | |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 52 | #define BO_SIZE (16*1024) |
| 53 | |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 54 | static char counter; |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 55 | volatile int pls_die = 0; |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 56 | |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 57 | static void |
| 58 | check_bo(int fd1, uint32_t handle1, int fd2, uint32_t handle2) |
| 59 | { |
| 60 | char *ptr1, *ptr2; |
Daniel Vetter | 5d9e223 | 2012-07-23 10:06:25 +0200 | [diff] [blame] | 61 | int i; |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 62 | |
Ville Syrjälä | f52e7ec | 2015-10-09 19:11:39 +0300 | [diff] [blame] | 63 | 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 Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 65 | |
Daniel Vetter | 5d9e223 | 2012-07-23 10:06:25 +0200 | [diff] [blame] | 66 | /* check whether it's still our old object first. */ |
| 67 | for (i = 0; i < BO_SIZE; i++) { |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 68 | igt_assert(ptr1[i] == counter); |
| 69 | igt_assert(ptr2[i] == counter); |
Daniel Vetter | 5d9e223 | 2012-07-23 10:06:25 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | counter++; |
| 73 | |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 74 | memset(ptr1, counter, BO_SIZE); |
Daniel Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 75 | igt_assert(memcmp(ptr1, ptr2, BO_SIZE) == 0); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 76 | |
| 77 | munmap(ptr1, BO_SIZE); |
| 78 | munmap(ptr2, BO_SIZE); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 81 | static 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 Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 89 | fd1 = drm_open_driver(DRIVER_INTEL); |
| 90 | fd2 = drm_open_driver(DRIVER_INTEL); |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 91 | |
| 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 | |
| 109 | static 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 Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 117 | fd1 = drm_open_driver(DRIVER_INTEL); |
| 118 | fd2 = drm_open_driver(DRIVER_INTEL); |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 119 | |
| 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 Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 142 | static 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 Vetter | 00280fa | 2013-08-07 18:36:31 +0200 | [diff] [blame] | 146 | int dma_buf_fd1, dma_buf_fd2; |
Daniel Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 147 | |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 148 | fd1 = drm_open_driver(DRIVER_INTEL); |
| 149 | fd2 = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 150 | |
| 151 | handle_orig = gem_create(fd1, BO_SIZE); |
Daniel Vetter | 00280fa | 2013-08-07 18:36:31 +0200 | [diff] [blame] | 152 | dma_buf_fd1 = prime_handle_to_fd(fd1, handle_orig); |
Daniel Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 153 | |
| 154 | flink_name = gem_flink(fd1, handle_orig); |
| 155 | handle_open = gem_open(fd2, flink_name); |
| 156 | |
Daniel Vetter | 00280fa | 2013-08-07 18:36:31 +0200 | [diff] [blame] | 157 | dma_buf_fd2 = prime_handle_to_fd(fd2, handle_open); |
| 158 | handle_import = prime_fd_to_handle(fd2, dma_buf_fd2); |
Daniel Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 159 | |
Derek Morton | 3953d2d | 2015-12-14 09:59:17 +0000 | [diff] [blame] | 160 | /* dma-buf self importing an flink bo should give the same handle */ |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 161 | igt_assert_eq_u32(handle_import, handle_open); |
Daniel Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 162 | |
| 163 | close(fd1); |
| 164 | close(fd2); |
Daniel Vetter | 00280fa | 2013-08-07 18:36:31 +0200 | [diff] [blame] | 165 | close(dma_buf_fd1); |
| 166 | close(dma_buf_fd2); |
Daniel Vetter | 8e11ab4 | 2013-08-07 18:12:20 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 169 | static void test_with_one_bo(void) |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 170 | { |
| 171 | int fd1, fd2; |
| 172 | uint32_t handle, handle_import1, handle_import2, handle_selfimport; |
| 173 | int dma_buf_fd; |
| 174 | |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 175 | fd1 = drm_open_driver(DRIVER_INTEL); |
| 176 | fd2 = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 177 | |
| 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 Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 188 | igt_assert_eq_u32(handle_import1, handle_import2); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 189 | |
| 190 | /* Same for re-importing on the exporting fd. */ |
| 191 | handle_selfimport = prime_fd_to_handle(fd1, dma_buf_fd); |
Matt Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 192 | igt_assert_eq_u32(handle, handle_selfimport); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 193 | |
| 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 Vetter | 5d9e223 | 2012-07-23 10:06:25 +0200 | [diff] [blame] | 203 | /* but drop all references to the obj in between */ |
| 204 | gem_close(fd2, handle_import1); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 205 | handle = prime_fd_to_handle(fd1, dma_buf_fd); |
Daniel Vetter | 5d9e223 | 2012-07-23 10:06:25 +0200 | [diff] [blame] | 206 | handle_import1 = prime_fd_to_handle(fd2, dma_buf_fd); |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 207 | 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 Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 214 | static 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 | |
| 232 | static void test_reimport_close_race(void) |
| 233 | { |
| 234 | pthread_t *threads; |
| 235 | int r, i, num_threads; |
| 236 | int fds[2]; |
Oscar Mateo | ece21fa | 2013-11-05 10:56:24 +0000 | [diff] [blame] | 237 | int obj_count; |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 238 | void *status; |
| 239 | uint32_t handle; |
Mika Kuoppala | 8741c22 | 2014-05-07 16:46:19 +0300 | [diff] [blame] | 240 | int fake; |
| 241 | |
| 242 | /* Allocate exit handler fds in here so that we dont screw |
| 243 | * up the counts */ |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 244 | fake = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 245 | |
Derek Morton | 3953d2d | 2015-12-14 09:59:17 +0000 | [diff] [blame] | 246 | obj_count = igt_get_stable_obj_count(fake); |
Oscar Mateo | ece21fa | 2013-11-05 10:56:24 +0000 | [diff] [blame] | 247 | |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 248 | num_threads = sysconf(_SC_NPROCESSORS_ONLN); |
| 249 | |
| 250 | threads = calloc(num_threads, sizeof(pthread_t)); |
| 251 | |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 252 | fds[0] = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 253 | |
| 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 Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 262 | igt_assert_eq(r, 0); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 263 | } |
| 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 Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 271 | igt_assert(status == 0); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | close(fds[0]); |
| 275 | close(fds[1]); |
| 276 | |
Derek Morton | 3953d2d | 2015-12-14 09:59:17 +0000 | [diff] [blame] | 277 | obj_count = igt_get_stable_obj_count(fake) - obj_count; |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 278 | |
Daniel Vetter | e624fa8 | 2014-05-14 00:36:04 +0200 | [diff] [blame] | 279 | igt_info("leaked %i objects\n", obj_count); |
Mika Kuoppala | 8741c22 | 2014-05-07 16:46:19 +0300 | [diff] [blame] | 280 | |
| 281 | close(fake); |
| 282 | |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 283 | igt_assert_eq(obj_count, 0); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static 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 Vetter | a285f03 | 2013-08-07 14:54:00 +0200 | [diff] [blame] | 294 | /* We want to race gem close against prime export on handle one.*/ |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 295 | 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 | |
| 319 | static void test_export_close_race(void) |
| 320 | { |
| 321 | pthread_t *threads; |
| 322 | int r, i, num_threads; |
| 323 | int fd; |
Oscar Mateo | ece21fa | 2013-11-05 10:56:24 +0000 | [diff] [blame] | 324 | int obj_count; |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 325 | void *status; |
Thomas Daniel | 51d87b8 | 2015-01-19 16:34:51 +0000 | [diff] [blame] | 326 | int fake; |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 327 | |
| 328 | num_threads = sysconf(_SC_NPROCESSORS_ONLN); |
| 329 | |
| 330 | threads = calloc(num_threads, sizeof(pthread_t)); |
| 331 | |
Thomas Daniel | 51d87b8 | 2015-01-19 16:34:51 +0000 | [diff] [blame] | 332 | /* Allocate exit handler fds in here so that we dont screw |
| 333 | * up the counts */ |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 334 | fake = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 335 | |
Derek Morton | 3953d2d | 2015-12-14 09:59:17 +0000 | [diff] [blame] | 336 | obj_count = igt_get_stable_obj_count(fake); |
Daniel Vetter | 08f0e1c | 2014-11-18 13:49:29 +0100 | [diff] [blame] | 337 | |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 338 | fd = drm_open_driver(DRIVER_INTEL); |
Thomas Daniel | 51d87b8 | 2015-01-19 16:34:51 +0000 | [diff] [blame] | 339 | |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 340 | 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 Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 344 | igt_assert_eq(r, 0); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 345 | } |
| 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 Vetter | 8344095 | 2013-08-13 12:35:58 +0200 | [diff] [blame] | 353 | igt_assert(status == 0); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | close(fd); |
| 357 | |
Derek Morton | 3953d2d | 2015-12-14 09:59:17 +0000 | [diff] [blame] | 358 | obj_count = igt_get_stable_obj_count(fake) - obj_count; |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 359 | |
Daniel Vetter | e624fa8 | 2014-05-14 00:36:04 +0200 | [diff] [blame] | 360 | igt_info("leaked %i objects\n", obj_count); |
Thomas Daniel | 51d87b8 | 2015-01-19 16:34:51 +0000 | [diff] [blame] | 361 | |
| 362 | close(fake); |
| 363 | |
Chris Wilson | 3b94d3f | 2014-08-29 13:11:40 +0100 | [diff] [blame] | 364 | igt_assert_eq(obj_count, 0); |
Daniel Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Daniel Vetter | e58891d | 2013-08-15 14:00:23 +0200 | [diff] [blame] | 367 | static 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 Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 375 | fd = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | e58891d | 2013-08-15 14:00:23 +0200 | [diff] [blame] | 376 | |
| 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 | |
| 394 | static void test_llseek_bad(void) |
| 395 | { |
| 396 | int fd; |
| 397 | uint32_t handle; |
| 398 | int dma_buf_fd; |
| 399 | |
| 400 | counter = 0; |
| 401 | |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 402 | fd = drm_open_driver(DRIVER_INTEL); |
Daniel Vetter | e58891d | 2013-08-15 14:00:23 +0200 | [diff] [blame] | 403 | |
| 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 Vetter | 071e9ca | 2013-10-31 16:23:26 +0100 | [diff] [blame] | 423 | igt_main |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 424 | { |
| 425 | struct { |
| 426 | const char *name; |
| 427 | void (*fn)(void); |
| 428 | } tests[] = { |
Jesse Barnes | e920d71 | 2015-08-13 12:57:22 -0700 | [diff] [blame] | 429 | { "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 Vetter | 4901b5f | 2013-07-25 21:09:43 +0200 | [diff] [blame] | 433 | { "export-vs-gem_close-race", test_export_close_race }, |
| 434 | { "reimport-vs-gem_close-race", test_reimport_close_race }, |
Jesse Barnes | e920d71 | 2015-08-13 12:57:22 -0700 | [diff] [blame] | 435 | { "basic-llseek-size", test_llseek_size }, |
| 436 | { "basic-llseek-bad", test_llseek_bad }, |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 437 | }; |
| 438 | int i; |
| 439 | |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 440 | for (i = 0; i < ARRAY_SIZE(tests); i++) { |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 441 | igt_subtest(tests[i].name) |
Imre Deak | 70afbea | 2013-04-17 23:18:02 +0300 | [diff] [blame] | 442 | tests[i].fn(); |
| 443 | } |
Daniel Vetter | 6bc22e3 | 2012-07-23 09:49:06 +0200 | [diff] [blame] | 444 | } |