xref: /aosp_15_r20/external/mesa3d/src/util/libsync.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <stdlib.h>
40 #include <time.h>
41 
42 #include "util/detect_os.h"
43 
44 #if defined(__cplusplus)
45 extern "C" {
46 #endif
47 
48 #if DETECT_OS_ANDROID
49 /* On Android, rely on the system's libsync instead of rolling our own
50  * sync_wait() and sync_merge().  This gives us compatibility with pre-4.7
51  * Android kernels.
52  */
53 #include <android/sync.h>
54 
55 /**
56  * Check if the fd represents a valid fence-fd.
57  *
58  * The android variant of this debug helper is implemented on top of the
59  * system's libsync for compatibility with pre-4.7 android kernels.
60  */
61 static inline bool
sync_valid_fd(int fd)62 sync_valid_fd(int fd)
63 {
64 	/* sync_file_info() only available in SDK 26. */
65 #if ANDROID_API_LEVEL >= 26
66 	struct sync_file_info *info = sync_file_info(fd);
67 	if (!info)
68 		return false;
69 	sync_file_info_free(info);
70 #endif
71 	return true;
72 }
73 #else
74 
75 #ifndef SYNC_IOC_MERGE
76 /* duplicated from linux/sync_file.h to avoid build-time dependency
77  * on new (v4.7) kernel headers.  Once distro's are mostly using
78  * something newer than v4.7 drop this and #include <linux/sync_file.h>
79  * instead.
80  */
81 struct sync_merge_data {
82 	char	name[32];
83 	int32_t	fd2;
84 	int32_t	fence;
85 	uint32_t	flags;
86 	uint32_t	pad;
87 };
88 
89 struct sync_fence_info {
90 	char obj_name[32];
91 	char driver_name[32];
92 	int32_t status;
93 	uint32_t flags;
94 	uint64_t timestamp_ns;
95 };
96 
97 struct sync_file_info {
98 	char	name[32];
99 	int32_t	status;
100 	uint32_t	flags;
101 	uint32_t	num_fences;
102 	uint32_t	pad;
103 
104 	uint64_t	sync_fence_info;
105 };
106 
107 #define SYNC_IOC_MAGIC		'>'
108 #define SYNC_IOC_MERGE		_IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
109 #define SYNC_IOC_FILE_INFO	_IOWR(SYNC_IOC_MAGIC, 4, struct sync_file_info)
110 #endif
111 
112 
113 static inline int sync_wait(int fd, int timeout)
114 {
115 	struct pollfd fds = {0};
116 	int ret;
117 	struct timespec poll_start, poll_end;
118 
119 	fds.fd = fd;
120 	fds.events = POLLIN;
121 
122 	do {
123 		clock_gettime(CLOCK_MONOTONIC, &poll_start);
124 		ret = poll(&fds, 1, timeout);
125 		clock_gettime(CLOCK_MONOTONIC, &poll_end);
126 		if (ret > 0) {
127 			if (fds.revents & (POLLERR | POLLNVAL)) {
128 				errno = EINVAL;
129 				return -1;
130 			}
131 			return 0;
132 		} else if (ret == 0) {
133 			errno = ETIME;
134 			return -1;
135 		}
136 		timeout -= (poll_end.tv_sec - poll_start.tv_sec) * 1000 +
137 			(poll_end.tv_nsec - poll_end.tv_nsec) / 1000000;
138 	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
139 
140 	return ret;
141 }
142 
143 static inline int sync_merge(const char *name, int fd1, int fd2)
144 {
145 	struct sync_merge_data data = {{0}};
146 	int ret;
147 
148 	data.fd2 = fd2;
149 	strncpy(data.name, name, sizeof(data.name));
150 
151 	do {
152 		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
153 	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
154 
155 	if (ret < 0)
156 		return ret;
157 
158 	return data.fence;
159 }
160 
161 /**
162  * Check if the fd represents a valid fence-fd.
163  */
164 static inline bool
165 sync_valid_fd(int fd)
166 {
167 	struct sync_file_info info = {{0}};
168 	return ioctl(fd, SYNC_IOC_FILE_INFO, &info) >= 0;
169 }
170 
171 static inline struct sync_file_info* sync_file_info(int32_t fd)
172 {
173     struct sync_file_info local_info;
174     struct sync_file_info *info;
175     int err;
176 
177     memset(&local_info, 0, sizeof(local_info));
178     err = ioctl(fd, SYNC_IOC_FILE_INFO, &local_info);
179     if (err < 0)
180         return NULL;
181 
182     info = (struct sync_file_info *)calloc(1, sizeof(struct sync_file_info) +
183                   local_info.num_fences * sizeof(struct sync_fence_info));
184     if (!info)
185         return NULL;
186 
187     info->num_fences = local_info.num_fences;
188     info->sync_fence_info = (uint64_t)(uintptr_t)(info + 1);
189 
190     err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
191     if (err < 0) {
192         free(info);
193         return NULL;
194     }
195 
196     return info;
197 }
198 
199 #endif /* DETECT_OS_ANDROID */
200 
201 /* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
202  * otherwise sync_merge() and close the old *fd1.  This can be used
203  * to implement the pattern:
204  *
205  *    init()
206  *    {
207  *       batch.fence_fd = -1;
208  *    }
209  *
210  *    // does *NOT* take ownership of fd
211  *    server_sync(int fd)
212  *    {
213  *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
214  *          ... error ...
215  *       }
216  *    }
217  */
sync_accumulate(const char * name,int * fd1,int fd2)218 static inline int sync_accumulate(const char *name, int *fd1, int fd2)
219 {
220 	int ret;
221 
222 	assert(fd2 >= 0);
223 
224 	if (*fd1 < 0) {
225 		*fd1 = dup(fd2);
226 		return 0;
227 	}
228 
229 	ret = sync_merge(name, *fd1, fd2);
230 	if (ret < 0) {
231 		/* leave *fd1 as it is */
232 		return ret;
233 	}
234 
235 	close(*fd1);
236 	*fd1 = ret;
237 
238 	return 0;
239 }
240 
241 /* Helper macro to complain if fd is non-negative and not a valid fence fd.
242  * Sprinkle this around to help catch fd lifetime issues.
243  */
244 #if MESA_DEBUG
245 #  include "util/log.h"
246 #  define validate_fence_fd(fd) do {                                         \
247       if (((fd) >= 0) && !sync_valid_fd(fd))                                 \
248          mesa_loge("%s:%d: invalid fence fd: %d", __func__, __LINE__, (fd)); \
249    } while (0)
250 #else
251 #  define validate_fence_fd(fd) do {} while (0)
252 #endif
253 
254 #if defined(__cplusplus)
255 }
256 #endif
257 
258 #endif
259