blob: 662d4e55b3c2c683f8ece4419282ad8925bedb90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050010#include <linux/blkdev.h>
11#include <linux/elevator.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010012#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020014#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020015#include <linux/blktrace_api.h>
Vivek Goyal25bc6b02009-12-03 12:59:43 -050016#include "blk-cgroup.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/*
19 * tunables
20 */
Jens Axboefe094d92008-01-31 13:08:54 +010021/* max queue in one round of service */
22static const int cfq_quantum = 4;
Arjan van de Ven64100092006-01-06 09:46:02 +010023static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010024/* maximum backwards seek, in KiB */
25static const int cfq_back_max = 16 * 1024;
26/* penalty of a backwards seek */
27static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020029static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010030static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020031static int cfq_slice_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010032static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
33static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020034
Jens Axboed9e76202007-04-20 14:27:50 +020035/*
Jens Axboe08717142008-01-28 11:38:15 +010036 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020037 */
Jens Axboe08717142008-01-28 11:38:15 +010038#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020039
40/*
41 * below this threshold, we consider thinktime immediate
42 */
43#define CFQ_MIN_TT (2)
44
Jeff Moyere6c5bc72009-10-23 17:14:52 -040045/*
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 Axboe22e2c502005-06-27 10:55:12 +020051#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020052#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050053#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020054
Jens Axboefe094d92008-01-31 13:08:54 +010055#define RQ_CIC(rq) \
56 ((struct cfq_io_context *) (rq)->elevator_private)
Jens Axboe7b679132008-05-30 12:23:07 +020057#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Christoph Lametere18b8902006-12-06 20:33:20 -080059static struct kmem_cache *cfq_pool;
60static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Tejun Heo245b2e72009-06-24 15:13:48 +090062static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050063static struct completion *ioc_gone;
Jens Axboe9a11b4e2008-05-29 09:32:08 +020064static DEFINE_SPINLOCK(ioc_gone_lock);
Al Viro334e94d2006-03-18 15:05:53 -050065
Jens Axboe22e2c502005-06-27 10:55:12 +020066#define CFQ_PRIO_LISTS IOPRIO_BE_NR
67#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020068#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
69
Jens Axboe206dc692006-03-28 13:03:44 +020070#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050071#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020072
Jens Axboe22e2c502005-06-27 10:55:12 +020073/*
Jens Axboecc09e292007-04-26 12:53:50 +020074 * 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 */
79struct cfq_rb_root {
80 struct rb_root rb;
81 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010082 unsigned count;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050083 u64 min_vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -050084 struct rb_node *active;
Vivek Goyal58ff82f2009-12-03 12:59:44 -050085 unsigned total_weight;
Jens Axboecc09e292007-04-26 12:53:50 +020086};
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050087#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, 0, 0, }
Jens Axboecc09e292007-04-26 12:53:50 +020088
89/*
Jens Axboe6118b702009-06-30 09:34:12 +020090 * Per process-grouping structure
91 */
92struct 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 Goyaldae739e2009-12-03 12:59:45 -0500118 /* time when queue got scheduled in to dispatch first request. */
119 unsigned long dispatch_start;
120 /* time when first request from queue completed and slice started. */
121 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200122 unsigned long slice_end;
123 long slice_resid;
124 unsigned int slice_dispatch;
125
126 /* pending metadata requests */
127 int meta_pending;
128 /* number of requests that are on the dispatch list or inside driver */
129 int dispatched;
130
131 /* io prio of this group */
132 unsigned short ioprio, org_ioprio;
133 unsigned short ioprio_class, org_ioprio_class;
134
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400135 unsigned int seek_samples;
136 u64 seek_total;
137 sector_t seek_mean;
138 sector_t last_request_pos;
Jeff Moyere6c5bc72009-10-23 17:14:52 -0400139 unsigned long seeky_start;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400140
Jens Axboe6118b702009-06-30 09:34:12 +0200141 pid_t pid;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400142
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100143 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400144 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500145 struct cfq_group *cfqg;
Jens Axboe6118b702009-06-30 09:34:12 +0200146};
147
148/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100149 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100150 * IDLE is handled separately, so it has negative index
151 */
152enum wl_prio_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100153 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500154 RT_WORKLOAD = 1,
155 IDLE_WORKLOAD = 2,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100156};
157
158/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100159 * Second index in the service_trees.
160 */
161enum wl_type_t {
162 ASYNC_WORKLOAD = 0,
163 SYNC_NOIDLE_WORKLOAD = 1,
164 SYNC_WORKLOAD = 2
165};
166
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500167/* This is per cgroup per device grouping structure */
168struct cfq_group {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500169 /* group service_tree member */
170 struct rb_node rb_node;
171
172 /* group service_tree key */
173 u64 vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500174 unsigned int weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500175 bool on_st;
176
177 /* number of cfqq currently on this group */
178 int nr_cfqq;
179
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500180 /* Per group busy queus average. Useful for workload slice calc. */
181 unsigned int busy_queues_avg[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200182 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100183 * rr lists of queues with requests, onle rr for each priority class.
184 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200185 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100186 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100187 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500188
189 unsigned long saved_workload_slice;
190 enum wl_type_t saved_workload;
191 enum wl_prio_t saved_serving_prio;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500192 struct blkio_group blkg;
193#ifdef CONFIG_CFQ_GROUP_IOSCHED
194 struct hlist_node cfqd_node;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500195 atomic_t ref;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500196#endif
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500197};
198
199/*
200 * Per block device queue structure
201 */
202struct cfq_data {
203 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500204 /* Root service tree for cfq_groups */
205 struct cfq_rb_root grp_service_tree;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500206 struct cfq_group root_group;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500207 /* Number of active cfq groups on group service tree */
208 int nr_groups;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500209
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100210 /*
211 * The priority currently being served
212 */
213 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100214 enum wl_type_t serving_type;
215 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500216 struct cfq_group *serving_group;
Corrado Zoccolo8e550632009-11-26 10:02:58 +0100217 bool noidle_tree_requires_idle;
Jens Axboea36e71f2009-04-15 12:15:11 +0200218
219 /*
220 * Each priority tree is sorted by next_request position. These
221 * trees are used when determining if two or more queues are
222 * interleaving requests (see cfq_close_cooperator).
223 */
224 struct rb_root prio_trees[CFQ_PRIO_LISTS];
225
Jens Axboe22e2c502005-06-27 10:55:12 +0200226 unsigned int busy_queues;
227
Jens Axboe5ad531d2009-07-03 12:57:48 +0200228 int rq_in_driver[2];
Jens Axboe3ed9a292007-04-23 08:33:33 +0200229 int sync_flight;
Aaron Carroll45333d52008-08-26 15:52:36 +0200230
231 /*
232 * queue-depth detection
233 */
234 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200235 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100236 /*
237 * hw_tag can be
238 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
239 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
240 * 0 => no NCQ
241 */
242 int hw_tag_est_depth;
243 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200244
245 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200246 * idle window management
247 */
248 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200249 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200250
251 struct cfq_queue *active_queue;
252 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200253
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200254 /*
255 * async queue for each priority case
256 */
257 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
258 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200259
Jens Axboe6d048f52007-04-25 12:44:27 +0200260 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * tunables, see top of file
264 */
265 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200266 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned int cfq_back_penalty;
268 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200269 unsigned int cfq_slice[2];
270 unsigned int cfq_slice_async_rq;
271 unsigned int cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200272 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500273
274 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jens Axboe6118b702009-06-30 09:34:12 +0200276 /*
277 * Fallback dummy cfqq for extreme OOM conditions
278 */
279 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200280
281 unsigned long last_end_sync_rq;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500282
283 /* List of cfq groups being managed on this device*/
284 struct hlist_head cfqg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
Vivek Goyal25fb5162009-12-03 12:59:46 -0500287static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
288
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500289static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
290 enum wl_prio_t prio,
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100291 enum wl_type_t type,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100292 struct cfq_data *cfqd)
293{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500294 if (!cfqg)
295 return NULL;
296
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100297 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500298 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100299
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500300 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100301}
302
Jens Axboe3b181522005-06-27 10:56:24 +0200303enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100304 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
305 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200306 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100307 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100308 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
309 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
310 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100311 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200312 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400313 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100314 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Jens Axboe3b181522005-06-27 10:56:24 +0200315};
316
317#define CFQ_CFQQ_FNS(name) \
318static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
319{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100320 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200321} \
322static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
323{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100324 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200325} \
326static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
327{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100328 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200329}
330
331CFQ_CFQQ_FNS(on_rr);
332CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200333CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200334CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200335CFQ_CFQQ_FNS(fifo_expire);
336CFQ_CFQQ_FNS(idle_window);
337CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100338CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200339CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200340CFQ_CFQQ_FNS(coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100341CFQ_CFQQ_FNS(deep);
Jens Axboe3b181522005-06-27 10:56:24 +0200342#undef CFQ_CFQQ_FNS
343
Vivek Goyal2868ef72009-12-03 12:59:48 -0500344#ifdef CONFIG_DEBUG_CFQ_IOSCHED
345#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
346 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
347 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
348 blkg_path(&(cfqq)->cfqg->blkg), ##args);
349
350#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
351 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
352 blkg_path(&(cfqg)->blkg), ##args); \
353
354#else
Jens Axboe7b679132008-05-30 12:23:07 +0200355#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
356 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500357#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0);
358#endif
Jens Axboe7b679132008-05-30 12:23:07 +0200359#define cfq_log(cfqd, fmt, args...) \
360 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
361
Vivek Goyal615f0252009-12-03 12:59:39 -0500362/* Traverses through cfq group service trees */
363#define for_each_cfqg_st(cfqg, i, j, st) \
364 for (i = 0; i <= IDLE_WORKLOAD; i++) \
365 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
366 : &cfqg->service_tree_idle; \
367 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
368 (i == IDLE_WORKLOAD && j == 0); \
369 j++, st = i < IDLE_WORKLOAD ? \
370 &cfqg->service_trees[i][j]: NULL) \
371
372
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100373static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
374{
375 if (cfq_class_idle(cfqq))
376 return IDLE_WORKLOAD;
377 if (cfq_class_rt(cfqq))
378 return RT_WORKLOAD;
379 return BE_WORKLOAD;
380}
381
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100382
383static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
384{
385 if (!cfq_cfqq_sync(cfqq))
386 return ASYNC_WORKLOAD;
387 if (!cfq_cfqq_idle_window(cfqq))
388 return SYNC_NOIDLE_WORKLOAD;
389 return SYNC_WORKLOAD;
390}
391
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500392static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
393 struct cfq_data *cfqd,
394 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100395{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500396 if (wl == IDLE_WORKLOAD)
397 return cfqg->service_tree_idle.count;
398
399 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
400 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
401 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100402}
403
Jens Axboe165125e2007-07-24 09:28:11 +0200404static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200405static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100406 struct io_context *, gfp_t);
Jens Axboe4ac845a2008-01-24 08:44:49 +0100407static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
Vasily Tarasov91fac312007-04-25 12:29:51 +0200408 struct io_context *);
409
Jens Axboe5ad531d2009-07-03 12:57:48 +0200410static inline int rq_in_driver(struct cfq_data *cfqd)
411{
412 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
413}
414
Vasily Tarasov91fac312007-04-25 12:29:51 +0200415static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200416 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200417{
Jens Axboea6151c32009-10-07 20:02:57 +0200418 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200419}
420
421static inline void cic_set_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200422 struct cfq_queue *cfqq, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200423{
Jens Axboea6151c32009-10-07 20:02:57 +0200424 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200425}
426
427/*
428 * We regard a request as SYNC, if it's either a read or has the SYNC bit
429 * set (in which case it could also be direct WRITE).
430 */
Jens Axboea6151c32009-10-07 20:02:57 +0200431static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200432{
Jens Axboea6151c32009-10-07 20:02:57 +0200433 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700437 * scheduler run of queue, if there are requests pending and no one in the
438 * driver that will restart queueing
439 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200440static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700441{
Jens Axboe7b679132008-05-30 12:23:07 +0200442 if (cfqd->busy_queues) {
443 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200444 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200445 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700446}
447
Jens Axboe165125e2007-07-24 09:28:11 +0200448static int cfq_queue_empty(struct request_queue *q)
Andrew Morton99f95e52005-06-27 20:14:05 -0700449{
450 struct cfq_data *cfqd = q->elevator->elevator_data;
451
Vivek Goyalf04a6422009-12-03 12:59:40 -0500452 return !cfqd->rq_queued;
Andrew Morton99f95e52005-06-27 20:14:05 -0700453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100456 * Scale schedule slice based on io priority. Use the sync time slice only
457 * if a queue is marked sync and has sync io queued. A sync queue with async
458 * io only, should not get full sync slice length.
459 */
Jens Axboea6151c32009-10-07 20:02:57 +0200460static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200461 unsigned short prio)
462{
463 const int base_slice = cfqd->cfq_slice[sync];
464
465 WARN_ON(prio >= IOPRIO_BE_NR);
466
467 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
468}
469
Jens Axboe44f7c162007-01-19 11:51:58 +1100470static inline int
471cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
472{
Jens Axboed9e76202007-04-20 14:27:50 +0200473 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100474}
475
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500476static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
477{
478 u64 d = delta << CFQ_SERVICE_SHIFT;
479
480 d = d * BLKIO_WEIGHT_DEFAULT;
481 do_div(d, cfqg->weight);
482 return d;
483}
484
485static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
486{
487 s64 delta = (s64)(vdisktime - min_vdisktime);
488 if (delta > 0)
489 min_vdisktime = vdisktime;
490
491 return min_vdisktime;
492}
493
494static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
495{
496 s64 delta = (s64)(vdisktime - min_vdisktime);
497 if (delta < 0)
498 min_vdisktime = vdisktime;
499
500 return min_vdisktime;
501}
502
503static void update_min_vdisktime(struct cfq_rb_root *st)
504{
505 u64 vdisktime = st->min_vdisktime;
506 struct cfq_group *cfqg;
507
508 if (st->active) {
509 cfqg = rb_entry_cfqg(st->active);
510 vdisktime = cfqg->vdisktime;
511 }
512
513 if (st->left) {
514 cfqg = rb_entry_cfqg(st->left);
515 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
516 }
517
518 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
519}
520
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100521/*
522 * get averaged number of queues of RT/BE priority.
523 * average is updated, with a formula that gives more weight to higher numbers,
524 * to quickly follows sudden increases and decrease slowly
525 */
526
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500527static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
528 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100529{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100530 unsigned min_q, max_q;
531 unsigned mult = cfq_hist_divisor - 1;
532 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500533 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100534
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500535 min_q = min(cfqg->busy_queues_avg[rt], busy);
536 max_q = max(cfqg->busy_queues_avg[rt], busy);
537 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100538 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500539 return cfqg->busy_queues_avg[rt];
540}
541
542static inline unsigned
543cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
544{
545 struct cfq_rb_root *st = &cfqd->grp_service_tree;
546
547 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100548}
549
Jens Axboe44f7c162007-01-19 11:51:58 +1100550static inline void
551cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
552{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100553 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
554 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500555 /*
556 * interested queues (we consider only the ones with the same
557 * priority class in the cfq group)
558 */
559 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
560 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100561 unsigned sync_slice = cfqd->cfq_slice[1];
562 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500563 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
564
565 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100566 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
567 /* scale low_slice according to IO priority
568 * and sync vs async */
569 unsigned low_slice =
570 min(slice, base_low_slice * slice / sync_slice);
571 /* the adapted slice value is scaled to fit all iqs
572 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500573 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100574 low_slice);
575 }
576 }
Vivek Goyaldae739e2009-12-03 12:59:45 -0500577 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100578 cfqq->slice_end = jiffies + slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200579 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100580}
581
582/*
583 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
584 * isn't valid until the first request from the dispatch is activated
585 * and the slice time set.
586 */
Jens Axboea6151c32009-10-07 20:02:57 +0200587static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100588{
589 if (cfq_cfqq_slice_new(cfqq))
590 return 0;
591 if (time_before(jiffies, cfqq->slice_end))
592 return 0;
593
594 return 1;
595}
596
597/*
Jens Axboe5e705372006-07-13 12:39:25 +0200598 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200600 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Jens Axboe5e705372006-07-13 12:39:25 +0200602static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100603cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100605 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200607#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
608#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
609 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jens Axboe5e705372006-07-13 12:39:25 +0200611 if (rq1 == NULL || rq1 == rq2)
612 return rq2;
613 if (rq2 == NULL)
614 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200615
Jens Axboe5e705372006-07-13 12:39:25 +0200616 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
617 return rq1;
618 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
619 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200620 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
621 return rq1;
622 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
623 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Tejun Heo83096eb2009-05-07 22:24:39 +0900625 s1 = blk_rq_pos(rq1);
626 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * by definition, 1KiB is 2 sectors
630 */
631 back_max = cfqd->cfq_back_max * 2;
632
633 /*
634 * Strict one way elevator _except_ in the case where we allow
635 * short backward seeks which are biased as twice the cost of a
636 * similar forward seek.
637 */
638 if (s1 >= last)
639 d1 = s1 - last;
640 else if (s1 + back_max >= last)
641 d1 = (last - s1) * cfqd->cfq_back_penalty;
642 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200643 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (s2 >= last)
646 d2 = s2 - last;
647 else if (s2 + back_max >= last)
648 d2 = (last - s2) * cfqd->cfq_back_penalty;
649 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200650 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Andreas Mohre8a99052006-03-28 08:59:49 +0200654 /*
655 * By doing switch() on the bit mask "wrap" we avoid having to
656 * check two variables for all permutations: --> faster!
657 */
658 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200659 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200660 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200661 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200662 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200663 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200664 else {
665 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200666 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200667 else
Jens Axboe5e705372006-07-13 12:39:25 +0200668 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200669 }
670
671 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200672 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200673 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200674 return rq2;
675 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200676 default:
677 /*
678 * Since both rqs are wrapped,
679 * start with the one that's further behind head
680 * (--> only *one* back seek required),
681 * since back seek takes more time than forward.
682 */
683 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200684 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 else
Jens Axboe5e705372006-07-13 12:39:25 +0200686 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688}
689
Jens Axboe498d3aa22007-04-26 12:54:48 +0200690/*
691 * The below is leftmost cache rbtree addon
692 */
Jens Axboe08717142008-01-28 11:38:15 +0100693static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200694{
Vivek Goyal615f0252009-12-03 12:59:39 -0500695 /* Service tree is empty */
696 if (!root->count)
697 return NULL;
698
Jens Axboecc09e292007-04-26 12:53:50 +0200699 if (!root->left)
700 root->left = rb_first(&root->rb);
701
Jens Axboe08717142008-01-28 11:38:15 +0100702 if (root->left)
703 return rb_entry(root->left, struct cfq_queue, rb_node);
704
705 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200706}
707
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500708static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
709{
710 if (!root->left)
711 root->left = rb_first(&root->rb);
712
713 if (root->left)
714 return rb_entry_cfqg(root->left);
715
716 return NULL;
717}
718
Jens Axboea36e71f2009-04-15 12:15:11 +0200719static void rb_erase_init(struct rb_node *n, struct rb_root *root)
720{
721 rb_erase(n, root);
722 RB_CLEAR_NODE(n);
723}
724
Jens Axboecc09e292007-04-26 12:53:50 +0200725static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
726{
727 if (root->left == n)
728 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200729 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100730 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * would be nice to take fifo expire time into account as well
735 */
Jens Axboe5e705372006-07-13 12:39:25 +0200736static struct request *
737cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
738 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jens Axboe21183b02006-07-13 12:33:14 +0200740 struct rb_node *rbnext = rb_next(&last->rb_node);
741 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200742 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jens Axboe21183b02006-07-13 12:33:14 +0200744 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200747 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200750 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200751 else {
752 rbnext = rb_first(&cfqq->sort_list);
753 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200754 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100757 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Jens Axboed9e76202007-04-20 14:27:50 +0200760static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
761 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Jens Axboed9e76202007-04-20 14:27:50 +0200763 /*
764 * just an approximation, should be ok.
765 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500766 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100767 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200768}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500770static inline s64
771cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
772{
773 return cfqg->vdisktime - st->min_vdisktime;
774}
775
776static void
777__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
778{
779 struct rb_node **node = &st->rb.rb_node;
780 struct rb_node *parent = NULL;
781 struct cfq_group *__cfqg;
782 s64 key = cfqg_key(st, cfqg);
783 int left = 1;
784
785 while (*node != NULL) {
786 parent = *node;
787 __cfqg = rb_entry_cfqg(parent);
788
789 if (key < cfqg_key(st, __cfqg))
790 node = &parent->rb_left;
791 else {
792 node = &parent->rb_right;
793 left = 0;
794 }
795 }
796
797 if (left)
798 st->left = &cfqg->rb_node;
799
800 rb_link_node(&cfqg->rb_node, parent, node);
801 rb_insert_color(&cfqg->rb_node, &st->rb);
802}
803
804static void
805cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
806{
807 struct cfq_rb_root *st = &cfqd->grp_service_tree;
808 struct cfq_group *__cfqg;
809 struct rb_node *n;
810
811 cfqg->nr_cfqq++;
812 if (cfqg->on_st)
813 return;
814
815 /*
816 * Currently put the group at the end. Later implement something
817 * so that groups get lesser vtime based on their weights, so that
818 * if group does not loose all if it was not continously backlogged.
819 */
820 n = rb_last(&st->rb);
821 if (n) {
822 __cfqg = rb_entry_cfqg(n);
823 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
824 } else
825 cfqg->vdisktime = st->min_vdisktime;
826
827 __cfq_group_service_tree_add(st, cfqg);
828 cfqg->on_st = true;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500829 cfqd->nr_groups++;
830 st->total_weight += cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500831}
832
833static void
834cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
835{
836 struct cfq_rb_root *st = &cfqd->grp_service_tree;
837
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500838 if (st->active == &cfqg->rb_node)
839 st->active = NULL;
840
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500841 BUG_ON(cfqg->nr_cfqq < 1);
842 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500843
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500844 /* If there are other cfq queues under this group, don't delete it */
845 if (cfqg->nr_cfqq)
846 return;
847
Vivek Goyal2868ef72009-12-03 12:59:48 -0500848 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500849 cfqg->on_st = false;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500850 cfqd->nr_groups--;
851 st->total_weight -= cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500852 if (!RB_EMPTY_NODE(&cfqg->rb_node))
853 cfq_rb_erase(&cfqg->rb_node, st);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500854 cfqg->saved_workload_slice = 0;
855}
856
857static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
858{
859 unsigned int slice_used, allocated_slice;
860
861 /*
862 * Queue got expired before even a single request completed or
863 * got expired immediately after first request completion.
864 */
865 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
866 /*
867 * Also charge the seek time incurred to the group, otherwise
868 * if there are mutiple queues in the group, each can dispatch
869 * a single request on seeky media and cause lots of seek time
870 * and group will never know it.
871 */
872 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
873 1);
874 } else {
875 slice_used = jiffies - cfqq->slice_start;
876 allocated_slice = cfqq->slice_end - cfqq->slice_start;
877 if (slice_used > allocated_slice)
878 slice_used = allocated_slice;
879 }
880
881 cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u", slice_used);
882 return slice_used;
883}
884
885static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
886 struct cfq_queue *cfqq)
887{
888 struct cfq_rb_root *st = &cfqd->grp_service_tree;
889 unsigned int used_sl;
890
891 used_sl = cfq_cfqq_slice_usage(cfqq);
892
893 /* Can't update vdisktime while group is on service tree */
894 cfq_rb_erase(&cfqg->rb_node, st);
895 cfqg->vdisktime += cfq_scale_slice(used_sl, cfqg);
896 __cfq_group_service_tree_add(st, cfqg);
897
898 /* This group is being expired. Save the context */
899 if (time_after(cfqd->workload_expires, jiffies)) {
900 cfqg->saved_workload_slice = cfqd->workload_expires
901 - jiffies;
902 cfqg->saved_workload = cfqd->serving_type;
903 cfqg->saved_serving_prio = cfqd->serving_prio;
904 } else
905 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -0500906
907 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
908 st->min_vdisktime);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500909}
910
Vivek Goyal25fb5162009-12-03 12:59:46 -0500911#ifdef CONFIG_CFQ_GROUP_IOSCHED
912static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
913{
914 if (blkg)
915 return container_of(blkg, struct cfq_group, blkg);
916 return NULL;
917}
918
919static struct cfq_group *
920cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
921{
922 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
923 struct cfq_group *cfqg = NULL;
924 void *key = cfqd;
925 int i, j;
926 struct cfq_rb_root *st;
927
928 /* Do we need to take this reference */
929 if (!css_tryget(&blkcg->css))
930 return NULL;;
931
932 cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
933 if (cfqg || !create)
934 goto done;
935
936 cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
937 if (!cfqg)
938 goto done;
939
940 cfqg->weight = blkcg->weight;
941 for_each_cfqg_st(cfqg, i, j, st)
942 *st = CFQ_RB_ROOT;
943 RB_CLEAR_NODE(&cfqg->rb_node);
944
Vivek Goyalb1c35762009-12-03 12:59:47 -0500945 /*
946 * Take the initial reference that will be released on destroy
947 * This can be thought of a joint reference by cgroup and
948 * elevator which will be dropped by either elevator exit
949 * or cgroup deletion path depending on who is exiting first.
950 */
951 atomic_set(&cfqg->ref, 1);
952
Vivek Goyal25fb5162009-12-03 12:59:46 -0500953 /* Add group onto cgroup list */
954 blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd);
955
956 /* Add group on cfqd list */
957 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
958
959done:
960 css_put(&blkcg->css);
961 return cfqg;
962}
963
964/*
965 * Search for the cfq group current task belongs to. If create = 1, then also
966 * create the cfq group if it does not exist. request_queue lock must be held.
967 */
968static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
969{
970 struct cgroup *cgroup;
971 struct cfq_group *cfqg = NULL;
972
973 rcu_read_lock();
974 cgroup = task_cgroup(current, blkio_subsys_id);
975 cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
976 if (!cfqg && create)
977 cfqg = &cfqd->root_group;
978 rcu_read_unlock();
979 return cfqg;
980}
981
982static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
983{
984 /* Currently, all async queues are mapped to root group */
985 if (!cfq_cfqq_sync(cfqq))
986 cfqg = &cfqq->cfqd->root_group;
987
988 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500989 /* cfqq reference on cfqg */
990 atomic_inc(&cfqq->cfqg->ref);
Vivek Goyal25fb5162009-12-03 12:59:46 -0500991}
Vivek Goyalb1c35762009-12-03 12:59:47 -0500992
993static void cfq_put_cfqg(struct cfq_group *cfqg)
994{
995 struct cfq_rb_root *st;
996 int i, j;
997
998 BUG_ON(atomic_read(&cfqg->ref) <= 0);
999 if (!atomic_dec_and_test(&cfqg->ref))
1000 return;
1001 for_each_cfqg_st(cfqg, i, j, st)
1002 BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
1003 kfree(cfqg);
1004}
1005
1006static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1007{
1008 /* Something wrong if we are trying to remove same group twice */
1009 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1010
1011 hlist_del_init(&cfqg->cfqd_node);
1012
1013 /*
1014 * Put the reference taken at the time of creation so that when all
1015 * queues are gone, group can be destroyed.
1016 */
1017 cfq_put_cfqg(cfqg);
1018}
1019
1020static void cfq_release_cfq_groups(struct cfq_data *cfqd)
1021{
1022 struct hlist_node *pos, *n;
1023 struct cfq_group *cfqg;
1024
1025 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1026 /*
1027 * If cgroup removal path got to blk_group first and removed
1028 * it from cgroup list, then it will take care of destroying
1029 * cfqg also.
1030 */
1031 if (!blkiocg_del_blkio_group(&cfqg->blkg))
1032 cfq_destroy_cfqg(cfqd, cfqg);
1033 }
1034}
1035
1036/*
1037 * Blk cgroup controller notification saying that blkio_group object is being
1038 * delinked as associated cgroup object is going away. That also means that
1039 * no new IO will come in this group. So get rid of this group as soon as
1040 * any pending IO in the group is finished.
1041 *
1042 * This function is called under rcu_read_lock(). key is the rcu protected
1043 * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
1044 * read lock.
1045 *
1046 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1047 * it should not be NULL as even if elevator was exiting, cgroup deltion
1048 * path got to it first.
1049 */
1050void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
1051{
1052 unsigned long flags;
1053 struct cfq_data *cfqd = key;
1054
1055 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1056 cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
1057 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1058}
1059
Vivek Goyal25fb5162009-12-03 12:59:46 -05001060#else /* GROUP_IOSCHED */
1061static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1062{
1063 return &cfqd->root_group;
1064}
1065static inline void
1066cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1067 cfqq->cfqg = cfqg;
1068}
1069
Vivek Goyalb1c35762009-12-03 12:59:47 -05001070static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1071static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1072
Vivek Goyal25fb5162009-12-03 12:59:46 -05001073#endif /* GROUP_IOSCHED */
1074
Jens Axboe498d3aa22007-04-26 12:54:48 +02001075/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001076 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001077 * requests waiting to be processed. It is sorted in the order that
1078 * we will service the queues.
1079 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001080static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001081 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001082{
Jens Axboe08717142008-01-28 11:38:15 +01001083 struct rb_node **p, *parent;
1084 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001085 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001086 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001087 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001088 int new_cfqq = 1;
Jens Axboed9e76202007-04-20 14:27:50 +02001089
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001090 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
1091 cfqq_type(cfqq), cfqd);
Jens Axboe08717142008-01-28 11:38:15 +01001092 if (cfq_class_idle(cfqq)) {
1093 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001094 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001095 if (parent && parent != &cfqq->rb_node) {
1096 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1097 rb_key += __cfqq->rb_key;
1098 } else
1099 rb_key += jiffies;
1100 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001101 /*
1102 * Get our rb key offset. Subtract any residual slice
1103 * value carried from last service. A negative resid
1104 * count indicates slice overrun, and this should position
1105 * the next service time further away in the tree.
1106 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001107 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001108 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001109 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001110 } else {
1111 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001112 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001113 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1114 }
Jens Axboed9e76202007-04-20 14:27:50 +02001115
1116 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001117 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001118 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001119 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001120 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001121 if (rb_key == cfqq->rb_key &&
1122 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001123 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001124
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001125 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1126 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001127 }
Jens Axboed9e76202007-04-20 14:27:50 +02001128
Jens Axboe498d3aa22007-04-26 12:54:48 +02001129 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001130 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001131 cfqq->service_tree = service_tree;
1132 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001133 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001134 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001135
Jens Axboed9e76202007-04-20 14:27:50 +02001136 parent = *p;
1137 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1138
Jens Axboe0c534e02007-04-18 20:01:57 +02001139 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001140 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001141 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001142 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001143 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001144 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001145 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001146 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001147 }
Jens Axboe67060e32007-04-18 20:13:32 +02001148
1149 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001150 }
1151
Jens Axboecc09e292007-04-26 12:53:50 +02001152 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001153 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001154
Jens Axboed9e76202007-04-20 14:27:50 +02001155 cfqq->rb_key = rb_key;
1156 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001157 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1158 service_tree->count++;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001159 if (add_front || !new_cfqq)
1160 return;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001161 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Jens Axboea36e71f2009-04-15 12:15:11 +02001164static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001165cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1166 sector_t sector, struct rb_node **ret_parent,
1167 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001168{
Jens Axboea36e71f2009-04-15 12:15:11 +02001169 struct rb_node **p, *parent;
1170 struct cfq_queue *cfqq = NULL;
1171
1172 parent = NULL;
1173 p = &root->rb_node;
1174 while (*p) {
1175 struct rb_node **n;
1176
1177 parent = *p;
1178 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1179
1180 /*
1181 * Sort strictly based on sector. Smallest to the left,
1182 * largest to the right.
1183 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001184 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001185 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001186 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001187 n = &(*p)->rb_left;
1188 else
1189 break;
1190 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001191 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001192 }
1193
1194 *ret_parent = parent;
1195 if (rb_link)
1196 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001197 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001198}
1199
1200static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1201{
Jens Axboea36e71f2009-04-15 12:15:11 +02001202 struct rb_node **p, *parent;
1203 struct cfq_queue *__cfqq;
1204
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001205 if (cfqq->p_root) {
1206 rb_erase(&cfqq->p_node, cfqq->p_root);
1207 cfqq->p_root = NULL;
1208 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001209
1210 if (cfq_class_idle(cfqq))
1211 return;
1212 if (!cfqq->next_rq)
1213 return;
1214
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001215 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001216 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1217 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001218 if (!__cfqq) {
1219 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001220 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1221 } else
1222 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001223}
1224
Jens Axboe498d3aa22007-04-26 12:54:48 +02001225/*
1226 * Update cfqq's position in the service tree.
1227 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001228static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001229{
Jens Axboe6d048f52007-04-25 12:44:27 +02001230 /*
1231 * Resorting requires the cfqq to be on the RR list already.
1232 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001233 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001234 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001235 cfq_prio_tree_add(cfqd, cfqq);
1236 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
1240 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001241 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Jens Axboefebffd62008-01-28 13:19:43 +01001243static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Jens Axboe7b679132008-05-30 12:23:07 +02001245 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001246 BUG_ON(cfq_cfqq_on_rr(cfqq));
1247 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 cfqd->busy_queues++;
1249
Jens Axboeedd75ff2007-04-19 12:03:34 +02001250 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Jens Axboe498d3aa22007-04-26 12:54:48 +02001253/*
1254 * Called when the cfqq no longer has requests pending, remove it from
1255 * the service tree.
1256 */
Jens Axboefebffd62008-01-28 13:19:43 +01001257static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Jens Axboe7b679132008-05-30 12:23:07 +02001259 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001260 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1261 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001263 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1264 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1265 cfqq->service_tree = NULL;
1266 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001267 if (cfqq->p_root) {
1268 rb_erase(&cfqq->p_node, cfqq->p_root);
1269 cfqq->p_root = NULL;
1270 }
Jens Axboed9e76202007-04-20 14:27:50 +02001271
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001272 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 BUG_ON(!cfqd->busy_queues);
1274 cfqd->busy_queues--;
1275}
1276
1277/*
1278 * rb tree support functions
1279 */
Jens Axboefebffd62008-01-28 13:19:43 +01001280static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Jens Axboe5e705372006-07-13 12:39:25 +02001282 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001283 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Jens Axboeb4878f22005-10-20 16:42:29 +02001285 BUG_ON(!cfqq->queued[sync]);
1286 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Jens Axboe5e705372006-07-13 12:39:25 +02001288 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Vivek Goyalf04a6422009-12-03 12:59:40 -05001290 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1291 /*
1292 * Queue will be deleted from service tree when we actually
1293 * expire it later. Right now just remove it from prio tree
1294 * as it is empty.
1295 */
1296 if (cfqq->p_root) {
1297 rb_erase(&cfqq->p_node, cfqq->p_root);
1298 cfqq->p_root = NULL;
1299 }
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Jens Axboe5e705372006-07-13 12:39:25 +02001303static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Jens Axboe5e705372006-07-13 12:39:25 +02001305 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboea36e71f2009-04-15 12:15:11 +02001307 struct request *__alias, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jens Axboe5380a102006-07-13 12:37:56 +02001309 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /*
1312 * looks a little odd, but the first insert might return an alias.
1313 * if that happens, put the alias on the dispatch list
1314 */
Jens Axboe21183b02006-07-13 12:33:14 +02001315 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +02001316 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001317
1318 if (!cfq_cfqq_on_rr(cfqq))
1319 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001320
1321 /*
1322 * check if this request is a better next-serve candidate
1323 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001324 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001325 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001326
1327 /*
1328 * adjust priority tree position, if ->next_rq changes
1329 */
1330 if (prev != cfqq->next_rq)
1331 cfq_prio_tree_add(cfqd, cfqq);
1332
Jens Axboe5044eed2007-04-25 11:53:48 +02001333 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Jens Axboefebffd62008-01-28 13:19:43 +01001336static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Jens Axboe5380a102006-07-13 12:37:56 +02001338 elv_rb_del(&cfqq->sort_list, rq);
1339 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +02001340 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Jens Axboe206dc692006-03-28 13:03:44 +02001343static struct request *
1344cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Jens Axboe206dc692006-03-28 13:03:44 +02001346 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001347 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001348 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jens Axboe4ac845a2008-01-24 08:44:49 +01001350 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001351 if (!cic)
1352 return NULL;
1353
1354 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001355 if (cfqq) {
1356 sector_t sector = bio->bi_sector + bio_sectors(bio);
1357
Jens Axboe21183b02006-07-13 12:33:14 +02001358 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return NULL;
1362}
1363
Jens Axboe165125e2007-07-24 09:28:11 +02001364static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001365{
1366 struct cfq_data *cfqd = q->elevator->elevator_data;
1367
Jens Axboe5ad531d2009-07-03 12:57:48 +02001368 cfqd->rq_in_driver[rq_is_sync(rq)]++;
Jens Axboe7b679132008-05-30 12:23:07 +02001369 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +02001370 rq_in_driver(cfqd));
Jens Axboe25776e32006-06-01 10:12:26 +02001371
Tejun Heo5b936292009-05-07 22:24:38 +09001372 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001373}
1374
Jens Axboe165125e2007-07-24 09:28:11 +02001375static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Jens Axboe22e2c502005-06-27 10:55:12 +02001377 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5ad531d2009-07-03 12:57:48 +02001378 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Jens Axboe5ad531d2009-07-03 12:57:48 +02001380 WARN_ON(!cfqd->rq_in_driver[sync]);
1381 cfqd->rq_in_driver[sync]--;
Jens Axboe7b679132008-05-30 12:23:07 +02001382 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +02001383 rq_in_driver(cfqd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Jens Axboeb4878f22005-10-20 16:42:29 +02001386static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Jens Axboe5e705372006-07-13 12:39:25 +02001388 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001389
Jens Axboe5e705372006-07-13 12:39:25 +02001390 if (cfqq->next_rq == rq)
1391 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Jens Axboeb4878f22005-10-20 16:42:29 +02001393 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001394 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001395
Aaron Carroll45333d52008-08-26 15:52:36 +02001396 cfqq->cfqd->rq_queued--;
Jens Axboe374f84a2006-07-23 01:42:19 +02001397 if (rq_is_meta(rq)) {
1398 WARN_ON(!cfqq->meta_pending);
1399 cfqq->meta_pending--;
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Jens Axboe165125e2007-07-24 09:28:11 +02001403static int cfq_merge(struct request_queue *q, struct request **req,
1404 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct cfq_data *cfqd = q->elevator->elevator_data;
1407 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Jens Axboe206dc692006-03-28 13:03:44 +02001409 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001410 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001411 *req = __rq;
1412 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Jens Axboe165125e2007-07-24 09:28:11 +02001418static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001419 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Jens Axboe21183b02006-07-13 12:33:14 +02001421 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001422 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Jens Axboe5e705372006-07-13 12:39:25 +02001424 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
1428static void
Jens Axboe165125e2007-07-24 09:28:11 +02001429cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct request *next)
1431{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001432 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001433 /*
1434 * reposition in fifo if next is older than rq
1435 */
1436 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001437 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001438 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001439 rq_set_fifo_time(rq, rq_fifo_time(next));
1440 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001441
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001442 if (cfqq->next_rq == next)
1443 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001444 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +02001445}
1446
Jens Axboe165125e2007-07-24 09:28:11 +02001447static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001448 struct bio *bio)
1449{
1450 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001451 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001452 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001453
1454 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001455 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001456 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001457 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001458 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001459
1460 /*
Jens Axboe719d3402006-12-22 09:38:53 +01001461 * Lookup the cfqq that this bio will be queued with. Allow
1462 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001463 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01001464 cic = cfq_cic_lookup(cfqd, current->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001465 if (!cic)
Jens Axboea6151c32009-10-07 20:02:57 +02001466 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001467
Vasily Tarasov91fac312007-04-25 12:29:51 +02001468 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001469 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001470}
1471
Jens Axboefebffd62008-01-28 13:19:43 +01001472static void __cfq_set_active_queue(struct cfq_data *cfqd,
1473 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001474{
1475 if (cfqq) {
Jens Axboe7b679132008-05-30 12:23:07 +02001476 cfq_log_cfqq(cfqd, cfqq, "set_active");
Vivek Goyaldae739e2009-12-03 12:59:45 -05001477 cfqq->slice_start = 0;
1478 cfqq->dispatch_start = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001479 cfqq->slice_end = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001480 cfqq->slice_dispatch = 0;
1481
Jens Axboe2f5cb732009-04-07 08:51:19 +02001482 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboeb0291952009-04-07 11:38:31 +02001483 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001484 cfq_clear_cfqq_must_alloc_slice(cfqq);
1485 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +11001486 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001487
1488 del_timer(&cfqd->idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02001489 }
1490
1491 cfqd->active_queue = cfqq;
1492}
1493
1494/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001495 * current cfqq expired its slice (or was too idle), select new one
1496 */
1497static void
1498__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001499 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001500{
Jens Axboe7b679132008-05-30 12:23:07 +02001501 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1502
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001503 if (cfq_cfqq_wait_request(cfqq))
1504 del_timer(&cfqd->idle_slice_timer);
1505
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001506 cfq_clear_cfqq_wait_request(cfqq);
1507
1508 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001509 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001510 */
Jens Axboe7b679132008-05-30 12:23:07 +02001511 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
Jens Axboec5b680f2007-01-19 11:56:49 +11001512 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001513 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1514 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001515
Vivek Goyaldae739e2009-12-03 12:59:45 -05001516 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
1517
Vivek Goyalf04a6422009-12-03 12:59:40 -05001518 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1519 cfq_del_cfqq_rr(cfqd, cfqq);
1520
Jens Axboeedd75ff2007-04-19 12:03:34 +02001521 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001522
1523 if (cfqq == cfqd->active_queue)
1524 cfqd->active_queue = NULL;
1525
Vivek Goyaldae739e2009-12-03 12:59:45 -05001526 if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
1527 cfqd->grp_service_tree.active = NULL;
1528
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001529 if (cfqd->active_cic) {
1530 put_io_context(cfqd->active_cic->ioc);
1531 cfqd->active_cic = NULL;
1532 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001533}
1534
Jens Axboea6151c32009-10-07 20:02:57 +02001535static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001536{
1537 struct cfq_queue *cfqq = cfqd->active_queue;
1538
1539 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +02001540 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001541}
1542
Jens Axboe498d3aa22007-04-26 12:54:48 +02001543/*
1544 * Get next queue for service. Unless we have a queue preemption,
1545 * we'll simply select the first cfqq in the service tree.
1546 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001547static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001548{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001549 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001550 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1551 cfqd->serving_type, cfqd);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001552
Vivek Goyalf04a6422009-12-03 12:59:40 -05001553 if (!cfqd->rq_queued)
1554 return NULL;
1555
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001556 /* There is nothing to dispatch */
1557 if (!service_tree)
1558 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001559 if (RB_EMPTY_ROOT(&service_tree->rb))
1560 return NULL;
1561 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001562}
1563
Vivek Goyalf04a6422009-12-03 12:59:40 -05001564static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1565{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001566 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001567 struct cfq_queue *cfqq;
1568 int i, j;
1569 struct cfq_rb_root *st;
1570
1571 if (!cfqd->rq_queued)
1572 return NULL;
1573
Vivek Goyal25fb5162009-12-03 12:59:46 -05001574 cfqg = cfq_get_next_cfqg(cfqd);
1575 if (!cfqg)
1576 return NULL;
1577
Vivek Goyalf04a6422009-12-03 12:59:40 -05001578 for_each_cfqg_st(cfqg, i, j, st)
1579 if ((cfqq = cfq_rb_first(st)) != NULL)
1580 return cfqq;
1581 return NULL;
1582}
1583
Jens Axboe498d3aa22007-04-26 12:54:48 +02001584/*
1585 * Get and set a new active queue for service.
1586 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001587static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1588 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001589{
Jens Axboee00ef792009-11-04 08:54:55 +01001590 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001591 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001592
Jens Axboe22e2c502005-06-27 10:55:12 +02001593 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001594 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001595}
1596
Jens Axboed9e76202007-04-20 14:27:50 +02001597static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1598 struct request *rq)
1599{
Tejun Heo83096eb2009-05-07 22:24:39 +09001600 if (blk_rq_pos(rq) >= cfqd->last_position)
1601 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001602 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001603 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001604}
1605
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001606#define CFQQ_SEEK_THR 8 * 1024
1607#define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001608
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001609static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1610 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001611{
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001612 sector_t sdist = cfqq->seek_mean;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001613
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001614 if (!sample_valid(cfqq->seek_samples))
1615 sdist = CFQQ_SEEK_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001616
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001617 return cfq_dist_from_last(cfqd, rq) <= sdist;
Jens Axboe6d048f52007-04-25 12:44:27 +02001618}
1619
Jens Axboea36e71f2009-04-15 12:15:11 +02001620static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1621 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001622{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001623 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001624 struct rb_node *parent, *node;
1625 struct cfq_queue *__cfqq;
1626 sector_t sector = cfqd->last_position;
1627
1628 if (RB_EMPTY_ROOT(root))
1629 return NULL;
1630
1631 /*
1632 * First, if we find a request starting at the end of the last
1633 * request, choose it.
1634 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001635 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001636 if (__cfqq)
1637 return __cfqq;
1638
1639 /*
1640 * If the exact sector wasn't found, the parent of the NULL leaf
1641 * will contain the closest sector.
1642 */
1643 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001644 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001645 return __cfqq;
1646
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001647 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001648 node = rb_next(&__cfqq->p_node);
1649 else
1650 node = rb_prev(&__cfqq->p_node);
1651 if (!node)
1652 return NULL;
1653
1654 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001655 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001656 return __cfqq;
1657
1658 return NULL;
1659}
1660
1661/*
1662 * cfqd - obvious
1663 * cur_cfqq - passed in so that we don't decide that the current queue is
1664 * closely cooperating with itself.
1665 *
1666 * So, basically we're assuming that that cur_cfqq has dispatched at least
1667 * one request, and that cfqd->last_position reflects a position on the disk
1668 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1669 * assumption.
1670 */
1671static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001672 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001673{
1674 struct cfq_queue *cfqq;
1675
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001676 if (!cfq_cfqq_sync(cur_cfqq))
1677 return NULL;
1678 if (CFQQ_SEEKY(cur_cfqq))
1679 return NULL;
1680
Jens Axboea36e71f2009-04-15 12:15:11 +02001681 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001682 * We should notice if some of the queues are cooperating, eg
1683 * working closely on the same area of the disk. In that case,
1684 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001685 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001686 cfqq = cfqq_close(cfqd, cur_cfqq);
1687 if (!cfqq)
1688 return NULL;
1689
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001690 /*
1691 * It only makes sense to merge sync queues.
1692 */
1693 if (!cfq_cfqq_sync(cfqq))
1694 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001695 if (CFQQ_SEEKY(cfqq))
1696 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001697
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001698 /*
1699 * Do not merge queues of different priority classes
1700 */
1701 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1702 return NULL;
1703
Jens Axboea36e71f2009-04-15 12:15:11 +02001704 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001705}
1706
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001707/*
1708 * Determine whether we should enforce idle window for this queue.
1709 */
1710
1711static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1712{
1713 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001714 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001715
Vivek Goyalf04a6422009-12-03 12:59:40 -05001716 BUG_ON(!service_tree);
1717 BUG_ON(!service_tree->count);
1718
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001719 /* We never do for idle class queues. */
1720 if (prio == IDLE_WORKLOAD)
1721 return false;
1722
1723 /* We do for queues that were marked with idle window flag. */
1724 if (cfq_cfqq_idle_window(cfqq))
1725 return true;
1726
1727 /*
1728 * Otherwise, we do only if they are the last ones
1729 * in their service tree.
1730 */
Vivek Goyalf04a6422009-12-03 12:59:40 -05001731 return service_tree->count == 1;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001732}
1733
Jens Axboe6d048f52007-04-25 12:44:27 +02001734static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001735{
Jens Axboe17926692007-01-19 11:59:30 +11001736 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +02001737 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001738 unsigned long sl;
1739
Jens Axboea68bbdd2008-09-24 13:03:33 +02001740 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001741 * SSD device without seek penalty, disable idling. But only do so
1742 * for devices that support queuing, otherwise we still have a problem
1743 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001744 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001745 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001746 return;
1747
Jens Axboedd67d052006-06-21 09:36:18 +02001748 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001749 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001750
1751 /*
1752 * idle is disabled, either manually or by past process history
1753 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001754 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
Jens Axboe6d048f52007-04-25 12:44:27 +02001755 return;
1756
Jens Axboe22e2c502005-06-27 10:55:12 +02001757 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001758 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001759 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001760 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001761 return;
1762
1763 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001764 * task has exited, don't wait
1765 */
Jens Axboe206dc692006-03-28 13:03:44 +02001766 cic = cfqd->active_cic;
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01001767 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001768 return;
1769
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001770 /*
1771 * If our average think time is larger than the remaining time
1772 * slice, then don't idle. This avoids overrunning the allotted
1773 * time slice.
1774 */
1775 if (sample_valid(cic->ttime_samples) &&
1776 (cfqq->slice_end - jiffies < cic->ttime_mean))
1777 return;
1778
Jens Axboe3b181522005-06-27 10:56:24 +02001779 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001780
Jens Axboe6d048f52007-04-25 12:44:27 +02001781 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001782
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001783 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe9481ffd2009-04-15 12:14:13 +02001784 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Jens Axboe498d3aa22007-04-26 12:54:48 +02001787/*
1788 * Move request from internal lists to the request queue dispatch list.
1789 */
Jens Axboe165125e2007-07-24 09:28:11 +02001790static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001792 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001793 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001794
Jens Axboe7b679132008-05-30 12:23:07 +02001795 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1796
Jeff Moyer06d21882009-09-11 17:08:59 +02001797 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001798 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001799 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001800 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001801
1802 if (cfq_cfqq_sync(cfqq))
1803 cfqd->sync_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806/*
1807 * return expired entry, or NULL to just start from scratch in rbtree
1808 */
Jens Axboefebffd62008-01-28 13:19:43 +01001809static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Jens Axboe30996f42009-10-05 11:03:39 +02001811 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jens Axboe3b181522005-06-27 10:56:24 +02001813 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001815
1816 cfq_mark_cfqq_fifo_expire(cfqq);
1817
Jens Axboe89850f72006-07-22 16:48:31 +02001818 if (list_empty(&cfqq->fifo))
1819 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Jens Axboe89850f72006-07-22 16:48:31 +02001821 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001822 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001823 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Jens Axboe30996f42009-10-05 11:03:39 +02001825 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001826 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Jens Axboe22e2c502005-06-27 10:55:12 +02001829static inline int
1830cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1831{
1832 const int base_rq = cfqd->cfq_slice_async_rq;
1833
1834 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1835
1836 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1837}
1838
1839/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001840 * Must be called with the queue_lock held.
1841 */
1842static int cfqq_process_refs(struct cfq_queue *cfqq)
1843{
1844 int process_refs, io_refs;
1845
1846 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1847 process_refs = atomic_read(&cfqq->ref) - io_refs;
1848 BUG_ON(process_refs < 0);
1849 return process_refs;
1850}
1851
1852static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1853{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001854 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001855 struct cfq_queue *__cfqq;
1856
1857 /* Avoid a circular list and skip interim queue merges */
1858 while ((__cfqq = new_cfqq->new_cfqq)) {
1859 if (__cfqq == cfqq)
1860 return;
1861 new_cfqq = __cfqq;
1862 }
1863
1864 process_refs = cfqq_process_refs(cfqq);
1865 /*
1866 * If the process for the cfqq has gone away, there is no
1867 * sense in merging the queues.
1868 */
1869 if (process_refs == 0)
1870 return;
1871
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001872 /*
1873 * Merge in the direction of the lesser amount of work.
1874 */
1875 new_process_refs = cfqq_process_refs(new_cfqq);
1876 if (new_process_refs >= process_refs) {
1877 cfqq->new_cfqq = new_cfqq;
1878 atomic_add(process_refs, &new_cfqq->ref);
1879 } else {
1880 new_cfqq->new_cfqq = cfqq;
1881 atomic_add(new_process_refs, &cfqq->ref);
1882 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001883}
1884
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001885static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
1886 struct cfq_group *cfqg, enum wl_prio_t prio,
1887 bool prio_changed)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001888{
1889 struct cfq_queue *queue;
1890 int i;
1891 bool key_valid = false;
1892 unsigned long lowest_key = 0;
1893 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1894
1895 if (prio_changed) {
1896 /*
1897 * When priorities switched, we prefer starting
1898 * from SYNC_NOIDLE (first choice), or just SYNC
1899 * over ASYNC
1900 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001901 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001902 return cur_best;
1903 cur_best = SYNC_WORKLOAD;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001904 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001905 return cur_best;
1906
1907 return ASYNC_WORKLOAD;
1908 }
1909
1910 for (i = 0; i < 3; ++i) {
1911 /* otherwise, select the one with lowest rb_key */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001912 queue = cfq_rb_first(service_tree_for(cfqg, prio, i, cfqd));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001913 if (queue &&
1914 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1915 lowest_key = queue->rb_key;
1916 cur_best = i;
1917 key_valid = true;
1918 }
1919 }
1920
1921 return cur_best;
1922}
1923
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001924static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001925{
1926 enum wl_prio_t previous_prio = cfqd->serving_prio;
1927 bool prio_changed;
1928 unsigned slice;
1929 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001930 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001931 unsigned group_slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001932
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001933 if (!cfqg) {
1934 cfqd->serving_prio = IDLE_WORKLOAD;
1935 cfqd->workload_expires = jiffies + 1;
1936 return;
1937 }
1938
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001939 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001940 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001941 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001942 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001943 cfqd->serving_prio = BE_WORKLOAD;
1944 else {
1945 cfqd->serving_prio = IDLE_WORKLOAD;
1946 cfqd->workload_expires = jiffies + 1;
1947 return;
1948 }
1949
1950 /*
1951 * For RT and BE, we have to choose also the type
1952 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
1953 * expiration time
1954 */
1955 prio_changed = (cfqd->serving_prio != previous_prio);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001956 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
1957 cfqd);
1958 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001959
1960 /*
1961 * If priority didn't change, check workload expiration,
1962 * and that we still have other queues ready
1963 */
1964 if (!prio_changed && count &&
1965 !time_after(jiffies, cfqd->workload_expires))
1966 return;
1967
1968 /* otherwise select new workload type */
1969 cfqd->serving_type =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001970 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio, prio_changed);
1971 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
1972 cfqd);
1973 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001974
1975 /*
1976 * the workload slice is computed as a fraction of target latency
1977 * proportional to the number of queues in that workload, over
1978 * all the queues in the same priority class
1979 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001980 group_slice = cfq_group_slice(cfqd, cfqg);
1981
1982 slice = group_slice * count /
1983 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
1984 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001985
1986 if (cfqd->serving_type == ASYNC_WORKLOAD)
1987 /* async workload slice is scaled down according to
1988 * the sync/async slice ratio. */
1989 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
1990 else
1991 /* sync workload slice is at least 2 * cfq_slice_idle */
1992 slice = max(slice, 2 * cfqd->cfq_slice_idle);
1993
1994 slice = max_t(unsigned, slice, CFQ_MIN_TT);
1995 cfqd->workload_expires = jiffies + slice;
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001996 cfqd->noidle_tree_requires_idle = false;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001997}
1998
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001999static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2000{
2001 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002002 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002003
2004 if (RB_EMPTY_ROOT(&st->rb))
2005 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002006 cfqg = cfq_rb_first_group(st);
2007 st->active = &cfqg->rb_node;
2008 update_min_vdisktime(st);
2009 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002010}
2011
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002012static void cfq_choose_cfqg(struct cfq_data *cfqd)
2013{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002014 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2015
2016 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002017
2018 /* Restore the workload type data */
2019 if (cfqg->saved_workload_slice) {
2020 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2021 cfqd->serving_type = cfqg->saved_workload;
2022 cfqd->serving_prio = cfqg->saved_serving_prio;
2023 }
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002024 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002025}
2026
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002027/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002028 * Select a queue for service. If we have a current active queue,
2029 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002030 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002031static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002032{
Jens Axboea36e71f2009-04-15 12:15:11 +02002033 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002034
2035 cfqq = cfqd->active_queue;
2036 if (!cfqq)
2037 goto new_queue;
2038
Vivek Goyalf04a6422009-12-03 12:59:40 -05002039 if (!cfqd->rq_queued)
2040 return NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002041 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002042 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002043 */
Jens Axboeb0291952009-04-07 11:38:31 +02002044 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02002045 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02002046
2047 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002048 * The active queue has requests and isn't expired, allow it to
2049 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002050 */
Jens Axboedd67d052006-06-21 09:36:18 +02002051 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002052 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002053
2054 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002055 * If another queue has a request waiting within our mean seek
2056 * distance, let it run. The expire code will check for close
2057 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002058 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002059 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002060 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002061 if (new_cfqq) {
2062 if (!cfqq->new_cfqq)
2063 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002064 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002065 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002066
2067 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002068 * No requests pending. If the active queue still has requests in
2069 * flight or is idling for a new request, allow either of these
2070 * conditions to happen (or time out) before selecting a new queue.
2071 */
Jens Axboecc197472007-04-20 20:45:39 +02002072 if (timer_pending(&cfqd->idle_slice_timer) ||
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002073 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002074 cfqq = NULL;
2075 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002076 }
2077
Jens Axboe3b181522005-06-27 10:56:24 +02002078expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02002079 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002080new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002081 /*
2082 * Current queue expired. Check if we have to switch to a new
2083 * service tree
2084 */
2085 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002086 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002087
Jens Axboea36e71f2009-04-15 12:15:11 +02002088 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002089keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002090 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002091}
2092
Jens Axboefebffd62008-01-28 13:19:43 +01002093static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002094{
2095 int dispatched = 0;
2096
2097 while (cfqq->next_rq) {
2098 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2099 dispatched++;
2100 }
2101
2102 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002103
2104 /* By default cfqq is not expired if it is empty. Do it explicitly */
2105 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002106 return dispatched;
2107}
2108
Jens Axboe498d3aa22007-04-26 12:54:48 +02002109/*
2110 * Drain our current requests. Used for barriers and when switching
2111 * io schedulers on-the-fly.
2112 */
Jens Axboed9e76202007-04-20 14:27:50 +02002113static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002114{
Jens Axboe08717142008-01-28 11:38:15 +01002115 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002116 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002117
Vivek Goyalf04a6422009-12-03 12:59:40 -05002118 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL)
2119 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002120
Jens Axboe6084cdd2007-04-23 08:25:00 +02002121 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002122 BUG_ON(cfqd->busy_queues);
2123
Jeff Moyer69237152009-06-12 15:29:30 +02002124 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002125 return dispatched;
2126}
2127
Jens Axboe0b182d62009-10-06 20:49:37 +02002128static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002129{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002130 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Jens Axboe2f5cb732009-04-07 08:51:19 +02002132 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002133 * Drain async requests before we start sync IO
2134 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002135 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002136 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002137
2138 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002139 * If this is an async queue and we have sync IO in flight, let it wait
2140 */
2141 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002142 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002143
2144 max_dispatch = cfqd->cfq_quantum;
2145 if (cfq_class_idle(cfqq))
2146 max_dispatch = 1;
2147
2148 /*
2149 * Does this cfqq already have too much IO in flight?
2150 */
2151 if (cfqq->dispatched >= max_dispatch) {
2152 /*
2153 * idle queue must always only have a single IO in flight
2154 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002155 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002156 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002157
Jens Axboe2f5cb732009-04-07 08:51:19 +02002158 /*
2159 * We have other queues, don't allow more IO from this one
2160 */
2161 if (cfqd->busy_queues > 1)
Jens Axboe0b182d62009-10-06 20:49:37 +02002162 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002163
Jens Axboe2f5cb732009-04-07 08:51:19 +02002164 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002165 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002166 */
Shaohua Li474b18c2009-12-03 12:58:05 +01002167 max_dispatch = -1;
Jens Axboe8e296752009-10-03 16:26:03 +02002168 }
2169
2170 /*
2171 * Async queues must wait a bit before being allowed dispatch.
2172 * We also ramp up the dispatch depth gradually for async IO,
2173 * based on the last sync IO we serviced
2174 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002175 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jens Axboe8e296752009-10-03 16:26:03 +02002176 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
2177 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002178
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002179 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002180 if (!depth && !cfqq->dispatched)
2181 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002182 if (depth < max_dispatch)
2183 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185
Jens Axboe0b182d62009-10-06 20:49:37 +02002186 /*
2187 * If we're below the current max, allow a dispatch
2188 */
2189 return cfqq->dispatched < max_dispatch;
2190}
2191
2192/*
2193 * Dispatch a request from cfqq, moving them to the request queue
2194 * dispatch list.
2195 */
2196static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2197{
2198 struct request *rq;
2199
2200 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2201
2202 if (!cfq_may_dispatch(cfqd, cfqq))
2203 return false;
2204
2205 /*
2206 * follow expired path, else get first next available
2207 */
2208 rq = cfq_check_fifo(cfqq);
2209 if (!rq)
2210 rq = cfqq->next_rq;
2211
2212 /*
2213 * insert request into driver dispatch list
2214 */
2215 cfq_dispatch_insert(cfqd->queue, rq);
2216
2217 if (!cfqd->active_cic) {
2218 struct cfq_io_context *cic = RQ_CIC(rq);
2219
2220 atomic_long_inc(&cic->ioc->refcount);
2221 cfqd->active_cic = cic;
2222 }
2223
2224 return true;
2225}
2226
2227/*
2228 * Find the cfqq that we need to service and move a request from that to the
2229 * dispatch list
2230 */
2231static int cfq_dispatch_requests(struct request_queue *q, int force)
2232{
2233 struct cfq_data *cfqd = q->elevator->elevator_data;
2234 struct cfq_queue *cfqq;
2235
2236 if (!cfqd->busy_queues)
2237 return 0;
2238
2239 if (unlikely(force))
2240 return cfq_forced_dispatch(cfqd);
2241
2242 cfqq = cfq_select_queue(cfqd);
2243 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002244 return 0;
2245
Jens Axboe2f5cb732009-04-07 08:51:19 +02002246 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002247 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002248 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002249 if (!cfq_dispatch_request(cfqd, cfqq))
2250 return 0;
2251
Jens Axboe2f5cb732009-04-07 08:51:19 +02002252 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002253 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002254
2255 /*
2256 * expire an async queue immediately if it has used up its slice. idle
2257 * queue always expire after 1 dispatch round.
2258 */
2259 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2260 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2261 cfq_class_idle(cfqq))) {
2262 cfqq->slice_end = jiffies + 1;
2263 cfq_slice_expired(cfqd, 0);
2264 }
2265
Shan Weib217a902009-09-01 10:06:42 +02002266 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002267 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270/*
Jens Axboe5e705372006-07-13 12:39:25 +02002271 * task holds one reference to the queue, dropped when task exits. each rq
2272 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002274 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * queue lock must be held here.
2276 */
2277static void cfq_put_queue(struct cfq_queue *cfqq)
2278{
Jens Axboe22e2c502005-06-27 10:55:12 +02002279 struct cfq_data *cfqd = cfqq->cfqd;
Vivek Goyalb1c35762009-12-03 12:59:47 -05002280 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002281
2282 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 if (!atomic_dec_and_test(&cfqq->ref))
2285 return;
2286
Jens Axboe7b679132008-05-30 12:23:07 +02002287 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002289 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002290 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002292 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02002293 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002294 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002295 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002296
Vivek Goyalf04a6422009-12-03 12:59:40 -05002297 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 kmem_cache_free(cfq_pool, cfqq);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002299 cfq_put_cfqg(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Jens Axboed6de8be2008-05-28 14:46:59 +02002302/*
2303 * Must always be called with the rcu_read_lock() held
2304 */
Jens Axboe07416d22008-05-07 09:17:12 +02002305static void
2306__call_for_each_cic(struct io_context *ioc,
2307 void (*func)(struct io_context *, struct cfq_io_context *))
2308{
2309 struct cfq_io_context *cic;
2310 struct hlist_node *n;
2311
2312 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
2313 func(ioc, cic);
2314}
2315
Jens Axboe4ac845a2008-01-24 08:44:49 +01002316/*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002317 * Call func for each cic attached to this ioc.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002318 */
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002319static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002320call_for_each_cic(struct io_context *ioc,
2321 void (*func)(struct io_context *, struct cfq_io_context *))
2322{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002323 rcu_read_lock();
Jens Axboe07416d22008-05-07 09:17:12 +02002324 __call_for_each_cic(ioc, func);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002325 rcu_read_unlock();
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002326}
Jens Axboe4ac845a2008-01-24 08:44:49 +01002327
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002328static void cfq_cic_free_rcu(struct rcu_head *head)
2329{
2330 struct cfq_io_context *cic;
2331
2332 cic = container_of(head, struct cfq_io_context, rcu_head);
2333
2334 kmem_cache_free(cfq_ioc_pool, cic);
Tejun Heo245b2e72009-06-24 15:13:48 +09002335 elv_ioc_count_dec(cfq_ioc_count);
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002336
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002337 if (ioc_gone) {
2338 /*
2339 * CFQ scheduler is exiting, grab exit lock and check
2340 * the pending io context count. If it hits zero,
2341 * complete ioc_gone and set it back to NULL
2342 */
2343 spin_lock(&ioc_gone_lock);
Tejun Heo245b2e72009-06-24 15:13:48 +09002344 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002345 complete(ioc_gone);
2346 ioc_gone = NULL;
2347 }
2348 spin_unlock(&ioc_gone_lock);
2349 }
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002350}
2351
2352static void cfq_cic_free(struct cfq_io_context *cic)
2353{
2354 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002355}
2356
2357static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
2358{
2359 unsigned long flags;
2360
2361 BUG_ON(!cic->dead_key);
2362
2363 spin_lock_irqsave(&ioc->lock, flags);
2364 radix_tree_delete(&ioc->radix_root, cic->dead_key);
Jens Axboeffc4e752008-02-19 10:02:29 +01002365 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002366 spin_unlock_irqrestore(&ioc->lock, flags);
2367
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002368 cfq_cic_free(cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002369}
2370
Jens Axboed6de8be2008-05-28 14:46:59 +02002371/*
2372 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
2373 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
2374 * and ->trim() which is called with the task lock held
2375 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002376static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002378 /*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002379 * ioc->refcount is zero here, or we are called from elv_unregister(),
2380 * so no more cic's are allowed to be linked into this ioc. So it
2381 * should be ok to iterate over the known list, we will see all cic's
2382 * since no new ones are added.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002383 */
Jens Axboe07416d22008-05-07 09:17:12 +02002384 __call_for_each_cic(ioc, cic_free_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
Jens Axboe89850f72006-07-22 16:48:31 +02002387static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2388{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002389 struct cfq_queue *__cfqq, *next;
2390
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002391 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02002392 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002393 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002394 }
Jens Axboe89850f72006-07-22 16:48:31 +02002395
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002396 /*
2397 * If this queue was scheduled to merge with another queue, be
2398 * sure to drop the reference taken on that queue (and others in
2399 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2400 */
2401 __cfqq = cfqq->new_cfqq;
2402 while (__cfqq) {
2403 if (__cfqq == cfqq) {
2404 WARN(1, "cfqq->new_cfqq loop detected\n");
2405 break;
2406 }
2407 next = __cfqq->new_cfqq;
2408 cfq_put_queue(__cfqq);
2409 __cfqq = next;
2410 }
2411
Jens Axboe89850f72006-07-22 16:48:31 +02002412 cfq_put_queue(cfqq);
2413}
2414
2415static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
2416 struct cfq_io_context *cic)
2417{
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002418 struct io_context *ioc = cic->ioc;
2419
Jens Axboefc463792006-08-29 09:05:44 +02002420 list_del_init(&cic->queue_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002421
2422 /*
2423 * Make sure key == NULL is seen for dead queues
2424 */
Jens Axboefc463792006-08-29 09:05:44 +02002425 smp_wmb();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002426 cic->dead_key = (unsigned long) cic->key;
Jens Axboefc463792006-08-29 09:05:44 +02002427 cic->key = NULL;
2428
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002429 if (ioc->ioc_data == cic)
2430 rcu_assign_pointer(ioc->ioc_data, NULL);
2431
Jens Axboeff6657c2009-04-08 10:58:57 +02002432 if (cic->cfqq[BLK_RW_ASYNC]) {
2433 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2434 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002435 }
2436
Jens Axboeff6657c2009-04-08 10:58:57 +02002437 if (cic->cfqq[BLK_RW_SYNC]) {
2438 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2439 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002440 }
Jens Axboe89850f72006-07-22 16:48:31 +02002441}
2442
Jens Axboe4ac845a2008-01-24 08:44:49 +01002443static void cfq_exit_single_io_context(struct io_context *ioc,
2444 struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002445{
Al Viro478a82b2006-03-18 13:25:24 -05002446 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02002447
Jens Axboe89850f72006-07-22 16:48:31 +02002448 if (cfqd) {
Jens Axboe165125e2007-07-24 09:28:11 +02002449 struct request_queue *q = cfqd->queue;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002450 unsigned long flags;
Jens Axboe22e2c502005-06-27 10:55:12 +02002451
Jens Axboe4ac845a2008-01-24 08:44:49 +01002452 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe62c1fe92008-12-15 21:19:25 +01002453
2454 /*
2455 * Ensure we get a fresh copy of the ->key to prevent
2456 * race between exiting task and queue
2457 */
2458 smp_read_barrier_depends();
2459 if (cic->key)
2460 __cfq_exit_single_io_context(cfqd, cic);
2461
Jens Axboe4ac845a2008-01-24 08:44:49 +01002462 spin_unlock_irqrestore(q->queue_lock, flags);
Al Viro12a05732006-03-18 13:38:01 -05002463 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002464}
2465
Jens Axboe498d3aa22007-04-26 12:54:48 +02002466/*
2467 * The process that ioc belongs to has exited, we need to clean up
2468 * and put the internal structures we have that belongs to that process.
2469 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002470static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002472 call_for_each_cic(ioc, cfq_exit_single_io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Jens Axboe22e2c502005-06-27 10:55:12 +02002475static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04002476cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Jens Axboeb5deef92006-07-19 23:39:40 +02002478 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Christoph Lameter94f60302007-07-17 04:03:29 -07002480 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
2481 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002483 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02002484 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboeffc4e752008-02-19 10:02:29 +01002485 INIT_HLIST_NODE(&cic->cic_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02002486 cic->dtor = cfq_free_io_context;
2487 cic->exit = cfq_exit_io_context;
Tejun Heo245b2e72009-06-24 15:13:48 +09002488 elv_ioc_count_inc(cfq_ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
2491 return cic;
2492}
2493
Jens Axboefd0928d2008-01-24 08:52:45 +01002494static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002495{
2496 struct task_struct *tsk = current;
2497 int ioprio_class;
2498
Jens Axboe3b181522005-06-27 10:56:24 +02002499 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002500 return;
2501
Jens Axboefd0928d2008-01-24 08:52:45 +01002502 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002503 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002504 default:
2505 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2506 case IOPRIO_CLASS_NONE:
2507 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002508 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002509 */
2510 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002511 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002512 break;
2513 case IOPRIO_CLASS_RT:
2514 cfqq->ioprio = task_ioprio(ioc);
2515 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2516 break;
2517 case IOPRIO_CLASS_BE:
2518 cfqq->ioprio = task_ioprio(ioc);
2519 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2520 break;
2521 case IOPRIO_CLASS_IDLE:
2522 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2523 cfqq->ioprio = 7;
2524 cfq_clear_cfqq_idle_window(cfqq);
2525 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002526 }
2527
2528 /*
2529 * keep track of original prio settings in case we have to temporarily
2530 * elevate the priority of this queue
2531 */
2532 cfqq->org_ioprio = cfqq->ioprio;
2533 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02002534 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002535}
2536
Jens Axboefebffd62008-01-28 13:19:43 +01002537static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002538{
Al Viro478a82b2006-03-18 13:25:24 -05002539 struct cfq_data *cfqd = cic->key;
2540 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01002541 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02002542
Jens Axboecaaa5f92006-06-16 11:23:00 +02002543 if (unlikely(!cfqd))
2544 return;
2545
Jens Axboec1b707d2006-10-30 19:54:23 +01002546 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002547
Jens Axboeff6657c2009-04-08 10:58:57 +02002548 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002549 if (cfqq) {
2550 struct cfq_queue *new_cfqq;
Jens Axboeff6657c2009-04-08 10:58:57 +02002551 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2552 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002553 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002554 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002555 cfq_put_queue(cfqq);
2556 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002557 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002558
Jens Axboeff6657c2009-04-08 10:58:57 +02002559 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002560 if (cfqq)
2561 cfq_mark_cfqq_prio_changed(cfqq);
2562
Jens Axboec1b707d2006-10-30 19:54:23 +01002563 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02002564}
2565
Jens Axboefc463792006-08-29 09:05:44 +02002566static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002568 call_for_each_cic(ioc, changed_ioprio);
Jens Axboefc463792006-08-29 09:05:44 +02002569 ioc->ioprio_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Jens Axboed5036d72009-06-26 10:44:34 +02002572static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002573 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002574{
2575 RB_CLEAR_NODE(&cfqq->rb_node);
2576 RB_CLEAR_NODE(&cfqq->p_node);
2577 INIT_LIST_HEAD(&cfqq->fifo);
2578
2579 atomic_set(&cfqq->ref, 0);
2580 cfqq->cfqd = cfqd;
2581
2582 cfq_mark_cfqq_prio_changed(cfqq);
2583
2584 if (is_sync) {
2585 if (!cfq_class_idle(cfqq))
2586 cfq_mark_cfqq_idle_window(cfqq);
2587 cfq_mark_cfqq_sync(cfqq);
2588 }
2589 cfqq->pid = pid;
2590}
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002593cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002594 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002597 struct cfq_io_context *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002598 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600retry:
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002601 cfqg = cfq_get_cfqg(cfqd, 1);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002602 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002603 /* cic always exists here */
2604 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Jens Axboe6118b702009-06-30 09:34:12 +02002606 /*
2607 * Always try a new alloc if we fell back to the OOM cfqq
2608 * originally, since it should just be a temporary situation.
2609 */
2610 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2611 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (new_cfqq) {
2613 cfqq = new_cfqq;
2614 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002615 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002617 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002618 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002619 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002621 if (new_cfqq)
2622 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002623 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002624 cfqq = kmem_cache_alloc_node(cfq_pool,
2625 gfp_mask | __GFP_ZERO,
2626 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Jens Axboe6118b702009-06-30 09:34:12 +02002629 if (cfqq) {
2630 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2631 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002632 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002633 cfq_log_cfqq(cfqd, cfqq, "alloced");
2634 } else
2635 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637
2638 if (new_cfqq)
2639 kmem_cache_free(cfq_pool, new_cfqq);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return cfqq;
2642}
2643
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002644static struct cfq_queue **
2645cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2646{
Jens Axboefe094d92008-01-31 13:08:54 +01002647 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002648 case IOPRIO_CLASS_RT:
2649 return &cfqd->async_cfqq[0][ioprio];
2650 case IOPRIO_CLASS_BE:
2651 return &cfqd->async_cfqq[1][ioprio];
2652 case IOPRIO_CLASS_IDLE:
2653 return &cfqd->async_idle_cfqq;
2654 default:
2655 BUG();
2656 }
2657}
2658
Jens Axboe15c31be2007-07-10 13:43:25 +02002659static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002660cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002661 gfp_t gfp_mask)
2662{
Jens Axboefd0928d2008-01-24 08:52:45 +01002663 const int ioprio = task_ioprio(ioc);
2664 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002665 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002666 struct cfq_queue *cfqq = NULL;
2667
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002668 if (!is_sync) {
2669 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2670 cfqq = *async_cfqq;
2671 }
2672
Jens Axboe6118b702009-06-30 09:34:12 +02002673 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002674 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002675
2676 /*
2677 * pin the queue now that it's allocated, scheduler exit will prune it
2678 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002679 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02002680 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002681 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002682 }
2683
2684 atomic_inc(&cfqq->ref);
2685 return cfqq;
2686}
2687
Jens Axboe498d3aa22007-04-26 12:54:48 +02002688/*
2689 * We drop cfq io contexts lazily, so we may find a dead one.
2690 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002691static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002692cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2693 struct cfq_io_context *cic)
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002694{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002695 unsigned long flags;
2696
Jens Axboefc463792006-08-29 09:05:44 +02002697 WARN_ON(!list_empty(&cic->queue_list));
Jens Axboe597bc482007-04-24 21:23:53 +02002698
Jens Axboe4ac845a2008-01-24 08:44:49 +01002699 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe597bc482007-04-24 21:23:53 +02002700
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002701 BUG_ON(ioc->ioc_data == cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002702
2703 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
Jens Axboeffc4e752008-02-19 10:02:29 +01002704 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002705 spin_unlock_irqrestore(&ioc->lock, flags);
2706
2707 cfq_cic_free(cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002708}
2709
Jens Axboee2d74ac2006-03-28 08:59:01 +02002710static struct cfq_io_context *
Jens Axboe4ac845a2008-01-24 08:44:49 +01002711cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002712{
Jens Axboee2d74ac2006-03-28 08:59:01 +02002713 struct cfq_io_context *cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002714 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002715 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002716
Vasily Tarasov91fac312007-04-25 12:29:51 +02002717 if (unlikely(!ioc))
2718 return NULL;
2719
Jens Axboed6de8be2008-05-28 14:46:59 +02002720 rcu_read_lock();
2721
Jens Axboe597bc482007-04-24 21:23:53 +02002722 /*
2723 * we maintain a last-hit cache, to avoid browsing over the tree
2724 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002725 cic = rcu_dereference(ioc->ioc_data);
Jens Axboed6de8be2008-05-28 14:46:59 +02002726 if (cic && cic->key == cfqd) {
2727 rcu_read_unlock();
Jens Axboe597bc482007-04-24 21:23:53 +02002728 return cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002729 }
Jens Axboe597bc482007-04-24 21:23:53 +02002730
Jens Axboe4ac845a2008-01-24 08:44:49 +01002731 do {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002732 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
2733 rcu_read_unlock();
2734 if (!cic)
2735 break;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02002736 /* ->key must be copied to avoid race with cfq_exit_queue() */
2737 k = cic->key;
2738 if (unlikely(!k)) {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002739 cfq_drop_dead_cic(cfqd, ioc, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002740 rcu_read_lock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002741 continue;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002742 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002743
Jens Axboed6de8be2008-05-28 14:46:59 +02002744 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002745 rcu_assign_pointer(ioc->ioc_data, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002746 spin_unlock_irqrestore(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002747 break;
2748 } while (1);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002749
Jens Axboe4ac845a2008-01-24 08:44:49 +01002750 return cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002751}
2752
Jens Axboe4ac845a2008-01-24 08:44:49 +01002753/*
2754 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2755 * the process specific cfq io context when entered from the block layer.
2756 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2757 */
Jens Axboefebffd62008-01-28 13:19:43 +01002758static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2759 struct cfq_io_context *cic, gfp_t gfp_mask)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002760{
Jens Axboe0261d682006-10-30 19:07:48 +01002761 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002762 int ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002763
Jens Axboe4ac845a2008-01-24 08:44:49 +01002764 ret = radix_tree_preload(gfp_mask);
2765 if (!ret) {
2766 cic->ioc = ioc;
2767 cic->key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002768
Jens Axboe4ac845a2008-01-24 08:44:49 +01002769 spin_lock_irqsave(&ioc->lock, flags);
2770 ret = radix_tree_insert(&ioc->radix_root,
2771 (unsigned long) cfqd, cic);
Jens Axboeffc4e752008-02-19 10:02:29 +01002772 if (!ret)
2773 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002774 spin_unlock_irqrestore(&ioc->lock, flags);
2775
2776 radix_tree_preload_end();
2777
2778 if (!ret) {
2779 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2780 list_add(&cic->queue_list, &cfqd->cic_list);
2781 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002782 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002783 }
2784
Jens Axboe4ac845a2008-01-24 08:44:49 +01002785 if (ret)
2786 printk(KERN_ERR "cfq: cic link failed!\n");
Jens Axboefc463792006-08-29 09:05:44 +02002787
Jens Axboe4ac845a2008-01-24 08:44:49 +01002788 return ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002789}
2790
Jens Axboe22e2c502005-06-27 10:55:12 +02002791/*
2792 * Setup general io context and cfq io context. There can be several cfq
2793 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02002794 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02002795 */
2796static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02002797cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Jens Axboe22e2c502005-06-27 10:55:12 +02002799 struct io_context *ioc = NULL;
2800 struct cfq_io_context *cic;
2801
2802 might_sleep_if(gfp_mask & __GFP_WAIT);
2803
Jens Axboeb5deef92006-07-19 23:39:40 +02002804 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02002805 if (!ioc)
2806 return NULL;
2807
Jens Axboe4ac845a2008-01-24 08:44:49 +01002808 cic = cfq_cic_lookup(cfqd, ioc);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002809 if (cic)
2810 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02002811
Jens Axboee2d74ac2006-03-28 08:59:01 +02002812 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2813 if (cic == NULL)
2814 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02002815
Jens Axboe4ac845a2008-01-24 08:44:49 +01002816 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2817 goto err_free;
2818
Jens Axboe22e2c502005-06-27 10:55:12 +02002819out:
Jens Axboefc463792006-08-29 09:05:44 +02002820 smp_read_barrier_depends();
2821 if (unlikely(ioc->ioprio_changed))
2822 cfq_ioc_set_ioprio(ioc);
2823
Jens Axboe22e2c502005-06-27 10:55:12 +02002824 return cic;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002825err_free:
2826 cfq_cic_free(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02002827err:
2828 put_io_context(ioc);
2829 return NULL;
2830}
2831
2832static void
2833cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2834{
Jens Axboeaaf12282007-01-19 11:30:16 +11002835 unsigned long elapsed = jiffies - cic->last_end_request;
2836 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002837
2838 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2839 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2840 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2841}
2842
Jens Axboe206dc692006-03-28 13:03:44 +02002843static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002844cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002845 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002846{
2847 sector_t sdist;
2848 u64 total;
2849
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002850 if (!cfqq->last_request_pos)
Jeff Moyer4d00aa42009-04-21 07:25:04 +02002851 sdist = 0;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002852 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2853 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02002854 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002855 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
Jens Axboe206dc692006-03-28 13:03:44 +02002856
2857 /*
2858 * Don't allow the seek distance to get too large from the
2859 * odd fragment, pagein, etc
2860 */
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002861 if (cfqq->seek_samples <= 60) /* second&third seek */
2862 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
Jens Axboe206dc692006-03-28 13:03:44 +02002863 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002864 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
Jens Axboe206dc692006-03-28 13:03:44 +02002865
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002866 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2867 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2868 total = cfqq->seek_total + (cfqq->seek_samples/2);
2869 do_div(total, cfqq->seek_samples);
2870 cfqq->seek_mean = (sector_t)total;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002871
2872 /*
2873 * If this cfqq is shared between multiple processes, check to
2874 * make sure that those processes are still issuing I/Os within
2875 * the mean seek distance. If not, it may be time to break the
2876 * queues apart again.
2877 */
2878 if (cfq_cfqq_coop(cfqq)) {
2879 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2880 cfqq->seeky_start = jiffies;
2881 else if (!CFQQ_SEEKY(cfqq))
2882 cfqq->seeky_start = 0;
2883 }
Jens Axboe206dc692006-03-28 13:03:44 +02002884}
Jens Axboe22e2c502005-06-27 10:55:12 +02002885
2886/*
2887 * Disable idle window if the process thinks too long or seeks so much that
2888 * it doesn't matter
2889 */
2890static void
2891cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2892 struct cfq_io_context *cic)
2893{
Jens Axboe7b679132008-05-30 12:23:07 +02002894 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002895
Jens Axboe08717142008-01-28 11:38:15 +01002896 /*
2897 * Don't idle for async or idle io prio class
2898 */
2899 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002900 return;
2901
Jens Axboec265a7f2008-06-26 13:49:33 +02002902 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002903
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002904 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2905 cfq_mark_cfqq_deep(cfqq);
2906
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01002907 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002908 (!cfq_cfqq_deep(cfqq) && sample_valid(cfqq->seek_samples)
2909 && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002910 enable_idle = 0;
2911 else if (sample_valid(cic->ttime_samples)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002912 if (cic->ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002913 enable_idle = 0;
2914 else
2915 enable_idle = 1;
2916 }
2917
Jens Axboe7b679132008-05-30 12:23:07 +02002918 if (old_idle != enable_idle) {
2919 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2920 if (enable_idle)
2921 cfq_mark_cfqq_idle_window(cfqq);
2922 else
2923 cfq_clear_cfqq_idle_window(cfqq);
2924 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002925}
2926
Jens Axboe22e2c502005-06-27 10:55:12 +02002927/*
2928 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2929 * no or if we aren't sure, a 1 will cause a preempt.
2930 */
Jens Axboea6151c32009-10-07 20:02:57 +02002931static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002932cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02002933 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002934{
Jens Axboe6d048f52007-04-25 12:44:27 +02002935 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002936
Jens Axboe6d048f52007-04-25 12:44:27 +02002937 cfqq = cfqd->active_queue;
2938 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02002939 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002940
Jens Axboe6d048f52007-04-25 12:44:27 +02002941 if (cfq_slice_used(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002942 return true;
Jens Axboe6d048f52007-04-25 12:44:27 +02002943
2944 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002945 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002946
2947 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002948 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002949
Vivek Goyalf04a6422009-12-03 12:59:40 -05002950 /* Allow preemption only if we are idling on sync-noidle tree */
Corrado Zoccoloe4a22912009-11-26 10:02:58 +01002951 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2952 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyalf04a6422009-12-03 12:59:40 -05002953 new_cfqq->service_tree->count == 2 &&
2954 RB_EMPTY_ROOT(&cfqq->sort_list))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002955 return true;
2956
Jens Axboe22e2c502005-06-27 10:55:12 +02002957 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02002958 * if the new request is sync, but the currently running queue is
2959 * not, let the sync request have priority.
2960 */
Jens Axboe5e705372006-07-13 12:39:25 +02002961 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002962 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002963
Jens Axboe374f84a2006-07-23 01:42:19 +02002964 /*
2965 * So both queues are sync. Let the new request get disk time if
2966 * it's a metadata request and the current queue is doing regular IO.
2967 */
2968 if (rq_is_meta(rq) && !cfqq->meta_pending)
Jens Axboee6ec4fe2009-11-03 20:21:35 +01002969 return true;
Jens Axboe22e2c502005-06-27 10:55:12 +02002970
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002971 /*
2972 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2973 */
2974 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002975 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002976
Jens Axboe1e3335d2007-02-14 19:59:49 +01002977 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002978 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002979
2980 /*
2981 * if this request is as-good as one we would expect from the
2982 * current cfqq, let it preempt
2983 */
Jens Axboee00ef792009-11-04 08:54:55 +01002984 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002985 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002986
Jens Axboea6151c32009-10-07 20:02:57 +02002987 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002988}
2989
2990/*
2991 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2992 * let it have half of its nominal slice.
2993 */
2994static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2995{
Jens Axboe7b679132008-05-30 12:23:07 +02002996 cfq_log_cfqq(cfqd, cfqq, "preempt");
Jens Axboe6084cdd2007-04-23 08:25:00 +02002997 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02002998
Jens Axboebf572252006-07-19 20:29:12 +02002999 /*
3000 * Put the new queue at the front of the of the current list,
3001 * so we know that it will be selected next.
3002 */
3003 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003004
3005 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02003006
Jens Axboe44f7c162007-01-19 11:51:58 +11003007 cfqq->slice_end = 0;
3008 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003009}
3010
3011/*
Jens Axboe5e705372006-07-13 12:39:25 +02003012 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003013 * something we should do about it
3014 */
3015static void
Jens Axboe5e705372006-07-13 12:39:25 +02003016cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3017 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003018{
Jens Axboe5e705372006-07-13 12:39:25 +02003019 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003020
Aaron Carroll45333d52008-08-26 15:52:36 +02003021 cfqd->rq_queued++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003022 if (rq_is_meta(rq))
3023 cfqq->meta_pending++;
3024
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003025 cfq_update_io_thinktime(cfqd, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003026 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003027 cfq_update_idle_window(cfqd, cfqq, cic);
3028
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003029 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003030
3031 if (cfqq == cfqd->active_queue) {
3032 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003033 * Remember that we saw a request from this process, but
3034 * don't start queuing just yet. Otherwise we risk seeing lots
3035 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003036 * and merging. If the request is already larger than a single
3037 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003038 * merging is already done. Ditto for a busy system that
3039 * has other work pending, don't risk delaying until the
3040 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003041 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003042 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003043 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3044 cfqd->busy_queues > 1) {
Jens Axboed6ceb252009-04-14 14:18:16 +02003045 del_timer(&cfqd->idle_slice_timer);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003046 __blk_run_queue(cfqd->queue);
3047 } else
3048 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboed6ceb252009-04-14 14:18:16 +02003049 }
Jens Axboe5e705372006-07-13 12:39:25 +02003050 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003051 /*
3052 * not the active queue - expire current slice if it is
3053 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003054 * has some old slice time left and is of higher priority or
3055 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003056 */
3057 cfq_preempt_queue(cfqd, cfqq);
Tejun Heoa7f55792009-04-23 11:05:17 +09003058 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003059 }
3060}
3061
Jens Axboe165125e2007-07-24 09:28:11 +02003062static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003063{
Jens Axboeb4878f22005-10-20 16:42:29 +02003064 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003065 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003066
Jens Axboe7b679132008-05-30 12:23:07 +02003067 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Jens Axboefd0928d2008-01-24 08:52:45 +01003068 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Jens Axboe30996f42009-10-05 11:03:39 +02003070 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003071 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003072 cfq_add_rq_rb(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003073
Jens Axboe5e705372006-07-13 12:39:25 +02003074 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
Aaron Carroll45333d52008-08-26 15:52:36 +02003077/*
3078 * Update hw_tag based on peak queue depth over 50 samples under
3079 * sufficient load.
3080 */
3081static void cfq_update_hw_tag(struct cfq_data *cfqd)
3082{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003083 struct cfq_queue *cfqq = cfqd->active_queue;
3084
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003085 if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
3086 cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
3087
3088 if (cfqd->hw_tag == 1)
3089 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003090
3091 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Jens Axboe5ad531d2009-07-03 12:57:48 +02003092 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003093 return;
3094
Shaohua Li1a1238a2009-10-27 08:46:23 +01003095 /*
3096 * If active queue hasn't enough requests and can idle, cfq might not
3097 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3098 * case
3099 */
3100 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3101 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
3102 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
3103 return;
3104
Aaron Carroll45333d52008-08-26 15:52:36 +02003105 if (cfqd->hw_tag_samples++ < 50)
3106 return;
3107
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003108 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003109 cfqd->hw_tag = 1;
3110 else
3111 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003112}
3113
Jens Axboe165125e2007-07-24 09:28:11 +02003114static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Jens Axboe5e705372006-07-13 12:39:25 +02003116 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003117 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003118 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003119 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Jens Axboeb4878f22005-10-20 16:42:29 +02003121 now = jiffies;
Vivek Goyal2868ef72009-12-03 12:59:48 -05003122 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", !!rq_noidle(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Aaron Carroll45333d52008-08-26 15:52:36 +02003124 cfq_update_hw_tag(cfqd);
3125
Jens Axboe5ad531d2009-07-03 12:57:48 +02003126 WARN_ON(!cfqd->rq_in_driver[sync]);
Jens Axboe6d048f52007-04-25 12:44:27 +02003127 WARN_ON(!cfqq->dispatched);
Jens Axboe5ad531d2009-07-03 12:57:48 +02003128 cfqd->rq_in_driver[sync]--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003129 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Jens Axboe3ed9a292007-04-23 08:33:33 +02003131 if (cfq_cfqq_sync(cfqq))
3132 cfqd->sync_flight--;
3133
Vivek Goyal365722b2009-10-03 15:21:27 +02003134 if (sync) {
Jens Axboe5e705372006-07-13 12:39:25 +02003135 RQ_CIC(rq)->last_end_request = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003136 cfqd->last_end_sync_rq = now;
3137 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003138
3139 /*
3140 * If this is the active queue, check if it needs to be expired,
3141 * or if we want to idle in case it has no pending requests.
3142 */
3143 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003144 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3145
Jens Axboe44f7c162007-01-19 11:51:58 +11003146 if (cfq_cfqq_slice_new(cfqq)) {
3147 cfq_set_prio_slice(cfqd, cfqq);
3148 cfq_clear_cfqq_slice_new(cfqq);
3149 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003150 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003151 * Idling is not enabled on:
3152 * - expired queues
3153 * - idle-priority queues
3154 * - async queues
3155 * - queues with still some requests queued
3156 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003157 */
Jens Axboe08717142008-01-28 11:38:15 +01003158 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02003159 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003160 else if (sync && cfqq_empty &&
3161 !cfq_close_cooperator(cfqd, cfqq)) {
3162 cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
3163 /*
3164 * Idling is enabled for SYNC_WORKLOAD.
3165 * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
3166 * only if we processed at least one !rq_noidle request
3167 */
3168 if (cfqd->serving_type == SYNC_WORKLOAD
3169 || cfqd->noidle_tree_requires_idle)
3170 cfq_arm_slice_timer(cfqd);
3171 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003172 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003173
Jens Axboe5ad531d2009-07-03 12:57:48 +02003174 if (!rq_in_driver(cfqd))
Jens Axboe23e018a2009-10-05 08:52:35 +02003175 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
Jens Axboe22e2c502005-06-27 10:55:12 +02003178/*
3179 * we temporarily boost lower priority queues if they are holding fs exclusive
3180 * resources. they are boosted to normal prio (CLASS_BE/4)
3181 */
3182static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Jens Axboe22e2c502005-06-27 10:55:12 +02003184 if (has_fs_excl()) {
3185 /*
3186 * boost idle prio on transactions that would lock out other
3187 * users of the filesystem
3188 */
3189 if (cfq_class_idle(cfqq))
3190 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3191 if (cfqq->ioprio > IOPRIO_NORM)
3192 cfqq->ioprio = IOPRIO_NORM;
3193 } else {
3194 /*
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003195 * unboost the queue (if needed)
Jens Axboe22e2c502005-06-27 10:55:12 +02003196 */
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003197 cfqq->ioprio_class = cfqq->org_ioprio_class;
3198 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003200}
3201
Jens Axboe89850f72006-07-22 16:48:31 +02003202static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003203{
Jens Axboe1b379d82009-08-11 08:26:11 +02003204 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003205 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003206 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003207 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003208
3209 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003210}
3211
Jens Axboe165125e2007-07-24 09:28:11 +02003212static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003213{
3214 struct cfq_data *cfqd = q->elevator->elevator_data;
3215 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02003216 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003217 struct cfq_queue *cfqq;
3218
3219 /*
3220 * don't force setup of a queue from here, as a call to may_queue
3221 * does not necessarily imply that a request actually will be queued.
3222 * so just lookup a possibly existing queue, or return 'may queue'
3223 * if that fails
3224 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003225 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003226 if (!cic)
3227 return ELV_MQUEUE_MAY;
3228
Jens Axboeb0b78f82009-04-08 10:56:08 +02003229 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003230 if (cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003231 cfq_init_prio_data(cfqq, cic->ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003232 cfq_prio_boost(cfqq);
3233
Jens Axboe89850f72006-07-22 16:48:31 +02003234 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003235 }
3236
3237 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240/*
3241 * queue lock held here
3242 */
Jens Axboebb37b942006-12-01 10:42:33 +01003243static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Jens Axboe5e705372006-07-13 12:39:25 +02003245 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Jens Axboe5e705372006-07-13 12:39:25 +02003247 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003248 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Jens Axboe22e2c502005-06-27 10:55:12 +02003250 BUG_ON(!cfqq->allocated[rw]);
3251 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Jens Axboe5e705372006-07-13 12:39:25 +02003253 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02003256 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 cfq_put_queue(cfqq);
3259 }
3260}
3261
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003262static struct cfq_queue *
3263cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
3264 struct cfq_queue *cfqq)
3265{
3266 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3267 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003268 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003269 cfq_put_queue(cfqq);
3270 return cic_to_cfqq(cic, 1);
3271}
3272
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003273static int should_split_cfqq(struct cfq_queue *cfqq)
3274{
3275 if (cfqq->seeky_start &&
3276 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
3277 return 1;
3278 return 0;
3279}
3280
3281/*
3282 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3283 * was the last process referring to said cfqq.
3284 */
3285static struct cfq_queue *
3286split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
3287{
3288 if (cfqq_process_refs(cfqq) == 1) {
3289 cfqq->seeky_start = 0;
3290 cfqq->pid = current->pid;
3291 cfq_clear_cfqq_coop(cfqq);
3292 return cfqq;
3293 }
3294
3295 cic_set_cfqq(cic, NULL, 1);
3296 cfq_put_queue(cfqq);
3297 return NULL;
3298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003300 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003302static int
Jens Axboe165125e2007-07-24 09:28:11 +02003303cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 struct cfq_data *cfqd = q->elevator->elevator_data;
3306 struct cfq_io_context *cic;
3307 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003308 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003309 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 unsigned long flags;
3311
3312 might_sleep_if(gfp_mask & __GFP_WAIT);
3313
Jens Axboee2d74ac2006-03-28 08:59:01 +02003314 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 spin_lock_irqsave(q->queue_lock, flags);
3317
Jens Axboe22e2c502005-06-27 10:55:12 +02003318 if (!cic)
3319 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003321new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003322 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003323 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003324 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003325 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003326 } else {
3327 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003328 * If the queue was seeky for too long, break it apart.
3329 */
3330 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
3331 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3332 cfqq = split_cfqq(cic, cfqq);
3333 if (!cfqq)
3334 goto new_queue;
3335 }
3336
3337 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003338 * Check to see if this queue is scheduled to merge with
3339 * another, closely cooperating queue. The merging of
3340 * queues happens here as it must be done in process context.
3341 * The reference on new_cfqq was taken in merge_cfqqs.
3342 */
3343 if (cfqq->new_cfqq)
3344 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
3347 cfqq->allocated[rw]++;
Jens Axboe22e2c502005-06-27 10:55:12 +02003348 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 spin_unlock_irqrestore(q->queue_lock, flags);
3351
Jens Axboe5e705372006-07-13 12:39:25 +02003352 rq->elevator_private = cic;
3353 rq->elevator_private2 = cfqq;
3354 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02003355
Jens Axboe22e2c502005-06-27 10:55:12 +02003356queue_fail:
3357 if (cic)
3358 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02003359
Jens Axboe23e018a2009-10-05 08:52:35 +02003360 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe7b679132008-05-30 12:23:07 +02003362 cfq_log(cfqd, "set_request fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return 1;
3364}
3365
David Howells65f27f32006-11-22 14:55:48 +00003366static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003367{
David Howells65f27f32006-11-22 14:55:48 +00003368 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003369 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003370 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003371
Jens Axboe40bb54d2009-04-15 12:11:10 +02003372 spin_lock_irq(q->queue_lock);
Tejun Heoa7f55792009-04-23 11:05:17 +09003373 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003374 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003375}
3376
3377/*
3378 * Timer running if the active_queue is currently idling inside its time slice
3379 */
3380static void cfq_idle_slice_timer(unsigned long data)
3381{
3382 struct cfq_data *cfqd = (struct cfq_data *) data;
3383 struct cfq_queue *cfqq;
3384 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003385 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003386
Jens Axboe7b679132008-05-30 12:23:07 +02003387 cfq_log(cfqd, "idle timer fired");
3388
Jens Axboe22e2c502005-06-27 10:55:12 +02003389 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3390
Jens Axboefe094d92008-01-31 13:08:54 +01003391 cfqq = cfqd->active_queue;
3392 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003393 timed_out = 0;
3394
Jens Axboe22e2c502005-06-27 10:55:12 +02003395 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003396 * We saw a request before the queue expired, let it through
3397 */
3398 if (cfq_cfqq_must_dispatch(cfqq))
3399 goto out_kick;
3400
3401 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003402 * expired
3403 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003404 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003405 goto expire;
3406
3407 /*
3408 * only expire and reinvoke request handler, if there are
3409 * other queues with pending requests
3410 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003411 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003412 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003413
3414 /*
3415 * not expired and it has a request pending, let it dispatch
3416 */
Jens Axboe75e50982009-04-07 08:56:14 +02003417 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003418 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003419
3420 /*
3421 * Queue depth flag is reset only when the idle didn't succeed
3422 */
3423 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003424 }
3425expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02003426 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003427out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003428 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003429out_cont:
3430 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3431}
3432
Jens Axboe3b181522005-06-27 10:56:24 +02003433static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3434{
3435 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003436 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003437}
Jens Axboe22e2c502005-06-27 10:55:12 +02003438
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003439static void cfq_put_async_queues(struct cfq_data *cfqd)
3440{
3441 int i;
3442
3443 for (i = 0; i < IOPRIO_BE_NR; i++) {
3444 if (cfqd->async_cfqq[0][i])
3445 cfq_put_queue(cfqd->async_cfqq[0][i]);
3446 if (cfqd->async_cfqq[1][i])
3447 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003448 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003449
3450 if (cfqd->async_idle_cfqq)
3451 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003452}
3453
Jens Axboeb374d182008-10-31 10:05:07 +01003454static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Jens Axboe22e2c502005-06-27 10:55:12 +02003456 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003457 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003458
Jens Axboe3b181522005-06-27 10:56:24 +02003459 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003460
Al Virod9ff4182006-03-18 13:51:22 -05003461 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003462
Al Virod9ff4182006-03-18 13:51:22 -05003463 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02003464 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003465
3466 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05003467 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
3468 struct cfq_io_context,
3469 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02003470
3471 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05003472 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02003473
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003474 cfq_put_async_queues(cfqd);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003475 cfq_release_cfq_groups(cfqd);
3476 blkiocg_del_blkio_group(&cfqd->root_group.blkg);
Jens Axboe15c31be2007-07-10 13:43:25 +02003477
Al Virod9ff4182006-03-18 13:51:22 -05003478 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05003479
3480 cfq_shutdown_timer_wq(cfqd);
3481
Vivek Goyalb1c35762009-12-03 12:59:47 -05003482 /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
3483 synchronize_rcu();
Al Viroa90d7422006-03-18 12:05:37 -05003484 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Jens Axboe165125e2007-07-24 09:28:11 +02003487static void *cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
3489 struct cfq_data *cfqd;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003490 int i, j;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003491 struct cfq_group *cfqg;
Vivek Goyal615f0252009-12-03 12:59:39 -05003492 struct cfq_rb_root *st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Christoph Lameter94f60302007-07-17 04:03:29 -07003494 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02003496 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003498 /* Init root service tree */
3499 cfqd->grp_service_tree = CFQ_RB_ROOT;
3500
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003501 /* Init root group */
3502 cfqg = &cfqd->root_group;
Vivek Goyal615f0252009-12-03 12:59:39 -05003503 for_each_cfqg_st(cfqg, i, j, st)
3504 *st = CFQ_RB_ROOT;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003505 RB_CLEAR_NODE(&cfqg->rb_node);
Jens Axboe26a2ac02009-04-23 12:13:27 +02003506
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003507 /* Give preference to root group over other groups */
3508 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3509
Vivek Goyal25fb5162009-12-03 12:59:46 -05003510#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyalb1c35762009-12-03 12:59:47 -05003511 /*
3512 * Take a reference to root group which we never drop. This is just
3513 * to make sure that cfq_put_cfqg() does not try to kfree root group
3514 */
3515 atomic_set(&cfqg->ref, 1);
Vivek Goyal25fb5162009-12-03 12:59:46 -05003516 blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd);
3517#endif
Jens Axboe26a2ac02009-04-23 12:13:27 +02003518 /*
3519 * Not strictly needed (since RB_ROOT just clears the node and we
3520 * zeroed cfqd on alloc), but better be safe in case someone decides
3521 * to add magic to the rb code
3522 */
3523 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3524 cfqd->prio_trees[i] = RB_ROOT;
3525
Jens Axboe6118b702009-06-30 09:34:12 +02003526 /*
3527 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3528 * Grab a permanent reference to it, so that the normal code flow
3529 * will not attempt to free it.
3530 */
3531 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3532 atomic_inc(&cfqd->oom_cfqq.ref);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003533 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
Jens Axboe6118b702009-06-30 09:34:12 +02003534
Al Virod9ff4182006-03-18 13:51:22 -05003535 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Jens Axboe22e2c502005-06-27 10:55:12 +02003539 init_timer(&cfqd->idle_slice_timer);
3540 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3541 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3542
Jens Axboe23e018a2009-10-05 08:52:35 +02003543 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003546 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3547 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 cfqd->cfq_back_max = cfq_back_max;
3549 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003550 cfqd->cfq_slice[0] = cfq_slice_async;
3551 cfqd->cfq_slice[1] = cfq_slice_sync;
3552 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3553 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003554 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003555 cfqd->hw_tag = -1;
Vivek Goyal365722b2009-10-03 15:21:27 +02003556 cfqd->last_end_sync_rq = jiffies;
Jens Axboebc1c1162006-06-08 08:49:06 +02003557 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
3559
3560static void cfq_slab_kill(void)
3561{
Jens Axboed6de8be2008-05-28 14:46:59 +02003562 /*
3563 * Caller already ensured that pending RCU callbacks are completed,
3564 * so we should have no busy allocations at this point.
3565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (cfq_pool)
3567 kmem_cache_destroy(cfq_pool);
3568 if (cfq_ioc_pool)
3569 kmem_cache_destroy(cfq_ioc_pool);
3570}
3571
3572static int __init cfq_slab_setup(void)
3573{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003574 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if (!cfq_pool)
3576 goto fail;
3577
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02003578 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 if (!cfq_ioc_pool)
3580 goto fail;
3581
3582 return 0;
3583fail:
3584 cfq_slab_kill();
3585 return -ENOMEM;
3586}
3587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588/*
3589 * sysfs parts below -->
3590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591static ssize_t
3592cfq_var_show(unsigned int var, char *page)
3593{
3594 return sprintf(page, "%d\n", var);
3595}
3596
3597static ssize_t
3598cfq_var_store(unsigned int *var, const char *page, size_t count)
3599{
3600 char *p = (char *) page;
3601
3602 *var = simple_strtoul(p, &p, 10);
3603 return count;
3604}
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003607static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003609 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 unsigned int __data = __VAR; \
3611 if (__CONV) \
3612 __data = jiffies_to_msecs(__data); \
3613 return cfq_var_show(__data, (page)); \
3614}
3615SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003616SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3617SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003618SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3619SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003620SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3621SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3622SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3623SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003624SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625#undef SHOW_FUNCTION
3626
3627#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003628static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003630 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 unsigned int __data; \
3632 int ret = cfq_var_store(&__data, (page), count); \
3633 if (__data < (MIN)) \
3634 __data = (MIN); \
3635 else if (__data > (MAX)) \
3636 __data = (MAX); \
3637 if (__CONV) \
3638 *(__PTR) = msecs_to_jiffies(__data); \
3639 else \
3640 *(__PTR) = __data; \
3641 return ret; \
3642}
3643STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003644STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3645 UINT_MAX, 1);
3646STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3647 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003648STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003649STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3650 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003651STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3652STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3653STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003654STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3655 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003656STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657#undef STORE_FUNCTION
3658
Al Viroe572ec72006-03-18 22:27:18 -05003659#define CFQ_ATTR(name) \
3660 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003661
Al Viroe572ec72006-03-18 22:27:18 -05003662static struct elv_fs_entry cfq_attrs[] = {
3663 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003664 CFQ_ATTR(fifo_expire_sync),
3665 CFQ_ATTR(fifo_expire_async),
3666 CFQ_ATTR(back_seek_max),
3667 CFQ_ATTR(back_seek_penalty),
3668 CFQ_ATTR(slice_sync),
3669 CFQ_ATTR(slice_async),
3670 CFQ_ATTR(slice_async_rq),
3671 CFQ_ATTR(slice_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003672 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003673 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674};
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676static struct elevator_type iosched_cfq = {
3677 .ops = {
3678 .elevator_merge_fn = cfq_merge,
3679 .elevator_merged_fn = cfq_merged_request,
3680 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003681 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02003682 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003684 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 .elevator_deactivate_req_fn = cfq_deactivate_request,
3686 .elevator_queue_empty_fn = cfq_queue_empty,
3687 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003688 .elevator_former_req_fn = elv_rb_former_request,
3689 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 .elevator_set_req_fn = cfq_set_request,
3691 .elevator_put_req_fn = cfq_put_request,
3692 .elevator_may_queue_fn = cfq_may_queue,
3693 .elevator_init_fn = cfq_init_queue,
3694 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02003695 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 },
Al Viro3d1ab402006-03-18 18:35:43 -05003697 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 .elevator_name = "cfq",
3699 .elevator_owner = THIS_MODULE,
3700};
3701
3702static int __init cfq_init(void)
3703{
Jens Axboe22e2c502005-06-27 10:55:12 +02003704 /*
3705 * could be 0 on HZ < 1000 setups
3706 */
3707 if (!cfq_slice_async)
3708 cfq_slice_async = 1;
3709 if (!cfq_slice_idle)
3710 cfq_slice_idle = 1;
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (cfq_slab_setup())
3713 return -ENOMEM;
3714
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003715 elv_register(&iosched_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
3719
3720static void __exit cfq_exit(void)
3721{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07003722 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05003724 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02003725 /* ioc_gone's update must be visible before reading ioc_count */
3726 smp_wmb();
Jens Axboed6de8be2008-05-28 14:46:59 +02003727
3728 /*
3729 * this also protects us from entering cfq_slab_kill() with
3730 * pending RCU callbacks
3731 */
Tejun Heo245b2e72009-06-24 15:13:48 +09003732 if (elv_ioc_count_read(cfq_ioc_count))
Jens Axboe9a11b4e2008-05-29 09:32:08 +02003733 wait_for_completion(&all_gone);
Christoph Hellwig83521d32005-10-30 15:01:39 -08003734 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
3737module_init(cfq_init);
3738module_exit(cfq_exit);
3739
3740MODULE_AUTHOR("Jens Axboe");
3741MODULE_LICENSE("GPL");
3742MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");