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