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