blob: 8bc31a50a57f77ec274d4ff78fc443f0977ba225 [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 Axboeb0b8d742007-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 Axboeb0b8d742007-01-19 11:35:30 +1100307 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d742007-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
Jens Axboe7b679132008-05-30 12:23:07 +0200344#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
345 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
346#define cfq_log(cfqd, fmt, args...) \
347 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
348
Vivek Goyal615f0252009-12-03 12:59:39 -0500349/* Traverses through cfq group service trees */
350#define for_each_cfqg_st(cfqg, i, j, st) \
351 for (i = 0; i <= IDLE_WORKLOAD; i++) \
352 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
353 : &cfqg->service_tree_idle; \
354 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
355 (i == IDLE_WORKLOAD && j == 0); \
356 j++, st = i < IDLE_WORKLOAD ? \
357 &cfqg->service_trees[i][j]: NULL) \
358
359
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100360static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
361{
362 if (cfq_class_idle(cfqq))
363 return IDLE_WORKLOAD;
364 if (cfq_class_rt(cfqq))
365 return RT_WORKLOAD;
366 return BE_WORKLOAD;
367}
368
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100369
370static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
371{
372 if (!cfq_cfqq_sync(cfqq))
373 return ASYNC_WORKLOAD;
374 if (!cfq_cfqq_idle_window(cfqq))
375 return SYNC_NOIDLE_WORKLOAD;
376 return SYNC_WORKLOAD;
377}
378
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500379static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
380 struct cfq_data *cfqd,
381 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100382{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500383 if (wl == IDLE_WORKLOAD)
384 return cfqg->service_tree_idle.count;
385
386 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
387 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
388 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100389}
390
Jens Axboe165125e2007-07-24 09:28:11 +0200391static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200392static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100393 struct io_context *, gfp_t);
Jens Axboe4ac845a2008-01-24 08:44:49 +0100394static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
Vasily Tarasov91fac312007-04-25 12:29:51 +0200395 struct io_context *);
396
Jens Axboe5ad531d2009-07-03 12:57:48 +0200397static inline int rq_in_driver(struct cfq_data *cfqd)
398{
399 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
400}
401
Vasily Tarasov91fac312007-04-25 12:29:51 +0200402static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200403 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200404{
Jens Axboea6151c32009-10-07 20:02:57 +0200405 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200406}
407
408static inline void cic_set_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200409 struct cfq_queue *cfqq, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200410{
Jens Axboea6151c32009-10-07 20:02:57 +0200411 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200412}
413
414/*
415 * We regard a request as SYNC, if it's either a read or has the SYNC bit
416 * set (in which case it could also be direct WRITE).
417 */
Jens Axboea6151c32009-10-07 20:02:57 +0200418static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200419{
Jens Axboea6151c32009-10-07 20:02:57 +0200420 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700424 * scheduler run of queue, if there are requests pending and no one in the
425 * driver that will restart queueing
426 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200427static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700428{
Jens Axboe7b679132008-05-30 12:23:07 +0200429 if (cfqd->busy_queues) {
430 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200431 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200432 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700433}
434
Jens Axboe165125e2007-07-24 09:28:11 +0200435static int cfq_queue_empty(struct request_queue *q)
Andrew Morton99f95e52005-06-27 20:14:05 -0700436{
437 struct cfq_data *cfqd = q->elevator->elevator_data;
438
Vivek Goyalf04a6422009-12-03 12:59:40 -0500439 return !cfqd->rq_queued;
Andrew Morton99f95e52005-06-27 20:14:05 -0700440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100443 * Scale schedule slice based on io priority. Use the sync time slice only
444 * if a queue is marked sync and has sync io queued. A sync queue with async
445 * io only, should not get full sync slice length.
446 */
Jens Axboea6151c32009-10-07 20:02:57 +0200447static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200448 unsigned short prio)
449{
450 const int base_slice = cfqd->cfq_slice[sync];
451
452 WARN_ON(prio >= IOPRIO_BE_NR);
453
454 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
455}
456
Jens Axboe44f7c162007-01-19 11:51:58 +1100457static inline int
458cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
459{
Jens Axboed9e76202007-04-20 14:27:50 +0200460 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100461}
462
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500463static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
464{
465 u64 d = delta << CFQ_SERVICE_SHIFT;
466
467 d = d * BLKIO_WEIGHT_DEFAULT;
468 do_div(d, cfqg->weight);
469 return d;
470}
471
472static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
473{
474 s64 delta = (s64)(vdisktime - min_vdisktime);
475 if (delta > 0)
476 min_vdisktime = vdisktime;
477
478 return min_vdisktime;
479}
480
481static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
482{
483 s64 delta = (s64)(vdisktime - min_vdisktime);
484 if (delta < 0)
485 min_vdisktime = vdisktime;
486
487 return min_vdisktime;
488}
489
490static void update_min_vdisktime(struct cfq_rb_root *st)
491{
492 u64 vdisktime = st->min_vdisktime;
493 struct cfq_group *cfqg;
494
495 if (st->active) {
496 cfqg = rb_entry_cfqg(st->active);
497 vdisktime = cfqg->vdisktime;
498 }
499
500 if (st->left) {
501 cfqg = rb_entry_cfqg(st->left);
502 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
503 }
504
505 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
506}
507
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100508/*
509 * get averaged number of queues of RT/BE priority.
510 * average is updated, with a formula that gives more weight to higher numbers,
511 * to quickly follows sudden increases and decrease slowly
512 */
513
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500514static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
515 struct cfq_group *cfqg, bool rt)
Jens Axboe58696192009-10-28 09:27:07 +0100516{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100517 unsigned min_q, max_q;
518 unsigned mult = cfq_hist_divisor - 1;
519 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500520 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100521
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500522 min_q = min(cfqg->busy_queues_avg[rt], busy);
523 max_q = max(cfqg->busy_queues_avg[rt], busy);
524 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100525 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500526 return cfqg->busy_queues_avg[rt];
527}
528
529static inline unsigned
530cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
531{
532 struct cfq_rb_root *st = &cfqd->grp_service_tree;
533
534 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100535}
536
Jens Axboe44f7c162007-01-19 11:51:58 +1100537static inline void
538cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
539{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100540 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
541 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500542 /*
543 * interested queues (we consider only the ones with the same
544 * priority class in the cfq group)
545 */
546 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
547 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100548 unsigned sync_slice = cfqd->cfq_slice[1];
549 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500550 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
551
552 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100553 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
554 /* scale low_slice according to IO priority
555 * and sync vs async */
556 unsigned low_slice =
557 min(slice, base_low_slice * slice / sync_slice);
558 /* the adapted slice value is scaled to fit all iqs
559 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500560 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100561 low_slice);
562 }
563 }
Vivek Goyaldae739e2009-12-03 12:59:45 -0500564 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100565 cfqq->slice_end = jiffies + slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200566 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100567}
568
569/*
570 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
571 * isn't valid until the first request from the dispatch is activated
572 * and the slice time set.
573 */
Jens Axboea6151c32009-10-07 20:02:57 +0200574static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100575{
576 if (cfq_cfqq_slice_new(cfqq))
577 return 0;
578 if (time_before(jiffies, cfqq->slice_end))
579 return 0;
580
581 return 1;
582}
583
584/*
Jens Axboe5e705372006-07-13 12:39:25 +0200585 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200587 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Jens Axboe5e705372006-07-13 12:39:25 +0200589static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100590cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100592 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200594#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
595#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
596 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jens Axboe5e705372006-07-13 12:39:25 +0200598 if (rq1 == NULL || rq1 == rq2)
599 return rq2;
600 if (rq2 == NULL)
601 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200602
Jens Axboe5e705372006-07-13 12:39:25 +0200603 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
604 return rq1;
605 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
606 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200607 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
608 return rq1;
609 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
610 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Tejun Heo83096eb2009-05-07 22:24:39 +0900612 s1 = blk_rq_pos(rq1);
613 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * by definition, 1KiB is 2 sectors
617 */
618 back_max = cfqd->cfq_back_max * 2;
619
620 /*
621 * Strict one way elevator _except_ in the case where we allow
622 * short backward seeks which are biased as twice the cost of a
623 * similar forward seek.
624 */
625 if (s1 >= last)
626 d1 = s1 - last;
627 else if (s1 + back_max >= last)
628 d1 = (last - s1) * cfqd->cfq_back_penalty;
629 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200630 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (s2 >= last)
633 d2 = s2 - last;
634 else if (s2 + back_max >= last)
635 d2 = (last - s2) * cfqd->cfq_back_penalty;
636 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200637 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Andreas Mohre8a99052006-03-28 08:59:49 +0200641 /*
642 * By doing switch() on the bit mask "wrap" we avoid having to
643 * check two variables for all permutations: --> faster!
644 */
645 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200646 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200647 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200648 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200649 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200650 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200651 else {
652 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200653 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200654 else
Jens Axboe5e705372006-07-13 12:39:25 +0200655 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200656 }
657
658 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200659 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200660 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200661 return rq2;
662 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200663 default:
664 /*
665 * Since both rqs are wrapped,
666 * start with the one that's further behind head
667 * (--> only *one* back seek required),
668 * since back seek takes more time than forward.
669 */
670 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200671 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 else
Jens Axboe5e705372006-07-13 12:39:25 +0200673 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675}
676
Jens Axboe498d3aa2007-04-26 12:54:48 +0200677/*
678 * The below is leftmost cache rbtree addon
679 */
Jens Axboe08717142008-01-28 11:38:15 +0100680static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200681{
Vivek Goyal615f0252009-12-03 12:59:39 -0500682 /* Service tree is empty */
683 if (!root->count)
684 return NULL;
685
Jens Axboecc09e292007-04-26 12:53:50 +0200686 if (!root->left)
687 root->left = rb_first(&root->rb);
688
Jens Axboe08717142008-01-28 11:38:15 +0100689 if (root->left)
690 return rb_entry(root->left, struct cfq_queue, rb_node);
691
692 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200693}
694
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500695static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
696{
697 if (!root->left)
698 root->left = rb_first(&root->rb);
699
700 if (root->left)
701 return rb_entry_cfqg(root->left);
702
703 return NULL;
704}
705
Jens Axboea36e71f2009-04-15 12:15:11 +0200706static void rb_erase_init(struct rb_node *n, struct rb_root *root)
707{
708 rb_erase(n, root);
709 RB_CLEAR_NODE(n);
710}
711
Jens Axboecc09e292007-04-26 12:53:50 +0200712static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
713{
714 if (root->left == n)
715 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200716 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100717 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * would be nice to take fifo expire time into account as well
722 */
Jens Axboe5e705372006-07-13 12:39:25 +0200723static struct request *
724cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
725 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Jens Axboe21183b02006-07-13 12:33:14 +0200727 struct rb_node *rbnext = rb_next(&last->rb_node);
728 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200729 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jens Axboe21183b02006-07-13 12:33:14 +0200731 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200734 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200737 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200738 else {
739 rbnext = rb_first(&cfqq->sort_list);
740 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200741 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100744 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Jens Axboed9e76202007-04-20 14:27:50 +0200747static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
748 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Jens Axboed9e76202007-04-20 14:27:50 +0200750 /*
751 * just an approximation, should be ok.
752 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500753 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100754 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500757static inline s64
758cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
759{
760 return cfqg->vdisktime - st->min_vdisktime;
761}
762
763static void
764__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
765{
766 struct rb_node **node = &st->rb.rb_node;
767 struct rb_node *parent = NULL;
768 struct cfq_group *__cfqg;
769 s64 key = cfqg_key(st, cfqg);
770 int left = 1;
771
772 while (*node != NULL) {
773 parent = *node;
774 __cfqg = rb_entry_cfqg(parent);
775
776 if (key < cfqg_key(st, __cfqg))
777 node = &parent->rb_left;
778 else {
779 node = &parent->rb_right;
780 left = 0;
781 }
782 }
783
784 if (left)
785 st->left = &cfqg->rb_node;
786
787 rb_link_node(&cfqg->rb_node, parent, node);
788 rb_insert_color(&cfqg->rb_node, &st->rb);
789}
790
791static void
792cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
793{
794 struct cfq_rb_root *st = &cfqd->grp_service_tree;
795 struct cfq_group *__cfqg;
796 struct rb_node *n;
797
798 cfqg->nr_cfqq++;
799 if (cfqg->on_st)
800 return;
801
802 /*
803 * Currently put the group at the end. Later implement something
804 * so that groups get lesser vtime based on their weights, so that
805 * if group does not loose all if it was not continously backlogged.
806 */
807 n = rb_last(&st->rb);
808 if (n) {
809 __cfqg = rb_entry_cfqg(n);
810 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
811 } else
812 cfqg->vdisktime = st->min_vdisktime;
813
814 __cfq_group_service_tree_add(st, cfqg);
815 cfqg->on_st = true;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500816 cfqd->nr_groups++;
817 st->total_weight += cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500818}
819
820static void
821cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
822{
823 struct cfq_rb_root *st = &cfqd->grp_service_tree;
824
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500825 if (st->active == &cfqg->rb_node)
826 st->active = NULL;
827
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500828 BUG_ON(cfqg->nr_cfqq < 1);
829 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500830
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500831 /* If there are other cfq queues under this group, don't delete it */
832 if (cfqg->nr_cfqq)
833 return;
834
835 cfqg->on_st = false;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500836 cfqd->nr_groups--;
837 st->total_weight -= cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500838 if (!RB_EMPTY_NODE(&cfqg->rb_node))
839 cfq_rb_erase(&cfqg->rb_node, st);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500840 cfqg->saved_workload_slice = 0;
841}
842
843static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
844{
845 unsigned int slice_used, allocated_slice;
846
847 /*
848 * Queue got expired before even a single request completed or
849 * got expired immediately after first request completion.
850 */
851 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
852 /*
853 * Also charge the seek time incurred to the group, otherwise
854 * if there are mutiple queues in the group, each can dispatch
855 * a single request on seeky media and cause lots of seek time
856 * and group will never know it.
857 */
858 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
859 1);
860 } else {
861 slice_used = jiffies - cfqq->slice_start;
862 allocated_slice = cfqq->slice_end - cfqq->slice_start;
863 if (slice_used > allocated_slice)
864 slice_used = allocated_slice;
865 }
866
867 cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u", slice_used);
868 return slice_used;
869}
870
871static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
872 struct cfq_queue *cfqq)
873{
874 struct cfq_rb_root *st = &cfqd->grp_service_tree;
875 unsigned int used_sl;
876
877 used_sl = cfq_cfqq_slice_usage(cfqq);
878
879 /* Can't update vdisktime while group is on service tree */
880 cfq_rb_erase(&cfqg->rb_node, st);
881 cfqg->vdisktime += cfq_scale_slice(used_sl, cfqg);
882 __cfq_group_service_tree_add(st, cfqg);
883
884 /* This group is being expired. Save the context */
885 if (time_after(cfqd->workload_expires, jiffies)) {
886 cfqg->saved_workload_slice = cfqd->workload_expires
887 - jiffies;
888 cfqg->saved_workload = cfqd->serving_type;
889 cfqg->saved_serving_prio = cfqd->serving_prio;
890 } else
891 cfqg->saved_workload_slice = 0;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500892}
893
Vivek Goyal25fb5162009-12-03 12:59:46 -0500894#ifdef CONFIG_CFQ_GROUP_IOSCHED
895static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
896{
897 if (blkg)
898 return container_of(blkg, struct cfq_group, blkg);
899 return NULL;
900}
901
902static struct cfq_group *
903cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
904{
905 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
906 struct cfq_group *cfqg = NULL;
907 void *key = cfqd;
908 int i, j;
909 struct cfq_rb_root *st;
910
911 /* Do we need to take this reference */
912 if (!css_tryget(&blkcg->css))
913 return NULL;;
914
915 cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
916 if (cfqg || !create)
917 goto done;
918
919 cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
920 if (!cfqg)
921 goto done;
922
923 cfqg->weight = blkcg->weight;
924 for_each_cfqg_st(cfqg, i, j, st)
925 *st = CFQ_RB_ROOT;
926 RB_CLEAR_NODE(&cfqg->rb_node);
927
Vivek Goyalb1c35762009-12-03 12:59:47 -0500928 /*
929 * Take the initial reference that will be released on destroy
930 * This can be thought of a joint reference by cgroup and
931 * elevator which will be dropped by either elevator exit
932 * or cgroup deletion path depending on who is exiting first.
933 */
934 atomic_set(&cfqg->ref, 1);
935
Vivek Goyal25fb5162009-12-03 12:59:46 -0500936 /* Add group onto cgroup list */
937 blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd);
938
939 /* Add group on cfqd list */
940 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
941
942done:
943 css_put(&blkcg->css);
944 return cfqg;
945}
946
947/*
948 * Search for the cfq group current task belongs to. If create = 1, then also
949 * create the cfq group if it does not exist. request_queue lock must be held.
950 */
951static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
952{
953 struct cgroup *cgroup;
954 struct cfq_group *cfqg = NULL;
955
956 rcu_read_lock();
957 cgroup = task_cgroup(current, blkio_subsys_id);
958 cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
959 if (!cfqg && create)
960 cfqg = &cfqd->root_group;
961 rcu_read_unlock();
962 return cfqg;
963}
964
965static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
966{
967 /* Currently, all async queues are mapped to root group */
968 if (!cfq_cfqq_sync(cfqq))
969 cfqg = &cfqq->cfqd->root_group;
970
971 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500972 /* cfqq reference on cfqg */
973 atomic_inc(&cfqq->cfqg->ref);
Vivek Goyal25fb5162009-12-03 12:59:46 -0500974}
Vivek Goyalb1c35762009-12-03 12:59:47 -0500975
976static void cfq_put_cfqg(struct cfq_group *cfqg)
977{
978 struct cfq_rb_root *st;
979 int i, j;
980
981 BUG_ON(atomic_read(&cfqg->ref) <= 0);
982 if (!atomic_dec_and_test(&cfqg->ref))
983 return;
984 for_each_cfqg_st(cfqg, i, j, st)
985 BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
986 kfree(cfqg);
987}
988
989static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
990{
991 /* Something wrong if we are trying to remove same group twice */
992 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
993
994 hlist_del_init(&cfqg->cfqd_node);
995
996 /*
997 * Put the reference taken at the time of creation so that when all
998 * queues are gone, group can be destroyed.
999 */
1000 cfq_put_cfqg(cfqg);
1001}
1002
1003static void cfq_release_cfq_groups(struct cfq_data *cfqd)
1004{
1005 struct hlist_node *pos, *n;
1006 struct cfq_group *cfqg;
1007
1008 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1009 /*
1010 * If cgroup removal path got to blk_group first and removed
1011 * it from cgroup list, then it will take care of destroying
1012 * cfqg also.
1013 */
1014 if (!blkiocg_del_blkio_group(&cfqg->blkg))
1015 cfq_destroy_cfqg(cfqd, cfqg);
1016 }
1017}
1018
1019/*
1020 * Blk cgroup controller notification saying that blkio_group object is being
1021 * delinked as associated cgroup object is going away. That also means that
1022 * no new IO will come in this group. So get rid of this group as soon as
1023 * any pending IO in the group is finished.
1024 *
1025 * This function is called under rcu_read_lock(). key is the rcu protected
1026 * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
1027 * read lock.
1028 *
1029 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1030 * it should not be NULL as even if elevator was exiting, cgroup deltion
1031 * path got to it first.
1032 */
1033void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
1034{
1035 unsigned long flags;
1036 struct cfq_data *cfqd = key;
1037
1038 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1039 cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
1040 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1041}
1042
Vivek Goyal25fb5162009-12-03 12:59:46 -05001043#else /* GROUP_IOSCHED */
1044static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1045{
1046 return &cfqd->root_group;
1047}
1048static inline void
1049cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1050 cfqq->cfqg = cfqg;
1051}
1052
Vivek Goyalb1c35762009-12-03 12:59:47 -05001053static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1054static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1055
Vivek Goyal25fb5162009-12-03 12:59:46 -05001056#endif /* GROUP_IOSCHED */
1057
Jens Axboe498d3aa2007-04-26 12:54:48 +02001058/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001059 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa2007-04-26 12:54:48 +02001060 * requests waiting to be processed. It is sorted in the order that
1061 * we will service the queues.
1062 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001063static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001064 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001065{
Jens Axboe08717142008-01-28 11:38:15 +01001066 struct rb_node **p, *parent;
1067 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001068 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001069 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa2007-04-26 12:54:48 +02001070 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001071 int new_cfqq = 1;
Jens Axboed9e76202007-04-20 14:27:50 +02001072
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001073 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
1074 cfqq_type(cfqq), cfqd);
Jens Axboe08717142008-01-28 11:38:15 +01001075 if (cfq_class_idle(cfqq)) {
1076 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001077 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001078 if (parent && parent != &cfqq->rb_node) {
1079 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1080 rb_key += __cfqq->rb_key;
1081 } else
1082 rb_key += jiffies;
1083 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001084 /*
1085 * Get our rb key offset. Subtract any residual slice
1086 * value carried from last service. A negative resid
1087 * count indicates slice overrun, and this should position
1088 * the next service time further away in the tree.
1089 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001090 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001091 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001092 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001093 } else {
1094 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001095 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001096 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1097 }
Jens Axboed9e76202007-04-20 14:27:50 +02001098
1099 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001100 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001101 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001102 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001103 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001104 if (rb_key == cfqq->rb_key &&
1105 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001106 return;
Jens Axboe53b03742006-07-28 09:48:51 +02001107
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001108 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1109 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001110 }
Jens Axboed9e76202007-04-20 14:27:50 +02001111
Jens Axboe498d3aa2007-04-26 12:54:48 +02001112 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001113 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001114 cfqq->service_tree = service_tree;
1115 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001116 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001117 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001118
Jens Axboed9e76202007-04-20 14:27:50 +02001119 parent = *p;
1120 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1121
Jens Axboe0c534e02007-04-18 20:01:57 +02001122 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001123 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001124 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001125 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001126 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001127 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001128 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001129 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001130 }
Jens Axboe67060e32007-04-18 20:13:32 +02001131
1132 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001133 }
1134
Jens Axboecc09e292007-04-26 12:53:50 +02001135 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001136 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001137
Jens Axboed9e76202007-04-20 14:27:50 +02001138 cfqq->rb_key = rb_key;
1139 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001140 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1141 service_tree->count++;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001142 if (add_front || !new_cfqq)
1143 return;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001144 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Jens Axboea36e71f2009-04-15 12:15:11 +02001147static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001148cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1149 sector_t sector, struct rb_node **ret_parent,
1150 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001151{
Jens Axboea36e71f2009-04-15 12:15:11 +02001152 struct rb_node **p, *parent;
1153 struct cfq_queue *cfqq = NULL;
1154
1155 parent = NULL;
1156 p = &root->rb_node;
1157 while (*p) {
1158 struct rb_node **n;
1159
1160 parent = *p;
1161 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1162
1163 /*
1164 * Sort strictly based on sector. Smallest to the left,
1165 * largest to the right.
1166 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001167 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001168 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001169 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001170 n = &(*p)->rb_left;
1171 else
1172 break;
1173 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001174 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001175 }
1176
1177 *ret_parent = parent;
1178 if (rb_link)
1179 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001180 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001181}
1182
1183static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1184{
Jens Axboea36e71f2009-04-15 12:15:11 +02001185 struct rb_node **p, *parent;
1186 struct cfq_queue *__cfqq;
1187
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001188 if (cfqq->p_root) {
1189 rb_erase(&cfqq->p_node, cfqq->p_root);
1190 cfqq->p_root = NULL;
1191 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001192
1193 if (cfq_class_idle(cfqq))
1194 return;
1195 if (!cfqq->next_rq)
1196 return;
1197
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001198 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001199 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1200 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001201 if (!__cfqq) {
1202 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001203 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1204 } else
1205 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001206}
1207
Jens Axboe498d3aa2007-04-26 12:54:48 +02001208/*
1209 * Update cfqq's position in the service tree.
1210 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001211static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001212{
Jens Axboe6d048f52007-04-25 12:44:27 +02001213 /*
1214 * Resorting requires the cfqq to be on the RR list already.
1215 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001216 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001217 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001218 cfq_prio_tree_add(cfqd, cfqq);
1219 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001220}
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/*
1223 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001224 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Jens Axboefebffd62008-01-28 13:19:43 +01001226static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Jens Axboe7b679132008-05-30 12:23:07 +02001228 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001229 BUG_ON(cfq_cfqq_on_rr(cfqq));
1230 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 cfqd->busy_queues++;
1232
Jens Axboeedd75ff2007-04-19 12:03:34 +02001233 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Jens Axboe498d3aa2007-04-26 12:54:48 +02001236/*
1237 * Called when the cfqq no longer has requests pending, remove it from
1238 * the service tree.
1239 */
Jens Axboefebffd62008-01-28 13:19:43 +01001240static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Jens Axboe7b679132008-05-30 12:23:07 +02001242 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001243 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1244 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001246 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1247 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1248 cfqq->service_tree = NULL;
1249 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001250 if (cfqq->p_root) {
1251 rb_erase(&cfqq->p_node, cfqq->p_root);
1252 cfqq->p_root = NULL;
1253 }
Jens Axboed9e76202007-04-20 14:27:50 +02001254
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001255 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 BUG_ON(!cfqd->busy_queues);
1257 cfqd->busy_queues--;
1258}
1259
1260/*
1261 * rb tree support functions
1262 */
Jens Axboefebffd62008-01-28 13:19:43 +01001263static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Jens Axboe5e705372006-07-13 12:39:25 +02001265 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001266 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jens Axboeb4878f22005-10-20 16:42:29 +02001268 BUG_ON(!cfqq->queued[sync]);
1269 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jens Axboe5e705372006-07-13 12:39:25 +02001271 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Vivek Goyalf04a6422009-12-03 12:59:40 -05001273 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1274 /*
1275 * Queue will be deleted from service tree when we actually
1276 * expire it later. Right now just remove it from prio tree
1277 * as it is empty.
1278 */
1279 if (cfqq->p_root) {
1280 rb_erase(&cfqq->p_node, cfqq->p_root);
1281 cfqq->p_root = NULL;
1282 }
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Jens Axboe5e705372006-07-13 12:39:25 +02001286static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Jens Axboe5e705372006-07-13 12:39:25 +02001288 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboea36e71f2009-04-15 12:15:11 +02001290 struct request *__alias, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jens Axboe5380a102006-07-13 12:37:56 +02001292 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /*
1295 * looks a little odd, but the first insert might return an alias.
1296 * if that happens, put the alias on the dispatch list
1297 */
Jens Axboe21183b02006-07-13 12:33:14 +02001298 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +02001299 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001300
1301 if (!cfq_cfqq_on_rr(cfqq))
1302 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001303
1304 /*
1305 * check if this request is a better next-serve candidate
1306 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001307 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001308 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001309
1310 /*
1311 * adjust priority tree position, if ->next_rq changes
1312 */
1313 if (prev != cfqq->next_rq)
1314 cfq_prio_tree_add(cfqd, cfqq);
1315
Jens Axboe5044eed2007-04-25 11:53:48 +02001316 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Jens Axboefebffd62008-01-28 13:19:43 +01001319static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Jens Axboe5380a102006-07-13 12:37:56 +02001321 elv_rb_del(&cfqq->sort_list, rq);
1322 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +02001323 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Jens Axboe206dc692006-03-28 13:03:44 +02001326static struct request *
1327cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Jens Axboe206dc692006-03-28 13:03:44 +02001329 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001330 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001331 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jens Axboe4ac845a2008-01-24 08:44:49 +01001333 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001334 if (!cic)
1335 return NULL;
1336
1337 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001338 if (cfqq) {
1339 sector_t sector = bio->bi_sector + bio_sectors(bio);
1340
Jens Axboe21183b02006-07-13 12:33:14 +02001341 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return NULL;
1345}
1346
Jens Axboe165125e2007-07-24 09:28:11 +02001347static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001348{
1349 struct cfq_data *cfqd = q->elevator->elevator_data;
1350
Jens Axboe5ad531d2009-07-03 12:57:48 +02001351 cfqd->rq_in_driver[rq_is_sync(rq)]++;
Jens Axboe7b679132008-05-30 12:23:07 +02001352 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +02001353 rq_in_driver(cfqd));
Jens Axboe25776e32006-06-01 10:12:26 +02001354
Tejun Heo5b936292009-05-07 22:24:38 +09001355 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001356}
1357
Jens Axboe165125e2007-07-24 09:28:11 +02001358static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Jens Axboe22e2c502005-06-27 10:55:12 +02001360 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5ad531d2009-07-03 12:57:48 +02001361 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jens Axboe5ad531d2009-07-03 12:57:48 +02001363 WARN_ON(!cfqd->rq_in_driver[sync]);
1364 cfqd->rq_in_driver[sync]--;
Jens Axboe7b679132008-05-30 12:23:07 +02001365 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +02001366 rq_in_driver(cfqd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Jens Axboeb4878f22005-10-20 16:42:29 +02001369static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Jens Axboe5e705372006-07-13 12:39:25 +02001371 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001372
Jens Axboe5e705372006-07-13 12:39:25 +02001373 if (cfqq->next_rq == rq)
1374 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Jens Axboeb4878f22005-10-20 16:42:29 +02001376 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001377 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001378
Aaron Carroll45333d52008-08-26 15:52:36 +02001379 cfqq->cfqd->rq_queued--;
Jens Axboe374f84a2006-07-23 01:42:19 +02001380 if (rq_is_meta(rq)) {
1381 WARN_ON(!cfqq->meta_pending);
1382 cfqq->meta_pending--;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Jens Axboe165125e2007-07-24 09:28:11 +02001386static int cfq_merge(struct request_queue *q, struct request **req,
1387 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 struct cfq_data *cfqd = q->elevator->elevator_data;
1390 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jens Axboe206dc692006-03-28 13:03:44 +02001392 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001393 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001394 *req = __rq;
1395 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Jens Axboe165125e2007-07-24 09:28:11 +02001401static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001402 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Jens Axboe21183b02006-07-13 12:33:14 +02001404 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001405 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Jens Axboe5e705372006-07-13 12:39:25 +02001407 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411static void
Jens Axboe165125e2007-07-24 09:28:11 +02001412cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 struct request *next)
1414{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001415 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001416 /*
1417 * reposition in fifo if next is older than rq
1418 */
1419 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001420 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001421 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001422 rq_set_fifo_time(rq, rq_fifo_time(next));
1423 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001424
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001425 if (cfqq->next_rq == next)
1426 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001427 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +02001428}
1429
Jens Axboe165125e2007-07-24 09:28:11 +02001430static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001431 struct bio *bio)
1432{
1433 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001434 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001435 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001436
1437 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001438 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001439 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001440 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001441 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001442
1443 /*
Jens Axboe719d3402006-12-22 09:38:53 +01001444 * Lookup the cfqq that this bio will be queued with. Allow
1445 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001446 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01001447 cic = cfq_cic_lookup(cfqd, current->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001448 if (!cic)
Jens Axboea6151c32009-10-07 20:02:57 +02001449 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001450
Vasily Tarasov91fac312007-04-25 12:29:51 +02001451 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001452 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001453}
1454
Jens Axboefebffd62008-01-28 13:19:43 +01001455static void __cfq_set_active_queue(struct cfq_data *cfqd,
1456 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001457{
1458 if (cfqq) {
Jens Axboe7b679132008-05-30 12:23:07 +02001459 cfq_log_cfqq(cfqd, cfqq, "set_active");
Vivek Goyaldae739e2009-12-03 12:59:45 -05001460 cfqq->slice_start = 0;
1461 cfqq->dispatch_start = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001462 cfqq->slice_end = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001463 cfqq->slice_dispatch = 0;
1464
Jens Axboe2f5cb732009-04-07 08:51:19 +02001465 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboeb0291952009-04-07 11:38:31 +02001466 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001467 cfq_clear_cfqq_must_alloc_slice(cfqq);
1468 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +11001469 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001470
1471 del_timer(&cfqd->idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02001472 }
1473
1474 cfqd->active_queue = cfqq;
1475}
1476
1477/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001478 * current cfqq expired its slice (or was too idle), select new one
1479 */
1480static void
1481__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001482 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001483{
Jens Axboe7b679132008-05-30 12:23:07 +02001484 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1485
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001486 if (cfq_cfqq_wait_request(cfqq))
1487 del_timer(&cfqd->idle_slice_timer);
1488
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001489 cfq_clear_cfqq_wait_request(cfqq);
1490
1491 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001492 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001493 */
Jens Axboe7b679132008-05-30 12:23:07 +02001494 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
Jens Axboec5b680f2007-01-19 11:56:49 +11001495 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001496 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1497 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001498
Vivek Goyaldae739e2009-12-03 12:59:45 -05001499 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
1500
Vivek Goyalf04a6422009-12-03 12:59:40 -05001501 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1502 cfq_del_cfqq_rr(cfqd, cfqq);
1503
Jens Axboeedd75ff2007-04-19 12:03:34 +02001504 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001505
1506 if (cfqq == cfqd->active_queue)
1507 cfqd->active_queue = NULL;
1508
Vivek Goyaldae739e2009-12-03 12:59:45 -05001509 if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
1510 cfqd->grp_service_tree.active = NULL;
1511
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001512 if (cfqd->active_cic) {
1513 put_io_context(cfqd->active_cic->ioc);
1514 cfqd->active_cic = NULL;
1515 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001516}
1517
Jens Axboea6151c32009-10-07 20:02:57 +02001518static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001519{
1520 struct cfq_queue *cfqq = cfqd->active_queue;
1521
1522 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +02001523 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001524}
1525
Jens Axboe498d3aa2007-04-26 12:54:48 +02001526/*
1527 * Get next queue for service. Unless we have a queue preemption,
1528 * we'll simply select the first cfqq in the service tree.
1529 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001530static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001531{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001532 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001533 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1534 cfqd->serving_type, cfqd);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001535
Vivek Goyalf04a6422009-12-03 12:59:40 -05001536 if (!cfqd->rq_queued)
1537 return NULL;
1538
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001539 /* There is nothing to dispatch */
1540 if (!service_tree)
1541 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001542 if (RB_EMPTY_ROOT(&service_tree->rb))
1543 return NULL;
1544 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001545}
1546
Vivek Goyalf04a6422009-12-03 12:59:40 -05001547static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1548{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001549 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001550 struct cfq_queue *cfqq;
1551 int i, j;
1552 struct cfq_rb_root *st;
1553
1554 if (!cfqd->rq_queued)
1555 return NULL;
1556
Vivek Goyal25fb5162009-12-03 12:59:46 -05001557 cfqg = cfq_get_next_cfqg(cfqd);
1558 if (!cfqg)
1559 return NULL;
1560
Vivek Goyalf04a6422009-12-03 12:59:40 -05001561 for_each_cfqg_st(cfqg, i, j, st)
1562 if ((cfqq = cfq_rb_first(st)) != NULL)
1563 return cfqq;
1564 return NULL;
1565}
1566
Jens Axboe498d3aa2007-04-26 12:54:48 +02001567/*
1568 * Get and set a new active queue for service.
1569 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001570static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1571 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001572{
Jens Axboee00ef792009-11-04 08:54:55 +01001573 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001574 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001575
Jens Axboe22e2c502005-06-27 10:55:12 +02001576 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001577 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001578}
1579
Jens Axboed9e76202007-04-20 14:27:50 +02001580static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1581 struct request *rq)
1582{
Tejun Heo83096eb2009-05-07 22:24:39 +09001583 if (blk_rq_pos(rq) >= cfqd->last_position)
1584 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001585 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001586 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001587}
1588
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001589#define CFQQ_SEEK_THR 8 * 1024
1590#define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001591
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001592static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1593 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001594{
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001595 sector_t sdist = cfqq->seek_mean;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001596
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001597 if (!sample_valid(cfqq->seek_samples))
1598 sdist = CFQQ_SEEK_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001599
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001600 return cfq_dist_from_last(cfqd, rq) <= sdist;
Jens Axboe6d048f52007-04-25 12:44:27 +02001601}
1602
Jens Axboea36e71f2009-04-15 12:15:11 +02001603static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1604 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001605{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001606 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001607 struct rb_node *parent, *node;
1608 struct cfq_queue *__cfqq;
1609 sector_t sector = cfqd->last_position;
1610
1611 if (RB_EMPTY_ROOT(root))
1612 return NULL;
1613
1614 /*
1615 * First, if we find a request starting at the end of the last
1616 * request, choose it.
1617 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001618 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001619 if (__cfqq)
1620 return __cfqq;
1621
1622 /*
1623 * If the exact sector wasn't found, the parent of the NULL leaf
1624 * will contain the closest sector.
1625 */
1626 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001627 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001628 return __cfqq;
1629
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001630 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001631 node = rb_next(&__cfqq->p_node);
1632 else
1633 node = rb_prev(&__cfqq->p_node);
1634 if (!node)
1635 return NULL;
1636
1637 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001638 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001639 return __cfqq;
1640
1641 return NULL;
1642}
1643
1644/*
1645 * cfqd - obvious
1646 * cur_cfqq - passed in so that we don't decide that the current queue is
1647 * closely cooperating with itself.
1648 *
1649 * So, basically we're assuming that that cur_cfqq has dispatched at least
1650 * one request, and that cfqd->last_position reflects a position on the disk
1651 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1652 * assumption.
1653 */
1654static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001655 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001656{
1657 struct cfq_queue *cfqq;
1658
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001659 if (!cfq_cfqq_sync(cur_cfqq))
1660 return NULL;
1661 if (CFQQ_SEEKY(cur_cfqq))
1662 return NULL;
1663
Jens Axboea36e71f2009-04-15 12:15:11 +02001664 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001665 * We should notice if some of the queues are cooperating, eg
1666 * working closely on the same area of the disk. In that case,
1667 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001668 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001669 cfqq = cfqq_close(cfqd, cur_cfqq);
1670 if (!cfqq)
1671 return NULL;
1672
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001673 /*
1674 * It only makes sense to merge sync queues.
1675 */
1676 if (!cfq_cfqq_sync(cfqq))
1677 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001678 if (CFQQ_SEEKY(cfqq))
1679 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001680
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001681 /*
1682 * Do not merge queues of different priority classes
1683 */
1684 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1685 return NULL;
1686
Jens Axboea36e71f2009-04-15 12:15:11 +02001687 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001688}
1689
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001690/*
1691 * Determine whether we should enforce idle window for this queue.
1692 */
1693
1694static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1695{
1696 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001697 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001698
Vivek Goyalf04a6422009-12-03 12:59:40 -05001699 BUG_ON(!service_tree);
1700 BUG_ON(!service_tree->count);
1701
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001702 /* We never do for idle class queues. */
1703 if (prio == IDLE_WORKLOAD)
1704 return false;
1705
1706 /* We do for queues that were marked with idle window flag. */
1707 if (cfq_cfqq_idle_window(cfqq))
1708 return true;
1709
1710 /*
1711 * Otherwise, we do only if they are the last ones
1712 * in their service tree.
1713 */
Vivek Goyalf04a6422009-12-03 12:59:40 -05001714 return service_tree->count == 1;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001715}
1716
Jens Axboe6d048f52007-04-25 12:44:27 +02001717static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001718{
Jens Axboe17926692007-01-19 11:59:30 +11001719 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +02001720 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001721 unsigned long sl;
1722
Jens Axboea68bbdd2008-09-24 13:03:33 +02001723 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001724 * SSD device without seek penalty, disable idling. But only do so
1725 * for devices that support queuing, otherwise we still have a problem
1726 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001727 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001728 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001729 return;
1730
Jens Axboedd67d052006-06-21 09:36:18 +02001731 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001732 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001733
1734 /*
1735 * idle is disabled, either manually or by past process history
1736 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001737 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
Jens Axboe6d048f52007-04-25 12:44:27 +02001738 return;
1739
Jens Axboe22e2c502005-06-27 10:55:12 +02001740 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001741 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001742 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001743 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001744 return;
1745
1746 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001747 * task has exited, don't wait
1748 */
Jens Axboe206dc692006-03-28 13:03:44 +02001749 cic = cfqd->active_cic;
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01001750 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001751 return;
1752
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001753 /*
1754 * If our average think time is larger than the remaining time
1755 * slice, then don't idle. This avoids overrunning the allotted
1756 * time slice.
1757 */
1758 if (sample_valid(cic->ttime_samples) &&
1759 (cfqq->slice_end - jiffies < cic->ttime_mean))
1760 return;
1761
Jens Axboe3b181522005-06-27 10:56:24 +02001762 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001763
Jens Axboe6d048f52007-04-25 12:44:27 +02001764 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001765
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001766 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe9481ffd2009-04-15 12:14:13 +02001767 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Jens Axboe498d3aa2007-04-26 12:54:48 +02001770/*
1771 * Move request from internal lists to the request queue dispatch list.
1772 */
Jens Axboe165125e2007-07-24 09:28:11 +02001773static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001775 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001776 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001777
Jens Axboe7b679132008-05-30 12:23:07 +02001778 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1779
Jeff Moyer06d21882009-09-11 17:08:59 +02001780 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001781 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001782 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001783 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001784
1785 if (cfq_cfqq_sync(cfqq))
1786 cfqd->sync_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789/*
1790 * return expired entry, or NULL to just start from scratch in rbtree
1791 */
Jens Axboefebffd62008-01-28 13:19:43 +01001792static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Jens Axboe30996f42009-10-05 11:03:39 +02001794 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jens Axboe3b181522005-06-27 10:56:24 +02001796 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001798
1799 cfq_mark_cfqq_fifo_expire(cfqq);
1800
Jens Axboe89850f72006-07-22 16:48:31 +02001801 if (list_empty(&cfqq->fifo))
1802 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jens Axboe89850f72006-07-22 16:48:31 +02001804 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001805 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001806 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Jens Axboe30996f42009-10-05 11:03:39 +02001808 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001809 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Jens Axboe22e2c502005-06-27 10:55:12 +02001812static inline int
1813cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1814{
1815 const int base_rq = cfqd->cfq_slice_async_rq;
1816
1817 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1818
1819 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1820}
1821
1822/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001823 * Must be called with the queue_lock held.
1824 */
1825static int cfqq_process_refs(struct cfq_queue *cfqq)
1826{
1827 int process_refs, io_refs;
1828
1829 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1830 process_refs = atomic_read(&cfqq->ref) - io_refs;
1831 BUG_ON(process_refs < 0);
1832 return process_refs;
1833}
1834
1835static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1836{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001837 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001838 struct cfq_queue *__cfqq;
1839
1840 /* Avoid a circular list and skip interim queue merges */
1841 while ((__cfqq = new_cfqq->new_cfqq)) {
1842 if (__cfqq == cfqq)
1843 return;
1844 new_cfqq = __cfqq;
1845 }
1846
1847 process_refs = cfqq_process_refs(cfqq);
1848 /*
1849 * If the process for the cfqq has gone away, there is no
1850 * sense in merging the queues.
1851 */
1852 if (process_refs == 0)
1853 return;
1854
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001855 /*
1856 * Merge in the direction of the lesser amount of work.
1857 */
1858 new_process_refs = cfqq_process_refs(new_cfqq);
1859 if (new_process_refs >= process_refs) {
1860 cfqq->new_cfqq = new_cfqq;
1861 atomic_add(process_refs, &new_cfqq->ref);
1862 } else {
1863 new_cfqq->new_cfqq = cfqq;
1864 atomic_add(new_process_refs, &cfqq->ref);
1865 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001866}
1867
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001868static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
1869 struct cfq_group *cfqg, enum wl_prio_t prio,
1870 bool prio_changed)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001871{
1872 struct cfq_queue *queue;
1873 int i;
1874 bool key_valid = false;
1875 unsigned long lowest_key = 0;
1876 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1877
1878 if (prio_changed) {
1879 /*
1880 * When priorities switched, we prefer starting
1881 * from SYNC_NOIDLE (first choice), or just SYNC
1882 * over ASYNC
1883 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001884 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001885 return cur_best;
1886 cur_best = SYNC_WORKLOAD;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001887 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001888 return cur_best;
1889
1890 return ASYNC_WORKLOAD;
1891 }
1892
1893 for (i = 0; i < 3; ++i) {
1894 /* otherwise, select the one with lowest rb_key */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001895 queue = cfq_rb_first(service_tree_for(cfqg, prio, i, cfqd));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001896 if (queue &&
1897 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1898 lowest_key = queue->rb_key;
1899 cur_best = i;
1900 key_valid = true;
1901 }
1902 }
1903
1904 return cur_best;
1905}
1906
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001907static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001908{
1909 enum wl_prio_t previous_prio = cfqd->serving_prio;
1910 bool prio_changed;
1911 unsigned slice;
1912 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001913 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001914 unsigned group_slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001915
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001916 if (!cfqg) {
1917 cfqd->serving_prio = IDLE_WORKLOAD;
1918 cfqd->workload_expires = jiffies + 1;
1919 return;
1920 }
1921
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001922 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001923 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001924 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001925 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001926 cfqd->serving_prio = BE_WORKLOAD;
1927 else {
1928 cfqd->serving_prio = IDLE_WORKLOAD;
1929 cfqd->workload_expires = jiffies + 1;
1930 return;
1931 }
1932
1933 /*
1934 * For RT and BE, we have to choose also the type
1935 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
1936 * expiration time
1937 */
1938 prio_changed = (cfqd->serving_prio != previous_prio);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001939 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
1940 cfqd);
1941 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001942
1943 /*
1944 * If priority didn't change, check workload expiration,
1945 * and that we still have other queues ready
1946 */
1947 if (!prio_changed && count &&
1948 !time_after(jiffies, cfqd->workload_expires))
1949 return;
1950
1951 /* otherwise select new workload type */
1952 cfqd->serving_type =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001953 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio, prio_changed);
1954 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
1955 cfqd);
1956 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001957
1958 /*
1959 * the workload slice is computed as a fraction of target latency
1960 * proportional to the number of queues in that workload, over
1961 * all the queues in the same priority class
1962 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001963 group_slice = cfq_group_slice(cfqd, cfqg);
1964
1965 slice = group_slice * count /
1966 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
1967 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001968
1969 if (cfqd->serving_type == ASYNC_WORKLOAD)
1970 /* async workload slice is scaled down according to
1971 * the sync/async slice ratio. */
1972 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
1973 else
1974 /* sync workload slice is at least 2 * cfq_slice_idle */
1975 slice = max(slice, 2 * cfqd->cfq_slice_idle);
1976
1977 slice = max_t(unsigned, slice, CFQ_MIN_TT);
1978 cfqd->workload_expires = jiffies + slice;
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001979 cfqd->noidle_tree_requires_idle = false;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001980}
1981
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001982static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
1983{
1984 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001985 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001986
1987 if (RB_EMPTY_ROOT(&st->rb))
1988 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001989 cfqg = cfq_rb_first_group(st);
1990 st->active = &cfqg->rb_node;
1991 update_min_vdisktime(st);
1992 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001993}
1994
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001995static void cfq_choose_cfqg(struct cfq_data *cfqd)
1996{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001997 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
1998
1999 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002000
2001 /* Restore the workload type data */
2002 if (cfqg->saved_workload_slice) {
2003 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2004 cfqd->serving_type = cfqg->saved_workload;
2005 cfqd->serving_prio = cfqg->saved_serving_prio;
2006 }
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002007 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002008}
2009
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002010/*
Jens Axboe498d3aa2007-04-26 12:54:48 +02002011 * Select a queue for service. If we have a current active queue,
2012 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002013 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002014static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002015{
Jens Axboea36e71f2009-04-15 12:15:11 +02002016 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002017
2018 cfqq = cfqd->active_queue;
2019 if (!cfqq)
2020 goto new_queue;
2021
Vivek Goyalf04a6422009-12-03 12:59:40 -05002022 if (!cfqd->rq_queued)
2023 return NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002024 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002025 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002026 */
Jens Axboeb0291952009-04-07 11:38:31 +02002027 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02002028 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02002029
2030 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002031 * The active queue has requests and isn't expired, allow it to
2032 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002033 */
Jens Axboedd67d052006-06-21 09:36:18 +02002034 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002035 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002036
2037 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002038 * If another queue has a request waiting within our mean seek
2039 * distance, let it run. The expire code will check for close
2040 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002041 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002042 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002043 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002044 if (new_cfqq) {
2045 if (!cfqq->new_cfqq)
2046 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002047 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002048 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002049
2050 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002051 * No requests pending. If the active queue still has requests in
2052 * flight or is idling for a new request, allow either of these
2053 * conditions to happen (or time out) before selecting a new queue.
2054 */
Jens Axboecc197472007-04-20 20:45:39 +02002055 if (timer_pending(&cfqd->idle_slice_timer) ||
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002056 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002057 cfqq = NULL;
2058 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002059 }
2060
Jens Axboe3b181522005-06-27 10:56:24 +02002061expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02002062 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002063new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002064 /*
2065 * Current queue expired. Check if we have to switch to a new
2066 * service tree
2067 */
2068 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002069 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002070
Jens Axboea36e71f2009-04-15 12:15:11 +02002071 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002072keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002073 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002074}
2075
Jens Axboefebffd62008-01-28 13:19:43 +01002076static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002077{
2078 int dispatched = 0;
2079
2080 while (cfqq->next_rq) {
2081 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2082 dispatched++;
2083 }
2084
2085 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002086
2087 /* By default cfqq is not expired if it is empty. Do it explicitly */
2088 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002089 return dispatched;
2090}
2091
Jens Axboe498d3aa2007-04-26 12:54:48 +02002092/*
2093 * Drain our current requests. Used for barriers and when switching
2094 * io schedulers on-the-fly.
2095 */
Jens Axboed9e76202007-04-20 14:27:50 +02002096static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002097{
Jens Axboe08717142008-01-28 11:38:15 +01002098 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002099 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002100
Vivek Goyalf04a6422009-12-03 12:59:40 -05002101 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL)
2102 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002103
Jens Axboe6084cdd2007-04-23 08:25:00 +02002104 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002105 BUG_ON(cfqd->busy_queues);
2106
Jeff Moyer69237152009-06-12 15:29:30 +02002107 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002108 return dispatched;
2109}
2110
Jens Axboe0b182d62009-10-06 20:49:37 +02002111static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002112{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002113 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Jens Axboe2f5cb732009-04-07 08:51:19 +02002115 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002116 * Drain async requests before we start sync IO
2117 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002118 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002119 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002120
2121 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002122 * If this is an async queue and we have sync IO in flight, let it wait
2123 */
2124 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002125 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002126
2127 max_dispatch = cfqd->cfq_quantum;
2128 if (cfq_class_idle(cfqq))
2129 max_dispatch = 1;
2130
2131 /*
2132 * Does this cfqq already have too much IO in flight?
2133 */
2134 if (cfqq->dispatched >= max_dispatch) {
2135 /*
2136 * idle queue must always only have a single IO in flight
2137 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002138 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002139 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002140
Jens Axboe2f5cb732009-04-07 08:51:19 +02002141 /*
2142 * We have other queues, don't allow more IO from this one
2143 */
2144 if (cfqd->busy_queues > 1)
Jens Axboe0b182d62009-10-06 20:49:37 +02002145 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002146
Jens Axboe2f5cb732009-04-07 08:51:19 +02002147 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002148 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002149 */
Shaohua Li474b18c2009-12-03 12:58:05 +01002150 max_dispatch = -1;
Jens Axboe8e296752009-10-03 16:26:03 +02002151 }
2152
2153 /*
2154 * Async queues must wait a bit before being allowed dispatch.
2155 * We also ramp up the dispatch depth gradually for async IO,
2156 * based on the last sync IO we serviced
2157 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002158 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jens Axboe8e296752009-10-03 16:26:03 +02002159 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
2160 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002161
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002162 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002163 if (!depth && !cfqq->dispatched)
2164 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002165 if (depth < max_dispatch)
2166 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
Jens Axboe0b182d62009-10-06 20:49:37 +02002169 /*
2170 * If we're below the current max, allow a dispatch
2171 */
2172 return cfqq->dispatched < max_dispatch;
2173}
2174
2175/*
2176 * Dispatch a request from cfqq, moving them to the request queue
2177 * dispatch list.
2178 */
2179static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2180{
2181 struct request *rq;
2182
2183 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2184
2185 if (!cfq_may_dispatch(cfqd, cfqq))
2186 return false;
2187
2188 /*
2189 * follow expired path, else get first next available
2190 */
2191 rq = cfq_check_fifo(cfqq);
2192 if (!rq)
2193 rq = cfqq->next_rq;
2194
2195 /*
2196 * insert request into driver dispatch list
2197 */
2198 cfq_dispatch_insert(cfqd->queue, rq);
2199
2200 if (!cfqd->active_cic) {
2201 struct cfq_io_context *cic = RQ_CIC(rq);
2202
2203 atomic_long_inc(&cic->ioc->refcount);
2204 cfqd->active_cic = cic;
2205 }
2206
2207 return true;
2208}
2209
2210/*
2211 * Find the cfqq that we need to service and move a request from that to the
2212 * dispatch list
2213 */
2214static int cfq_dispatch_requests(struct request_queue *q, int force)
2215{
2216 struct cfq_data *cfqd = q->elevator->elevator_data;
2217 struct cfq_queue *cfqq;
2218
2219 if (!cfqd->busy_queues)
2220 return 0;
2221
2222 if (unlikely(force))
2223 return cfq_forced_dispatch(cfqd);
2224
2225 cfqq = cfq_select_queue(cfqd);
2226 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002227 return 0;
2228
Jens Axboe2f5cb732009-04-07 08:51:19 +02002229 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002230 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002231 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002232 if (!cfq_dispatch_request(cfqd, cfqq))
2233 return 0;
2234
Jens Axboe2f5cb732009-04-07 08:51:19 +02002235 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002236 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002237
2238 /*
2239 * expire an async queue immediately if it has used up its slice. idle
2240 * queue always expire after 1 dispatch round.
2241 */
2242 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2243 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2244 cfq_class_idle(cfqq))) {
2245 cfqq->slice_end = jiffies + 1;
2246 cfq_slice_expired(cfqd, 0);
2247 }
2248
Shan Weib217a902009-09-01 10:06:42 +02002249 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002250 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253/*
Jens Axboe5e705372006-07-13 12:39:25 +02002254 * task holds one reference to the queue, dropped when task exits. each rq
2255 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002257 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * queue lock must be held here.
2259 */
2260static void cfq_put_queue(struct cfq_queue *cfqq)
2261{
Jens Axboe22e2c502005-06-27 10:55:12 +02002262 struct cfq_data *cfqd = cfqq->cfqd;
Vivek Goyalb1c35762009-12-03 12:59:47 -05002263 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002264
2265 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 if (!atomic_dec_and_test(&cfqq->ref))
2268 return;
2269
Jens Axboe7b679132008-05-30 12:23:07 +02002270 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002272 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002273 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002275 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02002276 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002277 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002278 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002279
Vivek Goyalf04a6422009-12-03 12:59:40 -05002280 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 kmem_cache_free(cfq_pool, cfqq);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002282 cfq_put_cfqg(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Jens Axboed6de8be2008-05-28 14:46:59 +02002285/*
2286 * Must always be called with the rcu_read_lock() held
2287 */
Jens Axboe07416d22008-05-07 09:17:12 +02002288static void
2289__call_for_each_cic(struct io_context *ioc,
2290 void (*func)(struct io_context *, struct cfq_io_context *))
2291{
2292 struct cfq_io_context *cic;
2293 struct hlist_node *n;
2294
2295 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
2296 func(ioc, cic);
2297}
2298
Jens Axboe4ac845a2008-01-24 08:44:49 +01002299/*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002300 * Call func for each cic attached to this ioc.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002301 */
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002302static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002303call_for_each_cic(struct io_context *ioc,
2304 void (*func)(struct io_context *, struct cfq_io_context *))
2305{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002306 rcu_read_lock();
Jens Axboe07416d22008-05-07 09:17:12 +02002307 __call_for_each_cic(ioc, func);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002308 rcu_read_unlock();
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002309}
Jens Axboe4ac845a2008-01-24 08:44:49 +01002310
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002311static void cfq_cic_free_rcu(struct rcu_head *head)
2312{
2313 struct cfq_io_context *cic;
2314
2315 cic = container_of(head, struct cfq_io_context, rcu_head);
2316
2317 kmem_cache_free(cfq_ioc_pool, cic);
Tejun Heo245b2e72009-06-24 15:13:48 +09002318 elv_ioc_count_dec(cfq_ioc_count);
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002319
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002320 if (ioc_gone) {
2321 /*
2322 * CFQ scheduler is exiting, grab exit lock and check
2323 * the pending io context count. If it hits zero,
2324 * complete ioc_gone and set it back to NULL
2325 */
2326 spin_lock(&ioc_gone_lock);
Tejun Heo245b2e72009-06-24 15:13:48 +09002327 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002328 complete(ioc_gone);
2329 ioc_gone = NULL;
2330 }
2331 spin_unlock(&ioc_gone_lock);
2332 }
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002333}
2334
2335static void cfq_cic_free(struct cfq_io_context *cic)
2336{
2337 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002338}
2339
2340static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
2341{
2342 unsigned long flags;
2343
2344 BUG_ON(!cic->dead_key);
2345
2346 spin_lock_irqsave(&ioc->lock, flags);
2347 radix_tree_delete(&ioc->radix_root, cic->dead_key);
Jens Axboeffc4e752008-02-19 10:02:29 +01002348 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002349 spin_unlock_irqrestore(&ioc->lock, flags);
2350
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002351 cfq_cic_free(cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002352}
2353
Jens Axboed6de8be2008-05-28 14:46:59 +02002354/*
2355 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
2356 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
2357 * and ->trim() which is called with the task lock held
2358 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002359static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002361 /*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002362 * ioc->refcount is zero here, or we are called from elv_unregister(),
2363 * so no more cic's are allowed to be linked into this ioc. So it
2364 * should be ok to iterate over the known list, we will see all cic's
2365 * since no new ones are added.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002366 */
Jens Axboe07416d22008-05-07 09:17:12 +02002367 __call_for_each_cic(ioc, cic_free_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Jens Axboe89850f72006-07-22 16:48:31 +02002370static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2371{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002372 struct cfq_queue *__cfqq, *next;
2373
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002374 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02002375 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002376 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002377 }
Jens Axboe89850f72006-07-22 16:48:31 +02002378
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002379 /*
2380 * If this queue was scheduled to merge with another queue, be
2381 * sure to drop the reference taken on that queue (and others in
2382 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2383 */
2384 __cfqq = cfqq->new_cfqq;
2385 while (__cfqq) {
2386 if (__cfqq == cfqq) {
2387 WARN(1, "cfqq->new_cfqq loop detected\n");
2388 break;
2389 }
2390 next = __cfqq->new_cfqq;
2391 cfq_put_queue(__cfqq);
2392 __cfqq = next;
2393 }
2394
Jens Axboe89850f72006-07-22 16:48:31 +02002395 cfq_put_queue(cfqq);
2396}
2397
2398static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
2399 struct cfq_io_context *cic)
2400{
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002401 struct io_context *ioc = cic->ioc;
2402
Jens Axboefc463792006-08-29 09:05:44 +02002403 list_del_init(&cic->queue_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002404
2405 /*
2406 * Make sure key == NULL is seen for dead queues
2407 */
Jens Axboefc463792006-08-29 09:05:44 +02002408 smp_wmb();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002409 cic->dead_key = (unsigned long) cic->key;
Jens Axboefc463792006-08-29 09:05:44 +02002410 cic->key = NULL;
2411
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002412 if (ioc->ioc_data == cic)
2413 rcu_assign_pointer(ioc->ioc_data, NULL);
2414
Jens Axboeff6657c2009-04-08 10:58:57 +02002415 if (cic->cfqq[BLK_RW_ASYNC]) {
2416 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2417 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002418 }
2419
Jens Axboeff6657c2009-04-08 10:58:57 +02002420 if (cic->cfqq[BLK_RW_SYNC]) {
2421 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2422 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002423 }
Jens Axboe89850f72006-07-22 16:48:31 +02002424}
2425
Jens Axboe4ac845a2008-01-24 08:44:49 +01002426static void cfq_exit_single_io_context(struct io_context *ioc,
2427 struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002428{
Al Viro478a82b2006-03-18 13:25:24 -05002429 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02002430
Jens Axboe89850f72006-07-22 16:48:31 +02002431 if (cfqd) {
Jens Axboe165125e2007-07-24 09:28:11 +02002432 struct request_queue *q = cfqd->queue;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002433 unsigned long flags;
Jens Axboe22e2c502005-06-27 10:55:12 +02002434
Jens Axboe4ac845a2008-01-24 08:44:49 +01002435 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe62c1fe92008-12-15 21:19:25 +01002436
2437 /*
2438 * Ensure we get a fresh copy of the ->key to prevent
2439 * race between exiting task and queue
2440 */
2441 smp_read_barrier_depends();
2442 if (cic->key)
2443 __cfq_exit_single_io_context(cfqd, cic);
2444
Jens Axboe4ac845a2008-01-24 08:44:49 +01002445 spin_unlock_irqrestore(q->queue_lock, flags);
Al Viro12a05732006-03-18 13:38:01 -05002446 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002447}
2448
Jens Axboe498d3aa2007-04-26 12:54:48 +02002449/*
2450 * The process that ioc belongs to has exited, we need to clean up
2451 * and put the internal structures we have that belongs to that process.
2452 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002453static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002455 call_for_each_cic(ioc, cfq_exit_single_io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Jens Axboe22e2c502005-06-27 10:55:12 +02002458static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04002459cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Jens Axboeb5deef92006-07-19 23:39:40 +02002461 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Christoph Lameter94f60302007-07-17 04:03:29 -07002463 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
2464 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002466 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02002467 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboeffc4e752008-02-19 10:02:29 +01002468 INIT_HLIST_NODE(&cic->cic_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02002469 cic->dtor = cfq_free_io_context;
2470 cic->exit = cfq_exit_io_context;
Tejun Heo245b2e72009-06-24 15:13:48 +09002471 elv_ioc_count_inc(cfq_ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473
2474 return cic;
2475}
2476
Jens Axboefd0928d2008-01-24 08:52:45 +01002477static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002478{
2479 struct task_struct *tsk = current;
2480 int ioprio_class;
2481
Jens Axboe3b181522005-06-27 10:56:24 +02002482 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002483 return;
2484
Jens Axboefd0928d2008-01-24 08:52:45 +01002485 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002486 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002487 default:
2488 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2489 case IOPRIO_CLASS_NONE:
2490 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002491 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002492 */
2493 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002494 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002495 break;
2496 case IOPRIO_CLASS_RT:
2497 cfqq->ioprio = task_ioprio(ioc);
2498 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2499 break;
2500 case IOPRIO_CLASS_BE:
2501 cfqq->ioprio = task_ioprio(ioc);
2502 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2503 break;
2504 case IOPRIO_CLASS_IDLE:
2505 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2506 cfqq->ioprio = 7;
2507 cfq_clear_cfqq_idle_window(cfqq);
2508 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002509 }
2510
2511 /*
2512 * keep track of original prio settings in case we have to temporarily
2513 * elevate the priority of this queue
2514 */
2515 cfqq->org_ioprio = cfqq->ioprio;
2516 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02002517 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002518}
2519
Jens Axboefebffd62008-01-28 13:19:43 +01002520static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002521{
Al Viro478a82b2006-03-18 13:25:24 -05002522 struct cfq_data *cfqd = cic->key;
2523 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01002524 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02002525
Jens Axboecaaa5f92006-06-16 11:23:00 +02002526 if (unlikely(!cfqd))
2527 return;
2528
Jens Axboec1b707d2006-10-30 19:54:23 +01002529 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002530
Jens Axboeff6657c2009-04-08 10:58:57 +02002531 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002532 if (cfqq) {
2533 struct cfq_queue *new_cfqq;
Jens Axboeff6657c2009-04-08 10:58:57 +02002534 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2535 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002536 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002537 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002538 cfq_put_queue(cfqq);
2539 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002540 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002541
Jens Axboeff6657c2009-04-08 10:58:57 +02002542 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002543 if (cfqq)
2544 cfq_mark_cfqq_prio_changed(cfqq);
2545
Jens Axboec1b707d2006-10-30 19:54:23 +01002546 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02002547}
2548
Jens Axboefc463792006-08-29 09:05:44 +02002549static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002551 call_for_each_cic(ioc, changed_ioprio);
Jens Axboefc463792006-08-29 09:05:44 +02002552 ioc->ioprio_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
Jens Axboed5036d72009-06-26 10:44:34 +02002555static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002556 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002557{
2558 RB_CLEAR_NODE(&cfqq->rb_node);
2559 RB_CLEAR_NODE(&cfqq->p_node);
2560 INIT_LIST_HEAD(&cfqq->fifo);
2561
2562 atomic_set(&cfqq->ref, 0);
2563 cfqq->cfqd = cfqd;
2564
2565 cfq_mark_cfqq_prio_changed(cfqq);
2566
2567 if (is_sync) {
2568 if (!cfq_class_idle(cfqq))
2569 cfq_mark_cfqq_idle_window(cfqq);
2570 cfq_mark_cfqq_sync(cfqq);
2571 }
2572 cfqq->pid = pid;
2573}
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002576cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002577 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002580 struct cfq_io_context *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002581 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583retry:
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002584 cfqg = cfq_get_cfqg(cfqd, 1);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002585 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002586 /* cic always exists here */
2587 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Jens Axboe6118b702009-06-30 09:34:12 +02002589 /*
2590 * Always try a new alloc if we fell back to the OOM cfqq
2591 * originally, since it should just be a temporary situation.
2592 */
2593 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2594 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (new_cfqq) {
2596 cfqq = new_cfqq;
2597 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002598 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002600 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002601 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002602 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002604 if (new_cfqq)
2605 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002606 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002607 cfqq = kmem_cache_alloc_node(cfq_pool,
2608 gfp_mask | __GFP_ZERO,
2609 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Jens Axboe6118b702009-06-30 09:34:12 +02002612 if (cfqq) {
2613 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2614 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002615 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002616 cfq_log_cfqq(cfqd, cfqq, "alloced");
2617 } else
2618 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620
2621 if (new_cfqq)
2622 kmem_cache_free(cfq_pool, new_cfqq);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return cfqq;
2625}
2626
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002627static struct cfq_queue **
2628cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2629{
Jens Axboefe094d92008-01-31 13:08:54 +01002630 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002631 case IOPRIO_CLASS_RT:
2632 return &cfqd->async_cfqq[0][ioprio];
2633 case IOPRIO_CLASS_BE:
2634 return &cfqd->async_cfqq[1][ioprio];
2635 case IOPRIO_CLASS_IDLE:
2636 return &cfqd->async_idle_cfqq;
2637 default:
2638 BUG();
2639 }
2640}
2641
Jens Axboe15c31be2007-07-10 13:43:25 +02002642static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002643cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002644 gfp_t gfp_mask)
2645{
Jens Axboefd0928d2008-01-24 08:52:45 +01002646 const int ioprio = task_ioprio(ioc);
2647 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002648 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002649 struct cfq_queue *cfqq = NULL;
2650
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002651 if (!is_sync) {
2652 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2653 cfqq = *async_cfqq;
2654 }
2655
Jens Axboe6118b702009-06-30 09:34:12 +02002656 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002657 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002658
2659 /*
2660 * pin the queue now that it's allocated, scheduler exit will prune it
2661 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002662 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02002663 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002664 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002665 }
2666
2667 atomic_inc(&cfqq->ref);
2668 return cfqq;
2669}
2670
Jens Axboe498d3aa2007-04-26 12:54:48 +02002671/*
2672 * We drop cfq io contexts lazily, so we may find a dead one.
2673 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002674static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002675cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2676 struct cfq_io_context *cic)
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002677{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002678 unsigned long flags;
2679
Jens Axboefc463792006-08-29 09:05:44 +02002680 WARN_ON(!list_empty(&cic->queue_list));
Jens Axboe597bc482007-04-24 21:23:53 +02002681
Jens Axboe4ac845a2008-01-24 08:44:49 +01002682 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe597bc482007-04-24 21:23:53 +02002683
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002684 BUG_ON(ioc->ioc_data == cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002685
2686 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
Jens Axboeffc4e752008-02-19 10:02:29 +01002687 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002688 spin_unlock_irqrestore(&ioc->lock, flags);
2689
2690 cfq_cic_free(cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002691}
2692
Jens Axboee2d74ac2006-03-28 08:59:01 +02002693static struct cfq_io_context *
Jens Axboe4ac845a2008-01-24 08:44:49 +01002694cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002695{
Jens Axboee2d74ac2006-03-28 08:59:01 +02002696 struct cfq_io_context *cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002697 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002698 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002699
Vasily Tarasov91fac312007-04-25 12:29:51 +02002700 if (unlikely(!ioc))
2701 return NULL;
2702
Jens Axboed6de8be2008-05-28 14:46:59 +02002703 rcu_read_lock();
2704
Jens Axboe597bc482007-04-24 21:23:53 +02002705 /*
2706 * we maintain a last-hit cache, to avoid browsing over the tree
2707 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002708 cic = rcu_dereference(ioc->ioc_data);
Jens Axboed6de8be2008-05-28 14:46:59 +02002709 if (cic && cic->key == cfqd) {
2710 rcu_read_unlock();
Jens Axboe597bc482007-04-24 21:23:53 +02002711 return cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002712 }
Jens Axboe597bc482007-04-24 21:23:53 +02002713
Jens Axboe4ac845a2008-01-24 08:44:49 +01002714 do {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002715 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
2716 rcu_read_unlock();
2717 if (!cic)
2718 break;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02002719 /* ->key must be copied to avoid race with cfq_exit_queue() */
2720 k = cic->key;
2721 if (unlikely(!k)) {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002722 cfq_drop_dead_cic(cfqd, ioc, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002723 rcu_read_lock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002724 continue;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002725 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002726
Jens Axboed6de8be2008-05-28 14:46:59 +02002727 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002728 rcu_assign_pointer(ioc->ioc_data, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002729 spin_unlock_irqrestore(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002730 break;
2731 } while (1);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002732
Jens Axboe4ac845a2008-01-24 08:44:49 +01002733 return cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002734}
2735
Jens Axboe4ac845a2008-01-24 08:44:49 +01002736/*
2737 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2738 * the process specific cfq io context when entered from the block layer.
2739 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2740 */
Jens Axboefebffd62008-01-28 13:19:43 +01002741static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2742 struct cfq_io_context *cic, gfp_t gfp_mask)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002743{
Jens Axboe0261d682006-10-30 19:07:48 +01002744 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002745 int ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002746
Jens Axboe4ac845a2008-01-24 08:44:49 +01002747 ret = radix_tree_preload(gfp_mask);
2748 if (!ret) {
2749 cic->ioc = ioc;
2750 cic->key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002751
Jens Axboe4ac845a2008-01-24 08:44:49 +01002752 spin_lock_irqsave(&ioc->lock, flags);
2753 ret = radix_tree_insert(&ioc->radix_root,
2754 (unsigned long) cfqd, cic);
Jens Axboeffc4e752008-02-19 10:02:29 +01002755 if (!ret)
2756 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002757 spin_unlock_irqrestore(&ioc->lock, flags);
2758
2759 radix_tree_preload_end();
2760
2761 if (!ret) {
2762 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2763 list_add(&cic->queue_list, &cfqd->cic_list);
2764 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002765 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002766 }
2767
Jens Axboe4ac845a2008-01-24 08:44:49 +01002768 if (ret)
2769 printk(KERN_ERR "cfq: cic link failed!\n");
Jens Axboefc463792006-08-29 09:05:44 +02002770
Jens Axboe4ac845a2008-01-24 08:44:49 +01002771 return ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002772}
2773
Jens Axboe22e2c502005-06-27 10:55:12 +02002774/*
2775 * Setup general io context and cfq io context. There can be several cfq
2776 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02002777 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02002778 */
2779static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02002780cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Jens Axboe22e2c502005-06-27 10:55:12 +02002782 struct io_context *ioc = NULL;
2783 struct cfq_io_context *cic;
2784
2785 might_sleep_if(gfp_mask & __GFP_WAIT);
2786
Jens Axboeb5deef92006-07-19 23:39:40 +02002787 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02002788 if (!ioc)
2789 return NULL;
2790
Jens Axboe4ac845a2008-01-24 08:44:49 +01002791 cic = cfq_cic_lookup(cfqd, ioc);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002792 if (cic)
2793 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02002794
Jens Axboee2d74ac2006-03-28 08:59:01 +02002795 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2796 if (cic == NULL)
2797 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02002798
Jens Axboe4ac845a2008-01-24 08:44:49 +01002799 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2800 goto err_free;
2801
Jens Axboe22e2c502005-06-27 10:55:12 +02002802out:
Jens Axboefc463792006-08-29 09:05:44 +02002803 smp_read_barrier_depends();
2804 if (unlikely(ioc->ioprio_changed))
2805 cfq_ioc_set_ioprio(ioc);
2806
Jens Axboe22e2c502005-06-27 10:55:12 +02002807 return cic;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002808err_free:
2809 cfq_cic_free(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02002810err:
2811 put_io_context(ioc);
2812 return NULL;
2813}
2814
2815static void
2816cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2817{
Jens Axboeaaf12282007-01-19 11:30:16 +11002818 unsigned long elapsed = jiffies - cic->last_end_request;
2819 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002820
2821 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2822 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2823 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2824}
2825
Jens Axboe206dc692006-03-28 13:03:44 +02002826static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002827cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002828 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002829{
2830 sector_t sdist;
2831 u64 total;
2832
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002833 if (!cfqq->last_request_pos)
Jeff Moyer4d00aa42009-04-21 07:25:04 +02002834 sdist = 0;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002835 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2836 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02002837 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002838 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
Jens Axboe206dc692006-03-28 13:03:44 +02002839
2840 /*
2841 * Don't allow the seek distance to get too large from the
2842 * odd fragment, pagein, etc
2843 */
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002844 if (cfqq->seek_samples <= 60) /* second&third seek */
2845 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
Jens Axboe206dc692006-03-28 13:03:44 +02002846 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002847 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
Jens Axboe206dc692006-03-28 13:03:44 +02002848
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002849 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2850 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2851 total = cfqq->seek_total + (cfqq->seek_samples/2);
2852 do_div(total, cfqq->seek_samples);
2853 cfqq->seek_mean = (sector_t)total;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002854
2855 /*
2856 * If this cfqq is shared between multiple processes, check to
2857 * make sure that those processes are still issuing I/Os within
2858 * the mean seek distance. If not, it may be time to break the
2859 * queues apart again.
2860 */
2861 if (cfq_cfqq_coop(cfqq)) {
2862 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2863 cfqq->seeky_start = jiffies;
2864 else if (!CFQQ_SEEKY(cfqq))
2865 cfqq->seeky_start = 0;
2866 }
Jens Axboe206dc692006-03-28 13:03:44 +02002867}
Jens Axboe22e2c502005-06-27 10:55:12 +02002868
2869/*
2870 * Disable idle window if the process thinks too long or seeks so much that
2871 * it doesn't matter
2872 */
2873static void
2874cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2875 struct cfq_io_context *cic)
2876{
Jens Axboe7b679132008-05-30 12:23:07 +02002877 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002878
Jens Axboe08717142008-01-28 11:38:15 +01002879 /*
2880 * Don't idle for async or idle io prio class
2881 */
2882 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002883 return;
2884
Jens Axboec265a7f2008-06-26 13:49:33 +02002885 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002886
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002887 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2888 cfq_mark_cfqq_deep(cfqq);
2889
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01002890 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002891 (!cfq_cfqq_deep(cfqq) && sample_valid(cfqq->seek_samples)
2892 && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002893 enable_idle = 0;
2894 else if (sample_valid(cic->ttime_samples)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002895 if (cic->ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002896 enable_idle = 0;
2897 else
2898 enable_idle = 1;
2899 }
2900
Jens Axboe7b679132008-05-30 12:23:07 +02002901 if (old_idle != enable_idle) {
2902 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2903 if (enable_idle)
2904 cfq_mark_cfqq_idle_window(cfqq);
2905 else
2906 cfq_clear_cfqq_idle_window(cfqq);
2907 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002908}
2909
Jens Axboe22e2c502005-06-27 10:55:12 +02002910/*
2911 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2912 * no or if we aren't sure, a 1 will cause a preempt.
2913 */
Jens Axboea6151c32009-10-07 20:02:57 +02002914static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002915cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02002916 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002917{
Jens Axboe6d048f52007-04-25 12:44:27 +02002918 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002919
Jens Axboe6d048f52007-04-25 12:44:27 +02002920 cfqq = cfqd->active_queue;
2921 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02002922 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002923
Jens Axboe6d048f52007-04-25 12:44:27 +02002924 if (cfq_slice_used(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002925 return true;
Jens Axboe6d048f52007-04-25 12:44:27 +02002926
2927 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002928 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002929
2930 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002931 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002932
Vivek Goyalf04a6422009-12-03 12:59:40 -05002933 /* Allow preemption only if we are idling on sync-noidle tree */
Corrado Zoccoloe4a22912009-11-26 10:02:58 +01002934 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2935 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyalf04a6422009-12-03 12:59:40 -05002936 new_cfqq->service_tree->count == 2 &&
2937 RB_EMPTY_ROOT(&cfqq->sort_list))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002938 return true;
2939
Jens Axboe22e2c502005-06-27 10:55:12 +02002940 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02002941 * if the new request is sync, but the currently running queue is
2942 * not, let the sync request have priority.
2943 */
Jens Axboe5e705372006-07-13 12:39:25 +02002944 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002945 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002946
Jens Axboe374f84a2006-07-23 01:42:19 +02002947 /*
2948 * So both queues are sync. Let the new request get disk time if
2949 * it's a metadata request and the current queue is doing regular IO.
2950 */
2951 if (rq_is_meta(rq) && !cfqq->meta_pending)
Jens Axboee6ec4fe2009-11-03 20:21:35 +01002952 return true;
Jens Axboe22e2c502005-06-27 10:55:12 +02002953
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002954 /*
2955 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2956 */
2957 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002958 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002959
Jens Axboe1e3335d2007-02-14 19:59:49 +01002960 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002961 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002962
2963 /*
2964 * if this request is as-good as one we would expect from the
2965 * current cfqq, let it preempt
2966 */
Jens Axboee00ef792009-11-04 08:54:55 +01002967 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002968 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002969
Jens Axboea6151c32009-10-07 20:02:57 +02002970 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002971}
2972
2973/*
2974 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2975 * let it have half of its nominal slice.
2976 */
2977static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2978{
Jens Axboe7b679132008-05-30 12:23:07 +02002979 cfq_log_cfqq(cfqd, cfqq, "preempt");
Jens Axboe6084cdd2007-04-23 08:25:00 +02002980 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02002981
Jens Axboebf572252006-07-19 20:29:12 +02002982 /*
2983 * Put the new queue at the front of the of the current list,
2984 * so we know that it will be selected next.
2985 */
2986 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02002987
2988 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02002989
Jens Axboe44f7c162007-01-19 11:51:58 +11002990 cfqq->slice_end = 0;
2991 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002992}
2993
2994/*
Jens Axboe5e705372006-07-13 12:39:25 +02002995 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02002996 * something we should do about it
2997 */
2998static void
Jens Axboe5e705372006-07-13 12:39:25 +02002999cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3000 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003001{
Jens Axboe5e705372006-07-13 12:39:25 +02003002 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003003
Aaron Carroll45333d52008-08-26 15:52:36 +02003004 cfqd->rq_queued++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003005 if (rq_is_meta(rq))
3006 cfqq->meta_pending++;
3007
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003008 cfq_update_io_thinktime(cfqd, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003009 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003010 cfq_update_idle_window(cfqd, cfqq, cic);
3011
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003012 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003013
3014 if (cfqq == cfqd->active_queue) {
3015 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003016 * Remember that we saw a request from this process, but
3017 * don't start queuing just yet. Otherwise we risk seeing lots
3018 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003019 * and merging. If the request is already larger than a single
3020 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003021 * merging is already done. Ditto for a busy system that
3022 * has other work pending, don't risk delaying until the
3023 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003024 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003025 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003026 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3027 cfqd->busy_queues > 1) {
Jens Axboed6ceb252009-04-14 14:18:16 +02003028 del_timer(&cfqd->idle_slice_timer);
Vivek Goyalbf791932009-12-03 12:59:37 -05003029 __blk_run_queue(cfqd->queue);
3030 } else
3031 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboed6ceb252009-04-14 14:18:16 +02003032 }
Jens Axboe5e705372006-07-13 12:39:25 +02003033 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003034 /*
3035 * not the active queue - expire current slice if it is
3036 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003037 * has some old slice time left and is of higher priority or
3038 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003039 */
3040 cfq_preempt_queue(cfqd, cfqq);
Tejun Heoa7f55792009-04-23 11:05:17 +09003041 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003042 }
3043}
3044
Jens Axboe165125e2007-07-24 09:28:11 +02003045static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003046{
Jens Axboeb4878f22005-10-20 16:42:29 +02003047 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003048 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003049
Jens Axboe7b679132008-05-30 12:23:07 +02003050 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Jens Axboefd0928d2008-01-24 08:52:45 +01003051 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Jens Axboe30996f42009-10-05 11:03:39 +02003053 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003054 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003055 cfq_add_rq_rb(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003056
Jens Axboe5e705372006-07-13 12:39:25 +02003057 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Aaron Carroll45333d52008-08-26 15:52:36 +02003060/*
3061 * Update hw_tag based on peak queue depth over 50 samples under
3062 * sufficient load.
3063 */
3064static void cfq_update_hw_tag(struct cfq_data *cfqd)
3065{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003066 struct cfq_queue *cfqq = cfqd->active_queue;
3067
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003068 if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
3069 cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
3070
3071 if (cfqd->hw_tag == 1)
3072 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003073
3074 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Jens Axboe5ad531d2009-07-03 12:57:48 +02003075 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003076 return;
3077
Shaohua Li1a1238a2009-10-27 08:46:23 +01003078 /*
3079 * If active queue hasn't enough requests and can idle, cfq might not
3080 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3081 * case
3082 */
3083 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3084 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
3085 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
3086 return;
3087
Aaron Carroll45333d52008-08-26 15:52:36 +02003088 if (cfqd->hw_tag_samples++ < 50)
3089 return;
3090
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003091 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003092 cfqd->hw_tag = 1;
3093 else
3094 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003095}
3096
Jens Axboe165125e2007-07-24 09:28:11 +02003097static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Jens Axboe5e705372006-07-13 12:39:25 +02003099 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003100 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003101 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003102 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Jens Axboeb4878f22005-10-20 16:42:29 +02003104 now = jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02003105 cfq_log_cfqq(cfqd, cfqq, "complete");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Aaron Carroll45333d52008-08-26 15:52:36 +02003107 cfq_update_hw_tag(cfqd);
3108
Jens Axboe5ad531d2009-07-03 12:57:48 +02003109 WARN_ON(!cfqd->rq_in_driver[sync]);
Jens Axboe6d048f52007-04-25 12:44:27 +02003110 WARN_ON(!cfqq->dispatched);
Jens Axboe5ad531d2009-07-03 12:57:48 +02003111 cfqd->rq_in_driver[sync]--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003112 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Jens Axboe3ed9a292007-04-23 08:33:33 +02003114 if (cfq_cfqq_sync(cfqq))
3115 cfqd->sync_flight--;
3116
Vivek Goyal365722b2009-10-03 15:21:27 +02003117 if (sync) {
Jens Axboe5e705372006-07-13 12:39:25 +02003118 RQ_CIC(rq)->last_end_request = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003119 cfqd->last_end_sync_rq = now;
3120 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003121
3122 /*
3123 * If this is the active queue, check if it needs to be expired,
3124 * or if we want to idle in case it has no pending requests.
3125 */
3126 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003127 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3128
Jens Axboe44f7c162007-01-19 11:51:58 +11003129 if (cfq_cfqq_slice_new(cfqq)) {
3130 cfq_set_prio_slice(cfqd, cfqq);
3131 cfq_clear_cfqq_slice_new(cfqq);
3132 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003133 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003134 * Idling is not enabled on:
3135 * - expired queues
3136 * - idle-priority queues
3137 * - async queues
3138 * - queues with still some requests queued
3139 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003140 */
Jens Axboe08717142008-01-28 11:38:15 +01003141 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02003142 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003143 else if (sync && cfqq_empty &&
3144 !cfq_close_cooperator(cfqd, cfqq)) {
3145 cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
3146 /*
3147 * Idling is enabled for SYNC_WORKLOAD.
3148 * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
3149 * only if we processed at least one !rq_noidle request
3150 */
3151 if (cfqd->serving_type == SYNC_WORKLOAD
3152 || cfqd->noidle_tree_requires_idle)
3153 cfq_arm_slice_timer(cfqd);
3154 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003155 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003156
Jens Axboe5ad531d2009-07-03 12:57:48 +02003157 if (!rq_in_driver(cfqd))
Jens Axboe23e018a2009-10-05 08:52:35 +02003158 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Jens Axboe22e2c502005-06-27 10:55:12 +02003161/*
3162 * we temporarily boost lower priority queues if they are holding fs exclusive
3163 * resources. they are boosted to normal prio (CLASS_BE/4)
3164 */
3165static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
Jens Axboe22e2c502005-06-27 10:55:12 +02003167 if (has_fs_excl()) {
3168 /*
3169 * boost idle prio on transactions that would lock out other
3170 * users of the filesystem
3171 */
3172 if (cfq_class_idle(cfqq))
3173 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3174 if (cfqq->ioprio > IOPRIO_NORM)
3175 cfqq->ioprio = IOPRIO_NORM;
3176 } else {
3177 /*
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003178 * unboost the queue (if needed)
Jens Axboe22e2c502005-06-27 10:55:12 +02003179 */
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003180 cfqq->ioprio_class = cfqq->org_ioprio_class;
3181 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003183}
3184
Jens Axboe89850f72006-07-22 16:48:31 +02003185static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003186{
Jens Axboe1b379d82009-08-11 08:26:11 +02003187 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003188 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003189 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003190 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003191
3192 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003193}
3194
Jens Axboe165125e2007-07-24 09:28:11 +02003195static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003196{
3197 struct cfq_data *cfqd = q->elevator->elevator_data;
3198 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02003199 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003200 struct cfq_queue *cfqq;
3201
3202 /*
3203 * don't force setup of a queue from here, as a call to may_queue
3204 * does not necessarily imply that a request actually will be queued.
3205 * so just lookup a possibly existing queue, or return 'may queue'
3206 * if that fails
3207 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003208 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003209 if (!cic)
3210 return ELV_MQUEUE_MAY;
3211
Jens Axboeb0b78f82009-04-08 10:56:08 +02003212 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003213 if (cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003214 cfq_init_prio_data(cfqq, cic->ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003215 cfq_prio_boost(cfqq);
3216
Jens Axboe89850f72006-07-22 16:48:31 +02003217 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003218 }
3219
3220 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223/*
3224 * queue lock held here
3225 */
Jens Axboebb37b942006-12-01 10:42:33 +01003226static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Jens Axboe5e705372006-07-13 12:39:25 +02003228 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Jens Axboe5e705372006-07-13 12:39:25 +02003230 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003231 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Jens Axboe22e2c502005-06-27 10:55:12 +02003233 BUG_ON(!cfqq->allocated[rw]);
3234 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Jens Axboe5e705372006-07-13 12:39:25 +02003236 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02003239 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 cfq_put_queue(cfqq);
3242 }
3243}
3244
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003245static struct cfq_queue *
3246cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
3247 struct cfq_queue *cfqq)
3248{
3249 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3250 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003251 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003252 cfq_put_queue(cfqq);
3253 return cic_to_cfqq(cic, 1);
3254}
3255
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003256static int should_split_cfqq(struct cfq_queue *cfqq)
3257{
3258 if (cfqq->seeky_start &&
3259 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
3260 return 1;
3261 return 0;
3262}
3263
3264/*
3265 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3266 * was the last process referring to said cfqq.
3267 */
3268static struct cfq_queue *
3269split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
3270{
3271 if (cfqq_process_refs(cfqq) == 1) {
3272 cfqq->seeky_start = 0;
3273 cfqq->pid = current->pid;
3274 cfq_clear_cfqq_coop(cfqq);
3275 return cfqq;
3276 }
3277
3278 cic_set_cfqq(cic, NULL, 1);
3279 cfq_put_queue(cfqq);
3280 return NULL;
3281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003283 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003285static int
Jens Axboe165125e2007-07-24 09:28:11 +02003286cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
3288 struct cfq_data *cfqd = q->elevator->elevator_data;
3289 struct cfq_io_context *cic;
3290 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003291 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003292 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 unsigned long flags;
3294
3295 might_sleep_if(gfp_mask & __GFP_WAIT);
3296
Jens Axboee2d74ac2006-03-28 08:59:01 +02003297 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 spin_lock_irqsave(q->queue_lock, flags);
3300
Jens Axboe22e2c502005-06-27 10:55:12 +02003301 if (!cic)
3302 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003304new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003305 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003306 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003307 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003308 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003309 } else {
3310 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003311 * If the queue was seeky for too long, break it apart.
3312 */
3313 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
3314 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3315 cfqq = split_cfqq(cic, cfqq);
3316 if (!cfqq)
3317 goto new_queue;
3318 }
3319
3320 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003321 * Check to see if this queue is scheduled to merge with
3322 * another, closely cooperating queue. The merging of
3323 * queues happens here as it must be done in process context.
3324 * The reference on new_cfqq was taken in merge_cfqqs.
3325 */
3326 if (cfqq->new_cfqq)
3327 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 cfqq->allocated[rw]++;
Jens Axboe22e2c502005-06-27 10:55:12 +02003331 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 spin_unlock_irqrestore(q->queue_lock, flags);
3334
Jens Axboe5e705372006-07-13 12:39:25 +02003335 rq->elevator_private = cic;
3336 rq->elevator_private2 = cfqq;
3337 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02003338
Jens Axboe22e2c502005-06-27 10:55:12 +02003339queue_fail:
3340 if (cic)
3341 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02003342
Jens Axboe23e018a2009-10-05 08:52:35 +02003343 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe7b679132008-05-30 12:23:07 +02003345 cfq_log(cfqd, "set_request fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return 1;
3347}
3348
David Howells65f27f32006-11-22 14:55:48 +00003349static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003350{
David Howells65f27f32006-11-22 14:55:48 +00003351 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003352 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003353 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003354
Jens Axboe40bb54d2009-04-15 12:11:10 +02003355 spin_lock_irq(q->queue_lock);
Tejun Heoa7f55792009-04-23 11:05:17 +09003356 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003357 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003358}
3359
3360/*
3361 * Timer running if the active_queue is currently idling inside its time slice
3362 */
3363static void cfq_idle_slice_timer(unsigned long data)
3364{
3365 struct cfq_data *cfqd = (struct cfq_data *) data;
3366 struct cfq_queue *cfqq;
3367 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003368 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003369
Jens Axboe7b679132008-05-30 12:23:07 +02003370 cfq_log(cfqd, "idle timer fired");
3371
Jens Axboe22e2c502005-06-27 10:55:12 +02003372 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3373
Jens Axboefe094d92008-01-31 13:08:54 +01003374 cfqq = cfqd->active_queue;
3375 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003376 timed_out = 0;
3377
Jens Axboe22e2c502005-06-27 10:55:12 +02003378 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003379 * We saw a request before the queue expired, let it through
3380 */
3381 if (cfq_cfqq_must_dispatch(cfqq))
3382 goto out_kick;
3383
3384 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003385 * expired
3386 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003387 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003388 goto expire;
3389
3390 /*
3391 * only expire and reinvoke request handler, if there are
3392 * other queues with pending requests
3393 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003394 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003395 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003396
3397 /*
3398 * not expired and it has a request pending, let it dispatch
3399 */
Jens Axboe75e50982009-04-07 08:56:14 +02003400 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003401 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003402
3403 /*
3404 * Queue depth flag is reset only when the idle didn't succeed
3405 */
3406 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003407 }
3408expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02003409 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003410out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003411 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003412out_cont:
3413 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3414}
3415
Jens Axboe3b181522005-06-27 10:56:24 +02003416static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3417{
3418 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003419 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003420}
Jens Axboe22e2c502005-06-27 10:55:12 +02003421
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003422static void cfq_put_async_queues(struct cfq_data *cfqd)
3423{
3424 int i;
3425
3426 for (i = 0; i < IOPRIO_BE_NR; i++) {
3427 if (cfqd->async_cfqq[0][i])
3428 cfq_put_queue(cfqd->async_cfqq[0][i]);
3429 if (cfqd->async_cfqq[1][i])
3430 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003431 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003432
3433 if (cfqd->async_idle_cfqq)
3434 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003435}
3436
Jens Axboeb374d182008-10-31 10:05:07 +01003437static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Jens Axboe22e2c502005-06-27 10:55:12 +02003439 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003440 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003441
Jens Axboe3b181522005-06-27 10:56:24 +02003442 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003443
Al Virod9ff4182006-03-18 13:51:22 -05003444 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003445
Al Virod9ff4182006-03-18 13:51:22 -05003446 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02003447 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003448
3449 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05003450 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
3451 struct cfq_io_context,
3452 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02003453
3454 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05003455 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02003456
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003457 cfq_put_async_queues(cfqd);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003458 cfq_release_cfq_groups(cfqd);
3459 blkiocg_del_blkio_group(&cfqd->root_group.blkg);
Jens Axboe15c31be2007-07-10 13:43:25 +02003460
Al Virod9ff4182006-03-18 13:51:22 -05003461 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05003462
3463 cfq_shutdown_timer_wq(cfqd);
3464
Vivek Goyalb1c35762009-12-03 12:59:47 -05003465 /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
3466 synchronize_rcu();
Al Viroa90d7422006-03-18 12:05:37 -05003467 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
Jens Axboe165125e2007-07-24 09:28:11 +02003470static void *cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
3472 struct cfq_data *cfqd;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003473 int i, j;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003474 struct cfq_group *cfqg;
Vivek Goyal615f0252009-12-03 12:59:39 -05003475 struct cfq_rb_root *st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Christoph Lameter94f60302007-07-17 04:03:29 -07003477 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02003479 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003481 /* Init root service tree */
3482 cfqd->grp_service_tree = CFQ_RB_ROOT;
3483
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003484 /* Init root group */
3485 cfqg = &cfqd->root_group;
Vivek Goyal615f0252009-12-03 12:59:39 -05003486 for_each_cfqg_st(cfqg, i, j, st)
3487 *st = CFQ_RB_ROOT;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003488 RB_CLEAR_NODE(&cfqg->rb_node);
Jens Axboe26a2ac02009-04-23 12:13:27 +02003489
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003490 /* Give preference to root group over other groups */
3491 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3492
Vivek Goyal25fb5162009-12-03 12:59:46 -05003493#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyalb1c35762009-12-03 12:59:47 -05003494 /*
3495 * Take a reference to root group which we never drop. This is just
3496 * to make sure that cfq_put_cfqg() does not try to kfree root group
3497 */
3498 atomic_set(&cfqg->ref, 1);
Vivek Goyal25fb5162009-12-03 12:59:46 -05003499 blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd);
3500#endif
Jens Axboe26a2ac02009-04-23 12:13:27 +02003501 /*
3502 * Not strictly needed (since RB_ROOT just clears the node and we
3503 * zeroed cfqd on alloc), but better be safe in case someone decides
3504 * to add magic to the rb code
3505 */
3506 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3507 cfqd->prio_trees[i] = RB_ROOT;
3508
Jens Axboe6118b702009-06-30 09:34:12 +02003509 /*
3510 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3511 * Grab a permanent reference to it, so that the normal code flow
3512 * will not attempt to free it.
3513 */
3514 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3515 atomic_inc(&cfqd->oom_cfqq.ref);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003516 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
Jens Axboe6118b702009-06-30 09:34:12 +02003517
Al Virod9ff4182006-03-18 13:51:22 -05003518 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Jens Axboe22e2c502005-06-27 10:55:12 +02003522 init_timer(&cfqd->idle_slice_timer);
3523 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3524 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3525
Jens Axboe23e018a2009-10-05 08:52:35 +02003526 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003529 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3530 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 cfqd->cfq_back_max = cfq_back_max;
3532 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003533 cfqd->cfq_slice[0] = cfq_slice_async;
3534 cfqd->cfq_slice[1] = cfq_slice_sync;
3535 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3536 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003537 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003538 cfqd->hw_tag = -1;
Vivek Goyal365722b2009-10-03 15:21:27 +02003539 cfqd->last_end_sync_rq = jiffies;
Jens Axboebc1c1162006-06-08 08:49:06 +02003540 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
3543static void cfq_slab_kill(void)
3544{
Jens Axboed6de8be2008-05-28 14:46:59 +02003545 /*
3546 * Caller already ensured that pending RCU callbacks are completed,
3547 * so we should have no busy allocations at this point.
3548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 if (cfq_pool)
3550 kmem_cache_destroy(cfq_pool);
3551 if (cfq_ioc_pool)
3552 kmem_cache_destroy(cfq_ioc_pool);
3553}
3554
3555static int __init cfq_slab_setup(void)
3556{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003557 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 if (!cfq_pool)
3559 goto fail;
3560
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02003561 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 if (!cfq_ioc_pool)
3563 goto fail;
3564
3565 return 0;
3566fail:
3567 cfq_slab_kill();
3568 return -ENOMEM;
3569}
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571/*
3572 * sysfs parts below -->
3573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574static ssize_t
3575cfq_var_show(unsigned int var, char *page)
3576{
3577 return sprintf(page, "%d\n", var);
3578}
3579
3580static ssize_t
3581cfq_var_store(unsigned int *var, const char *page, size_t count)
3582{
3583 char *p = (char *) page;
3584
3585 *var = simple_strtoul(p, &p, 10);
3586 return count;
3587}
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003590static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003592 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 unsigned int __data = __VAR; \
3594 if (__CONV) \
3595 __data = jiffies_to_msecs(__data); \
3596 return cfq_var_show(__data, (page)); \
3597}
3598SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003599SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3600SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003601SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3602SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003603SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3604SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3605SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3606SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003607SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608#undef SHOW_FUNCTION
3609
3610#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003611static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003613 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 unsigned int __data; \
3615 int ret = cfq_var_store(&__data, (page), count); \
3616 if (__data < (MIN)) \
3617 __data = (MIN); \
3618 else if (__data > (MAX)) \
3619 __data = (MAX); \
3620 if (__CONV) \
3621 *(__PTR) = msecs_to_jiffies(__data); \
3622 else \
3623 *(__PTR) = __data; \
3624 return ret; \
3625}
3626STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003627STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3628 UINT_MAX, 1);
3629STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3630 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003631STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003632STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3633 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003634STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3635STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3636STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003637STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3638 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003639STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640#undef STORE_FUNCTION
3641
Al Viroe572ec72006-03-18 22:27:18 -05003642#define CFQ_ATTR(name) \
3643 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003644
Al Viroe572ec72006-03-18 22:27:18 -05003645static struct elv_fs_entry cfq_attrs[] = {
3646 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003647 CFQ_ATTR(fifo_expire_sync),
3648 CFQ_ATTR(fifo_expire_async),
3649 CFQ_ATTR(back_seek_max),
3650 CFQ_ATTR(back_seek_penalty),
3651 CFQ_ATTR(slice_sync),
3652 CFQ_ATTR(slice_async),
3653 CFQ_ATTR(slice_async_rq),
3654 CFQ_ATTR(slice_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003655 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003656 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657};
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659static struct elevator_type iosched_cfq = {
3660 .ops = {
3661 .elevator_merge_fn = cfq_merge,
3662 .elevator_merged_fn = cfq_merged_request,
3663 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003664 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02003665 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003667 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 .elevator_deactivate_req_fn = cfq_deactivate_request,
3669 .elevator_queue_empty_fn = cfq_queue_empty,
3670 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003671 .elevator_former_req_fn = elv_rb_former_request,
3672 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 .elevator_set_req_fn = cfq_set_request,
3674 .elevator_put_req_fn = cfq_put_request,
3675 .elevator_may_queue_fn = cfq_may_queue,
3676 .elevator_init_fn = cfq_init_queue,
3677 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02003678 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 },
Al Viro3d1ab402006-03-18 18:35:43 -05003680 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 .elevator_name = "cfq",
3682 .elevator_owner = THIS_MODULE,
3683};
3684
3685static int __init cfq_init(void)
3686{
Jens Axboe22e2c502005-06-27 10:55:12 +02003687 /*
3688 * could be 0 on HZ < 1000 setups
3689 */
3690 if (!cfq_slice_async)
3691 cfq_slice_async = 1;
3692 if (!cfq_slice_idle)
3693 cfq_slice_idle = 1;
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 if (cfq_slab_setup())
3696 return -ENOMEM;
3697
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003698 elv_register(&iosched_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
3703static void __exit cfq_exit(void)
3704{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07003705 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05003707 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02003708 /* ioc_gone's update must be visible before reading ioc_count */
3709 smp_wmb();
Jens Axboed6de8be2008-05-28 14:46:59 +02003710
3711 /*
3712 * this also protects us from entering cfq_slab_kill() with
3713 * pending RCU callbacks
3714 */
Tejun Heo245b2e72009-06-24 15:13:48 +09003715 if (elv_ioc_count_read(cfq_ioc_count))
Jens Axboe9a11b4e2008-05-29 09:32:08 +02003716 wait_for_completion(&all_gone);
Christoph Hellwig83521d32005-10-30 15:01:39 -08003717 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
3719
3720module_init(cfq_init);
3721module_exit(cfq_exit);
3722
3723MODULE_AUTHOR("Jens Axboe");
3724MODULE_LICENSE("GPL");
3725MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");