git: 56b274213024 - main - Add function to OSD to get values without taking the lock.

From: Stephen J. Kiernan <stevek_at_FreeBSD.org>
Date: Wed, 22 May 2024 19:57:50 UTC
The branch main has been updated by stevek:

URL: https://cgit.FreeBSD.org/src/commit/?id=56b274213024957cea378d27393304ae5cc8ded9

commit 56b274213024957cea378d27393304ae5cc8ded9
Author:     Stephen J. Kiernan <stevek@FreeBSD.org>
AuthorDate: 2024-04-04 00:12:57 +0000
Commit:     Stephen J. Kiernan <stevek@FreeBSD.org>
CommitDate: 2024-05-22 19:55:48 +0000

    Add function to OSD to get values without taking the lock.
    
    There are some cases of OSD use where the value is only initialized once
    at a point where successive access of the value can be done so safely
    without the need to take the lock.
    
    Reviewed by:    markj
    Obtained from:  Juniper Networks, Inc.
    Differential Revision:  https://reviews.freebsd.org/D44631
---
 sys/kern/kern_osd.c | 23 ++++++++++++++++-------
 sys/sys/osd.h       |  5 +++++
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/sys/kern/kern_osd.c b/sys/kern/kern_osd.c
index 38db612d34d9..dcd80a948ea7 100644
--- a/sys/kern/kern_osd.c
+++ b/sys/kern/kern_osd.c
@@ -278,25 +278,34 @@ osd_free_reserved(void **rsv)
 }
 
 void *
-osd_get(u_int type, struct osd *osd, u_int slot)
+osd_get_unlocked(u_int type, struct osd *osd, u_int slot)
 {
-	struct rm_priotracker tracker;
 	void *value;
 
-	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
-	KASSERT(slot > 0, ("Invalid slot."));
-
-	rm_rlock(&osdm[type].osd_object_lock, &tracker);
 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
 
 	if (slot > osd->osd_nslots) {
 		value = NULL;
 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
 	} else {
-		value = osd->osd_slots[slot - 1];
+		value = atomic_load_ptr(&osd->osd_slots[slot - 1]);
 		OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).",
 		    type, slot, value);
 	}
+	return (value);
+}
+
+void *
+osd_get(u_int type, struct osd *osd, u_int slot)
+{
+	struct rm_priotracker tracker;
+	void *value;
+
+	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
+	KASSERT(slot > 0, ("Invalid slot."));
+
+	rm_rlock(&osdm[type].osd_object_lock, &tracker);
+	value = osd_get_unlocked(type, osd, slot);
 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
 	return (value);
 }
diff --git a/sys/sys/osd.h b/sys/sys/osd.h
index 1c58c4fff8de..498cad5064f9 100644
--- a/sys/sys/osd.h
+++ b/sys/sys/osd.h
@@ -64,6 +64,7 @@ int osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv,
     void *value);
 void osd_free_reserved(void **rsv);
 void *osd_get(u_int type, struct osd *osd, u_int slot);
+void *osd_get_unlocked(u_int type, struct osd *osd, u_int slot);
 void osd_del(u_int type, struct osd *osd, u_int slot);
 int osd_call(u_int type, u_int method, void *obj, void *data);
 
@@ -79,6 +80,8 @@ void osd_exit(u_int type, struct osd *osd);
 	osd_set_reserved(OSD_THREAD, &(td)->td_osd, (slot), (rsv), (value))
 #define	osd_thread_get(td, slot)					\
 	osd_get(OSD_THREAD, &(td)->td_osd, (slot))
+#define	osd_thread_get_unlocked(td, slot)				\
+	osd_get_unlocked(OSD_THREAD, &(td)->td_osd, (slot))
 #define	osd_thread_del(td, slot)	do {				\
 	KASSERT((td) == curthread, ("Not curthread."));			\
 	osd_del(OSD_THREAD, &(td)->td_osd, (slot));			\
@@ -98,6 +101,8 @@ void osd_exit(u_int type, struct osd *osd);
 	osd_set_reserved(OSD_JAIL, &(pr)->pr_osd, (slot), (rsv), (value))
 #define	osd_jail_get(pr, slot)						\
 	osd_get(OSD_JAIL, &(pr)->pr_osd, (slot))
+#define	osd_jail_get_unlocked(pr, slot)					\
+	osd_get_unlocked(OSD_JAIL, &(pr)->pr_osd, (slot))
 #define	osd_jail_del(pr, slot)						\
 	osd_del(OSD_JAIL, &(pr)->pr_osd, (slot))
 #define	osd_jail_call(pr, method, data)					\