s/drmtest_subtest_block/drmtest_subtest/
[platform/upstream/intel-gpu-tools.git] / tests / gem_flink_race.c
1 /*
2  * Copyright (c) 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 #define _GNU_SOURCE
28 #include <sys/ioctl.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <pthread.h>
35
36 #include "drmtest.h"
37 #include "i915_drm.h"
38 #include "intel_bufmgr.h"
39
40 /* Testcase: check for flink/open vs. gem close races
41  *
42  * The gem flink open ioctl had a little race with gem close which could result
43  * in the flink name and corresponding reference getting leaked.
44  */
45
46 /* We want lockless and I'm to lazy to dig out an atomic libarary. On x86 this
47  * works, too. */
48 volatile int pls_die = 0;
49 int fd;
50
51 static int get_object_count(void)
52 {
53         FILE *file;
54         int ret, scanned;
55         int device = drm_get_card(0);
56         char *path;
57
58         ret = asprintf(&path, "/sys/kernel/debug/dri/%d/i915_gem_objects", device);
59         assert(ret != -1);
60
61         file = fopen(path, "r");
62
63         scanned = fscanf(file, "%i objects", &ret);
64         assert(scanned == 1);
65
66         return ret;
67 }
68
69
70 static void *thread_fn_flink_name(void *p)
71 {
72         struct drm_gem_open open_struct;
73         int ret;
74
75         while (!pls_die) {
76                 memset(&open_struct, 0, sizeof(open_struct));
77
78                 open_struct.name = 1;
79                 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
80                 if (ret == 0) {
81                         uint32_t name = gem_flink(fd, open_struct.handle);
82
83                         assert(name == 1);
84
85                         gem_close(fd, open_struct.handle);
86                 } else
87                         assert(errno == ENOENT);
88         }
89
90         return (void *)0;
91 }
92
93 static void test_flink_name(void)
94 {
95         pthread_t *threads;
96         int r, i, num_threads;
97         void *status;
98
99         num_threads = sysconf(_SC_NPROCESSORS_ONLN) - 1;
100         if (!num_threads)
101                 num_threads = 1;
102
103         threads = calloc(num_threads, sizeof(pthread_t));
104
105         fd = drm_open_any();
106         assert(fd >= 0);
107
108         for (i = 0; i < num_threads; i++) {
109                 r = pthread_create(&threads[i], NULL,
110                                    thread_fn_flink_name, NULL);
111                 assert(r == 0);
112         }
113
114         for (i = 0; i < 1000000; i++) {
115                 uint32_t handle;
116
117                 handle = gem_create(fd, 4096);
118
119                 gem_flink(fd, handle);
120
121                 gem_close(fd, handle);
122         }
123
124         pls_die = 1;
125
126         for (i = 0;  i < num_threads; i++) {
127                 pthread_join(threads[i], &status);
128                 assert(status == 0);
129         }
130
131         close(fd);
132 }
133
134 static void *thread_fn_flink_close(void *p)
135 {
136         struct drm_gem_flink flink;
137         struct drm_gem_close close_bo;
138         uint32_t handle;
139
140         while (!pls_die) {
141                 /* We want to race gem close against flink on handle one.*/
142                 handle = gem_create(fd, 4096);
143                 if (handle != 1)
144                         gem_close(fd, handle);
145
146                 /* raw ioctl since we expect this to fail */
147                 flink.handle = 1;
148                 ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
149
150                 close_bo.handle = 1;
151                 ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
152         }
153
154         return (void *)0;
155 }
156
157 static void test_flink_close(void)
158 {
159         pthread_t *threads;
160         int r, i, num_threads;
161         int obj_count = get_object_count();
162         void *status;
163
164         num_threads = sysconf(_SC_NPROCESSORS_ONLN);
165
166         threads = calloc(num_threads, sizeof(pthread_t));
167
168         fd = drm_open_any();
169         assert(fd >= 0);
170
171         for (i = 0; i < num_threads; i++) {
172                 r = pthread_create(&threads[i], NULL,
173                                    thread_fn_flink_close, NULL);
174                 assert(r == 0);
175         }
176
177         sleep(5);
178
179         pls_die = 1;
180
181         for (i = 0;  i < num_threads; i++) {
182                 pthread_join(threads[i], &status);
183                 assert(status == 0);
184         }
185
186         close(fd);
187
188         obj_count = get_object_count() - obj_count;
189
190         printf("leaked %i objects\n", obj_count);
191         assert(obj_count == 0);
192 }
193
194 int main(int argc, char **argv)
195 {
196         drmtest_skip_on_simulation();
197
198         drmtest_subtest_init(argc, argv);
199
200         drmtest_subtest("flink_name")
201                 test_flink_name();
202
203         drmtest_subtest("flink_close")
204                 test_flink_close();
205
206         return 0;
207 }