Index: head/sys/net/pfil.c =================================================================== --- head/sys/net/pfil.c (revision 260215) +++ head/sys/net/pfil.c (working copy) @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -47,8 +48,11 @@ #include #include #include +#include static struct mtx pfil_global_lock; +#define PFIL_HEADLIST_LOCK() mtx_lock(&pfil_global_lock) +#define PFIL_HEADLIST_UNLOCK() mtx_unlock(&pfil_global_lock) MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock", MTX_DEF); @@ -75,7 +79,7 @@ pfil_run_hooks(struct pfil_head *ph, struct mbuf * struct mbuf *m = *mp; int rv = 0; - PFIL_RLOCK(ph, &rmpt); + rm_rlock(&V_pfil_lock, &rmpt); KASSERT(ph->ph_nhooks >= 0, ("Pfil hook count dropped < 0")); for (pfh = pfil_chain_get(dir, ph); pfh != NULL; pfh = TAILQ_NEXT(pfh, pfil_chain)) { @@ -86,7 +90,7 @@ pfil_run_hooks(struct pfil_head *ph, struct mbuf * break; } } - PFIL_RUNLOCK(ph, &rmpt); + rm_runlock(&V_pfil_lock, &rmpt); *mp = m; return (rv); } @@ -104,54 +108,53 @@ pfil_chain_get(int dir, struct pfil_head *ph) } /* - * pfil_try_rlock() acquires rm reader lock for specified head - * if this is immediately possible. + * pfil_try_rlock() acquires rm reader lock if this is immediately possible. */ int -pfil_try_rlock(struct pfil_head *ph, struct rm_priotracker *tracker) +pfil_try_rlock(struct rm_priotracker *tracker) { - return (PFIL_TRY_RLOCK(ph, tracker)); + return (rm_try_rlock(&V_pfil_lock, tracker)); } /* - * pfil_rlock() acquires rm reader lock for specified head. + * pfil_rlock() acquires rm reader lock. */ void -pfil_rlock(struct pfil_head *ph, struct rm_priotracker *tracker) +pfil_rlock(struct rm_priotracker *tracker) { - PFIL_RLOCK(ph, tracker); + rm_rlock(&V_pfil_lock, tracker); } /* - * pfil_runlock() releases reader lock for specified head. + * pfil_runlock() releases reader lock. */ void -pfil_runlock(struct pfil_head *ph, struct rm_priotracker *tracker) +pfil_runlock(struct rm_priotracker *tracker) { - PFIL_RUNLOCK(ph, tracker); + rm_runlock(&V_pfil_lock, tracker); } /* - * pfil_wlock() acquires writer lock for specified head. + * pfil_wlock() acquires writer lock. */ void -pfil_wlock(struct pfil_head *ph) +pfil_wlock(void) { - PFIL_WLOCK(ph); + rm_wlock(&V_pfil_lock); } /* - * pfil_wunlock() releases writer lock for specified head. + * pfil_wunlock() releases writer. */ void -pfil_wunlock(struct pfil_head *ph) +pfil_wunlock(void) { - PFIL_WUNLOCK(ph); + rm_wunlock(&V_pfil_lock); } /* @@ -159,13 +162,24 @@ void * an exclusive lock. */ int -pfil_wowned(struct pfil_head *ph) +pfil_wowned(void) { - return (PFIL_WOWNED(ph)); + return (rm_wowned(&V_pfil_lock)); } /* + * pfil_lock_assert() asserts that the rm lock is in the state specified + * by what. + */ +void +pfil_lock_assert(int what) +{ + + rm_assert(&V_pfil_lock, what); +} + +/* * pfil_head_register() registers a pfil_head with the packet filter hook * mechanism. */ @@ -182,7 +196,6 @@ pfil_head_register(struct pfil_head *ph) return (EEXIST); } } - PFIL_LOCK_INIT(ph); ph->ph_nhooks = 0; TAILQ_INIT(&ph->ph_in); TAILQ_INIT(&ph->ph_out); @@ -200,7 +213,7 @@ int pfil_head_unregister(struct pfil_head *ph) { struct packet_filter_hook *pfh, *pfnext; - + PFIL_HEADLIST_LOCK(); LIST_REMOVE(ph, ph_list); PFIL_HEADLIST_UNLOCK(); @@ -208,7 +221,6 @@ pfil_head_unregister(struct pfil_head *ph) free(pfh, M_IFADDR); TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_chain, pfnext) free(pfh, M_IFADDR); - PFIL_LOCK_DESTROY(ph); return (0); } @@ -259,7 +271,7 @@ pfil_add_hook(pfil_func_t func, void *arg, int fla goto error; } } - PFIL_WLOCK(ph); + rm_wlock(&V_pfil_lock); if (flags & PFIL_IN) { pfh1->pfil_func = func; pfh1->pfil_arg = arg; @@ -279,10 +291,10 @@ pfil_add_hook(pfil_func_t func, void *arg, int fla } ph->ph_nhooks++; } - PFIL_WUNLOCK(ph); + rm_wunlock(&V_pfil_lock); return (0); locked_error: - PFIL_WUNLOCK(ph); + rm_wunlock(&V_pfil_lock); error: if (pfh1 != NULL) free(pfh1, M_IFADDR); @@ -300,7 +312,7 @@ pfil_remove_hook(pfil_func_t func, void *arg, int { int err = 0; - PFIL_WLOCK(ph); + rm_wlock(&V_pfil_lock); if (flags & PFIL_IN) { err = pfil_chain_remove(&ph->ph_in, func, arg); if (err == 0) @@ -311,7 +323,7 @@ pfil_remove_hook(pfil_func_t func, void *arg, int if (err == 0) ph->ph_nhooks--; } - PFIL_WUNLOCK(ph); + rm_wunlock(&V_pfil_lock); return (err); } @@ -368,7 +380,7 @@ vnet_pfil_init(const void *unused) { LIST_INIT(&V_pfil_head_list); - PFIL_LOCK_INIT_REAL(&V_pfil_lock, "shared"); + rm_init_flags(&V_pfil_lock, "PFil rmlock", RM_RECURSE); return (0); } @@ -381,7 +393,7 @@ vnet_pfil_uninit(const void *unused) KASSERT(LIST_EMPTY(&V_pfil_head_list), ("%s: pfil_head_list %p not empty", __func__, &V_pfil_head_list)); - PFIL_LOCK_DESTROY_REAL(&V_pfil_lock); + rm_destroy(&V_pfil_lock); return (0); } Index: head/sys/net/pfil.h =================================================================== --- head/sys/net/pfil.h (revision 260215) +++ head/sys/net/pfil.h (working copy) @@ -34,14 +34,11 @@ #include #include -#include -#include -#include -#include struct mbuf; struct ifnet; struct inpcb; +struct rm_priotracker; typedef int (*pfil_func_t)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *); @@ -67,8 +64,6 @@ typedef TAILQ_HEAD(pfil_chain, packet_filter_hook) #define PFIL_TYPE_AF 1 /* key is AF_* type */ #define PFIL_TYPE_IFNET 2 /* key is ifnet pointer */ -#define PFIL_FLAG_PRIVATE_LOCK 0x01 /* Personal lock instead of global */ - /* * A pfil head is created by each protocol or packet intercept point. * For packet is then run through the hook chain for inspection. @@ -80,10 +75,6 @@ struct pfil_head { int ph_nhooks; #if defined( __linux__ ) || defined( _WIN32 ) rwlock_t ph_mtx; -#else - struct rmlock *ph_plock; /* Pointer to the used lock */ - struct rmlock ph_lock; /* Private lock storage */ - int flags; #endif union { u_long phu_val; @@ -94,6 +85,15 @@ struct pfil_head { LIST_ENTRY(pfil_head) ph_list; }; +/* Public pfil locking functions for self managed locks by packet filters. */ +int pfil_try_rlock(struct rm_priotracker *); +void pfil_rlock(struct rm_priotracker *); +void pfil_runlock(struct rm_priotracker *); +void pfil_wlock(void); +void pfil_wunlock(void); +int pfil_wowned(void); +void pfil_lock_assert(int); + /* Public functions for pfil hook management by packet filters. */ struct pfil_head *pfil_head_get(int, u_long); int pfil_add_hook(pfil_func_t, void *, int, struct pfil_head *); @@ -108,41 +108,4 @@ int pfil_run_hooks(struct pfil_head *, struct mbuf int pfil_head_register(struct pfil_head *); int pfil_head_unregister(struct pfil_head *); -/* Public pfil locking functions for self managed locks by packet filters. */ -struct rm_priotracker; /* Do not require including rmlock header */ -int pfil_try_rlock(struct pfil_head *, struct rm_priotracker *); -void pfil_rlock(struct pfil_head *, struct rm_priotracker *); -void pfil_runlock(struct pfil_head *, struct rm_priotracker *); -void pfil_wlock(struct pfil_head *); -void pfil_wunlock(struct pfil_head *); -int pfil_wowned(struct pfil_head *ph); - -/* Internal pfil locking functions. */ -#define PFIL_LOCK_INIT_REAL(l, t) \ - rm_init_flags(l, "PFil " t " rmlock", RM_RECURSE) -#define PFIL_LOCK_DESTROY_REAL(l) \ - rm_destroy(l) -#define PFIL_LOCK_INIT(p) do { \ - if ((p)->flags & PFIL_FLAG_PRIVATE_LOCK) { \ - PFIL_LOCK_INIT_REAL(&(p)->ph_lock, "private"); \ - (p)->ph_plock = &(p)->ph_lock; \ - } else \ - (p)->ph_plock = &V_pfil_lock; \ -} while (0) -#define PFIL_LOCK_DESTROY(p) do { \ - if ((p)->flags & PFIL_FLAG_PRIVATE_LOCK) \ - PFIL_LOCK_DESTROY_REAL((p)->ph_plock); \ -} while (0) - -#define PFIL_TRY_RLOCK(p, t) rm_try_rlock((p)->ph_plock, (t)) -#define PFIL_RLOCK(p, t) rm_rlock((p)->ph_plock, (t)) -#define PFIL_WLOCK(p) rm_wlock((p)->ph_plock) -#define PFIL_RUNLOCK(p, t) rm_runlock((p)->ph_plock, (t)) -#define PFIL_WUNLOCK(p) rm_wunlock((p)->ph_plock) -#define PFIL_WOWNED(p) rm_wowned((p)->ph_plock) - -/* Internal locking macros for global/vnet pfil_head_list. */ -#define PFIL_HEADLIST_LOCK() mtx_lock(&pfil_global_lock) -#define PFIL_HEADLIST_UNLOCK() mtx_unlock(&pfil_global_lock) - #endif /* _NET_PFIL_H_ */