priv: Use PRIV_NETINET_RESERVEDPORT
[dragonfly.git] / sys / kern / kern_ccms.c
blob6130d119e6ee163d334010b83f01f59f9d6894d3
1 /*
2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
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:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/kern/kern_ccms.c,v 1.4 2007/04/30 07:18:53 dillon Exp $
37 * The Cache Coherency Management System (CCMS)
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/objcache.h>
45 #include <sys/ccms.h>
46 #include <sys/sysctl.h>
47 #include <sys/uio.h>
48 #include <machine/limits.h>
50 struct ccms_lock_scan_info {
51 ccms_dataspace_t ds;
52 ccms_lock_t lock;
53 ccms_cst_t cst1;
54 ccms_cst_t cst2;
55 ccms_cst_t coll_cst;
58 static int ccms_cst_cmp(ccms_cst_t b1, ccms_cst_t b2);
59 static int ccms_lock_scan_cmp(ccms_cst_t b1, void *arg);
60 static int ccms_lock_undo_cmp(ccms_cst_t b1, void *arg);
61 static int ccms_dataspace_destroy_match(ccms_cst_t cst, void *arg);
62 static int ccms_lock_get_match(struct ccms_cst *cst, void *arg);
63 static int ccms_lock_undo_match(struct ccms_cst *cst, void *arg);
64 static int ccms_lock_redo_match(struct ccms_cst *cst, void *arg);
65 static int ccms_lock_put_match(struct ccms_cst *cst, void *arg);
67 RB_GENERATE3(ccms_rb_tree, ccms_cst, rbnode, ccms_cst_cmp,
68 off_t, beg_offset, end_offset);
69 static MALLOC_DEFINE(M_CCMS, "CCMS", "Cache Coherency Management System");
71 static int ccms_enable;
72 SYSCTL_INT(_kern, OID_AUTO, ccms_enable, CTLFLAG_RW, &ccms_enable, 0, "");
74 static struct objcache *ccms_oc;
77 * Initialize the CCMS subsystem
79 static void
80 ccmsinit(void *dummy)
82 ccms_oc = objcache_create_simple(M_CCMS, sizeof(struct ccms_cst));
84 SYSINIT(ccms, SI_BOOT2_MACHDEP, SI_ORDER_ANY, ccmsinit, NULL);
87 * Initialize a new CCMS dataspace. Create a new RB tree with a single
88 * element covering the entire 64 bit offset range. This simplifies
89 * algorithms enormously by removing a number of special cases.
91 void
92 ccms_dataspace_init(ccms_dataspace_t ds)
94 ccms_cst_t cst;
96 RB_INIT(&ds->tree);
97 ds->info = NULL;
98 ds->chain = NULL;
99 cst = objcache_get(ccms_oc, M_WAITOK);
100 bzero(cst, sizeof(*cst));
101 cst->beg_offset = LLONG_MIN;
102 cst->end_offset = LLONG_MAX;
103 cst->state = CCMS_STATE_INVALID;
104 RB_INSERT(ccms_rb_tree, &ds->tree, cst);
108 * Destroy a CCMS dataspace.
110 void
111 ccms_dataspace_destroy(ccms_dataspace_t ds)
113 RB_SCAN(ccms_rb_tree, &ds->tree, NULL,
114 ccms_dataspace_destroy_match, ds);
117 static
119 ccms_dataspace_destroy_match(ccms_cst_t cst, void *arg)
121 ccms_dataspace_t ds = arg;
123 RB_REMOVE(ccms_rb_tree, &ds->tree, cst);
124 objcache_put(ccms_oc, cst);
125 return(0);
129 * Obtain a CCMS lock
132 ccms_lock_get(ccms_dataspace_t ds, ccms_lock_t lock)
134 struct ccms_lock_scan_info info;
136 if (ccms_enable == 0) {
137 lock->ds = NULL;
138 return(0);
142 * Partition the CST space so the precise range is covered and
143 * attempt to obtain the requested local lock (ltype) at the same
144 * time.
146 lock->ds = ds;
147 info.lock = lock;
148 info.ds = ds;
149 info.coll_cst = NULL;
150 info.cst1 = objcache_get(ccms_oc, M_WAITOK);
151 info.cst2 = objcache_get(ccms_oc, M_WAITOK);
153 RB_SCAN(ccms_rb_tree, &ds->tree, ccms_lock_scan_cmp,
154 ccms_lock_get_match, &info);
157 * If a collision occured, undo the fragments we were able to obtain,
158 * block, and try again.
160 while (info.coll_cst != NULL) {
161 RB_SCAN(ccms_rb_tree, &ds->tree, ccms_lock_undo_cmp,
162 ccms_lock_undo_match, &info);
163 info.coll_cst->blocked = 1;
164 tsleep(info.coll_cst, 0,
165 ((lock->ltype == CCMS_LTYPE_SHARED) ? "rngsh" : "rngex"),
166 hz);
167 info.coll_cst = NULL;
168 RB_SCAN(ccms_rb_tree, &ds->tree, ccms_lock_scan_cmp,
169 ccms_lock_redo_match, &info);
173 * Cleanup
175 if (info.cst1)
176 objcache_put(ccms_oc, info.cst1);
177 if (info.cst2)
178 objcache_put(ccms_oc, info.cst2);
180 return(0);
184 * Obtain a CCMS lock, initialize the lock structure from the uio.
187 ccms_lock_get_uio(ccms_dataspace_t ds, ccms_lock_t lock, struct uio *uio)
189 ccms_ltype_t ltype;
190 off_t eoff;
192 if (uio->uio_rw == UIO_READ)
193 ltype = CCMS_LTYPE_SHARED;
194 else
195 ltype = CCMS_LTYPE_MODIFYING;
198 * Calculate the ending offset (byte inclusive), make sure a seek
199 * overflow does not blow us up.
201 eoff = uio->uio_offset + uio->uio_resid - 1;
202 if (eoff < uio->uio_offset)
203 eoff = 0x7FFFFFFFFFFFFFFFLL;
204 ccms_lock_init(lock, uio->uio_offset, eoff, ltype);
205 return(ccms_lock_get(ds, lock));
208 static
210 ccms_lock_get_match(ccms_cst_t cst, void *arg)
212 struct ccms_lock_scan_info *info = arg;
213 ccms_lock_t lock = info->lock;
214 ccms_cst_t ncst;
217 * If the lock's left edge is within the CST we must split the CST
218 * into two pieces [cst][ncst]. lrefs must be bumped on the CST
219 * containing the left edge.
221 * NOTE! cst->beg_offset may not be modified. This allows us to avoid
222 * having to manipulate the cst's position in the tree.
224 if (lock->beg_offset > cst->beg_offset) {
225 ncst = info->cst1;
226 info->cst1 = NULL;
227 KKASSERT(ncst != NULL);
228 *ncst = *cst;
229 cst->end_offset = lock->beg_offset - 1;
230 cst->rrefs = 0;
231 ncst->beg_offset = lock->beg_offset;
232 ncst->lrefs = 1;
233 RB_INSERT(ccms_rb_tree, &info->ds->tree, ncst);
236 * ncst becomes our 'matching' cst.
238 cst = ncst;
239 } else if (lock->beg_offset == cst->beg_offset) {
240 ++cst->lrefs;
244 * If the lock's right edge is within the CST we must split the CST
245 * into two pieces [cst][ncst]. rrefs must be bumped on the CST
246 * containing the right edge.
248 * NOTE! cst->beg_offset may not be modified. This allows us to avoid
249 * having to manipulate the cst's position in the tree.
251 if (lock->end_offset < cst->end_offset) {
252 ncst = info->cst2;
253 info->cst2 = NULL;
254 KKASSERT(ncst != NULL);
255 *ncst = *cst;
256 cst->end_offset = lock->end_offset;
257 cst->rrefs = 1;
258 ncst->beg_offset = lock->end_offset + 1;
259 ncst->lrefs = 0;
260 RB_INSERT(ccms_rb_tree, &info->ds->tree, ncst);
261 /* cst remains our 'matching' cst */
262 } else if (lock->end_offset == cst->end_offset) {
263 ++cst->rrefs;
267 * The lock covers the CST, so increment the CST's coverage count.
268 * Then attempt to obtain the shared/exclusive ltype.
270 ++cst->xrefs;
272 if (info->coll_cst == NULL) {
273 switch(lock->ltype) {
274 case CCMS_LTYPE_SHARED:
275 if (cst->sharecount < 0) {
276 info->coll_cst = cst;
277 } else {
278 ++cst->sharecount;
279 if (ccms_enable >= 9) {
280 kprintf("CST SHARE %d %lld-%lld\n",
281 cst->sharecount,
282 (long long)cst->beg_offset,
283 (long long)cst->end_offset);
286 break;
287 case CCMS_LTYPE_EXCLUSIVE:
288 if (cst->sharecount != 0) {
289 info->coll_cst = cst;
290 } else {
291 --cst->sharecount;
292 if (ccms_enable >= 9) {
293 kprintf("CST EXCLS %d %lld-%lld\n",
294 cst->sharecount,
295 (long long)cst->beg_offset,
296 (long long)cst->end_offset);
299 break;
300 case CCMS_LTYPE_MODIFYING:
301 if (cst->sharecount != 0) {
302 info->coll_cst = cst;
303 } else {
304 --cst->sharecount;
305 ++cst->modifycount;
306 if (ccms_enable >= 9) {
307 kprintf("CST MODXL %d %lld-%lld\n",
308 cst->sharecount,
309 (long long)cst->beg_offset,
310 (long long)cst->end_offset);
313 break;
316 return(0);
320 * Undo a partially resolved ccms_ltype rangelock. This is atomic with
321 * the scan/redo code so there should not be any blocked locks when
322 * transitioning to 0.
324 static
326 ccms_lock_undo_match(ccms_cst_t cst, void *arg)
328 struct ccms_lock_scan_info *info = arg;
329 ccms_lock_t lock = info->lock;
331 switch(lock->ltype) {
332 case CCMS_LTYPE_SHARED:
333 KKASSERT(cst->sharecount > 0);
334 --cst->sharecount;
335 KKASSERT(cst->sharecount || cst->blocked == 0);
336 break;
337 case CCMS_LTYPE_EXCLUSIVE:
338 KKASSERT(cst->sharecount < 0);
339 ++cst->sharecount;
340 KKASSERT(cst->sharecount || cst->blocked == 0);
341 break;
342 case CCMS_LTYPE_MODIFYING:
343 KKASSERT(cst->sharecount < 0 && cst->modifycount > 0);
344 ++cst->sharecount;
345 --cst->modifycount;
346 KKASSERT(cst->sharecount || cst->blocked == 0);
347 break;
349 return(0);
353 * Redo the local lock request for a range which has already been
354 * partitioned.
356 static
358 ccms_lock_redo_match(ccms_cst_t cst, void *arg)
360 struct ccms_lock_scan_info *info = arg;
361 ccms_lock_t lock = info->lock;
363 if (info->coll_cst == NULL) {
364 switch(lock->ltype) {
365 case CCMS_LTYPE_SHARED:
366 if (cst->sharecount < 0) {
367 info->coll_cst = cst;
368 } else {
369 if (ccms_enable >= 9) {
370 kprintf("CST SHARE %d %lld-%lld\n",
371 cst->sharecount,
372 (long long)cst->beg_offset,
373 (long long)cst->end_offset);
375 ++cst->sharecount;
377 break;
378 case CCMS_LTYPE_EXCLUSIVE:
379 if (cst->sharecount != 0) {
380 info->coll_cst = cst;
381 } else {
382 --cst->sharecount;
383 if (ccms_enable >= 9) {
384 kprintf("CST EXCLS %d %lld-%lld\n",
385 cst->sharecount,
386 (long long)cst->beg_offset,
387 (long long)cst->end_offset);
390 break;
391 case CCMS_LTYPE_MODIFYING:
392 if (cst->sharecount != 0) {
393 info->coll_cst = cst;
394 } else {
395 --cst->sharecount;
396 ++cst->modifycount;
397 if (ccms_enable >= 9) {
398 kprintf("CST MODXL %d %lld-%lld\n",
399 cst->sharecount,
400 (long long)cst->beg_offset,
401 (long long)cst->end_offset);
404 break;
407 return(0);
411 * Release a CCMS lock
414 ccms_lock_put(ccms_dataspace_t ds, ccms_lock_t lock)
416 struct ccms_lock_scan_info info;
418 if (lock->ds == NULL)
419 return(0);
421 lock->ds = NULL;
422 info.lock = lock;
423 info.ds = ds;
424 info.cst1 = NULL;
425 info.cst2 = NULL;
427 RB_SCAN(ccms_rb_tree, &ds->tree, ccms_lock_scan_cmp,
428 ccms_lock_put_match, &info);
430 if (info.cst1)
431 objcache_put(ccms_oc, info.cst1);
432 if (info.cst2)
433 objcache_put(ccms_oc, info.cst2);
434 return(0);
437 static
439 ccms_lock_put_match(ccms_cst_t cst, void *arg)
441 struct ccms_lock_scan_info *info = arg;
442 ccms_lock_t lock = info->lock;
443 ccms_cst_t ocst;
446 * Undo the local shared/exclusive rangelock.
448 switch(lock->ltype) {
449 case CCMS_LTYPE_SHARED:
450 KKASSERT(cst->sharecount > 0);
451 --cst->sharecount;
452 if (ccms_enable >= 9) {
453 kprintf("CST UNSHR %d %lld-%lld (%d)\n", cst->sharecount,
454 (long long)cst->beg_offset,
455 (long long)cst->end_offset,
456 cst->blocked);
458 if (cst->blocked && cst->sharecount == 0) {
459 cst->blocked = 0;
460 wakeup(cst);
462 break;
463 case CCMS_LTYPE_EXCLUSIVE:
464 KKASSERT(cst->sharecount < 0);
465 ++cst->sharecount;
466 if (ccms_enable >= 9) {
467 kprintf("CST UNEXC %d %lld-%lld (%d)\n", cst->sharecount,
468 (long long)cst->beg_offset,
469 (long long)cst->end_offset,
470 cst->blocked);
472 if (cst->blocked && cst->sharecount == 0) {
473 cst->blocked = 0;
474 wakeup(cst);
476 break;
477 case CCMS_LTYPE_MODIFYING:
478 KKASSERT(cst->sharecount < 0 && cst->modifycount > 0);
479 ++cst->sharecount;
480 --cst->modifycount;
481 if (ccms_enable >= 9) {
482 kprintf("CST UNMOD %d %lld-%lld (%d)\n", cst->sharecount,
483 (long long)cst->beg_offset,
484 (long long)cst->end_offset,
485 cst->blocked);
487 if (cst->blocked && cst->sharecount == 0) {
488 cst->blocked = 0;
489 wakeup(cst);
491 break;
495 * Decrement the lock coverage count on the CST. Decrement the left and
496 * right edge counts as appropriate.
498 * When lrefs or rrefs drops to zero we check the adjacent entry to
499 * determine whether a merge is possible. If the appropriate refs field
500 * (rrefs for the entry to our left, lrefs for the entry to our right)
501 * is 0, then all covering locks must cover both entries and the xrefs
502 * field must match. We can then merge the entries if they have
503 * compatible cache states.
505 * However, because we are cleaning up the shared/exclusive count at
506 * the same time, the sharecount field may be temporarily out of
507 * sync, so require that the sharecount field also match before doing
508 * a merge.
510 * When merging an element which is being blocked on, the blocking
511 * thread(s) will be woken up.
513 * If the dataspace has too many CSTs we may be able to merge the
514 * entries even if their cache states are not the same, by dropping
515 * both to a compatible (lower) cache state and performing the appropriate
516 * management operations. XXX
518 --cst->xrefs;
519 if (lock->beg_offset == cst->beg_offset) {
520 --cst->lrefs;
521 if (cst->lrefs == 0) {
522 if ((ocst = RB_PREV(ccms_rb_tree, &info->ds->tree, cst)) != NULL &&
523 ocst->rrefs == 0 &&
524 ocst->state == cst->state &&
525 ocst->sharecount == cst->sharecount
527 KKASSERT(ocst->xrefs == cst->xrefs);
528 KKASSERT(ocst->end_offset + 1 == cst->beg_offset);
529 RB_REMOVE(ccms_rb_tree, &info->ds->tree, ocst);
530 cst->beg_offset = ocst->beg_offset;
531 cst->lrefs = ocst->lrefs;
532 if (ccms_enable >= 9) {
533 kprintf("MERGELEFT %p %lld-%lld (%d)\n",
534 ocst,
535 (long long)cst->beg_offset,
536 (long long)cst->end_offset,
537 cst->blocked);
539 if (ocst->blocked) {
540 ocst->blocked = 0;
541 wakeup(ocst);
543 objcache_put(ccms_oc, ocst);
547 if (lock->end_offset == cst->end_offset) {
548 --cst->rrefs;
549 if (cst->rrefs == 0) {
550 if ((ocst = RB_NEXT(ccms_rb_tree, &info->ds->tree, cst)) != NULL &&
551 ocst->lrefs == 0 &&
552 ocst->state == cst->state &&
553 ocst->sharecount == cst->sharecount
555 KKASSERT(ocst->xrefs == cst->xrefs);
556 KKASSERT(cst->end_offset + 1 == ocst->beg_offset);
557 RB_REMOVE(ccms_rb_tree, &info->ds->tree, ocst);
558 cst->end_offset = ocst->end_offset;
559 cst->rrefs = ocst->rrefs;
560 if (ccms_enable >= 9) {
561 kprintf("MERGERIGHT %p %lld-%lld\n",
562 ocst,
563 (long long)cst->beg_offset,
564 (long long)cst->end_offset);
566 objcache_put(ccms_oc, ocst);
570 return(0);
575 * RB tree compare function for insertions and deletions. This function
576 * compares two CSTs.
578 static int
579 ccms_cst_cmp(ccms_cst_t b1, ccms_cst_t b2)
581 if (b1->end_offset < b2->beg_offset)
582 return(-1);
583 if (b1->beg_offset > b2->end_offset)
584 return(1);
585 return(0);
589 * RB tree scanning compare function. This function compares the CST
590 * from the tree against the supplied ccms_lock and returns the CST's
591 * placement relative to the lock.
593 static int
594 ccms_lock_scan_cmp(ccms_cst_t cst, void *arg)
596 struct ccms_lock_scan_info *info = arg;
597 ccms_lock_t lock = info->lock;
599 if (cst->end_offset < lock->beg_offset)
600 return(-1);
601 if (cst->beg_offset > lock->end_offset)
602 return(1);
603 return(0);
607 * This function works like ccms_lock_scan_cmp but terminates at the
608 * collision point rather then at the lock's ending offset. Only
609 * the CSTs that were already partially resolved are returned by the scan.
611 static int
612 ccms_lock_undo_cmp(ccms_cst_t cst, void *arg)
614 struct ccms_lock_scan_info *info = arg;
615 ccms_lock_t lock = info->lock;
617 if (cst->end_offset < lock->beg_offset)
618 return(-1);
619 if (cst->beg_offset >= info->coll_cst->beg_offset)
620 return(1);
621 return(0);