2 * Copyright (c) 2009, 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
41 #include <sys/diskslice.h>
43 #include <sys/malloc.h>
44 #include <machine/md_var.h>
45 #include <sys/ctype.h>
46 #include <sys/syslog.h>
47 #include <sys/device.h>
48 #include <sys/msgport.h>
49 #include <sys/msgport2.h>
51 #include <sys/dsched.h>
52 #include <sys/fcntl.h>
53 #include <machine/varargs.h>
55 TAILQ_HEAD(tdio_list_head, dsched_thread_io);
57 MALLOC_DEFINE(M_DSCHED, "dsched", "dsched allocs");
59 static dsched_prepare_t noop_prepare;
60 static dsched_teardown_t noop_teardown;
61 static dsched_cancel_t noop_cancel;
62 static dsched_queue_t noop_queue;
64 static void dsched_thread_io_unref_destroy(struct dsched_thread_io *tdio);
65 static void dsched_sysctl_add_disk(struct dsched_disk_ctx *diskctx, char *name);
66 static void dsched_disk_ctx_destroy(struct dsched_disk_ctx *diskctx);
67 static void dsched_thread_io_destroy(struct dsched_thread_io *tdio);
68 static void dsched_thread_ctx_destroy(struct dsched_thread_ctx *tdctx);
70 static struct dsched_thread_io *dsched_thread_io_alloc(
71 struct disk *dp, struct dsched_thread_ctx *tdctx,
72 struct dsched_policy *pol, int tdctx_locked);
74 static int dsched_inited = 0;
75 static int default_set = 0;
77 struct lock dsched_lock;
78 static int dsched_debug_enable = 0;
80 struct dsched_stats dsched_stats;
82 struct objcache_malloc_args dsched_disk_ctx_malloc_args = {
83 DSCHED_DISK_CTX_MAX_SZ, M_DSCHED };
84 struct objcache_malloc_args dsched_thread_io_malloc_args = {
85 DSCHED_THREAD_IO_MAX_SZ, M_DSCHED };
86 struct objcache_malloc_args dsched_thread_ctx_malloc_args = {
87 DSCHED_THREAD_CTX_MAX_SZ, M_DSCHED };
89 static struct objcache *dsched_diskctx_cache;
90 static struct objcache *dsched_tdctx_cache;
91 static struct objcache *dsched_tdio_cache;
93 struct lock dsched_tdctx_lock;
95 static struct dsched_policy_head dsched_policy_list =
96 TAILQ_HEAD_INITIALIZER(dsched_policy_list);
98 static struct dsched_policy dsched_noop_policy = {
101 .prepare = noop_prepare,
102 .teardown = noop_teardown,
103 .cancel_all = noop_cancel,
104 .bio_queue = noop_queue
107 static struct dsched_policy *default_policy = &dsched_noop_policy;
110 * dsched_debug() is a SYSCTL and TUNABLE controlled debug output function
114 dsched_debug(int level, char *fmt, ...)
119 if (level <= dsched_debug_enable)
127 * Called on disk_create()
128 * tries to read which policy to use from loader.conf, if there's
129 * none specified, the default policy is used.
132 dsched_disk_create_callback(struct disk *dp, const char *head_name, int unit)
134 char tunable_key[SPECNAMELEN + 48];
135 char sched_policy[DSCHED_POLICY_NAME_LENGTH];
137 struct dsched_policy *policy = NULL;
139 /* Also look for serno stuff? */
140 lockmgr(&dsched_lock, LK_EXCLUSIVE);
142 ksnprintf(tunable_key, sizeof(tunable_key),
143 "dsched.policy.%s%d", head_name, unit);
144 if (TUNABLE_STR_FETCH(tunable_key, sched_policy,
145 sizeof(sched_policy)) != 0) {
146 policy = dsched_find_policy(sched_policy);
149 ksnprintf(tunable_key, sizeof(tunable_key),
150 "dsched.policy.%s", head_name);
152 for (ptr = tunable_key; *ptr; ptr++) {
156 if (!policy && (TUNABLE_STR_FETCH(tunable_key, sched_policy,
157 sizeof(sched_policy)) != 0)) {
158 policy = dsched_find_policy(sched_policy);
161 ksnprintf(tunable_key, sizeof(tunable_key), "dsched.policy.default");
162 if (!policy && !default_set &&
163 (TUNABLE_STR_FETCH(tunable_key, sched_policy,
164 sizeof(sched_policy)) != 0)) {
165 policy = dsched_find_policy(sched_policy);
169 if (!default_set && bootverbose) {
171 "No policy for %s%d specified, "
172 "or policy not found\n",
175 dsched_set_policy(dp, default_policy);
177 dsched_set_policy(dp, policy);
180 if (strncmp(head_name, "mapper/", strlen("mapper/")) == 0)
181 ksnprintf(tunable_key, sizeof(tunable_key), "%s", head_name);
183 ksnprintf(tunable_key, sizeof(tunable_key), "%s%d", head_name, unit);
184 for (ptr = tunable_key; *ptr; ptr++) {
188 dsched_sysctl_add_disk(
189 (struct dsched_disk_ctx *)dsched_get_disk_priv(dp),
192 lockmgr(&dsched_lock, LK_RELEASE);
196 * Called from disk_setdiskinfo (or rather _setdiskinfo). This will check if
197 * there's any policy associated with the serial number of the device.
200 dsched_disk_update_callback(struct disk *dp, struct disk_info *info)
202 char tunable_key[SPECNAMELEN + 48];
203 char sched_policy[DSCHED_POLICY_NAME_LENGTH];
204 struct dsched_policy *policy = NULL;
206 if (info->d_serialno == NULL)
209 lockmgr(&dsched_lock, LK_EXCLUSIVE);
211 ksnprintf(tunable_key, sizeof(tunable_key), "dsched.policy.%s",
214 if((TUNABLE_STR_FETCH(tunable_key, sched_policy,
215 sizeof(sched_policy)) != 0)) {
216 policy = dsched_find_policy(sched_policy);
220 dsched_switch(dp, policy);
223 dsched_sysctl_add_disk(
224 (struct dsched_disk_ctx *)dsched_get_disk_priv(dp),
227 lockmgr(&dsched_lock, LK_RELEASE);
231 * Called on disk_destroy()
232 * shuts down the scheduler core and cancels all remaining bios
235 dsched_disk_destroy_callback(struct disk *dp)
237 struct dsched_policy *old_policy;
238 struct dsched_disk_ctx *diskctx;
240 lockmgr(&dsched_lock, LK_EXCLUSIVE);
242 diskctx = dsched_get_disk_priv(dp);
244 old_policy = dp->d_sched_policy;
245 dp->d_sched_policy = &dsched_noop_policy;
246 old_policy->cancel_all(dsched_get_disk_priv(dp));
247 old_policy->teardown(dsched_get_disk_priv(dp));
249 if (diskctx->flags & DSCHED_SYSCTL_CTX_INITED)
250 sysctl_ctx_free(&diskctx->sysctl_ctx);
253 atomic_subtract_int(&old_policy->ref_count, 1);
254 KKASSERT(old_policy->ref_count >= 0);
256 lockmgr(&dsched_lock, LK_RELEASE);
261 * Caller must have dp->diskctx locked
264 dsched_queue(struct disk *dp, struct bio *bio)
266 struct dsched_thread_ctx *tdctx;
267 struct dsched_thread_io *tdio;
268 struct dsched_disk_ctx *diskctx;
272 if (dp->d_sched_policy == &dsched_noop_policy) {
273 dsched_clr_buf_priv(bio->bio_buf);
274 atomic_add_int(&dsched_stats.no_tdctx, 1);
275 dsched_strategy_raw(dp, bio);
281 tdctx = dsched_get_buf_priv(bio->bio_buf);
283 /* We don't handle this case, let dsched dispatch */
284 atomic_add_int(&dsched_stats.no_tdctx, 1);
285 dsched_strategy_raw(dp, bio);
289 DSCHED_THREAD_CTX_LOCK(tdctx);
293 * iterate in reverse to make sure we find the most up-to-date
294 * tdio for a given disk. After a switch it may take some time
295 * for everything to clean up.
297 TAILQ_FOREACH_REVERSE(tdio, &tdctx->tdio_list, tdio_list_head, link) {
298 if (tdio->dp == dp) {
299 dsched_thread_io_ref(tdio);
304 tdio = dsched_thread_io_alloc(dp, tdctx, dp->d_sched_policy, 1);
305 dsched_thread_io_ref(tdio);
308 DSCHED_THREAD_CTX_UNLOCK(tdctx);
309 dsched_clr_buf_priv(bio->bio_buf);
310 dsched_thread_ctx_unref(tdctx); /* acquired on new_buf */
312 KKASSERT(found == 1);
313 diskctx = dsched_get_disk_priv(dp);
314 dsched_disk_ctx_ref(diskctx);
316 if (dp->d_sched_policy != &dsched_noop_policy)
317 KKASSERT(tdio->debug_policy == dp->d_sched_policy);
319 KKASSERT(tdio->debug_inited == 0xF00F1234);
321 error = dp->d_sched_policy->bio_queue(diskctx, tdio, bio);
324 dsched_strategy_raw(dp, bio);
326 dsched_disk_ctx_unref(diskctx);
327 dsched_thread_io_unref(tdio);
332 * Called from each module_init or module_attach of each policy
333 * registers the policy in the local policy list.
336 dsched_register(struct dsched_policy *d_policy)
338 struct dsched_policy *policy;
341 lockmgr(&dsched_lock, LK_EXCLUSIVE);
343 policy = dsched_find_policy(d_policy->name);
346 TAILQ_INSERT_TAIL(&dsched_policy_list, d_policy, link);
347 atomic_add_int(&d_policy->ref_count, 1);
349 dsched_debug(LOG_ERR, "Policy with name %s already registered!\n",
354 lockmgr(&dsched_lock, LK_RELEASE);
359 * Called from each module_detach of each policy
360 * unregisters the policy
363 dsched_unregister(struct dsched_policy *d_policy)
365 struct dsched_policy *policy;
367 lockmgr(&dsched_lock, LK_EXCLUSIVE);
368 policy = dsched_find_policy(d_policy->name);
371 if (policy->ref_count > 1) {
372 lockmgr(&dsched_lock, LK_RELEASE);
375 TAILQ_REMOVE(&dsched_policy_list, policy, link);
376 atomic_subtract_int(&policy->ref_count, 1);
377 KKASSERT(policy->ref_count == 0);
379 lockmgr(&dsched_lock, LK_RELEASE);
386 * switches the policy by first removing the old one and then
387 * enabling the new one.
390 dsched_switch(struct disk *dp, struct dsched_policy *new_policy)
392 struct dsched_policy *old_policy;
394 /* If we are asked to set the same policy, do nothing */
395 if (dp->d_sched_policy == new_policy)
398 /* lock everything down, diskwise */
399 lockmgr(&dsched_lock, LK_EXCLUSIVE);
400 old_policy = dp->d_sched_policy;
402 atomic_subtract_int(&old_policy->ref_count, 1);
403 KKASSERT(old_policy->ref_count >= 0);
405 dp->d_sched_policy = &dsched_noop_policy;
406 old_policy->teardown(dsched_get_disk_priv(dp));
409 /* Bring everything back to life */
410 dsched_set_policy(dp, new_policy);
411 lockmgr(&dsched_lock, LK_RELEASE);
418 * Loads a given policy and attaches it to the specified disk.
419 * Also initializes the core for the policy
422 dsched_set_policy(struct disk *dp, struct dsched_policy *new_policy)
426 /* Check if it is locked already. if not, we acquire the devfs lock */
427 if ((lockstatus(&dsched_lock, curthread)) != LK_EXCLUSIVE) {
428 lockmgr(&dsched_lock, LK_EXCLUSIVE);
432 DSCHED_GLOBAL_THREAD_CTX_LOCK();
434 policy_new(dp, new_policy);
435 new_policy->prepare(dsched_get_disk_priv(dp));
436 dp->d_sched_policy = new_policy;
437 atomic_add_int(&new_policy->ref_count, 1);
439 DSCHED_GLOBAL_THREAD_CTX_UNLOCK();
441 kprintf("disk scheduler: set policy of %s to %s\n", dp->d_cdev->si_name,
444 /* If we acquired the lock, we also get rid of it */
446 lockmgr(&dsched_lock, LK_RELEASE);
449 struct dsched_policy*
450 dsched_find_policy(char *search)
452 struct dsched_policy *policy;
453 struct dsched_policy *policy_found = NULL;
456 /* Check if it is locked already. if not, we acquire the devfs lock */
457 if ((lockstatus(&dsched_lock, curthread)) != LK_EXCLUSIVE) {
458 lockmgr(&dsched_lock, LK_EXCLUSIVE);
462 TAILQ_FOREACH(policy, &dsched_policy_list, link) {
463 if (!strcmp(policy->name, search)) {
464 policy_found = policy;
469 /* If we acquired the lock, we also get rid of it */
471 lockmgr(&dsched_lock, LK_RELEASE);
480 dsched_find_disk(char *search)
483 struct disk *dp = NULL;
485 while ((dp = disk_enumerate(&marker, dp)) != NULL) {
486 if (strcmp(dp->d_cdev->si_name, search) == 0) {
487 disk_enumerate_stop(&marker, NULL);
488 /* leave ref on dp */
496 dsched_disk_enumerate(struct disk *marker, struct disk *dp,
497 struct dsched_policy *policy)
499 while ((dp = disk_enumerate(marker, dp)) != NULL) {
500 if (dp->d_sched_policy == policy)
506 struct dsched_policy *
507 dsched_policy_enumerate(struct dsched_policy *pol)
510 return (TAILQ_FIRST(&dsched_policy_list));
512 return (TAILQ_NEXT(pol, link));
516 dsched_cancel_bio(struct bio *bp)
518 bp->bio_buf->b_error = ENXIO;
519 bp->bio_buf->b_flags |= B_ERROR;
520 bp->bio_buf->b_resid = bp->bio_buf->b_bcount;
526 dsched_strategy_raw(struct disk *dp, struct bio *bp)
529 * Ideally, this stuff shouldn't be needed... but just in case, we leave it in
532 KASSERT(dp->d_rawdev != NULL, ("dsched_strategy_raw sees NULL d_rawdev!!"));
533 if(bp->bio_track != NULL) {
534 dsched_debug(LOG_INFO,
535 "dsched_strategy_raw sees non-NULL bio_track!! "
537 bp->bio_track = NULL;
539 dev_dstrategy(dp->d_rawdev, bp);
543 dsched_strategy_sync(struct disk *dp, struct bio *bio)
545 struct buf *bp, *nbp;
553 nbp->b_cmd = bp->b_cmd;
554 nbp->b_bufsize = bp->b_bufsize;
555 nbp->b_runningbufspace = bp->b_runningbufspace;
556 nbp->b_bcount = bp->b_bcount;
557 nbp->b_resid = bp->b_resid;
558 nbp->b_data = bp->b_data;
561 * Buffers undergoing device I/O do not need a kvabase/size.
563 nbp->b_kvabase = bp->b_kvabase;
564 nbp->b_kvasize = bp->b_kvasize;
566 nbp->b_dirtyend = bp->b_dirtyend;
568 nbio->bio_done = biodone_sync;
569 nbio->bio_flags |= BIO_SYNC;
570 nbio->bio_track = NULL;
572 nbio->bio_caller_info1.ptr = dp;
573 nbio->bio_offset = bio->bio_offset;
575 dev_dstrategy(dp->d_rawdev, nbio);
576 biowait(nbio, "dschedsync");
577 bp->b_resid = nbp->b_resid;
578 bp->b_error = nbp->b_error;
581 nbp->b_kvabase = NULL;
588 dsched_strategy_async(struct disk *dp, struct bio *bio, biodone_t *done, void *priv)
592 nbio = push_bio(bio);
593 nbio->bio_done = done;
594 nbio->bio_offset = bio->bio_offset;
596 dsched_set_bio_dp(nbio, dp);
597 dsched_set_bio_priv(nbio, priv);
599 getmicrotime(&nbio->bio_caller_info3.tv);
600 dev_dstrategy(dp->d_rawdev, nbio);
604 * A special bio done call back function
605 * used by policy having request polling implemented.
608 request_polling_biodone(struct bio *bp)
610 struct dsched_disk_ctx *diskctx = NULL;
611 struct disk *dp = NULL;
613 struct dsched_policy *policy;
615 dp = dsched_get_bio_dp(bp);
616 policy = dp->d_sched_policy;
617 diskctx = dsched_get_disk_priv(dp);
618 KKASSERT(diskctx && policy);
619 dsched_disk_ctx_ref(diskctx);
623 * the bio_done function should not be blocked !
625 if (diskctx->dp->d_sched_policy->bio_done)
626 diskctx->dp->d_sched_policy->bio_done(bp);
631 atomic_subtract_int(&diskctx->current_tag_queue_depth, 1);
633 /* call the polling function,
635 * the polling function should not be blocked!
637 if (policy->polling_func)
638 policy->polling_func(diskctx);
640 dsched_debug(0, "dsched: the policy uses request polling without a polling function!\n");
641 dsched_disk_ctx_unref(diskctx);
645 * A special dsched strategy used by policy having request polling
646 * (polling function) implemented.
648 * The strategy is the just like dsched_strategy_async(), but
649 * the biodone call back is set to a preset one.
651 * If the policy needs its own biodone callback, it should
652 * register it in the policy structure. (bio_done field)
654 * The current_tag_queue_depth is maintained by this function
655 * and the request_polling_biodone() function
659 dsched_strategy_request_polling(struct disk *dp, struct bio *bio, struct dsched_disk_ctx *diskctx)
661 atomic_add_int(&diskctx->current_tag_queue_depth, 1);
662 dsched_strategy_async(dp, bio, request_polling_biodone, dsched_get_bio_priv(bio));
666 * Ref and deref various structures. The 1->0 transition of the reference
667 * count actually transitions 1->0x80000000 and causes the object to be
668 * destroyed. It is possible for transitory references to occur on the
669 * object while it is being destroyed. We use bit 31 to indicate that
670 * destruction is in progress and to prevent nested destructions.
673 dsched_disk_ctx_ref(struct dsched_disk_ctx *diskctx)
677 refcount = atomic_fetchadd_int(&diskctx->refcount, 1);
681 dsched_thread_io_ref(struct dsched_thread_io *tdio)
685 refcount = atomic_fetchadd_int(&tdio->refcount, 1);
689 dsched_thread_ctx_ref(struct dsched_thread_ctx *tdctx)
693 refcount = atomic_fetchadd_int(&tdctx->refcount, 1);
697 dsched_disk_ctx_unref(struct dsched_disk_ctx *diskctx)
703 * Handle 1->0 transitions for diskctx and nested destruction
704 * recursions. If the refs are already in destruction mode (bit 31
705 * set) on the 1->0 transition we don't try to destruct it again.
707 * 0x80000001->0x80000000 transitions are handled normally and
708 * thus avoid nested dstruction.
711 refs = diskctx->refcount;
715 KKASSERT(((refs ^ nrefs) & 0x80000000) == 0);
717 if (atomic_cmpset_int(&diskctx->refcount, refs, nrefs))
722 if (atomic_cmpset_int(&diskctx->refcount, refs, nrefs)) {
723 dsched_disk_ctx_destroy(diskctx);
731 dsched_disk_ctx_destroy(struct dsched_disk_ctx *diskctx)
733 struct dsched_thread_io *tdio;
738 kprintf("diskctx (%p) destruction started, trace:\n", diskctx);
741 lockmgr(&diskctx->lock, LK_EXCLUSIVE);
742 while ((tdio = TAILQ_FIRST(&diskctx->tdio_list)) != NULL) {
743 KKASSERT(tdio->flags & DSCHED_LINKED_DISK_CTX);
744 TAILQ_REMOVE(&diskctx->tdio_list, tdio, dlink);
745 atomic_clear_int(&tdio->flags, DSCHED_LINKED_DISK_CTX);
746 tdio->diskctx = NULL;
747 /* XXX tdio->diskctx->dp->d_sched_policy->destroy_tdio(tdio);*/
748 lockmgr(&diskctx->lock, LK_RELEASE);
749 dsched_thread_io_unref_destroy(tdio);
750 lockmgr(&diskctx->lock, LK_EXCLUSIVE);
752 lockmgr(&diskctx->lock, LK_RELEASE);
755 * Expect diskctx->refcount to be 0x80000000. If it isn't someone
756 * else still has a temporary ref on the diskctx and we have to
757 * transition it back to an undestroyed-state (albeit without any
758 * associations), so the other user destroys it properly when the
761 while ((refs = diskctx->refcount) != 0x80000000) {
762 kprintf("dsched_thread_io: destroy race diskctx=%p\n", diskctx);
764 KKASSERT(refs & 0x80000000);
765 nrefs = refs & 0x7FFFFFFF;
766 if (atomic_cmpset_int(&diskctx->refcount, refs, nrefs))
771 * Really for sure now.
773 if (diskctx->dp->d_sched_policy->destroy_diskctx)
774 diskctx->dp->d_sched_policy->destroy_diskctx(diskctx);
775 objcache_put(dsched_diskctx_cache, diskctx);
776 atomic_subtract_int(&dsched_stats.diskctx_allocations, 1);
780 dsched_thread_io_unref(struct dsched_thread_io *tdio)
786 * Handle 1->0 transitions for tdio and nested destruction
787 * recursions. If the refs are already in destruction mode (bit 31
788 * set) on the 1->0 transition we don't try to destruct it again.
790 * 0x80000001->0x80000000 transitions are handled normally and
791 * thus avoid nested dstruction.
794 refs = tdio->refcount;
798 KKASSERT(((refs ^ nrefs) & 0x80000000) == 0);
800 if (atomic_cmpset_int(&tdio->refcount, refs, nrefs))
805 if (atomic_cmpset_int(&tdio->refcount, refs, nrefs)) {
806 dsched_thread_io_destroy(tdio);
813 * Unref and destroy the tdio even if additional refs are present.
817 dsched_thread_io_unref_destroy(struct dsched_thread_io *tdio)
823 * If not already transitioned to destroy-in-progress we transition
824 * to destroy-in-progress, cleanup our ref, and destroy the tdio.
827 refs = tdio->refcount;
831 KKASSERT(((refs ^ nrefs) & 0x80000000) == 0);
832 if (nrefs & 0x80000000) {
833 if (atomic_cmpset_int(&tdio->refcount, refs, nrefs))
838 if (atomic_cmpset_int(&tdio->refcount, refs, nrefs)) {
839 dsched_thread_io_destroy(tdio);
846 dsched_thread_io_destroy(struct dsched_thread_io *tdio)
848 struct dsched_thread_ctx *tdctx;
849 struct dsched_disk_ctx *diskctx;
854 kprintf("tdio (%p) destruction started, trace:\n", tdio);
857 KKASSERT(tdio->qlength == 0);
859 while ((diskctx = tdio->diskctx) != NULL) {
860 dsched_disk_ctx_ref(diskctx);
861 lockmgr(&diskctx->lock, LK_EXCLUSIVE);
862 if (diskctx != tdio->diskctx) {
863 lockmgr(&diskctx->lock, LK_RELEASE);
864 dsched_disk_ctx_unref(diskctx);
867 KKASSERT(tdio->flags & DSCHED_LINKED_DISK_CTX);
868 if (diskctx->dp->d_sched_policy->destroy_tdio)
869 diskctx->dp->d_sched_policy->destroy_tdio(tdio);
870 TAILQ_REMOVE(&diskctx->tdio_list, tdio, dlink);
871 atomic_clear_int(&tdio->flags, DSCHED_LINKED_DISK_CTX);
872 tdio->diskctx = NULL;
873 dsched_thread_io_unref(tdio);
874 lockmgr(&diskctx->lock, LK_RELEASE);
875 dsched_disk_ctx_unref(diskctx);
877 while ((tdctx = tdio->tdctx) != NULL) {
878 dsched_thread_ctx_ref(tdctx);
879 lockmgr(&tdctx->lock, LK_EXCLUSIVE);
880 if (tdctx != tdio->tdctx) {
881 lockmgr(&tdctx->lock, LK_RELEASE);
882 dsched_thread_ctx_unref(tdctx);
885 KKASSERT(tdio->flags & DSCHED_LINKED_THREAD_CTX);
886 TAILQ_REMOVE(&tdctx->tdio_list, tdio, link);
887 atomic_clear_int(&tdio->flags, DSCHED_LINKED_THREAD_CTX);
889 dsched_thread_io_unref(tdio);
890 lockmgr(&tdctx->lock, LK_RELEASE);
891 dsched_thread_ctx_unref(tdctx);
895 * Expect tdio->refcount to be 0x80000000. If it isn't someone else
896 * still has a temporary ref on the tdio and we have to transition
897 * it back to an undestroyed-state (albeit without any associations)
898 * so the other user destroys it properly when the ref is released.
900 while ((refs = tdio->refcount) != 0x80000000) {
901 kprintf("dsched_thread_io: destroy race tdio=%p\n", tdio);
903 KKASSERT(refs & 0x80000000);
904 nrefs = refs & 0x7FFFFFFF;
905 if (atomic_cmpset_int(&tdio->refcount, refs, nrefs))
910 * Really for sure now.
912 objcache_put(dsched_tdio_cache, tdio);
913 atomic_subtract_int(&dsched_stats.tdio_allocations, 1);
917 dsched_thread_ctx_unref(struct dsched_thread_ctx *tdctx)
923 * Handle 1->0 transitions for tdctx and nested destruction
924 * recursions. If the refs are already in destruction mode (bit 31
925 * set) on the 1->0 transition we don't try to destruct it again.
927 * 0x80000001->0x80000000 transitions are handled normally and
928 * thus avoid nested dstruction.
931 refs = tdctx->refcount;
935 KKASSERT(((refs ^ nrefs) & 0x80000000) == 0);
937 if (atomic_cmpset_int(&tdctx->refcount, refs, nrefs))
942 if (atomic_cmpset_int(&tdctx->refcount, refs, nrefs)) {
943 dsched_thread_ctx_destroy(tdctx);
950 dsched_thread_ctx_destroy(struct dsched_thread_ctx *tdctx)
952 struct dsched_thread_io *tdio;
954 lockmgr(&tdctx->lock, LK_EXCLUSIVE);
956 while ((tdio = TAILQ_FIRST(&tdctx->tdio_list)) != NULL) {
957 KKASSERT(tdio->flags & DSCHED_LINKED_THREAD_CTX);
958 TAILQ_REMOVE(&tdctx->tdio_list, tdio, link);
959 atomic_clear_int(&tdio->flags, DSCHED_LINKED_THREAD_CTX);
961 lockmgr(&tdctx->lock, LK_RELEASE); /* avoid deadlock */
962 dsched_thread_io_unref_destroy(tdio);
963 lockmgr(&tdctx->lock, LK_EXCLUSIVE);
965 KKASSERT(tdctx->refcount == 0x80000000);
967 lockmgr(&tdctx->lock, LK_RELEASE);
969 objcache_put(dsched_tdctx_cache, tdctx);
970 atomic_subtract_int(&dsched_stats.tdctx_allocations, 1);
974 * Ensures that a tdio is assigned to tdctx and disk.
977 struct dsched_thread_io *
978 dsched_thread_io_alloc(struct disk *dp, struct dsched_thread_ctx *tdctx,
979 struct dsched_policy *pol, int tdctx_locked)
981 struct dsched_thread_io *tdio;
983 dsched_disk_ctx_ref(dsched_get_disk_priv(dp));
985 tdio = objcache_get(dsched_tdio_cache, M_INTWAIT);
986 bzero(tdio, DSCHED_THREAD_IO_MAX_SZ);
988 dsched_thread_io_ref(tdio); /* prevent ripout */
989 dsched_thread_io_ref(tdio); /* for diskctx ref */
991 DSCHED_THREAD_IO_LOCKINIT(tdio);
994 tdio->diskctx = dsched_get_disk_priv(dp);
995 TAILQ_INIT(&tdio->queue);
1000 lockmgr(&tdio->diskctx->lock, LK_EXCLUSIVE);
1001 TAILQ_INSERT_TAIL(&tdio->diskctx->tdio_list, tdio, dlink);
1002 atomic_set_int(&tdio->flags, DSCHED_LINKED_DISK_CTX);
1006 * Put the tdio in the tdctx list. Inherit the temporary
1007 * ref (one ref for each list).
1009 if (tdctx_locked == 0)
1010 DSCHED_THREAD_CTX_LOCK(tdctx);
1011 tdio->tdctx = tdctx;
1013 TAILQ_INSERT_TAIL(&tdctx->tdio_list, tdio, link);
1014 atomic_set_int(&tdio->flags, DSCHED_LINKED_THREAD_CTX);
1015 if (tdctx_locked == 0)
1016 DSCHED_THREAD_CTX_UNLOCK(tdctx);
1018 dsched_thread_io_unref(tdio);
1021 tdio->debug_policy = pol;
1022 tdio->debug_inited = 0xF00F1234;
1024 atomic_add_int(&dsched_stats.tdio_allocations, 1);
1030 struct dsched_disk_ctx *
1031 dsched_disk_ctx_alloc(struct disk *dp, struct dsched_policy *pol)
1033 struct dsched_disk_ctx *diskctx;
1035 diskctx = objcache_get(dsched_diskctx_cache, M_WAITOK);
1036 bzero(diskctx, DSCHED_DISK_CTX_MAX_SZ);
1037 dsched_disk_ctx_ref(diskctx);
1039 DSCHED_DISK_CTX_LOCKINIT(diskctx);
1040 TAILQ_INIT(&diskctx->tdio_list);
1042 * XXX: magic number 32: most device has a tag queue
1044 * Better to retrive more precise value from the driver
1046 diskctx->max_tag_queue_depth = 32;
1047 diskctx->current_tag_queue_depth = 0;
1049 atomic_add_int(&dsched_stats.diskctx_allocations, 1);
1050 if (pol->new_diskctx)
1051 pol->new_diskctx(diskctx);
1056 struct dsched_thread_ctx *
1057 dsched_thread_ctx_alloc(struct proc *p)
1059 struct dsched_thread_ctx *tdctx;
1061 tdctx = objcache_get(dsched_tdctx_cache, M_WAITOK);
1062 bzero(tdctx, DSCHED_THREAD_CTX_MAX_SZ);
1063 dsched_thread_ctx_ref(tdctx);
1065 kprintf("dsched_thread_ctx_alloc, new tdctx = %p\n", tdctx);
1067 DSCHED_THREAD_CTX_LOCKINIT(tdctx);
1068 TAILQ_INIT(&tdctx->tdio_list);
1071 atomic_add_int(&dsched_stats.tdctx_allocations, 1);
1072 /* XXX: no callback here */
1078 policy_new(struct disk *dp, struct dsched_policy *pol)
1080 struct dsched_disk_ctx *diskctx;
1082 diskctx = dsched_disk_ctx_alloc(dp, pol);
1083 dsched_disk_ctx_ref(diskctx);
1084 dsched_set_disk_priv(dp, diskctx);
1088 policy_destroy(struct disk *dp) {
1089 struct dsched_disk_ctx *diskctx;
1091 diskctx = dsched_get_disk_priv(dp);
1092 KKASSERT(diskctx != NULL);
1094 dsched_disk_ctx_unref(diskctx); /* from prepare */
1095 dsched_disk_ctx_unref(diskctx); /* from alloc */
1097 dsched_set_disk_priv(dp, NULL);
1101 dsched_new_buf(struct buf *bp)
1103 struct dsched_thread_ctx *tdctx = NULL;
1105 if (dsched_inited == 0)
1108 if (curproc != NULL) {
1109 tdctx = dsched_get_proc_priv(curproc);
1111 /* This is a kernel thread, so no proc info is available */
1112 tdctx = dsched_get_thread_priv(curthread);
1117 * XXX: hack. we don't want this assert because we aren't catching all
1118 * threads. mi_startup() is still getting away without an tdctx.
1121 /* by now we should have an tdctx. if not, something bad is going on */
1122 KKASSERT(tdctx != NULL);
1126 dsched_thread_ctx_ref(tdctx);
1128 dsched_set_buf_priv(bp, tdctx);
1132 dsched_exit_buf(struct buf *bp)
1134 struct dsched_thread_ctx *tdctx;
1136 tdctx = dsched_get_buf_priv(bp);
1137 if (tdctx != NULL) {
1138 dsched_clr_buf_priv(bp);
1139 dsched_thread_ctx_unref(tdctx);
1144 dsched_new_proc(struct proc *p)
1146 struct dsched_thread_ctx *tdctx;
1148 if (dsched_inited == 0)
1151 KKASSERT(p != NULL);
1153 tdctx = dsched_thread_ctx_alloc(p);
1155 dsched_thread_ctx_ref(tdctx);
1157 dsched_set_proc_priv(p, tdctx);
1158 atomic_add_int(&dsched_stats.nprocs, 1);
1163 dsched_new_thread(struct thread *td)
1165 struct dsched_thread_ctx *tdctx;
1167 if (dsched_inited == 0)
1170 KKASSERT(td != NULL);
1172 tdctx = dsched_thread_ctx_alloc(NULL);
1174 dsched_thread_ctx_ref(tdctx);
1176 dsched_set_thread_priv(td, tdctx);
1177 atomic_add_int(&dsched_stats.nthreads, 1);
1181 dsched_exit_proc(struct proc *p)
1183 struct dsched_thread_ctx *tdctx;
1185 if (dsched_inited == 0)
1188 KKASSERT(p != NULL);
1190 tdctx = dsched_get_proc_priv(p);
1191 KKASSERT(tdctx != NULL);
1193 tdctx->dead = 0xDEAD;
1194 dsched_set_proc_priv(p, NULL);
1196 dsched_thread_ctx_unref(tdctx); /* one for alloc, */
1197 dsched_thread_ctx_unref(tdctx); /* one for ref */
1198 atomic_subtract_int(&dsched_stats.nprocs, 1);
1203 dsched_exit_thread(struct thread *td)
1205 struct dsched_thread_ctx *tdctx;
1207 if (dsched_inited == 0)
1210 KKASSERT(td != NULL);
1212 tdctx = dsched_get_thread_priv(td);
1213 KKASSERT(tdctx != NULL);
1215 tdctx->dead = 0xDEAD;
1216 dsched_set_thread_priv(td, 0);
1218 dsched_thread_ctx_unref(tdctx); /* one for alloc, */
1219 dsched_thread_ctx_unref(tdctx); /* one for ref */
1220 atomic_subtract_int(&dsched_stats.nthreads, 1);
1224 * Returns ref'd tdio.
1226 * tdio may have additional refs for the diskctx and tdctx it resides on.
1229 dsched_new_policy_thread_tdio(struct dsched_disk_ctx *diskctx,
1230 struct dsched_policy *pol)
1232 struct dsched_thread_ctx *tdctx;
1234 tdctx = dsched_get_thread_priv(curthread);
1235 KKASSERT(tdctx != NULL);
1236 dsched_thread_io_alloc(diskctx->dp, tdctx, pol, 0);
1239 /* DEFAULT NOOP POLICY */
1242 noop_prepare(struct dsched_disk_ctx *diskctx)
1248 noop_teardown(struct dsched_disk_ctx *diskctx)
1254 noop_cancel(struct dsched_disk_ctx *diskctx)
1260 noop_queue(struct dsched_disk_ctx *diskctx, struct dsched_thread_io *tdio,
1263 dsched_strategy_raw(diskctx->dp, bio);
1265 dsched_strategy_async(diskctx->dp, bio, noop_completed, NULL);
1276 dsched_tdio_cache = objcache_create("dsched-tdio-cache", 0, 0,
1278 objcache_malloc_alloc,
1279 objcache_malloc_free,
1280 &dsched_thread_io_malloc_args );
1282 dsched_tdctx_cache = objcache_create("dsched-tdctx-cache", 0, 0,
1284 objcache_malloc_alloc,
1285 objcache_malloc_free,
1286 &dsched_thread_ctx_malloc_args );
1288 dsched_diskctx_cache = objcache_create("dsched-diskctx-cache", 0, 0,
1290 objcache_malloc_alloc,
1291 objcache_malloc_free,
1292 &dsched_disk_ctx_malloc_args );
1294 bzero(&dsched_stats, sizeof(struct dsched_stats));
1296 lockinit(&dsched_lock, "dsched lock", 0, LK_CANRECURSE);
1297 DSCHED_GLOBAL_THREAD_CTX_LOCKINIT();
1299 dsched_register(&dsched_noop_policy);
1309 SYSINIT(subr_dsched_register, SI_SUB_CREATE_INIT-1, SI_ORDER_FIRST, dsched_init, NULL);
1310 SYSUNINIT(subr_dsched_register, SI_SUB_CREATE_INIT-1, SI_ORDER_ANY, dsched_uninit, NULL);
1316 sysctl_dsched_stats(SYSCTL_HANDLER_ARGS)
1318 return (sysctl_handle_opaque(oidp, &dsched_stats, sizeof(struct dsched_stats), req));
1322 sysctl_dsched_list_policies(SYSCTL_HANDLER_ARGS)
1324 struct dsched_policy *pol = NULL;
1325 int error, first = 1;
1327 lockmgr(&dsched_lock, LK_EXCLUSIVE);
1329 while ((pol = dsched_policy_enumerate(pol))) {
1331 error = SYSCTL_OUT(req, " ", 1);
1337 error = SYSCTL_OUT(req, pol->name, strlen(pol->name));
1343 lockmgr(&dsched_lock, LK_RELEASE);
1345 error = SYSCTL_OUT(req, "", 1);
1351 sysctl_dsched_policy(SYSCTL_HANDLER_ARGS)
1353 char buf[DSCHED_POLICY_NAME_LENGTH];
1354 struct dsched_disk_ctx *diskctx = arg1;
1355 struct dsched_policy *pol = NULL;
1358 if (diskctx == NULL) {
1362 lockmgr(&dsched_lock, LK_EXCLUSIVE);
1364 pol = diskctx->dp->d_sched_policy;
1365 memcpy(buf, pol->name, DSCHED_POLICY_NAME_LENGTH);
1367 error = sysctl_handle_string(oidp, buf, DSCHED_POLICY_NAME_LENGTH, req);
1368 if (error || req->newptr == NULL) {
1369 lockmgr(&dsched_lock, LK_RELEASE);
1373 pol = dsched_find_policy(buf);
1375 lockmgr(&dsched_lock, LK_RELEASE);
1379 dsched_switch(diskctx->dp, pol);
1381 lockmgr(&dsched_lock, LK_RELEASE);
1387 sysctl_dsched_default_policy(SYSCTL_HANDLER_ARGS)
1389 char buf[DSCHED_POLICY_NAME_LENGTH];
1390 struct dsched_policy *pol = NULL;
1393 lockmgr(&dsched_lock, LK_EXCLUSIVE);
1395 pol = default_policy;
1396 memcpy(buf, pol->name, DSCHED_POLICY_NAME_LENGTH);
1398 error = sysctl_handle_string(oidp, buf, DSCHED_POLICY_NAME_LENGTH, req);
1399 if (error || req->newptr == NULL) {
1400 lockmgr(&dsched_lock, LK_RELEASE);
1404 pol = dsched_find_policy(buf);
1406 lockmgr(&dsched_lock, LK_RELEASE);
1411 default_policy = pol;
1413 lockmgr(&dsched_lock, LK_RELEASE);
1418 SYSCTL_NODE(, OID_AUTO, dsched, CTLFLAG_RD, NULL,
1419 "Disk Scheduler Framework (dsched) magic");
1420 SYSCTL_NODE(_dsched, OID_AUTO, policy, CTLFLAG_RW, NULL,
1421 "List of disks and their policies");
1422 SYSCTL_INT(_dsched, OID_AUTO, debug, CTLFLAG_RW, &dsched_debug_enable,
1423 0, "Enable dsched debugging");
1424 SYSCTL_PROC(_dsched, OID_AUTO, stats, CTLTYPE_OPAQUE|CTLFLAG_RD,
1425 0, sizeof(struct dsched_stats), sysctl_dsched_stats, "dsched_stats",
1426 "dsched statistics");
1427 SYSCTL_PROC(_dsched, OID_AUTO, policies, CTLTYPE_STRING|CTLFLAG_RD,
1428 NULL, 0, sysctl_dsched_list_policies, "A", "names of available policies");
1429 SYSCTL_PROC(_dsched_policy, OID_AUTO, default, CTLTYPE_STRING|CTLFLAG_RW,
1430 NULL, 0, sysctl_dsched_default_policy, "A", "default dsched policy");
1433 dsched_sysctl_add_disk(struct dsched_disk_ctx *diskctx, char *name)
1435 if (!(diskctx->flags & DSCHED_SYSCTL_CTX_INITED)) {
1436 diskctx->flags |= DSCHED_SYSCTL_CTX_INITED;
1437 sysctl_ctx_init(&diskctx->sysctl_ctx);
1440 SYSCTL_ADD_PROC(&diskctx->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_dsched_policy),
1441 OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RW,
1442 diskctx, 0, sysctl_dsched_policy, "A", "policy");