xref: /freebsd/sys/kern/kern_osd.c (revision 4ffa7e126ed0081b804bda6fb71a60acf49dabda)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/sysctl.h>
33 #include <sys/errno.h>
34 #include <sys/jail.h>
35 #include <sys/malloc.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/rmlock.h>
39 #include <sys/sx.h>
40 #include <sys/queue.h>
41 #include <sys/proc.h>
42 #include <sys/osd.h>
43 
44 /* OSD (Object Specific Data) */
45 
46 /*
47  * Lock key:
48  *  (m) osd_module_lock
49  *  (o) osd_object_lock
50  *  (l) osd_list_lock
51  */
52 struct osd_master {
53 	struct sx		 osd_module_lock;
54 	struct rmlock		 osd_object_lock;
55 	struct mtx		 osd_list_lock;
56 	LIST_HEAD(, osd)	 osd_list;		/* (l) */
57 	osd_destructor_t	*osd_destructors;	/* (o) */
58 	osd_method_t		*osd_methods;		/* (m) */
59 	u_int			 osd_ntslots;		/* (m) */
60 	const u_int		 osd_nmethods;
61 };
62 
63 static MALLOC_DEFINE(M_OSD, "osd", "Object Specific Data");
64 
65 static int osd_debug = 0;
66 SYSCTL_INT(_debug, OID_AUTO, osd, CTLFLAG_RWTUN, &osd_debug, 0, "OSD debug level");
67 
68 #define	OSD_DEBUG(...)	do {						\
69 	if (osd_debug) {						\
70 		printf("OSD (%s:%u): ", __func__, __LINE__);		\
71 		printf(__VA_ARGS__);					\
72 		printf("\n");						\
73 	}								\
74 } while (0)
75 
76 #define	OSD_METHOD(osdm, slot, method)	\
77     ((osdm).osd_methods[((slot) - 1) * (osdm).osd_nmethods + (method)])
78 
79 static void do_osd_del(u_int type, struct osd *osd, u_int slot,
80     int list_locked);
81 
82 /*
83  * List of objects with OSD.
84  */
85 struct osd_master osdm[OSD_LAST + 1] = {
86 	[OSD_JAIL] = { .osd_nmethods = PR_MAXMETHOD },
87 };
88 
89 static void
osd_default_destructor(void * value __unused)90 osd_default_destructor(void *value __unused)
91 {
92 	/* Do nothing. */
93 }
94 
95 int
osd_register(u_int type,osd_destructor_t destructor,const osd_method_t * methods)96 osd_register(u_int type, osd_destructor_t destructor, const osd_method_t *methods)
97 {
98 	void *newptr;
99 	u_int i, m;
100 
101 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
102 
103 	/*
104 	 * If no destructor is given, use default one. We need to use some
105 	 * destructor, because NULL destructor means unused slot.
106 	 */
107 	if (destructor == NULL)
108 		destructor = osd_default_destructor;
109 
110 	sx_xlock(&osdm[type].osd_module_lock);
111 	/*
112 	 * First, we try to find unused slot.
113 	 */
114 	for (i = 0; i < osdm[type].osd_ntslots; i++) {
115 		if (osdm[type].osd_destructors[i] == NULL) {
116 			OSD_DEBUG("Unused slot found (type=%u, slot=%u).",
117 			    type, i + 1);
118 			break;
119 		}
120 	}
121 	/*
122 	 * If no unused slot was found, allocate one.
123 	 */
124 	if (i == osdm[type].osd_ntslots) {
125 		osdm[type].osd_ntslots++;
126 		if (osdm[type].osd_nmethods != 0)
127 			osdm[type].osd_methods = realloc(osdm[type].osd_methods,
128 			    sizeof(osd_method_t) * osdm[type].osd_ntslots *
129 			    osdm[type].osd_nmethods, M_OSD, M_WAITOK);
130 		newptr = malloc(sizeof(osd_destructor_t) *
131 		    osdm[type].osd_ntslots, M_OSD, M_WAITOK);
132 		rm_wlock(&osdm[type].osd_object_lock);
133 		bcopy(osdm[type].osd_destructors, newptr,
134 		    sizeof(osd_destructor_t) * i);
135 		free(osdm[type].osd_destructors, M_OSD);
136 		osdm[type].osd_destructors = newptr;
137 		rm_wunlock(&osdm[type].osd_object_lock);
138 		OSD_DEBUG("New slot allocated (type=%u, slot=%u).",
139 		    type, i + 1);
140 	}
141 
142 	osdm[type].osd_destructors[i] = destructor;
143 	if (osdm[type].osd_nmethods != 0) {
144 		for (m = 0; m < osdm[type].osd_nmethods; m++)
145 			OSD_METHOD(osdm[type], i + 1, m) =
146 			    methods != NULL ? methods[m] : NULL;
147 	}
148 	sx_xunlock(&osdm[type].osd_module_lock);
149 	return (i + 1);
150 }
151 
152 void
osd_deregister(u_int type,u_int slot)153 osd_deregister(u_int type, u_int slot)
154 {
155 	struct osd *osd, *tosd;
156 
157 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
158 	KASSERT(slot > 0, ("Invalid slot."));
159 
160 	sx_xlock(&osdm[type].osd_module_lock);
161 	rm_wlock(&osdm[type].osd_object_lock);
162 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
163 
164 	/*
165 	 * Free all OSD for the given slot.
166 	 */
167 	mtx_lock(&osdm[type].osd_list_lock);
168 	LIST_FOREACH_SAFE(osd, &osdm[type].osd_list, osd_next, tosd)
169 		do_osd_del(type, osd, slot, 1);
170 	mtx_unlock(&osdm[type].osd_list_lock);
171 
172 	/*
173 	 * Set destructor to NULL to free the slot.  We don't bother actually
174 	 * freeing any memory here because we'll gracefully reuse any freed
175 	 * slots, and reallocating the arrays as a smaller chunk of memory isn't
176 	 * actually guaranteed to succeed.  As such, we'll err on the side of
177 	 * caution and just leave it be since these are generally modestly sized
178 	 * allocations.
179 	 */
180 	osdm[type].osd_destructors[slot - 1] = NULL;
181 	OSD_DEBUG("Slot deregistration (type=%u, slot=%u).", type, slot);
182 
183 #ifdef INVARIANTS
184 	/*
185 	 * We trash the slot's osd_methods upon deregistration so that we take
186 	 * a fault if something calls them, rather than potentially
187 	 * inadvertently executing some arbitrary function if another module's
188 	 * been mapped over the one that deregistered.
189 	 */
190 	for (u_int method = 0; method < osdm[type].osd_nmethods; method++) {
191 		OSD_METHOD(osdm[type], slot, method) = (void *)0xdeadc0de;
192 	}
193 #endif
194 
195 	rm_wunlock(&osdm[type].osd_object_lock);
196 	sx_xunlock(&osdm[type].osd_module_lock);
197 }
198 
199 int
osd_set(u_int type,struct osd * osd,u_int slot,void * value)200 osd_set(u_int type, struct osd *osd, u_int slot, void *value)
201 {
202 
203 	return (osd_set_reserved(type, osd, slot, NULL, value));
204 }
205 
206 void **
osd_reserve(u_int slot)207 osd_reserve(u_int slot)
208 {
209 
210 	KASSERT(slot > 0, ("Invalid slot."));
211 
212 	OSD_DEBUG("Reserving slot array (slot=%u).", slot);
213 	return (malloc(sizeof(void *) * slot, M_OSD, M_WAITOK | M_ZERO));
214 }
215 
216 int
osd_set_reserved(u_int type,struct osd * osd,u_int slot,void ** rsv,void * value)217 osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv,
218     void *value)
219 {
220 	struct rm_priotracker tracker;
221 
222 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
223 	KASSERT(slot > 0, ("Invalid slot."));
224 
225 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
226 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
227 
228 	if (slot > osd->osd_nslots) {
229 		void **newptr;
230 
231 		if (value == NULL) {
232 			OSD_DEBUG(
233 			    "Not allocating null slot (type=%u, slot=%u).",
234 			    type, slot);
235 			rm_runlock(&osdm[type].osd_object_lock, &tracker);
236 			if (rsv)
237 				osd_free_reserved(rsv);
238 			return (0);
239 		}
240 
241 		/*
242 		 * Too few slots allocated here, so we need to extend or create
243 		 * the array.
244 		 */
245 		if (rsv) {
246 			/*
247 			 * Use the reserve passed in (assumed to be
248 			 * the right size).
249 			 */
250 			newptr = rsv;
251 			if (osd->osd_nslots != 0) {
252 				memcpy(newptr, osd->osd_slots,
253 				    sizeof(void *) * osd->osd_nslots);
254 				free(osd->osd_slots, M_OSD);
255 			}
256 		} else {
257 			newptr = realloc(osd->osd_slots, sizeof(void *) * slot,
258 			    M_OSD, M_NOWAIT | M_ZERO);
259 			if (newptr == NULL) {
260 				rm_runlock(&osdm[type].osd_object_lock,
261 				    &tracker);
262 				return (ENOMEM);
263 			}
264 		}
265 		if (osd->osd_nslots == 0) {
266 			/*
267 			 * First OSD for this object, so we need to put it
268 			 * onto the list.
269 			 */
270 			mtx_lock(&osdm[type].osd_list_lock);
271 			LIST_INSERT_HEAD(&osdm[type].osd_list, osd, osd_next);
272 			mtx_unlock(&osdm[type].osd_list_lock);
273 			OSD_DEBUG("Setting first slot (type=%u).", type);
274 		} else
275 			OSD_DEBUG("Growing slots array (type=%u).", type);
276 		osd->osd_slots = newptr;
277 		osd->osd_nslots = slot;
278 	} else if (rsv)
279 		osd_free_reserved(rsv);
280 	OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type,
281 	    slot, value);
282 	osd->osd_slots[slot - 1] = value;
283 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
284 	return (0);
285 }
286 
287 void
osd_free_reserved(void ** rsv)288 osd_free_reserved(void **rsv)
289 {
290 
291 	OSD_DEBUG("Discarding reserved slot array.");
292 	free(rsv, M_OSD);
293 }
294 
295 void *
osd_get_unlocked(u_int type,struct osd * osd,u_int slot)296 osd_get_unlocked(u_int type, struct osd *osd, u_int slot)
297 {
298 	void *value;
299 
300 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
301 
302 	if (slot > osd->osd_nslots) {
303 		value = NULL;
304 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
305 	} else {
306 		value = atomic_load_ptr(&osd->osd_slots[slot - 1]);
307 		OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).",
308 		    type, slot, value);
309 	}
310 	return (value);
311 }
312 
313 void *
osd_get(u_int type,struct osd * osd,u_int slot)314 osd_get(u_int type, struct osd *osd, u_int slot)
315 {
316 	struct rm_priotracker tracker;
317 	void *value;
318 
319 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
320 	KASSERT(slot > 0, ("Invalid slot."));
321 
322 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
323 	value = osd_get_unlocked(type, osd, slot);
324 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
325 	return (value);
326 }
327 
328 void
osd_del(u_int type,struct osd * osd,u_int slot)329 osd_del(u_int type, struct osd *osd, u_int slot)
330 {
331 	struct rm_priotracker tracker;
332 
333 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
334 	do_osd_del(type, osd, slot, 0);
335 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
336 }
337 
338 static void
do_osd_del(u_int type,struct osd * osd,u_int slot,int list_locked)339 do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked)
340 {
341 	int i;
342 
343 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
344 	KASSERT(slot > 0, ("Invalid slot."));
345 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
346 
347 	OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot);
348 
349 	if (slot > osd->osd_nslots) {
350 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
351 		return;
352 	}
353 	if (osd->osd_slots[slot - 1] != NULL) {
354 		osdm[type].osd_destructors[slot - 1](osd->osd_slots[slot - 1]);
355 		osd->osd_slots[slot - 1] = NULL;
356 	}
357 	for (i = osd->osd_nslots - 1; i >= 0; i--) {
358 		if (osd->osd_slots[i] != NULL) {
359 			OSD_DEBUG("Slot still has a value (type=%u, slot=%u).",
360 			    type, i + 1);
361 			break;
362 		}
363 	}
364 	if (i == -1) {
365 		/* No values left for this object. */
366 		OSD_DEBUG("No more slots left (type=%u).", type);
367 		if (!list_locked)
368 			mtx_lock(&osdm[type].osd_list_lock);
369 		LIST_REMOVE(osd, osd_next);
370 		if (!list_locked)
371 			mtx_unlock(&osdm[type].osd_list_lock);
372 		free(osd->osd_slots, M_OSD);
373 		osd->osd_slots = NULL;
374 		osd->osd_nslots = 0;
375 	} else if (slot == osd->osd_nslots) {
376 		/* This was the last slot. */
377 		osd->osd_slots = realloc(osd->osd_slots,
378 		    sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO);
379 		/*
380 		 * We always reallocate to smaller size, so we assume it will
381 		 * always succeed.
382 		 */
383 		KASSERT(osd->osd_slots != NULL, ("realloc() failed"));
384 		osd->osd_nslots = i + 1;
385 		OSD_DEBUG("Reducing slots array to %u (type=%u).",
386 		    osd->osd_nslots, type);
387 	}
388 }
389 
390 int
osd_call(u_int type,u_int method,void * obj,void * data)391 osd_call(u_int type, u_int method, void *obj, void *data)
392 {
393 	osd_method_t methodfun;
394 	int error, i;
395 
396 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
397 	KASSERT(method < osdm[type].osd_nmethods, ("Invalid method."));
398 
399 	/*
400 	 * Call this method for every slot that defines it, stopping if an
401 	 * error is encountered.
402 	 */
403 	error = 0;
404 	sx_slock(&osdm[type].osd_module_lock);
405 	for (i = 0; i < osdm[type].osd_ntslots; i++) {
406 		/* Hole in the slot map; avoid dereferencing. */
407 		if (osdm[type].osd_destructors[i] == NULL)
408 			continue;
409 		methodfun = OSD_METHOD(osdm[type], i + 1, method);
410 		if (methodfun != NULL && (error = methodfun(obj, data)) != 0)
411 			break;
412 	}
413 	sx_sunlock(&osdm[type].osd_module_lock);
414 	return (error);
415 }
416 
417 void
osd_exit(u_int type,struct osd * osd)418 osd_exit(u_int type, struct osd *osd)
419 {
420 	struct rm_priotracker tracker;
421 	u_int i;
422 
423 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
424 
425 	if (osd->osd_nslots == 0) {
426 		KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots."));
427 		/* No OSD attached, just leave. */
428 		return;
429 	}
430 
431 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
432 	for (i = 1; i <= osd->osd_nslots; i++) {
433 		if (osdm[type].osd_destructors[i - 1] != NULL)
434 			do_osd_del(type, osd, i, 0);
435 		else
436 			OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i);
437 	}
438 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
439 	OSD_DEBUG("Object exit (type=%u).", type);
440 }
441 
442 static void
osd_init(void * arg __unused)443 osd_init(void *arg __unused)
444 {
445 	u_int i;
446 
447 	for (i = OSD_FIRST; i <= OSD_LAST; i++) {
448 		sx_init(&osdm[i].osd_module_lock, "osd_module");
449 		rm_init(&osdm[i].osd_object_lock, "osd_object");
450 		mtx_init(&osdm[i].osd_list_lock, "osd_list", NULL, MTX_DEF);
451 		LIST_INIT(&osdm[i].osd_list);
452 		osdm[i].osd_destructors = NULL;
453 		osdm[i].osd_ntslots = 0;
454 		osdm[i].osd_methods = NULL;
455 	}
456 }
457 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL);
458