• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  sync abstraction
3  *  Copyright 2015-2016 Collabora Ltd.
4  *
5  *  Based on the implementation from the Android Open Source Project,
6  *
7  *  Copyright 2012 Google, Inc
8  *
9  *  Permission is hereby granted, free of charge, to any person obtaining a
10  *  copy of this software and associated documentation files (the "Software"),
11  *  to deal in the Software without restriction, including without limitation
12  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  *  and/or sell copies of the Software, and to permit persons to whom the
14  *  Software is furnished to do so, subject to the following conditions:
15  *
16  *  The above copyright notice and this permission notice shall be included in
17  *  all copies or substantial portions of the Software.
18  *
19  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *  OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #ifndef _LIBSYNC_H
29 #define _LIBSYNC_H
30 
31 #include <assert.h>
32 #include <errno.h>
33 #include <poll.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <sys/ioctl.h>
38 #include <unistd.h>
39 #include <time.h>
40 
41 #include "util/detect_os.h"
42 
43 #if defined(__cplusplus)
44 extern "C" {
45 #endif
46 
47 #if DETECT_OS_ANDROID
48 /* On Android, rely on the system's libsync instead of rolling our own
49  * sync_wait() and sync_merge().  This gives us compatibility with pre-4.7
50  * Android kernels.
51  */
52 #include <android/sync.h>
53 
54 /**
55  * Check if the fd represents a valid fence-fd.
56  *
57  * The android variant of this debug helper is implemented on top of the
58  * system's libsync for compatibility with pre-4.7 android kernels.
59  */
60 static inline bool
sync_valid_fd(int fd)61 sync_valid_fd(int fd)
62 {
63 	/* sync_file_info() only available in SDK 26. */
64 #if ANDROID_API_LEVEL >= 26
65 	struct sync_file_info *info = sync_file_info(fd);
66 	if (!info)
67 		return false;
68 	sync_file_info_free(info);
69 #endif
70 	return true;
71 }
72 #else
73 
74 #ifndef SYNC_IOC_MERGE
75 /* duplicated from linux/sync_file.h to avoid build-time dependency
76  * on new (v4.7) kernel headers.  Once distro's are mostly using
77  * something newer than v4.7 drop this and #include <linux/sync_file.h>
78  * instead.
79  */
80 struct sync_merge_data {
81 	char	name[32];
82 	int32_t	fd2;
83 	int32_t	fence;
84 	uint32_t	flags;
85 	uint32_t	pad;
86 };
87 
88 struct sync_file_info {
89 	char	name[32];
90 	int32_t	status;
91 	uint32_t	flags;
92 	uint32_t	num_fences;
93 	uint32_t	pad;
94 
95 	uint64_t	sync_fence_info;
96 };
97 
98 #define SYNC_IOC_MAGIC		'>'
99 #define SYNC_IOC_MERGE		_IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
100 #define SYNC_IOC_FILE_INFO	_IOWR(SYNC_IOC_MAGIC, 4, struct sync_file_info)
101 #endif
102 
103 
104 static inline int sync_wait(int fd, int timeout)
105 {
106 	struct pollfd fds = {0};
107 	int ret;
108 	struct timespec poll_start, poll_end;
109 
110 	fds.fd = fd;
111 	fds.events = POLLIN;
112 
113 	do {
114 		clock_gettime(CLOCK_MONOTONIC, &poll_start);
115 		ret = poll(&fds, 1, timeout);
116 		clock_gettime(CLOCK_MONOTONIC, &poll_end);
117 		if (ret > 0) {
118 			if (fds.revents & (POLLERR | POLLNVAL)) {
119 				errno = EINVAL;
120 				return -1;
121 			}
122 			return 0;
123 		} else if (ret == 0) {
124 			errno = ETIME;
125 			return -1;
126 		}
127 		timeout -= (poll_end.tv_sec - poll_start.tv_sec) * 1000 +
128 			(poll_end.tv_nsec - poll_end.tv_nsec) / 1000000;
129 	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
130 
131 	return ret;
132 }
133 
134 static inline int sync_merge(const char *name, int fd1, int fd2)
135 {
136 	struct sync_merge_data data = {{0}};
137 	int ret;
138 
139 	data.fd2 = fd2;
140 	strncpy(data.name, name, sizeof(data.name));
141 
142 	do {
143 		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
144 	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
145 
146 	if (ret < 0)
147 		return ret;
148 
149 	return data.fence;
150 }
151 
152 /**
153  * Check if the fd represents a valid fence-fd.
154  */
155 static inline bool
156 sync_valid_fd(int fd)
157 {
158 	struct sync_file_info info = {{0}};
159 	return ioctl(fd, SYNC_IOC_FILE_INFO, &info) >= 0;
160 }
161 
162 #endif /* DETECT_OS_ANDROID */
163 
164 /* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
165  * otherwise sync_merge() and close the old *fd1.  This can be used
166  * to implement the pattern:
167  *
168  *    init()
169  *    {
170  *       batch.fence_fd = -1;
171  *    }
172  *
173  *    // does *NOT* take ownership of fd
174  *    server_sync(int fd)
175  *    {
176  *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
177  *          ... error ...
178  *       }
179  *    }
180  */
sync_accumulate(const char * name,int * fd1,int fd2)181 static inline int sync_accumulate(const char *name, int *fd1, int fd2)
182 {
183 	int ret;
184 
185 	assert(fd2 >= 0);
186 
187 	if (*fd1 < 0) {
188 		*fd1 = dup(fd2);
189 		return 0;
190 	}
191 
192 	ret = sync_merge(name, *fd1, fd2);
193 	if (ret < 0) {
194 		/* leave *fd1 as it is */
195 		return ret;
196 	}
197 
198 	close(*fd1);
199 	*fd1 = ret;
200 
201 	return 0;
202 }
203 
204 /* Helper macro to complain if fd is non-negative and not a valid fence fd.
205  * Sprinkle this around to help catch fd lifetime issues.
206  */
207 #ifdef DEBUG
208 #  include "util/log.h"
209 #  define validate_fence_fd(fd) do {                                         \
210       if (((fd) >= 0) && !sync_valid_fd(fd))                                 \
211          mesa_loge("%s:%d: invalid fence fd: %d", __func__, __LINE__, (fd)); \
212    } while (0)
213 #else
214 #  define validate_fence_fd(fd) do {} while (0)
215 #endif
216 
217 #if defined(__cplusplus)
218 }
219 #endif
220 
221 #endif
222