commit 4907cafed81435c426299cccd2ecee63088db39c Author: Ryota Ozaki Date: Mon Jun 13 12:35:05 2016 +0900 Introduce curlwp_bind and curlwp_unbind for psref(9) diff --git a/sys/compat/common/uipc_syscalls_40.c b/sys/compat/common/uipc_syscalls_40.c index 6bf7838..37efc1e 100644 --- a/sys/compat/common/uipc_syscalls_40.c +++ b/sys/compat/common/uipc_syscalls_40.c @@ -40,7 +40,7 @@ compat_ifconf(u_long cmd, void *data) const int sz = (int)sizeof(ifr); const bool docopy = ifc->ifc_req != NULL; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; struct psref psref; if (docopy) { @@ -48,7 +48,7 @@ compat_ifconf(u_long cmd, void *data) ifrp = ifc->ifc_req; } - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); @@ -121,7 +121,7 @@ compat_ifconf(u_long cmd, void *data) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); if (docopy) ifc->ifc_len -= space; @@ -131,7 +131,7 @@ compat_ifconf(u_long cmd, void *data) release_exit: psref_release(&psref, &ifp->if_psref, ifnet_psref_class); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } #endif diff --git a/sys/compat/linux/common/linux_socket.c b/sys/compat/linux/common/linux_socket.c index 2c2e7cd..1d2d8db 100644 --- a/sys/compat/linux/common/linux_socket.c +++ b/sys/compat/linux/common/linux_socket.c @@ -1119,7 +1119,7 @@ linux_getifconf(struct lwp *l, register_t *retval, void *data) const int sz = (int)sizeof(ifr); bool docopy; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; struct psref psref; error = copyin(data, &ifc, sizeof(ifc)); @@ -1132,7 +1132,7 @@ linux_getifconf(struct lwp *l, register_t *retval, void *data) ifrp = ifc.ifc_req; } - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); @@ -1167,7 +1167,7 @@ linux_getifconf(struct lwp *l, register_t *retval, void *data) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); if (docopy) ifc.ifc_len -= space; @@ -1178,7 +1178,7 @@ linux_getifconf(struct lwp *l, register_t *retval, void *data) release_exit: psref_release(&psref, &ifp->if_psref, ifnet_psref_class); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } diff --git a/sys/compat/linux32/common/linux32_socket.c b/sys/compat/linux32/common/linux32_socket.c index 0bd1b96..cb992204 100644 --- a/sys/compat/linux32/common/linux32_socket.c +++ b/sys/compat/linux32/common/linux32_socket.c @@ -420,7 +420,7 @@ linux32_getifconf(struct lwp *l, register_t *retval, void *data) const int sz = (int)sizeof(ifr); bool docopy; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; struct psref psref; error = copyin(data, &ifc, sizeof(ifc)); @@ -433,7 +433,7 @@ linux32_getifconf(struct lwp *l, register_t *retval, void *data) ifrp = NETBSD32PTR64(ifc.ifc_req); } - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); @@ -468,7 +468,7 @@ linux32_getifconf(struct lwp *l, register_t *retval, void *data) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); if (docopy) ifc.ifc_len -= space; @@ -479,7 +479,7 @@ linux32_getifconf(struct lwp *l, register_t *retval, void *data) release_exit: psref_release(&psref, &ifp->if_psref, ifnet_psref_class); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } diff --git a/sys/dist/pf/net/pf_if.c b/sys/dist/pf/net/pf_if.c index 4aa6d80..fcf51d3 100644 --- a/sys/dist/pf/net/pf_if.c +++ b/sys/dist/pf/net/pf_if.c @@ -101,7 +101,7 @@ void pfi_initialize(void) { int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; if (pfi_all != NULL) /* already initialized */ return; @@ -122,7 +122,7 @@ pfi_initialize(void) #ifdef __NetBSD__ ifnet_t *ifp; - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { struct psref psref; @@ -136,7 +136,7 @@ pfi_initialize(void) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); pfil_add_hook(pfil_ifnet_wrapper, NULL, PFIL_IFNET, if_pfil); pfil_add_hook(pfil_ifaddr_wrapper, NULL, PFIL_IFADDR, if_pfil); @@ -150,12 +150,12 @@ pfi_destroy(void) struct pfi_kif *p; ifnet_t *ifp; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; pfil_remove_hook(pfil_ifaddr_wrapper, NULL, PFIL_IFADDR, if_pfil); pfil_remove_hook(pfil_ifnet_wrapper, NULL, PFIL_IFNET, if_pfil); - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { struct psref psref; @@ -169,7 +169,7 @@ pfi_destroy(void) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); while ((p = RB_MIN(pfi_ifhead, &pfi_ifs))) { RB_REMOVE(pfi_ifhead, &pfi_ifs, p); diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c index 632a08a..eef82a7 100644 --- a/sys/kern/uipc_mbuf.c +++ b/sys/kern/uipc_mbuf.c @@ -558,8 +558,7 @@ m_reclaim(void *arg, int flags) } /* XXX we cannot use psref in H/W interrupt */ if (!cpu_intr_p()) { - int bound = curlwp->l_pflag & LP_BOUND; - curlwp->l_pflag |= LP_BOUND; + int bound = curlwp_bind(); IFNET_READER_FOREACH(ifp) { struct psref psref; @@ -572,7 +571,7 @@ m_reclaim(void *arg, int flags) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); } splx(s); mbstat.m_drain++; diff --git a/sys/net/if.c b/sys/net/if.c index 8b076e9..6954eeb 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -941,9 +941,8 @@ if_attachdomain(void) { struct ifnet *ifp; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound = curlwp_bind(); - curlwp->l_pflag |= LP_BOUND; s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { struct psref psref; @@ -954,7 +953,7 @@ if_attachdomain(void) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); } static void @@ -2478,7 +2477,7 @@ doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) #endif int r; struct psref psref; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; switch (cmd) { #ifdef COMPAT_OIFREQ @@ -2510,7 +2509,7 @@ doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) switch (cmd) { case SIOCIFCREATE: case SIOCIFDESTROY: - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); if (l != NULL) { ifp = if_get(ifr->ifr_name, &psref); error = kauth_authorize_network(l->l_cred, @@ -2520,7 +2519,7 @@ doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) if (ifp != NULL) if_put(ifp, &psref); if (error != 0) { - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } } @@ -2529,7 +2528,7 @@ doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) if_clone_create(ifr->ifr_name) : if_clone_destroy(ifr->ifr_name); mutex_exit(&if_clone_mtx); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return r; case SIOCIFGCLONERS: @@ -2540,10 +2539,10 @@ doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) } } - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); ifp = if_get(ifr->ifr_name, &psref); if (ifp == NULL) { - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return ENXIO; } @@ -2617,7 +2616,7 @@ doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) mutex_exit(ifp->if_ioctl_lock); out: if_put(ifp, &psref); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } @@ -2661,7 +2660,7 @@ ifconf(u_long cmd, void *data) const int sz = (int)sizeof(struct ifreq); const bool docopy = ifc->ifc_req != NULL; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound; struct psref psref; if (docopy) { @@ -2669,7 +2668,7 @@ ifconf(u_long cmd, void *data) ifrp = ifc->ifc_req; } - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); @@ -2719,7 +2718,7 @@ ifconf(u_long cmd, void *data) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); if (docopy) { KASSERT(0 <= space && space <= ifc->ifc_len); @@ -2732,7 +2731,7 @@ ifconf(u_long cmd, void *data) release_exit: psref_release(&psref, &ifp->if_psref, ifnet_psref_class); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } diff --git a/sys/net/rtsock.c b/sys/net/rtsock.c index e8d410f..d1c2761 100644 --- a/sys/net/rtsock.c +++ b/sys/net/rtsock.c @@ -1460,11 +1460,10 @@ sysctl_iflist(int af, struct rt_walkarg *w, int type) int len, error = 0; int s; struct psref psref; - int bound = curlwp->l_pflag & LP_BOUND; + int bound = curlwp_bind(); memset(&info, 0, sizeof(info)); - curlwp->l_pflag |= LP_BOUND; s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { if (w->w_arg && w->w_arg != ifp->if_index) @@ -1560,13 +1559,13 @@ sysctl_iflist(int af, struct rt_walkarg *w, int type) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return 0; release_exit: psref_release(&psref, &ifp->if_psref, ifnet_psref_class); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } diff --git a/sys/netinet/ip_carp.c b/sys/netinet/ip_carp.c index 7b9adad3..161f9b2 100644 --- a/sys/netinet/ip_carp.c +++ b/sys/netinet/ip_carp.c @@ -933,9 +933,8 @@ carp_send_ad_all(void) struct carp_if *cif; struct carp_softc *vh; int s; - int bound = curlwp->l_pflag & LP_BOUND; + int bound = curlwp_bind(); - curlwp->l_pflag |= LP_BOUND; s = pserialize_read_enter(); IFNET_READER_FOREACH(ifp) { struct psref psref; @@ -956,7 +955,7 @@ carp_send_ad_all(void) psref_release(&psref, &ifp->if_psref, ifnet_psref_class); } pserialize_read_exit(s); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); } diff --git a/sys/netinet6/raw_ip6.c b/sys/netinet6/raw_ip6.c index 92392dc..f255886 100644 --- a/sys/netinet6/raw_ip6.c +++ b/sys/netinet6/raw_ip6.c @@ -387,9 +387,7 @@ rip6_output(struct mbuf *m, struct socket * const so, int type, code; /* for ICMPv6 output statistics only */ int scope_ambiguous = 0; struct in6_addr *in6a; - int bound = curlwp->l_pflag & LP_BOUND; - - curlwp->l_pflag |= LP_BOUND; + int bound = curlwp_bind(); in6p = sotoin6pcb(so); @@ -536,7 +534,7 @@ rip6_output(struct mbuf *m, struct socket * const so, ip6_clearpktopts(&opt, -1); m_freem(control); } - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); return error; } diff --git a/sys/rump/net/lib/libshmif/if_shmem.c b/sys/rump/net/lib/libshmif/if_shmem.c index 0e2616f..35ac51b 100644 --- a/sys/rump/net/lib/libshmif/if_shmem.c +++ b/sys/rump/net/lib/libshmif/if_shmem.c @@ -764,14 +764,14 @@ shmif_rcv(void *arg) } if (passup) { - int bound = curlwp->l_pflag & LP_BOUND; + int bound; ifp->if_ipackets++; KERNEL_LOCK(1, NULL); /* Prevent LWP migrations between CPUs for psref(9) */ - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); bpf_mtap(ifp, m); if_input(ifp, m); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); KERNEL_UNLOCK_ONE(NULL); m = NULL; } diff --git a/sys/rump/net/lib/libvirtif/if_virt.c b/sys/rump/net/lib/libvirtif/if_virt.c index 076bfde..aa97d3b 100644 --- a/sys/rump/net/lib/libvirtif/if_virt.c +++ b/sys/rump/net/lib/libvirtif/if_virt.c @@ -374,15 +374,15 @@ VIF_DELIVERPKT(struct virtif_sc *sc, struct iovec *iov, size_t iovlen) } if (passup) { - int bound = curlwp->l_pflag & LP_BOUND; + int bound; ifp->if_ipackets++; m_set_rcvif(m, ifp); KERNEL_LOCK(1, NULL); /* Prevent LWP migrations between CPUs for psref(9) */ - curlwp->l_pflag |= LP_BOUND; + bound = curlwp_bind(); bpf_mtap(ifp, m); if_input(ifp, m); - curlwp->l_pflag ^= bound ^ LP_BOUND; + curlwp_unbind(bound); KERNEL_UNLOCK_LAST(NULL); } else { m_freem(m); diff --git a/sys/sys/lwp.h b/sys/sys/lwp.h index 10a291c..983e216 100644 --- a/sys/sys/lwp.h +++ b/sys/sys/lwp.h @@ -521,6 +521,24 @@ KPREEMPT_ENABLE(lwp_t *l) #define DOPREEMPT_ACTIVE 0x01 #define DOPREEMPT_COUNTED 0x02 +/* For psref(9) used in unbound LWP contexts */ +static inline int +curlwp_bind(void) +{ + int bound; + + bound = curlwp->l_pflag & LP_BOUND; + curlwp->l_pflag |= LP_BOUND; + + return bound; +} + +static inline void +curlwp_unbind(int bound) +{ + curlwp->l_pflag ^= bound ^ LP_BOUND; +} + #endif /* _KERNEL */ /* Flags for _lwp_create(), as per Solaris. */