Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/sync.c |
| 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 | |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 17 | #include <linux/debugfs.h> |
Erik Gilling | 8edb4ad | 2013-02-28 16:43:06 -0800 | [diff] [blame] | 18 | #include <linux/export.h> |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 19 | #include <linux/file.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/kernel.h> |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 22 | #include <linux/poll.h> |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 23 | #include <linux/sched.h> |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/anon_inodes.h> |
| 28 | |
| 29 | #include "sync.h" |
| 30 | |
Erik Gilling | b699a64 | 2013-02-28 16:43:23 -0800 | [diff] [blame] | 31 | #define CREATE_TRACE_POINTS |
| 32 | #include "trace/sync.h" |
| 33 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 34 | static const struct fence_ops android_fence_ops; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 35 | static const struct file_operations sync_file_fops; |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 36 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 37 | struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops, |
| 38 | int size, const char *name) |
| 39 | { |
| 40 | struct sync_timeline *obj; |
| 41 | |
| 42 | if (size < sizeof(struct sync_timeline)) |
| 43 | return NULL; |
| 44 | |
| 45 | obj = kzalloc(size, GFP_KERNEL); |
Ioana Ciornei | 375fb53 | 2015-11-01 16:38:20 +0200 | [diff] [blame] | 46 | if (!obj) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 47 | return NULL; |
| 48 | |
Erik Gilling | c5b86b7 | 2013-02-28 16:43:11 -0800 | [diff] [blame] | 49 | kref_init(&obj->kref); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 50 | obj->ops = ops; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 51 | obj->context = fence_context_alloc(1); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 52 | strlcpy(obj->name, name, sizeof(obj->name)); |
| 53 | |
| 54 | INIT_LIST_HEAD(&obj->child_list_head); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 55 | INIT_LIST_HEAD(&obj->active_list_head); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 56 | spin_lock_init(&obj->child_list_lock); |
| 57 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 58 | sync_timeline_debug_add(obj); |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 59 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 60 | return obj; |
| 61 | } |
Erik Gilling | 8edb4ad | 2013-02-28 16:43:06 -0800 | [diff] [blame] | 62 | EXPORT_SYMBOL(sync_timeline_create); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 63 | |
Erik Gilling | c5b86b7 | 2013-02-28 16:43:11 -0800 | [diff] [blame] | 64 | static void sync_timeline_free(struct kref *kref) |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 65 | { |
Erik Gilling | c5b86b7 | 2013-02-28 16:43:11 -0800 | [diff] [blame] | 66 | struct sync_timeline *obj = |
| 67 | container_of(kref, struct sync_timeline, kref); |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 68 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 69 | sync_timeline_debug_remove(obj); |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 70 | |
| 71 | kfree(obj); |
| 72 | } |
| 73 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 74 | static void sync_timeline_get(struct sync_timeline *obj) |
| 75 | { |
| 76 | kref_get(&obj->kref); |
| 77 | } |
| 78 | |
| 79 | static void sync_timeline_put(struct sync_timeline *obj) |
| 80 | { |
| 81 | kref_put(&obj->kref, sync_timeline_free); |
| 82 | } |
| 83 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 84 | void sync_timeline_destroy(struct sync_timeline *obj) |
| 85 | { |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 86 | obj->destroyed = true; |
Niv Yehezkel | 2960660 | 2014-05-24 10:28:07 -0400 | [diff] [blame] | 87 | /* |
| 88 | * Ensure timeline is marked as destroyed before |
| 89 | * changing timeline's fences status. |
| 90 | */ |
Prakash Kamliya | ac5b705 | 2014-02-04 16:08:35 -0800 | [diff] [blame] | 91 | smp_wmb(); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 92 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 93 | sync_timeline_put(obj); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 94 | } |
Erik Gilling | 8edb4ad | 2013-02-28 16:43:06 -0800 | [diff] [blame] | 95 | EXPORT_SYMBOL(sync_timeline_destroy); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 96 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 97 | void sync_timeline_signal(struct sync_timeline *obj) |
| 98 | { |
| 99 | unsigned long flags; |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 100 | struct fence *fence, *next; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 101 | |
Erik Gilling | b699a64 | 2013-02-28 16:43:23 -0800 | [diff] [blame] | 102 | trace_sync_timeline(obj); |
| 103 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 104 | spin_lock_irqsave(&obj->child_list_lock, flags); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 105 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 106 | list_for_each_entry_safe(fence, next, &obj->active_list_head, |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 107 | active_list) { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 108 | if (fence_is_signaled_locked(fence)) |
| 109 | list_del_init(&fence->active_list); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 112 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 113 | } |
Erik Gilling | 8edb4ad | 2013-02-28 16:43:06 -0800 | [diff] [blame] | 114 | EXPORT_SYMBOL(sync_timeline_signal); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 115 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 116 | struct fence *sync_pt_create(struct sync_timeline *obj, int size) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 117 | { |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 118 | unsigned long flags; |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 119 | struct fence *fence; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 120 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 121 | if (size < sizeof(*fence)) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 122 | return NULL; |
| 123 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 124 | fence = kzalloc(size, GFP_KERNEL); |
| 125 | if (!fence) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 126 | return NULL; |
| 127 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 128 | spin_lock_irqsave(&obj->child_list_lock, flags); |
| 129 | sync_timeline_get(obj); |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 130 | fence_init(fence, &android_fence_ops, &obj->child_list_lock, |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 131 | obj->context, ++obj->value); |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 132 | list_add_tail(&fence->child_list, &obj->child_list_head); |
| 133 | INIT_LIST_HEAD(&fence->active_list); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 134 | spin_unlock_irqrestore(&obj->child_list_lock, flags); |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 135 | return fence; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 136 | } |
Erik Gilling | 8edb4ad | 2013-02-28 16:43:06 -0800 | [diff] [blame] | 137 | EXPORT_SYMBOL(sync_pt_create); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 138 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 139 | static struct sync_file *sync_file_alloc(int size, const char *name) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 140 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 141 | struct sync_file *sync_file; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 142 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 143 | sync_file = kzalloc(size, GFP_KERNEL); |
| 144 | if (!sync_file) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 145 | return NULL; |
| 146 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 147 | sync_file->file = anon_inode_getfile("sync_file", &sync_file_fops, |
| 148 | sync_file, 0); |
| 149 | if (IS_ERR(sync_file->file)) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 150 | goto err; |
| 151 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 152 | kref_init(&sync_file->kref); |
| 153 | strlcpy(sync_file->name, name, sizeof(sync_file->name)); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 154 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 155 | init_waitqueue_head(&sync_file->wq); |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 156 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 157 | return sync_file; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 158 | |
| 159 | err: |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 160 | kfree(sync_file); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 164 | static void fence_check_cb_func(struct fence *f, struct fence_cb *cb) |
| 165 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 166 | struct sync_file_cb *check; |
| 167 | struct sync_file *sync_file; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 168 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 169 | check = container_of(cb, struct sync_file_cb, cb); |
| 170 | sync_file = check->sync_file; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 171 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 172 | if (atomic_dec_and_test(&sync_file->status)) |
| 173 | wake_up_all(&sync_file->wq); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Gustavo Padovan | 696f74c | 2016-04-28 10:46:50 -0300 | [diff] [blame^] | 176 | /** |
| 177 | * sync_fence_create() - creates a sync fence |
| 178 | * @name: name of fence to create |
| 179 | * @fence: fence to add to the sync_fence |
| 180 | * |
| 181 | * Creates a sync_file containg @fence. Once this is called, the sync_file |
| 182 | * takes ownership of @fence. |
| 183 | */ |
Gustavo Padovan | d52ef2c | 2016-01-21 10:49:24 -0200 | [diff] [blame] | 184 | struct sync_file *sync_file_create(const char *name, struct fence *fence) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 185 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 186 | struct sync_file *sync_file; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 187 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 188 | sync_file = sync_file_alloc(offsetof(struct sync_file, cbs[1]), |
| 189 | name); |
| 190 | if (!sync_file) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 191 | return NULL; |
| 192 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 193 | sync_file->num_fences = 1; |
| 194 | atomic_set(&sync_file->status, 1); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 195 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 196 | sync_file->cbs[0].fence = fence; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 197 | sync_file->cbs[0].sync_file = sync_file; |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 198 | if (fence_add_callback(fence, &sync_file->cbs[0].cb, |
| 199 | fence_check_cb_func)) |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 200 | atomic_dec(&sync_file->status); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 201 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 202 | sync_file_debug_add(sync_file); |
Erik Gilling | eeb2f57 | 2013-02-28 16:43:19 -0800 | [diff] [blame] | 203 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 204 | return sync_file; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 205 | } |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 206 | EXPORT_SYMBOL(sync_file_create); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 207 | |
Gustavo Padovan | 696f74c | 2016-04-28 10:46:50 -0300 | [diff] [blame^] | 208 | /** |
| 209 | * sync_file_fdget() - get a sync_file from an fd |
| 210 | * @fd: fd referencing a fence |
| 211 | * |
| 212 | * Ensures @fd references a valid sync_file, increments the refcount of the |
| 213 | * backing file. Returns the sync_file or NULL in case of error. |
| 214 | */ |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 215 | struct sync_file *sync_file_fdget(int fd) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 216 | { |
| 217 | struct file *file = fget(fd); |
| 218 | |
Ioana Ciornei | 375fb53 | 2015-11-01 16:38:20 +0200 | [diff] [blame] | 219 | if (!file) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 220 | return NULL; |
| 221 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 222 | if (file->f_op != &sync_file_fops) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 223 | goto err; |
| 224 | |
| 225 | return file->private_data; |
| 226 | |
| 227 | err: |
| 228 | fput(file); |
| 229 | return NULL; |
| 230 | } |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 231 | EXPORT_SYMBOL(sync_file_fdget); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 232 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 233 | static void sync_file_add_pt(struct sync_file *sync_file, int *i, |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 234 | struct fence *fence) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 235 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 236 | sync_file->cbs[*i].fence = fence; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 237 | sync_file->cbs[*i].sync_file = sync_file; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 238 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 239 | if (!fence_add_callback(fence, &sync_file->cbs[*i].cb, |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 240 | fence_check_cb_func)) { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 241 | fence_get(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 242 | (*i)++; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 243 | } |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Gustavo Padovan | 696f74c | 2016-04-28 10:46:50 -0300 | [diff] [blame^] | 246 | /** |
| 247 | * sync_file_merge() - merge two sync_files |
| 248 | * @name: name of new fence |
| 249 | * @a: sync_file a |
| 250 | * @b: sync_file b |
| 251 | * |
| 252 | * Creates a new sync_file which contains copies of all the fences in both |
| 253 | * @a and @b. @a and @b remain valid, independent sync_file. Returns the |
| 254 | * new merged sync_file or NULL in case of error. |
| 255 | */ |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 256 | struct sync_file *sync_file_merge(const char *name, |
| 257 | struct sync_file *a, struct sync_file *b) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 258 | { |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 259 | int num_fences = a->num_fences + b->num_fences; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 260 | struct sync_file *sync_file; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 261 | int i, i_a, i_b; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 262 | unsigned long size = offsetof(struct sync_file, cbs[num_fences]); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 263 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 264 | sync_file = sync_file_alloc(size, name); |
| 265 | if (!sync_file) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 266 | return NULL; |
| 267 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 268 | atomic_set(&sync_file->status, num_fences); |
Ørjan Eide | 713648f | 2013-02-28 16:43:24 -0800 | [diff] [blame] | 269 | |
Erik Gilling | eeb2f57 | 2013-02-28 16:43:19 -0800 | [diff] [blame] | 270 | /* |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 271 | * Assume sync_file a and b are both ordered and have no |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 272 | * duplicates with the same context. |
| 273 | * |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 274 | * If a sync_file can only be created with sync_file_merge |
| 275 | * and sync_file_create, this is a reasonable assumption. |
Erik Gilling | eeb2f57 | 2013-02-28 16:43:19 -0800 | [diff] [blame] | 276 | */ |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 277 | for (i = i_a = i_b = 0; i_a < a->num_fences && i_b < b->num_fences; ) { |
Gustavo Padovan | c88b26d | 2016-01-21 10:49:20 -0200 | [diff] [blame] | 278 | struct fence *pt_a = a->cbs[i_a].fence; |
| 279 | struct fence *pt_b = b->cbs[i_b].fence; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 280 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 281 | if (pt_a->context < pt_b->context) { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 282 | sync_file_add_pt(sync_file, &i, pt_a); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 283 | |
| 284 | i_a++; |
| 285 | } else if (pt_a->context > pt_b->context) { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 286 | sync_file_add_pt(sync_file, &i, pt_b); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 287 | |
| 288 | i_b++; |
| 289 | } else { |
| 290 | if (pt_a->seqno - pt_b->seqno <= INT_MAX) |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 291 | sync_file_add_pt(sync_file, &i, pt_a); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 292 | else |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 293 | sync_file_add_pt(sync_file, &i, pt_b); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 294 | |
| 295 | i_a++; |
| 296 | i_b++; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | for (; i_a < a->num_fences; i_a++) |
Gustavo Padovan | c88b26d | 2016-01-21 10:49:20 -0200 | [diff] [blame] | 301 | sync_file_add_pt(sync_file, &i, a->cbs[i_a].fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 302 | |
| 303 | for (; i_b < b->num_fences; i_b++) |
Gustavo Padovan | c88b26d | 2016-01-21 10:49:20 -0200 | [diff] [blame] | 304 | sync_file_add_pt(sync_file, &i, b->cbs[i_b].fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 305 | |
| 306 | if (num_fences > i) |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 307 | atomic_sub(num_fences - i, &sync_file->status); |
| 308 | sync_file->num_fences = i; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 309 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 310 | sync_file_debug_add(sync_file); |
| 311 | return sync_file; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 312 | } |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 313 | EXPORT_SYMBOL(sync_file_merge); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 314 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 315 | static const char *android_fence_get_driver_name(struct fence *fence) |
| 316 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 317 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 318 | |
| 319 | return parent->ops->driver_name; |
| 320 | } |
| 321 | |
| 322 | static const char *android_fence_get_timeline_name(struct fence *fence) |
| 323 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 324 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 325 | |
| 326 | return parent->name; |
| 327 | } |
| 328 | |
| 329 | static void android_fence_release(struct fence *fence) |
| 330 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 331 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 332 | unsigned long flags; |
| 333 | |
| 334 | spin_lock_irqsave(fence->lock, flags); |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 335 | list_del(&fence->child_list); |
| 336 | if (WARN_ON_ONCE(!list_empty(&fence->active_list))) |
| 337 | list_del(&fence->active_list); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 338 | spin_unlock_irqrestore(fence->lock, flags); |
| 339 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 340 | sync_timeline_put(parent); |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 341 | fence_free(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static bool android_fence_signaled(struct fence *fence) |
| 345 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 346 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 347 | int ret; |
| 348 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 349 | ret = parent->ops->has_signaled(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 350 | if (ret < 0) |
| 351 | fence->status = ret; |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static bool android_fence_enable_signaling(struct fence *fence) |
| 356 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 357 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 358 | |
| 359 | if (android_fence_signaled(fence)) |
| 360 | return false; |
| 361 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 362 | list_add_tail(&fence->active_list, &parent->active_list_head); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 366 | static void android_fence_value_str(struct fence *fence, |
| 367 | char *str, int size) |
| 368 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 369 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 370 | |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 371 | if (!parent->ops->fence_value_str) { |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 372 | if (size) |
| 373 | *str = 0; |
| 374 | return; |
| 375 | } |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 376 | parent->ops->fence_value_str(fence, str, size); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void android_fence_timeline_value_str(struct fence *fence, |
| 380 | char *str, int size) |
| 381 | { |
Gustavo Padovan | b55b54b | 2016-01-21 10:49:21 -0200 | [diff] [blame] | 382 | struct sync_timeline *parent = fence_parent(fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 383 | |
| 384 | if (!parent->ops->timeline_value_str) { |
| 385 | if (size) |
| 386 | *str = 0; |
| 387 | return; |
| 388 | } |
| 389 | parent->ops->timeline_value_str(parent, str, size); |
| 390 | } |
| 391 | |
| 392 | static const struct fence_ops android_fence_ops = { |
| 393 | .get_driver_name = android_fence_get_driver_name, |
| 394 | .get_timeline_name = android_fence_get_timeline_name, |
| 395 | .enable_signaling = android_fence_enable_signaling, |
| 396 | .signaled = android_fence_signaled, |
| 397 | .wait = fence_default_wait, |
| 398 | .release = android_fence_release, |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 399 | .fence_value_str = android_fence_value_str, |
| 400 | .timeline_value_str = android_fence_timeline_value_str, |
| 401 | }; |
| 402 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 403 | static void sync_file_free(struct kref *kref) |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 404 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 405 | struct sync_file *sync_file = container_of(kref, struct sync_file, |
| 406 | kref); |
Dmitry Torokhov | 699f685 | 2015-12-14 17:34:08 -0800 | [diff] [blame] | 407 | int i; |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 408 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 409 | for (i = 0; i < sync_file->num_fences; ++i) { |
Gustavo Padovan | c88b26d | 2016-01-21 10:49:20 -0200 | [diff] [blame] | 410 | fence_remove_callback(sync_file->cbs[i].fence, |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 411 | &sync_file->cbs[i].cb); |
Gustavo Padovan | c88b26d | 2016-01-21 10:49:20 -0200 | [diff] [blame] | 412 | fence_put(sync_file->cbs[i].fence); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 413 | } |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 414 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 415 | kfree(sync_file); |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 418 | static int sync_file_release(struct inode *inode, struct file *file) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 419 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 420 | struct sync_file *sync_file = file->private_data; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 421 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 422 | sync_file_debug_remove(sync_file); |
Erik Gilling | cc3c5cd | 2013-02-28 16:43:08 -0800 | [diff] [blame] | 423 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 424 | kref_put(&sync_file->kref, sync_file_free); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 428 | static unsigned int sync_file_poll(struct file *file, poll_table *wait) |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 429 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 430 | struct sync_file *sync_file = file->private_data; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 431 | int status; |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 432 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 433 | poll_wait(file, &sync_file->wq, wait); |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 434 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 435 | status = atomic_read(&sync_file->status); |
Erik Gilling | c679212 | 2013-02-28 16:43:18 -0800 | [diff] [blame] | 436 | |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 437 | if (!status) |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 438 | return POLLIN; |
Janani Ravichandran | 5f9001d | 2016-02-18 17:39:11 -0500 | [diff] [blame] | 439 | if (status < 0) |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 440 | return POLLERR; |
Lucas Tanure | 6a44b50 | 2014-07-15 00:32:35 -0300 | [diff] [blame] | 441 | return 0; |
Erik Gilling | 57b505b | 2013-02-28 16:43:04 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 444 | static long sync_file_ioctl_merge(struct sync_file *sync_file, |
| 445 | unsigned long arg) |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 446 | { |
Yann Droneaud | 9c6cd3b | 2013-08-15 15:10:54 +0200 | [diff] [blame] | 447 | int fd = get_unused_fd_flags(O_CLOEXEC); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 448 | int err; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 449 | struct sync_file *fence2, *fence3; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 450 | struct sync_merge_data data; |
| 451 | |
Rebecca Schultz Zavin | 92ea915a | 2013-02-28 16:43:12 -0800 | [diff] [blame] | 452 | if (fd < 0) |
| 453 | return fd; |
| 454 | |
| 455 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) { |
| 456 | err = -EFAULT; |
| 457 | goto err_put_fd; |
| 458 | } |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 459 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 460 | if (data.flags || data.pad) { |
| 461 | err = -EINVAL; |
| 462 | goto err_put_fd; |
| 463 | } |
| 464 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 465 | fence2 = sync_file_fdget(data.fd2); |
Ioana Ciornei | 375fb53 | 2015-11-01 16:38:20 +0200 | [diff] [blame] | 466 | if (!fence2) { |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 467 | err = -ENOENT; |
| 468 | goto err_put_fd; |
| 469 | } |
| 470 | |
| 471 | data.name[sizeof(data.name) - 1] = '\0'; |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 472 | fence3 = sync_file_merge(data.name, sync_file, fence2); |
Ioana Ciornei | 375fb53 | 2015-11-01 16:38:20 +0200 | [diff] [blame] | 473 | if (!fence3) { |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 474 | err = -ENOMEM; |
| 475 | goto err_put_fence2; |
| 476 | } |
| 477 | |
| 478 | data.fence = fd; |
| 479 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { |
| 480 | err = -EFAULT; |
| 481 | goto err_put_fence3; |
| 482 | } |
| 483 | |
Gustavo Padovan | 2a7c1db | 2016-04-28 10:46:49 -0300 | [diff] [blame] | 484 | fd_install(fd, fence3->file); |
| 485 | fput(fence2->file); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 486 | return 0; |
| 487 | |
| 488 | err_put_fence3: |
Gustavo Padovan | 2a7c1db | 2016-04-28 10:46:49 -0300 | [diff] [blame] | 489 | fput(fence3->file); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 490 | |
| 491 | err_put_fence2: |
Gustavo Padovan | 2a7c1db | 2016-04-28 10:46:49 -0300 | [diff] [blame] | 492 | fput(fence2->file); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 493 | |
| 494 | err_put_fd: |
| 495 | put_unused_fd(fd); |
| 496 | return err; |
| 497 | } |
| 498 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 499 | static void sync_fill_fence_info(struct fence *fence, |
| 500 | struct sync_fence_info *info) |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 501 | { |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 502 | strlcpy(info->obj_name, fence->ops->get_timeline_name(fence), |
| 503 | sizeof(info->obj_name)); |
| 504 | strlcpy(info->driver_name, fence->ops->get_driver_name(fence), |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 505 | sizeof(info->driver_name)); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 506 | if (fence_is_signaled(fence)) |
| 507 | info->status = fence->status >= 0 ? 1 : fence->status; |
| 508 | else |
| 509 | info->status = 0; |
| 510 | info->timestamp_ns = ktime_to_ns(fence->timestamp); |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 513 | static long sync_file_ioctl_fence_info(struct sync_file *sync_file, |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 514 | unsigned long arg) |
| 515 | { |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 516 | struct sync_file_info info; |
| 517 | struct sync_fence_info *fence_info = NULL; |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 518 | __u32 size; |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 519 | int ret, i; |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 520 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 521 | if (copy_from_user(&info, (void __user *)arg, sizeof(info))) |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 522 | return -EFAULT; |
| 523 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 524 | if (info.flags || info.pad) |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 525 | return -EINVAL; |
| 526 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 527 | /* |
| 528 | * Passing num_fences = 0 means that userspace doesn't want to |
| 529 | * retrieve any sync_fence_info. If num_fences = 0 we skip filling |
| 530 | * sync_fence_info and return the actual number of fences on |
| 531 | * info->num_fences. |
| 532 | */ |
| 533 | if (!info.num_fences) |
| 534 | goto no_fences; |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 535 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 536 | if (info.num_fences < sync_file->num_fences) |
| 537 | return -EINVAL; |
| 538 | |
| 539 | size = sync_file->num_fences * sizeof(*fence_info); |
| 540 | fence_info = kzalloc(size, GFP_KERNEL); |
| 541 | if (!fence_info) |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 542 | return -ENOMEM; |
| 543 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 544 | for (i = 0; i < sync_file->num_fences; ++i) |
| 545 | sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]); |
Maarten Lankhorst | 0f0d840 | 2014-07-01 12:57:31 +0200 | [diff] [blame] | 546 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 547 | if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info, |
| 548 | size)) { |
| 549 | ret = -EFAULT; |
| 550 | goto out; |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 553 | no_fences: |
| 554 | strlcpy(info.name, sync_file->name, sizeof(info.name)); |
| 555 | info.status = atomic_read(&sync_file->status); |
| 556 | if (info.status >= 0) |
| 557 | info.status = !info.status; |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 558 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 559 | info.num_fences = sync_file->num_fences; |
| 560 | |
| 561 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 562 | ret = -EFAULT; |
| 563 | else |
| 564 | ret = 0; |
| 565 | |
| 566 | out: |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 567 | kfree(fence_info); |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 568 | |
| 569 | return ret; |
| 570 | } |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 571 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 572 | static long sync_file_ioctl(struct file *file, unsigned int cmd, |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 573 | unsigned long arg) |
| 574 | { |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 575 | struct sync_file *sync_file = file->private_data; |
Dipak Zope | 7b1046e | 2014-08-19 04:21:54 -0400 | [diff] [blame] | 576 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 577 | switch (cmd) { |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 578 | case SYNC_IOC_MERGE: |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 579 | return sync_file_ioctl_merge(sync_file, arg); |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 580 | |
Gustavo Padovan | 2d75c88 | 2016-04-26 12:32:28 -0300 | [diff] [blame] | 581 | case SYNC_IOC_FILE_INFO: |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 582 | return sync_file_ioctl_fence_info(sync_file, arg); |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 583 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 584 | default: |
| 585 | return -ENOTTY; |
| 586 | } |
| 587 | } |
| 588 | |
Gustavo Padovan | d7fdb0a | 2016-01-21 10:49:19 -0200 | [diff] [blame] | 589 | static const struct file_operations sync_file_fops = { |
| 590 | .release = sync_file_release, |
| 591 | .poll = sync_file_poll, |
| 592 | .unlocked_ioctl = sync_file_ioctl, |
| 593 | .compat_ioctl = sync_file_ioctl, |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 594 | }; |
| 595 | |