blob: e744fd87c63c8b604d64516bc73c9052d913f991 [file] [log] [blame]
Christian König068d9d72021-05-11 10:42:52 +02001// SPDX-License-Identifier: MIT
Maarten Lankhorst786d7252013-06-27 13:48:16 +02002/*
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +02003 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
Maarten Lankhorst786d7252013-06-27 13:48:16 +02004 *
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önig52791ee2019-08-11 10:06:32 +020036#include <linux/dma-resv.h>
Maarten Lankhorst786d7252013-06-27 13:48:16 +020037#include <linux/export.h>
Michel Lespinasse0adf65f2020-06-08 21:33:21 -070038#include <linux/mm.h>
Daniel Vetterb2a81162019-11-04 18:37:59 +010039#include <linux/sched/mm.h>
Daniel Vetterd0b9a9a2020-07-07 22:12:06 +020040#include <linux/mmu_notifier.h>
Maarten Lankhorst786d7252013-06-27 13:48:16 +020041
Rob Clarkdad6c392016-03-31 16:26:51 -040042/**
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 Hellstrom08295b32018-06-15 10:17:38 +020053DEFINE_WD_CLASS(reservation_ww_class);
Maarten Lankhorst786d7252013-06-27 13:48:16 +020054EXPORT_SYMBOL(reservation_ww_class);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +020055
Rob Clarkdad6c392016-03-31 16:26:51 -040056/**
Christian König52791ee2019-08-11 10:06:32 +020057 * dma_resv_list_alloc - allocate fence list
Christian König96e95492019-08-06 13:33:12 +020058 * @shared_max: number of fences we need space for
59 *
Christian König52791ee2019-08-11 10:06:32 +020060 * Allocate a new dma_resv_list and make sure to correctly initialize
Christian König96e95492019-08-06 13:33:12 +020061 * shared_max.
62 */
Christian König52791ee2019-08-11 10:06:32 +020063static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
Christian König96e95492019-08-06 13:33:12 +020064{
Christian König52791ee2019-08-11 10:06:32 +020065 struct dma_resv_list *list;
Christian König96e95492019-08-06 13:33:12 +020066
Christian König82e1b932020-10-08 10:03:22 +020067 list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL);
Christian König96e95492019-08-06 13:33:12 +020068 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önig52791ee2019-08-11 10:06:32 +020078 * dma_resv_list_free - free fence list
Christian König96e95492019-08-06 13:33:12 +020079 * @list: list to free
80 *
Christian König52791ee2019-08-11 10:06:32 +020081 * Free a dma_resv_list and make sure to drop all references.
Christian König96e95492019-08-06 13:33:12 +020082 */
Christian König52791ee2019-08-11 10:06:32 +020083static void dma_resv_list_free(struct dma_resv_list *list)
Christian König96e95492019-08-06 13:33:12 +020084{
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önig52791ee2019-08-11 10:06:32 +020097 * dma_resv_init - initialize a reservation object
Christian König8735f162019-06-26 16:31:46 +020098 * @obj: the reservation object
99 */
Christian König52791ee2019-08-11 10:06:32 +0200100void dma_resv_init(struct dma_resv *obj)
Christian König8735f162019-06-26 16:31:46 +0200101{
102 ww_mutex_init(&obj->lock, &reservation_ww_class);
Ahmed S. Darwishcd29f222020-07-20 17:55:18 +0200103 seqcount_ww_mutex_init(&obj->seq, &obj->lock);
Chris Wilsonb016cd62019-08-14 19:24:01 +0100104
Christian König8735f162019-06-26 16:31:46 +0200105 RCU_INIT_POINTER(obj->fence, NULL);
106 RCU_INIT_POINTER(obj->fence_excl, NULL);
107}
Christian König52791ee2019-08-11 10:06:32 +0200108EXPORT_SYMBOL(dma_resv_init);
Christian König8735f162019-06-26 16:31:46 +0200109
110/**
Christian König52791ee2019-08-11 10:06:32 +0200111 * dma_resv_fini - destroys a reservation object
Christian König8735f162019-06-26 16:31:46 +0200112 * @obj: the reservation object
113 */
Christian König52791ee2019-08-11 10:06:32 +0200114void dma_resv_fini(struct dma_resv *obj)
Christian König8735f162019-06-26 16:31:46 +0200115{
Christian König52791ee2019-08-11 10:06:32 +0200116 struct dma_resv_list *fobj;
Christian König8735f162019-06-26 16:31:46 +0200117 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önig52791ee2019-08-11 10:06:32 +0200128 dma_resv_list_free(fobj);
Christian König8735f162019-06-26 16:31:46 +0200129 ww_mutex_destroy(&obj->lock);
130}
Christian König52791ee2019-08-11 10:06:32 +0200131EXPORT_SYMBOL(dma_resv_fini);
Christian König8735f162019-06-26 16:31:46 +0200132
133/**
Christian König52791ee2019-08-11 10:06:32 +0200134 * dma_resv_reserve_shared - Reserve space to add shared fences to
135 * a dma_resv.
Rob Clarkdad6c392016-03-31 16:26:51 -0400136 * @obj: reservation object
Christian Königca053592018-09-19 16:12:25 +0200137 * @num_fences: number of fences we want to add
Rob Clarkdad6c392016-03-31 16:26:51 -0400138 *
Christian König52791ee2019-08-11 10:06:32 +0200139 * Should be called before dma_resv_add_shared_fence(). Must
Rob Clarkdad6c392016-03-31 16:26:51 -0400140 * be called with obj->lock held.
141 *
142 * RETURNS
143 * Zero for success, or -errno
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200144 */
Christian König52791ee2019-08-11 10:06:32 +0200145int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200146{
Christian König52791ee2019-08-11 10:06:32 +0200147 struct dma_resv_list *old, *new;
Christian König27836b62018-08-08 16:01:22 +0200148 unsigned int i, j, k, max;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200149
Christian König52791ee2019-08-11 10:06:32 +0200150 dma_resv_assert_held(obj);
Lucas Stach547c7132017-06-13 10:26:46 +0200151
Christian Königfb5ce732021-05-11 14:11:41 +0200152 old = dma_resv_shared_list(obj);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200153 if (old && old->shared_max) {
Christian Königca053592018-09-19 16:12:25 +0200154 if ((old->shared_count + num_fences) <= old->shared_max)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200155 return 0;
Christian König068d9d72021-05-11 10:42:52 +0200156 max = max(old->shared_count + num_fences, old->shared_max * 2);
Christian Königca25fe52017-11-14 15:24:36 +0100157 } else {
Maarten Lankhorstbf897582020-11-24 12:57:07 +0100158 max = max(4ul, roundup_pow_of_two(num_fences));
Christian Königca25fe52017-11-14 15:24:36 +0100159 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200160
Christian König52791ee2019-08-11 10:06:32 +0200161 new = dma_resv_list_alloc(max);
Christian König27836b62018-08-08 16:01:22 +0200162 if (!new)
163 return -ENOMEM;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200164
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önig27836b62018-08-08 16:01:22 +0200171 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
172 struct dma_fence *fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200173
Christian König27836b62018-08-08 16:01:22 +0200174 fence = rcu_dereference_protected(old->shared[i],
Christian König52791ee2019-08-11 10:06:32 +0200175 dma_resv_held(obj));
Christian König27836b62018-08-08 16:01:22 +0200176 if (dma_fence_is_signaled(fence))
177 RCU_INIT_POINTER(new->shared[--k], fence);
Christian König4d9c62e2017-11-14 15:24:35 +0100178 else
Christian König27836b62018-08-08 16:01:22 +0200179 RCU_INIT_POINTER(new->shared[j++], fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200180 }
Christian König27836b62018-08-08 16:01:22 +0200181 new->shared_count = j;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200182
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200183 /*
Chris Wilson30fe7b02019-07-12 09:03:14 +0100184 * 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 Lankhorst3c3b1772014-07-01 12:58:00 +0200190 */
Chris Wilson30fe7b02019-07-12 09:03:14 +0100191 rcu_assign_pointer(obj->fence, new);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200192
Christian König4d9c62e2017-11-14 15:24:35 +0100193 if (!old)
Christian König27836b62018-08-08 16:01:22 +0200194 return 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200195
Christian König4d9c62e2017-11-14 15:24:35 +0100196 /* Drop the references to the signaled fences */
Chris Wilson94eb1e12019-07-12 09:03:13 +0100197 for (i = k; i < max; ++i) {
Christian König27836b62018-08-08 16:01:22 +0200198 struct dma_fence *fence;
Christian König4d9c62e2017-11-14 15:24:35 +0100199
Christian König27836b62018-08-08 16:01:22 +0200200 fence = rcu_dereference_protected(new->shared[i],
Christian König52791ee2019-08-11 10:06:32 +0200201 dma_resv_held(obj));
Christian König27836b62018-08-08 16:01:22 +0200202 dma_fence_put(fence);
Christian König4d9c62e2017-11-14 15:24:35 +0100203 }
204 kfree_rcu(old, rcu);
Christian König27836b62018-08-08 16:01:22 +0200205
206 return 0;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200207}
Christian König52791ee2019-08-11 10:06:32 +0200208EXPORT_SYMBOL(dma_resv_reserve_shared);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200209
Christian König0c6b5222021-05-06 14:16:01 +0200210#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 */
219void dma_resv_reset_shared_max(struct dma_resv *obj)
220{
Christian Königfb5ce732021-05-11 14:11:41 +0200221 struct dma_resv_list *fences = dma_resv_shared_list(obj);
Christian König0c6b5222021-05-06 14:16:01 +0200222
Christian Königfb5ce732021-05-11 14:11:41 +0200223 dma_resv_assert_held(obj);
224
225 /* Test shared fence slot reservation */
226 if (fences)
227 fences->shared_max = fences->shared_count;
Christian König0c6b5222021-05-06 14:16:01 +0200228}
Christian König415f6762021-06-04 17:47:39 +0200229EXPORT_SYMBOL(dma_resv_reset_shared_max);
Christian König0c6b5222021-05-06 14:16:01 +0200230#endif
231
Rob Clarkdad6c392016-03-31 16:26:51 -0400232/**
Christian König52791ee2019-08-11 10:06:32 +0200233 * dma_resv_add_shared_fence - Add a fence to a shared slot
Rob Clarkdad6c392016-03-31 16:26:51 -0400234 * @obj: the reservation object
235 * @fence: the shared fence to add
236 *
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200237 * Add a fence to a shared slot, obj->lock must be held, and
Christian König52791ee2019-08-11 10:06:32 +0200238 * dma_resv_reserve_shared() has been called.
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200239 */
Christian König52791ee2019-08-11 10:06:32 +0200240void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200241{
Christian König52791ee2019-08-11 10:06:32 +0200242 struct dma_resv_list *fobj;
Christian König93505ee2019-08-05 11:14:27 +0200243 struct dma_fence *old;
Chris Wilsona590d0f2018-10-26 09:03:02 +0100244 unsigned int i, count;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200245
Christian König27836b62018-08-08 16:01:22 +0200246 dma_fence_get(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200247
Christian König52791ee2019-08-11 10:06:32 +0200248 dma_resv_assert_held(obj);
Lucas Stach547c7132017-06-13 10:26:46 +0200249
Christian Königfb5ce732021-05-11 14:11:41 +0200250 fobj = dma_resv_shared_list(obj);
Chris Wilsona590d0f2018-10-26 09:03:02 +0100251 count = fobj->shared_count;
Christian König27836b62018-08-08 16:01:22 +0200252
Chris Wilsonb016cd62019-08-14 19:24:01 +0100253 write_seqcount_begin(&obj->seq);
254
Chris Wilsona590d0f2018-10-26 09:03:02 +0100255 for (i = 0; i < count; ++i) {
Christian König27836b62018-08-08 16:01:22 +0200256
Christian König93505ee2019-08-05 11:14:27 +0200257 old = rcu_dereference_protected(fobj->shared[i],
Christian König52791ee2019-08-11 10:06:32 +0200258 dma_resv_held(obj));
Christian König93505ee2019-08-05 11:14:27 +0200259 if (old->context == fence->context ||
260 dma_fence_is_signaled(old))
Christian König27836b62018-08-08 16:01:22 +0200261 goto replace;
Christian König27836b62018-08-08 16:01:22 +0200262 }
263
264 BUG_ON(fobj->shared_count >= fobj->shared_max);
Christian König93505ee2019-08-05 11:14:27 +0200265 old = NULL;
Chris Wilsona590d0f2018-10-26 09:03:02 +0100266 count++;
Christian König27836b62018-08-08 16:01:22 +0200267
268replace:
Christian König27836b62018-08-08 16:01:22 +0200269 RCU_INIT_POINTER(fobj->shared[i], fence);
Chris Wilsona590d0f2018-10-26 09:03:02 +0100270 /* pointer update must be visible before we extend the shared_count */
271 smp_store_mb(fobj->shared_count, count);
Chris Wilsonb016cd62019-08-14 19:24:01 +0100272
273 write_seqcount_end(&obj->seq);
Christian König93505ee2019-08-05 11:14:27 +0200274 dma_fence_put(old);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200275}
Christian König52791ee2019-08-11 10:06:32 +0200276EXPORT_SYMBOL(dma_resv_add_shared_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200277
Rob Clarkdad6c392016-03-31 16:26:51 -0400278/**
Christian König52791ee2019-08-11 10:06:32 +0200279 * dma_resv_add_excl_fence - Add an exclusive fence.
Rob Clarkdad6c392016-03-31 16:26:51 -0400280 * @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önig52791ee2019-08-11 10:06:32 +0200285void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200286{
Christian König6edbd6a2021-05-10 16:14:09 +0200287 struct dma_fence *old_fence = dma_resv_excl_fence(obj);
Christian König52791ee2019-08-11 10:06:32 +0200288 struct dma_resv_list *old;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200289 u32 i = 0;
290
Christian König52791ee2019-08-11 10:06:32 +0200291 dma_resv_assert_held(obj);
Lucas Stach547c7132017-06-13 10:26:46 +0200292
Christian Königfb5ce732021-05-11 14:11:41 +0200293 old = dma_resv_shared_list(obj);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200294 if (old)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200295 i = old->shared_count;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200296
297 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100298 dma_fence_get(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200299
Chris Wilsonb016cd62019-08-14 19:24:01 +0100300 write_seqcount_begin(&obj->seq);
301 /* write_seqcount_begin provides the necessary memory barrier */
302 RCU_INIT_POINTER(obj->fence_excl, fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200303 if (old)
Chris Wilsonb016cd62019-08-14 19:24:01 +0100304 old->shared_count = 0;
305 write_seqcount_end(&obj->seq);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200306
307 /* inplace update, no shared fences */
308 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100309 dma_fence_put(rcu_dereference_protected(old->shared[i],
Christian König52791ee2019-08-11 10:06:32 +0200310 dma_resv_held(obj)));
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200311
Christian Königf3e31b72017-08-07 17:32:21 -0400312 dma_fence_put(old_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200313}
Christian König52791ee2019-08-11 10:06:32 +0200314EXPORT_SYMBOL(dma_resv_add_excl_fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200315
Rob Clarkdad6c392016-03-31 16:26:51 -0400316/**
Christian König068d9d72021-05-11 10:42:52 +0200317 * 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önig52791ee2019-08-11 10:06:32 +0200323int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
Christian König7faf9522017-08-10 13:01:48 -0400324{
Christian König52791ee2019-08-11 10:06:32 +0200325 struct dma_resv_list *src_list, *dst_list;
Christian König7faf9522017-08-10 13:01:48 -0400326 struct dma_fence *old, *new;
Christian König068d9d72021-05-11 10:42:52 +0200327 unsigned int i;
Christian König7faf9522017-08-10 13:01:48 -0400328
Christian König52791ee2019-08-11 10:06:32 +0200329 dma_resv_assert_held(dst);
Lucas Stach547c7132017-06-13 10:26:46 +0200330
Christian König39e16ba2017-09-04 21:02:45 +0200331 rcu_read_lock();
Christian Königfb5ce732021-05-11 14:11:41 +0200332 src_list = dma_resv_shared_list(src);
Christian König7faf9522017-08-10 13:01:48 -0400333
Christian König39e16ba2017-09-04 21:02:45 +0200334retry:
Chris Wilsonb016cd62019-08-14 19:24:01 +0100335 if (src_list) {
Christian König068d9d72021-05-11 10:42:52 +0200336 unsigned int shared_count = src_list->shared_count;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100337
Christian König39e16ba2017-09-04 21:02:45 +0200338 rcu_read_unlock();
339
Christian König52791ee2019-08-11 10:06:32 +0200340 dst_list = dma_resv_list_alloc(shared_count);
Christian König7faf9522017-08-10 13:01:48 -0400341 if (!dst_list)
342 return -ENOMEM;
343
Christian König39e16ba2017-09-04 21:02:45 +0200344 rcu_read_lock();
Christian Königfb5ce732021-05-11 14:11:41 +0200345 src_list = dma_resv_shared_list(src);
Chris Wilsonb016cd62019-08-14 19:24:01 +0100346 if (!src_list || src_list->shared_count > shared_count) {
Christian König39e16ba2017-09-04 21:02:45 +0200347 kfree(dst_list);
348 goto retry;
349 }
350
351 dst_list->shared_count = 0;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100352 for (i = 0; i < src_list->shared_count; ++i) {
Christian König068d9d72021-05-11 10:42:52 +0200353 struct dma_fence __rcu **dst;
Christian König39e16ba2017-09-04 21:02:45 +0200354 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önig52791ee2019-08-11 10:06:32 +0200362 dma_resv_list_free(dst_list);
Christian Königfb5ce732021-05-11 14:11:41 +0200363 src_list = dma_resv_shared_list(src);
Christian König39e16ba2017-09-04 21:02:45 +0200364 goto retry;
365 }
366
367 if (dma_fence_is_signaled(fence)) {
368 dma_fence_put(fence);
369 continue;
370 }
371
Christian König068d9d72021-05-11 10:42:52 +0200372 dst = &dst_list->shared[dst_list->shared_count++];
373 rcu_assign_pointer(*dst, fence);
Christian König39e16ba2017-09-04 21:02:45 +0200374 }
Christian König7faf9522017-08-10 13:01:48 -0400375 } else {
376 dst_list = NULL;
377 }
378
Chris Wilsonb016cd62019-08-14 19:24:01 +0100379 new = dma_fence_get_rcu_safe(&src->fence_excl);
Christian König39e16ba2017-09-04 21:02:45 +0200380 rcu_read_unlock();
381
Christian Königfb5ce732021-05-11 14:11:41 +0200382 src_list = dma_resv_shared_list(dst);
Christian König6edbd6a2021-05-10 16:14:09 +0200383 old = dma_resv_excl_fence(dst);
Christian König7faf9522017-08-10 13:01:48 -0400384
Chris Wilsonb016cd62019-08-14 19:24:01 +0100385 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önig7faf9522017-08-10 13:01:48 -0400390
Christian König52791ee2019-08-11 10:06:32 +0200391 dma_resv_list_free(src_list);
Christian König7faf9522017-08-10 13:01:48 -0400392 dma_fence_put(old);
393
394 return 0;
395}
Christian König52791ee2019-08-11 10:06:32 +0200396EXPORT_SYMBOL(dma_resv_copy_fences);
Christian König7faf9522017-08-10 13:01:48 -0400397
398/**
Christian Königd3fae3b2021-06-02 13:01:15 +0200399 * dma_resv_get_fences - Get an object's shared and exclusive
Rob Clarkdad6c392016-03-31 16:26:51 -0400400 * 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öniga35f2f32018-01-10 13:53:41 +0100407 * 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 Clarkdad6c392016-03-31 16:26:51 -0400410 */
Christian Königd3fae3b2021-06-02 13:01:15 +0200411int dma_resv_get_fences(struct dma_resv *obj, struct dma_fence **pfence_excl,
412 unsigned int *pshared_count,
413 struct dma_fence ***pshared)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200414{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100415 struct dma_fence **shared = NULL;
416 struct dma_fence *fence_excl;
Chris Wilsonfedf5412016-08-29 08:08:30 +0100417 unsigned int shared_count;
418 int ret = 1;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200419
Chris Wilsonfedf5412016-08-29 08:08:30 +0100420 do {
Christian König52791ee2019-08-11 10:06:32 +0200421 struct dma_resv_list *fobj;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100422 unsigned int i, seq;
Christian Königa35f2f32018-01-10 13:53:41 +0100423 size_t sz = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200424
Chris Wilsonb016cd62019-08-14 19:24:01 +0100425 shared_count = i = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200426
427 rcu_read_lock();
Chris Wilsonb016cd62019-08-14 19:24:01 +0100428 seq = read_seqcount_begin(&obj->seq);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100429
Christian König6edbd6a2021-05-10 16:14:09 +0200430 fence_excl = dma_resv_excl_fence(obj);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100431 if (fence_excl && !dma_fence_get_rcu(fence_excl))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100432 goto unlock;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200433
Christian Königfb5ce732021-05-11 14:11:41 +0200434 fobj = dma_resv_shared_list(obj);
Christian Königa35f2f32018-01-10 13:53:41 +0100435 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 Wilsonf54d1862016-10-25 13:00:45 +0100442 struct dma_fence **nshared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200443
444 nshared = krealloc(shared, sz,
445 GFP_NOWAIT | __GFP_NOWARN);
446 if (!nshared) {
447 rcu_read_unlock();
Chris Wilsonf5b07b02019-06-04 13:53:23 +0100448
449 dma_fence_put(fence_excl);
450 fence_excl = NULL;
451
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200452 nshared = krealloc(shared, sz, GFP_KERNEL);
453 if (nshared) {
454 shared = nshared;
455 continue;
456 }
457
458 ret = -ENOMEM;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200459 break;
460 }
461 shared = nshared;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100462 shared_count = fobj ? fobj->shared_count : 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200463 for (i = 0; i < shared_count; ++i) {
Chris Wilsonfedf5412016-08-29 08:08:30 +0100464 shared[i] = rcu_dereference(fobj->shared[i]);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100465 if (!dma_fence_get_rcu(shared[i]))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100466 break;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200467 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100468 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200469
Chris Wilsonb016cd62019-08-14 19:24:01 +0100470 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
Chris Wilsonfedf5412016-08-29 08:08:30 +0100471 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100472 dma_fence_put(shared[i]);
473 dma_fence_put(fence_excl);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100474 goto unlock;
475 }
476
477 ret = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200478unlock:
479 rcu_read_unlock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100480 } while (ret);
481
Christian Königb8c036d2019-08-05 11:49:20 +0200482 if (pfence_excl)
483 *pfence_excl = fence_excl;
484 else if (fence_excl)
Qiang Yu7fbd0782019-09-22 15:49:00 +0800485 shared[shared_count++] = fence_excl;
Christian Königb8c036d2019-08-05 11:49:20 +0200486
Chris Wilsonfedf5412016-08-29 08:08:30 +0100487 if (!shared_count) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200488 kfree(shared);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100489 shared = NULL;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200490 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100491
492 *pshared_count = shared_count;
493 *pshared = shared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200494 return ret;
495}
Christian Königd3fae3b2021-06-02 13:01:15 +0200496EXPORT_SYMBOL_GPL(dma_resv_get_fences);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200497
Rob Clarkdad6c392016-03-31 16:26:51 -0400498/**
Christian Königd3fae3b2021-06-02 13:01:15 +0200499 * dma_resv_wait_timeout - Wait on reservation's objects
Rob Clarkdad6c392016-03-31 16:26:51 -0400500 * 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önigd3fae3b2021-06-02 13:01:15 +0200506 * Callers are not required to hold specific locks, but maybe hold
507 * dma_resv_lock() already
Rob Clarkdad6c392016-03-31 16:26:51 -0400508 * RETURNS
509 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
510 * greater than zer on success.
511 */
Christian Königd3fae3b2021-06-02 13:01:15 +0200512long dma_resv_wait_timeout(struct dma_resv *obj, bool wait_all, bool intr,
513 unsigned long timeout)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200514{
Christian König06a66b52016-11-07 16:16:16 -0500515 long ret = timeout ? timeout : 1;
Christian König068d9d72021-05-11 10:42:52 +0200516 unsigned int seq, shared_count;
517 struct dma_fence *fence;
Christian König5bffee82018-01-22 21:00:03 +0100518 int i;
Jammy Zhoufb8b7d22015-01-21 18:35:47 +0800519
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200520retry:
Chris Wilsonb016cd62019-08-14 19:24:01 +0100521 shared_count = 0;
522 seq = read_seqcount_begin(&obj->seq);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200523 rcu_read_lock();
Christian König5bffee82018-01-22 21:00:03 +0100524 i = -1;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200525
Christian König6edbd6a2021-05-10 16:14:09 +0200526 fence = dma_resv_excl_fence(obj);
Christian Königb88fa002017-08-10 13:01:49 -0400527 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önig5bffee82018-01-22 21:00:03 +0100540 if (wait_all) {
Christian Königfb5ce732021-05-11 14:11:41 +0200541 struct dma_resv_list *fobj = dma_resv_shared_list(obj);
Chris Wilsonb016cd62019-08-14 19:24:01 +0100542
543 if (fobj)
544 shared_count = fobj->shared_count;
545
Christian König5bffee82018-01-22 21:00:03 +0100546 for (i = 0; !fence && i < shared_count; ++i) {
Christian König068d9d72021-05-11 10:42:52 +0200547 struct dma_fence *lfence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200548
Christian König068d9d72021-05-11 10:42:52 +0200549 lfence = rcu_dereference(fobj->shared[i]);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100550 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
551 &lfence->flags))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200552 continue;
553
Chris Wilsonf54d1862016-10-25 13:00:45 +0100554 if (!dma_fence_get_rcu(lfence))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200555 goto unlock_retry;
556
Chris Wilsonf54d1862016-10-25 13:00:45 +0100557 if (dma_fence_is_signaled(lfence)) {
558 dma_fence_put(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200559 continue;
560 }
561
562 fence = lfence;
563 break;
564 }
565 }
566
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200567 rcu_read_unlock();
568 if (fence) {
Chris Wilsonb016cd62019-08-14 19:24:01 +0100569 if (read_seqcount_retry(&obj->seq, seq)) {
570 dma_fence_put(fence);
571 goto retry;
572 }
573
Chris Wilsonf54d1862016-10-25 13:00:45 +0100574 ret = dma_fence_wait_timeout(fence, intr, ret);
575 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200576 if (ret > 0 && wait_all && (i + 1 < shared_count))
577 goto retry;
578 }
579 return ret;
580
581unlock_retry:
582 rcu_read_unlock();
583 goto retry;
584}
Christian Königd3fae3b2021-06-02 13:01:15 +0200585EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200586
587
Christian König52791ee2019-08-11 10:06:32 +0200588static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200589{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100590 struct dma_fence *fence, *lfence = passed_fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200591 int ret = 1;
592
Chris Wilsonf54d1862016-10-25 13:00:45 +0100593 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
594 fence = dma_fence_get_rcu(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200595 if (!fence)
596 return -1;
597
Chris Wilsonf54d1862016-10-25 13:00:45 +0100598 ret = !!dma_fence_is_signaled(fence);
599 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200600 }
601 return ret;
602}
603
Rob Clarkdad6c392016-03-31 16:26:51 -0400604/**
Christian Königd3fae3b2021-06-02 13:01:15 +0200605 * dma_resv_test_signaled - Test if a reservation object's fences have been
606 * signaled.
Rob Clarkdad6c392016-03-31 16:26:51 -0400607 * @obj: the reservation object
608 * @test_all: if true, test all fences, otherwise only test the exclusive
609 * fence
610 *
Christian Königd3fae3b2021-06-02 13:01:15 +0200611 * Callers are not required to hold specific locks, but maybe hold
612 * dma_resv_lock() already
Rob Clarkdad6c392016-03-31 16:26:51 -0400613 * RETURNS
614 * true if all fences signaled, else false
615 */
Christian Königd3fae3b2021-06-02 13:01:15 +0200616bool dma_resv_test_signaled(struct dma_resv *obj, bool test_all)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200617{
Christian König9d388142021-06-06 11:46:33 +0200618 struct dma_fence *fence;
619 unsigned int seq;
Chris Wilsonb68d8372016-08-29 08:08:32 +0100620 int ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200621
Chris Wilsonb68d8372016-08-29 08:08:32 +0100622 rcu_read_lock();
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200623retry:
Chris Wilsonb68d8372016-08-29 08:08:32 +0100624 ret = true;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100625 seq = read_seqcount_begin(&obj->seq);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200626
627 if (test_all) {
Christian Königfb5ce732021-05-11 14:11:41 +0200628 struct dma_resv_list *fobj = dma_resv_shared_list(obj);
Christian König9d388142021-06-06 11:46:33 +0200629 unsigned int i, shared_count;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100630
Christian König9d388142021-06-06 11:46:33 +0200631 shared_count = fobj ? fobj->shared_count : 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200632 for (i = 0; i < shared_count; ++i) {
Christian König068d9d72021-05-11 10:42:52 +0200633 fence = rcu_dereference(fobj->shared[i]);
Christian König52791ee2019-08-11 10:06:32 +0200634 ret = dma_resv_test_signaled_single(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200635 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100636 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200637 else if (!ret)
638 break;
639 }
Christian König9d388142021-06-06 11:46:33 +0200640 }
Chris Wilsonb016cd62019-08-14 19:24:01 +0100641
Christian König9d388142021-06-06 11:46:33 +0200642 fence = dma_resv_excl_fence(obj);
643 if (ret && fence) {
644 ret = dma_resv_test_signaled_single(fence);
645 if (ret < 0)
Chris Wilsonb016cd62019-08-14 19:24:01 +0100646 goto retry;
Christian König9d388142021-06-06 11:46:33 +0200647
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200648 }
649
Christian König9d388142021-06-06 11:46:33 +0200650 if (read_seqcount_retry(&obj->seq, seq))
651 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200652
653 rcu_read_unlock();
654 return ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200655}
Christian Königd3fae3b2021-06-02 13:01:15 +0200656EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
Christian König068d9d72021-05-11 10:42:52 +0200657
658#if IS_ENABLED(CONFIG_LOCKDEP)
659static 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}
698subsys_initcall(dma_resv_lockdep);
699#endif