From 5d678afb4ba0a8c763e53e4d72f92accf783a599 Mon Sep 17 00:00:00 2001 From: rwatson Date: Wed, 14 Jul 2004 20:24:21 +0000 Subject: Introduce a new mutex, ng_iface_mtx, to protect the global unit list lock used to synchronize allocation of unit numbers for new netgraph interfaces. Reviewed by: glebius Tested by: glebius --- sys/netgraph/ng_iface.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sys/netgraph/ng_iface.c b/sys/netgraph/ng_iface.c index 899bf22..67ad539 100644 --- a/sys/netgraph/ng_iface.c +++ b/sys/netgraph/ng_iface.c @@ -59,8 +59,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -218,6 +220,9 @@ static int ng_units_in_use = 0; #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY) +static struct mtx ng_iface_mtx; +MTX_SYSINIT(ng_iface, &ng_iface_mtx, "ng_iface", MTX_DEF); + /************************************************************************ HELPER STUFF ************************************************************************/ @@ -289,6 +294,7 @@ ng_iface_get_unit(int *unit) { int index, bit; + mtx_lock(&ng_iface_mtx); for (index = 0; index < ng_iface_units_len && ng_iface_units[index] == 0; index++); if (index == ng_iface_units_len) { /* extend array */ @@ -297,8 +303,10 @@ ng_iface_get_unit(int *unit) newlen = (2 * ng_iface_units_len) + 4; MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units), M_NETGRAPH_IFACE, M_NOWAIT); - if (newarray == NULL) + if (newarray == NULL) { + mtx_unlock(&ng_iface_mtx); return (ENOMEM); + } bcopy(ng_iface_units, newarray, ng_iface_units_len * sizeof(*ng_iface_units)); for (i = ng_iface_units_len; i < newlen; i++) @@ -314,6 +322,7 @@ ng_iface_get_unit(int *unit) ng_iface_units[index] &= ~(1 << bit); *unit = (index * UNITS_BITSPERWORD) + bit; ng_units_in_use++; + mtx_unlock(&ng_iface_mtx); return (0); } @@ -327,6 +336,7 @@ ng_iface_free_unit(int unit) index = unit / UNITS_BITSPERWORD; bit = unit % UNITS_BITSPERWORD; + mtx_lock(&ng_iface_mtx); KASSERT(index < ng_iface_units_len, ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len)); KASSERT((ng_iface_units[index] & (1 << bit)) == 0, @@ -344,6 +354,7 @@ ng_iface_free_unit(int unit) ng_iface_units_len = 0; ng_iface_units = NULL; } + mtx_unlock(&ng_iface_mtx); } /************************************************************************ -- cgit v1.1