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;
102 list = kmalloc(struct_size(list, table, max_fences), GFP_KERNEL);
106 list->max_fences = (ksize(list) - offsetof(typeof(*list), table)) /
107 sizeof(*list->table);
112 /* Free a dma_resv_list and make sure to drop all references. */
113 static void dma_resv_list_free(struct dma_resv_list *list)
120 for (i = 0; i < list->num_fences; ++i) {
121 struct dma_fence *fence;
123 dma_resv_list_entry(list, i, NULL, &fence, NULL);
124 dma_fence_put(fence);
126 kfree_rcu(list, rcu);
130 * dma_resv_init - initialize a reservation object
131 * @obj: the reservation object
133 void dma_resv_init(struct dma_resv *obj)
135 ww_mutex_init(&obj->lock, &reservation_ww_class);
137 RCU_INIT_POINTER(obj->fences, NULL);
139 EXPORT_SYMBOL(dma_resv_init);
142 * dma_resv_fini - destroys a reservation object
143 * @obj: the reservation object
145 void dma_resv_fini(struct dma_resv *obj)
148 * This object should be dead and all references must have
149 * been released to it, so no need to be protected with rcu.
151 dma_resv_list_free(rcu_dereference_protected(obj->fences, true));
152 ww_mutex_destroy(&obj->lock);
154 EXPORT_SYMBOL(dma_resv_fini);
156 /* Dereference the fences while ensuring RCU rules */
157 static inline struct dma_resv_list *dma_resv_fences_list(struct dma_resv *obj)
159 return rcu_dereference_check(obj->fences, dma_resv_held(obj));
163 * dma_resv_reserve_fences - Reserve space to add fences to a dma_resv object.
164 * @obj: reservation object
165 * @num_fences: number of fences we want to add
167 * Should be called before dma_resv_add_fence(). Must be called with @obj
168 * locked through dma_resv_lock().
170 * Note that the preallocated slots need to be re-reserved if @obj is unlocked
171 * at any time before calling dma_resv_add_fence(). This is validated when
172 * CONFIG_DEBUG_MUTEXES is enabled.
175 * Zero for success, or -errno
177 int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences)
179 struct dma_resv_list *old, *new;
180 unsigned int i, j, k, max;
182 dma_resv_assert_held(obj);
184 old = dma_resv_fences_list(obj);
185 if (old && old->max_fences) {
186 if ((old->num_fences + num_fences) <= old->max_fences)
188 max = max(old->num_fences + num_fences, old->max_fences * 2);
190 max = max(4ul, roundup_pow_of_two(num_fences));
193 new = dma_resv_list_alloc(max);
198 * no need to bump fence refcounts, rcu_read access
199 * requires the use of kref_get_unless_zero, and the
200 * references from the old struct are carried over to
203 for (i = 0, j = 0, k = max; i < (old ? old->num_fences : 0); ++i) {
204 enum dma_resv_usage usage;
205 struct dma_fence *fence;
207 dma_resv_list_entry(old, i, obj, &fence, &usage);
208 if (dma_fence_is_signaled(fence))
209 RCU_INIT_POINTER(new->table[--k], fence);
211 dma_resv_list_set(new, j++, fence, usage);
216 * We are not changing the effective set of fences here so can
217 * merely update the pointer to the new array; both existing
218 * readers and new readers will see exactly the same set of
219 * active (unsignaled) fences. Individual fences and the
220 * old array are protected by RCU and so will not vanish under
221 * the gaze of the rcu_read_lock() readers.
223 rcu_assign_pointer(obj->fences, new);
228 /* Drop the references to the signaled fences */
229 for (i = k; i < max; ++i) {
230 struct dma_fence *fence;
232 fence = rcu_dereference_protected(new->table[i],
234 dma_fence_put(fence);
240 EXPORT_SYMBOL(dma_resv_reserve_fences);
242 #ifdef CONFIG_DEBUG_MUTEXES
244 * dma_resv_reset_max_fences - reset fences for debugging
245 * @obj: the dma_resv object to reset
247 * Reset the number of pre-reserved fence slots to test that drivers do
248 * correct slot allocation using dma_resv_reserve_fences(). See also
249 * &dma_resv_list.max_fences.
251 void dma_resv_reset_max_fences(struct dma_resv *obj)
253 struct dma_resv_list *fences = dma_resv_fences_list(obj);
255 dma_resv_assert_held(obj);
257 /* Test fence slot reservation */
259 fences->max_fences = fences->num_fences;
261 EXPORT_SYMBOL(dma_resv_reset_max_fences);
265 * dma_resv_add_fence - Add a fence to the dma_resv obj
266 * @obj: the reservation object
267 * @fence: the fence to add
268 * @usage: how the fence is used, see enum dma_resv_usage
270 * Add a fence to a slot, @obj must be locked with dma_resv_lock(), and
271 * dma_resv_reserve_fences() has been called.
273 * See also &dma_resv.fence for a discussion of the semantics.
275 void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
276 enum dma_resv_usage usage)
278 struct dma_resv_list *fobj;
279 struct dma_fence *old;
280 unsigned int i, count;
282 dma_fence_get(fence);
284 dma_resv_assert_held(obj);
286 /* Drivers should not add containers here, instead add each fence
289 WARN_ON(dma_fence_is_container(fence));
291 fobj = dma_resv_fences_list(obj);
292 count = fobj->num_fences;
294 for (i = 0; i < count; ++i) {
295 enum dma_resv_usage old_usage;
297 dma_resv_list_entry(fobj, i, obj, &old, &old_usage);
298 if ((old->context == fence->context && old_usage >= usage) ||
299 dma_fence_is_signaled(old)) {
300 dma_resv_list_set(fobj, i, fence, usage);
306 BUG_ON(fobj->num_fences >= fobj->max_fences);
309 dma_resv_list_set(fobj, i, fence, usage);
310 /* pointer update must be visible before we extend the num_fences */
311 smp_store_mb(fobj->num_fences, count);
313 EXPORT_SYMBOL(dma_resv_add_fence);
316 * dma_resv_replace_fences - replace fences in the dma_resv obj
317 * @obj: the reservation object
318 * @context: the context of the fences to replace
319 * @replacement: the new fence to use instead
320 * @usage: how the new fence is used, see enum dma_resv_usage
322 * Replace fences with a specified context with a new fence. Only valid if the
323 * operation represented by the original fence has no longer access to the
324 * resources represented by the dma_resv object when the new fence completes.
326 * And example for using this is replacing a preemption fence with a page table
327 * update fence which makes the resource inaccessible.
329 void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
330 struct dma_fence *replacement,
331 enum dma_resv_usage usage)
333 struct dma_resv_list *list;
336 dma_resv_assert_held(obj);
338 list = dma_resv_fences_list(obj);
339 for (i = 0; list && i < list->num_fences; ++i) {
340 struct dma_fence *old;
342 dma_resv_list_entry(list, i, obj, &old, NULL);
343 if (old->context != context)
346 dma_resv_list_set(list, i, replacement, usage);
350 EXPORT_SYMBOL(dma_resv_replace_fences);
352 /* Restart the unlocked iteration by initializing the cursor object. */
353 static void dma_resv_iter_restart_unlocked(struct dma_resv_iter *cursor)
356 cursor->num_fences = 0;
357 cursor->fences = dma_resv_fences_list(cursor->obj);
359 cursor->num_fences = cursor->fences->num_fences;
360 cursor->is_restarted = true;
363 /* Walk to the next not signaled fence and grab a reference to it */
364 static void dma_resv_iter_walk_unlocked(struct dma_resv_iter *cursor)
370 /* Drop the reference from the previous round */
371 dma_fence_put(cursor->fence);
373 if (cursor->index >= cursor->num_fences) {
374 cursor->fence = NULL;
379 dma_resv_list_entry(cursor->fences, cursor->index++,
380 cursor->obj, &cursor->fence,
381 &cursor->fence_usage);
382 cursor->fence = dma_fence_get_rcu(cursor->fence);
383 if (!cursor->fence) {
384 dma_resv_iter_restart_unlocked(cursor);
388 if (!dma_fence_is_signaled(cursor->fence) &&
389 cursor->usage >= cursor->fence_usage)
395 * dma_resv_iter_first_unlocked - first fence in an unlocked dma_resv obj.
396 * @cursor: the cursor with the current position
398 * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
400 * Beware that the iterator can be restarted. Code which accumulates statistics
401 * or similar needs to check for this with dma_resv_iter_is_restarted(). For
402 * this reason prefer the locked dma_resv_iter_first() whenver possible.
404 * Returns the first fence from an unlocked dma_resv obj.
406 struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor)
410 dma_resv_iter_restart_unlocked(cursor);
411 dma_resv_iter_walk_unlocked(cursor);
412 } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
415 return cursor->fence;
417 EXPORT_SYMBOL(dma_resv_iter_first_unlocked);
420 * dma_resv_iter_next_unlocked - next fence in an unlocked dma_resv obj.
421 * @cursor: the cursor with the current position
423 * Beware that the iterator can be restarted. Code which accumulates statistics
424 * or similar needs to check for this with dma_resv_iter_is_restarted(). For
425 * this reason prefer the locked dma_resv_iter_next() whenver possible.
427 * Returns the next fence from an unlocked dma_resv obj.
429 struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor)
434 cursor->is_restarted = false;
435 restart = dma_resv_fences_list(cursor->obj) != cursor->fences;
438 dma_resv_iter_restart_unlocked(cursor);
439 dma_resv_iter_walk_unlocked(cursor);
441 } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
444 return cursor->fence;
446 EXPORT_SYMBOL(dma_resv_iter_next_unlocked);
449 * dma_resv_iter_first - first fence from a locked dma_resv object
450 * @cursor: cursor to record the current position
452 * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
454 * Return the first fence in the dma_resv object while holding the
457 struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor)
459 struct dma_fence *fence;
461 dma_resv_assert_held(cursor->obj);
464 cursor->fences = dma_resv_fences_list(cursor->obj);
466 fence = dma_resv_iter_next(cursor);
467 cursor->is_restarted = true;
470 EXPORT_SYMBOL_GPL(dma_resv_iter_first);
473 * dma_resv_iter_next - next fence from a locked dma_resv object
474 * @cursor: cursor to record the current position
476 * Return the next fences from the dma_resv object while holding the
479 struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor)
481 struct dma_fence *fence;
483 dma_resv_assert_held(cursor->obj);
485 cursor->is_restarted = false;
488 if (!cursor->fences ||
489 cursor->index >= cursor->fences->num_fences)
492 dma_resv_list_entry(cursor->fences, cursor->index++,
493 cursor->obj, &fence, &cursor->fence_usage);
494 } while (cursor->fence_usage > cursor->usage);
498 EXPORT_SYMBOL_GPL(dma_resv_iter_next);
501 * dma_resv_copy_fences - Copy all fences from src to dst.
502 * @dst: the destination reservation object
503 * @src: the source reservation object
505 * Copy all fences from src to dst. dst-lock must be held.
507 int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
509 struct dma_resv_iter cursor;
510 struct dma_resv_list *list;
513 dma_resv_assert_held(dst);
517 dma_resv_iter_begin(&cursor, src, DMA_RESV_USAGE_BOOKKEEP);
518 dma_resv_for_each_fence_unlocked(&cursor, f) {
520 if (dma_resv_iter_is_restarted(&cursor)) {
521 dma_resv_list_free(list);
523 list = dma_resv_list_alloc(cursor.num_fences);
525 dma_resv_iter_end(&cursor);
528 list->num_fences = 0;
532 dma_resv_list_set(list, list->num_fences++, f,
533 dma_resv_iter_usage(&cursor));
535 dma_resv_iter_end(&cursor);
537 list = rcu_replace_pointer(dst->fences, list, dma_resv_held(dst));
538 dma_resv_list_free(list);
541 EXPORT_SYMBOL(dma_resv_copy_fences);
544 * dma_resv_get_fences - Get an object's fences
545 * fences without update side lock held
546 * @obj: the reservation object
547 * @usage: controls which fences to include, see enum dma_resv_usage.
548 * @num_fences: the number of fences returned
549 * @fences: the array of fence ptrs returned (array is krealloc'd to the
550 * required size, and must be freed by caller)
552 * Retrieve all fences from the reservation object.
553 * Returns either zero or -ENOMEM.
555 int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
556 unsigned int *num_fences, struct dma_fence ***fences)
558 struct dma_resv_iter cursor;
559 struct dma_fence *fence;
564 dma_resv_iter_begin(&cursor, obj, usage);
565 dma_resv_for_each_fence_unlocked(&cursor, fence) {
567 if (dma_resv_iter_is_restarted(&cursor)) {
571 dma_fence_put((*fences)[--(*num_fences)]);
573 count = cursor.num_fences + 1;
575 /* Eventually re-allocate the array */
576 *fences = krealloc_array(*fences, count,
579 if (count && !*fences) {
580 dma_resv_iter_end(&cursor);
585 (*fences)[(*num_fences)++] = dma_fence_get(fence);
587 dma_resv_iter_end(&cursor);
591 EXPORT_SYMBOL_GPL(dma_resv_get_fences);
594 * dma_resv_get_singleton - Get a single fence for all the fences
595 * @obj: the reservation object
596 * @usage: controls which fences to include, see enum dma_resv_usage.
597 * @fence: the resulting fence
599 * Get a single fence representing all the fences inside the resv object.
600 * Returns either 0 for success or -ENOMEM.
602 * Warning: This can't be used like this when adding the fence back to the resv
603 * object since that can lead to stack corruption when finalizing the
606 * Returns 0 on success and negative error values on failure.
608 int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
609 struct dma_fence **fence)
611 struct dma_fence_array *array;
612 struct dma_fence **fences;
616 r = dma_resv_get_fences(obj, usage, &count, &fences);
631 array = dma_fence_array_create(count, fences,
632 dma_fence_context_alloc(1),
636 dma_fence_put(fences[count]);
641 *fence = &array->base;
644 EXPORT_SYMBOL_GPL(dma_resv_get_singleton);
647 * dma_resv_wait_timeout - Wait on reservation's objects fences
648 * @obj: the reservation object
649 * @usage: controls which fences to include, see enum dma_resv_usage.
650 * @intr: if true, do interruptible wait
651 * @timeout: timeout value in jiffies or zero to return immediately
653 * Callers are not required to hold specific locks, but maybe hold
654 * dma_resv_lock() already
656 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
657 * greater than zer on success.
659 long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
660 bool intr, unsigned long timeout)
662 long ret = timeout ? timeout : 1;
663 struct dma_resv_iter cursor;
664 struct dma_fence *fence;
666 dma_resv_iter_begin(&cursor, obj, usage);
667 dma_resv_for_each_fence_unlocked(&cursor, fence) {
669 ret = dma_fence_wait_timeout(fence, intr, ret);
671 dma_resv_iter_end(&cursor);
675 dma_resv_iter_end(&cursor);
679 EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
683 * dma_resv_test_signaled - Test if a reservation object's fences have been
685 * @obj: the reservation object
686 * @usage: controls which fences to include, see enum dma_resv_usage.
688 * Callers are not required to hold specific locks, but maybe hold
689 * dma_resv_lock() already.
693 * True if all fences signaled, else false.
695 bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage)
697 struct dma_resv_iter cursor;
698 struct dma_fence *fence;
700 dma_resv_iter_begin(&cursor, obj, usage);
701 dma_resv_for_each_fence_unlocked(&cursor, fence) {
702 dma_resv_iter_end(&cursor);
705 dma_resv_iter_end(&cursor);
708 EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
711 * dma_resv_describe - Dump description of the resv object into seq_file
712 * @obj: the reservation object
713 * @seq: the seq_file to dump the description into
715 * Dump a textual description of the fences inside an dma_resv object into the
718 void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq)
720 static const char *usage[] = { "kernel", "write", "read", "bookkeep" };
721 struct dma_resv_iter cursor;
722 struct dma_fence *fence;
724 dma_resv_for_each_fence(&cursor, obj, DMA_RESV_USAGE_READ, fence) {
725 seq_printf(seq, "\t%s fence:",
726 usage[dma_resv_iter_usage(&cursor)]);
727 dma_fence_describe(fence, seq);
730 EXPORT_SYMBOL_GPL(dma_resv_describe);
732 #if IS_ENABLED(CONFIG_LOCKDEP)
733 static int __init dma_resv_lockdep(void)
735 struct mm_struct *mm = mm_alloc();
736 struct ww_acquire_ctx ctx;
738 struct address_space mapping;
745 address_space_init_once(&mapping);
748 ww_acquire_init(&ctx, &reservation_ww_class);
749 ret = dma_resv_lock(&obj, &ctx);
751 dma_resv_lock_slow(&obj, &ctx);
752 fs_reclaim_acquire(GFP_KERNEL);
753 /* for unmap_mapping_range on trylocked buffer objects in shrinkers */
754 i_mmap_lock_write(&mapping);
755 i_mmap_unlock_write(&mapping);
756 #ifdef CONFIG_MMU_NOTIFIER
757 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
758 __dma_fence_might_wait();
759 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
761 __dma_fence_might_wait();
763 fs_reclaim_release(GFP_KERNEL);
764 ww_mutex_unlock(&obj.lock);
765 ww_acquire_fini(&ctx);
766 mmap_read_unlock(mm);
772 subsys_initcall(dma_resv_lockdep);