Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 1 | /* |
Gustavo Padovan | e912c88 | 2016-08-11 12:26:42 -0300 | [diff] [blame] | 2 | * Sync File validation framework |
Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/uaccess.h> |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 20 | #include <linux/slab.h> |
Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 21 | #include <linux/sync_file.h> |
| 22 | |
Gustavo Padovan | 1fe82e2 | 2016-05-31 16:59:12 -0300 | [diff] [blame] | 23 | #include "sync_debug.h" |
Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 24 | |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 25 | #define CREATE_TRACE_POINTS |
Gustavo Padovan | a04f915 | 2016-08-11 12:26:41 -0300 | [diff] [blame] | 26 | #include "sync_trace.h" |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 27 | |
Gustavo Padovan | fc0c9a0 | 2016-08-11 13:45:53 -0300 | [diff] [blame] | 28 | /* |
| 29 | * SW SYNC validation framework |
| 30 | * |
| 31 | * A sync object driver that uses a 32bit counter to coordinate |
| 32 | * synchronization. Useful when there is no hardware primitive backing |
| 33 | * the synchronization. |
| 34 | * |
| 35 | * To start the framework just open: |
| 36 | * |
| 37 | * <debugfs>/sync/sw_sync |
| 38 | * |
| 39 | * That will create a sync timeline, all fences created under this timeline |
| 40 | * file descriptor will belong to the this timeline. |
| 41 | * |
| 42 | * The 'sw_sync' file can be opened many times as to create different |
| 43 | * timelines. |
| 44 | * |
| 45 | * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct |
| 46 | * sw_sync_ioctl_create_fence as parameter. |
| 47 | * |
| 48 | * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used |
| 49 | * with the increment as u32. This will update the last signaled value |
| 50 | * from the timeline and signal any fence that has a seqno smaller or equal |
| 51 | * to it. |
| 52 | * |
| 53 | * struct sw_sync_ioctl_create_fence |
| 54 | * @value: the seqno to initialise the fence with |
| 55 | * @name: the name of the new sync point |
| 56 | * @fence: return the fd of the new sync_file with the created fence |
| 57 | */ |
Gustavo Padovan | 6f65aa8 | 2016-05-31 16:59:08 -0300 | [diff] [blame] | 58 | struct sw_sync_create_fence_data { |
| 59 | __u32 value; |
| 60 | char name[32]; |
| 61 | __s32 fence; /* fd of new fence */ |
| 62 | }; |
| 63 | |
| 64 | #define SW_SYNC_IOC_MAGIC 'W' |
| 65 | |
| 66 | #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\ |
| 67 | struct sw_sync_create_fence_data) |
Gustavo Padovan | fc0c9a0 | 2016-08-11 13:45:53 -0300 | [diff] [blame] | 68 | |
Gustavo Padovan | 6f65aa8 | 2016-05-31 16:59:08 -0300 | [diff] [blame] | 69 | #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32) |
| 70 | |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 71 | static const struct fence_ops timeline_fence_ops; |
| 72 | |
| 73 | static inline struct sync_pt *fence_to_sync_pt(struct fence *fence) |
| 74 | { |
| 75 | if (fence->ops != &timeline_fence_ops) |
| 76 | return NULL; |
| 77 | return container_of(fence, struct sync_pt, base); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * sync_timeline_create() - creates a sync object |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 82 | * @name: sync_timeline name |
| 83 | * |
| 84 | * Creates a new sync_timeline. Returns the sync_timeline object or NULL in |
| 85 | * case of error. |
| 86 | */ |
Gustavo Padovan | b9bc2b7 | 2016-05-31 16:59:11 -0300 | [diff] [blame] | 87 | struct sync_timeline *sync_timeline_create(const char *name) |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 88 | { |
| 89 | struct sync_timeline *obj; |
| 90 | |
| 91 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 92 | if (!obj) |
| 93 | return NULL; |
| 94 | |
| 95 | kref_init(&obj->kref); |
| 96 | obj->context = fence_context_alloc(1); |
| 97 | strlcpy(obj->name, name, sizeof(obj->name)); |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 98 | |
| 99 | INIT_LIST_HEAD(&obj->child_list_head); |
| 100 | INIT_LIST_HEAD(&obj->active_list_head); |
| 101 | spin_lock_init(&obj->child_list_lock); |
| 102 | |
| 103 | sync_timeline_debug_add(obj); |
| 104 | |
| 105 | return obj; |
| 106 | } |
| 107 | |
| 108 | static void sync_timeline_free(struct kref *kref) |
| 109 | { |
| 110 | struct sync_timeline *obj = |
| 111 | container_of(kref, struct sync_timeline, kref); |
| 112 | |
| 113 | sync_timeline_debug_remove(obj); |
| 114 | |
| 115 | kfree(obj); |
| 116 | } |
| 117 | |
| 118 | static void sync_timeline_get(struct sync_timeline *obj) |
| 119 | { |
| 120 | kref_get(&obj->kref); |
| 121 | } |
| 122 | |
| 123 | static void sync_timeline_put(struct sync_timeline *obj) |
| 124 | { |
| 125 | kref_put(&obj->kref, sync_timeline_free); |
| 126 | } |
| 127 | |
| 128 | /** |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 129 | * sync_timeline_signal() - signal a status change on a sync_timeline |
| 130 | * @obj: sync_timeline to signal |
| 131 | * @inc: num to increment on timeline->value |
| 132 | * |
| 133 | * A sync implementation should call this any time one of it's fences |
| 134 | * has signaled or has an error condition. |
| 135 | */ |
| 136 | static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) |
| 137 | { |
| 138 | unsigned long flags; |
| 139 | struct sync_pt *pt, *next; |
| 140 | |
| 141 | trace_sync_timeline(obj); |
| 142 | |
| 143 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 144 | |
| 145 | obj->value += inc; |
| 146 | |
| 147 | list_for_each_entry_safe(pt, next, &obj->active_list_head, |
| 148 | active_list) { |
| 149 | if (fence_is_signaled_locked(&pt->base)) |
| 150 | list_del_init(&pt->active_list); |
| 151 | } |
| 152 | |
| 153 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * sync_pt_create() - creates a sync pt |
| 158 | * @parent: fence's parent sync_timeline |
| 159 | * @size: size to allocate for this pt |
| 160 | * @inc: value of the fence |
| 161 | * |
| 162 | * Creates a new sync_pt as a child of @parent. @size bytes will be |
| 163 | * allocated allowing for implementation specific data to be kept after |
| 164 | * the generic sync_timeline struct. Returns the sync_pt object or |
| 165 | * NULL in case of error. |
| 166 | */ |
| 167 | static struct sync_pt *sync_pt_create(struct sync_timeline *obj, int size, |
| 168 | unsigned int value) |
| 169 | { |
| 170 | unsigned long flags; |
| 171 | struct sync_pt *pt; |
| 172 | |
| 173 | if (size < sizeof(*pt)) |
| 174 | return NULL; |
| 175 | |
| 176 | pt = kzalloc(size, GFP_KERNEL); |
| 177 | if (!pt) |
| 178 | return NULL; |
| 179 | |
| 180 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 181 | sync_timeline_get(obj); |
| 182 | fence_init(&pt->base, &timeline_fence_ops, &obj->child_list_lock, |
| 183 | obj->context, value); |
| 184 | list_add_tail(&pt->child_list, &obj->child_list_head); |
| 185 | INIT_LIST_HEAD(&pt->active_list); |
| 186 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
| 187 | return pt; |
| 188 | } |
| 189 | |
| 190 | static const char *timeline_fence_get_driver_name(struct fence *fence) |
| 191 | { |
Gustavo Padovan | b9bc2b7 | 2016-05-31 16:59:11 -0300 | [diff] [blame] | 192 | return "sw_sync"; |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static const char *timeline_fence_get_timeline_name(struct fence *fence) |
| 196 | { |
| 197 | struct sync_timeline *parent = fence_parent(fence); |
| 198 | |
| 199 | return parent->name; |
| 200 | } |
| 201 | |
| 202 | static void timeline_fence_release(struct fence *fence) |
| 203 | { |
| 204 | struct sync_pt *pt = fence_to_sync_pt(fence); |
| 205 | struct sync_timeline *parent = fence_parent(fence); |
| 206 | unsigned long flags; |
| 207 | |
| 208 | spin_lock_irqsave(fence->lock, flags); |
| 209 | list_del(&pt->child_list); |
Gustavo Padovan | a4ebee6 | 2016-08-11 12:26:40 -0300 | [diff] [blame] | 210 | if (!list_empty(&pt->active_list)) |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 211 | list_del(&pt->active_list); |
| 212 | spin_unlock_irqrestore(fence->lock, flags); |
| 213 | |
| 214 | sync_timeline_put(parent); |
| 215 | fence_free(fence); |
| 216 | } |
| 217 | |
| 218 | static bool timeline_fence_signaled(struct fence *fence) |
| 219 | { |
| 220 | struct sync_timeline *parent = fence_parent(fence); |
| 221 | |
| 222 | return (fence->seqno > parent->value) ? false : true; |
| 223 | } |
| 224 | |
| 225 | static bool timeline_fence_enable_signaling(struct fence *fence) |
| 226 | { |
| 227 | struct sync_pt *pt = fence_to_sync_pt(fence); |
| 228 | struct sync_timeline *parent = fence_parent(fence); |
| 229 | |
| 230 | if (timeline_fence_signaled(fence)) |
| 231 | return false; |
| 232 | |
| 233 | list_add_tail(&pt->active_list, &parent->active_list_head); |
| 234 | return true; |
| 235 | } |
| 236 | |
Dmitry Torokhov | f271994 | 2015-09-08 17:30:52 -0700 | [diff] [blame] | 237 | static void timeline_fence_disable_signaling(struct fence *fence) |
| 238 | { |
| 239 | struct sync_pt *pt = container_of(fence, struct sync_pt, base); |
| 240 | |
| 241 | list_del_init(&pt->active_list); |
| 242 | } |
| 243 | |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 244 | static void timeline_fence_value_str(struct fence *fence, |
| 245 | char *str, int size) |
| 246 | { |
| 247 | snprintf(str, size, "%d", fence->seqno); |
| 248 | } |
| 249 | |
| 250 | static void timeline_fence_timeline_value_str(struct fence *fence, |
| 251 | char *str, int size) |
| 252 | { |
| 253 | struct sync_timeline *parent = fence_parent(fence); |
| 254 | |
| 255 | snprintf(str, size, "%d", parent->value); |
| 256 | } |
| 257 | |
| 258 | static const struct fence_ops timeline_fence_ops = { |
| 259 | .get_driver_name = timeline_fence_get_driver_name, |
| 260 | .get_timeline_name = timeline_fence_get_timeline_name, |
| 261 | .enable_signaling = timeline_fence_enable_signaling, |
Dmitry Torokhov | f271994 | 2015-09-08 17:30:52 -0700 | [diff] [blame] | 262 | .disable_signaling = timeline_fence_disable_signaling, |
Gustavo Padovan | aff9da1 | 2016-05-31 16:59:07 -0300 | [diff] [blame] | 263 | .signaled = timeline_fence_signaled, |
| 264 | .wait = fence_default_wait, |
| 265 | .release = timeline_fence_release, |
| 266 | .fence_value_str = timeline_fence_value_str, |
| 267 | .timeline_value_str = timeline_fence_timeline_value_str, |
| 268 | }; |
| 269 | |
Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 270 | /* |
| 271 | * *WARNING* |
| 272 | * |
| 273 | * improper use of this can result in deadlocking kernel drivers from userspace. |
| 274 | */ |
| 275 | |
| 276 | /* opening sw_sync create a new sync obj */ |
| 277 | static int sw_sync_debugfs_open(struct inode *inode, struct file *file) |
| 278 | { |
| 279 | struct sync_timeline *obj; |
| 280 | char task_comm[TASK_COMM_LEN]; |
| 281 | |
| 282 | get_task_comm(task_comm, current); |
| 283 | |
Gustavo Padovan | b9bc2b7 | 2016-05-31 16:59:11 -0300 | [diff] [blame] | 284 | obj = sync_timeline_create(task_comm); |
Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 285 | if (!obj) |
| 286 | return -ENOMEM; |
| 287 | |
| 288 | file->private_data = obj; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static int sw_sync_debugfs_release(struct inode *inode, struct file *file) |
| 294 | { |
| 295 | struct sync_timeline *obj = file->private_data; |
| 296 | |
Gustavo Padovan | 7111023 | 2016-05-31 16:59:10 -0300 | [diff] [blame] | 297 | smp_wmb(); |
| 298 | |
| 299 | sync_timeline_put(obj); |
Gustavo Padovan | 1867a23 | 2016-05-31 16:59:05 -0300 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static long sw_sync_ioctl_create_fence(struct sync_timeline *obj, |
| 304 | unsigned long arg) |
| 305 | { |
| 306 | int fd = get_unused_fd_flags(O_CLOEXEC); |
| 307 | int err; |
| 308 | struct sync_pt *pt; |
| 309 | struct sync_file *sync_file; |
| 310 | struct sw_sync_create_fence_data data; |
| 311 | |
| 312 | if (fd < 0) |
| 313 | return fd; |
| 314 | |
| 315 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) { |
| 316 | err = -EFAULT; |
| 317 | goto err; |
| 318 | } |
| 319 | |
| 320 | pt = sync_pt_create(obj, sizeof(*pt), data.value); |
| 321 | if (!pt) { |
| 322 | err = -ENOMEM; |
| 323 | goto err; |
| 324 | } |
| 325 | |
| 326 | sync_file = sync_file_create(&pt->base); |
| 327 | if (!sync_file) { |
| 328 | fence_put(&pt->base); |
| 329 | err = -ENOMEM; |
| 330 | goto err; |
| 331 | } |
| 332 | |
| 333 | data.fence = fd; |
| 334 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { |
| 335 | fput(sync_file->file); |
| 336 | err = -EFAULT; |
| 337 | goto err; |
| 338 | } |
| 339 | |
| 340 | fd_install(fd, sync_file->file); |
| 341 | |
| 342 | return 0; |
| 343 | |
| 344 | err: |
| 345 | put_unused_fd(fd); |
| 346 | return err; |
| 347 | } |
| 348 | |
| 349 | static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg) |
| 350 | { |
| 351 | u32 value; |
| 352 | |
| 353 | if (copy_from_user(&value, (void __user *)arg, sizeof(value))) |
| 354 | return -EFAULT; |
| 355 | |
| 356 | sync_timeline_signal(obj, value); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static long sw_sync_ioctl(struct file *file, unsigned int cmd, |
| 362 | unsigned long arg) |
| 363 | { |
| 364 | struct sync_timeline *obj = file->private_data; |
| 365 | |
| 366 | switch (cmd) { |
| 367 | case SW_SYNC_IOC_CREATE_FENCE: |
| 368 | return sw_sync_ioctl_create_fence(obj, arg); |
| 369 | |
| 370 | case SW_SYNC_IOC_INC: |
| 371 | return sw_sync_ioctl_inc(obj, arg); |
| 372 | |
| 373 | default: |
| 374 | return -ENOTTY; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | const struct file_operations sw_sync_debugfs_fops = { |
| 379 | .open = sw_sync_debugfs_open, |
| 380 | .release = sw_sync_debugfs_release, |
| 381 | .unlocked_ioctl = sw_sync_ioctl, |
| 382 | .compat_ioctl = sw_sync_ioctl, |
| 383 | }; |