From nobody Thu May 05 23:42:31 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 6E4DA1AC1E8C; Thu, 5 May 2022 23:42:33 +0000 (UTC) (envelope-from git@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KvVc80Hsgz4XhJ; Thu, 5 May 2022 23:42:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651794152; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Vev+Y/qjuvNvokAMp8bymZlvfKtfUVyBrBFaKJPrKL4=; b=TJVAclrfJ5q/ztIX2blizdcaq93q+FL/TaTA958qCRYSEH1lN+fGpLLHM0ta82DWxzh483 fGs5bIhjiQWMFDliliJyrww7j8FgKOxEIYjWV3Gv7onpbtV4p73pJ51eRxD8skOLlQfgbo x3ZITNiBg8sdzPzah3z+usu3kv6Purjy9ldWMG0rPoP5OiDtvGY8vuzAB10X1o8Qj9iNFk FXcxR22iTBUhWqgvT7GjOf5711j0ziO9Qb/Ll1q11fK2icKPVp9IO8twGKMZm9RhsiBUsk 9iwHBMQk3AQT0eITd9YMaoNaRJQtY5JIngn8R9v30TxA4m9YGRl/TFGQmjtZRQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 623DB1150A; Thu, 5 May 2022 23:42:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 245NgV1R055732; Thu, 5 May 2022 23:42:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 245NgVgZ055731; Thu, 5 May 2022 23:42:31 GMT (envelope-from git) Date: Thu, 5 May 2022 23:42:31 GMT Message-Id: <202205052342.245NgVgZ055731@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 51f481235c29 - main - pbio: Add locking and remove D_NEEDGIANT. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 51f481235c29fb38b92fb08a830385e39df80093 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651794152; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Vev+Y/qjuvNvokAMp8bymZlvfKtfUVyBrBFaKJPrKL4=; b=qF3b/9qnRGlDGcpLkbW1cXFFI4oA+/tTHXwZI8cLVRfx+Vb2pXaa0ZwSyYO/Flw/1QNmVJ 11QsimyMHhEuCPq1wjA0aDTk0zKg2uW0cuGdMa3UUb5ll0ljzbDOxhBND1olxTC2cVZBs0 UDOtsUNR8HkN6dKHUf9+Yh04mXrlpFETuoG20McsxZLmpo4Rysz0oiydWEuDlTty1yYePa u8pmatFaJfzY0KwQkgi9glsYUrFAU2yCrhhBUHELT0Yk1OI902zotLB738qU0DcN+JKnJH 2uXSVgCIDZJCW2/xKIvV/e3+5AU07R7wU9Hceb+VDo+twBHG/x9AKW3V1lUesg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1651794152; a=rsa-sha256; cv=none; b=elnA7wNugSwHaPDS46+lg8QAG3i0TAZW8lfiOYRRlZ7GeWcR4dzHBLYD0mvjgxsXQyQrsF bzELb9UAVmirwDOP05QyQ0CptKOx74h7NbXWDUIE/jVrS+0TK+bX6ZdmRYIzV7b1Z2D10q TkLS1l/6B6qeDTMoQeMfcL5MufgN/dlINKAK1jYhIpuuRW8hc6ifRXt3aKg5eRGIa8qPdw 21gr6rsy12c5fHK0RT9gbd0uL4i4dRT+UI9jVCdYeZD3+JBR6sq22nXl5bX0DCSfX2dN+p vgULy3lCymvrcNId4nRF0afoWQC21+LJeteiIRyZzx9VOm/bqtWvCwzfEYZE1w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=51f481235c29fb38b92fb08a830385e39df80093 commit 51f481235c29fb38b92fb08a830385e39df80093 Author: John Baldwin AuthorDate: 2022-05-05 23:40:04 +0000 Commit: John Baldwin CommitDate: 2022-05-05 23:40:04 +0000 pbio: Add locking and remove D_NEEDGIANT. Use a sx lock to permit uiomove directly into/out of the the per-port buffers. In addition, the sx lock provides a stronger guarantee that I think this driver wants which is to single-thread read and write calls even while paused. Finally, replace tsleep with dummy wait channels with calls to pause_sig to more clearly communicate the intent. Differential Revision: https://reviews.freebsd.org/D35081 --- sys/dev/pbio/pbio.c | 54 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/sys/dev/pbio/pbio.c b/sys/dev/pbio/pbio.c index 516ec3a73408..0fd841cd5720 100644 --- a/sys/dev/pbio/pbio.c +++ b/sys/dev/pbio/pbio.c @@ -49,6 +49,7 @@ __FBSDID("$FreeBSD$"); #include /* pbio IOCTL definitions */ #include #include +#include #include /* Function prototypes (these should all be static) */ @@ -84,11 +85,8 @@ static char *port_names[] = {"a", "b", "ch", "cl"}; #define pbio_addr(dev) ((dev)->si_drv1) -#define PBIOPRI ((PZERO + 5) | PCATCH) - static struct cdevsw pbio_cdevsw = { .d_version = D_VERSION, - .d_flags = D_NEEDGIANT, .d_open = pbioopen, .d_read = pbioread, .d_write = pbiowrite, @@ -117,6 +115,7 @@ struct pbio_softc { int iomode; /* Virtualized I/O mode port value */ /* The real port is write-only */ struct resource *res; + struct sx lock; }; typedef struct pbio_softc *sc_p; @@ -230,6 +229,7 @@ pbioattach (device_t dev) */ sc->iomode = 0x9b; /* All ports to input */ + sx_init(&sc->lock, "pbio"); for (i = 0; i < PBIO_NPORTS; i++) { make_dev_args_init(&args); args.mda_devsw = &pbio_cdevsw; @@ -249,10 +249,12 @@ pbioioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) { struct pbio_softc *scp; - int port; + int error, port; + error = 0; port = PORT(dev); scp = pbio_addr(dev); + sx_xlock(&scp->lock); switch (cmd) { case PBIO_SETDIFF: scp->pd[port].diff = *(int *)data; @@ -273,16 +275,17 @@ pbioioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, *(int *)data = scp->pd[port].opace; break; default: - return ENXIO; + error = ENXIO; } - return (0); + sx_xunlock(&scp->lock); + return (error); } static int pbioopen(struct cdev *dev, int oflags, int devtype, struct thread *td) { struct pbio_softc *scp; - int ocfg, port; + int error, ocfg, port; int portbit; /* Port configuration bit */ port = PORT(dev); @@ -297,6 +300,8 @@ pbioopen(struct cdev *dev, int oflags, int devtype, struct thread *td) } ocfg = scp->iomode; + error = 0; + sx_xlock(&scp->lock); if (oflags & FWRITE) /* Writing == output; zero the bit */ pboutb(scp, PBIO_CFG, scp->iomode = (ocfg & (~portbit))); @@ -304,9 +309,10 @@ pbioopen(struct cdev *dev, int oflags, int devtype, struct thread *td) /* Reading == input; set the bit */ pboutb(scp, PBIO_CFG, scp->iomode = (ocfg | portbit)); else - return (EACCES); + error = EACCES; + sx_xunlock(&scp->lock); - return (0); + return (error); } /* @@ -341,8 +347,7 @@ portval(int port, struct pbio_softc *scp, char *val) scp->pd[port].oldval = *val; return (0); } - err = tsleep((caddr_t)&(scp->pd[port].diff), PBIOPRI, - "pbiopl", max(1, scp->pd[port].ipace)); + err = pause_sig("pbiopl", max(1, scp->pd[port].ipace)); if (err == EINTR) return (EINTR); } else @@ -354,26 +359,28 @@ static int pbioread(struct cdev *dev, struct uio *uio, int ioflag) { struct pbio_softc *scp; - int err, i, port, ret, toread; + int err, i, port, toread; char val; port = PORT(dev); scp = pbio_addr(dev); + err = 0; + sx_xlock(&scp->lock); while (uio->uio_resid > 0) { toread = min(uio->uio_resid, PBIO_BUFSIZ); - if ((ret = uiomove(scp->pd[port].buff, toread, uio)) != 0) - return (ret); + if ((err = uiomove(scp->pd[port].buff, toread, uio)) != 0) + break; for (i = 0; i < toread; i++) { if ((err = portval(port, scp, &val)) != 0) - return (err); + break; scp->pd[port].buff[i] = val; if (!scp->pd[port].diff && scp->pd[port].ipace) - tsleep((caddr_t)&(scp->pd[port].ipace), PBIOPRI, - "pbioip", scp->pd[port].ipace); + pause_sig("pbioip", scp->pd[port].ipace); } } - return 0; + sx_xunlock(&scp->lock); + return (err); } static int @@ -386,10 +393,12 @@ pbiowrite(struct cdev *dev, struct uio *uio, int ioflag) port = PORT(dev); scp = pbio_addr(dev); + ret = 0; + sx_xlock(&scp->lock); while (uio->uio_resid > 0) { towrite = min(uio->uio_resid, PBIO_BUFSIZ); if ((ret = uiomove(scp->pd[port].buff, towrite, uio)) != 0) - return (ret); + break; for (i = 0; i < towrite; i++) { val = scp->pd[port].buff[i]; switch (port) { @@ -413,12 +422,11 @@ pbiowrite(struct cdev *dev, struct uio *uio, int ioflag) break; } if (scp->pd[port].opace) - tsleep((caddr_t)&(scp->pd[port].opace), - PBIOPRI, "pbioop", - scp->pd[port].opace); + pause_sig("pbioop", scp->pd[port].opace); } } - return (0); + sx_xunlock(&scp->lock); + return (ret); } static int