Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 1 | /* |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 2 | * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst) |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 3 | * |
| 4 | * Based on bo.c which bears the following copyright notice, |
| 5 | * but is dual licensed: |
| 6 | * |
| 7 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
| 8 | * All Rights Reserved. |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | * copy of this software and associated documentation files (the |
| 12 | * "Software"), to deal in the Software without restriction, including |
| 13 | * without limitation the rights to use, copy, modify, merge, publish, |
| 14 | * distribute, sub license, and/or sell copies of the Software, and to |
| 15 | * permit persons to whom the Software is furnished to do so, subject to |
| 16 | * the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice (including the |
| 19 | * next paragraph) shall be included in all copies or substantial portions |
| 20 | * of the Software. |
| 21 | * |
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 25 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 26 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 27 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 28 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | * |
| 30 | **************************************************************************/ |
| 31 | /* |
| 32 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 33 | */ |
| 34 | |
| 35 | #include <linux/reservation.h> |
| 36 | #include <linux/export.h> |
| 37 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 38 | /** |
| 39 | * DOC: Reservation Object Overview |
| 40 | * |
| 41 | * The reservation object provides a mechanism to manage shared and |
| 42 | * exclusive fences associated with a buffer. A reservation object |
| 43 | * can have attached one exclusive fence (normally associated with |
| 44 | * write operations) or N shared fences (read operations). The RCU |
| 45 | * mechanism is used to protect read access to fences from locked |
| 46 | * write-side updates. |
| 47 | */ |
| 48 | |
Thomas Hellstrom | 08295b3 | 2018-06-15 10:17:38 +0200 | [diff] [blame] | 49 | DEFINE_WD_CLASS(reservation_ww_class); |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(reservation_ww_class); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 51 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 52 | struct lock_class_key reservation_seqcount_class; |
| 53 | EXPORT_SYMBOL(reservation_seqcount_class); |
| 54 | |
| 55 | const char reservation_seqcount_string[] = "reservation_seqcount"; |
| 56 | EXPORT_SYMBOL(reservation_seqcount_string); |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 57 | |
| 58 | /** |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame^] | 59 | * reservation_object_reserve_shared - Reserve space to add shared fences to |
| 60 | * a reservation_object. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 61 | * @obj: reservation object |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame^] | 62 | * @num_fences: number of fences we want to add |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 63 | * |
| 64 | * Should be called before reservation_object_add_shared_fence(). Must |
| 65 | * be called with obj->lock held. |
| 66 | * |
| 67 | * RETURNS |
| 68 | * Zero for success, or -errno |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 69 | */ |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame^] | 70 | int reservation_object_reserve_shared(struct reservation_object *obj, |
| 71 | unsigned int num_fences) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 72 | { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 73 | struct reservation_object_list *old, *new; |
| 74 | unsigned int i, j, k, max; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 75 | |
| 76 | old = reservation_object_get_list(obj); |
| 77 | |
| 78 | if (old && old->shared_max) { |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame^] | 79 | if ((old->shared_count + num_fences) <= old->shared_max) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 80 | return 0; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 81 | else |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame^] | 82 | max = max(old->shared_count + num_fences, |
| 83 | old->shared_max * 2); |
Christian König | ca25fe5 | 2017-11-14 15:24:36 +0100 | [diff] [blame] | 84 | } else { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 85 | max = 4; |
Christian König | ca25fe5 | 2017-11-14 15:24:36 +0100 | [diff] [blame] | 86 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 87 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 88 | new = kmalloc(offsetof(typeof(*new), shared[max]), GFP_KERNEL); |
| 89 | if (!new) |
| 90 | return -ENOMEM; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * no need to bump fence refcounts, rcu_read access |
| 94 | * requires the use of kref_get_unless_zero, and the |
| 95 | * references from the old struct are carried over to |
| 96 | * the new. |
| 97 | */ |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 98 | for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) { |
| 99 | struct dma_fence *fence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 100 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 101 | fence = rcu_dereference_protected(old->shared[i], |
| 102 | reservation_object_held(obj)); |
| 103 | if (dma_fence_is_signaled(fence)) |
| 104 | RCU_INIT_POINTER(new->shared[--k], fence); |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 105 | else |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 106 | RCU_INIT_POINTER(new->shared[j++], fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 107 | } |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 108 | new->shared_count = j; |
| 109 | new->shared_max = max; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 110 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 111 | preempt_disable(); |
| 112 | write_seqcount_begin(&obj->seq); |
| 113 | /* |
| 114 | * RCU_INIT_POINTER can be used here, |
| 115 | * seqcount provides the necessary barriers |
| 116 | */ |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 117 | RCU_INIT_POINTER(obj->fence, new); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 118 | write_seqcount_end(&obj->seq); |
| 119 | preempt_enable(); |
| 120 | |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 121 | if (!old) |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 122 | return 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 123 | |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 124 | /* Drop the references to the signaled fences */ |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 125 | for (i = k; i < new->shared_max; ++i) { |
| 126 | struct dma_fence *fence; |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 127 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 128 | fence = rcu_dereference_protected(new->shared[i], |
| 129 | reservation_object_held(obj)); |
| 130 | dma_fence_put(fence); |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 131 | } |
| 132 | kfree_rcu(old, rcu); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 133 | |
| 134 | return 0; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 135 | } |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 136 | EXPORT_SYMBOL(reservation_object_reserve_shared); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 137 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 138 | /** |
| 139 | * reservation_object_add_shared_fence - Add a fence to a shared slot |
| 140 | * @obj: the reservation object |
| 141 | * @fence: the shared fence to add |
| 142 | * |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 143 | * Add a fence to a shared slot, obj->lock must be held, and |
Rob Clark | f5bef0b | 2016-08-19 16:55:34 -0400 | [diff] [blame] | 144 | * reservation_object_reserve_shared() has been called. |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 145 | */ |
| 146 | void reservation_object_add_shared_fence(struct reservation_object *obj, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 147 | struct dma_fence *fence) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 148 | { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 149 | struct reservation_object_list *fobj; |
| 150 | unsigned int i; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 151 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 152 | dma_fence_get(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 153 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 154 | fobj = reservation_object_get_list(obj); |
| 155 | |
| 156 | preempt_disable(); |
| 157 | write_seqcount_begin(&obj->seq); |
| 158 | |
| 159 | for (i = 0; i < fobj->shared_count; ++i) { |
| 160 | struct dma_fence *old_fence; |
| 161 | |
| 162 | old_fence = rcu_dereference_protected(fobj->shared[i], |
| 163 | reservation_object_held(obj)); |
| 164 | if (old_fence->context == fence->context || |
| 165 | dma_fence_is_signaled(old_fence)) { |
| 166 | dma_fence_put(old_fence); |
| 167 | goto replace; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | BUG_ON(fobj->shared_count >= fobj->shared_max); |
| 172 | fobj->shared_count++; |
| 173 | |
| 174 | replace: |
| 175 | /* |
| 176 | * memory barrier is added by write_seqcount_begin, |
| 177 | * fobj->shared_count is protected by this lock too |
| 178 | */ |
| 179 | RCU_INIT_POINTER(fobj->shared[i], fence); |
| 180 | write_seqcount_end(&obj->seq); |
| 181 | preempt_enable(); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 182 | } |
| 183 | EXPORT_SYMBOL(reservation_object_add_shared_fence); |
| 184 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 185 | /** |
| 186 | * reservation_object_add_excl_fence - Add an exclusive fence. |
| 187 | * @obj: the reservation object |
| 188 | * @fence: the shared fence to add |
| 189 | * |
| 190 | * Add a fence to the exclusive slot. The obj->lock must be held. |
| 191 | */ |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 192 | void reservation_object_add_excl_fence(struct reservation_object *obj, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 193 | struct dma_fence *fence) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 194 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 195 | struct dma_fence *old_fence = reservation_object_get_excl(obj); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 196 | struct reservation_object_list *old; |
| 197 | u32 i = 0; |
| 198 | |
| 199 | old = reservation_object_get_list(obj); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 200 | if (old) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 201 | i = old->shared_count; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 202 | |
| 203 | if (fence) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 204 | dma_fence_get(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 205 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 206 | preempt_disable(); |
| 207 | write_seqcount_begin(&obj->seq); |
| 208 | /* write_seqcount_begin provides the necessary memory barrier */ |
| 209 | RCU_INIT_POINTER(obj->fence_excl, fence); |
| 210 | if (old) |
| 211 | old->shared_count = 0; |
| 212 | write_seqcount_end(&obj->seq); |
| 213 | preempt_enable(); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 214 | |
| 215 | /* inplace update, no shared fences */ |
| 216 | while (i--) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 217 | dma_fence_put(rcu_dereference_protected(old->shared[i], |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 218 | reservation_object_held(obj))); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 219 | |
Christian König | f3e31b7 | 2017-08-07 17:32:21 -0400 | [diff] [blame] | 220 | dma_fence_put(old_fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 221 | } |
| 222 | EXPORT_SYMBOL(reservation_object_add_excl_fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 223 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 224 | /** |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 225 | * reservation_object_copy_fences - Copy all fences from src to dst. |
| 226 | * @dst: the destination reservation object |
| 227 | * @src: the source reservation object |
| 228 | * |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 229 | * Copy all fences from src to dst. dst-lock must be held. |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 230 | */ |
| 231 | int reservation_object_copy_fences(struct reservation_object *dst, |
| 232 | struct reservation_object *src) |
| 233 | { |
| 234 | struct reservation_object_list *src_list, *dst_list; |
| 235 | struct dma_fence *old, *new; |
| 236 | size_t size; |
| 237 | unsigned i; |
| 238 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 239 | rcu_read_lock(); |
| 240 | src_list = rcu_dereference(src->fence); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 241 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 242 | retry: |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 243 | if (src_list) { |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 244 | unsigned shared_count = src_list->shared_count; |
| 245 | |
| 246 | size = offsetof(typeof(*src_list), shared[shared_count]); |
| 247 | rcu_read_unlock(); |
| 248 | |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 249 | dst_list = kmalloc(size, GFP_KERNEL); |
| 250 | if (!dst_list) |
| 251 | return -ENOMEM; |
| 252 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 253 | rcu_read_lock(); |
| 254 | src_list = rcu_dereference(src->fence); |
| 255 | if (!src_list || src_list->shared_count > shared_count) { |
| 256 | kfree(dst_list); |
| 257 | goto retry; |
| 258 | } |
| 259 | |
| 260 | dst_list->shared_count = 0; |
| 261 | dst_list->shared_max = shared_count; |
| 262 | for (i = 0; i < src_list->shared_count; ++i) { |
| 263 | struct dma_fence *fence; |
| 264 | |
| 265 | fence = rcu_dereference(src_list->shared[i]); |
| 266 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 267 | &fence->flags)) |
| 268 | continue; |
| 269 | |
| 270 | if (!dma_fence_get_rcu(fence)) { |
| 271 | kfree(dst_list); |
| 272 | src_list = rcu_dereference(src->fence); |
| 273 | goto retry; |
| 274 | } |
| 275 | |
| 276 | if (dma_fence_is_signaled(fence)) { |
| 277 | dma_fence_put(fence); |
| 278 | continue; |
| 279 | } |
| 280 | |
Ville Syrjälä | ad46d7b | 2017-11-02 22:03:36 +0200 | [diff] [blame] | 281 | rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence); |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 282 | } |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 283 | } else { |
| 284 | dst_list = NULL; |
| 285 | } |
| 286 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 287 | new = dma_fence_get_rcu_safe(&src->fence_excl); |
| 288 | rcu_read_unlock(); |
| 289 | |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 290 | src_list = reservation_object_get_list(dst); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 291 | old = reservation_object_get_excl(dst); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 292 | |
| 293 | preempt_disable(); |
| 294 | write_seqcount_begin(&dst->seq); |
| 295 | /* write_seqcount_begin provides the necessary memory barrier */ |
| 296 | RCU_INIT_POINTER(dst->fence_excl, new); |
| 297 | RCU_INIT_POINTER(dst->fence, dst_list); |
| 298 | write_seqcount_end(&dst->seq); |
| 299 | preempt_enable(); |
| 300 | |
| 301 | if (src_list) |
| 302 | kfree_rcu(src_list, rcu); |
| 303 | dma_fence_put(old); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | EXPORT_SYMBOL(reservation_object_copy_fences); |
| 308 | |
| 309 | /** |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 310 | * reservation_object_get_fences_rcu - Get an object's shared and exclusive |
| 311 | * fences without update side lock held |
| 312 | * @obj: the reservation object |
| 313 | * @pfence_excl: the returned exclusive fence (or NULL) |
| 314 | * @pshared_count: the number of shared fences returned |
| 315 | * @pshared: the array of shared fence ptrs returned (array is krealloc'd to |
| 316 | * the required size, and must be freed by caller) |
| 317 | * |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 318 | * Retrieve all fences from the reservation object. If the pointer for the |
| 319 | * exclusive fence is not specified the fence is put into the array of the |
| 320 | * shared fences as well. Returns either zero or -ENOMEM. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 321 | */ |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 322 | int reservation_object_get_fences_rcu(struct reservation_object *obj, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 323 | struct dma_fence **pfence_excl, |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 324 | unsigned *pshared_count, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 325 | struct dma_fence ***pshared) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 326 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 327 | struct dma_fence **shared = NULL; |
| 328 | struct dma_fence *fence_excl; |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 329 | unsigned int shared_count; |
| 330 | int ret = 1; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 331 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 332 | do { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 333 | struct reservation_object_list *fobj; |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 334 | unsigned int i, seq; |
| 335 | size_t sz = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 336 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 337 | shared_count = i = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 338 | |
| 339 | rcu_read_lock(); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 340 | seq = read_seqcount_begin(&obj->seq); |
| 341 | |
| 342 | fence_excl = rcu_dereference(obj->fence_excl); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 343 | if (fence_excl && !dma_fence_get_rcu(fence_excl)) |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 344 | goto unlock; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 345 | |
| 346 | fobj = rcu_dereference(obj->fence); |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 347 | if (fobj) |
| 348 | sz += sizeof(*shared) * fobj->shared_max; |
| 349 | |
| 350 | if (!pfence_excl && fence_excl) |
| 351 | sz += sizeof(*shared); |
| 352 | |
| 353 | if (sz) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 354 | struct dma_fence **nshared; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 355 | |
| 356 | nshared = krealloc(shared, sz, |
| 357 | GFP_NOWAIT | __GFP_NOWARN); |
| 358 | if (!nshared) { |
| 359 | rcu_read_unlock(); |
| 360 | nshared = krealloc(shared, sz, GFP_KERNEL); |
| 361 | if (nshared) { |
| 362 | shared = nshared; |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | ret = -ENOMEM; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | shared = nshared; |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 370 | shared_count = fobj ? fobj->shared_count : 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 371 | for (i = 0; i < shared_count; ++i) { |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 372 | shared[i] = rcu_dereference(fobj->shared[i]); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 373 | if (!dma_fence_get_rcu(shared[i])) |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 374 | break; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 375 | } |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 376 | |
| 377 | if (!pfence_excl && fence_excl) { |
| 378 | shared[i] = fence_excl; |
| 379 | fence_excl = NULL; |
| 380 | ++i; |
| 381 | ++shared_count; |
| 382 | } |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 383 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 384 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 385 | if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) { |
| 386 | while (i--) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 387 | dma_fence_put(shared[i]); |
| 388 | dma_fence_put(fence_excl); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 389 | goto unlock; |
| 390 | } |
| 391 | |
| 392 | ret = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 393 | unlock: |
| 394 | rcu_read_unlock(); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 395 | } while (ret); |
| 396 | |
| 397 | if (!shared_count) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 398 | kfree(shared); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 399 | shared = NULL; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 400 | } |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 401 | |
| 402 | *pshared_count = shared_count; |
| 403 | *pshared = shared; |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 404 | if (pfence_excl) |
| 405 | *pfence_excl = fence_excl; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu); |
| 410 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 411 | /** |
| 412 | * reservation_object_wait_timeout_rcu - Wait on reservation's objects |
| 413 | * shared and/or exclusive fences. |
| 414 | * @obj: the reservation object |
| 415 | * @wait_all: if true, wait on all fences, else wait on just exclusive fence |
| 416 | * @intr: if true, do interruptible wait |
| 417 | * @timeout: timeout value in jiffies or zero to return immediately |
| 418 | * |
| 419 | * RETURNS |
| 420 | * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or |
| 421 | * greater than zer on success. |
| 422 | */ |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 423 | long reservation_object_wait_timeout_rcu(struct reservation_object *obj, |
| 424 | bool wait_all, bool intr, |
| 425 | unsigned long timeout) |
| 426 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 427 | struct dma_fence *fence; |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 428 | unsigned seq, shared_count; |
Christian König | 06a66b5 | 2016-11-07 16:16:16 -0500 | [diff] [blame] | 429 | long ret = timeout ? timeout : 1; |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 430 | int i; |
Jammy Zhou | fb8b7d2 | 2015-01-21 18:35:47 +0800 | [diff] [blame] | 431 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 432 | retry: |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 433 | shared_count = 0; |
| 434 | seq = read_seqcount_begin(&obj->seq); |
| 435 | rcu_read_lock(); |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 436 | i = -1; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 437 | |
Christian König | b88fa00 | 2017-08-10 13:01:49 -0400 | [diff] [blame] | 438 | fence = rcu_dereference(obj->fence_excl); |
| 439 | if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
| 440 | if (!dma_fence_get_rcu(fence)) |
| 441 | goto unlock_retry; |
| 442 | |
| 443 | if (dma_fence_is_signaled(fence)) { |
| 444 | dma_fence_put(fence); |
| 445 | fence = NULL; |
| 446 | } |
| 447 | |
| 448 | } else { |
| 449 | fence = NULL; |
| 450 | } |
| 451 | |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 452 | if (wait_all) { |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 453 | struct reservation_object_list *fobj = |
| 454 | rcu_dereference(obj->fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 455 | |
| 456 | if (fobj) |
| 457 | shared_count = fobj->shared_count; |
| 458 | |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 459 | for (i = 0; !fence && i < shared_count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 460 | struct dma_fence *lfence = rcu_dereference(fobj->shared[i]); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 461 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 462 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 463 | &lfence->flags)) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 464 | continue; |
| 465 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 466 | if (!dma_fence_get_rcu(lfence)) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 467 | goto unlock_retry; |
| 468 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 469 | if (dma_fence_is_signaled(lfence)) { |
| 470 | dma_fence_put(lfence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | |
| 474 | fence = lfence; |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 479 | rcu_read_unlock(); |
| 480 | if (fence) { |
Chris Wilson | 1cec20f | 2016-08-29 08:08:31 +0100 | [diff] [blame] | 481 | if (read_seqcount_retry(&obj->seq, seq)) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 482 | dma_fence_put(fence); |
Chris Wilson | 1cec20f | 2016-08-29 08:08:31 +0100 | [diff] [blame] | 483 | goto retry; |
| 484 | } |
| 485 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 486 | ret = dma_fence_wait_timeout(fence, intr, ret); |
| 487 | dma_fence_put(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 488 | if (ret > 0 && wait_all && (i + 1 < shared_count)) |
| 489 | goto retry; |
| 490 | } |
| 491 | return ret; |
| 492 | |
| 493 | unlock_retry: |
| 494 | rcu_read_unlock(); |
| 495 | goto retry; |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu); |
| 498 | |
| 499 | |
| 500 | static inline int |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 501 | reservation_object_test_signaled_single(struct dma_fence *passed_fence) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 502 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 503 | struct dma_fence *fence, *lfence = passed_fence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 504 | int ret = 1; |
| 505 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 506 | if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) { |
| 507 | fence = dma_fence_get_rcu(lfence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 508 | if (!fence) |
| 509 | return -1; |
| 510 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 511 | ret = !!dma_fence_is_signaled(fence); |
| 512 | dma_fence_put(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 513 | } |
| 514 | return ret; |
| 515 | } |
| 516 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 517 | /** |
| 518 | * reservation_object_test_signaled_rcu - Test if a reservation object's |
| 519 | * fences have been signaled. |
| 520 | * @obj: the reservation object |
| 521 | * @test_all: if true, test all fences, otherwise only test the exclusive |
| 522 | * fence |
| 523 | * |
| 524 | * RETURNS |
| 525 | * true if all fences signaled, else false |
| 526 | */ |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 527 | bool reservation_object_test_signaled_rcu(struct reservation_object *obj, |
| 528 | bool test_all) |
| 529 | { |
| 530 | unsigned seq, shared_count; |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 531 | int ret; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 532 | |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 533 | rcu_read_lock(); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 534 | retry: |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 535 | ret = true; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 536 | shared_count = 0; |
| 537 | seq = read_seqcount_begin(&obj->seq); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 538 | |
| 539 | if (test_all) { |
| 540 | unsigned i; |
| 541 | |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 542 | struct reservation_object_list *fobj = |
| 543 | rcu_dereference(obj->fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 544 | |
| 545 | if (fobj) |
| 546 | shared_count = fobj->shared_count; |
| 547 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 548 | for (i = 0; i < shared_count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 549 | struct dma_fence *fence = rcu_dereference(fobj->shared[i]); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 550 | |
| 551 | ret = reservation_object_test_signaled_single(fence); |
| 552 | if (ret < 0) |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 553 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 554 | else if (!ret) |
| 555 | break; |
| 556 | } |
| 557 | |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 558 | if (read_seqcount_retry(&obj->seq, seq)) |
| 559 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (!shared_count) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 563 | struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 564 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 565 | if (fence_excl) { |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 566 | ret = reservation_object_test_signaled_single( |
| 567 | fence_excl); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 568 | if (ret < 0) |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 569 | goto retry; |
| 570 | |
| 571 | if (read_seqcount_retry(&obj->seq, seq)) |
| 572 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | rcu_read_unlock(); |
| 577 | return ret; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 578 | } |
| 579 | EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu); |