Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * CFQ, or complete fairness queueing, disk scheduler. |
| 3 | * |
| 4 | * Based on ideas from a previously unfinished io |
| 5 | * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli. |
| 6 | * |
| 7 | * Copyright (C) 2003 Jens Axboe <axboe@suse.de> |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/config.h> |
| 10 | #include <linux/module.h> |
Al Viro | 1cc9be6 | 2006-03-18 12:29:52 -0500 | [diff] [blame] | 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/elevator.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/hash.h> |
| 14 | #include <linux/rbtree.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 15 | #include <linux/ioprio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * tunables |
| 19 | */ |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 20 | static const int cfq_quantum = 4; /* max queue in one round of service */ |
| 21 | static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/ |
| 22 | static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 }; |
| 23 | static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */ |
| 24 | static const int cfq_back_penalty = 2; /* penalty of a backwards seek */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 26 | static const int cfq_slice_sync = HZ / 10; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 27 | static int cfq_slice_async = HZ / 25; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 28 | static const int cfq_slice_async_rq = 2; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 29 | static int cfq_slice_idle = HZ / 100; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 30 | |
| 31 | #define CFQ_IDLE_GRACE (HZ / 10) |
| 32 | #define CFQ_SLICE_SCALE (5) |
| 33 | |
| 34 | #define CFQ_KEY_ASYNC (0) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 35 | #define CFQ_KEY_ANY (0xffff) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * disable queueing at the driver/hardware level |
| 39 | */ |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 40 | static const int cfq_max_depth = 2; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 41 | |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 42 | static DEFINE_RWLOCK(cfq_exit_lock); |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * for the hash of cfqq inside the cfqd |
| 46 | */ |
| 47 | #define CFQ_QHASH_SHIFT 6 |
| 48 | #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT) |
| 49 | #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash) |
| 50 | |
| 51 | /* |
| 52 | * for the hash of crq inside the cfqq |
| 53 | */ |
| 54 | #define CFQ_MHASH_SHIFT 6 |
| 55 | #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3) |
| 56 | #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT) |
| 57 | #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT) |
| 58 | #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors) |
| 59 | #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash) |
| 60 | |
| 61 | #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 62 | #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | #define RQ_DATA(rq) (rq)->elevator_private |
| 65 | |
| 66 | /* |
| 67 | * rb-tree defines |
| 68 | */ |
| 69 | #define RB_NONE (2) |
| 70 | #define RB_EMPTY(node) ((node)->rb_node == NULL) |
| 71 | #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE |
| 72 | #define RB_CLEAR(node) do { \ |
| 73 | (node)->rb_parent = NULL; \ |
| 74 | RB_CLEAR_COLOR((node)); \ |
| 75 | (node)->rb_right = NULL; \ |
| 76 | (node)->rb_left = NULL; \ |
| 77 | } while (0) |
| 78 | #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node) |
| 80 | #define rq_rb_key(rq) (rq)->sector |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | static kmem_cache_t *crq_pool; |
| 83 | static kmem_cache_t *cfq_pool; |
| 84 | static kmem_cache_t *cfq_ioc_pool; |
| 85 | |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 86 | static atomic_t ioc_count = ATOMIC_INIT(0); |
| 87 | static struct completion *ioc_gone; |
| 88 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 89 | #define CFQ_PRIO_LISTS IOPRIO_BE_NR |
| 90 | #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) |
| 91 | #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE) |
| 92 | #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) |
| 93 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 94 | #define ASYNC (0) |
| 95 | #define SYNC (1) |
| 96 | |
| 97 | #define cfq_cfqq_dispatched(cfqq) \ |
| 98 | ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC]) |
| 99 | |
| 100 | #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC) |
| 101 | |
| 102 | #define cfq_cfqq_sync(cfqq) \ |
| 103 | (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC]) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Per block device queue structure |
| 107 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | struct cfq_data { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 109 | request_queue_t *queue; |
| 110 | |
| 111 | /* |
| 112 | * rr list of queues with requests and the count of them |
| 113 | */ |
| 114 | struct list_head rr_list[CFQ_PRIO_LISTS]; |
| 115 | struct list_head busy_rr; |
| 116 | struct list_head cur_rr; |
| 117 | struct list_head idle_rr; |
| 118 | unsigned int busy_queues; |
| 119 | |
| 120 | /* |
| 121 | * non-ordered list of empty cfqq's |
| 122 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct list_head empty_list; |
| 124 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 125 | /* |
| 126 | * cfqq lookup hash |
| 127 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct hlist_head *cfq_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 130 | /* |
| 131 | * global crq hash for all queues |
| 132 | */ |
| 133 | struct hlist_head *crq_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | unsigned int max_queued; |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | mempool_t *crq_pool; |
| 138 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 139 | int rq_in_driver; |
| 140 | |
| 141 | /* |
| 142 | * schedule slice state info |
| 143 | */ |
| 144 | /* |
| 145 | * idle window management |
| 146 | */ |
| 147 | struct timer_list idle_slice_timer; |
| 148 | struct work_struct unplug_work; |
| 149 | |
| 150 | struct cfq_queue *active_queue; |
| 151 | struct cfq_io_context *active_cic; |
| 152 | int cur_prio, cur_end_prio; |
| 153 | unsigned int dispatch_slice; |
| 154 | |
| 155 | struct timer_list idle_class_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | sector_t last_sector; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 158 | unsigned long last_end_request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 160 | unsigned int rq_starved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * tunables, see top of file |
| 164 | */ |
| 165 | unsigned int cfq_quantum; |
| 166 | unsigned int cfq_queued; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 167 | unsigned int cfq_fifo_expire[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | unsigned int cfq_back_penalty; |
| 169 | unsigned int cfq_back_max; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 170 | unsigned int cfq_slice[2]; |
| 171 | unsigned int cfq_slice_async_rq; |
| 172 | unsigned int cfq_slice_idle; |
| 173 | unsigned int cfq_max_depth; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 174 | |
| 175 | struct list_head cic_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Per process-grouping structure |
| 180 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | struct cfq_queue { |
| 182 | /* reference count */ |
| 183 | atomic_t ref; |
| 184 | /* parent cfq_data */ |
| 185 | struct cfq_data *cfqd; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 186 | /* cfqq lookup hash */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | struct hlist_node cfq_hash; |
| 188 | /* hash key */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 189 | unsigned int key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* on either rr or empty list of cfqd */ |
| 191 | struct list_head cfq_list; |
| 192 | /* sorted list of pending requests */ |
| 193 | struct rb_root sort_list; |
| 194 | /* if fifo isn't expired, next request to serve */ |
| 195 | struct cfq_rq *next_crq; |
| 196 | /* requests queued in sort_list */ |
| 197 | int queued[2]; |
| 198 | /* currently allocated requests */ |
| 199 | int allocated[2]; |
| 200 | /* fifo list of requests in sort_list */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 201 | struct list_head fifo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 203 | unsigned long slice_start; |
| 204 | unsigned long slice_end; |
| 205 | unsigned long slice_left; |
| 206 | unsigned long service_last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 208 | /* number of requests that are on the dispatch list */ |
| 209 | int on_dispatch[2]; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 210 | |
| 211 | /* io prio of this group */ |
| 212 | unsigned short ioprio, org_ioprio; |
| 213 | unsigned short ioprio_class, org_ioprio_class; |
| 214 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 215 | /* various state flags, see below */ |
| 216 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | struct cfq_rq { |
| 220 | struct rb_node rb_node; |
| 221 | sector_t rb_key; |
| 222 | struct request *request; |
| 223 | struct hlist_node hash; |
| 224 | |
| 225 | struct cfq_queue *cfq_queue; |
| 226 | struct cfq_io_context *io_context; |
| 227 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 228 | unsigned int crq_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 231 | enum cfqq_state_flags { |
| 232 | CFQ_CFQQ_FLAG_on_rr = 0, |
| 233 | CFQ_CFQQ_FLAG_wait_request, |
| 234 | CFQ_CFQQ_FLAG_must_alloc, |
| 235 | CFQ_CFQQ_FLAG_must_alloc_slice, |
| 236 | CFQ_CFQQ_FLAG_must_dispatch, |
| 237 | CFQ_CFQQ_FLAG_fifo_expire, |
| 238 | CFQ_CFQQ_FLAG_idle_window, |
| 239 | CFQ_CFQQ_FLAG_prio_changed, |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | #define CFQ_CFQQ_FNS(name) \ |
| 243 | static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ |
| 244 | { \ |
| 245 | cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ |
| 246 | } \ |
| 247 | static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ |
| 248 | { \ |
| 249 | cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ |
| 250 | } \ |
| 251 | static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ |
| 252 | { \ |
| 253 | return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ |
| 254 | } |
| 255 | |
| 256 | CFQ_CFQQ_FNS(on_rr); |
| 257 | CFQ_CFQQ_FNS(wait_request); |
| 258 | CFQ_CFQQ_FNS(must_alloc); |
| 259 | CFQ_CFQQ_FNS(must_alloc_slice); |
| 260 | CFQ_CFQQ_FNS(must_dispatch); |
| 261 | CFQ_CFQQ_FNS(fifo_expire); |
| 262 | CFQ_CFQQ_FNS(idle_window); |
| 263 | CFQ_CFQQ_FNS(prio_changed); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 264 | #undef CFQ_CFQQ_FNS |
| 265 | |
| 266 | enum cfq_rq_state_flags { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 267 | CFQ_CRQ_FLAG_is_sync = 0, |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | #define CFQ_CRQ_FNS(name) \ |
| 271 | static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \ |
| 272 | { \ |
| 273 | crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \ |
| 274 | } \ |
| 275 | static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \ |
| 276 | { \ |
| 277 | crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \ |
| 278 | } \ |
| 279 | static inline int cfq_crq_##name(const struct cfq_rq *crq) \ |
| 280 | { \ |
| 281 | return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \ |
| 282 | } |
| 283 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 284 | CFQ_CRQ_FNS(is_sync); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 285 | #undef CFQ_CRQ_FNS |
| 286 | |
| 287 | static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 288 | static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *); |
Al Viro | 6f325a1 | 2006-03-18 14:58:37 -0500 | [diff] [blame] | 289 | static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 291 | #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * lots of deadline iosched dupes, can be abstracted later... |
| 295 | */ |
| 296 | static inline void cfq_del_crq_hash(struct cfq_rq *crq) |
| 297 | { |
| 298 | hlist_del_init(&crq->hash); |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq) |
| 302 | { |
| 303 | const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request)); |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]); |
| 306 | } |
| 307 | |
| 308 | static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset) |
| 309 | { |
| 310 | struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)]; |
| 311 | struct hlist_node *entry, *next; |
| 312 | |
| 313 | hlist_for_each_safe(entry, next, hash_list) { |
| 314 | struct cfq_rq *crq = list_entry_hash(entry); |
| 315 | struct request *__rq = crq->request; |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if (!rq_mergeable(__rq)) { |
| 318 | cfq_del_crq_hash(crq); |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | if (rq_hash_key(__rq) == offset) |
| 323 | return __rq; |
| 324 | } |
| 325 | |
| 326 | return NULL; |
| 327 | } |
| 328 | |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 329 | /* |
| 330 | * scheduler run of queue, if there are requests pending and no one in the |
| 331 | * driver that will restart queueing |
| 332 | */ |
| 333 | static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) |
| 334 | { |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 335 | if (cfqd->busy_queues) |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 336 | kblockd_schedule_work(&cfqd->unplug_work); |
| 337 | } |
| 338 | |
| 339 | static int cfq_queue_empty(request_queue_t *q) |
| 340 | { |
| 341 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 342 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 343 | return !cfqd->busy_queues; |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /* |
| 347 | * Lifted from AS - choose which of crq1 and crq2 that is best served now. |
| 348 | * We choose the request that is closest to the head right now. Distance |
| 349 | * behind the head are penalized and only allowed to a certain extent. |
| 350 | */ |
| 351 | static struct cfq_rq * |
| 352 | cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2) |
| 353 | { |
| 354 | sector_t last, s1, s2, d1 = 0, d2 = 0; |
| 355 | int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */ |
| 356 | unsigned long back_max; |
| 357 | |
| 358 | if (crq1 == NULL || crq1 == crq2) |
| 359 | return crq2; |
| 360 | if (crq2 == NULL) |
| 361 | return crq1; |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 362 | |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 363 | if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2)) |
| 364 | return crq1; |
| 365 | else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 366 | return crq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | s1 = crq1->request->sector; |
| 369 | s2 = crq2->request->sector; |
| 370 | |
| 371 | last = cfqd->last_sector; |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* |
| 374 | * by definition, 1KiB is 2 sectors |
| 375 | */ |
| 376 | back_max = cfqd->cfq_back_max * 2; |
| 377 | |
| 378 | /* |
| 379 | * Strict one way elevator _except_ in the case where we allow |
| 380 | * short backward seeks which are biased as twice the cost of a |
| 381 | * similar forward seek. |
| 382 | */ |
| 383 | if (s1 >= last) |
| 384 | d1 = s1 - last; |
| 385 | else if (s1 + back_max >= last) |
| 386 | d1 = (last - s1) * cfqd->cfq_back_penalty; |
| 387 | else |
| 388 | r1_wrap = 1; |
| 389 | |
| 390 | if (s2 >= last) |
| 391 | d2 = s2 - last; |
| 392 | else if (s2 + back_max >= last) |
| 393 | d2 = (last - s2) * cfqd->cfq_back_penalty; |
| 394 | else |
| 395 | r2_wrap = 1; |
| 396 | |
| 397 | /* Found required data */ |
| 398 | if (!r1_wrap && r2_wrap) |
| 399 | return crq1; |
| 400 | else if (!r2_wrap && r1_wrap) |
| 401 | return crq2; |
| 402 | else if (r1_wrap && r2_wrap) { |
| 403 | /* both behind the head */ |
| 404 | if (s1 <= s2) |
| 405 | return crq1; |
| 406 | else |
| 407 | return crq2; |
| 408 | } |
| 409 | |
| 410 | /* Both requests in front of the head */ |
| 411 | if (d1 < d2) |
| 412 | return crq1; |
| 413 | else if (d2 < d1) |
| 414 | return crq2; |
| 415 | else { |
| 416 | if (s1 >= s2) |
| 417 | return crq1; |
| 418 | else |
| 419 | return crq2; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * would be nice to take fifo expire time into account as well |
| 425 | */ |
| 426 | static struct cfq_rq * |
| 427 | cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 428 | struct cfq_rq *last) |
| 429 | { |
| 430 | struct cfq_rq *crq_next = NULL, *crq_prev = NULL; |
| 431 | struct rb_node *rbnext, *rbprev; |
| 432 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 433 | if (!(rbnext = rb_next(&last->rb_node))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | rbnext = rb_first(&cfqq->sort_list); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 435 | if (rbnext == &last->rb_node) |
| 436 | rbnext = NULL; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | rbprev = rb_prev(&last->rb_node); |
| 440 | |
| 441 | if (rbprev) |
| 442 | crq_prev = rb_entry_crq(rbprev); |
| 443 | if (rbnext) |
| 444 | crq_next = rb_entry_crq(rbnext); |
| 445 | |
| 446 | return cfq_choose_req(cfqd, crq_next, crq_prev); |
| 447 | } |
| 448 | |
| 449 | static void cfq_update_next_crq(struct cfq_rq *crq) |
| 450 | { |
| 451 | struct cfq_queue *cfqq = crq->cfq_queue; |
| 452 | |
| 453 | if (cfqq->next_crq == crq) |
| 454 | cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq); |
| 455 | } |
| 456 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 457 | static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 459 | struct cfq_data *cfqd = cfqq->cfqd; |
| 460 | struct list_head *list, *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 462 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | list_del(&cfqq->cfq_list); |
| 465 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 466 | if (cfq_class_rt(cfqq)) |
| 467 | list = &cfqd->cur_rr; |
| 468 | else if (cfq_class_idle(cfqq)) |
| 469 | list = &cfqd->idle_rr; |
| 470 | else { |
| 471 | /* |
| 472 | * if cfqq has requests in flight, don't allow it to be |
| 473 | * found in cfq_set_active_queue before it has finished them. |
| 474 | * this is done to increase fairness between a process that |
| 475 | * has lots of io pending vs one that only generates one |
| 476 | * sporadically or synchronously |
| 477 | */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 478 | if (cfq_cfqq_dispatched(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 479 | list = &cfqd->busy_rr; |
| 480 | else |
| 481 | list = &cfqd->rr_list[cfqq->ioprio]; |
| 482 | } |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 485 | * if queue was preempted, just add to front to be fair. busy_rr |
| 486 | * isn't sorted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 488 | if (preempted || list == &cfqd->busy_rr) { |
| 489 | list_add(&cfqq->cfq_list, list); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * sort by when queue was last serviced |
| 495 | */ |
| 496 | entry = list; |
| 497 | while ((entry = entry->prev) != list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | struct cfq_queue *__cfqq = list_entry_cfqq(entry); |
| 499 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 500 | if (!__cfqq->service_last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 502 | if (time_before(__cfqq->service_last, cfqq->service_last)) |
| 503 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | list_add(&cfqq->cfq_list, entry); |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * add to busy list of queues for service, trying to be fair in ordering |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 511 | * the pending list according to last request service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | */ |
| 513 | static inline void |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 514 | cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 516 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
| 517 | cfq_mark_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | cfqd->busy_queues++; |
| 519 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 520 | cfq_resort_rr_list(cfqq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static inline void |
| 524 | cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 525 | { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 526 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
| 527 | cfq_clear_cfqq_on_rr(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 528 | list_move(&cfqq->cfq_list, &cfqd->empty_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | BUG_ON(!cfqd->busy_queues); |
| 531 | cfqd->busy_queues--; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * rb tree support functions |
| 536 | */ |
| 537 | static inline void cfq_del_crq_rb(struct cfq_rq *crq) |
| 538 | { |
| 539 | struct cfq_queue *cfqq = crq->cfq_queue; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 540 | struct cfq_data *cfqd = cfqq->cfqd; |
| 541 | const int sync = cfq_crq_is_sync(crq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 543 | BUG_ON(!cfqq->queued[sync]); |
| 544 | cfqq->queued[sync]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 546 | cfq_update_next_crq(crq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 548 | rb_erase(&crq->rb_node, &cfqq->sort_list); |
| 549 | RB_CLEAR_COLOR(&crq->rb_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 551 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list)) |
| 552 | cfq_del_cfqq_rr(cfqd, cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static struct cfq_rq * |
| 556 | __cfq_add_crq_rb(struct cfq_rq *crq) |
| 557 | { |
| 558 | struct rb_node **p = &crq->cfq_queue->sort_list.rb_node; |
| 559 | struct rb_node *parent = NULL; |
| 560 | struct cfq_rq *__crq; |
| 561 | |
| 562 | while (*p) { |
| 563 | parent = *p; |
| 564 | __crq = rb_entry_crq(parent); |
| 565 | |
| 566 | if (crq->rb_key < __crq->rb_key) |
| 567 | p = &(*p)->rb_left; |
| 568 | else if (crq->rb_key > __crq->rb_key) |
| 569 | p = &(*p)->rb_right; |
| 570 | else |
| 571 | return __crq; |
| 572 | } |
| 573 | |
| 574 | rb_link_node(&crq->rb_node, parent, p); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | static void cfq_add_crq_rb(struct cfq_rq *crq) |
| 579 | { |
| 580 | struct cfq_queue *cfqq = crq->cfq_queue; |
| 581 | struct cfq_data *cfqd = cfqq->cfqd; |
| 582 | struct request *rq = crq->request; |
| 583 | struct cfq_rq *__alias; |
| 584 | |
| 585 | crq->rb_key = rq_rb_key(rq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 586 | cfqq->queued[cfq_crq_is_sync(crq)]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * looks a little odd, but the first insert might return an alias. |
| 590 | * if that happens, put the alias on the dispatch list |
| 591 | */ |
| 592 | while ((__alias = __cfq_add_crq_rb(crq)) != NULL) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 593 | cfq_dispatch_insert(cfqd->queue, __alias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
| 595 | rb_insert_color(&crq->rb_node, &cfqq->sort_list); |
| 596 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 597 | if (!cfq_cfqq_on_rr(cfqq)) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 598 | cfq_add_cfqq_rr(cfqd, cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * check if this request is a better next-serve candidate |
| 602 | */ |
| 603 | cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq); |
| 604 | } |
| 605 | |
| 606 | static inline void |
| 607 | cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq) |
| 608 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 609 | rb_erase(&crq->rb_node, &cfqq->sort_list); |
| 610 | cfqq->queued[cfq_crq_is_sync(crq)]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | cfq_add_crq_rb(crq); |
| 613 | } |
| 614 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 615 | static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector) |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 618 | struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | struct rb_node *n; |
| 620 | |
| 621 | if (!cfqq) |
| 622 | goto out; |
| 623 | |
| 624 | n = cfqq->sort_list.rb_node; |
| 625 | while (n) { |
| 626 | struct cfq_rq *crq = rb_entry_crq(n); |
| 627 | |
| 628 | if (sector < crq->rb_key) |
| 629 | n = n->rb_left; |
| 630 | else if (sector > crq->rb_key) |
| 631 | n = n->rb_right; |
| 632 | else |
| 633 | return crq->request; |
| 634 | } |
| 635 | |
| 636 | out: |
| 637 | return NULL; |
| 638 | } |
| 639 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 640 | static void cfq_activate_request(request_queue_t *q, struct request *rq) |
| 641 | { |
| 642 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 643 | |
| 644 | cfqd->rq_in_driver++; |
| 645 | } |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | static void cfq_deactivate_request(request_queue_t *q, struct request *rq) |
| 648 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 649 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 651 | WARN_ON(!cfqd->rq_in_driver); |
| 652 | cfqd->rq_in_driver--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 655 | static void cfq_remove_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct cfq_rq *crq = RQ_DATA(rq); |
| 658 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 659 | list_del_init(&rq->queuelist); |
| 660 | cfq_del_crq_rb(crq); |
Tejun Heo | 98b1147 | 2005-10-20 16:46:54 +0200 | [diff] [blame] | 661 | cfq_del_crq_hash(crq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static int |
| 665 | cfq_merge(request_queue_t *q, struct request **req, struct bio *bio) |
| 666 | { |
| 667 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 668 | struct request *__rq; |
| 669 | int ret; |
| 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | __rq = cfq_find_rq_hash(cfqd, bio->bi_sector); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 672 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
| 673 | ret = ELEVATOR_BACK_MERGE; |
| 674 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 678 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
| 679 | ret = ELEVATOR_FRONT_MERGE; |
| 680 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | return ELEVATOR_NO_MERGE; |
| 684 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | *req = __rq; |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | static void cfq_merged_request(request_queue_t *q, struct request *req) |
| 690 | { |
| 691 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 692 | struct cfq_rq *crq = RQ_DATA(req); |
| 693 | |
| 694 | cfq_del_crq_hash(crq); |
| 695 | cfq_add_crq_hash(cfqd, crq); |
| 696 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 697 | if (rq_rb_key(req) != crq->rb_key) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | struct cfq_queue *cfqq = crq->cfq_queue; |
| 699 | |
| 700 | cfq_update_next_crq(crq); |
| 701 | cfq_reposition_crq_rb(cfqq, crq); |
| 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static void |
| 706 | cfq_merged_requests(request_queue_t *q, struct request *rq, |
| 707 | struct request *next) |
| 708 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | cfq_merged_request(q, rq); |
| 710 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 711 | /* |
| 712 | * reposition in fifo if next is older than rq |
| 713 | */ |
| 714 | if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && |
| 715 | time_before(next->start_time, rq->start_time)) |
| 716 | list_move(&rq->queuelist, &next->queuelist); |
| 717 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 718 | cfq_remove_request(next); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static inline void |
| 722 | __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 723 | { |
| 724 | if (cfqq) { |
| 725 | /* |
| 726 | * stop potential idle class queues waiting service |
| 727 | */ |
| 728 | del_timer(&cfqd->idle_class_timer); |
| 729 | |
| 730 | cfqq->slice_start = jiffies; |
| 731 | cfqq->slice_end = 0; |
| 732 | cfqq->slice_left = 0; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 733 | cfq_clear_cfqq_must_alloc_slice(cfqq); |
| 734 | cfq_clear_cfqq_fifo_expire(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | cfqd->active_queue = cfqq; |
| 738 | } |
| 739 | |
| 740 | /* |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 741 | * current cfqq expired its slice (or was too idle), select new one |
| 742 | */ |
| 743 | static void |
| 744 | __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 745 | int preempted) |
| 746 | { |
| 747 | unsigned long now = jiffies; |
| 748 | |
| 749 | if (cfq_cfqq_wait_request(cfqq)) |
| 750 | del_timer(&cfqd->idle_slice_timer); |
| 751 | |
| 752 | if (!preempted && !cfq_cfqq_dispatched(cfqq)) { |
| 753 | cfqq->service_last = now; |
| 754 | cfq_schedule_dispatch(cfqd); |
| 755 | } |
| 756 | |
| 757 | cfq_clear_cfqq_must_dispatch(cfqq); |
| 758 | cfq_clear_cfqq_wait_request(cfqq); |
| 759 | |
| 760 | /* |
| 761 | * store what was left of this slice, if the queue idled out |
| 762 | * or was preempted |
| 763 | */ |
| 764 | if (time_after(cfqq->slice_end, now)) |
| 765 | cfqq->slice_left = cfqq->slice_end - now; |
| 766 | else |
| 767 | cfqq->slice_left = 0; |
| 768 | |
| 769 | if (cfq_cfqq_on_rr(cfqq)) |
| 770 | cfq_resort_rr_list(cfqq, preempted); |
| 771 | |
| 772 | if (cfqq == cfqd->active_queue) |
| 773 | cfqd->active_queue = NULL; |
| 774 | |
| 775 | if (cfqd->active_cic) { |
| 776 | put_io_context(cfqd->active_cic->ioc); |
| 777 | cfqd->active_cic = NULL; |
| 778 | } |
| 779 | |
| 780 | cfqd->dispatch_slice = 0; |
| 781 | } |
| 782 | |
| 783 | static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted) |
| 784 | { |
| 785 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 786 | |
| 787 | if (cfqq) |
| 788 | __cfq_slice_expired(cfqd, cfqq, preempted); |
| 789 | } |
| 790 | |
| 791 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 792 | * 0 |
| 793 | * 0,1 |
| 794 | * 0,1,2 |
| 795 | * 0,1,2,3 |
| 796 | * 0,1,2,3,4 |
| 797 | * 0,1,2,3,4,5 |
| 798 | * 0,1,2,3,4,5,6 |
| 799 | * 0,1,2,3,4,5,6,7 |
| 800 | */ |
| 801 | static int cfq_get_next_prio_level(struct cfq_data *cfqd) |
| 802 | { |
| 803 | int prio, wrap; |
| 804 | |
| 805 | prio = -1; |
| 806 | wrap = 0; |
| 807 | do { |
| 808 | int p; |
| 809 | |
| 810 | for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) { |
| 811 | if (!list_empty(&cfqd->rr_list[p])) { |
| 812 | prio = p; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if (prio != -1) |
| 818 | break; |
| 819 | cfqd->cur_prio = 0; |
| 820 | if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) { |
| 821 | cfqd->cur_end_prio = 0; |
| 822 | if (wrap) |
| 823 | break; |
| 824 | wrap = 1; |
| 825 | } |
| 826 | } while (1); |
| 827 | |
| 828 | if (unlikely(prio == -1)) |
| 829 | return -1; |
| 830 | |
| 831 | BUG_ON(prio >= CFQ_PRIO_LISTS); |
| 832 | |
| 833 | list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr); |
| 834 | |
| 835 | cfqd->cur_prio = prio + 1; |
| 836 | if (cfqd->cur_prio > cfqd->cur_end_prio) { |
| 837 | cfqd->cur_end_prio = cfqd->cur_prio; |
| 838 | cfqd->cur_prio = 0; |
| 839 | } |
| 840 | if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) { |
| 841 | cfqd->cur_prio = 0; |
| 842 | cfqd->cur_end_prio = 0; |
| 843 | } |
| 844 | |
| 845 | return prio; |
| 846 | } |
| 847 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 848 | static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 849 | { |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 850 | struct cfq_queue *cfqq = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | * if current list is non-empty, grab first entry. if it is empty, |
| 854 | * get next prio level and grab first entry then if any are spliced |
| 855 | */ |
| 856 | if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) |
| 857 | cfqq = list_entry_cfqq(cfqd->cur_rr.next); |
| 858 | |
| 859 | /* |
| 860 | * if we have idle queues and no rt or be queues had pending |
| 861 | * requests, either allow immediate service if the grace period |
| 862 | * has passed or arm the idle grace timer |
| 863 | */ |
| 864 | if (!cfqq && !list_empty(&cfqd->idle_rr)) { |
| 865 | unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE; |
| 866 | |
| 867 | if (time_after_eq(jiffies, end)) |
| 868 | cfqq = list_entry_cfqq(cfqd->idle_rr.next); |
| 869 | else |
| 870 | mod_timer(&cfqd->idle_class_timer, end); |
| 871 | } |
| 872 | |
| 873 | __cfq_set_active_queue(cfqd, cfqq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 874 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 875 | } |
| 876 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 877 | static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 878 | |
| 879 | { |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 880 | unsigned long sl; |
| 881 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 882 | WARN_ON(!RB_EMPTY(&cfqq->sort_list)); |
| 883 | WARN_ON(cfqq != cfqd->active_queue); |
| 884 | |
| 885 | /* |
| 886 | * idle is disabled, either manually or by past process history |
| 887 | */ |
| 888 | if (!cfqd->cfq_slice_idle) |
| 889 | return 0; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 890 | if (!cfq_cfqq_idle_window(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 891 | return 0; |
| 892 | /* |
| 893 | * task has exited, don't wait |
| 894 | */ |
| 895 | if (cfqd->active_cic && !cfqd->active_cic->ioc->task) |
| 896 | return 0; |
| 897 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 898 | cfq_mark_cfqq_must_dispatch(cfqq); |
| 899 | cfq_mark_cfqq_wait_request(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 900 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 901 | sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle); |
| 902 | mod_timer(&cfqd->idle_slice_timer, jiffies + sl); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 903 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 906 | static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
| 908 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 909 | struct cfq_queue *cfqq = crq->cfq_queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 910 | |
| 911 | cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 912 | cfq_remove_request(crq->request); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 913 | cfqq->on_dispatch[cfq_crq_is_sync(crq)]++; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 914 | elv_dispatch_sort(q, crq->request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* |
| 918 | * return expired entry, or NULL to just start from scratch in rbtree |
| 919 | */ |
| 920 | static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq) |
| 921 | { |
| 922 | struct cfq_data *cfqd = cfqq->cfqd; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 923 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | struct cfq_rq *crq; |
| 925 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 926 | if (cfq_cfqq_fifo_expire(cfqq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return NULL; |
| 928 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 929 | if (!list_empty(&cfqq->fifo)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 930 | int fifo = cfq_cfqq_class_sync(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 932 | crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next)); |
| 933 | rq = crq->request; |
| 934 | if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 935 | cfq_mark_cfqq_fifo_expire(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 936 | return crq; |
| 937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | /* |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 944 | * Scale schedule slice based on io priority. Use the sync time slice only |
| 945 | * if a queue is marked sync and has sync io queued. A sync queue with async |
| 946 | * io only, should not get full sync slice length. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 948 | static inline int |
| 949 | cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 951 | const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 953 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 955 | return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 958 | static inline void |
| 959 | cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 960 | { |
| 961 | cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies; |
| 962 | } |
| 963 | |
| 964 | static inline int |
| 965 | cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 966 | { |
| 967 | const int base_rq = cfqd->cfq_slice_async_rq; |
| 968 | |
| 969 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
| 970 | |
| 971 | return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio)); |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * get next queue for service |
| 976 | */ |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 977 | static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 978 | { |
| 979 | unsigned long now = jiffies; |
| 980 | struct cfq_queue *cfqq; |
| 981 | |
| 982 | cfqq = cfqd->active_queue; |
| 983 | if (!cfqq) |
| 984 | goto new_queue; |
| 985 | |
| 986 | /* |
| 987 | * slice has expired |
| 988 | */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 989 | if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end)) |
| 990 | goto expire; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 991 | |
| 992 | /* |
| 993 | * if queue has requests, dispatch one. if not, check if |
| 994 | * enough slice is left to wait for one |
| 995 | */ |
| 996 | if (!RB_EMPTY(&cfqq->sort_list)) |
| 997 | goto keep_queue; |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 998 | else if (cfq_cfqq_class_sync(cfqq) && |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 999 | time_before(now, cfqq->slice_end)) { |
| 1000 | if (cfq_arm_slice_timer(cfqd, cfqq)) |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1004 | expire: |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1005 | cfq_slice_expired(cfqd, 0); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1006 | new_queue: |
| 1007 | cfqq = cfq_set_active_queue(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1008 | keep_queue: |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1009 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static int |
| 1013 | __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1014 | int max_dispatch) |
| 1015 | { |
| 1016 | int dispatched = 0; |
| 1017 | |
| 1018 | BUG_ON(RB_EMPTY(&cfqq->sort_list)); |
| 1019 | |
| 1020 | do { |
| 1021 | struct cfq_rq *crq; |
| 1022 | |
| 1023 | /* |
| 1024 | * follow expired path, else get first next available |
| 1025 | */ |
| 1026 | if ((crq = cfq_check_fifo(cfqq)) == NULL) |
| 1027 | crq = cfqq->next_crq; |
| 1028 | |
| 1029 | /* |
| 1030 | * finally, insert request into driver dispatch list |
| 1031 | */ |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1032 | cfq_dispatch_insert(cfqd->queue, crq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1033 | |
| 1034 | cfqd->dispatch_slice++; |
| 1035 | dispatched++; |
| 1036 | |
| 1037 | if (!cfqd->active_cic) { |
| 1038 | atomic_inc(&crq->io_context->ioc->refcount); |
| 1039 | cfqd->active_cic = crq->io_context; |
| 1040 | } |
| 1041 | |
| 1042 | if (RB_EMPTY(&cfqq->sort_list)) |
| 1043 | break; |
| 1044 | |
| 1045 | } while (dispatched < max_dispatch); |
| 1046 | |
| 1047 | /* |
| 1048 | * if slice end isn't set yet, set it. if at least one request was |
| 1049 | * sync, use the sync time slice value |
| 1050 | */ |
| 1051 | if (!cfqq->slice_end) |
| 1052 | cfq_set_prio_slice(cfqd, cfqq); |
| 1053 | |
| 1054 | /* |
| 1055 | * expire an async queue immediately if it has used up its slice. idle |
| 1056 | * queue always expire after 1 dispatch round. |
| 1057 | */ |
| 1058 | if ((!cfq_cfqq_sync(cfqq) && |
| 1059 | cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) || |
| 1060 | cfq_class_idle(cfqq)) |
| 1061 | cfq_slice_expired(cfqd, 0); |
| 1062 | |
| 1063 | return dispatched; |
| 1064 | } |
| 1065 | |
| 1066 | static int |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 1067 | cfq_forced_dispatch_cfqqs(struct list_head *list) |
| 1068 | { |
| 1069 | int dispatched = 0; |
| 1070 | struct cfq_queue *cfqq, *next; |
| 1071 | struct cfq_rq *crq; |
| 1072 | |
| 1073 | list_for_each_entry_safe(cfqq, next, list, cfq_list) { |
| 1074 | while ((crq = cfqq->next_crq)) { |
| 1075 | cfq_dispatch_insert(cfqq->cfqd->queue, crq); |
| 1076 | dispatched++; |
| 1077 | } |
| 1078 | BUG_ON(!list_empty(&cfqq->fifo)); |
| 1079 | } |
| 1080 | return dispatched; |
| 1081 | } |
| 1082 | |
| 1083 | static int |
| 1084 | cfq_forced_dispatch(struct cfq_data *cfqd) |
| 1085 | { |
| 1086 | int i, dispatched = 0; |
| 1087 | |
| 1088 | for (i = 0; i < CFQ_PRIO_LISTS; i++) |
| 1089 | dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]); |
| 1090 | |
| 1091 | dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr); |
| 1092 | dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr); |
| 1093 | dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr); |
| 1094 | |
| 1095 | cfq_slice_expired(cfqd, 0); |
| 1096 | |
| 1097 | BUG_ON(cfqd->busy_queues); |
| 1098 | |
| 1099 | return dispatched; |
| 1100 | } |
| 1101 | |
| 1102 | static int |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1103 | cfq_dispatch_requests(request_queue_t *q, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | { |
| 1105 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 1106 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1108 | if (!cfqd->busy_queues) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | return 0; |
| 1110 | |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 1111 | if (unlikely(force)) |
| 1112 | return cfq_forced_dispatch(cfqd); |
| 1113 | |
| 1114 | cfqq = cfq_select_queue(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1115 | if (cfqq) { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1116 | int max_dispatch; |
| 1117 | |
| 1118 | /* |
| 1119 | * if idle window is disabled, allow queue buildup |
| 1120 | */ |
| 1121 | if (!cfq_cfqq_idle_window(cfqq) && |
| 1122 | cfqd->rq_in_driver >= cfqd->cfq_max_depth) |
| 1123 | return 0; |
| 1124 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1125 | cfq_clear_cfqq_must_dispatch(cfqq); |
| 1126 | cfq_clear_cfqq_wait_request(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1127 | del_timer(&cfqd->idle_slice_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 1129 | max_dispatch = cfqd->cfq_quantum; |
| 1130 | if (cfq_class_idle(cfqq)) |
| 1131 | max_dispatch = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1133 | return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1136 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | /* |
| 1140 | * task holds one reference to the queue, dropped when task exits. each crq |
| 1141 | * in-flight on this queue also holds a reference, dropped when crq is freed. |
| 1142 | * |
| 1143 | * queue lock must be held here. |
| 1144 | */ |
| 1145 | static void cfq_put_queue(struct cfq_queue *cfqq) |
| 1146 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1147 | struct cfq_data *cfqd = cfqq->cfqd; |
| 1148 | |
| 1149 | BUG_ON(atomic_read(&cfqq->ref) <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | if (!atomic_dec_and_test(&cfqq->ref)) |
| 1152 | return; |
| 1153 | |
| 1154 | BUG_ON(rb_first(&cfqq->sort_list)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1155 | BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1156 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1158 | if (unlikely(cfqd->active_queue == cfqq)) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1159 | __cfq_slice_expired(cfqd, cfqq, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | /* |
| 1162 | * it's on the empty list and still hashed |
| 1163 | */ |
| 1164 | list_del(&cfqq->cfq_list); |
| 1165 | hlist_del(&cfqq->cfq_hash); |
| 1166 | kmem_cache_free(cfq_pool, cfqq); |
| 1167 | } |
| 1168 | |
| 1169 | static inline struct cfq_queue * |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1170 | __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio, |
| 1171 | const int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | { |
| 1173 | struct hlist_head *hash_list = &cfqd->cfq_hash[hashval]; |
| 1174 | struct hlist_node *entry, *next; |
| 1175 | |
| 1176 | hlist_for_each_safe(entry, next, hash_list) { |
| 1177 | struct cfq_queue *__cfqq = list_entry_qhash(entry); |
Al Viro | b0a6916 | 2006-03-14 15:32:50 -0500 | [diff] [blame] | 1178 | const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1180 | if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | return __cfqq; |
| 1182 | } |
| 1183 | |
| 1184 | return NULL; |
| 1185 | } |
| 1186 | |
| 1187 | static struct cfq_queue * |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1188 | cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1190 | return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | static void cfq_free_io_context(struct cfq_io_context *cic) |
| 1194 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1195 | struct cfq_io_context *__cic; |
| 1196 | struct list_head *entry, *next; |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1197 | int freed = 1; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1198 | |
| 1199 | list_for_each_safe(entry, next, &cic->list) { |
| 1200 | __cic = list_entry(entry, struct cfq_io_context, list); |
| 1201 | kmem_cache_free(cfq_ioc_pool, __cic); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1202 | freed++; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | kmem_cache_free(cfq_ioc_pool, cic); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1206 | if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone) |
| 1207 | complete(ioc_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 1210 | static void cfq_trim(struct io_context *ioc) |
| 1211 | { |
| 1212 | ioc->set_ioprio = NULL; |
| 1213 | if (ioc->cic) |
| 1214 | cfq_free_io_context(ioc->cic); |
| 1215 | } |
| 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1218 | * Called with interrupts disabled |
| 1219 | */ |
| 1220 | static void cfq_exit_single_io_context(struct cfq_io_context *cic) |
| 1221 | { |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1222 | struct cfq_data *cfqd = cic->key; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1223 | request_queue_t *q; |
| 1224 | |
| 1225 | if (!cfqd) |
| 1226 | return; |
| 1227 | |
| 1228 | q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1229 | |
| 1230 | WARN_ON(!irqs_disabled()); |
| 1231 | |
| 1232 | spin_lock(q->queue_lock); |
| 1233 | |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1234 | if (cic->cfqq[ASYNC]) { |
| 1235 | if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue)) |
| 1236 | __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0); |
| 1237 | cfq_put_queue(cic->cfqq[ASYNC]); |
| 1238 | cic->cfqq[ASYNC] = NULL; |
| 1239 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1240 | |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1241 | if (cic->cfqq[SYNC]) { |
| 1242 | if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue)) |
| 1243 | __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0); |
| 1244 | cfq_put_queue(cic->cfqq[SYNC]); |
| 1245 | cic->cfqq[SYNC] = NULL; |
| 1246 | } |
| 1247 | |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1248 | cic->key = NULL; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1249 | list_del_init(&cic->queue_list); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1250 | spin_unlock(q->queue_lock); |
| 1251 | } |
| 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | static void cfq_exit_io_context(struct cfq_io_context *cic) |
| 1254 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1255 | struct cfq_io_context *__cic; |
| 1256 | struct list_head *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | unsigned long flags; |
| 1258 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1259 | local_irq_save(flags); |
| 1260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | /* |
| 1262 | * put the reference this task is holding to the various queues |
| 1263 | */ |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1264 | read_lock(&cfq_exit_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1265 | list_for_each(entry, &cic->list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | __cic = list_entry(entry, struct cfq_io_context, list); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1267 | cfq_exit_single_io_context(__cic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1270 | cfq_exit_single_io_context(cic); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1271 | read_unlock(&cfq_exit_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1272 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1275 | static struct cfq_io_context * |
Al Viro | 8267e26 | 2005-10-21 03:20:53 -0400 | [diff] [blame] | 1276 | cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1278 | struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | if (cic) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | INIT_LIST_HEAD(&cic->list); |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1282 | cic->cfqq[ASYNC] = NULL; |
| 1283 | cic->cfqq[SYNC] = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1284 | cic->key = NULL; |
| 1285 | cic->last_end_request = jiffies; |
| 1286 | cic->ttime_total = 0; |
| 1287 | cic->ttime_samples = 0; |
| 1288 | cic->ttime_mean = 0; |
| 1289 | cic->dtor = cfq_free_io_context; |
| 1290 | cic->exit = cfq_exit_io_context; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1291 | INIT_LIST_HEAD(&cic->queue_list); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1292 | atomic_inc(&ioc_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | return cic; |
| 1296 | } |
| 1297 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1298 | static void cfq_init_prio_data(struct cfq_queue *cfqq) |
| 1299 | { |
| 1300 | struct task_struct *tsk = current; |
| 1301 | int ioprio_class; |
| 1302 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1303 | if (!cfq_cfqq_prio_changed(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1304 | return; |
| 1305 | |
| 1306 | ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio); |
| 1307 | switch (ioprio_class) { |
| 1308 | default: |
| 1309 | printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); |
| 1310 | case IOPRIO_CLASS_NONE: |
| 1311 | /* |
| 1312 | * no prio set, place us in the middle of the BE classes |
| 1313 | */ |
| 1314 | cfqq->ioprio = task_nice_ioprio(tsk); |
| 1315 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 1316 | break; |
| 1317 | case IOPRIO_CLASS_RT: |
| 1318 | cfqq->ioprio = task_ioprio(tsk); |
| 1319 | cfqq->ioprio_class = IOPRIO_CLASS_RT; |
| 1320 | break; |
| 1321 | case IOPRIO_CLASS_BE: |
| 1322 | cfqq->ioprio = task_ioprio(tsk); |
| 1323 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 1324 | break; |
| 1325 | case IOPRIO_CLASS_IDLE: |
| 1326 | cfqq->ioprio_class = IOPRIO_CLASS_IDLE; |
| 1327 | cfqq->ioprio = 7; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1328 | cfq_clear_cfqq_idle_window(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * keep track of original prio settings in case we have to temporarily |
| 1334 | * elevate the priority of this queue |
| 1335 | */ |
| 1336 | cfqq->org_ioprio = cfqq->ioprio; |
| 1337 | cfqq->org_ioprio_class = cfqq->ioprio_class; |
| 1338 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1339 | if (cfq_cfqq_on_rr(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1340 | cfq_resort_rr_list(cfqq, 0); |
| 1341 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1342 | cfq_clear_cfqq_prio_changed(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1345 | static inline void changed_ioprio(struct cfq_io_context *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1346 | { |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1347 | struct cfq_data *cfqd = cic->key; |
| 1348 | struct cfq_queue *cfqq; |
| 1349 | if (cfqd) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1350 | spin_lock(cfqd->queue->queue_lock); |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1351 | cfqq = cic->cfqq[ASYNC]; |
| 1352 | if (cfqq) { |
Al Viro | 6f325a1 | 2006-03-18 14:58:37 -0500 | [diff] [blame] | 1353 | struct cfq_queue *new_cfqq; |
| 1354 | new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, |
| 1355 | cic->ioc->task, GFP_ATOMIC); |
| 1356 | if (new_cfqq) { |
| 1357 | cic->cfqq[ASYNC] = new_cfqq; |
| 1358 | cfq_put_queue(cfqq); |
| 1359 | } |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1360 | } |
| 1361 | cfqq = cic->cfqq[SYNC]; |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1362 | if (cfqq) { |
| 1363 | cfq_mark_cfqq_prio_changed(cfqq); |
| 1364 | cfq_init_prio_data(cfqq); |
| 1365 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1366 | spin_unlock(cfqd->queue->queue_lock); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1371 | * callback from sys_ioprio_set, irqs are disabled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1373 | static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | { |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 1375 | struct cfq_io_context *cic; |
| 1376 | |
| 1377 | write_lock(&cfq_exit_lock); |
| 1378 | |
| 1379 | cic = ioc->cic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1381 | changed_ioprio(cic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1383 | list_for_each_entry(cic, &cic->list, list) |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1384 | changed_ioprio(cic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 1386 | write_unlock(&cfq_exit_lock); |
| 1387 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1388 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | static struct cfq_queue * |
Al Viro | 6f325a1 | 2006-03-18 14:58:37 -0500 | [diff] [blame] | 1392 | cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, |
Al Viro | 8267e26 | 2005-10-21 03:20:53 -0400 | [diff] [blame] | 1393 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | { |
| 1395 | const int hashval = hash_long(key, CFQ_QHASH_SHIFT); |
| 1396 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
Al Viro | 6f325a1 | 2006-03-18 14:58:37 -0500 | [diff] [blame] | 1397 | unsigned short ioprio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
| 1399 | retry: |
Al Viro | 6f325a1 | 2006-03-18 14:58:37 -0500 | [diff] [blame] | 1400 | ioprio = tsk->ioprio; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1401 | cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | if (!cfqq) { |
| 1404 | if (new_cfqq) { |
| 1405 | cfqq = new_cfqq; |
| 1406 | new_cfqq = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1407 | } else if (gfp_mask & __GFP_WAIT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | spin_unlock_irq(cfqd->queue->queue_lock); |
| 1409 | new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask); |
| 1410 | spin_lock_irq(cfqd->queue->queue_lock); |
| 1411 | goto retry; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1412 | } else { |
| 1413 | cfqq = kmem_cache_alloc(cfq_pool, gfp_mask); |
| 1414 | if (!cfqq) |
| 1415 | goto out; |
Kiyoshi Ueda | db3b584 | 2005-06-17 16:15:10 +0200 | [diff] [blame] | 1416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | memset(cfqq, 0, sizeof(*cfqq)); |
| 1419 | |
| 1420 | INIT_HLIST_NODE(&cfqq->cfq_hash); |
| 1421 | INIT_LIST_HEAD(&cfqq->cfq_list); |
| 1422 | RB_CLEAR_ROOT(&cfqq->sort_list); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1423 | INIT_LIST_HEAD(&cfqq->fifo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | cfqq->key = key; |
| 1426 | hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]); |
| 1427 | atomic_set(&cfqq->ref, 0); |
| 1428 | cfqq->cfqd = cfqd; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1429 | cfqq->service_last = 0; |
| 1430 | /* |
| 1431 | * set ->slice_left to allow preemption for a new process |
| 1432 | */ |
| 1433 | cfqq->slice_left = 2 * cfqd->cfq_slice_idle; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1434 | cfq_mark_cfqq_idle_window(cfqq); |
| 1435 | cfq_mark_cfqq_prio_changed(cfqq); |
| 1436 | cfq_init_prio_data(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | if (new_cfqq) |
| 1440 | kmem_cache_free(cfq_pool, new_cfqq); |
| 1441 | |
| 1442 | atomic_inc(&cfqq->ref); |
| 1443 | out: |
| 1444 | WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq); |
| 1445 | return cfqq; |
| 1446 | } |
| 1447 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1448 | /* |
| 1449 | * Setup general io context and cfq io context. There can be several cfq |
| 1450 | * io contexts per general io context, if this process is doing io to more |
| 1451 | * than one device managed by cfq. Note that caller is holding a reference to |
| 1452 | * cfqq, so we don't need to worry about it disappearing |
| 1453 | */ |
| 1454 | static struct cfq_io_context * |
Al Viro | 8267e26 | 2005-10-21 03:20:53 -0400 | [diff] [blame] | 1455 | cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1457 | struct io_context *ioc = NULL; |
| 1458 | struct cfq_io_context *cic; |
| 1459 | |
| 1460 | might_sleep_if(gfp_mask & __GFP_WAIT); |
| 1461 | |
| 1462 | ioc = get_io_context(gfp_mask); |
| 1463 | if (!ioc) |
| 1464 | return NULL; |
| 1465 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1466 | restart: |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1467 | if ((cic = ioc->cic) == NULL) { |
| 1468 | cic = cfq_alloc_io_context(cfqd, gfp_mask); |
| 1469 | |
| 1470 | if (cic == NULL) |
| 1471 | goto err; |
| 1472 | |
| 1473 | /* |
| 1474 | * manually increment generic io_context usage count, it |
| 1475 | * cannot go away since we are already holding one ref to it |
| 1476 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1477 | cic->ioc = ioc; |
| 1478 | cic->key = cfqd; |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 1479 | read_lock(&cfq_exit_lock); |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 1480 | ioc->set_ioprio = cfq_ioc_set_ioprio; |
| 1481 | ioc->cic = cic; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1482 | list_add(&cic->queue_list, &cfqd->cic_list); |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 1483 | read_unlock(&cfq_exit_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1484 | } else { |
| 1485 | struct cfq_io_context *__cic; |
| 1486 | |
| 1487 | /* |
| 1488 | * the first cic on the list is actually the head itself |
| 1489 | */ |
| 1490 | if (cic->key == cfqd) |
| 1491 | goto out; |
| 1492 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1493 | if (unlikely(!cic->key)) { |
| 1494 | read_lock(&cfq_exit_lock); |
| 1495 | if (list_empty(&cic->list)) |
| 1496 | ioc->cic = NULL; |
| 1497 | else |
| 1498 | ioc->cic = list_entry(cic->list.next, |
| 1499 | struct cfq_io_context, |
| 1500 | list); |
| 1501 | read_unlock(&cfq_exit_lock); |
| 1502 | kmem_cache_free(cfq_ioc_pool, cic); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1503 | atomic_dec(&ioc_count); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1504 | goto restart; |
| 1505 | } |
| 1506 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1507 | /* |
| 1508 | * cic exists, check if we already are there. linear search |
| 1509 | * should be ok here, the list will usually not be more than |
| 1510 | * 1 or a few entries long |
| 1511 | */ |
| 1512 | list_for_each_entry(__cic, &cic->list, list) { |
| 1513 | /* |
| 1514 | * this process is already holding a reference to |
| 1515 | * this queue, so no need to get one more |
| 1516 | */ |
| 1517 | if (__cic->key == cfqd) { |
| 1518 | cic = __cic; |
| 1519 | goto out; |
| 1520 | } |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1521 | if (unlikely(!__cic->key)) { |
| 1522 | read_lock(&cfq_exit_lock); |
| 1523 | list_del(&__cic->list); |
| 1524 | read_unlock(&cfq_exit_lock); |
| 1525 | kmem_cache_free(cfq_ioc_pool, __cic); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 1526 | atomic_dec(&ioc_count); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1527 | goto restart; |
| 1528 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * nope, process doesn't have a cic assoicated with this |
| 1533 | * cfqq yet. get a new one and add to list |
| 1534 | */ |
| 1535 | __cic = cfq_alloc_io_context(cfqd, gfp_mask); |
| 1536 | if (__cic == NULL) |
| 1537 | goto err; |
| 1538 | |
| 1539 | __cic->ioc = ioc; |
| 1540 | __cic->key = cfqd; |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 1541 | read_lock(&cfq_exit_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1542 | list_add(&__cic->list, &cic->list); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 1543 | list_add(&__cic->queue_list, &cfqd->cic_list); |
Al Viro | a6a0763 | 2006-03-18 13:26:44 -0500 | [diff] [blame] | 1544 | read_unlock(&cfq_exit_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1545 | cic = __cic; |
| 1546 | } |
| 1547 | |
| 1548 | out: |
| 1549 | return cic; |
| 1550 | err: |
| 1551 | put_io_context(ioc); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | |
| 1555 | static void |
| 1556 | cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic) |
| 1557 | { |
| 1558 | unsigned long elapsed, ttime; |
| 1559 | |
| 1560 | /* |
| 1561 | * if this context already has stuff queued, thinktime is from |
| 1562 | * last queue not last end |
| 1563 | */ |
| 1564 | #if 0 |
| 1565 | if (time_after(cic->last_end_request, cic->last_queue)) |
| 1566 | elapsed = jiffies - cic->last_end_request; |
| 1567 | else |
| 1568 | elapsed = jiffies - cic->last_queue; |
| 1569 | #else |
| 1570 | elapsed = jiffies - cic->last_end_request; |
| 1571 | #endif |
| 1572 | |
| 1573 | ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle); |
| 1574 | |
| 1575 | cic->ttime_samples = (7*cic->ttime_samples + 256) / 8; |
| 1576 | cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8; |
| 1577 | cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples; |
| 1578 | } |
| 1579 | |
| 1580 | #define sample_valid(samples) ((samples) > 80) |
| 1581 | |
| 1582 | /* |
| 1583 | * Disable idle window if the process thinks too long or seeks so much that |
| 1584 | * it doesn't matter |
| 1585 | */ |
| 1586 | static void |
| 1587 | cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1588 | struct cfq_io_context *cic) |
| 1589 | { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1590 | int enable_idle = cfq_cfqq_idle_window(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1591 | |
| 1592 | if (!cic->ioc->task || !cfqd->cfq_slice_idle) |
| 1593 | enable_idle = 0; |
| 1594 | else if (sample_valid(cic->ttime_samples)) { |
| 1595 | if (cic->ttime_mean > cfqd->cfq_slice_idle) |
| 1596 | enable_idle = 0; |
| 1597 | else |
| 1598 | enable_idle = 1; |
| 1599 | } |
| 1600 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1601 | if (enable_idle) |
| 1602 | cfq_mark_cfqq_idle_window(cfqq); |
| 1603 | else |
| 1604 | cfq_clear_cfqq_idle_window(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | |
| 1608 | /* |
| 1609 | * Check if new_cfqq should preempt the currently active queue. Return 0 for |
| 1610 | * no or if we aren't sure, a 1 will cause a preempt. |
| 1611 | */ |
| 1612 | static int |
| 1613 | cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, |
| 1614 | struct cfq_rq *crq) |
| 1615 | { |
| 1616 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 1617 | |
| 1618 | if (cfq_class_idle(new_cfqq)) |
| 1619 | return 0; |
| 1620 | |
| 1621 | if (!cfqq) |
| 1622 | return 1; |
| 1623 | |
| 1624 | if (cfq_class_idle(cfqq)) |
| 1625 | return 1; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1626 | if (!cfq_cfqq_wait_request(new_cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1627 | return 0; |
| 1628 | /* |
| 1629 | * if it doesn't have slice left, forget it |
| 1630 | */ |
| 1631 | if (new_cfqq->slice_left < cfqd->cfq_slice_idle) |
| 1632 | return 0; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1633 | if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1634 | return 1; |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * cfqq preempts the active queue. if we allowed preempt with no slice left, |
| 1641 | * let it have half of its nominal slice. |
| 1642 | */ |
| 1643 | static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1644 | { |
| 1645 | struct cfq_queue *__cfqq, *next; |
| 1646 | |
| 1647 | list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list) |
| 1648 | cfq_resort_rr_list(__cfqq, 1); |
| 1649 | |
| 1650 | if (!cfqq->slice_left) |
| 1651 | cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2; |
| 1652 | |
| 1653 | cfqq->slice_end = cfqq->slice_left + jiffies; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1654 | __cfq_slice_expired(cfqd, cfqq, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1655 | __cfq_set_active_queue(cfqd, cfqq); |
| 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * should really be a ll_rw_blk.c helper |
| 1660 | */ |
| 1661 | static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1662 | { |
| 1663 | request_queue_t *q = cfqd->queue; |
| 1664 | |
| 1665 | if (!blk_queue_plugged(q)) |
| 1666 | q->request_fn(q); |
| 1667 | else |
| 1668 | __generic_unplug_device(q); |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * Called when a new fs request (crq) is added (to cfqq). Check if there's |
| 1673 | * something we should do about it |
| 1674 | */ |
| 1675 | static void |
| 1676 | cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1677 | struct cfq_rq *crq) |
| 1678 | { |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 1679 | struct cfq_io_context *cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1680 | |
| 1681 | cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq); |
| 1682 | |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 1683 | /* |
| 1684 | * we never wait for an async request and we don't allow preemption |
| 1685 | * of an async request. so just return early |
| 1686 | */ |
| 1687 | if (!cfq_crq_is_sync(crq)) |
| 1688 | return; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1689 | |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 1690 | cic = crq->io_context; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1691 | |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 1692 | cfq_update_io_thinktime(cfqd, cic); |
| 1693 | cfq_update_idle_window(cfqd, cfqq, cic); |
| 1694 | |
| 1695 | cic->last_queue = jiffies; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1696 | |
| 1697 | if (cfqq == cfqd->active_queue) { |
| 1698 | /* |
| 1699 | * if we are waiting for a request for this queue, let it rip |
| 1700 | * immediately and flag that we must not expire this queue |
| 1701 | * just now |
| 1702 | */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1703 | if (cfq_cfqq_wait_request(cfqq)) { |
| 1704 | cfq_mark_cfqq_must_dispatch(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1705 | del_timer(&cfqd->idle_slice_timer); |
| 1706 | cfq_start_queueing(cfqd, cfqq); |
| 1707 | } |
| 1708 | } else if (cfq_should_preempt(cfqd, cfqq, crq)) { |
| 1709 | /* |
| 1710 | * not the active queue - expire current slice if it is |
| 1711 | * idle and has expired it's mean thinktime or this new queue |
| 1712 | * has some old slice time left and is of higher priority |
| 1713 | */ |
| 1714 | cfq_preempt_queue(cfqd, cfqq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1715 | cfq_mark_cfqq_must_dispatch(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1716 | cfq_start_queueing(cfqd, cfqq); |
| 1717 | } |
| 1718 | } |
| 1719 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1720 | static void cfq_insert_request(request_queue_t *q, struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1721 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1722 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1723 | struct cfq_rq *crq = RQ_DATA(rq); |
| 1724 | struct cfq_queue *cfqq = crq->cfq_queue; |
| 1725 | |
| 1726 | cfq_init_prio_data(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | |
| 1728 | cfq_add_crq_rb(crq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1730 | list_add_tail(&rq->queuelist, &cfqq->fifo); |
| 1731 | |
Tejun Heo | 98b1147 | 2005-10-20 16:46:54 +0200 | [diff] [blame] | 1732 | if (rq_mergeable(rq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1733 | cfq_add_crq_hash(cfqd, crq); |
| 1734 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1735 | cfq_crq_enqueued(cfqd, cfqq, crq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | static void cfq_completed_request(request_queue_t *q, struct request *rq) |
| 1739 | { |
| 1740 | struct cfq_rq *crq = RQ_DATA(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1741 | struct cfq_queue *cfqq = crq->cfq_queue; |
| 1742 | struct cfq_data *cfqd = cfqq->cfqd; |
| 1743 | const int sync = cfq_crq_is_sync(crq); |
| 1744 | unsigned long now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1746 | now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1748 | WARN_ON(!cfqd->rq_in_driver); |
| 1749 | WARN_ON(!cfqq->on_dispatch[sync]); |
| 1750 | cfqd->rq_in_driver--; |
| 1751 | cfqq->on_dispatch[sync]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1753 | if (!cfq_class_idle(cfqq)) |
| 1754 | cfqd->last_end_request = now; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1755 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1756 | if (!cfq_cfqq_dispatched(cfqq)) { |
| 1757 | if (cfq_cfqq_on_rr(cfqq)) { |
| 1758 | cfqq->service_last = now; |
| 1759 | cfq_resort_rr_list(cfqq, 0); |
| 1760 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1761 | cfq_schedule_dispatch(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1764 | if (cfq_crq_is_sync(crq)) |
| 1765 | crq->io_context->last_end_request = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | static struct request * |
| 1769 | cfq_former_request(request_queue_t *q, struct request *rq) |
| 1770 | { |
| 1771 | struct cfq_rq *crq = RQ_DATA(rq); |
| 1772 | struct rb_node *rbprev = rb_prev(&crq->rb_node); |
| 1773 | |
| 1774 | if (rbprev) |
| 1775 | return rb_entry_crq(rbprev)->request; |
| 1776 | |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | |
| 1780 | static struct request * |
| 1781 | cfq_latter_request(request_queue_t *q, struct request *rq) |
| 1782 | { |
| 1783 | struct cfq_rq *crq = RQ_DATA(rq); |
| 1784 | struct rb_node *rbnext = rb_next(&crq->rb_node); |
| 1785 | |
| 1786 | if (rbnext) |
| 1787 | return rb_entry_crq(rbnext)->request; |
| 1788 | |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1792 | /* |
| 1793 | * we temporarily boost lower priority queues if they are holding fs exclusive |
| 1794 | * resources. they are boosted to normal prio (CLASS_BE/4) |
| 1795 | */ |
| 1796 | static void cfq_prio_boost(struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1798 | const int ioprio_class = cfqq->ioprio_class; |
| 1799 | const int ioprio = cfqq->ioprio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1801 | if (has_fs_excl()) { |
| 1802 | /* |
| 1803 | * boost idle prio on transactions that would lock out other |
| 1804 | * users of the filesystem |
| 1805 | */ |
| 1806 | if (cfq_class_idle(cfqq)) |
| 1807 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 1808 | if (cfqq->ioprio > IOPRIO_NORM) |
| 1809 | cfqq->ioprio = IOPRIO_NORM; |
| 1810 | } else { |
| 1811 | /* |
| 1812 | * check if we need to unboost the queue |
| 1813 | */ |
| 1814 | if (cfqq->ioprio_class != cfqq->org_ioprio_class) |
| 1815 | cfqq->ioprio_class = cfqq->org_ioprio_class; |
| 1816 | if (cfqq->ioprio != cfqq->org_ioprio) |
| 1817 | cfqq->ioprio = cfqq->org_ioprio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1820 | /* |
| 1821 | * refile between round-robin lists if we moved the priority class |
| 1822 | */ |
| 1823 | if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) && |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1824 | cfq_cfqq_on_rr(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1825 | cfq_resort_rr_list(cfqq, 0); |
| 1826 | } |
| 1827 | |
| 1828 | static inline pid_t cfq_queue_pid(struct task_struct *task, int rw) |
| 1829 | { |
| 1830 | if (rw == READ || process_sync(task)) |
| 1831 | return task->pid; |
| 1832 | |
| 1833 | return CFQ_KEY_ASYNC; |
| 1834 | } |
| 1835 | |
| 1836 | static inline int |
| 1837 | __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1838 | struct task_struct *task, int rw) |
| 1839 | { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1840 | #if 1 |
| 1841 | if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) && |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 1842 | !cfq_cfqq_must_alloc_slice(cfqq)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1843 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1844 | return ELV_MQUEUE_MUST; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1845 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1846 | |
| 1847 | return ELV_MQUEUE_MAY; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1848 | #else |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1849 | if (!cfqq || task->flags & PF_MEMALLOC) |
| 1850 | return ELV_MQUEUE_MAY; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1851 | if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) { |
| 1852 | if (cfq_cfqq_wait_request(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1853 | return ELV_MQUEUE_MUST; |
| 1854 | |
| 1855 | /* |
| 1856 | * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we |
| 1857 | * can quickly flood the queue with writes from a single task |
| 1858 | */ |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 1859 | if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1860 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1861 | return ELV_MQUEUE_MUST; |
| 1862 | } |
| 1863 | |
| 1864 | return ELV_MQUEUE_MAY; |
| 1865 | } |
| 1866 | if (cfq_class_idle(cfqq)) |
| 1867 | return ELV_MQUEUE_NO; |
| 1868 | if (cfqq->allocated[rw] >= cfqd->max_queued) { |
| 1869 | struct io_context *ioc = get_io_context(GFP_ATOMIC); |
| 1870 | int ret = ELV_MQUEUE_NO; |
| 1871 | |
| 1872 | if (ioc && ioc->nr_batch_requests) |
| 1873 | ret = ELV_MQUEUE_MAY; |
| 1874 | |
| 1875 | put_io_context(ioc); |
| 1876 | return ret; |
| 1877 | } |
| 1878 | |
| 1879 | return ELV_MQUEUE_MAY; |
| 1880 | #endif |
| 1881 | } |
| 1882 | |
| 1883 | static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio) |
| 1884 | { |
| 1885 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 1886 | struct task_struct *tsk = current; |
| 1887 | struct cfq_queue *cfqq; |
| 1888 | |
| 1889 | /* |
| 1890 | * don't force setup of a queue from here, as a call to may_queue |
| 1891 | * does not necessarily imply that a request actually will be queued. |
| 1892 | * so just lookup a possibly existing queue, or return 'may queue' |
| 1893 | * if that fails |
| 1894 | */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1895 | cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1896 | if (cfqq) { |
| 1897 | cfq_init_prio_data(cfqq); |
| 1898 | cfq_prio_boost(cfqq); |
| 1899 | |
| 1900 | return __cfq_may_queue(cfqd, cfqq, tsk, rw); |
| 1901 | } |
| 1902 | |
| 1903 | return ELV_MQUEUE_MAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq) |
| 1907 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1908 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | struct request_list *rl = &q->rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1911 | if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) { |
| 1912 | smp_mb(); |
| 1913 | if (waitqueue_active(&rl->wait[READ])) |
| 1914 | wake_up(&rl->wait[READ]); |
| 1915 | } |
| 1916 | |
| 1917 | if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) { |
| 1918 | smp_mb(); |
| 1919 | if (waitqueue_active(&rl->wait[WRITE])) |
| 1920 | wake_up(&rl->wait[WRITE]); |
| 1921 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * queue lock held here |
| 1926 | */ |
| 1927 | static void cfq_put_request(request_queue_t *q, struct request *rq) |
| 1928 | { |
| 1929 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 1930 | struct cfq_rq *crq = RQ_DATA(rq); |
| 1931 | |
| 1932 | if (crq) { |
| 1933 | struct cfq_queue *cfqq = crq->cfq_queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1934 | const int rw = rq_data_dir(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1936 | BUG_ON(!cfqq->allocated[rw]); |
| 1937 | cfqq->allocated[rw]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1939 | put_io_context(crq->io_context->ioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | |
| 1941 | mempool_free(crq, cfqd->crq_pool); |
| 1942 | rq->elevator_private = NULL; |
| 1943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | cfq_check_waiters(q, cfqq); |
| 1945 | cfq_put_queue(cfqq); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1950 | * Allocate cfq data structures associated with this request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1952 | static int |
| 1953 | cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio, |
Al Viro | 8267e26 | 2005-10-21 03:20:53 -0400 | [diff] [blame] | 1954 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | { |
| 1956 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1957 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | struct cfq_io_context *cic; |
| 1959 | const int rw = rq_data_dir(rq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1960 | pid_t key = cfq_queue_pid(tsk, rw); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1961 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | struct cfq_rq *crq; |
| 1963 | unsigned long flags; |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1964 | int is_sync = key != CFQ_KEY_ASYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
| 1966 | might_sleep_if(gfp_mask & __GFP_WAIT); |
| 1967 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1968 | cic = cfq_get_io_context(cfqd, key, gfp_mask); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | spin_lock_irqsave(q->queue_lock, flags); |
| 1971 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1972 | if (!cic) |
| 1973 | goto queue_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1975 | if (!cic->cfqq[is_sync]) { |
Al Viro | 6f325a1 | 2006-03-18 14:58:37 -0500 | [diff] [blame] | 1976 | cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1977 | if (!cfqq) |
| 1978 | goto queue_fail; |
| 1979 | |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1980 | cic->cfqq[is_sync] = cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1981 | } else |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1982 | cfqq = cic->cfqq[is_sync]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
| 1984 | cfqq->allocated[rw]++; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1985 | cfq_clear_cfqq_must_alloc(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1986 | cfqd->rq_starved = 0; |
| 1987 | atomic_inc(&cfqq->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | crq = mempool_alloc(cfqd->crq_pool, gfp_mask); |
| 1991 | if (crq) { |
| 1992 | RB_CLEAR(&crq->rb_node); |
| 1993 | crq->rb_key = 0; |
| 1994 | crq->request = rq; |
| 1995 | INIT_HLIST_NODE(&crq->hash); |
| 1996 | crq->cfq_queue = cfqq; |
| 1997 | crq->io_context = cic; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1998 | |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 1999 | if (is_sync) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2000 | cfq_mark_crq_is_sync(crq); |
| 2001 | else |
| 2002 | cfq_clear_crq_is_sync(crq); |
| 2003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | rq->elevator_private = crq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | return 0; |
| 2006 | } |
| 2007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | spin_lock_irqsave(q->queue_lock, flags); |
| 2009 | cfqq->allocated[rw]--; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2010 | if (!(cfqq->allocated[0] + cfqq->allocated[1])) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2011 | cfq_mark_cfqq_must_alloc(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | cfq_put_queue(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2013 | queue_fail: |
| 2014 | if (cic) |
| 2015 | put_io_context(cic->ioc); |
| 2016 | /* |
| 2017 | * mark us rq allocation starved. we need to kickstart the process |
| 2018 | * ourselves if there are no pending requests that can do it for us. |
| 2019 | * that would be an extremely rare OOM situation |
| 2020 | */ |
| 2021 | cfqd->rq_starved = 1; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2022 | cfq_schedule_dispatch(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 2024 | return 1; |
| 2025 | } |
| 2026 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2027 | static void cfq_kick_queue(void *data) |
| 2028 | { |
| 2029 | request_queue_t *q = data; |
| 2030 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2031 | unsigned long flags; |
| 2032 | |
| 2033 | spin_lock_irqsave(q->queue_lock, flags); |
| 2034 | |
| 2035 | if (cfqd->rq_starved) { |
| 2036 | struct request_list *rl = &q->rq; |
| 2037 | |
| 2038 | /* |
| 2039 | * we aren't guaranteed to get a request after this, but we |
| 2040 | * have to be opportunistic |
| 2041 | */ |
| 2042 | smp_mb(); |
| 2043 | if (waitqueue_active(&rl->wait[READ])) |
| 2044 | wake_up(&rl->wait[READ]); |
| 2045 | if (waitqueue_active(&rl->wait[WRITE])) |
| 2046 | wake_up(&rl->wait[WRITE]); |
| 2047 | } |
| 2048 | |
| 2049 | blk_remove_plug(q); |
| 2050 | q->request_fn(q); |
| 2051 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * Timer running if the active_queue is currently idling inside its time slice |
| 2056 | */ |
| 2057 | static void cfq_idle_slice_timer(unsigned long data) |
| 2058 | { |
| 2059 | struct cfq_data *cfqd = (struct cfq_data *) data; |
| 2060 | struct cfq_queue *cfqq; |
| 2061 | unsigned long flags; |
| 2062 | |
| 2063 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 2064 | |
| 2065 | if ((cfqq = cfqd->active_queue) != NULL) { |
| 2066 | unsigned long now = jiffies; |
| 2067 | |
| 2068 | /* |
| 2069 | * expired |
| 2070 | */ |
| 2071 | if (time_after(now, cfqq->slice_end)) |
| 2072 | goto expire; |
| 2073 | |
| 2074 | /* |
| 2075 | * only expire and reinvoke request handler, if there are |
| 2076 | * other queues with pending requests |
| 2077 | */ |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2078 | if (!cfqd->busy_queues) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2079 | cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end); |
| 2080 | add_timer(&cfqd->idle_slice_timer); |
| 2081 | goto out_cont; |
| 2082 | } |
| 2083 | |
| 2084 | /* |
| 2085 | * not expired and it has a request pending, let it dispatch |
| 2086 | */ |
| 2087 | if (!RB_EMPTY(&cfqq->sort_list)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2088 | cfq_mark_cfqq_must_dispatch(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2089 | goto out_kick; |
| 2090 | } |
| 2091 | } |
| 2092 | expire: |
| 2093 | cfq_slice_expired(cfqd, 0); |
| 2094 | out_kick: |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2095 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2096 | out_cont: |
| 2097 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
| 2098 | } |
| 2099 | |
| 2100 | /* |
| 2101 | * Timer running if an idle class queue is waiting for service |
| 2102 | */ |
| 2103 | static void cfq_idle_class_timer(unsigned long data) |
| 2104 | { |
| 2105 | struct cfq_data *cfqd = (struct cfq_data *) data; |
| 2106 | unsigned long flags, end; |
| 2107 | |
| 2108 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 2109 | |
| 2110 | /* |
| 2111 | * race with a non-idle queue, reset timer |
| 2112 | */ |
| 2113 | end = cfqd->last_end_request + CFQ_IDLE_GRACE; |
| 2114 | if (!time_after_eq(jiffies, end)) { |
| 2115 | cfqd->idle_class_timer.expires = end; |
| 2116 | add_timer(&cfqd->idle_class_timer); |
| 2117 | } else |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2118 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2119 | |
| 2120 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
| 2121 | } |
| 2122 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2123 | static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) |
| 2124 | { |
| 2125 | del_timer_sync(&cfqd->idle_slice_timer); |
| 2126 | del_timer_sync(&cfqd->idle_class_timer); |
| 2127 | blk_sync_queue(cfqd->queue); |
| 2128 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | static void cfq_exit_queue(elevator_t *e) |
| 2131 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2132 | struct cfq_data *cfqd = e->elevator_data; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 2133 | request_queue_t *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2134 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2135 | cfq_shutdown_timer_wq(cfqd); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 2136 | write_lock(&cfq_exit_lock); |
| 2137 | spin_lock_irq(q->queue_lock); |
| 2138 | if (cfqd->active_queue) |
| 2139 | __cfq_slice_expired(cfqd, cfqd->active_queue, 0); |
| 2140 | while(!list_empty(&cfqd->cic_list)) { |
| 2141 | struct cfq_io_context *cic = list_entry(cfqd->cic_list.next, |
| 2142 | struct cfq_io_context, |
| 2143 | queue_list); |
| 2144 | if (cic->cfqq[ASYNC]) { |
| 2145 | cfq_put_queue(cic->cfqq[ASYNC]); |
| 2146 | cic->cfqq[ASYNC] = NULL; |
| 2147 | } |
| 2148 | if (cic->cfqq[SYNC]) { |
| 2149 | cfq_put_queue(cic->cfqq[SYNC]); |
| 2150 | cic->cfqq[SYNC] = NULL; |
| 2151 | } |
| 2152 | cic->key = NULL; |
| 2153 | list_del_init(&cic->queue_list); |
| 2154 | } |
| 2155 | spin_unlock_irq(q->queue_lock); |
| 2156 | write_unlock(&cfq_exit_lock); |
Al Viro | a90d742 | 2006-03-18 12:05:37 -0500 | [diff] [blame] | 2157 | |
| 2158 | cfq_shutdown_timer_wq(cfqd); |
| 2159 | |
| 2160 | mempool_destroy(cfqd->crq_pool); |
| 2161 | kfree(cfqd->crq_hash); |
| 2162 | kfree(cfqd->cfq_hash); |
| 2163 | kfree(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | static int cfq_init_queue(request_queue_t *q, elevator_t *e) |
| 2167 | { |
| 2168 | struct cfq_data *cfqd; |
| 2169 | int i; |
| 2170 | |
| 2171 | cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL); |
| 2172 | if (!cfqd) |
| 2173 | return -ENOMEM; |
| 2174 | |
| 2175 | memset(cfqd, 0, sizeof(*cfqd)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2176 | |
| 2177 | for (i = 0; i < CFQ_PRIO_LISTS; i++) |
| 2178 | INIT_LIST_HEAD(&cfqd->rr_list[i]); |
| 2179 | |
| 2180 | INIT_LIST_HEAD(&cfqd->busy_rr); |
| 2181 | INIT_LIST_HEAD(&cfqd->cur_rr); |
| 2182 | INIT_LIST_HEAD(&cfqd->idle_rr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | INIT_LIST_HEAD(&cfqd->empty_list); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 2184 | INIT_LIST_HEAD(&cfqd->cic_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | |
| 2186 | cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL); |
| 2187 | if (!cfqd->crq_hash) |
| 2188 | goto out_crqhash; |
| 2189 | |
| 2190 | cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL); |
| 2191 | if (!cfqd->cfq_hash) |
| 2192 | goto out_cfqhash; |
| 2193 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame^] | 2194 | cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | if (!cfqd->crq_pool) |
| 2196 | goto out_crqpool; |
| 2197 | |
| 2198 | for (i = 0; i < CFQ_MHASH_ENTRIES; i++) |
| 2199 | INIT_HLIST_HEAD(&cfqd->crq_hash[i]); |
| 2200 | for (i = 0; i < CFQ_QHASH_ENTRIES; i++) |
| 2201 | INIT_HLIST_HEAD(&cfqd->cfq_hash[i]); |
| 2202 | |
| 2203 | e->elevator_data = cfqd; |
| 2204 | |
| 2205 | cfqd->queue = q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2207 | cfqd->max_queued = q->nr_requests / 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | q->nr_batching = cfq_queued; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2209 | |
| 2210 | init_timer(&cfqd->idle_slice_timer); |
| 2211 | cfqd->idle_slice_timer.function = cfq_idle_slice_timer; |
| 2212 | cfqd->idle_slice_timer.data = (unsigned long) cfqd; |
| 2213 | |
| 2214 | init_timer(&cfqd->idle_class_timer); |
| 2215 | cfqd->idle_class_timer.function = cfq_idle_class_timer; |
| 2216 | cfqd->idle_class_timer.data = (unsigned long) cfqd; |
| 2217 | |
| 2218 | INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q); |
| 2219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | cfqd->cfq_queued = cfq_queued; |
| 2221 | cfqd->cfq_quantum = cfq_quantum; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2222 | cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; |
| 2223 | cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | cfqd->cfq_back_max = cfq_back_max; |
| 2225 | cfqd->cfq_back_penalty = cfq_back_penalty; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2226 | cfqd->cfq_slice[0] = cfq_slice_async; |
| 2227 | cfqd->cfq_slice[1] = cfq_slice_sync; |
| 2228 | cfqd->cfq_slice_async_rq = cfq_slice_async_rq; |
| 2229 | cfqd->cfq_slice_idle = cfq_slice_idle; |
| 2230 | cfqd->cfq_max_depth = cfq_max_depth; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | return 0; |
| 2233 | out_crqpool: |
| 2234 | kfree(cfqd->cfq_hash); |
| 2235 | out_cfqhash: |
| 2236 | kfree(cfqd->crq_hash); |
| 2237 | out_crqhash: |
| 2238 | kfree(cfqd); |
| 2239 | return -ENOMEM; |
| 2240 | } |
| 2241 | |
| 2242 | static void cfq_slab_kill(void) |
| 2243 | { |
| 2244 | if (crq_pool) |
| 2245 | kmem_cache_destroy(crq_pool); |
| 2246 | if (cfq_pool) |
| 2247 | kmem_cache_destroy(cfq_pool); |
| 2248 | if (cfq_ioc_pool) |
| 2249 | kmem_cache_destroy(cfq_ioc_pool); |
| 2250 | } |
| 2251 | |
| 2252 | static int __init cfq_slab_setup(void) |
| 2253 | { |
| 2254 | crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0, |
| 2255 | NULL, NULL); |
| 2256 | if (!crq_pool) |
| 2257 | goto fail; |
| 2258 | |
| 2259 | cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0, |
| 2260 | NULL, NULL); |
| 2261 | if (!cfq_pool) |
| 2262 | goto fail; |
| 2263 | |
| 2264 | cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool", |
| 2265 | sizeof(struct cfq_io_context), 0, 0, NULL, NULL); |
| 2266 | if (!cfq_ioc_pool) |
| 2267 | goto fail; |
| 2268 | |
| 2269 | return 0; |
| 2270 | fail: |
| 2271 | cfq_slab_kill(); |
| 2272 | return -ENOMEM; |
| 2273 | } |
| 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | /* |
| 2276 | * sysfs parts below --> |
| 2277 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | |
| 2279 | static ssize_t |
| 2280 | cfq_var_show(unsigned int var, char *page) |
| 2281 | { |
| 2282 | return sprintf(page, "%d\n", var); |
| 2283 | } |
| 2284 | |
| 2285 | static ssize_t |
| 2286 | cfq_var_store(unsigned int *var, const char *page, size_t count) |
| 2287 | { |
| 2288 | char *p = (char *) page; |
| 2289 | |
| 2290 | *var = simple_strtoul(p, &p, 10); |
| 2291 | return count; |
| 2292 | } |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 2295 | static ssize_t __FUNC(elevator_t *e, char *page) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 2297 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | unsigned int __data = __VAR; \ |
| 2299 | if (__CONV) \ |
| 2300 | __data = jiffies_to_msecs(__data); \ |
| 2301 | return cfq_var_show(__data, (page)); \ |
| 2302 | } |
| 2303 | SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); |
| 2304 | SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2305 | SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); |
| 2306 | SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 2307 | SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); |
| 2308 | SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2309 | SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); |
| 2310 | SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); |
| 2311 | SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); |
| 2312 | SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); |
| 2313 | SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | #undef SHOW_FUNCTION |
| 2315 | |
| 2316 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 2317 | static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 2319 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | unsigned int __data; \ |
| 2321 | int ret = cfq_var_store(&__data, (page), count); \ |
| 2322 | if (__data < (MIN)) \ |
| 2323 | __data = (MIN); \ |
| 2324 | else if (__data > (MAX)) \ |
| 2325 | __data = (MAX); \ |
| 2326 | if (__CONV) \ |
| 2327 | *(__PTR) = msecs_to_jiffies(__data); \ |
| 2328 | else \ |
| 2329 | *(__PTR) = __data; \ |
| 2330 | return ret; \ |
| 2331 | } |
| 2332 | STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); |
| 2333 | STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2334 | STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1); |
| 2335 | STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 2336 | STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); |
| 2337 | STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2338 | STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); |
| 2339 | STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); |
| 2340 | STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); |
| 2341 | STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0); |
| 2342 | STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | #undef STORE_FUNCTION |
| 2344 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 2345 | #define CFQ_ATTR(name) \ |
| 2346 | __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2347 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 2348 | static struct elv_fs_entry cfq_attrs[] = { |
| 2349 | CFQ_ATTR(quantum), |
| 2350 | CFQ_ATTR(queued), |
| 2351 | CFQ_ATTR(fifo_expire_sync), |
| 2352 | CFQ_ATTR(fifo_expire_async), |
| 2353 | CFQ_ATTR(back_seek_max), |
| 2354 | CFQ_ATTR(back_seek_penalty), |
| 2355 | CFQ_ATTR(slice_sync), |
| 2356 | CFQ_ATTR(slice_async), |
| 2357 | CFQ_ATTR(slice_async_rq), |
| 2358 | CFQ_ATTR(slice_idle), |
| 2359 | CFQ_ATTR(max_depth), |
| 2360 | __ATTR_NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | }; |
| 2362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | static struct elevator_type iosched_cfq = { |
| 2364 | .ops = { |
| 2365 | .elevator_merge_fn = cfq_merge, |
| 2366 | .elevator_merged_fn = cfq_merged_request, |
| 2367 | .elevator_merge_req_fn = cfq_merged_requests, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2368 | .elevator_dispatch_fn = cfq_dispatch_requests, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | .elevator_add_req_fn = cfq_insert_request, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2370 | .elevator_activate_req_fn = cfq_activate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | .elevator_deactivate_req_fn = cfq_deactivate_request, |
| 2372 | .elevator_queue_empty_fn = cfq_queue_empty, |
| 2373 | .elevator_completed_req_fn = cfq_completed_request, |
| 2374 | .elevator_former_req_fn = cfq_former_request, |
| 2375 | .elevator_latter_req_fn = cfq_latter_request, |
| 2376 | .elevator_set_req_fn = cfq_set_request, |
| 2377 | .elevator_put_req_fn = cfq_put_request, |
| 2378 | .elevator_may_queue_fn = cfq_may_queue, |
| 2379 | .elevator_init_fn = cfq_init_queue, |
| 2380 | .elevator_exit_fn = cfq_exit_queue, |
Al Viro | e17a948 | 2006-03-18 13:21:20 -0500 | [diff] [blame] | 2381 | .trim = cfq_trim, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | }, |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 2383 | .elevator_attrs = cfq_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | .elevator_name = "cfq", |
| 2385 | .elevator_owner = THIS_MODULE, |
| 2386 | }; |
| 2387 | |
| 2388 | static int __init cfq_init(void) |
| 2389 | { |
| 2390 | int ret; |
| 2391 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2392 | /* |
| 2393 | * could be 0 on HZ < 1000 setups |
| 2394 | */ |
| 2395 | if (!cfq_slice_async) |
| 2396 | cfq_slice_async = 1; |
| 2397 | if (!cfq_slice_idle) |
| 2398 | cfq_slice_idle = 1; |
| 2399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | if (cfq_slab_setup()) |
| 2401 | return -ENOMEM; |
| 2402 | |
| 2403 | ret = elv_register(&iosched_cfq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2404 | if (ret) |
| 2405 | cfq_slab_kill(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | return ret; |
| 2408 | } |
| 2409 | |
| 2410 | static void __exit cfq_exit(void) |
| 2411 | { |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 2412 | DECLARE_COMPLETION(all_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | elv_unregister(&iosched_cfq); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 2414 | ioc_gone = &all_gone; |
| 2415 | barrier(); |
| 2416 | if (atomic_read(&ioc_count)) |
| 2417 | complete(ioc_gone); |
| 2418 | synchronize_rcu(); |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 2419 | cfq_slab_kill(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | module_init(cfq_init); |
| 2423 | module_exit(cfq_exit); |
| 2424 | |
| 2425 | MODULE_AUTHOR("Jens Axboe"); |
| 2426 | MODULE_LICENSE("GPL"); |
| 2427 | MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler"); |