lib/igt.cocci: Add s/assert/igt_assert/
[platform/upstream/intel-gpu-tools.git] / tests / gem_close_race.c
1 /*
2  * Copyright © 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  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include "drm.h"
41 #include "ioctl_wrappers.h"
42 #include "drmtest.h"
43 #include "intel_chipset.h"
44
45 #define OBJECT_SIZE (256 * 1024)
46
47 #define COPY_BLT_CMD            (2<<29|0x53<<22|0x6)
48 #define BLT_WRITE_ALPHA         (1<<21)
49 #define BLT_WRITE_RGB           (1<<20)
50
51 static char device[80];
52 static uint32_t devid;
53 static bool has_64bit_relocations;
54
55 static void selfcopy(int fd, uint32_t handle, int loops)
56 {
57         struct drm_i915_gem_relocation_entry reloc[2];
58         struct drm_i915_gem_exec_object2 gem_exec[2];
59         struct drm_i915_gem_execbuffer2 execbuf;
60         struct drm_i915_gem_pwrite gem_pwrite;
61         struct drm_i915_gem_create create;
62         uint32_t buf[12], *b = buf;
63
64         memset(reloc, 0, sizeof(reloc));
65         memset(gem_exec, 0, sizeof(gem_exec));
66         memset(&execbuf, 0, sizeof(execbuf));
67
68         *b = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
69         if (has_64bit_relocations)
70                 *b += 2;
71         b++;
72         *b++ = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
73         *b++ = 0;
74         *b++ = 1 << 16 | 1024;
75
76         reloc[0].offset = (b - buf) * sizeof(*b);
77         reloc[0].target_handle = handle;
78         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
79         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
80         *b++ = 0;
81         if (has_64bit_relocations)
82                 *b++ = 0;
83
84         *b++ = 512 << 16;
85         *b++ = 4*1024;
86
87         reloc[1].offset = (b - buf) * sizeof(*b);
88         reloc[1].target_handle = handle;
89         reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
90         reloc[1].write_domain = 0;
91         *b++ = 0;
92         if (has_64bit_relocations)
93                 *b++ = 0;
94
95         *b++ = MI_BATCH_BUFFER_END;
96         *b++ = 0;
97
98         gem_exec[0].handle = handle;
99
100         create.handle = 0;
101         create.size = 4096;
102         drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
103         gem_exec[1].handle = create.handle;
104         gem_exec[1].relocation_count = 2;
105         gem_exec[1].relocs_ptr = (uintptr_t)reloc;
106
107         execbuf.buffers_ptr = (uintptr_t)gem_exec;
108         execbuf.buffer_count = 2;
109         execbuf.batch_len = (b - buf) * sizeof(*b);
110         if (HAS_BLT_RING(devid))
111                 execbuf.flags |= I915_EXEC_BLT;
112
113         gem_pwrite.handle = gem_exec[1].handle;
114         gem_pwrite.offset = 0;
115         gem_pwrite.size = execbuf.batch_len;
116         gem_pwrite.data_ptr = (uintptr_t)buf;
117         if (drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite) == 0) {
118                 while (loops--)
119                         drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
120         }
121
122         drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
123 }
124
125 static uint32_t load(int fd)
126 {
127         uint32_t handle;
128
129         handle = gem_create(fd, OBJECT_SIZE);
130         if (handle == 0)
131                 return 0;
132
133         selfcopy(fd, handle, 100);
134         return handle;
135 }
136
137 static void run(int child)
138 {
139         uint32_t handle;
140         int fd;
141
142         fd = open(device, O_RDWR);
143         igt_assert(fd != -1);
144
145         handle = load(fd);
146         if ((child & 63) == 63)
147                 gem_read(fd, handle, 0, &handle, sizeof(handle));
148 }
149
150 #define NUM_FD 768
151
152 struct thread {
153         pthread_mutex_t mutex;
154         int device;
155         int fds[NUM_FD];
156         int done;
157 };
158
159 static void *thread_run(void *_data)
160 {
161         struct thread *t = _data;
162         uint32_t handle = gem_create(t->device, OBJECT_SIZE);
163         struct drm_gem_open arg = { gem_flink(t->device, handle) };
164
165         pthread_mutex_lock(&t->mutex);
166         while (!t->done) {
167                 pthread_mutex_unlock(&t->mutex);
168
169                 for (int n = 0; n < NUM_FD; n++) {
170                         int fd = t->fds[n];
171
172                         arg.handle = 0;
173                         drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &arg);
174                         if (arg.handle == 0)
175                                 continue;
176
177                         selfcopy(fd, arg.handle, 100);
178
179                         drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &arg.handle);
180                 }
181
182                 pthread_mutex_lock(&t->mutex);
183         }
184         pthread_mutex_unlock(&t->mutex);
185
186         gem_close(t->device, handle);
187         return 0;
188 }
189
190 static void *thread_busy(void *_data)
191 {
192         struct thread *t = _data;
193         uint32_t handle = gem_create(t->device, OBJECT_SIZE);
194         struct drm_gem_open arg = { gem_flink(t->device, handle) };
195
196         pthread_mutex_lock(&t->mutex);
197         while (!t->done) {
198                 struct drm_i915_gem_busy busy;
199                 int fd = t->fds[rand() % NUM_FD];
200
201                 pthread_mutex_unlock(&t->mutex);
202
203                 arg.handle = 0;
204                 drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &arg);
205                 if (arg.handle == 0)
206                         continue;
207
208                 selfcopy(fd, arg.handle, 10);
209
210                 busy.handle = arg.handle;
211                 drmIoctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
212
213                 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &arg.handle);
214
215                 usleep(10*1000);
216
217                 pthread_mutex_lock(&t->mutex);
218         }
219         pthread_mutex_unlock(&t->mutex);
220
221         gem_close(t->device, handle);
222         return 0;
223 }
224
225 igt_main
226 {
227         igt_skip_on_simulation();
228
229         igt_fixture {
230                 int fd;
231
232                 sprintf(device, "/dev/dri/card%d", drm_get_card());
233                 fd = open(device, O_RDWR);
234
235                 igt_assert(fd != -1);
236                 devid = intel_get_drm_devid(fd);
237                 has_64bit_relocations = intel_gen(devid) >= 8;
238                 close(fd);
239         }
240
241         igt_subtest("process-exit") {
242                 igt_fork(child, NUM_FD)
243                         run(child);
244                 igt_waitchildren();
245         }
246
247         igt_subtest("gem-close-race") {
248                 pthread_t thread[2];
249                 struct thread *data = calloc(1, sizeof(struct thread));
250                 int n;
251
252                 igt_assert(data);
253
254                 pthread_mutex_init(&data->mutex, NULL);
255                 data->device = open(device, O_RDWR);
256                 for (n = 0; n < NUM_FD; n++)
257                         data->fds[n] = open(device, O_RDWR);
258
259                 pthread_create(&thread[0], NULL, thread_run, data);
260                 pthread_create(&thread[1], NULL, thread_busy, data);
261
262                 for (n = 0; n < 1000*NUM_FD; n++) {
263                         int i = rand() % NUM_FD;
264                         if (data->fds[i] == -1) {
265                                 data->fds[i] = open(device, O_RDWR);
266                         } else{
267                                 close(data->fds[i]);
268                                 data->fds[i] = -1;
269                         }
270                 }
271
272                 pthread_mutex_lock(&data->mutex);
273                 data->done = 1;
274                 pthread_mutex_unlock(&data->mutex);
275
276                 pthread_join(thread[1], NULL);
277                 pthread_join(thread[0], NULL);
278
279                 for (n = 0; n < NUM_FD; n++)
280                         close(data->fds[n]);
281                 close(data->device);
282                 free(data);
283         }
284 }