Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftest: sync: basic tests for sw_sync framework

These tests are based on the libsync test suite from Android.
This commit lays the ground for future tests, as well as includes
tests for a variety of basic allocation commands.

Signed-off-by: Emilio López <emilio.lopez@collabora.co.uk>
Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>

authored by

Emilio López and committed by
Shuah Khan
82208160 1001354c

+519
+1
tools/testing/selftests/Makefile
··· 23 23 TARGETS += sigaltstack 24 24 TARGETS += size 25 25 TARGETS += static_keys 26 + TARGETS += sync 26 27 TARGETS += sysctl 27 28 ifneq (1, $(quicktest)) 28 29 TARGETS += timers
+1
tools/testing/selftests/sync/.gitignore
··· 1 + sync_test
+18
tools/testing/selftests/sync/Makefile
··· 1 + CFLAGS += -O2 -g -std=gnu89 -pthread -Wall -Wextra 2 + CFLAGS += -I../../../../usr/include/ 3 + LDFLAGS += -pthread 4 + 5 + TEST_PROGS = sync_test 6 + 7 + all: $(TEST_PROGS) 8 + 9 + include ../lib.mk 10 + 11 + OBJS = sync_test.o sync.o 12 + 13 + TESTS += sync_alloc.o 14 + 15 + sync_test: $(OBJS) $(TESTS) 16 + 17 + clean: 18 + $(RM) sync_test $(OBJS) $(TESTS)
+46
tools/testing/selftests/sync/sw_sync.h
··· 1 + /* 2 + * sw_sync abstraction 3 + * 4 + * Copyright 2015-2016 Collabora Ltd. 5 + * 6 + * Based on the implementation from the Android Open Source Project, 7 + * 8 + * Copyright 2013 Google, Inc 9 + * 10 + * Permission is hereby granted, free of charge, to any person obtaining a 11 + * copy of this software and associated documentation files (the "Software"), 12 + * to deal in the Software without restriction, including without limitation 13 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 + * and/or sell copies of the Software, and to permit persons to whom the 15 + * Software is furnished to do so, subject to the following conditions: 16 + * 17 + * The above copyright notice and this permission notice shall be included in 18 + * all copies or substantial portions of the Software. 19 + * 20 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 24 + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25 + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 + * OTHER DEALINGS IN THE SOFTWARE. 27 + */ 28 + 29 + #ifndef SELFTESTS_SW_SYNC_H 30 + #define SELFTESTS_SW_SYNC_H 31 + 32 + /* 33 + * sw_sync is mainly intended for testing and should not be compiled into 34 + * production kernels 35 + */ 36 + 37 + int sw_sync_timeline_create(void); 38 + int sw_sync_timeline_is_valid(int fd); 39 + int sw_sync_timeline_inc(int fd, unsigned int count); 40 + void sw_sync_timeline_destroy(int fd); 41 + 42 + int sw_sync_fence_create(int fd, const char *name, unsigned int value); 43 + int sw_sync_fence_is_valid(int fd); 44 + void sw_sync_fence_destroy(int fd); 45 + 46 + #endif
+221
tools/testing/selftests/sync/sync.c
··· 1 + /* 2 + * sync / sw_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 + #include <fcntl.h> 29 + #include <malloc.h> 30 + #include <poll.h> 31 + #include <stdint.h> 32 + #include <string.h> 33 + #include <unistd.h> 34 + 35 + #include <sys/ioctl.h> 36 + #include <sys/stat.h> 37 + #include <sys/types.h> 38 + 39 + #include "sync.h" 40 + #include "sw_sync.h" 41 + 42 + #include <linux/sync_file.h> 43 + 44 + 45 + /* SW_SYNC ioctls */ 46 + struct sw_sync_create_fence_data { 47 + __u32 value; 48 + char name[32]; 49 + __s32 fence; 50 + }; 51 + 52 + #define SW_SYNC_IOC_MAGIC 'W' 53 + #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\ 54 + struct sw_sync_create_fence_data) 55 + #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32) 56 + 57 + 58 + int sync_wait(int fd, int timeout) 59 + { 60 + struct pollfd fds; 61 + 62 + fds.fd = fd; 63 + fds.events = POLLIN | POLLERR; 64 + 65 + return poll(&fds, 1, timeout); 66 + } 67 + 68 + int sync_merge(const char *name, int fd1, int fd2) 69 + { 70 + struct sync_merge_data data = {}; 71 + int err; 72 + 73 + data.fd2 = fd2; 74 + strncpy(data.name, name, sizeof(data.name) - 1); 75 + data.name[sizeof(data.name) - 1] = '\0'; 76 + 77 + err = ioctl(fd1, SYNC_IOC_MERGE, &data); 78 + if (err < 0) 79 + return err; 80 + 81 + return data.fence; 82 + } 83 + 84 + static struct sync_file_info *sync_file_info(int fd) 85 + { 86 + struct sync_file_info *info; 87 + struct sync_fence_info *fence_info; 88 + int err, num_fences; 89 + 90 + info = calloc(1, sizeof(*info)); 91 + if (info == NULL) 92 + return NULL; 93 + 94 + err = ioctl(fd, SYNC_IOC_FILE_INFO, info); 95 + if (err < 0) { 96 + free(info); 97 + return NULL; 98 + } 99 + 100 + num_fences = info->num_fences; 101 + 102 + if (num_fences) { 103 + info->flags = 0; 104 + info->num_fences = num_fences; 105 + 106 + fence_info = calloc(num_fences, sizeof(*fence_info)); 107 + if (!fence_info) { 108 + free(info); 109 + return NULL; 110 + } 111 + 112 + info->sync_fence_info = (uint64_t)fence_info; 113 + 114 + err = ioctl(fd, SYNC_IOC_FILE_INFO, info); 115 + if (err < 0) { 116 + free(fence_info); 117 + free(info); 118 + return NULL; 119 + } 120 + } 121 + 122 + return info; 123 + } 124 + 125 + static void sync_file_info_free(struct sync_file_info *info) 126 + { 127 + free((void *)info->sync_fence_info); 128 + free(info); 129 + } 130 + 131 + int sync_fence_size(int fd) 132 + { 133 + int count; 134 + struct sync_file_info *info = sync_file_info(fd); 135 + 136 + if (!info) 137 + return 0; 138 + 139 + count = info->num_fences; 140 + 141 + sync_file_info_free(info); 142 + 143 + return count; 144 + } 145 + 146 + int sync_fence_count_with_status(int fd, int status) 147 + { 148 + unsigned int i, count = 0; 149 + struct sync_fence_info *fence_info = NULL; 150 + struct sync_file_info *info = sync_file_info(fd); 151 + 152 + if (!info) 153 + return -1; 154 + 155 + fence_info = (struct sync_fence_info *)info->sync_fence_info; 156 + for (i = 0 ; i < info->num_fences ; i++) { 157 + if (fence_info[i].status == status) 158 + count++; 159 + } 160 + 161 + sync_file_info_free(info); 162 + 163 + return count; 164 + } 165 + 166 + int sw_sync_timeline_create(void) 167 + { 168 + return open("/sys/kernel/debug/sync/sw_sync", O_RDWR); 169 + } 170 + 171 + int sw_sync_timeline_inc(int fd, unsigned int count) 172 + { 173 + __u32 arg = count; 174 + 175 + return ioctl(fd, SW_SYNC_IOC_INC, &arg); 176 + } 177 + 178 + int sw_sync_timeline_is_valid(int fd) 179 + { 180 + int status; 181 + 182 + if (fd == -1) 183 + return 0; 184 + 185 + status = fcntl(fd, F_GETFD, 0); 186 + return (status >= 0); 187 + } 188 + 189 + void sw_sync_timeline_destroy(int fd) 190 + { 191 + if (sw_sync_timeline_is_valid(fd)) 192 + close(fd); 193 + } 194 + 195 + int sw_sync_fence_create(int fd, const char *name, unsigned int value) 196 + { 197 + struct sw_sync_create_fence_data data = {}; 198 + int err; 199 + 200 + data.value = value; 201 + strncpy(data.name, name, sizeof(data.name) - 1); 202 + data.name[sizeof(data.name) - 1] = '\0'; 203 + 204 + err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data); 205 + if (err < 0) 206 + return err; 207 + 208 + return data.fence; 209 + } 210 + 211 + int sw_sync_fence_is_valid(int fd) 212 + { 213 + /* Same code! */ 214 + return sw_sync_timeline_is_valid(fd); 215 + } 216 + 217 + void sw_sync_fence_destroy(int fd) 218 + { 219 + if (sw_sync_fence_is_valid(fd)) 220 + close(fd); 221 + }
+40
tools/testing/selftests/sync/sync.h
··· 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 SELFTESTS_SYNC_H 29 + #define SELFTESTS_SYNC_H 30 + 31 + #define FENCE_STATUS_ERROR (-1) 32 + #define FENCE_STATUS_ACTIVE (0) 33 + #define FENCE_STATUS_SIGNALED (1) 34 + 35 + int sync_wait(int fd, int timeout); 36 + int sync_merge(const char *name, int fd1, int fd2); 37 + int sync_fence_size(int fd); 38 + int sync_fence_count_with_status(int fd, int status); 39 + 40 + #endif
+74
tools/testing/selftests/sync/sync_alloc.c
··· 1 + /* 2 + * sync allocation tests 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 + #include "sync.h" 29 + #include "sw_sync.h" 30 + #include "synctest.h" 31 + 32 + int test_alloc_timeline(void) 33 + { 34 + int timeline, valid; 35 + 36 + timeline = sw_sync_timeline_create(); 37 + valid = sw_sync_timeline_is_valid(timeline); 38 + ASSERT(valid, "Failure allocating timeline\n"); 39 + 40 + sw_sync_timeline_destroy(timeline); 41 + return 0; 42 + } 43 + 44 + int test_alloc_fence(void) 45 + { 46 + int timeline, fence, valid; 47 + 48 + timeline = sw_sync_timeline_create(); 49 + valid = sw_sync_timeline_is_valid(timeline); 50 + ASSERT(valid, "Failure allocating timeline\n"); 51 + 52 + fence = sw_sync_fence_create(timeline, "allocFence", 1); 53 + valid = sw_sync_fence_is_valid(fence); 54 + ASSERT(valid, "Failure allocating fence\n"); 55 + 56 + sw_sync_fence_destroy(fence); 57 + sw_sync_timeline_destroy(timeline); 58 + return 0; 59 + } 60 + 61 + int test_alloc_fence_negative(void) 62 + { 63 + int fence, timeline; 64 + 65 + timeline = sw_sync_timeline_create(); 66 + ASSERT(timeline > 0, "Failure allocating timeline\n"); 67 + 68 + fence = sw_sync_fence_create(-1, "fence", 1); 69 + ASSERT(fence < 0, "Success allocating negative fence\n"); 70 + 71 + sw_sync_fence_destroy(fence); 72 + sw_sync_timeline_destroy(timeline); 73 + return 0; 74 + }
+71
tools/testing/selftests/sync/sync_test.c
··· 1 + /* 2 + * sync test runner 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 + #include <stdio.h> 29 + #include <unistd.h> 30 + #include <stdlib.h> 31 + #include <sys/types.h> 32 + #include <sys/wait.h> 33 + 34 + #include "synctest.h" 35 + 36 + static int run_test(int (*test)(void), char *name) 37 + { 38 + int result; 39 + pid_t childpid; 40 + 41 + fflush(stdout); 42 + childpid = fork(); 43 + 44 + if (childpid) { 45 + waitpid(childpid, &result, 0); 46 + if (WIFEXITED(result)) 47 + return WEXITSTATUS(result); 48 + return 1; 49 + } 50 + 51 + printf("[RUN]\tExecuting %s\n", name); 52 + exit(test()); 53 + } 54 + 55 + int main(void) 56 + { 57 + int err = 0; 58 + 59 + printf("[RUN]\tTesting sync framework\n"); 60 + 61 + err += RUN_TEST(test_alloc_timeline); 62 + err += RUN_TEST(test_alloc_fence); 63 + err += RUN_TEST(test_alloc_fence_negative); 64 + 65 + if (err) 66 + printf("[FAIL]\tsync errors: %d\n", err); 67 + else 68 + printf("[OK]\tsync\n"); 69 + 70 + return !!err; 71 + }
+47
tools/testing/selftests/sync/synctest.h
··· 1 + /* 2 + * sync tests 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 SELFTESTS_SYNCTEST_H 29 + #define SELFTESTS_SYNCTEST_H 30 + 31 + #include <stdio.h> 32 + 33 + #define ASSERT(cond, msg) do { \ 34 + if (!(cond)) { \ 35 + printf("[BAD]\t%s", (msg)); \ 36 + return 1; \ 37 + } \ 38 + } while (0) 39 + 40 + #define RUN_TEST(x) run_test((x), #x) 41 + 42 + /* Allocation tests */ 43 + int test_alloc_timeline(void); 44 + int test_alloc_fence(void); 45 + int test_alloc_fence_negative(void); 46 + 47 + #endif