Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: MIT |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 2 | /* |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 3 | * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst) |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 4 | * |
| 5 | * Based on bo.c which bears the following copyright notice, |
| 6 | * but is dual licensed: |
| 7 | * |
| 8 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
| 9 | * All Rights Reserved. |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 12 | * copy of this software and associated documentation files (the |
| 13 | * "Software"), to deal in the Software without restriction, including |
| 14 | * without limitation the rights to use, copy, modify, merge, publish, |
| 15 | * distribute, sub license, and/or sell copies of the Software, and to |
| 16 | * permit persons to whom the Software is furnished to do so, subject to |
| 17 | * the following conditions: |
| 18 | * |
| 19 | * The above copyright notice and this permission notice (including the |
| 20 | * next paragraph) shall be included in all copies or substantial portions |
| 21 | * of the Software. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 26 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 30 | * |
| 31 | **************************************************************************/ |
| 32 | /* |
| 33 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 34 | */ |
| 35 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 36 | #include <linux/dma-resv.h> |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 37 | #include <linux/export.h> |
Michel Lespinasse | 0adf65f | 2020-06-08 21:33:21 -0700 | [diff] [blame] | 38 | #include <linux/mm.h> |
Daniel Vetter | b2a8116 | 2019-11-04 18:37:59 +0100 | [diff] [blame] | 39 | #include <linux/sched/mm.h> |
Daniel Vetter | d0b9a9a | 2020-07-07 22:12:06 +0200 | [diff] [blame] | 40 | #include <linux/mmu_notifier.h> |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 41 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 42 | /** |
| 43 | * DOC: Reservation Object Overview |
| 44 | * |
| 45 | * The reservation object provides a mechanism to manage shared and |
| 46 | * exclusive fences associated with a buffer. A reservation object |
| 47 | * can have attached one exclusive fence (normally associated with |
| 48 | * write operations) or N shared fences (read operations). The RCU |
| 49 | * mechanism is used to protect read access to fences from locked |
| 50 | * write-side updates. |
| 51 | */ |
| 52 | |
Thomas Hellstrom | 08295b3 | 2018-06-15 10:17:38 +0200 | [diff] [blame] | 53 | DEFINE_WD_CLASS(reservation_ww_class); |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 54 | EXPORT_SYMBOL(reservation_ww_class); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 55 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 56 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 57 | * dma_resv_list_alloc - allocate fence list |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 58 | * @shared_max: number of fences we need space for |
| 59 | * |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 60 | * Allocate a new dma_resv_list and make sure to correctly initialize |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 61 | * shared_max. |
| 62 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 63 | static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max) |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 64 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 65 | struct dma_resv_list *list; |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 66 | |
Christian König | 82e1b93 | 2020-10-08 10:03:22 +0200 | [diff] [blame] | 67 | list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL); |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 68 | if (!list) |
| 69 | return NULL; |
| 70 | |
| 71 | list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) / |
| 72 | sizeof(*list->shared); |
| 73 | |
| 74 | return list; |
| 75 | } |
| 76 | |
| 77 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 78 | * dma_resv_list_free - free fence list |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 79 | * @list: list to free |
| 80 | * |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 81 | * Free a dma_resv_list and make sure to drop all references. |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 82 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 83 | static void dma_resv_list_free(struct dma_resv_list *list) |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 84 | { |
| 85 | unsigned int i; |
| 86 | |
| 87 | if (!list) |
| 88 | return; |
| 89 | |
| 90 | for (i = 0; i < list->shared_count; ++i) |
| 91 | dma_fence_put(rcu_dereference_protected(list->shared[i], true)); |
| 92 | |
| 93 | kfree_rcu(list, rcu); |
| 94 | } |
| 95 | |
| 96 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 97 | * dma_resv_init - initialize a reservation object |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 98 | * @obj: the reservation object |
| 99 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 100 | void dma_resv_init(struct dma_resv *obj) |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 101 | { |
| 102 | ww_mutex_init(&obj->lock, &reservation_ww_class); |
Ahmed S. Darwish | cd29f22 | 2020-07-20 17:55:18 +0200 | [diff] [blame] | 103 | seqcount_ww_mutex_init(&obj->seq, &obj->lock); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 104 | |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 105 | RCU_INIT_POINTER(obj->fence, NULL); |
| 106 | RCU_INIT_POINTER(obj->fence_excl, NULL); |
| 107 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 108 | EXPORT_SYMBOL(dma_resv_init); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 109 | |
| 110 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 111 | * dma_resv_fini - destroys a reservation object |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 112 | * @obj: the reservation object |
| 113 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 114 | void dma_resv_fini(struct dma_resv *obj) |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 115 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 116 | struct dma_resv_list *fobj; |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 117 | struct dma_fence *excl; |
| 118 | |
| 119 | /* |
| 120 | * This object should be dead and all references must have |
| 121 | * been released to it, so no need to be protected with rcu. |
| 122 | */ |
| 123 | excl = rcu_dereference_protected(obj->fence_excl, 1); |
| 124 | if (excl) |
| 125 | dma_fence_put(excl); |
| 126 | |
| 127 | fobj = rcu_dereference_protected(obj->fence, 1); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 128 | dma_resv_list_free(fobj); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 129 | ww_mutex_destroy(&obj->lock); |
| 130 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 131 | EXPORT_SYMBOL(dma_resv_fini); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 134 | * dma_resv_reserve_shared - Reserve space to add shared fences to |
| 135 | * a dma_resv. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 136 | * @obj: reservation object |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame] | 137 | * @num_fences: number of fences we want to add |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 138 | * |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 139 | * Should be called before dma_resv_add_shared_fence(). Must |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 140 | * be called with obj->lock held. |
| 141 | * |
| 142 | * RETURNS |
| 143 | * Zero for success, or -errno |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 144 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 145 | int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 146 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 147 | struct dma_resv_list *old, *new; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 148 | unsigned int i, j, k, max; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 149 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 150 | dma_resv_assert_held(obj); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 151 | |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 152 | old = dma_resv_shared_list(obj); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 153 | if (old && old->shared_max) { |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame] | 154 | if ((old->shared_count + num_fences) <= old->shared_max) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 155 | return 0; |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 156 | max = max(old->shared_count + num_fences, old->shared_max * 2); |
Christian König | ca25fe5 | 2017-11-14 15:24:36 +0100 | [diff] [blame] | 157 | } else { |
Maarten Lankhorst | bf89758 | 2020-11-24 12:57:07 +0100 | [diff] [blame] | 158 | max = max(4ul, roundup_pow_of_two(num_fences)); |
Christian König | ca25fe5 | 2017-11-14 15:24:36 +0100 | [diff] [blame] | 159 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 160 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 161 | new = dma_resv_list_alloc(max); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 162 | if (!new) |
| 163 | return -ENOMEM; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * no need to bump fence refcounts, rcu_read access |
| 167 | * requires the use of kref_get_unless_zero, and the |
| 168 | * references from the old struct are carried over to |
| 169 | * the new. |
| 170 | */ |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 171 | for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) { |
| 172 | struct dma_fence *fence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 173 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 174 | fence = rcu_dereference_protected(old->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 175 | dma_resv_held(obj)); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 176 | if (dma_fence_is_signaled(fence)) |
| 177 | RCU_INIT_POINTER(new->shared[--k], fence); |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 178 | else |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 179 | RCU_INIT_POINTER(new->shared[j++], fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 180 | } |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 181 | new->shared_count = j; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 182 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 183 | /* |
Chris Wilson | 30fe7b0 | 2019-07-12 09:03:14 +0100 | [diff] [blame] | 184 | * We are not changing the effective set of fences here so can |
| 185 | * merely update the pointer to the new array; both existing |
| 186 | * readers and new readers will see exactly the same set of |
| 187 | * active (unsignaled) shared fences. Individual fences and the |
| 188 | * old array are protected by RCU and so will not vanish under |
| 189 | * the gaze of the rcu_read_lock() readers. |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 190 | */ |
Chris Wilson | 30fe7b0 | 2019-07-12 09:03:14 +0100 | [diff] [blame] | 191 | rcu_assign_pointer(obj->fence, new); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 192 | |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 193 | if (!old) |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 194 | return 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 195 | |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 196 | /* Drop the references to the signaled fences */ |
Chris Wilson | 94eb1e1 | 2019-07-12 09:03:13 +0100 | [diff] [blame] | 197 | for (i = k; i < max; ++i) { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 198 | struct dma_fence *fence; |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 199 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 200 | fence = rcu_dereference_protected(new->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 201 | dma_resv_held(obj)); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 202 | dma_fence_put(fence); |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 203 | } |
| 204 | kfree_rcu(old, rcu); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 205 | |
| 206 | return 0; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 207 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 208 | EXPORT_SYMBOL(dma_resv_reserve_shared); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 209 | |
Christian König | 0c6b522 | 2021-05-06 14:16:01 +0200 | [diff] [blame] | 210 | #ifdef CONFIG_DEBUG_MUTEXES |
| 211 | /** |
| 212 | * dma_resv_reset_shared_max - reset shared fences for debugging |
| 213 | * @obj: the dma_resv object to reset |
| 214 | * |
| 215 | * Reset the number of pre-reserved shared slots to test that drivers do |
| 216 | * correct slot allocation using dma_resv_reserve_shared(). See also |
| 217 | * &dma_resv_list.shared_max. |
| 218 | */ |
| 219 | void dma_resv_reset_shared_max(struct dma_resv *obj) |
| 220 | { |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 221 | struct dma_resv_list *fences = dma_resv_shared_list(obj); |
Christian König | 0c6b522 | 2021-05-06 14:16:01 +0200 | [diff] [blame] | 222 | |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 223 | dma_resv_assert_held(obj); |
| 224 | |
| 225 | /* Test shared fence slot reservation */ |
| 226 | if (fences) |
| 227 | fences->shared_max = fences->shared_count; |
Christian König | 0c6b522 | 2021-05-06 14:16:01 +0200 | [diff] [blame] | 228 | } |
Christian König | 415f676 | 2021-06-04 17:47:39 +0200 | [diff] [blame] | 229 | EXPORT_SYMBOL(dma_resv_reset_shared_max); |
Christian König | 0c6b522 | 2021-05-06 14:16:01 +0200 | [diff] [blame] | 230 | #endif |
| 231 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 232 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 233 | * dma_resv_add_shared_fence - Add a fence to a shared slot |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 234 | * @obj: the reservation object |
| 235 | * @fence: the shared fence to add |
| 236 | * |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 237 | * Add a fence to a shared slot, obj->lock must be held, and |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 238 | * dma_resv_reserve_shared() has been called. |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 239 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 240 | void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 241 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 242 | struct dma_resv_list *fobj; |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 243 | struct dma_fence *old; |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 244 | unsigned int i, count; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 245 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 246 | dma_fence_get(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 247 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 248 | dma_resv_assert_held(obj); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 249 | |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 250 | fobj = dma_resv_shared_list(obj); |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 251 | count = fobj->shared_count; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 252 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 253 | write_seqcount_begin(&obj->seq); |
| 254 | |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 255 | for (i = 0; i < count; ++i) { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 256 | |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 257 | old = rcu_dereference_protected(fobj->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 258 | dma_resv_held(obj)); |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 259 | if (old->context == fence->context || |
| 260 | dma_fence_is_signaled(old)) |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 261 | goto replace; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | BUG_ON(fobj->shared_count >= fobj->shared_max); |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 265 | old = NULL; |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 266 | count++; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 267 | |
| 268 | replace: |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 269 | RCU_INIT_POINTER(fobj->shared[i], fence); |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 270 | /* pointer update must be visible before we extend the shared_count */ |
| 271 | smp_store_mb(fobj->shared_count, count); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 272 | |
| 273 | write_seqcount_end(&obj->seq); |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 274 | dma_fence_put(old); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 275 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 276 | EXPORT_SYMBOL(dma_resv_add_shared_fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 277 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 278 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 279 | * dma_resv_add_excl_fence - Add an exclusive fence. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 280 | * @obj: the reservation object |
| 281 | * @fence: the shared fence to add |
| 282 | * |
| 283 | * Add a fence to the exclusive slot. The obj->lock must be held. |
| 284 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 285 | void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 286 | { |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 287 | struct dma_fence *old_fence = dma_resv_excl_fence(obj); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 288 | struct dma_resv_list *old; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 289 | u32 i = 0; |
| 290 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 291 | dma_resv_assert_held(obj); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 292 | |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 293 | old = dma_resv_shared_list(obj); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 294 | if (old) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 295 | i = old->shared_count; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 296 | |
| 297 | if (fence) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 298 | dma_fence_get(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 299 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 300 | write_seqcount_begin(&obj->seq); |
| 301 | /* write_seqcount_begin provides the necessary memory barrier */ |
| 302 | RCU_INIT_POINTER(obj->fence_excl, fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 303 | if (old) |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 304 | old->shared_count = 0; |
| 305 | write_seqcount_end(&obj->seq); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 306 | |
| 307 | /* inplace update, no shared fences */ |
| 308 | while (i--) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 309 | dma_fence_put(rcu_dereference_protected(old->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 310 | dma_resv_held(obj))); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 311 | |
Christian König | f3e31b7 | 2017-08-07 17:32:21 -0400 | [diff] [blame] | 312 | dma_fence_put(old_fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 313 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 314 | EXPORT_SYMBOL(dma_resv_add_excl_fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 315 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 316 | /** |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 317 | * dma_resv_copy_fences - Copy all fences from src to dst. |
| 318 | * @dst: the destination reservation object |
| 319 | * @src: the source reservation object |
| 320 | * |
| 321 | * Copy all fences from src to dst. dst-lock must be held. |
| 322 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 323 | int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src) |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 324 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 325 | struct dma_resv_list *src_list, *dst_list; |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 326 | struct dma_fence *old, *new; |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 327 | unsigned int i; |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 328 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 329 | dma_resv_assert_held(dst); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 330 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 331 | rcu_read_lock(); |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 332 | src_list = dma_resv_shared_list(src); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 333 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 334 | retry: |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 335 | if (src_list) { |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 336 | unsigned int shared_count = src_list->shared_count; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 337 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 338 | rcu_read_unlock(); |
| 339 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 340 | dst_list = dma_resv_list_alloc(shared_count); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 341 | if (!dst_list) |
| 342 | return -ENOMEM; |
| 343 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 344 | rcu_read_lock(); |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 345 | src_list = dma_resv_shared_list(src); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 346 | if (!src_list || src_list->shared_count > shared_count) { |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 347 | kfree(dst_list); |
| 348 | goto retry; |
| 349 | } |
| 350 | |
| 351 | dst_list->shared_count = 0; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 352 | for (i = 0; i < src_list->shared_count; ++i) { |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 353 | struct dma_fence __rcu **dst; |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 354 | struct dma_fence *fence; |
| 355 | |
| 356 | fence = rcu_dereference(src_list->shared[i]); |
| 357 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 358 | &fence->flags)) |
| 359 | continue; |
| 360 | |
| 361 | if (!dma_fence_get_rcu(fence)) { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 362 | dma_resv_list_free(dst_list); |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 363 | src_list = dma_resv_shared_list(src); |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 364 | goto retry; |
| 365 | } |
| 366 | |
| 367 | if (dma_fence_is_signaled(fence)) { |
| 368 | dma_fence_put(fence); |
| 369 | continue; |
| 370 | } |
| 371 | |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 372 | dst = &dst_list->shared[dst_list->shared_count++]; |
| 373 | rcu_assign_pointer(*dst, fence); |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 374 | } |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 375 | } else { |
| 376 | dst_list = NULL; |
| 377 | } |
| 378 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 379 | new = dma_fence_get_rcu_safe(&src->fence_excl); |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 380 | rcu_read_unlock(); |
| 381 | |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 382 | src_list = dma_resv_shared_list(dst); |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 383 | old = dma_resv_excl_fence(dst); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 384 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 385 | write_seqcount_begin(&dst->seq); |
| 386 | /* write_seqcount_begin provides the necessary memory barrier */ |
| 387 | RCU_INIT_POINTER(dst->fence_excl, new); |
| 388 | RCU_INIT_POINTER(dst->fence, dst_list); |
| 389 | write_seqcount_end(&dst->seq); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 390 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 391 | dma_resv_list_free(src_list); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 392 | dma_fence_put(old); |
| 393 | |
| 394 | return 0; |
| 395 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 396 | EXPORT_SYMBOL(dma_resv_copy_fences); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 397 | |
| 398 | /** |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 399 | * dma_resv_get_fences - Get an object's shared and exclusive |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 400 | * fences without update side lock held |
| 401 | * @obj: the reservation object |
| 402 | * @pfence_excl: the returned exclusive fence (or NULL) |
| 403 | * @pshared_count: the number of shared fences returned |
| 404 | * @pshared: the array of shared fence ptrs returned (array is krealloc'd to |
| 405 | * the required size, and must be freed by caller) |
| 406 | * |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 407 | * Retrieve all fences from the reservation object. If the pointer for the |
| 408 | * exclusive fence is not specified the fence is put into the array of the |
| 409 | * shared fences as well. Returns either zero or -ENOMEM. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 410 | */ |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 411 | int dma_resv_get_fences(struct dma_resv *obj, struct dma_fence **pfence_excl, |
| 412 | unsigned int *pshared_count, |
| 413 | struct dma_fence ***pshared) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 414 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 415 | struct dma_fence **shared = NULL; |
| 416 | struct dma_fence *fence_excl; |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 417 | unsigned int shared_count; |
| 418 | int ret = 1; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 419 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 420 | do { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 421 | struct dma_resv_list *fobj; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 422 | unsigned int i, seq; |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 423 | size_t sz = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 424 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 425 | shared_count = i = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 426 | |
| 427 | rcu_read_lock(); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 428 | seq = read_seqcount_begin(&obj->seq); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 429 | |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 430 | fence_excl = dma_resv_excl_fence(obj); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 431 | if (fence_excl && !dma_fence_get_rcu(fence_excl)) |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 432 | goto unlock; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 433 | |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 434 | fobj = dma_resv_shared_list(obj); |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 435 | if (fobj) |
| 436 | sz += sizeof(*shared) * fobj->shared_max; |
| 437 | |
| 438 | if (!pfence_excl && fence_excl) |
| 439 | sz += sizeof(*shared); |
| 440 | |
| 441 | if (sz) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 442 | struct dma_fence **nshared; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 443 | |
| 444 | nshared = krealloc(shared, sz, |
| 445 | GFP_NOWAIT | __GFP_NOWARN); |
| 446 | if (!nshared) { |
| 447 | rcu_read_unlock(); |
Chris Wilson | f5b07b0 | 2019-06-04 13:53:23 +0100 | [diff] [blame] | 448 | |
| 449 | dma_fence_put(fence_excl); |
| 450 | fence_excl = NULL; |
| 451 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 452 | nshared = krealloc(shared, sz, GFP_KERNEL); |
| 453 | if (nshared) { |
| 454 | shared = nshared; |
| 455 | continue; |
| 456 | } |
| 457 | |
| 458 | ret = -ENOMEM; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | shared = nshared; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 462 | shared_count = fobj ? fobj->shared_count : 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 463 | for (i = 0; i < shared_count; ++i) { |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 464 | shared[i] = rcu_dereference(fobj->shared[i]); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 465 | if (!dma_fence_get_rcu(shared[i])) |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 466 | break; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 467 | } |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 468 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 469 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 470 | if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) { |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 471 | while (i--) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 472 | dma_fence_put(shared[i]); |
| 473 | dma_fence_put(fence_excl); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 474 | goto unlock; |
| 475 | } |
| 476 | |
| 477 | ret = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 478 | unlock: |
| 479 | rcu_read_unlock(); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 480 | } while (ret); |
| 481 | |
Christian König | b8c036d | 2019-08-05 11:49:20 +0200 | [diff] [blame] | 482 | if (pfence_excl) |
| 483 | *pfence_excl = fence_excl; |
| 484 | else if (fence_excl) |
Qiang Yu | 7fbd078 | 2019-09-22 15:49:00 +0800 | [diff] [blame] | 485 | shared[shared_count++] = fence_excl; |
Christian König | b8c036d | 2019-08-05 11:49:20 +0200 | [diff] [blame] | 486 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 487 | if (!shared_count) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 488 | kfree(shared); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 489 | shared = NULL; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 490 | } |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 491 | |
| 492 | *pshared_count = shared_count; |
| 493 | *pshared = shared; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 494 | return ret; |
| 495 | } |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 496 | EXPORT_SYMBOL_GPL(dma_resv_get_fences); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 497 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 498 | /** |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 499 | * dma_resv_wait_timeout - Wait on reservation's objects |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 500 | * shared and/or exclusive fences. |
| 501 | * @obj: the reservation object |
| 502 | * @wait_all: if true, wait on all fences, else wait on just exclusive fence |
| 503 | * @intr: if true, do interruptible wait |
| 504 | * @timeout: timeout value in jiffies or zero to return immediately |
| 505 | * |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 506 | * Callers are not required to hold specific locks, but maybe hold |
| 507 | * dma_resv_lock() already |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 508 | * RETURNS |
| 509 | * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or |
| 510 | * greater than zer on success. |
| 511 | */ |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 512 | long dma_resv_wait_timeout(struct dma_resv *obj, bool wait_all, bool intr, |
| 513 | unsigned long timeout) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 514 | { |
Christian König | 06a66b5 | 2016-11-07 16:16:16 -0500 | [diff] [blame] | 515 | long ret = timeout ? timeout : 1; |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 516 | unsigned int seq, shared_count; |
| 517 | struct dma_fence *fence; |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 518 | int i; |
Jammy Zhou | fb8b7d2 | 2015-01-21 18:35:47 +0800 | [diff] [blame] | 519 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 520 | retry: |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 521 | shared_count = 0; |
| 522 | seq = read_seqcount_begin(&obj->seq); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 523 | rcu_read_lock(); |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 524 | i = -1; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 525 | |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 526 | fence = dma_resv_excl_fence(obj); |
Christian König | b88fa00 | 2017-08-10 13:01:49 -0400 | [diff] [blame] | 527 | if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
| 528 | if (!dma_fence_get_rcu(fence)) |
| 529 | goto unlock_retry; |
| 530 | |
| 531 | if (dma_fence_is_signaled(fence)) { |
| 532 | dma_fence_put(fence); |
| 533 | fence = NULL; |
| 534 | } |
| 535 | |
| 536 | } else { |
| 537 | fence = NULL; |
| 538 | } |
| 539 | |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 540 | if (wait_all) { |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 541 | struct dma_resv_list *fobj = dma_resv_shared_list(obj); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 542 | |
| 543 | if (fobj) |
| 544 | shared_count = fobj->shared_count; |
| 545 | |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 546 | for (i = 0; !fence && i < shared_count; ++i) { |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 547 | struct dma_fence *lfence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 548 | |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 549 | lfence = rcu_dereference(fobj->shared[i]); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 550 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 551 | &lfence->flags)) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 552 | continue; |
| 553 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 554 | if (!dma_fence_get_rcu(lfence)) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 555 | goto unlock_retry; |
| 556 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 557 | if (dma_fence_is_signaled(lfence)) { |
| 558 | dma_fence_put(lfence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 559 | continue; |
| 560 | } |
| 561 | |
| 562 | fence = lfence; |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 567 | rcu_read_unlock(); |
| 568 | if (fence) { |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 569 | if (read_seqcount_retry(&obj->seq, seq)) { |
| 570 | dma_fence_put(fence); |
| 571 | goto retry; |
| 572 | } |
| 573 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 574 | ret = dma_fence_wait_timeout(fence, intr, ret); |
| 575 | dma_fence_put(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 576 | if (ret > 0 && wait_all && (i + 1 < shared_count)) |
| 577 | goto retry; |
| 578 | } |
| 579 | return ret; |
| 580 | |
| 581 | unlock_retry: |
| 582 | rcu_read_unlock(); |
| 583 | goto retry; |
| 584 | } |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 585 | EXPORT_SYMBOL_GPL(dma_resv_wait_timeout); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 586 | |
| 587 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 588 | static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 589 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 590 | struct dma_fence *fence, *lfence = passed_fence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 591 | int ret = 1; |
| 592 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 593 | if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) { |
| 594 | fence = dma_fence_get_rcu(lfence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 595 | if (!fence) |
| 596 | return -1; |
| 597 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 598 | ret = !!dma_fence_is_signaled(fence); |
| 599 | dma_fence_put(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 600 | } |
| 601 | return ret; |
| 602 | } |
| 603 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 604 | /** |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 605 | * dma_resv_test_signaled - Test if a reservation object's fences have been |
| 606 | * signaled. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 607 | * @obj: the reservation object |
| 608 | * @test_all: if true, test all fences, otherwise only test the exclusive |
| 609 | * fence |
| 610 | * |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 611 | * Callers are not required to hold specific locks, but maybe hold |
| 612 | * dma_resv_lock() already |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 613 | * RETURNS |
| 614 | * true if all fences signaled, else false |
| 615 | */ |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 616 | bool dma_resv_test_signaled(struct dma_resv *obj, bool test_all) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 617 | { |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 618 | struct dma_fence *fence; |
| 619 | unsigned int seq; |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 620 | int ret; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 621 | |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 622 | rcu_read_lock(); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 623 | retry: |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 624 | ret = true; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 625 | seq = read_seqcount_begin(&obj->seq); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 626 | |
| 627 | if (test_all) { |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 628 | struct dma_resv_list *fobj = dma_resv_shared_list(obj); |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 629 | unsigned int i, shared_count; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 630 | |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 631 | shared_count = fobj ? fobj->shared_count : 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 632 | for (i = 0; i < shared_count; ++i) { |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 633 | fence = rcu_dereference(fobj->shared[i]); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 634 | ret = dma_resv_test_signaled_single(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 635 | if (ret < 0) |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 636 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 637 | else if (!ret) |
| 638 | break; |
| 639 | } |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 640 | } |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 641 | |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 642 | fence = dma_resv_excl_fence(obj); |
| 643 | if (ret && fence) { |
| 644 | ret = dma_resv_test_signaled_single(fence); |
| 645 | if (ret < 0) |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 646 | goto retry; |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 647 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Christian König | 9d38814 | 2021-06-06 11:46:33 +0200 | [diff] [blame^] | 650 | if (read_seqcount_retry(&obj->seq, seq)) |
| 651 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 652 | |
| 653 | rcu_read_unlock(); |
| 654 | return ret; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 655 | } |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 656 | EXPORT_SYMBOL_GPL(dma_resv_test_signaled); |
Christian König | 068d9d7 | 2021-05-11 10:42:52 +0200 | [diff] [blame] | 657 | |
| 658 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 659 | static int __init dma_resv_lockdep(void) |
| 660 | { |
| 661 | struct mm_struct *mm = mm_alloc(); |
| 662 | struct ww_acquire_ctx ctx; |
| 663 | struct dma_resv obj; |
| 664 | struct address_space mapping; |
| 665 | int ret; |
| 666 | |
| 667 | if (!mm) |
| 668 | return -ENOMEM; |
| 669 | |
| 670 | dma_resv_init(&obj); |
| 671 | address_space_init_once(&mapping); |
| 672 | |
| 673 | mmap_read_lock(mm); |
| 674 | ww_acquire_init(&ctx, &reservation_ww_class); |
| 675 | ret = dma_resv_lock(&obj, &ctx); |
| 676 | if (ret == -EDEADLK) |
| 677 | dma_resv_lock_slow(&obj, &ctx); |
| 678 | fs_reclaim_acquire(GFP_KERNEL); |
| 679 | /* for unmap_mapping_range on trylocked buffer objects in shrinkers */ |
| 680 | i_mmap_lock_write(&mapping); |
| 681 | i_mmap_unlock_write(&mapping); |
| 682 | #ifdef CONFIG_MMU_NOTIFIER |
| 683 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); |
| 684 | __dma_fence_might_wait(); |
| 685 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); |
| 686 | #else |
| 687 | __dma_fence_might_wait(); |
| 688 | #endif |
| 689 | fs_reclaim_release(GFP_KERNEL); |
| 690 | ww_mutex_unlock(&obj.lock); |
| 691 | ww_acquire_fini(&ctx); |
| 692 | mmap_read_unlock(mm); |
| 693 | |
| 694 | mmput(mm); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | subsys_initcall(dma_resv_lockdep); |
| 699 | #endif |