Wed Apr 17 18:52:54 2024 UTC (23d)
tap(4): Just use mutex_enter.

PR kern/58167


(riastradh)
diff -r1.131 -r1.132 src/sys/net/if_tap.c

cvs diff -r1.131 -r1.132 src/sys/net/if_tap.c (expand / switch to unified diff)

--- src/sys/net/if_tap.c 2024/04/17 18:52:39 1.131
+++ src/sys/net/if_tap.c 2024/04/17 18:52:54 1.132
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: if_tap.c,v 1.131 2024/04/17 18:52:39 riastradh Exp $ */ 1/* $NetBSD: if_tap.c,v 1.132 2024/04/17 18:52:54 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation. 4 * Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -23,27 +23,27 @@ @@ -23,27 +23,27 @@
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * tap(4) is a virtual Ethernet interface. It appears as a real Ethernet 30 * tap(4) is a virtual Ethernet interface. It appears as a real Ethernet
31 * device to the system, but can also be accessed by userland through a 31 * device to the system, but can also be accessed by userland through a
32 * character device interface, which allows reading and injecting frames. 32 * character device interface, which allows reading and injecting frames.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.131 2024/04/17 18:52:39 riastradh Exp $"); 36__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.132 2024/04/17 18:52:54 riastradh Exp $");
37 37
38#if defined(_KERNEL_OPT) 38#if defined(_KERNEL_OPT)
39 39
40#include "opt_modular.h" 40#include "opt_modular.h"
41#endif 41#endif
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/atomic.h> 44#include <sys/atomic.h>
45#include <sys/conf.h> 45#include <sys/conf.h>
46#include <sys/cprng.h> 46#include <sys/cprng.h>
47#include <sys/device.h> 47#include <sys/device.h>
48#include <sys/file.h> 48#include <sys/file.h>
49#include <sys/filedesc.h> 49#include <sys/filedesc.h>
@@ -847,33 +847,27 @@ tap_dev_read(int unit, struct uio *uio,  @@ -847,33 +847,27 @@ tap_dev_read(int unit, struct uio *uio,
847 struct ifnet *ifp; 847 struct ifnet *ifp;
848 struct mbuf *m, *n; 848 struct mbuf *m, *n;
849 int error = 0; 849 int error = 0;
850 850
851 if (sc == NULL) 851 if (sc == NULL)
852 return ENXIO; 852 return ENXIO;
853 853
854 getnanotime(&sc->sc_atime); 854 getnanotime(&sc->sc_atime);
855 855
856 ifp = &sc->sc_ec.ec_if; 856 ifp = &sc->sc_ec.ec_if;
857 if ((ifp->if_flags & IFF_UP) == 0) 857 if ((ifp->if_flags & IFF_UP) == 0)
858 return EHOSTDOWN; 858 return EHOSTDOWN;
859 859
860 /* In the TAP_NBIO case, we have to make sure we won't be sleeping */ 860 mutex_enter(&sc->sc_lock);
861 if ((sc->sc_flags & TAP_NBIO) != 0) { 
862 if (!mutex_tryenter(&sc->sc_lock)) 
863 return EWOULDBLOCK; 
864 } else 
865 mutex_enter(&sc->sc_lock); 
866 
867 if (IFQ_IS_EMPTY(&ifp->if_snd)) { 861 if (IFQ_IS_EMPTY(&ifp->if_snd)) {
868 ifp->if_flags &= ~IFF_OACTIVE; 862 ifp->if_flags &= ~IFF_OACTIVE;
869 if (sc->sc_flags & TAP_NBIO) 863 if (sc->sc_flags & TAP_NBIO)
870 error = EWOULDBLOCK; 864 error = EWOULDBLOCK;
871 else 865 else
872 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock); 866 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
873 867
874 if (error != 0) { 868 if (error != 0) {
875 mutex_exit(&sc->sc_lock); 869 mutex_exit(&sc->sc_lock);
876 return error; 870 return error;
877 } 871 }
878 /* The device might have been downed */ 872 /* The device might have been downed */
879 if ((ifp->if_flags & IFF_UP) == 0) { 873 if ((ifp->if_flags & IFF_UP) == 0) {