diff -r f41945fd7fec sys/dev/vnd.c
--- sys/dev/vnd.c	Sun May 29 13:11:17 2022 +0200
+++ sys/dev/vnd.c	Mon May 30 14:24:07 2022 +0200
@@ -164,8 +164,8 @@ struct vndxfer {
 #define	VND_MAXPAGES(vnd)	(1024 * 1024 / PAGE_SIZE)
 
 
-static void	vndclear(struct vnd_softc *, int);
-static int	vnddoclear(struct vnd_softc *, int, int, bool);
+static void	vndclear(struct vnd_softc *);
+static int	vnddoclear(struct vnd_softc *, int, bool);
 static int	vndsetcred(struct vnd_softc *, kauth_cred_t);
 static void	vndthrottle(struct vnd_softc *, struct vnode *);
 static void	vndiodone(struct buf *);
@@ -235,7 +235,6 @@ CFATTACH_DECL3_NEW(vnd, sizeof(struct vn
     vnd_match, vnd_attach, vnd_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
 
 static struct vnd_softc	*vnd_spawn(int);
-static int	vnd_destroy(device_t);
 
 static const struct	dkdriver vnddkdriver = {
 	.d_strategy = vndstrategy,
@@ -278,19 +277,32 @@ vnd_attach(device_t parent, device_t sel
 static int
 vnd_detach(device_t self, int flags)
 {
+	int bmaj, cmaj, mn;
 	int error;
 	struct vnd_softc *sc = device_private(self);
+	cfdata_t cf = device_cfdata(self);
 
 	if (sc->sc_flags & VNF_INITED) {
-		error = vnddoclear(sc, 0, -1, (flags & DETACH_FORCE) != 0);
+		error = vnddoclear(sc, 0, (flags & DETACH_FORCE) != 0);
 		if (error != 0)
 			return error;
 	}
 
+	/* locate the major number */
+	bmaj = bdevsw_lookup_major(&vnd_bdevsw);
+	cmaj = cdevsw_lookup_major(&vnd_cdevsw);
+
+	/* Nuke the vnodes for any open instances */
+	mn = DISKMINOR(device_unit(sc->sc_dev), 0);
+	vdevgone(bmaj, mn, mn + MAXPARTITIONS - 1, VBLK);
+	vdevgone(cmaj, mn, mn + MAXPARTITIONS - 1, VCHR);
+
 	pmf_device_deregister(self);
 	bufq_free(sc->sc_tab);
 	disk_destroy(&sc->sc_dkdev);
 
+	free(cf, M_DEVBUF);
+
 	return 0;
 }
 
@@ -309,20 +321,6 @@ vnd_spawn(int unit)
 }
 
 static int
-vnd_destroy(device_t dev)
-{
-	int error;
-	cfdata_t cf;
-
-	cf = device_cfdata(dev);
-	error = config_detach(dev, DETACH_QUIET);
-	if (error)
-		return error;
-	free(cf, M_DEVBUF);
-	return 0;
-}
-
-static int
 vndopen(dev_t dev, int flags, int mode, struct lwp *l)
 {
 	int unit = vndunit(dev);
@@ -464,14 +462,6 @@ vndclose(dev_t dev, int flags, int mode,
 
 	vndunlock(sc);
 
-	if ((sc->sc_flags & VNF_INITED) == 0) {
-		if ((error = vnd_destroy(sc->sc_dev)) != 0) {
-			aprint_error_dev(sc->sc_dev,
-			    "unable to detach instance\n");
-			return error;
-		}
-	}
-
 	return 0;
 }
 
@@ -1068,7 +1058,7 @@ vnd_cget(struct lwp *l, int unit, int *u
 }
 
 static int
-vnddoclear(struct vnd_softc *vnd, int pmask, int minor, bool force)
+vnddoclear(struct vnd_softc *vnd, int pmask, bool force)
 {
 	int error;
 
@@ -1097,7 +1087,7 @@ vnddoclear(struct vnd_softc *vnd, int pm
 	 */
 	vnd->sc_flags |= VNF_CLEARING;
 	vndunlock(vnd);
-	vndclear(vnd, minor);
+	vndclear(vnd);
 #ifdef DEBUG
 	if (vnddebug & VDB_INIT)
 		printf("%s: CLRed\n", __func__);
@@ -1566,7 +1556,7 @@ unlock_and_exit:
 		pmask = (1 << part);
 		force = (vio->vnd_flags & VNDIOF_FORCE) != 0;
 
-		if ((error = vnddoclear(vnd, pmask, minor(dev), force)) != 0)
+		if ((error = vnddoclear(vnd, pmask, force)) != 0)
 			return error;
 
 		break;
@@ -1749,28 +1739,16 @@ vndshutdown(void)
 #endif
 
 static void
-vndclear(struct vnd_softc *vnd, int myminor)
+vndclear(struct vnd_softc *vnd)
 {
 	struct vnode *vp = vnd->sc_vp;
 	int fflags = FREAD;
-	int bmaj, cmaj, i, mn;
 	int s;
 
 #ifdef DEBUG
 	if (vnddebug & VDB_FOLLOW)
 		printf("vndclear(%p): vp %p\n", vnd, vp);
 #endif
-	/* locate the major number */
-	bmaj = bdevsw_lookup_major(&vnd_bdevsw);
-	cmaj = cdevsw_lookup_major(&vnd_cdevsw);
-
-	/* Nuke the vnodes for any open instances */
-	for (i = 0; i < MAXPARTITIONS; i++) {
-		mn = DISKMINOR(device_unit(vnd->sc_dev), i);
-		vdevgone(bmaj, mn, mn, VBLK);
-		if (mn != myminor) /* XXX avoid to kill own vnode */
-			vdevgone(cmaj, mn, mn, VCHR);
-	}
 
 	if ((vnd->sc_flags & VNF_READONLY) == 0)
 		fflags |= FWRITE;
