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