DF-3486 / fix.diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | --- a/sys/netgraph/eiface/ng_eiface.c +++ b/sys/netgraph/eiface/ng_eiface.c @@ -41,6 +41,8 @@ #include <sys/syslog.h> #include <sys/serialize.h> #include <sys/thread2.h> +#include <sys/spinlock2.h> +#include <sys/kthread.h> #include <net/if.h> #include <net/if_types.h> @@ -84,6 +86,9 @@ node_p node; /* Our netgraph node */ hook_p ether; /* Hook for ethernet stream */ struct private *next; /* When hung on the free list */ + int attach_state; /* async ether_ifattach state */ + int down_pending; /* shutdown seen while attaching */ + struct spinlock attach_spin; /* protects attach_state/down_pending */ }; typedef struct ng_eiface_private *priv_p; @@ -307,6 +312,55 @@ ************************************************************************/ /* + * Deferred interface attachment. + * + * Netgraph node constructors run in the control-message path, i.e. on a + * TD_TYPE_NETISR thread (the socket protocol thread). if_attach() may + * not run in netisr context: it takes the ifnet lock, which asserts + * against netisr, and it calls netmsg_service_sync(). So the + * ether_ifattach() is executed from a short-lived kernel thread. + */ +#define EIFACE_ATTACH_PENDING 0 /* thread not started yet */ +#define EIFACE_ATTACH_RUNNING 1 /* thread owns the ifnet */ +#define EIFACE_ATTACH_DONE 2 /* ifnet attached */ +#define EIFACE_ATTACH_CANCELLED 3 /* node died before attach */ + +static void +ng_eiface_attach_thread(void *xpriv) +{ + priv_p priv = xpriv; + int attach, down; + + spin_lock(&priv->attach_spin); + attach = (priv->attach_state == EIFACE_ATTACH_PENDING); + if (attach) + priv->attach_state = EIFACE_ATTACH_RUNNING; + spin_unlock(&priv->attach_spin); + + if (attach) + ether_ifattach(priv->ifp, priv->arpcom.ac_enaddr, NULL); + + spin_lock(&priv->attach_spin); + down = attach && priv->down_pending; + if (attach) + priv->attach_state = EIFACE_ATTACH_DONE; + spin_unlock(&priv->attach_spin); + + /* + * If the node was shut down while we were attaching, bring the + * interface down here; ng_eiface_rmnode() could not do it safely + * at that point. + */ + if (down) { + ifnet_serialize_all(priv->ifp); + priv->ifp->if_flags &= ~(IFF_UP | IFF_RUNNING); + ifq_clr_oactive(&priv->ifp->if_snd); + ifnet_deserialize_all(priv->ifp); + } + lwkt_exit(); +} + +/* * Constructor for a node */ static int @@ -352,8 +406,16 @@ ng_name_node(node, ifname); */ - /* Attach the interface */ - ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL); + /* Attach the interface (deferred; see ng_eiface_attach_thread) */ + spin_init(&priv->attach_spin, "ng_eiface_attach"); + priv->attach_state = EIFACE_ATTACH_PENDING; + if (kthread_create(ng_eiface_attach_thread, priv, NULL, + "ng_eiface_attach") != 0) { + node->private = NULL; + ng_unref(node); + kfree(priv, M_NETGRAPH); + return (ENOMEM); + } /* Done */ return (0); @@ -539,13 +601,31 @@ { const priv_p priv = node->private; struct ifnet *const ifp = priv->ifp; + int down_now; ng_cutlinks(node); node->flags &= ~NG_INVALID; - ifnet_serialize_all(ifp); - ifp->if_flags &= ~(IFF_UP | IFF_RUNNING); - ifq_clr_oactive(&ifp->if_snd); - ifnet_deserialize_all(ifp); + + spin_lock(&priv->attach_spin); + if (priv->attach_state == EIFACE_ATTACH_PENDING) { + /* attach thread has not touched the ifnet yet */ + priv->attach_state = EIFACE_ATTACH_CANCELLED; + down_now = 0; + } else if (priv->attach_state == EIFACE_ATTACH_RUNNING) { + /* defer the interface shutdown to the attach thread */ + priv->down_pending = 1; + down_now = 0; + } else { + down_now = 1; + } + spin_unlock(&priv->attach_spin); + + if (down_now) { + ifnet_serialize_all(ifp); + ifp->if_flags &= ~(IFF_UP | IFF_RUNNING); + ifq_clr_oactive(&ifp->if_snd); + ifnet_deserialize_all(ifp); + } return (0); } --- a/sys/netgraph/iface/ng_iface.c +++ b/sys/netgraph/iface/ng_iface.c @@ -65,6 +65,8 @@ #include <sys/syslog.h> #include <sys/libkern.h> #include <sys/thread2.h> +#include <sys/spinlock2.h> +#include <sys/kthread.h> #include <net/if.h> #include <net/if_types.h> @@ -102,6 +104,8 @@ int unit; /* Interface unit number */ node_p node; /* Our netgraph node */ hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */ + int attach_state; /* async if_attach state */ + struct spinlock attach_spin; /* protects attach_state */ }; typedef struct ng_iface_private *priv_p; @@ -536,6 +540,77 @@ ************************************************************************/ /* + * Deferred interface attachment/detachment. + * + * Netgraph node constructors and shutdown methods run in the + * control-message path, i.e. on a TD_TYPE_NETISR thread (the socket + * protocol thread). if_attach()/if_detach() may not run in netisr + * context: they take the ifnet lock, which asserts against netisr, + * and if_attach() calls netmsg_service_sync(). So the ifnet side of + * the node lifecycle is executed from short-lived kernel threads. + * + * The attach_state machine transfers ownership of the ifnet (and, on + * teardown, of priv itself) atomically between the control path and + * the helper threads. + */ +#define IFACE_ATTACH_PENDING 0 /* thread not started yet */ +#define IFACE_ATTACH_RUNNING 1 /* attach thread owns the ifnet */ +#define IFACE_ATTACH_DONE 2 /* ifnet attached; rmnode owns it */ +#define IFACE_ATTACH_CANCELLED 3 /* rmnode owns it; never attached */ +#define IFACE_ATTACH_DYING 4 /* attach thread owns it + teardown */ +#define IFACE_ATTACH_DETACHING 5 /* detach thread owns it + teardown */ + +static void ng_iface_attach_thread(void *); +static void ng_iface_detach_thread(void *); +static void ng_iface_teardown(priv_p priv); + +static void +ng_iface_attach_thread(void *xpriv) +{ + priv_p priv = xpriv; + int attach; + + spin_lock(&priv->attach_spin); + attach = (priv->attach_state == IFACE_ATTACH_PENDING); + if (attach) + priv->attach_state = IFACE_ATTACH_RUNNING; + spin_unlock(&priv->attach_spin); + + if (attach) { + if_attach(priv->ifp, NULL); + bpfattach(priv->ifp, DLT_NULL, sizeof(uint32_t)); + + spin_lock(&priv->attach_spin); + if (priv->attach_state != IFACE_ATTACH_DYING) + priv->attach_state = IFACE_ATTACH_DONE; + attach = (priv->attach_state == IFACE_ATTACH_DYING); + spin_unlock(&priv->attach_spin); + + if (attach) + ng_iface_teardown(priv); + } + lwkt_exit(); +} + +static void +ng_iface_detach_thread(void *xpriv) +{ + ng_iface_teardown((priv_p)xpriv); + lwkt_exit(); +} + +static void +ng_iface_teardown(priv_p priv) +{ + bpfdetach(priv->ifp); + if_detach(priv->ifp); + kfree(priv->ifp, M_NETGRAPH); + priv->ifp = NULL; + ng_iface_free_unit(priv->unit); + kfree(priv, M_NETGRAPH); +} + +/* * Constructor for a node */ static int @@ -601,9 +676,20 @@ if (ng_name_node(node, ifname) != 0) log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname); - /* Attach the interface */ - if_attach(ifp, NULL); - bpfattach(ifp, DLT_NULL, sizeof(uint32_t)); + /* Attach the interface (deferred; see ng_iface_attach_thread) */ + spin_init(&priv->attach_spin, "ng_iface_attach"); + priv->attach_state = IFACE_ATTACH_PENDING; + error = kthread_create(ng_iface_attach_thread, priv, NULL, + "ng_iface_attach"); + if (error) { + ng_unname(node); + node->private = NULL; + ng_unref(node); + ng_iface_free_unit(priv->unit); + kfree(ifp, M_NETGRAPH); + kfree(priv, M_NETGRAPH); + return (error); + } /* Done */ return (0); @@ -794,15 +880,47 @@ ng_iface_rmnode(node_p node) { const priv_p priv = node->private; + int own_ifp; ng_cutlinks(node); ng_unname(node); - bpfdetach(priv->ifp); - if_detach(priv->ifp); - kfree(priv->ifp, M_NETGRAPH); - priv->ifp = NULL; - ng_iface_free_unit(priv->unit); - kfree(priv, M_NETGRAPH); + + spin_lock(&priv->attach_spin); + switch (priv->attach_state) { + case IFACE_ATTACH_PENDING: + /* never attached: inline cleanup is safe (no ifnet ops) */ + priv->attach_state = IFACE_ATTACH_CANCELLED; + own_ifp = 1; + break; + case IFACE_ATTACH_RUNNING: + /* attach thread owns it and will do the teardown */ + priv->attach_state = IFACE_ATTACH_DYING; + own_ifp = 0; + break; + case IFACE_ATTACH_DONE: + /* + * Attached: if_detach() must not run from the netisr + * control path; hand the teardown to a kernel thread. + */ + priv->attach_state = IFACE_ATTACH_DETACHING; + if (kthread_create(ng_iface_detach_thread, priv, NULL, + "ng_iface_detach") == 0) + own_ifp = 0; + else + own_ifp = 1; /* leak rather than panic */ + break; + default: + own_ifp = 0; + break; + } + spin_unlock(&priv->attach_spin); + + if (own_ifp) { + kfree(priv->ifp, M_NETGRAPH); + priv->ifp = NULL; + ng_iface_free_unit(priv->unit); + kfree(priv, M_NETGRAPH); + } node->private = NULL; ng_unref(node); return (0); |