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