From nobody Sun Sep 1 14:09:45 2024 X-Original-To: dev-commits-src-all@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 4WxYfx5bDbz5VGjM; Sun, 01 Sep 2024 14:09:45 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WxYfx27ygz4Qrh; Sun, 1 Sep 2024 14:09:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725199785; 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=grDxcspXMaSnbwBUgqBkUbLCWmyKwlELf1fblCPrMs4=; b=Uv8gZ1Mt+qHKt2g+VLIYY6rAgwqYWBYzpX0BTnx6CJE+BpILNn/a9acIEV1qhom5K5a3xh qeW/EEqbzeByN177SuRoojCNpnG/CDb9THan16UGuanPKD5/d/ZOHI0jSJiAlhIM36SDms BlbOzE3eqp9LJ65xFABGwV0Y9WSbQKymbH4KBJXI/HGPJr/RApdOx4BK30HK8wHf0wWy6v qy2rmkL2v82RC7DfM8AnCAqZo/ewfjMLOTdQ62tP5QypXKyj26EYWjhrrNUzbhd5MNNkCB TwXNx/7KvEEegNEdRe3+SPjjS7Wnl97CBUjuJy9Dum7feciVerO6ZXJdW4pa3Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725199785; a=rsa-sha256; cv=none; b=Vx7lQguB9OajRpRYAJa6L3Kh4jD4fRJOE2WDK9pWdGcHbTxri5xPCheYRdBnILxB+RTqJX xBi5dnAwaxuR+9qwT9AaBteK0Es7Nch2U5boctdgMjOwLbNaUkhZIzc91sEX+3jhjxnXeY 28LrdP/oNcKMLIQ+FgVPjcZWWgMKrEIguX6ev10YLUuJPAGrdx2pIXD14NjmWNdBvSjeAy V0QXIaclhGTcbVqtKHnIXYC43LSVtwcU+HuxhiN9mPw20/Xn1MaF9Z1Qh+As+rrZ+Hx4lL 5vCp2ubq09y6Rmgy47BjtJdWjj5R3pko/kmVnN2f1ZXDgB7NT+W2ObyuJQCOfA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725199785; 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=grDxcspXMaSnbwBUgqBkUbLCWmyKwlELf1fblCPrMs4=; b=OGBcl0uHmbDGXkwH+lmkpa9hZmMRNVw4CpYWHIRjpmBbHdvW3uR/om2tFeWWdbqJjK7sGb dXbieF87eJwoa5OI/u8/tn6HxkXf/2CCoKZh6W13lQiWSDqhQZXJ28jTFDeg5z4tX5GfI1 I0/eV7tDAMJ40qqStISWv7uy+erxQ8mAey5OCkJRaCa1PWHGT0fhZGirRCOeDbpbZc0LJ/ J7zujsTIFwbUrMugwZBAThvFLCh5JDDIWihmQkxXBWTVNBslfQHtvGRqVkS8U9Y83dnHIG YnVl3gb016SZX1xcqNS+v1yOEFxFgDjX9FGAFIshYCLJKqT8thCvr79AAMrdJA== 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 4WxYfx1lV2zX1s; Sun, 1 Sep 2024 14:09:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 481E9jqK099752; Sun, 1 Sep 2024 14:09:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 481E9jQx099749; Sun, 1 Sep 2024 14:09:45 GMT (envelope-from git) Date: Sun, 1 Sep 2024 14:09:45 GMT Message-Id: <202409011409.481E9jQx099749@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: cef5f43f819c - main - vmm: Use make_dev_s() to create vmm devices List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cef5f43f819cecdbd16f9686e8186d055b19479e Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=cef5f43f819cecdbd16f9686e8186d055b19479e commit cef5f43f819cecdbd16f9686e8186d055b19479e Author: Mark Johnston AuthorDate: 2024-09-01 14:00:39 +0000 Commit: Mark Johnston CommitDate: 2024-09-01 14:09:17 +0000 vmm: Use make_dev_s() to create vmm devices This avoids creating windows where a device file is accessible but the device-specific field is not set. Now that vmmdev_mtx is a sleepable lock, avoid dropping it while creating devices files. This makes it easier to handle races and simplifies some code; for example, the VSC_LINKED flag is no longer needed. Suggested by: jhb Reviewed by: imp, jhb Differential Revision: https://reviews.freebsd.org/D46488 --- sys/dev/vmm/vmm_dev.c | 103 +++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 59 deletions(-) diff --git a/sys/dev/vmm/vmm_dev.c b/sys/dev/vmm/vmm_dev.c index 91d33ccba261..ea2aaace832c 100644 --- a/sys/dev/vmm/vmm_dev.c +++ b/sys/dev/vmm/vmm_dev.c @@ -58,7 +58,6 @@ struct vmmdev_softc { SLIST_HEAD(, devmem_softc) devmem; int flags; }; -#define VSC_LINKED 0x01 static SLIST_HEAD(, vmmdev_softc) head; @@ -750,6 +749,8 @@ vmmdev_destroy(struct vmmdev_softc *sc) struct devmem_softc *dsc; int error __diagused; + KASSERT(sc->cdev == NULL, ("%s: cdev not free", __func__)); + /* * Destroy all cdevs: * @@ -759,7 +760,6 @@ vmmdev_destroy(struct vmmdev_softc *sc) */ SLIST_FOREACH(dsc, &sc->devmem, link) { KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed")); - destroy_dev(dsc->cdev); devmem_destroy(dsc); } @@ -775,21 +775,15 @@ vmmdev_destroy(struct vmmdev_softc *sc) free(dsc, M_VMMDEV); } - if (sc->cdev != NULL) - destroy_dev(sc->cdev); - if (sc->vm != NULL) vm_destroy(sc->vm); if (sc->ucred != NULL) crfree(sc->ucred); - if ((sc->flags & VSC_LINKED) != 0) { - sx_xlock(&vmmdev_mtx); - SLIST_REMOVE(&head, sc, vmmdev_softc, link); - sx_xunlock(&vmmdev_mtx); - } - + sx_xlock(&vmmdev_mtx); + SLIST_REMOVE(&head, sc, vmmdev_softc, link); + sx_xunlock(&vmmdev_mtx); free(sc, M_VMMDEV); } @@ -869,50 +863,43 @@ vmmdev_alloc(struct vm *vm, struct ucred *cred) static int vmmdev_create(const char *name, struct ucred *cred) { + struct make_dev_args mda; struct cdev *cdev; - struct vmmdev_softc *sc, *sc2; + struct vmmdev_softc *sc; struct vm *vm; int error; sx_xlock(&vmmdev_mtx); sc = vmmdev_lookup(name, cred); - sx_xunlock(&vmmdev_mtx); - if (sc != NULL) + if (sc != NULL) { + sx_xunlock(&vmmdev_mtx); return (EEXIST); + } error = vm_create(name, &vm); - if (error != 0) - return (error); - - sc = vmmdev_alloc(vm, cred); - - /* - * Lookup the name again just in case somebody sneaked in when we - * dropped the lock. - */ - sx_xlock(&vmmdev_mtx); - sc2 = vmmdev_lookup(name, cred); - if (sc2 != NULL) { + if (error != 0) { sx_xunlock(&vmmdev_mtx); - vmmdev_destroy(sc); - return (EEXIST); + return (error); } - sc->flags |= VSC_LINKED; + sc = vmmdev_alloc(vm, cred); SLIST_INSERT_HEAD(&head, sc, link); - sx_xunlock(&vmmdev_mtx); - error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, sc->ucred, - UID_ROOT, GID_WHEEL, 0600, "vmm/%s", name); + make_dev_args_init(&mda); + mda.mda_devsw = &vmmdevsw; + mda.mda_cr = sc->ucred; + mda.mda_uid = UID_ROOT; + mda.mda_gid = GID_WHEEL; + mda.mda_mode = 0600; + mda.mda_si_drv1 = sc; + mda.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; + error = make_dev_s(&mda, &cdev, "vmm/%s", name); if (error != 0) { + sx_xunlock(&vmmdev_mtx); vmmdev_destroy(sc); return (error); } - - sx_xlock(&vmmdev_mtx); sc->cdev = cdev; - sc->cdev->si_drv1 = sc; sx_xunlock(&vmmdev_mtx); - return (0); } @@ -1005,39 +992,37 @@ static struct cdevsw devmemsw = { static int devmem_create_cdev(struct vmmdev_softc *sc, int segid, char *devname) { + struct make_dev_args mda; struct devmem_softc *dsc; - struct cdev *cdev; - const char *vmname; int error; - vmname = vm_name(sc->vm); - - error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &devmemsw, sc->ucred, - UID_ROOT, GID_WHEEL, 0600, "vmm.io/%s.%s", vmname, devname); - if (error) - return (error); - - dsc = malloc(sizeof(struct devmem_softc), M_VMMDEV, M_WAITOK | M_ZERO); - sx_xlock(&vmmdev_mtx); - if (sc->cdev == NULL) { - /* virtual machine is being created or destroyed */ - sx_xunlock(&vmmdev_mtx); - free(dsc, M_VMMDEV); - destroy_dev_sched_cb(cdev, NULL, 0); - return (ENODEV); - } + dsc = malloc(sizeof(struct devmem_softc), M_VMMDEV, M_WAITOK | M_ZERO); dsc->segid = segid; dsc->name = devname; - dsc->cdev = cdev; dsc->sc = sc; SLIST_INSERT_HEAD(&sc->devmem, dsc, link); + + make_dev_args_init(&mda); + mda.mda_devsw = &devmemsw; + mda.mda_cr = sc->ucred; + mda.mda_uid = UID_ROOT; + mda.mda_gid = GID_WHEEL; + mda.mda_mode = 0600; + mda.mda_si_drv1 = dsc; + mda.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; + error = make_dev_s(&mda, &dsc->cdev, "vmm.io/%s.%s", vm_name(sc->vm), + devname); + if (error != 0) { + SLIST_REMOVE(&sc->devmem, dsc, devmem_softc, link); + free(dsc->name, M_VMMDEV); + free(dsc, M_VMMDEV); + } + sx_xunlock(&vmmdev_mtx); - /* The 'cdev' is ready for use after 'si_drv1' is initialized */ - cdev->si_drv1 = dsc; - return (0); + return (error); } static void @@ -1045,7 +1030,7 @@ devmem_destroy(void *arg) { struct devmem_softc *dsc = arg; - KASSERT(dsc->cdev, ("%s: devmem cdev already destroyed", __func__)); + destroy_dev(dsc->cdev); dsc->cdev = NULL; dsc->sc = NULL; }