summaryrefslogtreecommitdiff
path: root/tests/gem_flink_close_race.c
blob: d4f4fe865810165b9ac43489891c12d734712a5c (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* try and provoke a race between gem flink and close */

/* create a GEM object -
   flink the object.

   have two threads, a) closing the object,
   b) flinking the object, then trying to open it

   if the flink succeeds but the open fails then I believe we have hit the
   race as got a global name back for the object that no longer existed.

   For me this only really fails once in 100,000 or so runs or less,
   so this acts more like a demonstration of the race than a useful test.
*/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pthread.h>
#include "drm.h"
#include "drmtest.h"

int fd1;

static void *
worker_thread (void *_arg)
{
	int handle = *(int *)_arg;
	struct drm_gem_flink flink;
	struct drm_gem_open gem_open;
	int ret;

	flink.handle = handle;
	ret = ioctl(fd1, DRM_IOCTL_GEM_FLINK, &flink);
	if (ret)
		return NULL;

	gem_open.name = flink.name;
	ret = ioctl(fd1, DRM_IOCTL_GEM_OPEN, &gem_open);
	if (ret) {
		 fprintf(stderr,"open ret for name %d is %d %d\n", gem_open.name, ret, errno);
		 return (void *)1;
	}

	gem_close(fd1, gem_open.handle);
	return NULL;
}

int main(int argc, char **argv)
{
	int i;
	uint32_t handle;
	int ret;
	pthread_t flinker;
	struct drm_gem_flink flink;
	struct drm_gem_open gem_open;
	int name1, name2;
	void *retval;
	fd1 = drm_open_any();

	for (i = 0; i < 100000; i++) {
		handle = gem_create(fd1, 4096);

		flink.handle = handle;
		ret = ioctl(fd1, DRM_IOCTL_GEM_FLINK, &flink);
		assert(ret == 0);	

		pthread_create(&flinker, NULL, worker_thread, &handle);
		
		gem_close(fd1, handle);

		pthread_join(flinker, &retval);
		if (retval != NULL)
			fprintf(stderr,"triggered race on round %d\n", i);
	}
	close(fd1);
}