1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
5 * Based on bo.c which bears the following copyright notice,
6 * but is dual licensed:
8 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
19 * The above copyright notice and this permission notice (including the
20 * next paragraph) shall be included in all copies or substantial portions
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.
31 **************************************************************************/
33 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
36 #include <linux/dma-resv.h>
37 #include <linux/dma-fence-array.h>
38 #include <linux/export.h>
40 #include <linux/sched/mm.h>
41 #include <linux/mmu_notifier.h>
42 #include <linux/seq_file.h>
45 * DOC: Reservation Object Overview
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.
54 * See struct dma_resv for more details.
57 DEFINE_WD_CLASS(reservation_ww_class);
58 EXPORT_SYMBOL(reservation_ww_class);
60 /* Mask for the lower fence pointer bits */
61 #define DMA_RESV_LIST_MASK 0x3
63 struct dma_resv_list {
65 u32 num_fences, max_fences;
66 struct dma_fence __rcu *table[];
69 /* Extract the fence and usage flags from an RCU protected entry in the list. */
70 static 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)
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);
80 *usage = tmp & DMA_RESV_LIST_MASK;
83 /* Set the fence and usage flags at the specific index in the list. */
84 static void dma_resv_list_set(struct dma_resv_list *list,
86 struct dma_fence *fence,
87 enum dma_resv_usage usage)
89 long tmp = ((long)fence) | usage;
91 RCU_INIT_POINTER(list->table[index], (struct dma_fence *)tmp);
95 * Allocate a new dma_resv_list and make sure to correctly initialize
98 static struct dma_resv_list *dma_resv_list_alloc(unsigned int max_fences)
100 struct dma_resv_list *list;
103 /* Round up to the next kmalloc bucket size. */
104 size = kmalloc_size_roundup(struct_size(list, table, max_fences));
106 list = kmalloc(size, GFP_KERNEL);
110 /* Given the resulting bucket size, recalculated max_fences. */
111 list->max_fences = (size - offsetof(typeof(*list), table)) /
112 sizeof(*list->table);
117 /* Free a dma_resv_list and make sure to drop all references. */
118 static void dma_resv_list_free(struct dma_resv_list *list)
125 for (i = 0; i < list->num_fences; ++i) {
126 struct dma_fence *fence;
128 dma_resv_list_entry(list, i, NULL, &fence, NULL);
129 dma_fence_put(fence);
131 kfree_rcu(list, rcu);
135 * dma_resv_init - initialize a reservation object
136 * @obj: the reservation object
138 void dma_resv_init(struct dma_resv *obj)
140 ww_mutex_init(&obj->lock, &reservation_ww_class);
142 RCU_INIT_POINTER(obj->fences, NULL);
144 EXPORT_SYMBOL(dma_resv_init);
147 * dma_resv_fini - destroys a reservation object
148 * @obj: the reservation object
150 void dma_resv_fini(struct dma_resv *obj)
153 * This object should be dead and all references must have
154 * been released to it, so no need to be protected with rcu.
156 dma_resv_list_free(rcu_dereference_protected(obj->fences, true));
157 ww_mutex_destroy(&obj->lock);
159 EXPORT_SYMBOL(dma_resv_fini);
161 /* Dereference the fences while ensuring RCU rules */
162 static inline struct dma_resv_list *dma_resv_fences_list(struct dma_resv *obj)
164 return rcu_dereference_check(obj->fences, dma_resv_held(obj));
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
172 * Should be called before dma_resv_add_fence(). Must be called with @obj
173 * locked through dma_resv_lock().
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.
180 * Zero for success, or -errno
182 int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences)
184 struct dma_resv_list *old, *new;
185 unsigned int i, j, k, max;
187 dma_resv_assert_held(obj);
189 old = dma_resv_fences_list(obj);
190 if (old && old->max_fences) {
191 if ((old->num_fences + num_fences) <= old->max_fences)
193 max = max(old->num_fences + num_fences, old->max_fences * 2);
195 max = max(4ul, roundup_pow_of_two(num_fences));
198 new = dma_resv_list_alloc(max);
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
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;
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);
216 dma_resv_list_set(new, j++, fence, usage);
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.
228 rcu_assign_pointer(obj->fences, new);
233 /* Drop the references to the signaled fences */
234 for (i = k; i < max; ++i) {
235 struct dma_fence *fence;
237 fence = rcu_dereference_protected(new->table[i],
239 dma_fence_put(fence);
245 EXPORT_SYMBOL(dma_resv_reserve_fences);
247 #ifdef CONFIG_DEBUG_MUTEXES
249 * dma_resv_reset_max_fences - reset fences for debugging
250 * @obj: the dma_resv object to reset
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.
256 void dma_resv_reset_max_fences(struct dma_resv *obj)
258 struct dma_resv_list *fences = dma_resv_fences_list(obj);
260 dma_resv_assert_held(obj);
262 /* Test fence slot reservation */
264 fences->max_fences = fences->num_fences;
266 EXPORT_SYMBOL(dma_resv_reset_max_fences);
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
275 * Add a fence to a slot, @obj must be locked with dma_resv_lock(), and
276 * dma_resv_reserve_fences() has been called.
278 * See also &dma_resv.fence for a discussion of the semantics.
280 void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
281 enum dma_resv_usage usage)
283 struct dma_resv_list *fobj;
284 struct dma_fence *old;
285 unsigned int i, count;
287 dma_fence_get(fence);
289 dma_resv_assert_held(obj);
291 /* Drivers should not add containers here, instead add each fence
294 WARN_ON(dma_fence_is_container(fence));
296 fobj = dma_resv_fences_list(obj);
297 count = fobj->num_fences;
299 for (i = 0; i < count; ++i) {
300 enum dma_resv_usage old_usage;
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(fence, old)) ||
305 dma_fence_is_signaled(old)) {
306 dma_resv_list_set(fobj, i, fence, usage);
312 BUG_ON(fobj->num_fences >= fobj->max_fences);
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);
319 EXPORT_SYMBOL(dma_resv_add_fence);
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
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.
332 * And example for using this is replacing a preemption fence with a page table
333 * update fence which makes the resource inaccessible.
335 void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
336 struct dma_fence *replacement,
337 enum dma_resv_usage usage)
339 struct dma_resv_list *list;
342 dma_resv_assert_held(obj);
344 list = dma_resv_fences_list(obj);
345 for (i = 0; list && i < list->num_fences; ++i) {
346 struct dma_fence *old;
348 dma_resv_list_entry(list, i, obj, &old, NULL);
349 if (old->context != context)
352 dma_resv_list_set(list, i, dma_fence_get(replacement), usage);
356 EXPORT_SYMBOL(dma_resv_replace_fences);
358 /* Restart the unlocked iteration by initializing the cursor object. */
359 static void dma_resv_iter_restart_unlocked(struct dma_resv_iter *cursor)
362 cursor->num_fences = 0;
363 cursor->fences = dma_resv_fences_list(cursor->obj);
365 cursor->num_fences = cursor->fences->num_fences;
366 cursor->is_restarted = true;
369 /* Walk to the next not signaled fence and grab a reference to it */
370 static void dma_resv_iter_walk_unlocked(struct dma_resv_iter *cursor)
376 /* Drop the reference from the previous round */
377 dma_fence_put(cursor->fence);
379 if (cursor->index >= cursor->num_fences) {
380 cursor->fence = NULL;
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);
394 if (!dma_fence_is_signaled(cursor->fence) &&
395 cursor->usage >= cursor->fence_usage)
401 * dma_resv_iter_first_unlocked - first fence in an unlocked dma_resv obj.
402 * @cursor: the cursor with the current position
404 * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
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.
410 * Returns the first fence from an unlocked dma_resv obj.
412 struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor)
416 dma_resv_iter_restart_unlocked(cursor);
417 dma_resv_iter_walk_unlocked(cursor);
418 } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
421 return cursor->fence;
423 EXPORT_SYMBOL(dma_resv_iter_first_unlocked);
426 * dma_resv_iter_next_unlocked - next fence in an unlocked dma_resv obj.
427 * @cursor: the cursor with the current position
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.
433 * Returns the next fence from an unlocked dma_resv obj.
435 struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor)
440 cursor->is_restarted = false;
441 restart = dma_resv_fences_list(cursor->obj) != cursor->fences;
444 dma_resv_iter_restart_unlocked(cursor);
445 dma_resv_iter_walk_unlocked(cursor);
447 } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
450 return cursor->fence;
452 EXPORT_SYMBOL(dma_resv_iter_next_unlocked);
455 * dma_resv_iter_first - first fence from a locked dma_resv object
456 * @cursor: cursor to record the current position
458 * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
460 * Return the first fence in the dma_resv object while holding the
463 struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor)
465 struct dma_fence *fence;
467 dma_resv_assert_held(cursor->obj);
470 cursor->fences = dma_resv_fences_list(cursor->obj);
472 fence = dma_resv_iter_next(cursor);
473 cursor->is_restarted = true;
476 EXPORT_SYMBOL_GPL(dma_resv_iter_first);
479 * dma_resv_iter_next - next fence from a locked dma_resv object
480 * @cursor: cursor to record the current position
482 * Return the next fences from the dma_resv object while holding the
485 struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor)
487 struct dma_fence *fence;
489 dma_resv_assert_held(cursor->obj);
491 cursor->is_restarted = false;
494 if (!cursor->fences ||
495 cursor->index >= cursor->fences->num_fences)
498 dma_resv_list_entry(cursor->fences, cursor->index++,
499 cursor->obj, &fence, &cursor->fence_usage);
500 } while (cursor->fence_usage > cursor->usage);
504 EXPORT_SYMBOL_GPL(dma_resv_iter_next);
507 * dma_resv_copy_fences - Copy all fences from src to dst.
508 * @dst: the destination reservation object
509 * @src: the source reservation object
511 * Copy all fences from src to dst. dst-lock must be held.
513 int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
515 struct dma_resv_iter cursor;
516 struct dma_resv_list *list;
519 dma_resv_assert_held(dst);
523 dma_resv_iter_begin(&cursor, src, DMA_RESV_USAGE_BOOKKEEP);
524 dma_resv_for_each_fence_unlocked(&cursor, f) {
526 if (dma_resv_iter_is_restarted(&cursor)) {
527 dma_resv_list_free(list);
529 list = dma_resv_list_alloc(cursor.num_fences);
531 dma_resv_iter_end(&cursor);
534 list->num_fences = 0;
538 dma_resv_list_set(list, list->num_fences++, f,
539 dma_resv_iter_usage(&cursor));
541 dma_resv_iter_end(&cursor);
543 list = rcu_replace_pointer(dst->fences, list, dma_resv_held(dst));
544 dma_resv_list_free(list);
547 EXPORT_SYMBOL(dma_resv_copy_fences);
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)
558 * Retrieve all fences from the reservation object.
559 * Returns either zero or -ENOMEM.
561 int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
562 unsigned int *num_fences, struct dma_fence ***fences)
564 struct dma_resv_iter cursor;
565 struct dma_fence *fence;
570 dma_resv_iter_begin(&cursor, obj, usage);
571 dma_resv_for_each_fence_unlocked(&cursor, fence) {
573 if (dma_resv_iter_is_restarted(&cursor)) {
574 struct dma_fence **new_fences;
578 dma_fence_put((*fences)[--(*num_fences)]);
580 count = cursor.num_fences + 1;
582 /* Eventually re-allocate the array */
583 new_fences = krealloc_array(*fences, count,
586 if (count && !new_fences) {
590 dma_resv_iter_end(&cursor);
593 *fences = new_fences;
596 (*fences)[(*num_fences)++] = dma_fence_get(fence);
598 dma_resv_iter_end(&cursor);
602 EXPORT_SYMBOL_GPL(dma_resv_get_fences);
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
610 * Get a single fence representing all the fences inside the resv object.
611 * Returns either 0 for success or -ENOMEM.
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
617 * Returns 0 on success and negative error values on failure.
619 int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
620 struct dma_fence **fence)
622 struct dma_fence_array *array;
623 struct dma_fence **fences;
627 r = dma_resv_get_fences(obj, usage, &count, &fences);
642 array = dma_fence_array_create(count, fences,
643 dma_fence_context_alloc(1),
647 dma_fence_put(fences[count]);
652 *fence = &array->base;
655 EXPORT_SYMBOL_GPL(dma_resv_get_singleton);
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
664 * Callers are not required to hold specific locks, but maybe hold
665 * dma_resv_lock() already
667 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
668 * greater than zero on success.
670 long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
671 bool intr, unsigned long timeout)
673 long ret = timeout ? timeout : 1;
674 struct dma_resv_iter cursor;
675 struct dma_fence *fence;
677 dma_resv_iter_begin(&cursor, obj, usage);
678 dma_resv_for_each_fence_unlocked(&cursor, fence) {
680 ret = dma_fence_wait_timeout(fence, intr, ret);
682 dma_resv_iter_end(&cursor);
686 dma_resv_iter_end(&cursor);
690 EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
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)
698 * May be called without holding the dma_resv lock. Sets @deadline on
699 * all fences filtered by @usage.
701 void dma_resv_set_deadline(struct dma_resv *obj, enum dma_resv_usage usage,
704 struct dma_resv_iter cursor;
705 struct dma_fence *fence;
707 dma_resv_iter_begin(&cursor, obj, usage);
708 dma_resv_for_each_fence_unlocked(&cursor, fence) {
709 dma_fence_set_deadline(fence, deadline);
711 dma_resv_iter_end(&cursor);
713 EXPORT_SYMBOL_GPL(dma_resv_set_deadline);
716 * dma_resv_test_signaled - Test if a reservation object's fences have been
718 * @obj: the reservation object
719 * @usage: controls which fences to include, see enum dma_resv_usage.
721 * Callers are not required to hold specific locks, but maybe hold
722 * dma_resv_lock() already.
726 * True if all fences signaled, else false.
728 bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage)
730 struct dma_resv_iter cursor;
731 struct dma_fence *fence;
733 dma_resv_iter_begin(&cursor, obj, usage);
734 dma_resv_for_each_fence_unlocked(&cursor, fence) {
735 dma_resv_iter_end(&cursor);
738 dma_resv_iter_end(&cursor);
741 EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
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
748 * Dump a textual description of the fences inside an dma_resv object into the
751 void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq)
753 static const char *usage[] = { "kernel", "write", "read", "bookkeep" };
754 struct dma_resv_iter cursor;
755 struct dma_fence *fence;
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);
763 EXPORT_SYMBOL_GPL(dma_resv_describe);
765 #if IS_ENABLED(CONFIG_LOCKDEP)
766 static int __init dma_resv_lockdep(void)
768 struct mm_struct *mm = mm_alloc();
769 struct ww_acquire_ctx ctx;
771 struct address_space mapping;
778 address_space_init_once(&mapping);
781 ww_acquire_init(&ctx, &reservation_ww_class);
782 ret = dma_resv_lock(&obj, &ctx);
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);
794 __dma_fence_might_wait();
796 fs_reclaim_release(GFP_KERNEL);
797 ww_mutex_unlock(&obj.lock);
798 ww_acquire_fini(&ctx);
799 mmap_read_unlock(mm);
805 subsys_initcall(dma_resv_lockdep);