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