Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Oct 2018 14:37:27 +0000 (UTC)
From:      Hans Petter Selasky <hselasky@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r339862 - stable/11/sys/sys
Message-ID:  <201810291437.w9TEbRH1041468@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hselasky
Date: Mon Oct 29 14:37:27 2018
New Revision: 339862
URL: https://svnweb.freebsd.org/changeset/base/339862

Log:
  MFC r339600:
  Make sure returned value is checked and assert a valid refcount.
  While at it fix a print: Unsigned types cannot be negative.
  
  Reviewed by:		kib, mjg
  Differential revision:	https://reviews.freebsd.org/D17616
  Sponsored by:		Mellanox Technologies

Modified:
  stable/11/sys/sys/refcount.h
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/sys/sys/refcount.h
==============================================================================
--- stable/11/sys/sys/refcount.h	Mon Oct 29 14:36:03 2018	(r339861)
+++ stable/11/sys/sys/refcount.h	Mon Oct 29 14:37:27 2018	(r339862)
@@ -60,7 +60,7 @@ refcount_release(volatile u_int *count)
 
 	atomic_thread_fence_rel();
 	old = atomic_fetchadd_int(count, -1);
-	KASSERT(old > 0, ("negative refcount %p", count));
+	KASSERT(old > 0, ("refcount %p is zero", count));
 	if (old > 1)
 		return (0);
 
@@ -75,15 +75,19 @@ refcount_release(volatile u_int *count)
 }
 
 /*
+ * This functions returns non-zero if the refcount was
+ * incremented. Else zero is returned.
+ *
  * A temporary hack until refcount_* APIs are sorted out.
  */
-static __inline int
+static __inline __result_use_check int
 refcount_acquire_if_not_zero(volatile u_int *count)
 {
 	u_int old;
 
 	old = *count;
 	for (;;) {
+		KASSERT(old < UINT_MAX, ("refcount %p overflowed", count));
 		if (old == 0)
 			return (0);
 		if (atomic_fcmpset_int(count, &old, old + 1))
@@ -91,13 +95,14 @@ refcount_acquire_if_not_zero(volatile u_int *count)
 	}
 }
 
-static __inline int
+static __inline __result_use_check int
 refcount_release_if_not_last(volatile u_int *count)
 {
 	u_int old;
 
 	old = *count;
 	for (;;) {
+		KASSERT(old > 0, ("refcount %p is zero", count));
 		if (old == 1)
 			return (0);
 		if (atomic_fcmpset_int(count, &old, old - 1))



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201810291437.w9TEbRH1041468>