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