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 <[email protected]>
25 */
26
27 #include "igt.h"
28 #include <sys/ioctl.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <pthread.h>
34 #include <errno.h>
35
36 #include "intel_bufmgr.h"
37
38 IGT_TEST_DESCRIPTION("Check for flink/open vs. gem close races.");
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 library. On x86 this
47 * works, too. */
48 volatile int pls_die = 0;
49 int fd;
50
51 struct flink_name {
52 pthread_t thread;
53 unsigned long count;
54 };
55
thread_fn_flink_name(void * p)56 static void *thread_fn_flink_name(void *p)
57 {
58 struct flink_name *t = p;
59 struct drm_gem_open open_struct;
60 int ret;
61
62 while (!pls_die) {
63 memset(&open_struct, 0, sizeof(open_struct));
64
65 open_struct.name = 1;
66 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
67 if (ret == 0) {
68 uint32_t name = gem_flink(fd, open_struct.handle);
69
70 igt_assert(name == 1);
71
72 gem_close(fd, open_struct.handle);
73 t->count++;
74 } else
75 igt_assert(errno == ENOENT);
76 }
77
78 return (void *)0;
79 }
80
test_flink_name(int timeout)81 static void test_flink_name(int timeout)
82 {
83 struct flink_name *threads;
84 int r, i, num_threads;
85 unsigned long count;
86 char buf[256];
87 void *status;
88 int len;
89
90 num_threads = sysconf(_SC_NPROCESSORS_ONLN) - 1;
91 if (!num_threads)
92 num_threads = 1;
93
94 threads = calloc(num_threads, sizeof(*threads));
95
96 fd = drm_open_driver(DRIVER_INTEL);
97
98 for (i = 0; i < num_threads; i++) {
99 r = pthread_create(&threads[i].thread, NULL,
100 thread_fn_flink_name, &threads[i]);
101 igt_assert_eq(r, 0);
102 }
103
104 count = 0;
105 igt_until_timeout(timeout) {
106 uint32_t handle;
107
108 handle = gem_create(fd, 4096);
109 gem_flink(fd, handle);
110 gem_close(fd, handle);
111
112 count++;
113 }
114
115 pls_die = 1;
116
117 len = snprintf(buf, sizeof(buf), "Completed %lu cycles with [", count);
118 for (i = 0; i < num_threads; i++) {
119 pthread_join(threads[i].thread, &status);
120 igt_assert(status == 0);
121 len += snprintf(buf + len, sizeof(buf) - len, "%lu, ", threads[i].count);
122 }
123 snprintf(buf + len - 2, sizeof(buf) - len + 2, "] races");
124 igt_info("%s\n", buf);
125
126 close(fd);
127 }
128
thread_fn_flink_close(void * p)129 static void *thread_fn_flink_close(void *p)
130 {
131 struct drm_gem_flink flink;
132 struct drm_gem_close close_bo;
133 uint32_t handle;
134
135 while (!pls_die) {
136 /* We want to race gem close against flink on handle one.*/
137 handle = gem_create(fd, 4096);
138 if (handle != 1)
139 gem_close(fd, handle);
140
141 /* raw ioctl since we expect this to fail */
142 flink.handle = 1;
143 ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
144
145 close_bo.handle = 1;
146 ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
147 }
148
149 return (void *)0;
150 }
151
test_flink_close(void)152 static void test_flink_close(void)
153 {
154 pthread_t *threads;
155 int r, i, num_threads;
156 int obj_count;
157 void *status;
158 int fake;
159
160 /* Allocate exit handler fds in here so that we dont screw
161 * up the counts */
162 fake = drm_open_driver(DRIVER_INTEL);
163
164 obj_count = igt_get_stable_obj_count(fake);
165
166 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
167
168 threads = calloc(num_threads, sizeof(pthread_t));
169
170 fd = drm_open_driver(DRIVER_INTEL);
171
172 for (i = 0; i < num_threads; i++) {
173 r = pthread_create(&threads[i], NULL,
174 thread_fn_flink_close, NULL);
175 igt_assert_eq(r, 0);
176 }
177
178 sleep(5);
179
180 pls_die = 1;
181
182 for (i = 0; i < num_threads; i++) {
183 pthread_join(threads[i], &status);
184 igt_assert(status == 0);
185 }
186
187 close(fd);
188
189 obj_count = igt_get_stable_obj_count(fake) - obj_count;
190
191 igt_info("leaked %i objects\n", obj_count);
192
193 close(fake);
194
195 igt_assert_eq(obj_count, 0);
196 }
197
198 igt_main
199 {
200 igt_skip_on_simulation();
201
202 igt_subtest("flink_name")
203 test_flink_name(5);
204
205 igt_subtest("flink_close")
206 test_flink_close();
207 }
208