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