xref: /dragonfly/sys/kern/subr_rman.c (revision 23265324)
1 /*
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/kern/subr_rman.c,v 1.10.2.1 2001/06/05 08:06:08 imp Exp $
30  * $DragonFly: src/sys/kern/subr_rman.c,v 1.11 2006/12/23 00:35:04 swildner Exp $
31  */
32 
33 /*
34  * The kernel resource manager.  This code is responsible for keeping track
35  * of hardware resources which are apportioned out to various drivers.
36  * It does not actually assign those resources, and it is not expected
37  * that end-device drivers will call into this code directly.  Rather,
38  * the code which implements the buses that those devices are attached to,
39  * and the code which manages CPU resources, will call this code, and the
40  * end-device drivers will make upcalls to that code to actually perform
41  * the allocation.
42  *
43  * There are two sorts of resources managed by this code.  The first is
44  * the more familiar array (RMAN_ARRAY) type; resources in this class
45  * consist of a sequence of individually-allocatable objects which have
46  * been numbered in some well-defined order.  Most of the resources
47  * are of this type, as it is the most familiar.  The second type is
48  * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
49  * resources in which each instance is indistinguishable from every
50  * other instance).  The principal anticipated application of gauges
51  * is in the context of power consumption, where a bus may have a specific
52  * power budget which all attached devices share.  RMAN_GAUGE is not
53  * implemented yet.
54  *
55  * For array resources, we make one simplifying assumption: two clients
56  * sharing the same resource must use the same range of indices.  That
57  * is to say, sharing of overlapping-but-not-identical regions is not
58  * permitted.
59  */
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/lock.h>
65 #include <sys/malloc.h>
66 #include <sys/bus.h>		/* XXX debugging */
67 #include <sys/rman.h>
68 
69 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
70 
71 struct	rman_head rman_head;
72 static	struct lwkt_token rman_tok; /* mutex to protect rman_head */
73 static	int int_rman_activate_resource(struct rman *rm, struct resource *r,
74 				       struct resource **whohas);
75 static	int int_rman_deactivate_resource(struct resource *r);
76 static	int int_rman_release_resource(struct rman *rm, struct resource *r);
77 
78 #define	CIRCLEQ_TERMCOND(var, head)	(var == (void *)&(head))
79 
80 int
81 rman_init(struct rman *rm)
82 {
83 	static int once;
84 	lwkt_tokref ilock;
85 
86 	if (once == 0) {
87 		once = 1;
88 		TAILQ_INIT(&rman_head);
89 		lwkt_token_init(&rman_tok);
90 	}
91 
92 	if (rm->rm_type == RMAN_UNINIT)
93 		panic("rman_init");
94 	if (rm->rm_type == RMAN_GAUGE)
95 		panic("implement RMAN_GAUGE");
96 
97 	CIRCLEQ_INIT(&rm->rm_list);
98 	rm->rm_slock = kmalloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT);
99 	if (rm->rm_slock == NULL)
100 		return ENOMEM;
101 	lwkt_token_init(rm->rm_slock);
102 
103 	lwkt_gettoken(&ilock, &rman_tok);
104 	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
105 	lwkt_reltoken(&ilock);
106 	return 0;
107 }
108 
109 /*
110  * NB: this interface is not robust against programming errors which
111  * add multiple copies of the same region.
112  */
113 int
114 rman_manage_region(struct rman *rm, u_long start, u_long end)
115 {
116 	struct resource *r, *s;
117 	lwkt_tokref ilock;
118 
119 	r = kmalloc(sizeof *r, M_RMAN, M_NOWAIT);
120 	if (r == 0)
121 		return ENOMEM;
122 	bzero(r, sizeof *r);
123 	r->r_sharehead = 0;
124 	r->r_start = start;
125 	r->r_end = end;
126 	r->r_flags = 0;
127 	r->r_dev = 0;
128 	r->r_rm = rm;
129 
130 	lwkt_gettoken(&ilock, rm->rm_slock);
131 	for (s = CIRCLEQ_FIRST(&rm->rm_list);
132 	     !CIRCLEQ_TERMCOND(s, rm->rm_list) && s->r_end < r->r_start;
133 	     s = CIRCLEQ_NEXT(s, r_link))
134 		;
135 
136 	if (CIRCLEQ_TERMCOND(s, rm->rm_list)) {
137 		CIRCLEQ_INSERT_TAIL(&rm->rm_list, r, r_link);
138 	} else {
139 		CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, r, r_link);
140 	}
141 
142 	lwkt_reltoken(&ilock);
143 	return 0;
144 }
145 
146 int
147 rman_fini(struct rman *rm)
148 {
149 	struct resource *r;
150 	lwkt_tokref ilock;
151 
152 	lwkt_gettoken(&ilock, rm->rm_slock);
153 	CIRCLEQ_FOREACH(r, &rm->rm_list, r_link) {
154 		if (r->r_flags & RF_ALLOCATED) {
155 			lwkt_reltoken(&ilock);
156 			return EBUSY;
157 		}
158 	}
159 
160 	/*
161 	 * There really should only be one of these if we are in this
162 	 * state and the code is working properly, but it can't hurt.
163 	 */
164 	while (!CIRCLEQ_EMPTY(&rm->rm_list)) {
165 		r = CIRCLEQ_FIRST(&rm->rm_list);
166 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
167 		kfree(r, M_RMAN);
168 	}
169 	lwkt_reltoken(&ilock);
170 	/* XXX what's the point of this if we are going to free the struct? */
171 	lwkt_gettoken(&ilock, &rman_tok);
172 	TAILQ_REMOVE(&rman_head, rm, rm_link);
173 	lwkt_reltoken(&ilock);
174 	kfree(rm->rm_slock, M_RMAN);
175 
176 	return 0;
177 }
178 
179 struct resource *
180 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
181 		      u_int flags, struct device *dev)
182 {
183 	u_int	want_activate;
184 	struct	resource *r, *s, *rv;
185 	u_long	rstart, rend;
186 	lwkt_tokref ilock;
187 
188 	rv = 0;
189 
190 #ifdef RMAN_DEBUG
191 	kprintf("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
192 	       "%#lx, flags %u, device %s%d\n", rm->rm_descr, start, end,
193 	       count, flags, device_get_name(dev), device_get_unit(dev));
194 #endif /* RMAN_DEBUG */
195 	want_activate = (flags & RF_ACTIVE);
196 	flags &= ~RF_ACTIVE;
197 
198 	lwkt_gettoken(&ilock, rm->rm_slock);
199 
200 	for (r = CIRCLEQ_FIRST(&rm->rm_list);
201 	     !CIRCLEQ_TERMCOND(r, rm->rm_list) && r->r_end < start;
202 	     r = CIRCLEQ_NEXT(r, r_link))
203 		;
204 
205 	if (CIRCLEQ_TERMCOND(r, rm->rm_list)) {
206 #ifdef RMAN_DEBUG
207 		kprintf("could not find a region\n");
208 #endif
209 		goto out;
210 	}
211 
212 	/*
213 	 * First try to find an acceptable totally-unshared region.
214 	 */
215 	for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
216 	     s = CIRCLEQ_NEXT(s, r_link)) {
217 #ifdef RMAN_DEBUG
218 		kprintf("considering [%#lx, %#lx]\n", s->r_start, s->r_end);
219 #endif /* RMAN_DEBUG */
220 		if (s->r_start > end) {
221 #ifdef RMAN_DEBUG
222 			kprintf("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end);
223 #endif /* RMAN_DEBUG */
224 			break;
225 		}
226 		if (s->r_flags & RF_ALLOCATED) {
227 #ifdef RMAN_DEBUG
228 			kprintf("region is allocated\n");
229 #endif /* RMAN_DEBUG */
230 			continue;
231 		}
232 		rstart = max(s->r_start, start);
233 		rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
234 		    ~((1ul << RF_ALIGNMENT(flags)) - 1);
235 		rend = min(s->r_end, max(start + count, end));
236 #ifdef RMAN_DEBUG
237 		kprintf("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
238 		       rstart, rend, (rend - rstart + 1), count);
239 #endif /* RMAN_DEBUG */
240 
241 		if ((rend - rstart + 1) >= count) {
242 #ifdef RMAN_DEBUG
243 			kprintf("candidate region: [%#lx, %#lx], size %#lx\n",
244 			       rend, rstart, (rend - rstart + 1));
245 #endif /* RMAN_DEBUG */
246 			if ((s->r_end - s->r_start + 1) == count) {
247 #ifdef RMAN_DEBUG
248 				kprintf("candidate region is entire chunk\n");
249 #endif /* RMAN_DEBUG */
250 				rv = s;
251 				rv->r_flags |= RF_ALLOCATED | flags;
252 				rv->r_dev = dev;
253 				goto out;
254 			}
255 
256 			/*
257 			 * If s->r_start < rstart and
258 			 *    s->r_end > rstart + count - 1, then
259 			 * we need to split the region into three pieces
260 			 * (the middle one will get returned to the user).
261 			 * Otherwise, we are allocating at either the
262 			 * beginning or the end of s, so we only need to
263 			 * split it in two.  The first case requires
264 			 * two new allocations; the second requires but one.
265 			 */
266 			rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT);
267 			if (rv == 0)
268 				goto out;
269 			bzero(rv, sizeof *rv);
270 			rv->r_start = rstart;
271 			rv->r_end = rstart + count - 1;
272 			rv->r_flags = flags | RF_ALLOCATED;
273 			rv->r_dev = dev;
274 			rv->r_sharehead = 0;
275 			rv->r_rm = rm;
276 
277 			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
278 #ifdef RMAN_DEBUG
279 				kprintf("splitting region in three parts: "
280 				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
281 				       s->r_start, rv->r_start - 1,
282 				       rv->r_start, rv->r_end,
283 				       rv->r_end + 1, s->r_end);
284 #endif /* RMAN_DEBUG */
285 				/*
286 				 * We are allocating in the middle.
287 				 */
288 				r = kmalloc(sizeof *r, M_RMAN, M_NOWAIT);
289 				if (r == 0) {
290 					kfree(rv, M_RMAN);
291 					rv = 0;
292 					goto out;
293 				}
294 				bzero(r, sizeof *r);
295 				r->r_start = rv->r_end + 1;
296 				r->r_end = s->r_end;
297 				r->r_flags = s->r_flags;
298 				r->r_dev = 0;
299 				r->r_sharehead = 0;
300 				r->r_rm = rm;
301 				s->r_end = rv->r_start - 1;
302 				CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
303 						     r_link);
304 				CIRCLEQ_INSERT_AFTER(&rm->rm_list, rv, r,
305 						     r_link);
306 			} else if (s->r_start == rv->r_start) {
307 #ifdef RMAN_DEBUG
308 				kprintf("allocating from the beginning\n");
309 #endif /* RMAN_DEBUG */
310 				/*
311 				 * We are allocating at the beginning.
312 				 */
313 				s->r_start = rv->r_end + 1;
314 				CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, rv,
315 						      r_link);
316 			} else {
317 #ifdef RMAN_DEBUG
318 				kprintf("allocating at the end\n");
319 #endif /* RMAN_DEBUG */
320 				/*
321 				 * We are allocating at the end.
322 				 */
323 				s->r_end = rv->r_start - 1;
324 				CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
325 						     r_link);
326 			}
327 			goto out;
328 		}
329 	}
330 
331 	/*
332 	 * Now find an acceptable shared region, if the client's requirements
333 	 * allow sharing.  By our implementation restriction, a candidate
334 	 * region must match exactly by both size and sharing type in order
335 	 * to be considered compatible with the client's request.  (The
336 	 * former restriction could probably be lifted without too much
337 	 * additional work, but this does not seem warranted.)
338 	 */
339 #ifdef RMAN_DEBUG
340 	kprintf("no unshared regions found\n");
341 #endif /* RMAN_DEBUG */
342 	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
343 		goto out;
344 
345 	for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
346 	     s = CIRCLEQ_NEXT(s, r_link)) {
347 		if (s->r_start > end)
348 			break;
349 		if ((s->r_flags & flags) != flags)
350 			continue;
351 		rstart = max(s->r_start, start);
352 		rend = min(s->r_end, max(start + count, end));
353 		if (s->r_start >= start && s->r_end <= end
354 		    && (s->r_end - s->r_start + 1) == count) {
355 			rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT);
356 			if (rv == 0)
357 				goto out;
358 			bzero(rv, sizeof *rv);
359 			rv->r_start = s->r_start;
360 			rv->r_end = s->r_end;
361 			rv->r_flags = s->r_flags &
362 				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
363 			rv->r_dev = dev;
364 			rv->r_rm = rm;
365 			if (s->r_sharehead == 0) {
366 				s->r_sharehead = kmalloc(sizeof *s->r_sharehead,
367 							M_RMAN, M_NOWAIT);
368 				if (s->r_sharehead == 0) {
369 					kfree(rv, M_RMAN);
370 					rv = 0;
371 					goto out;
372 				}
373 				bzero(s->r_sharehead, sizeof *s->r_sharehead);
374 				LIST_INIT(s->r_sharehead);
375 				LIST_INSERT_HEAD(s->r_sharehead, s,
376 						 r_sharelink);
377 				s->r_flags |= RF_FIRSTSHARE;
378 			}
379 			rv->r_sharehead = s->r_sharehead;
380 			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
381 			goto out;
382 		}
383 	}
384 
385 	/*
386 	 * We couldn't find anything.
387 	 */
388 out:
389 	/*
390 	 * If the user specified RF_ACTIVE in the initial flags,
391 	 * which is reflected in `want_activate', we attempt to atomically
392 	 * activate the resource.  If this fails, we release the resource
393 	 * and indicate overall failure.  (This behavior probably doesn't
394 	 * make sense for RF_TIMESHARE-type resources.)
395 	 */
396 	if (rv && want_activate) {
397 		struct resource *whohas;
398 		if (int_rman_activate_resource(rm, rv, &whohas)) {
399 			int_rman_release_resource(rm, rv);
400 			rv = 0;
401 		}
402 	}
403 	lwkt_reltoken(&ilock);
404 	return (rv);
405 }
406 
407 static int
408 int_rman_activate_resource(struct rman *rm, struct resource *r,
409 			   struct resource **whohas)
410 {
411 	struct resource *s;
412 	int ok;
413 
414 	/*
415 	 * If we are not timesharing, then there is nothing much to do.
416 	 * If we already have the resource, then there is nothing at all to do.
417 	 * If we are not on a sharing list with anybody else, then there is
418 	 * little to do.
419 	 */
420 	if ((r->r_flags & RF_TIMESHARE) == 0
421 	    || (r->r_flags & RF_ACTIVE) != 0
422 	    || r->r_sharehead == 0) {
423 		r->r_flags |= RF_ACTIVE;
424 		return 0;
425 	}
426 
427 	ok = 1;
428 	for (s = LIST_FIRST(r->r_sharehead); s && ok;
429 	     s = LIST_NEXT(s, r_sharelink)) {
430 		if ((s->r_flags & RF_ACTIVE) != 0) {
431 			ok = 0;
432 			*whohas = s;
433 		}
434 	}
435 	if (ok) {
436 		r->r_flags |= RF_ACTIVE;
437 		return 0;
438 	}
439 	return EBUSY;
440 }
441 
442 int
443 rman_activate_resource(struct resource *r)
444 {
445 	int rv;
446 	struct resource *whohas;
447 	lwkt_tokref ilock;
448 	struct rman *rm;
449 
450 	rm = r->r_rm;
451 	lwkt_gettoken(&ilock, rm->rm_slock);
452 	rv = int_rman_activate_resource(rm, r, &whohas);
453 	lwkt_reltoken(&ilock);
454 	return rv;
455 }
456 
457 #if 0
458 
459 /* XXX */
460 int
461 rman_await_resource(struct resource *r, lwkt_tokref_t ilock, int slpflags, int timo)
462 {
463 	int	rv;
464 	struct	resource *whohas;
465 	struct	rman *rm;
466 
467 	rm = r->r_rm;
468 	for (;;) {
469 		lwkt_gettoken(ilock, rm->rm_slock);
470 		rv = int_rman_activate_resource(rm, r, &whohas);
471 		if (rv != EBUSY)
472 			return (rv);	/* returns with ilock held */
473 
474 		if (r->r_sharehead == 0)
475 			panic("rman_await_resource");
476 		/*
477 		 * A critical section will hopefully will prevent a race
478 		 * between lwkt_reltoken and tsleep where a process
479 		 * could conceivably get in and release the resource
480 		 * before we have a chance to sleep on it. YYY
481 		 */
482 		crit_enter();
483 		whohas->r_flags |= RF_WANTED;
484 		rv = tsleep(r->r_sharehead, slpflags, "rmwait", timo);
485 		if (rv) {
486 			lwkt_reltoken(ilock);
487 			crit_exit();
488 			return rv;
489 		}
490 		crit_exit();
491 	}
492 }
493 
494 #endif
495 
496 static int
497 int_rman_deactivate_resource(struct resource *r)
498 {
499 	struct	rman *rm;
500 
501 	rm = r->r_rm;
502 	r->r_flags &= ~RF_ACTIVE;
503 	if (r->r_flags & RF_WANTED) {
504 		r->r_flags &= ~RF_WANTED;
505 		wakeup(r->r_sharehead);
506 	}
507 	return 0;
508 }
509 
510 int
511 rman_deactivate_resource(struct resource *r)
512 {
513 	lwkt_tokref ilock;
514 	struct rman *rm;
515 
516 	rm = r->r_rm;
517 	lwkt_gettoken(&ilock, rm->rm_slock);
518 	int_rman_deactivate_resource(r);
519 	lwkt_reltoken(&ilock);
520 	return 0;
521 }
522 
523 static int
524 int_rman_release_resource(struct rman *rm, struct resource *r)
525 {
526 	struct	resource *s, *t;
527 
528 	if (r->r_flags & RF_ACTIVE)
529 		int_rman_deactivate_resource(r);
530 
531 	/*
532 	 * Check for a sharing list first.  If there is one, then we don't
533 	 * have to think as hard.
534 	 */
535 	if (r->r_sharehead) {
536 		/*
537 		 * If a sharing list exists, then we know there are at
538 		 * least two sharers.
539 		 *
540 		 * If we are in the main circleq, appoint someone else.
541 		 */
542 		LIST_REMOVE(r, r_sharelink);
543 		s = LIST_FIRST(r->r_sharehead);
544 		if (r->r_flags & RF_FIRSTSHARE) {
545 			s->r_flags |= RF_FIRSTSHARE;
546 			CIRCLEQ_INSERT_BEFORE(&rm->rm_list, r, s, r_link);
547 			CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
548 		}
549 
550 		/*
551 		 * Make sure that the sharing list goes away completely
552 		 * if the resource is no longer being shared at all.
553 		 */
554 		if (LIST_NEXT(s, r_sharelink) == 0) {
555 			kfree(s->r_sharehead, M_RMAN);
556 			s->r_sharehead = 0;
557 			s->r_flags &= ~RF_FIRSTSHARE;
558 		}
559 		goto out;
560 	}
561 
562 	/*
563 	 * Look at the adjacent resources in the list and see if our
564 	 * segment can be merged with any of them.
565 	 */
566 	s = CIRCLEQ_PREV(r, r_link);
567 	t = CIRCLEQ_NEXT(r, r_link);
568 
569 	if (s != (void *)&rm->rm_list && (s->r_flags & RF_ALLOCATED) == 0
570 	    && t != (void *)&rm->rm_list && (t->r_flags & RF_ALLOCATED) == 0) {
571 		/*
572 		 * Merge all three segments.
573 		 */
574 		s->r_end = t->r_end;
575 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
576 		CIRCLEQ_REMOVE(&rm->rm_list, t, r_link);
577 		kfree(t, M_RMAN);
578 	} else if (s != (void *)&rm->rm_list
579 		   && (s->r_flags & RF_ALLOCATED) == 0) {
580 		/*
581 		 * Merge previous segment with ours.
582 		 */
583 		s->r_end = r->r_end;
584 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
585 	} else if (t != (void *)&rm->rm_list
586 		   && (t->r_flags & RF_ALLOCATED) == 0) {
587 		/*
588 		 * Merge next segment with ours.
589 		 */
590 		t->r_start = r->r_start;
591 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
592 	} else {
593 		/*
594 		 * At this point, we know there is nothing we
595 		 * can potentially merge with, because on each
596 		 * side, there is either nothing there or what is
597 		 * there is still allocated.  In that case, we don't
598 		 * want to remove r from the list; we simply want to
599 		 * change it to an unallocated region and return
600 		 * without freeing anything.
601 		 */
602 		r->r_flags &= ~RF_ALLOCATED;
603 		return 0;
604 	}
605 
606 out:
607 	kfree(r, M_RMAN);
608 	return 0;
609 }
610 
611 int
612 rman_release_resource(struct resource *r)
613 {
614 	struct	rman *rm = r->r_rm;
615 	lwkt_tokref ilock;
616 	int	rv;
617 
618 	lwkt_gettoken(&ilock, rm->rm_slock);
619 	rv = int_rman_release_resource(rm, r);
620 	lwkt_reltoken(&ilock);
621 	return (rv);
622 }
623 
624 uint32_t
625 rman_make_alignment_flags(uint32_t size)
626 {
627 	int	i;
628 
629 	/*
630 	 * Find the hightest bit set, and add one if more than one bit
631 	 * set.  We're effectively computing the ceil(log2(size)) here.
632 	 */
633 	for (i = 32; i > 0; i--)
634 		if ((1 << i) & size)
635 			break;
636 	if (~(1 << i) & size)
637 		i++;
638 
639 	return(RF_ALIGNMENT_LOG2(i));
640 }
641