From owner-svn-src-stable@FreeBSD.ORG Fri Aug 28 21:12:39 2009 Return-Path: Delivered-To: svn-src-stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 51C0F106566B; Fri, 28 Aug 2009 21:12:39 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 3987E8FC1E; Fri, 28 Aug 2009 21:12:39 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n7SLCdiJ036720; Fri, 28 Aug 2009 21:12:39 GMT (envelope-from rwatson@svn.freebsd.org) Received: (from rwatson@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n7SLCdeP036718; Fri, 28 Aug 2009 21:12:39 GMT (envelope-from rwatson@svn.freebsd.org) Message-Id: <200908282112.n7SLCdeP036718@svn.freebsd.org> From: Robert Watson Date: Fri, 28 Aug 2009 21:12:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r196631 - in stable/8/sys: . amd64/include/xen cddl/contrib/opensolaris contrib/dev/acpica contrib/pf dev/xen/xenpci net X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Aug 2009 21:12:39 -0000 Author: rwatson Date: Fri Aug 28 21:12:38 2009 New Revision: 196631 URL: http://svn.freebsd.org/changeset/base/196631 Log: Merge r196553 from head to stable/8: Break out allocation of new ifindex values from if_alloc() and if_vmove(), and centralize in a single function ifindex_alloc(). Assert the IFNET_WLOCK, and add missing IFNET_WLOCK in if_alloc(). This does not close all known races in this code. Reviewed by: bz Approved by: re (kib) Modified: stable/8/sys/ (props changed) stable/8/sys/amd64/include/xen/ (props changed) stable/8/sys/cddl/contrib/opensolaris/ (props changed) stable/8/sys/contrib/dev/acpica/ (props changed) stable/8/sys/contrib/pf/ (props changed) stable/8/sys/dev/xen/xenpci/ (props changed) stable/8/sys/net/if.c Modified: stable/8/sys/net/if.c ============================================================================== --- stable/8/sys/net/if.c Fri Aug 28 21:10:26 2009 (r196630) +++ stable/8/sys/net/if.c Fri Aug 28 21:12:38 2009 (r196631) @@ -223,6 +223,37 @@ ifnet_byindex_ref(u_short idx) return (ifp); } +/* + * Allocate an ifindex array entry; return 0 on success or an error on + * failure. + */ +static int +ifindex_alloc(u_short *idxp) +{ + u_short idx; + + IFNET_WLOCK_ASSERT(); + + /* + * Try to find an empty slot below if_index. If we fail, take the + * next slot. + */ + for (idx = 1; idx <= V_if_index; idx++) { + if (ifnet_byindex_locked(idx) == NULL) + break; + } + + /* Catch if_index overflow. */ + if (idx < 1) + return (ENOSPC); + if (idx > V_if_index) + V_if_index = idx; + if (V_if_index >= V_if_indexlim) + if_grow(); + *idxp = idx; + return (0); +} + static void ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) { @@ -335,32 +366,19 @@ struct ifnet * if_alloc(u_char type) { struct ifnet *ifp; + u_short idx; ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); - - /* - * Try to find an empty slot below if_index. If we fail, take - * the next slot. - * - * XXX: should be locked! - */ - for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { - if (ifnet_byindex(ifp->if_index) == NULL) - break; - } - /* Catch if_index overflow. */ - if (ifp->if_index < 1) { + IFNET_WLOCK(); + if (ifindex_alloc(&idx) != 0) { + IFNET_WUNLOCK(); free(ifp, M_IFNET); return (NULL); } - if (ifp->if_index > V_if_index) - V_if_index = ifp->if_index; - if (V_if_index >= V_if_indexlim) - if_grow(); - + IFNET_WUNLOCK(); + ifp->if_index = idx; ifp->if_type = type; ifp->if_alloctype = type; - if (if_com_alloc[type] != NULL) { ifp->if_l2com = if_com_alloc[type](type, ifp); if (ifp->if_l2com == NULL) { @@ -882,6 +900,7 @@ if_detach_internal(struct ifnet *ifp, in void if_vmove(struct ifnet *ifp, struct vnet *new_vnet) { + u_short idx; /* * Detach from current vnet, but preserve LLADDR info, do not @@ -907,23 +926,12 @@ if_vmove(struct ifnet *ifp, struct vnet */ CURVNET_SET_QUIET(new_vnet); - /* - * Try to find an empty slot below if_index. If we fail, take - * the next slot. - */ IFNET_WLOCK(); - for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { - if (ifnet_byindex_locked(ifp->if_index) == NULL) - break; - } - /* Catch if_index overflow. */ - if (ifp->if_index < 1) + if (ifindex_alloc(&idx) != 0) { + IFNET_WUNLOCK(); panic("if_index overflow"); - - if (ifp->if_index > V_if_index) - V_if_index = ifp->if_index; - if (V_if_index >= V_if_indexlim) - if_grow(); + } + ifp->if_index = idx; ifnet_setbyindex_locked(ifp->if_index, ifp); IFNET_WUNLOCK();