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