gem_close_race.c 6.6 KB
Newer Older
Chris Wilson's avatar
Chris Wilson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Chris Wilson <chris@chris-wilson.co.uk>
 *
 */

28
#include <pthread.h>
Chris Wilson's avatar
Chris Wilson committed
29 30 31 32 33 34 35 36 37 38 39 40
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include "drm.h"
41
#include "ioctl_wrappers.h"
Chris Wilson's avatar
Chris Wilson committed
42
#include "drmtest.h"
43
#include "intel_chipset.h"
Chris Wilson's avatar
Chris Wilson committed
44

45
#define OBJECT_SIZE (256 * 1024)
Chris Wilson's avatar
Chris Wilson committed
46 47 48 49 50

#define COPY_BLT_CMD		(2<<29|0x53<<22|0x6)
#define BLT_WRITE_ALPHA		(1<<21)
#define BLT_WRITE_RGB		(1<<20)

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)
Chris Wilson's avatar
Chris Wilson committed
56 57 58 59
{
	struct drm_i915_gem_relocation_entry reloc[2];
	struct drm_i915_gem_exec_object2 gem_exec[2];
	struct drm_i915_gem_execbuffer2 execbuf;
60 61
	struct drm_i915_gem_pwrite gem_pwrite;
	struct drm_i915_gem_create create;
62
	uint32_t buf[12], *b = buf;
Chris Wilson's avatar
Chris Wilson committed
63 64 65 66 67

	memset(reloc, 0, sizeof(reloc));
	memset(gem_exec, 0, sizeof(gem_exec));
	memset(&execbuf, 0, sizeof(execbuf));

68 69 70 71 72 73
	*b = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
	if (has_64bit_relocations)
		*b += 2;
	b++;
	*b++ = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
	*b++ = 0;
74
	*b++ = 1 << 16 | 1024;
75 76

	reloc[0].offset = (b - buf) * sizeof(*b);
Chris Wilson's avatar
Chris Wilson committed
77 78 79
	reloc[0].target_handle = handle;
	reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
	reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
80 81 82 83 84 85
	*b++ = 0;
	if (has_64bit_relocations)
		*b++ = 0;

	*b++ = 512 << 16;
	*b++ = 4*1024;
Chris Wilson's avatar
Chris Wilson committed
86

87
	reloc[1].offset = (b - buf) * sizeof(*b);
Chris Wilson's avatar
Chris Wilson committed
88 89 90
	reloc[1].target_handle = handle;
	reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
	reloc[1].write_domain = 0;
91 92 93
	*b++ = 0;
	if (has_64bit_relocations)
		*b++ = 0;
Chris Wilson's avatar
Chris Wilson committed
94

95 96
	*b++ = MI_BATCH_BUFFER_END;
	*b++ = 0;
Chris Wilson's avatar
Chris Wilson committed
97 98 99

	gem_exec[0].handle = handle;

100 101 102 103
	create.handle = 0;
	create.size = 4096;
	drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
	gem_exec[1].handle = create.handle;
Chris Wilson's avatar
Chris Wilson committed
104 105 106
	gem_exec[1].relocation_count = 2;
	gem_exec[1].relocs_ptr = (uintptr_t)reloc;

107
	execbuf.buffers_ptr = (uintptr_t)gem_exec;
Chris Wilson's avatar
Chris Wilson committed
108
	execbuf.buffer_count = 2;
109
	execbuf.batch_len = (b - buf) * sizeof(*b);
110
	if (HAS_BLT_RING(devid))
Chris Wilson's avatar
Chris Wilson committed
111 112
		execbuf.flags |= I915_EXEC_BLT;

113 114
	gem_pwrite.handle = gem_exec[1].handle;
	gem_pwrite.offset = 0;
115
	gem_pwrite.size = execbuf.batch_len;
116 117 118 119 120 121 122
	gem_pwrite.data_ptr = (uintptr_t)buf;
	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite) == 0) {
		while (loops--)
			drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
	}

	drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
Chris Wilson's avatar
Chris Wilson committed
123 124 125 126 127 128 129 130 131 132
}

static uint32_t load(int fd)
{
	uint32_t handle;

	handle = gem_create(fd, OBJECT_SIZE);
	if (handle == 0)
		return 0;

133
	selfcopy(fd, handle, 100);
Chris Wilson's avatar
Chris Wilson committed
134 135 136 137 138 139
	return handle;
}

static void run(int child)
{
	uint32_t handle;
140
	int fd;
Chris Wilson's avatar
Chris Wilson committed
141

142
	fd = open(device, O_RDWR);
143
	igt_assert_neq(fd, -1);
Chris Wilson's avatar
Chris Wilson committed
144 145

	handle = load(fd);
146
	if ((child & 63) == 63)
Chris Wilson's avatar
Chris Wilson committed
147 148 149
		gem_read(fd, handle, 0, &handle, sizeof(handle));
}

150
#define NUM_FD 768
151 152 153

struct thread {
	pthread_mutex_t mutex;
154
	int device;
155 156 157 158 159 160 161
	int fds[NUM_FD];
	int done;
};

static void *thread_run(void *_data)
{
	struct thread *t = _data;
162 163
	uint32_t handle = gem_create(t->device, OBJECT_SIZE);
	struct drm_gem_open arg = { gem_flink(t->device, handle) };
164 165 166 167 168 169

	pthread_mutex_lock(&t->mutex);
	while (!t->done) {
		pthread_mutex_unlock(&t->mutex);

		for (int n = 0; n < NUM_FD; n++) {
170
			int fd = t->fds[n];
171

172 173 174
			arg.handle = 0;
			drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &arg);
			if (arg.handle == 0)
175 176
				continue;

177
			selfcopy(fd, arg.handle, 100);
178

179
			drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &arg.handle);
180 181 182 183 184 185
		}

		pthread_mutex_lock(&t->mutex);
	}
	pthread_mutex_unlock(&t->mutex);

186
	gem_close(t->device, handle);
187 188 189 190 191 192
	return 0;
}

static void *thread_busy(void *_data)
{
	struct thread *t = _data;
193 194
	uint32_t handle = gem_create(t->device, OBJECT_SIZE);
	struct drm_gem_open arg = { gem_flink(t->device, handle) };
195 196 197 198

	pthread_mutex_lock(&t->mutex);
	while (!t->done) {
		struct drm_i915_gem_busy busy;
199
		int fd = t->fds[rand() % NUM_FD];
200 201 202

		pthread_mutex_unlock(&t->mutex);

203 204 205
		arg.handle = 0;
		drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &arg);
		if (arg.handle == 0)
206 207
			continue;

208
		selfcopy(fd, arg.handle, 10);
209

210 211
		busy.handle = arg.handle;
		drmIoctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
212

213
		drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &arg.handle);
214 215 216 217 218 219 220

		usleep(10*1000);

		pthread_mutex_lock(&t->mutex);
	}
	pthread_mutex_unlock(&t->mutex);

221
	gem_close(t->device, handle);
222 223 224
	return 0;
}

Daniel Vetter's avatar
Daniel Vetter committed
225
igt_main
Chris Wilson's avatar
Chris Wilson committed
226 227 228
{
	igt_skip_on_simulation();

229 230 231 232 233 234
	igt_fixture {
		int fd;

		sprintf(device, "/dev/dri/card%d", drm_get_card());
		fd = open(device, O_RDWR);

235 236
		igt_assert(fd != -1);
		devid = intel_get_drm_devid(fd);
237
		has_64bit_relocations = intel_gen(devid) >= 8;
238 239
		close(fd);
	}
240

241
	igt_subtest("process-exit") {
242
		igt_fork(child, NUM_FD)
Chris Wilson's avatar
Chris Wilson committed
243
			run(child);
244 245
		igt_waitchildren();
	}
Chris Wilson's avatar
Chris Wilson committed
246

247 248 249 250 251 252 253 254
	igt_subtest("gem-close-race") {
		pthread_t thread[2];
		struct thread *data = calloc(1, sizeof(struct thread));
		int n;

		igt_assert(data);

		pthread_mutex_init(&data->mutex, NULL);
255
		data->device = open(device, O_RDWR);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		for (n = 0; n < NUM_FD; n++)
			data->fds[n] = open(device, O_RDWR);

		pthread_create(&thread[0], NULL, thread_run, data);
		pthread_create(&thread[1], NULL, thread_busy, data);

		for (n = 0; n < 1000*NUM_FD; n++) {
			int i = rand() % NUM_FD;
			if (data->fds[i] == -1) {
				data->fds[i] = open(device, O_RDWR);
			} else{
				close(data->fds[i]);
				data->fds[i] = -1;
			}
		}

		pthread_mutex_lock(&data->mutex);
		data->done = 1;
		pthread_mutex_unlock(&data->mutex);

		pthread_join(thread[1], NULL);
		pthread_join(thread[0], NULL);

		for (n = 0; n < NUM_FD; n++)
			close(data->fds[n]);
281
		close(data->device);
282 283
		free(data);
	}
Chris Wilson's avatar
Chris Wilson committed
284
}