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