diff options
author | Gustavo Padovan <gustavo.padovan@collabora.co.uk> | 2016-08-05 09:39:35 -0400 |
---|---|---|
committer | Sumit Semwal <sumit.semwal@linaro.org> | 2016-08-11 06:03:24 -0400 |
commit | a02b9dc90d844cc7df7b63264e7920cc425052d9 (patch) | |
tree | c6e8cf1d7d2f7f6c0e3c205f4af5ae2880f924d6 | |
parent | e1aaf311dbe82221910cc0e0809c988de210cc3c (diff) |
dma-buf/sync_file: refactor fence storage in struct sync_file
Create sync_file->fence to abstract the type of fence we are using for
each sync_file. If only one fence is present we use a normal struct fence
but if there is more fences to be added to the sync_file a fence_array
is created.
This change cleans up sync_file a bit. We don't need to have sync_file_cb
array anymore. Instead, as we always have one fence, only one fence
callback is registered per sync_file.
v2: Comments from Chris Wilson and Christian König
- Not using fence_ops anymore
- fence_is_array() was created to differentiate fence from fence_array
- fence_array_teardown() is now exported and used under fence_is_array()
- struct sync_file lost num_fences member
v3: Comments from Chris Wilson and Christian König
- struct sync_file lost status member in favor of fence_is_signaled()
- drop use of fence_array_teardown()
- use sizeof(*fence) to allocate only an array on fence pointers
v4: Comments from Chris Wilson
- use sizeof(*fence) to reallocate array
- fix typo in comments
- protect num_fences sum against overflows
- use array->base instead of casting the to struct fence
v5: fixes checkpatch warnings
v6: fix case where all fences are signaled.
Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
Acked-by: Christian König <christian.koenig@amd.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
-rw-r--r-- | drivers/dma-buf/sync_file.c | 174 | ||||
-rw-r--r-- | drivers/staging/android/sync_debug.c | 12 | ||||
-rw-r--r-- | include/linux/sync_file.h | 17 |
3 files changed, 129 insertions, 74 deletions
diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c index 9aaa608dfe01..ac9c250af302 100644 --- a/drivers/dma-buf/sync_file.c +++ b/drivers/dma-buf/sync_file.c | |||
@@ -28,11 +28,11 @@ | |||
28 | 28 | ||
29 | static const struct file_operations sync_file_fops; | 29 | static const struct file_operations sync_file_fops; |
30 | 30 | ||
31 | static struct sync_file *sync_file_alloc(int size) | 31 | static struct sync_file *sync_file_alloc(void) |
32 | { | 32 | { |
33 | struct sync_file *sync_file; | 33 | struct sync_file *sync_file; |
34 | 34 | ||
35 | sync_file = kzalloc(size, GFP_KERNEL); | 35 | sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL); |
36 | if (!sync_file) | 36 | if (!sync_file) |
37 | return NULL; | 37 | return NULL; |
38 | 38 | ||
@@ -45,6 +45,8 @@ static struct sync_file *sync_file_alloc(int size) | |||
45 | 45 | ||
46 | init_waitqueue_head(&sync_file->wq); | 46 | init_waitqueue_head(&sync_file->wq); |
47 | 47 | ||
48 | INIT_LIST_HEAD(&sync_file->cb.node); | ||
49 | |||
48 | return sync_file; | 50 | return sync_file; |
49 | 51 | ||
50 | err: | 52 | err: |
@@ -54,14 +56,11 @@ err: | |||
54 | 56 | ||
55 | static void fence_check_cb_func(struct fence *f, struct fence_cb *cb) | 57 | static void fence_check_cb_func(struct fence *f, struct fence_cb *cb) |
56 | { | 58 | { |
57 | struct sync_file_cb *check; | ||
58 | struct sync_file *sync_file; | 59 | struct sync_file *sync_file; |
59 | 60 | ||
60 | check = container_of(cb, struct sync_file_cb, cb); | 61 | sync_file = container_of(cb, struct sync_file, cb); |
61 | sync_file = check->sync_file; | ||
62 | 62 | ||
63 | if (atomic_dec_and_test(&sync_file->status)) | 63 | wake_up_all(&sync_file->wq); |
64 | wake_up_all(&sync_file->wq); | ||
65 | } | 64 | } |
66 | 65 | ||
67 | /** | 66 | /** |
@@ -76,22 +75,18 @@ struct sync_file *sync_file_create(struct fence *fence) | |||
76 | { | 75 | { |
77 | struct sync_file *sync_file; | 76 | struct sync_file *sync_file; |
78 | 77 | ||
79 | sync_file = sync_file_alloc(offsetof(struct sync_file, cbs[1])); | 78 | sync_file = sync_file_alloc(); |
80 | if (!sync_file) | 79 | if (!sync_file) |
81 | return NULL; | 80 | return NULL; |
82 | 81 | ||
83 | sync_file->num_fences = 1; | 82 | sync_file->fence = fence; |
84 | atomic_set(&sync_file->status, 1); | 83 | |
85 | snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d", | 84 | snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d", |
86 | fence->ops->get_driver_name(fence), | 85 | fence->ops->get_driver_name(fence), |
87 | fence->ops->get_timeline_name(fence), fence->context, | 86 | fence->ops->get_timeline_name(fence), fence->context, |
88 | fence->seqno); | 87 | fence->seqno); |
89 | 88 | ||
90 | sync_file->cbs[0].fence = fence; | 89 | fence_add_callback(fence, &sync_file->cb, fence_check_cb_func); |
91 | sync_file->cbs[0].sync_file = sync_file; | ||
92 | if (fence_add_callback(fence, &sync_file->cbs[0].cb, | ||
93 | fence_check_cb_func)) | ||
94 | atomic_dec(&sync_file->status); | ||
95 | 90 | ||
96 | return sync_file; | 91 | return sync_file; |
97 | } | 92 | } |
@@ -121,14 +116,49 @@ err: | |||
121 | return NULL; | 116 | return NULL; |
122 | } | 117 | } |
123 | 118 | ||
124 | static void sync_file_add_pt(struct sync_file *sync_file, int *i, | 119 | static int sync_file_set_fence(struct sync_file *sync_file, |
125 | struct fence *fence) | 120 | struct fence **fences, int num_fences) |
126 | { | 121 | { |
127 | sync_file->cbs[*i].fence = fence; | 122 | struct fence_array *array; |
128 | sync_file->cbs[*i].sync_file = sync_file; | 123 | |
124 | /* | ||
125 | * The reference for the fences in the new sync_file and held | ||
126 | * in add_fence() during the merge procedure, so for num_fences == 1 | ||
127 | * we already own a new reference to the fence. For num_fence > 1 | ||
128 | * we own the reference of the fence_array creation. | ||
129 | */ | ||
130 | if (num_fences == 1) { | ||
131 | sync_file->fence = fences[0]; | ||
132 | } else { | ||
133 | array = fence_array_create(num_fences, fences, | ||
134 | fence_context_alloc(1), 1, false); | ||
135 | if (!array) | ||
136 | return -ENOMEM; | ||
137 | |||
138 | sync_file->fence = &array->base; | ||
139 | } | ||
129 | 140 | ||
130 | if (!fence_add_callback(fence, &sync_file->cbs[*i].cb, | 141 | return 0; |
131 | fence_check_cb_func)) { | 142 | } |
143 | |||
144 | static struct fence **get_fences(struct sync_file *sync_file, int *num_fences) | ||
145 | { | ||
146 | if (fence_is_array(sync_file->fence)) { | ||
147 | struct fence_array *array = to_fence_array(sync_file->fence); | ||
148 | |||
149 | *num_fences = array->num_fences; | ||
150 | return array->fences; | ||
151 | } | ||
152 | |||
153 | *num_fences = 1; | ||
154 | return &sync_file->fence; | ||
155 | } | ||
156 | |||
157 | static void add_fence(struct fence **fences, int *i, struct fence *fence) | ||
158 | { | ||
159 | fences[*i] = fence; | ||
160 | |||
161 | if (!fence_is_signaled(fence)) { | ||
132 | fence_get(fence); | 162 | fence_get(fence); |
133 | (*i)++; | 163 | (*i)++; |
134 | } | 164 | } |
@@ -147,16 +177,24 @@ static void sync_file_add_pt(struct sync_file *sync_file, int *i, | |||
147 | static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, | 177 | static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, |
148 | struct sync_file *b) | 178 | struct sync_file *b) |
149 | { | 179 | { |
150 | int num_fences = a->num_fences + b->num_fences; | ||
151 | struct sync_file *sync_file; | 180 | struct sync_file *sync_file; |
152 | int i, i_a, i_b; | 181 | struct fence **fences, **nfences, **a_fences, **b_fences; |
153 | unsigned long size = offsetof(struct sync_file, cbs[num_fences]); | 182 | int i, i_a, i_b, num_fences, a_num_fences, b_num_fences; |
154 | 183 | ||
155 | sync_file = sync_file_alloc(size); | 184 | sync_file = sync_file_alloc(); |
156 | if (!sync_file) | 185 | if (!sync_file) |
157 | return NULL; | 186 | return NULL; |
158 | 187 | ||
159 | atomic_set(&sync_file->status, num_fences); | 188 | a_fences = get_fences(a, &a_num_fences); |
189 | b_fences = get_fences(b, &b_num_fences); | ||
190 | if (a_num_fences > INT_MAX - b_num_fences) | ||
191 | return NULL; | ||
192 | |||
193 | num_fences = a_num_fences + b_num_fences; | ||
194 | |||
195 | fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL); | ||
196 | if (!fences) | ||
197 | goto err; | ||
160 | 198 | ||
161 | /* | 199 | /* |
162 | * Assume sync_file a and b are both ordered and have no | 200 | * Assume sync_file a and b are both ordered and have no |
@@ -165,55 +203,73 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, | |||
165 | * If a sync_file can only be created with sync_file_merge | 203 | * If a sync_file can only be created with sync_file_merge |
166 | * and sync_file_create, this is a reasonable assumption. | 204 | * and sync_file_create, this is a reasonable assumption. |
167 | */ | 205 | */ |
168 | for (i = i_a = i_b = 0; i_a < a->num_fences && i_b < b->num_fences; ) { | 206 | for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) { |
169 | struct fence *pt_a = a->cbs[i_a].fence; | 207 | struct fence *pt_a = a_fences[i_a]; |
170 | struct fence *pt_b = b->cbs[i_b].fence; | 208 | struct fence *pt_b = b_fences[i_b]; |
171 | 209 | ||
172 | if (pt_a->context < pt_b->context) { | 210 | if (pt_a->context < pt_b->context) { |
173 | sync_file_add_pt(sync_file, &i, pt_a); | 211 | add_fence(fences, &i, pt_a); |
174 | 212 | ||
175 | i_a++; | 213 | i_a++; |
176 | } else if (pt_a->context > pt_b->context) { | 214 | } else if (pt_a->context > pt_b->context) { |
177 | sync_file_add_pt(sync_file, &i, pt_b); | 215 | add_fence(fences, &i, pt_b); |
178 | 216 | ||
179 | i_b++; | 217 | i_b++; |
180 | } else { | 218 | } else { |
181 | if (pt_a->seqno - pt_b->seqno <= INT_MAX) | 219 | if (pt_a->seqno - pt_b->seqno <= INT_MAX) |
182 | sync_file_add_pt(sync_file, &i, pt_a); | 220 | add_fence(fences, &i, pt_a); |
183 | else | 221 | else |
184 | sync_file_add_pt(sync_file, &i, pt_b); | 222 | add_fence(fences, &i, pt_b); |
185 | 223 | ||
186 | i_a++; | 224 | i_a++; |
187 | i_b++; | 225 | i_b++; |
188 | } | 226 | } |
189 | } | 227 | } |
190 | 228 | ||
191 | for (; i_a < a->num_fences; i_a++) | 229 | for (; i_a < a_num_fences; i_a++) |
192 | sync_file_add_pt(sync_file, &i, a->cbs[i_a].fence); | 230 | add_fence(fences, &i, a_fences[i_a]); |
231 | |||
232 | for (; i_b < b_num_fences; i_b++) | ||
233 | add_fence(fences, &i, b_fences[i_b]); | ||
234 | |||
235 | if (i == 0) { | ||
236 | add_fence(fences, &i, a_fences[0]); | ||
237 | i++; | ||
238 | } | ||
193 | 239 | ||
194 | for (; i_b < b->num_fences; i_b++) | 240 | if (num_fences > i) { |
195 | sync_file_add_pt(sync_file, &i, b->cbs[i_b].fence); | 241 | nfences = krealloc(fences, i * sizeof(*fences), |
242 | GFP_KERNEL); | ||
243 | if (!nfences) | ||
244 | goto err; | ||
245 | |||
246 | fences = nfences; | ||
247 | } | ||
248 | |||
249 | if (sync_file_set_fence(sync_file, fences, i) < 0) { | ||
250 | kfree(fences); | ||
251 | goto err; | ||
252 | } | ||
196 | 253 | ||
197 | if (num_fences > i) | 254 | fence_add_callback(sync_file->fence, &sync_file->cb, |
198 | atomic_sub(num_fences - i, &sync_file->status); | 255 | fence_check_cb_func); |
199 | sync_file->num_fences = i; | ||
200 | 256 | ||
201 | strlcpy(sync_file->name, name, sizeof(sync_file->name)); | 257 | strlcpy(sync_file->name, name, sizeof(sync_file->name)); |
202 | return sync_file; | 258 | return sync_file; |
259 | |||
260 | err: | ||
261 | fput(sync_file->file); | ||
262 | return NULL; | ||
263 | |||
203 | } | 264 | } |
204 | 265 | ||
205 | static void sync_file_free(struct kref *kref) | 266 | static void sync_file_free(struct kref *kref) |
206 | { | 267 | { |
207 | struct sync_file *sync_file = container_of(kref, struct sync_file, | 268 | struct sync_file *sync_file = container_of(kref, struct sync_file, |
208 | kref); | 269 | kref); |
209 | int i; | ||
210 | |||
211 | for (i = 0; i < sync_file->num_fences; ++i) { | ||
212 | fence_remove_callback(sync_file->cbs[i].fence, | ||
213 | &sync_file->cbs[i].cb); | ||
214 | fence_put(sync_file->cbs[i].fence); | ||
215 | } | ||
216 | 270 | ||
271 | fence_remove_callback(sync_file->fence, &sync_file->cb); | ||
272 | fence_put(sync_file->fence); | ||
217 | kfree(sync_file); | 273 | kfree(sync_file); |
218 | } | 274 | } |
219 | 275 | ||
@@ -232,9 +288,9 @@ static unsigned int sync_file_poll(struct file *file, poll_table *wait) | |||
232 | 288 | ||
233 | poll_wait(file, &sync_file->wq, wait); | 289 | poll_wait(file, &sync_file->wq, wait); |
234 | 290 | ||
235 | status = atomic_read(&sync_file->status); | 291 | status = fence_is_signaled(sync_file->fence); |
236 | 292 | ||
237 | if (!status) | 293 | if (status) |
238 | return POLLIN; | 294 | return POLLIN; |
239 | if (status < 0) | 295 | if (status < 0) |
240 | return POLLERR; | 296 | return POLLERR; |
@@ -315,8 +371,9 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, | |||
315 | { | 371 | { |
316 | struct sync_file_info info; | 372 | struct sync_file_info info; |
317 | struct sync_fence_info *fence_info = NULL; | 373 | struct sync_fence_info *fence_info = NULL; |
374 | struct fence **fences; | ||
318 | __u32 size; | 375 | __u32 size; |
319 | int ret, i; | 376 | int num_fences, ret, i; |
320 | 377 | ||
321 | if (copy_from_user(&info, (void __user *)arg, sizeof(info))) | 378 | if (copy_from_user(&info, (void __user *)arg, sizeof(info))) |
322 | return -EFAULT; | 379 | return -EFAULT; |
@@ -324,6 +381,8 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, | |||
324 | if (info.flags || info.pad) | 381 | if (info.flags || info.pad) |
325 | return -EINVAL; | 382 | return -EINVAL; |
326 | 383 | ||
384 | fences = get_fences(sync_file, &num_fences); | ||
385 | |||
327 | /* | 386 | /* |
328 | * Passing num_fences = 0 means that userspace doesn't want to | 387 | * Passing num_fences = 0 means that userspace doesn't want to |
329 | * retrieve any sync_fence_info. If num_fences = 0 we skip filling | 388 | * retrieve any sync_fence_info. If num_fences = 0 we skip filling |
@@ -333,16 +392,16 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, | |||
333 | if (!info.num_fences) | 392 | if (!info.num_fences) |
334 | goto no_fences; | 393 | goto no_fences; |
335 | 394 | ||
336 | if (info.num_fences < sync_file->num_fences) | 395 | if (info.num_fences < num_fences) |
337 | return -EINVAL; | 396 | return -EINVAL; |
338 | 397 | ||
339 | size = sync_file->num_fences * sizeof(*fence_info); | 398 | size = num_fences * sizeof(*fence_info); |
340 | fence_info = kzalloc(size, GFP_KERNEL); | 399 | fence_info = kzalloc(size, GFP_KERNEL); |
341 | if (!fence_info) | 400 | if (!fence_info) |
342 | return -ENOMEM; | 401 | return -ENOMEM; |
343 | 402 | ||
344 | for (i = 0; i < sync_file->num_fences; ++i) | 403 | for (i = 0; i < num_fences; i++) |
345 | sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]); | 404 | sync_fill_fence_info(fences[i], &fence_info[i]); |
346 | 405 | ||
347 | if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info, | 406 | if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info, |
348 | size)) { | 407 | size)) { |
@@ -352,11 +411,8 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, | |||
352 | 411 | ||
353 | no_fences: | 412 | no_fences: |
354 | strlcpy(info.name, sync_file->name, sizeof(info.name)); | 413 | strlcpy(info.name, sync_file->name, sizeof(info.name)); |
355 | info.status = atomic_read(&sync_file->status); | 414 | info.status = fence_is_signaled(sync_file->fence); |
356 | if (info.status >= 0) | 415 | info.num_fences = num_fences; |
357 | info.status = !info.status; | ||
358 | |||
359 | info.num_fences = sync_file->num_fences; | ||
360 | 416 | ||
361 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) | 417 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) |
362 | ret = -EFAULT; | 418 | ret = -EFAULT; |
diff --git a/drivers/staging/android/sync_debug.c b/drivers/staging/android/sync_debug.c index 4c5a85595a85..961a94bf156c 100644 --- a/drivers/staging/android/sync_debug.c +++ b/drivers/staging/android/sync_debug.c | |||
@@ -135,10 +135,16 @@ static void sync_print_sync_file(struct seq_file *s, | |||
135 | int i; | 135 | int i; |
136 | 136 | ||
137 | seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name, | 137 | seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name, |
138 | sync_status_str(atomic_read(&sync_file->status))); | 138 | sync_status_str(!fence_is_signaled(sync_file->fence))); |
139 | 139 | ||
140 | for (i = 0; i < sync_file->num_fences; ++i) | 140 | if (fence_is_array(sync_file->fence)) { |
141 | sync_print_fence(s, sync_file->cbs[i].fence, true); | 141 | struct fence_array *array = to_fence_array(sync_file->fence); |
142 | |||
143 | for (i = 0; i < array->num_fences; ++i) | ||
144 | sync_print_fence(s, array->fences[i], true); | ||
145 | } else { | ||
146 | sync_print_fence(s, sync_file->fence, true); | ||
147 | } | ||
142 | } | 148 | } |
143 | 149 | ||
144 | static int sync_debugfs_show(struct seq_file *s, void *unused) | 150 | static int sync_debugfs_show(struct seq_file *s, void *unused) |
diff --git a/include/linux/sync_file.h b/include/linux/sync_file.h index c6ffe8b0725c..2efc5ec60575 100644 --- a/include/linux/sync_file.h +++ b/include/linux/sync_file.h | |||
@@ -19,12 +19,7 @@ | |||
19 | #include <linux/list.h> | 19 | #include <linux/list.h> |
20 | #include <linux/spinlock.h> | 20 | #include <linux/spinlock.h> |
21 | #include <linux/fence.h> | 21 | #include <linux/fence.h> |
22 | 22 | #include <linux/fence-array.h> | |
23 | struct sync_file_cb { | ||
24 | struct fence_cb cb; | ||
25 | struct fence *fence; | ||
26 | struct sync_file *sync_file; | ||
27 | }; | ||
28 | 23 | ||
29 | /** | 24 | /** |
30 | * struct sync_file - sync file to export to the userspace | 25 | * struct sync_file - sync file to export to the userspace |
@@ -32,10 +27,9 @@ struct sync_file_cb { | |||
32 | * @kref: reference count on fence. | 27 | * @kref: reference count on fence. |
33 | * @name: name of sync_file. Useful for debugging | 28 | * @name: name of sync_file. Useful for debugging |
34 | * @sync_file_list: membership in global file list | 29 | * @sync_file_list: membership in global file list |
35 | * @num_fences: number of sync_pts in the fence | ||
36 | * @wq: wait queue for fence signaling | 30 | * @wq: wait queue for fence signaling |
37 | * @status: 0: signaled, >0:active, <0: error | 31 | * @fence: fence with the fences in the sync_file |
38 | * @cbs: sync_pts callback information | 32 | * @cb: fence callback information |
39 | */ | 33 | */ |
40 | struct sync_file { | 34 | struct sync_file { |
41 | struct file *file; | 35 | struct file *file; |
@@ -44,12 +38,11 @@ struct sync_file { | |||
44 | #ifdef CONFIG_DEBUG_FS | 38 | #ifdef CONFIG_DEBUG_FS |
45 | struct list_head sync_file_list; | 39 | struct list_head sync_file_list; |
46 | #endif | 40 | #endif |
47 | int num_fences; | ||
48 | 41 | ||
49 | wait_queue_head_t wq; | 42 | wait_queue_head_t wq; |
50 | atomic_t status; | ||
51 | 43 | ||
52 | struct sync_file_cb cbs[]; | 44 | struct fence *fence; |
45 | struct fence_cb cb; | ||
53 | }; | 46 | }; |
54 | 47 | ||
55 | struct sync_file *sync_file_create(struct fence *fence); | 48 | struct sync_file *sync_file_create(struct fence *fence); |