Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: MIT
  2/*
  3 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
  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
 36#include <linux/dma-resv.h>
 37#include <linux/dma-fence-array.h>
 38#include <linux/export.h>
 39#include <linux/mm.h>
 40#include <linux/sched/mm.h>
 41#include <linux/mmu_notifier.h>
 42#include <linux/seq_file.h>
 43
 44/**
 45 * DOC: Reservation Object Overview
 46 *
 47 * The reservation object provides a mechanism to manage a container of
 48 * dma_fence object associated with a resource. A reservation object
 49 * can have any number of fences attaches to it. Each fence carries an usage
 50 * parameter determining how the operation represented by the fence is using the
 51 * resource. The RCU mechanism is used to protect read access to fences from
 52 * locked write-side updates.
 53 *
 54 * See struct dma_resv for more details.
 55 */
 56
 57DEFINE_WD_CLASS(reservation_ww_class);
 58EXPORT_SYMBOL(reservation_ww_class);
 59
 60/* Mask for the lower fence pointer bits */
 61#define DMA_RESV_LIST_MASK	0x3
 62
 63struct dma_resv_list {
 64	struct rcu_head rcu;
 65	u32 num_fences, max_fences;
 66	struct dma_fence __rcu *table[];
 67};
 68
 69/* Extract the fence and usage flags from an RCU protected entry in the list. */
 70static void dma_resv_list_entry(struct dma_resv_list *list, unsigned int index,
 71				struct dma_resv *resv, struct dma_fence **fence,
 72				enum dma_resv_usage *usage)
 73{
 74	long tmp;
 75
 76	tmp = (long)rcu_dereference_check(list->table[index],
 77					  resv ? dma_resv_held(resv) : true);
 78	*fence = (struct dma_fence *)(tmp & ~DMA_RESV_LIST_MASK);
 79	if (usage)
 80		*usage = tmp & DMA_RESV_LIST_MASK;
 81}
 82
 83/* Set the fence and usage flags at the specific index in the list. */
 84static void dma_resv_list_set(struct dma_resv_list *list,
 85			      unsigned int index,
 86			      struct dma_fence *fence,
 87			      enum dma_resv_usage usage)
 88{
 89	long tmp = ((long)fence) | usage;
 90
 91	RCU_INIT_POINTER(list->table[index], (struct dma_fence *)tmp);
 92}
 93
 94/*
 95 * Allocate a new dma_resv_list and make sure to correctly initialize
 96 * max_fences.
 97 */
 98static struct dma_resv_list *dma_resv_list_alloc(unsigned int max_fences)
 99{
100	struct dma_resv_list *list;
101	size_t size;
102
103	/* Round up to the next kmalloc bucket size. */
104	size = kmalloc_size_roundup(struct_size(list, table, max_fences));
105
106	list = kmalloc(size, GFP_KERNEL);
107	if (!list)
108		return NULL;
109
110	/* Given the resulting bucket size, recalculated max_fences. */
111	list->max_fences = (size - offsetof(typeof(*list), table)) /
112		sizeof(*list->table);
113
114	return list;
115}
116
117/* Free a dma_resv_list and make sure to drop all references. */
 
 
 
 
 
118static void dma_resv_list_free(struct dma_resv_list *list)
119{
120	unsigned int i;
121
122	if (!list)
123		return;
124
125	for (i = 0; i < list->num_fences; ++i) {
126		struct dma_fence *fence;
127
128		dma_resv_list_entry(list, i, NULL, &fence, NULL);
129		dma_fence_put(fence);
130	}
131	kfree_rcu(list, rcu);
132}
133
134/**
135 * dma_resv_init - initialize a reservation object
136 * @obj: the reservation object
137 */
138void dma_resv_init(struct dma_resv *obj)
139{
140	ww_mutex_init(&obj->lock, &reservation_ww_class);
 
141
142	RCU_INIT_POINTER(obj->fences, NULL);
 
143}
144EXPORT_SYMBOL(dma_resv_init);
145
146/**
147 * dma_resv_fini - destroys a reservation object
148 * @obj: the reservation object
149 */
150void dma_resv_fini(struct dma_resv *obj)
151{
 
 
 
152	/*
153	 * This object should be dead and all references must have
154	 * been released to it, so no need to be protected with rcu.
155	 */
156	dma_resv_list_free(rcu_dereference_protected(obj->fences, true));
 
 
 
 
 
157	ww_mutex_destroy(&obj->lock);
158}
159EXPORT_SYMBOL(dma_resv_fini);
160
161/* Dereference the fences while ensuring RCU rules */
162static inline struct dma_resv_list *dma_resv_fences_list(struct dma_resv *obj)
163{
164	return rcu_dereference_check(obj->fences, dma_resv_held(obj));
165}
166
167/**
168 * dma_resv_reserve_fences - Reserve space to add fences to a dma_resv object.
 
169 * @obj: reservation object
170 * @num_fences: number of fences we want to add
171 *
172 * Should be called before dma_resv_add_fence().  Must be called with @obj
173 * locked through dma_resv_lock().
174 *
175 * Note that the preallocated slots need to be re-reserved if @obj is unlocked
176 * at any time before calling dma_resv_add_fence(). This is validated when
177 * CONFIG_DEBUG_MUTEXES is enabled.
178 *
179 * RETURNS
180 * Zero for success, or -errno
181 */
182int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences)
183{
184	struct dma_resv_list *old, *new;
185	unsigned int i, j, k, max;
186
187	dma_resv_assert_held(obj);
188
189	old = dma_resv_fences_list(obj);
190	if (old && old->max_fences) {
191		if ((old->num_fences + num_fences) <= old->max_fences)
192			return 0;
193		max = max(old->num_fences + num_fences, old->max_fences * 2);
194	} else {
195		max = max(4ul, roundup_pow_of_two(num_fences));
196	}
197
198	new = dma_resv_list_alloc(max);
199	if (!new)
200		return -ENOMEM;
201
202	/*
203	 * no need to bump fence refcounts, rcu_read access
204	 * requires the use of kref_get_unless_zero, and the
205	 * references from the old struct are carried over to
206	 * the new.
207	 */
208	for (i = 0, j = 0, k = max; i < (old ? old->num_fences : 0); ++i) {
209		enum dma_resv_usage usage;
210		struct dma_fence *fence;
211
212		dma_resv_list_entry(old, i, obj, &fence, &usage);
 
213		if (dma_fence_is_signaled(fence))
214			RCU_INIT_POINTER(new->table[--k], fence);
215		else
216			dma_resv_list_set(new, j++, fence, usage);
217	}
218	new->num_fences = j;
219
220	/*
221	 * We are not changing the effective set of fences here so can
222	 * merely update the pointer to the new array; both existing
223	 * readers and new readers will see exactly the same set of
224	 * active (unsignaled) fences. Individual fences and the
225	 * old array are protected by RCU and so will not vanish under
226	 * the gaze of the rcu_read_lock() readers.
227	 */
228	rcu_assign_pointer(obj->fences, new);
229
230	if (!old)
231		return 0;
232
233	/* Drop the references to the signaled fences */
234	for (i = k; i < max; ++i) {
235		struct dma_fence *fence;
236
237		fence = rcu_dereference_protected(new->table[i],
238						  dma_resv_held(obj));
239		dma_fence_put(fence);
240	}
241	kfree_rcu(old, rcu);
242
243	return 0;
244}
245EXPORT_SYMBOL(dma_resv_reserve_fences);
246
247#ifdef CONFIG_DEBUG_MUTEXES
248/**
249 * dma_resv_reset_max_fences - reset fences for debugging
250 * @obj: the dma_resv object to reset
251 *
252 * Reset the number of pre-reserved fence slots to test that drivers do
253 * correct slot allocation using dma_resv_reserve_fences(). See also
254 * &dma_resv_list.max_fences.
255 */
256void dma_resv_reset_max_fences(struct dma_resv *obj)
257{
258	struct dma_resv_list *fences = dma_resv_fences_list(obj);
259
260	dma_resv_assert_held(obj);
261
262	/* Test fence slot reservation */
263	if (fences)
264		fences->max_fences = fences->num_fences;
265}
266EXPORT_SYMBOL(dma_resv_reset_max_fences);
267#endif
268
269/**
270 * dma_resv_add_fence - Add a fence to the dma_resv obj
271 * @obj: the reservation object
272 * @fence: the fence to add
273 * @usage: how the fence is used, see enum dma_resv_usage
274 *
275 * Add a fence to a slot, @obj must be locked with dma_resv_lock(), and
276 * dma_resv_reserve_fences() has been called.
277 *
278 * See also &dma_resv.fence for a discussion of the semantics.
 
279 */
280void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
281			enum dma_resv_usage usage)
282{
283	struct dma_resv_list *fobj;
284	struct dma_fence *old;
285	unsigned int i, count;
286
287	dma_fence_get(fence);
288
289	dma_resv_assert_held(obj);
290
291	/* Drivers should not add containers here, instead add each fence
292	 * individually.
293	 */
294	WARN_ON(dma_fence_is_container(fence));
295
296	fobj = dma_resv_fences_list(obj);
297	count = fobj->num_fences;
298
299	for (i = 0; i < count; ++i) {
300		enum dma_resv_usage old_usage;
301
302		dma_resv_list_entry(fobj, i, obj, &old, &old_usage);
303		if ((old->context == fence->context && old_usage >= usage &&
304		     dma_fence_is_later_or_same(fence, old)) ||
305		    dma_fence_is_signaled(old)) {
306			dma_resv_list_set(fobj, i, fence, usage);
307			dma_fence_put(old);
308			return;
309		}
310	}
311
312	BUG_ON(fobj->num_fences >= fobj->max_fences);
 
313	count++;
314
315	dma_resv_list_set(fobj, i, fence, usage);
316	/* pointer update must be visible before we extend the num_fences */
317	smp_store_mb(fobj->num_fences, count);
318}
319EXPORT_SYMBOL(dma_resv_add_fence);
320
321/**
322 * dma_resv_replace_fences - replace fences in the dma_resv obj
323 * @obj: the reservation object
324 * @context: the context of the fences to replace
325 * @replacement: the new fence to use instead
326 * @usage: how the new fence is used, see enum dma_resv_usage
327 *
328 * Replace fences with a specified context with a new fence. Only valid if the
329 * operation represented by the original fence has no longer access to the
330 * resources represented by the dma_resv object when the new fence completes.
331 *
332 * And example for using this is replacing a preemption fence with a page table
333 * update fence which makes the resource inaccessible.
334 */
335void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
336			     struct dma_fence *replacement,
337			     enum dma_resv_usage usage)
338{
339	struct dma_resv_list *list;
340	unsigned int i;
341
342	dma_resv_assert_held(obj);
343
344	list = dma_resv_fences_list(obj);
345	for (i = 0; list && i < list->num_fences; ++i) {
346		struct dma_fence *old;
347
348		dma_resv_list_entry(list, i, obj, &old, NULL);
349		if (old->context != context)
350			continue;
351
352		dma_resv_list_set(list, i, dma_fence_get(replacement), usage);
353		dma_fence_put(old);
354	}
355}
356EXPORT_SYMBOL(dma_resv_replace_fences);
357
358/* Restart the unlocked iteration by initializing the cursor object. */
359static void dma_resv_iter_restart_unlocked(struct dma_resv_iter *cursor)
360{
361	cursor->index = 0;
362	cursor->num_fences = 0;
363	cursor->fences = dma_resv_fences_list(cursor->obj);
364	if (cursor->fences)
365		cursor->num_fences = cursor->fences->num_fences;
366	cursor->is_restarted = true;
367}
368
369/* Walk to the next not signaled fence and grab a reference to it */
370static void dma_resv_iter_walk_unlocked(struct dma_resv_iter *cursor)
371{
372	if (!cursor->fences)
373		return;
374
375	do {
376		/* Drop the reference from the previous round */
377		dma_fence_put(cursor->fence);
378
379		if (cursor->index >= cursor->num_fences) {
380			cursor->fence = NULL;
381			break;
382
383		}
384
385		dma_resv_list_entry(cursor->fences, cursor->index++,
386				    cursor->obj, &cursor->fence,
387				    &cursor->fence_usage);
388		cursor->fence = dma_fence_get_rcu(cursor->fence);
389		if (!cursor->fence) {
390			dma_resv_iter_restart_unlocked(cursor);
391			continue;
392		}
393
394		if (!dma_fence_is_signaled(cursor->fence) &&
395		    cursor->usage >= cursor->fence_usage)
396			break;
397	} while (true);
398}
 
399
400/**
401 * dma_resv_iter_first_unlocked - first fence in an unlocked dma_resv obj.
402 * @cursor: the cursor with the current position
403 *
404 * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
405 *
406 * Beware that the iterator can be restarted.  Code which accumulates statistics
407 * or similar needs to check for this with dma_resv_iter_is_restarted(). For
408 * this reason prefer the locked dma_resv_iter_first() whenver possible.
409 *
410 * Returns the first fence from an unlocked dma_resv obj.
411 */
412struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor)
413{
414	rcu_read_lock();
415	do {
416		dma_resv_iter_restart_unlocked(cursor);
417		dma_resv_iter_walk_unlocked(cursor);
418	} while (dma_resv_fences_list(cursor->obj) != cursor->fences);
419	rcu_read_unlock();
420
421	return cursor->fence;
422}
423EXPORT_SYMBOL(dma_resv_iter_first_unlocked);
424
425/**
426 * dma_resv_iter_next_unlocked - next fence in an unlocked dma_resv obj.
427 * @cursor: the cursor with the current position
428 *
429 * Beware that the iterator can be restarted.  Code which accumulates statistics
430 * or similar needs to check for this with dma_resv_iter_is_restarted(). For
431 * this reason prefer the locked dma_resv_iter_next() whenver possible.
432 *
433 * Returns the next fence from an unlocked dma_resv obj.
434 */
435struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor)
436{
437	bool restart;
438
439	rcu_read_lock();
440	cursor->is_restarted = false;
441	restart = dma_resv_fences_list(cursor->obj) != cursor->fences;
442	do {
443		if (restart)
444			dma_resv_iter_restart_unlocked(cursor);
445		dma_resv_iter_walk_unlocked(cursor);
446		restart = true;
447	} while (dma_resv_fences_list(cursor->obj) != cursor->fences);
448	rcu_read_unlock();
449
450	return cursor->fence;
451}
452EXPORT_SYMBOL(dma_resv_iter_next_unlocked);
453
454/**
455 * dma_resv_iter_first - first fence from a locked dma_resv object
456 * @cursor: cursor to record the current position
457 *
458 * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
459 *
460 * Return the first fence in the dma_resv object while holding the
461 * &dma_resv.lock.
462 */
463struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor)
464{
465	struct dma_fence *fence;
466
467	dma_resv_assert_held(cursor->obj);
468
469	cursor->index = 0;
470	cursor->fences = dma_resv_fences_list(cursor->obj);
471
472	fence = dma_resv_iter_next(cursor);
473	cursor->is_restarted = true;
474	return fence;
475}
476EXPORT_SYMBOL_GPL(dma_resv_iter_first);
477
478/**
479 * dma_resv_iter_next - next fence from a locked dma_resv object
480 * @cursor: cursor to record the current position
481 *
482 * Return the next fences from the dma_resv object while holding the
483 * &dma_resv.lock.
484 */
485struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor)
486{
487	struct dma_fence *fence;
488
489	dma_resv_assert_held(cursor->obj);
490
491	cursor->is_restarted = false;
492
493	do {
494		if (!cursor->fences ||
495		    cursor->index >= cursor->fences->num_fences)
496			return NULL;
497
498		dma_resv_list_entry(cursor->fences, cursor->index++,
499				    cursor->obj, &fence, &cursor->fence_usage);
500	} while (cursor->fence_usage > cursor->usage);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
502	return fence;
503}
504EXPORT_SYMBOL_GPL(dma_resv_iter_next);
505
506/**
507 * dma_resv_copy_fences - Copy all fences from src to dst.
508 * @dst: the destination reservation object
509 * @src: the source reservation object
510 *
511 * Copy all fences from src to dst. dst-lock must be held.
512 */
513int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
514{
515	struct dma_resv_iter cursor;
516	struct dma_resv_list *list;
517	struct dma_fence *f;
518
519	dma_resv_assert_held(dst);
520
521	list = NULL;
 
522
523	dma_resv_iter_begin(&cursor, src, DMA_RESV_USAGE_BOOKKEEP);
524	dma_resv_for_each_fence_unlocked(&cursor, f) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
525
526		if (dma_resv_iter_is_restarted(&cursor)) {
527			dma_resv_list_free(list);
 
 
 
 
 
 
 
 
 
 
 
 
 
528
529			list = dma_resv_list_alloc(cursor.num_fences);
530			if (!list) {
531				dma_resv_iter_end(&cursor);
532				return -ENOMEM;
533			}
534			list->num_fences = 0;
535		}
536
537		dma_fence_get(f);
538		dma_resv_list_set(list, list->num_fences++, f,
539				  dma_resv_iter_usage(&cursor));
 
 
540	}
541	dma_resv_iter_end(&cursor);
542
543	list = rcu_replace_pointer(dst->fences, list, dma_resv_held(dst));
544	dma_resv_list_free(list);
 
 
 
 
 
 
 
 
 
 
 
 
 
545	return 0;
546}
547EXPORT_SYMBOL(dma_resv_copy_fences);
548
549/**
550 * dma_resv_get_fences - Get an object's fences
551 * fences without update side lock held
552 * @obj: the reservation object
553 * @usage: controls which fences to include, see enum dma_resv_usage.
554 * @num_fences: the number of fences returned
555 * @fences: the array of fence ptrs returned (array is krealloc'd to the
556 * required size, and must be freed by caller)
557 *
558 * Retrieve all fences from the reservation object.
559 * Returns either zero or -ENOMEM.
560 */
561int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
562			unsigned int *num_fences, struct dma_fence ***fences)
563{
564	struct dma_resv_iter cursor;
565	struct dma_fence *fence;
 
 
 
 
566
567	*num_fences = 0;
568	*fences = NULL;
 
 
569
570	dma_resv_iter_begin(&cursor, obj, usage);
571	dma_resv_for_each_fence_unlocked(&cursor, fence) {
572
573		if (dma_resv_iter_is_restarted(&cursor)) {
574			struct dma_fence **new_fences;
575			unsigned int count;
576
577			while (*num_fences)
578				dma_fence_put((*fences)[--(*num_fences)]);
579
580			count = cursor.num_fences + 1;
581
582			/* Eventually re-allocate the array */
583			new_fences = krealloc_array(*fences, count,
584						    sizeof(void *),
585						    GFP_KERNEL);
586			if (count && !new_fences) {
587				kfree(*fences);
588				*fences = NULL;
589				*num_fences = 0;
590				dma_resv_iter_end(&cursor);
591				return -ENOMEM;
592			}
593			*fences = new_fences;
594		}
595
596		(*fences)[(*num_fences)++] = dma_fence_get(fence);
597	}
598	dma_resv_iter_end(&cursor);
599
600	return 0;
601}
602EXPORT_SYMBOL_GPL(dma_resv_get_fences);
603
604/**
605 * dma_resv_get_singleton - Get a single fence for all the fences
606 * @obj: the reservation object
607 * @usage: controls which fences to include, see enum dma_resv_usage.
608 * @fence: the resulting fence
609 *
610 * Get a single fence representing all the fences inside the resv object.
611 * Returns either 0 for success or -ENOMEM.
612 *
613 * Warning: This can't be used like this when adding the fence back to the resv
614 * object since that can lead to stack corruption when finalizing the
615 * dma_fence_array.
616 *
617 * Returns 0 on success and negative error values on failure.
618 */
619int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
620			   struct dma_fence **fence)
621{
622	struct dma_fence_array *array;
623	struct dma_fence **fences;
624	unsigned count;
625	int r;
626
627	r = dma_resv_get_fences(obj, usage, &count, &fences);
628        if (r)
629		return r;
630
631	if (count == 0) {
632		*fence = NULL;
633		return 0;
634	}
635
636	if (count == 1) {
637		*fence = fences[0];
638		kfree(fences);
639		return 0;
640	}
641
642	array = dma_fence_array_create(count, fences,
643				       dma_fence_context_alloc(1),
644				       1, false);
645	if (!array) {
646		while (count--)
647			dma_fence_put(fences[count]);
648		kfree(fences);
649		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650	}
651
652	*fence = &array->base;
653	return 0;
 
654}
655EXPORT_SYMBOL_GPL(dma_resv_get_singleton);
656
657/**
658 * dma_resv_wait_timeout - Wait on reservation's objects fences
 
659 * @obj: the reservation object
660 * @usage: controls which fences to include, see enum dma_resv_usage.
661 * @intr: if true, do interruptible wait
662 * @timeout: timeout value in jiffies or zero to return immediately
663 *
664 * Callers are not required to hold specific locks, but maybe hold
665 * dma_resv_lock() already
666 * RETURNS
667 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
668 * greater than zero on success.
669 */
670long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
671			   bool intr, unsigned long timeout)
672{
673	long ret = timeout ? timeout : 1;
674	struct dma_resv_iter cursor;
675	struct dma_fence *fence;
 
 
 
 
 
 
 
676
677	dma_resv_iter_begin(&cursor, obj, usage);
678	dma_resv_for_each_fence_unlocked(&cursor, fence) {
 
 
 
 
 
 
 
679
680		ret = dma_fence_wait_timeout(fence, intr, ret);
681		if (ret <= 0) {
682			dma_resv_iter_end(&cursor);
683			return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
684		}
685	}
686	dma_resv_iter_end(&cursor);
687
 
 
 
 
 
 
 
 
 
 
 
 
688	return ret;
 
 
 
 
689}
690EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
691
692/**
693 * dma_resv_set_deadline - Set a deadline on reservation's objects fences
694 * @obj: the reservation object
695 * @usage: controls which fences to include, see enum dma_resv_usage.
696 * @deadline: the requested deadline (MONOTONIC)
697 *
698 * May be called without holding the dma_resv lock.  Sets @deadline on
699 * all fences filtered by @usage.
700 */
701void dma_resv_set_deadline(struct dma_resv *obj, enum dma_resv_usage usage,
702			   ktime_t deadline)
703{
704	struct dma_resv_iter cursor;
705	struct dma_fence *fence;
706
707	dma_resv_iter_begin(&cursor, obj, usage);
708	dma_resv_for_each_fence_unlocked(&cursor, fence) {
709		dma_fence_set_deadline(fence, deadline);
 
 
 
 
710	}
711	dma_resv_iter_end(&cursor);
712}
713EXPORT_SYMBOL_GPL(dma_resv_set_deadline);
714
715/**
716 * dma_resv_test_signaled - Test if a reservation object's fences have been
717 * signaled.
718 * @obj: the reservation object
719 * @usage: controls which fences to include, see enum dma_resv_usage.
 
720 *
721 * Callers are not required to hold specific locks, but maybe hold
722 * dma_resv_lock() already.
723 *
724 * RETURNS
725 *
726 * True if all fences signaled, else false.
727 */
728bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage)
729{
730	struct dma_resv_iter cursor;
731	struct dma_fence *fence;
 
 
732
733	dma_resv_iter_begin(&cursor, obj, usage);
734	dma_resv_for_each_fence_unlocked(&cursor, fence) {
735		dma_resv_iter_end(&cursor);
736		return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
737	}
738	dma_resv_iter_end(&cursor);
739	return true;
740}
741EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
742
743/**
744 * dma_resv_describe - Dump description of the resv object into seq_file
745 * @obj: the reservation object
746 * @seq: the seq_file to dump the description into
747 *
748 * Dump a textual description of the fences inside an dma_resv object into the
749 * seq_file.
750 */
751void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq)
752{
753	static const char *usage[] = { "kernel", "write", "read", "bookkeep" };
754	struct dma_resv_iter cursor;
755	struct dma_fence *fence;
756
757	dma_resv_for_each_fence(&cursor, obj, DMA_RESV_USAGE_READ, fence) {
758		seq_printf(seq, "\t%s fence:",
759			   usage[dma_resv_iter_usage(&cursor)]);
760		dma_fence_describe(fence, seq);
761	}
 
 
 
 
 
 
762}
763EXPORT_SYMBOL_GPL(dma_resv_describe);
764
765#if IS_ENABLED(CONFIG_LOCKDEP)
766static int __init dma_resv_lockdep(void)
767{
768	struct mm_struct *mm = mm_alloc();
769	struct ww_acquire_ctx ctx;
770	struct dma_resv obj;
771	struct address_space mapping;
772	int ret;
773
774	if (!mm)
775		return -ENOMEM;
776
777	dma_resv_init(&obj);
778	address_space_init_once(&mapping);
779
780	mmap_read_lock(mm);
781	ww_acquire_init(&ctx, &reservation_ww_class);
782	ret = dma_resv_lock(&obj, &ctx);
783	if (ret == -EDEADLK)
784		dma_resv_lock_slow(&obj, &ctx);
785	fs_reclaim_acquire(GFP_KERNEL);
786	/* for unmap_mapping_range on trylocked buffer objects in shrinkers */
787	i_mmap_lock_write(&mapping);
788	i_mmap_unlock_write(&mapping);
789#ifdef CONFIG_MMU_NOTIFIER
790	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
791	__dma_fence_might_wait();
792	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
793#else
794	__dma_fence_might_wait();
795#endif
796	fs_reclaim_release(GFP_KERNEL);
797	ww_mutex_unlock(&obj.lock);
798	ww_acquire_fini(&ctx);
799	mmap_read_unlock(mm);
800
801	mmput(mm);
802
803	return 0;
804}
805subsys_initcall(dma_resv_lockdep);
806#endif
v5.14.15
  1// SPDX-License-Identifier: MIT
  2/*
  3 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
  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
 36#include <linux/dma-resv.h>
 
 37#include <linux/export.h>
 38#include <linux/mm.h>
 39#include <linux/sched/mm.h>
 40#include <linux/mmu_notifier.h>
 
 41
 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
 53DEFINE_WD_CLASS(reservation_ww_class);
 54EXPORT_SYMBOL(reservation_ww_class);
 55
 56/**
 57 * dma_resv_list_alloc - allocate fence list
 58 * @shared_max: number of fences we need space for
 59 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 60 * Allocate a new dma_resv_list and make sure to correctly initialize
 61 * shared_max.
 62 */
 63static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
 64{
 65	struct dma_resv_list *list;
 
 66
 67	list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL);
 
 
 
 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/**
 78 * dma_resv_list_free - free fence list
 79 * @list: list to free
 80 *
 81 * Free a dma_resv_list and make sure to drop all references.
 82 */
 83static void dma_resv_list_free(struct dma_resv_list *list)
 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/**
 97 * dma_resv_init - initialize a reservation object
 98 * @obj: the reservation object
 99 */
100void dma_resv_init(struct dma_resv *obj)
101{
102	ww_mutex_init(&obj->lock, &reservation_ww_class);
103	seqcount_ww_mutex_init(&obj->seq, &obj->lock);
104
105	RCU_INIT_POINTER(obj->fence, NULL);
106	RCU_INIT_POINTER(obj->fence_excl, NULL);
107}
108EXPORT_SYMBOL(dma_resv_init);
109
110/**
111 * dma_resv_fini - destroys a reservation object
112 * @obj: the reservation object
113 */
114void dma_resv_fini(struct dma_resv *obj)
115{
116	struct dma_resv_list *fobj;
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);
128	dma_resv_list_free(fobj);
129	ww_mutex_destroy(&obj->lock);
130}
131EXPORT_SYMBOL(dma_resv_fini);
132
 
 
 
 
 
 
133/**
134 * dma_resv_reserve_shared - Reserve space to add shared fences to
135 * a dma_resv.
136 * @obj: reservation object
137 * @num_fences: number of fences we want to add
138 *
139 * Should be called before dma_resv_add_shared_fence().  Must
140 * be called with obj->lock held.
 
 
 
 
141 *
142 * RETURNS
143 * Zero for success, or -errno
144 */
145int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
146{
147	struct dma_resv_list *old, *new;
148	unsigned int i, j, k, max;
149
150	dma_resv_assert_held(obj);
151
152	old = dma_resv_shared_list(obj);
153	if (old && old->shared_max) {
154		if ((old->shared_count + num_fences) <= old->shared_max)
155			return 0;
156		max = max(old->shared_count + num_fences, old->shared_max * 2);
157	} else {
158		max = max(4ul, roundup_pow_of_two(num_fences));
159	}
160
161	new = dma_resv_list_alloc(max);
162	if (!new)
163		return -ENOMEM;
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	 */
171	for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
 
172		struct dma_fence *fence;
173
174		fence = rcu_dereference_protected(old->shared[i],
175						  dma_resv_held(obj));
176		if (dma_fence_is_signaled(fence))
177			RCU_INIT_POINTER(new->shared[--k], fence);
178		else
179			RCU_INIT_POINTER(new->shared[j++], fence);
180	}
181	new->shared_count = j;
182
183	/*
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.
190	 */
191	rcu_assign_pointer(obj->fence, new);
192
193	if (!old)
194		return 0;
195
196	/* Drop the references to the signaled fences */
197	for (i = k; i < max; ++i) {
198		struct dma_fence *fence;
199
200		fence = rcu_dereference_protected(new->shared[i],
201						  dma_resv_held(obj));
202		dma_fence_put(fence);
203	}
204	kfree_rcu(old, rcu);
205
206	return 0;
207}
208EXPORT_SYMBOL(dma_resv_reserve_shared);
209
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 */
219void dma_resv_reset_shared_max(struct dma_resv *obj)
220{
221	struct dma_resv_list *fences = dma_resv_shared_list(obj);
222
223	dma_resv_assert_held(obj);
224
225	/* Test shared fence slot reservation */
226	if (fences)
227		fences->shared_max = fences->shared_count;
228}
229EXPORT_SYMBOL(dma_resv_reset_shared_max);
230#endif
231
232/**
233 * dma_resv_add_shared_fence - Add a fence to a shared slot
234 * @obj: the reservation object
235 * @fence: the shared fence to add
 
 
 
 
236 *
237 * Add a fence to a shared slot, obj->lock must be held, and
238 * dma_resv_reserve_shared() has been called.
239 */
240void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
 
241{
242	struct dma_resv_list *fobj;
243	struct dma_fence *old;
244	unsigned int i, count;
245
246	dma_fence_get(fence);
247
248	dma_resv_assert_held(obj);
249
250	fobj = dma_resv_shared_list(obj);
251	count = fobj->shared_count;
 
 
252
253	write_seqcount_begin(&obj->seq);
 
254
255	for (i = 0; i < count; ++i) {
 
256
257		old = rcu_dereference_protected(fobj->shared[i],
258						dma_resv_held(obj));
259		if (old->context == fence->context ||
260		    dma_fence_is_signaled(old))
261			goto replace;
 
 
 
262	}
263
264	BUG_ON(fobj->shared_count >= fobj->shared_max);
265	old = NULL;
266	count++;
267
268replace:
269	RCU_INIT_POINTER(fobj->shared[i], fence);
270	/* pointer update must be visible before we extend the shared_count */
271	smp_store_mb(fobj->shared_count, count);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
273	write_seqcount_end(&obj->seq);
274	dma_fence_put(old);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275}
276EXPORT_SYMBOL(dma_resv_add_shared_fence);
277
278/**
279 * dma_resv_add_excl_fence - Add an exclusive fence.
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 */
285void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
286{
287	struct dma_fence *old_fence = dma_resv_excl_fence(obj);
288	struct dma_resv_list *old;
289	u32 i = 0;
 
 
290
291	dma_resv_assert_held(obj);
 
 
 
292
293	old = dma_resv_shared_list(obj);
294	if (old)
295		i = old->shared_count;
296
297	if (fence)
298		dma_fence_get(fence);
299
300	write_seqcount_begin(&obj->seq);
301	/* write_seqcount_begin provides the necessary memory barrier */
302	RCU_INIT_POINTER(obj->fence_excl, fence);
303	if (old)
304		old->shared_count = 0;
305	write_seqcount_end(&obj->seq);
306
307	/* inplace update, no shared fences */
308	while (i--)
309		dma_fence_put(rcu_dereference_protected(old->shared[i],
310						dma_resv_held(obj)));
311
312	dma_fence_put(old_fence);
313}
314EXPORT_SYMBOL(dma_resv_add_excl_fence);
315
316/**
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 */
323int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
324{
325	struct dma_resv_list *src_list, *dst_list;
326	struct dma_fence *old, *new;
327	unsigned int i;
328
329	dma_resv_assert_held(dst);
330
331	rcu_read_lock();
332	src_list = dma_resv_shared_list(src);
333
334retry:
335	if (src_list) {
336		unsigned int shared_count = src_list->shared_count;
337
338		rcu_read_unlock();
339
340		dst_list = dma_resv_list_alloc(shared_count);
341		if (!dst_list)
342			return -ENOMEM;
343
344		rcu_read_lock();
345		src_list = dma_resv_shared_list(src);
346		if (!src_list || src_list->shared_count > shared_count) {
347			kfree(dst_list);
348			goto retry;
349		}
350
351		dst_list->shared_count = 0;
352		for (i = 0; i < src_list->shared_count; ++i) {
353			struct dma_fence __rcu **dst;
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)) {
362				dma_resv_list_free(dst_list);
363				src_list = dma_resv_shared_list(src);
364				goto retry;
365			}
366
367			if (dma_fence_is_signaled(fence)) {
368				dma_fence_put(fence);
369				continue;
 
370			}
 
 
371
372			dst = &dst_list->shared[dst_list->shared_count++];
373			rcu_assign_pointer(*dst, fence);
374		}
375	} else {
376		dst_list = NULL;
377	}
 
378
379	new = dma_fence_get_rcu_safe(&src->fence_excl);
380	rcu_read_unlock();
381
382	src_list = dma_resv_shared_list(dst);
383	old = dma_resv_excl_fence(dst);
384
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);
390
391	dma_resv_list_free(src_list);
392	dma_fence_put(old);
393
394	return 0;
395}
396EXPORT_SYMBOL(dma_resv_copy_fences);
397
398/**
399 * dma_resv_get_fences - Get an object's shared and exclusive
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 *
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.
410 */
411int dma_resv_get_fences(struct dma_resv *obj, struct dma_fence **pfence_excl,
412			unsigned int *pshared_count,
413			struct dma_fence ***pshared)
414{
415	struct dma_fence **shared = NULL;
416	struct dma_fence *fence_excl;
417	unsigned int shared_count;
418	int ret = 1;
419
420	do {
421		struct dma_resv_list *fobj;
422		unsigned int i, seq;
423		size_t sz = 0;
424
425		shared_count = i = 0;
 
426
427		rcu_read_lock();
428		seq = read_seqcount_begin(&obj->seq);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429
430		fence_excl = dma_resv_excl_fence(obj);
431		if (fence_excl && !dma_fence_get_rcu(fence_excl))
432			goto unlock;
433
434		fobj = dma_resv_shared_list(obj);
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) {
442			struct dma_fence **nshared;
 
443
444			nshared = krealloc(shared, sz,
445					   GFP_NOWAIT | __GFP_NOWARN);
446			if (!nshared) {
447				rcu_read_unlock();
448
449				dma_fence_put(fence_excl);
450				fence_excl = NULL;
 
 
 
451
452				nshared = krealloc(shared, sz, GFP_KERNEL);
453				if (nshared) {
454					shared = nshared;
455					continue;
456				}
457
458				ret = -ENOMEM;
459				break;
460			}
461			shared = nshared;
462			shared_count = fobj ? fobj->shared_count : 0;
463			for (i = 0; i < shared_count; ++i) {
464				shared[i] = rcu_dereference(fobj->shared[i]);
465				if (!dma_fence_get_rcu(shared[i]))
466					break;
467			}
468		}
469
470		if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
471			while (i--)
472				dma_fence_put(shared[i]);
473			dma_fence_put(fence_excl);
474			goto unlock;
475		}
476
477		ret = 0;
478unlock:
479		rcu_read_unlock();
480	} while (ret);
481
482	if (pfence_excl)
483		*pfence_excl = fence_excl;
484	else if (fence_excl)
485		shared[shared_count++] = fence_excl;
486
487	if (!shared_count) {
488		kfree(shared);
489		shared = NULL;
490	}
491
492	*pshared_count = shared_count;
493	*pshared = shared;
494	return ret;
495}
496EXPORT_SYMBOL_GPL(dma_resv_get_fences);
497
498/**
499 * dma_resv_wait_timeout - Wait on reservation's objects
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 *
506 * Callers are not required to hold specific locks, but maybe hold
507 * dma_resv_lock() already
508 * RETURNS
509 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
510 * greater than zer on success.
511 */
512long dma_resv_wait_timeout(struct dma_resv *obj, bool wait_all, bool intr,
513			   unsigned long timeout)
514{
515	long ret = timeout ? timeout : 1;
516	unsigned int seq, shared_count;
517	struct dma_fence *fence;
518	int i;
519
520retry:
521	shared_count = 0;
522	seq = read_seqcount_begin(&obj->seq);
523	rcu_read_lock();
524	i = -1;
525
526	fence = dma_resv_excl_fence(obj);
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
540	if (wait_all) {
541		struct dma_resv_list *fobj = dma_resv_shared_list(obj);
542
543		if (fobj)
544			shared_count = fobj->shared_count;
545
546		for (i = 0; !fence && i < shared_count; ++i) {
547			struct dma_fence *lfence;
548
549			lfence = rcu_dereference(fobj->shared[i]);
550			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
551				     &lfence->flags))
552				continue;
553
554			if (!dma_fence_get_rcu(lfence))
555				goto unlock_retry;
556
557			if (dma_fence_is_signaled(lfence)) {
558				dma_fence_put(lfence);
559				continue;
560			}
561
562			fence = lfence;
563			break;
564		}
565	}
 
566
567	rcu_read_unlock();
568	if (fence) {
569		if (read_seqcount_retry(&obj->seq, seq)) {
570			dma_fence_put(fence);
571			goto retry;
572		}
573
574		ret = dma_fence_wait_timeout(fence, intr, ret);
575		dma_fence_put(fence);
576		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}
585EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
586
587
588static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
 
 
 
 
 
 
 
 
 
589{
590	struct dma_fence *fence, *lfence = passed_fence;
591	int ret = 1;
592
593	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
594		fence = dma_fence_get_rcu(lfence);
595		if (!fence)
596			return -1;
597
598		ret = !!dma_fence_is_signaled(fence);
599		dma_fence_put(fence);
600	}
601	return ret;
602}
 
603
604/**
605 * dma_resv_test_signaled - Test if a reservation object's fences have been
606 * signaled.
607 * @obj: the reservation object
608 * @test_all: if true, test all fences, otherwise only test the exclusive
609 * fence
610 *
611 * Callers are not required to hold specific locks, but maybe hold
612 * dma_resv_lock() already
 
613 * RETURNS
614 * true if all fences signaled, else false
 
615 */
616bool dma_resv_test_signaled(struct dma_resv *obj, bool test_all)
617{
 
618	struct dma_fence *fence;
619	unsigned int seq;
620	int ret;
621
622	rcu_read_lock();
623retry:
624	ret = true;
625	seq = read_seqcount_begin(&obj->seq);
626
627	if (test_all) {
628		struct dma_resv_list *fobj = dma_resv_shared_list(obj);
629		unsigned int i, shared_count;
630
631		shared_count = fobj ? fobj->shared_count : 0;
632		for (i = 0; i < shared_count; ++i) {
633			fence = rcu_dereference(fobj->shared[i]);
634			ret = dma_resv_test_signaled_single(fence);
635			if (ret < 0)
636				goto retry;
637			else if (!ret)
638				break;
639		}
640	}
 
 
 
 
641
642	fence = dma_resv_excl_fence(obj);
643	if (ret && fence) {
644		ret = dma_resv_test_signaled_single(fence);
645		if (ret < 0)
646			goto retry;
 
 
 
 
 
 
 
 
647
 
 
 
 
648	}
649
650	if (read_seqcount_retry(&obj->seq, seq))
651		goto retry;
652
653	rcu_read_unlock();
654	return ret;
655}
656EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
657
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