From 59f5133ebe9a9b6db385e0bf3f3cfee74c50fbf6 Mon Sep 17 00:00:00 2001 From: tb Date: Mon, 14 Mar 2022 22:38:43 +0000 Subject: [PATCH] Unbreak the tree, revert commitid aZ8fm4iaUnTCc0ul This reverts the commit protecting the list and hashes in the PCB tables with a mutex since the build of sysctl(8) breaks, as found by kettenis. ok sthen --- sys/kern/kern_sysctl.c | 10 +------ sys/netinet/in_pcb.c | 62 +++++++--------------------------------- sys/netinet/in_pcb.h | 31 +++++++------------- sys/netinet/raw_ip.c | 5 +--- sys/netinet/udp_usrreq.c | 4 +-- sys/netinet6/in6_pcb.c | 28 ++++-------------- sys/netinet6/raw_ip6.c | 9 ++---- 7 files changed, 33 insertions(+), 116 deletions(-) diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 85be1df3b4c..0f8d053b4c5 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_sysctl.c,v 1.400 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: kern_sysctl.c,v 1.401 2022/03/14 22:38:43 tb Exp $ */ /* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */ /*- @@ -1366,24 +1366,16 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep, struct inpcb *inp; NET_LOCK(); - mtx_enter(&tcbtable.inpt_mtx); TAILQ_FOREACH(inp, &tcbtable.inpt_queue, inp_queue) FILLSO(inp->inp_socket); - mtx_leave(&tcbtable.inpt_mtx); - mtx_enter(&udbtable.inpt_mtx); TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) FILLSO(inp->inp_socket); - mtx_leave(&udbtable.inpt_mtx); - mtx_enter(&rawcbtable.inpt_mtx); TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) FILLSO(inp->inp_socket); - mtx_leave(&rawcbtable.inpt_mtx); #ifdef INET6 - mtx_enter(&rawin6pcbtable.inpt_mtx); TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue, inp_queue) FILLSO(inp->inp_socket); - mtx_leave(&rawin6pcbtable.inpt_mtx); #endif NET_UNLOCK(); } diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c index 8cddceea296..561a8d2e3a1 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: in_pcb.c,v 1.260 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: in_pcb.c,v 1.261 2022/03/14 22:38:43 tb Exp $ */ /* $NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $ */ /* @@ -120,8 +120,7 @@ struct baddynamicports baddynamicports; struct baddynamicports rootonlyports; struct pool inpcb_pool; -void in_pcbrehash_locked(struct inpcb *); -int in_pcbresize(struct inpcbtable *, int); +int in_pcbresize (struct inpcbtable *, int); #define INPCBHASH_LOADFACTOR(_x) (((_x) * 3) / 4) @@ -174,7 +173,7 @@ in_pcblhash(struct inpcbtable *table, int rdom, u_short lport) void in_pcbinit(struct inpcbtable *table, int hashsize) { - mtx_init(&table->inpt_mtx, IPL_SOFTNET); + TAILQ_INIT(&table->inpt_queue); table->inpt_hashtbl = hashinit(hashsize, M_PCB, M_WAITOK, &table->inpt_mask); @@ -253,7 +252,6 @@ in_pcballoc(struct socket *so, struct inpcbtable *table) inp->inp_cksum6 = -1; #endif /* INET6 */ - mtx_enter(&table->inpt_mtx); if (table->inpt_count++ > INPCBHASH_LOADFACTOR(table->inpt_size)) (void)in_pcbresize(table, table->inpt_size * 2); TAILQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue); @@ -270,8 +268,6 @@ in_pcballoc(struct socket *so, struct inpcbtable *table) &inp->inp_faddr, inp->inp_fport, &inp->inp_laddr, inp->inp_lport); LIST_INSERT_HEAD(head, inp, inp_hash); - mtx_leave(&table->inpt_mtx); - so->so_pcb = inp; return (0); @@ -560,7 +556,6 @@ void in_pcbdetach(struct inpcb *inp) { struct socket *so = inp->inp_socket; - struct inpcbtable *table = inp->inp_table; NET_ASSERT_LOCKED(); @@ -590,13 +585,10 @@ in_pcbdetach(struct inpcb *inp) pf_inp_unlink(inp); } #endif - mtx_enter(&table->inpt_mtx); LIST_REMOVE(inp, inp_lhash); LIST_REMOVE(inp, inp_hash); - TAILQ_REMOVE(&table->inpt_queue, inp, inp_queue); - table->inpt_count--; - mtx_leave(&table->inpt_mtx); - + TAILQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue); + inp->inp_table->inpt_count--; in_pcbunref(inp); } @@ -669,25 +661,20 @@ void in_pcbnotifyall(struct inpcbtable *table, struct sockaddr *dst, u_int rtable, int errno, void (*notify)(struct inpcb *, int)) { - SIMPLEQ_HEAD(, inpcb) inpcblist; - struct inpcb *inp; + struct inpcb *inp, *ninp; struct in_addr faddr; u_int rdomain; - NET_ASSERT_WLOCKED(); + NET_ASSERT_LOCKED(); if (dst->sa_family != AF_INET) return; faddr = satosin(dst)->sin_addr; if (faddr.s_addr == INADDR_ANY) return; - if (notify == NULL) - return; - SIMPLEQ_INIT(&inpcblist); rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); - TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) { + TAILQ_FOREACH_SAFE(inp, &table->inpt_queue, inp_queue, ninp) { #ifdef INET6 if (inp->inp_flags & INP_IPV6) continue; @@ -697,15 +684,8 @@ in_pcbnotifyall(struct inpcbtable *table, struct sockaddr *dst, u_int rtable, inp->inp_socket == NULL) { continue; } - in_pcbref(inp); - SIMPLEQ_INSERT_TAIL(&inpcblist, inp, inp_notify); - } - mtx_leave(&table->inpt_mtx); - - while ((inp = SIMPLEQ_FIRST(&inpcblist)) != NULL) { - SIMPLEQ_REMOVE_HEAD(&inpcblist, inp_notify); - (*notify)(inp, errno); - in_pcbunref(inp); + if (notify) + (*notify)(inp, errno); } } @@ -779,7 +759,6 @@ in_pcblookup_local(struct inpcbtable *table, void *laddrp, u_int lport_arg, u_int rdomain; rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); head = in_pcblhash(table, rdomain, lport); LIST_FOREACH(inp, head, inp_lhash) { if (rtable_l2(inp->inp_rtableid) != rdomain) @@ -830,8 +809,6 @@ in_pcblookup_local(struct inpcbtable *table, void *laddrp, u_int lport_arg, break; } } - mtx_leave(&table->inpt_mtx); - return (match); } @@ -998,22 +975,11 @@ in_pcbselsrc(struct in_addr **insrc, struct sockaddr_in *sin, void in_pcbrehash(struct inpcb *inp) -{ - struct inpcbtable *table = inp->inp_table; - - mtx_enter(&table->inpt_mtx); - in_pcbrehash_locked(inp); - mtx_leave(&table->inpt_mtx); -} - -void -in_pcbrehash_locked(struct inpcb *inp) { struct inpcbtable *table = inp->inp_table; struct inpcbhead *head; NET_ASSERT_LOCKED(); - MUTEX_ASSERT_LOCKED(&table->inpt_mtx); LIST_REMOVE(inp, inp_lhash); head = in_pcblhash(table, inp->inp_rtableid, inp->inp_lport); @@ -1040,8 +1006,6 @@ in_pcbresize(struct inpcbtable *table, int hashsize) void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl; struct inpcb *inp; - MUTEX_ASSERT_LOCKED(&table->inpt_mtx); - ohashtbl = table->inpt_hashtbl; olhashtbl = table->inpt_lhashtbl; osize = table->inpt_size; @@ -1063,7 +1027,7 @@ in_pcbresize(struct inpcbtable *table, int hashsize) arc4random_buf(&table->inpt_lkey, sizeof(table->inpt_lkey)); TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) { - in_pcbrehash_locked(inp); + in_pcbrehash(inp); } hashfree(ohashtbl, osize, M_PCB); hashfree(olhashtbl, osize, M_PCB); @@ -1094,7 +1058,6 @@ in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr, u_int rdomain; rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); head = in_pcbhash(table, rdomain, &faddr, fport, &laddr, lport); LIST_FOREACH(inp, head, inp_hash) { #ifdef INET6 @@ -1117,7 +1080,6 @@ in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr, break; } } - mtx_leave(&table->inpt_mtx); #ifdef DIAGNOSTIC if (inp == NULL && in_pcbnotifymiss) { printf("%s: faddr=%08x fport=%d laddr=%08x lport=%d rdom=%u\n", @@ -1179,7 +1141,6 @@ in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr, #endif rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); head = in_pcbhash(table, rdomain, &zeroin_addr, 0, key1, lport); LIST_FOREACH(inp, head, inp_hash) { #ifdef INET6 @@ -1216,7 +1177,6 @@ in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr, LIST_REMOVE(inp, inp_hash); LIST_INSERT_HEAD(head, inp, inp_hash); } - mtx_leave(&table->inpt_mtx); #ifdef DIAGNOSTIC if (inp == NULL && in_pcbnotifymiss) { printf("%s: laddr=%08x lport=%d rdom=%u\n", diff --git a/sys/netinet/in_pcb.h b/sys/netinet/in_pcb.h index 1db1bfba501..1f72d73c030 100644 --- a/sys/netinet/in_pcb.h +++ b/sys/netinet/in_pcb.h @@ -1,4 +1,4 @@ -/* $OpenBSD: in_pcb.h,v 1.124 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: in_pcb.h,v 1.125 2022/03/14 22:38:43 tb Exp $ */ /* $NetBSD: in_pcb.h,v 1.14 1996/02/13 23:42:00 christos Exp $ */ /* @@ -73,13 +73,6 @@ #include -/* - * Locks used to protect struct members in this file: - * I immutable after creation - * N net lock - * t inpt_mtx pcb table mutex - */ - struct pf_state_key; union inpaddru { @@ -98,11 +91,10 @@ union inpaddru { * control block. */ struct inpcb { - LIST_ENTRY(inpcb) inp_hash; /* [t] local and foreign hash */ - LIST_ENTRY(inpcb) inp_lhash; /* [t] local port hash */ - TAILQ_ENTRY(inpcb) inp_queue; /* [t] inet PCB queue */ - SIMPLEQ_ENTRY(inpcb) inp_notify; /* [N] queue to notify PCB */ - struct inpcbtable *inp_table; /* [I] inet queue/hash table */ + LIST_ENTRY(inpcb) inp_hash; /* local and foreign hash */ + LIST_ENTRY(inpcb) inp_lhash; /* local port hash */ + TAILQ_ENTRY(inpcb) inp_queue; /* inet PCB queue */ + struct inpcbtable *inp_table; /* inet queue/hash table */ union inpaddru inp_faddru; /* Foreign address. */ union inpaddru inp_laddru; /* Local address. */ #define inp_faddr inp_faddru.iau_a4u.inaddr @@ -162,13 +154,12 @@ struct inpcb { LIST_HEAD(inpcbhead, inpcb); struct inpcbtable { - struct mutex inpt_mtx; /* protect queue and hash */ - TAILQ_HEAD(inpthead, inpcb) inpt_queue; /* [t] inet PCB queue */ - struct inpcbhead *inpt_hashtbl; /* [t] local and foreign hash */ - struct inpcbhead *inpt_lhashtbl; /* [t] local port hash */ - SIPHASH_KEY inpt_key, inpt_lkey; /* [t] secrets for hashes */ - u_long inpt_mask, inpt_lmask; /* [t] hash masks */ - int inpt_count, inpt_size; /* [t] queue count, hash size */ + TAILQ_HEAD(inpthead, inpcb) inpt_queue; /* inet PCB queue */ + struct inpcbhead *inpt_hashtbl; /* local and foreign hash */ + struct inpcbhead *inpt_lhashtbl; /* local port hash */ + SIPHASH_KEY inpt_key, inpt_lkey; /* secrets for hashes */ + u_long inpt_mask, inpt_lmask; /* hash masks */ + int inpt_count, inpt_size; /* queue count, hash size */ }; /* flags in inp_flags: */ diff --git a/sys/netinet/raw_ip.c b/sys/netinet/raw_ip.c index a25bef9fe21..3dbf9278130 100644 --- a/sys/netinet/raw_ip.c +++ b/sys/netinet/raw_ip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: raw_ip.c,v 1.122 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: raw_ip.c,v 1.123 2022/03/14 22:38:43 tb Exp $ */ /* $NetBSD: raw_ip.c,v 1.25 1996/02/18 18:58:33 christos Exp $ */ /* @@ -151,7 +151,6 @@ rip_input(struct mbuf **mp, int *offp, int proto, int af) } #endif NET_ASSERT_LOCKED(); - mtx_enter(&rawcbtable.inpt_mtx); TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) { if (inp->inp_socket->so_state & SS_CANTRCVMORE) continue; @@ -191,8 +190,6 @@ rip_input(struct mbuf **mp, int *offp, int proto, int af) } last = inp; } - mtx_leave(&rawcbtable.inpt_mtx); - if (last) { if (last->inp_flags & INP_CONTROLOPTS || last->inp_socket->so_options & SO_TIMESTAMP) diff --git a/sys/netinet/udp_usrreq.c b/sys/netinet/udp_usrreq.c index 76a65b8c48a..b60e5bc2ea7 100644 --- a/sys/netinet/udp_usrreq.c +++ b/sys/netinet/udp_usrreq.c @@ -1,4 +1,4 @@ -/* $OpenBSD: udp_usrreq.c,v 1.273 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: udp_usrreq.c,v 1.274 2022/03/14 22:38:43 tb Exp $ */ /* $NetBSD: udp_usrreq.c,v 1.28 1996/03/16 23:54:03 christos Exp $ */ /* @@ -365,7 +365,6 @@ udp_input(struct mbuf **mp, int *offp, int proto, int af) */ last = NULL; NET_ASSERT_LOCKED(); - mtx_enter(&udbtable.inpt_mtx); TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) { if (inp->inp_socket->so_state & SS_CANTRCVMORE) continue; @@ -441,7 +440,6 @@ udp_input(struct mbuf **mp, int *offp, int proto, int af) SO_REUSEADDR)) == 0) break; } - mtx_leave(&udbtable.inpt_mtx); if (last == NULL) { /* diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c index e5390085aba..65e578413dc 100644 --- a/sys/netinet6/in6_pcb.c +++ b/sys/netinet6/in6_pcb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: in6_pcb.c,v 1.114 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: in6_pcb.c,v 1.115 2022/03/14 22:38:43 tb Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -369,15 +369,14 @@ in6_pcbnotify(struct inpcbtable *table, struct sockaddr_in6 *dst, uint fport_arg, const struct sockaddr_in6 *src, uint lport_arg, u_int rtable, int cmd, void *cmdarg, void (*notify)(struct inpcb *, int)) { - SIMPLEQ_HEAD(, inpcb) inpcblist; - struct inpcb *inp; + struct inpcb *inp, *ninp; u_short fport = fport_arg, lport = lport_arg; struct sockaddr_in6 sa6_src; int errno; u_int32_t flowinfo; u_int rdomain; - NET_ASSERT_WLOCKED(); + NET_ASSERT_LOCKED(); if ((unsigned)cmd >= PRC_NCMDS) return; @@ -415,13 +414,9 @@ in6_pcbnotify(struct inpcbtable *table, struct sockaddr_in6 *dst, notify = in_rtchange; } errno = inet6ctlerrmap[cmd]; - if (notify == NULL) - return; - SIMPLEQ_INIT(&inpcblist); rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); - TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) { + TAILQ_FOREACH_SAFE(inp, &table->inpt_queue, inp_queue, ninp) { if ((inp->inp_flags & INP_IPV6) == 0) continue; @@ -493,15 +488,8 @@ in6_pcbnotify(struct inpcbtable *table, struct sockaddr_in6 *dst, continue; } do_notify: - in_pcbref(inp); - SIMPLEQ_INSERT_TAIL(&inpcblist, inp, inp_notify); - } - mtx_leave(&table->inpt_mtx); - - while ((inp = SIMPLEQ_FIRST(&inpcblist)) != NULL) { - SIMPLEQ_REMOVE_HEAD(&inpcblist, inp_notify); - (*notify)(inp, errno); - in_pcbunref(inp); + if (notify) + (*notify)(inp, errno); } } @@ -516,7 +504,6 @@ in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr, u_int rdomain; rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); head = in6_pcbhash(table, rdomain, faddr, fport, laddr, lport); LIST_FOREACH(inp, head, inp_hash) { if (!(inp->inp_flags & INP_IPV6)) @@ -537,7 +524,6 @@ in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr, break; } } - mtx_leave(&table->inpt_mtx); #ifdef DIAGNOSTIC if (inp == NULL && in_pcbnotifymiss) { printf("%s: faddr= fport=%d laddr= lport=%d rdom=%u\n", @@ -588,7 +574,6 @@ in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr, #endif rdomain = rtable_l2(rtable); - mtx_enter(&table->inpt_mtx); head = in6_pcbhash(table, rdomain, &zeroin6_addr, 0, key1, lport); LIST_FOREACH(inp, head, inp_hash) { if (!(inp->inp_flags & INP_IPV6)) @@ -621,7 +606,6 @@ in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr, LIST_REMOVE(inp, inp_hash); LIST_INSERT_HEAD(head, inp, inp_hash); } - mtx_leave(&table->inpt_mtx); #ifdef DIAGNOSTIC if (inp == NULL && in_pcbnotifymiss) { printf("%s: laddr= lport=%d rdom=%u\n", diff --git a/sys/netinet6/raw_ip6.c b/sys/netinet6/raw_ip6.c index 36334853b97..6b588d69450 100644 --- a/sys/netinet6/raw_ip6.c +++ b/sys/netinet6/raw_ip6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: raw_ip6.c,v 1.143 2022/03/14 17:23:00 bluhm Exp $ */ +/* $OpenBSD: raw_ip6.c,v 1.144 2022/03/14 22:38:43 tb Exp $ */ /* $KAME: raw_ip6.c,v 1.69 2001/03/04 15:55:44 itojun Exp $ */ /* @@ -157,7 +157,6 @@ rip6_input(struct mbuf **mp, int *offp, int proto, int af) } #endif NET_ASSERT_LOCKED(); - mtx_enter(&rawin6pcbtable.inpt_mtx); TAILQ_FOREACH(in6p, &rawin6pcbtable.inpt_queue, inp_queue) { if (in6p->inp_socket->so_state & SS_CANTRCVMORE) continue; @@ -181,10 +180,8 @@ rip6_input(struct mbuf **mp, int *offp, int proto, int af) IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, *offp, sizeof(*icmp6)); - if (icmp6 == NULL) { - mtx_leave(&rawin6pcbtable.inpt_mtx); + if (icmp6 == NULL) return IPPROTO_DONE; - } if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, in6p->inp_icmp6filt)) continue; @@ -227,8 +224,6 @@ rip6_input(struct mbuf **mp, int *offp, int proto, int af) } last = in6p; } - mtx_leave(&rawin6pcbtable.inpt_mtx); - if (last) { if (last->inp_flags & IN6P_CONTROLOPTS) ip6_savecontrol(last, m, &opts); -- 2.20.1