2 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
4 * Based on bo.c which bears the following copyright notice,
5 * but is dual licensed:
7 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sub license, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
18 * The above copyright notice and this permission notice (including the
19 * next paragraph) shall be included in all copies or substantial portions
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
26 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
28 * USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
32 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
35 #include <linux/dma-resv.h>
36 #include <linux/export.h>
38 #include <linux/sched/mm.h>
39 #include <linux/mmu_notifier.h>
42 * DOC: Reservation Object Overview
44 * The reservation object provides a mechanism to manage shared and
45 * exclusive fences associated with a buffer. A reservation object
46 * can have attached one exclusive fence (normally associated with
47 * write operations) or N shared fences (read operations). The RCU
48 * mechanism is used to protect read access to fences from locked
52 DEFINE_WD_CLASS(reservation_ww_class);
53 EXPORT_SYMBOL(reservation_ww_class);
55 struct lock_class_key reservation_seqcount_class;
56 EXPORT_SYMBOL(reservation_seqcount_class);
58 const char reservation_seqcount_string[] = "reservation_seqcount";
59 EXPORT_SYMBOL(reservation_seqcount_string);
62 * dma_resv_list_alloc - allocate fence list
63 * @shared_max: number of fences we need space for
65 * Allocate a new dma_resv_list and make sure to correctly initialize
68 static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
70 struct dma_resv_list *list;
72 list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
76 list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
77 sizeof(*list->shared);
83 * dma_resv_list_free - free fence list
86 * Free a dma_resv_list and make sure to drop all references.
88 static void dma_resv_list_free(struct dma_resv_list *list)
95 for (i = 0; i < list->shared_count; ++i)
96 dma_fence_put(rcu_dereference_protected(list->shared[i], true));
101 #if IS_ENABLED(CONFIG_LOCKDEP)
102 static int __init dma_resv_lockdep(void)
104 struct mm_struct *mm = mm_alloc();
105 struct ww_acquire_ctx ctx;
115 ww_acquire_init(&ctx, &reservation_ww_class);
116 ret = dma_resv_lock(&obj, &ctx);
118 dma_resv_lock_slow(&obj, &ctx);
119 fs_reclaim_acquire(GFP_KERNEL);
120 #ifdef CONFIG_MMU_NOTIFIER
121 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
122 __dma_fence_might_wait();
123 lock_map_release(&__mmu_notifier_invalidate_range_start_map);
125 __dma_fence_might_wait();
127 fs_reclaim_release(GFP_KERNEL);
128 ww_mutex_unlock(&obj.lock);
129 ww_acquire_fini(&ctx);
130 mmap_read_unlock(mm);
136 subsys_initcall(dma_resv_lockdep);
140 * dma_resv_init - initialize a reservation object
141 * @obj: the reservation object
143 void dma_resv_init(struct dma_resv *obj)
145 ww_mutex_init(&obj->lock, &reservation_ww_class);
147 __seqcount_init(&obj->seq, reservation_seqcount_string,
148 &reservation_seqcount_class);
149 RCU_INIT_POINTER(obj->fence, NULL);
150 RCU_INIT_POINTER(obj->fence_excl, NULL);
152 EXPORT_SYMBOL(dma_resv_init);
155 * dma_resv_fini - destroys a reservation object
156 * @obj: the reservation object
158 void dma_resv_fini(struct dma_resv *obj)
160 struct dma_resv_list *fobj;
161 struct dma_fence *excl;
164 * This object should be dead and all references must have
165 * been released to it, so no need to be protected with rcu.
167 excl = rcu_dereference_protected(obj->fence_excl, 1);
171 fobj = rcu_dereference_protected(obj->fence, 1);
172 dma_resv_list_free(fobj);
173 ww_mutex_destroy(&obj->lock);
175 EXPORT_SYMBOL(dma_resv_fini);
178 * dma_resv_reserve_shared - Reserve space to add shared fences to
180 * @obj: reservation object
181 * @num_fences: number of fences we want to add
183 * Should be called before dma_resv_add_shared_fence(). Must
184 * be called with obj->lock held.
187 * Zero for success, or -errno
189 int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
191 struct dma_resv_list *old, *new;
192 unsigned int i, j, k, max;
194 dma_resv_assert_held(obj);
196 old = dma_resv_get_list(obj);
198 if (old && old->shared_max) {
199 if ((old->shared_count + num_fences) <= old->shared_max)
202 max = max(old->shared_count + num_fences,
203 old->shared_max * 2);
208 new = dma_resv_list_alloc(max);
213 * no need to bump fence refcounts, rcu_read access
214 * requires the use of kref_get_unless_zero, and the
215 * references from the old struct are carried over to
218 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
219 struct dma_fence *fence;
221 fence = rcu_dereference_protected(old->shared[i],
223 if (dma_fence_is_signaled(fence))
224 RCU_INIT_POINTER(new->shared[--k], fence);
226 RCU_INIT_POINTER(new->shared[j++], fence);
228 new->shared_count = j;
231 * We are not changing the effective set of fences here so can
232 * merely update the pointer to the new array; both existing
233 * readers and new readers will see exactly the same set of
234 * active (unsignaled) shared fences. Individual fences and the
235 * old array are protected by RCU and so will not vanish under
236 * the gaze of the rcu_read_lock() readers.
238 rcu_assign_pointer(obj->fence, new);
243 /* Drop the references to the signaled fences */
244 for (i = k; i < max; ++i) {
245 struct dma_fence *fence;
247 fence = rcu_dereference_protected(new->shared[i],
249 dma_fence_put(fence);
255 EXPORT_SYMBOL(dma_resv_reserve_shared);
258 * dma_resv_add_shared_fence - Add a fence to a shared slot
259 * @obj: the reservation object
260 * @fence: the shared fence to add
262 * Add a fence to a shared slot, obj->lock must be held, and
263 * dma_resv_reserve_shared() has been called.
265 void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
267 struct dma_resv_list *fobj;
268 struct dma_fence *old;
269 unsigned int i, count;
271 dma_fence_get(fence);
273 dma_resv_assert_held(obj);
275 fobj = dma_resv_get_list(obj);
276 count = fobj->shared_count;
279 write_seqcount_begin(&obj->seq);
281 for (i = 0; i < count; ++i) {
283 old = rcu_dereference_protected(fobj->shared[i],
285 if (old->context == fence->context ||
286 dma_fence_is_signaled(old))
290 BUG_ON(fobj->shared_count >= fobj->shared_max);
295 RCU_INIT_POINTER(fobj->shared[i], fence);
296 /* pointer update must be visible before we extend the shared_count */
297 smp_store_mb(fobj->shared_count, count);
299 write_seqcount_end(&obj->seq);
303 EXPORT_SYMBOL(dma_resv_add_shared_fence);
306 * dma_resv_add_excl_fence - Add an exclusive fence.
307 * @obj: the reservation object
308 * @fence: the shared fence to add
310 * Add a fence to the exclusive slot. The obj->lock must be held.
312 void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
314 struct dma_fence *old_fence = dma_resv_get_excl(obj);
315 struct dma_resv_list *old;
318 dma_resv_assert_held(obj);
320 old = dma_resv_get_list(obj);
322 i = old->shared_count;
325 dma_fence_get(fence);
328 write_seqcount_begin(&obj->seq);
329 /* write_seqcount_begin provides the necessary memory barrier */
330 RCU_INIT_POINTER(obj->fence_excl, fence);
332 old->shared_count = 0;
333 write_seqcount_end(&obj->seq);
336 /* inplace update, no shared fences */
338 dma_fence_put(rcu_dereference_protected(old->shared[i],
339 dma_resv_held(obj)));
341 dma_fence_put(old_fence);
343 EXPORT_SYMBOL(dma_resv_add_excl_fence);
346 * dma_resv_copy_fences - Copy all fences from src to dst.
347 * @dst: the destination reservation object
348 * @src: the source reservation object
350 * Copy all fences from src to dst. dst-lock must be held.
352 int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
354 struct dma_resv_list *src_list, *dst_list;
355 struct dma_fence *old, *new;
358 dma_resv_assert_held(dst);
361 src_list = rcu_dereference(src->fence);
365 unsigned shared_count = src_list->shared_count;
369 dst_list = dma_resv_list_alloc(shared_count);
374 src_list = rcu_dereference(src->fence);
375 if (!src_list || src_list->shared_count > shared_count) {
380 dst_list->shared_count = 0;
381 for (i = 0; i < src_list->shared_count; ++i) {
382 struct dma_fence *fence;
384 fence = rcu_dereference(src_list->shared[i]);
385 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
389 if (!dma_fence_get_rcu(fence)) {
390 dma_resv_list_free(dst_list);
391 src_list = rcu_dereference(src->fence);
395 if (dma_fence_is_signaled(fence)) {
396 dma_fence_put(fence);
400 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
406 new = dma_fence_get_rcu_safe(&src->fence_excl);
409 src_list = dma_resv_get_list(dst);
410 old = dma_resv_get_excl(dst);
413 write_seqcount_begin(&dst->seq);
414 /* write_seqcount_begin provides the necessary memory barrier */
415 RCU_INIT_POINTER(dst->fence_excl, new);
416 RCU_INIT_POINTER(dst->fence, dst_list);
417 write_seqcount_end(&dst->seq);
420 dma_resv_list_free(src_list);
425 EXPORT_SYMBOL(dma_resv_copy_fences);
428 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
429 * fences without update side lock held
430 * @obj: the reservation object
431 * @pfence_excl: the returned exclusive fence (or NULL)
432 * @pshared_count: the number of shared fences returned
433 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
434 * the required size, and must be freed by caller)
436 * Retrieve all fences from the reservation object. If the pointer for the
437 * exclusive fence is not specified the fence is put into the array of the
438 * shared fences as well. Returns either zero or -ENOMEM.
440 int dma_resv_get_fences_rcu(struct dma_resv *obj,
441 struct dma_fence **pfence_excl,
442 unsigned *pshared_count,
443 struct dma_fence ***pshared)
445 struct dma_fence **shared = NULL;
446 struct dma_fence *fence_excl;
447 unsigned int shared_count;
451 struct dma_resv_list *fobj;
455 shared_count = i = 0;
458 seq = read_seqcount_begin(&obj->seq);
460 fence_excl = rcu_dereference(obj->fence_excl);
461 if (fence_excl && !dma_fence_get_rcu(fence_excl))
464 fobj = rcu_dereference(obj->fence);
466 sz += sizeof(*shared) * fobj->shared_max;
468 if (!pfence_excl && fence_excl)
469 sz += sizeof(*shared);
472 struct dma_fence **nshared;
474 nshared = krealloc(shared, sz,
475 GFP_NOWAIT | __GFP_NOWARN);
479 dma_fence_put(fence_excl);
482 nshared = krealloc(shared, sz, GFP_KERNEL);
492 shared_count = fobj ? fobj->shared_count : 0;
493 for (i = 0; i < shared_count; ++i) {
494 shared[i] = rcu_dereference(fobj->shared[i]);
495 if (!dma_fence_get_rcu(shared[i]))
500 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
502 dma_fence_put(shared[i]);
503 dma_fence_put(fence_excl);
513 *pfence_excl = fence_excl;
515 shared[shared_count++] = fence_excl;
522 *pshared_count = shared_count;
526 EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu);
529 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
530 * shared and/or exclusive fences.
531 * @obj: the reservation object
532 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
533 * @intr: if true, do interruptible wait
534 * @timeout: timeout value in jiffies or zero to return immediately
537 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
538 * greater than zer on success.
540 long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
541 bool wait_all, bool intr,
542 unsigned long timeout)
544 struct dma_fence *fence;
545 unsigned seq, shared_count;
546 long ret = timeout ? timeout : 1;
551 seq = read_seqcount_begin(&obj->seq);
555 fence = rcu_dereference(obj->fence_excl);
556 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
557 if (!dma_fence_get_rcu(fence))
560 if (dma_fence_is_signaled(fence)) {
561 dma_fence_put(fence);
570 struct dma_resv_list *fobj = rcu_dereference(obj->fence);
573 shared_count = fobj->shared_count;
575 for (i = 0; !fence && i < shared_count; ++i) {
576 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
578 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
582 if (!dma_fence_get_rcu(lfence))
585 if (dma_fence_is_signaled(lfence)) {
586 dma_fence_put(lfence);
597 if (read_seqcount_retry(&obj->seq, seq)) {
598 dma_fence_put(fence);
602 ret = dma_fence_wait_timeout(fence, intr, ret);
603 dma_fence_put(fence);
604 if (ret > 0 && wait_all && (i + 1 < shared_count))
613 EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu);
616 static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
618 struct dma_fence *fence, *lfence = passed_fence;
621 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
622 fence = dma_fence_get_rcu(lfence);
626 ret = !!dma_fence_is_signaled(fence);
627 dma_fence_put(fence);
633 * dma_resv_test_signaled_rcu - Test if a reservation object's
634 * fences have been signaled.
635 * @obj: the reservation object
636 * @test_all: if true, test all fences, otherwise only test the exclusive
640 * true if all fences signaled, else false
642 bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all)
644 unsigned seq, shared_count;
651 seq = read_seqcount_begin(&obj->seq);
656 struct dma_resv_list *fobj = rcu_dereference(obj->fence);
659 shared_count = fobj->shared_count;
661 for (i = 0; i < shared_count; ++i) {
662 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
664 ret = dma_resv_test_signaled_single(fence);
671 if (read_seqcount_retry(&obj->seq, seq))
676 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
679 ret = dma_resv_test_signaled_single(fence_excl);
683 if (read_seqcount_retry(&obj->seq, seq))
691 EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);