From owner-svn-src-head@freebsd.org Sun Oct 20 22:55:47 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E27A216D423; Sun, 20 Oct 2019 22:55:47 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46xFVW5m26z4Stc; Sun, 20 Oct 2019 22:55:47 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id AA2CE1AEB4; Sun, 20 Oct 2019 22:55:47 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x9KMtlT7086928; Sun, 20 Oct 2019 22:55:47 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x9KMtlki086927; Sun, 20 Oct 2019 22:55:47 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201910202255.x9KMtlki086927@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Sun, 20 Oct 2019 22:55:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r353786 - head/sys/net X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/sys/net X-SVN-Commit-Revision: 353786 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Oct 2019 22:55:47 -0000 Author: kevans Date: Sun Oct 20 22:55:47 2019 New Revision: 353786 URL: https://svnweb.freebsd.org/changeset/base/353786 Log: tuntap(4): use cdevpriv w/ dtor for last close instead of d_close cdevpriv dtors will be called when the reference count on the associated struct file drops to 0, while d_close can be unreliable for cleaning up state at "last close" for a number of reasons. As far as tunclose/tundtor is concerned the difference is minimal, so make the switch. Modified: head/sys/net/if_tuntap.c Modified: head/sys/net/if_tuntap.c ============================================================================== --- head/sys/net/if_tuntap.c Sun Oct 20 22:39:40 2019 (r353785) +++ head/sys/net/if_tuntap.c Sun Oct 20 22:55:47 2019 (r353786) @@ -220,6 +220,7 @@ static int tuntap_name2info(const char *name, int *uni static void tunclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev); static void tuncreate(struct cdev *dev); +static void tundtor(void *data); static void tunrename(void *arg, struct ifnet *ifp); static int tunifioctl(struct ifnet *, u_long, caddr_t); static void tuninit(struct ifnet *); @@ -238,7 +239,6 @@ static int tun_clone_destroy(struct if_clone *, struct static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen); static d_open_t tunopen; -static d_close_t tunclose; static d_read_t tunread; static d_write_t tunwrite; static d_ioctl_t tunioctl; @@ -278,7 +278,6 @@ static struct tuntap_driver { .d_version = D_VERSION, .d_flags = D_NEEDMINOR, .d_open = tunopen, - .d_close = tunclose, .d_read = tunread, .d_write = tunwrite, .d_ioctl = tunioctl, @@ -296,7 +295,6 @@ static struct tuntap_driver { .d_version = D_VERSION, .d_flags = D_NEEDMINOR, .d_open = tunopen, - .d_close = tunclose, .d_read = tunread, .d_write = tunwrite, .d_ioctl = tunioctl, @@ -314,7 +312,6 @@ static struct tuntap_driver { .d_version = D_VERSION, .d_flags = D_NEEDMINOR, .d_open = tunopen, - .d_close = tunclose, .d_read = tunread, .d_write = tunwrite, .d_ioctl = tunioctl, @@ -1100,24 +1097,33 @@ tunopen(struct cdev *dev, int flag, int mode, struct t if_link_state_change(ifp, LINK_STATE_UP); TUNDEBUG(ifp, "open\n"); TUN_UNLOCK(tp); + + /* + * This can fail with either ENOENT or EBUSY. This is in the middle of + * d_open, so ENOENT should not be possible. EBUSY is possible, but + * the only cdevpriv dtor being set will be tundtor and the softc being + * passed is constant for a given cdev. We ignore the possible error + * because of this as either "unlikely" or "not actually a problem." + */ + (void)devfs_set_cdevpriv(tp, tundtor); CURVNET_RESTORE(); return (0); } /* - * tunclose - close the device - mark i/f down & delete + * tundtor - tear down the device - mark i/f down & delete * routing info */ -static int -tunclose(struct cdev *dev, int foo, int bar, struct thread *td) +static void +tundtor(void *data) { struct proc *p; struct tuntap_softc *tp; struct ifnet *ifp; bool l2tun; - p = td->td_proc; - tp = dev->si_drv1; + tp = data; + p = curproc; ifp = TUN2IFP(tp); TUN_LOCK(tp); @@ -1133,7 +1139,7 @@ tunclose(struct cdev *dev, int foo, int bar, struct th if (p->p_pid != tp->tun_pid) { log(LOG_INFO, "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n", - p->p_pid, p->p_comm, dev->si_name); + p->p_pid, p->p_comm, tp->tun_dev->si_name); } /* @@ -1193,7 +1199,6 @@ out: tun_unbusy_locked(tp); TUN_UNLOCK(tp); - return (0); } static void