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