From 01e259ddb9e2f84b6e3affe2a84a300efca317fb Mon Sep 17 00:00:00 2001 From: visa Date: Sat, 6 Nov 2021 05:26:33 +0000 Subject: [PATCH] Allocate socket and initialize so_lock in one place This makes witness(4) use a single lock type for tracking so_lock. Previously, so_lock was covered by two distinct lock types because there were separate rw_init() initializers in socreate() and sonewconn(). OK kettenis@ --- sys/kern/uipc_socket.c | 17 ++++++++++++++--- sys/kern/uipc_socket2.c | 5 ++--- sys/sys/socketvar.h | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c index f0d7046c40c..56d3098a6fd 100644 --- a/sys/kern/uipc_socket.c +++ b/sys/kern/uipc_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_socket.c,v 1.267 2021/10/24 07:02:47 visa Exp $ */ +/* $OpenBSD: uipc_socket.c,v 1.268 2021/11/06 05:26:33 visa Exp $ */ /* $NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 christos Exp $ */ /* @@ -148,6 +148,18 @@ soinit(void) #endif } +struct socket * +soalloc(int prflags) +{ + struct socket *so; + + so = pool_get(&socket_pool, prflags); + if (so == NULL) + return (NULL); + rw_init(&so->so_lock, "solock"); + return (so); +} + /* * Socket operation routines. * These routines are called by the routines in @@ -171,8 +183,7 @@ socreate(int dom, struct socket **aso, int type, int proto) return (EPROTONOSUPPORT); if (prp->pr_type != type) return (EPROTOTYPE); - so = pool_get(&socket_pool, PR_WAITOK | PR_ZERO); - rw_init(&so->so_lock, "solock"); + so = soalloc(PR_WAITOK | PR_ZERO); klist_init(&so->so_rcv.sb_sel.si_note, &socket_klistops, so); klist_init(&so->so_snd.sb_sel.si_note, &socket_klistops, so); sigio_init(&so->so_sigio); diff --git a/sys/kern/uipc_socket2.c b/sys/kern/uipc_socket2.c index 66c76560958..42a61e60bd2 100644 --- a/sys/kern/uipc_socket2.c +++ b/sys/kern/uipc_socket2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_socket2.c,v 1.115 2021/10/27 13:41:09 mvs Exp $ */ +/* $OpenBSD: uipc_socket2.c,v 1.116 2021/11/06 05:26:33 visa Exp $ */ /* $NetBSD: uipc_socket2.c,v 1.11 1996/02/04 02:17:55 christos Exp $ */ /* @@ -159,10 +159,9 @@ sonewconn(struct socket *head, int connstatus) return (NULL); if (head->so_qlen + head->so_q0len > head->so_qlimit * 3) return (NULL); - so = pool_get(&socket_pool, PR_NOWAIT|PR_ZERO); + so = soalloc(PR_NOWAIT | PR_ZERO); if (so == NULL) return (NULL); - rw_init(&so->so_lock, "solock"); so->so_type = head->so_type; so->so_options = head->so_options &~ SO_ACCEPTCONN; so->so_linger = head->so_linger; diff --git a/sys/sys/socketvar.h b/sys/sys/socketvar.h index 6c338ac5211..5d9ff7d40a9 100644 --- a/sys/sys/socketvar.h +++ b/sys/sys/socketvar.h @@ -1,4 +1,4 @@ -/* $OpenBSD: socketvar.h,v 1.100 2021/07/26 05:51:13 mpi Exp $ */ +/* $OpenBSD: socketvar.h,v 1.101 2021/11/06 05:26:33 visa Exp $ */ /* $NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $ */ /*- @@ -311,6 +311,7 @@ int soconnect(struct socket *, struct mbuf *); int soconnect2(struct socket *, struct socket *); int socreate(int, struct socket **, int, int); int sodisconnect(struct socket *); +struct socket *soalloc(int); void sofree(struct socket *, int); int sogetopt(struct socket *, int, int, struct mbuf *); void sohasoutofband(struct socket *); -- 2.20.1