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