1 /*
2  *  sync.c
3  *
4  *   Copyright 2012 Google, Inc
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <malloc.h>
22 #include <poll.h>
23 #include <stdatomic.h>
24 #include <stdint.h>
25 #include <string.h>
26 
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include <android/sync.h>
32 
33 /* Prototypes for deprecated functions that used to be declared in the legacy
34  * android/sync.h. They've been moved here to make sure new code does not use
35  * them, but the functions are still defined to avoid breaking existing
36  * binaries. Eventually they can be removed altogether.
37  */
38 struct sync_fence_info_data {
39     uint32_t len;
40     char name[32];
41     int32_t status;
42     uint8_t pt_info[0];
43 };
44 struct sync_pt_info {
45     uint32_t len;
46     char obj_name[32];
47     char driver_name[32];
48     int32_t status;
49     uint64_t timestamp_ns;
50     uint8_t driver_data[0];
51 };
52 struct sync_fence_info_data* sync_fence_info(int fd);
53 struct sync_pt_info* sync_pt_info(struct sync_fence_info_data* info, struct sync_pt_info* itr);
54 void sync_fence_info_free(struct sync_fence_info_data* info);
55 
56 /* Legacy Sync API */
57 
58 struct sync_legacy_merge_data {
59  int32_t fd2;
60  char name[32];
61  int32_t fence;
62 };
63 
64 /**
65  * DOC: SYNC_IOC_MERGE - merge two fences
66  *
67  * Takes a struct sync_merge_data.  Creates a new fence containing copies of
68  * the sync_pts in both the calling fd and sync_merge_data.fd2.  Returns the
69  * new fence's fd in sync_merge_data.fence
70  *
71  * This is the legacy version of the Sync API before the de-stage that happened
72  * on Linux kernel 4.7.
73  */
74 #define SYNC_IOC_LEGACY_MERGE   _IOWR(SYNC_IOC_MAGIC, 1, \
75     struct sync_legacy_merge_data)
76 
77 /**
78  * DOC: SYNC_IOC_LEGACY_FENCE_INFO - get detailed information on a fence
79  *
80  * Takes a struct sync_fence_info_data with extra space allocated for pt_info.
81  * Caller should write the size of the buffer into len.  On return, len is
82  * updated to reflect the total size of the sync_fence_info_data including
83  * pt_info.
84  *
85  * pt_info is a buffer containing sync_pt_infos for every sync_pt in the fence.
86  * To iterate over the sync_pt_infos, use the sync_pt_info.len field.
87  *
88  * This is the legacy version of the Sync API before the de-stage that happened
89  * on Linux kernel 4.7.
90  */
91 #define SYNC_IOC_LEGACY_FENCE_INFO  _IOWR(SYNC_IOC_MAGIC, 2,\
92     struct sync_fence_info_data)
93 
94 /* SW Sync API */
95 
96 struct sw_sync_create_fence_data {
97   __u32 value;
98   char name[32];
99   __s32 fence;
100 };
101 
102 #define SW_SYNC_IOC_MAGIC 'W'
103 #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data)
104 #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
105 
106 // ---------------------------------------------------------------------------
107 // Support for caching the sync uapi version.
108 //
109 // This library supports both legacy (android/staging) uapi and modern
110 // (mainline) sync uapi. Library calls first try one uapi, and if that fails,
111 // try the other. Since any given kernel only supports one uapi version, after
112 // the first successful syscall we know what the kernel supports and can skip
113 // trying the other.
114 
115 enum uapi_version {
116     UAPI_UNKNOWN,
117     UAPI_MODERN,
118     UAPI_LEGACY
119 };
120 static atomic_int g_uapi_version = ATOMIC_VAR_INIT(UAPI_UNKNOWN);
121 
122 // ---------------------------------------------------------------------------
123 
sync_wait(int fd,int timeout)124 int sync_wait(int fd, int timeout)
125 {
126     struct pollfd fds;
127     int ret;
128 
129     if (fd < 0) {
130         errno = EINVAL;
131         return -1;
132     }
133 
134     fds.fd = fd;
135     fds.events = POLLIN;
136 
137     do {
138         ret = poll(&fds, 1, timeout);
139         if (ret > 0) {
140             if (fds.revents & (POLLERR | POLLNVAL)) {
141                 errno = EINVAL;
142                 return -1;
143             }
144             return 0;
145         } else if (ret == 0) {
146             errno = ETIME;
147             return -1;
148         }
149     } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
150 
151     return ret;
152 }
153 
legacy_sync_merge(const char * name,int fd1,int fd2)154 static int legacy_sync_merge(const char *name, int fd1, int fd2)
155 {
156     struct sync_legacy_merge_data data;
157     int ret;
158 
159     data.fd2 = fd2;
160     strlcpy(data.name, name, sizeof(data.name));
161     ret = ioctl(fd1, SYNC_IOC_LEGACY_MERGE, &data);
162     if (ret < 0)
163         return ret;
164     return data.fence;
165 }
166 
modern_sync_merge(const char * name,int fd1,int fd2)167 static int modern_sync_merge(const char *name, int fd1, int fd2)
168 {
169     struct sync_merge_data data;
170     int ret;
171 
172     data.fd2 = fd2;
173     strlcpy(data.name, name, sizeof(data.name));
174     data.flags = 0;
175     data.pad = 0;
176 
177     ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
178     if (ret < 0)
179         return ret;
180     return data.fence;
181 }
182 
sync_merge(const char * name,int fd1,int fd2)183 int sync_merge(const char *name, int fd1, int fd2)
184 {
185     int uapi;
186     int ret;
187 
188     uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
189 
190     if (uapi == UAPI_MODERN || uapi == UAPI_UNKNOWN) {
191         ret = modern_sync_merge(name, fd1, fd2);
192         if (ret >= 0 || errno != ENOTTY) {
193             if (ret >= 0 && uapi == UAPI_UNKNOWN) {
194                 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
195                                       memory_order_release);
196             }
197             return ret;
198         }
199     }
200 
201     ret = legacy_sync_merge(name, fd1, fd2);
202     if (ret >= 0 && uapi == UAPI_UNKNOWN) {
203         atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
204                               memory_order_release);
205     }
206     return ret;
207 }
208 
legacy_sync_fence_info(int fd)209 static struct sync_fence_info_data *legacy_sync_fence_info(int fd)
210 {
211     struct sync_fence_info_data *legacy_info;
212     struct sync_pt_info *legacy_pt_info;
213     int err;
214 
215     legacy_info = malloc(4096);
216     if (legacy_info == NULL)
217         return NULL;
218 
219     legacy_info->len = 4096;
220     err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info);
221     if (err < 0) {
222         free(legacy_info);
223         return NULL;
224     }
225     return legacy_info;
226 }
227 
modern_sync_file_info(int fd)228 static struct sync_file_info *modern_sync_file_info(int fd)
229 {
230     struct sync_file_info local_info;
231     struct sync_file_info *info;
232     int err;
233 
234     memset(&local_info, 0, sizeof(local_info));
235     err = ioctl(fd, SYNC_IOC_FILE_INFO, &local_info);
236     if (err < 0)
237         return NULL;
238 
239     info = calloc(1, sizeof(struct sync_file_info) +
240                   local_info.num_fences * sizeof(struct sync_fence_info));
241     if (!info)
242         return NULL;
243 
244     info->num_fences = local_info.num_fences;
245     info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
246 
247     err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
248     if (err < 0) {
249         free(info);
250         return NULL;
251     }
252 
253     return info;
254 }
255 
sync_file_info_to_legacy_fence_info(const struct sync_file_info * info)256 static struct sync_fence_info_data *sync_file_info_to_legacy_fence_info(
257     const struct sync_file_info *info)
258 {
259     struct sync_fence_info_data *legacy_info;
260     struct sync_pt_info *legacy_pt_info;
261     const struct sync_fence_info *fence_info = sync_get_fence_info(info);
262     const uint32_t num_fences = info->num_fences;
263 
264     legacy_info = malloc(4096);
265     if (legacy_info == NULL)
266         return NULL;
267     legacy_info->len = sizeof(*legacy_info) +
268                         num_fences * sizeof(struct sync_pt_info);
269     strlcpy(legacy_info->name, info->name, sizeof(legacy_info->name));
270     legacy_info->status = info->status;
271 
272     legacy_pt_info = (struct sync_pt_info *)legacy_info->pt_info;
273     for (uint32_t i = 0; i < num_fences; i++) {
274         legacy_pt_info[i].len = sizeof(*legacy_pt_info);
275         strlcpy(legacy_pt_info[i].obj_name, fence_info[i].obj_name,
276                 sizeof(legacy_pt_info->obj_name));
277         strlcpy(legacy_pt_info[i].driver_name, fence_info[i].driver_name,
278                 sizeof(legacy_pt_info->driver_name));
279         legacy_pt_info[i].status = fence_info[i].status;
280         legacy_pt_info[i].timestamp_ns = fence_info[i].timestamp_ns;
281     }
282 
283     return legacy_info;
284 }
285 
legacy_fence_info_to_sync_file_info(struct sync_fence_info_data * legacy_info)286 static struct sync_file_info* legacy_fence_info_to_sync_file_info(
287                                     struct sync_fence_info_data *legacy_info)
288 {
289     struct sync_file_info *info;
290     struct sync_pt_info *pt;
291     struct sync_fence_info *fence;
292     size_t num_fences;
293     int err;
294 
295     pt = NULL;
296     num_fences = 0;
297     while ((pt = sync_pt_info(legacy_info, pt)) != NULL)
298         num_fences++;
299 
300     info = calloc(1, sizeof(struct sync_file_info) +
301                      num_fences * sizeof(struct sync_fence_info));
302     if (!info) {
303         return NULL;
304     }
305     info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
306 
307     strlcpy(info->name, legacy_info->name, sizeof(info->name));
308     info->status = legacy_info->status;
309     info->num_fences = num_fences;
310 
311     pt = NULL;
312     fence = sync_get_fence_info(info);
313     while ((pt = sync_pt_info(legacy_info, pt)) != NULL) {
314         strlcpy(fence->obj_name, pt->obj_name, sizeof(fence->obj_name));
315         strlcpy(fence->driver_name, pt->driver_name,
316                 sizeof(fence->driver_name));
317         fence->status = pt->status;
318         fence->timestamp_ns = pt->timestamp_ns;
319         fence++;
320     }
321 
322     return info;
323 }
324 
sync_fence_info(int fd)325 struct sync_fence_info_data *sync_fence_info(int fd)
326 {
327     struct sync_fence_info_data *legacy_info;
328     int uapi;
329 
330     uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
331 
332     if (uapi == UAPI_LEGACY || uapi == UAPI_UNKNOWN) {
333         legacy_info = legacy_sync_fence_info(fd);
334         if (legacy_info || errno != ENOTTY) {
335             if (legacy_info && uapi == UAPI_UNKNOWN) {
336                 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
337                                       memory_order_release);
338             }
339             return legacy_info;
340         }
341     }
342 
343     struct sync_file_info* file_info;
344     file_info = modern_sync_file_info(fd);
345     if (!file_info)
346         return NULL;
347     if (uapi == UAPI_UNKNOWN) {
348         atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
349                               memory_order_release);
350     }
351     legacy_info = sync_file_info_to_legacy_fence_info(file_info);
352     sync_file_info_free(file_info);
353     return legacy_info;
354 }
355 
sync_file_info(int32_t fd)356 struct sync_file_info* sync_file_info(int32_t fd)
357 {
358     struct sync_file_info *info;
359     int uapi;
360 
361     uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
362 
363     if (uapi == UAPI_MODERN || uapi == UAPI_UNKNOWN) {
364         info = modern_sync_file_info(fd);
365         if (info || errno != ENOTTY) {
366             if (info && uapi == UAPI_UNKNOWN) {
367                 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
368                                       memory_order_release);
369             }
370             return info;
371         }
372     }
373 
374     struct sync_fence_info_data *legacy_info;
375     legacy_info = legacy_sync_fence_info(fd);
376     if (!legacy_info)
377         return NULL;
378     if (uapi == UAPI_UNKNOWN) {
379         atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
380                               memory_order_release);
381     }
382     info = legacy_fence_info_to_sync_file_info(legacy_info);
383     sync_fence_info_free(legacy_info);
384     return info;
385 }
386 
sync_pt_info(struct sync_fence_info_data * info,struct sync_pt_info * itr)387 struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
388                                   struct sync_pt_info *itr)
389 {
390     if (itr == NULL)
391         itr = (struct sync_pt_info *) info->pt_info;
392     else
393         itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
394 
395     if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
396         return NULL;
397 
398     return itr;
399 }
400 
sync_fence_info_free(struct sync_fence_info_data * info)401 void sync_fence_info_free(struct sync_fence_info_data *info)
402 {
403     free(info);
404 }
405 
sync_file_info_free(struct sync_file_info * info)406 void sync_file_info_free(struct sync_file_info *info)
407 {
408     free(info);
409 }
410 
411 
sw_sync_timeline_create(void)412 int sw_sync_timeline_create(void)
413 {
414     int ret;
415 
416     ret = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
417     if (ret < 0)
418         ret = open("/dev/sw_sync", O_RDWR);
419 
420     return ret;
421 }
422 
sw_sync_timeline_inc(int fd,unsigned count)423 int sw_sync_timeline_inc(int fd, unsigned count)
424 {
425     __u32 arg = count;
426 
427     return ioctl(fd, SW_SYNC_IOC_INC, &arg);
428 }
429 
sw_sync_fence_create(int fd,const char * name,unsigned value)430 int sw_sync_fence_create(int fd, const char *name, unsigned value)
431 {
432     struct sw_sync_create_fence_data data;
433     int err;
434 
435     data.value = value;
436     strlcpy(data.name, name, sizeof(data.name));
437 
438     err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
439     if (err < 0)
440         return err;
441 
442     return data.fence;
443 }
444