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