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> |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 12 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/rbtree.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 14 | #include <linux/ioprio.h> |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 15 | #include <linux/blktrace_api.h> |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 16 | #include "blk-cgroup.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * tunables |
| 20 | */ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 21 | /* max queue in one round of service */ |
| 22 | static const int cfq_quantum = 4; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 23 | static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 }; |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 24 | /* maximum backwards seek, in KiB */ |
| 25 | static const int cfq_back_max = 16 * 1024; |
| 26 | /* penalty of a backwards seek */ |
| 27 | static const int cfq_back_penalty = 2; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 28 | static const int cfq_slice_sync = HZ / 10; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 29 | static int cfq_slice_async = HZ / 25; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 30 | static const int cfq_slice_async_rq = 2; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 31 | static int cfq_slice_idle = HZ / 125; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 32 | static const int cfq_target_latency = HZ * 3/10; /* 300 ms */ |
| 33 | static const int cfq_hist_divisor = 4; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 34 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 35 | /* |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 36 | * offset from end of service tree |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 37 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 38 | #define CFQ_IDLE_DELAY (HZ / 5) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * below this threshold, we consider thinktime immediate |
| 42 | */ |
| 43 | #define CFQ_MIN_TT (2) |
| 44 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 45 | /* |
| 46 | * Allow merged cfqqs to perform this amount of seeky I/O before |
| 47 | * deciding to break the queues up again. |
| 48 | */ |
| 49 | #define CFQQ_COOP_TOUT (HZ) |
| 50 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 51 | #define CFQ_SLICE_SCALE (5) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 52 | #define CFQ_HW_QUEUE_MIN (5) |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 53 | #define CFQ_SERVICE_SHIFT 12 |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 54 | |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 55 | #define RQ_CIC(rq) \ |
| 56 | ((struct cfq_io_context *) (rq)->elevator_private) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 57 | #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 59 | static struct kmem_cache *cfq_pool; |
| 60 | static struct kmem_cache *cfq_ioc_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 62 | static DEFINE_PER_CPU(unsigned long, cfq_ioc_count); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 63 | static struct completion *ioc_gone; |
Jens Axboe | 9a11b4e | 2008-05-29 09:32:08 +0200 | [diff] [blame] | 64 | static DEFINE_SPINLOCK(ioc_gone_lock); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 65 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 66 | #define CFQ_PRIO_LISTS IOPRIO_BE_NR |
| 67 | #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 68 | #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) |
| 69 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 70 | #define sample_valid(samples) ((samples) > 80) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 71 | #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node) |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 72 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 73 | /* |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 74 | * Most of our rbtree usage is for sorting with min extraction, so |
| 75 | * if we cache the leftmost node we don't have to walk down the tree |
| 76 | * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should |
| 77 | * move this into the elevator for the rq sorting as well. |
| 78 | */ |
| 79 | struct cfq_rb_root { |
| 80 | struct rb_root rb; |
| 81 | struct rb_node *left; |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 82 | unsigned count; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 83 | u64 min_vdisktime; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 84 | struct rb_node *active; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 85 | unsigned total_weight; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 86 | }; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 87 | #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, 0, 0, } |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 88 | |
| 89 | /* |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 90 | * Per process-grouping structure |
| 91 | */ |
| 92 | struct cfq_queue { |
| 93 | /* reference count */ |
| 94 | atomic_t ref; |
| 95 | /* various state flags, see below */ |
| 96 | unsigned int flags; |
| 97 | /* parent cfq_data */ |
| 98 | struct cfq_data *cfqd; |
| 99 | /* service_tree member */ |
| 100 | struct rb_node rb_node; |
| 101 | /* service_tree key */ |
| 102 | unsigned long rb_key; |
| 103 | /* prio tree member */ |
| 104 | struct rb_node p_node; |
| 105 | /* prio tree root we belong to, if any */ |
| 106 | struct rb_root *p_root; |
| 107 | /* sorted list of pending requests */ |
| 108 | struct rb_root sort_list; |
| 109 | /* if fifo isn't expired, next request to serve */ |
| 110 | struct request *next_rq; |
| 111 | /* requests queued in sort_list */ |
| 112 | int queued[2]; |
| 113 | /* currently allocated requests */ |
| 114 | int allocated[2]; |
| 115 | /* fifo list of requests in sort_list */ |
| 116 | struct list_head fifo; |
| 117 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 118 | /* time when queue got scheduled in to dispatch first request. */ |
| 119 | unsigned long dispatch_start; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 120 | unsigned int allocated_slice; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 121 | /* time when first request from queue completed and slice started. */ |
| 122 | unsigned long slice_start; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 123 | unsigned long slice_end; |
| 124 | long slice_resid; |
| 125 | unsigned int slice_dispatch; |
| 126 | |
| 127 | /* pending metadata requests */ |
| 128 | int meta_pending; |
| 129 | /* number of requests that are on the dispatch list or inside driver */ |
| 130 | int dispatched; |
| 131 | |
| 132 | /* io prio of this group */ |
| 133 | unsigned short ioprio, org_ioprio; |
| 134 | unsigned short ioprio_class, org_ioprio_class; |
| 135 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 136 | unsigned int seek_samples; |
| 137 | u64 seek_total; |
| 138 | sector_t seek_mean; |
| 139 | sector_t last_request_pos; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 140 | unsigned long seeky_start; |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 141 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 142 | pid_t pid; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 143 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 144 | struct cfq_rb_root *service_tree; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 145 | struct cfq_queue *new_cfqq; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 146 | struct cfq_group *cfqg; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 147 | /* Sectors dispatched in current dispatch round */ |
| 148 | unsigned long nr_sectors; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 152 | * First index in the service_trees. |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 153 | * IDLE is handled separately, so it has negative index |
| 154 | */ |
| 155 | enum wl_prio_t { |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 156 | BE_WORKLOAD = 0, |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 157 | RT_WORKLOAD = 1, |
| 158 | IDLE_WORKLOAD = 2, |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /* |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 162 | * Second index in the service_trees. |
| 163 | */ |
| 164 | enum wl_type_t { |
| 165 | ASYNC_WORKLOAD = 0, |
| 166 | SYNC_NOIDLE_WORKLOAD = 1, |
| 167 | SYNC_WORKLOAD = 2 |
| 168 | }; |
| 169 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 170 | /* This is per cgroup per device grouping structure */ |
| 171 | struct cfq_group { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 172 | /* group service_tree member */ |
| 173 | struct rb_node rb_node; |
| 174 | |
| 175 | /* group service_tree key */ |
| 176 | u64 vdisktime; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 177 | unsigned int weight; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 178 | bool on_st; |
| 179 | |
| 180 | /* number of cfqq currently on this group */ |
| 181 | int nr_cfqq; |
| 182 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 183 | /* Per group busy queus average. Useful for workload slice calc. */ |
| 184 | unsigned int busy_queues_avg[2]; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 185 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 186 | * rr lists of queues with requests, onle rr for each priority class. |
| 187 | * Counts are embedded in the cfq_rb_root |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 188 | */ |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 189 | struct cfq_rb_root service_trees[2][3]; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 190 | struct cfq_rb_root service_tree_idle; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 191 | |
| 192 | unsigned long saved_workload_slice; |
| 193 | enum wl_type_t saved_workload; |
| 194 | enum wl_prio_t saved_serving_prio; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 195 | struct blkio_group blkg; |
| 196 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 197 | struct hlist_node cfqd_node; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 198 | atomic_t ref; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 199 | #endif |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | /* |
| 203 | * Per block device queue structure |
| 204 | */ |
| 205 | struct cfq_data { |
| 206 | struct request_queue *queue; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 207 | /* Root service tree for cfq_groups */ |
| 208 | struct cfq_rb_root grp_service_tree; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 209 | struct cfq_group root_group; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 210 | /* Number of active cfq groups on group service tree */ |
| 211 | int nr_groups; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 212 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 213 | /* |
| 214 | * The priority currently being served |
| 215 | */ |
| 216 | enum wl_prio_t serving_prio; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 217 | enum wl_type_t serving_type; |
| 218 | unsigned long workload_expires; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 219 | struct cfq_group *serving_group; |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 220 | bool noidle_tree_requires_idle; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Each priority tree is sorted by next_request position. These |
| 224 | * trees are used when determining if two or more queues are |
| 225 | * interleaving requests (see cfq_close_cooperator). |
| 226 | */ |
| 227 | struct rb_root prio_trees[CFQ_PRIO_LISTS]; |
| 228 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 229 | unsigned int busy_queues; |
| 230 | |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 231 | int rq_in_driver[2]; |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 232 | int sync_flight; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * queue-depth detection |
| 236 | */ |
| 237 | int rq_queued; |
Jens Axboe | 25776e3 | 2006-06-01 10:12:26 +0200 | [diff] [blame] | 238 | int hw_tag; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 239 | /* |
| 240 | * hw_tag can be |
| 241 | * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection) |
| 242 | * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth) |
| 243 | * 0 => no NCQ |
| 244 | */ |
| 245 | int hw_tag_est_depth; |
| 246 | unsigned int hw_tag_samples; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 247 | |
| 248 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 249 | * idle window management |
| 250 | */ |
| 251 | struct timer_list idle_slice_timer; |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 252 | struct work_struct unplug_work; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 253 | |
| 254 | struct cfq_queue *active_queue; |
| 255 | struct cfq_io_context *active_cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 256 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 257 | /* |
| 258 | * async queue for each priority case |
| 259 | */ |
| 260 | struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR]; |
| 261 | struct cfq_queue *async_idle_cfqq; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 262 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 263 | sector_t last_position; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* |
| 266 | * tunables, see top of file |
| 267 | */ |
| 268 | unsigned int cfq_quantum; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 269 | unsigned int cfq_fifo_expire[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | unsigned int cfq_back_penalty; |
| 271 | unsigned int cfq_back_max; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 272 | unsigned int cfq_slice[2]; |
| 273 | unsigned int cfq_slice_async_rq; |
| 274 | unsigned int cfq_slice_idle; |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 275 | unsigned int cfq_latency; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 276 | |
| 277 | struct list_head cic_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 279 | /* |
| 280 | * Fallback dummy cfqq for extreme OOM conditions |
| 281 | */ |
| 282 | struct cfq_queue oom_cfqq; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 283 | |
| 284 | unsigned long last_end_sync_rq; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 285 | |
| 286 | /* List of cfq groups being managed on this device*/ |
| 287 | struct hlist_head cfqg_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 290 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd); |
| 291 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 292 | static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg, |
| 293 | enum wl_prio_t prio, |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 294 | enum wl_type_t type, |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 295 | struct cfq_data *cfqd) |
| 296 | { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 297 | if (!cfqg) |
| 298 | return NULL; |
| 299 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 300 | if (prio == IDLE_WORKLOAD) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 301 | return &cfqg->service_tree_idle; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 302 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 303 | return &cfqg->service_trees[prio][type]; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 306 | enum cfqq_state_flags { |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 307 | CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ |
| 308 | CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 309 | CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */ |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 310 | CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */ |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 311 | CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */ |
| 312 | CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */ |
| 313 | CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 314 | CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 315 | CFQ_CFQQ_FLAG_sync, /* synchronous queue */ |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 316 | CFQ_CFQQ_FLAG_coop, /* cfqq is shared */ |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 317 | CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */ |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 318 | CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */ |
| 319 | CFQ_CFQQ_FLAG_wait_busy_done, /* Got new request. Expire the queue */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | #define CFQ_CFQQ_FNS(name) \ |
| 323 | static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ |
| 324 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 325 | (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 326 | } \ |
| 327 | static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ |
| 328 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 329 | (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 330 | } \ |
| 331 | static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ |
| 332 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 333 | return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | CFQ_CFQQ_FNS(on_rr); |
| 337 | CFQ_CFQQ_FNS(wait_request); |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 338 | CFQ_CFQQ_FNS(must_dispatch); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 339 | CFQ_CFQQ_FNS(must_alloc_slice); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 340 | CFQ_CFQQ_FNS(fifo_expire); |
| 341 | CFQ_CFQQ_FNS(idle_window); |
| 342 | CFQ_CFQQ_FNS(prio_changed); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 343 | CFQ_CFQQ_FNS(slice_new); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 344 | CFQ_CFQQ_FNS(sync); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 345 | CFQ_CFQQ_FNS(coop); |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 346 | CFQ_CFQQ_FNS(deep); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 347 | CFQ_CFQQ_FNS(wait_busy); |
| 348 | CFQ_CFQQ_FNS(wait_busy_done); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 349 | #undef CFQ_CFQQ_FNS |
| 350 | |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 351 | #ifdef CONFIG_DEBUG_CFQ_IOSCHED |
| 352 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ |
| 353 | blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \ |
| 354 | cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ |
| 355 | blkg_path(&(cfqq)->cfqg->blkg), ##args); |
| 356 | |
| 357 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \ |
| 358 | blk_add_trace_msg((cfqd)->queue, "%s " fmt, \ |
| 359 | blkg_path(&(cfqg)->blkg), ##args); \ |
| 360 | |
| 361 | #else |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 362 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ |
| 363 | blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args) |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 364 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0); |
| 365 | #endif |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 366 | #define cfq_log(cfqd, fmt, args...) \ |
| 367 | blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args) |
| 368 | |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 369 | /* Traverses through cfq group service trees */ |
| 370 | #define for_each_cfqg_st(cfqg, i, j, st) \ |
| 371 | for (i = 0; i <= IDLE_WORKLOAD; i++) \ |
| 372 | for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\ |
| 373 | : &cfqg->service_tree_idle; \ |
| 374 | (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \ |
| 375 | (i == IDLE_WORKLOAD && j == 0); \ |
| 376 | j++, st = i < IDLE_WORKLOAD ? \ |
| 377 | &cfqg->service_trees[i][j]: NULL) \ |
| 378 | |
| 379 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 380 | static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq) |
| 381 | { |
| 382 | if (cfq_class_idle(cfqq)) |
| 383 | return IDLE_WORKLOAD; |
| 384 | if (cfq_class_rt(cfqq)) |
| 385 | return RT_WORKLOAD; |
| 386 | return BE_WORKLOAD; |
| 387 | } |
| 388 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 389 | |
| 390 | static enum wl_type_t cfqq_type(struct cfq_queue *cfqq) |
| 391 | { |
| 392 | if (!cfq_cfqq_sync(cfqq)) |
| 393 | return ASYNC_WORKLOAD; |
| 394 | if (!cfq_cfqq_idle_window(cfqq)) |
| 395 | return SYNC_NOIDLE_WORKLOAD; |
| 396 | return SYNC_WORKLOAD; |
| 397 | } |
| 398 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 399 | static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl, |
| 400 | struct cfq_data *cfqd, |
| 401 | struct cfq_group *cfqg) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 402 | { |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 403 | if (wl == IDLE_WORKLOAD) |
| 404 | return cfqg->service_tree_idle.count; |
| 405 | |
| 406 | return cfqg->service_trees[wl][ASYNC_WORKLOAD].count |
| 407 | + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count |
| 408 | + cfqg->service_trees[wl][SYNC_WORKLOAD].count; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 411 | static void cfq_dispatch_insert(struct request_queue *, struct request *); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 412 | static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool, |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 413 | struct io_context *, gfp_t); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 414 | static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *, |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 415 | struct io_context *); |
| 416 | |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 417 | static inline int rq_in_driver(struct cfq_data *cfqd) |
| 418 | { |
| 419 | return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1]; |
| 420 | } |
| 421 | |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 422 | static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 423 | bool is_sync) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 424 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 425 | return cic->cfqq[is_sync]; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static inline void cic_set_cfqq(struct cfq_io_context *cic, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 429 | struct cfq_queue *cfqq, bool is_sync) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 430 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 431 | cic->cfqq[is_sync] = cfqq; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | * We regard a request as SYNC, if it's either a read or has the SYNC bit |
| 436 | * set (in which case it could also be direct WRITE). |
| 437 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 438 | static inline bool cfq_bio_sync(struct bio *bio) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 439 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 440 | return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /* |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 444 | * scheduler run of queue, if there are requests pending and no one in the |
| 445 | * driver that will restart queueing |
| 446 | */ |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 447 | static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 448 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 449 | if (cfqd->busy_queues) { |
| 450 | cfq_log(cfqd, "schedule dispatch"); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 451 | kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work); |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 452 | } |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 455 | static int cfq_queue_empty(struct request_queue *q) |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 456 | { |
| 457 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 458 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 459 | return !cfqd->rq_queued; |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | /* |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 463 | * Scale schedule slice based on io priority. Use the sync time slice only |
| 464 | * if a queue is marked sync and has sync io queued. A sync queue with async |
| 465 | * io only, should not get full sync slice length. |
| 466 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 467 | static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync, |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 468 | unsigned short prio) |
| 469 | { |
| 470 | const int base_slice = cfqd->cfq_slice[sync]; |
| 471 | |
| 472 | WARN_ON(prio >= IOPRIO_BE_NR); |
| 473 | |
| 474 | return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio)); |
| 475 | } |
| 476 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 477 | static inline int |
| 478 | cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 479 | { |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 480 | return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 481 | } |
| 482 | |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 483 | static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg) |
| 484 | { |
| 485 | u64 d = delta << CFQ_SERVICE_SHIFT; |
| 486 | |
| 487 | d = d * BLKIO_WEIGHT_DEFAULT; |
| 488 | do_div(d, cfqg->weight); |
| 489 | return d; |
| 490 | } |
| 491 | |
| 492 | static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime) |
| 493 | { |
| 494 | s64 delta = (s64)(vdisktime - min_vdisktime); |
| 495 | if (delta > 0) |
| 496 | min_vdisktime = vdisktime; |
| 497 | |
| 498 | return min_vdisktime; |
| 499 | } |
| 500 | |
| 501 | static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime) |
| 502 | { |
| 503 | s64 delta = (s64)(vdisktime - min_vdisktime); |
| 504 | if (delta < 0) |
| 505 | min_vdisktime = vdisktime; |
| 506 | |
| 507 | return min_vdisktime; |
| 508 | } |
| 509 | |
| 510 | static void update_min_vdisktime(struct cfq_rb_root *st) |
| 511 | { |
| 512 | u64 vdisktime = st->min_vdisktime; |
| 513 | struct cfq_group *cfqg; |
| 514 | |
| 515 | if (st->active) { |
| 516 | cfqg = rb_entry_cfqg(st->active); |
| 517 | vdisktime = cfqg->vdisktime; |
| 518 | } |
| 519 | |
| 520 | if (st->left) { |
| 521 | cfqg = rb_entry_cfqg(st->left); |
| 522 | vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime); |
| 523 | } |
| 524 | |
| 525 | st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime); |
| 526 | } |
| 527 | |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 528 | /* |
| 529 | * get averaged number of queues of RT/BE priority. |
| 530 | * average is updated, with a formula that gives more weight to higher numbers, |
| 531 | * to quickly follows sudden increases and decrease slowly |
| 532 | */ |
| 533 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 534 | static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd, |
| 535 | struct cfq_group *cfqg, bool rt) |
Jens Axboe | 5869619c | 2009-10-28 09:27:07 +0100 | [diff] [blame] | 536 | { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 537 | unsigned min_q, max_q; |
| 538 | unsigned mult = cfq_hist_divisor - 1; |
| 539 | unsigned round = cfq_hist_divisor / 2; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 540 | unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 541 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 542 | min_q = min(cfqg->busy_queues_avg[rt], busy); |
| 543 | max_q = max(cfqg->busy_queues_avg[rt], busy); |
| 544 | cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) / |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 545 | cfq_hist_divisor; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 546 | return cfqg->busy_queues_avg[rt]; |
| 547 | } |
| 548 | |
| 549 | static inline unsigned |
| 550 | cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg) |
| 551 | { |
| 552 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 553 | |
| 554 | return cfq_target_latency * cfqg->weight / st->total_weight; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 557 | static inline void |
| 558 | cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 559 | { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 560 | unsigned slice = cfq_prio_to_slice(cfqd, cfqq); |
| 561 | if (cfqd->cfq_latency) { |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 562 | /* |
| 563 | * interested queues (we consider only the ones with the same |
| 564 | * priority class in the cfq group) |
| 565 | */ |
| 566 | unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg, |
| 567 | cfq_class_rt(cfqq)); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 568 | unsigned sync_slice = cfqd->cfq_slice[1]; |
| 569 | unsigned expect_latency = sync_slice * iq; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 570 | unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg); |
| 571 | |
| 572 | if (expect_latency > group_slice) { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 573 | unsigned base_low_slice = 2 * cfqd->cfq_slice_idle; |
| 574 | /* scale low_slice according to IO priority |
| 575 | * and sync vs async */ |
| 576 | unsigned low_slice = |
| 577 | min(slice, base_low_slice * slice / sync_slice); |
| 578 | /* the adapted slice value is scaled to fit all iqs |
| 579 | * into the target latency */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 580 | slice = max(slice * group_slice / expect_latency, |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 581 | low_slice); |
| 582 | } |
| 583 | } |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 584 | cfqq->slice_start = jiffies; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 585 | cfqq->slice_end = jiffies + slice; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 586 | cfqq->allocated_slice = slice; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 587 | cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* |
| 591 | * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end |
| 592 | * isn't valid until the first request from the dispatch is activated |
| 593 | * and the slice time set. |
| 594 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 595 | static inline bool cfq_slice_used(struct cfq_queue *cfqq) |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 596 | { |
| 597 | if (cfq_cfqq_slice_new(cfqq)) |
| 598 | return 0; |
| 599 | if (time_before(jiffies, cfqq->slice_end)) |
| 600 | return 0; |
| 601 | |
| 602 | return 1; |
| 603 | } |
| 604 | |
| 605 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 606 | * 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] | 607 | * 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] | 608 | * behind the head is penalized and only allowed to a certain extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 610 | static struct request * |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 611 | cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 613 | sector_t s1, s2, d1 = 0, d2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | unsigned long back_max; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 615 | #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ |
| 616 | #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ |
| 617 | unsigned wrap = 0; /* bit mask: requests behind the disk head? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 619 | if (rq1 == NULL || rq1 == rq2) |
| 620 | return rq2; |
| 621 | if (rq2 == NULL) |
| 622 | return rq1; |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 623 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 624 | if (rq_is_sync(rq1) && !rq_is_sync(rq2)) |
| 625 | return rq1; |
| 626 | else if (rq_is_sync(rq2) && !rq_is_sync(rq1)) |
| 627 | return rq2; |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 628 | if (rq_is_meta(rq1) && !rq_is_meta(rq2)) |
| 629 | return rq1; |
| 630 | else if (rq_is_meta(rq2) && !rq_is_meta(rq1)) |
| 631 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 633 | s1 = blk_rq_pos(rq1); |
| 634 | s2 = blk_rq_pos(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /* |
| 637 | * by definition, 1KiB is 2 sectors |
| 638 | */ |
| 639 | back_max = cfqd->cfq_back_max * 2; |
| 640 | |
| 641 | /* |
| 642 | * Strict one way elevator _except_ in the case where we allow |
| 643 | * short backward seeks which are biased as twice the cost of a |
| 644 | * similar forward seek. |
| 645 | */ |
| 646 | if (s1 >= last) |
| 647 | d1 = s1 - last; |
| 648 | else if (s1 + back_max >= last) |
| 649 | d1 = (last - s1) * cfqd->cfq_back_penalty; |
| 650 | else |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 651 | wrap |= CFQ_RQ1_WRAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | if (s2 >= last) |
| 654 | d2 = s2 - last; |
| 655 | else if (s2 + back_max >= last) |
| 656 | d2 = (last - s2) * cfqd->cfq_back_penalty; |
| 657 | else |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 658 | wrap |= CFQ_RQ2_WRAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | /* Found required data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 662 | /* |
| 663 | * By doing switch() on the bit mask "wrap" we avoid having to |
| 664 | * check two variables for all permutations: --> faster! |
| 665 | */ |
| 666 | switch (wrap) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 667 | case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 668 | if (d1 < d2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 669 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 670 | else if (d2 < d1) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 671 | return rq2; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 672 | else { |
| 673 | if (s1 >= s2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 674 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 675 | else |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 676 | return rq2; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | case CFQ_RQ2_WRAP: |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 680 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 681 | case CFQ_RQ1_WRAP: |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 682 | return rq2; |
| 683 | case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */ |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 684 | default: |
| 685 | /* |
| 686 | * Since both rqs are wrapped, |
| 687 | * start with the one that's further behind head |
| 688 | * (--> only *one* back seek required), |
| 689 | * since back seek takes more time than forward. |
| 690 | */ |
| 691 | if (s1 <= s2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 692 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | else |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 694 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 698 | /* |
| 699 | * The below is leftmost cache rbtree addon |
| 700 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 701 | static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root) |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 702 | { |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 703 | /* Service tree is empty */ |
| 704 | if (!root->count) |
| 705 | return NULL; |
| 706 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 707 | if (!root->left) |
| 708 | root->left = rb_first(&root->rb); |
| 709 | |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 710 | if (root->left) |
| 711 | return rb_entry(root->left, struct cfq_queue, rb_node); |
| 712 | |
| 713 | return NULL; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 716 | static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root) |
| 717 | { |
| 718 | if (!root->left) |
| 719 | root->left = rb_first(&root->rb); |
| 720 | |
| 721 | if (root->left) |
| 722 | return rb_entry_cfqg(root->left); |
| 723 | |
| 724 | return NULL; |
| 725 | } |
| 726 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 727 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 728 | { |
| 729 | rb_erase(n, root); |
| 730 | RB_CLEAR_NODE(n); |
| 731 | } |
| 732 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 733 | static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root) |
| 734 | { |
| 735 | if (root->left == n) |
| 736 | root->left = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 737 | rb_erase_init(n, &root->rb); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 738 | --root->count; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | /* |
| 742 | * would be nice to take fifo expire time into account as well |
| 743 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 744 | static struct request * |
| 745 | cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 746 | struct request *last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 748 | struct rb_node *rbnext = rb_next(&last->rb_node); |
| 749 | struct rb_node *rbprev = rb_prev(&last->rb_node); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 750 | struct request *next = NULL, *prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 752 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | if (rbprev) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 755 | prev = rb_entry_rq(rbprev); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | if (rbnext) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 758 | next = rb_entry_rq(rbnext); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 759 | else { |
| 760 | rbnext = rb_first(&cfqq->sort_list); |
| 761 | if (rbnext && rbnext != &last->rb_node) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 762 | next = rb_entry_rq(rbnext); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 765 | return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 768 | static unsigned long cfq_slice_offset(struct cfq_data *cfqd, |
| 769 | struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | { |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 771 | /* |
| 772 | * just an approximation, should be ok. |
| 773 | */ |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 774 | return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) - |
Jens Axboe | 464191c | 2009-11-30 09:38:13 +0100 | [diff] [blame] | 775 | cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio)); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 778 | static inline s64 |
| 779 | cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 780 | { |
| 781 | return cfqg->vdisktime - st->min_vdisktime; |
| 782 | } |
| 783 | |
| 784 | static void |
| 785 | __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 786 | { |
| 787 | struct rb_node **node = &st->rb.rb_node; |
| 788 | struct rb_node *parent = NULL; |
| 789 | struct cfq_group *__cfqg; |
| 790 | s64 key = cfqg_key(st, cfqg); |
| 791 | int left = 1; |
| 792 | |
| 793 | while (*node != NULL) { |
| 794 | parent = *node; |
| 795 | __cfqg = rb_entry_cfqg(parent); |
| 796 | |
| 797 | if (key < cfqg_key(st, __cfqg)) |
| 798 | node = &parent->rb_left; |
| 799 | else { |
| 800 | node = &parent->rb_right; |
| 801 | left = 0; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | if (left) |
| 806 | st->left = &cfqg->rb_node; |
| 807 | |
| 808 | rb_link_node(&cfqg->rb_node, parent, node); |
| 809 | rb_insert_color(&cfqg->rb_node, &st->rb); |
| 810 | } |
| 811 | |
| 812 | static void |
| 813 | cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg) |
| 814 | { |
| 815 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 816 | struct cfq_group *__cfqg; |
| 817 | struct rb_node *n; |
| 818 | |
| 819 | cfqg->nr_cfqq++; |
| 820 | if (cfqg->on_st) |
| 821 | return; |
| 822 | |
| 823 | /* |
| 824 | * Currently put the group at the end. Later implement something |
| 825 | * so that groups get lesser vtime based on their weights, so that |
| 826 | * if group does not loose all if it was not continously backlogged. |
| 827 | */ |
| 828 | n = rb_last(&st->rb); |
| 829 | if (n) { |
| 830 | __cfqg = rb_entry_cfqg(n); |
| 831 | cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY; |
| 832 | } else |
| 833 | cfqg->vdisktime = st->min_vdisktime; |
| 834 | |
| 835 | __cfq_group_service_tree_add(st, cfqg); |
| 836 | cfqg->on_st = true; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 837 | cfqd->nr_groups++; |
| 838 | st->total_weight += cfqg->weight; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static void |
| 842 | cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg) |
| 843 | { |
| 844 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 845 | |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 846 | if (st->active == &cfqg->rb_node) |
| 847 | st->active = NULL; |
| 848 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 849 | BUG_ON(cfqg->nr_cfqq < 1); |
| 850 | cfqg->nr_cfqq--; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 851 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 852 | /* If there are other cfq queues under this group, don't delete it */ |
| 853 | if (cfqg->nr_cfqq) |
| 854 | return; |
| 855 | |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 856 | cfq_log_cfqg(cfqd, cfqg, "del_from_rr group"); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 857 | cfqg->on_st = false; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 858 | cfqd->nr_groups--; |
| 859 | st->total_weight -= cfqg->weight; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 860 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) |
| 861 | cfq_rb_erase(&cfqg->rb_node, st); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 862 | cfqg->saved_workload_slice = 0; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 863 | blkiocg_update_blkio_group_dequeue_stats(&cfqg->blkg, 1); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq) |
| 867 | { |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 868 | unsigned int slice_used; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 869 | |
| 870 | /* |
| 871 | * Queue got expired before even a single request completed or |
| 872 | * got expired immediately after first request completion. |
| 873 | */ |
| 874 | if (!cfqq->slice_start || cfqq->slice_start == jiffies) { |
| 875 | /* |
| 876 | * Also charge the seek time incurred to the group, otherwise |
| 877 | * if there are mutiple queues in the group, each can dispatch |
| 878 | * a single request on seeky media and cause lots of seek time |
| 879 | * and group will never know it. |
| 880 | */ |
| 881 | slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start), |
| 882 | 1); |
| 883 | } else { |
| 884 | slice_used = jiffies - cfqq->slice_start; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 885 | if (slice_used > cfqq->allocated_slice) |
| 886 | slice_used = cfqq->allocated_slice; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 887 | } |
| 888 | |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 889 | cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u sect=%lu", slice_used, |
| 890 | cfqq->nr_sectors); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 891 | return slice_used; |
| 892 | } |
| 893 | |
| 894 | static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg, |
| 895 | struct cfq_queue *cfqq) |
| 896 | { |
| 897 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 898 | unsigned int used_sl; |
| 899 | |
| 900 | used_sl = cfq_cfqq_slice_usage(cfqq); |
| 901 | |
| 902 | /* Can't update vdisktime while group is on service tree */ |
| 903 | cfq_rb_erase(&cfqg->rb_node, st); |
| 904 | cfqg->vdisktime += cfq_scale_slice(used_sl, cfqg); |
| 905 | __cfq_group_service_tree_add(st, cfqg); |
| 906 | |
| 907 | /* This group is being expired. Save the context */ |
| 908 | if (time_after(cfqd->workload_expires, jiffies)) { |
| 909 | cfqg->saved_workload_slice = cfqd->workload_expires |
| 910 | - jiffies; |
| 911 | cfqg->saved_workload = cfqd->serving_type; |
| 912 | cfqg->saved_serving_prio = cfqd->serving_prio; |
| 913 | } else |
| 914 | cfqg->saved_workload_slice = 0; |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 915 | |
| 916 | cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime, |
| 917 | st->min_vdisktime); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 918 | blkiocg_update_blkio_group_stats(&cfqg->blkg, used_sl, |
| 919 | cfqq->nr_sectors); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 920 | } |
| 921 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 922 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 923 | static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg) |
| 924 | { |
| 925 | if (blkg) |
| 926 | return container_of(blkg, struct cfq_group, blkg); |
| 927 | return NULL; |
| 928 | } |
| 929 | |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 930 | void |
| 931 | cfq_update_blkio_group_weight(struct blkio_group *blkg, unsigned int weight) |
| 932 | { |
| 933 | cfqg_of_blkg(blkg)->weight = weight; |
| 934 | } |
| 935 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 936 | static struct cfq_group * |
| 937 | cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create) |
| 938 | { |
| 939 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 940 | struct cfq_group *cfqg = NULL; |
| 941 | void *key = cfqd; |
| 942 | int i, j; |
| 943 | struct cfq_rb_root *st; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 944 | struct backing_dev_info *bdi = &cfqd->queue->backing_dev_info; |
| 945 | unsigned int major, minor; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 946 | |
| 947 | /* Do we need to take this reference */ |
| 948 | if (!css_tryget(&blkcg->css)) |
| 949 | return NULL;; |
| 950 | |
| 951 | cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key)); |
| 952 | if (cfqg || !create) |
| 953 | goto done; |
| 954 | |
| 955 | cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node); |
| 956 | if (!cfqg) |
| 957 | goto done; |
| 958 | |
| 959 | cfqg->weight = blkcg->weight; |
| 960 | for_each_cfqg_st(cfqg, i, j, st) |
| 961 | *st = CFQ_RB_ROOT; |
| 962 | RB_CLEAR_NODE(&cfqg->rb_node); |
| 963 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 964 | /* |
| 965 | * Take the initial reference that will be released on destroy |
| 966 | * This can be thought of a joint reference by cgroup and |
| 967 | * elevator which will be dropped by either elevator exit |
| 968 | * or cgroup deletion path depending on who is exiting first. |
| 969 | */ |
| 970 | atomic_set(&cfqg->ref, 1); |
| 971 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 972 | /* Add group onto cgroup list */ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 973 | sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor); |
| 974 | blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd, |
| 975 | MKDEV(major, minor)); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 976 | |
| 977 | /* Add group on cfqd list */ |
| 978 | hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list); |
| 979 | |
| 980 | done: |
| 981 | css_put(&blkcg->css); |
| 982 | return cfqg; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Search for the cfq group current task belongs to. If create = 1, then also |
| 987 | * create the cfq group if it does not exist. request_queue lock must be held. |
| 988 | */ |
| 989 | static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create) |
| 990 | { |
| 991 | struct cgroup *cgroup; |
| 992 | struct cfq_group *cfqg = NULL; |
| 993 | |
| 994 | rcu_read_lock(); |
| 995 | cgroup = task_cgroup(current, blkio_subsys_id); |
| 996 | cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create); |
| 997 | if (!cfqg && create) |
| 998 | cfqg = &cfqd->root_group; |
| 999 | rcu_read_unlock(); |
| 1000 | return cfqg; |
| 1001 | } |
| 1002 | |
| 1003 | static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) |
| 1004 | { |
| 1005 | /* Currently, all async queues are mapped to root group */ |
| 1006 | if (!cfq_cfqq_sync(cfqq)) |
| 1007 | cfqg = &cfqq->cfqd->root_group; |
| 1008 | |
| 1009 | cfqq->cfqg = cfqg; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1010 | /* cfqq reference on cfqg */ |
| 1011 | atomic_inc(&cfqq->cfqg->ref); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1012 | } |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1013 | |
| 1014 | static void cfq_put_cfqg(struct cfq_group *cfqg) |
| 1015 | { |
| 1016 | struct cfq_rb_root *st; |
| 1017 | int i, j; |
| 1018 | |
| 1019 | BUG_ON(atomic_read(&cfqg->ref) <= 0); |
| 1020 | if (!atomic_dec_and_test(&cfqg->ref)) |
| 1021 | return; |
| 1022 | for_each_cfqg_st(cfqg, i, j, st) |
| 1023 | BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL); |
| 1024 | kfree(cfqg); |
| 1025 | } |
| 1026 | |
| 1027 | static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg) |
| 1028 | { |
| 1029 | /* Something wrong if we are trying to remove same group twice */ |
| 1030 | BUG_ON(hlist_unhashed(&cfqg->cfqd_node)); |
| 1031 | |
| 1032 | hlist_del_init(&cfqg->cfqd_node); |
| 1033 | |
| 1034 | /* |
| 1035 | * Put the reference taken at the time of creation so that when all |
| 1036 | * queues are gone, group can be destroyed. |
| 1037 | */ |
| 1038 | cfq_put_cfqg(cfqg); |
| 1039 | } |
| 1040 | |
| 1041 | static void cfq_release_cfq_groups(struct cfq_data *cfqd) |
| 1042 | { |
| 1043 | struct hlist_node *pos, *n; |
| 1044 | struct cfq_group *cfqg; |
| 1045 | |
| 1046 | hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) { |
| 1047 | /* |
| 1048 | * If cgroup removal path got to blk_group first and removed |
| 1049 | * it from cgroup list, then it will take care of destroying |
| 1050 | * cfqg also. |
| 1051 | */ |
| 1052 | if (!blkiocg_del_blkio_group(&cfqg->blkg)) |
| 1053 | cfq_destroy_cfqg(cfqd, cfqg); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * Blk cgroup controller notification saying that blkio_group object is being |
| 1059 | * delinked as associated cgroup object is going away. That also means that |
| 1060 | * no new IO will come in this group. So get rid of this group as soon as |
| 1061 | * any pending IO in the group is finished. |
| 1062 | * |
| 1063 | * This function is called under rcu_read_lock(). key is the rcu protected |
| 1064 | * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu |
| 1065 | * read lock. |
| 1066 | * |
| 1067 | * "key" was fetched from blkio_group under blkio_cgroup->lock. That means |
| 1068 | * it should not be NULL as even if elevator was exiting, cgroup deltion |
| 1069 | * path got to it first. |
| 1070 | */ |
| 1071 | void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg) |
| 1072 | { |
| 1073 | unsigned long flags; |
| 1074 | struct cfq_data *cfqd = key; |
| 1075 | |
| 1076 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 1077 | cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg)); |
| 1078 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
| 1079 | } |
| 1080 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1081 | #else /* GROUP_IOSCHED */ |
| 1082 | static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create) |
| 1083 | { |
| 1084 | return &cfqd->root_group; |
| 1085 | } |
| 1086 | static inline void |
| 1087 | cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) { |
| 1088 | cfqq->cfqg = cfqg; |
| 1089 | } |
| 1090 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1091 | static void cfq_release_cfq_groups(struct cfq_data *cfqd) {} |
| 1092 | static inline void cfq_put_cfqg(struct cfq_group *cfqg) {} |
| 1093 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1094 | #endif /* GROUP_IOSCHED */ |
| 1095 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1096 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1097 | * The cfqd->service_trees holds all pending cfq_queue's that have |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1098 | * requests waiting to be processed. It is sorted in the order that |
| 1099 | * we will service the queues. |
| 1100 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1101 | static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1102 | bool add_front) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1103 | { |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1104 | struct rb_node **p, *parent; |
| 1105 | struct cfq_queue *__cfqq; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1106 | unsigned long rb_key; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1107 | struct cfq_rb_root *service_tree; |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1108 | int left; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1109 | int new_cfqq = 1; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1110 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1111 | service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq), |
| 1112 | cfqq_type(cfqq), cfqd); |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1113 | if (cfq_class_idle(cfqq)) { |
| 1114 | rb_key = CFQ_IDLE_DELAY; |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1115 | parent = rb_last(&service_tree->rb); |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1116 | if (parent && parent != &cfqq->rb_node) { |
| 1117 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); |
| 1118 | rb_key += __cfqq->rb_key; |
| 1119 | } else |
| 1120 | rb_key += jiffies; |
| 1121 | } else if (!add_front) { |
Jens Axboe | b9c8946 | 2009-10-06 20:53:44 +0200 | [diff] [blame] | 1122 | /* |
| 1123 | * Get our rb key offset. Subtract any residual slice |
| 1124 | * value carried from last service. A negative resid |
| 1125 | * count indicates slice overrun, and this should position |
| 1126 | * the next service time further away in the tree. |
| 1127 | */ |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1128 | rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies; |
Jens Axboe | b9c8946 | 2009-10-06 20:53:44 +0200 | [diff] [blame] | 1129 | rb_key -= cfqq->slice_resid; |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1130 | cfqq->slice_resid = 0; |
Corrado Zoccolo | 48e025e | 2009-10-05 08:49:23 +0200 | [diff] [blame] | 1131 | } else { |
| 1132 | rb_key = -HZ; |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1133 | __cfqq = cfq_rb_first(service_tree); |
Corrado Zoccolo | 48e025e | 2009-10-05 08:49:23 +0200 | [diff] [blame] | 1134 | rb_key += __cfqq ? __cfqq->rb_key : jiffies; |
| 1135 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1136 | |
| 1137 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1138 | new_cfqq = 0; |
Jens Axboe | 99f9628 | 2007-02-05 11:56:25 +0100 | [diff] [blame] | 1139 | /* |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1140 | * same position, nothing more to do |
Jens Axboe | 99f9628 | 2007-02-05 11:56:25 +0100 | [diff] [blame] | 1141 | */ |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1142 | if (rb_key == cfqq->rb_key && |
| 1143 | cfqq->service_tree == service_tree) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1144 | return; |
Jens Axboe | 53b03744 | 2006-07-28 09:48:51 +0200 | [diff] [blame] | 1145 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1146 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
| 1147 | cfqq->service_tree = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1148 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1149 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1150 | left = 1; |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1151 | parent = NULL; |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1152 | cfqq->service_tree = service_tree; |
| 1153 | p = &service_tree->rb.rb_node; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1154 | while (*p) { |
Jens Axboe | 67060e3 | 2007-04-18 20:13:32 +0200 | [diff] [blame] | 1155 | struct rb_node **n; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1156 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1157 | parent = *p; |
| 1158 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); |
| 1159 | |
Jens Axboe | 0c534e0 | 2007-04-18 20:01:57 +0200 | [diff] [blame] | 1160 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1161 | * sort by key, that represents service time. |
Jens Axboe | 0c534e0 | 2007-04-18 20:01:57 +0200 | [diff] [blame] | 1162 | */ |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1163 | if (time_before(rb_key, __cfqq->rb_key)) |
Jens Axboe | 67060e3 | 2007-04-18 20:13:32 +0200 | [diff] [blame] | 1164 | n = &(*p)->rb_left; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1165 | else { |
Jens Axboe | 67060e3 | 2007-04-18 20:13:32 +0200 | [diff] [blame] | 1166 | n = &(*p)->rb_right; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1167 | left = 0; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1168 | } |
Jens Axboe | 67060e3 | 2007-04-18 20:13:32 +0200 | [diff] [blame] | 1169 | |
| 1170 | p = n; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1173 | if (left) |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1174 | service_tree->left = &cfqq->rb_node; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1175 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1176 | cfqq->rb_key = rb_key; |
| 1177 | rb_link_node(&cfqq->rb_node, parent, p); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1178 | rb_insert_color(&cfqq->rb_node, &service_tree->rb); |
| 1179 | service_tree->count++; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1180 | if (add_front || !new_cfqq) |
| 1181 | return; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1182 | cfq_group_service_tree_add(cfqd, cfqq->cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1185 | static struct cfq_queue * |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1186 | cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root, |
| 1187 | sector_t sector, struct rb_node **ret_parent, |
| 1188 | struct rb_node ***rb_link) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1189 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1190 | struct rb_node **p, *parent; |
| 1191 | struct cfq_queue *cfqq = NULL; |
| 1192 | |
| 1193 | parent = NULL; |
| 1194 | p = &root->rb_node; |
| 1195 | while (*p) { |
| 1196 | struct rb_node **n; |
| 1197 | |
| 1198 | parent = *p; |
| 1199 | cfqq = rb_entry(parent, struct cfq_queue, p_node); |
| 1200 | |
| 1201 | /* |
| 1202 | * Sort strictly based on sector. Smallest to the left, |
| 1203 | * largest to the right. |
| 1204 | */ |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1205 | if (sector > blk_rq_pos(cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1206 | n = &(*p)->rb_right; |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1207 | else if (sector < blk_rq_pos(cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1208 | n = &(*p)->rb_left; |
| 1209 | else |
| 1210 | break; |
| 1211 | p = n; |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 1212 | cfqq = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | *ret_parent = parent; |
| 1216 | if (rb_link) |
| 1217 | *rb_link = p; |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 1218 | return cfqq; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1222 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1223 | struct rb_node **p, *parent; |
| 1224 | struct cfq_queue *__cfqq; |
| 1225 | |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1226 | if (cfqq->p_root) { |
| 1227 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 1228 | cfqq->p_root = NULL; |
| 1229 | } |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1230 | |
| 1231 | if (cfq_class_idle(cfqq)) |
| 1232 | return; |
| 1233 | if (!cfqq->next_rq) |
| 1234 | return; |
| 1235 | |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1236 | cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio]; |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1237 | __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root, |
| 1238 | blk_rq_pos(cfqq->next_rq), &parent, &p); |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 1239 | if (!__cfqq) { |
| 1240 | rb_link_node(&cfqq->p_node, parent, p); |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1241 | rb_insert_color(&cfqq->p_node, cfqq->p_root); |
| 1242 | } else |
| 1243 | cfqq->p_root = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1246 | /* |
| 1247 | * Update cfqq's position in the service tree. |
| 1248 | */ |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1249 | static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1250 | { |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1251 | /* |
| 1252 | * Resorting requires the cfqq to be on the RR list already. |
| 1253 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1254 | if (cfq_cfqq_on_rr(cfqq)) { |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1255 | cfq_service_tree_add(cfqd, cfqq, 0); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1256 | cfq_prio_tree_add(cfqd, cfqq); |
| 1257 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | /* |
| 1261 | * 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] | 1262 | * the pending list according to last request service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 1264 | static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1266 | cfq_log_cfqq(cfqd, cfqq, "add_to_rr"); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1267 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
| 1268 | cfq_mark_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | cfqd->busy_queues++; |
| 1270 | |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1271 | cfq_resort_rr_list(cfqd, cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1274 | /* |
| 1275 | * Called when the cfqq no longer has requests pending, remove it from |
| 1276 | * the service tree. |
| 1277 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 1278 | static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1280 | cfq_log_cfqq(cfqd, cfqq, "del_from_rr"); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1281 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
| 1282 | cfq_clear_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1284 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
| 1285 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
| 1286 | cfqq->service_tree = NULL; |
| 1287 | } |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1288 | if (cfqq->p_root) { |
| 1289 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 1290 | cfqq->p_root = NULL; |
| 1291 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1292 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1293 | cfq_group_service_tree_del(cfqd, cfqq->cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | BUG_ON(!cfqd->busy_queues); |
| 1295 | cfqd->busy_queues--; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * rb tree support functions |
| 1300 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 1301 | static void cfq_del_rq_rb(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1303 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1304 | const int sync = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1306 | BUG_ON(!cfqq->queued[sync]); |
| 1307 | cfqq->queued[sync]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1309 | elv_rb_del(&cfqq->sort_list, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1311 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) { |
| 1312 | /* |
| 1313 | * Queue will be deleted from service tree when we actually |
| 1314 | * expire it later. Right now just remove it from prio tree |
| 1315 | * as it is empty. |
| 1316 | */ |
| 1317 | if (cfqq->p_root) { |
| 1318 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 1319 | cfqq->p_root = NULL; |
| 1320 | } |
| 1321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1324 | static void cfq_add_rq_rb(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1326 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | struct cfq_data *cfqd = cfqq->cfqd; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1328 | struct request *__alias, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 1330 | cfqq->queued[rq_is_sync(rq)]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
| 1332 | /* |
| 1333 | * looks a little odd, but the first insert might return an alias. |
| 1334 | * if that happens, put the alias on the dispatch list |
| 1335 | */ |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1336 | while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1337 | cfq_dispatch_insert(cfqd->queue, __alias); |
Jens Axboe | 5fccbf6 | 2006-10-31 14:21:55 +0100 | [diff] [blame] | 1338 | |
| 1339 | if (!cfq_cfqq_on_rr(cfqq)) |
| 1340 | cfq_add_cfqq_rr(cfqd, cfqq); |
Jens Axboe | 5044eed | 2007-04-25 11:53:48 +0200 | [diff] [blame] | 1341 | |
| 1342 | /* |
| 1343 | * check if this request is a better next-serve candidate |
| 1344 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1345 | prev = cfqq->next_rq; |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1346 | cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1347 | |
| 1348 | /* |
| 1349 | * adjust priority tree position, if ->next_rq changes |
| 1350 | */ |
| 1351 | if (prev != cfqq->next_rq) |
| 1352 | cfq_prio_tree_add(cfqd, cfqq); |
| 1353 | |
Jens Axboe | 5044eed | 2007-04-25 11:53:48 +0200 | [diff] [blame] | 1354 | BUG_ON(!cfqq->next_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 1357 | static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | { |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 1359 | elv_rb_del(&cfqq->sort_list, rq); |
| 1360 | cfqq->queued[rq_is_sync(rq)]--; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1361 | cfq_add_rq_rb(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1364 | static struct request * |
| 1365 | cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | { |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1367 | struct task_struct *tsk = current; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 1368 | struct cfq_io_context *cic; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1369 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 1371 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 1372 | if (!cic) |
| 1373 | return NULL; |
| 1374 | |
| 1375 | cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio)); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 1376 | if (cfqq) { |
| 1377 | sector_t sector = bio->bi_sector + bio_sectors(bio); |
| 1378 | |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1379 | return elv_rb_find(&cfqq->sort_list, sector); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | return NULL; |
| 1383 | } |
| 1384 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1385 | static void cfq_activate_request(struct request_queue *q, struct request *rq) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1386 | { |
| 1387 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 1388 | |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 1389 | cfqd->rq_in_driver[rq_is_sync(rq)]++; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1390 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d", |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 1391 | rq_in_driver(cfqd)); |
Jens Axboe | 25776e3 | 2006-06-01 10:12:26 +0200 | [diff] [blame] | 1392 | |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 1393 | cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1396 | static void cfq_deactivate_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1398 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 1399 | const int sync = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 1401 | WARN_ON(!cfqd->rq_in_driver[sync]); |
| 1402 | cfqd->rq_in_driver[sync]--; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1403 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d", |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 1404 | rq_in_driver(cfqd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1407 | static void cfq_remove_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1409 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1410 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1411 | if (cfqq->next_rq == rq) |
| 1412 | cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1414 | list_del_init(&rq->queuelist); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1415 | cfq_del_rq_rb(rq); |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 1416 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 1417 | cfqq->cfqd->rq_queued--; |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 1418 | if (rq_is_meta(rq)) { |
| 1419 | WARN_ON(!cfqq->meta_pending); |
| 1420 | cfqq->meta_pending--; |
| 1421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1424 | static int cfq_merge(struct request_queue *q, struct request **req, |
| 1425 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | { |
| 1427 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 1428 | struct request *__rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1430 | __rq = cfq_find_rq_fmerge(cfqd, bio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1431 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 1432 | *req = __rq; |
| 1433 | return ELEVATOR_FRONT_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | return ELEVATOR_NO_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1439 | static void cfq_merged_request(struct request_queue *q, struct request *req, |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1440 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1442 | if (type == ELEVATOR_FRONT_MERGE) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1443 | struct cfq_queue *cfqq = RQ_CFQQ(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1445 | cfq_reposition_rq_rb(cfqq, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | static void |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1450 | cfq_merged_requests(struct request_queue *q, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | struct request *next) |
| 1452 | { |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1453 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1454 | /* |
| 1455 | * reposition in fifo if next is older than rq |
| 1456 | */ |
| 1457 | if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 1458 | time_before(rq_fifo_time(next), rq_fifo_time(rq))) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1459 | list_move(&rq->queuelist, &next->queuelist); |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 1460 | rq_set_fifo_time(rq, rq_fifo_time(next)); |
| 1461 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1462 | |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1463 | if (cfqq->next_rq == next) |
| 1464 | cfqq->next_rq = rq; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 1465 | cfq_remove_request(next); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1468 | static int cfq_allow_merge(struct request_queue *q, struct request *rq, |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1469 | struct bio *bio) |
| 1470 | { |
| 1471 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 1472 | struct cfq_io_context *cic; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1473 | struct cfq_queue *cfqq; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1474 | |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 1475 | /* Deny merge if bio and rq don't belong to same cfq group */ |
| 1476 | if ((RQ_CFQQ(rq))->cfqg != cfq_get_cfqg(cfqd, 0)) |
| 1477 | return false; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1478 | /* |
Jens Axboe | ec8acb6 | 2007-01-02 18:32:11 +0100 | [diff] [blame] | 1479 | * Disallow merge of a sync bio into an async request. |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1480 | */ |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 1481 | if (cfq_bio_sync(bio) && !rq_is_sync(rq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1482 | return false; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* |
Jens Axboe | 719d340 | 2006-12-22 09:38:53 +0100 | [diff] [blame] | 1485 | * Lookup the cfqq that this bio will be queued with. Allow |
| 1486 | * merge only if rq is queued there. |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1487 | */ |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 1488 | cic = cfq_cic_lookup(cfqd, current->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 1489 | if (!cic) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1490 | return false; |
Jens Axboe | 719d340 | 2006-12-22 09:38:53 +0100 | [diff] [blame] | 1491 | |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 1492 | cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio)); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1493 | return cfqq == RQ_CFQQ(rq); |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 1494 | } |
| 1495 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 1496 | static void __cfq_set_active_queue(struct cfq_data *cfqd, |
| 1497 | struct cfq_queue *cfqq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1498 | { |
| 1499 | if (cfqq) { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1500 | cfq_log_cfqq(cfqd, cfqq, "set_active"); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1501 | cfqq->slice_start = 0; |
| 1502 | cfqq->dispatch_start = jiffies; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 1503 | cfqq->allocated_slice = 0; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1504 | cfqq->slice_end = 0; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 1505 | cfqq->slice_dispatch = 0; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1506 | cfqq->nr_sectors = 0; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 1507 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 1508 | cfq_clear_cfqq_wait_request(cfqq); |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 1509 | cfq_clear_cfqq_must_dispatch(cfqq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1510 | cfq_clear_cfqq_must_alloc_slice(cfqq); |
| 1511 | cfq_clear_cfqq_fifo_expire(cfqq); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1512 | cfq_mark_cfqq_slice_new(cfqq); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 1513 | |
| 1514 | del_timer(&cfqd->idle_slice_timer); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | cfqd->active_queue = cfqq; |
| 1518 | } |
| 1519 | |
| 1520 | /* |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1521 | * current cfqq expired its slice (or was too idle), select new one |
| 1522 | */ |
| 1523 | static void |
| 1524 | __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1525 | bool timed_out) |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1526 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1527 | cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out); |
| 1528 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1529 | if (cfq_cfqq_wait_request(cfqq)) |
| 1530 | del_timer(&cfqd->idle_slice_timer); |
| 1531 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1532 | cfq_clear_cfqq_wait_request(cfqq); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 1533 | cfq_clear_cfqq_wait_busy(cfqq); |
| 1534 | cfq_clear_cfqq_wait_busy_done(cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1535 | |
| 1536 | /* |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 1537 | * store what was left of this slice, if the queue idled/timed out |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1538 | */ |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1539 | if (timed_out && !cfq_cfqq_slice_new(cfqq)) { |
Jens Axboe | c5b680f | 2007-01-19 11:56:49 +1100 | [diff] [blame] | 1540 | cfqq->slice_resid = cfqq->slice_end - jiffies; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1541 | cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid); |
| 1542 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1543 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1544 | cfq_group_served(cfqd, cfqq->cfqg, cfqq); |
| 1545 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1546 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 1547 | cfq_del_cfqq_rr(cfqd, cfqq); |
| 1548 | |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1549 | cfq_resort_rr_list(cfqd, cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1550 | |
| 1551 | if (cfqq == cfqd->active_queue) |
| 1552 | cfqd->active_queue = NULL; |
| 1553 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1554 | if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active) |
| 1555 | cfqd->grp_service_tree.active = NULL; |
| 1556 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1557 | if (cfqd->active_cic) { |
| 1558 | put_io_context(cfqd->active_cic->ioc); |
| 1559 | cfqd->active_cic = NULL; |
| 1560 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1563 | static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out) |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1564 | { |
| 1565 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 1566 | |
| 1567 | if (cfqq) |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 1568 | __cfq_slice_expired(cfqd, cfqq, timed_out); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1571 | /* |
| 1572 | * Get next queue for service. Unless we have a queue preemption, |
| 1573 | * we'll simply select the first cfqq in the service tree. |
| 1574 | */ |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1575 | static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1576 | { |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1577 | struct cfq_rb_root *service_tree = |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1578 | service_tree_for(cfqd->serving_group, cfqd->serving_prio, |
| 1579 | cfqd->serving_type, cfqd); |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1580 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1581 | if (!cfqd->rq_queued) |
| 1582 | return NULL; |
| 1583 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1584 | /* There is nothing to dispatch */ |
| 1585 | if (!service_tree) |
| 1586 | return NULL; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1587 | if (RB_EMPTY_ROOT(&service_tree->rb)) |
| 1588 | return NULL; |
| 1589 | return cfq_rb_first(service_tree); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1592 | static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd) |
| 1593 | { |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1594 | struct cfq_group *cfqg; |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1595 | struct cfq_queue *cfqq; |
| 1596 | int i, j; |
| 1597 | struct cfq_rb_root *st; |
| 1598 | |
| 1599 | if (!cfqd->rq_queued) |
| 1600 | return NULL; |
| 1601 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1602 | cfqg = cfq_get_next_cfqg(cfqd); |
| 1603 | if (!cfqg) |
| 1604 | return NULL; |
| 1605 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1606 | for_each_cfqg_st(cfqg, i, j, st) |
| 1607 | if ((cfqq = cfq_rb_first(st)) != NULL) |
| 1608 | return cfqq; |
| 1609 | return NULL; |
| 1610 | } |
| 1611 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1612 | /* |
| 1613 | * Get and set a new active queue for service. |
| 1614 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1615 | static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd, |
| 1616 | struct cfq_queue *cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1617 | { |
Jens Axboe | e00ef79 | 2009-11-04 08:54:55 +0100 | [diff] [blame] | 1618 | if (!cfqq) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1619 | cfqq = cfq_get_next_queue(cfqd); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1620 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1621 | __cfq_set_active_queue(cfqd, cfqq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1622 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1623 | } |
| 1624 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1625 | static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd, |
| 1626 | struct request *rq) |
| 1627 | { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1628 | if (blk_rq_pos(rq) >= cfqd->last_position) |
| 1629 | return blk_rq_pos(rq) - cfqd->last_position; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1630 | else |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1631 | return cfqd->last_position - blk_rq_pos(rq); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1632 | } |
| 1633 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 1634 | #define CFQQ_SEEK_THR 8 * 1024 |
| 1635 | #define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR) |
Jeff Moyer | 04dc6e7 | 2009-04-21 07:31:56 +0200 | [diff] [blame] | 1636 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 1637 | static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1638 | struct request *rq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1639 | { |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 1640 | sector_t sdist = cfqq->seek_mean; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 1641 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 1642 | if (!sample_valid(cfqq->seek_samples)) |
| 1643 | sdist = CFQQ_SEEK_THR; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1644 | |
Jeff Moyer | 04dc6e7 | 2009-04-21 07:31:56 +0200 | [diff] [blame] | 1645 | return cfq_dist_from_last(cfqd, rq) <= sdist; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1648 | static struct cfq_queue *cfqq_close(struct cfq_data *cfqd, |
| 1649 | struct cfq_queue *cur_cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1650 | { |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1651 | struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio]; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1652 | struct rb_node *parent, *node; |
| 1653 | struct cfq_queue *__cfqq; |
| 1654 | sector_t sector = cfqd->last_position; |
| 1655 | |
| 1656 | if (RB_EMPTY_ROOT(root)) |
| 1657 | return NULL; |
| 1658 | |
| 1659 | /* |
| 1660 | * First, if we find a request starting at the end of the last |
| 1661 | * request, choose it. |
| 1662 | */ |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1663 | __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1664 | if (__cfqq) |
| 1665 | return __cfqq; |
| 1666 | |
| 1667 | /* |
| 1668 | * If the exact sector wasn't found, the parent of the NULL leaf |
| 1669 | * will contain the closest sector. |
| 1670 | */ |
| 1671 | __cfqq = rb_entry(parent, struct cfq_queue, p_node); |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 1672 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1673 | return __cfqq; |
| 1674 | |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1675 | if (blk_rq_pos(__cfqq->next_rq) < sector) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1676 | node = rb_next(&__cfqq->p_node); |
| 1677 | else |
| 1678 | node = rb_prev(&__cfqq->p_node); |
| 1679 | if (!node) |
| 1680 | return NULL; |
| 1681 | |
| 1682 | __cfqq = rb_entry(node, struct cfq_queue, p_node); |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 1683 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1684 | return __cfqq; |
| 1685 | |
| 1686 | return NULL; |
| 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | * cfqd - obvious |
| 1691 | * cur_cfqq - passed in so that we don't decide that the current queue is |
| 1692 | * closely cooperating with itself. |
| 1693 | * |
| 1694 | * So, basically we're assuming that that cur_cfqq has dispatched at least |
| 1695 | * one request, and that cfqd->last_position reflects a position on the disk |
| 1696 | * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid |
| 1697 | * assumption. |
| 1698 | */ |
| 1699 | static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd, |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 1700 | struct cfq_queue *cur_cfqq) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1701 | { |
| 1702 | struct cfq_queue *cfqq; |
| 1703 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 1704 | if (!cfq_cfqq_sync(cur_cfqq)) |
| 1705 | return NULL; |
| 1706 | if (CFQQ_SEEKY(cur_cfqq)) |
| 1707 | return NULL; |
| 1708 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1709 | /* |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1710 | * We should notice if some of the queues are cooperating, eg |
| 1711 | * working closely on the same area of the disk. In that case, |
| 1712 | * we can group them together and don't waste time idling. |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1713 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1714 | cfqq = cfqq_close(cfqd, cur_cfqq); |
| 1715 | if (!cfqq) |
| 1716 | return NULL; |
| 1717 | |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 1718 | /* If new queue belongs to different cfq_group, don't choose it */ |
| 1719 | if (cur_cfqq->cfqg != cfqq->cfqg) |
| 1720 | return NULL; |
| 1721 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 1722 | /* |
| 1723 | * It only makes sense to merge sync queues. |
| 1724 | */ |
| 1725 | if (!cfq_cfqq_sync(cfqq)) |
| 1726 | return NULL; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 1727 | if (CFQQ_SEEKY(cfqq)) |
| 1728 | return NULL; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 1729 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1730 | /* |
| 1731 | * Do not merge queues of different priority classes |
| 1732 | */ |
| 1733 | if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq)) |
| 1734 | return NULL; |
| 1735 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1736 | return cfqq; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 1739 | /* |
| 1740 | * Determine whether we should enforce idle window for this queue. |
| 1741 | */ |
| 1742 | |
| 1743 | static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1744 | { |
| 1745 | enum wl_prio_t prio = cfqq_prio(cfqq); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1746 | struct cfq_rb_root *service_tree = cfqq->service_tree; |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 1747 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1748 | BUG_ON(!service_tree); |
| 1749 | BUG_ON(!service_tree->count); |
| 1750 | |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 1751 | /* We never do for idle class queues. */ |
| 1752 | if (prio == IDLE_WORKLOAD) |
| 1753 | return false; |
| 1754 | |
| 1755 | /* We do for queues that were marked with idle window flag. */ |
| 1756 | if (cfq_cfqq_idle_window(cfqq)) |
| 1757 | return true; |
| 1758 | |
| 1759 | /* |
| 1760 | * Otherwise, we do only if they are the last ones |
| 1761 | * in their service tree. |
| 1762 | */ |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 1763 | return service_tree->count == 1; |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1766 | static void cfq_arm_slice_timer(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1767 | { |
Jens Axboe | 1792669 | 2007-01-19 11:59:30 +1100 | [diff] [blame] | 1768 | struct cfq_queue *cfqq = cfqd->active_queue; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1769 | struct cfq_io_context *cic; |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1770 | unsigned long sl; |
| 1771 | |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 1772 | /* |
Jens Axboe | f7d7b7a | 2008-09-25 11:37:50 +0200 | [diff] [blame] | 1773 | * SSD device without seek penalty, disable idling. But only do so |
| 1774 | * for devices that support queuing, otherwise we still have a problem |
| 1775 | * with sync vs async workloads. |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 1776 | */ |
Jens Axboe | f7d7b7a | 2008-09-25 11:37:50 +0200 | [diff] [blame] | 1777 | if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag) |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 1778 | return; |
| 1779 | |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 1780 | WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list)); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1781 | WARN_ON(cfq_cfqq_slice_new(cfqq)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1782 | |
| 1783 | /* |
| 1784 | * idle is disabled, either manually or by past process history |
| 1785 | */ |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 1786 | if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq)) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1787 | return; |
| 1788 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1789 | /* |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 1790 | * still active requests from this queue, don't idle |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1791 | */ |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 1792 | if (cfqq->dispatched) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1793 | return; |
| 1794 | |
| 1795 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1796 | * task has exited, don't wait |
| 1797 | */ |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1798 | cic = cfqd->active_cic; |
Nikanth Karthikesan | 66dac98 | 2007-11-27 12:47:04 +0100 | [diff] [blame] | 1799 | if (!cic || !atomic_read(&cic->ioc->nr_tasks)) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1800 | return; |
| 1801 | |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 1802 | /* |
| 1803 | * If our average think time is larger than the remaining time |
| 1804 | * slice, then don't idle. This avoids overrunning the allotted |
| 1805 | * time slice. |
| 1806 | */ |
| 1807 | if (sample_valid(cic->ttime_samples) && |
| 1808 | (cfqq->slice_end - jiffies < cic->ttime_mean)) |
| 1809 | return; |
| 1810 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1811 | cfq_mark_cfqq_wait_request(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1812 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1813 | sl = cfqd->cfq_slice_idle; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 1814 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 1815 | mod_timer(&cfqd->idle_slice_timer, jiffies + sl); |
Jens Axboe | 9481ffd | 2009-04-15 12:14:13 +0200 | [diff] [blame] | 1816 | cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1819 | /* |
| 1820 | * Move request from internal lists to the request queue dispatch list. |
| 1821 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1822 | static void cfq_dispatch_insert(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | { |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 1824 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1825 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1826 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1827 | cfq_log_cfqq(cfqd, cfqq, "dispatch_insert"); |
| 1828 | |
Jeff Moyer | 06d2188 | 2009-09-11 17:08:59 +0200 | [diff] [blame] | 1829 | cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq); |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 1830 | cfq_remove_request(rq); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1831 | cfqq->dispatched++; |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 1832 | elv_dispatch_sort(q, rq); |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 1833 | |
| 1834 | if (cfq_cfqq_sync(cfqq)) |
| 1835 | cfqd->sync_flight++; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1836 | cfqq->nr_sectors += blk_rq_sectors(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * return expired entry, or NULL to just start from scratch in rbtree |
| 1841 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 1842 | static struct request *cfq_check_fifo(struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | { |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 1844 | struct request *rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 1846 | if (cfq_cfqq_fifo_expire(cfqq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | return NULL; |
Jens Axboe | cb88741 | 2007-01-19 12:01:16 +1100 | [diff] [blame] | 1848 | |
| 1849 | cfq_mark_cfqq_fifo_expire(cfqq); |
| 1850 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 1851 | if (list_empty(&cfqq->fifo)) |
| 1852 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 1854 | rq = rq_entry_fifo(cfqq->fifo.next); |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 1855 | if (time_before(jiffies, rq_fifo_time(rq))) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1856 | rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 1858 | cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1859 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1862 | static inline int |
| 1863 | cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1864 | { |
| 1865 | const int base_rq = cfqd->cfq_slice_async_rq; |
| 1866 | |
| 1867 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
| 1868 | |
| 1869 | return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio)); |
| 1870 | } |
| 1871 | |
| 1872 | /* |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 1873 | * Must be called with the queue_lock held. |
| 1874 | */ |
| 1875 | static int cfqq_process_refs(struct cfq_queue *cfqq) |
| 1876 | { |
| 1877 | int process_refs, io_refs; |
| 1878 | |
| 1879 | io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE]; |
| 1880 | process_refs = atomic_read(&cfqq->ref) - io_refs; |
| 1881 | BUG_ON(process_refs < 0); |
| 1882 | return process_refs; |
| 1883 | } |
| 1884 | |
| 1885 | static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq) |
| 1886 | { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 1887 | int process_refs, new_process_refs; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 1888 | struct cfq_queue *__cfqq; |
| 1889 | |
| 1890 | /* Avoid a circular list and skip interim queue merges */ |
| 1891 | while ((__cfqq = new_cfqq->new_cfqq)) { |
| 1892 | if (__cfqq == cfqq) |
| 1893 | return; |
| 1894 | new_cfqq = __cfqq; |
| 1895 | } |
| 1896 | |
| 1897 | process_refs = cfqq_process_refs(cfqq); |
| 1898 | /* |
| 1899 | * If the process for the cfqq has gone away, there is no |
| 1900 | * sense in merging the queues. |
| 1901 | */ |
| 1902 | if (process_refs == 0) |
| 1903 | return; |
| 1904 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 1905 | /* |
| 1906 | * Merge in the direction of the lesser amount of work. |
| 1907 | */ |
| 1908 | new_process_refs = cfqq_process_refs(new_cfqq); |
| 1909 | if (new_process_refs >= process_refs) { |
| 1910 | cfqq->new_cfqq = new_cfqq; |
| 1911 | atomic_add(process_refs, &new_cfqq->ref); |
| 1912 | } else { |
| 1913 | new_cfqq->new_cfqq = cfqq; |
| 1914 | atomic_add(new_process_refs, &cfqq->ref); |
| 1915 | } |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 1916 | } |
| 1917 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1918 | static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd, |
| 1919 | struct cfq_group *cfqg, enum wl_prio_t prio, |
| 1920 | bool prio_changed) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1921 | { |
| 1922 | struct cfq_queue *queue; |
| 1923 | int i; |
| 1924 | bool key_valid = false; |
| 1925 | unsigned long lowest_key = 0; |
| 1926 | enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD; |
| 1927 | |
| 1928 | if (prio_changed) { |
| 1929 | /* |
| 1930 | * When priorities switched, we prefer starting |
| 1931 | * from SYNC_NOIDLE (first choice), or just SYNC |
| 1932 | * over ASYNC |
| 1933 | */ |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1934 | if (service_tree_for(cfqg, prio, cur_best, cfqd)->count) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1935 | return cur_best; |
| 1936 | cur_best = SYNC_WORKLOAD; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1937 | if (service_tree_for(cfqg, prio, cur_best, cfqd)->count) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1938 | return cur_best; |
| 1939 | |
| 1940 | return ASYNC_WORKLOAD; |
| 1941 | } |
| 1942 | |
| 1943 | for (i = 0; i < 3; ++i) { |
| 1944 | /* otherwise, select the one with lowest rb_key */ |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1945 | queue = cfq_rb_first(service_tree_for(cfqg, prio, i, cfqd)); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1946 | if (queue && |
| 1947 | (!key_valid || time_before(queue->rb_key, lowest_key))) { |
| 1948 | lowest_key = queue->rb_key; |
| 1949 | cur_best = i; |
| 1950 | key_valid = true; |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | return cur_best; |
| 1955 | } |
| 1956 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1957 | static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1958 | { |
| 1959 | enum wl_prio_t previous_prio = cfqd->serving_prio; |
| 1960 | bool prio_changed; |
| 1961 | unsigned slice; |
| 1962 | unsigned count; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1963 | struct cfq_rb_root *st; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1964 | unsigned group_slice; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1965 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1966 | if (!cfqg) { |
| 1967 | cfqd->serving_prio = IDLE_WORKLOAD; |
| 1968 | cfqd->workload_expires = jiffies + 1; |
| 1969 | return; |
| 1970 | } |
| 1971 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1972 | /* Choose next priority. RT > BE > IDLE */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1973 | if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg)) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1974 | cfqd->serving_prio = RT_WORKLOAD; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1975 | else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg)) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1976 | cfqd->serving_prio = BE_WORKLOAD; |
| 1977 | else { |
| 1978 | cfqd->serving_prio = IDLE_WORKLOAD; |
| 1979 | cfqd->workload_expires = jiffies + 1; |
| 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * For RT and BE, we have to choose also the type |
| 1985 | * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload |
| 1986 | * expiration time |
| 1987 | */ |
| 1988 | prio_changed = (cfqd->serving_prio != previous_prio); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1989 | st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type, |
| 1990 | cfqd); |
| 1991 | count = st->count; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 1992 | |
| 1993 | /* |
| 1994 | * If priority didn't change, check workload expiration, |
| 1995 | * and that we still have other queues ready |
| 1996 | */ |
| 1997 | if (!prio_changed && count && |
| 1998 | !time_after(jiffies, cfqd->workload_expires)) |
| 1999 | return; |
| 2000 | |
| 2001 | /* otherwise select new workload type */ |
| 2002 | cfqd->serving_type = |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2003 | cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio, prio_changed); |
| 2004 | st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type, |
| 2005 | cfqd); |
| 2006 | count = st->count; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2007 | |
| 2008 | /* |
| 2009 | * the workload slice is computed as a fraction of target latency |
| 2010 | * proportional to the number of queues in that workload, over |
| 2011 | * all the queues in the same priority class |
| 2012 | */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 2013 | group_slice = cfq_group_slice(cfqd, cfqg); |
| 2014 | |
| 2015 | slice = group_slice * count / |
| 2016 | max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio], |
| 2017 | cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg)); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2018 | |
| 2019 | if (cfqd->serving_type == ASYNC_WORKLOAD) |
| 2020 | /* async workload slice is scaled down according to |
| 2021 | * the sync/async slice ratio. */ |
| 2022 | slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1]; |
| 2023 | else |
| 2024 | /* sync workload slice is at least 2 * cfq_slice_idle */ |
| 2025 | slice = max(slice, 2 * cfqd->cfq_slice_idle); |
| 2026 | |
| 2027 | slice = max_t(unsigned, slice, CFQ_MIN_TT); |
| 2028 | cfqd->workload_expires = jiffies + slice; |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2029 | cfqd->noidle_tree_requires_idle = false; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2030 | } |
| 2031 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2032 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd) |
| 2033 | { |
| 2034 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 2035 | struct cfq_group *cfqg; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2036 | |
| 2037 | if (RB_EMPTY_ROOT(&st->rb)) |
| 2038 | return NULL; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 2039 | cfqg = cfq_rb_first_group(st); |
| 2040 | st->active = &cfqg->rb_node; |
| 2041 | update_min_vdisktime(st); |
| 2042 | return cfqg; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2043 | } |
| 2044 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2045 | static void cfq_choose_cfqg(struct cfq_data *cfqd) |
| 2046 | { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2047 | struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd); |
| 2048 | |
| 2049 | cfqd->serving_group = cfqg; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2050 | |
| 2051 | /* Restore the workload type data */ |
| 2052 | if (cfqg->saved_workload_slice) { |
| 2053 | cfqd->workload_expires = jiffies + cfqg->saved_workload_slice; |
| 2054 | cfqd->serving_type = cfqg->saved_workload; |
| 2055 | cfqd->serving_prio = cfqg->saved_serving_prio; |
| 2056 | } |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2057 | choose_service_tree(cfqd, cfqg); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2058 | } |
| 2059 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2060 | /* |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2061 | * Select a queue for service. If we have a current active queue, |
| 2062 | * check whether to continue servicing it, or retrieve and set a new one. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2063 | */ |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2064 | static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2065 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2066 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2067 | |
| 2068 | cfqq = cfqd->active_queue; |
| 2069 | if (!cfqq) |
| 2070 | goto new_queue; |
| 2071 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2072 | if (!cfqd->rq_queued) |
| 2073 | return NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2074 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2075 | * 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] | 2076 | */ |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 2077 | if ((cfq_slice_used(cfqq) || cfq_cfqq_wait_busy_done(cfqq)) |
| 2078 | && !cfq_cfqq_must_dispatch(cfqq)) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2079 | goto expire; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2080 | |
| 2081 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2082 | * The active queue has requests and isn't expired, allow it to |
| 2083 | * dispatch. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2084 | */ |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 2085 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2086 | goto keep_queue; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2087 | |
| 2088 | /* |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2089 | * If another queue has a request waiting within our mean seek |
| 2090 | * distance, let it run. The expire code will check for close |
| 2091 | * cooperators and put the close queue at the front of the service |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2092 | * tree. If possible, merge the expiring queue with the new cfqq. |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2093 | */ |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 2094 | new_cfqq = cfq_close_cooperator(cfqd, cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2095 | if (new_cfqq) { |
| 2096 | if (!cfqq->new_cfqq) |
| 2097 | cfq_setup_merge(cfqq, new_cfqq); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2098 | goto expire; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2099 | } |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2102 | * No requests pending. If the active queue still has requests in |
| 2103 | * flight or is idling for a new request, allow either of these |
| 2104 | * conditions to happen (or time out) before selecting a new queue. |
| 2105 | */ |
Jens Axboe | cc19747 | 2007-04-20 20:45:39 +0200 | [diff] [blame] | 2106 | if (timer_pending(&cfqd->idle_slice_timer) || |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2107 | (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) { |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2108 | cfqq = NULL; |
| 2109 | goto keep_queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2110 | } |
| 2111 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2112 | expire: |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 2113 | cfq_slice_expired(cfqd, 0); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2114 | new_queue: |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2115 | /* |
| 2116 | * Current queue expired. Check if we have to switch to a new |
| 2117 | * service tree |
| 2118 | */ |
| 2119 | if (!new_cfqq) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2120 | cfq_choose_cfqg(cfqd); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2121 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2122 | cfqq = cfq_set_active_queue(cfqd, new_cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2123 | keep_queue: |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2124 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2127 | static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2128 | { |
| 2129 | int dispatched = 0; |
| 2130 | |
| 2131 | while (cfqq->next_rq) { |
| 2132 | cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq); |
| 2133 | dispatched++; |
| 2134 | } |
| 2135 | |
| 2136 | BUG_ON(!list_empty(&cfqq->fifo)); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2137 | |
| 2138 | /* By default cfqq is not expired if it is empty. Do it explicitly */ |
| 2139 | __cfq_slice_expired(cfqq->cfqd, cfqq, 0); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2140 | return dispatched; |
| 2141 | } |
| 2142 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2143 | /* |
| 2144 | * Drain our current requests. Used for barriers and when switching |
| 2145 | * io schedulers on-the-fly. |
| 2146 | */ |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2147 | static int cfq_forced_dispatch(struct cfq_data *cfqd) |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2148 | { |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2149 | struct cfq_queue *cfqq; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2150 | int dispatched = 0; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2151 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2152 | while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) |
| 2153 | dispatched += __cfq_forced_dispatch_cfqq(cfqq); |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2154 | |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 2155 | cfq_slice_expired(cfqd, 0); |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2156 | BUG_ON(cfqd->busy_queues); |
| 2157 | |
Jeff Moyer | 6923715a | 2009-06-12 15:29:30 +0200 | [diff] [blame] | 2158 | cfq_log(cfqd, "forced_dispatch=%d", dispatched); |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2159 | return dispatched; |
| 2160 | } |
| 2161 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2162 | static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2163 | { |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2164 | unsigned int max_dispatch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2166 | /* |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 2167 | * Drain async requests before we start sync IO |
| 2168 | */ |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2169 | if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC]) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2170 | return false; |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 2171 | |
| 2172 | /* |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2173 | * If this is an async queue and we have sync IO in flight, let it wait |
| 2174 | */ |
| 2175 | if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq)) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2176 | return false; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2177 | |
| 2178 | max_dispatch = cfqd->cfq_quantum; |
| 2179 | if (cfq_class_idle(cfqq)) |
| 2180 | max_dispatch = 1; |
| 2181 | |
| 2182 | /* |
| 2183 | * Does this cfqq already have too much IO in flight? |
| 2184 | */ |
| 2185 | if (cfqq->dispatched >= max_dispatch) { |
| 2186 | /* |
| 2187 | * idle queue must always only have a single IO in flight |
| 2188 | */ |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 2189 | if (cfq_class_idle(cfqq)) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2190 | return false; |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 2191 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2192 | /* |
| 2193 | * We have other queues, don't allow more IO from this one |
| 2194 | */ |
| 2195 | if (cfqd->busy_queues > 1) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2196 | return false; |
Jens Axboe | 9ede209 | 2007-01-19 12:11:44 +1100 | [diff] [blame] | 2197 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2198 | /* |
Shaohua Li | 474b18c | 2009-12-03 12:58:05 +0100 | [diff] [blame] | 2199 | * Sole queue user, no limit |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 2200 | */ |
Shaohua Li | 474b18c | 2009-12-03 12:58:05 +0100 | [diff] [blame] | 2201 | max_dispatch = -1; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * Async queues must wait a bit before being allowed dispatch. |
| 2206 | * We also ramp up the dispatch depth gradually for async IO, |
| 2207 | * based on the last sync IO we serviced |
| 2208 | */ |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 2209 | if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) { |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 2210 | unsigned long last_sync = jiffies - cfqd->last_end_sync_rq; |
| 2211 | unsigned int depth; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 2212 | |
Jens Axboe | 61f0c1d | 2009-10-03 19:46:03 +0200 | [diff] [blame] | 2213 | depth = last_sync / cfqd->cfq_slice[1]; |
Jens Axboe | e00c54c | 2009-10-04 20:36:19 +0200 | [diff] [blame] | 2214 | if (!depth && !cfqq->dispatched) |
| 2215 | depth = 1; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 2216 | if (depth < max_dispatch) |
| 2217 | max_dispatch = depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2220 | /* |
| 2221 | * If we're below the current max, allow a dispatch |
| 2222 | */ |
| 2223 | return cfqq->dispatched < max_dispatch; |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * Dispatch a request from cfqq, moving them to the request queue |
| 2228 | * dispatch list. |
| 2229 | */ |
| 2230 | static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2231 | { |
| 2232 | struct request *rq; |
| 2233 | |
| 2234 | BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list)); |
| 2235 | |
| 2236 | if (!cfq_may_dispatch(cfqd, cfqq)) |
| 2237 | return false; |
| 2238 | |
| 2239 | /* |
| 2240 | * follow expired path, else get first next available |
| 2241 | */ |
| 2242 | rq = cfq_check_fifo(cfqq); |
| 2243 | if (!rq) |
| 2244 | rq = cfqq->next_rq; |
| 2245 | |
| 2246 | /* |
| 2247 | * insert request into driver dispatch list |
| 2248 | */ |
| 2249 | cfq_dispatch_insert(cfqd->queue, rq); |
| 2250 | |
| 2251 | if (!cfqd->active_cic) { |
| 2252 | struct cfq_io_context *cic = RQ_CIC(rq); |
| 2253 | |
| 2254 | atomic_long_inc(&cic->ioc->refcount); |
| 2255 | cfqd->active_cic = cic; |
| 2256 | } |
| 2257 | |
| 2258 | return true; |
| 2259 | } |
| 2260 | |
| 2261 | /* |
| 2262 | * Find the cfqq that we need to service and move a request from that to the |
| 2263 | * dispatch list |
| 2264 | */ |
| 2265 | static int cfq_dispatch_requests(struct request_queue *q, int force) |
| 2266 | { |
| 2267 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2268 | struct cfq_queue *cfqq; |
| 2269 | |
| 2270 | if (!cfqd->busy_queues) |
| 2271 | return 0; |
| 2272 | |
| 2273 | if (unlikely(force)) |
| 2274 | return cfq_forced_dispatch(cfqd); |
| 2275 | |
| 2276 | cfqq = cfq_select_queue(cfqd); |
| 2277 | if (!cfqq) |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 2278 | return 0; |
| 2279 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2280 | /* |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2281 | * Dispatch a request from this cfqq, if it is allowed |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2282 | */ |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 2283 | if (!cfq_dispatch_request(cfqd, cfqq)) |
| 2284 | return 0; |
| 2285 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2286 | cfqq->slice_dispatch++; |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 2287 | cfq_clear_cfqq_must_dispatch(cfqq); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2288 | |
| 2289 | /* |
| 2290 | * expire an async queue immediately if it has used up its slice. idle |
| 2291 | * queue always expire after 1 dispatch round. |
| 2292 | */ |
| 2293 | if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) && |
| 2294 | cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) || |
| 2295 | cfq_class_idle(cfqq))) { |
| 2296 | cfqq->slice_end = jiffies + 1; |
| 2297 | cfq_slice_expired(cfqd, 0); |
| 2298 | } |
| 2299 | |
Shan Wei | b217a90 | 2009-09-01 10:06:42 +0200 | [diff] [blame] | 2300 | cfq_log_cfqq(cfqd, cfqq, "dispatched a request"); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 2301 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2305 | * task holds one reference to the queue, dropped when task exits. each rq |
| 2306 | * 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] | 2307 | * |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 2308 | * Each cfq queue took a reference on the parent group. Drop it now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | * queue lock must be held here. |
| 2310 | */ |
| 2311 | static void cfq_put_queue(struct cfq_queue *cfqq) |
| 2312 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2313 | struct cfq_data *cfqd = cfqq->cfqd; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 2314 | struct cfq_group *cfqg; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2315 | |
| 2316 | BUG_ON(atomic_read(&cfqq->ref) <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
| 2318 | if (!atomic_dec_and_test(&cfqq->ref)) |
| 2319 | return; |
| 2320 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2321 | cfq_log_cfqq(cfqd, cfqq, "put_queue"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | BUG_ON(rb_first(&cfqq->sort_list)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2323 | BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 2324 | cfqg = cfqq->cfqg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 2326 | if (unlikely(cfqd->active_queue == cfqq)) { |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 2327 | __cfq_slice_expired(cfqd, cfqq, 0); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 2328 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 2329 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2330 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2331 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | kmem_cache_free(cfq_pool, cfqq); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 2333 | cfq_put_cfqg(cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2336 | /* |
| 2337 | * Must always be called with the rcu_read_lock() held |
| 2338 | */ |
Jens Axboe | 07416d2 | 2008-05-07 09:17:12 +0200 | [diff] [blame] | 2339 | static void |
| 2340 | __call_for_each_cic(struct io_context *ioc, |
| 2341 | void (*func)(struct io_context *, struct cfq_io_context *)) |
| 2342 | { |
| 2343 | struct cfq_io_context *cic; |
| 2344 | struct hlist_node *n; |
| 2345 | |
| 2346 | hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list) |
| 2347 | func(ioc, cic); |
| 2348 | } |
| 2349 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2350 | /* |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2351 | * Call func for each cic attached to this ioc. |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2352 | */ |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2353 | static void |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2354 | call_for_each_cic(struct io_context *ioc, |
| 2355 | void (*func)(struct io_context *, struct cfq_io_context *)) |
| 2356 | { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2357 | rcu_read_lock(); |
Jens Axboe | 07416d2 | 2008-05-07 09:17:12 +0200 | [diff] [blame] | 2358 | __call_for_each_cic(ioc, func); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2359 | rcu_read_unlock(); |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2360 | } |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2361 | |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2362 | static void cfq_cic_free_rcu(struct rcu_head *head) |
| 2363 | { |
| 2364 | struct cfq_io_context *cic; |
| 2365 | |
| 2366 | cic = container_of(head, struct cfq_io_context, rcu_head); |
| 2367 | |
| 2368 | kmem_cache_free(cfq_ioc_pool, cic); |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2369 | elv_ioc_count_dec(cfq_ioc_count); |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2370 | |
Jens Axboe | 9a11b4e | 2008-05-29 09:32:08 +0200 | [diff] [blame] | 2371 | if (ioc_gone) { |
| 2372 | /* |
| 2373 | * CFQ scheduler is exiting, grab exit lock and check |
| 2374 | * the pending io context count. If it hits zero, |
| 2375 | * complete ioc_gone and set it back to NULL |
| 2376 | */ |
| 2377 | spin_lock(&ioc_gone_lock); |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2378 | if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) { |
Jens Axboe | 9a11b4e | 2008-05-29 09:32:08 +0200 | [diff] [blame] | 2379 | complete(ioc_gone); |
| 2380 | ioc_gone = NULL; |
| 2381 | } |
| 2382 | spin_unlock(&ioc_gone_lock); |
| 2383 | } |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | static void cfq_cic_free(struct cfq_io_context *cic) |
| 2387 | { |
| 2388 | call_rcu(&cic->rcu_head, cfq_cic_free_rcu); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic) |
| 2392 | { |
| 2393 | unsigned long flags; |
| 2394 | |
| 2395 | BUG_ON(!cic->dead_key); |
| 2396 | |
| 2397 | spin_lock_irqsave(&ioc->lock, flags); |
| 2398 | radix_tree_delete(&ioc->radix_root, cic->dead_key); |
Jens Axboe | ffc4e75 | 2008-02-19 10:02:29 +0100 | [diff] [blame] | 2399 | hlist_del_rcu(&cic->cic_list); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2400 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 2401 | |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2402 | cfq_cic_free(cic); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2405 | /* |
| 2406 | * Must be called with rcu_read_lock() held or preemption otherwise disabled. |
| 2407 | * Only two callers of this - ->dtor() which is called with the rcu_read_lock(), |
| 2408 | * and ->trim() which is called with the task lock held |
| 2409 | */ |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2410 | static void cfq_free_io_context(struct io_context *ioc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2412 | /* |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 2413 | * ioc->refcount is zero here, or we are called from elv_unregister(), |
| 2414 | * so no more cic's are allowed to be linked into this ioc. So it |
| 2415 | * should be ok to iterate over the known list, we will see all cic's |
| 2416 | * since no new ones are added. |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2417 | */ |
Jens Axboe | 07416d2 | 2008-05-07 09:17:12 +0200 | [diff] [blame] | 2418 | __call_for_each_cic(ioc, cic_free_func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2421 | static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2422 | { |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2423 | struct cfq_queue *__cfqq, *next; |
| 2424 | |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 2425 | if (unlikely(cfqq == cfqd->active_queue)) { |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 2426 | __cfq_slice_expired(cfqd, cfqq, 0); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 2427 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 2428 | } |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2429 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2430 | /* |
| 2431 | * If this queue was scheduled to merge with another queue, be |
| 2432 | * sure to drop the reference taken on that queue (and others in |
| 2433 | * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs. |
| 2434 | */ |
| 2435 | __cfqq = cfqq->new_cfqq; |
| 2436 | while (__cfqq) { |
| 2437 | if (__cfqq == cfqq) { |
| 2438 | WARN(1, "cfqq->new_cfqq loop detected\n"); |
| 2439 | break; |
| 2440 | } |
| 2441 | next = __cfqq->new_cfqq; |
| 2442 | cfq_put_queue(__cfqq); |
| 2443 | __cfqq = next; |
| 2444 | } |
| 2445 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2446 | cfq_put_queue(cfqq); |
| 2447 | } |
| 2448 | |
| 2449 | static void __cfq_exit_single_io_context(struct cfq_data *cfqd, |
| 2450 | struct cfq_io_context *cic) |
| 2451 | { |
Fabio Checconi | 4faa3c8 | 2008-04-10 08:28:01 +0200 | [diff] [blame] | 2452 | struct io_context *ioc = cic->ioc; |
| 2453 | |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2454 | list_del_init(&cic->queue_list); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2455 | |
| 2456 | /* |
| 2457 | * Make sure key == NULL is seen for dead queues |
| 2458 | */ |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2459 | smp_wmb(); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2460 | cic->dead_key = (unsigned long) cic->key; |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2461 | cic->key = NULL; |
| 2462 | |
Fabio Checconi | 4faa3c8 | 2008-04-10 08:28:01 +0200 | [diff] [blame] | 2463 | if (ioc->ioc_data == cic) |
| 2464 | rcu_assign_pointer(ioc->ioc_data, NULL); |
| 2465 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 2466 | if (cic->cfqq[BLK_RW_ASYNC]) { |
| 2467 | cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]); |
| 2468 | cic->cfqq[BLK_RW_ASYNC] = NULL; |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 2471 | if (cic->cfqq[BLK_RW_SYNC]) { |
| 2472 | cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]); |
| 2473 | cic->cfqq[BLK_RW_SYNC] = NULL; |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2474 | } |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2475 | } |
| 2476 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2477 | static void cfq_exit_single_io_context(struct io_context *ioc, |
| 2478 | struct cfq_io_context *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2479 | { |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 2480 | struct cfq_data *cfqd = cic->key; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2481 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2482 | if (cfqd) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2483 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2484 | unsigned long flags; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2485 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2486 | spin_lock_irqsave(q->queue_lock, flags); |
Jens Axboe | 62c1fe9 | 2008-12-15 21:19:25 +0100 | [diff] [blame] | 2487 | |
| 2488 | /* |
| 2489 | * Ensure we get a fresh copy of the ->key to prevent |
| 2490 | * race between exiting task and queue |
| 2491 | */ |
| 2492 | smp_read_barrier_depends(); |
| 2493 | if (cic->key) |
| 2494 | __cfq_exit_single_io_context(cfqd, cic); |
| 2495 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2496 | spin_unlock_irqrestore(q->queue_lock, flags); |
Al Viro | 12a0573 | 2006-03-18 13:38:01 -0500 | [diff] [blame] | 2497 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2498 | } |
| 2499 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2500 | /* |
| 2501 | * The process that ioc belongs to has exited, we need to clean up |
| 2502 | * and put the internal structures we have that belongs to that process. |
| 2503 | */ |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2504 | static void cfq_exit_io_context(struct io_context *ioc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2506 | call_for_each_cic(ioc, cfq_exit_single_io_context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | } |
| 2508 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2509 | static struct cfq_io_context * |
Al Viro | 8267e26 | 2005-10-21 03:20:53 -0400 | [diff] [blame] | 2510 | cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | { |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 2512 | struct cfq_io_context *cic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 2514 | cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO, |
| 2515 | cfqd->queue->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | if (cic) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2517 | cic->last_end_request = jiffies; |
Jens Axboe | 553698f | 2006-06-14 19:11:57 +0200 | [diff] [blame] | 2518 | INIT_LIST_HEAD(&cic->queue_list); |
Jens Axboe | ffc4e75 | 2008-02-19 10:02:29 +0100 | [diff] [blame] | 2519 | INIT_HLIST_NODE(&cic->cic_list); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2520 | cic->dtor = cfq_free_io_context; |
| 2521 | cic->exit = cfq_exit_io_context; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 2522 | elv_ioc_count_inc(cfq_ioc_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | return cic; |
| 2526 | } |
| 2527 | |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 2528 | static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2529 | { |
| 2530 | struct task_struct *tsk = current; |
| 2531 | int ioprio_class; |
| 2532 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2533 | if (!cfq_cfqq_prio_changed(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2534 | return; |
| 2535 | |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 2536 | ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2537 | switch (ioprio_class) { |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 2538 | default: |
| 2539 | printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); |
| 2540 | case IOPRIO_CLASS_NONE: |
| 2541 | /* |
Jens Axboe | 6d63c27 | 2008-05-07 09:51:23 +0200 | [diff] [blame] | 2542 | * no prio set, inherit CPU scheduling settings |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 2543 | */ |
| 2544 | cfqq->ioprio = task_nice_ioprio(tsk); |
Jens Axboe | 6d63c27 | 2008-05-07 09:51:23 +0200 | [diff] [blame] | 2545 | cfqq->ioprio_class = task_nice_ioclass(tsk); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 2546 | break; |
| 2547 | case IOPRIO_CLASS_RT: |
| 2548 | cfqq->ioprio = task_ioprio(ioc); |
| 2549 | cfqq->ioprio_class = IOPRIO_CLASS_RT; |
| 2550 | break; |
| 2551 | case IOPRIO_CLASS_BE: |
| 2552 | cfqq->ioprio = task_ioprio(ioc); |
| 2553 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 2554 | break; |
| 2555 | case IOPRIO_CLASS_IDLE: |
| 2556 | cfqq->ioprio_class = IOPRIO_CLASS_IDLE; |
| 2557 | cfqq->ioprio = 7; |
| 2558 | cfq_clear_cfqq_idle_window(cfqq); |
| 2559 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * keep track of original prio settings in case we have to temporarily |
| 2564 | * elevate the priority of this queue |
| 2565 | */ |
| 2566 | cfqq->org_ioprio = cfqq->ioprio; |
| 2567 | cfqq->org_ioprio_class = cfqq->ioprio_class; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2568 | cfq_clear_cfqq_prio_changed(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2569 | } |
| 2570 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2571 | static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2572 | { |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 2573 | struct cfq_data *cfqd = cic->key; |
| 2574 | struct cfq_queue *cfqq; |
Jens Axboe | c1b707d | 2006-10-30 19:54:23 +0100 | [diff] [blame] | 2575 | unsigned long flags; |
Jens Axboe | 35e6077 | 2006-06-14 09:10:45 +0200 | [diff] [blame] | 2576 | |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2577 | if (unlikely(!cfqd)) |
| 2578 | return; |
| 2579 | |
Jens Axboe | c1b707d | 2006-10-30 19:54:23 +0100 | [diff] [blame] | 2580 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2581 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 2582 | cfqq = cic->cfqq[BLK_RW_ASYNC]; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2583 | if (cfqq) { |
| 2584 | struct cfq_queue *new_cfqq; |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 2585 | new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc, |
| 2586 | GFP_ATOMIC); |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2587 | if (new_cfqq) { |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 2588 | cic->cfqq[BLK_RW_ASYNC] = new_cfqq; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2589 | cfq_put_queue(cfqq); |
| 2590 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2591 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2592 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 2593 | cfqq = cic->cfqq[BLK_RW_SYNC]; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2594 | if (cfqq) |
| 2595 | cfq_mark_cfqq_prio_changed(cfqq); |
| 2596 | |
Jens Axboe | c1b707d | 2006-10-30 19:54:23 +0100 | [diff] [blame] | 2597 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2600 | static void cfq_ioc_set_ioprio(struct io_context *ioc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2602 | call_for_each_cic(ioc, changed_ioprio); |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2603 | ioc->ioprio_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | } |
| 2605 | |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 2606 | static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2607 | pid_t pid, bool is_sync) |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 2608 | { |
| 2609 | RB_CLEAR_NODE(&cfqq->rb_node); |
| 2610 | RB_CLEAR_NODE(&cfqq->p_node); |
| 2611 | INIT_LIST_HEAD(&cfqq->fifo); |
| 2612 | |
| 2613 | atomic_set(&cfqq->ref, 0); |
| 2614 | cfqq->cfqd = cfqd; |
| 2615 | |
| 2616 | cfq_mark_cfqq_prio_changed(cfqq); |
| 2617 | |
| 2618 | if (is_sync) { |
| 2619 | if (!cfq_class_idle(cfqq)) |
| 2620 | cfq_mark_cfqq_idle_window(cfqq); |
| 2621 | cfq_mark_cfqq_sync(cfqq); |
| 2622 | } |
| 2623 | cfqq->pid = pid; |
| 2624 | } |
| 2625 | |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 2626 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 2627 | static void changed_cgroup(struct io_context *ioc, struct cfq_io_context *cic) |
| 2628 | { |
| 2629 | struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1); |
| 2630 | struct cfq_data *cfqd = cic->key; |
| 2631 | unsigned long flags; |
| 2632 | struct request_queue *q; |
| 2633 | |
| 2634 | if (unlikely(!cfqd)) |
| 2635 | return; |
| 2636 | |
| 2637 | q = cfqd->queue; |
| 2638 | |
| 2639 | spin_lock_irqsave(q->queue_lock, flags); |
| 2640 | |
| 2641 | if (sync_cfqq) { |
| 2642 | /* |
| 2643 | * Drop reference to sync queue. A new sync queue will be |
| 2644 | * assigned in new group upon arrival of a fresh request. |
| 2645 | */ |
| 2646 | cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup"); |
| 2647 | cic_set_cfqq(cic, NULL, 1); |
| 2648 | cfq_put_queue(sync_cfqq); |
| 2649 | } |
| 2650 | |
| 2651 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 2652 | } |
| 2653 | |
| 2654 | static void cfq_ioc_set_cgroup(struct io_context *ioc) |
| 2655 | { |
| 2656 | call_for_each_cic(ioc, changed_cgroup); |
| 2657 | ioc->cgroup_changed = 0; |
| 2658 | } |
| 2659 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 2660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | static struct cfq_queue * |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2662 | cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 2663 | struct io_context *ioc, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2666 | struct cfq_io_context *cic; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2667 | struct cfq_group *cfqg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | |
| 2669 | retry: |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2670 | cfqg = cfq_get_cfqg(cfqd, 1); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2671 | cic = cfq_cic_lookup(cfqd, ioc); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2672 | /* cic always exists here */ |
| 2673 | cfqq = cic_to_cfqq(cic, is_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 2675 | /* |
| 2676 | * Always try a new alloc if we fell back to the OOM cfqq |
| 2677 | * originally, since it should just be a temporary situation. |
| 2678 | */ |
| 2679 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { |
| 2680 | cfqq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | if (new_cfqq) { |
| 2682 | cfqq = new_cfqq; |
| 2683 | new_cfqq = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2684 | } else if (gfp_mask & __GFP_WAIT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | spin_unlock_irq(cfqd->queue->queue_lock); |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 2686 | new_cfqq = kmem_cache_alloc_node(cfq_pool, |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 2687 | gfp_mask | __GFP_ZERO, |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 2688 | cfqd->queue->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | spin_lock_irq(cfqd->queue->queue_lock); |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 2690 | if (new_cfqq) |
| 2691 | goto retry; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2692 | } else { |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 2693 | cfqq = kmem_cache_alloc_node(cfq_pool, |
| 2694 | gfp_mask | __GFP_ZERO, |
| 2695 | cfqd->queue->node); |
Kiyoshi Ueda | db3b584 | 2005-06-17 16:15:10 +0200 | [diff] [blame] | 2696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 2698 | if (cfqq) { |
| 2699 | cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync); |
| 2700 | cfq_init_prio_data(cfqq, ioc); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2701 | cfq_link_cfqq_cfqg(cfqq, cfqg); |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 2702 | cfq_log_cfqq(cfqd, cfqq, "alloced"); |
| 2703 | } else |
| 2704 | cfqq = &cfqd->oom_cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | if (new_cfqq) |
| 2708 | kmem_cache_free(cfq_pool, new_cfqq); |
| 2709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | return cfqq; |
| 2711 | } |
| 2712 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 2713 | static struct cfq_queue ** |
| 2714 | cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio) |
| 2715 | { |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 2716 | switch (ioprio_class) { |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 2717 | case IOPRIO_CLASS_RT: |
| 2718 | return &cfqd->async_cfqq[0][ioprio]; |
| 2719 | case IOPRIO_CLASS_BE: |
| 2720 | return &cfqd->async_cfqq[1][ioprio]; |
| 2721 | case IOPRIO_CLASS_IDLE: |
| 2722 | return &cfqd->async_idle_cfqq; |
| 2723 | default: |
| 2724 | BUG(); |
| 2725 | } |
| 2726 | } |
| 2727 | |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 2728 | static struct cfq_queue * |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2729 | cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc, |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 2730 | gfp_t gfp_mask) |
| 2731 | { |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 2732 | const int ioprio = task_ioprio(ioc); |
| 2733 | const int ioprio_class = task_ioprio_class(ioc); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 2734 | struct cfq_queue **async_cfqq = NULL; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 2735 | struct cfq_queue *cfqq = NULL; |
| 2736 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 2737 | if (!is_sync) { |
| 2738 | async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio); |
| 2739 | cfqq = *async_cfqq; |
| 2740 | } |
| 2741 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 2742 | if (!cfqq) |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 2743 | cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask); |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 2744 | |
| 2745 | /* |
| 2746 | * pin the queue now that it's allocated, scheduler exit will prune it |
| 2747 | */ |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 2748 | if (!is_sync && !(*async_cfqq)) { |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 2749 | atomic_inc(&cfqq->ref); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 2750 | *async_cfqq = cfqq; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | atomic_inc(&cfqq->ref); |
| 2754 | return cfqq; |
| 2755 | } |
| 2756 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2757 | /* |
| 2758 | * We drop cfq io contexts lazily, so we may find a dead one. |
| 2759 | */ |
OGAWA Hirofumi | dbecf3a | 2006-04-18 09:45:18 +0200 | [diff] [blame] | 2760 | static void |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2761 | cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc, |
| 2762 | struct cfq_io_context *cic) |
OGAWA Hirofumi | dbecf3a | 2006-04-18 09:45:18 +0200 | [diff] [blame] | 2763 | { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2764 | unsigned long flags; |
| 2765 | |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2766 | WARN_ON(!list_empty(&cic->queue_list)); |
Jens Axboe | 597bc48 | 2007-04-24 21:23:53 +0200 | [diff] [blame] | 2767 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2768 | spin_lock_irqsave(&ioc->lock, flags); |
Jens Axboe | 597bc48 | 2007-04-24 21:23:53 +0200 | [diff] [blame] | 2769 | |
Fabio Checconi | 4faa3c8 | 2008-04-10 08:28:01 +0200 | [diff] [blame] | 2770 | BUG_ON(ioc->ioc_data == cic); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2771 | |
| 2772 | radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd); |
Jens Axboe | ffc4e75 | 2008-02-19 10:02:29 +0100 | [diff] [blame] | 2773 | hlist_del_rcu(&cic->cic_list); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2774 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 2775 | |
| 2776 | cfq_cic_free(cic); |
OGAWA Hirofumi | dbecf3a | 2006-04-18 09:45:18 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2779 | static struct cfq_io_context * |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2780 | cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc) |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2781 | { |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2782 | struct cfq_io_context *cic; |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2783 | unsigned long flags; |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2784 | void *k; |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2785 | |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2786 | if (unlikely(!ioc)) |
| 2787 | return NULL; |
| 2788 | |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2789 | rcu_read_lock(); |
| 2790 | |
Jens Axboe | 597bc48 | 2007-04-24 21:23:53 +0200 | [diff] [blame] | 2791 | /* |
| 2792 | * we maintain a last-hit cache, to avoid browsing over the tree |
| 2793 | */ |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2794 | cic = rcu_dereference(ioc->ioc_data); |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2795 | if (cic && cic->key == cfqd) { |
| 2796 | rcu_read_unlock(); |
Jens Axboe | 597bc48 | 2007-04-24 21:23:53 +0200 | [diff] [blame] | 2797 | return cic; |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2798 | } |
Jens Axboe | 597bc48 | 2007-04-24 21:23:53 +0200 | [diff] [blame] | 2799 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2800 | do { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2801 | cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd); |
| 2802 | rcu_read_unlock(); |
| 2803 | if (!cic) |
| 2804 | break; |
OGAWA Hirofumi | be3b075 | 2006-04-18 19:18:31 +0200 | [diff] [blame] | 2805 | /* ->key must be copied to avoid race with cfq_exit_queue() */ |
| 2806 | k = cic->key; |
| 2807 | if (unlikely(!k)) { |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2808 | cfq_drop_dead_cic(cfqd, ioc, cic); |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2809 | rcu_read_lock(); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2810 | continue; |
OGAWA Hirofumi | dbecf3a | 2006-04-18 09:45:18 +0200 | [diff] [blame] | 2811 | } |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2812 | |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2813 | spin_lock_irqsave(&ioc->lock, flags); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2814 | rcu_assign_pointer(ioc->ioc_data, cic); |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 2815 | spin_unlock_irqrestore(&ioc->lock, flags); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2816 | break; |
| 2817 | } while (1); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2818 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2819 | return cic; |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2820 | } |
| 2821 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2822 | /* |
| 2823 | * Add cic into ioc, using cfqd as the search key. This enables us to lookup |
| 2824 | * the process specific cfq io context when entered from the block layer. |
| 2825 | * Also adds the cic to a per-cfqd list, used when this queue is removed. |
| 2826 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2827 | static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc, |
| 2828 | struct cfq_io_context *cic, gfp_t gfp_mask) |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2829 | { |
Jens Axboe | 0261d68 | 2006-10-30 19:07:48 +0100 | [diff] [blame] | 2830 | unsigned long flags; |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2831 | int ret; |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2832 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2833 | ret = radix_tree_preload(gfp_mask); |
| 2834 | if (!ret) { |
| 2835 | cic->ioc = ioc; |
| 2836 | cic->key = cfqd; |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2837 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2838 | spin_lock_irqsave(&ioc->lock, flags); |
| 2839 | ret = radix_tree_insert(&ioc->radix_root, |
| 2840 | (unsigned long) cfqd, cic); |
Jens Axboe | ffc4e75 | 2008-02-19 10:02:29 +0100 | [diff] [blame] | 2841 | if (!ret) |
| 2842 | hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list); |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2843 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 2844 | |
| 2845 | radix_tree_preload_end(); |
| 2846 | |
| 2847 | if (!ret) { |
| 2848 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 2849 | list_add(&cic->queue_list, &cfqd->cic_list); |
| 2850 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
OGAWA Hirofumi | dbecf3a | 2006-04-18 09:45:18 +0200 | [diff] [blame] | 2851 | } |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2854 | if (ret) |
| 2855 | printk(KERN_ERR "cfq: cic link failed!\n"); |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2856 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2857 | return ret; |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2860 | /* |
| 2861 | * Setup general io context and cfq io context. There can be several cfq |
| 2862 | * 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] | 2863 | * than one device managed by cfq. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2864 | */ |
| 2865 | static struct cfq_io_context * |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2866 | cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2868 | struct io_context *ioc = NULL; |
| 2869 | struct cfq_io_context *cic; |
| 2870 | |
| 2871 | might_sleep_if(gfp_mask & __GFP_WAIT); |
| 2872 | |
Jens Axboe | b5deef9 | 2006-07-19 23:39:40 +0200 | [diff] [blame] | 2873 | ioc = get_io_context(gfp_mask, cfqd->queue->node); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2874 | if (!ioc) |
| 2875 | return NULL; |
| 2876 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2877 | cic = cfq_cic_lookup(cfqd, ioc); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2878 | if (cic) |
| 2879 | goto out; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2880 | |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 2881 | cic = cfq_alloc_io_context(cfqd, gfp_mask); |
| 2882 | if (cic == NULL) |
| 2883 | goto err; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2884 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2885 | if (cfq_cic_link(cfqd, ioc, cic, gfp_mask)) |
| 2886 | goto err_free; |
| 2887 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2888 | out: |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 2889 | smp_read_barrier_depends(); |
| 2890 | if (unlikely(ioc->ioprio_changed)) |
| 2891 | cfq_ioc_set_ioprio(ioc); |
| 2892 | |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 2893 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 2894 | if (unlikely(ioc->cgroup_changed)) |
| 2895 | cfq_ioc_set_cgroup(ioc); |
| 2896 | #endif |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2897 | return cic; |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2898 | err_free: |
| 2899 | cfq_cic_free(cic); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2900 | err: |
| 2901 | put_io_context(ioc); |
| 2902 | return NULL; |
| 2903 | } |
| 2904 | |
| 2905 | static void |
| 2906 | cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic) |
| 2907 | { |
Jens Axboe | aaf1228 | 2007-01-19 11:30:16 +1100 | [diff] [blame] | 2908 | unsigned long elapsed = jiffies - cic->last_end_request; |
| 2909 | unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2910 | |
| 2911 | cic->ttime_samples = (7*cic->ttime_samples + 256) / 8; |
| 2912 | cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8; |
| 2913 | cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples; |
| 2914 | } |
| 2915 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2916 | static void |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2917 | cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2918 | struct request *rq) |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2919 | { |
| 2920 | sector_t sdist; |
| 2921 | u64 total; |
| 2922 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2923 | if (!cfqq->last_request_pos) |
Jeff Moyer | 4d00aa4 | 2009-04-21 07:25:04 +0200 | [diff] [blame] | 2924 | sdist = 0; |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2925 | else if (cfqq->last_request_pos < blk_rq_pos(rq)) |
| 2926 | sdist = blk_rq_pos(rq) - cfqq->last_request_pos; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2927 | else |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2928 | sdist = cfqq->last_request_pos - blk_rq_pos(rq); |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2929 | |
| 2930 | /* |
| 2931 | * Don't allow the seek distance to get too large from the |
| 2932 | * odd fragment, pagein, etc |
| 2933 | */ |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2934 | if (cfqq->seek_samples <= 60) /* second&third seek */ |
| 2935 | sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024); |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2936 | else |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2937 | sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64); |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2938 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2939 | cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8; |
| 2940 | cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8; |
| 2941 | total = cfqq->seek_total + (cfqq->seek_samples/2); |
| 2942 | do_div(total, cfqq->seek_samples); |
| 2943 | cfqq->seek_mean = (sector_t)total; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2944 | |
| 2945 | /* |
| 2946 | * If this cfqq is shared between multiple processes, check to |
| 2947 | * make sure that those processes are still issuing I/Os within |
| 2948 | * the mean seek distance. If not, it may be time to break the |
| 2949 | * queues apart again. |
| 2950 | */ |
| 2951 | if (cfq_cfqq_coop(cfqq)) { |
| 2952 | if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start) |
| 2953 | cfqq->seeky_start = jiffies; |
| 2954 | else if (!CFQQ_SEEKY(cfqq)) |
| 2955 | cfqq->seeky_start = 0; |
| 2956 | } |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2957 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2958 | |
| 2959 | /* |
| 2960 | * Disable idle window if the process thinks too long or seeks so much that |
| 2961 | * it doesn't matter |
| 2962 | */ |
| 2963 | static void |
| 2964 | cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 2965 | struct cfq_io_context *cic) |
| 2966 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2967 | int old_idle, enable_idle; |
Jens Axboe | 1be92f2f | 2007-04-19 14:32:26 +0200 | [diff] [blame] | 2968 | |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2969 | /* |
| 2970 | * Don't idle for async or idle io prio class |
| 2971 | */ |
| 2972 | if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq)) |
Jens Axboe | 1be92f2f | 2007-04-19 14:32:26 +0200 | [diff] [blame] | 2973 | return; |
| 2974 | |
Jens Axboe | c265a7f | 2008-06-26 13:49:33 +0200 | [diff] [blame] | 2975 | enable_idle = old_idle = cfq_cfqq_idle_window(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2976 | |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2977 | if (cfqq->queued[0] + cfqq->queued[1] >= 4) |
| 2978 | cfq_mark_cfqq_deep(cfqq); |
| 2979 | |
Nikanth Karthikesan | 66dac98 | 2007-11-27 12:47:04 +0100 | [diff] [blame] | 2980 | if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle || |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2981 | (!cfq_cfqq_deep(cfqq) && sample_valid(cfqq->seek_samples) |
| 2982 | && CFQQ_SEEKY(cfqq))) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2983 | enable_idle = 0; |
| 2984 | else if (sample_valid(cic->ttime_samples)) { |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2985 | if (cic->ttime_mean > cfqd->cfq_slice_idle) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2986 | enable_idle = 0; |
| 2987 | else |
| 2988 | enable_idle = 1; |
| 2989 | } |
| 2990 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2991 | if (old_idle != enable_idle) { |
| 2992 | cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle); |
| 2993 | if (enable_idle) |
| 2994 | cfq_mark_cfqq_idle_window(cfqq); |
| 2995 | else |
| 2996 | cfq_clear_cfqq_idle_window(cfqq); |
| 2997 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2998 | } |
| 2999 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3000 | /* |
| 3001 | * Check if new_cfqq should preempt the currently active queue. Return 0 for |
| 3002 | * no or if we aren't sure, a 1 will cause a preempt. |
| 3003 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3004 | static bool |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3005 | cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3006 | struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3007 | { |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3008 | struct cfq_queue *cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3009 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3010 | cfqq = cfqd->active_queue; |
| 3011 | if (!cfqq) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3012 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3013 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3014 | if (cfq_class_idle(new_cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3015 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3016 | |
| 3017 | if (cfq_class_idle(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3018 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3019 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3020 | /* |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 3021 | * if the new request is sync, but the currently running queue is |
| 3022 | * not, let the sync request have priority. |
| 3023 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3024 | if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3025 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3026 | |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 3027 | if (new_cfqq->cfqg != cfqq->cfqg) |
| 3028 | return false; |
| 3029 | |
| 3030 | if (cfq_slice_used(cfqq)) |
| 3031 | return true; |
| 3032 | |
| 3033 | /* Allow preemption only if we are idling on sync-noidle tree */ |
| 3034 | if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD && |
| 3035 | cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD && |
| 3036 | new_cfqq->service_tree->count == 2 && |
| 3037 | RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 3038 | return true; |
| 3039 | |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 3040 | /* |
| 3041 | * So both queues are sync. Let the new request get disk time if |
| 3042 | * it's a metadata request and the current queue is doing regular IO. |
| 3043 | */ |
| 3044 | if (rq_is_meta(rq) && !cfqq->meta_pending) |
Jens Axboe | e6ec4fe | 2009-11-03 20:21:35 +0100 | [diff] [blame] | 3045 | return true; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3046 | |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 3047 | /* |
| 3048 | * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice. |
| 3049 | */ |
| 3050 | if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3051 | return true; |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 3052 | |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3053 | if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3054 | return false; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3055 | |
| 3056 | /* |
| 3057 | * if this request is as-good as one we would expect from the |
| 3058 | * current cfqq, let it preempt |
| 3059 | */ |
Jens Axboe | e00ef79 | 2009-11-04 08:54:55 +0100 | [diff] [blame] | 3060 | if (cfq_rq_close(cfqd, cfqq, rq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3061 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3062 | |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3063 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | /* |
| 3067 | * cfqq preempts the active queue. if we allowed preempt with no slice left, |
| 3068 | * let it have half of its nominal slice. |
| 3069 | */ |
| 3070 | static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3071 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3072 | cfq_log_cfqq(cfqd, cfqq, "preempt"); |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 3073 | cfq_slice_expired(cfqd, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3074 | |
Jens Axboe | bf57225 | 2006-07-19 20:29:12 +0200 | [diff] [blame] | 3075 | /* |
| 3076 | * Put the new queue at the front of the of the current list, |
| 3077 | * so we know that it will be selected next. |
| 3078 | */ |
| 3079 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 3080 | |
| 3081 | cfq_service_tree_add(cfqd, cfqq, 1); |
Jens Axboe | bf57225 | 2006-07-19 20:29:12 +0200 | [diff] [blame] | 3082 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 3083 | cfqq->slice_end = 0; |
| 3084 | cfq_mark_cfqq_slice_new(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3088 | * 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] | 3089 | * something we should do about it |
| 3090 | */ |
| 3091 | static void |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3092 | cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 3093 | struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3094 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3095 | struct cfq_io_context *cic = RQ_CIC(rq); |
Jens Axboe | 12e9fdd | 2006-06-01 10:09:56 +0200 | [diff] [blame] | 3096 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3097 | cfqd->rq_queued++; |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 3098 | if (rq_is_meta(rq)) |
| 3099 | cfqq->meta_pending++; |
| 3100 | |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 3101 | cfq_update_io_thinktime(cfqd, cic); |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 3102 | cfq_update_io_seektime(cfqd, cfqq, rq); |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 3103 | cfq_update_idle_window(cfqd, cfqq, cic); |
| 3104 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 3105 | cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3106 | |
| 3107 | if (cfqq == cfqd->active_queue) { |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 3108 | if (cfq_cfqq_wait_busy(cfqq)) { |
| 3109 | cfq_clear_cfqq_wait_busy(cfqq); |
| 3110 | cfq_mark_cfqq_wait_busy_done(cfqq); |
| 3111 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3112 | /* |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 3113 | * Remember that we saw a request from this process, but |
| 3114 | * don't start queuing just yet. Otherwise we risk seeing lots |
| 3115 | * of tiny requests, because we disrupt the normal plugging |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3116 | * and merging. If the request is already larger than a single |
| 3117 | * page, let it rip immediately. For that case we assume that |
Jens Axboe | 2d87072 | 2009-04-15 12:12:46 +0200 | [diff] [blame] | 3118 | * merging is already done. Ditto for a busy system that |
| 3119 | * has other work pending, don't risk delaying until the |
| 3120 | * idle timer unplug to continue working. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3121 | */ |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3122 | if (cfq_cfqq_wait_request(cfqq)) { |
Jens Axboe | 2d87072 | 2009-04-15 12:12:46 +0200 | [diff] [blame] | 3123 | if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE || |
| 3124 | cfqd->busy_queues > 1) { |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3125 | del_timer(&cfqd->idle_slice_timer); |
Vivek Goyal | bf791937 | 2009-12-03 12:59:37 -0500 | [diff] [blame] | 3126 | __blk_run_queue(cfqd->queue); |
| 3127 | } else |
| 3128 | cfq_mark_cfqq_must_dispatch(cfqq); |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3129 | } |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3130 | } else if (cfq_should_preempt(cfqd, cfqq, rq)) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3131 | /* |
| 3132 | * not the active queue - expire current slice if it is |
| 3133 | * idle and has expired it's mean thinktime or this new queue |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 3134 | * has some old slice time left and is of higher priority or |
| 3135 | * this new queue is RT and the current one is BE |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3136 | */ |
| 3137 | cfq_preempt_queue(cfqd, cfqq); |
Tejun Heo | a7f5579 | 2009-04-23 11:05:17 +0900 | [diff] [blame] | 3138 | __blk_run_queue(cfqd->queue); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3142 | static void cfq_insert_request(struct request_queue *q, struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3143 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3144 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3145 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3146 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3147 | cfq_log_cfqq(cfqd, cfqq, "insert_request"); |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 3148 | cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 3150 | rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3151 | list_add_tail(&rq->queuelist, &cfqq->fifo); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 3152 | cfq_add_rq_rb(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3153 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3154 | cfq_rq_enqueued(cfqd, cfqq, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3157 | /* |
| 3158 | * Update hw_tag based on peak queue depth over 50 samples under |
| 3159 | * sufficient load. |
| 3160 | */ |
| 3161 | static void cfq_update_hw_tag(struct cfq_data *cfqd) |
| 3162 | { |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 3163 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 3164 | |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 3165 | if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth) |
| 3166 | cfqd->hw_tag_est_depth = rq_in_driver(cfqd); |
| 3167 | |
| 3168 | if (cfqd->hw_tag == 1) |
| 3169 | return; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3170 | |
| 3171 | if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN && |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3172 | rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3173 | return; |
| 3174 | |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 3175 | /* |
| 3176 | * If active queue hasn't enough requests and can idle, cfq might not |
| 3177 | * dispatch sufficient requests to hardware. Don't zero hw_tag in this |
| 3178 | * case |
| 3179 | */ |
| 3180 | if (cfqq && cfq_cfqq_idle_window(cfqq) && |
| 3181 | cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] < |
| 3182 | CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN) |
| 3183 | return; |
| 3184 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3185 | if (cfqd->hw_tag_samples++ < 50) |
| 3186 | return; |
| 3187 | |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 3188 | if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3189 | cfqd->hw_tag = 1; |
| 3190 | else |
| 3191 | cfqd->hw_tag = 0; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3192 | } |
| 3193 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3194 | static void cfq_completed_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3196 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3197 | struct cfq_data *cfqd = cfqq->cfqd; |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 3198 | const int sync = rq_is_sync(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3199 | unsigned long now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3201 | now = jiffies; |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 3202 | cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", !!rq_noidle(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3204 | cfq_update_hw_tag(cfqd); |
| 3205 | |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3206 | WARN_ON(!cfqd->rq_in_driver[sync]); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3207 | WARN_ON(!cfqq->dispatched); |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3208 | cfqd->rq_in_driver[sync]--; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3209 | cfqq->dispatched--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3211 | if (cfq_cfqq_sync(cfqq)) |
| 3212 | cfqd->sync_flight--; |
| 3213 | |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3214 | if (sync) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3215 | RQ_CIC(rq)->last_end_request = now; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3216 | cfqd->last_end_sync_rq = now; |
| 3217 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3218 | |
| 3219 | /* |
| 3220 | * If this is the active queue, check if it needs to be expired, |
| 3221 | * or if we want to idle in case it has no pending requests. |
| 3222 | */ |
| 3223 | if (cfqd->active_queue == cfqq) { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3224 | const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list); |
| 3225 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 3226 | if (cfq_cfqq_slice_new(cfqq)) { |
| 3227 | cfq_set_prio_slice(cfqd, cfqq); |
| 3228 | cfq_clear_cfqq_slice_new(cfqq); |
| 3229 | } |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame^] | 3230 | |
| 3231 | /* |
| 3232 | * If this queue consumed its slice and this is last queue |
| 3233 | * in the group, wait for next request before we expire |
| 3234 | * the queue |
| 3235 | */ |
| 3236 | if (cfq_slice_used(cfqq) && cfqq->cfqg->nr_cfqq == 1) { |
| 3237 | cfqq->slice_end = jiffies + cfqd->cfq_slice_idle; |
| 3238 | cfq_mark_cfqq_wait_busy(cfqq); |
| 3239 | } |
| 3240 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3241 | /* |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3242 | * Idling is not enabled on: |
| 3243 | * - expired queues |
| 3244 | * - idle-priority queues |
| 3245 | * - async queues |
| 3246 | * - queues with still some requests queued |
| 3247 | * - when there is a close cooperator |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3248 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 3249 | if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq)) |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 3250 | cfq_slice_expired(cfqd, 1); |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3251 | else if (sync && cfqq_empty && |
| 3252 | !cfq_close_cooperator(cfqd, cfqq)) { |
| 3253 | cfqd->noidle_tree_requires_idle |= !rq_noidle(rq); |
| 3254 | /* |
| 3255 | * Idling is enabled for SYNC_WORKLOAD. |
| 3256 | * SYNC_NOIDLE_WORKLOAD idles at the end of the tree |
| 3257 | * only if we processed at least one !rq_noidle request |
| 3258 | */ |
| 3259 | if (cfqd->serving_type == SYNC_WORKLOAD |
| 3260 | || cfqd->noidle_tree_requires_idle) |
| 3261 | cfq_arm_slice_timer(cfqd); |
| 3262 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3263 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3264 | |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3265 | if (!rq_in_driver(cfqd)) |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3266 | cfq_schedule_dispatch(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | } |
| 3268 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3269 | /* |
| 3270 | * we temporarily boost lower priority queues if they are holding fs exclusive |
| 3271 | * resources. they are boosted to normal prio (CLASS_BE/4) |
| 3272 | */ |
| 3273 | static void cfq_prio_boost(struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3275 | if (has_fs_excl()) { |
| 3276 | /* |
| 3277 | * boost idle prio on transactions that would lock out other |
| 3278 | * users of the filesystem |
| 3279 | */ |
| 3280 | if (cfq_class_idle(cfqq)) |
| 3281 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 3282 | if (cfqq->ioprio > IOPRIO_NORM) |
| 3283 | cfqq->ioprio = IOPRIO_NORM; |
| 3284 | } else { |
| 3285 | /* |
Corrado Zoccolo | dddb745 | 2009-11-02 10:40:37 +0100 | [diff] [blame] | 3286 | * unboost the queue (if needed) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3287 | */ |
Corrado Zoccolo | dddb745 | 2009-11-02 10:40:37 +0100 | [diff] [blame] | 3288 | cfqq->ioprio_class = cfqq->org_ioprio_class; |
| 3289 | cfqq->ioprio = cfqq->org_ioprio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3291 | } |
| 3292 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3293 | static inline int __cfq_may_queue(struct cfq_queue *cfqq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3294 | { |
Jens Axboe | 1b379d8 | 2009-08-11 08:26:11 +0200 | [diff] [blame] | 3295 | if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3296 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3297 | return ELV_MQUEUE_MUST; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3298 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3299 | |
| 3300 | return ELV_MQUEUE_MAY; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3301 | } |
| 3302 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3303 | static int cfq_may_queue(struct request_queue *q, int rw) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3304 | { |
| 3305 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 3306 | struct task_struct *tsk = current; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3307 | struct cfq_io_context *cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3308 | struct cfq_queue *cfqq; |
| 3309 | |
| 3310 | /* |
| 3311 | * don't force setup of a queue from here, as a call to may_queue |
| 3312 | * does not necessarily imply that a request actually will be queued. |
| 3313 | * so just lookup a possibly existing queue, or return 'may queue' |
| 3314 | * if that fails |
| 3315 | */ |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 3316 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3317 | if (!cic) |
| 3318 | return ELV_MQUEUE_MAY; |
| 3319 | |
Jens Axboe | b0b78f8 | 2009-04-08 10:56:08 +0200 | [diff] [blame] | 3320 | cfqq = cic_to_cfqq(cic, rw_is_sync(rw)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3321 | if (cfqq) { |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 3322 | cfq_init_prio_data(cfqq, cic->ioc); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3323 | cfq_prio_boost(cfqq); |
| 3324 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3325 | return __cfq_may_queue(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | return ELV_MQUEUE_MAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | /* |
| 3332 | * queue lock held here |
| 3333 | */ |
Jens Axboe | bb37b94 | 2006-12-01 10:42:33 +0100 | [diff] [blame] | 3334 | static void cfq_put_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3336 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3338 | if (cfqq) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3339 | const int rw = rq_data_dir(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3341 | BUG_ON(!cfqq->allocated[rw]); |
| 3342 | cfqq->allocated[rw]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3344 | put_io_context(RQ_CIC(rq)->ioc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | rq->elevator_private = NULL; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3347 | rq->elevator_private2 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | cfq_put_queue(cfqq); |
| 3350 | } |
| 3351 | } |
| 3352 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3353 | static struct cfq_queue * |
| 3354 | cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic, |
| 3355 | struct cfq_queue *cfqq) |
| 3356 | { |
| 3357 | cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq); |
| 3358 | cic_set_cfqq(cic, cfqq->new_cfqq, 1); |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 3359 | cfq_mark_cfqq_coop(cfqq->new_cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3360 | cfq_put_queue(cfqq); |
| 3361 | return cic_to_cfqq(cic, 1); |
| 3362 | } |
| 3363 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3364 | static int should_split_cfqq(struct cfq_queue *cfqq) |
| 3365 | { |
| 3366 | if (cfqq->seeky_start && |
| 3367 | time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT)) |
| 3368 | return 1; |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | /* |
| 3373 | * Returns NULL if a new cfqq should be allocated, or the old cfqq if this |
| 3374 | * was the last process referring to said cfqq. |
| 3375 | */ |
| 3376 | static struct cfq_queue * |
| 3377 | split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq) |
| 3378 | { |
| 3379 | if (cfqq_process_refs(cfqq) == 1) { |
| 3380 | cfqq->seeky_start = 0; |
| 3381 | cfqq->pid = current->pid; |
| 3382 | cfq_clear_cfqq_coop(cfqq); |
| 3383 | return cfqq; |
| 3384 | } |
| 3385 | |
| 3386 | cic_set_cfqq(cic, NULL, 1); |
| 3387 | cfq_put_queue(cfqq); |
| 3388 | return NULL; |
| 3389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3391 | * Allocate cfq data structures associated with this request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3393 | static int |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3394 | cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | { |
| 3396 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 3397 | struct cfq_io_context *cic; |
| 3398 | const int rw = rq_data_dir(rq); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3399 | const bool is_sync = rq_is_sync(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3400 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | unsigned long flags; |
| 3402 | |
| 3403 | might_sleep_if(gfp_mask & __GFP_WAIT); |
| 3404 | |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 3405 | cic = cfq_get_io_context(cfqd, gfp_mask); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | spin_lock_irqsave(q->queue_lock, flags); |
| 3408 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3409 | if (!cic) |
| 3410 | goto queue_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3412 | new_queue: |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3413 | cfqq = cic_to_cfqq(cic, is_sync); |
Vivek Goyal | 32f2e80 | 2009-07-09 22:13:16 +0200 | [diff] [blame] | 3414 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 3415 | cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3416 | cic_set_cfqq(cic, cfqq, is_sync); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3417 | } else { |
| 3418 | /* |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3419 | * If the queue was seeky for too long, break it apart. |
| 3420 | */ |
| 3421 | if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) { |
| 3422 | cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq"); |
| 3423 | cfqq = split_cfqq(cic, cfqq); |
| 3424 | if (!cfqq) |
| 3425 | goto new_queue; |
| 3426 | } |
| 3427 | |
| 3428 | /* |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3429 | * Check to see if this queue is scheduled to merge with |
| 3430 | * another, closely cooperating queue. The merging of |
| 3431 | * queues happens here as it must be done in process context. |
| 3432 | * The reference on new_cfqq was taken in merge_cfqqs. |
| 3433 | */ |
| 3434 | if (cfqq->new_cfqq) |
| 3435 | cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | |
| 3438 | cfqq->allocated[rw]++; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3439 | atomic_inc(&cfqq->ref); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 3442 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3443 | rq->elevator_private = cic; |
| 3444 | rq->elevator_private2 = cfqq; |
| 3445 | return 0; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3446 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3447 | queue_fail: |
| 3448 | if (cic) |
| 3449 | put_io_context(cic->ioc); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3450 | |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3451 | cfq_schedule_dispatch(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | spin_unlock_irqrestore(q->queue_lock, flags); |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3453 | cfq_log(cfqd, "set_request fail"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | return 1; |
| 3455 | } |
| 3456 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3457 | static void cfq_kick_queue(struct work_struct *work) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3458 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3459 | struct cfq_data *cfqd = |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3460 | container_of(work, struct cfq_data, unplug_work); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3461 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3462 | |
Jens Axboe | 40bb54d | 2009-04-15 12:11:10 +0200 | [diff] [blame] | 3463 | spin_lock_irq(q->queue_lock); |
Tejun Heo | a7f5579 | 2009-04-23 11:05:17 +0900 | [diff] [blame] | 3464 | __blk_run_queue(cfqd->queue); |
Jens Axboe | 40bb54d | 2009-04-15 12:11:10 +0200 | [diff] [blame] | 3465 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | /* |
| 3469 | * Timer running if the active_queue is currently idling inside its time slice |
| 3470 | */ |
| 3471 | static void cfq_idle_slice_timer(unsigned long data) |
| 3472 | { |
| 3473 | struct cfq_data *cfqd = (struct cfq_data *) data; |
| 3474 | struct cfq_queue *cfqq; |
| 3475 | unsigned long flags; |
Jens Axboe | 3c6bd2f | 2007-01-19 12:06:33 +1100 | [diff] [blame] | 3476 | int timed_out = 1; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3477 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3478 | cfq_log(cfqd, "idle timer fired"); |
| 3479 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3480 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 3481 | |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3482 | cfqq = cfqd->active_queue; |
| 3483 | if (cfqq) { |
Jens Axboe | 3c6bd2f | 2007-01-19 12:06:33 +1100 | [diff] [blame] | 3484 | timed_out = 0; |
| 3485 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3486 | /* |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 3487 | * We saw a request before the queue expired, let it through |
| 3488 | */ |
| 3489 | if (cfq_cfqq_must_dispatch(cfqq)) |
| 3490 | goto out_kick; |
| 3491 | |
| 3492 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3493 | * expired |
| 3494 | */ |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 3495 | if (cfq_slice_used(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3496 | goto expire; |
| 3497 | |
| 3498 | /* |
| 3499 | * only expire and reinvoke request handler, if there are |
| 3500 | * other queues with pending requests |
| 3501 | */ |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3502 | if (!cfqd->busy_queues) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3503 | goto out_cont; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3504 | |
| 3505 | /* |
| 3506 | * not expired and it has a request pending, let it dispatch |
| 3507 | */ |
Jens Axboe | 75e5098 | 2009-04-07 08:56:14 +0200 | [diff] [blame] | 3508 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3509 | goto out_kick; |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3510 | |
| 3511 | /* |
| 3512 | * Queue depth flag is reset only when the idle didn't succeed |
| 3513 | */ |
| 3514 | cfq_clear_cfqq_deep(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3515 | } |
| 3516 | expire: |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 3517 | cfq_slice_expired(cfqd, timed_out); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3518 | out_kick: |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3519 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3520 | out_cont: |
| 3521 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
| 3522 | } |
| 3523 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3524 | static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) |
| 3525 | { |
| 3526 | del_timer_sync(&cfqd->idle_slice_timer); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3527 | cancel_work_sync(&cfqd->unplug_work); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3528 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3529 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3530 | static void cfq_put_async_queues(struct cfq_data *cfqd) |
| 3531 | { |
| 3532 | int i; |
| 3533 | |
| 3534 | for (i = 0; i < IOPRIO_BE_NR; i++) { |
| 3535 | if (cfqd->async_cfqq[0][i]) |
| 3536 | cfq_put_queue(cfqd->async_cfqq[0][i]); |
| 3537 | if (cfqd->async_cfqq[1][i]) |
| 3538 | cfq_put_queue(cfqd->async_cfqq[1][i]); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3539 | } |
Oleg Nesterov | 2389d1e | 2007-11-05 08:58:05 +0100 | [diff] [blame] | 3540 | |
| 3541 | if (cfqd->async_idle_cfqq) |
| 3542 | cfq_put_queue(cfqd->async_idle_cfqq); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3543 | } |
| 3544 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 3545 | static void cfq_exit_queue(struct elevator_queue *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3547 | struct cfq_data *cfqd = e->elevator_data; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3548 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3549 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3550 | cfq_shutdown_timer_wq(cfqd); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 3551 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 3552 | spin_lock_irq(q->queue_lock); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 3553 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 3554 | if (cfqd->active_queue) |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 3555 | __cfq_slice_expired(cfqd, cfqd->active_queue, 0); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 3556 | |
| 3557 | while (!list_empty(&cfqd->cic_list)) { |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 3558 | struct cfq_io_context *cic = list_entry(cfqd->cic_list.next, |
| 3559 | struct cfq_io_context, |
| 3560 | queue_list); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3561 | |
| 3562 | __cfq_exit_single_io_context(cfqd, cic); |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 3563 | } |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 3564 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3565 | cfq_put_async_queues(cfqd); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3566 | cfq_release_cfq_groups(cfqd); |
| 3567 | blkiocg_del_blkio_group(&cfqd->root_group.blkg); |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3568 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 3569 | spin_unlock_irq(q->queue_lock); |
Al Viro | a90d742 | 2006-03-18 12:05:37 -0500 | [diff] [blame] | 3570 | |
| 3571 | cfq_shutdown_timer_wq(cfqd); |
| 3572 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3573 | /* Wait for cfqg->blkg->key accessors to exit their grace periods. */ |
| 3574 | synchronize_rcu(); |
Al Viro | a90d742 | 2006-03-18 12:05:37 -0500 | [diff] [blame] | 3575 | kfree(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3578 | static void *cfq_init_queue(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | { |
| 3580 | struct cfq_data *cfqd; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3581 | int i, j; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3582 | struct cfq_group *cfqg; |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 3583 | struct cfq_rb_root *st; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 3585 | cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | if (!cfqd) |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 3587 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3589 | /* Init root service tree */ |
| 3590 | cfqd->grp_service_tree = CFQ_RB_ROOT; |
| 3591 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3592 | /* Init root group */ |
| 3593 | cfqg = &cfqd->root_group; |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 3594 | for_each_cfqg_st(cfqg, i, j, st) |
| 3595 | *st = CFQ_RB_ROOT; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3596 | RB_CLEAR_NODE(&cfqg->rb_node); |
Jens Axboe | 26a2ac0 | 2009-04-23 12:13:27 +0200 | [diff] [blame] | 3597 | |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 3598 | /* Give preference to root group over other groups */ |
| 3599 | cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT; |
| 3600 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 3601 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3602 | /* |
| 3603 | * Take a reference to root group which we never drop. This is just |
| 3604 | * to make sure that cfq_put_cfqg() does not try to kfree root group |
| 3605 | */ |
| 3606 | atomic_set(&cfqg->ref, 1); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 3607 | blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd, |
| 3608 | 0); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 3609 | #endif |
Jens Axboe | 26a2ac0 | 2009-04-23 12:13:27 +0200 | [diff] [blame] | 3610 | /* |
| 3611 | * Not strictly needed (since RB_ROOT just clears the node and we |
| 3612 | * zeroed cfqd on alloc), but better be safe in case someone decides |
| 3613 | * to add magic to the rb code |
| 3614 | */ |
| 3615 | for (i = 0; i < CFQ_PRIO_LISTS; i++) |
| 3616 | cfqd->prio_trees[i] = RB_ROOT; |
| 3617 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3618 | /* |
| 3619 | * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues. |
| 3620 | * Grab a permanent reference to it, so that the normal code flow |
| 3621 | * will not attempt to free it. |
| 3622 | */ |
| 3623 | cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0); |
| 3624 | atomic_inc(&cfqd->oom_cfqq.ref); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3625 | cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group); |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3626 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 3627 | INIT_LIST_HEAD(&cfqd->cic_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | cfqd->queue = q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3631 | init_timer(&cfqd->idle_slice_timer); |
| 3632 | cfqd->idle_slice_timer.function = cfq_idle_slice_timer; |
| 3633 | cfqd->idle_slice_timer.data = (unsigned long) cfqd; |
| 3634 | |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3635 | INIT_WORK(&cfqd->unplug_work, cfq_kick_queue); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | cfqd->cfq_quantum = cfq_quantum; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3638 | cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; |
| 3639 | cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | cfqd->cfq_back_max = cfq_back_max; |
| 3641 | cfqd->cfq_back_penalty = cfq_back_penalty; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3642 | cfqd->cfq_slice[0] = cfq_slice_async; |
| 3643 | cfqd->cfq_slice[1] = cfq_slice_sync; |
| 3644 | cfqd->cfq_slice_async_rq = cfq_slice_async_rq; |
| 3645 | cfqd->cfq_slice_idle = cfq_slice_idle; |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 3646 | cfqd->cfq_latency = 1; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 3647 | cfqd->hw_tag = -1; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3648 | cfqd->last_end_sync_rq = jiffies; |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 3649 | return cfqd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | static void cfq_slab_kill(void) |
| 3653 | { |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 3654 | /* |
| 3655 | * Caller already ensured that pending RCU callbacks are completed, |
| 3656 | * so we should have no busy allocations at this point. |
| 3657 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | if (cfq_pool) |
| 3659 | kmem_cache_destroy(cfq_pool); |
| 3660 | if (cfq_ioc_pool) |
| 3661 | kmem_cache_destroy(cfq_ioc_pool); |
| 3662 | } |
| 3663 | |
| 3664 | static int __init cfq_slab_setup(void) |
| 3665 | { |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 3666 | cfq_pool = KMEM_CACHE(cfq_queue, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | if (!cfq_pool) |
| 3668 | goto fail; |
| 3669 | |
Fabio Checconi | 34e6bbf | 2008-04-02 14:31:02 +0200 | [diff] [blame] | 3670 | cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | if (!cfq_ioc_pool) |
| 3672 | goto fail; |
| 3673 | |
| 3674 | return 0; |
| 3675 | fail: |
| 3676 | cfq_slab_kill(); |
| 3677 | return -ENOMEM; |
| 3678 | } |
| 3679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | /* |
| 3681 | * sysfs parts below --> |
| 3682 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | static ssize_t |
| 3684 | cfq_var_show(unsigned int var, char *page) |
| 3685 | { |
| 3686 | return sprintf(page, "%d\n", var); |
| 3687 | } |
| 3688 | |
| 3689 | static ssize_t |
| 3690 | cfq_var_store(unsigned int *var, const char *page, size_t count) |
| 3691 | { |
| 3692 | char *p = (char *) page; |
| 3693 | |
| 3694 | *var = simple_strtoul(p, &p, 10); |
| 3695 | return count; |
| 3696 | } |
| 3697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 3699 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 3701 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | unsigned int __data = __VAR; \ |
| 3703 | if (__CONV) \ |
| 3704 | __data = jiffies_to_msecs(__data); \ |
| 3705 | return cfq_var_show(__data, (page)); \ |
| 3706 | } |
| 3707 | SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3708 | SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); |
| 3709 | 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] | 3710 | SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); |
| 3711 | SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3712 | SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); |
| 3713 | SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); |
| 3714 | SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); |
| 3715 | SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 3716 | SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | #undef SHOW_FUNCTION |
| 3718 | |
| 3719 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 3720 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 3722 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | unsigned int __data; \ |
| 3724 | int ret = cfq_var_store(&__data, (page), count); \ |
| 3725 | if (__data < (MIN)) \ |
| 3726 | __data = (MIN); \ |
| 3727 | else if (__data > (MAX)) \ |
| 3728 | __data = (MAX); \ |
| 3729 | if (__CONV) \ |
| 3730 | *(__PTR) = msecs_to_jiffies(__data); \ |
| 3731 | else \ |
| 3732 | *(__PTR) = __data; \ |
| 3733 | return ret; \ |
| 3734 | } |
| 3735 | STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3736 | STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, |
| 3737 | UINT_MAX, 1); |
| 3738 | STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, |
| 3739 | UINT_MAX, 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 3740 | STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3741 | STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, |
| 3742 | UINT_MAX, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3743 | STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); |
| 3744 | STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); |
| 3745 | STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3746 | STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, |
| 3747 | UINT_MAX, 0); |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 3748 | STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | #undef STORE_FUNCTION |
| 3750 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 3751 | #define CFQ_ATTR(name) \ |
| 3752 | __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3753 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 3754 | static struct elv_fs_entry cfq_attrs[] = { |
| 3755 | CFQ_ATTR(quantum), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 3756 | CFQ_ATTR(fifo_expire_sync), |
| 3757 | CFQ_ATTR(fifo_expire_async), |
| 3758 | CFQ_ATTR(back_seek_max), |
| 3759 | CFQ_ATTR(back_seek_penalty), |
| 3760 | CFQ_ATTR(slice_sync), |
| 3761 | CFQ_ATTR(slice_async), |
| 3762 | CFQ_ATTR(slice_async_rq), |
| 3763 | CFQ_ATTR(slice_idle), |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 3764 | CFQ_ATTR(low_latency), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 3765 | __ATTR_NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | }; |
| 3767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | static struct elevator_type iosched_cfq = { |
| 3769 | .ops = { |
| 3770 | .elevator_merge_fn = cfq_merge, |
| 3771 | .elevator_merged_fn = cfq_merged_request, |
| 3772 | .elevator_merge_req_fn = cfq_merged_requests, |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 3773 | .elevator_allow_merge_fn = cfq_allow_merge, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3774 | .elevator_dispatch_fn = cfq_dispatch_requests, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | .elevator_add_req_fn = cfq_insert_request, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3776 | .elevator_activate_req_fn = cfq_activate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | .elevator_deactivate_req_fn = cfq_deactivate_request, |
| 3778 | .elevator_queue_empty_fn = cfq_queue_empty, |
| 3779 | .elevator_completed_req_fn = cfq_completed_request, |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 3780 | .elevator_former_req_fn = elv_rb_former_request, |
| 3781 | .elevator_latter_req_fn = elv_rb_latter_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3782 | .elevator_set_req_fn = cfq_set_request, |
| 3783 | .elevator_put_req_fn = cfq_put_request, |
| 3784 | .elevator_may_queue_fn = cfq_may_queue, |
| 3785 | .elevator_init_fn = cfq_init_queue, |
| 3786 | .elevator_exit_fn = cfq_exit_queue, |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 3787 | .trim = cfq_free_io_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | }, |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 3789 | .elevator_attrs = cfq_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | .elevator_name = "cfq", |
| 3791 | .elevator_owner = THIS_MODULE, |
| 3792 | }; |
| 3793 | |
| 3794 | static int __init cfq_init(void) |
| 3795 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3796 | /* |
| 3797 | * could be 0 on HZ < 1000 setups |
| 3798 | */ |
| 3799 | if (!cfq_slice_async) |
| 3800 | cfq_slice_async = 1; |
| 3801 | if (!cfq_slice_idle) |
| 3802 | cfq_slice_idle = 1; |
| 3803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | if (cfq_slab_setup()) |
| 3805 | return -ENOMEM; |
| 3806 | |
Adrian Bunk | 2fdd82b | 2007-12-12 18:51:56 +0100 | [diff] [blame] | 3807 | elv_register(&iosched_cfq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | |
Adrian Bunk | 2fdd82b | 2007-12-12 18:51:56 +0100 | [diff] [blame] | 3809 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | static void __exit cfq_exit(void) |
| 3813 | { |
Peter Zijlstra | 6e9a473 | 2006-09-30 23:28:10 -0700 | [diff] [blame] | 3814 | DECLARE_COMPLETION_ONSTACK(all_gone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | elv_unregister(&iosched_cfq); |
Al Viro | 334e94d | 2006-03-18 15:05:53 -0500 | [diff] [blame] | 3816 | ioc_gone = &all_gone; |
OGAWA Hirofumi | fba8227 | 2006-04-18 09:44:06 +0200 | [diff] [blame] | 3817 | /* ioc_gone's update must be visible before reading ioc_count */ |
| 3818 | smp_wmb(); |
Jens Axboe | d6de8be | 2008-05-28 14:46:59 +0200 | [diff] [blame] | 3819 | |
| 3820 | /* |
| 3821 | * this also protects us from entering cfq_slab_kill() with |
| 3822 | * pending RCU callbacks |
| 3823 | */ |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 3824 | if (elv_ioc_count_read(cfq_ioc_count)) |
Jens Axboe | 9a11b4e | 2008-05-29 09:32:08 +0200 | [diff] [blame] | 3825 | wait_for_completion(&all_gone); |
Christoph Hellwig | 83521d3 | 2005-10-30 15:01:39 -0800 | [diff] [blame] | 3826 | cfq_slab_kill(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | module_init(cfq_init); |
| 3830 | module_exit(cfq_exit); |
| 3831 | |
| 3832 | MODULE_AUTHOR("Jens Axboe"); |
| 3833 | MODULE_LICENSE("GPL"); |
| 3834 | MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler"); |