Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 19 Nov 2009 14:45:43 +0000
From:      Bruce Simpson <bms@incunabulum.net>
To:        FreeBSD Net <freebsd-net@freebsd.org>
Subject:   [PATCH] CFR: use refcount(9) in mcast
Message-ID:  <4B055A17.9080503@incunabulum.net>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------080101090006010303010009
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Look OK?

All accesses are covered by a mutex, so the atomic ops aren't really 
needed -- but it makes for clearer source.

--------------080101090006010303010009
Content-Type: text/plain;
 name="mcast-refcount9.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="mcast-refcount9.diff"

Index: netinet/in_mcast.c
===================================================================
--- netinet/in_mcast.c	(revision 199528)
+++ netinet/in_mcast.c	(working copy)
@@ -47,6 +47,7 @@
 #include <sys/sysctl.h>
 #include <sys/ktr.h>
 #include <sys/tree.h>
+#include <sys/refcount.h>
 
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -396,13 +397,7 @@
 
 	inm = inm_lookup(ifp, *group);
 	if (inm != NULL) {
-		/*
-		 * If we already joined this group, just bump the
-		 * refcount and return it.
-		 */
-		KASSERT(inm->inm_refcount >= 1,
-		    ("%s: bad refcount %d", __func__, inm->inm_refcount));
-		++inm->inm_refcount;
+		refcount_acquire(&inm->inm_refcount);
 		*pinm = inm;
 		return (0);
 	}
@@ -443,7 +438,7 @@
 			panic("%s: ifma %p is inconsistent with %p (%s)",
 			    __func__, ifma, inm, inet_ntoa(*group));
 #endif
-		++inm->inm_refcount;
+		refcount_acquire(&inm->inm_refcount);
 		*pinm = inm;
 		IF_ADDR_UNLOCK(ifp);
 		return (0);
@@ -464,11 +459,12 @@
 		IF_ADDR_UNLOCK(ifp);
 		return (ENOMEM);
 	}
+	refcount_init(&inm->inm_refcount, 1);
+
 	inm->inm_addr = *group;
 	inm->inm_ifp = ifp;
 	inm->inm_igi = ii->ii_igmp;
 	inm->inm_ifma = ifma;
-	inm->inm_refcount = 1;
 	inm->inm_state = IGMP_NOT_MEMBER;
 
 	/*
@@ -503,11 +499,8 @@
 
 	CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
 
-	if (--inm->inm_refcount > 0) {
-		CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
-		    inm->inm_refcount);
+	if (refcount_release(&inm->inm_refcount) == 0)
 		return;
-	}
 
 	CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
 
Index: netinet/in_var.h
===================================================================
--- netinet/in_var.h	(revision 199528)
+++ netinet/in_var.h	(working copy)
@@ -403,15 +403,6 @@
 	return (inm);
 }
 
-/* Acquire an in_multi record. */
-static __inline void
-inm_acquire_locked(struct in_multi *inm)
-{
-
-	IN_MULTI_LOCK_ASSERT();
-	++inm->inm_refcount;
-}
-
 /*
  * Return values for imo_multi_filter().
  */
Index: netinet/igmp.c
===================================================================
--- netinet/igmp.c	(revision 199528)
+++ netinet/igmp.c	(working copy)
@@ -60,7 +60,7 @@
 #include <sys/kernel.h>
 #include <sys/sysctl.h>
 #include <sys/ktr.h>
-#include <sys/condvar.h>
+#include <sys/refcount.h>
 
 #include <net/if.h>
 #include <net/netisr.h>
@@ -2579,7 +2579,7 @@
 			} else {
 				int retval;
 
-				inm_acquire_locked(inm);
+				refcount_acquire(&inm->inm_refcount);
 
 				retval = igmp_v3_enqueue_group_record(
 				    &inm->inm_scq, inm, 1, 0, 0);
Index: netinet6/in6_var.h
===================================================================
--- netinet6/in6_var.h	(revision 199528)
+++ netinet6/in6_var.h	(working copy)
@@ -713,15 +713,6 @@
 	return (inm);
 }
 
-/* Acquire an in6_multi record. */
-static __inline void
-in6m_acquire_locked(struct in6_multi *inm)
-{
-
-	IN6_MULTI_LOCK_ASSERT();
-	++inm->in6m_refcount;
-}
-
 struct ip6_moptions;
 struct sockopt;
 
Index: netinet6/mld6.c
===================================================================
--- netinet6/mld6.c	(revision 199528)
+++ netinet6/mld6.c	(working copy)
@@ -2199,7 +2199,7 @@
 			} else {
 				int retval;
 
-				in6m_acquire_locked(inm);
+				refcount_acquire(&inm->in6m_refcount);
 
 				retval = mld_v2_enqueue_group_record(
 				    &inm->in6m_scq, inm, 1, 0, 0);
Index: netinet6/in6_mcast.c
===================================================================
--- netinet6/in6_mcast.c	(revision 199528)
+++ netinet6/in6_mcast.c	(working copy)
@@ -50,6 +50,7 @@
 #include <sys/priv.h>
 #include <sys/ktr.h>
 #include <sys/tree.h>
+#include <sys/refcount.h>
 
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -403,13 +404,7 @@
 
 	inm = in6m_lookup_locked(ifp, group);
 	if (inm != NULL) {
-		/*
-		 * If we already joined this group, just bump the
-		 * refcount and return it.
-		 */
-		KASSERT(inm->in6m_refcount >= 1,
-		    ("%s: bad refcount %d", __func__, inm->in6m_refcount));
-		++inm->in6m_refcount;
+		refcount_acquire(&inm->in6m_refcount);
 		*pinm = inm;
 		goto out_locked;
 	}
@@ -449,7 +444,7 @@
 			panic("%s: ifma %p is inconsistent with %p (%p)",
 			    __func__, ifma, inm, group);
 #endif
-		++inm->in6m_refcount;
+		refcount_acquire(&inm->in6m_refcount);
 		*pinm = inm;
 		goto out_locked;
 	}
@@ -470,11 +465,12 @@
 		error = ENOMEM;
 		goto out_locked;
 	}
+	refcount_init(&inm->in6m_refcount, 1);
+
 	inm->in6m_addr = *group;
 	inm->in6m_ifp = ifp;
 	inm->in6m_mli = MLD_IFINFO(ifp);
 	inm->in6m_ifma = ifma;
-	inm->in6m_refcount = 1;
 	inm->in6m_state = MLD_NOT_MEMBER;
 	IFQ_SET_MAXLEN(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
 
@@ -505,11 +501,8 @@
 
 	CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
 
-	if (--inm->in6m_refcount > 0) {
-		CTR2(KTR_MLD, "%s: refcount is now %d", __func__,
-		    inm->in6m_refcount);
+	if (refcount_release(&inm->in6m_refcount) == 0)
 		return;
-	}
 
 	CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
 

--------------080101090006010303010009--



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