Home | History | Annotate | Line # | Download | only in linux
linux_dma_resv.c revision 1.6
      1 /*	$NetBSD: linux_dma_resv.c,v 1.6 2021/12/19 11:53:33 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_dma_resv.c,v 1.6 2021/12/19 11:53:33 riastradh Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/poll.h>
     37 #include <sys/select.h>
     38 
     39 #include <linux/dma-fence.h>
     40 #include <linux/dma-resv.h>
     41 #include <linux/seqlock.h>
     42 #include <linux/ww_mutex.h>
     43 
     44 DEFINE_WW_CLASS(reservation_ww_class __cacheline_aligned);
     45 
     46 static struct dma_resv_list *
     47 objlist_tryalloc(uint32_t n)
     48 {
     49 	struct dma_resv_list *list;
     50 
     51 	list = kmem_alloc(offsetof(typeof(*list), shared[n]), KM_NOSLEEP);
     52 	if (list == NULL)
     53 		return NULL;
     54 	list->shared_max = n;
     55 
     56 	return list;
     57 }
     58 
     59 static void
     60 objlist_free(struct dma_resv_list *list)
     61 {
     62 	uint32_t n = list->shared_max;
     63 
     64 	kmem_free(list, offsetof(typeof(*list), shared[n]));
     65 }
     66 
     67 static void
     68 objlist_free_cb(struct rcu_head *rcu)
     69 {
     70 	struct dma_resv_list *list = container_of(rcu,
     71 	    struct dma_resv_list, rol_rcu);
     72 
     73 	objlist_free(list);
     74 }
     75 
     76 static void
     77 objlist_defer_free(struct dma_resv_list *list)
     78 {
     79 
     80 	call_rcu(&list->rol_rcu, objlist_free_cb);
     81 }
     82 
     83 /*
     84  * dma_resv_init(robj)
     85  *
     86  *	Initialize a reservation object.  Caller must later destroy it
     87  *	with dma_resv_fini.
     88  */
     89 void
     90 dma_resv_init(struct dma_resv *robj)
     91 {
     92 
     93 	ww_mutex_init(&robj->lock, &reservation_ww_class);
     94 	seqcount_init(&robj->seq);
     95 	robj->fence_excl = NULL;
     96 	robj->fence = NULL;
     97 	robj->robj_prealloc = NULL;
     98 }
     99 
    100 /*
    101  * dma_resv_fini(robj)
    102  *
    103  *	Destroy a reservation object, freeing any memory that had been
    104  *	allocated for it.  Caller must have exclusive access to it.
    105  */
    106 void
    107 dma_resv_fini(struct dma_resv *robj)
    108 {
    109 	unsigned i;
    110 
    111 	if (robj->robj_prealloc)
    112 		objlist_free(robj->robj_prealloc);
    113 	if (robj->fence) {
    114 		for (i = 0; i < robj->fence->shared_count; i++)
    115 			dma_fence_put(robj->fence->shared[i]);
    116 		objlist_free(robj->fence);
    117 	}
    118 	if (robj->fence_excl)
    119 		dma_fence_put(robj->fence_excl);
    120 	ww_mutex_destroy(&robj->lock);
    121 }
    122 
    123 /*
    124  * dma_resv_lock(robj, ctx)
    125  *
    126  *	Acquire a reservation object's lock.  Return 0 on success,
    127  *	-EALREADY if caller already holds it, -EDEADLK if a
    128  *	higher-priority owner holds it and the caller must back out and
    129  *	retry.
    130  */
    131 int
    132 dma_resv_lock(struct dma_resv *robj,
    133     struct ww_acquire_ctx *ctx)
    134 {
    135 
    136 	return ww_mutex_lock(&robj->lock, ctx);
    137 }
    138 
    139 /*
    140  * dma_resv_lock_slow(robj, ctx)
    141  *
    142  *	Acquire a reservation object's lock.  Caller must not hold
    143  *	this lock or any others -- this is to be used in slow paths
    144  *	after dma_resv_lock or dma_resv_lock_interruptible has failed
    145  *	and the caller has backed out all other locks.
    146  */
    147 void
    148 dma_resv_lock_slow(struct dma_resv *robj,
    149     struct ww_acquire_ctx *ctx)
    150 {
    151 
    152 	ww_mutex_lock_slow(&robj->lock, ctx);
    153 }
    154 
    155 /*
    156  * dma_resv_lock_interruptible(robj, ctx)
    157  *
    158  *	Acquire a reservation object's lock.  Return 0 on success,
    159  *	-EALREADY if caller already holds it, -EDEADLK if a
    160  *	higher-priority owner holds it and the caller must back out and
    161  *	retry, -ERESTART/-EINTR if interrupted.
    162  */
    163 int
    164 dma_resv_lock_interruptible(struct dma_resv *robj,
    165     struct ww_acquire_ctx *ctx)
    166 {
    167 
    168 	return ww_mutex_lock_interruptible(&robj->lock, ctx);
    169 }
    170 
    171 /*
    172  * dma_resv_lock_slow_interruptible(robj, ctx)
    173  *
    174  *	Acquire a reservation object's lock.  Caller must not hold
    175  *	this lock or any others -- this is to be used in slow paths
    176  *	after dma_resv_lock or dma_resv_lock_interruptible has failed
    177  *	and the caller has backed out all other locks.  Return 0 on
    178  *	success, -ERESTART/-EINTR if interrupted.
    179  */
    180 int
    181 dma_resv_lock_slow_interruptible(struct dma_resv *robj,
    182     struct ww_acquire_ctx *ctx)
    183 {
    184 
    185 	return ww_mutex_lock_slow_interruptible(&robj->lock, ctx);
    186 }
    187 
    188 /*
    189  * dma_resv_trylock(robj)
    190  *
    191  *	Try to acquire a reservation object's lock without blocking.
    192  *	Return true on success, false on failure.
    193  */
    194 bool
    195 dma_resv_trylock(struct dma_resv *robj)
    196 {
    197 
    198 	return ww_mutex_trylock(&robj->lock);
    199 }
    200 
    201 /*
    202  * dma_resv_locking_ctx(robj)
    203  *
    204  *	Return a pointer to the ww_acquire_ctx used by the owner of
    205  *	the reservation object's lock, or NULL if it is either not
    206  *	owned or if it is locked without context.
    207  */
    208 struct ww_acquire_ctx *
    209 dma_resv_locking_ctx(struct dma_resv *robj)
    210 {
    211 
    212 	return ww_mutex_locking_ctx(&robj->lock);
    213 }
    214 
    215 /*
    216  * dma_resv_unlock(robj)
    217  *
    218  *	Release a reservation object's lock.
    219  */
    220 void
    221 dma_resv_unlock(struct dma_resv *robj)
    222 {
    223 
    224 	return ww_mutex_unlock(&robj->lock);
    225 }
    226 
    227 /*
    228  * dma_resv_held(robj)
    229  *
    230  *	True if robj is locked.
    231  */
    232 bool
    233 dma_resv_held(struct dma_resv *robj)
    234 {
    235 
    236 	return ww_mutex_is_locked(&robj->lock);
    237 }
    238 
    239 /*
    240  * dma_resv_assert_held(robj)
    241  *
    242  *	Panic if robj is not held, in DIAGNOSTIC builds.
    243  */
    244 void
    245 dma_resv_assert_held(struct dma_resv *robj)
    246 {
    247 
    248 	KASSERT(dma_resv_held(robj));
    249 }
    250 
    251 /*
    252  * dma_resv_get_excl(robj)
    253  *
    254  *	Return a pointer to the exclusive fence of the reservation
    255  *	object robj.
    256  *
    257  *	Caller must have robj locked.
    258  */
    259 struct dma_fence *
    260 dma_resv_get_excl(struct dma_resv *robj)
    261 {
    262 
    263 	KASSERT(dma_resv_held(robj));
    264 	return robj->fence_excl;
    265 }
    266 
    267 /*
    268  * dma_resv_get_list(robj)
    269  *
    270  *	Return a pointer to the shared fence list of the reservation
    271  *	object robj.
    272  *
    273  *	Caller must have robj locked.
    274  */
    275 struct dma_resv_list *
    276 dma_resv_get_list(struct dma_resv *robj)
    277 {
    278 
    279 	KASSERT(dma_resv_held(robj));
    280 	return robj->fence;
    281 }
    282 
    283 /*
    284  * dma_resv_reserve_shared(robj)
    285  *
    286  *	Reserve space in robj to add a shared fence.  To be used only
    287  *	once before calling dma_resv_add_shared_fence.
    288  *
    289  *	Caller must have robj locked.
    290  *
    291  *	Internally, we start with room for four entries and double if
    292  *	we don't have enough.  This is not guaranteed.
    293  */
    294 int
    295 dma_resv_reserve_shared(struct dma_resv *robj, unsigned int num_fences)
    296 {
    297 	struct dma_resv_list *list, *prealloc;
    298 	uint32_t n, nalloc;
    299 
    300 	KASSERT(dma_resv_held(robj));
    301 	KASSERT(num_fences == 1);
    302 
    303 	list = robj->fence;
    304 	prealloc = robj->robj_prealloc;
    305 
    306 	/* If there's an existing list, check it for space.  */
    307 	if (list) {
    308 		/* If there's too many already, give up.  */
    309 		if (list->shared_count == UINT32_MAX)
    310 			return -ENOMEM;
    311 
    312 		/* Add one more. */
    313 		n = list->shared_count + 1;
    314 
    315 		/* If there's enough for one more, we're done.  */
    316 		if (n <= list->shared_max)
    317 			return 0;
    318 	} else {
    319 		/* No list already.  We need space for 1.  */
    320 		n = 1;
    321 	}
    322 
    323 	/* If not, maybe there's a preallocated list ready.  */
    324 	if (prealloc != NULL) {
    325 		/* If there's enough room in it, stop here.  */
    326 		if (n <= prealloc->shared_max)
    327 			return 0;
    328 
    329 		/* Try to double its capacity.  */
    330 		nalloc = n > UINT32_MAX/2 ? UINT32_MAX : 2*n;
    331 		prealloc = objlist_tryalloc(nalloc);
    332 		if (prealloc == NULL)
    333 			return -ENOMEM;
    334 
    335 		/* Swap the new preallocated list and free the old one.  */
    336 		objlist_free(robj->robj_prealloc);
    337 		robj->robj_prealloc = prealloc;
    338 	} else {
    339 		/* Start with some spare.  */
    340 		nalloc = n > UINT32_MAX/2 ? UINT32_MAX : MAX(2*n, 4);
    341 		prealloc = objlist_tryalloc(nalloc);
    342 		if (prealloc == NULL)
    343 			return -ENOMEM;
    344 		/* Save the new preallocated list.  */
    345 		robj->robj_prealloc = prealloc;
    346 	}
    347 
    348 	/* Success!  */
    349 	return 0;
    350 }
    351 
    352 struct dma_resv_write_ticket {
    353 };
    354 
    355 /*
    356  * dma_resv_write_begin(robj, ticket)
    357  *
    358  *	Begin an atomic batch of writes to robj, and initialize opaque
    359  *	ticket for it.  The ticket must be passed to
    360  *	dma_resv_write_commit to commit the writes.
    361  *
    362  *	Caller must have robj locked.
    363  *
    364  *	Implies membar_producer, i.e. store-before-store barrier.  Does
    365  *	NOT serve as an acquire operation, however.
    366  */
    367 static void
    368 dma_resv_write_begin(struct dma_resv *robj,
    369     struct dma_resv_write_ticket *ticket)
    370 {
    371 
    372 	KASSERT(dma_resv_held(robj));
    373 
    374 	write_seqcount_begin(&robj->seq);
    375 }
    376 
    377 /*
    378  * dma_resv_write_commit(robj, ticket)
    379  *
    380  *	Commit an atomic batch of writes to robj begun with the call to
    381  *	dma_resv_write_begin that returned ticket.
    382  *
    383  *	Caller must have robj locked.
    384  *
    385  *	Implies membar_producer, i.e. store-before-store barrier.  Does
    386  *	NOT serve as a release operation, however.
    387  */
    388 static void
    389 dma_resv_write_commit(struct dma_resv *robj,
    390     struct dma_resv_write_ticket *ticket)
    391 {
    392 
    393 	KASSERT(dma_resv_held(robj));
    394 
    395 	write_seqcount_end(&robj->seq);
    396 }
    397 
    398 struct dma_resv_read_ticket {
    399 	unsigned version;
    400 };
    401 
    402 /*
    403  * dma_resv_read_begin(robj, ticket)
    404  *
    405  *	Begin a read section, and initialize opaque ticket for it.  The
    406  *	ticket must be passed to dma_resv_read_exit, and the
    407  *	caller must be prepared to retry reading if it fails.
    408  */
    409 static void
    410 dma_resv_read_begin(const struct dma_resv *robj,
    411     struct dma_resv_read_ticket *ticket)
    412 {
    413 
    414 	ticket->version = read_seqcount_begin(&robj->seq);
    415 }
    416 
    417 /*
    418  * dma_resv_read_valid(robj, ticket)
    419  *
    420  *	Test whether the read sections are valid.  Return true on
    421  *	success, or false on failure if the read ticket has been
    422  *	invalidated.
    423  */
    424 static bool
    425 dma_resv_read_valid(const struct dma_resv *robj,
    426     struct dma_resv_read_ticket *ticket)
    427 {
    428 
    429 	return !read_seqcount_retry(&robj->seq, ticket->version);
    430 }
    431 
    432 /*
    433  * dma_resv_add_excl_fence(robj, fence)
    434  *
    435  *	Empty and release all of robj's shared fences, and clear and
    436  *	release its exclusive fence.  If fence is nonnull, acquire a
    437  *	reference to it and save it as robj's exclusive fence.
    438  *
    439  *	Caller must have robj locked.
    440  */
    441 void
    442 dma_resv_add_excl_fence(struct dma_resv *robj,
    443     struct dma_fence *fence)
    444 {
    445 	struct dma_fence *old_fence = robj->fence_excl;
    446 	struct dma_resv_list *old_list = robj->fence;
    447 	uint32_t old_shared_count;
    448 	struct dma_resv_write_ticket ticket;
    449 
    450 	KASSERT(dma_resv_held(robj));
    451 
    452 	/*
    453 	 * If we are setting rather than just removing a fence, acquire
    454 	 * a reference for ourselves.
    455 	 */
    456 	if (fence)
    457 		(void)dma_fence_get(fence);
    458 
    459 	/* If there are any shared fences, remember how many.  */
    460 	if (old_list)
    461 		old_shared_count = old_list->shared_count;
    462 
    463 	/* Begin an update.  */
    464 	dma_resv_write_begin(robj, &ticket);
    465 
    466 	/* Replace the fence and zero the shared count.  */
    467 	atomic_store_release(&robj->fence_excl, fence);
    468 	if (old_list)
    469 		old_list->shared_count = 0;
    470 
    471 	/* Commit the update.  */
    472 	dma_resv_write_commit(robj, &ticket);
    473 
    474 	/* Release the old exclusive fence, if any.  */
    475 	if (old_fence)
    476 		dma_fence_put(old_fence);
    477 
    478 	/* Release any old shared fences.  */
    479 	if (old_list) {
    480 		while (old_shared_count--)
    481 			dma_fence_put(old_list->shared[old_shared_count]);
    482 	}
    483 }
    484 
    485 /*
    486  * dma_resv_add_shared_fence(robj, fence)
    487  *
    488  *	Acquire a reference to fence and add it to robj's shared list.
    489  *	If any fence was already added with the same context number,
    490  *	release it and replace it by this one.
    491  *
    492  *	Caller must have robj locked, and must have preceded with a
    493  *	call to dma_resv_reserve_shared for each shared fence
    494  *	added.
    495  */
    496 void
    497 dma_resv_add_shared_fence(struct dma_resv *robj,
    498     struct dma_fence *fence)
    499 {
    500 	struct dma_resv_list *list = robj->fence;
    501 	struct dma_resv_list *prealloc = robj->robj_prealloc;
    502 	struct dma_resv_write_ticket ticket;
    503 	struct dma_fence *replace = NULL;
    504 	uint32_t i;
    505 
    506 	KASSERT(dma_resv_held(robj));
    507 
    508 	/* Acquire a reference to the fence.  */
    509 	KASSERT(fence != NULL);
    510 	(void)dma_fence_get(fence);
    511 
    512 	/* Check for a preallocated replacement list.  */
    513 	if (prealloc == NULL) {
    514 		/*
    515 		 * If there is no preallocated replacement list, then
    516 		 * there must be room in the current list.
    517 		 */
    518 		KASSERT(list != NULL);
    519 		KASSERT(list->shared_count < list->shared_max);
    520 
    521 		/* Begin an update.  Implies membar_producer for fence.  */
    522 		dma_resv_write_begin(robj, &ticket);
    523 
    524 		/* Find a fence with the same context number.  */
    525 		for (i = 0; i < list->shared_count; i++) {
    526 			if (list->shared[i]->context == fence->context) {
    527 				replace = list->shared[i];
    528 				list->shared[i] = fence;
    529 				break;
    530 			}
    531 		}
    532 
    533 		/* If we didn't find one, add it at the end.  */
    534 		if (i == list->shared_count)
    535 			list->shared[list->shared_count++] = fence;
    536 
    537 		/* Commit the update.  */
    538 		dma_resv_write_commit(robj, &ticket);
    539 	} else {
    540 		/*
    541 		 * There is a preallocated replacement list.  There may
    542 		 * not be a current list.  If not, treat it as a zero-
    543 		 * length list.
    544 		 */
    545 		uint32_t shared_count = (list == NULL? 0 : list->shared_count);
    546 
    547 		/* There had better be room in the preallocated list.  */
    548 		KASSERT(shared_count < prealloc->shared_max);
    549 
    550 		/*
    551 		 * Copy the fences over, but replace if we find one
    552 		 * with the same context number.
    553 		 */
    554 		for (i = 0; i < shared_count; i++) {
    555 			if (replace == NULL &&
    556 			    list->shared[i]->context == fence->context) {
    557 				replace = list->shared[i];
    558 				prealloc->shared[i] = fence;
    559 			} else {
    560 				prealloc->shared[i] = list->shared[i];
    561 			}
    562 		}
    563 		prealloc->shared_count = shared_count;
    564 
    565 		/* If we didn't find one, add it at the end.  */
    566 		if (replace == NULL)
    567 			prealloc->shared[prealloc->shared_count++] = fence;
    568 
    569 		/*
    570 		 * Now ready to replace the list.  Begin an update.
    571 		 * Implies membar_producer for fence and prealloc.
    572 		 */
    573 		dma_resv_write_begin(robj, &ticket);
    574 
    575 		/* Replace the list.  */
    576 		atomic_store_release(&robj->fence, prealloc);
    577 		robj->robj_prealloc = NULL;
    578 
    579 		/* Commit the update.  */
    580 		dma_resv_write_commit(robj, &ticket);
    581 
    582 		/*
    583 		 * If there is an old list, free it when convenient.
    584 		 * (We are not in a position at this point to sleep
    585 		 * waiting for activity on all CPUs.)
    586 		 */
    587 		if (list)
    588 			objlist_defer_free(list);
    589 	}
    590 
    591 	/* Release a fence if we replaced it.  */
    592 	if (replace)
    593 		dma_fence_put(replace);
    594 }
    595 
    596 /*
    597  * dma_resv_get_excl_rcu(robj)
    598  *
    599  *	Note: Caller need not call this from an RCU read section.
    600  */
    601 struct dma_fence *
    602 dma_resv_get_excl_rcu(const struct dma_resv *robj)
    603 {
    604 	struct dma_fence *fence;
    605 
    606 	rcu_read_lock();
    607 	fence = dma_fence_get_rcu_safe(&robj->fence_excl);
    608 	rcu_read_unlock();
    609 
    610 	return fence;
    611 }
    612 
    613 /*
    614  * dma_resv_get_fences_rcu(robj, fencep, nsharedp, sharedp)
    615  */
    616 int
    617 dma_resv_get_fences_rcu(const struct dma_resv *robj,
    618     struct dma_fence **fencep, unsigned *nsharedp, struct dma_fence ***sharedp)
    619 {
    620 	const struct dma_resv_list *list;
    621 	struct dma_fence *fence;
    622 	struct dma_fence **shared = NULL;
    623 	unsigned shared_alloc, shared_count, i;
    624 	struct dma_resv_read_ticket ticket;
    625 
    626 top:
    627 	/* Enter an RCU read section and get a read ticket.  */
    628 	rcu_read_lock();
    629 	dma_resv_read_begin(robj, &ticket);
    630 
    631 	/* If there is a shared list, grab it.  */
    632 	if ((list = atomic_load_consume(&robj->fence)) != NULL) {
    633 
    634 		/* Check whether we have a buffer.  */
    635 		if (shared == NULL) {
    636 			/*
    637 			 * We don't have a buffer yet.  Try to allocate
    638 			 * one without waiting.
    639 			 */
    640 			shared_alloc = list->shared_max;
    641 			shared = kcalloc(shared_alloc, sizeof(shared[0]),
    642 			    GFP_NOWAIT);
    643 			if (shared == NULL) {
    644 				/*
    645 				 * Couldn't do it immediately.  Back
    646 				 * out of RCU and allocate one with
    647 				 * waiting.
    648 				 */
    649 				rcu_read_unlock();
    650 				shared = kcalloc(shared_alloc,
    651 				    sizeof(shared[0]), GFP_KERNEL);
    652 				if (shared == NULL)
    653 					return -ENOMEM;
    654 				goto top;
    655 			}
    656 		} else if (shared_alloc < list->shared_max) {
    657 			/*
    658 			 * We have a buffer but it's too small.  We're
    659 			 * already racing in this case, so just back
    660 			 * out and wait to allocate a bigger one.
    661 			 */
    662 			shared_alloc = list->shared_max;
    663 			rcu_read_unlock();
    664 			kfree(shared);
    665 			shared = kcalloc(shared_alloc, sizeof(shared[0]),
    666 			    GFP_KERNEL);
    667 			if (shared == NULL)
    668 				return -ENOMEM;
    669 		}
    670 
    671 		/*
    672 		 * We got a buffer large enough.  Copy into the buffer
    673 		 * and record the number of elements.
    674 		 */
    675 		memcpy(shared, list->shared, shared_alloc * sizeof(shared[0]));
    676 		shared_count = list->shared_count;
    677 	} else {
    678 		/* No shared list: shared count is zero.  */
    679 		shared_count = 0;
    680 	}
    681 
    682 	/* If there is an exclusive fence, grab it.  */
    683 	fence = atomic_load_consume(&robj->fence_excl);
    684 
    685 	/*
    686 	 * We are done reading from robj and list.  Validate our
    687 	 * parking ticket.  If it's invalid, do not pass go and do not
    688 	 * collect $200.
    689 	 */
    690 	if (!dma_resv_read_valid(robj, &ticket))
    691 		goto restart;
    692 
    693 	/*
    694 	 * Try to get a reference to the exclusive fence, if there is
    695 	 * one.  If we can't, start over.
    696 	 */
    697 	if (fence) {
    698 		if (dma_fence_get_rcu(fence) == NULL)
    699 			goto restart;
    700 	}
    701 
    702 	/*
    703 	 * Try to get a reference to all of the shared fences.
    704 	 */
    705 	for (i = 0; i < shared_count; i++) {
    706 		if (dma_fence_get_rcu(shared[i]) == NULL)
    707 			goto put_restart;
    708 	}
    709 
    710 	/* Success!  */
    711 	rcu_read_unlock();
    712 	*fencep = fence;
    713 	*nsharedp = shared_count;
    714 	*sharedp = shared;
    715 	return 0;
    716 
    717 put_restart:
    718 	/* Back out.  */
    719 	while (i --> 0) {
    720 		dma_fence_put(shared[i]);
    721 		shared[i] = NULL; /* paranoia */
    722 	}
    723 	if (fence) {
    724 		dma_fence_put(fence);
    725 		fence = NULL;	/* paranoia */
    726 	}
    727 
    728 restart:
    729 	rcu_read_unlock();
    730 	goto top;
    731 }
    732 
    733 /*
    734  * dma_resv_copy_fences(dst, src)
    735  *
    736  *	Copy the exclusive fence and all the shared fences from src to
    737  *	dst.
    738  *
    739  *	Caller must have dst locked.
    740  */
    741 int
    742 dma_resv_copy_fences(struct dma_resv *dst_robj,
    743     const struct dma_resv *src_robj)
    744 {
    745 	const struct dma_resv_list *src_list;
    746 	struct dma_resv_list *dst_list = NULL;
    747 	struct dma_resv_list *old_list;
    748 	struct dma_fence *fence = NULL;
    749 	struct dma_fence *old_fence;
    750 	uint32_t shared_count, i;
    751 	struct dma_resv_read_ticket read_ticket;
    752 	struct dma_resv_write_ticket write_ticket;
    753 
    754 	KASSERT(dma_resv_held(dst_robj));
    755 
    756 top:
    757 	/* Enter an RCU read section and get a read ticket.  */
    758 	rcu_read_lock();
    759 	dma_resv_read_begin(src_robj, &read_ticket);
    760 
    761 	/* Get the shared list.  */
    762 	if ((src_list = atomic_load_consume(&src_robj->fence)) != NULL) {
    763 
    764 		/* Find out how long it is.  */
    765 		shared_count = src_list->shared_count;
    766 
    767 		/*
    768 		 * Make sure we saw a consistent snapshot of the list
    769 		 * pointer and length.
    770 		 */
    771 		if (!dma_resv_read_valid(src_robj, &read_ticket))
    772 			goto restart;
    773 
    774 		/* Allocate a new list.  */
    775 		dst_list = objlist_tryalloc(shared_count);
    776 		if (dst_list == NULL)
    777 			return -ENOMEM;
    778 
    779 		/* Copy over all fences that are not yet signalled.  */
    780 		dst_list->shared_count = 0;
    781 		for (i = 0; i < shared_count; i++) {
    782 			if ((fence = dma_fence_get_rcu(src_list->shared[i]))
    783 			    != NULL)
    784 				goto restart;
    785 			if (dma_fence_is_signaled(fence)) {
    786 				dma_fence_put(fence);
    787 				fence = NULL;
    788 				continue;
    789 			}
    790 			dst_list->shared[dst_list->shared_count++] = fence;
    791 			fence = NULL;
    792 		}
    793 	}
    794 
    795 	/* Get the exclusive fence.  */
    796 	if ((fence = atomic_load_consume(&src_robj->fence_excl)) != NULL) {
    797 
    798 		/*
    799 		 * Make sure we saw a consistent snapshot of the fence.
    800 		 *
    801 		 * XXX I'm not actually sure this is necessary since
    802 		 * pointer writes are supposed to be atomic.
    803 		 */
    804 		if (!dma_resv_read_valid(src_robj, &read_ticket)) {
    805 			fence = NULL;
    806 			goto restart;
    807 		}
    808 
    809 		/*
    810 		 * If it is going away, restart.  Otherwise, acquire a
    811 		 * reference to it.
    812 		 */
    813 		if (!dma_fence_get_rcu(fence)) {
    814 			fence = NULL;
    815 			goto restart;
    816 		}
    817 	}
    818 
    819 	/* All done with src; exit the RCU read section.  */
    820 	rcu_read_unlock();
    821 
    822 	/*
    823 	 * We now have a snapshot of the shared and exclusive fences of
    824 	 * src_robj and we have acquired references to them so they
    825 	 * won't go away.  Transfer them over to dst_robj, releasing
    826 	 * references to any that were there.
    827 	 */
    828 
    829 	/* Get the old shared and exclusive fences, if any.  */
    830 	old_list = dst_robj->fence;
    831 	old_fence = dst_robj->fence_excl;
    832 
    833 	/* Begin an update.  */
    834 	dma_resv_write_begin(dst_robj, &write_ticket);
    835 
    836 	/* Replace the fences.  */
    837 	membar_exit();
    838 	atomic_store_relaxed(&dst_robj->fence, dst_list);
    839 	atomic_store_relaxed(&dst_robj->fence_excl, fence);
    840 
    841 	/* Commit the update.  */
    842 	dma_resv_write_commit(dst_robj, &write_ticket);
    843 
    844 	/* Release the old exclusive fence, if any.  */
    845 	if (old_fence)
    846 		dma_fence_put(old_fence);
    847 
    848 	/* Release any old shared fences.  */
    849 	if (old_list) {
    850 		for (i = old_list->shared_count; i --> 0;)
    851 			dma_fence_put(old_list->shared[i]);
    852 	}
    853 
    854 	/* Success!  */
    855 	return 0;
    856 
    857 restart:
    858 	rcu_read_unlock();
    859 	if (dst_list) {
    860 		for (i = dst_list->shared_count; i --> 0;) {
    861 			dma_fence_put(dst_list->shared[i]);
    862 			dst_list->shared[i] = NULL;
    863 		}
    864 		objlist_free(dst_list);
    865 		dst_list = NULL;
    866 	}
    867 	if (fence) {
    868 		dma_fence_put(fence);
    869 		fence = NULL;
    870 	}
    871 	goto top;
    872 }
    873 
    874 /*
    875  * dma_resv_test_signaled_rcu(robj, shared)
    876  *
    877  *	If shared is true, test whether all of the shared fences are
    878  *	signalled, or if there are none, test whether the exclusive
    879  *	fence is signalled.  If shared is false, test only whether the
    880  *	exclusive fence is signalled.
    881  *
    882  *	XXX Why does this _not_ test the exclusive fence if shared is
    883  *	true only if there are no shared fences?  This makes no sense.
    884  */
    885 bool
    886 dma_resv_test_signaled_rcu(const struct dma_resv *robj,
    887     bool shared)
    888 {
    889 	struct dma_resv_read_ticket ticket;
    890 	struct dma_resv_list *list;
    891 	struct dma_fence *fence;
    892 	uint32_t i, shared_count;
    893 	bool signaled = true;
    894 
    895 top:
    896 	/* Enter an RCU read section and get a read ticket.  */
    897 	rcu_read_lock();
    898 	dma_resv_read_begin(robj, &ticket);
    899 
    900 	/* If shared is requested and there is a shared list, test it.  */
    901 	if (!shared)
    902 		goto excl;
    903 	if ((list = atomic_load_consume(&robj->fence)) != NULL) {
    904 
    905 		/* Find out how long it is.  */
    906 		shared_count = list->shared_count;
    907 
    908 		/*
    909 		 * Make sure we saw a consistent snapshot of the list
    910 		 * pointer and length.
    911 		 */
    912 		if (!dma_resv_read_valid(robj, &ticket))
    913 			goto restart;
    914 
    915 		/*
    916 		 * For each fence, if it is going away, restart.
    917 		 * Otherwise, acquire a reference to it to test whether
    918 		 * it is signalled.  Stop if we find any that is not
    919 		 * signalled.
    920 		 */
    921 		for (i = 0; i < shared_count; i++) {
    922 			fence = dma_fence_get_rcu(list->shared[i]);
    923 			if (fence == NULL)
    924 				goto restart;
    925 			signaled &= dma_fence_is_signaled(fence);
    926 			dma_fence_put(fence);
    927 			if (!signaled)
    928 				goto out;
    929 		}
    930 	}
    931 
    932 excl:
    933 	/* If there is an exclusive fence, test it.  */
    934 	if ((fence = atomic_load_consume(&robj->fence_excl)) != NULL) {
    935 
    936 		/*
    937 		 * Make sure we saw a consistent snapshot of the fence.
    938 		 *
    939 		 * XXX I'm not actually sure this is necessary since
    940 		 * pointer writes are supposed to be atomic.
    941 		 */
    942 		if (!dma_resv_read_valid(robj, &ticket))
    943 			goto restart;
    944 
    945 		/*
    946 		 * If it is going away, restart.  Otherwise, acquire a
    947 		 * reference to it to test whether it is signalled.
    948 		 */
    949 		if ((fence = dma_fence_get_rcu(fence)) == NULL)
    950 			goto restart;
    951 		signaled &= dma_fence_is_signaled(fence);
    952 		dma_fence_put(fence);
    953 		if (!signaled)
    954 			goto out;
    955 	}
    956 
    957 out:	rcu_read_unlock();
    958 	return signaled;
    959 
    960 restart:
    961 	rcu_read_unlock();
    962 	goto top;
    963 }
    964 
    965 /*
    966  * dma_resv_wait_timeout_rcu(robj, shared, intr, timeout)
    967  *
    968  *	If shared is true, wait for all of the shared fences to be
    969  *	signalled, or if there are none, wait for the exclusive fence
    970  *	to be signalled.  If shared is false, wait only for the
    971  *	exclusive fence to be signalled.  If timeout is zero, don't
    972  *	wait, only test.
    973  *
    974  *	XXX Why does this _not_ wait for the exclusive fence if shared
    975  *	is true only if there are no shared fences?  This makes no
    976  *	sense.
    977  */
    978 long
    979 dma_resv_wait_timeout_rcu(const struct dma_resv *robj,
    980     bool shared, bool intr, unsigned long timeout)
    981 {
    982 	struct dma_resv_read_ticket ticket;
    983 	struct dma_resv_list *list;
    984 	struct dma_fence *fence;
    985 	uint32_t i, shared_count;
    986 	long ret;
    987 
    988 	if (timeout == 0)
    989 		return dma_resv_test_signaled_rcu(robj, shared);
    990 
    991 top:
    992 	/* Enter an RCU read section and get a read ticket.  */
    993 	rcu_read_lock();
    994 	dma_resv_read_begin(robj, &ticket);
    995 
    996 	/* If shared is requested and there is a shared list, wait on it.  */
    997 	if (!shared)
    998 		goto excl;
    999 	if ((list = atomic_load_consume(&robj->fence)) != NULL) {
   1000 
   1001 		/* Find out how long it is.  */
   1002 		shared_count = list->shared_count;
   1003 
   1004 		/*
   1005 		 * Make sure we saw a consistent snapshot of the list
   1006 		 * pointer and length.
   1007 		 */
   1008 		if (!dma_resv_read_valid(robj, &ticket))
   1009 			goto restart;
   1010 
   1011 		/*
   1012 		 * For each fence, if it is going away, restart.
   1013 		 * Otherwise, acquire a reference to it to test whether
   1014 		 * it is signalled.  Stop and wait if we find any that
   1015 		 * is not signalled.
   1016 		 */
   1017 		for (i = 0; i < shared_count; i++) {
   1018 			fence = dma_fence_get_rcu(list->shared[i]);
   1019 			if (fence == NULL)
   1020 				goto restart;
   1021 			if (!dma_fence_is_signaled(fence))
   1022 				goto wait;
   1023 			dma_fence_put(fence);
   1024 		}
   1025 	}
   1026 
   1027 excl:
   1028 	/* If there is an exclusive fence, test it.  */
   1029 	if ((fence = atomic_load_consume(&robj->fence_excl)) != NULL) {
   1030 
   1031 		/*
   1032 		 * Make sure we saw a consistent snapshot of the fence.
   1033 		 *
   1034 		 * XXX I'm not actually sure this is necessary since
   1035 		 * pointer writes are supposed to be atomic.
   1036 		 */
   1037 		if (!dma_resv_read_valid(robj, &ticket))
   1038 			goto restart;
   1039 
   1040 		/*
   1041 		 * If it is going away, restart.  Otherwise, acquire a
   1042 		 * reference to it to test whether it is signalled.  If
   1043 		 * not, wait for it.
   1044 		 */
   1045 		if ((fence = dma_fence_get_rcu(fence)) == NULL)
   1046 			goto restart;
   1047 		if (!dma_fence_is_signaled(fence))
   1048 			goto wait;
   1049 		dma_fence_put(fence);
   1050 	}
   1051 
   1052 	/* Success!  Return the number of ticks left.  */
   1053 	rcu_read_unlock();
   1054 	return timeout;
   1055 
   1056 restart:
   1057 	rcu_read_unlock();
   1058 	goto top;
   1059 
   1060 wait:
   1061 	/*
   1062 	 * Exit the RCU read section, wait for it, and release the
   1063 	 * fence when we're done.  If we time out or fail, bail.
   1064 	 * Otherwise, go back to the top.
   1065 	 */
   1066 	KASSERT(fence != NULL);
   1067 	rcu_read_unlock();
   1068 	ret = dma_fence_wait_timeout(fence, intr, timeout);
   1069 	dma_fence_put(fence);
   1070 	if (ret <= 0)
   1071 		return ret;
   1072 	KASSERT(ret <= timeout);
   1073 	timeout = ret;
   1074 	goto top;
   1075 }
   1076 
   1077 /*
   1078  * dma_resv_poll_init(rpoll, lock)
   1079  *
   1080  *	Initialize reservation poll state.
   1081  */
   1082 void
   1083 dma_resv_poll_init(struct dma_resv_poll *rpoll)
   1084 {
   1085 
   1086 	mutex_init(&rpoll->rp_lock, MUTEX_DEFAULT, IPL_VM);
   1087 	selinit(&rpoll->rp_selq);
   1088 	rpoll->rp_claimed = 0;
   1089 }
   1090 
   1091 /*
   1092  * dma_resv_poll_fini(rpoll)
   1093  *
   1094  *	Release any resource associated with reservation poll state.
   1095  */
   1096 void
   1097 dma_resv_poll_fini(struct dma_resv_poll *rpoll)
   1098 {
   1099 
   1100 	KASSERT(rpoll->rp_claimed == 0);
   1101 	seldestroy(&rpoll->rp_selq);
   1102 	mutex_destroy(&rpoll->rp_lock);
   1103 }
   1104 
   1105 /*
   1106  * dma_resv_poll_cb(fence, fcb)
   1107  *
   1108  *	Callback to notify a reservation poll that a fence has
   1109  *	completed.  Notify any waiters and allow the next poller to
   1110  *	claim the callback.
   1111  *
   1112  *	If one thread is waiting for the exclusive fence only, and we
   1113  *	spuriously notify them about a shared fence, tough.
   1114  */
   1115 static void
   1116 dma_resv_poll_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
   1117 {
   1118 	struct dma_resv_poll *rpoll = container_of(fcb,
   1119 	    struct dma_resv_poll, rp_fcb);
   1120 
   1121 	mutex_enter(&rpoll->rp_lock);
   1122 	selnotify(&rpoll->rp_selq, 0, NOTE_SUBMIT);
   1123 	rpoll->rp_claimed = 0;
   1124 	mutex_exit(&rpoll->rp_lock);
   1125 }
   1126 
   1127 /*
   1128  * dma_resv_do_poll(robj, events, rpoll)
   1129  *
   1130  *	Poll for reservation object events using the reservation poll
   1131  *	state in rpoll:
   1132  *
   1133  *	- POLLOUT	wait for all fences shared and exclusive
   1134  *	- POLLIN	wait for the exclusive fence
   1135  *
   1136  *	Return the subset of events in events that are ready.  If any
   1137  *	are requested but not ready, arrange to be notified with
   1138  *	selnotify when they are.
   1139  */
   1140 int
   1141 dma_resv_do_poll(const struct dma_resv *robj, int events,
   1142     struct dma_resv_poll *rpoll)
   1143 {
   1144 	struct dma_resv_read_ticket ticket;
   1145 	struct dma_resv_list *list;
   1146 	struct dma_fence *fence;
   1147 	uint32_t i, shared_count;
   1148 	int revents;
   1149 	bool recorded = false;	/* curlwp is on the selq */
   1150 	bool claimed = false;	/* we claimed the callback */
   1151 	bool callback = false;	/* we requested a callback */
   1152 
   1153 	/*
   1154 	 * Start with the maximal set of events that could be ready.
   1155 	 * We will eliminate the events that are definitely not ready
   1156 	 * as we go at the same time as we add callbacks to notify us
   1157 	 * that they may be ready.
   1158 	 */
   1159 	revents = events & (POLLIN|POLLOUT);
   1160 	if (revents == 0)
   1161 		return 0;
   1162 
   1163 top:
   1164 	/* Enter an RCU read section and get a read ticket.  */
   1165 	rcu_read_lock();
   1166 	dma_resv_read_begin(robj, &ticket);
   1167 
   1168 	/* If we want to wait for all fences, get the shared list.  */
   1169 	if (!(events & POLLOUT))
   1170 		goto excl;
   1171 	if ((list = atomic_load_consume(&robj->fence)) != NULL) do {
   1172 
   1173 		/* Find out how long it is.  */
   1174 		shared_count = list->shared_count;
   1175 
   1176 		/*
   1177 		 * Make sure we saw a consistent snapshot of the list
   1178 		 * pointer and length.
   1179 		 */
   1180 		if (!dma_resv_read_valid(robj, &ticket))
   1181 			goto restart;
   1182 
   1183 		/*
   1184 		 * For each fence, if it is going away, restart.
   1185 		 * Otherwise, acquire a reference to it to test whether
   1186 		 * it is signalled.  Stop and request a callback if we
   1187 		 * find any that is not signalled.
   1188 		 */
   1189 		for (i = 0; i < shared_count; i++) {
   1190 			fence = dma_fence_get_rcu(list->shared[i]);
   1191 			if (fence == NULL)
   1192 				goto restart;
   1193 			if (!dma_fence_is_signaled(fence)) {
   1194 				dma_fence_put(fence);
   1195 				break;
   1196 			}
   1197 			dma_fence_put(fence);
   1198 		}
   1199 
   1200 		/* If all shared fences have been signalled, move on.  */
   1201 		if (i == shared_count)
   1202 			break;
   1203 
   1204 		/* Put ourselves on the selq if we haven't already.  */
   1205 		if (!recorded)
   1206 			goto record;
   1207 
   1208 		/*
   1209 		 * If someone else claimed the callback, or we already
   1210 		 * requested it, we're guaranteed to be notified, so
   1211 		 * assume the event is not ready.
   1212 		 */
   1213 		if (!claimed || callback) {
   1214 			revents &= ~POLLOUT;
   1215 			break;
   1216 		}
   1217 
   1218 		/*
   1219 		 * Otherwise, find the first fence that is not
   1220 		 * signalled, request the callback, and clear POLLOUT
   1221 		 * from the possible ready events.  If they are all
   1222 		 * signalled, leave POLLOUT set; we will simulate the
   1223 		 * callback later.
   1224 		 */
   1225 		for (i = 0; i < shared_count; i++) {
   1226 			fence = dma_fence_get_rcu(list->shared[i]);
   1227 			if (fence == NULL)
   1228 				goto restart;
   1229 			if (!dma_fence_add_callback(fence, &rpoll->rp_fcb,
   1230 				dma_resv_poll_cb)) {
   1231 				dma_fence_put(fence);
   1232 				revents &= ~POLLOUT;
   1233 				callback = true;
   1234 				break;
   1235 			}
   1236 			dma_fence_put(fence);
   1237 		}
   1238 	} while (0);
   1239 
   1240 excl:
   1241 	/* We always wait for at least the exclusive fence, so get it.  */
   1242 	if ((fence = atomic_load_consume(&robj->fence_excl)) != NULL) do {
   1243 
   1244 		/*
   1245 		 * Make sure we saw a consistent snapshot of the fence.
   1246 		 *
   1247 		 * XXX I'm not actually sure this is necessary since
   1248 		 * pointer writes are supposed to be atomic.
   1249 		 */
   1250 		if (!dma_resv_read_valid(robj, &ticket))
   1251 			goto restart;
   1252 
   1253 		/*
   1254 		 * If it is going away, restart.  Otherwise, acquire a
   1255 		 * reference to it to test whether it is signalled.  If
   1256 		 * not, stop and request a callback.
   1257 		 */
   1258 		if ((fence = dma_fence_get_rcu(fence)) == NULL)
   1259 			goto restart;
   1260 		if (dma_fence_is_signaled(fence)) {
   1261 			dma_fence_put(fence);
   1262 			break;
   1263 		}
   1264 
   1265 		/* Put ourselves on the selq if we haven't already.  */
   1266 		if (!recorded) {
   1267 			dma_fence_put(fence);
   1268 			goto record;
   1269 		}
   1270 
   1271 		/*
   1272 		 * If someone else claimed the callback, or we already
   1273 		 * requested it, we're guaranteed to be notified, so
   1274 		 * assume the event is not ready.
   1275 		 */
   1276 		if (!claimed || callback) {
   1277 			dma_fence_put(fence);
   1278 			revents = 0;
   1279 			break;
   1280 		}
   1281 
   1282 		/*
   1283 		 * Otherwise, try to request the callback, and clear
   1284 		 * all possible ready events.  If the fence has been
   1285 		 * signalled in the interim, leave the events set; we
   1286 		 * will simulate the callback later.
   1287 		 */
   1288 		if (!dma_fence_add_callback(fence, &rpoll->rp_fcb,
   1289 			dma_resv_poll_cb)) {
   1290 			dma_fence_put(fence);
   1291 			revents = 0;
   1292 			callback = true;
   1293 			break;
   1294 		}
   1295 		dma_fence_put(fence);
   1296 	} while (0);
   1297 
   1298 	/* All done reading the fences.  */
   1299 	rcu_read_unlock();
   1300 
   1301 	if (claimed && !callback) {
   1302 		/*
   1303 		 * We claimed the callback but we didn't actually
   1304 		 * request it because a fence was signalled while we
   1305 		 * were claiming it.  Call it ourselves now.  The
   1306 		 * callback doesn't use the fence nor rely on holding
   1307 		 * any of the fence locks, so this is safe.
   1308 		 */
   1309 		dma_resv_poll_cb(NULL, &rpoll->rp_fcb);
   1310 	}
   1311 	return revents;
   1312 
   1313 restart:
   1314 	rcu_read_unlock();
   1315 	goto top;
   1316 
   1317 record:
   1318 	rcu_read_unlock();
   1319 	mutex_enter(&rpoll->rp_lock);
   1320 	selrecord(curlwp, &rpoll->rp_selq);
   1321 	if (!rpoll->rp_claimed)
   1322 		claimed = rpoll->rp_claimed = true;
   1323 	mutex_exit(&rpoll->rp_lock);
   1324 	recorded = true;
   1325 	goto top;
   1326 }
   1327 
   1328 /*
   1329  * dma_resv_kqfilter(robj, kn, rpoll)
   1330  *
   1331  *	Kqueue filter for reservation objects.  Currently not
   1332  *	implemented because the logic to implement it is nontrivial,
   1333  *	and userland will presumably never use it, so it would be
   1334  *	dangerous to add never-tested complex code paths to the kernel.
   1335  */
   1336 int
   1337 dma_resv_kqfilter(const struct dma_resv *robj,
   1338     struct knote *kn, struct dma_resv_poll *rpoll)
   1339 {
   1340 
   1341 	return EINVAL;
   1342 }
   1343