git: 48c59140bd7d - stable/13 - bus: Set the current VNET in device_attach()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 07 Nov 2024 14:28:10 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=48c59140bd7d82de26966bbc9723a49de95abd94

commit 48c59140bd7d82de26966bbc9723a49de95abd94
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-10-19 13:03:56 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-11-07 14:26:34 +0000

    bus: Set the current VNET in device_attach()
    
    Some drivers, in particular anything which creates an ifnet during
    attach, need to have the current VNET set, as if_attach_internal() and
    its callees access VNET-global variables.
    
    device_probe_and_attach() handles this, but this is not the only way to
    arrive in DEVICE_ATTACH.  In particular, bus drivers may invoke
    device_attach() directly, as does devctl2's DEV_ENABLE ioctl handler.
    So, set the current VNET in device_attach() instead.
    
    I believe it is always safe to use vnet0, as devctl2 ioctls are not
    permitted within a jail.
    
    PR:             282168
    Reviewed by:    zlei, kevans, bz, imp, glebius
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D47174
    
    (cherry picked from commit f4e35c044c8988b7452cefbdcc417f5fd723e021)
    (cherry picked from commit 2e80ea70b98ef75f8bea9155944e6f093c0fa828)
---
 sys/kern/subr_bus.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
index 06c64ef28fba..9b4a4c65190a 100644
--- a/sys/kern/subr_bus.c
+++ b/sys/kern/subr_bus.c
@@ -35,6 +35,7 @@
 #include <sys/domainset.h>
 #include <sys/eventhandler.h>
 #include <sys/filio.h>
+#include <sys/jail.h>
 #include <sys/lock.h>
 #include <sys/kernel.h>
 #include <sys/kobj.h>
@@ -2984,10 +2985,7 @@ device_probe_and_attach(device_t dev)
 	else if (error != 0)
 		return (error);
 
-	CURVNET_SET_QUIET(vnet0);
-	error = device_attach(dev);
-	CURVNET_RESTORE();
-	return error;
+	return (device_attach(dev));
 }
 
 /**
@@ -3023,6 +3021,10 @@ device_attach(device_t dev)
 		return (ENXIO);
 	}
 
+	KASSERT(IS_DEFAULT_VNET(TD_TO_VNET(curthread)),
+	    ("device_attach: curthread is not in default vnet"));
+	CURVNET_SET_QUIET(TD_TO_VNET(curthread));
+
 	device_sysctl_init(dev);
 	if (!device_is_quiet(dev))
 		device_print_child(dev->parent, dev);
@@ -3037,8 +3039,10 @@ device_attach(device_t dev)
 		device_sysctl_fini(dev);
 		KASSERT(dev->busy == 0, ("attach failed but busy"));
 		dev->state = DS_NOTPRESENT;
+		CURVNET_RESTORE();
 		return (error);
 	}
+	CURVNET_RESTORE();
 	dev->flags |= DF_ATTACHED_ONCE;
 	/* We only need the low bits of this time, but ranges from tens to thousands
 	 * have been seen, so keep 2 bytes' worth.