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