diff options
author | rwatson <rwatson@FreeBSD.org> | 2009-03-14 16:06:06 +0000 |
---|---|---|
committer | rwatson <rwatson@FreeBSD.org> | 2009-03-14 16:06:06 +0000 |
commit | 874a1578bb8d1d47508c0fab69ef796bed233686 (patch) | |
tree | a0703274e6f642c1143773213e750b20510252bc /sys/security/mac/mac_net.c | |
parent | 7f1b26ac0cda36f28a34c828f3295d040943c70b (diff) | |
download | FreeBSD-src-874a1578bb8d1d47508c0fab69ef796bed233686.zip FreeBSD-src-874a1578bb8d1d47508c0fab69ef796bed233686.tar.gz |
Rework MAC Framework synchronization in a number of ways in order to
improve performance:
- Eliminate custom reference count and condition variable to monitor
threads entering the framework, as this had both significant overhead
and behaved badly in the face of contention.
- Replace reference count with two locks: an rwlock and an sx lock,
which will be read-acquired by threads entering the framework
depending on whether a give policy entry point is permitted to sleep
or not.
- Replace previous mutex locking of the reference count for exclusive
access with write acquiring of both the policy list sx and rw locks,
which occurs only when policies are attached or detached.
- Do a lockless read of the dynamic policy list head before acquiring
any locks in order to reduce overhead when no dynamic policies are
loaded; this a race we can afford to lose.
- For every policy entry point invocation, decide whether sleeping is
permitted, and if not, use a _NOSLEEP() variant of the composition
macros, which will use the rwlock instead of the sxlock. In some
cases, we decide which to use based on allocation flags passed to the
MAC Framework entry point.
As with the move to rwlocks/rmlocks in pfil, this may trigger witness
warnings, but these should (generally) be false positives as all
acquisition of the locks is for read with two very narrow exceptions
for policy load/unload, and those code blocks should never acquire
other locks.
Sponsored by: Google, Inc.
Obtained from: TrustedBSD Project
Discussed with: csjp (idea, not specific patch)
Diffstat (limited to 'sys/security/mac/mac_net.c')
-rw-r--r-- | sys/security/mac/mac_net.c | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/sys/security/mac/mac_net.c b/sys/security/mac/mac_net.c index 4fccbd7..697d02a 100644 --- a/sys/security/mac/mac_net.c +++ b/sys/security/mac/mac_net.c @@ -153,9 +153,12 @@ mac_mbuf_tag_init(struct m_tag *tag, int flag) label = (struct label *) (tag + 1); mac_init_label(label); - MAC_CHECK(mbuf_init_label, label, flag); + if (flag & M_WAITOK) + MAC_CHECK(mbuf_init_label, label, flag); + else + MAC_CHECK_NOSLEEP(mbuf_init_label, label, flag); if (error) { - MAC_PERFORM(mbuf_destroy_label, label); + MAC_PERFORM_NOSLEEP(mbuf_destroy_label, label); mac_destroy_label(label); } return (error); @@ -188,7 +191,7 @@ static void mac_bpfdesc_label_free(struct label *label) { - MAC_PERFORM(bpfdesc_destroy_label, label); + MAC_PERFORM_NOSLEEP(bpfdesc_destroy_label, label); mac_labelzone_free(label); } @@ -206,7 +209,7 @@ static void mac_ifnet_label_free(struct label *label) { - MAC_PERFORM(ifnet_destroy_label, label); + MAC_PERFORM_NOSLEEP(ifnet_destroy_label, label); mac_labelzone_free(label); } @@ -227,7 +230,7 @@ mac_mbuf_tag_destroy(struct m_tag *tag) label = (struct label *)(tag+1); - MAC_PERFORM(mbuf_destroy_label, label); + MAC_PERFORM_NOSLEEP(mbuf_destroy_label, label); mac_destroy_label(label); } @@ -247,7 +250,7 @@ mac_mbuf_tag_copy(struct m_tag *src, struct m_tag *dest) * mac_mbuf_tag_init() is called on the target tag in m_tag_copy(), * so we don't need to call it here. */ - MAC_PERFORM(mbuf_copy_label, src_label, dest_label); + MAC_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label); } void @@ -258,14 +261,14 @@ mac_mbuf_copy(struct mbuf *m_from, struct mbuf *m_to) src_label = mac_mbuf_to_label(m_from); dest_label = mac_mbuf_to_label(m_to); - MAC_PERFORM(mbuf_copy_label, src_label, dest_label); + MAC_PERFORM_NOSLEEP(mbuf_copy_label, src_label, dest_label); } static void mac_ifnet_copy_label(struct label *src, struct label *dest) { - MAC_PERFORM(ifnet_copy_label, src, dest); + MAC_PERFORM_NOSLEEP(ifnet_copy_label, src, dest); } static int @@ -294,7 +297,7 @@ mac_ifnet_create(struct ifnet *ifp) { MAC_IFNET_LOCK(ifp); - MAC_PERFORM(ifnet_create, ifp, ifp->if_label); + MAC_PERFORM_NOSLEEP(ifnet_create, ifp, ifp->if_label); MAC_IFNET_UNLOCK(ifp); } @@ -302,7 +305,7 @@ void mac_bpfdesc_create(struct ucred *cred, struct bpf_d *d) { - MAC_PERFORM(bpfdesc_create, cred, d, d->bd_label); + MAC_PERFORM_NOSLEEP(bpfdesc_create, cred, d, d->bd_label); } void @@ -314,7 +317,7 @@ mac_bpfdesc_create_mbuf(struct bpf_d *d, struct mbuf *m) label = mac_mbuf_to_label(m); - MAC_PERFORM(bpfdesc_create_mbuf, d, d->bd_label, m, label); + MAC_PERFORM_NOSLEEP(bpfdesc_create_mbuf, d, d->bd_label, m, label); } void @@ -325,7 +328,7 @@ mac_ifnet_create_mbuf(struct ifnet *ifp, struct mbuf *m) label = mac_mbuf_to_label(m); MAC_IFNET_LOCK(ifp); - MAC_PERFORM(ifnet_create_mbuf, ifp, ifp->if_label, m, label); + MAC_PERFORM_NOSLEEP(ifnet_create_mbuf, ifp, ifp->if_label, m, label); MAC_IFNET_UNLOCK(ifp); } @@ -340,7 +343,8 @@ mac_bpfdesc_check_receive(struct bpf_d *d, struct ifnet *ifp) BPFD_LOCK_ASSERT(d); MAC_IFNET_LOCK(ifp); - MAC_CHECK(bpfdesc_check_receive, d, d->bd_label, ifp, ifp->if_label); + MAC_CHECK_NOSLEEP(bpfdesc_check_receive, d, d->bd_label, ifp, + ifp->if_label); MAC_CHECK_PROBE2(bpfdesc_check_receive, error, d, ifp); MAC_IFNET_UNLOCK(ifp); @@ -361,7 +365,8 @@ mac_ifnet_check_transmit(struct ifnet *ifp, struct mbuf *m) label = mac_mbuf_to_label(m); MAC_IFNET_LOCK(ifp); - MAC_CHECK(ifnet_check_transmit, ifp, ifp->if_label, m, label); + MAC_CHECK_NOSLEEP(ifnet_check_transmit, ifp, ifp->if_label, m, + label); MAC_CHECK_PROBE2(ifnet_check_transmit, error, ifp, m); MAC_IFNET_UNLOCK(ifp); @@ -458,14 +463,16 @@ mac_ifnet_ioctl_set(struct ucred *cred, struct ifreq *ifr, struct ifnet *ifp) } MAC_IFNET_LOCK(ifp); - MAC_CHECK(ifnet_check_relabel, cred, ifp, ifp->if_label, intlabel); + MAC_CHECK_NOSLEEP(ifnet_check_relabel, cred, ifp, ifp->if_label, + intlabel); if (error) { MAC_IFNET_UNLOCK(ifp); mac_ifnet_label_free(intlabel); return (error); } - MAC_PERFORM(ifnet_relabel, cred, ifp, ifp->if_label, intlabel); + MAC_PERFORM_NOSLEEP(ifnet_relabel, cred, ifp, ifp->if_label, + intlabel); MAC_IFNET_UNLOCK(ifp); mac_ifnet_label_free(intlabel); |