Sun Dec 19 12:13:45 2021 UTC ()
drm: dma_fence_get allows fence to be null; fix assertion.

While here, update comments about semantics for dma_fence_get and
dma_fence_get_rcu (which does not allow null fence).


(riastradh)
diff -r1.25 -r1.26 src/sys/external/bsd/drm2/linux/linux_dma_fence.c

cvs diff -r1.25 -r1.26 src/sys/external/bsd/drm2/linux/linux_dma_fence.c (expand / switch to unified diff)

--- src/sys/external/bsd/drm2/linux/linux_dma_fence.c 2021/12/19 12:11:05 1.25
+++ src/sys/external/bsd/drm2/linux/linux_dma_fence.c 2021/12/19 12:13:45 1.26
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: linux_dma_fence.c,v 1.25 2021/12/19 12:11:05 riastradh Exp $ */ 1/* $NetBSD: linux_dma_fence.c,v 1.26 2021/12/19 12:13:45 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell. 8 * by Taylor R. Campbell.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: linux_dma_fence.c,v 1.25 2021/12/19 12:11:05 riastradh Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: linux_dma_fence.c,v 1.26 2021/12/19 12:13:45 riastradh Exp $");
34 34
35#include <sys/atomic.h> 35#include <sys/atomic.h>
36#include <sys/condvar.h> 36#include <sys/condvar.h>
37#include <sys/queue.h> 37#include <sys/queue.h>
38 38
39#include <linux/atomic.h> 39#include <linux/atomic.h>
40#include <linux/dma-fence.h> 40#include <linux/dma-fence.h>
41#include <linux/errno.h> 41#include <linux/errno.h>
42#include <linux/kref.h> 42#include <linux/kref.h>
43#include <linux/sched.h> 43#include <linux/sched.h>
44#include <linux/spinlock.h> 44#include <linux/spinlock.h>
45 45
46#define FENCE_MAGIC_GOOD 0x607ba424048c37e5ULL 46#define FENCE_MAGIC_GOOD 0x607ba424048c37e5ULL
@@ -236,47 +236,50 @@ dma_fence_get_stub(void) @@ -236,47 +236,50 @@ dma_fence_get_stub(void)
236 * load/unload. 236 * load/unload.
237 */ 237 */
238 static struct dma_fence fence = { 238 static struct dma_fence fence = {
239 .refcount = {1}, /* always referenced */ 239 .refcount = {1}, /* always referenced */
240 .flags = 1u << DMA_FENCE_FLAG_SIGNALED_BIT, 240 .flags = 1u << DMA_FENCE_FLAG_SIGNALED_BIT,
241 }; 241 };
242 242
243 return dma_fence_get(&fence); 243 return dma_fence_get(&fence);
244} 244}
245 245
246/* 246/*
247 * dma_fence_get(fence) 247 * dma_fence_get(fence)
248 * 248 *
249 * Acquire a reference to fence. The fence must not be being 249 * Acquire a reference to fence and return it, or return NULL if
250 * destroyed. Return the fence. 250 * fence is NULL. The fence, if nonnull, must not be being
 251 * destroyed.
251 */ 252 */
252struct dma_fence * 253struct dma_fence *
253dma_fence_get(struct dma_fence *fence) 254dma_fence_get(struct dma_fence *fence)
254{ 255{
255 256
 257 if (fence == NULL)
 258 return NULL;
 259
256 KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence); 260 KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
257 KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence); 261 KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
258 262
259 if (fence) 263 kref_get(&fence->refcount);
260 kref_get(&fence->refcount); 
261 return fence; 264 return fence;
262} 265}
263 266
264/* 267/*
265 * dma_fence_get_rcu(fence) 268 * dma_fence_get_rcu(fence)
266 * 269 *
267 * Attempt to acquire a reference to a fence that may be about to 270 * Attempt to acquire a reference to a fence that may be about to
268 * be destroyed, during a read section. Return the fence on 271 * be destroyed, during a read section. Return the fence on
269 * success, or NULL on failure. 272 * success, or NULL on failure. The fence must be nonnull.
270 */ 273 */
271struct dma_fence * 274struct dma_fence *
272dma_fence_get_rcu(struct dma_fence *fence) 275dma_fence_get_rcu(struct dma_fence *fence)
273{ 276{
274 277
275 __insn_barrier(); 278 __insn_barrier();
276 KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence); 279 KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
277 KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence); 280 KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
278 if (!kref_get_unless_zero(&fence->refcount)) 281 if (!kref_get_unless_zero(&fence->refcount))
279 return NULL; 282 return NULL;
280 return fence; 283 return fence;
281} 284}
282 285